inode.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  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. * NOTE! This filesystem is probably most useful
  12. * not as a real filesystem, but as an example of
  13. * how virtual filesystems can be written.
  14. *
  15. * It doesn't get much simpler than this. Consider
  16. * that this file implements the full semantics of
  17. * a POSIX-compliant read-write filesystem.
  18. *
  19. * Note in particular how the filesystem does not
  20. * need to implement any data structures of its own
  21. * to keep track of the virtual data: using the VFS
  22. * caches is sufficient.
  23. */
  24. #include <linux/fs.h>
  25. #include <linux/pagemap.h>
  26. #include <linux/highmem.h>
  27. #include <linux/time.h>
  28. #include <linux/init.h>
  29. #include <linux/string.h>
  30. #include <linux/backing-dev.h>
  31. #include <linux/ramfs.h>
  32. #include <linux/sched.h>
  33. #include <linux/parser.h>
  34. #include <linux/magic.h>
  35. #include <linux/slab.h>
  36. #include <linux/uaccess.h>
  37. #include "internal.h"
  38. struct ramfs_mount_opts {
  39. umode_t mode;
  40. };
  41. struct ramfs_fs_info {
  42. struct ramfs_mount_opts mount_opts;
  43. };
  44. #define RAMFS_DEFAULT_MODE 0755
  45. static const struct super_operations ramfs_ops;
  46. static const struct inode_operations ramfs_dir_inode_operations;
  47. static const struct address_space_operations ramfs_aops = {
  48. .readpage = simple_readpage,
  49. .write_begin = simple_write_begin,
  50. .write_end = simple_write_end,
  51. .set_page_dirty = __set_page_dirty_no_writeback,
  52. };
  53. struct inode *ramfs_get_inode(struct super_block *sb,
  54. const struct inode *dir, umode_t mode, dev_t dev)
  55. {
  56. struct inode * inode = new_inode(sb);
  57. if (inode) {
  58. inode->i_ino = get_next_ino();
  59. inode_init_owner(inode, dir, mode);
  60. inode->i_mapping->a_ops = &ramfs_aops;
  61. mapping_set_gfp_mask(inode->i_mapping, GFP_HIGHUSER);
  62. mapping_set_unevictable(inode->i_mapping);
  63. inode->i_atime = inode->i_mtime = inode->i_ctime = current_time(inode);
  64. switch (mode & S_IFMT) {
  65. default:
  66. init_special_inode(inode, mode, dev);
  67. break;
  68. case S_IFREG:
  69. inode->i_op = &ramfs_file_inode_operations;
  70. inode->i_fop = &ramfs_file_operations;
  71. break;
  72. case S_IFDIR:
  73. inode->i_op = &ramfs_dir_inode_operations;
  74. inode->i_fop = &simple_dir_operations;
  75. /* directory inodes start off with i_nlink == 2 (for "." entry) */
  76. inc_nlink(inode);
  77. break;
  78. case S_IFLNK:
  79. inode->i_op = &page_symlink_inode_operations;
  80. inode_nohighmem(inode);
  81. break;
  82. }
  83. }
  84. return inode;
  85. }
  86. /*
  87. * File creation. Allocate an inode, and we're done..
  88. */
  89. /* SMP-safe */
  90. static int
  91. ramfs_mknod(struct inode *dir, struct dentry *dentry, umode_t mode, dev_t dev)
  92. {
  93. struct inode * inode = ramfs_get_inode(dir->i_sb, dir, mode, dev);
  94. int error = -ENOSPC;
  95. if (inode) {
  96. d_instantiate(dentry, inode);
  97. dget(dentry); /* Extra count - pin the dentry in core */
  98. error = 0;
  99. dir->i_mtime = dir->i_ctime = current_time(dir);
  100. }
  101. return error;
  102. }
  103. static int ramfs_mkdir(struct inode * dir, struct dentry * dentry, umode_t mode)
  104. {
  105. int retval = ramfs_mknod(dir, dentry, mode | S_IFDIR, 0);
  106. if (!retval)
  107. inc_nlink(dir);
  108. return retval;
  109. }
  110. static int ramfs_create(struct inode *dir, struct dentry *dentry, umode_t mode, bool excl)
  111. {
  112. return ramfs_mknod(dir, dentry, mode | S_IFREG, 0);
  113. }
  114. static int ramfs_symlink(struct inode * dir, struct dentry *dentry, const char * symname)
  115. {
  116. struct inode *inode;
  117. int error = -ENOSPC;
  118. inode = ramfs_get_inode(dir->i_sb, dir, S_IFLNK|S_IRWXUGO, 0);
  119. if (inode) {
  120. int l = strlen(symname)+1;
  121. error = page_symlink(inode, symname, l);
  122. if (!error) {
  123. d_instantiate(dentry, inode);
  124. dget(dentry);
  125. dir->i_mtime = dir->i_ctime = current_time(dir);
  126. } else
  127. iput(inode);
  128. }
  129. return error;
  130. }
  131. static const struct inode_operations ramfs_dir_inode_operations = {
  132. .create = ramfs_create,
  133. .lookup = simple_lookup,
  134. .link = simple_link,
  135. .unlink = simple_unlink,
  136. .symlink = ramfs_symlink,
  137. .mkdir = ramfs_mkdir,
  138. .rmdir = simple_rmdir,
  139. .mknod = ramfs_mknod,
  140. .rename = simple_rename,
  141. };
  142. /*
  143. * Display the mount options in /proc/mounts.
  144. */
  145. static int ramfs_show_options(struct seq_file *m, struct dentry *root)
  146. {
  147. struct ramfs_fs_info *fsi = root->d_sb->s_fs_info;
  148. if (fsi->mount_opts.mode != RAMFS_DEFAULT_MODE)
  149. seq_printf(m, ",mode=%o", fsi->mount_opts.mode);
  150. return 0;
  151. }
  152. static const struct super_operations ramfs_ops = {
  153. .statfs = simple_statfs,
  154. .drop_inode = generic_delete_inode,
  155. .show_options = ramfs_show_options,
  156. };
  157. enum {
  158. Opt_mode,
  159. Opt_err
  160. };
  161. static const match_table_t tokens = {
  162. {Opt_mode, "mode=%o"},
  163. {Opt_err, NULL}
  164. };
  165. static int ramfs_parse_options(char *data, struct ramfs_mount_opts *opts)
  166. {
  167. substring_t args[MAX_OPT_ARGS];
  168. int option;
  169. int token;
  170. char *p;
  171. opts->mode = RAMFS_DEFAULT_MODE;
  172. while ((p = strsep(&data, ",")) != NULL) {
  173. if (!*p)
  174. continue;
  175. token = match_token(p, tokens, args);
  176. switch (token) {
  177. case Opt_mode:
  178. if (match_octal(&args[0], &option))
  179. return -EINVAL;
  180. opts->mode = option & S_IALLUGO;
  181. break;
  182. /*
  183. * We might like to report bad mount options here;
  184. * but traditionally ramfs has ignored all mount options,
  185. * and as it is used as a !CONFIG_SHMEM simple substitute
  186. * for tmpfs, better continue to ignore other mount options.
  187. */
  188. }
  189. }
  190. return 0;
  191. }
  192. int ramfs_fill_super(struct super_block *sb, void *data, int silent)
  193. {
  194. struct ramfs_fs_info *fsi;
  195. struct inode *inode;
  196. int err;
  197. fsi = kzalloc(sizeof(struct ramfs_fs_info), GFP_KERNEL);
  198. sb->s_fs_info = fsi;
  199. if (!fsi)
  200. return -ENOMEM;
  201. err = ramfs_parse_options(data, &fsi->mount_opts);
  202. if (err)
  203. return err;
  204. sb->s_maxbytes = MAX_LFS_FILESIZE;
  205. sb->s_blocksize = PAGE_SIZE;
  206. sb->s_blocksize_bits = PAGE_SHIFT;
  207. sb->s_magic = RAMFS_MAGIC;
  208. sb->s_op = &ramfs_ops;
  209. sb->s_time_gran = 1;
  210. inode = ramfs_get_inode(sb, NULL, S_IFDIR | fsi->mount_opts.mode, 0);
  211. sb->s_root = d_make_root(inode);
  212. if (!sb->s_root)
  213. return -ENOMEM;
  214. return 0;
  215. }
  216. struct dentry *ramfs_mount(struct file_system_type *fs_type,
  217. int flags, const char *dev_name, void *data)
  218. {
  219. return mount_nodev(fs_type, flags, data, ramfs_fill_super);
  220. }
  221. static void ramfs_kill_sb(struct super_block *sb)
  222. {
  223. kfree(sb->s_fs_info);
  224. kill_litter_super(sb);
  225. }
  226. static struct file_system_type ramfs_fs_type = {
  227. .name = "ramfs",
  228. .mount = ramfs_mount,
  229. .kill_sb = ramfs_kill_sb,
  230. .fs_flags = FS_USERNS_MOUNT,
  231. };
  232. int __init init_ramfs_fs(void)
  233. {
  234. static unsigned long once;
  235. if (test_and_set_bit(0, &once))
  236. return 0;
  237. return register_filesystem(&ramfs_fs_type);
  238. }
  239. fs_initcall(init_ramfs_fs);