md: Fix unfortunate interaction with evms
[linux-2.6/mini2440.git] / fs / fuse / dir.c
blobd0dcaef04e467de958bdcada2baf37b02f6da490
1 /*
2 FUSE: Filesystem in Userspace
3 Copyright (C) 2001-2008 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 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_conn *fc, struct fuse_req *req,
116 u64 nodeid, struct qstr *name,
117 struct fuse_entry_out *outarg)
119 memset(outarg, 0, sizeof(struct fuse_entry_out));
120 req->in.h.opcode = FUSE_LOOKUP;
121 req->in.h.nodeid = nodeid;
122 req->in.numargs = 1;
123 req->in.args[0].size = name->len + 1;
124 req->in.args[0].value = name->name;
125 req->out.numargs = 1;
126 if (fc->minor < 9)
127 req->out.args[0].size = FUSE_COMPAT_ENTRY_OUT_SIZE;
128 else
129 req->out.args[0].size = sizeof(struct fuse_entry_out);
130 req->out.args[0].value = outarg;
133 u64 fuse_get_attr_version(struct fuse_conn *fc)
135 u64 curr_version;
138 * The spin lock isn't actually needed on 64bit archs, but we
139 * don't yet care too much about such optimizations.
141 spin_lock(&fc->lock);
142 curr_version = fc->attr_version;
143 spin_unlock(&fc->lock);
145 return curr_version;
149 * Check whether the dentry is still valid
151 * If the entry validity timeout has expired and the dentry is
152 * positive, try to redo the lookup. If the lookup results in a
153 * different inode, then let the VFS invalidate the dentry and redo
154 * the lookup once more. If the lookup results in the same inode,
155 * then refresh the attributes, timeouts and mark the dentry valid.
157 static int fuse_dentry_revalidate(struct dentry *entry, struct nameidata *nd)
159 struct inode *inode = entry->d_inode;
161 if (inode && is_bad_inode(inode))
162 return 0;
163 else if (fuse_dentry_time(entry) < get_jiffies_64()) {
164 int err;
165 struct fuse_entry_out outarg;
166 struct fuse_conn *fc;
167 struct fuse_req *req;
168 struct fuse_req *forget_req;
169 struct dentry *parent;
170 u64 attr_version;
172 /* For negative dentries, always do a fresh lookup */
173 if (!inode)
174 return 0;
176 fc = get_fuse_conn(inode);
177 req = fuse_get_req(fc);
178 if (IS_ERR(req))
179 return 0;
181 forget_req = fuse_get_req(fc);
182 if (IS_ERR(forget_req)) {
183 fuse_put_request(fc, req);
184 return 0;
187 attr_version = fuse_get_attr_version(fc);
189 parent = dget_parent(entry);
190 fuse_lookup_init(fc, req, get_node_id(parent->d_inode),
191 &entry->d_name, &outarg);
192 fuse_request_send(fc, req);
193 dput(parent);
194 err = req->out.h.error;
195 fuse_put_request(fc, req);
196 /* Zero nodeid is same as -ENOENT */
197 if (!err && !outarg.nodeid)
198 err = -ENOENT;
199 if (!err) {
200 struct fuse_inode *fi = get_fuse_inode(inode);
201 if (outarg.nodeid != get_node_id(inode)) {
202 fuse_send_forget(fc, forget_req,
203 outarg.nodeid, 1);
204 return 0;
206 spin_lock(&fc->lock);
207 fi->nlookup++;
208 spin_unlock(&fc->lock);
210 fuse_put_request(fc, forget_req);
211 if (err || (outarg.attr.mode ^ inode->i_mode) & S_IFMT)
212 return 0;
214 fuse_change_attributes(inode, &outarg.attr,
215 entry_attr_timeout(&outarg),
216 attr_version);
217 fuse_change_entry_timeout(entry, &outarg);
219 return 1;
222 static int invalid_nodeid(u64 nodeid)
224 return !nodeid || nodeid == FUSE_ROOT_ID;
227 const struct dentry_operations fuse_dentry_operations = {
228 .d_revalidate = fuse_dentry_revalidate,
231 int fuse_valid_type(int m)
233 return S_ISREG(m) || S_ISDIR(m) || S_ISLNK(m) || S_ISCHR(m) ||
234 S_ISBLK(m) || S_ISFIFO(m) || S_ISSOCK(m);
238 * Add a directory inode to a dentry, ensuring that no other dentry
239 * refers to this inode. Called with fc->inst_mutex.
241 static struct dentry *fuse_d_add_directory(struct dentry *entry,
242 struct inode *inode)
244 struct dentry *alias = d_find_alias(inode);
245 if (alias && !(alias->d_flags & DCACHE_DISCONNECTED)) {
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 ERR_PTR(-EBUSY);
251 } else {
252 dput(alias);
254 return d_splice_alias(inode, entry);
257 int fuse_lookup_name(struct super_block *sb, u64 nodeid, struct qstr *name,
258 struct fuse_entry_out *outarg, struct inode **inode)
260 struct fuse_conn *fc = get_fuse_conn_super(sb);
261 struct fuse_req *req;
262 struct fuse_req *forget_req;
263 u64 attr_version;
264 int err;
266 *inode = NULL;
267 err = -ENAMETOOLONG;
268 if (name->len > FUSE_NAME_MAX)
269 goto out;
271 req = fuse_get_req(fc);
272 err = PTR_ERR(req);
273 if (IS_ERR(req))
274 goto out;
276 forget_req = fuse_get_req(fc);
277 err = PTR_ERR(forget_req);
278 if (IS_ERR(forget_req)) {
279 fuse_put_request(fc, req);
280 goto out;
283 attr_version = fuse_get_attr_version(fc);
285 fuse_lookup_init(fc, req, nodeid, name, outarg);
286 fuse_request_send(fc, req);
287 err = req->out.h.error;
288 fuse_put_request(fc, req);
289 /* Zero nodeid is same as -ENOENT, but with valid timeout */
290 if (err || !outarg->nodeid)
291 goto out_put_forget;
293 err = -EIO;
294 if (!outarg->nodeid)
295 goto out_put_forget;
296 if (!fuse_valid_type(outarg->attr.mode))
297 goto out_put_forget;
299 *inode = fuse_iget(sb, outarg->nodeid, outarg->generation,
300 &outarg->attr, entry_attr_timeout(outarg),
301 attr_version);
302 err = -ENOMEM;
303 if (!*inode) {
304 fuse_send_forget(fc, forget_req, outarg->nodeid, 1);
305 goto out;
307 err = 0;
309 out_put_forget:
310 fuse_put_request(fc, forget_req);
311 out:
312 return err;
315 static struct dentry *fuse_lookup(struct inode *dir, struct dentry *entry,
316 struct nameidata *nd)
318 int err;
319 struct fuse_entry_out outarg;
320 struct inode *inode;
321 struct dentry *newent;
322 struct fuse_conn *fc = get_fuse_conn(dir);
323 bool outarg_valid = true;
325 err = fuse_lookup_name(dir->i_sb, get_node_id(dir), &entry->d_name,
326 &outarg, &inode);
327 if (err == -ENOENT) {
328 outarg_valid = false;
329 err = 0;
331 if (err)
332 goto out_err;
334 err = -EIO;
335 if (inode && get_node_id(inode) == FUSE_ROOT_ID)
336 goto out_iput;
338 if (inode && S_ISDIR(inode->i_mode)) {
339 mutex_lock(&fc->inst_mutex);
340 newent = fuse_d_add_directory(entry, inode);
341 mutex_unlock(&fc->inst_mutex);
342 err = PTR_ERR(newent);
343 if (IS_ERR(newent))
344 goto out_iput;
345 } else {
346 newent = d_splice_alias(inode, entry);
349 entry = newent ? newent : entry;
350 entry->d_op = &fuse_dentry_operations;
351 if (outarg_valid)
352 fuse_change_entry_timeout(entry, &outarg);
353 else
354 fuse_invalidate_entry_cache(entry);
356 return newent;
358 out_iput:
359 iput(inode);
360 out_err:
361 return ERR_PTR(err);
365 * Atomic create+open operation
367 * If the filesystem doesn't support this, then fall back to separate
368 * 'mknod' + 'open' requests.
370 static int fuse_create_open(struct inode *dir, struct dentry *entry, int mode,
371 struct nameidata *nd)
373 int err;
374 struct inode *inode;
375 struct fuse_conn *fc = get_fuse_conn(dir);
376 struct fuse_req *req;
377 struct fuse_req *forget_req;
378 struct fuse_create_in inarg;
379 struct fuse_open_out outopen;
380 struct fuse_entry_out outentry;
381 struct fuse_file *ff;
382 struct file *file;
383 int flags = nd->intent.open.flags - 1;
385 if (fc->no_create)
386 return -ENOSYS;
388 if (flags & O_DIRECT)
389 return -EINVAL;
391 forget_req = fuse_get_req(fc);
392 if (IS_ERR(forget_req))
393 return PTR_ERR(forget_req);
395 req = fuse_get_req(fc);
396 err = PTR_ERR(req);
397 if (IS_ERR(req))
398 goto out_put_forget_req;
400 err = -ENOMEM;
401 ff = fuse_file_alloc(fc);
402 if (!ff)
403 goto out_put_request;
405 if (!fc->dont_mask)
406 mode &= ~current_umask();
408 flags &= ~O_NOCTTY;
409 memset(&inarg, 0, sizeof(inarg));
410 memset(&outentry, 0, sizeof(outentry));
411 inarg.flags = flags;
412 inarg.mode = mode;
413 inarg.umask = current_umask();
414 req->in.h.opcode = FUSE_CREATE;
415 req->in.h.nodeid = get_node_id(dir);
416 req->in.numargs = 2;
417 req->in.args[0].size = fc->minor < 12 ? sizeof(struct fuse_open_in) :
418 sizeof(inarg);
419 req->in.args[0].value = &inarg;
420 req->in.args[1].size = entry->d_name.len + 1;
421 req->in.args[1].value = entry->d_name.name;
422 req->out.numargs = 2;
423 if (fc->minor < 9)
424 req->out.args[0].size = FUSE_COMPAT_ENTRY_OUT_SIZE;
425 else
426 req->out.args[0].size = sizeof(outentry);
427 req->out.args[0].value = &outentry;
428 req->out.args[1].size = sizeof(outopen);
429 req->out.args[1].value = &outopen;
430 fuse_request_send(fc, req);
431 err = req->out.h.error;
432 if (err) {
433 if (err == -ENOSYS)
434 fc->no_create = 1;
435 goto out_free_ff;
438 err = -EIO;
439 if (!S_ISREG(outentry.attr.mode) || invalid_nodeid(outentry.nodeid))
440 goto out_free_ff;
442 fuse_put_request(fc, req);
443 ff->fh = outopen.fh;
444 ff->nodeid = outentry.nodeid;
445 ff->open_flags = outopen.open_flags;
446 inode = fuse_iget(dir->i_sb, outentry.nodeid, outentry.generation,
447 &outentry.attr, entry_attr_timeout(&outentry), 0);
448 if (!inode) {
449 flags &= ~(O_CREAT | O_EXCL | O_TRUNC);
450 fuse_sync_release(ff, flags);
451 fuse_send_forget(fc, forget_req, outentry.nodeid, 1);
452 return -ENOMEM;
454 fuse_put_request(fc, forget_req);
455 d_instantiate(entry, inode);
456 fuse_change_entry_timeout(entry, &outentry);
457 fuse_invalidate_attr(dir);
458 file = lookup_instantiate_filp(nd, entry, generic_file_open);
459 if (IS_ERR(file)) {
460 fuse_sync_release(ff, flags);
461 return PTR_ERR(file);
463 file->private_data = fuse_file_get(ff);
464 fuse_finish_open(inode, file);
465 return 0;
467 out_free_ff:
468 fuse_file_free(ff);
469 out_put_request:
470 fuse_put_request(fc, req);
471 out_put_forget_req:
472 fuse_put_request(fc, forget_req);
473 return err;
477 * Code shared between mknod, mkdir, symlink and link
479 static int create_new_entry(struct fuse_conn *fc, struct fuse_req *req,
480 struct inode *dir, struct dentry *entry,
481 int mode)
483 struct fuse_entry_out outarg;
484 struct inode *inode;
485 int err;
486 struct fuse_req *forget_req;
488 forget_req = fuse_get_req(fc);
489 if (IS_ERR(forget_req)) {
490 fuse_put_request(fc, req);
491 return PTR_ERR(forget_req);
494 memset(&outarg, 0, sizeof(outarg));
495 req->in.h.nodeid = get_node_id(dir);
496 req->out.numargs = 1;
497 if (fc->minor < 9)
498 req->out.args[0].size = FUSE_COMPAT_ENTRY_OUT_SIZE;
499 else
500 req->out.args[0].size = sizeof(outarg);
501 req->out.args[0].value = &outarg;
502 fuse_request_send(fc, req);
503 err = req->out.h.error;
504 fuse_put_request(fc, req);
505 if (err)
506 goto out_put_forget_req;
508 err = -EIO;
509 if (invalid_nodeid(outarg.nodeid))
510 goto out_put_forget_req;
512 if ((outarg.attr.mode ^ mode) & S_IFMT)
513 goto out_put_forget_req;
515 inode = fuse_iget(dir->i_sb, outarg.nodeid, outarg.generation,
516 &outarg.attr, entry_attr_timeout(&outarg), 0);
517 if (!inode) {
518 fuse_send_forget(fc, forget_req, outarg.nodeid, 1);
519 return -ENOMEM;
521 fuse_put_request(fc, forget_req);
523 if (S_ISDIR(inode->i_mode)) {
524 struct dentry *alias;
525 mutex_lock(&fc->inst_mutex);
526 alias = d_find_alias(inode);
527 if (alias) {
528 /* New directory must have moved since mkdir */
529 mutex_unlock(&fc->inst_mutex);
530 dput(alias);
531 iput(inode);
532 return -EBUSY;
534 d_instantiate(entry, inode);
535 mutex_unlock(&fc->inst_mutex);
536 } else
537 d_instantiate(entry, inode);
539 fuse_change_entry_timeout(entry, &outarg);
540 fuse_invalidate_attr(dir);
541 return 0;
543 out_put_forget_req:
544 fuse_put_request(fc, forget_req);
545 return err;
548 static int fuse_mknod(struct inode *dir, struct dentry *entry, int mode,
549 dev_t rdev)
551 struct fuse_mknod_in inarg;
552 struct fuse_conn *fc = get_fuse_conn(dir);
553 struct fuse_req *req = fuse_get_req(fc);
554 if (IS_ERR(req))
555 return PTR_ERR(req);
557 if (!fc->dont_mask)
558 mode &= ~current_umask();
560 memset(&inarg, 0, sizeof(inarg));
561 inarg.mode = mode;
562 inarg.rdev = new_encode_dev(rdev);
563 inarg.umask = current_umask();
564 req->in.h.opcode = FUSE_MKNOD;
565 req->in.numargs = 2;
566 req->in.args[0].size = fc->minor < 12 ? FUSE_COMPAT_MKNOD_IN_SIZE :
567 sizeof(inarg);
568 req->in.args[0].value = &inarg;
569 req->in.args[1].size = entry->d_name.len + 1;
570 req->in.args[1].value = entry->d_name.name;
571 return create_new_entry(fc, req, dir, entry, mode);
574 static int fuse_create(struct inode *dir, struct dentry *entry, int mode,
575 struct nameidata *nd)
577 if (nd && (nd->flags & LOOKUP_OPEN)) {
578 int err = fuse_create_open(dir, entry, mode, nd);
579 if (err != -ENOSYS)
580 return err;
581 /* Fall back on mknod */
583 return fuse_mknod(dir, entry, mode, 0);
586 static int fuse_mkdir(struct inode *dir, struct dentry *entry, int mode)
588 struct fuse_mkdir_in inarg;
589 struct fuse_conn *fc = get_fuse_conn(dir);
590 struct fuse_req *req = fuse_get_req(fc);
591 if (IS_ERR(req))
592 return PTR_ERR(req);
594 if (!fc->dont_mask)
595 mode &= ~current_umask();
597 memset(&inarg, 0, sizeof(inarg));
598 inarg.mode = mode;
599 inarg.umask = current_umask();
600 req->in.h.opcode = FUSE_MKDIR;
601 req->in.numargs = 2;
602 req->in.args[0].size = sizeof(inarg);
603 req->in.args[0].value = &inarg;
604 req->in.args[1].size = entry->d_name.len + 1;
605 req->in.args[1].value = entry->d_name.name;
606 return create_new_entry(fc, req, dir, entry, S_IFDIR);
609 static int fuse_symlink(struct inode *dir, struct dentry *entry,
610 const char *link)
612 struct fuse_conn *fc = get_fuse_conn(dir);
613 unsigned len = strlen(link) + 1;
614 struct fuse_req *req = fuse_get_req(fc);
615 if (IS_ERR(req))
616 return PTR_ERR(req);
618 req->in.h.opcode = FUSE_SYMLINK;
619 req->in.numargs = 2;
620 req->in.args[0].size = entry->d_name.len + 1;
621 req->in.args[0].value = entry->d_name.name;
622 req->in.args[1].size = len;
623 req->in.args[1].value = link;
624 return create_new_entry(fc, req, dir, entry, S_IFLNK);
627 static int fuse_unlink(struct inode *dir, struct dentry *entry)
629 int err;
630 struct fuse_conn *fc = get_fuse_conn(dir);
631 struct fuse_req *req = fuse_get_req(fc);
632 if (IS_ERR(req))
633 return PTR_ERR(req);
635 req->in.h.opcode = FUSE_UNLINK;
636 req->in.h.nodeid = get_node_id(dir);
637 req->in.numargs = 1;
638 req->in.args[0].size = entry->d_name.len + 1;
639 req->in.args[0].value = entry->d_name.name;
640 fuse_request_send(fc, req);
641 err = req->out.h.error;
642 fuse_put_request(fc, req);
643 if (!err) {
644 struct inode *inode = entry->d_inode;
647 * Set nlink to zero so the inode can be cleared, if the inode
648 * does have more links this will be discovered at the next
649 * lookup/getattr.
651 clear_nlink(inode);
652 fuse_invalidate_attr(inode);
653 fuse_invalidate_attr(dir);
654 fuse_invalidate_entry_cache(entry);
655 } else if (err == -EINTR)
656 fuse_invalidate_entry(entry);
657 return err;
660 static int fuse_rmdir(struct inode *dir, struct dentry *entry)
662 int err;
663 struct fuse_conn *fc = get_fuse_conn(dir);
664 struct fuse_req *req = fuse_get_req(fc);
665 if (IS_ERR(req))
666 return PTR_ERR(req);
668 req->in.h.opcode = FUSE_RMDIR;
669 req->in.h.nodeid = get_node_id(dir);
670 req->in.numargs = 1;
671 req->in.args[0].size = entry->d_name.len + 1;
672 req->in.args[0].value = entry->d_name.name;
673 fuse_request_send(fc, req);
674 err = req->out.h.error;
675 fuse_put_request(fc, req);
676 if (!err) {
677 clear_nlink(entry->d_inode);
678 fuse_invalidate_attr(dir);
679 fuse_invalidate_entry_cache(entry);
680 } else if (err == -EINTR)
681 fuse_invalidate_entry(entry);
682 return err;
685 static int fuse_rename(struct inode *olddir, struct dentry *oldent,
686 struct inode *newdir, struct dentry *newent)
688 int err;
689 struct fuse_rename_in inarg;
690 struct fuse_conn *fc = get_fuse_conn(olddir);
691 struct fuse_req *req = fuse_get_req(fc);
692 if (IS_ERR(req))
693 return PTR_ERR(req);
695 memset(&inarg, 0, sizeof(inarg));
696 inarg.newdir = get_node_id(newdir);
697 req->in.h.opcode = FUSE_RENAME;
698 req->in.h.nodeid = get_node_id(olddir);
699 req->in.numargs = 3;
700 req->in.args[0].size = sizeof(inarg);
701 req->in.args[0].value = &inarg;
702 req->in.args[1].size = oldent->d_name.len + 1;
703 req->in.args[1].value = oldent->d_name.name;
704 req->in.args[2].size = newent->d_name.len + 1;
705 req->in.args[2].value = newent->d_name.name;
706 fuse_request_send(fc, req);
707 err = req->out.h.error;
708 fuse_put_request(fc, req);
709 if (!err) {
710 /* ctime changes */
711 fuse_invalidate_attr(oldent->d_inode);
713 fuse_invalidate_attr(olddir);
714 if (olddir != newdir)
715 fuse_invalidate_attr(newdir);
717 /* newent will end up negative */
718 if (newent->d_inode)
719 fuse_invalidate_entry_cache(newent);
720 } else if (err == -EINTR) {
721 /* If request was interrupted, DEITY only knows if the
722 rename actually took place. If the invalidation
723 fails (e.g. some process has CWD under the renamed
724 directory), then there can be inconsistency between
725 the dcache and the real filesystem. Tough luck. */
726 fuse_invalidate_entry(oldent);
727 if (newent->d_inode)
728 fuse_invalidate_entry(newent);
731 return err;
734 static int fuse_link(struct dentry *entry, struct inode *newdir,
735 struct dentry *newent)
737 int err;
738 struct fuse_link_in inarg;
739 struct inode *inode = entry->d_inode;
740 struct fuse_conn *fc = get_fuse_conn(inode);
741 struct fuse_req *req = fuse_get_req(fc);
742 if (IS_ERR(req))
743 return PTR_ERR(req);
745 memset(&inarg, 0, sizeof(inarg));
746 inarg.oldnodeid = get_node_id(inode);
747 req->in.h.opcode = FUSE_LINK;
748 req->in.numargs = 2;
749 req->in.args[0].size = sizeof(inarg);
750 req->in.args[0].value = &inarg;
751 req->in.args[1].size = newent->d_name.len + 1;
752 req->in.args[1].value = newent->d_name.name;
753 err = create_new_entry(fc, req, newdir, newent, inode->i_mode);
754 /* Contrary to "normal" filesystems it can happen that link
755 makes two "logical" inodes point to the same "physical"
756 inode. We invalidate the attributes of the old one, so it
757 will reflect changes in the backing inode (link count,
758 etc.)
760 if (!err || err == -EINTR)
761 fuse_invalidate_attr(inode);
762 return err;
765 static void fuse_fillattr(struct inode *inode, struct fuse_attr *attr,
766 struct kstat *stat)
768 stat->dev = inode->i_sb->s_dev;
769 stat->ino = attr->ino;
770 stat->mode = (inode->i_mode & S_IFMT) | (attr->mode & 07777);
771 stat->nlink = attr->nlink;
772 stat->uid = attr->uid;
773 stat->gid = attr->gid;
774 stat->rdev = inode->i_rdev;
775 stat->atime.tv_sec = attr->atime;
776 stat->atime.tv_nsec = attr->atimensec;
777 stat->mtime.tv_sec = attr->mtime;
778 stat->mtime.tv_nsec = attr->mtimensec;
779 stat->ctime.tv_sec = attr->ctime;
780 stat->ctime.tv_nsec = attr->ctimensec;
781 stat->size = attr->size;
782 stat->blocks = attr->blocks;
783 stat->blksize = (1 << inode->i_blkbits);
786 static int fuse_do_getattr(struct inode *inode, struct kstat *stat,
787 struct file *file)
789 int err;
790 struct fuse_getattr_in inarg;
791 struct fuse_attr_out outarg;
792 struct fuse_conn *fc = get_fuse_conn(inode);
793 struct fuse_req *req;
794 u64 attr_version;
796 req = fuse_get_req(fc);
797 if (IS_ERR(req))
798 return PTR_ERR(req);
800 attr_version = fuse_get_attr_version(fc);
802 memset(&inarg, 0, sizeof(inarg));
803 memset(&outarg, 0, sizeof(outarg));
804 /* Directories have separate file-handle space */
805 if (file && S_ISREG(inode->i_mode)) {
806 struct fuse_file *ff = file->private_data;
808 inarg.getattr_flags |= FUSE_GETATTR_FH;
809 inarg.fh = ff->fh;
811 req->in.h.opcode = FUSE_GETATTR;
812 req->in.h.nodeid = get_node_id(inode);
813 req->in.numargs = 1;
814 req->in.args[0].size = sizeof(inarg);
815 req->in.args[0].value = &inarg;
816 req->out.numargs = 1;
817 if (fc->minor < 9)
818 req->out.args[0].size = FUSE_COMPAT_ATTR_OUT_SIZE;
819 else
820 req->out.args[0].size = sizeof(outarg);
821 req->out.args[0].value = &outarg;
822 fuse_request_send(fc, req);
823 err = req->out.h.error;
824 fuse_put_request(fc, req);
825 if (!err) {
826 if ((inode->i_mode ^ outarg.attr.mode) & S_IFMT) {
827 make_bad_inode(inode);
828 err = -EIO;
829 } else {
830 fuse_change_attributes(inode, &outarg.attr,
831 attr_timeout(&outarg),
832 attr_version);
833 if (stat)
834 fuse_fillattr(inode, &outarg.attr, stat);
837 return err;
840 int fuse_update_attributes(struct inode *inode, struct kstat *stat,
841 struct file *file, bool *refreshed)
843 struct fuse_inode *fi = get_fuse_inode(inode);
844 int err;
845 bool r;
847 if (fi->i_time < get_jiffies_64()) {
848 r = true;
849 err = fuse_do_getattr(inode, stat, file);
850 } else {
851 r = false;
852 err = 0;
853 if (stat) {
854 generic_fillattr(inode, stat);
855 stat->mode = fi->orig_i_mode;
859 if (refreshed != NULL)
860 *refreshed = r;
862 return err;
865 int fuse_reverse_inval_entry(struct super_block *sb, u64 parent_nodeid,
866 struct qstr *name)
868 int err = -ENOTDIR;
869 struct inode *parent;
870 struct dentry *dir;
871 struct dentry *entry;
873 parent = ilookup5(sb, parent_nodeid, fuse_inode_eq, &parent_nodeid);
874 if (!parent)
875 return -ENOENT;
877 mutex_lock(&parent->i_mutex);
878 if (!S_ISDIR(parent->i_mode))
879 goto unlock;
881 err = -ENOENT;
882 dir = d_find_alias(parent);
883 if (!dir)
884 goto unlock;
886 entry = d_lookup(dir, name);
887 dput(dir);
888 if (!entry)
889 goto unlock;
891 fuse_invalidate_attr(parent);
892 fuse_invalidate_entry(entry);
893 dput(entry);
894 err = 0;
896 unlock:
897 mutex_unlock(&parent->i_mutex);
898 iput(parent);
899 return err;
903 * Calling into a user-controlled filesystem gives the filesystem
904 * daemon ptrace-like capabilities over the requester process. This
905 * means, that the filesystem daemon is able to record the exact
906 * filesystem operations performed, and can also control the behavior
907 * of the requester process in otherwise impossible ways. For example
908 * it can delay the operation for arbitrary length of time allowing
909 * DoS against the requester.
911 * For this reason only those processes can call into the filesystem,
912 * for which the owner of the mount has ptrace privilege. This
913 * excludes processes started by other users, suid or sgid processes.
915 int fuse_allow_task(struct fuse_conn *fc, struct task_struct *task)
917 const struct cred *cred;
918 int ret;
920 if (fc->flags & FUSE_ALLOW_OTHER)
921 return 1;
923 rcu_read_lock();
924 ret = 0;
925 cred = __task_cred(task);
926 if (cred->euid == fc->user_id &&
927 cred->suid == fc->user_id &&
928 cred->uid == fc->user_id &&
929 cred->egid == fc->group_id &&
930 cred->sgid == fc->group_id &&
931 cred->gid == fc->group_id)
932 ret = 1;
933 rcu_read_unlock();
935 return ret;
938 static int fuse_access(struct inode *inode, int mask)
940 struct fuse_conn *fc = get_fuse_conn(inode);
941 struct fuse_req *req;
942 struct fuse_access_in inarg;
943 int err;
945 if (fc->no_access)
946 return 0;
948 req = fuse_get_req(fc);
949 if (IS_ERR(req))
950 return PTR_ERR(req);
952 memset(&inarg, 0, sizeof(inarg));
953 inarg.mask = mask & (MAY_READ | MAY_WRITE | MAY_EXEC);
954 req->in.h.opcode = FUSE_ACCESS;
955 req->in.h.nodeid = get_node_id(inode);
956 req->in.numargs = 1;
957 req->in.args[0].size = sizeof(inarg);
958 req->in.args[0].value = &inarg;
959 fuse_request_send(fc, req);
960 err = req->out.h.error;
961 fuse_put_request(fc, req);
962 if (err == -ENOSYS) {
963 fc->no_access = 1;
964 err = 0;
966 return err;
970 * Check permission. The two basic access models of FUSE are:
972 * 1) Local access checking ('default_permissions' mount option) based
973 * on file mode. This is the plain old disk filesystem permission
974 * modell.
976 * 2) "Remote" access checking, where server is responsible for
977 * checking permission in each inode operation. An exception to this
978 * is if ->permission() was invoked from sys_access() in which case an
979 * access request is sent. Execute permission is still checked
980 * locally based on file mode.
982 static int fuse_permission(struct inode *inode, int mask)
984 struct fuse_conn *fc = get_fuse_conn(inode);
985 bool refreshed = false;
986 int err = 0;
988 if (!fuse_allow_task(fc, current))
989 return -EACCES;
992 * If attributes are needed, refresh them before proceeding
994 if ((fc->flags & FUSE_DEFAULT_PERMISSIONS) ||
995 ((mask & MAY_EXEC) && S_ISREG(inode->i_mode))) {
996 err = fuse_update_attributes(inode, NULL, NULL, &refreshed);
997 if (err)
998 return err;
1001 if (fc->flags & FUSE_DEFAULT_PERMISSIONS) {
1002 err = generic_permission(inode, mask, NULL);
1004 /* If permission is denied, try to refresh file
1005 attributes. This is also needed, because the root
1006 node will at first have no permissions */
1007 if (err == -EACCES && !refreshed) {
1008 err = fuse_do_getattr(inode, NULL, NULL);
1009 if (!err)
1010 err = generic_permission(inode, mask, NULL);
1013 /* Note: the opposite of the above test does not
1014 exist. So if permissions are revoked this won't be
1015 noticed immediately, only after the attribute
1016 timeout has expired */
1017 } else if (mask & MAY_ACCESS) {
1018 err = fuse_access(inode, mask);
1019 } else if ((mask & MAY_EXEC) && S_ISREG(inode->i_mode)) {
1020 if (!(inode->i_mode & S_IXUGO)) {
1021 if (refreshed)
1022 return -EACCES;
1024 err = fuse_do_getattr(inode, NULL, NULL);
1025 if (!err && !(inode->i_mode & S_IXUGO))
1026 return -EACCES;
1029 return err;
1032 static int parse_dirfile(char *buf, size_t nbytes, struct file *file,
1033 void *dstbuf, filldir_t filldir)
1035 while (nbytes >= FUSE_NAME_OFFSET) {
1036 struct fuse_dirent *dirent = (struct fuse_dirent *) buf;
1037 size_t reclen = FUSE_DIRENT_SIZE(dirent);
1038 int over;
1039 if (!dirent->namelen || dirent->namelen > FUSE_NAME_MAX)
1040 return -EIO;
1041 if (reclen > nbytes)
1042 break;
1044 over = filldir(dstbuf, dirent->name, dirent->namelen,
1045 file->f_pos, dirent->ino, dirent->type);
1046 if (over)
1047 break;
1049 buf += reclen;
1050 nbytes -= reclen;
1051 file->f_pos = dirent->off;
1054 return 0;
1057 static int fuse_readdir(struct file *file, void *dstbuf, filldir_t filldir)
1059 int err;
1060 size_t nbytes;
1061 struct page *page;
1062 struct inode *inode = file->f_path.dentry->d_inode;
1063 struct fuse_conn *fc = get_fuse_conn(inode);
1064 struct fuse_req *req;
1066 if (is_bad_inode(inode))
1067 return -EIO;
1069 req = fuse_get_req(fc);
1070 if (IS_ERR(req))
1071 return PTR_ERR(req);
1073 page = alloc_page(GFP_KERNEL);
1074 if (!page) {
1075 fuse_put_request(fc, req);
1076 return -ENOMEM;
1078 req->out.argpages = 1;
1079 req->num_pages = 1;
1080 req->pages[0] = page;
1081 fuse_read_fill(req, file, file->f_pos, PAGE_SIZE, FUSE_READDIR);
1082 fuse_request_send(fc, req);
1083 nbytes = req->out.args[0].size;
1084 err = req->out.h.error;
1085 fuse_put_request(fc, req);
1086 if (!err)
1087 err = parse_dirfile(page_address(page), nbytes, file, dstbuf,
1088 filldir);
1090 __free_page(page);
1091 fuse_invalidate_attr(inode); /* atime changed */
1092 return err;
1095 static char *read_link(struct dentry *dentry)
1097 struct inode *inode = dentry->d_inode;
1098 struct fuse_conn *fc = get_fuse_conn(inode);
1099 struct fuse_req *req = fuse_get_req(fc);
1100 char *link;
1102 if (IS_ERR(req))
1103 return ERR_CAST(req);
1105 link = (char *) __get_free_page(GFP_KERNEL);
1106 if (!link) {
1107 link = ERR_PTR(-ENOMEM);
1108 goto out;
1110 req->in.h.opcode = FUSE_READLINK;
1111 req->in.h.nodeid = get_node_id(inode);
1112 req->out.argvar = 1;
1113 req->out.numargs = 1;
1114 req->out.args[0].size = PAGE_SIZE - 1;
1115 req->out.args[0].value = link;
1116 fuse_request_send(fc, req);
1117 if (req->out.h.error) {
1118 free_page((unsigned long) link);
1119 link = ERR_PTR(req->out.h.error);
1120 } else
1121 link[req->out.args[0].size] = '\0';
1122 out:
1123 fuse_put_request(fc, req);
1124 fuse_invalidate_attr(inode); /* atime changed */
1125 return link;
1128 static void free_link(char *link)
1130 if (!IS_ERR(link))
1131 free_page((unsigned long) link);
1134 static void *fuse_follow_link(struct dentry *dentry, struct nameidata *nd)
1136 nd_set_link(nd, read_link(dentry));
1137 return NULL;
1140 static void fuse_put_link(struct dentry *dentry, struct nameidata *nd, void *c)
1142 free_link(nd_get_link(nd));
1145 static int fuse_dir_open(struct inode *inode, struct file *file)
1147 return fuse_open_common(inode, file, true);
1150 static int fuse_dir_release(struct inode *inode, struct file *file)
1152 fuse_release_common(file, FUSE_RELEASEDIR);
1154 return 0;
1157 static int fuse_dir_fsync(struct file *file, struct dentry *de, int datasync)
1159 /* nfsd can call this with no file */
1160 return file ? fuse_fsync_common(file, de, datasync, 1) : 0;
1163 static bool update_mtime(unsigned ivalid)
1165 /* Always update if mtime is explicitly set */
1166 if (ivalid & ATTR_MTIME_SET)
1167 return true;
1169 /* If it's an open(O_TRUNC) or an ftruncate(), don't update */
1170 if ((ivalid & ATTR_SIZE) && (ivalid & (ATTR_OPEN | ATTR_FILE)))
1171 return false;
1173 /* In all other cases update */
1174 return true;
1177 static void iattr_to_fattr(struct iattr *iattr, struct fuse_setattr_in *arg)
1179 unsigned ivalid = iattr->ia_valid;
1181 if (ivalid & ATTR_MODE)
1182 arg->valid |= FATTR_MODE, arg->mode = iattr->ia_mode;
1183 if (ivalid & ATTR_UID)
1184 arg->valid |= FATTR_UID, arg->uid = iattr->ia_uid;
1185 if (ivalid & ATTR_GID)
1186 arg->valid |= FATTR_GID, arg->gid = iattr->ia_gid;
1187 if (ivalid & ATTR_SIZE)
1188 arg->valid |= FATTR_SIZE, arg->size = iattr->ia_size;
1189 if (ivalid & ATTR_ATIME) {
1190 arg->valid |= FATTR_ATIME;
1191 arg->atime = iattr->ia_atime.tv_sec;
1192 arg->atimensec = iattr->ia_atime.tv_nsec;
1193 if (!(ivalid & ATTR_ATIME_SET))
1194 arg->valid |= FATTR_ATIME_NOW;
1196 if ((ivalid & ATTR_MTIME) && update_mtime(ivalid)) {
1197 arg->valid |= FATTR_MTIME;
1198 arg->mtime = iattr->ia_mtime.tv_sec;
1199 arg->mtimensec = iattr->ia_mtime.tv_nsec;
1200 if (!(ivalid & ATTR_MTIME_SET))
1201 arg->valid |= FATTR_MTIME_NOW;
1206 * Prevent concurrent writepages on inode
1208 * This is done by adding a negative bias to the inode write counter
1209 * and waiting for all pending writes to finish.
1211 void fuse_set_nowrite(struct inode *inode)
1213 struct fuse_conn *fc = get_fuse_conn(inode);
1214 struct fuse_inode *fi = get_fuse_inode(inode);
1216 BUG_ON(!mutex_is_locked(&inode->i_mutex));
1218 spin_lock(&fc->lock);
1219 BUG_ON(fi->writectr < 0);
1220 fi->writectr += FUSE_NOWRITE;
1221 spin_unlock(&fc->lock);
1222 wait_event(fi->page_waitq, fi->writectr == FUSE_NOWRITE);
1226 * Allow writepages on inode
1228 * Remove the bias from the writecounter and send any queued
1229 * writepages.
1231 static void __fuse_release_nowrite(struct inode *inode)
1233 struct fuse_inode *fi = get_fuse_inode(inode);
1235 BUG_ON(fi->writectr != FUSE_NOWRITE);
1236 fi->writectr = 0;
1237 fuse_flush_writepages(inode);
1240 void fuse_release_nowrite(struct inode *inode)
1242 struct fuse_conn *fc = get_fuse_conn(inode);
1244 spin_lock(&fc->lock);
1245 __fuse_release_nowrite(inode);
1246 spin_unlock(&fc->lock);
1250 * Set attributes, and at the same time refresh them.
1252 * Truncation is slightly complicated, because the 'truncate' request
1253 * may fail, in which case we don't want to touch the mapping.
1254 * vmtruncate() doesn't allow for this case, so do the rlimit checking
1255 * and the actual truncation by hand.
1257 static int fuse_do_setattr(struct dentry *entry, struct iattr *attr,
1258 struct file *file)
1260 struct inode *inode = entry->d_inode;
1261 struct fuse_conn *fc = get_fuse_conn(inode);
1262 struct fuse_req *req;
1263 struct fuse_setattr_in inarg;
1264 struct fuse_attr_out outarg;
1265 bool is_truncate = false;
1266 loff_t oldsize;
1267 int err;
1269 if (!fuse_allow_task(fc, current))
1270 return -EACCES;
1272 if (fc->flags & FUSE_DEFAULT_PERMISSIONS) {
1273 err = inode_change_ok(inode, attr);
1274 if (err)
1275 return err;
1278 if ((attr->ia_valid & ATTR_OPEN) && fc->atomic_o_trunc)
1279 return 0;
1281 if (attr->ia_valid & ATTR_SIZE) {
1282 unsigned long limit;
1283 if (IS_SWAPFILE(inode))
1284 return -ETXTBSY;
1285 limit = current->signal->rlim[RLIMIT_FSIZE].rlim_cur;
1286 if (limit != RLIM_INFINITY && attr->ia_size > (loff_t) limit) {
1287 send_sig(SIGXFSZ, current, 0);
1288 return -EFBIG;
1290 is_truncate = true;
1293 req = fuse_get_req(fc);
1294 if (IS_ERR(req))
1295 return PTR_ERR(req);
1297 if (is_truncate)
1298 fuse_set_nowrite(inode);
1300 memset(&inarg, 0, sizeof(inarg));
1301 memset(&outarg, 0, sizeof(outarg));
1302 iattr_to_fattr(attr, &inarg);
1303 if (file) {
1304 struct fuse_file *ff = file->private_data;
1305 inarg.valid |= FATTR_FH;
1306 inarg.fh = ff->fh;
1308 if (attr->ia_valid & ATTR_SIZE) {
1309 /* For mandatory locking in truncate */
1310 inarg.valid |= FATTR_LOCKOWNER;
1311 inarg.lock_owner = fuse_lock_owner_id(fc, current->files);
1313 req->in.h.opcode = FUSE_SETATTR;
1314 req->in.h.nodeid = get_node_id(inode);
1315 req->in.numargs = 1;
1316 req->in.args[0].size = sizeof(inarg);
1317 req->in.args[0].value = &inarg;
1318 req->out.numargs = 1;
1319 if (fc->minor < 9)
1320 req->out.args[0].size = FUSE_COMPAT_ATTR_OUT_SIZE;
1321 else
1322 req->out.args[0].size = sizeof(outarg);
1323 req->out.args[0].value = &outarg;
1324 fuse_request_send(fc, req);
1325 err = req->out.h.error;
1326 fuse_put_request(fc, req);
1327 if (err) {
1328 if (err == -EINTR)
1329 fuse_invalidate_attr(inode);
1330 goto error;
1333 if ((inode->i_mode ^ outarg.attr.mode) & S_IFMT) {
1334 make_bad_inode(inode);
1335 err = -EIO;
1336 goto error;
1339 spin_lock(&fc->lock);
1340 fuse_change_attributes_common(inode, &outarg.attr,
1341 attr_timeout(&outarg));
1342 oldsize = inode->i_size;
1343 i_size_write(inode, outarg.attr.size);
1345 if (is_truncate) {
1346 /* NOTE: this may release/reacquire fc->lock */
1347 __fuse_release_nowrite(inode);
1349 spin_unlock(&fc->lock);
1352 * Only call invalidate_inode_pages2() after removing
1353 * FUSE_NOWRITE, otherwise fuse_launder_page() would deadlock.
1355 if (S_ISREG(inode->i_mode) && oldsize != outarg.attr.size) {
1356 if (outarg.attr.size < oldsize)
1357 fuse_truncate(inode->i_mapping, outarg.attr.size);
1358 invalidate_inode_pages2(inode->i_mapping);
1361 return 0;
1363 error:
1364 if (is_truncate)
1365 fuse_release_nowrite(inode);
1367 return err;
1370 static int fuse_setattr(struct dentry *entry, struct iattr *attr)
1372 if (attr->ia_valid & ATTR_FILE)
1373 return fuse_do_setattr(entry, attr, attr->ia_file);
1374 else
1375 return fuse_do_setattr(entry, attr, NULL);
1378 static int fuse_getattr(struct vfsmount *mnt, struct dentry *entry,
1379 struct kstat *stat)
1381 struct inode *inode = entry->d_inode;
1382 struct fuse_conn *fc = get_fuse_conn(inode);
1384 if (!fuse_allow_task(fc, current))
1385 return -EACCES;
1387 return fuse_update_attributes(inode, stat, NULL, NULL);
1390 static int fuse_setxattr(struct dentry *entry, const char *name,
1391 const void *value, size_t size, int flags)
1393 struct inode *inode = entry->d_inode;
1394 struct fuse_conn *fc = get_fuse_conn(inode);
1395 struct fuse_req *req;
1396 struct fuse_setxattr_in inarg;
1397 int err;
1399 if (fc->no_setxattr)
1400 return -EOPNOTSUPP;
1402 req = fuse_get_req(fc);
1403 if (IS_ERR(req))
1404 return PTR_ERR(req);
1406 memset(&inarg, 0, sizeof(inarg));
1407 inarg.size = size;
1408 inarg.flags = flags;
1409 req->in.h.opcode = FUSE_SETXATTR;
1410 req->in.h.nodeid = get_node_id(inode);
1411 req->in.numargs = 3;
1412 req->in.args[0].size = sizeof(inarg);
1413 req->in.args[0].value = &inarg;
1414 req->in.args[1].size = strlen(name) + 1;
1415 req->in.args[1].value = name;
1416 req->in.args[2].size = size;
1417 req->in.args[2].value = value;
1418 fuse_request_send(fc, req);
1419 err = req->out.h.error;
1420 fuse_put_request(fc, req);
1421 if (err == -ENOSYS) {
1422 fc->no_setxattr = 1;
1423 err = -EOPNOTSUPP;
1425 return err;
1428 static ssize_t fuse_getxattr(struct dentry *entry, const char *name,
1429 void *value, size_t size)
1431 struct inode *inode = entry->d_inode;
1432 struct fuse_conn *fc = get_fuse_conn(inode);
1433 struct fuse_req *req;
1434 struct fuse_getxattr_in inarg;
1435 struct fuse_getxattr_out outarg;
1436 ssize_t ret;
1438 if (fc->no_getxattr)
1439 return -EOPNOTSUPP;
1441 req = fuse_get_req(fc);
1442 if (IS_ERR(req))
1443 return PTR_ERR(req);
1445 memset(&inarg, 0, sizeof(inarg));
1446 inarg.size = size;
1447 req->in.h.opcode = FUSE_GETXATTR;
1448 req->in.h.nodeid = get_node_id(inode);
1449 req->in.numargs = 2;
1450 req->in.args[0].size = sizeof(inarg);
1451 req->in.args[0].value = &inarg;
1452 req->in.args[1].size = strlen(name) + 1;
1453 req->in.args[1].value = name;
1454 /* This is really two different operations rolled into one */
1455 req->out.numargs = 1;
1456 if (size) {
1457 req->out.argvar = 1;
1458 req->out.args[0].size = size;
1459 req->out.args[0].value = value;
1460 } else {
1461 req->out.args[0].size = sizeof(outarg);
1462 req->out.args[0].value = &outarg;
1464 fuse_request_send(fc, req);
1465 ret = req->out.h.error;
1466 if (!ret)
1467 ret = size ? req->out.args[0].size : outarg.size;
1468 else {
1469 if (ret == -ENOSYS) {
1470 fc->no_getxattr = 1;
1471 ret = -EOPNOTSUPP;
1474 fuse_put_request(fc, req);
1475 return ret;
1478 static ssize_t fuse_listxattr(struct dentry *entry, char *list, size_t size)
1480 struct inode *inode = entry->d_inode;
1481 struct fuse_conn *fc = get_fuse_conn(inode);
1482 struct fuse_req *req;
1483 struct fuse_getxattr_in inarg;
1484 struct fuse_getxattr_out outarg;
1485 ssize_t ret;
1487 if (!fuse_allow_task(fc, current))
1488 return -EACCES;
1490 if (fc->no_listxattr)
1491 return -EOPNOTSUPP;
1493 req = fuse_get_req(fc);
1494 if (IS_ERR(req))
1495 return PTR_ERR(req);
1497 memset(&inarg, 0, sizeof(inarg));
1498 inarg.size = size;
1499 req->in.h.opcode = FUSE_LISTXATTR;
1500 req->in.h.nodeid = get_node_id(inode);
1501 req->in.numargs = 1;
1502 req->in.args[0].size = sizeof(inarg);
1503 req->in.args[0].value = &inarg;
1504 /* This is really two different operations rolled into one */
1505 req->out.numargs = 1;
1506 if (size) {
1507 req->out.argvar = 1;
1508 req->out.args[0].size = size;
1509 req->out.args[0].value = list;
1510 } else {
1511 req->out.args[0].size = sizeof(outarg);
1512 req->out.args[0].value = &outarg;
1514 fuse_request_send(fc, req);
1515 ret = req->out.h.error;
1516 if (!ret)
1517 ret = size ? req->out.args[0].size : outarg.size;
1518 else {
1519 if (ret == -ENOSYS) {
1520 fc->no_listxattr = 1;
1521 ret = -EOPNOTSUPP;
1524 fuse_put_request(fc, req);
1525 return ret;
1528 static int fuse_removexattr(struct dentry *entry, const char *name)
1530 struct inode *inode = entry->d_inode;
1531 struct fuse_conn *fc = get_fuse_conn(inode);
1532 struct fuse_req *req;
1533 int err;
1535 if (fc->no_removexattr)
1536 return -EOPNOTSUPP;
1538 req = fuse_get_req(fc);
1539 if (IS_ERR(req))
1540 return PTR_ERR(req);
1542 req->in.h.opcode = FUSE_REMOVEXATTR;
1543 req->in.h.nodeid = get_node_id(inode);
1544 req->in.numargs = 1;
1545 req->in.args[0].size = strlen(name) + 1;
1546 req->in.args[0].value = name;
1547 fuse_request_send(fc, req);
1548 err = req->out.h.error;
1549 fuse_put_request(fc, req);
1550 if (err == -ENOSYS) {
1551 fc->no_removexattr = 1;
1552 err = -EOPNOTSUPP;
1554 return err;
1557 static const struct inode_operations fuse_dir_inode_operations = {
1558 .lookup = fuse_lookup,
1559 .mkdir = fuse_mkdir,
1560 .symlink = fuse_symlink,
1561 .unlink = fuse_unlink,
1562 .rmdir = fuse_rmdir,
1563 .rename = fuse_rename,
1564 .link = fuse_link,
1565 .setattr = fuse_setattr,
1566 .create = fuse_create,
1567 .mknod = fuse_mknod,
1568 .permission = fuse_permission,
1569 .getattr = fuse_getattr,
1570 .setxattr = fuse_setxattr,
1571 .getxattr = fuse_getxattr,
1572 .listxattr = fuse_listxattr,
1573 .removexattr = fuse_removexattr,
1576 static const struct file_operations fuse_dir_operations = {
1577 .llseek = generic_file_llseek,
1578 .read = generic_read_dir,
1579 .readdir = fuse_readdir,
1580 .open = fuse_dir_open,
1581 .release = fuse_dir_release,
1582 .fsync = fuse_dir_fsync,
1585 static const struct inode_operations fuse_common_inode_operations = {
1586 .setattr = fuse_setattr,
1587 .permission = fuse_permission,
1588 .getattr = fuse_getattr,
1589 .setxattr = fuse_setxattr,
1590 .getxattr = fuse_getxattr,
1591 .listxattr = fuse_listxattr,
1592 .removexattr = fuse_removexattr,
1595 static const struct inode_operations fuse_symlink_inode_operations = {
1596 .setattr = fuse_setattr,
1597 .follow_link = fuse_follow_link,
1598 .put_link = fuse_put_link,
1599 .readlink = generic_readlink,
1600 .getattr = fuse_getattr,
1601 .setxattr = fuse_setxattr,
1602 .getxattr = fuse_getxattr,
1603 .listxattr = fuse_listxattr,
1604 .removexattr = fuse_removexattr,
1607 void fuse_init_common(struct inode *inode)
1609 inode->i_op = &fuse_common_inode_operations;
1612 void fuse_init_dir(struct inode *inode)
1614 inode->i_op = &fuse_dir_inode_operations;
1615 inode->i_fop = &fuse_dir_operations;
1618 void fuse_init_symlink(struct inode *inode)
1620 inode->i_op = &fuse_symlink_inode_operations;