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(&init_user_ns, 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 user_namespace *mnt_userns, struct inode *dir, 318 struct dentry *dentry, umode_t mode, 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 page *page; 420 unsigned long ino = f2fs_inode_by_name(d_inode(child), &dotdot_name, &page); 421 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 generic_set_encrypted_ci_d_ops(dentry); 501 if (err == -ENOENT) 502 goto out_splice; 503 if (err) 504 goto out; 505 de = __f2fs_find_entry(dir, &fname, &page); 506 f2fs_free_filename(&fname); 507 508 if (!de) { 509 if (IS_ERR(page)) { 510 err = PTR_ERR(page); 511 goto out; 512 } 513 err = -ENOENT; 514 goto out_splice; 515 } 516 517 ino = le32_to_cpu(de->ino); 518 f2fs_put_page(page, 0); 519 520 inode = f2fs_iget(dir->i_sb, ino); 521 if (IS_ERR(inode)) { 522 err = PTR_ERR(inode); 523 goto out; 524 } 525 526 if ((dir->i_ino == root_ino) && f2fs_has_inline_dots(dir)) { 527 err = __recover_dot_dentries(dir, root_ino); 528 if (err) 529 goto out_iput; 530 } 531 532 if (f2fs_has_inline_dots(inode)) { 533 err = __recover_dot_dentries(inode, dir->i_ino); 534 if (err) 535 goto out_iput; 536 } 537 if (IS_ENCRYPTED(dir) && 538 (S_ISDIR(inode->i_mode) || S_ISLNK(inode->i_mode)) && 539 !fscrypt_has_permitted_context(dir, inode)) { 540 f2fs_warn(F2FS_I_SB(inode), "Inconsistent encryption contexts: %lu/%lu", 541 dir->i_ino, inode->i_ino); 542 err = -EPERM; 543 goto out_iput; 544 } 545 out_splice: 546 #ifdef CONFIG_UNICODE 547 if (!inode && IS_CASEFOLDED(dir)) { 548 /* Eventually we want to call d_add_ci(dentry, NULL) 549 * for negative dentries in the encoding case as 550 * well. For now, prevent the negative dentry 551 * from being cached. 552 */ 553 trace_f2fs_lookup_end(dir, dentry, ino, err); 554 return NULL; 555 } 556 #endif 557 new = d_splice_alias(inode, dentry); 558 err = PTR_ERR_OR_ZERO(new); 559 trace_f2fs_lookup_end(dir, dentry, ino, !new ? -ENOENT : err); 560 return new; 561 out_iput: 562 iput(inode); 563 out: 564 trace_f2fs_lookup_end(dir, dentry, ino, err); 565 return ERR_PTR(err); 566 } 567 568 static int f2fs_unlink(struct inode *dir, struct dentry *dentry) 569 { 570 struct f2fs_sb_info *sbi = F2FS_I_SB(dir); 571 struct inode *inode = d_inode(dentry); 572 struct f2fs_dir_entry *de; 573 struct page *page; 574 int err; 575 576 trace_f2fs_unlink_enter(dir, dentry); 577 578 if (unlikely(f2fs_cp_error(sbi))) { 579 err = -EIO; 580 goto fail; 581 } 582 583 err = dquot_initialize(dir); 584 if (err) 585 goto fail; 586 err = dquot_initialize(inode); 587 if (err) 588 goto fail; 589 590 de = f2fs_find_entry(dir, &dentry->d_name, &page); 591 if (!de) { 592 if (IS_ERR(page)) 593 err = PTR_ERR(page); 594 goto fail; 595 } 596 597 f2fs_balance_fs(sbi, true); 598 599 f2fs_lock_op(sbi); 600 err = f2fs_acquire_orphan_inode(sbi); 601 if (err) { 602 f2fs_unlock_op(sbi); 603 f2fs_put_page(page, 0); 604 goto fail; 605 } 606 f2fs_delete_entry(de, page, dir, inode); 607 #ifdef CONFIG_UNICODE 608 /* VFS negative dentries are incompatible with Encoding and 609 * Case-insensitiveness. Eventually we'll want avoid 610 * invalidating the dentries here, alongside with returning the 611 * negative dentries at f2fs_lookup(), when it is better 612 * supported by the VFS for the CI case. 613 */ 614 if (IS_CASEFOLDED(dir)) 615 d_invalidate(dentry); 616 #endif 617 f2fs_unlock_op(sbi); 618 619 if (IS_DIRSYNC(dir)) 620 f2fs_sync_fs(sbi->sb, 1); 621 fail: 622 trace_f2fs_unlink_exit(inode, err); 623 return err; 624 } 625 626 static const char *f2fs_get_link(struct dentry *dentry, 627 struct inode *inode, 628 struct delayed_call *done) 629 { 630 const char *link = page_get_link(dentry, inode, done); 631 632 if (!IS_ERR(link) && !*link) { 633 /* this is broken symlink case */ 634 do_delayed_call(done); 635 clear_delayed_call(done); 636 link = ERR_PTR(-ENOENT); 637 } 638 return link; 639 } 640 641 static int f2fs_symlink(struct user_namespace *mnt_userns, struct inode *dir, 642 struct dentry *dentry, const char *symname) 643 { 644 struct f2fs_sb_info *sbi = F2FS_I_SB(dir); 645 struct inode *inode; 646 size_t len = strlen(symname); 647 struct fscrypt_str disk_link; 648 int err; 649 650 if (unlikely(f2fs_cp_error(sbi))) 651 return -EIO; 652 if (!f2fs_is_checkpoint_ready(sbi)) 653 return -ENOSPC; 654 655 err = fscrypt_prepare_symlink(dir, symname, len, dir->i_sb->s_blocksize, 656 &disk_link); 657 if (err) 658 return err; 659 660 err = dquot_initialize(dir); 661 if (err) 662 return err; 663 664 inode = f2fs_new_inode(dir, S_IFLNK | S_IRWXUGO); 665 if (IS_ERR(inode)) 666 return PTR_ERR(inode); 667 668 if (IS_ENCRYPTED(inode)) 669 inode->i_op = &f2fs_encrypted_symlink_inode_operations; 670 else 671 inode->i_op = &f2fs_symlink_inode_operations; 672 inode_nohighmem(inode); 673 inode->i_mapping->a_ops = &f2fs_dblock_aops; 674 675 f2fs_lock_op(sbi); 676 err = f2fs_add_link(dentry, inode); 677 if (err) 678 goto out_f2fs_handle_failed_inode; 679 f2fs_unlock_op(sbi); 680 f2fs_alloc_nid_done(sbi, inode->i_ino); 681 682 err = fscrypt_encrypt_symlink(inode, symname, len, &disk_link); 683 if (err) 684 goto err_out; 685 686 err = page_symlink(inode, disk_link.name, disk_link.len); 687 688 err_out: 689 d_instantiate_new(dentry, inode); 690 691 /* 692 * Let's flush symlink data in order to avoid broken symlink as much as 693 * possible. Nevertheless, fsyncing is the best way, but there is no 694 * way to get a file descriptor in order to flush that. 695 * 696 * Note that, it needs to do dir->fsync to make this recoverable. 697 * If the symlink path is stored into inline_data, there is no 698 * performance regression. 699 */ 700 if (!err) { 701 filemap_write_and_wait_range(inode->i_mapping, 0, 702 disk_link.len - 1); 703 704 if (IS_DIRSYNC(dir)) 705 f2fs_sync_fs(sbi->sb, 1); 706 } else { 707 f2fs_unlink(dir, dentry); 708 } 709 710 f2fs_balance_fs(sbi, true); 711 goto out_free_encrypted_link; 712 713 out_f2fs_handle_failed_inode: 714 f2fs_handle_failed_inode(inode); 715 out_free_encrypted_link: 716 if (disk_link.name != (unsigned char *)symname) 717 kfree(disk_link.name); 718 return err; 719 } 720 721 static int f2fs_mkdir(struct user_namespace *mnt_userns, struct inode *dir, 722 struct dentry *dentry, umode_t mode) 723 { 724 struct f2fs_sb_info *sbi = F2FS_I_SB(dir); 725 struct inode *inode; 726 int err; 727 728 if (unlikely(f2fs_cp_error(sbi))) 729 return -EIO; 730 731 err = dquot_initialize(dir); 732 if (err) 733 return err; 734 735 inode = f2fs_new_inode(dir, S_IFDIR | mode); 736 if (IS_ERR(inode)) 737 return PTR_ERR(inode); 738 739 inode->i_op = &f2fs_dir_inode_operations; 740 inode->i_fop = &f2fs_dir_operations; 741 inode->i_mapping->a_ops = &f2fs_dblock_aops; 742 inode_nohighmem(inode); 743 744 set_inode_flag(inode, FI_INC_LINK); 745 f2fs_lock_op(sbi); 746 err = f2fs_add_link(dentry, inode); 747 if (err) 748 goto out_fail; 749 f2fs_unlock_op(sbi); 750 751 f2fs_alloc_nid_done(sbi, inode->i_ino); 752 753 d_instantiate_new(dentry, inode); 754 755 if (IS_DIRSYNC(dir)) 756 f2fs_sync_fs(sbi->sb, 1); 757 758 f2fs_balance_fs(sbi, true); 759 return 0; 760 761 out_fail: 762 clear_inode_flag(inode, FI_INC_LINK); 763 f2fs_handle_failed_inode(inode); 764 return err; 765 } 766 767 static int f2fs_rmdir(struct inode *dir, struct dentry *dentry) 768 { 769 struct inode *inode = d_inode(dentry); 770 771 if (f2fs_empty_dir(inode)) 772 return f2fs_unlink(dir, dentry); 773 return -ENOTEMPTY; 774 } 775 776 static int f2fs_mknod(struct user_namespace *mnt_userns, struct inode *dir, 777 struct dentry *dentry, umode_t mode, dev_t rdev) 778 { 779 struct f2fs_sb_info *sbi = F2FS_I_SB(dir); 780 struct inode *inode; 781 int err = 0; 782 783 if (unlikely(f2fs_cp_error(sbi))) 784 return -EIO; 785 if (!f2fs_is_checkpoint_ready(sbi)) 786 return -ENOSPC; 787 788 err = dquot_initialize(dir); 789 if (err) 790 return err; 791 792 inode = f2fs_new_inode(dir, mode); 793 if (IS_ERR(inode)) 794 return PTR_ERR(inode); 795 796 init_special_inode(inode, inode->i_mode, rdev); 797 inode->i_op = &f2fs_special_inode_operations; 798 799 f2fs_lock_op(sbi); 800 err = f2fs_add_link(dentry, inode); 801 if (err) 802 goto out; 803 f2fs_unlock_op(sbi); 804 805 f2fs_alloc_nid_done(sbi, inode->i_ino); 806 807 d_instantiate_new(dentry, inode); 808 809 if (IS_DIRSYNC(dir)) 810 f2fs_sync_fs(sbi->sb, 1); 811 812 f2fs_balance_fs(sbi, true); 813 return 0; 814 out: 815 f2fs_handle_failed_inode(inode); 816 return err; 817 } 818 819 static int __f2fs_tmpfile(struct inode *dir, struct dentry *dentry, 820 umode_t mode, struct inode **whiteout) 821 { 822 struct f2fs_sb_info *sbi = F2FS_I_SB(dir); 823 struct inode *inode; 824 int err; 825 826 err = dquot_initialize(dir); 827 if (err) 828 return err; 829 830 inode = f2fs_new_inode(dir, mode); 831 if (IS_ERR(inode)) 832 return PTR_ERR(inode); 833 834 if (whiteout) { 835 init_special_inode(inode, inode->i_mode, WHITEOUT_DEV); 836 inode->i_op = &f2fs_special_inode_operations; 837 } else { 838 inode->i_op = &f2fs_file_inode_operations; 839 inode->i_fop = &f2fs_file_operations; 840 inode->i_mapping->a_ops = &f2fs_dblock_aops; 841 } 842 843 f2fs_lock_op(sbi); 844 err = f2fs_acquire_orphan_inode(sbi); 845 if (err) 846 goto out; 847 848 err = f2fs_do_tmpfile(inode, dir); 849 if (err) 850 goto release_out; 851 852 /* 853 * add this non-linked tmpfile to orphan list, in this way we could 854 * remove all unused data of tmpfile after abnormal power-off. 855 */ 856 f2fs_add_orphan_inode(inode); 857 f2fs_alloc_nid_done(sbi, inode->i_ino); 858 859 if (whiteout) { 860 f2fs_i_links_write(inode, false); 861 862 spin_lock(&inode->i_lock); 863 inode->i_state |= I_LINKABLE; 864 spin_unlock(&inode->i_lock); 865 866 *whiteout = inode; 867 } else { 868 d_tmpfile(dentry, inode); 869 } 870 /* link_count was changed by d_tmpfile as well. */ 871 f2fs_unlock_op(sbi); 872 unlock_new_inode(inode); 873 874 f2fs_balance_fs(sbi, true); 875 return 0; 876 877 release_out: 878 f2fs_release_orphan_inode(sbi); 879 out: 880 f2fs_handle_failed_inode(inode); 881 return err; 882 } 883 884 static int f2fs_tmpfile(struct user_namespace *mnt_userns, struct inode *dir, 885 struct dentry *dentry, umode_t mode) 886 { 887 struct f2fs_sb_info *sbi = F2FS_I_SB(dir); 888 889 if (unlikely(f2fs_cp_error(sbi))) 890 return -EIO; 891 if (!f2fs_is_checkpoint_ready(sbi)) 892 return -ENOSPC; 893 894 return __f2fs_tmpfile(dir, dentry, mode, NULL); 895 } 896 897 static int f2fs_create_whiteout(struct inode *dir, struct inode **whiteout) 898 { 899 if (unlikely(f2fs_cp_error(F2FS_I_SB(dir)))) 900 return -EIO; 901 902 return __f2fs_tmpfile(dir, NULL, S_IFCHR | WHITEOUT_MODE, whiteout); 903 } 904 905 static int f2fs_rename(struct inode *old_dir, struct dentry *old_dentry, 906 struct inode *new_dir, struct dentry *new_dentry, 907 unsigned int flags) 908 { 909 struct f2fs_sb_info *sbi = F2FS_I_SB(old_dir); 910 struct inode *old_inode = d_inode(old_dentry); 911 struct inode *new_inode = d_inode(new_dentry); 912 struct inode *whiteout = NULL; 913 struct page *old_dir_page = NULL; 914 struct page *old_page, *new_page = NULL; 915 struct f2fs_dir_entry *old_dir_entry = NULL; 916 struct f2fs_dir_entry *old_entry; 917 struct f2fs_dir_entry *new_entry; 918 int err; 919 920 if (unlikely(f2fs_cp_error(sbi))) 921 return -EIO; 922 if (!f2fs_is_checkpoint_ready(sbi)) 923 return -ENOSPC; 924 925 if (is_inode_flag_set(new_dir, FI_PROJ_INHERIT) && 926 (!projid_eq(F2FS_I(new_dir)->i_projid, 927 F2FS_I(old_dentry->d_inode)->i_projid))) 928 return -EXDEV; 929 930 /* 931 * If new_inode is null, the below renaming flow will 932 * add a link in old_dir which can conver inline_dir. 933 * After then, if we failed to get the entry due to other 934 * reasons like ENOMEM, we had to remove the new entry. 935 * Instead of adding such the error handling routine, let's 936 * simply convert first here. 937 */ 938 if (old_dir == new_dir && !new_inode) { 939 err = f2fs_try_convert_inline_dir(old_dir, new_dentry); 940 if (err) 941 return err; 942 } 943 944 if (flags & RENAME_WHITEOUT) { 945 err = f2fs_create_whiteout(old_dir, &whiteout); 946 if (err) 947 return err; 948 } 949 950 err = dquot_initialize(old_dir); 951 if (err) 952 goto out; 953 954 err = dquot_initialize(new_dir); 955 if (err) 956 goto out; 957 958 if (new_inode) { 959 err = dquot_initialize(new_inode); 960 if (err) 961 goto out; 962 } 963 964 err = -ENOENT; 965 old_entry = f2fs_find_entry(old_dir, &old_dentry->d_name, &old_page); 966 if (!old_entry) { 967 if (IS_ERR(old_page)) 968 err = PTR_ERR(old_page); 969 goto out; 970 } 971 972 if (S_ISDIR(old_inode->i_mode)) { 973 old_dir_entry = f2fs_parent_dir(old_inode, &old_dir_page); 974 if (!old_dir_entry) { 975 if (IS_ERR(old_dir_page)) 976 err = PTR_ERR(old_dir_page); 977 goto out_old; 978 } 979 } 980 981 if (new_inode) { 982 983 err = -ENOTEMPTY; 984 if (old_dir_entry && !f2fs_empty_dir(new_inode)) 985 goto out_dir; 986 987 err = -ENOENT; 988 new_entry = f2fs_find_entry(new_dir, &new_dentry->d_name, 989 &new_page); 990 if (!new_entry) { 991 if (IS_ERR(new_page)) 992 err = PTR_ERR(new_page); 993 goto out_dir; 994 } 995 996 f2fs_balance_fs(sbi, true); 997 998 f2fs_lock_op(sbi); 999 1000 err = f2fs_acquire_orphan_inode(sbi); 1001 if (err) 1002 goto put_out_dir; 1003 1004 f2fs_set_link(new_dir, new_entry, new_page, old_inode); 1005 new_page = NULL; 1006 1007 new_inode->i_ctime = current_time(new_inode); 1008 down_write(&F2FS_I(new_inode)->i_sem); 1009 if (old_dir_entry) 1010 f2fs_i_links_write(new_inode, false); 1011 f2fs_i_links_write(new_inode, false); 1012 up_write(&F2FS_I(new_inode)->i_sem); 1013 1014 if (!new_inode->i_nlink) 1015 f2fs_add_orphan_inode(new_inode); 1016 else 1017 f2fs_release_orphan_inode(sbi); 1018 } else { 1019 f2fs_balance_fs(sbi, true); 1020 1021 f2fs_lock_op(sbi); 1022 1023 err = f2fs_add_link(new_dentry, old_inode); 1024 if (err) { 1025 f2fs_unlock_op(sbi); 1026 goto out_dir; 1027 } 1028 1029 if (old_dir_entry) 1030 f2fs_i_links_write(new_dir, true); 1031 } 1032 1033 down_write(&F2FS_I(old_inode)->i_sem); 1034 if (!old_dir_entry || whiteout) 1035 file_lost_pino(old_inode); 1036 else 1037 /* adjust dir's i_pino to pass fsck check */ 1038 f2fs_i_pino_write(old_inode, new_dir->i_ino); 1039 up_write(&F2FS_I(old_inode)->i_sem); 1040 1041 old_inode->i_ctime = current_time(old_inode); 1042 f2fs_mark_inode_dirty_sync(old_inode, false); 1043 1044 f2fs_delete_entry(old_entry, old_page, old_dir, NULL); 1045 old_page = NULL; 1046 1047 if (whiteout) { 1048 set_inode_flag(whiteout, FI_INC_LINK); 1049 err = f2fs_add_link(old_dentry, whiteout); 1050 if (err) 1051 goto put_out_dir; 1052 1053 spin_lock(&whiteout->i_lock); 1054 whiteout->i_state &= ~I_LINKABLE; 1055 spin_unlock(&whiteout->i_lock); 1056 1057 iput(whiteout); 1058 } 1059 1060 if (old_dir_entry) { 1061 if (old_dir != new_dir && !whiteout) 1062 f2fs_set_link(old_inode, old_dir_entry, 1063 old_dir_page, new_dir); 1064 else 1065 f2fs_put_page(old_dir_page, 0); 1066 f2fs_i_links_write(old_dir, false); 1067 } 1068 if (F2FS_OPTION(sbi).fsync_mode == FSYNC_MODE_STRICT) { 1069 f2fs_add_ino_entry(sbi, new_dir->i_ino, TRANS_DIR_INO); 1070 if (S_ISDIR(old_inode->i_mode)) 1071 f2fs_add_ino_entry(sbi, old_inode->i_ino, 1072 TRANS_DIR_INO); 1073 } 1074 1075 f2fs_unlock_op(sbi); 1076 1077 if (IS_DIRSYNC(old_dir) || IS_DIRSYNC(new_dir)) 1078 f2fs_sync_fs(sbi->sb, 1); 1079 1080 f2fs_update_time(sbi, REQ_TIME); 1081 return 0; 1082 1083 put_out_dir: 1084 f2fs_unlock_op(sbi); 1085 f2fs_put_page(new_page, 0); 1086 out_dir: 1087 if (old_dir_entry) 1088 f2fs_put_page(old_dir_page, 0); 1089 out_old: 1090 f2fs_put_page(old_page, 0); 1091 out: 1092 if (whiteout) 1093 iput(whiteout); 1094 return err; 1095 } 1096 1097 static int f2fs_cross_rename(struct inode *old_dir, struct dentry *old_dentry, 1098 struct inode *new_dir, struct dentry *new_dentry) 1099 { 1100 struct f2fs_sb_info *sbi = F2FS_I_SB(old_dir); 1101 struct inode *old_inode = d_inode(old_dentry); 1102 struct inode *new_inode = d_inode(new_dentry); 1103 struct page *old_dir_page, *new_dir_page; 1104 struct page *old_page, *new_page; 1105 struct f2fs_dir_entry *old_dir_entry = NULL, *new_dir_entry = NULL; 1106 struct f2fs_dir_entry *old_entry, *new_entry; 1107 int old_nlink = 0, new_nlink = 0; 1108 int err; 1109 1110 if (unlikely(f2fs_cp_error(sbi))) 1111 return -EIO; 1112 if (!f2fs_is_checkpoint_ready(sbi)) 1113 return -ENOSPC; 1114 1115 if ((is_inode_flag_set(new_dir, FI_PROJ_INHERIT) && 1116 !projid_eq(F2FS_I(new_dir)->i_projid, 1117 F2FS_I(old_dentry->d_inode)->i_projid)) || 1118 (is_inode_flag_set(new_dir, FI_PROJ_INHERIT) && 1119 !projid_eq(F2FS_I(old_dir)->i_projid, 1120 F2FS_I(new_dentry->d_inode)->i_projid))) 1121 return -EXDEV; 1122 1123 err = dquot_initialize(old_dir); 1124 if (err) 1125 goto out; 1126 1127 err = dquot_initialize(new_dir); 1128 if (err) 1129 goto out; 1130 1131 err = -ENOENT; 1132 old_entry = f2fs_find_entry(old_dir, &old_dentry->d_name, &old_page); 1133 if (!old_entry) { 1134 if (IS_ERR(old_page)) 1135 err = PTR_ERR(old_page); 1136 goto out; 1137 } 1138 1139 new_entry = f2fs_find_entry(new_dir, &new_dentry->d_name, &new_page); 1140 if (!new_entry) { 1141 if (IS_ERR(new_page)) 1142 err = PTR_ERR(new_page); 1143 goto out_old; 1144 } 1145 1146 /* prepare for updating ".." directory entry info later */ 1147 if (old_dir != new_dir) { 1148 if (S_ISDIR(old_inode->i_mode)) { 1149 old_dir_entry = f2fs_parent_dir(old_inode, 1150 &old_dir_page); 1151 if (!old_dir_entry) { 1152 if (IS_ERR(old_dir_page)) 1153 err = PTR_ERR(old_dir_page); 1154 goto out_new; 1155 } 1156 } 1157 1158 if (S_ISDIR(new_inode->i_mode)) { 1159 new_dir_entry = f2fs_parent_dir(new_inode, 1160 &new_dir_page); 1161 if (!new_dir_entry) { 1162 if (IS_ERR(new_dir_page)) 1163 err = PTR_ERR(new_dir_page); 1164 goto out_old_dir; 1165 } 1166 } 1167 } 1168 1169 /* 1170 * If cross rename between file and directory those are not 1171 * in the same directory, we will inc nlink of file's parent 1172 * later, so we should check upper boundary of its nlink. 1173 */ 1174 if ((!old_dir_entry || !new_dir_entry) && 1175 old_dir_entry != new_dir_entry) { 1176 old_nlink = old_dir_entry ? -1 : 1; 1177 new_nlink = -old_nlink; 1178 err = -EMLINK; 1179 if ((old_nlink > 0 && old_dir->i_nlink >= F2FS_LINK_MAX) || 1180 (new_nlink > 0 && new_dir->i_nlink >= F2FS_LINK_MAX)) 1181 goto out_new_dir; 1182 } 1183 1184 f2fs_balance_fs(sbi, true); 1185 1186 f2fs_lock_op(sbi); 1187 1188 /* update ".." directory entry info of old dentry */ 1189 if (old_dir_entry) 1190 f2fs_set_link(old_inode, old_dir_entry, old_dir_page, new_dir); 1191 1192 /* update ".." directory entry info of new dentry */ 1193 if (new_dir_entry) 1194 f2fs_set_link(new_inode, new_dir_entry, new_dir_page, old_dir); 1195 1196 /* update directory entry info of old dir inode */ 1197 f2fs_set_link(old_dir, old_entry, old_page, new_inode); 1198 1199 down_write(&F2FS_I(old_inode)->i_sem); 1200 if (!old_dir_entry) 1201 file_lost_pino(old_inode); 1202 else 1203 /* adjust dir's i_pino to pass fsck check */ 1204 f2fs_i_pino_write(old_inode, new_dir->i_ino); 1205 up_write(&F2FS_I(old_inode)->i_sem); 1206 1207 old_dir->i_ctime = current_time(old_dir); 1208 if (old_nlink) { 1209 down_write(&F2FS_I(old_dir)->i_sem); 1210 f2fs_i_links_write(old_dir, old_nlink > 0); 1211 up_write(&F2FS_I(old_dir)->i_sem); 1212 } 1213 f2fs_mark_inode_dirty_sync(old_dir, false); 1214 1215 /* update directory entry info of new dir inode */ 1216 f2fs_set_link(new_dir, new_entry, new_page, old_inode); 1217 1218 down_write(&F2FS_I(new_inode)->i_sem); 1219 if (!new_dir_entry) 1220 file_lost_pino(new_inode); 1221 else 1222 /* adjust dir's i_pino to pass fsck check */ 1223 f2fs_i_pino_write(new_inode, old_dir->i_ino); 1224 up_write(&F2FS_I(new_inode)->i_sem); 1225 1226 new_dir->i_ctime = current_time(new_dir); 1227 if (new_nlink) { 1228 down_write(&F2FS_I(new_dir)->i_sem); 1229 f2fs_i_links_write(new_dir, new_nlink > 0); 1230 up_write(&F2FS_I(new_dir)->i_sem); 1231 } 1232 f2fs_mark_inode_dirty_sync(new_dir, false); 1233 1234 if (F2FS_OPTION(sbi).fsync_mode == FSYNC_MODE_STRICT) { 1235 f2fs_add_ino_entry(sbi, old_dir->i_ino, TRANS_DIR_INO); 1236 f2fs_add_ino_entry(sbi, new_dir->i_ino, TRANS_DIR_INO); 1237 } 1238 1239 f2fs_unlock_op(sbi); 1240 1241 if (IS_DIRSYNC(old_dir) || IS_DIRSYNC(new_dir)) 1242 f2fs_sync_fs(sbi->sb, 1); 1243 1244 f2fs_update_time(sbi, REQ_TIME); 1245 return 0; 1246 out_new_dir: 1247 if (new_dir_entry) { 1248 f2fs_put_page(new_dir_page, 0); 1249 } 1250 out_old_dir: 1251 if (old_dir_entry) { 1252 f2fs_put_page(old_dir_page, 0); 1253 } 1254 out_new: 1255 f2fs_put_page(new_page, 0); 1256 out_old: 1257 f2fs_put_page(old_page, 0); 1258 out: 1259 return err; 1260 } 1261 1262 static int f2fs_rename2(struct user_namespace *mnt_userns, 1263 struct inode *old_dir, struct dentry *old_dentry, 1264 struct inode *new_dir, struct dentry *new_dentry, 1265 unsigned int flags) 1266 { 1267 int err; 1268 1269 if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE | RENAME_WHITEOUT)) 1270 return -EINVAL; 1271 1272 err = fscrypt_prepare_rename(old_dir, old_dentry, new_dir, new_dentry, 1273 flags); 1274 if (err) 1275 return err; 1276 1277 if (flags & RENAME_EXCHANGE) { 1278 return f2fs_cross_rename(old_dir, old_dentry, 1279 new_dir, new_dentry); 1280 } 1281 /* 1282 * VFS has already handled the new dentry existence case, 1283 * here, we just deal with "RENAME_NOREPLACE" as regular rename. 1284 */ 1285 return f2fs_rename(old_dir, old_dentry, new_dir, new_dentry, flags); 1286 } 1287 1288 static const char *f2fs_encrypted_get_link(struct dentry *dentry, 1289 struct inode *inode, 1290 struct delayed_call *done) 1291 { 1292 struct page *page; 1293 const char *target; 1294 1295 if (!dentry) 1296 return ERR_PTR(-ECHILD); 1297 1298 page = read_mapping_page(inode->i_mapping, 0, NULL); 1299 if (IS_ERR(page)) 1300 return ERR_CAST(page); 1301 1302 target = fscrypt_get_symlink(inode, page_address(page), 1303 inode->i_sb->s_blocksize, done); 1304 put_page(page); 1305 return target; 1306 } 1307 1308 const struct inode_operations f2fs_encrypted_symlink_inode_operations = { 1309 .get_link = f2fs_encrypted_get_link, 1310 .getattr = f2fs_getattr, 1311 .setattr = f2fs_setattr, 1312 .listxattr = f2fs_listxattr, 1313 }; 1314 1315 const struct inode_operations f2fs_dir_inode_operations = { 1316 .create = f2fs_create, 1317 .lookup = f2fs_lookup, 1318 .link = f2fs_link, 1319 .unlink = f2fs_unlink, 1320 .symlink = f2fs_symlink, 1321 .mkdir = f2fs_mkdir, 1322 .rmdir = f2fs_rmdir, 1323 .mknod = f2fs_mknod, 1324 .rename = f2fs_rename2, 1325 .tmpfile = f2fs_tmpfile, 1326 .getattr = f2fs_getattr, 1327 .setattr = f2fs_setattr, 1328 .get_acl = f2fs_get_acl, 1329 .set_acl = f2fs_set_acl, 1330 .listxattr = f2fs_listxattr, 1331 .fiemap = f2fs_fiemap, 1332 .fileattr_get = f2fs_fileattr_get, 1333 .fileattr_set = f2fs_fileattr_set, 1334 }; 1335 1336 const struct inode_operations f2fs_symlink_inode_operations = { 1337 .get_link = f2fs_get_link, 1338 .getattr = f2fs_getattr, 1339 .setattr = f2fs_setattr, 1340 .listxattr = f2fs_listxattr, 1341 }; 1342 1343 const struct inode_operations f2fs_special_inode_operations = { 1344 .getattr = f2fs_getattr, 1345 .setattr = f2fs_setattr, 1346 .get_acl = f2fs_get_acl, 1347 .set_acl = f2fs_set_acl, 1348 .listxattr = f2fs_listxattr, 1349 }; 1350