control.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  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. char tmp[32];
  66. unsigned limit = (1 << 16) - 1;
  67. int err;
  68. if (*ppos || count >= sizeof(tmp) - 1)
  69. return -EINVAL;
  70. if (copy_from_user(tmp, buf, count))
  71. return -EINVAL;
  72. tmp[count] = '\0';
  73. err = strict_strtoul(tmp, 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 = 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. fc->max_background = val;
  108. fuse_conn_put(fc);
  109. }
  110. }
  111. return ret;
  112. }
  113. static ssize_t fuse_conn_congestion_threshold_read(struct file *file,
  114. char __user *buf, size_t len,
  115. loff_t *ppos)
  116. {
  117. struct fuse_conn *fc;
  118. unsigned val;
  119. fc = fuse_ctl_file_conn_get(file);
  120. if (!fc)
  121. return 0;
  122. val = fc->congestion_threshold;
  123. fuse_conn_put(fc);
  124. return fuse_conn_limit_read(file, buf, len, ppos, val);
  125. }
  126. static ssize_t fuse_conn_congestion_threshold_write(struct file *file,
  127. const char __user *buf,
  128. size_t count, loff_t *ppos)
  129. {
  130. unsigned val;
  131. ssize_t ret;
  132. ret = fuse_conn_limit_write(file, buf, count, ppos, &val,
  133. max_user_congthresh);
  134. if (ret > 0) {
  135. struct fuse_conn *fc = fuse_ctl_file_conn_get(file);
  136. if (fc) {
  137. fc->congestion_threshold = val;
  138. fuse_conn_put(fc);
  139. }
  140. }
  141. return ret;
  142. }
  143. static const struct file_operations fuse_ctl_abort_ops = {
  144. .open = nonseekable_open,
  145. .write = fuse_conn_abort_write,
  146. };
  147. static const struct file_operations fuse_ctl_waiting_ops = {
  148. .open = nonseekable_open,
  149. .read = fuse_conn_waiting_read,
  150. };
  151. static const struct file_operations fuse_conn_max_background_ops = {
  152. .open = nonseekable_open,
  153. .read = fuse_conn_max_background_read,
  154. .write = fuse_conn_max_background_write,
  155. };
  156. static const struct file_operations fuse_conn_congestion_threshold_ops = {
  157. .open = nonseekable_open,
  158. .read = fuse_conn_congestion_threshold_read,
  159. .write = fuse_conn_congestion_threshold_write,
  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_mode = mode;
  179. inode->i_uid = fc->user_id;
  180. inode->i_gid = fc->group_id;
  181. inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
  182. /* setting ->i_op to NULL is not allowed */
  183. if (iop)
  184. inode->i_op = iop;
  185. inode->i_fop = fop;
  186. inode->i_nlink = nlink;
  187. inode->i_private = fc;
  188. d_add(dentry, inode);
  189. return dentry;
  190. }
  191. /*
  192. * Add a connection to the control filesystem (if it exists). Caller
  193. * must hold fuse_mutex
  194. */
  195. int fuse_ctl_add_conn(struct fuse_conn *fc)
  196. {
  197. struct dentry *parent;
  198. char name[32];
  199. if (!fuse_control_sb)
  200. return 0;
  201. parent = fuse_control_sb->s_root;
  202. inc_nlink(parent->d_inode);
  203. sprintf(name, "%u", fc->dev);
  204. parent = fuse_ctl_add_dentry(parent, fc, name, S_IFDIR | 0500, 2,
  205. &simple_dir_inode_operations,
  206. &simple_dir_operations);
  207. if (!parent)
  208. goto err;
  209. if (!fuse_ctl_add_dentry(parent, fc, "waiting", S_IFREG | 0400, 1,
  210. NULL, &fuse_ctl_waiting_ops) ||
  211. !fuse_ctl_add_dentry(parent, fc, "abort", S_IFREG | 0200, 1,
  212. NULL, &fuse_ctl_abort_ops) ||
  213. !fuse_ctl_add_dentry(parent, fc, "max_background", S_IFREG | 0600,
  214. 1, NULL, &fuse_conn_max_background_ops) ||
  215. !fuse_ctl_add_dentry(parent, fc, "congestion_threshold",
  216. S_IFREG | 0600, 1, NULL,
  217. &fuse_conn_congestion_threshold_ops))
  218. goto err;
  219. return 0;
  220. err:
  221. fuse_ctl_remove_conn(fc);
  222. return -ENOMEM;
  223. }
  224. /*
  225. * Remove a connection from the control filesystem (if it exists).
  226. * Caller must hold fuse_mutex
  227. */
  228. void fuse_ctl_remove_conn(struct fuse_conn *fc)
  229. {
  230. int i;
  231. if (!fuse_control_sb)
  232. return;
  233. for (i = fc->ctl_ndents - 1; i >= 0; i--) {
  234. struct dentry *dentry = fc->ctl_dentry[i];
  235. dentry->d_inode->i_private = NULL;
  236. d_drop(dentry);
  237. dput(dentry);
  238. }
  239. drop_nlink(fuse_control_sb->s_root->d_inode);
  240. }
  241. static int fuse_ctl_fill_super(struct super_block *sb, void *data, int silent)
  242. {
  243. struct tree_descr empty_descr = {""};
  244. struct fuse_conn *fc;
  245. int err;
  246. err = simple_fill_super(sb, FUSE_CTL_SUPER_MAGIC, &empty_descr);
  247. if (err)
  248. return err;
  249. mutex_lock(&fuse_mutex);
  250. BUG_ON(fuse_control_sb);
  251. fuse_control_sb = sb;
  252. list_for_each_entry(fc, &fuse_conn_list, entry) {
  253. err = fuse_ctl_add_conn(fc);
  254. if (err) {
  255. fuse_control_sb = NULL;
  256. mutex_unlock(&fuse_mutex);
  257. return err;
  258. }
  259. }
  260. mutex_unlock(&fuse_mutex);
  261. return 0;
  262. }
  263. static int fuse_ctl_get_sb(struct file_system_type *fs_type, int flags,
  264. const char *dev_name, void *raw_data,
  265. struct vfsmount *mnt)
  266. {
  267. return get_sb_single(fs_type, flags, raw_data,
  268. fuse_ctl_fill_super, mnt);
  269. }
  270. static void fuse_ctl_kill_sb(struct super_block *sb)
  271. {
  272. struct fuse_conn *fc;
  273. mutex_lock(&fuse_mutex);
  274. fuse_control_sb = NULL;
  275. list_for_each_entry(fc, &fuse_conn_list, entry)
  276. fc->ctl_ndents = 0;
  277. mutex_unlock(&fuse_mutex);
  278. kill_litter_super(sb);
  279. }
  280. static struct file_system_type fuse_ctl_fs_type = {
  281. .owner = THIS_MODULE,
  282. .name = "fusectl",
  283. .get_sb = fuse_ctl_get_sb,
  284. .kill_sb = fuse_ctl_kill_sb,
  285. };
  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. }