2 /* $NetBSD: msdosfs_fat.c,v 1.28 1997/11/17 15:36:49 ws Exp $ */
5 * Copyright (C) 1994, 1995, 1997 Wolfgang Solfrank.
6 * Copyright (C) 1994, 1995, 1997 TooLs GmbH.
8 * Original code by Paul Popelka (paulp@uts.amdahl.com) (see below).
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by TooLs GmbH.
21 * 4. The name of TooLs GmbH may not be used to endorse or promote products
22 * derived from this software without specific prior written permission.
24 * THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``AS IS'' AND ANY EXPRESS OR
25 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
26 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
27 * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
28 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
29 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
30 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
31 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
32 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
33 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 * Written by Paul Popelka (paulp@uts.amdahl.com)
38 * You can do anything you want with this software, just don't say you wrote
39 * it, and don't remove this notice.
41 * This software is provided "as is".
43 * The author supplies this software to be publicly redistributed on the
44 * understanding that the author is not responsible for the correct
45 * functioning of this software in any circumstances and is not liable for
46 * any damages caused by this software.
51 #include <sys/param.h>
52 #include <sys/systm.h>
54 #include <sys/mount.h>
55 #include <sys/vnode.h>
57 #include <fs/msdosfs/bpb.h>
58 #include <fs/msdosfs/direntry.h>
59 #include <fs/msdosfs/denode.h>
60 #include <fs/msdosfs/fat.h>
61 #include <fs/msdosfs/msdosfsmount.h>
63 #define FULL_RUN ((u_int)0xffffffff)
65 static int chainalloc(struct msdosfsmount *pmp, u_long start,
66 u_long count, u_long fillwith, u_long *retcluster,
68 static int chainlength(struct msdosfsmount *pmp, u_long start,
70 static void fatblock(struct msdosfsmount *pmp, u_long ofs, u_long *bnp,
71 u_long *sizep, u_long *bop);
72 static int fatchain(struct msdosfsmount *pmp, u_long start, u_long count,
74 static void fc_lookup(struct denode *dep, u_long findcn, u_long *frcnp,
76 static void updatefats(struct msdosfsmount *pmp, struct buf *bp,
79 usemap_alloc(struct msdosfsmount *pmp, u_long cn);
81 usemap_free(struct msdosfsmount *pmp, u_long cn);
82 static int clusteralloc1(struct msdosfsmount *pmp, u_long start,
83 u_long count, u_long fillwith, u_long *retcluster,
87 fatblock(struct msdosfsmount *pmp, u_long ofs, u_long *bnp, u_long *sizep,
92 bn = ofs / pmp->pm_fatblocksize * pmp->pm_fatblocksec;
93 size = min(pmp->pm_fatblocksec, pmp->pm_FATsecs - bn)
95 bn += pmp->pm_fatblk + pmp->pm_curfat * pmp->pm_FATsecs;
102 *bop = ofs % pmp->pm_fatblocksize;
106 * Map the logical cluster number of a file into a physical disk sector
107 * that is filesystem relative.
109 * dep - address of denode representing the file of interest
110 * findcn - file relative cluster whose filesystem relative cluster number
111 * and/or block number are/is to be found
112 * bnp - address of where to place the filesystem relative block number.
113 * If this pointer is null then don't return this quantity.
114 * cnp - address of where to place the filesystem relative cluster number.
115 * If this pointer is null then don't return this quantity.
116 * sp - pointer to returned block size
118 * NOTE: Either bnp or cnp must be non-null.
119 * This function has one side effect. If the requested file relative cluster
120 * is beyond the end of file, then the actual number of clusters in the file
121 * is returned in *cnp. This is useful for determining how long a directory is.
122 * If cnp is null, nothing is returned.
125 pcbmap(struct denode *dep, u_long findcn, daddr_t *bnp, u_long *cnp, int *sp)
130 u_long prevcn = 0; /* XXX: prevcn could be used unititialized */
134 struct buf *bp = NULL;
136 struct msdosfsmount *pmp = dep->de_pmp;
139 KASSERT(bnp != NULL || cnp != NULL || sp != NULL,
140 ("pcbmap: extra call"));
141 ASSERT_VOP_ELOCKED(DETOV(dep), "pcbmap");
143 cn = dep->de_StartCluster;
145 * The "file" that makes up the root directory is contiguous,
146 * permanently allocated, of fixed size, and is not made up of
147 * clusters. If the cluster number is beyond the end of the root
148 * directory, then return the number of clusters in the file.
150 if (cn == MSDOSFSROOT) {
151 if (dep->de_Attributes & ATTR_DIRECTORY) {
152 if (de_cn2off(pmp, findcn) >= dep->de_FileSize) {
154 *cnp = de_bn2cn(pmp, pmp->pm_rootdirsize);
158 *bnp = pmp->pm_rootdirblk + de_cn2bn(pmp, findcn);
162 *sp = min(pmp->pm_bpcluster,
163 dep->de_FileSize - de_cn2off(pmp, findcn));
165 } else { /* just an empty file */
173 * All other files do I/O in cluster sized blocks
176 *sp = pmp->pm_bpcluster;
179 * Rummage around in the fat cache, maybe we can avoid tromping
180 * through every fat entry for the file. And, keep track of how far
181 * off the cache was from where we wanted to be.
184 fc_lookup(dep, findcn, &i, &cn);
187 * Handle all other files or directories the normal way.
189 for (; i < findcn; i++) {
191 * Stop with all reserved clusters, not just with EOF.
193 if ((cn | ~pmp->pm_fatmask) >= CLUST_RSRVD)
195 byteoffset = FATOFS(pmp, cn);
196 fatblock(pmp, byteoffset, &bn, &bsize, &bo);
200 error = bread(pmp->pm_devvp, bn, bsize, NOCRED, &bp);
214 cn = getulong(&bp->b_data[bo]);
216 cn = getushort(&bp->b_data[bo]);
217 if (FAT12(pmp) && (prevcn & 1))
219 cn &= pmp->pm_fatmask;
222 * Force the special cluster numbers
223 * to be the same for all cluster sizes
224 * to let the rest of msdosfs handle
225 * all cases the same.
227 if ((cn | ~pmp->pm_fatmask) >= CLUST_RSRVD)
228 cn |= ~pmp->pm_fatmask;
231 if (!MSDOSFSEOF(pmp, cn)) {
235 *bnp = cntobn(pmp, cn);
238 fc_setcache(dep, FC_LASTMAP, i, cn);
247 /* update last file cluster entry in the fat cache */
248 fc_setcache(dep, FC_LASTFC, i - 1, prevcn);
253 * Find the closest entry in the fat cache to the cluster we are looking
257 fc_lookup(struct denode *dep, u_long findcn, u_long *frcnp, u_long *fsrcnp)
261 struct fatcache *closest = NULL;
263 ASSERT_VOP_LOCKED(DETOV(dep), "fc_lookup");
265 for (i = 0; i < FC_SIZE; i++) {
266 cn = dep->de_fc[i].fc_frcn;
267 if (cn != FCE_EMPTY && cn <= findcn) {
268 if (closest == NULL || cn > closest->fc_frcn)
269 closest = &dep->de_fc[i];
273 *frcnp = closest->fc_frcn;
274 *fsrcnp = closest->fc_fsrcn;
279 * Purge the fat cache in denode dep of all entries relating to file
280 * relative cluster frcn and beyond.
283 fc_purge(struct denode *dep, u_int frcn)
286 struct fatcache *fcp;
288 ASSERT_VOP_ELOCKED(DETOV(dep), "fc_purge");
291 for (i = 0; i < FC_SIZE; i++, fcp++) {
292 if (fcp->fc_frcn >= frcn)
293 fcp->fc_frcn = FCE_EMPTY;
299 * If mirroring the fat, update all copies, with the first copy as last.
300 * Else update only the current fat (ignoring the others).
302 * pmp - msdosfsmount structure for filesystem to update
303 * bp - addr of modified fat block
304 * fatbn - block number relative to begin of filesystem of the modified fat block.
307 updatefats(struct msdosfsmount *pmp, struct buf *bp, u_long fatbn)
313 printf("updatefats(pmp %p, bp %p, fatbn %lu)\n", pmp, bp, fatbn);
316 if (pmp->pm_flags & MSDOSFS_FATMIRROR) {
318 * Now copy the block(s) of the modified fat to the other copies of
319 * the fat and write them out. This is faster than reading in the
320 * other fats and then writing them back out. This could tie up
321 * the fat for quite a while. Preventing others from accessing it.
322 * To prevent us from going after the fat quite so much we use
323 * delayed writes, unless they specfied "synchronous" when the
324 * filesystem was mounted. If synch is asked for then use
325 * bwrite()'s and really slow things down.
327 if (fatbn != pmp->pm_fatblk || FAT12(pmp))
333 for (i = 1; i < pmp->pm_FATs; i++) {
334 fatbn += pmp->pm_FATsecs;
335 /* getblk() never fails */
336 bpn = getblk(pmp->pm_devvp, fatbn, bp->b_bcount,
338 bcopy(bp->b_data, bpn->b_data, bp->b_bcount);
339 /* Force the clean bit on in the other copies. */
341 ((u_int8_t *)bpn->b_data)[3] |= 0x80;
342 else if (cleanfat == 32)
343 ((u_int8_t *)bpn->b_data)[7] |= 0x08;
344 if (pmp->pm_mountp->mnt_flag & MNT_SYNCHRONOUS)
352 * Write out the first (or current) fat last.
354 if (pmp->pm_mountp->mnt_flag & MNT_SYNCHRONOUS)
361 * Updating entries in 12 bit fats is a pain in the butt.
363 * The following picture shows where nibbles go when moving from a 12 bit
364 * cluster number into the appropriate bytes in the FAT.
366 * byte m byte m+1 byte m+2
367 * +----+----+ +----+----+ +----+----+
368 * | 0 1 | | 2 3 | | 4 5 | FAT bytes
369 * +----+----+ +----+----+ +----+----+
371 * +----+----+----+ +----+----+----+
372 * | 3 0 1 | | 4 5 2 |
373 * +----+----+----+ +----+----+----+
374 * cluster n cluster n+1
376 * Where n is even. m = n + (n >> 2)
380 usemap_alloc(struct msdosfsmount *pmp, u_long cn)
383 MSDOSFS_ASSERT_MP_LOCKED(pmp);
385 KASSERT(cn <= pmp->pm_maxcluster, ("cn too large %lu %lu", cn,
386 pmp->pm_maxcluster));
387 KASSERT((pmp->pm_flags & MSDOSFSMNT_RONLY) == 0,
388 ("usemap_alloc on ro msdosfs mount"));
389 KASSERT((pmp->pm_inusemap[cn / N_INUSEBITS] & (1 << (cn % N_INUSEBITS)))
390 == 0, ("Allocating used sector %ld %ld %x", cn, cn % N_INUSEBITS,
391 (unsigned)pmp->pm_inusemap[cn / N_INUSEBITS]));
392 pmp->pm_inusemap[cn / N_INUSEBITS] |= 1 << (cn % N_INUSEBITS);
393 KASSERT(pmp->pm_freeclustercount > 0, ("usemap_alloc: too little"));
394 pmp->pm_freeclustercount--;
395 pmp->pm_flags |= MSDOSFS_FSIMOD;
399 usemap_free(struct msdosfsmount *pmp, u_long cn)
402 MSDOSFS_ASSERT_MP_LOCKED(pmp);
404 KASSERT(cn <= pmp->pm_maxcluster, ("cn too large %lu %lu", cn,
405 pmp->pm_maxcluster));
406 KASSERT((pmp->pm_flags & MSDOSFSMNT_RONLY) == 0,
407 ("usemap_free on ro msdosfs mount"));
408 pmp->pm_freeclustercount++;
409 pmp->pm_flags |= MSDOSFS_FSIMOD;
410 KASSERT((pmp->pm_inusemap[cn / N_INUSEBITS] & (1 << (cn % N_INUSEBITS)))
411 != 0, ("Freeing unused sector %ld %ld %x", cn, cn % N_INUSEBITS,
412 (unsigned)pmp->pm_inusemap[cn / N_INUSEBITS]));
413 pmp->pm_inusemap[cn / N_INUSEBITS] &= ~(1 << (cn % N_INUSEBITS));
417 clusterfree(struct msdosfsmount *pmp, u_long cluster, u_long *oldcnp)
422 error = fatentry(FAT_GET_AND_SET, pmp, cluster, &oldcn, MSDOSFSFREE);
426 * If the cluster was successfully marked free, then update
427 * the count of free clusters, and turn off the "allocated"
428 * bit in the "in use" cluster bit map.
430 MSDOSFS_LOCK_MP(pmp);
431 usemap_free(pmp, cluster);
432 MSDOSFS_UNLOCK_MP(pmp);
439 * Get or Set or 'Get and Set' the cluster'th entry in the fat.
441 * function - whether to get or set a fat entry
442 * pmp - address of the msdosfsmount structure for the filesystem
443 * whose fat is to be manipulated.
444 * cn - which cluster is of interest
445 * oldcontents - address of a word that is to receive the contents of the
446 * cluster'th entry if this is a get function
447 * newcontents - the new value to be written into the cluster'th element of
448 * the fat if this is a set function.
450 * This function can also be used to free a cluster by setting the fat entry
451 * for a cluster to 0.
453 * All copies of the fat are updated if this is a set function. NOTE: If
454 * fatentry() marks a cluster as free it does not update the inusemap in
455 * the msdosfsmount structure. This is left to the caller.
458 fatentry(int function, struct msdosfsmount *pmp, u_long cn, u_long *oldcontents,
463 u_long bn, bo, bsize, byteoffset;
467 printf("fatentry(func %d, pmp %p, clust %lu, oldcon %p, newcon %lx)\n",
468 function, pmp, cn, oldcontents, newcontents);
473 * Be sure they asked us to do something.
475 if ((function & (FAT_SET | FAT_GET)) == 0) {
477 printf("fatentry(): function code doesn't specify get or set\n");
483 * If they asked us to return a cluster number but didn't tell us
484 * where to put it, give them an error.
486 if ((function & FAT_GET) && oldcontents == NULL) {
488 printf("fatentry(): get function with no place to put result\n");
495 * Be sure the requested cluster is in the filesystem.
497 if (cn < CLUST_FIRST || cn > pmp->pm_maxcluster)
500 byteoffset = FATOFS(pmp, cn);
501 fatblock(pmp, byteoffset, &bn, &bsize, &bo);
502 error = bread(pmp->pm_devvp, bn, bsize, NOCRED, &bp);
508 if (function & FAT_GET) {
510 readcn = getulong(&bp->b_data[bo]);
512 readcn = getushort(&bp->b_data[bo]);
513 if (FAT12(pmp) & (cn & 1))
515 readcn &= pmp->pm_fatmask;
516 /* map reserved fat entries to same values for all fats */
517 if ((readcn | ~pmp->pm_fatmask) >= CLUST_RSRVD)
518 readcn |= ~pmp->pm_fatmask;
519 *oldcontents = readcn;
521 if (function & FAT_SET) {
522 switch (pmp->pm_fatmask) {
524 readcn = getushort(&bp->b_data[bo]);
527 readcn |= newcontents << 4;
530 readcn |= newcontents & 0xfff;
532 putushort(&bp->b_data[bo], readcn);
535 putushort(&bp->b_data[bo], newcontents);
539 * According to spec we have to retain the
540 * high order bits of the fat entry.
542 readcn = getulong(&bp->b_data[bo]);
543 readcn &= ~FAT32_MASK;
544 readcn |= newcontents & FAT32_MASK;
545 putulong(&bp->b_data[bo], readcn);
548 updatefats(pmp, bp, bn);
558 * Update a contiguous cluster chain
561 * start - first cluster of chain
562 * count - number of clusters in chain
563 * fillwith - what to write into fat entry of last cluster
566 fatchain(struct msdosfsmount *pmp, u_long start, u_long count, u_long fillwith)
569 u_long bn, bo, bsize, byteoffset, readcn, newc;
573 printf("fatchain(pmp %p, start %lu, count %lu, fillwith %lx)\n",
574 pmp, start, count, fillwith);
577 * Be sure the clusters are in the filesystem.
579 if (start < CLUST_FIRST || start + count - 1 > pmp->pm_maxcluster)
583 byteoffset = FATOFS(pmp, start);
584 fatblock(pmp, byteoffset, &bn, &bsize, &bo);
585 error = bread(pmp->pm_devvp, bn, bsize, NOCRED, &bp);
592 newc = --count > 0 ? start : fillwith;
593 switch (pmp->pm_fatmask) {
595 readcn = getushort(&bp->b_data[bo]);
598 readcn |= newc & 0xfff;
603 putushort(&bp->b_data[bo], readcn);
609 putushort(&bp->b_data[bo], newc);
613 readcn = getulong(&bp->b_data[bo]);
614 readcn &= ~pmp->pm_fatmask;
615 readcn |= newc & pmp->pm_fatmask;
616 putulong(&bp->b_data[bo], readcn);
623 updatefats(pmp, bp, bn);
630 * Check the length of a free cluster chain starting at start.
633 * start - start of chain
634 * count - maximum interesting length
637 chainlength(struct msdosfsmount *pmp, u_long start, u_long count)
643 MSDOSFS_ASSERT_MP_LOCKED(pmp);
645 if (start > pmp->pm_maxcluster)
647 max_idx = pmp->pm_maxcluster / N_INUSEBITS;
648 idx = start / N_INUSEBITS;
649 start %= N_INUSEBITS;
650 map = pmp->pm_inusemap[idx];
651 map &= ~((1 << start) - 1);
653 len = ffs(map) - 1 - start;
654 len = MIN(len, count);
655 if (start + len > pmp->pm_maxcluster)
656 len = pmp->pm_maxcluster - start + 1;
659 len = N_INUSEBITS - start;
662 if (start + len > pmp->pm_maxcluster)
663 len = pmp->pm_maxcluster - start + 1;
666 while (++idx <= max_idx) {
669 map = pmp->pm_inusemap[idx];
676 len = MIN(len, count);
677 if (start + len > pmp->pm_maxcluster)
678 len = pmp->pm_maxcluster - start + 1;
683 * Allocate contigous free clusters.
686 * start - start of cluster chain.
687 * count - number of clusters to allocate.
688 * fillwith - put this value into the fat entry for the
689 * last allocated cluster.
690 * retcluster - put the first allocated cluster's number here.
691 * got - how many clusters were actually allocated.
694 chainalloc(struct msdosfsmount *pmp, u_long start, u_long count,
695 u_long fillwith, u_long *retcluster, u_long *got)
700 MSDOSFS_ASSERT_MP_LOCKED(pmp);
701 KASSERT((pmp->pm_flags & MSDOSFSMNT_RONLY) == 0,
702 ("chainalloc on ro msdosfs mount"));
704 for (cl = start, n = count; n-- > 0;)
705 usemap_alloc(pmp, cl++);
706 pmp->pm_nxtfree = start + count;
707 if (pmp->pm_nxtfree > pmp->pm_maxcluster)
708 pmp->pm_nxtfree = CLUST_FIRST;
709 pmp->pm_flags |= MSDOSFS_FSIMOD;
710 error = fatchain(pmp, start, count, fillwith);
712 for (cl = start, n = count; n-- > 0;)
713 usemap_free(pmp, cl++);
717 printf("clusteralloc(): allocated cluster chain at %lu (%lu clusters)\n",
728 * Allocate contiguous free clusters.
731 * start - preferred start of cluster chain.
732 * count - number of clusters requested.
733 * fillwith - put this value into the fat entry for the
734 * last allocated cluster.
735 * retcluster - put the first allocated cluster's number here.
736 * got - how many clusters were actually allocated.
739 clusteralloc(struct msdosfsmount *pmp, u_long start, u_long count,
740 u_long fillwith, u_long *retcluster, u_long *got)
744 MSDOSFS_LOCK_MP(pmp);
745 error = clusteralloc1(pmp, start, count, fillwith, retcluster, got);
746 MSDOSFS_UNLOCK_MP(pmp);
751 clusteralloc1(struct msdosfsmount *pmp, u_long start, u_long count,
752 u_long fillwith, u_long *retcluster, u_long *got)
755 u_long len, newst, foundl, cn, l;
756 u_long foundcn = 0; /* XXX: foundcn could be used unititialized */
759 MSDOSFS_ASSERT_MP_LOCKED(pmp);
762 printf("clusteralloc(): find %lu clusters\n", count);
765 if ((len = chainlength(pmp, start, count)) >= count)
766 return (chainalloc(pmp, start, count, fillwith, retcluster, got));
770 newst = pmp->pm_nxtfree;
773 for (cn = newst; cn <= pmp->pm_maxcluster;) {
774 idx = cn / N_INUSEBITS;
775 map = pmp->pm_inusemap[idx];
776 map |= (1 << (cn % N_INUSEBITS)) - 1;
777 if (map != FULL_RUN) {
778 cn = idx * N_INUSEBITS + ffs(map ^ FULL_RUN) - 1;
779 if ((l = chainlength(pmp, cn, count)) >= count)
780 return (chainalloc(pmp, cn, count, fillwith, retcluster, got));
788 cn += N_INUSEBITS - cn % N_INUSEBITS;
790 for (cn = 0; cn < newst;) {
791 idx = cn / N_INUSEBITS;
792 map = pmp->pm_inusemap[idx];
793 map |= (1 << (cn % N_INUSEBITS)) - 1;
794 if (map != FULL_RUN) {
795 cn = idx * N_INUSEBITS + ffs(map ^ FULL_RUN) - 1;
796 if ((l = chainlength(pmp, cn, count)) >= count)
797 return (chainalloc(pmp, cn, count, fillwith, retcluster, got));
805 cn += N_INUSEBITS - cn % N_INUSEBITS;
812 return (chainalloc(pmp, start, len, fillwith, retcluster, got));
814 return (chainalloc(pmp, foundcn, foundl, fillwith, retcluster, got));
819 * Free a chain of clusters.
821 * pmp - address of the msdosfs mount structure for the filesystem
822 * containing the cluster chain to be freed.
823 * startcluster - number of the 1st cluster in the chain of clusters to be
827 freeclusterchain(struct msdosfsmount *pmp, u_long cluster)
830 struct buf *bp = NULL;
831 u_long bn, bo, bsize, byteoffset;
832 u_long readcn, lbn = -1;
834 MSDOSFS_LOCK_MP(pmp);
835 while (cluster >= CLUST_FIRST && cluster <= pmp->pm_maxcluster) {
836 byteoffset = FATOFS(pmp, cluster);
837 fatblock(pmp, byteoffset, &bn, &bsize, &bo);
840 updatefats(pmp, bp, lbn);
841 error = bread(pmp->pm_devvp, bn, bsize, NOCRED, &bp);
844 MSDOSFS_UNLOCK_MP(pmp);
849 usemap_free(pmp, cluster);
850 switch (pmp->pm_fatmask) {
852 readcn = getushort(&bp->b_data[bo]);
854 cluster = readcn >> 4;
856 readcn |= MSDOSFSFREE << 4;
860 readcn |= MSDOSFSFREE & 0xfff;
862 putushort(&bp->b_data[bo], readcn);
865 cluster = getushort(&bp->b_data[bo]);
866 putushort(&bp->b_data[bo], MSDOSFSFREE);
869 cluster = getulong(&bp->b_data[bo]);
870 putulong(&bp->b_data[bo],
871 (MSDOSFSFREE & FAT32_MASK) | (cluster & ~FAT32_MASK));
874 cluster &= pmp->pm_fatmask;
875 if ((cluster | ~pmp->pm_fatmask) >= CLUST_RSRVD)
876 cluster |= pmp->pm_fatmask;
879 updatefats(pmp, bp, bn);
880 MSDOSFS_UNLOCK_MP(pmp);
885 * Read in fat blocks looking for free clusters. For every free cluster
886 * found turn off its corresponding bit in the pm_inusemap.
889 fillinusemap(struct msdosfsmount *pmp)
891 struct buf *bp = NULL;
894 u_long bn, bo, bsize, byteoffset;
896 MSDOSFS_ASSERT_MP_LOCKED(pmp);
899 * Mark all clusters in use, we mark the free ones in the fat scan
902 for (cn = 0; cn < (pmp->pm_maxcluster + N_INUSEBITS) / N_INUSEBITS; cn++)
903 pmp->pm_inusemap[cn] = FULL_RUN;
906 * Figure how many free clusters are in the filesystem by ripping
907 * through the fat counting the number of entries whose content is
908 * zero. These represent free clusters.
910 pmp->pm_freeclustercount = 0;
911 for (cn = CLUST_FIRST; cn <= pmp->pm_maxcluster; cn++) {
912 byteoffset = FATOFS(pmp, cn);
913 bo = byteoffset % pmp->pm_fatblocksize;
915 /* Read new FAT block */
918 fatblock(pmp, byteoffset, &bn, &bsize, NULL);
919 error = bread(pmp->pm_devvp, bn, bsize, NOCRED, &bp);
926 readcn = getulong(&bp->b_data[bo]);
928 readcn = getushort(&bp->b_data[bo]);
929 if (FAT12(pmp) && (cn & 1))
931 readcn &= pmp->pm_fatmask;
933 if (readcn == CLUST_FREE)
934 usemap_free(pmp, cn);
939 for (cn = pmp->pm_maxcluster + 1; cn < (pmp->pm_maxcluster +
940 N_INUSEBITS) / N_INUSEBITS; cn++)
941 pmp->pm_inusemap[cn / N_INUSEBITS] |= 1 << (cn % N_INUSEBITS);
947 * Allocate a new cluster and chain it onto the end of the file.
949 * dep - the file to extend
950 * count - number of clusters to allocate
951 * bpp - where to return the address of the buf header for the first new
953 * ncp - where to put cluster number of the first newly allocated cluster
954 * If this pointer is 0, do not return the cluster number.
957 * NOTE: This function is not responsible for turning on the DE_UPDATE bit of
958 * the de_flag field of the denode and it does not change the de_FileSize
959 * field. This is left for the caller to do.
962 extendfile(struct denode *dep, u_long count, struct buf **bpp, u_long *ncp,
968 struct msdosfsmount *pmp = dep->de_pmp;
973 * Don't try to extend the root directory
975 if (dep->de_StartCluster == MSDOSFSROOT
976 && (dep->de_Attributes & ATTR_DIRECTORY)) {
978 printf("extendfile(): attempt to extend root directory\n");
984 * If the "file's last cluster" cache entry is empty, and the file
985 * is not empty, then fill the cache entry by calling pcbmap().
987 if (dep->de_fc[FC_LASTFC].fc_frcn == FCE_EMPTY &&
988 dep->de_StartCluster != 0) {
989 error = pcbmap(dep, 0xffff, 0, &cn, 0);
990 /* we expect it to return E2BIG */
995 dep->de_fc[FC_NEXTTOLASTFC].fc_frcn =
996 dep->de_fc[FC_LASTFC].fc_frcn;
997 dep->de_fc[FC_NEXTTOLASTFC].fc_fsrcn =
998 dep->de_fc[FC_LASTFC].fc_fsrcn;
1001 * Allocate a new cluster chain and cat onto the end of the
1003 * If the file is empty we make de_StartCluster point
1004 * to the new block. Note that de_StartCluster being
1005 * 0 is sufficient to be sure the file is empty since
1006 * we exclude attempts to extend the root directory
1007 * above, and the root dir is the only file with a
1008 * startcluster of 0 that has blocks allocated (sort
1011 if (dep->de_StartCluster == 0)
1014 cn = dep->de_fc[FC_LASTFC].fc_fsrcn + 1;
1015 error = clusteralloc(pmp, cn, count, CLUST_EOFE, &cn, &got);
1022 * Give them the filesystem relative cluster number if they want
1030 if (dep->de_StartCluster == 0) {
1031 dep->de_StartCluster = cn;
1034 error = fatentry(FAT_SET, pmp,
1035 dep->de_fc[FC_LASTFC].fc_fsrcn,
1038 clusterfree(pmp, cn, NULL);
1041 frcn = dep->de_fc[FC_LASTFC].fc_frcn + 1;
1045 * Update the "last cluster of the file" entry in the denode's fat
1048 fc_setcache(dep, FC_LASTFC, frcn + got - 1, cn + got - 1);
1050 if (flags & DE_CLEAR) {
1053 * Get the buf header for the new block of the file.
1055 if (dep->de_Attributes & ATTR_DIRECTORY)
1056 bp = getblk(pmp->pm_devvp,
1058 pmp->pm_bpcluster, 0, 0, 0);
1060 bp = getblk(DETOV(dep),
1062 pmp->pm_bpcluster, 0, 0, 0);
1064 * Do the bmap now, as in msdosfs_write
1070 if (bp->b_blkno == -1)
1071 panic("extendfile: pcbmap");
1073 bp->b_blkno = blkno;
1089 * Routine to mark a FAT16 or FAT32 volume as "clean" or "dirty" by
1090 * manipulating the upper bit of the FAT entry for cluster 1. Note that
1091 * this bit is not defined for FAT12 volumes, which are always assumed to
1094 * The fatentry() routine only works on cluster numbers that a file could
1095 * occupy, so it won't manipulate the entry for cluster 1. So we have to do
1096 * it here. The code was stolen from fatentry() and tailored for cluster 1.
1099 * pmp The MS-DOS volume to mark
1100 * dirty Non-zero if the volume should be marked dirty; zero if it
1101 * should be marked clean
1105 * EROFS Volume is read-only
1106 * ? (other errors from called routines)
1109 markvoldirty(struct msdosfsmount *pmp, int dirty)
1112 u_long bn, bo, bsize, byteoffset, fatval;
1116 * FAT12 does not support a "clean" bit, so don't do anything for
1122 /* Can't change the bit on a read-only filesystem. */
1123 if (pmp->pm_flags & MSDOSFSMNT_RONLY)
1127 * Fetch the block containing the FAT entry. It is given by the
1130 byteoffset = FATOFS(pmp, 1);
1131 fatblock(pmp, byteoffset, &bn, &bsize, &bo);
1132 error = bread(pmp->pm_devvp, bn, bsize, NOCRED, &bp);
1139 * Get the current value of the FAT entry and set/clear the relevant
1140 * bit. Dirty means clear the "clean" bit; clean means set the
1144 /* FAT32 uses bit 27. */
1145 fatval = getulong(&bp->b_data[bo]);
1147 fatval &= 0xF7FFFFFF;
1149 fatval |= 0x08000000;
1150 putulong(&bp->b_data[bo], fatval);
1152 /* Must be FAT16; use bit 15. */
1153 fatval = getushort(&bp->b_data[bo]);
1158 putushort(&bp->b_data[bo], fatval);
1161 /* Write out the modified FAT block synchronously. */
1162 return (bwrite(bp));