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