control.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  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. fc->max_background = val;
  104. fuse_conn_put(fc);
  105. }
  106. }
  107. return ret;
  108. }
  109. static ssize_t fuse_conn_congestion_threshold_read(struct file *file,
  110. char __user *buf, size_t len,
  111. loff_t *ppos)
  112. {
  113. struct fuse_conn *fc;
  114. unsigned val;
  115. fc = fuse_ctl_file_conn_get(file);
  116. if (!fc)
  117. return 0;
  118. val = READ_ONCE(fc->congestion_threshold);
  119. fuse_conn_put(fc);
  120. return fuse_conn_limit_read(file, buf, len, ppos, val);
  121. }
  122. static ssize_t fuse_conn_congestion_threshold_write(struct file *file,
  123. const char __user *buf,
  124. size_t count, loff_t *ppos)
  125. {
  126. unsigned uninitialized_var(val);
  127. ssize_t ret;
  128. ret = fuse_conn_limit_write(file, buf, count, ppos, &val,
  129. max_user_congthresh);
  130. if (ret > 0) {
  131. struct fuse_conn *fc = fuse_ctl_file_conn_get(file);
  132. if (fc) {
  133. fc->congestion_threshold = val;
  134. fuse_conn_put(fc);
  135. }
  136. }
  137. return ret;
  138. }
  139. static const struct file_operations fuse_ctl_abort_ops = {
  140. .open = nonseekable_open,
  141. .write = fuse_conn_abort_write,
  142. .llseek = no_llseek,
  143. };
  144. static const struct file_operations fuse_ctl_waiting_ops = {
  145. .open = nonseekable_open,
  146. .read = fuse_conn_waiting_read,
  147. .llseek = no_llseek,
  148. };
  149. static const struct file_operations fuse_conn_max_background_ops = {
  150. .open = nonseekable_open,
  151. .read = fuse_conn_max_background_read,
  152. .write = fuse_conn_max_background_write,
  153. .llseek = no_llseek,
  154. };
  155. static const struct file_operations fuse_conn_congestion_threshold_ops = {
  156. .open = nonseekable_open,
  157. .read = fuse_conn_congestion_threshold_read,
  158. .write = fuse_conn_congestion_threshold_write,
  159. .llseek = no_llseek,
  160. };
  161. static struct dentry *fuse_ctl_add_dentry(struct dentry *parent,
  162. struct fuse_conn *fc,
  163. const char *name,
  164. int mode, int nlink,
  165. const struct inode_operations *iop,
  166. const struct file_operations *fop)
  167. {
  168. struct dentry *dentry;
  169. struct inode *inode;
  170. BUG_ON(fc->ctl_ndents >= FUSE_CTL_NUM_DENTRIES);
  171. dentry = d_alloc_name(parent, name);
  172. if (!dentry)
  173. return NULL;
  174. inode = new_inode(fuse_control_sb);
  175. if (!inode) {
  176. dput(dentry);
  177. return NULL;
  178. }
  179. inode->i_ino = get_next_ino();
  180. inode->i_mode = mode;
  181. inode->i_uid = fc->user_id;
  182. inode->i_gid = fc->group_id;
  183. inode->i_atime = inode->i_mtime = inode->i_ctime = current_time(inode);
  184. /* setting ->i_op to NULL is not allowed */
  185. if (iop)
  186. inode->i_op = iop;
  187. inode->i_fop = fop;
  188. set_nlink(inode, nlink);
  189. inode->i_private = fc;
  190. d_add(dentry, inode);
  191. fc->ctl_dentry[fc->ctl_ndents++] = dentry;
  192. return dentry;
  193. }
  194. /*
  195. * Add a connection to the control filesystem (if it exists). Caller
  196. * must hold fuse_mutex
  197. */
  198. int fuse_ctl_add_conn(struct fuse_conn *fc)
  199. {
  200. struct dentry *parent;
  201. char name[32];
  202. if (!fuse_control_sb)
  203. return 0;
  204. parent = fuse_control_sb->s_root;
  205. inc_nlink(d_inode(parent));
  206. sprintf(name, "%u", fc->dev);
  207. parent = fuse_ctl_add_dentry(parent, fc, name, S_IFDIR | 0500, 2,
  208. &simple_dir_inode_operations,
  209. &simple_dir_operations);
  210. if (!parent)
  211. goto err;
  212. if (!fuse_ctl_add_dentry(parent, fc, "waiting", S_IFREG | 0400, 1,
  213. NULL, &fuse_ctl_waiting_ops) ||
  214. !fuse_ctl_add_dentry(parent, fc, "abort", S_IFREG | 0200, 1,
  215. NULL, &fuse_ctl_abort_ops) ||
  216. !fuse_ctl_add_dentry(parent, fc, "max_background", S_IFREG | 0600,
  217. 1, NULL, &fuse_conn_max_background_ops) ||
  218. !fuse_ctl_add_dentry(parent, fc, "congestion_threshold",
  219. S_IFREG | 0600, 1, NULL,
  220. &fuse_conn_congestion_threshold_ops))
  221. goto err;
  222. return 0;
  223. err:
  224. fuse_ctl_remove_conn(fc);
  225. return -ENOMEM;
  226. }
  227. /*
  228. * Remove a connection from the control filesystem (if it exists).
  229. * Caller must hold fuse_mutex
  230. */
  231. void fuse_ctl_remove_conn(struct fuse_conn *fc)
  232. {
  233. int i;
  234. if (!fuse_control_sb)
  235. return;
  236. for (i = fc->ctl_ndents - 1; i >= 0; i--) {
  237. struct dentry *dentry = fc->ctl_dentry[i];
  238. d_inode(dentry)->i_private = NULL;
  239. if (!i) {
  240. /* Get rid of submounts: */
  241. d_invalidate(dentry);
  242. }
  243. dput(dentry);
  244. }
  245. drop_nlink(d_inode(fuse_control_sb->s_root));
  246. }
  247. static int fuse_ctl_fill_super(struct super_block *sb, void *data, int silent)
  248. {
  249. static const struct tree_descr empty_descr = {""};
  250. struct fuse_conn *fc;
  251. int err;
  252. err = simple_fill_super(sb, FUSE_CTL_SUPER_MAGIC, &empty_descr);
  253. if (err)
  254. return err;
  255. mutex_lock(&fuse_mutex);
  256. BUG_ON(fuse_control_sb);
  257. fuse_control_sb = sb;
  258. list_for_each_entry(fc, &fuse_conn_list, entry) {
  259. err = fuse_ctl_add_conn(fc);
  260. if (err) {
  261. fuse_control_sb = NULL;
  262. mutex_unlock(&fuse_mutex);
  263. return err;
  264. }
  265. }
  266. mutex_unlock(&fuse_mutex);
  267. return 0;
  268. }
  269. static struct dentry *fuse_ctl_mount(struct file_system_type *fs_type,
  270. int flags, const char *dev_name, void *raw_data)
  271. {
  272. return mount_single(fs_type, flags, raw_data, fuse_ctl_fill_super);
  273. }
  274. static void fuse_ctl_kill_sb(struct super_block *sb)
  275. {
  276. struct fuse_conn *fc;
  277. mutex_lock(&fuse_mutex);
  278. fuse_control_sb = NULL;
  279. list_for_each_entry(fc, &fuse_conn_list, entry)
  280. fc->ctl_ndents = 0;
  281. mutex_unlock(&fuse_mutex);
  282. kill_litter_super(sb);
  283. }
  284. static struct file_system_type fuse_ctl_fs_type = {
  285. .owner = THIS_MODULE,
  286. .name = "fusectl",
  287. .mount = fuse_ctl_mount,
  288. .kill_sb = fuse_ctl_kill_sb,
  289. };
  290. MODULE_ALIAS_FS("fusectl");
  291. int __init fuse_ctl_init(void)
  292. {
  293. return register_filesystem(&fuse_ctl_fs_type);
  294. }
  295. void __exit fuse_ctl_cleanup(void)
  296. {
  297. unregister_filesystem(&fuse_ctl_fs_type);
  298. }