From: Sage Weil <sage@newdream.net>
To: linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org
Cc: Sage Weil <sage@newdream.net>
Subject: [PATCH 05/26] ceph: ref counted buffer
Date: Tue, 2 Mar 2010 16:46:36 -0800 [thread overview]
Message-ID: <1267577217-31923-6-git-send-email-sage@newdream.net> (raw)
In-Reply-To: <1267577217-31923-1-git-send-email-sage@newdream.net>
struct ceph_buffer is a simple ref-counted buffer. We transparently
choose between kmalloc for small buffers and vmalloc for large ones.
This is currently used only for allocating memory for xattr data.
Signed-off-by: Sage Weil <sage@newdream.net>
---
fs/ceph/buffer.c | 78 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
fs/ceph/buffer.h | 39 +++++++++++++++++++++++++++
2 files changed, 117 insertions(+), 0 deletions(-)
create mode 100644 fs/ceph/buffer.c
create mode 100644 fs/ceph/buffer.h
diff --git a/fs/ceph/buffer.c b/fs/ceph/buffer.c
new file mode 100644
index 0000000..b98086c
--- /dev/null
+++ b/fs/ceph/buffer.c
@@ -0,0 +1,78 @@
+
+#include "ceph_debug.h"
+#include "buffer.h"
+#include "decode.h"
+
+struct ceph_buffer *ceph_buffer_new(size_t len, gfp_t gfp)
+{
+ struct ceph_buffer *b;
+
+ b = kmalloc(sizeof(*b), gfp);
+ if (!b)
+ return NULL;
+
+ b->vec.iov_base = kmalloc(len, gfp | __GFP_NOWARN);
+ if (b->vec.iov_base) {
+ b->is_vmalloc = false;
+ } else {
+ b->vec.iov_base = __vmalloc(len, gfp, PAGE_KERNEL);
+ if (!b->vec.iov_base) {
+ kfree(b);
+ return NULL;
+ }
+ b->is_vmalloc = true;
+ }
+
+ kref_init(&b->kref);
+ b->alloc_len = len;
+ b->vec.iov_len = len;
+ dout("buffer_new %p\n", b);
+ return b;
+}
+
+void ceph_buffer_release(struct kref *kref)
+{
+ struct ceph_buffer *b = container_of(kref, struct ceph_buffer, kref);
+
+ dout("buffer_release %p\n", b);
+ if (b->vec.iov_base) {
+ if (b->is_vmalloc)
+ vfree(b->vec.iov_base);
+ else
+ kfree(b->vec.iov_base);
+ }
+ kfree(b);
+}
+
+int ceph_buffer_alloc(struct ceph_buffer *b, int len, gfp_t gfp)
+{
+ b->vec.iov_base = kmalloc(len, gfp | __GFP_NOWARN);
+ if (b->vec.iov_base) {
+ b->is_vmalloc = false;
+ } else {
+ b->vec.iov_base = __vmalloc(len, gfp, PAGE_KERNEL);
+ b->is_vmalloc = true;
+ }
+ if (!b->vec.iov_base)
+ return -ENOMEM;
+ b->alloc_len = len;
+ b->vec.iov_len = len;
+ return 0;
+}
+
+int ceph_decode_buffer(struct ceph_buffer **b, void **p, void *end)
+{
+ size_t len;
+
+ ceph_decode_need(p, end, sizeof(u32), bad);
+ len = ceph_decode_32(p);
+ dout("decode_buffer len %d\n", (int)len);
+ ceph_decode_need(p, end, len, bad);
+ *b = ceph_buffer_new(len, GFP_NOFS);
+ if (!*b)
+ return -ENOMEM;
+ ceph_decode_copy(p, (*b)->vec.iov_base, len);
+ return 0;
+bad:
+ return -EINVAL;
+}
diff --git a/fs/ceph/buffer.h b/fs/ceph/buffer.h
new file mode 100644
index 0000000..58d1901
--- /dev/null
+++ b/fs/ceph/buffer.h
@@ -0,0 +1,39 @@
+#ifndef __FS_CEPH_BUFFER_H
+#define __FS_CEPH_BUFFER_H
+
+#include <linux/kref.h>
+#include <linux/mm.h>
+#include <linux/vmalloc.h>
+#include <linux/types.h>
+#include <linux/uio.h>
+
+/*
+ * a simple reference counted buffer.
+ *
+ * use kmalloc for small sizes (<= one page), vmalloc for larger
+ * sizes.
+ */
+struct ceph_buffer {
+ struct kref kref;
+ struct kvec vec;
+ size_t alloc_len;
+ bool is_vmalloc;
+};
+
+extern struct ceph_buffer *ceph_buffer_new(size_t len, gfp_t gfp);
+extern void ceph_buffer_release(struct kref *kref);
+
+static inline struct ceph_buffer *ceph_buffer_get(struct ceph_buffer *b)
+{
+ kref_get(&b->kref);
+ return b;
+}
+
+static inline void ceph_buffer_put(struct ceph_buffer *b)
+{
+ kref_put(&b->kref, ceph_buffer_release);
+}
+
+extern int ceph_decode_buffer(struct ceph_buffer **b, void **p, void *end);
+
+#endif
--
1.7.0
next prev parent reply other threads:[~2010-03-03 0:46 UTC|newest]
Thread overview: 30+ messages / expand[flat|nested] mbox.gz Atom feed top
2010-03-03 0:46 [PATCH 00/26] ceph distributed file system client Sage Weil
2010-03-03 0:46 ` [PATCH 01/26] ceph: documentation Sage Weil
2010-03-03 0:46 ` [PATCH 02/26] ceph: on-wire types Sage Weil
2010-03-03 0:46 ` [PATCH 03/26] ceph: client types Sage Weil
2010-03-03 0:46 ` [PATCH 04/26] ceph: hash function Sage Weil
2010-03-03 0:46 ` Sage Weil [this message]
2010-03-03 0:46 ` [PATCH 06/26] ceph: dynamic pagelist buffer Sage Weil
2010-03-03 0:46 ` [PATCH 07/26] ceph: super.c Sage Weil
2010-03-03 0:46 ` [PATCH 08/26] ceph: inode operations Sage Weil
2010-03-03 0:46 ` [PATCH 09/26] ceph: directory operations Sage Weil
2010-03-03 0:46 ` [PATCH 10/26] ceph: file operations Sage Weil
2010-03-03 0:46 ` [PATCH 11/26] ceph: address space operations Sage Weil
2010-03-03 0:46 ` [PATCH 12/26] ceph: MDS client Sage Weil
2010-03-03 0:46 ` [PATCH 13/26] ceph: OSD client Sage Weil
2010-03-03 0:46 ` [PATCH 14/26] ceph: CRUSH mapping algorithm Sage Weil
2010-03-03 0:46 ` [PATCH 15/26] ceph: monitor client Sage Weil
2010-03-03 0:46 ` [PATCH 16/26] ceph: authentication interface Sage Weil
2010-03-03 0:46 ` [PATCH 17/26] ceph: trivial 'auth_none' authentication scheme Sage Weil
2010-03-03 0:46 ` [PATCH 18/26] ceph: 'auth_x' " Sage Weil
2010-03-03 0:46 ` [PATCH 19/26] ceph: capability management Sage Weil
2010-03-03 0:46 ` [PATCH 20/26] ceph: snapshot management Sage Weil
2010-03-03 0:46 ` [PATCH 21/26] ceph: messenger library Sage Weil
2010-03-03 0:46 ` [PATCH 22/26] ceph: message pools Sage Weil
2010-03-03 0:46 ` [PATCH 23/26] ceph: nfs re-export support Sage Weil
2010-03-03 7:48 ` Christoph Hellwig
2010-03-03 21:37 ` Sage Weil
2010-03-04 11:50 ` Miklos Szeredi
2010-03-03 0:46 ` [PATCH 24/26] ceph: ioctls Sage Weil
2010-03-03 0:46 ` [PATCH 25/26] ceph: debugfs Sage Weil
2010-03-03 0:46 ` [PATCH 26/26] ceph: Kconfig, Makefile, MAINTAINERS entry Sage Weil
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=1267577217-31923-6-git-send-email-sage@newdream.net \
--to=sage@newdream.net \
--cc=linux-fsdevel@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).