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