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