xref: /kernel/linux/linux-5.10/fs/ramfs/inode.c (revision 8c2ecf20)
1/*
2 * Resizable simple ram filesystem for Linux.
3 *
4 * Copyright (C) 2000 Linus Torvalds.
5 *               2000 Transmeta Corp.
6 *
7 * Usage limits added by David Gibson, Linuxcare Australia.
8 * This file is released under the GPL.
9 */
10
11/*
12 * NOTE! This filesystem is probably most useful
13 * not as a real filesystem, but as an example of
14 * how virtual filesystems can be written.
15 *
16 * It doesn't get much simpler than this. Consider
17 * that this file implements the full semantics of
18 * a POSIX-compliant read-write filesystem.
19 *
20 * Note in particular how the filesystem does not
21 * need to implement any data structures of its own
22 * to keep track of the virtual data: using the VFS
23 * caches is sufficient.
24 */
25
26#include <linux/fs.h>
27#include <linux/pagemap.h>
28#include <linux/highmem.h>
29#include <linux/time.h>
30#include <linux/init.h>
31#include <linux/string.h>
32#include <linux/backing-dev.h>
33#include <linux/ramfs.h>
34#include <linux/sched.h>
35#include <linux/parser.h>
36#include <linux/magic.h>
37#include <linux/slab.h>
38#include <linux/uaccess.h>
39#include <linux/fs_context.h>
40#include <linux/fs_parser.h>
41#include "internal.h"
42
43struct ramfs_mount_opts {
44	umode_t mode;
45};
46
47struct ramfs_fs_info {
48	struct ramfs_mount_opts mount_opts;
49};
50
51#define RAMFS_DEFAULT_MODE	0755
52
53static const struct super_operations ramfs_ops;
54static const struct inode_operations ramfs_dir_inode_operations;
55
56static const struct address_space_operations ramfs_aops = {
57	.readpage	= simple_readpage,
58	.write_begin	= simple_write_begin,
59	.write_end	= simple_write_end,
60	.set_page_dirty	= __set_page_dirty_no_writeback,
61};
62
63struct inode *ramfs_get_inode(struct super_block *sb,
64				const struct inode *dir, umode_t mode, dev_t dev)
65{
66	struct inode * inode = new_inode(sb);
67
68	if (inode) {
69		inode->i_ino = get_next_ino();
70		inode_init_owner(inode, dir, mode);
71		inode->i_mapping->a_ops = &ramfs_aops;
72		mapping_set_gfp_mask(inode->i_mapping, GFP_HIGHUSER);
73		mapping_set_unevictable(inode->i_mapping);
74		inode->i_atime = inode->i_mtime = inode->i_ctime = current_time(inode);
75		switch (mode & S_IFMT) {
76		default:
77			init_special_inode(inode, mode, dev);
78			break;
79		case S_IFREG:
80			inode->i_op = &ramfs_file_inode_operations;
81			inode->i_fop = &ramfs_file_operations;
82			break;
83		case S_IFDIR:
84			inode->i_op = &ramfs_dir_inode_operations;
85			inode->i_fop = &simple_dir_operations;
86
87			/* directory inodes start off with i_nlink == 2 (for "." entry) */
88			inc_nlink(inode);
89			break;
90		case S_IFLNK:
91			inode->i_op = &page_symlink_inode_operations;
92			inode_nohighmem(inode);
93			break;
94		}
95	}
96	return inode;
97}
98
99/*
100 * File creation. Allocate an inode, and we're done..
101 */
102/* SMP-safe */
103static int
104ramfs_mknod(struct inode *dir, struct dentry *dentry, umode_t mode, dev_t dev)
105{
106	struct inode * inode = ramfs_get_inode(dir->i_sb, dir, mode, dev);
107	int error = -ENOSPC;
108
109	if (inode) {
110		d_instantiate(dentry, inode);
111		dget(dentry);	/* Extra count - pin the dentry in core */
112		error = 0;
113		dir->i_mtime = dir->i_ctime = current_time(dir);
114	}
115	return error;
116}
117
118static int ramfs_mkdir(struct inode * dir, struct dentry * dentry, umode_t mode)
119{
120	int retval = ramfs_mknod(dir, dentry, mode | S_IFDIR, 0);
121	if (!retval)
122		inc_nlink(dir);
123	return retval;
124}
125
126static int ramfs_create(struct inode *dir, struct dentry *dentry, umode_t mode, bool excl)
127{
128	return ramfs_mknod(dir, dentry, mode | S_IFREG, 0);
129}
130
131static int ramfs_symlink(struct inode * dir, struct dentry *dentry, const char * symname)
132{
133	struct inode *inode;
134	int error = -ENOSPC;
135
136	inode = ramfs_get_inode(dir->i_sb, dir, S_IFLNK|S_IRWXUGO, 0);
137	if (inode) {
138		int l = strlen(symname)+1;
139		error = page_symlink(inode, symname, l);
140		if (!error) {
141			d_instantiate(dentry, inode);
142			dget(dentry);
143			dir->i_mtime = dir->i_ctime = current_time(dir);
144		} else
145			iput(inode);
146	}
147	return error;
148}
149
150static const struct inode_operations ramfs_dir_inode_operations = {
151	.create		= ramfs_create,
152	.lookup		= simple_lookup,
153	.link		= simple_link,
154	.unlink		= simple_unlink,
155	.symlink	= ramfs_symlink,
156	.mkdir		= ramfs_mkdir,
157	.rmdir		= simple_rmdir,
158	.mknod		= ramfs_mknod,
159	.rename		= simple_rename,
160};
161
162/*
163 * Display the mount options in /proc/mounts.
164 */
165static int ramfs_show_options(struct seq_file *m, struct dentry *root)
166{
167	struct ramfs_fs_info *fsi = root->d_sb->s_fs_info;
168
169	if (fsi->mount_opts.mode != RAMFS_DEFAULT_MODE)
170		seq_printf(m, ",mode=%o", fsi->mount_opts.mode);
171	return 0;
172}
173
174static const struct super_operations ramfs_ops = {
175	.statfs		= simple_statfs,
176	.drop_inode	= generic_delete_inode,
177	.show_options	= ramfs_show_options,
178};
179
180enum ramfs_param {
181	Opt_mode,
182};
183
184const struct fs_parameter_spec ramfs_fs_parameters[] = {
185	fsparam_u32oct("mode",	Opt_mode),
186	{}
187};
188
189static int ramfs_parse_param(struct fs_context *fc, struct fs_parameter *param)
190{
191	struct fs_parse_result result;
192	struct ramfs_fs_info *fsi = fc->s_fs_info;
193	int opt;
194
195	opt = fs_parse(fc, ramfs_fs_parameters, param, &result);
196	if (opt == -ENOPARAM) {
197		opt = vfs_parse_fs_param_source(fc, param);
198		if (opt != -ENOPARAM)
199			return opt;
200		/*
201		 * We might like to report bad mount options here;
202		 * but traditionally ramfs has ignored all mount options,
203		 * and as it is used as a !CONFIG_SHMEM simple substitute
204		 * for tmpfs, better continue to ignore other mount options.
205		 */
206		return 0;
207	}
208	if (opt < 0)
209		return opt;
210
211	switch (opt) {
212	case Opt_mode:
213		fsi->mount_opts.mode = result.uint_32 & S_IALLUGO;
214		break;
215	}
216
217	return 0;
218}
219
220static int ramfs_fill_super(struct super_block *sb, struct fs_context *fc)
221{
222	struct ramfs_fs_info *fsi = sb->s_fs_info;
223	struct inode *inode;
224
225	sb->s_maxbytes		= MAX_LFS_FILESIZE;
226	sb->s_blocksize		= PAGE_SIZE;
227	sb->s_blocksize_bits	= PAGE_SHIFT;
228	sb->s_magic		= RAMFS_MAGIC;
229	sb->s_op		= &ramfs_ops;
230	sb->s_time_gran		= 1;
231
232	inode = ramfs_get_inode(sb, NULL, S_IFDIR | fsi->mount_opts.mode, 0);
233	sb->s_root = d_make_root(inode);
234	if (!sb->s_root)
235		return -ENOMEM;
236
237	return 0;
238}
239
240static int ramfs_get_tree(struct fs_context *fc)
241{
242	return get_tree_nodev(fc, ramfs_fill_super);
243}
244
245static void ramfs_free_fc(struct fs_context *fc)
246{
247	kfree(fc->s_fs_info);
248}
249
250static const struct fs_context_operations ramfs_context_ops = {
251	.free		= ramfs_free_fc,
252	.parse_param	= ramfs_parse_param,
253	.get_tree	= ramfs_get_tree,
254};
255
256int ramfs_init_fs_context(struct fs_context *fc)
257{
258	struct ramfs_fs_info *fsi;
259
260	fsi = kzalloc(sizeof(*fsi), GFP_KERNEL);
261	if (!fsi)
262		return -ENOMEM;
263
264	fsi->mount_opts.mode = RAMFS_DEFAULT_MODE;
265	fc->s_fs_info = fsi;
266	fc->ops = &ramfs_context_ops;
267	return 0;
268}
269
270void ramfs_kill_sb(struct super_block *sb)
271{
272	kfree(sb->s_fs_info);
273	kill_litter_super(sb);
274}
275
276static struct file_system_type ramfs_fs_type = {
277	.name		= "ramfs",
278	.init_fs_context = ramfs_init_fs_context,
279	.parameters	= ramfs_fs_parameters,
280	.kill_sb	= ramfs_kill_sb,
281	.fs_flags	= FS_USERNS_MOUNT,
282};
283
284static int __init init_ramfs_fs(void)
285{
286	return register_filesystem(&ramfs_fs_type);
287}
288fs_initcall(init_ramfs_fs);
289