From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 From: Tyler Smith <74526874+tylertms@users.noreply.github.com> Date: Wed, 4 Nov 2026 02:48:46 +0000 Subject: [PATCH] refactor(vfs): separate original stashed path construction --- fs/Makefile | 2 +- fs/libfs.c | 116 ------------------------------------------- fs/stashed_path.c | 122 ++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 123 insertions(+), 117 deletions(-) create mode 100644 fs/stashed_path.c diff --git a/fs/Makefile b/fs/Makefile index a2161dfacfe2ac2f4fb44ea0833c34a3d396e9ec..cf56ae3bdd7c0d2c8753907c0c6f8b5bfe0c05e1 100644 --- a/fs/Makefile +++ b/fs/Makefile @@ -11,7 +11,7 @@ obj-y := open.o filp_close.o read_write.o file_table.o file_table_init.o super.o char_dev.o stat.o exec.o task_comm.o pipe.o namei.o path_get.o name_hash.o filename.o fcntl.o \ ioctl.o readdir.o select.o dcache.o dcache_init.o dentry_ancestry.o inode.o inode_cache_init.o inode_evict.o \ attr.o bad_inode.o bad_inode_ops.o file.o file_ref.o init_fdtable.o filesystems.o namespace.o mount_cache.o \ - seq_file.o seq_lifetime.o seq_output.o seq_path.o xattr.o simple_xattr.o libfs.o pseudo_metadata.o fs-writeback.o \ + seq_file.o seq_lifetime.o seq_output.o seq_path.o xattr.o simple_xattr.o libfs.o pseudo_metadata.o stashed_path.o fs-writeback.o \ pnode.o splice.o sync.o utimes.o d_path.o \ stack.o fs_struct.o statfs.o fs_pin.o nsfs.o \ fs_dirent.o fs_context.o fs_context_log.o fs_context_params.o fs_parser.o fs_parser_path.o fsopen.o init.o \ diff --git a/fs/libfs.c b/fs/libfs.c index 284c392113905595672fa091ba5f47b0a6e31193..fec9bf30c02bb36bb17f78d296110da62d4dc088 100644 --- a/fs/libfs.c +++ b/fs/libfs.c @@ -2000,122 +2000,6 @@ ssize_t direct_write_fallback(struct kiocb *iocb, struct iov_iter *iter, } EXPORT_SYMBOL_GPL(direct_write_fallback); -/** - * simple_inode_init_ts - initialize the timestamps for a new inode - * @inode: inode to be initialized - * - * When a new inode is created, most filesystems set the timestamps to the - * current time. Add a helper to do this. - */ -struct timespec64 simple_inode_init_ts(struct inode *inode) -{ - struct timespec64 ts = inode_set_ctime_current(inode); - - inode_set_atime_to_ts(inode, ts); - inode_set_mtime_to_ts(inode, ts); - return ts; -} -EXPORT_SYMBOL(simple_inode_init_ts); - -static struct dentry *prepare_anon_dentry(struct dentry **stashed, - struct super_block *sb, - void *data) -{ - struct dentry *dentry; - struct inode *inode; - const struct stashed_operations *sops = sb->s_fs_info; - int ret; - - inode = new_inode_pseudo(sb); - if (!inode) { - sops->put_data(data); - return ERR_PTR(-ENOMEM); - } - - inode->i_flags |= S_IMMUTABLE; - inode->i_mode = S_IFREG; - simple_inode_init_ts(inode); - - ret = sops->init_inode(inode, data); - if (ret < 0) { - iput(inode); - return ERR_PTR(ret); - } - - /* Notice when this is changed. */ - WARN_ON_ONCE(!S_ISREG(inode->i_mode)); - - dentry = d_alloc_anon(sb); - if (!dentry) { - iput(inode); - return ERR_PTR(-ENOMEM); - } - - /* Store address of location where dentry's supposed to be stashed. */ - dentry->d_fsdata = stashed; - - /* @data is now owned by the fs */ - d_instantiate(dentry, inode); - return dentry; -} - -/** - * path_from_stashed - create path from stashed or new dentry - * @stashed: where to retrieve or stash dentry - * @mnt: mnt of the filesystems to use - * @data: data to store in inode->i_private - * @path: path to create - * - * The function tries to retrieve a stashed dentry from @stashed. If the dentry - * is still valid then it will be reused. If the dentry isn't able the function - * will allocate a new dentry and inode. It will then check again whether it - * can reuse an existing dentry in case one has been added in the meantime or - * update @stashed with the newly added dentry. - * - * Special-purpose helper for nsfs and pidfs. - * - * Return: On success zero and on failure a negative error is returned. - */ -int path_from_stashed(struct dentry **stashed, struct vfsmount *mnt, void *data, - struct path *path) -{ - struct dentry *dentry, *res; - const struct stashed_operations *sops = mnt->mnt_sb->s_fs_info; - - /* See if dentry can be reused. */ - res = stashed_dentry_get(stashed); - if (IS_ERR(res)) - return PTR_ERR(res); - if (res) { - sops->put_data(data); - goto make_path; - } - - /* Allocate a new dentry. */ - dentry = prepare_anon_dentry(stashed, mnt->mnt_sb, data); - if (IS_ERR(dentry)) - return PTR_ERR(dentry); - - /* Added a new dentry. @data is now owned by the filesystem. */ - if (sops->stash_dentry) - res = sops->stash_dentry(stashed, dentry); - else - res = stash_dentry(stashed, dentry); - if (IS_ERR(res)) { - dput(dentry); - return PTR_ERR(res); - } - if (res != dentry) - dput(dentry); - -make_path: - path->dentry = res; - path->mnt = mntget(mnt); - VFS_WARN_ON_ONCE(path->dentry->d_fsdata != stashed); - VFS_WARN_ON_ONCE(d_inode(path->dentry)->i_private != data); - return 0; -} - /** * simple_start_creating - prepare to create a given name * @parent: directory in which to prepare to create the name diff --git a/fs/stashed_path.c b/fs/stashed_path.c new file mode 100644 index 0000000000000000000000000000000000000000..8b7b863e04fd977513e549a13541a36e24e74899 --- /dev/null +++ b/fs/stashed_path.c @@ -0,0 +1,122 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* Pseudo-file path and timestamp helpers from fs/libfs.c. */ +#include +#include + +#include "internal.h" + +/** + * simple_inode_init_ts - initialize the timestamps for a new inode + * @inode: inode to be initialized + * + * When a new inode is created, most filesystems set the timestamps to the + * current time. Add a helper to do this. + */ +struct timespec64 simple_inode_init_ts(struct inode *inode) +{ + struct timespec64 ts = inode_set_ctime_current(inode); + + inode_set_atime_to_ts(inode, ts); + inode_set_mtime_to_ts(inode, ts); + return ts; +} +EXPORT_SYMBOL(simple_inode_init_ts); + +static struct dentry *prepare_anon_dentry(struct dentry **stashed, + struct super_block *sb, + void *data) +{ + struct dentry *dentry; + struct inode *inode; + const struct stashed_operations *sops = sb->s_fs_info; + int ret; + + inode = new_inode_pseudo(sb); + if (!inode) { + sops->put_data(data); + return ERR_PTR(-ENOMEM); + } + + inode->i_flags |= S_IMMUTABLE; + inode->i_mode = S_IFREG; + simple_inode_init_ts(inode); + + ret = sops->init_inode(inode, data); + if (ret < 0) { + iput(inode); + return ERR_PTR(ret); + } + + /* Notice when this is changed. */ + WARN_ON_ONCE(!S_ISREG(inode->i_mode)); + + dentry = d_alloc_anon(sb); + if (!dentry) { + iput(inode); + return ERR_PTR(-ENOMEM); + } + + /* Store address of location where dentry's supposed to be stashed. */ + dentry->d_fsdata = stashed; + + /* @data is now owned by the fs */ + d_instantiate(dentry, inode); + return dentry; +} + +/** + * path_from_stashed - create path from stashed or new dentry + * @stashed: where to retrieve or stash dentry + * @mnt: mnt of the filesystems to use + * @data: data to store in inode->i_private + * @path: path to create + * + * The function tries to retrieve a stashed dentry from @stashed. If the dentry + * is still valid then it will be reused. If the dentry isn't able the function + * will allocate a new dentry and inode. It will then check again whether it + * can reuse an existing dentry in case one has been added in the meantime or + * update @stashed with the newly added dentry. + * + * Special-purpose helper for nsfs and pidfs. + * + * Return: On success zero and on failure a negative error is returned. + */ +int path_from_stashed(struct dentry **stashed, struct vfsmount *mnt, void *data, + struct path *path) +{ + struct dentry *dentry, *res; + const struct stashed_operations *sops = mnt->mnt_sb->s_fs_info; + + /* See if dentry can be reused. */ + res = stashed_dentry_get(stashed); + if (IS_ERR(res)) + return PTR_ERR(res); + if (res) { + sops->put_data(data); + goto make_path; + } + + /* Allocate a new dentry. */ + dentry = prepare_anon_dentry(stashed, mnt->mnt_sb, data); + if (IS_ERR(dentry)) + return PTR_ERR(dentry); + + /* Added a new dentry. @data is now owned by the filesystem. */ + if (sops->stash_dentry) + res = sops->stash_dentry(stashed, dentry); + else + res = stash_dentry(stashed, dentry); + if (IS_ERR(res)) { + dput(dentry); + return PTR_ERR(res); + } + if (res != dentry) + dput(dentry); + +make_path: + path->dentry = res; + path->mnt = mntget(mnt); + VFS_WARN_ON_ONCE(path->dentry->d_fsdata != stashed); + VFS_WARN_ON_ONCE(d_inode(path->dentry)->i_private != data); + return 0; +}