xref: /linux/fs/fuse/dir.c (revision 6e8331ac6973435b1e7604c30f2ad394035b46e1)
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/file.h>
13 #include <linux/gfp.h>
14 #include <linux/sched.h>
15 #include <linux/namei.h>
16 
17 #if BITS_PER_LONG >= 64
18 static inline void fuse_dentry_settime(struct dentry *entry, u64 time)
19 {
20 	entry->d_time = time;
21 }
22 
23 static inline u64 fuse_dentry_time(struct dentry *entry)
24 {
25 	return entry->d_time;
26 }
27 #else
28 /*
29  * On 32 bit archs store the high 32 bits of time in d_fsdata
30  */
31 static void fuse_dentry_settime(struct dentry *entry, u64 time)
32 {
33 	entry->d_time = time;
34 	entry->d_fsdata = (void *) (unsigned long) (time >> 32);
35 }
36 
37 static u64 fuse_dentry_time(struct dentry *entry)
38 {
39 	return (u64) entry->d_time +
40 		((u64) (unsigned long) entry->d_fsdata << 32);
41 }
42 #endif
43 
44 /*
45  * FUSE caches dentries and attributes with separate timeout.  The
46  * time in jiffies until the dentry/attributes are valid is stored in
47  * dentry->d_time and fuse_inode->i_time respectively.
48  */
49 
50 /*
51  * Calculate the time in jiffies until a dentry/attributes are valid
52  */
53 static u64 time_to_jiffies(unsigned long sec, unsigned long nsec)
54 {
55 	if (sec || nsec) {
56 		struct timespec ts = {sec, nsec};
57 		return get_jiffies_64() + timespec_to_jiffies(&ts);
58 	} else
59 		return 0;
60 }
61 
62 /*
63  * Set dentry and possibly attribute timeouts from the lookup/mk*
64  * replies
65  */
66 static void fuse_change_timeout(struct dentry *entry, struct fuse_entry_out *o)
67 {
68 	fuse_dentry_settime(entry,
69 		time_to_jiffies(o->entry_valid, o->entry_valid_nsec));
70 	if (entry->d_inode)
71 		get_fuse_inode(entry->d_inode)->i_time =
72 			time_to_jiffies(o->attr_valid, o->attr_valid_nsec);
73 }
74 
75 /*
76  * Mark the attributes as stale, so that at the next call to
77  * ->getattr() they will be fetched from userspace
78  */
79 void fuse_invalidate_attr(struct inode *inode)
80 {
81 	get_fuse_inode(inode)->i_time = 0;
82 }
83 
84 /*
85  * Just mark the entry as stale, so that a next attempt to look it up
86  * will result in a new lookup call to userspace
87  *
88  * This is called when a dentry is about to become negative and the
89  * timeout is unknown (unlink, rmdir, rename and in some cases
90  * lookup)
91  */
92 static void fuse_invalidate_entry_cache(struct dentry *entry)
93 {
94 	fuse_dentry_settime(entry, 0);
95 }
96 
97 /*
98  * Same as fuse_invalidate_entry_cache(), but also try to remove the
99  * dentry from the hash
100  */
101 static void fuse_invalidate_entry(struct dentry *entry)
102 {
103 	d_invalidate(entry);
104 	fuse_invalidate_entry_cache(entry);
105 }
106 
107 static void fuse_lookup_init(struct fuse_req *req, struct inode *dir,
108 			     struct dentry *entry,
109 			     struct fuse_entry_out *outarg)
110 {
111 	req->in.h.opcode = FUSE_LOOKUP;
112 	req->in.h.nodeid = get_node_id(dir);
113 	req->in.numargs = 1;
114 	req->in.args[0].size = entry->d_name.len + 1;
115 	req->in.args[0].value = entry->d_name.name;
116 	req->out.numargs = 1;
117 	req->out.args[0].size = sizeof(struct fuse_entry_out);
118 	req->out.args[0].value = outarg;
119 }
120 
121 /*
122  * Check whether the dentry is still valid
123  *
124  * If the entry validity timeout has expired and the dentry is
125  * positive, try to redo the lookup.  If the lookup results in a
126  * different inode, then let the VFS invalidate the dentry and redo
127  * the lookup once more.  If the lookup results in the same inode,
128  * then refresh the attributes, timeouts and mark the dentry valid.
129  */
130 static int fuse_dentry_revalidate(struct dentry *entry, struct nameidata *nd)
131 {
132 	struct inode *inode = entry->d_inode;
133 
134 	if (inode && is_bad_inode(inode))
135 		return 0;
136 	else if (fuse_dentry_time(entry) < get_jiffies_64()) {
137 		int err;
138 		struct fuse_entry_out outarg;
139 		struct fuse_conn *fc;
140 		struct fuse_req *req;
141 
142 		/* Doesn't hurt to "reset" the validity timeout */
143 		fuse_invalidate_entry_cache(entry);
144 
145 		/* For negative dentries, always do a fresh lookup */
146 		if (!inode)
147 			return 0;
148 
149 		fc = get_fuse_conn(inode);
150 		req = fuse_get_req(fc);
151 		if (IS_ERR(req))
152 			return 0;
153 
154 		fuse_lookup_init(req, entry->d_parent->d_inode, entry, &outarg);
155 		request_send(fc, req);
156 		err = req->out.h.error;
157 		/* Zero nodeid is same as -ENOENT */
158 		if (!err && !outarg.nodeid)
159 			err = -ENOENT;
160 		if (!err) {
161 			struct fuse_inode *fi = get_fuse_inode(inode);
162 			if (outarg.nodeid != get_node_id(inode)) {
163 				fuse_send_forget(fc, req, outarg.nodeid, 1);
164 				return 0;
165 			}
166 			fi->nlookup ++;
167 		}
168 		fuse_put_request(fc, req);
169 		if (err || (outarg.attr.mode ^ inode->i_mode) & S_IFMT)
170 			return 0;
171 
172 		fuse_change_attributes(inode, &outarg.attr);
173 		fuse_change_timeout(entry, &outarg);
174 	}
175 	return 1;
176 }
177 
178 /*
179  * Check if there's already a hashed alias of this directory inode.
180  * If yes, then lookup and mkdir must not create a new alias.
181  */
182 static int dir_alias(struct inode *inode)
183 {
184 	if (S_ISDIR(inode->i_mode)) {
185 		struct dentry *alias = d_find_alias(inode);
186 		if (alias) {
187 			dput(alias);
188 			return 1;
189 		}
190 	}
191 	return 0;
192 }
193 
194 static int invalid_nodeid(u64 nodeid)
195 {
196 	return !nodeid || nodeid == FUSE_ROOT_ID;
197 }
198 
199 static struct dentry_operations fuse_dentry_operations = {
200 	.d_revalidate	= fuse_dentry_revalidate,
201 };
202 
203 static int valid_mode(int m)
204 {
205 	return S_ISREG(m) || S_ISDIR(m) || S_ISLNK(m) || S_ISCHR(m) ||
206 		S_ISBLK(m) || S_ISFIFO(m) || S_ISSOCK(m);
207 }
208 
209 static struct dentry *fuse_lookup(struct inode *dir, struct dentry *entry,
210 				  struct nameidata *nd)
211 {
212 	int err;
213 	struct fuse_entry_out outarg;
214 	struct inode *inode = NULL;
215 	struct fuse_conn *fc = get_fuse_conn(dir);
216 	struct fuse_req *req;
217 
218 	if (entry->d_name.len > FUSE_NAME_MAX)
219 		return ERR_PTR(-ENAMETOOLONG);
220 
221 	req = fuse_get_req(fc);
222 	if (IS_ERR(req))
223 		return ERR_PTR(PTR_ERR(req));
224 
225 	fuse_lookup_init(req, dir, entry, &outarg);
226 	request_send(fc, req);
227 	err = req->out.h.error;
228 	/* Zero nodeid is same as -ENOENT, but with valid timeout */
229 	if (!err && outarg.nodeid &&
230 	    (invalid_nodeid(outarg.nodeid) || !valid_mode(outarg.attr.mode)))
231 		err = -EIO;
232 	if (!err && outarg.nodeid) {
233 		inode = fuse_iget(dir->i_sb, outarg.nodeid, outarg.generation,
234 				  &outarg.attr);
235 		if (!inode) {
236 			fuse_send_forget(fc, req, outarg.nodeid, 1);
237 			return ERR_PTR(-ENOMEM);
238 		}
239 	}
240 	fuse_put_request(fc, req);
241 	if (err && err != -ENOENT)
242 		return ERR_PTR(err);
243 
244 	if (inode && dir_alias(inode)) {
245 		iput(inode);
246 		return ERR_PTR(-EIO);
247 	}
248 	d_add(entry, inode);
249 	entry->d_op = &fuse_dentry_operations;
250 	if (!err)
251 		fuse_change_timeout(entry, &outarg);
252 	else
253 		fuse_invalidate_entry_cache(entry);
254 	return NULL;
255 }
256 
257 /*
258  * Synchronous release for the case when something goes wrong in CREATE_OPEN
259  */
260 static void fuse_sync_release(struct fuse_conn *fc, struct fuse_file *ff,
261 			      u64 nodeid, int flags)
262 {
263 	struct fuse_req *req;
264 
265 	req = fuse_release_fill(ff, nodeid, flags, FUSE_RELEASE);
266 	req->force = 1;
267 	request_send(fc, req);
268 	fuse_put_request(fc, req);
269 }
270 
271 /*
272  * Atomic create+open operation
273  *
274  * If the filesystem doesn't support this, then fall back to separate
275  * 'mknod' + 'open' requests.
276  */
277 static int fuse_create_open(struct inode *dir, struct dentry *entry, int mode,
278 			    struct nameidata *nd)
279 {
280 	int err;
281 	struct inode *inode;
282 	struct fuse_conn *fc = get_fuse_conn(dir);
283 	struct fuse_req *req;
284 	struct fuse_req *forget_req;
285 	struct fuse_open_in inarg;
286 	struct fuse_open_out outopen;
287 	struct fuse_entry_out outentry;
288 	struct fuse_file *ff;
289 	struct file *file;
290 	int flags = nd->intent.open.flags - 1;
291 
292 	if (fc->no_create)
293 		return -ENOSYS;
294 
295 	forget_req = fuse_get_req(fc);
296 	if (IS_ERR(forget_req))
297 		return PTR_ERR(forget_req);
298 
299 	req = fuse_get_req(fc);
300 	err = PTR_ERR(req);
301 	if (IS_ERR(req))
302 		goto out_put_forget_req;
303 
304 	err = -ENOMEM;
305 	ff = fuse_file_alloc();
306 	if (!ff)
307 		goto out_put_request;
308 
309 	flags &= ~O_NOCTTY;
310 	memset(&inarg, 0, sizeof(inarg));
311 	inarg.flags = flags;
312 	inarg.mode = mode;
313 	req->in.h.opcode = FUSE_CREATE;
314 	req->in.h.nodeid = get_node_id(dir);
315 	req->in.numargs = 2;
316 	req->in.args[0].size = sizeof(inarg);
317 	req->in.args[0].value = &inarg;
318 	req->in.args[1].size = entry->d_name.len + 1;
319 	req->in.args[1].value = entry->d_name.name;
320 	req->out.numargs = 2;
321 	req->out.args[0].size = sizeof(outentry);
322 	req->out.args[0].value = &outentry;
323 	req->out.args[1].size = sizeof(outopen);
324 	req->out.args[1].value = &outopen;
325 	request_send(fc, req);
326 	err = req->out.h.error;
327 	if (err) {
328 		if (err == -ENOSYS)
329 			fc->no_create = 1;
330 		goto out_free_ff;
331 	}
332 
333 	err = -EIO;
334 	if (!S_ISREG(outentry.attr.mode) || invalid_nodeid(outentry.nodeid))
335 		goto out_free_ff;
336 
337 	fuse_put_request(fc, req);
338 	inode = fuse_iget(dir->i_sb, outentry.nodeid, outentry.generation,
339 			  &outentry.attr);
340 	if (!inode) {
341 		flags &= ~(O_CREAT | O_EXCL | O_TRUNC);
342 		ff->fh = outopen.fh;
343 		fuse_sync_release(fc, ff, outentry.nodeid, flags);
344 		fuse_send_forget(fc, forget_req, outentry.nodeid, 1);
345 		return -ENOMEM;
346 	}
347 	fuse_put_request(fc, forget_req);
348 	d_instantiate(entry, inode);
349 	fuse_change_timeout(entry, &outentry);
350 	file = lookup_instantiate_filp(nd, entry, generic_file_open);
351 	if (IS_ERR(file)) {
352 		ff->fh = outopen.fh;
353 		fuse_sync_release(fc, ff, outentry.nodeid, flags);
354 		return PTR_ERR(file);
355 	}
356 	fuse_finish_open(inode, file, ff, &outopen);
357 	return 0;
358 
359  out_free_ff:
360 	fuse_file_free(ff);
361  out_put_request:
362 	fuse_put_request(fc, req);
363  out_put_forget_req:
364 	fuse_put_request(fc, forget_req);
365 	return err;
366 }
367 
368 /*
369  * Code shared between mknod, mkdir, symlink and link
370  */
371 static int create_new_entry(struct fuse_conn *fc, struct fuse_req *req,
372 			    struct inode *dir, struct dentry *entry,
373 			    int mode)
374 {
375 	struct fuse_entry_out outarg;
376 	struct inode *inode;
377 	int err;
378 
379 	req->in.h.nodeid = get_node_id(dir);
380 	req->out.numargs = 1;
381 	req->out.args[0].size = sizeof(outarg);
382 	req->out.args[0].value = &outarg;
383 	request_send(fc, req);
384 	err = req->out.h.error;
385 	if (err) {
386 		fuse_put_request(fc, req);
387 		return err;
388 	}
389 	err = -EIO;
390 	if (invalid_nodeid(outarg.nodeid))
391 		goto out_put_request;
392 
393 	if ((outarg.attr.mode ^ mode) & S_IFMT)
394 		goto out_put_request;
395 
396 	inode = fuse_iget(dir->i_sb, outarg.nodeid, outarg.generation,
397 			  &outarg.attr);
398 	if (!inode) {
399 		fuse_send_forget(fc, req, outarg.nodeid, 1);
400 		return -ENOMEM;
401 	}
402 	fuse_put_request(fc, req);
403 
404 	if (dir_alias(inode)) {
405 		iput(inode);
406 		return -EIO;
407 	}
408 
409 	d_instantiate(entry, inode);
410 	fuse_change_timeout(entry, &outarg);
411 	fuse_invalidate_attr(dir);
412 	return 0;
413 
414  out_put_request:
415 	fuse_put_request(fc, req);
416 	return err;
417 }
418 
419 static int fuse_mknod(struct inode *dir, struct dentry *entry, int mode,
420 		      dev_t rdev)
421 {
422 	struct fuse_mknod_in inarg;
423 	struct fuse_conn *fc = get_fuse_conn(dir);
424 	struct fuse_req *req = fuse_get_req(fc);
425 	if (IS_ERR(req))
426 		return PTR_ERR(req);
427 
428 	memset(&inarg, 0, sizeof(inarg));
429 	inarg.mode = mode;
430 	inarg.rdev = new_encode_dev(rdev);
431 	req->in.h.opcode = FUSE_MKNOD;
432 	req->in.numargs = 2;
433 	req->in.args[0].size = sizeof(inarg);
434 	req->in.args[0].value = &inarg;
435 	req->in.args[1].size = entry->d_name.len + 1;
436 	req->in.args[1].value = entry->d_name.name;
437 	return create_new_entry(fc, req, dir, entry, mode);
438 }
439 
440 static int fuse_create(struct inode *dir, struct dentry *entry, int mode,
441 		       struct nameidata *nd)
442 {
443 	if (nd && (nd->flags & LOOKUP_CREATE)) {
444 		int err = fuse_create_open(dir, entry, mode, nd);
445 		if (err != -ENOSYS)
446 			return err;
447 		/* Fall back on mknod */
448 	}
449 	return fuse_mknod(dir, entry, mode, 0);
450 }
451 
452 static int fuse_mkdir(struct inode *dir, struct dentry *entry, int mode)
453 {
454 	struct fuse_mkdir_in inarg;
455 	struct fuse_conn *fc = get_fuse_conn(dir);
456 	struct fuse_req *req = fuse_get_req(fc);
457 	if (IS_ERR(req))
458 		return PTR_ERR(req);
459 
460 	memset(&inarg, 0, sizeof(inarg));
461 	inarg.mode = mode;
462 	req->in.h.opcode = FUSE_MKDIR;
463 	req->in.numargs = 2;
464 	req->in.args[0].size = sizeof(inarg);
465 	req->in.args[0].value = &inarg;
466 	req->in.args[1].size = entry->d_name.len + 1;
467 	req->in.args[1].value = entry->d_name.name;
468 	return create_new_entry(fc, req, dir, entry, S_IFDIR);
469 }
470 
471 static int fuse_symlink(struct inode *dir, struct dentry *entry,
472 			const char *link)
473 {
474 	struct fuse_conn *fc = get_fuse_conn(dir);
475 	unsigned len = strlen(link) + 1;
476 	struct fuse_req *req = fuse_get_req(fc);
477 	if (IS_ERR(req))
478 		return PTR_ERR(req);
479 
480 	req->in.h.opcode = FUSE_SYMLINK;
481 	req->in.numargs = 2;
482 	req->in.args[0].size = entry->d_name.len + 1;
483 	req->in.args[0].value = entry->d_name.name;
484 	req->in.args[1].size = len;
485 	req->in.args[1].value = link;
486 	return create_new_entry(fc, req, dir, entry, S_IFLNK);
487 }
488 
489 static int fuse_unlink(struct inode *dir, struct dentry *entry)
490 {
491 	int err;
492 	struct fuse_conn *fc = get_fuse_conn(dir);
493 	struct fuse_req *req = fuse_get_req(fc);
494 	if (IS_ERR(req))
495 		return PTR_ERR(req);
496 
497 	req->in.h.opcode = FUSE_UNLINK;
498 	req->in.h.nodeid = get_node_id(dir);
499 	req->in.numargs = 1;
500 	req->in.args[0].size = entry->d_name.len + 1;
501 	req->in.args[0].value = entry->d_name.name;
502 	request_send(fc, req);
503 	err = req->out.h.error;
504 	fuse_put_request(fc, req);
505 	if (!err) {
506 		struct inode *inode = entry->d_inode;
507 
508 		/* Set nlink to zero so the inode can be cleared, if
509                    the inode does have more links this will be
510                    discovered at the next lookup/getattr */
511 		inode->i_nlink = 0;
512 		fuse_invalidate_attr(inode);
513 		fuse_invalidate_attr(dir);
514 		fuse_invalidate_entry_cache(entry);
515 	} else if (err == -EINTR)
516 		fuse_invalidate_entry(entry);
517 	return err;
518 }
519 
520 static int fuse_rmdir(struct inode *dir, struct dentry *entry)
521 {
522 	int err;
523 	struct fuse_conn *fc = get_fuse_conn(dir);
524 	struct fuse_req *req = fuse_get_req(fc);
525 	if (IS_ERR(req))
526 		return PTR_ERR(req);
527 
528 	req->in.h.opcode = FUSE_RMDIR;
529 	req->in.h.nodeid = get_node_id(dir);
530 	req->in.numargs = 1;
531 	req->in.args[0].size = entry->d_name.len + 1;
532 	req->in.args[0].value = entry->d_name.name;
533 	request_send(fc, req);
534 	err = req->out.h.error;
535 	fuse_put_request(fc, req);
536 	if (!err) {
537 		entry->d_inode->i_nlink = 0;
538 		fuse_invalidate_attr(dir);
539 		fuse_invalidate_entry_cache(entry);
540 	} else if (err == -EINTR)
541 		fuse_invalidate_entry(entry);
542 	return err;
543 }
544 
545 static int fuse_rename(struct inode *olddir, struct dentry *oldent,
546 		       struct inode *newdir, struct dentry *newent)
547 {
548 	int err;
549 	struct fuse_rename_in inarg;
550 	struct fuse_conn *fc = get_fuse_conn(olddir);
551 	struct fuse_req *req = fuse_get_req(fc);
552 	if (IS_ERR(req))
553 		return PTR_ERR(req);
554 
555 	memset(&inarg, 0, sizeof(inarg));
556 	inarg.newdir = get_node_id(newdir);
557 	req->in.h.opcode = FUSE_RENAME;
558 	req->in.h.nodeid = get_node_id(olddir);
559 	req->in.numargs = 3;
560 	req->in.args[0].size = sizeof(inarg);
561 	req->in.args[0].value = &inarg;
562 	req->in.args[1].size = oldent->d_name.len + 1;
563 	req->in.args[1].value = oldent->d_name.name;
564 	req->in.args[2].size = newent->d_name.len + 1;
565 	req->in.args[2].value = newent->d_name.name;
566 	request_send(fc, req);
567 	err = req->out.h.error;
568 	fuse_put_request(fc, req);
569 	if (!err) {
570 		fuse_invalidate_attr(olddir);
571 		if (olddir != newdir)
572 			fuse_invalidate_attr(newdir);
573 
574 		/* newent will end up negative */
575 		if (newent->d_inode)
576 			fuse_invalidate_entry_cache(newent);
577 	} else if (err == -EINTR) {
578 		/* If request was interrupted, DEITY only knows if the
579 		   rename actually took place.  If the invalidation
580 		   fails (e.g. some process has CWD under the renamed
581 		   directory), then there can be inconsistency between
582 		   the dcache and the real filesystem.  Tough luck. */
583 		fuse_invalidate_entry(oldent);
584 		if (newent->d_inode)
585 			fuse_invalidate_entry(newent);
586 	}
587 
588 	return err;
589 }
590 
591 static int fuse_link(struct dentry *entry, struct inode *newdir,
592 		     struct dentry *newent)
593 {
594 	int err;
595 	struct fuse_link_in inarg;
596 	struct inode *inode = entry->d_inode;
597 	struct fuse_conn *fc = get_fuse_conn(inode);
598 	struct fuse_req *req = fuse_get_req(fc);
599 	if (IS_ERR(req))
600 		return PTR_ERR(req);
601 
602 	memset(&inarg, 0, sizeof(inarg));
603 	inarg.oldnodeid = get_node_id(inode);
604 	req->in.h.opcode = FUSE_LINK;
605 	req->in.numargs = 2;
606 	req->in.args[0].size = sizeof(inarg);
607 	req->in.args[0].value = &inarg;
608 	req->in.args[1].size = newent->d_name.len + 1;
609 	req->in.args[1].value = newent->d_name.name;
610 	err = create_new_entry(fc, req, newdir, newent, inode->i_mode);
611 	/* Contrary to "normal" filesystems it can happen that link
612 	   makes two "logical" inodes point to the same "physical"
613 	   inode.  We invalidate the attributes of the old one, so it
614 	   will reflect changes in the backing inode (link count,
615 	   etc.)
616 	*/
617 	if (!err || err == -EINTR)
618 		fuse_invalidate_attr(inode);
619 	return err;
620 }
621 
622 int fuse_do_getattr(struct inode *inode)
623 {
624 	int err;
625 	struct fuse_attr_out arg;
626 	struct fuse_conn *fc = get_fuse_conn(inode);
627 	struct fuse_req *req = fuse_get_req(fc);
628 	if (IS_ERR(req))
629 		return PTR_ERR(req);
630 
631 	req->in.h.opcode = FUSE_GETATTR;
632 	req->in.h.nodeid = get_node_id(inode);
633 	req->out.numargs = 1;
634 	req->out.args[0].size = sizeof(arg);
635 	req->out.args[0].value = &arg;
636 	request_send(fc, req);
637 	err = req->out.h.error;
638 	fuse_put_request(fc, req);
639 	if (!err) {
640 		if ((inode->i_mode ^ arg.attr.mode) & S_IFMT) {
641 			make_bad_inode(inode);
642 			err = -EIO;
643 		} else {
644 			struct fuse_inode *fi = get_fuse_inode(inode);
645 			fuse_change_attributes(inode, &arg.attr);
646 			fi->i_time = time_to_jiffies(arg.attr_valid,
647 						     arg.attr_valid_nsec);
648 		}
649 	}
650 	return err;
651 }
652 
653 /*
654  * Calling into a user-controlled filesystem gives the filesystem
655  * daemon ptrace-like capabilities over the requester process.  This
656  * means, that the filesystem daemon is able to record the exact
657  * filesystem operations performed, and can also control the behavior
658  * of the requester process in otherwise impossible ways.  For example
659  * it can delay the operation for arbitrary length of time allowing
660  * DoS against the requester.
661  *
662  * For this reason only those processes can call into the filesystem,
663  * for which the owner of the mount has ptrace privilege.  This
664  * excludes processes started by other users, suid or sgid processes.
665  */
666 static int fuse_allow_task(struct fuse_conn *fc, struct task_struct *task)
667 {
668 	if (fc->flags & FUSE_ALLOW_OTHER)
669 		return 1;
670 
671 	if (task->euid == fc->user_id &&
672 	    task->suid == fc->user_id &&
673 	    task->uid == fc->user_id &&
674 	    task->egid == fc->group_id &&
675 	    task->sgid == fc->group_id &&
676 	    task->gid == fc->group_id)
677 		return 1;
678 
679 	return 0;
680 }
681 
682 /*
683  * Check whether the inode attributes are still valid
684  *
685  * If the attribute validity timeout has expired, then fetch the fresh
686  * attributes with a 'getattr' request
687  *
688  * I'm not sure why cached attributes are never returned for the root
689  * inode, this is probably being too cautious.
690  */
691 static int fuse_revalidate(struct dentry *entry)
692 {
693 	struct inode *inode = entry->d_inode;
694 	struct fuse_inode *fi = get_fuse_inode(inode);
695 	struct fuse_conn *fc = get_fuse_conn(inode);
696 
697 	if (!fuse_allow_task(fc, current))
698 		return -EACCES;
699 	if (get_node_id(inode) != FUSE_ROOT_ID &&
700 	    fi->i_time >= get_jiffies_64())
701 		return 0;
702 
703 	return fuse_do_getattr(inode);
704 }
705 
706 static int fuse_access(struct inode *inode, int mask)
707 {
708 	struct fuse_conn *fc = get_fuse_conn(inode);
709 	struct fuse_req *req;
710 	struct fuse_access_in inarg;
711 	int err;
712 
713 	if (fc->no_access)
714 		return 0;
715 
716 	req = fuse_get_req(fc);
717 	if (IS_ERR(req))
718 		return PTR_ERR(req);
719 
720 	memset(&inarg, 0, sizeof(inarg));
721 	inarg.mask = mask;
722 	req->in.h.opcode = FUSE_ACCESS;
723 	req->in.h.nodeid = get_node_id(inode);
724 	req->in.numargs = 1;
725 	req->in.args[0].size = sizeof(inarg);
726 	req->in.args[0].value = &inarg;
727 	request_send(fc, req);
728 	err = req->out.h.error;
729 	fuse_put_request(fc, req);
730 	if (err == -ENOSYS) {
731 		fc->no_access = 1;
732 		err = 0;
733 	}
734 	return err;
735 }
736 
737 /*
738  * Check permission.  The two basic access models of FUSE are:
739  *
740  * 1) Local access checking ('default_permissions' mount option) based
741  * on file mode.  This is the plain old disk filesystem permission
742  * modell.
743  *
744  * 2) "Remote" access checking, where server is responsible for
745  * checking permission in each inode operation.  An exception to this
746  * is if ->permission() was invoked from sys_access() in which case an
747  * access request is sent.  Execute permission is still checked
748  * locally based on file mode.
749  */
750 static int fuse_permission(struct inode *inode, int mask, struct nameidata *nd)
751 {
752 	struct fuse_conn *fc = get_fuse_conn(inode);
753 
754 	if (!fuse_allow_task(fc, current))
755 		return -EACCES;
756 	else if (fc->flags & FUSE_DEFAULT_PERMISSIONS) {
757 		int err = generic_permission(inode, mask, NULL);
758 
759 		/* If permission is denied, try to refresh file
760 		   attributes.  This is also needed, because the root
761 		   node will at first have no permissions */
762 		if (err == -EACCES) {
763 		 	err = fuse_do_getattr(inode);
764 			if (!err)
765 				err = generic_permission(inode, mask, NULL);
766 		}
767 
768 		/* Note: the opposite of the above test does not
769 		   exist.  So if permissions are revoked this won't be
770 		   noticed immediately, only after the attribute
771 		   timeout has expired */
772 
773 		return err;
774 	} else {
775 		int mode = inode->i_mode;
776 		if ((mask & MAY_EXEC) && !S_ISDIR(mode) && !(mode & S_IXUGO))
777 			return -EACCES;
778 
779 		if (nd && (nd->flags & LOOKUP_ACCESS))
780 			return fuse_access(inode, mask);
781 		return 0;
782 	}
783 }
784 
785 static int parse_dirfile(char *buf, size_t nbytes, struct file *file,
786 			 void *dstbuf, filldir_t filldir)
787 {
788 	while (nbytes >= FUSE_NAME_OFFSET) {
789 		struct fuse_dirent *dirent = (struct fuse_dirent *) buf;
790 		size_t reclen = FUSE_DIRENT_SIZE(dirent);
791 		int over;
792 		if (!dirent->namelen || dirent->namelen > FUSE_NAME_MAX)
793 			return -EIO;
794 		if (reclen > nbytes)
795 			break;
796 
797 		over = filldir(dstbuf, dirent->name, dirent->namelen,
798 			       file->f_pos, dirent->ino, dirent->type);
799 		if (over)
800 			break;
801 
802 		buf += reclen;
803 		nbytes -= reclen;
804 		file->f_pos = dirent->off;
805 	}
806 
807 	return 0;
808 }
809 
810 static int fuse_readdir(struct file *file, void *dstbuf, filldir_t filldir)
811 {
812 	int err;
813 	size_t nbytes;
814 	struct page *page;
815 	struct inode *inode = file->f_dentry->d_inode;
816 	struct fuse_conn *fc = get_fuse_conn(inode);
817 	struct fuse_req *req;
818 
819 	if (is_bad_inode(inode))
820 		return -EIO;
821 
822 	req = fuse_get_req(fc);
823 	if (IS_ERR(req))
824 		return PTR_ERR(req);
825 
826 	page = alloc_page(GFP_KERNEL);
827 	if (!page) {
828 		fuse_put_request(fc, req);
829 		return -ENOMEM;
830 	}
831 	req->num_pages = 1;
832 	req->pages[0] = page;
833 	fuse_read_fill(req, file, inode, file->f_pos, PAGE_SIZE, FUSE_READDIR);
834 	request_send(fc, req);
835 	nbytes = req->out.args[0].size;
836 	err = req->out.h.error;
837 	fuse_put_request(fc, req);
838 	if (!err)
839 		err = parse_dirfile(page_address(page), nbytes, file, dstbuf,
840 				    filldir);
841 
842 	__free_page(page);
843 	fuse_invalidate_attr(inode); /* atime changed */
844 	return err;
845 }
846 
847 static char *read_link(struct dentry *dentry)
848 {
849 	struct inode *inode = dentry->d_inode;
850 	struct fuse_conn *fc = get_fuse_conn(inode);
851 	struct fuse_req *req = fuse_get_req(fc);
852 	char *link;
853 
854 	if (IS_ERR(req))
855 		return ERR_PTR(PTR_ERR(req));
856 
857 	link = (char *) __get_free_page(GFP_KERNEL);
858 	if (!link) {
859 		link = ERR_PTR(-ENOMEM);
860 		goto out;
861 	}
862 	req->in.h.opcode = FUSE_READLINK;
863 	req->in.h.nodeid = get_node_id(inode);
864 	req->out.argvar = 1;
865 	req->out.numargs = 1;
866 	req->out.args[0].size = PAGE_SIZE - 1;
867 	req->out.args[0].value = link;
868 	request_send(fc, req);
869 	if (req->out.h.error) {
870 		free_page((unsigned long) link);
871 		link = ERR_PTR(req->out.h.error);
872 	} else
873 		link[req->out.args[0].size] = '\0';
874  out:
875 	fuse_put_request(fc, req);
876 	fuse_invalidate_attr(inode); /* atime changed */
877 	return link;
878 }
879 
880 static void free_link(char *link)
881 {
882 	if (!IS_ERR(link))
883 		free_page((unsigned long) link);
884 }
885 
886 static void *fuse_follow_link(struct dentry *dentry, struct nameidata *nd)
887 {
888 	nd_set_link(nd, read_link(dentry));
889 	return NULL;
890 }
891 
892 static void fuse_put_link(struct dentry *dentry, struct nameidata *nd, void *c)
893 {
894 	free_link(nd_get_link(nd));
895 }
896 
897 static int fuse_dir_open(struct inode *inode, struct file *file)
898 {
899 	return fuse_open_common(inode, file, 1);
900 }
901 
902 static int fuse_dir_release(struct inode *inode, struct file *file)
903 {
904 	return fuse_release_common(inode, file, 1);
905 }
906 
907 static int fuse_dir_fsync(struct file *file, struct dentry *de, int datasync)
908 {
909 	/* nfsd can call this with no file */
910 	return file ? fuse_fsync_common(file, de, datasync, 1) : 0;
911 }
912 
913 static void iattr_to_fattr(struct iattr *iattr, struct fuse_setattr_in *arg)
914 {
915 	unsigned ivalid = iattr->ia_valid;
916 
917 	if (ivalid & ATTR_MODE)
918 		arg->valid |= FATTR_MODE,   arg->mode = iattr->ia_mode;
919 	if (ivalid & ATTR_UID)
920 		arg->valid |= FATTR_UID,    arg->uid = iattr->ia_uid;
921 	if (ivalid & ATTR_GID)
922 		arg->valid |= FATTR_GID,    arg->gid = iattr->ia_gid;
923 	if (ivalid & ATTR_SIZE)
924 		arg->valid |= FATTR_SIZE,   arg->size = iattr->ia_size;
925 	/* You can only _set_ these together (they may change by themselves) */
926 	if ((ivalid & (ATTR_ATIME | ATTR_MTIME)) == (ATTR_ATIME | ATTR_MTIME)) {
927 		arg->valid |= FATTR_ATIME | FATTR_MTIME;
928 		arg->atime = iattr->ia_atime.tv_sec;
929 		arg->mtime = iattr->ia_mtime.tv_sec;
930 	}
931 	if (ivalid & ATTR_FILE) {
932 		struct fuse_file *ff = iattr->ia_file->private_data;
933 		arg->valid |= FATTR_FH;
934 		arg->fh = ff->fh;
935 	}
936 }
937 
938 /*
939  * Set attributes, and at the same time refresh them.
940  *
941  * Truncation is slightly complicated, because the 'truncate' request
942  * may fail, in which case we don't want to touch the mapping.
943  * vmtruncate() doesn't allow for this case.  So do the rlimit
944  * checking by hand and call vmtruncate() only after the file has
945  * actually been truncated.
946  */
947 static int fuse_setattr(struct dentry *entry, struct iattr *attr)
948 {
949 	struct inode *inode = entry->d_inode;
950 	struct fuse_conn *fc = get_fuse_conn(inode);
951 	struct fuse_inode *fi = get_fuse_inode(inode);
952 	struct fuse_req *req;
953 	struct fuse_setattr_in inarg;
954 	struct fuse_attr_out outarg;
955 	int err;
956 	int is_truncate = 0;
957 
958 	if (fc->flags & FUSE_DEFAULT_PERMISSIONS) {
959 		err = inode_change_ok(inode, attr);
960 		if (err)
961 			return err;
962 	}
963 
964 	if (attr->ia_valid & ATTR_SIZE) {
965 		unsigned long limit;
966 		is_truncate = 1;
967 		limit = current->signal->rlim[RLIMIT_FSIZE].rlim_cur;
968 		if (limit != RLIM_INFINITY && attr->ia_size > (loff_t) limit) {
969 			send_sig(SIGXFSZ, current, 0);
970 			return -EFBIG;
971 		}
972 	}
973 
974 	req = fuse_get_req(fc);
975 	if (IS_ERR(req))
976 		return PTR_ERR(req);
977 
978 	memset(&inarg, 0, sizeof(inarg));
979 	iattr_to_fattr(attr, &inarg);
980 	req->in.h.opcode = FUSE_SETATTR;
981 	req->in.h.nodeid = get_node_id(inode);
982 	req->in.numargs = 1;
983 	req->in.args[0].size = sizeof(inarg);
984 	req->in.args[0].value = &inarg;
985 	req->out.numargs = 1;
986 	req->out.args[0].size = sizeof(outarg);
987 	req->out.args[0].value = &outarg;
988 	request_send(fc, req);
989 	err = req->out.h.error;
990 	fuse_put_request(fc, req);
991 	if (!err) {
992 		if ((inode->i_mode ^ outarg.attr.mode) & S_IFMT) {
993 			make_bad_inode(inode);
994 			err = -EIO;
995 		} else {
996 			if (is_truncate) {
997 				loff_t origsize = i_size_read(inode);
998 				i_size_write(inode, outarg.attr.size);
999 				if (origsize > outarg.attr.size)
1000 					vmtruncate(inode, outarg.attr.size);
1001 			}
1002 			fuse_change_attributes(inode, &outarg.attr);
1003 			fi->i_time = time_to_jiffies(outarg.attr_valid,
1004 						     outarg.attr_valid_nsec);
1005 		}
1006 	} else if (err == -EINTR)
1007 		fuse_invalidate_attr(inode);
1008 
1009 	return err;
1010 }
1011 
1012 static int fuse_getattr(struct vfsmount *mnt, struct dentry *entry,
1013 			struct kstat *stat)
1014 {
1015 	struct inode *inode = entry->d_inode;
1016 	int err = fuse_revalidate(entry);
1017 	if (!err)
1018 		generic_fillattr(inode, stat);
1019 
1020 	return err;
1021 }
1022 
1023 static int fuse_setxattr(struct dentry *entry, const char *name,
1024 			 const void *value, size_t size, int flags)
1025 {
1026 	struct inode *inode = entry->d_inode;
1027 	struct fuse_conn *fc = get_fuse_conn(inode);
1028 	struct fuse_req *req;
1029 	struct fuse_setxattr_in inarg;
1030 	int err;
1031 
1032 	if (fc->no_setxattr)
1033 		return -EOPNOTSUPP;
1034 
1035 	req = fuse_get_req(fc);
1036 	if (IS_ERR(req))
1037 		return PTR_ERR(req);
1038 
1039 	memset(&inarg, 0, sizeof(inarg));
1040 	inarg.size = size;
1041 	inarg.flags = flags;
1042 	req->in.h.opcode = FUSE_SETXATTR;
1043 	req->in.h.nodeid = get_node_id(inode);
1044 	req->in.numargs = 3;
1045 	req->in.args[0].size = sizeof(inarg);
1046 	req->in.args[0].value = &inarg;
1047 	req->in.args[1].size = strlen(name) + 1;
1048 	req->in.args[1].value = name;
1049 	req->in.args[2].size = size;
1050 	req->in.args[2].value = value;
1051 	request_send(fc, req);
1052 	err = req->out.h.error;
1053 	fuse_put_request(fc, req);
1054 	if (err == -ENOSYS) {
1055 		fc->no_setxattr = 1;
1056 		err = -EOPNOTSUPP;
1057 	}
1058 	return err;
1059 }
1060 
1061 static ssize_t fuse_getxattr(struct dentry *entry, const char *name,
1062 			     void *value, size_t size)
1063 {
1064 	struct inode *inode = entry->d_inode;
1065 	struct fuse_conn *fc = get_fuse_conn(inode);
1066 	struct fuse_req *req;
1067 	struct fuse_getxattr_in inarg;
1068 	struct fuse_getxattr_out outarg;
1069 	ssize_t ret;
1070 
1071 	if (fc->no_getxattr)
1072 		return -EOPNOTSUPP;
1073 
1074 	req = fuse_get_req(fc);
1075 	if (IS_ERR(req))
1076 		return PTR_ERR(req);
1077 
1078 	memset(&inarg, 0, sizeof(inarg));
1079 	inarg.size = size;
1080 	req->in.h.opcode = FUSE_GETXATTR;
1081 	req->in.h.nodeid = get_node_id(inode);
1082 	req->in.numargs = 2;
1083 	req->in.args[0].size = sizeof(inarg);
1084 	req->in.args[0].value = &inarg;
1085 	req->in.args[1].size = strlen(name) + 1;
1086 	req->in.args[1].value = name;
1087 	/* This is really two different operations rolled into one */
1088 	req->out.numargs = 1;
1089 	if (size) {
1090 		req->out.argvar = 1;
1091 		req->out.args[0].size = size;
1092 		req->out.args[0].value = value;
1093 	} else {
1094 		req->out.args[0].size = sizeof(outarg);
1095 		req->out.args[0].value = &outarg;
1096 	}
1097 	request_send(fc, req);
1098 	ret = req->out.h.error;
1099 	if (!ret)
1100 		ret = size ? req->out.args[0].size : outarg.size;
1101 	else {
1102 		if (ret == -ENOSYS) {
1103 			fc->no_getxattr = 1;
1104 			ret = -EOPNOTSUPP;
1105 		}
1106 	}
1107 	fuse_put_request(fc, req);
1108 	return ret;
1109 }
1110 
1111 static ssize_t fuse_listxattr(struct dentry *entry, char *list, size_t size)
1112 {
1113 	struct inode *inode = entry->d_inode;
1114 	struct fuse_conn *fc = get_fuse_conn(inode);
1115 	struct fuse_req *req;
1116 	struct fuse_getxattr_in inarg;
1117 	struct fuse_getxattr_out outarg;
1118 	ssize_t ret;
1119 
1120 	if (fc->no_listxattr)
1121 		return -EOPNOTSUPP;
1122 
1123 	req = fuse_get_req(fc);
1124 	if (IS_ERR(req))
1125 		return PTR_ERR(req);
1126 
1127 	memset(&inarg, 0, sizeof(inarg));
1128 	inarg.size = size;
1129 	req->in.h.opcode = FUSE_LISTXATTR;
1130 	req->in.h.nodeid = get_node_id(inode);
1131 	req->in.numargs = 1;
1132 	req->in.args[0].size = sizeof(inarg);
1133 	req->in.args[0].value = &inarg;
1134 	/* This is really two different operations rolled into one */
1135 	req->out.numargs = 1;
1136 	if (size) {
1137 		req->out.argvar = 1;
1138 		req->out.args[0].size = size;
1139 		req->out.args[0].value = list;
1140 	} else {
1141 		req->out.args[0].size = sizeof(outarg);
1142 		req->out.args[0].value = &outarg;
1143 	}
1144 	request_send(fc, req);
1145 	ret = req->out.h.error;
1146 	if (!ret)
1147 		ret = size ? req->out.args[0].size : outarg.size;
1148 	else {
1149 		if (ret == -ENOSYS) {
1150 			fc->no_listxattr = 1;
1151 			ret = -EOPNOTSUPP;
1152 		}
1153 	}
1154 	fuse_put_request(fc, req);
1155 	return ret;
1156 }
1157 
1158 static int fuse_removexattr(struct dentry *entry, const char *name)
1159 {
1160 	struct inode *inode = entry->d_inode;
1161 	struct fuse_conn *fc = get_fuse_conn(inode);
1162 	struct fuse_req *req;
1163 	int err;
1164 
1165 	if (fc->no_removexattr)
1166 		return -EOPNOTSUPP;
1167 
1168 	req = fuse_get_req(fc);
1169 	if (IS_ERR(req))
1170 		return PTR_ERR(req);
1171 
1172 	req->in.h.opcode = FUSE_REMOVEXATTR;
1173 	req->in.h.nodeid = get_node_id(inode);
1174 	req->in.numargs = 1;
1175 	req->in.args[0].size = strlen(name) + 1;
1176 	req->in.args[0].value = name;
1177 	request_send(fc, req);
1178 	err = req->out.h.error;
1179 	fuse_put_request(fc, req);
1180 	if (err == -ENOSYS) {
1181 		fc->no_removexattr = 1;
1182 		err = -EOPNOTSUPP;
1183 	}
1184 	return err;
1185 }
1186 
1187 static struct inode_operations fuse_dir_inode_operations = {
1188 	.lookup		= fuse_lookup,
1189 	.mkdir		= fuse_mkdir,
1190 	.symlink	= fuse_symlink,
1191 	.unlink		= fuse_unlink,
1192 	.rmdir		= fuse_rmdir,
1193 	.rename		= fuse_rename,
1194 	.link		= fuse_link,
1195 	.setattr	= fuse_setattr,
1196 	.create		= fuse_create,
1197 	.mknod		= fuse_mknod,
1198 	.permission	= fuse_permission,
1199 	.getattr	= fuse_getattr,
1200 	.setxattr	= fuse_setxattr,
1201 	.getxattr	= fuse_getxattr,
1202 	.listxattr	= fuse_listxattr,
1203 	.removexattr	= fuse_removexattr,
1204 };
1205 
1206 static const struct file_operations fuse_dir_operations = {
1207 	.llseek		= generic_file_llseek,
1208 	.read		= generic_read_dir,
1209 	.readdir	= fuse_readdir,
1210 	.open		= fuse_dir_open,
1211 	.release	= fuse_dir_release,
1212 	.fsync		= fuse_dir_fsync,
1213 };
1214 
1215 static struct inode_operations fuse_common_inode_operations = {
1216 	.setattr	= fuse_setattr,
1217 	.permission	= fuse_permission,
1218 	.getattr	= fuse_getattr,
1219 	.setxattr	= fuse_setxattr,
1220 	.getxattr	= fuse_getxattr,
1221 	.listxattr	= fuse_listxattr,
1222 	.removexattr	= fuse_removexattr,
1223 };
1224 
1225 static struct inode_operations fuse_symlink_inode_operations = {
1226 	.setattr	= fuse_setattr,
1227 	.follow_link	= fuse_follow_link,
1228 	.put_link	= fuse_put_link,
1229 	.readlink	= generic_readlink,
1230 	.getattr	= fuse_getattr,
1231 	.setxattr	= fuse_setxattr,
1232 	.getxattr	= fuse_getxattr,
1233 	.listxattr	= fuse_listxattr,
1234 	.removexattr	= fuse_removexattr,
1235 };
1236 
1237 void fuse_init_common(struct inode *inode)
1238 {
1239 	inode->i_op = &fuse_common_inode_operations;
1240 }
1241 
1242 void fuse_init_dir(struct inode *inode)
1243 {
1244 	inode->i_op = &fuse_dir_inode_operations;
1245 	inode->i_fop = &fuse_dir_operations;
1246 }
1247 
1248 void fuse_init_symlink(struct inode *inode)
1249 {
1250 	inode->i_op = &fuse_symlink_inode_operations;
1251 }
1252