All of lore.kernel.org
 help / color / mirror / Atom feed
From: Omar Sandoval <osandov@osandov.com>
To: linux-btrfs@vger.kernel.org
Cc: Omar Sandoval <osandov@osandov.com>
Subject: [PATCH v2 1/9] Btrfs: add extent buffer bitmap operations
Date: Thu,  3 Sep 2015 12:44:19 -0700	[thread overview]
Message-ID: <98a5cd3528b5a4e11c59ae0521feef8a0697c4d5.1441309178.git.osandov@fb.com> (raw)
In-Reply-To: <cover.1441309178.git.osandov@fb.com>
In-Reply-To: <cover.1441309178.git.osandov@fb.com>

These are going to be used for the free space tree bitmap items.

Signed-off-by: Omar Sandoval <osandov@fb.com>
---
 fs/btrfs/extent_io.c | 149 +++++++++++++++++++++++++++++++++++++++++++++++++++
 fs/btrfs/extent_io.h |   6 +++
 2 files changed, 155 insertions(+)

diff --git a/fs/btrfs/extent_io.c b/fs/btrfs/extent_io.c
index 02d05817cbdf..eae9175ff62b 100644
--- a/fs/btrfs/extent_io.c
+++ b/fs/btrfs/extent_io.c
@@ -5475,6 +5475,155 @@ void copy_extent_buffer(struct extent_buffer *dst, struct extent_buffer *src,
 	}
 }
 
