1 // SPDX-License-Identifier: GPL-2.0-only 2 /* Copyright (c) 2026 Christian Brauner <brauner@kernel.org> */ 3 #include <linux/fs/super_types.h> 4 #include <linux/fs_context.h> 5 #include <linux/magic.h> 6 #include <linux/splice.h> 7 #include <linux/uio.h> 8 9 #include "mount.h" 10 11 static const struct super_operations nullfs_super_operations = { 12 .statfs = simple_statfs, 13 }; 14 15 static loff_t nullfs_dir_llseek(struct file *file, loff_t offset, int whence) 16 { 17 /* an empty directory has two entries . and .. at offsets 0 and 1 */ 18 return generic_file_llseek_size(file, offset, whence, 2, 2); 19 } 20 21 static int nullfs_dir_readdir(struct file *file, struct dir_context *ctx) 22 { 23 dir_emit_dots(file, ctx); 24 return 0; 25 } 26 27 /* the one inode of nullfs is shared by every holder, so no locks on it */ 28 static int nullfs_nolock(struct file *file, int cmd, struct file_lock *fl) 29 { 30 return -ENOLCK; 31 } 32 33 /* what libfs gives an empty directory, plus the refusal of file locks */ 34 static const struct file_operations nullfs_dir_operations = { 35 .llseek = nullfs_dir_llseek, 36 .read = generic_read_dir, 37 .iterate_shared = nullfs_dir_readdir, 38 .fsync = noop_fsync, 39 .lock = nullfs_nolock, 40 .flock = nullfs_nolock, 41 .fop_flags = FOP_IMMUTABLE, 42 }; 43 44 static ssize_t nullfs_file_read_iter(struct kiocb *iocb, struct iov_iter *to) 45 { 46 return 0; 47 } 48 49 static ssize_t nullfs_file_splice_read(struct file *in, loff_t *ppos, 50 struct pipe_inode_info *pipe, 51 size_t len, unsigned int flags) 52 { 53 return 0; 54 } 55 56 static ssize_t nullfs_file_write_iter(struct kiocb *iocb, struct iov_iter *from) 57 { 58 size_t count = iov_iter_count(from); 59 60 iov_iter_advance(from, count); 61 return count; 62 } 63 64 static int nullfs_pipe_to_null(struct pipe_inode_info *pipe, 65 struct pipe_buffer *buf, struct splice_desc *sd) 66 { 67 return sd->len; 68 } 69 70 static ssize_t nullfs_file_splice_write(struct pipe_inode_info *pipe, 71 struct file *out, loff_t *ppos, 72 size_t len, unsigned int flags) 73 { 74 return splice_from_pipe(pipe, out, ppos, len, flags, nullfs_pipe_to_null); 75 } 76 77 static int nullfs_file_setattr(const struct mnt_idmap *idmap, 78 struct dentry *dentry, 79 struct iattr *attr) 80 { 81 if (attr->ia_valid & (ATTR_MODE | ATTR_UID | ATTR_GID)) 82 return -EPERM; 83 return 0; 84 } 85 86 static const struct inode_operations nullfs_file_inode_operations = { 87 .setattr = nullfs_file_setattr, 88 }; 89 90 static const struct file_operations nullfs_file_operations = { 91 .llseek = generic_file_llseek, 92 .read_iter = nullfs_file_read_iter, 93 .write_iter = nullfs_file_write_iter, 94 .splice_read = nullfs_file_splice_read, 95 .splice_write = nullfs_file_splice_write, 96 .fsync = noop_fsync, 97 .lock = nullfs_nolock, 98 .flock = nullfs_nolock, 99 }; 100 101 /* 102 * An empty regular file on @sb as a dentry of its own, never hashed under 103 * the root so no lookup finds it. 104 */ 105 struct dentry *nullfs_new_file(struct super_block *sb) 106 { 107 struct dentry *dentry; 108 struct inode *inode; 109 110 inode = new_inode(sb); 111 if (!inode) 112 return ERR_PTR(-ENOMEM); 113 114 /* the root directory is 1 */ 115 inode->i_ino = 2; 116 inode->i_mode = S_IFREG | 0666; 117 inode->i_op = &nullfs_file_inode_operations; 118 inode->i_fop = &nullfs_file_operations; 119 simple_inode_init_ts(inode); 120 inode->i_flags |= S_NOATIME; 121 122 dentry = d_alloc_anon(sb); 123 if (!dentry) { 124 iput(inode); 125 return ERR_PTR(-ENOMEM); 126 } 127 d_instantiate(dentry, inode); 128 return dentry; 129 } 130 131 static int nullfs_fs_fill_super(struct super_block *s, struct fs_context *fc) 132 { 133 struct inode *inode; 134 135 s->s_maxbytes = MAX_LFS_FILESIZE; 136 s->s_blocksize = PAGE_SIZE; 137 s->s_blocksize_bits = PAGE_SHIFT; 138 s->s_magic = NULL_FS_MAGIC; 139 s->s_op = &nullfs_super_operations; 140 s->s_export_op = NULL; 141 s->s_xattr = NULL; 142 s->s_time_gran = 1; 143 s->s_d_flags = 0; 144 145 inode = new_inode(s); 146 if (!inode) 147 return -ENOMEM; 148 149 /* nullfs is permanently empty... */ 150 make_empty_dir_inode(inode); 151 inode->i_fop = &nullfs_dir_operations; 152 simple_inode_init_ts(inode); 153 inode->i_ino = 1; 154 /* ... and immutable, reading it leaves no trace either. */ 155 inode->i_flags |= S_IMMUTABLE | S_NOATIME; 156 157 s->s_root = d_make_root(inode); 158 if (!s->s_root) 159 return -ENOMEM; 160 161 return 0; 162 } 163 164 static int nullfs_fs_get_tree(struct fs_context *fc) 165 { 166 return get_tree_nodev(fc, nullfs_fs_fill_super); 167 } 168 169 static const struct fs_context_operations nullfs_fs_context_ops = { 170 .get_tree = nullfs_fs_get_tree, 171 }; 172 173 static int nullfs_init_fs_context(struct fs_context *fc) 174 { 175 fc->ops = &nullfs_fs_context_ops; 176 fc->sb_flags |= SB_NOUSER; 177 fc->s_iflags |= SB_I_NOEXEC | SB_I_NODEV; 178 return 0; 179 } 180 181 struct file_system_type nullfs_fs_type = { 182 .name = "nullfs", 183 .fs_flags = FS_DISALLOW_NOTIFY, 184 .init_fs_context = nullfs_init_fs_context, 185 .kill_sb = kill_anon_super, 186 }; 187