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 19 #include "f2fs.h" 20 #include "node.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 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 (!alloc_nid(sbi, &ino)) { 39 f2fs_unlock_op(sbi); 40 err = -ENOSPC; 41 goto fail; 42 } 43 f2fs_unlock_op(sbi); 44 45 inode_init_owner(inode, dir, mode); 46 47 inode->i_ino = ino; 48 inode->i_blocks = 0; 49 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME; 50 inode->i_generation = sbi->s_next_generation++; 51 52 err = insert_inode_locked(inode); 53 if (err) { 54 err = -EINVAL; 55 nid_free = true; 56 goto out; 57 } 58 59 /* If the directory encrypted, then we should encrypt the inode. */ 60 if (f2fs_encrypted_inode(dir) && f2fs_may_encrypt(inode)) 61 f2fs_set_encrypted_inode(inode); 62 63 if (f2fs_may_inline_data(inode)) 64 set_inode_flag(F2FS_I(inode), FI_INLINE_DATA); 65 if (f2fs_may_inline_dentry(inode)) 66 set_inode_flag(F2FS_I(inode), FI_INLINE_DENTRY); 67 68 stat_inc_inline_inode(inode); 69 stat_inc_inline_dir(inode); 70 71 trace_f2fs_new_inode(inode, 0); 72 mark_inode_dirty(inode); 73 return inode; 74 75 out: 76 clear_nlink(inode); 77 unlock_new_inode(inode); 78 fail: 79 trace_f2fs_new_inode(inode, err); 80 make_bad_inode(inode); 81 iput(inode); 82 if (nid_free) 83 alloc_nid_failed(sbi, ino); 84 return ERR_PTR(err); 85 } 86 87 static int is_multimedia_file(const unsigned char *s, const char *sub) 88 { 89 size_t slen = strlen(s); 90 size_t sublen = strlen(sub); 91 92 if (sublen > slen) 93 return 0; 94 95 return !strncasecmp(s + slen - sublen, sub, sublen); 96 } 97 98 /* 99 * Set multimedia files as cold files for hot/cold data separation 100 */ 101 static inline void set_cold_files(struct f2fs_sb_info *sbi, struct inode *inode, 102 const unsigned char *name) 103 { 104 int i; 105 __u8 (*extlist)[8] = sbi->raw_super->extension_list; 106 107 int count = le32_to_cpu(sbi->raw_super->extension_count); 108 for (i = 0; i < count; i++) { 109 if (is_multimedia_file(name, extlist[i])) { 110 file_set_cold(inode); 111 break; 112 } 113 } 114 } 115 116 static int f2fs_create(struct inode *dir, struct dentry *dentry, umode_t mode, 117 bool excl) 118 { 119 struct f2fs_sb_info *sbi = F2FS_I_SB(dir); 120 struct inode *inode; 121 nid_t ino = 0; 122 int err; 123 124 f2fs_balance_fs(sbi); 125 126 inode = f2fs_new_inode(dir, mode); 127 if (IS_ERR(inode)) 128 return PTR_ERR(inode); 129 130 if (!test_opt(sbi, DISABLE_EXT_IDENTIFY)) 131 set_cold_files(sbi, inode, dentry->d_name.name); 132 133 inode->i_op = &f2fs_file_inode_operations; 134 inode->i_fop = &f2fs_file_operations; 135 inode->i_mapping->a_ops = &f2fs_dblock_aops; 136 ino = inode->i_ino; 137 138 f2fs_lock_op(sbi); 139 err = f2fs_add_link(dentry, inode); 140 if (err) 141 goto out; 142 f2fs_unlock_op(sbi); 143 144 alloc_nid_done(sbi, ino); 145 146 d_instantiate(dentry, inode); 147 unlock_new_inode(inode); 148 149 if (IS_DIRSYNC(dir)) 150 f2fs_sync_fs(sbi->sb, 1); 151 return 0; 152 out: 153 handle_failed_inode(inode); 154 return err; 155 } 156 157 static int f2fs_link(struct dentry *old_dentry, struct inode *dir, 158 struct dentry *dentry) 159 { 160 struct inode *inode = d_inode(old_dentry); 161 struct f2fs_sb_info *sbi = F2FS_I_SB(dir); 162 int err; 163 164 if (f2fs_encrypted_inode(dir) && 165 !f2fs_is_child_context_consistent_with_parent(dir, inode)) 166 return -EPERM; 167 168 f2fs_balance_fs(sbi); 169 170 inode->i_ctime = CURRENT_TIME; 171 ihold(inode); 172 173 set_inode_flag(F2FS_I(inode), FI_INC_LINK); 174 f2fs_lock_op(sbi); 175 err = f2fs_add_link(dentry, inode); 176 if (err) 177 goto out; 178 f2fs_unlock_op(sbi); 179 180 d_instantiate(dentry, inode); 181 182 if (IS_DIRSYNC(dir)) 183 f2fs_sync_fs(sbi->sb, 1); 184 return 0; 185 out: 186 clear_inode_flag(F2FS_I(inode), FI_INC_LINK); 187 iput(inode); 188 f2fs_unlock_op(sbi); 189 return err; 190 } 191 192 struct dentry *f2fs_get_parent(struct dentry *child) 193 { 194 struct qstr dotdot = QSTR_INIT("..", 2); 195 unsigned long ino = f2fs_inode_by_name(d_inode(child), &dotdot); 196 if (!ino) 197 return ERR_PTR(-ENOENT); 198 return d_obtain_alias(f2fs_iget(d_inode(child)->i_sb, ino)); 199 } 200 201 static int __recover_dot_dentries(struct inode *dir, nid_t pino) 202 { 203 struct f2fs_sb_info *sbi = F2FS_I_SB(dir); 204 struct qstr dot = QSTR_INIT(".", 1); 205 struct qstr dotdot = QSTR_INIT("..", 2); 206 struct f2fs_dir_entry *de; 207 struct page *page; 208 int err = 0; 209 210 f2fs_lock_op(sbi); 211 212 de = f2fs_find_entry(dir, &dot, &page); 213 if (de) { 214 f2fs_dentry_kunmap(dir, page); 215 f2fs_put_page(page, 0); 216 } else { 217 err = __f2fs_add_link(dir, &dot, NULL, dir->i_ino, S_IFDIR); 218 if (err) 219 goto out; 220 } 221 222 de = f2fs_find_entry(dir, &dotdot, &page); 223 if (de) { 224 f2fs_dentry_kunmap(dir, page); 225 f2fs_put_page(page, 0); 226 } else { 227 err = __f2fs_add_link(dir, &dotdot, NULL, pino, S_IFDIR); 228 } 229 out: 230 if (!err) { 231 clear_inode_flag(F2FS_I(dir), FI_INLINE_DOTS); 232 mark_inode_dirty(dir); 233 } 234 235 f2fs_unlock_op(sbi); 236 return err; 237 } 238 239 static struct dentry *f2fs_lookup(struct inode *dir, struct dentry *dentry, 240 unsigned int flags) 241 { 242 struct inode *inode = NULL; 243 struct f2fs_dir_entry *de; 244 struct page *page; 245 nid_t ino; 246 int err = 0; 247 248 if (dentry->d_name.len > F2FS_NAME_LEN) 249 return ERR_PTR(-ENAMETOOLONG); 250 251 de = f2fs_find_entry(dir, &dentry->d_name, &page); 252 if (!de) 253 return d_splice_alias(inode, dentry); 254 255 ino = le32_to_cpu(de->ino); 256 f2fs_dentry_kunmap(dir, page); 257 f2fs_put_page(page, 0); 258 259 inode = f2fs_iget(dir->i_sb, ino); 260 if (IS_ERR(inode)) 261 return ERR_CAST(inode); 262 263 if (f2fs_has_inline_dots(inode)) { 264 err = __recover_dot_dentries(inode, dir->i_ino); 265 if (err) 266 goto err_out; 267 } 268 return d_splice_alias(inode, dentry); 269 270 err_out: 271 iget_failed(inode); 272 return ERR_PTR(err); 273 } 274 275 static int f2fs_unlink(struct inode *dir, struct dentry *dentry) 276 { 277 struct f2fs_sb_info *sbi = F2FS_I_SB(dir); 278 struct inode *inode = d_inode(dentry); 279 struct f2fs_dir_entry *de; 280 struct page *page; 281 int err = -ENOENT; 282 283 trace_f2fs_unlink_enter(dir, dentry); 284 f2fs_balance_fs(sbi); 285 286 de = f2fs_find_entry(dir, &dentry->d_name, &page); 287 if (!de) 288 goto fail; 289 290 f2fs_lock_op(sbi); 291 err = acquire_orphan_inode(sbi); 292 if (err) { 293 f2fs_unlock_op(sbi); 294 f2fs_dentry_kunmap(dir, page); 295 f2fs_put_page(page, 0); 296 goto fail; 297 } 298 f2fs_delete_entry(de, page, dir, inode); 299 f2fs_unlock_op(sbi); 300 301 /* In order to evict this inode, we set it dirty */ 302 mark_inode_dirty(inode); 303 304 if (IS_DIRSYNC(dir)) 305 f2fs_sync_fs(sbi->sb, 1); 306 fail: 307 trace_f2fs_unlink_exit(inode, err); 308 return err; 309 } 310 311 static const char *f2fs_follow_link(struct dentry *dentry, void **cookie) 312 { 313 const char *link = page_follow_link_light(dentry, cookie); 314 if (!IS_ERR(link) && !*link) { 315 /* this is broken symlink case */ 316 page_put_link(NULL, *cookie); 317 link = ERR_PTR(-ENOENT); 318 } 319 return link; 320 } 321 322 static int f2fs_symlink(struct inode *dir, struct dentry *dentry, 323 const char *symname) 324 { 325 struct f2fs_sb_info *sbi = F2FS_I_SB(dir); 326 struct inode *inode; 327 size_t len = strlen(symname); 328 size_t p_len; 329 char *p_str; 330 struct f2fs_str disk_link = FSTR_INIT(NULL, 0); 331 struct f2fs_encrypted_symlink_data *sd = NULL; 332 int err; 333 334 if (len > dir->i_sb->s_blocksize) 335 return -ENAMETOOLONG; 336 337 f2fs_balance_fs(sbi); 338 339 inode = f2fs_new_inode(dir, S_IFLNK | S_IRWXUGO); 340 if (IS_ERR(inode)) 341 return PTR_ERR(inode); 342 343 if (f2fs_encrypted_inode(inode)) 344 inode->i_op = &f2fs_encrypted_symlink_inode_operations; 345 else 346 inode->i_op = &f2fs_symlink_inode_operations; 347 inode->i_mapping->a_ops = &f2fs_dblock_aops; 348 349 f2fs_lock_op(sbi); 350 err = f2fs_add_link(dentry, inode); 351 if (err) 352 goto out; 353 f2fs_unlock_op(sbi); 354 alloc_nid_done(sbi, inode->i_ino); 355 356 if (f2fs_encrypted_inode(dir)) { 357 struct qstr istr = QSTR_INIT(symname, len); 358 359 err = f2fs_get_encryption_info(inode); 360 if (err) 361 goto err_out; 362 363 err = f2fs_fname_crypto_alloc_buffer(inode, len, &disk_link); 364 if (err) 365 goto err_out; 366 367 err = f2fs_fname_usr_to_disk(inode, &istr, &disk_link); 368 if (err < 0) 369 goto err_out; 370 371 p_len = encrypted_symlink_data_len(disk_link.len) + 1; 372 373 if (p_len > dir->i_sb->s_blocksize) { 374 err = -ENAMETOOLONG; 375 goto err_out; 376 } 377 378 sd = kzalloc(p_len, GFP_NOFS); 379 if (!sd) { 380 err = -ENOMEM; 381 goto err_out; 382 } 383 memcpy(sd->encrypted_path, disk_link.name, disk_link.len); 384 sd->len = cpu_to_le16(disk_link.len); 385 p_str = (char *)sd; 386 } else { 387 p_len = len + 1; 388 p_str = (char *)symname; 389 } 390 391 err = page_symlink(inode, p_str, p_len); 392 393 err_out: 394 d_instantiate(dentry, inode); 395 unlock_new_inode(inode); 396 397 /* 398 * Let's flush symlink data in order to avoid broken symlink as much as 399 * possible. Nevertheless, fsyncing is the best way, but there is no 400 * way to get a file descriptor in order to flush that. 401 * 402 * Note that, it needs to do dir->fsync to make this recoverable. 403 * If the symlink path is stored into inline_data, there is no 404 * performance regression. 405 */ 406 if (!err) 407 filemap_write_and_wait_range(inode->i_mapping, 0, p_len - 1); 408 409 if (IS_DIRSYNC(dir)) 410 f2fs_sync_fs(sbi->sb, 1); 411 412 kfree(sd); 413 f2fs_fname_crypto_free_buffer(&disk_link); 414 return err; 415 out: 416 handle_failed_inode(inode); 417 return err; 418 } 419 420 static int f2fs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode) 421 { 422 struct f2fs_sb_info *sbi = F2FS_I_SB(dir); 423 struct inode *inode; 424 int err; 425 426 f2fs_balance_fs(sbi); 427 428 inode = f2fs_new_inode(dir, S_IFDIR | mode); 429 if (IS_ERR(inode)) 430 return PTR_ERR(inode); 431 432 inode->i_op = &f2fs_dir_inode_operations; 433 inode->i_fop = &f2fs_dir_operations; 434 inode->i_mapping->a_ops = &f2fs_dblock_aops; 435 mapping_set_gfp_mask(inode->i_mapping, GFP_F2FS_HIGH_ZERO); 436 437 set_inode_flag(F2FS_I(inode), FI_INC_LINK); 438 f2fs_lock_op(sbi); 439 err = f2fs_add_link(dentry, inode); 440 if (err) 441 goto out_fail; 442 f2fs_unlock_op(sbi); 443 444 alloc_nid_done(sbi, inode->i_ino); 445 446 d_instantiate(dentry, inode); 447 unlock_new_inode(inode); 448 449 if (IS_DIRSYNC(dir)) 450 f2fs_sync_fs(sbi->sb, 1); 451 return 0; 452 453 out_fail: 454 clear_inode_flag(F2FS_I(inode), FI_INC_LINK); 455 handle_failed_inode(inode); 456 return err; 457 } 458 459 static int f2fs_rmdir(struct inode *dir, struct dentry *dentry) 460 { 461 struct inode *inode = d_inode(dentry); 462 if (f2fs_empty_dir(inode)) 463 return f2fs_unlink(dir, dentry); 464 return -ENOTEMPTY; 465 } 466 467 static int f2fs_mknod(struct inode *dir, struct dentry *dentry, 468 umode_t mode, dev_t rdev) 469 { 470 struct f2fs_sb_info *sbi = F2FS_I_SB(dir); 471 struct inode *inode; 472 int err = 0; 473 474 if (!new_valid_dev(rdev)) 475 return -EINVAL; 476 477 f2fs_balance_fs(sbi); 478 479 inode = f2fs_new_inode(dir, mode); 480 if (IS_ERR(inode)) 481 return PTR_ERR(inode); 482 483 init_special_inode(inode, inode->i_mode, rdev); 484 inode->i_op = &f2fs_special_inode_operations; 485 486 f2fs_lock_op(sbi); 487 err = f2fs_add_link(dentry, inode); 488 if (err) 489 goto out; 490 f2fs_unlock_op(sbi); 491 492 alloc_nid_done(sbi, inode->i_ino); 493 494 d_instantiate(dentry, inode); 495 unlock_new_inode(inode); 496 497 if (IS_DIRSYNC(dir)) 498 f2fs_sync_fs(sbi->sb, 1); 499 return 0; 500 out: 501 handle_failed_inode(inode); 502 return err; 503 } 504 505 static int __f2fs_tmpfile(struct inode *dir, struct dentry *dentry, 506 umode_t mode, struct inode **whiteout) 507 { 508 struct f2fs_sb_info *sbi = F2FS_I_SB(dir); 509 struct inode *inode; 510 int err; 511 512 if (!whiteout) 513 f2fs_balance_fs(sbi); 514 515 inode = f2fs_new_inode(dir, mode); 516 if (IS_ERR(inode)) 517 return PTR_ERR(inode); 518 519 if (whiteout) { 520 init_special_inode(inode, inode->i_mode, WHITEOUT_DEV); 521 inode->i_op = &f2fs_special_inode_operations; 522 } else { 523 inode->i_op = &f2fs_file_inode_operations; 524 inode->i_fop = &f2fs_file_operations; 525 inode->i_mapping->a_ops = &f2fs_dblock_aops; 526 } 527 528 f2fs_lock_op(sbi); 529 err = acquire_orphan_inode(sbi); 530 if (err) 531 goto out; 532 533 err = f2fs_do_tmpfile(inode, dir); 534 if (err) 535 goto release_out; 536 537 /* 538 * add this non-linked tmpfile to orphan list, in this way we could 539 * remove all unused data of tmpfile after abnormal power-off. 540 */ 541 add_orphan_inode(sbi, inode->i_ino); 542 f2fs_unlock_op(sbi); 543 544 alloc_nid_done(sbi, inode->i_ino); 545 546 if (whiteout) { 547 inode_dec_link_count(inode); 548 *whiteout = inode; 549 } else { 550 d_tmpfile(dentry, inode); 551 } 552 unlock_new_inode(inode); 553 return 0; 554 555 release_out: 556 release_orphan_inode(sbi); 557 out: 558 handle_failed_inode(inode); 559 return err; 560 } 561 562 static int f2fs_tmpfile(struct inode *dir, struct dentry *dentry, umode_t mode) 563 { 564 if (f2fs_encrypted_inode(dir)) { 565 int err = f2fs_get_encryption_info(dir); 566 if (err) 567 return err; 568 } 569 570 return __f2fs_tmpfile(dir, dentry, mode, NULL); 571 } 572 573 static int f2fs_create_whiteout(struct inode *dir, struct inode **whiteout) 574 { 575 return __f2fs_tmpfile(dir, NULL, S_IFCHR | WHITEOUT_MODE, whiteout); 576 } 577 578 static int f2fs_rename(struct inode *old_dir, struct dentry *old_dentry, 579 struct inode *new_dir, struct dentry *new_dentry, 580 unsigned int flags) 581 { 582 struct f2fs_sb_info *sbi = F2FS_I_SB(old_dir); 583 struct inode *old_inode = d_inode(old_dentry); 584 struct inode *new_inode = d_inode(new_dentry); 585 struct inode *whiteout = NULL; 586 struct page *old_dir_page; 587 struct page *old_page, *new_page = NULL; 588 struct f2fs_dir_entry *old_dir_entry = NULL; 589 struct f2fs_dir_entry *old_entry; 590 struct f2fs_dir_entry *new_entry; 591 int err = -ENOENT; 592 593 if ((old_dir != new_dir) && f2fs_encrypted_inode(new_dir) && 594 !f2fs_is_child_context_consistent_with_parent(new_dir, 595 old_inode)) { 596 err = -EPERM; 597 goto out; 598 } 599 600 f2fs_balance_fs(sbi); 601 602 old_entry = f2fs_find_entry(old_dir, &old_dentry->d_name, &old_page); 603 if (!old_entry) 604 goto out; 605 606 if (S_ISDIR(old_inode->i_mode)) { 607 err = -EIO; 608 old_dir_entry = f2fs_parent_dir(old_inode, &old_dir_page); 609 if (!old_dir_entry) 610 goto out_old; 611 } 612 613 if (flags & RENAME_WHITEOUT) { 614 err = f2fs_create_whiteout(old_dir, &whiteout); 615 if (err) 616 goto out_dir; 617 } 618 619 if (new_inode) { 620 621 err = -ENOTEMPTY; 622 if (old_dir_entry && !f2fs_empty_dir(new_inode)) 623 goto out_whiteout; 624 625 err = -ENOENT; 626 new_entry = f2fs_find_entry(new_dir, &new_dentry->d_name, 627 &new_page); 628 if (!new_entry) 629 goto out_whiteout; 630 631 f2fs_lock_op(sbi); 632 633 err = acquire_orphan_inode(sbi); 634 if (err) 635 goto put_out_dir; 636 637 if (update_dent_inode(old_inode, new_inode, 638 &new_dentry->d_name)) { 639 release_orphan_inode(sbi); 640 goto put_out_dir; 641 } 642 643 f2fs_set_link(new_dir, new_entry, new_page, old_inode); 644 645 new_inode->i_ctime = CURRENT_TIME; 646 down_write(&F2FS_I(new_inode)->i_sem); 647 if (old_dir_entry) 648 drop_nlink(new_inode); 649 drop_nlink(new_inode); 650 up_write(&F2FS_I(new_inode)->i_sem); 651 652 mark_inode_dirty(new_inode); 653 654 if (!new_inode->i_nlink) 655 add_orphan_inode(sbi, new_inode->i_ino); 656 else 657 release_orphan_inode(sbi); 658 659 update_inode_page(old_inode); 660 update_inode_page(new_inode); 661 } else { 662 f2fs_lock_op(sbi); 663 664 err = f2fs_add_link(new_dentry, old_inode); 665 if (err) { 666 f2fs_unlock_op(sbi); 667 goto out_whiteout; 668 } 669 670 if (old_dir_entry) { 671 inc_nlink(new_dir); 672 update_inode_page(new_dir); 673 } 674 } 675 676 down_write(&F2FS_I(old_inode)->i_sem); 677 file_lost_pino(old_inode); 678 if (new_inode && file_enc_name(new_inode)) 679 file_set_enc_name(old_inode); 680 up_write(&F2FS_I(old_inode)->i_sem); 681 682 old_inode->i_ctime = CURRENT_TIME; 683 mark_inode_dirty(old_inode); 684 685 f2fs_delete_entry(old_entry, old_page, old_dir, NULL); 686 687 if (whiteout) { 688 whiteout->i_state |= I_LINKABLE; 689 set_inode_flag(F2FS_I(whiteout), FI_INC_LINK); 690 err = f2fs_add_link(old_dentry, whiteout); 691 if (err) 692 goto put_out_dir; 693 whiteout->i_state &= ~I_LINKABLE; 694 iput(whiteout); 695 } 696 697 if (old_dir_entry) { 698 if (old_dir != new_dir && !whiteout) { 699 f2fs_set_link(old_inode, old_dir_entry, 700 old_dir_page, new_dir); 701 update_inode_page(old_inode); 702 } else { 703 f2fs_dentry_kunmap(old_inode, old_dir_page); 704 f2fs_put_page(old_dir_page, 0); 705 } 706 drop_nlink(old_dir); 707 mark_inode_dirty(old_dir); 708 update_inode_page(old_dir); 709 } 710 711 f2fs_unlock_op(sbi); 712 713 if (IS_DIRSYNC(old_dir) || IS_DIRSYNC(new_dir)) 714 f2fs_sync_fs(sbi->sb, 1); 715 return 0; 716 717 put_out_dir: 718 f2fs_unlock_op(sbi); 719 if (new_page) { 720 f2fs_dentry_kunmap(new_dir, new_page); 721 f2fs_put_page(new_page, 0); 722 } 723 out_whiteout: 724 if (whiteout) 725 iput(whiteout); 726 out_dir: 727 if (old_dir_entry) { 728 f2fs_dentry_kunmap(old_inode, old_dir_page); 729 f2fs_put_page(old_dir_page, 0); 730 } 731 out_old: 732 f2fs_dentry_kunmap(old_dir, old_page); 733 f2fs_put_page(old_page, 0); 734 out: 735 return err; 736 } 737 738 static int f2fs_cross_rename(struct inode *old_dir, struct dentry *old_dentry, 739 struct inode *new_dir, struct dentry *new_dentry) 740 { 741 struct f2fs_sb_info *sbi = F2FS_I_SB(old_dir); 742 struct inode *old_inode = d_inode(old_dentry); 743 struct inode *new_inode = d_inode(new_dentry); 744 struct page *old_dir_page, *new_dir_page; 745 struct page *old_page, *new_page; 746 struct f2fs_dir_entry *old_dir_entry = NULL, *new_dir_entry = NULL; 747 struct f2fs_dir_entry *old_entry, *new_entry; 748 int old_nlink = 0, new_nlink = 0; 749 int err = -ENOENT; 750 751 if ((f2fs_encrypted_inode(old_dir) || f2fs_encrypted_inode(new_dir)) && 752 (old_dir != new_dir) && 753 (!f2fs_is_child_context_consistent_with_parent(new_dir, 754 old_inode) || 755 !f2fs_is_child_context_consistent_with_parent(old_dir, 756 new_inode))) 757 return -EPERM; 758 759 f2fs_balance_fs(sbi); 760 761 old_entry = f2fs_find_entry(old_dir, &old_dentry->d_name, &old_page); 762 if (!old_entry) 763 goto out; 764 765 new_entry = f2fs_find_entry(new_dir, &new_dentry->d_name, &new_page); 766 if (!new_entry) 767 goto out_old; 768 769 /* prepare for updating ".." directory entry info later */ 770 if (old_dir != new_dir) { 771 if (S_ISDIR(old_inode->i_mode)) { 772 err = -EIO; 773 old_dir_entry = f2fs_parent_dir(old_inode, 774 &old_dir_page); 775 if (!old_dir_entry) 776 goto out_new; 777 } 778 779 if (S_ISDIR(new_inode->i_mode)) { 780 err = -EIO; 781 new_dir_entry = f2fs_parent_dir(new_inode, 782 &new_dir_page); 783 if (!new_dir_entry) 784 goto out_old_dir; 785 } 786 } 787 788 /* 789 * If cross rename between file and directory those are not 790 * in the same directory, we will inc nlink of file's parent 791 * later, so we should check upper boundary of its nlink. 792 */ 793 if ((!old_dir_entry || !new_dir_entry) && 794 old_dir_entry != new_dir_entry) { 795 old_nlink = old_dir_entry ? -1 : 1; 796 new_nlink = -old_nlink; 797 err = -EMLINK; 798 if ((old_nlink > 0 && old_inode->i_nlink >= F2FS_LINK_MAX) || 799 (new_nlink > 0 && new_inode->i_nlink >= F2FS_LINK_MAX)) 800 goto out_new_dir; 801 } 802 803 f2fs_lock_op(sbi); 804 805 err = update_dent_inode(old_inode, new_inode, &new_dentry->d_name); 806 if (err) 807 goto out_unlock; 808 if (file_enc_name(new_inode)) 809 file_set_enc_name(old_inode); 810 811 err = update_dent_inode(new_inode, old_inode, &old_dentry->d_name); 812 if (err) 813 goto out_undo; 814 if (file_enc_name(old_inode)) 815 file_set_enc_name(new_inode); 816 817 /* update ".." directory entry info of old dentry */ 818 if (old_dir_entry) 819 f2fs_set_link(old_inode, old_dir_entry, old_dir_page, new_dir); 820 821 /* update ".." directory entry info of new dentry */ 822 if (new_dir_entry) 823 f2fs_set_link(new_inode, new_dir_entry, new_dir_page, old_dir); 824 825 /* update directory entry info of old dir inode */ 826 f2fs_set_link(old_dir, old_entry, old_page, new_inode); 827 828 down_write(&F2FS_I(old_inode)->i_sem); 829 file_lost_pino(old_inode); 830 up_write(&F2FS_I(old_inode)->i_sem); 831 832 update_inode_page(old_inode); 833 834 old_dir->i_ctime = CURRENT_TIME; 835 if (old_nlink) { 836 down_write(&F2FS_I(old_dir)->i_sem); 837 if (old_nlink < 0) 838 drop_nlink(old_dir); 839 else 840 inc_nlink(old_dir); 841 up_write(&F2FS_I(old_dir)->i_sem); 842 } 843 mark_inode_dirty(old_dir); 844 update_inode_page(old_dir); 845 846 /* update directory entry info of new dir inode */ 847 f2fs_set_link(new_dir, new_entry, new_page, old_inode); 848 849 down_write(&F2FS_I(new_inode)->i_sem); 850 file_lost_pino(new_inode); 851 up_write(&F2FS_I(new_inode)->i_sem); 852 853 update_inode_page(new_inode); 854 855 new_dir->i_ctime = CURRENT_TIME; 856 if (new_nlink) { 857 down_write(&F2FS_I(new_dir)->i_sem); 858 if (new_nlink < 0) 859 drop_nlink(new_dir); 860 else 861 inc_nlink(new_dir); 862 up_write(&F2FS_I(new_dir)->i_sem); 863 } 864 mark_inode_dirty(new_dir); 865 update_inode_page(new_dir); 866 867 f2fs_unlock_op(sbi); 868 869 if (IS_DIRSYNC(old_dir) || IS_DIRSYNC(new_dir)) 870 f2fs_sync_fs(sbi->sb, 1); 871 return 0; 872 out_undo: 873 /* 874 * Still we may fail to recover name info of f2fs_inode here 875 * Drop it, once its name is set as encrypted 876 */ 877 update_dent_inode(old_inode, old_inode, &old_dentry->d_name); 878 out_unlock: 879 f2fs_unlock_op(sbi); 880 out_new_dir: 881 if (new_dir_entry) { 882 f2fs_dentry_kunmap(new_inode, new_dir_page); 883 f2fs_put_page(new_dir_page, 0); 884 } 885 out_old_dir: 886 if (old_dir_entry) { 887 f2fs_dentry_kunmap(old_inode, old_dir_page); 888 f2fs_put_page(old_dir_page, 0); 889 } 890 out_new: 891 f2fs_dentry_kunmap(new_dir, new_page); 892 f2fs_put_page(new_page, 0); 893 out_old: 894 f2fs_dentry_kunmap(old_dir, old_page); 895 f2fs_put_page(old_page, 0); 896 out: 897 return err; 898 } 899 900 static int f2fs_rename2(struct inode *old_dir, struct dentry *old_dentry, 901 struct inode *new_dir, struct dentry *new_dentry, 902 unsigned int flags) 903 { 904 if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE | RENAME_WHITEOUT)) 905 return -EINVAL; 906 907 if (flags & RENAME_EXCHANGE) { 908 return f2fs_cross_rename(old_dir, old_dentry, 909 new_dir, new_dentry); 910 } 911 /* 912 * VFS has already handled the new dentry existence case, 913 * here, we just deal with "RENAME_NOREPLACE" as regular rename. 914 */ 915 return f2fs_rename(old_dir, old_dentry, new_dir, new_dentry, flags); 916 } 917 918 #ifdef CONFIG_F2FS_FS_ENCRYPTION 919 static const char *f2fs_encrypted_follow_link(struct dentry *dentry, void **cookie) 920 { 921 struct page *cpage = NULL; 922 char *caddr, *paddr = NULL; 923 struct f2fs_str cstr; 924 struct f2fs_str pstr = FSTR_INIT(NULL, 0); 925 struct inode *inode = d_inode(dentry); 926 struct f2fs_encrypted_symlink_data *sd; 927 loff_t size = min_t(loff_t, i_size_read(inode), PAGE_SIZE - 1); 928 u32 max_size = inode->i_sb->s_blocksize; 929 int res; 930 931 res = f2fs_get_encryption_info(inode); 932 if (res) 933 return ERR_PTR(res); 934 935 cpage = read_mapping_page(inode->i_mapping, 0, NULL); 936 if (IS_ERR(cpage)) 937 return ERR_CAST(cpage); 938 caddr = kmap(cpage); 939 caddr[size] = 0; 940 941 /* Symlink is encrypted */ 942 sd = (struct f2fs_encrypted_symlink_data *)caddr; 943 cstr.name = sd->encrypted_path; 944 cstr.len = le16_to_cpu(sd->len); 945 946 /* this is broken symlink case */ 947 if (cstr.name[0] == 0 && cstr.len == 0) { 948 res = -ENOENT; 949 goto errout; 950 } 951 952 if ((cstr.len + sizeof(struct f2fs_encrypted_symlink_data) - 1) > 953 max_size) { 954 /* Symlink data on the disk is corrupted */ 955 res = -EIO; 956 goto errout; 957 } 958 res = f2fs_fname_crypto_alloc_buffer(inode, cstr.len, &pstr); 959 if (res) 960 goto errout; 961 962 res = f2fs_fname_disk_to_usr(inode, NULL, &cstr, &pstr); 963 if (res < 0) 964 goto errout; 965 966 paddr = pstr.name; 967 968 /* Null-terminate the name */ 969 paddr[res] = '\0'; 970 971 kunmap(cpage); 972 page_cache_release(cpage); 973 return *cookie = paddr; 974 errout: 975 f2fs_fname_crypto_free_buffer(&pstr); 976 kunmap(cpage); 977 page_cache_release(cpage); 978 return ERR_PTR(res); 979 } 980 981 const struct inode_operations f2fs_encrypted_symlink_inode_operations = { 982 .readlink = generic_readlink, 983 .follow_link = f2fs_encrypted_follow_link, 984 .put_link = kfree_put_link, 985 .getattr = f2fs_getattr, 986 .setattr = f2fs_setattr, 987 .setxattr = generic_setxattr, 988 .getxattr = generic_getxattr, 989 .listxattr = f2fs_listxattr, 990 .removexattr = generic_removexattr, 991 }; 992 #endif 993 994 const struct inode_operations f2fs_dir_inode_operations = { 995 .create = f2fs_create, 996 .lookup = f2fs_lookup, 997 .link = f2fs_link, 998 .unlink = f2fs_unlink, 999 .symlink = f2fs_symlink, 1000 .mkdir = f2fs_mkdir, 1001 .rmdir = f2fs_rmdir, 1002 .mknod = f2fs_mknod, 1003 .rename2 = f2fs_rename2, 1004 .tmpfile = f2fs_tmpfile, 1005 .getattr = f2fs_getattr, 1006 .setattr = f2fs_setattr, 1007 .get_acl = f2fs_get_acl, 1008 .set_acl = f2fs_set_acl, 1009 #ifdef CONFIG_F2FS_FS_XATTR 1010 .setxattr = generic_setxattr, 1011 .getxattr = generic_getxattr, 1012 .listxattr = f2fs_listxattr, 1013 .removexattr = generic_removexattr, 1014 #endif 1015 }; 1016 1017 const struct inode_operations f2fs_symlink_inode_operations = { 1018 .readlink = generic_readlink, 1019 .follow_link = f2fs_follow_link, 1020 .put_link = page_put_link, 1021 .getattr = f2fs_getattr, 1022 .setattr = f2fs_setattr, 1023 #ifdef CONFIG_F2FS_FS_XATTR 1024 .setxattr = generic_setxattr, 1025 .getxattr = generic_getxattr, 1026 .listxattr = f2fs_listxattr, 1027 .removexattr = generic_removexattr, 1028 #endif 1029 }; 1030 1031 const struct inode_operations f2fs_special_inode_operations = { 1032 .getattr = f2fs_getattr, 1033 .setattr = f2fs_setattr, 1034 .get_acl = f2fs_get_acl, 1035 .set_acl = f2fs_set_acl, 1036 #ifdef CONFIG_F2FS_FS_XATTR 1037 .setxattr = generic_setxattr, 1038 .getxattr = generic_getxattr, 1039 .listxattr = f2fs_listxattr, 1040 .removexattr = generic_removexattr, 1041 #endif 1042 }; 1043