[PATCH 03/10] guestmemfs: add persistent data block allocator

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



In order to assign backing data memory to files there needs to be the
ability to allocate blocks of data from the large contiguous reserved
memory block of filesystem memory. Here an allocated is added to serve
that purpose. For now it's a simple bitmap allocator: each bit
corresponds to a 2 MiB chunk in the filesystem data block.

On initialisation the bitmap is allocated for a fixed size (TODO: make
this dynamic based on filesystem memory size). Allocating a block
involves finding and setting the next free bit.

Allocations will be done in the next commit which adds support for
truncating files.

It's quite limiting having a fixed size bitmap, and we perhaps want to
look at making this a dynamic and potentially large allocation early in
boot using the memblock allocator. It may also turn out that a simple
bitmap is too limiting and something with more metadata is needed.

Signed-off-by: James Gowans <jgowans@xxxxxxxxxx>
---
 fs/guestmemfs/Makefile     |  2 +-
 fs/guestmemfs/allocator.c  | 40 ++++++++++++++++++++++++++++++++++++++
 fs/guestmemfs/guestmemfs.c |  4 ++++
 fs/guestmemfs/guestmemfs.h |  3 +++
 4 files changed, 48 insertions(+), 1 deletion(-)
 create mode 100644 fs/guestmemfs/allocator.c

diff --git a/fs/guestmemfs/Makefile b/fs/guestmemfs/Makefile
index 804997799ce8..b357073a60f3 100644
--- a/fs/guestmemfs/Makefile
+++ b/fs/guestmemfs/Makefile
@@ -3,4 +3,4 @@
 # Makefile for persistent kernel filesystem
 #
 
-obj-y += guestmemfs.o inode.o dir.o
+obj-y += guestmemfs.o inode.o dir.o allocator.o
diff --git a/fs/guestmemfs/allocator.c b/fs/guestmemfs/allocator.c
new file mode 100644
index 000000000000..3da14d11b60f
--- /dev/null
+++ b/fs/guestmemfs/allocator.c
@@ -0,0 +1,40 @@
+// SPDX-License-Identifier: GPL-2.0-only
+
+#include "guestmemfs.h"
+
+/**
+ * For allocating blocks from the guestmemfs filesystem.
+ */
+
+static void *guestmemfs_allocations_bitmap(struct super_block *sb)
+{
+	return GUESTMEMFS_PSB(sb)->allocator_bitmap;
+}
+
+void guestmemfs_zero_allocations(struct super_block *sb)
+{
+	memset(guestmemfs_allocations_bitmap(sb), 0, (1 << 20));
+}
+
+/*
+ * Allocs one 2 MiB block, and returns the block index.
+ * Index is 2 MiB chunk index.
+ * Negative error code if unable to alloc.
+ */
+long guestmemfs_alloc_block(struct super_block *sb)
+{
+	unsigned long free_bit;
+	void *allocations_mem = guestmemfs_allocations_bitmap(sb);
+
+	free_bit = bitmap_find_next_zero_area(allocations_mem,
+			(1 << 20), /* Size */
+			0, /* Start */
+			1, /* Number of zeroed bits to look for */
+			0); /* Alignment mask - none required. */
+
+	if (free_bit >= PMD_SIZE / 2)
+		return -ENOMEM;
+
+	bitmap_set(allocations_mem, free_bit, 1);
+	return free_bit;
+}
diff --git a/fs/guestmemfs/guestmemfs.c b/fs/guestmemfs/guestmemfs.c
index 21cb3490a2bd..c45c796c497a 100644
--- a/fs/guestmemfs/guestmemfs.c
+++ b/fs/guestmemfs/guestmemfs.c
@@ -37,6 +37,9 @@ static int guestmemfs_fill_super(struct super_block *sb, struct fs_context *fc)
 	psb->inodes = kzalloc(2 << 20, GFP_KERNEL);
 	if (!psb->inodes)
 		return -ENOMEM;
+	psb->allocator_bitmap = kzalloc(1 << 20, GFP_KERNEL);
+	if (!psb->allocator_bitmap)
+		return -ENOMEM;
 
 	/*
 	 * Keep a reference to the persistent super block in the
@@ -45,6 +48,7 @@ static int guestmemfs_fill_super(struct super_block *sb, struct fs_context *fc)
 	sb->s_fs_info = psb;
 	spin_lock_init(&psb->allocation_lock);
 	guestmemfs_initialise_inode_store(sb);
+	guestmemfs_zero_allocations(sb);
 	guestmemfs_get_persisted_inode(sb, 1)->flags = GUESTMEMFS_INODE_FLAG_DIR;
 	strscpy(guestmemfs_get_persisted_inode(sb, 1)->filename, ".",
 			GUESTMEMFS_FILENAME_LEN);
diff --git a/fs/guestmemfs/guestmemfs.h b/fs/guestmemfs/guestmemfs.h
index 3a2954d1beec..af9832390be3 100644
--- a/fs/guestmemfs/guestmemfs.h
+++ b/fs/guestmemfs/guestmemfs.h
@@ -13,6 +13,7 @@ struct guestmemfs_sb {
 	unsigned long next_free_ino;
 	unsigned long allocated_inodes;
 	struct guestmemfs_inode *inodes;
+	void *allocator_bitmap;
 	spinlock_t allocation_lock;
 };
 
@@ -37,6 +38,8 @@ struct guestmemfs_inode {
 };
 
 void guestmemfs_initialise_inode_store(struct super_block *sb);
+void guestmemfs_zero_allocations(struct super_block *sb);
+long guestmemfs_alloc_block(struct super_block *sb);
 struct inode *guestmemfs_inode_get(struct super_block *sb, unsigned long ino);
 struct guestmemfs_inode *guestmemfs_get_persisted_inode(struct super_block *sb, int ino);
 
-- 
2.34.1





[Index of Archives]     [KVM ARM]     [KVM ia64]     [KVM ppc]     [Virtualization Tools]     [Spice Development]     [Libvirt]     [Libvirt Users]     [Linux USB Devel]     [Linux Audio Users]     [Yosemite Questions]     [Linux Kernel]     [Linux SCSI]     [XFree86]

  Powered by Linux