control.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. /*
  2. FUSE: Filesystem in Userspace
  3. Copyright (C) 2001-2006 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. struct fuse_conn *fc = fuse_ctl_file_conn_get(file);
  43. if (!fc)
  44. return 0;
  45. file->private_data=(void *)(long)atomic_read(&fc->num_waiting);
  46. fuse_conn_put(fc);
  47. }
  48. size = sprintf(tmp, "%ld\n", (long)file->private_data);
  49. return simple_read_from_buffer(buf, len, ppos, tmp, size);
  50. }
  51. static const struct file_operations fuse_ctl_abort_ops = {
  52. .open = nonseekable_open,
  53. .write = fuse_conn_abort_write,
  54. };
  55. static const struct file_operations fuse_ctl_waiting_ops = {
  56. .open = nonseekable_open,
  57. .read = fuse_conn_waiting_read,
  58. };
  59. static struct dentry *fuse_ctl_add_dentry(struct dentry *parent,
  60. struct fuse_conn *fc,
  61. const char *name,
  62. int mode, int nlink,
  63. const struct inode_operations *iop,
  64. const struct file_operations *fop)
  65. {
  66. struct dentry *dentry;
  67. struct inode *inode;
  68. BUG_ON(fc->ctl_ndents >= FUSE_CTL_NUM_DENTRIES);
  69. dentry = d_alloc_name(parent, name);
  70. if (!dentry)
  71. return NULL;
  72. fc->ctl_dentry[fc->ctl_ndents++] = dentry;
  73. inode = new_inode(fuse_control_sb);
  74. if (!inode)
  75. return NULL;
  76. inode->i_mode = mode;
  77. inode->i_uid = fc->user_id;
  78. inode->i_gid = fc->group_id;
  79. inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
  80. /* setting ->i_op to NULL is not allowed */
  81. if (iop)
  82. inode->i_op = iop;
  83. inode->i_fop = fop;
  84. inode->i_nlink = nlink;
  85. inode->i_private = fc;
  86. d_add(dentry, inode);
  87. return dentry;
  88. }
  89. /*
  90. * Add a connection to the control filesystem (if it exists). Caller
  91. * must hold fuse_mutex
  92. */
  93. int fuse_ctl_add_conn(struct fuse_conn *fc)
  94. {
  95. struct dentry *parent;
  96. char name[32];
  97. if (!fuse_control_sb)
  98. return 0;
  99. parent = fuse_control_sb->s_root;
  100. inc_nlink(parent->d_inode);
  101. sprintf(name, "%llu", (unsigned long long) fc->id);
  102. parent = fuse_ctl_add_dentry(parent, fc, name, S_IFDIR | 0500, 2,
  103. &simple_dir_inode_operations,
  104. &simple_dir_operations);
  105. if (!parent)
  106. goto err;
  107. if (!fuse_ctl_add_dentry(parent, fc, "waiting", S_IFREG | 0400, 1,
  108. NULL, &fuse_ctl_waiting_ops) ||
  109. !fuse_ctl_add_dentry(parent, fc, "abort", S_IFREG | 0200, 1,
  110. NULL, &fuse_ctl_abort_ops))
  111. goto err;
  112. return 0;
  113. err:
  114. fuse_ctl_remove_conn(fc);
  115. return -ENOMEM;
  116. }
  117. /*
  118. * Remove a connection from the control filesystem (if it exists).
  119. * Caller must hold fuse_mutex
  120. */
  121. void fuse_ctl_remove_conn(struct fuse_conn *fc)
  122. {
  123. int i;
  124. if (!fuse_control_sb)
  125. return;
  126. for (i = fc->ctl_ndents - 1; i >= 0; i--) {
  127. struct dentry *dentry = fc->ctl_dentry[i];
  128. dentry->d_inode->i_private = NULL;
  129. d_drop(dentry);
  130. dput(dentry);
  131. }
  132. fuse_control_sb->s_root->d_inode->i_nlink--;
  133. }
  134. static int fuse_ctl_fill_super(struct super_block *sb, void *data, int silent)
  135. {
  136. struct tree_descr empty_descr = {""};
  137. struct fuse_conn *fc;
  138. int err;
  139. err = simple_fill_super(sb, FUSE_CTL_SUPER_MAGIC, &empty_descr);
  140. if (err)
  141. return err;
  142. mutex_lock(&fuse_mutex);
  143. BUG_ON(fuse_control_sb);
  144. fuse_control_sb = sb;
  145. list_for_each_entry(fc, &fuse_conn_list, entry) {
  146. err = fuse_ctl_add_conn(fc);
  147. if (err) {
  148. fuse_control_sb = NULL;
  149. mutex_unlock(&fuse_mutex);
  150. return err;
  151. }
  152. }
  153. mutex_unlock(&fuse_mutex);
  154. return 0;
  155. }
  156. static int fuse_ctl_get_sb(struct file_system_type *fs_type, int flags,
  157. const char *dev_name, void *raw_data,
  158. struct vfsmount *mnt)
  159. {
  160. return get_sb_single(fs_type, flags, raw_data,
  161. fuse_ctl_fill_super, mnt);
  162. }
  163. static void fuse_ctl_kill_sb(struct super_block *sb)
  164. {
  165. struct fuse_conn *fc;
  166. mutex_lock(&fuse_mutex);
  167. fuse_control_sb = NULL;
  168. list_for_each_entry(fc, &fuse_conn_list, entry)
  169. fc->ctl_ndents = 0;
  170. mutex_unlock(&fuse_mutex);
  171. kill_litter_super(sb);
  172. }
  173. static struct file_system_type fuse_ctl_fs_type = {
  174. .owner = THIS_MODULE,
  175. .name = "fusectl",
  176. .get_sb = fuse_ctl_get_sb,
  177. .kill_sb = fuse_ctl_kill_sb,
  178. };
  179. int __init fuse_ctl_init(void)
  180. {
  181. return register_filesystem(&fuse_ctl_fs_type);
  182. }
  183. void fuse_ctl_cleanup(void)
  184. {
  185. unregister_filesystem(&fuse_ctl_fs_type);
  186. }