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 goto out; 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 out: 174 dir_entry->namebuf.lfn[0] = '\0'; 175 *cpos = EXFAT_DEN_TO_B(dentry); 176 return 0; 177 } 178 179 static void exfat_init_namebuf(struct exfat_dentry_namebuf *nb) 180 { 181 nb->lfn = NULL; 182 nb->lfnbuf_len = 0; 183 } 184 185 static int exfat_alloc_namebuf(struct exfat_dentry_namebuf *nb) 186 { 187 nb->lfn = __getname(); 188 if (!nb->lfn) 189 return -ENOMEM; 190 nb->lfnbuf_len = MAX_VFSNAME_BUF_SIZE; 191 return 0; 192 } 193 194 static void exfat_free_namebuf(struct exfat_dentry_namebuf *nb) 195 { 196 if (!nb->lfn) 197 return; 198 199 __putname(nb->lfn); 200 exfat_init_namebuf(nb); 201 } 202 203 /* 204 * Before calling dir_emit*(), sbi->s_lock should be released 205 * because page fault can occur in dir_emit*(). 206 */ 207 #define ITER_POS_FILLED_DOTS (2) 208 static int exfat_iterate(struct file *file, struct dir_context *ctx) 209 { 210 struct inode *inode = file_inode(file); 211 struct super_block *sb = inode->i_sb; 212 struct inode *tmp; 213 struct exfat_dir_entry de; 214 struct exfat_dentry_namebuf *nb = &(de.namebuf); 215 struct exfat_inode_info *ei = EXFAT_I(inode); 216 unsigned long inum; 217 loff_t cpos, i_pos; 218 int err = 0, fake_offset = 0; 219 220 exfat_init_namebuf(nb); 221 222 cpos = ctx->pos; 223 if (!dir_emit_dots(file, ctx)) 224 goto out; 225 226 if (ctx->pos == ITER_POS_FILLED_DOTS) { 227 cpos = 0; 228 fake_offset = 1; 229 } 230 231 cpos = round_up(cpos, DENTRY_SIZE); 232 233 /* name buffer should be allocated before use */ 234 err = exfat_alloc_namebuf(nb); 235 if (err) 236 goto out; 237 get_new: 238 mutex_lock(&EXFAT_SB(sb)->s_lock); 239 240 if (ei->flags == ALLOC_NO_FAT_CHAIN && cpos >= i_size_read(inode)) 241 goto end_of_dir; 242 243 err = exfat_readdir(inode, &cpos, &de); 244 if (err) { 245 /* 246 * At least we tried to read a sector. 247 * Move cpos to next sector position (should be aligned). 248 */ 249 if (err == -EIO) { 250 cpos += 1 << (sb->s_blocksize_bits); 251 cpos &= ~(sb->s_blocksize - 1); 252 } 253 254 err = -EIO; 255 goto end_of_dir; 256 } 257 258 if (!nb->lfn[0]) 259 goto end_of_dir; 260 261 i_pos = ((loff_t)de.dir.dir << 32) | (de.entry & 0xffffffff); 262 tmp = exfat_iget(sb, i_pos); 263 if (tmp) { 264 inum = tmp->i_ino; 265 iput(tmp); 266 } else { 267 inum = iunique(sb, EXFAT_ROOT_INO); 268 } 269 270 mutex_unlock(&EXFAT_SB(sb)->s_lock); 271 if (!dir_emit(ctx, nb->lfn, strlen(nb->lfn), inum, 272 (de.attr & EXFAT_ATTR_SUBDIR) ? DT_DIR : DT_REG)) 273 goto out; 274 ctx->pos = cpos; 275 goto get_new; 276 277 end_of_dir: 278 if (!cpos && fake_offset) 279 cpos = ITER_POS_FILLED_DOTS; 280 ctx->pos = cpos; 281 mutex_unlock(&EXFAT_SB(sb)->s_lock); 282 out: 283 /* 284 * To improve performance, free namebuf after unlock sb_lock. 285 * If namebuf is not allocated, this function do nothing 286 */ 287 exfat_free_namebuf(nb); 288 return err; 289 } 290 291 WRAP_DIR_ITER(exfat_iterate) // FIXME! 292 const struct file_operations exfat_dir_operations = { 293 .llseek = generic_file_llseek, 294 .read = generic_read_dir, 295 .iterate_shared = shared_exfat_iterate, 296 .unlocked_ioctl = exfat_ioctl, 297 #ifdef CONFIG_COMPAT 298 .compat_ioctl = exfat_compat_ioctl, 299 #endif 300 .fsync = exfat_file_fsync, 301 }; 302 303 int exfat_alloc_new_dir(struct inode *inode, struct exfat_chain *clu) 304 { 305 int ret; 306 307 exfat_chain_set(clu, EXFAT_EOF_CLUSTER, 0, ALLOC_NO_FAT_CHAIN); 308 309 ret = exfat_alloc_cluster(inode, 1, clu, IS_DIRSYNC(inode)); 310 if (ret) 311 return ret; 312 313 return exfat_zeroed_cluster(inode, clu->dir); 314 } 315 316 int exfat_calc_num_entries(struct exfat_uni_name *p_uniname) 317 { 318 int len; 319 320 len = p_uniname->name_len; 321 if (len == 0) 322 return -EINVAL; 323 324 /* 1 file entry + 1 stream entry + name entries */ 325 return ES_ENTRY_NUM(len); 326 } 327 328 unsigned int exfat_get_entry_type(struct exfat_dentry *ep) 329 { 330 if (ep->type == EXFAT_UNUSED) 331 return TYPE_UNUSED; 332 if (IS_EXFAT_DELETED(ep->type)) 333 return TYPE_DELETED; 334 if (ep->type == EXFAT_INVAL) 335 return TYPE_INVALID; 336 if (IS_EXFAT_CRITICAL_PRI(ep->type)) { 337 if (ep->type == EXFAT_BITMAP) 338 return TYPE_BITMAP; 339 if (ep->type == EXFAT_UPCASE) 340 return TYPE_UPCASE; 341 if (ep->type == EXFAT_VOLUME) 342 return TYPE_VOLUME; 343 if (ep->type == EXFAT_FILE) { 344 if (le16_to_cpu(ep->dentry.file.attr) & EXFAT_ATTR_SUBDIR) 345 return TYPE_DIR; 346 return TYPE_FILE; 347 } 348 return TYPE_CRITICAL_PRI; 349 } 350 if (IS_EXFAT_BENIGN_PRI(ep->type)) { 351 if (ep->type == EXFAT_GUID) 352 return TYPE_GUID; 353 if (ep->type == EXFAT_PADDING) 354 return TYPE_PADDING; 355 if (ep->type == EXFAT_ACLTAB) 356 return TYPE_ACLTAB; 357 return TYPE_BENIGN_PRI; 358 } 359 if (IS_EXFAT_CRITICAL_SEC(ep->type)) { 360 if (ep->type == EXFAT_STREAM) 361 return TYPE_STREAM; 362 if (ep->type == EXFAT_NAME) 363 return TYPE_EXTEND; 364 if (ep->type == EXFAT_ACL) 365 return TYPE_ACL; 366 return TYPE_CRITICAL_SEC; 367 } 368 369 if (ep->type == EXFAT_VENDOR_EXT) 370 return TYPE_VENDOR_EXT; 371 if (ep->type == EXFAT_VENDOR_ALLOC) 372 return TYPE_VENDOR_ALLOC; 373 374 return TYPE_BENIGN_SEC; 375 } 376 377 static void exfat_set_entry_type(struct exfat_dentry *ep, unsigned int type) 378 { 379 if (type == TYPE_UNUSED) { 380 ep->type = EXFAT_UNUSED; 381 } else if (type == TYPE_DELETED) { 382 ep->type &= EXFAT_DELETE; 383 } else if (type == TYPE_STREAM) { 384 ep->type = EXFAT_STREAM; 385 } else if (type == TYPE_EXTEND) { 386 ep->type = EXFAT_NAME; 387 } else if (type == TYPE_BITMAP) { 388 ep->type = EXFAT_BITMAP; 389 } else if (type == TYPE_UPCASE) { 390 ep->type = EXFAT_UPCASE; 391 } else if (type == TYPE_VOLUME) { 392 ep->type = EXFAT_VOLUME; 393 } else if (type == TYPE_DIR) { 394 ep->type = EXFAT_FILE; 395 ep->dentry.file.attr = cpu_to_le16(EXFAT_ATTR_SUBDIR); 396 } else if (type == TYPE_FILE) { 397 ep->type = EXFAT_FILE; 398 ep->dentry.file.attr = cpu_to_le16(EXFAT_ATTR_ARCHIVE); 399 } 400 } 401 402 static void exfat_init_stream_entry(struct exfat_dentry *ep, 403 unsigned int start_clu, unsigned long long size) 404 { 405 memset(ep, 0, sizeof(*ep)); 406 exfat_set_entry_type(ep, TYPE_STREAM); 407 if (size == 0) 408 ep->dentry.stream.flags = ALLOC_FAT_CHAIN; 409 else 410 ep->dentry.stream.flags = ALLOC_NO_FAT_CHAIN; 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 416 static 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 434 void exfat_init_dir_entry(struct exfat_entry_set_cache *es, 435 unsigned int type, unsigned int start_clu, 436 unsigned long long size, struct timespec64 *ts) 437 { 438 struct super_block *sb = es->sb; 439 struct exfat_sb_info *sbi = EXFAT_SB(sb); 440 struct exfat_dentry *ep; 441 442 ep = exfat_get_dentry_cached(es, ES_IDX_FILE); 443 memset(ep, 0, sizeof(*ep)); 444 exfat_set_entry_type(ep, type); 445 exfat_set_entry_time(sbi, ts, 446 &ep->dentry.file.create_tz, 447 &ep->dentry.file.create_time, 448 &ep->dentry.file.create_date, 449 &ep->dentry.file.create_time_cs); 450 exfat_set_entry_time(sbi, ts, 451 &ep->dentry.file.modify_tz, 452 &ep->dentry.file.modify_time, 453 &ep->dentry.file.modify_date, 454 &ep->dentry.file.modify_time_cs); 455 exfat_set_entry_time(sbi, ts, 456 &ep->dentry.file.access_tz, 457 &ep->dentry.file.access_time, 458 &ep->dentry.file.access_date, 459 NULL); 460 461 ep = exfat_get_dentry_cached(es, ES_IDX_STREAM); 462 exfat_init_stream_entry(ep, start_clu, size); 463 } 464 465 static void exfat_free_benign_secondary_clusters(struct inode *inode, 466 struct exfat_dentry *ep) 467 { 468 struct super_block *sb = inode->i_sb; 469 struct exfat_chain dir; 470 unsigned int start_clu = 471 le32_to_cpu(ep->dentry.generic_secondary.start_clu); 472 u64 size = le64_to_cpu(ep->dentry.generic_secondary.size); 473 unsigned char flags = ep->dentry.generic_secondary.flags; 474 475 if (!(flags & ALLOC_POSSIBLE) || !start_clu || !size) 476 return; 477 478 exfat_chain_set(&dir, start_clu, 479 EXFAT_B_TO_CLU_ROUND_UP(size, EXFAT_SB(sb)), 480 flags); 481 exfat_free_cluster(inode, &dir); 482 } 483 484 void exfat_init_ext_entry(struct exfat_entry_set_cache *es, int num_entries, 485 struct exfat_uni_name *p_uniname) 486 { 487 int i; 488 unsigned short *uniname = p_uniname->name; 489 struct exfat_dentry *ep; 490 491 ep = exfat_get_dentry_cached(es, ES_IDX_FILE); 492 ep->dentry.file.num_ext = (unsigned char)(num_entries - 1); 493 494 ep = exfat_get_dentry_cached(es, ES_IDX_STREAM); 495 ep->dentry.stream.name_len = p_uniname->name_len; 496 ep->dentry.stream.name_hash = cpu_to_le16(p_uniname->name_hash); 497 498 for (i = ES_IDX_FIRST_FILENAME; i < num_entries; i++) { 499 ep = exfat_get_dentry_cached(es, i); 500 exfat_init_name_entry(ep, uniname); 501 uniname += EXFAT_FILE_NAME_LEN; 502 } 503 504 exfat_update_dir_chksum(es); 505 } 506 507 void exfat_remove_entries(struct inode *inode, struct exfat_entry_set_cache *es, 508 int order) 509 { 510 int i; 511 struct exfat_dentry *ep; 512 513 for (i = order; i < es->num_entries; i++) { 514 ep = exfat_get_dentry_cached(es, i); 515 516 if (exfat_get_entry_type(ep) & TYPE_BENIGN_SEC) 517 exfat_free_benign_secondary_clusters(inode, ep); 518 519 exfat_set_entry_type(ep, TYPE_DELETED); 520 } 521 522 if (order < es->num_entries) 523 es->modified = true; 524 } 525 526 void exfat_update_dir_chksum(struct exfat_entry_set_cache *es) 527 { 528 int chksum_type = CS_DIR_ENTRY, i; 529 unsigned short chksum = 0; 530 struct exfat_dentry *ep; 531 532 for (i = ES_IDX_FILE; i < es->num_entries; i++) { 533 ep = exfat_get_dentry_cached(es, i); 534 chksum = exfat_calc_chksum16(ep, DENTRY_SIZE, chksum, 535 chksum_type); 536 chksum_type = CS_DEFAULT; 537 } 538 ep = exfat_get_dentry_cached(es, ES_IDX_FILE); 539 ep->dentry.file.checksum = cpu_to_le16(chksum); 540 es->modified = true; 541 } 542 543 int exfat_put_dentry_set(struct exfat_entry_set_cache *es, int sync) 544 { 545 int i, err = 0; 546 547 if (es->modified) 548 err = exfat_update_bhs(es->bh, es->num_bh, sync); 549 550 for (i = 0; i < es->num_bh; i++) 551 if (err) 552 bforget(es->bh[i]); 553 else 554 brelse(es->bh[i]); 555 556 if (IS_DYNAMIC_ES(es)) 557 kfree(es->bh); 558 559 return err; 560 } 561 562 static int exfat_walk_fat_chain(struct super_block *sb, 563 struct exfat_chain *p_dir, unsigned int byte_offset, 564 unsigned int *clu) 565 { 566 struct exfat_sb_info *sbi = EXFAT_SB(sb); 567 unsigned int clu_offset; 568 unsigned int cur_clu; 569 570 clu_offset = EXFAT_B_TO_CLU(byte_offset, sbi); 571 cur_clu = p_dir->dir; 572 573 if (p_dir->flags == ALLOC_NO_FAT_CHAIN) { 574 cur_clu += clu_offset; 575 } else { 576 while (clu_offset > 0) { 577 if (exfat_get_next_cluster(sb, &cur_clu)) 578 return -EIO; 579 if (cur_clu == EXFAT_EOF_CLUSTER) { 580 exfat_fs_error(sb, 581 "invalid dentry access beyond EOF (clu : %u, eidx : %d)", 582 p_dir->dir, 583 EXFAT_B_TO_DEN(byte_offset)); 584 return -EIO; 585 } 586 clu_offset--; 587 } 588 } 589 590 *clu = cur_clu; 591 return 0; 592 } 593 594 static int exfat_find_location(struct super_block *sb, struct exfat_chain *p_dir, 595 int entry, sector_t *sector, int *offset) 596 { 597 int ret; 598 unsigned int off, clu = 0; 599 struct exfat_sb_info *sbi = EXFAT_SB(sb); 600 601 off = EXFAT_DEN_TO_B(entry); 602 603 ret = exfat_walk_fat_chain(sb, p_dir, off, &clu); 604 if (ret) 605 return ret; 606 607 if (!exfat_test_bitmap(sb, clu)) { 608 exfat_err(sb, "failed to test cluster bit(%u)", clu); 609 return -EIO; 610 } 611 612 /* byte offset in cluster */ 613 off = EXFAT_CLU_OFFSET(off, sbi); 614 615 /* byte offset in sector */ 616 *offset = EXFAT_BLK_OFFSET(off, sb); 617 618 /* sector offset in cluster */ 619 *sector = EXFAT_B_TO_BLK(off, sb); 620 *sector += exfat_cluster_to_sector(sbi, clu); 621 return 0; 622 } 623 624 #define EXFAT_MAX_RA_SIZE (128*1024) 625 static int exfat_dir_readahead(struct super_block *sb, sector_t sec) 626 { 627 struct exfat_sb_info *sbi = EXFAT_SB(sb); 628 struct buffer_head *bh; 629 unsigned int max_ra_count = EXFAT_MAX_RA_SIZE >> sb->s_blocksize_bits; 630 unsigned int page_ra_count = PAGE_SIZE >> sb->s_blocksize_bits; 631 unsigned int adj_ra_count = max(sbi->sect_per_clus, page_ra_count); 632 unsigned int ra_count = min(adj_ra_count, max_ra_count); 633 634 /* Read-ahead is not required */ 635 if (sbi->sect_per_clus == 1) 636 return 0; 637 638 if (sec < sbi->data_start_sector) { 639 exfat_err(sb, "requested sector is invalid(sect:%llu, root:%llu)", 640 (unsigned long long)sec, sbi->data_start_sector); 641 return -EIO; 642 } 643 644 /* Not sector aligned with ra_count, resize ra_count to page size */ 645 if ((sec - sbi->data_start_sector) & (ra_count - 1)) 646 ra_count = page_ra_count; 647 648 bh = sb_find_get_block(sb, sec); 649 if (!bh || !buffer_uptodate(bh)) { 650 unsigned int i; 651 652 for (i = 0; i < ra_count; i++) 653 sb_breadahead(sb, (sector_t)(sec + i)); 654 } 655 brelse(bh); 656 return 0; 657 } 658 659 struct exfat_dentry *exfat_get_dentry(struct super_block *sb, 660 struct exfat_chain *p_dir, int entry, struct buffer_head **bh) 661 { 662 unsigned int dentries_per_page = EXFAT_B_TO_DEN(PAGE_SIZE); 663 int off; 664 sector_t sec; 665 666 if (p_dir->dir == DIR_DELETED) { 667 exfat_err(sb, "abnormal access to deleted dentry"); 668 return NULL; 669 } 670 671 if (exfat_find_location(sb, p_dir, entry, &sec, &off)) 672 return NULL; 673 674 if (p_dir->dir != EXFAT_FREE_CLUSTER && 675 !(entry & (dentries_per_page - 1))) 676 exfat_dir_readahead(sb, sec); 677 678 *bh = sb_bread(sb, sec); 679 if (!*bh) 680 return NULL; 681 682 return (struct exfat_dentry *)((*bh)->b_data + off); 683 } 684 685 enum exfat_validate_dentry_mode { 686 ES_MODE_GET_FILE_ENTRY, 687 ES_MODE_GET_STRM_ENTRY, 688 ES_MODE_GET_NAME_ENTRY, 689 ES_MODE_GET_CRITICAL_SEC_ENTRY, 690 ES_MODE_GET_BENIGN_SEC_ENTRY, 691 }; 692 693 static bool exfat_validate_entry(unsigned int type, 694 enum exfat_validate_dentry_mode *mode) 695 { 696 if (type == TYPE_UNUSED || type == TYPE_DELETED) 697 return false; 698 699 switch (*mode) { 700 case ES_MODE_GET_FILE_ENTRY: 701 if (type != TYPE_STREAM) 702 return false; 703 *mode = ES_MODE_GET_STRM_ENTRY; 704 break; 705 case ES_MODE_GET_STRM_ENTRY: 706 if (type != TYPE_EXTEND) 707 return false; 708 *mode = ES_MODE_GET_NAME_ENTRY; 709 break; 710 case ES_MODE_GET_NAME_ENTRY: 711 if (type & TYPE_BENIGN_SEC) 712 *mode = ES_MODE_GET_BENIGN_SEC_ENTRY; 713 else if (type != TYPE_EXTEND) 714 return false; 715 break; 716 case ES_MODE_GET_BENIGN_SEC_ENTRY: 717 /* Assume unreconized benign secondary entry */ 718 if (!(type & TYPE_BENIGN_SEC)) 719 return false; 720 break; 721 default: 722 return false; 723 } 724 725 return true; 726 } 727 728 struct exfat_dentry *exfat_get_dentry_cached( 729 struct exfat_entry_set_cache *es, int num) 730 { 731 int off = es->start_off + num * DENTRY_SIZE; 732 struct buffer_head *bh = es->bh[EXFAT_B_TO_BLK(off, es->sb)]; 733 char *p = bh->b_data + EXFAT_BLK_OFFSET(off, es->sb); 734 735 return (struct exfat_dentry *)p; 736 } 737 738 /* 739 * Returns a set of dentries. 740 * 741 * Note It provides a direct pointer to bh->data via exfat_get_dentry_cached(). 742 * User should call exfat_get_dentry_set() after setting 'modified' to apply 743 * changes made in this entry set to the real device. 744 * 745 * in: 746 * sb+p_dir+entry: indicates a file/dir 747 * num_entries: specifies how many dentries should be included. 748 * It will be set to es->num_entries if it is not 0. 749 * If num_entries is 0, es->num_entries will be obtained 750 * from the first dentry. 751 * out: 752 * es: pointer of entry set on success. 753 * return: 754 * 0 on success 755 * -error code on failure 756 */ 757 static int __exfat_get_dentry_set(struct exfat_entry_set_cache *es, 758 struct super_block *sb, struct exfat_chain *p_dir, int entry, 759 unsigned int num_entries) 760 { 761 int ret, i, num_bh; 762 unsigned int off; 763 sector_t sec; 764 struct exfat_sb_info *sbi = EXFAT_SB(sb); 765 struct buffer_head *bh; 766 767 if (p_dir->dir == DIR_DELETED) { 768 exfat_err(sb, "access to deleted dentry"); 769 return -EIO; 770 } 771 772 ret = exfat_find_location(sb, p_dir, entry, &sec, &off); 773 if (ret) 774 return ret; 775 776 memset(es, 0, sizeof(*es)); 777 es->sb = sb; 778 es->modified = false; 779 es->start_off = off; 780 es->bh = es->__bh; 781 782 bh = sb_bread(sb, sec); 783 if (!bh) 784 return -EIO; 785 es->bh[es->num_bh++] = bh; 786 787 if (num_entries == ES_ALL_ENTRIES) { 788 struct exfat_dentry *ep; 789 790 ep = exfat_get_dentry_cached(es, ES_IDX_FILE); 791 if (ep->type != EXFAT_FILE) { 792 brelse(bh); 793 return -EIO; 794 } 795 796 num_entries = ep->dentry.file.num_ext + 1; 797 } 798 799 es->num_entries = num_entries; 800 801 num_bh = EXFAT_B_TO_BLK_ROUND_UP(off + num_entries * DENTRY_SIZE, sb); 802 if (num_bh > ARRAY_SIZE(es->__bh)) { 803 es->bh = kmalloc_array(num_bh, sizeof(*es->bh), GFP_NOFS); 804 if (!es->bh) { 805 brelse(bh); 806 return -ENOMEM; 807 } 808 es->bh[0] = bh; 809 } 810 811 for (i = 1; i < num_bh; i++) { 812 /* get the next sector */ 813 if (exfat_is_last_sector_in_cluster(sbi, sec)) { 814 unsigned int clu = exfat_sector_to_cluster(sbi, sec); 815 816 if (p_dir->flags == ALLOC_NO_FAT_CHAIN) 817 clu++; 818 else if (exfat_get_next_cluster(sb, &clu)) 819 goto put_es; 820 sec = exfat_cluster_to_sector(sbi, clu); 821 } else { 822 sec++; 823 } 824 825 bh = sb_bread(sb, sec); 826 if (!bh) 827 goto put_es; 828 es->bh[es->num_bh++] = bh; 829 } 830 831 return 0; 832 833 put_es: 834 exfat_put_dentry_set(es, false); 835 return -EIO; 836 } 837 838 int exfat_get_dentry_set(struct exfat_entry_set_cache *es, 839 struct super_block *sb, struct exfat_chain *p_dir, 840 int entry, unsigned int num_entries) 841 { 842 int ret, i; 843 struct exfat_dentry *ep; 844 enum exfat_validate_dentry_mode mode = ES_MODE_GET_FILE_ENTRY; 845 846 ret = __exfat_get_dentry_set(es, sb, p_dir, entry, num_entries); 847 if (ret < 0) 848 return ret; 849 850 /* validate cached dentries */ 851 for (i = ES_IDX_STREAM; i < es->num_entries; i++) { 852 ep = exfat_get_dentry_cached(es, i); 853 if (!exfat_validate_entry(exfat_get_entry_type(ep), &mode)) 854 goto put_es; 855 } 856 return 0; 857 858 put_es: 859 exfat_put_dentry_set(es, false); 860 return -EIO; 861 } 862 863 static int exfat_validate_empty_dentry_set(struct exfat_entry_set_cache *es) 864 { 865 struct exfat_dentry *ep; 866 struct buffer_head *bh; 867 int i, off; 868 bool unused_hit = false; 869 870 /* 871 * ONLY UNUSED OR DELETED DENTRIES ARE ALLOWED: 872 * Although it violates the specification for a deleted entry to 873 * follow an unused entry, some exFAT implementations could work 874 * like this. Therefore, to improve compatibility, let's allow it. 875 */ 876 for (i = 0; i < es->num_entries; i++) { 877 ep = exfat_get_dentry_cached(es, i); 878 if (ep->type == EXFAT_UNUSED) { 879 unused_hit = true; 880 } else if (!IS_EXFAT_DELETED(ep->type)) { 881 if (unused_hit) 882 goto err_used_follow_unused; 883 i++; 884 goto count_skip_entries; 885 } 886 } 887 888 return 0; 889 890 err_used_follow_unused: 891 off = es->start_off + (i << DENTRY_SIZE_BITS); 892 bh = es->bh[EXFAT_B_TO_BLK(off, es->sb)]; 893 894 exfat_fs_error(es->sb, 895 "in sector %lld, dentry %d should be unused, but 0x%x", 896 bh->b_blocknr, off >> DENTRY_SIZE_BITS, ep->type); 897 898 return -EIO; 899 900 count_skip_entries: 901 es->num_entries = EXFAT_B_TO_DEN(EXFAT_BLK_TO_B(es->num_bh, es->sb) - es->start_off); 902 for (; i < es->num_entries; i++) { 903 ep = exfat_get_dentry_cached(es, i); 904 if (IS_EXFAT_DELETED(ep->type)) 905 break; 906 } 907 908 return i; 909 } 910 911 /* 912 * Get an empty dentry set. 913 * 914 * in: 915 * sb+p_dir+entry: indicates the empty dentry location 916 * num_entries: specifies how many empty dentries should be included. 917 * out: 918 * es: pointer of empty dentry set on success. 919 * return: 920 * 0 : on success 921 * >0 : the dentries are not empty, the return value is the number of 922 * dentries to be skipped for the next lookup. 923 * <0 : on failure 924 */ 925 int exfat_get_empty_dentry_set(struct exfat_entry_set_cache *es, 926 struct super_block *sb, struct exfat_chain *p_dir, 927 int entry, unsigned int num_entries) 928 { 929 int ret; 930 931 ret = __exfat_get_dentry_set(es, sb, p_dir, entry, num_entries); 932 if (ret < 0) 933 return ret; 934 935 ret = exfat_validate_empty_dentry_set(es); 936 if (ret) 937 exfat_put_dentry_set(es, false); 938 939 return ret; 940 } 941 942 static inline void exfat_reset_empty_hint(struct exfat_hint_femp *hint_femp) 943 { 944 hint_femp->eidx = EXFAT_HINT_NONE; 945 hint_femp->count = 0; 946 } 947 948 static inline void exfat_set_empty_hint(struct exfat_inode_info *ei, 949 struct exfat_hint_femp *candi_empty, struct exfat_chain *clu, 950 int dentry, int num_entries, int entry_type) 951 { 952 if (ei->hint_femp.eidx == EXFAT_HINT_NONE || 953 ei->hint_femp.eidx > dentry) { 954 int total_entries = EXFAT_B_TO_DEN(i_size_read(&ei->vfs_inode)); 955 956 if (candi_empty->count == 0) { 957 candi_empty->cur = *clu; 958 candi_empty->eidx = dentry; 959 } 960 961 if (entry_type == TYPE_UNUSED) 962 candi_empty->count += total_entries - dentry; 963 else 964 candi_empty->count++; 965 966 if (candi_empty->count == num_entries || 967 candi_empty->count + candi_empty->eidx == total_entries) 968 ei->hint_femp = *candi_empty; 969 } 970 } 971 972 enum { 973 DIRENT_STEP_FILE, 974 DIRENT_STEP_STRM, 975 DIRENT_STEP_NAME, 976 DIRENT_STEP_SECD, 977 }; 978 979 /* 980 * @ei: inode info of parent directory 981 * @p_dir: directory structure of parent directory 982 * @num_entries:entry size of p_uniname 983 * @hint_opt: If p_uniname is found, filled with optimized dir/entry 984 * for traversing cluster chain. 985 * @return: 986 * >= 0: file directory entry position where the name exists 987 * -ENOENT: entry with the name does not exist 988 * -EIO: I/O error 989 */ 990 int exfat_find_dir_entry(struct super_block *sb, struct exfat_inode_info *ei, 991 struct exfat_chain *p_dir, struct exfat_uni_name *p_uniname, 992 struct exfat_hint *hint_opt) 993 { 994 int i, rewind = 0, dentry = 0, end_eidx = 0, num_ext = 0, len; 995 int order, step, name_len = 0; 996 int dentries_per_clu; 997 unsigned int entry_type; 998 unsigned short *uniname = NULL; 999 struct exfat_chain clu; 1000 struct exfat_hint *hint_stat = &ei->hint_stat; 1001 struct exfat_hint_femp candi_empty; 1002 struct exfat_sb_info *sbi = EXFAT_SB(sb); 1003 int num_entries = exfat_calc_num_entries(p_uniname); 1004 unsigned int clu_count = 0; 1005 1006 if (num_entries < 0) 1007 return num_entries; 1008 1009 dentries_per_clu = sbi->dentries_per_clu; 1010 1011 exfat_chain_dup(&clu, p_dir); 1012 1013 if (hint_stat->eidx) { 1014 clu.dir = hint_stat->clu; 1015 dentry = hint_stat->eidx; 1016 end_eidx = dentry; 1017 } 1018 1019 exfat_reset_empty_hint(&ei->hint_femp); 1020 1021 rewind: 1022 order = 0; 1023 step = DIRENT_STEP_FILE; 1024 exfat_reset_empty_hint(&candi_empty); 1025 1026 while (clu.dir != EXFAT_EOF_CLUSTER) { 1027 i = dentry & (dentries_per_clu - 1); 1028 for (; i < dentries_per_clu; i++, dentry++) { 1029 struct exfat_dentry *ep; 1030 struct buffer_head *bh; 1031 1032 if (rewind && dentry == end_eidx) 1033 goto not_found; 1034 1035 ep = exfat_get_dentry(sb, &clu, i, &bh); 1036 if (!ep) 1037 return -EIO; 1038 1039 entry_type = exfat_get_entry_type(ep); 1040 1041 if (entry_type == TYPE_UNUSED || 1042 entry_type == TYPE_DELETED) { 1043 step = DIRENT_STEP_FILE; 1044 1045 exfat_set_empty_hint(ei, &candi_empty, &clu, 1046 dentry, num_entries, 1047 entry_type); 1048 1049 brelse(bh); 1050 if (entry_type == TYPE_UNUSED) 1051 goto not_found; 1052 continue; 1053 } 1054 1055 exfat_reset_empty_hint(&candi_empty); 1056 1057 if (entry_type == TYPE_FILE || entry_type == TYPE_DIR) { 1058 step = DIRENT_STEP_FILE; 1059 hint_opt->clu = clu.dir; 1060 hint_opt->eidx = i; 1061 num_ext = ep->dentry.file.num_ext; 1062 step = DIRENT_STEP_STRM; 1063 brelse(bh); 1064 continue; 1065 } 1066 1067 if (entry_type == TYPE_STREAM) { 1068 u16 name_hash; 1069 1070 if (step != DIRENT_STEP_STRM) { 1071 step = DIRENT_STEP_FILE; 1072 brelse(bh); 1073 continue; 1074 } 1075 step = DIRENT_STEP_FILE; 1076 name_hash = le16_to_cpu( 1077 ep->dentry.stream.name_hash); 1078 if (p_uniname->name_hash == name_hash && 1079 p_uniname->name_len == 1080 ep->dentry.stream.name_len) { 1081 step = DIRENT_STEP_NAME; 1082 order = 1; 1083 name_len = 0; 1084 } 1085 brelse(bh); 1086 continue; 1087 } 1088 1089 brelse(bh); 1090 if (entry_type == TYPE_EXTEND) { 1091 unsigned short entry_uniname[16], unichar; 1092 1093 if (step != DIRENT_STEP_NAME || 1094 name_len >= MAX_NAME_LENGTH) { 1095 step = DIRENT_STEP_FILE; 1096 continue; 1097 } 1098 1099 if (++order == 2) 1100 uniname = p_uniname->name; 1101 else 1102 uniname += EXFAT_FILE_NAME_LEN; 1103 1104 len = exfat_extract_uni_name(ep, entry_uniname); 1105 name_len += len; 1106 1107 unichar = *(uniname+len); 1108 *(uniname+len) = 0x0; 1109 1110 if (exfat_uniname_ncmp(sb, uniname, 1111 entry_uniname, len)) { 1112 step = DIRENT_STEP_FILE; 1113 } else if (p_uniname->name_len == name_len) { 1114 if (order == num_ext) 1115 goto found; 1116 step = DIRENT_STEP_SECD; 1117 } 1118 1119 *(uniname+len) = unichar; 1120 continue; 1121 } 1122 1123 if (entry_type & 1124 (TYPE_CRITICAL_SEC | TYPE_BENIGN_SEC)) { 1125 if (step == DIRENT_STEP_SECD) { 1126 if (++order == num_ext) 1127 goto found; 1128 continue; 1129 } 1130 } 1131 step = DIRENT_STEP_FILE; 1132 } 1133 1134 if (clu.flags == ALLOC_NO_FAT_CHAIN) { 1135 if (--clu.size > 0) 1136 clu.dir++; 1137 else 1138 clu.dir = EXFAT_EOF_CLUSTER; 1139 } else { 1140 if (exfat_get_next_cluster(sb, &clu.dir)) 1141 return -EIO; 1142 1143 /* break if the cluster chain includes a loop */ 1144 if (unlikely(++clu_count > EXFAT_DATA_CLUSTER_COUNT(sbi))) 1145 goto not_found; 1146 } 1147 } 1148 1149 not_found: 1150 /* 1151 * We started at not 0 index,so we should try to find target 1152 * from 0 index to the index we started at. 1153 */ 1154 if (!rewind && end_eidx) { 1155 rewind = 1; 1156 dentry = 0; 1157 clu.dir = p_dir->dir; 1158 goto rewind; 1159 } 1160 1161 /* 1162 * set the EXFAT_EOF_CLUSTER flag to avoid search 1163 * from the beginning again when allocated a new cluster 1164 */ 1165 if (ei->hint_femp.eidx == EXFAT_HINT_NONE) { 1166 ei->hint_femp.cur.dir = EXFAT_EOF_CLUSTER; 1167 ei->hint_femp.eidx = p_dir->size * dentries_per_clu; 1168 ei->hint_femp.count = 0; 1169 } 1170 1171 /* initialized hint_stat */ 1172 hint_stat->clu = p_dir->dir; 1173 hint_stat->eidx = 0; 1174 return -ENOENT; 1175 1176 found: 1177 /* next dentry we'll find is out of this cluster */ 1178 if (!((dentry + 1) & (dentries_per_clu - 1))) { 1179 int ret = 0; 1180 1181 if (clu.flags == ALLOC_NO_FAT_CHAIN) { 1182 if (--clu.size > 0) 1183 clu.dir++; 1184 else 1185 clu.dir = EXFAT_EOF_CLUSTER; 1186 } else { 1187 ret = exfat_get_next_cluster(sb, &clu.dir); 1188 } 1189 1190 if (ret || clu.dir == EXFAT_EOF_CLUSTER) { 1191 /* just initialized hint_stat */ 1192 hint_stat->clu = p_dir->dir; 1193 hint_stat->eidx = 0; 1194 return (dentry - num_ext); 1195 } 1196 } 1197 1198 hint_stat->clu = clu.dir; 1199 hint_stat->eidx = dentry + 1; 1200 return dentry - num_ext; 1201 } 1202 1203 int exfat_count_dir_entries(struct super_block *sb, struct exfat_chain *p_dir) 1204 { 1205 int i, count = 0; 1206 int dentries_per_clu; 1207 unsigned int entry_type; 1208 unsigned int clu_count = 0; 1209 struct exfat_chain clu; 1210 struct exfat_dentry *ep; 1211 struct exfat_sb_info *sbi = EXFAT_SB(sb); 1212 struct buffer_head *bh; 1213 1214 dentries_per_clu = sbi->dentries_per_clu; 1215 1216 exfat_chain_dup(&clu, p_dir); 1217 1218 while (clu.dir != EXFAT_EOF_CLUSTER) { 1219 for (i = 0; i < dentries_per_clu; i++) { 1220 ep = exfat_get_dentry(sb, &clu, i, &bh); 1221 if (!ep) 1222 return -EIO; 1223 entry_type = exfat_get_entry_type(ep); 1224 brelse(bh); 1225 1226 if (entry_type == TYPE_UNUSED) 1227 return count; 1228 if (entry_type != TYPE_DIR) 1229 continue; 1230 count++; 1231 } 1232 1233 if (clu.flags == ALLOC_NO_FAT_CHAIN) { 1234 if (--clu.size > 0) 1235 clu.dir++; 1236 else 1237 clu.dir = EXFAT_EOF_CLUSTER; 1238 } else { 1239 if (exfat_get_next_cluster(sb, &(clu.dir))) 1240 return -EIO; 1241 1242 if (unlikely(++clu_count > sbi->used_clusters)) { 1243 exfat_fs_error(sb, "FAT or bitmap is corrupted"); 1244 return -EIO; 1245 } 1246 1247 } 1248 } 1249 1250 return count; 1251 } 1252 1253 static int exfat_get_volume_label_dentry(struct super_block *sb, 1254 struct exfat_entry_set_cache *es) 1255 { 1256 int i; 1257 int dentry = 0; 1258 unsigned int type; 1259 struct exfat_sb_info *sbi = EXFAT_SB(sb); 1260 struct exfat_hint_femp hint_femp; 1261 struct exfat_inode_info *ei = EXFAT_I(sb->s_root->d_inode); 1262 struct exfat_chain clu; 1263 struct exfat_dentry *ep; 1264 struct buffer_head *bh; 1265 1266 hint_femp.eidx = EXFAT_HINT_NONE; 1267 exfat_chain_set(&clu, sbi->root_dir, 0, ALLOC_FAT_CHAIN); 1268 1269 while (clu.dir != EXFAT_EOF_CLUSTER) { 1270 for (i = 0; i < sbi->dentries_per_clu; i++, dentry++) { 1271 ep = exfat_get_dentry(sb, &clu, i, &bh); 1272 if (!ep) 1273 return -EIO; 1274 1275 type = exfat_get_entry_type(ep); 1276 if (hint_femp.eidx == EXFAT_HINT_NONE) { 1277 if (type == TYPE_DELETED || type == TYPE_UNUSED) { 1278 hint_femp.cur = clu; 1279 hint_femp.eidx = dentry; 1280 hint_femp.count = 1; 1281 } 1282 } 1283 1284 if (type == TYPE_UNUSED) { 1285 brelse(bh); 1286 goto not_found; 1287 } 1288 1289 if (type != TYPE_VOLUME) { 1290 brelse(bh); 1291 continue; 1292 } 1293 1294 memset(es, 0, sizeof(*es)); 1295 es->sb = sb; 1296 es->bh = es->__bh; 1297 es->bh[0] = bh; 1298 es->num_bh = 1; 1299 es->start_off = EXFAT_DEN_TO_B(i) % sb->s_blocksize; 1300 1301 return 0; 1302 } 1303 1304 if (exfat_get_next_cluster(sb, &(clu.dir))) 1305 return -EIO; 1306 } 1307 1308 not_found: 1309 if (hint_femp.eidx == EXFAT_HINT_NONE) { 1310 hint_femp.cur.dir = EXFAT_EOF_CLUSTER; 1311 hint_femp.eidx = dentry; 1312 hint_femp.count = 0; 1313 } 1314 1315 ei->hint_femp = hint_femp; 1316 1317 return -ENOENT; 1318 } 1319 1320 int exfat_read_volume_label(struct super_block *sb, struct exfat_uni_name *label_out) 1321 { 1322 int ret, i; 1323 struct exfat_sb_info *sbi = EXFAT_SB(sb); 1324 struct exfat_entry_set_cache es; 1325 struct exfat_dentry *ep; 1326 1327 mutex_lock(&sbi->s_lock); 1328 1329 memset(label_out, 0, sizeof(*label_out)); 1330 ret = exfat_get_volume_label_dentry(sb, &es); 1331 if (ret < 0) { 1332 /* 1333 * ENOENT signifies that a volume label dentry doesn't exist 1334 * We will treat this as an empty volume label and not fail. 1335 */ 1336 if (ret == -ENOENT) 1337 ret = 0; 1338 1339 goto unlock; 1340 } 1341 1342 ep = exfat_get_dentry_cached(&es, 0); 1343 label_out->name_len = ep->dentry.volume_label.char_count; 1344 if (label_out->name_len > EXFAT_VOLUME_LABEL_LEN) { 1345 ret = -EIO; 1346 exfat_put_dentry_set(&es, false); 1347 goto unlock; 1348 } 1349 1350 for (i = 0; i < label_out->name_len; i++) 1351 label_out->name[i] = le16_to_cpu(ep->dentry.volume_label.volume_label[i]); 1352 1353 exfat_put_dentry_set(&es, false); 1354 unlock: 1355 mutex_unlock(&sbi->s_lock); 1356 return ret; 1357 } 1358 1359 int exfat_write_volume_label(struct super_block *sb, 1360 struct exfat_uni_name *label) 1361 { 1362 int ret, i; 1363 struct exfat_sb_info *sbi = EXFAT_SB(sb); 1364 struct inode *root_inode = sb->s_root->d_inode; 1365 struct exfat_entry_set_cache es; 1366 struct exfat_chain clu; 1367 struct exfat_dentry *ep; 1368 1369 if (label->name_len > EXFAT_VOLUME_LABEL_LEN) 1370 return -EINVAL; 1371 1372 mutex_lock(&sbi->s_lock); 1373 1374 ret = exfat_get_volume_label_dentry(sb, &es); 1375 if (ret == -ENOENT) { 1376 if (label->name_len == 0) { 1377 /* No volume label dentry, no need to clear */ 1378 ret = 0; 1379 goto unlock; 1380 } 1381 1382 ret = exfat_find_empty_entry(root_inode, &clu, 1, &es); 1383 } 1384 1385 if (ret < 0) 1386 goto unlock; 1387 1388 ep = exfat_get_dentry_cached(&es, 0); 1389 1390 if (label->name_len == 0 && ep->dentry.volume_label.char_count == 0) { 1391 /* volume label had been cleared */ 1392 exfat_put_dentry_set(&es, 0); 1393 goto unlock; 1394 } 1395 1396 memset(ep, 0, sizeof(*ep)); 1397 ep->type = EXFAT_VOLUME; 1398 1399 for (i = 0; i < label->name_len; i++) 1400 ep->dentry.volume_label.volume_label[i] = 1401 cpu_to_le16(label->name[i]); 1402 1403 ep->dentry.volume_label.char_count = label->name_len; 1404 es.modified = true; 1405 1406 ret = exfat_put_dentry_set(&es, IS_DIRSYNC(root_inode)); 1407 1408 unlock: 1409 mutex_unlock(&sbi->s_lock); 1410 return ret; 1411 } 1412