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_alloc_cluster(struct super_block
*sb
, s32 num_alloc
, CHAIN_T
*p_chain
, int dest
)
160 s32 i
, num_clusters
= 0;
161 u32 new_clu
, last_clu
= CLUS_EOF
, read_clu
;
162 FS_INFO_T
*fsi
= &(SDFAT_SB(sb
)->fsi
);
165 new_clu
= p_chain
->dir
;
166 if (IS_CLUS_EOF(new_clu
))
167 new_clu
= fsi
->clu_srch_ptr
;
168 else if (new_clu
>= fsi
->num_clusters
)
173 p_chain
->dir
= CLUS_EOF
;
175 for (i
= CLUS_BASE
; i
< fsi
->num_clusters
; i
++) {
176 if (fat_ent_get(sb
, new_clu
, &read_clu
))
179 if (IS_CLUS_FREE(read_clu
)) {
180 if (fat_ent_set(sb
, new_clu
, CLUS_EOF
))
184 if (IS_CLUS_EOF(p_chain
->dir
)) {
185 p_chain
->dir
= new_clu
;
187 if (fat_ent_set(sb
, last_clu
, new_clu
))
193 if ((--num_alloc
) == 0) {
194 fsi
->clu_srch_ptr
= new_clu
;
195 if (fsi
->used_clusters
!= (u32
) ~0)
196 fsi
->used_clusters
+= num_clusters
;
201 if ((++new_clu
) >= fsi
->num_clusters
)
205 fsi
->clu_srch_ptr
= new_clu
;
206 if (fsi
->used_clusters
!= (u32
) ~0)
207 fsi
->used_clusters
+= num_clusters
;
210 } /* end of fat_alloc_cluster */
212 static s32
fat_free_cluster(struct super_block
*sb
, CHAIN_T
*p_chain
, s32 do_relse
)
215 s32 num_clusters
= 0;
217 FS_INFO_T
*fsi
= &(SDFAT_SB(sb
)->fsi
);
221 /* invalid cluster number */
222 if (IS_CLUS_FREE(p_chain
->dir
) || IS_CLUS_EOF(p_chain
->dir
))
225 /* no cluster to truncate */
226 if (p_chain
->size
<= 0) {
227 DMSG("%s: cluster(%u) truncation is not required.",
228 __func__
, p_chain
->dir
);
232 /* check cluster validation */
233 if ((p_chain
->dir
< 2) && (p_chain
->dir
>= fsi
->num_clusters
)) {
234 EMSG("%s: invalid start cluster (%u)\n", __func__
, p_chain
->dir
);
235 sdfat_debug_bug_on(1);
245 sector
= CLUS_TO_SECT(fsi
, clu
);
246 for (i
= 0; i
< fsi
->sect_per_clus
; i
++) {
247 if (dcache_release(sb
, sector
+i
) == -EIO
)
253 if (get_next_clus_safe(sb
, &clu
)) {
254 /* print more helpful log */
255 if (IS_CLUS_BAD(clu
)) {
256 sdfat_log_msg(sb
, KERN_ERR
, "%s : "
257 "deleting bad cluster (clu[%u]->BAD)",
259 } else if (IS_CLUS_FREE(clu
)) {
260 sdfat_log_msg(sb
, KERN_ERR
, "%s : "
261 "deleting free cluster (clu[%u]->FREE)",
268 if (fat_ent_set(sb
, prev
, CLUS_FREE
))
271 /* Update AMAP if needed */
273 if (amap_release_cluster(sb
, prev
))
279 } while (!IS_CLUS_EOF(clu
));
284 if (fsi
->used_clusters
!= (u32
) ~0)
285 fsi
->used_clusters
-= num_clusters
;
287 } /* end of fat_free_cluster */
289 static s32
fat_count_used_clusters(struct super_block
*sb
, u32
*ret_count
)
293 FS_INFO_T
*fsi
= &(SDFAT_SB(sb
)->fsi
);
295 for (i
= CLUS_BASE
; i
< fsi
->num_clusters
; i
++) {
296 if (fat_ent_get(sb
, i
, &clu
))
299 if (!IS_CLUS_FREE(clu
))
305 } /* end of fat_count_used_clusters */
309 * Directory Entry Management Functions
311 static u32
fat_get_entry_type(DENTRY_T
*p_entry
)
313 DOS_DENTRY_T
*ep
= (DOS_DENTRY_T
*)p_entry
;
315 /* first byte of 32bytes dummy */
316 if (*(ep
->name
) == MSDOS_UNUSED
)
319 /* 0xE5 of Kanji Japanese is replaced to 0x05 */
320 else if (*(ep
->name
) == MSDOS_DELETED
)
323 /* 11th byte of 32bytes dummy */
324 else if ((ep
->attr
& ATTR_EXTEND_MASK
) == ATTR_EXTEND
)
327 else if (!(ep
->attr
& (ATTR_SUBDIR
| ATTR_VOLUME
)))
330 else if ((ep
->attr
& (ATTR_SUBDIR
| ATTR_VOLUME
)) == ATTR_SUBDIR
)
333 else if ((ep
->attr
& (ATTR_SUBDIR
| ATTR_VOLUME
)) == ATTR_VOLUME
)
337 } /* end of fat_get_entry_type */
339 static void fat_set_entry_type(DENTRY_T
*p_entry
, u32 type
)
341 DOS_DENTRY_T
*ep
= (DOS_DENTRY_T
*)p_entry
;
343 if (type
== TYPE_UNUSED
)
344 *(ep
->name
) = MSDOS_UNUSED
; /* 0x0 */
346 else if (type
== TYPE_DELETED
)
347 *(ep
->name
) = MSDOS_DELETED
; /* 0xE5 */
349 else if (type
== TYPE_EXTEND
)
350 ep
->attr
= ATTR_EXTEND
;
352 else if (type
== TYPE_DIR
)
353 ep
->attr
= ATTR_SUBDIR
;
355 else if (type
== TYPE_FILE
)
356 ep
->attr
= ATTR_ARCHIVE
;
358 else if (type
== TYPE_SYMLINK
)
359 ep
->attr
= ATTR_ARCHIVE
| ATTR_SYMLINK
;
360 } /* end of fat_set_entry_type */
362 static u32
fat_get_entry_attr(DENTRY_T
*p_entry
)
364 DOS_DENTRY_T
*ep
= (DOS_DENTRY_T
*)p_entry
;
366 return (u32
)ep
->attr
;
367 } /* end of fat_get_entry_attr */
369 static void fat_set_entry_attr(DENTRY_T
*p_entry
, u32 attr
)
371 DOS_DENTRY_T
*ep
= (DOS_DENTRY_T
*)p_entry
;
374 } /* end of fat_set_entry_attr */
376 static u8
fat_get_entry_flag(DENTRY_T
*p_entry
)
379 } /* end of fat_get_entry_flag */
381 static void fat_set_entry_flag(DENTRY_T
*p_entry
, u8 flags
)
383 } /* end of fat_set_entry_flag */
385 static u32
fat_get_entry_clu0(DENTRY_T
*p_entry
)
387 DOS_DENTRY_T
*ep
= (DOS_DENTRY_T
*)p_entry
;
389 return(((u32
)(le16_to_cpu(ep
->start_clu_hi
)) << 16) | le16_to_cpu(ep
->start_clu_lo
));
390 } /* end of fat_get_entry_clu0 */
392 static void fat_set_entry_clu0(DENTRY_T
*p_entry
, u32 start_clu
)
394 DOS_DENTRY_T
*ep
= (DOS_DENTRY_T
*)p_entry
;
396 ep
->start_clu_lo
= cpu_to_le16(CLUSTER_16(start_clu
));
397 ep
->start_clu_hi
= cpu_to_le16(CLUSTER_16(start_clu
>> 16));
398 } /* end of fat_set_entry_clu0 */
400 static u64
fat_get_entry_size(DENTRY_T
*p_entry
)
402 DOS_DENTRY_T
*ep
= (DOS_DENTRY_T
*)p_entry
;
404 return (u64
)le32_to_cpu(ep
->size
);
405 } /* end of fat_get_entry_size */
407 static void fat_set_entry_size(DENTRY_T
*p_entry
, u64 size
)
409 DOS_DENTRY_T
*ep
= (DOS_DENTRY_T
*)p_entry
;
411 ep
->size
= cpu_to_le32((u32
)size
);
412 } /* end of fat_set_entry_size */
414 static void fat_get_entry_time(DENTRY_T
*p_entry
, TIMESTAMP_T
*tp
, u8 mode
)
416 u16 t
= 0x00, d
= 0x21;
417 DOS_DENTRY_T
*ep
= (DOS_DENTRY_T
*) p_entry
;
421 t
= le16_to_cpu(ep
->create_time
);
422 d
= le16_to_cpu(ep
->create_date
);
425 t
= le16_to_cpu(ep
->modify_time
);
426 d
= le16_to_cpu(ep
->modify_date
);
430 tp
->sec
= (t
& 0x001F) << 1;
431 tp
->min
= (t
>> 5) & 0x003F;
432 tp
->hour
= (t
>> 11);
433 tp
->day
= (d
& 0x001F);
434 tp
->mon
= (d
>> 5) & 0x000F;
436 } /* end of fat_get_entry_time */
438 static void fat_set_entry_time(DENTRY_T
*p_entry
, TIMESTAMP_T
*tp
, u8 mode
)
441 DOS_DENTRY_T
*ep
= (DOS_DENTRY_T
*) p_entry
;
443 t
= (tp
->hour
<< 11) | (tp
->min
<< 5) | (tp
->sec
>> 1);
444 d
= (tp
->year
<< 9) | (tp
->mon
<< 5) | tp
->day
;
448 ep
->create_time
= cpu_to_le16(t
);
449 ep
->create_date
= cpu_to_le16(d
);
452 ep
->modify_time
= cpu_to_le16(t
);
453 ep
->modify_date
= cpu_to_le16(d
);
456 } /* end of fat_set_entry_time */
458 static void __init_dos_entry(struct super_block
*sb
, DOS_DENTRY_T
*ep
, u32 type
, u32 start_clu
)
462 fat_set_entry_type((DENTRY_T
*) ep
, type
);
463 ep
->start_clu_lo
= cpu_to_le16(CLUSTER_16(start_clu
));
464 ep
->start_clu_hi
= cpu_to_le16(CLUSTER_16(start_clu
>> 16));
467 tp
= tm_now(SDFAT_SB(sb
), &tm
);
468 fat_set_entry_time((DENTRY_T
*) ep
, tp
, TM_CREATE
);
469 fat_set_entry_time((DENTRY_T
*) ep
, tp
, TM_MODIFY
);
471 ep
->create_time_ms
= 0;
472 } /* end of __init_dos_entry */
474 static void __init_ext_entry(EXT_DENTRY_T
*ep
, s32 order
, u8 chksum
, u16
*uniname
)
479 fat_set_entry_type((DENTRY_T
*) ep
, TYPE_EXTEND
);
480 ep
->order
= (u8
) order
;
482 ep
->checksum
= chksum
;
486 for (i
= 0; i
< 5; i
++) {
488 put_unaligned_le16(*uniname
, &(ep
->unicode_0_4
[i
<<1]));
494 put_unaligned_le16(0xFFFF, &(ep
->unicode_0_4
[i
<<1]));
499 for (i
= 0; i
< 6; i
++) {
501 ep
->unicode_5_10
[i
] = cpu_to_le16(*uniname
);
507 ep
->unicode_5_10
[i
] = cpu_to_le16(0xFFFF);
512 for (i
= 0; i
< 2; i
++) {
514 ep
->unicode_11_12
[i
] = cpu_to_le16(*uniname
);
520 ep
->unicode_11_12
[i
] = cpu_to_le16(0xFFFF);
523 } /* end of __init_ext_entry */
525 static s32
fat_init_dir_entry(struct super_block
*sb
, CHAIN_T
*p_dir
, s32 entry
, u32 type
,
526 u32 start_clu
, u64 size
)
529 DOS_DENTRY_T
*dos_ep
;
531 dos_ep
= (DOS_DENTRY_T
*) get_dentry_in_dir(sb
, p_dir
, entry
, §or
);
535 __init_dos_entry(sb
, dos_ep
, type
, start_clu
);
536 dcache_modify(sb
, sector
);
539 } /* end of fat_init_dir_entry */
541 static s32
fat_init_ext_entry(struct super_block
*sb
, CHAIN_T
*p_dir
, s32 entry
, s32 num_entries
,
542 UNI_NAME_T
*p_uniname
, DOS_NAME_T
*p_dosname
)
547 u16
*uniname
= p_uniname
->name
;
548 DOS_DENTRY_T
*dos_ep
;
549 EXT_DENTRY_T
*ext_ep
;
551 dos_ep
= (DOS_DENTRY_T
*) get_dentry_in_dir(sb
, p_dir
, entry
, §or
);
555 dos_ep
->lcase
= p_dosname
->name_case
;
556 memcpy(dos_ep
->name
, p_dosname
->name
, DOS_NAME_LENGTH
);
557 if (dcache_modify(sb
, sector
))
560 if ((--num_entries
) > 0) {
561 chksum
= calc_chksum_1byte((void *) dos_ep
->name
, DOS_NAME_LENGTH
, 0);
563 for (i
= 1; i
< num_entries
; i
++) {
564 ext_ep
= (EXT_DENTRY_T
*) get_dentry_in_dir(sb
, p_dir
, entry
-i
, §or
);
568 __init_ext_entry(ext_ep
, i
, chksum
, uniname
);
569 if (dcache_modify(sb
, sector
))
574 ext_ep
= (EXT_DENTRY_T
*) get_dentry_in_dir(sb
, p_dir
, entry
-i
, §or
);
578 __init_ext_entry(ext_ep
, i
+MSDOS_LAST_LFN
, chksum
, uniname
);
579 if (dcache_modify(sb
, sector
))
584 } /* end of fat_init_ext_entry */
586 static s32
fat_delete_dir_entry(struct super_block
*sb
, CHAIN_T
*p_dir
, s32 entry
, s32 order
, s32 num_entries
)
592 for (i
= num_entries
-1; i
>= order
; i
--) {
593 ep
= get_dentry_in_dir(sb
, p_dir
, entry
-i
, §or
);
597 fat_set_entry_type(ep
, TYPE_DELETED
);
598 if (dcache_modify(sb
, sector
))
605 /* return values of fat_find_dir_entry()
606 * >= 0 : return dir entiry position with the name in dir
607 * -EEXIST : (root dir, ".") it is the root dir itself
608 * -ENOENT : entry with the name does not exist
611 static inline s32
__get_dentries_per_clu(FS_INFO_T
*fsi
, s32 clu
)
613 if (IS_CLUS_FREE(clu
)) /* FAT16 root_dir */
614 return fsi
->dentries_in_root
;
616 return fsi
->dentries_per_clu
;
619 static s32
fat_find_dir_entry(struct super_block
*sb
, FILE_ID_T
*fid
,
620 CHAIN_T
*p_dir
, UNI_NAME_T
*p_uniname
, s32 num_entries
, DOS_NAME_T
*p_dosname
, u32 type
)
622 s32 i
, rewind
= 0, dentry
= 0, end_eidx
= 0;
623 s32 chksum
= 0, lfn_ord
= 0, lfn_len
= 0;
624 s32 dentries_per_clu
, num_empty
= 0;
626 u16 entry_uniname
[14], *uniname
= NULL
;
629 HINT_T
*hint_stat
= &fid
->hint_stat
;
630 HINT_FEMP_T candi_empty
;
631 FS_INFO_T
*fsi
= &(SDFAT_SB(sb
)->fsi
);
635 * DOT and DOTDOT are handled by VFS layer
638 dentries_per_clu
= __get_dentries_per_clu(fsi
, p_dir
->dir
);
639 clu
.dir
= p_dir
->dir
;
640 clu
.flags
= p_dir
->flags
;
642 if (hint_stat
->eidx
) {
643 clu
.dir
= hint_stat
->clu
;
644 dentry
= hint_stat
->eidx
;
648 candi_empty
.eidx
= -1;
650 MMSG("lookup dir= %s\n", p_dosname
->name
);
652 while (!IS_CLUS_EOF(clu
.dir
)) {
653 i
= dentry
% dentries_per_clu
;
654 for (; i
< dentries_per_clu
; i
++, dentry
++) {
655 if (rewind
&& (dentry
== end_eidx
))
658 ep
= get_dentry_in_dir(sb
, &clu
, i
, NULL
);
662 entry_type
= fat_get_entry_type(ep
);
665 * Most directory entries have long name,
666 * So, we check extend directory entry first.
668 if (entry_type
== TYPE_EXTEND
) {
669 EXT_DENTRY_T
*ext_ep
= (EXT_DENTRY_T
*)ep
;
670 u32 cur_ord
= (u32
)ext_ep
->order
;
671 u32 cur_chksum
= (s32
)ext_ep
->checksum
;
676 candi_empty
.eidx
= -1;
678 /* check whether new lfn or not */
679 if (cur_ord
& MSDOS_LAST_LFN
) {
680 cur_ord
&= ~(MSDOS_LAST_LFN
);
682 len
= (13 * (cur_ord
-1));
683 uniname
= (p_uniname
->name
+ len
);
684 lfn_ord
= cur_ord
+ 1;
687 /* check minimum name length */
689 (len
> p_uniname
->name_len
)) {
690 /* MISMATCHED NAME LENGTH */
696 /* invalid lfn order */
697 if (!cur_ord
|| (cur_ord
> MAX_LFN_ORDER
) ||
698 ((cur_ord
+ 1) != lfn_ord
))
699 goto reset_dentry_set
;
701 /* check checksum of directory entry set */
702 if (cur_chksum
!= chksum
)
703 goto reset_dentry_set
;
705 /* update order for next dentry */
708 /* check whether mismatched lfn or not */
710 /* MISMATCHED LFN DENTRY SET */
716 "%s : abnormal dentry "
718 "idx[%u])", __func__
,
720 sdfat_debug_bug_on(1);
724 /* update position of name buffer */
727 /* get utf16 characters saved on this entry */
728 len
= __extract_uni_name_from_ext_entry(ext_ep
, entry_uniname
, lfn_ord
);
730 /* replace last char to null */
731 unichar
= *(uniname
+len
);
732 *(uniname
+len
) = (u16
)0x0;
734 /* uniname ext_dentry unit compare repeatdly */
735 if (nls_cmp_uniname(sb
, uniname
, entry_uniname
)) {
736 /* DO HANDLE WRONG NAME */
739 /* add matched chars length */
743 /* restore previous character */
744 *(uniname
+len
) = unichar
;
746 /* jump to check next dentry */
749 } else if ((entry_type
== TYPE_FILE
) || (entry_type
== TYPE_DIR
)) {
750 DOS_DENTRY_T
*dos_ep
= (DOS_DENTRY_T
*)ep
;
751 u32 cur_chksum
= (s32
)calc_chksum_1byte(
752 (void *) dos_ep
->name
,
756 candi_empty
.eidx
= -1;
758 MMSG("checking dir= %c%c%c%c%c%c%c%c%c%c%c\n",
759 dos_ep
->name
[0], dos_ep
->name
[1],
760 dos_ep
->name
[2], dos_ep
->name
[3],
761 dos_ep
->name
[4], dos_ep
->name
[5],
762 dos_ep
->name
[6], dos_ep
->name
[7],
763 dos_ep
->name
[8], dos_ep
->name
[9],
767 * if there is no valid long filename,
768 * we should check short filename.
770 if (!lfn_len
|| (cur_chksum
!= chksum
)) {
771 /* check shortname */
772 if ((p_dosname
->name
[0] != '\0') &&
778 /* check name length */
779 } else if ((lfn_len
> 0) &&
780 ((s32
)p_uniname
->name_len
==
785 /* DO HANDLE MISMATCHED SFN, FALL THROUGH */
786 } else if ((entry_type
== TYPE_UNUSED
) || (entry_type
== TYPE_DELETED
)) {
788 if (candi_empty
.eidx
== -1) {
789 if (num_empty
== 1) {
790 candi_empty
.cur
.dir
= clu
.dir
;
791 candi_empty
.cur
.size
= clu
.size
;
792 candi_empty
.cur
.flags
= clu
.flags
;
795 if (num_empty
>= num_entries
) {
796 candi_empty
.eidx
= dentry
- (num_empty
- 1);
797 ASSERT(0 <= candi_empty
.eidx
);
798 candi_empty
.count
= num_empty
;
800 if ((fid
->hint_femp
.eidx
== -1) ||
801 (candi_empty
.eidx
<= fid
->hint_femp
.eidx
)) {
802 memcpy(&fid
->hint_femp
,
804 sizeof(HINT_FEMP_T
));
809 if (entry_type
== TYPE_UNUSED
)
814 /* TYPE_DELETED, TYPE_VOLUME OR MISMATCHED SFN */
820 if (IS_CLUS_FREE(p_dir
->dir
))
821 break; /* FAT16 root_dir */
823 if (get_next_clus_safe(sb
, &clu
.dir
))
828 /* we started at not 0 index,so we should try to find target
829 * from 0 index to the index we started at.
831 if (!rewind
&& end_eidx
) {
834 clu
.dir
= p_dir
->dir
;
835 /* reset dentry set */
839 /* reset empty hint_*/
841 candi_empty
.eidx
= -1;
845 /* initialized hint_stat */
846 hint_stat
->clu
= p_dir
->dir
;
851 /* next dentry we'll find is out of this cluster */
852 if (!((dentry
+ 1) % dentries_per_clu
)) {
855 if (IS_CLUS_FREE(p_dir
->dir
))
858 ret
= get_next_clus_safe(sb
, &clu
.dir
);
860 if (ret
|| IS_CLUS_EOF(clu
.dir
)) {
861 /* just initialized hint_stat */
862 hint_stat
->clu
= p_dir
->dir
;
868 hint_stat
->clu
= clu
.dir
;
869 hint_stat
->eidx
= dentry
+ 1;
871 } /* end of fat_find_dir_entry */
873 /* returns -EIO on error */
874 static s32
fat_count_ext_entries(struct super_block
*sb
, CHAIN_T
*p_dir
, s32 entry
, DENTRY_T
*p_entry
)
878 DOS_DENTRY_T
*dos_ep
= (DOS_DENTRY_T
*) p_entry
;
879 EXT_DENTRY_T
*ext_ep
;
881 chksum
= calc_chksum_1byte((void *) dos_ep
->name
, DOS_NAME_LENGTH
, 0);
883 for (entry
--; entry
>= 0; entry
--) {
884 ext_ep
= (EXT_DENTRY_T
*)get_dentry_in_dir(sb
, p_dir
, entry
, NULL
);
888 if ((fat_get_entry_type((DENTRY_T
*)ext_ep
) == TYPE_EXTEND
) &&
889 (ext_ep
->checksum
== chksum
)) {
891 if (ext_ep
->order
> MSDOS_LAST_LFN
)
903 * Name Conversion Functions
905 static s32
__extract_uni_name_from_ext_entry(EXT_DENTRY_T
*ep
, u16
*uniname
, s32 order
)
909 for (i
= 0; i
< 5; i
++) {
910 *uniname
= get_unaligned_le16(&(ep
->unicode_0_4
[i
<<1]));
918 for (i
= 0; i
< 6; i
++) {
919 /* FIXME : unaligned? */
920 *uniname
= le16_to_cpu(ep
->unicode_5_10
[i
]);
927 for (i
= 0; i
< 4; i
++) {
928 /* FIXME : unaligned? */
929 *uniname
= le16_to_cpu(ep
->unicode_5_10
[i
]);
935 *uniname
= 0x0; /* uniname[MAX_NAME_LENGTH] */
939 for (i
= 0; i
< 2; i
++) {
940 /* FIXME : unaligned? */
941 *uniname
= le16_to_cpu(ep
->unicode_11_12
[i
]);
951 } /* end of __extract_uni_name_from_ext_entry */
953 static void fat_get_uniname_from_ext_entry(struct super_block
*sb
, CHAIN_T
*p_dir
, s32 entry
, u16
*uniname
)
959 DOS_DENTRY_T
*dos_ep
=
960 (DOS_DENTRY_T
*)get_dentry_in_dir(sb
, p_dir
, entry
, NULL
);
962 if (unlikely(!dos_ep
))
965 chksum
= (u32
)calc_chksum_1byte(
966 (void *) dos_ep
->name
,
969 for (entry
--, i
= 1; entry
>= 0; entry
--, i
++) {
972 ep
= (EXT_DENTRY_T
*)get_dentry_in_dir(sb
, p_dir
, entry
, NULL
);
976 if (fat_get_entry_type((DENTRY_T
*) ep
) != TYPE_EXTEND
)
979 if (chksum
!= (u32
)ep
->checksum
)
982 if (i
!= (u32
)(ep
->order
& ~(MSDOS_LAST_LFN
)))
985 __extract_uni_name_from_ext_entry(ep
, name
, (s32
)i
);
986 if (ep
->order
& MSDOS_LAST_LFN
)
993 } /* end of fat_get_uniname_from_ext_entry */
995 /* Find if the shortname exists
996 * and check if there are free entries
998 static s32
__fat_find_shortname_entry(struct super_block
*sb
, CHAIN_T
*p_dir
,
999 u8
*p_dosname
, s32
*offset
, __attribute__((unused
))int n_entry_needed
)
1003 s32 dentries_per_clu
;
1004 DENTRY_T
*ep
= NULL
;
1005 DOS_DENTRY_T
*dos_ep
= NULL
;
1006 CHAIN_T clu
= *p_dir
;
1007 FS_INFO_T
*fsi
= &(SDFAT_SB(sb
)->fsi
);
1012 if (IS_CLUS_FREE(clu
.dir
)) /* FAT16 root_dir */
1013 dentries_per_clu
= fsi
->dentries_in_root
;
1015 dentries_per_clu
= fsi
->dentries_per_clu
;
1017 while (!IS_CLUS_EOF(clu
.dir
)) {
1018 for (i
= 0; i
< dentries_per_clu
; i
++, dentry
++) {
1019 ep
= get_dentry_in_dir(sb
, &clu
, i
, NULL
);
1023 type
= fat_get_entry_type(ep
);
1025 if ((type
== TYPE_FILE
) || (type
== TYPE_DIR
)) {
1026 dos_ep
= (DOS_DENTRY_T
*)ep
;
1027 if (!nls_cmp_sfn(sb
, p_dosname
, dos_ep
->name
)) {
1035 /* fat12/16 root dir */
1036 if (IS_CLUS_FREE(clu
.dir
))
1039 if (get_next_clus_safe(sb
, &clu
.dir
))
1045 #ifdef CONFIG_SDFAT_FAT32_SHORTNAME_SEQ
1046 static void __fat_attach_count_to_dos_name(u8
*dosname
, s32 count
)
1051 snprintf(str_count
, sizeof(str_count
), "~%d", count
);
1052 length
= strlen(str_count
);
1055 while (j
<= (8 - length
)) {
1057 if (dosname
[j
] == ' ')
1059 if (dosname
[j
] & 0x80)
1065 for (j
= 0; j
< length
; i
++, j
++)
1066 dosname
[i
] = (u8
) str_count
[j
];
1071 } /* end of __fat_attach_count_to_dos_name */
1074 s32
fat_generate_dos_name_new(struct super_block
*sb
, CHAIN_T
*p_dir
, DOS_NAME_T
*p_dosname
, s32 n_entry_needed
)
1078 u8 work
[DOS_NAME_LENGTH
], buf
[5];
1082 memset(work
, ' ', DOS_NAME_LENGTH
);
1083 memcpy(work
, p_dosname
->name
, DOS_NAME_LENGTH
);
1085 while (baselen
&& (work
[--baselen
] == ' ')) {
1086 /* DO NOTHING, JUST FOR CHECK_PATCH */
1092 BUG_ON(baselen
< 0);
1094 #ifdef CONFIG_SDFAT_FAT32_SHORTNAME_SEQ
1095 /* example) namei_exfat.c -> NAMEI_~1 - NAMEI_~9 */
1096 work
[baselen
] = '~';
1097 for (i
= 1; i
< 10; i
++) {
1098 // '0' + i = 1 ~ 9 ASCII
1099 work
[baselen
+ 1] = '0' + i
;
1100 err
= __fat_find_shortname_entry(sb
, p_dir
, work
, NULL
, n_entry_needed
);
1101 if (err
== -ENOENT
) {
1103 __fat_attach_count_to_dos_name(p_dosname
->name
, i
);
1107 /* any other error */
1114 tail
= (jiffies
>> 16) & 0x7;
1119 BUG_ON(baselen
< 0);
1121 work
[baselen
+ 4] = '~';
1123 work
[baselen
+ 5] = '1' + tail
;
1125 snprintf(buf
, sizeof(buf
), "%04X", i
& 0xffff);
1126 memcpy(&work
[baselen
], buf
, 4);
1127 err
= __fat_find_shortname_entry(sb
, p_dir
, work
, NULL
, n_entry_needed
);
1128 if (err
== -ENOENT
) {
1129 memcpy(p_dosname
->name
, work
, DOS_NAME_LENGTH
);
1133 /* any other error */
1140 } /* end of generate_dos_name_new */
1142 static s32
fat_calc_num_entries(UNI_NAME_T
*p_uniname
)
1146 len
= p_uniname
->name_len
;
1150 /* 1 dos name entry + extended entries */
1151 return((len
-1) / 13 + 2);
1153 } /* end of calc_num_enties */
1155 static s32
fat_check_max_dentries(FILE_ID_T
*fid
)
1157 if ((fid
->size
>> DENTRY_SIZE_BITS
) >= MAX_FAT_DENTRIES
) {
1158 /* FAT spec allows a dir to grow upto 65536 dentries */
1162 } /* end of check_max_dentries */
1166 * File Operation Functions
1168 static FS_FUNC_T fat_fs_func
= {
1169 .alloc_cluster
= fat_alloc_cluster
,
1170 .free_cluster
= fat_free_cluster
,
1171 .count_used_clusters
= fat_count_used_clusters
,
1173 .init_dir_entry
= fat_init_dir_entry
,
1174 .init_ext_entry
= fat_init_ext_entry
,
1175 .find_dir_entry
= fat_find_dir_entry
,
1176 .delete_dir_entry
= fat_delete_dir_entry
,
1177 .get_uniname_from_ext_entry
= fat_get_uniname_from_ext_entry
,
1178 .count_ext_entries
= fat_count_ext_entries
,
1179 .calc_num_entries
= fat_calc_num_entries
,
1180 .check_max_dentries
= fat_check_max_dentries
,
1182 .get_entry_type
= fat_get_entry_type
,
1183 .set_entry_type
= fat_set_entry_type
,
1184 .get_entry_attr
= fat_get_entry_attr
,
1185 .set_entry_attr
= fat_set_entry_attr
,
1186 .get_entry_flag
= fat_get_entry_flag
,
1187 .set_entry_flag
= fat_set_entry_flag
,
1188 .get_entry_clu0
= fat_get_entry_clu0
,
1189 .set_entry_clu0
= fat_set_entry_clu0
,
1190 .get_entry_size
= fat_get_entry_size
,
1191 .set_entry_size
= fat_set_entry_size
,
1192 .get_entry_time
= fat_get_entry_time
,
1193 .set_entry_time
= fat_set_entry_time
,
1196 static FS_FUNC_T amap_fat_fs_func
= {
1197 .alloc_cluster
= amap_fat_alloc_cluster
,
1198 .free_cluster
= fat_free_cluster
,
1199 .count_used_clusters
= fat_count_used_clusters
,
1201 .init_dir_entry
= fat_init_dir_entry
,
1202 .init_ext_entry
= fat_init_ext_entry
,
1203 .find_dir_entry
= fat_find_dir_entry
,
1204 .delete_dir_entry
= fat_delete_dir_entry
,
1205 .get_uniname_from_ext_entry
= fat_get_uniname_from_ext_entry
,
1206 .count_ext_entries
= fat_count_ext_entries
,
1207 .calc_num_entries
= fat_calc_num_entries
,
1208 .check_max_dentries
= fat_check_max_dentries
,
1210 .get_entry_type
= fat_get_entry_type
,
1211 .set_entry_type
= fat_set_entry_type
,
1212 .get_entry_attr
= fat_get_entry_attr
,
1213 .set_entry_attr
= fat_set_entry_attr
,
1214 .get_entry_flag
= fat_get_entry_flag
,
1215 .set_entry_flag
= fat_set_entry_flag
,
1216 .get_entry_clu0
= fat_get_entry_clu0
,
1217 .set_entry_clu0
= fat_set_entry_clu0
,
1218 .get_entry_size
= fat_get_entry_size
,
1219 .set_entry_size
= fat_set_entry_size
,
1220 .get_entry_time
= fat_get_entry_time
,
1221 .set_entry_time
= fat_set_entry_time
,
1223 .get_au_stat
= amap_get_au_stat
,
1226 s32
mount_fat16(struct super_block
*sb
, pbr_t
*p_pbr
)
1228 s32 num_reserved
, num_root_sectors
;
1229 bpb16_t
*p_bpb
= &(p_pbr
->bpb
.f16
);
1230 FS_INFO_T
*fsi
= &(SDFAT_SB(sb
)->fsi
);
1232 if (!p_bpb
->num_fats
) {
1233 sdfat_msg(sb
, KERN_ERR
, "bogus number of FAT structure");
1237 num_root_sectors
= get_unaligned_le16(p_bpb
->num_root_entries
) << DENTRY_SIZE_BITS
;
1238 num_root_sectors
= ((num_root_sectors
-1) >> sb
->s_blocksize_bits
) + 1;
1240 fsi
->sect_per_clus
= p_bpb
->sect_per_clus
;
1241 fsi
->sect_per_clus_bits
= ilog2(p_bpb
->sect_per_clus
);
1242 fsi
->cluster_size_bits
= fsi
->sect_per_clus_bits
+ sb
->s_blocksize_bits
;
1243 fsi
->cluster_size
= 1 << fsi
->cluster_size_bits
;
1245 fsi
->num_FAT_sectors
= le16_to_cpu(p_bpb
->num_fat_sectors
);
1247 fsi
->FAT1_start_sector
= le16_to_cpu(p_bpb
->num_reserved
);
1248 if (p_bpb
->num_fats
== 1)
1249 fsi
->FAT2_start_sector
= fsi
->FAT1_start_sector
;
1251 fsi
->FAT2_start_sector
= fsi
->FAT1_start_sector
+ fsi
->num_FAT_sectors
;
1253 fsi
->root_start_sector
= fsi
->FAT2_start_sector
+ fsi
->num_FAT_sectors
;
1254 fsi
->data_start_sector
= fsi
->root_start_sector
+ num_root_sectors
;
1256 fsi
->num_sectors
= get_unaligned_le16(p_bpb
->num_sectors
);
1257 if (!fsi
->num_sectors
)
1258 fsi
->num_sectors
= le32_to_cpu(p_bpb
->num_huge_sectors
);
1260 if (!fsi
->num_sectors
) {
1261 sdfat_msg(sb
, KERN_ERR
, "bogus number of total sector count");
1265 num_reserved
= fsi
->data_start_sector
;
1266 fsi
->num_clusters
= ((fsi
->num_sectors
- num_reserved
) >> fsi
->sect_per_clus_bits
) + CLUS_BASE
;
1267 /* because the cluster index starts with 2 */
1269 fsi
->vol_type
= FAT16
;
1270 if (fsi
->num_clusters
< FAT12_THRESHOLD
)
1271 fsi
->vol_type
= FAT12
;
1273 fsi
->vol_id
= get_unaligned_le32(p_bpb
->vol_serial
);
1276 fsi
->dentries_in_root
= get_unaligned_le16(p_bpb
->num_root_entries
);
1277 if (!fsi
->dentries_in_root
) {
1278 sdfat_msg(sb
, KERN_ERR
, "bogus number of max dentry count "
1279 "of the root directory");
1283 fsi
->dentries_per_clu
= 1 << (fsi
->cluster_size_bits
- DENTRY_SIZE_BITS
);
1285 fsi
->vol_flag
= VOL_CLEAN
;
1286 fsi
->clu_srch_ptr
= 2;
1287 fsi
->used_clusters
= (u32
) ~0;
1289 fsi
->fs_func
= &fat_fs_func
;
1290 fat_ent_ops_init(sb
);
1292 if (p_bpb
->state
& FAT_VOL_DIRTY
) {
1293 fsi
->vol_flag
|= VOL_DIRTY
;
1294 sdfat_log_msg(sb
, KERN_WARNING
, "Volume was not properly "
1295 "unmounted. Some data may be corrupt. "
1296 "Please run fsck.");
1300 } /* end of mount_fat16 */
1302 static sector_t
__calc_hidden_sect(struct super_block
*sb
)
1304 struct block_device
*bdev
= sb
->s_bdev
;
1305 sector_t hidden
= 0;
1310 hidden
= bdev
->bd_part
->start_sect
;
1311 /* a disk device, not a partition */
1313 ASSERT(bdev
== bdev
->bd_contains
);
1314 ASSERT(!bdev
->bd_part
->partno
);
1318 if (sb
->s_blocksize_bits
!= 9) {
1319 ASSERT(sb
->s_blocksize_bits
> 9);
1320 hidden
>>= (sb
->s_blocksize_bits
- 9);
1324 sdfat_log_msg(sb
, KERN_INFO
, "start_sect of partition : %lld",
1330 s32
mount_fat32(struct super_block
*sb
, pbr_t
*p_pbr
)
1333 pbr32_t
*p_bpb
= (pbr32_t
*)p_pbr
;
1334 FS_INFO_T
*fsi
= &(SDFAT_SB(sb
)->fsi
);
1336 if (!p_bpb
->bpb
.num_fats
) {
1337 sdfat_msg(sb
, KERN_ERR
, "bogus number of FAT structure");
1341 fsi
->sect_per_clus
= p_bpb
->bpb
.sect_per_clus
;
1342 fsi
->sect_per_clus_bits
= ilog2(p_bpb
->bpb
.sect_per_clus
);
1343 fsi
->cluster_size_bits
= fsi
->sect_per_clus_bits
+ sb
->s_blocksize_bits
;
1344 fsi
->cluster_size
= 1 << fsi
->cluster_size_bits
;
1346 fsi
->num_FAT_sectors
= le32_to_cpu(p_bpb
->bpb
.num_fat32_sectors
);
1348 fsi
->FAT1_start_sector
= le16_to_cpu(p_bpb
->bpb
.num_reserved
);
1349 if (p_bpb
->bpb
.num_fats
== 1)
1350 fsi
->FAT2_start_sector
= fsi
->FAT1_start_sector
;
1352 fsi
->FAT2_start_sector
= fsi
->FAT1_start_sector
+ fsi
->num_FAT_sectors
;
1354 fsi
->root_start_sector
= fsi
->FAT2_start_sector
+ fsi
->num_FAT_sectors
;
1355 fsi
->data_start_sector
= fsi
->root_start_sector
;
1357 /* SPEC violation for compatibility */
1358 fsi
->num_sectors
= get_unaligned_le16(p_bpb
->bpb
.num_sectors
);
1359 if (!fsi
->num_sectors
)
1360 fsi
->num_sectors
= le32_to_cpu(p_bpb
->bpb
.num_huge_sectors
);
1363 if (!fsi
->num_sectors
) {
1364 sdfat_msg(sb
, KERN_ERR
, "bogus number of total sector count");
1368 num_reserved
= fsi
->data_start_sector
;
1370 fsi
->num_clusters
= ((fsi
->num_sectors
-num_reserved
) >> fsi
->sect_per_clus_bits
) + 2;
1371 /* because the cluster index starts with 2 */
1373 fsi
->vol_type
= FAT32
;
1374 fsi
->vol_id
= get_unaligned_le32(p_bpb
->bsx
.vol_serial
);
1376 fsi
->root_dir
= le32_to_cpu(p_bpb
->bpb
.root_cluster
);
1377 fsi
->dentries_in_root
= 0;
1378 fsi
->dentries_per_clu
= 1 << (fsi
->cluster_size_bits
- DENTRY_SIZE_BITS
);
1380 fsi
->vol_flag
= VOL_CLEAN
;
1381 fsi
->clu_srch_ptr
= 2;
1382 fsi
->used_clusters
= (u32
) ~0;
1384 fsi
->fs_func
= &fat_fs_func
;
1386 /* Delayed / smart allocation related init */
1387 fsi
->reserved_clusters
= 0;
1389 /* Should be initialized before calling amap_create() */
1390 fat_ent_ops_init(sb
);
1392 /* AU Map Creation */
1393 if (SDFAT_SB(sb
)->options
.improved_allocation
& SDFAT_ALLOC_SMART
) {
1394 u32 hidden_sectors
= le32_to_cpu(p_bpb
->bpb
.num_hid_sectors
);
1395 u32 calc_hid_sect
= 0;
1399 /* calculate hidden sector size */
1400 calc_hid_sect
= __calc_hidden_sect(sb
);
1401 if (calc_hid_sect
!= hidden_sectors
) {
1402 sdfat_log_msg(sb
, KERN_WARNING
, "abnormal hidden "
1403 "sector : bpb(%u) != ondisk(%u)",
1404 hidden_sectors
, calc_hid_sect
);
1405 if (SDFAT_SB(sb
)->options
.adj_hidsect
) {
1406 sdfat_log_msg(sb
, KERN_INFO
,
1407 "adjustment hidden sector : "
1408 "bpb(%u) -> ondisk(%u)",
1409 hidden_sectors
, calc_hid_sect
);
1410 hidden_sectors
= calc_hid_sect
;
1414 SDFAT_SB(sb
)->options
.amap_opt
.misaligned_sect
= hidden_sectors
;
1416 /* calculate AU size if it's not set */
1417 if (!SDFAT_SB(sb
)->options
.amap_opt
.sect_per_au
) {
1418 SDFAT_SB(sb
)->options
.amap_opt
.sect_per_au
=
1419 __calc_default_au_size(sb
);
1422 ret
= amap_create(sb
,
1423 SDFAT_SB(sb
)->options
.amap_opt
.pack_ratio
,
1424 SDFAT_SB(sb
)->options
.amap_opt
.sect_per_au
,
1425 SDFAT_SB(sb
)->options
.amap_opt
.misaligned_sect
);
1427 sdfat_log_msg(sb
, KERN_WARNING
, "failed to create AMAP."
1428 " disabling smart allocation. (err:%d)", ret
);
1429 SDFAT_SB(sb
)->options
.improved_allocation
&= ~(SDFAT_ALLOC_SMART
);
1431 fsi
->fs_func
= &amap_fat_fs_func
;
1435 /* Check dependency of mount options */
1436 if (SDFAT_SB(sb
)->options
.improved_allocation
!=
1437 (SDFAT_ALLOC_DELAY
| SDFAT_ALLOC_SMART
)) {
1438 sdfat_log_msg(sb
, KERN_INFO
, "disabling defragmentation because"
1439 " smart, delay options are disabled");
1440 SDFAT_SB(sb
)->options
.defrag
= 0;
1443 if (p_bpb
->bsx
.state
& FAT_VOL_DIRTY
) {
1444 fsi
->vol_flag
|= VOL_DIRTY
;
1445 sdfat_log_msg(sb
, KERN_WARNING
, "Volume was not properly "
1446 "unmounted. Some data may be corrupt. "
1447 "Please run fsck.");
1451 } /* end of mount_fat32 */
1453 /* end of core_fat.c */