inode.c 7.1 KB

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