]> git.proxmox.com Git - mirror_ubuntu-zesty-kernel.git/blame_incremental - fs/fuse/dir.c
fuse: prepare lookup for nfs export
[mirror_ubuntu-zesty-kernel.git] / fs / fuse / dir.c
... / ...
CommitLineData
1/*
2 FUSE: Filesystem in Userspace
3 Copyright (C) 2001-2006 Miklos Szeredi <miklos@szeredi.hu>
4
5 This program can be distributed under the terms of the GNU GPL.
6 See the file COPYING.
7*/
8
9#include "fuse_i.h"
10
11#include <linux/pagemap.h>
12#include <linux/file.h>
13#include <linux/gfp.h>
14#include <linux/sched.h>
15#include <linux/namei.h>
16
17#if BITS_PER_LONG >= 64
18static inline void fuse_dentry_settime(struct dentry *entry, u64 time)
19{
20 entry->d_time = time;
21}
22
23static inline u64 fuse_dentry_time(struct dentry *entry)
24{
25 return entry->d_time;
26}
27#else
28/*
29 * On 32 bit archs store the high 32 bits of time in d_fsdata
30 */
31static void fuse_dentry_settime(struct dentry *entry, u64 time)
32{
33 entry->d_time = time;
34 entry->d_fsdata = (void *) (unsigned long) (time >> 32);
35}
36
37static u64 fuse_dentry_time(struct dentry *entry)
38{
39 return (u64) entry->d_time +
40 ((u64) (unsigned long) entry->d_fsdata << 32);
41}
42#endif
43
44/*
45 * FUSE caches dentries and attributes with separate timeout. The
46 * time in jiffies until the dentry/attributes are valid is stored in
47 * dentry->d_time and fuse_inode->i_time respectively.
48 */
49
50/*
51 * Calculate the time in jiffies until a dentry/attributes are valid
52 */
53static u64 time_to_jiffies(unsigned long sec, unsigned long nsec)
54{
55 if (sec || nsec) {
56 struct timespec ts = {sec, nsec};
57 return get_jiffies_64() + timespec_to_jiffies(&ts);
58 } else
59 return 0;
60}
61
62/*
63 * Set dentry and possibly attribute timeouts from the lookup/mk*
64 * replies
65 */
66static void fuse_change_entry_timeout(struct dentry *entry,
67 struct fuse_entry_out *o)
68{
69 fuse_dentry_settime(entry,
70 time_to_jiffies(o->entry_valid, o->entry_valid_nsec));
71}
72
73static u64 attr_timeout(struct fuse_attr_out *o)
74{
75 return time_to_jiffies(o->attr_valid, o->attr_valid_nsec);
76}
77
78static u64 entry_attr_timeout(struct fuse_entry_out *o)
79{
80 return time_to_jiffies(o->attr_valid, o->attr_valid_nsec);
81}
82
83/*
84 * Mark the attributes as stale, so that at the next call to
85 * ->getattr() they will be fetched from userspace
86 */
87void fuse_invalidate_attr(struct inode *inode)
88{
89 get_fuse_inode(inode)->i_time = 0;
90}
91
92/*
93 * Just mark the entry as stale, so that a next attempt to look it up
94 * will result in a new lookup call to userspace
95 *
96 * This is called when a dentry is about to become negative and the
97 * timeout is unknown (unlink, rmdir, rename and in some cases
98 * lookup)
99 */
100static void fuse_invalidate_entry_cache(struct dentry *entry)
101{
102 fuse_dentry_settime(entry, 0);
103}
104
105/*
106 * Same as fuse_invalidate_entry_cache(), but also try to remove the
107 * dentry from the hash
108 */
109static void fuse_invalidate_entry(struct dentry *entry)
110{
111 d_invalidate(entry);
112 fuse_invalidate_entry_cache(entry);
113}
114
115static void fuse_lookup_init(struct fuse_req *req, struct inode *dir,
116 struct dentry *entry,
117 struct fuse_entry_out *outarg)
118{
119 struct fuse_conn *fc = get_fuse_conn(dir);
120
121 memset(outarg, 0, sizeof(struct fuse_entry_out));
122 req->in.h.opcode = FUSE_LOOKUP;
123 req->in.h.nodeid = get_node_id(dir);
124 req->in.numargs = 1;
125 req->in.args[0].size = entry->d_name.len + 1;
126 req->in.args[0].value = entry->d_name.name;
127 req->out.numargs = 1;
128 if (fc->minor < 9)
129 req->out.args[0].size = FUSE_COMPAT_ENTRY_OUT_SIZE;
130 else
131 req->out.args[0].size = sizeof(struct fuse_entry_out);
132 req->out.args[0].value = outarg;
133}
134
135u64 fuse_get_attr_version(struct fuse_conn *fc)
136{
137 u64 curr_version;
138
139 /*
140 * The spin lock isn't actually needed on 64bit archs, but we
141 * don't yet care too much about such optimizations.
142 */
143 spin_lock(&fc->lock);
144 curr_version = fc->attr_version;
145 spin_unlock(&fc->lock);
146
147 return curr_version;
148}
149
150/*
151 * Check whether the dentry is still valid
152 *
153 * If the entry validity timeout has expired and the dentry is
154 * positive, try to redo the lookup. If the lookup results in a
155 * different inode, then let the VFS invalidate the dentry and redo
156 * the lookup once more. If the lookup results in the same inode,
157 * then refresh the attributes, timeouts and mark the dentry valid.
158 */
159static int fuse_dentry_revalidate(struct dentry *entry, struct nameidata *nd)
160{
161 struct inode *inode = entry->d_inode;
162
163 if (inode && is_bad_inode(inode))
164 return 0;
165 else if (fuse_dentry_time(entry) < get_jiffies_64()) {
166 int err;
167 struct fuse_entry_out outarg;
168 struct fuse_conn *fc;
169 struct fuse_req *req;
170 struct fuse_req *forget_req;
171 struct dentry *parent;
172 u64 attr_version;
173
174 /* For negative dentries, always do a fresh lookup */
175 if (!inode)
176 return 0;
177
178 fc = get_fuse_conn(inode);
179 req = fuse_get_req(fc);
180 if (IS_ERR(req))
181 return 0;
182
183 forget_req = fuse_get_req(fc);
184 if (IS_ERR(forget_req)) {
185 fuse_put_request(fc, req);
186 return 0;
187 }
188
189 attr_version = fuse_get_attr_version(fc);
190
191 parent = dget_parent(entry);
192 fuse_lookup_init(req, parent->d_inode, entry, &outarg);
193 request_send(fc, req);
194 dput(parent);
195 err = req->out.h.error;
196 fuse_put_request(fc, req);
197 /* Zero nodeid is same as -ENOENT */
198 if (!err && !outarg.nodeid)
199 err = -ENOENT;
200 if (!err) {
201 struct fuse_inode *fi = get_fuse_inode(inode);
202 if (outarg.nodeid != get_node_id(inode)) {
203 fuse_send_forget(fc, forget_req,
204 outarg.nodeid, 1);
205 return 0;
206 }
207 spin_lock(&fc->lock);
208 fi->nlookup ++;
209 spin_unlock(&fc->lock);
210 }
211 fuse_put_request(fc, forget_req);
212 if (err || (outarg.attr.mode ^ inode->i_mode) & S_IFMT)
213 return 0;
214
215 fuse_change_attributes(inode, &outarg.attr,
216 entry_attr_timeout(&outarg),
217 attr_version);
218 fuse_change_entry_timeout(entry, &outarg);
219 }
220 return 1;
221}
222
223static int invalid_nodeid(u64 nodeid)
224{
225 return !nodeid || nodeid == FUSE_ROOT_ID;
226}
227
228static struct dentry_operations fuse_dentry_operations = {
229 .d_revalidate = fuse_dentry_revalidate,
230};
231
232int fuse_valid_type(int m)
233{
234 return S_ISREG(m) || S_ISDIR(m) || S_ISLNK(m) || S_ISCHR(m) ||
235 S_ISBLK(m) || S_ISFIFO(m) || S_ISSOCK(m);
236}
237
238/*
239 * Add a directory inode to a dentry, ensuring that no other dentry
240 * refers to this inode. Called with fc->inst_mutex.
241 */
242static struct dentry *fuse_d_add_directory(struct dentry *entry,
243 struct inode *inode)
244{
245 struct dentry *alias = d_find_alias(inode);
246 if (alias && !(alias->d_flags & DCACHE_DISCONNECTED)) {
247 /* This tries to shrink the subtree below alias */
248 fuse_invalidate_entry(alias);
249 dput(alias);
250 if (!list_empty(&inode->i_dentry))
251 return ERR_PTR(-EBUSY);
252 } else {
253 dput(alias);
254 }
255 return d_splice_alias(inode, entry);
256}
257
258static struct dentry *fuse_lookup(struct inode *dir, struct dentry *entry,
259 struct nameidata *nd)
260{
261 int err;
262 struct fuse_entry_out outarg;
263 struct inode *inode = NULL;
264 struct dentry *newent;
265 struct fuse_conn *fc = get_fuse_conn(dir);
266 struct fuse_req *req;
267 struct fuse_req *forget_req;
268 u64 attr_version;
269
270 if (entry->d_name.len > FUSE_NAME_MAX)
271 return ERR_PTR(-ENAMETOOLONG);
272
273 req = fuse_get_req(fc);
274 if (IS_ERR(req))
275 return ERR_CAST(req);
276
277 forget_req = fuse_get_req(fc);
278 if (IS_ERR(forget_req)) {
279 fuse_put_request(fc, req);
280 return ERR_CAST(forget_req);
281 }
282
283 attr_version = fuse_get_attr_version(fc);
284
285 fuse_lookup_init(req, dir, entry, &outarg);
286 request_send(fc, req);
287 err = req->out.h.error;
288 fuse_put_request(fc, req);
289 /* Zero nodeid is same as -ENOENT, but with valid timeout */
290 if (!err && outarg.nodeid &&
291 (invalid_nodeid(outarg.nodeid) ||
292 !fuse_valid_type(outarg.attr.mode)))
293 err = -EIO;
294 if (!err && outarg.nodeid) {
295 inode = fuse_iget(dir->i_sb, outarg.nodeid, outarg.generation,
296 &outarg.attr, entry_attr_timeout(&outarg),
297 attr_version);
298 if (!inode) {
299 fuse_send_forget(fc, forget_req, outarg.nodeid, 1);
300 return ERR_PTR(-ENOMEM);
301 }
302 }
303 fuse_put_request(fc, forget_req);
304 if (err && err != -ENOENT)
305 return ERR_PTR(err);
306
307 if (inode && S_ISDIR(inode->i_mode)) {
308 mutex_lock(&fc->inst_mutex);
309 newent = fuse_d_add_directory(entry, inode);
310 mutex_unlock(&fc->inst_mutex);
311 if (IS_ERR(newent)) {
312 iput(inode);
313 return newent;
314 }
315 } else
316 newent = d_splice_alias(inode, entry);
317
318 entry = newent ? newent : entry;
319 entry->d_op = &fuse_dentry_operations;
320 if (!err)
321 fuse_change_entry_timeout(entry, &outarg);
322 else
323 fuse_invalidate_entry_cache(entry);
324 return newent;
325}
326
327/*
328 * Synchronous release for the case when something goes wrong in CREATE_OPEN
329 */
330static void fuse_sync_release(struct fuse_conn *fc, struct fuse_file *ff,
331 u64 nodeid, int flags)
332{
333 fuse_release_fill(ff, nodeid, flags, FUSE_RELEASE);
334 ff->reserved_req->force = 1;
335 request_send(fc, ff->reserved_req);
336 fuse_put_request(fc, ff->reserved_req);
337 kfree(ff);
338}
339
340/*
341 * Atomic create+open operation
342 *
343 * If the filesystem doesn't support this, then fall back to separate
344 * 'mknod' + 'open' requests.
345 */
346static int fuse_create_open(struct inode *dir, struct dentry *entry, int mode,
347 struct nameidata *nd)
348{
349 int err;
350 struct inode *inode;
351 struct fuse_conn *fc = get_fuse_conn(dir);
352 struct fuse_req *req;
353 struct fuse_req *forget_req;
354 struct fuse_open_in inarg;
355 struct fuse_open_out outopen;
356 struct fuse_entry_out outentry;
357 struct fuse_file *ff;
358 struct file *file;
359 int flags = nd->intent.open.flags - 1;
360
361 if (fc->no_create)
362 return -ENOSYS;
363
364 forget_req = fuse_get_req(fc);
365 if (IS_ERR(forget_req))
366 return PTR_ERR(forget_req);
367
368 req = fuse_get_req(fc);
369 err = PTR_ERR(req);
370 if (IS_ERR(req))
371 goto out_put_forget_req;
372
373 err = -ENOMEM;
374 ff = fuse_file_alloc();
375 if (!ff)
376 goto out_put_request;
377
378 flags &= ~O_NOCTTY;
379 memset(&inarg, 0, sizeof(inarg));
380 memset(&outentry, 0, sizeof(outentry));
381 inarg.flags = flags;
382 inarg.mode = mode;
383 req->in.h.opcode = FUSE_CREATE;
384 req->in.h.nodeid = get_node_id(dir);
385 req->in.numargs = 2;
386 req->in.args[0].size = sizeof(inarg);
387 req->in.args[0].value = &inarg;
388 req->in.args[1].size = entry->d_name.len + 1;
389 req->in.args[1].value = entry->d_name.name;
390 req->out.numargs = 2;
391 if (fc->minor < 9)
392 req->out.args[0].size = FUSE_COMPAT_ENTRY_OUT_SIZE;
393 else
394 req->out.args[0].size = sizeof(outentry);
395 req->out.args[0].value = &outentry;
396 req->out.args[1].size = sizeof(outopen);
397 req->out.args[1].value = &outopen;
398 request_send(fc, req);
399 err = req->out.h.error;
400 if (err) {
401 if (err == -ENOSYS)
402 fc->no_create = 1;
403 goto out_free_ff;
404 }
405
406 err = -EIO;
407 if (!S_ISREG(outentry.attr.mode) || invalid_nodeid(outentry.nodeid))
408 goto out_free_ff;
409
410 fuse_put_request(fc, req);
411 inode = fuse_iget(dir->i_sb, outentry.nodeid, outentry.generation,
412 &outentry.attr, entry_attr_timeout(&outentry), 0);
413 if (!inode) {
414 flags &= ~(O_CREAT | O_EXCL | O_TRUNC);
415 ff->fh = outopen.fh;
416 fuse_sync_release(fc, ff, outentry.nodeid, flags);
417 fuse_send_forget(fc, forget_req, outentry.nodeid, 1);
418 return -ENOMEM;
419 }
420 fuse_put_request(fc, forget_req);
421 d_instantiate(entry, inode);
422 fuse_change_entry_timeout(entry, &outentry);
423 fuse_invalidate_attr(dir);
424 file = lookup_instantiate_filp(nd, entry, generic_file_open);
425 if (IS_ERR(file)) {
426 ff->fh = outopen.fh;
427 fuse_sync_release(fc, ff, outentry.nodeid, flags);
428 return PTR_ERR(file);
429 }
430 fuse_finish_open(inode, file, ff, &outopen);
431 return 0;
432
433 out_free_ff:
434 fuse_file_free(ff);
435 out_put_request:
436 fuse_put_request(fc, req);
437 out_put_forget_req:
438 fuse_put_request(fc, forget_req);
439 return err;
440}
441
442/*
443 * Code shared between mknod, mkdir, symlink and link
444 */
445static int create_new_entry(struct fuse_conn *fc, struct fuse_req *req,
446 struct inode *dir, struct dentry *entry,
447 int mode)
448{
449 struct fuse_entry_out outarg;
450 struct inode *inode;
451 int err;
452 struct fuse_req *forget_req;
453
454 forget_req = fuse_get_req(fc);
455 if (IS_ERR(forget_req)) {
456 fuse_put_request(fc, req);
457 return PTR_ERR(forget_req);
458 }
459
460 memset(&outarg, 0, sizeof(outarg));
461 req->in.h.nodeid = get_node_id(dir);
462 req->out.numargs = 1;
463 if (fc->minor < 9)
464 req->out.args[0].size = FUSE_COMPAT_ENTRY_OUT_SIZE;
465 else
466 req->out.args[0].size = sizeof(outarg);
467 req->out.args[0].value = &outarg;
468 request_send(fc, req);
469 err = req->out.h.error;
470 fuse_put_request(fc, req);
471 if (err)
472 goto out_put_forget_req;
473
474 err = -EIO;
475 if (invalid_nodeid(outarg.nodeid))
476 goto out_put_forget_req;
477
478 if ((outarg.attr.mode ^ mode) & S_IFMT)
479 goto out_put_forget_req;
480
481 inode = fuse_iget(dir->i_sb, outarg.nodeid, outarg.generation,
482 &outarg.attr, entry_attr_timeout(&outarg), 0);
483 if (!inode) {
484 fuse_send_forget(fc, forget_req, outarg.nodeid, 1);
485 return -ENOMEM;
486 }
487 fuse_put_request(fc, forget_req);
488
489 if (S_ISDIR(inode->i_mode)) {
490 struct dentry *alias;
491 mutex_lock(&fc->inst_mutex);
492 alias = d_find_alias(inode);
493 if (alias) {
494 /* New directory must have moved since mkdir */
495 mutex_unlock(&fc->inst_mutex);
496 dput(alias);
497 iput(inode);
498 return -EBUSY;
499 }
500 d_instantiate(entry, inode);
501 mutex_unlock(&fc->inst_mutex);
502 } else
503 d_instantiate(entry, inode);
504
505 fuse_change_entry_timeout(entry, &outarg);
506 fuse_invalidate_attr(dir);
507 return 0;
508
509 out_put_forget_req:
510 fuse_put_request(fc, forget_req);
511 return err;
512}
513
514static int fuse_mknod(struct inode *dir, struct dentry *entry, int mode,
515 dev_t rdev)
516{
517 struct fuse_mknod_in inarg;
518 struct fuse_conn *fc = get_fuse_conn(dir);
519 struct fuse_req *req = fuse_get_req(fc);
520 if (IS_ERR(req))
521 return PTR_ERR(req);
522
523 memset(&inarg, 0, sizeof(inarg));
524 inarg.mode = mode;
525 inarg.rdev = new_encode_dev(rdev);
526 req->in.h.opcode = FUSE_MKNOD;
527 req->in.numargs = 2;
528 req->in.args[0].size = sizeof(inarg);
529 req->in.args[0].value = &inarg;
530 req->in.args[1].size = entry->d_name.len + 1;
531 req->in.args[1].value = entry->d_name.name;
532 return create_new_entry(fc, req, dir, entry, mode);
533}
534
535static int fuse_create(struct inode *dir, struct dentry *entry, int mode,
536 struct nameidata *nd)
537{
538 if (nd && (nd->flags & LOOKUP_OPEN)) {
539 int err = fuse_create_open(dir, entry, mode, nd);
540 if (err != -ENOSYS)
541 return err;
542 /* Fall back on mknod */
543 }
544 return fuse_mknod(dir, entry, mode, 0);
545}
546
547static int fuse_mkdir(struct inode *dir, struct dentry *entry, int mode)
548{
549 struct fuse_mkdir_in inarg;
550 struct fuse_conn *fc = get_fuse_conn(dir);
551 struct fuse_req *req = fuse_get_req(fc);
552 if (IS_ERR(req))
553 return PTR_ERR(req);
554
555 memset(&inarg, 0, sizeof(inarg));
556 inarg.mode = mode;
557 req->in.h.opcode = FUSE_MKDIR;
558 req->in.numargs = 2;
559 req->in.args[0].size = sizeof(inarg);
560 req->in.args[0].value = &inarg;
561 req->in.args[1].size = entry->d_name.len + 1;
562 req->in.args[1].value = entry->d_name.name;
563 return create_new_entry(fc, req, dir, entry, S_IFDIR);
564}
565
566static int fuse_symlink(struct inode *dir, struct dentry *entry,
567 const char *link)
568{
569 struct fuse_conn *fc = get_fuse_conn(dir);
570 unsigned len = strlen(link) + 1;
571 struct fuse_req *req = fuse_get_req(fc);
572 if (IS_ERR(req))
573 return PTR_ERR(req);
574
575 req->in.h.opcode = FUSE_SYMLINK;
576 req->in.numargs = 2;
577 req->in.args[0].size = entry->d_name.len + 1;
578 req->in.args[0].value = entry->d_name.name;
579 req->in.args[1].size = len;
580 req->in.args[1].value = link;
581 return create_new_entry(fc, req, dir, entry, S_IFLNK);
582}
583
584static int fuse_unlink(struct inode *dir, struct dentry *entry)
585{
586 int err;
587 struct fuse_conn *fc = get_fuse_conn(dir);
588 struct fuse_req *req = fuse_get_req(fc);
589 if (IS_ERR(req))
590 return PTR_ERR(req);
591
592 req->in.h.opcode = FUSE_UNLINK;
593 req->in.h.nodeid = get_node_id(dir);
594 req->in.numargs = 1;
595 req->in.args[0].size = entry->d_name.len + 1;
596 req->in.args[0].value = entry->d_name.name;
597 request_send(fc, req);
598 err = req->out.h.error;
599 fuse_put_request(fc, req);
600 if (!err) {
601 struct inode *inode = entry->d_inode;
602
603 /* Set nlink to zero so the inode can be cleared, if
604 the inode does have more links this will be
605 discovered at the next lookup/getattr */
606 clear_nlink(inode);
607 fuse_invalidate_attr(inode);
608 fuse_invalidate_attr(dir);
609 fuse_invalidate_entry_cache(entry);
610 } else if (err == -EINTR)
611 fuse_invalidate_entry(entry);
612 return err;
613}
614
615static int fuse_rmdir(struct inode *dir, struct dentry *entry)
616{
617 int err;
618 struct fuse_conn *fc = get_fuse_conn(dir);
619 struct fuse_req *req = fuse_get_req(fc);
620 if (IS_ERR(req))
621 return PTR_ERR(req);
622
623 req->in.h.opcode = FUSE_RMDIR;
624 req->in.h.nodeid = get_node_id(dir);
625 req->in.numargs = 1;
626 req->in.args[0].size = entry->d_name.len + 1;
627 req->in.args[0].value = entry->d_name.name;
628 request_send(fc, req);
629 err = req->out.h.error;
630 fuse_put_request(fc, req);
631 if (!err) {
632 clear_nlink(entry->d_inode);
633 fuse_invalidate_attr(dir);
634 fuse_invalidate_entry_cache(entry);
635 } else if (err == -EINTR)
636 fuse_invalidate_entry(entry);
637 return err;
638}
639
640static int fuse_rename(struct inode *olddir, struct dentry *oldent,
641 struct inode *newdir, struct dentry *newent)
642{
643 int err;
644 struct fuse_rename_in inarg;
645 struct fuse_conn *fc = get_fuse_conn(olddir);
646 struct fuse_req *req = fuse_get_req(fc);
647 if (IS_ERR(req))
648 return PTR_ERR(req);
649
650 memset(&inarg, 0, sizeof(inarg));
651 inarg.newdir = get_node_id(newdir);
652 req->in.h.opcode = FUSE_RENAME;
653 req->in.h.nodeid = get_node_id(olddir);
654 req->in.numargs = 3;
655 req->in.args[0].size = sizeof(inarg);
656 req->in.args[0].value = &inarg;
657 req->in.args[1].size = oldent->d_name.len + 1;
658 req->in.args[1].value = oldent->d_name.name;
659 req->in.args[2].size = newent->d_name.len + 1;
660 req->in.args[2].value = newent->d_name.name;
661 request_send(fc, req);
662 err = req->out.h.error;
663 fuse_put_request(fc, req);
664 if (!err) {
665 /* ctime changes */
666 fuse_invalidate_attr(oldent->d_inode);
667
668 fuse_invalidate_attr(olddir);
669 if (olddir != newdir)
670 fuse_invalidate_attr(newdir);
671
672 /* newent will end up negative */
673 if (newent->d_inode)
674 fuse_invalidate_entry_cache(newent);
675 } else if (err == -EINTR) {
676 /* If request was interrupted, DEITY only knows if the
677 rename actually took place. If the invalidation
678 fails (e.g. some process has CWD under the renamed
679 directory), then there can be inconsistency between
680 the dcache and the real filesystem. Tough luck. */
681 fuse_invalidate_entry(oldent);
682 if (newent->d_inode)
683 fuse_invalidate_entry(newent);
684 }
685
686 return err;
687}
688
689static int fuse_link(struct dentry *entry, struct inode *newdir,
690 struct dentry *newent)
691{
692 int err;
693 struct fuse_link_in inarg;
694 struct inode *inode = entry->d_inode;
695 struct fuse_conn *fc = get_fuse_conn(inode);
696 struct fuse_req *req = fuse_get_req(fc);
697 if (IS_ERR(req))
698 return PTR_ERR(req);
699
700 memset(&inarg, 0, sizeof(inarg));
701 inarg.oldnodeid = get_node_id(inode);
702 req->in.h.opcode = FUSE_LINK;
703 req->in.numargs = 2;
704 req->in.args[0].size = sizeof(inarg);
705 req->in.args[0].value = &inarg;
706 req->in.args[1].size = newent->d_name.len + 1;
707 req->in.args[1].value = newent->d_name.name;
708 err = create_new_entry(fc, req, newdir, newent, inode->i_mode);
709 /* Contrary to "normal" filesystems it can happen that link
710 makes two "logical" inodes point to the same "physical"
711 inode. We invalidate the attributes of the old one, so it
712 will reflect changes in the backing inode (link count,
713 etc.)
714 */
715 if (!err || err == -EINTR)
716 fuse_invalidate_attr(inode);
717 return err;
718}
719
720static void fuse_fillattr(struct inode *inode, struct fuse_attr *attr,
721 struct kstat *stat)
722{
723 stat->dev = inode->i_sb->s_dev;
724 stat->ino = attr->ino;
725 stat->mode = (inode->i_mode & S_IFMT) | (attr->mode & 07777);
726 stat->nlink = attr->nlink;
727 stat->uid = attr->uid;
728 stat->gid = attr->gid;
729 stat->rdev = inode->i_rdev;
730 stat->atime.tv_sec = attr->atime;
731 stat->atime.tv_nsec = attr->atimensec;
732 stat->mtime.tv_sec = attr->mtime;
733 stat->mtime.tv_nsec = attr->mtimensec;
734 stat->ctime.tv_sec = attr->ctime;
735 stat->ctime.tv_nsec = attr->ctimensec;
736 stat->size = attr->size;
737 stat->blocks = attr->blocks;
738 stat->blksize = (1 << inode->i_blkbits);
739}
740
741static int fuse_do_getattr(struct inode *inode, struct kstat *stat,
742 struct file *file)
743{
744 int err;
745 struct fuse_getattr_in inarg;
746 struct fuse_attr_out outarg;
747 struct fuse_conn *fc = get_fuse_conn(inode);
748 struct fuse_req *req;
749 u64 attr_version;
750
751 req = fuse_get_req(fc);
752 if (IS_ERR(req))
753 return PTR_ERR(req);
754
755 attr_version = fuse_get_attr_version(fc);
756
757 memset(&inarg, 0, sizeof(inarg));
758 memset(&outarg, 0, sizeof(outarg));
759 /* Directories have separate file-handle space */
760 if (file && S_ISREG(inode->i_mode)) {
761 struct fuse_file *ff = file->private_data;
762
763 inarg.getattr_flags |= FUSE_GETATTR_FH;
764 inarg.fh = ff->fh;
765 }
766 req->in.h.opcode = FUSE_GETATTR;
767 req->in.h.nodeid = get_node_id(inode);
768 req->in.numargs = 1;
769 req->in.args[0].size = sizeof(inarg);
770 req->in.args[0].value = &inarg;
771 req->out.numargs = 1;
772 if (fc->minor < 9)
773 req->out.args[0].size = FUSE_COMPAT_ATTR_OUT_SIZE;
774 else
775 req->out.args[0].size = sizeof(outarg);
776 req->out.args[0].value = &outarg;
777 request_send(fc, req);
778 err = req->out.h.error;
779 fuse_put_request(fc, req);
780 if (!err) {
781 if ((inode->i_mode ^ outarg.attr.mode) & S_IFMT) {
782 make_bad_inode(inode);
783 err = -EIO;
784 } else {
785 fuse_change_attributes(inode, &outarg.attr,
786 attr_timeout(&outarg),
787 attr_version);
788 if (stat)
789 fuse_fillattr(inode, &outarg.attr, stat);
790 }
791 }
792 return err;
793}
794
795int fuse_update_attributes(struct inode *inode, struct kstat *stat,
796 struct file *file, bool *refreshed)
797{
798 struct fuse_inode *fi = get_fuse_inode(inode);
799 int err;
800 bool r;
801
802 if (fi->i_time < get_jiffies_64()) {
803 r = true;
804 err = fuse_do_getattr(inode, stat, file);
805 } else {
806 r = false;
807 err = 0;
808 if (stat) {
809 generic_fillattr(inode, stat);
810 stat->mode = fi->orig_i_mode;
811 }
812 }
813
814 if (refreshed != NULL)
815 *refreshed = r;
816
817 return err;
818}
819
820/*
821 * Calling into a user-controlled filesystem gives the filesystem
822 * daemon ptrace-like capabilities over the requester process. This
823 * means, that the filesystem daemon is able to record the exact
824 * filesystem operations performed, and can also control the behavior
825 * of the requester process in otherwise impossible ways. For example
826 * it can delay the operation for arbitrary length of time allowing
827 * DoS against the requester.
828 *
829 * For this reason only those processes can call into the filesystem,
830 * for which the owner of the mount has ptrace privilege. This
831 * excludes processes started by other users, suid or sgid processes.
832 */
833int fuse_allow_task(struct fuse_conn *fc, struct task_struct *task)
834{
835 if (fc->flags & FUSE_ALLOW_OTHER)
836 return 1;
837
838 if (task->euid == fc->user_id &&
839 task->suid == fc->user_id &&
840 task->uid == fc->user_id &&
841 task->egid == fc->group_id &&
842 task->sgid == fc->group_id &&
843 task->gid == fc->group_id)
844 return 1;
845
846 return 0;
847}
848
849static int fuse_access(struct inode *inode, int mask)
850{
851 struct fuse_conn *fc = get_fuse_conn(inode);
852 struct fuse_req *req;
853 struct fuse_access_in inarg;
854 int err;
855
856 if (fc->no_access)
857 return 0;
858
859 req = fuse_get_req(fc);
860 if (IS_ERR(req))
861 return PTR_ERR(req);
862
863 memset(&inarg, 0, sizeof(inarg));
864 inarg.mask = mask;
865 req->in.h.opcode = FUSE_ACCESS;
866 req->in.h.nodeid = get_node_id(inode);
867 req->in.numargs = 1;
868 req->in.args[0].size = sizeof(inarg);
869 req->in.args[0].value = &inarg;
870 request_send(fc, req);
871 err = req->out.h.error;
872 fuse_put_request(fc, req);
873 if (err == -ENOSYS) {
874 fc->no_access = 1;
875 err = 0;
876 }
877 return err;
878}
879
880/*
881 * Check permission. The two basic access models of FUSE are:
882 *
883 * 1) Local access checking ('default_permissions' mount option) based
884 * on file mode. This is the plain old disk filesystem permission
885 * modell.
886 *
887 * 2) "Remote" access checking, where server is responsible for
888 * checking permission in each inode operation. An exception to this
889 * is if ->permission() was invoked from sys_access() in which case an
890 * access request is sent. Execute permission is still checked
891 * locally based on file mode.
892 */
893static int fuse_permission(struct inode *inode, int mask, struct nameidata *nd)
894{
895 struct fuse_conn *fc = get_fuse_conn(inode);
896 bool refreshed = false;
897 int err = 0;
898
899 if (!fuse_allow_task(fc, current))
900 return -EACCES;
901
902 /*
903 * If attributes are needed, refresh them before proceeding
904 */
905 if ((fc->flags & FUSE_DEFAULT_PERMISSIONS) ||
906 ((mask & MAY_EXEC) && S_ISREG(inode->i_mode))) {
907 err = fuse_update_attributes(inode, NULL, NULL, &refreshed);
908 if (err)
909 return err;
910 }
911
912 if (fc->flags & FUSE_DEFAULT_PERMISSIONS) {
913 err = generic_permission(inode, mask, NULL);
914
915 /* If permission is denied, try to refresh file
916 attributes. This is also needed, because the root
917 node will at first have no permissions */
918 if (err == -EACCES && !refreshed) {
919 err = fuse_do_getattr(inode, NULL, NULL);
920 if (!err)
921 err = generic_permission(inode, mask, NULL);
922 }
923
924 /* Note: the opposite of the above test does not
925 exist. So if permissions are revoked this won't be
926 noticed immediately, only after the attribute
927 timeout has expired */
928 } else if (nd && (nd->flags & (LOOKUP_ACCESS | LOOKUP_CHDIR))) {
929 err = fuse_access(inode, mask);
930 } else if ((mask & MAY_EXEC) && S_ISREG(inode->i_mode)) {
931 if (!(inode->i_mode & S_IXUGO)) {
932 if (refreshed)
933 return -EACCES;
934
935 err = fuse_do_getattr(inode, NULL, NULL);
936 if (!err && !(inode->i_mode & S_IXUGO))
937 return -EACCES;
938 }
939 }
940 return err;
941}
942
943static int parse_dirfile(char *buf, size_t nbytes, struct file *file,
944 void *dstbuf, filldir_t filldir)
945{
946 while (nbytes >= FUSE_NAME_OFFSET) {
947 struct fuse_dirent *dirent = (struct fuse_dirent *) buf;
948 size_t reclen = FUSE_DIRENT_SIZE(dirent);
949 int over;
950 if (!dirent->namelen || dirent->namelen > FUSE_NAME_MAX)
951 return -EIO;
952 if (reclen > nbytes)
953 break;
954
955 over = filldir(dstbuf, dirent->name, dirent->namelen,
956 file->f_pos, dirent->ino, dirent->type);
957 if (over)
958 break;
959
960 buf += reclen;
961 nbytes -= reclen;
962 file->f_pos = dirent->off;
963 }
964
965 return 0;
966}
967
968static int fuse_readdir(struct file *file, void *dstbuf, filldir_t filldir)
969{
970 int err;
971 size_t nbytes;
972 struct page *page;
973 struct inode *inode = file->f_path.dentry->d_inode;
974 struct fuse_conn *fc = get_fuse_conn(inode);
975 struct fuse_req *req;
976
977 if (is_bad_inode(inode))
978 return -EIO;
979
980 req = fuse_get_req(fc);
981 if (IS_ERR(req))
982 return PTR_ERR(req);
983
984 page = alloc_page(GFP_KERNEL);
985 if (!page) {
986 fuse_put_request(fc, req);
987 return -ENOMEM;
988 }
989 req->num_pages = 1;
990 req->pages[0] = page;
991 fuse_read_fill(req, file, inode, file->f_pos, PAGE_SIZE, FUSE_READDIR);
992 request_send(fc, req);
993 nbytes = req->out.args[0].size;
994 err = req->out.h.error;
995 fuse_put_request(fc, req);
996 if (!err)
997 err = parse_dirfile(page_address(page), nbytes, file, dstbuf,
998 filldir);
999
1000 __free_page(page);
1001 fuse_invalidate_attr(inode); /* atime changed */
1002 return err;
1003}
1004
1005static char *read_link(struct dentry *dentry)
1006{
1007 struct inode *inode = dentry->d_inode;
1008 struct fuse_conn *fc = get_fuse_conn(inode);
1009 struct fuse_req *req = fuse_get_req(fc);
1010 char *link;
1011
1012 if (IS_ERR(req))
1013 return ERR_CAST(req);
1014
1015 link = (char *) __get_free_page(GFP_KERNEL);
1016 if (!link) {
1017 link = ERR_PTR(-ENOMEM);
1018 goto out;
1019 }
1020 req->in.h.opcode = FUSE_READLINK;
1021 req->in.h.nodeid = get_node_id(inode);
1022 req->out.argvar = 1;
1023 req->out.numargs = 1;
1024 req->out.args[0].size = PAGE_SIZE - 1;
1025 req->out.args[0].value = link;
1026 request_send(fc, req);
1027 if (req->out.h.error) {
1028 free_page((unsigned long) link);
1029 link = ERR_PTR(req->out.h.error);
1030 } else
1031 link[req->out.args[0].size] = '\0';
1032 out:
1033 fuse_put_request(fc, req);
1034 fuse_invalidate_attr(inode); /* atime changed */
1035 return link;
1036}
1037
1038static void free_link(char *link)
1039{
1040 if (!IS_ERR(link))
1041 free_page((unsigned long) link);
1042}
1043
1044static void *fuse_follow_link(struct dentry *dentry, struct nameidata *nd)
1045{
1046 nd_set_link(nd, read_link(dentry));
1047 return NULL;
1048}
1049
1050static void fuse_put_link(struct dentry *dentry, struct nameidata *nd, void *c)
1051{
1052 free_link(nd_get_link(nd));
1053}
1054
1055static int fuse_dir_open(struct inode *inode, struct file *file)
1056{
1057 return fuse_open_common(inode, file, 1);
1058}
1059
1060static int fuse_dir_release(struct inode *inode, struct file *file)
1061{
1062 return fuse_release_common(inode, file, 1);
1063}
1064
1065static int fuse_dir_fsync(struct file *file, struct dentry *de, int datasync)
1066{
1067 /* nfsd can call this with no file */
1068 return file ? fuse_fsync_common(file, de, datasync, 1) : 0;
1069}
1070
1071static bool update_mtime(unsigned ivalid)
1072{
1073 /* Always update if mtime is explicitly set */
1074 if (ivalid & ATTR_MTIME_SET)
1075 return true;
1076
1077 /* If it's an open(O_TRUNC) or an ftruncate(), don't update */
1078 if ((ivalid & ATTR_SIZE) && (ivalid & (ATTR_OPEN | ATTR_FILE)))
1079 return false;
1080
1081 /* In all other cases update */
1082 return true;
1083}
1084
1085static void iattr_to_fattr(struct iattr *iattr, struct fuse_setattr_in *arg)
1086{
1087 unsigned ivalid = iattr->ia_valid;
1088
1089 if (ivalid & ATTR_MODE)
1090 arg->valid |= FATTR_MODE, arg->mode = iattr->ia_mode;
1091 if (ivalid & ATTR_UID)
1092 arg->valid |= FATTR_UID, arg->uid = iattr->ia_uid;
1093 if (ivalid & ATTR_GID)
1094 arg->valid |= FATTR_GID, arg->gid = iattr->ia_gid;
1095 if (ivalid & ATTR_SIZE)
1096 arg->valid |= FATTR_SIZE, arg->size = iattr->ia_size;
1097 if (ivalid & ATTR_ATIME) {
1098 arg->valid |= FATTR_ATIME;
1099 arg->atime = iattr->ia_atime.tv_sec;
1100 arg->atimensec = iattr->ia_atime.tv_nsec;
1101 if (!(ivalid & ATTR_ATIME_SET))
1102 arg->valid |= FATTR_ATIME_NOW;
1103 }
1104 if ((ivalid & ATTR_MTIME) && update_mtime(ivalid)) {
1105 arg->valid |= FATTR_MTIME;
1106 arg->mtime = iattr->ia_mtime.tv_sec;
1107 arg->mtimensec = iattr->ia_mtime.tv_nsec;
1108 if (!(ivalid & ATTR_MTIME_SET))
1109 arg->valid |= FATTR_MTIME_NOW;
1110 }
1111}
1112
1113/*
1114 * Prevent concurrent writepages on inode
1115 *
1116 * This is done by adding a negative bias to the inode write counter
1117 * and waiting for all pending writes to finish.
1118 */
1119void fuse_set_nowrite(struct inode *inode)
1120{
1121 struct fuse_conn *fc = get_fuse_conn(inode);
1122 struct fuse_inode *fi = get_fuse_inode(inode);
1123
1124 BUG_ON(!mutex_is_locked(&inode->i_mutex));
1125
1126 spin_lock(&fc->lock);
1127 BUG_ON(fi->writectr < 0);
1128 fi->writectr += FUSE_NOWRITE;
1129 spin_unlock(&fc->lock);
1130 wait_event(fi->page_waitq, fi->writectr == FUSE_NOWRITE);
1131}
1132
1133/*
1134 * Allow writepages on inode
1135 *
1136 * Remove the bias from the writecounter and send any queued
1137 * writepages.
1138 */
1139static void __fuse_release_nowrite(struct inode *inode)
1140{
1141 struct fuse_inode *fi = get_fuse_inode(inode);
1142
1143 BUG_ON(fi->writectr != FUSE_NOWRITE);
1144 fi->writectr = 0;
1145 fuse_flush_writepages(inode);
1146}
1147
1148void fuse_release_nowrite(struct inode *inode)
1149{
1150 struct fuse_conn *fc = get_fuse_conn(inode);
1151
1152 spin_lock(&fc->lock);
1153 __fuse_release_nowrite(inode);
1154 spin_unlock(&fc->lock);
1155}
1156
1157/*
1158 * Set attributes, and at the same time refresh them.
1159 *
1160 * Truncation is slightly complicated, because the 'truncate' request
1161 * may fail, in which case we don't want to touch the mapping.
1162 * vmtruncate() doesn't allow for this case, so do the rlimit checking
1163 * and the actual truncation by hand.
1164 */
1165static int fuse_do_setattr(struct dentry *entry, struct iattr *attr,
1166 struct file *file)
1167{
1168 struct inode *inode = entry->d_inode;
1169 struct fuse_conn *fc = get_fuse_conn(inode);
1170 struct fuse_req *req;
1171 struct fuse_setattr_in inarg;
1172 struct fuse_attr_out outarg;
1173 bool is_truncate = false;
1174 loff_t oldsize;
1175 int err;
1176
1177 if (!fuse_allow_task(fc, current))
1178 return -EACCES;
1179
1180 if (fc->flags & FUSE_DEFAULT_PERMISSIONS) {
1181 err = inode_change_ok(inode, attr);
1182 if (err)
1183 return err;
1184 }
1185
1186 if ((attr->ia_valid & ATTR_OPEN) && fc->atomic_o_trunc)
1187 return 0;
1188
1189 if (attr->ia_valid & ATTR_SIZE) {
1190 unsigned long limit;
1191 if (IS_SWAPFILE(inode))
1192 return -ETXTBSY;
1193 limit = current->signal->rlim[RLIMIT_FSIZE].rlim_cur;
1194 if (limit != RLIM_INFINITY && attr->ia_size > (loff_t) limit) {
1195 send_sig(SIGXFSZ, current, 0);
1196 return -EFBIG;
1197 }
1198 is_truncate = true;
1199 }
1200
1201 req = fuse_get_req(fc);
1202 if (IS_ERR(req))
1203 return PTR_ERR(req);
1204
1205 if (is_truncate)
1206 fuse_set_nowrite(inode);
1207
1208 memset(&inarg, 0, sizeof(inarg));
1209 memset(&outarg, 0, sizeof(outarg));
1210 iattr_to_fattr(attr, &inarg);
1211 if (file) {
1212 struct fuse_file *ff = file->private_data;
1213 inarg.valid |= FATTR_FH;
1214 inarg.fh = ff->fh;
1215 }
1216 if (attr->ia_valid & ATTR_SIZE) {
1217 /* For mandatory locking in truncate */
1218 inarg.valid |= FATTR_LOCKOWNER;
1219 inarg.lock_owner = fuse_lock_owner_id(fc, current->files);
1220 }
1221 req->in.h.opcode = FUSE_SETATTR;
1222 req->in.h.nodeid = get_node_id(inode);
1223 req->in.numargs = 1;
1224 req->in.args[0].size = sizeof(inarg);
1225 req->in.args[0].value = &inarg;
1226 req->out.numargs = 1;
1227 if (fc->minor < 9)
1228 req->out.args[0].size = FUSE_COMPAT_ATTR_OUT_SIZE;
1229 else
1230 req->out.args[0].size = sizeof(outarg);
1231 req->out.args[0].value = &outarg;
1232 request_send(fc, req);
1233 err = req->out.h.error;
1234 fuse_put_request(fc, req);
1235 if (err) {
1236 if (err == -EINTR)
1237 fuse_invalidate_attr(inode);
1238 goto error;
1239 }
1240
1241 if ((inode->i_mode ^ outarg.attr.mode) & S_IFMT) {
1242 make_bad_inode(inode);
1243 err = -EIO;
1244 goto error;
1245 }
1246
1247 spin_lock(&fc->lock);
1248 fuse_change_attributes_common(inode, &outarg.attr,
1249 attr_timeout(&outarg));
1250 oldsize = inode->i_size;
1251 i_size_write(inode, outarg.attr.size);
1252
1253 if (is_truncate) {
1254 /* NOTE: this may release/reacquire fc->lock */
1255 __fuse_release_nowrite(inode);
1256 }
1257 spin_unlock(&fc->lock);
1258
1259 /*
1260 * Only call invalidate_inode_pages2() after removing
1261 * FUSE_NOWRITE, otherwise fuse_launder_page() would deadlock.
1262 */
1263 if (S_ISREG(inode->i_mode) && oldsize != outarg.attr.size) {
1264 if (outarg.attr.size < oldsize)
1265 fuse_truncate(inode->i_mapping, outarg.attr.size);
1266 invalidate_inode_pages2(inode->i_mapping);
1267 }
1268
1269 return 0;
1270
1271error:
1272 if (is_truncate)
1273 fuse_release_nowrite(inode);
1274
1275 return err;
1276}
1277
1278static int fuse_setattr(struct dentry *entry, struct iattr *attr)
1279{
1280 if (attr->ia_valid & ATTR_FILE)
1281 return fuse_do_setattr(entry, attr, attr->ia_file);
1282 else
1283 return fuse_do_setattr(entry, attr, NULL);
1284}
1285
1286static int fuse_getattr(struct vfsmount *mnt, struct dentry *entry,
1287 struct kstat *stat)
1288{
1289 struct inode *inode = entry->d_inode;
1290 struct fuse_conn *fc = get_fuse_conn(inode);
1291
1292 if (!fuse_allow_task(fc, current))
1293 return -EACCES;
1294
1295 return fuse_update_attributes(inode, stat, NULL, NULL);
1296}
1297
1298static int fuse_setxattr(struct dentry *entry, const char *name,
1299 const void *value, size_t size, int flags)
1300{
1301 struct inode *inode = entry->d_inode;
1302 struct fuse_conn *fc = get_fuse_conn(inode);
1303 struct fuse_req *req;
1304 struct fuse_setxattr_in inarg;
1305 int err;
1306
1307 if (fc->no_setxattr)
1308 return -EOPNOTSUPP;
1309
1310 req = fuse_get_req(fc);
1311 if (IS_ERR(req))
1312 return PTR_ERR(req);
1313
1314 memset(&inarg, 0, sizeof(inarg));
1315 inarg.size = size;
1316 inarg.flags = flags;
1317 req->in.h.opcode = FUSE_SETXATTR;
1318 req->in.h.nodeid = get_node_id(inode);
1319 req->in.numargs = 3;
1320 req->in.args[0].size = sizeof(inarg);
1321 req->in.args[0].value = &inarg;
1322 req->in.args[1].size = strlen(name) + 1;
1323 req->in.args[1].value = name;
1324 req->in.args[2].size = size;
1325 req->in.args[2].value = value;
1326 request_send(fc, req);
1327 err = req->out.h.error;
1328 fuse_put_request(fc, req);
1329 if (err == -ENOSYS) {
1330 fc->no_setxattr = 1;
1331 err = -EOPNOTSUPP;
1332 }
1333 return err;
1334}
1335
1336static ssize_t fuse_getxattr(struct dentry *entry, const char *name,
1337 void *value, size_t size)
1338{
1339 struct inode *inode = entry->d_inode;
1340 struct fuse_conn *fc = get_fuse_conn(inode);
1341 struct fuse_req *req;
1342 struct fuse_getxattr_in inarg;
1343 struct fuse_getxattr_out outarg;
1344 ssize_t ret;
1345
1346 if (fc->no_getxattr)
1347 return -EOPNOTSUPP;
1348
1349 req = fuse_get_req(fc);
1350 if (IS_ERR(req))
1351 return PTR_ERR(req);
1352
1353 memset(&inarg, 0, sizeof(inarg));
1354 inarg.size = size;
1355 req->in.h.opcode = FUSE_GETXATTR;
1356 req->in.h.nodeid = get_node_id(inode);
1357 req->in.numargs = 2;
1358 req->in.args[0].size = sizeof(inarg);
1359 req->in.args[0].value = &inarg;
1360 req->in.args[1].size = strlen(name) + 1;
1361 req->in.args[1].value = name;
1362 /* This is really two different operations rolled into one */
1363 req->out.numargs = 1;
1364 if (size) {
1365 req->out.argvar = 1;
1366 req->out.args[0].size = size;
1367 req->out.args[0].value = value;
1368 } else {
1369 req->out.args[0].size = sizeof(outarg);
1370 req->out.args[0].value = &outarg;
1371 }
1372 request_send(fc, req);
1373 ret = req->out.h.error;
1374 if (!ret)
1375 ret = size ? req->out.args[0].size : outarg.size;
1376 else {
1377 if (ret == -ENOSYS) {
1378 fc->no_getxattr = 1;
1379 ret = -EOPNOTSUPP;
1380 }
1381 }
1382 fuse_put_request(fc, req);
1383 return ret;
1384}
1385
1386static ssize_t fuse_listxattr(struct dentry *entry, char *list, size_t size)
1387{
1388 struct inode *inode = entry->d_inode;
1389 struct fuse_conn *fc = get_fuse_conn(inode);
1390 struct fuse_req *req;
1391 struct fuse_getxattr_in inarg;
1392 struct fuse_getxattr_out outarg;
1393 ssize_t ret;
1394
1395 if (!fuse_allow_task(fc, current))
1396 return -EACCES;
1397
1398 if (fc->no_listxattr)
1399 return -EOPNOTSUPP;
1400
1401 req = fuse_get_req(fc);
1402 if (IS_ERR(req))
1403 return PTR_ERR(req);
1404
1405 memset(&inarg, 0, sizeof(inarg));
1406 inarg.size = size;
1407 req->in.h.opcode = FUSE_LISTXATTR;
1408 req->in.h.nodeid = get_node_id(inode);
1409 req->in.numargs = 1;
1410 req->in.args[0].size = sizeof(inarg);
1411 req->in.args[0].value = &inarg;
1412 /* This is really two different operations rolled into one */
1413 req->out.numargs = 1;
1414 if (size) {
1415 req->out.argvar = 1;
1416 req->out.args[0].size = size;
1417 req->out.args[0].value = list;
1418 } else {
1419 req->out.args[0].size = sizeof(outarg);
1420 req->out.args[0].value = &outarg;
1421 }
1422 request_send(fc, req);
1423 ret = req->out.h.error;
1424 if (!ret)
1425 ret = size ? req->out.args[0].size : outarg.size;
1426 else {
1427 if (ret == -ENOSYS) {
1428 fc->no_listxattr = 1;
1429 ret = -EOPNOTSUPP;
1430 }
1431 }
1432 fuse_put_request(fc, req);
1433 return ret;
1434}
1435
1436static int fuse_removexattr(struct dentry *entry, const char *name)
1437{
1438 struct inode *inode = entry->d_inode;
1439 struct fuse_conn *fc = get_fuse_conn(inode);
1440 struct fuse_req *req;
1441 int err;
1442
1443 if (fc->no_removexattr)
1444 return -EOPNOTSUPP;
1445
1446 req = fuse_get_req(fc);
1447 if (IS_ERR(req))
1448 return PTR_ERR(req);
1449
1450 req->in.h.opcode = FUSE_REMOVEXATTR;
1451 req->in.h.nodeid = get_node_id(inode);
1452 req->in.numargs = 1;
1453 req->in.args[0].size = strlen(name) + 1;
1454 req->in.args[0].value = name;
1455 request_send(fc, req);
1456 err = req->out.h.error;
1457 fuse_put_request(fc, req);
1458 if (err == -ENOSYS) {
1459 fc->no_removexattr = 1;
1460 err = -EOPNOTSUPP;
1461 }
1462 return err;
1463}
1464
1465static const struct inode_operations fuse_dir_inode_operations = {
1466 .lookup = fuse_lookup,
1467 .mkdir = fuse_mkdir,
1468 .symlink = fuse_symlink,
1469 .unlink = fuse_unlink,
1470 .rmdir = fuse_rmdir,
1471 .rename = fuse_rename,
1472 .link = fuse_link,
1473 .setattr = fuse_setattr,
1474 .create = fuse_create,
1475 .mknod = fuse_mknod,
1476 .permission = fuse_permission,
1477 .getattr = fuse_getattr,
1478 .setxattr = fuse_setxattr,
1479 .getxattr = fuse_getxattr,
1480 .listxattr = fuse_listxattr,
1481 .removexattr = fuse_removexattr,
1482};
1483
1484static const struct file_operations fuse_dir_operations = {
1485 .llseek = generic_file_llseek,
1486 .read = generic_read_dir,
1487 .readdir = fuse_readdir,
1488 .open = fuse_dir_open,
1489 .release = fuse_dir_release,
1490 .fsync = fuse_dir_fsync,
1491};
1492
1493static const struct inode_operations fuse_common_inode_operations = {
1494 .setattr = fuse_setattr,
1495 .permission = fuse_permission,
1496 .getattr = fuse_getattr,
1497 .setxattr = fuse_setxattr,
1498 .getxattr = fuse_getxattr,
1499 .listxattr = fuse_listxattr,
1500 .removexattr = fuse_removexattr,
1501};
1502
1503static const struct inode_operations fuse_symlink_inode_operations = {
1504 .setattr = fuse_setattr,
1505 .follow_link = fuse_follow_link,
1506 .put_link = fuse_put_link,
1507 .readlink = generic_readlink,
1508 .getattr = fuse_getattr,
1509 .setxattr = fuse_setxattr,
1510 .getxattr = fuse_getxattr,
1511 .listxattr = fuse_listxattr,
1512 .removexattr = fuse_removexattr,
1513};
1514
1515void fuse_init_common(struct inode *inode)
1516{
1517 inode->i_op = &fuse_common_inode_operations;
1518}
1519
1520void fuse_init_dir(struct inode *inode)
1521{
1522 inode->i_op = &fuse_dir_inode_operations;
1523 inode->i_fop = &fuse_dir_operations;
1524}
1525
1526void fuse_init_symlink(struct inode *inode)
1527{
1528 inode->i_op = &fuse_symlink_inode_operations;
1529}