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