anon_inodes.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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/file.h>
  11. #include <linux/poll.h>
  12. #include <linux/slab.h>
  13. #include <linux/init.h>
  14. #include <linux/fs.h>
  15. #include <linux/mount.h>
  16. #include <linux/module.h>
  17. #include <linux/kernel.h>
  18. #include <linux/magic.h>
  19. #include <linux/anon_inodes.h>
  20. #include <asm/uaccess.h>
  21. static struct vfsmount *anon_inode_mnt __read_mostly;
  22. static struct inode *anon_inode_inode;
  23. static const struct file_operations anon_inode_fops;
  24. static int anon_inodefs_get_sb(struct file_system_type *fs_type, int flags,
  25. const char *dev_name, void *data,
  26. struct vfsmount *mnt)
  27. {
  28. return get_sb_pseudo(fs_type, "anon_inode:", NULL, ANON_INODE_FS_MAGIC,
  29. mnt);
  30. }
  31. static int anon_inodefs_delete_dentry(struct dentry *dentry)
  32. {
  33. /*
  34. * We faked vfs to believe the dentry was hashed when we created it.
  35. * Now we restore the flag so that dput() will work correctly.
  36. */
  37. dentry->d_flags |= DCACHE_UNHASHED;
  38. return 1;
  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_delete = anon_inodefs_delete_dentry,
  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_getfd() will share a single inode,
  72. * hence saving memory and avoiding code duplication for the file/inode/dentry
  73. * setup. Returns new descriptor or -error.
  74. */
  75. int anon_inode_getfd(const char *name, const struct file_operations *fops,
  76. void *priv, int flags)
  77. {
  78. struct qstr this;
  79. struct dentry *dentry;
  80. struct file *file;
  81. int error, fd;
  82. if (IS_ERR(anon_inode_inode))
  83. return -ENODEV;
  84. if (fops->owner && !try_module_get(fops->owner))
  85. return -ENOENT;
  86. error = get_unused_fd_flags(flags);
  87. if (error < 0)
  88. goto err_module;
  89. fd = error;
  90. /*
  91. * Link the inode to a directory entry by creating a unique name
  92. * using the inode sequence number.
  93. */
  94. error = -ENOMEM;
  95. this.name = name;
  96. this.len = strlen(name);
  97. this.hash = 0;
  98. dentry = d_alloc(anon_inode_mnt->mnt_sb->s_root, &this);
  99. if (!dentry)
  100. goto err_put_unused_fd;
  101. /*
  102. * We know the anon_inode inode count is always greater than zero,
  103. * so we can avoid doing an igrab() and we can use an open-coded
  104. * atomic_inc().
  105. */
  106. atomic_inc(&anon_inode_inode->i_count);
  107. dentry->d_op = &anon_inodefs_dentry_operations;
  108. /* Do not publish this dentry inside the global dentry hash table */
  109. dentry->d_flags &= ~DCACHE_UNHASHED;
  110. d_instantiate(dentry, anon_inode_inode);
  111. error = -ENFILE;
  112. file = alloc_file(anon_inode_mnt, dentry,
  113. FMODE_READ | FMODE_WRITE, fops);
  114. if (!file)
  115. goto err_dput;
  116. file->f_mapping = anon_inode_inode->i_mapping;
  117. file->f_pos = 0;
  118. file->f_flags = O_RDWR | (flags & O_NONBLOCK);
  119. file->f_version = 0;
  120. file->private_data = priv;
  121. fd_install(fd, file);
  122. return fd;
  123. err_dput:
  124. dput(dentry);
  125. err_put_unused_fd:
  126. put_unused_fd(fd);
  127. err_module:
  128. module_put(fops->owner);
  129. return error;
  130. }
  131. EXPORT_SYMBOL_GPL(anon_inode_getfd);
  132. /*
  133. * A single inode exists for all anon_inode files. Contrary to pipes,
  134. * anon_inode inodes have no associated per-instance data, so we need
  135. * only allocate one of them.
  136. */
  137. static struct inode *anon_inode_mkinode(void)
  138. {
  139. struct inode *inode = new_inode(anon_inode_mnt->mnt_sb);
  140. if (!inode)
  141. return ERR_PTR(-ENOMEM);
  142. inode->i_fop = &anon_inode_fops;
  143. inode->i_mapping->a_ops = &anon_aops;
  144. /*
  145. * Mark the inode dirty from the very beginning,
  146. * that way it will never be moved to the dirty
  147. * list because mark_inode_dirty() will think
  148. * that it already _is_ on the dirty list.
  149. */
  150. inode->i_state = I_DIRTY;
  151. inode->i_mode = S_IRUSR | S_IWUSR;
  152. inode->i_uid = current_fsuid();
  153. inode->i_gid = current_fsgid();
  154. inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
  155. return inode;
  156. }
  157. static int __init anon_inode_init(void)
  158. {
  159. int error;
  160. error = register_filesystem(&anon_inode_fs_type);
  161. if (error)
  162. goto err_exit;
  163. anon_inode_mnt = kern_mount(&anon_inode_fs_type);
  164. if (IS_ERR(anon_inode_mnt)) {
  165. error = PTR_ERR(anon_inode_mnt);
  166. goto err_unregister_filesystem;
  167. }
  168. anon_inode_inode = anon_inode_mkinode();
  169. if (IS_ERR(anon_inode_inode)) {
  170. error = PTR_ERR(anon_inode_inode);
  171. goto err_mntput;
  172. }
  173. return 0;
  174. err_mntput:
  175. mntput(anon_inode_mnt);
  176. err_unregister_filesystem:
  177. unregister_filesystem(&anon_inode_fs_type);
  178. err_exit:
  179. panic(KERN_ERR "anon_inode_init() failed (%d)\n", error);
  180. }
  181. fs_initcall(anon_inode_init);