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 int chainalloc (struct msdosfsmount *pmp, u_long start,
85 u_long count, u_long fillwith,
86 u_long *retcluster, u_long *got);
87 static int chainlength (struct msdosfsmount *pmp, u_long start,
89 static void fatblock (struct msdosfsmount *pmp, u_long ofs,
90 u_long *bnp, u_long *sizep, u_long *bop);
91 static int fatchain (struct msdosfsmount *pmp, u_long start,
92 u_long count, u_long fillwith);
93 static void fc_lookup (struct denode *dep, u_long findcn,
94 u_long *frcnp, u_long *fsrcnp);
95 static void updatefats (struct msdosfsmount *pmp, struct buf *bp,
98 usemap_alloc (struct msdosfsmount *pmp, u_long cn);
100 usemap_free (struct msdosfsmount *pmp, u_long cn);
103 fatblock(struct msdosfsmount *pmp, u_long ofs, u_long *bnp, u_long *sizep,
108 bn = ofs / pmp->pm_fatblocksize * pmp->pm_fatblocksec;
109 size = min(pmp->pm_fatblocksec, pmp->pm_FATsecs - bn)
111 bn += pmp->pm_fatblk + pmp->pm_curfat * pmp->pm_FATsecs;
118 *bop = ofs % pmp->pm_fatblocksize;
122 * Map the logical cluster number of a file into a physical disk sector
123 * that is filesystem relative.
125 * dep - address of denode representing the file of interest
126 * findcn - file relative cluster whose filesystem relative cluster number
127 * and/or block number are/is to be found
128 * bnp - address of where to place the file system relative block number.
129 * If this pointer is null then don't return this quantity.
130 * cnp - address of where to place the file system relative cluster number.
131 * If this pointer is null then don't return this quantity.
133 * NOTE: Either bnp or cnp must be non-null.
134 * This function has one side effect. If the requested file relative cluster
135 * is beyond the end of file, then the actual number of clusters in the file
136 * is returned in *cnp. This is useful for determining how long a directory is.
137 * If cnp is null, nothing is returned.
140 pcbmap(struct denode *dep,
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)
215 * Handle all other files or directories the normal way.
217 for (; i < findcn; i++) {
219 * Stop with all reserved clusters, not just with EOF.
221 if ((cn | ~pmp->pm_fatmask) >= CLUST_RSRVD)
223 byteoffset = FATOFS(pmp, cn);
224 fatblock(pmp, byteoffset, &bn, &bsize, &bo);
228 error = bread(pmp->pm_devvp, de_bntodoff(pmp, bn), bsize, &bp);
237 cn = getulong(&bp->b_data[bo]);
239 cn = getushort(&bp->b_data[bo]);
240 if (FAT12(pmp) && (prevcn & 1))
242 cn &= pmp->pm_fatmask;
245 * Force the special cluster numbers
246 * to be the same for all cluster sizes
247 * to let the rest of msdosfs handle
248 * all cases the same.
250 if ((cn | ~pmp->pm_fatmask) >= CLUST_RSRVD)
251 cn |= ~pmp->pm_fatmask;
254 if (!MSDOSFSEOF(pmp, cn)) {
258 *bnp = xcntobn(pmp, cn);
261 fc_setcache(dep, FC_LASTMAP, i, cn);
270 /* update last file cluster entry in the fat cache */
271 fc_setcache(dep, FC_LASTFC, i - 1, prevcn);
276 * Find the closest entry in the fat cache to the cluster we are looking
280 fc_lookup(struct denode *dep, u_long findcn, u_long *frcnp, u_long *fsrcnp)
284 struct fatcache *closest = NULL;
286 for (i = 0; i < FC_SIZE; i++) {
287 cn = dep->de_fc[i].fc_frcn;
288 if (cn != FCE_EMPTY && cn <= findcn) {
289 if (closest == NULL || cn > closest->fc_frcn)
290 closest = &dep->de_fc[i];
294 *frcnp = closest->fc_frcn;
295 *fsrcnp = closest->fc_fsrcn;
300 * Purge the fat cache in denode dep of all entries relating to file
301 * relative cluster frcn and beyond.
304 fc_purge(struct denode *dep, u_int frcn)
307 struct fatcache *fcp;
310 for (i = 0; i < FC_SIZE; i++, fcp++) {
311 if (fcp->fc_frcn >= frcn)
312 fcp->fc_frcn = FCE_EMPTY;
318 * If mirroring the fat, update all copies, with the first copy as last.
319 * Else update only the current fat (ignoring the others).
321 * pmp - msdosfsmount structure for filesystem to update
322 * bp - addr of modified fat block
323 * fatbn - block number relative to begin of filesystem of the modified fat block.
326 updatefats(struct msdosfsmount *pmp, struct buf *bp, u_long fatbn)
332 kprintf("updatefats(pmp %p, bp %p, fatbn %lu)\n", pmp, bp, fatbn);
336 * If we have an FSInfo block, update it.
338 if (pmp->pm_fsinfo) {
339 u_long cn = pmp->pm_nxtfree;
341 if (pmp->pm_freeclustercount
342 && (pmp->pm_inusemap[cn / N_INUSEBITS]
343 & (1 << (cn % N_INUSEBITS)))) {
345 * The cluster indicated in FSInfo isn't free
346 * any longer. Got get a new free one.
348 for (cn = 0; cn < pmp->pm_maxcluster; cn += N_INUSEBITS)
349 if (pmp->pm_inusemap[cn / N_INUSEBITS] != (u_int)-1)
352 + ffs(pmp->pm_inusemap[cn / N_INUSEBITS]
355 if (bread(pmp->pm_devvp, de_bntodoff(pmp, pmp->pm_fsinfo), fsi_size(pmp), &bpn) != 0) {
357 * Ignore the error, but turn off FSInfo update for the future.
362 struct fsinfo *fp = (struct fsinfo *)bpn->b_data;
364 putulong(fp->fsinfree, pmp->pm_freeclustercount);
365 putulong(fp->fsinxtfree, pmp->pm_nxtfree);
366 if (pmp->pm_flags & MSDOSFSMNT_WAITONFAT)
373 if (pmp->pm_flags & MSDOSFS_FATMIRROR) {
375 * Now copy the block(s) of the modified fat to the other copies of
376 * the fat and write them out. This is faster than reading in the
377 * other fats and then writing them back out. This could tie up
378 * the fat for quite a while. Preventing others from accessing it.
379 * To prevent us from going after the fat quite so much we use
380 * delayed writes, unless they specfied "synchronous" when the
381 * filesystem was mounted. If synch is asked for then use
382 * bwrite()'s and really slow things down.
384 for (i = 1; i < pmp->pm_FATs; i++) {
385 fatbn += pmp->pm_FATsecs;
386 /* getblk() never fails */
387 bpn = getblk(pmp->pm_devvp, de_bntodoff(pmp, fatbn),
389 bcopy(bp->b_data, bpn->b_data, bp->b_bcount);
390 if (pmp->pm_flags & MSDOSFSMNT_WAITONFAT)
398 * Write out the first (or current) fat last.
400 if (pmp->pm_flags & MSDOSFSMNT_WAITONFAT)
405 * Maybe update fsinfo sector here?
410 * Updating entries in 12 bit fats is a pain in the butt.
412 * The following picture shows where nibbles go when moving from a 12 bit
413 * cluster number into the appropriate bytes in the FAT.
415 * byte m byte m+1 byte m+2
416 * +----+----+ +----+----+ +----+----+
417 * | 0 1 | | 2 3 | | 4 5 | FAT bytes
418 * +----+----+ +----+----+ +----+----+
420 * +----+----+----+ +----+----+----+
421 * | 3 0 1 | | 4 5 2 |
422 * +----+----+----+ +----+----+----+
423 * cluster n cluster n+1
425 * Where n is even. m = n + (n >> 2)
429 usemap_alloc(struct msdosfsmount *pmp, u_long cn)
432 pmp->pm_inusemap[cn / N_INUSEBITS] |= 1 << (cn % N_INUSEBITS);
433 pmp->pm_freeclustercount--;
437 usemap_free(struct msdosfsmount *pmp, u_long cn)
440 pmp->pm_freeclustercount++;
441 pmp->pm_inusemap[cn / N_INUSEBITS] &= ~(1 << (cn % N_INUSEBITS));
445 clusterfree(struct msdosfsmount *pmp, u_long cluster, u_long *oldcnp)
450 usemap_free(pmp, cluster);
451 error = fatentry(FAT_GET_AND_SET, pmp, cluster, &oldcn, MSDOSFSFREE);
453 usemap_alloc(pmp, cluster);
457 * If the cluster was successfully marked free, then update
458 * the count of free clusters, and turn off the "allocated"
459 * bit in the "in use" cluster bit map.
467 * Get or Set or 'Get and Set' the cluster'th entry in the fat.
469 * function - whether to get or set a fat entry
470 * pmp - address of the msdosfsmount structure for the filesystem
471 * whose fat is to be manipulated.
472 * cn - which cluster is of interest
473 * oldcontents - address of a word that is to receive the contents of the
474 * cluster'th entry if this is a get function
475 * newcontents - the new value to be written into the cluster'th element of
476 * the fat if this is a set function.
478 * This function can also be used to free a cluster by setting the fat entry
479 * for a cluster to 0.
481 * All copies of the fat are updated if this is a set function. NOTE: If
482 * fatentry() marks a cluster as free it does not update the inusemap in
483 * the msdosfsmount structure. This is left to the caller.
486 fatentry(int function, struct msdosfsmount *pmp, u_long cn, u_long *oldcontents,
491 u_long bn, bo, bsize, byteoffset;
495 kprintf("fatentry(func %d, pmp %p, clust %lu, oldcon %p, newcon %lx)\n",
496 function, pmp, cn, oldcontents, newcontents);
501 * Be sure they asked us to do something.
503 if ((function & (FAT_SET | FAT_GET)) == 0) {
504 kprintf("fatentry(): function code doesn't specify get or set\n");
509 * If they asked us to return a cluster number but didn't tell us
510 * where to put it, give them an error.
512 if ((function & FAT_GET) && oldcontents == NULL) {
513 kprintf("fatentry(): get function with no place to put result\n");
519 * Be sure the requested cluster is in the filesystem.
521 if (cn < CLUST_FIRST || cn > pmp->pm_maxcluster)
524 byteoffset = FATOFS(pmp, cn);
525 fatblock(pmp, byteoffset, &bn, &bsize, &bo);
526 error = bread(pmp->pm_devvp, de_bntodoff(pmp, bn), bsize, &bp);
532 if (function & FAT_GET) {
534 readcn = getulong(&bp->b_data[bo]);
536 readcn = getushort(&bp->b_data[bo]);
537 if (FAT12(pmp) & (cn & 1))
539 readcn &= pmp->pm_fatmask;
540 /* map reserved fat entries to same values for all fats */
541 if ((readcn | ~pmp->pm_fatmask) >= CLUST_RSRVD)
542 readcn |= ~pmp->pm_fatmask;
543 *oldcontents = readcn;
545 if (function & FAT_SET) {
546 switch (pmp->pm_fatmask) {
548 readcn = getushort(&bp->b_data[bo]);
551 readcn |= newcontents << 4;
554 readcn |= newcontents & 0xfff;
556 putushort(&bp->b_data[bo], readcn);
559 putushort(&bp->b_data[bo], newcontents);
563 * According to spec we have to retain the
564 * high order bits of the fat entry.
566 readcn = getulong(&bp->b_data[bo]);
567 readcn &= ~FAT32_MASK;
568 readcn |= newcontents & FAT32_MASK;
569 putulong(&bp->b_data[bo], readcn);
572 updatefats(pmp, bp, bn);
582 * Update a contiguous cluster chain
585 * start - first cluster of chain
586 * count - number of clusters in chain
587 * fillwith - what to write into fat entry of last cluster
590 fatchain(struct msdosfsmount *pmp, u_long start, u_long count, u_long fillwith)
593 u_long bn, bo, bsize, byteoffset, readcn, newc;
597 kprintf("fatchain(pmp %p, start %lu, count %lu, fillwith %lx)\n",
598 pmp, start, count, fillwith);
601 * Be sure the clusters are in the filesystem.
603 if (start < CLUST_FIRST || start + count - 1 > pmp->pm_maxcluster)
607 byteoffset = FATOFS(pmp, start);
608 fatblock(pmp, byteoffset, &bn, &bsize, &bo);
609 error = bread(pmp->pm_devvp, de_bntodoff(pmp, bn), bsize, &bp);
616 newc = --count > 0 ? start : fillwith;
617 switch (pmp->pm_fatmask) {
619 readcn = getushort(&bp->b_data[bo]);
622 readcn |= newc & 0xfff;
627 putushort(&bp->b_data[bo], readcn);
633 putushort(&bp->b_data[bo], newc);
637 readcn = getulong(&bp->b_data[bo]);
638 readcn &= ~pmp->pm_fatmask;
639 readcn |= newc & pmp->pm_fatmask;
640 putulong(&bp->b_data[bo], readcn);
647 updatefats(pmp, bp, bn);
654 * Check the length of a free cluster chain starting at start.
657 * start - start of chain
658 * count - maximum interesting length
661 chainlength(struct msdosfsmount *pmp, u_long start, u_long count)
667 max_idx = pmp->pm_maxcluster / N_INUSEBITS;
668 idx = start / N_INUSEBITS;
669 start %= N_INUSEBITS;
670 map = pmp->pm_inusemap[idx];
671 map &= ~((1 << start) - 1);
673 len = ffs(map) - 1 - start;
674 return (len > count ? count : len);
676 len = N_INUSEBITS - start;
679 while (++idx <= max_idx) {
682 map = pmp->pm_inusemap[idx];
689 return (len > count ? count : len);
693 * Allocate contigous free clusters.
696 * start - start of cluster chain.
697 * count - number of clusters to allocate.
698 * fillwith - put this value into the fat entry for the
699 * last allocated cluster.
700 * retcluster - put the first allocated cluster's number here.
701 * got - how many clusters were actually allocated.
704 chainalloc(struct msdosfsmount *pmp, u_long start, u_long count,
705 u_long fillwith, u_long *retcluster, u_long *got)
710 for (cl = start, n = count; n-- > 0;)
711 usemap_alloc(pmp, cl++);
713 error = fatchain(pmp, start, count, fillwith);
717 kprintf("clusteralloc(): allocated cluster chain at %lu (%lu clusters)\n",
728 * Allocate contiguous free clusters.
731 * start - preferred start of cluster chain.
732 * count - number of clusters requested.
733 * fillwith - put this value into the fat entry for the
734 * last allocated cluster.
735 * retcluster - put the first allocated cluster's number here.
736 * got - how many clusters were actually allocated.
739 clusteralloc(struct msdosfsmount *pmp, u_long start, u_long count,
740 u_long fillwith, u_long *retcluster, u_long *got)
743 u_long len, newst, foundl, cn, l;
744 u_long foundcn = 0; /* XXX: foundcn could be used unititialized */
748 kprintf("clusteralloc(): find %lu clusters\n",count);
751 if ((len = chainlength(pmp, start, count)) >= count)
752 return (chainalloc(pmp, start, count, fillwith, retcluster, got));
757 * Start at a (pseudo) random place to maximize cluster runs
758 * under multiple writers.
760 newst = krandom() % (pmp->pm_maxcluster + 1);
763 for (cn = newst; cn <= pmp->pm_maxcluster;) {
764 idx = cn / N_INUSEBITS;
765 map = pmp->pm_inusemap[idx];
766 map |= (1 << (cn % N_INUSEBITS)) - 1;
767 if (map != (u_int)-1) {
768 cn = idx * N_INUSEBITS + ffs(map^(u_int)-1) - 1;
769 if ((l = chainlength(pmp, cn, count)) >= count)
770 return (chainalloc(pmp, cn, count, fillwith, retcluster, got));
778 cn += N_INUSEBITS - cn % N_INUSEBITS;
780 for (cn = 0; cn < newst;) {
781 idx = cn / N_INUSEBITS;
782 map = pmp->pm_inusemap[idx];
783 map |= (1 << (cn % N_INUSEBITS)) - 1;
784 if (map != (u_int)-1) {
785 cn = idx * N_INUSEBITS + ffs(map^(u_int)-1) - 1;
786 if ((l = chainlength(pmp, cn, count)) >= count)
787 return (chainalloc(pmp, cn, count, fillwith, retcluster, got));
795 cn += N_INUSEBITS - cn % N_INUSEBITS;
802 return (chainalloc(pmp, start, len, fillwith, retcluster, got));
804 return (chainalloc(pmp, foundcn, foundl, fillwith, retcluster, got));
809 * Free a chain of clusters.
811 * pmp - address of the msdosfs mount structure for the filesystem
812 * containing the cluster chain to be freed.
813 * startcluster - number of the 1st cluster in the chain of clusters to be
817 freeclusterchain(struct msdosfsmount *pmp, u_long cluster)
820 struct buf *bp = NULL;
821 u_long bn, bo, bsize, byteoffset;
822 u_long readcn, lbn = -1;
824 while (cluster >= CLUST_FIRST && cluster <= pmp->pm_maxcluster) {
825 byteoffset = FATOFS(pmp, cluster);
826 fatblock(pmp, byteoffset, &bn, &bsize, &bo);
829 updatefats(pmp, bp, lbn);
830 error = bread(pmp->pm_devvp, de_bntodoff(pmp, bn), bsize, &bp);
837 usemap_free(pmp, cluster);
838 switch (pmp->pm_fatmask) {
840 readcn = getushort(&bp->b_data[bo]);
842 cluster = readcn >> 4;
844 readcn |= MSDOSFSFREE << 4;
848 readcn |= MSDOSFSFREE & 0xfff;
850 putushort(&bp->b_data[bo], readcn);
853 cluster = getushort(&bp->b_data[bo]);
854 putushort(&bp->b_data[bo], MSDOSFSFREE);
857 cluster = getulong(&bp->b_data[bo]);
858 putulong(&bp->b_data[bo],
859 (MSDOSFSFREE & FAT32_MASK) | (cluster & ~FAT32_MASK));
862 cluster &= pmp->pm_fatmask;
863 if ((cluster | ~pmp->pm_fatmask) >= CLUST_RSRVD)
864 cluster |= pmp->pm_fatmask;
867 updatefats(pmp, bp, bn);
872 * Read in fat blocks looking for free clusters. For every free cluster
873 * found turn off its corresponding bit in the pm_inusemap.
876 fillinusemap(struct msdosfsmount *pmp)
878 struct buf *bp = NULL;
881 u_long bn, bo, bsize, byteoffset;
884 * Mark all clusters in use, we mark the free ones in the fat scan
887 for (cn = 0; cn < (pmp->pm_maxcluster + N_INUSEBITS) / N_INUSEBITS; cn++)
888 pmp->pm_inusemap[cn] = (u_int)-1;
891 * Figure how many free clusters are in the filesystem by ripping
892 * through the fat counting the number of entries whose content is
893 * zero. These represent free clusters.
895 pmp->pm_freeclustercount = 0;
896 for (cn = CLUST_FIRST; cn <= pmp->pm_maxcluster; cn++) {
897 byteoffset = FATOFS(pmp, cn);
898 bo = byteoffset % pmp->pm_fatblocksize;
900 /* Read new FAT block */
903 fatblock(pmp, byteoffset, &bn, &bsize, NULL);
904 error = bread(pmp->pm_devvp, de_bntodoff(pmp, bn), bsize, &bp);
911 readcn = getulong(&bp->b_data[bo]);
913 readcn = getushort(&bp->b_data[bo]);
914 if (FAT12(pmp) && (cn & 1))
916 readcn &= pmp->pm_fatmask;
919 usemap_free(pmp, cn);
926 * Allocate a new cluster and chain it onto the end of the file.
928 * dep - the file to extend
929 * count - number of clusters to allocate
930 * bpp - where to return the address of the buf header for the first new
932 * ncp - where to put cluster number of the first newly allocated cluster
933 * If this pointer is 0, do not return the cluster number.
936 * NOTE: This function is not responsible for turning on the DE_UPDATE bit of
937 * the de_flag field of the denode and it does not change the de_FileSize
938 * field. This is left for the caller to do.
941 extendfile(struct denode *dep, u_long count, struct buf **bpp, u_long *ncp,
947 struct msdosfsmount *pmp = dep->de_pmp;
951 * Don't try to extend the root directory
953 if (dep->de_StartCluster == MSDOSFSROOT
954 && (dep->de_Attributes & ATTR_DIRECTORY)) {
955 kprintf("extendfile(): attempt to extend root directory\n");
960 * If the "file's last cluster" cache entry is empty, and the file
961 * is not empty, then fill the cache entry by calling pcbmap().
964 if (dep->de_fc[FC_LASTFC].fc_frcn == FCE_EMPTY &&
965 dep->de_StartCluster != 0) {
967 error = pcbmap(dep, 0xffff, NULL, &cn, NULL);
968 /* we expect it to return E2BIG */
975 * Allocate a new cluster chain and cat onto the end of the
976 * file. * If the file is empty we make de_StartCluster point
977 * to the new block. Note that de_StartCluster being 0 is
978 * sufficient to be sure the file is empty since we exclude
979 * attempts to extend the root directory above, and the root
980 * dir is the only file with a startcluster of 0 that has
981 * blocks allocated (sort of).
983 if (dep->de_StartCluster == 0)
986 cn = dep->de_fc[FC_LASTFC].fc_fsrcn + 1;
987 error = clusteralloc(pmp, cn, count, CLUST_EOFE, &cn, &got);
994 * Give them the filesystem relative cluster number if they want
1002 if (dep->de_StartCluster == 0) {
1003 dep->de_StartCluster = cn;
1006 error = fatentry(FAT_SET, pmp,
1007 dep->de_fc[FC_LASTFC].fc_fsrcn,
1010 clusterfree(pmp, cn, NULL);
1013 frcn = dep->de_fc[FC_LASTFC].fc_frcn + 1;
1017 * Update the "last cluster of the file" entry in the
1018 * denode's fat cache.
1020 fc_setcache(dep, FC_LASTFC, frcn + got - 1, cn + got - 1);
1022 if (flags & DE_CLEAR) {
1025 * Get the buf header for the new block of the file.
1027 if (dep->de_Attributes & ATTR_DIRECTORY) {
1028 bp = getblk(pmp->pm_devvp,
1030 pmp->pm_bpcluster, 0, 0);
1035 bp = getblk(DETOV(dep),
1036 de_cn2doff(pmp, frcn),
1037 pmp->pm_bpcluster, 0, 0);
1040 * Do the bmap now, as in msdosfs_write
1043 de_bn2cn(pmp, de_off2bn(pmp, bp->b_bio1.bio_offset)),
1044 &dblkno, NULL, NULL)) {
1045 bp->b_bio2.bio_offset = NOOFFSET;
1047 bp->b_bio2.bio_offset = de_bntodoff(pmp, dblkno);
1049 if (bp->b_bio2.bio_offset == NOOFFSET)
1050 panic("extendfile: pcbmap");