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