Back to home page

OSCL-LXR

 
 

    


0001 /*
0002  * Resizable simple ram filesystem for Linux.
0003  *
0004  * Copyright (C) 2000 Linus Torvalds.
0005  *               2000 Transmeta Corp.
0006  *
0007  * Usage limits added by David Gibson, Linuxcare Australia.
0008  * This file is released under the GPL.
0009  */
0010 
0011 /*
0012  * NOTE! This filesystem is probably most useful
0013  * not as a real filesystem, but as an example of
0014  * how virtual filesystems can be written.
0015  *
0016  * It doesn't get much simpler than this. Consider
0017  * that this file implements the full semantics of
0018  * a POSIX-compliant read-write filesystem.
0019  *
0020  * Note in particular how the filesystem does not
0021  * need to implement any data structures of its own
0022  * to keep track of the virtual data: using the VFS
0023  * caches is sufficient.
0024  */
0025 
0026 #include <linux/fs.h>
0027 #include <linux/pagemap.h>
0028 #include <linux/highmem.h>
0029 #include <linux/time.h>
0030 #include <linux/init.h>
0031 #include <linux/string.h>
0032 #include <linux/backing-dev.h>
0033 #include <linux/ramfs.h>
0034 #include <linux/sched.h>
0035 #include <linux/parser.h>
0036 #include <linux/magic.h>
0037 #include <linux/slab.h>
0038 #include <linux/uaccess.h>
0039 #include <linux/fs_context.h>
0040 #include <linux/fs_parser.h>
0041 #include <linux/seq_file.h>
0042 #include "internal.h"
0043 
0044 struct ramfs_mount_opts {
0045     umode_t mode;
0046 };
0047 
0048 struct ramfs_fs_info {
0049     struct ramfs_mount_opts mount_opts;
0050 };
0051 
0052 #define RAMFS_DEFAULT_MODE  0755
0053 
0054 static const struct super_operations ramfs_ops;
0055 static const struct inode_operations ramfs_dir_inode_operations;
0056 
0057 struct inode *ramfs_get_inode(struct super_block *sb,
0058                 const struct inode *dir, umode_t mode, dev_t dev)
0059 {
0060     struct inode * inode = new_inode(sb);
0061 
0062     if (inode) {
0063         inode->i_ino = get_next_ino();
0064         inode_init_owner(&init_user_ns, inode, dir, mode);
0065         inode->i_mapping->a_ops = &ram_aops;
0066         mapping_set_gfp_mask(inode->i_mapping, GFP_HIGHUSER);
0067         mapping_set_unevictable(inode->i_mapping);
0068         inode->i_atime = inode->i_mtime = inode->i_ctime = current_time(inode);
0069         switch (mode & S_IFMT) {
0070         default:
0071             init_special_inode(inode, mode, dev);
0072             break;
0073         case S_IFREG:
0074             inode->i_op = &ramfs_file_inode_operations;
0075             inode->i_fop = &ramfs_file_operations;
0076             break;
0077         case S_IFDIR:
0078             inode->i_op = &ramfs_dir_inode_operations;
0079             inode->i_fop = &simple_dir_operations;
0080 
0081             /* directory inodes start off with i_nlink == 2 (for "." entry) */
0082             inc_nlink(inode);
0083             break;
0084         case S_IFLNK:
0085             inode->i_op = &page_symlink_inode_operations;
0086             inode_nohighmem(inode);
0087             break;
0088         }
0089     }
0090     return inode;
0091 }
0092 
0093 /*
0094  * File creation. Allocate an inode, and we're done..
0095  */
0096 /* SMP-safe */
0097 static int
0098 ramfs_mknod(struct user_namespace *mnt_userns, struct inode *dir,
0099         struct dentry *dentry, umode_t mode, dev_t dev)
0100 {
0101     struct inode * inode = ramfs_get_inode(dir->i_sb, dir, mode, dev);
0102     int error = -ENOSPC;
0103 
0104     if (inode) {
0105         d_instantiate(dentry, inode);
0106         dget(dentry);   /* Extra count - pin the dentry in core */
0107         error = 0;
0108         dir->i_mtime = dir->i_ctime = current_time(dir);
0109     }
0110     return error;
0111 }
0112 
0113 static int ramfs_mkdir(struct user_namespace *mnt_userns, struct inode *dir,
0114                struct dentry *dentry, umode_t mode)
0115 {
0116     int retval = ramfs_mknod(&init_user_ns, dir, dentry, mode | S_IFDIR, 0);
0117     if (!retval)
0118         inc_nlink(dir);
0119     return retval;
0120 }
0121 
0122 static int ramfs_create(struct user_namespace *mnt_userns, struct inode *dir,
0123             struct dentry *dentry, umode_t mode, bool excl)
0124 {
0125     return ramfs_mknod(&init_user_ns, dir, dentry, mode | S_IFREG, 0);
0126 }
0127 
0128 static int ramfs_symlink(struct user_namespace *mnt_userns, struct inode *dir,
0129              struct dentry *dentry, const char *symname)
0130 {
0131     struct inode *inode;
0132     int error = -ENOSPC;
0133 
0134     inode = ramfs_get_inode(dir->i_sb, dir, S_IFLNK|S_IRWXUGO, 0);
0135     if (inode) {
0136         int l = strlen(symname)+1;
0137         error = page_symlink(inode, symname, l);
0138         if (!error) {
0139             d_instantiate(dentry, inode);
0140             dget(dentry);
0141             dir->i_mtime = dir->i_ctime = current_time(dir);
0142         } else
0143             iput(inode);
0144     }
0145     return error;
0146 }
0147 
0148 static int ramfs_tmpfile(struct user_namespace *mnt_userns,
0149              struct inode *dir, struct dentry *dentry, umode_t mode)
0150 {
0151     struct inode *inode;
0152 
0153     inode = ramfs_get_inode(dir->i_sb, dir, mode, 0);
0154     if (!inode)
0155         return -ENOSPC;
0156     d_tmpfile(dentry, inode);
0157     return 0;
0158 }
0159 
0160 static const struct inode_operations ramfs_dir_inode_operations = {
0161     .create     = ramfs_create,
0162     .lookup     = simple_lookup,
0163     .link       = simple_link,
0164     .unlink     = simple_unlink,
0165     .symlink    = ramfs_symlink,
0166     .mkdir      = ramfs_mkdir,
0167     .rmdir      = simple_rmdir,
0168     .mknod      = ramfs_mknod,
0169     .rename     = simple_rename,
0170     .tmpfile    = ramfs_tmpfile,
0171 };
0172 
0173 /*
0174  * Display the mount options in /proc/mounts.
0175  */
0176 static int ramfs_show_options(struct seq_file *m, struct dentry *root)
0177 {
0178     struct ramfs_fs_info *fsi = root->d_sb->s_fs_info;
0179 
0180     if (fsi->mount_opts.mode != RAMFS_DEFAULT_MODE)
0181         seq_printf(m, ",mode=%o", fsi->mount_opts.mode);
0182     return 0;
0183 }
0184 
0185 static const struct super_operations ramfs_ops = {
0186     .statfs     = simple_statfs,
0187     .drop_inode = generic_delete_inode,
0188     .show_options   = ramfs_show_options,
0189 };
0190 
0191 enum ramfs_param {
0192     Opt_mode,
0193 };
0194 
0195 const struct fs_parameter_spec ramfs_fs_parameters[] = {
0196     fsparam_u32oct("mode",  Opt_mode),
0197     {}
0198 };
0199 
0200 static int ramfs_parse_param(struct fs_context *fc, struct fs_parameter *param)
0201 {
0202     struct fs_parse_result result;
0203     struct ramfs_fs_info *fsi = fc->s_fs_info;
0204     int opt;
0205 
0206     opt = fs_parse(fc, ramfs_fs_parameters, param, &result);
0207     if (opt == -ENOPARAM) {
0208         opt = vfs_parse_fs_param_source(fc, param);
0209         if (opt != -ENOPARAM)
0210             return opt;
0211         /*
0212          * We might like to report bad mount options here;
0213          * but traditionally ramfs has ignored all mount options,
0214          * and as it is used as a !CONFIG_SHMEM simple substitute
0215          * for tmpfs, better continue to ignore other mount options.
0216          */
0217         return 0;
0218     }
0219     if (opt < 0)
0220         return opt;
0221 
0222     switch (opt) {
0223     case Opt_mode:
0224         fsi->mount_opts.mode = result.uint_32 & S_IALLUGO;
0225         break;
0226     }
0227 
0228     return 0;
0229 }
0230 
0231 static int ramfs_fill_super(struct super_block *sb, struct fs_context *fc)
0232 {
0233     struct ramfs_fs_info *fsi = sb->s_fs_info;
0234     struct inode *inode;
0235 
0236     sb->s_maxbytes      = MAX_LFS_FILESIZE;
0237     sb->s_blocksize     = PAGE_SIZE;
0238     sb->s_blocksize_bits    = PAGE_SHIFT;
0239     sb->s_magic     = RAMFS_MAGIC;
0240     sb->s_op        = &ramfs_ops;
0241     sb->s_time_gran     = 1;
0242 
0243     inode = ramfs_get_inode(sb, NULL, S_IFDIR | fsi->mount_opts.mode, 0);
0244     sb->s_root = d_make_root(inode);
0245     if (!sb->s_root)
0246         return -ENOMEM;
0247 
0248     return 0;
0249 }
0250 
0251 static int ramfs_get_tree(struct fs_context *fc)
0252 {
0253     return get_tree_nodev(fc, ramfs_fill_super);
0254 }
0255 
0256 static void ramfs_free_fc(struct fs_context *fc)
0257 {
0258     kfree(fc->s_fs_info);
0259 }
0260 
0261 static const struct fs_context_operations ramfs_context_ops = {
0262     .free       = ramfs_free_fc,
0263     .parse_param    = ramfs_parse_param,
0264     .get_tree   = ramfs_get_tree,
0265 };
0266 
0267 int ramfs_init_fs_context(struct fs_context *fc)
0268 {
0269     struct ramfs_fs_info *fsi;
0270 
0271     fsi = kzalloc(sizeof(*fsi), GFP_KERNEL);
0272     if (!fsi)
0273         return -ENOMEM;
0274 
0275     fsi->mount_opts.mode = RAMFS_DEFAULT_MODE;
0276     fc->s_fs_info = fsi;
0277     fc->ops = &ramfs_context_ops;
0278     return 0;
0279 }
0280 
0281 static void ramfs_kill_sb(struct super_block *sb)
0282 {
0283     kfree(sb->s_fs_info);
0284     kill_litter_super(sb);
0285 }
0286 
0287 static struct file_system_type ramfs_fs_type = {
0288     .name       = "ramfs",
0289     .init_fs_context = ramfs_init_fs_context,
0290     .parameters = ramfs_fs_parameters,
0291     .kill_sb    = ramfs_kill_sb,
0292     .fs_flags   = FS_USERNS_MOUNT,
0293 };
0294 
0295 static int __init init_ramfs_fs(void)
0296 {
0297     return register_filesystem(&ramfs_fs_type);
0298 }
0299 fs_initcall(init_ramfs_fs);