control.c 8.2 KB

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