1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * fs/f2fs/namei.c 4 * 5 * Copyright (c) 2012 Samsung Electronics Co., Ltd. 6 * http://www.samsung.com/ 7 */ 8 #include <linux/fs.h> 9 #include <linux/f2fs_fs.h> 10 #include <linux/pagemap.h> 11 #include <linux/sched.h> 12 #include <linux/ctype.h> 13 #include <linux/random.h> 14 #include <linux/dcache.h> 15 #include <linux/namei.h> 16 #include <linux/quotaops.h> 17 18 #include "f2fs.h" 19 #include "node.h" 20 #include "segment.h" 21 #include "xattr.h" 22 #include "acl.h" 23 #include <trace/events/f2fs.h> 24 25 static struct inode *f2fs_new_inode(struct inode *dir, umode_t mode) 26 { 27 struct f2fs_sb_info *sbi = F2FS_I_SB(dir); 28 nid_t ino; 29 struct inode *inode; 30 bool nid_free = false; 31 int xattr_size = 0; 32 int err; 33 34 inode = new_inode(dir->i_sb); 35 if (!inode) 36 return ERR_PTR(-ENOMEM); 37 38 f2fs_lock_op(sbi); 39 if (!f2fs_alloc_nid(sbi, &ino)) { 40 f2fs_unlock_op(sbi); 41 err = -ENOSPC; 42 goto fail; 43 } 44 f2fs_unlock_op(sbi); 45 46 nid_free = true; 47 48 inode_init_owner(inode, dir, mode); 49 50 inode->i_ino = ino; 51 inode->i_blocks = 0; 52 inode->i_mtime = inode->i_atime = inode->i_ctime = current_time(inode); 53 F2FS_I(inode)->i_crtime = inode->i_mtime; 54 inode->i_generation = prandom_u32(); 55 56 if (S_ISDIR(inode->i_mode)) 57 F2FS_I(inode)->i_current_depth = 1; 58 59 err = insert_inode_locked(inode); 60 if (err) { 61 err = -EINVAL; 62 goto fail; 63 } 64 65 if (f2fs_sb_has_project_quota(sbi) && 66 (F2FS_I(dir)->i_flags & F2FS_PROJINHERIT_FL)) 67 F2FS_I(inode)->i_projid = F2FS_I(dir)->i_projid; 68 else 69 F2FS_I(inode)->i_projid = make_kprojid(&init_user_ns, 70 F2FS_DEF_PROJID); 71 72 err = dquot_initialize(inode); 73 if (err) 74 goto fail_drop; 75 76 set_inode_flag(inode, FI_NEW_INODE); 77 78 /* If the directory encrypted, then we should encrypt the inode. */ 79 if ((IS_ENCRYPTED(dir) || DUMMY_ENCRYPTION_ENABLED(sbi)) && 80 f2fs_may_encrypt(inode)) 81 f2fs_set_encrypted_inode(inode); 82 83 if (f2fs_sb_has_extra_attr(sbi)) { 84 set_inode_flag(inode, FI_EXTRA_ATTR); 85 F2FS_I(inode)->i_extra_isize = F2FS_TOTAL_EXTRA_ATTR_SIZE; 86 } 87 88 if (test_opt(sbi, INLINE_XATTR)) 89 set_inode_flag(inode, FI_INLINE_XATTR); 90 91 if (test_opt(sbi, INLINE_DATA) && f2fs_may_inline_data(inode)) 92 set_inode_flag(inode, FI_INLINE_DATA); 93 if (f2fs_may_inline_dentry(inode)) 94 set_inode_flag(inode, FI_INLINE_DENTRY); 95 96 if (f2fs_sb_has_flexible_inline_xattr(sbi)) { 97 f2fs_bug_on(sbi, !f2fs_has_extra_attr(inode)); 98 if (f2fs_has_inline_xattr(inode)) 99 xattr_size = F2FS_OPTION(sbi).inline_xattr_size; 100 /* Otherwise, will be 0 */ 101 } else if (f2fs_has_inline_xattr(inode) || 102 f2fs_has_inline_dentry(inode)) { 103 xattr_size = DEFAULT_INLINE_XATTR_ADDRS; 104 } 105 F2FS_I(inode)->i_inline_xattr_size = xattr_size; 106 107 f2fs_init_extent_tree(inode, NULL); 108 109 stat_inc_inline_xattr(inode); 110 stat_inc_inline_inode(inode); 111 stat_inc_inline_dir(inode); 112 113 F2FS_I(inode)->i_flags = 114 f2fs_mask_flags(mode, F2FS_I(dir)->i_flags & F2FS_FL_INHERITED); 115 116 if (S_ISDIR(inode->i_mode)) 117 F2FS_I(inode)->i_flags |= F2FS_INDEX_FL; 118 119 if (F2FS_I(inode)->i_flags & F2FS_PROJINHERIT_FL) 120 set_inode_flag(inode, FI_PROJ_INHERIT); 121 122 f2fs_set_inode_flags(inode); 123 124 trace_f2fs_new_inode(inode, 0); 125 return inode; 126 127 fail: 128 trace_f2fs_new_inode(inode, err); 129 make_bad_inode(inode); 130 if (nid_free) 131 set_inode_flag(inode, FI_FREE_NID); 132 iput(inode); 133 return ERR_PTR(err); 134 fail_drop: 135 trace_f2fs_new_inode(inode, err); 136 dquot_drop(inode); 137 inode->i_flags |= S_NOQUOTA; 138 if (nid_free) 139 set_inode_flag(inode, FI_FREE_NID); 140 clear_nlink(inode); 141 unlock_new_inode(inode); 142 iput(inode); 143 return ERR_PTR(err); 144 } 145 146 static int is_extension_exist(const unsigned char *s, const char *sub) 147 { 148 size_t slen = strlen(s); 149 size_t sublen = strlen(sub); 150 int i; 151 152 /* 153 * filename format of multimedia file should be defined as: 154 * "filename + '.' + extension + (optional: '.' + temp extension)". 155 */ 156 if (slen < sublen + 2) 157 return 0; 158 159 for (i = 1; i < slen - sublen; i++) { 160 if (s[i] != '.') 161 continue; 162 if (!strncasecmp(s + i + 1, sub, sublen)) 163 return 1; 164 } 165 166 return 0; 167 } 168 169 /* 170 * Set multimedia files as cold files for hot/cold data separation 171 */ 172 static inline void set_file_temperature(struct f2fs_sb_info *sbi, struct inode *inode, 173 const unsigned char *name) 174 { 175 __u8 (*extlist)[F2FS_EXTENSION_LEN] = sbi->raw_super->extension_list; 176 int i, cold_count, hot_count; 177 178 down_read(&sbi->sb_lock); 179 180 cold_count = le32_to_cpu(sbi->raw_super->extension_count); 181 hot_count = sbi->raw_super->hot_ext_count; 182 183 for (i = 0; i < cold_count + hot_count; i++) { 184 if (is_extension_exist(name, extlist[i])) 185 break; 186 } 187 188 up_read(&sbi->sb_lock); 189 190 if (i == cold_count + hot_count) 191 return; 192 193 if (i < cold_count) 194 file_set_cold(inode); 195 else 196 file_set_hot(inode); 197 } 198 199 int f2fs_update_extension_list(struct f2fs_sb_info *sbi, const char *name, 200 bool hot, bool set) 201 { 202 __u8 (*extlist)[F2FS_EXTENSION_LEN] = sbi->raw_super->extension_list; 203 int cold_count = le32_to_cpu(sbi->raw_super->extension_count); 204 int hot_count = sbi->raw_super->hot_ext_count; 205 int total_count = cold_count + hot_count; 206 int start, count; 207 int i; 208 209 if (set) { 210 if (total_count == F2FS_MAX_EXTENSION) 211 return -EINVAL; 212 } else { 213 if (!hot && !cold_count) 214 return -EINVAL; 215 if (hot && !hot_count) 216 return -EINVAL; 217 } 218 219 if (hot) { 220 start = cold_count; 221 count = total_count; 222 } else { 223 start = 0; 224 count = cold_count; 225 } 226 227 for (i = start; i < count; i++) { 228 if (strcmp(name, extlist[i])) 229 continue; 230 231 if (set) 232 return -EINVAL; 233 234 memcpy(extlist[i], extlist[i + 1], 235 F2FS_EXTENSION_LEN * (total_count - i - 1)); 236 memset(extlist[total_count - 1], 0, F2FS_EXTENSION_LEN); 237 if (hot) 238 sbi->raw_super->hot_ext_count = hot_count - 1; 239 else 240 sbi->raw_super->extension_count = 241 cpu_to_le32(cold_count - 1); 242 return 0; 243 } 244 245 if (!set) 246 return -EINVAL; 247 248 if (hot) { 249 memcpy(extlist[count], name, strlen(name)); 250 sbi->raw_super->hot_ext_count = hot_count + 1; 251 } else { 252 char buf[F2FS_MAX_EXTENSION][F2FS_EXTENSION_LEN]; 253 254 memcpy(buf, &extlist[cold_count], 255 F2FS_EXTENSION_LEN * hot_count); 256 memset(extlist[cold_count], 0, F2FS_EXTENSION_LEN); 257 memcpy(extlist[cold_count], name, strlen(name)); 258 memcpy(&extlist[cold_count + 1], buf, 259 F2FS_EXTENSION_LEN * hot_count); 260 sbi->raw_super->extension_count = cpu_to_le32(cold_count + 1); 261 } 262 return 0; 263 } 264 265 static int f2fs_create(struct inode *dir, struct dentry *dentry, umode_t mode, 266 bool excl) 267 { 268 struct f2fs_sb_info *sbi = F2FS_I_SB(dir); 269 struct inode *inode; 270 nid_t ino = 0; 271 int err; 272 273 if (unlikely(f2fs_cp_error(sbi))) 274 return -EIO; 275 err = f2fs_is_checkpoint_ready(sbi); 276 if (err) 277 return err; 278 279 err = dquot_initialize(dir); 280 if (err) 281 return err; 282 283 inode = f2fs_new_inode(dir, mode); 284 if (IS_ERR(inode)) 285 return PTR_ERR(inode); 286 287 if (!test_opt(sbi, DISABLE_EXT_IDENTIFY)) 288 set_file_temperature(sbi, inode, dentry->d_name.name); 289 290 inode->i_op = &f2fs_file_inode_operations; 291 inode->i_fop = &f2fs_file_operations; 292 inode->i_mapping->a_ops = &f2fs_dblock_aops; 293 ino = inode->i_ino; 294 295 f2fs_lock_op(sbi); 296 err = f2fs_add_link(dentry, inode); 297 if (err) 298 goto out; 299 f2fs_unlock_op(sbi); 300 301 f2fs_alloc_nid_done(sbi, ino); 302 303 d_instantiate_new(dentry, inode); 304 305 if (IS_DIRSYNC(dir)) 306 f2fs_sync_fs(sbi->sb, 1); 307 308 f2fs_balance_fs(sbi, true); 309 return 0; 310 out: 311 f2fs_handle_failed_inode(inode); 312 return err; 313 } 314 315 static int f2fs_link(struct dentry *old_dentry, struct inode *dir, 316 struct dentry *dentry) 317 { 318 struct inode *inode = d_inode(old_dentry); 319 struct f2fs_sb_info *sbi = F2FS_I_SB(dir); 320 int err; 321 322 if (unlikely(f2fs_cp_error(sbi))) 323 return -EIO; 324 err = f2fs_is_checkpoint_ready(sbi); 325 if (err) 326 return err; 327 328 err = fscrypt_prepare_link(old_dentry, dir, dentry); 329 if (err) 330 return err; 331 332 if (is_inode_flag_set(dir, FI_PROJ_INHERIT) && 333 (!projid_eq(F2FS_I(dir)->i_projid, 334 F2FS_I(old_dentry->d_inode)->i_projid))) 335 return -EXDEV; 336 337 err = dquot_initialize(dir); 338 if (err) 339 return err; 340 341 f2fs_balance_fs(sbi, true); 342 343 inode->i_ctime = current_time(inode); 344 ihold(inode); 345 346 set_inode_flag(inode, FI_INC_LINK); 347 f2fs_lock_op(sbi); 348 err = f2fs_add_link(dentry, inode); 349 if (err) 350 goto out; 351 f2fs_unlock_op(sbi); 352 353 d_instantiate(dentry, inode); 354 355 if (IS_DIRSYNC(dir)) 356 f2fs_sync_fs(sbi->sb, 1); 357 return 0; 358 out: 359 clear_inode_flag(inode, FI_INC_LINK); 360 iput(inode); 361 f2fs_unlock_op(sbi); 362 return err; 363 } 364 365 struct dentry *f2fs_get_parent(struct dentry *child) 366 { 367 struct qstr dotdot = QSTR_INIT("..", 2); 368 struct page *page; 369 unsigned long ino = f2fs_inode_by_name(d_inode(child), &dotdot, &page); 370 if (!ino) { 371 if (IS_ERR(page)) 372 return ERR_CAST(page); 373 return ERR_PTR(-ENOENT); 374 } 375 return d_obtain_alias(f2fs_iget(child->d_sb, ino)); 376 } 377 378 static int __recover_dot_dentries(struct inode *dir, nid_t pino) 379 { 380 struct f2fs_sb_info *sbi = F2FS_I_SB(dir); 381 struct qstr dot = QSTR_INIT(".", 1); 382 struct qstr dotdot = QSTR_INIT("..", 2); 383 struct f2fs_dir_entry *de; 384 struct page *page; 385 int err = 0; 386 387 if (f2fs_readonly(sbi->sb)) { 388 f2fs_msg(sbi->sb, KERN_INFO, 389 "skip recovering inline_dots inode (ino:%lu, pino:%u) " 390 "in readonly mountpoint", dir->i_ino, pino); 391 return 0; 392 } 393 394 err = dquot_initialize(dir); 395 if (err) 396 return err; 397 398 f2fs_balance_fs(sbi, true); 399 400 f2fs_lock_op(sbi); 401 402 de = f2fs_find_entry(dir, &dot, &page); 403 if (de) { 404 f2fs_put_page(page, 0); 405 } else if (IS_ERR(page)) { 406 err = PTR_ERR(page); 407 goto out; 408 } else { 409 err = f2fs_do_add_link(dir, &dot, NULL, dir->i_ino, S_IFDIR); 410 if (err) 411 goto out; 412 } 413 414 de = f2fs_find_entry(dir, &dotdot, &page); 415 if (de) 416 f2fs_put_page(page, 0); 417 else if (IS_ERR(page)) 418 err = PTR_ERR(page); 419 else 420 err = f2fs_do_add_link(dir, &dotdot, NULL, pino, S_IFDIR); 421 out: 422 if (!err) 423 clear_inode_flag(dir, FI_INLINE_DOTS); 424 425 f2fs_unlock_op(sbi); 426 return err; 427 } 428 429 static struct dentry *f2fs_lookup(struct inode *dir, struct dentry *dentry, 430 unsigned int flags) 431 { 432 struct inode *inode = NULL; 433 struct f2fs_dir_entry *de; 434 struct page *page; 435 struct dentry *new; 436 nid_t ino = -1; 437 int err = 0; 438 unsigned int root_ino = F2FS_ROOT_INO(F2FS_I_SB(dir)); 439 440 trace_f2fs_lookup_start(dir, dentry, flags); 441 442 err = fscrypt_prepare_lookup(dir, dentry, flags); 443 if (err) 444 goto out; 445 446 if (dentry->d_name.len > F2FS_NAME_LEN) { 447 err = -ENAMETOOLONG; 448 goto out; 449 } 450 451 de = f2fs_find_entry(dir, &dentry->d_name, &page); 452 if (!de) { 453 if (IS_ERR(page)) { 454 err = PTR_ERR(page); 455 goto out; 456 } 457 goto out_splice; 458 } 459 460 ino = le32_to_cpu(de->ino); 461 f2fs_put_page(page, 0); 462 463 inode = f2fs_iget(dir->i_sb, ino); 464 if (IS_ERR(inode)) { 465 err = PTR_ERR(inode); 466 goto out; 467 } 468 469 if ((dir->i_ino == root_ino) && f2fs_has_inline_dots(dir)) { 470 err = __recover_dot_dentries(dir, root_ino); 471 if (err) 472 goto out_iput; 473 } 474 475 if (f2fs_has_inline_dots(inode)) { 476 err = __recover_dot_dentries(inode, dir->i_ino); 477 if (err) 478 goto out_iput; 479 } 480 if (IS_ENCRYPTED(dir) && 481 (S_ISDIR(inode->i_mode) || S_ISLNK(inode->i_mode)) && 482 !fscrypt_has_permitted_context(dir, inode)) { 483 f2fs_msg(inode->i_sb, KERN_WARNING, 484 "Inconsistent encryption contexts: %lu/%lu", 485 dir->i_ino, inode->i_ino); 486 err = -EPERM; 487 goto out_iput; 488 } 489 out_splice: 490 new = d_splice_alias(inode, dentry); 491 if (IS_ERR(new)) 492 err = PTR_ERR(new); 493 trace_f2fs_lookup_end(dir, dentry, ino, err); 494 return new; 495 out_iput: 496 iput(inode); 497 out: 498 trace_f2fs_lookup_end(dir, dentry, ino, err); 499 return ERR_PTR(err); 500 } 501 502 static int f2fs_unlink(struct inode *dir, struct dentry *dentry) 503 { 504 struct f2fs_sb_info *sbi = F2FS_I_SB(dir); 505 struct inode *inode = d_inode(dentry); 506 struct f2fs_dir_entry *de; 507 struct page *page; 508 int err = -ENOENT; 509 510 trace_f2fs_unlink_enter(dir, dentry); 511 512 if (unlikely(f2fs_cp_error(sbi))) 513 return -EIO; 514 515 err = dquot_initialize(dir); 516 if (err) 517 return err; 518 err = dquot_initialize(inode); 519 if (err) 520 return err; 521 522 de = f2fs_find_entry(dir, &dentry->d_name, &page); 523 if (!de) { 524 if (IS_ERR(page)) 525 err = PTR_ERR(page); 526 goto fail; 527 } 528 529 f2fs_balance_fs(sbi, true); 530 531 f2fs_lock_op(sbi); 532 err = f2fs_acquire_orphan_inode(sbi); 533 if (err) { 534 f2fs_unlock_op(sbi); 535 f2fs_put_page(page, 0); 536 goto fail; 537 } 538 f2fs_delete_entry(de, page, dir, inode); 539 f2fs_unlock_op(sbi); 540 541 if (IS_DIRSYNC(dir)) 542 f2fs_sync_fs(sbi->sb, 1); 543 fail: 544 trace_f2fs_unlink_exit(inode, err); 545 return err; 546 } 547 548 static const char *f2fs_get_link(struct dentry *dentry, 549 struct inode *inode, 550 struct delayed_call *done) 551 { 552 const char *link = page_get_link(dentry, inode, done); 553 if (!IS_ERR(link) && !*link) { 554 /* this is broken symlink case */ 555 do_delayed_call(done); 556 clear_delayed_call(done); 557 link = ERR_PTR(-ENOENT); 558 } 559 return link; 560 } 561 562 static int f2fs_symlink(struct inode *dir, struct dentry *dentry, 563 const char *symname) 564 { 565 struct f2fs_sb_info *sbi = F2FS_I_SB(dir); 566 struct inode *inode; 567 size_t len = strlen(symname); 568 struct fscrypt_str disk_link; 569 int err; 570 571 if (unlikely(f2fs_cp_error(sbi))) 572 return -EIO; 573 err = f2fs_is_checkpoint_ready(sbi); 574 if (err) 575 return err; 576 577 err = fscrypt_prepare_symlink(dir, symname, len, dir->i_sb->s_blocksize, 578 &disk_link); 579 if (err) 580 return err; 581 582 err = dquot_initialize(dir); 583 if (err) 584 return err; 585 586 inode = f2fs_new_inode(dir, S_IFLNK | S_IRWXUGO); 587 if (IS_ERR(inode)) 588 return PTR_ERR(inode); 589 590 if (IS_ENCRYPTED(inode)) 591 inode->i_op = &f2fs_encrypted_symlink_inode_operations; 592 else 593 inode->i_op = &f2fs_symlink_inode_operations; 594 inode_nohighmem(inode); 595 inode->i_mapping->a_ops = &f2fs_dblock_aops; 596 597 f2fs_lock_op(sbi); 598 err = f2fs_add_link(dentry, inode); 599 if (err) 600 goto out_f2fs_handle_failed_inode; 601 f2fs_unlock_op(sbi); 602 f2fs_alloc_nid_done(sbi, inode->i_ino); 603 604 err = fscrypt_encrypt_symlink(inode, symname, len, &disk_link); 605 if (err) 606 goto err_out; 607 608 err = page_symlink(inode, disk_link.name, disk_link.len); 609 610 err_out: 611 d_instantiate_new(dentry, inode); 612 613 /* 614 * Let's flush symlink data in order to avoid broken symlink as much as 615 * possible. Nevertheless, fsyncing is the best way, but there is no 616 * way to get a file descriptor in order to flush that. 617 * 618 * Note that, it needs to do dir->fsync to make this recoverable. 619 * If the symlink path is stored into inline_data, there is no 620 * performance regression. 621 */ 622 if (!err) { 623 filemap_write_and_wait_range(inode->i_mapping, 0, 624 disk_link.len - 1); 625 626 if (IS_DIRSYNC(dir)) 627 f2fs_sync_fs(sbi->sb, 1); 628 } else { 629 f2fs_unlink(dir, dentry); 630 } 631 632 f2fs_balance_fs(sbi, true); 633 goto out_free_encrypted_link; 634 635 out_f2fs_handle_failed_inode: 636 f2fs_handle_failed_inode(inode); 637 out_free_encrypted_link: 638 if (disk_link.name != (unsigned char *)symname) 639 kvfree(disk_link.name); 640 return err; 641 } 642 643 static int f2fs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode) 644 { 645 struct f2fs_sb_info *sbi = F2FS_I_SB(dir); 646 struct inode *inode; 647 int err; 648 649 if (unlikely(f2fs_cp_error(sbi))) 650 return -EIO; 651 652 err = dquot_initialize(dir); 653 if (err) 654 return err; 655 656 inode = f2fs_new_inode(dir, S_IFDIR | mode); 657 if (IS_ERR(inode)) 658 return PTR_ERR(inode); 659 660 inode->i_op = &f2fs_dir_inode_operations; 661 inode->i_fop = &f2fs_dir_operations; 662 inode->i_mapping->a_ops = &f2fs_dblock_aops; 663 inode_nohighmem(inode); 664 665 set_inode_flag(inode, FI_INC_LINK); 666 f2fs_lock_op(sbi); 667 err = f2fs_add_link(dentry, inode); 668 if (err) 669 goto out_fail; 670 f2fs_unlock_op(sbi); 671 672 f2fs_alloc_nid_done(sbi, inode->i_ino); 673 674 d_instantiate_new(dentry, inode); 675 676 if (IS_DIRSYNC(dir)) 677 f2fs_sync_fs(sbi->sb, 1); 678 679 f2fs_balance_fs(sbi, true); 680 return 0; 681 682 out_fail: 683 clear_inode_flag(inode, FI_INC_LINK); 684 f2fs_handle_failed_inode(inode); 685 return err; 686 } 687 688 static int f2fs_rmdir(struct inode *dir, struct dentry *dentry) 689 { 690 struct inode *inode = d_inode(dentry); 691 if (f2fs_empty_dir(inode)) 692 return f2fs_unlink(dir, dentry); 693 return -ENOTEMPTY; 694 } 695 696 static int f2fs_mknod(struct inode *dir, struct dentry *dentry, 697 umode_t mode, dev_t rdev) 698 { 699 struct f2fs_sb_info *sbi = F2FS_I_SB(dir); 700 struct inode *inode; 701 int err = 0; 702 703 if (unlikely(f2fs_cp_error(sbi))) 704 return -EIO; 705 err = f2fs_is_checkpoint_ready(sbi); 706 if (err) 707 return err; 708 709 err = dquot_initialize(dir); 710 if (err) 711 return err; 712 713 inode = f2fs_new_inode(dir, mode); 714 if (IS_ERR(inode)) 715 return PTR_ERR(inode); 716 717 init_special_inode(inode, inode->i_mode, rdev); 718 inode->i_op = &f2fs_special_inode_operations; 719 720 f2fs_lock_op(sbi); 721 err = f2fs_add_link(dentry, inode); 722 if (err) 723 goto out; 724 f2fs_unlock_op(sbi); 725 726 f2fs_alloc_nid_done(sbi, inode->i_ino); 727 728 d_instantiate_new(dentry, inode); 729 730 if (IS_DIRSYNC(dir)) 731 f2fs_sync_fs(sbi->sb, 1); 732 733 f2fs_balance_fs(sbi, true); 734 return 0; 735 out: 736 f2fs_handle_failed_inode(inode); 737 return err; 738 } 739 740 static int __f2fs_tmpfile(struct inode *dir, struct dentry *dentry, 741 umode_t mode, struct inode **whiteout) 742 { 743 struct f2fs_sb_info *sbi = F2FS_I_SB(dir); 744 struct inode *inode; 745 int err; 746 747 err = dquot_initialize(dir); 748 if (err) 749 return err; 750 751 inode = f2fs_new_inode(dir, mode); 752 if (IS_ERR(inode)) 753 return PTR_ERR(inode); 754 755 if (whiteout) { 756 init_special_inode(inode, inode->i_mode, WHITEOUT_DEV); 757 inode->i_op = &f2fs_special_inode_operations; 758 } else { 759 inode->i_op = &f2fs_file_inode_operations; 760 inode->i_fop = &f2fs_file_operations; 761 inode->i_mapping->a_ops = &f2fs_dblock_aops; 762 } 763 764 f2fs_lock_op(sbi); 765 err = f2fs_acquire_orphan_inode(sbi); 766 if (err) 767 goto out; 768 769 err = f2fs_do_tmpfile(inode, dir); 770 if (err) 771 goto release_out; 772 773 /* 774 * add this non-linked tmpfile to orphan list, in this way we could 775 * remove all unused data of tmpfile after abnormal power-off. 776 */ 777 f2fs_add_orphan_inode(inode); 778 f2fs_alloc_nid_done(sbi, inode->i_ino); 779 780 if (whiteout) { 781 f2fs_i_links_write(inode, false); 782 *whiteout = inode; 783 } else { 784 d_tmpfile(dentry, inode); 785 } 786 /* link_count was changed by d_tmpfile as well. */ 787 f2fs_unlock_op(sbi); 788 unlock_new_inode(inode); 789 790 f2fs_balance_fs(sbi, true); 791 return 0; 792 793 release_out: 794 f2fs_release_orphan_inode(sbi); 795 out: 796 f2fs_handle_failed_inode(inode); 797 return err; 798 } 799 800 static int f2fs_tmpfile(struct inode *dir, struct dentry *dentry, umode_t mode) 801 { 802 struct f2fs_sb_info *sbi = F2FS_I_SB(dir); 803 804 if (unlikely(f2fs_cp_error(sbi))) 805 return -EIO; 806 807 if (IS_ENCRYPTED(dir) || DUMMY_ENCRYPTION_ENABLED(sbi)) { 808 int err = fscrypt_get_encryption_info(dir); 809 if (err) 810 return err; 811 } 812 813 return __f2fs_tmpfile(dir, dentry, mode, NULL); 814 } 815 816 static int f2fs_create_whiteout(struct inode *dir, struct inode **whiteout) 817 { 818 if (unlikely(f2fs_cp_error(F2FS_I_SB(dir)))) 819 return -EIO; 820 821 return __f2fs_tmpfile(dir, NULL, S_IFCHR | WHITEOUT_MODE, whiteout); 822 } 823 824 static int f2fs_rename(struct inode *old_dir, struct dentry *old_dentry, 825 struct inode *new_dir, struct dentry *new_dentry, 826 unsigned int flags) 827 { 828 struct f2fs_sb_info *sbi = F2FS_I_SB(old_dir); 829 struct inode *old_inode = d_inode(old_dentry); 830 struct inode *new_inode = d_inode(new_dentry); 831 struct inode *whiteout = NULL; 832 struct page *old_dir_page; 833 struct page *old_page, *new_page = NULL; 834 struct f2fs_dir_entry *old_dir_entry = NULL; 835 struct f2fs_dir_entry *old_entry; 836 struct f2fs_dir_entry *new_entry; 837 bool is_old_inline = f2fs_has_inline_dentry(old_dir); 838 int err; 839 840 if (unlikely(f2fs_cp_error(sbi))) 841 return -EIO; 842 err = f2fs_is_checkpoint_ready(sbi); 843 if (err) 844 return err; 845 846 if (is_inode_flag_set(new_dir, FI_PROJ_INHERIT) && 847 (!projid_eq(F2FS_I(new_dir)->i_projid, 848 F2FS_I(old_dentry->d_inode)->i_projid))) 849 return -EXDEV; 850 851 err = dquot_initialize(old_dir); 852 if (err) 853 goto out; 854 855 err = dquot_initialize(new_dir); 856 if (err) 857 goto out; 858 859 if (new_inode) { 860 err = dquot_initialize(new_inode); 861 if (err) 862 goto out; 863 } 864 865 err = -ENOENT; 866 old_entry = f2fs_find_entry(old_dir, &old_dentry->d_name, &old_page); 867 if (!old_entry) { 868 if (IS_ERR(old_page)) 869 err = PTR_ERR(old_page); 870 goto out; 871 } 872 873 if (S_ISDIR(old_inode->i_mode)) { 874 old_dir_entry = f2fs_parent_dir(old_inode, &old_dir_page); 875 if (!old_dir_entry) { 876 if (IS_ERR(old_dir_page)) 877 err = PTR_ERR(old_dir_page); 878 goto out_old; 879 } 880 } 881 882 if (flags & RENAME_WHITEOUT) { 883 err = f2fs_create_whiteout(old_dir, &whiteout); 884 if (err) 885 goto out_dir; 886 } 887 888 if (new_inode) { 889 890 err = -ENOTEMPTY; 891 if (old_dir_entry && !f2fs_empty_dir(new_inode)) 892 goto out_whiteout; 893 894 err = -ENOENT; 895 new_entry = f2fs_find_entry(new_dir, &new_dentry->d_name, 896 &new_page); 897 if (!new_entry) { 898 if (IS_ERR(new_page)) 899 err = PTR_ERR(new_page); 900 goto out_whiteout; 901 } 902 903 f2fs_balance_fs(sbi, true); 904 905 f2fs_lock_op(sbi); 906 907 err = f2fs_acquire_orphan_inode(sbi); 908 if (err) 909 goto put_out_dir; 910 911 f2fs_set_link(new_dir, new_entry, new_page, old_inode); 912 913 new_inode->i_ctime = current_time(new_inode); 914 down_write(&F2FS_I(new_inode)->i_sem); 915 if (old_dir_entry) 916 f2fs_i_links_write(new_inode, false); 917 f2fs_i_links_write(new_inode, false); 918 up_write(&F2FS_I(new_inode)->i_sem); 919 920 if (!new_inode->i_nlink) 921 f2fs_add_orphan_inode(new_inode); 922 else 923 f2fs_release_orphan_inode(sbi); 924 } else { 925 f2fs_balance_fs(sbi, true); 926 927 f2fs_lock_op(sbi); 928 929 err = f2fs_add_link(new_dentry, old_inode); 930 if (err) { 931 f2fs_unlock_op(sbi); 932 goto out_whiteout; 933 } 934 935 if (old_dir_entry) 936 f2fs_i_links_write(new_dir, true); 937 938 /* 939 * old entry and new entry can locate in the same inline 940 * dentry in inode, when attaching new entry in inline dentry, 941 * it could force inline dentry conversion, after that, 942 * old_entry and old_page will point to wrong address, in 943 * order to avoid this, let's do the check and update here. 944 */ 945 if (is_old_inline && !f2fs_has_inline_dentry(old_dir)) { 946 f2fs_put_page(old_page, 0); 947 old_page = NULL; 948 949 old_entry = f2fs_find_entry(old_dir, 950 &old_dentry->d_name, &old_page); 951 if (!old_entry) { 952 err = -ENOENT; 953 if (IS_ERR(old_page)) 954 err = PTR_ERR(old_page); 955 f2fs_unlock_op(sbi); 956 goto out_whiteout; 957 } 958 } 959 } 960 961 down_write(&F2FS_I(old_inode)->i_sem); 962 if (!old_dir_entry || whiteout) 963 file_lost_pino(old_inode); 964 else 965 F2FS_I(old_inode)->i_pino = new_dir->i_ino; 966 up_write(&F2FS_I(old_inode)->i_sem); 967 968 old_inode->i_ctime = current_time(old_inode); 969 f2fs_mark_inode_dirty_sync(old_inode, false); 970 971 f2fs_delete_entry(old_entry, old_page, old_dir, NULL); 972 973 if (whiteout) { 974 whiteout->i_state |= I_LINKABLE; 975 set_inode_flag(whiteout, FI_INC_LINK); 976 err = f2fs_add_link(old_dentry, whiteout); 977 if (err) 978 goto put_out_dir; 979 whiteout->i_state &= ~I_LINKABLE; 980 iput(whiteout); 981 } 982 983 if (old_dir_entry) { 984 if (old_dir != new_dir && !whiteout) 985 f2fs_set_link(old_inode, old_dir_entry, 986 old_dir_page, new_dir); 987 else 988 f2fs_put_page(old_dir_page, 0); 989 f2fs_i_links_write(old_dir, false); 990 } 991 if (F2FS_OPTION(sbi).fsync_mode == FSYNC_MODE_STRICT) { 992 f2fs_add_ino_entry(sbi, new_dir->i_ino, TRANS_DIR_INO); 993 if (S_ISDIR(old_inode->i_mode)) 994 f2fs_add_ino_entry(sbi, old_inode->i_ino, 995 TRANS_DIR_INO); 996 } 997 998 f2fs_unlock_op(sbi); 999 1000 if (IS_DIRSYNC(old_dir) || IS_DIRSYNC(new_dir)) 1001 f2fs_sync_fs(sbi->sb, 1); 1002 1003 f2fs_update_time(sbi, REQ_TIME); 1004 return 0; 1005 1006 put_out_dir: 1007 f2fs_unlock_op(sbi); 1008 if (new_page) 1009 f2fs_put_page(new_page, 0); 1010 out_whiteout: 1011 if (whiteout) 1012 iput(whiteout); 1013 out_dir: 1014 if (old_dir_entry) 1015 f2fs_put_page(old_dir_page, 0); 1016 out_old: 1017 f2fs_put_page(old_page, 0); 1018 out: 1019 return err; 1020 } 1021 1022 static int f2fs_cross_rename(struct inode *old_dir, struct dentry *old_dentry, 1023 struct inode *new_dir, struct dentry *new_dentry) 1024 { 1025 struct f2fs_sb_info *sbi = F2FS_I_SB(old_dir); 1026 struct inode *old_inode = d_inode(old_dentry); 1027 struct inode *new_inode = d_inode(new_dentry); 1028 struct page *old_dir_page, *new_dir_page; 1029 struct page *old_page, *new_page; 1030 struct f2fs_dir_entry *old_dir_entry = NULL, *new_dir_entry = NULL; 1031 struct f2fs_dir_entry *old_entry, *new_entry; 1032 int old_nlink = 0, new_nlink = 0; 1033 int err; 1034 1035 if (unlikely(f2fs_cp_error(sbi))) 1036 return -EIO; 1037 err = f2fs_is_checkpoint_ready(sbi); 1038 if (err) 1039 return err; 1040 1041 if ((is_inode_flag_set(new_dir, FI_PROJ_INHERIT) && 1042 !projid_eq(F2FS_I(new_dir)->i_projid, 1043 F2FS_I(old_dentry->d_inode)->i_projid)) || 1044 (is_inode_flag_set(new_dir, FI_PROJ_INHERIT) && 1045 !projid_eq(F2FS_I(old_dir)->i_projid, 1046 F2FS_I(new_dentry->d_inode)->i_projid))) 1047 return -EXDEV; 1048 1049 err = dquot_initialize(old_dir); 1050 if (err) 1051 goto out; 1052 1053 err = dquot_initialize(new_dir); 1054 if (err) 1055 goto out; 1056 1057 err = -ENOENT; 1058 old_entry = f2fs_find_entry(old_dir, &old_dentry->d_name, &old_page); 1059 if (!old_entry) { 1060 if (IS_ERR(old_page)) 1061 err = PTR_ERR(old_page); 1062 goto out; 1063 } 1064 1065 new_entry = f2fs_find_entry(new_dir, &new_dentry->d_name, &new_page); 1066 if (!new_entry) { 1067 if (IS_ERR(new_page)) 1068 err = PTR_ERR(new_page); 1069 goto out_old; 1070 } 1071 1072 /* prepare for updating ".." directory entry info later */ 1073 if (old_dir != new_dir) { 1074 if (S_ISDIR(old_inode->i_mode)) { 1075 old_dir_entry = f2fs_parent_dir(old_inode, 1076 &old_dir_page); 1077 if (!old_dir_entry) { 1078 if (IS_ERR(old_dir_page)) 1079 err = PTR_ERR(old_dir_page); 1080 goto out_new; 1081 } 1082 } 1083 1084 if (S_ISDIR(new_inode->i_mode)) { 1085 new_dir_entry = f2fs_parent_dir(new_inode, 1086 &new_dir_page); 1087 if (!new_dir_entry) { 1088 if (IS_ERR(new_dir_page)) 1089 err = PTR_ERR(new_dir_page); 1090 goto out_old_dir; 1091 } 1092 } 1093 } 1094 1095 /* 1096 * If cross rename between file and directory those are not 1097 * in the same directory, we will inc nlink of file's parent 1098 * later, so we should check upper boundary of its nlink. 1099 */ 1100 if ((!old_dir_entry || !new_dir_entry) && 1101 old_dir_entry != new_dir_entry) { 1102 old_nlink = old_dir_entry ? -1 : 1; 1103 new_nlink = -old_nlink; 1104 err = -EMLINK; 1105 if ((old_nlink > 0 && old_dir->i_nlink >= F2FS_LINK_MAX) || 1106 (new_nlink > 0 && new_dir->i_nlink >= F2FS_LINK_MAX)) 1107 goto out_new_dir; 1108 } 1109 1110 f2fs_balance_fs(sbi, true); 1111 1112 f2fs_lock_op(sbi); 1113 1114 /* update ".." directory entry info of old dentry */ 1115 if (old_dir_entry) 1116 f2fs_set_link(old_inode, old_dir_entry, old_dir_page, new_dir); 1117 1118 /* update ".." directory entry info of new dentry */ 1119 if (new_dir_entry) 1120 f2fs_set_link(new_inode, new_dir_entry, new_dir_page, old_dir); 1121 1122 /* update directory entry info of old dir inode */ 1123 f2fs_set_link(old_dir, old_entry, old_page, new_inode); 1124 1125 down_write(&F2FS_I(old_inode)->i_sem); 1126 file_lost_pino(old_inode); 1127 up_write(&F2FS_I(old_inode)->i_sem); 1128 1129 old_dir->i_ctime = current_time(old_dir); 1130 if (old_nlink) { 1131 down_write(&F2FS_I(old_dir)->i_sem); 1132 f2fs_i_links_write(old_dir, old_nlink > 0); 1133 up_write(&F2FS_I(old_dir)->i_sem); 1134 } 1135 f2fs_mark_inode_dirty_sync(old_dir, false); 1136 1137 /* update directory entry info of new dir inode */ 1138 f2fs_set_link(new_dir, new_entry, new_page, old_inode); 1139 1140 down_write(&F2FS_I(new_inode)->i_sem); 1141 file_lost_pino(new_inode); 1142 up_write(&F2FS_I(new_inode)->i_sem); 1143 1144 new_dir->i_ctime = current_time(new_dir); 1145 if (new_nlink) { 1146 down_write(&F2FS_I(new_dir)->i_sem); 1147 f2fs_i_links_write(new_dir, new_nlink > 0); 1148 up_write(&F2FS_I(new_dir)->i_sem); 1149 } 1150 f2fs_mark_inode_dirty_sync(new_dir, false); 1151 1152 if (F2FS_OPTION(sbi).fsync_mode == FSYNC_MODE_STRICT) { 1153 f2fs_add_ino_entry(sbi, old_dir->i_ino, TRANS_DIR_INO); 1154 f2fs_add_ino_entry(sbi, new_dir->i_ino, TRANS_DIR_INO); 1155 } 1156 1157 f2fs_unlock_op(sbi); 1158 1159 if (IS_DIRSYNC(old_dir) || IS_DIRSYNC(new_dir)) 1160 f2fs_sync_fs(sbi->sb, 1); 1161 1162 f2fs_update_time(sbi, REQ_TIME); 1163 return 0; 1164 out_new_dir: 1165 if (new_dir_entry) { 1166 f2fs_put_page(new_dir_page, 0); 1167 } 1168 out_old_dir: 1169 if (old_dir_entry) { 1170 f2fs_put_page(old_dir_page, 0); 1171 } 1172 out_new: 1173 f2fs_put_page(new_page, 0); 1174 out_old: 1175 f2fs_put_page(old_page, 0); 1176 out: 1177 return err; 1178 } 1179 1180 static int f2fs_rename2(struct inode *old_dir, struct dentry *old_dentry, 1181 struct inode *new_dir, struct dentry *new_dentry, 1182 unsigned int flags) 1183 { 1184 int err; 1185 1186 if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE | RENAME_WHITEOUT)) 1187 return -EINVAL; 1188 1189 err = fscrypt_prepare_rename(old_dir, old_dentry, new_dir, new_dentry, 1190 flags); 1191 if (err) 1192 return err; 1193 1194 if (flags & RENAME_EXCHANGE) { 1195 return f2fs_cross_rename(old_dir, old_dentry, 1196 new_dir, new_dentry); 1197 } 1198 /* 1199 * VFS has already handled the new dentry existence case, 1200 * here, we just deal with "RENAME_NOREPLACE" as regular rename. 1201 */ 1202 return f2fs_rename(old_dir, old_dentry, new_dir, new_dentry, flags); 1203 } 1204 1205 static const char *f2fs_encrypted_get_link(struct dentry *dentry, 1206 struct inode *inode, 1207 struct delayed_call *done) 1208 { 1209 struct page *page; 1210 const char *target; 1211 1212 if (!dentry) 1213 return ERR_PTR(-ECHILD); 1214 1215 page = read_mapping_page(inode->i_mapping, 0, NULL); 1216 if (IS_ERR(page)) 1217 return ERR_CAST(page); 1218 1219 target = fscrypt_get_symlink(inode, page_address(page), 1220 inode->i_sb->s_blocksize, done); 1221 put_page(page); 1222 return target; 1223 } 1224 1225 const struct inode_operations f2fs_encrypted_symlink_inode_operations = { 1226 .get_link = f2fs_encrypted_get_link, 1227 .getattr = f2fs_getattr, 1228 .setattr = f2fs_setattr, 1229 #ifdef CONFIG_F2FS_FS_XATTR 1230 .listxattr = f2fs_listxattr, 1231 #endif 1232 }; 1233 1234 const struct inode_operations f2fs_dir_inode_operations = { 1235 .create = f2fs_create, 1236 .lookup = f2fs_lookup, 1237 .link = f2fs_link, 1238 .unlink = f2fs_unlink, 1239 .symlink = f2fs_symlink, 1240 .mkdir = f2fs_mkdir, 1241 .rmdir = f2fs_rmdir, 1242 .mknod = f2fs_mknod, 1243 .rename = f2fs_rename2, 1244 .tmpfile = f2fs_tmpfile, 1245 .getattr = f2fs_getattr, 1246 .setattr = f2fs_setattr, 1247 .get_acl = f2fs_get_acl, 1248 .set_acl = f2fs_set_acl, 1249 #ifdef CONFIG_F2FS_FS_XATTR 1250 .listxattr = f2fs_listxattr, 1251 #endif 1252 }; 1253 1254 const struct inode_operations f2fs_symlink_inode_operations = { 1255 .get_link = f2fs_get_link, 1256 .getattr = f2fs_getattr, 1257 .setattr = f2fs_setattr, 1258 #ifdef CONFIG_F2FS_FS_XATTR 1259 .listxattr = f2fs_listxattr, 1260 #endif 1261 }; 1262 1263 const struct inode_operations f2fs_special_inode_operations = { 1264 .getattr = f2fs_getattr, 1265 .setattr = f2fs_setattr, 1266 .get_acl = f2fs_get_acl, 1267 .set_acl = f2fs_set_acl, 1268 #ifdef CONFIG_F2FS_FS_XATTR 1269 .listxattr = f2fs_listxattr, 1270 #endif 1271 }; 1272