Lines Matching refs:fat
186 fat_clear_cl_head(struct fat_descriptor *fat, cl_t cl) in fat_clear_cl_head() argument
188 bitmap_clear(&fat->headbitmap, cl); in fat_clear_cl_head()
192 fat_is_cl_head(struct fat_descriptor *fat, cl_t cl) in fat_is_cl_head() argument
194 return (bitmap_get(&fat->headbitmap, cl)); in fat_is_cl_head()
198 fat_is_cl_head_in_range(struct fat_descriptor *fat, cl_t cl) in fat_is_cl_head_in_range() argument
200 return (!(bitmap_none_in_range(&fat->headbitmap, cl))); in fat_is_cl_head_in_range()
204 fat_get_head_count(struct fat_descriptor *fat) in fat_get_head_count() argument
206 return (bitmap_count(&fat->headbitmap)); in fat_get_head_count()
215 fat_get_fat12_ptr(struct fat_descriptor *fat, cl_t cl) in fat_get_fat12_ptr() argument
217 return (fat->fatbuf + ((cl + (cl >> 1)))); in fat_get_fat12_ptr()
221 fat_get_fat12_next(struct fat_descriptor *fat, cl_t cl) in fat_get_fat12_next() argument
226 p = fat_get_fat12_ptr(fat, cl); in fat_get_fat12_next()
240 fat_set_fat12_next(struct fat_descriptor *fat, cl_t cl, cl_t nextcl) in fat_set_fat12_next() argument
247 p = fat_get_fat12_ptr(fat, cl); in fat_set_fat12_next()
272 fat_get_fat16_ptr(struct fat_descriptor *fat, cl_t cl) in fat_get_fat16_ptr() argument
274 return (fat->fatbuf + (cl << 1)); in fat_get_fat16_ptr()
278 fat_get_fat16_next(struct fat_descriptor *fat, cl_t cl) in fat_get_fat16_next() argument
283 p = fat_get_fat16_ptr(fat, cl); in fat_get_fat16_next()
293 fat_set_fat16_next(struct fat_descriptor *fat, cl_t cl, cl_t nextcl) in fat_set_fat16_next() argument
300 p = fat_get_fat16_ptr(fat, cl); in fat_set_fat16_next()
311 fat_get_fat32_ptr(struct fat_descriptor *fat, cl_t cl) in fat_get_fat32_ptr() argument
313 return (fat->fatbuf + (cl << 2)); in fat_get_fat32_ptr()
317 fat_get_fat32_next(struct fat_descriptor *fat, cl_t cl) in fat_get_fat32_next() argument
322 p = fat_get_fat32_ptr(fat, cl); in fat_get_fat32_next()
332 fat_set_fat32_next(struct fat_descriptor *fat, cl_t cl, cl_t nextcl) in fat_set_fat32_next() argument
339 p = fat_get_fat32_ptr(fat, cl); in fat_set_fat32_next()
347 fat_get_iosize(struct fat_descriptor *fat, off_t address) in fat_get_iosize() argument
350 if (address == fat->fat32_lastaddr) { in fat_get_iosize()
351 return (fat->fatsize & ((off_t)fat32_cache_chunk_size - 1)); in fat_get_iosize()
358 fat_flush_fat32_cache_entry(struct fat_descriptor *fat, in fat_flush_fat32_cache_entry() argument
365 fd = fd_of_(fat); in fat_flush_fat32_cache_entry()
370 writesize = fat_get_iosize(fat, entry->addr); in fat_flush_fat32_cache_entry()
372 fat_addr = fat->fat32_offset + entry->addr; in fat_flush_fat32_cache_entry()
384 fat_get_fat32_cache_entry(struct fat_descriptor *fat, off_t addr, in fat_get_fat32_cache_entry() argument
394 first = TAILQ_FIRST(&fat->fat32_cache_head); in fat_get_fat32_cache_entry()
399 TAILQ_FOREACH(entry, &fat->fat32_cache_head, entries) { in fat_get_fat32_cache_entry()
406 TAILQ_REMOVE(&fat->fat32_cache_head, entry, entries); in fat_get_fat32_cache_entry()
407 TAILQ_INSERT_HEAD(&fat->fat32_cache_head, entry, entries); in fat_get_fat32_cache_entry()
417 entry = TAILQ_LAST(&fat->fat32_cache_head, cachehead); in fat_get_fat32_cache_entry()
418 TAILQ_REMOVE(&fat->fat32_cache_head, entry, entries); in fat_get_fat32_cache_entry()
419 if (fat_flush_fat32_cache_entry(fat, entry) != FSOK) { in fat_get_fat32_cache_entry()
423 rwsize = fat_get_iosize(fat, addr); in fat_get_fat32_cache_entry()
424 fat_addr = fat->fat32_offset + addr; in fat_get_fat32_cache_entry()
426 fd = fd_of_(fat); in fat_get_fat32_cache_entry()
435 TAILQ_INSERT_HEAD(&fat->fat32_cache_head, entry, entries); in fat_get_fat32_cache_entry()
441 fat_get_fat32_cached_ptr(struct fat_descriptor *fat, cl_t cl, bool writing) in fat_get_fat32_cached_ptr() argument
447 entry = fat_get_fat32_cache_entry(fat, addr, writing); in fat_get_fat32_cached_ptr()
459 fat_get_fat32_cached_next(struct fat_descriptor *fat, cl_t cl) in fat_get_fat32_cached_next() argument
464 p = fat_get_fat32_cached_ptr(fat, cl, false); in fat_get_fat32_cached_next()
477 fat_set_fat32_cached_next(struct fat_descriptor *fat, cl_t cl, cl_t nextcl) in fat_set_fat32_cached_next() argument
484 p = fat_get_fat32_cached_ptr(fat, cl, true); in fat_set_fat32_cached_next()
493 cl_t fat_get_cl_next(struct fat_descriptor *fat, cl_t cl) in fat_get_cl_next() argument
496 if (!valid_cl(fat, cl)) { in fat_get_cl_next()
501 return (fat->get(fat, cl)); in fat_get_cl_next()
504 int fat_set_cl_next(struct fat_descriptor *fat, cl_t cl, cl_t nextcl) in fat_set_cl_next() argument
512 if (!valid_cl(fat, cl)) { in fat_set_cl_next()
517 return (fat->set(fat, cl, nextcl)); in fat_set_cl_next()
521 boot_of_(struct fat_descriptor *fat) { in boot_of_() argument
523 return (fat->boot); in boot_of_()
527 fat_get_boot(struct fat_descriptor *fat) { in fat_get_boot() argument
529 return (boot_of_(fat)); in fat_get_boot()
533 fd_of_(struct fat_descriptor *fat) in fd_of_() argument
535 return (fat->fd); in fd_of_()
539 fat_get_fd(struct fat_descriptor * fat) in fat_get_fd() argument
541 return (fd_of_(fat)); in fat_get_fd()
548 fat_is_valid_cl(struct fat_descriptor *fat, cl_t cl) in fat_is_valid_cl() argument
551 return (valid_cl(fat, cl)); in fat_is_valid_cl()
555 valid_cl(struct fat_descriptor *fat, cl_t cl) in valid_cl() argument
557 const struct bootblock *boot = boot_of_(fat); in valid_cl()
643 cleardirty(struct fat_descriptor *fat) in cleardirty() argument
651 boot = boot_of_(fat); in cleardirty()
652 fd = fd_of_(fat); in cleardirty()
693 _readfat(struct fat_descriptor *fat) in _readfat() argument
702 boot = boot_of_(fat); in _readfat()
703 fd = fd_of_(fat); in _readfat()
704 fat->fatsize = boot->FATsecs * boot->bpbBytesPerSec; in _readfat()
709 fat->is_mmapped = false; in _readfat()
710 fat->use_cache = false; in _readfat()
714 fat->fatbuf = mmap(NULL, fat->fatsize, in _readfat()
716 MAP_SHARED, fd_of_(fat), off); in _readfat()
717 if (fat->fatbuf != MAP_FAILED) { in _readfat()
718 fat->is_mmapped = true; in _readfat()
733 fat->fatsize >= fat32_cache_size) { in _readfat()
735 fat->use_cache = true; in _readfat()
737 fat->fat32_offset = boot->bpbResSectors * boot->bpbBytesPerSec; in _readfat()
738 fat->fat32_lastaddr = fat->fatsize & ~(fat32_cache_chunk_size); in _readfat()
740 readsize = fat->fatsize; in _readfat()
742 fat->fatbuf = malloc(readsize); in _readfat()
743 if (fat->fatbuf == NULL) { in _readfat()
752 if ((size_t)read(fd, fat->fatbuf, readsize) != readsize) { in _readfat()
761 if (fat->use_cache) { in _readfat()
762 TAILQ_INIT(&fat->fat32_cache_head); in _readfat()
771 entry[i].chunk = &fat->fatbuf[entry[i].addr]; in _readfat()
772 TAILQ_INSERT_TAIL(&fat->fat32_cache_head, in _readfat()
775 fat->fat32_cache_allentries = entry; in _readfat()
781 free(fat->fatbuf); in _readfat()
782 fat->fatbuf = NULL; in _readfat()
787 releasefat(struct fat_descriptor *fat) in releasefat() argument
789 if (fat->is_mmapped) { in releasefat()
790 munmap(fat->fatbuf, fat->fatsize); in releasefat()
792 if (fat->use_cache) { in releasefat()
793 free(fat->fat32_cache_allentries); in releasefat()
794 fat->fat32_cache_allentries = NULL; in releasefat()
796 free(fat->fatbuf); in releasefat()
798 fat->fatbuf = NULL; in releasefat()
799 bitmap_dtor(&fat->headbitmap); in releasefat()
808 struct fat_descriptor *fat; in readfat() local
815 fat = calloc(1, sizeof(struct fat_descriptor)); in readfat()
816 if (fat == NULL) { in readfat()
821 fat->fd = fs; in readfat()
822 fat->boot = boot; in readfat()
824 if (!_readfat(fat)) { in readfat()
825 free(fat); in readfat()
828 buffer = fat->fatbuf; in readfat()
833 fat->get = fat_get_fat12_next; in readfat()
834 fat->set = fat_set_fat12_next; in readfat()
837 fat->get = fat_get_fat16_next; in readfat()
838 fat->set = fat_set_fat16_next; in readfat()
841 if (fat->is_mmapped || !fat->use_cache) { in readfat()
842 fat->get = fat_get_fat32_next; in readfat()
843 fat->set = fat_set_fat32_next; in readfat()
845 fat->get = fat_get_fat32_cached_next; in readfat()
846 fat->set = fat_set_fat32_cached_next; in readfat()
851 releasefat(fat); in readfat()
852 free(fat); in readfat()
856 if (bitmap_ctor(&fat->headbitmap, boot->NumClusters, in readfat()
860 releasefat(fat); in readfat()
861 free(fat); in readfat()
965 nextcl = fat_get_cl_next(fat, cl); in readfat()
973 if (fat_is_cl_head(fat, cl)) { in readfat()
974 fat_clear_cl_head(fat, cl); in readfat()
978 if (fat_is_cl_head(fat, cl)) { in readfat()
979 fat_clear_cl_head(fat, cl); in readfat()
982 } else if (!valid_cl(fat, nextcl) && nextcl < CLUST_RSRVD) { in readfat()
988 ret |= fat_set_cl_next(fat, cl, CLUST_EOF); in readfat()
991 } else if (valid_cl(fat, nextcl)) { in readfat()
992 if (fat_is_cl_head(fat, nextcl)) { in readfat()
993 fat_clear_cl_head(fat, nextcl); in readfat()
998 ret |= fat_set_cl_next(fat, cl, CLUST_EOF); in readfat()
1007 releasefat(fat); in readfat()
1008 free(fat); in readfat()
1011 *fp = fat; in readfat()
1034 checkchain(struct fat_descriptor *fat, cl_t head, size_t *chainsize) in checkchain() argument
1045 assert(valid_cl(fat, head)); in checkchain()
1046 assert(fat_is_cl_head(fat, head)); in checkchain()
1051 fat_clear_cl_head(fat, head); in checkchain()
1069 for (next_cl = fat_get_cl_next(fat, current_cl); in checkchain()
1070 valid_cl(fat, next_cl); in checkchain()
1071 prev_cl = current_cl, current_cl = next_cl, next_cl = fat_get_cl_next(fat, current_cl)) in checkchain()
1098 next_cl & boot_of_(fat)->ClustMask); in checkchain()
1110 return (fat_set_cl_next(fat, current_cl, next_cl) | FSFATMOD); in checkchain()
1120 clearchain(struct fat_descriptor *fat, cl_t head) in clearchain() argument
1123 struct bootblock *boot = boot_of_(fat); in clearchain()
1127 while (valid_cl(fat, current_cl)) { in clearchain()
1128 next_cl = fat_get_cl_next(fat, current_cl); in clearchain()
1129 (void)fat_set_cl_next(fat, current_cl, CLUST_FREE); in clearchain()
1140 copyfat(struct fat_descriptor *fat, int n) in copyfat() argument
1148 fd = fd_of_(fat); in copyfat()
1149 boot = boot_of_(fat); in copyfat()
1151 blobs = howmany(fat->fatsize, fat32_cache_size); in copyfat()
1152 tailsize = fat->fatsize % fat32_cache_size; in copyfat()
1158 src_off = fat->fat32_offset; in copyfat()
1168 (size_t)read(fd, fat->fatbuf, rwsize) != rwsize) && in copyfat()
1175 (size_t)write(fd, fat->fatbuf, rwsize) != rwsize) && in copyfat()
1188 writefat(struct fat_descriptor *fat) in writefat() argument
1197 boot = boot_of_(fat); in writefat()
1198 fd = fd_of_(fat); in writefat()
1200 if (fat->use_cache) { in writefat()
1207 TAILQ_FOREACH(entry, &fat->fat32_cache_head, entries) { in writefat()
1208 if (fat_flush_fat32_cache_entry(fat, entry) != FSOK) { in writefat()
1220 if (copyfat(fat, i) != FSOK) in writefat()
1224 writesz = fat->fatsize; in writefat()
1226 for (i = fat->is_mmapped ? 1 : 0; i < boot->bpbFATs; i++) { in writefat()
1230 (size_t)write(fd, fat->fatbuf, writesz) != writesz) && in writefat()
1245 checklost(struct fat_descriptor *fat) in checklost() argument
1253 dosfs = fd_of_(fat); in checklost()
1254 boot = boot_of_(fat); in checklost()
1261 chains = fat_get_head_count(fat); in checklost()
1270 !fat_is_cl_head_in_range(fat, head)) { in checklost()
1274 if (fat_is_cl_head(fat, head)) { in checklost()
1275 ret = checkchain(fat, head, &chainlength); in checklost()
1280 mod |= ret = reconnect(fat, head, in checklost()
1286 clearchain(fat, head); in checklost()
1309 (boot->NumFree && fat_get_cl_next(fat, boot->FSNext) != CLUST_FREE))) { in checklost()
1315 if (fat_get_cl_next(fat, head) == CLUST_FREE) { in checklost()