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