2 * Copyright (C) 2012-2013 Samsung Electronics Co., Ltd.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, see <http://www.gnu.org/licenses/>.
18 /************************************************************************/
20 /* PROJECT : exFAT & FAT12/16/32 File System */
21 /* FILE : core_exfat.c */
22 /* PURPOSE : exFAT-fs core code for sdFAT */
24 /*----------------------------------------------------------------------*/
28 /************************************************************************/
30 #include <linux/version.h>
31 #include <linux/blkdev.h>
32 #include <linux/workqueue.h>
33 #include <linux/kernel.h>
34 #include <linux/log2.h>
38 #include <asm/byteorder.h>
39 #include <asm/unaligned.h>
41 /*----------------------------------------------------------------------*/
42 /* Constant & Macro Definitions */
43 /*----------------------------------------------------------------------*/
45 /*----------------------------------------------------------------------*/
46 /* Global Variable Definitions */
47 /*----------------------------------------------------------------------*/
49 /*----------------------------------------------------------------------*/
50 /* Local Variable Definitions */
51 /*----------------------------------------------------------------------*/
52 static u8 free_bit
[] = {
53 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, 4, 0, 1, 0, 2,/* 0 ~ 19*/
54 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, 5, 0, 1, 0, 2, 0, 1, 0, 3,/* 20 ~ 39*/
55 0, 1, 0, 2, 0, 1, 0, 4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2,/* 40 ~ 59*/
56 0, 1, 0, 6, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, 4,/* 60 ~ 79*/
57 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, 5, 0, 1, 0, 2,/* 80 ~ 99*/
58 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, 4, 0, 1, 0, 2, 0, 1, 0, 3,/*100 ~ 119*/
59 0, 1, 0, 2, 0, 1, 0, 7, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2,/*120 ~ 139*/
60 0, 1, 0, 4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, 5,/*140 ~ 159*/
61 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, 4, 0, 1, 0, 2,/*160 ~ 179*/
62 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, 6, 0, 1, 0, 2, 0, 1, 0, 3,/*180 ~ 199*/
63 0, 1, 0, 2, 0, 1, 0, 4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2,/*200 ~ 219*/
64 0, 1, 0, 5, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, 4,/*220 ~ 239*/
65 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0 /*240 ~ 254*/
68 static u8 used_bit
[] = {
69 0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4, 1, 2, 2, 3,/* 0 ~ 19*/
70 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5, 1, 2, 2, 3, 2, 3, 3, 4,/* 20 ~ 39*/
71 2, 3, 3, 4, 3, 4, 4, 5, 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5,/* 40 ~ 59*/
72 4, 5, 5, 6, 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5,/* 60 ~ 79*/
73 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6, 2, 3, 3, 4,/* 80 ~ 99*/
74 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6, 3, 4, 4, 5, 4, 5, 5, 6,/*100 ~ 119*/
75 4, 5, 5, 6, 5, 6, 6, 7, 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4,/*120 ~ 139*/
76 3, 4, 4, 5, 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,/*140 ~ 159*/
77 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6, 3, 4, 4, 5,/*160 ~ 179*/
78 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7, 2, 3, 3, 4, 3, 4, 4, 5,/*180 ~ 199*/
79 3, 4, 4, 5, 4, 5, 5, 6, 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6,/*200 ~ 219*/
80 5, 6, 6, 7, 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,/*220 ~ 239*/
81 4, 5, 5, 6, 5, 6, 6, 7, 5, 6, 6, 7, 6, 7, 7, 8 /*240 ~ 255*/
85 /*======================================================================*/
86 /* Local Function Definitions */
87 /*======================================================================*/
89 * Directory Entry Management Functions
91 static u32
exfat_get_entry_type(DENTRY_T
*p_entry
)
93 FILE_DENTRY_T
*ep
= (FILE_DENTRY_T
*) p_entry
;
95 if (ep
->type
== EXFAT_UNUSED
)
101 if (ep
->type
< 0xA0) {
102 if (ep
->type
== 0x81)
104 if (ep
->type
== 0x82)
106 if (ep
->type
== 0x83)
108 if (ep
->type
== 0x85) {
109 if (le16_to_cpu(ep
->attr
) & ATTR_SUBDIR
)
113 return TYPE_CRITICAL_PRI
;
115 if (ep
->type
< 0xC0) {
116 if (ep
->type
== 0xA0)
118 if (ep
->type
== 0xA1)
120 if (ep
->type
== 0xA2)
122 return TYPE_BENIGN_PRI
;
124 if (ep
->type
< 0xE0) {
125 if (ep
->type
== 0xC0)
127 if (ep
->type
== 0xC1)
129 if (ep
->type
== 0xC2)
131 return TYPE_CRITICAL_SEC
;
133 return TYPE_BENIGN_SEC
;
134 } /* end of exfat_get_entry_type */
136 static void exfat_set_entry_type(DENTRY_T
*p_entry
, u32 type
)
138 FILE_DENTRY_T
*ep
= (FILE_DENTRY_T
*) p_entry
;
140 if (type
== TYPE_UNUSED
) {
142 } else if (type
== TYPE_DELETED
) {
144 } else if (type
== TYPE_STREAM
) {
146 } else if (type
== TYPE_EXTEND
) {
148 } else if (type
== TYPE_BITMAP
) {
150 } else if (type
== TYPE_UPCASE
) {
152 } else if (type
== TYPE_VOLUME
) {
154 } else if (type
== TYPE_DIR
) {
156 ep
->attr
= cpu_to_le16(ATTR_SUBDIR
);
157 } else if (type
== TYPE_FILE
) {
159 ep
->attr
= cpu_to_le16(ATTR_ARCHIVE
);
160 } else if (type
== TYPE_SYMLINK
) {
162 ep
->attr
= cpu_to_le16(ATTR_ARCHIVE
| ATTR_SYMLINK
);
164 } /* end of exfat_set_entry_type */
166 static u32
exfat_get_entry_attr(DENTRY_T
*p_entry
)
168 FILE_DENTRY_T
*ep
= (FILE_DENTRY_T
*)p_entry
;
170 return (u32
)le16_to_cpu(ep
->attr
);
171 } /* end of exfat_get_entry_attr */
173 static void exfat_set_entry_attr(DENTRY_T
*p_entry
, u32 attr
)
175 FILE_DENTRY_T
*ep
= (FILE_DENTRY_T
*)p_entry
;
177 ep
->attr
= cpu_to_le16((u16
) attr
);
178 } /* end of exfat_set_entry_attr */
180 static u8
exfat_get_entry_flag(DENTRY_T
*p_entry
)
182 STRM_DENTRY_T
*ep
= (STRM_DENTRY_T
*)p_entry
;
185 } /* end of exfat_get_entry_flag */
187 static void exfat_set_entry_flag(DENTRY_T
*p_entry
, u8 flags
)
189 STRM_DENTRY_T
*ep
= (STRM_DENTRY_T
*)p_entry
;
192 } /* end of exfat_set_entry_flag */
194 static u32
exfat_get_entry_clu0(DENTRY_T
*p_entry
)
196 STRM_DENTRY_T
*ep
= (STRM_DENTRY_T
*)p_entry
;
198 return (u32
)le32_to_cpu(ep
->start_clu
);
199 } /* end of exfat_get_entry_clu0 */
201 static void exfat_set_entry_clu0(DENTRY_T
*p_entry
, u32 start_clu
)
203 STRM_DENTRY_T
*ep
= (STRM_DENTRY_T
*)p_entry
;
205 ep
->start_clu
= cpu_to_le32(start_clu
);
206 } /* end of exfat_set_entry_clu0 */
208 static u64
exfat_get_entry_size(DENTRY_T
*p_entry
)
210 STRM_DENTRY_T
*ep
= (STRM_DENTRY_T
*)p_entry
;
212 return le64_to_cpu(ep
->valid_size
);
213 } /* end of exfat_get_entry_size */
215 static void exfat_set_entry_size(DENTRY_T
*p_entry
, u64 size
)
217 STRM_DENTRY_T
*ep
= (STRM_DENTRY_T
*)p_entry
;
219 ep
->valid_size
= cpu_to_le64(size
);
220 ep
->size
= cpu_to_le64(size
);
221 } /* end of exfat_set_entry_size */
224 #define TENS_MS_PER_SEC (100)
225 #define SEC_TO_TENS_MS(sec) (((sec) & 0x01) ? TENS_MS_PER_SEC : 0)
226 #define TENS_MS_TO_SEC(tens_ms) (((tens_ms) / TENS_MS_PER_SEC) ? 1 : 0)
228 static void exfat_get_entry_time(DENTRY_T
*p_entry
, TIMESTAMP_T
*tp
, u8 mode
)
230 u16 t
= 0x00, d
= 0x21, tz
= 0x00, s
= 0x00;
231 FILE_DENTRY_T
*ep
= (FILE_DENTRY_T
*)p_entry
;
235 t
= le16_to_cpu(ep
->create_time
);
236 d
= le16_to_cpu(ep
->create_date
);
237 s
= TENS_MS_TO_SEC(ep
->create_time_ms
);
241 t
= le16_to_cpu(ep
->modify_time
);
242 d
= le16_to_cpu(ep
->modify_date
);
243 s
= TENS_MS_TO_SEC(ep
->modify_time_ms
);
247 t
= le16_to_cpu(ep
->access_time
);
248 d
= le16_to_cpu(ep
->access_date
);
254 tp
->sec
= ((t
& 0x001F) << 1) + s
;
255 tp
->min
= (t
>> 5) & 0x003F;
256 tp
->hour
= (t
>> 11);
257 tp
->day
= (d
& 0x001F);
258 tp
->mon
= (d
>> 5) & 0x000F;
260 } /* end of exfat_get_entry_time */
262 static void exfat_set_entry_time(DENTRY_T
*p_entry
, TIMESTAMP_T
*tp
, u8 mode
)
265 FILE_DENTRY_T
*ep
= (FILE_DENTRY_T
*)p_entry
;
267 t
= (tp
->hour
<< 11) | (tp
->min
<< 5) | (tp
->sec
>> 1);
268 d
= (tp
->year
<< 9) | (tp
->mon
<< 5) | tp
->day
;
272 ep
->create_time
= cpu_to_le16(t
);
273 ep
->create_time_ms
= SEC_TO_TENS_MS(tp
->sec
);
274 ep
->create_date
= cpu_to_le16(d
);
275 ep
->create_tz
= tp
->tz
.value
;
278 ep
->modify_time
= cpu_to_le16(t
);
279 ep
->modify_date
= cpu_to_le16(d
);
280 ep
->modify_time_ms
= (tp
->sec
& 0x1) ? TENS_MS_PER_SEC
: 0;
281 ep
->modify_tz
= tp
->tz
.value
;
284 ep
->access_time
= cpu_to_le16(t
);
285 ep
->access_date
= cpu_to_le16(d
);
286 ep
->access_tz
= tp
->tz
.value
;
289 } /* end of exfat_set_entry_time */
292 static void __init_file_entry(struct super_block
*sb
, FILE_DENTRY_T
*ep
, u32 type
)
296 exfat_set_entry_type((DENTRY_T
*) ep
, type
);
298 tp
= tm_now_sb(sb
, &tm
);
299 exfat_set_entry_time((DENTRY_T
*) ep
, tp
, TM_CREATE
);
300 exfat_set_entry_time((DENTRY_T
*) ep
, tp
, TM_MODIFY
);
301 exfat_set_entry_time((DENTRY_T
*) ep
, tp
, TM_ACCESS
);
302 } /* end of __init_file_entry */
304 static void __init_strm_entry(STRM_DENTRY_T
*ep
, u8 flags
, u32 start_clu
, u64 size
)
306 exfat_set_entry_type((DENTRY_T
*) ep
, TYPE_STREAM
);
308 ep
->start_clu
= cpu_to_le32(start_clu
);
309 ep
->valid_size
= cpu_to_le64(size
);
310 ep
->size
= cpu_to_le64(size
);
311 } /* end of __init_strm_entry */
313 static void __init_name_entry(NAME_DENTRY_T
*ep
, u16
*uniname
)
317 exfat_set_entry_type((DENTRY_T
*) ep
, TYPE_EXTEND
);
320 for (i
= 0; i
< 15; i
++) {
321 ep
->unicode_0_14
[i
] = cpu_to_le16(*uniname
);
326 } /* end of __init_name_entry */
328 static s32
exfat_init_dir_entry(struct super_block
*sb
, CHAIN_T
*p_dir
, s32 entry
, u32 type
, u32 start_clu
, u64 size
)
332 FILE_DENTRY_T
*file_ep
;
333 STRM_DENTRY_T
*strm_ep
;
335 flags
= (type
== TYPE_FILE
) ? 0x01 : 0x03;
337 /* we cannot use get_dentry_set_in_dir here because file ep is not initialized yet */
338 file_ep
= (FILE_DENTRY_T
*)get_dentry_in_dir(sb
, p_dir
, entry
, §or
);
342 strm_ep
= (STRM_DENTRY_T
*)get_dentry_in_dir(sb
, p_dir
, entry
+1, §or
);
346 __init_file_entry(sb
, file_ep
, type
);
347 if (dcache_modify(sb
, sector
))
350 __init_strm_entry(strm_ep
, flags
, start_clu
, size
);
351 if (dcache_modify(sb
, sector
))
355 } /* end of exfat_init_dir_entry */
357 s32
update_dir_chksum(struct super_block
*sb
, CHAIN_T
*p_dir
, s32 entry
)
363 FILE_DENTRY_T
*file_ep
;
366 file_ep
= (FILE_DENTRY_T
*)get_dentry_in_dir(sb
, p_dir
, entry
, §or
);
370 dcache_lock(sb
, sector
);
372 num_entries
= (s32
) file_ep
->num_ext
+ 1;
373 chksum
= calc_chksum_2byte((void *) file_ep
, DENTRY_SIZE
, 0, CS_DIR_ENTRY
);
375 for (i
= 1; i
< num_entries
; i
++) {
376 ep
= get_dentry_in_dir(sb
, p_dir
, entry
+i
, NULL
);
380 chksum
= calc_chksum_2byte((void *) ep
, DENTRY_SIZE
, chksum
, CS_DEFAULT
);
383 file_ep
->checksum
= cpu_to_le16(chksum
);
384 ret
= dcache_modify(sb
, sector
);
386 dcache_unlock(sb
, sector
);
389 } /* end of update_dir_chksum */
392 static s32
exfat_init_ext_entry(struct super_block
*sb
, CHAIN_T
*p_dir
, s32 entry
, s32 num_entries
,
393 UNI_NAME_T
*p_uniname
, DOS_NAME_T
*p_dosname
)
397 u16
*uniname
= p_uniname
->name
;
398 FILE_DENTRY_T
*file_ep
;
399 STRM_DENTRY_T
*strm_ep
;
400 NAME_DENTRY_T
*name_ep
;
402 file_ep
= (FILE_DENTRY_T
*)get_dentry_in_dir(sb
, p_dir
, entry
, §or
);
406 file_ep
->num_ext
= (u8
)(num_entries
- 1);
407 dcache_modify(sb
, sector
);
409 strm_ep
= (STRM_DENTRY_T
*)get_dentry_in_dir(sb
, p_dir
, entry
+1, §or
);
413 strm_ep
->name_len
= p_uniname
->name_len
;
414 strm_ep
->name_hash
= cpu_to_le16(p_uniname
->name_hash
);
415 dcache_modify(sb
, sector
);
417 for (i
= 2; i
< num_entries
; i
++) {
418 name_ep
= (NAME_DENTRY_T
*)get_dentry_in_dir(sb
, p_dir
, entry
+i
, §or
);
422 __init_name_entry(name_ep
, uniname
);
423 dcache_modify(sb
, sector
);
427 update_dir_chksum(sb
, p_dir
, entry
);
430 } /* end of exfat_init_ext_entry */
433 static s32
exfat_delete_dir_entry(struct super_block
*sb
, CHAIN_T
*p_dir
, s32 entry
, s32 order
, s32 num_entries
)
439 for (i
= order
; i
< num_entries
; i
++) {
440 ep
= get_dentry_in_dir(sb
, p_dir
, entry
+i
, §or
);
444 exfat_set_entry_type(ep
, TYPE_DELETED
);
445 if (dcache_modify(sb
, sector
))
452 static s32
__write_partial_entries_in_entry_set(struct super_block
*sb
,
453 ENTRY_SET_CACHE_T
*es
, u64 sec
, u32 off
, u32 count
)
456 u32 buf_off
= (off
- es
->offset
);
457 u32 remaining_byte_in_sector
, copy_entries
;
458 FS_INFO_T
*fsi
= &(SDFAT_SB(sb
)->fsi
);
460 u8
*buf
, *esbuf
= (u8
*)&(es
->__buf
);
462 TMSG("%s entered\n", __func__
);
463 MMSG("%s: es %p sec %llu off %u cnt %d\n", __func__
, es
, sec
, off
, count
);
466 while (num_entries
) {
467 /* write per sector base */
468 remaining_byte_in_sector
= (1 << sb
->s_blocksize_bits
) - off
;
469 copy_entries
= min((s32
)(remaining_byte_in_sector
>> DENTRY_SIZE_BITS
), num_entries
);
470 buf
= dcache_getblk(sb
, sec
);
473 MMSG("es->buf %p buf_off %u\n", esbuf
, buf_off
);
474 MMSG("copying %d entries from %p to sector %llu\n", copy_entries
, (esbuf
+ buf_off
), sec
);
475 memcpy(buf
+ off
, esbuf
+ buf_off
, copy_entries
<< DENTRY_SIZE_BITS
);
476 dcache_modify(sb
, sec
);
477 num_entries
-= copy_entries
;
481 if (IS_LAST_SECT_IN_CLUS(fsi
, sec
)) {
482 clu
= SECT_TO_CLUS(fsi
, sec
);
483 if (es
->alloc_flag
== 0x03)
485 else if (get_next_clus_safe(sb
, &clu
))
487 sec
= CLUS_TO_SECT(fsi
, clu
);
492 buf_off
+= copy_entries
<< DENTRY_SIZE_BITS
;
496 TMSG("%s exited successfully\n", __func__
);
499 TMSG("%s failed\n", __func__
);
503 /* write back all entries in entry set */
504 static s32
__write_whole_entry_set(struct super_block
*sb
, ENTRY_SET_CACHE_T
*es
)
506 return __write_partial_entries_in_entry_set(sb
, es
, es
->sector
, es
->offset
, es
->num_entries
);
509 s32
update_dir_chksum_with_entry_set(struct super_block
*sb
, ENTRY_SET_CACHE_T
*es
)
513 s32 chksum_type
= CS_DIR_ENTRY
, i
;
515 ep
= (DENTRY_T
*)&(es
->__buf
);
516 for (i
= 0; i
< es
->num_entries
; i
++) {
517 MMSG("%s %p\n", __func__
, ep
);
518 chksum
= calc_chksum_2byte((void *) ep
, DENTRY_SIZE
, chksum
, chksum_type
);
520 chksum_type
= CS_DEFAULT
;
523 ep
= (DENTRY_T
*)&(es
->__buf
);
524 ((FILE_DENTRY_T
*)ep
)->checksum
= cpu_to_le16(chksum
);
525 return __write_whole_entry_set(sb
, es
);
528 /* returns a set of dentries for a file or dir.
529 * Note that this is a copy (dump) of dentries so that user should call write_entry_set()
530 * to apply changes made in this entry set to the real device.
532 * sb+p_dir+entry: indicates a file/dir
533 * type: specifies how many dentries should be included.
535 * file_ep: will point the first dentry(= file dentry) on success
537 * pointer of entry set on success,
541 #define ES_MODE_STARTED 0
542 #define ES_MODE_GET_FILE_ENTRY 1
543 #define ES_MODE_GET_STRM_ENTRY 2
544 #define ES_MODE_GET_NAME_ENTRY 3
545 #define ES_MODE_GET_CRITICAL_SEC_ENTRY 4
546 ENTRY_SET_CACHE_T
*get_dentry_set_in_dir(struct super_block
*sb
,
547 CHAIN_T
*p_dir
, s32 entry
, u32 type
, DENTRY_T
**file_ep
)
550 u32 off
, byte_offset
, clu
= 0;
553 FS_INFO_T
*fsi
= &(SDFAT_SB(sb
)->fsi
);
554 ENTRY_SET_CACHE_T
*es
= NULL
;
558 s32 mode
= ES_MODE_STARTED
;
560 /* FIXME : is available in error case? */
561 if (p_dir
->dir
== DIR_DELETED
) {
562 EMSG("%s : access to deleted dentry\n", __func__
);
563 BUG_ON(!fsi
->prev_eio
);
567 TMSG("%s entered\n", __func__
);
568 MMSG("p_dir dir %u flags %x size %d\n", p_dir
->dir
, p_dir
->flags
, p_dir
->size
);
569 MMSG("entry %d type %d\n", entry
, type
);
571 byte_offset
= entry
<< DENTRY_SIZE_BITS
;
572 ret
= walk_fat_chain(sb
, p_dir
, byte_offset
, &clu
);
576 /* byte offset in cluster */
577 byte_offset
&= fsi
->cluster_size
- 1;
579 /* byte offset in sector */
580 off
= byte_offset
& (u32
)(sb
->s_blocksize
- 1);
582 /* sector offset in cluster */
583 sec
= byte_offset
>> (sb
->s_blocksize_bits
);
584 sec
+= CLUS_TO_SECT(fsi
, clu
);
586 buf
= dcache_getblk(sb
, sec
);
590 ep
= (DENTRY_T
*)(buf
+ off
);
591 entry_type
= exfat_get_entry_type(ep
);
593 if ((entry_type
!= TYPE_FILE
)
594 && (entry_type
!= TYPE_DIR
))
597 if (type
== ES_ALL_ENTRIES
)
598 num_entries
= ((FILE_DENTRY_T
*)ep
)->num_ext
+1;
602 MMSG("trying to malloc %lx bytes for %d entries\n",
603 (unsigned long)(offsetof(ENTRY_SET_CACHE_T
, __buf
) + (num_entries
) * sizeof(DENTRY_T
)), num_entries
);
604 es
= kmalloc((offsetof(ENTRY_SET_CACHE_T
, __buf
) + (num_entries
) * sizeof(DENTRY_T
)), GFP_KERNEL
);
606 EMSG("%s: failed to alloc entryset\n", __func__
);
610 es
->num_entries
= num_entries
;
613 es
->alloc_flag
= p_dir
->flags
;
615 pos
= (DENTRY_T
*) &(es
->__buf
);
617 while (num_entries
) {
618 // instead of copying whole sector, we will check every entry.
619 // this will provide minimum stablity and consistency.
620 entry_type
= exfat_get_entry_type(ep
);
622 if ((entry_type
== TYPE_UNUSED
) || (entry_type
== TYPE_DELETED
))
626 case ES_MODE_STARTED
:
627 if ((entry_type
== TYPE_FILE
) || (entry_type
== TYPE_DIR
))
628 mode
= ES_MODE_GET_FILE_ENTRY
;
632 case ES_MODE_GET_FILE_ENTRY
:
633 if (entry_type
== TYPE_STREAM
)
634 mode
= ES_MODE_GET_STRM_ENTRY
;
638 case ES_MODE_GET_STRM_ENTRY
:
639 if (entry_type
== TYPE_EXTEND
)
640 mode
= ES_MODE_GET_NAME_ENTRY
;
644 case ES_MODE_GET_NAME_ENTRY
:
645 if (entry_type
== TYPE_EXTEND
)
647 else if (entry_type
== TYPE_STREAM
)
649 else if (entry_type
& TYPE_CRITICAL_SEC
)
650 mode
= ES_MODE_GET_CRITICAL_SEC_ENTRY
;
654 case ES_MODE_GET_CRITICAL_SEC_ENTRY
:
655 if ((entry_type
== TYPE_EXTEND
) || (entry_type
== TYPE_STREAM
))
657 else if ((entry_type
& TYPE_CRITICAL_SEC
) != TYPE_CRITICAL_SEC
)
663 memcpy(pos
, ep
, sizeof(DENTRY_T
));
665 if (--num_entries
== 0)
668 if (((off
+ DENTRY_SIZE
) & (u32
)(sb
->s_blocksize
- 1)) <
669 (off
& (u32
)(sb
->s_blocksize
- 1))) {
670 // get the next sector
671 if (IS_LAST_SECT_IN_CLUS(fsi
, sec
)) {
672 if (es
->alloc_flag
== 0x03)
674 else if (get_next_clus_safe(sb
, &clu
))
676 sec
= CLUS_TO_SECT(fsi
, clu
);
680 buf
= dcache_getblk(sb
, sec
);
684 ep
= (DENTRY_T
*)(buf
);
693 *file_ep
= (DENTRY_T
*)&(es
->__buf
);
695 MMSG("es sec %llu offset %u flags %d, num_entries %u buf ptr %p\n",
696 es
->sector
, es
->offset
, es
->alloc_flag
, es
->num_entries
, &(es
->__buf
));
697 TMSG("%s exited %p\n", __func__
, es
);
700 TMSG("%s exited (return NULL) (es %p)\n", __func__
, es
);
702 /* kfree(NULL) is safe */
708 void release_dentry_set(ENTRY_SET_CACHE_T
*es
)
710 TMSG("%s %p\n", __func__
, es
);
712 /* kfree(NULL) is safe */
717 static s32
__extract_uni_name_from_name_entry(NAME_DENTRY_T
*ep
, u16
*uniname
, s32 order
)
721 for (i
= 0; i
< 15; i
++) {
722 /* FIXME : unaligned? */
723 *uniname
= le16_to_cpu(ep
->unicode_0_14
[i
]);
733 } /* end of __extract_uni_name_from_name_entry */
735 #define DIRENT_STEP_FILE (0)
736 #define DIRENT_STEP_STRM (1)
737 #define DIRENT_STEP_NAME (2)
738 #define DIRENT_STEP_SECD (3)
740 /* return values of exfat_find_dir_entry()
741 * >= 0 : return dir entiry position with the name in dir
742 * -EEXIST : (root dir, ".") it is the root dir itself
743 * -ENOENT : entry with the name does not exist
746 static s32
exfat_find_dir_entry(struct super_block
*sb
, FILE_ID_T
*fid
,
747 CHAIN_T
*p_dir
, UNI_NAME_T
*p_uniname
, s32 num_entries
, DOS_NAME_T
*unused
, u32 type
)
749 s32 i
, rewind
= 0, dentry
= 0, end_eidx
= 0, num_ext
= 0, len
;
750 s32 order
, step
, name_len
;
751 s32 dentries_per_clu
, num_empty
= 0;
753 u16 entry_uniname
[16], *uniname
= NULL
, unichar
;
756 HINT_T
*hint_stat
= &fid
->hint_stat
;
757 HINT_FEMP_T candi_empty
;
758 FILE_DENTRY_T
*file_ep
;
759 STRM_DENTRY_T
*strm_ep
;
760 NAME_DENTRY_T
*name_ep
;
761 FS_INFO_T
*fsi
= &(SDFAT_SB(sb
)->fsi
);
765 * DOT and DOTDOT are handled by VFS layer
768 if (IS_CLUS_FREE(p_dir
->dir
))
771 dentries_per_clu
= fsi
->dentries_per_clu
;
773 clu
.dir
= p_dir
->dir
;
774 clu
.size
= p_dir
->size
;
775 clu
.flags
= p_dir
->flags
;
777 if (hint_stat
->eidx
) {
778 clu
.dir
= hint_stat
->clu
;
779 dentry
= hint_stat
->eidx
;
783 candi_empty
.eidx
= -1;
786 step
= DIRENT_STEP_FILE
;
787 while (!IS_CLUS_EOF(clu
.dir
)) {
788 i
= dentry
& (dentries_per_clu
- 1);
789 for (; i
< dentries_per_clu
; i
++, dentry
++) {
790 if (rewind
&& (dentry
== end_eidx
))
793 ep
= get_dentry_in_dir(sb
, &clu
, i
, NULL
);
797 entry_type
= exfat_get_entry_type(ep
);
799 if ((entry_type
== TYPE_UNUSED
) || (entry_type
== TYPE_DELETED
)) {
800 step
= DIRENT_STEP_FILE
;
803 if (candi_empty
.eidx
== -1) {
804 if (num_empty
== 1) {
805 candi_empty
.cur
.dir
= clu
.dir
;
806 candi_empty
.cur
.size
= clu
.size
;
807 candi_empty
.cur
.flags
= clu
.flags
;
810 if (num_empty
>= num_entries
) {
811 candi_empty
.eidx
= dentry
- (num_empty
- 1);
812 ASSERT(0 <= candi_empty
.eidx
);
813 candi_empty
.count
= num_empty
;
815 if ((fid
->hint_femp
.eidx
== -1) ||
816 (candi_empty
.eidx
<= fid
->hint_femp
.eidx
)) {
817 memcpy(&fid
->hint_femp
,
819 sizeof(HINT_FEMP_T
));
824 if (entry_type
== TYPE_UNUSED
)
830 candi_empty
.eidx
= -1;
832 if ((entry_type
== TYPE_FILE
) || (entry_type
== TYPE_DIR
)) {
833 step
= DIRENT_STEP_FILE
;
834 if ((type
== TYPE_ALL
) || (type
== entry_type
)) {
835 file_ep
= (FILE_DENTRY_T
*) ep
;
836 num_ext
= file_ep
->num_ext
;
837 step
= DIRENT_STEP_STRM
;
842 if (entry_type
== TYPE_STREAM
) {
843 if (step
!= DIRENT_STEP_STRM
) {
844 step
= DIRENT_STEP_FILE
;
847 step
= DIRENT_STEP_FILE
;
848 strm_ep
= (STRM_DENTRY_T
*) ep
;
849 if ((p_uniname
->name_hash
== le16_to_cpu(strm_ep
->name_hash
)) &&
850 (p_uniname
->name_len
== strm_ep
->name_len
)) {
851 step
= DIRENT_STEP_NAME
;
858 if (entry_type
== TYPE_EXTEND
) {
859 if (step
!= DIRENT_STEP_NAME
) {
860 step
= DIRENT_STEP_FILE
;
863 name_ep
= (NAME_DENTRY_T
*) ep
;
866 uniname
= p_uniname
->name
;
870 len
= __extract_uni_name_from_name_entry(name_ep
, entry_uniname
, order
);
873 unichar
= *(uniname
+len
);
874 *(uniname
+len
) = 0x0;
876 if (nls_cmp_uniname(sb
, uniname
, entry_uniname
)) {
877 step
= DIRENT_STEP_FILE
;
878 } else if (name_len
== p_uniname
->name_len
) {
879 if (order
== num_ext
) {
880 //fid->hint_femp.eidx = -1;
883 step
= DIRENT_STEP_SECD
;
886 *(uniname
+len
) = unichar
;
890 if (entry_type
& (TYPE_CRITICAL_SEC
| TYPE_BENIGN_SEC
)) {
891 if (step
== DIRENT_STEP_SECD
) {
892 if (++order
== num_ext
)
897 step
= DIRENT_STEP_FILE
;
900 if (clu
.flags
== 0x03) {
901 if ((--clu
.size
) > 0)
906 if (get_next_clus_safe(sb
, &clu
.dir
))
912 /* we started at not 0 index,so we should try to find target
913 * from 0 index to the index we started at.
915 if (!rewind
&& end_eidx
) {
918 clu
.dir
= p_dir
->dir
;
919 /* reset empty hint */
921 candi_empty
.eidx
= -1;
925 /* initialized hint_stat */
926 hint_stat
->clu
= p_dir
->dir
;
931 /* next dentry we'll find is out of this cluster */
932 if (!((dentry
+ 1) & (dentries_per_clu
-1))) {
935 if (clu
.flags
== 0x03) {
936 if ((--clu
.size
) > 0)
941 ret
= get_next_clus_safe(sb
, &clu
.dir
);
944 if (ret
|| IS_CLUS_EOF(clu
.dir
)) {
945 /* just initialized hint_stat */
946 hint_stat
->clu
= p_dir
->dir
;
948 return (dentry
- num_ext
);
952 hint_stat
->clu
= clu
.dir
;
953 hint_stat
->eidx
= dentry
+ 1;
954 return (dentry
- num_ext
);
955 } /* end of exfat_find_dir_entry */
957 /* returns -EIO on error */
958 static s32
exfat_count_ext_entries(struct super_block
*sb
, CHAIN_T
*p_dir
, s32 entry
, DENTRY_T
*p_entry
)
962 FILE_DENTRY_T
*file_ep
= (FILE_DENTRY_T
*) p_entry
;
965 for (i
= 0, entry
++; i
< file_ep
->num_ext
; i
++, entry
++) {
966 ext_ep
= get_dentry_in_dir(sb
, p_dir
, entry
, NULL
);
970 type
= exfat_get_entry_type(ext_ep
);
971 if ((type
== TYPE_EXTEND
) || (type
== TYPE_STREAM
))
978 } /* end of exfat_count_ext_entries */
982 * Name Conversion Functions
984 static void exfat_get_uniname_from_ext_entry(struct super_block
*sb
, CHAIN_T
*p_dir
, s32 entry
, u16
*uniname
)
988 ENTRY_SET_CACHE_T
*es
;
990 es
= get_dentry_set_in_dir(sb
, p_dir
, entry
, ES_ALL_ENTRIES
, &ep
);
994 if (es
->num_entries
< 3)
1000 * First entry : file entry
1001 * Second entry : stream-extension entry
1002 * Third entry : first file-name entry
1003 * So, the index of first file-name dentry should start from 2.
1005 for (i
= 2; i
< es
->num_entries
; i
++, ep
++) {
1006 /* end of name entry */
1007 if (exfat_get_entry_type(ep
) != TYPE_EXTEND
)
1010 __extract_uni_name_from_name_entry((NAME_DENTRY_T
*)ep
, uniname
, i
);
1015 release_dentry_set(es
);
1016 } /* end of exfat_get_uniname_from_ext_entry */
1018 static s32
exfat_calc_num_entries(UNI_NAME_T
*p_uniname
)
1022 len
= p_uniname
->name_len
;
1026 /* 1 file entry + 1 stream entry + name entries */
1027 return((len
-1) / 15 + 3);
1029 } /* end of exfat_calc_num_entries */
1031 static s32
exfat_check_max_dentries(FILE_ID_T
*fid
)
1033 if ((fid
->size
>> DENTRY_SIZE_BITS
) >= MAX_EXFAT_DENTRIES
) {
1034 /* exFAT spec allows a dir to grow upto 8388608(256MB) dentries */
1038 } /* end of check_max_dentries */
1041 * Allocation Bitmap Management Functions
1043 s32
load_alloc_bmp(struct super_block
*sb
)
1046 u32 i
, j
, map_size
, type
, need_map_size
;
1050 FS_INFO_T
*fsi
= &(SDFAT_SB(sb
)->fsi
);
1052 clu
.dir
= fsi
->root_dir
;
1055 while (!IS_CLUS_EOF(clu
.dir
)) {
1056 for (i
= 0; i
< fsi
->dentries_per_clu
; i
++) {
1057 ep
= (BMAP_DENTRY_T
*) get_dentry_in_dir(sb
, &clu
, i
, NULL
);
1061 type
= exfat_get_entry_type((DENTRY_T
*) ep
);
1063 if (type
== TYPE_UNUSED
)
1065 if (type
!= TYPE_BITMAP
)
1068 if (ep
->flags
== 0x0) {
1069 fsi
->map_clu
= le32_to_cpu(ep
->start_clu
);
1070 map_size
= (u32
) le64_to_cpu(ep
->size
);
1072 need_map_size
= (((fsi
->num_clusters
- CLUS_BASE
) - 1) >> 3) + 1;
1073 if (need_map_size
!= map_size
) {
1074 sdfat_log_msg(sb
, KERN_ERR
,
1075 "bogus allocation bitmap size(need : %u, cur : %u)",
1076 need_map_size
, map_size
);
1077 /* Only allowed when bogus allocation bitmap size is large */
1078 if (need_map_size
> map_size
)
1081 fsi
->map_sectors
= ((need_map_size
- 1) >> (sb
->s_blocksize_bits
)) + 1;
1083 kmalloc((sizeof(struct buffer_head
*) * fsi
->map_sectors
), GFP_KERNEL
);
1087 sector
= CLUS_TO_SECT(fsi
, fsi
->map_clu
);
1089 for (j
= 0; j
< fsi
->map_sectors
; j
++) {
1090 fsi
->vol_amap
[j
] = NULL
;
1091 ret
= read_sect(sb
, sector
+j
, &(fsi
->vol_amap
[j
]), 1);
1093 /* release all buffers and free vol_amap */
1096 brelse(fsi
->vol_amap
[i
++]);
1098 /* kfree(NULL) is safe */
1099 kfree(fsi
->vol_amap
);
1100 fsi
->vol_amap
= NULL
;
1110 if (get_next_clus_safe(sb
, &clu
.dir
))
1115 } /* end of load_alloc_bmp */
1117 void free_alloc_bmp(struct super_block
*sb
)
1120 FS_INFO_T
*fsi
= &(SDFAT_SB(sb
)->fsi
);
1122 brelse(fsi
->pbr_bh
);
1124 for (i
= 0; i
< fsi
->map_sectors
; i
++)
1125 __brelse(fsi
->vol_amap
[i
]);
1127 /* kfree(NULL) is safe */
1128 kfree(fsi
->vol_amap
);
1129 fsi
->vol_amap
= NULL
;
1133 * If the value of "clu" is 0, it means cluster 2 which is
1134 * the first cluster of cluster heap.
1136 static s32
set_alloc_bitmap(struct super_block
*sb
, u32 clu
)
1140 FS_INFO_T
*fsi
= &(SDFAT_SB(sb
)->fsi
);
1142 i
= clu
>> (sb
->s_blocksize_bits
+ 3);
1143 b
= clu
& (u32
)((sb
->s_blocksize
<< 3) - 1);
1145 sector
= CLUS_TO_SECT(fsi
, fsi
->map_clu
) + i
;
1146 bitmap_set((unsigned long *)(fsi
->vol_amap
[i
]->b_data
), b
, 1);
1148 return write_sect(sb
, sector
, fsi
->vol_amap
[i
], 0);
1149 } /* end of set_alloc_bitmap */
1152 * If the value of "clu" is 0, it means cluster 2 which is
1153 * the first cluster of cluster heap.
1155 static s32
clr_alloc_bitmap(struct super_block
*sb
, u32 clu
)
1160 struct sdfat_sb_info
*sbi
= SDFAT_SB(sb
);
1161 struct sdfat_mount_options
*opts
= &sbi
->options
;
1162 FS_INFO_T
*fsi
= &(SDFAT_SB(sb
)->fsi
);
1164 i
= clu
>> (sb
->s_blocksize_bits
+ 3);
1165 b
= clu
& (u32
)((sb
->s_blocksize
<< 3) - 1);
1167 sector
= CLUS_TO_SECT(fsi
, fsi
->map_clu
) + i
;
1169 bitmap_clear((unsigned long *)(fsi
->vol_amap
[i
]->b_data
), b
, 1);
1171 ret
= write_sect(sb
, sector
, fsi
->vol_amap
[i
], 0);
1173 if (opts
->discard
) {
1176 TMSG("discard cluster(%08x)\n", clu
+2);
1177 ret_discard
= sb_issue_discard(sb
, CLUS_TO_SECT(fsi
, clu
+2),
1178 (1 << fsi
->sect_per_clus_bits
), GFP_NOFS
, 0);
1180 if (ret_discard
== -EOPNOTSUPP
) {
1181 sdfat_msg(sb
, KERN_ERR
,
1182 "discard not supported by device, disabling");
1188 } /* end of clr_alloc_bitmap */
1191 * If the value of "clu" is 0, it means cluster 2 which is
1192 * the first cluster of cluster heap.
1194 static u32
test_alloc_bitmap(struct super_block
*sb
, u32 clu
)
1196 u32 i
, map_i
, map_b
;
1197 u32 clu_base
, clu_free
;
1199 FS_INFO_T
*fsi
= &(SDFAT_SB(sb
)->fsi
);
1201 clu_base
= (clu
& ~(0x7)) + 2;
1202 clu_mask
= (1 << (clu
- clu_base
+ 2)) - 1;
1204 map_i
= clu
>> (sb
->s_blocksize_bits
+ 3);
1205 map_b
= (clu
>> 3) & (u32
)(sb
->s_blocksize
- 1);
1207 for (i
= 2; i
< fsi
->num_clusters
; i
+= 8) {
1208 k
= *(((u8
*) fsi
->vol_amap
[map_i
]->b_data
) + map_b
);
1214 clu_free
= clu_base
+ free_bit
[k
];
1215 if (clu_free
< fsi
->num_clusters
)
1220 if (((++map_b
) >= (u32
)sb
->s_blocksize
) ||
1221 (clu_base
>= fsi
->num_clusters
)) {
1222 if ((++map_i
) >= fsi
->map_sectors
) {
1231 } /* end of test_alloc_bitmap */
1233 void sync_alloc_bmp(struct super_block
*sb
)
1236 FS_INFO_T
*fsi
= &(SDFAT_SB(sb
)->fsi
);
1238 if (fsi
->vol_amap
== NULL
)
1241 for (i
= 0; i
< fsi
->map_sectors
; i
++)
1242 sync_dirty_buffer(fsi
->vol_amap
[i
]);
1245 static s32
exfat_chain_cont_cluster(struct super_block
*sb
, u32 chain
, u32 len
)
1251 if (fat_ent_set(sb
, chain
, chain
+1))
1257 if (fat_ent_set(sb
, chain
, CLUS_EOF
))
1262 s32
chain_cont_cluster(struct super_block
*sb
, u32 chain
, u32 len
)
1264 return exfat_chain_cont_cluster(sb
, chain
, len
);
1268 static s32
exfat_free_cluster(struct super_block
*sb
, CHAIN_T
*p_chain
, s32 do_relse
)
1271 u32 num_clusters
= 0;
1273 FS_INFO_T
*fsi
= &(SDFAT_SB(sb
)->fsi
);
1277 /* invalid cluster number */
1278 if (IS_CLUS_FREE(p_chain
->dir
) || IS_CLUS_EOF(p_chain
->dir
))
1281 /* no cluster to truncate */
1282 if (p_chain
->size
== 0) {
1283 DMSG("%s: cluster(%u) truncation is not required.",
1284 __func__
, p_chain
->dir
);
1288 /* check cluster validation */
1289 if (!is_valid_clus(fsi
, p_chain
->dir
)) {
1290 EMSG("%s: invalid start cluster (%u)\n", __func__
, p_chain
->dir
);
1291 sdfat_debug_bug_on(1);
1298 if (p_chain
->flags
== 0x03) {
1301 sector
= CLUS_TO_SECT(fsi
, clu
);
1302 for (i
= 0; i
< fsi
->sect_per_clus
; i
++) {
1303 if (dcache_release(sb
, sector
+i
) == -EIO
)
1308 if (clr_alloc_bitmap(sb
, clu
-2))
1313 } while (num_clusters
< p_chain
->size
);
1317 sector
= CLUS_TO_SECT(fsi
, clu
);
1318 for (i
= 0; i
< fsi
->sect_per_clus
; i
++) {
1319 if (dcache_release(sb
, sector
+i
) == -EIO
)
1324 if (clr_alloc_bitmap(sb
, (clu
- CLUS_BASE
)))
1327 if (get_next_clus_safe(sb
, &clu
))
1331 } while (!IS_CLUS_EOF(clu
));
1338 fsi
->used_clusters
-= num_clusters
;
1340 } /* end of exfat_free_cluster */
1342 static s32
exfat_alloc_cluster(struct super_block
*sb
, u32 num_alloc
, CHAIN_T
*p_chain
, s32 dest
)
1345 u32 num_clusters
= 0, total_cnt
;
1346 u32 hint_clu
, new_clu
, last_clu
= CLUS_EOF
;
1347 FS_INFO_T
*fsi
= &(SDFAT_SB(sb
)->fsi
);
1349 total_cnt
= fsi
->num_clusters
- CLUS_BASE
;
1351 if (unlikely(total_cnt
< fsi
->used_clusters
)) {
1352 sdfat_fs_error_ratelimit(sb
,
1353 "%s: invalid used clusters(t:%u,u:%u)\n",
1354 __func__
, total_cnt
, fsi
->used_clusters
);
1358 if (num_alloc
> total_cnt
- fsi
->used_clusters
)
1361 hint_clu
= p_chain
->dir
;
1362 /* find new cluster */
1363 if (IS_CLUS_EOF(hint_clu
)) {
1364 if (fsi
->clu_srch_ptr
< CLUS_BASE
) {
1365 EMSG("%s: fsi->clu_srch_ptr is invalid (%u)\n",
1366 __func__
, fsi
->clu_srch_ptr
);
1368 fsi
->clu_srch_ptr
= CLUS_BASE
;
1371 hint_clu
= test_alloc_bitmap(sb
, fsi
->clu_srch_ptr
- CLUS_BASE
);
1372 if (IS_CLUS_EOF(hint_clu
))
1376 /* check cluster validation */
1377 if (!is_valid_clus(fsi
, hint_clu
)) {
1378 /* "last + 1" can be passed as hint_clu. Otherwise, bug_on */
1379 if (hint_clu
!= fsi
->num_clusters
) {
1380 EMSG("%s: hint_cluster is invalid (%u)\n",
1381 __func__
, hint_clu
);
1382 sdfat_debug_bug_on(1);
1384 hint_clu
= CLUS_BASE
;
1385 if (p_chain
->flags
== 0x03) {
1386 if (exfat_chain_cont_cluster(sb
, p_chain
->dir
, num_clusters
))
1388 p_chain
->flags
= 0x01;
1394 p_chain
->dir
= CLUS_EOF
;
1396 while ((new_clu
= test_alloc_bitmap(sb
, hint_clu
- CLUS_BASE
)) != CLUS_EOF
) {
1397 if ((new_clu
!= hint_clu
) && (p_chain
->flags
== 0x03)) {
1398 if (exfat_chain_cont_cluster(sb
, p_chain
->dir
, num_clusters
)) {
1402 p_chain
->flags
= 0x01;
1405 /* update allocation bitmap */
1406 if (set_alloc_bitmap(sb
, new_clu
- CLUS_BASE
)) {
1413 /* update FAT table */
1414 if (p_chain
->flags
== 0x01) {
1415 if (fat_ent_set(sb
, new_clu
, CLUS_EOF
)) {
1421 if (IS_CLUS_EOF(p_chain
->dir
)) {
1422 p_chain
->dir
= new_clu
;
1423 } else if (p_chain
->flags
== 0x01) {
1424 if (fat_ent_set(sb
, last_clu
, new_clu
)) {
1431 if ((--num_alloc
) == 0) {
1432 fsi
->clu_srch_ptr
= hint_clu
;
1433 fsi
->used_clusters
+= num_clusters
;
1435 p_chain
->size
+= num_clusters
;
1439 hint_clu
= new_clu
+ 1;
1440 if (hint_clu
>= fsi
->num_clusters
) {
1441 hint_clu
= CLUS_BASE
;
1443 if (p_chain
->flags
== 0x03) {
1444 if (exfat_chain_cont_cluster(sb
, p_chain
->dir
, num_clusters
)) {
1448 p_chain
->flags
= 0x01;
1454 exfat_free_cluster(sb
, p_chain
, 0);
1456 } /* end of exfat_alloc_cluster */
1458 static s32
exfat_count_used_clusters(struct super_block
*sb
, u32
*ret_count
)
1461 u32 i
, map_i
, map_b
;
1462 FS_INFO_T
*fsi
= &(SDFAT_SB(sb
)->fsi
);
1463 u32 total_clus
= fsi
->num_clusters
- 2;
1467 for (i
= 0; i
< total_clus
; i
+= 8) {
1468 u8 k
= *(((u8
*) fsi
->vol_amap
[map_i
]->b_data
) + map_b
);
1470 count
+= used_bit
[k
];
1471 if ((++map_b
) >= (u32
)sb
->s_blocksize
) {
1477 /* FIXME : abnormal bitmap count should be handled as more smart */
1478 if (total_clus
< count
)
1483 } /* end of exfat_count_used_clusters */
1487 * File Operation Functions
1489 static FS_FUNC_T exfat_fs_func
= {
1490 .alloc_cluster
= exfat_alloc_cluster
,
1491 .free_cluster
= exfat_free_cluster
,
1492 .count_used_clusters
= exfat_count_used_clusters
,
1494 .init_dir_entry
= exfat_init_dir_entry
,
1495 .init_ext_entry
= exfat_init_ext_entry
,
1496 .find_dir_entry
= exfat_find_dir_entry
,
1497 .delete_dir_entry
= exfat_delete_dir_entry
,
1498 .get_uniname_from_ext_entry
= exfat_get_uniname_from_ext_entry
,
1499 .count_ext_entries
= exfat_count_ext_entries
,
1500 .calc_num_entries
= exfat_calc_num_entries
,
1501 .check_max_dentries
= exfat_check_max_dentries
,
1503 .get_entry_type
= exfat_get_entry_type
,
1504 .set_entry_type
= exfat_set_entry_type
,
1505 .get_entry_attr
= exfat_get_entry_attr
,
1506 .set_entry_attr
= exfat_set_entry_attr
,
1507 .get_entry_flag
= exfat_get_entry_flag
,
1508 .set_entry_flag
= exfat_set_entry_flag
,
1509 .get_entry_clu0
= exfat_get_entry_clu0
,
1510 .set_entry_clu0
= exfat_set_entry_clu0
,
1511 .get_entry_size
= exfat_get_entry_size
,
1512 .set_entry_size
= exfat_set_entry_size
,
1513 .get_entry_time
= exfat_get_entry_time
,
1514 .set_entry_time
= exfat_set_entry_time
,
1517 s32
mount_exfat(struct super_block
*sb
, pbr_t
*p_pbr
)
1519 pbr64_t
*p_bpb
= (pbr64_t
*)p_pbr
;
1520 FS_INFO_T
*fsi
= &(SDFAT_SB(sb
)->fsi
);
1522 fsi
->sect_per_clus
= 1 << p_bpb
->bsx
.sect_per_clus_bits
;
1523 fsi
->sect_per_clus_bits
= p_bpb
->bsx
.sect_per_clus_bits
;
1524 fsi
->cluster_size_bits
= fsi
->sect_per_clus_bits
+ sb
->s_blocksize_bits
;
1525 fsi
->cluster_size
= 1 << fsi
->cluster_size_bits
;
1527 if (!p_bpb
->bsx
.num_fats
) {
1528 sdfat_msg(sb
, KERN_ERR
, "bogus number of FAT structure");
1532 if (p_bpb
->bsx
.num_fats
>= 2) {
1533 sdfat_msg(sb
, KERN_WARNING
,
1534 "unsupported number of FAT structure :%u, try with 1",
1535 p_bpb
->bsx
.num_fats
);
1538 fsi
->num_FAT_sectors
= le32_to_cpu(p_bpb
->bsx
.fat_length
);
1539 if (!fsi
->num_FAT_sectors
) {
1540 sdfat_msg(sb
, KERN_ERR
, "bogus fat size");
1544 fsi
->FAT1_start_sector
= le32_to_cpu(p_bpb
->bsx
.fat_offset
);
1545 fsi
->FAT2_start_sector
= fsi
->FAT1_start_sector
;
1547 fsi
->root_start_sector
= le32_to_cpu(p_bpb
->bsx
.clu_offset
);
1548 fsi
->data_start_sector
= fsi
->root_start_sector
;
1550 fsi
->num_sectors
= le64_to_cpu(p_bpb
->bsx
.vol_length
);
1551 if (!fsi
->num_sectors
) {
1552 sdfat_msg(sb
, KERN_ERR
, "bogus number of total sector count");
1556 /* because the cluster index starts with 2 */
1557 fsi
->num_clusters
= le32_to_cpu(p_bpb
->bsx
.clu_count
) + CLUS_BASE
;
1559 fsi
->vol_id
= le32_to_cpu(p_bpb
->bsx
.vol_serial
);
1560 fsi
->root_dir
= le32_to_cpu(p_bpb
->bsx
.root_cluster
);
1561 fsi
->dentries_in_root
= 0;
1562 fsi
->dentries_per_clu
= 1 << (fsi
->cluster_size_bits
- DENTRY_SIZE_BITS
);
1563 fsi
->vol_flag
= (u32
) le16_to_cpu(p_bpb
->bsx
.vol_flags
);
1564 fsi
->clu_srch_ptr
= CLUS_BASE
;
1565 fsi
->used_clusters
= (u32
) ~0;
1567 fsi
->fs_func
= &exfat_fs_func
;
1568 fsi
->vol_type
= EXFAT
;
1569 fat_ent_ops_init(sb
);
1571 if (p_bpb
->bsx
.vol_flags
& VOL_DIRTY
) {
1572 fsi
->vol_flag
|= VOL_DIRTY
;
1573 sdfat_log_msg(sb
, KERN_WARNING
, "Volume was not properly "
1574 "unmounted. Some data may be corrupt. "
1575 "Please run fsck.");
1579 } /* end of mount_exfat */
1581 /* end of core_exfat.c */