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>
66 static int fc_fileextends; /* # of file extends */
67 static int fc_lfcempty; /* # of time last file cluster cache entry
69 static int fc_bmapcalls; /* # of times pcbmap was called */
72 static int fc_lmdistance[LMMAX];/* counters for how far off the last
73 * cluster mapped entry was. */
74 static int fc_largedistance; /* off by more than LMMAX */
76 static int chainalloc(struct msdosfsmount *pmp, u_long start,
77 u_long count, u_long fillwith, u_long *retcluster,
79 static int chainlength(struct msdosfsmount *pmp, u_long start,
81 static void fatblock(struct msdosfsmount *pmp, u_long ofs, u_long *bnp,
82 u_long *sizep, u_long *bop);
83 static int fatchain(struct msdosfsmount *pmp, u_long start, u_long count,
85 static void fc_lookup(struct denode *dep, u_long findcn, u_long *frcnp,
87 static void updatefats(struct msdosfsmount *pmp, struct buf *bp,
90 usemap_alloc(struct msdosfsmount *pmp, u_long cn);
92 usemap_free(struct msdosfsmount *pmp, u_long cn);
95 fatblock(pmp, ofs, bnp, sizep, bop)
96 struct msdosfsmount *pmp;
104 bn = ofs / pmp->pm_fatblocksize * pmp->pm_fatblocksec;
105 size = min(pmp->pm_fatblocksec, pmp->pm_FATsecs - bn)
107 bn += pmp->pm_fatblk + pmp->pm_curfat * pmp->pm_FATsecs;
114 *bop = ofs % pmp->pm_fatblocksize;
118 * Map the logical cluster number of a file into a physical disk sector
119 * that is filesystem relative.
121 * dep - address of denode representing the file of interest
122 * findcn - file relative cluster whose filesystem relative cluster number
123 * and/or block number are/is to be found
124 * bnp - address of where to place the filesystem relative block number.
125 * If this pointer is null then don't return this quantity.
126 * cnp - address of where to place the filesystem relative cluster number.
127 * If this pointer is null then don't return this quantity.
129 * NOTE: Either bnp or cnp must be non-null.
130 * This function has one side effect. If the requested file relative cluster
131 * is beyond the end of file, then the actual number of clusters in the file
132 * is returned in *cnp. This is useful for determining how long a directory is.
133 * If cnp is null, nothing is returned.
136 pcbmap(dep, findcn, bnp, cnp, sp)
138 u_long findcn; /* file relative cluster to get */
139 daddr_t *bnp; /* returned filesys relative blk number */
140 u_long *cnp; /* returned cluster number */
141 int *sp; /* returned block size */
146 u_long prevcn = 0; /* XXX: prevcn could be used unititialized */
150 struct buf *bp = NULL;
152 struct msdosfsmount *pmp = dep->de_pmp;
158 * If they don't give us someplace to return a value then don't
159 * bother doing anything.
161 if (bnp == NULL && cnp == NULL && sp == NULL)
164 cn = dep->de_StartCluster;
166 * The "file" that makes up the root directory is contiguous,
167 * permanently allocated, of fixed size, and is not made up of
168 * clusters. If the cluster number is beyond the end of the root
169 * directory, then return the number of clusters in the file.
171 if (cn == MSDOSFSROOT) {
172 if (dep->de_Attributes & ATTR_DIRECTORY) {
173 if (de_cn2off(pmp, findcn) >= dep->de_FileSize) {
175 *cnp = de_bn2cn(pmp, pmp->pm_rootdirsize);
179 *bnp = pmp->pm_rootdirblk + de_cn2bn(pmp, findcn);
183 *sp = min(pmp->pm_bpcluster,
184 dep->de_FileSize - de_cn2off(pmp, findcn));
186 } else { /* just an empty file */
194 * All other files do I/O in cluster sized blocks
197 *sp = pmp->pm_bpcluster;
200 * Rummage around in the fat cache, maybe we can avoid tromping
201 * thru every fat entry for the file. And, keep track of how far
202 * off the cache was from where we wanted to be.
205 fc_lookup(dep, findcn, &i, &cn);
206 if ((bn = findcn - i) >= LMMAX)
212 * Handle all other files or directories the normal way.
214 for (; i < findcn; i++) {
216 * Stop with all reserved clusters, not just with EOF.
218 if ((cn | ~pmp->pm_fatmask) >= CLUST_RSRVD)
220 byteoffset = FATOFS(pmp, cn);
221 fatblock(pmp, byteoffset, &bn, &bsize, &bo);
225 error = bread(pmp->pm_devvp, bn, bsize, NOCRED, &bp);
239 cn = getulong(&bp->b_data[bo]);
241 cn = getushort(&bp->b_data[bo]);
242 if (FAT12(pmp) && (prevcn & 1))
244 cn &= pmp->pm_fatmask;
247 * Force the special cluster numbers
248 * to be the same for all cluster sizes
249 * to let the rest of msdosfs handle
250 * all cases the same.
252 if ((cn | ~pmp->pm_fatmask) >= CLUST_RSRVD)
253 cn |= ~pmp->pm_fatmask;
256 if (!MSDOSFSEOF(pmp, cn)) {
260 *bnp = cntobn(pmp, cn);
263 fc_setcache(dep, FC_LASTMAP, i, cn);
272 /* update last file cluster entry in the fat cache */
273 fc_setcache(dep, FC_LASTFC, i - 1, prevcn);
278 * Find the closest entry in the fat cache to the cluster we are looking
282 fc_lookup(dep, findcn, frcnp, fsrcnp)
290 struct fatcache *closest = 0;
292 for (i = 0; i < FC_SIZE; i++) {
293 cn = dep->de_fc[i].fc_frcn;
294 if (cn != FCE_EMPTY && cn <= findcn) {
295 if (closest == 0 || cn > closest->fc_frcn)
296 closest = &dep->de_fc[i];
300 *frcnp = closest->fc_frcn;
301 *fsrcnp = closest->fc_fsrcn;
306 * Purge the fat cache in denode dep of all entries relating to file
307 * relative cluster frcn and beyond.
315 struct fatcache *fcp;
318 for (i = 0; i < FC_SIZE; i++, fcp++) {
319 if (fcp->fc_frcn >= frcn)
320 fcp->fc_frcn = FCE_EMPTY;
326 * If mirroring the fat, update all copies, with the first copy as last.
327 * Else update only the current fat (ignoring the others).
329 * pmp - msdosfsmount structure for filesystem to update
330 * bp - addr of modified fat block
331 * fatbn - block number relative to begin of filesystem of the modified fat block.
334 updatefats(pmp, bp, fatbn)
335 struct msdosfsmount *pmp;
343 printf("updatefats(pmp %p, bp %p, fatbn %lu)\n", pmp, bp, fatbn);
347 * If we have an FSInfo block, update it.
349 if (pmp->pm_fsinfo) {
350 if (bread(pmp->pm_devvp, pmp->pm_fsinfo, pmp->pm_BytesPerSec,
351 NOCRED, &bpn) != 0) {
353 * Ignore the error, but turn off FSInfo update for the future.
358 struct fsinfo *fp = (struct fsinfo *)bpn->b_data;
360 putulong(fp->fsinfree, pmp->pm_freeclustercount);
361 putulong(fp->fsinxtfree, pmp->pm_nxtfree);
362 if (pmp->pm_flags & MSDOSFSMNT_WAITONFAT)
369 if (pmp->pm_flags & MSDOSFS_FATMIRROR) {
371 * Now copy the block(s) of the modified fat to the other copies of
372 * the fat and write them out. This is faster than reading in the
373 * other fats and then writing them back out. This could tie up
374 * the fat for quite a while. Preventing others from accessing it.
375 * To prevent us from going after the fat quite so much we use
376 * delayed writes, unless they specfied "synchronous" when the
377 * filesystem was mounted. If synch is asked for then use
378 * bwrite()'s and really slow things down.
380 for (i = 1; i < pmp->pm_FATs; i++) {
381 fatbn += pmp->pm_FATsecs;
382 /* getblk() never fails */
383 bpn = getblk(pmp->pm_devvp, fatbn, bp->b_bcount,
385 bcopy(bp->b_data, bpn->b_data, bp->b_bcount);
386 if (pmp->pm_flags & MSDOSFSMNT_WAITONFAT)
394 * Write out the first (or current) fat last.
396 if (pmp->pm_flags & MSDOSFSMNT_WAITONFAT)
401 * Maybe update fsinfo sector here?
406 * Updating entries in 12 bit fats is a pain in the butt.
408 * The following picture shows where nibbles go when moving from a 12 bit
409 * cluster number into the appropriate bytes in the FAT.
411 * byte m byte m+1 byte m+2
412 * +----+----+ +----+----+ +----+----+
413 * | 0 1 | | 2 3 | | 4 5 | FAT bytes
414 * +----+----+ +----+----+ +----+----+
416 * +----+----+----+ +----+----+----+
417 * | 3 0 1 | | 4 5 2 |
418 * +----+----+----+ +----+----+----+
419 * cluster n cluster n+1
421 * Where n is even. m = n + (n >> 2)
425 usemap_alloc(pmp, cn)
426 struct msdosfsmount *pmp;
430 pmp->pm_inusemap[cn / N_INUSEBITS] |= 1 << (cn % N_INUSEBITS);
431 pmp->pm_freeclustercount--;
436 struct msdosfsmount *pmp;
440 pmp->pm_freeclustercount++;
441 pmp->pm_inusemap[cn / N_INUSEBITS] &= ~(1 << (cn % N_INUSEBITS));
445 clusterfree(pmp, cluster, oldcnp)
446 struct msdosfsmount *pmp;
453 usemap_free(pmp, cluster);
454 error = fatentry(FAT_GET_AND_SET, pmp, cluster, &oldcn, MSDOSFSFREE);
456 usemap_alloc(pmp, cluster);
460 * If the cluster was successfully marked free, then update
461 * the count of free clusters, and turn off the "allocated"
462 * bit in the "in use" cluster bit map.
470 * Get or Set or 'Get and Set' the cluster'th entry in the fat.
472 * function - whether to get or set a fat entry
473 * pmp - address of the msdosfsmount structure for the filesystem
474 * whose fat is to be manipulated.
475 * cn - which cluster is of interest
476 * oldcontents - address of a word that is to receive the contents of the
477 * cluster'th entry if this is a get function
478 * newcontents - the new value to be written into the cluster'th element of
479 * the fat if this is a set function.
481 * This function can also be used to free a cluster by setting the fat entry
482 * for a cluster to 0.
484 * All copies of the fat are updated if this is a set function. NOTE: If
485 * fatentry() marks a cluster as free it does not update the inusemap in
486 * the msdosfsmount structure. This is left to the caller.
489 fatentry(function, pmp, cn, oldcontents, newcontents)
491 struct msdosfsmount *pmp;
498 u_long bn, bo, bsize, byteoffset;
502 printf("fatentry(func %d, pmp %p, clust %lu, oldcon %p, newcon %lx)\n",
503 function, pmp, cn, oldcontents, newcontents);
508 * Be sure they asked us to do something.
510 if ((function & (FAT_SET | FAT_GET)) == 0) {
511 printf("fatentry(): function code doesn't specify get or set\n");
516 * If they asked us to return a cluster number but didn't tell us
517 * where to put it, give them an error.
519 if ((function & FAT_GET) && oldcontents == NULL) {
520 printf("fatentry(): get function with no place to put result\n");
526 * Be sure the requested cluster is in the filesystem.
528 if (cn < CLUST_FIRST || cn > pmp->pm_maxcluster)
531 byteoffset = FATOFS(pmp, cn);
532 fatblock(pmp, byteoffset, &bn, &bsize, &bo);
533 error = bread(pmp->pm_devvp, bn, bsize, NOCRED, &bp);
539 if (function & FAT_GET) {
541 readcn = getulong(&bp->b_data[bo]);
543 readcn = getushort(&bp->b_data[bo]);
544 if (FAT12(pmp) & (cn & 1))
546 readcn &= pmp->pm_fatmask;
547 /* map reserved fat entries to same values for all fats */
548 if ((readcn | ~pmp->pm_fatmask) >= CLUST_RSRVD)
549 readcn |= ~pmp->pm_fatmask;
550 *oldcontents = readcn;
552 if (function & FAT_SET) {
553 switch (pmp->pm_fatmask) {
555 readcn = getushort(&bp->b_data[bo]);
558 readcn |= newcontents << 4;
561 readcn |= newcontents & 0xfff;
563 putushort(&bp->b_data[bo], readcn);
566 putushort(&bp->b_data[bo], newcontents);
570 * According to spec we have to retain the
571 * high order bits of the fat entry.
573 readcn = getulong(&bp->b_data[bo]);
574 readcn &= ~FAT32_MASK;
575 readcn |= newcontents & FAT32_MASK;
576 putulong(&bp->b_data[bo], readcn);
579 updatefats(pmp, bp, bn);
589 * Update a contiguous cluster chain
592 * start - first cluster of chain
593 * count - number of clusters in chain
594 * fillwith - what to write into fat entry of last cluster
597 fatchain(pmp, start, count, fillwith)
598 struct msdosfsmount *pmp;
604 u_long bn, bo, bsize, byteoffset, readcn, newc;
608 printf("fatchain(pmp %p, start %lu, count %lu, fillwith %lx)\n",
609 pmp, start, count, fillwith);
612 * Be sure the clusters are in the filesystem.
614 if (start < CLUST_FIRST || start + count - 1 > pmp->pm_maxcluster)
618 byteoffset = FATOFS(pmp, start);
619 fatblock(pmp, byteoffset, &bn, &bsize, &bo);
620 error = bread(pmp->pm_devvp, bn, bsize, NOCRED, &bp);
627 newc = --count > 0 ? start : fillwith;
628 switch (pmp->pm_fatmask) {
630 readcn = getushort(&bp->b_data[bo]);
633 readcn |= newc & 0xfff;
638 putushort(&bp->b_data[bo], readcn);
644 putushort(&bp->b_data[bo], newc);
648 readcn = getulong(&bp->b_data[bo]);
649 readcn &= ~pmp->pm_fatmask;
650 readcn |= newc & pmp->pm_fatmask;
651 putulong(&bp->b_data[bo], readcn);
658 updatefats(pmp, bp, bn);
665 * Check the length of a free cluster chain starting at start.
668 * start - start of chain
669 * count - maximum interesting length
672 chainlength(pmp, start, count)
673 struct msdosfsmount *pmp;
681 max_idx = pmp->pm_maxcluster / N_INUSEBITS;
682 idx = start / N_INUSEBITS;
683 start %= N_INUSEBITS;
684 map = pmp->pm_inusemap[idx];
685 map &= ~((1 << start) - 1);
687 len = ffs(map) - 1 - start;
688 return (len > count ? count : len);
690 len = N_INUSEBITS - start;
693 while (++idx <= max_idx) {
696 map = pmp->pm_inusemap[idx];
703 return (len > count ? count : len);
707 * Allocate contigous free clusters.
710 * start - start of cluster chain.
711 * count - number of clusters to allocate.
712 * fillwith - put this value into the fat entry for the
713 * last allocated cluster.
714 * retcluster - put the first allocated cluster's number here.
715 * got - how many clusters were actually allocated.
718 chainalloc(pmp, start, count, fillwith, retcluster, got)
719 struct msdosfsmount *pmp;
729 for (cl = start, n = count; n-- > 0;)
730 usemap_alloc(pmp, cl++);
732 error = fatchain(pmp, start, count, fillwith);
736 printf("clusteralloc(): allocated cluster chain at %lu (%lu clusters)\n",
743 pmp->pm_nxtfree = start + count;
744 if (pmp->pm_nxtfree > pmp->pm_maxcluster)
745 pmp->pm_nxtfree = CLUST_FIRST;
750 * Allocate contiguous free clusters.
753 * start - preferred start of cluster chain.
754 * count - number of clusters requested.
755 * fillwith - put this value into the fat entry for the
756 * last allocated cluster.
757 * retcluster - put the first allocated cluster's number here.
758 * got - how many clusters were actually allocated.
761 clusteralloc(pmp, start, count, fillwith, retcluster, got)
762 struct msdosfsmount *pmp;
770 u_long len, newst, foundl, cn, l;
771 u_long foundcn = 0; /* XXX: foundcn could be used unititialized */
775 printf("clusteralloc(): find %lu clusters\n", count);
778 if ((len = chainlength(pmp, start, count)) >= count)
779 return (chainalloc(pmp, start, count, fillwith, retcluster, got));
783 newst = pmp->pm_nxtfree;
786 for (cn = newst; cn <= pmp->pm_maxcluster;) {
787 idx = cn / N_INUSEBITS;
788 map = pmp->pm_inusemap[idx];
789 map |= (1 << (cn % N_INUSEBITS)) - 1;
790 if (map != (u_int)-1) {
791 cn = idx * N_INUSEBITS + ffs(map^(u_int)-1) - 1;
792 if ((l = chainlength(pmp, cn, count)) >= count)
793 return (chainalloc(pmp, cn, count, fillwith, retcluster, got));
801 cn += N_INUSEBITS - cn % N_INUSEBITS;
803 for (cn = 0; cn < newst;) {
804 idx = cn / N_INUSEBITS;
805 map = pmp->pm_inusemap[idx];
806 map |= (1 << (cn % N_INUSEBITS)) - 1;
807 if (map != (u_int)-1) {
808 cn = idx * N_INUSEBITS + ffs(map^(u_int)-1) - 1;
809 if ((l = chainlength(pmp, cn, count)) >= count)
810 return (chainalloc(pmp, cn, count, fillwith, retcluster, got));
818 cn += N_INUSEBITS - cn % N_INUSEBITS;
825 return (chainalloc(pmp, start, len, fillwith, retcluster, got));
827 return (chainalloc(pmp, foundcn, foundl, fillwith, retcluster, got));
832 * Free a chain of clusters.
834 * pmp - address of the msdosfs mount structure for the filesystem
835 * containing the cluster chain to be freed.
836 * startcluster - number of the 1st cluster in the chain of clusters to be
840 freeclusterchain(pmp, cluster)
841 struct msdosfsmount *pmp;
845 struct buf *bp = NULL;
846 u_long bn, bo, bsize, byteoffset;
847 u_long readcn, lbn = -1;
849 while (cluster >= CLUST_FIRST && cluster <= pmp->pm_maxcluster) {
850 byteoffset = FATOFS(pmp, cluster);
851 fatblock(pmp, byteoffset, &bn, &bsize, &bo);
854 updatefats(pmp, bp, lbn);
855 error = bread(pmp->pm_devvp, bn, bsize, NOCRED, &bp);
862 usemap_free(pmp, cluster);
863 switch (pmp->pm_fatmask) {
865 readcn = getushort(&bp->b_data[bo]);
867 cluster = readcn >> 4;
869 readcn |= MSDOSFSFREE << 4;
873 readcn |= MSDOSFSFREE & 0xfff;
875 putushort(&bp->b_data[bo], readcn);
878 cluster = getushort(&bp->b_data[bo]);
879 putushort(&bp->b_data[bo], MSDOSFSFREE);
882 cluster = getulong(&bp->b_data[bo]);
883 putulong(&bp->b_data[bo],
884 (MSDOSFSFREE & FAT32_MASK) | (cluster & ~FAT32_MASK));
887 cluster &= pmp->pm_fatmask;
888 if ((cluster | ~pmp->pm_fatmask) >= CLUST_RSRVD)
889 cluster |= pmp->pm_fatmask;
892 updatefats(pmp, bp, bn);
897 * Read in fat blocks looking for free clusters. For every free cluster
898 * found turn off its corresponding bit in the pm_inusemap.
902 struct msdosfsmount *pmp;
904 struct buf *bp = NULL;
907 u_long bn, bo, bsize, byteoffset;
910 * Mark all clusters in use, we mark the free ones in the fat scan
913 for (cn = 0; cn < (pmp->pm_maxcluster + N_INUSEBITS) / N_INUSEBITS; cn++)
914 pmp->pm_inusemap[cn] = (u_int)-1;
917 * Figure how many free clusters are in the filesystem by ripping
918 * through the fat counting the number of entries whose content is
919 * zero. These represent free clusters.
921 pmp->pm_freeclustercount = 0;
922 for (cn = CLUST_FIRST; cn <= pmp->pm_maxcluster; cn++) {
923 byteoffset = FATOFS(pmp, cn);
924 bo = byteoffset % pmp->pm_fatblocksize;
926 /* Read new FAT block */
929 fatblock(pmp, byteoffset, &bn, &bsize, NULL);
930 error = bread(pmp->pm_devvp, bn, bsize, NOCRED, &bp);
937 readcn = getulong(&bp->b_data[bo]);
939 readcn = getushort(&bp->b_data[bo]);
940 if (FAT12(pmp) && (cn & 1))
942 readcn &= pmp->pm_fatmask;
945 usemap_free(pmp, cn);
952 * Allocate a new cluster and chain it onto the end of the file.
954 * dep - the file to extend
955 * count - number of clusters to allocate
956 * bpp - where to return the address of the buf header for the first new
958 * ncp - where to put cluster number of the first newly allocated cluster
959 * If this pointer is 0, do not return the cluster number.
962 * NOTE: This function is not responsible for turning on the DE_UPDATE bit of
963 * the de_flag field of the denode and it does not change the de_FileSize
964 * field. This is left for the caller to do.
967 extendfile(dep, count, bpp, ncp, flags)
977 struct msdosfsmount *pmp = dep->de_pmp;
982 * Don't try to extend the root directory
984 if (dep->de_StartCluster == MSDOSFSROOT
985 && (dep->de_Attributes & ATTR_DIRECTORY)) {
986 printf("extendfile(): attempt to extend root directory\n");
991 * If the "file's last cluster" cache entry is empty, and the file
992 * is not empty, then fill the cache entry by calling pcbmap().
995 if (dep->de_fc[FC_LASTFC].fc_frcn == FCE_EMPTY &&
996 dep->de_StartCluster != 0) {
998 error = pcbmap(dep, 0xffff, 0, &cn, 0);
999 /* we expect it to return E2BIG */
1004 dep->de_fc[FC_NEXTTOLASTFC].fc_frcn =
1005 dep->de_fc[FC_LASTFC].fc_frcn;
1006 dep->de_fc[FC_NEXTTOLASTFC].fc_fsrcn =
1007 dep->de_fc[FC_LASTFC].fc_fsrcn;
1010 * Allocate a new cluster chain and cat onto the end of the
1011 * file. * If the file is empty we make de_StartCluster point
1012 * to the new block. Note that de_StartCluster being 0 is
1013 * sufficient to be sure the file is empty since we exclude
1014 * attempts to extend the root directory above, and the root
1015 * dir is the only file with a startcluster of 0 that has
1016 * blocks allocated (sort of).
1018 if (dep->de_StartCluster == 0)
1021 cn = dep->de_fc[FC_LASTFC].fc_fsrcn + 1;
1022 error = clusteralloc(pmp, cn, count, CLUST_EOFE, &cn, &got);
1029 * Give them the filesystem relative cluster number if they want
1037 if (dep->de_StartCluster == 0) {
1038 dep->de_StartCluster = cn;
1041 error = fatentry(FAT_SET, pmp,
1042 dep->de_fc[FC_LASTFC].fc_fsrcn,
1045 clusterfree(pmp, cn, NULL);
1048 frcn = dep->de_fc[FC_LASTFC].fc_frcn + 1;
1052 * Update the "last cluster of the file" entry in the denode's fat
1055 fc_setcache(dep, FC_LASTFC, frcn + got - 1, cn + got - 1);
1057 if (flags & DE_CLEAR) {
1060 * Get the buf header for the new block of the file.
1062 if (dep->de_Attributes & ATTR_DIRECTORY)
1063 bp = getblk(pmp->pm_devvp,
1065 pmp->pm_bpcluster, 0, 0, 0);
1067 bp = getblk(DETOV(dep),
1069 pmp->pm_bpcluster, 0, 0, 0);
1071 * Do the bmap now, as in msdosfs_write
1077 if (bp->b_blkno == -1)
1078 panic("extendfile: pcbmap");
1080 bp->b_blkno = blkno;
1096 * Routine to mark a FAT16 or FAT32 volume as "clean" or "dirty" by
1097 * manipulating the upper bit of the FAT entry for cluster 1. Note that
1098 * this bit is not defined for FAT12 volumes, which are always assumed to
1101 * The fatentry() routine only works on cluster numbers that a file could
1102 * occupy, so it won't manipulate the entry for cluster 1. So we have to do
1103 * it here. The code was stolen from fatentry() and tailored for cluster 1.
1106 * pmp The MS-DOS volume to mark
1107 * dirty Non-zero if the volume should be marked dirty; zero if it
1108 * should be marked clean
1112 * EROFS Volume is read-only
1113 * ? (other errors from called routines)
1116 markvoldirty(struct msdosfsmount *pmp, int dirty)
1119 u_long bn, bo, bsize, byteoffset, fatval;
1123 * FAT12 does not support a "clean" bit, so don't do anything for
1129 /* Can't change the bit on a read-only filesystem. */
1130 if (pmp->pm_flags & MSDOSFSMNT_RONLY)
1134 * Fetch the block containing the FAT entry. It is given by the
1137 byteoffset = FATOFS(pmp, 1);
1138 fatblock(pmp, byteoffset, &bn, &bsize, &bo);
1139 error = bread(pmp->pm_devvp, bn, bsize, NOCRED, &bp);
1146 * Get the current value of the FAT entry and set/clear the relevant
1147 * bit. Dirty means clear the "clean" bit; clean means set the
1151 /* FAT32 uses bit 27. */
1152 fatval = getulong(&bp->b_data[bo]);
1154 fatval &= 0xF7FFFFFF;
1156 fatval |= 0x08000000;
1157 putulong(&bp->b_data[bo], fatval);
1159 /* Must be FAT16; use bit 15. */
1160 fatval = getushort(&bp->b_data[bo]);
1165 putushort(&bp->b_data[bo], fatval);
1168 /* Write out the modified FAT block synchronously. */
1169 return (bwrite(bp));