Lines Matching full:fs

45 #include "linux/fs.h"
177 static __inline__ void format_root(Fs_t *Fs, char *label, union bootsector *boot) in format_root() argument
189 buf = safe_malloc(Fs->sector_size); in format_root()
190 RootDir = OpenRoot((Stream_t *)Fs); in format_root()
196 memset(buf, '\0', Fs->sector_size); in format_root()
198 if(Fs->fat_bits == 32) { in format_root()
201 dirlen = Fs->cluster_size; in format_root()
202 fatAllocate(Fs, Fs->rootCluster, Fs->end_fat); in format_root()
204 dirlen = Fs->dir_len; in format_root()
206 PWRITES(RootDir, buf, sectorsToBytes(Fs, i), in format_root()
207 Fs->sector_size); in format_root()
214 if(Fs->fat_bits == 32) in format_root()
218 (uint16_t) (Fs->dir_len * (Fs->sector_size / 32))); in format_root()
227 * into Fs->fat_len)
231 static int calc_fat_len(Fs_t *Fs, uint32_t tot_sectors) in calc_fat_len() argument
241 assert(Fs->fat_bits != 0); in calc_fat_len()
245 fprintf(stderr, "Fat start=%d\n", Fs->fat_start); in calc_fat_len()
247 fprintf(stderr, "dir_len=%d\n", Fs->dir_len); in calc_fat_len()
249 Fs->fat_len = 0; in calc_fat_len()
250 clus_start = calc_clus_start(Fs); in calc_fat_len()
259 Fs->num_fat % 2 == 0 && in calc_fat_len()
260 Fs->cluster_size % 2 == 0) in calc_fat_len()
272 fat_nybbles = Fs->fat_bits / 4; in calc_fat_len()
273 numerator = rem_sect+2*Fs->cluster_size; in calc_fat_len()
283 Fs->cluster_size * Fs->sector_size * 2 + in calc_fat_len()
284 Fs->num_fat * fat_nybbles; in calc_fat_len()
315 Fs->fat_len = (numerator-1)/denominator+1+corr; in calc_fat_len()
320 * This only works if we assume that it is already clear that Fs->num_clus is
322 static inline bool clusters_fit_into_fat(Fs_t *Fs) { in clusters_fit_into_fat() argument
323 return ((Fs->num_clus+2) * (Fs->fat_bits/4) - 1) / (Fs->sector_size*2) < in clusters_fit_into_fat()
324 Fs->fat_len; in clusters_fit_into_fat()
331 static void check_fs_params_and_set_fat(Fs_t *Fs, uint32_t tot_sectors) in check_fs_params_and_set_fat() argument
337 Fs->num_clus, Fs->fat_len, fat_nybbles); in check_fs_params_and_set_fat()
343 assert(Fs->fat_bits == 32 ? (Fs->dir_len == 0) : (Fs->dir_len != 0)); in check_fs_params_and_set_fat()
348 Fs->clus_start + Fs->num_clus * Fs->cluster_size); in check_fs_params_and_set_fat()
350 Fs->clus_start + Fs->num_clus * Fs->cluster_size + in check_fs_params_and_set_fat()
351 Fs->cluster_size - 1); in check_fs_params_and_set_fat()
354 assert(clusters_fit_into_fat(Fs)); in check_fs_params_and_set_fat()
356 provisional_fat_bits = Fs->fat_bits; in check_fs_params_and_set_fat()
357 set_fat(Fs); in check_fs_params_and_set_fat()
359 assert(provisional_fat_bits == Fs->fat_bits); in check_fs_params_and_set_fat()
363 static void fat32_specific_init(Fs_t *Fs) { in fat32_specific_init() argument
364 Fs->primaryFat = 0; in fat32_specific_init()
365 Fs->writeAllFats = 1; in fat32_specific_init()
366 if(!Fs->backupBoot) { in fat32_specific_init()
367 if(Fs->fat_start <= 6) in fat32_specific_init()
368 Fs->backupBoot = Fs->fat_start - 1; in fat32_specific_init()
370 Fs->backupBoot=6; in fat32_specific_init()
373 if(Fs->fat_start < 3) { in fat32_specific_init()
379 if(Fs->fat_start <= Fs->backupBoot) { in fat32_specific_init()
381 "Reserved sectors (%d) must be more than backupBoot (%d)\n", Fs->fat_start, Fs->backupBoot); in fat32_specific_init()
382 Fs->backupBoot = 0; in fat32_specific_init()
389 * Fs the file system object
415 static int try_cluster_size(Fs_t *Fs, in try_cluster_size() argument
425 switch(Fs->fat_bits) { in try_cluster_size()
447 Fs->fat_bits, Fs->cluster_size, in try_cluster_size()
452 int fit=calc_fat_len(Fs, tot_sectors); in try_cluster_size()
462 if(calc_num_clus(Fs, tot_sectors) < 0) in try_cluster_size()
464 if(Fs->num_clus < minClus) in try_cluster_size()
472 if(Fs->num_clus >= FAT32 || !clusters_fit_into_fat(Fs)) in try_cluster_size()
478 if(Fs->num_clus < maxClus) in try_cluster_size()
486 /* This is needed when a size of a FAT fs somehow is in try_cluster_size()
509 bwaste = tot_sectors - Fs->clus_start - in try_cluster_size()
510 maxClus * Fs->cluster_size + 1; in try_cluster_size()
517 dir_grow = 32 - Fs->dir_len; in try_cluster_size()
523 (!may_change_boot_size || Fs->fat_bits == 12)) { in try_cluster_size()
525 (waste + Fs->num_fat - 1) / Fs->num_fat; in try_cluster_size()
527 Fs->fat_len += fat_grow; in try_cluster_size()
531 dir_shrink = waste - fat_grow * Fs->num_fat; in try_cluster_size()
536 Fs->fat_start += waste; in try_cluster_size()
538 Fs->dir_len += dir_grow; in try_cluster_size()
546 assert(Fs->num_clus >= minClus); in try_cluster_size()
547 assert(Fs->num_clus < maxClus); in try_cluster_size()
554 * On return, Fs will be initialized, or one of the following error codes
563 struct Fs_t *Fs, uint8_t *descr) in calc_fs_parameters() argument
565 bool may_change_boot_size = (Fs->fat_start == 0); in calc_fs_parameters()
567 bool may_change_cluster_size = (Fs->cluster_size == 0); in calc_fs_parameters()
568 bool may_change_root_size = (Fs->dir_len == 0); in calc_fs_parameters()
569 bool may_change_fat_len = (Fs->fat_len == 0); in calc_fs_parameters()
574 Fs->infoSectorLoc = 0; in calc_fs_parameters()
576 (may_change_boot_size || Fs->fat_start == 1) ) in calc_fs_parameters()
578 Fs->dir_len, Fs->cluster_size); in calc_fs_parameters()
582 Fs->fat_start = 1; in calc_fs_parameters()
583 Fs->cluster_size = params->cluster_size; in calc_fs_parameters()
584 Fs->dir_len = params->dir_len; in calc_fs_parameters()
585 Fs->fat_len = params->fat_len; in calc_fs_parameters()
586 Fs->fat_bits = 12; in calc_fs_parameters()
587 num_clus_valid = calc_num_clus(Fs, tot_sectors); in calc_fs_parameters()
591 check_fs_params_and_set_fat(Fs, tot_sectors); in calc_fs_parameters()
601 Fs->fat_bits = abs(dev->fat_bits); in calc_fs_parameters()
602 if(Fs->fat_bits == 0) in calc_fs_parameters()
605 Fs->fat_bits = fat32 ? 32 : 12; in calc_fs_parameters()
606 if(!Fs->cluster_size) { in calc_fs_parameters()
609 Fs->cluster_size = 2; in calc_fs_parameters()
610 else if(may_change_fat_len && Fs->fat_bits == 32) in calc_fs_parameters()
612 Fs->cluster_size = 8; in calc_fs_parameters()
615 Fs->cluster_size = 1; in calc_fs_parameters()
618 if(!Fs->dir_len) { in calc_fs_parameters()
622 Fs->dir_len = 4; in calc_fs_parameters()
624 Fs->dir_len = 7; in calc_fs_parameters()
627 Fs->dir_len = 14; in calc_fs_parameters()
630 Fs->dir_len = 15; in calc_fs_parameters()
632 Fs->dir_len = 32; in calc_fs_parameters()
634 saved_dir_len = Fs->dir_len; in calc_fs_parameters()
639 if(Fs->fat_bits == 32) in calc_fs_parameters()
640 Fs->fat_start = 32; in calc_fs_parameters()
642 Fs->fat_start = 1; in calc_fs_parameters()
645 if(Fs->fat_bits == 32) in calc_fs_parameters()
646 Fs->dir_len = 0; in calc_fs_parameters()
647 else if(Fs->dir_len == 0) in calc_fs_parameters()
648 Fs->dir_len = saved_dir_len; in calc_fs_parameters()
650 if(Fs->fat_bits == 32 && in calc_fs_parameters()
665 Fs->cluster_size = tot_sectors >= 32*1024*1024*2 ? 64 : in calc_fs_parameters()
668 Fs->cluster_size; in calc_fs_parameters()
671 fit=try_cluster_size(Fs, in calc_fs_parameters()
692 Fs->cluster_size > 1) { in calc_fs_parameters()
693 Fs->cluster_size = Fs->cluster_size / 2; in calc_fs_parameters()
706 if(!may_change_fat_bits || Fs->fat_bits == 12) in calc_fs_parameters()
709 switch(Fs->fat_bits) { in calc_fs_parameters()
711 Fs->fat_bits=12; in calc_fs_parameters()
714 Fs->fat_bits=16; in calc_fs_parameters()
726 if(Fs->fat_bits == 12 && in calc_fs_parameters()
728 Fs->cluster_size >= 8)) { in calc_fs_parameters()
729 Fs->fat_bits = 16; in calc_fs_parameters()
731 Fs->cluster_size = 1; in calc_fs_parameters()
735 if(Fs->fat_bits == 16 && in calc_fs_parameters()
737 Fs->cluster_size >= 64)) { in calc_fs_parameters()
738 Fs->fat_bits = 32; in calc_fs_parameters()
740 Fs->cluster_size = in calc_fs_parameters()
746 if(may_change_cluster_size && Fs->cluster_size < 128) { in calc_fs_parameters()
748 Fs->cluster_size = 2 * Fs->cluster_size; in calc_fs_parameters()
754 Fs->fat_bits == 16) { in calc_fs_parameters()
755 Fs->fat_bits=12; in calc_fs_parameters()
767 Fs->fat_bits, in calc_fs_parameters()
768 Fs->cluster_size, in calc_fs_parameters()
769 Fs->num_clus, in calc_fs_parameters()
770 Fs->fat_len); in calc_fs_parameters()
772 check_fs_params_and_set_fat(Fs, tot_sectors); in calc_fs_parameters()
773 if(Fs->fat_bits == 32) in calc_fs_parameters()
774 fat32_specific_init(Fs); in calc_fs_parameters()
778 void initFsForFormat(Fs_t *Fs) in initFsForFormat() argument
780 memset(Fs, 0, sizeof(*Fs)); in initFsForFormat()
781 init_head(&Fs->head, &FsClass, NULL); in initFsForFormat()
783 Fs->cluster_size = 0; in initFsForFormat()
784 Fs->dir_len = 0; in initFsForFormat()
785 Fs->fat_len = 0; in initFsForFormat()
786 Fs->num_fat = 2; in initFsForFormat()
787 Fs->backupBoot = 0; in initFsForFormat()
790 void setFsSectorSize(Fs_t *Fs, struct device *dev, uint16_t msize) { in setFsSectorSize() argument
792 Fs->sector_size = 512; in setFsSectorSize()
794 Fs->sector_size = (uint16_t) (128u << (dev->ssize & 0x7f)); in setFsSectorSize()
797 SET_INT(Fs->sector_size, msize); in setFsSectorSize()
799 if (Fs->sector_size == (unsigned int) (1 << j)) { in setFsSectorSize()
800 Fs->sectorShift = j; in setFsSectorSize()
804 Fs->sectorMask = Fs->sector_size - 1; in setFsSectorSize()
847 Fs_t *Fs; in mformat() local
907 Fs = New(Fs_t); in mformat()
908 if (!Fs) { in mformat()
912 initFsForFormat(Fs); in mformat()
914 Fs->dir_len = atou16(getenv("MTOOLS_DIR_LEN")); in mformat()
915 if(Fs->dir_len <= 0) in mformat()
916 Fs->dir_len=0; in mformat()
919 Fs->num_fat = atou8(getenv("MTOOLS_NFATS")); in mformat()
920 if(Fs->num_fat <= 0) in mformat()
921 Fs->num_fat=2; in mformat()
1061 Fs->cluster_size = atou8(optarg); in mformat()
1065 Fs->dir_len = strtou16(optarg,&endptr,0); in mformat()
1068 Fs->fat_len = strtoui(optarg,&endptr,0); in mformat()
1078 Fs->backupBoot = atou16(optarg); in mformat()
1079 if(Fs->backupBoot < 2) { in mformat()
1085 Fs->fat_start = atou8(optarg); in mformat()
1091 Fs->num_fat = atou8(optarg); in mformat()
1141 FREE(&(Fs->head.Next)); in mformat()
1167 Fs->head.Next = OpenImage(&used_dev, dev, name, in mformat()
1180 if(Fs->head.Next && info.FatSize) { in mformat()
1181 if(!Fs->fat_len) in mformat()
1182 Fs->fat_len = info.FatSize; in mformat()
1183 if(!Fs->dir_len) in mformat()
1184 Fs->dir_len = info.RootDirSize; in mformat()
1188 if (!Fs->head.Next) in mformat()
1194 setFsSectorSize(Fs, &used_dev, msize); in mformat()
1196 if(!used_dev.blocksize || used_dev.blocksize < Fs->sector_size) in mformat()
1197 blocksize = Fs->sector_size; in mformat()
1207 FREE(&Fs->head.Next); in mformat()
1216 PREADS(Fs->head.Next, in mformat()
1217 &boot.characters, 0, Fs->sector_size) != in mformat()
1218 (signed int) Fs->sector_size) { in mformat()
1228 FREE(&Fs->head.Next); in mformat()
1236 FREE(&Fs->head.Next); in mformat()
1248 PWRITES(Fs->head.Next, &boot.characters, in mformat()
1249 sectorsToBytes(Fs, tot_sectors-1), in mformat()
1250 Fs->sector_size); in mformat()
1272 memset(boot.characters, '\0', Fs->sector_size); in mformat()
1274 Fs->head.Next = buf_init(Fs->head.Next, in mformat()
1279 boot.boot.nfat = Fs->num_fat; in mformat()
1287 switch(calc_fs_parameters(&used_dev, fat32, tot_sectors, Fs, in mformat()
1294 Fs->fat_bits); in mformat()
1298 Fs->fat_bits); in mformat()
1302 Fs->fat_len); in mformat()
1316 Fs->fat_bits); in mformat()
1320 if(Fs->fat_bits == 32) { in mformat()
1322 set_dword(boot.boot.ext.fat32.bigFat, Fs->fat_len); in mformat()
1324 Fs->clus_start = Fs->num_fat * Fs->fat_len + Fs->fat_start; in mformat()
1329 /* fs version. What should go here? */ in mformat()
1333 set_dword(boot.boot.ext.fat32.rootCluster, Fs->rootCluster = 2); in mformat()
1336 set_word(boot.boot.ext.fat32.infoSector, Fs->infoSectorLoc = 1); in mformat()
1337 Fs->infoSectorLoc = 1; in mformat()
1340 set_word(boot.boot.ext.fat32.backupBoot, Fs->backupBoot); in mformat()
1344 set_word(boot.boot.fatlen, (uint16_t) Fs->fat_len); in mformat()
1345 Fs->dir_start = Fs->num_fat * Fs->fat_len + Fs->fat_start; in mformat()
1346 Fs->clus_start = Fs->dir_start + Fs->dir_len; in mformat()
1351 Fs->cp = cp_open(used_dev.codepage); in mformat()
1352 if(Fs->cp == NULL) in mformat()
1367 label_name_pc(GET_DOSCONVERT((Stream_t *)Fs), in mformat()
1372 sprintf(labelBlock->fat_type, "FAT%2.2d ", Fs->fat_bits); in mformat()
1375 set_word(boot.boot.secsiz, Fs->sector_size); in mformat()
1376 boot.boot.clsiz = (unsigned char) Fs->cluster_size; in mformat()
1377 set_word(boot.boot.nrsvsect, Fs->fat_start); in mformat()
1404 Fs->num_fat = 1; in mformat()
1407 Fs->lastFatSectorNr = 0; in mformat()
1408 Fs->lastFatSectorData = 0; in mformat()
1409 zero_fat(Fs, boot.boot.descr); in mformat()
1410 Fs->freeSpace = Fs->num_clus; in mformat()
1411 Fs->last = 2; in mformat()
1416 i < (info.BadSectors+Fs->cluster_size-1)/Fs->cluster_size; in mformat()
1418 fatEncode(Fs, i+2, 0xfff7); in mformat()
1421 format_root(Fs, label, &boot); in mformat()
1422 if(PWRITES((Stream_t *)Fs, boot.characters, 0, Fs->sector_size) < 0) { in mformat()
1427 if(Fs->fat_bits == 32 && WORD_S(ext.fat32.backupBoot) != MAX16) { in mformat()
1428 if(PWRITES((Stream_t *)Fs, boot.characters, in mformat()
1429 sectorsToBytes(Fs, WORD_S(ext.fat32.backupBoot)), in mformat()
1430 Fs->sector_size) < 0) { in mformat()
1436 FREE((Stream_t **)&Fs); in mformat()