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