1 /* $FreeBSD: src/sys/msdosfs/msdosfs_fat.c,v 1.23 2000/01/27 14:43:06 nyan Exp $ */
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.
52 * kernel include files.
54 #include <sys/param.h>
55 #include <sys/systm.h>
57 #include <sys/mount.h> /* to define statfs structure */
58 #include <sys/vnode.h> /* to define vattr structure */
63 * msdosfs include files.
66 #include "msdosfsmount.h"
74 static int fc_fileextends; /* # of file extends */
75 static int fc_lfcempty; /* # of time last file cluster cache entry
77 static int fc_bmapcalls; /* # of times pcbmap was called */
80 static int fc_lmdistance[LMMAX];/* counters for how far off the last
81 * cluster mapped entry was. */
82 static int fc_largedistance; /* off by more than LMMAX */
84 static void fc_lookup(struct denode *dep, u_long findcn, u_long *frcnp,
88 fatblock(struct msdosfsmount *pmp, u_long ofs, u_long *bnp, u_long *sizep,
93 bn = ofs / pmp->pm_fatblocksize * pmp->pm_fatblocksec;
94 size = min(pmp->pm_fatblocksec, pmp->pm_FATsecs - bn) * DEV_BSIZE;
95 bn += pmp->pm_fatblk + pmp->pm_curfat * pmp->pm_FATsecs;
102 *bop = ofs % pmp->pm_fatblocksize;
106 * Map the logical cluster number of a file into a physical disk sector
107 * that is filesystem relative.
109 * dep - address of denode representing the file of interest
110 * findcn - file relative cluster whose filesystem relative cluster number
111 * and/or block number are/is to be found
112 * bnp - address of where to place the file system relative block number.
113 * If this pointer is null then don't return this quantity.
114 * cnp - address of where to place the file system relative cluster number.
115 * If this pointer is null then don't return this quantity.
117 * NOTE: Either bnp or cnp must be non-null.
118 * This function has one side effect. If the requested file relative cluster
119 * is beyond the end of file, then the actual number of clusters in the file
120 * is returned in *cnp. This is useful for determining how long a directory is.
121 * If cnp is null, nothing is returned.
124 pcbmap(struct denode *dep,
125 u_long findcn, /* file relative cluster to get */
126 daddr_t *bnp, /* returned filesys relative blk number */
127 u_long *cnp, /* returned cluster number */
128 int *sp) /* returned block size */
133 u_long prevcn = 0; /* XXX: prevcn could be used unititialized */
137 struct buf *bp = NULL;
139 struct msdosfsmount *pmp = dep->de_pmp;
145 * If they don't give us someplace to return a value then don't
146 * bother doing anything.
148 if (bnp == NULL && cnp == NULL && sp == NULL)
151 cn = dep->de_StartCluster;
153 * The "file" that makes up the root directory is contiguous,
154 * permanently allocated, of fixed size, and is not made up of
155 * clusters. If the cluster number is beyond the end of the root
156 * directory, then return the number of clusters in the file.
158 if (cn == MSDOSFSROOT) {
159 if (dep->de_Attributes & ATTR_DIRECTORY) {
160 if (de_cn2off(pmp, findcn) >= dep->de_FileSize) {
163 pmp->pm_rootdirsize);
167 *bnp = pmp->pm_rootdirblk +
168 de_cn2bn(pmp, findcn);
172 *sp = min(pmp->pm_bpcluster,
173 dep->de_FileSize - de_cn2off(pmp, findcn));
175 } else { /* just an empty file */
183 * All other files do I/O in cluster sized blocks
186 *sp = pmp->pm_bpcluster;
189 * Rummage around in the fat cache, maybe we can avoid tromping
190 * thru every fat entry for the file. And, keep track of how far
191 * off the cache was from where we wanted to be.
194 fc_lookup(dep, findcn, &i, &cn);
195 if ((bn = findcn - i) >= LMMAX)
201 * Handle all other files or directories the normal way.
203 for (; i < findcn; i++) {
205 * Stop with all reserved clusters, not just with EOF.
207 if ((cn | ~pmp->pm_fatmask) >= CLUST_RSRVD)
209 byteoffset = FATOFS(pmp, cn);
210 fatblock(pmp, byteoffset, &bn, &bsize, &bo);
214 error = bread(pmp->pm_devvp, de_bntodoff(pmp, bn),
224 cn = getulong(&bp->b_data[bo]);
226 cn = getushort(&bp->b_data[bo]);
227 if (FAT12(pmp) && (prevcn & 1))
229 cn &= pmp->pm_fatmask;
232 * Force the special cluster numbers
233 * to be the same for all cluster sizes
234 * to let the rest of msdosfs handle
235 * all cases the same.
237 if ((cn | ~pmp->pm_fatmask) >= CLUST_RSRVD)
238 cn |= ~pmp->pm_fatmask;
241 if (!MSDOSFSEOF(pmp, cn)) {
245 *bnp = xcntobn(pmp, cn);
248 fc_setcache(dep, FC_LASTMAP, i, cn);
257 /* update last file cluster entry in the fat cache */
258 fc_setcache(dep, FC_LASTFC, i - 1, prevcn);
263 * Find the closest entry in the fat cache to the cluster we are looking
267 fc_lookup(struct denode *dep, u_long findcn, u_long *frcnp, u_long *fsrcnp)
271 struct fatcache *closest = NULL;
273 for (i = 0; i < FC_SIZE; i++) {
274 cn = dep->de_fc[i].fc_frcn;
275 if (cn != FCE_EMPTY && cn <= findcn) {
276 if (closest == NULL || cn > closest->fc_frcn)
277 closest = &dep->de_fc[i];
281 *frcnp = closest->fc_frcn;
282 *fsrcnp = closest->fc_fsrcn;
287 * Purge the fat cache in denode dep of all entries relating to file
288 * relative cluster frcn and beyond.
291 fc_purge(struct denode *dep, u_int frcn)
294 struct fatcache *fcp;
297 for (i = 0; i < FC_SIZE; i++, fcp++) {
298 if (fcp->fc_frcn >= frcn)
299 fcp->fc_frcn = FCE_EMPTY;
305 * If mirroring the fat, update all copies, with the first copy as last.
306 * Else update only the current fat (ignoring the others).
308 * pmp - msdosfsmount structure for filesystem to update
309 * bp - addr of modified fat block
310 * fatbn - block number relative to begin of filesystem of the modified fat block.
313 updatefats(struct msdosfsmount *pmp, struct buf *bp, u_long fatbn)
318 mprintf("updatefats(pmp %p, bp %p, fatbn %lu)\n", pmp, bp, fatbn);
321 * If we have an FSInfo block, update it.
323 if (pmp->pm_fsinfo) {
324 u_long cn = pmp->pm_nxtfree;
326 if (pmp->pm_freeclustercount
327 && (pmp->pm_inusemap[cn / N_INUSEBITS]
328 & (1 << (cn % N_INUSEBITS)))) {
330 * The cluster indicated in FSInfo isn't free
331 * any longer. Got get a new free one.
333 for (cn = 0; cn < pmp->pm_maxcluster; cn += N_INUSEBITS)
334 if (pmp->pm_inusemap[cn / N_INUSEBITS] != (u_int)-1)
337 + ffs(pmp->pm_inusemap[cn / N_INUSEBITS]
340 if (bread(pmp->pm_devvp, de_bntodoff(pmp, pmp->pm_fsinfo),
341 fsi_size(pmp), &bpn) != 0) {
343 * Ignore the error, but turn off FSInfo update for the future.
348 struct fsinfo *fp = (struct fsinfo *)bpn->b_data;
350 putulong(fp->fsinfree, pmp->pm_freeclustercount);
351 putulong(fp->fsinxtfree, pmp->pm_nxtfree);
352 if (pmp->pm_flags & MSDOSFSMNT_WAITONFAT)
359 if (pmp->pm_flags & MSDOSFS_FATMIRROR) {
361 * Now copy the block(s) of the modified fat to the other copies of
362 * the fat and write them out. This is faster than reading in the
363 * other fats and then writing them back out. This could tie up
364 * the fat for quite a while. Preventing others from accessing it.
365 * To prevent us from going after the fat quite so much we use
366 * delayed writes, unless they specfied "synchronous" when the
367 * filesystem was mounted. If synch is asked for then use
368 * bwrite()'s and really slow things down.
370 for (i = 1; i < pmp->pm_FATs; i++) {
371 fatbn += pmp->pm_FATsecs;
372 /* getblk() never fails */
373 bpn = getblk(pmp->pm_devvp, de_bntodoff(pmp, fatbn),
375 bcopy(bp->b_data, bpn->b_data, bp->b_bcount);
376 if (pmp->pm_flags & MSDOSFSMNT_WAITONFAT)
384 * Write out the first (or current) fat last.
386 if (pmp->pm_flags & MSDOSFSMNT_WAITONFAT)
391 * Maybe update fsinfo sector here?
396 * Updating entries in 12 bit fats is a pain in the butt.
398 * The following picture shows where nibbles go when moving from a 12 bit
399 * cluster number into the appropriate bytes in the FAT.
401 * byte m byte m+1 byte m+2
402 * +----+----+ +----+----+ +----+----+
403 * | 0 1 | | 2 3 | | 4 5 | FAT bytes
404 * +----+----+ +----+----+ +----+----+
406 * +----+----+----+ +----+----+----+
407 * | 3 0 1 | | 4 5 2 |
408 * +----+----+----+ +----+----+----+
409 * cluster n cluster n+1
411 * Where n is even. m = n + (n >> 2)
415 usemap_alloc(struct msdosfsmount *pmp, u_long cn)
417 pmp->pm_inusemap[cn / N_INUSEBITS] |= 1 << (cn % N_INUSEBITS);
418 pmp->pm_freeclustercount--;
422 usemap_free(struct msdosfsmount *pmp, u_long cn)
424 pmp->pm_freeclustercount++;
425 pmp->pm_inusemap[cn / N_INUSEBITS] &= ~(1 << (cn % N_INUSEBITS));
429 clusterfree(struct msdosfsmount *pmp, u_long cluster, u_long *oldcnp)
434 usemap_free(pmp, cluster);
435 error = fatentry(FAT_GET_AND_SET, pmp, cluster, &oldcn, MSDOSFSFREE);
437 usemap_alloc(pmp, cluster);
441 * If the cluster was successfully marked free, then update
442 * the count of free clusters, and turn off the "allocated"
443 * bit in the "in use" cluster bit map.
451 * Get or Set or 'Get and Set' the cluster'th entry in the fat.
453 * function - whether to get or set a fat entry
454 * pmp - address of the msdosfsmount structure for the filesystem
455 * whose fat is to be manipulated.
456 * cn - which cluster is of interest
457 * oldcontents - address of a word that is to receive the contents of the
458 * cluster'th entry if this is a get function
459 * newcontents - the new value to be written into the cluster'th element of
460 * the fat if this is a set function.
462 * This function can also be used to free a cluster by setting the fat entry
463 * for a cluster to 0.
465 * All copies of the fat are updated if this is a set function. NOTE: If
466 * fatentry() marks a cluster as free it does not update the inusemap in
467 * the msdosfsmount structure. This is left to the caller.
470 fatentry(int function, struct msdosfsmount *pmp, u_long cn, u_long *oldcontents,
475 u_long bn, bo, bsize, byteoffset;
478 mprintf("fatentry(func %d, pmp %p, clust %lu, oldcon %p, newcon %lx)\n",
479 function, pmp, cn, oldcontents, newcontents);
483 * Be sure they asked us to do something.
485 if ((function & (FAT_SET | FAT_GET)) == 0) {
486 kprintf("fatentry(): function code doesn't specify get or set\n");
491 * If they asked us to return a cluster number but didn't tell us
492 * where to put it, give them an error.
494 if ((function & FAT_GET) && oldcontents == NULL) {
495 kprintf("fatentry(): get function with no place to put result\n");
501 * Be sure the requested cluster is in the filesystem.
503 if (cn < CLUST_FIRST || cn > pmp->pm_maxcluster)
506 byteoffset = FATOFS(pmp, cn);
507 fatblock(pmp, byteoffset, &bn, &bsize, &bo);
508 error = bread(pmp->pm_devvp, de_bntodoff(pmp, bn), bsize, &bp);
514 if (function & FAT_GET) {
516 readcn = getulong(&bp->b_data[bo]);
518 readcn = getushort(&bp->b_data[bo]);
519 if (FAT12(pmp) & (cn & 1))
521 readcn &= pmp->pm_fatmask;
522 /* map reserved fat entries to same values for all fats */
523 if ((readcn | ~pmp->pm_fatmask) >= CLUST_RSRVD)
524 readcn |= ~pmp->pm_fatmask;
525 *oldcontents = readcn;
527 if (function & FAT_SET) {
528 switch (pmp->pm_fatmask) {
530 readcn = getushort(&bp->b_data[bo]);
533 readcn |= newcontents << 4;
536 readcn |= newcontents & 0xfff;
538 putushort(&bp->b_data[bo], readcn);
541 putushort(&bp->b_data[bo], newcontents);
545 * According to spec we have to retain the
546 * high order bits of the fat entry.
548 readcn = getulong(&bp->b_data[bo]);
549 readcn &= ~FAT32_MASK;
550 readcn |= newcontents & FAT32_MASK;
551 putulong(&bp->b_data[bo], readcn);
554 updatefats(pmp, bp, bn);
564 * Update a contiguous cluster chain
567 * start - first cluster of chain
568 * count - number of clusters in chain
569 * fillwith - what to write into fat entry of last cluster
572 fatchain(struct msdosfsmount *pmp, u_long start, u_long count, u_long fillwith)
575 u_long bn, bo, bsize, byteoffset, readcn, newc;
578 mprintf("fatchain(pmp %p, start %lu, count %lu, fillwith %lx)\n",
579 pmp, start, count, fillwith);
581 * Be sure the clusters are in the filesystem.
583 if (start < CLUST_FIRST || start + count - 1 > pmp->pm_maxcluster)
587 byteoffset = FATOFS(pmp, start);
588 fatblock(pmp, byteoffset, &bn, &bsize, &bo);
589 error = bread(pmp->pm_devvp, de_bntodoff(pmp, bn), bsize, &bp);
596 newc = --count > 0 ? start : fillwith;
597 switch (pmp->pm_fatmask) {
599 readcn = getushort(&bp->b_data[bo]);
602 readcn |= newc & 0xfff;
607 putushort(&bp->b_data[bo], readcn);
613 putushort(&bp->b_data[bo], newc);
617 readcn = getulong(&bp->b_data[bo]);
618 readcn &= ~pmp->pm_fatmask;
619 readcn |= newc & pmp->pm_fatmask;
620 putulong(&bp->b_data[bo], readcn);
627 updatefats(pmp, bp, bn);
634 * Check the length of a free cluster chain starting at start.
637 * start - start of chain
638 * count - maximum interesting length
641 chainlength(struct msdosfsmount *pmp, u_long start, u_long count)
647 max_idx = pmp->pm_maxcluster / N_INUSEBITS;
648 idx = start / N_INUSEBITS;
649 start %= N_INUSEBITS;
650 map = pmp->pm_inusemap[idx];
651 map &= ~((1 << start) - 1);
653 len = ffs(map) - 1 - start;
654 return (len > count ? count : len);
656 len = N_INUSEBITS - start;
659 while (++idx <= max_idx) {
662 map = pmp->pm_inusemap[idx];
669 return (len > count ? count : len);
673 * Allocate contigous free clusters.
676 * start - start of cluster chain.
677 * count - number of clusters to allocate.
678 * fillwith - put this value into the fat entry for the
679 * last allocated cluster.
680 * retcluster - put the first allocated cluster's number here.
681 * got - how many clusters were actually allocated.
684 chainalloc(struct msdosfsmount *pmp, u_long start, u_long count,
685 u_long fillwith, u_long *retcluster, u_long *got)
690 for (cl = start, n = count; n-- > 0;)
691 usemap_alloc(pmp, cl++);
693 error = fatchain(pmp, start, count, fillwith);
696 mprintf("clusteralloc(): allocated cluster chain at %lu (%lu clusters)\n",
706 * Allocate contiguous free clusters.
709 * start - preferred start of cluster chain.
710 * count - number of clusters requested.
711 * fillwith - put this value into the fat entry for the
712 * last allocated cluster.
713 * retcluster - put the first allocated cluster's number here.
714 * got - how many clusters were actually allocated.
717 clusteralloc(struct msdosfsmount *pmp, u_long start, u_long count,
718 u_long fillwith, u_long *retcluster, u_long *got)
721 u_long len, newst, foundl, cn, l;
722 u_long foundcn = 0; /* XXX: foundcn could be used unititialized */
725 mprintf("clusteralloc(): find %lu clusters\n",count);
727 if ((len = chainlength(pmp, start, count)) >= count)
728 return (chainalloc(pmp, start, count, fillwith,
734 * Start at a (pseudo) random place to maximize cluster runs
735 * under multiple writers.
737 newst = krandom() % (pmp->pm_maxcluster + 1);
740 for (cn = newst; cn <= pmp->pm_maxcluster;) {
741 idx = cn / N_INUSEBITS;
742 map = pmp->pm_inusemap[idx];
743 map |= (1 << (cn % N_INUSEBITS)) - 1;
744 if (map != (u_int)-1) {
745 cn = idx * N_INUSEBITS + ffs(map^(u_int)-1) - 1;
746 if ((l = chainlength(pmp, cn, count)) >= count)
747 return (chainalloc(pmp, cn, count, fillwith,
756 cn += N_INUSEBITS - cn % N_INUSEBITS;
758 for (cn = 0; cn < newst;) {
759 idx = cn / N_INUSEBITS;
760 map = pmp->pm_inusemap[idx];
761 map |= (1 << (cn % N_INUSEBITS)) - 1;
762 if (map != (u_int)-1) {
763 cn = idx * N_INUSEBITS + ffs(map^(u_int)-1) - 1;
764 if ((l = chainlength(pmp, cn, count)) >= count)
765 return (chainalloc(pmp, cn, count, fillwith,
774 cn += N_INUSEBITS - cn % N_INUSEBITS;
781 return (chainalloc(pmp, start, len, fillwith, retcluster, got));
783 return (chainalloc(pmp, foundcn, foundl, fillwith, retcluster,
789 * Free a chain of clusters.
791 * pmp - address of the msdosfs mount structure for the filesystem
792 * containing the cluster chain to be freed.
793 * startcluster - number of the 1st cluster in the chain of clusters to be
797 freeclusterchain(struct msdosfsmount *pmp, u_long cluster)
800 struct buf *bp = NULL;
801 u_long bn, bo, bsize, byteoffset;
802 u_long readcn, lbn = -1;
804 while (cluster >= CLUST_FIRST && cluster <= pmp->pm_maxcluster) {
805 byteoffset = FATOFS(pmp, cluster);
806 fatblock(pmp, byteoffset, &bn, &bsize, &bo);
809 updatefats(pmp, bp, lbn);
810 error = bread(pmp->pm_devvp, de_bntodoff(pmp, bn),
818 usemap_free(pmp, cluster);
819 switch (pmp->pm_fatmask) {
821 readcn = getushort(&bp->b_data[bo]);
823 cluster = readcn >> 4;
825 readcn |= MSDOSFSFREE << 4;
829 readcn |= MSDOSFSFREE & 0xfff;
831 putushort(&bp->b_data[bo], readcn);
834 cluster = getushort(&bp->b_data[bo]);
835 putushort(&bp->b_data[bo], MSDOSFSFREE);
838 cluster = getulong(&bp->b_data[bo]);
839 putulong(&bp->b_data[bo],
840 (MSDOSFSFREE & FAT32_MASK) |
841 (cluster & ~FAT32_MASK));
844 cluster &= pmp->pm_fatmask;
845 if ((cluster | ~pmp->pm_fatmask) >= CLUST_RSRVD)
846 cluster |= pmp->pm_fatmask;
849 updatefats(pmp, bp, bn);
854 * Read in fat blocks looking for free clusters. For every free cluster
855 * found turn off its corresponding bit in the pm_inusemap.
858 fillinusemap(struct msdosfsmount *pmp)
860 struct buf *bp = NULL;
863 u_long bn, bo, bsize, byteoffset;
866 * Mark all clusters in use, we mark the free ones in the fat scan
869 for (cn = 0; cn < (pmp->pm_maxcluster + N_INUSEBITS) / N_INUSEBITS; cn++)
870 pmp->pm_inusemap[cn] = (u_int)-1;
873 * Figure how many free clusters are in the filesystem by ripping
874 * through the fat counting the number of entries whose content is
875 * zero. These represent free clusters.
877 pmp->pm_freeclustercount = 0;
878 for (cn = CLUST_FIRST; cn <= pmp->pm_maxcluster; cn++) {
879 byteoffset = FATOFS(pmp, cn);
880 bo = byteoffset % pmp->pm_fatblocksize;
882 /* Read new FAT block */
885 fatblock(pmp, byteoffset, &bn, &bsize, NULL);
886 error = bread(pmp->pm_devvp, de_bntodoff(pmp, bn),
894 readcn = getulong(&bp->b_data[bo]);
896 readcn = getushort(&bp->b_data[bo]);
897 if (FAT12(pmp) && (cn & 1))
899 readcn &= pmp->pm_fatmask;
902 usemap_free(pmp, cn);
909 * Allocate a new cluster and chain it onto the end of the file.
911 * dep - the file to extend
912 * count - number of clusters to allocate
913 * bpp - where to return the address of the buf header for the first new
915 * ncp - where to put cluster number of the first newly allocated cluster
916 * If this pointer is 0, do not return the cluster number.
919 * NOTE: This function is not responsible for turning on the DE_UPDATE bit of
920 * the de_flag field of the denode and it does not change the de_FileSize
921 * field. This is left for the caller to do.
924 extendfile(struct denode *dep, u_long count, struct buf **bpp, u_long *ncp,
930 struct msdosfsmount *pmp = dep->de_pmp;
934 * Don't try to extend the root directory
936 if (dep->de_StartCluster == MSDOSFSROOT
937 && (dep->de_Attributes & ATTR_DIRECTORY)) {
938 kprintf("extendfile(): attempt to extend root directory\n");
943 * If the "file's last cluster" cache entry is empty, and the file
944 * is not empty, then fill the cache entry by calling pcbmap().
947 if (dep->de_fc[FC_LASTFC].fc_frcn == FCE_EMPTY &&
948 dep->de_StartCluster != 0) {
950 error = pcbmap(dep, 0xffff, NULL, &cn, NULL);
951 /* we expect it to return E2BIG */
958 * Allocate a new cluster chain and cat onto the end of the
959 * file. * If the file is empty we make de_StartCluster point
960 * to the new block. Note that de_StartCluster being 0 is
961 * sufficient to be sure the file is empty since we exclude
962 * attempts to extend the root directory above, and the root
963 * dir is the only file with a startcluster of 0 that has
964 * blocks allocated (sort of).
966 if (dep->de_StartCluster == 0)
969 cn = dep->de_fc[FC_LASTFC].fc_fsrcn + 1;
970 error = clusteralloc(pmp, cn, count, CLUST_EOFE, &cn, &got);
977 * Give them the filesystem relative cluster number if they want
985 if (dep->de_StartCluster == 0) {
986 dep->de_StartCluster = cn;
989 error = fatentry(FAT_SET, pmp,
990 dep->de_fc[FC_LASTFC].fc_fsrcn,
993 clusterfree(pmp, cn, NULL);
996 frcn = dep->de_fc[FC_LASTFC].fc_frcn + 1;
1000 * Update the "last cluster of the file" entry in the
1001 * denode's fat cache.
1003 fc_setcache(dep, FC_LASTFC, frcn + got - 1, cn + got - 1);
1005 if (flags & DE_CLEAR) {
1008 * Get the buf header for the new block of the file.
1010 if (dep->de_Attributes & ATTR_DIRECTORY) {
1011 bp = getblk(pmp->pm_devvp,
1013 pmp->pm_bpcluster, 0, 0);
1018 bp = getblk(DETOV(dep),
1019 de_cn2doff(pmp, frcn),
1020 pmp->pm_bpcluster, 0, 0);
1023 * Do the bmap now, as in msdosfs_write
1026 de_bn2cn(pmp, de_off2bn(pmp,
1027 bp->b_bio1.bio_offset)),
1028 &dblkno, NULL, NULL)) {
1029 bp->b_bio2.bio_offset = NOOFFSET;
1031 bp->b_bio2.bio_offset = de_bntodoff(pmp, dblkno);
1033 if (bp->b_bio2.bio_offset == NOOFFSET)
1034 panic("extendfile: pcbmap");