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