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