anon_inodes.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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 struct dentry_operations anon_inodefs_dentry_operations = {
  46. .d_delete = anon_inodefs_delete_dentry,
  47. };
  48. /**
  49. * anon_inode_getfd - creates a new file instance by hooking it up to an
  50. * anonymous inode, and a dentry that describe the "class"
  51. * of the file
  52. *
  53. * @pfd: [out] pointer to the file descriptor
  54. * @dpinode: [out] pointer to the inode
  55. * @pfile: [out] pointer to the file struct
  56. * @name: [in] name of the "class" of the new file
  57. * @fops [in] file operations for the new file
  58. * @priv [in] private data for the new file (will be file's private_data)
  59. *
  60. * Creates a new file by hooking it on a single inode. This is useful for files
  61. * that do not need to have a full-fledged inode in order to operate correctly.
  62. * All the files created with anon_inode_getfd() will share a single inode,
  63. * hence saving memory and avoiding code duplication for the file/inode/dentry
  64. * setup.
  65. */
  66. int anon_inode_getfd(int *pfd, struct inode **pinode, struct file **pfile,
  67. const char *name, const struct file_operations *fops,
  68. void *priv)
  69. {
  70. struct qstr this;
  71. struct dentry *dentry;
  72. struct inode *inode;
  73. struct file *file;
  74. int error, fd;
  75. if (IS_ERR(anon_inode_inode))
  76. return -ENODEV;
  77. file = get_empty_filp();
  78. if (!file)
  79. return -ENFILE;
  80. inode = igrab(anon_inode_inode);
  81. if (IS_ERR(inode)) {
  82. error = PTR_ERR(inode);
  83. goto err_put_filp;
  84. }
  85. error = get_unused_fd();
  86. if (error < 0)
  87. goto err_iput;
  88. fd = error;
  89. /*
  90. * Link the inode to a directory entry by creating a unique name
  91. * using the inode sequence number.
  92. */
  93. error = -ENOMEM;
  94. this.name = name;
  95. this.len = strlen(name);
  96. this.hash = 0;
  97. dentry = d_alloc(anon_inode_mnt->mnt_sb->s_root, &this);
  98. if (!dentry)
  99. goto err_put_unused_fd;
  100. dentry->d_op = &anon_inodefs_dentry_operations;
  101. /* Do not publish this dentry inside the global dentry hash table */
  102. dentry->d_flags &= ~DCACHE_UNHASHED;
  103. d_instantiate(dentry, inode);
  104. file->f_path.mnt = mntget(anon_inode_mnt);
  105. file->f_path.dentry = dentry;
  106. file->f_mapping = inode->i_mapping;
  107. file->f_pos = 0;
  108. file->f_flags = O_RDWR;
  109. file->f_op = fops;
  110. file->f_mode = FMODE_READ | FMODE_WRITE;
  111. file->f_version = 0;
  112. file->private_data = priv;
  113. fd_install(fd, file);
  114. *pfd = fd;
  115. *pinode = inode;
  116. *pfile = file;
  117. return 0;
  118. err_put_unused_fd:
  119. put_unused_fd(fd);
  120. err_iput:
  121. iput(inode);
  122. err_put_filp:
  123. put_filp(file);
  124. return error;
  125. }
  126. EXPORT_SYMBOL_GPL(anon_inode_getfd);
  127. /*
  128. * A single inode exists for all anon_inode files. Contrary to pipes,
  129. * anon_inode inodes have no associated per-instance data, so we need
  130. * only allocate one of them.
  131. */
  132. static struct inode *anon_inode_mkinode(void)
  133. {
  134. struct inode *inode = new_inode(anon_inode_mnt->mnt_sb);
  135. if (!inode)
  136. return ERR_PTR(-ENOMEM);
  137. inode->i_fop = &anon_inode_fops;
  138. /*
  139. * Mark the inode dirty from the very beginning,
  140. * that way it will never be moved to the dirty
  141. * list because mark_inode_dirty() will think
  142. * that it already _is_ on the dirty list.
  143. */
  144. inode->i_state = I_DIRTY;
  145. inode->i_mode = S_IRUSR | S_IWUSR;
  146. inode->i_uid = current->fsuid;
  147. inode->i_gid = current->fsgid;
  148. inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
  149. return inode;
  150. }
  151. static int __init anon_inode_init(void)
  152. {
  153. int error;
  154. error = register_filesystem(&anon_inode_fs_type);
  155. if (error)
  156. goto err_exit;
  157. anon_inode_mnt = kern_mount(&anon_inode_fs_type);
  158. if (IS_ERR(anon_inode_mnt)) {
  159. error = PTR_ERR(anon_inode_mnt);
  160. goto err_unregister_filesystem;
  161. }
  162. anon_inode_inode = anon_inode_mkinode();
  163. if (IS_ERR(anon_inode_inode)) {
  164. error = PTR_ERR(anon_inode_inode);
  165. goto err_mntput;
  166. }
  167. return 0;
  168. err_mntput:
  169. mntput(anon_inode_mnt);
  170. err_unregister_filesystem:
  171. unregister_filesystem(&anon_inode_fs_type);
  172. err_exit:
  173. panic(KERN_ERR "anon_inode_init() failed (%d)\n", error);
  174. }
  175. fs_initcall(anon_inode_init);