anon_inodes.c 5.1 KB

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