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