control.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  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. struct fuse_mount *fm;
  137. ssize_t ret;
  138. ret = fuse_conn_limit_write(file, buf, count, ppos, &val,
  139. max_user_congthresh);
  140. if (ret <= 0)
  141. goto out;
  142. fc = fuse_ctl_file_conn_get(file);
  143. if (!fc)
  144. goto out;
  145. down_read(&fc->killsb);
  146. spin_lock(&fc->bg_lock);
  147. fc->congestion_threshold = val;
  148. /*
  149. * Get any fuse_mount belonging to this fuse_conn; s_bdi is
  150. * shared between all of them
  151. */
  152. if (!list_empty(&fc->mounts)) {
  153. fm = list_first_entry(&fc->mounts, struct fuse_mount, fc_entry);
  154. if (fc->num_background < fc->congestion_threshold) {
  155. clear_bdi_congested(fm->sb->s_bdi, BLK_RW_SYNC);
  156. clear_bdi_congested(fm->sb->s_bdi, BLK_RW_ASYNC);
  157. } else {
  158. set_bdi_congested(fm->sb->s_bdi, BLK_RW_SYNC);
  159. set_bdi_congested(fm->sb->s_bdi, BLK_RW_ASYNC);
  160. }
  161. }
  162. spin_unlock(&fc->bg_lock);
  163. up_read(&fc->killsb);
  164. fuse_conn_put(fc);
  165. out:
  166. return ret;
  167. }
  168. static const struct file_operations fuse_ctl_abort_ops = {
  169. .open = nonseekable_open,
  170. .write = fuse_conn_abort_write,
  171. .llseek = no_llseek,
  172. };
  173. static const struct file_operations fuse_ctl_waiting_ops = {
  174. .open = nonseekable_open,
  175. .read = fuse_conn_waiting_read,
  176. .llseek = no_llseek,
  177. };
  178. static const struct file_operations fuse_conn_max_background_ops = {
  179. .open = nonseekable_open,
  180. .read = fuse_conn_max_background_read,
  181. .write = fuse_conn_max_background_write,
  182. .llseek = no_llseek,
  183. };
  184. static const struct file_operations fuse_conn_congestion_threshold_ops = {
  185. .open = nonseekable_open,
  186. .read = fuse_conn_congestion_threshold_read,
  187. .write = fuse_conn_congestion_threshold_write,
  188. .llseek = no_llseek,
  189. };
  190. static struct dentry *fuse_ctl_add_dentry(struct dentry *parent,
  191. struct fuse_conn *fc,
  192. const char *name,
  193. int mode, int nlink,
  194. const struct inode_operations *iop,
  195. const struct file_operations *fop)
  196. {
  197. struct dentry *dentry;
  198. struct inode *inode;
  199. BUG_ON(fc->ctl_ndents >= FUSE_CTL_NUM_DENTRIES);
  200. dentry = d_alloc_name(parent, name);
  201. if (!dentry)
  202. return NULL;
  203. inode = new_inode(fuse_control_sb);
  204. if (!inode) {
  205. dput(dentry);
  206. return NULL;
  207. }
  208. inode->i_ino = get_next_ino();
  209. inode->i_mode = mode;
  210. inode->i_uid = fc->user_id;
  211. inode->i_gid = fc->group_id;
  212. inode->i_atime = inode->i_mtime = inode->i_ctime = current_time(inode);
  213. /* setting ->i_op to NULL is not allowed */
  214. if (iop)
  215. inode->i_op = iop;
  216. inode->i_fop = fop;
  217. set_nlink(inode, nlink);
  218. inode->i_private = fc;
  219. d_add(dentry, inode);
  220. fc->ctl_dentry[fc->ctl_ndents++] = dentry;
  221. return dentry;
  222. }
  223. /*
  224. * Add a connection to the control filesystem (if it exists). Caller
  225. * must hold fuse_mutex
  226. */
  227. int fuse_ctl_add_conn(struct fuse_conn *fc)
  228. {
  229. struct dentry *parent;
  230. char name[32];
  231. if (!fuse_control_sb)
  232. return 0;
  233. parent = fuse_control_sb->s_root;
  234. inc_nlink(d_inode(parent));
  235. sprintf(name, "%u", fc->dev);
  236. parent = fuse_ctl_add_dentry(parent, fc, name, S_IFDIR | 0500, 2,
  237. &simple_dir_inode_operations,
  238. &simple_dir_operations);
  239. if (!parent)
  240. goto err;
  241. if (!fuse_ctl_add_dentry(parent, fc, "waiting", S_IFREG | 0400, 1,
  242. NULL, &fuse_ctl_waiting_ops) ||
  243. !fuse_ctl_add_dentry(parent, fc, "abort", S_IFREG | 0200, 1,
  244. NULL, &fuse_ctl_abort_ops) ||
  245. !fuse_ctl_add_dentry(parent, fc, "max_background", S_IFREG | 0600,
  246. 1, NULL, &fuse_conn_max_background_ops) ||
  247. !fuse_ctl_add_dentry(parent, fc, "congestion_threshold",
  248. S_IFREG | 0600, 1, NULL,
  249. &fuse_conn_congestion_threshold_ops))
  250. goto err;
  251. return 0;
  252. err:
  253. fuse_ctl_remove_conn(fc);
  254. return -ENOMEM;
  255. }
  256. /*
  257. * Remove a connection from the control filesystem (if it exists).
  258. * Caller must hold fuse_mutex
  259. */
  260. void fuse_ctl_remove_conn(struct fuse_conn *fc)
  261. {
  262. int i;
  263. if (!fuse_control_sb)
  264. return;
  265. for (i = fc->ctl_ndents - 1; i >= 0; i--) {
  266. struct dentry *dentry = fc->ctl_dentry[i];
  267. d_inode(dentry)->i_private = NULL;
  268. if (!i) {
  269. /* Get rid of submounts: */
  270. d_invalidate(dentry);
  271. }
  272. dput(dentry);
  273. }
  274. drop_nlink(d_inode(fuse_control_sb->s_root));
  275. }
  276. static int fuse_ctl_fill_super(struct super_block *sb, struct fs_context *fctx)
  277. {
  278. static const struct tree_descr empty_descr = {""};
  279. struct fuse_conn *fc;
  280. int err;
  281. err = simple_fill_super(sb, FUSE_CTL_SUPER_MAGIC, &empty_descr);
  282. if (err)
  283. return err;
  284. mutex_lock(&fuse_mutex);
  285. BUG_ON(fuse_control_sb);
  286. fuse_control_sb = sb;
  287. list_for_each_entry(fc, &fuse_conn_list, entry) {
  288. err = fuse_ctl_add_conn(fc);
  289. if (err) {
  290. fuse_control_sb = NULL;
  291. mutex_unlock(&fuse_mutex);
  292. return err;
  293. }
  294. }
  295. mutex_unlock(&fuse_mutex);
  296. return 0;
  297. }
  298. static int fuse_ctl_get_tree(struct fs_context *fc)
  299. {
  300. return get_tree_single(fc, fuse_ctl_fill_super);
  301. }
  302. static const struct fs_context_operations fuse_ctl_context_ops = {
  303. .get_tree = fuse_ctl_get_tree,
  304. };
  305. static int fuse_ctl_init_fs_context(struct fs_context *fc)
  306. {
  307. fc->ops = &fuse_ctl_context_ops;
  308. return 0;
  309. }
  310. static void fuse_ctl_kill_sb(struct super_block *sb)
  311. {
  312. struct fuse_conn *fc;
  313. mutex_lock(&fuse_mutex);
  314. fuse_control_sb = NULL;
  315. list_for_each_entry(fc, &fuse_conn_list, entry)
  316. fc->ctl_ndents = 0;
  317. mutex_unlock(&fuse_mutex);
  318. kill_litter_super(sb);
  319. }
  320. static struct file_system_type fuse_ctl_fs_type = {
  321. .owner = THIS_MODULE,
  322. .name = "fusectl",
  323. .init_fs_context = fuse_ctl_init_fs_context,
  324. .kill_sb = fuse_ctl_kill_sb,
  325. };
  326. MODULE_ALIAS_FS("fusectl");
  327. int __init fuse_ctl_init(void)
  328. {
  329. return register_filesystem(&fuse_ctl_fs_type);
  330. }
  331. void __exit fuse_ctl_cleanup(void)
  332. {
  333. unregister_filesystem(&fuse_ctl_fs_type);
  334. }