anon_inodes.c 6.4 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. 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 we can avoid doing an igrab() and we can use an open-coded
  102. * atomic_inc().
  103. */
  104. atomic_inc(&anon_inode_inode->i_count);
  105. path.dentry->d_op = &anon_inodefs_dentry_operations;
  106. d_instantiate(path.dentry, anon_inode_inode);
  107. error = -ENFILE;
  108. file = alloc_file(&path, OPEN_FMODE(flags), fops);
  109. if (!file)
  110. goto err_dput;
  111. file->f_mapping = anon_inode_inode->i_mapping;
  112. file->f_pos = 0;
  113. file->f_flags = flags & (O_ACCMODE | O_NONBLOCK);
  114. file->f_version = 0;
  115. file->private_data = priv;
  116. return file;
  117. err_dput:
  118. path_put(&path);
  119. err_module:
  120. module_put(fops->owner);
  121. return ERR_PTR(error);
  122. }
  123. EXPORT_SYMBOL_GPL(anon_inode_getfile);
  124. /**
  125. * anon_inode_getfd - creates a new file instance by hooking it up to an
  126. * anonymous inode, and a dentry that describe the "class"
  127. * of the file
  128. *
  129. * @name: [in] name of the "class" of the new file
  130. * @fops: [in] file operations for the new file
  131. * @priv: [in] private data for the new file (will be file's private_data)
  132. * @flags: [in] flags
  133. *
  134. * Creates a new file by hooking it on a single inode. This is useful for files
  135. * that do not need to have a full-fledged inode in order to operate correctly.
  136. * All the files created with anon_inode_getfd() will share a single inode,
  137. * hence saving memory and avoiding code duplication for the file/inode/dentry
  138. * setup. Returns new descriptor or an error code.
  139. */
  140. int anon_inode_getfd(const char *name, const struct file_operations *fops,
  141. void *priv, int flags)
  142. {
  143. int error, fd;
  144. struct file *file;
  145. error = get_unused_fd_flags(flags);
  146. if (error < 0)
  147. return error;
  148. fd = error;
  149. file = anon_inode_getfile(name, fops, priv, flags);
  150. if (IS_ERR(file)) {
  151. error = PTR_ERR(file);
  152. goto err_put_unused_fd;
  153. }
  154. fd_install(fd, file);
  155. return fd;
  156. err_put_unused_fd:
  157. put_unused_fd(fd);
  158. return error;
  159. }
  160. EXPORT_SYMBOL_GPL(anon_inode_getfd);
  161. /*
  162. * A single inode exists for all anon_inode files. Contrary to pipes,
  163. * anon_inode inodes have no associated per-instance data, so we need
  164. * only allocate one of them.
  165. */
  166. static struct inode *anon_inode_mkinode(void)
  167. {
  168. struct inode *inode = new_inode(anon_inode_mnt->mnt_sb);
  169. if (!inode)
  170. return ERR_PTR(-ENOMEM);
  171. inode->i_fop = &anon_inode_fops;
  172. inode->i_mapping->a_ops = &anon_aops;
  173. /*
  174. * Mark the inode dirty from the very beginning,
  175. * that way it will never be moved to the dirty
  176. * list because mark_inode_dirty() will think
  177. * that it already _is_ on the dirty list.
  178. */
  179. inode->i_state = I_DIRTY;
  180. inode->i_mode = S_IRUSR | S_IWUSR;
  181. inode->i_uid = current_fsuid();
  182. inode->i_gid = current_fsgid();
  183. inode->i_flags |= S_PRIVATE;
  184. inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
  185. return inode;
  186. }
  187. static int __init anon_inode_init(void)
  188. {
  189. int error;
  190. error = register_filesystem(&anon_inode_fs_type);
  191. if (error)
  192. goto err_exit;
  193. anon_inode_mnt = kern_mount(&anon_inode_fs_type);
  194. if (IS_ERR(anon_inode_mnt)) {
  195. error = PTR_ERR(anon_inode_mnt);
  196. goto err_unregister_filesystem;
  197. }
  198. anon_inode_inode = anon_inode_mkinode();
  199. if (IS_ERR(anon_inode_inode)) {
  200. error = PTR_ERR(anon_inode_inode);
  201. goto err_mntput;
  202. }
  203. return 0;
  204. err_mntput:
  205. mntput(anon_inode_mnt);
  206. err_unregister_filesystem:
  207. unregister_filesystem(&anon_inode_fs_type);
  208. err_exit:
  209. panic(KERN_ERR "anon_inode_init() failed (%d)\n", error);
  210. }
  211. fs_initcall(anon_inode_init);