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