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