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