[PATCH 04/10] guestmemfs: support file truncation

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

 



In a previous commit a block allocator was added. Now use that block
allocator to allocate blocks for files when ftruncate is run on them.

To do that a inode_operations is added on the file inodes with a getattr
callback handling the ATTR_SIZE attribute. When this is invoked pages
are allocated, the indexes of which are put into a mappings block.
The mappings block is an array with the index being the file offset
block and the value at that index being the pkernfs block backign that
file offset.

Signed-off-by: James Gowans <jgowans@xxxxxxxxxx>
---
 fs/guestmemfs/Makefile     |  2 +-
 fs/guestmemfs/file.c       | 52 ++++++++++++++++++++++++++++++++++++++
 fs/guestmemfs/guestmemfs.h |  2 ++
 fs/guestmemfs/inode.c      | 25 +++++++++++++++---
 4 files changed, 77 insertions(+), 4 deletions(-)
 create mode 100644 fs/guestmemfs/file.c

diff --git a/fs/guestmemfs/Makefile b/fs/guestmemfs/Makefile
index b357073a60f3..e93e43ba274b 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 allocator.o
+obj-y += guestmemfs.o inode.o dir.o allocator.o file.o
diff --git a/fs/guestmemfs/file.c b/fs/guestmemfs/file.c
new file mode 100644
index 000000000000..618c93b12196
--- /dev/null
+++ b/fs/guestmemfs/file.c
@@ -0,0 +1,52 @@
+// SPDX-License-Identifier: GPL-2.0-only
+
+#include "guestmemfs.h"
+
+static int truncate(struct inode *inode, loff_t newsize)
+{
+	unsigned long free_block;
+	struct guestmemfs_inode *guestmemfs_inode;
+	unsigned long *mappings;
+
+	guestmemfs_inode = guestmemfs_get_persisted_inode(inode->i_sb, inode->i_ino);
+	mappings = guestmemfs_inode->mappings;
+	i_size_write(inode, newsize);
+	for (int block_idx = 0; block_idx * PMD_SIZE < newsize; ++block_idx) {
+		free_block = guestmemfs_alloc_block(inode->i_sb);
+		if (free_block < 0)
+			/* TODO: roll back allocations. */
+			return -ENOMEM;
+		*(mappings + block_idx) = free_block;
+		++guestmemfs_inode->num_mappings;
+	}
+	return 0;
+}
+
+static int inode_setattr(struct mnt_idmap *idmap, struct dentry *dentry, struct iattr *iattr)
+{
+	struct inode *inode = dentry->d_inode;
+	int error;
+
+	error = setattr_prepare(idmap, dentry, iattr);
+	if (error)
+		return error;
+
+	if (iattr->ia_valid & ATTR_SIZE) {
+		error = truncate(inode, iattr->ia_size);
+		if (error)
+			return error;
+	}
+	setattr_copy(idmap, inode, iattr);
+	mark_inode_dirty(inode);
+	return 0;
+}
+
+const struct inode_operations guestmemfs_file_inode_operations = {
+	.setattr = inode_setattr,
+	.getattr = simple_getattr,
+};
+
+const struct file_operations guestmemfs_file_fops = {
+	.owner = THIS_MODULE,
+	.iterate_shared = NULL,
+};
diff --git a/fs/guestmemfs/guestmemfs.h b/fs/guestmemfs/guestmemfs.h
index af9832390be3..7ea03ac8ecca 100644
--- a/fs/guestmemfs/guestmemfs.h
+++ b/fs/guestmemfs/guestmemfs.h
@@ -44,3 +44,5 @@ 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);
 
 extern const struct file_operations guestmemfs_dir_fops;
+extern const struct file_operations guestmemfs_file_fops;
+extern const struct inode_operations guestmemfs_file_inode_operations;
diff --git a/fs/guestmemfs/inode.c b/fs/guestmemfs/inode.c
index 2360c3a4857d..61f70441d82c 100644
--- a/fs/guestmemfs/inode.c
+++ b/fs/guestmemfs/inode.c
@@ -15,14 +15,28 @@ struct guestmemfs_inode *guestmemfs_get_persisted_inode(struct super_block *sb,
 
 struct inode *guestmemfs_inode_get(struct super_block *sb, unsigned long ino)
 {
+	struct guestmemfs_inode *guestmemfs_inode;
 	struct inode *inode = iget_locked(sb, ino);
 
 	/* If this inode is cached it is already populated; just return */
 	if (!(inode->i_state & I_NEW))
 		return inode;
-	inode->i_op = &guestmemfs_dir_inode_operations;
+	guestmemfs_inode = guestmemfs_get_persisted_inode(sb, ino);
 	inode->i_sb = sb;
-	inode->i_mode = S_IFREG;
+
+	if (guestmemfs_inode->flags & GUESTMEMFS_INODE_FLAG_DIR) {
+		inode->i_op = &guestmemfs_dir_inode_operations;
+		inode->i_mode = S_IFDIR;
+	} else {
+		inode->i_op = &guestmemfs_file_inode_operations;
+		inode->i_mode = S_IFREG;
+		inode->i_fop = &guestmemfs_file_fops;
+		inode->i_size = guestmemfs_inode->num_mappings * PMD_SIZE;
+	}
+
+	set_nlink(inode, 1);
+
+	/* Switch based on file type */
 	unlock_new_inode(inode);
 	return inode;
 }
@@ -103,6 +117,7 @@ static struct dentry *guestmemfs_lookup(struct inode *dir,
 		unsigned int flags)
 {
 	struct guestmemfs_inode *guestmemfs_inode;
+	struct inode *vfs_inode;
 	unsigned long ino;
 
 	guestmemfs_inode = guestmemfs_get_persisted_inode(dir->i_sb, dir->i_ino);
@@ -112,7 +127,10 @@ static struct dentry *guestmemfs_lookup(struct inode *dir,
 		if (!strncmp(guestmemfs_inode->filename,
 			     dentry->d_name.name,
 			     GUESTMEMFS_FILENAME_LEN)) {
-			d_add(dentry, guestmemfs_inode_get(dir->i_sb, ino));
+			vfs_inode = guestmemfs_inode_get(dir->i_sb, ino);
+			mark_inode_dirty(dir);
+			inode_update_timestamps(vfs_inode, S_ATIME);
+			d_add(dentry, vfs_inode);
 			break;
 		}
 		ino = guestmemfs_inode->sibling_ino;
@@ -162,3 +180,4 @@ const struct inode_operations guestmemfs_dir_inode_operations = {
 	.lookup		= guestmemfs_lookup,
 	.unlink		= guestmemfs_unlink,
 };
+
-- 
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