anon_inodes.c 6.4 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/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. int error;
  120. if (IS_ERR(anon_inode_inode))
  121. return ERR_PTR(-ENODEV);
  122. if (fops->owner && !try_module_get(fops->owner))
  123. return ERR_PTR(-ENOENT);
  124. /*
  125. * Link the inode to a directory entry by creating a unique name
  126. * using the inode sequence number.
  127. */
  128. error = -ENOMEM;
  129. this.name = name;
  130. this.len = strlen(name);
  131. this.hash = 0;
  132. path.dentry = d_alloc_pseudo(anon_inode_mnt->mnt_sb, &this);
  133. if (!path.dentry)
  134. goto err_module;
  135. path.mnt = mntget(anon_inode_mnt);
  136. /*
  137. * We know the anon_inode inode count is always greater than zero,
  138. * so ihold() is safe.
  139. */
  140. ihold(anon_inode_inode);
  141. d_instantiate(path.dentry, anon_inode_inode);
  142. error = -ENFILE;
  143. file = alloc_file(&path, OPEN_FMODE(flags), fops);
  144. if (!file)
  145. goto err_dput;
  146. file->f_mapping = anon_inode_inode->i_mapping;
  147. file->f_pos = 0;
  148. file->f_flags = flags & (O_ACCMODE | O_NONBLOCK);
  149. file->f_version = 0;
  150. file->private_data = priv;
  151. return file;
  152. err_dput:
  153. path_put(&path);
  154. err_module:
  155. module_put(fops->owner);
  156. return ERR_PTR(error);
  157. }
  158. EXPORT_SYMBOL_GPL(anon_inode_getfile);
  159. /**
  160. * anon_inode_getfd - creates a new file instance by hooking it up to an
  161. * anonymous inode, and a dentry that describe the "class"
  162. * of the file
  163. *
  164. * @name: [in] name of the "class" of the new file
  165. * @fops: [in] file operations for the new file
  166. * @priv: [in] private data for the new file (will be file's private_data)
  167. * @flags: [in] flags
  168. *
  169. * Creates a new file by hooking it on a single inode. This is useful for files
  170. * that do not need to have a full-fledged inode in order to operate correctly.
  171. * All the files created with anon_inode_getfd() will share a single inode,
  172. * hence saving memory and avoiding code duplication for the file/inode/dentry
  173. * setup. Returns new descriptor or an error code.
  174. */
  175. int anon_inode_getfd(const char *name, const struct file_operations *fops,
  176. void *priv, int flags)
  177. {
  178. int error, fd;
  179. struct file *file;
  180. error = get_unused_fd_flags(flags);
  181. if (error < 0)
  182. return error;
  183. fd = error;
  184. file = anon_inode_getfile(name, fops, priv, flags);
  185. if (IS_ERR(file)) {
  186. error = PTR_ERR(file);
  187. goto err_put_unused_fd;
  188. }
  189. fd_install(fd, file);
  190. return fd;
  191. err_put_unused_fd:
  192. put_unused_fd(fd);
  193. return error;
  194. }
  195. EXPORT_SYMBOL_GPL(anon_inode_getfd);
  196. static int __init anon_inode_init(void)
  197. {
  198. int error;
  199. error = register_filesystem(&anon_inode_fs_type);
  200. if (error)
  201. goto err_exit;
  202. anon_inode_mnt = kern_mount(&anon_inode_fs_type);
  203. if (IS_ERR(anon_inode_mnt)) {
  204. error = PTR_ERR(anon_inode_mnt);
  205. goto err_unregister_filesystem;
  206. }
  207. return 0;
  208. err_unregister_filesystem:
  209. unregister_filesystem(&anon_inode_fs_type);
  210. err_exit:
  211. panic(KERN_ERR "anon_inode_init() failed (%d)\n", error);
  212. }
  213. fs_initcall(anon_inode_init);