Blame view

fs/fuse/control.c 7.92 KB
bafa96541   Miklos Szeredi   [PATCH] fuse: add...
1
2
  /*
    FUSE: Filesystem in Userspace
1729a16c2   Miklos Szeredi   fuse: style fixes
3
    Copyright (C) 2001-2008  Miklos Szeredi <miklos@szeredi.hu>
bafa96541   Miklos Szeredi   [PATCH] fuse: add...
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
  
    This program can be distributed under the terms of the GNU GPL.
    See the file COPYING.
  */
  
  #include "fuse_i.h"
  
  #include <linux/init.h>
  #include <linux/module.h>
  
  #define FUSE_CTL_SUPER_MAGIC 0x65735543
  
  /*
   * This is non-NULL when the single instance of the control filesystem
   * exists.  Protected by fuse_mutex
   */
  static struct super_block *fuse_control_sb;
  
  static struct fuse_conn *fuse_ctl_file_conn_get(struct file *file)
  {
  	struct fuse_conn *fc;
  	mutex_lock(&fuse_mutex);
7706a9d61   Josef Sipek   [PATCH] struct pa...
26
  	fc = file->f_path.dentry->d_inode->i_private;
bafa96541   Miklos Szeredi   [PATCH] fuse: add...
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
  	if (fc)
  		fc = fuse_conn_get(fc);
  	mutex_unlock(&fuse_mutex);
  	return fc;
  }
  
  static ssize_t fuse_conn_abort_write(struct file *file, const char __user *buf,
  				     size_t count, loff_t *ppos)
  {
  	struct fuse_conn *fc = fuse_ctl_file_conn_get(file);
  	if (fc) {
  		fuse_abort_conn(fc);
  		fuse_conn_put(fc);
  	}
  	return count;
  }
  
  static ssize_t fuse_conn_waiting_read(struct file *file, char __user *buf,
  				      size_t len, loff_t *ppos)
  {
  	char tmp[32];
  	size_t size;
  
  	if (!*ppos) {
1729a16c2   Miklos Szeredi   fuse: style fixes
51
  		long value;
bafa96541   Miklos Szeredi   [PATCH] fuse: add...
52
53
54
  		struct fuse_conn *fc = fuse_ctl_file_conn_get(file);
  		if (!fc)
  			return 0;
1729a16c2   Miklos Szeredi   fuse: style fixes
55
56
  		value = atomic_read(&fc->num_waiting);
  		file->private_data = (void *)value;
bafa96541   Miklos Szeredi   [PATCH] fuse: add...
57
58
59
60
61
62
  		fuse_conn_put(fc);
  	}
  	size = sprintf(tmp, "%ld
  ", (long)file->private_data);
  	return simple_read_from_buffer(buf, len, ppos, tmp, size);
  }
79a9d9943   Csaba Henk   fuse: add fusectl...
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
  static ssize_t fuse_conn_limit_read(struct file *file, char __user *buf,
  				    size_t len, loff_t *ppos, unsigned val)
  {
  	char tmp[32];
  	size_t size = sprintf(tmp, "%u
  ", val);
  
  	return simple_read_from_buffer(buf, len, ppos, tmp, size);
  }
  
  static ssize_t fuse_conn_limit_write(struct file *file, const char __user *buf,
  				     size_t count, loff_t *ppos, unsigned *val,
  				     unsigned global_limit)
  {
  	unsigned long t;
  	char tmp[32];
  	unsigned limit = (1 << 16) - 1;
  	int err;
  
  	if (*ppos || count >= sizeof(tmp) - 1)
  		return -EINVAL;
  
  	if (copy_from_user(tmp, buf, count))
  		return -EINVAL;
  
  	tmp[count] = '\0';
  
  	err = strict_strtoul(tmp, 0, &t);
  	if (err)
  		return err;
  
  	if (!capable(CAP_SYS_ADMIN))
  		limit = min(limit, global_limit);
  
  	if (t > limit)
  		return -EINVAL;
  
  	*val = t;
  
  	return count;
  }
  
  static ssize_t fuse_conn_max_background_read(struct file *file,
  					     char __user *buf, size_t len,
  					     loff_t *ppos)
  {
  	struct fuse_conn *fc;
  	unsigned val;
  
  	fc = fuse_ctl_file_conn_get(file);
  	if (!fc)
  		return 0;
  
  	val = fc->max_background;
  	fuse_conn_put(fc);
  
  	return fuse_conn_limit_read(file, buf, len, ppos, val);
  }
  
  static ssize_t fuse_conn_max_background_write(struct file *file,
  					      const char __user *buf,
  					      size_t count, loff_t *ppos)
  {
  	unsigned val;
  	ssize_t ret;
  
  	ret = fuse_conn_limit_write(file, buf, count, ppos, &val,
  				    max_user_bgreq);
  	if (ret > 0) {
  		struct fuse_conn *fc = fuse_ctl_file_conn_get(file);
  		if (fc) {
  			fc->max_background = val;
  			fuse_conn_put(fc);
  		}
  	}
  
  	return ret;
  }
  
  static ssize_t fuse_conn_congestion_threshold_read(struct file *file,
  						   char __user *buf, size_t len,
  						   loff_t *ppos)
  {
  	struct fuse_conn *fc;
  	unsigned val;
  
  	fc = fuse_ctl_file_conn_get(file);
  	if (!fc)
  		return 0;
  
  	val = fc->congestion_threshold;
  	fuse_conn_put(fc);
  
  	return fuse_conn_limit_read(file, buf, len, ppos, val);
  }
  
  static ssize_t fuse_conn_congestion_threshold_write(struct file *file,
  						    const char __user *buf,
  						    size_t count, loff_t *ppos)
  {
  	unsigned val;
  	ssize_t ret;
  
  	ret = fuse_conn_limit_write(file, buf, count, ppos, &val,
  				    max_user_congthresh);
  	if (ret > 0) {
  		struct fuse_conn *fc = fuse_ctl_file_conn_get(file);
  		if (fc) {
  			fc->congestion_threshold = val;
  			fuse_conn_put(fc);
  		}
  	}
  
  	return ret;
  }
bafa96541   Miklos Szeredi   [PATCH] fuse: add...
178
179
180
  static const struct file_operations fuse_ctl_abort_ops = {
  	.open = nonseekable_open,
  	.write = fuse_conn_abort_write,
6038f373a   Arnd Bergmann   llseek: automatic...
181
  	.llseek = no_llseek,
bafa96541   Miklos Szeredi   [PATCH] fuse: add...
182
183
184
185
186
  };
  
  static const struct file_operations fuse_ctl_waiting_ops = {
  	.open = nonseekable_open,
  	.read = fuse_conn_waiting_read,
6038f373a   Arnd Bergmann   llseek: automatic...
187
  	.llseek = no_llseek,
bafa96541   Miklos Szeredi   [PATCH] fuse: add...
188
  };
79a9d9943   Csaba Henk   fuse: add fusectl...
189
190
191
192
  static const struct file_operations fuse_conn_max_background_ops = {
  	.open = nonseekable_open,
  	.read = fuse_conn_max_background_read,
  	.write = fuse_conn_max_background_write,
6038f373a   Arnd Bergmann   llseek: automatic...
193
  	.llseek = no_llseek,
79a9d9943   Csaba Henk   fuse: add fusectl...
194
195
196
197
198
199
  };
  
  static const struct file_operations fuse_conn_congestion_threshold_ops = {
  	.open = nonseekable_open,
  	.read = fuse_conn_congestion_threshold_read,
  	.write = fuse_conn_congestion_threshold_write,
6038f373a   Arnd Bergmann   llseek: automatic...
200
  	.llseek = no_llseek,
79a9d9943   Csaba Henk   fuse: add fusectl...
201
  };
bafa96541   Miklos Szeredi   [PATCH] fuse: add...
202
203
204
205
  static struct dentry *fuse_ctl_add_dentry(struct dentry *parent,
  					  struct fuse_conn *fc,
  					  const char *name,
  					  int mode, int nlink,
754661f14   Arjan van de Ven   [PATCH] mark stru...
206
  					  const struct inode_operations *iop,
bafa96541   Miklos Szeredi   [PATCH] fuse: add...
207
208
209
210
211
212
213
214
215
216
217
218
219
220
  					  const struct file_operations *fop)
  {
  	struct dentry *dentry;
  	struct inode *inode;
  
  	BUG_ON(fc->ctl_ndents >= FUSE_CTL_NUM_DENTRIES);
  	dentry = d_alloc_name(parent, name);
  	if (!dentry)
  		return NULL;
  
  	fc->ctl_dentry[fc->ctl_ndents++] = dentry;
  	inode = new_inode(fuse_control_sb);
  	if (!inode)
  		return NULL;
85fe4025c   Christoph Hellwig   fs: do not assign...
221
  	inode->i_ino = get_next_ino();
bafa96541   Miklos Szeredi   [PATCH] fuse: add...
222
223
224
225
226
227
228
229
230
  	inode->i_mode = mode;
  	inode->i_uid = fc->user_id;
  	inode->i_gid = fc->group_id;
  	inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
  	/* setting ->i_op to NULL is not allowed */
  	if (iop)
  		inode->i_op = iop;
  	inode->i_fop = fop;
  	inode->i_nlink = nlink;
8e18e2941   Theodore Ts'o   [PATCH] inode_die...
231
  	inode->i_private = fc;
bafa96541   Miklos Szeredi   [PATCH] fuse: add...
232
233
234
235
236
237
  	d_add(dentry, inode);
  	return dentry;
  }
  
  /*
   * Add a connection to the control filesystem (if it exists).  Caller
873302c71   Miklos Szeredi   [PATCH] fuse: fix...
238
   * must hold fuse_mutex
bafa96541   Miklos Szeredi   [PATCH] fuse: add...
239
240
241
242
243
244
245
246
247
248
   */
  int fuse_ctl_add_conn(struct fuse_conn *fc)
  {
  	struct dentry *parent;
  	char name[32];
  
  	if (!fuse_control_sb)
  		return 0;
  
  	parent = fuse_control_sb->s_root;
d8c76e6f4   Dave Hansen   [PATCH] r/o bind ...
249
  	inc_nlink(parent->d_inode);
b6f2fcbcf   Miklos Szeredi   mm: bdi: expose t...
250
  	sprintf(name, "%u", fc->dev);
bafa96541   Miklos Szeredi   [PATCH] fuse: add...
251
252
253
254
255
256
257
  	parent = fuse_ctl_add_dentry(parent, fc, name, S_IFDIR | 0500, 2,
  				     &simple_dir_inode_operations,
  				     &simple_dir_operations);
  	if (!parent)
  		goto err;
  
  	if (!fuse_ctl_add_dentry(parent, fc, "waiting", S_IFREG | 0400, 1,
79a9d9943   Csaba Henk   fuse: add fusectl...
258
  				 NULL, &fuse_ctl_waiting_ops) ||
bafa96541   Miklos Szeredi   [PATCH] fuse: add...
259
  	    !fuse_ctl_add_dentry(parent, fc, "abort", S_IFREG | 0200, 1,
79a9d9943   Csaba Henk   fuse: add fusectl...
260
261
262
263
264
265
  				 NULL, &fuse_ctl_abort_ops) ||
  	    !fuse_ctl_add_dentry(parent, fc, "max_background", S_IFREG | 0600,
  				 1, NULL, &fuse_conn_max_background_ops) ||
  	    !fuse_ctl_add_dentry(parent, fc, "congestion_threshold",
  				 S_IFREG | 0600, 1, NULL,
  				 &fuse_conn_congestion_threshold_ops))
bafa96541   Miklos Szeredi   [PATCH] fuse: add...
266
267
268
269
270
271
272
273
274
275
276
  		goto err;
  
  	return 0;
  
   err:
  	fuse_ctl_remove_conn(fc);
  	return -ENOMEM;
  }
  
  /*
   * Remove a connection from the control filesystem (if it exists).
873302c71   Miklos Szeredi   [PATCH] fuse: fix...
277
   * Caller must hold fuse_mutex
bafa96541   Miklos Szeredi   [PATCH] fuse: add...
278
279
280
281
282
283
284
285
286
287
   */
  void fuse_ctl_remove_conn(struct fuse_conn *fc)
  {
  	int i;
  
  	if (!fuse_control_sb)
  		return;
  
  	for (i = fc->ctl_ndents - 1; i >= 0; i--) {
  		struct dentry *dentry = fc->ctl_dentry[i];
8e18e2941   Theodore Ts'o   [PATCH] inode_die...
288
  		dentry->d_inode->i_private = NULL;
bafa96541   Miklos Szeredi   [PATCH] fuse: add...
289
290
291
  		d_drop(dentry);
  		dput(dentry);
  	}
d6db07ded   Csaba Henk   fuse: use drop_nl...
292
  	drop_nlink(fuse_control_sb->s_root->d_inode);
bafa96541   Miklos Szeredi   [PATCH] fuse: add...
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
  }
  
  static int fuse_ctl_fill_super(struct super_block *sb, void *data, int silent)
  {
  	struct tree_descr empty_descr = {""};
  	struct fuse_conn *fc;
  	int err;
  
  	err = simple_fill_super(sb, FUSE_CTL_SUPER_MAGIC, &empty_descr);
  	if (err)
  		return err;
  
  	mutex_lock(&fuse_mutex);
  	BUG_ON(fuse_control_sb);
  	fuse_control_sb = sb;
  	list_for_each_entry(fc, &fuse_conn_list, entry) {
  		err = fuse_ctl_add_conn(fc);
  		if (err) {
  			fuse_control_sb = NULL;
  			mutex_unlock(&fuse_mutex);
  			return err;
  		}
  	}
  	mutex_unlock(&fuse_mutex);
  
  	return 0;
  }
fc14f2fef   Al Viro   convert get_sb_si...
320
321
  static struct dentry *fuse_ctl_mount(struct file_system_type *fs_type,
  			int flags, const char *dev_name, void *raw_data)
bafa96541   Miklos Szeredi   [PATCH] fuse: add...
322
  {
fc14f2fef   Al Viro   convert get_sb_si...
323
  	return mount_single(fs_type, flags, raw_data, fuse_ctl_fill_super);
bafa96541   Miklos Szeredi   [PATCH] fuse: add...
324
325
326
327
  }
  
  static void fuse_ctl_kill_sb(struct super_block *sb)
  {
ff7954475   Miklos Szeredi   [PATCH] fuse: fix...
328
  	struct fuse_conn *fc;
bafa96541   Miklos Szeredi   [PATCH] fuse: add...
329
330
  	mutex_lock(&fuse_mutex);
  	fuse_control_sb = NULL;
ff7954475   Miklos Szeredi   [PATCH] fuse: fix...
331
332
  	list_for_each_entry(fc, &fuse_conn_list, entry)
  		fc->ctl_ndents = 0;
bafa96541   Miklos Szeredi   [PATCH] fuse: add...
333
334
335
336
337
338
339
340
  	mutex_unlock(&fuse_mutex);
  
  	kill_litter_super(sb);
  }
  
  static struct file_system_type fuse_ctl_fs_type = {
  	.owner		= THIS_MODULE,
  	.name		= "fusectl",
fc14f2fef   Al Viro   convert get_sb_si...
341
  	.mount		= fuse_ctl_mount,
bafa96541   Miklos Szeredi   [PATCH] fuse: add...
342
343
344
345
346
347
348
349
350
351
352
353
  	.kill_sb	= fuse_ctl_kill_sb,
  };
  
  int __init fuse_ctl_init(void)
  {
  	return register_filesystem(&fuse_ctl_fs_type);
  }
  
  void fuse_ctl_cleanup(void)
  {
  	unregister_filesystem(&fuse_ctl_fs_type);
  }