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 */
75 static int fc_wherefrom, fc_whereto, fc_lastclust;
76 static int pm_fatblocksize;
78 static int chainalloc(struct msdosfsmount *pmp, u_long start,
79 u_long count, u_long fillwith, u_long *retcluster,
81 static int chainlength(struct msdosfsmount *pmp, u_long start,
83 static void fatblock(struct msdosfsmount *pmp, u_long ofs, u_long *bnp,
84 u_long *sizep, u_long *bop);
85 static int fatchain(struct msdosfsmount *pmp, u_long start, u_long count,
87 static void fc_lookup(struct denode *dep, u_long findcn, u_long *frcnp,
89 static void updatefats(struct msdosfsmount *pmp, struct buf *bp,
92 usemap_alloc(struct msdosfsmount *pmp, u_long cn);
94 usemap_free(struct msdosfsmount *pmp, u_long cn);
97 fatblock(pmp, ofs, bnp, sizep, bop)
98 struct msdosfsmount *pmp;
106 bn = ofs / pmp->pm_fatblocksize * pmp->pm_fatblocksec;
107 size = min(pmp->pm_fatblocksec, pmp->pm_FATsecs - bn)
109 bn += pmp->pm_fatblk + pmp->pm_curfat * pmp->pm_FATsecs;
116 *bop = ofs % pmp->pm_fatblocksize;
117 pm_fatblocksize = pmp->pm_fatblocksize;
121 * Map the logical cluster number of a file into a physical disk sector
122 * that is filesystem relative.
124 * dep - address of denode representing the file of interest
125 * findcn - file relative cluster whose filesystem relative cluster number
126 * and/or block number are/is to be found
127 * bnp - address of where to place the filesystem relative block number.
128 * If this pointer is null then don't return this quantity.
129 * cnp - address of where to place the filesystem relative cluster number.
130 * If this pointer is null then don't return this quantity.
132 * NOTE: Either bnp or cnp must be non-null.
133 * This function has one side effect. If the requested file relative cluster
134 * is beyond the end of file, then the actual number of clusters in the file
135 * is returned in *cnp. This is useful for determining how long a directory is.
136 * If cnp is null, nothing is returned.
139 pcbmap(dep, findcn, bnp, cnp, sp)
141 u_long findcn; /* file relative cluster to get */
142 daddr_t *bnp; /* returned filesys relative blk number */
143 u_long *cnp; /* returned cluster number */
144 int *sp; /* returned block size */
149 u_long prevcn = 0; /* XXX: prevcn could be used unititialized */
153 struct buf *bp = NULL;
155 struct msdosfsmount *pmp = dep->de_pmp;
161 * If they don't give us someplace to return a value then don't
162 * bother doing anything.
164 if (bnp == NULL && cnp == NULL && sp == NULL)
167 cn = dep->de_StartCluster;
169 * The "file" that makes up the root directory is contiguous,
170 * permanently allocated, of fixed size, and is not made up of
171 * clusters. If the cluster number is beyond the end of the root
172 * directory, then return the number of clusters in the file.
174 if (cn == MSDOSFSROOT) {
175 if (dep->de_Attributes & ATTR_DIRECTORY) {
176 if (de_cn2off(pmp, findcn) >= dep->de_FileSize) {
178 *cnp = de_bn2cn(pmp, pmp->pm_rootdirsize);
182 *bnp = pmp->pm_rootdirblk + de_cn2bn(pmp, findcn);
186 *sp = min(pmp->pm_bpcluster,
187 dep->de_FileSize - de_cn2off(pmp, findcn));
189 } else { /* just an empty file */
197 * All other files do I/O in cluster sized blocks
200 *sp = pmp->pm_bpcluster;
203 * Rummage around in the fat cache, maybe we can avoid tromping
204 * thru every fat entry for the file. And, keep track of how far
205 * off the cache was from where we wanted to be.
208 fc_lookup(dep, findcn, &i, &cn);
209 if ((bn = findcn - i) >= LMMAX) {
213 fc_lastclust = dep->de_fc[FC_LASTFC].fc_frcn;
218 * Handle all other files or directories the normal way.
220 for (; i < findcn; i++) {
222 * Stop with all reserved clusters, not just with EOF.
224 if ((cn | ~pmp->pm_fatmask) >= CLUST_RSRVD)
226 byteoffset = FATOFS(pmp, cn);
227 fatblock(pmp, byteoffset, &bn, &bsize, &bo);
231 error = bread(pmp->pm_devvp, bn, bsize, NOCRED, &bp);
245 cn = getulong(&bp->b_data[bo]);
247 cn = getushort(&bp->b_data[bo]);
248 if (FAT12(pmp) && (prevcn & 1))
250 cn &= pmp->pm_fatmask;
253 * Force the special cluster numbers
254 * to be the same for all cluster sizes
255 * to let the rest of msdosfs handle
256 * all cases the same.
258 if ((cn | ~pmp->pm_fatmask) >= CLUST_RSRVD)
259 cn |= ~pmp->pm_fatmask;
262 if (!MSDOSFSEOF(pmp, cn)) {
266 *bnp = cntobn(pmp, cn);
269 fc_setcache(dep, FC_LASTMAP, i, cn);
278 /* update last file cluster entry in the fat cache */
279 fc_setcache(dep, FC_LASTFC, i - 1, prevcn);
284 * Find the closest entry in the fat cache to the cluster we are looking
288 fc_lookup(dep, findcn, frcnp, fsrcnp)
296 struct fatcache *closest = 0;
298 for (i = 0; i < FC_SIZE; i++) {
299 cn = dep->de_fc[i].fc_frcn;
300 if (cn != FCE_EMPTY && cn <= findcn) {
301 if (closest == 0 || cn > closest->fc_frcn)
302 closest = &dep->de_fc[i];
306 *frcnp = closest->fc_frcn;
307 *fsrcnp = closest->fc_fsrcn;
312 * Purge the fat cache in denode dep of all entries relating to file
313 * relative cluster frcn and beyond.
321 struct fatcache *fcp;
324 for (i = 0; i < FC_SIZE; i++, fcp++) {
325 if (fcp->fc_frcn >= frcn)
326 fcp->fc_frcn = FCE_EMPTY;
332 * If mirroring the fat, update all copies, with the first copy as last.
333 * Else update only the current fat (ignoring the others).
335 * pmp - msdosfsmount structure for filesystem to update
336 * bp - addr of modified fat block
337 * fatbn - block number relative to begin of filesystem of the modified fat block.
340 updatefats(pmp, bp, fatbn)
341 struct msdosfsmount *pmp;
349 printf("updatefats(pmp %p, bp %p, fatbn %lu)\n", pmp, bp, fatbn);
353 * If we have an FSInfo block, update it.
355 if (pmp->pm_fsinfo) {
356 if (bread(pmp->pm_devvp, pmp->pm_fsinfo, pmp->pm_BytesPerSec,
357 NOCRED, &bpn) != 0) {
359 * Ignore the error, but turn off FSInfo update for the future.
364 struct fsinfo *fp = (struct fsinfo *)bpn->b_data;
366 putulong(fp->fsinfree, pmp->pm_freeclustercount);
367 putulong(fp->fsinxtfree, pmp->pm_nxtfree);
368 if (pmp->pm_flags & MSDOSFSMNT_WAITONFAT)
375 if (pmp->pm_flags & MSDOSFS_FATMIRROR) {
377 * Now copy the block(s) of the modified fat to the other copies of
378 * the fat and write them out. This is faster than reading in the
379 * other fats and then writing them back out. This could tie up
380 * the fat for quite a while. Preventing others from accessing it.
381 * To prevent us from going after the fat quite so much we use
382 * delayed writes, unless they specfied "synchronous" when the
383 * filesystem was mounted. If synch is asked for then use
384 * bwrite()'s and really slow things down.
386 for (i = 1; i < pmp->pm_FATs; i++) {
387 fatbn += pmp->pm_FATsecs;
388 /* getblk() never fails */
389 bpn = getblk(pmp->pm_devvp, fatbn, bp->b_bcount,
391 bcopy(bp->b_data, bpn->b_data, bp->b_bcount);
392 if (pmp->pm_flags & MSDOSFSMNT_WAITONFAT)
400 * Write out the first (or current) fat last.
402 if (pmp->pm_flags & MSDOSFSMNT_WAITONFAT)
407 * Maybe update fsinfo sector here?
412 * Updating entries in 12 bit fats is a pain in the butt.
414 * The following picture shows where nibbles go when moving from a 12 bit
415 * cluster number into the appropriate bytes in the FAT.
417 * byte m byte m+1 byte m+2
418 * +----+----+ +----+----+ +----+----+
419 * | 0 1 | | 2 3 | | 4 5 | FAT bytes
420 * +----+----+ +----+----+ +----+----+
422 * +----+----+----+ +----+----+----+
423 * | 3 0 1 | | 4 5 2 |
424 * +----+----+----+ +----+----+----+
425 * cluster n cluster n+1
427 * Where n is even. m = n + (n >> 2)
431 usemap_alloc(pmp, cn)
432 struct msdosfsmount *pmp;
436 pmp->pm_inusemap[cn / N_INUSEBITS] |= 1 << (cn % N_INUSEBITS);
437 pmp->pm_freeclustercount--;
442 struct msdosfsmount *pmp;
446 pmp->pm_freeclustercount++;
447 pmp->pm_inusemap[cn / N_INUSEBITS] &= ~(1 << (cn % N_INUSEBITS));
451 clusterfree(pmp, cluster, oldcnp)
452 struct msdosfsmount *pmp;
459 usemap_free(pmp, cluster);
460 error = fatentry(FAT_GET_AND_SET, pmp, cluster, &oldcn, MSDOSFSFREE);
462 usemap_alloc(pmp, cluster);
466 * If the cluster was successfully marked free, then update
467 * the count of free clusters, and turn off the "allocated"
468 * bit in the "in use" cluster bit map.
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) {
517 printf("fatentry(): function code doesn't specify get or set\n");
522 * If they asked us to return a cluster number but didn't tell us
523 * where to put it, give them an error.
525 if ((function & FAT_GET) && oldcontents == NULL) {
526 printf("fatentry(): get function with no place to put result\n");
532 * Be sure the requested cluster is in the filesystem.
534 if (cn < CLUST_FIRST || cn > pmp->pm_maxcluster)
537 byteoffset = FATOFS(pmp, cn);
538 fatblock(pmp, byteoffset, &bn, &bsize, &bo);
539 error = bread(pmp->pm_devvp, bn, bsize, NOCRED, &bp);
545 if (function & FAT_GET) {
547 readcn = getulong(&bp->b_data[bo]);
549 readcn = getushort(&bp->b_data[bo]);
550 if (FAT12(pmp) & (cn & 1))
552 readcn &= pmp->pm_fatmask;
553 /* map reserved fat entries to same values for all fats */
554 if ((readcn | ~pmp->pm_fatmask) >= CLUST_RSRVD)
555 readcn |= ~pmp->pm_fatmask;
556 *oldcontents = readcn;
558 if (function & FAT_SET) {
559 switch (pmp->pm_fatmask) {
561 readcn = getushort(&bp->b_data[bo]);
564 readcn |= newcontents << 4;
567 readcn |= newcontents & 0xfff;
569 putushort(&bp->b_data[bo], readcn);
572 putushort(&bp->b_data[bo], newcontents);
576 * According to spec we have to retain the
577 * high order bits of the fat entry.
579 readcn = getulong(&bp->b_data[bo]);
580 readcn &= ~FAT32_MASK;
581 readcn |= newcontents & FAT32_MASK;
582 putulong(&bp->b_data[bo], readcn);
585 updatefats(pmp, bp, bn);
595 * Update a contiguous cluster chain
598 * start - first cluster of chain
599 * count - number of clusters in chain
600 * fillwith - what to write into fat entry of last cluster
603 fatchain(pmp, start, count, fillwith)
604 struct msdosfsmount *pmp;
610 u_long bn, bo, bsize, byteoffset, readcn, newc;
614 printf("fatchain(pmp %p, start %lu, count %lu, fillwith %lx)\n",
615 pmp, start, count, fillwith);
618 * Be sure the clusters are in the filesystem.
620 if (start < CLUST_FIRST || start + count - 1 > pmp->pm_maxcluster)
624 byteoffset = FATOFS(pmp, start);
625 fatblock(pmp, byteoffset, &bn, &bsize, &bo);
626 error = bread(pmp->pm_devvp, bn, bsize, NOCRED, &bp);
633 newc = --count > 0 ? start : fillwith;
634 switch (pmp->pm_fatmask) {
636 readcn = getushort(&bp->b_data[bo]);
639 readcn |= newc & 0xfff;
644 putushort(&bp->b_data[bo], readcn);
650 putushort(&bp->b_data[bo], newc);
654 readcn = getulong(&bp->b_data[bo]);
655 readcn &= ~pmp->pm_fatmask;
656 readcn |= newc & pmp->pm_fatmask;
657 putulong(&bp->b_data[bo], readcn);
664 updatefats(pmp, bp, bn);
671 * Check the length of a free cluster chain starting at start.
674 * start - start of chain
675 * count - maximum interesting length
678 chainlength(pmp, start, count)
679 struct msdosfsmount *pmp;
687 max_idx = pmp->pm_maxcluster / N_INUSEBITS;
688 idx = start / N_INUSEBITS;
689 start %= N_INUSEBITS;
690 map = pmp->pm_inusemap[idx];
691 map &= ~((1 << start) - 1);
693 len = ffs(map) - 1 - start;
694 return (len > count ? count : len);
696 len = N_INUSEBITS - start;
699 while (++idx <= max_idx) {
702 map = pmp->pm_inusemap[idx];
709 return (len > count ? count : len);
713 * Allocate contigous free clusters.
716 * start - start of cluster chain.
717 * count - number of clusters to allocate.
718 * fillwith - put this value into the fat entry for the
719 * last allocated cluster.
720 * retcluster - put the first allocated cluster's number here.
721 * got - how many clusters were actually allocated.
724 chainalloc(pmp, start, count, fillwith, retcluster, got)
725 struct msdosfsmount *pmp;
735 for (cl = start, n = count; n-- > 0;)
736 usemap_alloc(pmp, cl++);
738 error = fatchain(pmp, start, count, fillwith);
742 printf("clusteralloc(): allocated cluster chain at %lu (%lu clusters)\n",
749 pmp->pm_nxtfree = start + count;
750 if (pmp->pm_nxtfree > pmp->pm_maxcluster)
751 pmp->pm_nxtfree = CLUST_FIRST;
756 * Allocate contiguous free clusters.
759 * start - preferred start of cluster chain.
760 * count - number of clusters requested.
761 * fillwith - put this value into the fat entry for the
762 * last allocated cluster.
763 * retcluster - put the first allocated cluster's number here.
764 * got - how many clusters were actually allocated.
767 clusteralloc(pmp, start, count, fillwith, retcluster, got)
768 struct msdosfsmount *pmp;
776 u_long len, newst, foundl, cn, l;
777 u_long foundcn = 0; /* XXX: foundcn could be used unititialized */
781 printf("clusteralloc(): find %lu clusters\n", count);
784 if ((len = chainlength(pmp, start, count)) >= count)
785 return (chainalloc(pmp, start, count, fillwith, retcluster, got));
789 newst = pmp->pm_nxtfree;
792 for (cn = newst; cn <= pmp->pm_maxcluster;) {
793 idx = cn / N_INUSEBITS;
794 map = pmp->pm_inusemap[idx];
795 map |= (1 << (cn % N_INUSEBITS)) - 1;
796 if (map != (u_int)-1) {
797 cn = idx * N_INUSEBITS + ffs(map^(u_int)-1) - 1;
798 if ((l = chainlength(pmp, cn, count)) >= count)
799 return (chainalloc(pmp, cn, count, fillwith, retcluster, got));
807 cn += N_INUSEBITS - cn % N_INUSEBITS;
809 for (cn = 0; cn < newst;) {
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;
831 return (chainalloc(pmp, start, len, fillwith, retcluster, got));
833 return (chainalloc(pmp, foundcn, foundl, fillwith, retcluster, got));
838 * Free a chain of clusters.
840 * pmp - address of the msdosfs mount structure for the filesystem
841 * containing the cluster chain to be freed.
842 * startcluster - number of the 1st cluster in the chain of clusters to be
846 freeclusterchain(pmp, cluster)
847 struct msdosfsmount *pmp;
851 struct buf *bp = NULL;
852 u_long bn, bo, bsize, byteoffset;
853 u_long readcn, lbn = -1;
855 while (cluster >= CLUST_FIRST && cluster <= pmp->pm_maxcluster) {
856 byteoffset = FATOFS(pmp, cluster);
857 fatblock(pmp, byteoffset, &bn, &bsize, &bo);
860 updatefats(pmp, bp, lbn);
861 error = bread(pmp->pm_devvp, bn, bsize, NOCRED, &bp);
868 usemap_free(pmp, cluster);
869 switch (pmp->pm_fatmask) {
871 readcn = getushort(&bp->b_data[bo]);
873 cluster = readcn >> 4;
875 readcn |= MSDOSFSFREE << 4;
879 readcn |= MSDOSFSFREE & 0xfff;
881 putushort(&bp->b_data[bo], readcn);
884 cluster = getushort(&bp->b_data[bo]);
885 putushort(&bp->b_data[bo], MSDOSFSFREE);
888 cluster = getulong(&bp->b_data[bo]);
889 putulong(&bp->b_data[bo],
890 (MSDOSFSFREE & FAT32_MASK) | (cluster & ~FAT32_MASK));
893 cluster &= pmp->pm_fatmask;
894 if ((cluster | ~pmp->pm_fatmask) >= CLUST_RSRVD)
895 cluster |= pmp->pm_fatmask;
898 updatefats(pmp, bp, bn);
903 * Read in fat blocks looking for free clusters. For every free cluster
904 * found turn off its corresponding bit in the pm_inusemap.
908 struct msdosfsmount *pmp;
910 struct buf *bp = NULL;
913 u_long bn, bo, bsize, byteoffset;
916 * Mark all clusters in use, we mark the free ones in the fat scan
919 for (cn = 0; cn < (pmp->pm_maxcluster + N_INUSEBITS) / N_INUSEBITS; cn++)
920 pmp->pm_inusemap[cn] = (u_int)-1;
923 * Figure how many free clusters are in the filesystem by ripping
924 * through the fat counting the number of entries whose content is
925 * zero. These represent free clusters.
927 pmp->pm_freeclustercount = 0;
928 for (cn = CLUST_FIRST; cn <= pmp->pm_maxcluster; cn++) {
929 byteoffset = FATOFS(pmp, cn);
930 bo = byteoffset % pmp->pm_fatblocksize;
932 /* Read new FAT block */
935 fatblock(pmp, byteoffset, &bn, &bsize, NULL);
936 error = bread(pmp->pm_devvp, bn, bsize, NOCRED, &bp);
943 readcn = getulong(&bp->b_data[bo]);
945 readcn = getushort(&bp->b_data[bo]);
946 if (FAT12(pmp) && (cn & 1))
948 readcn &= pmp->pm_fatmask;
951 usemap_free(pmp, cn);
958 * Allocate a new cluster and chain it onto the end of the file.
960 * dep - the file to extend
961 * count - number of clusters to allocate
962 * bpp - where to return the address of the buf header for the first new
964 * ncp - where to put cluster number of the first newly allocated cluster
965 * If this pointer is 0, do not return the cluster number.
968 * NOTE: This function is not responsible for turning on the DE_UPDATE bit of
969 * the de_flag field of the denode and it does not change the de_FileSize
970 * field. This is left for the caller to do.
973 extendfile(dep, count, bpp, ncp, flags)
983 struct msdosfsmount *pmp = dep->de_pmp;
988 * Don't try to extend the root directory
990 if (dep->de_StartCluster == MSDOSFSROOT
991 && (dep->de_Attributes & ATTR_DIRECTORY)) {
992 printf("extendfile(): attempt to extend root directory\n");
997 * If the "file's last cluster" cache entry is empty, and the file
998 * is not empty, then fill the cache entry by calling pcbmap().
1001 if (dep->de_fc[FC_LASTFC].fc_frcn == FCE_EMPTY &&
1002 dep->de_StartCluster != 0) {
1004 error = pcbmap(dep, 0xffff, 0, &cn, 0);
1005 /* we expect it to return E2BIG */
1010 fc_last_to_nexttolast(dep);
1013 * Allocate a new cluster chain and cat onto the end of the
1014 * file. * If the file is empty we make de_StartCluster point
1015 * to the new block. Note that de_StartCluster being 0 is
1016 * sufficient to be sure the file is empty since we exclude
1017 * attempts to extend the root directory above, and the root
1018 * dir is the only file with a startcluster of 0 that has
1019 * blocks allocated (sort of).
1021 if (dep->de_StartCluster == 0)
1024 cn = dep->de_fc[FC_LASTFC].fc_fsrcn + 1;
1025 error = clusteralloc(pmp, cn, count, CLUST_EOFE, &cn, &got);
1032 * Give them the filesystem relative cluster number if they want
1040 if (dep->de_StartCluster == 0) {
1041 dep->de_StartCluster = cn;
1044 error = fatentry(FAT_SET, pmp,
1045 dep->de_fc[FC_LASTFC].fc_fsrcn,
1048 clusterfree(pmp, cn, NULL);
1051 frcn = dep->de_fc[FC_LASTFC].fc_frcn + 1;
1055 * Update the "last cluster of the file" entry in the denode's fat
1058 fc_setcache(dep, FC_LASTFC, frcn + got - 1, cn + got - 1);
1060 if (flags & DE_CLEAR) {
1063 * Get the buf header for the new block of the file.
1065 if (dep->de_Attributes & ATTR_DIRECTORY)
1066 bp = getblk(pmp->pm_devvp,
1068 pmp->pm_bpcluster, 0, 0, 0);
1070 bp = getblk(DETOV(dep),
1072 pmp->pm_bpcluster, 0, 0, 0);
1074 * Do the bmap now, as in msdosfs_write
1080 if (bp->b_blkno == -1)
1081 panic("extendfile: pcbmap");
1083 bp->b_blkno = blkno;
1099 * Routine to mark a FAT16 or FAT32 volume as "clean" or "dirty" by
1100 * manipulating the upper bit of the FAT entry for cluster 1. Note that
1101 * this bit is not defined for FAT12 volumes, which are always assumed to
1104 * The fatentry() routine only works on cluster numbers that a file could
1105 * occupy, so it won't manipulate the entry for cluster 1. So we have to do
1106 * it here. The code was stolen from fatentry() and tailored for cluster 1.
1109 * pmp The MS-DOS volume to mark
1110 * dirty Non-zero if the volume should be marked dirty; zero if it
1111 * should be marked clean
1115 * EROFS Volume is read-only
1116 * ? (other errors from called routines)
1119 markvoldirty(struct msdosfsmount *pmp, int dirty)
1122 u_long bn, bo, bsize, byteoffset, fatval;
1126 * FAT12 does not support a "clean" bit, so don't do anything for
1132 /* Can't change the bit on a read-only filesystem. */
1133 if (pmp->pm_flags & MSDOSFSMNT_RONLY)
1137 * Fetch the block containing the FAT entry. It is given by the
1140 byteoffset = FATOFS(pmp, 1);
1141 fatblock(pmp, byteoffset, &bn, &bsize, &bo);
1142 error = bread(pmp->pm_devvp, bn, bsize, NOCRED, &bp);
1149 * Get the current value of the FAT entry and set/clear the relevant
1150 * bit. Dirty means clear the "clean" bit; clean means set the
1154 /* FAT32 uses bit 27. */
1155 fatval = getulong(&bp->b_data[bo]);
1157 fatval &= 0xF7FFFFFF;
1159 fatval |= 0x08000000;
1160 putulong(&bp->b_data[bo], fatval);
1162 /* Must be FAT16; use bit 15. */
1163 fatval = getushort(&bp->b_data[bo]);
1168 putushort(&bp->b_data[bo], fatval);
1171 /* Write out the modified FAT block synchronously. */
1172 return (bwrite(bp));