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 #ifdef CONFIG_EXFAT_DISCARD
197 ret
= sb_issue_discard(sb
, START_SECTOR(clu
),
198 (1 << p_fs
->sectors_per_clu_bits
),
200 if (ret
== -EOPNOTSUPP
) {
201 pr_warn("discard not supported by device, disabling");
207 #endif /* CONFIG_EXFAT_DISCARD */
209 return sector_write(sb
, sector
, p_fs
->vol_amap
[i
], 0);
212 static u32
test_alloc_bitmap(struct super_block
*sb
, u32 clu
)
215 u32 clu_base
, clu_free
;
217 struct fs_info_t
*p_fs
= &(EXFAT_SB(sb
)->fs_info
);
218 struct bd_info_t
*p_bd
= &(EXFAT_SB(sb
)->bd_info
);
220 clu_base
= (clu
& ~(0x7)) + 2;
221 clu_mask
= (1 << (clu
- clu_base
+ 2)) - 1;
223 map_i
= clu
>> (p_bd
->sector_size_bits
+ 3);
224 map_b
= (clu
>> 3) & p_bd
->sector_size_mask
;
226 for (i
= 2; i
< p_fs
->num_clusters
; i
+= 8) {
227 k
= *(((u8
*)p_fs
->vol_amap
[map_i
]->b_data
) + map_b
);
233 clu_free
= clu_base
+ free_bit
[k
];
234 if (clu_free
< p_fs
->num_clusters
)
239 if (((++map_b
) >= p_bd
->sector_size
) ||
240 (clu_base
>= p_fs
->num_clusters
)) {
241 if ((++map_i
) >= p_fs
->map_sectors
) {
249 return CLUSTER_32(~0);
252 static s32
exfat_alloc_cluster(struct super_block
*sb
, s32 num_alloc
,
253 struct chain_t
*p_chain
)
255 s32 num_clusters
= 0;
256 u32 hint_clu
, new_clu
, last_clu
= CLUSTER_32(~0);
257 struct fs_info_t
*p_fs
= &(EXFAT_SB(sb
)->fs_info
);
259 hint_clu
= p_chain
->dir
;
260 if (hint_clu
== CLUSTER_32(~0)) {
261 hint_clu
= test_alloc_bitmap(sb
, p_fs
->clu_srch_ptr
- 2);
262 if (hint_clu
== CLUSTER_32(~0))
264 } else if (hint_clu
>= p_fs
->num_clusters
) {
266 p_chain
->flags
= 0x01;
271 p_chain
->dir
= CLUSTER_32(~0);
273 while ((new_clu
= test_alloc_bitmap(sb
, hint_clu
- 2)) != CLUSTER_32(~0)) {
274 if (new_clu
!= hint_clu
) {
275 if (p_chain
->flags
== 0x03) {
276 exfat_chain_cont_cluster(sb
, p_chain
->dir
,
278 p_chain
->flags
= 0x01;
282 if (set_alloc_bitmap(sb
, new_clu
- 2) != 0)
287 if (p_chain
->flags
== 0x01) {
288 if (exfat_fat_write(sb
, new_clu
, CLUSTER_32(~0)) < 0)
292 if (p_chain
->dir
== CLUSTER_32(~0)) {
293 p_chain
->dir
= new_clu
;
295 if (p_chain
->flags
== 0x01) {
296 if (exfat_fat_write(sb
, last_clu
, new_clu
) < 0)
302 if ((--num_alloc
) == 0) {
303 p_fs
->clu_srch_ptr
= hint_clu
;
304 if (p_fs
->used_clusters
!= UINT_MAX
)
305 p_fs
->used_clusters
+= num_clusters
;
307 p_chain
->size
+= num_clusters
;
311 hint_clu
= new_clu
+ 1;
312 if (hint_clu
>= p_fs
->num_clusters
) {
315 if (p_chain
->flags
== 0x03) {
316 exfat_chain_cont_cluster(sb
, p_chain
->dir
,
318 p_chain
->flags
= 0x01;
323 p_fs
->clu_srch_ptr
= hint_clu
;
324 if (p_fs
->used_clusters
!= UINT_MAX
)
325 p_fs
->used_clusters
+= num_clusters
;
327 p_chain
->size
+= num_clusters
;
331 static void exfat_free_cluster(struct super_block
*sb
, struct chain_t
*p_chain
,
334 s32 num_clusters
= 0;
336 struct fs_info_t
*p_fs
= &(EXFAT_SB(sb
)->fs_info
);
340 if ((p_chain
->dir
== CLUSTER_32(0)) || (p_chain
->dir
== CLUSTER_32(~0)))
343 if (p_chain
->size
<= 0) {
344 pr_err("[EXFAT] free_cluster : skip free-req clu:%u, because of zero-size truncation\n",
352 if (p_chain
->flags
== 0x03) {
355 sector
= START_SECTOR(clu
);
356 for (i
= 0; i
< p_fs
->sectors_per_clu
; i
++)
357 exfat_buf_release(sb
, sector
+ i
);
360 if (clr_alloc_bitmap(sb
, clu
- 2) != 0)
365 } while (num_clusters
< p_chain
->size
);
368 if (p_fs
->dev_ejected
)
372 sector
= START_SECTOR(clu
);
373 for (i
= 0; i
< p_fs
->sectors_per_clu
; i
++)
374 exfat_buf_release(sb
, sector
+ i
);
377 if (clr_alloc_bitmap(sb
, clu
- 2) != 0)
380 if (exfat_fat_read(sb
, clu
, &clu
) == -1)
383 } while ((clu
!= CLUSTER_32(0)) && (clu
!= CLUSTER_32(~0)));
386 if (p_fs
->used_clusters
!= UINT_MAX
)
387 p_fs
->used_clusters
-= num_clusters
;
390 static u32
find_last_cluster(struct super_block
*sb
, struct chain_t
*p_chain
)
393 struct fs_info_t
*p_fs
= &(EXFAT_SB(sb
)->fs_info
);
397 if (p_chain
->flags
== 0x03) {
398 clu
+= p_chain
->size
- 1;
400 while ((exfat_fat_read(sb
, clu
, &next
) == 0) &&
401 (next
!= CLUSTER_32(~0))) {
402 if (p_fs
->dev_ejected
)
411 s32
count_num_clusters(struct super_block
*sb
, struct chain_t
*p_chain
)
415 struct fs_info_t
*p_fs
= &(EXFAT_SB(sb
)->fs_info
);
417 if ((p_chain
->dir
== CLUSTER_32(0)) || (p_chain
->dir
== CLUSTER_32(~0)))
422 if (p_chain
->flags
== 0x03) {
423 count
= p_chain
->size
;
425 for (i
= 2; i
< p_fs
->num_clusters
; i
++) {
427 if (exfat_fat_read(sb
, clu
, &clu
) != 0)
429 if (clu
== CLUSTER_32(~0))
437 static s32
exfat_count_used_clusters(struct super_block
*sb
)
439 int i
, map_i
, map_b
, count
= 0;
441 struct fs_info_t
*p_fs
= &(EXFAT_SB(sb
)->fs_info
);
442 struct bd_info_t
*p_bd
= &(EXFAT_SB(sb
)->bd_info
);
447 for (i
= 2; i
< p_fs
->num_clusters
; i
+= 8) {
448 k
= *(((u8
*)p_fs
->vol_amap
[map_i
]->b_data
) + map_b
);
449 count
+= used_bit
[k
];
451 if ((++map_b
) >= p_bd
->sector_size
) {
460 void exfat_chain_cont_cluster(struct super_block
*sb
, u32 chain
, s32 len
)
466 if (exfat_fat_write(sb
, chain
, chain
+ 1) < 0)
471 exfat_fat_write(sb
, chain
, CLUSTER_32(~0));
475 * Allocation Bitmap Management Functions
478 s32
load_alloc_bitmap(struct super_block
*sb
)
485 struct bmap_dentry_t
*ep
;
486 struct fs_info_t
*p_fs
= &(EXFAT_SB(sb
)->fs_info
);
487 struct bd_info_t
*p_bd
= &(EXFAT_SB(sb
)->bd_info
);
489 clu
.dir
= p_fs
->root_dir
;
492 while (clu
.dir
!= CLUSTER_32(~0)) {
493 if (p_fs
->dev_ejected
)
496 for (i
= 0; i
< p_fs
->dentries_per_clu
; i
++) {
497 ep
= (struct bmap_dentry_t
*)get_entry_in_dir(sb
, &clu
,
502 type
= p_fs
->fs_func
->get_entry_type((struct dentry_t
*)ep
);
504 if (type
== TYPE_UNUSED
)
506 if (type
!= TYPE_BITMAP
)
509 if (ep
->flags
== 0x0) {
510 p_fs
->map_clu
= GET32_A(ep
->start_clu
);
511 map_size
= (u32
)GET64_A(ep
->size
);
513 p_fs
->map_sectors
= ((map_size
- 1) >> p_bd
->sector_size_bits
) + 1;
515 p_fs
->vol_amap
= kmalloc_array(p_fs
->map_sectors
,
516 sizeof(struct buffer_head
*),
521 sector
= START_SECTOR(p_fs
->map_clu
);
523 for (j
= 0; j
< p_fs
->map_sectors
; j
++) {
524 p_fs
->vol_amap
[j
] = NULL
;
525 ret
= sector_read(sb
, sector
+ j
, &p_fs
->vol_amap
[j
], 1);
527 /* release all buffers and free vol_amap */
530 brelse(p_fs
->vol_amap
[i
++]);
532 kfree(p_fs
->vol_amap
);
533 p_fs
->vol_amap
= NULL
;
543 if (exfat_fat_read(sb
, clu
.dir
, &clu
.dir
) != 0)
547 return -EFSCORRUPTED
;
550 void free_alloc_bitmap(struct super_block
*sb
)
553 struct fs_info_t
*p_fs
= &(EXFAT_SB(sb
)->fs_info
);
555 brelse(p_fs
->pbr_bh
);
557 for (i
= 0; i
< p_fs
->map_sectors
; i
++)
558 __brelse(p_fs
->vol_amap
[i
]);
560 kfree(p_fs
->vol_amap
);
561 p_fs
->vol_amap
= NULL
;
564 void sync_alloc_bitmap(struct super_block
*sb
)
567 struct fs_info_t
*p_fs
= &(EXFAT_SB(sb
)->fs_info
);
572 for (i
= 0; i
< p_fs
->map_sectors
; i
++)
573 sync_dirty_buffer(p_fs
->vol_amap
[i
]);
577 * Upcase table Management Functions
579 static s32
__load_upcase_table(struct super_block
*sb
, sector_t sector
,
580 u32 num_sectors
, u32 utbl_checksum
)
582 int i
, ret
= -EINVAL
;
584 struct fs_info_t
*p_fs
= &(EXFAT_SB(sb
)->fs_info
);
585 struct bd_info_t
*p_bd
= &(EXFAT_SB(sb
)->bd_info
);
586 struct buffer_head
*tmp_bh
= NULL
;
587 sector_t end_sector
= num_sectors
+ sector
;
596 upcase_table
= kmalloc_array(UTBL_COL_COUNT
, sizeof(u16
*), GFP_KERNEL
);
597 p_fs
->vol_utbl
= upcase_table
;
600 memset(upcase_table
, 0, UTBL_COL_COUNT
* sizeof(u16
*));
602 while (sector
< end_sector
) {
603 ret
= sector_read(sb
, sector
, &tmp_bh
, 1);
605 pr_debug("sector read (0x%llX)fail\n",
606 (unsigned long long)sector
);
611 for (i
= 0; i
< p_bd
->sector_size
&& index
<= 0xFFFF; i
+= 2) {
612 uni
= GET16(((u8
*)tmp_bh
->b_data
) + i
);
614 checksum
= ((checksum
& 1) ? 0x80000000 : 0) +
615 (checksum
>> 1) + *(((u8
*)tmp_bh
->b_data
) +
617 checksum
= ((checksum
& 1) ? 0x80000000 : 0) +
618 (checksum
>> 1) + *(((u8
*)tmp_bh
->b_data
) +
622 pr_debug("skip from 0x%X ", index
);
624 pr_debug("to 0x%X (amount of 0x%X)\n",
627 } else if (uni
== index
) {
629 } else if (uni
== 0xFFFF) {
631 } else { /* uni != index , uni != 0xFFFF */
632 u16 col_index
= get_col_index(index
);
634 if (!upcase_table
[col_index
]) {
635 pr_debug("alloc = 0x%X\n", col_index
);
636 upcase_table
[col_index
] = kmalloc_array(UTBL_ROW_COUNT
,
637 sizeof(u16
), GFP_KERNEL
);
638 if (!upcase_table
[col_index
]) {
643 for (j
= 0; j
< UTBL_ROW_COUNT
; j
++)
644 upcase_table
[col_index
][j
] = (col_index
<< LOW_INDEX_BIT
) | j
;
647 upcase_table
[col_index
][get_row_index(index
)] = uni
;
652 if (index
>= 0xFFFF && utbl_checksum
== checksum
) {
661 free_upcase_table(sb
);
665 static s32
__load_default_upcase_table(struct super_block
*sb
)
667 int i
, ret
= -EINVAL
;
669 struct fs_info_t
*p_fs
= &(EXFAT_SB(sb
)->fs_info
);
676 upcase_table
= kmalloc_array(UTBL_COL_COUNT
, sizeof(u16
*), GFP_KERNEL
);
677 p_fs
->vol_utbl
= upcase_table
;
680 memset(upcase_table
, 0, UTBL_COL_COUNT
* sizeof(u16
*));
682 for (i
= 0; index
<= 0xFFFF && i
< NUM_UPCASE
* 2; i
+= 2) {
683 uni
= GET16(uni_upcase
+ i
);
685 pr_debug("skip from 0x%X ", index
);
687 pr_debug("to 0x%X (amount of 0x%X)\n", index
, uni
);
689 } else if (uni
== index
) {
691 } else if (uni
== 0xFFFF) {
693 } else { /* uni != index , uni != 0xFFFF */
694 u16 col_index
= get_col_index(index
);
696 if (!upcase_table
[col_index
]) {
697 pr_debug("alloc = 0x%X\n", col_index
);
698 upcase_table
[col_index
] = kmalloc_array(UTBL_ROW_COUNT
,
701 if (!upcase_table
[col_index
]) {
706 for (j
= 0; j
< UTBL_ROW_COUNT
; j
++)
707 upcase_table
[col_index
][j
] = (col_index
<< LOW_INDEX_BIT
) | j
;
710 upcase_table
[col_index
][get_row_index(index
)] = uni
;
719 /* FATAL error: default upcase table has error */
720 free_upcase_table(sb
);
724 s32
load_upcase_table(struct super_block
*sb
)
727 u32 tbl_clu
, tbl_size
;
729 u32 type
, num_sectors
;
731 struct case_dentry_t
*ep
;
732 struct fs_info_t
*p_fs
= &(EXFAT_SB(sb
)->fs_info
);
733 struct bd_info_t
*p_bd
= &(EXFAT_SB(sb
)->bd_info
);
735 clu
.dir
= p_fs
->root_dir
;
738 if (p_fs
->dev_ejected
)
741 while (clu
.dir
!= CLUSTER_32(~0)) {
742 for (i
= 0; i
< p_fs
->dentries_per_clu
; i
++) {
743 ep
= (struct case_dentry_t
*)get_entry_in_dir(sb
, &clu
,
748 type
= p_fs
->fs_func
->get_entry_type((struct dentry_t
*)ep
);
750 if (type
== TYPE_UNUSED
)
752 if (type
!= TYPE_UPCASE
)
755 tbl_clu
= GET32_A(ep
->start_clu
);
756 tbl_size
= (u32
)GET64_A(ep
->size
);
758 sector
= START_SECTOR(tbl_clu
);
759 num_sectors
= ((tbl_size
- 1) >> p_bd
->sector_size_bits
) + 1;
760 if (__load_upcase_table(sb
, sector
, num_sectors
,
761 GET32_A(ep
->checksum
)) != 0)
765 if (exfat_fat_read(sb
, clu
.dir
, &clu
.dir
) != 0)
768 /* load default upcase table */
769 return __load_default_upcase_table(sb
);
772 void free_upcase_table(struct super_block
*sb
)
775 struct fs_info_t
*p_fs
= &(EXFAT_SB(sb
)->fs_info
);
778 upcase_table
= p_fs
->vol_utbl
;
779 for (i
= 0; i
< UTBL_COL_COUNT
; i
++)
780 kfree(upcase_table
[i
]);
782 kfree(p_fs
->vol_utbl
);
783 p_fs
->vol_utbl
= NULL
;
787 * Directory Entry Management Functions
790 static u32
exfat_get_entry_type(struct dentry_t
*p_entry
)
792 struct file_dentry_t
*ep
= (struct file_dentry_t
*)p_entry
;
794 if (ep
->type
== 0x0) {
796 } else if (ep
->type
< 0x80) {
798 } else if (ep
->type
== 0x80) {
800 } else if (ep
->type
< 0xA0) {
801 if (ep
->type
== 0x81) {
803 } else if (ep
->type
== 0x82) {
805 } else if (ep
->type
== 0x83) {
807 } else if (ep
->type
== 0x85) {
808 if (GET16_A(ep
->attr
) & ATTR_SUBDIR
)
813 return TYPE_CRITICAL_PRI
;
814 } else if (ep
->type
< 0xC0) {
815 if (ep
->type
== 0xA0)
817 else if (ep
->type
== 0xA1)
819 else if (ep
->type
== 0xA2)
821 return TYPE_BENIGN_PRI
;
822 } else if (ep
->type
< 0xE0) {
823 if (ep
->type
== 0xC0)
825 else if (ep
->type
== 0xC1)
827 else if (ep
->type
== 0xC2)
829 return TYPE_CRITICAL_SEC
;
832 return TYPE_BENIGN_SEC
;
835 static void exfat_set_entry_type(struct dentry_t
*p_entry
, u32 type
)
837 struct file_dentry_t
*ep
= (struct file_dentry_t
*)p_entry
;
839 if (type
== TYPE_UNUSED
) {
841 } else if (type
== TYPE_DELETED
) {
843 } else if (type
== TYPE_STREAM
) {
845 } else if (type
== TYPE_EXTEND
) {
847 } else if (type
== TYPE_BITMAP
) {
849 } else if (type
== TYPE_UPCASE
) {
851 } else if (type
== TYPE_VOLUME
) {
853 } else if (type
== TYPE_DIR
) {
855 SET16_A(ep
->attr
, ATTR_SUBDIR
);
856 } else if (type
== TYPE_FILE
) {
858 SET16_A(ep
->attr
, ATTR_ARCHIVE
);
859 } else if (type
== TYPE_SYMLINK
) {
861 SET16_A(ep
->attr
, ATTR_ARCHIVE
| ATTR_SYMLINK
);
865 static u32
exfat_get_entry_attr(struct dentry_t
*p_entry
)
867 struct file_dentry_t
*ep
= (struct file_dentry_t
*)p_entry
;
869 return (u32
)GET16_A(ep
->attr
);
872 static void exfat_set_entry_attr(struct dentry_t
*p_entry
, u32 attr
)
874 struct file_dentry_t
*ep
= (struct file_dentry_t
*)p_entry
;
876 SET16_A(ep
->attr
, (u16
)attr
);
879 static u8
exfat_get_entry_flag(struct dentry_t
*p_entry
)
881 struct strm_dentry_t
*ep
= (struct strm_dentry_t
*)p_entry
;
886 static void exfat_set_entry_flag(struct dentry_t
*p_entry
, u8 flags
)
888 struct strm_dentry_t
*ep
= (struct strm_dentry_t
*)p_entry
;
893 static u32
exfat_get_entry_clu0(struct dentry_t
*p_entry
)
895 struct strm_dentry_t
*ep
= (struct strm_dentry_t
*)p_entry
;
897 return GET32_A(ep
->start_clu
);
900 static void exfat_set_entry_clu0(struct dentry_t
*p_entry
, u32 start_clu
)
902 struct strm_dentry_t
*ep
= (struct strm_dentry_t
*)p_entry
;
904 SET32_A(ep
->start_clu
, start_clu
);
907 static u64
exfat_get_entry_size(struct dentry_t
*p_entry
)
909 struct strm_dentry_t
*ep
= (struct strm_dentry_t
*)p_entry
;
911 return GET64_A(ep
->valid_size
);
914 static void exfat_set_entry_size(struct dentry_t
*p_entry
, u64 size
)
916 struct strm_dentry_t
*ep
= (struct strm_dentry_t
*)p_entry
;
918 SET64_A(ep
->valid_size
, size
);
919 SET64_A(ep
->size
, size
);
922 static void exfat_get_entry_time(struct dentry_t
*p_entry
, struct timestamp_t
*tp
,
925 u16 t
= 0x00, d
= 0x21;
926 struct file_dentry_t
*ep
= (struct file_dentry_t
*)p_entry
;
930 t
= GET16_A(ep
->create_time
);
931 d
= GET16_A(ep
->create_date
);
934 t
= GET16_A(ep
->modify_time
);
935 d
= GET16_A(ep
->modify_date
);
938 t
= GET16_A(ep
->access_time
);
939 d
= GET16_A(ep
->access_date
);
943 tp
->sec
= (t
& 0x001F) << 1;
944 tp
->min
= (t
>> 5) & 0x003F;
945 tp
->hour
= (t
>> 11);
946 tp
->day
= (d
& 0x001F);
947 tp
->mon
= (d
>> 5) & 0x000F;
951 static void exfat_set_entry_time(struct dentry_t
*p_entry
, struct timestamp_t
*tp
,
955 struct file_dentry_t
*ep
= (struct file_dentry_t
*)p_entry
;
957 t
= (tp
->hour
<< 11) | (tp
->min
<< 5) | (tp
->sec
>> 1);
958 d
= (tp
->year
<< 9) | (tp
->mon
<< 5) | tp
->day
;
962 SET16_A(ep
->create_time
, t
);
963 SET16_A(ep
->create_date
, d
);
966 SET16_A(ep
->modify_time
, t
);
967 SET16_A(ep
->modify_date
, d
);
970 SET16_A(ep
->access_time
, t
);
971 SET16_A(ep
->access_date
, d
);
976 static void init_file_entry(struct file_dentry_t
*ep
, u32 type
)
978 struct timestamp_t tm
, *tp
;
980 exfat_set_entry_type((struct dentry_t
*)ep
, type
);
982 tp
= tm_current(&tm
);
983 exfat_set_entry_time((struct dentry_t
*)ep
, tp
, TM_CREATE
);
984 exfat_set_entry_time((struct dentry_t
*)ep
, tp
, TM_MODIFY
);
985 exfat_set_entry_time((struct dentry_t
*)ep
, tp
, TM_ACCESS
);
986 ep
->create_time_ms
= 0;
987 ep
->modify_time_ms
= 0;
988 ep
->access_time_ms
= 0;
991 static void init_strm_entry(struct strm_dentry_t
*ep
, u8 flags
, u32 start_clu
, u64 size
)
993 exfat_set_entry_type((struct dentry_t
*)ep
, TYPE_STREAM
);
995 SET32_A(ep
->start_clu
, start_clu
);
996 SET64_A(ep
->valid_size
, size
);
997 SET64_A(ep
->size
, size
);
1000 static void init_name_entry(struct name_dentry_t
*ep
, u16
*uniname
)
1004 exfat_set_entry_type((struct dentry_t
*)ep
, TYPE_EXTEND
);
1007 for (i
= 0; i
< 30; i
++, i
++) {
1008 SET16_A(ep
->unicode_0_14
+ i
, *uniname
);
1009 if (*uniname
== 0x0)
1015 static s32
exfat_init_dir_entry(struct super_block
*sb
, struct chain_t
*p_dir
,
1016 s32 entry
, u32 type
, u32 start_clu
, u64 size
)
1020 struct file_dentry_t
*file_ep
;
1021 struct strm_dentry_t
*strm_ep
;
1023 flags
= (type
== TYPE_FILE
) ? 0x01 : 0x03;
1025 /* we cannot use get_entry_set_in_dir here because file ep is not initialized yet */
1026 file_ep
= (struct file_dentry_t
*)get_entry_in_dir(sb
, p_dir
, entry
,
1031 strm_ep
= (struct strm_dentry_t
*)get_entry_in_dir(sb
, p_dir
, entry
+ 1,
1036 init_file_entry(file_ep
, type
);
1037 exfat_buf_modify(sb
, sector
);
1039 init_strm_entry(strm_ep
, flags
, start_clu
, size
);
1040 exfat_buf_modify(sb
, sector
);
1045 static s32
exfat_init_ext_entry(struct super_block
*sb
, struct chain_t
*p_dir
,
1046 s32 entry
, s32 num_entries
,
1047 struct uni_name_t
*p_uniname
,
1048 struct dos_name_t
*p_dosname
)
1052 u16
*uniname
= p_uniname
->name
;
1053 struct file_dentry_t
*file_ep
;
1054 struct strm_dentry_t
*strm_ep
;
1055 struct name_dentry_t
*name_ep
;
1057 file_ep
= (struct file_dentry_t
*)get_entry_in_dir(sb
, p_dir
, entry
,
1062 file_ep
->num_ext
= (u8
)(num_entries
- 1);
1063 exfat_buf_modify(sb
, sector
);
1065 strm_ep
= (struct strm_dentry_t
*)get_entry_in_dir(sb
, p_dir
, entry
+ 1,
1070 strm_ep
->name_len
= p_uniname
->name_len
;
1071 SET16_A(strm_ep
->name_hash
, p_uniname
->name_hash
);
1072 exfat_buf_modify(sb
, sector
);
1074 for (i
= 2; i
< num_entries
; i
++) {
1075 name_ep
= (struct name_dentry_t
*)get_entry_in_dir(sb
, p_dir
,
1081 init_name_entry(name_ep
, uniname
);
1082 exfat_buf_modify(sb
, sector
);
1086 update_dir_checksum(sb
, p_dir
, entry
);
1091 static void exfat_delete_dir_entry(struct super_block
*sb
, struct chain_t
*p_dir
,
1092 s32 entry
, s32 order
, s32 num_entries
)
1096 struct dentry_t
*ep
;
1097 struct fs_info_t
*p_fs
= &(EXFAT_SB(sb
)->fs_info
);
1099 for (i
= order
; i
< num_entries
; i
++) {
1100 ep
= get_entry_in_dir(sb
, p_dir
, entry
+ i
, §or
);
1104 p_fs
->fs_func
->set_entry_type(ep
, TYPE_DELETED
);
1105 exfat_buf_modify(sb
, sector
);
1109 void update_dir_checksum(struct super_block
*sb
, struct chain_t
*p_dir
,
1115 struct file_dentry_t
*file_ep
;
1116 struct dentry_t
*ep
;
1118 file_ep
= (struct file_dentry_t
*)get_entry_in_dir(sb
, p_dir
, entry
,
1123 exfat_buf_lock(sb
, sector
);
1125 num_entries
= (s32
)file_ep
->num_ext
+ 1;
1126 chksum
= calc_checksum_2byte((void *)file_ep
, DENTRY_SIZE
, 0,
1129 for (i
= 1; i
< num_entries
; i
++) {
1130 ep
= get_entry_in_dir(sb
, p_dir
, entry
+ i
, NULL
);
1132 exfat_buf_unlock(sb
, sector
);
1136 chksum
= calc_checksum_2byte((void *)ep
, DENTRY_SIZE
, chksum
,
1140 SET16_A(file_ep
->checksum
, chksum
);
1141 exfat_buf_modify(sb
, sector
);
1142 exfat_buf_unlock(sb
, sector
);
1145 static s32
__write_partial_entries_in_entry_set(struct super_block
*sb
,
1146 struct entry_set_cache_t
*es
,
1147 sector_t sec
, s32 off
, u32 count
)
1149 s32 num_entries
, buf_off
= (off
- es
->offset
);
1150 u32 remaining_byte_in_sector
, copy_entries
;
1151 struct fs_info_t
*p_fs
= &(EXFAT_SB(sb
)->fs_info
);
1152 struct bd_info_t
*p_bd
= &(EXFAT_SB(sb
)->bd_info
);
1154 u8
*buf
, *esbuf
= (u8
*)&es
->__buf
;
1156 pr_debug("%s entered es %p sec %llu off %d count %d\n",
1157 __func__
, es
, (unsigned long long)sec
, off
, count
);
1158 num_entries
= count
;
1160 while (num_entries
) {
1161 /* white per sector base */
1162 remaining_byte_in_sector
= (1 << p_bd
->sector_size_bits
) - off
;
1163 copy_entries
= min_t(s32
,
1164 remaining_byte_in_sector
>> DENTRY_SIZE_BITS
,
1166 buf
= exfat_buf_getblk(sb
, sec
);
1169 pr_debug("es->buf %p buf_off %u\n", esbuf
, buf_off
);
1170 pr_debug("copying %d entries from %p to sector %llu\n",
1171 copy_entries
, (esbuf
+ buf_off
),
1172 (unsigned long long)sec
);
1173 memcpy(buf
+ off
, esbuf
+ buf_off
,
1174 copy_entries
<< DENTRY_SIZE_BITS
);
1175 exfat_buf_modify(sb
, sec
);
1176 num_entries
-= copy_entries
;
1179 /* get next sector */
1180 if (IS_LAST_SECTOR_IN_CLUSTER(sec
)) {
1181 clu
= GET_CLUSTER_FROM_SECTOR(sec
);
1182 if (es
->alloc_flag
== 0x03) {
1185 if (exfat_fat_read(sb
, clu
, &clu
) == -1)
1188 sec
= START_SECTOR(clu
);
1193 buf_off
+= copy_entries
<< DENTRY_SIZE_BITS
;
1197 pr_debug("%s exited successfully\n", __func__
);
1200 pr_debug("%s failed\n", __func__
);
1204 /* write back all entries in entry set */
1205 static s32
write_whole_entry_set(struct super_block
*sb
, struct entry_set_cache_t
*es
)
1207 return __write_partial_entries_in_entry_set(sb
, es
, es
->sector
,
1212 void update_dir_checksum_with_entry_set(struct super_block
*sb
,
1213 struct entry_set_cache_t
*es
)
1215 struct dentry_t
*ep
;
1217 s32 chksum_type
= CS_DIR_ENTRY
, i
;
1219 ep
= (struct dentry_t
*)&es
->__buf
;
1220 for (i
= 0; i
< es
->num_entries
; i
++) {
1221 pr_debug("%s ep %p\n", __func__
, ep
);
1222 chksum
= calc_checksum_2byte((void *)ep
, DENTRY_SIZE
, chksum
,
1225 chksum_type
= CS_DEFAULT
;
1228 ep
= (struct dentry_t
*)&es
->__buf
;
1229 SET16_A(((struct file_dentry_t
*)ep
)->checksum
, chksum
);
1230 write_whole_entry_set(sb
, es
);
1233 static s32
_walk_fat_chain(struct super_block
*sb
, struct chain_t
*p_dir
,
1234 s32 byte_offset
, u32
*clu
)
1236 struct fs_info_t
*p_fs
= &(EXFAT_SB(sb
)->fs_info
);
1240 clu_offset
= byte_offset
>> p_fs
->cluster_size_bits
;
1241 cur_clu
= p_dir
->dir
;
1243 if (p_dir
->flags
== 0x03) {
1244 cur_clu
+= clu_offset
;
1246 while (clu_offset
> 0) {
1247 if (exfat_fat_read(sb
, cur_clu
, &cur_clu
) == -1)
1258 static s32
find_location(struct super_block
*sb
, struct chain_t
*p_dir
, s32 entry
,
1259 sector_t
*sector
, s32
*offset
)
1263 struct fs_info_t
*p_fs
= &(EXFAT_SB(sb
)->fs_info
);
1264 struct bd_info_t
*p_bd
= &(EXFAT_SB(sb
)->bd_info
);
1266 off
= entry
<< DENTRY_SIZE_BITS
;
1268 if (p_dir
->dir
== CLUSTER_32(0)) { /* FAT16 root_dir */
1269 *offset
= off
& p_bd
->sector_size_mask
;
1270 *sector
= off
>> p_bd
->sector_size_bits
;
1271 *sector
+= p_fs
->root_start_sector
;
1273 ret
= _walk_fat_chain(sb
, p_dir
, off
, &clu
);
1277 /* byte offset in cluster */
1278 off
&= p_fs
->cluster_size
- 1;
1280 /* byte offset in sector */
1281 *offset
= off
& p_bd
->sector_size_mask
;
1283 /* sector offset in cluster */
1284 *sector
= off
>> p_bd
->sector_size_bits
;
1285 *sector
+= START_SECTOR(clu
);
1290 struct dentry_t
*get_entry_in_dir(struct super_block
*sb
, struct chain_t
*p_dir
,
1291 s32 entry
, sector_t
*sector
)
1297 if (find_location(sb
, p_dir
, entry
, &sec
, &off
) != 0)
1300 buf
= exfat_buf_getblk(sb
, sec
);
1307 return (struct dentry_t
*)(buf
+ off
);
1310 /* returns a set of dentries for a file or dir.
1311 * Note that this is a copy (dump) of dentries so that user should call write_entry_set()
1312 * to apply changes made in this entry set to the real device.
1314 * sb+p_dir+entry: indicates a file/dir
1315 * type: specifies how many dentries should be included.
1317 * file_ep: will point the first dentry(= file dentry) on success
1319 * pointer of entry set on success,
1323 #define ES_MODE_STARTED 0
1324 #define ES_MODE_GET_FILE_ENTRY 1
1325 #define ES_MODE_GET_STRM_ENTRY 2
1326 #define ES_MODE_GET_NAME_ENTRY 3
1327 #define ES_MODE_GET_CRITICAL_SEC_ENTRY 4
1328 struct entry_set_cache_t
*get_entry_set_in_dir(struct super_block
*sb
,
1329 struct chain_t
*p_dir
, s32 entry
,
1331 struct dentry_t
**file_ep
)
1333 s32 off
, ret
, byte_offset
;
1337 struct fs_info_t
*p_fs
= &(EXFAT_SB(sb
)->fs_info
);
1338 struct bd_info_t
*p_bd
= &(EXFAT_SB(sb
)->bd_info
);
1339 struct entry_set_cache_t
*es
= NULL
;
1340 struct dentry_t
*ep
, *pos
;
1343 s32 mode
= ES_MODE_STARTED
;
1346 pr_debug("%s entered p_dir dir %u flags %x size %d\n",
1347 __func__
, p_dir
->dir
, p_dir
->flags
, p_dir
->size
);
1349 byte_offset
= entry
<< DENTRY_SIZE_BITS
;
1350 ret
= _walk_fat_chain(sb
, p_dir
, byte_offset
, &clu
);
1354 /* byte offset in cluster */
1355 byte_offset
&= p_fs
->cluster_size
- 1;
1357 /* byte offset in sector */
1358 off
= byte_offset
& p_bd
->sector_size_mask
;
1360 /* sector offset in cluster */
1361 sec
= byte_offset
>> p_bd
->sector_size_bits
;
1362 sec
+= START_SECTOR(clu
);
1364 buf
= exfat_buf_getblk(sb
, sec
);
1368 ep
= (struct dentry_t
*)(buf
+ off
);
1369 entry_type
= p_fs
->fs_func
->get_entry_type(ep
);
1371 if ((entry_type
!= TYPE_FILE
) && (entry_type
!= TYPE_DIR
))
1374 if (type
== ES_ALL_ENTRIES
)
1375 num_entries
= ((struct file_dentry_t
*)ep
)->num_ext
+ 1;
1379 bufsize
= offsetof(struct entry_set_cache_t
, __buf
) + (num_entries
) *
1380 sizeof(struct dentry_t
);
1381 pr_debug("%s: trying to kmalloc %zx bytes for %d entries\n", __func__
,
1382 bufsize
, num_entries
);
1383 es
= kmalloc(bufsize
, GFP_KERNEL
);
1387 es
->num_entries
= num_entries
;
1390 es
->alloc_flag
= p_dir
->flags
;
1392 pos
= (struct dentry_t
*)&es
->__buf
;
1394 while (num_entries
) {
1396 * instead of copying whole sector, we will check every entry.
1397 * this will provide minimum stablity and consistency.
1399 entry_type
= p_fs
->fs_func
->get_entry_type(ep
);
1401 if ((entry_type
== TYPE_UNUSED
) || (entry_type
== TYPE_DELETED
))
1405 case ES_MODE_STARTED
:
1406 if ((entry_type
== TYPE_FILE
) || (entry_type
== TYPE_DIR
))
1407 mode
= ES_MODE_GET_FILE_ENTRY
;
1411 case ES_MODE_GET_FILE_ENTRY
:
1412 if (entry_type
== TYPE_STREAM
)
1413 mode
= ES_MODE_GET_STRM_ENTRY
;
1417 case ES_MODE_GET_STRM_ENTRY
:
1418 if (entry_type
== TYPE_EXTEND
)
1419 mode
= ES_MODE_GET_NAME_ENTRY
;
1423 case ES_MODE_GET_NAME_ENTRY
:
1424 if (entry_type
== TYPE_EXTEND
)
1426 else if (entry_type
== TYPE_STREAM
)
1428 else if (entry_type
& TYPE_CRITICAL_SEC
)
1429 mode
= ES_MODE_GET_CRITICAL_SEC_ENTRY
;
1433 case ES_MODE_GET_CRITICAL_SEC_ENTRY
:
1434 if ((entry_type
== TYPE_EXTEND
) ||
1435 (entry_type
== TYPE_STREAM
))
1437 else if ((entry_type
& TYPE_CRITICAL_SEC
) !=
1443 memcpy(pos
, ep
, sizeof(struct dentry_t
));
1445 if (--num_entries
== 0)
1448 if (((off
+ DENTRY_SIZE
) & p_bd
->sector_size_mask
) <
1449 (off
& p_bd
->sector_size_mask
)) {
1450 /* get the next sector */
1451 if (IS_LAST_SECTOR_IN_CLUSTER(sec
)) {
1452 if (es
->alloc_flag
== 0x03) {
1455 if (exfat_fat_read(sb
, clu
, &clu
) == -1)
1458 sec
= START_SECTOR(clu
);
1462 buf
= exfat_buf_getblk(sb
, sec
);
1466 ep
= (struct dentry_t
*)(buf
);
1475 *file_ep
= (struct dentry_t
*)&es
->__buf
;
1477 pr_debug("%s exiting es %p sec %llu offset %d flags %d, num_entries %u buf ptr %p\n",
1478 __func__
, es
, (unsigned long long)es
->sector
, es
->offset
,
1479 es
->alloc_flag
, es
->num_entries
, &es
->__buf
);
1482 pr_debug("%s exited NULL (es %p)\n", __func__
, es
);
1487 void release_entry_set(struct entry_set_cache_t
*es
)
1489 pr_debug("%s es=%p\n", __func__
, es
);
1493 /* search EMPTY CONTINUOUS "num_entries" entries */
1494 static s32
search_deleted_or_unused_entry(struct super_block
*sb
,
1495 struct chain_t
*p_dir
, s32 num_entries
)
1497 int i
, dentry
, num_empty
= 0;
1498 s32 dentries_per_clu
;
1501 struct dentry_t
*ep
;
1502 struct fs_info_t
*p_fs
= &(EXFAT_SB(sb
)->fs_info
);
1504 if (p_dir
->dir
== CLUSTER_32(0)) /* FAT16 root_dir */
1505 dentries_per_clu
= p_fs
->dentries_in_root
;
1507 dentries_per_clu
= p_fs
->dentries_per_clu
;
1509 if (p_fs
->hint_uentry
.dir
== p_dir
->dir
) {
1510 if (p_fs
->hint_uentry
.entry
== -1)
1513 clu
.dir
= p_fs
->hint_uentry
.clu
.dir
;
1514 clu
.size
= p_fs
->hint_uentry
.clu
.size
;
1515 clu
.flags
= p_fs
->hint_uentry
.clu
.flags
;
1517 dentry
= p_fs
->hint_uentry
.entry
;
1519 p_fs
->hint_uentry
.entry
= -1;
1521 clu
.dir
= p_dir
->dir
;
1522 clu
.size
= p_dir
->size
;
1523 clu
.flags
= p_dir
->flags
;
1528 while (clu
.dir
!= CLUSTER_32(~0)) {
1529 if (p_fs
->dev_ejected
)
1532 if (p_dir
->dir
== CLUSTER_32(0)) /* FAT16 root_dir */
1533 i
= dentry
% dentries_per_clu
;
1535 i
= dentry
& (dentries_per_clu
- 1);
1537 for (; i
< dentries_per_clu
; i
++, dentry
++) {
1538 ep
= get_entry_in_dir(sb
, &clu
, i
, NULL
);
1542 type
= p_fs
->fs_func
->get_entry_type(ep
);
1544 if (type
== TYPE_UNUSED
) {
1546 if (p_fs
->hint_uentry
.entry
== -1) {
1547 p_fs
->hint_uentry
.dir
= p_dir
->dir
;
1548 p_fs
->hint_uentry
.entry
= dentry
;
1550 p_fs
->hint_uentry
.clu
.dir
= clu
.dir
;
1551 p_fs
->hint_uentry
.clu
.size
= clu
.size
;
1552 p_fs
->hint_uentry
.clu
.flags
= clu
.flags
;
1554 } else if (type
== TYPE_DELETED
) {
1560 if (num_empty
>= num_entries
) {
1561 p_fs
->hint_uentry
.dir
= CLUSTER_32(~0);
1562 p_fs
->hint_uentry
.entry
= -1;
1564 if (p_fs
->vol_type
== EXFAT
)
1565 return dentry
- (num_entries
- 1);
1571 if (p_dir
->dir
== CLUSTER_32(0))
1572 break; /* FAT16 root_dir */
1574 if (clu
.flags
== 0x03) {
1575 if ((--clu
.size
) > 0)
1578 clu
.dir
= CLUSTER_32(~0);
1580 if (exfat_fat_read(sb
, clu
.dir
, &clu
.dir
) != 0)
1588 static s32
find_empty_entry(struct inode
*inode
, struct chain_t
*p_dir
, s32 num_entries
)
1595 struct dentry_t
*ep
= NULL
;
1596 struct super_block
*sb
= inode
->i_sb
;
1597 struct fs_info_t
*p_fs
= &(EXFAT_SB(sb
)->fs_info
);
1598 struct file_id_t
*fid
= &(EXFAT_I(inode
)->fid
);
1600 if (p_dir
->dir
== CLUSTER_32(0)) /* FAT16 root_dir */
1601 return search_deleted_or_unused_entry(sb
, p_dir
, num_entries
);
1603 while ((dentry
= search_deleted_or_unused_entry(sb
, p_dir
, num_entries
)) < 0) {
1604 if (p_fs
->dev_ejected
)
1607 if (p_dir
->dir
!= p_fs
->root_dir
)
1608 size
= i_size_read(inode
);
1610 last_clu
= find_last_cluster(sb
, p_dir
);
1611 clu
.dir
= last_clu
+ 1;
1613 clu
.flags
= p_dir
->flags
;
1615 /* (1) allocate a cluster */
1616 ret
= p_fs
->fs_func
->alloc_cluster(sb
, 1, &clu
);
1620 if (clear_cluster(sb
, clu
.dir
) != 0)
1623 /* (2) append to the FAT chain */
1624 if (clu
.flags
!= p_dir
->flags
) {
1625 exfat_chain_cont_cluster(sb
, p_dir
->dir
, p_dir
->size
);
1626 p_dir
->flags
= 0x01;
1627 p_fs
->hint_uentry
.clu
.flags
= 0x01;
1629 if (clu
.flags
== 0x01)
1630 if (exfat_fat_write(sb
, last_clu
, clu
.dir
) < 0)
1633 if (p_fs
->hint_uentry
.entry
== -1) {
1634 p_fs
->hint_uentry
.dir
= p_dir
->dir
;
1635 p_fs
->hint_uentry
.entry
= p_dir
->size
<< (p_fs
->cluster_size_bits
- DENTRY_SIZE_BITS
);
1637 p_fs
->hint_uentry
.clu
.dir
= clu
.dir
;
1638 p_fs
->hint_uentry
.clu
.size
= 0;
1639 p_fs
->hint_uentry
.clu
.flags
= clu
.flags
;
1641 p_fs
->hint_uentry
.clu
.size
++;
1644 /* (3) update the directory entry */
1645 if (p_dir
->dir
!= p_fs
->root_dir
) {
1646 size
+= p_fs
->cluster_size
;
1648 ep
= get_entry_in_dir(sb
, &fid
->dir
,
1649 fid
->entry
+ 1, §or
);
1652 p_fs
->fs_func
->set_entry_size(ep
, size
);
1653 p_fs
->fs_func
->set_entry_flag(ep
, p_dir
->flags
);
1654 exfat_buf_modify(sb
, sector
);
1656 update_dir_checksum(sb
, &fid
->dir
,
1660 i_size_write(inode
, i_size_read(inode
) + p_fs
->cluster_size
);
1661 EXFAT_I(inode
)->mmu_private
+= p_fs
->cluster_size
;
1662 EXFAT_I(inode
)->fid
.size
+= p_fs
->cluster_size
;
1663 EXFAT_I(inode
)->fid
.flags
= p_dir
->flags
;
1664 inode
->i_blocks
+= 1 << (p_fs
->cluster_size_bits
- 9);
1670 static s32
extract_uni_name_from_name_entry(struct name_dentry_t
*ep
, u16
*uniname
,
1675 for (i
= 0; i
< 30; i
+= 2) {
1676 *uniname
= GET16_A(ep
->unicode_0_14
+ i
);
1677 if (*uniname
== 0x0)
1687 /* return values of exfat_find_dir_entry()
1688 * >= 0 : return dir entiry position with the name in dir
1689 * -1 : (root dir, ".") it is the root dir itself
1690 * -2 : entry with the name does not exist
1692 static s32
exfat_find_dir_entry(struct super_block
*sb
, struct chain_t
*p_dir
,
1693 struct uni_name_t
*p_uniname
, s32 num_entries
,
1694 struct dos_name_t
*p_dosname
, u32 type
)
1696 int i
= 0, dentry
= 0, num_ext_entries
= 0, len
, step
;
1698 bool is_feasible_entry
= false;
1699 s32 dentries_per_clu
, num_empty
= 0;
1701 u16 entry_uniname
[16], *uniname
= NULL
, unichar
;
1703 struct dentry_t
*ep
;
1704 struct file_dentry_t
*file_ep
;
1705 struct strm_dentry_t
*strm_ep
;
1706 struct name_dentry_t
*name_ep
;
1707 struct fs_info_t
*p_fs
= &(EXFAT_SB(sb
)->fs_info
);
1709 if (p_dir
->dir
== p_fs
->root_dir
) {
1710 if ((!nls_uniname_cmp(sb
, p_uniname
->name
,
1711 (u16
*)UNI_CUR_DIR_NAME
)) ||
1712 (!nls_uniname_cmp(sb
, p_uniname
->name
,
1713 (u16
*)UNI_PAR_DIR_NAME
)))
1714 return -1; // special case, root directory itself
1717 if (p_dir
->dir
== CLUSTER_32(0)) /* FAT16 root_dir */
1718 dentries_per_clu
= p_fs
->dentries_in_root
;
1720 dentries_per_clu
= p_fs
->dentries_per_clu
;
1722 clu
.dir
= p_dir
->dir
;
1723 clu
.size
= p_dir
->size
;
1724 clu
.flags
= p_dir
->flags
;
1726 p_fs
->hint_uentry
.dir
= p_dir
->dir
;
1727 p_fs
->hint_uentry
.entry
= -1;
1729 while (clu
.dir
!= CLUSTER_32(~0)) {
1730 if (p_fs
->dev_ejected
)
1733 while (i
< dentries_per_clu
) {
1734 ep
= get_entry_in_dir(sb
, &clu
, i
, NULL
);
1738 entry_type
= p_fs
->fs_func
->get_entry_type(ep
);
1741 if ((entry_type
== TYPE_UNUSED
) || (entry_type
== TYPE_DELETED
)) {
1742 is_feasible_entry
= false;
1744 if (p_fs
->hint_uentry
.entry
== -1) {
1747 if (num_empty
== 1) {
1748 p_fs
->hint_uentry
.clu
.dir
= clu
.dir
;
1749 p_fs
->hint_uentry
.clu
.size
= clu
.size
;
1750 p_fs
->hint_uentry
.clu
.flags
= clu
.flags
;
1752 if ((num_empty
>= num_entries
) || (entry_type
== TYPE_UNUSED
))
1753 p_fs
->hint_uentry
.entry
= dentry
- (num_empty
- 1);
1756 if (entry_type
== TYPE_UNUSED
)
1761 if ((entry_type
== TYPE_FILE
) || (entry_type
== TYPE_DIR
)) {
1762 file_ep
= (struct file_dentry_t
*)ep
;
1763 if ((type
== TYPE_ALL
) || (type
== entry_type
)) {
1764 num_ext_entries
= file_ep
->num_ext
;
1765 is_feasible_entry
= true;
1767 is_feasible_entry
= false;
1768 step
= file_ep
->num_ext
+ 1;
1770 } else if (entry_type
== TYPE_STREAM
) {
1771 if (is_feasible_entry
) {
1772 strm_ep
= (struct strm_dentry_t
*)ep
;
1773 if (p_uniname
->name_hash
== GET16_A(strm_ep
->name_hash
) &&
1774 p_uniname
->name_len
== strm_ep
->name_len
) {
1777 is_feasible_entry
= false;
1778 step
= num_ext_entries
;
1781 } else if (entry_type
== TYPE_EXTEND
) {
1782 if (is_feasible_entry
) {
1783 name_ep
= (struct name_dentry_t
*)ep
;
1786 uniname
= p_uniname
->name
;
1790 len
= extract_uni_name_from_name_entry(name_ep
,
1791 entry_uniname
, order
);
1793 unichar
= *(uniname
+ len
);
1794 *(uniname
+ len
) = 0x0;
1796 if (nls_uniname_cmp(sb
, uniname
, entry_uniname
)) {
1797 is_feasible_entry
= false;
1798 step
= num_ext_entries
- order
+ 1;
1799 } else if (order
== num_ext_entries
) {
1800 p_fs
->hint_uentry
.dir
= CLUSTER_32(~0);
1801 p_fs
->hint_uentry
.entry
= -1;
1802 return dentry
- (num_ext_entries
);
1805 *(uniname
+ len
) = unichar
;
1808 is_feasible_entry
= false;
1816 i
-= dentries_per_clu
;
1818 if (p_dir
->dir
== CLUSTER_32(0))
1819 break; /* FAT16 root_dir */
1821 if (clu
.flags
== 0x03) {
1822 if ((--clu
.size
) > 0)
1825 clu
.dir
= CLUSTER_32(~0);
1827 if (exfat_fat_read(sb
, clu
.dir
, &clu
.dir
) != 0)
1835 static s32
exfat_count_ext_entries(struct super_block
*sb
, struct chain_t
*p_dir
,
1836 s32 entry
, struct dentry_t
*p_entry
)
1840 struct file_dentry_t
*file_ep
= (struct file_dentry_t
*)p_entry
;
1841 struct dentry_t
*ext_ep
;
1842 struct fs_info_t
*p_fs
= &(EXFAT_SB(sb
)->fs_info
);
1844 for (i
= 0, entry
++; i
< file_ep
->num_ext
; i
++, entry
++) {
1845 ext_ep
= get_entry_in_dir(sb
, p_dir
, entry
, NULL
);
1849 type
= p_fs
->fs_func
->get_entry_type(ext_ep
);
1850 if ((type
== TYPE_EXTEND
) || (type
== TYPE_STREAM
))
1859 s32
count_dos_name_entries(struct super_block
*sb
, struct chain_t
*p_dir
,
1863 s32 dentries_per_clu
;
1866 struct dentry_t
*ep
;
1867 struct fs_info_t
*p_fs
= &(EXFAT_SB(sb
)->fs_info
);
1869 if (p_dir
->dir
== CLUSTER_32(0)) /* FAT16 root_dir */
1870 dentries_per_clu
= p_fs
->dentries_in_root
;
1872 dentries_per_clu
= p_fs
->dentries_per_clu
;
1874 clu
.dir
= p_dir
->dir
;
1875 clu
.size
= p_dir
->size
;
1876 clu
.flags
= p_dir
->flags
;
1878 while (clu
.dir
!= CLUSTER_32(~0)) {
1879 if (p_fs
->dev_ejected
)
1882 for (i
= 0; i
< dentries_per_clu
; i
++) {
1883 ep
= get_entry_in_dir(sb
, &clu
, i
, NULL
);
1887 entry_type
= p_fs
->fs_func
->get_entry_type(ep
);
1889 if (entry_type
== TYPE_UNUSED
)
1891 if (!(type
& TYPE_CRITICAL_PRI
) &&
1892 !(type
& TYPE_BENIGN_PRI
))
1895 if ((type
== TYPE_ALL
) || (type
== entry_type
))
1899 if (p_dir
->dir
== CLUSTER_32(0))
1900 break; /* FAT16 root_dir */
1902 if (clu
.flags
== 0x03) {
1903 if ((--clu
.size
) > 0)
1906 clu
.dir
= CLUSTER_32(~0);
1908 if (exfat_fat_read(sb
, clu
.dir
, &clu
.dir
) != 0)
1916 bool is_dir_empty(struct super_block
*sb
, struct chain_t
*p_dir
)
1919 s32 dentries_per_clu
;
1922 struct dentry_t
*ep
;
1923 struct fs_info_t
*p_fs
= &(EXFAT_SB(sb
)->fs_info
);
1925 if (p_dir
->dir
== CLUSTER_32(0)) /* FAT16 root_dir */
1926 dentries_per_clu
= p_fs
->dentries_in_root
;
1928 dentries_per_clu
= p_fs
->dentries_per_clu
;
1930 clu
.dir
= p_dir
->dir
;
1931 clu
.size
= p_dir
->size
;
1932 clu
.flags
= p_dir
->flags
;
1934 while (clu
.dir
!= CLUSTER_32(~0)) {
1935 if (p_fs
->dev_ejected
)
1938 for (i
= 0; i
< dentries_per_clu
; i
++) {
1939 ep
= get_entry_in_dir(sb
, &clu
, i
, NULL
);
1943 type
= p_fs
->fs_func
->get_entry_type(ep
);
1945 if (type
== TYPE_UNUSED
)
1947 if ((type
!= TYPE_FILE
) && (type
!= TYPE_DIR
))
1950 if (p_dir
->dir
== CLUSTER_32(0)) /* FAT16 root_dir */
1953 if (p_fs
->vol_type
== EXFAT
)
1955 if ((p_dir
->dir
== p_fs
->root_dir
) || ((++count
) > 2))
1959 if (p_dir
->dir
== CLUSTER_32(0))
1960 break; /* FAT16 root_dir */
1962 if (clu
.flags
== 0x03) {
1963 if ((--clu
.size
) > 0)
1966 clu
.dir
= CLUSTER_32(~0);
1968 if (exfat_fat_read(sb
, clu
.dir
, &clu
.dir
) != 0)
1976 * Name Conversion Functions
1979 /* input : dir, uni_name
1980 * output : num_of_entry, dos_name(format : aaaaaa~1.bbb)
1982 s32
get_num_entries_and_dos_name(struct super_block
*sb
, struct chain_t
*p_dir
,
1983 struct uni_name_t
*p_uniname
, s32
*entries
,
1984 struct dos_name_t
*p_dosname
)
1987 struct fs_info_t
*p_fs
= &(EXFAT_SB(sb
)->fs_info
);
1989 num_entries
= p_fs
->fs_func
->calc_num_entries(p_uniname
);
1990 if (num_entries
== 0)
1993 *entries
= num_entries
;
1998 static void exfat_get_uni_name_from_ext_entry(struct super_block
*sb
,
1999 struct chain_t
*p_dir
, s32 entry
,
2003 struct dentry_t
*ep
;
2004 struct entry_set_cache_t
*es
;
2005 struct fs_info_t
*p_fs
= &(EXFAT_SB(sb
)->fs_info
);
2007 es
= get_entry_set_in_dir(sb
, p_dir
, entry
, ES_ALL_ENTRIES
, &ep
);
2008 if (!es
|| es
->num_entries
< 3) {
2010 release_entry_set(es
);
2017 * First entry : file entry
2018 * Second entry : stream-extension entry
2019 * Third entry : first file-name entry
2020 * So, the index of first file-name dentry should start from 2.
2022 for (i
= 2; i
< es
->num_entries
; i
++, ep
++) {
2023 if (p_fs
->fs_func
->get_entry_type(ep
) == TYPE_EXTEND
)
2024 extract_uni_name_from_name_entry((struct name_dentry_t
*)
2032 release_entry_set(es
);
2035 static s32
exfat_calc_num_entries(struct uni_name_t
*p_uniname
)
2039 len
= p_uniname
->name_len
;
2043 /* 1 file entry + 1 stream entry + name entries */
2044 return (len
- 1) / 15 + 3;
2047 u16
calc_checksum_2byte(void *data
, s32 len
, u16 chksum
, s32 type
)
2054 for (i
= 0; i
< len
; i
++, c
++) {
2055 if ((i
== 2) || (i
== 3))
2057 chksum
= (((chksum
& 1) << 15) |
2058 ((chksum
& 0xFFFE) >> 1)) + (u16
)*c
;
2063 for (i
= 0; i
< len
; i
++, c
++)
2064 chksum
= (((chksum
& 1) << 15) |
2065 ((chksum
& 0xFFFE) >> 1)) + (u16
)*c
;
2072 * Name Resolution Functions
2075 /* return values of resolve_path()
2076 * > 0 : return the length of the path
2077 * < 0 : return error
2079 s32
resolve_path(struct inode
*inode
, char *path
, struct chain_t
*p_dir
,
2080 struct uni_name_t
*p_uniname
)
2083 struct super_block
*sb
= inode
->i_sb
;
2084 struct fs_info_t
*p_fs
= &(EXFAT_SB(sb
)->fs_info
);
2085 struct file_id_t
*fid
= &(EXFAT_I(inode
)->fid
);
2087 if (strscpy(name_buf
, path
, sizeof(name_buf
)) < 0)
2090 nls_cstring_to_uniname(sb
, p_uniname
, name_buf
, &lossy
);
2094 fid
->size
= i_size_read(inode
);
2096 p_dir
->dir
= fid
->start_clu
;
2097 p_dir
->size
= (s32
)(fid
->size
>> p_fs
->cluster_size_bits
);
2098 p_dir
->flags
= fid
->flags
;
2104 * File Operation Functions
2106 static struct fs_func exfat_fs_func
= {
2107 .alloc_cluster
= exfat_alloc_cluster
,
2108 .free_cluster
= exfat_free_cluster
,
2109 .count_used_clusters
= exfat_count_used_clusters
,
2111 .init_dir_entry
= exfat_init_dir_entry
,
2112 .init_ext_entry
= exfat_init_ext_entry
,
2113 .find_dir_entry
= exfat_find_dir_entry
,
2114 .delete_dir_entry
= exfat_delete_dir_entry
,
2115 .get_uni_name_from_ext_entry
= exfat_get_uni_name_from_ext_entry
,
2116 .count_ext_entries
= exfat_count_ext_entries
,
2117 .calc_num_entries
= exfat_calc_num_entries
,
2119 .get_entry_type
= exfat_get_entry_type
,
2120 .set_entry_type
= exfat_set_entry_type
,
2121 .get_entry_attr
= exfat_get_entry_attr
,
2122 .set_entry_attr
= exfat_set_entry_attr
,
2123 .get_entry_flag
= exfat_get_entry_flag
,
2124 .set_entry_flag
= exfat_set_entry_flag
,
2125 .get_entry_clu0
= exfat_get_entry_clu0
,
2126 .set_entry_clu0
= exfat_set_entry_clu0
,
2127 .get_entry_size
= exfat_get_entry_size
,
2128 .set_entry_size
= exfat_set_entry_size
,
2129 .get_entry_time
= exfat_get_entry_time
,
2130 .set_entry_time
= exfat_set_entry_time
,
2133 s32
exfat_mount(struct super_block
*sb
, struct pbr_sector_t
*p_pbr
)
2135 struct bpbex_t
*p_bpb
= (struct bpbex_t
*)p_pbr
->bpb
;
2136 struct fs_info_t
*p_fs
= &(EXFAT_SB(sb
)->fs_info
);
2137 struct bd_info_t
*p_bd
= &(EXFAT_SB(sb
)->bd_info
);
2139 if (p_bpb
->num_fats
== 0)
2140 return -EFSCORRUPTED
;
2142 p_fs
->sectors_per_clu
= 1 << p_bpb
->sectors_per_clu_bits
;
2143 p_fs
->sectors_per_clu_bits
= p_bpb
->sectors_per_clu_bits
;
2144 p_fs
->cluster_size_bits
= p_fs
->sectors_per_clu_bits
+
2145 p_bd
->sector_size_bits
;
2146 p_fs
->cluster_size
= 1 << p_fs
->cluster_size_bits
;
2148 p_fs
->num_FAT_sectors
= GET32(p_bpb
->fat_length
);
2150 p_fs
->FAT1_start_sector
= p_fs
->PBR_sector
+ GET32(p_bpb
->fat_offset
);
2151 if (p_bpb
->num_fats
== 1)
2152 p_fs
->FAT2_start_sector
= p_fs
->FAT1_start_sector
;
2154 p_fs
->FAT2_start_sector
= p_fs
->FAT1_start_sector
+
2155 p_fs
->num_FAT_sectors
;
2157 p_fs
->root_start_sector
= p_fs
->PBR_sector
+ GET32(p_bpb
->clu_offset
);
2158 p_fs
->data_start_sector
= p_fs
->root_start_sector
;
2160 p_fs
->num_sectors
= GET64(p_bpb
->vol_length
);
2161 p_fs
->num_clusters
= GET32(p_bpb
->clu_count
) + 2;
2162 /* because the cluster index starts with 2 */
2164 p_fs
->vol_type
= EXFAT
;
2165 p_fs
->vol_id
= GET32(p_bpb
->vol_serial
);
2167 p_fs
->root_dir
= GET32(p_bpb
->root_cluster
);
2168 p_fs
->dentries_in_root
= 0;
2169 p_fs
->dentries_per_clu
= 1 << (p_fs
->cluster_size_bits
-
2172 p_fs
->vol_flag
= (u32
)GET16(p_bpb
->vol_flags
);
2173 p_fs
->clu_srch_ptr
= 2;
2174 p_fs
->used_clusters
= UINT_MAX
;
2176 p_fs
->fs_func
= &exfat_fs_func
;
2181 s32
create_dir(struct inode
*inode
, struct chain_t
*p_dir
,
2182 struct uni_name_t
*p_uniname
, struct file_id_t
*fid
)
2184 s32 ret
, dentry
, num_entries
;
2187 struct dos_name_t dos_name
;
2188 struct super_block
*sb
= inode
->i_sb
;
2189 struct fs_info_t
*p_fs
= &(EXFAT_SB(sb
)->fs_info
);
2190 struct fs_func
*fs_func
= p_fs
->fs_func
;
2192 ret
= get_num_entries_and_dos_name(sb
, p_dir
, p_uniname
, &num_entries
,
2197 /* find_empty_entry must be called before alloc_cluster */
2198 dentry
= find_empty_entry(inode
, p_dir
, num_entries
);
2202 clu
.dir
= CLUSTER_32(~0);
2204 clu
.flags
= (p_fs
->vol_type
== EXFAT
) ? 0x03 : 0x01;
2206 /* (1) allocate a cluster */
2207 ret
= fs_func
->alloc_cluster(sb
, 1, &clu
);
2213 ret
= clear_cluster(sb
, clu
.dir
);
2217 size
= p_fs
->cluster_size
;
2219 /* (2) update the directory entry */
2220 /* make sub-dir entry in parent directory */
2221 ret
= fs_func
->init_dir_entry(sb
, p_dir
, dentry
, TYPE_DIR
, clu
.dir
,
2226 ret
= fs_func
->init_ext_entry(sb
, p_dir
, dentry
, num_entries
, p_uniname
,
2231 fid
->dir
.dir
= p_dir
->dir
;
2232 fid
->dir
.size
= p_dir
->size
;
2233 fid
->dir
.flags
= p_dir
->flags
;
2234 fid
->entry
= dentry
;
2236 fid
->attr
= ATTR_SUBDIR
;
2237 fid
->flags
= (p_fs
->vol_type
== EXFAT
) ? 0x03 : 0x01;
2239 fid
->start_clu
= clu
.dir
;
2241 fid
->type
= TYPE_DIR
;
2243 fid
->hint_last_off
= -1;
2248 s32
create_file(struct inode
*inode
, struct chain_t
*p_dir
,
2249 struct uni_name_t
*p_uniname
, u8 mode
, struct file_id_t
*fid
)
2251 s32 ret
, dentry
, num_entries
;
2252 struct dos_name_t dos_name
;
2253 struct super_block
*sb
= inode
->i_sb
;
2254 struct fs_info_t
*p_fs
= &(EXFAT_SB(sb
)->fs_info
);
2255 struct fs_func
*fs_func
= p_fs
->fs_func
;
2257 ret
= get_num_entries_and_dos_name(sb
, p_dir
, p_uniname
, &num_entries
,
2262 /* find_empty_entry must be called before alloc_cluster() */
2263 dentry
= find_empty_entry(inode
, p_dir
, num_entries
);
2267 /* (1) update the directory entry */
2268 /* fill the dos name directory entry information of the created file.
2269 * the first cluster is not determined yet. (0)
2271 ret
= fs_func
->init_dir_entry(sb
, p_dir
, dentry
, TYPE_FILE
| mode
,
2276 ret
= fs_func
->init_ext_entry(sb
, p_dir
, dentry
, num_entries
, p_uniname
,
2281 fid
->dir
.dir
= p_dir
->dir
;
2282 fid
->dir
.size
= p_dir
->size
;
2283 fid
->dir
.flags
= p_dir
->flags
;
2284 fid
->entry
= dentry
;
2286 fid
->attr
= ATTR_ARCHIVE
| mode
;
2287 fid
->flags
= (p_fs
->vol_type
== EXFAT
) ? 0x03 : 0x01;
2289 fid
->start_clu
= CLUSTER_32(~0);
2291 fid
->type
= TYPE_FILE
;
2293 fid
->hint_last_off
= -1;
2298 void remove_file(struct inode
*inode
, struct chain_t
*p_dir
, s32 entry
)
2302 struct dentry_t
*ep
;
2303 struct super_block
*sb
= inode
->i_sb
;
2304 struct fs_info_t
*p_fs
= &(EXFAT_SB(sb
)->fs_info
);
2305 struct fs_func
*fs_func
= p_fs
->fs_func
;
2307 ep
= get_entry_in_dir(sb
, p_dir
, entry
, §or
);
2311 exfat_buf_lock(sb
, sector
);
2313 /* exfat_buf_lock() before call count_ext_entries() */
2314 num_entries
= fs_func
->count_ext_entries(sb
, p_dir
, entry
, ep
);
2315 if (num_entries
< 0) {
2316 exfat_buf_unlock(sb
, sector
);
2321 exfat_buf_unlock(sb
, sector
);
2323 /* (1) update the directory entry */
2324 fs_func
->delete_dir_entry(sb
, p_dir
, entry
, 0, num_entries
);
2327 s32
exfat_rename_file(struct inode
*inode
, struct chain_t
*p_dir
, s32 oldentry
,
2328 struct uni_name_t
*p_uniname
, struct file_id_t
*fid
)
2330 s32 ret
, newentry
= -1, num_old_entries
, num_new_entries
;
2331 sector_t sector_old
, sector_new
;
2332 struct dos_name_t dos_name
;
2333 struct dentry_t
*epold
, *epnew
;
2334 struct super_block
*sb
= inode
->i_sb
;
2335 struct fs_info_t
*p_fs
= &(EXFAT_SB(sb
)->fs_info
);
2336 struct fs_func
*fs_func
= p_fs
->fs_func
;
2338 epold
= get_entry_in_dir(sb
, p_dir
, oldentry
, §or_old
);
2342 exfat_buf_lock(sb
, sector_old
);
2344 /* exfat_buf_lock() before call count_ext_entries() */
2345 num_old_entries
= fs_func
->count_ext_entries(sb
, p_dir
, oldentry
,
2347 if (num_old_entries
< 0) {
2348 exfat_buf_unlock(sb
, sector_old
);
2353 ret
= get_num_entries_and_dos_name(sb
, p_dir
, p_uniname
,
2354 &num_new_entries
, &dos_name
);
2356 exfat_buf_unlock(sb
, sector_old
);
2360 if (num_old_entries
< num_new_entries
) {
2361 newentry
= find_empty_entry(inode
, p_dir
, num_new_entries
);
2363 exfat_buf_unlock(sb
, sector_old
);
2367 epnew
= get_entry_in_dir(sb
, p_dir
, newentry
, §or_new
);
2369 exfat_buf_unlock(sb
, sector_old
);
2373 memcpy((void *)epnew
, (void *)epold
, DENTRY_SIZE
);
2374 if (fs_func
->get_entry_type(epnew
) == TYPE_FILE
) {
2375 fs_func
->set_entry_attr(epnew
,
2376 fs_func
->get_entry_attr(epnew
) |
2378 fid
->attr
|= ATTR_ARCHIVE
;
2380 exfat_buf_modify(sb
, sector_new
);
2381 exfat_buf_unlock(sb
, sector_old
);
2383 epold
= get_entry_in_dir(sb
, p_dir
, oldentry
+ 1,
2385 exfat_buf_lock(sb
, sector_old
);
2386 epnew
= get_entry_in_dir(sb
, p_dir
, newentry
+ 1,
2389 if (!epold
|| !epnew
) {
2390 exfat_buf_unlock(sb
, sector_old
);
2394 memcpy((void *)epnew
, (void *)epold
, DENTRY_SIZE
);
2395 exfat_buf_modify(sb
, sector_new
);
2396 exfat_buf_unlock(sb
, sector_old
);
2398 ret
= fs_func
->init_ext_entry(sb
, p_dir
, newentry
,
2399 num_new_entries
, p_uniname
,
2404 fs_func
->delete_dir_entry(sb
, p_dir
, oldentry
, 0,
2406 fid
->entry
= newentry
;
2408 if (fs_func
->get_entry_type(epold
) == TYPE_FILE
) {
2409 fs_func
->set_entry_attr(epold
,
2410 fs_func
->get_entry_attr(epold
) |
2412 fid
->attr
|= ATTR_ARCHIVE
;
2414 exfat_buf_modify(sb
, sector_old
);
2415 exfat_buf_unlock(sb
, sector_old
);
2417 ret
= fs_func
->init_ext_entry(sb
, p_dir
, oldentry
,
2418 num_new_entries
, p_uniname
,
2423 fs_func
->delete_dir_entry(sb
, p_dir
, oldentry
, num_new_entries
,
2430 s32
move_file(struct inode
*inode
, struct chain_t
*p_olddir
, s32 oldentry
,
2431 struct chain_t
*p_newdir
, struct uni_name_t
*p_uniname
,
2432 struct file_id_t
*fid
)
2434 s32 ret
, newentry
, num_new_entries
, num_old_entries
;
2435 sector_t sector_mov
, sector_new
;
2436 struct dos_name_t dos_name
;
2437 struct dentry_t
*epmov
, *epnew
;
2438 struct super_block
*sb
= inode
->i_sb
;
2439 struct fs_info_t
*p_fs
= &(EXFAT_SB(sb
)->fs_info
);
2440 struct fs_func
*fs_func
= p_fs
->fs_func
;
2442 epmov
= get_entry_in_dir(sb
, p_olddir
, oldentry
, §or_mov
);
2446 /* check if the source and target directory is the same */
2447 if (fs_func
->get_entry_type(epmov
) == TYPE_DIR
&&
2448 fs_func
->get_entry_clu0(epmov
) == p_newdir
->dir
)
2451 exfat_buf_lock(sb
, sector_mov
);
2453 /* exfat_buf_lock() before call count_ext_entries() */
2454 num_old_entries
= fs_func
->count_ext_entries(sb
, p_olddir
, oldentry
,
2456 if (num_old_entries
< 0) {
2457 exfat_buf_unlock(sb
, sector_mov
);
2462 ret
= get_num_entries_and_dos_name(sb
, p_newdir
, p_uniname
,
2463 &num_new_entries
, &dos_name
);
2465 exfat_buf_unlock(sb
, sector_mov
);
2469 newentry
= find_empty_entry(inode
, p_newdir
, num_new_entries
);
2471 exfat_buf_unlock(sb
, sector_mov
);
2475 epnew
= get_entry_in_dir(sb
, p_newdir
, newentry
, §or_new
);
2477 exfat_buf_unlock(sb
, sector_mov
);
2481 memcpy((void *)epnew
, (void *)epmov
, DENTRY_SIZE
);
2482 if (fs_func
->get_entry_type(epnew
) == TYPE_FILE
) {
2483 fs_func
->set_entry_attr(epnew
, fs_func
->get_entry_attr(epnew
) |
2485 fid
->attr
|= ATTR_ARCHIVE
;
2487 exfat_buf_modify(sb
, sector_new
);
2488 exfat_buf_unlock(sb
, sector_mov
);
2490 epmov
= get_entry_in_dir(sb
, p_olddir
, oldentry
+ 1,
2492 exfat_buf_lock(sb
, sector_mov
);
2493 epnew
= get_entry_in_dir(sb
, p_newdir
, newentry
+ 1,
2495 if (!epmov
|| !epnew
) {
2496 exfat_buf_unlock(sb
, sector_mov
);
2500 memcpy((void *)epnew
, (void *)epmov
, DENTRY_SIZE
);
2501 exfat_buf_modify(sb
, sector_new
);
2502 exfat_buf_unlock(sb
, sector_mov
);
2504 ret
= fs_func
->init_ext_entry(sb
, p_newdir
, newentry
, num_new_entries
,
2505 p_uniname
, &dos_name
);
2509 fs_func
->delete_dir_entry(sb
, p_olddir
, oldentry
, 0, num_old_entries
);
2511 fid
->dir
.dir
= p_newdir
->dir
;
2512 fid
->dir
.size
= p_newdir
->size
;
2513 fid
->dir
.flags
= p_newdir
->flags
;
2515 fid
->entry
= newentry
;
2521 * Sector Read/Write Functions
2524 int sector_read(struct super_block
*sb
, sector_t sec
, struct buffer_head
**bh
,
2528 struct fs_info_t
*p_fs
= &(EXFAT_SB(sb
)->fs_info
);
2530 if ((sec
>= (p_fs
->PBR_sector
+ p_fs
->num_sectors
)) &&
2531 (p_fs
->num_sectors
> 0)) {
2532 pr_err("[EXFAT] %s: out of range error! (sec = %llu)\n",
2533 __func__
, (unsigned long long)sec
);
2538 if (!p_fs
->dev_ejected
) {
2539 ret
= exfat_bdev_read(sb
, sec
, bh
, 1, read
);
2541 p_fs
->dev_ejected
= 1;
2547 int sector_write(struct super_block
*sb
, sector_t sec
, struct buffer_head
*bh
,
2551 struct fs_info_t
*p_fs
= &(EXFAT_SB(sb
)->fs_info
);
2553 if (sec
>= (p_fs
->PBR_sector
+ p_fs
->num_sectors
) &&
2554 (p_fs
->num_sectors
> 0)) {
2555 pr_err("[EXFAT] %s: out of range error! (sec = %llu)\n",
2556 __func__
, (unsigned long long)sec
);
2562 pr_err("[EXFAT] %s: bh is NULL!\n", __func__
);
2567 if (!p_fs
->dev_ejected
) {
2568 ret
= exfat_bdev_write(sb
, sec
, bh
, 1, sync
);
2570 p_fs
->dev_ejected
= 1;
2576 int multi_sector_read(struct super_block
*sb
, sector_t sec
,
2577 struct buffer_head
**bh
, s32 num_secs
, bool read
)
2580 struct fs_info_t
*p_fs
= &(EXFAT_SB(sb
)->fs_info
);
2582 if (((sec
+ num_secs
) > (p_fs
->PBR_sector
+ p_fs
->num_sectors
)) &&
2583 (p_fs
->num_sectors
> 0)) {
2584 pr_err("[EXFAT] %s: out of range error! (sec = %llu, num_secs = %d)\n",
2585 __func__
, (unsigned long long)sec
, num_secs
);
2590 if (!p_fs
->dev_ejected
) {
2591 ret
= exfat_bdev_read(sb
, sec
, bh
, num_secs
, read
);
2593 p_fs
->dev_ejected
= 1;
2599 int multi_sector_write(struct super_block
*sb
, sector_t sec
,
2600 struct buffer_head
*bh
, s32 num_secs
, bool sync
)
2603 struct fs_info_t
*p_fs
= &(EXFAT_SB(sb
)->fs_info
);
2605 if ((sec
+ num_secs
) > (p_fs
->PBR_sector
+ p_fs
->num_sectors
) &&
2606 (p_fs
->num_sectors
> 0)) {
2607 pr_err("[EXFAT] %s: out of range error! (sec = %llu, num_secs = %d)\n",
2608 __func__
, (unsigned long long)sec
, num_secs
);
2613 pr_err("[EXFAT] %s: bh is NULL!\n", __func__
);
2618 if (!p_fs
->dev_ejected
) {
2619 ret
= exfat_bdev_write(sb
, sec
, bh
, num_secs
, sync
);
2621 p_fs
->dev_ejected
= 1;