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(pmp, ofs, bnp, sizep, bop)
88 struct msdosfsmount *pmp;
96 bn = ofs / pmp->pm_fatblocksize * pmp->pm_fatblocksec;
97 size = min(pmp->pm_fatblocksec, pmp->pm_FATsecs - bn)
99 bn += pmp->pm_fatblk + pmp->pm_curfat * pmp->pm_FATsecs;
106 *bop = ofs % pmp->pm_fatblocksize;
110 * Map the logical cluster number of a file into a physical disk sector
111 * that is filesystem relative.
113 * dep - address of denode representing the file of interest
114 * findcn - file relative cluster whose filesystem relative cluster number
115 * and/or block number are/is to be found
116 * bnp - address of where to place the filesystem relative block number.
117 * If this pointer is null then don't return this quantity.
118 * cnp - address of where to place the filesystem relative cluster number.
119 * If this pointer is null then don't return this quantity.
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(dep, findcn, bnp, cnp, sp)
130 u_long findcn; /* file relative cluster to get */
131 daddr_t *bnp; /* returned filesys relative blk number */
132 u_long *cnp; /* returned cluster number */
133 int *sp; /* returned block size */
138 u_long prevcn = 0; /* XXX: prevcn could be used unititialized */
142 struct buf *bp = NULL;
144 struct msdosfsmount *pmp = dep->de_pmp;
147 KASSERT(bnp != NULL || cnp != NULL || sp != NULL,
148 ("pcbmap: extra call"));
149 ASSERT_VOP_ELOCKED(DETOV(dep), "pcbmap");
151 cn = dep->de_StartCluster;
153 * The "file" that makes up the root directory is contiguous,
154 * permanently allocated, of fixed size, and is not made up of
155 * clusters. If the cluster number is beyond the end of the root
156 * directory, then return the number of clusters in the file.
158 if (cn == MSDOSFSROOT) {
159 if (dep->de_Attributes & ATTR_DIRECTORY) {
160 if (de_cn2off(pmp, findcn) >= dep->de_FileSize) {
162 *cnp = de_bn2cn(pmp, pmp->pm_rootdirsize);
166 *bnp = pmp->pm_rootdirblk + de_cn2bn(pmp, findcn);
170 *sp = min(pmp->pm_bpcluster,
171 dep->de_FileSize - de_cn2off(pmp, findcn));
173 } else { /* just an empty file */
181 * All other files do I/O in cluster sized blocks
184 *sp = pmp->pm_bpcluster;
187 * Rummage around in the fat cache, maybe we can avoid tromping
188 * thru every fat entry for the file. And, keep track of how far
189 * off the cache was from where we wanted to be.
192 fc_lookup(dep, findcn, &i, &cn);
195 * Handle all other files or directories the normal way.
197 for (; i < findcn; i++) {
199 * Stop with all reserved clusters, not just with EOF.
201 if ((cn | ~pmp->pm_fatmask) >= CLUST_RSRVD)
203 byteoffset = FATOFS(pmp, cn);
204 fatblock(pmp, byteoffset, &bn, &bsize, &bo);
208 error = bread(pmp->pm_devvp, bn, bsize, NOCRED, &bp);
222 cn = getulong(&bp->b_data[bo]);
224 cn = getushort(&bp->b_data[bo]);
225 if (FAT12(pmp) && (prevcn & 1))
227 cn &= pmp->pm_fatmask;
230 * Force the special cluster numbers
231 * to be the same for all cluster sizes
232 * to let the rest of msdosfs handle
233 * all cases the same.
235 if ((cn | ~pmp->pm_fatmask) >= CLUST_RSRVD)
236 cn |= ~pmp->pm_fatmask;
239 if (!MSDOSFSEOF(pmp, cn)) {
243 *bnp = cntobn(pmp, cn);
246 fc_setcache(dep, FC_LASTMAP, i, cn);
255 /* update last file cluster entry in the fat cache */
256 fc_setcache(dep, FC_LASTFC, i - 1, prevcn);
261 * Find the closest entry in the fat cache to the cluster we are looking
265 fc_lookup(dep, findcn, frcnp, fsrcnp)
273 struct fatcache *closest = 0;
275 ASSERT_VOP_LOCKED(DETOV(dep), "fc_lookup");
277 for (i = 0; i < FC_SIZE; i++) {
278 cn = dep->de_fc[i].fc_frcn;
279 if (cn != FCE_EMPTY && cn <= findcn) {
280 if (closest == 0 || cn > closest->fc_frcn)
281 closest = &dep->de_fc[i];
285 *frcnp = closest->fc_frcn;
286 *fsrcnp = closest->fc_fsrcn;
291 * Purge the fat cache in denode dep of all entries relating to file
292 * relative cluster frcn and beyond.
300 struct fatcache *fcp;
302 ASSERT_VOP_ELOCKED(DETOV(dep), "fc_purge");
305 for (i = 0; i < FC_SIZE; i++, fcp++) {
306 if (fcp->fc_frcn >= frcn)
307 fcp->fc_frcn = FCE_EMPTY;
313 * If mirroring the fat, update all copies, with the first copy as last.
314 * Else update only the current fat (ignoring the others).
316 * pmp - msdosfsmount structure for filesystem to update
317 * bp - addr of modified fat block
318 * fatbn - block number relative to begin of filesystem of the modified fat block.
321 updatefats(pmp, bp, fatbn)
322 struct msdosfsmount *pmp;
330 printf("updatefats(pmp %p, bp %p, fatbn %lu)\n", pmp, bp, fatbn);
333 if (pmp->pm_flags & MSDOSFS_FATMIRROR) {
335 * Now copy the block(s) of the modified fat to the other copies of
336 * the fat and write them out. This is faster than reading in the
337 * other fats and then writing them back out. This could tie up
338 * the fat for quite a while. Preventing others from accessing it.
339 * To prevent us from going after the fat quite so much we use
340 * delayed writes, unless they specfied "synchronous" when the
341 * filesystem was mounted. If synch is asked for then use
342 * bwrite()'s and really slow things down.
344 if (fatbn != pmp->pm_fatblk || FAT12(pmp))
350 for (i = 1; i < pmp->pm_FATs; i++) {
351 fatbn += pmp->pm_FATsecs;
352 /* getblk() never fails */
353 bpn = getblk(pmp->pm_devvp, fatbn, bp->b_bcount,
355 bcopy(bp->b_data, bpn->b_data, bp->b_bcount);
356 /* Force the clean bit on in the other copies. */
358 ((u_int8_t *)bpn->b_data)[3] |= 0x80;
359 else if (cleanfat == 32)
360 ((u_int8_t *)bpn->b_data)[7] |= 0x08;
361 if (pmp->pm_mountp->mnt_flag & MNT_SYNCHRONOUS)
369 * Write out the first (or current) fat last.
371 if (pmp->pm_mountp->mnt_flag & MNT_SYNCHRONOUS)
378 * Updating entries in 12 bit fats is a pain in the butt.
380 * The following picture shows where nibbles go when moving from a 12 bit
381 * cluster number into the appropriate bytes in the FAT.
383 * byte m byte m+1 byte m+2
384 * +----+----+ +----+----+ +----+----+
385 * | 0 1 | | 2 3 | | 4 5 | FAT bytes
386 * +----+----+ +----+----+ +----+----+
388 * +----+----+----+ +----+----+----+
389 * | 3 0 1 | | 4 5 2 |
390 * +----+----+----+ +----+----+----+
391 * cluster n cluster n+1
393 * Where n is even. m = n + (n >> 2)
397 usemap_alloc(pmp, cn)
398 struct msdosfsmount *pmp;
402 MSDOSFS_ASSERT_MP_LOCKED(pmp);
404 KASSERT((pmp->pm_flags & MSDOSFSMNT_RONLY) == 0,
405 ("usemap_alloc on ro msdosfs mount"));
406 KASSERT((pmp->pm_inusemap[cn / N_INUSEBITS] & (1 << (cn % N_INUSEBITS)))
407 == 0, ("Allocating used sector %ld %ld %x", cn, cn % N_INUSEBITS,
408 (unsigned)pmp->pm_inusemap[cn / N_INUSEBITS]));
409 pmp->pm_inusemap[cn / N_INUSEBITS] |= 1 << (cn % N_INUSEBITS);
410 KASSERT(pmp->pm_freeclustercount > 0, ("usemap_alloc: too little"));
411 pmp->pm_freeclustercount--;
412 pmp->pm_flags |= MSDOSFS_FSIMOD;
417 struct msdosfsmount *pmp;
421 MSDOSFS_ASSERT_MP_LOCKED(pmp);
422 KASSERT((pmp->pm_flags & MSDOSFSMNT_RONLY) == 0,
423 ("usemap_free on ro msdosfs mount"));
424 pmp->pm_freeclustercount++;
425 pmp->pm_flags |= MSDOSFS_FSIMOD;
426 KASSERT((pmp->pm_inusemap[cn / N_INUSEBITS] & (1 << (cn % N_INUSEBITS)))
427 != 0, ("Freeing unused sector %ld %ld %x", cn, cn % N_INUSEBITS,
428 (unsigned)pmp->pm_inusemap[cn / N_INUSEBITS]));
429 pmp->pm_inusemap[cn / N_INUSEBITS] &= ~(1 << (cn % N_INUSEBITS));
433 clusterfree(pmp, cluster, oldcnp)
434 struct msdosfsmount *pmp;
441 error = fatentry(FAT_GET_AND_SET, pmp, cluster, &oldcn, MSDOSFSFREE);
445 * If the cluster was successfully marked free, then update
446 * the count of free clusters, and turn off the "allocated"
447 * bit in the "in use" cluster bit map.
449 MSDOSFS_LOCK_MP(pmp);
450 usemap_free(pmp, cluster);
451 MSDOSFS_UNLOCK_MP(pmp);
458 * Get or Set or 'Get and Set' the cluster'th entry in the fat.
460 * function - whether to get or set a fat entry
461 * pmp - address of the msdosfsmount structure for the filesystem
462 * whose fat is to be manipulated.
463 * cn - which cluster is of interest
464 * oldcontents - address of a word that is to receive the contents of the
465 * cluster'th entry if this is a get function
466 * newcontents - the new value to be written into the cluster'th element of
467 * the fat if this is a set function.
469 * This function can also be used to free a cluster by setting the fat entry
470 * for a cluster to 0.
472 * All copies of the fat are updated if this is a set function. NOTE: If
473 * fatentry() marks a cluster as free it does not update the inusemap in
474 * the msdosfsmount structure. This is left to the caller.
477 fatentry(function, pmp, cn, oldcontents, newcontents)
479 struct msdosfsmount *pmp;
486 u_long bn, bo, bsize, byteoffset;
490 printf("fatentry(func %d, pmp %p, clust %lu, oldcon %p, newcon %lx)\n",
491 function, pmp, cn, oldcontents, newcontents);
496 * Be sure they asked us to do something.
498 if ((function & (FAT_SET | FAT_GET)) == 0) {
500 printf("fatentry(): function code doesn't specify get or set\n");
506 * If they asked us to return a cluster number but didn't tell us
507 * where to put it, give them an error.
509 if ((function & FAT_GET) && oldcontents == NULL) {
511 printf("fatentry(): get function with no place to put result\n");
518 * Be sure the requested cluster is in the filesystem.
520 if (cn < CLUST_FIRST || cn > pmp->pm_maxcluster)
523 byteoffset = FATOFS(pmp, cn);
524 fatblock(pmp, byteoffset, &bn, &bsize, &bo);
525 error = bread(pmp->pm_devvp, bn, bsize, NOCRED, &bp);
531 if (function & FAT_GET) {
533 readcn = getulong(&bp->b_data[bo]);
535 readcn = getushort(&bp->b_data[bo]);
536 if (FAT12(pmp) & (cn & 1))
538 readcn &= pmp->pm_fatmask;
539 /* map reserved fat entries to same values for all fats */
540 if ((readcn | ~pmp->pm_fatmask) >= CLUST_RSRVD)
541 readcn |= ~pmp->pm_fatmask;
542 *oldcontents = readcn;
544 if (function & FAT_SET) {
545 switch (pmp->pm_fatmask) {
547 readcn = getushort(&bp->b_data[bo]);
550 readcn |= newcontents << 4;
553 readcn |= newcontents & 0xfff;
555 putushort(&bp->b_data[bo], readcn);
558 putushort(&bp->b_data[bo], newcontents);
562 * According to spec we have to retain the
563 * high order bits of the fat entry.
565 readcn = getulong(&bp->b_data[bo]);
566 readcn &= ~FAT32_MASK;
567 readcn |= newcontents & FAT32_MASK;
568 putulong(&bp->b_data[bo], readcn);
571 updatefats(pmp, bp, bn);
581 * Update a contiguous cluster chain
584 * start - first cluster of chain
585 * count - number of clusters in chain
586 * fillwith - what to write into fat entry of last cluster
589 fatchain(pmp, start, count, fillwith)
590 struct msdosfsmount *pmp;
596 u_long bn, bo, bsize, byteoffset, readcn, newc;
600 printf("fatchain(pmp %p, start %lu, count %lu, fillwith %lx)\n",
601 pmp, start, count, fillwith);
604 * Be sure the clusters are in the filesystem.
606 if (start < CLUST_FIRST || start + count - 1 > pmp->pm_maxcluster)
610 byteoffset = FATOFS(pmp, start);
611 fatblock(pmp, byteoffset, &bn, &bsize, &bo);
612 error = bread(pmp->pm_devvp, bn, bsize, NOCRED, &bp);
619 newc = --count > 0 ? start : fillwith;
620 switch (pmp->pm_fatmask) {
622 readcn = getushort(&bp->b_data[bo]);
625 readcn |= newc & 0xfff;
630 putushort(&bp->b_data[bo], readcn);
636 putushort(&bp->b_data[bo], newc);
640 readcn = getulong(&bp->b_data[bo]);
641 readcn &= ~pmp->pm_fatmask;
642 readcn |= newc & pmp->pm_fatmask;
643 putulong(&bp->b_data[bo], readcn);
650 updatefats(pmp, bp, bn);
657 * Check the length of a free cluster chain starting at start.
660 * start - start of chain
661 * count - maximum interesting length
664 chainlength(pmp, start, count)
665 struct msdosfsmount *pmp;
673 MSDOSFS_ASSERT_MP_LOCKED(pmp);
675 max_idx = pmp->pm_maxcluster / N_INUSEBITS;
676 idx = start / N_INUSEBITS;
677 start %= N_INUSEBITS;
678 map = pmp->pm_inusemap[idx];
679 map &= ~((1 << start) - 1);
681 len = ffs(map) - 1 - start;
682 return (len > count ? count : len);
684 len = N_INUSEBITS - start;
687 while (++idx <= max_idx) {
690 map = pmp->pm_inusemap[idx];
697 return (len > count ? count : len);
701 * Allocate contigous free clusters.
704 * start - start of cluster chain.
705 * count - number of clusters to allocate.
706 * fillwith - put this value into the fat entry for the
707 * last allocated cluster.
708 * retcluster - put the first allocated cluster's number here.
709 * got - how many clusters were actually allocated.
712 chainalloc(pmp, start, count, fillwith, retcluster, got)
713 struct msdosfsmount *pmp;
723 MSDOSFS_ASSERT_MP_LOCKED(pmp);
724 KASSERT((pmp->pm_flags & MSDOSFSMNT_RONLY) == 0,
725 ("chainalloc on ro msdosfs mount"));
727 for (cl = start, n = count; n-- > 0;)
728 usemap_alloc(pmp, cl++);
729 pmp->pm_nxtfree = start + count;
730 if (pmp->pm_nxtfree > pmp->pm_maxcluster)
731 pmp->pm_nxtfree = CLUST_FIRST;
732 pmp->pm_flags |= MSDOSFS_FSIMOD;
733 error = fatchain(pmp, start, count, fillwith);
735 for (cl = start, n = count; n-- > 0;)
736 usemap_free(pmp, cl++);
740 printf("clusteralloc(): allocated cluster chain at %lu (%lu clusters)\n",
751 * Allocate contiguous free clusters.
754 * start - preferred start of cluster chain.
755 * count - number of clusters requested.
756 * fillwith - put this value into the fat entry for the
757 * last allocated cluster.
758 * retcluster - put the first allocated cluster's number here.
759 * got - how many clusters were actually allocated.
762 clusteralloc(struct msdosfsmount *pmp, u_long start, u_long count,
763 u_long fillwith, u_long *retcluster, u_long *got)
767 MSDOSFS_LOCK_MP(pmp);
768 error = clusteralloc1(pmp, start, count, fillwith, retcluster, got);
769 MSDOSFS_UNLOCK_MP(pmp);
774 clusteralloc1(struct msdosfsmount *pmp, u_long start, u_long count,
775 u_long fillwith, u_long *retcluster, u_long *got)
778 u_long len, newst, foundl, cn, l;
779 u_long foundcn = 0; /* XXX: foundcn could be used unititialized */
782 MSDOSFS_ASSERT_MP_LOCKED(pmp);
785 printf("clusteralloc(): find %lu clusters\n", count);
788 if ((len = chainlength(pmp, start, count)) >= count)
789 return (chainalloc(pmp, start, count, fillwith, retcluster, got));
793 newst = pmp->pm_nxtfree;
796 for (cn = newst; cn <= pmp->pm_maxcluster;) {
797 idx = cn / N_INUSEBITS;
798 map = pmp->pm_inusemap[idx];
799 map |= (1 << (cn % N_INUSEBITS)) - 1;
800 if (map != FULL_RUN) {
801 cn = idx * N_INUSEBITS + ffs(map ^ FULL_RUN) - 1;
802 if ((l = chainlength(pmp, cn, count)) >= count)
803 return (chainalloc(pmp, cn, count, fillwith, retcluster, got));
811 cn += N_INUSEBITS - cn % N_INUSEBITS;
813 for (cn = 0; cn < newst;) {
814 idx = cn / N_INUSEBITS;
815 map = pmp->pm_inusemap[idx];
816 map |= (1 << (cn % N_INUSEBITS)) - 1;
817 if (map != FULL_RUN) {
818 cn = idx * N_INUSEBITS + ffs(map ^ FULL_RUN) - 1;
819 if ((l = chainlength(pmp, cn, count)) >= count)
820 return (chainalloc(pmp, cn, count, fillwith, retcluster, got));
828 cn += N_INUSEBITS - cn % N_INUSEBITS;
835 return (chainalloc(pmp, start, len, fillwith, retcluster, got));
837 return (chainalloc(pmp, foundcn, foundl, fillwith, retcluster, got));
842 * Free a chain of clusters.
844 * pmp - address of the msdosfs mount structure for the filesystem
845 * containing the cluster chain to be freed.
846 * startcluster - number of the 1st cluster in the chain of clusters to be
850 freeclusterchain(pmp, cluster)
851 struct msdosfsmount *pmp;
855 struct buf *bp = NULL;
856 u_long bn, bo, bsize, byteoffset;
857 u_long readcn, lbn = -1;
859 MSDOSFS_LOCK_MP(pmp);
860 while (cluster >= CLUST_FIRST && cluster <= pmp->pm_maxcluster) {
861 byteoffset = FATOFS(pmp, cluster);
862 fatblock(pmp, byteoffset, &bn, &bsize, &bo);
865 updatefats(pmp, bp, lbn);
866 error = bread(pmp->pm_devvp, bn, bsize, NOCRED, &bp);
869 MSDOSFS_UNLOCK_MP(pmp);
874 usemap_free(pmp, cluster);
875 switch (pmp->pm_fatmask) {
877 readcn = getushort(&bp->b_data[bo]);
879 cluster = readcn >> 4;
881 readcn |= MSDOSFSFREE << 4;
885 readcn |= MSDOSFSFREE & 0xfff;
887 putushort(&bp->b_data[bo], readcn);
890 cluster = getushort(&bp->b_data[bo]);
891 putushort(&bp->b_data[bo], MSDOSFSFREE);
894 cluster = getulong(&bp->b_data[bo]);
895 putulong(&bp->b_data[bo],
896 (MSDOSFSFREE & FAT32_MASK) | (cluster & ~FAT32_MASK));
899 cluster &= pmp->pm_fatmask;
900 if ((cluster | ~pmp->pm_fatmask) >= CLUST_RSRVD)
901 cluster |= pmp->pm_fatmask;
904 updatefats(pmp, bp, bn);
905 MSDOSFS_UNLOCK_MP(pmp);
910 * Read in fat blocks looking for free clusters. For every free cluster
911 * found turn off its corresponding bit in the pm_inusemap.
915 struct msdosfsmount *pmp;
917 struct buf *bp = NULL;
920 u_long bn, bo, bsize, byteoffset;
922 MSDOSFS_ASSERT_MP_LOCKED(pmp);
925 * Mark all clusters in use, we mark the free ones in the fat scan
928 for (cn = 0; cn < (pmp->pm_maxcluster + N_INUSEBITS) / N_INUSEBITS; cn++)
929 pmp->pm_inusemap[cn] = FULL_RUN;
932 * Figure how many free clusters are in the filesystem by ripping
933 * through the fat counting the number of entries whose content is
934 * zero. These represent free clusters.
936 pmp->pm_freeclustercount = 0;
937 for (cn = CLUST_FIRST; cn <= pmp->pm_maxcluster; cn++) {
938 byteoffset = FATOFS(pmp, cn);
939 bo = byteoffset % pmp->pm_fatblocksize;
941 /* Read new FAT block */
944 fatblock(pmp, byteoffset, &bn, &bsize, NULL);
945 error = bread(pmp->pm_devvp, bn, bsize, NOCRED, &bp);
952 readcn = getulong(&bp->b_data[bo]);
954 readcn = getushort(&bp->b_data[bo]);
955 if (FAT12(pmp) && (cn & 1))
957 readcn &= pmp->pm_fatmask;
959 if (readcn == CLUST_FREE)
960 usemap_free(pmp, cn);
968 * Allocate a new cluster and chain it onto the end of the file.
970 * dep - the file to extend
971 * count - number of clusters to allocate
972 * bpp - where to return the address of the buf header for the first new
974 * ncp - where to put cluster number of the first newly allocated cluster
975 * If this pointer is 0, do not return the cluster number.
978 * NOTE: This function is not responsible for turning on the DE_UPDATE bit of
979 * the de_flag field of the denode and it does not change the de_FileSize
980 * field. This is left for the caller to do.
983 extendfile(dep, count, bpp, ncp, flags)
993 struct msdosfsmount *pmp = dep->de_pmp;
998 * Don't try to extend the root directory
1000 if (dep->de_StartCluster == MSDOSFSROOT
1001 && (dep->de_Attributes & ATTR_DIRECTORY)) {
1002 #ifdef MSDOSFS_DEBUG
1003 printf("extendfile(): attempt to extend root directory\n");
1009 * If the "file's last cluster" cache entry is empty, and the file
1010 * is not empty, then fill the cache entry by calling pcbmap().
1012 if (dep->de_fc[FC_LASTFC].fc_frcn == FCE_EMPTY &&
1013 dep->de_StartCluster != 0) {
1014 error = pcbmap(dep, 0xffff, 0, &cn, 0);
1015 /* we expect it to return E2BIG */
1020 dep->de_fc[FC_NEXTTOLASTFC].fc_frcn =
1021 dep->de_fc[FC_LASTFC].fc_frcn;
1022 dep->de_fc[FC_NEXTTOLASTFC].fc_fsrcn =
1023 dep->de_fc[FC_LASTFC].fc_fsrcn;
1026 * Allocate a new cluster chain and cat onto the end of the
1028 * If the file is empty we make de_StartCluster point
1029 * to the new block. Note that de_StartCluster being
1030 * 0 is sufficient to be sure the file is empty since
1031 * we exclude attempts to extend the root directory
1032 * above, and the root dir is the only file with a
1033 * startcluster of 0 that has blocks allocated (sort
1036 if (dep->de_StartCluster == 0)
1039 cn = dep->de_fc[FC_LASTFC].fc_fsrcn + 1;
1040 error = clusteralloc(pmp, cn, count, CLUST_EOFE, &cn, &got);
1047 * Give them the filesystem relative cluster number if they want
1055 if (dep->de_StartCluster == 0) {
1056 dep->de_StartCluster = cn;
1059 error = fatentry(FAT_SET, pmp,
1060 dep->de_fc[FC_LASTFC].fc_fsrcn,
1063 clusterfree(pmp, cn, NULL);
1066 frcn = dep->de_fc[FC_LASTFC].fc_frcn + 1;
1070 * Update the "last cluster of the file" entry in the denode's fat
1073 fc_setcache(dep, FC_LASTFC, frcn + got - 1, cn + got - 1);
1075 if (flags & DE_CLEAR) {
1078 * Get the buf header for the new block of the file.
1080 if (dep->de_Attributes & ATTR_DIRECTORY)
1081 bp = getblk(pmp->pm_devvp,
1083 pmp->pm_bpcluster, 0, 0, 0);
1085 bp = getblk(DETOV(dep),
1087 pmp->pm_bpcluster, 0, 0, 0);
1089 * Do the bmap now, as in msdosfs_write
1095 if (bp->b_blkno == -1)
1096 panic("extendfile: pcbmap");
1098 bp->b_blkno = blkno;
1114 * Routine to mark a FAT16 or FAT32 volume as "clean" or "dirty" by
1115 * manipulating the upper bit of the FAT entry for cluster 1. Note that
1116 * this bit is not defined for FAT12 volumes, which are always assumed to
1119 * The fatentry() routine only works on cluster numbers that a file could
1120 * occupy, so it won't manipulate the entry for cluster 1. So we have to do
1121 * it here. The code was stolen from fatentry() and tailored for cluster 1.
1124 * pmp The MS-DOS volume to mark
1125 * dirty Non-zero if the volume should be marked dirty; zero if it
1126 * should be marked clean
1130 * EROFS Volume is read-only
1131 * ? (other errors from called routines)
1134 markvoldirty(struct msdosfsmount *pmp, int dirty)
1137 u_long bn, bo, bsize, byteoffset, fatval;
1141 * FAT12 does not support a "clean" bit, so don't do anything for
1147 /* Can't change the bit on a read-only filesystem. */
1148 if (pmp->pm_flags & MSDOSFSMNT_RONLY)
1152 * Fetch the block containing the FAT entry. It is given by the
1155 byteoffset = FATOFS(pmp, 1);
1156 fatblock(pmp, byteoffset, &bn, &bsize, &bo);
1157 error = bread(pmp->pm_devvp, bn, bsize, NOCRED, &bp);
1164 * Get the current value of the FAT entry and set/clear the relevant
1165 * bit. Dirty means clear the "clean" bit; clean means set the
1169 /* FAT32 uses bit 27. */
1170 fatval = getulong(&bp->b_data[bo]);
1172 fatval &= 0xF7FFFFFF;
1174 fatval |= 0x08000000;
1175 putulong(&bp->b_data[bo], fatval);
1177 /* Must be FAT16; use bit 15. */
1178 fatval = getushort(&bp->b_data[bo]);
1183 putushort(&bp->b_data[bo], fatval);
1186 /* Write out the modified FAT block synchronously. */
1187 return (bwrite(bp));