anon_inodes.c 4.9 KB

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