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 18 static 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 24 static unsigned int dir_buckets(unsigned int level, int dir_level) 25 { 26 if (level + dir_level < MAX_DIR_HASH_DEPTH / 2) 27 return 1 << (level + dir_level); 28 else 29 return MAX_DIR_BUCKETS; 30 } 31 32 static 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 40 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 52 static 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 62 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 68 static unsigned long dir_block_index(unsigned int level, 69 int dir_level, unsigned int idx) 70 { 71 unsigned long i; 72 unsigned long bidx = 0; 73 74 for (i = 0; i < level; i++) 75 bidx += dir_buckets(i, dir_level) * bucket_blocks(i); 76 bidx += idx * bucket_blocks(level); 77 return bidx; 78 } 79 80 static bool early_match_name(size_t namelen, f2fs_hash_t namehash, 81 struct f2fs_dir_entry *de) 82 { 83 if (le16_to_cpu(de->name_len) != namelen) 84 return false; 85 86 if (de->hash_code != namehash) 87 return false; 88 89 return true; 90 } 91 92 static struct f2fs_dir_entry *find_in_block(struct page *dentry_page, 93 struct qstr *name, int *max_slots, 94 struct page **res_page) 95 { 96 struct f2fs_dentry_block *dentry_blk; 97 struct f2fs_dir_entry *de; 98 struct f2fs_dentry_ptr d; 99 100 dentry_blk = (struct f2fs_dentry_block *)kmap(dentry_page); 101 102 make_dentry_ptr(&d, (void *)dentry_blk, 1); 103 de = find_target_dentry(name, max_slots, &d); 104 105 if (de) 106 *res_page = dentry_page; 107 else 108 kunmap(dentry_page); 109 110 /* 111 * For the most part, it should be a bug when name_len is zero. 112 * We stop here for figuring out where the bugs has occurred. 113 */ 114 f2fs_bug_on(F2FS_P_SB(dentry_page), d.max < 0); 115 return de; 116 } 117 118 struct f2fs_dir_entry *find_target_dentry(struct qstr *name, int *max_slots, 119 struct f2fs_dentry_ptr *d) 120 { 121 struct f2fs_dir_entry *de; 122 unsigned long bit_pos = 0; 123 f2fs_hash_t namehash = f2fs_dentry_hash(name); 124 int max_len = 0; 125 126 if (max_slots) 127 *max_slots = 0; 128 while (bit_pos < d->max) { 129 if (!test_bit_le(bit_pos, d->bitmap)) { 130 if (bit_pos == 0) 131 max_len = 1; 132 else if (!test_bit_le(bit_pos - 1, d->bitmap)) 133 max_len++; 134 bit_pos++; 135 continue; 136 } 137 de = &d->dentry[bit_pos]; 138 if (early_match_name(name->len, namehash, de) && 139 !memcmp(d->filename[bit_pos], name->name, name->len)) 140 goto found; 141 142 if (max_slots && *max_slots >= 0 && max_len > *max_slots) { 143 *max_slots = max_len; 144 max_len = 0; 145 } 146 147 /* remain bug on condition */ 148 if (unlikely(!de->name_len)) 149 d->max = -1; 150 151 bit_pos += GET_DENTRY_SLOTS(le16_to_cpu(de->name_len)); 152 } 153 154 de = NULL; 155 found: 156 if (max_slots && max_len > *max_slots) 157 *max_slots = max_len; 158 return de; 159 } 160 161 static struct f2fs_dir_entry *find_in_level(struct inode *dir, 162 unsigned int level, struct qstr *name, 163 f2fs_hash_t namehash, struct page **res_page) 164 { 165 int s = GET_DENTRY_SLOTS(name->len); 166 unsigned int nbucket, nblock; 167 unsigned int bidx, end_block; 168 struct page *dentry_page; 169 struct f2fs_dir_entry *de = NULL; 170 bool room = false; 171 int max_slots; 172 173 f2fs_bug_on(F2FS_I_SB(dir), level > MAX_DIR_HASH_DEPTH); 174 175 nbucket = dir_buckets(level, F2FS_I(dir)->i_dir_level); 176 nblock = bucket_blocks(level); 177 178 bidx = dir_block_index(level, F2FS_I(dir)->i_dir_level, 179 le32_to_cpu(namehash) % nbucket); 180 end_block = bidx + nblock; 181 182 for (; bidx < end_block; bidx++) { 183 /* no need to allocate new dentry pages to all the indices */ 184 dentry_page = find_data_page(dir, bidx, true); 185 if (IS_ERR(dentry_page)) { 186 room = true; 187 continue; 188 } 189 190 de = find_in_block(dentry_page, name, &max_slots, res_page); 191 if (de) 192 break; 193 194 if (max_slots >= s) 195 room = true; 196 f2fs_put_page(dentry_page, 0); 197 } 198 199 if (!de && room && F2FS_I(dir)->chash != namehash) { 200 F2FS_I(dir)->chash = namehash; 201 F2FS_I(dir)->clevel = level; 202 } 203 204 return de; 205 } 206 207 /* 208 * Find an entry in the specified directory with the wanted name. 209 * It returns the page where the entry was found (as a parameter - res_page), 210 * and the entry itself. Page is returned mapped and unlocked. 211 * Entry is guaranteed to be valid. 212 */ 213 struct f2fs_dir_entry *f2fs_find_entry(struct inode *dir, 214 struct qstr *child, struct page **res_page) 215 { 216 unsigned long npages = dir_blocks(dir); 217 struct f2fs_dir_entry *de = NULL; 218 f2fs_hash_t name_hash; 219 unsigned int max_depth; 220 unsigned int level; 221 222 if (f2fs_has_inline_dentry(dir)) 223 return find_in_inline_dir(dir, child, res_page); 224 225 if (npages == 0) 226 return NULL; 227 228 *res_page = NULL; 229 230 name_hash = f2fs_dentry_hash(child); 231 max_depth = F2FS_I(dir)->i_current_depth; 232 233 for (level = 0; level < max_depth; level++) { 234 de = find_in_level(dir, level, child, name_hash, res_page); 235 if (de) 236 break; 237 } 238 if (!de && F2FS_I(dir)->chash != name_hash) { 239 F2FS_I(dir)->chash = name_hash; 240 F2FS_I(dir)->clevel = level - 1; 241 } 242 return de; 243 } 244 245 struct f2fs_dir_entry *f2fs_parent_dir(struct inode *dir, struct page **p) 246 { 247 struct page *page; 248 struct f2fs_dir_entry *de; 249 struct f2fs_dentry_block *dentry_blk; 250 251 if (f2fs_has_inline_dentry(dir)) 252 return f2fs_parent_inline_dir(dir, p); 253 254 page = get_lock_data_page(dir, 0); 255 if (IS_ERR(page)) 256 return NULL; 257 258 dentry_blk = kmap(page); 259 de = &dentry_blk->dentry[1]; 260 *p = page; 261 unlock_page(page); 262 return de; 263 } 264 265 ino_t f2fs_inode_by_name(struct inode *dir, struct qstr *qstr) 266 { 267 ino_t res = 0; 268 struct f2fs_dir_entry *de; 269 struct page *page; 270 271 de = f2fs_find_entry(dir, qstr, &page); 272 if (de) { 273 res = le32_to_cpu(de->ino); 274 f2fs_dentry_kunmap(dir, page); 275 f2fs_put_page(page, 0); 276 } 277 278 return res; 279 } 280 281 void f2fs_set_link(struct inode *dir, struct f2fs_dir_entry *de, 282 struct page *page, struct inode *inode) 283 { 284 enum page_type type = f2fs_has_inline_dentry(dir) ? NODE : DATA; 285 lock_page(page); 286 f2fs_wait_on_page_writeback(page, type); 287 de->ino = cpu_to_le32(inode->i_ino); 288 set_de_type(de, inode); 289 if (!f2fs_has_inline_dentry(dir)) 290 kunmap(page); 291 set_page_dirty(page); 292 dir->i_mtime = dir->i_ctime = CURRENT_TIME; 293 mark_inode_dirty(dir); 294 295 f2fs_put_page(page, 1); 296 } 297 298 static void init_dent_inode(const struct qstr *name, struct page *ipage) 299 { 300 struct f2fs_inode *ri; 301 302 f2fs_wait_on_page_writeback(ipage, NODE); 303 304 /* copy name info. to this inode page */ 305 ri = F2FS_INODE(ipage); 306 ri->i_namelen = cpu_to_le32(name->len); 307 memcpy(ri->i_name, name->name, name->len); 308 set_page_dirty(ipage); 309 } 310 311 int update_dent_inode(struct inode *inode, const struct qstr *name) 312 { 313 struct page *page; 314 315 page = get_node_page(F2FS_I_SB(inode), inode->i_ino); 316 if (IS_ERR(page)) 317 return PTR_ERR(page); 318 319 init_dent_inode(name, page); 320 f2fs_put_page(page, 1); 321 322 return 0; 323 } 324 325 void do_make_empty_dir(struct inode *inode, struct inode *parent, 326 struct f2fs_dentry_ptr *d) 327 { 328 struct f2fs_dir_entry *de; 329 330 de = &d->dentry[0]; 331 de->name_len = cpu_to_le16(1); 332 de->hash_code = 0; 333 de->ino = cpu_to_le32(inode->i_ino); 334 memcpy(d->filename[0], ".", 1); 335 set_de_type(de, inode); 336 337 de = &d->dentry[1]; 338 de->hash_code = 0; 339 de->name_len = cpu_to_le16(2); 340 de->ino = cpu_to_le32(parent->i_ino); 341 memcpy(d->filename[1], "..", 2); 342 set_de_type(de, inode); 343 344 test_and_set_bit_le(0, (void *)d->bitmap); 345 test_and_set_bit_le(1, (void *)d->bitmap); 346 } 347 348 static int make_empty_dir(struct inode *inode, 349 struct inode *parent, struct page *page) 350 { 351 struct page *dentry_page; 352 struct f2fs_dentry_block *dentry_blk; 353 struct f2fs_dentry_ptr d; 354 355 if (f2fs_has_inline_dentry(inode)) 356 return make_empty_inline_dir(inode, parent, page); 357 358 dentry_page = get_new_data_page(inode, page, 0, true); 359 if (IS_ERR(dentry_page)) 360 return PTR_ERR(dentry_page); 361 362 dentry_blk = kmap_atomic(dentry_page); 363 364 make_dentry_ptr(&d, (void *)dentry_blk, 1); 365 do_make_empty_dir(inode, parent, &d); 366 367 kunmap_atomic(dentry_blk); 368 369 set_page_dirty(dentry_page); 370 f2fs_put_page(dentry_page, 1); 371 return 0; 372 } 373 374 struct page *init_inode_metadata(struct inode *inode, struct inode *dir, 375 const struct qstr *name, struct page *dpage) 376 { 377 struct page *page; 378 int err; 379 380 if (is_inode_flag_set(F2FS_I(inode), FI_NEW_INODE)) { 381 page = new_inode_page(inode); 382 if (IS_ERR(page)) 383 return page; 384 385 if (S_ISDIR(inode->i_mode)) { 386 err = make_empty_dir(inode, dir, page); 387 if (err) 388 goto error; 389 } 390 391 err = f2fs_init_acl(inode, dir, page, dpage); 392 if (err) 393 goto put_error; 394 395 err = f2fs_init_security(inode, dir, name, page); 396 if (err) 397 goto put_error; 398 } else { 399 page = get_node_page(F2FS_I_SB(dir), inode->i_ino); 400 if (IS_ERR(page)) 401 return page; 402 403 set_cold_node(inode, page); 404 } 405 406 if (name) 407 init_dent_inode(name, page); 408 409 /* 410 * This file should be checkpointed during fsync. 411 * We lost i_pino from now on. 412 */ 413 if (is_inode_flag_set(F2FS_I(inode), FI_INC_LINK)) { 414 file_lost_pino(inode); 415 /* 416 * If link the tmpfile to alias through linkat path, 417 * we should remove this inode from orphan list. 418 */ 419 if (inode->i_nlink == 0) 420 remove_orphan_inode(F2FS_I_SB(dir), inode->i_ino); 421 inc_nlink(inode); 422 } 423 return page; 424 425 put_error: 426 f2fs_put_page(page, 1); 427 error: 428 /* once the failed inode becomes a bad inode, i_mode is S_IFREG */ 429 truncate_inode_pages(&inode->i_data, 0); 430 truncate_blocks(inode, 0, false); 431 remove_dirty_dir_inode(inode); 432 remove_inode_page(inode); 433 return ERR_PTR(err); 434 } 435 436 void update_parent_metadata(struct inode *dir, struct inode *inode, 437 unsigned int current_depth) 438 { 439 if (is_inode_flag_set(F2FS_I(inode), FI_NEW_INODE)) { 440 if (S_ISDIR(inode->i_mode)) { 441 inc_nlink(dir); 442 set_inode_flag(F2FS_I(dir), FI_UPDATE_DIR); 443 } 444 clear_inode_flag(F2FS_I(inode), FI_NEW_INODE); 445 } 446 dir->i_mtime = dir->i_ctime = CURRENT_TIME; 447 mark_inode_dirty(dir); 448 449 if (F2FS_I(dir)->i_current_depth != current_depth) { 450 F2FS_I(dir)->i_current_depth = current_depth; 451 set_inode_flag(F2FS_I(dir), FI_UPDATE_DIR); 452 } 453 454 if (is_inode_flag_set(F2FS_I(inode), FI_INC_LINK)) 455 clear_inode_flag(F2FS_I(inode), FI_INC_LINK); 456 } 457 458 int room_for_filename(const void *bitmap, int slots, int max_slots) 459 { 460 int bit_start = 0; 461 int zero_start, zero_end; 462 next: 463 zero_start = find_next_zero_bit_le(bitmap, max_slots, bit_start); 464 if (zero_start >= max_slots) 465 return max_slots; 466 467 zero_end = find_next_bit_le(bitmap, max_slots, zero_start); 468 if (zero_end - zero_start >= slots) 469 return zero_start; 470 471 bit_start = zero_end + 1; 472 473 if (zero_end + 1 >= max_slots) 474 return max_slots; 475 goto next; 476 } 477 478 /* 479 * Caller should grab and release a rwsem by calling f2fs_lock_op() and 480 * f2fs_unlock_op(). 481 */ 482 int __f2fs_add_link(struct inode *dir, const struct qstr *name, 483 struct inode *inode) 484 { 485 unsigned int bit_pos; 486 unsigned int level; 487 unsigned int current_depth; 488 unsigned long bidx, block; 489 f2fs_hash_t dentry_hash; 490 struct f2fs_dir_entry *de; 491 unsigned int nbucket, nblock; 492 size_t namelen = name->len; 493 struct page *dentry_page = NULL; 494 struct f2fs_dentry_block *dentry_blk = NULL; 495 int slots = GET_DENTRY_SLOTS(namelen); 496 struct page *page; 497 int err = 0; 498 int i; 499 500 if (f2fs_has_inline_dentry(dir)) { 501 err = f2fs_add_inline_entry(dir, name, inode); 502 if (!err || err != -EAGAIN) 503 return err; 504 else 505 err = 0; 506 } 507 508 dentry_hash = f2fs_dentry_hash(name); 509 level = 0; 510 current_depth = F2FS_I(dir)->i_current_depth; 511 if (F2FS_I(dir)->chash == dentry_hash) { 512 level = F2FS_I(dir)->clevel; 513 F2FS_I(dir)->chash = 0; 514 } 515 516 start: 517 if (unlikely(current_depth == MAX_DIR_HASH_DEPTH)) 518 return -ENOSPC; 519 520 /* Increase the depth, if required */ 521 if (level == current_depth) 522 ++current_depth; 523 524 nbucket = dir_buckets(level, F2FS_I(dir)->i_dir_level); 525 nblock = bucket_blocks(level); 526 527 bidx = dir_block_index(level, F2FS_I(dir)->i_dir_level, 528 (le32_to_cpu(dentry_hash) % nbucket)); 529 530 for (block = bidx; block <= (bidx + nblock - 1); block++) { 531 dentry_page = get_new_data_page(dir, NULL, block, true); 532 if (IS_ERR(dentry_page)) 533 return PTR_ERR(dentry_page); 534 535 dentry_blk = kmap(dentry_page); 536 bit_pos = room_for_filename(&dentry_blk->dentry_bitmap, 537 slots, NR_DENTRY_IN_BLOCK); 538 if (bit_pos < NR_DENTRY_IN_BLOCK) 539 goto add_dentry; 540 541 kunmap(dentry_page); 542 f2fs_put_page(dentry_page, 1); 543 } 544 545 /* Move to next level to find the empty slot for new dentry */ 546 ++level; 547 goto start; 548 add_dentry: 549 f2fs_wait_on_page_writeback(dentry_page, DATA); 550 551 down_write(&F2FS_I(inode)->i_sem); 552 page = init_inode_metadata(inode, dir, name, NULL); 553 if (IS_ERR(page)) { 554 err = PTR_ERR(page); 555 goto fail; 556 } 557 de = &dentry_blk->dentry[bit_pos]; 558 de->hash_code = dentry_hash; 559 de->name_len = cpu_to_le16(namelen); 560 memcpy(dentry_blk->filename[bit_pos], name->name, name->len); 561 de->ino = cpu_to_le32(inode->i_ino); 562 set_de_type(de, inode); 563 for (i = 0; i < slots; i++) 564 test_and_set_bit_le(bit_pos + i, &dentry_blk->dentry_bitmap); 565 set_page_dirty(dentry_page); 566 567 /* we don't need to mark_inode_dirty now */ 568 F2FS_I(inode)->i_pino = dir->i_ino; 569 update_inode(inode, page); 570 f2fs_put_page(page, 1); 571 572 update_parent_metadata(dir, inode, current_depth); 573 fail: 574 up_write(&F2FS_I(inode)->i_sem); 575 576 if (is_inode_flag_set(F2FS_I(dir), FI_UPDATE_DIR)) { 577 update_inode_page(dir); 578 clear_inode_flag(F2FS_I(dir), FI_UPDATE_DIR); 579 } 580 kunmap(dentry_page); 581 f2fs_put_page(dentry_page, 1); 582 return err; 583 } 584 585 int f2fs_do_tmpfile(struct inode *inode, struct inode *dir) 586 { 587 struct page *page; 588 int err = 0; 589 590 down_write(&F2FS_I(inode)->i_sem); 591 page = init_inode_metadata(inode, dir, NULL, NULL); 592 if (IS_ERR(page)) { 593 err = PTR_ERR(page); 594 goto fail; 595 } 596 /* we don't need to mark_inode_dirty now */ 597 update_inode(inode, page); 598 f2fs_put_page(page, 1); 599 600 clear_inode_flag(F2FS_I(inode), FI_NEW_INODE); 601 fail: 602 up_write(&F2FS_I(inode)->i_sem); 603 return err; 604 } 605 606 void f2fs_drop_nlink(struct inode *dir, struct inode *inode, struct page *page) 607 { 608 struct f2fs_sb_info *sbi = F2FS_I_SB(dir); 609 610 down_write(&F2FS_I(inode)->i_sem); 611 612 if (S_ISDIR(inode->i_mode)) { 613 drop_nlink(dir); 614 if (page) 615 update_inode(dir, page); 616 else 617 update_inode_page(dir); 618 } 619 inode->i_ctime = CURRENT_TIME; 620 621 drop_nlink(inode); 622 if (S_ISDIR(inode->i_mode)) { 623 drop_nlink(inode); 624 i_size_write(inode, 0); 625 } 626 up_write(&F2FS_I(inode)->i_sem); 627 update_inode_page(inode); 628 629 if (inode->i_nlink == 0) 630 add_orphan_inode(sbi, inode->i_ino); 631 else 632 release_orphan_inode(sbi); 633 } 634 635 /* 636 * It only removes the dentry from the dentry page, corresponding name 637 * entry in name page does not need to be touched during deletion. 638 */ 639 void f2fs_delete_entry(struct f2fs_dir_entry *dentry, struct page *page, 640 struct inode *dir, struct inode *inode) 641 { 642 struct f2fs_dentry_block *dentry_blk; 643 unsigned int bit_pos; 644 int slots = GET_DENTRY_SLOTS(le16_to_cpu(dentry->name_len)); 645 int i; 646 647 if (f2fs_has_inline_dentry(dir)) 648 return f2fs_delete_inline_entry(dentry, page, dir, inode); 649 650 lock_page(page); 651 f2fs_wait_on_page_writeback(page, DATA); 652 653 dentry_blk = page_address(page); 654 bit_pos = dentry - dentry_blk->dentry; 655 for (i = 0; i < slots; i++) 656 clear_bit_le(bit_pos + i, &dentry_blk->dentry_bitmap); 657 658 /* Let's check and deallocate this dentry page */ 659 bit_pos = find_next_bit_le(&dentry_blk->dentry_bitmap, 660 NR_DENTRY_IN_BLOCK, 661 0); 662 kunmap(page); /* kunmap - pair of f2fs_find_entry */ 663 set_page_dirty(page); 664 665 dir->i_ctime = dir->i_mtime = CURRENT_TIME; 666 667 if (inode) 668 f2fs_drop_nlink(dir, inode, NULL); 669 670 if (bit_pos == NR_DENTRY_IN_BLOCK) { 671 truncate_hole(dir, page->index, page->index + 1); 672 clear_page_dirty_for_io(page); 673 ClearPageUptodate(page); 674 inode_dec_dirty_pages(dir); 675 } 676 f2fs_put_page(page, 1); 677 } 678 679 bool f2fs_empty_dir(struct inode *dir) 680 { 681 unsigned long bidx; 682 struct page *dentry_page; 683 unsigned int bit_pos; 684 struct f2fs_dentry_block *dentry_blk; 685 unsigned long nblock = dir_blocks(dir); 686 687 if (f2fs_has_inline_dentry(dir)) 688 return f2fs_empty_inline_dir(dir); 689 690 for (bidx = 0; bidx < nblock; bidx++) { 691 dentry_page = get_lock_data_page(dir, bidx); 692 if (IS_ERR(dentry_page)) { 693 if (PTR_ERR(dentry_page) == -ENOENT) 694 continue; 695 else 696 return false; 697 } 698 699 dentry_blk = kmap_atomic(dentry_page); 700 if (bidx == 0) 701 bit_pos = 2; 702 else 703 bit_pos = 0; 704 bit_pos = find_next_bit_le(&dentry_blk->dentry_bitmap, 705 NR_DENTRY_IN_BLOCK, 706 bit_pos); 707 kunmap_atomic(dentry_blk); 708 709 f2fs_put_page(dentry_page, 1); 710 711 if (bit_pos < NR_DENTRY_IN_BLOCK) 712 return false; 713 } 714 return true; 715 } 716 717 bool f2fs_fill_dentries(struct dir_context *ctx, struct f2fs_dentry_ptr *d, 718 unsigned int start_pos) 719 { 720 unsigned char d_type = DT_UNKNOWN; 721 unsigned int bit_pos; 722 struct f2fs_dir_entry *de = NULL; 723 724 bit_pos = ((unsigned long)ctx->pos % d->max); 725 726 while (bit_pos < d->max) { 727 bit_pos = find_next_bit_le(d->bitmap, d->max, bit_pos); 728 if (bit_pos >= d->max) 729 break; 730 731 de = &d->dentry[bit_pos]; 732 if (de->file_type < F2FS_FT_MAX) 733 d_type = f2fs_filetype_table[de->file_type]; 734 else 735 d_type = DT_UNKNOWN; 736 if (!dir_emit(ctx, d->filename[bit_pos], 737 le16_to_cpu(de->name_len), 738 le32_to_cpu(de->ino), d_type)) 739 return true; 740 741 bit_pos += GET_DENTRY_SLOTS(le16_to_cpu(de->name_len)); 742 ctx->pos = start_pos + bit_pos; 743 } 744 return false; 745 } 746 747 static int f2fs_readdir(struct file *file, struct dir_context *ctx) 748 { 749 struct inode *inode = file_inode(file); 750 unsigned long npages = dir_blocks(inode); 751 struct f2fs_dentry_block *dentry_blk = NULL; 752 struct page *dentry_page = NULL; 753 struct file_ra_state *ra = &file->f_ra; 754 unsigned int n = ((unsigned long)ctx->pos / NR_DENTRY_IN_BLOCK); 755 struct f2fs_dentry_ptr d; 756 757 if (f2fs_has_inline_dentry(inode)) 758 return f2fs_read_inline_dir(file, ctx); 759 760 /* readahead for multi pages of dir */ 761 if (npages - n > 1 && !ra_has_index(ra, n)) 762 page_cache_sync_readahead(inode->i_mapping, ra, file, n, 763 min(npages - n, (pgoff_t)MAX_DIR_RA_PAGES)); 764 765 for (; n < npages; n++) { 766 dentry_page = get_lock_data_page(inode, n); 767 if (IS_ERR(dentry_page)) 768 continue; 769 770 dentry_blk = kmap(dentry_page); 771 772 make_dentry_ptr(&d, (void *)dentry_blk, 1); 773 774 if (f2fs_fill_dentries(ctx, &d, n * NR_DENTRY_IN_BLOCK)) 775 goto stop; 776 777 ctx->pos = (n + 1) * NR_DENTRY_IN_BLOCK; 778 kunmap(dentry_page); 779 f2fs_put_page(dentry_page, 1); 780 dentry_page = NULL; 781 } 782 stop: 783 if (dentry_page && !IS_ERR(dentry_page)) { 784 kunmap(dentry_page); 785 f2fs_put_page(dentry_page, 1); 786 } 787 788 return 0; 789 } 790 791 const struct file_operations f2fs_dir_operations = { 792 .llseek = generic_file_llseek, 793 .read = generic_read_dir, 794 .iterate = f2fs_readdir, 795 .fsync = f2fs_sync_file, 796 .unlocked_ioctl = f2fs_ioctl, 797 }; 798