anon_inodes.c 5.2 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/cred.h>
  11. #include <linux/file.h>
  12. #include <linux/poll.h>
  13. #include <linux/sched.h>
  14. #include <linux/init.h>
  15. #include <linux/fs.h>
  16. #include <linux/mount.h>
  17. #include <linux/module.h>
  18. #include <linux/kernel.h>
  19. #include <linux/magic.h>
  20. #include <linux/anon_inodes.h>
  21. #include <asm/uaccess.h>
  22. static struct vfsmount *anon_inode_mnt __read_mostly;
  23. static struct inode *anon_inode_inode;
  24. /*
  25. * anon_inodefs_dname() is called from d_path().
  26. */
  27. static char *anon_inodefs_dname(struct dentry *dentry, char *buffer, int buflen)
  28. {
  29. return dynamic_dname(dentry, buffer, buflen, "anon_inode:%s",
  30. dentry->d_name.name);
  31. }
  32. static const struct dentry_operations anon_inodefs_dentry_operations = {
  33. .d_dname = anon_inodefs_dname,
  34. };
  35. static struct dentry *anon_inodefs_mount(struct file_system_type *fs_type,
  36. int flags, const char *dev_name, void *data)
  37. {
  38. struct dentry *root;
  39. root = mount_pseudo(fs_type, "anon_inode:", NULL,
  40. &anon_inodefs_dentry_operations, ANON_INODE_FS_MAGIC);
  41. if (!IS_ERR(root)) {
  42. struct super_block *s = root->d_sb;
  43. anon_inode_inode = alloc_anon_inode(s);
  44. if (IS_ERR(anon_inode_inode)) {
  45. dput(root);
  46. deactivate_locked_super(s);
  47. root = ERR_CAST(anon_inode_inode);
  48. }
  49. }
  50. return root;
  51. }
  52. static struct file_system_type anon_inode_fs_type = {
  53. .name = "anon_inodefs",
  54. .mount = anon_inodefs_mount,
  55. .kill_sb = kill_anon_super,
  56. };
  57. /**
  58. * anon_inode_getfile - creates a new file instance by hooking it up to an
  59. * anonymous inode, and a dentry that describe the "class"
  60. * of the file
  61. *
  62. * @name: [in] name of the "class" of the new file
  63. * @fops: [in] file operations for the new file
  64. * @priv: [in] private data for the new file (will be file's private_data)
  65. * @flags: [in] flags
  66. *
  67. * Creates a new file by hooking it on a single inode. This is useful for files
  68. * that do not need to have a full-fledged inode in order to operate correctly.
  69. * All the files created with anon_inode_getfile() will share a single inode,
  70. * hence saving memory and avoiding code duplication for the file/inode/dentry
  71. * setup. Returns the newly created file* or an error pointer.
  72. */
  73. struct file *anon_inode_getfile(const char *name,
  74. const struct file_operations *fops,
  75. void *priv, int flags)
  76. {
  77. struct qstr this;
  78. struct path path;
  79. struct file *file;
  80. if (IS_ERR(anon_inode_inode))
  81. return ERR_PTR(-ENODEV);
  82. if (fops->owner && !try_module_get(fops->owner))
  83. return ERR_PTR(-ENOENT);
  84. /*
  85. * Link the inode to a directory entry by creating a unique name
  86. * using the inode sequence number.
  87. */
  88. file = ERR_PTR(-ENOMEM);
  89. this.name = name;
  90. this.len = strlen(name);
  91. this.hash = 0;
  92. path.dentry = d_alloc_pseudo(anon_inode_mnt->mnt_sb, &this);
  93. if (!path.dentry)
  94. goto err_module;
  95. path.mnt = mntget(anon_inode_mnt);
  96. /*
  97. * We know the anon_inode inode count is always greater than zero,
  98. * so ihold() is safe.
  99. */
  100. ihold(anon_inode_inode);
  101. d_instantiate(path.dentry, anon_inode_inode);
  102. file = alloc_file(&path, OPEN_FMODE(flags), fops);
  103. if (IS_ERR(file))
  104. goto err_dput;
  105. file->f_mapping = anon_inode_inode->i_mapping;
  106. file->f_flags = flags & (O_ACCMODE | O_NONBLOCK);
  107. file->private_data = priv;
  108. return file;
  109. err_dput:
  110. path_put(&path);
  111. err_module:
  112. module_put(fops->owner);
  113. return file;
  114. }
  115. EXPORT_SYMBOL_GPL(anon_inode_getfile);
  116. /**
  117. * anon_inode_getfd - creates a new file instance by hooking it up to an
  118. * anonymous inode, and a dentry that describe the "class"
  119. * of the file
  120. *
  121. * @name: [in] name of the "class" of the new file
  122. * @fops: [in] file operations for the new file
  123. * @priv: [in] private data for the new file (will be file's private_data)
  124. * @flags: [in] flags
  125. *
  126. * Creates a new file by hooking it on a single inode. This is useful for files
  127. * that do not need to have a full-fledged inode in order to operate correctly.
  128. * All the files created with anon_inode_getfd() will share a single inode,
  129. * hence saving memory and avoiding code duplication for the file/inode/dentry
  130. * setup. Returns new descriptor or an error code.
  131. */
  132. int anon_inode_getfd(const char *name, const struct file_operations *fops,
  133. void *priv, int flags)
  134. {
  135. int error, fd;
  136. struct file *file;
  137. error = get_unused_fd_flags(flags);
  138. if (error < 0)
  139. return error;
  140. fd = error;
  141. file = anon_inode_getfile(name, fops, priv, flags);
  142. if (IS_ERR(file)) {
  143. error = PTR_ERR(file);
  144. goto err_put_unused_fd;
  145. }
  146. fd_install(fd, file);
  147. return fd;
  148. err_put_unused_fd:
  149. put_unused_fd(fd);
  150. return error;
  151. }
  152. EXPORT_SYMBOL_GPL(anon_inode_getfd);
  153. static int __init anon_inode_init(void)
  154. {
  155. int error;
  156. error = register_filesystem(&anon_inode_fs_type);
  157. if (error)
  158. goto err_exit;
  159. anon_inode_mnt = kern_mount(&anon_inode_fs_type);
  160. if (IS_ERR(anon_inode_mnt)) {
  161. error = PTR_ERR(anon_inode_mnt);
  162. goto err_unregister_filesystem;
  163. }
  164. return 0;
  165. err_unregister_filesystem:
  166. unregister_filesystem(&anon_inode_fs_type);
  167. err_exit:
  168. panic(KERN_ERR "anon_inode_init() failed (%d)\n", error);
  169. }
  170. fs_initcall(anon_inode_init);