control.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  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->f_path.dentry->d_inode->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);
  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 = 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 = 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. fc->ctl_dentry[fc->ctl_ndents++] = dentry;
  175. inode = new_inode(fuse_control_sb);
  176. if (!inode)
  177. return NULL;
  178. inode->i_ino = get_next_ino();
  179. inode->i_mode = mode;
  180. inode->i_uid = fc->user_id;
  181. inode->i_gid = fc->group_id;
  182. inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
  183. /* setting ->i_op to NULL is not allowed */
  184. if (iop)
  185. inode->i_op = iop;
  186. inode->i_fop = fop;
  187. set_nlink(inode, nlink);
  188. inode->i_private = fc;
  189. d_add(dentry, inode);
  190. return dentry;
  191. }
  192. /*
  193. * Add a connection to the control filesystem (if it exists). Caller
  194. * must hold fuse_mutex
  195. */
  196. int fuse_ctl_add_conn(struct fuse_conn *fc)
  197. {
  198. struct dentry *parent;
  199. char name[32];
  200. if (!fuse_control_sb)
  201. return 0;
  202. parent = fuse_control_sb->s_root;
  203. inc_nlink(parent->d_inode);
  204. sprintf(name, "%u", fc->dev);
  205. parent = fuse_ctl_add_dentry(parent, fc, name, S_IFDIR | 0500, 2,
  206. &simple_dir_inode_operations,
  207. &simple_dir_operations);
  208. if (!parent)
  209. goto err;
  210. if (!fuse_ctl_add_dentry(parent, fc, "waiting", S_IFREG | 0400, 1,
  211. NULL, &fuse_ctl_waiting_ops) ||
  212. !fuse_ctl_add_dentry(parent, fc, "abort", S_IFREG | 0200, 1,
  213. NULL, &fuse_ctl_abort_ops) ||
  214. !fuse_ctl_add_dentry(parent, fc, "max_background", S_IFREG | 0600,
  215. 1, NULL, &fuse_conn_max_background_ops) ||
  216. !fuse_ctl_add_dentry(parent, fc, "congestion_threshold",
  217. S_IFREG | 0600, 1, NULL,
  218. &fuse_conn_congestion_threshold_ops))
  219. goto err;
  220. return 0;
  221. err:
  222. fuse_ctl_remove_conn(fc);
  223. return -ENOMEM;
  224. }
  225. /*
  226. * Remove a connection from the control filesystem (if it exists).
  227. * Caller must hold fuse_mutex
  228. */
  229. void fuse_ctl_remove_conn(struct fuse_conn *fc)
  230. {
  231. int i;
  232. if (!fuse_control_sb)
  233. return;
  234. for (i = fc->ctl_ndents - 1; i >= 0; i--) {
  235. struct dentry *dentry = fc->ctl_dentry[i];
  236. dentry->d_inode->i_private = NULL;
  237. d_drop(dentry);
  238. dput(dentry);
  239. }
  240. drop_nlink(fuse_control_sb->s_root->d_inode);
  241. }
  242. static int fuse_ctl_fill_super(struct super_block *sb, void *data, int silent)
  243. {
  244. struct tree_descr empty_descr = {""};
  245. struct fuse_conn *fc;
  246. int err;
  247. err = simple_fill_super(sb, FUSE_CTL_SUPER_MAGIC, &empty_descr);
  248. if (err)
  249. return err;
  250. mutex_lock(&fuse_mutex);
  251. BUG_ON(fuse_control_sb);
  252. fuse_control_sb = sb;
  253. list_for_each_entry(fc, &fuse_conn_list, entry) {
  254. err = fuse_ctl_add_conn(fc);
  255. if (err) {
  256. fuse_control_sb = NULL;
  257. mutex_unlock(&fuse_mutex);
  258. return err;
  259. }
  260. }
  261. mutex_unlock(&fuse_mutex);
  262. return 0;
  263. }
  264. static struct dentry *fuse_ctl_mount(struct file_system_type *fs_type,
  265. int flags, const char *dev_name, void *raw_data)
  266. {
  267. return mount_single(fs_type, flags, raw_data, fuse_ctl_fill_super);
  268. }
  269. static void fuse_ctl_kill_sb(struct super_block *sb)
  270. {
  271. struct fuse_conn *fc;
  272. mutex_lock(&fuse_mutex);
  273. fuse_control_sb = NULL;
  274. list_for_each_entry(fc, &fuse_conn_list, entry)
  275. fc->ctl_ndents = 0;
  276. mutex_unlock(&fuse_mutex);
  277. kill_litter_super(sb);
  278. }
  279. static struct file_system_type fuse_ctl_fs_type = {
  280. .owner = THIS_MODULE,
  281. .name = "fusectl",
  282. .mount = fuse_ctl_mount,
  283. .kill_sb = fuse_ctl_kill_sb,
  284. };
  285. MODULE_ALIAS_FS("fusectl");
  286. int __init fuse_ctl_init(void)
  287. {
  288. return register_filesystem(&fuse_ctl_fs_type);
  289. }
  290. void fuse_ctl_cleanup(void)
  291. {
  292. unregister_filesystem(&fuse_ctl_fs_type);
  293. }