[SCSI] u14-34f: Fix 32bit only problem
[linux-2.6/lfs.git] / fs / fuse / dir.c
blobc4807b3fc8a360776bda6f976baebd26b5c9ae92
1 /*
2 FUSE: Filesystem in Userspace
3 Copyright (C) 2001-2006 Miklos Szeredi <miklos@szeredi.hu>
5 This program can be distributed under the terms of the GNU GPL.
6 See the file COPYING.
7 */
9 #include "fuse_i.h"
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>
17 #if BITS_PER_LONG >= 64
18 static inline void fuse_dentry_settime(struct dentry *entry, u64 time)
20 entry->d_time = time;
23 static inline u64 fuse_dentry_time(struct dentry *entry)
25 return entry->d_time;
27 #else
29 * On 32 bit archs store the high 32 bits of time in d_fsdata
31 static void fuse_dentry_settime(struct dentry *entry, u64 time)
33 entry->d_time = time;
34 entry->d_fsdata = (void *) (unsigned long) (time >> 32);
37 static u64 fuse_dentry_time(struct dentry *entry)
39 return (u64) entry->d_time +
40 ((u64) (unsigned long) entry->d_fsdata << 32);
42 #endif
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.
51 * Calculate the time in jiffies until a dentry/attributes are valid
53 static u64 time_to_jiffies(unsigned long sec, unsigned long nsec)
55 if (sec || nsec) {
56 struct timespec ts = {sec, nsec};
57 return get_jiffies_64() + timespec_to_jiffies(&ts);
58 } else
59 return 0;
63 * Set dentry and possibly attribute timeouts from the lookup/mk*
64 * replies
66 static void fuse_change_entry_timeout(struct dentry *entry,
67 struct fuse_entry_out *o)
69 fuse_dentry_settime(entry,
70 time_to_jiffies(o->entry_valid, o->entry_valid_nsec));
73 static u64 attr_timeout(struct fuse_attr_out *o)
75 return time_to_jiffies(o->attr_valid, o->attr_valid_nsec);
78 static u64 entry_attr_timeout(struct fuse_entry_out *o)
80 return time_to_jiffies(o->attr_valid, o->attr_valid_nsec);
84 * Mark the attributes as stale, so that at the next call to
85 * ->getattr() they will be fetched from userspace
87 void fuse_invalidate_attr(struct inode *inode)
89 get_fuse_inode(inode)->i_time = 0;
93 * Just mark the entry as stale, so that a next attempt to look it up
94 * will result in a new lookup call to userspace
96 * This is called when a dentry is about to become negative and the
97 * timeout is unknown (unlink, rmdir, rename and in some cases
98 * lookup)
100 static void fuse_invalidate_entry_cache(struct dentry *entry)
102 fuse_dentry_settime(entry, 0);
106 * Same as fuse_invalidate_entry_cache(), but also try to remove the
107 * dentry from the hash
109 static void fuse_invalidate_entry(struct dentry *entry)
111 d_invalidate(entry);
112 fuse_invalidate_entry_cache(entry);
115 static void fuse_lookup_init(struct fuse_req *req, struct inode *dir,
116 struct dentry *entry,
117 struct fuse_entry_out *outarg)
119 struct fuse_conn *fc = get_fuse_conn(dir);
121 memset(outarg, 0, sizeof(struct fuse_entry_out));
122 req->in.h.opcode = FUSE_LOOKUP;
123 req->in.h.nodeid = get_node_id(dir);
124 req->in.numargs = 1;
125 req->in.args[0].size = entry->d_name.len + 1;
126 req->in.args[0].value = entry->d_name.name;
127 req->out.numargs = 1;
128 if (fc->minor < 9)
129 req->out.args[0].size = FUSE_COMPAT_ENTRY_OUT_SIZE;
130 else
131 req->out.args[0].size = sizeof(struct fuse_entry_out);
132 req->out.args[0].value = outarg;
135 static u64 fuse_get_attr_version(struct fuse_conn *fc)
137 u64 curr_version;
140 * The spin lock isn't actually needed on 64bit archs, but we
141 * don't yet care too much about such optimizations.
143 spin_lock(&fc->lock);
144 curr_version = fc->attr_version;
145 spin_unlock(&fc->lock);
147 return curr_version;
151 * Check whether the dentry is still valid
153 * If the entry validity timeout has expired and the dentry is
154 * positive, try to redo the lookup. If the lookup results in a
155 * different inode, then let the VFS invalidate the dentry and redo
156 * the lookup once more. If the lookup results in the same inode,
157 * then refresh the attributes, timeouts and mark the dentry valid.
159 static int fuse_dentry_revalidate(struct dentry *entry, struct nameidata *nd)
161 struct inode *inode = entry->d_inode;
163 if (inode && is_bad_inode(inode))
164 return 0;
165 else if (fuse_dentry_time(entry) < get_jiffies_64()) {
166 int err;
167 struct fuse_entry_out outarg;
168 struct fuse_conn *fc;
169 struct fuse_req *req;
170 struct fuse_req *forget_req;
171 struct dentry *parent;
172 u64 attr_version;
174 /* For negative dentries, always do a fresh lookup */
175 if (!inode)
176 return 0;
178 fc = get_fuse_conn(inode);
179 req = fuse_get_req(fc);
180 if (IS_ERR(req))
181 return 0;
183 forget_req = fuse_get_req(fc);
184 if (IS_ERR(forget_req)) {
185 fuse_put_request(fc, req);
186 return 0;
189 attr_version = fuse_get_attr_version(fc);
191 parent = dget_parent(entry);
192 fuse_lookup_init(req, parent->d_inode, entry, &outarg);
193 request_send(fc, req);
194 dput(parent);
195 err = req->out.h.error;
196 fuse_put_request(fc, req);
197 /* Zero nodeid is same as -ENOENT */
198 if (!err && !outarg.nodeid)
199 err = -ENOENT;
200 if (!err) {
201 struct fuse_inode *fi = get_fuse_inode(inode);
202 if (outarg.nodeid != get_node_id(inode)) {
203 fuse_send_forget(fc, forget_req,
204 outarg.nodeid, 1);
205 return 0;
207 spin_lock(&fc->lock);
208 fi->nlookup ++;
209 spin_unlock(&fc->lock);
211 fuse_put_request(fc, forget_req);
212 if (err || (outarg.attr.mode ^ inode->i_mode) & S_IFMT)
213 return 0;
215 fuse_change_attributes(inode, &outarg.attr,
216 entry_attr_timeout(&outarg),
217 attr_version);
218 fuse_change_entry_timeout(entry, &outarg);
220 return 1;
223 static int invalid_nodeid(u64 nodeid)
225 return !nodeid || nodeid == FUSE_ROOT_ID;
228 static struct dentry_operations fuse_dentry_operations = {
229 .d_revalidate = fuse_dentry_revalidate,
232 int fuse_valid_type(int m)
234 return S_ISREG(m) || S_ISDIR(m) || S_ISLNK(m) || S_ISCHR(m) ||
235 S_ISBLK(m) || S_ISFIFO(m) || S_ISSOCK(m);
239 * Add a directory inode to a dentry, ensuring that no other dentry
240 * refers to this inode. Called with fc->inst_mutex.
242 static int fuse_d_add_directory(struct dentry *entry, struct inode *inode)
244 struct dentry *alias = d_find_alias(inode);
245 if (alias) {
246 /* This tries to shrink the subtree below alias */
247 fuse_invalidate_entry(alias);
248 dput(alias);
249 if (!list_empty(&inode->i_dentry))
250 return -EBUSY;
252 d_add(entry, inode);
253 return 0;
256 static struct dentry *fuse_lookup(struct inode *dir, struct dentry *entry,
257 struct nameidata *nd)
259 int err;
260 struct fuse_entry_out outarg;
261 struct inode *inode = NULL;
262 struct fuse_conn *fc = get_fuse_conn(dir);
263 struct fuse_req *req;
264 struct fuse_req *forget_req;
265 u64 attr_version;
267 if (entry->d_name.len > FUSE_NAME_MAX)
268 return ERR_PTR(-ENAMETOOLONG);
270 req = fuse_get_req(fc);
271 if (IS_ERR(req))
272 return ERR_CAST(req);
274 forget_req = fuse_get_req(fc);
275 if (IS_ERR(forget_req)) {
276 fuse_put_request(fc, req);
277 return ERR_CAST(forget_req);
280 attr_version = fuse_get_attr_version(fc);
282 fuse_lookup_init(req, dir, entry, &outarg);
283 request_send(fc, req);
284 err = req->out.h.error;
285 fuse_put_request(fc, req);
286 /* Zero nodeid is same as -ENOENT, but with valid timeout */
287 if (!err && outarg.nodeid &&
288 (invalid_nodeid(outarg.nodeid) ||
289 !fuse_valid_type(outarg.attr.mode)))
290 err = -EIO;
291 if (!err && outarg.nodeid) {
292 inode = fuse_iget(dir->i_sb, outarg.nodeid, outarg.generation,
293 &outarg.attr, entry_attr_timeout(&outarg),
294 attr_version);
295 if (!inode) {
296 fuse_send_forget(fc, forget_req, outarg.nodeid, 1);
297 return ERR_PTR(-ENOMEM);
300 fuse_put_request(fc, forget_req);
301 if (err && err != -ENOENT)
302 return ERR_PTR(err);
304 if (inode && S_ISDIR(inode->i_mode)) {
305 mutex_lock(&fc->inst_mutex);
306 err = fuse_d_add_directory(entry, inode);
307 mutex_unlock(&fc->inst_mutex);
308 if (err) {
309 iput(inode);
310 return ERR_PTR(err);
312 } else
313 d_add(entry, inode);
315 entry->d_op = &fuse_dentry_operations;
316 if (!err)
317 fuse_change_entry_timeout(entry, &outarg);
318 else
319 fuse_invalidate_entry_cache(entry);
320 return NULL;
324 * Synchronous release for the case when something goes wrong in CREATE_OPEN
326 static void fuse_sync_release(struct fuse_conn *fc, struct fuse_file *ff,
327 u64 nodeid, int flags)
329 fuse_release_fill(ff, nodeid, flags, FUSE_RELEASE);
330 ff->reserved_req->force = 1;
331 request_send(fc, ff->reserved_req);
332 fuse_put_request(fc, ff->reserved_req);
333 kfree(ff);
337 * Atomic create+open operation
339 * If the filesystem doesn't support this, then fall back to separate
340 * 'mknod' + 'open' requests.
342 static int fuse_create_open(struct inode *dir, struct dentry *entry, int mode,
343 struct nameidata *nd)
345 int err;
346 struct inode *inode;
347 struct fuse_conn *fc = get_fuse_conn(dir);
348 struct fuse_req *req;
349 struct fuse_req *forget_req;
350 struct fuse_open_in inarg;
351 struct fuse_open_out outopen;
352 struct fuse_entry_out outentry;
353 struct fuse_file *ff;
354 struct file *file;
355 int flags = nd->intent.open.flags - 1;
357 if (fc->no_create)
358 return -ENOSYS;
360 forget_req = fuse_get_req(fc);
361 if (IS_ERR(forget_req))
362 return PTR_ERR(forget_req);
364 req = fuse_get_req(fc);
365 err = PTR_ERR(req);
366 if (IS_ERR(req))
367 goto out_put_forget_req;
369 err = -ENOMEM;
370 ff = fuse_file_alloc();
371 if (!ff)
372 goto out_put_request;
374 flags &= ~O_NOCTTY;
375 memset(&inarg, 0, sizeof(inarg));
376 memset(&outentry, 0, sizeof(outentry));
377 inarg.flags = flags;
378 inarg.mode = mode;
379 req->in.h.opcode = FUSE_CREATE;
380 req->in.h.nodeid = get_node_id(dir);
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 req->out.numargs = 2;
387 if (fc->minor < 9)
388 req->out.args[0].size = FUSE_COMPAT_ENTRY_OUT_SIZE;
389 else
390 req->out.args[0].size = sizeof(outentry);
391 req->out.args[0].value = &outentry;
392 req->out.args[1].size = sizeof(outopen);
393 req->out.args[1].value = &outopen;
394 request_send(fc, req);
395 err = req->out.h.error;
396 if (err) {
397 if (err == -ENOSYS)
398 fc->no_create = 1;
399 goto out_free_ff;
402 err = -EIO;
403 if (!S_ISREG(outentry.attr.mode) || invalid_nodeid(outentry.nodeid))
404 goto out_free_ff;
406 fuse_put_request(fc, req);
407 inode = fuse_iget(dir->i_sb, outentry.nodeid, outentry.generation,
408 &outentry.attr, entry_attr_timeout(&outentry), 0);
409 if (!inode) {
410 flags &= ~(O_CREAT | O_EXCL | O_TRUNC);
411 ff->fh = outopen.fh;
412 fuse_sync_release(fc, ff, outentry.nodeid, flags);
413 fuse_send_forget(fc, forget_req, outentry.nodeid, 1);
414 return -ENOMEM;
416 fuse_put_request(fc, forget_req);
417 d_instantiate(entry, inode);
418 fuse_change_entry_timeout(entry, &outentry);
419 fuse_invalidate_attr(dir);
420 file = lookup_instantiate_filp(nd, entry, generic_file_open);
421 if (IS_ERR(file)) {
422 ff->fh = outopen.fh;
423 fuse_sync_release(fc, ff, outentry.nodeid, flags);
424 return PTR_ERR(file);
426 fuse_finish_open(inode, file, ff, &outopen);
427 return 0;
429 out_free_ff:
430 fuse_file_free(ff);
431 out_put_request:
432 fuse_put_request(fc, req);
433 out_put_forget_req:
434 fuse_put_request(fc, forget_req);
435 return err;
439 * Code shared between mknod, mkdir, symlink and link
441 static int create_new_entry(struct fuse_conn *fc, struct fuse_req *req,
442 struct inode *dir, struct dentry *entry,
443 int mode)
445 struct fuse_entry_out outarg;
446 struct inode *inode;
447 int err;
448 struct fuse_req *forget_req;
450 forget_req = fuse_get_req(fc);
451 if (IS_ERR(forget_req)) {
452 fuse_put_request(fc, req);
453 return PTR_ERR(forget_req);
456 memset(&outarg, 0, sizeof(outarg));
457 req->in.h.nodeid = get_node_id(dir);
458 req->out.numargs = 1;
459 if (fc->minor < 9)
460 req->out.args[0].size = FUSE_COMPAT_ENTRY_OUT_SIZE;
461 else
462 req->out.args[0].size = sizeof(outarg);
463 req->out.args[0].value = &outarg;
464 request_send(fc, req);
465 err = req->out.h.error;
466 fuse_put_request(fc, req);
467 if (err)
468 goto out_put_forget_req;
470 err = -EIO;
471 if (invalid_nodeid(outarg.nodeid))
472 goto out_put_forget_req;
474 if ((outarg.attr.mode ^ mode) & S_IFMT)
475 goto out_put_forget_req;
477 inode = fuse_iget(dir->i_sb, outarg.nodeid, outarg.generation,
478 &outarg.attr, entry_attr_timeout(&outarg), 0);
479 if (!inode) {
480 fuse_send_forget(fc, forget_req, outarg.nodeid, 1);
481 return -ENOMEM;
483 fuse_put_request(fc, forget_req);
485 if (S_ISDIR(inode->i_mode)) {
486 struct dentry *alias;
487 mutex_lock(&fc->inst_mutex);
488 alias = d_find_alias(inode);
489 if (alias) {
490 /* New directory must have moved since mkdir */
491 mutex_unlock(&fc->inst_mutex);
492 dput(alias);
493 iput(inode);
494 return -EBUSY;
496 d_instantiate(entry, inode);
497 mutex_unlock(&fc->inst_mutex);
498 } else
499 d_instantiate(entry, inode);
501 fuse_change_entry_timeout(entry, &outarg);
502 fuse_invalidate_attr(dir);
503 return 0;
505 out_put_forget_req:
506 fuse_put_request(fc, forget_req);
507 return err;
510 static int fuse_mknod(struct inode *dir, struct dentry *entry, int mode,
511 dev_t rdev)
513 struct fuse_mknod_in inarg;
514 struct fuse_conn *fc = get_fuse_conn(dir);
515 struct fuse_req *req = fuse_get_req(fc);
516 if (IS_ERR(req))
517 return PTR_ERR(req);
519 memset(&inarg, 0, sizeof(inarg));
520 inarg.mode = mode;
521 inarg.rdev = new_encode_dev(rdev);
522 req->in.h.opcode = FUSE_MKNOD;
523 req->in.numargs = 2;
524 req->in.args[0].size = sizeof(inarg);
525 req->in.args[0].value = &inarg;
526 req->in.args[1].size = entry->d_name.len + 1;
527 req->in.args[1].value = entry->d_name.name;
528 return create_new_entry(fc, req, dir, entry, mode);
531 static int fuse_create(struct inode *dir, struct dentry *entry, int mode,
532 struct nameidata *nd)
534 if (nd && (nd->flags & LOOKUP_OPEN)) {
535 int err = fuse_create_open(dir, entry, mode, nd);
536 if (err != -ENOSYS)
537 return err;
538 /* Fall back on mknod */
540 return fuse_mknod(dir, entry, mode, 0);
543 static int fuse_mkdir(struct inode *dir, struct dentry *entry, int mode)
545 struct fuse_mkdir_in inarg;
546 struct fuse_conn *fc = get_fuse_conn(dir);
547 struct fuse_req *req = fuse_get_req(fc);
548 if (IS_ERR(req))
549 return PTR_ERR(req);
551 memset(&inarg, 0, sizeof(inarg));
552 inarg.mode = mode;
553 req->in.h.opcode = FUSE_MKDIR;
554 req->in.numargs = 2;
555 req->in.args[0].size = sizeof(inarg);
556 req->in.args[0].value = &inarg;
557 req->in.args[1].size = entry->d_name.len + 1;
558 req->in.args[1].value = entry->d_name.name;
559 return create_new_entry(fc, req, dir, entry, S_IFDIR);
562 static int fuse_symlink(struct inode *dir, struct dentry *entry,
563 const char *link)
565 struct fuse_conn *fc = get_fuse_conn(dir);
566 unsigned len = strlen(link) + 1;
567 struct fuse_req *req = fuse_get_req(fc);
568 if (IS_ERR(req))
569 return PTR_ERR(req);
571 req->in.h.opcode = FUSE_SYMLINK;
572 req->in.numargs = 2;
573 req->in.args[0].size = entry->d_name.len + 1;
574 req->in.args[0].value = entry->d_name.name;
575 req->in.args[1].size = len;
576 req->in.args[1].value = link;
577 return create_new_entry(fc, req, dir, entry, S_IFLNK);
580 static int fuse_unlink(struct inode *dir, struct dentry *entry)
582 int err;
583 struct fuse_conn *fc = get_fuse_conn(dir);
584 struct fuse_req *req = fuse_get_req(fc);
585 if (IS_ERR(req))
586 return PTR_ERR(req);
588 req->in.h.opcode = FUSE_UNLINK;
589 req->in.h.nodeid = get_node_id(dir);
590 req->in.numargs = 1;
591 req->in.args[0].size = entry->d_name.len + 1;
592 req->in.args[0].value = entry->d_name.name;
593 request_send(fc, req);
594 err = req->out.h.error;
595 fuse_put_request(fc, req);
596 if (!err) {
597 struct inode *inode = entry->d_inode;
599 /* Set nlink to zero so the inode can be cleared, if
600 the inode does have more links this will be
601 discovered at the next lookup/getattr */
602 clear_nlink(inode);
603 fuse_invalidate_attr(inode);
604 fuse_invalidate_attr(dir);
605 fuse_invalidate_entry_cache(entry);
606 } else if (err == -EINTR)
607 fuse_invalidate_entry(entry);
608 return err;
611 static int fuse_rmdir(struct inode *dir, struct dentry *entry)
613 int err;
614 struct fuse_conn *fc = get_fuse_conn(dir);
615 struct fuse_req *req = fuse_get_req(fc);
616 if (IS_ERR(req))
617 return PTR_ERR(req);
619 req->in.h.opcode = FUSE_RMDIR;
620 req->in.h.nodeid = get_node_id(dir);
621 req->in.numargs = 1;
622 req->in.args[0].size = entry->d_name.len + 1;
623 req->in.args[0].value = entry->d_name.name;
624 request_send(fc, req);
625 err = req->out.h.error;
626 fuse_put_request(fc, req);
627 if (!err) {
628 clear_nlink(entry->d_inode);
629 fuse_invalidate_attr(dir);
630 fuse_invalidate_entry_cache(entry);
631 } else if (err == -EINTR)
632 fuse_invalidate_entry(entry);
633 return err;
636 static int fuse_rename(struct inode *olddir, struct dentry *oldent,
637 struct inode *newdir, struct dentry *newent)
639 int err;
640 struct fuse_rename_in inarg;
641 struct fuse_conn *fc = get_fuse_conn(olddir);
642 struct fuse_req *req = fuse_get_req(fc);
643 if (IS_ERR(req))
644 return PTR_ERR(req);
646 memset(&inarg, 0, sizeof(inarg));
647 inarg.newdir = get_node_id(newdir);
648 req->in.h.opcode = FUSE_RENAME;
649 req->in.h.nodeid = get_node_id(olddir);
650 req->in.numargs = 3;
651 req->in.args[0].size = sizeof(inarg);
652 req->in.args[0].value = &inarg;
653 req->in.args[1].size = oldent->d_name.len + 1;
654 req->in.args[1].value = oldent->d_name.name;
655 req->in.args[2].size = newent->d_name.len + 1;
656 req->in.args[2].value = newent->d_name.name;
657 request_send(fc, req);
658 err = req->out.h.error;
659 fuse_put_request(fc, req);
660 if (!err) {
661 /* ctime changes */
662 fuse_invalidate_attr(oldent->d_inode);
664 fuse_invalidate_attr(olddir);
665 if (olddir != newdir)
666 fuse_invalidate_attr(newdir);
668 /* newent will end up negative */
669 if (newent->d_inode)
670 fuse_invalidate_entry_cache(newent);
671 } else if (err == -EINTR) {
672 /* If request was interrupted, DEITY only knows if the
673 rename actually took place. If the invalidation
674 fails (e.g. some process has CWD under the renamed
675 directory), then there can be inconsistency between
676 the dcache and the real filesystem. Tough luck. */
677 fuse_invalidate_entry(oldent);
678 if (newent->d_inode)
679 fuse_invalidate_entry(newent);
682 return err;
685 static int fuse_link(struct dentry *entry, struct inode *newdir,
686 struct dentry *newent)
688 int err;
689 struct fuse_link_in inarg;
690 struct inode *inode = entry->d_inode;
691 struct fuse_conn *fc = get_fuse_conn(inode);
692 struct fuse_req *req = fuse_get_req(fc);
693 if (IS_ERR(req))
694 return PTR_ERR(req);
696 memset(&inarg, 0, sizeof(inarg));
697 inarg.oldnodeid = get_node_id(inode);
698 req->in.h.opcode = FUSE_LINK;
699 req->in.numargs = 2;
700 req->in.args[0].size = sizeof(inarg);
701 req->in.args[0].value = &inarg;
702 req->in.args[1].size = newent->d_name.len + 1;
703 req->in.args[1].value = newent->d_name.name;
704 err = create_new_entry(fc, req, newdir, newent, inode->i_mode);
705 /* Contrary to "normal" filesystems it can happen that link
706 makes two "logical" inodes point to the same "physical"
707 inode. We invalidate the attributes of the old one, so it
708 will reflect changes in the backing inode (link count,
709 etc.)
711 if (!err || err == -EINTR)
712 fuse_invalidate_attr(inode);
713 return err;
716 static void fuse_fillattr(struct inode *inode, struct fuse_attr *attr,
717 struct kstat *stat)
719 stat->dev = inode->i_sb->s_dev;
720 stat->ino = attr->ino;
721 stat->mode = (inode->i_mode & S_IFMT) | (attr->mode & 07777);
722 stat->nlink = attr->nlink;
723 stat->uid = attr->uid;
724 stat->gid = attr->gid;
725 stat->rdev = inode->i_rdev;
726 stat->atime.tv_sec = attr->atime;
727 stat->atime.tv_nsec = attr->atimensec;
728 stat->mtime.tv_sec = attr->mtime;
729 stat->mtime.tv_nsec = attr->mtimensec;
730 stat->ctime.tv_sec = attr->ctime;
731 stat->ctime.tv_nsec = attr->ctimensec;
732 stat->size = attr->size;
733 stat->blocks = attr->blocks;
734 stat->blksize = (1 << inode->i_blkbits);
737 static int fuse_do_getattr(struct inode *inode, struct kstat *stat,
738 struct file *file)
740 int err;
741 struct fuse_getattr_in inarg;
742 struct fuse_attr_out outarg;
743 struct fuse_conn *fc = get_fuse_conn(inode);
744 struct fuse_req *req;
745 u64 attr_version;
747 req = fuse_get_req(fc);
748 if (IS_ERR(req))
749 return PTR_ERR(req);
751 attr_version = fuse_get_attr_version(fc);
753 memset(&inarg, 0, sizeof(inarg));
754 memset(&outarg, 0, sizeof(outarg));
755 /* Directories have separate file-handle space */
756 if (file && S_ISREG(inode->i_mode)) {
757 struct fuse_file *ff = file->private_data;
759 inarg.getattr_flags |= FUSE_GETATTR_FH;
760 inarg.fh = ff->fh;
762 req->in.h.opcode = FUSE_GETATTR;
763 req->in.h.nodeid = get_node_id(inode);
764 req->in.numargs = 1;
765 req->in.args[0].size = sizeof(inarg);
766 req->in.args[0].value = &inarg;
767 req->out.numargs = 1;
768 if (fc->minor < 9)
769 req->out.args[0].size = FUSE_COMPAT_ATTR_OUT_SIZE;
770 else
771 req->out.args[0].size = sizeof(outarg);
772 req->out.args[0].value = &outarg;
773 request_send(fc, req);
774 err = req->out.h.error;
775 fuse_put_request(fc, req);
776 if (!err) {
777 if ((inode->i_mode ^ outarg.attr.mode) & S_IFMT) {
778 make_bad_inode(inode);
779 err = -EIO;
780 } else {
781 fuse_change_attributes(inode, &outarg.attr,
782 attr_timeout(&outarg),
783 attr_version);
784 if (stat)
785 fuse_fillattr(inode, &outarg.attr, stat);
788 return err;
791 int fuse_update_attributes(struct inode *inode, struct kstat *stat,
792 struct file *file, bool *refreshed)
794 struct fuse_inode *fi = get_fuse_inode(inode);
795 int err;
796 bool r;
798 if (fi->i_time < get_jiffies_64()) {
799 r = true;
800 err = fuse_do_getattr(inode, stat, file);
801 } else {
802 r = false;
803 err = 0;
804 if (stat) {
805 generic_fillattr(inode, stat);
806 stat->mode = fi->orig_i_mode;
810 if (refreshed != NULL)
811 *refreshed = r;
813 return err;
817 * Calling into a user-controlled filesystem gives the filesystem
818 * daemon ptrace-like capabilities over the requester process. This
819 * means, that the filesystem daemon is able to record the exact
820 * filesystem operations performed, and can also control the behavior
821 * of the requester process in otherwise impossible ways. For example
822 * it can delay the operation for arbitrary length of time allowing
823 * DoS against the requester.
825 * For this reason only those processes can call into the filesystem,
826 * for which the owner of the mount has ptrace privilege. This
827 * excludes processes started by other users, suid or sgid processes.
829 int fuse_allow_task(struct fuse_conn *fc, struct task_struct *task)
831 if (fc->flags & FUSE_ALLOW_OTHER)
832 return 1;
834 if (task->euid == fc->user_id &&
835 task->suid == fc->user_id &&
836 task->uid == fc->user_id &&
837 task->egid == fc->group_id &&
838 task->sgid == fc->group_id &&
839 task->gid == fc->group_id)
840 return 1;
842 return 0;
845 static int fuse_access(struct inode *inode, int mask)
847 struct fuse_conn *fc = get_fuse_conn(inode);
848 struct fuse_req *req;
849 struct fuse_access_in inarg;
850 int err;
852 if (fc->no_access)
853 return 0;
855 req = fuse_get_req(fc);
856 if (IS_ERR(req))
857 return PTR_ERR(req);
859 memset(&inarg, 0, sizeof(inarg));
860 inarg.mask = mask;
861 req->in.h.opcode = FUSE_ACCESS;
862 req->in.h.nodeid = get_node_id(inode);
863 req->in.numargs = 1;
864 req->in.args[0].size = sizeof(inarg);
865 req->in.args[0].value = &inarg;
866 request_send(fc, req);
867 err = req->out.h.error;
868 fuse_put_request(fc, req);
869 if (err == -ENOSYS) {
870 fc->no_access = 1;
871 err = 0;
873 return err;
877 * Check permission. The two basic access models of FUSE are:
879 * 1) Local access checking ('default_permissions' mount option) based
880 * on file mode. This is the plain old disk filesystem permission
881 * modell.
883 * 2) "Remote" access checking, where server is responsible for
884 * checking permission in each inode operation. An exception to this
885 * is if ->permission() was invoked from sys_access() in which case an
886 * access request is sent. Execute permission is still checked
887 * locally based on file mode.
889 static int fuse_permission(struct inode *inode, int mask, struct nameidata *nd)
891 struct fuse_conn *fc = get_fuse_conn(inode);
892 bool refreshed = false;
893 int err = 0;
895 if (!fuse_allow_task(fc, current))
896 return -EACCES;
899 * If attributes are needed, refresh them before proceeding
901 if ((fc->flags & FUSE_DEFAULT_PERMISSIONS) ||
902 ((mask & MAY_EXEC) && S_ISREG(inode->i_mode))) {
903 err = fuse_update_attributes(inode, NULL, NULL, &refreshed);
904 if (err)
905 return err;
908 if (fc->flags & FUSE_DEFAULT_PERMISSIONS) {
909 err = generic_permission(inode, mask, NULL);
911 /* If permission is denied, try to refresh file
912 attributes. This is also needed, because the root
913 node will at first have no permissions */
914 if (err == -EACCES && !refreshed) {
915 err = fuse_do_getattr(inode, NULL, NULL);
916 if (!err)
917 err = generic_permission(inode, mask, NULL);
920 /* Note: the opposite of the above test does not
921 exist. So if permissions are revoked this won't be
922 noticed immediately, only after the attribute
923 timeout has expired */
924 } else if (nd && (nd->flags & (LOOKUP_ACCESS | LOOKUP_CHDIR))) {
925 err = fuse_access(inode, mask);
926 } else if ((mask & MAY_EXEC) && S_ISREG(inode->i_mode)) {
927 if (!(inode->i_mode & S_IXUGO)) {
928 if (refreshed)
929 return -EACCES;
931 err = fuse_do_getattr(inode, NULL, NULL);
932 if (!err && !(inode->i_mode & S_IXUGO))
933 return -EACCES;
936 return err;
939 static int parse_dirfile(char *buf, size_t nbytes, struct file *file,
940 void *dstbuf, filldir_t filldir)
942 while (nbytes >= FUSE_NAME_OFFSET) {
943 struct fuse_dirent *dirent = (struct fuse_dirent *) buf;
944 size_t reclen = FUSE_DIRENT_SIZE(dirent);
945 int over;
946 if (!dirent->namelen || dirent->namelen > FUSE_NAME_MAX)
947 return -EIO;
948 if (reclen > nbytes)
949 break;
951 over = filldir(dstbuf, dirent->name, dirent->namelen,
952 file->f_pos, dirent->ino, dirent->type);
953 if (over)
954 break;
956 buf += reclen;
957 nbytes -= reclen;
958 file->f_pos = dirent->off;
961 return 0;
964 static int fuse_readdir(struct file *file, void *dstbuf, filldir_t filldir)
966 int err;
967 size_t nbytes;
968 struct page *page;
969 struct inode *inode = file->f_path.dentry->d_inode;
970 struct fuse_conn *fc = get_fuse_conn(inode);
971 struct fuse_req *req;
973 if (is_bad_inode(inode))
974 return -EIO;
976 req = fuse_get_req(fc);
977 if (IS_ERR(req))
978 return PTR_ERR(req);
980 page = alloc_page(GFP_KERNEL);
981 if (!page) {
982 fuse_put_request(fc, req);
983 return -ENOMEM;
985 req->num_pages = 1;
986 req->pages[0] = page;
987 fuse_read_fill(req, file, inode, file->f_pos, PAGE_SIZE, FUSE_READDIR);
988 request_send(fc, req);
989 nbytes = req->out.args[0].size;
990 err = req->out.h.error;
991 fuse_put_request(fc, req);
992 if (!err)
993 err = parse_dirfile(page_address(page), nbytes, file, dstbuf,
994 filldir);
996 __free_page(page);
997 fuse_invalidate_attr(inode); /* atime changed */
998 return err;
1001 static char *read_link(struct dentry *dentry)
1003 struct inode *inode = dentry->d_inode;
1004 struct fuse_conn *fc = get_fuse_conn(inode);
1005 struct fuse_req *req = fuse_get_req(fc);
1006 char *link;
1008 if (IS_ERR(req))
1009 return ERR_CAST(req);
1011 link = (char *) __get_free_page(GFP_KERNEL);
1012 if (!link) {
1013 link = ERR_PTR(-ENOMEM);
1014 goto out;
1016 req->in.h.opcode = FUSE_READLINK;
1017 req->in.h.nodeid = get_node_id(inode);
1018 req->out.argvar = 1;
1019 req->out.numargs = 1;
1020 req->out.args[0].size = PAGE_SIZE - 1;
1021 req->out.args[0].value = link;
1022 request_send(fc, req);
1023 if (req->out.h.error) {
1024 free_page((unsigned long) link);
1025 link = ERR_PTR(req->out.h.error);
1026 } else
1027 link[req->out.args[0].size] = '\0';
1028 out:
1029 fuse_put_request(fc, req);
1030 fuse_invalidate_attr(inode); /* atime changed */
1031 return link;
1034 static void free_link(char *link)
1036 if (!IS_ERR(link))
1037 free_page((unsigned long) link);
1040 static void *fuse_follow_link(struct dentry *dentry, struct nameidata *nd)
1042 nd_set_link(nd, read_link(dentry));
1043 return NULL;
1046 static void fuse_put_link(struct dentry *dentry, struct nameidata *nd, void *c)
1048 free_link(nd_get_link(nd));
1051 static int fuse_dir_open(struct inode *inode, struct file *file)
1053 return fuse_open_common(inode, file, 1);
1056 static int fuse_dir_release(struct inode *inode, struct file *file)
1058 return fuse_release_common(inode, file, 1);
1061 static int fuse_dir_fsync(struct file *file, struct dentry *de, int datasync)
1063 /* nfsd can call this with no file */
1064 return file ? fuse_fsync_common(file, de, datasync, 1) : 0;
1067 static bool update_mtime(unsigned ivalid)
1069 /* Always update if mtime is explicitly set */
1070 if (ivalid & ATTR_MTIME_SET)
1071 return true;
1073 /* If it's an open(O_TRUNC) or an ftruncate(), don't update */
1074 if ((ivalid & ATTR_SIZE) && (ivalid & (ATTR_OPEN | ATTR_FILE)))
1075 return false;
1077 /* In all other cases update */
1078 return true;
1081 static void iattr_to_fattr(struct iattr *iattr, struct fuse_setattr_in *arg)
1083 unsigned ivalid = iattr->ia_valid;
1085 if (ivalid & ATTR_MODE)
1086 arg->valid |= FATTR_MODE, arg->mode = iattr->ia_mode;
1087 if (ivalid & ATTR_UID)
1088 arg->valid |= FATTR_UID, arg->uid = iattr->ia_uid;
1089 if (ivalid & ATTR_GID)
1090 arg->valid |= FATTR_GID, arg->gid = iattr->ia_gid;
1091 if (ivalid & ATTR_SIZE)
1092 arg->valid |= FATTR_SIZE, arg->size = iattr->ia_size;
1093 if (ivalid & ATTR_ATIME) {
1094 arg->valid |= FATTR_ATIME;
1095 arg->atime = iattr->ia_atime.tv_sec;
1096 arg->atimensec = iattr->ia_atime.tv_nsec;
1097 if (!(ivalid & ATTR_ATIME_SET))
1098 arg->valid |= FATTR_ATIME_NOW;
1100 if ((ivalid & ATTR_MTIME) && update_mtime(ivalid)) {
1101 arg->valid |= FATTR_MTIME;
1102 arg->mtime = iattr->ia_mtime.tv_sec;
1103 arg->mtimensec = iattr->ia_mtime.tv_nsec;
1104 if (!(ivalid & ATTR_MTIME_SET))
1105 arg->valid |= FATTR_MTIME_NOW;
1110 * Set attributes, and at the same time refresh them.
1112 * Truncation is slightly complicated, because the 'truncate' request
1113 * may fail, in which case we don't want to touch the mapping.
1114 * vmtruncate() doesn't allow for this case, so do the rlimit checking
1115 * and the actual truncation by hand.
1117 static int fuse_do_setattr(struct dentry *entry, struct iattr *attr,
1118 struct file *file)
1120 struct inode *inode = entry->d_inode;
1121 struct fuse_conn *fc = get_fuse_conn(inode);
1122 struct fuse_req *req;
1123 struct fuse_setattr_in inarg;
1124 struct fuse_attr_out outarg;
1125 int err;
1127 if (!fuse_allow_task(fc, current))
1128 return -EACCES;
1130 if (fc->flags & FUSE_DEFAULT_PERMISSIONS) {
1131 err = inode_change_ok(inode, attr);
1132 if (err)
1133 return err;
1136 if ((attr->ia_valid & ATTR_OPEN) && fc->atomic_o_trunc)
1137 return 0;
1139 if (attr->ia_valid & ATTR_SIZE) {
1140 unsigned long limit;
1141 if (IS_SWAPFILE(inode))
1142 return -ETXTBSY;
1143 limit = current->signal->rlim[RLIMIT_FSIZE].rlim_cur;
1144 if (limit != RLIM_INFINITY && attr->ia_size > (loff_t) limit) {
1145 send_sig(SIGXFSZ, current, 0);
1146 return -EFBIG;
1150 req = fuse_get_req(fc);
1151 if (IS_ERR(req))
1152 return PTR_ERR(req);
1154 memset(&inarg, 0, sizeof(inarg));
1155 memset(&outarg, 0, sizeof(outarg));
1156 iattr_to_fattr(attr, &inarg);
1157 if (file) {
1158 struct fuse_file *ff = file->private_data;
1159 inarg.valid |= FATTR_FH;
1160 inarg.fh = ff->fh;
1162 if (attr->ia_valid & ATTR_SIZE) {
1163 /* For mandatory locking in truncate */
1164 inarg.valid |= FATTR_LOCKOWNER;
1165 inarg.lock_owner = fuse_lock_owner_id(fc, current->files);
1167 req->in.h.opcode = FUSE_SETATTR;
1168 req->in.h.nodeid = get_node_id(inode);
1169 req->in.numargs = 1;
1170 req->in.args[0].size = sizeof(inarg);
1171 req->in.args[0].value = &inarg;
1172 req->out.numargs = 1;
1173 if (fc->minor < 9)
1174 req->out.args[0].size = FUSE_COMPAT_ATTR_OUT_SIZE;
1175 else
1176 req->out.args[0].size = sizeof(outarg);
1177 req->out.args[0].value = &outarg;
1178 request_send(fc, req);
1179 err = req->out.h.error;
1180 fuse_put_request(fc, req);
1181 if (err) {
1182 if (err == -EINTR)
1183 fuse_invalidate_attr(inode);
1184 return err;
1187 if ((inode->i_mode ^ outarg.attr.mode) & S_IFMT) {
1188 make_bad_inode(inode);
1189 return -EIO;
1192 fuse_change_attributes(inode, &outarg.attr, attr_timeout(&outarg), 0);
1193 return 0;
1196 static int fuse_setattr(struct dentry *entry, struct iattr *attr)
1198 if (attr->ia_valid & ATTR_FILE)
1199 return fuse_do_setattr(entry, attr, attr->ia_file);
1200 else
1201 return fuse_do_setattr(entry, attr, NULL);
1204 static int fuse_getattr(struct vfsmount *mnt, struct dentry *entry,
1205 struct kstat *stat)
1207 struct inode *inode = entry->d_inode;
1208 struct fuse_conn *fc = get_fuse_conn(inode);
1210 if (!fuse_allow_task(fc, current))
1211 return -EACCES;
1213 return fuse_update_attributes(inode, stat, NULL, NULL);
1216 static int fuse_setxattr(struct dentry *entry, const char *name,
1217 const void *value, size_t size, int flags)
1219 struct inode *inode = entry->d_inode;
1220 struct fuse_conn *fc = get_fuse_conn(inode);
1221 struct fuse_req *req;
1222 struct fuse_setxattr_in inarg;
1223 int err;
1225 if (fc->no_setxattr)
1226 return -EOPNOTSUPP;
1228 req = fuse_get_req(fc);
1229 if (IS_ERR(req))
1230 return PTR_ERR(req);
1232 memset(&inarg, 0, sizeof(inarg));
1233 inarg.size = size;
1234 inarg.flags = flags;
1235 req->in.h.opcode = FUSE_SETXATTR;
1236 req->in.h.nodeid = get_node_id(inode);
1237 req->in.numargs = 3;
1238 req->in.args[0].size = sizeof(inarg);
1239 req->in.args[0].value = &inarg;
1240 req->in.args[1].size = strlen(name) + 1;
1241 req->in.args[1].value = name;
1242 req->in.args[2].size = size;
1243 req->in.args[2].value = value;
1244 request_send(fc, req);
1245 err = req->out.h.error;
1246 fuse_put_request(fc, req);
1247 if (err == -ENOSYS) {
1248 fc->no_setxattr = 1;
1249 err = -EOPNOTSUPP;
1251 return err;
1254 static ssize_t fuse_getxattr(struct dentry *entry, const char *name,
1255 void *value, size_t size)
1257 struct inode *inode = entry->d_inode;
1258 struct fuse_conn *fc = get_fuse_conn(inode);
1259 struct fuse_req *req;
1260 struct fuse_getxattr_in inarg;
1261 struct fuse_getxattr_out outarg;
1262 ssize_t ret;
1264 if (fc->no_getxattr)
1265 return -EOPNOTSUPP;
1267 req = fuse_get_req(fc);
1268 if (IS_ERR(req))
1269 return PTR_ERR(req);
1271 memset(&inarg, 0, sizeof(inarg));
1272 inarg.size = size;
1273 req->in.h.opcode = FUSE_GETXATTR;
1274 req->in.h.nodeid = get_node_id(inode);
1275 req->in.numargs = 2;
1276 req->in.args[0].size = sizeof(inarg);
1277 req->in.args[0].value = &inarg;
1278 req->in.args[1].size = strlen(name) + 1;
1279 req->in.args[1].value = name;
1280 /* This is really two different operations rolled into one */
1281 req->out.numargs = 1;
1282 if (size) {
1283 req->out.argvar = 1;
1284 req->out.args[0].size = size;
1285 req->out.args[0].value = value;
1286 } else {
1287 req->out.args[0].size = sizeof(outarg);
1288 req->out.args[0].value = &outarg;
1290 request_send(fc, req);
1291 ret = req->out.h.error;
1292 if (!ret)
1293 ret = size ? req->out.args[0].size : outarg.size;
1294 else {
1295 if (ret == -ENOSYS) {
1296 fc->no_getxattr = 1;
1297 ret = -EOPNOTSUPP;
1300 fuse_put_request(fc, req);
1301 return ret;
1304 static ssize_t fuse_listxattr(struct dentry *entry, char *list, size_t size)
1306 struct inode *inode = entry->d_inode;
1307 struct fuse_conn *fc = get_fuse_conn(inode);
1308 struct fuse_req *req;
1309 struct fuse_getxattr_in inarg;
1310 struct fuse_getxattr_out outarg;
1311 ssize_t ret;
1313 if (!fuse_allow_task(fc, current))
1314 return -EACCES;
1316 if (fc->no_listxattr)
1317 return -EOPNOTSUPP;
1319 req = fuse_get_req(fc);
1320 if (IS_ERR(req))
1321 return PTR_ERR(req);
1323 memset(&inarg, 0, sizeof(inarg));
1324 inarg.size = size;
1325 req->in.h.opcode = FUSE_LISTXATTR;
1326 req->in.h.nodeid = get_node_id(inode);
1327 req->in.numargs = 1;
1328 req->in.args[0].size = sizeof(inarg);
1329 req->in.args[0].value = &inarg;
1330 /* This is really two different operations rolled into one */
1331 req->out.numargs = 1;
1332 if (size) {
1333 req->out.argvar = 1;
1334 req->out.args[0].size = size;
1335 req->out.args[0].value = list;
1336 } else {
1337 req->out.args[0].size = sizeof(outarg);
1338 req->out.args[0].value = &outarg;
1340 request_send(fc, req);
1341 ret = req->out.h.error;
1342 if (!ret)
1343 ret = size ? req->out.args[0].size : outarg.size;
1344 else {
1345 if (ret == -ENOSYS) {
1346 fc->no_listxattr = 1;
1347 ret = -EOPNOTSUPP;
1350 fuse_put_request(fc, req);
1351 return ret;
1354 static int fuse_removexattr(struct dentry *entry, const char *name)
1356 struct inode *inode = entry->d_inode;
1357 struct fuse_conn *fc = get_fuse_conn(inode);
1358 struct fuse_req *req;
1359 int err;
1361 if (fc->no_removexattr)
1362 return -EOPNOTSUPP;
1364 req = fuse_get_req(fc);
1365 if (IS_ERR(req))
1366 return PTR_ERR(req);
1368 req->in.h.opcode = FUSE_REMOVEXATTR;
1369 req->in.h.nodeid = get_node_id(inode);
1370 req->in.numargs = 1;
1371 req->in.args[0].size = strlen(name) + 1;
1372 req->in.args[0].value = name;
1373 request_send(fc, req);
1374 err = req->out.h.error;
1375 fuse_put_request(fc, req);
1376 if (err == -ENOSYS) {
1377 fc->no_removexattr = 1;
1378 err = -EOPNOTSUPP;
1380 return err;
1383 static const struct inode_operations fuse_dir_inode_operations = {
1384 .lookup = fuse_lookup,
1385 .mkdir = fuse_mkdir,
1386 .symlink = fuse_symlink,
1387 .unlink = fuse_unlink,
1388 .rmdir = fuse_rmdir,
1389 .rename = fuse_rename,
1390 .link = fuse_link,
1391 .setattr = fuse_setattr,
1392 .create = fuse_create,
1393 .mknod = fuse_mknod,
1394 .permission = fuse_permission,
1395 .getattr = fuse_getattr,
1396 .setxattr = fuse_setxattr,
1397 .getxattr = fuse_getxattr,
1398 .listxattr = fuse_listxattr,
1399 .removexattr = fuse_removexattr,
1402 static const struct file_operations fuse_dir_operations = {
1403 .llseek = generic_file_llseek,
1404 .read = generic_read_dir,
1405 .readdir = fuse_readdir,
1406 .open = fuse_dir_open,
1407 .release = fuse_dir_release,
1408 .fsync = fuse_dir_fsync,
1411 static const struct inode_operations fuse_common_inode_operations = {
1412 .setattr = fuse_setattr,
1413 .permission = fuse_permission,
1414 .getattr = fuse_getattr,
1415 .setxattr = fuse_setxattr,
1416 .getxattr = fuse_getxattr,
1417 .listxattr = fuse_listxattr,
1418 .removexattr = fuse_removexattr,
1421 static const struct inode_operations fuse_symlink_inode_operations = {
1422 .setattr = fuse_setattr,
1423 .follow_link = fuse_follow_link,
1424 .put_link = fuse_put_link,
1425 .readlink = generic_readlink,
1426 .getattr = fuse_getattr,
1427 .setxattr = fuse_setxattr,
1428 .getxattr = fuse_getxattr,
1429 .listxattr = fuse_listxattr,
1430 .removexattr = fuse_removexattr,
1433 void fuse_init_common(struct inode *inode)
1435 inode->i_op = &fuse_common_inode_operations;
1438 void fuse_init_dir(struct inode *inode)
1440 inode->i_op = &fuse_dir_inode_operations;
1441 inode->i_fop = &fuse_dir_operations;
1444 void fuse_init_symlink(struct inode *inode)
1446 inode->i_op = &fuse_symlink_inode_operations;