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