inode.c 6.7 KB

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