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