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