anon_inodes.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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 <linux/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 file *file;
  67. if (IS_ERR(anon_inode_inode))
  68. return ERR_PTR(-ENODEV);
  69. if (fops->owner && !try_module_get(fops->owner))
  70. return ERR_PTR(-ENOENT);
  71. /*
  72. * We know the anon_inode inode count is always greater than zero,
  73. * so ihold() is safe.
  74. */
  75. ihold(anon_inode_inode);
  76. file = alloc_file_pseudo(anon_inode_inode, anon_inode_mnt, name,
  77. flags & (O_ACCMODE | O_NONBLOCK), fops);
  78. if (IS_ERR(file))
  79. goto err;
  80. file->f_mapping = anon_inode_inode->i_mapping;
  81. file->private_data = priv;
  82. return file;
  83. err:
  84. iput(anon_inode_inode);
  85. module_put(fops->owner);
  86. return file;
  87. }
  88. EXPORT_SYMBOL_GPL(anon_inode_getfile);
  89. /**
  90. * anon_inode_getfd - creates a new file instance by hooking it up to an
  91. * anonymous inode, and a dentry that describe the "class"
  92. * of the file
  93. *
  94. * @name: [in] name of the "class" of the new file
  95. * @fops: [in] file operations for the new file
  96. * @priv: [in] private data for the new file (will be file's private_data)
  97. * @flags: [in] flags
  98. *
  99. * Creates a new file by hooking it on a single inode. This is useful for files
  100. * that do not need to have a full-fledged inode in order to operate correctly.
  101. * All the files created with anon_inode_getfd() will share a single inode,
  102. * hence saving memory and avoiding code duplication for the file/inode/dentry
  103. * setup. Returns new descriptor or an error code.
  104. */
  105. int anon_inode_getfd(const char *name, const struct file_operations *fops,
  106. void *priv, int flags)
  107. {
  108. int error, fd;
  109. struct file *file;
  110. error = get_unused_fd_flags(flags);
  111. if (error < 0)
  112. return error;
  113. fd = error;
  114. file = anon_inode_getfile(name, fops, priv, flags);
  115. if (IS_ERR(file)) {
  116. error = PTR_ERR(file);
  117. goto err_put_unused_fd;
  118. }
  119. fd_install(fd, file);
  120. return fd;
  121. err_put_unused_fd:
  122. put_unused_fd(fd);
  123. return error;
  124. }
  125. EXPORT_SYMBOL_GPL(anon_inode_getfd);
  126. static int __init anon_inode_init(void)
  127. {
  128. anon_inode_mnt = kern_mount(&anon_inode_fs_type);
  129. if (IS_ERR(anon_inode_mnt))
  130. panic("anon_inode_init() kernel mount failed (%ld)\n", PTR_ERR(anon_inode_mnt));
  131. anon_inode_inode = alloc_anon_inode(anon_inode_mnt->mnt_sb);
  132. if (IS_ERR(anon_inode_inode))
  133. panic("anon_inode_init() inode allocation failed (%ld)\n", PTR_ERR(anon_inode_inode));
  134. return 0;
  135. }
  136. fs_initcall(anon_inode_init);