1 /* 2 FUSE: Filesystem in Userspace 3 Copyright (C) 2001-2008 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/sched.h> 14 #include <linux/namei.h> 15 #include <linux/slab.h> 16 17 static bool fuse_use_readdirplus(struct inode *dir, struct dir_context *ctx) 18 { 19 struct fuse_conn *fc = get_fuse_conn(dir); 20 struct fuse_inode *fi = get_fuse_inode(dir); 21 22 if (!fc->do_readdirplus) 23 return false; 24 if (!fc->readdirplus_auto) 25 return true; 26 if (test_and_clear_bit(FUSE_I_ADVISE_RDPLUS, &fi->state)) 27 return true; 28 if (ctx->pos == 0) 29 return true; 30 return false; 31 } 32 33 static void fuse_advise_use_readdirplus(struct inode *dir) 34 { 35 struct fuse_inode *fi = get_fuse_inode(dir); 36 37 set_bit(FUSE_I_ADVISE_RDPLUS, &fi->state); 38 } 39 40 #if BITS_PER_LONG >= 64 41 static inline void fuse_dentry_settime(struct dentry *entry, u64 time) 42 { 43 entry->d_time = time; 44 } 45 46 static inline u64 fuse_dentry_time(struct dentry *entry) 47 { 48 return entry->d_time; 49 } 50 #else 51 /* 52 * On 32 bit archs store the high 32 bits of time in d_fsdata 53 */ 54 static void fuse_dentry_settime(struct dentry *entry, u64 time) 55 { 56 entry->d_time = time; 57 entry->d_fsdata = (void *) (unsigned long) (time >> 32); 58 } 59 60 static u64 fuse_dentry_time(struct dentry *entry) 61 { 62 return (u64) entry->d_time + 63 ((u64) (unsigned long) entry->d_fsdata << 32); 64 } 65 #endif 66 67 /* 68 * FUSE caches dentries and attributes with separate timeout. The 69 * time in jiffies until the dentry/attributes are valid is stored in 70 * dentry->d_time and fuse_inode->i_time respectively. 71 */ 72 73 /* 74 * Calculate the time in jiffies until a dentry/attributes are valid 75 */ 76 static u64 time_to_jiffies(unsigned long sec, unsigned long nsec) 77 { 78 if (sec || nsec) { 79 struct timespec ts = {sec, nsec}; 80 return get_jiffies_64() + timespec_to_jiffies(&ts); 81 } else 82 return 0; 83 } 84 85 /* 86 * Set dentry and possibly attribute timeouts from the lookup/mk* 87 * replies 88 */ 89 static void fuse_change_entry_timeout(struct dentry *entry, 90 struct fuse_entry_out *o) 91 { 92 fuse_dentry_settime(entry, 93 time_to_jiffies(o->entry_valid, o->entry_valid_nsec)); 94 } 95 96 static u64 attr_timeout(struct fuse_attr_out *o) 97 { 98 return time_to_jiffies(o->attr_valid, o->attr_valid_nsec); 99 } 100 101 static u64 entry_attr_timeout(struct fuse_entry_out *o) 102 { 103 return time_to_jiffies(o->attr_valid, o->attr_valid_nsec); 104 } 105 106 /* 107 * Mark the attributes as stale, so that at the next call to 108 * ->getattr() they will be fetched from userspace 109 */ 110 void fuse_invalidate_attr(struct inode *inode) 111 { 112 get_fuse_inode(inode)->i_time = 0; 113 } 114 115 /** 116 * Mark the attributes as stale due to an atime change. Avoid the invalidate if 117 * atime is not used. 118 */ 119 void fuse_invalidate_atime(struct inode *inode) 120 { 121 if (!IS_RDONLY(inode)) 122 fuse_invalidate_attr(inode); 123 } 124 125 /* 126 * Just mark the entry as stale, so that a next attempt to look it up 127 * will result in a new lookup call to userspace 128 * 129 * This is called when a dentry is about to become negative and the 130 * timeout is unknown (unlink, rmdir, rename and in some cases 131 * lookup) 132 */ 133 void fuse_invalidate_entry_cache(struct dentry *entry) 134 { 135 fuse_dentry_settime(entry, 0); 136 } 137 138 /* 139 * Same as fuse_invalidate_entry_cache(), but also try to remove the 140 * dentry from the hash 141 */ 142 static void fuse_invalidate_entry(struct dentry *entry) 143 { 144 d_invalidate(entry); 145 fuse_invalidate_entry_cache(entry); 146 } 147 148 static void fuse_lookup_init(struct fuse_conn *fc, struct fuse_args *args, 149 u64 nodeid, struct qstr *name, 150 struct fuse_entry_out *outarg) 151 { 152 memset(outarg, 0, sizeof(struct fuse_entry_out)); 153 args->in.h.opcode = FUSE_LOOKUP; 154 args->in.h.nodeid = nodeid; 155 args->in.numargs = 1; 156 args->in.args[0].size = name->len + 1; 157 args->in.args[0].value = name->name; 158 args->out.numargs = 1; 159 if (fc->minor < 9) 160 args->out.args[0].size = FUSE_COMPAT_ENTRY_OUT_SIZE; 161 else 162 args->out.args[0].size = sizeof(struct fuse_entry_out); 163 args->out.args[0].value = outarg; 164 } 165 166 u64 fuse_get_attr_version(struct fuse_conn *fc) 167 { 168 u64 curr_version; 169 170 /* 171 * The spin lock isn't actually needed on 64bit archs, but we 172 * don't yet care too much about such optimizations. 173 */ 174 spin_lock(&fc->lock); 175 curr_version = fc->attr_version; 176 spin_unlock(&fc->lock); 177 178 return curr_version; 179 } 180 181 /* 182 * Check whether the dentry is still valid 183 * 184 * If the entry validity timeout has expired and the dentry is 185 * positive, try to redo the lookup. If the lookup results in a 186 * different inode, then let the VFS invalidate the dentry and redo 187 * the lookup once more. If the lookup results in the same inode, 188 * then refresh the attributes, timeouts and mark the dentry valid. 189 */ 190 static int fuse_dentry_revalidate(struct dentry *entry, unsigned int flags) 191 { 192 struct inode *inode; 193 struct dentry *parent; 194 struct fuse_conn *fc; 195 struct fuse_inode *fi; 196 int ret; 197 198 inode = ACCESS_ONCE(entry->d_inode); 199 if (inode && is_bad_inode(inode)) 200 goto invalid; 201 else if (time_before64(fuse_dentry_time(entry), get_jiffies_64()) || 202 (flags & LOOKUP_REVAL)) { 203 struct fuse_entry_out outarg; 204 FUSE_ARGS(args); 205 struct fuse_forget_link *forget; 206 u64 attr_version; 207 208 /* For negative dentries, always do a fresh lookup */ 209 if (!inode) 210 goto invalid; 211 212 ret = -ECHILD; 213 if (flags & LOOKUP_RCU) 214 goto out; 215 216 fc = get_fuse_conn(inode); 217 218 forget = fuse_alloc_forget(); 219 ret = -ENOMEM; 220 if (!forget) 221 goto out; 222 223 attr_version = fuse_get_attr_version(fc); 224 225 parent = dget_parent(entry); 226 fuse_lookup_init(fc, &args, get_node_id(parent->d_inode), 227 &entry->d_name, &outarg); 228 ret = fuse_simple_request(fc, &args); 229 dput(parent); 230 /* Zero nodeid is same as -ENOENT */ 231 if (!ret && !outarg.nodeid) 232 ret = -ENOENT; 233 if (!ret) { 234 fi = get_fuse_inode(inode); 235 if (outarg.nodeid != get_node_id(inode)) { 236 fuse_queue_forget(fc, forget, outarg.nodeid, 1); 237 goto invalid; 238 } 239 spin_lock(&fc->lock); 240 fi->nlookup++; 241 spin_unlock(&fc->lock); 242 } 243 kfree(forget); 244 if (ret == -ENOMEM) 245 goto out; 246 if (ret || (outarg.attr.mode ^ inode->i_mode) & S_IFMT) 247 goto invalid; 248 249 fuse_change_attributes(inode, &outarg.attr, 250 entry_attr_timeout(&outarg), 251 attr_version); 252 fuse_change_entry_timeout(entry, &outarg); 253 } else if (inode) { 254 fi = get_fuse_inode(inode); 255 if (flags & LOOKUP_RCU) { 256 if (test_bit(FUSE_I_INIT_RDPLUS, &fi->state)) 257 return -ECHILD; 258 } else if (test_and_clear_bit(FUSE_I_INIT_RDPLUS, &fi->state)) { 259 parent = dget_parent(entry); 260 fuse_advise_use_readdirplus(parent->d_inode); 261 dput(parent); 262 } 263 } 264 ret = 1; 265 out: 266 return ret; 267 268 invalid: 269 ret = 0; 270 goto out; 271 } 272 273 static int invalid_nodeid(u64 nodeid) 274 { 275 return !nodeid || nodeid == FUSE_ROOT_ID; 276 } 277 278 const struct dentry_operations fuse_dentry_operations = { 279 .d_revalidate = fuse_dentry_revalidate, 280 }; 281 282 int fuse_valid_type(int m) 283 { 284 return S_ISREG(m) || S_ISDIR(m) || S_ISLNK(m) || S_ISCHR(m) || 285 S_ISBLK(m) || S_ISFIFO(m) || S_ISSOCK(m); 286 } 287 288 int fuse_lookup_name(struct super_block *sb, u64 nodeid, struct qstr *name, 289 struct fuse_entry_out *outarg, struct inode **inode) 290 { 291 struct fuse_conn *fc = get_fuse_conn_super(sb); 292 FUSE_ARGS(args); 293 struct fuse_forget_link *forget; 294 u64 attr_version; 295 int err; 296 297 *inode = NULL; 298 err = -ENAMETOOLONG; 299 if (name->len > FUSE_NAME_MAX) 300 goto out; 301 302 303 forget = fuse_alloc_forget(); 304 err = -ENOMEM; 305 if (!forget) 306 goto out; 307 308 attr_version = fuse_get_attr_version(fc); 309 310 fuse_lookup_init(fc, &args, nodeid, name, outarg); 311 err = fuse_simple_request(fc, &args); 312 /* Zero nodeid is same as -ENOENT, but with valid timeout */ 313 if (err || !outarg->nodeid) 314 goto out_put_forget; 315 316 err = -EIO; 317 if (!outarg->nodeid) 318 goto out_put_forget; 319 if (!fuse_valid_type(outarg->attr.mode)) 320 goto out_put_forget; 321 322 *inode = fuse_iget(sb, outarg->nodeid, outarg->generation, 323 &outarg->attr, entry_attr_timeout(outarg), 324 attr_version); 325 err = -ENOMEM; 326 if (!*inode) { 327 fuse_queue_forget(fc, forget, outarg->nodeid, 1); 328 goto out; 329 } 330 err = 0; 331 332 out_put_forget: 333 kfree(forget); 334 out: 335 return err; 336 } 337 338 static struct dentry *fuse_lookup(struct inode *dir, struct dentry *entry, 339 unsigned int flags) 340 { 341 int err; 342 struct fuse_entry_out outarg; 343 struct inode *inode; 344 struct dentry *newent; 345 bool outarg_valid = true; 346 347 err = fuse_lookup_name(dir->i_sb, get_node_id(dir), &entry->d_name, 348 &outarg, &inode); 349 if (err == -ENOENT) { 350 outarg_valid = false; 351 err = 0; 352 } 353 if (err) 354 goto out_err; 355 356 err = -EIO; 357 if (inode && get_node_id(inode) == FUSE_ROOT_ID) 358 goto out_iput; 359 360 newent = d_splice_alias(inode, entry); 361 err = PTR_ERR(newent); 362 if (IS_ERR(newent)) 363 goto out_err; 364 365 entry = newent ? newent : entry; 366 if (outarg_valid) 367 fuse_change_entry_timeout(entry, &outarg); 368 else 369 fuse_invalidate_entry_cache(entry); 370 371 fuse_advise_use_readdirplus(dir); 372 return newent; 373 374 out_iput: 375 iput(inode); 376 out_err: 377 return ERR_PTR(err); 378 } 379 380 /* 381 * Atomic create+open operation 382 * 383 * If the filesystem doesn't support this, then fall back to separate 384 * 'mknod' + 'open' requests. 385 */ 386 static int fuse_create_open(struct inode *dir, struct dentry *entry, 387 struct file *file, unsigned flags, 388 umode_t mode, int *opened) 389 { 390 int err; 391 struct inode *inode; 392 struct fuse_conn *fc = get_fuse_conn(dir); 393 FUSE_ARGS(args); 394 struct fuse_forget_link *forget; 395 struct fuse_create_in inarg; 396 struct fuse_open_out outopen; 397 struct fuse_entry_out outentry; 398 struct fuse_file *ff; 399 400 /* Userspace expects S_IFREG in create mode */ 401 BUG_ON((mode & S_IFMT) != S_IFREG); 402 403 forget = fuse_alloc_forget(); 404 err = -ENOMEM; 405 if (!forget) 406 goto out_err; 407 408 err = -ENOMEM; 409 ff = fuse_file_alloc(fc); 410 if (!ff) 411 goto out_put_forget_req; 412 413 if (!fc->dont_mask) 414 mode &= ~current_umask(); 415 416 flags &= ~O_NOCTTY; 417 memset(&inarg, 0, sizeof(inarg)); 418 memset(&outentry, 0, sizeof(outentry)); 419 inarg.flags = flags; 420 inarg.mode = mode; 421 inarg.umask = current_umask(); 422 args.in.h.opcode = FUSE_CREATE; 423 args.in.h.nodeid = get_node_id(dir); 424 args.in.numargs = 2; 425 args.in.args[0].size = fc->minor < 12 ? sizeof(struct fuse_open_in) : 426 sizeof(inarg); 427 args.in.args[0].value = &inarg; 428 args.in.args[1].size = entry->d_name.len + 1; 429 args.in.args[1].value = entry->d_name.name; 430 args.out.numargs = 2; 431 if (fc->minor < 9) 432 args.out.args[0].size = FUSE_COMPAT_ENTRY_OUT_SIZE; 433 else 434 args.out.args[0].size = sizeof(outentry); 435 args.out.args[0].value = &outentry; 436 args.out.args[1].size = sizeof(outopen); 437 args.out.args[1].value = &outopen; 438 err = fuse_simple_request(fc, &args); 439 if (err) 440 goto out_free_ff; 441 442 err = -EIO; 443 if (!S_ISREG(outentry.attr.mode) || invalid_nodeid(outentry.nodeid)) 444 goto out_free_ff; 445 446 ff->fh = outopen.fh; 447 ff->nodeid = outentry.nodeid; 448 ff->open_flags = outopen.open_flags; 449 inode = fuse_iget(dir->i_sb, outentry.nodeid, outentry.generation, 450 &outentry.attr, entry_attr_timeout(&outentry), 0); 451 if (!inode) { 452 flags &= ~(O_CREAT | O_EXCL | O_TRUNC); 453 fuse_sync_release(ff, flags); 454 fuse_queue_forget(fc, forget, outentry.nodeid, 1); 455 err = -ENOMEM; 456 goto out_err; 457 } 458 kfree(forget); 459 d_instantiate(entry, inode); 460 fuse_change_entry_timeout(entry, &outentry); 461 fuse_invalidate_attr(dir); 462 err = finish_open(file, entry, generic_file_open, opened); 463 if (err) { 464 fuse_sync_release(ff, flags); 465 } else { 466 file->private_data = fuse_file_get(ff); 467 fuse_finish_open(inode, file); 468 } 469 return err; 470 471 out_free_ff: 472 fuse_file_free(ff); 473 out_put_forget_req: 474 kfree(forget); 475 out_err: 476 return err; 477 } 478 479 static int fuse_mknod(struct inode *, struct dentry *, umode_t, dev_t); 480 static int fuse_atomic_open(struct inode *dir, struct dentry *entry, 481 struct file *file, unsigned flags, 482 umode_t mode, int *opened) 483 { 484 int err; 485 struct fuse_conn *fc = get_fuse_conn(dir); 486 struct dentry *res = NULL; 487 488 if (d_unhashed(entry)) { 489 res = fuse_lookup(dir, entry, 0); 490 if (IS_ERR(res)) 491 return PTR_ERR(res); 492 493 if (res) 494 entry = res; 495 } 496 497 if (!(flags & O_CREAT) || entry->d_inode) 498 goto no_open; 499 500 /* Only creates */ 501 *opened |= FILE_CREATED; 502 503 if (fc->no_create) 504 goto mknod; 505 506 err = fuse_create_open(dir, entry, file, flags, mode, opened); 507 if (err == -ENOSYS) { 508 fc->no_create = 1; 509 goto mknod; 510 } 511 out_dput: 512 dput(res); 513 return err; 514 515 mknod: 516 err = fuse_mknod(dir, entry, mode, 0); 517 if (err) 518 goto out_dput; 519 no_open: 520 return finish_no_open(file, res); 521 } 522 523 /* 524 * Code shared between mknod, mkdir, symlink and link 525 */ 526 static int create_new_entry(struct fuse_conn *fc, struct fuse_args *args, 527 struct inode *dir, struct dentry *entry, 528 umode_t mode) 529 { 530 struct fuse_entry_out outarg; 531 struct inode *inode; 532 int err; 533 struct fuse_forget_link *forget; 534 535 forget = fuse_alloc_forget(); 536 if (!forget) 537 return -ENOMEM; 538 539 memset(&outarg, 0, sizeof(outarg)); 540 args->in.h.nodeid = get_node_id(dir); 541 args->out.numargs = 1; 542 if (fc->minor < 9) 543 args->out.args[0].size = FUSE_COMPAT_ENTRY_OUT_SIZE; 544 else 545 args->out.args[0].size = sizeof(outarg); 546 args->out.args[0].value = &outarg; 547 err = fuse_simple_request(fc, args); 548 if (err) 549 goto out_put_forget_req; 550 551 err = -EIO; 552 if (invalid_nodeid(outarg.nodeid)) 553 goto out_put_forget_req; 554 555 if ((outarg.attr.mode ^ mode) & S_IFMT) 556 goto out_put_forget_req; 557 558 inode = fuse_iget(dir->i_sb, outarg.nodeid, outarg.generation, 559 &outarg.attr, entry_attr_timeout(&outarg), 0); 560 if (!inode) { 561 fuse_queue_forget(fc, forget, outarg.nodeid, 1); 562 return -ENOMEM; 563 } 564 kfree(forget); 565 566 err = d_instantiate_no_diralias(entry, inode); 567 if (err) 568 return err; 569 570 fuse_change_entry_timeout(entry, &outarg); 571 fuse_invalidate_attr(dir); 572 return 0; 573 574 out_put_forget_req: 575 kfree(forget); 576 return err; 577 } 578 579 static int fuse_mknod(struct inode *dir, struct dentry *entry, umode_t mode, 580 dev_t rdev) 581 { 582 struct fuse_mknod_in inarg; 583 struct fuse_conn *fc = get_fuse_conn(dir); 584 FUSE_ARGS(args); 585 586 if (!fc->dont_mask) 587 mode &= ~current_umask(); 588 589 memset(&inarg, 0, sizeof(inarg)); 590 inarg.mode = mode; 591 inarg.rdev = new_encode_dev(rdev); 592 inarg.umask = current_umask(); 593 args.in.h.opcode = FUSE_MKNOD; 594 args.in.numargs = 2; 595 args.in.args[0].size = fc->minor < 12 ? FUSE_COMPAT_MKNOD_IN_SIZE : 596 sizeof(inarg); 597 args.in.args[0].value = &inarg; 598 args.in.args[1].size = entry->d_name.len + 1; 599 args.in.args[1].value = entry->d_name.name; 600 return create_new_entry(fc, &args, dir, entry, mode); 601 } 602 603 static int fuse_create(struct inode *dir, struct dentry *entry, umode_t mode, 604 bool excl) 605 { 606 return fuse_mknod(dir, entry, mode, 0); 607 } 608 609 static int fuse_mkdir(struct inode *dir, struct dentry *entry, umode_t mode) 610 { 611 struct fuse_mkdir_in inarg; 612 struct fuse_conn *fc = get_fuse_conn(dir); 613 FUSE_ARGS(args); 614 615 if (!fc->dont_mask) 616 mode &= ~current_umask(); 617 618 memset(&inarg, 0, sizeof(inarg)); 619 inarg.mode = mode; 620 inarg.umask = current_umask(); 621 args.in.h.opcode = FUSE_MKDIR; 622 args.in.numargs = 2; 623 args.in.args[0].size = sizeof(inarg); 624 args.in.args[0].value = &inarg; 625 args.in.args[1].size = entry->d_name.len + 1; 626 args.in.args[1].value = entry->d_name.name; 627 return create_new_entry(fc, &args, dir, entry, S_IFDIR); 628 } 629 630 static int fuse_symlink(struct inode *dir, struct dentry *entry, 631 const char *link) 632 { 633 struct fuse_conn *fc = get_fuse_conn(dir); 634 unsigned len = strlen(link) + 1; 635 FUSE_ARGS(args); 636 637 args.in.h.opcode = FUSE_SYMLINK; 638 args.in.numargs = 2; 639 args.in.args[0].size = entry->d_name.len + 1; 640 args.in.args[0].value = entry->d_name.name; 641 args.in.args[1].size = len; 642 args.in.args[1].value = link; 643 return create_new_entry(fc, &args, dir, entry, S_IFLNK); 644 } 645 646 static inline void fuse_update_ctime(struct inode *inode) 647 { 648 if (!IS_NOCMTIME(inode)) { 649 inode->i_ctime = current_fs_time(inode->i_sb); 650 mark_inode_dirty_sync(inode); 651 } 652 } 653 654 static int fuse_unlink(struct inode *dir, struct dentry *entry) 655 { 656 int err; 657 struct fuse_conn *fc = get_fuse_conn(dir); 658 FUSE_ARGS(args); 659 660 args.in.h.opcode = FUSE_UNLINK; 661 args.in.h.nodeid = get_node_id(dir); 662 args.in.numargs = 1; 663 args.in.args[0].size = entry->d_name.len + 1; 664 args.in.args[0].value = entry->d_name.name; 665 err = fuse_simple_request(fc, &args); 666 if (!err) { 667 struct inode *inode = entry->d_inode; 668 struct fuse_inode *fi = get_fuse_inode(inode); 669 670 spin_lock(&fc->lock); 671 fi->attr_version = ++fc->attr_version; 672 /* 673 * If i_nlink == 0 then unlink doesn't make sense, yet this can 674 * happen if userspace filesystem is careless. It would be 675 * difficult to enforce correct nlink usage so just ignore this 676 * condition here 677 */ 678 if (inode->i_nlink > 0) 679 drop_nlink(inode); 680 spin_unlock(&fc->lock); 681 fuse_invalidate_attr(inode); 682 fuse_invalidate_attr(dir); 683 fuse_invalidate_entry_cache(entry); 684 fuse_update_ctime(inode); 685 } else if (err == -EINTR) 686 fuse_invalidate_entry(entry); 687 return err; 688 } 689 690 static int fuse_rmdir(struct inode *dir, struct dentry *entry) 691 { 692 int err; 693 struct fuse_conn *fc = get_fuse_conn(dir); 694 FUSE_ARGS(args); 695 696 args.in.h.opcode = FUSE_RMDIR; 697 args.in.h.nodeid = get_node_id(dir); 698 args.in.numargs = 1; 699 args.in.args[0].size = entry->d_name.len + 1; 700 args.in.args[0].value = entry->d_name.name; 701 err = fuse_simple_request(fc, &args); 702 if (!err) { 703 clear_nlink(entry->d_inode); 704 fuse_invalidate_attr(dir); 705 fuse_invalidate_entry_cache(entry); 706 } else if (err == -EINTR) 707 fuse_invalidate_entry(entry); 708 return err; 709 } 710 711 static int fuse_rename_common(struct inode *olddir, struct dentry *oldent, 712 struct inode *newdir, struct dentry *newent, 713 unsigned int flags, int opcode, size_t argsize) 714 { 715 int err; 716 struct fuse_rename2_in inarg; 717 struct fuse_conn *fc = get_fuse_conn(olddir); 718 FUSE_ARGS(args); 719 720 memset(&inarg, 0, argsize); 721 inarg.newdir = get_node_id(newdir); 722 inarg.flags = flags; 723 args.in.h.opcode = opcode; 724 args.in.h.nodeid = get_node_id(olddir); 725 args.in.numargs = 3; 726 args.in.args[0].size = argsize; 727 args.in.args[0].value = &inarg; 728 args.in.args[1].size = oldent->d_name.len + 1; 729 args.in.args[1].value = oldent->d_name.name; 730 args.in.args[2].size = newent->d_name.len + 1; 731 args.in.args[2].value = newent->d_name.name; 732 err = fuse_simple_request(fc, &args); 733 if (!err) { 734 /* ctime changes */ 735 fuse_invalidate_attr(oldent->d_inode); 736 fuse_update_ctime(oldent->d_inode); 737 738 if (flags & RENAME_EXCHANGE) { 739 fuse_invalidate_attr(newent->d_inode); 740 fuse_update_ctime(newent->d_inode); 741 } 742 743 fuse_invalidate_attr(olddir); 744 if (olddir != newdir) 745 fuse_invalidate_attr(newdir); 746 747 /* newent will end up negative */ 748 if (!(flags & RENAME_EXCHANGE) && newent->d_inode) { 749 fuse_invalidate_attr(newent->d_inode); 750 fuse_invalidate_entry_cache(newent); 751 fuse_update_ctime(newent->d_inode); 752 } 753 } else if (err == -EINTR) { 754 /* If request was interrupted, DEITY only knows if the 755 rename actually took place. If the invalidation 756 fails (e.g. some process has CWD under the renamed 757 directory), then there can be inconsistency between 758 the dcache and the real filesystem. Tough luck. */ 759 fuse_invalidate_entry(oldent); 760 if (newent->d_inode) 761 fuse_invalidate_entry(newent); 762 } 763 764 return err; 765 } 766 767 static int fuse_rename2(struct inode *olddir, struct dentry *oldent, 768 struct inode *newdir, struct dentry *newent, 769 unsigned int flags) 770 { 771 struct fuse_conn *fc = get_fuse_conn(olddir); 772 int err; 773 774 if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE)) 775 return -EINVAL; 776 777 if (flags) { 778 if (fc->no_rename2 || fc->minor < 23) 779 return -EINVAL; 780 781 err = fuse_rename_common(olddir, oldent, newdir, newent, flags, 782 FUSE_RENAME2, 783 sizeof(struct fuse_rename2_in)); 784 if (err == -ENOSYS) { 785 fc->no_rename2 = 1; 786 err = -EINVAL; 787 } 788 } else { 789 err = fuse_rename_common(olddir, oldent, newdir, newent, 0, 790 FUSE_RENAME, 791 sizeof(struct fuse_rename_in)); 792 } 793 794 return err; 795 } 796 797 static int fuse_link(struct dentry *entry, struct inode *newdir, 798 struct dentry *newent) 799 { 800 int err; 801 struct fuse_link_in inarg; 802 struct inode *inode = entry->d_inode; 803 struct fuse_conn *fc = get_fuse_conn(inode); 804 FUSE_ARGS(args); 805 806 memset(&inarg, 0, sizeof(inarg)); 807 inarg.oldnodeid = get_node_id(inode); 808 args.in.h.opcode = FUSE_LINK; 809 args.in.numargs = 2; 810 args.in.args[0].size = sizeof(inarg); 811 args.in.args[0].value = &inarg; 812 args.in.args[1].size = newent->d_name.len + 1; 813 args.in.args[1].value = newent->d_name.name; 814 err = create_new_entry(fc, &args, newdir, newent, inode->i_mode); 815 /* Contrary to "normal" filesystems it can happen that link 816 makes two "logical" inodes point to the same "physical" 817 inode. We invalidate the attributes of the old one, so it 818 will reflect changes in the backing inode (link count, 819 etc.) 820 */ 821 if (!err) { 822 struct fuse_inode *fi = get_fuse_inode(inode); 823 824 spin_lock(&fc->lock); 825 fi->attr_version = ++fc->attr_version; 826 inc_nlink(inode); 827 spin_unlock(&fc->lock); 828 fuse_invalidate_attr(inode); 829 fuse_update_ctime(inode); 830 } else if (err == -EINTR) { 831 fuse_invalidate_attr(inode); 832 } 833 return err; 834 } 835 836 static void fuse_fillattr(struct inode *inode, struct fuse_attr *attr, 837 struct kstat *stat) 838 { 839 unsigned int blkbits; 840 struct fuse_conn *fc = get_fuse_conn(inode); 841 842 /* see the comment in fuse_change_attributes() */ 843 if (fc->writeback_cache && S_ISREG(inode->i_mode)) { 844 attr->size = i_size_read(inode); 845 attr->mtime = inode->i_mtime.tv_sec; 846 attr->mtimensec = inode->i_mtime.tv_nsec; 847 attr->ctime = inode->i_ctime.tv_sec; 848 attr->ctimensec = inode->i_ctime.tv_nsec; 849 } 850 851 stat->dev = inode->i_sb->s_dev; 852 stat->ino = attr->ino; 853 stat->mode = (inode->i_mode & S_IFMT) | (attr->mode & 07777); 854 stat->nlink = attr->nlink; 855 stat->uid = make_kuid(&init_user_ns, attr->uid); 856 stat->gid = make_kgid(&init_user_ns, attr->gid); 857 stat->rdev = inode->i_rdev; 858 stat->atime.tv_sec = attr->atime; 859 stat->atime.tv_nsec = attr->atimensec; 860 stat->mtime.tv_sec = attr->mtime; 861 stat->mtime.tv_nsec = attr->mtimensec; 862 stat->ctime.tv_sec = attr->ctime; 863 stat->ctime.tv_nsec = attr->ctimensec; 864 stat->size = attr->size; 865 stat->blocks = attr->blocks; 866 867 if (attr->blksize != 0) 868 blkbits = ilog2(attr->blksize); 869 else 870 blkbits = inode->i_sb->s_blocksize_bits; 871 872 stat->blksize = 1 << blkbits; 873 } 874 875 static int fuse_do_getattr(struct inode *inode, struct kstat *stat, 876 struct file *file) 877 { 878 int err; 879 struct fuse_getattr_in inarg; 880 struct fuse_attr_out outarg; 881 struct fuse_conn *fc = get_fuse_conn(inode); 882 FUSE_ARGS(args); 883 u64 attr_version; 884 885 attr_version = fuse_get_attr_version(fc); 886 887 memset(&inarg, 0, sizeof(inarg)); 888 memset(&outarg, 0, sizeof(outarg)); 889 /* Directories have separate file-handle space */ 890 if (file && S_ISREG(inode->i_mode)) { 891 struct fuse_file *ff = file->private_data; 892 893 inarg.getattr_flags |= FUSE_GETATTR_FH; 894 inarg.fh = ff->fh; 895 } 896 args.in.h.opcode = FUSE_GETATTR; 897 args.in.h.nodeid = get_node_id(inode); 898 args.in.numargs = 1; 899 args.in.args[0].size = sizeof(inarg); 900 args.in.args[0].value = &inarg; 901 args.out.numargs = 1; 902 if (fc->minor < 9) 903 args.out.args[0].size = FUSE_COMPAT_ATTR_OUT_SIZE; 904 else 905 args.out.args[0].size = sizeof(outarg); 906 args.out.args[0].value = &outarg; 907 err = fuse_simple_request(fc, &args); 908 if (!err) { 909 if ((inode->i_mode ^ outarg.attr.mode) & S_IFMT) { 910 make_bad_inode(inode); 911 err = -EIO; 912 } else { 913 fuse_change_attributes(inode, &outarg.attr, 914 attr_timeout(&outarg), 915 attr_version); 916 if (stat) 917 fuse_fillattr(inode, &outarg.attr, stat); 918 } 919 } 920 return err; 921 } 922 923 int fuse_update_attributes(struct inode *inode, struct kstat *stat, 924 struct file *file, bool *refreshed) 925 { 926 struct fuse_inode *fi = get_fuse_inode(inode); 927 int err; 928 bool r; 929 930 if (time_before64(fi->i_time, get_jiffies_64())) { 931 r = true; 932 err = fuse_do_getattr(inode, stat, file); 933 } else { 934 r = false; 935 err = 0; 936 if (stat) { 937 generic_fillattr(inode, stat); 938 stat->mode = fi->orig_i_mode; 939 stat->ino = fi->orig_ino; 940 } 941 } 942 943 if (refreshed != NULL) 944 *refreshed = r; 945 946 return err; 947 } 948 949 int fuse_reverse_inval_entry(struct super_block *sb, u64 parent_nodeid, 950 u64 child_nodeid, struct qstr *name) 951 { 952 int err = -ENOTDIR; 953 struct inode *parent; 954 struct dentry *dir; 955 struct dentry *entry; 956 957 parent = ilookup5(sb, parent_nodeid, fuse_inode_eq, &parent_nodeid); 958 if (!parent) 959 return -ENOENT; 960 961 mutex_lock(&parent->i_mutex); 962 if (!S_ISDIR(parent->i_mode)) 963 goto unlock; 964 965 err = -ENOENT; 966 dir = d_find_alias(parent); 967 if (!dir) 968 goto unlock; 969 970 entry = d_lookup(dir, name); 971 dput(dir); 972 if (!entry) 973 goto unlock; 974 975 fuse_invalidate_attr(parent); 976 fuse_invalidate_entry(entry); 977 978 if (child_nodeid != 0 && entry->d_inode) { 979 mutex_lock(&entry->d_inode->i_mutex); 980 if (get_node_id(entry->d_inode) != child_nodeid) { 981 err = -ENOENT; 982 goto badentry; 983 } 984 if (d_mountpoint(entry)) { 985 err = -EBUSY; 986 goto badentry; 987 } 988 if (S_ISDIR(entry->d_inode->i_mode)) { 989 shrink_dcache_parent(entry); 990 if (!simple_empty(entry)) { 991 err = -ENOTEMPTY; 992 goto badentry; 993 } 994 entry->d_inode->i_flags |= S_DEAD; 995 } 996 dont_mount(entry); 997 clear_nlink(entry->d_inode); 998 err = 0; 999 badentry: 1000 mutex_unlock(&entry->d_inode->i_mutex); 1001 if (!err) 1002 d_delete(entry); 1003 } else { 1004 err = 0; 1005 } 1006 dput(entry); 1007 1008 unlock: 1009 mutex_unlock(&parent->i_mutex); 1010 iput(parent); 1011 return err; 1012 } 1013 1014 /* 1015 * Calling into a user-controlled filesystem gives the filesystem 1016 * daemon ptrace-like capabilities over the current process. This 1017 * means, that the filesystem daemon is able to record the exact 1018 * filesystem operations performed, and can also control the behavior 1019 * of the requester process in otherwise impossible ways. For example 1020 * it can delay the operation for arbitrary length of time allowing 1021 * DoS against the requester. 1022 * 1023 * For this reason only those processes can call into the filesystem, 1024 * for which the owner of the mount has ptrace privilege. This 1025 * excludes processes started by other users, suid or sgid processes. 1026 */ 1027 int fuse_allow_current_process(struct fuse_conn *fc) 1028 { 1029 const struct cred *cred; 1030 1031 if (fc->flags & FUSE_ALLOW_OTHER) 1032 return 1; 1033 1034 cred = current_cred(); 1035 if (uid_eq(cred->euid, fc->user_id) && 1036 uid_eq(cred->suid, fc->user_id) && 1037 uid_eq(cred->uid, fc->user_id) && 1038 gid_eq(cred->egid, fc->group_id) && 1039 gid_eq(cred->sgid, fc->group_id) && 1040 gid_eq(cred->gid, fc->group_id)) 1041 return 1; 1042 1043 return 0; 1044 } 1045 1046 static int fuse_access(struct inode *inode, int mask) 1047 { 1048 struct fuse_conn *fc = get_fuse_conn(inode); 1049 FUSE_ARGS(args); 1050 struct fuse_access_in inarg; 1051 int err; 1052 1053 BUG_ON(mask & MAY_NOT_BLOCK); 1054 1055 if (fc->no_access) 1056 return 0; 1057 1058 memset(&inarg, 0, sizeof(inarg)); 1059 inarg.mask = mask & (MAY_READ | MAY_WRITE | MAY_EXEC); 1060 args.in.h.opcode = FUSE_ACCESS; 1061 args.in.h.nodeid = get_node_id(inode); 1062 args.in.numargs = 1; 1063 args.in.args[0].size = sizeof(inarg); 1064 args.in.args[0].value = &inarg; 1065 err = fuse_simple_request(fc, &args); 1066 if (err == -ENOSYS) { 1067 fc->no_access = 1; 1068 err = 0; 1069 } 1070 return err; 1071 } 1072 1073 static int fuse_perm_getattr(struct inode *inode, int mask) 1074 { 1075 if (mask & MAY_NOT_BLOCK) 1076 return -ECHILD; 1077 1078 return fuse_do_getattr(inode, NULL, NULL); 1079 } 1080 1081 /* 1082 * Check permission. The two basic access models of FUSE are: 1083 * 1084 * 1) Local access checking ('default_permissions' mount option) based 1085 * on file mode. This is the plain old disk filesystem permission 1086 * modell. 1087 * 1088 * 2) "Remote" access checking, where server is responsible for 1089 * checking permission in each inode operation. An exception to this 1090 * is if ->permission() was invoked from sys_access() in which case an 1091 * access request is sent. Execute permission is still checked 1092 * locally based on file mode. 1093 */ 1094 static int fuse_permission(struct inode *inode, int mask) 1095 { 1096 struct fuse_conn *fc = get_fuse_conn(inode); 1097 bool refreshed = false; 1098 int err = 0; 1099 1100 if (!fuse_allow_current_process(fc)) 1101 return -EACCES; 1102 1103 /* 1104 * If attributes are needed, refresh them before proceeding 1105 */ 1106 if ((fc->flags & FUSE_DEFAULT_PERMISSIONS) || 1107 ((mask & MAY_EXEC) && S_ISREG(inode->i_mode))) { 1108 struct fuse_inode *fi = get_fuse_inode(inode); 1109 1110 if (time_before64(fi->i_time, get_jiffies_64())) { 1111 refreshed = true; 1112 1113 err = fuse_perm_getattr(inode, mask); 1114 if (err) 1115 return err; 1116 } 1117 } 1118 1119 if (fc->flags & FUSE_DEFAULT_PERMISSIONS) { 1120 err = generic_permission(inode, mask); 1121 1122 /* If permission is denied, try to refresh file 1123 attributes. This is also needed, because the root 1124 node will at first have no permissions */ 1125 if (err == -EACCES && !refreshed) { 1126 err = fuse_perm_getattr(inode, mask); 1127 if (!err) 1128 err = generic_permission(inode, mask); 1129 } 1130 1131 /* Note: the opposite of the above test does not 1132 exist. So if permissions are revoked this won't be 1133 noticed immediately, only after the attribute 1134 timeout has expired */ 1135 } else if (mask & (MAY_ACCESS | MAY_CHDIR)) { 1136 err = fuse_access(inode, mask); 1137 } else if ((mask & MAY_EXEC) && S_ISREG(inode->i_mode)) { 1138 if (!(inode->i_mode & S_IXUGO)) { 1139 if (refreshed) 1140 return -EACCES; 1141 1142 err = fuse_perm_getattr(inode, mask); 1143 if (!err && !(inode->i_mode & S_IXUGO)) 1144 return -EACCES; 1145 } 1146 } 1147 return err; 1148 } 1149 1150 static int parse_dirfile(char *buf, size_t nbytes, struct file *file, 1151 struct dir_context *ctx) 1152 { 1153 while (nbytes >= FUSE_NAME_OFFSET) { 1154 struct fuse_dirent *dirent = (struct fuse_dirent *) buf; 1155 size_t reclen = FUSE_DIRENT_SIZE(dirent); 1156 if (!dirent->namelen || dirent->namelen > FUSE_NAME_MAX) 1157 return -EIO; 1158 if (reclen > nbytes) 1159 break; 1160 if (memchr(dirent->name, '/', dirent->namelen) != NULL) 1161 return -EIO; 1162 1163 if (!dir_emit(ctx, dirent->name, dirent->namelen, 1164 dirent->ino, dirent->type)) 1165 break; 1166 1167 buf += reclen; 1168 nbytes -= reclen; 1169 ctx->pos = dirent->off; 1170 } 1171 1172 return 0; 1173 } 1174 1175 static int fuse_direntplus_link(struct file *file, 1176 struct fuse_direntplus *direntplus, 1177 u64 attr_version) 1178 { 1179 int err; 1180 struct fuse_entry_out *o = &direntplus->entry_out; 1181 struct fuse_dirent *dirent = &direntplus->dirent; 1182 struct dentry *parent = file->f_path.dentry; 1183 struct qstr name = QSTR_INIT(dirent->name, dirent->namelen); 1184 struct dentry *dentry; 1185 struct dentry *alias; 1186 struct inode *dir = parent->d_inode; 1187 struct fuse_conn *fc; 1188 struct inode *inode; 1189 1190 if (!o->nodeid) { 1191 /* 1192 * Unlike in the case of fuse_lookup, zero nodeid does not mean 1193 * ENOENT. Instead, it only means the userspace filesystem did 1194 * not want to return attributes/handle for this entry. 1195 * 1196 * So do nothing. 1197 */ 1198 return 0; 1199 } 1200 1201 if (name.name[0] == '.') { 1202 /* 1203 * We could potentially refresh the attributes of the directory 1204 * and its parent? 1205 */ 1206 if (name.len == 1) 1207 return 0; 1208 if (name.name[1] == '.' && name.len == 2) 1209 return 0; 1210 } 1211 1212 if (invalid_nodeid(o->nodeid)) 1213 return -EIO; 1214 if (!fuse_valid_type(o->attr.mode)) 1215 return -EIO; 1216 1217 fc = get_fuse_conn(dir); 1218 1219 name.hash = full_name_hash(name.name, name.len); 1220 dentry = d_lookup(parent, &name); 1221 if (dentry) { 1222 inode = dentry->d_inode; 1223 if (!inode) { 1224 d_drop(dentry); 1225 } else if (get_node_id(inode) != o->nodeid || 1226 ((o->attr.mode ^ inode->i_mode) & S_IFMT)) { 1227 d_invalidate(dentry); 1228 } else if (is_bad_inode(inode)) { 1229 err = -EIO; 1230 goto out; 1231 } else { 1232 struct fuse_inode *fi; 1233 fi = get_fuse_inode(inode); 1234 spin_lock(&fc->lock); 1235 fi->nlookup++; 1236 spin_unlock(&fc->lock); 1237 1238 fuse_change_attributes(inode, &o->attr, 1239 entry_attr_timeout(o), 1240 attr_version); 1241 1242 /* 1243 * The other branch to 'found' comes via fuse_iget() 1244 * which bumps nlookup inside 1245 */ 1246 goto found; 1247 } 1248 dput(dentry); 1249 } 1250 1251 dentry = d_alloc(parent, &name); 1252 err = -ENOMEM; 1253 if (!dentry) 1254 goto out; 1255 1256 inode = fuse_iget(dir->i_sb, o->nodeid, o->generation, 1257 &o->attr, entry_attr_timeout(o), attr_version); 1258 if (!inode) 1259 goto out; 1260 1261 alias = d_splice_alias(inode, dentry); 1262 err = PTR_ERR(alias); 1263 if (IS_ERR(alias)) 1264 goto out; 1265 1266 if (alias) { 1267 dput(dentry); 1268 dentry = alias; 1269 } 1270 1271 found: 1272 if (fc->readdirplus_auto) 1273 set_bit(FUSE_I_INIT_RDPLUS, &get_fuse_inode(inode)->state); 1274 fuse_change_entry_timeout(dentry, o); 1275 1276 err = 0; 1277 out: 1278 dput(dentry); 1279 return err; 1280 } 1281 1282 static int parse_dirplusfile(char *buf, size_t nbytes, struct file *file, 1283 struct dir_context *ctx, u64 attr_version) 1284 { 1285 struct fuse_direntplus *direntplus; 1286 struct fuse_dirent *dirent; 1287 size_t reclen; 1288 int over = 0; 1289 int ret; 1290 1291 while (nbytes >= FUSE_NAME_OFFSET_DIRENTPLUS) { 1292 direntplus = (struct fuse_direntplus *) buf; 1293 dirent = &direntplus->dirent; 1294 reclen = FUSE_DIRENTPLUS_SIZE(direntplus); 1295 1296 if (!dirent->namelen || dirent->namelen > FUSE_NAME_MAX) 1297 return -EIO; 1298 if (reclen > nbytes) 1299 break; 1300 if (memchr(dirent->name, '/', dirent->namelen) != NULL) 1301 return -EIO; 1302 1303 if (!over) { 1304 /* We fill entries into dstbuf only as much as 1305 it can hold. But we still continue iterating 1306 over remaining entries to link them. If not, 1307 we need to send a FORGET for each of those 1308 which we did not link. 1309 */ 1310 over = !dir_emit(ctx, dirent->name, dirent->namelen, 1311 dirent->ino, dirent->type); 1312 ctx->pos = dirent->off; 1313 } 1314 1315 buf += reclen; 1316 nbytes -= reclen; 1317 1318 ret = fuse_direntplus_link(file, direntplus, attr_version); 1319 if (ret) 1320 fuse_force_forget(file, direntplus->entry_out.nodeid); 1321 } 1322 1323 return 0; 1324 } 1325 1326 static int fuse_readdir(struct file *file, struct dir_context *ctx) 1327 { 1328 int plus, err; 1329 size_t nbytes; 1330 struct page *page; 1331 struct inode *inode = file_inode(file); 1332 struct fuse_conn *fc = get_fuse_conn(inode); 1333 struct fuse_req *req; 1334 u64 attr_version = 0; 1335 1336 if (is_bad_inode(inode)) 1337 return -EIO; 1338 1339 req = fuse_get_req(fc, 1); 1340 if (IS_ERR(req)) 1341 return PTR_ERR(req); 1342 1343 page = alloc_page(GFP_KERNEL); 1344 if (!page) { 1345 fuse_put_request(fc, req); 1346 return -ENOMEM; 1347 } 1348 1349 plus = fuse_use_readdirplus(inode, ctx); 1350 req->out.argpages = 1; 1351 req->num_pages = 1; 1352 req->pages[0] = page; 1353 req->page_descs[0].length = PAGE_SIZE; 1354 if (plus) { 1355 attr_version = fuse_get_attr_version(fc); 1356 fuse_read_fill(req, file, ctx->pos, PAGE_SIZE, 1357 FUSE_READDIRPLUS); 1358 } else { 1359 fuse_read_fill(req, file, ctx->pos, PAGE_SIZE, 1360 FUSE_READDIR); 1361 } 1362 fuse_request_send(fc, req); 1363 nbytes = req->out.args[0].size; 1364 err = req->out.h.error; 1365 fuse_put_request(fc, req); 1366 if (!err) { 1367 if (plus) { 1368 err = parse_dirplusfile(page_address(page), nbytes, 1369 file, ctx, 1370 attr_version); 1371 } else { 1372 err = parse_dirfile(page_address(page), nbytes, file, 1373 ctx); 1374 } 1375 } 1376 1377 __free_page(page); 1378 fuse_invalidate_atime(inode); 1379 return err; 1380 } 1381 1382 static char *read_link(struct dentry *dentry) 1383 { 1384 struct inode *inode = dentry->d_inode; 1385 struct fuse_conn *fc = get_fuse_conn(inode); 1386 FUSE_ARGS(args); 1387 char *link; 1388 ssize_t ret; 1389 1390 link = (char *) __get_free_page(GFP_KERNEL); 1391 if (!link) 1392 return ERR_PTR(-ENOMEM); 1393 1394 args.in.h.opcode = FUSE_READLINK; 1395 args.in.h.nodeid = get_node_id(inode); 1396 args.out.argvar = 1; 1397 args.out.numargs = 1; 1398 args.out.args[0].size = PAGE_SIZE - 1; 1399 args.out.args[0].value = link; 1400 ret = fuse_simple_request(fc, &args); 1401 if (ret < 0) { 1402 free_page((unsigned long) link); 1403 link = ERR_PTR(ret); 1404 } else { 1405 link[ret] = '\0'; 1406 } 1407 fuse_invalidate_atime(inode); 1408 return link; 1409 } 1410 1411 static void free_link(char *link) 1412 { 1413 if (!IS_ERR(link)) 1414 free_page((unsigned long) link); 1415 } 1416 1417 static void *fuse_follow_link(struct dentry *dentry, struct nameidata *nd) 1418 { 1419 nd_set_link(nd, read_link(dentry)); 1420 return NULL; 1421 } 1422 1423 static void fuse_put_link(struct dentry *dentry, struct nameidata *nd, void *c) 1424 { 1425 free_link(nd_get_link(nd)); 1426 } 1427 1428 static int fuse_dir_open(struct inode *inode, struct file *file) 1429 { 1430 return fuse_open_common(inode, file, true); 1431 } 1432 1433 static int fuse_dir_release(struct inode *inode, struct file *file) 1434 { 1435 fuse_release_common(file, FUSE_RELEASEDIR); 1436 1437 return 0; 1438 } 1439 1440 static int fuse_dir_fsync(struct file *file, loff_t start, loff_t end, 1441 int datasync) 1442 { 1443 return fuse_fsync_common(file, start, end, datasync, 1); 1444 } 1445 1446 static long fuse_dir_ioctl(struct file *file, unsigned int cmd, 1447 unsigned long arg) 1448 { 1449 struct fuse_conn *fc = get_fuse_conn(file->f_mapping->host); 1450 1451 /* FUSE_IOCTL_DIR only supported for API version >= 7.18 */ 1452 if (fc->minor < 18) 1453 return -ENOTTY; 1454 1455 return fuse_ioctl_common(file, cmd, arg, FUSE_IOCTL_DIR); 1456 } 1457 1458 static long fuse_dir_compat_ioctl(struct file *file, unsigned int cmd, 1459 unsigned long arg) 1460 { 1461 struct fuse_conn *fc = get_fuse_conn(file->f_mapping->host); 1462 1463 if (fc->minor < 18) 1464 return -ENOTTY; 1465 1466 return fuse_ioctl_common(file, cmd, arg, 1467 FUSE_IOCTL_COMPAT | FUSE_IOCTL_DIR); 1468 } 1469 1470 static bool update_mtime(unsigned ivalid, bool trust_local_mtime) 1471 { 1472 /* Always update if mtime is explicitly set */ 1473 if (ivalid & ATTR_MTIME_SET) 1474 return true; 1475 1476 /* Or if kernel i_mtime is the official one */ 1477 if (trust_local_mtime) 1478 return true; 1479 1480 /* If it's an open(O_TRUNC) or an ftruncate(), don't update */ 1481 if ((ivalid & ATTR_SIZE) && (ivalid & (ATTR_OPEN | ATTR_FILE))) 1482 return false; 1483 1484 /* In all other cases update */ 1485 return true; 1486 } 1487 1488 static void iattr_to_fattr(struct iattr *iattr, struct fuse_setattr_in *arg, 1489 bool trust_local_cmtime) 1490 { 1491 unsigned ivalid = iattr->ia_valid; 1492 1493 if (ivalid & ATTR_MODE) 1494 arg->valid |= FATTR_MODE, arg->mode = iattr->ia_mode; 1495 if (ivalid & ATTR_UID) 1496 arg->valid |= FATTR_UID, arg->uid = from_kuid(&init_user_ns, iattr->ia_uid); 1497 if (ivalid & ATTR_GID) 1498 arg->valid |= FATTR_GID, arg->gid = from_kgid(&init_user_ns, iattr->ia_gid); 1499 if (ivalid & ATTR_SIZE) 1500 arg->valid |= FATTR_SIZE, arg->size = iattr->ia_size; 1501 if (ivalid & ATTR_ATIME) { 1502 arg->valid |= FATTR_ATIME; 1503 arg->atime = iattr->ia_atime.tv_sec; 1504 arg->atimensec = iattr->ia_atime.tv_nsec; 1505 if (!(ivalid & ATTR_ATIME_SET)) 1506 arg->valid |= FATTR_ATIME_NOW; 1507 } 1508 if ((ivalid & ATTR_MTIME) && update_mtime(ivalid, trust_local_cmtime)) { 1509 arg->valid |= FATTR_MTIME; 1510 arg->mtime = iattr->ia_mtime.tv_sec; 1511 arg->mtimensec = iattr->ia_mtime.tv_nsec; 1512 if (!(ivalid & ATTR_MTIME_SET) && !trust_local_cmtime) 1513 arg->valid |= FATTR_MTIME_NOW; 1514 } 1515 if ((ivalid & ATTR_CTIME) && trust_local_cmtime) { 1516 arg->valid |= FATTR_CTIME; 1517 arg->ctime = iattr->ia_ctime.tv_sec; 1518 arg->ctimensec = iattr->ia_ctime.tv_nsec; 1519 } 1520 } 1521 1522 /* 1523 * Prevent concurrent writepages on inode 1524 * 1525 * This is done by adding a negative bias to the inode write counter 1526 * and waiting for all pending writes to finish. 1527 */ 1528 void fuse_set_nowrite(struct inode *inode) 1529 { 1530 struct fuse_conn *fc = get_fuse_conn(inode); 1531 struct fuse_inode *fi = get_fuse_inode(inode); 1532 1533 BUG_ON(!mutex_is_locked(&inode->i_mutex)); 1534 1535 spin_lock(&fc->lock); 1536 BUG_ON(fi->writectr < 0); 1537 fi->writectr += FUSE_NOWRITE; 1538 spin_unlock(&fc->lock); 1539 wait_event(fi->page_waitq, fi->writectr == FUSE_NOWRITE); 1540 } 1541 1542 /* 1543 * Allow writepages on inode 1544 * 1545 * Remove the bias from the writecounter and send any queued 1546 * writepages. 1547 */ 1548 static void __fuse_release_nowrite(struct inode *inode) 1549 { 1550 struct fuse_inode *fi = get_fuse_inode(inode); 1551 1552 BUG_ON(fi->writectr != FUSE_NOWRITE); 1553 fi->writectr = 0; 1554 fuse_flush_writepages(inode); 1555 } 1556 1557 void fuse_release_nowrite(struct inode *inode) 1558 { 1559 struct fuse_conn *fc = get_fuse_conn(inode); 1560 1561 spin_lock(&fc->lock); 1562 __fuse_release_nowrite(inode); 1563 spin_unlock(&fc->lock); 1564 } 1565 1566 static void fuse_setattr_fill(struct fuse_conn *fc, struct fuse_args *args, 1567 struct inode *inode, 1568 struct fuse_setattr_in *inarg_p, 1569 struct fuse_attr_out *outarg_p) 1570 { 1571 args->in.h.opcode = FUSE_SETATTR; 1572 args->in.h.nodeid = get_node_id(inode); 1573 args->in.numargs = 1; 1574 args->in.args[0].size = sizeof(*inarg_p); 1575 args->in.args[0].value = inarg_p; 1576 args->out.numargs = 1; 1577 if (fc->minor < 9) 1578 args->out.args[0].size = FUSE_COMPAT_ATTR_OUT_SIZE; 1579 else 1580 args->out.args[0].size = sizeof(*outarg_p); 1581 args->out.args[0].value = outarg_p; 1582 } 1583 1584 /* 1585 * Flush inode->i_mtime to the server 1586 */ 1587 int fuse_flush_times(struct inode *inode, struct fuse_file *ff) 1588 { 1589 struct fuse_conn *fc = get_fuse_conn(inode); 1590 FUSE_ARGS(args); 1591 struct fuse_setattr_in inarg; 1592 struct fuse_attr_out outarg; 1593 1594 memset(&inarg, 0, sizeof(inarg)); 1595 memset(&outarg, 0, sizeof(outarg)); 1596 1597 inarg.valid = FATTR_MTIME; 1598 inarg.mtime = inode->i_mtime.tv_sec; 1599 inarg.mtimensec = inode->i_mtime.tv_nsec; 1600 if (fc->minor >= 23) { 1601 inarg.valid |= FATTR_CTIME; 1602 inarg.ctime = inode->i_ctime.tv_sec; 1603 inarg.ctimensec = inode->i_ctime.tv_nsec; 1604 } 1605 if (ff) { 1606 inarg.valid |= FATTR_FH; 1607 inarg.fh = ff->fh; 1608 } 1609 fuse_setattr_fill(fc, &args, inode, &inarg, &outarg); 1610 1611 return fuse_simple_request(fc, &args); 1612 } 1613 1614 /* 1615 * Set attributes, and at the same time refresh them. 1616 * 1617 * Truncation is slightly complicated, because the 'truncate' request 1618 * may fail, in which case we don't want to touch the mapping. 1619 * vmtruncate() doesn't allow for this case, so do the rlimit checking 1620 * and the actual truncation by hand. 1621 */ 1622 int fuse_do_setattr(struct inode *inode, struct iattr *attr, 1623 struct file *file) 1624 { 1625 struct fuse_conn *fc = get_fuse_conn(inode); 1626 struct fuse_inode *fi = get_fuse_inode(inode); 1627 FUSE_ARGS(args); 1628 struct fuse_setattr_in inarg; 1629 struct fuse_attr_out outarg; 1630 bool is_truncate = false; 1631 bool is_wb = fc->writeback_cache; 1632 loff_t oldsize; 1633 int err; 1634 bool trust_local_cmtime = is_wb && S_ISREG(inode->i_mode); 1635 1636 if (!(fc->flags & FUSE_DEFAULT_PERMISSIONS)) 1637 attr->ia_valid |= ATTR_FORCE; 1638 1639 err = inode_change_ok(inode, attr); 1640 if (err) 1641 return err; 1642 1643 if (attr->ia_valid & ATTR_OPEN) { 1644 if (fc->atomic_o_trunc) 1645 return 0; 1646 file = NULL; 1647 } 1648 1649 if (attr->ia_valid & ATTR_SIZE) 1650 is_truncate = true; 1651 1652 if (is_truncate) { 1653 fuse_set_nowrite(inode); 1654 set_bit(FUSE_I_SIZE_UNSTABLE, &fi->state); 1655 if (trust_local_cmtime && attr->ia_size != inode->i_size) 1656 attr->ia_valid |= ATTR_MTIME | ATTR_CTIME; 1657 } 1658 1659 memset(&inarg, 0, sizeof(inarg)); 1660 memset(&outarg, 0, sizeof(outarg)); 1661 iattr_to_fattr(attr, &inarg, trust_local_cmtime); 1662 if (file) { 1663 struct fuse_file *ff = file->private_data; 1664 inarg.valid |= FATTR_FH; 1665 inarg.fh = ff->fh; 1666 } 1667 if (attr->ia_valid & ATTR_SIZE) { 1668 /* For mandatory locking in truncate */ 1669 inarg.valid |= FATTR_LOCKOWNER; 1670 inarg.lock_owner = fuse_lock_owner_id(fc, current->files); 1671 } 1672 fuse_setattr_fill(fc, &args, inode, &inarg, &outarg); 1673 err = fuse_simple_request(fc, &args); 1674 if (err) { 1675 if (err == -EINTR) 1676 fuse_invalidate_attr(inode); 1677 goto error; 1678 } 1679 1680 if ((inode->i_mode ^ outarg.attr.mode) & S_IFMT) { 1681 make_bad_inode(inode); 1682 err = -EIO; 1683 goto error; 1684 } 1685 1686 spin_lock(&fc->lock); 1687 /* the kernel maintains i_mtime locally */ 1688 if (trust_local_cmtime) { 1689 if (attr->ia_valid & ATTR_MTIME) 1690 inode->i_mtime = attr->ia_mtime; 1691 if (attr->ia_valid & ATTR_CTIME) 1692 inode->i_ctime = attr->ia_ctime; 1693 /* FIXME: clear I_DIRTY_SYNC? */ 1694 } 1695 1696 fuse_change_attributes_common(inode, &outarg.attr, 1697 attr_timeout(&outarg)); 1698 oldsize = inode->i_size; 1699 /* see the comment in fuse_change_attributes() */ 1700 if (!is_wb || is_truncate || !S_ISREG(inode->i_mode)) 1701 i_size_write(inode, outarg.attr.size); 1702 1703 if (is_truncate) { 1704 /* NOTE: this may release/reacquire fc->lock */ 1705 __fuse_release_nowrite(inode); 1706 } 1707 spin_unlock(&fc->lock); 1708 1709 /* 1710 * Only call invalidate_inode_pages2() after removing 1711 * FUSE_NOWRITE, otherwise fuse_launder_page() would deadlock. 1712 */ 1713 if ((is_truncate || !is_wb) && 1714 S_ISREG(inode->i_mode) && oldsize != outarg.attr.size) { 1715 truncate_pagecache(inode, outarg.attr.size); 1716 invalidate_inode_pages2(inode->i_mapping); 1717 } 1718 1719 clear_bit(FUSE_I_SIZE_UNSTABLE, &fi->state); 1720 return 0; 1721 1722 error: 1723 if (is_truncate) 1724 fuse_release_nowrite(inode); 1725 1726 clear_bit(FUSE_I_SIZE_UNSTABLE, &fi->state); 1727 return err; 1728 } 1729 1730 static int fuse_setattr(struct dentry *entry, struct iattr *attr) 1731 { 1732 struct inode *inode = entry->d_inode; 1733 1734 if (!fuse_allow_current_process(get_fuse_conn(inode))) 1735 return -EACCES; 1736 1737 if (attr->ia_valid & ATTR_FILE) 1738 return fuse_do_setattr(inode, attr, attr->ia_file); 1739 else 1740 return fuse_do_setattr(inode, attr, NULL); 1741 } 1742 1743 static int fuse_getattr(struct vfsmount *mnt, struct dentry *entry, 1744 struct kstat *stat) 1745 { 1746 struct inode *inode = entry->d_inode; 1747 struct fuse_conn *fc = get_fuse_conn(inode); 1748 1749 if (!fuse_allow_current_process(fc)) 1750 return -EACCES; 1751 1752 return fuse_update_attributes(inode, stat, NULL, NULL); 1753 } 1754 1755 static int fuse_setxattr(struct dentry *entry, const char *name, 1756 const void *value, size_t size, int flags) 1757 { 1758 struct inode *inode = entry->d_inode; 1759 struct fuse_conn *fc = get_fuse_conn(inode); 1760 FUSE_ARGS(args); 1761 struct fuse_setxattr_in inarg; 1762 int err; 1763 1764 if (fc->no_setxattr) 1765 return -EOPNOTSUPP; 1766 1767 memset(&inarg, 0, sizeof(inarg)); 1768 inarg.size = size; 1769 inarg.flags = flags; 1770 args.in.h.opcode = FUSE_SETXATTR; 1771 args.in.h.nodeid = get_node_id(inode); 1772 args.in.numargs = 3; 1773 args.in.args[0].size = sizeof(inarg); 1774 args.in.args[0].value = &inarg; 1775 args.in.args[1].size = strlen(name) + 1; 1776 args.in.args[1].value = name; 1777 args.in.args[2].size = size; 1778 args.in.args[2].value = value; 1779 err = fuse_simple_request(fc, &args); 1780 if (err == -ENOSYS) { 1781 fc->no_setxattr = 1; 1782 err = -EOPNOTSUPP; 1783 } 1784 if (!err) { 1785 fuse_invalidate_attr(inode); 1786 fuse_update_ctime(inode); 1787 } 1788 return err; 1789 } 1790 1791 static ssize_t fuse_getxattr(struct dentry *entry, const char *name, 1792 void *value, size_t size) 1793 { 1794 struct inode *inode = entry->d_inode; 1795 struct fuse_conn *fc = get_fuse_conn(inode); 1796 FUSE_ARGS(args); 1797 struct fuse_getxattr_in inarg; 1798 struct fuse_getxattr_out outarg; 1799 ssize_t ret; 1800 1801 if (fc->no_getxattr) 1802 return -EOPNOTSUPP; 1803 1804 memset(&inarg, 0, sizeof(inarg)); 1805 inarg.size = size; 1806 args.in.h.opcode = FUSE_GETXATTR; 1807 args.in.h.nodeid = get_node_id(inode); 1808 args.in.numargs = 2; 1809 args.in.args[0].size = sizeof(inarg); 1810 args.in.args[0].value = &inarg; 1811 args.in.args[1].size = strlen(name) + 1; 1812 args.in.args[1].value = name; 1813 /* This is really two different operations rolled into one */ 1814 args.out.numargs = 1; 1815 if (size) { 1816 args.out.argvar = 1; 1817 args.out.args[0].size = size; 1818 args.out.args[0].value = value; 1819 } else { 1820 args.out.args[0].size = sizeof(outarg); 1821 args.out.args[0].value = &outarg; 1822 } 1823 ret = fuse_simple_request(fc, &args); 1824 if (!ret && !size) 1825 ret = outarg.size; 1826 if (ret == -ENOSYS) { 1827 fc->no_getxattr = 1; 1828 ret = -EOPNOTSUPP; 1829 } 1830 return ret; 1831 } 1832 1833 static ssize_t fuse_listxattr(struct dentry *entry, char *list, size_t size) 1834 { 1835 struct inode *inode = entry->d_inode; 1836 struct fuse_conn *fc = get_fuse_conn(inode); 1837 FUSE_ARGS(args); 1838 struct fuse_getxattr_in inarg; 1839 struct fuse_getxattr_out outarg; 1840 ssize_t ret; 1841 1842 if (!fuse_allow_current_process(fc)) 1843 return -EACCES; 1844 1845 if (fc->no_listxattr) 1846 return -EOPNOTSUPP; 1847 1848 memset(&inarg, 0, sizeof(inarg)); 1849 inarg.size = size; 1850 args.in.h.opcode = FUSE_LISTXATTR; 1851 args.in.h.nodeid = get_node_id(inode); 1852 args.in.numargs = 1; 1853 args.in.args[0].size = sizeof(inarg); 1854 args.in.args[0].value = &inarg; 1855 /* This is really two different operations rolled into one */ 1856 args.out.numargs = 1; 1857 if (size) { 1858 args.out.argvar = 1; 1859 args.out.args[0].size = size; 1860 args.out.args[0].value = list; 1861 } else { 1862 args.out.args[0].size = sizeof(outarg); 1863 args.out.args[0].value = &outarg; 1864 } 1865 ret = fuse_simple_request(fc, &args); 1866 if (!ret && !size) 1867 ret = outarg.size; 1868 if (ret == -ENOSYS) { 1869 fc->no_listxattr = 1; 1870 ret = -EOPNOTSUPP; 1871 } 1872 return ret; 1873 } 1874 1875 static int fuse_removexattr(struct dentry *entry, const char *name) 1876 { 1877 struct inode *inode = entry->d_inode; 1878 struct fuse_conn *fc = get_fuse_conn(inode); 1879 FUSE_ARGS(args); 1880 int err; 1881 1882 if (fc->no_removexattr) 1883 return -EOPNOTSUPP; 1884 1885 args.in.h.opcode = FUSE_REMOVEXATTR; 1886 args.in.h.nodeid = get_node_id(inode); 1887 args.in.numargs = 1; 1888 args.in.args[0].size = strlen(name) + 1; 1889 args.in.args[0].value = name; 1890 err = fuse_simple_request(fc, &args); 1891 if (err == -ENOSYS) { 1892 fc->no_removexattr = 1; 1893 err = -EOPNOTSUPP; 1894 } 1895 if (!err) { 1896 fuse_invalidate_attr(inode); 1897 fuse_update_ctime(inode); 1898 } 1899 return err; 1900 } 1901 1902 static const struct inode_operations fuse_dir_inode_operations = { 1903 .lookup = fuse_lookup, 1904 .mkdir = fuse_mkdir, 1905 .symlink = fuse_symlink, 1906 .unlink = fuse_unlink, 1907 .rmdir = fuse_rmdir, 1908 .rename2 = fuse_rename2, 1909 .link = fuse_link, 1910 .setattr = fuse_setattr, 1911 .create = fuse_create, 1912 .atomic_open = fuse_atomic_open, 1913 .mknod = fuse_mknod, 1914 .permission = fuse_permission, 1915 .getattr = fuse_getattr, 1916 .setxattr = fuse_setxattr, 1917 .getxattr = fuse_getxattr, 1918 .listxattr = fuse_listxattr, 1919 .removexattr = fuse_removexattr, 1920 }; 1921 1922 static const struct file_operations fuse_dir_operations = { 1923 .llseek = generic_file_llseek, 1924 .read = generic_read_dir, 1925 .iterate = fuse_readdir, 1926 .open = fuse_dir_open, 1927 .release = fuse_dir_release, 1928 .fsync = fuse_dir_fsync, 1929 .unlocked_ioctl = fuse_dir_ioctl, 1930 .compat_ioctl = fuse_dir_compat_ioctl, 1931 }; 1932 1933 static const struct inode_operations fuse_common_inode_operations = { 1934 .setattr = fuse_setattr, 1935 .permission = fuse_permission, 1936 .getattr = fuse_getattr, 1937 .setxattr = fuse_setxattr, 1938 .getxattr = fuse_getxattr, 1939 .listxattr = fuse_listxattr, 1940 .removexattr = fuse_removexattr, 1941 }; 1942 1943 static const struct inode_operations fuse_symlink_inode_operations = { 1944 .setattr = fuse_setattr, 1945 .follow_link = fuse_follow_link, 1946 .put_link = fuse_put_link, 1947 .readlink = generic_readlink, 1948 .getattr = fuse_getattr, 1949 .setxattr = fuse_setxattr, 1950 .getxattr = fuse_getxattr, 1951 .listxattr = fuse_listxattr, 1952 .removexattr = fuse_removexattr, 1953 }; 1954 1955 void fuse_init_common(struct inode *inode) 1956 { 1957 inode->i_op = &fuse_common_inode_operations; 1958 } 1959 1960 void fuse_init_dir(struct inode *inode) 1961 { 1962 inode->i_op = &fuse_dir_inode_operations; 1963 inode->i_fop = &fuse_dir_operations; 1964 } 1965 1966 void fuse_init_symlink(struct inode *inode) 1967 { 1968 inode->i_op = &fuse_symlink_inode_operations; 1969 } 1970