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