dir.c revision 924a2ddbd0c2829ebca9ac899522cbb16a9b6d8c
1/* 2 * fs/f2fs/dir.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 "f2fs.h" 14#include "node.h" 15#include "acl.h" 16#include "xattr.h" 17 18static unsigned long dir_blocks(struct inode *inode) 19{ 20 return ((unsigned long long) (i_size_read(inode) + PAGE_CACHE_SIZE - 1)) 21 >> PAGE_CACHE_SHIFT; 22} 23 24static unsigned int dir_buckets(unsigned int level) 25{ 26 if (level < MAX_DIR_HASH_DEPTH / 2) 27 return 1 << level; 28 else 29 return 1 << ((MAX_DIR_HASH_DEPTH / 2) - 1); 30} 31 32static unsigned int bucket_blocks(unsigned int level) 33{ 34 if (level < MAX_DIR_HASH_DEPTH / 2) 35 return 2; 36 else 37 return 4; 38} 39 40static unsigned char f2fs_filetype_table[F2FS_FT_MAX] = { 41 [F2FS_FT_UNKNOWN] = DT_UNKNOWN, 42 [F2FS_FT_REG_FILE] = DT_REG, 43 [F2FS_FT_DIR] = DT_DIR, 44 [F2FS_FT_CHRDEV] = DT_CHR, 45 [F2FS_FT_BLKDEV] = DT_BLK, 46 [F2FS_FT_FIFO] = DT_FIFO, 47 [F2FS_FT_SOCK] = DT_SOCK, 48 [F2FS_FT_SYMLINK] = DT_LNK, 49}; 50 51#define S_SHIFT 12 52static unsigned char f2fs_type_by_mode[S_IFMT >> S_SHIFT] = { 53 [S_IFREG >> S_SHIFT] = F2FS_FT_REG_FILE, 54 [S_IFDIR >> S_SHIFT] = F2FS_FT_DIR, 55 [S_IFCHR >> S_SHIFT] = F2FS_FT_CHRDEV, 56 [S_IFBLK >> S_SHIFT] = F2FS_FT_BLKDEV, 57 [S_IFIFO >> S_SHIFT] = F2FS_FT_FIFO, 58 [S_IFSOCK >> S_SHIFT] = F2FS_FT_SOCK, 59 [S_IFLNK >> S_SHIFT] = F2FS_FT_SYMLINK, 60}; 61 62static void set_de_type(struct f2fs_dir_entry *de, struct inode *inode) 63{ 64 umode_t mode = inode->i_mode; 65 de->file_type = f2fs_type_by_mode[(mode & S_IFMT) >> S_SHIFT]; 66} 67 68static unsigned long dir_block_index(unsigned int level, unsigned int idx) 69{ 70 unsigned long i; 71 unsigned long bidx = 0; 72 73 for (i = 0; i < level; i++) 74 bidx += dir_buckets(i) * bucket_blocks(i); 75 bidx += idx * bucket_blocks(level); 76 return bidx; 77} 78 79static bool early_match_name(const char *name, size_t namelen, 80 f2fs_hash_t namehash, struct f2fs_dir_entry *de) 81{ 82 if (le16_to_cpu(de->name_len) != namelen) 83 return false; 84 85 if (de->hash_code != namehash) 86 return false; 87 88 return true; 89} 90 91static struct f2fs_dir_entry *find_in_block(struct page *dentry_page, 92 const char *name, size_t namelen, int *max_slots, 93 f2fs_hash_t namehash, struct page **res_page) 94{ 95 struct f2fs_dir_entry *de; 96 unsigned long bit_pos, end_pos, next_pos; 97 struct f2fs_dentry_block *dentry_blk = kmap(dentry_page); 98 int slots; 99 100 bit_pos = find_next_bit_le(&dentry_blk->dentry_bitmap, 101 NR_DENTRY_IN_BLOCK, 0); 102 while (bit_pos < NR_DENTRY_IN_BLOCK) { 103 de = &dentry_blk->dentry[bit_pos]; 104 slots = GET_DENTRY_SLOTS(le16_to_cpu(de->name_len)); 105 106 if (early_match_name(name, namelen, namehash, de)) { 107 if (!memcmp(dentry_blk->filename[bit_pos], 108 name, namelen)) { 109 *res_page = dentry_page; 110 goto found; 111 } 112 } 113 next_pos = bit_pos + slots; 114 bit_pos = find_next_bit_le(&dentry_blk->dentry_bitmap, 115 NR_DENTRY_IN_BLOCK, next_pos); 116 if (bit_pos >= NR_DENTRY_IN_BLOCK) 117 end_pos = NR_DENTRY_IN_BLOCK; 118 else 119 end_pos = bit_pos; 120 if (*max_slots < end_pos - next_pos) 121 *max_slots = end_pos - next_pos; 122 } 123 124 de = NULL; 125 kunmap(dentry_page); 126found: 127 return de; 128} 129 130static struct f2fs_dir_entry *find_in_level(struct inode *dir, 131 unsigned int level, const char *name, size_t namelen, 132 f2fs_hash_t namehash, struct page **res_page) 133{ 134 int s = GET_DENTRY_SLOTS(namelen); 135 unsigned int nbucket, nblock; 136 unsigned int bidx, end_block; 137 struct page *dentry_page; 138 struct f2fs_dir_entry *de = NULL; 139 bool room = false; 140 int max_slots = 0; 141 142 f2fs_bug_on(level > MAX_DIR_HASH_DEPTH); 143 144 nbucket = dir_buckets(level); 145 nblock = bucket_blocks(level); 146 147 bidx = dir_block_index(level, le32_to_cpu(namehash) % nbucket); 148 end_block = bidx + nblock; 149 150 for (; bidx < end_block; bidx++) { 151 /* no need to allocate new dentry pages to all the indices */ 152 dentry_page = find_data_page(dir, bidx, true); 153 if (IS_ERR(dentry_page)) { 154 room = true; 155 continue; 156 } 157 158 de = find_in_block(dentry_page, name, namelen, 159 &max_slots, namehash, res_page); 160 if (de) 161 break; 162 163 if (max_slots >= s) 164 room = true; 165 f2fs_put_page(dentry_page, 0); 166 } 167 168 if (!de && room && F2FS_I(dir)->chash != namehash) { 169 F2FS_I(dir)->chash = namehash; 170 F2FS_I(dir)->clevel = level; 171 } 172 173 return de; 174} 175 176/* 177 * Find an entry in the specified directory with the wanted name. 178 * It returns the page where the entry was found (as a parameter - res_page), 179 * and the entry itself. Page is returned mapped and unlocked. 180 * Entry is guaranteed to be valid. 181 */ 182struct f2fs_dir_entry *f2fs_find_entry(struct inode *dir, 183 struct qstr *child, struct page **res_page) 184{ 185 const char *name = child->name; 186 size_t namelen = child->len; 187 unsigned long npages = dir_blocks(dir); 188 struct f2fs_dir_entry *de = NULL; 189 f2fs_hash_t name_hash; 190 unsigned int max_depth; 191 unsigned int level; 192 193 if (npages == 0) 194 return NULL; 195 196 *res_page = NULL; 197 198 name_hash = f2fs_dentry_hash(name, namelen); 199 max_depth = F2FS_I(dir)->i_current_depth; 200 201 for (level = 0; level < max_depth; level++) { 202 de = find_in_level(dir, level, name, 203 namelen, name_hash, res_page); 204 if (de) 205 break; 206 } 207 if (!de && F2FS_I(dir)->chash != name_hash) { 208 F2FS_I(dir)->chash = name_hash; 209 F2FS_I(dir)->clevel = level - 1; 210 } 211 return de; 212} 213 214struct f2fs_dir_entry *f2fs_parent_dir(struct inode *dir, struct page **p) 215{ 216 struct page *page; 217 struct f2fs_dir_entry *de; 218 struct f2fs_dentry_block *dentry_blk; 219 220 page = get_lock_data_page(dir, 0); 221 if (IS_ERR(page)) 222 return NULL; 223 224 dentry_blk = kmap(page); 225 de = &dentry_blk->dentry[1]; 226 *p = page; 227 unlock_page(page); 228 return de; 229} 230 231ino_t f2fs_inode_by_name(struct inode *dir, struct qstr *qstr) 232{ 233 ino_t res = 0; 234 struct f2fs_dir_entry *de; 235 struct page *page; 236 237 de = f2fs_find_entry(dir, qstr, &page); 238 if (de) { 239 res = le32_to_cpu(de->ino); 240 kunmap(page); 241 f2fs_put_page(page, 0); 242 } 243 244 return res; 245} 246 247void f2fs_set_link(struct inode *dir, struct f2fs_dir_entry *de, 248 struct page *page, struct inode *inode) 249{ 250 lock_page(page); 251 wait_on_page_writeback(page); 252 de->ino = cpu_to_le32(inode->i_ino); 253 set_de_type(de, inode); 254 kunmap(page); 255 set_page_dirty(page); 256 dir->i_mtime = dir->i_ctime = CURRENT_TIME; 257 mark_inode_dirty(dir); 258 259 f2fs_put_page(page, 1); 260} 261 262static void init_dent_inode(const struct qstr *name, struct page *ipage) 263{ 264 struct f2fs_inode *ri; 265 266 /* copy name info. to this inode page */ 267 ri = F2FS_INODE(ipage); 268 ri->i_namelen = cpu_to_le32(name->len); 269 memcpy(ri->i_name, name->name, name->len); 270 set_page_dirty(ipage); 271} 272 273int update_dent_inode(struct inode *inode, const struct qstr *name) 274{ 275 struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb); 276 struct page *page; 277 278 page = get_node_page(sbi, inode->i_ino); 279 if (IS_ERR(page)) 280 return PTR_ERR(page); 281 282 init_dent_inode(name, page); 283 f2fs_put_page(page, 1); 284 285 return 0; 286} 287 288static int make_empty_dir(struct inode *inode, 289 struct inode *parent, struct page *page) 290{ 291 struct page *dentry_page; 292 struct f2fs_dentry_block *dentry_blk; 293 struct f2fs_dir_entry *de; 294 void *kaddr; 295 296 dentry_page = get_new_data_page(inode, page, 0, true); 297 if (IS_ERR(dentry_page)) 298 return PTR_ERR(dentry_page); 299 300 kaddr = kmap_atomic(dentry_page); 301 dentry_blk = (struct f2fs_dentry_block *)kaddr; 302 303 de = &dentry_blk->dentry[0]; 304 de->name_len = cpu_to_le16(1); 305 de->hash_code = 0; 306 de->ino = cpu_to_le32(inode->i_ino); 307 memcpy(dentry_blk->filename[0], ".", 1); 308 set_de_type(de, inode); 309 310 de = &dentry_blk->dentry[1]; 311 de->hash_code = 0; 312 de->name_len = cpu_to_le16(2); 313 de->ino = cpu_to_le32(parent->i_ino); 314 memcpy(dentry_blk->filename[1], "..", 2); 315 set_de_type(de, inode); 316 317 test_and_set_bit_le(0, &dentry_blk->dentry_bitmap); 318 test_and_set_bit_le(1, &dentry_blk->dentry_bitmap); 319 kunmap_atomic(kaddr); 320 321 set_page_dirty(dentry_page); 322 f2fs_put_page(dentry_page, 1); 323 return 0; 324} 325 326static struct page *init_inode_metadata(struct inode *inode, 327 struct inode *dir, const struct qstr *name) 328{ 329 struct page *page; 330 int err; 331 332 if (is_inode_flag_set(F2FS_I(inode), FI_NEW_INODE)) { 333 page = new_inode_page(inode, name); 334 if (IS_ERR(page)) 335 return page; 336 337 if (S_ISDIR(inode->i_mode)) { 338 err = make_empty_dir(inode, dir, page); 339 if (err) 340 goto error; 341 } 342 343 err = f2fs_init_acl(inode, dir, page); 344 if (err) 345 goto put_error; 346 347 err = f2fs_init_security(inode, dir, name, page); 348 if (err) 349 goto put_error; 350 351 wait_on_page_writeback(page); 352 } else { 353 page = get_node_page(F2FS_SB(dir->i_sb), inode->i_ino); 354 if (IS_ERR(page)) 355 return page; 356 357 wait_on_page_writeback(page); 358 set_cold_node(inode, page); 359 } 360 361 init_dent_inode(name, page); 362 363 /* 364 * This file should be checkpointed during fsync. 365 * We lost i_pino from now on. 366 */ 367 if (is_inode_flag_set(F2FS_I(inode), FI_INC_LINK)) { 368 file_lost_pino(inode); 369 inc_nlink(inode); 370 } 371 return page; 372 373put_error: 374 f2fs_put_page(page, 1); 375error: 376 remove_inode_page(inode); 377 return ERR_PTR(err); 378} 379 380static void update_parent_metadata(struct inode *dir, struct inode *inode, 381 unsigned int current_depth) 382{ 383 if (is_inode_flag_set(F2FS_I(inode), FI_NEW_INODE)) { 384 if (S_ISDIR(inode->i_mode)) { 385 inc_nlink(dir); 386 set_inode_flag(F2FS_I(dir), FI_UPDATE_DIR); 387 } 388 clear_inode_flag(F2FS_I(inode), FI_NEW_INODE); 389 } 390 dir->i_mtime = dir->i_ctime = CURRENT_TIME; 391 mark_inode_dirty(dir); 392 393 if (F2FS_I(dir)->i_current_depth != current_depth) { 394 F2FS_I(dir)->i_current_depth = current_depth; 395 set_inode_flag(F2FS_I(dir), FI_UPDATE_DIR); 396 } 397 398 if (is_inode_flag_set(F2FS_I(inode), FI_INC_LINK)) 399 clear_inode_flag(F2FS_I(inode), FI_INC_LINK); 400} 401 402static int room_for_filename(struct f2fs_dentry_block *dentry_blk, int slots) 403{ 404 int bit_start = 0; 405 int zero_start, zero_end; 406next: 407 zero_start = find_next_zero_bit_le(&dentry_blk->dentry_bitmap, 408 NR_DENTRY_IN_BLOCK, 409 bit_start); 410 if (zero_start >= NR_DENTRY_IN_BLOCK) 411 return NR_DENTRY_IN_BLOCK; 412 413 zero_end = find_next_bit_le(&dentry_blk->dentry_bitmap, 414 NR_DENTRY_IN_BLOCK, 415 zero_start); 416 if (zero_end - zero_start >= slots) 417 return zero_start; 418 419 bit_start = zero_end + 1; 420 421 if (zero_end + 1 >= NR_DENTRY_IN_BLOCK) 422 return NR_DENTRY_IN_BLOCK; 423 goto next; 424} 425 426/* 427 * Caller should grab and release a rwsem by calling f2fs_lock_op() and 428 * f2fs_unlock_op(). 429 */ 430int __f2fs_add_link(struct inode *dir, const struct qstr *name, 431 struct inode *inode) 432{ 433 unsigned int bit_pos; 434 unsigned int level; 435 unsigned int current_depth; 436 unsigned long bidx, block; 437 f2fs_hash_t dentry_hash; 438 struct f2fs_dir_entry *de; 439 unsigned int nbucket, nblock; 440 size_t namelen = name->len; 441 struct page *dentry_page = NULL; 442 struct f2fs_dentry_block *dentry_blk = NULL; 443 int slots = GET_DENTRY_SLOTS(namelen); 444 struct page *page; 445 int err = 0; 446 int i; 447 448 dentry_hash = f2fs_dentry_hash(name->name, name->len); 449 level = 0; 450 current_depth = F2FS_I(dir)->i_current_depth; 451 if (F2FS_I(dir)->chash == dentry_hash) { 452 level = F2FS_I(dir)->clevel; 453 F2FS_I(dir)->chash = 0; 454 } 455 456start: 457 if (unlikely(current_depth == MAX_DIR_HASH_DEPTH)) 458 return -ENOSPC; 459 460 /* Increase the depth, if required */ 461 if (level == current_depth) 462 ++current_depth; 463 464 nbucket = dir_buckets(level); 465 nblock = bucket_blocks(level); 466 467 bidx = dir_block_index(level, (le32_to_cpu(dentry_hash) % nbucket)); 468 469 for (block = bidx; block <= (bidx + nblock - 1); block++) { 470 dentry_page = get_new_data_page(dir, NULL, block, true); 471 if (IS_ERR(dentry_page)) 472 return PTR_ERR(dentry_page); 473 474 dentry_blk = kmap(dentry_page); 475 bit_pos = room_for_filename(dentry_blk, slots); 476 if (bit_pos < NR_DENTRY_IN_BLOCK) 477 goto add_dentry; 478 479 kunmap(dentry_page); 480 f2fs_put_page(dentry_page, 1); 481 } 482 483 /* Move to next level to find the empty slot for new dentry */ 484 ++level; 485 goto start; 486add_dentry: 487 wait_on_page_writeback(dentry_page); 488 489 page = init_inode_metadata(inode, dir, name); 490 if (IS_ERR(page)) { 491 err = PTR_ERR(page); 492 goto fail; 493 } 494 de = &dentry_blk->dentry[bit_pos]; 495 de->hash_code = dentry_hash; 496 de->name_len = cpu_to_le16(namelen); 497 memcpy(dentry_blk->filename[bit_pos], name->name, name->len); 498 de->ino = cpu_to_le32(inode->i_ino); 499 set_de_type(de, inode); 500 for (i = 0; i < slots; i++) 501 test_and_set_bit_le(bit_pos + i, &dentry_blk->dentry_bitmap); 502 set_page_dirty(dentry_page); 503 504 /* we don't need to mark_inode_dirty now */ 505 F2FS_I(inode)->i_pino = dir->i_ino; 506 update_inode(inode, page); 507 f2fs_put_page(page, 1); 508 509 update_parent_metadata(dir, inode, current_depth); 510fail: 511 if (is_inode_flag_set(F2FS_I(dir), FI_UPDATE_DIR)) { 512 update_inode_page(dir); 513 clear_inode_flag(F2FS_I(dir), FI_UPDATE_DIR); 514 } 515 kunmap(dentry_page); 516 f2fs_put_page(dentry_page, 1); 517 return err; 518} 519 520/* 521 * It only removes the dentry from the dentry page,corresponding name 522 * entry in name page does not need to be touched during deletion. 523 */ 524void f2fs_delete_entry(struct f2fs_dir_entry *dentry, struct page *page, 525 struct inode *inode) 526{ 527 struct f2fs_dentry_block *dentry_blk; 528 unsigned int bit_pos; 529 struct address_space *mapping = page->mapping; 530 struct inode *dir = mapping->host; 531 struct f2fs_sb_info *sbi = F2FS_SB(dir->i_sb); 532 int slots = GET_DENTRY_SLOTS(le16_to_cpu(dentry->name_len)); 533 void *kaddr = page_address(page); 534 int i; 535 536 lock_page(page); 537 wait_on_page_writeback(page); 538 539 dentry_blk = (struct f2fs_dentry_block *)kaddr; 540 bit_pos = dentry - (struct f2fs_dir_entry *)dentry_blk->dentry; 541 for (i = 0; i < slots; i++) 542 test_and_clear_bit_le(bit_pos + i, &dentry_blk->dentry_bitmap); 543 544 /* Let's check and deallocate this dentry page */ 545 bit_pos = find_next_bit_le(&dentry_blk->dentry_bitmap, 546 NR_DENTRY_IN_BLOCK, 547 0); 548 kunmap(page); /* kunmap - pair of f2fs_find_entry */ 549 set_page_dirty(page); 550 551 dir->i_ctime = dir->i_mtime = CURRENT_TIME; 552 553 if (inode) { 554 if (S_ISDIR(inode->i_mode)) { 555 drop_nlink(dir); 556 update_inode_page(dir); 557 } 558 inode->i_ctime = CURRENT_TIME; 559 drop_nlink(inode); 560 if (S_ISDIR(inode->i_mode)) { 561 drop_nlink(inode); 562 i_size_write(inode, 0); 563 } 564 update_inode_page(inode); 565 566 if (inode->i_nlink == 0) 567 add_orphan_inode(sbi, inode->i_ino); 568 else 569 release_orphan_inode(sbi); 570 } 571 572 if (bit_pos == NR_DENTRY_IN_BLOCK) { 573 truncate_hole(dir, page->index, page->index + 1); 574 clear_page_dirty_for_io(page); 575 ClearPageUptodate(page); 576 dec_page_count(sbi, F2FS_DIRTY_DENTS); 577 inode_dec_dirty_dents(dir); 578 } 579 f2fs_put_page(page, 1); 580} 581 582bool f2fs_empty_dir(struct inode *dir) 583{ 584 unsigned long bidx; 585 struct page *dentry_page; 586 unsigned int bit_pos; 587 struct f2fs_dentry_block *dentry_blk; 588 unsigned long nblock = dir_blocks(dir); 589 590 for (bidx = 0; bidx < nblock; bidx++) { 591 void *kaddr; 592 dentry_page = get_lock_data_page(dir, bidx); 593 if (IS_ERR(dentry_page)) { 594 if (PTR_ERR(dentry_page) == -ENOENT) 595 continue; 596 else 597 return false; 598 } 599 600 kaddr = kmap_atomic(dentry_page); 601 dentry_blk = (struct f2fs_dentry_block *)kaddr; 602 if (bidx == 0) 603 bit_pos = 2; 604 else 605 bit_pos = 0; 606 bit_pos = find_next_bit_le(&dentry_blk->dentry_bitmap, 607 NR_DENTRY_IN_BLOCK, 608 bit_pos); 609 kunmap_atomic(kaddr); 610 611 f2fs_put_page(dentry_page, 1); 612 613 if (bit_pos < NR_DENTRY_IN_BLOCK) 614 return false; 615 } 616 return true; 617} 618 619static int f2fs_readdir(struct file *file, struct dir_context *ctx) 620{ 621 struct inode *inode = file_inode(file); 622 unsigned long npages = dir_blocks(inode); 623 unsigned int bit_pos = 0; 624 struct f2fs_dentry_block *dentry_blk = NULL; 625 struct f2fs_dir_entry *de = NULL; 626 struct page *dentry_page = NULL; 627 unsigned int n = ((unsigned long)ctx->pos / NR_DENTRY_IN_BLOCK); 628 unsigned char d_type = DT_UNKNOWN; 629 630 bit_pos = ((unsigned long)ctx->pos % NR_DENTRY_IN_BLOCK); 631 632 for (; n < npages; n++) { 633 dentry_page = get_lock_data_page(inode, n); 634 if (IS_ERR(dentry_page)) 635 continue; 636 637 dentry_blk = kmap(dentry_page); 638 while (bit_pos < NR_DENTRY_IN_BLOCK) { 639 bit_pos = find_next_bit_le(&dentry_blk->dentry_bitmap, 640 NR_DENTRY_IN_BLOCK, 641 bit_pos); 642 if (bit_pos >= NR_DENTRY_IN_BLOCK) 643 break; 644 645 de = &dentry_blk->dentry[bit_pos]; 646 if (de->file_type < F2FS_FT_MAX) 647 d_type = f2fs_filetype_table[de->file_type]; 648 else 649 d_type = DT_UNKNOWN; 650 if (!dir_emit(ctx, 651 dentry_blk->filename[bit_pos], 652 le16_to_cpu(de->name_len), 653 le32_to_cpu(de->ino), d_type)) 654 goto stop; 655 656 bit_pos += GET_DENTRY_SLOTS(le16_to_cpu(de->name_len)); 657 ctx->pos = n * NR_DENTRY_IN_BLOCK + bit_pos; 658 } 659 bit_pos = 0; 660 ctx->pos = (n + 1) * NR_DENTRY_IN_BLOCK; 661 kunmap(dentry_page); 662 f2fs_put_page(dentry_page, 1); 663 dentry_page = NULL; 664 } 665stop: 666 if (dentry_page && !IS_ERR(dentry_page)) { 667 kunmap(dentry_page); 668 f2fs_put_page(dentry_page, 1); 669 } 670 671 return 0; 672} 673 674const struct file_operations f2fs_dir_operations = { 675 .llseek = generic_file_llseek, 676 .read = generic_read_dir, 677 .iterate = f2fs_readdir, 678 .fsync = f2fs_sync_file, 679 .unlocked_ioctl = f2fs_ioctl, 680}; 681