/** Resizable simple ram filesystem for Linux. ** Copyright (C) 2000 Linus Torvalds. * 2000 Transmeta Corp. ** Usage limits added by David Gibson, Linuxcare Australia. * This file is released under the GPL. * // ** NOTE! This filesystem is probably most useful * not as a real filesystem, but as an example of * how virtual filesystems can be written. ** It doesn' t get much simpler than this. consider * that this file implements the full semantics of * a POSIX-compliant read-write filesystem. ** Note in particle how the filesystem does not * need to implement any data structures of its own * to keep track of Virtual data: using the VFS * caches is sufficient. */# include <linux/module. h> # include <linux/fs. h> # include <linux/pagemap. h> # include <linux/highmem. h> # include <linux/time. h> # include <linux/init. h> # include <linux/string. h> # include <linux/backing-dev.h> # include <linux/ramfs. h> # include <linux/sched. h> # include <linux/parser. h> # include <linux/magic. h> # include <asm/uaccess. h> # include "internal. h "# def Ine RAMFS_DEFAULT_MODE0755static const struct super_operations ramfs_ops;/* callback function for ramfs operations */static const strude_operations ramfs_dir_inode_operations; /* operation callback function for directory inode * // * describes the underlying block device */static struct backing_dev_info ramfs_backing_dev_info = {. name = "ramfs ",. ra_pages = 0,/* No readahead because ramfs is directly stored in the cache, pre-reading is not required * // * describes the functions of the underlying block device, BDI_CAP_NO_ACCT_AND_WRITEBACK indicates that dirty pages are not written back, dirty pages are not counted, and dirty pages are not automatically written back. BDI_CA P_MAP_DIRECT indicates that the block device supports the mmap operation. MAP_PRIVATE partition indicates that the block device supports the mmap operation. MAP_PRIVATE partition indicates that the block device supports the mmap operation. PROT_READ bdi_cape_map indicates that the block device supports the mmap operation. PROT_EXEC */. capabilities = bytes | BDI_CAP_MAP_DIRECT | BDI_CAP_MAP_COPY | BDI_CAP_READ_MAP | BDI_CAP_WRITE_MAP | bytes,};/* Create an inode */struct inode * ramfs_get_inode Ct super_block * sb, int mode, dev_t dev) {/* allocate an inode space from the memory */struct inode * inode = new_inode (sb); if (inode) {/* fill in inode structure */inode-> I _mode = mode;/* file type */inode-> I _uid = current_fsuid (); /* obtain the UID of the current process */inode-> I _gid = current_fsgid ();/* obtain the GID of the current process */inode-> I _mapping-> a_ops = & ramfs_aops; /* register the Memory Operation callback */inode-> I _mapping-> backing_dev_info = & ramfs_backing_dev_info;/* Save the underlying block device information */mapping_set_gf P_mask (inode-> I _mapping, GFP_HIGHUSER);/* allocate memory address space for inode */mapping_set_unevictable (inode-> I _mapping ); inode-> I _atime = inode-> I _mtime = inode-> I _ctime = CURRENT_TIME; switch (mode & S_IFMT) {default:/* handle special inode, including socket, fifo, block device, character device */init_special_inode (inode, mode, dev); break; case S_IFREG:/* Common file, register the callback function */inode-> I _op = & ramfs_file_inode_operations; inode-> I _fop = & ramfs_file_operations; break; case S_IFDIR:/* directory, register the callback function */inode-> I _op = & ramfs_dir_inode_operations; inode-> I _fop = & simple_dir_operations; /* directory inodes start off with I _nlink = 2 (". "entry) * // * increase the file reference count, that is, inode-> I _nlink. The reference count of the directory is 2, because ". "When inode-> I _nlink is 0, this inode is idle */inc_nlink (inode); break; case S_IFLNK: inode-> I _op = & page_symlink_inode_operations; break ;}} return inode;}/** File creation. allocate an inode, and we're Done .. * // * SMP-safe * // * Create a node in the specified directory */static intramfs_mknod (struct inode * dir, struct dentry * dentry, int mode, dev_t dev) {/* Get An inode */struct inode * inode = ramfs_get_inode (dir-> I _sb, mode, dev); int error =-ENOSPC; if (inode) {if (dir-> I _mode & S_ISGID) {/* if mode has a GID, you need to pay the GID to inode */inode-> I _gid = dir-> I _gid; if (S_ISDIR (mode) inode-> I _mode | = S_ISGID;} d_instantiate (dentry, inode );/* Enter inode information */dget (dentry) in the dentry structure;/* Extra count-pin the dentry in core pair dentry-> d_count plus one */error = 0; dir-> I _mtime = dir-> I _ctime = CURRENT_TIME;/* modify the Directory Access time and inode modification time */} return error ;} /* create directory */static int ramfs_mkdir (struct inode * dir, struct dentry * dentry, int mode) {int retval = ramfs_mknod (dir, dentry, mode | S_IFDIR, 0 ); if (! Retval) inc_nlink (dir);/* Add the directory inode-> I _nlink plus one */return retval;}/* Create File */static int ramfs_create (struct inode * dir, struct dentry * dentry, int mode, struct nameidata * nd) {return ramfs_mknod (dir, dentry, mode | S_IFREG, 0 );} /* Create a soft connection */static int ramfs_symlink (struct inode * dir, struct dentry * dentry, const char * symname) {struct inode * inode; int error =-ENOSPC; /* Get An inode */inode = ramfs_get_ino De (dir-> I _sb, S_IFLNK | S_IRWXUGO, 0); if (inode) {int l = strlen (symname) + 1; error = page_symlink (inode, symname, l ); /* write the soft connection to pagecache and set the page to dirty */if (! Error) {if (dir-> I _mode & S_ISGID) inode-> I _gid = dir-> I _gid; d_instantiate (dentry, inode ); /* fill in inode information */dget (dentry) to the dentry structure;/* dentry-> d_count plus 1 */dir-> I _mtime = dir-> I _ctime = CURRENT_TIME; /* set the operation time */} elseiput (inode);} return error;}/* registers the callback function for the inode operation */static const strude_operations ramfs_dir_inode_operations = {. create = ramfs_create ,. lookup = simple_lookup ,. link = simple_link ,. unlink = Simple_unlink ,. symlink = ramfs_symlink ,. mkdir = ramfs_mkdir ,. rmdir = simple_rmdir ,. mknod = ramfs_mknod ,. rename = simple_rename,};/* registers callback for super block operations */static const struct super_operations ramfs_ops = {. statfs = simple_statfs ,. drop_inode = generic_delete_inode ,. show_options = generic_show_options,}; struct ramfs_mount_opts {umode_t mode ;}; enum {Opt_mode, Opt_err}; static const match_table_t tokens ={{ Opt_mode, "m Ode = % o "},{ Opt_err, NULL }}; struct ramfs_fs_info {struct ramfs_mount_opts mount_opts;}; static int ramfs_parse_options (char * data, struct ramfs_mount_opts * opts) {substring_t args [MAX_OPT_ARGS]; int option; int token; char * p; opts-> mode = RAMFS_DEFAULT_MODE; while (p = strsep (& data ,","))! = NULL) {if (! * P) continue; token = match_token (p, tokens, args); switch (token) {case Opt_mode: if (match_octal (& args [0], & option )) return-EINVAL; opts-> mode = option & S_IALLUGO; break;/** We might like to report bad mount options here; * but traditionally ramfs has ignored all mount options, * and as it is used as! CONFIG_SHMEM simple substitute * for tmpfs, better continue to ignore other mount options. * //} return 0;}/* fill super block */static int ramfs_fill_super (struct super_block * sb, void * data, int silent) {struct ramfs_fs_info * fsi; struct inode * inode = NULL; struct dentry * root; int err; save_mount_options (sb, data); fsi = kzarloc (sizeof (struct ramfs_fs_info), GFP_KERNEL ); sb-> s_fs_info = fsi; if (! Fsi) {err =-ENOMEM; goto fail;} err = ramfs_parse_options (data, & fsi-> mount_opts); if (err) goto fail; /* fill in the super block structure */sb-> s_maxbytes = MAX_LFS_FILESIZE; sb-> s_blocksize = PAGE_CACHE_SIZE; sb-> s_blocksize_bits = PAGE_CACHE_SHIFT; sb-> s_magic = RAMFS_MAGIC; sb-> s_op = & ramfs_ops;/* register the super block Operation callback */sb-> s_time_gran = 1;/* assign inode */inode = ramfs_get_inode (sb, s_IFDIR | fsi-> mount_opts.mode, 0); if (! Inode) {err =-ENOMEM; goto fail;}/* allocate cache for root */root = d_alloc_root (inode); sb-> s_root = root; if (! Root) {err =-ENOMEM; goto fail;} return 0; fail:/* Exception Handling */kfree (fsi); sb-> s_fs_info = NULL; iput (inode ); return err;}/* super block loaded with ramfs */int ramfs_get_sb (struct file_system_type * fs_type, int flags, const char * dev_name, void * data, struct vfsmount * mnt) {/* allocate a super block structure (struct super_block) sb to the memory and initialize some of its member variables, insert s_instances to the two-way linked list pointed by fs_supers in the rootfs file system type structure. */Return get_sb_nodev (fs_type, flags, data, ramfs_fill_super, mnt);}/* super block for loading rootfs */static int rootfs_get_sb (struct file_system_type * fs_type, int flags, const char * dev_name, void * data, struct vfsmount * mnt) {return get_sb_nodev (fs_type, flags | MS_NOUSER, data, ramfs_fill_super, mnt );} /* uninstall super block */static void ramfs_kill_sb (struct super_block * sb) {kfree (sb-> s_fs_info); kill_litter_super (sb);} static struct file_system_type ramfs_fs_type = {. name = "ramfs ",. get_sb = ramfs_get_sb ,. kill_sb = ramfs_kill_sb,}; static struct file_system_type rootfs_fs_type = {. name = "rootfs ",. get_sb = rootfs_get_sb ,. kill_sb = kill_litter_super,};/* initialize the module, register the file system */static int _ init init_ramfs_fs (void) {return register_filesystem (& ramfs_fs_type);}/* exit the module, log out of the file system */static void _ exit exit_ramfs_fs (void) {unregister_filesystem (& ramfs_fs_type);} module_init (init_ramfs_fs) module_exit (logout)/* initialize rootfs, during boot, call */int _ init init_rootfs (void) {int err;/* initialize the corresponding block device */err = bdi_init (& ramfs_backing_dev_info ); if (err) return err;/* Register rootfs File System */err = register_filesystem (& rootfs_fs_type); if (err) bdi_destroy (& ramfs_backing_dev_info); return err ;} MODULE_LICENSE ("GPL ");