]> Git Repo - linux.git/blob - fs/fuse/dir.c
fs: rcu-walk aware d_revalidate method
[linux.git] / fs / fuse / dir.c
1 /*
2   FUSE: Filesystem in Userspace
3   Copyright (C) 2001-2008  Miklos Szeredi <[email protected]>
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_entry_timeout(struct dentry *entry,
67                                       struct fuse_entry_out *o)
68 {
69         fuse_dentry_settime(entry,
70                 time_to_jiffies(o->entry_valid, o->entry_valid_nsec));
71 }
72
73 static u64 attr_timeout(struct fuse_attr_out *o)
74 {
75         return time_to_jiffies(o->attr_valid, o->attr_valid_nsec);
76 }
77
78 static u64 entry_attr_timeout(struct fuse_entry_out *o)
79 {
80         return time_to_jiffies(o->attr_valid, o->attr_valid_nsec);
81 }
82
83 /*
84  * Mark the attributes as stale, so that at the next call to
85  * ->getattr() they will be fetched from userspace
86  */
87 void fuse_invalidate_attr(struct inode *inode)
88 {
89         get_fuse_inode(inode)->i_time = 0;
90 }
91
92 /*
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
95  *
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)
99  */
100 void fuse_invalidate_entry_cache(struct dentry *entry)
101 {
102         fuse_dentry_settime(entry, 0);
103 }
104
105 /*
106  * Same as fuse_invalidate_entry_cache(), but also try to remove the
107  * dentry from the hash
108  */
109 static void fuse_invalidate_entry(struct dentry *entry)
110 {
111         d_invalidate(entry);
112         fuse_invalidate_entry_cache(entry);
113 }
114
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)
118 {
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;
131 }
132
133 u64 fuse_get_attr_version(struct fuse_conn *fc)
134 {
135         u64 curr_version;
136
137         /*
138          * The spin lock isn't actually needed on 64bit archs, but we
139          * don't yet care too much about such optimizations.
140          */
141         spin_lock(&fc->lock);
142         curr_version = fc->attr_version;
143         spin_unlock(&fc->lock);
144
145         return curr_version;
146 }
147
148 /*
149  * Check whether the dentry is still valid
150  *
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.
156  */
157 static int fuse_dentry_revalidate(struct dentry *entry, struct nameidata *nd)
158 {
159         struct inode *inode;
160
161         if (nd->flags & LOOKUP_RCU)
162                 return -ECHILD;
163
164         inode = entry->d_inode;
165         if (inode && is_bad_inode(inode))
166                 return 0;
167         else if (fuse_dentry_time(entry) < get_jiffies_64()) {
168                 int err;
169                 struct fuse_entry_out outarg;
170                 struct fuse_conn *fc;
171                 struct fuse_req *req;
172                 struct fuse_req *forget_req;
173                 struct dentry *parent;
174                 u64 attr_version;
175
176                 /* For negative dentries, always do a fresh lookup */
177                 if (!inode)
178                         return 0;
179
180                 fc = get_fuse_conn(inode);
181                 req = fuse_get_req(fc);
182                 if (IS_ERR(req))
183                         return 0;
184
185                 forget_req = fuse_get_req(fc);
186                 if (IS_ERR(forget_req)) {
187                         fuse_put_request(fc, req);
188                         return 0;
189                 }
190
191                 attr_version = fuse_get_attr_version(fc);
192
193                 parent = dget_parent(entry);
194                 fuse_lookup_init(fc, req, get_node_id(parent->d_inode),
195                                  &entry->d_name, &outarg);
196                 fuse_request_send(fc, req);
197                 dput(parent);
198                 err = req->out.h.error;
199                 fuse_put_request(fc, req);
200                 /* Zero nodeid is same as -ENOENT */
201                 if (!err && !outarg.nodeid)
202                         err = -ENOENT;
203                 if (!err) {
204                         struct fuse_inode *fi = get_fuse_inode(inode);
205                         if (outarg.nodeid != get_node_id(inode)) {
206                                 fuse_send_forget(fc, forget_req,
207                                                  outarg.nodeid, 1);
208                                 return 0;
209                         }
210                         spin_lock(&fc->lock);
211                         fi->nlookup++;
212                         spin_unlock(&fc->lock);
213                 }
214                 fuse_put_request(fc, forget_req);
215                 if (err || (outarg.attr.mode ^ inode->i_mode) & S_IFMT)
216                         return 0;
217
218                 fuse_change_attributes(inode, &outarg.attr,
219                                        entry_attr_timeout(&outarg),
220                                        attr_version);
221                 fuse_change_entry_timeout(entry, &outarg);
222         }
223         return 1;
224 }
225
226 static int invalid_nodeid(u64 nodeid)
227 {
228         return !nodeid || nodeid == FUSE_ROOT_ID;
229 }
230
231 const struct dentry_operations fuse_dentry_operations = {
232         .d_revalidate   = fuse_dentry_revalidate,
233 };
234
235 int fuse_valid_type(int m)
236 {
237         return S_ISREG(m) || S_ISDIR(m) || S_ISLNK(m) || S_ISCHR(m) ||
238                 S_ISBLK(m) || S_ISFIFO(m) || S_ISSOCK(m);
239 }
240
241 /*
242  * Add a directory inode to a dentry, ensuring that no other dentry
243  * refers to this inode.  Called with fc->inst_mutex.
244  */
245 static struct dentry *fuse_d_add_directory(struct dentry *entry,
246                                            struct inode *inode)
247 {
248         struct dentry *alias = d_find_alias(inode);
249         if (alias && !(alias->d_flags & DCACHE_DISCONNECTED)) {
250                 /* This tries to shrink the subtree below alias */
251                 fuse_invalidate_entry(alias);
252                 dput(alias);
253                 if (!list_empty(&inode->i_dentry))
254                         return ERR_PTR(-EBUSY);
255         } else {
256                 dput(alias);
257         }
258         return d_splice_alias(inode, entry);
259 }
260
261 int fuse_lookup_name(struct super_block *sb, u64 nodeid, struct qstr *name,
262                      struct fuse_entry_out *outarg, struct inode **inode)
263 {
264         struct fuse_conn *fc = get_fuse_conn_super(sb);
265         struct fuse_req *req;
266         struct fuse_req *forget_req;
267         u64 attr_version;
268         int err;
269
270         *inode = NULL;
271         err = -ENAMETOOLONG;
272         if (name->len > FUSE_NAME_MAX)
273                 goto out;
274
275         req = fuse_get_req(fc);
276         err = PTR_ERR(req);
277         if (IS_ERR(req))
278                 goto out;
279
280         forget_req = fuse_get_req(fc);
281         err = PTR_ERR(forget_req);
282         if (IS_ERR(forget_req)) {
283                 fuse_put_request(fc, req);
284                 goto out;
285         }
286
287         attr_version = fuse_get_attr_version(fc);
288
289         fuse_lookup_init(fc, req, nodeid, name, outarg);
290         fuse_request_send(fc, req);
291         err = req->out.h.error;
292         fuse_put_request(fc, req);
293         /* Zero nodeid is same as -ENOENT, but with valid timeout */
294         if (err || !outarg->nodeid)
295                 goto out_put_forget;
296
297         err = -EIO;
298         if (!outarg->nodeid)
299                 goto out_put_forget;
300         if (!fuse_valid_type(outarg->attr.mode))
301                 goto out_put_forget;
302
303         *inode = fuse_iget(sb, outarg->nodeid, outarg->generation,
304                            &outarg->attr, entry_attr_timeout(outarg),
305                            attr_version);
306         err = -ENOMEM;
307         if (!*inode) {
308                 fuse_send_forget(fc, forget_req, outarg->nodeid, 1);
309                 goto out;
310         }
311         err = 0;
312
313  out_put_forget:
314         fuse_put_request(fc, forget_req);
315  out:
316         return err;
317 }
318
319 static struct dentry *fuse_lookup(struct inode *dir, struct dentry *entry,
320                                   struct nameidata *nd)
321 {
322         int err;
323         struct fuse_entry_out outarg;
324         struct inode *inode;
325         struct dentry *newent;
326         struct fuse_conn *fc = get_fuse_conn(dir);
327         bool outarg_valid = true;
328
329         err = fuse_lookup_name(dir->i_sb, get_node_id(dir), &entry->d_name,
330                                &outarg, &inode);
331         if (err == -ENOENT) {
332                 outarg_valid = false;
333                 err = 0;
334         }
335         if (err)
336                 goto out_err;
337
338         err = -EIO;
339         if (inode && get_node_id(inode) == FUSE_ROOT_ID)
340                 goto out_iput;
341
342         if (inode && S_ISDIR(inode->i_mode)) {
343                 mutex_lock(&fc->inst_mutex);
344                 newent = fuse_d_add_directory(entry, inode);
345                 mutex_unlock(&fc->inst_mutex);
346                 err = PTR_ERR(newent);
347                 if (IS_ERR(newent))
348                         goto out_iput;
349         } else {
350                 newent = d_splice_alias(inode, entry);
351         }
352
353         entry = newent ? newent : entry;
354         d_set_d_op(entry, &fuse_dentry_operations);
355         if (outarg_valid)
356                 fuse_change_entry_timeout(entry, &outarg);
357         else
358                 fuse_invalidate_entry_cache(entry);
359
360         return newent;
361
362  out_iput:
363         iput(inode);
364  out_err:
365         return ERR_PTR(err);
366 }
367
368 /*
369  * Atomic create+open operation
370  *
371  * If the filesystem doesn't support this, then fall back to separate
372  * 'mknod' + 'open' requests.
373  */
374 static int fuse_create_open(struct inode *dir, struct dentry *entry, int mode,
375                             struct nameidata *nd)
376 {
377         int err;
378         struct inode *inode;
379         struct fuse_conn *fc = get_fuse_conn(dir);
380         struct fuse_req *req;
381         struct fuse_req *forget_req;
382         struct fuse_create_in inarg;
383         struct fuse_open_out outopen;
384         struct fuse_entry_out outentry;
385         struct fuse_file *ff;
386         struct file *file;
387         int flags = nd->intent.open.flags - 1;
388
389         if (fc->no_create)
390                 return -ENOSYS;
391
392         if (flags & O_DIRECT)
393                 return -EINVAL;
394
395         forget_req = fuse_get_req(fc);
396         if (IS_ERR(forget_req))
397                 return PTR_ERR(forget_req);
398
399         req = fuse_get_req(fc);
400         err = PTR_ERR(req);
401         if (IS_ERR(req))
402                 goto out_put_forget_req;
403
404         err = -ENOMEM;
405         ff = fuse_file_alloc(fc);
406         if (!ff)
407                 goto out_put_request;
408
409         if (!fc->dont_mask)
410                 mode &= ~current_umask();
411
412         flags &= ~O_NOCTTY;
413         memset(&inarg, 0, sizeof(inarg));
414         memset(&outentry, 0, sizeof(outentry));
415         inarg.flags = flags;
416         inarg.mode = mode;
417         inarg.umask = current_umask();
418         req->in.h.opcode = FUSE_CREATE;
419         req->in.h.nodeid = get_node_id(dir);
420         req->in.numargs = 2;
421         req->in.args[0].size = fc->minor < 12 ? sizeof(struct fuse_open_in) :
422                                                 sizeof(inarg);
423         req->in.args[0].value = &inarg;
424         req->in.args[1].size = entry->d_name.len + 1;
425         req->in.args[1].value = entry->d_name.name;
426         req->out.numargs = 2;
427         if (fc->minor < 9)
428                 req->out.args[0].size = FUSE_COMPAT_ENTRY_OUT_SIZE;
429         else
430                 req->out.args[0].size = sizeof(outentry);
431         req->out.args[0].value = &outentry;
432         req->out.args[1].size = sizeof(outopen);
433         req->out.args[1].value = &outopen;
434         fuse_request_send(fc, req);
435         err = req->out.h.error;
436         if (err) {
437                 if (err == -ENOSYS)
438                         fc->no_create = 1;
439                 goto out_free_ff;
440         }
441
442         err = -EIO;
443         if (!S_ISREG(outentry.attr.mode) || invalid_nodeid(outentry.nodeid))
444                 goto out_free_ff;
445
446         fuse_put_request(fc, req);
447         ff->fh = outopen.fh;
448         ff->nodeid = outentry.nodeid;
449         ff->open_flags = outopen.open_flags;
450         inode = fuse_iget(dir->i_sb, outentry.nodeid, outentry.generation,
451                           &outentry.attr, entry_attr_timeout(&outentry), 0);
452         if (!inode) {
453                 flags &= ~(O_CREAT | O_EXCL | O_TRUNC);
454                 fuse_sync_release(ff, flags);
455                 fuse_send_forget(fc, forget_req, outentry.nodeid, 1);
456                 return -ENOMEM;
457         }
458         fuse_put_request(fc, forget_req);
459         d_instantiate(entry, inode);
460         fuse_change_entry_timeout(entry, &outentry);
461         fuse_invalidate_attr(dir);
462         file = lookup_instantiate_filp(nd, entry, generic_file_open);
463         if (IS_ERR(file)) {
464                 fuse_sync_release(ff, flags);
465                 return PTR_ERR(file);
466         }
467         file->private_data = fuse_file_get(ff);
468         fuse_finish_open(inode, file);
469         return 0;
470
471  out_free_ff:
472         fuse_file_free(ff);
473  out_put_request:
474         fuse_put_request(fc, req);
475  out_put_forget_req:
476         fuse_put_request(fc, forget_req);
477         return err;
478 }
479
480 /*
481  * Code shared between mknod, mkdir, symlink and link
482  */
483 static int create_new_entry(struct fuse_conn *fc, struct fuse_req *req,
484                             struct inode *dir, struct dentry *entry,
485                             int mode)
486 {
487         struct fuse_entry_out outarg;
488         struct inode *inode;
489         int err;
490         struct fuse_req *forget_req;
491
492         forget_req = fuse_get_req(fc);
493         if (IS_ERR(forget_req)) {
494                 fuse_put_request(fc, req);
495                 return PTR_ERR(forget_req);
496         }
497
498         memset(&outarg, 0, sizeof(outarg));
499         req->in.h.nodeid = get_node_id(dir);
500         req->out.numargs = 1;
501         if (fc->minor < 9)
502                 req->out.args[0].size = FUSE_COMPAT_ENTRY_OUT_SIZE;
503         else
504                 req->out.args[0].size = sizeof(outarg);
505         req->out.args[0].value = &outarg;
506         fuse_request_send(fc, req);
507         err = req->out.h.error;
508         fuse_put_request(fc, req);
509         if (err)
510                 goto out_put_forget_req;
511
512         err = -EIO;
513         if (invalid_nodeid(outarg.nodeid))
514                 goto out_put_forget_req;
515
516         if ((outarg.attr.mode ^ mode) & S_IFMT)
517                 goto out_put_forget_req;
518
519         inode = fuse_iget(dir->i_sb, outarg.nodeid, outarg.generation,
520                           &outarg.attr, entry_attr_timeout(&outarg), 0);
521         if (!inode) {
522                 fuse_send_forget(fc, forget_req, outarg.nodeid, 1);
523                 return -ENOMEM;
524         }
525         fuse_put_request(fc, forget_req);
526
527         if (S_ISDIR(inode->i_mode)) {
528                 struct dentry *alias;
529                 mutex_lock(&fc->inst_mutex);
530                 alias = d_find_alias(inode);
531                 if (alias) {
532                         /* New directory must have moved since mkdir */
533                         mutex_unlock(&fc->inst_mutex);
534                         dput(alias);
535                         iput(inode);
536                         return -EBUSY;
537                 }
538                 d_instantiate(entry, inode);
539                 mutex_unlock(&fc->inst_mutex);
540         } else
541                 d_instantiate(entry, inode);
542
543         fuse_change_entry_timeout(entry, &outarg);
544         fuse_invalidate_attr(dir);
545         return 0;
546
547  out_put_forget_req:
548         fuse_put_request(fc, forget_req);
549         return err;
550 }
551
552 static int fuse_mknod(struct inode *dir, struct dentry *entry, int mode,
553                       dev_t rdev)
554 {
555         struct fuse_mknod_in inarg;
556         struct fuse_conn *fc = get_fuse_conn(dir);
557         struct fuse_req *req = fuse_get_req(fc);
558         if (IS_ERR(req))
559                 return PTR_ERR(req);
560
561         if (!fc->dont_mask)
562                 mode &= ~current_umask();
563
564         memset(&inarg, 0, sizeof(inarg));
565         inarg.mode = mode;
566         inarg.rdev = new_encode_dev(rdev);
567         inarg.umask = current_umask();
568         req->in.h.opcode = FUSE_MKNOD;
569         req->in.numargs = 2;
570         req->in.args[0].size = fc->minor < 12 ? FUSE_COMPAT_MKNOD_IN_SIZE :
571                                                 sizeof(inarg);
572         req->in.args[0].value = &inarg;
573         req->in.args[1].size = entry->d_name.len + 1;
574         req->in.args[1].value = entry->d_name.name;
575         return create_new_entry(fc, req, dir, entry, mode);
576 }
577
578 static int fuse_create(struct inode *dir, struct dentry *entry, int mode,
579                        struct nameidata *nd)
580 {
581         if (nd && (nd->flags & LOOKUP_OPEN)) {
582                 int err = fuse_create_open(dir, entry, mode, nd);
583                 if (err != -ENOSYS)
584                         return err;
585                 /* Fall back on mknod */
586         }
587         return fuse_mknod(dir, entry, mode, 0);
588 }
589
590 static int fuse_mkdir(struct inode *dir, struct dentry *entry, int mode)
591 {
592         struct fuse_mkdir_in inarg;
593         struct fuse_conn *fc = get_fuse_conn(dir);
594         struct fuse_req *req = fuse_get_req(fc);
595         if (IS_ERR(req))
596                 return PTR_ERR(req);
597
598         if (!fc->dont_mask)
599                 mode &= ~current_umask();
600
601         memset(&inarg, 0, sizeof(inarg));
602         inarg.mode = mode;
603         inarg.umask = current_umask();
604         req->in.h.opcode = FUSE_MKDIR;
605         req->in.numargs = 2;
606         req->in.args[0].size = sizeof(inarg);
607         req->in.args[0].value = &inarg;
608         req->in.args[1].size = entry->d_name.len + 1;
609         req->in.args[1].value = entry->d_name.name;
610         return create_new_entry(fc, req, dir, entry, S_IFDIR);
611 }
612
613 static int fuse_symlink(struct inode *dir, struct dentry *entry,
614                         const char *link)
615 {
616         struct fuse_conn *fc = get_fuse_conn(dir);
617         unsigned len = strlen(link) + 1;
618         struct fuse_req *req = fuse_get_req(fc);
619         if (IS_ERR(req))
620                 return PTR_ERR(req);
621
622         req->in.h.opcode = FUSE_SYMLINK;
623         req->in.numargs = 2;
624         req->in.args[0].size = entry->d_name.len + 1;
625         req->in.args[0].value = entry->d_name.name;
626         req->in.args[1].size = len;
627         req->in.args[1].value = link;
628         return create_new_entry(fc, req, dir, entry, S_IFLNK);
629 }
630
631 static int fuse_unlink(struct inode *dir, struct dentry *entry)
632 {
633         int err;
634         struct fuse_conn *fc = get_fuse_conn(dir);
635         struct fuse_req *req = fuse_get_req(fc);
636         if (IS_ERR(req))
637                 return PTR_ERR(req);
638
639         req->in.h.opcode = FUSE_UNLINK;
640         req->in.h.nodeid = get_node_id(dir);
641         req->in.numargs = 1;
642         req->in.args[0].size = entry->d_name.len + 1;
643         req->in.args[0].value = entry->d_name.name;
644         fuse_request_send(fc, req);
645         err = req->out.h.error;
646         fuse_put_request(fc, req);
647         if (!err) {
648                 struct inode *inode = entry->d_inode;
649
650                 /*
651                  * Set nlink to zero so the inode can be cleared, if the inode
652                  * does have more links this will be discovered at the next
653                  * lookup/getattr.
654                  */
655                 clear_nlink(inode);
656                 fuse_invalidate_attr(inode);
657                 fuse_invalidate_attr(dir);
658                 fuse_invalidate_entry_cache(entry);
659         } else if (err == -EINTR)
660                 fuse_invalidate_entry(entry);
661         return err;
662 }
663
664 static int fuse_rmdir(struct inode *dir, struct dentry *entry)
665 {
666         int err;
667         struct fuse_conn *fc = get_fuse_conn(dir);
668         struct fuse_req *req = fuse_get_req(fc);
669         if (IS_ERR(req))
670                 return PTR_ERR(req);
671
672         req->in.h.opcode = FUSE_RMDIR;
673         req->in.h.nodeid = get_node_id(dir);
674         req->in.numargs = 1;
675         req->in.args[0].size = entry->d_name.len + 1;
676         req->in.args[0].value = entry->d_name.name;
677         fuse_request_send(fc, req);
678         err = req->out.h.error;
679         fuse_put_request(fc, req);
680         if (!err) {
681                 clear_nlink(entry->d_inode);
682                 fuse_invalidate_attr(dir);
683                 fuse_invalidate_entry_cache(entry);
684         } else if (err == -EINTR)
685                 fuse_invalidate_entry(entry);
686         return err;
687 }
688
689 static int fuse_rename(struct inode *olddir, struct dentry *oldent,
690                        struct inode *newdir, struct dentry *newent)
691 {
692         int err;
693         struct fuse_rename_in inarg;
694         struct fuse_conn *fc = get_fuse_conn(olddir);
695         struct fuse_req *req = fuse_get_req(fc);
696         if (IS_ERR(req))
697                 return PTR_ERR(req);
698
699         memset(&inarg, 0, sizeof(inarg));
700         inarg.newdir = get_node_id(newdir);
701         req->in.h.opcode = FUSE_RENAME;
702         req->in.h.nodeid = get_node_id(olddir);
703         req->in.numargs = 3;
704         req->in.args[0].size = sizeof(inarg);
705         req->in.args[0].value = &inarg;
706         req->in.args[1].size = oldent->d_name.len + 1;
707         req->in.args[1].value = oldent->d_name.name;
708         req->in.args[2].size = newent->d_name.len + 1;
709         req->in.args[2].value = newent->d_name.name;
710         fuse_request_send(fc, req);
711         err = req->out.h.error;
712         fuse_put_request(fc, req);
713         if (!err) {
714                 /* ctime changes */
715                 fuse_invalidate_attr(oldent->d_inode);
716
717                 fuse_invalidate_attr(olddir);
718                 if (olddir != newdir)
719                         fuse_invalidate_attr(newdir);
720
721                 /* newent will end up negative */
722                 if (newent->d_inode) {
723                         fuse_invalidate_attr(newent->d_inode);
724                         fuse_invalidate_entry_cache(newent);
725                 }
726         } else if (err == -EINTR) {
727                 /* If request was interrupted, DEITY only knows if the
728                    rename actually took place.  If the invalidation
729                    fails (e.g. some process has CWD under the renamed
730                    directory), then there can be inconsistency between
731                    the dcache and the real filesystem.  Tough luck. */
732                 fuse_invalidate_entry(oldent);
733                 if (newent->d_inode)
734                         fuse_invalidate_entry(newent);
735         }
736
737         return err;
738 }
739
740 static int fuse_link(struct dentry *entry, struct inode *newdir,
741                      struct dentry *newent)
742 {
743         int err;
744         struct fuse_link_in inarg;
745         struct inode *inode = entry->d_inode;
746         struct fuse_conn *fc = get_fuse_conn(inode);
747         struct fuse_req *req = fuse_get_req(fc);
748         if (IS_ERR(req))
749                 return PTR_ERR(req);
750
751         memset(&inarg, 0, sizeof(inarg));
752         inarg.oldnodeid = get_node_id(inode);
753         req->in.h.opcode = FUSE_LINK;
754         req->in.numargs = 2;
755         req->in.args[0].size = sizeof(inarg);
756         req->in.args[0].value = &inarg;
757         req->in.args[1].size = newent->d_name.len + 1;
758         req->in.args[1].value = newent->d_name.name;
759         err = create_new_entry(fc, req, newdir, newent, inode->i_mode);
760         /* Contrary to "normal" filesystems it can happen that link
761            makes two "logical" inodes point to the same "physical"
762            inode.  We invalidate the attributes of the old one, so it
763            will reflect changes in the backing inode (link count,
764            etc.)
765         */
766         if (!err || err == -EINTR)
767                 fuse_invalidate_attr(inode);
768         return err;
769 }
770
771 static void fuse_fillattr(struct inode *inode, struct fuse_attr *attr,
772                           struct kstat *stat)
773 {
774         stat->dev = inode->i_sb->s_dev;
775         stat->ino = attr->ino;
776         stat->mode = (inode->i_mode & S_IFMT) | (attr->mode & 07777);
777         stat->nlink = attr->nlink;
778         stat->uid = attr->uid;
779         stat->gid = attr->gid;
780         stat->rdev = inode->i_rdev;
781         stat->atime.tv_sec = attr->atime;
782         stat->atime.tv_nsec = attr->atimensec;
783         stat->mtime.tv_sec = attr->mtime;
784         stat->mtime.tv_nsec = attr->mtimensec;
785         stat->ctime.tv_sec = attr->ctime;
786         stat->ctime.tv_nsec = attr->ctimensec;
787         stat->size = attr->size;
788         stat->blocks = attr->blocks;
789         stat->blksize = (1 << inode->i_blkbits);
790 }
791
792 static int fuse_do_getattr(struct inode *inode, struct kstat *stat,
793                            struct file *file)
794 {
795         int err;
796         struct fuse_getattr_in inarg;
797         struct fuse_attr_out outarg;
798         struct fuse_conn *fc = get_fuse_conn(inode);
799         struct fuse_req *req;
800         u64 attr_version;
801
802         req = fuse_get_req(fc);
803         if (IS_ERR(req))
804                 return PTR_ERR(req);
805
806         attr_version = fuse_get_attr_version(fc);
807
808         memset(&inarg, 0, sizeof(inarg));
809         memset(&outarg, 0, sizeof(outarg));
810         /* Directories have separate file-handle space */
811         if (file && S_ISREG(inode->i_mode)) {
812                 struct fuse_file *ff = file->private_data;
813
814                 inarg.getattr_flags |= FUSE_GETATTR_FH;
815                 inarg.fh = ff->fh;
816         }
817         req->in.h.opcode = FUSE_GETATTR;
818         req->in.h.nodeid = get_node_id(inode);
819         req->in.numargs = 1;
820         req->in.args[0].size = sizeof(inarg);
821         req->in.args[0].value = &inarg;
822         req->out.numargs = 1;
823         if (fc->minor < 9)
824                 req->out.args[0].size = FUSE_COMPAT_ATTR_OUT_SIZE;
825         else
826                 req->out.args[0].size = sizeof(outarg);
827         req->out.args[0].value = &outarg;
828         fuse_request_send(fc, req);
829         err = req->out.h.error;
830         fuse_put_request(fc, req);
831         if (!err) {
832                 if ((inode->i_mode ^ outarg.attr.mode) & S_IFMT) {
833                         make_bad_inode(inode);
834                         err = -EIO;
835                 } else {
836                         fuse_change_attributes(inode, &outarg.attr,
837                                                attr_timeout(&outarg),
838                                                attr_version);
839                         if (stat)
840                                 fuse_fillattr(inode, &outarg.attr, stat);
841                 }
842         }
843         return err;
844 }
845
846 int fuse_update_attributes(struct inode *inode, struct kstat *stat,
847                            struct file *file, bool *refreshed)
848 {
849         struct fuse_inode *fi = get_fuse_inode(inode);
850         int err;
851         bool r;
852
853         if (fi->i_time < get_jiffies_64()) {
854                 r = true;
855                 err = fuse_do_getattr(inode, stat, file);
856         } else {
857                 r = false;
858                 err = 0;
859                 if (stat) {
860                         generic_fillattr(inode, stat);
861                         stat->mode = fi->orig_i_mode;
862                 }
863         }
864
865         if (refreshed != NULL)
866                 *refreshed = r;
867
868         return err;
869 }
870
871 int fuse_reverse_inval_entry(struct super_block *sb, u64 parent_nodeid,
872                              struct qstr *name)
873 {
874         int err = -ENOTDIR;
875         struct inode *parent;
876         struct dentry *dir;
877         struct dentry *entry;
878
879         parent = ilookup5(sb, parent_nodeid, fuse_inode_eq, &parent_nodeid);
880         if (!parent)
881                 return -ENOENT;
882
883         mutex_lock(&parent->i_mutex);
884         if (!S_ISDIR(parent->i_mode))
885                 goto unlock;
886
887         err = -ENOENT;
888         dir = d_find_alias(parent);
889         if (!dir)
890                 goto unlock;
891
892         entry = d_lookup(dir, name);
893         dput(dir);
894         if (!entry)
895                 goto unlock;
896
897         fuse_invalidate_attr(parent);
898         fuse_invalidate_entry(entry);
899         dput(entry);
900         err = 0;
901
902  unlock:
903         mutex_unlock(&parent->i_mutex);
904         iput(parent);
905         return err;
906 }
907
908 /*
909  * Calling into a user-controlled filesystem gives the filesystem
910  * daemon ptrace-like capabilities over the requester process.  This
911  * means, that the filesystem daemon is able to record the exact
912  * filesystem operations performed, and can also control the behavior
913  * of the requester process in otherwise impossible ways.  For example
914  * it can delay the operation for arbitrary length of time allowing
915  * DoS against the requester.
916  *
917  * For this reason only those processes can call into the filesystem,
918  * for which the owner of the mount has ptrace privilege.  This
919  * excludes processes started by other users, suid or sgid processes.
920  */
921 int fuse_allow_task(struct fuse_conn *fc, struct task_struct *task)
922 {
923         const struct cred *cred;
924         int ret;
925
926         if (fc->flags & FUSE_ALLOW_OTHER)
927                 return 1;
928
929         rcu_read_lock();
930         ret = 0;
931         cred = __task_cred(task);
932         if (cred->euid == fc->user_id &&
933             cred->suid == fc->user_id &&
934             cred->uid  == fc->user_id &&
935             cred->egid == fc->group_id &&
936             cred->sgid == fc->group_id &&
937             cred->gid  == fc->group_id)
938                 ret = 1;
939         rcu_read_unlock();
940
941         return ret;
942 }
943
944 static int fuse_access(struct inode *inode, int mask)
945 {
946         struct fuse_conn *fc = get_fuse_conn(inode);
947         struct fuse_req *req;
948         struct fuse_access_in inarg;
949         int err;
950
951         if (fc->no_access)
952                 return 0;
953
954         req = fuse_get_req(fc);
955         if (IS_ERR(req))
956                 return PTR_ERR(req);
957
958         memset(&inarg, 0, sizeof(inarg));
959         inarg.mask = mask & (MAY_READ | MAY_WRITE | MAY_EXEC);
960         req->in.h.opcode = FUSE_ACCESS;
961         req->in.h.nodeid = get_node_id(inode);
962         req->in.numargs = 1;
963         req->in.args[0].size = sizeof(inarg);
964         req->in.args[0].value = &inarg;
965         fuse_request_send(fc, req);
966         err = req->out.h.error;
967         fuse_put_request(fc, req);
968         if (err == -ENOSYS) {
969                 fc->no_access = 1;
970                 err = 0;
971         }
972         return err;
973 }
974
975 /*
976  * Check permission.  The two basic access models of FUSE are:
977  *
978  * 1) Local access checking ('default_permissions' mount option) based
979  * on file mode.  This is the plain old disk filesystem permission
980  * modell.
981  *
982  * 2) "Remote" access checking, where server is responsible for
983  * checking permission in each inode operation.  An exception to this
984  * is if ->permission() was invoked from sys_access() in which case an
985  * access request is sent.  Execute permission is still checked
986  * locally based on file mode.
987  */
988 static int fuse_permission(struct inode *inode, int mask)
989 {
990         struct fuse_conn *fc = get_fuse_conn(inode);
991         bool refreshed = false;
992         int err = 0;
993
994         if (!fuse_allow_task(fc, current))
995                 return -EACCES;
996
997         /*
998          * If attributes are needed, refresh them before proceeding
999          */
1000         if ((fc->flags & FUSE_DEFAULT_PERMISSIONS) ||
1001             ((mask & MAY_EXEC) && S_ISREG(inode->i_mode))) {
1002                 err = fuse_update_attributes(inode, NULL, NULL, &refreshed);
1003                 if (err)
1004                         return err;
1005         }
1006
1007         if (fc->flags & FUSE_DEFAULT_PERMISSIONS) {
1008                 err = generic_permission(inode, mask, NULL);
1009
1010                 /* If permission is denied, try to refresh file
1011                    attributes.  This is also needed, because the root
1012                    node will at first have no permissions */
1013                 if (err == -EACCES && !refreshed) {
1014                         err = fuse_do_getattr(inode, NULL, NULL);
1015                         if (!err)
1016                                 err = generic_permission(inode, mask, NULL);
1017                 }
1018
1019                 /* Note: the opposite of the above test does not
1020                    exist.  So if permissions are revoked this won't be
1021                    noticed immediately, only after the attribute
1022                    timeout has expired */
1023         } else if (mask & (MAY_ACCESS | MAY_CHDIR)) {
1024                 err = fuse_access(inode, mask);
1025         } else if ((mask & MAY_EXEC) && S_ISREG(inode->i_mode)) {
1026                 if (!(inode->i_mode & S_IXUGO)) {
1027                         if (refreshed)
1028                                 return -EACCES;
1029
1030                         err = fuse_do_getattr(inode, NULL, NULL);
1031                         if (!err && !(inode->i_mode & S_IXUGO))
1032                                 return -EACCES;
1033                 }
1034         }
1035         return err;
1036 }
1037
1038 static int parse_dirfile(char *buf, size_t nbytes, struct file *file,
1039                          void *dstbuf, filldir_t filldir)
1040 {
1041         while (nbytes >= FUSE_NAME_OFFSET) {
1042                 struct fuse_dirent *dirent = (struct fuse_dirent *) buf;
1043                 size_t reclen = FUSE_DIRENT_SIZE(dirent);
1044                 int over;
1045                 if (!dirent->namelen || dirent->namelen > FUSE_NAME_MAX)
1046                         return -EIO;
1047                 if (reclen > nbytes)
1048                         break;
1049
1050                 over = filldir(dstbuf, dirent->name, dirent->namelen,
1051                                file->f_pos, dirent->ino, dirent->type);
1052                 if (over)
1053                         break;
1054
1055                 buf += reclen;
1056                 nbytes -= reclen;
1057                 file->f_pos = dirent->off;
1058         }
1059
1060         return 0;
1061 }
1062
1063 static int fuse_readdir(struct file *file, void *dstbuf, filldir_t filldir)
1064 {
1065         int err;
1066         size_t nbytes;
1067         struct page *page;
1068         struct inode *inode = file->f_path.dentry->d_inode;
1069         struct fuse_conn *fc = get_fuse_conn(inode);
1070         struct fuse_req *req;
1071
1072         if (is_bad_inode(inode))
1073                 return -EIO;
1074
1075         req = fuse_get_req(fc);
1076         if (IS_ERR(req))
1077                 return PTR_ERR(req);
1078
1079         page = alloc_page(GFP_KERNEL);
1080         if (!page) {
1081                 fuse_put_request(fc, req);
1082                 return -ENOMEM;
1083         }
1084         req->out.argpages = 1;
1085         req->num_pages = 1;
1086         req->pages[0] = page;
1087         fuse_read_fill(req, file, file->f_pos, PAGE_SIZE, FUSE_READDIR);
1088         fuse_request_send(fc, req);
1089         nbytes = req->out.args[0].size;
1090         err = req->out.h.error;
1091         fuse_put_request(fc, req);
1092         if (!err)
1093                 err = parse_dirfile(page_address(page), nbytes, file, dstbuf,
1094                                     filldir);
1095
1096         __free_page(page);
1097         fuse_invalidate_attr(inode); /* atime changed */
1098         return err;
1099 }
1100
1101 static char *read_link(struct dentry *dentry)
1102 {
1103         struct inode *inode = dentry->d_inode;
1104         struct fuse_conn *fc = get_fuse_conn(inode);
1105         struct fuse_req *req = fuse_get_req(fc);
1106         char *link;
1107
1108         if (IS_ERR(req))
1109                 return ERR_CAST(req);
1110
1111         link = (char *) __get_free_page(GFP_KERNEL);
1112         if (!link) {
1113                 link = ERR_PTR(-ENOMEM);
1114                 goto out;
1115         }
1116         req->in.h.opcode = FUSE_READLINK;
1117         req->in.h.nodeid = get_node_id(inode);
1118         req->out.argvar = 1;
1119         req->out.numargs = 1;
1120         req->out.args[0].size = PAGE_SIZE - 1;
1121         req->out.args[0].value = link;
1122         fuse_request_send(fc, req);
1123         if (req->out.h.error) {
1124                 free_page((unsigned long) link);
1125                 link = ERR_PTR(req->out.h.error);
1126         } else
1127                 link[req->out.args[0].size] = '\0';
1128  out:
1129         fuse_put_request(fc, req);
1130         fuse_invalidate_attr(inode); /* atime changed */
1131         return link;
1132 }
1133
1134 static void free_link(char *link)
1135 {
1136         if (!IS_ERR(link))
1137                 free_page((unsigned long) link);
1138 }
1139
1140 static void *fuse_follow_link(struct dentry *dentry, struct nameidata *nd)
1141 {
1142         nd_set_link(nd, read_link(dentry));
1143         return NULL;
1144 }
1145
1146 static void fuse_put_link(struct dentry *dentry, struct nameidata *nd, void *c)
1147 {
1148         free_link(nd_get_link(nd));
1149 }
1150
1151 static int fuse_dir_open(struct inode *inode, struct file *file)
1152 {
1153         return fuse_open_common(inode, file, true);
1154 }
1155
1156 static int fuse_dir_release(struct inode *inode, struct file *file)
1157 {
1158         fuse_release_common(file, FUSE_RELEASEDIR);
1159
1160         return 0;
1161 }
1162
1163 static int fuse_dir_fsync(struct file *file, int datasync)
1164 {
1165         return fuse_fsync_common(file, datasync, 1);
1166 }
1167
1168 static bool update_mtime(unsigned ivalid)
1169 {
1170         /* Always update if mtime is explicitly set  */
1171         if (ivalid & ATTR_MTIME_SET)
1172                 return true;
1173
1174         /* If it's an open(O_TRUNC) or an ftruncate(), don't update */
1175         if ((ivalid & ATTR_SIZE) && (ivalid & (ATTR_OPEN | ATTR_FILE)))
1176                 return false;
1177
1178         /* In all other cases update */
1179         return true;
1180 }
1181
1182 static void iattr_to_fattr(struct iattr *iattr, struct fuse_setattr_in *arg)
1183 {
1184         unsigned ivalid = iattr->ia_valid;
1185
1186         if (ivalid & ATTR_MODE)
1187                 arg->valid |= FATTR_MODE,   arg->mode = iattr->ia_mode;
1188         if (ivalid & ATTR_UID)
1189                 arg->valid |= FATTR_UID,    arg->uid = iattr->ia_uid;
1190         if (ivalid & ATTR_GID)
1191                 arg->valid |= FATTR_GID,    arg->gid = iattr->ia_gid;
1192         if (ivalid & ATTR_SIZE)
1193                 arg->valid |= FATTR_SIZE,   arg->size = iattr->ia_size;
1194         if (ivalid & ATTR_ATIME) {
1195                 arg->valid |= FATTR_ATIME;
1196                 arg->atime = iattr->ia_atime.tv_sec;
1197                 arg->atimensec = iattr->ia_atime.tv_nsec;
1198                 if (!(ivalid & ATTR_ATIME_SET))
1199                         arg->valid |= FATTR_ATIME_NOW;
1200         }
1201         if ((ivalid & ATTR_MTIME) && update_mtime(ivalid)) {
1202                 arg->valid |= FATTR_MTIME;
1203                 arg->mtime = iattr->ia_mtime.tv_sec;
1204                 arg->mtimensec = iattr->ia_mtime.tv_nsec;
1205                 if (!(ivalid & ATTR_MTIME_SET))
1206                         arg->valid |= FATTR_MTIME_NOW;
1207         }
1208 }
1209
1210 /*
1211  * Prevent concurrent writepages on inode
1212  *
1213  * This is done by adding a negative bias to the inode write counter
1214  * and waiting for all pending writes to finish.
1215  */
1216 void fuse_set_nowrite(struct inode *inode)
1217 {
1218         struct fuse_conn *fc = get_fuse_conn(inode);
1219         struct fuse_inode *fi = get_fuse_inode(inode);
1220
1221         BUG_ON(!mutex_is_locked(&inode->i_mutex));
1222
1223         spin_lock(&fc->lock);
1224         BUG_ON(fi->writectr < 0);
1225         fi->writectr += FUSE_NOWRITE;
1226         spin_unlock(&fc->lock);
1227         wait_event(fi->page_waitq, fi->writectr == FUSE_NOWRITE);
1228 }
1229
1230 /*
1231  * Allow writepages on inode
1232  *
1233  * Remove the bias from the writecounter and send any queued
1234  * writepages.
1235  */
1236 static void __fuse_release_nowrite(struct inode *inode)
1237 {
1238         struct fuse_inode *fi = get_fuse_inode(inode);
1239
1240         BUG_ON(fi->writectr != FUSE_NOWRITE);
1241         fi->writectr = 0;
1242         fuse_flush_writepages(inode);
1243 }
1244
1245 void fuse_release_nowrite(struct inode *inode)
1246 {
1247         struct fuse_conn *fc = get_fuse_conn(inode);
1248
1249         spin_lock(&fc->lock);
1250         __fuse_release_nowrite(inode);
1251         spin_unlock(&fc->lock);
1252 }
1253
1254 /*
1255  * Set attributes, and at the same time refresh them.
1256  *
1257  * Truncation is slightly complicated, because the 'truncate' request
1258  * may fail, in which case we don't want to touch the mapping.
1259  * vmtruncate() doesn't allow for this case, so do the rlimit checking
1260  * and the actual truncation by hand.
1261  */
1262 static int fuse_do_setattr(struct dentry *entry, struct iattr *attr,
1263                            struct file *file)
1264 {
1265         struct inode *inode = entry->d_inode;
1266         struct fuse_conn *fc = get_fuse_conn(inode);
1267         struct fuse_req *req;
1268         struct fuse_setattr_in inarg;
1269         struct fuse_attr_out outarg;
1270         bool is_truncate = false;
1271         loff_t oldsize;
1272         int err;
1273
1274         if (!fuse_allow_task(fc, current))
1275                 return -EACCES;
1276
1277         if (!(fc->flags & FUSE_DEFAULT_PERMISSIONS))
1278                 attr->ia_valid |= ATTR_FORCE;
1279
1280         err = inode_change_ok(inode, attr);
1281         if (err)
1282                 return err;
1283
1284         if ((attr->ia_valid & ATTR_OPEN) && fc->atomic_o_trunc)
1285                 return 0;
1286
1287         if (attr->ia_valid & ATTR_SIZE)
1288                 is_truncate = true;
1289
1290         req = fuse_get_req(fc);
1291         if (IS_ERR(req))
1292                 return PTR_ERR(req);
1293
1294         if (is_truncate)
1295                 fuse_set_nowrite(inode);
1296
1297         memset(&inarg, 0, sizeof(inarg));
1298         memset(&outarg, 0, sizeof(outarg));
1299         iattr_to_fattr(attr, &inarg);
1300         if (file) {
1301                 struct fuse_file *ff = file->private_data;
1302                 inarg.valid |= FATTR_FH;
1303                 inarg.fh = ff->fh;
1304         }
1305         if (attr->ia_valid & ATTR_SIZE) {
1306                 /* For mandatory locking in truncate */
1307                 inarg.valid |= FATTR_LOCKOWNER;
1308                 inarg.lock_owner = fuse_lock_owner_id(fc, current->files);
1309         }
1310         req->in.h.opcode = FUSE_SETATTR;
1311         req->in.h.nodeid = get_node_id(inode);
1312         req->in.numargs = 1;
1313         req->in.args[0].size = sizeof(inarg);
1314         req->in.args[0].value = &inarg;
1315         req->out.numargs = 1;
1316         if (fc->minor < 9)
1317                 req->out.args[0].size = FUSE_COMPAT_ATTR_OUT_SIZE;
1318         else
1319                 req->out.args[0].size = sizeof(outarg);
1320         req->out.args[0].value = &outarg;
1321         fuse_request_send(fc, req);
1322         err = req->out.h.error;
1323         fuse_put_request(fc, req);
1324         if (err) {
1325                 if (err == -EINTR)
1326                         fuse_invalidate_attr(inode);
1327                 goto error;
1328         }
1329
1330         if ((inode->i_mode ^ outarg.attr.mode) & S_IFMT) {
1331                 make_bad_inode(inode);
1332                 err = -EIO;
1333                 goto error;
1334         }
1335
1336         spin_lock(&fc->lock);
1337         fuse_change_attributes_common(inode, &outarg.attr,
1338                                       attr_timeout(&outarg));
1339         oldsize = inode->i_size;
1340         i_size_write(inode, outarg.attr.size);
1341
1342         if (is_truncate) {
1343                 /* NOTE: this may release/reacquire fc->lock */
1344                 __fuse_release_nowrite(inode);
1345         }
1346         spin_unlock(&fc->lock);
1347
1348         /*
1349          * Only call invalidate_inode_pages2() after removing
1350          * FUSE_NOWRITE, otherwise fuse_launder_page() would deadlock.
1351          */
1352         if (S_ISREG(inode->i_mode) && oldsize != outarg.attr.size) {
1353                 truncate_pagecache(inode, oldsize, outarg.attr.size);
1354                 invalidate_inode_pages2(inode->i_mapping);
1355         }
1356
1357         return 0;
1358
1359 error:
1360         if (is_truncate)
1361                 fuse_release_nowrite(inode);
1362
1363         return err;
1364 }
1365
1366 static int fuse_setattr(struct dentry *entry, struct iattr *attr)
1367 {
1368         if (attr->ia_valid & ATTR_FILE)
1369                 return fuse_do_setattr(entry, attr, attr->ia_file);
1370         else
1371                 return fuse_do_setattr(entry, attr, NULL);
1372 }
1373
1374 static int fuse_getattr(struct vfsmount *mnt, struct dentry *entry,
1375                         struct kstat *stat)
1376 {
1377         struct inode *inode = entry->d_inode;
1378         struct fuse_conn *fc = get_fuse_conn(inode);
1379
1380         if (!fuse_allow_task(fc, current))
1381                 return -EACCES;
1382
1383         return fuse_update_attributes(inode, stat, NULL, NULL);
1384 }
1385
1386 static int fuse_setxattr(struct dentry *entry, const char *name,
1387                          const void *value, size_t size, int flags)
1388 {
1389         struct inode *inode = entry->d_inode;
1390         struct fuse_conn *fc = get_fuse_conn(inode);
1391         struct fuse_req *req;
1392         struct fuse_setxattr_in inarg;
1393         int err;
1394
1395         if (fc->no_setxattr)
1396                 return -EOPNOTSUPP;
1397
1398         req = fuse_get_req(fc);
1399         if (IS_ERR(req))
1400                 return PTR_ERR(req);
1401
1402         memset(&inarg, 0, sizeof(inarg));
1403         inarg.size = size;
1404         inarg.flags = flags;
1405         req->in.h.opcode = FUSE_SETXATTR;
1406         req->in.h.nodeid = get_node_id(inode);
1407         req->in.numargs = 3;
1408         req->in.args[0].size = sizeof(inarg);
1409         req->in.args[0].value = &inarg;
1410         req->in.args[1].size = strlen(name) + 1;
1411         req->in.args[1].value = name;
1412         req->in.args[2].size = size;
1413         req->in.args[2].value = value;
1414         fuse_request_send(fc, req);
1415         err = req->out.h.error;
1416         fuse_put_request(fc, req);
1417         if (err == -ENOSYS) {
1418                 fc->no_setxattr = 1;
1419                 err = -EOPNOTSUPP;
1420         }
1421         return err;
1422 }
1423
1424 static ssize_t fuse_getxattr(struct dentry *entry, const char *name,
1425                              void *value, size_t size)
1426 {
1427         struct inode *inode = entry->d_inode;
1428         struct fuse_conn *fc = get_fuse_conn(inode);
1429         struct fuse_req *req;
1430         struct fuse_getxattr_in inarg;
1431         struct fuse_getxattr_out outarg;
1432         ssize_t ret;
1433
1434         if (fc->no_getxattr)
1435                 return -EOPNOTSUPP;
1436
1437         req = fuse_get_req(fc);
1438         if (IS_ERR(req))
1439                 return PTR_ERR(req);
1440
1441         memset(&inarg, 0, sizeof(inarg));
1442         inarg.size = size;
1443         req->in.h.opcode = FUSE_GETXATTR;
1444         req->in.h.nodeid = get_node_id(inode);
1445         req->in.numargs = 2;
1446         req->in.args[0].size = sizeof(inarg);
1447         req->in.args[0].value = &inarg;
1448         req->in.args[1].size = strlen(name) + 1;
1449         req->in.args[1].value = name;
1450         /* This is really two different operations rolled into one */
1451         req->out.numargs = 1;
1452         if (size) {
1453                 req->out.argvar = 1;
1454                 req->out.args[0].size = size;
1455                 req->out.args[0].value = value;
1456         } else {
1457                 req->out.args[0].size = sizeof(outarg);
1458                 req->out.args[0].value = &outarg;
1459         }
1460         fuse_request_send(fc, req);
1461         ret = req->out.h.error;
1462         if (!ret)
1463                 ret = size ? req->out.args[0].size : outarg.size;
1464         else {
1465                 if (ret == -ENOSYS) {
1466                         fc->no_getxattr = 1;
1467                         ret = -EOPNOTSUPP;
1468                 }
1469         }
1470         fuse_put_request(fc, req);
1471         return ret;
1472 }
1473
1474 static ssize_t fuse_listxattr(struct dentry *entry, char *list, size_t size)
1475 {
1476         struct inode *inode = entry->d_inode;
1477         struct fuse_conn *fc = get_fuse_conn(inode);
1478         struct fuse_req *req;
1479         struct fuse_getxattr_in inarg;
1480         struct fuse_getxattr_out outarg;
1481         ssize_t ret;
1482
1483         if (!fuse_allow_task(fc, current))
1484                 return -EACCES;
1485
1486         if (fc->no_listxattr)
1487                 return -EOPNOTSUPP;
1488
1489         req = fuse_get_req(fc);
1490         if (IS_ERR(req))
1491                 return PTR_ERR(req);
1492
1493         memset(&inarg, 0, sizeof(inarg));
1494         inarg.size = size;
1495         req->in.h.opcode = FUSE_LISTXATTR;
1496         req->in.h.nodeid = get_node_id(inode);
1497         req->in.numargs = 1;
1498         req->in.args[0].size = sizeof(inarg);
1499         req->in.args[0].value = &inarg;
1500         /* This is really two different operations rolled into one */
1501         req->out.numargs = 1;
1502         if (size) {
1503                 req->out.argvar = 1;
1504                 req->out.args[0].size = size;
1505                 req->out.args[0].value = list;
1506         } else {
1507                 req->out.args[0].size = sizeof(outarg);
1508                 req->out.args[0].value = &outarg;
1509         }
1510         fuse_request_send(fc, req);
1511         ret = req->out.h.error;
1512         if (!ret)
1513                 ret = size ? req->out.args[0].size : outarg.size;
1514         else {
1515                 if (ret == -ENOSYS) {
1516                         fc->no_listxattr = 1;
1517                         ret = -EOPNOTSUPP;
1518                 }
1519         }
1520         fuse_put_request(fc, req);
1521         return ret;
1522 }
1523
1524 static int fuse_removexattr(struct dentry *entry, const char *name)
1525 {
1526         struct inode *inode = entry->d_inode;
1527         struct fuse_conn *fc = get_fuse_conn(inode);
1528         struct fuse_req *req;
1529         int err;
1530
1531         if (fc->no_removexattr)
1532                 return -EOPNOTSUPP;
1533
1534         req = fuse_get_req(fc);
1535         if (IS_ERR(req))
1536                 return PTR_ERR(req);
1537
1538         req->in.h.opcode = FUSE_REMOVEXATTR;
1539         req->in.h.nodeid = get_node_id(inode);
1540         req->in.numargs = 1;
1541         req->in.args[0].size = strlen(name) + 1;
1542         req->in.args[0].value = name;
1543         fuse_request_send(fc, req);
1544         err = req->out.h.error;
1545         fuse_put_request(fc, req);
1546         if (err == -ENOSYS) {
1547                 fc->no_removexattr = 1;
1548                 err = -EOPNOTSUPP;
1549         }
1550         return err;
1551 }
1552
1553 static const struct inode_operations fuse_dir_inode_operations = {
1554         .lookup         = fuse_lookup,
1555         .mkdir          = fuse_mkdir,
1556         .symlink        = fuse_symlink,
1557         .unlink         = fuse_unlink,
1558         .rmdir          = fuse_rmdir,
1559         .rename         = fuse_rename,
1560         .link           = fuse_link,
1561         .setattr        = fuse_setattr,
1562         .create         = fuse_create,
1563         .mknod          = fuse_mknod,
1564         .permission     = fuse_permission,
1565         .getattr        = fuse_getattr,
1566         .setxattr       = fuse_setxattr,
1567         .getxattr       = fuse_getxattr,
1568         .listxattr      = fuse_listxattr,
1569         .removexattr    = fuse_removexattr,
1570 };
1571
1572 static const struct file_operations fuse_dir_operations = {
1573         .llseek         = generic_file_llseek,
1574         .read           = generic_read_dir,
1575         .readdir        = fuse_readdir,
1576         .open           = fuse_dir_open,
1577         .release        = fuse_dir_release,
1578         .fsync          = fuse_dir_fsync,
1579 };
1580
1581 static const struct inode_operations fuse_common_inode_operations = {
1582         .setattr        = fuse_setattr,
1583         .permission     = fuse_permission,
1584         .getattr        = fuse_getattr,
1585         .setxattr       = fuse_setxattr,
1586         .getxattr       = fuse_getxattr,
1587         .listxattr      = fuse_listxattr,
1588         .removexattr    = fuse_removexattr,
1589 };
1590
1591 static const struct inode_operations fuse_symlink_inode_operations = {
1592         .setattr        = fuse_setattr,
1593         .follow_link    = fuse_follow_link,
1594         .put_link       = fuse_put_link,
1595         .readlink       = generic_readlink,
1596         .getattr        = fuse_getattr,
1597         .setxattr       = fuse_setxattr,
1598         .getxattr       = fuse_getxattr,
1599         .listxattr      = fuse_listxattr,
1600         .removexattr    = fuse_removexattr,
1601 };
1602
1603 void fuse_init_common(struct inode *inode)
1604 {
1605         inode->i_op = &fuse_common_inode_operations;
1606 }
1607
1608 void fuse_init_dir(struct inode *inode)
1609 {
1610         inode->i_op = &fuse_dir_inode_operations;
1611         inode->i_fop = &fuse_dir_operations;
1612 }
1613
1614 void fuse_init_symlink(struct inode *inode)
1615 {
1616         inode->i_op = &fuse_symlink_inode_operations;
1617 }
This page took 0.124431 seconds and 4 git commands to generate.