inode.c 6.2 KB

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