fs: Add sdfat
[GitHub/LineageOS/android_kernel_samsung_universal7580.git] / fs / sdfat / core_fat.c
1 /*
2 * Copyright (C) 2012-2013 Samsung Electronics Co., Ltd.
3 *
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.
8 *
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.
13 *
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/>.
16 */
17
18 /************************************************************************/
19 /* */
20 /* PROJECT : exFAT & FAT12/16/32 File System */
21 /* FILE : core_fat.c */
22 /* PURPOSE : FAT-fs core code for sdFAT */
23 /* */
24 /*----------------------------------------------------------------------*/
25 /* NOTES */
26 /* */
27 /* */
28 /************************************************************************/
29
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>
35
36 #include "sdfat.h"
37 #include "core.h"
38 #include <asm/byteorder.h>
39 #include <asm/unaligned.h>
40
41 /*----------------------------------------------------------------------*/
42 /* Constant & Macro Definitions */
43 /*----------------------------------------------------------------------*/
44 #define MAX_LFN_ORDER (20)
45
46 /*
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.
51 */
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 */
55 #else
56 #define MAX_EST_AU_SECT (32768) /* upto 16MB */
57 #endif
58
59 /*======================================================================*/
60 /* Local Function Declarations */
61 /*======================================================================*/
62 static s32 __extract_uni_name_from_ext_entry(EXT_DENTRY_T *, u16 *, s32);
63
64 /*----------------------------------------------------------------------*/
65 /* Global Variable Definitions */
66 /*----------------------------------------------------------------------*/
67
68 /*----------------------------------------------------------------------*/
69 /* Local Variable Definitions */
70 /*----------------------------------------------------------------------*/
71
72 /*======================================================================*/
73 /* Local Function Definitions */
74 /*======================================================================*/
75 static u32 __calc_default_au_size(struct super_block *sb)
76 {
77 struct block_device *bdev = sb->s_bdev;
78 struct gendisk *disk;
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;
85
86 /* we assumed that sector size is 512 bytes */
87
88 disk = bdev->bd_disk;
89 if (!disk)
90 goto out;
91
92 queue = disk->queue;
93 if (!queue)
94 goto out;
95
96 limit = &queue->limits;
97 queue_au_size = limit->discard_granularity;
98
99 /* estimate function(x) =
100 * (total_sect / 2) * 512 / 1024
101 * => (total_sect >> 1) >> 1)
102 * => (total_sect >> 2)
103 * => estimated bytes size
104 *
105 * ex1) <= 8GB -> 4MB
106 * ex2) 16GB -> 8MB
107 * ex3) >= 32GB -> 16MB
108 */
109 total_sect = disk->part0.nr_sects;
110 est_au_size = total_sect >> 2;
111
112 /* au_size assumed that bytes per sector is 512 */
113 est_au_sect = est_au_size >> 9;
114
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);
117
118 if (est_au_sect <= 8192) {
119 /* 4MB */
120 est_au_sect = 8192;
121 } else if (est_au_sect <= 16384) {
122 /* 8MB */
123 est_au_sect = 16384;
124 } else {
125 /* 8MB or 16MB */
126 est_au_sect = MAX_EST_AU_SECT;
127 }
128
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);
131
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;
137 }
138
139 out:
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)",
144 sb->s_blocksize);
145 est_au_sect >>= (sb->s_blocksize_bits - 9);
146 }
147
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));
151 return est_au_sect;
152 }
153
154
155 /*
156 * Cluster Management Functions
157 */
158 static s32 fat_alloc_cluster(struct super_block *sb, s32 num_alloc, CHAIN_T *p_chain, int dest)
159 {
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);
163
164
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)
169 new_clu = 2;
170
171 set_sb_dirty(sb);
172
173 p_chain->dir = CLUS_EOF;
174
175 for (i = CLUS_BASE; i < fsi->num_clusters; i++) {
176 if (fat_ent_get(sb, new_clu, &read_clu))
177 return -EIO;
178
179 if (IS_CLUS_FREE(read_clu)) {
180 if (fat_ent_set(sb, new_clu, CLUS_EOF))
181 return -EIO;
182 num_clusters++;
183
184 if (IS_CLUS_EOF(p_chain->dir)) {
185 p_chain->dir = new_clu;
186 } else {
187 if (fat_ent_set(sb, last_clu, new_clu))
188 return -EIO;
189 }
190
191 last_clu = new_clu;
192
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;
197
198 return num_clusters;
199 }
200 }
201 if ((++new_clu) >= fsi->num_clusters)
202 new_clu = CLUS_BASE;
203 }
204
205 fsi->clu_srch_ptr = new_clu;
206 if (fsi->used_clusters != (u32) ~0)
207 fsi->used_clusters += num_clusters;
208
209 return num_clusters;
210 } /* end of fat_alloc_cluster */
211
212 static s32 fat_free_cluster(struct super_block *sb, CHAIN_T *p_chain, s32 do_relse)
213 {
214 s32 ret = -EIO;
215 s32 num_clusters = 0;
216 u32 clu, prev;
217 FS_INFO_T *fsi = &(SDFAT_SB(sb)->fsi);
218 s32 i;
219 u32 sector;
220
221 /* invalid cluster number */
222 if (IS_CLUS_FREE(p_chain->dir) || IS_CLUS_EOF(p_chain->dir))
223 return 0;
224
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);
229 return 0;
230 }
231
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);
236 return -EIO;
237 }
238
239
240 set_sb_dirty(sb);
241 clu = p_chain->dir;
242
243 do {
244 if (do_relse) {
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)
248 goto out;
249 }
250 }
251
252 prev = clu;
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)",
258 __func__, prev);
259 } else if (IS_CLUS_FREE(clu)) {
260 sdfat_log_msg(sb, KERN_ERR, "%s : "
261 "deleting free cluster (clu[%u]->FREE)",
262 __func__, prev);
263 }
264 goto out;
265 }
266
267 /* Free FAT chain */
268 if (fat_ent_set(sb, prev, CLUS_FREE))
269 goto out;
270
271 /* Update AMAP if needed */
272 if (fsi->amap) {
273 if (amap_release_cluster(sb, prev))
274 return -EIO;
275 }
276
277 num_clusters++;
278
279 } while (!IS_CLUS_EOF(clu));
280
281 /* success */
282 ret = 0;
283 out:
284 if (fsi->used_clusters != (u32) ~0)
285 fsi->used_clusters -= num_clusters;
286 return ret;
287 } /* end of fat_free_cluster */
288
289 static s32 fat_count_used_clusters(struct super_block *sb, u32 *ret_count)
290 {
291 s32 i;
292 u32 clu, count = 0;
293 FS_INFO_T *fsi = &(SDFAT_SB(sb)->fsi);
294
295 for (i = CLUS_BASE; i < fsi->num_clusters; i++) {
296 if (fat_ent_get(sb, i, &clu))
297 return -EIO;
298
299 if (!IS_CLUS_FREE(clu))
300 count++;
301 }
302
303 *ret_count = count;
304 return 0;
305 } /* end of fat_count_used_clusters */
306
307
308 /*
309 * Directory Entry Management Functions
310 */
311 static u32 fat_get_entry_type(DENTRY_T *p_entry)
312 {
313 DOS_DENTRY_T *ep = (DOS_DENTRY_T *)p_entry;
314
315 /* first byte of 32bytes dummy */
316 if (*(ep->name) == MSDOS_UNUSED)
317 return TYPE_UNUSED;
318
319 /* 0xE5 of Kanji Japanese is replaced to 0x05 */
320 else if (*(ep->name) == MSDOS_DELETED)
321 return TYPE_DELETED;
322
323 /* 11th byte of 32bytes dummy */
324 else if ((ep->attr & ATTR_EXTEND_MASK) == ATTR_EXTEND)
325 return TYPE_EXTEND;
326
327 else if (!(ep->attr & (ATTR_SUBDIR | ATTR_VOLUME)))
328 return TYPE_FILE;
329
330 else if ((ep->attr & (ATTR_SUBDIR | ATTR_VOLUME)) == ATTR_SUBDIR)
331 return TYPE_DIR;
332
333 else if ((ep->attr & (ATTR_SUBDIR | ATTR_VOLUME)) == ATTR_VOLUME)
334 return TYPE_VOLUME;
335
336 return TYPE_INVALID;
337 } /* end of fat_get_entry_type */
338
339 static void fat_set_entry_type(DENTRY_T *p_entry, u32 type)
340 {
341 DOS_DENTRY_T *ep = (DOS_DENTRY_T *)p_entry;
342
343 if (type == TYPE_UNUSED)
344 *(ep->name) = MSDOS_UNUSED; /* 0x0 */
345
346 else if (type == TYPE_DELETED)
347 *(ep->name) = MSDOS_DELETED; /* 0xE5 */
348
349 else if (type == TYPE_EXTEND)
350 ep->attr = ATTR_EXTEND;
351
352 else if (type == TYPE_DIR)
353 ep->attr = ATTR_SUBDIR;
354
355 else if (type == TYPE_FILE)
356 ep->attr = ATTR_ARCHIVE;
357
358 else if (type == TYPE_SYMLINK)
359 ep->attr = ATTR_ARCHIVE | ATTR_SYMLINK;
360 } /* end of fat_set_entry_type */
361
362 static u32 fat_get_entry_attr(DENTRY_T *p_entry)
363 {
364 DOS_DENTRY_T *ep = (DOS_DENTRY_T *)p_entry;
365
366 return (u32)ep->attr;
367 } /* end of fat_get_entry_attr */
368
369 static void fat_set_entry_attr(DENTRY_T *p_entry, u32 attr)
370 {
371 DOS_DENTRY_T *ep = (DOS_DENTRY_T *)p_entry;
372
373 ep->attr = (u8)attr;
374 } /* end of fat_set_entry_attr */
375
376 static u8 fat_get_entry_flag(DENTRY_T *p_entry)
377 {
378 return 0x01;
379 } /* end of fat_get_entry_flag */
380
381 static void fat_set_entry_flag(DENTRY_T *p_entry, u8 flags)
382 {
383 } /* end of fat_set_entry_flag */
384
385 static u32 fat_get_entry_clu0(DENTRY_T *p_entry)
386 {
387 DOS_DENTRY_T *ep = (DOS_DENTRY_T *)p_entry;
388 /* FIXME : is ok? */
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 */
391
392 static void fat_set_entry_clu0(DENTRY_T *p_entry, u32 start_clu)
393 {
394 DOS_DENTRY_T *ep = (DOS_DENTRY_T *)p_entry;
395
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 */
399
400 static u64 fat_get_entry_size(DENTRY_T *p_entry)
401 {
402 DOS_DENTRY_T *ep = (DOS_DENTRY_T *)p_entry;
403
404 return (u64)le32_to_cpu(ep->size);
405 } /* end of fat_get_entry_size */
406
407 static void fat_set_entry_size(DENTRY_T *p_entry, u64 size)
408 {
409 DOS_DENTRY_T *ep = (DOS_DENTRY_T *)p_entry;
410
411 ep->size = cpu_to_le32((u32)size);
412 } /* end of fat_set_entry_size */
413
414 static void fat_get_entry_time(DENTRY_T *p_entry, TIMESTAMP_T *tp, u8 mode)
415 {
416 u16 t = 0x00, d = 0x21;
417 DOS_DENTRY_T *ep = (DOS_DENTRY_T *) p_entry;
418
419 switch (mode) {
420 case TM_CREATE:
421 t = le16_to_cpu(ep->create_time);
422 d = le16_to_cpu(ep->create_date);
423 break;
424 case TM_MODIFY:
425 t = le16_to_cpu(ep->modify_time);
426 d = le16_to_cpu(ep->modify_date);
427 break;
428 }
429
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;
435 tp->year = (d >> 9);
436 } /* end of fat_get_entry_time */
437
438 static void fat_set_entry_time(DENTRY_T *p_entry, TIMESTAMP_T *tp, u8 mode)
439 {
440 u16 t, d;
441 DOS_DENTRY_T *ep = (DOS_DENTRY_T *) p_entry;
442
443 t = (tp->hour << 11) | (tp->min << 5) | (tp->sec >> 1);
444 d = (tp->year << 9) | (tp->mon << 5) | tp->day;
445
446 switch (mode) {
447 case TM_CREATE:
448 ep->create_time = cpu_to_le16(t);
449 ep->create_date = cpu_to_le16(d);
450 break;
451 case TM_MODIFY:
452 ep->modify_time = cpu_to_le16(t);
453 ep->modify_date = cpu_to_le16(d);
454 break;
455 }
456 } /* end of fat_set_entry_time */
457
458 static void __init_dos_entry(struct super_block *sb, DOS_DENTRY_T *ep, u32 type, u32 start_clu)
459 {
460 TIMESTAMP_T tm, *tp;
461
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));
465 ep->size = 0;
466
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);
470 ep->access_date = 0;
471 ep->create_time_ms = 0;
472 } /* end of __init_dos_entry */
473
474 static void __init_ext_entry(EXT_DENTRY_T *ep, s32 order, u8 chksum, u16 *uniname)
475 {
476 s32 i;
477 u8 end = false;
478
479 fat_set_entry_type((DENTRY_T *) ep, TYPE_EXTEND);
480 ep->order = (u8) order;
481 ep->sysid = 0;
482 ep->checksum = chksum;
483 ep->start_clu = 0;
484
485 /* unaligned name */
486 for (i = 0; i < 5; i++) {
487 if (!end) {
488 put_unaligned_le16(*uniname, &(ep->unicode_0_4[i<<1]));
489 if (*uniname == 0x0)
490 end = true;
491 else
492 uniname++;
493 } else {
494 put_unaligned_le16(0xFFFF, &(ep->unicode_0_4[i<<1]));
495 }
496 }
497
498 /* aligned name */
499 for (i = 0; i < 6; i++) {
500 if (!end) {
501 ep->unicode_5_10[i] = cpu_to_le16(*uniname);
502 if (*uniname == 0x0)
503 end = true;
504 else
505 uniname++;
506 } else {
507 ep->unicode_5_10[i] = cpu_to_le16(0xFFFF);
508 }
509 }
510
511 /* aligned name */
512 for (i = 0; i < 2; i++) {
513 if (!end) {
514 ep->unicode_11_12[i] = cpu_to_le16(*uniname);
515 if (*uniname == 0x0)
516 end = true;
517 else
518 uniname++;
519 } else {
520 ep->unicode_11_12[i] = cpu_to_le16(0xFFFF);
521 }
522 }
523 } /* end of __init_ext_entry */
524
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)
527 {
528 u32 sector;
529 DOS_DENTRY_T *dos_ep;
530
531 dos_ep = (DOS_DENTRY_T *) get_dentry_in_dir(sb, p_dir, entry, &sector);
532 if (!dos_ep)
533 return -EIO;
534
535 __init_dos_entry(sb, dos_ep, type, start_clu);
536 dcache_modify(sb, sector);
537
538 return 0;
539 } /* end of fat_init_dir_entry */
540
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)
543 {
544 s32 i;
545 u32 sector;
546 u8 chksum;
547 u16 *uniname = p_uniname->name;
548 DOS_DENTRY_T *dos_ep;
549 EXT_DENTRY_T *ext_ep;
550
551 dos_ep = (DOS_DENTRY_T *) get_dentry_in_dir(sb, p_dir, entry, &sector);
552 if (!dos_ep)
553 return -EIO;
554
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))
558 return -EIO;
559
560 if ((--num_entries) > 0) {
561 chksum = calc_chksum_1byte((void *) dos_ep->name, DOS_NAME_LENGTH, 0);
562
563 for (i = 1; i < num_entries; i++) {
564 ext_ep = (EXT_DENTRY_T *) get_dentry_in_dir(sb, p_dir, entry-i, &sector);
565 if (!ext_ep)
566 return -EIO;
567
568 __init_ext_entry(ext_ep, i, chksum, uniname);
569 if (dcache_modify(sb, sector))
570 return -EIO;
571 uniname += 13;
572 }
573
574 ext_ep = (EXT_DENTRY_T *) get_dentry_in_dir(sb, p_dir, entry-i, &sector);
575 if (!ext_ep)
576 return -EIO;
577
578 __init_ext_entry(ext_ep, i+MSDOS_LAST_LFN, chksum, uniname);
579 if (dcache_modify(sb, sector))
580 return -EIO;
581 }
582
583 return 0;
584 } /* end of fat_init_ext_entry */
585
586 static s32 fat_delete_dir_entry(struct super_block *sb, CHAIN_T *p_dir, s32 entry, s32 order, s32 num_entries)
587 {
588 s32 i;
589 u32 sector;
590 DENTRY_T *ep;
591
592 for (i = num_entries-1; i >= order; i--) {
593 ep = get_dentry_in_dir(sb, p_dir, entry-i, &sector);
594 if (!ep)
595 return -EIO;
596
597 fat_set_entry_type(ep, TYPE_DELETED);
598 if (dcache_modify(sb, sector))
599 return -EIO;
600 }
601
602 return 0;
603 }
604
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
609 * -EIO : I/O error
610 */
611 static inline s32 __get_dentries_per_clu(FS_INFO_T *fsi, s32 clu)
612 {
613 if (IS_CLUS_FREE(clu)) /* FAT16 root_dir */
614 return fsi->dentries_in_root;
615
616 return fsi->dentries_per_clu;
617 }
618
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)
621 {
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;
625 u32 entry_type;
626 u16 entry_uniname[14], *uniname = NULL;
627 CHAIN_T clu;
628 DENTRY_T *ep;
629 HINT_T *hint_stat = &fid->hint_stat;
630 HINT_FEMP_T candi_empty;
631 FS_INFO_T *fsi = &(SDFAT_SB(sb)->fsi);
632
633 /*
634 * REMARK:
635 * DOT and DOTDOT are handled by VFS layer
636 */
637
638 dentries_per_clu = __get_dentries_per_clu(fsi, p_dir->dir);
639 clu.dir = p_dir->dir;
640 clu.flags = p_dir->flags;
641
642 if (hint_stat->eidx) {
643 clu.dir = hint_stat->clu;
644 dentry = hint_stat->eidx;
645 end_eidx = dentry;
646 }
647
648 candi_empty.eidx = -1;
649
650 MMSG("lookup dir= %s\n", p_dosname->name);
651 rewind:
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))
656 goto not_found;
657
658 ep = get_dentry_in_dir(sb, &clu, i, NULL);
659 if (!ep)
660 return -EIO;
661
662 entry_type = fat_get_entry_type(ep);
663
664 /*
665 * Most directory entries have long name,
666 * So, we check extend directory entry first.
667 */
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;
672 s32 len = 13;
673 u16 unichar;
674
675 num_empty = 0;
676 candi_empty.eidx = -1;
677
678 /* check whether new lfn or not */
679 if (cur_ord & MSDOS_LAST_LFN) {
680 cur_ord &= ~(MSDOS_LAST_LFN);
681 chksum = cur_chksum;
682 len = (13 * (cur_ord-1));
683 uniname = (p_uniname->name + len);
684 lfn_ord = cur_ord + 1;
685 lfn_len = 0;
686
687 /* check minimum name length */
688 if (cur_ord &&
689 (len > p_uniname->name_len)) {
690 /* MISMATCHED NAME LENGTH */
691 lfn_len = -1;
692 }
693 len = 0;
694 }
695
696 /* invalid lfn order */
697 if (!cur_ord || (cur_ord > MAX_LFN_ORDER) ||
698 ((cur_ord + 1) != lfn_ord))
699 goto reset_dentry_set;
700
701 /* check checksum of directory entry set */
702 if (cur_chksum != chksum)
703 goto reset_dentry_set;
704
705 /* update order for next dentry */
706 lfn_ord = cur_ord;
707
708 /* check whether mismatched lfn or not */
709 if (lfn_len == -1) {
710 /* MISMATCHED LFN DENTRY SET */
711 continue;
712 }
713
714 if (!uniname) {
715 sdfat_fs_error(sb,
716 "%s : abnormal dentry "
717 "(start_clu[%u], "
718 "idx[%u])", __func__,
719 p_dir->dir, dentry);
720 sdfat_debug_bug_on(1);
721 return -EIO;
722 }
723
724 /* update position of name buffer */
725 uniname -= len;
726
727 /* get utf16 characters saved on this entry */
728 len = __extract_uni_name_from_ext_entry(ext_ep, entry_uniname, lfn_ord);
729
730 /* replace last char to null */
731 unichar = *(uniname+len);
732 *(uniname+len) = (u16)0x0;
733
734 /* uniname ext_dentry unit compare repeatdly */
735 if (nls_cmp_uniname(sb, uniname, entry_uniname)) {
736 /* DO HANDLE WRONG NAME */
737 lfn_len = -1;
738 } else {
739 /* add matched chars length */
740 lfn_len += len;
741 }
742
743 /* restore previous character */
744 *(uniname+len) = unichar;
745
746 /* jump to check next dentry */
747 continue;
748
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,
753 DOS_NAME_LENGTH, 0);
754
755 num_empty = 0;
756 candi_empty.eidx = -1;
757
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],
764 dos_ep->name[10]);
765
766 /*
767 * if there is no valid long filename,
768 * we should check short filename.
769 */
770 if (!lfn_len || (cur_chksum != chksum)) {
771 /* check shortname */
772 if ((p_dosname->name[0] != '\0') &&
773 !nls_cmp_sfn(sb,
774 p_dosname->name,
775 dos_ep->name)) {
776 goto found;
777 }
778 /* check name length */
779 } else if ((lfn_len > 0) &&
780 ((s32)p_uniname->name_len ==
781 lfn_len)) {
782 goto found;
783 }
784
785 /* DO HANDLE MISMATCHED SFN, FALL THROUGH */
786 } else if ((entry_type == TYPE_UNUSED) || (entry_type == TYPE_DELETED)) {
787 num_empty++;
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;
793 }
794
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;
799
800 if ((fid->hint_femp.eidx == -1) ||
801 (candi_empty.eidx <= fid->hint_femp.eidx)) {
802 memcpy(&fid->hint_femp,
803 &candi_empty,
804 sizeof(HINT_FEMP_T));
805 }
806 }
807 }
808
809 if (entry_type == TYPE_UNUSED)
810 goto not_found;
811 /* FALL THROUGH */
812 }
813 reset_dentry_set:
814 /* TYPE_DELETED, TYPE_VOLUME OR MISMATCHED SFN */
815 lfn_ord = 0;
816 lfn_len = 0;
817 chksum = 0;
818 }
819
820 if (IS_CLUS_FREE(p_dir->dir))
821 break; /* FAT16 root_dir */
822
823 if (get_next_clus_safe(sb, &clu.dir))
824 return -EIO;
825 }
826
827 not_found:
828 /* we started at not 0 index,so we should try to find target
829 * from 0 index to the index we started at.
830 */
831 if (!rewind && end_eidx) {
832 rewind = 1;
833 dentry = 0;
834 clu.dir = p_dir->dir;
835 /* reset dentry set */
836 lfn_ord = 0;
837 lfn_len = 0;
838 chksum = 0;
839 /* reset empty hint_*/
840 num_empty = 0;
841 candi_empty.eidx = -1;
842 goto rewind;
843 }
844
845 /* initialized hint_stat */
846 hint_stat->clu = p_dir->dir;
847 hint_stat->eidx = 0;
848 return -ENOENT;
849
850 found:
851 /* next dentry we'll find is out of this cluster */
852 if (!((dentry + 1) % dentries_per_clu)) {
853 int ret = 0;
854 /* FAT16 root_dir */
855 if (IS_CLUS_FREE(p_dir->dir))
856 clu.dir = CLUS_EOF;
857 else
858 ret = get_next_clus_safe(sb, &clu.dir);
859
860 if (ret || IS_CLUS_EOF(clu.dir)) {
861 /* just initialized hint_stat */
862 hint_stat->clu = p_dir->dir;
863 hint_stat->eidx = 0;
864 return dentry;
865 }
866 }
867
868 hint_stat->clu = clu.dir;
869 hint_stat->eidx = dentry + 1;
870 return dentry;
871 } /* end of fat_find_dir_entry */
872
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)
875 {
876 s32 count = 0;
877 u8 chksum;
878 DOS_DENTRY_T *dos_ep = (DOS_DENTRY_T *) p_entry;
879 EXT_DENTRY_T *ext_ep;
880
881 chksum = calc_chksum_1byte((void *) dos_ep->name, DOS_NAME_LENGTH, 0);
882
883 for (entry--; entry >= 0; entry--) {
884 ext_ep = (EXT_DENTRY_T *)get_dentry_in_dir(sb, p_dir, entry, NULL);
885 if (!ext_ep)
886 return -EIO;
887
888 if ((fat_get_entry_type((DENTRY_T *)ext_ep) == TYPE_EXTEND) &&
889 (ext_ep->checksum == chksum)) {
890 count++;
891 if (ext_ep->order > MSDOS_LAST_LFN)
892 return count;
893 } else {
894 return count;
895 }
896 }
897
898 return count;
899 }
900
901
902 /*
903 * Name Conversion Functions
904 */
905 static s32 __extract_uni_name_from_ext_entry(EXT_DENTRY_T *ep, u16 *uniname, s32 order)
906 {
907 s32 i, len = 0;
908
909 for (i = 0; i < 5; i++) {
910 *uniname = get_unaligned_le16(&(ep->unicode_0_4[i<<1]));
911 if (*uniname == 0x0)
912 return len;
913 uniname++;
914 len++;
915 }
916
917 if (order < 20) {
918 for (i = 0; i < 6; i++) {
919 /* FIXME : unaligned? */
920 *uniname = le16_to_cpu(ep->unicode_5_10[i]);
921 if (*uniname == 0x0)
922 return len;
923 uniname++;
924 len++;
925 }
926 } else {
927 for (i = 0; i < 4; i++) {
928 /* FIXME : unaligned? */
929 *uniname = le16_to_cpu(ep->unicode_5_10[i]);
930 if (*uniname == 0x0)
931 return len;
932 uniname++;
933 len++;
934 }
935 *uniname = 0x0; /* uniname[MAX_NAME_LENGTH] */
936 return len;
937 }
938
939 for (i = 0; i < 2; i++) {
940 /* FIXME : unaligned? */
941 *uniname = le16_to_cpu(ep->unicode_11_12[i]);
942 if (*uniname == 0x0)
943 return len;
944 uniname++;
945 len++;
946 }
947
948 *uniname = 0x0;
949 return len;
950
951 } /* end of __extract_uni_name_from_ext_entry */
952
953 static void fat_get_uniname_from_ext_entry(struct super_block *sb, CHAIN_T *p_dir, s32 entry, u16 *uniname)
954 {
955 u32 i;
956 u16 *name = uniname;
957 u32 chksum;
958
959 DOS_DENTRY_T *dos_ep =
960 (DOS_DENTRY_T *)get_dentry_in_dir(sb, p_dir, entry, NULL);
961
962 if (unlikely(!dos_ep))
963 goto invalid_lfn;
964
965 chksum = (u32)calc_chksum_1byte(
966 (void *) dos_ep->name,
967 DOS_NAME_LENGTH, 0);
968
969 for (entry--, i = 1; entry >= 0; entry--, i++) {
970 EXT_DENTRY_T *ep;
971
972 ep = (EXT_DENTRY_T *)get_dentry_in_dir(sb, p_dir, entry, NULL);
973 if (!ep)
974 goto invalid_lfn;
975
976 if (fat_get_entry_type((DENTRY_T *) ep) != TYPE_EXTEND)
977 goto invalid_lfn;
978
979 if (chksum != (u32)ep->checksum)
980 goto invalid_lfn;
981
982 if (i != (u32)(ep->order & ~(MSDOS_LAST_LFN)))
983 goto invalid_lfn;
984
985 __extract_uni_name_from_ext_entry(ep, name, (s32)i);
986 if (ep->order & MSDOS_LAST_LFN)
987 return;
988
989 name += 13;
990 }
991 invalid_lfn:
992 *uniname = (u16)0x0;
993 } /* end of fat_get_uniname_from_ext_entry */
994
995 /* Find if the shortname exists
996 * and check if there are free entries
997 */
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)
1000 {
1001 u32 type;
1002 s32 i, dentry = 0;
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);
1008
1009 if (offset)
1010 *offset = -1;
1011
1012 if (IS_CLUS_FREE(clu.dir)) /* FAT16 root_dir */
1013 dentries_per_clu = fsi->dentries_in_root;
1014 else
1015 dentries_per_clu = fsi->dentries_per_clu;
1016
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);
1020 if (!ep)
1021 return -EIO;
1022
1023 type = fat_get_entry_type(ep);
1024
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)) {
1028 if (offset)
1029 *offset = dentry;
1030 return 0;
1031 }
1032 }
1033 }
1034
1035 /* fat12/16 root dir */
1036 if (IS_CLUS_FREE(clu.dir))
1037 break;
1038
1039 if (get_next_clus_safe(sb, &clu.dir))
1040 return -EIO;
1041 }
1042 return -ENOENT;
1043 }
1044
1045 #ifdef CONFIG_SDFAT_FAT32_SHORTNAME_SEQ
1046 static void __fat_attach_count_to_dos_name(u8 *dosname, s32 count)
1047 {
1048 s32 i, j, length;
1049 s8 str_count[6];
1050
1051 snprintf(str_count, sizeof(str_count), "~%d", count);
1052 length = strlen(str_count);
1053
1054 i = j = 0;
1055 while (j <= (8 - length)) {
1056 i = j;
1057 if (dosname[j] == ' ')
1058 break;
1059 if (dosname[j] & 0x80)
1060 j += 2;
1061 else
1062 j++;
1063 }
1064
1065 for (j = 0; j < length; i++, j++)
1066 dosname[i] = (u8) str_count[j];
1067
1068 if (i == 7)
1069 dosname[7] = ' ';
1070
1071 } /* end of __fat_attach_count_to_dos_name */
1072 #endif
1073
1074 s32 fat_generate_dos_name_new(struct super_block *sb, CHAIN_T *p_dir, DOS_NAME_T *p_dosname, s32 n_entry_needed)
1075 {
1076 s32 i;
1077 s32 baselen, err;
1078 u8 work[DOS_NAME_LENGTH], buf[5];
1079 u8 tail;
1080
1081 baselen = 8;
1082 memset(work, ' ', DOS_NAME_LENGTH);
1083 memcpy(work, p_dosname->name, DOS_NAME_LENGTH);
1084
1085 while (baselen && (work[--baselen] == ' ')) {
1086 /* DO NOTHING, JUST FOR CHECK_PATCH */
1087 }
1088
1089 if (baselen > 6)
1090 baselen = 6;
1091
1092 BUG_ON(baselen < 0);
1093
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) {
1102 /* void return */
1103 __fat_attach_count_to_dos_name(p_dosname->name, i);
1104 return 0;
1105 }
1106
1107 /* any other error */
1108 if (err)
1109 return err;
1110 }
1111 #endif
1112
1113 i = jiffies;
1114 tail = (jiffies >> 16) & 0x7;
1115
1116 if (baselen > 2)
1117 baselen = 2;
1118
1119 BUG_ON(baselen < 0);
1120
1121 work[baselen + 4] = '~';
1122 // 1 ~ 8 ASCII
1123 work[baselen + 5] = '1' + tail;
1124 while (1) {
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);
1130 break;
1131 }
1132
1133 /* any other error */
1134 if (err)
1135 return err;
1136
1137 i -= 11;
1138 }
1139 return 0;
1140 } /* end of generate_dos_name_new */
1141
1142 static s32 fat_calc_num_entries(UNI_NAME_T *p_uniname)
1143 {
1144 s32 len;
1145
1146 len = p_uniname->name_len;
1147 if (len == 0)
1148 return 0;
1149
1150 /* 1 dos name entry + extended entries */
1151 return((len-1) / 13 + 2);
1152
1153 } /* end of calc_num_enties */
1154
1155 static s32 fat_check_max_dentries(FILE_ID_T *fid)
1156 {
1157 if ((fid->size >> DENTRY_SIZE_BITS) >= MAX_FAT_DENTRIES) {
1158 /* FAT spec allows a dir to grow upto 65536 dentries */
1159 return -ENOSPC;
1160 }
1161 return 0;
1162 } /* end of check_max_dentries */
1163
1164
1165 /*
1166 * File Operation Functions
1167 */
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,
1172
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,
1181
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,
1194 };
1195
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,
1200
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,
1209
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,
1222
1223 .get_au_stat = amap_get_au_stat,
1224 };
1225
1226 s32 mount_fat16(struct super_block *sb, pbr_t *p_pbr)
1227 {
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);
1231
1232 if (!p_bpb->num_fats) {
1233 sdfat_msg(sb, KERN_ERR, "bogus number of FAT structure");
1234 return -EINVAL;
1235 }
1236
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;
1239
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;
1244
1245 fsi->num_FAT_sectors = le16_to_cpu(p_bpb->num_fat_sectors);
1246
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;
1250 else
1251 fsi->FAT2_start_sector = fsi->FAT1_start_sector + fsi->num_FAT_sectors;
1252
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;
1255
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);
1259
1260 if (!fsi->num_sectors) {
1261 sdfat_msg(sb, KERN_ERR, "bogus number of total sector count");
1262 return -EINVAL;
1263 }
1264
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 */
1268
1269 fsi->vol_type = FAT16;
1270 if (fsi->num_clusters < FAT12_THRESHOLD)
1271 fsi->vol_type = FAT12;
1272
1273 fsi->vol_id = get_unaligned_le32(p_bpb->vol_serial);
1274
1275 fsi->root_dir = 0;
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");
1280 return -EINVAL;
1281 }
1282
1283 fsi->dentries_per_clu = 1 << (fsi->cluster_size_bits - DENTRY_SIZE_BITS);
1284
1285 fsi->vol_flag = VOL_CLEAN;
1286 fsi->clu_srch_ptr = 2;
1287 fsi->used_clusters = (u32) ~0;
1288
1289 fsi->fs_func = &fat_fs_func;
1290 fat_ent_ops_init(sb);
1291
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.");
1297 }
1298
1299 return 0;
1300 } /* end of mount_fat16 */
1301
1302 static sector_t __calc_hidden_sect(struct super_block *sb)
1303 {
1304 struct block_device *bdev = sb->s_bdev;
1305 sector_t hidden = 0;
1306
1307 if (!bdev)
1308 goto out;
1309
1310 hidden = bdev->bd_part->start_sect;
1311 /* a disk device, not a partition */
1312 if (!hidden) {
1313 ASSERT(bdev == bdev->bd_contains);
1314 ASSERT(!bdev->bd_part->partno);
1315 goto out;
1316 }
1317
1318 if (sb->s_blocksize_bits != 9) {
1319 ASSERT(sb->s_blocksize_bits > 9);
1320 hidden >>= (sb->s_blocksize_bits - 9);
1321 }
1322
1323 out:
1324 sdfat_log_msg(sb, KERN_INFO, "start_sect of partition : %lld",
1325 (s64)hidden);
1326 return hidden;
1327
1328 }
1329
1330 s32 mount_fat32(struct super_block *sb, pbr_t *p_pbr)
1331 {
1332 s32 num_reserved;
1333 pbr32_t *p_bpb = (pbr32_t *)p_pbr;
1334 FS_INFO_T *fsi = &(SDFAT_SB(sb)->fsi);
1335
1336 if (!p_bpb->bpb.num_fats) {
1337 sdfat_msg(sb, KERN_ERR, "bogus number of FAT structure");
1338 return -EINVAL;
1339 }
1340
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;
1345
1346 fsi->num_FAT_sectors = le32_to_cpu(p_bpb->bpb.num_fat32_sectors);
1347
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;
1351 else
1352 fsi->FAT2_start_sector = fsi->FAT1_start_sector + fsi->num_FAT_sectors;
1353
1354 fsi->root_start_sector = fsi->FAT2_start_sector + fsi->num_FAT_sectors;
1355 fsi->data_start_sector = fsi->root_start_sector;
1356
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);
1361
1362 /* 2nd check */
1363 if (!fsi->num_sectors) {
1364 sdfat_msg(sb, KERN_ERR, "bogus number of total sector count");
1365 return -EINVAL;
1366 }
1367
1368 num_reserved = fsi->data_start_sector;
1369
1370 fsi->num_clusters = ((fsi->num_sectors-num_reserved) >> fsi->sect_per_clus_bits) + 2;
1371 /* because the cluster index starts with 2 */
1372
1373 fsi->vol_type = FAT32;
1374 fsi->vol_id = get_unaligned_le32(p_bpb->bsx.vol_serial);
1375
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);
1379
1380 fsi->vol_flag = VOL_CLEAN;
1381 fsi->clu_srch_ptr = 2;
1382 fsi->used_clusters = (u32) ~0;
1383
1384 fsi->fs_func = &fat_fs_func;
1385
1386 /* Delayed / smart allocation related init */
1387 fsi->reserved_clusters = 0;
1388
1389 /* Should be initialized before calling amap_create() */
1390 fat_ent_ops_init(sb);
1391
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;
1396 int ret;
1397
1398
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;
1411 }
1412 }
1413
1414 SDFAT_SB(sb)->options.amap_opt.misaligned_sect = hidden_sectors;
1415
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);
1420 }
1421
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);
1426 if (ret) {
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);
1430 } else {
1431 fsi->fs_func = &amap_fat_fs_func;
1432 }
1433 }
1434
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;
1441 }
1442
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.");
1448 }
1449
1450 return 0;
1451 } /* end of mount_fat32 */
1452
1453 /* end of core_fat.c */