control.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  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. #include <linux/fs_context.h>
  11. #define FUSE_CTL_SUPER_MAGIC 0x65735543
  12. /*
  13. * This is non-NULL when the single instance of the control filesystem
  14. * exists. Protected by fuse_mutex
  15. */
  16. static struct super_block *fuse_control_sb;
  17. static struct fuse_conn *fuse_ctl_file_conn_get(struct file *file)
  18. {
  19. struct fuse_conn *fc;
  20. mutex_lock(&fuse_mutex);
  21. fc = file_inode(file)->i_private;
  22. if (fc)
  23. fc = fuse_conn_get(fc);
  24. mutex_unlock(&fuse_mutex);
  25. return fc;
  26. }
  27. static ssize_t fuse_conn_abort_write(struct file *file, const char __user *buf,
  28. size_t count, loff_t *ppos)
  29. {
  30. struct fuse_conn *fc = fuse_ctl_file_conn_get(file);
  31. if (fc) {
  32. if (fc->abort_err)
  33. fc->aborted = true;
  34. fuse_abort_conn(fc);
  35. fuse_conn_put(fc);
  36. }
  37. return count;
  38. }
  39. static ssize_t fuse_conn_waiting_read(struct file *file, char __user *buf,
  40. size_t len, loff_t *ppos)
  41. {
  42. char tmp[32];
  43. size_t size;
  44. if (!*ppos) {
  45. long value;
  46. struct fuse_conn *fc = fuse_ctl_file_conn_get(file);
  47. if (!fc)
  48. return 0;
  49. value = atomic_read(&fc->num_waiting);
  50. file->private_data = (void *)value;
  51. fuse_conn_put(fc);
  52. }
  53. size = sprintf(tmp, "%ld\n", (long)file->private_data);
  54. return simple_read_from_buffer(buf, len, ppos, tmp, size);
  55. }
  56. static ssize_t fuse_conn_limit_read(struct file *file, char __user *buf,
  57. size_t len, loff_t *ppos, unsigned val)
  58. {
  59. char tmp[32];
  60. size_t size = sprintf(tmp, "%u\n", val);
  61. return simple_read_from_buffer(buf, len, ppos, tmp, size);
  62. }
  63. static ssize_t fuse_conn_limit_write(struct file *file, const char __user *buf,
  64. size_t count, loff_t *ppos, unsigned *val,
  65. unsigned global_limit)
  66. {
  67. unsigned long t;
  68. unsigned limit = (1 << 16) - 1;
  69. int err;
  70. if (*ppos)
  71. return -EINVAL;
  72. err = kstrtoul_from_user(buf, count, 0, &t);
  73. if (err)
  74. return err;
  75. if (!capable(CAP_SYS_ADMIN))
  76. limit = min(limit, global_limit);
  77. if (t > limit)
  78. return -EINVAL;
  79. *val = t;
  80. return count;
  81. }
  82. static ssize_t fuse_conn_max_background_read(struct file *file,
  83. char __user *buf, size_t len,
  84. loff_t *ppos)
  85. {
  86. struct fuse_conn *fc;
  87. unsigned val;
  88. fc = fuse_ctl_file_conn_get(file);
  89. if (!fc)
  90. return 0;
  91. val = READ_ONCE(fc->max_background);
  92. fuse_conn_put(fc);
  93. return fuse_conn_limit_read(file, buf, len, ppos, val);
  94. }
  95. static ssize_t fuse_conn_max_background_write(struct file *file,
  96. const char __user *buf,
  97. size_t count, loff_t *ppos)
  98. {
  99. unsigned val;
  100. ssize_t ret;
  101. ret = fuse_conn_limit_write(file, buf, count, ppos, &val,
  102. max_user_bgreq);
  103. if (ret > 0) {
  104. struct fuse_conn *fc = fuse_ctl_file_conn_get(file);
  105. if (fc) {
  106. spin_lock(&fc->bg_lock);
  107. fc->max_background = val;
  108. fc->blocked = fc->num_background >= fc->max_background;
  109. if (!fc->blocked)
  110. wake_up(&fc->blocked_waitq);
  111. spin_unlock(&fc->bg_lock);
  112. fuse_conn_put(fc);
  113. }
  114. }
  115. return ret;
  116. }
  117. static ssize_t fuse_conn_congestion_threshold_read(struct file *file,
  118. char __user *buf, size_t len,
  119. loff_t *ppos)
  120. {
  121. struct fuse_conn *fc;
  122. unsigned val;
  123. fc = fuse_ctl_file_conn_get(file);
  124. if (!fc)
  125. return 0;
  126. val = READ_ONCE(fc->congestion_threshold);
  127. fuse_conn_put(fc);
  128. return fuse_conn_limit_read(file, buf, len, ppos, val);
  129. }
  130. static ssize_t fuse_conn_congestion_threshold_write(struct file *file,
  131. const char __user *buf,
  132. size_t count, loff_t *ppos)
  133. {
  134. unsigned val;
  135. struct fuse_conn *fc;
  136. ssize_t ret;
  137. ret = fuse_conn_limit_write(file, buf, count, ppos, &val,
  138. max_user_congthresh);
  139. if (ret <= 0)
  140. goto out;
  141. fc = fuse_ctl_file_conn_get(file);
  142. if (!fc)
  143. goto out;
  144. WRITE_ONCE(fc->congestion_threshold, val);
  145. fuse_conn_put(fc);
  146. out:
  147. return ret;
  148. }
  149. static const struct file_operations fuse_ctl_abort_ops = {
  150. .open = nonseekable_open,
  151. .write = fuse_conn_abort_write,
  152. };
  153. static const struct file_operations fuse_ctl_waiting_ops = {
  154. .open = nonseekable_open,
  155. .read = fuse_conn_waiting_read,
  156. };
  157. static const struct file_operations fuse_conn_max_background_ops = {
  158. .open = nonseekable_open,
  159. .read = fuse_conn_max_background_read,
  160. .write = fuse_conn_max_background_write,
  161. };
  162. static const struct file_operations fuse_conn_congestion_threshold_ops = {
  163. .open = nonseekable_open,
  164. .read = fuse_conn_congestion_threshold_read,
  165. .write = fuse_conn_congestion_threshold_write,
  166. };
  167. static struct dentry *fuse_ctl_add_dentry(struct dentry *parent,
  168. struct fuse_conn *fc,
  169. const char *name,
  170. int mode, int nlink,
  171. const struct inode_operations *iop,
  172. const struct file_operations *fop)
  173. {
  174. struct dentry *dentry;
  175. struct inode *inode;
  176. BUG_ON(fc->ctl_ndents >= FUSE_CTL_NUM_DENTRIES);
  177. dentry = d_alloc_name(parent, name);
  178. if (!dentry)
  179. return NULL;
  180. inode = new_inode(fuse_control_sb);
  181. if (!inode) {
  182. dput(dentry);
  183. return NULL;
  184. }
  185. inode->i_ino = get_next_ino();
  186. inode->i_mode = mode;
  187. inode->i_uid = fc->user_id;
  188. inode->i_gid = fc->group_id;
  189. simple_inode_init_ts(inode);
  190. /* setting ->i_op to NULL is not allowed */
  191. if (iop)
  192. inode->i_op = iop;
  193. inode->i_fop = fop;
  194. set_nlink(inode, nlink);
  195. inode->i_private = fc;
  196. d_add(dentry, inode);
  197. fc->ctl_dentry[fc->ctl_ndents++] = dentry;
  198. return dentry;
  199. }
  200. /*
  201. * Add a connection to the control filesystem (if it exists). Caller
  202. * must hold fuse_mutex
  203. */
  204. int fuse_ctl_add_conn(struct fuse_conn *fc)
  205. {
  206. struct dentry *parent;
  207. char name[32];
  208. if (!fuse_control_sb || fc->no_control)
  209. return 0;
  210. parent = fuse_control_sb->s_root;
  211. inc_nlink(d_inode(parent));
  212. sprintf(name, "%u", fc->dev);
  213. parent = fuse_ctl_add_dentry(parent, fc, name, S_IFDIR | 0500, 2,
  214. &simple_dir_inode_operations,
  215. &simple_dir_operations);
  216. if (!parent)
  217. goto err;
  218. if (!fuse_ctl_add_dentry(parent, fc, "waiting", S_IFREG | 0400, 1,
  219. NULL, &fuse_ctl_waiting_ops) ||
  220. !fuse_ctl_add_dentry(parent, fc, "abort", S_IFREG | 0200, 1,
  221. NULL, &fuse_ctl_abort_ops) ||
  222. !fuse_ctl_add_dentry(parent, fc, "max_background", S_IFREG | 0600,
  223. 1, NULL, &fuse_conn_max_background_ops) ||
  224. !fuse_ctl_add_dentry(parent, fc, "congestion_threshold",
  225. S_IFREG | 0600, 1, NULL,
  226. &fuse_conn_congestion_threshold_ops))
  227. goto err;
  228. return 0;
  229. err:
  230. fuse_ctl_remove_conn(fc);
  231. return -ENOMEM;
  232. }
  233. /*
  234. * Remove a connection from the control filesystem (if it exists).
  235. * Caller must hold fuse_mutex
  236. */
  237. void fuse_ctl_remove_conn(struct fuse_conn *fc)
  238. {
  239. int i;
  240. if (!fuse_control_sb || fc->no_control)
  241. return;
  242. for (i = fc->ctl_ndents - 1; i >= 0; i--) {
  243. struct dentry *dentry = fc->ctl_dentry[i];
  244. d_inode(dentry)->i_private = NULL;
  245. if (!i) {
  246. /* Get rid of submounts: */
  247. d_invalidate(dentry);
  248. }
  249. dput(dentry);
  250. }
  251. drop_nlink(d_inode(fuse_control_sb->s_root));
  252. }
  253. static int fuse_ctl_fill_super(struct super_block *sb, struct fs_context *fsc)
  254. {
  255. static const struct tree_descr empty_descr = {""};
  256. struct fuse_conn *fc;
  257. int err;
  258. err = simple_fill_super(sb, FUSE_CTL_SUPER_MAGIC, &empty_descr);
  259. if (err)
  260. return err;
  261. mutex_lock(&fuse_mutex);
  262. BUG_ON(fuse_control_sb);
  263. fuse_control_sb = sb;
  264. list_for_each_entry(fc, &fuse_conn_list, entry) {
  265. err = fuse_ctl_add_conn(fc);
  266. if (err) {
  267. fuse_control_sb = NULL;
  268. mutex_unlock(&fuse_mutex);
  269. return err;
  270. }
  271. }
  272. mutex_unlock(&fuse_mutex);
  273. return 0;
  274. }
  275. static int fuse_ctl_get_tree(struct fs_context *fsc)
  276. {
  277. return get_tree_single(fsc, fuse_ctl_fill_super);
  278. }
  279. static const struct fs_context_operations fuse_ctl_context_ops = {
  280. .get_tree = fuse_ctl_get_tree,
  281. };
  282. static int fuse_ctl_init_fs_context(struct fs_context *fsc)
  283. {
  284. fsc->ops = &fuse_ctl_context_ops;
  285. return 0;
  286. }
  287. static void fuse_ctl_kill_sb(struct super_block *sb)
  288. {
  289. struct fuse_conn *fc;
  290. mutex_lock(&fuse_mutex);
  291. fuse_control_sb = NULL;
  292. list_for_each_entry(fc, &fuse_conn_list, entry)
  293. fc->ctl_ndents = 0;
  294. mutex_unlock(&fuse_mutex);
  295. kill_litter_super(sb);
  296. }
  297. static struct file_system_type fuse_ctl_fs_type = {
  298. .owner = THIS_MODULE,
  299. .name = "fusectl",
  300. .init_fs_context = fuse_ctl_init_fs_context,
  301. .kill_sb = fuse_ctl_kill_sb,
  302. };
  303. MODULE_ALIAS_FS("fusectl");
  304. int __init fuse_ctl_init(void)
  305. {
  306. return register_filesystem(&fuse_ctl_fs_type);
  307. }
  308. void __exit fuse_ctl_cleanup(void)
  309. {
  310. unregister_filesystem(&fuse_ctl_fs_type);
  311. }