2 /* $NetBSD: msdosfs_fat.c,v 1.28 1997/11/17 15:36:49 ws Exp $ */
5 * SPDX-License-Identifier: BSD-4-Clause
7 * Copyright (C) 1994, 1995, 1997 Wolfgang Solfrank.
8 * Copyright (C) 1994, 1995, 1997 TooLs GmbH.
10 * Original code by Paul Popelka (paulp@uts.amdahl.com) (see below).
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
20 * 3. All advertising materials mentioning features or use of this software
21 * must display the following acknowledgement:
22 * This product includes software developed by TooLs GmbH.
23 * 4. The name of TooLs GmbH may not be used to endorse or promote products
24 * derived from this software without specific prior written permission.
26 * THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``AS IS'' AND ANY EXPRESS OR
27 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
28 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
29 * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
30 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
31 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
32 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
33 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
34 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
35 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
38 * Written by Paul Popelka (paulp@uts.amdahl.com)
40 * You can do anything you want with this software, just don't say you wrote
41 * it, and don't remove this notice.
43 * This software is provided "as is".
45 * The author supplies this software to be publicly redistributed on the
46 * understanding that the author is not responsible for the correct
47 * functioning of this software in any circumstances and is not liable for
48 * any damages caused by this software.
53 #include <sys/param.h>
54 #include <sys/systm.h>
56 #include <sys/mount.h>
57 #include <sys/vmmeter.h>
58 #include <sys/vnode.h>
60 #include <fs/msdosfs/bpb.h>
61 #include <fs/msdosfs/direntry.h>
62 #include <fs/msdosfs/denode.h>
63 #include <fs/msdosfs/fat.h>
64 #include <fs/msdosfs/msdosfsmount.h>
66 #define FULL_RUN ((u_int)0xffffffff)
68 static int chainalloc(struct msdosfsmount *pmp, u_long start,
69 u_long count, u_long fillwith, u_long *retcluster,
71 static int chainlength(struct msdosfsmount *pmp, u_long start,
73 static void fatblock(struct msdosfsmount *pmp, u_long ofs, u_long *bnp,
74 u_long *sizep, u_long *bop);
75 static int fatchain(struct msdosfsmount *pmp, u_long start, u_long count,
77 static void fc_lookup(struct denode *dep, u_long findcn, u_long *frcnp,
79 static void updatefats(struct msdosfsmount *pmp, struct buf *bp,
82 usemap_alloc(struct msdosfsmount *pmp, u_long cn);
84 usemap_free(struct msdosfsmount *pmp, u_long cn);
85 static int clusteralloc1(struct msdosfsmount *pmp, u_long start,
86 u_long count, u_long fillwith, u_long *retcluster,
90 fatblock(struct msdosfsmount *pmp, u_long ofs, u_long *bnp, u_long *sizep,
95 bn = ofs / pmp->pm_fatblocksize * pmp->pm_fatblocksec;
96 size = min(pmp->pm_fatblocksec, pmp->pm_FATsecs - bn)
98 bn += pmp->pm_fatblk + pmp->pm_curfat * pmp->pm_FATsecs;
105 *bop = ofs % pmp->pm_fatblocksize;
109 * Map the logical cluster number of a file into a physical disk sector
110 * that is filesystem relative.
112 * dep - address of denode representing the file of interest
113 * findcn - file relative cluster whose filesystem relative cluster number
114 * and/or block number are/is to be found
115 * bnp - address of where to place the filesystem relative block number.
116 * If this pointer is null then don't return this quantity.
117 * cnp - address of where to place the filesystem relative cluster number.
118 * If this pointer is null then don't return this quantity.
119 * sp - pointer to returned block size
121 * NOTE: Either bnp or cnp must be non-null.
122 * This function has one side effect. If the requested file relative cluster
123 * is beyond the end of file, then the actual number of clusters in the file
124 * is returned in *cnp. This is useful for determining how long a directory is.
125 * If cnp is null, nothing is returned.
128 pcbmap(struct denode *dep, u_long findcn, daddr_t *bnp, u_long *cnp, int *sp)
133 u_long prevcn = 0; /* XXX: prevcn could be used unititialized */
137 struct buf *bp = NULL;
139 struct msdosfsmount *pmp = dep->de_pmp;
142 KASSERT(bnp != NULL || cnp != NULL || sp != NULL,
143 ("pcbmap: extra call"));
144 ASSERT_VOP_ELOCKED(DETOV(dep), "pcbmap");
146 cn = dep->de_StartCluster;
148 * The "file" that makes up the root directory is contiguous,
149 * permanently allocated, of fixed size, and is not made up of
150 * clusters. If the cluster number is beyond the end of the root
151 * directory, then return the number of clusters in the file.
153 if (cn == MSDOSFSROOT) {
154 if (dep->de_Attributes & ATTR_DIRECTORY) {
155 if (de_cn2off(pmp, findcn) >= dep->de_FileSize) {
157 *cnp = de_bn2cn(pmp, pmp->pm_rootdirsize);
161 *bnp = pmp->pm_rootdirblk + de_cn2bn(pmp, findcn);
165 *sp = min(pmp->pm_bpcluster,
166 dep->de_FileSize - de_cn2off(pmp, findcn));
168 } else { /* just an empty file */
176 * All other files do I/O in cluster sized blocks
179 *sp = pmp->pm_bpcluster;
182 * Rummage around in the FAT cache, maybe we can avoid tromping
183 * through every FAT entry for the file. And, keep track of how far
184 * off the cache was from where we wanted to be.
187 fc_lookup(dep, findcn, &i, &cn);
190 * Handle all other files or directories the normal way.
192 for (; i < findcn; i++) {
194 * Stop with all reserved clusters, not just with EOF.
196 if ((cn | ~pmp->pm_fatmask) >= CLUST_RSRVD)
198 byteoffset = FATOFS(pmp, cn);
199 fatblock(pmp, byteoffset, &bn, &bsize, &bo);
203 error = bread(pmp->pm_devvp, bn, bsize, NOCRED, &bp);
217 cn = getulong(&bp->b_data[bo]);
219 cn = getushort(&bp->b_data[bo]);
220 if (FAT12(pmp) && (prevcn & 1))
222 cn &= pmp->pm_fatmask;
225 * Force the special cluster numbers
226 * to be the same for all cluster sizes
227 * to let the rest of msdosfs handle
228 * all cases the same.
230 if ((cn | ~pmp->pm_fatmask) >= CLUST_RSRVD)
231 cn |= ~pmp->pm_fatmask;
234 if (!MSDOSFSEOF(pmp, cn)) {
238 *bnp = cntobn(pmp, cn);
241 fc_setcache(dep, FC_LASTMAP, i, cn);
250 /* update last file cluster entry in the FAT cache */
251 fc_setcache(dep, FC_LASTFC, i - 1, prevcn);
256 * Find the closest entry in the FAT cache to the cluster we are looking
260 fc_lookup(struct denode *dep, u_long findcn, u_long *frcnp, u_long *fsrcnp)
264 struct fatcache *closest = NULL;
266 ASSERT_VOP_LOCKED(DETOV(dep), "fc_lookup");
268 for (i = 0; i < FC_SIZE; i++) {
269 cn = dep->de_fc[i].fc_frcn;
270 if (cn != FCE_EMPTY && cn <= findcn) {
271 if (closest == NULL || cn > closest->fc_frcn)
272 closest = &dep->de_fc[i];
276 *frcnp = closest->fc_frcn;
277 *fsrcnp = closest->fc_fsrcn;
282 * Purge the FAT cache in denode dep of all entries relating to file
283 * relative cluster frcn and beyond.
286 fc_purge(struct denode *dep, u_int frcn)
289 struct fatcache *fcp;
291 ASSERT_VOP_ELOCKED(DETOV(dep), "fc_purge");
294 for (i = 0; i < FC_SIZE; i++, fcp++) {
295 if (fcp->fc_frcn >= frcn)
296 fcp->fc_frcn = FCE_EMPTY;
302 * If mirroring the FAT, update all copies, with the first copy as last.
303 * Else update only the current FAT (ignoring the others).
305 * pmp - msdosfsmount structure for filesystem to update
306 * bp - addr of modified FAT block
307 * fatbn - block number relative to begin of filesystem of the modified FAT block.
310 updatefats(struct msdosfsmount *pmp, struct buf *bp, u_long fatbn)
316 printf("updatefats(pmp %p, bp %p, fatbn %lu)\n", pmp, bp, fatbn);
319 if (pmp->pm_flags & MSDOSFS_FATMIRROR) {
321 * Now copy the block(s) of the modified FAT to the other copies of
322 * the FAT and write them out. This is faster than reading in the
323 * other FATs and then writing them back out. This could tie up
324 * the FAT for quite a while. Preventing others from accessing it.
325 * To prevent us from going after the FAT quite so much we use
326 * delayed writes, unless they specified "synchronous" when the
327 * filesystem was mounted. If synch is asked for then use
328 * bwrite()'s and really slow things down.
330 if (fatbn != pmp->pm_fatblk || FAT12(pmp))
336 for (i = 1; i < pmp->pm_FATs; i++) {
337 fatbn += pmp->pm_FATsecs;
338 /* getblk() never fails */
339 bpn = getblk(pmp->pm_devvp, fatbn, bp->b_bcount,
341 memcpy(bpn->b_data, bp->b_data, bp->b_bcount);
342 /* Force the clean bit on in the other copies. */
344 ((uint8_t *)bpn->b_data)[3] |= 0x80;
345 else if (cleanfat == 32)
346 ((uint8_t *)bpn->b_data)[7] |= 0x08;
347 if (pmp->pm_mountp->mnt_flag & MNT_SYNCHRONOUS)
355 * Write out the first (or current) FAT last.
357 if (pmp->pm_mountp->mnt_flag & MNT_SYNCHRONOUS)
364 * Updating entries in 12 bit FATs is a pain in the butt.
366 * The following picture shows where nibbles go when moving from a 12 bit
367 * cluster number into the appropriate bytes in the FAT.
369 * byte m byte m+1 byte m+2
370 * +----+----+ +----+----+ +----+----+
371 * | 0 1 | | 2 3 | | 4 5 | FAT bytes
372 * +----+----+ +----+----+ +----+----+
374 * +----+----+----+ +----+----+----+
375 * | 3 0 1 | | 4 5 2 |
376 * +----+----+----+ +----+----+----+
377 * cluster n cluster n+1
379 * Where n is even. m = n + (n >> 2)
383 usemap_alloc(struct msdosfsmount *pmp, u_long cn)
386 MSDOSFS_ASSERT_MP_LOCKED(pmp);
388 KASSERT(cn <= pmp->pm_maxcluster, ("cn too large %lu %lu", cn,
389 pmp->pm_maxcluster));
390 KASSERT((pmp->pm_flags & MSDOSFSMNT_RONLY) == 0,
391 ("usemap_alloc on ro msdosfs mount"));
392 KASSERT((pmp->pm_inusemap[cn / N_INUSEBITS] & (1 << (cn % N_INUSEBITS)))
393 == 0, ("Allocating used sector %ld %ld %x", cn, cn % N_INUSEBITS,
394 (unsigned)pmp->pm_inusemap[cn / N_INUSEBITS]));
395 pmp->pm_inusemap[cn / N_INUSEBITS] |= 1U << (cn % N_INUSEBITS);
396 KASSERT(pmp->pm_freeclustercount > 0, ("usemap_alloc: too little"));
397 pmp->pm_freeclustercount--;
398 pmp->pm_flags |= MSDOSFS_FSIMOD;
402 usemap_free(struct msdosfsmount *pmp, u_long cn)
405 MSDOSFS_ASSERT_MP_LOCKED(pmp);
407 KASSERT(cn <= pmp->pm_maxcluster, ("cn too large %lu %lu", cn,
408 pmp->pm_maxcluster));
409 KASSERT((pmp->pm_flags & MSDOSFSMNT_RONLY) == 0,
410 ("usemap_free on ro msdosfs mount"));
411 pmp->pm_freeclustercount++;
412 pmp->pm_flags |= MSDOSFS_FSIMOD;
413 KASSERT((pmp->pm_inusemap[cn / N_INUSEBITS] & (1 << (cn % N_INUSEBITS)))
414 != 0, ("Freeing unused sector %ld %ld %x", cn, cn % N_INUSEBITS,
415 (unsigned)pmp->pm_inusemap[cn / N_INUSEBITS]));
416 pmp->pm_inusemap[cn / N_INUSEBITS] &= ~(1U << (cn % N_INUSEBITS));
420 clusterfree(struct msdosfsmount *pmp, u_long cluster, u_long *oldcnp)
425 error = fatentry(FAT_GET_AND_SET, pmp, cluster, &oldcn, MSDOSFSFREE);
429 * If the cluster was successfully marked free, then update
430 * the count of free clusters, and turn off the "allocated"
431 * bit in the "in use" cluster bit map.
433 MSDOSFS_LOCK_MP(pmp);
434 usemap_free(pmp, cluster);
435 MSDOSFS_UNLOCK_MP(pmp);
442 * Get or Set or 'Get and Set' the cluster'th entry in the FAT.
444 * function - whether to get or set a FAT entry
445 * pmp - address of the msdosfsmount structure for the filesystem
446 * whose FAT is to be manipulated.
447 * cn - which cluster is of interest
448 * oldcontents - address of a word that is to receive the contents of the
449 * cluster'th entry if this is a get function
450 * newcontents - the new value to be written into the cluster'th element of
451 * the FAT if this is a set function.
453 * This function can also be used to free a cluster by setting the FAT entry
454 * for a cluster to 0.
456 * All copies of the FAT are updated if this is a set function. NOTE: If
457 * fatentry() marks a cluster as free it does not update the inusemap in
458 * the msdosfsmount structure. This is left to the caller.
461 fatentry(int function, struct msdosfsmount *pmp, u_long cn, u_long *oldcontents,
466 u_long bn, bo, bsize, byteoffset;
470 printf("fatentry(func %d, pmp %p, clust %lu, oldcon %p, newcon %lx)\n",
471 function, pmp, cn, oldcontents, newcontents);
476 * Be sure they asked us to do something.
478 if ((function & (FAT_SET | FAT_GET)) == 0) {
480 printf("fatentry(): function code doesn't specify get or set\n");
486 * If they asked us to return a cluster number but didn't tell us
487 * where to put it, give them an error.
489 if ((function & FAT_GET) && oldcontents == NULL) {
491 printf("fatentry(): get function with no place to put result\n");
498 * Be sure the requested cluster is in the filesystem.
500 if (cn < CLUST_FIRST || cn > pmp->pm_maxcluster)
503 byteoffset = FATOFS(pmp, cn);
504 fatblock(pmp, byteoffset, &bn, &bsize, &bo);
505 error = bread(pmp->pm_devvp, bn, bsize, NOCRED, &bp);
511 if (function & FAT_GET) {
513 readcn = getulong(&bp->b_data[bo]);
515 readcn = getushort(&bp->b_data[bo]);
516 if (FAT12(pmp) & (cn & 1))
518 readcn &= pmp->pm_fatmask;
519 /* map reserved FAT entries to same values for all FATs */
520 if ((readcn | ~pmp->pm_fatmask) >= CLUST_RSRVD)
521 readcn |= ~pmp->pm_fatmask;
522 *oldcontents = readcn;
524 if (function & FAT_SET) {
525 switch (pmp->pm_fatmask) {
527 readcn = getushort(&bp->b_data[bo]);
530 readcn |= newcontents << 4;
533 readcn |= newcontents & 0xfff;
535 putushort(&bp->b_data[bo], readcn);
538 putushort(&bp->b_data[bo], newcontents);
542 * According to spec we have to retain the
543 * high order bits of the FAT entry.
545 readcn = getulong(&bp->b_data[bo]);
546 readcn &= ~FAT32_MASK;
547 readcn |= newcontents & FAT32_MASK;
548 putulong(&bp->b_data[bo], readcn);
551 updatefats(pmp, bp, bn);
561 * Update a contiguous cluster chain
564 * start - first cluster of chain
565 * count - number of clusters in chain
566 * fillwith - what to write into FAT entry of last cluster
569 fatchain(struct msdosfsmount *pmp, u_long start, u_long count, u_long fillwith)
572 u_long bn, bo, bsize, byteoffset, readcn, newc;
576 printf("fatchain(pmp %p, start %lu, count %lu, fillwith %lx)\n",
577 pmp, start, count, fillwith);
580 * Be sure the clusters are in the filesystem.
582 if (start < CLUST_FIRST || start + count - 1 > pmp->pm_maxcluster)
586 byteoffset = FATOFS(pmp, start);
587 fatblock(pmp, byteoffset, &bn, &bsize, &bo);
588 error = bread(pmp->pm_devvp, bn, bsize, NOCRED, &bp);
595 newc = --count > 0 ? start : fillwith;
596 switch (pmp->pm_fatmask) {
598 readcn = getushort(&bp->b_data[bo]);
601 readcn |= newc & 0xfff;
606 putushort(&bp->b_data[bo], readcn);
612 putushort(&bp->b_data[bo], newc);
616 readcn = getulong(&bp->b_data[bo]);
617 readcn &= ~pmp->pm_fatmask;
618 readcn |= newc & pmp->pm_fatmask;
619 putulong(&bp->b_data[bo], readcn);
626 updatefats(pmp, bp, bn);
633 * Check the length of a free cluster chain starting at start.
636 * start - start of chain
637 * count - maximum interesting length
640 chainlength(struct msdosfsmount *pmp, u_long start, u_long count)
646 MSDOSFS_ASSERT_MP_LOCKED(pmp);
648 if (start > pmp->pm_maxcluster)
650 max_idx = pmp->pm_maxcluster / N_INUSEBITS;
651 idx = start / N_INUSEBITS;
652 start %= N_INUSEBITS;
653 map = pmp->pm_inusemap[idx];
654 map &= ~((1 << start) - 1);
656 len = ffs(map) - 1 - start;
657 len = MIN(len, count);
658 if (start + len > pmp->pm_maxcluster)
659 len = pmp->pm_maxcluster - start + 1;
662 len = N_INUSEBITS - start;
665 if (start + len > pmp->pm_maxcluster)
666 len = pmp->pm_maxcluster - start + 1;
669 while (++idx <= max_idx) {
672 map = pmp->pm_inusemap[idx];
679 len = MIN(len, count);
680 if (start + len > pmp->pm_maxcluster)
681 len = pmp->pm_maxcluster - start + 1;
686 * Allocate contigous free clusters.
689 * start - start of cluster chain.
690 * count - number of clusters to allocate.
691 * fillwith - put this value into the FAT entry for the
692 * last allocated cluster.
693 * retcluster - put the first allocated cluster's number here.
694 * got - how many clusters were actually allocated.
697 chainalloc(struct msdosfsmount *pmp, u_long start, u_long count,
698 u_long fillwith, u_long *retcluster, u_long *got)
703 MSDOSFS_ASSERT_MP_LOCKED(pmp);
704 KASSERT((pmp->pm_flags & MSDOSFSMNT_RONLY) == 0,
705 ("chainalloc on ro msdosfs mount"));
707 for (cl = start, n = count; n-- > 0;)
708 usemap_alloc(pmp, cl++);
709 pmp->pm_nxtfree = start + count;
710 if (pmp->pm_nxtfree > pmp->pm_maxcluster)
711 pmp->pm_nxtfree = CLUST_FIRST;
712 pmp->pm_flags |= MSDOSFS_FSIMOD;
713 error = fatchain(pmp, start, count, fillwith);
715 for (cl = start, n = count; n-- > 0;)
716 usemap_free(pmp, cl++);
720 printf("clusteralloc(): allocated cluster chain at %lu (%lu clusters)\n",
731 * Allocate contiguous free clusters.
734 * start - preferred start of cluster chain.
735 * count - number of clusters requested.
736 * fillwith - put this value into the FAT entry for the
737 * last allocated cluster.
738 * retcluster - put the first allocated cluster's number here.
739 * got - how many clusters were actually allocated.
742 clusteralloc(struct msdosfsmount *pmp, u_long start, u_long count,
743 u_long fillwith, u_long *retcluster, u_long *got)
747 MSDOSFS_LOCK_MP(pmp);
748 error = clusteralloc1(pmp, start, count, fillwith, retcluster, got);
749 MSDOSFS_UNLOCK_MP(pmp);
754 clusteralloc1(struct msdosfsmount *pmp, u_long start, u_long count,
755 u_long fillwith, u_long *retcluster, u_long *got)
758 u_long len, newst, foundl, cn, l;
759 u_long foundcn = 0; /* XXX: foundcn could be used unititialized */
762 MSDOSFS_ASSERT_MP_LOCKED(pmp);
765 printf("clusteralloc(): find %lu clusters\n", count);
768 if ((len = chainlength(pmp, start, count)) >= count)
769 return (chainalloc(pmp, start, count, fillwith, retcluster, got));
773 newst = pmp->pm_nxtfree;
776 for (cn = newst; cn <= pmp->pm_maxcluster;) {
777 idx = cn / N_INUSEBITS;
778 map = pmp->pm_inusemap[idx];
779 map |= (1U << (cn % N_INUSEBITS)) - 1;
780 if (map != FULL_RUN) {
781 cn = idx * N_INUSEBITS + ffs(map ^ FULL_RUN) - 1;
782 if ((l = chainlength(pmp, cn, count)) >= count)
783 return (chainalloc(pmp, cn, count, fillwith, retcluster, got));
791 cn += N_INUSEBITS - cn % N_INUSEBITS;
793 for (cn = 0; cn < newst;) {
794 idx = cn / N_INUSEBITS;
795 map = pmp->pm_inusemap[idx];
796 map |= (1U << (cn % N_INUSEBITS)) - 1;
797 if (map != FULL_RUN) {
798 cn = idx * N_INUSEBITS + ffs(map ^ FULL_RUN) - 1;
799 if ((l = chainlength(pmp, cn, count)) >= count)
800 return (chainalloc(pmp, cn, count, fillwith, retcluster, got));
808 cn += N_INUSEBITS - cn % N_INUSEBITS;
815 return (chainalloc(pmp, start, len, fillwith, retcluster, got));
817 return (chainalloc(pmp, foundcn, foundl, fillwith, retcluster, got));
822 * Free a chain of clusters.
824 * pmp - address of the msdosfs mount structure for the filesystem
825 * containing the cluster chain to be freed.
826 * startcluster - number of the 1st cluster in the chain of clusters to be
830 freeclusterchain(struct msdosfsmount *pmp, u_long cluster)
833 struct buf *bp = NULL;
834 u_long bn, bo, bsize, byteoffset;
835 u_long readcn, lbn = -1;
837 MSDOSFS_LOCK_MP(pmp);
838 while (cluster >= CLUST_FIRST && cluster <= pmp->pm_maxcluster) {
839 byteoffset = FATOFS(pmp, cluster);
840 fatblock(pmp, byteoffset, &bn, &bsize, &bo);
843 updatefats(pmp, bp, lbn);
844 error = bread(pmp->pm_devvp, bn, bsize, NOCRED, &bp);
847 MSDOSFS_UNLOCK_MP(pmp);
852 usemap_free(pmp, cluster);
853 switch (pmp->pm_fatmask) {
855 readcn = getushort(&bp->b_data[bo]);
857 cluster = readcn >> 4;
859 readcn |= MSDOSFSFREE << 4;
863 readcn |= MSDOSFSFREE & 0xfff;
865 putushort(&bp->b_data[bo], readcn);
868 cluster = getushort(&bp->b_data[bo]);
869 putushort(&bp->b_data[bo], MSDOSFSFREE);
872 cluster = getulong(&bp->b_data[bo]);
873 putulong(&bp->b_data[bo],
874 (MSDOSFSFREE & FAT32_MASK) | (cluster & ~FAT32_MASK));
877 cluster &= pmp->pm_fatmask;
878 if ((cluster | ~pmp->pm_fatmask) >= CLUST_RSRVD)
879 cluster |= pmp->pm_fatmask;
882 updatefats(pmp, bp, bn);
883 MSDOSFS_UNLOCK_MP(pmp);
888 * Read in FAT blocks looking for free clusters. For every free cluster
889 * found turn off its corresponding bit in the pm_inusemap.
892 fillinusemap(struct msdosfsmount *pmp)
895 u_long bn, bo, bsize, byteoffset, cn, readcn;
898 MSDOSFS_ASSERT_MP_LOCKED(pmp);
902 * Mark all clusters in use, we mark the free ones in the FAT scan
905 for (cn = 0; cn < (pmp->pm_maxcluster + N_INUSEBITS) / N_INUSEBITS; cn++)
906 pmp->pm_inusemap[cn] = FULL_RUN;
909 * Figure how many free clusters are in the filesystem by ripping
910 * through the FAT counting the number of entries whose content is
911 * zero. These represent free clusters.
913 pmp->pm_freeclustercount = 0;
914 for (cn = 0; cn <= pmp->pm_maxcluster; cn++) {
915 byteoffset = FATOFS(pmp, cn);
916 bo = byteoffset % pmp->pm_fatblocksize;
918 /* Read new FAT block */
921 fatblock(pmp, byteoffset, &bn, &bsize, NULL);
922 error = bread(pmp->pm_devvp, bn, bsize, NOCRED, &bp);
927 readcn = getulong(&bp->b_data[bo]);
929 readcn = getushort(&bp->b_data[bo]);
930 if (FAT12(pmp) && (cn & 1))
932 readcn &= pmp->pm_fatmask;
935 * Check if the FAT ID matches the BPB's media descriptor and
936 * all other bits are set to 1.
938 if (cn == 0 && readcn != ((pmp->pm_fatmask & 0xffffff00) |
939 pmp->pm_bpb.bpbMedia)) {
941 printf("mountmsdosfs(): Media descriptor in BPB"
942 "does not match FAT ID\n");
946 } else if (readcn == CLUST_FREE)
947 usemap_free(pmp, cn);
952 for (cn = pmp->pm_maxcluster + 1; cn < (pmp->pm_maxcluster +
953 N_INUSEBITS) / N_INUSEBITS; cn++)
954 pmp->pm_inusemap[cn / N_INUSEBITS] |= 1U << (cn % N_INUSEBITS);
960 * Allocate a new cluster and chain it onto the end of the file.
962 * dep - the file to extend
963 * count - number of clusters to allocate
964 * bpp - where to return the address of the buf header for the first new
966 * ncp - where to put cluster number of the first newly allocated cluster
967 * If this pointer is 0, do not return the cluster number.
970 * NOTE: This function is not responsible for turning on the DE_UPDATE bit of
971 * the de_flag field of the denode and it does not change the de_FileSize
972 * field. This is left for the caller to do.
975 extendfile(struct denode *dep, u_long count, struct buf **bpp, u_long *ncp,
981 struct msdosfsmount *pmp = dep->de_pmp;
986 * Don't try to extend the root directory
988 if (dep->de_StartCluster == MSDOSFSROOT
989 && (dep->de_Attributes & ATTR_DIRECTORY)) {
991 printf("extendfile(): attempt to extend root directory\n");
997 * If the "file's last cluster" cache entry is empty, and the file
998 * is not empty, then fill the cache entry by calling pcbmap().
1000 if (dep->de_fc[FC_LASTFC].fc_frcn == FCE_EMPTY &&
1001 dep->de_StartCluster != 0) {
1002 error = pcbmap(dep, 0xffff, 0, &cn, 0);
1003 /* we expect it to return E2BIG */
1008 dep->de_fc[FC_NEXTTOLASTFC].fc_frcn =
1009 dep->de_fc[FC_LASTFC].fc_frcn;
1010 dep->de_fc[FC_NEXTTOLASTFC].fc_fsrcn =
1011 dep->de_fc[FC_LASTFC].fc_fsrcn;
1014 * Allocate a new cluster chain and cat onto the end of the
1015 * file. If the file is empty we make de_StartCluster point
1016 * to the new block. Note that de_StartCluster being 0 is
1017 * sufficient to be sure the file is empty since we exclude
1018 * attempts to extend the root directory above, and the root
1019 * dir is the only file with a startcluster of 0 that has
1020 * blocks allocated (sort of).
1022 if (dep->de_StartCluster == 0)
1025 cn = dep->de_fc[FC_LASTFC].fc_fsrcn + 1;
1026 error = clusteralloc(pmp, cn, count, CLUST_EOFE, &cn, &got);
1033 * Give them the filesystem relative cluster number if they want
1041 if (dep->de_StartCluster == 0) {
1042 dep->de_StartCluster = cn;
1045 error = fatentry(FAT_SET, pmp,
1046 dep->de_fc[FC_LASTFC].fc_fsrcn,
1049 clusterfree(pmp, cn, NULL);
1052 frcn = dep->de_fc[FC_LASTFC].fc_frcn + 1;
1056 * Update the "last cluster of the file" entry in the
1057 * denode's FAT cache.
1059 fc_setcache(dep, FC_LASTFC, frcn + got - 1, cn + got - 1);
1061 if (flags & DE_CLEAR) {
1064 * Get the buf header for the new block of the file.
1066 if (dep->de_Attributes & ATTR_DIRECTORY)
1067 bp = getblk(pmp->pm_devvp,
1069 pmp->pm_bpcluster, 0, 0, 0);
1071 bp = getblk(DETOV(dep),
1073 pmp->pm_bpcluster, 0, 0, 0);
1075 * Do the bmap now, as in msdosfs_write
1081 if (bp->b_blkno == -1)
1082 panic("extendfile: pcbmap");
1084 bp->b_blkno = blkno;
1093 if (vm_page_count_severe() ||
1094 buf_dirty_count_severe())
1095 vn_fsync_buf(DETOV(dep), MNT_WAIT);
1104 * Routine to mark a FAT16 or FAT32 volume as "clean" or "dirty" by
1105 * manipulating the upper bit of the FAT entry for cluster 1. Note that
1106 * this bit is not defined for FAT12 volumes, which are always assumed to
1109 * The fatentry() routine only works on cluster numbers that a file could
1110 * occupy, so it won't manipulate the entry for cluster 1. So we have to do
1111 * it here. The code was stolen from fatentry() and tailored for cluster 1.
1114 * pmp The MS-DOS volume to mark
1115 * dirty Non-zero if the volume should be marked dirty; zero if it
1116 * should be marked clean
1120 * EROFS Volume is read-only
1121 * ? (other errors from called routines)
1124 markvoldirty(struct msdosfsmount *pmp, int dirty)
1127 u_long bn, bo, bsize, byteoffset, fatval;
1131 * FAT12 does not support a "clean" bit, so don't do anything for
1137 /* Can't change the bit on a read-only filesystem. */
1138 if (pmp->pm_flags & MSDOSFSMNT_RONLY)
1142 * Fetch the block containing the FAT entry. It is given by the
1145 byteoffset = FATOFS(pmp, 1);
1146 fatblock(pmp, byteoffset, &bn, &bsize, &bo);
1147 error = bread(pmp->pm_devvp, bn, bsize, NOCRED, &bp);
1154 * Get the current value of the FAT entry and set/clear the relevant
1155 * bit. Dirty means clear the "clean" bit; clean means set the
1159 /* FAT32 uses bit 27. */
1160 fatval = getulong(&bp->b_data[bo]);
1162 fatval &= 0xF7FFFFFF;
1164 fatval |= 0x08000000;
1165 putulong(&bp->b_data[bo], fatval);
1167 /* Must be FAT16; use bit 15. */
1168 fatval = getushort(&bp->b_data[bo]);
1173 putushort(&bp->b_data[bo], fatval);
1176 /* Write out the modified FAT block synchronously. */
1177 return (bwrite(bp));