anon_inodes.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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. * @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. error = get_unused_fd_flags(flags);
  74. if (error < 0)
  75. return error;
  76. fd = error;
  77. /*
  78. * Link the inode to a directory entry by creating a unique name
  79. * using the inode sequence number.
  80. */
  81. error = -ENOMEM;
  82. this.name = name;
  83. this.len = strlen(name);
  84. this.hash = 0;
  85. dentry = d_alloc(anon_inode_mnt->mnt_sb->s_root, &this);
  86. if (!dentry)
  87. goto err_put_unused_fd;
  88. /*
  89. * We know the anon_inode inode count is always greater than zero,
  90. * so we can avoid doing an igrab() and we can use an open-coded
  91. * atomic_inc().
  92. */
  93. atomic_inc(&anon_inode_inode->i_count);
  94. dentry->d_op = &anon_inodefs_dentry_operations;
  95. /* Do not publish this dentry inside the global dentry hash table */
  96. dentry->d_flags &= ~DCACHE_UNHASHED;
  97. d_instantiate(dentry, anon_inode_inode);
  98. error = -ENFILE;
  99. file = alloc_file(anon_inode_mnt, dentry,
  100. FMODE_READ | FMODE_WRITE, fops);
  101. if (!file)
  102. goto err_dput;
  103. file->f_mapping = anon_inode_inode->i_mapping;
  104. file->f_pos = 0;
  105. file->f_flags = O_RDWR | (flags & O_NONBLOCK);
  106. file->f_version = 0;
  107. file->private_data = priv;
  108. fd_install(fd, file);
  109. return fd;
  110. err_dput:
  111. dput(dentry);
  112. err_put_unused_fd:
  113. put_unused_fd(fd);
  114. return error;
  115. }
  116. EXPORT_SYMBOL_GPL(anon_inode_getfd);
  117. /*
  118. * A single inode exists for all anon_inode files. Contrary to pipes,
  119. * anon_inode inodes have no associated per-instance data, so we need
  120. * only allocate one of them.
  121. */
  122. static struct inode *anon_inode_mkinode(void)
  123. {
  124. struct inode *inode = new_inode(anon_inode_mnt->mnt_sb);
  125. if (!inode)
  126. return ERR_PTR(-ENOMEM);
  127. inode->i_fop = &anon_inode_fops;
  128. /*
  129. * Mark the inode dirty from the very beginning,
  130. * that way it will never be moved to the dirty
  131. * list because mark_inode_dirty() will think
  132. * that it already _is_ on the dirty list.
  133. */
  134. inode->i_state = I_DIRTY;
  135. inode->i_mode = S_IRUSR | S_IWUSR;
  136. inode->i_uid = current->fsuid;
  137. inode->i_gid = current->fsgid;
  138. inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
  139. return inode;
  140. }
  141. static int __init anon_inode_init(void)
  142. {
  143. int error;
  144. error = register_filesystem(&anon_inode_fs_type);
  145. if (error)
  146. goto err_exit;
  147. anon_inode_mnt = kern_mount(&anon_inode_fs_type);
  148. if (IS_ERR(anon_inode_mnt)) {
  149. error = PTR_ERR(anon_inode_mnt);
  150. goto err_unregister_filesystem;
  151. }
  152. anon_inode_inode = anon_inode_mkinode();
  153. if (IS_ERR(anon_inode_inode)) {
  154. error = PTR_ERR(anon_inode_inode);
  155. goto err_mntput;
  156. }
  157. return 0;
  158. err_mntput:
  159. mntput(anon_inode_mnt);
  160. err_unregister_filesystem:
  161. unregister_filesystem(&anon_inode_fs_type);
  162. err_exit:
  163. panic(KERN_ERR "anon_inode_init() failed (%d)\n", error);
  164. }
  165. fs_initcall(anon_inode_init);