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