2 /* $NetBSD: msdosfs_fat.c,v 1.28 1997/11/17 15:36:49 ws Exp $ */
5 * SPDX-License-Identifier: BSD-4-Clause
7 * Copyright (C) 1994, 1995, 1997 Wolfgang Solfrank.
8 * Copyright (C) 1994, 1995, 1997 TooLs GmbH.
10 * Original code by Paul Popelka (paulp@uts.amdahl.com) (see below).
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
20 * 3. All advertising materials mentioning features or use of this software
21 * must display the following acknowledgement:
22 * This product includes software developed by TooLs GmbH.
23 * 4. The name of TooLs GmbH may not be used to endorse or promote products
24 * derived from this software without specific prior written permission.
26 * THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``AS IS'' AND ANY EXPRESS OR
27 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
28 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
29 * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
30 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
31 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
32 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
33 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
34 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
35 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
38 * Written by Paul Popelka (paulp@uts.amdahl.com)
40 * You can do anything you want with this software, just don't say you wrote
41 * it, and don't remove this notice.
43 * This software is provided "as is".
45 * The author supplies this software to be publicly redistributed on the
46 * understanding that the author is not responsible for the correct
47 * functioning of this software in any circumstances and is not liable for
48 * any damages caused by this software.
53 #include <sys/param.h>
54 #include <sys/errno.h>
64 #include <fs/msdosfs/bpb.h>
65 #include "msdos/direntry.h"
66 #include <fs/msdosfs/denode.h>
67 #include <fs/msdosfs/fat.h>
68 #include <fs/msdosfs/msdosfsmount.h>
73 #define FULL_RUN ((u_int)0xffffffff)
74 #define SYNCHRONOUS_WRITES(pmp) 1
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);
93 static int clusteralloc1(struct msdosfsmount *pmp, u_long start,
94 u_long count, u_long fillwith, u_long *retcluster,
98 fatblock(struct msdosfsmount *pmp, u_long ofs, u_long *bnp, u_long *sizep,
103 bn = ofs / pmp->pm_fatblocksize * pmp->pm_fatblocksec;
104 size = MIN(pmp->pm_fatblocksec, pmp->pm_FATsecs - bn)
106 bn += pmp->pm_fatblk + pmp->pm_curfat * pmp->pm_FATsecs;
113 *bop = ofs % pmp->pm_fatblocksize;
117 * Map the logical cluster number of a file into a physical disk sector
118 * that is filesystem relative.
120 * dep - address of denode representing the file of interest
121 * findcn - file relative cluster whose filesystem relative cluster number
122 * and/or block number are/is to be found
123 * bnp - address of where to place the filesystem relative block number.
124 * If this pointer is null then don't return this quantity.
125 * cnp - address of where to place the filesystem relative cluster number.
126 * If this pointer is null then don't return this quantity.
127 * sp - pointer to returned block size
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(struct denode *dep, u_long findcn, daddr_t *bnp, u_long *cnp, int *sp)
141 u_long prevcn = 0; /* XXX: prevcn could be used unititialized */
145 struct buf *bp = NULL;
147 struct msdosfsmount *pmp = dep->de_pmp;
150 assert(bnp != NULL || cnp != NULL || sp != NULL);
152 cn = dep->de_StartCluster;
154 * The "file" that makes up the root directory is contiguous,
155 * permanently allocated, of fixed size, and is not made up of
156 * clusters. If the cluster number is beyond the end of the root
157 * directory, then return the number of clusters in the file.
159 if (cn == MSDOSFSROOT) {
160 if (dep->de_Attributes & ATTR_DIRECTORY) {
161 if (de_cn2off(pmp, findcn) >= dep->de_FileSize) {
163 *cnp = de_bn2cn(pmp, pmp->pm_rootdirsize);
167 *bnp = pmp->pm_rootdirblk + de_cn2bn(pmp, findcn);
171 *sp = MIN(pmp->pm_bpcluster,
172 dep->de_FileSize - de_cn2off(pmp, findcn));
174 } else { /* just an empty file */
182 * All other files do I/O in cluster sized blocks
185 *sp = pmp->pm_bpcluster;
188 * Rummage around in the FAT cache, maybe we can avoid tromping
189 * through every FAT entry for the file. And, keep track of how far
190 * off the cache was from where we wanted to be.
193 fc_lookup(dep, findcn, &i, &cn);
196 * Handle all other files or directories the normal way.
198 for (; i < findcn; i++) {
200 * Stop with all reserved clusters, not just with EOF.
202 if ((cn | ~pmp->pm_fatmask) >= CLUST_RSRVD)
204 byteoffset = FATOFS(pmp, cn);
205 fatblock(pmp, byteoffset, &bn, &bsize, &bo);
209 error = bread(pmp->pm_devvp, bn, bsize, NOCRED, &bp);
223 cn = getulong(bp->b_data + bo);
225 cn = getushort(bp->b_data + bo);
226 if (FAT12(pmp) && (prevcn & 1))
228 cn &= pmp->pm_fatmask;
231 * Force the special cluster numbers
232 * to be the same for all cluster sizes
233 * to let the rest of msdosfs handle
234 * all cases the same.
236 if ((cn | ~pmp->pm_fatmask) >= CLUST_RSRVD)
237 cn |= ~pmp->pm_fatmask;
240 if (!MSDOSFSEOF(pmp, cn)) {
244 *bnp = cntobn(pmp, cn);
247 fc_setcache(dep, FC_LASTMAP, i, cn);
256 /* update last file cluster entry in the FAT cache */
257 fc_setcache(dep, FC_LASTFC, i - 1, prevcn);
262 * Find the closest entry in the FAT cache to the cluster we are looking
266 fc_lookup(struct denode *dep, u_long findcn, u_long *frcnp, u_long *fsrcnp)
270 struct fatcache *closest = NULL;
272 for (i = 0; i < FC_SIZE; i++) {
273 cn = dep->de_fc[i].fc_frcn;
274 if (cn != FCE_EMPTY && cn <= findcn) {
275 if (closest == NULL || cn > closest->fc_frcn)
276 closest = &dep->de_fc[i];
280 *frcnp = closest->fc_frcn;
281 *fsrcnp = closest->fc_fsrcn;
286 * Purge the FAT cache in denode dep of all entries relating to file
287 * relative cluster frcn and beyond.
290 fc_purge(struct denode *dep, u_int frcn)
293 struct fatcache *fcp;
296 for (i = 0; i < FC_SIZE; i++, fcp++) {
297 if (fcp->fc_frcn >= frcn)
298 fcp->fc_frcn = FCE_EMPTY;
304 * If mirroring the FAT, update all copies, with the first copy as last.
305 * Else update only the current FAT (ignoring the others).
307 * pmp - msdosfsmount structure for filesystem to update
308 * bp - addr of modified FAT block
309 * fatbn - block number relative to begin of filesystem of the modified FAT block.
312 updatefats(struct msdosfsmount *pmp, struct buf *bp, u_long fatbn)
318 printf("updatefats(pmp %p, bp %p, fatbn %lu)\n", pmp, bp, fatbn);
321 if (pmp->pm_flags & MSDOSFS_FATMIRROR) {
323 * Now copy the block(s) of the modified FAT to the other copies of
324 * the FAT and write them out. This is faster than reading in the
325 * other FATs and then writing them back out. This could tie up
326 * the FAT for quite a while. Preventing others from accessing it.
327 * To prevent us from going after the FAT quite so much we use
328 * delayed writes, unless they specified "synchronous" when the
329 * filesystem was mounted. If synch is asked for then use
330 * bwrite()'s and really slow things down.
332 if (fatbn != pmp->pm_fatblk || FAT12(pmp))
338 for (i = 1; i < pmp->pm_FATs; i++) {
339 fatbn += pmp->pm_FATsecs;
340 /* getblk() never fails */
341 bpn = getblk(pmp->pm_devvp, fatbn, bp->b_bcount,
343 memcpy(bpn->b_data, bp->b_data, bp->b_bcount);
344 /* Force the clean bit on in the other copies. */
346 ((uint8_t *)bpn->b_data)[3] |= 0x80;
347 else if (cleanfat == 32)
348 ((uint8_t *)bpn->b_data)[7] |= 0x08;
349 if (SYNCHRONOUS_WRITES(pmp))
357 * Write out the first (or current) FAT last.
359 if (SYNCHRONOUS_WRITES(pmp))
366 * Updating entries in 12 bit FATs is a pain in the butt.
368 * The following picture shows where nibbles go when moving from a 12 bit
369 * cluster number into the appropriate bytes in the FAT.
371 * byte m byte m+1 byte m+2
372 * +----+----+ +----+----+ +----+----+
373 * | 0 1 | | 2 3 | | 4 5 | FAT bytes
374 * +----+----+ +----+----+ +----+----+
376 * +----+----+----+ +----+----+----+
377 * | 3 0 1 | | 4 5 2 |
378 * +----+----+----+ +----+----+----+
379 * cluster n cluster n+1
381 * Where n is even. m = n + (n >> 2)
385 usemap_alloc(struct msdosfsmount *pmp, u_long cn)
388 assert(cn <= pmp->pm_maxcluster);
389 assert((pmp->pm_flags & MSDOSFSMNT_RONLY) == 0);
390 assert((pmp->pm_inusemap[cn / N_INUSEBITS] & (1 << (cn % N_INUSEBITS)))
392 assert(pmp->pm_freeclustercount > 0);
394 pmp->pm_inusemap[cn / N_INUSEBITS] |= 1U << (cn % N_INUSEBITS);
395 pmp->pm_freeclustercount--;
396 pmp->pm_flags |= MSDOSFS_FSIMOD;
400 usemap_free(struct msdosfsmount *pmp, u_long cn)
403 assert(cn <= pmp->pm_maxcluster);
404 assert((pmp->pm_flags & MSDOSFSMNT_RONLY) == 0);
405 assert((pmp->pm_inusemap[cn / N_INUSEBITS] & (1 << (cn % N_INUSEBITS)))
408 pmp->pm_freeclustercount++;
409 pmp->pm_flags |= MSDOSFS_FSIMOD;
410 pmp->pm_inusemap[cn / N_INUSEBITS] &= ~(1U << (cn % N_INUSEBITS));
414 clusterfree(struct msdosfsmount *pmp, u_long cluster, u_long *oldcnp)
419 error = fatentry(FAT_GET_AND_SET, pmp, cluster, &oldcn, MSDOSFSFREE);
423 * If the cluster was successfully marked free, then update
424 * the count of free clusters, and turn off the "allocated"
425 * bit in the "in use" cluster bit map.
427 usemap_free(pmp, cluster);
434 * Get or Set or 'Get and Set' the cluster'th entry in the FAT.
436 * function - whether to get or set a FAT entry
437 * pmp - address of the msdosfsmount structure for the filesystem
438 * whose FAT is to be manipulated.
439 * cn - which cluster is of interest
440 * oldcontents - address of a word that is to receive the contents of the
441 * cluster'th entry if this is a get function
442 * newcontents - the new value to be written into the cluster'th element of
443 * the FAT if this is a set function.
445 * This function can also be used to free a cluster by setting the FAT entry
446 * for a cluster to 0.
448 * All copies of the FAT are updated if this is a set function. NOTE: If
449 * fatentry() marks a cluster as free it does not update the inusemap in
450 * the msdosfsmount structure. This is left to the caller.
453 fatentry(int function, struct msdosfsmount *pmp, u_long cn, u_long *oldcontents,
458 u_long bn, bo, bsize, byteoffset;
462 printf("fatentry(func %d, pmp %p, clust %lu, oldcon %p, newcon %lx)\n",
463 function, pmp, cn, oldcontents, newcontents);
468 * Be sure they asked us to do something.
470 if ((function & (FAT_SET | FAT_GET)) == 0) {
472 printf("fatentry(): function code doesn't specify get or set\n");
478 * If they asked us to return a cluster number but didn't tell us
479 * where to put it, give them an error.
481 if ((function & FAT_GET) && oldcontents == NULL) {
483 printf("fatentry(): get function with no place to put result\n");
490 * Be sure the requested cluster is in the filesystem.
492 if (cn < CLUST_FIRST || cn > pmp->pm_maxcluster)
495 byteoffset = FATOFS(pmp, cn);
496 fatblock(pmp, byteoffset, &bn, &bsize, &bo);
497 error = bread(pmp->pm_devvp, bn, bsize, NOCRED, &bp);
503 if (function & FAT_GET) {
505 readcn = getulong(bp->b_data + bo);
507 readcn = getushort(bp->b_data + bo);
508 if (FAT12(pmp) & (cn & 1))
510 readcn &= pmp->pm_fatmask;
511 /* map reserved FAT entries to same values for all FATs */
512 if ((readcn | ~pmp->pm_fatmask) >= CLUST_RSRVD)
513 readcn |= ~pmp->pm_fatmask;
514 *oldcontents = readcn;
516 if (function & FAT_SET) {
517 switch (pmp->pm_fatmask) {
519 readcn = getushort(bp->b_data + bo);
522 readcn |= newcontents << 4;
525 readcn |= newcontents & 0xfff;
527 putushort(bp->b_data + bo, readcn);
530 putushort(bp->b_data + bo, newcontents);
534 * According to spec we have to retain the
535 * high order bits of the FAT entry.
537 readcn = getulong(bp->b_data + bo);
538 readcn &= ~FAT32_MASK;
539 readcn |= newcontents & FAT32_MASK;
540 putulong(bp->b_data + bo, readcn);
543 updatefats(pmp, bp, bn);
553 * Update a contiguous cluster chain
556 * start - first cluster of chain
557 * count - number of clusters in chain
558 * fillwith - what to write into FAT entry of last cluster
561 fatchain(struct msdosfsmount *pmp, u_long start, u_long count, u_long fillwith)
564 u_long bn, bo, bsize, byteoffset, readcn, newc;
568 printf("fatchain(pmp %p, start %lu, count %lu, fillwith %lx)\n",
569 pmp, start, count, fillwith);
572 * Be sure the clusters are in the filesystem.
574 if (start < CLUST_FIRST || start + count - 1 > pmp->pm_maxcluster)
578 byteoffset = FATOFS(pmp, start);
579 fatblock(pmp, byteoffset, &bn, &bsize, &bo);
580 error = bread(pmp->pm_devvp, bn, bsize, NOCRED, &bp);
587 newc = --count > 0 ? start : fillwith;
588 switch (pmp->pm_fatmask) {
590 readcn = getushort(bp->b_data + bo);
593 readcn |= newc & 0xfff;
598 putushort(bp->b_data + bo, readcn);
604 putushort(bp->b_data + bo, newc);
608 readcn = getulong(bp->b_data + bo);
609 readcn &= ~pmp->pm_fatmask;
610 readcn |= newc & pmp->pm_fatmask;
611 putulong(bp->b_data + bo, readcn);
618 updatefats(pmp, bp, bn);
625 * Check the length of a free cluster chain starting at start.
628 * start - start of chain
629 * count - maximum interesting length
632 chainlength(struct msdosfsmount *pmp, u_long start, u_long count)
638 if (start > pmp->pm_maxcluster)
640 max_idx = pmp->pm_maxcluster / N_INUSEBITS;
641 idx = start / N_INUSEBITS;
642 start %= N_INUSEBITS;
643 map = pmp->pm_inusemap[idx];
644 map &= ~((1 << start) - 1);
646 len = ffs(map) - 1 - start;
647 len = MIN(len, count);
648 if (start + len > pmp->pm_maxcluster)
649 len = pmp->pm_maxcluster - start + 1;
652 len = N_INUSEBITS - start;
655 if (start + len > pmp->pm_maxcluster)
656 len = pmp->pm_maxcluster - start + 1;
659 while (++idx <= max_idx) {
662 map = pmp->pm_inusemap[idx];
669 len = MIN(len, count);
670 if (start + len > pmp->pm_maxcluster)
671 len = pmp->pm_maxcluster - start + 1;
676 * Allocate contigous free clusters.
679 * start - start of cluster chain.
680 * count - number of clusters to allocate.
681 * fillwith - put this value into the FAT entry for the
682 * last allocated cluster.
683 * retcluster - put the first allocated cluster's number here.
684 * got - how many clusters were actually allocated.
687 chainalloc(struct msdosfsmount *pmp, u_long start, u_long count,
688 u_long fillwith, u_long *retcluster, u_long *got)
693 assert((pmp->pm_flags & MSDOSFSMNT_RONLY) == 0);
695 for (cl = start, n = count; n-- > 0;)
696 usemap_alloc(pmp, cl++);
697 pmp->pm_nxtfree = start + count;
698 if (pmp->pm_nxtfree > pmp->pm_maxcluster)
699 pmp->pm_nxtfree = CLUST_FIRST;
700 pmp->pm_flags |= MSDOSFS_FSIMOD;
701 error = fatchain(pmp, start, count, fillwith);
703 for (cl = start, n = count; n-- > 0;)
704 usemap_free(pmp, cl++);
708 printf("clusteralloc(): allocated cluster chain at %lu (%lu clusters)\n",
719 * Allocate contiguous free clusters.
722 * start - preferred start of cluster chain.
723 * count - number of clusters requested.
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 clusteralloc(struct msdosfsmount *pmp, u_long start, u_long count,
731 u_long fillwith, u_long *retcluster, u_long *got)
735 error = clusteralloc1(pmp, start, count, fillwith, retcluster, got);
740 clusteralloc1(struct msdosfsmount *pmp, u_long start, u_long count,
741 u_long fillwith, u_long *retcluster, u_long *got)
744 u_long len, newst, foundl, cn, l;
745 u_long foundcn = 0; /* XXX: foundcn could be used unititialized */
748 MSDOSFS_DPRINTF(("clusteralloc(): find %lu clusters\n", count));
751 if ((len = chainlength(pmp, start, count)) >= count)
752 return (chainalloc(pmp, start, count, fillwith, retcluster, got));
756 newst = pmp->pm_nxtfree;
759 for (cn = newst; cn <= pmp->pm_maxcluster;) {
760 idx = cn / N_INUSEBITS;
761 map = pmp->pm_inusemap[idx];
762 map |= (1U << (cn % N_INUSEBITS)) - 1;
763 if (map != FULL_RUN) {
764 cn = idx * N_INUSEBITS + ffs(map ^ FULL_RUN) - 1;
765 if ((l = chainlength(pmp, cn, count)) >= count)
766 return (chainalloc(pmp, cn, count, fillwith, retcluster, got));
774 cn += N_INUSEBITS - cn % N_INUSEBITS;
776 for (cn = 0; cn < newst;) {
777 idx = cn / N_INUSEBITS;
778 map = pmp->pm_inusemap[idx];
779 map |= (1U << (cn % N_INUSEBITS)) - 1;
780 if (map != FULL_RUN) {
781 cn = idx * N_INUSEBITS + ffs(map ^ FULL_RUN) - 1;
782 if ((l = chainlength(pmp, cn, count)) >= count)
783 return (chainalloc(pmp, cn, count, fillwith, retcluster, got));
791 cn += N_INUSEBITS - cn % N_INUSEBITS;
798 return (chainalloc(pmp, start, len, fillwith, retcluster, got));
800 return (chainalloc(pmp, foundcn, foundl, fillwith, retcluster, got));
805 * Free a chain of clusters.
807 * pmp - address of the msdosfs mount structure for the filesystem
808 * containing the cluster chain to be freed.
809 * startcluster - number of the 1st cluster in the chain of clusters to be
813 freeclusterchain(struct msdosfsmount *pmp, u_long cluster)
816 struct buf *bp = NULL;
817 u_long bn, bo, bsize, byteoffset;
818 u_long readcn, lbn = -1;
820 while (cluster >= CLUST_FIRST && cluster <= pmp->pm_maxcluster) {
821 byteoffset = FATOFS(pmp, cluster);
822 fatblock(pmp, byteoffset, &bn, &bsize, &bo);
825 updatefats(pmp, bp, lbn);
826 error = bread(pmp->pm_devvp, bn, bsize, NOCRED, &bp);
833 usemap_free(pmp, cluster);
834 switch (pmp->pm_fatmask) {
836 readcn = getushort(bp->b_data + bo);
838 cluster = readcn >> 4;
840 readcn |= MSDOSFSFREE << 4;
844 readcn |= MSDOSFSFREE & 0xfff;
846 putushort(bp->b_data + bo, readcn);
849 cluster = getushort(bp->b_data + bo);
850 putushort(bp->b_data + bo, MSDOSFSFREE);
853 cluster = getulong(bp->b_data + bo);
854 putulong(bp->b_data + bo,
855 (MSDOSFSFREE & FAT32_MASK) | (cluster & ~FAT32_MASK));
858 cluster &= pmp->pm_fatmask;
859 if ((cluster | ~pmp->pm_fatmask) >= CLUST_RSRVD)
860 cluster |= pmp->pm_fatmask;
863 updatefats(pmp, bp, bn);
868 * Read in FAT blocks looking for free clusters. For every free cluster
869 * found turn off its corresponding bit in the pm_inusemap.
872 fillinusemap(struct msdosfsmount *pmp)
875 u_long bn, bo, bsize, byteoffset, cn, readcn;
881 * Mark all clusters in use, we mark the free ones in the FAT scan
884 for (cn = 0; cn < (pmp->pm_maxcluster + N_INUSEBITS) / N_INUSEBITS; cn++)
885 pmp->pm_inusemap[cn] = FULL_RUN;
888 * Figure how many free clusters are in the filesystem by ripping
889 * through the FAT counting the number of entries whose content is
890 * zero. These represent free clusters.
892 pmp->pm_freeclustercount = 0;
893 for (cn = 0; cn <= pmp->pm_maxcluster; cn++) {
894 byteoffset = FATOFS(pmp, cn);
895 bo = byteoffset % pmp->pm_fatblocksize;
897 /* Read new FAT block */
900 fatblock(pmp, byteoffset, &bn, &bsize, NULL);
901 error = bread(pmp->pm_devvp, bn, bsize, NOCRED, &bp);
906 readcn = getulong(bp->b_data + bo);
908 readcn = getushort(bp->b_data + bo);
909 if (FAT12(pmp) && (cn & 1))
911 readcn &= pmp->pm_fatmask;
914 * Check if the FAT ID matches the BPB's media descriptor and
915 * all other bits are set to 1.
917 if (cn == 0 && readcn != ((pmp->pm_fatmask & 0xffffff00) |
918 pmp->pm_bpb.bpbMedia)) {
920 printf("mountmsdosfs(): Media descriptor in BPB"
921 "does not match FAT ID\n");
925 } else if (readcn == CLUST_FREE)
926 usemap_free(pmp, cn);
931 for (cn = pmp->pm_maxcluster + 1; cn < (pmp->pm_maxcluster +
932 N_INUSEBITS) / N_INUSEBITS; cn++)
933 pmp->pm_inusemap[cn / N_INUSEBITS] |= 1U << (cn % N_INUSEBITS);
939 * Allocate a new cluster and chain it onto the end of the file.
941 * dep - the file to extend
942 * count - number of clusters to allocate
943 * bpp - where to return the address of the buf header for the first new
945 * ncp - where to put cluster number of the first newly allocated cluster
946 * If this pointer is 0, do not return the cluster number.
949 * NOTE: This function is not responsible for turning on the DE_UPDATE bit of
950 * the de_flag field of the denode and it does not change the de_FileSize
951 * field. This is left for the caller to do.
954 extendfile(struct denode *dep, u_long count, struct buf **bpp, u_long *ncp,
960 struct msdosfsmount *pmp = dep->de_pmp;
964 * Don't try to extend the root directory
966 if (dep->de_StartCluster == MSDOSFSROOT
967 && (dep->de_Attributes & ATTR_DIRECTORY)) {
969 printf("extendfile(): attempt to extend root directory\n");
975 * If the "file's last cluster" cache entry is empty, and the file
976 * is not empty, then fill the cache entry by calling pcbmap().
978 if (dep->de_fc[FC_LASTFC].fc_frcn == FCE_EMPTY &&
979 dep->de_StartCluster != 0) {
980 error = pcbmap(dep, 0xffff, 0, &cn, 0);
981 /* we expect it to return E2BIG */
986 dep->de_fc[FC_NEXTTOLASTFC].fc_frcn =
987 dep->de_fc[FC_LASTFC].fc_frcn;
988 dep->de_fc[FC_NEXTTOLASTFC].fc_fsrcn =
989 dep->de_fc[FC_LASTFC].fc_fsrcn;
992 * Allocate a new cluster chain and cat onto the end of the
993 * file. If the file is empty we make de_StartCluster point
994 * to the new block. Note that de_StartCluster being 0 is
995 * sufficient to be sure the file is empty since we exclude
996 * attempts to extend the root directory above, and the root
997 * dir is the only file with a startcluster of 0 that has
998 * blocks allocated (sort of).
1000 if (dep->de_StartCluster == 0)
1003 cn = dep->de_fc[FC_LASTFC].fc_fsrcn + 1;
1004 error = clusteralloc(pmp, cn, count, CLUST_EOFE, &cn, &got);
1011 * Give them the filesystem relative cluster number if they want
1019 if (dep->de_StartCluster == 0) {
1020 dep->de_StartCluster = cn;
1023 error = fatentry(FAT_SET, pmp,
1024 dep->de_fc[FC_LASTFC].fc_fsrcn,
1027 clusterfree(pmp, cn, NULL);
1030 frcn = dep->de_fc[FC_LASTFC].fc_frcn + 1;
1034 * Update the "last cluster of the file" entry in the
1035 * denode's FAT cache.
1037 fc_setcache(dep, FC_LASTFC, frcn + got - 1, cn + got - 1);
1039 if ((flags & DE_CLEAR) &&
1040 (dep->de_Attributes & ATTR_DIRECTORY)) {
1042 bp = getblk(pmp->pm_devvp,
1044 pmp->pm_bpcluster, 0, 0, 0);