1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* CacheFiles path walking and related routines 3 * 4 * Copyright (C) 2021 Red Hat, Inc. All Rights Reserved. 5 * Written by David Howells (dhowells@redhat.com) 6 */ 7 8 #include <linux/fs.h> 9 #include <linux/namei.h> 10 #include "internal.h" 11 12 /* 13 * Mark the backing file as being a cache file if it's not already in use. The 14 * mark tells the culling request command that it's not allowed to cull the 15 * file or directory. The caller must hold the inode lock. 16 */ 17 static bool __cachefiles_mark_inode_in_use(struct cachefiles_object *object, 18 struct dentry *dentry) 19 { 20 struct inode *inode = d_backing_inode(dentry); 21 bool can_use = false; 22 23 if (!(inode->i_flags & S_KERNEL_FILE)) { 24 inode->i_flags |= S_KERNEL_FILE; 25 trace_cachefiles_mark_active(object, inode); 26 can_use = true; 27 } else { 28 pr_notice("cachefiles: Inode already in use: %pd\n", dentry); 29 } 30 31 return can_use; 32 } 33 34 static bool cachefiles_mark_inode_in_use(struct cachefiles_object *object, 35 struct dentry *dentry) 36 { 37 struct inode *inode = d_backing_inode(dentry); 38 bool can_use; 39 40 inode_lock(inode); 41 can_use = __cachefiles_mark_inode_in_use(object, dentry); 42 inode_unlock(inode); 43 return can_use; 44 } 45 46 /* 47 * Unmark a backing inode. The caller must hold the inode lock. 48 */ 49 static void __cachefiles_unmark_inode_in_use(struct cachefiles_object *object, 50 struct dentry *dentry) 51 { 52 struct inode *inode = d_backing_inode(dentry); 53 54 inode->i_flags &= ~S_KERNEL_FILE; 55 trace_cachefiles_mark_inactive(object, inode); 56 } 57 58 /* 59 * Unmark a backing inode and tell cachefilesd that there's something that can 60 * be culled. 61 */ 62 void cachefiles_unmark_inode_in_use(struct cachefiles_object *object, 63 struct file *file) 64 { 65 struct cachefiles_cache *cache = object->volume->cache; 66 struct inode *inode = file_inode(file); 67 68 if (inode) { 69 inode_lock(inode); 70 __cachefiles_unmark_inode_in_use(object, file->f_path.dentry); 71 inode_unlock(inode); 72 73 if (!test_bit(CACHEFILES_OBJECT_USING_TMPFILE, &object->flags)) { 74 atomic_long_add(inode->i_blocks, &cache->b_released); 75 if (atomic_inc_return(&cache->f_released)) 76 cachefiles_state_changed(cache); 77 } 78 } 79 } 80 81 /* 82 * get a subdirectory 83 */ 84 struct dentry *cachefiles_get_directory(struct cachefiles_cache *cache, 85 struct dentry *dir, 86 const char *dirname, 87 bool *_is_new) 88 { 89 struct dentry *subdir; 90 struct path path; 91 int ret; 92 93 _enter(",,%s", dirname); 94 95 /* search the current directory for the element name */ 96 inode_lock_nested(d_inode(dir), I_MUTEX_PARENT); 97 98 retry: 99 ret = cachefiles_inject_read_error(); 100 if (ret == 0) 101 subdir = lookup_one_len(dirname, dir, strlen(dirname)); 102 else 103 subdir = ERR_PTR(ret); 104 if (IS_ERR(subdir)) { 105 trace_cachefiles_vfs_error(NULL, d_backing_inode(dir), 106 PTR_ERR(subdir), 107 cachefiles_trace_lookup_error); 108 if (PTR_ERR(subdir) == -ENOMEM) 109 goto nomem_d_alloc; 110 goto lookup_error; 111 } 112 113 _debug("subdir -> %pd %s", 114 subdir, d_backing_inode(subdir) ? "positive" : "negative"); 115 116 /* we need to create the subdir if it doesn't exist yet */ 117 if (d_is_negative(subdir)) { 118 ret = cachefiles_has_space(cache, 1, 0, 119 cachefiles_has_space_for_create); 120 if (ret < 0) 121 goto mkdir_error; 122 123 _debug("attempt mkdir"); 124 125 path.mnt = cache->mnt; 126 path.dentry = dir; 127 ret = security_path_mkdir(&path, subdir, 0700); 128 if (ret < 0) 129 goto mkdir_error; 130 ret = cachefiles_inject_write_error(); 131 if (ret == 0) 132 ret = vfs_mkdir(&init_user_ns, d_inode(dir), subdir, 0700); 133 if (ret < 0) { 134 trace_cachefiles_vfs_error(NULL, d_inode(dir), ret, 135 cachefiles_trace_mkdir_error); 136 goto mkdir_error; 137 } 138 139 if (unlikely(d_unhashed(subdir))) { 140 cachefiles_put_directory(subdir); 141 goto retry; 142 } 143 ASSERT(d_backing_inode(subdir)); 144 145 _debug("mkdir -> %pd{ino=%lu}", 146 subdir, d_backing_inode(subdir)->i_ino); 147 if (_is_new) 148 *_is_new = true; 149 } 150 151 /* Tell rmdir() it's not allowed to delete the subdir */ 152 inode_lock(d_inode(subdir)); 153 inode_unlock(d_inode(dir)); 154 155 if (!__cachefiles_mark_inode_in_use(NULL, subdir)) 156 goto mark_error; 157 158 inode_unlock(d_inode(subdir)); 159 160 /* we need to make sure the subdir is a directory */ 161 ASSERT(d_backing_inode(subdir)); 162 163 if (!d_can_lookup(subdir)) { 164 pr_err("%s is not a directory\n", dirname); 165 ret = -EIO; 166 goto check_error; 167 } 168 169 ret = -EPERM; 170 if (!(d_backing_inode(subdir)->i_opflags & IOP_XATTR) || 171 !d_backing_inode(subdir)->i_op->lookup || 172 !d_backing_inode(subdir)->i_op->mkdir || 173 !d_backing_inode(subdir)->i_op->rename || 174 !d_backing_inode(subdir)->i_op->rmdir || 175 !d_backing_inode(subdir)->i_op->unlink) 176 goto check_error; 177 178 _leave(" = [%lu]", d_backing_inode(subdir)->i_ino); 179 return subdir; 180 181 check_error: 182 cachefiles_put_directory(subdir); 183 _leave(" = %d [check]", ret); 184 return ERR_PTR(ret); 185 186 mark_error: 187 inode_unlock(d_inode(subdir)); 188 dput(subdir); 189 return ERR_PTR(-EBUSY); 190 191 mkdir_error: 192 inode_unlock(d_inode(dir)); 193 dput(subdir); 194 pr_err("mkdir %s failed with error %d\n", dirname, ret); 195 return ERR_PTR(ret); 196 197 lookup_error: 198 inode_unlock(d_inode(dir)); 199 ret = PTR_ERR(subdir); 200 pr_err("Lookup %s failed with error %d\n", dirname, ret); 201 return ERR_PTR(ret); 202 203 nomem_d_alloc: 204 inode_unlock(d_inode(dir)); 205 _leave(" = -ENOMEM"); 206 return ERR_PTR(-ENOMEM); 207 } 208 209 /* 210 * Put a subdirectory. 211 */ 212 void cachefiles_put_directory(struct dentry *dir) 213 { 214 if (dir) { 215 inode_lock(dir->d_inode); 216 __cachefiles_unmark_inode_in_use(NULL, dir); 217 inode_unlock(dir->d_inode); 218 dput(dir); 219 } 220 } 221 222 /* 223 * Remove a regular file from the cache. 224 */ 225 static int cachefiles_unlink(struct cachefiles_cache *cache, 226 struct cachefiles_object *object, 227 struct dentry *dir, struct dentry *dentry, 228 enum fscache_why_object_killed why) 229 { 230 struct path path = { 231 .mnt = cache->mnt, 232 .dentry = dir, 233 }; 234 int ret; 235 236 trace_cachefiles_unlink(object, dentry, why); 237 ret = security_path_unlink(&path, dentry); 238 if (ret < 0) { 239 cachefiles_io_error(cache, "Unlink security error"); 240 return ret; 241 } 242 243 ret = cachefiles_inject_remove_error(); 244 if (ret == 0) { 245 ret = vfs_unlink(&init_user_ns, d_backing_inode(dir), dentry, NULL); 246 if (ret == -EIO) 247 cachefiles_io_error(cache, "Unlink failed"); 248 } 249 if (ret != 0) 250 trace_cachefiles_vfs_error(object, d_backing_inode(dir), ret, 251 cachefiles_trace_unlink_error); 252 return ret; 253 } 254 255 /* 256 * Delete an object representation from the cache 257 * - File backed objects are unlinked 258 * - Directory backed objects are stuffed into the graveyard for userspace to 259 * delete 260 */ 261 int cachefiles_bury_object(struct cachefiles_cache *cache, 262 struct cachefiles_object *object, 263 struct dentry *dir, 264 struct dentry *rep, 265 enum fscache_why_object_killed why) 266 { 267 struct dentry *grave, *trap; 268 struct path path, path_to_graveyard; 269 char nbuffer[8 + 8 + 1]; 270 int ret; 271 272 _enter(",'%pd','%pd'", dir, rep); 273 274 if (rep->d_parent != dir) { 275 inode_unlock(d_inode(dir)); 276 _leave(" = -ESTALE"); 277 return -ESTALE; 278 } 279 280 /* non-directories can just be unlinked */ 281 if (!d_is_dir(rep)) { 282 dget(rep); /* Stop the dentry being negated if it's only pinned 283 * by a file struct. 284 */ 285 ret = cachefiles_unlink(cache, object, dir, rep, why); 286 dput(rep); 287 288 inode_unlock(d_inode(dir)); 289 _leave(" = %d", ret); 290 return ret; 291 } 292 293 /* directories have to be moved to the graveyard */ 294 _debug("move stale object to graveyard"); 295 inode_unlock(d_inode(dir)); 296 297 try_again: 298 /* first step is to make up a grave dentry in the graveyard */ 299 sprintf(nbuffer, "%08x%08x", 300 (uint32_t) ktime_get_real_seconds(), 301 (uint32_t) atomic_inc_return(&cache->gravecounter)); 302 303 /* do the multiway lock magic */ 304 trap = lock_rename(cache->graveyard, dir); 305 306 /* do some checks before getting the grave dentry */ 307 if (rep->d_parent != dir || IS_DEADDIR(d_inode(rep))) { 308 /* the entry was probably culled when we dropped the parent dir 309 * lock */ 310 unlock_rename(cache->graveyard, dir); 311 _leave(" = 0 [culled?]"); 312 return 0; 313 } 314 315 if (!d_can_lookup(cache->graveyard)) { 316 unlock_rename(cache->graveyard, dir); 317 cachefiles_io_error(cache, "Graveyard no longer a directory"); 318 return -EIO; 319 } 320 321 if (trap == rep) { 322 unlock_rename(cache->graveyard, dir); 323 cachefiles_io_error(cache, "May not make directory loop"); 324 return -EIO; 325 } 326 327 if (d_mountpoint(rep)) { 328 unlock_rename(cache->graveyard, dir); 329 cachefiles_io_error(cache, "Mountpoint in cache"); 330 return -EIO; 331 } 332 333 grave = lookup_one_len(nbuffer, cache->graveyard, strlen(nbuffer)); 334 if (IS_ERR(grave)) { 335 unlock_rename(cache->graveyard, dir); 336 trace_cachefiles_vfs_error(object, d_inode(cache->graveyard), 337 PTR_ERR(grave), 338 cachefiles_trace_lookup_error); 339 340 if (PTR_ERR(grave) == -ENOMEM) { 341 _leave(" = -ENOMEM"); 342 return -ENOMEM; 343 } 344 345 cachefiles_io_error(cache, "Lookup error %ld", PTR_ERR(grave)); 346 return -EIO; 347 } 348 349 if (d_is_positive(grave)) { 350 unlock_rename(cache->graveyard, dir); 351 dput(grave); 352 grave = NULL; 353 cond_resched(); 354 goto try_again; 355 } 356 357 if (d_mountpoint(grave)) { 358 unlock_rename(cache->graveyard, dir); 359 dput(grave); 360 cachefiles_io_error(cache, "Mountpoint in graveyard"); 361 return -EIO; 362 } 363 364 /* target should not be an ancestor of source */ 365 if (trap == grave) { 366 unlock_rename(cache->graveyard, dir); 367 dput(grave); 368 cachefiles_io_error(cache, "May not make directory loop"); 369 return -EIO; 370 } 371 372 /* attempt the rename */ 373 path.mnt = cache->mnt; 374 path.dentry = dir; 375 path_to_graveyard.mnt = cache->mnt; 376 path_to_graveyard.dentry = cache->graveyard; 377 ret = security_path_rename(&path, rep, &path_to_graveyard, grave, 0); 378 if (ret < 0) { 379 cachefiles_io_error(cache, "Rename security error %d", ret); 380 } else { 381 struct renamedata rd = { 382 .old_mnt_userns = &init_user_ns, 383 .old_dir = d_inode(dir), 384 .old_dentry = rep, 385 .new_mnt_userns = &init_user_ns, 386 .new_dir = d_inode(cache->graveyard), 387 .new_dentry = grave, 388 }; 389 trace_cachefiles_rename(object, rep, grave, why); 390 ret = cachefiles_inject_read_error(); 391 if (ret == 0) 392 ret = vfs_rename(&rd); 393 if (ret != 0) 394 trace_cachefiles_vfs_error(object, d_inode(dir), ret, 395 cachefiles_trace_rename_error); 396 if (ret != 0 && ret != -ENOMEM) 397 cachefiles_io_error(cache, 398 "Rename failed with error %d", ret); 399 } 400 401 __cachefiles_unmark_inode_in_use(object, rep); 402 unlock_rename(cache->graveyard, dir); 403 dput(grave); 404 _leave(" = 0"); 405 return 0; 406 } 407 408 /* 409 * Delete a cache file. 410 */ 411 int cachefiles_delete_object(struct cachefiles_object *object, 412 enum fscache_why_object_killed why) 413 { 414 struct cachefiles_volume *volume = object->volume; 415 struct dentry *dentry = object->file->f_path.dentry; 416 struct dentry *fan = volume->fanout[(u8)object->cookie->key_hash]; 417 int ret; 418 419 _enter(",OBJ%x{%pD}", object->debug_id, object->file); 420 421 /* Stop the dentry being negated if it's only pinned by a file struct. */ 422 dget(dentry); 423 424 inode_lock_nested(d_backing_inode(fan), I_MUTEX_PARENT); 425 ret = cachefiles_unlink(volume->cache, object, fan, dentry, why); 426 inode_unlock(d_backing_inode(fan)); 427 dput(dentry); 428 return ret; 429 } 430 431 /* 432 * Create a temporary file and leave it unattached and un-xattr'd until the 433 * time comes to discard the object from memory. 434 */ 435 struct file *cachefiles_create_tmpfile(struct cachefiles_object *object) 436 { 437 struct cachefiles_volume *volume = object->volume; 438 struct cachefiles_cache *cache = volume->cache; 439 const struct cred *saved_cred; 440 struct dentry *fan = volume->fanout[(u8)object->cookie->key_hash]; 441 struct file *file; 442 struct path path; 443 uint64_t ni_size = object->cookie->object_size; 444 long ret; 445 446 ni_size = round_up(ni_size, CACHEFILES_DIO_BLOCK_SIZE); 447 448 cachefiles_begin_secure(cache, &saved_cred); 449 450 path.mnt = cache->mnt; 451 ret = cachefiles_inject_write_error(); 452 if (ret == 0) 453 path.dentry = vfs_tmpfile(&init_user_ns, fan, S_IFREG, O_RDWR); 454 else 455 path.dentry = ERR_PTR(ret); 456 if (IS_ERR(path.dentry)) { 457 trace_cachefiles_vfs_error(object, d_inode(fan), PTR_ERR(path.dentry), 458 cachefiles_trace_tmpfile_error); 459 if (PTR_ERR(path.dentry) == -EIO) 460 cachefiles_io_error_obj(object, "Failed to create tmpfile"); 461 file = ERR_CAST(path.dentry); 462 goto out; 463 } 464 465 trace_cachefiles_tmpfile(object, d_backing_inode(path.dentry)); 466 467 if (!cachefiles_mark_inode_in_use(object, path.dentry)) { 468 file = ERR_PTR(-EBUSY); 469 goto out_dput; 470 } 471 472 if (ni_size > 0) { 473 trace_cachefiles_trunc(object, d_backing_inode(path.dentry), 0, ni_size, 474 cachefiles_trunc_expand_tmpfile); 475 ret = cachefiles_inject_write_error(); 476 if (ret == 0) 477 ret = vfs_truncate(&path, ni_size); 478 if (ret < 0) { 479 trace_cachefiles_vfs_error( 480 object, d_backing_inode(path.dentry), ret, 481 cachefiles_trace_trunc_error); 482 file = ERR_PTR(ret); 483 goto out_dput; 484 } 485 } 486 487 file = open_with_fake_path(&path, O_RDWR | O_LARGEFILE | O_DIRECT, 488 d_backing_inode(path.dentry), cache->cache_cred); 489 if (IS_ERR(file)) { 490 trace_cachefiles_vfs_error(object, d_backing_inode(path.dentry), 491 PTR_ERR(file), 492 cachefiles_trace_open_error); 493 goto out_dput; 494 } 495 if (unlikely(!file->f_op->read_iter) || 496 unlikely(!file->f_op->write_iter)) { 497 fput(file); 498 pr_notice("Cache does not support read_iter and write_iter\n"); 499 file = ERR_PTR(-EINVAL); 500 } 501 502 out_dput: 503 dput(path.dentry); 504 out: 505 cachefiles_end_secure(cache, saved_cred); 506 return file; 507 } 508 509 /* 510 * Create a new file. 511 */ 512 static bool cachefiles_create_file(struct cachefiles_object *object) 513 { 514 struct file *file; 515 int ret; 516 517 ret = cachefiles_has_space(object->volume->cache, 1, 0, 518 cachefiles_has_space_for_create); 519 if (ret < 0) 520 return false; 521 522 file = cachefiles_create_tmpfile(object); 523 if (IS_ERR(file)) 524 return false; 525 526 set_bit(FSCACHE_COOKIE_NEEDS_UPDATE, &object->cookie->flags); 527 set_bit(CACHEFILES_OBJECT_USING_TMPFILE, &object->flags); 528 _debug("create -> %pD{ino=%lu}", file, file_inode(file)->i_ino); 529 object->file = file; 530 return true; 531 } 532 533 /* 534 * Open an existing file, checking its attributes and replacing it if it is 535 * stale. 536 */ 537 static bool cachefiles_open_file(struct cachefiles_object *object, 538 struct dentry *dentry) 539 { 540 struct cachefiles_cache *cache = object->volume->cache; 541 struct file *file; 542 struct path path; 543 int ret; 544 545 _enter("%pd", dentry); 546 547 if (!cachefiles_mark_inode_in_use(object, dentry)) 548 return false; 549 550 /* We need to open a file interface onto a data file now as we can't do 551 * it on demand because writeback called from do_exit() sees 552 * current->fs == NULL - which breaks d_path() called from ext4 open. 553 */ 554 path.mnt = cache->mnt; 555 path.dentry = dentry; 556 file = open_with_fake_path(&path, O_RDWR | O_LARGEFILE | O_DIRECT, 557 d_backing_inode(dentry), cache->cache_cred); 558 if (IS_ERR(file)) { 559 trace_cachefiles_vfs_error(object, d_backing_inode(dentry), 560 PTR_ERR(file), 561 cachefiles_trace_open_error); 562 goto error; 563 } 564 565 if (unlikely(!file->f_op->read_iter) || 566 unlikely(!file->f_op->write_iter)) { 567 pr_notice("Cache does not support read_iter and write_iter\n"); 568 goto error_fput; 569 } 570 _debug("file -> %pd positive", dentry); 571 572 ret = cachefiles_check_auxdata(object, file); 573 if (ret < 0) 574 goto check_failed; 575 576 object->file = file; 577 578 /* Always update the atime on an object we've just looked up (this is 579 * used to keep track of culling, and atimes are only updated by read, 580 * write and readdir but not lookup or open). 581 */ 582 touch_atime(&file->f_path); 583 dput(dentry); 584 return true; 585 586 check_failed: 587 fscache_cookie_lookup_negative(object->cookie); 588 cachefiles_unmark_inode_in_use(object, file); 589 if (ret == -ESTALE) { 590 fput(file); 591 dput(dentry); 592 return cachefiles_create_file(object); 593 } 594 error_fput: 595 fput(file); 596 error: 597 dput(dentry); 598 return false; 599 } 600 601 /* 602 * walk from the parent object to the child object through the backing 603 * filesystem, creating directories as we go 604 */ 605 bool cachefiles_look_up_object(struct cachefiles_object *object) 606 { 607 struct cachefiles_volume *volume = object->volume; 608 struct dentry *dentry, *fan = volume->fanout[(u8)object->cookie->key_hash]; 609 int ret; 610 611 _enter("OBJ%x,%s,", object->debug_id, object->d_name); 612 613 /* Look up path "cache/vol/fanout/file". */ 614 ret = cachefiles_inject_read_error(); 615 if (ret == 0) 616 dentry = lookup_positive_unlocked(object->d_name, fan, 617 object->d_name_len); 618 else 619 dentry = ERR_PTR(ret); 620 trace_cachefiles_lookup(object, dentry); 621 if (IS_ERR(dentry)) { 622 if (dentry == ERR_PTR(-ENOENT)) 623 goto new_file; 624 if (dentry == ERR_PTR(-EIO)) 625 cachefiles_io_error_obj(object, "Lookup failed"); 626 return false; 627 } 628 629 if (!d_is_reg(dentry)) { 630 pr_err("%pd is not a file\n", dentry); 631 inode_lock_nested(d_inode(fan), I_MUTEX_PARENT); 632 ret = cachefiles_bury_object(volume->cache, object, fan, dentry, 633 FSCACHE_OBJECT_IS_WEIRD); 634 dput(dentry); 635 if (ret < 0) 636 return false; 637 goto new_file; 638 } 639 640 if (!cachefiles_open_file(object, dentry)) 641 return false; 642 643 _leave(" = t [%lu]", file_inode(object->file)->i_ino); 644 return true; 645 646 new_file: 647 fscache_cookie_lookup_negative(object->cookie); 648 return cachefiles_create_file(object); 649 } 650 651 /* 652 * Attempt to link a temporary file into its rightful place in the cache. 653 */ 654 bool cachefiles_commit_tmpfile(struct cachefiles_cache *cache, 655 struct cachefiles_object *object) 656 { 657 struct cachefiles_volume *volume = object->volume; 658 struct dentry *dentry, *fan = volume->fanout[(u8)object->cookie->key_hash]; 659 bool success = false; 660 int ret; 661 662 _enter(",%pD", object->file); 663 664 inode_lock_nested(d_inode(fan), I_MUTEX_PARENT); 665 ret = cachefiles_inject_read_error(); 666 if (ret == 0) 667 dentry = lookup_one_len(object->d_name, fan, object->d_name_len); 668 else 669 dentry = ERR_PTR(ret); 670 if (IS_ERR(dentry)) { 671 trace_cachefiles_vfs_error(object, d_inode(fan), PTR_ERR(dentry), 672 cachefiles_trace_lookup_error); 673 _debug("lookup fail %ld", PTR_ERR(dentry)); 674 goto out_unlock; 675 } 676 677 if (!d_is_negative(dentry)) { 678 if (d_backing_inode(dentry) == file_inode(object->file)) { 679 success = true; 680 goto out_dput; 681 } 682 683 ret = cachefiles_unlink(volume->cache, object, fan, dentry, 684 FSCACHE_OBJECT_IS_STALE); 685 if (ret < 0) 686 goto out_dput; 687 688 dput(dentry); 689 ret = cachefiles_inject_read_error(); 690 if (ret == 0) 691 dentry = lookup_one_len(object->d_name, fan, object->d_name_len); 692 else 693 dentry = ERR_PTR(ret); 694 if (IS_ERR(dentry)) { 695 trace_cachefiles_vfs_error(object, d_inode(fan), PTR_ERR(dentry), 696 cachefiles_trace_lookup_error); 697 _debug("lookup fail %ld", PTR_ERR(dentry)); 698 goto out_unlock; 699 } 700 } 701 702 ret = cachefiles_inject_read_error(); 703 if (ret == 0) 704 ret = vfs_link(object->file->f_path.dentry, &init_user_ns, 705 d_inode(fan), dentry, NULL); 706 if (ret < 0) { 707 trace_cachefiles_vfs_error(object, d_inode(fan), ret, 708 cachefiles_trace_link_error); 709 _debug("link fail %d", ret); 710 } else { 711 trace_cachefiles_link(object, file_inode(object->file)); 712 spin_lock(&object->lock); 713 /* TODO: Do we want to switch the file pointer to the new dentry? */ 714 clear_bit(CACHEFILES_OBJECT_USING_TMPFILE, &object->flags); 715 spin_unlock(&object->lock); 716 success = true; 717 } 718 719 out_dput: 720 dput(dentry); 721 out_unlock: 722 inode_unlock(d_inode(fan)); 723 _leave(" = %u", success); 724 return success; 725 } 726 727 /* 728 * Look up an inode to be checked or culled. Return -EBUSY if the inode is 729 * marked in use. 730 */ 731 static struct dentry *cachefiles_lookup_for_cull(struct cachefiles_cache *cache, 732 struct dentry *dir, 733 char *filename) 734 { 735 struct dentry *victim; 736 int ret = -ENOENT; 737 738 inode_lock_nested(d_inode(dir), I_MUTEX_PARENT); 739 740 victim = lookup_one_len(filename, dir, strlen(filename)); 741 if (IS_ERR(victim)) 742 goto lookup_error; 743 if (d_is_negative(victim)) 744 goto lookup_put; 745 if (d_inode(victim)->i_flags & S_KERNEL_FILE) 746 goto lookup_busy; 747 return victim; 748 749 lookup_busy: 750 ret = -EBUSY; 751 lookup_put: 752 inode_unlock(d_inode(dir)); 753 dput(victim); 754 return ERR_PTR(ret); 755 756 lookup_error: 757 inode_unlock(d_inode(dir)); 758 ret = PTR_ERR(victim); 759 if (ret == -ENOENT) 760 return ERR_PTR(-ESTALE); /* Probably got retired by the netfs */ 761 762 if (ret == -EIO) { 763 cachefiles_io_error(cache, "Lookup failed"); 764 } else if (ret != -ENOMEM) { 765 pr_err("Internal error: %d\n", ret); 766 ret = -EIO; 767 } 768 769 return ERR_PTR(ret); 770 } 771 772 /* 773 * Cull an object if it's not in use 774 * - called only by cache manager daemon 775 */ 776 int cachefiles_cull(struct cachefiles_cache *cache, struct dentry *dir, 777 char *filename) 778 { 779 struct dentry *victim; 780 struct inode *inode; 781 int ret; 782 783 _enter(",%pd/,%s", dir, filename); 784 785 victim = cachefiles_lookup_for_cull(cache, dir, filename); 786 if (IS_ERR(victim)) 787 return PTR_ERR(victim); 788 789 /* check to see if someone is using this object */ 790 inode = d_inode(victim); 791 inode_lock(inode); 792 if (inode->i_flags & S_KERNEL_FILE) { 793 ret = -EBUSY; 794 } else { 795 /* Stop the cache from picking it back up */ 796 inode->i_flags |= S_KERNEL_FILE; 797 ret = 0; 798 } 799 inode_unlock(inode); 800 if (ret < 0) 801 goto error_unlock; 802 803 ret = cachefiles_bury_object(cache, NULL, dir, victim, 804 FSCACHE_OBJECT_WAS_CULLED); 805 if (ret < 0) 806 goto error; 807 808 fscache_count_culled(); 809 dput(victim); 810 _leave(" = 0"); 811 return 0; 812 813 error_unlock: 814 inode_unlock(d_inode(dir)); 815 error: 816 dput(victim); 817 if (ret == -ENOENT) 818 return -ESTALE; /* Probably got retired by the netfs */ 819 820 if (ret != -ENOMEM) { 821 pr_err("Internal error: %d\n", ret); 822 ret = -EIO; 823 } 824 825 _leave(" = %d", ret); 826 return ret; 827 } 828 829 /* 830 * Find out if an object is in use or not 831 * - called only by cache manager daemon 832 * - returns -EBUSY or 0 to indicate whether an object is in use or not 833 */ 834 int cachefiles_check_in_use(struct cachefiles_cache *cache, struct dentry *dir, 835 char *filename) 836 { 837 struct dentry *victim; 838 int ret = 0; 839 840 victim = cachefiles_lookup_for_cull(cache, dir, filename); 841 if (IS_ERR(victim)) 842 return PTR_ERR(victim); 843 844 inode_unlock(d_inode(dir)); 845 dput(victim); 846 return ret; 847 } 848