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