fuse: make the number of max background requests and congestion threshold tunable
[deliverable/linux.git] / fs / fuse / inode.c
CommitLineData
d8a5ba45
MS
1/*
2 FUSE: Filesystem in Userspace
1729a16c 3 Copyright (C) 2001-2008 Miklos Szeredi <miklos@szeredi.hu>
d8a5ba45
MS
4
5 This program can be distributed under the terms of the GNU GPL.
6 See the file COPYING.
7*/
8
9#include "fuse_i.h"
10
11#include <linux/pagemap.h>
12#include <linux/slab.h>
13#include <linux/file.h>
d8a5ba45
MS
14#include <linux/seq_file.h>
15#include <linux/init.h>
16#include <linux/module.h>
d8a5ba45
MS
17#include <linux/parser.h>
18#include <linux/statfs.h>
9c8ef561 19#include <linux/random.h>
e8edc6e0 20#include <linux/sched.h>
dbd561d2 21#include <linux/exportfs.h>
d8a5ba45
MS
22
23MODULE_AUTHOR("Miklos Szeredi <miklos@szeredi.hu>");
24MODULE_DESCRIPTION("Filesystem in Userspace");
25MODULE_LICENSE("GPL");
26
e18b890b 27static struct kmem_cache *fuse_inode_cachep;
bafa9654
MS
28struct list_head fuse_conn_list;
29DEFINE_MUTEX(fuse_mutex);
d8a5ba45
MS
30
31#define FUSE_SUPER_MAGIC 0x65735546
32
d1875dba
MS
33#define FUSE_DEFAULT_BLKSIZE 512
34
7a6d3c8b
CH
35/** Maximum number of outstanding background requests */
36#define FUSE_DEFAULT_MAX_BACKGROUND 12
37
38/** Congestion starts at 75% of maximum */
39#define FUSE_DEFAULT_CONGESTION_THRESHOLD (FUSE_DEFAULT_MAX_BACKGROUND * 3 / 4)
40
d8a5ba45
MS
41struct fuse_mount_data {
42 int fd;
43 unsigned rootmode;
44 unsigned user_id;
87729a55 45 unsigned group_id;
1729a16c
MS
46 unsigned fd_present:1;
47 unsigned rootmode_present:1;
48 unsigned user_id_present:1;
49 unsigned group_id_present:1;
1e9a4ed9 50 unsigned flags;
db50b96c 51 unsigned max_read;
d8091614 52 unsigned blksize;
d8a5ba45
MS
53};
54
55static struct inode *fuse_alloc_inode(struct super_block *sb)
56{
57 struct inode *inode;
58 struct fuse_inode *fi;
59
e94b1766 60 inode = kmem_cache_alloc(fuse_inode_cachep, GFP_KERNEL);
d8a5ba45
MS
61 if (!inode)
62 return NULL;
63
64 fi = get_fuse_inode(inode);
0a0898cf 65 fi->i_time = 0;
d8a5ba45 66 fi->nodeid = 0;
9e6268db 67 fi->nlookup = 0;
fbee36b9 68 fi->attr_version = 0;
3be5a52b 69 fi->writectr = 0;
93a8c3cd 70 INIT_LIST_HEAD(&fi->write_files);
3be5a52b
MS
71 INIT_LIST_HEAD(&fi->queued_writes);
72 INIT_LIST_HEAD(&fi->writepages);
73 init_waitqueue_head(&fi->page_waitq);
e5e5558e
MS
74 fi->forget_req = fuse_request_alloc();
75 if (!fi->forget_req) {
76 kmem_cache_free(fuse_inode_cachep, inode);
77 return NULL;
78 }
d8a5ba45
MS
79
80 return inode;
81}
82
83static void fuse_destroy_inode(struct inode *inode)
84{
e5e5558e 85 struct fuse_inode *fi = get_fuse_inode(inode);
93a8c3cd 86 BUG_ON(!list_empty(&fi->write_files));
3be5a52b 87 BUG_ON(!list_empty(&fi->queued_writes));
e5e5558e
MS
88 if (fi->forget_req)
89 fuse_request_free(fi->forget_req);
d8a5ba45
MS
90 kmem_cache_free(fuse_inode_cachep, inode);
91}
92
e5e5558e 93void fuse_send_forget(struct fuse_conn *fc, struct fuse_req *req,
b48badf0 94 u64 nodeid, u64 nlookup)
e5e5558e
MS
95{
96 struct fuse_forget_in *inarg = &req->misc.forget_in;
9e6268db 97 inarg->nlookup = nlookup;
e5e5558e
MS
98 req->in.h.opcode = FUSE_FORGET;
99 req->in.h.nodeid = nodeid;
100 req->in.numargs = 1;
101 req->in.args[0].size = sizeof(struct fuse_forget_in);
102 req->in.args[0].value = inarg;
b93f858a 103 fuse_request_send_noreply(fc, req);
e5e5558e
MS
104}
105
d8a5ba45
MS
106static void fuse_clear_inode(struct inode *inode)
107{
1e9a4ed9
MS
108 if (inode->i_sb->s_flags & MS_ACTIVE) {
109 struct fuse_conn *fc = get_fuse_conn(inode);
e5e5558e 110 struct fuse_inode *fi = get_fuse_inode(inode);
9e6268db 111 fuse_send_forget(fc, fi->forget_req, fi->nodeid, fi->nlookup);
e5e5558e
MS
112 fi->forget_req = NULL;
113 }
d8a5ba45
MS
114}
115
71421259
MS
116static int fuse_remount_fs(struct super_block *sb, int *flags, char *data)
117{
118 if (*flags & MS_MANDLOCK)
119 return -EINVAL;
120
121 return 0;
122}
123
3be5a52b 124void fuse_truncate(struct address_space *mapping, loff_t offset)
e00d2c2d
MS
125{
126 /* See vmtruncate() */
127 unmap_mapping_range(mapping, offset + PAGE_SIZE - 1, 0, 1);
128 truncate_inode_pages(mapping, offset);
129 unmap_mapping_range(mapping, offset + PAGE_SIZE - 1, 0, 1);
130}
131
3be5a52b
MS
132void fuse_change_attributes_common(struct inode *inode, struct fuse_attr *attr,
133 u64 attr_valid)
d8a5ba45 134{
9ffbb916 135 struct fuse_conn *fc = get_fuse_conn(inode);
ebc14c4d 136 struct fuse_inode *fi = get_fuse_inode(inode);
d8a5ba45 137
1fb69e78
MS
138 fi->attr_version = ++fc->attr_version;
139 fi->i_time = attr_valid;
140
d8a5ba45 141 inode->i_ino = attr->ino;
ebc14c4d 142 inode->i_mode = (inode->i_mode & S_IFMT) | (attr->mode & 07777);
d8a5ba45
MS
143 inode->i_nlink = attr->nlink;
144 inode->i_uid = attr->uid;
145 inode->i_gid = attr->gid;
d8a5ba45
MS
146 inode->i_blocks = attr->blocks;
147 inode->i_atime.tv_sec = attr->atime;
148 inode->i_atime.tv_nsec = attr->atimensec;
149 inode->i_mtime.tv_sec = attr->mtime;
150 inode->i_mtime.tv_nsec = attr->mtimensec;
151 inode->i_ctime.tv_sec = attr->ctime;
152 inode->i_ctime.tv_nsec = attr->ctimensec;
e00d2c2d 153
0e9663ee
MS
154 if (attr->blksize != 0)
155 inode->i_blkbits = ilog2(attr->blksize);
156 else
157 inode->i_blkbits = inode->i_sb->s_blocksize_bits;
158
ebc14c4d
MS
159 /*
160 * Don't set the sticky bit in i_mode, unless we want the VFS
161 * to check permissions. This prevents failures due to the
162 * check in may_delete().
163 */
164 fi->orig_i_mode = inode->i_mode;
165 if (!(fc->flags & FUSE_DEFAULT_PERMISSIONS))
166 inode->i_mode &= ~S_ISVTX;
3be5a52b
MS
167}
168
169void fuse_change_attributes(struct inode *inode, struct fuse_attr *attr,
170 u64 attr_valid, u64 attr_version)
171{
172 struct fuse_conn *fc = get_fuse_conn(inode);
173 struct fuse_inode *fi = get_fuse_inode(inode);
174 loff_t oldsize;
175
176 spin_lock(&fc->lock);
177 if (attr_version != 0 && fi->attr_version > attr_version) {
178 spin_unlock(&fc->lock);
179 return;
180 }
181
182 fuse_change_attributes_common(inode, attr, attr_valid);
ebc14c4d 183
e00d2c2d
MS
184 oldsize = inode->i_size;
185 i_size_write(inode, attr->size);
186 spin_unlock(&fc->lock);
187
188 if (S_ISREG(inode->i_mode) && oldsize != attr->size) {
189 if (attr->size < oldsize)
190 fuse_truncate(inode->i_mapping, attr->size);
b1009979 191 invalidate_inode_pages2(inode->i_mapping);
e00d2c2d 192 }
d8a5ba45
MS
193}
194
195static void fuse_init_inode(struct inode *inode, struct fuse_attr *attr)
196{
197 inode->i_mode = attr->mode & S_IFMT;
9ffbb916 198 inode->i_size = attr->size;
e5e5558e
MS
199 if (S_ISREG(inode->i_mode)) {
200 fuse_init_common(inode);
b6aeaded 201 fuse_init_file_inode(inode);
e5e5558e
MS
202 } else if (S_ISDIR(inode->i_mode))
203 fuse_init_dir(inode);
204 else if (S_ISLNK(inode->i_mode))
205 fuse_init_symlink(inode);
206 else if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode) ||
207 S_ISFIFO(inode->i_mode) || S_ISSOCK(inode->i_mode)) {
208 fuse_init_common(inode);
209 init_special_inode(inode, inode->i_mode,
210 new_decode_dev(attr->rdev));
39ee059a
MS
211 } else
212 BUG();
d8a5ba45
MS
213}
214
3b463ae0 215int fuse_inode_eq(struct inode *inode, void *_nodeidp)
d8a5ba45 216{
b48badf0 217 u64 nodeid = *(u64 *) _nodeidp;
d8a5ba45
MS
218 if (get_node_id(inode) == nodeid)
219 return 1;
220 else
221 return 0;
222}
223
224static int fuse_inode_set(struct inode *inode, void *_nodeidp)
225{
b48badf0 226 u64 nodeid = *(u64 *) _nodeidp;
d8a5ba45
MS
227 get_fuse_inode(inode)->nodeid = nodeid;
228 return 0;
229}
230
b48badf0 231struct inode *fuse_iget(struct super_block *sb, u64 nodeid,
1fb69e78
MS
232 int generation, struct fuse_attr *attr,
233 u64 attr_valid, u64 attr_version)
d8a5ba45
MS
234{
235 struct inode *inode;
9e6268db 236 struct fuse_inode *fi;
d8a5ba45 237 struct fuse_conn *fc = get_fuse_conn_super(sb);
d8a5ba45
MS
238
239 retry:
240 inode = iget5_locked(sb, nodeid, fuse_inode_eq, fuse_inode_set, &nodeid);
241 if (!inode)
242 return NULL;
243
244 if ((inode->i_state & I_NEW)) {
b36c31ba 245 inode->i_flags |= S_NOATIME|S_NOCMTIME;
d8a5ba45
MS
246 inode->i_generation = generation;
247 inode->i_data.backing_dev_info = &fc->bdi;
248 fuse_init_inode(inode, attr);
249 unlock_new_inode(inode);
250 } else if ((inode->i_mode ^ attr->mode) & S_IFMT) {
d8a5ba45
MS
251 /* Inode has changed type, any I/O on the old should fail */
252 make_bad_inode(inode);
253 iput(inode);
d8a5ba45
MS
254 goto retry;
255 }
256
9e6268db 257 fi = get_fuse_inode(inode);
8da5ff23 258 spin_lock(&fc->lock);
1729a16c 259 fi->nlookup++;
8da5ff23 260 spin_unlock(&fc->lock);
1fb69e78
MS
261 fuse_change_attributes(inode, attr, attr_valid, attr_version);
262
d8a5ba45
MS
263 return inode;
264}
265
3b463ae0
JM
266int fuse_reverse_inval_inode(struct super_block *sb, u64 nodeid,
267 loff_t offset, loff_t len)
268{
269 struct inode *inode;
270 pgoff_t pg_start;
271 pgoff_t pg_end;
272
273 inode = ilookup5(sb, nodeid, fuse_inode_eq, &nodeid);
274 if (!inode)
275 return -ENOENT;
276
277 fuse_invalidate_attr(inode);
278 if (offset >= 0) {
279 pg_start = offset >> PAGE_CACHE_SHIFT;
280 if (len <= 0)
281 pg_end = -1;
282 else
283 pg_end = (offset + len - 1) >> PAGE_CACHE_SHIFT;
284 invalidate_inode_pages2_range(inode->i_mapping,
285 pg_start, pg_end);
286 }
287 iput(inode);
288 return 0;
289}
290
42faad99 291static void fuse_umount_begin(struct super_block *sb)
69a53bf2 292{
42faad99 293 fuse_abort_conn(get_fuse_conn_super(sb));
69a53bf2
MS
294}
295
0ec7ca41
MS
296static void fuse_send_destroy(struct fuse_conn *fc)
297{
298 struct fuse_req *req = fc->destroy_req;
299 if (req && fc->conn_init) {
300 fc->destroy_req = NULL;
301 req->in.h.opcode = FUSE_DESTROY;
302 req->force = 1;
b93f858a 303 fuse_request_send(fc, req);
0ec7ca41
MS
304 fuse_put_request(fc, req);
305 }
306}
307
a325f9b9 308static void fuse_bdi_destroy(struct fuse_conn *fc)
d8a5ba45 309{
a325f9b9
TH
310 if (fc->bdi_initialized)
311 bdi_destroy(&fc->bdi);
312}
d8a5ba45 313
08cbf542 314void fuse_conn_kill(struct fuse_conn *fc)
a325f9b9 315{
d7133114 316 spin_lock(&fc->lock);
9ba7cbba 317 fc->connected = 0;
51eb01e7 318 fc->blocked = 0;
d7133114 319 spin_unlock(&fc->lock);
334f485d 320 /* Flush all readers on this fs */
385a17bf 321 kill_fasync(&fc->fasync, SIGIO, POLL_IN);
334f485d 322 wake_up_all(&fc->waitq);
51eb01e7 323 wake_up_all(&fc->blocked_waitq);
de5e3dec 324 wake_up_all(&fc->reserved_req_waitq);
bafa9654
MS
325 mutex_lock(&fuse_mutex);
326 list_del(&fc->entry);
327 fuse_ctl_remove_conn(fc);
328 mutex_unlock(&fuse_mutex);
a325f9b9
TH
329 fuse_bdi_destroy(fc);
330}
08cbf542 331EXPORT_SYMBOL_GPL(fuse_conn_kill);
a325f9b9
TH
332
333static void fuse_put_super(struct super_block *sb)
334{
335 struct fuse_conn *fc = get_fuse_conn_super(sb);
336
337 fuse_send_destroy(fc);
338 fuse_conn_kill(fc);
bafa9654 339 fuse_conn_put(fc);
d8a5ba45
MS
340}
341
e5e5558e
MS
342static void convert_fuse_statfs(struct kstatfs *stbuf, struct fuse_kstatfs *attr)
343{
344 stbuf->f_type = FUSE_SUPER_MAGIC;
345 stbuf->f_bsize = attr->bsize;
de5f1202 346 stbuf->f_frsize = attr->frsize;
e5e5558e
MS
347 stbuf->f_blocks = attr->blocks;
348 stbuf->f_bfree = attr->bfree;
349 stbuf->f_bavail = attr->bavail;
350 stbuf->f_files = attr->files;
351 stbuf->f_ffree = attr->ffree;
352 stbuf->f_namelen = attr->namelen;
353 /* fsid is left zero */
354}
355
726c3342 356static int fuse_statfs(struct dentry *dentry, struct kstatfs *buf)
e5e5558e 357{
726c3342 358 struct super_block *sb = dentry->d_sb;
e5e5558e
MS
359 struct fuse_conn *fc = get_fuse_conn_super(sb);
360 struct fuse_req *req;
361 struct fuse_statfs_out outarg;
362 int err;
363
e57ac683
MS
364 if (!fuse_allow_task(fc, current)) {
365 buf->f_type = FUSE_SUPER_MAGIC;
366 return 0;
367 }
368
ce1d5a49
MS
369 req = fuse_get_req(fc);
370 if (IS_ERR(req))
371 return PTR_ERR(req);
e5e5558e 372
de5f1202 373 memset(&outarg, 0, sizeof(outarg));
e5e5558e
MS
374 req->in.numargs = 0;
375 req->in.h.opcode = FUSE_STATFS;
5b35e8e5 376 req->in.h.nodeid = get_node_id(dentry->d_inode);
e5e5558e 377 req->out.numargs = 1;
de5f1202
MS
378 req->out.args[0].size =
379 fc->minor < 4 ? FUSE_COMPAT_STATFS_SIZE : sizeof(outarg);
e5e5558e 380 req->out.args[0].value = &outarg;
b93f858a 381 fuse_request_send(fc, req);
e5e5558e
MS
382 err = req->out.h.error;
383 if (!err)
384 convert_fuse_statfs(buf, &outarg.st);
385 fuse_put_request(fc, req);
386 return err;
387}
388
d8a5ba45
MS
389enum {
390 OPT_FD,
391 OPT_ROOTMODE,
392 OPT_USER_ID,
87729a55 393 OPT_GROUP_ID,
d8a5ba45
MS
394 OPT_DEFAULT_PERMISSIONS,
395 OPT_ALLOW_OTHER,
db50b96c 396 OPT_MAX_READ,
d8091614 397 OPT_BLKSIZE,
d8a5ba45
MS
398 OPT_ERR
399};
400
a447c093 401static const match_table_t tokens = {
d8a5ba45
MS
402 {OPT_FD, "fd=%u"},
403 {OPT_ROOTMODE, "rootmode=%o"},
404 {OPT_USER_ID, "user_id=%u"},
87729a55 405 {OPT_GROUP_ID, "group_id=%u"},
d8a5ba45
MS
406 {OPT_DEFAULT_PERMISSIONS, "default_permissions"},
407 {OPT_ALLOW_OTHER, "allow_other"},
db50b96c 408 {OPT_MAX_READ, "max_read=%u"},
d8091614 409 {OPT_BLKSIZE, "blksize=%u"},
d8a5ba45
MS
410 {OPT_ERR, NULL}
411};
412
d8091614 413static int parse_fuse_opt(char *opt, struct fuse_mount_data *d, int is_bdev)
d8a5ba45
MS
414{
415 char *p;
416 memset(d, 0, sizeof(struct fuse_mount_data));
db50b96c 417 d->max_read = ~0;
d1875dba 418 d->blksize = FUSE_DEFAULT_BLKSIZE;
d8a5ba45
MS
419
420 while ((p = strsep(&opt, ",")) != NULL) {
421 int token;
422 int value;
423 substring_t args[MAX_OPT_ARGS];
424 if (!*p)
425 continue;
426
427 token = match_token(p, tokens, args);
428 switch (token) {
429 case OPT_FD:
430 if (match_int(&args[0], &value))
431 return 0;
432 d->fd = value;
5a533682 433 d->fd_present = 1;
d8a5ba45
MS
434 break;
435
436 case OPT_ROOTMODE:
437 if (match_octal(&args[0], &value))
438 return 0;
a5bfffac
TS
439 if (!fuse_valid_type(value))
440 return 0;
d8a5ba45 441 d->rootmode = value;
5a533682 442 d->rootmode_present = 1;
d8a5ba45
MS
443 break;
444
445 case OPT_USER_ID:
446 if (match_int(&args[0], &value))
447 return 0;
448 d->user_id = value;
5a533682 449 d->user_id_present = 1;
d8a5ba45
MS
450 break;
451
87729a55
MS
452 case OPT_GROUP_ID:
453 if (match_int(&args[0], &value))
454 return 0;
455 d->group_id = value;
5a533682 456 d->group_id_present = 1;
87729a55
MS
457 break;
458
1e9a4ed9
MS
459 case OPT_DEFAULT_PERMISSIONS:
460 d->flags |= FUSE_DEFAULT_PERMISSIONS;
461 break;
462
463 case OPT_ALLOW_OTHER:
464 d->flags |= FUSE_ALLOW_OTHER;
465 break;
466
db50b96c
MS
467 case OPT_MAX_READ:
468 if (match_int(&args[0], &value))
469 return 0;
470 d->max_read = value;
471 break;
472
d8091614
MS
473 case OPT_BLKSIZE:
474 if (!is_bdev || match_int(&args[0], &value))
475 return 0;
476 d->blksize = value;
477 break;
478
d8a5ba45
MS
479 default:
480 return 0;
481 }
482 }
5a533682
MS
483
484 if (!d->fd_present || !d->rootmode_present ||
485 !d->user_id_present || !d->group_id_present)
d8a5ba45
MS
486 return 0;
487
488 return 1;
489}
490
491static int fuse_show_options(struct seq_file *m, struct vfsmount *mnt)
492{
493 struct fuse_conn *fc = get_fuse_conn_super(mnt->mnt_sb);
494
495 seq_printf(m, ",user_id=%u", fc->user_id);
87729a55 496 seq_printf(m, ",group_id=%u", fc->group_id);
1e9a4ed9
MS
497 if (fc->flags & FUSE_DEFAULT_PERMISSIONS)
498 seq_puts(m, ",default_permissions");
499 if (fc->flags & FUSE_ALLOW_OTHER)
500 seq_puts(m, ",allow_other");
db50b96c
MS
501 if (fc->max_read != ~0)
502 seq_printf(m, ",max_read=%u", fc->max_read);
d1875dba
MS
503 if (mnt->mnt_sb->s_bdev &&
504 mnt->mnt_sb->s_blocksize != FUSE_DEFAULT_BLKSIZE)
505 seq_printf(m, ",blksize=%lu", mnt->mnt_sb->s_blocksize);
d8a5ba45
MS
506 return 0;
507}
508
a325f9b9 509void fuse_conn_init(struct fuse_conn *fc)
d8a5ba45 510{
0d179aa5
TH
511 memset(fc, 0, sizeof(*fc));
512 spin_lock_init(&fc->lock);
513 mutex_init(&fc->inst_mutex);
3b463ae0 514 init_rwsem(&fc->killsb);
0d179aa5
TH
515 atomic_set(&fc->count, 1);
516 init_waitqueue_head(&fc->waitq);
517 init_waitqueue_head(&fc->blocked_waitq);
518 init_waitqueue_head(&fc->reserved_req_waitq);
519 INIT_LIST_HEAD(&fc->pending);
520 INIT_LIST_HEAD(&fc->processing);
521 INIT_LIST_HEAD(&fc->io);
522 INIT_LIST_HEAD(&fc->interrupts);
523 INIT_LIST_HEAD(&fc->bg_queue);
524 INIT_LIST_HEAD(&fc->entry);
525 atomic_set(&fc->num_waiting, 0);
7a6d3c8b
CH
526 fc->max_background = FUSE_DEFAULT_MAX_BACKGROUND;
527 fc->congestion_threshold = FUSE_DEFAULT_CONGESTION_THRESHOLD;
0d179aa5
TH
528 fc->khctr = 0;
529 fc->polled_files = RB_ROOT;
0d179aa5
TH
530 fc->reqctr = 0;
531 fc->blocked = 1;
532 fc->attr_version = 1;
533 get_random_bytes(&fc->scramble_key, sizeof(fc->scramble_key));
d8a5ba45 534}
0d179aa5 535EXPORT_SYMBOL_GPL(fuse_conn_init);
d8a5ba45 536
bafa9654
MS
537void fuse_conn_put(struct fuse_conn *fc)
538{
d2a85164 539 if (atomic_dec_and_test(&fc->count)) {
0ec7ca41
MS
540 if (fc->destroy_req)
541 fuse_request_free(fc->destroy_req);
d2a85164 542 mutex_destroy(&fc->inst_mutex);
43901aab 543 fc->release(fc);
d2a85164 544 }
bafa9654 545}
08cbf542 546EXPORT_SYMBOL_GPL(fuse_conn_put);
bafa9654
MS
547
548struct fuse_conn *fuse_conn_get(struct fuse_conn *fc)
549{
550 atomic_inc(&fc->count);
551 return fc;
552}
08cbf542 553EXPORT_SYMBOL_GPL(fuse_conn_get);
bafa9654 554
b93f858a 555static struct inode *fuse_get_root_inode(struct super_block *sb, unsigned mode)
d8a5ba45
MS
556{
557 struct fuse_attr attr;
558 memset(&attr, 0, sizeof(attr));
559
560 attr.mode = mode;
561 attr.ino = FUSE_ROOT_ID;
074406fa 562 attr.nlink = 1;
1fb69e78 563 return fuse_iget(sb, 1, 0, &attr, 0, 0);
d8a5ba45
MS
564}
565
1729a16c 566struct fuse_inode_handle {
dbd561d2
MS
567 u64 nodeid;
568 u32 generation;
569};
570
571static struct dentry *fuse_get_dentry(struct super_block *sb,
572 struct fuse_inode_handle *handle)
573{
33670fa2 574 struct fuse_conn *fc = get_fuse_conn_super(sb);
dbd561d2
MS
575 struct inode *inode;
576 struct dentry *entry;
577 int err = -ESTALE;
578
579 if (handle->nodeid == 0)
580 goto out_err;
581
582 inode = ilookup5(sb, handle->nodeid, fuse_inode_eq, &handle->nodeid);
33670fa2
MS
583 if (!inode) {
584 struct fuse_entry_out outarg;
585 struct qstr name;
586
587 if (!fc->export_support)
588 goto out_err;
589
590 name.len = 1;
591 name.name = ".";
592 err = fuse_lookup_name(sb, handle->nodeid, &name, &outarg,
593 &inode);
594 if (err && err != -ENOENT)
595 goto out_err;
596 if (err || !inode) {
597 err = -ESTALE;
598 goto out_err;
599 }
600 err = -EIO;
601 if (get_node_id(inode) != handle->nodeid)
602 goto out_iput;
603 }
dbd561d2
MS
604 err = -ESTALE;
605 if (inode->i_generation != handle->generation)
606 goto out_iput;
607
44003728
CH
608 entry = d_obtain_alias(inode);
609 if (!IS_ERR(entry) && get_node_id(inode) != FUSE_ROOT_ID) {
dbd561d2
MS
610 entry->d_op = &fuse_dentry_operations;
611 fuse_invalidate_entry_cache(entry);
612 }
613
614 return entry;
615
616 out_iput:
617 iput(inode);
618 out_err:
619 return ERR_PTR(err);
620}
621
622static int fuse_encode_fh(struct dentry *dentry, u32 *fh, int *max_len,
623 int connectable)
624{
625 struct inode *inode = dentry->d_inode;
626 bool encode_parent = connectable && !S_ISDIR(inode->i_mode);
627 int len = encode_parent ? 6 : 3;
628 u64 nodeid;
629 u32 generation;
630
631 if (*max_len < len)
632 return 255;
633
634 nodeid = get_fuse_inode(inode)->nodeid;
635 generation = inode->i_generation;
636
637 fh[0] = (u32)(nodeid >> 32);
638 fh[1] = (u32)(nodeid & 0xffffffff);
639 fh[2] = generation;
640
641 if (encode_parent) {
642 struct inode *parent;
643
644 spin_lock(&dentry->d_lock);
645 parent = dentry->d_parent->d_inode;
646 nodeid = get_fuse_inode(parent)->nodeid;
647 generation = parent->i_generation;
648 spin_unlock(&dentry->d_lock);
649
650 fh[3] = (u32)(nodeid >> 32);
651 fh[4] = (u32)(nodeid & 0xffffffff);
652 fh[5] = generation;
653 }
654
655 *max_len = len;
656 return encode_parent ? 0x82 : 0x81;
657}
658
659static struct dentry *fuse_fh_to_dentry(struct super_block *sb,
660 struct fid *fid, int fh_len, int fh_type)
661{
662 struct fuse_inode_handle handle;
663
664 if ((fh_type != 0x81 && fh_type != 0x82) || fh_len < 3)
665 return NULL;
666
667 handle.nodeid = (u64) fid->raw[0] << 32;
668 handle.nodeid |= (u64) fid->raw[1];
669 handle.generation = fid->raw[2];
670 return fuse_get_dentry(sb, &handle);
671}
672
673static struct dentry *fuse_fh_to_parent(struct super_block *sb,
674 struct fid *fid, int fh_len, int fh_type)
675{
676 struct fuse_inode_handle parent;
677
678 if (fh_type != 0x82 || fh_len < 6)
679 return NULL;
680
681 parent.nodeid = (u64) fid->raw[3] << 32;
682 parent.nodeid |= (u64) fid->raw[4];
683 parent.generation = fid->raw[5];
684 return fuse_get_dentry(sb, &parent);
685}
686
33670fa2
MS
687static struct dentry *fuse_get_parent(struct dentry *child)
688{
689 struct inode *child_inode = child->d_inode;
690 struct fuse_conn *fc = get_fuse_conn(child_inode);
691 struct inode *inode;
692 struct dentry *parent;
693 struct fuse_entry_out outarg;
694 struct qstr name;
695 int err;
696
697 if (!fc->export_support)
698 return ERR_PTR(-ESTALE);
699
700 name.len = 2;
701 name.name = "..";
702 err = fuse_lookup_name(child_inode->i_sb, get_node_id(child_inode),
703 &name, &outarg, &inode);
44003728
CH
704 if (err) {
705 if (err == -ENOENT)
706 return ERR_PTR(-ESTALE);
33670fa2 707 return ERR_PTR(err);
33670fa2 708 }
44003728
CH
709
710 parent = d_obtain_alias(inode);
711 if (!IS_ERR(parent) && get_node_id(inode) != FUSE_ROOT_ID) {
33670fa2
MS
712 parent->d_op = &fuse_dentry_operations;
713 fuse_invalidate_entry_cache(parent);
714 }
715
716 return parent;
717}
dbd561d2
MS
718
719static const struct export_operations fuse_export_operations = {
720 .fh_to_dentry = fuse_fh_to_dentry,
721 .fh_to_parent = fuse_fh_to_parent,
722 .encode_fh = fuse_encode_fh,
33670fa2 723 .get_parent = fuse_get_parent,
dbd561d2
MS
724};
725
ee9b6d61 726static const struct super_operations fuse_super_operations = {
d8a5ba45
MS
727 .alloc_inode = fuse_alloc_inode,
728 .destroy_inode = fuse_destroy_inode,
d8a5ba45 729 .clear_inode = fuse_clear_inode,
ead5f0b5 730 .drop_inode = generic_delete_inode,
71421259 731 .remount_fs = fuse_remount_fs,
d8a5ba45 732 .put_super = fuse_put_super,
69a53bf2 733 .umount_begin = fuse_umount_begin,
e5e5558e 734 .statfs = fuse_statfs,
d8a5ba45
MS
735 .show_options = fuse_show_options,
736};
737
9b9a0469
MS
738static void process_init_reply(struct fuse_conn *fc, struct fuse_req *req)
739{
9b9a0469
MS
740 struct fuse_init_out *arg = &req->misc.init_out;
741
742 if (req->out.h.error || arg->major != FUSE_KERNEL_VERSION)
743 fc->conn_error = 1;
744 else {
9cd68455
MS
745 unsigned long ra_pages;
746
7a6d3c8b
CH
747 if (arg->minor >= 13) {
748 if (arg->max_background)
749 fc->max_background = arg->max_background;
750 if (arg->congestion_threshold)
751 fc->congestion_threshold = arg->congestion_threshold;
752 }
9cd68455
MS
753 if (arg->minor >= 6) {
754 ra_pages = arg->max_readahead / PAGE_CACHE_SIZE;
755 if (arg->flags & FUSE_ASYNC_READ)
756 fc->async_read = 1;
71421259
MS
757 if (!(arg->flags & FUSE_POSIX_LOCKS))
758 fc->no_lock = 1;
6ff958ed
MS
759 if (arg->flags & FUSE_ATOMIC_O_TRUNC)
760 fc->atomic_o_trunc = 1;
33670fa2
MS
761 if (arg->minor >= 9) {
762 /* LOOKUP has dependency on proto version */
763 if (arg->flags & FUSE_EXPORT_SUPPORT)
764 fc->export_support = 1;
765 }
78bb6cb9
MS
766 if (arg->flags & FUSE_BIG_WRITES)
767 fc->big_writes = 1;
e0a43ddc
MS
768 if (arg->flags & FUSE_DONT_MASK)
769 fc->dont_mask = 1;
71421259 770 } else {
9cd68455 771 ra_pages = fc->max_read / PAGE_CACHE_SIZE;
71421259
MS
772 fc->no_lock = 1;
773 }
9cd68455
MS
774
775 fc->bdi.ra_pages = min(fc->bdi.ra_pages, ra_pages);
9b9a0469
MS
776 fc->minor = arg->minor;
777 fc->max_write = arg->minor < 5 ? 4096 : arg->max_write;
f948d564 778 fc->max_write = max_t(unsigned, 4096, fc->max_write);
0ec7ca41 779 fc->conn_init = 1;
9b9a0469 780 }
08a53cdc
MS
781 fc->blocked = 0;
782 wake_up_all(&fc->blocked_waitq);
9b9a0469
MS
783}
784
ce1d5a49 785static void fuse_send_init(struct fuse_conn *fc, struct fuse_req *req)
9b9a0469 786{
9b9a0469 787 struct fuse_init_in *arg = &req->misc.init_in;
095da6cb 788
9b9a0469
MS
789 arg->major = FUSE_KERNEL_VERSION;
790 arg->minor = FUSE_KERNEL_MINOR_VERSION;
9cd68455 791 arg->max_readahead = fc->bdi.ra_pages * PAGE_CACHE_SIZE;
78bb6cb9 792 arg->flags |= FUSE_ASYNC_READ | FUSE_POSIX_LOCKS | FUSE_ATOMIC_O_TRUNC |
e0a43ddc 793 FUSE_EXPORT_SUPPORT | FUSE_BIG_WRITES | FUSE_DONT_MASK;
9b9a0469
MS
794 req->in.h.opcode = FUSE_INIT;
795 req->in.numargs = 1;
796 req->in.args[0].size = sizeof(*arg);
797 req->in.args[0].value = arg;
798 req->out.numargs = 1;
799 /* Variable length arguement used for backward compatibility
800 with interface version < 7.5. Rest of init_out is zeroed
801 by do_get_request(), so a short reply is not a problem */
802 req->out.argvar = 1;
803 req->out.args[0].size = sizeof(struct fuse_init_out);
804 req->out.args[0].value = &req->misc.init_out;
805 req->end = process_init_reply;
b93f858a 806 fuse_request_send_background(fc, req);
9b9a0469
MS
807}
808
43901aab
TH
809static void fuse_free_conn(struct fuse_conn *fc)
810{
811 kfree(fc);
812}
813
a325f9b9
TH
814static int fuse_bdi_init(struct fuse_conn *fc, struct super_block *sb)
815{
816 int err;
817
818 fc->bdi.ra_pages = (VM_MAX_READAHEAD * 1024) / PAGE_CACHE_SIZE;
819 fc->bdi.unplug_io_fn = default_unplug_io_fn;
820 /* fuse does it's own writeback accounting */
821 fc->bdi.capabilities = BDI_CAP_NO_ACCT_WB;
822
823 err = bdi_init(&fc->bdi);
824 if (err)
825 return err;
826
827 fc->bdi_initialized = 1;
828
829 if (sb->s_bdev) {
830 err = bdi_register(&fc->bdi, NULL, "%u:%u-fuseblk",
831 MAJOR(fc->dev), MINOR(fc->dev));
832 } else {
833 err = bdi_register_dev(&fc->bdi, fc->dev);
834 }
835
836 if (err)
837 return err;
838
839 /*
840 * For a single fuse filesystem use max 1% of dirty +
841 * writeback threshold.
842 *
843 * This gives about 1M of write buffer for memory maps on a
844 * machine with 1G and 10% dirty_ratio, which should be more
845 * than enough.
846 *
847 * Privileged users can raise it by writing to
848 *
849 * /sys/class/bdi/<bdi>/max_ratio
850 */
851 bdi_set_max_ratio(&fc->bdi, 1);
852
853 return 0;
854}
855
d8a5ba45
MS
856static int fuse_fill_super(struct super_block *sb, void *data, int silent)
857{
858 struct fuse_conn *fc;
859 struct inode *root;
860 struct fuse_mount_data d;
861 struct file *file;
f543f253 862 struct dentry *root_dentry;
ce1d5a49 863 struct fuse_req *init_req;
d8a5ba45 864 int err;
d8091614 865 int is_bdev = sb->s_bdev != NULL;
d8a5ba45 866
c2b8f006 867 err = -EINVAL;
71421259 868 if (sb->s_flags & MS_MANDLOCK)
c2b8f006 869 goto err;
71421259 870
d8091614 871 if (!parse_fuse_opt((char *) data, &d, is_bdev))
c2b8f006 872 goto err;
d8a5ba45 873
d8091614 874 if (is_bdev) {
875d95ec 875#ifdef CONFIG_BLOCK
c2b8f006 876 err = -EINVAL;
d8091614 877 if (!sb_set_blocksize(sb, d.blksize))
c2b8f006 878 goto err;
875d95ec 879#endif
d8091614
MS
880 } else {
881 sb->s_blocksize = PAGE_CACHE_SIZE;
882 sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
883 }
d8a5ba45
MS
884 sb->s_magic = FUSE_SUPER_MAGIC;
885 sb->s_op = &fuse_super_operations;
886 sb->s_maxbytes = MAX_LFS_FILESIZE;
dbd561d2 887 sb->s_export_op = &fuse_export_operations;
d8a5ba45
MS
888
889 file = fget(d.fd);
c2b8f006 890 err = -EINVAL;
d8a5ba45 891 if (!file)
c2b8f006 892 goto err;
d8a5ba45 893
c2b8f006
MS
894 if (file->f_op != &fuse_dev_operations)
895 goto err_fput;
0720b315 896
0d179aa5 897 fc = kmalloc(sizeof(*fc), GFP_KERNEL);
c2b8f006
MS
898 err = -ENOMEM;
899 if (!fc)
900 goto err_fput;
d8a5ba45 901
a325f9b9
TH
902 fuse_conn_init(fc);
903
904 fc->dev = sb->s_dev;
3b463ae0 905 fc->sb = sb;
a325f9b9
TH
906 err = fuse_bdi_init(fc, sb);
907 if (err)
908 goto err_put_conn;
0d179aa5 909
e0a43ddc
MS
910 /* Handle umasking inside the fuse code */
911 if (sb->s_flags & MS_POSIXACL)
912 fc->dont_mask = 1;
913 sb->s_flags |= MS_POSIXACL;
914
43901aab 915 fc->release = fuse_free_conn;
1e9a4ed9 916 fc->flags = d.flags;
d8a5ba45 917 fc->user_id = d.user_id;
87729a55 918 fc->group_id = d.group_id;
f948d564 919 fc->max_read = max_t(unsigned, 4096, d.max_read);
d8a5ba45 920
f543f253
MS
921 /* Used by get_root_inode() */
922 sb->s_fs_info = fc;
923
d8a5ba45 924 err = -ENOMEM;
b93f858a 925 root = fuse_get_root_inode(sb, d.rootmode);
f543f253 926 if (!root)
c2b8f006 927 goto err_put_conn;
d8a5ba45 928
f543f253
MS
929 root_dentry = d_alloc_root(root);
930 if (!root_dentry) {
d8a5ba45 931 iput(root);
c2b8f006 932 goto err_put_conn;
d8a5ba45 933 }
f543f253 934
ce1d5a49
MS
935 init_req = fuse_request_alloc();
936 if (!init_req)
937 goto err_put_root;
938
0ec7ca41
MS
939 if (is_bdev) {
940 fc->destroy_req = fuse_request_alloc();
941 if (!fc->destroy_req)
17e18ab6 942 goto err_free_init_req;
0ec7ca41
MS
943 }
944
bafa9654 945 mutex_lock(&fuse_mutex);
8aa09a50
MS
946 err = -EINVAL;
947 if (file->private_data)
bafa9654 948 goto err_unlock;
8aa09a50 949
bafa9654
MS
950 err = fuse_ctl_add_conn(fc);
951 if (err)
952 goto err_unlock;
953
954 list_add_tail(&fc->entry, &fuse_conn_list);
f543f253 955 sb->s_root = root_dentry;
f543f253 956 fc->connected = 1;
bafa9654
MS
957 file->private_data = fuse_conn_get(fc);
958 mutex_unlock(&fuse_mutex);
0720b315
MS
959 /*
960 * atomic_dec_and_test() in fput() provides the necessary
961 * memory barrier for file->private_data to be visible on all
962 * CPUs after this
963 */
964 fput(file);
f543f253 965
ce1d5a49 966 fuse_send_init(fc, init_req);
f543f253 967
d8a5ba45
MS
968 return 0;
969
bafa9654
MS
970 err_unlock:
971 mutex_unlock(&fuse_mutex);
17e18ab6 972 err_free_init_req:
ce1d5a49 973 fuse_request_free(init_req);
f543f253
MS
974 err_put_root:
975 dput(root_dentry);
c2b8f006 976 err_put_conn:
a325f9b9 977 fuse_bdi_destroy(fc);
bafa9654 978 fuse_conn_put(fc);
c2b8f006
MS
979 err_fput:
980 fput(file);
981 err:
d8a5ba45
MS
982 return err;
983}
984
454e2398
DH
985static int fuse_get_sb(struct file_system_type *fs_type,
986 int flags, const char *dev_name,
987 void *raw_data, struct vfsmount *mnt)
d8a5ba45 988{
454e2398 989 return get_sb_nodev(fs_type, flags, raw_data, fuse_fill_super, mnt);
d8a5ba45
MS
990}
991
3b463ae0
JM
992static void fuse_kill_sb_anon(struct super_block *sb)
993{
994 struct fuse_conn *fc = get_fuse_conn_super(sb);
995
996 if (fc) {
997 down_write(&fc->killsb);
998 fc->sb = NULL;
999 up_write(&fc->killsb);
1000 }
1001
1002 kill_anon_super(sb);
1003}
1004
875d95ec
MS
1005static struct file_system_type fuse_fs_type = {
1006 .owner = THIS_MODULE,
1007 .name = "fuse",
79c0b2df 1008 .fs_flags = FS_HAS_SUBTYPE,
875d95ec 1009 .get_sb = fuse_get_sb,
3b463ae0 1010 .kill_sb = fuse_kill_sb_anon,
875d95ec
MS
1011};
1012
1013#ifdef CONFIG_BLOCK
d6392f87
MS
1014static int fuse_get_sb_blk(struct file_system_type *fs_type,
1015 int flags, const char *dev_name,
1016 void *raw_data, struct vfsmount *mnt)
1017{
1018 return get_sb_bdev(fs_type, flags, dev_name, raw_data, fuse_fill_super,
1019 mnt);
1020}
1021
3b463ae0
JM
1022static void fuse_kill_sb_blk(struct super_block *sb)
1023{
1024 struct fuse_conn *fc = get_fuse_conn_super(sb);
1025
1026 if (fc) {
1027 down_write(&fc->killsb);
1028 fc->sb = NULL;
1029 up_write(&fc->killsb);
1030 }
1031
1032 kill_block_super(sb);
1033}
1034
d6392f87
MS
1035static struct file_system_type fuseblk_fs_type = {
1036 .owner = THIS_MODULE,
1037 .name = "fuseblk",
1038 .get_sb = fuse_get_sb_blk,
3b463ae0 1039 .kill_sb = fuse_kill_sb_blk,
edad01e2 1040 .fs_flags = FS_REQUIRES_DEV | FS_HAS_SUBTYPE,
d6392f87
MS
1041};
1042
875d95ec
MS
1043static inline int register_fuseblk(void)
1044{
1045 return register_filesystem(&fuseblk_fs_type);
1046}
1047
1048static inline void unregister_fuseblk(void)
1049{
1050 unregister_filesystem(&fuseblk_fs_type);
1051}
1052#else
1053static inline int register_fuseblk(void)
1054{
1055 return 0;
1056}
1057
1058static inline void unregister_fuseblk(void)
1059{
1060}
1061#endif
1062
51cc5068 1063static void fuse_inode_init_once(void *foo)
d8a5ba45 1064{
1729a16c 1065 struct inode *inode = foo;
d8a5ba45 1066
a35afb83 1067 inode_init_once(inode);
d8a5ba45
MS
1068}
1069
1070static int __init fuse_fs_init(void)
1071{
1072 int err;
1073
1074 err = register_filesystem(&fuse_fs_type);
1075 if (err)
d6392f87
MS
1076 goto out;
1077
875d95ec 1078 err = register_fuseblk();
d6392f87
MS
1079 if (err)
1080 goto out_unreg;
1081
1082 fuse_inode_cachep = kmem_cache_create("fuse_inode",
1083 sizeof(struct fuse_inode),
1084 0, SLAB_HWCACHE_ALIGN,
20c2df83 1085 fuse_inode_init_once);
d6392f87
MS
1086 err = -ENOMEM;
1087 if (!fuse_inode_cachep)
1088 goto out_unreg2;
1089
1090 return 0;
d8a5ba45 1091
d6392f87 1092 out_unreg2:
875d95ec 1093 unregister_fuseblk();
d6392f87
MS
1094 out_unreg:
1095 unregister_filesystem(&fuse_fs_type);
1096 out:
d8a5ba45
MS
1097 return err;
1098}
1099
1100static void fuse_fs_cleanup(void)
1101{
1102 unregister_filesystem(&fuse_fs_type);
875d95ec 1103 unregister_fuseblk();
d8a5ba45
MS
1104 kmem_cache_destroy(fuse_inode_cachep);
1105}
1106
5c89e17e
GKH
1107static struct kobject *fuse_kobj;
1108static struct kobject *connections_kobj;
1109
f543f253
MS
1110static int fuse_sysfs_init(void)
1111{
1112 int err;
1113
00d26666 1114 fuse_kobj = kobject_create_and_add("fuse", fs_kobj);
5c89e17e
GKH
1115 if (!fuse_kobj) {
1116 err = -ENOMEM;
f543f253 1117 goto out_err;
5c89e17e 1118 }
f543f253 1119
5c89e17e
GKH
1120 connections_kobj = kobject_create_and_add("connections", fuse_kobj);
1121 if (!connections_kobj) {
1122 err = -ENOMEM;
f543f253 1123 goto out_fuse_unregister;
5c89e17e 1124 }
f543f253
MS
1125
1126 return 0;
1127
1128 out_fuse_unregister:
197b12d6 1129 kobject_put(fuse_kobj);
f543f253
MS
1130 out_err:
1131 return err;
1132}
1133
1134static void fuse_sysfs_cleanup(void)
1135{
197b12d6
GKH
1136 kobject_put(connections_kobj);
1137 kobject_put(fuse_kobj);
f543f253
MS
1138}
1139
d8a5ba45
MS
1140static int __init fuse_init(void)
1141{
1142 int res;
1143
1729a16c 1144 printk(KERN_INFO "fuse init (API version %i.%i)\n",
d8a5ba45
MS
1145 FUSE_KERNEL_VERSION, FUSE_KERNEL_MINOR_VERSION);
1146
bafa9654 1147 INIT_LIST_HEAD(&fuse_conn_list);
d8a5ba45
MS
1148 res = fuse_fs_init();
1149 if (res)
1150 goto err;
1151
334f485d
MS
1152 res = fuse_dev_init();
1153 if (res)
1154 goto err_fs_cleanup;
1155
f543f253
MS
1156 res = fuse_sysfs_init();
1157 if (res)
1158 goto err_dev_cleanup;
1159
bafa9654
MS
1160 res = fuse_ctl_init();
1161 if (res)
1162 goto err_sysfs_cleanup;
1163
d8a5ba45
MS
1164 return 0;
1165
bafa9654
MS
1166 err_sysfs_cleanup:
1167 fuse_sysfs_cleanup();
f543f253
MS
1168 err_dev_cleanup:
1169 fuse_dev_cleanup();
334f485d
MS
1170 err_fs_cleanup:
1171 fuse_fs_cleanup();
d8a5ba45
MS
1172 err:
1173 return res;
1174}
1175
1176static void __exit fuse_exit(void)
1177{
1178 printk(KERN_DEBUG "fuse exit\n");
1179
bafa9654 1180 fuse_ctl_cleanup();
f543f253 1181 fuse_sysfs_cleanup();
d8a5ba45 1182 fuse_fs_cleanup();
334f485d 1183 fuse_dev_cleanup();
d8a5ba45
MS
1184}
1185
1186module_init(fuse_init);
1187module_exit(fuse_exit);
This page took 0.572444 seconds and 5 git commands to generate.