anon_inodes.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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. return mount_pseudo(fs_type, "anon_inode:", NULL,
  39. &anon_inodefs_dentry_operations, ANON_INODE_FS_MAGIC);
  40. }
  41. static struct file_system_type anon_inode_fs_type = {
  42. .name = "anon_inodefs",
  43. .mount = anon_inodefs_mount,
  44. .kill_sb = kill_anon_super,
  45. };
  46. /**
  47. * anon_inode_getfile - creates a new file instance by hooking it up to an
  48. * anonymous inode, and a dentry that describe the "class"
  49. * of the file
  50. *
  51. * @name: [in] name of the "class" of the new file
  52. * @fops: [in] file operations for the new file
  53. * @priv: [in] private data for the new file (will be file's private_data)
  54. * @flags: [in] flags
  55. *
  56. * Creates a new file by hooking it on a single inode. This is useful for files
  57. * that do not need to have a full-fledged inode in order to operate correctly.
  58. * All the files created with anon_inode_getfile() will share a single inode,
  59. * hence saving memory and avoiding code duplication for the file/inode/dentry
  60. * setup. Returns the newly created file* or an error pointer.
  61. */
  62. struct file *anon_inode_getfile(const char *name,
  63. const struct file_operations *fops,
  64. void *priv, int flags)
  65. {
  66. struct qstr this;
  67. struct path path;
  68. struct file *file;
  69. if (IS_ERR(anon_inode_inode))
  70. return ERR_PTR(-ENODEV);
  71. if (fops->owner && !try_module_get(fops->owner))
  72. return ERR_PTR(-ENOENT);
  73. /*
  74. * Link the inode to a directory entry by creating a unique name
  75. * using the inode sequence number.
  76. */
  77. file = ERR_PTR(-ENOMEM);
  78. this.name = name;
  79. this.len = strlen(name);
  80. this.hash = 0;
  81. path.dentry = d_alloc_pseudo(anon_inode_mnt->mnt_sb, &this);
  82. if (!path.dentry)
  83. goto err_module;
  84. path.mnt = mntget(anon_inode_mnt);
  85. /*
  86. * We know the anon_inode inode count is always greater than zero,
  87. * so ihold() is safe.
  88. */
  89. ihold(anon_inode_inode);
  90. d_instantiate(path.dentry, anon_inode_inode);
  91. file = alloc_file(&path, OPEN_FMODE(flags), fops);
  92. if (IS_ERR(file))
  93. goto err_dput;
  94. file->f_mapping = anon_inode_inode->i_mapping;
  95. file->f_flags = flags & (O_ACCMODE | O_NONBLOCK);
  96. file->private_data = priv;
  97. return file;
  98. err_dput:
  99. path_put(&path);
  100. err_module:
  101. module_put(fops->owner);
  102. return file;
  103. }
  104. EXPORT_SYMBOL_GPL(anon_inode_getfile);
  105. /**
  106. * anon_inode_getfd - creates a new file instance by hooking it up to an
  107. * anonymous inode, and a dentry that describe the "class"
  108. * of the file
  109. *
  110. * @name: [in] name of the "class" of the new file
  111. * @fops: [in] file operations for the new file
  112. * @priv: [in] private data for the new file (will be file's private_data)
  113. * @flags: [in] flags
  114. *
  115. * Creates a new file by hooking it on a single inode. This is useful for files
  116. * that do not need to have a full-fledged inode in order to operate correctly.
  117. * All the files created with anon_inode_getfd() will share a single inode,
  118. * hence saving memory and avoiding code duplication for the file/inode/dentry
  119. * setup. Returns new descriptor or an error code.
  120. */
  121. int anon_inode_getfd(const char *name, const struct file_operations *fops,
  122. void *priv, int flags)
  123. {
  124. int error, fd;
  125. struct file *file;
  126. error = get_unused_fd_flags(flags);
  127. if (error < 0)
  128. return error;
  129. fd = error;
  130. file = anon_inode_getfile(name, fops, priv, flags);
  131. if (IS_ERR(file)) {
  132. error = PTR_ERR(file);
  133. goto err_put_unused_fd;
  134. }
  135. fd_install(fd, file);
  136. return fd;
  137. err_put_unused_fd:
  138. put_unused_fd(fd);
  139. return error;
  140. }
  141. EXPORT_SYMBOL_GPL(anon_inode_getfd);
  142. static int __init anon_inode_init(void)
  143. {
  144. anon_inode_mnt = kern_mount(&anon_inode_fs_type);
  145. if (IS_ERR(anon_inode_mnt))
  146. panic("anon_inode_init() kernel mount failed (%ld)\n", PTR_ERR(anon_inode_mnt));
  147. anon_inode_inode = alloc_anon_inode(anon_inode_mnt->mnt_sb);
  148. if (IS_ERR(anon_inode_inode))
  149. panic("anon_inode_init() inode allocation failed (%ld)\n", PTR_ERR(anon_inode_inode));
  150. return 0;
  151. }
  152. fs_initcall(anon_inode_init);