inode.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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/init.h>
  29. #include <linux/string.h>
  30. #include <linux/smp_lock.h>
  31. #include <linux/backing-dev.h>
  32. #include <linux/ramfs.h>
  33. #include <asm/uaccess.h>
  34. /* some random number */
  35. #define RAMFS_MAGIC 0x858458f6
  36. static struct super_operations ramfs_ops;
  37. static struct address_space_operations ramfs_aops;
  38. static struct inode_operations ramfs_file_inode_operations;
  39. static struct inode_operations ramfs_dir_inode_operations;
  40. static struct backing_dev_info ramfs_backing_dev_info = {
  41. .ra_pages = 0, /* No readahead */
  42. .capabilities = BDI_CAP_NO_ACCT_DIRTY | BDI_CAP_NO_WRITEBACK |
  43. BDI_CAP_MAP_DIRECT | BDI_CAP_MAP_COPY |
  44. BDI_CAP_READ_MAP | BDI_CAP_WRITE_MAP | BDI_CAP_EXEC_MAP,
  45. };
  46. struct inode *ramfs_get_inode(struct super_block *sb, int mode, dev_t dev)
  47. {
  48. struct inode * inode = new_inode(sb);
  49. if (inode) {
  50. inode->i_mode = mode;
  51. inode->i_uid = current->fsuid;
  52. inode->i_gid = current->fsgid;
  53. inode->i_blksize = PAGE_CACHE_SIZE;
  54. inode->i_blocks = 0;
  55. inode->i_mapping->a_ops = &ramfs_aops;
  56. inode->i_mapping->backing_dev_info = &ramfs_backing_dev_info;
  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. inode->i_nlink++;
  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, int mode, dev_t dev)
  85. {
  86. struct inode * inode = ramfs_get_inode(dir->i_sb, mode, dev);
  87. int error = -ENOSPC;
  88. if (inode) {
  89. if (dir->i_mode & S_ISGID) {
  90. inode->i_gid = dir->i_gid;
  91. if (S_ISDIR(mode))
  92. inode->i_mode |= S_ISGID;
  93. }
  94. d_instantiate(dentry, inode);
  95. dget(dentry); /* Extra count - pin the dentry in core */
  96. error = 0;
  97. }
  98. return error;
  99. }
  100. static int ramfs_mkdir(struct inode * dir, struct dentry * dentry, int mode)
  101. {
  102. int retval = ramfs_mknod(dir, dentry, mode | S_IFDIR, 0);
  103. if (!retval)
  104. dir->i_nlink++;
  105. return retval;
  106. }
  107. static int ramfs_create(struct inode *dir, struct dentry *dentry, int mode, struct nameidata *nd)
  108. {
  109. return ramfs_mknod(dir, dentry, mode | S_IFREG, 0);
  110. }
  111. static int ramfs_symlink(struct inode * dir, struct dentry *dentry, const char * symname)
  112. {
  113. struct inode *inode;
  114. int error = -ENOSPC;
  115. inode = ramfs_get_inode(dir->i_sb, S_IFLNK|S_IRWXUGO, 0);
  116. if (inode) {
  117. int l = strlen(symname)+1;
  118. error = page_symlink(inode, symname, l);
  119. if (!error) {
  120. if (dir->i_mode & S_ISGID)
  121. inode->i_gid = dir->i_gid;
  122. d_instantiate(dentry, inode);
  123. dget(dentry);
  124. } else
  125. iput(inode);
  126. }
  127. return error;
  128. }
  129. static struct address_space_operations ramfs_aops = {
  130. .readpage = simple_readpage,
  131. .prepare_write = simple_prepare_write,
  132. .commit_write = simple_commit_write
  133. };
  134. struct file_operations ramfs_file_operations = {
  135. .read = generic_file_read,
  136. .write = generic_file_write,
  137. .mmap = generic_file_mmap,
  138. .fsync = simple_sync_file,
  139. .sendfile = generic_file_sendfile,
  140. .llseek = generic_file_llseek,
  141. };
  142. static struct inode_operations ramfs_file_inode_operations = {
  143. .getattr = simple_getattr,
  144. };
  145. static struct inode_operations ramfs_dir_inode_operations = {
  146. .create = ramfs_create,
  147. .lookup = simple_lookup,
  148. .link = simple_link,
  149. .unlink = simple_unlink,
  150. .symlink = ramfs_symlink,
  151. .mkdir = ramfs_mkdir,
  152. .rmdir = simple_rmdir,
  153. .mknod = ramfs_mknod,
  154. .rename = simple_rename,
  155. };
  156. static struct super_operations ramfs_ops = {
  157. .statfs = simple_statfs,
  158. .drop_inode = generic_delete_inode,
  159. };
  160. static int ramfs_fill_super(struct super_block * sb, void * data, int silent)
  161. {
  162. struct inode * inode;
  163. struct dentry * root;
  164. sb->s_maxbytes = MAX_LFS_FILESIZE;
  165. sb->s_blocksize = PAGE_CACHE_SIZE;
  166. sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
  167. sb->s_magic = RAMFS_MAGIC;
  168. sb->s_op = &ramfs_ops;
  169. sb->s_time_gran = 1;
  170. inode = ramfs_get_inode(sb, S_IFDIR | 0755, 0);
  171. if (!inode)
  172. return -ENOMEM;
  173. root = d_alloc_root(inode);
  174. if (!root) {
  175. iput(inode);
  176. return -ENOMEM;
  177. }
  178. sb->s_root = root;
  179. return 0;
  180. }
  181. struct super_block *ramfs_get_sb(struct file_system_type *fs_type,
  182. int flags, const char *dev_name, void *data)
  183. {
  184. return get_sb_nodev(fs_type, flags, data, ramfs_fill_super);
  185. }
  186. static struct super_block *rootfs_get_sb(struct file_system_type *fs_type,
  187. int flags, const char *dev_name, void *data)
  188. {
  189. return get_sb_nodev(fs_type, flags|MS_NOUSER, data, ramfs_fill_super);
  190. }
  191. static struct file_system_type ramfs_fs_type = {
  192. .name = "ramfs",
  193. .get_sb = ramfs_get_sb,
  194. .kill_sb = kill_litter_super,
  195. };
  196. static struct file_system_type rootfs_fs_type = {
  197. .name = "rootfs",
  198. .get_sb = rootfs_get_sb,
  199. .kill_sb = kill_litter_super,
  200. };
  201. static int __init init_ramfs_fs(void)
  202. {
  203. return register_filesystem(&ramfs_fs_type);
  204. }
  205. static void __exit exit_ramfs_fs(void)
  206. {
  207. unregister_filesystem(&ramfs_fs_type);
  208. }
  209. module_init(init_ramfs_fs)
  210. module_exit(exit_ramfs_fs)
  211. int __init init_rootfs(void)
  212. {
  213. return register_filesystem(&rootfs_fs_type);
  214. }
  215. MODULE_LICENSE("GPL");