control.c 7.9 KB

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