inode.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  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_instantiate(dentry, inode);
  102. dget(dentry); /* Extra count - pin the dentry in core */
  103. error = 0;
  104. inode_set_mtime_to_ts(dir, inode_set_ctime_current(dir));
  105. }
  106. out:
  107. return error;
  108. }
  109. static int ramfs_mkdir(struct mnt_idmap *idmap, struct inode *dir,
  110. struct dentry *dentry, umode_t mode)
  111. {
  112. int retval = ramfs_mknod(&nop_mnt_idmap, dir, dentry, mode | S_IFDIR, 0);
  113. if (!retval)
  114. inc_nlink(dir);
  115. return retval;
  116. }
  117. static int ramfs_create(struct mnt_idmap *idmap, struct inode *dir,
  118. struct dentry *dentry, umode_t mode, bool excl)
  119. {
  120. return ramfs_mknod(&nop_mnt_idmap, dir, dentry, mode | S_IFREG, 0);
  121. }
  122. static int ramfs_symlink(struct mnt_idmap *idmap, struct inode *dir,
  123. struct dentry *dentry, const char *symname)
  124. {
  125. struct inode *inode;
  126. int error = -ENOSPC;
  127. inode = ramfs_get_inode(dir->i_sb, dir, S_IFLNK|S_IRWXUGO, 0);
  128. if (inode) {
  129. int l = strlen(symname)+1;
  130. error = security_inode_init_security(inode, dir,
  131. &dentry->d_name, NULL,
  132. NULL);
  133. if (error) {
  134. iput(inode);
  135. goto out;
  136. }
  137. error = page_symlink(inode, symname, l);
  138. if (!error) {
  139. d_instantiate(dentry, inode);
  140. dget(dentry);
  141. inode_set_mtime_to_ts(dir,
  142. inode_set_ctime_current(dir));
  143. } else
  144. iput(inode);
  145. }
  146. out:
  147. return error;
  148. }
  149. static int ramfs_tmpfile(struct mnt_idmap *idmap,
  150. struct inode *dir, struct file *file, umode_t mode)
  151. {
  152. struct inode *inode;
  153. int error;
  154. inode = ramfs_get_inode(dir->i_sb, dir, mode, 0);
  155. if (!inode)
  156. return -ENOSPC;
  157. error = security_inode_init_security(inode, dir,
  158. &file_dentry(file)->d_name, NULL,
  159. NULL);
  160. if (error) {
  161. iput(inode);
  162. goto out;
  163. }
  164. d_tmpfile(file, inode);
  165. out:
  166. return finish_open_simple(file, error);
  167. }
  168. static const struct inode_operations ramfs_dir_inode_operations = {
  169. .create = ramfs_create,
  170. .lookup = simple_lookup,
  171. .link = simple_link,
  172. .unlink = simple_unlink,
  173. .symlink = ramfs_symlink,
  174. .mkdir = ramfs_mkdir,
  175. .rmdir = simple_rmdir,
  176. .mknod = ramfs_mknod,
  177. .rename = simple_rename,
  178. .tmpfile = ramfs_tmpfile,
  179. };
  180. /*
  181. * Display the mount options in /proc/mounts.
  182. */
  183. static int ramfs_show_options(struct seq_file *m, struct dentry *root)
  184. {
  185. struct ramfs_fs_info *fsi = root->d_sb->s_fs_info;
  186. if (fsi->mount_opts.mode != RAMFS_DEFAULT_MODE)
  187. seq_printf(m, ",mode=%o", fsi->mount_opts.mode);
  188. return 0;
  189. }
  190. static const struct super_operations ramfs_ops = {
  191. .statfs = simple_statfs,
  192. .drop_inode = generic_delete_inode,
  193. .show_options = ramfs_show_options,
  194. };
  195. enum ramfs_param {
  196. Opt_mode,
  197. };
  198. const struct fs_parameter_spec ramfs_fs_parameters[] = {
  199. fsparam_u32oct("mode", Opt_mode),
  200. {}
  201. };
  202. static int ramfs_parse_param(struct fs_context *fc, struct fs_parameter *param)
  203. {
  204. struct fs_parse_result result;
  205. struct ramfs_fs_info *fsi = fc->s_fs_info;
  206. int opt;
  207. opt = fs_parse(fc, ramfs_fs_parameters, param, &result);
  208. if (opt == -ENOPARAM) {
  209. opt = vfs_parse_fs_param_source(fc, param);
  210. if (opt != -ENOPARAM)
  211. return opt;
  212. /*
  213. * We might like to report bad mount options here;
  214. * but traditionally ramfs has ignored all mount options,
  215. * and as it is used as a !CONFIG_SHMEM simple substitute
  216. * for tmpfs, better continue to ignore other mount options.
  217. */
  218. return 0;
  219. }
  220. if (opt < 0)
  221. return opt;
  222. switch (opt) {
  223. case Opt_mode:
  224. fsi->mount_opts.mode = result.uint_32 & S_IALLUGO;
  225. break;
  226. }
  227. return 0;
  228. }
  229. static int ramfs_fill_super(struct super_block *sb, struct fs_context *fc)
  230. {
  231. struct ramfs_fs_info *fsi = sb->s_fs_info;
  232. struct inode *inode;
  233. sb->s_maxbytes = MAX_LFS_FILESIZE;
  234. sb->s_blocksize = PAGE_SIZE;
  235. sb->s_blocksize_bits = PAGE_SHIFT;
  236. sb->s_magic = RAMFS_MAGIC;
  237. sb->s_op = &ramfs_ops;
  238. sb->s_time_gran = 1;
  239. inode = ramfs_get_inode(sb, NULL, S_IFDIR | fsi->mount_opts.mode, 0);
  240. sb->s_root = d_make_root(inode);
  241. if (!sb->s_root)
  242. return -ENOMEM;
  243. return 0;
  244. }
  245. static int ramfs_get_tree(struct fs_context *fc)
  246. {
  247. return get_tree_nodev(fc, ramfs_fill_super);
  248. }
  249. static void ramfs_free_fc(struct fs_context *fc)
  250. {
  251. kfree(fc->s_fs_info);
  252. }
  253. static const struct fs_context_operations ramfs_context_ops = {
  254. .free = ramfs_free_fc,
  255. .parse_param = ramfs_parse_param,
  256. .get_tree = ramfs_get_tree,
  257. };
  258. int ramfs_init_fs_context(struct fs_context *fc)
  259. {
  260. struct ramfs_fs_info *fsi;
  261. fsi = kzalloc(sizeof(*fsi), GFP_KERNEL);
  262. if (!fsi)
  263. return -ENOMEM;
  264. fsi->mount_opts.mode = RAMFS_DEFAULT_MODE;
  265. fc->s_fs_info = fsi;
  266. fc->ops = &ramfs_context_ops;
  267. return 0;
  268. }
  269. void ramfs_kill_sb(struct super_block *sb)
  270. {
  271. kfree(sb->s_fs_info);
  272. kill_litter_super(sb);
  273. }
  274. static struct file_system_type ramfs_fs_type = {
  275. .name = "ramfs",
  276. .init_fs_context = ramfs_init_fs_context,
  277. .parameters = ramfs_fs_parameters,
  278. .kill_sb = ramfs_kill_sb,
  279. .fs_flags = FS_USERNS_MOUNT,
  280. };
  281. static int __init init_ramfs_fs(void)
  282. {
  283. return register_filesystem(&ramfs_fs_type);
  284. }
  285. fs_initcall(init_ramfs_fs);