anon_inodes.c 6.4 KB

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