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 static int chainalloc(struct msdosfsmount *pmp, u_long start,
64 u_long count, u_long fillwith, u_long *retcluster,
66 static int chainlength(struct msdosfsmount *pmp, u_long start,
68 static void fatblock(struct msdosfsmount *pmp, u_long ofs, u_long *bnp,
69 u_long *sizep, u_long *bop);
70 static int fatchain(struct msdosfsmount *pmp, u_long start, u_long count,
72 static void fc_lookup(struct denode *dep, u_long findcn, u_long *frcnp,
74 static void updatefats(struct msdosfsmount *pmp, struct buf *bp,
77 usemap_alloc(struct msdosfsmount *pmp, u_long cn);
79 usemap_free(struct msdosfsmount *pmp, u_long cn);
80 static int clusteralloc1(struct msdosfsmount *pmp, u_long start,
81 u_long count, u_long fillwith, u_long *retcluster,
85 fatblock(pmp, ofs, bnp, sizep, bop)
86 struct msdosfsmount *pmp;
94 bn = ofs / pmp->pm_fatblocksize * pmp->pm_fatblocksec;
95 size = min(pmp->pm_fatblocksec, pmp->pm_FATsecs - bn)
97 bn += pmp->pm_fatblk + pmp->pm_curfat * pmp->pm_FATsecs;
104 *bop = ofs % pmp->pm_fatblocksize;
108 * Map the logical cluster number of a file into a physical disk sector
109 * that is filesystem relative.
111 * dep - address of denode representing the file of interest
112 * findcn - file relative cluster whose filesystem relative cluster number
113 * and/or block number are/is to be found
114 * bnp - address of where to place the filesystem relative block number.
115 * If this pointer is null then don't return this quantity.
116 * cnp - address of where to place the filesystem relative cluster number.
117 * If this pointer is null then don't return this quantity.
119 * NOTE: Either bnp or cnp must be non-null.
120 * This function has one side effect. If the requested file relative cluster
121 * is beyond the end of file, then the actual number of clusters in the file
122 * is returned in *cnp. This is useful for determining how long a directory is.
123 * If cnp is null, nothing is returned.
126 pcbmap(dep, findcn, bnp, cnp, sp)
128 u_long findcn; /* file relative cluster to get */
129 daddr_t *bnp; /* returned filesys relative blk number */
130 u_long *cnp; /* returned cluster number */
131 int *sp; /* returned block size */
136 u_long prevcn = 0; /* XXX: prevcn could be used unititialized */
140 struct buf *bp = NULL;
142 struct msdosfsmount *pmp = dep->de_pmp;
145 KASSERT(bnp != NULL || cnp != NULL || sp != NULL,
146 ("pcbmap: extra call"));
147 ASSERT_VOP_ELOCKED(DETOV(dep), "pcbmap");
149 cn = dep->de_StartCluster;
151 * The "file" that makes up the root directory is contiguous,
152 * permanently allocated, of fixed size, and is not made up of
153 * clusters. If the cluster number is beyond the end of the root
154 * directory, then return the number of clusters in the file.
156 if (cn == MSDOSFSROOT) {
157 if (dep->de_Attributes & ATTR_DIRECTORY) {
158 if (de_cn2off(pmp, findcn) >= dep->de_FileSize) {
160 *cnp = de_bn2cn(pmp, pmp->pm_rootdirsize);
164 *bnp = pmp->pm_rootdirblk + de_cn2bn(pmp, findcn);
168 *sp = min(pmp->pm_bpcluster,
169 dep->de_FileSize - de_cn2off(pmp, findcn));
171 } else { /* just an empty file */
179 * All other files do I/O in cluster sized blocks
182 *sp = pmp->pm_bpcluster;
185 * Rummage around in the fat cache, maybe we can avoid tromping
186 * thru every fat entry for the file. And, keep track of how far
187 * off the cache was from where we wanted to be.
190 fc_lookup(dep, findcn, &i, &cn);
193 * Handle all other files or directories the normal way.
195 for (; i < findcn; i++) {
197 * Stop with all reserved clusters, not just with EOF.
199 if ((cn | ~pmp->pm_fatmask) >= CLUST_RSRVD)
201 byteoffset = FATOFS(pmp, cn);
202 fatblock(pmp, byteoffset, &bn, &bsize, &bo);
206 error = bread(pmp->pm_devvp, bn, bsize, NOCRED, &bp);
220 cn = getulong(&bp->b_data[bo]);
222 cn = getushort(&bp->b_data[bo]);
223 if (FAT12(pmp) && (prevcn & 1))
225 cn &= pmp->pm_fatmask;
228 * Force the special cluster numbers
229 * to be the same for all cluster sizes
230 * to let the rest of msdosfs handle
231 * all cases the same.
233 if ((cn | ~pmp->pm_fatmask) >= CLUST_RSRVD)
234 cn |= ~pmp->pm_fatmask;
237 if (!MSDOSFSEOF(pmp, cn)) {
241 *bnp = cntobn(pmp, cn);
244 fc_setcache(dep, FC_LASTMAP, i, cn);
253 /* update last file cluster entry in the fat cache */
254 fc_setcache(dep, FC_LASTFC, i - 1, prevcn);
259 * Find the closest entry in the fat cache to the cluster we are looking
263 fc_lookup(dep, findcn, frcnp, fsrcnp)
271 struct fatcache *closest = 0;
273 ASSERT_VOP_LOCKED(DETOV(dep), "fc_lookup");
275 for (i = 0; i < FC_SIZE; i++) {
276 cn = dep->de_fc[i].fc_frcn;
277 if (cn != FCE_EMPTY && cn <= findcn) {
278 if (closest == 0 || cn > closest->fc_frcn)
279 closest = &dep->de_fc[i];
283 *frcnp = closest->fc_frcn;
284 *fsrcnp = closest->fc_fsrcn;
289 * Purge the fat cache in denode dep of all entries relating to file
290 * relative cluster frcn and beyond.
298 struct fatcache *fcp;
300 ASSERT_VOP_ELOCKED(DETOV(dep), "fc_purge");
303 for (i = 0; i < FC_SIZE; i++, fcp++) {
304 if (fcp->fc_frcn >= frcn)
305 fcp->fc_frcn = FCE_EMPTY;
311 * If mirroring the fat, update all copies, with the first copy as last.
312 * Else update only the current fat (ignoring the others).
314 * pmp - msdosfsmount structure for filesystem to update
315 * bp - addr of modified fat block
316 * fatbn - block number relative to begin of filesystem of the modified fat block.
319 updatefats(pmp, bp, fatbn)
320 struct msdosfsmount *pmp;
328 printf("updatefats(pmp %p, bp %p, fatbn %lu)\n", pmp, bp, fatbn);
332 * If we have an FSInfo block, update it.
334 if (pmp->pm_fsinfo) {
335 if (bread(pmp->pm_devvp, pmp->pm_fsinfo, pmp->pm_BytesPerSec,
336 NOCRED, &bpn) != 0) {
338 * Ignore the error, but turn off FSInfo update for the future.
343 struct fsinfo *fp = (struct fsinfo *)bpn->b_data;
345 putulong(fp->fsinfree, pmp->pm_freeclustercount);
346 putulong(fp->fsinxtfree, pmp->pm_nxtfree);
347 if (pmp->pm_flags & MSDOSFSMNT_WAITONFAT)
354 if (pmp->pm_flags & MSDOSFS_FATMIRROR) {
356 * Now copy the block(s) of the modified fat to the other copies of
357 * the fat and write them out. This is faster than reading in the
358 * other fats and then writing them back out. This could tie up
359 * the fat for quite a while. Preventing others from accessing it.
360 * To prevent us from going after the fat quite so much we use
361 * delayed writes, unless they specfied "synchronous" when the
362 * filesystem was mounted. If synch is asked for then use
363 * bwrite()'s and really slow things down.
365 if (fatbn != pmp->pm_fatblk || FAT12(pmp))
371 for (i = 1; i < pmp->pm_FATs; i++) {
372 fatbn += pmp->pm_FATsecs;
373 /* getblk() never fails */
374 bpn = getblk(pmp->pm_devvp, fatbn, bp->b_bcount,
376 bcopy(bp->b_data, bpn->b_data, bp->b_bcount);
377 /* Force the clean bit on in the other copies. */
379 ((u_int8_t *)bpn->b_data)[3] |= 0x80;
380 else if (cleanfat == 32)
381 ((u_int8_t *)bpn->b_data)[7] |= 0x08;
382 if (pmp->pm_mountp->mnt_flag & MNT_SYNCHRONOUS)
390 * Write out the first (or current) fat last.
392 if (pmp->pm_mountp->mnt_flag & MNT_SYNCHRONOUS)
397 * Maybe update fsinfo sector here?
402 * Updating entries in 12 bit fats is a pain in the butt.
404 * The following picture shows where nibbles go when moving from a 12 bit
405 * cluster number into the appropriate bytes in the FAT.
407 * byte m byte m+1 byte m+2
408 * +----+----+ +----+----+ +----+----+
409 * | 0 1 | | 2 3 | | 4 5 | FAT bytes
410 * +----+----+ +----+----+ +----+----+
412 * +----+----+----+ +----+----+----+
413 * | 3 0 1 | | 4 5 2 |
414 * +----+----+----+ +----+----+----+
415 * cluster n cluster n+1
417 * Where n is even. m = n + (n >> 2)
421 usemap_alloc(pmp, cn)
422 struct msdosfsmount *pmp;
426 MSDOSFS_ASSERT_MP_LOCKED(pmp);
428 KASSERT((pmp->pm_inusemap[cn / N_INUSEBITS] & (1 << (cn % N_INUSEBITS)))
429 == 0, ("Allocating used sector %ld %ld %x", cn, cn % N_INUSEBITS,
430 (unsigned)pmp->pm_inusemap[cn / N_INUSEBITS]));
431 pmp->pm_inusemap[cn / N_INUSEBITS] |= 1 << (cn % N_INUSEBITS);
432 KASSERT(pmp->pm_freeclustercount > 0, ("usemap_alloc: too little"));
433 pmp->pm_freeclustercount--;
438 struct msdosfsmount *pmp;
442 MSDOSFS_ASSERT_MP_LOCKED(pmp);
443 pmp->pm_freeclustercount++;
444 KASSERT((pmp->pm_inusemap[cn / N_INUSEBITS] & (1 << (cn % N_INUSEBITS)))
445 != 0, ("Freeing unused sector %ld %ld %x", cn, cn % N_INUSEBITS,
446 (unsigned)pmp->pm_inusemap[cn / N_INUSEBITS]));
447 pmp->pm_inusemap[cn / N_INUSEBITS] &= ~(1 << (cn % N_INUSEBITS));
451 clusterfree(pmp, cluster, oldcnp)
452 struct msdosfsmount *pmp;
459 error = fatentry(FAT_GET_AND_SET, pmp, cluster, &oldcn, MSDOSFSFREE);
463 * If the cluster was successfully marked free, then update
464 * the count of free clusters, and turn off the "allocated"
465 * bit in the "in use" cluster bit map.
467 MSDOSFS_LOCK_MP(pmp);
468 usemap_free(pmp, cluster);
469 MSDOSFS_UNLOCK_MP(pmp);
476 * Get or Set or 'Get and Set' the cluster'th entry in the fat.
478 * function - whether to get or set a fat entry
479 * pmp - address of the msdosfsmount structure for the filesystem
480 * whose fat is to be manipulated.
481 * cn - which cluster is of interest
482 * oldcontents - address of a word that is to receive the contents of the
483 * cluster'th entry if this is a get function
484 * newcontents - the new value to be written into the cluster'th element of
485 * the fat if this is a set function.
487 * This function can also be used to free a cluster by setting the fat entry
488 * for a cluster to 0.
490 * All copies of the fat are updated if this is a set function. NOTE: If
491 * fatentry() marks a cluster as free it does not update the inusemap in
492 * the msdosfsmount structure. This is left to the caller.
495 fatentry(function, pmp, cn, oldcontents, newcontents)
497 struct msdosfsmount *pmp;
504 u_long bn, bo, bsize, byteoffset;
508 printf("fatentry(func %d, pmp %p, clust %lu, oldcon %p, newcon %lx)\n",
509 function, pmp, cn, oldcontents, newcontents);
514 * Be sure they asked us to do something.
516 if ((function & (FAT_SET | FAT_GET)) == 0) {
518 printf("fatentry(): function code doesn't specify get or set\n");
524 * If they asked us to return a cluster number but didn't tell us
525 * where to put it, give them an error.
527 if ((function & FAT_GET) && oldcontents == NULL) {
529 printf("fatentry(): get function with no place to put result\n");
536 * Be sure the requested cluster is in the filesystem.
538 if (cn < CLUST_FIRST || cn > pmp->pm_maxcluster)
541 byteoffset = FATOFS(pmp, cn);
542 fatblock(pmp, byteoffset, &bn, &bsize, &bo);
543 error = bread(pmp->pm_devvp, bn, bsize, NOCRED, &bp);
549 if (function & FAT_GET) {
551 readcn = getulong(&bp->b_data[bo]);
553 readcn = getushort(&bp->b_data[bo]);
554 if (FAT12(pmp) & (cn & 1))
556 readcn &= pmp->pm_fatmask;
557 /* map reserved fat entries to same values for all fats */
558 if ((readcn | ~pmp->pm_fatmask) >= CLUST_RSRVD)
559 readcn |= ~pmp->pm_fatmask;
560 *oldcontents = readcn;
562 if (function & FAT_SET) {
563 switch (pmp->pm_fatmask) {
565 readcn = getushort(&bp->b_data[bo]);
568 readcn |= newcontents << 4;
571 readcn |= newcontents & 0xfff;
573 putushort(&bp->b_data[bo], readcn);
576 putushort(&bp->b_data[bo], newcontents);
580 * According to spec we have to retain the
581 * high order bits of the fat entry.
583 readcn = getulong(&bp->b_data[bo]);
584 readcn &= ~FAT32_MASK;
585 readcn |= newcontents & FAT32_MASK;
586 putulong(&bp->b_data[bo], readcn);
589 updatefats(pmp, bp, bn);
599 * Update a contiguous cluster chain
602 * start - first cluster of chain
603 * count - number of clusters in chain
604 * fillwith - what to write into fat entry of last cluster
607 fatchain(pmp, start, count, fillwith)
608 struct msdosfsmount *pmp;
614 u_long bn, bo, bsize, byteoffset, readcn, newc;
618 printf("fatchain(pmp %p, start %lu, count %lu, fillwith %lx)\n",
619 pmp, start, count, fillwith);
622 * Be sure the clusters are in the filesystem.
624 if (start < CLUST_FIRST || start + count - 1 > pmp->pm_maxcluster)
628 byteoffset = FATOFS(pmp, start);
629 fatblock(pmp, byteoffset, &bn, &bsize, &bo);
630 error = bread(pmp->pm_devvp, bn, bsize, NOCRED, &bp);
637 newc = --count > 0 ? start : fillwith;
638 switch (pmp->pm_fatmask) {
640 readcn = getushort(&bp->b_data[bo]);
643 readcn |= newc & 0xfff;
648 putushort(&bp->b_data[bo], readcn);
654 putushort(&bp->b_data[bo], newc);
658 readcn = getulong(&bp->b_data[bo]);
659 readcn &= ~pmp->pm_fatmask;
660 readcn |= newc & pmp->pm_fatmask;
661 putulong(&bp->b_data[bo], readcn);
668 updatefats(pmp, bp, bn);
675 * Check the length of a free cluster chain starting at start.
678 * start - start of chain
679 * count - maximum interesting length
682 chainlength(pmp, start, count)
683 struct msdosfsmount *pmp;
691 MSDOSFS_ASSERT_MP_LOCKED(pmp);
693 max_idx = pmp->pm_maxcluster / N_INUSEBITS;
694 idx = start / N_INUSEBITS;
695 start %= N_INUSEBITS;
696 map = pmp->pm_inusemap[idx];
697 map &= ~((1 << start) - 1);
699 len = ffs(map) - 1 - start;
700 return (len > count ? count : len);
702 len = N_INUSEBITS - start;
705 while (++idx <= max_idx) {
708 map = pmp->pm_inusemap[idx];
715 return (len > count ? count : len);
719 * Allocate contigous free clusters.
722 * start - start of cluster chain.
723 * count - number of clusters to allocate.
724 * fillwith - put this value into the fat entry for the
725 * last allocated cluster.
726 * retcluster - put the first allocated cluster's number here.
727 * got - how many clusters were actually allocated.
730 chainalloc(pmp, start, count, fillwith, retcluster, got)
731 struct msdosfsmount *pmp;
741 MSDOSFS_ASSERT_MP_LOCKED(pmp);
743 for (cl = start, n = count; n-- > 0;)
744 usemap_alloc(pmp, cl++);
746 error = fatchain(pmp, start, count, fillwith);
750 printf("clusteralloc(): allocated cluster chain at %lu (%lu clusters)\n",
757 pmp->pm_nxtfree = start + count;
758 if (pmp->pm_nxtfree > pmp->pm_maxcluster)
759 pmp->pm_nxtfree = CLUST_FIRST;
764 * Allocate contiguous free clusters.
767 * start - preferred start of cluster chain.
768 * count - number of clusters requested.
769 * fillwith - put this value into the fat entry for the
770 * last allocated cluster.
771 * retcluster - put the first allocated cluster's number here.
772 * got - how many clusters were actually allocated.
775 clusteralloc(struct msdosfsmount *pmp, u_long start, u_long count,
776 u_long fillwith, u_long *retcluster, u_long *got)
780 MSDOSFS_LOCK_MP(pmp);
781 error = clusteralloc1(pmp, start, count, fillwith, retcluster, got);
782 MSDOSFS_UNLOCK_MP(pmp);
787 clusteralloc1(struct msdosfsmount *pmp, u_long start, u_long count,
788 u_long fillwith, u_long *retcluster, u_long *got)
791 u_long len, newst, foundl, cn, l;
792 u_long foundcn = 0; /* XXX: foundcn could be used unititialized */
795 MSDOSFS_ASSERT_MP_LOCKED(pmp);
798 printf("clusteralloc(): find %lu clusters\n", count);
801 if ((len = chainlength(pmp, start, count)) >= count)
802 return (chainalloc(pmp, start, count, fillwith, retcluster, got));
806 newst = pmp->pm_nxtfree;
809 for (cn = newst; cn <= pmp->pm_maxcluster;) {
810 idx = cn / N_INUSEBITS;
811 map = pmp->pm_inusemap[idx];
812 map |= (1 << (cn % N_INUSEBITS)) - 1;
813 if (map != (u_int)-1) {
814 cn = idx * N_INUSEBITS + ffs(map^(u_int)-1) - 1;
815 if ((l = chainlength(pmp, cn, count)) >= count)
816 return (chainalloc(pmp, cn, count, fillwith, retcluster, got));
824 cn += N_INUSEBITS - cn % N_INUSEBITS;
826 for (cn = 0; cn < newst;) {
827 idx = cn / N_INUSEBITS;
828 map = pmp->pm_inusemap[idx];
829 map |= (1 << (cn % N_INUSEBITS)) - 1;
830 if (map != (u_int)-1) {
831 cn = idx * N_INUSEBITS + ffs(map^(u_int)-1) - 1;
832 if ((l = chainlength(pmp, cn, count)) >= count)
833 return (chainalloc(pmp, cn, count, fillwith, retcluster, got));
841 cn += N_INUSEBITS - cn % N_INUSEBITS;
848 return (chainalloc(pmp, start, len, fillwith, retcluster, got));
850 return (chainalloc(pmp, foundcn, foundl, fillwith, retcluster, got));
855 * Free a chain of clusters.
857 * pmp - address of the msdosfs mount structure for the filesystem
858 * containing the cluster chain to be freed.
859 * startcluster - number of the 1st cluster in the chain of clusters to be
863 freeclusterchain(pmp, cluster)
864 struct msdosfsmount *pmp;
868 struct buf *bp = NULL;
869 u_long bn, bo, bsize, byteoffset;
870 u_long readcn, lbn = -1;
872 MSDOSFS_LOCK_MP(pmp);
873 while (cluster >= CLUST_FIRST && cluster <= pmp->pm_maxcluster) {
874 byteoffset = FATOFS(pmp, cluster);
875 fatblock(pmp, byteoffset, &bn, &bsize, &bo);
878 updatefats(pmp, bp, lbn);
879 error = bread(pmp->pm_devvp, bn, bsize, NOCRED, &bp);
882 MSDOSFS_UNLOCK_MP(pmp);
887 usemap_free(pmp, cluster);
888 switch (pmp->pm_fatmask) {
890 readcn = getushort(&bp->b_data[bo]);
892 cluster = readcn >> 4;
894 readcn |= MSDOSFSFREE << 4;
898 readcn |= MSDOSFSFREE & 0xfff;
900 putushort(&bp->b_data[bo], readcn);
903 cluster = getushort(&bp->b_data[bo]);
904 putushort(&bp->b_data[bo], MSDOSFSFREE);
907 cluster = getulong(&bp->b_data[bo]);
908 putulong(&bp->b_data[bo],
909 (MSDOSFSFREE & FAT32_MASK) | (cluster & ~FAT32_MASK));
912 cluster &= pmp->pm_fatmask;
913 if ((cluster | ~pmp->pm_fatmask) >= CLUST_RSRVD)
914 cluster |= pmp->pm_fatmask;
917 updatefats(pmp, bp, bn);
918 MSDOSFS_UNLOCK_MP(pmp);
923 * Read in fat blocks looking for free clusters. For every free cluster
924 * found turn off its corresponding bit in the pm_inusemap.
928 struct msdosfsmount *pmp;
930 struct buf *bp = NULL;
933 u_long bn, bo, bsize, byteoffset;
935 MSDOSFS_ASSERT_MP_LOCKED(pmp);
938 * Mark all clusters in use, we mark the free ones in the fat scan
941 for (cn = 0; cn < (pmp->pm_maxcluster + N_INUSEBITS) / N_INUSEBITS; cn++)
942 pmp->pm_inusemap[cn] = (u_int)-1;
945 * Figure how many free clusters are in the filesystem by ripping
946 * through the fat counting the number of entries whose content is
947 * zero. These represent free clusters.
949 pmp->pm_freeclustercount = 0;
950 for (cn = CLUST_FIRST; cn <= pmp->pm_maxcluster; cn++) {
951 byteoffset = FATOFS(pmp, cn);
952 bo = byteoffset % pmp->pm_fatblocksize;
954 /* Read new FAT block */
957 fatblock(pmp, byteoffset, &bn, &bsize, NULL);
958 error = bread(pmp->pm_devvp, bn, bsize, NOCRED, &bp);
965 readcn = getulong(&bp->b_data[bo]);
967 readcn = getushort(&bp->b_data[bo]);
968 if (FAT12(pmp) && (cn & 1))
970 readcn &= pmp->pm_fatmask;
973 usemap_free(pmp, cn);
981 * Allocate a new cluster and chain it onto the end of the file.
983 * dep - the file to extend
984 * count - number of clusters to allocate
985 * bpp - where to return the address of the buf header for the first new
987 * ncp - where to put cluster number of the first newly allocated cluster
988 * If this pointer is 0, do not return the cluster number.
991 * NOTE: This function is not responsible for turning on the DE_UPDATE bit of
992 * the de_flag field of the denode and it does not change the de_FileSize
993 * field. This is left for the caller to do.
996 extendfile(dep, count, bpp, ncp, flags)
1006 struct msdosfsmount *pmp = dep->de_pmp;
1011 * Don't try to extend the root directory
1013 if (dep->de_StartCluster == MSDOSFSROOT
1014 && (dep->de_Attributes & ATTR_DIRECTORY)) {
1015 #ifdef MSDOSFS_DEBUG
1016 printf("extendfile(): attempt to extend root directory\n");
1022 * If the "file's last cluster" cache entry is empty, and the file
1023 * is not empty, then fill the cache entry by calling pcbmap().
1025 if (dep->de_fc[FC_LASTFC].fc_frcn == FCE_EMPTY &&
1026 dep->de_StartCluster != 0) {
1027 error = pcbmap(dep, 0xffff, 0, &cn, 0);
1028 /* we expect it to return E2BIG */
1033 dep->de_fc[FC_NEXTTOLASTFC].fc_frcn =
1034 dep->de_fc[FC_LASTFC].fc_frcn;
1035 dep->de_fc[FC_NEXTTOLASTFC].fc_fsrcn =
1036 dep->de_fc[FC_LASTFC].fc_fsrcn;
1039 * Allocate a new cluster chain and cat onto the end of the
1040 * file. * If the file is empty we make de_StartCluster point
1041 * to the new block. Note that de_StartCluster being 0 is
1042 * sufficient to be sure the file is empty since we exclude
1043 * attempts to extend the root directory above, and the root
1044 * dir is the only file with a startcluster of 0 that has
1045 * blocks allocated (sort of).
1047 if (dep->de_StartCluster == 0)
1050 cn = dep->de_fc[FC_LASTFC].fc_fsrcn + 1;
1051 error = clusteralloc(pmp, cn, count, CLUST_EOFE, &cn, &got);
1058 * Give them the filesystem relative cluster number if they want
1066 if (dep->de_StartCluster == 0) {
1067 dep->de_StartCluster = cn;
1070 error = fatentry(FAT_SET, pmp,
1071 dep->de_fc[FC_LASTFC].fc_fsrcn,
1074 clusterfree(pmp, cn, NULL);
1077 frcn = dep->de_fc[FC_LASTFC].fc_frcn + 1;
1081 * Update the "last cluster of the file" entry in the denode's fat
1084 fc_setcache(dep, FC_LASTFC, frcn + got - 1, cn + got - 1);
1086 if (flags & DE_CLEAR) {
1089 * Get the buf header for the new block of the file.
1091 if (dep->de_Attributes & ATTR_DIRECTORY)
1092 bp = getblk(pmp->pm_devvp,
1094 pmp->pm_bpcluster, 0, 0, 0);
1096 bp = getblk(DETOV(dep),
1098 pmp->pm_bpcluster, 0, 0, 0);
1100 * Do the bmap now, as in msdosfs_write
1106 if (bp->b_blkno == -1)
1107 panic("extendfile: pcbmap");
1109 bp->b_blkno = blkno;
1125 * Routine to mark a FAT16 or FAT32 volume as "clean" or "dirty" by
1126 * manipulating the upper bit of the FAT entry for cluster 1. Note that
1127 * this bit is not defined for FAT12 volumes, which are always assumed to
1130 * The fatentry() routine only works on cluster numbers that a file could
1131 * occupy, so it won't manipulate the entry for cluster 1. So we have to do
1132 * it here. The code was stolen from fatentry() and tailored for cluster 1.
1135 * pmp The MS-DOS volume to mark
1136 * dirty Non-zero if the volume should be marked dirty; zero if it
1137 * should be marked clean
1141 * EROFS Volume is read-only
1142 * ? (other errors from called routines)
1145 markvoldirty(struct msdosfsmount *pmp, int dirty)
1148 u_long bn, bo, bsize, byteoffset, fatval;
1152 * FAT12 does not support a "clean" bit, so don't do anything for
1158 /* Can't change the bit on a read-only filesystem. */
1159 if (pmp->pm_flags & MSDOSFSMNT_RONLY)
1163 * Fetch the block containing the FAT entry. It is given by the
1166 byteoffset = FATOFS(pmp, 1);
1167 fatblock(pmp, byteoffset, &bn, &bsize, &bo);
1168 error = bread(pmp->pm_devvp, bn, bsize, NOCRED, &bp);
1175 * Get the current value of the FAT entry and set/clear the relevant
1176 * bit. Dirty means clear the "clean" bit; clean means set the
1180 /* FAT32 uses bit 27. */
1181 fatval = getulong(&bp->b_data[bo]);
1183 fatval &= 0xF7FFFFFF;
1185 fatval |= 0x08000000;
1186 putulong(&bp->b_data[bo], fatval);
1188 /* Must be FAT16; use bit 15. */
1189 fatval = getushort(&bp->b_data[bo]);
1194 putushort(&bp->b_data[bo], fatval);
1197 /* Write out the modified FAT block synchronously. */
1198 return (bwrite(bp));