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