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 inline 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_info(sbi, "skip recovering inline_dots inode (ino:%lu, pino:%u) in readonly mountpoint", 389 dir->i_ino, pino); 390 return 0; 391 } 392 393 err = dquot_initialize(dir); 394 if (err) 395 return err; 396 397 f2fs_balance_fs(sbi, true); 398 399 f2fs_lock_op(sbi); 400 401 de = f2fs_find_entry(dir, &dot, &page); 402 if (de) { 403 f2fs_put_page(page, 0); 404 } else if (IS_ERR(page)) { 405 err = PTR_ERR(page); 406 goto out; 407 } else { 408 err = f2fs_do_add_link(dir, &dot, NULL, dir->i_ino, S_IFDIR); 409 if (err) 410 goto out; 411 } 412 413 de = f2fs_find_entry(dir, &dotdot, &page); 414 if (de) 415 f2fs_put_page(page, 0); 416 else if (IS_ERR(page)) 417 err = PTR_ERR(page); 418 else 419 err = f2fs_do_add_link(dir, &dotdot, NULL, pino, S_IFDIR); 420 out: 421 if (!err) 422 clear_inode_flag(dir, FI_INLINE_DOTS); 423 424 f2fs_unlock_op(sbi); 425 return err; 426 } 427 428 static struct dentry *f2fs_lookup(struct inode *dir, struct dentry *dentry, 429 unsigned int flags) 430 { 431 struct inode *inode = NULL; 432 struct f2fs_dir_entry *de; 433 struct page *page; 434 struct dentry *new; 435 nid_t ino = -1; 436 int err = 0; 437 unsigned int root_ino = F2FS_ROOT_INO(F2FS_I_SB(dir)); 438 struct fscrypt_name fname; 439 440 trace_f2fs_lookup_start(dir, dentry, flags); 441 442 if (dentry->d_name.len > F2FS_NAME_LEN) { 443 err = -ENAMETOOLONG; 444 goto out; 445 } 446 447 err = fscrypt_prepare_lookup(dir, dentry, &fname); 448 if (err == -ENOENT) 449 goto out_splice; 450 if (err) 451 goto out; 452 de = __f2fs_find_entry(dir, &fname, &page); 453 fscrypt_free_filename(&fname); 454 455 if (!de) { 456 if (IS_ERR(page)) { 457 err = PTR_ERR(page); 458 goto out; 459 } 460 goto out_splice; 461 } 462 463 ino = le32_to_cpu(de->ino); 464 f2fs_put_page(page, 0); 465 466 inode = f2fs_iget(dir->i_sb, ino); 467 if (IS_ERR(inode)) { 468 err = PTR_ERR(inode); 469 goto out; 470 } 471 472 if ((dir->i_ino == root_ino) && f2fs_has_inline_dots(dir)) { 473 err = __recover_dot_dentries(dir, root_ino); 474 if (err) 475 goto out_iput; 476 } 477 478 if (f2fs_has_inline_dots(inode)) { 479 err = __recover_dot_dentries(inode, dir->i_ino); 480 if (err) 481 goto out_iput; 482 } 483 if (IS_ENCRYPTED(dir) && 484 (S_ISDIR(inode->i_mode) || S_ISLNK(inode->i_mode)) && 485 !fscrypt_has_permitted_context(dir, inode)) { 486 f2fs_warn(F2FS_I_SB(inode), "Inconsistent encryption contexts: %lu/%lu", 487 dir->i_ino, inode->i_ino); 488 err = -EPERM; 489 goto out_iput; 490 } 491 out_splice: 492 new = d_splice_alias(inode, dentry); 493 err = PTR_ERR_OR_ZERO(new); 494 trace_f2fs_lookup_end(dir, dentry, ino, err); 495 return new; 496 out_iput: 497 iput(inode); 498 out: 499 trace_f2fs_lookup_end(dir, dentry, ino, err); 500 return ERR_PTR(err); 501 } 502 503 static int f2fs_unlink(struct inode *dir, struct dentry *dentry) 504 { 505 struct f2fs_sb_info *sbi = F2FS_I_SB(dir); 506 struct inode *inode = d_inode(dentry); 507 struct f2fs_dir_entry *de; 508 struct page *page; 509 int err = -ENOENT; 510 511 trace_f2fs_unlink_enter(dir, dentry); 512 513 if (unlikely(f2fs_cp_error(sbi))) 514 return -EIO; 515 516 err = dquot_initialize(dir); 517 if (err) 518 return err; 519 err = dquot_initialize(inode); 520 if (err) 521 return err; 522 523 de = f2fs_find_entry(dir, &dentry->d_name, &page); 524 if (!de) { 525 if (IS_ERR(page)) 526 err = PTR_ERR(page); 527 goto fail; 528 } 529 530 f2fs_balance_fs(sbi, true); 531 532 f2fs_lock_op(sbi); 533 err = f2fs_acquire_orphan_inode(sbi); 534 if (err) { 535 f2fs_unlock_op(sbi); 536 f2fs_put_page(page, 0); 537 goto fail; 538 } 539 f2fs_delete_entry(de, page, dir, inode); 540 f2fs_unlock_op(sbi); 541 542 if (IS_DIRSYNC(dir)) 543 f2fs_sync_fs(sbi->sb, 1); 544 fail: 545 trace_f2fs_unlink_exit(inode, err); 546 return err; 547 } 548 549 static const char *f2fs_get_link(struct dentry *dentry, 550 struct inode *inode, 551 struct delayed_call *done) 552 { 553 const char *link = page_get_link(dentry, inode, done); 554 if (!IS_ERR(link) && !*link) { 555 /* this is broken symlink case */ 556 do_delayed_call(done); 557 clear_delayed_call(done); 558 link = ERR_PTR(-ENOENT); 559 } 560 return link; 561 } 562 563 static int f2fs_symlink(struct inode *dir, struct dentry *dentry, 564 const char *symname) 565 { 566 struct f2fs_sb_info *sbi = F2FS_I_SB(dir); 567 struct inode *inode; 568 size_t len = strlen(symname); 569 struct fscrypt_str disk_link; 570 int err; 571 572 if (unlikely(f2fs_cp_error(sbi))) 573 return -EIO; 574 err = f2fs_is_checkpoint_ready(sbi); 575 if (err) 576 return err; 577 578 err = fscrypt_prepare_symlink(dir, symname, len, dir->i_sb->s_blocksize, 579 &disk_link); 580 if (err) 581 return err; 582 583 err = dquot_initialize(dir); 584 if (err) 585 return err; 586 587 inode = f2fs_new_inode(dir, S_IFLNK | S_IRWXUGO); 588 if (IS_ERR(inode)) 589 return PTR_ERR(inode); 590 591 if (IS_ENCRYPTED(inode)) 592 inode->i_op = &f2fs_encrypted_symlink_inode_operations; 593 else 594 inode->i_op = &f2fs_symlink_inode_operations; 595 inode_nohighmem(inode); 596 inode->i_mapping->a_ops = &f2fs_dblock_aops; 597 598 f2fs_lock_op(sbi); 599 err = f2fs_add_link(dentry, inode); 600 if (err) 601 goto out_f2fs_handle_failed_inode; 602 f2fs_unlock_op(sbi); 603 f2fs_alloc_nid_done(sbi, inode->i_ino); 604 605 err = fscrypt_encrypt_symlink(inode, symname, len, &disk_link); 606 if (err) 607 goto err_out; 608 609 err = page_symlink(inode, disk_link.name, disk_link.len); 610 611 err_out: 612 d_instantiate_new(dentry, inode); 613 614 /* 615 * Let's flush symlink data in order to avoid broken symlink as much as 616 * possible. Nevertheless, fsyncing is the best way, but there is no 617 * way to get a file descriptor in order to flush that. 618 * 619 * Note that, it needs to do dir->fsync to make this recoverable. 620 * If the symlink path is stored into inline_data, there is no 621 * performance regression. 622 */ 623 if (!err) { 624 filemap_write_and_wait_range(inode->i_mapping, 0, 625 disk_link.len - 1); 626 627 if (IS_DIRSYNC(dir)) 628 f2fs_sync_fs(sbi->sb, 1); 629 } else { 630 f2fs_unlink(dir, dentry); 631 } 632 633 f2fs_balance_fs(sbi, true); 634 goto out_free_encrypted_link; 635 636 out_f2fs_handle_failed_inode: 637 f2fs_handle_failed_inode(inode); 638 out_free_encrypted_link: 639 if (disk_link.name != (unsigned char *)symname) 640 kvfree(disk_link.name); 641 return err; 642 } 643 644 static int f2fs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode) 645 { 646 struct f2fs_sb_info *sbi = F2FS_I_SB(dir); 647 struct inode *inode; 648 int err; 649 650 if (unlikely(f2fs_cp_error(sbi))) 651 return -EIO; 652 653 err = dquot_initialize(dir); 654 if (err) 655 return err; 656 657 inode = f2fs_new_inode(dir, S_IFDIR | mode); 658 if (IS_ERR(inode)) 659 return PTR_ERR(inode); 660 661 inode->i_op = &f2fs_dir_inode_operations; 662 inode->i_fop = &f2fs_dir_operations; 663 inode->i_mapping->a_ops = &f2fs_dblock_aops; 664 inode_nohighmem(inode); 665 666 set_inode_flag(inode, FI_INC_LINK); 667 f2fs_lock_op(sbi); 668 err = f2fs_add_link(dentry, inode); 669 if (err) 670 goto out_fail; 671 f2fs_unlock_op(sbi); 672 673 f2fs_alloc_nid_done(sbi, inode->i_ino); 674 675 d_instantiate_new(dentry, inode); 676 677 if (IS_DIRSYNC(dir)) 678 f2fs_sync_fs(sbi->sb, 1); 679 680 f2fs_balance_fs(sbi, true); 681 return 0; 682 683 out_fail: 684 clear_inode_flag(inode, FI_INC_LINK); 685 f2fs_handle_failed_inode(inode); 686 return err; 687 } 688 689 static int f2fs_rmdir(struct inode *dir, struct dentry *dentry) 690 { 691 struct inode *inode = d_inode(dentry); 692 if (f2fs_empty_dir(inode)) 693 return f2fs_unlink(dir, dentry); 694 return -ENOTEMPTY; 695 } 696 697 static int f2fs_mknod(struct inode *dir, struct dentry *dentry, 698 umode_t mode, dev_t rdev) 699 { 700 struct f2fs_sb_info *sbi = F2FS_I_SB(dir); 701 struct inode *inode; 702 int err = 0; 703 704 if (unlikely(f2fs_cp_error(sbi))) 705 return -EIO; 706 err = f2fs_is_checkpoint_ready(sbi); 707 if (err) 708 return err; 709 710 err = dquot_initialize(dir); 711 if (err) 712 return err; 713 714 inode = f2fs_new_inode(dir, mode); 715 if (IS_ERR(inode)) 716 return PTR_ERR(inode); 717 718 init_special_inode(inode, inode->i_mode, rdev); 719 inode->i_op = &f2fs_special_inode_operations; 720 721 f2fs_lock_op(sbi); 722 err = f2fs_add_link(dentry, inode); 723 if (err) 724 goto out; 725 f2fs_unlock_op(sbi); 726 727 f2fs_alloc_nid_done(sbi, inode->i_ino); 728 729 d_instantiate_new(dentry, inode); 730 731 if (IS_DIRSYNC(dir)) 732 f2fs_sync_fs(sbi->sb, 1); 733 734 f2fs_balance_fs(sbi, true); 735 return 0; 736 out: 737 f2fs_handle_failed_inode(inode); 738 return err; 739 } 740 741 static int __f2fs_tmpfile(struct inode *dir, struct dentry *dentry, 742 umode_t mode, struct inode **whiteout) 743 { 744 struct f2fs_sb_info *sbi = F2FS_I_SB(dir); 745 struct inode *inode; 746 int err; 747 748 err = dquot_initialize(dir); 749 if (err) 750 return err; 751 752 inode = f2fs_new_inode(dir, mode); 753 if (IS_ERR(inode)) 754 return PTR_ERR(inode); 755 756 if (whiteout) { 757 init_special_inode(inode, inode->i_mode, WHITEOUT_DEV); 758 inode->i_op = &f2fs_special_inode_operations; 759 } else { 760 inode->i_op = &f2fs_file_inode_operations; 761 inode->i_fop = &f2fs_file_operations; 762 inode->i_mapping->a_ops = &f2fs_dblock_aops; 763 } 764 765 f2fs_lock_op(sbi); 766 err = f2fs_acquire_orphan_inode(sbi); 767 if (err) 768 goto out; 769 770 err = f2fs_do_tmpfile(inode, dir); 771 if (err) 772 goto release_out; 773 774 /* 775 * add this non-linked tmpfile to orphan list, in this way we could 776 * remove all unused data of tmpfile after abnormal power-off. 777 */ 778 f2fs_add_orphan_inode(inode); 779 f2fs_alloc_nid_done(sbi, inode->i_ino); 780 781 if (whiteout) { 782 f2fs_i_links_write(inode, false); 783 *whiteout = inode; 784 } else { 785 d_tmpfile(dentry, inode); 786 } 787 /* link_count was changed by d_tmpfile as well. */ 788 f2fs_unlock_op(sbi); 789 unlock_new_inode(inode); 790 791 f2fs_balance_fs(sbi, true); 792 return 0; 793 794 release_out: 795 f2fs_release_orphan_inode(sbi); 796 out: 797 f2fs_handle_failed_inode(inode); 798 return err; 799 } 800 801 static int f2fs_tmpfile(struct inode *dir, struct dentry *dentry, umode_t mode) 802 { 803 struct f2fs_sb_info *sbi = F2FS_I_SB(dir); 804 805 if (unlikely(f2fs_cp_error(sbi))) 806 return -EIO; 807 808 if (IS_ENCRYPTED(dir) || DUMMY_ENCRYPTION_ENABLED(sbi)) { 809 int err = fscrypt_get_encryption_info(dir); 810 if (err) 811 return err; 812 } 813 814 return __f2fs_tmpfile(dir, dentry, mode, NULL); 815 } 816 817 static int f2fs_create_whiteout(struct inode *dir, struct inode **whiteout) 818 { 819 if (unlikely(f2fs_cp_error(F2FS_I_SB(dir)))) 820 return -EIO; 821 822 return __f2fs_tmpfile(dir, NULL, S_IFCHR | WHITEOUT_MODE, whiteout); 823 } 824 825 static int f2fs_rename(struct inode *old_dir, struct dentry *old_dentry, 826 struct inode *new_dir, struct dentry *new_dentry, 827 unsigned int flags) 828 { 829 struct f2fs_sb_info *sbi = F2FS_I_SB(old_dir); 830 struct inode *old_inode = d_inode(old_dentry); 831 struct inode *new_inode = d_inode(new_dentry); 832 struct inode *whiteout = NULL; 833 struct page *old_dir_page; 834 struct page *old_page, *new_page = NULL; 835 struct f2fs_dir_entry *old_dir_entry = NULL; 836 struct f2fs_dir_entry *old_entry; 837 struct f2fs_dir_entry *new_entry; 838 bool is_old_inline = f2fs_has_inline_dentry(old_dir); 839 int err; 840 841 if (unlikely(f2fs_cp_error(sbi))) 842 return -EIO; 843 err = f2fs_is_checkpoint_ready(sbi); 844 if (err) 845 return err; 846 847 if (is_inode_flag_set(new_dir, FI_PROJ_INHERIT) && 848 (!projid_eq(F2FS_I(new_dir)->i_projid, 849 F2FS_I(old_dentry->d_inode)->i_projid))) 850 return -EXDEV; 851 852 err = dquot_initialize(old_dir); 853 if (err) 854 goto out; 855 856 err = dquot_initialize(new_dir); 857 if (err) 858 goto out; 859 860 if (new_inode) { 861 err = dquot_initialize(new_inode); 862 if (err) 863 goto out; 864 } 865 866 err = -ENOENT; 867 old_entry = f2fs_find_entry(old_dir, &old_dentry->d_name, &old_page); 868 if (!old_entry) { 869 if (IS_ERR(old_page)) 870 err = PTR_ERR(old_page); 871 goto out; 872 } 873 874 if (S_ISDIR(old_inode->i_mode)) { 875 old_dir_entry = f2fs_parent_dir(old_inode, &old_dir_page); 876 if (!old_dir_entry) { 877 if (IS_ERR(old_dir_page)) 878 err = PTR_ERR(old_dir_page); 879 goto out_old; 880 } 881 } 882 883 if (flags & RENAME_WHITEOUT) { 884 err = f2fs_create_whiteout(old_dir, &whiteout); 885 if (err) 886 goto out_dir; 887 } 888 889 if (new_inode) { 890 891 err = -ENOTEMPTY; 892 if (old_dir_entry && !f2fs_empty_dir(new_inode)) 893 goto out_whiteout; 894 895 err = -ENOENT; 896 new_entry = f2fs_find_entry(new_dir, &new_dentry->d_name, 897 &new_page); 898 if (!new_entry) { 899 if (IS_ERR(new_page)) 900 err = PTR_ERR(new_page); 901 goto out_whiteout; 902 } 903 904 f2fs_balance_fs(sbi, true); 905 906 f2fs_lock_op(sbi); 907 908 err = f2fs_acquire_orphan_inode(sbi); 909 if (err) 910 goto put_out_dir; 911 912 f2fs_set_link(new_dir, new_entry, new_page, old_inode); 913 914 new_inode->i_ctime = current_time(new_inode); 915 down_write(&F2FS_I(new_inode)->i_sem); 916 if (old_dir_entry) 917 f2fs_i_links_write(new_inode, false); 918 f2fs_i_links_write(new_inode, false); 919 up_write(&F2FS_I(new_inode)->i_sem); 920 921 if (!new_inode->i_nlink) 922 f2fs_add_orphan_inode(new_inode); 923 else 924 f2fs_release_orphan_inode(sbi); 925 } else { 926 f2fs_balance_fs(sbi, true); 927 928 f2fs_lock_op(sbi); 929 930 err = f2fs_add_link(new_dentry, old_inode); 931 if (err) { 932 f2fs_unlock_op(sbi); 933 goto out_whiteout; 934 } 935 936 if (old_dir_entry) 937 f2fs_i_links_write(new_dir, true); 938 939 /* 940 * old entry and new entry can locate in the same inline 941 * dentry in inode, when attaching new entry in inline dentry, 942 * it could force inline dentry conversion, after that, 943 * old_entry and old_page will point to wrong address, in 944 * order to avoid this, let's do the check and update here. 945 */ 946 if (is_old_inline && !f2fs_has_inline_dentry(old_dir)) { 947 f2fs_put_page(old_page, 0); 948 old_page = NULL; 949 950 old_entry = f2fs_find_entry(old_dir, 951 &old_dentry->d_name, &old_page); 952 if (!old_entry) { 953 err = -ENOENT; 954 if (IS_ERR(old_page)) 955 err = PTR_ERR(old_page); 956 f2fs_unlock_op(sbi); 957 goto out_whiteout; 958 } 959 } 960 } 961 962 down_write(&F2FS_I(old_inode)->i_sem); 963 if (!old_dir_entry || whiteout) 964 file_lost_pino(old_inode); 965 else 966 F2FS_I(old_inode)->i_pino = new_dir->i_ino; 967 up_write(&F2FS_I(old_inode)->i_sem); 968 969 old_inode->i_ctime = current_time(old_inode); 970 f2fs_mark_inode_dirty_sync(old_inode, false); 971 972 f2fs_delete_entry(old_entry, old_page, old_dir, NULL); 973 974 if (whiteout) { 975 whiteout->i_state |= I_LINKABLE; 976 set_inode_flag(whiteout, FI_INC_LINK); 977 err = f2fs_add_link(old_dentry, whiteout); 978 if (err) 979 goto put_out_dir; 980 whiteout->i_state &= ~I_LINKABLE; 981 iput(whiteout); 982 } 983 984 if (old_dir_entry) { 985 if (old_dir != new_dir && !whiteout) 986 f2fs_set_link(old_inode, old_dir_entry, 987 old_dir_page, new_dir); 988 else 989 f2fs_put_page(old_dir_page, 0); 990 f2fs_i_links_write(old_dir, false); 991 } 992 if (F2FS_OPTION(sbi).fsync_mode == FSYNC_MODE_STRICT) { 993 f2fs_add_ino_entry(sbi, new_dir->i_ino, TRANS_DIR_INO); 994 if (S_ISDIR(old_inode->i_mode)) 995 f2fs_add_ino_entry(sbi, old_inode->i_ino, 996 TRANS_DIR_INO); 997 } 998 999 f2fs_unlock_op(sbi); 1000 1001 if (IS_DIRSYNC(old_dir) || IS_DIRSYNC(new_dir)) 1002 f2fs_sync_fs(sbi->sb, 1); 1003 1004 f2fs_update_time(sbi, REQ_TIME); 1005 return 0; 1006 1007 put_out_dir: 1008 f2fs_unlock_op(sbi); 1009 if (new_page) 1010 f2fs_put_page(new_page, 0); 1011 out_whiteout: 1012 if (whiteout) 1013 iput(whiteout); 1014 out_dir: 1015 if (old_dir_entry) 1016 f2fs_put_page(old_dir_page, 0); 1017 out_old: 1018 f2fs_put_page(old_page, 0); 1019 out: 1020 return err; 1021 } 1022 1023 static int f2fs_cross_rename(struct inode *old_dir, struct dentry *old_dentry, 1024 struct inode *new_dir, struct dentry *new_dentry) 1025 { 1026 struct f2fs_sb_info *sbi = F2FS_I_SB(old_dir); 1027 struct inode *old_inode = d_inode(old_dentry); 1028 struct inode *new_inode = d_inode(new_dentry); 1029 struct page *old_dir_page, *new_dir_page; 1030 struct page *old_page, *new_page; 1031 struct f2fs_dir_entry *old_dir_entry = NULL, *new_dir_entry = NULL; 1032 struct f2fs_dir_entry *old_entry, *new_entry; 1033 int old_nlink = 0, new_nlink = 0; 1034 int err; 1035 1036 if (unlikely(f2fs_cp_error(sbi))) 1037 return -EIO; 1038 err = f2fs_is_checkpoint_ready(sbi); 1039 if (err) 1040 return err; 1041 1042 if ((is_inode_flag_set(new_dir, FI_PROJ_INHERIT) && 1043 !projid_eq(F2FS_I(new_dir)->i_projid, 1044 F2FS_I(old_dentry->d_inode)->i_projid)) || 1045 (is_inode_flag_set(new_dir, FI_PROJ_INHERIT) && 1046 !projid_eq(F2FS_I(old_dir)->i_projid, 1047 F2FS_I(new_dentry->d_inode)->i_projid))) 1048 return -EXDEV; 1049 1050 err = dquot_initialize(old_dir); 1051 if (err) 1052 goto out; 1053 1054 err = dquot_initialize(new_dir); 1055 if (err) 1056 goto out; 1057 1058 err = -ENOENT; 1059 old_entry = f2fs_find_entry(old_dir, &old_dentry->d_name, &old_page); 1060 if (!old_entry) { 1061 if (IS_ERR(old_page)) 1062 err = PTR_ERR(old_page); 1063 goto out; 1064 } 1065 1066 new_entry = f2fs_find_entry(new_dir, &new_dentry->d_name, &new_page); 1067 if (!new_entry) { 1068 if (IS_ERR(new_page)) 1069 err = PTR_ERR(new_page); 1070 goto out_old; 1071 } 1072 1073 /* prepare for updating ".." directory entry info later */ 1074 if (old_dir != new_dir) { 1075 if (S_ISDIR(old_inode->i_mode)) { 1076 old_dir_entry = f2fs_parent_dir(old_inode, 1077 &old_dir_page); 1078 if (!old_dir_entry) { 1079 if (IS_ERR(old_dir_page)) 1080 err = PTR_ERR(old_dir_page); 1081 goto out_new; 1082 } 1083 } 1084 1085 if (S_ISDIR(new_inode->i_mode)) { 1086 new_dir_entry = f2fs_parent_dir(new_inode, 1087 &new_dir_page); 1088 if (!new_dir_entry) { 1089 if (IS_ERR(new_dir_page)) 1090 err = PTR_ERR(new_dir_page); 1091 goto out_old_dir; 1092 } 1093 } 1094 } 1095 1096 /* 1097 * If cross rename between file and directory those are not 1098 * in the same directory, we will inc nlink of file's parent 1099 * later, so we should check upper boundary of its nlink. 1100 */ 1101 if ((!old_dir_entry || !new_dir_entry) && 1102 old_dir_entry != new_dir_entry) { 1103 old_nlink = old_dir_entry ? -1 : 1; 1104 new_nlink = -old_nlink; 1105 err = -EMLINK; 1106 if ((old_nlink > 0 && old_dir->i_nlink >= F2FS_LINK_MAX) || 1107 (new_nlink > 0 && new_dir->i_nlink >= F2FS_LINK_MAX)) 1108 goto out_new_dir; 1109 } 1110 1111 f2fs_balance_fs(sbi, true); 1112 1113 f2fs_lock_op(sbi); 1114 1115 /* update ".." directory entry info of old dentry */ 1116 if (old_dir_entry) 1117 f2fs_set_link(old_inode, old_dir_entry, old_dir_page, new_dir); 1118 1119 /* update ".." directory entry info of new dentry */ 1120 if (new_dir_entry) 1121 f2fs_set_link(new_inode, new_dir_entry, new_dir_page, old_dir); 1122 1123 /* update directory entry info of old dir inode */ 1124 f2fs_set_link(old_dir, old_entry, old_page, new_inode); 1125 1126 down_write(&F2FS_I(old_inode)->i_sem); 1127 file_lost_pino(old_inode); 1128 up_write(&F2FS_I(old_inode)->i_sem); 1129 1130 old_dir->i_ctime = current_time(old_dir); 1131 if (old_nlink) { 1132 down_write(&F2FS_I(old_dir)->i_sem); 1133 f2fs_i_links_write(old_dir, old_nlink > 0); 1134 up_write(&F2FS_I(old_dir)->i_sem); 1135 } 1136 f2fs_mark_inode_dirty_sync(old_dir, false); 1137 1138 /* update directory entry info of new dir inode */ 1139 f2fs_set_link(new_dir, new_entry, new_page, old_inode); 1140 1141 down_write(&F2FS_I(new_inode)->i_sem); 1142 file_lost_pino(new_inode); 1143 up_write(&F2FS_I(new_inode)->i_sem); 1144 1145 new_dir->i_ctime = current_time(new_dir); 1146 if (new_nlink) { 1147 down_write(&F2FS_I(new_dir)->i_sem); 1148 f2fs_i_links_write(new_dir, new_nlink > 0); 1149 up_write(&F2FS_I(new_dir)->i_sem); 1150 } 1151 f2fs_mark_inode_dirty_sync(new_dir, false); 1152 1153 if (F2FS_OPTION(sbi).fsync_mode == FSYNC_MODE_STRICT) { 1154 f2fs_add_ino_entry(sbi, old_dir->i_ino, TRANS_DIR_INO); 1155 f2fs_add_ino_entry(sbi, new_dir->i_ino, TRANS_DIR_INO); 1156 } 1157 1158 f2fs_unlock_op(sbi); 1159 1160 if (IS_DIRSYNC(old_dir) || IS_DIRSYNC(new_dir)) 1161 f2fs_sync_fs(sbi->sb, 1); 1162 1163 f2fs_update_time(sbi, REQ_TIME); 1164 return 0; 1165 out_new_dir: 1166 if (new_dir_entry) { 1167 f2fs_put_page(new_dir_page, 0); 1168 } 1169 out_old_dir: 1170 if (old_dir_entry) { 1171 f2fs_put_page(old_dir_page, 0); 1172 } 1173 out_new: 1174 f2fs_put_page(new_page, 0); 1175 out_old: 1176 f2fs_put_page(old_page, 0); 1177 out: 1178 return err; 1179 } 1180 1181 static int f2fs_rename2(struct inode *old_dir, struct dentry *old_dentry, 1182 struct inode *new_dir, struct dentry *new_dentry, 1183 unsigned int flags) 1184 { 1185 int err; 1186 1187 if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE | RENAME_WHITEOUT)) 1188 return -EINVAL; 1189 1190 err = fscrypt_prepare_rename(old_dir, old_dentry, new_dir, new_dentry, 1191 flags); 1192 if (err) 1193 return err; 1194 1195 if (flags & RENAME_EXCHANGE) { 1196 return f2fs_cross_rename(old_dir, old_dentry, 1197 new_dir, new_dentry); 1198 } 1199 /* 1200 * VFS has already handled the new dentry existence case, 1201 * here, we just deal with "RENAME_NOREPLACE" as regular rename. 1202 */ 1203 return f2fs_rename(old_dir, old_dentry, new_dir, new_dentry, flags); 1204 } 1205 1206 static const char *f2fs_encrypted_get_link(struct dentry *dentry, 1207 struct inode *inode, 1208 struct delayed_call *done) 1209 { 1210 struct page *page; 1211 const char *target; 1212 1213 if (!dentry) 1214 return ERR_PTR(-ECHILD); 1215 1216 page = read_mapping_page(inode->i_mapping, 0, NULL); 1217 if (IS_ERR(page)) 1218 return ERR_CAST(page); 1219 1220 target = fscrypt_get_symlink(inode, page_address(page), 1221 inode->i_sb->s_blocksize, done); 1222 put_page(page); 1223 return target; 1224 } 1225 1226 const struct inode_operations f2fs_encrypted_symlink_inode_operations = { 1227 .get_link = f2fs_encrypted_get_link, 1228 .getattr = f2fs_getattr, 1229 .setattr = f2fs_setattr, 1230 #ifdef CONFIG_F2FS_FS_XATTR 1231 .listxattr = f2fs_listxattr, 1232 #endif 1233 }; 1234 1235 const struct inode_operations f2fs_dir_inode_operations = { 1236 .create = f2fs_create, 1237 .lookup = f2fs_lookup, 1238 .link = f2fs_link, 1239 .unlink = f2fs_unlink, 1240 .symlink = f2fs_symlink, 1241 .mkdir = f2fs_mkdir, 1242 .rmdir = f2fs_rmdir, 1243 .mknod = f2fs_mknod, 1244 .rename = f2fs_rename2, 1245 .tmpfile = f2fs_tmpfile, 1246 .getattr = f2fs_getattr, 1247 .setattr = f2fs_setattr, 1248 .get_acl = f2fs_get_acl, 1249 .set_acl = f2fs_set_acl, 1250 #ifdef CONFIG_F2FS_FS_XATTR 1251 .listxattr = f2fs_listxattr, 1252 #endif 1253 }; 1254 1255 const struct inode_operations f2fs_symlink_inode_operations = { 1256 .get_link = f2fs_get_link, 1257 .getattr = f2fs_getattr, 1258 .setattr = f2fs_setattr, 1259 #ifdef CONFIG_F2FS_FS_XATTR 1260 .listxattr = f2fs_listxattr, 1261 #endif 1262 }; 1263 1264 const struct inode_operations f2fs_special_inode_operations = { 1265 .getattr = f2fs_getattr, 1266 .setattr = f2fs_setattr, 1267 .get_acl = f2fs_get_acl, 1268 .set_acl = f2fs_set_acl, 1269 #ifdef CONFIG_F2FS_FS_XATTR 1270 .listxattr = f2fs_listxattr, 1271 #endif 1272 }; 1273