anon_inodes.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  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/init.h>
  15. #include <linux/fs.h>
  16. #include <linux/mount.h>
  17. #include <linux/module.h>
  18. #include <linux/kernel.h>
  19. #include <linux/magic.h>
  20. #include <linux/anon_inodes.h>
  21. #include <asm/uaccess.h>
  22. static struct vfsmount *anon_inode_mnt __read_mostly;
  23. static struct inode *anon_inode_inode;
  24. static const struct file_operations anon_inode_fops;
  25. /*
  26. * anon_inodefs_dname() is called from d_path().
  27. */
  28. static char *anon_inodefs_dname(struct dentry *dentry, char *buffer, int buflen)
  29. {
  30. return dynamic_dname(dentry, buffer, buflen, "anon_inode:%s",
  31. dentry->d_name.name);
  32. }
  33. static const struct dentry_operations anon_inodefs_dentry_operations = {
  34. .d_dname = anon_inodefs_dname,
  35. };
  36. /*
  37. * nop .set_page_dirty method so that people can use .page_mkwrite on
  38. * anon inodes.
  39. */
  40. static int anon_set_page_dirty(struct page *page)
  41. {
  42. return 0;
  43. };
  44. static const struct address_space_operations anon_aops = {
  45. .set_page_dirty = anon_set_page_dirty,
  46. };
  47. /*
  48. * A single inode exists for all anon_inode files. Contrary to pipes,
  49. * anon_inode inodes have no associated per-instance data, so we need
  50. * only allocate one of them.
  51. */
  52. static struct inode *anon_inode_mkinode(struct super_block *s)
  53. {
  54. struct inode *inode = new_inode_pseudo(s);
  55. if (!inode)
  56. return ERR_PTR(-ENOMEM);
  57. inode->i_ino = get_next_ino();
  58. inode->i_fop = &anon_inode_fops;
  59. inode->i_mapping->a_ops = &anon_aops;
  60. /*
  61. * Mark the inode dirty from the very beginning,
  62. * that way it will never be moved to the dirty
  63. * list because mark_inode_dirty() will think
  64. * that it already _is_ on the dirty list.
  65. */
  66. inode->i_state = I_DIRTY;
  67. inode->i_mode = S_IRUSR | S_IWUSR;
  68. inode->i_uid = current_fsuid();
  69. inode->i_gid = current_fsgid();
  70. inode->i_flags |= S_PRIVATE;
  71. inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
  72. return inode;
  73. }
  74. static struct dentry *anon_inodefs_mount(struct file_system_type *fs_type,
  75. int flags, const char *dev_name, void *data)
  76. {
  77. struct dentry *root;
  78. root = mount_pseudo(fs_type, "anon_inode:", NULL,
  79. &anon_inodefs_dentry_operations, ANON_INODE_FS_MAGIC);
  80. if (!IS_ERR(root)) {
  81. struct super_block *s = root->d_sb;
  82. anon_inode_inode = anon_inode_mkinode(s);
  83. if (IS_ERR(anon_inode_inode)) {
  84. dput(root);
  85. deactivate_locked_super(s);
  86. root = ERR_CAST(anon_inode_inode);
  87. }
  88. }
  89. return root;
  90. }
  91. static struct file_system_type anon_inode_fs_type = {
  92. .name = "anon_inodefs",
  93. .mount = anon_inodefs_mount,
  94. .kill_sb = kill_anon_super,
  95. };
  96. /**
  97. * anon_inode_getfile - creates a new file instance by hooking it up to an
  98. * anonymous inode, and a dentry that describe the "class"
  99. * of the file
  100. *
  101. * @name: [in] name of the "class" of the new file
  102. * @fops: [in] file operations for the new file
  103. * @priv: [in] private data for the new file (will be file's private_data)
  104. * @flags: [in] flags
  105. *
  106. * Creates a new file by hooking it on a single inode. This is useful for files
  107. * that do not need to have a full-fledged inode in order to operate correctly.
  108. * All the files created with anon_inode_getfile() will share a single inode,
  109. * hence saving memory and avoiding code duplication for the file/inode/dentry
  110. * setup. Returns the newly created file* or an error pointer.
  111. */
  112. struct file *anon_inode_getfile(const char *name,
  113. const struct file_operations *fops,
  114. void *priv, int flags)
  115. {
  116. struct qstr this;
  117. struct path path;
  118. struct file *file;
  119. if (IS_ERR(anon_inode_inode))
  120. return ERR_PTR(-ENODEV);
  121. if (fops->owner && !try_module_get(fops->owner))
  122. return ERR_PTR(-ENOENT);
  123. /*
  124. * Link the inode to a directory entry by creating a unique name
  125. * using the inode sequence number.
  126. */
  127. file = ERR_PTR(-ENOMEM);
  128. this.name = name;
  129. this.len = strlen(name);
  130. this.hash = 0;
  131. path.dentry = d_alloc_pseudo(anon_inode_mnt->mnt_sb, &this);
  132. if (!path.dentry)
  133. goto err_module;
  134. path.mnt = mntget(anon_inode_mnt);
  135. /*
  136. * We know the anon_inode inode count is always greater than zero,
  137. * so ihold() is safe.
  138. */
  139. ihold(anon_inode_inode);
  140. d_instantiate(path.dentry, anon_inode_inode);
  141. file = alloc_file(&path, OPEN_FMODE(flags), fops);
  142. if (IS_ERR(file))
  143. goto err_dput;
  144. file->f_mapping = anon_inode_inode->i_mapping;
  145. file->f_flags = flags & (O_ACCMODE | O_NONBLOCK);
  146. file->private_data = priv;
  147. return file;
  148. err_dput:
  149. path_put(&path);
  150. err_module:
  151. module_put(fops->owner);
  152. return file;
  153. }
  154. EXPORT_SYMBOL_GPL(anon_inode_getfile);
  155. /**
  156. * anon_inode_getfd - creates a new file instance by hooking it up to an
  157. * anonymous inode, and a dentry that describe the "class"
  158. * of the file
  159. *
  160. * @name: [in] name of the "class" of the new file
  161. * @fops: [in] file operations for the new file
  162. * @priv: [in] private data for the new file (will be file's private_data)
  163. * @flags: [in] flags
  164. *
  165. * Creates a new file by hooking it on a single inode. This is useful for files
  166. * that do not need to have a full-fledged inode in order to operate correctly.
  167. * All the files created with anon_inode_getfd() will share a single inode,
  168. * hence saving memory and avoiding code duplication for the file/inode/dentry
  169. * setup. Returns new descriptor or an error code.
  170. */
  171. int anon_inode_getfd(const char *name, const struct file_operations *fops,
  172. void *priv, int flags)
  173. {
  174. int error, fd;
  175. struct file *file;
  176. error = get_unused_fd_flags(flags);
  177. if (error < 0)
  178. return error;
  179. fd = error;
  180. file = anon_inode_getfile(name, fops, priv, flags);
  181. if (IS_ERR(file)) {
  182. error = PTR_ERR(file);
  183. goto err_put_unused_fd;
  184. }
  185. fd_install(fd, file);
  186. return fd;
  187. err_put_unused_fd:
  188. put_unused_fd(fd);
  189. return error;
  190. }
  191. EXPORT_SYMBOL_GPL(anon_inode_getfd);
  192. static int __init anon_inode_init(void)
  193. {
  194. int error;
  195. error = register_filesystem(&anon_inode_fs_type);
  196. if (error)
  197. goto err_exit;
  198. anon_inode_mnt = kern_mount(&anon_inode_fs_type);
  199. if (IS_ERR(anon_inode_mnt)) {
  200. error = PTR_ERR(anon_inode_mnt);
  201. goto err_unregister_filesystem;
  202. }
  203. return 0;
  204. err_unregister_filesystem:
  205. unregister_filesystem(&anon_inode_fs_type);
  206. err_exit:
  207. panic(KERN_ERR "anon_inode_init() failed (%d)\n", error);
  208. }
  209. fs_initcall(anon_inode_init);