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