1 /* $FreeBSD: src/sys/msdosfs/msdosfs_fat.c,v 1.23 2000/01/27 14:43:06 nyan Exp $ */
2 /* $DragonFly: src/sys/vfs/msdosfs/msdosfs_fat.c,v 1.6 2004/04/17 00:30:17 cpressey Exp $ */
3 /* $NetBSD: msdosfs_fat.c,v 1.28 1997/11/17 15:36:49 ws Exp $ */
6 * Copyright (C) 1994, 1995, 1997 Wolfgang Solfrank.
7 * Copyright (C) 1994, 1995, 1997 TooLs GmbH.
9 * Original code by Paul Popelka (paulp@uts.amdahl.com) (see below).
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. All advertising materials mentioning features or use of this software
20 * must display the following acknowledgement:
21 * This product includes software developed by TooLs GmbH.
22 * 4. The name of TooLs GmbH may not be used to endorse or promote products
23 * derived from this software without specific prior written permission.
25 * THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``AS IS'' AND ANY EXPRESS OR
26 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
27 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
28 * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
29 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
30 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
31 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
32 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
33 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
34 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37 * Written by Paul Popelka (paulp@uts.amdahl.com)
39 * You can do anything you want with this software, just don't say you wrote
40 * it, and don't remove this notice.
42 * This software is provided "as is".
44 * The author supplies this software to be publicly redistributed on the
45 * understanding that the author is not responsible for the correct
46 * functioning of this software in any circumstances and is not liable for
47 * any damages caused by this software.
53 * kernel include files.
55 #include <sys/param.h>
56 #include <sys/systm.h>
58 #include <sys/mount.h> /* to define statfs structure */
59 #include <sys/vnode.h> /* to define vattr structure */
62 * msdosfs include files.
65 #include "msdosfsmount.h"
73 static int fc_fileextends; /* # of file extends */
74 static int fc_lfcempty; /* # of time last file cluster cache entry
76 static int fc_bmapcalls; /* # of times pcbmap was called */
79 static int fc_lmdistance[LMMAX];/* counters for how far off the last
80 * cluster mapped entry was. */
81 static int fc_largedistance; /* off by more than LMMAX */
83 static int chainalloc (struct msdosfsmount *pmp, u_long start,
84 u_long count, u_long fillwith,
85 u_long *retcluster, u_long *got);
86 static int chainlength (struct msdosfsmount *pmp, u_long start,
88 static void fatblock (struct msdosfsmount *pmp, u_long ofs,
89 u_long *bnp, u_long *sizep, u_long *bop);
90 static int fatchain (struct msdosfsmount *pmp, u_long start,
91 u_long count, u_long fillwith);
92 static void fc_lookup (struct denode *dep, u_long findcn,
93 u_long *frcnp, u_long *fsrcnp);
94 static void updatefats (struct msdosfsmount *pmp, struct buf *bp,
97 usemap_alloc (struct msdosfsmount *pmp, u_long cn);
99 usemap_free (struct msdosfsmount *pmp, u_long cn);
102 fatblock(struct msdosfsmount *pmp, u_long ofs, u_long *bnp, u_long *sizep,
107 bn = ofs / pmp->pm_fatblocksize * pmp->pm_fatblocksec;
108 size = min(pmp->pm_fatblocksec, pmp->pm_FATsecs - bn)
110 bn += pmp->pm_fatblk + pmp->pm_curfat * pmp->pm_FATsecs;
117 *bop = ofs % 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 file system relative block number.
128 * If this pointer is null then don't return this quantity.
129 * cnp - address of where to place the file system 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(struct denode *dep,
140 u_long findcn, /* file relative cluster to get */
141 daddr_t *bnp, /* returned filesys relative blk number */
142 u_long *cnp, /* returned cluster number */
143 int *sp) /* returned block size */
148 u_long prevcn = 0; /* XXX: prevcn could be used unititialized */
152 struct buf *bp = NULL;
154 struct msdosfsmount *pmp = dep->de_pmp;
160 * If they don't give us someplace to return a value then don't
161 * bother doing anything.
163 if (bnp == NULL && cnp == NULL && sp == NULL)
166 cn = dep->de_StartCluster;
168 * The "file" that makes up the root directory is contiguous,
169 * permanently allocated, of fixed size, and is not made up of
170 * clusters. If the cluster number is beyond the end of the root
171 * directory, then return the number of clusters in the file.
173 if (cn == MSDOSFSROOT) {
174 if (dep->de_Attributes & ATTR_DIRECTORY) {
175 if (de_cn2off(pmp, findcn) >= dep->de_FileSize) {
177 *cnp = de_bn2cn(pmp, pmp->pm_rootdirsize);
181 *bnp = pmp->pm_rootdirblk + de_cn2bn(pmp, findcn);
185 *sp = min(pmp->pm_bpcluster,
186 dep->de_FileSize - de_cn2off(pmp, findcn));
188 } else { /* just an empty file */
196 * All other files do I/O in cluster sized blocks
199 *sp = pmp->pm_bpcluster;
202 * Rummage around in the fat cache, maybe we can avoid tromping
203 * thru every fat entry for the file. And, keep track of how far
204 * off the cache was from where we wanted to be.
207 fc_lookup(dep, findcn, &i, &cn);
208 if ((bn = findcn - i) >= LMMAX)
214 * Handle all other files or directories the normal way.
216 for (; i < findcn; i++) {
218 * Stop with all reserved clusters, not just with EOF.
220 if ((cn | ~pmp->pm_fatmask) >= CLUST_RSRVD)
222 byteoffset = FATOFS(pmp, cn);
223 fatblock(pmp, byteoffset, &bn, &bsize, &bo);
227 error = bread(pmp->pm_devvp, bn, bsize, &bp);
236 cn = getulong(&bp->b_data[bo]);
238 cn = getushort(&bp->b_data[bo]);
239 if (FAT12(pmp) && (prevcn & 1))
241 cn &= pmp->pm_fatmask;
244 * Force the special cluster numbers
245 * to be the same for all cluster sizes
246 * to let the rest of msdosfs handle
247 * all cases the same.
249 if ((cn | ~pmp->pm_fatmask) >= CLUST_RSRVD)
250 cn |= ~pmp->pm_fatmask;
253 if (!MSDOSFSEOF(pmp, cn)) {
257 *bnp = cntobn(pmp, cn);
260 fc_setcache(dep, FC_LASTMAP, i, cn);
269 /* update last file cluster entry in the fat cache */
270 fc_setcache(dep, FC_LASTFC, i - 1, prevcn);
275 * Find the closest entry in the fat cache to the cluster we are looking
279 fc_lookup(struct denode *dep, u_long findcn, u_long *frcnp, u_long *fsrcnp)
283 struct fatcache *closest = 0;
285 for (i = 0; i < FC_SIZE; i++) {
286 cn = dep->de_fc[i].fc_frcn;
287 if (cn != FCE_EMPTY && cn <= findcn) {
288 if (closest == 0 || cn > closest->fc_frcn)
289 closest = &dep->de_fc[i];
293 *frcnp = closest->fc_frcn;
294 *fsrcnp = closest->fc_fsrcn;
299 * Purge the fat cache in denode dep of all entries relating to file
300 * relative cluster frcn and beyond.
303 fc_purge(struct denode *dep, u_int frcn)
306 struct fatcache *fcp;
309 for (i = 0; i < FC_SIZE; i++, fcp++) {
310 if (fcp->fc_frcn >= frcn)
311 fcp->fc_frcn = FCE_EMPTY;
317 * If mirroring the fat, update all copies, with the first copy as last.
318 * Else update only the current fat (ignoring the others).
320 * pmp - msdosfsmount structure for filesystem to update
321 * bp - addr of modified fat block
322 * fatbn - block number relative to begin of filesystem of the modified fat block.
325 updatefats(struct msdosfsmount *pmp, struct buf *bp, u_long fatbn)
331 printf("updatefats(pmp %p, bp %p, fatbn %lu)\n", pmp, bp, fatbn);
335 * If we have an FSInfo block, update it.
337 if (pmp->pm_fsinfo) {
338 u_long cn = pmp->pm_nxtfree;
340 if (pmp->pm_freeclustercount
341 && (pmp->pm_inusemap[cn / N_INUSEBITS]
342 & (1 << (cn % N_INUSEBITS)))) {
344 * The cluster indicated in FSInfo isn't free
345 * any longer. Got get a new free one.
347 for (cn = 0; cn < pmp->pm_maxcluster; cn += N_INUSEBITS)
348 if (pmp->pm_inusemap[cn / N_INUSEBITS] != (u_int)-1)
351 + ffs(pmp->pm_inusemap[cn / N_INUSEBITS]
354 if (bread(pmp->pm_devvp, pmp->pm_fsinfo, fsi_size(pmp), &bpn) != 0) {
356 * Ignore the error, but turn off FSInfo update for the future.
361 struct fsinfo *fp = (struct fsinfo *)bpn->b_data;
363 putulong(fp->fsinfree, pmp->pm_freeclustercount);
364 putulong(fp->fsinxtfree, pmp->pm_nxtfree);
365 if (pmp->pm_flags & MSDOSFSMNT_WAITONFAT)
372 if (pmp->pm_flags & MSDOSFS_FATMIRROR) {
374 * Now copy the block(s) of the modified fat to the other copies of
375 * the fat and write them out. This is faster than reading in the
376 * other fats and then writing them back out. This could tie up
377 * the fat for quite a while. Preventing others from accessing it.
378 * To prevent us from going after the fat quite so much we use
379 * delayed writes, unless they specfied "synchronous" when the
380 * filesystem was mounted. If synch is asked for then use
381 * bwrite()'s and really slow things down.
383 for (i = 1; i < pmp->pm_FATs; i++) {
384 fatbn += pmp->pm_FATsecs;
385 /* getblk() never fails */
386 bpn = getblk(pmp->pm_devvp, fatbn, bp->b_bcount, 0, 0);
387 bcopy(bp->b_data, bpn->b_data, bp->b_bcount);
388 if (pmp->pm_flags & MSDOSFSMNT_WAITONFAT)
396 * Write out the first (or current) fat last.
398 if (pmp->pm_flags & MSDOSFSMNT_WAITONFAT)
403 * Maybe update fsinfo sector here?
408 * Updating entries in 12 bit fats is a pain in the butt.
410 * The following picture shows where nibbles go when moving from a 12 bit
411 * cluster number into the appropriate bytes in the FAT.
413 * byte m byte m+1 byte m+2
414 * +----+----+ +----+----+ +----+----+
415 * | 0 1 | | 2 3 | | 4 5 | FAT bytes
416 * +----+----+ +----+----+ +----+----+
418 * +----+----+----+ +----+----+----+
419 * | 3 0 1 | | 4 5 2 |
420 * +----+----+----+ +----+----+----+
421 * cluster n cluster n+1
423 * Where n is even. m = n + (n >> 2)
427 usemap_alloc(struct msdosfsmount *pmp, u_long cn)
430 pmp->pm_inusemap[cn / N_INUSEBITS] |= 1 << (cn % N_INUSEBITS);
431 pmp->pm_freeclustercount--;
435 usemap_free(struct msdosfsmount *pmp, u_long cn)
438 pmp->pm_freeclustercount++;
439 pmp->pm_inusemap[cn / N_INUSEBITS] &= ~(1 << (cn % N_INUSEBITS));
443 clusterfree(struct msdosfsmount *pmp, u_long cluster, u_long *oldcnp)
448 usemap_free(pmp, cluster);
449 error = fatentry(FAT_GET_AND_SET, pmp, cluster, &oldcn, MSDOSFSFREE);
451 usemap_alloc(pmp, cluster);
455 * If the cluster was successfully marked free, then update
456 * the count of free clusters, and turn off the "allocated"
457 * bit in the "in use" cluster bit map.
465 * Get or Set or 'Get and Set' the cluster'th entry in the fat.
467 * function - whether to get or set a fat entry
468 * pmp - address of the msdosfsmount structure for the filesystem
469 * whose fat is to be manipulated.
470 * cn - which cluster is of interest
471 * oldcontents - address of a word that is to receive the contents of the
472 * cluster'th entry if this is a get function
473 * newcontents - the new value to be written into the cluster'th element of
474 * the fat if this is a set function.
476 * This function can also be used to free a cluster by setting the fat entry
477 * for a cluster to 0.
479 * All copies of the fat are updated if this is a set function. NOTE: If
480 * fatentry() marks a cluster as free it does not update the inusemap in
481 * the msdosfsmount structure. This is left to the caller.
484 fatentry(int function, struct msdosfsmount *pmp, u_long cn, u_long *oldcontents,
489 u_long bn, bo, bsize, byteoffset;
493 printf("fatentry(func %d, pmp %p, clust %lu, oldcon %p, newcon %lx)\n",
494 function, pmp, cn, oldcontents, newcontents);
499 * Be sure they asked us to do something.
501 if ((function & (FAT_SET | FAT_GET)) == 0) {
502 printf("fatentry(): function code doesn't specify get or set\n");
507 * If they asked us to return a cluster number but didn't tell us
508 * where to put it, give them an error.
510 if ((function & FAT_GET) && oldcontents == NULL) {
511 printf("fatentry(): get function with no place to put result\n");
517 * Be sure the requested cluster is in the filesystem.
519 if (cn < CLUST_FIRST || cn > pmp->pm_maxcluster)
522 byteoffset = FATOFS(pmp, cn);
523 fatblock(pmp, byteoffset, &bn, &bsize, &bo);
524 error = bread(pmp->pm_devvp, bn, bsize, &bp);
530 if (function & FAT_GET) {
532 readcn = getulong(&bp->b_data[bo]);
534 readcn = getushort(&bp->b_data[bo]);
535 if (FAT12(pmp) & (cn & 1))
537 readcn &= pmp->pm_fatmask;
538 /* map reserved fat entries to same values for all fats */
539 if ((readcn | ~pmp->pm_fatmask) >= CLUST_RSRVD)
540 readcn |= ~pmp->pm_fatmask;
541 *oldcontents = readcn;
543 if (function & FAT_SET) {
544 switch (pmp->pm_fatmask) {
546 readcn = getushort(&bp->b_data[bo]);
549 readcn |= newcontents << 4;
552 readcn |= newcontents & 0xfff;
554 putushort(&bp->b_data[bo], readcn);
557 putushort(&bp->b_data[bo], newcontents);
561 * According to spec we have to retain the
562 * high order bits of the fat entry.
564 readcn = getulong(&bp->b_data[bo]);
565 readcn &= ~FAT32_MASK;
566 readcn |= newcontents & FAT32_MASK;
567 putulong(&bp->b_data[bo], readcn);
570 updatefats(pmp, bp, bn);
580 * Update a contiguous cluster chain
583 * start - first cluster of chain
584 * count - number of clusters in chain
585 * fillwith - what to write into fat entry of last cluster
588 fatchain(struct msdosfsmount *pmp, u_long start, u_long count, u_long fillwith)
591 u_long bn, bo, bsize, byteoffset, readcn, newc;
595 printf("fatchain(pmp %p, start %lu, count %lu, fillwith %lx)\n",
596 pmp, start, count, fillwith);
599 * Be sure the clusters are in the filesystem.
601 if (start < CLUST_FIRST || start + count - 1 > pmp->pm_maxcluster)
605 byteoffset = FATOFS(pmp, start);
606 fatblock(pmp, byteoffset, &bn, &bsize, &bo);
607 error = bread(pmp->pm_devvp, bn, bsize, &bp);
614 newc = --count > 0 ? start : fillwith;
615 switch (pmp->pm_fatmask) {
617 readcn = getushort(&bp->b_data[bo]);
620 readcn |= newc & 0xfff;
625 putushort(&bp->b_data[bo], readcn);
631 putushort(&bp->b_data[bo], newc);
635 readcn = getulong(&bp->b_data[bo]);
636 readcn &= ~pmp->pm_fatmask;
637 readcn |= newc & pmp->pm_fatmask;
638 putulong(&bp->b_data[bo], readcn);
645 updatefats(pmp, bp, bn);
652 * Check the length of a free cluster chain starting at start.
655 * start - start of chain
656 * count - maximum interesting length
659 chainlength(struct msdosfsmount *pmp, u_long start, u_long count)
665 max_idx = pmp->pm_maxcluster / N_INUSEBITS;
666 idx = start / N_INUSEBITS;
667 start %= N_INUSEBITS;
668 map = pmp->pm_inusemap[idx];
669 map &= ~((1 << start) - 1);
671 len = ffs(map) - 1 - start;
672 return (len > count ? count : len);
674 len = N_INUSEBITS - start;
677 while (++idx <= max_idx) {
680 map = pmp->pm_inusemap[idx];
687 return (len > count ? count : len);
691 * Allocate contigous free clusters.
694 * start - start of cluster chain.
695 * count - number of clusters to allocate.
696 * fillwith - put this value into the fat entry for the
697 * last allocated cluster.
698 * retcluster - put the first allocated cluster's number here.
699 * got - how many clusters were actually allocated.
702 chainalloc(struct msdosfsmount *pmp, u_long start, u_long count,
703 u_long fillwith, u_long *retcluster, u_long *got)
708 for (cl = start, n = count; n-- > 0;)
709 usemap_alloc(pmp, cl++);
711 error = fatchain(pmp, start, count, fillwith);
715 printf("clusteralloc(): allocated cluster chain at %lu (%lu clusters)\n",
726 * Allocate contiguous free clusters.
729 * start - preferred start of cluster chain.
730 * count - number of clusters requested.
731 * fillwith - put this value into the fat entry for the
732 * last allocated cluster.
733 * retcluster - put the first allocated cluster's number here.
734 * got - how many clusters were actually allocated.
737 clusteralloc(struct msdosfsmount *pmp, u_long start, u_long count,
738 u_long fillwith, u_long *retcluster, u_long *got)
741 u_long len, newst, foundl, cn, l;
742 u_long foundcn = 0; /* XXX: foundcn could be used unititialized */
746 printf("clusteralloc(): find %lu clusters\n",count);
749 if ((len = chainlength(pmp, start, count)) >= count)
750 return (chainalloc(pmp, start, count, fillwith, retcluster, got));
755 * Start at a (pseudo) random place to maximize cluster runs
756 * under multiple writers.
758 newst = random() % (pmp->pm_maxcluster + 1);
761 for (cn = newst; cn <= pmp->pm_maxcluster;) {
762 idx = cn / N_INUSEBITS;
763 map = pmp->pm_inusemap[idx];
764 map |= (1 << (cn % N_INUSEBITS)) - 1;
765 if (map != (u_int)-1) {
766 cn = idx * N_INUSEBITS + ffs(map^(u_int)-1) - 1;
767 if ((l = chainlength(pmp, cn, count)) >= count)
768 return (chainalloc(pmp, cn, count, fillwith, retcluster, got));
776 cn += N_INUSEBITS - cn % N_INUSEBITS;
778 for (cn = 0; cn < newst;) {
779 idx = cn / N_INUSEBITS;
780 map = pmp->pm_inusemap[idx];
781 map |= (1 << (cn % N_INUSEBITS)) - 1;
782 if (map != (u_int)-1) {
783 cn = idx * N_INUSEBITS + ffs(map^(u_int)-1) - 1;
784 if ((l = chainlength(pmp, cn, count)) >= count)
785 return (chainalloc(pmp, cn, count, fillwith, retcluster, got));
793 cn += N_INUSEBITS - cn % N_INUSEBITS;
800 return (chainalloc(pmp, start, len, fillwith, retcluster, got));
802 return (chainalloc(pmp, foundcn, foundl, fillwith, retcluster, got));
807 * Free a chain of clusters.
809 * pmp - address of the msdosfs mount structure for the filesystem
810 * containing the cluster chain to be freed.
811 * startcluster - number of the 1st cluster in the chain of clusters to be
815 freeclusterchain(struct msdosfsmount *pmp, u_long cluster)
818 struct buf *bp = NULL;
819 u_long bn, bo, bsize, byteoffset;
820 u_long readcn, lbn = -1;
822 while (cluster >= CLUST_FIRST && cluster <= pmp->pm_maxcluster) {
823 byteoffset = FATOFS(pmp, cluster);
824 fatblock(pmp, byteoffset, &bn, &bsize, &bo);
827 updatefats(pmp, bp, lbn);
828 error = bread(pmp->pm_devvp, bn, bsize, &bp);
835 usemap_free(pmp, cluster);
836 switch (pmp->pm_fatmask) {
838 readcn = getushort(&bp->b_data[bo]);
840 cluster = readcn >> 4;
842 readcn |= MSDOSFSFREE << 4;
846 readcn |= MSDOSFSFREE & 0xfff;
848 putushort(&bp->b_data[bo], readcn);
851 cluster = getushort(&bp->b_data[bo]);
852 putushort(&bp->b_data[bo], MSDOSFSFREE);
855 cluster = getulong(&bp->b_data[bo]);
856 putulong(&bp->b_data[bo],
857 (MSDOSFSFREE & FAT32_MASK) | (cluster & ~FAT32_MASK));
860 cluster &= pmp->pm_fatmask;
861 if ((cluster | ~pmp->pm_fatmask) >= CLUST_RSRVD)
862 cluster |= pmp->pm_fatmask;
865 updatefats(pmp, bp, bn);
870 * Read in fat blocks looking for free clusters. For every free cluster
871 * found turn off its corresponding bit in the pm_inusemap.
874 fillinusemap(struct msdosfsmount *pmp)
876 struct buf *bp = NULL;
879 u_long bn, bo, bsize, byteoffset;
882 * Mark all clusters in use, we mark the free ones in the fat scan
885 for (cn = 0; cn < (pmp->pm_maxcluster + N_INUSEBITS) / N_INUSEBITS; cn++)
886 pmp->pm_inusemap[cn] = (u_int)-1;
889 * Figure how many free clusters are in the filesystem by ripping
890 * through the fat counting the number of entries whose content is
891 * zero. These represent free clusters.
893 pmp->pm_freeclustercount = 0;
894 for (cn = CLUST_FIRST; cn <= pmp->pm_maxcluster; cn++) {
895 byteoffset = FATOFS(pmp, cn);
896 bo = byteoffset % pmp->pm_fatblocksize;
898 /* Read new FAT block */
901 fatblock(pmp, byteoffset, &bn, &bsize, NULL);
902 error = bread(pmp->pm_devvp, bn, bsize, &bp);
909 readcn = getulong(&bp->b_data[bo]);
911 readcn = getushort(&bp->b_data[bo]);
912 if (FAT12(pmp) && (cn & 1))
914 readcn &= pmp->pm_fatmask;
917 usemap_free(pmp, cn);
924 * Allocate a new cluster and chain it onto the end of the file.
926 * dep - the file to extend
927 * count - number of clusters to allocate
928 * bpp - where to return the address of the buf header for the first new
930 * ncp - where to put cluster number of the first newly allocated cluster
931 * If this pointer is 0, do not return the cluster number.
934 * NOTE: This function is not responsible for turning on the DE_UPDATE bit of
935 * the de_flag field of the denode and it does not change the de_FileSize
936 * field. This is left for the caller to do.
939 extendfile(struct denode *dep, u_long count, struct buf **bpp, u_long *ncp,
945 struct msdosfsmount *pmp = dep->de_pmp;
949 * Don't try to extend the root directory
951 if (dep->de_StartCluster == MSDOSFSROOT
952 && (dep->de_Attributes & ATTR_DIRECTORY)) {
953 printf("extendfile(): attempt to extend root directory\n");
958 * If the "file's last cluster" cache entry is empty, and the file
959 * is not empty, then fill the cache entry by calling pcbmap().
962 if (dep->de_fc[FC_LASTFC].fc_frcn == FCE_EMPTY &&
963 dep->de_StartCluster != 0) {
965 error = pcbmap(dep, 0xffff, 0, &cn, 0);
966 /* we expect it to return E2BIG */
973 * Allocate a new cluster chain and cat onto the end of the
974 * file. * If the file is empty we make de_StartCluster point
975 * to the new block. Note that de_StartCluster being 0 is
976 * sufficient to be sure the file is empty since we exclude
977 * attempts to extend the root directory above, and the root
978 * dir is the only file with a startcluster of 0 that has
979 * blocks allocated (sort of).
981 if (dep->de_StartCluster == 0)
984 cn = dep->de_fc[FC_LASTFC].fc_fsrcn + 1;
985 error = clusteralloc(pmp, cn, count, CLUST_EOFE, &cn, &got);
992 * Give them the filesystem relative cluster number if they want
1000 if (dep->de_StartCluster == 0) {
1001 dep->de_StartCluster = cn;
1004 error = fatentry(FAT_SET, pmp,
1005 dep->de_fc[FC_LASTFC].fc_fsrcn,
1008 clusterfree(pmp, cn, NULL);
1011 frcn = dep->de_fc[FC_LASTFC].fc_frcn + 1;
1015 * Update the "last cluster of the file" entry in the denode's fat
1018 fc_setcache(dep, FC_LASTFC, frcn + got - 1, cn + got - 1);
1020 if (flags & DE_CLEAR) {
1023 * Get the buf header for the new block of the file.
1025 if (dep->de_Attributes & ATTR_DIRECTORY)
1026 bp = getblk(pmp->pm_devvp, cntobn(pmp, cn++),
1027 pmp->pm_bpcluster, 0, 0);
1029 bp = getblk(DETOV(dep), de_cn2bn(pmp, frcn++),
1030 pmp->pm_bpcluster, 0, 0);
1032 * Do the bmap now, as in msdosfs_write
1035 de_bn2cn(pmp, bp->b_lblkno),
1036 &bp->b_blkno, 0, 0))
1038 if (bp->b_blkno == -1)
1039 panic("extendfile: pcbmap");