blob: cc58aec432b8995f2f3a39182135f0014bc5eaf6 [file] [log] [blame]
lh9ed821d2023-04-07 01:36:19 -07001/*
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 <asm/uaccess.h>
39#include "internal.h"
40
41#define RAMFS_DEFAULT_MODE 0755
42
43static const struct super_operations ramfs_ops;
44static const struct inode_operations ramfs_dir_inode_operations;
45
46static struct backing_dev_info ramfs_backing_dev_info = {
47 .name = "ramfs",
48 .ra_pages = 0, /* No readahead */
49 .capabilities = BDI_CAP_NO_ACCT_AND_WRITEBACK |
50 BDI_CAP_MAP_DIRECT | BDI_CAP_MAP_COPY |
51 BDI_CAP_READ_MAP | BDI_CAP_WRITE_MAP | BDI_CAP_EXEC_MAP,
52};
53
54struct inode *ramfs_get_inode(struct super_block *sb,
55 const struct inode *dir, umode_t mode, dev_t dev)
56{
57 struct inode * inode = new_inode(sb);
58
59 if (inode) {
60 inode->i_ino = get_next_ino();
61 inode_init_owner(inode, dir, mode);
62 inode->i_mapping->a_ops = &ramfs_aops;
63 inode->i_mapping->backing_dev_info = &ramfs_backing_dev_info;
64#ifdef CONFIG_LIMIT_PAGE_CACHE
65 mapping_set_gfp_mask(inode->i_mapping, GFP_HIGHUSER | __GFP_PAGERAMFS);
66#else
67 mapping_set_gfp_mask(inode->i_mapping, GFP_HIGHUSER);
68#endif
69 mapping_set_unevictable(inode->i_mapping);
70 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
71 switch (mode & S_IFMT) {
72 default:
73 init_special_inode(inode, mode, dev);
74 break;
75 case S_IFREG:
76 inode->i_op = &ramfs_file_inode_operations;
77 inode->i_fop = &ramfs_file_operations;
78 break;
79 case S_IFDIR:
80 inode->i_op = &ramfs_dir_inode_operations;
81 inode->i_fop = &simple_dir_operations;
82
83 /* directory inodes start off with i_nlink == 2 (for "." entry) */
84 inc_nlink(inode);
85 break;
86 case S_IFLNK:
87 inode->i_op = &page_symlink_inode_operations;
88 break;
89 }
90 }
91 return inode;
92}
93
94/*
95 * File creation. Allocate an inode, and we're done..
96 */
97/* SMP-safe */
98static int
99ramfs_mknod(struct inode *dir, struct dentry *dentry, umode_t mode, dev_t dev)
100{
101 struct inode * inode = ramfs_get_inode(dir->i_sb, dir, mode, dev);
102 int error = -ENOSPC;
103
104 if (inode) {
105 d_instantiate(dentry, inode);
106 dget(dentry); /* Extra count - pin the dentry in core */
107 error = 0;
108 dir->i_mtime = dir->i_ctime = CURRENT_TIME;
109 }
110 return error;
111}
112
113static int ramfs_mkdir(struct inode * dir, struct dentry * dentry, umode_t mode)
114{
115 int retval = ramfs_mknod(dir, dentry, mode | S_IFDIR, 0);
116 if (!retval)
117 inc_nlink(dir);
118 return retval;
119}
120
121static int ramfs_create(struct inode *dir, struct dentry *dentry, umode_t mode, struct nameidata *nd)
122{
123 return ramfs_mknod(dir, dentry, mode | S_IFREG, 0);
124}
125
126static int ramfs_symlink(struct inode * dir, struct dentry *dentry, const char * symname)
127{
128 struct inode *inode;
129 int error = -ENOSPC;
130
131 inode = ramfs_get_inode(dir->i_sb, dir, S_IFLNK|S_IRWXUGO, 0);
132 if (inode) {
133 int l = strlen(symname)+1;
134 error = page_symlink(inode, symname, l);
135 if (!error) {
136 d_instantiate(dentry, inode);
137 dget(dentry);
138 dir->i_mtime = dir->i_ctime = CURRENT_TIME;
139 } else
140 iput(inode);
141 }
142 return error;
143}
144
145static const struct inode_operations ramfs_dir_inode_operations = {
146 .create = ramfs_create,
147 .lookup = simple_lookup,
148 .link = simple_link,
149 .unlink = simple_unlink,
150 .symlink = ramfs_symlink,
151 .mkdir = ramfs_mkdir,
152 .rmdir = simple_rmdir,
153 .mknod = ramfs_mknod,
154 .rename = simple_rename,
155};
156
157static const struct super_operations ramfs_ops = {
158 .statfs = simple_statfs,
159 .drop_inode = generic_delete_inode,
160 .show_options = generic_show_options,
161};
162
163struct ramfs_mount_opts {
164 umode_t mode;
165};
166
167enum {
168 Opt_mode,
169 Opt_err
170};
171
172static const match_table_t tokens = {
173 {Opt_mode, "mode=%o"},
174 {Opt_err, NULL}
175};
176
177struct ramfs_fs_info {
178 struct ramfs_mount_opts mount_opts;
179};
180
181static int ramfs_parse_options(char *data, struct ramfs_mount_opts *opts)
182{
183 substring_t args[MAX_OPT_ARGS];
184 int option;
185 int token;
186 char *p;
187
188 opts->mode = RAMFS_DEFAULT_MODE;
189
190 while ((p = strsep(&data, ",")) != NULL) {
191 if (!*p)
192 continue;
193
194 token = match_token(p, tokens, args);
195 switch (token) {
196 case Opt_mode:
197 if (match_octal(&args[0], &option))
198 return -EINVAL;
199 opts->mode = option & S_IALLUGO;
200 break;
201 /*
202 * We might like to report bad mount options here;
203 * but traditionally ramfs has ignored all mount options,
204 * and as it is used as a !CONFIG_SHMEM simple substitute
205 * for tmpfs, better continue to ignore other mount options.
206 */
207 }
208 }
209
210 return 0;
211}
212
213int ramfs_fill_super(struct super_block *sb, void *data, int silent)
214{
215 struct ramfs_fs_info *fsi;
216 struct inode *inode;
217 int err;
218
219 save_mount_options(sb, data);
220
221 fsi = kzalloc(sizeof(struct ramfs_fs_info), GFP_KERNEL);
222 sb->s_fs_info = fsi;
223 if (!fsi)
224 return -ENOMEM;
225
226 err = ramfs_parse_options(data, &fsi->mount_opts);
227 if (err)
228 return err;
229
230 sb->s_maxbytes = MAX_LFS_FILESIZE;
231 sb->s_blocksize = PAGE_CACHE_SIZE;
232 sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
233 sb->s_magic = RAMFS_MAGIC;
234 sb->s_op = &ramfs_ops;
235 sb->s_time_gran = 1;
236
237 inode = ramfs_get_inode(sb, NULL, S_IFDIR | fsi->mount_opts.mode, 0);
238 sb->s_root = d_make_root(inode);
239 if (!sb->s_root)
240 return -ENOMEM;
241
242 return 0;
243}
244
245struct dentry *ramfs_mount(struct file_system_type *fs_type,
246 int flags, const char *dev_name, void *data)
247{
248 return mount_nodev(fs_type, flags, data, ramfs_fill_super);
249}
250
251static struct dentry *rootfs_mount(struct file_system_type *fs_type,
252 int flags, const char *dev_name, void *data)
253{
254 return mount_nodev(fs_type, flags|MS_NOUSER, data, ramfs_fill_super);
255}
256
257static void ramfs_kill_sb(struct super_block *sb)
258{
259 kfree(sb->s_fs_info);
260 kill_litter_super(sb);
261}
262
263static struct file_system_type ramfs_fs_type = {
264 .name = "ramfs",
265 .mount = ramfs_mount,
266 .kill_sb = ramfs_kill_sb,
267};
268static struct file_system_type rootfs_fs_type = {
269 .name = "rootfs",
270 .mount = rootfs_mount,
271 .kill_sb = kill_litter_super,
272};
273
274static int __init init_ramfs_fs(void)
275{
276 return register_filesystem(&ramfs_fs_type);
277}
278module_init(init_ramfs_fs)
279
280int __init init_rootfs(void)
281{
282 int err;
283
284 err = bdi_init(&ramfs_backing_dev_info);
285 if (err)
286 return err;
287
288 err = register_filesystem(&rootfs_fs_type);
289 if (err)
290 bdi_destroy(&ramfs_backing_dev_info);
291
292 return err;
293}