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_fat.c */
22 /* PURPOSE : FAT-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 /*----------------------------------------------------------------------*/
44 #define MAX_LFN_ORDER (20)
47 * MAX_EST_AU_SECT should be changed according to 32/64bits.
48 * On 32bit, 4KB page supports 512 clusters per AU.
49 * But, on 64bit, 4KB page can handle a half of total list_head of 32bit's.
50 * Bcause the size of list_head structure on 64bit increases twofold over 32bit.
52 #if (BITS_PER_LONG == 64)
53 //#define MAX_EST_AU_SECT (16384) /* upto 8MB */
54 #define MAX_EST_AU_SECT (32768) /* upto 16MB, used more page for list_head */
56 #define MAX_EST_AU_SECT (32768) /* upto 16MB */
59 /*======================================================================*/
60 /* Local Function Declarations */
61 /*======================================================================*/
62 static s32
__extract_uni_name_from_ext_entry(EXT_DENTRY_T
*, u16
*, s32
);
64 /*----------------------------------------------------------------------*/
65 /* Global Variable Definitions */
66 /*----------------------------------------------------------------------*/
68 /*----------------------------------------------------------------------*/
69 /* Local Variable Definitions */
70 /*----------------------------------------------------------------------*/
72 /*======================================================================*/
73 /* Local Function Definitions */
74 /*======================================================================*/
75 static u32
__calc_default_au_size(struct super_block
*sb
)
77 struct block_device
*bdev
= sb
->s_bdev
;
79 struct request_queue
*queue
;
80 struct queue_limits
*limit
;
81 unsigned int est_au_sect
= MAX_EST_AU_SECT
;
82 unsigned int est_au_size
= 0;
83 unsigned int queue_au_size
= 0;
84 sector_t total_sect
= 0;
86 /* we assumed that sector size is 512 bytes */
96 limit
= &queue
->limits
;
97 queue_au_size
= limit
->discard_granularity
;
99 /* estimate function(x) =
100 * (total_sect / 2) * 512 / 1024
101 * => (total_sect >> 1) >> 1)
102 * => (total_sect >> 2)
103 * => estimated bytes size
107 * ex3) >= 32GB -> 16MB
109 total_sect
= disk
->part0
.nr_sects
;
110 est_au_size
= total_sect
>> 2;
112 /* au_size assumed that bytes per sector is 512 */
113 est_au_sect
= est_au_size
>> 9;
115 MMSG("DBG1: total_sect(%llu) est_au_size(%u) est_au_sect(%u)\n",
116 (u64
)total_sect
, est_au_size
, est_au_sect
);
118 if (est_au_sect
<= 8192) {
121 } else if (est_au_sect
<= 16384) {
126 est_au_sect
= MAX_EST_AU_SECT
;
129 MMSG("DBG2: total_sect(%llu) est_au_size(%u) est_au_sect(%u)\n",
130 (u64
)total_sect
, est_au_size
, est_au_sect
);
132 if (est_au_size
< queue_au_size
&&
133 queue_au_size
<= (MAX_EST_AU_SECT
<< 9)) {
134 DMSG("use queue_au_size(%u) instead of est_au_size(%u)\n",
135 queue_au_size
, est_au_size
);
136 est_au_sect
= queue_au_size
>> 9;
140 if (sb
->s_blocksize
!= 512) {
141 ASSERT(sb
->s_blocksize_bits
> 9);
142 sdfat_log_msg(sb
, KERN_INFO
,
143 "adjustment est_au_size by logical block size(%lu)",
145 est_au_sect
>>= (sb
->s_blocksize_bits
- 9);
148 sdfat_log_msg(sb
, KERN_INFO
, "set default AU sectors : %u "
149 "(queue_au_size : %u KB, disk_size : %llu MB)",
150 est_au_sect
, queue_au_size
>> 10, (u64
)(total_sect
>> 11));
156 * Cluster Management Functions
158 static s32
fat_free_cluster(struct super_block
*sb
, CHAIN_T
*p_chain
, s32 do_relse
)
161 s32 num_clusters
= 0;
163 FS_INFO_T
*fsi
= &(SDFAT_SB(sb
)->fsi
);
167 /* invalid cluster number */
168 if (IS_CLUS_FREE(p_chain
->dir
) || IS_CLUS_EOF(p_chain
->dir
))
171 /* no cluster to truncate */
172 if (!p_chain
->size
) {
173 DMSG("%s: cluster(%u) truncation is not required.",
174 __func__
, p_chain
->dir
);
178 /* check cluster validation */
179 if ((p_chain
->dir
< 2) && (p_chain
->dir
>= fsi
->num_clusters
)) {
180 EMSG("%s: invalid start cluster (%u)\n", __func__
, p_chain
->dir
);
181 sdfat_debug_bug_on(1);
191 sector
= CLUS_TO_SECT(fsi
, clu
);
192 for (i
= 0; i
< fsi
->sect_per_clus
; i
++) {
193 if (dcache_release(sb
, sector
+i
) == -EIO
)
199 if (get_next_clus_safe(sb
, &clu
)) {
200 /* print more helpful log */
201 if (IS_CLUS_BAD(clu
)) {
202 sdfat_log_msg(sb
, KERN_ERR
, "%s : "
203 "deleting bad cluster (clu[%u]->BAD)",
205 } else if (IS_CLUS_FREE(clu
)) {
206 sdfat_log_msg(sb
, KERN_ERR
, "%s : "
207 "deleting free cluster (clu[%u]->FREE)",
214 if (fat_ent_set(sb
, prev
, CLUS_FREE
))
217 /* Update AMAP if needed */
219 if (amap_release_cluster(sb
, prev
))
225 } while (!IS_CLUS_EOF(clu
));
230 fsi
->used_clusters
-= num_clusters
;
232 } /* end of fat_free_cluster */
234 static s32
fat_alloc_cluster(struct super_block
*sb
, u32 num_alloc
, CHAIN_T
*p_chain
, s32 dest
)
237 u32 i
, num_clusters
= 0, total_cnt
;
238 u32 new_clu
, last_clu
= CLUS_EOF
, read_clu
;
239 FS_INFO_T
*fsi
= &(SDFAT_SB(sb
)->fsi
);
241 total_cnt
= fsi
->num_clusters
- CLUS_BASE
;
243 if (unlikely(total_cnt
< fsi
->used_clusters
)) {
244 sdfat_fs_error_ratelimit(sb
,
245 "%s : invalid used clusters(t:%u,u:%u)\n",
246 __func__
, total_cnt
, fsi
->used_clusters
);
250 if (num_alloc
> total_cnt
- fsi
->used_clusters
)
253 new_clu
= p_chain
->dir
;
254 if (IS_CLUS_EOF(new_clu
))
255 new_clu
= fsi
->clu_srch_ptr
;
256 else if (new_clu
>= fsi
->num_clusters
)
261 p_chain
->dir
= CLUS_EOF
;
263 for (i
= CLUS_BASE
; i
< fsi
->num_clusters
; i
++) {
264 if (fat_ent_get(sb
, new_clu
, &read_clu
)) {
269 if (IS_CLUS_FREE(read_clu
)) {
270 if (fat_ent_set(sb
, new_clu
, CLUS_EOF
)) {
276 if (IS_CLUS_EOF(p_chain
->dir
)) {
277 p_chain
->dir
= new_clu
;
279 if (fat_ent_set(sb
, last_clu
, new_clu
)) {
287 if ((--num_alloc
) == 0) {
288 fsi
->clu_srch_ptr
= new_clu
;
289 fsi
->used_clusters
+= num_clusters
;
294 if ((++new_clu
) >= fsi
->num_clusters
)
299 fat_free_cluster(sb
, p_chain
, 0);
301 } /* end of fat_alloc_cluster */
303 static s32
fat_count_used_clusters(struct super_block
*sb
, u32
*ret_count
)
307 FS_INFO_T
*fsi
= &(SDFAT_SB(sb
)->fsi
);
309 for (i
= CLUS_BASE
; i
< fsi
->num_clusters
; i
++) {
310 if (fat_ent_get(sb
, i
, &clu
))
313 if (!IS_CLUS_FREE(clu
))
319 } /* end of fat_count_used_clusters */
323 * Directory Entry Management Functions
325 static u32
fat_get_entry_type(DENTRY_T
*p_entry
)
327 DOS_DENTRY_T
*ep
= (DOS_DENTRY_T
*)p_entry
;
329 /* first byte of 32bytes dummy */
330 if (*(ep
->name
) == MSDOS_UNUSED
)
333 /* 0xE5 of Kanji Japanese is replaced to 0x05 */
334 else if (*(ep
->name
) == MSDOS_DELETED
)
337 /* 11th byte of 32bytes dummy */
338 else if ((ep
->attr
& ATTR_EXTEND_MASK
) == ATTR_EXTEND
)
341 else if (!(ep
->attr
& (ATTR_SUBDIR
| ATTR_VOLUME
)))
344 else if ((ep
->attr
& (ATTR_SUBDIR
| ATTR_VOLUME
)) == ATTR_SUBDIR
)
347 else if ((ep
->attr
& (ATTR_SUBDIR
| ATTR_VOLUME
)) == ATTR_VOLUME
)
351 } /* end of fat_get_entry_type */
353 static void fat_set_entry_type(DENTRY_T
*p_entry
, u32 type
)
355 DOS_DENTRY_T
*ep
= (DOS_DENTRY_T
*)p_entry
;
357 if (type
== TYPE_UNUSED
)
358 *(ep
->name
) = MSDOS_UNUSED
; /* 0x0 */
360 else if (type
== TYPE_DELETED
)
361 *(ep
->name
) = MSDOS_DELETED
; /* 0xE5 */
363 else if (type
== TYPE_EXTEND
)
364 ep
->attr
= ATTR_EXTEND
;
366 else if (type
== TYPE_DIR
)
367 ep
->attr
= ATTR_SUBDIR
;
369 else if (type
== TYPE_FILE
)
370 ep
->attr
= ATTR_ARCHIVE
;
372 else if (type
== TYPE_SYMLINK
)
373 ep
->attr
= ATTR_ARCHIVE
| ATTR_SYMLINK
;
374 } /* end of fat_set_entry_type */
376 static u32
fat_get_entry_attr(DENTRY_T
*p_entry
)
378 DOS_DENTRY_T
*ep
= (DOS_DENTRY_T
*)p_entry
;
380 return (u32
)ep
->attr
;
381 } /* end of fat_get_entry_attr */
383 static void fat_set_entry_attr(DENTRY_T
*p_entry
, u32 attr
)
385 DOS_DENTRY_T
*ep
= (DOS_DENTRY_T
*)p_entry
;
388 } /* end of fat_set_entry_attr */
390 static u8
fat_get_entry_flag(DENTRY_T
*p_entry
)
393 } /* end of fat_get_entry_flag */
395 static void fat_set_entry_flag(DENTRY_T
*p_entry
, u8 flags
)
397 } /* end of fat_set_entry_flag */
399 static u32
fat_get_entry_clu0(DENTRY_T
*p_entry
)
401 DOS_DENTRY_T
*ep
= (DOS_DENTRY_T
*)p_entry
;
403 return(((u32
)(le16_to_cpu(ep
->start_clu_hi
)) << 16) | le16_to_cpu(ep
->start_clu_lo
));
404 } /* end of fat_get_entry_clu0 */
406 static void fat_set_entry_clu0(DENTRY_T
*p_entry
, u32 start_clu
)
408 DOS_DENTRY_T
*ep
= (DOS_DENTRY_T
*)p_entry
;
410 ep
->start_clu_lo
= cpu_to_le16(CLUSTER_16(start_clu
));
411 ep
->start_clu_hi
= cpu_to_le16(CLUSTER_16(start_clu
>> 16));
412 } /* end of fat_set_entry_clu0 */
414 static u64
fat_get_entry_size(DENTRY_T
*p_entry
)
416 DOS_DENTRY_T
*ep
= (DOS_DENTRY_T
*)p_entry
;
418 return (u64
)le32_to_cpu(ep
->size
);
419 } /* end of fat_get_entry_size */
421 static void fat_set_entry_size(DENTRY_T
*p_entry
, u64 size
)
423 DOS_DENTRY_T
*ep
= (DOS_DENTRY_T
*)p_entry
;
425 ep
->size
= cpu_to_le32((u32
)size
);
426 } /* end of fat_set_entry_size */
428 static void fat_get_entry_time(DENTRY_T
*p_entry
, TIMESTAMP_T
*tp
, u8 mode
)
430 u16 t
= 0x00, d
= 0x21;
431 DOS_DENTRY_T
*ep
= (DOS_DENTRY_T
*) p_entry
;
435 t
= le16_to_cpu(ep
->create_time
);
436 d
= le16_to_cpu(ep
->create_date
);
439 t
= le16_to_cpu(ep
->modify_time
);
440 d
= le16_to_cpu(ep
->modify_date
);
445 tp
->sec
= (t
& 0x001F) << 1;
446 tp
->min
= (t
>> 5) & 0x003F;
447 tp
->hour
= (t
>> 11);
448 tp
->day
= (d
& 0x001F);
449 tp
->mon
= (d
>> 5) & 0x000F;
451 } /* end of fat_get_entry_time */
453 static void fat_set_entry_time(DENTRY_T
*p_entry
, TIMESTAMP_T
*tp
, u8 mode
)
456 DOS_DENTRY_T
*ep
= (DOS_DENTRY_T
*) p_entry
;
458 t
= (tp
->hour
<< 11) | (tp
->min
<< 5) | (tp
->sec
>> 1);
459 d
= (tp
->year
<< 9) | (tp
->mon
<< 5) | tp
->day
;
463 ep
->create_time
= cpu_to_le16(t
);
464 ep
->create_date
= cpu_to_le16(d
);
467 ep
->modify_time
= cpu_to_le16(t
);
468 ep
->modify_date
= cpu_to_le16(d
);
471 } /* end of fat_set_entry_time */
473 static void __init_dos_entry(struct super_block
*sb
, DOS_DENTRY_T
*ep
, u32 type
, u32 start_clu
)
477 fat_set_entry_type((DENTRY_T
*) ep
, type
);
478 ep
->start_clu_lo
= cpu_to_le16(CLUSTER_16(start_clu
));
479 ep
->start_clu_hi
= cpu_to_le16(CLUSTER_16(start_clu
>> 16));
482 tp
= tm_now(SDFAT_SB(sb
), &tm
);
483 fat_set_entry_time((DENTRY_T
*) ep
, tp
, TM_CREATE
);
484 fat_set_entry_time((DENTRY_T
*) ep
, tp
, TM_MODIFY
);
486 ep
->create_time_ms
= 0;
487 } /* end of __init_dos_entry */
489 static void __init_ext_entry(EXT_DENTRY_T
*ep
, s32 order
, u8 chksum
, u16
*uniname
)
494 fat_set_entry_type((DENTRY_T
*) ep
, TYPE_EXTEND
);
495 ep
->order
= (u8
) order
;
497 ep
->checksum
= chksum
;
501 for (i
= 0; i
< 5; i
++) {
503 put_unaligned_le16(*uniname
, &(ep
->unicode_0_4
[i
<<1]));
509 put_unaligned_le16(0xFFFF, &(ep
->unicode_0_4
[i
<<1]));
514 for (i
= 0; i
< 6; i
++) {
516 ep
->unicode_5_10
[i
] = cpu_to_le16(*uniname
);
522 ep
->unicode_5_10
[i
] = cpu_to_le16(0xFFFF);
527 for (i
= 0; i
< 2; i
++) {
529 ep
->unicode_11_12
[i
] = cpu_to_le16(*uniname
);
535 ep
->unicode_11_12
[i
] = cpu_to_le16(0xFFFF);
538 } /* end of __init_ext_entry */
540 static s32
fat_init_dir_entry(struct super_block
*sb
, CHAIN_T
*p_dir
, s32 entry
, u32 type
,
541 u32 start_clu
, u64 size
)
544 DOS_DENTRY_T
*dos_ep
;
546 dos_ep
= (DOS_DENTRY_T
*) get_dentry_in_dir(sb
, p_dir
, entry
, §or
);
550 __init_dos_entry(sb
, dos_ep
, type
, start_clu
);
551 dcache_modify(sb
, sector
);
554 } /* end of fat_init_dir_entry */
556 static s32
fat_init_ext_entry(struct super_block
*sb
, CHAIN_T
*p_dir
, s32 entry
, s32 num_entries
,
557 UNI_NAME_T
*p_uniname
, DOS_NAME_T
*p_dosname
)
562 u16
*uniname
= p_uniname
->name
;
563 DOS_DENTRY_T
*dos_ep
;
564 EXT_DENTRY_T
*ext_ep
;
566 dos_ep
= (DOS_DENTRY_T
*) get_dentry_in_dir(sb
, p_dir
, entry
, §or
);
570 dos_ep
->lcase
= p_dosname
->name_case
;
571 memcpy(dos_ep
->name
, p_dosname
->name
, DOS_NAME_LENGTH
);
572 if (dcache_modify(sb
, sector
))
575 if ((--num_entries
) > 0) {
576 chksum
= calc_chksum_1byte((void *) dos_ep
->name
, DOS_NAME_LENGTH
, 0);
578 for (i
= 1; i
< num_entries
; i
++) {
579 ext_ep
= (EXT_DENTRY_T
*) get_dentry_in_dir(sb
, p_dir
, entry
-i
, §or
);
583 __init_ext_entry(ext_ep
, i
, chksum
, uniname
);
584 if (dcache_modify(sb
, sector
))
589 ext_ep
= (EXT_DENTRY_T
*) get_dentry_in_dir(sb
, p_dir
, entry
-i
, §or
);
593 __init_ext_entry(ext_ep
, i
+MSDOS_LAST_LFN
, chksum
, uniname
);
594 if (dcache_modify(sb
, sector
))
599 } /* end of fat_init_ext_entry */
601 static s32
fat_delete_dir_entry(struct super_block
*sb
, CHAIN_T
*p_dir
, s32 entry
, s32 order
, s32 num_entries
)
607 for (i
= num_entries
-1; i
>= order
; i
--) {
608 ep
= get_dentry_in_dir(sb
, p_dir
, entry
-i
, §or
);
612 fat_set_entry_type(ep
, TYPE_DELETED
);
613 if (dcache_modify(sb
, sector
))
620 /* return values of fat_find_dir_entry()
621 * >= 0 : return dir entiry position with the name in dir
622 * -EEXIST : (root dir, ".") it is the root dir itself
623 * -ENOENT : entry with the name does not exist
626 static inline s32
__get_dentries_per_clu(FS_INFO_T
*fsi
, s32 clu
)
628 if (IS_CLUS_FREE(clu
)) /* FAT16 root_dir */
629 return fsi
->dentries_in_root
;
631 return fsi
->dentries_per_clu
;
634 static s32
fat_find_dir_entry(struct super_block
*sb
, FILE_ID_T
*fid
,
635 CHAIN_T
*p_dir
, UNI_NAME_T
*p_uniname
, s32 num_entries
, DOS_NAME_T
*p_dosname
, u32 type
)
637 s32 i
, rewind
= 0, dentry
= 0, end_eidx
= 0;
638 s32 chksum
= 0, lfn_ord
= 0, lfn_len
= 0;
639 s32 dentries_per_clu
, num_empty
= 0;
641 u16 entry_uniname
[14], *uniname
= NULL
;
644 HINT_T
*hint_stat
= &fid
->hint_stat
;
645 HINT_FEMP_T candi_empty
;
646 FS_INFO_T
*fsi
= &(SDFAT_SB(sb
)->fsi
);
650 * DOT and DOTDOT are handled by VFS layer
653 dentries_per_clu
= __get_dentries_per_clu(fsi
, p_dir
->dir
);
654 clu
.dir
= p_dir
->dir
;
655 clu
.flags
= p_dir
->flags
;
657 if (hint_stat
->eidx
) {
658 clu
.dir
= hint_stat
->clu
;
659 dentry
= hint_stat
->eidx
;
663 candi_empty
.eidx
= -1;
665 MMSG("lookup dir= %s\n", p_dosname
->name
);
667 while (!IS_CLUS_EOF(clu
.dir
)) {
668 i
= dentry
% dentries_per_clu
;
669 for (; i
< dentries_per_clu
; i
++, dentry
++) {
670 if (rewind
&& (dentry
== end_eidx
))
673 ep
= get_dentry_in_dir(sb
, &clu
, i
, NULL
);
677 entry_type
= fat_get_entry_type(ep
);
680 * Most directory entries have long name,
681 * So, we check extend directory entry first.
683 if (entry_type
== TYPE_EXTEND
) {
684 EXT_DENTRY_T
*ext_ep
= (EXT_DENTRY_T
*)ep
;
685 u32 cur_ord
= (u32
)ext_ep
->order
;
686 u32 cur_chksum
= (s32
)ext_ep
->checksum
;
691 candi_empty
.eidx
= -1;
693 /* check whether new lfn or not */
694 if (cur_ord
& MSDOS_LAST_LFN
) {
695 cur_ord
&= ~(MSDOS_LAST_LFN
);
697 len
= (13 * (cur_ord
-1));
698 uniname
= (p_uniname
->name
+ len
);
699 lfn_ord
= cur_ord
+ 1;
702 /* check minimum name length */
704 (len
> p_uniname
->name_len
)) {
705 /* MISMATCHED NAME LENGTH */
711 /* invalid lfn order */
712 if (!cur_ord
|| (cur_ord
> MAX_LFN_ORDER
) ||
713 ((cur_ord
+ 1) != lfn_ord
))
714 goto reset_dentry_set
;
716 /* check checksum of directory entry set */
717 if (cur_chksum
!= chksum
)
718 goto reset_dentry_set
;
720 /* update order for next dentry */
723 /* check whether mismatched lfn or not */
725 /* MISMATCHED LFN DENTRY SET */
731 "%s : abnormal dentry "
733 "idx[%u])", __func__
,
735 sdfat_debug_bug_on(1);
739 /* update position of name buffer */
742 /* get utf16 characters saved on this entry */
743 len
= __extract_uni_name_from_ext_entry(ext_ep
, entry_uniname
, lfn_ord
);
745 /* replace last char to null */
746 unichar
= *(uniname
+len
);
747 *(uniname
+len
) = (u16
)0x0;
749 /* uniname ext_dentry unit compare repeatdly */
750 if (nls_cmp_uniname(sb
, uniname
, entry_uniname
)) {
751 /* DO HANDLE WRONG NAME */
754 /* add matched chars length */
758 /* restore previous character */
759 *(uniname
+len
) = unichar
;
761 /* jump to check next dentry */
764 } else if ((entry_type
== TYPE_FILE
) || (entry_type
== TYPE_DIR
)) {
765 DOS_DENTRY_T
*dos_ep
= (DOS_DENTRY_T
*)ep
;
766 u32 cur_chksum
= (s32
)calc_chksum_1byte(
767 (void *) dos_ep
->name
,
771 candi_empty
.eidx
= -1;
773 MMSG("checking dir= %c%c%c%c%c%c%c%c%c%c%c\n",
774 dos_ep
->name
[0], dos_ep
->name
[1],
775 dos_ep
->name
[2], dos_ep
->name
[3],
776 dos_ep
->name
[4], dos_ep
->name
[5],
777 dos_ep
->name
[6], dos_ep
->name
[7],
778 dos_ep
->name
[8], dos_ep
->name
[9],
782 * if there is no valid long filename,
783 * we should check short filename.
785 if (!lfn_len
|| (cur_chksum
!= chksum
)) {
786 /* check shortname */
787 if ((p_dosname
->name
[0] != '\0') &&
793 /* check name length */
794 } else if ((lfn_len
> 0) &&
795 ((s32
)p_uniname
->name_len
==
800 /* DO HANDLE MISMATCHED SFN, FALL THROUGH */
801 } else if ((entry_type
== TYPE_UNUSED
) || (entry_type
== TYPE_DELETED
)) {
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
)
829 /* TYPE_DELETED, TYPE_VOLUME OR MISMATCHED SFN */
835 if (IS_CLUS_FREE(p_dir
->dir
))
836 break; /* FAT16 root_dir */
838 if (get_next_clus_safe(sb
, &clu
.dir
))
843 /* we started at not 0 index,so we should try to find target
844 * from 0 index to the index we started at.
846 if (!rewind
&& end_eidx
) {
849 clu
.dir
= p_dir
->dir
;
850 /* reset dentry set */
854 /* reset empty hint_*/
856 candi_empty
.eidx
= -1;
860 /* initialized hint_stat */
861 hint_stat
->clu
= p_dir
->dir
;
866 /* next dentry we'll find is out of this cluster */
867 if (!((dentry
+ 1) % dentries_per_clu
)) {
870 if (IS_CLUS_FREE(p_dir
->dir
))
873 ret
= get_next_clus_safe(sb
, &clu
.dir
);
875 if (ret
|| IS_CLUS_EOF(clu
.dir
)) {
876 /* just initialized hint_stat */
877 hint_stat
->clu
= p_dir
->dir
;
883 hint_stat
->clu
= clu
.dir
;
884 hint_stat
->eidx
= dentry
+ 1;
886 } /* end of fat_find_dir_entry */
888 /* returns -EIO on error */
889 static s32
fat_count_ext_entries(struct super_block
*sb
, CHAIN_T
*p_dir
, s32 entry
, DENTRY_T
*p_entry
)
893 DOS_DENTRY_T
*dos_ep
= (DOS_DENTRY_T
*) p_entry
;
894 EXT_DENTRY_T
*ext_ep
;
896 chksum
= calc_chksum_1byte((void *) dos_ep
->name
, DOS_NAME_LENGTH
, 0);
898 for (entry
--; entry
>= 0; entry
--) {
899 ext_ep
= (EXT_DENTRY_T
*)get_dentry_in_dir(sb
, p_dir
, entry
, NULL
);
903 if ((fat_get_entry_type((DENTRY_T
*)ext_ep
) == TYPE_EXTEND
) &&
904 (ext_ep
->checksum
== chksum
)) {
906 if (ext_ep
->order
> MSDOS_LAST_LFN
)
918 * Name Conversion Functions
920 static s32
__extract_uni_name_from_ext_entry(EXT_DENTRY_T
*ep
, u16
*uniname
, s32 order
)
924 for (i
= 0; i
< 5; i
++) {
925 *uniname
= get_unaligned_le16(&(ep
->unicode_0_4
[i
<<1]));
933 for (i
= 0; i
< 6; i
++) {
934 /* FIXME : unaligned? */
935 *uniname
= le16_to_cpu(ep
->unicode_5_10
[i
]);
942 for (i
= 0; i
< 4; i
++) {
943 /* FIXME : unaligned? */
944 *uniname
= le16_to_cpu(ep
->unicode_5_10
[i
]);
950 *uniname
= 0x0; /* uniname[MAX_NAME_LENGTH] */
954 for (i
= 0; i
< 2; i
++) {
955 /* FIXME : unaligned? */
956 *uniname
= le16_to_cpu(ep
->unicode_11_12
[i
]);
966 } /* end of __extract_uni_name_from_ext_entry */
968 static void fat_get_uniname_from_ext_entry(struct super_block
*sb
, CHAIN_T
*p_dir
, s32 entry
, u16
*uniname
)
974 DOS_DENTRY_T
*dos_ep
=
975 (DOS_DENTRY_T
*)get_dentry_in_dir(sb
, p_dir
, entry
, NULL
);
977 if (unlikely(!dos_ep
))
980 chksum
= (u32
)calc_chksum_1byte(
981 (void *) dos_ep
->name
,
984 for (entry
--, i
= 1; entry
>= 0; entry
--, i
++) {
987 ep
= (EXT_DENTRY_T
*)get_dentry_in_dir(sb
, p_dir
, entry
, NULL
);
991 if (fat_get_entry_type((DENTRY_T
*) ep
) != TYPE_EXTEND
)
994 if (chksum
!= (u32
)ep
->checksum
)
997 if (i
!= (u32
)(ep
->order
& ~(MSDOS_LAST_LFN
)))
1000 __extract_uni_name_from_ext_entry(ep
, name
, (s32
)i
);
1001 if (ep
->order
& MSDOS_LAST_LFN
)
1007 *uniname
= (u16
)0x0;
1008 } /* end of fat_get_uniname_from_ext_entry */
1010 /* Find if the shortname exists
1011 * and check if there are free entries
1013 static s32
__fat_find_shortname_entry(struct super_block
*sb
, CHAIN_T
*p_dir
,
1014 u8
*p_dosname
, s32
*offset
, __attribute__((unused
))int n_entry_needed
)
1018 s32 dentries_per_clu
;
1019 DENTRY_T
*ep
= NULL
;
1020 DOS_DENTRY_T
*dos_ep
= NULL
;
1021 CHAIN_T clu
= *p_dir
;
1022 FS_INFO_T
*fsi
= &(SDFAT_SB(sb
)->fsi
);
1027 if (IS_CLUS_FREE(clu
.dir
)) /* FAT16 root_dir */
1028 dentries_per_clu
= fsi
->dentries_in_root
;
1030 dentries_per_clu
= fsi
->dentries_per_clu
;
1032 while (!IS_CLUS_EOF(clu
.dir
)) {
1033 for (i
= 0; i
< dentries_per_clu
; i
++, dentry
++) {
1034 ep
= get_dentry_in_dir(sb
, &clu
, i
, NULL
);
1038 type
= fat_get_entry_type(ep
);
1040 if ((type
== TYPE_FILE
) || (type
== TYPE_DIR
)) {
1041 dos_ep
= (DOS_DENTRY_T
*)ep
;
1042 if (!nls_cmp_sfn(sb
, p_dosname
, dos_ep
->name
)) {
1050 /* fat12/16 root dir */
1051 if (IS_CLUS_FREE(clu
.dir
))
1054 if (get_next_clus_safe(sb
, &clu
.dir
))
1060 #ifdef CONFIG_SDFAT_FAT32_SHORTNAME_SEQ
1061 static void __fat_attach_count_to_dos_name(u8
*dosname
, s32 count
)
1066 snprintf(str_count
, sizeof(str_count
), "~%d", count
);
1067 length
= strlen(str_count
);
1070 while (j
<= (8 - length
)) {
1072 if (dosname
[j
] == ' ')
1074 if (dosname
[j
] & 0x80)
1080 for (j
= 0; j
< length
; i
++, j
++)
1081 dosname
[i
] = (u8
) str_count
[j
];
1086 } /* end of __fat_attach_count_to_dos_name */
1089 s32
fat_generate_dos_name_new(struct super_block
*sb
, CHAIN_T
*p_dir
, DOS_NAME_T
*p_dosname
, s32 n_entry_needed
)
1093 u8 work
[DOS_NAME_LENGTH
], buf
[5];
1097 memset(work
, ' ', DOS_NAME_LENGTH
);
1098 memcpy(work
, p_dosname
->name
, DOS_NAME_LENGTH
);
1100 while (baselen
&& (work
[--baselen
] == ' ')) {
1101 /* DO NOTHING, JUST FOR CHECK_PATCH */
1107 BUG_ON(baselen
< 0);
1109 #ifdef CONFIG_SDFAT_FAT32_SHORTNAME_SEQ
1110 /* example) namei_exfat.c -> NAMEI_~1 - NAMEI_~9 */
1111 work
[baselen
] = '~';
1112 for (i
= 1; i
< 10; i
++) {
1113 // '0' + i = 1 ~ 9 ASCII
1114 work
[baselen
+ 1] = '0' + i
;
1115 err
= __fat_find_shortname_entry(sb
, p_dir
, work
, NULL
, n_entry_needed
);
1116 if (err
== -ENOENT
) {
1118 __fat_attach_count_to_dos_name(p_dosname
->name
, i
);
1122 /* any other error */
1129 tail
= (jiffies
>> 16) & 0x7;
1134 BUG_ON(baselen
< 0);
1136 work
[baselen
+ 4] = '~';
1138 work
[baselen
+ 5] = '1' + tail
;
1140 snprintf(buf
, sizeof(buf
), "%04X", i
& 0xffff);
1141 memcpy(&work
[baselen
], buf
, 4);
1142 err
= __fat_find_shortname_entry(sb
, p_dir
, work
, NULL
, n_entry_needed
);
1143 if (err
== -ENOENT
) {
1144 memcpy(p_dosname
->name
, work
, DOS_NAME_LENGTH
);
1148 /* any other error */
1155 } /* end of generate_dos_name_new */
1157 static s32
fat_calc_num_entries(UNI_NAME_T
*p_uniname
)
1161 len
= p_uniname
->name_len
;
1165 /* 1 dos name entry + extended entries */
1166 return((len
-1) / 13 + 2);
1168 } /* end of calc_num_enties */
1170 static s32
fat_check_max_dentries(FILE_ID_T
*fid
)
1172 if ((fid
->size
>> DENTRY_SIZE_BITS
) >= MAX_FAT_DENTRIES
) {
1173 /* FAT spec allows a dir to grow upto 65536 dentries */
1177 } /* end of check_max_dentries */
1181 * File Operation Functions
1183 static FS_FUNC_T fat_fs_func
= {
1184 .alloc_cluster
= fat_alloc_cluster
,
1185 .free_cluster
= fat_free_cluster
,
1186 .count_used_clusters
= fat_count_used_clusters
,
1188 .init_dir_entry
= fat_init_dir_entry
,
1189 .init_ext_entry
= fat_init_ext_entry
,
1190 .find_dir_entry
= fat_find_dir_entry
,
1191 .delete_dir_entry
= fat_delete_dir_entry
,
1192 .get_uniname_from_ext_entry
= fat_get_uniname_from_ext_entry
,
1193 .count_ext_entries
= fat_count_ext_entries
,
1194 .calc_num_entries
= fat_calc_num_entries
,
1195 .check_max_dentries
= fat_check_max_dentries
,
1197 .get_entry_type
= fat_get_entry_type
,
1198 .set_entry_type
= fat_set_entry_type
,
1199 .get_entry_attr
= fat_get_entry_attr
,
1200 .set_entry_attr
= fat_set_entry_attr
,
1201 .get_entry_flag
= fat_get_entry_flag
,
1202 .set_entry_flag
= fat_set_entry_flag
,
1203 .get_entry_clu0
= fat_get_entry_clu0
,
1204 .set_entry_clu0
= fat_set_entry_clu0
,
1205 .get_entry_size
= fat_get_entry_size
,
1206 .set_entry_size
= fat_set_entry_size
,
1207 .get_entry_time
= fat_get_entry_time
,
1208 .set_entry_time
= fat_set_entry_time
,
1211 static FS_FUNC_T amap_fat_fs_func
= {
1212 .alloc_cluster
= amap_fat_alloc_cluster
,
1213 .free_cluster
= fat_free_cluster
,
1214 .count_used_clusters
= fat_count_used_clusters
,
1216 .init_dir_entry
= fat_init_dir_entry
,
1217 .init_ext_entry
= fat_init_ext_entry
,
1218 .find_dir_entry
= fat_find_dir_entry
,
1219 .delete_dir_entry
= fat_delete_dir_entry
,
1220 .get_uniname_from_ext_entry
= fat_get_uniname_from_ext_entry
,
1221 .count_ext_entries
= fat_count_ext_entries
,
1222 .calc_num_entries
= fat_calc_num_entries
,
1223 .check_max_dentries
= fat_check_max_dentries
,
1225 .get_entry_type
= fat_get_entry_type
,
1226 .set_entry_type
= fat_set_entry_type
,
1227 .get_entry_attr
= fat_get_entry_attr
,
1228 .set_entry_attr
= fat_set_entry_attr
,
1229 .get_entry_flag
= fat_get_entry_flag
,
1230 .set_entry_flag
= fat_set_entry_flag
,
1231 .get_entry_clu0
= fat_get_entry_clu0
,
1232 .set_entry_clu0
= fat_set_entry_clu0
,
1233 .get_entry_size
= fat_get_entry_size
,
1234 .set_entry_size
= fat_set_entry_size
,
1235 .get_entry_time
= fat_get_entry_time
,
1236 .set_entry_time
= fat_set_entry_time
,
1238 .get_au_stat
= amap_get_au_stat
,
1241 s32
mount_fat16(struct super_block
*sb
, pbr_t
*p_pbr
)
1243 s32 num_root_sectors
;
1244 bpb16_t
*p_bpb
= &(p_pbr
->bpb
.f16
);
1245 FS_INFO_T
*fsi
= &(SDFAT_SB(sb
)->fsi
);
1247 if (!p_bpb
->num_fats
) {
1248 sdfat_msg(sb
, KERN_ERR
, "bogus number of FAT structure");
1252 num_root_sectors
= get_unaligned_le16(p_bpb
->num_root_entries
) << DENTRY_SIZE_BITS
;
1253 num_root_sectors
= ((num_root_sectors
-1) >> sb
->s_blocksize_bits
) + 1;
1255 fsi
->sect_per_clus
= p_bpb
->sect_per_clus
;
1256 fsi
->sect_per_clus_bits
= ilog2(p_bpb
->sect_per_clus
);
1257 fsi
->cluster_size_bits
= fsi
->sect_per_clus_bits
+ sb
->s_blocksize_bits
;
1258 fsi
->cluster_size
= 1 << fsi
->cluster_size_bits
;
1260 fsi
->num_FAT_sectors
= le16_to_cpu(p_bpb
->num_fat_sectors
);
1262 fsi
->FAT1_start_sector
= le16_to_cpu(p_bpb
->num_reserved
);
1263 if (p_bpb
->num_fats
== 1)
1264 fsi
->FAT2_start_sector
= fsi
->FAT1_start_sector
;
1266 fsi
->FAT2_start_sector
= fsi
->FAT1_start_sector
+ fsi
->num_FAT_sectors
;
1268 fsi
->root_start_sector
= fsi
->FAT2_start_sector
+ fsi
->num_FAT_sectors
;
1269 fsi
->data_start_sector
= fsi
->root_start_sector
+ num_root_sectors
;
1271 fsi
->num_sectors
= get_unaligned_le16(p_bpb
->num_sectors
);
1272 if (!fsi
->num_sectors
)
1273 fsi
->num_sectors
= le32_to_cpu(p_bpb
->num_huge_sectors
);
1275 if (!fsi
->num_sectors
) {
1276 sdfat_msg(sb
, KERN_ERR
, "bogus number of total sector count");
1280 fsi
->num_clusters
= (u32
)((fsi
->num_sectors
- fsi
->data_start_sector
) >> fsi
->sect_per_clus_bits
) + CLUS_BASE
;
1281 /* because the cluster index starts with 2 */
1283 fsi
->vol_type
= FAT16
;
1284 if (fsi
->num_clusters
< FAT12_THRESHOLD
)
1285 fsi
->vol_type
= FAT12
;
1287 fsi
->vol_id
= get_unaligned_le32(p_bpb
->vol_serial
);
1290 fsi
->dentries_in_root
= get_unaligned_le16(p_bpb
->num_root_entries
);
1291 if (!fsi
->dentries_in_root
) {
1292 sdfat_msg(sb
, KERN_ERR
, "bogus number of max dentry count "
1293 "of the root directory");
1297 fsi
->dentries_per_clu
= 1 << (fsi
->cluster_size_bits
- DENTRY_SIZE_BITS
);
1299 fsi
->vol_flag
= VOL_CLEAN
;
1300 fsi
->clu_srch_ptr
= 2;
1301 fsi
->used_clusters
= (u32
) ~0;
1303 fsi
->fs_func
= &fat_fs_func
;
1304 fat_ent_ops_init(sb
);
1306 if (p_bpb
->state
& FAT_VOL_DIRTY
) {
1307 fsi
->vol_flag
|= VOL_DIRTY
;
1308 sdfat_log_msg(sb
, KERN_WARNING
, "Volume was not properly "
1309 "unmounted. Some data may be corrupt. "
1310 "Please run fsck.");
1314 } /* end of mount_fat16 */
1316 static sector_t
__calc_hidden_sect(struct super_block
*sb
)
1318 struct block_device
*bdev
= sb
->s_bdev
;
1319 sector_t hidden
= 0;
1324 hidden
= bdev
->bd_part
->start_sect
;
1325 /* a disk device, not a partition */
1327 if (bdev
!= bdev
->bd_contains
)
1328 sdfat_log_msg(sb
, KERN_WARNING
,
1329 "hidden(0), but disk has a partition table");
1333 if (sb
->s_blocksize_bits
!= 9) {
1334 ASSERT(sb
->s_blocksize_bits
> 9);
1335 hidden
>>= (sb
->s_blocksize_bits
- 9);
1339 sdfat_log_msg(sb
, KERN_INFO
, "start_sect of part(%d) : %lld",
1340 bdev
? bdev
->bd_part
->partno
: -1, (s64
)hidden
);
1345 s32
mount_fat32(struct super_block
*sb
, pbr_t
*p_pbr
)
1347 pbr32_t
*p_bpb
= (pbr32_t
*)p_pbr
;
1348 FS_INFO_T
*fsi
= &(SDFAT_SB(sb
)->fsi
);
1350 if (!p_bpb
->bpb
.num_fats
) {
1351 sdfat_msg(sb
, KERN_ERR
, "bogus number of FAT structure");
1355 fsi
->sect_per_clus
= p_bpb
->bpb
.sect_per_clus
;
1356 fsi
->sect_per_clus_bits
= ilog2(p_bpb
->bpb
.sect_per_clus
);
1357 fsi
->cluster_size_bits
= fsi
->sect_per_clus_bits
+ sb
->s_blocksize_bits
;
1358 fsi
->cluster_size
= 1 << fsi
->cluster_size_bits
;
1360 fsi
->num_FAT_sectors
= le32_to_cpu(p_bpb
->bpb
.num_fat32_sectors
);
1362 fsi
->FAT1_start_sector
= le16_to_cpu(p_bpb
->bpb
.num_reserved
);
1363 if (p_bpb
->bpb
.num_fats
== 1)
1364 fsi
->FAT2_start_sector
= fsi
->FAT1_start_sector
;
1366 fsi
->FAT2_start_sector
= fsi
->FAT1_start_sector
+ fsi
->num_FAT_sectors
;
1368 fsi
->root_start_sector
= fsi
->FAT2_start_sector
+ fsi
->num_FAT_sectors
;
1369 fsi
->data_start_sector
= fsi
->root_start_sector
;
1371 /* SPEC violation for compatibility */
1372 fsi
->num_sectors
= get_unaligned_le16(p_bpb
->bpb
.num_sectors
);
1373 if (!fsi
->num_sectors
)
1374 fsi
->num_sectors
= le32_to_cpu(p_bpb
->bpb
.num_huge_sectors
);
1377 if (!fsi
->num_sectors
) {
1378 sdfat_msg(sb
, KERN_ERR
, "bogus number of total sector count");
1382 fsi
->num_clusters
= (u32
)((fsi
->num_sectors
- fsi
->data_start_sector
) >> fsi
->sect_per_clus_bits
) + CLUS_BASE
;
1383 /* because the cluster index starts with 2 */
1385 fsi
->vol_type
= FAT32
;
1386 fsi
->vol_id
= get_unaligned_le32(p_bpb
->bsx
.vol_serial
);
1388 fsi
->root_dir
= le32_to_cpu(p_bpb
->bpb
.root_cluster
);
1389 fsi
->dentries_in_root
= 0;
1390 fsi
->dentries_per_clu
= 1 << (fsi
->cluster_size_bits
- DENTRY_SIZE_BITS
);
1392 fsi
->vol_flag
= VOL_CLEAN
;
1393 fsi
->clu_srch_ptr
= 2;
1394 fsi
->used_clusters
= (u32
) ~0;
1396 fsi
->fs_func
= &fat_fs_func
;
1398 /* Delayed / smart allocation related init */
1399 fsi
->reserved_clusters
= 0;
1401 /* Should be initialized before calling amap_create() */
1402 fat_ent_ops_init(sb
);
1404 /* AU Map Creation */
1405 if (SDFAT_SB(sb
)->options
.improved_allocation
& SDFAT_ALLOC_SMART
) {
1406 u32 hidden_sectors
= le32_to_cpu(p_bpb
->bpb
.num_hid_sectors
);
1407 u32 calc_hid_sect
= 0;
1411 /* calculate hidden sector size */
1412 calc_hid_sect
= __calc_hidden_sect(sb
);
1413 if (calc_hid_sect
!= hidden_sectors
) {
1414 sdfat_log_msg(sb
, KERN_WARNING
, "abnormal hidden "
1415 "sector : bpb(%u) != ondisk(%u)",
1416 hidden_sectors
, calc_hid_sect
);
1417 if (SDFAT_SB(sb
)->options
.adj_hidsect
) {
1418 sdfat_log_msg(sb
, KERN_INFO
,
1419 "adjustment hidden sector : "
1420 "bpb(%u) -> ondisk(%u)",
1421 hidden_sectors
, calc_hid_sect
);
1422 hidden_sectors
= calc_hid_sect
;
1426 SDFAT_SB(sb
)->options
.amap_opt
.misaligned_sect
= hidden_sectors
;
1428 /* calculate AU size if it's not set */
1429 if (!SDFAT_SB(sb
)->options
.amap_opt
.sect_per_au
) {
1430 SDFAT_SB(sb
)->options
.amap_opt
.sect_per_au
=
1431 __calc_default_au_size(sb
);
1434 ret
= amap_create(sb
,
1435 SDFAT_SB(sb
)->options
.amap_opt
.pack_ratio
,
1436 SDFAT_SB(sb
)->options
.amap_opt
.sect_per_au
,
1437 SDFAT_SB(sb
)->options
.amap_opt
.misaligned_sect
);
1439 sdfat_log_msg(sb
, KERN_WARNING
, "failed to create AMAP."
1440 " disabling smart allocation. (err:%d)", ret
);
1441 SDFAT_SB(sb
)->options
.improved_allocation
&= ~(SDFAT_ALLOC_SMART
);
1443 fsi
->fs_func
= &amap_fat_fs_func
;
1447 /* Check dependency of mount options */
1448 if (SDFAT_SB(sb
)->options
.improved_allocation
!=
1449 (SDFAT_ALLOC_DELAY
| SDFAT_ALLOC_SMART
)) {
1450 sdfat_log_msg(sb
, KERN_INFO
, "disabling defragmentation because"
1451 " smart, delay options are disabled");
1452 SDFAT_SB(sb
)->options
.defrag
= 0;
1455 if (p_bpb
->bsx
.state
& FAT_VOL_DIRTY
) {
1456 fsi
->vol_flag
|= VOL_DIRTY
;
1457 sdfat_log_msg(sb
, KERN_WARNING
, "Volume was not properly "
1458 "unmounted. Some data may be corrupt. "
1459 "Please run fsck.");
1463 } /* end of mount_fat32 */
1465 /* end of core_fat.c */