inode.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  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 <asm/uaccess.h>
  36. #include "internal.h"
  37. /* some random number */
  38. #define RAMFS_MAGIC 0x858458f6
  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. .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, int mode, dev_t dev)
  49. {
  50. struct inode * inode = new_inode(sb);
  51. if (inode) {
  52. inode->i_mode = mode;
  53. inode->i_uid = current_fsuid();
  54. inode->i_gid = current_fsgid();
  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, int mode, dev_t dev)
  87. {
  88. struct inode * inode = ramfs_get_inode(dir->i_sb, mode, dev);
  89. int error = -ENOSPC;
  90. if (inode) {
  91. if (dir->i_mode & S_ISGID) {
  92. inode->i_gid = dir->i_gid;
  93. if (S_ISDIR(mode))
  94. inode->i_mode |= S_ISGID;
  95. }
  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;
  100. }
  101. return error;
  102. }
  103. static int ramfs_mkdir(struct inode * dir, struct dentry * dentry, int 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, int mode, struct nameidata *nd)
  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, 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. if (dir->i_mode & S_ISGID)
  124. inode->i_gid = dir->i_gid;
  125. d_instantiate(dentry, inode);
  126. dget(dentry);
  127. dir->i_mtime = dir->i_ctime = CURRENT_TIME;
  128. } else
  129. iput(inode);
  130. }
  131. return error;
  132. }
  133. static const struct inode_operations ramfs_dir_inode_operations = {
  134. .create = ramfs_create,
  135. .lookup = simple_lookup,
  136. .link = simple_link,
  137. .unlink = simple_unlink,
  138. .symlink = ramfs_symlink,
  139. .mkdir = ramfs_mkdir,
  140. .rmdir = simple_rmdir,
  141. .mknod = ramfs_mknod,
  142. .rename = simple_rename,
  143. };
  144. static const struct super_operations ramfs_ops = {
  145. .statfs = simple_statfs,
  146. .drop_inode = generic_delete_inode,
  147. .show_options = generic_show_options,
  148. };
  149. struct ramfs_mount_opts {
  150. umode_t mode;
  151. };
  152. enum {
  153. Opt_mode,
  154. Opt_err
  155. };
  156. static const match_table_t tokens = {
  157. {Opt_mode, "mode=%o"},
  158. {Opt_err, NULL}
  159. };
  160. struct ramfs_fs_info {
  161. struct ramfs_mount_opts mount_opts;
  162. };
  163. static int ramfs_parse_options(char *data, struct ramfs_mount_opts *opts)
  164. {
  165. substring_t args[MAX_OPT_ARGS];
  166. int option;
  167. int token;
  168. char *p;
  169. opts->mode = RAMFS_DEFAULT_MODE;
  170. while ((p = strsep(&data, ",")) != NULL) {
  171. if (!*p)
  172. continue;
  173. token = match_token(p, tokens, args);
  174. switch (token) {
  175. case Opt_mode:
  176. if (match_octal(&args[0], &option))
  177. return -EINVAL;
  178. opts->mode = option & S_IALLUGO;
  179. break;
  180. /*
  181. * We might like to report bad mount options here;
  182. * but traditionally ramfs has ignored all mount options,
  183. * and as it is used as a !CONFIG_SHMEM simple substitute
  184. * for tmpfs, better continue to ignore other mount options.
  185. */
  186. }
  187. }
  188. return 0;
  189. }
  190. static int ramfs_fill_super(struct super_block * sb, void * data, int silent)
  191. {
  192. struct ramfs_fs_info *fsi;
  193. struct inode *inode = NULL;
  194. struct dentry *root;
  195. int err;
  196. save_mount_options(sb, data);
  197. fsi = kzalloc(sizeof(struct ramfs_fs_info), GFP_KERNEL);
  198. sb->s_fs_info = fsi;
  199. if (!fsi) {
  200. err = -ENOMEM;
  201. goto fail;
  202. }
  203. err = ramfs_parse_options(data, &fsi->mount_opts);
  204. if (err)
  205. goto fail;
  206. sb->s_maxbytes = MAX_LFS_FILESIZE;
  207. sb->s_blocksize = PAGE_CACHE_SIZE;
  208. sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
  209. sb->s_magic = RAMFS_MAGIC;
  210. sb->s_op = &ramfs_ops;
  211. sb->s_time_gran = 1;
  212. inode = ramfs_get_inode(sb, S_IFDIR | fsi->mount_opts.mode, 0);
  213. if (!inode) {
  214. err = -ENOMEM;
  215. goto fail;
  216. }
  217. root = d_alloc_root(inode);
  218. sb->s_root = root;
  219. if (!root) {
  220. err = -ENOMEM;
  221. goto fail;
  222. }
  223. return 0;
  224. fail:
  225. kfree(fsi);
  226. sb->s_fs_info = NULL;
  227. iput(inode);
  228. return err;
  229. }
  230. int ramfs_get_sb(struct file_system_type *fs_type,
  231. int flags, const char *dev_name, void *data, struct vfsmount *mnt)
  232. {
  233. return get_sb_nodev(fs_type, flags, data, ramfs_fill_super, mnt);
  234. }
  235. static int rootfs_get_sb(struct file_system_type *fs_type,
  236. int flags, const char *dev_name, void *data, struct vfsmount *mnt)
  237. {
  238. return get_sb_nodev(fs_type, flags|MS_NOUSER, data, ramfs_fill_super,
  239. mnt);
  240. }
  241. static void ramfs_kill_sb(struct super_block *sb)
  242. {
  243. kfree(sb->s_fs_info);
  244. kill_litter_super(sb);
  245. }
  246. static struct file_system_type ramfs_fs_type = {
  247. .name = "ramfs",
  248. .get_sb = ramfs_get_sb,
  249. .kill_sb = ramfs_kill_sb,
  250. };
  251. static struct file_system_type rootfs_fs_type = {
  252. .name = "rootfs",
  253. .get_sb = rootfs_get_sb,
  254. .kill_sb = kill_litter_super,
  255. };
  256. static int __init init_ramfs_fs(void)
  257. {
  258. return register_filesystem(&ramfs_fs_type);
  259. }
  260. static void __exit exit_ramfs_fs(void)
  261. {
  262. unregister_filesystem(&ramfs_fs_type);
  263. }
  264. module_init(init_ramfs_fs)
  265. module_exit(exit_ramfs_fs)
  266. int __init init_rootfs(void)
  267. {
  268. int err;
  269. err = bdi_init(&ramfs_backing_dev_info);
  270. if (err)
  271. return err;
  272. err = register_filesystem(&rootfs_fs_type);
  273. if (err)
  274. bdi_destroy(&ramfs_backing_dev_info);
  275. return err;
  276. }
  277. MODULE_LICENSE("GPL");