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