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 char *reserved_names[] = {
24 "AUX ", "CON ", "NUL ", "PRN ",
25 "COM1 ", "COM2 ", "COM3 ", "COM4 ",
26 "COM5 ", "COM6 ", "COM7 ", "COM8 ", "COM9 ",
27 "LPT1 ", "LPT2 ", "LPT3 ", "LPT4 ",
28 "LPT5 ", "LPT6 ", "LPT7 ", "LPT8 ", "LPT9 ",
32 static u8 free_bit[] = {
33 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, 4, 0, 1, 0, 2, /* 0 ~ 19 */
34 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, 5, 0, 1, 0, 2, 0, 1, 0, 3, /* 20 ~ 39 */
35 0, 1, 0, 2, 0, 1, 0, 4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, /* 40 ~ 59 */
36 0, 1, 0, 6, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, 4, /* 60 ~ 79 */
37 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, 5, 0, 1, 0, 2, /* 80 ~ 99 */
38 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, 4, 0, 1, 0, 2, 0, 1, 0, 3, /* 100 ~ 119 */
39 0, 1, 0, 2, 0, 1, 0, 7, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, /* 120 ~ 139 */
40 0, 1, 0, 4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, 5, /* 140 ~ 159 */
41 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, 4, 0, 1, 0, 2, /* 160 ~ 179 */
42 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, 6, 0, 1, 0, 2, 0, 1, 0, 3, /* 180 ~ 199 */
43 0, 1, 0, 2, 0, 1, 0, 4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, /* 200 ~ 219 */
44 0, 1, 0, 5, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, 4, /* 220 ~ 239 */
45 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0 /* 240 ~ 254 */
48 static u8 used_bit[] = {
49 0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4, 1, 2, 2, 3, /* 0 ~ 19 */
50 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5, 1, 2, 2, 3, 2, 3, 3, 4, /* 20 ~ 39 */
51 2, 3, 3, 4, 3, 4, 4, 5, 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, /* 40 ~ 59 */
52 4, 5, 5, 6, 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5, /* 60 ~ 79 */
53 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6, 2, 3, 3, 4, /* 80 ~ 99 */
54 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6, 3, 4, 4, 5, 4, 5, 5, 6, /* 100 ~ 119 */
55 4, 5, 5, 6, 5, 6, 6, 7, 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, /* 120 ~ 139 */
56 3, 4, 4, 5, 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6, /* 140 ~ 159 */
57 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6, 3, 4, 4, 5, /* 160 ~ 179 */
58 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7, 2, 3, 3, 4, 3, 4, 4, 5, /* 180 ~ 199 */
59 3, 4, 4, 5, 4, 5, 5, 6, 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, /* 200 ~ 219 */
60 5, 6, 6, 7, 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7, /* 220 ~ 239 */
61 4, 5, 5, 6, 5, 6, 6, 7, 5, 6, 6, 7, 6, 7, 7, 8 /* 240 ~ 255 */
64 #define BITMAP_LOC(v) ((v) >> 3)
65 #define BITMAP_SHIFT(v) ((v) & 0x07)
67 static inline s32 exfat_bitmap_test(u8 *bitmap, int i)
71 data = bitmap[BITMAP_LOC(i)];
72 if ((data >> BITMAP_SHIFT(i)) & 0x01)
77 static inline void exfat_bitmap_set(u8 *bitmap, int i)
79 bitmap[BITMAP_LOC(i)] |= (0x01 << BITMAP_SHIFT(i));
82 static inline void exfat_bitmap_clear(u8 *bitmap, int i)
84 bitmap[BITMAP_LOC(i)] &= ~(0x01 << BITMAP_SHIFT(i));
88 * File System Management Functions
91 void fs_set_vol_flags(struct super_block *sb, u32 new_flag)
93 struct pbr_sector_t *p_pbr;
94 struct bpbex_t *p_bpb;
95 struct fs_info_t *p_fs = &(EXFAT_SB(sb)->fs_info);
97 if (p_fs->vol_flag == new_flag)
100 p_fs->vol_flag = new_flag;
102 if (p_fs->vol_type == EXFAT) {
104 if (sector_read(sb, p_fs->PBR_sector,
105 &p_fs->pbr_bh, 1) != FFS_SUCCESS)
109 p_pbr = (struct pbr_sector_t *)p_fs->pbr_bh->b_data;
110 p_bpb = (struct bpbex_t *)p_pbr->bpb;
111 SET16(p_bpb->vol_flags, (u16)new_flag);
114 * what can we do here? (cuz fs_set_vol_flags() is void)
116 if ((new_flag == VOL_DIRTY) && (!buffer_dirty(p_fs->pbr_bh)))
117 sector_write(sb, p_fs->PBR_sector, p_fs->pbr_bh, 1);
119 sector_write(sb, p_fs->PBR_sector, p_fs->pbr_bh, 0);
123 void fs_error(struct super_block *sb)
125 struct exfat_mount_options *opts = &EXFAT_SB(sb)->options;
127 if (opts->errors == EXFAT_ERRORS_PANIC) {
128 panic("[EXFAT] Filesystem panic from previous error\n");
129 } else if ((opts->errors == EXFAT_ERRORS_RO) && !sb_rdonly(sb)) {
130 sb->s_flags |= SB_RDONLY;
131 pr_err("[EXFAT] Filesystem has been set read-only\n");
136 * Cluster Management Functions
139 s32 clear_cluster(struct super_block *sb, u32 clu)
142 s32 ret = FFS_SUCCESS;
143 struct buffer_head *tmp_bh = NULL;
144 struct fs_info_t *p_fs = &(EXFAT_SB(sb)->fs_info);
145 struct bd_info_t *p_bd = &(EXFAT_SB(sb)->bd_info);
147 if (clu == CLUSTER_32(0)) { /* FAT16 root_dir */
148 s = p_fs->root_start_sector;
149 n = p_fs->data_start_sector;
151 s = START_SECTOR(clu);
152 n = s + p_fs->sectors_per_clu;
156 ret = sector_read(sb, s, &tmp_bh, 0);
157 if (ret != FFS_SUCCESS)
160 memset((char *)tmp_bh->b_data, 0x0, p_bd->sector_size);
161 ret = sector_write(sb, s, tmp_bh, 0);
162 if (ret != FFS_SUCCESS)
170 s32 fat_alloc_cluster(struct super_block *sb, s32 num_alloc,
171 struct chain_t *p_chain)
173 int i, num_clusters = 0;
174 u32 new_clu, last_clu = CLUSTER_32(~0), read_clu;
175 struct fs_info_t *p_fs = &(EXFAT_SB(sb)->fs_info);
177 new_clu = p_chain->dir;
178 if (new_clu == CLUSTER_32(~0))
179 new_clu = p_fs->clu_srch_ptr;
180 else if (new_clu >= p_fs->num_clusters)
185 p_chain->dir = CLUSTER_32(~0);
187 for (i = 2; i < p_fs->num_clusters; i++) {
188 if (FAT_read(sb, new_clu, &read_clu) != 0)
191 if (read_clu == CLUSTER_32(0)) {
192 if (FAT_write(sb, new_clu, CLUSTER_32(~0)) < 0)
196 if (p_chain->dir == CLUSTER_32(~0)) {
197 p_chain->dir = new_clu;
199 if (FAT_write(sb, last_clu, new_clu) < 0)
205 if ((--num_alloc) == 0) {
206 p_fs->clu_srch_ptr = new_clu;
207 if (p_fs->used_clusters != UINT_MAX)
208 p_fs->used_clusters += num_clusters;
213 if ((++new_clu) >= p_fs->num_clusters)
217 p_fs->clu_srch_ptr = new_clu;
218 if (p_fs->used_clusters != UINT_MAX)
219 p_fs->used_clusters += num_clusters;
224 s32 exfat_alloc_cluster(struct super_block *sb, s32 num_alloc,
225 struct chain_t *p_chain)
227 s32 num_clusters = 0;
228 u32 hint_clu, new_clu, last_clu = CLUSTER_32(~0);
229 struct fs_info_t *p_fs = &(EXFAT_SB(sb)->fs_info);
231 hint_clu = p_chain->dir;
232 if (hint_clu == CLUSTER_32(~0)) {
233 hint_clu = test_alloc_bitmap(sb, p_fs->clu_srch_ptr - 2);
234 if (hint_clu == CLUSTER_32(~0))
236 } else if (hint_clu >= p_fs->num_clusters) {
238 p_chain->flags = 0x01;
243 p_chain->dir = CLUSTER_32(~0);
245 while ((new_clu = test_alloc_bitmap(sb, hint_clu - 2)) != CLUSTER_32(~0)) {
246 if (new_clu != hint_clu) {
247 if (p_chain->flags == 0x03) {
248 exfat_chain_cont_cluster(sb, p_chain->dir,
250 p_chain->flags = 0x01;
254 if (set_alloc_bitmap(sb, new_clu - 2) != FFS_SUCCESS)
259 if (p_chain->flags == 0x01) {
260 if (FAT_write(sb, new_clu, CLUSTER_32(~0)) < 0)
264 if (p_chain->dir == CLUSTER_32(~0)) {
265 p_chain->dir = new_clu;
267 if (p_chain->flags == 0x01) {
268 if (FAT_write(sb, last_clu, new_clu) < 0)
274 if ((--num_alloc) == 0) {
275 p_fs->clu_srch_ptr = hint_clu;
276 if (p_fs->used_clusters != UINT_MAX)
277 p_fs->used_clusters += num_clusters;
279 p_chain->size += num_clusters;
283 hint_clu = new_clu + 1;
284 if (hint_clu >= p_fs->num_clusters) {
287 if (p_chain->flags == 0x03) {
288 exfat_chain_cont_cluster(sb, p_chain->dir,
290 p_chain->flags = 0x01;
295 p_fs->clu_srch_ptr = hint_clu;
296 if (p_fs->used_clusters != UINT_MAX)
297 p_fs->used_clusters += num_clusters;
299 p_chain->size += num_clusters;
303 void fat_free_cluster(struct super_block *sb, struct chain_t *p_chain,
306 s32 num_clusters = 0;
308 struct fs_info_t *p_fs = &(EXFAT_SB(sb)->fs_info);
312 if ((p_chain->dir == CLUSTER_32(0)) || (p_chain->dir == CLUSTER_32(~0)))
317 if (p_chain->size <= 0)
321 if (p_fs->dev_ejected)
325 sector = START_SECTOR(clu);
326 for (i = 0; i < p_fs->sectors_per_clu; i++)
327 buf_release(sb, sector + i);
331 if (FAT_read(sb, clu, &clu) == -1)
334 if (FAT_write(sb, prev, CLUSTER_32(0)) < 0)
338 } while (clu != CLUSTER_32(~0));
340 if (p_fs->used_clusters != UINT_MAX)
341 p_fs->used_clusters -= num_clusters;
344 void exfat_free_cluster(struct super_block *sb, struct chain_t *p_chain,
347 s32 num_clusters = 0;
349 struct fs_info_t *p_fs = &(EXFAT_SB(sb)->fs_info);
353 if ((p_chain->dir == CLUSTER_32(0)) || (p_chain->dir == CLUSTER_32(~0)))
356 if (p_chain->size <= 0) {
357 pr_err("[EXFAT] free_cluster : skip free-req clu:%u, because of zero-size truncation\n",
365 if (p_chain->flags == 0x03) {
368 sector = START_SECTOR(clu);
369 for (i = 0; i < p_fs->sectors_per_clu; i++)
370 buf_release(sb, sector + i);
373 if (clr_alloc_bitmap(sb, clu - 2) != FFS_SUCCESS)
378 } while (num_clusters < p_chain->size);
381 if (p_fs->dev_ejected)
385 sector = START_SECTOR(clu);
386 for (i = 0; i < p_fs->sectors_per_clu; i++)
387 buf_release(sb, sector + i);
390 if (clr_alloc_bitmap(sb, clu - 2) != FFS_SUCCESS)
393 if (FAT_read(sb, clu, &clu) == -1)
396 } while ((clu != CLUSTER_32(0)) && (clu != CLUSTER_32(~0)));
399 if (p_fs->used_clusters != UINT_MAX)
400 p_fs->used_clusters -= num_clusters;
403 u32 find_last_cluster(struct super_block *sb, struct chain_t *p_chain)
406 struct fs_info_t *p_fs = &(EXFAT_SB(sb)->fs_info);
410 if (p_chain->flags == 0x03) {
411 clu += p_chain->size - 1;
413 while ((FAT_read(sb, clu, &next) == 0) &&
414 (next != CLUSTER_32(~0))) {
415 if (p_fs->dev_ejected)
424 s32 count_num_clusters(struct super_block *sb, struct chain_t *p_chain)
428 struct fs_info_t *p_fs = &(EXFAT_SB(sb)->fs_info);
430 if ((p_chain->dir == CLUSTER_32(0)) || (p_chain->dir == CLUSTER_32(~0)))
435 if (p_chain->flags == 0x03) {
436 count = p_chain->size;
438 for (i = 2; i < p_fs->num_clusters; i++) {
440 if (FAT_read(sb, clu, &clu) != 0)
442 if (clu == CLUSTER_32(~0))
450 s32 fat_count_used_clusters(struct super_block *sb)
454 struct fs_info_t *p_fs = &(EXFAT_SB(sb)->fs_info);
456 for (i = 2; i < p_fs->num_clusters; i++) {
457 if (FAT_read(sb, i, &clu) != 0)
459 if (clu != CLUSTER_32(0))
466 s32 exfat_count_used_clusters(struct super_block *sb)
468 int i, map_i, map_b, count = 0;
470 struct fs_info_t *p_fs = &(EXFAT_SB(sb)->fs_info);
471 struct bd_info_t *p_bd = &(EXFAT_SB(sb)->bd_info);
475 for (i = 2; i < p_fs->num_clusters; i += 8) {
476 k = *(((u8 *)p_fs->vol_amap[map_i]->b_data) + map_b);
477 count += used_bit[k];
479 if ((++map_b) >= p_bd->sector_size) {
488 void exfat_chain_cont_cluster(struct super_block *sb, u32 chain, s32 len)
494 if (FAT_write(sb, chain, chain + 1) < 0)
499 FAT_write(sb, chain, CLUSTER_32(~0));
503 * Allocation Bitmap Management Functions
506 s32 load_alloc_bitmap(struct super_block *sb)
513 struct bmap_dentry_t *ep;
514 struct fs_info_t *p_fs = &(EXFAT_SB(sb)->fs_info);
515 struct bd_info_t *p_bd = &(EXFAT_SB(sb)->bd_info);
517 clu.dir = p_fs->root_dir;
520 while (clu.dir != CLUSTER_32(~0)) {
521 if (p_fs->dev_ejected)
524 for (i = 0; i < p_fs->dentries_per_clu; i++) {
525 ep = (struct bmap_dentry_t *)get_entry_in_dir(sb, &clu,
530 type = p_fs->fs_func->get_entry_type((struct dentry_t *)ep);
532 if (type == TYPE_UNUSED)
534 if (type != TYPE_BITMAP)
537 if (ep->flags == 0x0) {
538 p_fs->map_clu = GET32_A(ep->start_clu);
539 map_size = (u32)GET64_A(ep->size);
541 p_fs->map_sectors = ((map_size - 1) >> p_bd->sector_size_bits) + 1;
543 p_fs->vol_amap = kmalloc_array(p_fs->map_sectors,
544 sizeof(struct buffer_head *),
547 return FFS_MEMORYERR;
549 sector = START_SECTOR(p_fs->map_clu);
551 for (j = 0; j < p_fs->map_sectors; j++) {
552 p_fs->vol_amap[j] = NULL;
553 ret = sector_read(sb, sector + j, &(p_fs->vol_amap[j]), 1);
554 if (ret != FFS_SUCCESS) {
555 /* release all buffers and free vol_amap */
558 brelse(p_fs->vol_amap[i++]);
560 kfree(p_fs->vol_amap);
561 p_fs->vol_amap = NULL;
571 if (FAT_read(sb, clu.dir, &clu.dir) != 0)
575 return FFS_FORMATERR;
578 void free_alloc_bitmap(struct super_block *sb)
581 struct fs_info_t *p_fs = &(EXFAT_SB(sb)->fs_info);
583 brelse(p_fs->pbr_bh);
585 for (i = 0; i < p_fs->map_sectors; i++)
586 __brelse(p_fs->vol_amap[i]);
588 kfree(p_fs->vol_amap);
589 p_fs->vol_amap = NULL;
592 s32 set_alloc_bitmap(struct super_block *sb, u32 clu)
596 struct fs_info_t *p_fs = &(EXFAT_SB(sb)->fs_info);
597 struct bd_info_t *p_bd = &(EXFAT_SB(sb)->bd_info);
599 i = clu >> (p_bd->sector_size_bits + 3);
600 b = clu & ((p_bd->sector_size << 3) - 1);
602 sector = START_SECTOR(p_fs->map_clu) + i;
604 exfat_bitmap_set((u8 *)p_fs->vol_amap[i]->b_data, b);
606 return sector_write(sb, sector, p_fs->vol_amap[i], 0);
609 s32 clr_alloc_bitmap(struct super_block *sb, u32 clu)
613 #ifdef CONFIG_EXFAT_DISCARD
614 struct exfat_sb_info *sbi = EXFAT_SB(sb);
615 struct exfat_mount_options *opts = &sbi->options;
617 #endif /* CONFIG_EXFAT_DISCARD */
618 struct fs_info_t *p_fs = &(EXFAT_SB(sb)->fs_info);
619 struct bd_info_t *p_bd = &(EXFAT_SB(sb)->bd_info);
621 i = clu >> (p_bd->sector_size_bits + 3);
622 b = clu & ((p_bd->sector_size << 3) - 1);
624 sector = START_SECTOR(p_fs->map_clu) + i;
626 exfat_bitmap_clear((u8 *)p_fs->vol_amap[i]->b_data, b);
628 return sector_write(sb, sector, p_fs->vol_amap[i], 0);
630 #ifdef CONFIG_EXFAT_DISCARD
632 ret = sb_issue_discard(sb, START_SECTOR(clu),
633 (1 << p_fs->sectors_per_clu_bits),
635 if (ret == -EOPNOTSUPP) {
636 pr_warn("discard not supported by device, disabling");
640 #endif /* CONFIG_EXFAT_DISCARD */
643 u32 test_alloc_bitmap(struct super_block *sb, u32 clu)
646 u32 clu_base, clu_free;
648 struct fs_info_t *p_fs = &(EXFAT_SB(sb)->fs_info);
649 struct bd_info_t *p_bd = &(EXFAT_SB(sb)->bd_info);
651 clu_base = (clu & ~(0x7)) + 2;
652 clu_mask = (1 << (clu - clu_base + 2)) - 1;
654 map_i = clu >> (p_bd->sector_size_bits + 3);
655 map_b = (clu >> 3) & p_bd->sector_size_mask;
657 for (i = 2; i < p_fs->num_clusters; i += 8) {
658 k = *(((u8 *)p_fs->vol_amap[map_i]->b_data) + map_b);
664 clu_free = clu_base + free_bit[k];
665 if (clu_free < p_fs->num_clusters)
670 if (((++map_b) >= p_bd->sector_size) ||
671 (clu_base >= p_fs->num_clusters)) {
672 if ((++map_i) >= p_fs->map_sectors) {
680 return CLUSTER_32(~0);
683 void sync_alloc_bitmap(struct super_block *sb)
686 struct fs_info_t *p_fs = &(EXFAT_SB(sb)->fs_info);
691 for (i = 0; i < p_fs->map_sectors; i++)
692 sync_dirty_buffer(p_fs->vol_amap[i]);
696 * Upcase table Management Functions
698 static s32 __load_upcase_table(struct super_block *sb, sector_t sector,
699 u32 num_sectors, u32 utbl_checksum)
701 int i, ret = FFS_ERROR;
703 struct fs_info_t *p_fs = &(EXFAT_SB(sb)->fs_info);
704 struct bd_info_t *p_bd = &(EXFAT_SB(sb)->bd_info);
705 struct buffer_head *tmp_bh = NULL;
706 sector_t end_sector = num_sectors + sector;
715 upcase_table = p_fs->vol_utbl = kmalloc(UTBL_COL_COUNT * sizeof(u16 *),
718 return FFS_MEMORYERR;
719 memset(upcase_table, 0, UTBL_COL_COUNT * sizeof(u16 *));
721 while (sector < end_sector) {
722 ret = sector_read(sb, sector, &tmp_bh, 1);
723 if (ret != FFS_SUCCESS) {
724 pr_debug("sector read (0x%llX)fail\n",
725 (unsigned long long)sector);
730 for (i = 0; i < p_bd->sector_size && index <= 0xFFFF; i += 2) {
731 uni = GET16(((u8 *)tmp_bh->b_data) + i);
733 checksum = ((checksum & 1) ? 0x80000000 : 0) +
734 (checksum >> 1) + *(((u8 *)tmp_bh->b_data) +
736 checksum = ((checksum & 1) ? 0x80000000 : 0) +
737 (checksum >> 1) + *(((u8 *)tmp_bh->b_data) +
741 pr_debug("skip from 0x%X ", index);
743 pr_debug("to 0x%X (amount of 0x%X)\n",
746 } else if (uni == index) {
748 } else if (uni == 0xFFFF) {
750 } else { /* uni != index , uni != 0xFFFF */
751 u16 col_index = get_col_index(index);
753 if (!upcase_table[col_index]) {
754 pr_debug("alloc = 0x%X\n", col_index);
755 upcase_table[col_index] = kmalloc_array(UTBL_ROW_COUNT,
756 sizeof(u16), GFP_KERNEL);
757 if (!upcase_table[col_index]) {
762 for (j = 0; j < UTBL_ROW_COUNT; j++)
763 upcase_table[col_index][j] = (col_index << LOW_INDEX_BIT) | j;
766 upcase_table[col_index][get_row_index(index)] = uni;
771 if (index >= 0xFFFF && utbl_checksum == checksum) {
780 free_upcase_table(sb);
784 static s32 __load_default_upcase_table(struct super_block *sb)
786 int i, ret = FFS_ERROR;
788 struct fs_info_t *p_fs = &(EXFAT_SB(sb)->fs_info);
795 upcase_table = p_fs->vol_utbl = kmalloc(UTBL_COL_COUNT * sizeof(u16 *),
798 return FFS_MEMORYERR;
799 memset(upcase_table, 0, UTBL_COL_COUNT * sizeof(u16 *));
801 for (i = 0; index <= 0xFFFF && i < NUM_UPCASE * 2; i += 2) {
802 uni = GET16(uni_upcase + i);
804 pr_debug("skip from 0x%X ", index);
806 pr_debug("to 0x%X (amount of 0x%X)\n", index, uni);
808 } else if (uni == index) {
810 } else if (uni == 0xFFFF) {
812 } else { /* uni != index , uni != 0xFFFF */
813 u16 col_index = get_col_index(index);
815 if (!upcase_table[col_index]) {
816 pr_debug("alloc = 0x%X\n", col_index);
817 upcase_table[col_index] = kmalloc_array(UTBL_ROW_COUNT,
820 if (!upcase_table[col_index]) {
825 for (j = 0; j < UTBL_ROW_COUNT; j++)
826 upcase_table[col_index][j] = (col_index << LOW_INDEX_BIT) | j;
829 upcase_table[col_index][get_row_index(index)] = uni;
838 /* FATAL error: default upcase table has error */
839 free_upcase_table(sb);
843 s32 load_upcase_table(struct super_block *sb)
846 u32 tbl_clu, tbl_size;
848 u32 type, num_sectors;
850 struct case_dentry_t *ep;
851 struct fs_info_t *p_fs = &(EXFAT_SB(sb)->fs_info);
852 struct bd_info_t *p_bd = &(EXFAT_SB(sb)->bd_info);
854 clu.dir = p_fs->root_dir;
857 if (p_fs->dev_ejected)
860 while (clu.dir != CLUSTER_32(~0)) {
861 for (i = 0; i < p_fs->dentries_per_clu; i++) {
862 ep = (struct case_dentry_t *)get_entry_in_dir(sb, &clu,
867 type = p_fs->fs_func->get_entry_type((struct dentry_t *)ep);
869 if (type == TYPE_UNUSED)
871 if (type != TYPE_UPCASE)
874 tbl_clu = GET32_A(ep->start_clu);
875 tbl_size = (u32)GET64_A(ep->size);
877 sector = START_SECTOR(tbl_clu);
878 num_sectors = ((tbl_size - 1) >> p_bd->sector_size_bits) + 1;
879 if (__load_upcase_table(sb, sector, num_sectors,
880 GET32_A(ep->checksum)) != FFS_SUCCESS)
884 if (FAT_read(sb, clu.dir, &clu.dir) != 0)
887 /* load default upcase table */
888 return __load_default_upcase_table(sb);
891 void free_upcase_table(struct super_block *sb)
894 struct fs_info_t *p_fs = &(EXFAT_SB(sb)->fs_info);
897 upcase_table = p_fs->vol_utbl;
898 for (i = 0; i < UTBL_COL_COUNT; i++)
899 kfree(upcase_table[i]);
901 kfree(p_fs->vol_utbl);
902 p_fs->vol_utbl = NULL;
906 * Directory Entry Management Functions
909 u32 fat_get_entry_type(struct dentry_t *p_entry)
911 struct dos_dentry_t *ep = (struct dos_dentry_t *)p_entry;
913 if (*(ep->name) == 0x0)
916 else if (*(ep->name) == 0xE5)
919 else if (ep->attr == ATTR_EXTEND)
922 else if ((ep->attr & (ATTR_SUBDIR | ATTR_VOLUME)) == ATTR_VOLUME)
925 else if ((ep->attr & (ATTR_SUBDIR | ATTR_VOLUME)) == ATTR_SUBDIR)
931 u32 exfat_get_entry_type(struct dentry_t *p_entry)
933 struct file_dentry_t *ep = (struct file_dentry_t *)p_entry;
935 if (ep->type == 0x0) {
937 } else if (ep->type < 0x80) {
939 } else if (ep->type == 0x80) {
941 } else if (ep->type < 0xA0) {
942 if (ep->type == 0x81) {
944 } else if (ep->type == 0x82) {
946 } else if (ep->type == 0x83) {
948 } else if (ep->type == 0x85) {
949 if (GET16_A(ep->attr) & ATTR_SUBDIR)
954 return TYPE_CRITICAL_PRI;
955 } else if (ep->type < 0xC0) {
956 if (ep->type == 0xA0)
958 else if (ep->type == 0xA1)
960 else if (ep->type == 0xA2)
962 return TYPE_BENIGN_PRI;
963 } else if (ep->type < 0xE0) {
964 if (ep->type == 0xC0)
966 else if (ep->type == 0xC1)
968 else if (ep->type == 0xC2)
970 return TYPE_CRITICAL_SEC;
973 return TYPE_BENIGN_SEC;
976 void fat_set_entry_type(struct dentry_t *p_entry, u32 type)
978 struct dos_dentry_t *ep = (struct dos_dentry_t *)p_entry;
980 if (type == TYPE_UNUSED)
983 else if (type == TYPE_DELETED)
986 else if (type == TYPE_EXTEND)
987 ep->attr = ATTR_EXTEND;
989 else if (type == TYPE_DIR)
990 ep->attr = ATTR_SUBDIR;
992 else if (type == TYPE_FILE)
993 ep->attr = ATTR_ARCHIVE;
995 else if (type == TYPE_SYMLINK)
996 ep->attr = ATTR_ARCHIVE | ATTR_SYMLINK;
999 void exfat_set_entry_type(struct dentry_t *p_entry, u32 type)
1001 struct file_dentry_t *ep = (struct file_dentry_t *)p_entry;
1003 if (type == TYPE_UNUSED) {
1005 } else if (type == TYPE_DELETED) {
1007 } else if (type == TYPE_STREAM) {
1009 } else if (type == TYPE_EXTEND) {
1011 } else if (type == TYPE_BITMAP) {
1013 } else if (type == TYPE_UPCASE) {
1015 } else if (type == TYPE_VOLUME) {
1017 } else if (type == TYPE_DIR) {
1019 SET16_A(ep->attr, ATTR_SUBDIR);
1020 } else if (type == TYPE_FILE) {
1022 SET16_A(ep->attr, ATTR_ARCHIVE);
1023 } else if (type == TYPE_SYMLINK) {
1025 SET16_A(ep->attr, ATTR_ARCHIVE | ATTR_SYMLINK);
1029 u32 fat_get_entry_attr(struct dentry_t *p_entry)
1031 struct dos_dentry_t *ep = (struct dos_dentry_t *)p_entry;
1033 return (u32)ep->attr;
1036 u32 exfat_get_entry_attr(struct dentry_t *p_entry)
1038 struct file_dentry_t *ep = (struct file_dentry_t *)p_entry;
1040 return (u32)GET16_A(ep->attr);
1043 void fat_set_entry_attr(struct dentry_t *p_entry, u32 attr)
1045 struct dos_dentry_t *ep = (struct dos_dentry_t *)p_entry;
1047 ep->attr = (u8)attr;
1050 void exfat_set_entry_attr(struct dentry_t *p_entry, u32 attr)
1052 struct file_dentry_t *ep = (struct file_dentry_t *)p_entry;
1054 SET16_A(ep->attr, (u16)attr);
1057 u8 fat_get_entry_flag(struct dentry_t *p_entry)
1062 u8 exfat_get_entry_flag(struct dentry_t *p_entry)
1064 struct strm_dentry_t *ep = (struct strm_dentry_t *)p_entry;
1069 void fat_set_entry_flag(struct dentry_t *p_entry, u8 flags)
1073 void exfat_set_entry_flag(struct dentry_t *p_entry, u8 flags)
1075 struct strm_dentry_t *ep = (struct strm_dentry_t *)p_entry;
1080 u32 fat_get_entry_clu0(struct dentry_t *p_entry)
1082 struct dos_dentry_t *ep = (struct dos_dentry_t *)p_entry;
1084 return ((u32)GET16_A(ep->start_clu_hi) << 16) |
1085 GET16_A(ep->start_clu_lo);
1088 u32 exfat_get_entry_clu0(struct dentry_t *p_entry)
1090 struct strm_dentry_t *ep = (struct strm_dentry_t *)p_entry;
1092 return GET32_A(ep->start_clu);
1095 void fat_set_entry_clu0(struct dentry_t *p_entry, u32 start_clu)
1097 struct dos_dentry_t *ep = (struct dos_dentry_t *)p_entry;
1099 SET16_A(ep->start_clu_lo, CLUSTER_16(start_clu));
1100 SET16_A(ep->start_clu_hi, CLUSTER_16(start_clu >> 16));
1103 void exfat_set_entry_clu0(struct dentry_t *p_entry, u32 start_clu)
1105 struct strm_dentry_t *ep = (struct strm_dentry_t *)p_entry;
1107 SET32_A(ep->start_clu, start_clu);
1110 u64 fat_get_entry_size(struct dentry_t *p_entry)
1112 struct dos_dentry_t *ep = (struct dos_dentry_t *)p_entry;
1114 return (u64)GET32_A(ep->size);
1117 u64 exfat_get_entry_size(struct dentry_t *p_entry)
1119 struct strm_dentry_t *ep = (struct strm_dentry_t *)p_entry;
1121 return GET64_A(ep->valid_size);
1124 void fat_set_entry_size(struct dentry_t *p_entry, u64 size)
1126 struct dos_dentry_t *ep = (struct dos_dentry_t *)p_entry;
1128 SET32_A(ep->size, (u32)size);
1131 void exfat_set_entry_size(struct dentry_t *p_entry, u64 size)
1133 struct strm_dentry_t *ep = (struct strm_dentry_t *)p_entry;
1135 SET64_A(ep->valid_size, size);
1136 SET64_A(ep->size, size);
1139 void fat_get_entry_time(struct dentry_t *p_entry, struct timestamp_t *tp,
1142 u16 t = 0x00, d = 0x21;
1143 struct dos_dentry_t *ep = (struct dos_dentry_t *)p_entry;
1147 t = GET16_A(ep->create_time);
1148 d = GET16_A(ep->create_date);
1151 t = GET16_A(ep->modify_time);
1152 d = GET16_A(ep->modify_date);
1156 tp->sec = (t & 0x001F) << 1;
1157 tp->min = (t >> 5) & 0x003F;
1158 tp->hour = (t >> 11);
1159 tp->day = (d & 0x001F);
1160 tp->mon = (d >> 5) & 0x000F;
1161 tp->year = (d >> 9);
1164 void exfat_get_entry_time(struct dentry_t *p_entry, struct timestamp_t *tp,
1167 u16 t = 0x00, d = 0x21;
1168 struct file_dentry_t *ep = (struct file_dentry_t *)p_entry;
1172 t = GET16_A(ep->create_time);
1173 d = GET16_A(ep->create_date);
1176 t = GET16_A(ep->modify_time);
1177 d = GET16_A(ep->modify_date);
1180 t = GET16_A(ep->access_time);
1181 d = GET16_A(ep->access_date);
1185 tp->sec = (t & 0x001F) << 1;
1186 tp->min = (t >> 5) & 0x003F;
1187 tp->hour = (t >> 11);
1188 tp->day = (d & 0x001F);
1189 tp->mon = (d >> 5) & 0x000F;
1190 tp->year = (d >> 9);
1193 void fat_set_entry_time(struct dentry_t *p_entry, struct timestamp_t *tp,
1197 struct dos_dentry_t *ep = (struct dos_dentry_t *)p_entry;
1199 t = (tp->hour << 11) | (tp->min << 5) | (tp->sec >> 1);
1200 d = (tp->year << 9) | (tp->mon << 5) | tp->day;
1204 SET16_A(ep->create_time, t);
1205 SET16_A(ep->create_date, d);
1208 SET16_A(ep->modify_time, t);
1209 SET16_A(ep->modify_date, d);
1214 void exfat_set_entry_time(struct dentry_t *p_entry, struct timestamp_t *tp,
1218 struct file_dentry_t *ep = (struct file_dentry_t *)p_entry;
1220 t = (tp->hour << 11) | (tp->min << 5) | (tp->sec >> 1);
1221 d = (tp->year << 9) | (tp->mon << 5) | tp->day;
1225 SET16_A(ep->create_time, t);
1226 SET16_A(ep->create_date, d);
1229 SET16_A(ep->modify_time, t);
1230 SET16_A(ep->modify_date, d);
1233 SET16_A(ep->access_time, t);
1234 SET16_A(ep->access_date, d);
1239 s32 fat_init_dir_entry(struct super_block *sb, struct chain_t *p_dir, s32 entry,
1240 u32 type, u32 start_clu, u64 size)
1243 struct dos_dentry_t *dos_ep;
1245 dos_ep = (struct dos_dentry_t *)get_entry_in_dir(sb, p_dir, entry,
1248 return FFS_MEDIAERR;
1250 init_dos_entry(dos_ep, type, start_clu);
1251 buf_modify(sb, sector);
1256 s32 exfat_init_dir_entry(struct super_block *sb, struct chain_t *p_dir,
1257 s32 entry, u32 type, u32 start_clu, u64 size)
1261 struct file_dentry_t *file_ep;
1262 struct strm_dentry_t *strm_ep;
1264 flags = (type == TYPE_FILE) ? 0x01 : 0x03;
1266 /* we cannot use get_entry_set_in_dir here because file ep is not initialized yet */
1267 file_ep = (struct file_dentry_t *)get_entry_in_dir(sb, p_dir, entry,
1270 return FFS_MEDIAERR;
1272 strm_ep = (struct strm_dentry_t *)get_entry_in_dir(sb, p_dir, entry + 1,
1275 return FFS_MEDIAERR;
1277 init_file_entry(file_ep, type);
1278 buf_modify(sb, sector);
1280 init_strm_entry(strm_ep, flags, start_clu, size);
1281 buf_modify(sb, sector);
1286 static s32 fat_init_ext_entry(struct super_block *sb, struct chain_t *p_dir,
1287 s32 entry, s32 num_entries,
1288 struct uni_name_t *p_uniname,
1289 struct dos_name_t *p_dosname)
1294 u16 *uniname = p_uniname->name;
1295 struct dos_dentry_t *dos_ep;
1296 struct ext_dentry_t *ext_ep;
1298 dos_ep = (struct dos_dentry_t *)get_entry_in_dir(sb, p_dir, entry,
1301 return FFS_MEDIAERR;
1303 dos_ep->lcase = p_dosname->name_case;
1304 memcpy(dos_ep->name, p_dosname->name, DOS_NAME_LENGTH);
1305 buf_modify(sb, sector);
1307 if ((--num_entries) > 0) {
1308 chksum = calc_checksum_1byte((void *)dos_ep->name,
1309 DOS_NAME_LENGTH, 0);
1311 for (i = 1; i < num_entries; i++) {
1312 ext_ep = (struct ext_dentry_t *)get_entry_in_dir(sb,
1317 return FFS_MEDIAERR;
1319 init_ext_entry(ext_ep, i, chksum, uniname);
1320 buf_modify(sb, sector);
1324 ext_ep = (struct ext_dentry_t *)get_entry_in_dir(sb, p_dir,
1328 return FFS_MEDIAERR;
1330 init_ext_entry(ext_ep, i + 0x40, chksum, uniname);
1331 buf_modify(sb, sector);
1337 static s32 exfat_init_ext_entry(struct super_block *sb, struct chain_t *p_dir,
1338 s32 entry, s32 num_entries,
1339 struct uni_name_t *p_uniname,
1340 struct dos_name_t *p_dosname)
1344 u16 *uniname = p_uniname->name;
1345 struct file_dentry_t *file_ep;
1346 struct strm_dentry_t *strm_ep;
1347 struct name_dentry_t *name_ep;
1349 file_ep = (struct file_dentry_t *)get_entry_in_dir(sb, p_dir, entry,
1352 return FFS_MEDIAERR;
1354 file_ep->num_ext = (u8)(num_entries - 1);
1355 buf_modify(sb, sector);
1357 strm_ep = (struct strm_dentry_t *)get_entry_in_dir(sb, p_dir, entry + 1,
1360 return FFS_MEDIAERR;
1362 strm_ep->name_len = p_uniname->name_len;
1363 SET16_A(strm_ep->name_hash, p_uniname->name_hash);
1364 buf_modify(sb, sector);
1366 for (i = 2; i < num_entries; i++) {
1367 name_ep = (struct name_dentry_t *)get_entry_in_dir(sb, p_dir,
1371 return FFS_MEDIAERR;
1373 init_name_entry(name_ep, uniname);
1374 buf_modify(sb, sector);
1378 update_dir_checksum(sb, p_dir, entry);
1383 void init_dos_entry(struct dos_dentry_t *ep, u32 type, u32 start_clu)
1385 struct timestamp_t tm, *tp;
1387 fat_set_entry_type((struct dentry_t *)ep, type);
1388 SET16_A(ep->start_clu_lo, CLUSTER_16(start_clu));
1389 SET16_A(ep->start_clu_hi, CLUSTER_16(start_clu >> 16));
1390 SET32_A(ep->size, 0);
1392 tp = tm_current(&tm);
1393 fat_set_entry_time((struct dentry_t *)ep, tp, TM_CREATE);
1394 fat_set_entry_time((struct dentry_t *)ep, tp, TM_MODIFY);
1395 SET16_A(ep->access_date, 0);
1396 ep->create_time_ms = 0;
1399 void init_ext_entry(struct ext_dentry_t *ep, s32 order, u8 chksum, u16 *uniname)
1404 fat_set_entry_type((struct dentry_t *)ep, TYPE_EXTEND);
1405 ep->order = (u8)order;
1407 ep->checksum = chksum;
1408 SET16_A(ep->start_clu, 0);
1410 for (i = 0; i < 10; i += 2) {
1412 SET16(ep->unicode_0_4 + i, *uniname);
1413 if (*uniname == 0x0)
1418 SET16(ep->unicode_0_4 + i, 0xFFFF);
1422 for (i = 0; i < 12; i += 2) {
1424 SET16_A(ep->unicode_5_10 + i, *uniname);
1425 if (*uniname == 0x0)
1430 SET16_A(ep->unicode_5_10 + i, 0xFFFF);
1434 for (i = 0; i < 4; i += 2) {
1436 SET16_A(ep->unicode_11_12 + i, *uniname);
1437 if (*uniname == 0x0)
1442 SET16_A(ep->unicode_11_12 + i, 0xFFFF);
1447 void init_file_entry(struct file_dentry_t *ep, u32 type)
1449 struct timestamp_t tm, *tp;
1451 exfat_set_entry_type((struct dentry_t *)ep, type);
1453 tp = tm_current(&tm);
1454 exfat_set_entry_time((struct dentry_t *)ep, tp, TM_CREATE);
1455 exfat_set_entry_time((struct dentry_t *)ep, tp, TM_MODIFY);
1456 exfat_set_entry_time((struct dentry_t *)ep, tp, TM_ACCESS);
1457 ep->create_time_ms = 0;
1458 ep->modify_time_ms = 0;
1459 ep->access_time_ms = 0;
1462 void init_strm_entry(struct strm_dentry_t *ep, u8 flags, u32 start_clu, u64 size)
1464 exfat_set_entry_type((struct dentry_t *)ep, TYPE_STREAM);
1466 SET32_A(ep->start_clu, start_clu);
1467 SET64_A(ep->valid_size, size);
1468 SET64_A(ep->size, size);
1471 void init_name_entry(struct name_dentry_t *ep, u16 *uniname)
1475 exfat_set_entry_type((struct dentry_t *)ep, TYPE_EXTEND);
1478 for (i = 0; i < 30; i++, i++) {
1479 SET16_A(ep->unicode_0_14 + i, *uniname);
1480 if (*uniname == 0x0)
1486 void fat_delete_dir_entry(struct super_block *sb, struct chain_t *p_dir,
1487 s32 entry, s32 order, s32 num_entries)
1491 struct dentry_t *ep;
1492 struct fs_info_t *p_fs = &(EXFAT_SB(sb)->fs_info);
1494 for (i = num_entries - 1; i >= order; i--) {
1495 ep = get_entry_in_dir(sb, p_dir, entry - i, §or);
1499 p_fs->fs_func->set_entry_type(ep, TYPE_DELETED);
1500 buf_modify(sb, sector);
1504 void exfat_delete_dir_entry(struct super_block *sb, struct chain_t *p_dir,
1505 s32 entry, s32 order, s32 num_entries)
1509 struct dentry_t *ep;
1510 struct fs_info_t *p_fs = &(EXFAT_SB(sb)->fs_info);
1512 for (i = order; i < num_entries; i++) {
1513 ep = get_entry_in_dir(sb, p_dir, entry + i, §or);
1517 p_fs->fs_func->set_entry_type(ep, TYPE_DELETED);
1518 buf_modify(sb, sector);
1522 void update_dir_checksum(struct super_block *sb, struct chain_t *p_dir,
1528 struct file_dentry_t *file_ep;
1529 struct dentry_t *ep;
1531 file_ep = (struct file_dentry_t *)get_entry_in_dir(sb, p_dir, entry,
1536 buf_lock(sb, sector);
1538 num_entries = (s32)file_ep->num_ext + 1;
1539 chksum = calc_checksum_2byte((void *)file_ep, DENTRY_SIZE, 0,
1542 for (i = 1; i < num_entries; i++) {
1543 ep = get_entry_in_dir(sb, p_dir, entry + i, NULL);
1545 buf_unlock(sb, sector);
1549 chksum = calc_checksum_2byte((void *)ep, DENTRY_SIZE, chksum,
1553 SET16_A(file_ep->checksum, chksum);
1554 buf_modify(sb, sector);
1555 buf_unlock(sb, sector);
1558 void update_dir_checksum_with_entry_set(struct super_block *sb,
1559 struct entry_set_cache_t *es)
1561 struct dentry_t *ep;
1563 s32 chksum_type = CS_DIR_ENTRY, i;
1565 ep = (struct dentry_t *)&(es->__buf);
1566 for (i = 0; i < es->num_entries; i++) {
1567 pr_debug("%s ep %p\n", __func__, ep);
1568 chksum = calc_checksum_2byte((void *)ep, DENTRY_SIZE, chksum,
1571 chksum_type = CS_DEFAULT;
1574 ep = (struct dentry_t *)&(es->__buf);
1575 SET16_A(((struct file_dentry_t *)ep)->checksum, chksum);
1576 write_whole_entry_set(sb, es);
1579 static s32 _walk_fat_chain(struct super_block *sb, struct chain_t *p_dir,
1580 s32 byte_offset, u32 *clu)
1582 struct fs_info_t *p_fs = &(EXFAT_SB(sb)->fs_info);
1586 clu_offset = byte_offset >> p_fs->cluster_size_bits;
1587 cur_clu = p_dir->dir;
1589 if (p_dir->flags == 0x03) {
1590 cur_clu += clu_offset;
1592 while (clu_offset > 0) {
1593 if (FAT_read(sb, cur_clu, &cur_clu) == -1)
1594 return FFS_MEDIAERR;
1604 s32 find_location(struct super_block *sb, struct chain_t *p_dir, s32 entry,
1605 sector_t *sector, s32 *offset)
1609 struct fs_info_t *p_fs = &(EXFAT_SB(sb)->fs_info);
1610 struct bd_info_t *p_bd = &(EXFAT_SB(sb)->bd_info);
1612 off = entry << DENTRY_SIZE_BITS;
1614 if (p_dir->dir == CLUSTER_32(0)) { /* FAT16 root_dir */
1615 *offset = off & p_bd->sector_size_mask;
1616 *sector = off >> p_bd->sector_size_bits;
1617 *sector += p_fs->root_start_sector;
1619 ret = _walk_fat_chain(sb, p_dir, off, &clu);
1620 if (ret != FFS_SUCCESS)
1623 /* byte offset in cluster */
1624 off &= p_fs->cluster_size - 1;
1626 /* byte offset in sector */
1627 *offset = off & p_bd->sector_size_mask;
1629 /* sector offset in cluster */
1630 *sector = off >> p_bd->sector_size_bits;
1631 *sector += START_SECTOR(clu);
1636 struct dentry_t *get_entry_with_sector(struct super_block *sb, sector_t sector,
1641 buf = buf_getblk(sb, sector);
1646 return (struct dentry_t *)(buf + offset);
1649 struct dentry_t *get_entry_in_dir(struct super_block *sb, struct chain_t *p_dir,
1650 s32 entry, sector_t *sector)
1656 if (find_location(sb, p_dir, entry, &sec, &off) != FFS_SUCCESS)
1659 buf = buf_getblk(sb, sec);
1666 return (struct dentry_t *)(buf + off);
1669 /* returns a set of dentries for a file or dir.
1670 * Note that this is a copy (dump) of dentries so that user should call write_entry_set()
1671 * to apply changes made in this entry set to the real device.
1673 * sb+p_dir+entry: indicates a file/dir
1674 * type: specifies how many dentries should be included.
1676 * file_ep: will point the first dentry(= file dentry) on success
1678 * pointer of entry set on success,
1682 #define ES_MODE_STARTED 0
1683 #define ES_MODE_GET_FILE_ENTRY 1
1684 #define ES_MODE_GET_STRM_ENTRY 2
1685 #define ES_MODE_GET_NAME_ENTRY 3
1686 #define ES_MODE_GET_CRITICAL_SEC_ENTRY 4
1687 struct entry_set_cache_t *get_entry_set_in_dir(struct super_block *sb,
1688 struct chain_t *p_dir, s32 entry,
1690 struct dentry_t **file_ep)
1692 s32 off, ret, byte_offset;
1696 struct fs_info_t *p_fs = &(EXFAT_SB(sb)->fs_info);
1697 struct bd_info_t *p_bd = &(EXFAT_SB(sb)->bd_info);
1698 struct entry_set_cache_t *es = NULL;
1699 struct dentry_t *ep, *pos;
1702 s32 mode = ES_MODE_STARTED;
1705 pr_debug("%s entered p_dir dir %u flags %x size %d\n",
1706 __func__, p_dir->dir, p_dir->flags, p_dir->size);
1708 byte_offset = entry << DENTRY_SIZE_BITS;
1709 ret = _walk_fat_chain(sb, p_dir, byte_offset, &clu);
1710 if (ret != FFS_SUCCESS)
1713 /* byte offset in cluster */
1714 byte_offset &= p_fs->cluster_size - 1;
1716 /* byte offset in sector */
1717 off = byte_offset & p_bd->sector_size_mask;
1719 /* sector offset in cluster */
1720 sec = byte_offset >> p_bd->sector_size_bits;
1721 sec += START_SECTOR(clu);
1723 buf = buf_getblk(sb, sec);
1727 ep = (struct dentry_t *)(buf + off);
1728 entry_type = p_fs->fs_func->get_entry_type(ep);
1730 if ((entry_type != TYPE_FILE)
1731 && (entry_type != TYPE_DIR))
1734 if (type == ES_ALL_ENTRIES)
1735 num_entries = ((struct file_dentry_t *)ep)->num_ext + 1;
1739 bufsize = offsetof(struct entry_set_cache_t, __buf) + (num_entries) *
1740 sizeof(struct dentry_t);
1741 pr_debug("%s: trying to kmalloc %zx bytes for %d entries\n", __func__,
1742 bufsize, num_entries);
1743 es = kmalloc(bufsize, GFP_KERNEL);
1747 es->num_entries = num_entries;
1750 es->alloc_flag = p_dir->flags;
1752 pos = (struct dentry_t *)&es->__buf;
1754 while (num_entries) {
1756 * instead of copying whole sector, we will check every entry.
1757 * this will provide minimum stablity and consistency.
1759 entry_type = p_fs->fs_func->get_entry_type(ep);
1761 if ((entry_type == TYPE_UNUSED) || (entry_type == TYPE_DELETED))
1765 case ES_MODE_STARTED:
1766 if ((entry_type == TYPE_FILE) || (entry_type == TYPE_DIR))
1767 mode = ES_MODE_GET_FILE_ENTRY;
1771 case ES_MODE_GET_FILE_ENTRY:
1772 if (entry_type == TYPE_STREAM)
1773 mode = ES_MODE_GET_STRM_ENTRY;
1777 case ES_MODE_GET_STRM_ENTRY:
1778 if (entry_type == TYPE_EXTEND)
1779 mode = ES_MODE_GET_NAME_ENTRY;
1783 case ES_MODE_GET_NAME_ENTRY:
1784 if (entry_type == TYPE_EXTEND)
1786 else if (entry_type == TYPE_STREAM)
1788 else if (entry_type & TYPE_CRITICAL_SEC)
1789 mode = ES_MODE_GET_CRITICAL_SEC_ENTRY;
1793 case ES_MODE_GET_CRITICAL_SEC_ENTRY:
1794 if ((entry_type == TYPE_EXTEND) ||
1795 (entry_type == TYPE_STREAM))
1797 else if ((entry_type & TYPE_CRITICAL_SEC) !=
1803 memcpy(pos, ep, sizeof(struct dentry_t));
1805 if (--num_entries == 0)
1808 if (((off + DENTRY_SIZE) & p_bd->sector_size_mask) <
1809 (off & p_bd->sector_size_mask)) {
1810 /* get the next sector */
1811 if (IS_LAST_SECTOR_IN_CLUSTER(sec)) {
1812 if (es->alloc_flag == 0x03) {
1815 if (FAT_read(sb, clu, &clu) == -1)
1818 sec = START_SECTOR(clu);
1822 buf = buf_getblk(sb, sec);
1826 ep = (struct dentry_t *)(buf);
1835 *file_ep = (struct dentry_t *)&(es->__buf);
1837 pr_debug("%s exiting es %p sec %llu offset %d flags %d, num_entries %u buf ptr %p\n",
1838 __func__, es, (unsigned long long)es->sector, es->offset,
1839 es->alloc_flag, es->num_entries, &es->__buf);
1842 pr_debug("%s exited NULL (es %p)\n", __func__, es);
1847 void release_entry_set(struct entry_set_cache_t *es)
1849 pr_debug("%s es=%p\n", __func__, es);
1853 static s32 __write_partial_entries_in_entry_set(struct super_block *sb,
1854 struct entry_set_cache_t *es,
1855 sector_t sec, s32 off, u32 count)
1857 s32 num_entries, buf_off = (off - es->offset);
1858 u32 remaining_byte_in_sector, copy_entries;
1859 struct fs_info_t *p_fs = &(EXFAT_SB(sb)->fs_info);
1860 struct bd_info_t *p_bd = &(EXFAT_SB(sb)->bd_info);
1862 u8 *buf, *esbuf = (u8 *)&(es->__buf);
1864 pr_debug("%s entered es %p sec %llu off %d count %d\n",
1865 __func__, es, (unsigned long long)sec, off, count);
1866 num_entries = count;
1868 while (num_entries) {
1869 /* white per sector base */
1870 remaining_byte_in_sector = (1 << p_bd->sector_size_bits) - off;
1871 copy_entries = min_t(s32,
1872 remaining_byte_in_sector >> DENTRY_SIZE_BITS,
1874 buf = buf_getblk(sb, sec);
1877 pr_debug("es->buf %p buf_off %u\n", esbuf, buf_off);
1878 pr_debug("copying %d entries from %p to sector %llu\n",
1879 copy_entries, (esbuf + buf_off),
1880 (unsigned long long)sec);
1881 memcpy(buf + off, esbuf + buf_off,
1882 copy_entries << DENTRY_SIZE_BITS);
1883 buf_modify(sb, sec);
1884 num_entries -= copy_entries;
1887 /* get next sector */
1888 if (IS_LAST_SECTOR_IN_CLUSTER(sec)) {
1889 clu = GET_CLUSTER_FROM_SECTOR(sec);
1890 if (es->alloc_flag == 0x03) {
1893 if (FAT_read(sb, clu, &clu) == -1)
1896 sec = START_SECTOR(clu);
1901 buf_off += copy_entries << DENTRY_SIZE_BITS;
1905 pr_debug("%s exited successfully\n", __func__);
1908 pr_debug("%s failed\n", __func__);
1912 /* write back all entries in entry set */
1913 s32 write_whole_entry_set(struct super_block *sb, struct entry_set_cache_t *es)
1915 return __write_partial_entries_in_entry_set(sb, es, es->sector,
1920 /* write back some entries in entry set */
1921 s32 write_partial_entries_in_entry_set(struct super_block *sb,
1922 struct entry_set_cache_t *es, struct dentry_t *ep, u32 count)
1924 s32 ret, byte_offset, off;
1927 struct fs_info_t *p_fs = &(EXFAT_SB(sb)->fs_info);
1928 struct bd_info_t *p_bd = &(EXFAT_SB(sb)->bd_info);
1932 if (ep + count > ((struct dentry_t *)&(es->__buf)) + es->num_entries)
1935 dir.dir = GET_CLUSTER_FROM_SECTOR(es->sector);
1936 dir.flags = es->alloc_flag;
1937 dir.size = 0xffffffff; /* XXX */
1939 byte_offset = (es->sector - START_SECTOR(dir.dir)) <<
1940 p_bd->sector_size_bits;
1941 byte_offset += ((void **)ep - &(es->__buf)) + es->offset;
1943 ret = _walk_fat_chain(sb, &dir, byte_offset, &clu);
1944 if (ret != FFS_SUCCESS)
1947 /* byte offset in cluster */
1948 byte_offset &= p_fs->cluster_size - 1;
1950 /* byte offset in sector */
1951 off = byte_offset & p_bd->sector_size_mask;
1953 /* sector offset in cluster */
1954 sec = byte_offset >> p_bd->sector_size_bits;
1955 sec += START_SECTOR(clu);
1956 return __write_partial_entries_in_entry_set(sb, es, sec, off, count);
1959 /* search EMPTY CONTINUOUS "num_entries" entries */
1960 s32 search_deleted_or_unused_entry(struct super_block *sb,
1961 struct chain_t *p_dir, s32 num_entries)
1963 int i, dentry, num_empty = 0;
1964 s32 dentries_per_clu;
1967 struct dentry_t *ep;
1968 struct fs_info_t *p_fs = &(EXFAT_SB(sb)->fs_info);
1970 if (p_dir->dir == CLUSTER_32(0)) /* FAT16 root_dir */
1971 dentries_per_clu = p_fs->dentries_in_root;
1973 dentries_per_clu = p_fs->dentries_per_clu;
1975 if (p_fs->hint_uentry.dir == p_dir->dir) {
1976 if (p_fs->hint_uentry.entry == -1)
1979 clu.dir = p_fs->hint_uentry.clu.dir;
1980 clu.size = p_fs->hint_uentry.clu.size;
1981 clu.flags = p_fs->hint_uentry.clu.flags;
1983 dentry = p_fs->hint_uentry.entry;
1985 p_fs->hint_uentry.entry = -1;
1987 clu.dir = p_dir->dir;
1988 clu.size = p_dir->size;
1989 clu.flags = p_dir->flags;
1994 while (clu.dir != CLUSTER_32(~0)) {
1995 if (p_fs->dev_ejected)
1998 if (p_dir->dir == CLUSTER_32(0)) /* FAT16 root_dir */
1999 i = dentry % dentries_per_clu;
2001 i = dentry & (dentries_per_clu - 1);
2003 for (; i < dentries_per_clu; i++, dentry++) {
2004 ep = get_entry_in_dir(sb, &clu, i, NULL);
2008 type = p_fs->fs_func->get_entry_type(ep);
2010 if (type == TYPE_UNUSED) {
2012 if (p_fs->hint_uentry.entry == -1) {
2013 p_fs->hint_uentry.dir = p_dir->dir;
2014 p_fs->hint_uentry.entry = dentry;
2016 p_fs->hint_uentry.clu.dir = clu.dir;
2017 p_fs->hint_uentry.clu.size = clu.size;
2018 p_fs->hint_uentry.clu.flags = clu.flags;
2020 } else if (type == TYPE_DELETED) {
2026 if (num_empty >= num_entries) {
2027 p_fs->hint_uentry.dir = CLUSTER_32(~0);
2028 p_fs->hint_uentry.entry = -1;
2030 if (p_fs->vol_type == EXFAT)
2031 return dentry - (num_entries - 1);
2037 if (p_dir->dir == CLUSTER_32(0))
2038 break; /* FAT16 root_dir */
2040 if (clu.flags == 0x03) {
2041 if ((--clu.size) > 0)
2044 clu.dir = CLUSTER_32(~0);
2046 if (FAT_read(sb, clu.dir, &clu.dir) != 0)
2054 s32 find_empty_entry(struct inode *inode, struct chain_t *p_dir, s32 num_entries)
2061 struct dentry_t *ep = NULL;
2062 struct super_block *sb = inode->i_sb;
2063 struct fs_info_t *p_fs = &(EXFAT_SB(sb)->fs_info);
2064 struct file_id_t *fid = &(EXFAT_I(inode)->fid);
2066 if (p_dir->dir == CLUSTER_32(0)) /* FAT16 root_dir */
2067 return search_deleted_or_unused_entry(sb, p_dir, num_entries);
2069 while ((dentry = search_deleted_or_unused_entry(sb, p_dir, num_entries)) < 0) {
2070 if (p_fs->dev_ejected)
2073 if (p_fs->vol_type == EXFAT) {
2074 if (p_dir->dir != p_fs->root_dir)
2075 size = i_size_read(inode);
2078 last_clu = find_last_cluster(sb, p_dir);
2079 clu.dir = last_clu + 1;
2081 clu.flags = p_dir->flags;
2083 /* (1) allocate a cluster */
2084 ret = p_fs->fs_func->alloc_cluster(sb, 1, &clu);
2088 if (clear_cluster(sb, clu.dir) != FFS_SUCCESS)
2091 /* (2) append to the FAT chain */
2092 if (clu.flags != p_dir->flags) {
2093 exfat_chain_cont_cluster(sb, p_dir->dir, p_dir->size);
2094 p_dir->flags = 0x01;
2095 p_fs->hint_uentry.clu.flags = 0x01;
2097 if (clu.flags == 0x01)
2098 if (FAT_write(sb, last_clu, clu.dir) < 0)
2101 if (p_fs->hint_uentry.entry == -1) {
2102 p_fs->hint_uentry.dir = p_dir->dir;
2103 p_fs->hint_uentry.entry = p_dir->size << (p_fs->cluster_size_bits - DENTRY_SIZE_BITS);
2105 p_fs->hint_uentry.clu.dir = clu.dir;
2106 p_fs->hint_uentry.clu.size = 0;
2107 p_fs->hint_uentry.clu.flags = clu.flags;
2109 p_fs->hint_uentry.clu.size++;
2112 /* (3) update the directory entry */
2113 if (p_fs->vol_type == EXFAT) {
2114 if (p_dir->dir != p_fs->root_dir) {
2115 size += p_fs->cluster_size;
2117 ep = get_entry_in_dir(sb, &fid->dir,
2118 fid->entry + 1, §or);
2121 p_fs->fs_func->set_entry_size(ep, size);
2122 p_fs->fs_func->set_entry_flag(ep, p_dir->flags);
2123 buf_modify(sb, sector);
2125 update_dir_checksum(sb, &(fid->dir),
2130 i_size_write(inode, i_size_read(inode) + p_fs->cluster_size);
2131 EXFAT_I(inode)->mmu_private += p_fs->cluster_size;
2132 EXFAT_I(inode)->fid.size += p_fs->cluster_size;
2133 EXFAT_I(inode)->fid.flags = p_dir->flags;
2134 inode->i_blocks += 1 << (p_fs->cluster_size_bits - 9);
2140 /* return values of fat_find_dir_entry()
2141 * >= 0 : return dir entiry position with the name in dir
2142 * -1 : (root dir, ".") it is the root dir itself
2143 * -2 : entry with the name does not exist
2145 s32 fat_find_dir_entry(struct super_block *sb, struct chain_t *p_dir,
2146 struct uni_name_t *p_uniname, s32 num_entries,
2147 struct dos_name_t *p_dosname, u32 type)
2149 int i, dentry = 0, len;
2151 bool is_feasible_entry = true, has_ext_entry = false;
2152 s32 dentries_per_clu;
2154 u16 entry_uniname[14], *uniname = NULL, unichar;
2156 struct dentry_t *ep;
2157 struct dos_dentry_t *dos_ep;
2158 struct ext_dentry_t *ext_ep;
2159 struct fs_info_t *p_fs = &(EXFAT_SB(sb)->fs_info);
2161 if (p_dir->dir == p_fs->root_dir) {
2162 if ((!nls_uniname_cmp(sb, p_uniname->name,
2163 (u16 *)UNI_CUR_DIR_NAME)) ||
2164 (!nls_uniname_cmp(sb, p_uniname->name,
2165 (u16 *)UNI_PAR_DIR_NAME)))
2166 return -1; // special case, root directory itself
2169 if (p_dir->dir == CLUSTER_32(0)) /* FAT16 root_dir */
2170 dentries_per_clu = p_fs->dentries_in_root;
2172 dentries_per_clu = p_fs->dentries_per_clu;
2174 clu.dir = p_dir->dir;
2175 clu.flags = p_dir->flags;
2177 while (clu.dir != CLUSTER_32(~0)) {
2178 if (p_fs->dev_ejected)
2181 for (i = 0; i < dentries_per_clu; i++, dentry++) {
2182 ep = get_entry_in_dir(sb, &clu, i, NULL);
2186 entry_type = p_fs->fs_func->get_entry_type(ep);
2188 if ((entry_type == TYPE_FILE) || (entry_type == TYPE_DIR)) {
2189 if ((type == TYPE_ALL) || (type == entry_type)) {
2190 if (is_feasible_entry && has_ext_entry)
2193 dos_ep = (struct dos_dentry_t *)ep;
2194 if (!nls_dosname_cmp(sb, p_dosname->name, dos_ep->name))
2197 is_feasible_entry = true;
2198 has_ext_entry = false;
2199 } else if (entry_type == TYPE_EXTEND) {
2200 if (is_feasible_entry) {
2201 ext_ep = (struct ext_dentry_t *)ep;
2202 if (ext_ep->order > 0x40) {
2203 order = (s32)(ext_ep->order - 0x40);
2204 uniname = p_uniname->name + 13 * (order - 1);
2206 order = (s32)ext_ep->order;
2210 len = extract_uni_name_from_ext_entry(ext_ep, entry_uniname, order);
2212 unichar = *(uniname + len);
2213 *(uniname + len) = 0x0;
2215 if (nls_uniname_cmp(sb, uniname, entry_uniname))
2216 is_feasible_entry = false;
2218 *(uniname + len) = unichar;
2220 has_ext_entry = true;
2221 } else if (entry_type == TYPE_UNUSED) {
2224 is_feasible_entry = true;
2225 has_ext_entry = false;
2228 if (p_dir->dir == CLUSTER_32(0))
2229 break; /* FAT16 root_dir */
2231 if (FAT_read(sb, clu.dir, &clu.dir) != 0)
2238 /* return values of exfat_find_dir_entry()
2239 * >= 0 : return dir entiry position with the name in dir
2240 * -1 : (root dir, ".") it is the root dir itself
2241 * -2 : entry with the name does not exist
2243 s32 exfat_find_dir_entry(struct super_block *sb, struct chain_t *p_dir,
2244 struct uni_name_t *p_uniname, s32 num_entries,
2245 struct dos_name_t *p_dosname, u32 type)
2247 int i = 0, dentry = 0, num_ext_entries = 0, len, step;
2249 bool is_feasible_entry = false;
2250 s32 dentries_per_clu, num_empty = 0;
2252 u16 entry_uniname[16], *uniname = NULL, unichar;
2254 struct dentry_t *ep;
2255 struct file_dentry_t *file_ep;
2256 struct strm_dentry_t *strm_ep;
2257 struct name_dentry_t *name_ep;
2258 struct fs_info_t *p_fs = &(EXFAT_SB(sb)->fs_info);
2260 if (p_dir->dir == p_fs->root_dir) {
2261 if ((!nls_uniname_cmp(sb, p_uniname->name,
2262 (u16 *)UNI_CUR_DIR_NAME)) ||
2263 (!nls_uniname_cmp(sb, p_uniname->name,
2264 (u16 *)UNI_PAR_DIR_NAME)))
2265 return -1; // special case, root directory itself
2268 if (p_dir->dir == CLUSTER_32(0)) /* FAT16 root_dir */
2269 dentries_per_clu = p_fs->dentries_in_root;
2271 dentries_per_clu = p_fs->dentries_per_clu;
2273 clu.dir = p_dir->dir;
2274 clu.size = p_dir->size;
2275 clu.flags = p_dir->flags;
2277 p_fs->hint_uentry.dir = p_dir->dir;
2278 p_fs->hint_uentry.entry = -1;
2280 while (clu.dir != CLUSTER_32(~0)) {
2281 if (p_fs->dev_ejected)
2284 while (i < dentries_per_clu) {
2285 ep = get_entry_in_dir(sb, &clu, i, NULL);
2289 entry_type = p_fs->fs_func->get_entry_type(ep);
2292 if ((entry_type == TYPE_UNUSED) || (entry_type == TYPE_DELETED)) {
2293 is_feasible_entry = false;
2295 if (p_fs->hint_uentry.entry == -1) {
2298 if (num_empty == 1) {
2299 p_fs->hint_uentry.clu.dir = clu.dir;
2300 p_fs->hint_uentry.clu.size = clu.size;
2301 p_fs->hint_uentry.clu.flags = clu.flags;
2303 if ((num_empty >= num_entries) || (entry_type == TYPE_UNUSED))
2304 p_fs->hint_uentry.entry = dentry - (num_empty - 1);
2307 if (entry_type == TYPE_UNUSED)
2312 if ((entry_type == TYPE_FILE) || (entry_type == TYPE_DIR)) {
2313 file_ep = (struct file_dentry_t *)ep;
2314 if ((type == TYPE_ALL) || (type == entry_type)) {
2315 num_ext_entries = file_ep->num_ext;
2316 is_feasible_entry = true;
2318 is_feasible_entry = false;
2319 step = file_ep->num_ext + 1;
2321 } else if (entry_type == TYPE_STREAM) {
2322 if (is_feasible_entry) {
2323 strm_ep = (struct strm_dentry_t *)ep;
2324 if (p_uniname->name_hash == GET16_A(strm_ep->name_hash) &&
2325 p_uniname->name_len == strm_ep->name_len) {
2328 is_feasible_entry = false;
2329 step = num_ext_entries;
2332 } else if (entry_type == TYPE_EXTEND) {
2333 if (is_feasible_entry) {
2334 name_ep = (struct name_dentry_t *)ep;
2337 uniname = p_uniname->name;
2341 len = extract_uni_name_from_name_entry(name_ep,
2342 entry_uniname, order);
2344 unichar = *(uniname + len);
2345 *(uniname + len) = 0x0;
2347 if (nls_uniname_cmp(sb, uniname, entry_uniname)) {
2348 is_feasible_entry = false;
2349 step = num_ext_entries - order + 1;
2350 } else if (order == num_ext_entries) {
2351 p_fs->hint_uentry.dir = CLUSTER_32(~0);
2352 p_fs->hint_uentry.entry = -1;
2353 return dentry - (num_ext_entries);
2356 *(uniname + len) = unichar;
2359 is_feasible_entry = false;
2367 i -= dentries_per_clu;
2369 if (p_dir->dir == CLUSTER_32(0))
2370 break; /* FAT16 root_dir */
2372 if (clu.flags == 0x03) {
2373 if ((--clu.size) > 0)
2376 clu.dir = CLUSTER_32(~0);
2378 if (FAT_read(sb, clu.dir, &clu.dir) != 0)
2386 s32 fat_count_ext_entries(struct super_block *sb, struct chain_t *p_dir,
2387 s32 entry, struct dentry_t *p_entry)
2391 struct dos_dentry_t *dos_ep = (struct dos_dentry_t *)p_entry;
2392 struct ext_dentry_t *ext_ep;
2393 struct fs_info_t *p_fs = &(EXFAT_SB(sb)->fs_info);
2395 chksum = calc_checksum_1byte((void *)dos_ep->name, DOS_NAME_LENGTH, 0);
2397 for (entry--; entry >= 0; entry--) {
2398 ext_ep = (struct ext_dentry_t *)get_entry_in_dir(sb, p_dir,
2403 if ((p_fs->fs_func->get_entry_type((struct dentry_t *)ext_ep) ==
2404 TYPE_EXTEND) && (ext_ep->checksum == chksum)) {
2406 if (ext_ep->order > 0x40)
2416 s32 exfat_count_ext_entries(struct super_block *sb, struct chain_t *p_dir,
2417 s32 entry, struct dentry_t *p_entry)
2421 struct file_dentry_t *file_ep = (struct file_dentry_t *)p_entry;
2422 struct dentry_t *ext_ep;
2423 struct fs_info_t *p_fs = &(EXFAT_SB(sb)->fs_info);
2425 for (i = 0, entry++; i < file_ep->num_ext; i++, entry++) {
2426 ext_ep = get_entry_in_dir(sb, p_dir, entry, NULL);
2430 type = p_fs->fs_func->get_entry_type(ext_ep);
2431 if ((type == TYPE_EXTEND) || (type == TYPE_STREAM))
2440 s32 count_dos_name_entries(struct super_block *sb, struct chain_t *p_dir,
2444 s32 dentries_per_clu;
2447 struct dentry_t *ep;
2448 struct fs_info_t *p_fs = &(EXFAT_SB(sb)->fs_info);
2450 if (p_dir->dir == CLUSTER_32(0)) /* FAT16 root_dir */
2451 dentries_per_clu = p_fs->dentries_in_root;
2453 dentries_per_clu = p_fs->dentries_per_clu;
2455 clu.dir = p_dir->dir;
2456 clu.size = p_dir->size;
2457 clu.flags = p_dir->flags;
2459 while (clu.dir != CLUSTER_32(~0)) {
2460 if (p_fs->dev_ejected)
2463 for (i = 0; i < dentries_per_clu; i++) {
2464 ep = get_entry_in_dir(sb, &clu, i, NULL);
2468 entry_type = p_fs->fs_func->get_entry_type(ep);
2470 if (entry_type == TYPE_UNUSED)
2472 if (!(type & TYPE_CRITICAL_PRI) &&
2473 !(type & TYPE_BENIGN_PRI))
2476 if ((type == TYPE_ALL) || (type == entry_type))
2480 if (p_dir->dir == CLUSTER_32(0))
2481 break; /* FAT16 root_dir */
2483 if (clu.flags == 0x03) {
2484 if ((--clu.size) > 0)
2487 clu.dir = CLUSTER_32(~0);
2489 if (FAT_read(sb, clu.dir, &clu.dir) != 0)
2497 bool is_dir_empty(struct super_block *sb, struct chain_t *p_dir)
2500 s32 dentries_per_clu;
2503 struct dentry_t *ep;
2504 struct fs_info_t *p_fs = &(EXFAT_SB(sb)->fs_info);
2506 if (p_dir->dir == CLUSTER_32(0)) /* FAT16 root_dir */
2507 dentries_per_clu = p_fs->dentries_in_root;
2509 dentries_per_clu = p_fs->dentries_per_clu;
2511 clu.dir = p_dir->dir;
2512 clu.size = p_dir->size;
2513 clu.flags = p_dir->flags;
2515 while (clu.dir != CLUSTER_32(~0)) {
2516 if (p_fs->dev_ejected)
2519 for (i = 0; i < dentries_per_clu; i++) {
2520 ep = get_entry_in_dir(sb, &clu, i, NULL);
2524 type = p_fs->fs_func->get_entry_type(ep);
2526 if (type == TYPE_UNUSED)
2528 if ((type != TYPE_FILE) && (type != TYPE_DIR))
2531 if (p_dir->dir == CLUSTER_32(0)) /* FAT16 root_dir */
2534 if (p_fs->vol_type == EXFAT)
2536 if ((p_dir->dir == p_fs->root_dir) || ((++count) > 2))
2540 if (p_dir->dir == CLUSTER_32(0))
2541 break; /* FAT16 root_dir */
2543 if (clu.flags == 0x03) {
2544 if ((--clu.size) > 0)
2547 clu.dir = CLUSTER_32(~0);
2549 if (FAT_read(sb, clu.dir, &clu.dir) != 0)
2557 * Name Conversion Functions
2560 /* input : dir, uni_name
2561 * output : num_of_entry, dos_name(format : aaaaaa~1.bbb)
2563 s32 get_num_entries_and_dos_name(struct super_block *sb, struct chain_t *p_dir,
2564 struct uni_name_t *p_uniname, s32 *entries,
2565 struct dos_name_t *p_dosname)
2567 s32 ret, num_entries;
2570 struct fs_info_t *p_fs = &(EXFAT_SB(sb)->fs_info);
2572 num_entries = p_fs->fs_func->calc_num_entries(p_uniname);
2573 if (num_entries == 0)
2574 return FFS_INVALIDPATH;
2576 if (p_fs->vol_type != EXFAT) {
2577 nls_uniname_to_dosname(sb, p_dosname, p_uniname, &lossy);
2580 ret = fat_generate_dos_name(sb, p_dir, p_dosname);
2584 for (r = reserved_names; *r; r++) {
2585 if (!strncmp((void *)p_dosname->name, *r, 8))
2586 return FFS_INVALIDPATH;
2589 if (p_dosname->name_case != 0xFF)
2593 if (num_entries > 1)
2594 p_dosname->name_case = 0x0;
2597 *entries = num_entries;
2602 void get_uni_name_from_dos_entry(struct super_block *sb,
2603 struct dos_dentry_t *ep,
2604 struct uni_name_t *p_uniname, u8 mode)
2606 struct dos_name_t dos_name;
2609 dos_name.name_case = 0x0;
2611 dos_name.name_case = ep->lcase;
2613 memcpy(dos_name.name, ep->name, DOS_NAME_LENGTH);
2614 nls_dosname_to_uniname(sb, p_uniname, &dos_name);
2617 void fat_get_uni_name_from_ext_entry(struct super_block *sb,
2618 struct chain_t *p_dir, s32 entry,
2622 struct ext_dentry_t *ep;
2623 struct fs_info_t *p_fs = &(EXFAT_SB(sb)->fs_info);
2625 for (entry--, i = 1; entry >= 0; entry--, i++) {
2626 ep = (struct ext_dentry_t *)get_entry_in_dir(sb, p_dir, entry,
2631 if (p_fs->fs_func->get_entry_type((struct dentry_t *)ep) ==
2633 extract_uni_name_from_ext_entry(ep, uniname, i);
2634 if (ep->order > 0x40)
2644 void exfat_get_uni_name_from_ext_entry(struct super_block *sb,
2645 struct chain_t *p_dir, s32 entry,
2649 struct dentry_t *ep;
2650 struct entry_set_cache_t *es;
2651 struct fs_info_t *p_fs = &(EXFAT_SB(sb)->fs_info);
2653 es = get_entry_set_in_dir(sb, p_dir, entry, ES_ALL_ENTRIES, &ep);
2654 if (!es || es->num_entries < 3) {
2656 release_entry_set(es);
2663 * First entry : file entry
2664 * Second entry : stream-extension entry
2665 * Third entry : first file-name entry
2666 * So, the index of first file-name dentry should start from 2.
2668 for (i = 2; i < es->num_entries; i++, ep++) {
2669 if (p_fs->fs_func->get_entry_type(ep) == TYPE_EXTEND)
2670 extract_uni_name_from_name_entry((struct name_dentry_t *)
2678 release_entry_set(es);
2681 s32 extract_uni_name_from_ext_entry(struct ext_dentry_t *ep, u16 *uniname,
2686 for (i = 0; i < 10; i += 2) {
2687 *uniname = GET16(ep->unicode_0_4 + i);
2688 if (*uniname == 0x0)
2695 for (i = 0; i < 12; i += 2) {
2696 *uniname = GET16_A(ep->unicode_5_10 + i);
2697 if (*uniname == 0x0)
2703 for (i = 0; i < 8; i += 2) {
2704 *uniname = GET16_A(ep->unicode_5_10 + i);
2705 if (*uniname == 0x0)
2710 *uniname = 0x0; /* uniname[MAX_NAME_LENGTH-1] */
2714 for (i = 0; i < 4; i += 2) {
2715 *uniname = GET16_A(ep->unicode_11_12 + i);
2716 if (*uniname == 0x0)
2726 s32 extract_uni_name_from_name_entry(struct name_dentry_t *ep, u16 *uniname,
2731 for (i = 0; i < 30; i += 2) {
2732 *uniname = GET16_A(ep->unicode_0_14 + i);
2733 if (*uniname == 0x0)
2743 s32 fat_generate_dos_name(struct super_block *sb, struct chain_t *p_dir,
2744 struct dos_name_t *p_dosname)
2746 int i, j, count = 0;
2747 bool count_begin = false;
2748 s32 dentries_per_clu;
2750 u8 bmap[128/* 1 ~ 1023 */];
2752 struct dos_dentry_t *ep;
2753 struct fs_info_t *p_fs = &(EXFAT_SB(sb)->fs_info);
2755 memset(bmap, 0, sizeof(bmap));
2756 exfat_bitmap_set(bmap, 0);
2758 if (p_dir->dir == CLUSTER_32(0)) /* FAT16 root_dir */
2759 dentries_per_clu = p_fs->dentries_in_root;
2761 dentries_per_clu = p_fs->dentries_per_clu;
2763 clu.dir = p_dir->dir;
2764 clu.flags = p_dir->flags;
2766 while (clu.dir != CLUSTER_32(~0)) {
2767 if (p_fs->dev_ejected)
2770 for (i = 0; i < dentries_per_clu; i++) {
2771 ep = (struct dos_dentry_t *)get_entry_in_dir(sb, &clu,
2774 return FFS_MEDIAERR;
2776 type = p_fs->fs_func->get_entry_type((struct dentry_t *)
2779 if (type == TYPE_UNUSED)
2781 if ((type != TYPE_FILE) && (type != TYPE_DIR))
2785 count_begin = false;
2787 for (j = 0; j < 8; j++) {
2788 if (ep->name[j] == ' ')
2791 if (ep->name[j] == '~') {
2793 } else if (count_begin) {
2794 if ((ep->name[j] >= '0') &&
2795 (ep->name[j] <= '9')) {
2796 count = count * 10 +
2797 (ep->name[j] - '0');
2800 count_begin = false;
2805 if ((count > 0) && (count < 1024))
2806 exfat_bitmap_set(bmap, count);
2809 if (p_dir->dir == CLUSTER_32(0))
2810 break; /* FAT16 root_dir */
2812 if (FAT_read(sb, clu.dir, &clu.dir) != 0)
2813 return FFS_MEDIAERR;
2817 for (i = 0; i < 128; i++) {
2818 if (bmap[i] != 0xFF) {
2819 for (j = 0; j < 8; j++) {
2820 if (exfat_bitmap_test(&bmap[i], j) == 0) {
2821 count = (i << 3) + j;
2830 if ((count == 0) || (count >= 1024))
2831 return FFS_FILEEXIST;
2832 fat_attach_count_to_dos_name(p_dosname->name, count);
2834 /* Now dos_name has DOS~????.EXT */
2838 void fat_attach_count_to_dos_name(u8 *dosname, s32 count)
2843 snprintf(str_count, sizeof(str_count), "~%d", count);
2844 length = strlen(str_count);
2848 while (j <= (8 - length)) {
2850 if (dosname[j] == ' ')
2852 if (dosname[j] & 0x80)
2858 for (j = 0; j < length; i++, j++)
2859 dosname[i] = (u8)str_count[j];
2865 s32 fat_calc_num_entries(struct uni_name_t *p_uniname)
2869 len = p_uniname->name_len;
2873 /* 1 dos name entry + extended entries */
2874 return (len - 1) / 13 + 2;
2877 s32 exfat_calc_num_entries(struct uni_name_t *p_uniname)
2881 len = p_uniname->name_len;
2885 /* 1 file entry + 1 stream entry + name entries */
2886 return (len - 1) / 15 + 3;
2889 u8 calc_checksum_1byte(void *data, s32 len, u8 chksum)
2894 for (i = 0; i < len; i++, c++)
2895 chksum = (((chksum & 1) << 7) | ((chksum & 0xFE) >> 1)) + *c;
2900 u16 calc_checksum_2byte(void *data, s32 len, u16 chksum, s32 type)
2907 for (i = 0; i < len; i++, c++) {
2908 if ((i == 2) || (i == 3))
2910 chksum = (((chksum & 1) << 15) |
2911 ((chksum & 0xFFFE) >> 1)) + (u16)*c;
2916 for (i = 0; i < len; i++, c++)
2917 chksum = (((chksum & 1) << 15) |
2918 ((chksum & 0xFFFE) >> 1)) + (u16)*c;
2924 u32 calc_checksum_4byte(void *data, s32 len, u32 chksum, s32 type)
2931 for (i = 0; i < len; i++, c++) {
2932 if ((i == 106) || (i == 107) || (i == 112))
2934 chksum = (((chksum & 1) << 31) |
2935 ((chksum & 0xFFFFFFFE) >> 1)) + (u32)*c;
2940 for (i = 0; i < len; i++, c++)
2941 chksum = (((chksum & 1) << 31) |
2942 ((chksum & 0xFFFFFFFE) >> 1)) + (u32)*c;
2949 * Name Resolution Functions
2952 /* return values of resolve_path()
2953 * > 0 : return the length of the path
2954 * < 0 : return error
2956 s32 resolve_path(struct inode *inode, char *path, struct chain_t *p_dir,
2957 struct uni_name_t *p_uniname)
2960 struct super_block *sb = inode->i_sb;
2961 struct fs_info_t *p_fs = &(EXFAT_SB(sb)->fs_info);
2962 struct file_id_t *fid = &(EXFAT_I(inode)->fid);
2964 if (strscpy(name_buf, path, sizeof(name_buf)) < 0)
2965 return FFS_INVALIDPATH;
2967 nls_cstring_to_uniname(sb, p_uniname, name_buf, &lossy);
2969 return FFS_INVALIDPATH;
2971 fid->size = i_size_read(inode);
2973 p_dir->dir = fid->start_clu;
2974 p_dir->size = (s32)(fid->size >> p_fs->cluster_size_bits);
2975 p_dir->flags = fid->flags;
2981 * File Operation Functions
2983 static struct fs_func fat_fs_func = {
2984 .alloc_cluster = fat_alloc_cluster,
2985 .free_cluster = fat_free_cluster,
2986 .count_used_clusters = fat_count_used_clusters,
2988 .init_dir_entry = fat_init_dir_entry,
2989 .init_ext_entry = fat_init_ext_entry,
2990 .find_dir_entry = fat_find_dir_entry,
2991 .delete_dir_entry = fat_delete_dir_entry,
2992 .get_uni_name_from_ext_entry = fat_get_uni_name_from_ext_entry,
2993 .count_ext_entries = fat_count_ext_entries,
2994 .calc_num_entries = fat_calc_num_entries,
2996 .get_entry_type = fat_get_entry_type,
2997 .set_entry_type = fat_set_entry_type,
2998 .get_entry_attr = fat_get_entry_attr,
2999 .set_entry_attr = fat_set_entry_attr,
3000 .get_entry_flag = fat_get_entry_flag,
3001 .set_entry_flag = fat_set_entry_flag,
3002 .get_entry_clu0 = fat_get_entry_clu0,
3003 .set_entry_clu0 = fat_set_entry_clu0,
3004 .get_entry_size = fat_get_entry_size,
3005 .set_entry_size = fat_set_entry_size,
3006 .get_entry_time = fat_get_entry_time,
3007 .set_entry_time = fat_set_entry_time,
3010 s32 fat16_mount(struct super_block *sb, struct pbr_sector_t *p_pbr)
3012 s32 num_reserved, num_root_sectors;
3013 struct bpb16_t *p_bpb = (struct bpb16_t *)p_pbr->bpb;
3014 struct fs_info_t *p_fs = &(EXFAT_SB(sb)->fs_info);
3015 struct bd_info_t *p_bd = &(EXFAT_SB(sb)->bd_info);
3017 if (p_bpb->num_fats == 0)
3018 return FFS_FORMATERR;
3020 num_root_sectors = GET16(p_bpb->num_root_entries) << DENTRY_SIZE_BITS;
3021 num_root_sectors = ((num_root_sectors - 1) >>
3022 p_bd->sector_size_bits) + 1;
3024 p_fs->sectors_per_clu = p_bpb->sectors_per_clu;
3025 p_fs->sectors_per_clu_bits = ilog2(p_bpb->sectors_per_clu);
3026 p_fs->cluster_size_bits = p_fs->sectors_per_clu_bits +
3027 p_bd->sector_size_bits;
3028 p_fs->cluster_size = 1 << p_fs->cluster_size_bits;
3030 p_fs->num_FAT_sectors = GET16(p_bpb->num_fat_sectors);
3032 p_fs->FAT1_start_sector = p_fs->PBR_sector + GET16(p_bpb->num_reserved);
3033 if (p_bpb->num_fats == 1)
3034 p_fs->FAT2_start_sector = p_fs->FAT1_start_sector;
3036 p_fs->FAT2_start_sector = p_fs->FAT1_start_sector +
3037 p_fs->num_FAT_sectors;
3039 p_fs->root_start_sector = p_fs->FAT2_start_sector +
3040 p_fs->num_FAT_sectors;
3041 p_fs->data_start_sector = p_fs->root_start_sector + num_root_sectors;
3043 p_fs->num_sectors = GET16(p_bpb->num_sectors);
3044 if (p_fs->num_sectors == 0)
3045 p_fs->num_sectors = GET32(p_bpb->num_huge_sectors);
3047 num_reserved = p_fs->data_start_sector - p_fs->PBR_sector;
3048 p_fs->num_clusters = ((p_fs->num_sectors - num_reserved) >>
3049 p_fs->sectors_per_clu_bits) + 2;
3050 /* because the cluster index starts with 2 */
3052 if (p_fs->num_clusters < FAT12_THRESHOLD)
3053 p_fs->vol_type = FAT12;
3055 p_fs->vol_type = FAT16;
3056 p_fs->vol_id = GET32(p_bpb->vol_serial);
3059 p_fs->dentries_in_root = GET16(p_bpb->num_root_entries);
3060 p_fs->dentries_per_clu = 1 << (p_fs->cluster_size_bits -
3063 p_fs->vol_flag = VOL_CLEAN;
3064 p_fs->clu_srch_ptr = 2;
3065 p_fs->used_clusters = UINT_MAX;
3067 p_fs->fs_func = &fat_fs_func;
3072 s32 fat32_mount(struct super_block *sb, struct pbr_sector_t *p_pbr)
3075 struct bpb32_t *p_bpb = (struct bpb32_t *)p_pbr->bpb;
3076 struct fs_info_t *p_fs = &(EXFAT_SB(sb)->fs_info);
3077 struct bd_info_t *p_bd = &(EXFAT_SB(sb)->bd_info);
3079 if (p_bpb->num_fats == 0)
3080 return FFS_FORMATERR;
3082 p_fs->sectors_per_clu = p_bpb->sectors_per_clu;
3083 p_fs->sectors_per_clu_bits = ilog2(p_bpb->sectors_per_clu);
3084 p_fs->cluster_size_bits = p_fs->sectors_per_clu_bits +
3085 p_bd->sector_size_bits;
3086 p_fs->cluster_size = 1 << p_fs->cluster_size_bits;
3088 p_fs->num_FAT_sectors = GET32(p_bpb->num_fat32_sectors);
3090 p_fs->FAT1_start_sector = p_fs->PBR_sector + GET16(p_bpb->num_reserved);
3091 if (p_bpb->num_fats == 1)
3092 p_fs->FAT2_start_sector = p_fs->FAT1_start_sector;
3094 p_fs->FAT2_start_sector = p_fs->FAT1_start_sector +
3095 p_fs->num_FAT_sectors;
3097 p_fs->root_start_sector = p_fs->FAT2_start_sector +
3098 p_fs->num_FAT_sectors;
3099 p_fs->data_start_sector = p_fs->root_start_sector;
3101 p_fs->num_sectors = GET32(p_bpb->num_huge_sectors);
3102 num_reserved = p_fs->data_start_sector - p_fs->PBR_sector;
3104 p_fs->num_clusters = ((p_fs->num_sectors - num_reserved) >>
3105 p_fs->sectors_per_clu_bits) + 2;
3106 /* because the cluster index starts with 2 */
3108 p_fs->vol_type = FAT32;
3109 p_fs->vol_id = GET32(p_bpb->vol_serial);
3111 p_fs->root_dir = GET32(p_bpb->root_cluster);
3112 p_fs->dentries_in_root = 0;
3113 p_fs->dentries_per_clu = 1 << (p_fs->cluster_size_bits -
3116 p_fs->vol_flag = VOL_CLEAN;
3117 p_fs->clu_srch_ptr = 2;
3118 p_fs->used_clusters = UINT_MAX;
3120 p_fs->fs_func = &fat_fs_func;
3125 static struct fs_func exfat_fs_func = {
3126 .alloc_cluster = exfat_alloc_cluster,
3127 .free_cluster = exfat_free_cluster,
3128 .count_used_clusters = exfat_count_used_clusters,
3130 .init_dir_entry = exfat_init_dir_entry,
3131 .init_ext_entry = exfat_init_ext_entry,
3132 .find_dir_entry = exfat_find_dir_entry,
3133 .delete_dir_entry = exfat_delete_dir_entry,
3134 .get_uni_name_from_ext_entry = exfat_get_uni_name_from_ext_entry,
3135 .count_ext_entries = exfat_count_ext_entries,
3136 .calc_num_entries = exfat_calc_num_entries,
3138 .get_entry_type = exfat_get_entry_type,
3139 .set_entry_type = exfat_set_entry_type,
3140 .get_entry_attr = exfat_get_entry_attr,
3141 .set_entry_attr = exfat_set_entry_attr,
3142 .get_entry_flag = exfat_get_entry_flag,
3143 .set_entry_flag = exfat_set_entry_flag,
3144 .get_entry_clu0 = exfat_get_entry_clu0,
3145 .set_entry_clu0 = exfat_set_entry_clu0,
3146 .get_entry_size = exfat_get_entry_size,
3147 .set_entry_size = exfat_set_entry_size,
3148 .get_entry_time = exfat_get_entry_time,
3149 .set_entry_time = exfat_set_entry_time,
3152 s32 exfat_mount(struct super_block *sb, struct pbr_sector_t *p_pbr)
3154 struct bpbex_t *p_bpb = (struct bpbex_t *)p_pbr->bpb;
3155 struct fs_info_t *p_fs = &(EXFAT_SB(sb)->fs_info);
3156 struct bd_info_t *p_bd = &(EXFAT_SB(sb)->bd_info);
3158 if (p_bpb->num_fats == 0)
3159 return FFS_FORMATERR;
3161 p_fs->sectors_per_clu = 1 << p_bpb->sectors_per_clu_bits;
3162 p_fs->sectors_per_clu_bits = p_bpb->sectors_per_clu_bits;
3163 p_fs->cluster_size_bits = p_fs->sectors_per_clu_bits +
3164 p_bd->sector_size_bits;
3165 p_fs->cluster_size = 1 << p_fs->cluster_size_bits;
3167 p_fs->num_FAT_sectors = GET32(p_bpb->fat_length);
3169 p_fs->FAT1_start_sector = p_fs->PBR_sector + GET32(p_bpb->fat_offset);
3170 if (p_bpb->num_fats == 1)
3171 p_fs->FAT2_start_sector = p_fs->FAT1_start_sector;
3173 p_fs->FAT2_start_sector = p_fs->FAT1_start_sector +
3174 p_fs->num_FAT_sectors;
3176 p_fs->root_start_sector = p_fs->PBR_sector + GET32(p_bpb->clu_offset);
3177 p_fs->data_start_sector = p_fs->root_start_sector;
3179 p_fs->num_sectors = GET64(p_bpb->vol_length);
3180 p_fs->num_clusters = GET32(p_bpb->clu_count) + 2;
3181 /* because the cluster index starts with 2 */
3183 p_fs->vol_type = EXFAT;
3184 p_fs->vol_id = GET32(p_bpb->vol_serial);
3186 p_fs->root_dir = GET32(p_bpb->root_cluster);
3187 p_fs->dentries_in_root = 0;
3188 p_fs->dentries_per_clu = 1 << (p_fs->cluster_size_bits -
3191 p_fs->vol_flag = (u32)GET16(p_bpb->vol_flags);
3192 p_fs->clu_srch_ptr = 2;
3193 p_fs->used_clusters = UINT_MAX;
3195 p_fs->fs_func = &exfat_fs_func;
3200 s32 create_dir(struct inode *inode, struct chain_t *p_dir,
3201 struct uni_name_t *p_uniname, struct file_id_t *fid)
3203 s32 ret, dentry, num_entries;
3206 struct dos_name_t dos_name, dot_name;
3207 struct super_block *sb = inode->i_sb;
3208 struct fs_info_t *p_fs = &(EXFAT_SB(sb)->fs_info);
3209 struct fs_func *fs_func = p_fs->fs_func;
3211 ret = get_num_entries_and_dos_name(sb, p_dir, p_uniname, &num_entries,
3216 /* find_empty_entry must be called before alloc_cluster */
3217 dentry = find_empty_entry(inode, p_dir, num_entries);
3221 clu.dir = CLUSTER_32(~0);
3223 clu.flags = (p_fs->vol_type == EXFAT) ? 0x03 : 0x01;
3225 /* (1) allocate a cluster */
3226 ret = fs_func->alloc_cluster(sb, 1, &clu);
3228 return FFS_MEDIAERR;
3232 ret = clear_cluster(sb, clu.dir);
3233 if (ret != FFS_SUCCESS)
3236 if (p_fs->vol_type == EXFAT) {
3237 size = p_fs->cluster_size;
3241 /* initialize the . and .. entry
3242 * Information for . points to itself
3243 * Information for .. points to parent dir
3246 dot_name.name_case = 0x0;
3247 memcpy(dot_name.name, DOS_CUR_DIR_NAME, DOS_NAME_LENGTH);
3249 ret = fs_func->init_dir_entry(sb, &clu, 0, TYPE_DIR, clu.dir,
3251 if (ret != FFS_SUCCESS)
3254 ret = fs_func->init_ext_entry(sb, &clu, 0, 1, NULL, &dot_name);
3255 if (ret != FFS_SUCCESS)
3258 memcpy(dot_name.name, DOS_PAR_DIR_NAME, DOS_NAME_LENGTH);
3260 if (p_dir->dir == p_fs->root_dir)
3261 ret = fs_func->init_dir_entry(sb, &clu, 1, TYPE_DIR,
3264 ret = fs_func->init_dir_entry(sb, &clu, 1, TYPE_DIR,
3267 if (ret != FFS_SUCCESS)
3270 ret = p_fs->fs_func->init_ext_entry(sb, &clu, 1, 1, NULL,
3272 if (ret != FFS_SUCCESS)
3276 /* (2) update the directory entry */
3277 /* make sub-dir entry in parent directory */
3278 ret = fs_func->init_dir_entry(sb, p_dir, dentry, TYPE_DIR, clu.dir,
3280 if (ret != FFS_SUCCESS)
3283 ret = fs_func->init_ext_entry(sb, p_dir, dentry, num_entries, p_uniname,
3285 if (ret != FFS_SUCCESS)
3288 fid->dir.dir = p_dir->dir;
3289 fid->dir.size = p_dir->size;
3290 fid->dir.flags = p_dir->flags;
3291 fid->entry = dentry;
3293 fid->attr = ATTR_SUBDIR;
3294 fid->flags = (p_fs->vol_type == EXFAT) ? 0x03 : 0x01;
3296 fid->start_clu = clu.dir;
3298 fid->type = TYPE_DIR;
3300 fid->hint_last_off = -1;
3305 s32 create_file(struct inode *inode, struct chain_t *p_dir,
3306 struct uni_name_t *p_uniname, u8 mode, struct file_id_t *fid)
3308 s32 ret, dentry, num_entries;
3309 struct dos_name_t dos_name;
3310 struct super_block *sb = inode->i_sb;
3311 struct fs_info_t *p_fs = &(EXFAT_SB(sb)->fs_info);
3312 struct fs_func *fs_func = p_fs->fs_func;
3314 ret = get_num_entries_and_dos_name(sb, p_dir, p_uniname, &num_entries,
3319 /* find_empty_entry must be called before alloc_cluster() */
3320 dentry = find_empty_entry(inode, p_dir, num_entries);
3324 /* (1) update the directory entry */
3325 /* fill the dos name directory entry information of the created file.
3326 * the first cluster is not determined yet. (0)
3328 ret = fs_func->init_dir_entry(sb, p_dir, dentry, TYPE_FILE | mode,
3330 if (ret != FFS_SUCCESS)
3333 ret = fs_func->init_ext_entry(sb, p_dir, dentry, num_entries, p_uniname,
3335 if (ret != FFS_SUCCESS)
3338 fid->dir.dir = p_dir->dir;
3339 fid->dir.size = p_dir->size;
3340 fid->dir.flags = p_dir->flags;
3341 fid->entry = dentry;
3343 fid->attr = ATTR_ARCHIVE | mode;
3344 fid->flags = (p_fs->vol_type == EXFAT) ? 0x03 : 0x01;
3346 fid->start_clu = CLUSTER_32(~0);
3348 fid->type = TYPE_FILE;
3350 fid->hint_last_off = -1;
3355 void remove_file(struct inode *inode, struct chain_t *p_dir, s32 entry)
3359 struct dentry_t *ep;
3360 struct super_block *sb = inode->i_sb;
3361 struct fs_info_t *p_fs = &(EXFAT_SB(sb)->fs_info);
3362 struct fs_func *fs_func = p_fs->fs_func;
3364 ep = get_entry_in_dir(sb, p_dir, entry, §or);
3368 buf_lock(sb, sector);
3370 /* buf_lock() before call count_ext_entries() */
3371 num_entries = fs_func->count_ext_entries(sb, p_dir, entry, ep);
3372 if (num_entries < 0) {
3373 buf_unlock(sb, sector);
3378 buf_unlock(sb, sector);
3380 /* (1) update the directory entry */
3381 fs_func->delete_dir_entry(sb, p_dir, entry, 0, num_entries);
3384 s32 exfat_rename_file(struct inode *inode, struct chain_t *p_dir, s32 oldentry,
3385 struct uni_name_t *p_uniname, struct file_id_t *fid)
3387 s32 ret, newentry = -1, num_old_entries, num_new_entries;
3388 sector_t sector_old, sector_new;
3389 struct dos_name_t dos_name;
3390 struct dentry_t *epold, *epnew;
3391 struct super_block *sb = inode->i_sb;
3392 struct fs_info_t *p_fs = &(EXFAT_SB(sb)->fs_info);
3393 struct fs_func *fs_func = p_fs->fs_func;
3395 epold = get_entry_in_dir(sb, p_dir, oldentry, §or_old);
3397 return FFS_MEDIAERR;
3399 buf_lock(sb, sector_old);
3401 /* buf_lock() before call count_ext_entries() */
3402 num_old_entries = fs_func->count_ext_entries(sb, p_dir, oldentry,
3404 if (num_old_entries < 0) {
3405 buf_unlock(sb, sector_old);
3406 return FFS_MEDIAERR;
3410 ret = get_num_entries_and_dos_name(sb, p_dir, p_uniname,
3411 &num_new_entries, &dos_name);
3413 buf_unlock(sb, sector_old);
3417 if (num_old_entries < num_new_entries) {
3418 newentry = find_empty_entry(inode, p_dir, num_new_entries);
3420 buf_unlock(sb, sector_old);
3424 epnew = get_entry_in_dir(sb, p_dir, newentry, §or_new);
3426 buf_unlock(sb, sector_old);
3427 return FFS_MEDIAERR;
3430 memcpy((void *)epnew, (void *)epold, DENTRY_SIZE);
3431 if (fs_func->get_entry_type(epnew) == TYPE_FILE) {
3432 fs_func->set_entry_attr(epnew,
3433 fs_func->get_entry_attr(epnew) |
3435 fid->attr |= ATTR_ARCHIVE;
3437 buf_modify(sb, sector_new);
3438 buf_unlock(sb, sector_old);
3440 if (p_fs->vol_type == EXFAT) {
3441 epold = get_entry_in_dir(sb, p_dir, oldentry + 1,
3443 buf_lock(sb, sector_old);
3444 epnew = get_entry_in_dir(sb, p_dir, newentry + 1,
3447 if (!epold || !epnew) {
3448 buf_unlock(sb, sector_old);
3449 return FFS_MEDIAERR;
3452 memcpy((void *)epnew, (void *)epold, DENTRY_SIZE);
3453 buf_modify(sb, sector_new);
3454 buf_unlock(sb, sector_old);
3457 ret = fs_func->init_ext_entry(sb, p_dir, newentry,
3458 num_new_entries, p_uniname,
3460 if (ret != FFS_SUCCESS)
3463 fs_func->delete_dir_entry(sb, p_dir, oldentry, 0,
3465 fid->entry = newentry;
3467 if (fs_func->get_entry_type(epold) == TYPE_FILE) {
3468 fs_func->set_entry_attr(epold,
3469 fs_func->get_entry_attr(epold) |
3471 fid->attr |= ATTR_ARCHIVE;
3473 buf_modify(sb, sector_old);
3474 buf_unlock(sb, sector_old);
3476 ret = fs_func->init_ext_entry(sb, p_dir, oldentry,
3477 num_new_entries, p_uniname,
3479 if (ret != FFS_SUCCESS)
3482 fs_func->delete_dir_entry(sb, p_dir, oldentry, num_new_entries,
3489 s32 move_file(struct inode *inode, struct chain_t *p_olddir, s32 oldentry,
3490 struct chain_t *p_newdir, struct uni_name_t *p_uniname,
3491 struct file_id_t *fid)
3493 s32 ret, newentry, num_new_entries, num_old_entries;
3494 sector_t sector_mov, sector_new;
3496 struct dos_name_t dos_name;
3497 struct dentry_t *epmov, *epnew;
3498 struct super_block *sb = inode->i_sb;
3499 struct fs_info_t *p_fs = &(EXFAT_SB(sb)->fs_info);
3500 struct fs_func *fs_func = p_fs->fs_func;
3502 epmov = get_entry_in_dir(sb, p_olddir, oldentry, §or_mov);
3504 return FFS_MEDIAERR;
3506 /* check if the source and target directory is the same */
3507 if (fs_func->get_entry_type(epmov) == TYPE_DIR &&
3508 fs_func->get_entry_clu0(epmov) == p_newdir->dir)
3509 return FFS_INVALIDPATH;
3511 buf_lock(sb, sector_mov);
3513 /* buf_lock() before call count_ext_entries() */
3514 num_old_entries = fs_func->count_ext_entries(sb, p_olddir, oldentry,
3516 if (num_old_entries < 0) {
3517 buf_unlock(sb, sector_mov);
3518 return FFS_MEDIAERR;
3522 ret = get_num_entries_and_dos_name(sb, p_newdir, p_uniname,
3523 &num_new_entries, &dos_name);
3525 buf_unlock(sb, sector_mov);
3529 newentry = find_empty_entry(inode, p_newdir, num_new_entries);
3531 buf_unlock(sb, sector_mov);
3535 epnew = get_entry_in_dir(sb, p_newdir, newentry, §or_new);
3537 buf_unlock(sb, sector_mov);
3538 return FFS_MEDIAERR;
3541 memcpy((void *)epnew, (void *)epmov, DENTRY_SIZE);
3542 if (fs_func->get_entry_type(epnew) == TYPE_FILE) {
3543 fs_func->set_entry_attr(epnew, fs_func->get_entry_attr(epnew) |
3545 fid->attr |= ATTR_ARCHIVE;
3547 buf_modify(sb, sector_new);
3548 buf_unlock(sb, sector_mov);
3550 if (p_fs->vol_type == EXFAT) {
3551 epmov = get_entry_in_dir(sb, p_olddir, oldentry + 1,
3553 buf_lock(sb, sector_mov);
3554 epnew = get_entry_in_dir(sb, p_newdir, newentry + 1,
3556 if (!epmov || !epnew) {
3557 buf_unlock(sb, sector_mov);
3558 return FFS_MEDIAERR;
3561 memcpy((void *)epnew, (void *)epmov, DENTRY_SIZE);
3562 buf_modify(sb, sector_new);
3563 buf_unlock(sb, sector_mov);
3564 } else if (fs_func->get_entry_type(epnew) == TYPE_DIR) {
3565 /* change ".." pointer to new parent dir */
3566 clu.dir = fs_func->get_entry_clu0(epnew);
3569 epnew = get_entry_in_dir(sb, &clu, 1, §or_new);
3571 return FFS_MEDIAERR;
3573 if (p_newdir->dir == p_fs->root_dir)
3574 fs_func->set_entry_clu0(epnew, CLUSTER_32(0));
3576 fs_func->set_entry_clu0(epnew, p_newdir->dir);
3577 buf_modify(sb, sector_new);
3580 ret = fs_func->init_ext_entry(sb, p_newdir, newentry, num_new_entries,
3581 p_uniname, &dos_name);
3582 if (ret != FFS_SUCCESS)
3585 fs_func->delete_dir_entry(sb, p_olddir, oldentry, 0, num_old_entries);
3587 fid->dir.dir = p_newdir->dir;
3588 fid->dir.size = p_newdir->size;
3589 fid->dir.flags = p_newdir->flags;
3591 fid->entry = newentry;
3597 * Sector Read/Write Functions
3600 int sector_read(struct super_block *sb, sector_t sec, struct buffer_head **bh,
3603 s32 ret = FFS_MEDIAERR;
3604 struct fs_info_t *p_fs = &(EXFAT_SB(sb)->fs_info);
3606 if ((sec >= (p_fs->PBR_sector + p_fs->num_sectors)) &&
3607 (p_fs->num_sectors > 0)) {
3608 pr_err("[EXFAT] %s: out of range error! (sec = %llu)\n",
3609 __func__, (unsigned long long)sec);
3614 if (!p_fs->dev_ejected) {
3615 ret = bdev_read(sb, sec, bh, 1, read);
3616 if (ret != FFS_SUCCESS)
3617 p_fs->dev_ejected = 1;
3623 int sector_write(struct super_block *sb, sector_t sec, struct buffer_head *bh,
3626 s32 ret = FFS_MEDIAERR;
3627 struct fs_info_t *p_fs = &(EXFAT_SB(sb)->fs_info);
3629 if (sec >= (p_fs->PBR_sector + p_fs->num_sectors) &&
3630 (p_fs->num_sectors > 0)) {
3631 pr_err("[EXFAT] %s: out of range error! (sec = %llu)\n",
3632 __func__, (unsigned long long)sec);
3638 pr_err("[EXFAT] %s: bh is NULL!\n", __func__);
3643 if (!p_fs->dev_ejected) {
3644 ret = bdev_write(sb, sec, bh, 1, sync);
3645 if (ret != FFS_SUCCESS)
3646 p_fs->dev_ejected = 1;
3652 int multi_sector_read(struct super_block *sb, sector_t sec,
3653 struct buffer_head **bh, s32 num_secs, bool read)
3655 s32 ret = FFS_MEDIAERR;
3656 struct fs_info_t *p_fs = &(EXFAT_SB(sb)->fs_info);
3658 if (((sec + num_secs) > (p_fs->PBR_sector + p_fs->num_sectors)) &&
3659 (p_fs->num_sectors > 0)) {
3660 pr_err("[EXFAT] %s: out of range error! (sec = %llu, num_secs = %d)\n",
3661 __func__, (unsigned long long)sec, num_secs);
3666 if (!p_fs->dev_ejected) {
3667 ret = bdev_read(sb, sec, bh, num_secs, read);
3668 if (ret != FFS_SUCCESS)
3669 p_fs->dev_ejected = 1;
3675 int multi_sector_write(struct super_block *sb, sector_t sec,
3676 struct buffer_head *bh, s32 num_secs, bool sync)
3678 s32 ret = FFS_MEDIAERR;
3679 struct fs_info_t *p_fs = &(EXFAT_SB(sb)->fs_info);
3681 if ((sec + num_secs) > (p_fs->PBR_sector + p_fs->num_sectors) &&
3682 (p_fs->num_sectors > 0)) {
3683 pr_err("[EXFAT] %s: out of range error! (sec = %llu, num_secs = %d)\n",
3684 __func__, (unsigned long long)sec, num_secs);
3689 pr_err("[EXFAT] %s: bh is NULL!\n", __func__);
3694 if (!p_fs->dev_ejected) {
3695 ret = bdev_write(sb, sec, bh, num_secs, sync);
3696 if (ret != FFS_SUCCESS)
3697 p_fs->dev_ejected = 1;