linux-fsdevel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
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 04/20] ceph: ref counted buffer
Date: Tue,  8 Sep 2009 15:56:25 -0700	[thread overview]
Message-ID: <1252450601-17610-5-git-send-email-sage@newdream.net> (raw)
In-Reply-To: <1252450601-17610-4-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 used for allocating memory for xattr data, among other things.

Signed-off-by: Sage Weil <sage@newdream.net>
---
 fs/ceph/buffer.h |   93 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 93 insertions(+), 0 deletions(-)
 create mode 100644 fs/ceph/buffer.h

diff --git a/fs/ceph/buffer.h b/fs/ceph/buffer.h
new file mode 100644
index 0000000..b4ccb94
--- /dev/null
+++ b/fs/ceph/buffer.h
@@ -0,0 +1,93 @@
+#ifndef __FS_CEPH_BUFFER_H
+#define __FS_CEPH_BUFFER_H
+
+#include <linux/mm.h>
+#include <linux/types.h>
+#include <linux/vmalloc.h>
+
+#include "ceph_debug.h"
+
+/*
+ * a simple reference counted buffer.
+ *
+ * use kmalloc for small sizes (<= one page), vmalloc for larger
+ * sizes.
+ */
+struct ceph_buffer {
+	atomic_t nref;
+	struct kvec vec;
+	size_t alloc_len;
+	bool is_static, is_vmalloc;
+};
+
+static inline void ceph_buffer_init_static(struct ceph_buffer *b)
+{
+	atomic_set(&b->nref, 1);
+	b->vec.iov_base = NULL;
+	b->vec.iov_len = 0;
+	b->alloc_len = 0;
+	b->is_static = true;
+}
+
+static inline struct ceph_buffer *ceph_buffer_new(gfp_t gfp)
+{
+	struct ceph_buffer *b;
+
+	b = kmalloc(sizeof(*b), gfp);
+	if (!b)
+		return NULL;
+	atomic_set(&b->nref, 1);
+	b->vec.iov_base = NULL;
+	b->vec.iov_len = 0;
+	b->alloc_len = 0;
+	b->is_static = false;
+	return b;
+}
+
+static inline int ceph_buffer_alloc(struct ceph_buffer *b, int len, gfp_t gfp)
+{
+	if (len <= PAGE_SIZE) {
+		b->vec.iov_base = kmalloc(len, gfp);
+		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;
+}
+
+static inline struct ceph_buffer *ceph_buffer_get(struct ceph_buffer *b)
+{
+	atomic_inc(&b->nref);
+	return b;
+}
+
+static inline void ceph_buffer_put(struct ceph_buffer *b)
+{
+	if (b && atomic_dec_and_test(&b->nref)) {
+		if (b->vec.iov_base) {
+			if (b->is_vmalloc)
+				vfree(b->vec.iov_base);
+			else
+				kfree(b->vec.iov_base);
+		}
+		kfree(b);
+	}
+}
+
+static inline struct ceph_buffer *ceph_buffer_new_alloc(int len, gfp_t gfp)
+{
+	struct ceph_buffer *b = ceph_buffer_new(gfp);
+
+	if (b && ceph_buffer_alloc(b, len, gfp) < 0) {
+		ceph_buffer_put(b);
+		b = NULL;
+	}
+	return b;
+}
+
+#endif
-- 
1.5.6.5


  reply	other threads:[~2009-09-08 22:57 UTC|newest]

Thread overview: 25+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2009-09-08 22:56 [PATCH 00/20] ceph distributed file system client Sage Weil
2009-09-08 22:56 ` [PATCH 01/20] ceph: documentation Sage Weil
2009-09-08 22:56   ` [PATCH 02/20] ceph: on-wire types Sage Weil
2009-09-08 22:56     ` [PATCH 03/20] ceph: client types Sage Weil
2009-09-08 22:56       ` Sage Weil [this message]
2009-09-08 22:56         ` [PATCH 05/20] ceph: super.c Sage Weil
2009-09-08 22:56           ` [PATCH 06/20] ceph: inode operations Sage Weil
2009-09-08 22:56             ` [PATCH 07/20] ceph: directory operations Sage Weil
2009-09-08 22:56               ` [PATCH 08/20] ceph: file operations Sage Weil
2009-09-08 22:56                 ` [PATCH 09/20] ceph: address space operations Sage Weil
2009-09-08 22:56                   ` [PATCH 10/20] ceph: MDS client Sage Weil
2009-09-08 22:56                     ` [PATCH 11/20] ceph: OSD client Sage Weil
2009-09-08 22:56                       ` [PATCH 12/20] ceph: CRUSH mapping algorithm Sage Weil
2009-09-08 22:56                         ` [PATCH 13/20] ceph: monitor client Sage Weil
2009-09-08 22:56                           ` [PATCH 14/20] ceph: capability management Sage Weil
2009-09-08 22:56                             ` [PATCH 15/20] ceph: snapshot management Sage Weil
2009-09-08 22:56                               ` [PATCH 16/20] ceph: messenger library Sage Weil
2009-09-08 22:56                                 ` [PATCH 17/20] ceph: nfs re-export support Sage Weil
2009-09-08 22:56                                   ` [PATCH 18/20] ceph: ioctls Sage Weil
2009-09-08 22:56                                     ` [PATCH 19/20] ceph: debugfs Sage Weil
2009-09-08 22:56                                       ` [PATCH 20/20] ceph: Kconfig, Makefile Sage Weil
2009-09-08 23:05                                     ` [PATCH 18/20] ceph: ioctls Randy Dunlap
2009-09-08 23:45                                       ` Sage Weil
2009-09-08 23:10 ` [PATCH 00/20] ceph distributed file system client Daniel Walker
2009-09-08 23:47   ` 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=1252450601-17610-5-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).