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 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 (!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 inode->i_generation = sbi->s_next_generation++; 54 55 err = insert_inode_locked(inode); 56 if (err) { 57 err = -EINVAL; 58 goto fail; 59 } 60 61 if (f2fs_sb_has_project_quota(sbi->sb) && 62 (F2FS_I(dir)->i_flags & FS_PROJINHERIT_FL)) 63 F2FS_I(inode)->i_projid = F2FS_I(dir)->i_projid; 64 else 65 F2FS_I(inode)->i_projid = make_kprojid(&init_user_ns, 66 F2FS_DEF_PROJID); 67 68 err = dquot_initialize(inode); 69 if (err) 70 goto fail_drop; 71 72 err = dquot_alloc_inode(inode); 73 if (err) 74 goto fail_drop; 75 76 /* If the directory encrypted, then we should encrypt the inode. */ 77 if (f2fs_encrypted_inode(dir) && f2fs_may_encrypt(inode)) 78 f2fs_set_encrypted_inode(inode); 79 80 set_inode_flag(inode, FI_NEW_INODE); 81 82 if (f2fs_sb_has_extra_attr(sbi->sb)) { 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 if (test_opt(sbi, INLINE_DATA) && f2fs_may_inline_data(inode)) 90 set_inode_flag(inode, FI_INLINE_DATA); 91 if (f2fs_may_inline_dentry(inode)) 92 set_inode_flag(inode, FI_INLINE_DENTRY); 93 94 f2fs_init_extent_tree(inode, NULL); 95 96 stat_inc_inline_xattr(inode); 97 stat_inc_inline_inode(inode); 98 stat_inc_inline_dir(inode); 99 100 F2FS_I(inode)->i_flags = 101 f2fs_mask_flags(mode, F2FS_I(dir)->i_flags & F2FS_FL_INHERITED); 102 103 if (S_ISDIR(inode->i_mode)) 104 F2FS_I(inode)->i_flags |= FS_INDEX_FL; 105 106 if (F2FS_I(inode)->i_flags & FS_PROJINHERIT_FL) 107 set_inode_flag(inode, FI_PROJ_INHERIT); 108 109 trace_f2fs_new_inode(inode, 0); 110 return inode; 111 112 fail: 113 trace_f2fs_new_inode(inode, err); 114 make_bad_inode(inode); 115 if (nid_free) 116 set_inode_flag(inode, FI_FREE_NID); 117 iput(inode); 118 return ERR_PTR(err); 119 fail_drop: 120 trace_f2fs_new_inode(inode, err); 121 dquot_drop(inode); 122 inode->i_flags |= S_NOQUOTA; 123 if (nid_free) 124 set_inode_flag(inode, FI_FREE_NID); 125 clear_nlink(inode); 126 unlock_new_inode(inode); 127 iput(inode); 128 return ERR_PTR(err); 129 } 130 131 static int is_multimedia_file(const unsigned char *s, const char *sub) 132 { 133 size_t slen = strlen(s); 134 size_t sublen = strlen(sub); 135 int i; 136 137 /* 138 * filename format of multimedia file should be defined as: 139 * "filename + '.' + extension + (optional: '.' + temp extension)". 140 */ 141 if (slen < sublen + 2) 142 return 0; 143 144 for (i = 1; i < slen - sublen; i++) { 145 if (s[i] != '.') 146 continue; 147 if (!strncasecmp(s + i + 1, sub, sublen)) 148 return 1; 149 } 150 151 return 0; 152 } 153 154 /* 155 * Set multimedia files as cold files for hot/cold data separation 156 */ 157 static inline void set_cold_files(struct f2fs_sb_info *sbi, struct inode *inode, 158 const unsigned char *name) 159 { 160 int i; 161 __u8 (*extlist)[8] = sbi->raw_super->extension_list; 162 163 int count = le32_to_cpu(sbi->raw_super->extension_count); 164 for (i = 0; i < count; i++) { 165 if (is_multimedia_file(name, extlist[i])) { 166 file_set_cold(inode); 167 break; 168 } 169 } 170 } 171 172 static int f2fs_create(struct inode *dir, struct dentry *dentry, umode_t mode, 173 bool excl) 174 { 175 struct f2fs_sb_info *sbi = F2FS_I_SB(dir); 176 struct inode *inode; 177 nid_t ino = 0; 178 int err; 179 180 err = dquot_initialize(dir); 181 if (err) 182 return err; 183 184 inode = f2fs_new_inode(dir, mode); 185 if (IS_ERR(inode)) 186 return PTR_ERR(inode); 187 188 if (!test_opt(sbi, DISABLE_EXT_IDENTIFY)) 189 set_cold_files(sbi, inode, dentry->d_name.name); 190 191 inode->i_op = &f2fs_file_inode_operations; 192 inode->i_fop = &f2fs_file_operations; 193 inode->i_mapping->a_ops = &f2fs_dblock_aops; 194 ino = inode->i_ino; 195 196 f2fs_lock_op(sbi); 197 err = f2fs_add_link(dentry, inode); 198 if (err) 199 goto out; 200 f2fs_unlock_op(sbi); 201 202 alloc_nid_done(sbi, ino); 203 204 d_instantiate(dentry, inode); 205 unlock_new_inode(inode); 206 207 if (IS_DIRSYNC(dir)) 208 f2fs_sync_fs(sbi->sb, 1); 209 210 f2fs_balance_fs(sbi, true); 211 return 0; 212 out: 213 handle_failed_inode(inode); 214 return err; 215 } 216 217 static int f2fs_link(struct dentry *old_dentry, struct inode *dir, 218 struct dentry *dentry) 219 { 220 struct inode *inode = d_inode(old_dentry); 221 struct f2fs_sb_info *sbi = F2FS_I_SB(dir); 222 int err; 223 224 if (f2fs_encrypted_inode(dir) && 225 !fscrypt_has_permitted_context(dir, inode)) 226 return -EPERM; 227 228 if (is_inode_flag_set(dir, FI_PROJ_INHERIT) && 229 (!projid_eq(F2FS_I(dir)->i_projid, 230 F2FS_I(old_dentry->d_inode)->i_projid))) 231 return -EXDEV; 232 233 err = dquot_initialize(dir); 234 if (err) 235 return err; 236 237 f2fs_balance_fs(sbi, true); 238 239 inode->i_ctime = current_time(inode); 240 ihold(inode); 241 242 set_inode_flag(inode, FI_INC_LINK); 243 f2fs_lock_op(sbi); 244 err = f2fs_add_link(dentry, inode); 245 if (err) 246 goto out; 247 f2fs_unlock_op(sbi); 248 249 d_instantiate(dentry, inode); 250 251 if (IS_DIRSYNC(dir)) 252 f2fs_sync_fs(sbi->sb, 1); 253 return 0; 254 out: 255 clear_inode_flag(inode, FI_INC_LINK); 256 iput(inode); 257 f2fs_unlock_op(sbi); 258 return err; 259 } 260 261 struct dentry *f2fs_get_parent(struct dentry *child) 262 { 263 struct qstr dotdot = QSTR_INIT("..", 2); 264 struct page *page; 265 unsigned long ino = f2fs_inode_by_name(d_inode(child), &dotdot, &page); 266 if (!ino) { 267 if (IS_ERR(page)) 268 return ERR_CAST(page); 269 return ERR_PTR(-ENOENT); 270 } 271 return d_obtain_alias(f2fs_iget(child->d_sb, ino)); 272 } 273 274 static int __recover_dot_dentries(struct inode *dir, nid_t pino) 275 { 276 struct f2fs_sb_info *sbi = F2FS_I_SB(dir); 277 struct qstr dot = QSTR_INIT(".", 1); 278 struct qstr dotdot = QSTR_INIT("..", 2); 279 struct f2fs_dir_entry *de; 280 struct page *page; 281 int err = 0; 282 283 if (f2fs_readonly(sbi->sb)) { 284 f2fs_msg(sbi->sb, KERN_INFO, 285 "skip recovering inline_dots inode (ino:%lu, pino:%u) " 286 "in readonly mountpoint", dir->i_ino, pino); 287 return 0; 288 } 289 290 err = dquot_initialize(dir); 291 if (err) 292 return err; 293 294 f2fs_balance_fs(sbi, true); 295 296 f2fs_lock_op(sbi); 297 298 de = f2fs_find_entry(dir, &dot, &page); 299 if (de) { 300 f2fs_dentry_kunmap(dir, page); 301 f2fs_put_page(page, 0); 302 } else if (IS_ERR(page)) { 303 err = PTR_ERR(page); 304 goto out; 305 } else { 306 err = __f2fs_add_link(dir, &dot, NULL, dir->i_ino, S_IFDIR); 307 if (err) 308 goto out; 309 } 310 311 de = f2fs_find_entry(dir, &dotdot, &page); 312 if (de) { 313 f2fs_dentry_kunmap(dir, page); 314 f2fs_put_page(page, 0); 315 } else if (IS_ERR(page)) { 316 err = PTR_ERR(page); 317 } else { 318 err = __f2fs_add_link(dir, &dotdot, NULL, pino, S_IFDIR); 319 } 320 out: 321 if (!err) 322 clear_inode_flag(dir, FI_INLINE_DOTS); 323 324 f2fs_unlock_op(sbi); 325 return err; 326 } 327 328 static struct dentry *f2fs_lookup(struct inode *dir, struct dentry *dentry, 329 unsigned int flags) 330 { 331 struct inode *inode = NULL; 332 struct f2fs_dir_entry *de; 333 struct page *page; 334 nid_t ino; 335 int err = 0; 336 unsigned int root_ino = F2FS_ROOT_INO(F2FS_I_SB(dir)); 337 338 if (f2fs_encrypted_inode(dir)) { 339 int res = fscrypt_get_encryption_info(dir); 340 341 /* 342 * DCACHE_ENCRYPTED_WITH_KEY is set if the dentry is 343 * created while the directory was encrypted and we 344 * don't have access to the key. 345 */ 346 if (fscrypt_has_encryption_key(dir)) 347 fscrypt_set_encrypted_dentry(dentry); 348 fscrypt_set_d_op(dentry); 349 if (res && res != -ENOKEY) 350 return ERR_PTR(res); 351 } 352 353 if (dentry->d_name.len > F2FS_NAME_LEN) 354 return ERR_PTR(-ENAMETOOLONG); 355 356 de = f2fs_find_entry(dir, &dentry->d_name, &page); 357 if (!de) { 358 if (IS_ERR(page)) 359 return (struct dentry *)page; 360 return d_splice_alias(inode, dentry); 361 } 362 363 ino = le32_to_cpu(de->ino); 364 f2fs_dentry_kunmap(dir, page); 365 f2fs_put_page(page, 0); 366 367 inode = f2fs_iget(dir->i_sb, ino); 368 if (IS_ERR(inode)) 369 return ERR_CAST(inode); 370 371 if ((dir->i_ino == root_ino) && f2fs_has_inline_dots(dir)) { 372 err = __recover_dot_dentries(dir, root_ino); 373 if (err) 374 goto err_out; 375 } 376 377 if (f2fs_has_inline_dots(inode)) { 378 err = __recover_dot_dentries(inode, dir->i_ino); 379 if (err) 380 goto err_out; 381 } 382 if (f2fs_encrypted_inode(dir) && 383 (S_ISDIR(inode->i_mode) || S_ISLNK(inode->i_mode)) && 384 !fscrypt_has_permitted_context(dir, inode)) { 385 f2fs_msg(inode->i_sb, KERN_WARNING, 386 "Inconsistent encryption contexts: %lu/%lu", 387 dir->i_ino, inode->i_ino); 388 err = -EPERM; 389 goto err_out; 390 } 391 return d_splice_alias(inode, dentry); 392 393 err_out: 394 iput(inode); 395 return ERR_PTR(err); 396 } 397 398 static int f2fs_unlink(struct inode *dir, struct dentry *dentry) 399 { 400 struct f2fs_sb_info *sbi = F2FS_I_SB(dir); 401 struct inode *inode = d_inode(dentry); 402 struct f2fs_dir_entry *de; 403 struct page *page; 404 int err = -ENOENT; 405 406 trace_f2fs_unlink_enter(dir, dentry); 407 408 err = dquot_initialize(dir); 409 if (err) 410 return err; 411 412 de = f2fs_find_entry(dir, &dentry->d_name, &page); 413 if (!de) { 414 if (IS_ERR(page)) 415 err = PTR_ERR(page); 416 goto fail; 417 } 418 419 f2fs_balance_fs(sbi, true); 420 421 f2fs_lock_op(sbi); 422 err = acquire_orphan_inode(sbi); 423 if (err) { 424 f2fs_unlock_op(sbi); 425 f2fs_dentry_kunmap(dir, page); 426 f2fs_put_page(page, 0); 427 goto fail; 428 } 429 f2fs_delete_entry(de, page, dir, inode); 430 f2fs_unlock_op(sbi); 431 432 if (IS_DIRSYNC(dir)) 433 f2fs_sync_fs(sbi->sb, 1); 434 fail: 435 trace_f2fs_unlink_exit(inode, err); 436 return err; 437 } 438 439 static const char *f2fs_get_link(struct dentry *dentry, 440 struct inode *inode, 441 struct delayed_call *done) 442 { 443 const char *link = page_get_link(dentry, inode, done); 444 if (!IS_ERR(link) && !*link) { 445 /* this is broken symlink case */ 446 do_delayed_call(done); 447 clear_delayed_call(done); 448 link = ERR_PTR(-ENOENT); 449 } 450 return link; 451 } 452 453 static int f2fs_symlink(struct inode *dir, struct dentry *dentry, 454 const char *symname) 455 { 456 struct f2fs_sb_info *sbi = F2FS_I_SB(dir); 457 struct inode *inode; 458 size_t len = strlen(symname); 459 struct fscrypt_str disk_link = FSTR_INIT((char *)symname, len + 1); 460 struct fscrypt_symlink_data *sd = NULL; 461 int err; 462 463 if (f2fs_encrypted_inode(dir)) { 464 err = fscrypt_get_encryption_info(dir); 465 if (err) 466 return err; 467 468 if (!fscrypt_has_encryption_key(dir)) 469 return -ENOKEY; 470 471 disk_link.len = (fscrypt_fname_encrypted_size(dir, len) + 472 sizeof(struct fscrypt_symlink_data)); 473 } 474 475 if (disk_link.len > dir->i_sb->s_blocksize) 476 return -ENAMETOOLONG; 477 478 err = dquot_initialize(dir); 479 if (err) 480 return err; 481 482 inode = f2fs_new_inode(dir, S_IFLNK | S_IRWXUGO); 483 if (IS_ERR(inode)) 484 return PTR_ERR(inode); 485 486 if (f2fs_encrypted_inode(inode)) 487 inode->i_op = &f2fs_encrypted_symlink_inode_operations; 488 else 489 inode->i_op = &f2fs_symlink_inode_operations; 490 inode_nohighmem(inode); 491 inode->i_mapping->a_ops = &f2fs_dblock_aops; 492 493 f2fs_lock_op(sbi); 494 err = f2fs_add_link(dentry, inode); 495 if (err) 496 goto out; 497 f2fs_unlock_op(sbi); 498 alloc_nid_done(sbi, inode->i_ino); 499 500 if (f2fs_encrypted_inode(inode)) { 501 struct qstr istr = QSTR_INIT(symname, len); 502 struct fscrypt_str ostr; 503 504 sd = kzalloc(disk_link.len, GFP_NOFS); 505 if (!sd) { 506 err = -ENOMEM; 507 goto err_out; 508 } 509 510 err = fscrypt_get_encryption_info(inode); 511 if (err) 512 goto err_out; 513 514 if (!fscrypt_has_encryption_key(inode)) { 515 err = -ENOKEY; 516 goto err_out; 517 } 518 519 ostr.name = sd->encrypted_path; 520 ostr.len = disk_link.len; 521 err = fscrypt_fname_usr_to_disk(inode, &istr, &ostr); 522 if (err) 523 goto err_out; 524 525 sd->len = cpu_to_le16(ostr.len); 526 disk_link.name = (char *)sd; 527 } 528 529 err = page_symlink(inode, disk_link.name, disk_link.len); 530 531 err_out: 532 d_instantiate(dentry, inode); 533 unlock_new_inode(inode); 534 535 /* 536 * Let's flush symlink data in order to avoid broken symlink as much as 537 * possible. Nevertheless, fsyncing is the best way, but there is no 538 * way to get a file descriptor in order to flush that. 539 * 540 * Note that, it needs to do dir->fsync to make this recoverable. 541 * If the symlink path is stored into inline_data, there is no 542 * performance regression. 543 */ 544 if (!err) { 545 filemap_write_and_wait_range(inode->i_mapping, 0, 546 disk_link.len - 1); 547 548 if (IS_DIRSYNC(dir)) 549 f2fs_sync_fs(sbi->sb, 1); 550 } else { 551 f2fs_unlink(dir, dentry); 552 } 553 554 kfree(sd); 555 556 f2fs_balance_fs(sbi, true); 557 return err; 558 out: 559 handle_failed_inode(inode); 560 return err; 561 } 562 563 static int f2fs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode) 564 { 565 struct f2fs_sb_info *sbi = F2FS_I_SB(dir); 566 struct inode *inode; 567 int err; 568 569 err = dquot_initialize(dir); 570 if (err) 571 return err; 572 573 inode = f2fs_new_inode(dir, S_IFDIR | mode); 574 if (IS_ERR(inode)) 575 return PTR_ERR(inode); 576 577 inode->i_op = &f2fs_dir_inode_operations; 578 inode->i_fop = &f2fs_dir_operations; 579 inode->i_mapping->a_ops = &f2fs_dblock_aops; 580 mapping_set_gfp_mask(inode->i_mapping, GFP_F2FS_HIGH_ZERO); 581 582 set_inode_flag(inode, FI_INC_LINK); 583 f2fs_lock_op(sbi); 584 err = f2fs_add_link(dentry, inode); 585 if (err) 586 goto out_fail; 587 f2fs_unlock_op(sbi); 588 589 alloc_nid_done(sbi, inode->i_ino); 590 591 d_instantiate(dentry, inode); 592 unlock_new_inode(inode); 593 594 if (IS_DIRSYNC(dir)) 595 f2fs_sync_fs(sbi->sb, 1); 596 597 f2fs_balance_fs(sbi, true); 598 return 0; 599 600 out_fail: 601 clear_inode_flag(inode, FI_INC_LINK); 602 handle_failed_inode(inode); 603 return err; 604 } 605 606 static int f2fs_rmdir(struct inode *dir, struct dentry *dentry) 607 { 608 struct inode *inode = d_inode(dentry); 609 if (f2fs_empty_dir(inode)) 610 return f2fs_unlink(dir, dentry); 611 return -ENOTEMPTY; 612 } 613 614 static int f2fs_mknod(struct inode *dir, struct dentry *dentry, 615 umode_t mode, dev_t rdev) 616 { 617 struct f2fs_sb_info *sbi = F2FS_I_SB(dir); 618 struct inode *inode; 619 int err = 0; 620 621 err = dquot_initialize(dir); 622 if (err) 623 return err; 624 625 inode = f2fs_new_inode(dir, mode); 626 if (IS_ERR(inode)) 627 return PTR_ERR(inode); 628 629 init_special_inode(inode, inode->i_mode, rdev); 630 inode->i_op = &f2fs_special_inode_operations; 631 632 f2fs_lock_op(sbi); 633 err = f2fs_add_link(dentry, inode); 634 if (err) 635 goto out; 636 f2fs_unlock_op(sbi); 637 638 alloc_nid_done(sbi, inode->i_ino); 639 640 d_instantiate(dentry, inode); 641 unlock_new_inode(inode); 642 643 if (IS_DIRSYNC(dir)) 644 f2fs_sync_fs(sbi->sb, 1); 645 646 f2fs_balance_fs(sbi, true); 647 return 0; 648 out: 649 handle_failed_inode(inode); 650 return err; 651 } 652 653 static int __f2fs_tmpfile(struct inode *dir, struct dentry *dentry, 654 umode_t mode, struct inode **whiteout) 655 { 656 struct f2fs_sb_info *sbi = F2FS_I_SB(dir); 657 struct inode *inode; 658 int err; 659 660 err = dquot_initialize(dir); 661 if (err) 662 return err; 663 664 inode = f2fs_new_inode(dir, mode); 665 if (IS_ERR(inode)) 666 return PTR_ERR(inode); 667 668 if (whiteout) { 669 init_special_inode(inode, inode->i_mode, WHITEOUT_DEV); 670 inode->i_op = &f2fs_special_inode_operations; 671 } else { 672 inode->i_op = &f2fs_file_inode_operations; 673 inode->i_fop = &f2fs_file_operations; 674 inode->i_mapping->a_ops = &f2fs_dblock_aops; 675 } 676 677 f2fs_lock_op(sbi); 678 err = acquire_orphan_inode(sbi); 679 if (err) 680 goto out; 681 682 err = f2fs_do_tmpfile(inode, dir); 683 if (err) 684 goto release_out; 685 686 /* 687 * add this non-linked tmpfile to orphan list, in this way we could 688 * remove all unused data of tmpfile after abnormal power-off. 689 */ 690 add_orphan_inode(inode); 691 alloc_nid_done(sbi, inode->i_ino); 692 693 if (whiteout) { 694 f2fs_i_links_write(inode, false); 695 *whiteout = inode; 696 } else { 697 d_tmpfile(dentry, inode); 698 } 699 /* link_count was changed by d_tmpfile as well. */ 700 f2fs_unlock_op(sbi); 701 unlock_new_inode(inode); 702 703 f2fs_balance_fs(sbi, true); 704 return 0; 705 706 release_out: 707 release_orphan_inode(sbi); 708 out: 709 handle_failed_inode(inode); 710 return err; 711 } 712 713 static int f2fs_tmpfile(struct inode *dir, struct dentry *dentry, umode_t mode) 714 { 715 if (f2fs_encrypted_inode(dir)) { 716 int err = fscrypt_get_encryption_info(dir); 717 if (err) 718 return err; 719 } 720 721 return __f2fs_tmpfile(dir, dentry, mode, NULL); 722 } 723 724 static int f2fs_create_whiteout(struct inode *dir, struct inode **whiteout) 725 { 726 return __f2fs_tmpfile(dir, NULL, S_IFCHR | WHITEOUT_MODE, whiteout); 727 } 728 729 static int f2fs_rename(struct inode *old_dir, struct dentry *old_dentry, 730 struct inode *new_dir, struct dentry *new_dentry, 731 unsigned int flags) 732 { 733 struct f2fs_sb_info *sbi = F2FS_I_SB(old_dir); 734 struct inode *old_inode = d_inode(old_dentry); 735 struct inode *new_inode = d_inode(new_dentry); 736 struct inode *whiteout = NULL; 737 struct page *old_dir_page; 738 struct page *old_page, *new_page = NULL; 739 struct f2fs_dir_entry *old_dir_entry = NULL; 740 struct f2fs_dir_entry *old_entry; 741 struct f2fs_dir_entry *new_entry; 742 bool is_old_inline = f2fs_has_inline_dentry(old_dir); 743 int err = -ENOENT; 744 745 if ((f2fs_encrypted_inode(old_dir) && 746 !fscrypt_has_encryption_key(old_dir)) || 747 (f2fs_encrypted_inode(new_dir) && 748 !fscrypt_has_encryption_key(new_dir))) 749 return -ENOKEY; 750 751 if ((old_dir != new_dir) && f2fs_encrypted_inode(new_dir) && 752 !fscrypt_has_permitted_context(new_dir, old_inode)) { 753 err = -EPERM; 754 goto out; 755 } 756 757 if (is_inode_flag_set(new_dir, FI_PROJ_INHERIT) && 758 (!projid_eq(F2FS_I(new_dir)->i_projid, 759 F2FS_I(old_dentry->d_inode)->i_projid))) 760 return -EXDEV; 761 762 err = dquot_initialize(old_dir); 763 if (err) 764 goto out; 765 766 err = dquot_initialize(new_dir); 767 if (err) 768 goto out; 769 770 old_entry = f2fs_find_entry(old_dir, &old_dentry->d_name, &old_page); 771 if (!old_entry) { 772 if (IS_ERR(old_page)) 773 err = PTR_ERR(old_page); 774 goto out; 775 } 776 777 if (S_ISDIR(old_inode->i_mode)) { 778 old_dir_entry = f2fs_parent_dir(old_inode, &old_dir_page); 779 if (!old_dir_entry) { 780 if (IS_ERR(old_dir_page)) 781 err = PTR_ERR(old_dir_page); 782 goto out_old; 783 } 784 } 785 786 if (flags & RENAME_WHITEOUT) { 787 err = f2fs_create_whiteout(old_dir, &whiteout); 788 if (err) 789 goto out_dir; 790 } 791 792 if (new_inode) { 793 794 err = -ENOTEMPTY; 795 if (old_dir_entry && !f2fs_empty_dir(new_inode)) 796 goto out_whiteout; 797 798 err = -ENOENT; 799 new_entry = f2fs_find_entry(new_dir, &new_dentry->d_name, 800 &new_page); 801 if (!new_entry) { 802 if (IS_ERR(new_page)) 803 err = PTR_ERR(new_page); 804 goto out_whiteout; 805 } 806 807 f2fs_balance_fs(sbi, true); 808 809 f2fs_lock_op(sbi); 810 811 err = acquire_orphan_inode(sbi); 812 if (err) 813 goto put_out_dir; 814 815 f2fs_set_link(new_dir, new_entry, new_page, old_inode); 816 817 new_inode->i_ctime = current_time(new_inode); 818 down_write(&F2FS_I(new_inode)->i_sem); 819 if (old_dir_entry) 820 f2fs_i_links_write(new_inode, false); 821 f2fs_i_links_write(new_inode, false); 822 up_write(&F2FS_I(new_inode)->i_sem); 823 824 if (!new_inode->i_nlink) 825 add_orphan_inode(new_inode); 826 else 827 release_orphan_inode(sbi); 828 } else { 829 f2fs_balance_fs(sbi, true); 830 831 f2fs_lock_op(sbi); 832 833 err = f2fs_add_link(new_dentry, old_inode); 834 if (err) { 835 f2fs_unlock_op(sbi); 836 goto out_whiteout; 837 } 838 839 if (old_dir_entry) 840 f2fs_i_links_write(new_dir, true); 841 842 /* 843 * old entry and new entry can locate in the same inline 844 * dentry in inode, when attaching new entry in inline dentry, 845 * it could force inline dentry conversion, after that, 846 * old_entry and old_page will point to wrong address, in 847 * order to avoid this, let's do the check and update here. 848 */ 849 if (is_old_inline && !f2fs_has_inline_dentry(old_dir)) { 850 f2fs_put_page(old_page, 0); 851 old_page = NULL; 852 853 old_entry = f2fs_find_entry(old_dir, 854 &old_dentry->d_name, &old_page); 855 if (!old_entry) { 856 err = -ENOENT; 857 if (IS_ERR(old_page)) 858 err = PTR_ERR(old_page); 859 f2fs_unlock_op(sbi); 860 goto out_whiteout; 861 } 862 } 863 } 864 865 down_write(&F2FS_I(old_inode)->i_sem); 866 if (!old_dir_entry || whiteout) 867 file_lost_pino(old_inode); 868 else 869 F2FS_I(old_inode)->i_pino = new_dir->i_ino; 870 up_write(&F2FS_I(old_inode)->i_sem); 871 872 old_inode->i_ctime = current_time(old_inode); 873 f2fs_mark_inode_dirty_sync(old_inode, false); 874 875 f2fs_delete_entry(old_entry, old_page, old_dir, NULL); 876 877 if (whiteout) { 878 whiteout->i_state |= I_LINKABLE; 879 set_inode_flag(whiteout, FI_INC_LINK); 880 err = f2fs_add_link(old_dentry, whiteout); 881 if (err) 882 goto put_out_dir; 883 whiteout->i_state &= ~I_LINKABLE; 884 iput(whiteout); 885 } 886 887 if (old_dir_entry) { 888 if (old_dir != new_dir && !whiteout) { 889 f2fs_set_link(old_inode, old_dir_entry, 890 old_dir_page, new_dir); 891 } else { 892 f2fs_dentry_kunmap(old_inode, old_dir_page); 893 f2fs_put_page(old_dir_page, 0); 894 } 895 f2fs_i_links_write(old_dir, false); 896 } 897 898 f2fs_unlock_op(sbi); 899 900 if (IS_DIRSYNC(old_dir) || IS_DIRSYNC(new_dir)) 901 f2fs_sync_fs(sbi->sb, 1); 902 return 0; 903 904 put_out_dir: 905 f2fs_unlock_op(sbi); 906 if (new_page) { 907 f2fs_dentry_kunmap(new_dir, new_page); 908 f2fs_put_page(new_page, 0); 909 } 910 out_whiteout: 911 if (whiteout) 912 iput(whiteout); 913 out_dir: 914 if (old_dir_entry) { 915 f2fs_dentry_kunmap(old_inode, old_dir_page); 916 f2fs_put_page(old_dir_page, 0); 917 } 918 out_old: 919 f2fs_dentry_kunmap(old_dir, old_page); 920 f2fs_put_page(old_page, 0); 921 out: 922 return err; 923 } 924 925 static int f2fs_cross_rename(struct inode *old_dir, struct dentry *old_dentry, 926 struct inode *new_dir, struct dentry *new_dentry) 927 { 928 struct f2fs_sb_info *sbi = F2FS_I_SB(old_dir); 929 struct inode *old_inode = d_inode(old_dentry); 930 struct inode *new_inode = d_inode(new_dentry); 931 struct page *old_dir_page, *new_dir_page; 932 struct page *old_page, *new_page; 933 struct f2fs_dir_entry *old_dir_entry = NULL, *new_dir_entry = NULL; 934 struct f2fs_dir_entry *old_entry, *new_entry; 935 int old_nlink = 0, new_nlink = 0; 936 int err = -ENOENT; 937 938 if ((f2fs_encrypted_inode(old_dir) && 939 !fscrypt_has_encryption_key(old_dir)) || 940 (f2fs_encrypted_inode(new_dir) && 941 !fscrypt_has_encryption_key(new_dir))) 942 return -ENOKEY; 943 944 if ((f2fs_encrypted_inode(old_dir) || f2fs_encrypted_inode(new_dir)) && 945 (old_dir != new_dir) && 946 (!fscrypt_has_permitted_context(new_dir, old_inode) || 947 !fscrypt_has_permitted_context(old_dir, new_inode))) 948 return -EPERM; 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 f2fs_unlock_op(sbi); 1061 1062 if (IS_DIRSYNC(old_dir) || IS_DIRSYNC(new_dir)) 1063 f2fs_sync_fs(sbi->sb, 1); 1064 return 0; 1065 out_new_dir: 1066 if (new_dir_entry) { 1067 f2fs_dentry_kunmap(new_inode, new_dir_page); 1068 f2fs_put_page(new_dir_page, 0); 1069 } 1070 out_old_dir: 1071 if (old_dir_entry) { 1072 f2fs_dentry_kunmap(old_inode, old_dir_page); 1073 f2fs_put_page(old_dir_page, 0); 1074 } 1075 out_new: 1076 f2fs_dentry_kunmap(new_dir, new_page); 1077 f2fs_put_page(new_page, 0); 1078 out_old: 1079 f2fs_dentry_kunmap(old_dir, old_page); 1080 f2fs_put_page(old_page, 0); 1081 out: 1082 return err; 1083 } 1084 1085 static int f2fs_rename2(struct inode *old_dir, struct dentry *old_dentry, 1086 struct inode *new_dir, struct dentry *new_dentry, 1087 unsigned int flags) 1088 { 1089 if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE | RENAME_WHITEOUT)) 1090 return -EINVAL; 1091 1092 if (flags & RENAME_EXCHANGE) { 1093 return f2fs_cross_rename(old_dir, old_dentry, 1094 new_dir, new_dentry); 1095 } 1096 /* 1097 * VFS has already handled the new dentry existence case, 1098 * here, we just deal with "RENAME_NOREPLACE" as regular rename. 1099 */ 1100 return f2fs_rename(old_dir, old_dentry, new_dir, new_dentry, flags); 1101 } 1102 1103 static const char *f2fs_encrypted_get_link(struct dentry *dentry, 1104 struct inode *inode, 1105 struct delayed_call *done) 1106 { 1107 struct page *cpage = NULL; 1108 char *caddr, *paddr = NULL; 1109 struct fscrypt_str cstr = FSTR_INIT(NULL, 0); 1110 struct fscrypt_str pstr = FSTR_INIT(NULL, 0); 1111 struct fscrypt_symlink_data *sd; 1112 u32 max_size = inode->i_sb->s_blocksize; 1113 int res; 1114 1115 if (!dentry) 1116 return ERR_PTR(-ECHILD); 1117 1118 res = fscrypt_get_encryption_info(inode); 1119 if (res) 1120 return ERR_PTR(res); 1121 1122 cpage = read_mapping_page(inode->i_mapping, 0, NULL); 1123 if (IS_ERR(cpage)) 1124 return ERR_CAST(cpage); 1125 caddr = page_address(cpage); 1126 1127 /* Symlink is encrypted */ 1128 sd = (struct fscrypt_symlink_data *)caddr; 1129 cstr.name = sd->encrypted_path; 1130 cstr.len = le16_to_cpu(sd->len); 1131 1132 /* this is broken symlink case */ 1133 if (unlikely(cstr.len == 0)) { 1134 res = -ENOENT; 1135 goto errout; 1136 } 1137 1138 if ((cstr.len + sizeof(struct fscrypt_symlink_data) - 1) > max_size) { 1139 /* Symlink data on the disk is corrupted */ 1140 res = -EIO; 1141 goto errout; 1142 } 1143 res = fscrypt_fname_alloc_buffer(inode, cstr.len, &pstr); 1144 if (res) 1145 goto errout; 1146 1147 res = fscrypt_fname_disk_to_usr(inode, 0, 0, &cstr, &pstr); 1148 if (res) 1149 goto errout; 1150 1151 /* this is broken symlink case */ 1152 if (unlikely(pstr.name[0] == 0)) { 1153 res = -ENOENT; 1154 goto errout; 1155 } 1156 1157 paddr = pstr.name; 1158 1159 /* Null-terminate the name */ 1160 paddr[pstr.len] = '\0'; 1161 1162 put_page(cpage); 1163 set_delayed_call(done, kfree_link, paddr); 1164 return paddr; 1165 errout: 1166 fscrypt_fname_free_buffer(&pstr); 1167 put_page(cpage); 1168 return ERR_PTR(res); 1169 } 1170 1171 const struct inode_operations f2fs_encrypted_symlink_inode_operations = { 1172 .get_link = f2fs_encrypted_get_link, 1173 .getattr = f2fs_getattr, 1174 .setattr = f2fs_setattr, 1175 #ifdef CONFIG_F2FS_FS_XATTR 1176 .listxattr = f2fs_listxattr, 1177 #endif 1178 }; 1179 1180 const struct inode_operations f2fs_dir_inode_operations = { 1181 .create = f2fs_create, 1182 .lookup = f2fs_lookup, 1183 .link = f2fs_link, 1184 .unlink = f2fs_unlink, 1185 .symlink = f2fs_symlink, 1186 .mkdir = f2fs_mkdir, 1187 .rmdir = f2fs_rmdir, 1188 .mknod = f2fs_mknod, 1189 .rename = f2fs_rename2, 1190 .tmpfile = f2fs_tmpfile, 1191 .getattr = f2fs_getattr, 1192 .setattr = f2fs_setattr, 1193 .get_acl = f2fs_get_acl, 1194 .set_acl = f2fs_set_acl, 1195 #ifdef CONFIG_F2FS_FS_XATTR 1196 .listxattr = f2fs_listxattr, 1197 #endif 1198 }; 1199 1200 const struct inode_operations f2fs_symlink_inode_operations = { 1201 .get_link = f2fs_get_link, 1202 .getattr = f2fs_getattr, 1203 .setattr = f2fs_setattr, 1204 #ifdef CONFIG_F2FS_FS_XATTR 1205 .listxattr = f2fs_listxattr, 1206 #endif 1207 }; 1208 1209 const struct inode_operations f2fs_special_inode_operations = { 1210 .getattr = f2fs_getattr, 1211 .setattr = f2fs_setattr, 1212 .get_acl = f2fs_get_acl, 1213 .set_acl = f2fs_set_acl, 1214 #ifdef CONFIG_F2FS_FS_XATTR 1215 .listxattr = f2fs_listxattr, 1216 #endif 1217 }; 1218