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