From patchwork Mon Aug 5 09:32:39 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Gowans, James" X-Patchwork-Id: 13753330 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 69DB2C3DA4A for ; Mon, 5 Aug 2024 09:34:33 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id EFB436B0093; Mon, 5 Aug 2024 05:34:32 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id EAED46B0095; Mon, 5 Aug 2024 05:34:32 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id D4F276B0096; Mon, 5 Aug 2024 05:34:32 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0010.hostedemail.com [216.40.44.10]) by kanga.kvack.org (Postfix) with ESMTP id B59F76B0093 for ; Mon, 5 Aug 2024 05:34:32 -0400 (EDT) Received: from smtpin14.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id 37CB7161C6D for ; Mon, 5 Aug 2024 09:34:32 +0000 (UTC) X-FDA: 82417681584.14.8CDB010 Received: from smtp-fw-9102.amazon.com (smtp-fw-9102.amazon.com [207.171.184.29]) by imf16.hostedemail.com (Postfix) with ESMTP id 13BBB180010 for ; Mon, 5 Aug 2024 09:34:29 +0000 (UTC) Authentication-Results: imf16.hostedemail.com; dkim=pass header.d=amazon.com header.s=amazon201209 header.b=v4hYxFIO; spf=pass (imf16.hostedemail.com: domain of "prvs=940e15008=jgowans@amazon.com" designates 207.171.184.29 as permitted sender) smtp.mailfrom="prvs=940e15008=jgowans@amazon.com"; dmarc=pass (policy=quarantine) header.from=amazon.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1722850408; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=rBGc29GJqkanga86wc+CDruFBMWuAWTKAOMI6eWst9M=; b=zkAgQOj00b54sz0wBvDT3ZS8VTA6EYeX9mqpbHgiGcKsCUK5h0Cf4OIr8PWdWl3ap4bup/ YEJ40oStYATPxberrYb8McdRSJGUvG8ZkznpbN1ZfRVxGxJrGDY/8G1iNvSqj6Vut48O2p qWJfkk+GkMq50hUnLYnvSUZkBipc+ic= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1722850408; a=rsa-sha256; cv=none; b=MCeBEdpkSsmSs7FNWUUKMG6ceNvVy3gRzFy35U7GI1tDARmTLc5LusPNefWhxDBkBi4Ky9 K8edObV4NyhOvkJyHqzdagtRZtncS380ib3/YwUzNkhvkGN169ZGjqgRe+jZrdiIRIquUi 4fTw6PwROwi20JTP/6GnVrAxP0r/+Kg= ARC-Authentication-Results: i=1; imf16.hostedemail.com; dkim=pass header.d=amazon.com header.s=amazon201209 header.b=v4hYxFIO; spf=pass (imf16.hostedemail.com: domain of "prvs=940e15008=jgowans@amazon.com" designates 207.171.184.29 as permitted sender) smtp.mailfrom="prvs=940e15008=jgowans@amazon.com"; dmarc=pass (policy=quarantine) header.from=amazon.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amazon.com; i=@amazon.com; q=dns/txt; s=amazon201209; t=1722850471; x=1754386471; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=rBGc29GJqkanga86wc+CDruFBMWuAWTKAOMI6eWst9M=; b=v4hYxFIOOn8QcARdOynxpYFVXg9WnxPMiM4F6TMDuEgPr9LGnHWLXWV4 2S+vTN1XmW32pRDcl3jtnqEu7RuxwR5gALvuVcPAaVAcmk63grZKxqOpO j7hAgl5FIcwRZtgVSyAfwpCWb2IrsJmaXhsZk/BMEsdJNkPiKwuay3dOI 8=; X-IronPort-AV: E=Sophos;i="6.09,264,1716249600"; d="scan'208";a="440962193" Received: from pdx4-co-svc-p1-lb2-vlan3.amazon.com (HELO smtpout.prod.us-east-1.prod.farcaster.email.amazon.dev) ([10.25.36.214]) by smtp-border-fw-9102.sea19.amazon.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 05 Aug 2024 09:34:20 +0000 Received: from EX19MTAEUB001.ant.amazon.com [10.0.10.100:16945] by smtpin.naws.eu-west-1.prod.farcaster.email.amazon.dev [10.0.35.73:2525] with esmtp (Farcaster) id bb9ee3fa-f148-4792-ad18-0aaa1262bc00; Mon, 5 Aug 2024 09:34:18 +0000 (UTC) X-Farcaster-Flow-ID: bb9ee3fa-f148-4792-ad18-0aaa1262bc00 Received: from EX19D014EUC004.ant.amazon.com (10.252.51.182) by EX19MTAEUB001.ant.amazon.com (10.252.51.26) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA) id 15.2.1258.34; Mon, 5 Aug 2024 09:34:18 +0000 Received: from u5d18b891348c5b.ant.amazon.com (10.146.13.113) by EX19D014EUC004.ant.amazon.com (10.252.51.182) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA) id 15.2.1258.34; Mon, 5 Aug 2024 09:34:09 +0000 From: James Gowans To: CC: James Gowans , Sean Christopherson , Paolo Bonzini , Alexander Viro , Steve Sistare , Christian Brauner , Jan Kara , "Anthony Yznaga" , Mike Rapoport , "Andrew Morton" , , Jason Gunthorpe , , Usama Arif , , Alexander Graf , David Woodhouse , Paul Durrant , Nicolas Saenz Julienne Subject: [PATCH 04/10] guestmemfs: support file truncation Date: Mon, 5 Aug 2024 11:32:39 +0200 Message-ID: <20240805093245.889357-5-jgowans@amazon.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240805093245.889357-1-jgowans@amazon.com> References: <20240805093245.889357-1-jgowans@amazon.com> MIME-Version: 1.0 X-Originating-IP: [10.146.13.113] X-ClientProxiedBy: EX19D044UWB004.ant.amazon.com (10.13.139.134) To EX19D014EUC004.ant.amazon.com (10.252.51.182) X-Stat-Signature: oznf4hzkknck6xjfm3ozhbquqyxsretw X-Rspamd-Queue-Id: 13BBB180010 X-Rspam-User: X-Rspamd-Server: rspam08 X-HE-Tag: 1722850469-862343 X-HE-Meta: U2FsdGVkX18lE6NqDcaoQz9vJnb5U9ycH63O7h0iIg+kRJuFTX3SCbwIMflgwZ4+cCOTaHoXHkfzHYBVRaKoghhv7gi75ByUE8s8cujNo50riLYkntgIBUORKt62DICgD9tn+OcoYoYf5x30Vjoi8bxFnK1vL/cIv0+K8J23tJkfcSfAGOdu+/XHRu6qHFZLSk0HbLetJRRkkDJSJMCDMsvqgm11hVBtugXBYKgqIov8gGYSVDFBQJi1GzcxuPg/+ntJoIX6k/PBSfhsx9396dDjjAxb3GzsHjygPlmYTM4OHQUf218eNFKs/TNolCQz7aSXsHwYxHzfnKeI2rIfoIOiYyXphyKsqEOrIwX3Dev1RFAWBic+x/lZbUlSbapymucmz66Ol1keSsegJlnXpvQZIgRkSU0Xsozc83L3OlMSE9/XfwMcGoKkeZv1Sqdp927DoQpT6yrjKjpSngjxTIvOcKhjOGF4/ZxACbkKja5+tqHfvXRNnj/J54s+APxz8KHWfBBF3ntbbRZVF+M3pkwrV+7hN4S45cUzcJWT4hedfDSHQOKm1ifLIGcoK1QYMOR6G3Qeb+UhpxT2ggXnxWBXEIn9acALkZywlbD0dS+pBVjQv1z7agO0x6YkhghW7qTG1CLTCR5M3NalMdYkBGoZS0ggDsR7SCuXEbfLlbbmZy0L7Jf+sBVocQq7BoeJzM5lHpZwPKcFvFFq/Ahodn5f5aY/c5SJaHyO3cCTzOye/P/vGiP9lGzpN1/XJGi81t3mJpAHlMaD7XiQLYircHarn7g8ckq5QzOVWe0o7JawOXbxxOMgcmb4bt+jgIzlE/cZC2l/h/ZfTZntZfyh6pZCotjl/JgX9YAm2uDpOlwqulh04X/wr8kr9eD9Nvp39r5HbXUJj8A7A27QgKDbQ3iQa9OVjRnRDdIYMqmWVjUorZRfOnfVwcz0vHVUkkFe/udepNobkvjqDSIYvb2 z/L6pqLC uZ5NkGNeVjqV4Y8fIolDynH08WOCAty+G/KuEtp0Q6/jDwdXA9LqRCWOW0T8WUmscVX9dn3aZiu0QH+9j8SgGvtrm2iIJ4Qbo59ze+JnbZ8rxZXRIo2kntbY5iJW+ywy+DaaBATm6z3u4rGrOJAZBsIRxfMT4ZwOGoac8IVRsQPdM2v7i7eu33dtavXycwP4D639shSkhSIXNdzJ8bKd7U1LKV1RgcjMJ1HtG/CkE2sbkQt8Rapoh3DKQLViEExmCDF9xI6AoCqljDQZEZ3xK/U8TVxdnug7QrpE1O90BfdTiTOsYRpCus3LAPqqD1LExdFr6aWoW6lqmXR4MPLKH+cDrZDLURQTQ/dXh3cw8P7JAgo38IZ8EKKydLXpYUSF9UlAh7280SoXmgCt+xbNYMjgPfpaS+dkag0M9rOZhYPP/MmJQEbiiWpcNaWjtNtGpgdoVRw3ymdOQZEIPvM7R2nfXl/nU3oI+hEQTavA+Wi37Anghbob/CMv3H1knjAV2mFceukju9N5eTPyqj2xE7JReWTqdo6LXed01cI3i0wSomYdAjJu/9o/bWWlhsiVLWs7Pg3Cgu8b+V0o= X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: 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 --- 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, }; +