inode.c 6.2 KB

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