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