1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Copyright (C) 2012-2013 Samsung Electronics Co., Ltd.
6 #include <linux/types.h>
7 #include <linux/buffer_head.h>
9 #include <linux/mutex.h>
10 #include <linux/blkdev.h>
11 #include <linux/slab.h>
14 static void __set_sb_dirty(struct super_block *sb)
16 struct exfat_sb_info *sbi = EXFAT_SB(sb);
21 static u8 name_buf[MAX_PATH_LENGTH * MAX_CHARSET_SIZE];
23 static u8 free_bit[] = {
24 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, 4, 0, 1, 0, 2, /* 0 ~ 19 */
25 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, 5, 0, 1, 0, 2, 0, 1, 0, 3, /* 20 ~ 39 */
26 0, 1, 0, 2, 0, 1, 0, 4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, /* 40 ~ 59 */
27 0, 1, 0, 6, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, 4, /* 60 ~ 79 */
28 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, 5, 0, 1, 0, 2, /* 80 ~ 99 */
29 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, 4, 0, 1, 0, 2, 0, 1, 0, 3, /* 100 ~ 119 */
30 0, 1, 0, 2, 0, 1, 0, 7, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, /* 120 ~ 139 */
31 0, 1, 0, 4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, 5, /* 140 ~ 159 */
32 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, 4, 0, 1, 0, 2, /* 160 ~ 179 */
33 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, 6, 0, 1, 0, 2, 0, 1, 0, 3, /* 180 ~ 199 */
34 0, 1, 0, 2, 0, 1, 0, 4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, /* 200 ~ 219 */
35 0, 1, 0, 5, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, 4, /* 220 ~ 239 */
36 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0 /* 240 ~ 254 */
39 static u8 used_bit[] = {
40 0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4, 1, 2, 2, 3, /* 0 ~ 19 */
41 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5, 1, 2, 2, 3, 2, 3, 3, 4, /* 20 ~ 39 */
42 2, 3, 3, 4, 3, 4, 4, 5, 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, /* 40 ~ 59 */
43 4, 5, 5, 6, 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5, /* 60 ~ 79 */
44 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6, 2, 3, 3, 4, /* 80 ~ 99 */
45 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6, 3, 4, 4, 5, 4, 5, 5, 6, /* 100 ~ 119 */
46 4, 5, 5, 6, 5, 6, 6, 7, 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, /* 120 ~ 139 */
47 3, 4, 4, 5, 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6, /* 140 ~ 159 */
48 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6, 3, 4, 4, 5, /* 160 ~ 179 */
49 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7, 2, 3, 3, 4, 3, 4, 4, 5, /* 180 ~ 199 */
50 3, 4, 4, 5, 4, 5, 5, 6, 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, /* 200 ~ 219 */
51 5, 6, 6, 7, 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7, /* 220 ~ 239 */
52 4, 5, 5, 6, 5, 6, 6, 7, 5, 6, 6, 7, 6, 7, 7, 8 /* 240 ~ 255 */
55 #define BITMAP_LOC(v) ((v) >> 3)
56 #define BITMAP_SHIFT(v) ((v) & 0x07)
58 static inline s32 exfat_bitmap_test(u8 *bitmap, int i)
62 data = bitmap[BITMAP_LOC(i)];
63 if ((data >> BITMAP_SHIFT(i)) & 0x01)
68 static inline void exfat_bitmap_set(u8 *bitmap, int i)
70 bitmap[BITMAP_LOC(i)] |= (0x01 << BITMAP_SHIFT(i));
73 static inline void exfat_bitmap_clear(u8 *bitmap, int i)
75 bitmap[BITMAP_LOC(i)] &= ~(0x01 << BITMAP_SHIFT(i));
79 * File System Management Functions
82 void fs_set_vol_flags(struct super_block *sb, u32 new_flag)
84 struct pbr_sector_t *p_pbr;
85 struct bpbex_t *p_bpb;
86 struct fs_info_t *p_fs = &(EXFAT_SB(sb)->fs_info);
88 if (p_fs->vol_flag == new_flag)
91 p_fs->vol_flag = new_flag;
94 if (sector_read(sb, p_fs->PBR_sector,
95 &p_fs->pbr_bh, 1) != 0)
99 p_pbr = (struct pbr_sector_t *)p_fs->pbr_bh->b_data;
100 p_bpb = (struct bpbex_t *)p_pbr->bpb;
101 SET16(p_bpb->vol_flags, (u16)new_flag);
104 * what can we do here? (cuz fs_set_vol_flags() is void)
106 if ((new_flag == VOL_DIRTY) && (!buffer_dirty(p_fs->pbr_bh)))
107 sector_write(sb, p_fs->PBR_sector, p_fs->pbr_bh, 1);
109 sector_write(sb, p_fs->PBR_sector, p_fs->pbr_bh, 0);
112 void fs_error(struct super_block *sb)
114 struct exfat_mount_options *opts = &EXFAT_SB(sb)->options;
116 if (opts->errors == EXFAT_ERRORS_PANIC) {
117 panic("[EXFAT] Filesystem panic from previous error\n");
118 } else if ((opts->errors == EXFAT_ERRORS_RO) && !sb_rdonly(sb)) {
119 sb->s_flags |= SB_RDONLY;
120 pr_err("[EXFAT] Filesystem has been set read-only\n");
125 * Cluster Management Functions
128 static s32 clear_cluster(struct super_block *sb, u32 clu)
132 struct buffer_head *tmp_bh = NULL;
133 struct fs_info_t *p_fs = &(EXFAT_SB(sb)->fs_info);
134 struct bd_info_t *p_bd = &(EXFAT_SB(sb)->bd_info);
136 if (clu == CLUSTER_32(0)) { /* FAT16 root_dir */
137 s = p_fs->root_start_sector;
138 n = p_fs->data_start_sector;
140 s = START_SECTOR(clu);
141 n = s + p_fs->sectors_per_clu;
145 ret = sector_read(sb, s, &tmp_bh, 0);
149 memset((char *)tmp_bh->b_data, 0x0, p_bd->sector_size);
150 ret = sector_write(sb, s, tmp_bh, 0);
159 static s32 set_alloc_bitmap(struct super_block *sb, u32 clu)
163 struct fs_info_t *p_fs = &(EXFAT_SB(sb)->fs_info);
164 struct bd_info_t *p_bd = &(EXFAT_SB(sb)->bd_info);
166 i = clu >> (p_bd->sector_size_bits + 3);
167 b = clu & ((p_bd->sector_size << 3) - 1);
169 sector = START_SECTOR(p_fs->map_clu) + i;
171 exfat_bitmap_set((u8 *)p_fs->vol_amap[i]->b_data, b);
173 return sector_write(sb, sector, p_fs->vol_amap[i], 0);
176 static s32 clr_alloc_bitmap(struct super_block *sb, u32 clu)
180 #ifdef CONFIG_EXFAT_DISCARD
181 struct exfat_sb_info *sbi = EXFAT_SB(sb);
182 struct exfat_mount_options *opts = &sbi->options;
184 #endif /* CONFIG_EXFAT_DISCARD */
185 struct fs_info_t *p_fs = &(EXFAT_SB(sb)->fs_info);
186 struct bd_info_t *p_bd = &(EXFAT_SB(sb)->bd_info);
188 i = clu >> (p_bd->sector_size_bits + 3);
189 b = clu & ((p_bd->sector_size << 3) - 1);
191 sector = START_SECTOR(p_fs->map_clu) + i;
193 exfat_bitmap_clear((u8 *)p_fs->vol_amap[i]->b_data, b);
195 return sector_write(sb, sector, p_fs->vol_amap[i], 0);
197 #ifdef CONFIG_EXFAT_DISCARD
199 ret = sb_issue_discard(sb, START_SECTOR(clu),
200 (1 << p_fs->sectors_per_clu_bits),
202 if (ret == -EOPNOTSUPP) {
203 pr_warn("discard not supported by device, disabling");
207 #endif /* CONFIG_EXFAT_DISCARD */
210 static u32 test_alloc_bitmap(struct super_block *sb, u32 clu)
213 u32 clu_base, clu_free;
215 struct fs_info_t *p_fs = &(EXFAT_SB(sb)->fs_info);
216 struct bd_info_t *p_bd = &(EXFAT_SB(sb)->bd_info);
218 clu_base = (clu & ~(0x7)) + 2;
219 clu_mask = (1 << (clu - clu_base + 2)) - 1;
221 map_i = clu >> (p_bd->sector_size_bits + 3);
222 map_b = (clu >> 3) & p_bd->sector_size_mask;
224 for (i = 2; i < p_fs->num_clusters; i += 8) {
225 k = *(((u8 *)p_fs->vol_amap[map_i]->b_data) + map_b);
231 clu_free = clu_base + free_bit[k];
232 if (clu_free < p_fs->num_clusters)
237 if (((++map_b) >= p_bd->sector_size) ||
238 (clu_base >= p_fs->num_clusters)) {
239 if ((++map_i) >= p_fs->map_sectors) {
247 return CLUSTER_32(~0);
250 static s32 exfat_alloc_cluster(struct super_block *sb, s32 num_alloc,
251 struct chain_t *p_chain)
253 s32 num_clusters = 0;
254 u32 hint_clu, new_clu, last_clu = CLUSTER_32(~0);
255 struct fs_info_t *p_fs = &(EXFAT_SB(sb)->fs_info);
257 hint_clu = p_chain->dir;
258 if (hint_clu == CLUSTER_32(~0)) {
259 hint_clu = test_alloc_bitmap(sb, p_fs->clu_srch_ptr - 2);
260 if (hint_clu == CLUSTER_32(~0))
262 } else if (hint_clu >= p_fs->num_clusters) {
264 p_chain->flags = 0x01;
269 p_chain->dir = CLUSTER_32(~0);
271 while ((new_clu = test_alloc_bitmap(sb, hint_clu - 2)) != CLUSTER_32(~0)) {
272 if (new_clu != hint_clu) {
273 if (p_chain->flags == 0x03) {
274 exfat_chain_cont_cluster(sb, p_chain->dir,
276 p_chain->flags = 0x01;
280 if (set_alloc_bitmap(sb, new_clu - 2) != 0)
285 if (p_chain->flags == 0x01) {
286 if (exfat_fat_write(sb, new_clu, CLUSTER_32(~0)) < 0)
290 if (p_chain->dir == CLUSTER_32(~0)) {
291 p_chain->dir = new_clu;
293 if (p_chain->flags == 0x01) {
294 if (exfat_fat_write(sb, last_clu, new_clu) < 0)
300 if ((--num_alloc) == 0) {
301 p_fs->clu_srch_ptr = hint_clu;
302 if (p_fs->used_clusters != UINT_MAX)
303 p_fs->used_clusters += num_clusters;
305 p_chain->size += num_clusters;
309 hint_clu = new_clu + 1;
310 if (hint_clu >= p_fs->num_clusters) {
313 if (p_chain->flags == 0x03) {
314 exfat_chain_cont_cluster(sb, p_chain->dir,
316 p_chain->flags = 0x01;
321 p_fs->clu_srch_ptr = hint_clu;
322 if (p_fs->used_clusters != UINT_MAX)
323 p_fs->used_clusters += num_clusters;
325 p_chain->size += num_clusters;
329 static void exfat_free_cluster(struct super_block *sb, struct chain_t *p_chain,
332 s32 num_clusters = 0;
334 struct fs_info_t *p_fs = &(EXFAT_SB(sb)->fs_info);
338 if ((p_chain->dir == CLUSTER_32(0)) || (p_chain->dir == CLUSTER_32(~0)))
341 if (p_chain->size <= 0) {
342 pr_err("[EXFAT] free_cluster : skip free-req clu:%u, because of zero-size truncation\n",
350 if (p_chain->flags == 0x03) {
353 sector = START_SECTOR(clu);
354 for (i = 0; i < p_fs->sectors_per_clu; i++)
355 exfat_buf_release(sb, sector + i);
358 if (clr_alloc_bitmap(sb, clu - 2) != 0)
363 } while (num_clusters < p_chain->size);
366 if (p_fs->dev_ejected)
370 sector = START_SECTOR(clu);
371 for (i = 0; i < p_fs->sectors_per_clu; i++)
372 exfat_buf_release(sb, sector + i);
375 if (clr_alloc_bitmap(sb, clu - 2) != 0)
378 if (exfat_fat_read(sb, clu, &clu) == -1)
381 } while ((clu != CLUSTER_32(0)) && (clu != CLUSTER_32(~0)));
384 if (p_fs->used_clusters != UINT_MAX)
385 p_fs->used_clusters -= num_clusters;
388 static u32 find_last_cluster(struct super_block *sb, struct chain_t *p_chain)
391 struct fs_info_t *p_fs = &(EXFAT_SB(sb)->fs_info);
395 if (p_chain->flags == 0x03) {
396 clu += p_chain->size - 1;
398 while ((exfat_fat_read(sb, clu, &next) == 0) &&
399 (next != CLUSTER_32(~0))) {
400 if (p_fs->dev_ejected)
409 s32 count_num_clusters(struct super_block *sb, struct chain_t *p_chain)
413 struct fs_info_t *p_fs = &(EXFAT_SB(sb)->fs_info);
415 if ((p_chain->dir == CLUSTER_32(0)) || (p_chain->dir == CLUSTER_32(~0)))
420 if (p_chain->flags == 0x03) {
421 count = p_chain->size;
423 for (i = 2; i < p_fs->num_clusters; i++) {
425 if (exfat_fat_read(sb, clu, &clu) != 0)
427 if (clu == CLUSTER_32(~0))
435 static s32 exfat_count_used_clusters(struct super_block *sb)
437 int i, map_i, map_b, count = 0;
439 struct fs_info_t *p_fs = &(EXFAT_SB(sb)->fs_info);
440 struct bd_info_t *p_bd = &(EXFAT_SB(sb)->bd_info);
445 for (i = 2; i < p_fs->num_clusters; i += 8) {
446 k = *(((u8 *)p_fs->vol_amap[map_i]->b_data) + map_b);
447 count += used_bit[k];
449 if ((++map_b) >= p_bd->sector_size) {
458 void exfat_chain_cont_cluster(struct super_block *sb, u32 chain, s32 len)
464 if (exfat_fat_write(sb, chain, chain + 1) < 0)
469 exfat_fat_write(sb, chain, CLUSTER_32(~0));
473 * Allocation Bitmap Management Functions
476 s32 load_alloc_bitmap(struct super_block *sb)
483 struct bmap_dentry_t *ep;
484 struct fs_info_t *p_fs = &(EXFAT_SB(sb)->fs_info);
485 struct bd_info_t *p_bd = &(EXFAT_SB(sb)->bd_info);
487 clu.dir = p_fs->root_dir;
490 while (clu.dir != CLUSTER_32(~0)) {
491 if (p_fs->dev_ejected)
494 for (i = 0; i < p_fs->dentries_per_clu; i++) {
495 ep = (struct bmap_dentry_t *)get_entry_in_dir(sb, &clu,
500 type = p_fs->fs_func->get_entry_type((struct dentry_t *)ep);
502 if (type == TYPE_UNUSED)
504 if (type != TYPE_BITMAP)
507 if (ep->flags == 0x0) {
508 p_fs->map_clu = GET32_A(ep->start_clu);
509 map_size = (u32)GET64_A(ep->size);
511 p_fs->map_sectors = ((map_size - 1) >> p_bd->sector_size_bits) + 1;
513 p_fs->vol_amap = kmalloc_array(p_fs->map_sectors,
514 sizeof(struct buffer_head *),
519 sector = START_SECTOR(p_fs->map_clu);
521 for (j = 0; j < p_fs->map_sectors; j++) {
522 p_fs->vol_amap[j] = NULL;
523 ret = sector_read(sb, sector + j, &p_fs->vol_amap[j], 1);
525 /* release all buffers and free vol_amap */
528 brelse(p_fs->vol_amap[i++]);
530 kfree(p_fs->vol_amap);
531 p_fs->vol_amap = NULL;
541 if (exfat_fat_read(sb, clu.dir, &clu.dir) != 0)
545 return -EFSCORRUPTED;
548 void free_alloc_bitmap(struct super_block *sb)
551 struct fs_info_t *p_fs = &(EXFAT_SB(sb)->fs_info);
553 brelse(p_fs->pbr_bh);
555 for (i = 0; i < p_fs->map_sectors; i++)
556 __brelse(p_fs->vol_amap[i]);
558 kfree(p_fs->vol_amap);
559 p_fs->vol_amap = NULL;
562 void sync_alloc_bitmap(struct super_block *sb)
565 struct fs_info_t *p_fs = &(EXFAT_SB(sb)->fs_info);
570 for (i = 0; i < p_fs->map_sectors; i++)
571 sync_dirty_buffer(p_fs->vol_amap[i]);
575 * Upcase table Management Functions
577 static s32 __load_upcase_table(struct super_block *sb, sector_t sector,
578 u32 num_sectors, u32 utbl_checksum)
580 int i, ret = -EINVAL;
582 struct fs_info_t *p_fs = &(EXFAT_SB(sb)->fs_info);
583 struct bd_info_t *p_bd = &(EXFAT_SB(sb)->bd_info);
584 struct buffer_head *tmp_bh = NULL;
585 sector_t end_sector = num_sectors + sector;
594 upcase_table = kmalloc_array(UTBL_COL_COUNT, sizeof(u16 *), GFP_KERNEL);
595 p_fs->vol_utbl = upcase_table;
598 memset(upcase_table, 0, UTBL_COL_COUNT * sizeof(u16 *));
600 while (sector < end_sector) {
601 ret = sector_read(sb, sector, &tmp_bh, 1);
603 pr_debug("sector read (0x%llX)fail\n",
604 (unsigned long long)sector);
609 for (i = 0; i < p_bd->sector_size && index <= 0xFFFF; i += 2) {
610 uni = GET16(((u8 *)tmp_bh->b_data) + i);
612 checksum = ((checksum & 1) ? 0x80000000 : 0) +
613 (checksum >> 1) + *(((u8 *)tmp_bh->b_data) +
615 checksum = ((checksum & 1) ? 0x80000000 : 0) +
616 (checksum >> 1) + *(((u8 *)tmp_bh->b_data) +
620 pr_debug("skip from 0x%X ", index);
622 pr_debug("to 0x%X (amount of 0x%X)\n",
625 } else if (uni == index) {
627 } else if (uni == 0xFFFF) {
629 } else { /* uni != index , uni != 0xFFFF */
630 u16 col_index = get_col_index(index);
632 if (!upcase_table[col_index]) {
633 pr_debug("alloc = 0x%X\n", col_index);
634 upcase_table[col_index] = kmalloc_array(UTBL_ROW_COUNT,
635 sizeof(u16), GFP_KERNEL);
636 if (!upcase_table[col_index]) {
641 for (j = 0; j < UTBL_ROW_COUNT; j++)
642 upcase_table[col_index][j] = (col_index << LOW_INDEX_BIT) | j;
645 upcase_table[col_index][get_row_index(index)] = uni;
650 if (index >= 0xFFFF && utbl_checksum == checksum) {
659 free_upcase_table(sb);
663 static s32 __load_default_upcase_table(struct super_block *sb)
665 int i, ret = -EINVAL;
667 struct fs_info_t *p_fs = &(EXFAT_SB(sb)->fs_info);
674 upcase_table = kmalloc_array(UTBL_COL_COUNT, sizeof(u16 *), GFP_KERNEL);
675 p_fs->vol_utbl = upcase_table;
678 memset(upcase_table, 0, UTBL_COL_COUNT * sizeof(u16 *));
680 for (i = 0; index <= 0xFFFF && i < NUM_UPCASE * 2; i += 2) {
681 uni = GET16(uni_upcase + i);
683 pr_debug("skip from 0x%X ", index);
685 pr_debug("to 0x%X (amount of 0x%X)\n", index, uni);
687 } else if (uni == index) {
689 } else if (uni == 0xFFFF) {
691 } else { /* uni != index , uni != 0xFFFF */
692 u16 col_index = get_col_index(index);
694 if (!upcase_table[col_index]) {
695 pr_debug("alloc = 0x%X\n", col_index);
696 upcase_table[col_index] = kmalloc_array(UTBL_ROW_COUNT,
699 if (!upcase_table[col_index]) {
704 for (j = 0; j < UTBL_ROW_COUNT; j++)
705 upcase_table[col_index][j] = (col_index << LOW_INDEX_BIT) | j;
708 upcase_table[col_index][get_row_index(index)] = uni;
717 /* FATAL error: default upcase table has error */
718 free_upcase_table(sb);
722 s32 load_upcase_table(struct super_block *sb)
725 u32 tbl_clu, tbl_size;
727 u32 type, num_sectors;
729 struct case_dentry_t *ep;
730 struct fs_info_t *p_fs = &(EXFAT_SB(sb)->fs_info);
731 struct bd_info_t *p_bd = &(EXFAT_SB(sb)->bd_info);
733 clu.dir = p_fs->root_dir;
736 if (p_fs->dev_ejected)
739 while (clu.dir != CLUSTER_32(~0)) {
740 for (i = 0; i < p_fs->dentries_per_clu; i++) {
741 ep = (struct case_dentry_t *)get_entry_in_dir(sb, &clu,
746 type = p_fs->fs_func->get_entry_type((struct dentry_t *)ep);
748 if (type == TYPE_UNUSED)
750 if (type != TYPE_UPCASE)
753 tbl_clu = GET32_A(ep->start_clu);
754 tbl_size = (u32)GET64_A(ep->size);
756 sector = START_SECTOR(tbl_clu);
757 num_sectors = ((tbl_size - 1) >> p_bd->sector_size_bits) + 1;
758 if (__load_upcase_table(sb, sector, num_sectors,
759 GET32_A(ep->checksum)) != 0)
763 if (exfat_fat_read(sb, clu.dir, &clu.dir) != 0)
766 /* load default upcase table */
767 return __load_default_upcase_table(sb);
770 void free_upcase_table(struct super_block *sb)
773 struct fs_info_t *p_fs = &(EXFAT_SB(sb)->fs_info);
776 upcase_table = p_fs->vol_utbl;
777 for (i = 0; i < UTBL_COL_COUNT; i++)
778 kfree(upcase_table[i]);
780 kfree(p_fs->vol_utbl);
781 p_fs->vol_utbl = NULL;
785 * Directory Entry Management Functions
788 static u32 exfat_get_entry_type(struct dentry_t *p_entry)
790 struct file_dentry_t *ep = (struct file_dentry_t *)p_entry;
792 if (ep->type == 0x0) {
794 } else if (ep->type < 0x80) {
796 } else if (ep->type == 0x80) {
798 } else if (ep->type < 0xA0) {
799 if (ep->type == 0x81) {
801 } else if (ep->type == 0x82) {
803 } else if (ep->type == 0x83) {
805 } else if (ep->type == 0x85) {
806 if (GET16_A(ep->attr) & ATTR_SUBDIR)
811 return TYPE_CRITICAL_PRI;
812 } else if (ep->type < 0xC0) {
813 if (ep->type == 0xA0)
815 else if (ep->type == 0xA1)
817 else if (ep->type == 0xA2)
819 return TYPE_BENIGN_PRI;
820 } else if (ep->type < 0xE0) {
821 if (ep->type == 0xC0)
823 else if (ep->type == 0xC1)
825 else if (ep->type == 0xC2)
827 return TYPE_CRITICAL_SEC;
830 return TYPE_BENIGN_SEC;
833 static void exfat_set_entry_type(struct dentry_t *p_entry, u32 type)
835 struct file_dentry_t *ep = (struct file_dentry_t *)p_entry;
837 if (type == TYPE_UNUSED) {
839 } else if (type == TYPE_DELETED) {
841 } else if (type == TYPE_STREAM) {
843 } else if (type == TYPE_EXTEND) {
845 } else if (type == TYPE_BITMAP) {
847 } else if (type == TYPE_UPCASE) {
849 } else if (type == TYPE_VOLUME) {
851 } else if (type == TYPE_DIR) {
853 SET16_A(ep->attr, ATTR_SUBDIR);
854 } else if (type == TYPE_FILE) {
856 SET16_A(ep->attr, ATTR_ARCHIVE);
857 } else if (type == TYPE_SYMLINK) {
859 SET16_A(ep->attr, ATTR_ARCHIVE | ATTR_SYMLINK);
863 static u32 exfat_get_entry_attr(struct dentry_t *p_entry)
865 struct file_dentry_t *ep = (struct file_dentry_t *)p_entry;
867 return (u32)GET16_A(ep->attr);
870 static void exfat_set_entry_attr(struct dentry_t *p_entry, u32 attr)
872 struct file_dentry_t *ep = (struct file_dentry_t *)p_entry;
874 SET16_A(ep->attr, (u16)attr);
877 static u8 exfat_get_entry_flag(struct dentry_t *p_entry)
879 struct strm_dentry_t *ep = (struct strm_dentry_t *)p_entry;
884 static void exfat_set_entry_flag(struct dentry_t *p_entry, u8 flags)
886 struct strm_dentry_t *ep = (struct strm_dentry_t *)p_entry;
891 static u32 exfat_get_entry_clu0(struct dentry_t *p_entry)
893 struct strm_dentry_t *ep = (struct strm_dentry_t *)p_entry;
895 return GET32_A(ep->start_clu);
898 static void exfat_set_entry_clu0(struct dentry_t *p_entry, u32 start_clu)
900 struct strm_dentry_t *ep = (struct strm_dentry_t *)p_entry;
902 SET32_A(ep->start_clu, start_clu);
905 static u64 exfat_get_entry_size(struct dentry_t *p_entry)
907 struct strm_dentry_t *ep = (struct strm_dentry_t *)p_entry;
909 return GET64_A(ep->valid_size);
912 static void exfat_set_entry_size(struct dentry_t *p_entry, u64 size)
914 struct strm_dentry_t *ep = (struct strm_dentry_t *)p_entry;
916 SET64_A(ep->valid_size, size);
917 SET64_A(ep->size, size);
920 static void exfat_get_entry_time(struct dentry_t *p_entry, struct timestamp_t *tp,
923 u16 t = 0x00, d = 0x21;
924 struct file_dentry_t *ep = (struct file_dentry_t *)p_entry;
928 t = GET16_A(ep->create_time);
929 d = GET16_A(ep->create_date);
932 t = GET16_A(ep->modify_time);
933 d = GET16_A(ep->modify_date);
936 t = GET16_A(ep->access_time);
937 d = GET16_A(ep->access_date);
941 tp->sec = (t & 0x001F) << 1;
942 tp->min = (t >> 5) & 0x003F;
943 tp->hour = (t >> 11);
944 tp->day = (d & 0x001F);
945 tp->mon = (d >> 5) & 0x000F;
949 static void exfat_set_entry_time(struct dentry_t *p_entry, struct timestamp_t *tp,
953 struct file_dentry_t *ep = (struct file_dentry_t *)p_entry;
955 t = (tp->hour << 11) | (tp->min << 5) | (tp->sec >> 1);
956 d = (tp->year << 9) | (tp->mon << 5) | tp->day;
960 SET16_A(ep->create_time, t);
961 SET16_A(ep->create_date, d);
964 SET16_A(ep->modify_time, t);
965 SET16_A(ep->modify_date, d);
968 SET16_A(ep->access_time, t);
969 SET16_A(ep->access_date, d);
974 static void init_file_entry(struct file_dentry_t *ep, u32 type)
976 struct timestamp_t tm, *tp;
978 exfat_set_entry_type((struct dentry_t *)ep, type);
980 tp = tm_current(&tm);
981 exfat_set_entry_time((struct dentry_t *)ep, tp, TM_CREATE);
982 exfat_set_entry_time((struct dentry_t *)ep, tp, TM_MODIFY);
983 exfat_set_entry_time((struct dentry_t *)ep, tp, TM_ACCESS);
984 ep->create_time_ms = 0;
985 ep->modify_time_ms = 0;
986 ep->access_time_ms = 0;
989 static void init_strm_entry(struct strm_dentry_t *ep, u8 flags, u32 start_clu, u64 size)
991 exfat_set_entry_type((struct dentry_t *)ep, TYPE_STREAM);
993 SET32_A(ep->start_clu, start_clu);
994 SET64_A(ep->valid_size, size);
995 SET64_A(ep->size, size);
998 static void init_name_entry(struct name_dentry_t *ep, u16 *uniname)
1002 exfat_set_entry_type((struct dentry_t *)ep, TYPE_EXTEND);
1005 for (i = 0; i < 30; i++, i++) {
1006 SET16_A(ep->unicode_0_14 + i, *uniname);
1007 if (*uniname == 0x0)
1013 static s32 exfat_init_dir_entry(struct super_block *sb, struct chain_t *p_dir,
1014 s32 entry, u32 type, u32 start_clu, u64 size)
1018 struct file_dentry_t *file_ep;
1019 struct strm_dentry_t *strm_ep;
1021 flags = (type == TYPE_FILE) ? 0x01 : 0x03;
1023 /* we cannot use get_entry_set_in_dir here because file ep is not initialized yet */
1024 file_ep = (struct file_dentry_t *)get_entry_in_dir(sb, p_dir, entry,
1029 strm_ep = (struct strm_dentry_t *)get_entry_in_dir(sb, p_dir, entry + 1,
1034 init_file_entry(file_ep, type);
1035 exfat_buf_modify(sb, sector);
1037 init_strm_entry(strm_ep, flags, start_clu, size);
1038 exfat_buf_modify(sb, sector);
1043 static s32 exfat_init_ext_entry(struct super_block *sb, struct chain_t *p_dir,
1044 s32 entry, s32 num_entries,
1045 struct uni_name_t *p_uniname,
1046 struct dos_name_t *p_dosname)
1050 u16 *uniname = p_uniname->name;
1051 struct file_dentry_t *file_ep;
1052 struct strm_dentry_t *strm_ep;
1053 struct name_dentry_t *name_ep;
1055 file_ep = (struct file_dentry_t *)get_entry_in_dir(sb, p_dir, entry,
1060 file_ep->num_ext = (u8)(num_entries - 1);
1061 exfat_buf_modify(sb, sector);
1063 strm_ep = (struct strm_dentry_t *)get_entry_in_dir(sb, p_dir, entry + 1,
1068 strm_ep->name_len = p_uniname->name_len;
1069 SET16_A(strm_ep->name_hash, p_uniname->name_hash);
1070 exfat_buf_modify(sb, sector);
1072 for (i = 2; i < num_entries; i++) {
1073 name_ep = (struct name_dentry_t *)get_entry_in_dir(sb, p_dir,
1079 init_name_entry(name_ep, uniname);
1080 exfat_buf_modify(sb, sector);
1084 update_dir_checksum(sb, p_dir, entry);
1089 static void exfat_delete_dir_entry(struct super_block *sb, struct chain_t *p_dir,
1090 s32 entry, s32 order, s32 num_entries)
1094 struct dentry_t *ep;
1095 struct fs_info_t *p_fs = &(EXFAT_SB(sb)->fs_info);
1097 for (i = order; i < num_entries; i++) {
1098 ep = get_entry_in_dir(sb, p_dir, entry + i, §or);
1102 p_fs->fs_func->set_entry_type(ep, TYPE_DELETED);
1103 exfat_buf_modify(sb, sector);
1107 void update_dir_checksum(struct super_block *sb, struct chain_t *p_dir,
1113 struct file_dentry_t *file_ep;
1114 struct dentry_t *ep;
1116 file_ep = (struct file_dentry_t *)get_entry_in_dir(sb, p_dir, entry,
1121 exfat_buf_lock(sb, sector);
1123 num_entries = (s32)file_ep->num_ext + 1;
1124 chksum = calc_checksum_2byte((void *)file_ep, DENTRY_SIZE, 0,
1127 for (i = 1; i < num_entries; i++) {
1128 ep = get_entry_in_dir(sb, p_dir, entry + i, NULL);
1130 exfat_buf_unlock(sb, sector);
1134 chksum = calc_checksum_2byte((void *)ep, DENTRY_SIZE, chksum,
1138 SET16_A(file_ep->checksum, chksum);
1139 exfat_buf_modify(sb, sector);
1140 exfat_buf_unlock(sb, sector);
1143 static s32 __write_partial_entries_in_entry_set(struct super_block *sb,
1144 struct entry_set_cache_t *es,
1145 sector_t sec, s32 off, u32 count)
1147 s32 num_entries, buf_off = (off - es->offset);
1148 u32 remaining_byte_in_sector, copy_entries;
1149 struct fs_info_t *p_fs = &(EXFAT_SB(sb)->fs_info);
1150 struct bd_info_t *p_bd = &(EXFAT_SB(sb)->bd_info);
1152 u8 *buf, *esbuf = (u8 *)&es->__buf;
1154 pr_debug("%s entered es %p sec %llu off %d count %d\n",
1155 __func__, es, (unsigned long long)sec, off, count);
1156 num_entries = count;
1158 while (num_entries) {
1159 /* white per sector base */
1160 remaining_byte_in_sector = (1 << p_bd->sector_size_bits) - off;
1161 copy_entries = min_t(s32,
1162 remaining_byte_in_sector >> DENTRY_SIZE_BITS,
1164 buf = exfat_buf_getblk(sb, sec);
1167 pr_debug("es->buf %p buf_off %u\n", esbuf, buf_off);
1168 pr_debug("copying %d entries from %p to sector %llu\n",
1169 copy_entries, (esbuf + buf_off),
1170 (unsigned long long)sec);
1171 memcpy(buf + off, esbuf + buf_off,
1172 copy_entries << DENTRY_SIZE_BITS);
1173 exfat_buf_modify(sb, sec);
1174 num_entries -= copy_entries;
1177 /* get next sector */
1178 if (IS_LAST_SECTOR_IN_CLUSTER(sec)) {
1179 clu = GET_CLUSTER_FROM_SECTOR(sec);
1180 if (es->alloc_flag == 0x03) {
1183 if (exfat_fat_read(sb, clu, &clu) == -1)
1186 sec = START_SECTOR(clu);
1191 buf_off += copy_entries << DENTRY_SIZE_BITS;
1195 pr_debug("%s exited successfully\n", __func__);
1198 pr_debug("%s failed\n", __func__);
1202 /* write back all entries in entry set */
1203 static s32 write_whole_entry_set(struct super_block *sb, struct entry_set_cache_t *es)
1205 return __write_partial_entries_in_entry_set(sb, es, es->sector,
1210 void update_dir_checksum_with_entry_set(struct super_block *sb,
1211 struct entry_set_cache_t *es)
1213 struct dentry_t *ep;
1215 s32 chksum_type = CS_DIR_ENTRY, i;
1217 ep = (struct dentry_t *)&es->__buf;
1218 for (i = 0; i < es->num_entries; i++) {
1219 pr_debug("%s ep %p\n", __func__, ep);
1220 chksum = calc_checksum_2byte((void *)ep, DENTRY_SIZE, chksum,
1223 chksum_type = CS_DEFAULT;
1226 ep = (struct dentry_t *)&es->__buf;
1227 SET16_A(((struct file_dentry_t *)ep)->checksum, chksum);
1228 write_whole_entry_set(sb, es);
1231 static s32 _walk_fat_chain(struct super_block *sb, struct chain_t *p_dir,
1232 s32 byte_offset, u32 *clu)
1234 struct fs_info_t *p_fs = &(EXFAT_SB(sb)->fs_info);
1238 clu_offset = byte_offset >> p_fs->cluster_size_bits;
1239 cur_clu = p_dir->dir;
1241 if (p_dir->flags == 0x03) {
1242 cur_clu += clu_offset;
1244 while (clu_offset > 0) {
1245 if (exfat_fat_read(sb, cur_clu, &cur_clu) == -1)
1256 static s32 find_location(struct super_block *sb, struct chain_t *p_dir, s32 entry,
1257 sector_t *sector, s32 *offset)
1261 struct fs_info_t *p_fs = &(EXFAT_SB(sb)->fs_info);
1262 struct bd_info_t *p_bd = &(EXFAT_SB(sb)->bd_info);
1264 off = entry << DENTRY_SIZE_BITS;
1266 if (p_dir->dir == CLUSTER_32(0)) { /* FAT16 root_dir */
1267 *offset = off & p_bd->sector_size_mask;
1268 *sector = off >> p_bd->sector_size_bits;
1269 *sector += p_fs->root_start_sector;
1271 ret = _walk_fat_chain(sb, p_dir, off, &clu);
1275 /* byte offset in cluster */
1276 off &= p_fs->cluster_size - 1;
1278 /* byte offset in sector */
1279 *offset = off & p_bd->sector_size_mask;
1281 /* sector offset in cluster */
1282 *sector = off >> p_bd->sector_size_bits;
1283 *sector += START_SECTOR(clu);
1288 struct dentry_t *get_entry_in_dir(struct super_block *sb, struct chain_t *p_dir,
1289 s32 entry, sector_t *sector)
1295 if (find_location(sb, p_dir, entry, &sec, &off) != 0)
1298 buf = exfat_buf_getblk(sb, sec);
1305 return (struct dentry_t *)(buf + off);
1308 /* returns a set of dentries for a file or dir.
1309 * Note that this is a copy (dump) of dentries so that user should call write_entry_set()
1310 * to apply changes made in this entry set to the real device.
1312 * sb+p_dir+entry: indicates a file/dir
1313 * type: specifies how many dentries should be included.
1315 * file_ep: will point the first dentry(= file dentry) on success
1317 * pointer of entry set on success,
1321 #define ES_MODE_STARTED 0
1322 #define ES_MODE_GET_FILE_ENTRY 1
1323 #define ES_MODE_GET_STRM_ENTRY 2
1324 #define ES_MODE_GET_NAME_ENTRY 3
1325 #define ES_MODE_GET_CRITICAL_SEC_ENTRY 4
1326 struct entry_set_cache_t *get_entry_set_in_dir(struct super_block *sb,
1327 struct chain_t *p_dir, s32 entry,
1329 struct dentry_t **file_ep)
1331 s32 off, ret, byte_offset;
1335 struct fs_info_t *p_fs = &(EXFAT_SB(sb)->fs_info);
1336 struct bd_info_t *p_bd = &(EXFAT_SB(sb)->bd_info);
1337 struct entry_set_cache_t *es = NULL;
1338 struct dentry_t *ep, *pos;
1341 s32 mode = ES_MODE_STARTED;
1344 pr_debug("%s entered p_dir dir %u flags %x size %d\n",
1345 __func__, p_dir->dir, p_dir->flags, p_dir->size);
1347 byte_offset = entry << DENTRY_SIZE_BITS;
1348 ret = _walk_fat_chain(sb, p_dir, byte_offset, &clu);
1352 /* byte offset in cluster */
1353 byte_offset &= p_fs->cluster_size - 1;
1355 /* byte offset in sector */
1356 off = byte_offset & p_bd->sector_size_mask;
1358 /* sector offset in cluster */
1359 sec = byte_offset >> p_bd->sector_size_bits;
1360 sec += START_SECTOR(clu);
1362 buf = exfat_buf_getblk(sb, sec);
1366 ep = (struct dentry_t *)(buf + off);
1367 entry_type = p_fs->fs_func->get_entry_type(ep);
1369 if ((entry_type != TYPE_FILE) && (entry_type != TYPE_DIR))
1372 if (type == ES_ALL_ENTRIES)
1373 num_entries = ((struct file_dentry_t *)ep)->num_ext + 1;
1377 bufsize = offsetof(struct entry_set_cache_t, __buf) + (num_entries) *
1378 sizeof(struct dentry_t);
1379 pr_debug("%s: trying to kmalloc %zx bytes for %d entries\n", __func__,
1380 bufsize, num_entries);
1381 es = kmalloc(bufsize, GFP_KERNEL);
1385 es->num_entries = num_entries;
1388 es->alloc_flag = p_dir->flags;
1390 pos = (struct dentry_t *)&es->__buf;
1392 while (num_entries) {
1394 * instead of copying whole sector, we will check every entry.
1395 * this will provide minimum stablity and consistency.
1397 entry_type = p_fs->fs_func->get_entry_type(ep);
1399 if ((entry_type == TYPE_UNUSED) || (entry_type == TYPE_DELETED))
1403 case ES_MODE_STARTED:
1404 if ((entry_type == TYPE_FILE) || (entry_type == TYPE_DIR))
1405 mode = ES_MODE_GET_FILE_ENTRY;
1409 case ES_MODE_GET_FILE_ENTRY:
1410 if (entry_type == TYPE_STREAM)
1411 mode = ES_MODE_GET_STRM_ENTRY;
1415 case ES_MODE_GET_STRM_ENTRY:
1416 if (entry_type == TYPE_EXTEND)
1417 mode = ES_MODE_GET_NAME_ENTRY;
1421 case ES_MODE_GET_NAME_ENTRY:
1422 if (entry_type == TYPE_EXTEND)
1424 else if (entry_type == TYPE_STREAM)
1426 else if (entry_type & TYPE_CRITICAL_SEC)
1427 mode = ES_MODE_GET_CRITICAL_SEC_ENTRY;
1431 case ES_MODE_GET_CRITICAL_SEC_ENTRY:
1432 if ((entry_type == TYPE_EXTEND) ||
1433 (entry_type == TYPE_STREAM))
1435 else if ((entry_type & TYPE_CRITICAL_SEC) !=
1441 memcpy(pos, ep, sizeof(struct dentry_t));
1443 if (--num_entries == 0)
1446 if (((off + DENTRY_SIZE) & p_bd->sector_size_mask) <
1447 (off & p_bd->sector_size_mask)) {
1448 /* get the next sector */
1449 if (IS_LAST_SECTOR_IN_CLUSTER(sec)) {
1450 if (es->alloc_flag == 0x03) {
1453 if (exfat_fat_read(sb, clu, &clu) == -1)
1456 sec = START_SECTOR(clu);
1460 buf = exfat_buf_getblk(sb, sec);
1464 ep = (struct dentry_t *)(buf);
1473 *file_ep = (struct dentry_t *)&es->__buf;
1475 pr_debug("%s exiting es %p sec %llu offset %d flags %d, num_entries %u buf ptr %p\n",
1476 __func__, es, (unsigned long long)es->sector, es->offset,
1477 es->alloc_flag, es->num_entries, &es->__buf);
1480 pr_debug("%s exited NULL (es %p)\n", __func__, es);
1485 void release_entry_set(struct entry_set_cache_t *es)
1487 pr_debug("%s es=%p\n", __func__, es);
1491 /* search EMPTY CONTINUOUS "num_entries" entries */
1492 static s32 search_deleted_or_unused_entry(struct super_block *sb,
1493 struct chain_t *p_dir, s32 num_entries)
1495 int i, dentry, num_empty = 0;
1496 s32 dentries_per_clu;
1499 struct dentry_t *ep;
1500 struct fs_info_t *p_fs = &(EXFAT_SB(sb)->fs_info);
1502 if (p_dir->dir == CLUSTER_32(0)) /* FAT16 root_dir */
1503 dentries_per_clu = p_fs->dentries_in_root;
1505 dentries_per_clu = p_fs->dentries_per_clu;
1507 if (p_fs->hint_uentry.dir == p_dir->dir) {
1508 if (p_fs->hint_uentry.entry == -1)
1511 clu.dir = p_fs->hint_uentry.clu.dir;
1512 clu.size = p_fs->hint_uentry.clu.size;
1513 clu.flags = p_fs->hint_uentry.clu.flags;
1515 dentry = p_fs->hint_uentry.entry;
1517 p_fs->hint_uentry.entry = -1;
1519 clu.dir = p_dir->dir;
1520 clu.size = p_dir->size;
1521 clu.flags = p_dir->flags;
1526 while (clu.dir != CLUSTER_32(~0)) {
1527 if (p_fs->dev_ejected)
1530 if (p_dir->dir == CLUSTER_32(0)) /* FAT16 root_dir */
1531 i = dentry % dentries_per_clu;
1533 i = dentry & (dentries_per_clu - 1);
1535 for (; i < dentries_per_clu; i++, dentry++) {
1536 ep = get_entry_in_dir(sb, &clu, i, NULL);
1540 type = p_fs->fs_func->get_entry_type(ep);
1542 if (type == TYPE_UNUSED) {
1544 if (p_fs->hint_uentry.entry == -1) {
1545 p_fs->hint_uentry.dir = p_dir->dir;
1546 p_fs->hint_uentry.entry = dentry;
1548 p_fs->hint_uentry.clu.dir = clu.dir;
1549 p_fs->hint_uentry.clu.size = clu.size;
1550 p_fs->hint_uentry.clu.flags = clu.flags;
1552 } else if (type == TYPE_DELETED) {
1558 if (num_empty >= num_entries) {
1559 p_fs->hint_uentry.dir = CLUSTER_32(~0);
1560 p_fs->hint_uentry.entry = -1;
1562 if (p_fs->vol_type == EXFAT)
1563 return dentry - (num_entries - 1);
1569 if (p_dir->dir == CLUSTER_32(0))
1570 break; /* FAT16 root_dir */
1572 if (clu.flags == 0x03) {
1573 if ((--clu.size) > 0)
1576 clu.dir = CLUSTER_32(~0);
1578 if (exfat_fat_read(sb, clu.dir, &clu.dir) != 0)
1586 static s32 find_empty_entry(struct inode *inode, struct chain_t *p_dir, s32 num_entries)
1593 struct dentry_t *ep = NULL;
1594 struct super_block *sb = inode->i_sb;
1595 struct fs_info_t *p_fs = &(EXFAT_SB(sb)->fs_info);
1596 struct file_id_t *fid = &(EXFAT_I(inode)->fid);
1598 if (p_dir->dir == CLUSTER_32(0)) /* FAT16 root_dir */
1599 return search_deleted_or_unused_entry(sb, p_dir, num_entries);
1601 while ((dentry = search_deleted_or_unused_entry(sb, p_dir, num_entries)) < 0) {
1602 if (p_fs->dev_ejected)
1605 if (p_dir->dir != p_fs->root_dir)
1606 size = i_size_read(inode);
1608 last_clu = find_last_cluster(sb, p_dir);
1609 clu.dir = last_clu + 1;
1611 clu.flags = p_dir->flags;
1613 /* (1) allocate a cluster */
1614 ret = p_fs->fs_func->alloc_cluster(sb, 1, &clu);
1618 if (clear_cluster(sb, clu.dir) != 0)
1621 /* (2) append to the FAT chain */
1622 if (clu.flags != p_dir->flags) {
1623 exfat_chain_cont_cluster(sb, p_dir->dir, p_dir->size);
1624 p_dir->flags = 0x01;
1625 p_fs->hint_uentry.clu.flags = 0x01;
1627 if (clu.flags == 0x01)
1628 if (exfat_fat_write(sb, last_clu, clu.dir) < 0)
1631 if (p_fs->hint_uentry.entry == -1) {
1632 p_fs->hint_uentry.dir = p_dir->dir;
1633 p_fs->hint_uentry.entry = p_dir->size << (p_fs->cluster_size_bits - DENTRY_SIZE_BITS);
1635 p_fs->hint_uentry.clu.dir = clu.dir;
1636 p_fs->hint_uentry.clu.size = 0;
1637 p_fs->hint_uentry.clu.flags = clu.flags;
1639 p_fs->hint_uentry.clu.size++;
1642 /* (3) update the directory entry */
1643 if (p_dir->dir != p_fs->root_dir) {
1644 size += p_fs->cluster_size;
1646 ep = get_entry_in_dir(sb, &fid->dir,
1647 fid->entry + 1, §or);
1650 p_fs->fs_func->set_entry_size(ep, size);
1651 p_fs->fs_func->set_entry_flag(ep, p_dir->flags);
1652 exfat_buf_modify(sb, sector);
1654 update_dir_checksum(sb, &fid->dir,
1658 i_size_write(inode, i_size_read(inode) + p_fs->cluster_size);
1659 EXFAT_I(inode)->mmu_private += p_fs->cluster_size;
1660 EXFAT_I(inode)->fid.size += p_fs->cluster_size;
1661 EXFAT_I(inode)->fid.flags = p_dir->flags;
1662 inode->i_blocks += 1 << (p_fs->cluster_size_bits - 9);
1668 static s32 extract_uni_name_from_name_entry(struct name_dentry_t *ep, u16 *uniname,
1673 for (i = 0; i < 30; i += 2) {
1674 *uniname = GET16_A(ep->unicode_0_14 + i);
1675 if (*uniname == 0x0)
1685 /* return values of exfat_find_dir_entry()
1686 * >= 0 : return dir entiry position with the name in dir
1687 * -1 : (root dir, ".") it is the root dir itself
1688 * -2 : entry with the name does not exist
1690 static s32 exfat_find_dir_entry(struct super_block *sb, struct chain_t *p_dir,
1691 struct uni_name_t *p_uniname, s32 num_entries,
1692 struct dos_name_t *p_dosname, u32 type)
1694 int i = 0, dentry = 0, num_ext_entries = 0, len, step;
1696 bool is_feasible_entry = false;
1697 s32 dentries_per_clu, num_empty = 0;
1699 u16 entry_uniname[16], *uniname = NULL, unichar;
1701 struct dentry_t *ep;
1702 struct file_dentry_t *file_ep;
1703 struct strm_dentry_t *strm_ep;
1704 struct name_dentry_t *name_ep;
1705 struct fs_info_t *p_fs = &(EXFAT_SB(sb)->fs_info);
1707 if (p_dir->dir == p_fs->root_dir) {
1708 if ((!nls_uniname_cmp(sb, p_uniname->name,
1709 (u16 *)UNI_CUR_DIR_NAME)) ||
1710 (!nls_uniname_cmp(sb, p_uniname->name,
1711 (u16 *)UNI_PAR_DIR_NAME)))
1712 return -1; // special case, root directory itself
1715 if (p_dir->dir == CLUSTER_32(0)) /* FAT16 root_dir */
1716 dentries_per_clu = p_fs->dentries_in_root;
1718 dentries_per_clu = p_fs->dentries_per_clu;
1720 clu.dir = p_dir->dir;
1721 clu.size = p_dir->size;
1722 clu.flags = p_dir->flags;
1724 p_fs->hint_uentry.dir = p_dir->dir;
1725 p_fs->hint_uentry.entry = -1;
1727 while (clu.dir != CLUSTER_32(~0)) {
1728 if (p_fs->dev_ejected)
1731 while (i < dentries_per_clu) {
1732 ep = get_entry_in_dir(sb, &clu, i, NULL);
1736 entry_type = p_fs->fs_func->get_entry_type(ep);
1739 if ((entry_type == TYPE_UNUSED) || (entry_type == TYPE_DELETED)) {
1740 is_feasible_entry = false;
1742 if (p_fs->hint_uentry.entry == -1) {
1745 if (num_empty == 1) {
1746 p_fs->hint_uentry.clu.dir = clu.dir;
1747 p_fs->hint_uentry.clu.size = clu.size;
1748 p_fs->hint_uentry.clu.flags = clu.flags;
1750 if ((num_empty >= num_entries) || (entry_type == TYPE_UNUSED))
1751 p_fs->hint_uentry.entry = dentry - (num_empty - 1);
1754 if (entry_type == TYPE_UNUSED)
1759 if ((entry_type == TYPE_FILE) || (entry_type == TYPE_DIR)) {
1760 file_ep = (struct file_dentry_t *)ep;
1761 if ((type == TYPE_ALL) || (type == entry_type)) {
1762 num_ext_entries = file_ep->num_ext;
1763 is_feasible_entry = true;
1765 is_feasible_entry = false;
1766 step = file_ep->num_ext + 1;
1768 } else if (entry_type == TYPE_STREAM) {
1769 if (is_feasible_entry) {
1770 strm_ep = (struct strm_dentry_t *)ep;
1771 if (p_uniname->name_hash == GET16_A(strm_ep->name_hash) &&
1772 p_uniname->name_len == strm_ep->name_len) {
1775 is_feasible_entry = false;
1776 step = num_ext_entries;
1779 } else if (entry_type == TYPE_EXTEND) {
1780 if (is_feasible_entry) {
1781 name_ep = (struct name_dentry_t *)ep;
1784 uniname = p_uniname->name;
1788 len = extract_uni_name_from_name_entry(name_ep,
1789 entry_uniname, order);
1791 unichar = *(uniname + len);
1792 *(uniname + len) = 0x0;
1794 if (nls_uniname_cmp(sb, uniname, entry_uniname)) {
1795 is_feasible_entry = false;
1796 step = num_ext_entries - order + 1;
1797 } else if (order == num_ext_entries) {
1798 p_fs->hint_uentry.dir = CLUSTER_32(~0);
1799 p_fs->hint_uentry.entry = -1;
1800 return dentry - (num_ext_entries);
1803 *(uniname + len) = unichar;
1806 is_feasible_entry = false;
1814 i -= dentries_per_clu;
1816 if (p_dir->dir == CLUSTER_32(0))
1817 break; /* FAT16 root_dir */
1819 if (clu.flags == 0x03) {
1820 if ((--clu.size) > 0)
1823 clu.dir = CLUSTER_32(~0);
1825 if (exfat_fat_read(sb, clu.dir, &clu.dir) != 0)
1833 static s32 exfat_count_ext_entries(struct super_block *sb, struct chain_t *p_dir,
1834 s32 entry, struct dentry_t *p_entry)
1838 struct file_dentry_t *file_ep = (struct file_dentry_t *)p_entry;
1839 struct dentry_t *ext_ep;
1840 struct fs_info_t *p_fs = &(EXFAT_SB(sb)->fs_info);
1842 for (i = 0, entry++; i < file_ep->num_ext; i++, entry++) {
1843 ext_ep = get_entry_in_dir(sb, p_dir, entry, NULL);
1847 type = p_fs->fs_func->get_entry_type(ext_ep);
1848 if ((type == TYPE_EXTEND) || (type == TYPE_STREAM))
1857 s32 count_dos_name_entries(struct super_block *sb, struct chain_t *p_dir,
1861 s32 dentries_per_clu;
1864 struct dentry_t *ep;
1865 struct fs_info_t *p_fs = &(EXFAT_SB(sb)->fs_info);
1867 if (p_dir->dir == CLUSTER_32(0)) /* FAT16 root_dir */
1868 dentries_per_clu = p_fs->dentries_in_root;
1870 dentries_per_clu = p_fs->dentries_per_clu;
1872 clu.dir = p_dir->dir;
1873 clu.size = p_dir->size;
1874 clu.flags = p_dir->flags;
1876 while (clu.dir != CLUSTER_32(~0)) {
1877 if (p_fs->dev_ejected)
1880 for (i = 0; i < dentries_per_clu; i++) {
1881 ep = get_entry_in_dir(sb, &clu, i, NULL);
1885 entry_type = p_fs->fs_func->get_entry_type(ep);
1887 if (entry_type == TYPE_UNUSED)
1889 if (!(type & TYPE_CRITICAL_PRI) &&
1890 !(type & TYPE_BENIGN_PRI))
1893 if ((type == TYPE_ALL) || (type == entry_type))
1897 if (p_dir->dir == CLUSTER_32(0))
1898 break; /* FAT16 root_dir */
1900 if (clu.flags == 0x03) {
1901 if ((--clu.size) > 0)
1904 clu.dir = CLUSTER_32(~0);
1906 if (exfat_fat_read(sb, clu.dir, &clu.dir) != 0)
1914 bool is_dir_empty(struct super_block *sb, struct chain_t *p_dir)
1917 s32 dentries_per_clu;
1920 struct dentry_t *ep;
1921 struct fs_info_t *p_fs = &(EXFAT_SB(sb)->fs_info);
1923 if (p_dir->dir == CLUSTER_32(0)) /* FAT16 root_dir */
1924 dentries_per_clu = p_fs->dentries_in_root;
1926 dentries_per_clu = p_fs->dentries_per_clu;
1928 clu.dir = p_dir->dir;
1929 clu.size = p_dir->size;
1930 clu.flags = p_dir->flags;
1932 while (clu.dir != CLUSTER_32(~0)) {
1933 if (p_fs->dev_ejected)
1936 for (i = 0; i < dentries_per_clu; i++) {
1937 ep = get_entry_in_dir(sb, &clu, i, NULL);
1941 type = p_fs->fs_func->get_entry_type(ep);
1943 if (type == TYPE_UNUSED)
1945 if ((type != TYPE_FILE) && (type != TYPE_DIR))
1948 if (p_dir->dir == CLUSTER_32(0)) /* FAT16 root_dir */
1951 if (p_fs->vol_type == EXFAT)
1953 if ((p_dir->dir == p_fs->root_dir) || ((++count) > 2))
1957 if (p_dir->dir == CLUSTER_32(0))
1958 break; /* FAT16 root_dir */
1960 if (clu.flags == 0x03) {
1961 if ((--clu.size) > 0)
1964 clu.dir = CLUSTER_32(~0);
1966 if (exfat_fat_read(sb, clu.dir, &clu.dir) != 0)
1974 * Name Conversion Functions
1977 /* input : dir, uni_name
1978 * output : num_of_entry, dos_name(format : aaaaaa~1.bbb)
1980 s32 get_num_entries_and_dos_name(struct super_block *sb, struct chain_t *p_dir,
1981 struct uni_name_t *p_uniname, s32 *entries,
1982 struct dos_name_t *p_dosname)
1985 struct fs_info_t *p_fs = &(EXFAT_SB(sb)->fs_info);
1987 num_entries = p_fs->fs_func->calc_num_entries(p_uniname);
1988 if (num_entries == 0)
1991 *entries = num_entries;
1996 static void exfat_get_uni_name_from_ext_entry(struct super_block *sb,
1997 struct chain_t *p_dir, s32 entry,
2001 struct dentry_t *ep;
2002 struct entry_set_cache_t *es;
2003 struct fs_info_t *p_fs = &(EXFAT_SB(sb)->fs_info);
2005 es = get_entry_set_in_dir(sb, p_dir, entry, ES_ALL_ENTRIES, &ep);
2006 if (!es || es->num_entries < 3) {
2008 release_entry_set(es);
2015 * First entry : file entry
2016 * Second entry : stream-extension entry
2017 * Third entry : first file-name entry
2018 * So, the index of first file-name dentry should start from 2.
2020 for (i = 2; i < es->num_entries; i++, ep++) {
2021 if (p_fs->fs_func->get_entry_type(ep) == TYPE_EXTEND)
2022 extract_uni_name_from_name_entry((struct name_dentry_t *)
2030 release_entry_set(es);
2033 static s32 exfat_calc_num_entries(struct uni_name_t *p_uniname)
2037 len = p_uniname->name_len;
2041 /* 1 file entry + 1 stream entry + name entries */
2042 return (len - 1) / 15 + 3;
2045 u16 calc_checksum_2byte(void *data, s32 len, u16 chksum, s32 type)
2052 for (i = 0; i < len; i++, c++) {
2053 if ((i == 2) || (i == 3))
2055 chksum = (((chksum & 1) << 15) |
2056 ((chksum & 0xFFFE) >> 1)) + (u16)*c;
2061 for (i = 0; i < len; i++, c++)
2062 chksum = (((chksum & 1) << 15) |
2063 ((chksum & 0xFFFE) >> 1)) + (u16)*c;
2070 * Name Resolution Functions
2073 /* return values of resolve_path()
2074 * > 0 : return the length of the path
2075 * < 0 : return error
2077 s32 resolve_path(struct inode *inode, char *path, struct chain_t *p_dir,
2078 struct uni_name_t *p_uniname)
2081 struct super_block *sb = inode->i_sb;
2082 struct fs_info_t *p_fs = &(EXFAT_SB(sb)->fs_info);
2083 struct file_id_t *fid = &(EXFAT_I(inode)->fid);
2085 if (strscpy(name_buf, path, sizeof(name_buf)) < 0)
2088 nls_cstring_to_uniname(sb, p_uniname, name_buf, &lossy);
2092 fid->size = i_size_read(inode);
2094 p_dir->dir = fid->start_clu;
2095 p_dir->size = (s32)(fid->size >> p_fs->cluster_size_bits);
2096 p_dir->flags = fid->flags;
2102 * File Operation Functions
2104 static struct fs_func exfat_fs_func = {
2105 .alloc_cluster = exfat_alloc_cluster,
2106 .free_cluster = exfat_free_cluster,
2107 .count_used_clusters = exfat_count_used_clusters,
2109 .init_dir_entry = exfat_init_dir_entry,
2110 .init_ext_entry = exfat_init_ext_entry,
2111 .find_dir_entry = exfat_find_dir_entry,
2112 .delete_dir_entry = exfat_delete_dir_entry,
2113 .get_uni_name_from_ext_entry = exfat_get_uni_name_from_ext_entry,
2114 .count_ext_entries = exfat_count_ext_entries,
2115 .calc_num_entries = exfat_calc_num_entries,
2117 .get_entry_type = exfat_get_entry_type,
2118 .set_entry_type = exfat_set_entry_type,
2119 .get_entry_attr = exfat_get_entry_attr,
2120 .set_entry_attr = exfat_set_entry_attr,
2121 .get_entry_flag = exfat_get_entry_flag,
2122 .set_entry_flag = exfat_set_entry_flag,
2123 .get_entry_clu0 = exfat_get_entry_clu0,
2124 .set_entry_clu0 = exfat_set_entry_clu0,
2125 .get_entry_size = exfat_get_entry_size,
2126 .set_entry_size = exfat_set_entry_size,
2127 .get_entry_time = exfat_get_entry_time,
2128 .set_entry_time = exfat_set_entry_time,
2131 s32 exfat_mount(struct super_block *sb, struct pbr_sector_t *p_pbr)
2133 struct bpbex_t *p_bpb = (struct bpbex_t *)p_pbr->bpb;
2134 struct fs_info_t *p_fs = &(EXFAT_SB(sb)->fs_info);
2135 struct bd_info_t *p_bd = &(EXFAT_SB(sb)->bd_info);
2137 if (p_bpb->num_fats == 0)
2138 return -EFSCORRUPTED;
2140 p_fs->sectors_per_clu = 1 << p_bpb->sectors_per_clu_bits;
2141 p_fs->sectors_per_clu_bits = p_bpb->sectors_per_clu_bits;
2142 p_fs->cluster_size_bits = p_fs->sectors_per_clu_bits +
2143 p_bd->sector_size_bits;
2144 p_fs->cluster_size = 1 << p_fs->cluster_size_bits;
2146 p_fs->num_FAT_sectors = GET32(p_bpb->fat_length);
2148 p_fs->FAT1_start_sector = p_fs->PBR_sector + GET32(p_bpb->fat_offset);
2149 if (p_bpb->num_fats == 1)
2150 p_fs->FAT2_start_sector = p_fs->FAT1_start_sector;
2152 p_fs->FAT2_start_sector = p_fs->FAT1_start_sector +
2153 p_fs->num_FAT_sectors;
2155 p_fs->root_start_sector = p_fs->PBR_sector + GET32(p_bpb->clu_offset);
2156 p_fs->data_start_sector = p_fs->root_start_sector;
2158 p_fs->num_sectors = GET64(p_bpb->vol_length);
2159 p_fs->num_clusters = GET32(p_bpb->clu_count) + 2;
2160 /* because the cluster index starts with 2 */
2162 p_fs->vol_type = EXFAT;
2163 p_fs->vol_id = GET32(p_bpb->vol_serial);
2165 p_fs->root_dir = GET32(p_bpb->root_cluster);
2166 p_fs->dentries_in_root = 0;
2167 p_fs->dentries_per_clu = 1 << (p_fs->cluster_size_bits -
2170 p_fs->vol_flag = (u32)GET16(p_bpb->vol_flags);
2171 p_fs->clu_srch_ptr = 2;
2172 p_fs->used_clusters = UINT_MAX;
2174 p_fs->fs_func = &exfat_fs_func;
2179 s32 create_dir(struct inode *inode, struct chain_t *p_dir,
2180 struct uni_name_t *p_uniname, struct file_id_t *fid)
2182 s32 ret, dentry, num_entries;
2185 struct dos_name_t dos_name;
2186 struct super_block *sb = inode->i_sb;
2187 struct fs_info_t *p_fs = &(EXFAT_SB(sb)->fs_info);
2188 struct fs_func *fs_func = p_fs->fs_func;
2190 ret = get_num_entries_and_dos_name(sb, p_dir, p_uniname, &num_entries,
2195 /* find_empty_entry must be called before alloc_cluster */
2196 dentry = find_empty_entry(inode, p_dir, num_entries);
2200 clu.dir = CLUSTER_32(~0);
2202 clu.flags = (p_fs->vol_type == EXFAT) ? 0x03 : 0x01;
2204 /* (1) allocate a cluster */
2205 ret = fs_func->alloc_cluster(sb, 1, &clu);
2211 ret = clear_cluster(sb, clu.dir);
2215 size = p_fs->cluster_size;
2217 /* (2) update the directory entry */
2218 /* make sub-dir entry in parent directory */
2219 ret = fs_func->init_dir_entry(sb, p_dir, dentry, TYPE_DIR, clu.dir,
2224 ret = fs_func->init_ext_entry(sb, p_dir, dentry, num_entries, p_uniname,
2229 fid->dir.dir = p_dir->dir;
2230 fid->dir.size = p_dir->size;
2231 fid->dir.flags = p_dir->flags;
2232 fid->entry = dentry;
2234 fid->attr = ATTR_SUBDIR;
2235 fid->flags = (p_fs->vol_type == EXFAT) ? 0x03 : 0x01;
2237 fid->start_clu = clu.dir;
2239 fid->type = TYPE_DIR;
2241 fid->hint_last_off = -1;
2246 s32 create_file(struct inode *inode, struct chain_t *p_dir,
2247 struct uni_name_t *p_uniname, u8 mode, struct file_id_t *fid)
2249 s32 ret, dentry, num_entries;
2250 struct dos_name_t dos_name;
2251 struct super_block *sb = inode->i_sb;
2252 struct fs_info_t *p_fs = &(EXFAT_SB(sb)->fs_info);
2253 struct fs_func *fs_func = p_fs->fs_func;
2255 ret = get_num_entries_and_dos_name(sb, p_dir, p_uniname, &num_entries,
2260 /* find_empty_entry must be called before alloc_cluster() */
2261 dentry = find_empty_entry(inode, p_dir, num_entries);
2265 /* (1) update the directory entry */
2266 /* fill the dos name directory entry information of the created file.
2267 * the first cluster is not determined yet. (0)
2269 ret = fs_func->init_dir_entry(sb, p_dir, dentry, TYPE_FILE | mode,
2274 ret = fs_func->init_ext_entry(sb, p_dir, dentry, num_entries, p_uniname,
2279 fid->dir.dir = p_dir->dir;
2280 fid->dir.size = p_dir->size;
2281 fid->dir.flags = p_dir->flags;
2282 fid->entry = dentry;
2284 fid->attr = ATTR_ARCHIVE | mode;
2285 fid->flags = (p_fs->vol_type == EXFAT) ? 0x03 : 0x01;
2287 fid->start_clu = CLUSTER_32(~0);
2289 fid->type = TYPE_FILE;
2291 fid->hint_last_off = -1;
2296 void remove_file(struct inode *inode, struct chain_t *p_dir, s32 entry)
2300 struct dentry_t *ep;
2301 struct super_block *sb = inode->i_sb;
2302 struct fs_info_t *p_fs = &(EXFAT_SB(sb)->fs_info);
2303 struct fs_func *fs_func = p_fs->fs_func;
2305 ep = get_entry_in_dir(sb, p_dir, entry, §or);
2309 exfat_buf_lock(sb, sector);
2311 /* exfat_buf_lock() before call count_ext_entries() */
2312 num_entries = fs_func->count_ext_entries(sb, p_dir, entry, ep);
2313 if (num_entries < 0) {
2314 exfat_buf_unlock(sb, sector);
2319 exfat_buf_unlock(sb, sector);
2321 /* (1) update the directory entry */
2322 fs_func->delete_dir_entry(sb, p_dir, entry, 0, num_entries);
2325 s32 rename_file(struct inode *inode, struct chain_t *p_dir, s32 oldentry,
2326 struct uni_name_t *p_uniname, struct file_id_t *fid)
2328 s32 ret, newentry = -1, num_old_entries, num_new_entries;
2329 sector_t sector_old, sector_new;
2330 struct dos_name_t dos_name;
2331 struct dentry_t *epold, *epnew;
2332 struct super_block *sb = inode->i_sb;
2333 struct fs_info_t *p_fs = &(EXFAT_SB(sb)->fs_info);
2334 struct fs_func *fs_func = p_fs->fs_func;
2336 epold = get_entry_in_dir(sb, p_dir, oldentry, §or_old);
2340 exfat_buf_lock(sb, sector_old);
2342 /* exfat_buf_lock() before call count_ext_entries() */
2343 num_old_entries = fs_func->count_ext_entries(sb, p_dir, oldentry,
2345 if (num_old_entries < 0) {
2346 exfat_buf_unlock(sb, sector_old);
2351 ret = get_num_entries_and_dos_name(sb, p_dir, p_uniname,
2352 &num_new_entries, &dos_name);
2354 exfat_buf_unlock(sb, sector_old);
2358 if (num_old_entries < num_new_entries) {
2359 newentry = find_empty_entry(inode, p_dir, num_new_entries);
2361 exfat_buf_unlock(sb, sector_old);
2365 epnew = get_entry_in_dir(sb, p_dir, newentry, §or_new);
2367 exfat_buf_unlock(sb, sector_old);
2371 memcpy((void *)epnew, (void *)epold, DENTRY_SIZE);
2372 if (fs_func->get_entry_type(epnew) == TYPE_FILE) {
2373 fs_func->set_entry_attr(epnew,
2374 fs_func->get_entry_attr(epnew) |
2376 fid->attr |= ATTR_ARCHIVE;
2378 exfat_buf_modify(sb, sector_new);
2379 exfat_buf_unlock(sb, sector_old);
2381 epold = get_entry_in_dir(sb, p_dir, oldentry + 1,
2383 exfat_buf_lock(sb, sector_old);
2384 epnew = get_entry_in_dir(sb, p_dir, newentry + 1,
2387 if (!epold || !epnew) {
2388 exfat_buf_unlock(sb, sector_old);
2392 memcpy((void *)epnew, (void *)epold, DENTRY_SIZE);
2393 exfat_buf_modify(sb, sector_new);
2394 exfat_buf_unlock(sb, sector_old);
2396 ret = fs_func->init_ext_entry(sb, p_dir, newentry,
2397 num_new_entries, p_uniname,
2402 fs_func->delete_dir_entry(sb, p_dir, oldentry, 0,
2404 fid->entry = newentry;
2406 if (fs_func->get_entry_type(epold) == TYPE_FILE) {
2407 fs_func->set_entry_attr(epold,
2408 fs_func->get_entry_attr(epold) |
2410 fid->attr |= ATTR_ARCHIVE;
2412 exfat_buf_modify(sb, sector_old);
2413 exfat_buf_unlock(sb, sector_old);
2415 ret = fs_func->init_ext_entry(sb, p_dir, oldentry,
2416 num_new_entries, p_uniname,
2421 fs_func->delete_dir_entry(sb, p_dir, oldentry, num_new_entries,
2428 s32 move_file(struct inode *inode, struct chain_t *p_olddir, s32 oldentry,
2429 struct chain_t *p_newdir, struct uni_name_t *p_uniname,
2430 struct file_id_t *fid)
2432 s32 ret, newentry, num_new_entries, num_old_entries;
2433 sector_t sector_mov, sector_new;
2434 struct dos_name_t dos_name;
2435 struct dentry_t *epmov, *epnew;
2436 struct super_block *sb = inode->i_sb;
2437 struct fs_info_t *p_fs = &(EXFAT_SB(sb)->fs_info);
2438 struct fs_func *fs_func = p_fs->fs_func;
2440 epmov = get_entry_in_dir(sb, p_olddir, oldentry, §or_mov);
2444 /* check if the source and target directory is the same */
2445 if (fs_func->get_entry_type(epmov) == TYPE_DIR &&
2446 fs_func->get_entry_clu0(epmov) == p_newdir->dir)
2449 exfat_buf_lock(sb, sector_mov);
2451 /* exfat_buf_lock() before call count_ext_entries() */
2452 num_old_entries = fs_func->count_ext_entries(sb, p_olddir, oldentry,
2454 if (num_old_entries < 0) {
2455 exfat_buf_unlock(sb, sector_mov);
2460 ret = get_num_entries_and_dos_name(sb, p_newdir, p_uniname,
2461 &num_new_entries, &dos_name);
2463 exfat_buf_unlock(sb, sector_mov);
2467 newentry = find_empty_entry(inode, p_newdir, num_new_entries);
2469 exfat_buf_unlock(sb, sector_mov);
2473 epnew = get_entry_in_dir(sb, p_newdir, newentry, §or_new);
2475 exfat_buf_unlock(sb, sector_mov);
2479 memcpy((void *)epnew, (void *)epmov, DENTRY_SIZE);
2480 if (fs_func->get_entry_type(epnew) == TYPE_FILE) {
2481 fs_func->set_entry_attr(epnew, fs_func->get_entry_attr(epnew) |
2483 fid->attr |= ATTR_ARCHIVE;
2485 exfat_buf_modify(sb, sector_new);
2486 exfat_buf_unlock(sb, sector_mov);
2488 epmov = get_entry_in_dir(sb, p_olddir, oldentry + 1,
2490 exfat_buf_lock(sb, sector_mov);
2491 epnew = get_entry_in_dir(sb, p_newdir, newentry + 1,
2493 if (!epmov || !epnew) {
2494 exfat_buf_unlock(sb, sector_mov);
2498 memcpy((void *)epnew, (void *)epmov, DENTRY_SIZE);
2499 exfat_buf_modify(sb, sector_new);
2500 exfat_buf_unlock(sb, sector_mov);
2502 ret = fs_func->init_ext_entry(sb, p_newdir, newentry, num_new_entries,
2503 p_uniname, &dos_name);
2507 fs_func->delete_dir_entry(sb, p_olddir, oldentry, 0, num_old_entries);
2509 fid->dir.dir = p_newdir->dir;
2510 fid->dir.size = p_newdir->size;
2511 fid->dir.flags = p_newdir->flags;
2513 fid->entry = newentry;
2519 * Sector Read/Write Functions
2522 int sector_read(struct super_block *sb, sector_t sec, struct buffer_head **bh,
2526 struct fs_info_t *p_fs = &(EXFAT_SB(sb)->fs_info);
2528 if ((sec >= (p_fs->PBR_sector + p_fs->num_sectors)) &&
2529 (p_fs->num_sectors > 0)) {
2530 pr_err("[EXFAT] %s: out of range error! (sec = %llu)\n",
2531 __func__, (unsigned long long)sec);
2536 if (!p_fs->dev_ejected) {
2537 ret = exfat_bdev_read(sb, sec, bh, 1, read);
2539 p_fs->dev_ejected = 1;
2545 int sector_write(struct super_block *sb, sector_t sec, struct buffer_head *bh,
2549 struct fs_info_t *p_fs = &(EXFAT_SB(sb)->fs_info);
2551 if (sec >= (p_fs->PBR_sector + p_fs->num_sectors) &&
2552 (p_fs->num_sectors > 0)) {
2553 pr_err("[EXFAT] %s: out of range error! (sec = %llu)\n",
2554 __func__, (unsigned long long)sec);
2560 pr_err("[EXFAT] %s: bh is NULL!\n", __func__);
2565 if (!p_fs->dev_ejected) {
2566 ret = exfat_bdev_write(sb, sec, bh, 1, sync);
2568 p_fs->dev_ejected = 1;
2574 int multi_sector_read(struct super_block *sb, sector_t sec,
2575 struct buffer_head **bh, s32 num_secs, bool read)
2578 struct fs_info_t *p_fs = &(EXFAT_SB(sb)->fs_info);
2580 if (((sec + num_secs) > (p_fs->PBR_sector + p_fs->num_sectors)) &&
2581 (p_fs->num_sectors > 0)) {
2582 pr_err("[EXFAT] %s: out of range error! (sec = %llu, num_secs = %d)\n",
2583 __func__, (unsigned long long)sec, num_secs);
2588 if (!p_fs->dev_ejected) {
2589 ret = exfat_bdev_read(sb, sec, bh, num_secs, read);
2591 p_fs->dev_ejected = 1;
2597 int multi_sector_write(struct super_block *sb, sector_t sec,
2598 struct buffer_head *bh, s32 num_secs, bool sync)
2601 struct fs_info_t *p_fs = &(EXFAT_SB(sb)->fs_info);
2603 if ((sec + num_secs) > (p_fs->PBR_sector + p_fs->num_sectors) &&
2604 (p_fs->num_sectors > 0)) {
2605 pr_err("[EXFAT] %s: out of range error! (sec = %llu, num_secs = %d)\n",
2606 __func__, (unsigned long long)sec, num_secs);
2611 pr_err("[EXFAT] %s: bh is NULL!\n", __func__);
2616 if (!p_fs->dev_ejected) {
2617 ret = exfat_bdev_write(sb, sec, bh, num_secs, sync);
2619 p_fs->dev_ejected = 1;