control.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. /*
  2. FUSE: Filesystem in Userspace
  3. Copyright (C) 2001-2008 Miklos Szeredi <miklos@szeredi.hu>
  4. This program can be distributed under the terms of the GNU GPL.
  5. See the file COPYING.
  6. */
  7. #include "fuse_i.h"
  8. #include <linux/init.h>
  9. #include <linux/module.h>
  10. #define FUSE_CTL_SUPER_MAGIC 0x65735543
  11. /*
  12. * This is non-NULL when the single instance of the control filesystem
  13. * exists. Protected by fuse_mutex
  14. */
  15. static struct super_block *fuse_control_sb;
  16. static struct fuse_conn *fuse_ctl_file_conn_get(struct file *file)
  17. {
  18. struct fuse_conn *fc;
  19. mutex_lock(&fuse_mutex);
  20. fc = file->f_path.dentry->d_inode->i_private;
  21. if (fc)
  22. fc = fuse_conn_get(fc);
  23. mutex_unlock(&fuse_mutex);
  24. return fc;
  25. }
  26. static ssize_t fuse_conn_abort_write(struct file *file, const char __user *buf,
  27. size_t count, loff_t *ppos)
  28. {
  29. struct fuse_conn *fc = fuse_ctl_file_conn_get(file);
  30. if (fc) {
  31. fuse_abort_conn(fc);
  32. fuse_conn_put(fc);
  33. }
  34. return count;
  35. }
  36. static ssize_t fuse_conn_waiting_read(struct file *file, char __user *buf,
  37. size_t len, loff_t *ppos)
  38. {
  39. char tmp[32];
  40. size_t size;
  41. if (!*ppos) {
  42. long value;
  43. struct fuse_conn *fc = fuse_ctl_file_conn_get(file);
  44. if (!fc)
  45. return 0;
  46. value = atomic_read(&fc->num_waiting);
  47. file->private_data = (void *)value;
  48. fuse_conn_put(fc);
  49. }
  50. size = sprintf(tmp, "%ld\n", (long)file->private_data);
  51. return simple_read_from_buffer(buf, len, ppos, tmp, size);
  52. }
  53. static const struct file_operations fuse_ctl_abort_ops = {
  54. .open = nonseekable_open,
  55. .write = fuse_conn_abort_write,
  56. };
  57. static const struct file_operations fuse_ctl_waiting_ops = {
  58. .open = nonseekable_open,
  59. .read = fuse_conn_waiting_read,
  60. };
  61. static struct dentry *fuse_ctl_add_dentry(struct dentry *parent,
  62. struct fuse_conn *fc,
  63. const char *name,
  64. int mode, int nlink,
  65. const struct inode_operations *iop,
  66. const struct file_operations *fop)
  67. {
  68. struct dentry *dentry;
  69. struct inode *inode;
  70. BUG_ON(fc->ctl_ndents >= FUSE_CTL_NUM_DENTRIES);
  71. dentry = d_alloc_name(parent, name);
  72. if (!dentry)
  73. return NULL;
  74. fc->ctl_dentry[fc->ctl_ndents++] = dentry;
  75. inode = new_inode(fuse_control_sb);
  76. if (!inode)
  77. return NULL;
  78. inode->i_mode = mode;
  79. inode->i_uid = fc->user_id;
  80. inode->i_gid = fc->group_id;
  81. inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
  82. /* setting ->i_op to NULL is not allowed */
  83. if (iop)
  84. inode->i_op = iop;
  85. inode->i_fop = fop;
  86. inode->i_nlink = nlink;
  87. inode->i_private = fc;
  88. d_add(dentry, inode);
  89. return dentry;
  90. }
  91. /*
  92. * Add a connection to the control filesystem (if it exists). Caller
  93. * must hold fuse_mutex
  94. */
  95. int fuse_ctl_add_conn(struct fuse_conn *fc)
  96. {
  97. struct dentry *parent;
  98. char name[32];
  99. if (!fuse_control_sb)
  100. return 0;
  101. parent = fuse_control_sb->s_root;
  102. inc_nlink(parent->d_inode);
  103. sprintf(name, "%u", fc->dev);
  104. parent = fuse_ctl_add_dentry(parent, fc, name, S_IFDIR | 0500, 2,
  105. &simple_dir_inode_operations,
  106. &simple_dir_operations);
  107. if (!parent)
  108. goto err;
  109. if (!fuse_ctl_add_dentry(parent, fc, "waiting", S_IFREG | 0400, 1,
  110. NULL, &fuse_ctl_waiting_ops) ||
  111. !fuse_ctl_add_dentry(parent, fc, "abort", S_IFREG | 0200, 1,
  112. NULL, &fuse_ctl_abort_ops))
  113. goto err;
  114. return 0;
  115. err:
  116. fuse_ctl_remove_conn(fc);
  117. return -ENOMEM;
  118. }
  119. /*
  120. * Remove a connection from the control filesystem (if it exists).
  121. * Caller must hold fuse_mutex
  122. */
  123. void fuse_ctl_remove_conn(struct fuse_conn *fc)
  124. {
  125. int i;
  126. if (!fuse_control_sb)
  127. return;
  128. for (i = fc->ctl_ndents - 1; i >= 0; i--) {
  129. struct dentry *dentry = fc->ctl_dentry[i];
  130. dentry->d_inode->i_private = NULL;
  131. d_drop(dentry);
  132. dput(dentry);
  133. }
  134. fuse_control_sb->s_root->d_inode->i_nlink--;
  135. }
  136. static int fuse_ctl_fill_super(struct super_block *sb, void *data, int silent)
  137. {
  138. struct tree_descr empty_descr = {""};
  139. struct fuse_conn *fc;
  140. int err;
  141. err = simple_fill_super(sb, FUSE_CTL_SUPER_MAGIC, &empty_descr);
  142. if (err)
  143. return err;
  144. mutex_lock(&fuse_mutex);
  145. BUG_ON(fuse_control_sb);
  146. fuse_control_sb = sb;
  147. list_for_each_entry(fc, &fuse_conn_list, entry) {
  148. err = fuse_ctl_add_conn(fc);
  149. if (err) {
  150. fuse_control_sb = NULL;
  151. mutex_unlock(&fuse_mutex);
  152. return err;
  153. }
  154. }
  155. mutex_unlock(&fuse_mutex);
  156. return 0;
  157. }
  158. static int fuse_ctl_get_sb(struct file_system_type *fs_type, int flags,
  159. const char *dev_name, void *raw_data,
  160. struct vfsmount *mnt)
  161. {
  162. return get_sb_single(fs_type, flags, raw_data,
  163. fuse_ctl_fill_super, mnt);
  164. }
  165. static void fuse_ctl_kill_sb(struct super_block *sb)
  166. {
  167. struct fuse_conn *fc;
  168. mutex_lock(&fuse_mutex);
  169. fuse_control_sb = NULL;
  170. list_for_each_entry(fc, &fuse_conn_list, entry)
  171. fc->ctl_ndents = 0;
  172. mutex_unlock(&fuse_mutex);
  173. kill_litter_super(sb);
  174. }
  175. static struct file_system_type fuse_ctl_fs_type = {
  176. .owner = THIS_MODULE,
  177. .name = "fusectl",
  178. .get_sb = fuse_ctl_get_sb,
  179. .kill_sb = fuse_ctl_kill_sb,
  180. };
  181. int __init fuse_ctl_init(void)
  182. {
  183. return register_filesystem(&fuse_ctl_fs_type);
  184. }
  185. void fuse_ctl_cleanup(void)
  186. {
  187. unregister_filesystem(&fuse_ctl_fs_type);
  188. }