control.c 8.5 KB

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