1 // SPDX-License-Identifier: GPL-2.0-only 2 /* Copyright (c) 2026 Christian Brauner <brauner@kernel.org> */ 3 #include <linux/fs.h> 4 #include <linux/fs/super_types.h> 5 #include <linux/fs_context.h> 6 #include <linux/fs_struct.h> 7 #include <linux/magic.h> 8 #include <linux/mount.h> 9 10 #include "internal.h" 11 12 static struct path failfs_root_path = {}; 13 14 void failfs_get_root(struct path *path) 15 { 16 *path = failfs_root_path; 17 path_get(path); 18 } 19 20 bool failfs_mnt(const struct vfsmount *mnt) 21 { 22 return mnt->mnt_sb == failfs_root_path.mnt->mnt_sb; 23 } 24 25 static int failfs_permission(struct mnt_idmap *idmap, struct inode *inode, 26 int mask) 27 { 28 return -EOPNOTSUPP; 29 } 30 31 static struct dentry *failfs_lookup(struct inode *dir, struct dentry *dentry, 32 unsigned int flags) 33 { 34 /* Unreachable: ->permission() already failed the walk. */ 35 return ERR_PTR(-EOPNOTSUPP); 36 } 37 38 static int failfs_getattr(struct mnt_idmap *idmap, const struct path *path, 39 struct kstat *stat, u32 request_mask, 40 unsigned int query_flags) 41 { 42 return -EOPNOTSUPP; 43 } 44 45 static const struct inode_operations failfs_dir_inode_operations = { 46 .permission = failfs_permission, 47 .lookup = failfs_lookup, 48 .getattr = failfs_getattr, 49 }; 50 51 static const struct file_operations failfs_dir_operations = {}; 52 53 static int failfs_d_weak_revalidate(struct dentry *dentry, unsigned int flags) 54 { 55 /* 56 * The root is only ever reached as a path-walk terminal by jumping 57 * to it: as "/" when it is the caller's root, or through a 58 * /proc/<pid>/{root,cwd} magic link. ->permission() already fails 59 * every walk of a component, but a jump lands on the root without 60 * one. Refuse here too so the root cannot be pinned by an O_PATH 61 * open or encoded into a file handle. 62 */ 63 return -EOPNOTSUPP; 64 } 65 66 static char *failfs_dname(struct dentry *dentry, char *buffer, int buflen) 67 { 68 return dynamic_dname(buffer, buflen, "failfs:/"); 69 } 70 71 static const struct dentry_operations failfs_dentry_operations = { 72 .d_dname = failfs_dname, 73 .d_weak_revalidate = failfs_d_weak_revalidate, 74 }; 75 76 static int failfs_statfs(struct dentry *dentry, struct kstatfs *buf) 77 { 78 return -EOPNOTSUPP; 79 } 80 81 static const struct super_operations failfs_super_operations = { 82 .statfs = failfs_statfs, 83 }; 84 85 static int failfs_fill_super(struct super_block *s, struct fs_context *fc) 86 { 87 struct inode *inode; 88 89 s->s_maxbytes = MAX_LFS_FILESIZE; 90 s->s_blocksize = PAGE_SIZE; 91 s->s_blocksize_bits = PAGE_SHIFT; 92 s->s_magic = FAIL_FS_MAGIC; 93 s->s_op = &failfs_super_operations; 94 s->s_export_op = NULL; 95 s->s_xattr = NULL; 96 s->s_time_gran = 1; 97 s->s_d_flags = 0; 98 99 inode = new_inode(s); 100 if (!inode) 101 return -ENOMEM; 102 103 /* failfs supports no operations... */ 104 inode->i_mode = S_IFDIR; 105 set_nlink(inode, 2); 106 inode->i_op = &failfs_dir_inode_operations; 107 inode->i_fop = &failfs_dir_operations; 108 simple_inode_init_ts(inode); 109 inode->i_ino = 1; 110 /* ... and is immutable. */ 111 inode->i_flags |= S_IMMUTABLE; 112 113 set_default_d_op(s, &failfs_dentry_operations); 114 s->s_root = d_make_root(inode); 115 if (!s->s_root) 116 return -ENOMEM; 117 118 return 0; 119 } 120 121 static int failfs_get_tree(struct fs_context *fc) 122 { 123 return get_tree_single(fc, failfs_fill_super); 124 } 125 126 static const struct fs_context_operations failfs_context_ops = { 127 .get_tree = failfs_get_tree, 128 }; 129 130 static int failfs_init_fs_context(struct fs_context *fc) 131 { 132 fc->ops = &failfs_context_ops; 133 fc->global = true; 134 fc->sb_flags |= SB_NOUSER; 135 fc->s_iflags |= SB_I_NOEXEC | SB_I_NODEV; 136 return 0; 137 } 138 139 int failfs_current_chdir(void) 140 { 141 struct path path; 142 143 failfs_get_root(&path); 144 set_fs_pwd(current->fs, &path); 145 path_put(&path); 146 return 0; 147 } 148 149 static struct file_system_type failfs_fs_type = { 150 .name = "failfs", 151 .init_fs_context = failfs_init_fs_context, 152 .kill_sb = kill_anon_super, 153 }; 154 155 void __init failfs_init(void) 156 { 157 struct vfsmount *mnt; 158 159 /* A single instance that is member of no mount namespace. */ 160 mnt = kern_mount(&failfs_fs_type); 161 if (IS_ERR(mnt)) 162 panic("VFS: Failed to create failfs"); 163 164 failfs_root_path.mnt = mnt; 165 failfs_root_path.dentry = mnt->mnt_root; 166 } 167