• Home
  • History
  • Annotate
  • Raw
  • Download
  • only in /freebsd-13-stable/sbin/fsck_msdosfs/

Lines Matching refs:fat

32 __RCSID("$NetBSD: fat.c,v 1.18 2006/06/05 16:51:18 christos Exp $");
188 fat_clear_cl_head(struct fat_descriptor *fat, cl_t cl)
190 bitmap_clear(&fat->headbitmap, cl);
194 fat_is_cl_head(struct fat_descriptor *fat, cl_t cl)
196 return (bitmap_get(&fat->headbitmap, cl));
200 fat_is_cl_head_in_range(struct fat_descriptor *fat, cl_t cl)
202 return (!(bitmap_none_in_range(&fat->headbitmap, cl)));
206 fat_get_head_count(struct fat_descriptor *fat)
208 return (bitmap_count(&fat->headbitmap));
217 fat_get_fat12_ptr(struct fat_descriptor *fat, cl_t cl)
219 return (fat->fatbuf + ((cl + (cl >> 1))));
223 fat_get_fat12_next(struct fat_descriptor *fat, cl_t cl)
228 p = fat_get_fat12_ptr(fat, cl);
242 fat_set_fat12_next(struct fat_descriptor *fat, cl_t cl, cl_t nextcl)
249 p = fat_get_fat12_ptr(fat, cl);
274 fat_get_fat16_ptr(struct fat_descriptor *fat, cl_t cl)
276 return (fat->fatbuf + (cl << 1));
280 fat_get_fat16_next(struct fat_descriptor *fat, cl_t cl)
285 p = fat_get_fat16_ptr(fat, cl);
295 fat_set_fat16_next(struct fat_descriptor *fat, cl_t cl, cl_t nextcl)
302 p = fat_get_fat16_ptr(fat, cl);
313 fat_get_fat32_ptr(struct fat_descriptor *fat, cl_t cl)
315 return (fat->fatbuf + (cl << 2));
319 fat_get_fat32_next(struct fat_descriptor *fat, cl_t cl)
324 p = fat_get_fat32_ptr(fat, cl);
334 fat_set_fat32_next(struct fat_descriptor *fat, cl_t cl, cl_t nextcl)
341 p = fat_get_fat32_ptr(fat, cl);
349 fat_get_iosize(struct fat_descriptor *fat, off_t address)
352 if (address == fat->fat32_lastaddr) {
353 return (fat->fatsize & ((off_t)fat32_cache_chunk_size - 1));
360 fat_flush_fat32_cache_entry(struct fat_descriptor *fat,
367 fd = fd_of_(fat);
372 writesize = fat_get_iosize(fat, entry->addr);
374 fat_addr = fat->fat32_offset + entry->addr;
386 fat_get_fat32_cache_entry(struct fat_descriptor *fat, off_t addr,
396 first = TAILQ_FIRST(&fat->fat32_cache_head);
401 TAILQ_FOREACH(entry, &fat->fat32_cache_head, entries) {
408 TAILQ_REMOVE(&fat->fat32_cache_head, entry, entries);
409 TAILQ_INSERT_HEAD(&fat->fat32_cache_head, entry, entries);
419 entry = TAILQ_LAST(&fat->fat32_cache_head, cachehead);
420 TAILQ_REMOVE(&fat->fat32_cache_head, entry, entries);
421 if (fat_flush_fat32_cache_entry(fat, entry) != FSOK) {
425 rwsize = fat_get_iosize(fat, addr);
426 fat_addr = fat->fat32_offset + addr;
428 fd = fd_of_(fat);
437 TAILQ_INSERT_HEAD(&fat->fat32_cache_head, entry, entries);
443 fat_get_fat32_cached_ptr(struct fat_descriptor *fat, cl_t cl, bool writing)
449 entry = fat_get_fat32_cache_entry(fat, addr, writing);
461 fat_get_fat32_cached_next(struct fat_descriptor *fat, cl_t cl)
466 p = fat_get_fat32_cached_ptr(fat, cl, false);
479 fat_set_fat32_cached_next(struct fat_descriptor *fat, cl_t cl, cl_t nextcl)
486 p = fat_get_fat32_cached_ptr(fat, cl, true);
495 cl_t fat_get_cl_next(struct fat_descriptor *fat, cl_t cl)
498 if (!valid_cl(fat, cl)) {
503 return (fat->get(fat, cl));
506 int fat_set_cl_next(struct fat_descriptor *fat, cl_t cl, cl_t nextcl)
514 if (!valid_cl(fat, cl)) {
519 return (fat->set(fat, cl, nextcl));
523 boot_of_(struct fat_descriptor *fat) {
525 return (fat->boot);
529 fat_get_boot(struct fat_descriptor *fat) {
531 return (boot_of_(fat));
535 fd_of_(struct fat_descriptor *fat)
537 return (fat->fd);
541 fat_get_fd(struct fat_descriptor * fat)
543 return (fd_of_(fat));
550 fat_is_valid_cl(struct fat_descriptor *fat, cl_t cl)
553 return (valid_cl(fat, cl));
557 valid_cl(struct fat_descriptor *fat, cl_t cl)
559 const struct bootblock *boot = boot_of_(fat);
645 cleardirty(struct fat_descriptor *fat)
653 boot = boot_of_(fat);
654 fd = fd_of_(fat);
695 _readfat(struct fat_descriptor *fat)
704 boot = boot_of_(fat);
705 fd = fd_of_(fat);
706 fat->fatsize = boot->FATsecs * boot->bpbBytesPerSec;
711 fat->is_mmapped = false;
712 fat->use_cache = false;
716 fat->fatbuf = mmap(NULL, fat->fatsize,
718 MAP_SHARED, fd_of_(fat), off);
719 if (fat->fatbuf != MAP_FAILED) {
720 fat->is_mmapped = true;
735 fat->fatsize >= fat32_cache_size) {
737 fat->use_cache = true;
739 fat->fat32_offset = boot->bpbResSectors * boot->bpbBytesPerSec;
740 fat->fat32_lastaddr = fat->fatsize & ~(fat32_cache_chunk_size);
742 readsize = fat->fatsize;
744 fat->fatbuf = malloc(readsize);
745 if (fat->fatbuf == NULL) {
754 if ((size_t)read(fd, fat->fatbuf, readsize) != readsize) {
763 if (fat->use_cache) {
764 TAILQ_INIT(&fat->fat32_cache_head);
773 entry[i].chunk = &fat->fatbuf[entry[i].addr];
774 TAILQ_INSERT_TAIL(&fat->fat32_cache_head,
777 fat->fat32_cache_allentries = entry;
783 free(fat->fatbuf);
784 fat->fatbuf = NULL;
789 releasefat(struct fat_descriptor *fat)
791 if (fat->is_mmapped) {
792 munmap(fat->fatbuf, fat->fatsize);
794 if (fat->use_cache) {
795 free(fat->fat32_cache_allentries);
796 fat->fat32_cache_allentries = NULL;
798 free(fat->fatbuf);
800 fat->fatbuf = NULL;
801 bitmap_dtor(&fat->headbitmap);
810 struct fat_descriptor *fat;
817 fat = calloc(1, sizeof(struct fat_descriptor));
818 if (fat == NULL) {
823 fat->fd = fs;
824 fat->boot = boot;
826 if (!_readfat(fat)) {
827 free(fat);
830 buffer = fat->fatbuf;
835 fat->get = fat_get_fat12_next;
836 fat->set = fat_set_fat12_next;
839 fat->get = fat_get_fat16_next;
840 fat->set = fat_set_fat16_next;
843 if (fat->is_mmapped || !fat->use_cache) {
844 fat->get = fat_get_fat32_next;
845 fat->set = fat_set_fat32_next;
847 fat->get = fat_get_fat32_cached_next;
848 fat->set = fat_set_fat32_cached_next;
853 releasefat(fat);
854 free(fat);
858 if (bitmap_ctor(&fat->headbitmap, boot->NumClusters,
862 releasefat(fat);
863 free(fat);
967 nextcl = fat_get_cl_next(fat, cl);
975 if (fat_is_cl_head(fat, cl)) {
976 fat_clear_cl_head(fat, cl);
980 if (fat_is_cl_head(fat, cl)) {
981 fat_clear_cl_head(fat, cl);
984 } else if (!valid_cl(fat, nextcl) && nextcl < CLUST_RSRVD) {
990 ret |= fat_set_cl_next(fat, cl, CLUST_EOF);
993 } else if (valid_cl(fat, nextcl)) {
994 if (fat_is_cl_head(fat, nextcl)) {
995 fat_clear_cl_head(fat, nextcl);
1000 ret |= fat_set_cl_next(fat, cl, CLUST_EOF);
1009 releasefat(fat);
1010 free(fat);
1013 *fp = fat;
1036 checkchain(struct fat_descriptor *fat, cl_t head, size_t *chainsize)
1047 assert(valid_cl(fat, head));
1048 assert(fat_is_cl_head(fat, head));
1053 fat_clear_cl_head(fat, head);
1071 for (next_cl = fat_get_cl_next(fat, current_cl);
1072 valid_cl(fat, next_cl);
1073 prev_cl = current_cl, current_cl = next_cl, next_cl = fat_get_cl_next(fat, current_cl))
1100 next_cl & boot_of_(fat)->ClustMask);
1112 return (fat_set_cl_next(fat, current_cl, next_cl) | FSFATMOD);
1122 clearchain(struct fat_descriptor *fat, cl_t head)
1125 struct bootblock *boot = boot_of_(fat);
1129 while (valid_cl(fat, current_cl)) {
1130 next_cl = fat_get_cl_next(fat, current_cl);
1131 (void)fat_set_cl_next(fat, current_cl, CLUST_FREE);
1142 copyfat(struct fat_descriptor *fat, int n)
1150 fd = fd_of_(fat);
1151 boot = boot_of_(fat);
1153 blobs = howmany(fat->fatsize, fat32_cache_size);
1154 tailsize = fat->fatsize % fat32_cache_size;
1160 src_off = fat->fat32_offset;
1170 (size_t)read(fd, fat->fatbuf, rwsize) != rwsize) &&
1177 (size_t)write(fd, fat->fatbuf, rwsize) != rwsize) &&
1190 writefat(struct fat_descriptor *fat)
1199 boot = boot_of_(fat);
1200 fd = fd_of_(fat);
1202 if (fat->use_cache) {
1209 TAILQ_FOREACH(entry, &fat->fat32_cache_head, entries) {
1210 if (fat_flush_fat32_cache_entry(fat, entry) != FSOK) {
1222 if (copyfat(fat, i) != FSOK)
1226 writesz = fat->fatsize;
1228 for (i = fat->is_mmapped ? 1 : 0; i < boot->bpbFATs; i++) {
1232 (size_t)write(fd, fat->fatbuf, writesz) != writesz) &&
1247 checklost(struct fat_descriptor *fat)
1255 dosfs = fd_of_(fat);
1256 boot = boot_of_(fat);
1263 chains = fat_get_head_count(fat);
1272 !fat_is_cl_head_in_range(fat, head)) {
1276 if (fat_is_cl_head(fat, head)) {
1277 ret = checkchain(fat, head, &chainlength);
1282 mod |= ret = reconnect(fat, head,
1288 clearchain(fat, head);
1311 (boot->NumFree && fat_get_cl_next(fat, boot->FSNext) != CLUST_FREE))) {
1317 if (fat_get_cl_next(fat, head) == CLUST_FREE) {