1 /* 2 * fs/f2fs/namei.c 3 * 4 * Copyright (c) 2012 Samsung Electronics Co., Ltd. 5 * http://www.samsung.com/ 6 * 7 * This program is free software; you can redistribute it and/or modify 8 * it under the terms of the GNU General Public License version 2 as 9 * published by the Free Software Foundation. 10 */ 11 #include <linux/fs.h> 12 #include <linux/f2fs_fs.h> 13 #include <linux/pagemap.h> 14 #include <linux/sched.h> 15 #include <linux/ctype.h> 16 #include <linux/dcache.h> 17 18 #include "f2fs.h" 19 #include "node.h" 20 #include "xattr.h" 21 #include "acl.h" 22 #include <trace/events/f2fs.h> 23 24 static struct inode *f2fs_new_inode(struct inode *dir, umode_t mode) 25 { 26 struct f2fs_sb_info *sbi = F2FS_I_SB(dir); 27 nid_t ino; 28 struct inode *inode; 29 bool nid_free = false; 30 int err; 31 32 inode = new_inode(dir->i_sb); 33 if (!inode) 34 return ERR_PTR(-ENOMEM); 35 36 f2fs_lock_op(sbi); 37 if (!alloc_nid(sbi, &ino)) { 38 f2fs_unlock_op(sbi); 39 err = -ENOSPC; 40 goto fail; 41 } 42 f2fs_unlock_op(sbi); 43 44 inode_init_owner(inode, dir, mode); 45 46 inode->i_ino = ino; 47 inode->i_blocks = 0; 48 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME; 49 inode->i_generation = sbi->s_next_generation++; 50 51 err = insert_inode_locked(inode); 52 if (err) { 53 err = -EINVAL; 54 nid_free = true; 55 goto out; 56 } 57 trace_f2fs_new_inode(inode, 0); 58 mark_inode_dirty(inode); 59 return inode; 60 61 out: 62 clear_nlink(inode); 63 unlock_new_inode(inode); 64 fail: 65 trace_f2fs_new_inode(inode, err); 66 make_bad_inode(inode); 67 iput(inode); 68 if (nid_free) 69 alloc_nid_failed(sbi, ino); 70 return ERR_PTR(err); 71 } 72 73 static int is_multimedia_file(const unsigned char *s, const char *sub) 74 { 75 size_t slen = strlen(s); 76 size_t sublen = strlen(sub); 77 78 if (sublen > slen) 79 return 0; 80 81 return !strncasecmp(s + slen - sublen, sub, sublen); 82 } 83 84 /* 85 * Set multimedia files as cold files for hot/cold data separation 86 */ 87 static inline void set_cold_files(struct f2fs_sb_info *sbi, struct inode *inode, 88 const unsigned char *name) 89 { 90 int i; 91 __u8 (*extlist)[8] = sbi->raw_super->extension_list; 92 93 int count = le32_to_cpu(sbi->raw_super->extension_count); 94 for (i = 0; i < count; i++) { 95 if (is_multimedia_file(name, extlist[i])) { 96 file_set_cold(inode); 97 break; 98 } 99 } 100 } 101 102 static int f2fs_create(struct inode *dir, struct dentry *dentry, umode_t mode, 103 bool excl) 104 { 105 struct f2fs_sb_info *sbi = F2FS_I_SB(dir); 106 struct inode *inode; 107 nid_t ino = 0; 108 int err; 109 110 f2fs_balance_fs(sbi); 111 112 inode = f2fs_new_inode(dir, mode); 113 if (IS_ERR(inode)) 114 return PTR_ERR(inode); 115 116 if (!test_opt(sbi, DISABLE_EXT_IDENTIFY)) 117 set_cold_files(sbi, inode, dentry->d_name.name); 118 119 inode->i_op = &f2fs_file_inode_operations; 120 inode->i_fop = &f2fs_file_operations; 121 inode->i_mapping->a_ops = &f2fs_dblock_aops; 122 ino = inode->i_ino; 123 124 f2fs_lock_op(sbi); 125 err = f2fs_add_link(dentry, inode); 126 if (err) 127 goto out; 128 f2fs_unlock_op(sbi); 129 130 alloc_nid_done(sbi, ino); 131 132 d_instantiate(dentry, inode); 133 unlock_new_inode(inode); 134 return 0; 135 out: 136 handle_failed_inode(inode); 137 return err; 138 } 139 140 static int f2fs_link(struct dentry *old_dentry, struct inode *dir, 141 struct dentry *dentry) 142 { 143 struct inode *inode = old_dentry->d_inode; 144 struct f2fs_sb_info *sbi = F2FS_I_SB(dir); 145 int err; 146 147 f2fs_balance_fs(sbi); 148 149 inode->i_ctime = CURRENT_TIME; 150 ihold(inode); 151 152 set_inode_flag(F2FS_I(inode), FI_INC_LINK); 153 f2fs_lock_op(sbi); 154 err = f2fs_add_link(dentry, inode); 155 if (err) 156 goto out; 157 f2fs_unlock_op(sbi); 158 159 d_instantiate(dentry, inode); 160 return 0; 161 out: 162 clear_inode_flag(F2FS_I(inode), FI_INC_LINK); 163 iput(inode); 164 f2fs_unlock_op(sbi); 165 return err; 166 } 167 168 struct dentry *f2fs_get_parent(struct dentry *child) 169 { 170 struct qstr dotdot = QSTR_INIT("..", 2); 171 unsigned long ino = f2fs_inode_by_name(child->d_inode, &dotdot); 172 if (!ino) 173 return ERR_PTR(-ENOENT); 174 return d_obtain_alias(f2fs_iget(child->d_inode->i_sb, ino)); 175 } 176 177 static struct dentry *f2fs_lookup(struct inode *dir, struct dentry *dentry, 178 unsigned int flags) 179 { 180 struct inode *inode = NULL; 181 struct f2fs_dir_entry *de; 182 struct page *page; 183 184 if (dentry->d_name.len > F2FS_NAME_LEN) 185 return ERR_PTR(-ENAMETOOLONG); 186 187 de = f2fs_find_entry(dir, &dentry->d_name, &page); 188 if (de) { 189 nid_t ino = le32_to_cpu(de->ino); 190 kunmap(page); 191 f2fs_put_page(page, 0); 192 193 inode = f2fs_iget(dir->i_sb, ino); 194 if (IS_ERR(inode)) 195 return ERR_CAST(inode); 196 197 stat_inc_inline_inode(inode); 198 } 199 200 return d_splice_alias(inode, dentry); 201 } 202 203 static int f2fs_unlink(struct inode *dir, struct dentry *dentry) 204 { 205 struct f2fs_sb_info *sbi = F2FS_I_SB(dir); 206 struct inode *inode = dentry->d_inode; 207 struct f2fs_dir_entry *de; 208 struct page *page; 209 int err = -ENOENT; 210 211 trace_f2fs_unlink_enter(dir, dentry); 212 f2fs_balance_fs(sbi); 213 214 de = f2fs_find_entry(dir, &dentry->d_name, &page); 215 if (!de) 216 goto fail; 217 218 f2fs_lock_op(sbi); 219 err = acquire_orphan_inode(sbi); 220 if (err) { 221 f2fs_unlock_op(sbi); 222 kunmap(page); 223 f2fs_put_page(page, 0); 224 goto fail; 225 } 226 f2fs_delete_entry(de, page, inode); 227 f2fs_unlock_op(sbi); 228 229 /* In order to evict this inode, we set it dirty */ 230 mark_inode_dirty(inode); 231 fail: 232 trace_f2fs_unlink_exit(inode, err); 233 return err; 234 } 235 236 static int f2fs_symlink(struct inode *dir, struct dentry *dentry, 237 const char *symname) 238 { 239 struct f2fs_sb_info *sbi = F2FS_I_SB(dir); 240 struct inode *inode; 241 size_t symlen = strlen(symname) + 1; 242 int err; 243 244 f2fs_balance_fs(sbi); 245 246 inode = f2fs_new_inode(dir, S_IFLNK | S_IRWXUGO); 247 if (IS_ERR(inode)) 248 return PTR_ERR(inode); 249 250 inode->i_op = &f2fs_symlink_inode_operations; 251 inode->i_mapping->a_ops = &f2fs_dblock_aops; 252 253 f2fs_lock_op(sbi); 254 err = f2fs_add_link(dentry, inode); 255 if (err) 256 goto out; 257 f2fs_unlock_op(sbi); 258 259 err = page_symlink(inode, symname, symlen); 260 alloc_nid_done(sbi, inode->i_ino); 261 262 d_instantiate(dentry, inode); 263 unlock_new_inode(inode); 264 return err; 265 out: 266 handle_failed_inode(inode); 267 return err; 268 } 269 270 static int f2fs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode) 271 { 272 struct f2fs_sb_info *sbi = F2FS_I_SB(dir); 273 struct inode *inode; 274 int err; 275 276 f2fs_balance_fs(sbi); 277 278 inode = f2fs_new_inode(dir, S_IFDIR | mode); 279 if (IS_ERR(inode)) 280 return PTR_ERR(inode); 281 282 inode->i_op = &f2fs_dir_inode_operations; 283 inode->i_fop = &f2fs_dir_operations; 284 inode->i_mapping->a_ops = &f2fs_dblock_aops; 285 mapping_set_gfp_mask(inode->i_mapping, GFP_F2FS_ZERO); 286 287 set_inode_flag(F2FS_I(inode), FI_INC_LINK); 288 f2fs_lock_op(sbi); 289 err = f2fs_add_link(dentry, inode); 290 if (err) 291 goto out_fail; 292 f2fs_unlock_op(sbi); 293 294 alloc_nid_done(sbi, inode->i_ino); 295 296 d_instantiate(dentry, inode); 297 unlock_new_inode(inode); 298 299 return 0; 300 301 out_fail: 302 clear_inode_flag(F2FS_I(inode), FI_INC_LINK); 303 handle_failed_inode(inode); 304 return err; 305 } 306 307 static int f2fs_rmdir(struct inode *dir, struct dentry *dentry) 308 { 309 struct inode *inode = dentry->d_inode; 310 if (f2fs_empty_dir(inode)) 311 return f2fs_unlink(dir, dentry); 312 return -ENOTEMPTY; 313 } 314 315 static int f2fs_mknod(struct inode *dir, struct dentry *dentry, 316 umode_t mode, dev_t rdev) 317 { 318 struct f2fs_sb_info *sbi = F2FS_I_SB(dir); 319 struct inode *inode; 320 int err = 0; 321 322 if (!new_valid_dev(rdev)) 323 return -EINVAL; 324 325 f2fs_balance_fs(sbi); 326 327 inode = f2fs_new_inode(dir, mode); 328 if (IS_ERR(inode)) 329 return PTR_ERR(inode); 330 331 init_special_inode(inode, inode->i_mode, rdev); 332 inode->i_op = &f2fs_special_inode_operations; 333 334 f2fs_lock_op(sbi); 335 err = f2fs_add_link(dentry, inode); 336 if (err) 337 goto out; 338 f2fs_unlock_op(sbi); 339 340 alloc_nid_done(sbi, inode->i_ino); 341 d_instantiate(dentry, inode); 342 unlock_new_inode(inode); 343 return 0; 344 out: 345 handle_failed_inode(inode); 346 return err; 347 } 348 349 static int f2fs_rename(struct inode *old_dir, struct dentry *old_dentry, 350 struct inode *new_dir, struct dentry *new_dentry) 351 { 352 struct f2fs_sb_info *sbi = F2FS_I_SB(old_dir); 353 struct inode *old_inode = old_dentry->d_inode; 354 struct inode *new_inode = new_dentry->d_inode; 355 struct page *old_dir_page; 356 struct page *old_page, *new_page; 357 struct f2fs_dir_entry *old_dir_entry = NULL; 358 struct f2fs_dir_entry *old_entry; 359 struct f2fs_dir_entry *new_entry; 360 int err = -ENOENT; 361 362 f2fs_balance_fs(sbi); 363 364 old_entry = f2fs_find_entry(old_dir, &old_dentry->d_name, &old_page); 365 if (!old_entry) 366 goto out; 367 368 if (S_ISDIR(old_inode->i_mode)) { 369 err = -EIO; 370 old_dir_entry = f2fs_parent_dir(old_inode, &old_dir_page); 371 if (!old_dir_entry) 372 goto out_old; 373 } 374 375 if (new_inode) { 376 377 err = -ENOTEMPTY; 378 if (old_dir_entry && !f2fs_empty_dir(new_inode)) 379 goto out_dir; 380 381 err = -ENOENT; 382 new_entry = f2fs_find_entry(new_dir, &new_dentry->d_name, 383 &new_page); 384 if (!new_entry) 385 goto out_dir; 386 387 f2fs_lock_op(sbi); 388 389 err = acquire_orphan_inode(sbi); 390 if (err) 391 goto put_out_dir; 392 393 if (update_dent_inode(old_inode, &new_dentry->d_name)) { 394 release_orphan_inode(sbi); 395 goto put_out_dir; 396 } 397 398 f2fs_set_link(new_dir, new_entry, new_page, old_inode); 399 400 new_inode->i_ctime = CURRENT_TIME; 401 down_write(&F2FS_I(new_inode)->i_sem); 402 if (old_dir_entry) 403 drop_nlink(new_inode); 404 drop_nlink(new_inode); 405 up_write(&F2FS_I(new_inode)->i_sem); 406 407 mark_inode_dirty(new_inode); 408 409 if (!new_inode->i_nlink) 410 add_orphan_inode(sbi, new_inode->i_ino); 411 else 412 release_orphan_inode(sbi); 413 414 update_inode_page(old_inode); 415 update_inode_page(new_inode); 416 } else { 417 f2fs_lock_op(sbi); 418 419 err = f2fs_add_link(new_dentry, old_inode); 420 if (err) { 421 f2fs_unlock_op(sbi); 422 goto out_dir; 423 } 424 425 if (old_dir_entry) { 426 inc_nlink(new_dir); 427 update_inode_page(new_dir); 428 } 429 } 430 431 down_write(&F2FS_I(old_inode)->i_sem); 432 file_lost_pino(old_inode); 433 up_write(&F2FS_I(old_inode)->i_sem); 434 435 old_inode->i_ctime = CURRENT_TIME; 436 mark_inode_dirty(old_inode); 437 438 f2fs_delete_entry(old_entry, old_page, NULL); 439 440 if (old_dir_entry) { 441 if (old_dir != new_dir) { 442 f2fs_set_link(old_inode, old_dir_entry, 443 old_dir_page, new_dir); 444 update_inode_page(old_inode); 445 } else { 446 kunmap(old_dir_page); 447 f2fs_put_page(old_dir_page, 0); 448 } 449 drop_nlink(old_dir); 450 mark_inode_dirty(old_dir); 451 update_inode_page(old_dir); 452 } 453 454 f2fs_unlock_op(sbi); 455 return 0; 456 457 put_out_dir: 458 f2fs_unlock_op(sbi); 459 kunmap(new_page); 460 f2fs_put_page(new_page, 0); 461 out_dir: 462 if (old_dir_entry) { 463 kunmap(old_dir_page); 464 f2fs_put_page(old_dir_page, 0); 465 } 466 out_old: 467 kunmap(old_page); 468 f2fs_put_page(old_page, 0); 469 out: 470 return err; 471 } 472 473 static int f2fs_cross_rename(struct inode *old_dir, struct dentry *old_dentry, 474 struct inode *new_dir, struct dentry *new_dentry) 475 { 476 struct f2fs_sb_info *sbi = F2FS_I_SB(old_dir); 477 struct inode *old_inode = old_dentry->d_inode; 478 struct inode *new_inode = new_dentry->d_inode; 479 struct page *old_dir_page, *new_dir_page; 480 struct page *old_page, *new_page; 481 struct f2fs_dir_entry *old_dir_entry = NULL, *new_dir_entry = NULL; 482 struct f2fs_dir_entry *old_entry, *new_entry; 483 int old_nlink = 0, new_nlink = 0; 484 int err = -ENOENT; 485 486 f2fs_balance_fs(sbi); 487 488 old_entry = f2fs_find_entry(old_dir, &old_dentry->d_name, &old_page); 489 if (!old_entry) 490 goto out; 491 492 new_entry = f2fs_find_entry(new_dir, &new_dentry->d_name, &new_page); 493 if (!new_entry) 494 goto out_old; 495 496 /* prepare for updating ".." directory entry info later */ 497 if (old_dir != new_dir) { 498 if (S_ISDIR(old_inode->i_mode)) { 499 err = -EIO; 500 old_dir_entry = f2fs_parent_dir(old_inode, 501 &old_dir_page); 502 if (!old_dir_entry) 503 goto out_new; 504 } 505 506 if (S_ISDIR(new_inode->i_mode)) { 507 err = -EIO; 508 new_dir_entry = f2fs_parent_dir(new_inode, 509 &new_dir_page); 510 if (!new_dir_entry) 511 goto out_old_dir; 512 } 513 } 514 515 /* 516 * If cross rename between file and directory those are not 517 * in the same directory, we will inc nlink of file's parent 518 * later, so we should check upper boundary of its nlink. 519 */ 520 if ((!old_dir_entry || !new_dir_entry) && 521 old_dir_entry != new_dir_entry) { 522 old_nlink = old_dir_entry ? -1 : 1; 523 new_nlink = -old_nlink; 524 err = -EMLINK; 525 if ((old_nlink > 0 && old_inode->i_nlink >= F2FS_LINK_MAX) || 526 (new_nlink > 0 && new_inode->i_nlink >= F2FS_LINK_MAX)) 527 goto out_new_dir; 528 } 529 530 f2fs_lock_op(sbi); 531 532 err = update_dent_inode(old_inode, &new_dentry->d_name); 533 if (err) 534 goto out_unlock; 535 536 err = update_dent_inode(new_inode, &old_dentry->d_name); 537 if (err) 538 goto out_undo; 539 540 /* update ".." directory entry info of old dentry */ 541 if (old_dir_entry) 542 f2fs_set_link(old_inode, old_dir_entry, old_dir_page, new_dir); 543 544 /* update ".." directory entry info of new dentry */ 545 if (new_dir_entry) 546 f2fs_set_link(new_inode, new_dir_entry, new_dir_page, old_dir); 547 548 /* update directory entry info of old dir inode */ 549 f2fs_set_link(old_dir, old_entry, old_page, new_inode); 550 551 down_write(&F2FS_I(old_inode)->i_sem); 552 file_lost_pino(old_inode); 553 up_write(&F2FS_I(old_inode)->i_sem); 554 555 update_inode_page(old_inode); 556 557 old_dir->i_ctime = CURRENT_TIME; 558 if (old_nlink) { 559 down_write(&F2FS_I(old_dir)->i_sem); 560 if (old_nlink < 0) 561 drop_nlink(old_dir); 562 else 563 inc_nlink(old_dir); 564 up_write(&F2FS_I(old_dir)->i_sem); 565 } 566 mark_inode_dirty(old_dir); 567 update_inode_page(old_dir); 568 569 /* update directory entry info of new dir inode */ 570 f2fs_set_link(new_dir, new_entry, new_page, old_inode); 571 572 down_write(&F2FS_I(new_inode)->i_sem); 573 file_lost_pino(new_inode); 574 up_write(&F2FS_I(new_inode)->i_sem); 575 576 update_inode_page(new_inode); 577 578 new_dir->i_ctime = CURRENT_TIME; 579 if (new_nlink) { 580 down_write(&F2FS_I(new_dir)->i_sem); 581 if (new_nlink < 0) 582 drop_nlink(new_dir); 583 else 584 inc_nlink(new_dir); 585 up_write(&F2FS_I(new_dir)->i_sem); 586 } 587 mark_inode_dirty(new_dir); 588 update_inode_page(new_dir); 589 590 f2fs_unlock_op(sbi); 591 return 0; 592 out_undo: 593 /* Still we may fail to recover name info of f2fs_inode here */ 594 update_dent_inode(old_inode, &old_dentry->d_name); 595 out_unlock: 596 f2fs_unlock_op(sbi); 597 out_new_dir: 598 if (new_dir_entry) { 599 kunmap(new_dir_page); 600 f2fs_put_page(new_dir_page, 0); 601 } 602 out_old_dir: 603 if (old_dir_entry) { 604 kunmap(old_dir_page); 605 f2fs_put_page(old_dir_page, 0); 606 } 607 out_new: 608 kunmap(new_page); 609 f2fs_put_page(new_page, 0); 610 out_old: 611 kunmap(old_page); 612 f2fs_put_page(old_page, 0); 613 out: 614 return err; 615 } 616 617 static int f2fs_rename2(struct inode *old_dir, struct dentry *old_dentry, 618 struct inode *new_dir, struct dentry *new_dentry, 619 unsigned int flags) 620 { 621 if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE)) 622 return -EINVAL; 623 624 if (flags & RENAME_EXCHANGE) { 625 return f2fs_cross_rename(old_dir, old_dentry, 626 new_dir, new_dentry); 627 } 628 /* 629 * VFS has already handled the new dentry existence case, 630 * here, we just deal with "RENAME_NOREPLACE" as regular rename. 631 */ 632 return f2fs_rename(old_dir, old_dentry, new_dir, new_dentry); 633 } 634 635 static int f2fs_tmpfile(struct inode *dir, struct dentry *dentry, umode_t mode) 636 { 637 struct f2fs_sb_info *sbi = F2FS_I_SB(dir); 638 struct inode *inode; 639 int err; 640 641 inode = f2fs_new_inode(dir, mode); 642 if (IS_ERR(inode)) 643 return PTR_ERR(inode); 644 645 inode->i_op = &f2fs_file_inode_operations; 646 inode->i_fop = &f2fs_file_operations; 647 inode->i_mapping->a_ops = &f2fs_dblock_aops; 648 649 f2fs_lock_op(sbi); 650 err = acquire_orphan_inode(sbi); 651 if (err) 652 goto out; 653 654 err = f2fs_do_tmpfile(inode, dir); 655 if (err) 656 goto release_out; 657 658 /* 659 * add this non-linked tmpfile to orphan list, in this way we could 660 * remove all unused data of tmpfile after abnormal power-off. 661 */ 662 add_orphan_inode(sbi, inode->i_ino); 663 f2fs_unlock_op(sbi); 664 665 alloc_nid_done(sbi, inode->i_ino); 666 d_tmpfile(dentry, inode); 667 unlock_new_inode(inode); 668 return 0; 669 670 release_out: 671 release_orphan_inode(sbi); 672 out: 673 handle_failed_inode(inode); 674 return err; 675 } 676 677 const struct inode_operations f2fs_dir_inode_operations = { 678 .create = f2fs_create, 679 .lookup = f2fs_lookup, 680 .link = f2fs_link, 681 .unlink = f2fs_unlink, 682 .symlink = f2fs_symlink, 683 .mkdir = f2fs_mkdir, 684 .rmdir = f2fs_rmdir, 685 .mknod = f2fs_mknod, 686 .rename2 = f2fs_rename2, 687 .tmpfile = f2fs_tmpfile, 688 .getattr = f2fs_getattr, 689 .setattr = f2fs_setattr, 690 .get_acl = f2fs_get_acl, 691 .set_acl = f2fs_set_acl, 692 #ifdef CONFIG_F2FS_FS_XATTR 693 .setxattr = generic_setxattr, 694 .getxattr = generic_getxattr, 695 .listxattr = f2fs_listxattr, 696 .removexattr = generic_removexattr, 697 #endif 698 }; 699 700 const struct inode_operations f2fs_symlink_inode_operations = { 701 .readlink = generic_readlink, 702 .follow_link = page_follow_link_light, 703 .put_link = page_put_link, 704 .getattr = f2fs_getattr, 705 .setattr = f2fs_setattr, 706 #ifdef CONFIG_F2FS_FS_XATTR 707 .setxattr = generic_setxattr, 708 .getxattr = generic_getxattr, 709 .listxattr = f2fs_listxattr, 710 .removexattr = generic_removexattr, 711 #endif 712 }; 713 714 const struct inode_operations f2fs_special_inode_operations = { 715 .getattr = f2fs_getattr, 716 .setattr = f2fs_setattr, 717 .get_acl = f2fs_get_acl, 718 .set_acl = f2fs_set_acl, 719 #ifdef CONFIG_F2FS_FS_XATTR 720 .setxattr = generic_setxattr, 721 .getxattr = generic_getxattr, 722 .listxattr = f2fs_listxattr, 723 .removexattr = generic_removexattr, 724 #endif 725 }; 726