1// SPDX-License-Identifier: GPL-2.0-or-later 2/* 3 * Copyright (C) 2012-2013 Samsung Electronics Co., Ltd. 4 */ 5 6#include <linux/slab.h> 7#include <linux/bio.h> 8#include <linux/buffer_head.h> 9 10#include "exfat_raw.h" 11#include "exfat_fs.h" 12 13static int exfat_extract_uni_name(struct exfat_dentry *ep, 14 unsigned short *uniname) 15{ 16 int i, len = 0; 17 18 for (i = 0; i < EXFAT_FILE_NAME_LEN; i++) { 19 *uniname = le16_to_cpu(ep->dentry.name.unicode_0_14[i]); 20 if (*uniname == 0x0) 21 return len; 22 uniname++; 23 len++; 24 } 25 26 *uniname = 0x0; 27 return len; 28 29} 30 31static void exfat_get_uniname_from_ext_entry(struct super_block *sb, 32 struct exfat_chain *p_dir, int entry, unsigned short *uniname) 33{ 34 int i; 35 struct exfat_entry_set_cache *es; 36 unsigned int uni_len = 0, len; 37 38 es = exfat_get_dentry_set(sb, p_dir, entry, ES_ALL_ENTRIES); 39 if (!es) 40 return; 41 42 /* 43 * First entry : file entry 44 * Second entry : stream-extension entry 45 * Third entry : first file-name entry 46 * So, the index of first file-name dentry should start from 2. 47 */ 48 for (i = 2; i < es->num_entries; i++) { 49 struct exfat_dentry *ep = exfat_get_dentry_cached(es, i); 50 51 /* end of name entry */ 52 if (exfat_get_entry_type(ep) != TYPE_EXTEND) 53 break; 54 55 len = exfat_extract_uni_name(ep, uniname); 56 uni_len += len; 57 if (len != EXFAT_FILE_NAME_LEN || uni_len >= MAX_NAME_LENGTH) 58 break; 59 uniname += EXFAT_FILE_NAME_LEN; 60 } 61 62 exfat_free_dentry_set(es, false); 63} 64 65/* read a directory entry from the opened directory */ 66static int exfat_readdir(struct inode *inode, loff_t *cpos, struct exfat_dir_entry *dir_entry) 67{ 68 int i, dentries_per_clu, dentries_per_clu_bits = 0, num_ext; 69 unsigned int type, clu_offset, max_dentries; 70 sector_t sector; 71 struct exfat_chain dir, clu; 72 struct exfat_uni_name uni_name; 73 struct exfat_dentry *ep; 74 struct super_block *sb = inode->i_sb; 75 struct exfat_sb_info *sbi = EXFAT_SB(sb); 76 struct exfat_inode_info *ei = EXFAT_I(inode); 77 unsigned int dentry = EXFAT_B_TO_DEN(*cpos) & 0xFFFFFFFF; 78 struct buffer_head *bh; 79 80 /* check if the given file ID is opened */ 81 if (ei->type != TYPE_DIR) 82 return -EPERM; 83 84 if (ei->entry == -1) 85 exfat_chain_set(&dir, sbi->root_dir, 0, ALLOC_FAT_CHAIN); 86 else 87 exfat_chain_set(&dir, ei->start_clu, 88 EXFAT_B_TO_CLU(i_size_read(inode), sbi), ei->flags); 89 90 dentries_per_clu = sbi->dentries_per_clu; 91 dentries_per_clu_bits = ilog2(dentries_per_clu); 92 max_dentries = (unsigned int)min_t(u64, MAX_EXFAT_DENTRIES, 93 (u64)sbi->num_clusters << dentries_per_clu_bits); 94 95 clu_offset = dentry >> dentries_per_clu_bits; 96 exfat_chain_dup(&clu, &dir); 97 98 if (clu.flags == ALLOC_NO_FAT_CHAIN) { 99 clu.dir += clu_offset; 100 clu.size -= clu_offset; 101 } else { 102 /* hint_information */ 103 if (clu_offset > 0 && ei->hint_bmap.off != EXFAT_EOF_CLUSTER && 104 ei->hint_bmap.off > 0 && clu_offset >= ei->hint_bmap.off) { 105 clu_offset -= ei->hint_bmap.off; 106 clu.dir = ei->hint_bmap.clu; 107 } 108 109 while (clu_offset > 0 && clu.dir != EXFAT_EOF_CLUSTER) { 110 if (exfat_get_next_cluster(sb, &(clu.dir))) 111 return -EIO; 112 113 clu_offset--; 114 } 115 } 116 117 while (clu.dir != EXFAT_EOF_CLUSTER && dentry < max_dentries) { 118 i = dentry & (dentries_per_clu - 1); 119 120 for ( ; i < dentries_per_clu; i++, dentry++) { 121 ep = exfat_get_dentry(sb, &clu, i, &bh, §or); 122 if (!ep) 123 return -EIO; 124 125 type = exfat_get_entry_type(ep); 126 if (type == TYPE_UNUSED) { 127 brelse(bh); 128 break; 129 } 130 131 if (type != TYPE_FILE && type != TYPE_DIR) { 132 brelse(bh); 133 continue; 134 } 135 136 num_ext = ep->dentry.file.num_ext; 137 dir_entry->attr = le16_to_cpu(ep->dentry.file.attr); 138 exfat_get_entry_time(sbi, &dir_entry->crtime, 139 ep->dentry.file.create_tz, 140 ep->dentry.file.create_time, 141 ep->dentry.file.create_date, 142 ep->dentry.file.create_time_cs); 143 exfat_get_entry_time(sbi, &dir_entry->mtime, 144 ep->dentry.file.modify_tz, 145 ep->dentry.file.modify_time, 146 ep->dentry.file.modify_date, 147 ep->dentry.file.modify_time_cs); 148 exfat_get_entry_time(sbi, &dir_entry->atime, 149 ep->dentry.file.access_tz, 150 ep->dentry.file.access_time, 151 ep->dentry.file.access_date, 152 0); 153 154 *uni_name.name = 0x0; 155 exfat_get_uniname_from_ext_entry(sb, &clu, i, 156 uni_name.name); 157 exfat_utf16_to_nls(sb, &uni_name, 158 dir_entry->namebuf.lfn, 159 dir_entry->namebuf.lfnbuf_len); 160 brelse(bh); 161 162 ep = exfat_get_dentry(sb, &clu, i + 1, &bh, NULL); 163 if (!ep) 164 return -EIO; 165 dir_entry->size = 166 le64_to_cpu(ep->dentry.stream.valid_size); 167 dir_entry->entry = dentry; 168 brelse(bh); 169 170 ei->hint_bmap.off = dentry >> dentries_per_clu_bits; 171 ei->hint_bmap.clu = clu.dir; 172 173 *cpos = EXFAT_DEN_TO_B(dentry + 1 + num_ext); 174 return 0; 175 } 176 177 if (clu.flags == ALLOC_NO_FAT_CHAIN) { 178 if (--clu.size > 0) 179 clu.dir++; 180 else 181 clu.dir = EXFAT_EOF_CLUSTER; 182 } else { 183 if (exfat_get_next_cluster(sb, &(clu.dir))) 184 return -EIO; 185 } 186 } 187 188 dir_entry->namebuf.lfn[0] = '\0'; 189 *cpos = EXFAT_DEN_TO_B(dentry); 190 return 0; 191} 192 193static void exfat_init_namebuf(struct exfat_dentry_namebuf *nb) 194{ 195 nb->lfn = NULL; 196 nb->lfnbuf_len = 0; 197} 198 199static int exfat_alloc_namebuf(struct exfat_dentry_namebuf *nb) 200{ 201 nb->lfn = __getname(); 202 if (!nb->lfn) 203 return -ENOMEM; 204 nb->lfnbuf_len = MAX_VFSNAME_BUF_SIZE; 205 return 0; 206} 207 208static void exfat_free_namebuf(struct exfat_dentry_namebuf *nb) 209{ 210 if (!nb->lfn) 211 return; 212 213 __putname(nb->lfn); 214 exfat_init_namebuf(nb); 215} 216 217/* 218 * Before calling dir_emit*(), sbi->s_lock should be released 219 * because page fault can occur in dir_emit*(). 220 */ 221#define ITER_POS_FILLED_DOTS (2) 222static int exfat_iterate(struct file *filp, struct dir_context *ctx) 223{ 224 struct inode *inode = filp->f_path.dentry->d_inode; 225 struct super_block *sb = inode->i_sb; 226 struct inode *tmp; 227 struct exfat_dir_entry de; 228 struct exfat_dentry_namebuf *nb = &(de.namebuf); 229 struct exfat_inode_info *ei = EXFAT_I(inode); 230 unsigned long inum; 231 loff_t cpos, i_pos; 232 int err = 0, fake_offset = 0; 233 234 exfat_init_namebuf(nb); 235 236 cpos = ctx->pos; 237 if (!dir_emit_dots(filp, ctx)) 238 goto out; 239 240 if (ctx->pos == ITER_POS_FILLED_DOTS) { 241 cpos = 0; 242 fake_offset = 1; 243 } 244 245 cpos = round_up(cpos, DENTRY_SIZE); 246 247 /* name buffer should be allocated before use */ 248 err = exfat_alloc_namebuf(nb); 249 if (err) 250 goto out; 251get_new: 252 mutex_lock(&EXFAT_SB(sb)->s_lock); 253 254 if (ei->flags == ALLOC_NO_FAT_CHAIN && cpos >= i_size_read(inode)) 255 goto end_of_dir; 256 257 err = exfat_readdir(inode, &cpos, &de); 258 if (err) { 259 /* 260 * At least we tried to read a sector. 261 * Move cpos to next sector position (should be aligned). 262 */ 263 if (err == -EIO) { 264 cpos += 1 << (sb->s_blocksize_bits); 265 cpos &= ~(sb->s_blocksize - 1); 266 } 267 268 err = -EIO; 269 goto end_of_dir; 270 } 271 272 if (!nb->lfn[0]) 273 goto end_of_dir; 274 275 i_pos = ((loff_t)ei->start_clu << 32) | (de.entry & 0xffffffff); 276 tmp = exfat_iget(sb, i_pos); 277 if (tmp) { 278 inum = tmp->i_ino; 279 iput(tmp); 280 } else { 281 inum = iunique(sb, EXFAT_ROOT_INO); 282 } 283 284 mutex_unlock(&EXFAT_SB(sb)->s_lock); 285 if (!dir_emit(ctx, nb->lfn, strlen(nb->lfn), inum, 286 (de.attr & ATTR_SUBDIR) ? DT_DIR : DT_REG)) 287 goto out; 288 ctx->pos = cpos; 289 goto get_new; 290 291end_of_dir: 292 if (!cpos && fake_offset) 293 cpos = ITER_POS_FILLED_DOTS; 294 ctx->pos = cpos; 295 mutex_unlock(&EXFAT_SB(sb)->s_lock); 296out: 297 /* 298 * To improve performance, free namebuf after unlock sb_lock. 299 * If namebuf is not allocated, this function do nothing 300 */ 301 exfat_free_namebuf(nb); 302 return err; 303} 304 305const struct file_operations exfat_dir_operations = { 306 .llseek = generic_file_llseek, 307 .read = generic_read_dir, 308 .iterate = exfat_iterate, 309 .fsync = exfat_file_fsync, 310}; 311 312int exfat_alloc_new_dir(struct inode *inode, struct exfat_chain *clu) 313{ 314 int ret; 315 316 exfat_chain_set(clu, EXFAT_EOF_CLUSTER, 0, ALLOC_NO_FAT_CHAIN); 317 318 ret = exfat_alloc_cluster(inode, 1, clu); 319 if (ret) 320 return ret; 321 322 return exfat_zeroed_cluster(inode, clu->dir); 323} 324 325int exfat_calc_num_entries(struct exfat_uni_name *p_uniname) 326{ 327 int len; 328 329 len = p_uniname->name_len; 330 if (len == 0) 331 return -EINVAL; 332 333 /* 1 file entry + 1 stream entry + name entries */ 334 return ((len - 1) / EXFAT_FILE_NAME_LEN + 3); 335} 336 337unsigned int exfat_get_entry_type(struct exfat_dentry *ep) 338{ 339 if (ep->type == EXFAT_UNUSED) 340 return TYPE_UNUSED; 341 if (IS_EXFAT_DELETED(ep->type)) 342 return TYPE_DELETED; 343 if (ep->type == EXFAT_INVAL) 344 return TYPE_INVALID; 345 if (IS_EXFAT_CRITICAL_PRI(ep->type)) { 346 if (ep->type == EXFAT_BITMAP) 347 return TYPE_BITMAP; 348 if (ep->type == EXFAT_UPCASE) 349 return TYPE_UPCASE; 350 if (ep->type == EXFAT_VOLUME) 351 return TYPE_VOLUME; 352 if (ep->type == EXFAT_FILE) { 353 if (le16_to_cpu(ep->dentry.file.attr) & ATTR_SUBDIR) 354 return TYPE_DIR; 355 return TYPE_FILE; 356 } 357 return TYPE_CRITICAL_PRI; 358 } 359 if (IS_EXFAT_BENIGN_PRI(ep->type)) { 360 if (ep->type == EXFAT_GUID) 361 return TYPE_GUID; 362 if (ep->type == EXFAT_PADDING) 363 return TYPE_PADDING; 364 if (ep->type == EXFAT_ACLTAB) 365 return TYPE_ACLTAB; 366 return TYPE_BENIGN_PRI; 367 } 368 if (IS_EXFAT_CRITICAL_SEC(ep->type)) { 369 if (ep->type == EXFAT_STREAM) 370 return TYPE_STREAM; 371 if (ep->type == EXFAT_NAME) 372 return TYPE_EXTEND; 373 if (ep->type == EXFAT_ACL) 374 return TYPE_ACL; 375 return TYPE_CRITICAL_SEC; 376 } 377 return TYPE_BENIGN_SEC; 378} 379 380static void exfat_set_entry_type(struct exfat_dentry *ep, unsigned int type) 381{ 382 if (type == TYPE_UNUSED) { 383 ep->type = EXFAT_UNUSED; 384 } else if (type == TYPE_DELETED) { 385 ep->type &= EXFAT_DELETE; 386 } else if (type == TYPE_STREAM) { 387 ep->type = EXFAT_STREAM; 388 } else if (type == TYPE_EXTEND) { 389 ep->type = EXFAT_NAME; 390 } else if (type == TYPE_BITMAP) { 391 ep->type = EXFAT_BITMAP; 392 } else if (type == TYPE_UPCASE) { 393 ep->type = EXFAT_UPCASE; 394 } else if (type == TYPE_VOLUME) { 395 ep->type = EXFAT_VOLUME; 396 } else if (type == TYPE_DIR) { 397 ep->type = EXFAT_FILE; 398 ep->dentry.file.attr = cpu_to_le16(ATTR_SUBDIR); 399 } else if (type == TYPE_FILE) { 400 ep->type = EXFAT_FILE; 401 ep->dentry.file.attr = cpu_to_le16(ATTR_ARCHIVE); 402 } 403} 404 405static void exfat_init_stream_entry(struct exfat_dentry *ep, 406 unsigned char flags, unsigned int start_clu, 407 unsigned long long size) 408{ 409 exfat_set_entry_type(ep, TYPE_STREAM); 410 ep->dentry.stream.flags = flags; 411 ep->dentry.stream.start_clu = cpu_to_le32(start_clu); 412 ep->dentry.stream.valid_size = cpu_to_le64(size); 413 ep->dentry.stream.size = cpu_to_le64(size); 414} 415 416static void exfat_init_name_entry(struct exfat_dentry *ep, 417 unsigned short *uniname) 418{ 419 int i; 420 421 exfat_set_entry_type(ep, TYPE_EXTEND); 422 ep->dentry.name.flags = 0x0; 423 424 for (i = 0; i < EXFAT_FILE_NAME_LEN; i++) { 425 if (*uniname != 0x0) { 426 ep->dentry.name.unicode_0_14[i] = cpu_to_le16(*uniname); 427 uniname++; 428 } else { 429 ep->dentry.name.unicode_0_14[i] = 0x0; 430 } 431 } 432} 433 434int exfat_init_dir_entry(struct inode *inode, struct exfat_chain *p_dir, 435 int entry, unsigned int type, unsigned int start_clu, 436 unsigned long long size) 437{ 438 struct super_block *sb = inode->i_sb; 439 struct exfat_sb_info *sbi = EXFAT_SB(sb); 440 struct timespec64 ts = current_time(inode); 441 sector_t sector; 442 struct exfat_dentry *ep; 443 struct buffer_head *bh; 444 445 /* 446 * We cannot use exfat_get_dentry_set here because file ep is not 447 * initialized yet. 448 */ 449 ep = exfat_get_dentry(sb, p_dir, entry, &bh, §or); 450 if (!ep) 451 return -EIO; 452 453 exfat_set_entry_type(ep, type); 454 exfat_set_entry_time(sbi, &ts, 455 &ep->dentry.file.create_tz, 456 &ep->dentry.file.create_time, 457 &ep->dentry.file.create_date, 458 &ep->dentry.file.create_time_cs); 459 exfat_set_entry_time(sbi, &ts, 460 &ep->dentry.file.modify_tz, 461 &ep->dentry.file.modify_time, 462 &ep->dentry.file.modify_date, 463 &ep->dentry.file.modify_time_cs); 464 exfat_set_entry_time(sbi, &ts, 465 &ep->dentry.file.access_tz, 466 &ep->dentry.file.access_time, 467 &ep->dentry.file.access_date, 468 NULL); 469 470 exfat_update_bh(bh, IS_DIRSYNC(inode)); 471 brelse(bh); 472 473 ep = exfat_get_dentry(sb, p_dir, entry + 1, &bh, §or); 474 if (!ep) 475 return -EIO; 476 477 exfat_init_stream_entry(ep, 478 (type == TYPE_FILE) ? ALLOC_FAT_CHAIN : ALLOC_NO_FAT_CHAIN, 479 start_clu, size); 480 exfat_update_bh(bh, IS_DIRSYNC(inode)); 481 brelse(bh); 482 483 return 0; 484} 485 486int exfat_update_dir_chksum(struct inode *inode, struct exfat_chain *p_dir, 487 int entry) 488{ 489 struct super_block *sb = inode->i_sb; 490 int ret = 0; 491 int i, num_entries; 492 sector_t sector; 493 u16 chksum; 494 struct exfat_dentry *ep, *fep; 495 struct buffer_head *fbh, *bh; 496 497 fep = exfat_get_dentry(sb, p_dir, entry, &fbh, §or); 498 if (!fep) 499 return -EIO; 500 501 num_entries = fep->dentry.file.num_ext + 1; 502 chksum = exfat_calc_chksum16(fep, DENTRY_SIZE, 0, CS_DIR_ENTRY); 503 504 for (i = 1; i < num_entries; i++) { 505 ep = exfat_get_dentry(sb, p_dir, entry + i, &bh, NULL); 506 if (!ep) { 507 ret = -EIO; 508 goto release_fbh; 509 } 510 chksum = exfat_calc_chksum16(ep, DENTRY_SIZE, chksum, 511 CS_DEFAULT); 512 brelse(bh); 513 } 514 515 fep->dentry.file.checksum = cpu_to_le16(chksum); 516 exfat_update_bh(fbh, IS_DIRSYNC(inode)); 517release_fbh: 518 brelse(fbh); 519 return ret; 520} 521 522int exfat_init_ext_entry(struct inode *inode, struct exfat_chain *p_dir, 523 int entry, int num_entries, struct exfat_uni_name *p_uniname) 524{ 525 struct super_block *sb = inode->i_sb; 526 int i; 527 sector_t sector; 528 unsigned short *uniname = p_uniname->name; 529 struct exfat_dentry *ep; 530 struct buffer_head *bh; 531 int sync = IS_DIRSYNC(inode); 532 533 ep = exfat_get_dentry(sb, p_dir, entry, &bh, §or); 534 if (!ep) 535 return -EIO; 536 537 ep->dentry.file.num_ext = (unsigned char)(num_entries - 1); 538 exfat_update_bh(bh, sync); 539 brelse(bh); 540 541 ep = exfat_get_dentry(sb, p_dir, entry + 1, &bh, §or); 542 if (!ep) 543 return -EIO; 544 545 ep->dentry.stream.name_len = p_uniname->name_len; 546 ep->dentry.stream.name_hash = cpu_to_le16(p_uniname->name_hash); 547 exfat_update_bh(bh, sync); 548 brelse(bh); 549 550 for (i = EXFAT_FIRST_CLUSTER; i < num_entries; i++) { 551 ep = exfat_get_dentry(sb, p_dir, entry + i, &bh, §or); 552 if (!ep) 553 return -EIO; 554 555 exfat_init_name_entry(ep, uniname); 556 exfat_update_bh(bh, sync); 557 brelse(bh); 558 uniname += EXFAT_FILE_NAME_LEN; 559 } 560 561 exfat_update_dir_chksum(inode, p_dir, entry); 562 return 0; 563} 564 565int exfat_remove_entries(struct inode *inode, struct exfat_chain *p_dir, 566 int entry, int order, int num_entries) 567{ 568 struct super_block *sb = inode->i_sb; 569 int i; 570 sector_t sector; 571 struct exfat_dentry *ep; 572 struct buffer_head *bh; 573 574 for (i = order; i < num_entries; i++) { 575 ep = exfat_get_dentry(sb, p_dir, entry + i, &bh, §or); 576 if (!ep) 577 return -EIO; 578 579 exfat_set_entry_type(ep, TYPE_DELETED); 580 exfat_update_bh(bh, IS_DIRSYNC(inode)); 581 brelse(bh); 582 } 583 584 return 0; 585} 586 587void exfat_update_dir_chksum_with_entry_set(struct exfat_entry_set_cache *es) 588{ 589 int chksum_type = CS_DIR_ENTRY, i; 590 unsigned short chksum = 0; 591 struct exfat_dentry *ep; 592 593 for (i = 0; i < es->num_entries; i++) { 594 ep = exfat_get_dentry_cached(es, i); 595 chksum = exfat_calc_chksum16(ep, DENTRY_SIZE, chksum, 596 chksum_type); 597 chksum_type = CS_DEFAULT; 598 } 599 ep = exfat_get_dentry_cached(es, 0); 600 ep->dentry.file.checksum = cpu_to_le16(chksum); 601 es->modified = true; 602} 603 604int exfat_free_dentry_set(struct exfat_entry_set_cache *es, int sync) 605{ 606 int i, err = 0; 607 608 if (es->modified) 609 err = exfat_update_bhs(es->bh, es->num_bh, sync); 610 611 for (i = 0; i < es->num_bh; i++) 612 if (err) 613 bforget(es->bh[i]); 614 else 615 brelse(es->bh[i]); 616 617 if (IS_DYNAMIC_ES(es)) 618 kfree(es->bh); 619 620 kfree(es); 621 return err; 622} 623 624static int exfat_walk_fat_chain(struct super_block *sb, 625 struct exfat_chain *p_dir, unsigned int byte_offset, 626 unsigned int *clu) 627{ 628 struct exfat_sb_info *sbi = EXFAT_SB(sb); 629 unsigned int clu_offset; 630 unsigned int cur_clu; 631 632 clu_offset = EXFAT_B_TO_CLU(byte_offset, sbi); 633 cur_clu = p_dir->dir; 634 635 if (p_dir->flags == ALLOC_NO_FAT_CHAIN) { 636 cur_clu += clu_offset; 637 } else { 638 while (clu_offset > 0) { 639 if (exfat_get_next_cluster(sb, &cur_clu)) 640 return -EIO; 641 if (cur_clu == EXFAT_EOF_CLUSTER) { 642 exfat_fs_error(sb, 643 "invalid dentry access beyond EOF (clu : %u, eidx : %d)", 644 p_dir->dir, 645 EXFAT_B_TO_DEN(byte_offset)); 646 return -EIO; 647 } 648 clu_offset--; 649 } 650 } 651 652 *clu = cur_clu; 653 return 0; 654} 655 656int exfat_find_location(struct super_block *sb, struct exfat_chain *p_dir, 657 int entry, sector_t *sector, int *offset) 658{ 659 int ret; 660 unsigned int off, clu = 0; 661 struct exfat_sb_info *sbi = EXFAT_SB(sb); 662 663 off = EXFAT_DEN_TO_B(entry); 664 665 ret = exfat_walk_fat_chain(sb, p_dir, off, &clu); 666 if (ret) 667 return ret; 668 669 /* byte offset in cluster */ 670 off = EXFAT_CLU_OFFSET(off, sbi); 671 672 /* byte offset in sector */ 673 *offset = EXFAT_BLK_OFFSET(off, sb); 674 675 /* sector offset in cluster */ 676 *sector = EXFAT_B_TO_BLK(off, sb); 677 *sector += exfat_cluster_to_sector(sbi, clu); 678 return 0; 679} 680 681#define EXFAT_MAX_RA_SIZE (128*1024) 682static int exfat_dir_readahead(struct super_block *sb, sector_t sec) 683{ 684 struct exfat_sb_info *sbi = EXFAT_SB(sb); 685 struct buffer_head *bh; 686 unsigned int max_ra_count = EXFAT_MAX_RA_SIZE >> sb->s_blocksize_bits; 687 unsigned int page_ra_count = PAGE_SIZE >> sb->s_blocksize_bits; 688 unsigned int adj_ra_count = max(sbi->sect_per_clus, page_ra_count); 689 unsigned int ra_count = min(adj_ra_count, max_ra_count); 690 691 /* Read-ahead is not required */ 692 if (sbi->sect_per_clus == 1) 693 return 0; 694 695 if (sec < sbi->data_start_sector) { 696 exfat_err(sb, "requested sector is invalid(sect:%llu, root:%llu)", 697 (unsigned long long)sec, sbi->data_start_sector); 698 return -EIO; 699 } 700 701 /* Not sector aligned with ra_count, resize ra_count to page size */ 702 if ((sec - sbi->data_start_sector) & (ra_count - 1)) 703 ra_count = page_ra_count; 704 705 bh = sb_find_get_block(sb, sec); 706 if (!bh || !buffer_uptodate(bh)) { 707 unsigned int i; 708 709 for (i = 0; i < ra_count; i++) 710 sb_breadahead(sb, (sector_t)(sec + i)); 711 } 712 brelse(bh); 713 return 0; 714} 715 716struct exfat_dentry *exfat_get_dentry(struct super_block *sb, 717 struct exfat_chain *p_dir, int entry, struct buffer_head **bh, 718 sector_t *sector) 719{ 720 unsigned int dentries_per_page = EXFAT_B_TO_DEN(PAGE_SIZE); 721 int off; 722 sector_t sec; 723 724 if (p_dir->dir == DIR_DELETED) { 725 exfat_err(sb, "abnormal access to deleted dentry"); 726 return NULL; 727 } 728 729 if (exfat_find_location(sb, p_dir, entry, &sec, &off)) 730 return NULL; 731 732 if (p_dir->dir != EXFAT_FREE_CLUSTER && 733 !(entry & (dentries_per_page - 1))) 734 exfat_dir_readahead(sb, sec); 735 736 *bh = sb_bread(sb, sec); 737 if (!*bh) 738 return NULL; 739 740 if (sector) 741 *sector = sec; 742 return (struct exfat_dentry *)((*bh)->b_data + off); 743} 744 745enum exfat_validate_dentry_mode { 746 ES_MODE_STARTED, 747 ES_MODE_GET_FILE_ENTRY, 748 ES_MODE_GET_STRM_ENTRY, 749 ES_MODE_GET_NAME_ENTRY, 750 ES_MODE_GET_CRITICAL_SEC_ENTRY, 751}; 752 753static bool exfat_validate_entry(unsigned int type, 754 enum exfat_validate_dentry_mode *mode) 755{ 756 if (type == TYPE_UNUSED || type == TYPE_DELETED) 757 return false; 758 759 switch (*mode) { 760 case ES_MODE_STARTED: 761 if (type != TYPE_FILE && type != TYPE_DIR) 762 return false; 763 *mode = ES_MODE_GET_FILE_ENTRY; 764 return true; 765 case ES_MODE_GET_FILE_ENTRY: 766 if (type != TYPE_STREAM) 767 return false; 768 *mode = ES_MODE_GET_STRM_ENTRY; 769 return true; 770 case ES_MODE_GET_STRM_ENTRY: 771 if (type != TYPE_EXTEND) 772 return false; 773 *mode = ES_MODE_GET_NAME_ENTRY; 774 return true; 775 case ES_MODE_GET_NAME_ENTRY: 776 if (type == TYPE_STREAM) 777 return false; 778 if (type != TYPE_EXTEND) { 779 if (!(type & TYPE_CRITICAL_SEC)) 780 return false; 781 *mode = ES_MODE_GET_CRITICAL_SEC_ENTRY; 782 } 783 return true; 784 case ES_MODE_GET_CRITICAL_SEC_ENTRY: 785 if (type == TYPE_EXTEND || type == TYPE_STREAM) 786 return false; 787 if ((type & TYPE_CRITICAL_SEC) != TYPE_CRITICAL_SEC) 788 return false; 789 return true; 790 default: 791 WARN_ON_ONCE(1); 792 return false; 793 } 794} 795 796struct exfat_dentry *exfat_get_dentry_cached( 797 struct exfat_entry_set_cache *es, int num) 798{ 799 int off = es->start_off + num * DENTRY_SIZE; 800 struct buffer_head *bh = es->bh[EXFAT_B_TO_BLK(off, es->sb)]; 801 char *p = bh->b_data + EXFAT_BLK_OFFSET(off, es->sb); 802 803 return (struct exfat_dentry *)p; 804} 805 806/* 807 * Returns a set of dentries for a file or dir. 808 * 809 * Note It provides a direct pointer to bh->data via exfat_get_dentry_cached(). 810 * User should call exfat_get_dentry_set() after setting 'modified' to apply 811 * changes made in this entry set to the real device. 812 * 813 * in: 814 * sb+p_dir+entry: indicates a file/dir 815 * type: specifies how many dentries should be included. 816 * return: 817 * pointer of entry set on success, 818 * NULL on failure. 819 */ 820struct exfat_entry_set_cache *exfat_get_dentry_set(struct super_block *sb, 821 struct exfat_chain *p_dir, int entry, unsigned int type) 822{ 823 int ret, i, num_bh; 824 unsigned int off, byte_offset, clu = 0; 825 sector_t sec; 826 struct exfat_sb_info *sbi = EXFAT_SB(sb); 827 struct exfat_entry_set_cache *es; 828 struct exfat_dentry *ep; 829 int num_entries; 830 enum exfat_validate_dentry_mode mode = ES_MODE_STARTED; 831 struct buffer_head *bh; 832 833 if (p_dir->dir == DIR_DELETED) { 834 exfat_err(sb, "access to deleted dentry"); 835 return NULL; 836 } 837 838 byte_offset = EXFAT_DEN_TO_B(entry); 839 ret = exfat_walk_fat_chain(sb, p_dir, byte_offset, &clu); 840 if (ret) 841 return NULL; 842 843 es = kzalloc(sizeof(*es), GFP_KERNEL); 844 if (!es) 845 return NULL; 846 es->sb = sb; 847 es->modified = false; 848 849 /* byte offset in cluster */ 850 byte_offset = EXFAT_CLU_OFFSET(byte_offset, sbi); 851 852 /* byte offset in sector */ 853 off = EXFAT_BLK_OFFSET(byte_offset, sb); 854 es->start_off = off; 855 es->bh = es->__bh; 856 857 /* sector offset in cluster */ 858 sec = EXFAT_B_TO_BLK(byte_offset, sb); 859 sec += exfat_cluster_to_sector(sbi, clu); 860 861 bh = sb_bread(sb, sec); 862 if (!bh) 863 goto free_es; 864 es->bh[es->num_bh++] = bh; 865 866 ep = exfat_get_dentry_cached(es, 0); 867 if (!exfat_validate_entry(exfat_get_entry_type(ep), &mode)) 868 goto free_es; 869 870 num_entries = type == ES_ALL_ENTRIES ? 871 ep->dentry.file.num_ext + 1 : type; 872 es->num_entries = num_entries; 873 874 num_bh = EXFAT_B_TO_BLK_ROUND_UP(off + num_entries * DENTRY_SIZE, sb); 875 if (num_bh > ARRAY_SIZE(es->__bh)) { 876 es->bh = kmalloc_array(num_bh, sizeof(*es->bh), GFP_KERNEL); 877 if (!es->bh) { 878 brelse(bh); 879 kfree(es); 880 return NULL; 881 } 882 es->bh[0] = bh; 883 } 884 885 for (i = 1; i < num_bh; i++) { 886 /* get the next sector */ 887 if (exfat_is_last_sector_in_cluster(sbi, sec)) { 888 if (p_dir->flags == ALLOC_NO_FAT_CHAIN) 889 clu++; 890 else if (exfat_get_next_cluster(sb, &clu)) 891 goto free_es; 892 sec = exfat_cluster_to_sector(sbi, clu); 893 } else { 894 sec++; 895 } 896 897 bh = sb_bread(sb, sec); 898 if (!bh) 899 goto free_es; 900 es->bh[es->num_bh++] = bh; 901 } 902 903 /* validiate cached dentries */ 904 for (i = 1; i < num_entries; i++) { 905 ep = exfat_get_dentry_cached(es, i); 906 if (!exfat_validate_entry(exfat_get_entry_type(ep), &mode)) 907 goto free_es; 908 } 909 return es; 910 911free_es: 912 exfat_free_dentry_set(es, false); 913 return NULL; 914} 915 916enum { 917 DIRENT_STEP_FILE, 918 DIRENT_STEP_STRM, 919 DIRENT_STEP_NAME, 920 DIRENT_STEP_SECD, 921}; 922 923/* 924 * @ei: inode info of parent directory 925 * @p_dir: directory structure of parent directory 926 * @num_entries:entry size of p_uniname 927 * @hint_opt: If p_uniname is found, filled with optimized dir/entry 928 * for traversing cluster chain. 929 * @return: 930 * >= 0: file directory entry position where the name exists 931 * -ENOENT: entry with the name does not exist 932 * -EIO: I/O error 933 */ 934int exfat_find_dir_entry(struct super_block *sb, struct exfat_inode_info *ei, 935 struct exfat_chain *p_dir, struct exfat_uni_name *p_uniname, 936 int num_entries, unsigned int type, struct exfat_hint *hint_opt) 937{ 938 int i, rewind = 0, dentry = 0, end_eidx = 0, num_ext = 0, len; 939 int order, step, name_len = 0; 940 int dentries_per_clu, num_empty = 0; 941 unsigned int entry_type; 942 unsigned short *uniname = NULL; 943 struct exfat_chain clu; 944 struct exfat_hint *hint_stat = &ei->hint_stat; 945 struct exfat_hint_femp candi_empty; 946 struct exfat_sb_info *sbi = EXFAT_SB(sb); 947 948 dentries_per_clu = sbi->dentries_per_clu; 949 950 exfat_chain_dup(&clu, p_dir); 951 952 if (hint_stat->eidx) { 953 clu.dir = hint_stat->clu; 954 dentry = hint_stat->eidx; 955 end_eidx = dentry; 956 } 957 958 candi_empty.eidx = EXFAT_HINT_NONE; 959rewind: 960 order = 0; 961 step = DIRENT_STEP_FILE; 962 while (clu.dir != EXFAT_EOF_CLUSTER) { 963 i = dentry & (dentries_per_clu - 1); 964 for (; i < dentries_per_clu; i++, dentry++) { 965 struct exfat_dentry *ep; 966 struct buffer_head *bh; 967 968 if (rewind && dentry == end_eidx) 969 goto not_found; 970 971 ep = exfat_get_dentry(sb, &clu, i, &bh, NULL); 972 if (!ep) 973 return -EIO; 974 975 entry_type = exfat_get_entry_type(ep); 976 977 if (entry_type == TYPE_UNUSED || 978 entry_type == TYPE_DELETED) { 979 step = DIRENT_STEP_FILE; 980 981 num_empty++; 982 if (candi_empty.eidx == EXFAT_HINT_NONE && 983 num_empty == 1) { 984 exfat_chain_set(&candi_empty.cur, 985 clu.dir, clu.size, clu.flags); 986 } 987 988 if (candi_empty.eidx == EXFAT_HINT_NONE && 989 num_empty >= num_entries) { 990 candi_empty.eidx = 991 dentry - (num_empty - 1); 992 WARN_ON(candi_empty.eidx < 0); 993 candi_empty.count = num_empty; 994 995 if (ei->hint_femp.eidx == 996 EXFAT_HINT_NONE || 997 candi_empty.eidx <= 998 ei->hint_femp.eidx) 999 ei->hint_femp = candi_empty; 1000 } 1001 1002 brelse(bh); 1003 if (entry_type == TYPE_UNUSED) 1004 goto not_found; 1005 continue; 1006 } 1007 1008 num_empty = 0; 1009 candi_empty.eidx = EXFAT_HINT_NONE; 1010 1011 if (entry_type == TYPE_FILE || entry_type == TYPE_DIR) { 1012 step = DIRENT_STEP_FILE; 1013 hint_opt->clu = clu.dir; 1014 hint_opt->eidx = i; 1015 if (type == TYPE_ALL || type == entry_type) { 1016 num_ext = ep->dentry.file.num_ext; 1017 step = DIRENT_STEP_STRM; 1018 } 1019 brelse(bh); 1020 continue; 1021 } 1022 1023 if (entry_type == TYPE_STREAM) { 1024 u16 name_hash; 1025 1026 if (step != DIRENT_STEP_STRM) { 1027 step = DIRENT_STEP_FILE; 1028 brelse(bh); 1029 continue; 1030 } 1031 step = DIRENT_STEP_FILE; 1032 name_hash = le16_to_cpu( 1033 ep->dentry.stream.name_hash); 1034 if (p_uniname->name_hash == name_hash && 1035 p_uniname->name_len == 1036 ep->dentry.stream.name_len) { 1037 step = DIRENT_STEP_NAME; 1038 order = 1; 1039 name_len = 0; 1040 } 1041 brelse(bh); 1042 continue; 1043 } 1044 1045 brelse(bh); 1046 if (entry_type == TYPE_EXTEND) { 1047 unsigned short entry_uniname[16], unichar; 1048 1049 if (step != DIRENT_STEP_NAME || 1050 name_len >= MAX_NAME_LENGTH) { 1051 step = DIRENT_STEP_FILE; 1052 continue; 1053 } 1054 1055 if (++order == 2) 1056 uniname = p_uniname->name; 1057 else 1058 uniname += EXFAT_FILE_NAME_LEN; 1059 1060 len = exfat_extract_uni_name(ep, entry_uniname); 1061 name_len += len; 1062 1063 unichar = *(uniname+len); 1064 *(uniname+len) = 0x0; 1065 1066 if (exfat_uniname_ncmp(sb, uniname, 1067 entry_uniname, len)) { 1068 step = DIRENT_STEP_FILE; 1069 } else if (p_uniname->name_len == name_len) { 1070 if (order == num_ext) 1071 goto found; 1072 step = DIRENT_STEP_SECD; 1073 } 1074 1075 *(uniname+len) = unichar; 1076 continue; 1077 } 1078 1079 if (entry_type & 1080 (TYPE_CRITICAL_SEC | TYPE_BENIGN_SEC)) { 1081 if (step == DIRENT_STEP_SECD) { 1082 if (++order == num_ext) 1083 goto found; 1084 continue; 1085 } 1086 } 1087 step = DIRENT_STEP_FILE; 1088 } 1089 1090 if (clu.flags == ALLOC_NO_FAT_CHAIN) { 1091 if (--clu.size > 0) 1092 clu.dir++; 1093 else 1094 clu.dir = EXFAT_EOF_CLUSTER; 1095 } else { 1096 if (exfat_get_next_cluster(sb, &clu.dir)) 1097 return -EIO; 1098 } 1099 } 1100 1101not_found: 1102 /* 1103 * We started at not 0 index,so we should try to find target 1104 * from 0 index to the index we started at. 1105 */ 1106 if (!rewind && end_eidx) { 1107 rewind = 1; 1108 dentry = 0; 1109 clu.dir = p_dir->dir; 1110 /* reset empty hint */ 1111 num_empty = 0; 1112 candi_empty.eidx = EXFAT_HINT_NONE; 1113 goto rewind; 1114 } 1115 1116 /* initialized hint_stat */ 1117 hint_stat->clu = p_dir->dir; 1118 hint_stat->eidx = 0; 1119 return -ENOENT; 1120 1121found: 1122 /* next dentry we'll find is out of this cluster */ 1123 if (!((dentry + 1) & (dentries_per_clu - 1))) { 1124 int ret = 0; 1125 1126 if (clu.flags == ALLOC_NO_FAT_CHAIN) { 1127 if (--clu.size > 0) 1128 clu.dir++; 1129 else 1130 clu.dir = EXFAT_EOF_CLUSTER; 1131 } else { 1132 ret = exfat_get_next_cluster(sb, &clu.dir); 1133 } 1134 1135 if (ret || clu.dir == EXFAT_EOF_CLUSTER) { 1136 /* just initialized hint_stat */ 1137 hint_stat->clu = p_dir->dir; 1138 hint_stat->eidx = 0; 1139 return (dentry - num_ext); 1140 } 1141 } 1142 1143 hint_stat->clu = clu.dir; 1144 hint_stat->eidx = dentry + 1; 1145 return dentry - num_ext; 1146} 1147 1148int exfat_count_ext_entries(struct super_block *sb, struct exfat_chain *p_dir, 1149 int entry, struct exfat_dentry *ep) 1150{ 1151 int i, count = 0; 1152 unsigned int type; 1153 struct exfat_dentry *ext_ep; 1154 struct buffer_head *bh; 1155 1156 for (i = 0, entry++; i < ep->dentry.file.num_ext; i++, entry++) { 1157 ext_ep = exfat_get_dentry(sb, p_dir, entry, &bh, NULL); 1158 if (!ext_ep) 1159 return -EIO; 1160 1161 type = exfat_get_entry_type(ext_ep); 1162 brelse(bh); 1163 if (type == TYPE_EXTEND || type == TYPE_STREAM) 1164 count++; 1165 else 1166 break; 1167 } 1168 return count; 1169} 1170 1171int exfat_count_dir_entries(struct super_block *sb, struct exfat_chain *p_dir) 1172{ 1173 int i, count = 0; 1174 int dentries_per_clu; 1175 unsigned int entry_type; 1176 struct exfat_chain clu; 1177 struct exfat_dentry *ep; 1178 struct exfat_sb_info *sbi = EXFAT_SB(sb); 1179 struct buffer_head *bh; 1180 1181 dentries_per_clu = sbi->dentries_per_clu; 1182 1183 exfat_chain_dup(&clu, p_dir); 1184 1185 while (clu.dir != EXFAT_EOF_CLUSTER) { 1186 for (i = 0; i < dentries_per_clu; i++) { 1187 ep = exfat_get_dentry(sb, &clu, i, &bh, NULL); 1188 if (!ep) 1189 return -EIO; 1190 entry_type = exfat_get_entry_type(ep); 1191 brelse(bh); 1192 1193 if (entry_type == TYPE_UNUSED) 1194 return count; 1195 if (entry_type != TYPE_DIR) 1196 continue; 1197 count++; 1198 } 1199 1200 if (clu.flags == ALLOC_NO_FAT_CHAIN) { 1201 if (--clu.size > 0) 1202 clu.dir++; 1203 else 1204 clu.dir = EXFAT_EOF_CLUSTER; 1205 } else { 1206 if (exfat_get_next_cluster(sb, &(clu.dir))) 1207 return -EIO; 1208 } 1209 } 1210 1211 return count; 1212} 1213