• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2   FUSE: Filesystem in Userspace
3   Copyright (C) 2001-2008  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/sched.h>
14 #include <linux/namei.h>
15 #include <linux/slab.h>
16 
fuse_use_readdirplus(struct inode * dir,struct dir_context * ctx)17 static bool fuse_use_readdirplus(struct inode *dir, struct dir_context *ctx)
18 {
19 	struct fuse_conn *fc = get_fuse_conn(dir);
20 	struct fuse_inode *fi = get_fuse_inode(dir);
21 
22 	if (!fc->do_readdirplus)
23 		return false;
24 	if (!fc->readdirplus_auto)
25 		return true;
26 	if (test_and_clear_bit(FUSE_I_ADVISE_RDPLUS, &fi->state))
27 		return true;
28 	if (ctx->pos == 0)
29 		return true;
30 	return false;
31 }
32 
fuse_advise_use_readdirplus(struct inode * dir)33 static void fuse_advise_use_readdirplus(struct inode *dir)
34 {
35 	struct fuse_inode *fi = get_fuse_inode(dir);
36 
37 	set_bit(FUSE_I_ADVISE_RDPLUS, &fi->state);
38 }
39 
40 #if BITS_PER_LONG >= 64
fuse_dentry_settime(struct dentry * entry,u64 time)41 static inline void fuse_dentry_settime(struct dentry *entry, u64 time)
42 {
43 	entry->d_time = time;
44 }
45 
fuse_dentry_time(struct dentry * entry)46 static inline u64 fuse_dentry_time(struct dentry *entry)
47 {
48 	return entry->d_time;
49 }
50 #else
51 /*
52  * On 32 bit archs store the high 32 bits of time in d_fsdata
53  */
fuse_dentry_settime(struct dentry * entry,u64 time)54 static void fuse_dentry_settime(struct dentry *entry, u64 time)
55 {
56 	entry->d_time = time;
57 	entry->d_fsdata = (void *) (unsigned long) (time >> 32);
58 }
59 
fuse_dentry_time(struct dentry * entry)60 static u64 fuse_dentry_time(struct dentry *entry)
61 {
62 	return (u64) entry->d_time +
63 		((u64) (unsigned long) entry->d_fsdata << 32);
64 }
65 #endif
66 
67 /*
68  * FUSE caches dentries and attributes with separate timeout.  The
69  * time in jiffies until the dentry/attributes are valid is stored in
70  * dentry->d_time and fuse_inode->i_time respectively.
71  */
72 
73 /*
74  * Calculate the time in jiffies until a dentry/attributes are valid
75  */
time_to_jiffies(unsigned long sec,unsigned long nsec)76 static u64 time_to_jiffies(unsigned long sec, unsigned long nsec)
77 {
78 	if (sec || nsec) {
79 		struct timespec ts = {sec, nsec};
80 		return get_jiffies_64() + timespec_to_jiffies(&ts);
81 	} else
82 		return 0;
83 }
84 
85 /*
86  * Set dentry and possibly attribute timeouts from the lookup/mk*
87  * replies
88  */
fuse_change_entry_timeout(struct dentry * entry,struct fuse_entry_out * o)89 static void fuse_change_entry_timeout(struct dentry *entry,
90 				      struct fuse_entry_out *o)
91 {
92 	fuse_dentry_settime(entry,
93 		time_to_jiffies(o->entry_valid, o->entry_valid_nsec));
94 }
95 
attr_timeout(struct fuse_attr_out * o)96 static u64 attr_timeout(struct fuse_attr_out *o)
97 {
98 	return time_to_jiffies(o->attr_valid, o->attr_valid_nsec);
99 }
100 
entry_attr_timeout(struct fuse_entry_out * o)101 static u64 entry_attr_timeout(struct fuse_entry_out *o)
102 {
103 	return time_to_jiffies(o->attr_valid, o->attr_valid_nsec);
104 }
105 
106 /*
107  * Mark the attributes as stale, so that at the next call to
108  * ->getattr() they will be fetched from userspace
109  */
fuse_invalidate_attr(struct inode * inode)110 void fuse_invalidate_attr(struct inode *inode)
111 {
112 	get_fuse_inode(inode)->i_time = 0;
113 }
114 
115 /**
116  * Mark the attributes as stale due to an atime change.  Avoid the invalidate if
117  * atime is not used.
118  */
fuse_invalidate_atime(struct inode * inode)119 void fuse_invalidate_atime(struct inode *inode)
120 {
121 	if (!IS_RDONLY(inode))
122 		fuse_invalidate_attr(inode);
123 }
124 
125 /*
126  * Just mark the entry as stale, so that a next attempt to look it up
127  * will result in a new lookup call to userspace
128  *
129  * This is called when a dentry is about to become negative and the
130  * timeout is unknown (unlink, rmdir, rename and in some cases
131  * lookup)
132  */
fuse_invalidate_entry_cache(struct dentry * entry)133 void fuse_invalidate_entry_cache(struct dentry *entry)
134 {
135 	fuse_dentry_settime(entry, 0);
136 }
137 
138 /*
139  * Same as fuse_invalidate_entry_cache(), but also try to remove the
140  * dentry from the hash
141  */
fuse_invalidate_entry(struct dentry * entry)142 static void fuse_invalidate_entry(struct dentry *entry)
143 {
144 	d_invalidate(entry);
145 	fuse_invalidate_entry_cache(entry);
146 }
147 
fuse_lookup_init(struct fuse_conn * fc,struct fuse_req * req,u64 nodeid,struct qstr * name,struct fuse_entry_out * outarg)148 static void fuse_lookup_init(struct fuse_conn *fc, struct fuse_req *req,
149 			     u64 nodeid, struct qstr *name,
150 			     struct fuse_entry_out *outarg)
151 {
152 	memset(outarg, 0, sizeof(struct fuse_entry_out));
153 	req->in.h.opcode = FUSE_LOOKUP;
154 	req->in.h.nodeid = nodeid;
155 	req->in.numargs = 1;
156 	req->in.args[0].size = name->len + 1;
157 	req->in.args[0].value = name->name;
158 	req->out.numargs = 1;
159 	if (fc->minor < 9)
160 		req->out.args[0].size = FUSE_COMPAT_ENTRY_OUT_SIZE;
161 	else
162 		req->out.args[0].size = sizeof(struct fuse_entry_out);
163 	req->out.args[0].value = outarg;
164 }
165 
fuse_get_attr_version(struct fuse_conn * fc)166 u64 fuse_get_attr_version(struct fuse_conn *fc)
167 {
168 	u64 curr_version;
169 
170 	/*
171 	 * The spin lock isn't actually needed on 64bit archs, but we
172 	 * don't yet care too much about such optimizations.
173 	 */
174 	spin_lock(&fc->lock);
175 	curr_version = fc->attr_version;
176 	spin_unlock(&fc->lock);
177 
178 	return curr_version;
179 }
180 
181 /*
182  * Check whether the dentry is still valid
183  *
184  * If the entry validity timeout has expired and the dentry is
185  * positive, try to redo the lookup.  If the lookup results in a
186  * different inode, then let the VFS invalidate the dentry and redo
187  * the lookup once more.  If the lookup results in the same inode,
188  * then refresh the attributes, timeouts and mark the dentry valid.
189  */
fuse_dentry_revalidate(struct dentry * entry,unsigned int flags)190 static int fuse_dentry_revalidate(struct dentry *entry, unsigned int flags)
191 {
192 	struct inode *inode;
193 	struct dentry *parent;
194 	struct fuse_conn *fc;
195 	struct fuse_inode *fi;
196 	int ret;
197 
198 	inode = ACCESS_ONCE(entry->d_inode);
199 	if (inode && is_bad_inode(inode))
200 		goto invalid;
201 	else if (time_before64(fuse_dentry_time(entry), get_jiffies_64()) ||
202 		 (flags & LOOKUP_REVAL)) {
203 		int err;
204 		struct fuse_entry_out outarg;
205 		struct fuse_req *req;
206 		struct fuse_forget_link *forget;
207 		u64 attr_version;
208 
209 		/* For negative dentries, always do a fresh lookup */
210 		if (!inode)
211 			goto invalid;
212 
213 		ret = -ECHILD;
214 		if (flags & LOOKUP_RCU)
215 			goto out;
216 
217 		fc = get_fuse_conn(inode);
218 		req = fuse_get_req_nopages(fc);
219 		ret = PTR_ERR(req);
220 		if (IS_ERR(req))
221 			goto out;
222 
223 		forget = fuse_alloc_forget();
224 		if (!forget) {
225 			fuse_put_request(fc, req);
226 			ret = -ENOMEM;
227 			goto out;
228 		}
229 
230 		attr_version = fuse_get_attr_version(fc);
231 
232 		parent = dget_parent(entry);
233 		fuse_lookup_init(fc, req, get_node_id(parent->d_inode),
234 				 &entry->d_name, &outarg);
235 		fuse_request_send(fc, req);
236 		dput(parent);
237 		err = req->out.h.error;
238 		fuse_put_request(fc, req);
239 		/* Zero nodeid is same as -ENOENT */
240 		if (!err && !outarg.nodeid)
241 			err = -ENOENT;
242 		if (!err) {
243 			fi = get_fuse_inode(inode);
244 			if (outarg.nodeid != get_node_id(inode)) {
245 				fuse_queue_forget(fc, forget, outarg.nodeid, 1);
246 				goto invalid;
247 			}
248 			spin_lock(&fc->lock);
249 			fi->nlookup++;
250 			spin_unlock(&fc->lock);
251 		}
252 		kfree(forget);
253 		if (err || (outarg.attr.mode ^ inode->i_mode) & S_IFMT)
254 			goto invalid;
255 
256 		fuse_change_attributes(inode, &outarg.attr,
257 				       entry_attr_timeout(&outarg),
258 				       attr_version);
259 		fuse_change_entry_timeout(entry, &outarg);
260 	} else if (inode) {
261 		fi = get_fuse_inode(inode);
262 		if (flags & LOOKUP_RCU) {
263 			if (test_bit(FUSE_I_INIT_RDPLUS, &fi->state))
264 				return -ECHILD;
265 		} else if (test_and_clear_bit(FUSE_I_INIT_RDPLUS, &fi->state)) {
266 			parent = dget_parent(entry);
267 			fuse_advise_use_readdirplus(parent->d_inode);
268 			dput(parent);
269 		}
270 	}
271 	ret = 1;
272 out:
273 	return ret;
274 
275 invalid:
276 	ret = 0;
277 	goto out;
278 }
279 
280 /*
281  * Get the canonical path. Since we must translate to a path, this must be done
282  * in the context of the userspace daemon, however, the userspace daemon cannot
283  * look up paths on its own. Instead, we handle the lookup as a special case
284  * inside of the write request.
285  */
fuse_dentry_canonical_path(const struct path * path,struct path * canonical_path)286 static void fuse_dentry_canonical_path(const struct path *path, struct path *canonical_path) {
287 	struct inode *inode = path->dentry->d_inode;
288 	struct fuse_conn *fc = get_fuse_conn(inode);
289 	struct fuse_req *req;
290 	int err;
291 	char *path_name;
292 
293 	req = fuse_get_req(fc, 1);
294 	err = PTR_ERR(req);
295 	if (IS_ERR(req))
296 		goto default_path;
297 
298 	path_name = (char*)__get_free_page(GFP_KERNEL);
299 	if (!path_name) {
300 		fuse_put_request(fc, req);
301 		goto default_path;
302 	}
303 
304 	req->in.h.opcode = FUSE_CANONICAL_PATH;
305 	req->in.h.nodeid = get_node_id(inode);
306 	req->in.numargs = 0;
307 	req->out.numargs = 1;
308 	req->out.args[0].size = PATH_MAX;
309 	req->out.args[0].value = path_name;
310 	req->canonical_path = canonical_path;
311 	req->out.argvar = 1;
312 	fuse_request_send(fc, req);
313 	err = req->out.h.error;
314 	fuse_put_request(fc, req);
315 	free_page((unsigned long)path_name);
316 	if (!err)
317 		return;
318 default_path:
319 	canonical_path->dentry = path->dentry;
320 	canonical_path->mnt = path->mnt;
321 	path_get(canonical_path);
322 }
323 
invalid_nodeid(u64 nodeid)324 static int invalid_nodeid(u64 nodeid)
325 {
326 	return !nodeid || nodeid == FUSE_ROOT_ID;
327 }
328 
329 const struct dentry_operations fuse_dentry_operations = {
330 	.d_revalidate	= fuse_dentry_revalidate,
331 	.d_canonical_path = fuse_dentry_canonical_path,
332 };
333 
fuse_valid_type(int m)334 int fuse_valid_type(int m)
335 {
336 	return S_ISREG(m) || S_ISDIR(m) || S_ISLNK(m) || S_ISCHR(m) ||
337 		S_ISBLK(m) || S_ISFIFO(m) || S_ISSOCK(m);
338 }
339 
fuse_lookup_name(struct super_block * sb,u64 nodeid,struct qstr * name,struct fuse_entry_out * outarg,struct inode ** inode)340 int fuse_lookup_name(struct super_block *sb, u64 nodeid, struct qstr *name,
341 		     struct fuse_entry_out *outarg, struct inode **inode)
342 {
343 	struct fuse_conn *fc = get_fuse_conn_super(sb);
344 	struct fuse_req *req;
345 	struct fuse_forget_link *forget;
346 	u64 attr_version;
347 	int err;
348 
349 	*inode = NULL;
350 	err = -ENAMETOOLONG;
351 	if (name->len > FUSE_NAME_MAX)
352 		goto out;
353 
354 	req = fuse_get_req_nopages(fc);
355 	err = PTR_ERR(req);
356 	if (IS_ERR(req))
357 		goto out;
358 
359 	forget = fuse_alloc_forget();
360 	err = -ENOMEM;
361 	if (!forget) {
362 		fuse_put_request(fc, req);
363 		goto out;
364 	}
365 
366 	attr_version = fuse_get_attr_version(fc);
367 
368 	fuse_lookup_init(fc, req, nodeid, name, outarg);
369 	fuse_request_send(fc, req);
370 	err = req->out.h.error;
371 	fuse_put_request(fc, req);
372 	/* Zero nodeid is same as -ENOENT, but with valid timeout */
373 	if (err || !outarg->nodeid)
374 		goto out_put_forget;
375 
376 	err = -EIO;
377 	if (!outarg->nodeid)
378 		goto out_put_forget;
379 	if (!fuse_valid_type(outarg->attr.mode))
380 		goto out_put_forget;
381 
382 	*inode = fuse_iget(sb, outarg->nodeid, outarg->generation,
383 			   &outarg->attr, entry_attr_timeout(outarg),
384 			   attr_version);
385 	err = -ENOMEM;
386 	if (!*inode) {
387 		fuse_queue_forget(fc, forget, outarg->nodeid, 1);
388 		goto out;
389 	}
390 	err = 0;
391 
392  out_put_forget:
393 	kfree(forget);
394  out:
395 	return err;
396 }
397 
fuse_lookup(struct inode * dir,struct dentry * entry,unsigned int flags)398 static struct dentry *fuse_lookup(struct inode *dir, struct dentry *entry,
399 				  unsigned int flags)
400 {
401 	int err;
402 	struct fuse_entry_out outarg;
403 	struct inode *inode;
404 	struct dentry *newent;
405 	bool outarg_valid = true;
406 
407 	err = fuse_lookup_name(dir->i_sb, get_node_id(dir), &entry->d_name,
408 			       &outarg, &inode);
409 	if (err == -ENOENT) {
410 		outarg_valid = false;
411 		err = 0;
412 	}
413 	if (err)
414 		goto out_err;
415 
416 	err = -EIO;
417 	if (inode && get_node_id(inode) == FUSE_ROOT_ID)
418 		goto out_iput;
419 
420 	newent = d_materialise_unique(entry, inode);
421 	err = PTR_ERR(newent);
422 	if (IS_ERR(newent))
423 		goto out_err;
424 
425 	entry = newent ? newent : entry;
426 	if (outarg_valid)
427 		fuse_change_entry_timeout(entry, &outarg);
428 	else
429 		fuse_invalidate_entry_cache(entry);
430 
431 	fuse_advise_use_readdirplus(dir);
432 	return newent;
433 
434  out_iput:
435 	iput(inode);
436  out_err:
437 	return ERR_PTR(err);
438 }
439 
440 /*
441  * Atomic create+open operation
442  *
443  * If the filesystem doesn't support this, then fall back to separate
444  * 'mknod' + 'open' requests.
445  */
fuse_create_open(struct inode * dir,struct dentry * entry,struct file * file,unsigned flags,umode_t mode,int * opened)446 static int fuse_create_open(struct inode *dir, struct dentry *entry,
447 			    struct file *file, unsigned flags,
448 			    umode_t mode, int *opened)
449 {
450 	int err;
451 	struct inode *inode;
452 	struct fuse_conn *fc = get_fuse_conn(dir);
453 	struct fuse_req *req;
454 	struct fuse_forget_link *forget;
455 	struct fuse_create_in inarg;
456 	struct fuse_open_out outopen;
457 	struct fuse_entry_out outentry;
458 	struct fuse_file *ff;
459 
460 	/* Userspace expects S_IFREG in create mode */
461 	BUG_ON((mode & S_IFMT) != S_IFREG);
462 
463 	forget = fuse_alloc_forget();
464 	err = -ENOMEM;
465 	if (!forget)
466 		goto out_err;
467 
468 	req = fuse_get_req_nopages(fc);
469 	err = PTR_ERR(req);
470 	if (IS_ERR(req))
471 		goto out_put_forget_req;
472 
473 	err = -ENOMEM;
474 	ff = fuse_file_alloc(fc);
475 	if (!ff)
476 		goto out_put_request;
477 
478 	if (!fc->dont_mask)
479 		mode &= ~current_umask();
480 
481 	flags &= ~O_NOCTTY;
482 	memset(&inarg, 0, sizeof(inarg));
483 	memset(&outentry, 0, sizeof(outentry));
484 	inarg.flags = flags;
485 	inarg.mode = mode;
486 	inarg.umask = current_umask();
487 	req->in.h.opcode = FUSE_CREATE;
488 	req->in.h.nodeid = get_node_id(dir);
489 	req->in.numargs = 2;
490 	req->in.args[0].size = fc->minor < 12 ? sizeof(struct fuse_open_in) :
491 						sizeof(inarg);
492 	req->in.args[0].value = &inarg;
493 	req->in.args[1].size = entry->d_name.len + 1;
494 	req->in.args[1].value = entry->d_name.name;
495 	req->out.numargs = 2;
496 	if (fc->minor < 9)
497 		req->out.args[0].size = FUSE_COMPAT_ENTRY_OUT_SIZE;
498 	else
499 		req->out.args[0].size = sizeof(outentry);
500 	req->out.args[0].value = &outentry;
501 	req->out.args[1].size = sizeof(outopen);
502 	req->out.args[1].value = &outopen;
503 	fuse_request_send(fc, req);
504 	err = req->out.h.error;
505 	if (err)
506 		goto out_free_ff;
507 
508 	err = -EIO;
509 	if (!S_ISREG(outentry.attr.mode) || invalid_nodeid(outentry.nodeid))
510 		goto out_free_ff;
511 
512 	fuse_put_request(fc, req);
513 	ff->fh = outopen.fh;
514 	ff->nodeid = outentry.nodeid;
515 	ff->open_flags = outopen.open_flags;
516 	inode = fuse_iget(dir->i_sb, outentry.nodeid, outentry.generation,
517 			  &outentry.attr, entry_attr_timeout(&outentry), 0);
518 	if (!inode) {
519 		flags &= ~(O_CREAT | O_EXCL | O_TRUNC);
520 		fuse_sync_release(ff, flags);
521 		fuse_queue_forget(fc, forget, outentry.nodeid, 1);
522 		err = -ENOMEM;
523 		goto out_err;
524 	}
525 	kfree(forget);
526 	d_instantiate(entry, inode);
527 	fuse_change_entry_timeout(entry, &outentry);
528 	fuse_invalidate_attr(dir);
529 	err = finish_open(file, entry, generic_file_open, opened);
530 	if (err) {
531 		fuse_sync_release(ff, flags);
532 	} else {
533 		file->private_data = fuse_file_get(ff);
534 		fuse_finish_open(inode, file);
535 	}
536 	return err;
537 
538 out_free_ff:
539 	fuse_file_free(ff);
540 out_put_request:
541 	fuse_put_request(fc, req);
542 out_put_forget_req:
543 	kfree(forget);
544 out_err:
545 	return err;
546 }
547 
548 static int fuse_mknod(struct inode *, struct dentry *, umode_t, dev_t);
fuse_atomic_open(struct inode * dir,struct dentry * entry,struct file * file,unsigned flags,umode_t mode,int * opened)549 static int fuse_atomic_open(struct inode *dir, struct dentry *entry,
550 			    struct file *file, unsigned flags,
551 			    umode_t mode, int *opened)
552 {
553 	int err;
554 	struct fuse_conn *fc = get_fuse_conn(dir);
555 	struct dentry *res = NULL;
556 
557 	if (d_unhashed(entry)) {
558 		res = fuse_lookup(dir, entry, 0);
559 		if (IS_ERR(res))
560 			return PTR_ERR(res);
561 
562 		if (res)
563 			entry = res;
564 	}
565 
566 	if (!(flags & O_CREAT) || entry->d_inode)
567 		goto no_open;
568 
569 	/* Only creates */
570 	*opened |= FILE_CREATED;
571 
572 	if (fc->no_create)
573 		goto mknod;
574 
575 	err = fuse_create_open(dir, entry, file, flags, mode, opened);
576 	if (err == -ENOSYS) {
577 		fc->no_create = 1;
578 		goto mknod;
579 	}
580 out_dput:
581 	dput(res);
582 	return err;
583 
584 mknod:
585 	err = fuse_mknod(dir, entry, mode, 0);
586 	if (err)
587 		goto out_dput;
588 no_open:
589 	return finish_no_open(file, res);
590 }
591 
592 /*
593  * Code shared between mknod, mkdir, symlink and link
594  */
create_new_entry(struct fuse_conn * fc,struct fuse_req * req,struct inode * dir,struct dentry * entry,umode_t mode)595 static int create_new_entry(struct fuse_conn *fc, struct fuse_req *req,
596 			    struct inode *dir, struct dentry *entry,
597 			    umode_t mode)
598 {
599 	struct fuse_entry_out outarg;
600 	struct inode *inode;
601 	int err;
602 	struct fuse_forget_link *forget;
603 
604 	forget = fuse_alloc_forget();
605 	if (!forget) {
606 		fuse_put_request(fc, req);
607 		return -ENOMEM;
608 	}
609 
610 	memset(&outarg, 0, sizeof(outarg));
611 	req->in.h.nodeid = get_node_id(dir);
612 	req->out.numargs = 1;
613 	if (fc->minor < 9)
614 		req->out.args[0].size = FUSE_COMPAT_ENTRY_OUT_SIZE;
615 	else
616 		req->out.args[0].size = sizeof(outarg);
617 	req->out.args[0].value = &outarg;
618 	fuse_request_send(fc, req);
619 	err = req->out.h.error;
620 	fuse_put_request(fc, req);
621 	if (err)
622 		goto out_put_forget_req;
623 
624 	err = -EIO;
625 	if (invalid_nodeid(outarg.nodeid))
626 		goto out_put_forget_req;
627 
628 	if ((outarg.attr.mode ^ mode) & S_IFMT)
629 		goto out_put_forget_req;
630 
631 	inode = fuse_iget(dir->i_sb, outarg.nodeid, outarg.generation,
632 			  &outarg.attr, entry_attr_timeout(&outarg), 0);
633 	if (!inode) {
634 		fuse_queue_forget(fc, forget, outarg.nodeid, 1);
635 		return -ENOMEM;
636 	}
637 	kfree(forget);
638 
639 	err = d_instantiate_no_diralias(entry, inode);
640 	if (err)
641 		return err;
642 
643 	fuse_change_entry_timeout(entry, &outarg);
644 	fuse_invalidate_attr(dir);
645 	return 0;
646 
647  out_put_forget_req:
648 	kfree(forget);
649 	return err;
650 }
651 
fuse_mknod(struct inode * dir,struct dentry * entry,umode_t mode,dev_t rdev)652 static int fuse_mknod(struct inode *dir, struct dentry *entry, umode_t mode,
653 		      dev_t rdev)
654 {
655 	struct fuse_mknod_in inarg;
656 	struct fuse_conn *fc = get_fuse_conn(dir);
657 	struct fuse_req *req = fuse_get_req_nopages(fc);
658 	if (IS_ERR(req))
659 		return PTR_ERR(req);
660 
661 	if (!fc->dont_mask)
662 		mode &= ~current_umask();
663 
664 	memset(&inarg, 0, sizeof(inarg));
665 	inarg.mode = mode;
666 	inarg.rdev = new_encode_dev(rdev);
667 	inarg.umask = current_umask();
668 	req->in.h.opcode = FUSE_MKNOD;
669 	req->in.numargs = 2;
670 	req->in.args[0].size = fc->minor < 12 ? FUSE_COMPAT_MKNOD_IN_SIZE :
671 						sizeof(inarg);
672 	req->in.args[0].value = &inarg;
673 	req->in.args[1].size = entry->d_name.len + 1;
674 	req->in.args[1].value = entry->d_name.name;
675 	return create_new_entry(fc, req, dir, entry, mode);
676 }
677 
fuse_create(struct inode * dir,struct dentry * entry,umode_t mode,bool excl)678 static int fuse_create(struct inode *dir, struct dentry *entry, umode_t mode,
679 		       bool excl)
680 {
681 	return fuse_mknod(dir, entry, mode, 0);
682 }
683 
fuse_mkdir(struct inode * dir,struct dentry * entry,umode_t mode)684 static int fuse_mkdir(struct inode *dir, struct dentry *entry, umode_t mode)
685 {
686 	struct fuse_mkdir_in inarg;
687 	struct fuse_conn *fc = get_fuse_conn(dir);
688 	struct fuse_req *req = fuse_get_req_nopages(fc);
689 	if (IS_ERR(req))
690 		return PTR_ERR(req);
691 
692 	if (!fc->dont_mask)
693 		mode &= ~current_umask();
694 
695 	memset(&inarg, 0, sizeof(inarg));
696 	inarg.mode = mode;
697 	inarg.umask = current_umask();
698 	req->in.h.opcode = FUSE_MKDIR;
699 	req->in.numargs = 2;
700 	req->in.args[0].size = sizeof(inarg);
701 	req->in.args[0].value = &inarg;
702 	req->in.args[1].size = entry->d_name.len + 1;
703 	req->in.args[1].value = entry->d_name.name;
704 	return create_new_entry(fc, req, dir, entry, S_IFDIR);
705 }
706 
fuse_symlink(struct inode * dir,struct dentry * entry,const char * link)707 static int fuse_symlink(struct inode *dir, struct dentry *entry,
708 			const char *link)
709 {
710 	struct fuse_conn *fc = get_fuse_conn(dir);
711 	unsigned len = strlen(link) + 1;
712 	struct fuse_req *req = fuse_get_req_nopages(fc);
713 	if (IS_ERR(req))
714 		return PTR_ERR(req);
715 
716 	req->in.h.opcode = FUSE_SYMLINK;
717 	req->in.numargs = 2;
718 	req->in.args[0].size = entry->d_name.len + 1;
719 	req->in.args[0].value = entry->d_name.name;
720 	req->in.args[1].size = len;
721 	req->in.args[1].value = link;
722 	return create_new_entry(fc, req, dir, entry, S_IFLNK);
723 }
724 
fuse_update_ctime(struct inode * inode)725 static inline void fuse_update_ctime(struct inode *inode)
726 {
727 	if (!IS_NOCMTIME(inode)) {
728 		inode->i_ctime = current_fs_time(inode->i_sb);
729 		mark_inode_dirty_sync(inode);
730 	}
731 }
732 
fuse_unlink(struct inode * dir,struct dentry * entry)733 static int fuse_unlink(struct inode *dir, struct dentry *entry)
734 {
735 	int err;
736 	struct fuse_conn *fc = get_fuse_conn(dir);
737 	struct fuse_req *req = fuse_get_req_nopages(fc);
738 	if (IS_ERR(req))
739 		return PTR_ERR(req);
740 
741 	req->in.h.opcode = FUSE_UNLINK;
742 	req->in.h.nodeid = get_node_id(dir);
743 	req->in.numargs = 1;
744 	req->in.args[0].size = entry->d_name.len + 1;
745 	req->in.args[0].value = entry->d_name.name;
746 	fuse_request_send(fc, req);
747 	err = req->out.h.error;
748 	fuse_put_request(fc, req);
749 	if (!err) {
750 		struct inode *inode = entry->d_inode;
751 		struct fuse_inode *fi = get_fuse_inode(inode);
752 
753 		spin_lock(&fc->lock);
754 		fi->attr_version = ++fc->attr_version;
755 		/*
756 		 * If i_nlink == 0 then unlink doesn't make sense, yet this can
757 		 * happen if userspace filesystem is careless.  It would be
758 		 * difficult to enforce correct nlink usage so just ignore this
759 		 * condition here
760 		 */
761 		if (inode->i_nlink > 0)
762 			drop_nlink(inode);
763 		spin_unlock(&fc->lock);
764 		fuse_invalidate_attr(inode);
765 		fuse_invalidate_attr(dir);
766 		fuse_invalidate_entry_cache(entry);
767 		fuse_update_ctime(inode);
768 	} else if (err == -EINTR)
769 		fuse_invalidate_entry(entry);
770 	return err;
771 }
772 
fuse_rmdir(struct inode * dir,struct dentry * entry)773 static int fuse_rmdir(struct inode *dir, struct dentry *entry)
774 {
775 	int err;
776 	struct fuse_conn *fc = get_fuse_conn(dir);
777 	struct fuse_req *req = fuse_get_req_nopages(fc);
778 	if (IS_ERR(req))
779 		return PTR_ERR(req);
780 
781 	req->in.h.opcode = FUSE_RMDIR;
782 	req->in.h.nodeid = get_node_id(dir);
783 	req->in.numargs = 1;
784 	req->in.args[0].size = entry->d_name.len + 1;
785 	req->in.args[0].value = entry->d_name.name;
786 	fuse_request_send(fc, req);
787 	err = req->out.h.error;
788 	fuse_put_request(fc, req);
789 	if (!err) {
790 		clear_nlink(entry->d_inode);
791 		fuse_invalidate_attr(dir);
792 		fuse_invalidate_entry_cache(entry);
793 	} else if (err == -EINTR)
794 		fuse_invalidate_entry(entry);
795 	return err;
796 }
797 
fuse_rename_common(struct inode * olddir,struct dentry * oldent,struct inode * newdir,struct dentry * newent,unsigned int flags,int opcode,size_t argsize)798 static int fuse_rename_common(struct inode *olddir, struct dentry *oldent,
799 			      struct inode *newdir, struct dentry *newent,
800 			      unsigned int flags, int opcode, size_t argsize)
801 {
802 	int err;
803 	struct fuse_rename2_in inarg;
804 	struct fuse_conn *fc = get_fuse_conn(olddir);
805 	struct fuse_req *req;
806 
807 	req = fuse_get_req_nopages(fc);
808 	if (IS_ERR(req))
809 		return PTR_ERR(req);
810 
811 	memset(&inarg, 0, argsize);
812 	inarg.newdir = get_node_id(newdir);
813 	inarg.flags = flags;
814 	req->in.h.opcode = opcode;
815 	req->in.h.nodeid = get_node_id(olddir);
816 	req->in.numargs = 3;
817 	req->in.args[0].size = argsize;
818 	req->in.args[0].value = &inarg;
819 	req->in.args[1].size = oldent->d_name.len + 1;
820 	req->in.args[1].value = oldent->d_name.name;
821 	req->in.args[2].size = newent->d_name.len + 1;
822 	req->in.args[2].value = newent->d_name.name;
823 	fuse_request_send(fc, req);
824 	err = req->out.h.error;
825 	fuse_put_request(fc, req);
826 	if (!err) {
827 		/* ctime changes */
828 		fuse_invalidate_attr(oldent->d_inode);
829 		fuse_update_ctime(oldent->d_inode);
830 
831 		if (flags & RENAME_EXCHANGE) {
832 			fuse_invalidate_attr(newent->d_inode);
833 			fuse_update_ctime(newent->d_inode);
834 		}
835 
836 		fuse_invalidate_attr(olddir);
837 		if (olddir != newdir)
838 			fuse_invalidate_attr(newdir);
839 
840 		/* newent will end up negative */
841 		if (!(flags & RENAME_EXCHANGE) && newent->d_inode) {
842 			fuse_invalidate_attr(newent->d_inode);
843 			fuse_invalidate_entry_cache(newent);
844 			fuse_update_ctime(newent->d_inode);
845 		}
846 	} else if (err == -EINTR) {
847 		/* If request was interrupted, DEITY only knows if the
848 		   rename actually took place.  If the invalidation
849 		   fails (e.g. some process has CWD under the renamed
850 		   directory), then there can be inconsistency between
851 		   the dcache and the real filesystem.  Tough luck. */
852 		fuse_invalidate_entry(oldent);
853 		if (newent->d_inode)
854 			fuse_invalidate_entry(newent);
855 	}
856 
857 	return err;
858 }
859 
fuse_rename2(struct inode * olddir,struct dentry * oldent,struct inode * newdir,struct dentry * newent,unsigned int flags)860 static int fuse_rename2(struct inode *olddir, struct dentry *oldent,
861 			struct inode *newdir, struct dentry *newent,
862 			unsigned int flags)
863 {
864 	struct fuse_conn *fc = get_fuse_conn(olddir);
865 	int err;
866 
867 	if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE))
868 		return -EINVAL;
869 
870 	if (flags) {
871 		if (fc->no_rename2 || fc->minor < 23)
872 			return -EINVAL;
873 
874 		err = fuse_rename_common(olddir, oldent, newdir, newent, flags,
875 					 FUSE_RENAME2,
876 					 sizeof(struct fuse_rename2_in));
877 		if (err == -ENOSYS) {
878 			fc->no_rename2 = 1;
879 			err = -EINVAL;
880 		}
881 	} else {
882 		err = fuse_rename_common(olddir, oldent, newdir, newent, 0,
883 					 FUSE_RENAME,
884 					 sizeof(struct fuse_rename_in));
885 	}
886 
887 	return err;
888 }
889 
fuse_link(struct dentry * entry,struct inode * newdir,struct dentry * newent)890 static int fuse_link(struct dentry *entry, struct inode *newdir,
891 		     struct dentry *newent)
892 {
893 	int err;
894 	struct fuse_link_in inarg;
895 	struct inode *inode = entry->d_inode;
896 	struct fuse_conn *fc = get_fuse_conn(inode);
897 	struct fuse_req *req = fuse_get_req_nopages(fc);
898 	if (IS_ERR(req))
899 		return PTR_ERR(req);
900 
901 	memset(&inarg, 0, sizeof(inarg));
902 	inarg.oldnodeid = get_node_id(inode);
903 	req->in.h.opcode = FUSE_LINK;
904 	req->in.numargs = 2;
905 	req->in.args[0].size = sizeof(inarg);
906 	req->in.args[0].value = &inarg;
907 	req->in.args[1].size = newent->d_name.len + 1;
908 	req->in.args[1].value = newent->d_name.name;
909 	err = create_new_entry(fc, req, newdir, newent, inode->i_mode);
910 	/* Contrary to "normal" filesystems it can happen that link
911 	   makes two "logical" inodes point to the same "physical"
912 	   inode.  We invalidate the attributes of the old one, so it
913 	   will reflect changes in the backing inode (link count,
914 	   etc.)
915 	*/
916 	if (!err) {
917 		struct fuse_inode *fi = get_fuse_inode(inode);
918 
919 		spin_lock(&fc->lock);
920 		fi->attr_version = ++fc->attr_version;
921 		inc_nlink(inode);
922 		spin_unlock(&fc->lock);
923 		fuse_invalidate_attr(inode);
924 		fuse_update_ctime(inode);
925 	} else if (err == -EINTR) {
926 		fuse_invalidate_attr(inode);
927 	}
928 	return err;
929 }
930 
fuse_fillattr(struct inode * inode,struct fuse_attr * attr,struct kstat * stat)931 static void fuse_fillattr(struct inode *inode, struct fuse_attr *attr,
932 			  struct kstat *stat)
933 {
934 	unsigned int blkbits;
935 	struct fuse_conn *fc = get_fuse_conn(inode);
936 
937 	/* see the comment in fuse_change_attributes() */
938 	if (fc->writeback_cache && S_ISREG(inode->i_mode)) {
939 		attr->size = i_size_read(inode);
940 		attr->mtime = inode->i_mtime.tv_sec;
941 		attr->mtimensec = inode->i_mtime.tv_nsec;
942 		attr->ctime = inode->i_ctime.tv_sec;
943 		attr->ctimensec = inode->i_ctime.tv_nsec;
944 	}
945 
946 	stat->dev = inode->i_sb->s_dev;
947 	stat->ino = attr->ino;
948 	stat->mode = (inode->i_mode & S_IFMT) | (attr->mode & 07777);
949 	stat->nlink = attr->nlink;
950 	stat->uid = make_kuid(&init_user_ns, attr->uid);
951 	stat->gid = make_kgid(&init_user_ns, attr->gid);
952 	stat->rdev = inode->i_rdev;
953 	stat->atime.tv_sec = attr->atime;
954 	stat->atime.tv_nsec = attr->atimensec;
955 	stat->mtime.tv_sec = attr->mtime;
956 	stat->mtime.tv_nsec = attr->mtimensec;
957 	stat->ctime.tv_sec = attr->ctime;
958 	stat->ctime.tv_nsec = attr->ctimensec;
959 	stat->size = attr->size;
960 	stat->blocks = attr->blocks;
961 
962 	if (attr->blksize != 0)
963 		blkbits = ilog2(attr->blksize);
964 	else
965 		blkbits = inode->i_sb->s_blocksize_bits;
966 
967 	stat->blksize = 1 << blkbits;
968 }
969 
fuse_do_getattr(struct inode * inode,struct kstat * stat,struct file * file)970 static int fuse_do_getattr(struct inode *inode, struct kstat *stat,
971 			   struct file *file)
972 {
973 	int err;
974 	struct fuse_getattr_in inarg;
975 	struct fuse_attr_out outarg;
976 	struct fuse_conn *fc = get_fuse_conn(inode);
977 	struct fuse_req *req;
978 	u64 attr_version;
979 
980 	req = fuse_get_req_nopages(fc);
981 	if (IS_ERR(req))
982 		return PTR_ERR(req);
983 
984 	attr_version = fuse_get_attr_version(fc);
985 
986 	memset(&inarg, 0, sizeof(inarg));
987 	memset(&outarg, 0, sizeof(outarg));
988 	/* Directories have separate file-handle space */
989 	if (file && S_ISREG(inode->i_mode)) {
990 		struct fuse_file *ff = file->private_data;
991 
992 		inarg.getattr_flags |= FUSE_GETATTR_FH;
993 		inarg.fh = ff->fh;
994 	}
995 	req->in.h.opcode = FUSE_GETATTR;
996 	req->in.h.nodeid = get_node_id(inode);
997 	req->in.numargs = 1;
998 	req->in.args[0].size = sizeof(inarg);
999 	req->in.args[0].value = &inarg;
1000 	req->out.numargs = 1;
1001 	if (fc->minor < 9)
1002 		req->out.args[0].size = FUSE_COMPAT_ATTR_OUT_SIZE;
1003 	else
1004 		req->out.args[0].size = sizeof(outarg);
1005 	req->out.args[0].value = &outarg;
1006 	fuse_request_send(fc, req);
1007 	err = req->out.h.error;
1008 	fuse_put_request(fc, req);
1009 	if (!err) {
1010 		if ((inode->i_mode ^ outarg.attr.mode) & S_IFMT) {
1011 			make_bad_inode(inode);
1012 			err = -EIO;
1013 		} else {
1014 			fuse_change_attributes(inode, &outarg.attr,
1015 					       attr_timeout(&outarg),
1016 					       attr_version);
1017 			if (stat)
1018 				fuse_fillattr(inode, &outarg.attr, stat);
1019 		}
1020 	}
1021 	return err;
1022 }
1023 
fuse_update_attributes(struct inode * inode,struct kstat * stat,struct file * file,bool * refreshed)1024 int fuse_update_attributes(struct inode *inode, struct kstat *stat,
1025 			   struct file *file, bool *refreshed)
1026 {
1027 	struct fuse_inode *fi = get_fuse_inode(inode);
1028 	int err;
1029 	bool r;
1030 
1031 	if (time_before64(fi->i_time, get_jiffies_64())) {
1032 		r = true;
1033 		err = fuse_do_getattr(inode, stat, file);
1034 	} else {
1035 		r = false;
1036 		err = 0;
1037 		if (stat) {
1038 			generic_fillattr(inode, stat);
1039 			stat->mode = fi->orig_i_mode;
1040 			stat->ino = fi->orig_ino;
1041 		}
1042 	}
1043 
1044 	if (refreshed != NULL)
1045 		*refreshed = r;
1046 
1047 	return err;
1048 }
1049 
fuse_reverse_inval_entry(struct super_block * sb,u64 parent_nodeid,u64 child_nodeid,struct qstr * name)1050 int fuse_reverse_inval_entry(struct super_block *sb, u64 parent_nodeid,
1051 			     u64 child_nodeid, struct qstr *name)
1052 {
1053 	int err = -ENOTDIR;
1054 	struct inode *parent;
1055 	struct dentry *dir;
1056 	struct dentry *entry;
1057 
1058 	parent = ilookup5(sb, parent_nodeid, fuse_inode_eq, &parent_nodeid);
1059 	if (!parent)
1060 		return -ENOENT;
1061 
1062 	mutex_lock(&parent->i_mutex);
1063 	if (!S_ISDIR(parent->i_mode))
1064 		goto unlock;
1065 
1066 	err = -ENOENT;
1067 	dir = d_find_alias(parent);
1068 	if (!dir)
1069 		goto unlock;
1070 
1071 	entry = d_lookup(dir, name);
1072 	dput(dir);
1073 	if (!entry)
1074 		goto unlock;
1075 
1076 	fuse_invalidate_attr(parent);
1077 	fuse_invalidate_entry(entry);
1078 
1079 	if (child_nodeid != 0 && entry->d_inode) {
1080 		mutex_lock(&entry->d_inode->i_mutex);
1081 		if (get_node_id(entry->d_inode) != child_nodeid) {
1082 			err = -ENOENT;
1083 			goto badentry;
1084 		}
1085 		if (d_mountpoint(entry)) {
1086 			err = -EBUSY;
1087 			goto badentry;
1088 		}
1089 		if (S_ISDIR(entry->d_inode->i_mode)) {
1090 			shrink_dcache_parent(entry);
1091 			if (!simple_empty(entry)) {
1092 				err = -ENOTEMPTY;
1093 				goto badentry;
1094 			}
1095 			entry->d_inode->i_flags |= S_DEAD;
1096 		}
1097 		dont_mount(entry);
1098 		clear_nlink(entry->d_inode);
1099 		err = 0;
1100  badentry:
1101 		mutex_unlock(&entry->d_inode->i_mutex);
1102 		if (!err)
1103 			d_delete(entry);
1104 	} else {
1105 		err = 0;
1106 	}
1107 	dput(entry);
1108 
1109  unlock:
1110 	mutex_unlock(&parent->i_mutex);
1111 	iput(parent);
1112 	return err;
1113 }
1114 
1115 /*
1116  * Calling into a user-controlled filesystem gives the filesystem
1117  * daemon ptrace-like capabilities over the current process.  This
1118  * means, that the filesystem daemon is able to record the exact
1119  * filesystem operations performed, and can also control the behavior
1120  * of the requester process in otherwise impossible ways.  For example
1121  * it can delay the operation for arbitrary length of time allowing
1122  * DoS against the requester.
1123  *
1124  * For this reason only those processes can call into the filesystem,
1125  * for which the owner of the mount has ptrace privilege.  This
1126  * excludes processes started by other users, suid or sgid processes.
1127  */
fuse_allow_current_process(struct fuse_conn * fc)1128 int fuse_allow_current_process(struct fuse_conn *fc)
1129 {
1130 	const struct cred *cred;
1131 
1132 	if (fc->flags & FUSE_ALLOW_OTHER)
1133 		return 1;
1134 
1135 	cred = current_cred();
1136 	if (uid_eq(cred->euid, fc->user_id) &&
1137 	    uid_eq(cred->suid, fc->user_id) &&
1138 	    uid_eq(cred->uid,  fc->user_id) &&
1139 	    gid_eq(cred->egid, fc->group_id) &&
1140 	    gid_eq(cred->sgid, fc->group_id) &&
1141 	    gid_eq(cred->gid,  fc->group_id))
1142 		return 1;
1143 
1144 	return 0;
1145 }
1146 
fuse_access(struct inode * inode,int mask)1147 static int fuse_access(struct inode *inode, int mask)
1148 {
1149 	struct fuse_conn *fc = get_fuse_conn(inode);
1150 	struct fuse_req *req;
1151 	struct fuse_access_in inarg;
1152 	int err;
1153 
1154 	BUG_ON(mask & MAY_NOT_BLOCK);
1155 
1156 	if (fc->no_access)
1157 		return 0;
1158 
1159 	req = fuse_get_req_nopages(fc);
1160 	if (IS_ERR(req))
1161 		return PTR_ERR(req);
1162 
1163 	memset(&inarg, 0, sizeof(inarg));
1164 	inarg.mask = mask & (MAY_READ | MAY_WRITE | MAY_EXEC);
1165 	req->in.h.opcode = FUSE_ACCESS;
1166 	req->in.h.nodeid = get_node_id(inode);
1167 	req->in.numargs = 1;
1168 	req->in.args[0].size = sizeof(inarg);
1169 	req->in.args[0].value = &inarg;
1170 	fuse_request_send(fc, req);
1171 	err = req->out.h.error;
1172 	fuse_put_request(fc, req);
1173 	if (err == -ENOSYS) {
1174 		fc->no_access = 1;
1175 		err = 0;
1176 	}
1177 	return err;
1178 }
1179 
fuse_perm_getattr(struct inode * inode,int mask)1180 static int fuse_perm_getattr(struct inode *inode, int mask)
1181 {
1182 	if (mask & MAY_NOT_BLOCK)
1183 		return -ECHILD;
1184 
1185 	return fuse_do_getattr(inode, NULL, NULL);
1186 }
1187 
1188 /*
1189  * Check permission.  The two basic access models of FUSE are:
1190  *
1191  * 1) Local access checking ('default_permissions' mount option) based
1192  * on file mode.  This is the plain old disk filesystem permission
1193  * modell.
1194  *
1195  * 2) "Remote" access checking, where server is responsible for
1196  * checking permission in each inode operation.  An exception to this
1197  * is if ->permission() was invoked from sys_access() in which case an
1198  * access request is sent.  Execute permission is still checked
1199  * locally based on file mode.
1200  */
fuse_permission(struct inode * inode,int mask)1201 static int fuse_permission(struct inode *inode, int mask)
1202 {
1203 	struct fuse_conn *fc = get_fuse_conn(inode);
1204 	bool refreshed = false;
1205 	int err = 0;
1206 
1207 	if (!fuse_allow_current_process(fc))
1208 		return -EACCES;
1209 
1210 	/*
1211 	 * If attributes are needed, refresh them before proceeding
1212 	 */
1213 	if ((fc->flags & FUSE_DEFAULT_PERMISSIONS) ||
1214 	    ((mask & MAY_EXEC) && S_ISREG(inode->i_mode))) {
1215 		struct fuse_inode *fi = get_fuse_inode(inode);
1216 
1217 		if (time_before64(fi->i_time, get_jiffies_64())) {
1218 			refreshed = true;
1219 
1220 			err = fuse_perm_getattr(inode, mask);
1221 			if (err)
1222 				return err;
1223 		}
1224 	}
1225 
1226 	if (fc->flags & FUSE_DEFAULT_PERMISSIONS) {
1227 		err = generic_permission(inode, mask);
1228 
1229 		/* If permission is denied, try to refresh file
1230 		   attributes.  This is also needed, because the root
1231 		   node will at first have no permissions */
1232 		if (err == -EACCES && !refreshed) {
1233 			err = fuse_perm_getattr(inode, mask);
1234 			if (!err)
1235 				err = generic_permission(inode, mask);
1236 		}
1237 
1238 		/* Note: the opposite of the above test does not
1239 		   exist.  So if permissions are revoked this won't be
1240 		   noticed immediately, only after the attribute
1241 		   timeout has expired */
1242 	} else if (mask & (MAY_ACCESS | MAY_CHDIR)) {
1243 		err = fuse_access(inode, mask);
1244 	} else if ((mask & MAY_EXEC) && S_ISREG(inode->i_mode)) {
1245 		if (!(inode->i_mode & S_IXUGO)) {
1246 			if (refreshed)
1247 				return -EACCES;
1248 
1249 			err = fuse_perm_getattr(inode, mask);
1250 			if (!err && !(inode->i_mode & S_IXUGO))
1251 				return -EACCES;
1252 		}
1253 	}
1254 	return err;
1255 }
1256 
parse_dirfile(char * buf,size_t nbytes,struct file * file,struct dir_context * ctx)1257 static int parse_dirfile(char *buf, size_t nbytes, struct file *file,
1258 			 struct dir_context *ctx)
1259 {
1260 	while (nbytes >= FUSE_NAME_OFFSET) {
1261 		struct fuse_dirent *dirent = (struct fuse_dirent *) buf;
1262 		size_t reclen = FUSE_DIRENT_SIZE(dirent);
1263 		if (!dirent->namelen || dirent->namelen > FUSE_NAME_MAX)
1264 			return -EIO;
1265 		if (reclen > nbytes)
1266 			break;
1267 		if (memchr(dirent->name, '/', dirent->namelen) != NULL)
1268 			return -EIO;
1269 
1270 		if (!dir_emit(ctx, dirent->name, dirent->namelen,
1271 			       dirent->ino, dirent->type))
1272 			break;
1273 
1274 		buf += reclen;
1275 		nbytes -= reclen;
1276 		ctx->pos = dirent->off;
1277 	}
1278 
1279 	return 0;
1280 }
1281 
fuse_direntplus_link(struct file * file,struct fuse_direntplus * direntplus,u64 attr_version)1282 static int fuse_direntplus_link(struct file *file,
1283 				struct fuse_direntplus *direntplus,
1284 				u64 attr_version)
1285 {
1286 	int err;
1287 	struct fuse_entry_out *o = &direntplus->entry_out;
1288 	struct fuse_dirent *dirent = &direntplus->dirent;
1289 	struct dentry *parent = file->f_path.dentry;
1290 	struct qstr name = QSTR_INIT(dirent->name, dirent->namelen);
1291 	struct dentry *dentry;
1292 	struct dentry *alias;
1293 	struct inode *dir = parent->d_inode;
1294 	struct fuse_conn *fc;
1295 	struct inode *inode;
1296 
1297 	if (!o->nodeid) {
1298 		/*
1299 		 * Unlike in the case of fuse_lookup, zero nodeid does not mean
1300 		 * ENOENT. Instead, it only means the userspace filesystem did
1301 		 * not want to return attributes/handle for this entry.
1302 		 *
1303 		 * So do nothing.
1304 		 */
1305 		return 0;
1306 	}
1307 
1308 	if (name.name[0] == '.') {
1309 		/*
1310 		 * We could potentially refresh the attributes of the directory
1311 		 * and its parent?
1312 		 */
1313 		if (name.len == 1)
1314 			return 0;
1315 		if (name.name[1] == '.' && name.len == 2)
1316 			return 0;
1317 	}
1318 
1319 	if (invalid_nodeid(o->nodeid))
1320 		return -EIO;
1321 	if (!fuse_valid_type(o->attr.mode))
1322 		return -EIO;
1323 
1324 	fc = get_fuse_conn(dir);
1325 
1326 	name.hash = full_name_hash(name.name, name.len);
1327 	dentry = d_lookup(parent, &name);
1328 	if (dentry) {
1329 		inode = dentry->d_inode;
1330 		if (!inode) {
1331 			d_drop(dentry);
1332 		} else if (get_node_id(inode) != o->nodeid ||
1333 			   ((o->attr.mode ^ inode->i_mode) & S_IFMT)) {
1334 			d_invalidate(dentry);
1335 		} else if (is_bad_inode(inode)) {
1336 			err = -EIO;
1337 			goto out;
1338 		} else {
1339 			struct fuse_inode *fi;
1340 			fi = get_fuse_inode(inode);
1341 			spin_lock(&fc->lock);
1342 			fi->nlookup++;
1343 			spin_unlock(&fc->lock);
1344 
1345 			fuse_change_attributes(inode, &o->attr,
1346 					       entry_attr_timeout(o),
1347 					       attr_version);
1348 
1349 			/*
1350 			 * The other branch to 'found' comes via fuse_iget()
1351 			 * which bumps nlookup inside
1352 			 */
1353 			goto found;
1354 		}
1355 		dput(dentry);
1356 	}
1357 
1358 	dentry = d_alloc(parent, &name);
1359 	err = -ENOMEM;
1360 	if (!dentry)
1361 		goto out;
1362 
1363 	inode = fuse_iget(dir->i_sb, o->nodeid, o->generation,
1364 			  &o->attr, entry_attr_timeout(o), attr_version);
1365 	if (!inode)
1366 		goto out;
1367 
1368 	alias = d_materialise_unique(dentry, inode);
1369 	err = PTR_ERR(alias);
1370 	if (IS_ERR(alias))
1371 		goto out;
1372 
1373 	if (alias) {
1374 		dput(dentry);
1375 		dentry = alias;
1376 	}
1377 
1378 found:
1379 	if (fc->readdirplus_auto)
1380 		set_bit(FUSE_I_INIT_RDPLUS, &get_fuse_inode(inode)->state);
1381 	fuse_change_entry_timeout(dentry, o);
1382 
1383 	err = 0;
1384 out:
1385 	dput(dentry);
1386 	return err;
1387 }
1388 
parse_dirplusfile(char * buf,size_t nbytes,struct file * file,struct dir_context * ctx,u64 attr_version)1389 static int parse_dirplusfile(char *buf, size_t nbytes, struct file *file,
1390 			     struct dir_context *ctx, u64 attr_version)
1391 {
1392 	struct fuse_direntplus *direntplus;
1393 	struct fuse_dirent *dirent;
1394 	size_t reclen;
1395 	int over = 0;
1396 	int ret;
1397 
1398 	while (nbytes >= FUSE_NAME_OFFSET_DIRENTPLUS) {
1399 		direntplus = (struct fuse_direntplus *) buf;
1400 		dirent = &direntplus->dirent;
1401 		reclen = FUSE_DIRENTPLUS_SIZE(direntplus);
1402 
1403 		if (!dirent->namelen || dirent->namelen > FUSE_NAME_MAX)
1404 			return -EIO;
1405 		if (reclen > nbytes)
1406 			break;
1407 		if (memchr(dirent->name, '/', dirent->namelen) != NULL)
1408 			return -EIO;
1409 
1410 		if (!over) {
1411 			/* We fill entries into dstbuf only as much as
1412 			   it can hold. But we still continue iterating
1413 			   over remaining entries to link them. If not,
1414 			   we need to send a FORGET for each of those
1415 			   which we did not link.
1416 			*/
1417 			over = !dir_emit(ctx, dirent->name, dirent->namelen,
1418 				       dirent->ino, dirent->type);
1419 			if (!over)
1420 				ctx->pos = dirent->off;
1421 		}
1422 
1423 		buf += reclen;
1424 		nbytes -= reclen;
1425 
1426 		ret = fuse_direntplus_link(file, direntplus, attr_version);
1427 		if (ret)
1428 			fuse_force_forget(file, direntplus->entry_out.nodeid);
1429 	}
1430 
1431 	return 0;
1432 }
1433 
fuse_readdir(struct file * file,struct dir_context * ctx)1434 static int fuse_readdir(struct file *file, struct dir_context *ctx)
1435 {
1436 	int plus, err;
1437 	size_t nbytes;
1438 	struct page *page;
1439 	struct inode *inode = file_inode(file);
1440 	struct fuse_conn *fc = get_fuse_conn(inode);
1441 	struct fuse_req *req;
1442 	u64 attr_version = 0;
1443 
1444 	if (is_bad_inode(inode))
1445 		return -EIO;
1446 
1447 	req = fuse_get_req(fc, 1);
1448 	if (IS_ERR(req))
1449 		return PTR_ERR(req);
1450 
1451 	page = alloc_page(GFP_KERNEL);
1452 	if (!page) {
1453 		fuse_put_request(fc, req);
1454 		return -ENOMEM;
1455 	}
1456 
1457 	plus = fuse_use_readdirplus(inode, ctx);
1458 	req->out.argpages = 1;
1459 	req->num_pages = 1;
1460 	req->pages[0] = page;
1461 	req->page_descs[0].length = PAGE_SIZE;
1462 	if (plus) {
1463 		attr_version = fuse_get_attr_version(fc);
1464 		fuse_read_fill(req, file, ctx->pos, PAGE_SIZE,
1465 			       FUSE_READDIRPLUS);
1466 	} else {
1467 		fuse_read_fill(req, file, ctx->pos, PAGE_SIZE,
1468 			       FUSE_READDIR);
1469 	}
1470 	fuse_request_send(fc, req);
1471 	nbytes = req->out.args[0].size;
1472 	err = req->out.h.error;
1473 	fuse_put_request(fc, req);
1474 	if (!err) {
1475 		if (plus) {
1476 			err = parse_dirplusfile(page_address(page), nbytes,
1477 						file, ctx,
1478 						attr_version);
1479 		} else {
1480 			err = parse_dirfile(page_address(page), nbytes, file,
1481 					    ctx);
1482 		}
1483 	}
1484 
1485 	__free_page(page);
1486 	fuse_invalidate_atime(inode);
1487 	return err;
1488 }
1489 
read_link(struct dentry * dentry)1490 static char *read_link(struct dentry *dentry)
1491 {
1492 	struct inode *inode = dentry->d_inode;
1493 	struct fuse_conn *fc = get_fuse_conn(inode);
1494 	struct fuse_req *req = fuse_get_req_nopages(fc);
1495 	char *link;
1496 
1497 	if (IS_ERR(req))
1498 		return ERR_CAST(req);
1499 
1500 	link = (char *) __get_free_page(GFP_KERNEL);
1501 	if (!link) {
1502 		link = ERR_PTR(-ENOMEM);
1503 		goto out;
1504 	}
1505 	req->in.h.opcode = FUSE_READLINK;
1506 	req->in.h.nodeid = get_node_id(inode);
1507 	req->out.argvar = 1;
1508 	req->out.numargs = 1;
1509 	req->out.args[0].size = PAGE_SIZE - 1;
1510 	req->out.args[0].value = link;
1511 	fuse_request_send(fc, req);
1512 	if (req->out.h.error) {
1513 		free_page((unsigned long) link);
1514 		link = ERR_PTR(req->out.h.error);
1515 	} else
1516 		link[req->out.args[0].size] = '\0';
1517  out:
1518 	fuse_put_request(fc, req);
1519 	fuse_invalidate_atime(inode);
1520 	return link;
1521 }
1522 
free_link(char * link)1523 static void free_link(char *link)
1524 {
1525 	if (!IS_ERR(link))
1526 		free_page((unsigned long) link);
1527 }
1528 
fuse_follow_link(struct dentry * dentry,struct nameidata * nd)1529 static void *fuse_follow_link(struct dentry *dentry, struct nameidata *nd)
1530 {
1531 	nd_set_link(nd, read_link(dentry));
1532 	return NULL;
1533 }
1534 
fuse_put_link(struct dentry * dentry,struct nameidata * nd,void * c)1535 static void fuse_put_link(struct dentry *dentry, struct nameidata *nd, void *c)
1536 {
1537 	free_link(nd_get_link(nd));
1538 }
1539 
fuse_dir_open(struct inode * inode,struct file * file)1540 static int fuse_dir_open(struct inode *inode, struct file *file)
1541 {
1542 	return fuse_open_common(inode, file, true);
1543 }
1544 
fuse_dir_release(struct inode * inode,struct file * file)1545 static int fuse_dir_release(struct inode *inode, struct file *file)
1546 {
1547 	fuse_release_common(file, FUSE_RELEASEDIR);
1548 
1549 	return 0;
1550 }
1551 
fuse_dir_fsync(struct file * file,loff_t start,loff_t end,int datasync)1552 static int fuse_dir_fsync(struct file *file, loff_t start, loff_t end,
1553 			  int datasync)
1554 {
1555 	return fuse_fsync_common(file, start, end, datasync, 1);
1556 }
1557 
fuse_dir_ioctl(struct file * file,unsigned int cmd,unsigned long arg)1558 static long fuse_dir_ioctl(struct file *file, unsigned int cmd,
1559 			    unsigned long arg)
1560 {
1561 	struct fuse_conn *fc = get_fuse_conn(file->f_mapping->host);
1562 
1563 	/* FUSE_IOCTL_DIR only supported for API version >= 7.18 */
1564 	if (fc->minor < 18)
1565 		return -ENOTTY;
1566 
1567 	return fuse_ioctl_common(file, cmd, arg, FUSE_IOCTL_DIR);
1568 }
1569 
fuse_dir_compat_ioctl(struct file * file,unsigned int cmd,unsigned long arg)1570 static long fuse_dir_compat_ioctl(struct file *file, unsigned int cmd,
1571 				   unsigned long arg)
1572 {
1573 	struct fuse_conn *fc = get_fuse_conn(file->f_mapping->host);
1574 
1575 	if (fc->minor < 18)
1576 		return -ENOTTY;
1577 
1578 	return fuse_ioctl_common(file, cmd, arg,
1579 				 FUSE_IOCTL_COMPAT | FUSE_IOCTL_DIR);
1580 }
1581 
update_mtime(unsigned ivalid,bool trust_local_mtime)1582 static bool update_mtime(unsigned ivalid, bool trust_local_mtime)
1583 {
1584 	/* Always update if mtime is explicitly set  */
1585 	if (ivalid & ATTR_MTIME_SET)
1586 		return true;
1587 
1588 	/* Or if kernel i_mtime is the official one */
1589 	if (trust_local_mtime)
1590 		return true;
1591 
1592 	/* If it's an open(O_TRUNC) or an ftruncate(), don't update */
1593 	if ((ivalid & ATTR_SIZE) && (ivalid & (ATTR_OPEN | ATTR_FILE)))
1594 		return false;
1595 
1596 	/* In all other cases update */
1597 	return true;
1598 }
1599 
iattr_to_fattr(struct iattr * iattr,struct fuse_setattr_in * arg,bool trust_local_cmtime)1600 static void iattr_to_fattr(struct iattr *iattr, struct fuse_setattr_in *arg,
1601 			   bool trust_local_cmtime)
1602 {
1603 	unsigned ivalid = iattr->ia_valid;
1604 
1605 	if (ivalid & ATTR_MODE)
1606 		arg->valid |= FATTR_MODE,   arg->mode = iattr->ia_mode;
1607 	if (ivalid & ATTR_UID)
1608 		arg->valid |= FATTR_UID,    arg->uid = from_kuid(&init_user_ns, iattr->ia_uid);
1609 	if (ivalid & ATTR_GID)
1610 		arg->valid |= FATTR_GID,    arg->gid = from_kgid(&init_user_ns, iattr->ia_gid);
1611 	if (ivalid & ATTR_SIZE)
1612 		arg->valid |= FATTR_SIZE,   arg->size = iattr->ia_size;
1613 	if (ivalid & ATTR_ATIME) {
1614 		arg->valid |= FATTR_ATIME;
1615 		arg->atime = iattr->ia_atime.tv_sec;
1616 		arg->atimensec = iattr->ia_atime.tv_nsec;
1617 		if (!(ivalid & ATTR_ATIME_SET))
1618 			arg->valid |= FATTR_ATIME_NOW;
1619 	}
1620 	if ((ivalid & ATTR_MTIME) && update_mtime(ivalid, trust_local_cmtime)) {
1621 		arg->valid |= FATTR_MTIME;
1622 		arg->mtime = iattr->ia_mtime.tv_sec;
1623 		arg->mtimensec = iattr->ia_mtime.tv_nsec;
1624 		if (!(ivalid & ATTR_MTIME_SET) && !trust_local_cmtime)
1625 			arg->valid |= FATTR_MTIME_NOW;
1626 	}
1627 	if ((ivalid & ATTR_CTIME) && trust_local_cmtime) {
1628 		arg->valid |= FATTR_CTIME;
1629 		arg->ctime = iattr->ia_ctime.tv_sec;
1630 		arg->ctimensec = iattr->ia_ctime.tv_nsec;
1631 	}
1632 }
1633 
1634 /*
1635  * Prevent concurrent writepages on inode
1636  *
1637  * This is done by adding a negative bias to the inode write counter
1638  * and waiting for all pending writes to finish.
1639  */
fuse_set_nowrite(struct inode * inode)1640 void fuse_set_nowrite(struct inode *inode)
1641 {
1642 	struct fuse_conn *fc = get_fuse_conn(inode);
1643 	struct fuse_inode *fi = get_fuse_inode(inode);
1644 
1645 	BUG_ON(!mutex_is_locked(&inode->i_mutex));
1646 
1647 	spin_lock(&fc->lock);
1648 	BUG_ON(fi->writectr < 0);
1649 	fi->writectr += FUSE_NOWRITE;
1650 	spin_unlock(&fc->lock);
1651 	wait_event(fi->page_waitq, fi->writectr == FUSE_NOWRITE);
1652 }
1653 
1654 /*
1655  * Allow writepages on inode
1656  *
1657  * Remove the bias from the writecounter and send any queued
1658  * writepages.
1659  */
__fuse_release_nowrite(struct inode * inode)1660 static void __fuse_release_nowrite(struct inode *inode)
1661 {
1662 	struct fuse_inode *fi = get_fuse_inode(inode);
1663 
1664 	BUG_ON(fi->writectr != FUSE_NOWRITE);
1665 	fi->writectr = 0;
1666 	fuse_flush_writepages(inode);
1667 }
1668 
fuse_release_nowrite(struct inode * inode)1669 void fuse_release_nowrite(struct inode *inode)
1670 {
1671 	struct fuse_conn *fc = get_fuse_conn(inode);
1672 
1673 	spin_lock(&fc->lock);
1674 	__fuse_release_nowrite(inode);
1675 	spin_unlock(&fc->lock);
1676 }
1677 
fuse_setattr_fill(struct fuse_conn * fc,struct fuse_req * req,struct inode * inode,struct fuse_setattr_in * inarg_p,struct fuse_attr_out * outarg_p)1678 static void fuse_setattr_fill(struct fuse_conn *fc, struct fuse_req *req,
1679 			      struct inode *inode,
1680 			      struct fuse_setattr_in *inarg_p,
1681 			      struct fuse_attr_out *outarg_p)
1682 {
1683 	req->in.h.opcode = FUSE_SETATTR;
1684 	req->in.h.nodeid = get_node_id(inode);
1685 	req->in.numargs = 1;
1686 	req->in.args[0].size = sizeof(*inarg_p);
1687 	req->in.args[0].value = inarg_p;
1688 	req->out.numargs = 1;
1689 	if (fc->minor < 9)
1690 		req->out.args[0].size = FUSE_COMPAT_ATTR_OUT_SIZE;
1691 	else
1692 		req->out.args[0].size = sizeof(*outarg_p);
1693 	req->out.args[0].value = outarg_p;
1694 }
1695 
1696 /*
1697  * Flush inode->i_mtime to the server
1698  */
fuse_flush_times(struct inode * inode,struct fuse_file * ff)1699 int fuse_flush_times(struct inode *inode, struct fuse_file *ff)
1700 {
1701 	struct fuse_conn *fc = get_fuse_conn(inode);
1702 	struct fuse_req *req;
1703 	struct fuse_setattr_in inarg;
1704 	struct fuse_attr_out outarg;
1705 	int err;
1706 
1707 	req = fuse_get_req_nopages(fc);
1708 	if (IS_ERR(req))
1709 		return PTR_ERR(req);
1710 
1711 	memset(&inarg, 0, sizeof(inarg));
1712 	memset(&outarg, 0, sizeof(outarg));
1713 
1714 	inarg.valid = FATTR_MTIME;
1715 	inarg.mtime = inode->i_mtime.tv_sec;
1716 	inarg.mtimensec = inode->i_mtime.tv_nsec;
1717 	if (fc->minor >= 23) {
1718 		inarg.valid |= FATTR_CTIME;
1719 		inarg.ctime = inode->i_ctime.tv_sec;
1720 		inarg.ctimensec = inode->i_ctime.tv_nsec;
1721 	}
1722 	if (ff) {
1723 		inarg.valid |= FATTR_FH;
1724 		inarg.fh = ff->fh;
1725 	}
1726 	fuse_setattr_fill(fc, req, inode, &inarg, &outarg);
1727 	fuse_request_send(fc, req);
1728 	err = req->out.h.error;
1729 	fuse_put_request(fc, req);
1730 
1731 	return err;
1732 }
1733 
1734 /*
1735  * Set attributes, and at the same time refresh them.
1736  *
1737  * Truncation is slightly complicated, because the 'truncate' request
1738  * may fail, in which case we don't want to touch the mapping.
1739  * vmtruncate() doesn't allow for this case, so do the rlimit checking
1740  * and the actual truncation by hand.
1741  */
fuse_do_setattr(struct dentry * dentry,struct iattr * attr,struct file * file)1742 int fuse_do_setattr(struct dentry *dentry, struct iattr *attr,
1743 		    struct file *file)
1744 {
1745 	struct inode *inode = d_inode(dentry);
1746 	struct fuse_conn *fc = get_fuse_conn(inode);
1747 	struct fuse_inode *fi = get_fuse_inode(inode);
1748 	struct fuse_req *req;
1749 	struct fuse_setattr_in inarg;
1750 	struct fuse_attr_out outarg;
1751 	bool is_truncate = false;
1752 	bool is_wb = fc->writeback_cache;
1753 	loff_t oldsize;
1754 	int err;
1755 	bool trust_local_cmtime = is_wb && S_ISREG(inode->i_mode);
1756 
1757 	if (!(fc->flags & FUSE_DEFAULT_PERMISSIONS))
1758 		attr->ia_valid |= ATTR_FORCE;
1759 
1760 	err = inode_change_ok(inode, attr);
1761 	if (err)
1762 		return err;
1763 
1764 	if (attr->ia_valid & ATTR_OPEN) {
1765 		if (fc->atomic_o_trunc)
1766 			return 0;
1767 		file = NULL;
1768 	}
1769 
1770 	if (attr->ia_valid & ATTR_SIZE)
1771 		is_truncate = true;
1772 
1773 	req = fuse_get_req_nopages(fc);
1774 	if (IS_ERR(req))
1775 		return PTR_ERR(req);
1776 
1777 	if (is_truncate) {
1778 		fuse_set_nowrite(inode);
1779 		set_bit(FUSE_I_SIZE_UNSTABLE, &fi->state);
1780 		if (trust_local_cmtime && attr->ia_size != inode->i_size)
1781 			attr->ia_valid |= ATTR_MTIME | ATTR_CTIME;
1782 	}
1783 
1784 	memset(&inarg, 0, sizeof(inarg));
1785 	memset(&outarg, 0, sizeof(outarg));
1786 	iattr_to_fattr(attr, &inarg, trust_local_cmtime);
1787 	if (file) {
1788 		struct fuse_file *ff = file->private_data;
1789 		inarg.valid |= FATTR_FH;
1790 		inarg.fh = ff->fh;
1791 	}
1792 	if (attr->ia_valid & ATTR_SIZE) {
1793 		/* For mandatory locking in truncate */
1794 		inarg.valid |= FATTR_LOCKOWNER;
1795 		inarg.lock_owner = fuse_lock_owner_id(fc, current->files);
1796 	}
1797 	fuse_setattr_fill(fc, req, inode, &inarg, &outarg);
1798 	fuse_request_send(fc, req);
1799 	err = req->out.h.error;
1800 	fuse_put_request(fc, req);
1801 	if (err) {
1802 		if (err == -EINTR)
1803 			fuse_invalidate_attr(inode);
1804 		goto error;
1805 	}
1806 
1807 	if ((inode->i_mode ^ outarg.attr.mode) & S_IFMT) {
1808 		make_bad_inode(inode);
1809 		err = -EIO;
1810 		goto error;
1811 	}
1812 
1813 	spin_lock(&fc->lock);
1814 	/* the kernel maintains i_mtime locally */
1815 	if (trust_local_cmtime) {
1816 		if (attr->ia_valid & ATTR_MTIME)
1817 			inode->i_mtime = attr->ia_mtime;
1818 		if (attr->ia_valid & ATTR_CTIME)
1819 			inode->i_ctime = attr->ia_ctime;
1820 		/* FIXME: clear I_DIRTY_SYNC? */
1821 	}
1822 
1823 	fuse_change_attributes_common(inode, &outarg.attr,
1824 				      attr_timeout(&outarg));
1825 	oldsize = inode->i_size;
1826 	/* see the comment in fuse_change_attributes() */
1827 	if (!is_wb || is_truncate || !S_ISREG(inode->i_mode))
1828 		i_size_write(inode, outarg.attr.size);
1829 
1830 	if (is_truncate) {
1831 		/* NOTE: this may release/reacquire fc->lock */
1832 		__fuse_release_nowrite(inode);
1833 	}
1834 	spin_unlock(&fc->lock);
1835 
1836 	/*
1837 	 * Only call invalidate_inode_pages2() after removing
1838 	 * FUSE_NOWRITE, otherwise fuse_launder_page() would deadlock.
1839 	 */
1840 	if ((is_truncate || !is_wb) &&
1841 	    S_ISREG(inode->i_mode) && oldsize != outarg.attr.size) {
1842 		truncate_pagecache(inode, outarg.attr.size);
1843 		invalidate_inode_pages2(inode->i_mapping);
1844 	}
1845 
1846 	clear_bit(FUSE_I_SIZE_UNSTABLE, &fi->state);
1847 	return 0;
1848 
1849 error:
1850 	if (is_truncate)
1851 		fuse_release_nowrite(inode);
1852 
1853 	clear_bit(FUSE_I_SIZE_UNSTABLE, &fi->state);
1854 	return err;
1855 }
1856 
fuse_setattr(struct dentry * entry,struct iattr * attr)1857 static int fuse_setattr(struct dentry *entry, struct iattr *attr)
1858 {
1859 	struct inode *inode = entry->d_inode;
1860 
1861 	if (!fuse_allow_current_process(get_fuse_conn(inode)))
1862 		return -EACCES;
1863 
1864 	if (attr->ia_valid & ATTR_FILE)
1865 		return fuse_do_setattr(entry, attr, attr->ia_file);
1866 	else
1867 		return fuse_do_setattr(entry, attr, NULL);
1868 }
1869 
fuse_getattr(struct vfsmount * mnt,struct dentry * entry,struct kstat * stat)1870 static int fuse_getattr(struct vfsmount *mnt, struct dentry *entry,
1871 			struct kstat *stat)
1872 {
1873 	struct inode *inode = entry->d_inode;
1874 	struct fuse_conn *fc = get_fuse_conn(inode);
1875 
1876 	if (!fuse_allow_current_process(fc))
1877 		return -EACCES;
1878 
1879 	return fuse_update_attributes(inode, stat, NULL, NULL);
1880 }
1881 
fuse_setxattr(struct dentry * entry,const char * name,const void * value,size_t size,int flags)1882 static int fuse_setxattr(struct dentry *entry, const char *name,
1883 			 const void *value, size_t size, int flags)
1884 {
1885 	struct inode *inode = entry->d_inode;
1886 	struct fuse_conn *fc = get_fuse_conn(inode);
1887 	struct fuse_req *req;
1888 	struct fuse_setxattr_in inarg;
1889 	int err;
1890 
1891 	if (fc->no_setxattr)
1892 		return -EOPNOTSUPP;
1893 
1894 	req = fuse_get_req_nopages(fc);
1895 	if (IS_ERR(req))
1896 		return PTR_ERR(req);
1897 
1898 	memset(&inarg, 0, sizeof(inarg));
1899 	inarg.size = size;
1900 	inarg.flags = flags;
1901 	req->in.h.opcode = FUSE_SETXATTR;
1902 	req->in.h.nodeid = get_node_id(inode);
1903 	req->in.numargs = 3;
1904 	req->in.args[0].size = sizeof(inarg);
1905 	req->in.args[0].value = &inarg;
1906 	req->in.args[1].size = strlen(name) + 1;
1907 	req->in.args[1].value = name;
1908 	req->in.args[2].size = size;
1909 	req->in.args[2].value = value;
1910 	fuse_request_send(fc, req);
1911 	err = req->out.h.error;
1912 	fuse_put_request(fc, req);
1913 	if (err == -ENOSYS) {
1914 		fc->no_setxattr = 1;
1915 		err = -EOPNOTSUPP;
1916 	}
1917 	if (!err) {
1918 		fuse_invalidate_attr(inode);
1919 		fuse_update_ctime(inode);
1920 	}
1921 	return err;
1922 }
1923 
fuse_getxattr(struct dentry * entry,const char * name,void * value,size_t size)1924 static ssize_t fuse_getxattr(struct dentry *entry, const char *name,
1925 			     void *value, size_t size)
1926 {
1927 	struct inode *inode = entry->d_inode;
1928 	struct fuse_conn *fc = get_fuse_conn(inode);
1929 	struct fuse_req *req;
1930 	struct fuse_getxattr_in inarg;
1931 	struct fuse_getxattr_out outarg;
1932 	ssize_t ret;
1933 
1934 	if (fc->no_getxattr)
1935 		return -EOPNOTSUPP;
1936 
1937 	req = fuse_get_req_nopages(fc);
1938 	if (IS_ERR(req))
1939 		return PTR_ERR(req);
1940 
1941 	memset(&inarg, 0, sizeof(inarg));
1942 	inarg.size = size;
1943 	req->in.h.opcode = FUSE_GETXATTR;
1944 	req->in.h.nodeid = get_node_id(inode);
1945 	req->in.numargs = 2;
1946 	req->in.args[0].size = sizeof(inarg);
1947 	req->in.args[0].value = &inarg;
1948 	req->in.args[1].size = strlen(name) + 1;
1949 	req->in.args[1].value = name;
1950 	/* This is really two different operations rolled into one */
1951 	req->out.numargs = 1;
1952 	if (size) {
1953 		req->out.argvar = 1;
1954 		req->out.args[0].size = size;
1955 		req->out.args[0].value = value;
1956 	} else {
1957 		req->out.args[0].size = sizeof(outarg);
1958 		req->out.args[0].value = &outarg;
1959 	}
1960 	fuse_request_send(fc, req);
1961 	ret = req->out.h.error;
1962 	if (!ret)
1963 		ret = size ? req->out.args[0].size : outarg.size;
1964 	else {
1965 		if (ret == -ENOSYS) {
1966 			fc->no_getxattr = 1;
1967 			ret = -EOPNOTSUPP;
1968 		}
1969 	}
1970 	fuse_put_request(fc, req);
1971 	return ret;
1972 }
1973 
fuse_listxattr(struct dentry * entry,char * list,size_t size)1974 static ssize_t fuse_listxattr(struct dentry *entry, char *list, size_t size)
1975 {
1976 	struct inode *inode = entry->d_inode;
1977 	struct fuse_conn *fc = get_fuse_conn(inode);
1978 	struct fuse_req *req;
1979 	struct fuse_getxattr_in inarg;
1980 	struct fuse_getxattr_out outarg;
1981 	ssize_t ret;
1982 
1983 	if (!fuse_allow_current_process(fc))
1984 		return -EACCES;
1985 
1986 	if (fc->no_listxattr)
1987 		return -EOPNOTSUPP;
1988 
1989 	req = fuse_get_req_nopages(fc);
1990 	if (IS_ERR(req))
1991 		return PTR_ERR(req);
1992 
1993 	memset(&inarg, 0, sizeof(inarg));
1994 	inarg.size = size;
1995 	req->in.h.opcode = FUSE_LISTXATTR;
1996 	req->in.h.nodeid = get_node_id(inode);
1997 	req->in.numargs = 1;
1998 	req->in.args[0].size = sizeof(inarg);
1999 	req->in.args[0].value = &inarg;
2000 	/* This is really two different operations rolled into one */
2001 	req->out.numargs = 1;
2002 	if (size) {
2003 		req->out.argvar = 1;
2004 		req->out.args[0].size = size;
2005 		req->out.args[0].value = list;
2006 	} else {
2007 		req->out.args[0].size = sizeof(outarg);
2008 		req->out.args[0].value = &outarg;
2009 	}
2010 	fuse_request_send(fc, req);
2011 	ret = req->out.h.error;
2012 	if (!ret)
2013 		ret = size ? req->out.args[0].size : outarg.size;
2014 	else {
2015 		if (ret == -ENOSYS) {
2016 			fc->no_listxattr = 1;
2017 			ret = -EOPNOTSUPP;
2018 		}
2019 	}
2020 	fuse_put_request(fc, req);
2021 	return ret;
2022 }
2023 
fuse_removexattr(struct dentry * entry,const char * name)2024 static int fuse_removexattr(struct dentry *entry, const char *name)
2025 {
2026 	struct inode *inode = entry->d_inode;
2027 	struct fuse_conn *fc = get_fuse_conn(inode);
2028 	struct fuse_req *req;
2029 	int err;
2030 
2031 	if (fc->no_removexattr)
2032 		return -EOPNOTSUPP;
2033 
2034 	req = fuse_get_req_nopages(fc);
2035 	if (IS_ERR(req))
2036 		return PTR_ERR(req);
2037 
2038 	req->in.h.opcode = FUSE_REMOVEXATTR;
2039 	req->in.h.nodeid = get_node_id(inode);
2040 	req->in.numargs = 1;
2041 	req->in.args[0].size = strlen(name) + 1;
2042 	req->in.args[0].value = name;
2043 	fuse_request_send(fc, req);
2044 	err = req->out.h.error;
2045 	fuse_put_request(fc, req);
2046 	if (err == -ENOSYS) {
2047 		fc->no_removexattr = 1;
2048 		err = -EOPNOTSUPP;
2049 	}
2050 	if (!err) {
2051 		fuse_invalidate_attr(inode);
2052 		fuse_update_ctime(inode);
2053 	}
2054 	return err;
2055 }
2056 
2057 static const struct inode_operations fuse_dir_inode_operations = {
2058 	.lookup		= fuse_lookup,
2059 	.mkdir		= fuse_mkdir,
2060 	.symlink	= fuse_symlink,
2061 	.unlink		= fuse_unlink,
2062 	.rmdir		= fuse_rmdir,
2063 	.rename2	= fuse_rename2,
2064 	.link		= fuse_link,
2065 	.setattr	= fuse_setattr,
2066 	.create		= fuse_create,
2067 	.atomic_open	= fuse_atomic_open,
2068 	.mknod		= fuse_mknod,
2069 	.permission	= fuse_permission,
2070 	.getattr	= fuse_getattr,
2071 	.setxattr	= fuse_setxattr,
2072 	.getxattr	= fuse_getxattr,
2073 	.listxattr	= fuse_listxattr,
2074 	.removexattr	= fuse_removexattr,
2075 };
2076 
2077 static const struct file_operations fuse_dir_operations = {
2078 	.llseek		= generic_file_llseek,
2079 	.read		= generic_read_dir,
2080 	.iterate	= fuse_readdir,
2081 	.open		= fuse_dir_open,
2082 	.release	= fuse_dir_release,
2083 	.fsync		= fuse_dir_fsync,
2084 	.unlocked_ioctl	= fuse_dir_ioctl,
2085 	.compat_ioctl	= fuse_dir_compat_ioctl,
2086 };
2087 
2088 static const struct inode_operations fuse_common_inode_operations = {
2089 	.setattr	= fuse_setattr,
2090 	.permission	= fuse_permission,
2091 	.getattr	= fuse_getattr,
2092 	.setxattr	= fuse_setxattr,
2093 	.getxattr	= fuse_getxattr,
2094 	.listxattr	= fuse_listxattr,
2095 	.removexattr	= fuse_removexattr,
2096 };
2097 
2098 static const struct inode_operations fuse_symlink_inode_operations = {
2099 	.setattr	= fuse_setattr,
2100 	.follow_link	= fuse_follow_link,
2101 	.put_link	= fuse_put_link,
2102 	.readlink	= generic_readlink,
2103 	.getattr	= fuse_getattr,
2104 	.setxattr	= fuse_setxattr,
2105 	.getxattr	= fuse_getxattr,
2106 	.listxattr	= fuse_listxattr,
2107 	.removexattr	= fuse_removexattr,
2108 };
2109 
fuse_init_common(struct inode * inode)2110 void fuse_init_common(struct inode *inode)
2111 {
2112 	inode->i_op = &fuse_common_inode_operations;
2113 }
2114 
fuse_init_dir(struct inode * inode)2115 void fuse_init_dir(struct inode *inode)
2116 {
2117 	inode->i_op = &fuse_dir_inode_operations;
2118 	inode->i_fop = &fuse_dir_operations;
2119 }
2120 
fuse_init_symlink(struct inode * inode)2121 void fuse_init_symlink(struct inode *inode)
2122 {
2123 	inode->i_op = &fuse_symlink_inode_operations;
2124 }
2125