+/*
+ * The extent buffer bitmap operations are done with byte granularity because
+ * bitmap items are not guaranteed to be aligned to a word and therefore a
+ * single word in a bitmap may straddle two pages in the extent buffer.
+ */
+#define BIT_BYTE(nr) ((nr) / BITS_PER_BYTE)
+#define BYTE_MASK ((1 << BITS_PER_BYTE) - 1)
+#define BITMAP_FIRST_BYTE_MASK(start) \
+	((BYTE_MASK << ((start) & (BITS_PER_BYTE - 1))) & BYTE_MASK)
+#define BITMAP_LAST_BYTE_MASK(nbits) \
+	(BYTE_MASK >> (-(nbits) & (BITS_PER_BYTE - 1)))
+
+/*
+ * eb_bitmap_offset() - calculate the page and offset of the byte containing the
+ * given bit number
+ * @eb: the extent buffer
+ * @start: offset of the bitmap item in the extent buffer
+ * @nr: bit number
+ * @page_index: return index of the page in the extent buffer that contains the
+ * given bit number
+ * @page_offset: return offset into the page given by page_index
+ *
+ * This helper hides the ugliness of finding the byte in an extent buffer which
+ * contains a given bit.
+ */
+static inline void eb_bitmap_offset(struct extent_buffer *eb,
+				    unsigned long start, unsigned long nr,
+				    unsigned long *page_index,
+				    size_t *page_offset)
+{
+	size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
+	size_t byte_offset = BIT_BYTE(nr);
+	size_t offset;
+
+	/*
+	 * The byte we want is the offset of the extent buffer + the offset of
+	 * the bitmap item in the extent buffer + the offset of the byte in the
+	 * bitmap item.
+	 */
+	offset = start_offset + start + byte_offset;
+
+	*page_index = offset >> PAGE_CACHE_SHIFT;
+	*page_offset = offset & (PAGE_CACHE_SIZE - 1);
+}
+
+/**
+ * extent_buffer_test_bit - determine whether a bit in a bitmap item is set
+ * @eb: the extent buffer
+ * @start: offset of the bitmap item in the extent buffer
+ * @nr: bit number to test
+ */
+int extent_buffer_test_bit(struct extent_buffer *eb, unsigned long start,
+			   unsigned long nr)
+{
+	char *kaddr;
+	struct page *page;
+	unsigned long i;
+	size_t offset;
+
+	eb_bitmap_offset(eb, start, nr, &i, &offset);
+	page = eb->pages[i];
+	WARN_ON(!PageUptodate(page));
+	kaddr = page_address(page);
+	return 1U & (kaddr[offset] >> (nr & (BITS_PER_BYTE - 1)));
+}
+
+/**
+ * extent_buffer_bitmap_set - set an area of a bitmap
+ * @eb: the extent buffer
+ * @start: offset of the bitmap item in the extent buffer
+ * @pos: bit number of the first bit
+ * @len: number of bits to set
+ */
+void extent_buffer_bitmap_set(struct extent_buffer *eb, unsigned long start,
+			      unsigned long pos, unsigned long len)
+{
+	char *kaddr;
+	struct page *page;
+	unsigned long i;
+	size_t offset;
+	const unsigned int size = pos + len;
+	int bits_to_set = BITS_PER_BYTE - (pos % BITS_PER_BYTE);
+	unsigned int mask_to_set = BITMAP_FIRST_BYTE_MASK(pos);
+
+	eb_bitmap_offset(eb, start, pos, &i, &offset);
+	page = eb->pages[i];
+	WARN_ON(!PageUptodate(page));
+	kaddr = page_address(page);
+
+	while (len >= bits_to_set) {
+		kaddr[offset] |= mask_to_set;
+		len -= bits_to_set;
+		bits_to_set = BITS_PER_BYTE;
+		mask_to_set = ~0U;
+		if (++offset >= PAGE_CACHE_SIZE && len > 0) {
+			offset = 0;
+			page = eb->pages[++i];
+			WARN_ON(!PageUptodate(page));
+			kaddr = page_address(page);
+		}
+	}
+	if (len) {
+		mask_to_set &= BITMAP_LAST_BYTE_MASK(size);
+		kaddr[offset] |= mask_to_set;
+	}
+}
+
+
+/**
+ * extent_buffer_bitmap_clear - clear an area of a bitmap
+ * @eb: the extent buffer
+ * @start: offset of the bitmap item in the extent buffer
+ * @pos: bit number of the first bit
+ * @len: number of bits to clear
+ */
+void extent_buffer_bitmap_clear(struct extent_buffer *eb, unsigned long start,
+				unsigned long pos, unsigned long len)
+{
+	char *kaddr;
+	struct page *page;
+	unsigned long i;
+	size_t offset;
+	const unsigned int size = pos + len;
+	int bits_to_clear = BITS_PER_BYTE - (pos % BITS_PER_BYTE);
+	unsigned int mask_to_clear = BITMAP_FIRST_BYTE_MASK(pos);
+
+	eb_bitmap_offset(eb, start, pos, &i, &offset);
+	page = eb->pages[i];
+	WARN_ON(!PageUptodate(page));
+	kaddr = page_address(page);
+
+	while (len >= bits_to_clear) {
+		kaddr[offset] &= ~mask_to_clear;
+		len -= bits_to_clear;
+		bits_to_clear = BITS_PER_BYTE;
+		mask_to_clear = ~0U;
+		if (++offset >= PAGE_CACHE_SIZE && len > 0) {
+			offset = 0;
+			page = eb->pages[++i];
+			WARN_ON(!PageUptodate(page));
+			kaddr = page_address(page);
+		}
+	}
+	if (len) {
+		mask_to_clear &= BITMAP_LAST_BYTE_MASK(size);
+		kaddr[offset] &= ~mask_to_clear;
+	}
+}
+
 static inline bool areas_overlap(unsigned long src, unsigned long dst, unsigned long len)
 {
 	unsigned long distance = (src > dst) ? src - dst : dst - src;
diff --git a/fs/btrfs/extent_io.h b/fs/btrfs/extent_io.h
index c668f36898d3..9185a20081d7 100644
--- a/fs/btrfs/extent_io.h
+++ b/fs/btrfs/extent_io.h
@@ -309,6 +309,12 @@ void memmove_extent_buffer(struct extent_buffer *dst, unsigned long dst_offset,
 			   unsigned long src_offset, unsigned long len);
 void memset_extent_buffer(struct extent_buffer *eb, char c,
 			  unsigned long start, unsigned long len);
+int extent_buffer_test_bit(struct extent_buffer *eb, unsigned long start,
+			   unsigned long pos);
+void extent_buffer_bitmap_set(struct extent_buffer *eb, unsigned long start,
+			      unsigned long pos, unsigned long len);
+void extent_buffer_bitmap_clear(struct extent_buffer *eb, unsigned long start,
+				unsigned long pos, unsigned long len);
 void clear_extent_buffer_dirty(struct extent_buffer *eb);
 int set_extent_buffer_dirty(struct extent_buffer *eb);
 int set_extent_buffer_uptodate(struct extent_buffer *eb);
-- 
2.5.1


  reply	other threads:[~2015-09-03 19:44 UTC|newest]

Thread overview: 43+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-09-01 19:01 [PATCH 0/6] free space B-tree Omar Sandoval
2015-09-01 19:01 ` [PATCH 1/6] Btrfs: add extent buffer bitmap operations Omar Sandoval
2015-09-01 19:25   ` Josef Bacik
2015-09-01 19:37     ` Omar Sandoval
2015-09-01 19:01 ` [PATCH 2/6] Btrfs: add helpers for read-only compat bits Omar Sandoval
2015-09-01 19:26   ` Josef Bacik
2015-09-01 19:01 ` [PATCH 3/6] Btrfs: introduce the free space B-tree on-disk format Omar Sandoval
2015-09-01 19:28   ` Josef Bacik
2015-09-01 19:05 ` [PATCH 5/6] Btrfs: wire up the free space tree to the extent tree Omar Sandoval
2015-09-01 19:48   ` Josef Bacik
2015-09-02  4:42     ` Omar Sandoval
2015-09-02 15:29       ` Josef Bacik
2015-09-01 19:05 ` [PATCH 6/6] Btrfs: add free space tree mount option Omar Sandoval
2015-09-01 19:49   ` Josef Bacik
2015-09-01 19:13 ` [PATCH 4/6] Btrfs: implement the free space B-tree Omar Sandoval
2015-09-01 19:44   ` Josef Bacik
2015-09-01 20:06     ` Omar Sandoval
2015-09-01 20:08       ` Josef Bacik
2015-09-01 19:17 ` [PATCH 0/6] " Omar Sandoval
2015-09-01 19:22 ` [PATCH 1/3] btrfs-progs: use calloc instead of malloc+memset for tree roots Omar Sandoval
2015-09-01 19:22   ` [PATCH 2/3] btrfs-progs: add basic awareness of the free space tree Omar Sandoval
2015-09-01 19:22   ` [PATCH 3/3] btrfs-progs: check the free space tree in btrfsck Omar Sandoval
2015-09-02 15:02   ` [PATCH 1/3] btrfs-progs: use calloc instead of malloc+memset for tree roots David Sterba
2015-09-03 19:44 ` [PATCH v2 0/9] free space B-tree Omar Sandoval
2015-09-03 19:44   ` Omar Sandoval [this message]
2015-09-03 19:44   ` [PATCH v2 2/9] Btrfs: add extent buffer bitmap sanity tests Omar Sandoval
2015-09-03 19:44   ` [PATCH v2 3/9] Btrfs: add helpers for read-only compat bits Omar Sandoval
2015-09-03 19:44   ` [PATCH v2 4/9] Btrfs: refactor caching_thread() Omar Sandoval
2015-09-03 19:44   ` [PATCH v2 5/9] Btrfs: introduce the free space B-tree on-disk format Omar Sandoval
2015-09-03 19:44   ` [PATCH v2 6/9] Btrfs: implement the free space B-tree Omar Sandoval
2015-09-03 19:44   ` [PATCH v2 7/9] Btrfs: add free space tree sanity tests Omar Sandoval
2015-09-03 19:44   ` [PATCH v2 8/9] Btrfs: wire up the free space tree to the extent tree Omar Sandoval
2015-09-04  5:56     ` Omar Sandoval
2015-09-03 19:44   ` [PATCH v2 9/9] Btrfs: add free space tree mount option Omar Sandoval
2015-09-09 12:00     ` David Sterba
2015-09-11  0:52       ` Omar Sandoval
2015-09-04  1:29   ` [PATCH v2 0/9] free space B-tree Zhao Lei
2015-09-04  5:43     ` Omar Sandoval
2015-09-11  1:21   ` Qu Wenruo
2015-09-11  3:48     ` Omar Sandoval
2015-09-11  3:58       ` Qu Wenruo
2015-09-11  4:15         ` Omar Sandoval
2015-09-22 14:41     ` David Sterba

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=98a5cd3528b5a4e11c59ae0521feef8a0697c4d5.1441309178.git.osandov@fb.com \
    --to=osandov@osandov.com \
    --cc=linux-btrfs@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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.