anon_inodes.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. /*
  2. * fs/anon_inodes.c
  3. *
  4. * Copyright (C) 2007 Davide Libenzi <davidel@xmailserver.org>
  5. *
  6. * Thanks to Arnd Bergmann for code review and suggestions.
  7. * More changes for Thomas Gleixner suggestions.
  8. *
  9. */
  10. #include <linux/cred.h>
  11. #include <linux/file.h>
  12. #include <linux/poll.h>
  13. #include <linux/sched.h>
  14. #include <linux/slab.h>
  15. #include <linux/init.h>
  16. #include <linux/fs.h>
  17. #include <linux/mount.h>
  18. #include <linux/module.h>
  19. #include <linux/kernel.h>
  20. #include <linux/magic.h>
  21. #include <linux/anon_inodes.h>
  22. #include <asm/uaccess.h>
  23. static struct vfsmount *anon_inode_mnt __read_mostly;
  24. static struct inode *anon_inode_inode;
  25. static const struct file_operations anon_inode_fops;
  26. static int anon_inodefs_get_sb(struct file_system_type *fs_type, int flags,
  27. const char *dev_name, void *data,
  28. struct vfsmount *mnt)
  29. {
  30. return get_sb_pseudo(fs_type, "anon_inode:", NULL, ANON_INODE_FS_MAGIC,
  31. mnt);
  32. }
  33. static int anon_inodefs_delete_dentry(struct dentry *dentry)
  34. {
  35. /*
  36. * We faked vfs to believe the dentry was hashed when we created it.
  37. * Now we restore the flag so that dput() will work correctly.
  38. */
  39. dentry->d_flags |= DCACHE_UNHASHED;
  40. return 1;
  41. }
  42. static struct file_system_type anon_inode_fs_type = {
  43. .name = "anon_inodefs",
  44. .get_sb = anon_inodefs_get_sb,
  45. .kill_sb = kill_anon_super,
  46. };
  47. static const struct dentry_operations anon_inodefs_dentry_operations = {
  48. .d_delete = anon_inodefs_delete_dentry,
  49. };
  50. /*
  51. * nop .set_page_dirty method so that people can use .page_mkwrite on
  52. * anon inodes.
  53. */
  54. static int anon_set_page_dirty(struct page *page)
  55. {
  56. return 0;
  57. };
  58. static const struct address_space_operations anon_aops = {
  59. .set_page_dirty = anon_set_page_dirty,
  60. };
  61. /**
  62. * anon_inode_getfd - creates a new file instance by hooking it up to an
  63. * anonymous inode, and a dentry that describe the "class"
  64. * of the file
  65. *
  66. * @name: [in] name of the "class" of the new file
  67. * @fops: [in] file operations for the new file
  68. * @priv: [in] private data for the new file (will be file's private_data)
  69. * @flags: [in] flags
  70. *
  71. * Creates a new file by hooking it on a single inode. This is useful for files
  72. * that do not need to have a full-fledged inode in order to operate correctly.
  73. * All the files created with anon_inode_getfile() will share a single inode,
  74. * hence saving memory and avoiding code duplication for the file/inode/dentry
  75. * setup. Returns the newly created file* or an error pointer.
  76. */
  77. struct file *anon_inode_getfile(const char *name,
  78. const struct file_operations *fops,
  79. void *priv, int flags)
  80. {
  81. struct qstr this;
  82. struct dentry *dentry;
  83. struct file *file;
  84. int error;
  85. if (IS_ERR(anon_inode_inode))
  86. return ERR_PTR(-ENODEV);
  87. if (fops->owner && !try_module_get(fops->owner))
  88. return ERR_PTR(-ENOENT);
  89. /*
  90. * Link the inode to a directory entry by creating a unique name
  91. * using the inode sequence number.
  92. */
  93. error = -ENOMEM;
  94. this.name = name;
  95. this.len = strlen(name);
  96. this.hash = 0;
  97. dentry = d_alloc(anon_inode_mnt->mnt_sb->s_root, &this);
  98. if (!dentry)
  99. goto err_module;
  100. /*
  101. * We know the anon_inode inode count is always greater than zero,
  102. * so we can avoid doing an igrab() and we can use an open-coded
  103. * atomic_inc().
  104. */
  105. atomic_inc(&anon_inode_inode->i_count);
  106. dentry->d_op = &anon_inodefs_dentry_operations;
  107. /* Do not publish this dentry inside the global dentry hash table */
  108. dentry->d_flags &= ~DCACHE_UNHASHED;
  109. d_instantiate(dentry, anon_inode_inode);
  110. error = -ENFILE;
  111. file = alloc_file(anon_inode_mnt, dentry,
  112. FMODE_READ | FMODE_WRITE, fops);
  113. if (!file)
  114. goto err_dput;
  115. file->f_mapping = anon_inode_inode->i_mapping;
  116. file->f_pos = 0;
  117. file->f_flags = O_RDWR | (flags & O_NONBLOCK);
  118. file->f_version = 0;
  119. file->private_data = priv;
  120. return file;
  121. err_dput:
  122. dput(dentry);
  123. err_module:
  124. module_put(fops->owner);
  125. return ERR_PTR(error);
  126. }
  127. EXPORT_SYMBOL_GPL(anon_inode_getfile);
  128. /**
  129. * anon_inode_getfd - creates a new file instance by hooking it up to an
  130. * anonymous inode, and a dentry that describe the "class"
  131. * of the file
  132. *
  133. * @name: [in] name of the "class" of the new file
  134. * @fops: [in] file operations for the new file
  135. * @priv: [in] private data for the new file (will be file's private_data)
  136. * @flags: [in] flags
  137. *
  138. * Creates a new file by hooking it on a single inode. This is useful for files
  139. * that do not need to have a full-fledged inode in order to operate correctly.
  140. * All the files created with anon_inode_getfd() will share a single inode,
  141. * hence saving memory and avoiding code duplication for the file/inode/dentry
  142. * setup. Returns new descriptor or an error code.
  143. */
  144. int anon_inode_getfd(const char *name, const struct file_operations *fops,
  145. void *priv, int flags)
  146. {
  147. int error, fd;
  148. struct file *file;
  149. error = get_unused_fd_flags(flags);
  150. if (error < 0)
  151. return error;
  152. fd = error;
  153. file = anon_inode_getfile(name, fops, priv, flags);
  154. if (IS_ERR(file)) {
  155. error = PTR_ERR(file);
  156. goto err_put_unused_fd;
  157. }
  158. fd_install(fd, file);
  159. return fd;
  160. err_put_unused_fd:
  161. put_unused_fd(fd);
  162. return error;
  163. }
  164. EXPORT_SYMBOL_GPL(anon_inode_getfd);
  165. /*
  166. * A single inode exists for all anon_inode files. Contrary to pipes,
  167. * anon_inode inodes have no associated per-instance data, so we need
  168. * only allocate one of them.
  169. */
  170. static struct inode *anon_inode_mkinode(void)
  171. {
  172. struct inode *inode = new_inode(anon_inode_mnt->mnt_sb);
  173. if (!inode)
  174. return ERR_PTR(-ENOMEM);
  175. inode->i_fop = &anon_inode_fops;
  176. inode->i_mapping->a_ops = &anon_aops;
  177. /*
  178. * Mark the inode dirty from the very beginning,
  179. * that way it will never be moved to the dirty
  180. * list because mark_inode_dirty() will think
  181. * that it already _is_ on the dirty list.
  182. */
  183. inode->i_state = I_DIRTY;
  184. inode->i_mode = S_IRUSR | S_IWUSR;
  185. inode->i_uid = current_fsuid();
  186. inode->i_gid = current_fsgid();
  187. inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
  188. return inode;
  189. }
  190. static int __init anon_inode_init(void)
  191. {
  192. int error;
  193. error = register_filesystem(&anon_inode_fs_type);
  194. if (error)
  195. goto err_exit;
  196. anon_inode_mnt = kern_mount(&anon_inode_fs_type);
  197. if (IS_ERR(anon_inode_mnt)) {
  198. error = PTR_ERR(anon_inode_mnt);
  199. goto err_unregister_filesystem;
  200. }
  201. anon_inode_inode = anon_inode_mkinode();
  202. if (IS_ERR(anon_inode_inode)) {
  203. error = PTR_ERR(anon_inode_inode);
  204. goto err_mntput;
  205. }
  206. return 0;
  207. err_mntput:
  208. mntput(anon_inode_mnt);
  209. err_unregister_filesystem:
  210. unregister_filesystem(&anon_inode_fs_type);
  211. err_exit:
  212. panic(KERN_ERR "anon_inode_init() failed (%d)\n", error);
  213. }
  214. fs_initcall(anon_inode_init);