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