inode.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  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 <linux/uaccess.h>
  37. #include <linux/fs_context.h>
  38. #include <linux/fs_parser.h>
  39. #include <linux/seq_file.h>
  40. #include "internal.h"
  41. struct ramfs_mount_opts {
  42. umode_t mode;
  43. };
  44. struct ramfs_fs_info {
  45. struct ramfs_mount_opts mount_opts;
  46. };
  47. #define RAMFS_DEFAULT_MODE 0755
  48. static const struct super_operations ramfs_ops;
  49. static const struct inode_operations ramfs_dir_inode_operations;
  50. struct inode *ramfs_get_inode(struct super_block *sb,
  51. const struct inode *dir, umode_t mode, dev_t dev)
  52. {
  53. struct inode * inode = new_inode(sb);
  54. if (inode) {
  55. inode->i_ino = get_next_ino();
  56. inode_init_owner(&nop_mnt_idmap, inode, dir, mode);
  57. inode->i_mapping->a_ops = &ram_aops;
  58. mapping_set_gfp_mask(inode->i_mapping, GFP_HIGHUSER);
  59. mapping_set_unevictable(inode->i_mapping);
  60. simple_inode_init_ts(inode);
  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. inode_nohighmem(inode);
  78. break;
  79. }
  80. }
  81. return inode;
  82. }
  83. /*
  84. * File creation. Allocate an inode, and we're done..
  85. */
  86. /* SMP-safe */
  87. static int
  88. ramfs_mknod(struct mnt_idmap *idmap, struct inode *dir,
  89. struct dentry *dentry, umode_t mode, dev_t dev)
  90. {
  91. struct inode * inode = ramfs_get_inode(dir->i_sb, dir, mode, dev);
  92. int error = -ENOSPC;
  93. if (inode) {
  94. error = security_inode_init_security(inode, dir,
  95. &dentry->d_name, NULL,
  96. NULL);
  97. if (error) {
  98. iput(inode);
  99. goto out;
  100. }
  101. d_make_persistent(dentry, inode);
  102. error = 0;
  103. inode_set_mtime_to_ts(dir, inode_set_ctime_current(dir));
  104. }
  105. out:
  106. return error;
  107. }
  108. static struct dentry *ramfs_mkdir(struct mnt_idmap *idmap, struct inode *dir,
  109. struct dentry *dentry, umode_t mode)
  110. {
  111. int retval = ramfs_mknod(&nop_mnt_idmap, dir, dentry, mode | S_IFDIR, 0);
  112. if (!retval)
  113. inc_nlink(dir);
  114. return ERR_PTR(retval);
  115. }
  116. static int ramfs_create(struct mnt_idmap *idmap, struct inode *dir,
  117. struct dentry *dentry, umode_t mode, bool excl)
  118. {
  119. return ramfs_mknod(&nop_mnt_idmap, dir, dentry, mode | S_IFREG, 0);
  120. }
  121. static int ramfs_symlink(struct mnt_idmap *idmap, struct inode *dir,
  122. struct dentry *dentry, const char *symname)
  123. {
  124. struct inode *inode;
  125. int error = -ENOSPC;
  126. inode = ramfs_get_inode(dir->i_sb, dir, S_IFLNK|S_IRWXUGO, 0);
  127. if (inode) {
  128. int l = strlen(symname)+1;
  129. error = security_inode_init_security(inode, dir,
  130. &dentry->d_name, NULL,
  131. NULL);
  132. if (error) {
  133. iput(inode);
  134. goto out;
  135. }
  136. error = page_symlink(inode, symname, l);
  137. if (!error) {
  138. d_make_persistent(dentry, inode);
  139. inode_set_mtime_to_ts(dir,
  140. inode_set_ctime_current(dir));
  141. } else
  142. iput(inode);
  143. }
  144. out:
  145. return error;
  146. }
  147. static int ramfs_tmpfile(struct mnt_idmap *idmap,
  148. struct inode *dir, struct file *file, umode_t mode)
  149. {
  150. struct inode *inode;
  151. int error;
  152. inode = ramfs_get_inode(dir->i_sb, dir, mode, 0);
  153. if (!inode)
  154. return -ENOSPC;
  155. error = security_inode_init_security(inode, dir,
  156. &file_dentry(file)->d_name, NULL,
  157. NULL);
  158. if (error) {
  159. iput(inode);
  160. goto out;
  161. }
  162. d_tmpfile(file, inode);
  163. out:
  164. return finish_open_simple(file, error);
  165. }
  166. static const struct inode_operations ramfs_dir_inode_operations = {
  167. .create = ramfs_create,
  168. .lookup = simple_lookup,
  169. .link = simple_link,
  170. .unlink = simple_unlink,
  171. .symlink = ramfs_symlink,
  172. .mkdir = ramfs_mkdir,
  173. .rmdir = simple_rmdir,
  174. .mknod = ramfs_mknod,
  175. .rename = simple_rename,
  176. .tmpfile = ramfs_tmpfile,
  177. };
  178. /*
  179. * Display the mount options in /proc/mounts.
  180. */
  181. static int ramfs_show_options(struct seq_file *m, struct dentry *root)
  182. {
  183. struct ramfs_fs_info *fsi = root->d_sb->s_fs_info;
  184. if (fsi->mount_opts.mode != RAMFS_DEFAULT_MODE)
  185. seq_printf(m, ",mode=%o", fsi->mount_opts.mode);
  186. return 0;
  187. }
  188. static const struct super_operations ramfs_ops = {
  189. .statfs = simple_statfs,
  190. .drop_inode = inode_just_drop,
  191. .show_options = ramfs_show_options,
  192. };
  193. enum ramfs_param {
  194. Opt_mode,
  195. };
  196. const struct fs_parameter_spec ramfs_fs_parameters[] = {
  197. fsparam_u32oct("mode", Opt_mode),
  198. {}
  199. };
  200. static int ramfs_parse_param(struct fs_context *fc, struct fs_parameter *param)
  201. {
  202. struct fs_parse_result result;
  203. struct ramfs_fs_info *fsi = fc->s_fs_info;
  204. int opt;
  205. opt = fs_parse(fc, ramfs_fs_parameters, param, &result);
  206. if (opt == -ENOPARAM) {
  207. opt = vfs_parse_fs_param_source(fc, param);
  208. if (opt != -ENOPARAM)
  209. return opt;
  210. /*
  211. * We might like to report bad mount options here;
  212. * but traditionally ramfs has ignored all mount options,
  213. * and as it is used as a !CONFIG_SHMEM simple substitute
  214. * for tmpfs, better continue to ignore other mount options.
  215. */
  216. return 0;
  217. }
  218. if (opt < 0)
  219. return opt;
  220. switch (opt) {
  221. case Opt_mode:
  222. fsi->mount_opts.mode = result.uint_32 & S_IALLUGO;
  223. break;
  224. }
  225. return 0;
  226. }
  227. static int ramfs_fill_super(struct super_block *sb, struct fs_context *fc)
  228. {
  229. struct ramfs_fs_info *fsi = sb->s_fs_info;
  230. struct inode *inode;
  231. sb->s_maxbytes = MAX_LFS_FILESIZE;
  232. sb->s_blocksize = PAGE_SIZE;
  233. sb->s_blocksize_bits = PAGE_SHIFT;
  234. sb->s_magic = RAMFS_MAGIC;
  235. sb->s_op = &ramfs_ops;
  236. sb->s_d_flags = DCACHE_DONTCACHE;
  237. sb->s_time_gran = 1;
  238. inode = ramfs_get_inode(sb, NULL, S_IFDIR | fsi->mount_opts.mode, 0);
  239. sb->s_root = d_make_root(inode);
  240. if (!sb->s_root)
  241. return -ENOMEM;
  242. return 0;
  243. }
  244. static int ramfs_get_tree(struct fs_context *fc)
  245. {
  246. return get_tree_nodev(fc, ramfs_fill_super);
  247. }
  248. static void ramfs_free_fc(struct fs_context *fc)
  249. {
  250. kfree(fc->s_fs_info);
  251. }
  252. static const struct fs_context_operations ramfs_context_ops = {
  253. .free = ramfs_free_fc,
  254. .parse_param = ramfs_parse_param,
  255. .get_tree = ramfs_get_tree,
  256. };
  257. int ramfs_init_fs_context(struct fs_context *fc)
  258. {
  259. struct ramfs_fs_info *fsi;
  260. fsi = kzalloc_obj(*fsi);
  261. if (!fsi)
  262. return -ENOMEM;
  263. fsi->mount_opts.mode = RAMFS_DEFAULT_MODE;
  264. fc->s_fs_info = fsi;
  265. fc->ops = &ramfs_context_ops;
  266. return 0;
  267. }
  268. void ramfs_kill_sb(struct super_block *sb)
  269. {
  270. kfree(sb->s_fs_info);
  271. kill_anon_super(sb);
  272. }
  273. static struct file_system_type ramfs_fs_type = {
  274. .name = "ramfs",
  275. .init_fs_context = ramfs_init_fs_context,
  276. .parameters = ramfs_fs_parameters,
  277. .kill_sb = ramfs_kill_sb,
  278. .fs_flags = FS_USERNS_MOUNT,
  279. };
  280. static int __init init_ramfs_fs(void)
  281. {
  282. return register_filesystem(&ramfs_fs_type);
  283. }
  284. fs_initcall(init_ramfs_fs);