anon_inodes.c 6.3 KB

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