linux-mm.kvack.org archive mirror
 help / color / mirror / Atom feed
From: James Gowans <jgowans@amazon.com>
To: <linux-kernel@vger.kernel.org>
Cc: James Gowans <jgowans@amazon.com>,
	Sean Christopherson <seanjc@google.com>,
	Paolo Bonzini <pbonzini@redhat.com>,
	Alexander Viro <viro@zeniv.linux.org.uk>,
	Steve Sistare <steven.sistare@oracle.com>,
	Christian Brauner <brauner@kernel.org>, Jan Kara <jack@suse.cz>,
	"Anthony Yznaga" <anthony.yznaga@oracle.com>,
	Mike Rapoport <rppt@kernel.org>,
	"Andrew Morton" <akpm@linux-foundation.org>, <linux-mm@kvack.org>,
	Jason Gunthorpe <jgg@ziepe.ca>, <linux-fsdevel@vger.kernel.org>,
	Usama Arif <usama.arif@bytedance.com>, <kvm@vger.kernel.org>,
	Alexander Graf <graf@amazon.com>,
	David Woodhouse <dwmw@amazon.co.uk>,
	Paul Durrant <pdurrant@amazon.co.uk>,
	Nicolas Saenz Julienne <nsaenz@amazon.es>
Subject: [PATCH 04/10] guestmemfs: support file truncation
Date: Mon, 5 Aug 2024 11:32:39 +0200	[thread overview]
Message-ID: <20240805093245.889357-5-jgowans@amazon.com> (raw)
In-Reply-To: <20240805093245.889357-1-jgowans@amazon.com>

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@amazon.com>
---
 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



  parent reply	other threads:[~2024-08-05  9:34 UTC|newest]

Thread overview: 34+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-08-05  9:32 [PATCH 00/10] Introduce guestmemfs: persistent in-memory filesystem James Gowans
2024-08-05  9:32 ` [PATCH 01/10] guestmemfs: Introduce filesystem skeleton James Gowans
2024-08-05 10:20   ` Christian Brauner
2024-08-05  9:32 ` [PATCH 02/10] guestmemfs: add inode store, files and dirs James Gowans
2024-08-05  9:32 ` [PATCH 03/10] guestmemfs: add persistent data block allocator James Gowans
2024-08-05  9:32 ` James Gowans [this message]
2024-08-05  9:32 ` [PATCH 05/10] guestmemfs: add file mmap callback James Gowans
2024-10-29 23:05   ` Elliot Berman
2024-10-30 22:18     ` Frank van der Linden
2024-11-01 12:55       ` Gowans, James
2024-10-31 15:30     ` Gowans, James
2024-10-31 16:06       ` Jason Gunthorpe
2024-11-01 13:01         ` Gowans, James
2024-11-01 13:42           ` Jason Gunthorpe
2024-11-02  8:24             ` Gowans, James
2024-11-04 11:11               ` Mike Rapoport
2024-11-04 14:39               ` Jason Gunthorpe
2024-11-04 10:49             ` Mike Rapoport
2024-08-05  9:32 ` [PATCH 06/10] kexec/kho: Add addr flag to not initialise memory James Gowans
2024-08-05  9:32 ` [PATCH 07/10] guestmemfs: Persist filesystem metadata via KHO James Gowans
2024-08-05  9:32 ` [PATCH 08/10] guestmemfs: Block modifications when serialised James Gowans
2024-08-05  9:32 ` [PATCH 09/10] guestmemfs: Add documentation and usage instructions James Gowans
2024-08-05  9:32 ` [PATCH 10/10] MAINTAINERS: Add maintainers for guestmemfs James Gowans
2024-08-05 14:32 ` [PATCH 00/10] Introduce guestmemfs: persistent in-memory filesystem Theodore Ts'o
2024-08-05 14:41   ` Paolo Bonzini
2024-08-05 19:47     ` Gowans, James
2024-08-05 19:53   ` Gowans, James
2024-08-05 20:01 ` Jan Kara
2024-08-05 23:29   ` Jason Gunthorpe
2024-08-06  8:26     ` Gowans, James
2024-08-06  8:12   ` Gowans, James
2024-08-06 13:43     ` David Hildenbrand
2024-10-17  4:53 ` Vishal Annapurve
2024-11-01 12:53   ` Gowans, James

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=20240805093245.889357-5-jgowans@amazon.com \
    --to=jgowans@amazon.com \
    --cc=akpm@linux-foundation.org \
    --cc=anthony.yznaga@oracle.com \
    --cc=brauner@kernel.org \
    --cc=dwmw@amazon.co.uk \
    --cc=graf@amazon.com \
    --cc=jack@suse.cz \
    --cc=jgg@ziepe.ca \
    --cc=kvm@vger.kernel.org \
    --cc=linux-fsdevel@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-mm@kvack.org \
    --cc=nsaenz@amazon.es \
    --cc=pbonzini@redhat.com \
    --cc=pdurrant@amazon.co.uk \
    --cc=rppt@kernel.org \
    --cc=seanjc@google.com \
    --cc=steven.sistare@oracle.com \
    --cc=usama.arif@bytedance.com \
    --cc=viro@zeniv.linux.org.uk \
    /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