anon_inodes.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  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_private - 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. *
  107. * Similar to anon_inode_getfile, but each file holds a single inode.
  108. *
  109. */
  110. struct file *anon_inode_getfile_private(const char *name,
  111. const struct file_operations *fops,
  112. void *priv, int flags)
  113. {
  114. struct qstr this;
  115. struct path path;
  116. struct file *file;
  117. struct inode *inode;
  118. if (fops->owner && !try_module_get(fops->owner))
  119. return ERR_PTR(-ENOENT);
  120. inode = anon_inode_mkinode(anon_inode_mnt->mnt_sb);
  121. if (IS_ERR(inode)) {
  122. file = ERR_PTR(-ENOMEM);
  123. goto err_module;
  124. }
  125. /*
  126. * Link the inode to a directory entry by creating a unique name
  127. * using the inode sequence number.
  128. */
  129. file = ERR_PTR(-ENOMEM);
  130. this.name = name;
  131. this.len = strlen(name);
  132. this.hash = 0;
  133. path.dentry = d_alloc_pseudo(anon_inode_mnt->mnt_sb, &this);
  134. if (!path.dentry)
  135. goto err_module;
  136. path.mnt = mntget(anon_inode_mnt);
  137. d_instantiate(path.dentry, inode);
  138. file = alloc_file(&path, OPEN_FMODE(flags), fops);
  139. if (IS_ERR(file))
  140. goto err_dput;
  141. file->f_mapping = inode->i_mapping;
  142. file->f_flags = flags & (O_ACCMODE | O_NONBLOCK);
  143. file->private_data = priv;
  144. return file;
  145. err_dput:
  146. path_put(&path);
  147. err_module:
  148. module_put(fops->owner);
  149. return file;
  150. }
  151. EXPORT_SYMBOL_GPL(anon_inode_getfile_private);
  152. /**
  153. * anon_inode_getfile - creates a new file instance by hooking it up to an
  154. * anonymous inode, and a dentry that describe the "class"
  155. * of the file
  156. *
  157. * @name: [in] name of the "class" of the new file
  158. * @fops: [in] file operations for the new file
  159. * @priv: [in] private data for the new file (will be file's private_data)
  160. * @flags: [in] flags
  161. *
  162. * Creates a new file by hooking it on a single inode. This is useful for files
  163. * that do not need to have a full-fledged inode in order to operate correctly.
  164. * All the files created with anon_inode_getfile() will share a single inode,
  165. * hence saving memory and avoiding code duplication for the file/inode/dentry
  166. * setup. Returns the newly created file* or an error pointer.
  167. */
  168. struct file *anon_inode_getfile(const char *name,
  169. const struct file_operations *fops,
  170. void *priv, int flags)
  171. {
  172. struct qstr this;
  173. struct path path;
  174. struct file *file;
  175. if (IS_ERR(anon_inode_inode))
  176. return ERR_PTR(-ENODEV);
  177. if (fops->owner && !try_module_get(fops->owner))
  178. return ERR_PTR(-ENOENT);
  179. /*
  180. * Link the inode to a directory entry by creating a unique name
  181. * using the inode sequence number.
  182. */
  183. file = ERR_PTR(-ENOMEM);
  184. this.name = name;
  185. this.len = strlen(name);
  186. this.hash = 0;
  187. path.dentry = d_alloc_pseudo(anon_inode_mnt->mnt_sb, &this);
  188. if (!path.dentry)
  189. goto err_module;
  190. path.mnt = mntget(anon_inode_mnt);
  191. /*
  192. * We know the anon_inode inode count is always greater than zero,
  193. * so ihold() is safe.
  194. */
  195. ihold(anon_inode_inode);
  196. d_instantiate(path.dentry, anon_inode_inode);
  197. file = alloc_file(&path, OPEN_FMODE(flags), fops);
  198. if (IS_ERR(file))
  199. goto err_dput;
  200. file->f_mapping = anon_inode_inode->i_mapping;
  201. file->f_flags = flags & (O_ACCMODE | O_NONBLOCK);
  202. file->private_data = priv;
  203. return file;
  204. err_dput:
  205. path_put(&path);
  206. err_module:
  207. module_put(fops->owner);
  208. return file;
  209. }
  210. EXPORT_SYMBOL_GPL(anon_inode_getfile);
  211. /**
  212. * anon_inode_getfd - creates a new file instance by hooking it up to an
  213. * anonymous inode, and a dentry that describe the "class"
  214. * of the file
  215. *
  216. * @name: [in] name of the "class" of the new file
  217. * @fops: [in] file operations for the new file
  218. * @priv: [in] private data for the new file (will be file's private_data)
  219. * @flags: [in] flags
  220. *
  221. * Creates a new file by hooking it on a single inode. This is useful for files
  222. * that do not need to have a full-fledged inode in order to operate correctly.
  223. * All the files created with anon_inode_getfd() will share a single inode,
  224. * hence saving memory and avoiding code duplication for the file/inode/dentry
  225. * setup. Returns new descriptor or an error code.
  226. */
  227. int anon_inode_getfd(const char *name, const struct file_operations *fops,
  228. void *priv, int flags)
  229. {
  230. int error, fd;
  231. struct file *file;
  232. error = get_unused_fd_flags(flags);
  233. if (error < 0)
  234. return error;
  235. fd = error;
  236. file = anon_inode_getfile(name, fops, priv, flags);
  237. if (IS_ERR(file)) {
  238. error = PTR_ERR(file);
  239. goto err_put_unused_fd;
  240. }
  241. fd_install(fd, file);
  242. return fd;
  243. err_put_unused_fd:
  244. put_unused_fd(fd);
  245. return error;
  246. }
  247. EXPORT_SYMBOL_GPL(anon_inode_getfd);
  248. static int __init anon_inode_init(void)
  249. {
  250. int error;
  251. error = register_filesystem(&anon_inode_fs_type);
  252. if (error)
  253. goto err_exit;
  254. anon_inode_mnt = kern_mount(&anon_inode_fs_type);
  255. if (IS_ERR(anon_inode_mnt)) {
  256. error = PTR_ERR(anon_inode_mnt);
  257. goto err_unregister_filesystem;
  258. }
  259. return 0;
  260. err_unregister_filesystem:
  261. unregister_filesystem(&anon_inode_fs_type);
  262. err_exit:
  263. panic(KERN_ERR "anon_inode_init() failed (%d)\n", error);
  264. }
  265. fs_initcall(anon_inode_init);