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