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.2 2003/06/17 04:28:47 dillon 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.
64 #include <msdosfs/bpb.h>
65 #include <msdosfs/msdosfsmount.h>
66 #include <msdosfs/direntry.h>
67 #include <msdosfs/denode.h>
68 #include <msdosfs/fat.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 __P((struct msdosfsmount *pmp, u_long start,
84 u_long count, u_long fillwith,
85 u_long *retcluster, u_long *got));
86 static int chainlength __P((struct msdosfsmount *pmp, u_long start,
88 static void fatblock __P((struct msdosfsmount *pmp, u_long ofs,
89 u_long *bnp, u_long *sizep, u_long *bop));
90 static int fatchain __P((struct msdosfsmount *pmp, u_long start,
91 u_long count, u_long fillwith));
92 static void fc_lookup __P((struct denode *dep, u_long findcn,
93 u_long *frcnp, u_long *fsrcnp));
94 static void updatefats __P((struct msdosfsmount *pmp, struct buf *bp,
97 usemap_alloc __P((struct msdosfsmount *pmp, u_long cn));
99 usemap_free __P((struct msdosfsmount *pmp, u_long cn));
102 fatblock(pmp, ofs, bnp, sizep, bop)
103 struct msdosfsmount *pmp;
111 bn = ofs / pmp->pm_fatblocksize * pmp->pm_fatblocksec;
112 size = min(pmp->pm_fatblocksec, pmp->pm_FATsecs - bn)
114 bn += pmp->pm_fatblk + pmp->pm_curfat * pmp->pm_FATsecs;
121 *bop = ofs % pmp->pm_fatblocksize;
125 * Map the logical cluster number of a file into a physical disk sector
126 * that is filesystem relative.
128 * dep - address of denode representing the file of interest
129 * findcn - file relative cluster whose filesystem relative cluster number
130 * and/or block number are/is to be found
131 * bnp - address of where to place the file system relative block number.
132 * If this pointer is null then don't return this quantity.
133 * cnp - address of where to place the file system relative cluster number.
134 * If this pointer is null then don't return this quantity.
136 * NOTE: Either bnp or cnp must be non-null.
137 * This function has one side effect. If the requested file relative cluster
138 * is beyond the end of file, then the actual number of clusters in the file
139 * is returned in *cnp. This is useful for determining how long a directory is.
140 * If cnp is null, nothing is returned.
143 pcbmap(dep, findcn, bnp, cnp, sp)
145 u_long findcn; /* file relative cluster to get */
146 daddr_t *bnp; /* returned filesys relative blk number */
147 u_long *cnp; /* returned cluster number */
148 int *sp; /* returned block size */
153 u_long prevcn = 0; /* XXX: prevcn could be used unititialized */
157 struct buf *bp = NULL;
159 struct msdosfsmount *pmp = dep->de_pmp;
165 * If they don't give us someplace to return a value then don't
166 * bother doing anything.
168 if (bnp == NULL && cnp == NULL && sp == NULL)
171 cn = dep->de_StartCluster;
173 * The "file" that makes up the root directory is contiguous,
174 * permanently allocated, of fixed size, and is not made up of
175 * clusters. If the cluster number is beyond the end of the root
176 * directory, then return the number of clusters in the file.
178 if (cn == MSDOSFSROOT) {
179 if (dep->de_Attributes & ATTR_DIRECTORY) {
180 if (de_cn2off(pmp, findcn) >= dep->de_FileSize) {
182 *cnp = de_bn2cn(pmp, pmp->pm_rootdirsize);
186 *bnp = pmp->pm_rootdirblk + de_cn2bn(pmp, findcn);
190 *sp = min(pmp->pm_bpcluster,
191 dep->de_FileSize - de_cn2off(pmp, findcn));
193 } else { /* just an empty file */
201 * All other files do I/O in cluster sized blocks
204 *sp = pmp->pm_bpcluster;
207 * Rummage around in the fat cache, maybe we can avoid tromping
208 * thru every fat entry for the file. And, keep track of how far
209 * off the cache was from where we wanted to be.
212 fc_lookup(dep, findcn, &i, &cn);
213 if ((bn = findcn - i) >= LMMAX)
219 * Handle all other files or directories the normal way.
221 for (; i < findcn; i++) {
223 * Stop with all reserved clusters, not just with EOF.
225 if ((cn | ~pmp->pm_fatmask) >= CLUST_RSRVD)
227 byteoffset = FATOFS(pmp, cn);
228 fatblock(pmp, byteoffset, &bn, &bsize, &bo);
232 error = bread(pmp->pm_devvp, bn, bsize, NOCRED, &bp);
241 cn = getulong(&bp->b_data[bo]);
243 cn = getushort(&bp->b_data[bo]);
244 if (FAT12(pmp) && (prevcn & 1))
246 cn &= pmp->pm_fatmask;
249 * Force the special cluster numbers
250 * to be the same for all cluster sizes
251 * to let the rest of msdosfs handle
252 * all cases the same.
254 if ((cn | ~pmp->pm_fatmask) >= CLUST_RSRVD)
255 cn |= ~pmp->pm_fatmask;
258 if (!MSDOSFSEOF(pmp, cn)) {
262 *bnp = cntobn(pmp, cn);
265 fc_setcache(dep, FC_LASTMAP, i, cn);
274 /* update last file cluster entry in the fat cache */
275 fc_setcache(dep, FC_LASTFC, i - 1, prevcn);
280 * Find the closest entry in the fat cache to the cluster we are looking
284 fc_lookup(dep, findcn, frcnp, fsrcnp)
292 struct fatcache *closest = 0;
294 for (i = 0; i < FC_SIZE; i++) {
295 cn = dep->de_fc[i].fc_frcn;
296 if (cn != FCE_EMPTY && cn <= findcn) {
297 if (closest == 0 || cn > closest->fc_frcn)
298 closest = &dep->de_fc[i];
302 *frcnp = closest->fc_frcn;
303 *fsrcnp = closest->fc_fsrcn;
308 * Purge the fat cache in denode dep of all entries relating to file
309 * relative cluster frcn and beyond.
317 struct fatcache *fcp;
320 for (i = 0; i < FC_SIZE; i++, fcp++) {
321 if (fcp->fc_frcn >= frcn)
322 fcp->fc_frcn = FCE_EMPTY;
328 * If mirroring the fat, update all copies, with the first copy as last.
329 * Else update only the current fat (ignoring the others).
331 * pmp - msdosfsmount structure for filesystem to update
332 * bp - addr of modified fat block
333 * fatbn - block number relative to begin of filesystem of the modified fat block.
336 updatefats(pmp, bp, fatbn)
337 struct msdosfsmount *pmp;
345 printf("updatefats(pmp %p, bp %p, fatbn %lu)\n", pmp, bp, fatbn);
349 * If we have an FSInfo block, update it.
351 if (pmp->pm_fsinfo) {
352 u_long cn = pmp->pm_nxtfree;
354 if (pmp->pm_freeclustercount
355 && (pmp->pm_inusemap[cn / N_INUSEBITS]
356 & (1 << (cn % N_INUSEBITS)))) {
358 * The cluster indicated in FSInfo isn't free
359 * any longer. Got get a new free one.
361 for (cn = 0; cn < pmp->pm_maxcluster; cn += N_INUSEBITS)
362 if (pmp->pm_inusemap[cn / N_INUSEBITS] != (u_int)-1)
365 + ffs(pmp->pm_inusemap[cn / N_INUSEBITS]
368 if (bread(pmp->pm_devvp, pmp->pm_fsinfo, fsi_size(pmp),
369 NOCRED, &bpn) != 0) {
371 * Ignore the error, but turn off FSInfo update for the future.
376 struct fsinfo *fp = (struct fsinfo *)bpn->b_data;
378 putulong(fp->fsinfree, pmp->pm_freeclustercount);
379 putulong(fp->fsinxtfree, pmp->pm_nxtfree);
380 if (pmp->pm_flags & MSDOSFSMNT_WAITONFAT)
387 if (pmp->pm_flags & MSDOSFS_FATMIRROR) {
389 * Now copy the block(s) of the modified fat to the other copies of
390 * the fat and write them out. This is faster than reading in the
391 * other fats and then writing them back out. This could tie up
392 * the fat for quite a while. Preventing others from accessing it.
393 * To prevent us from going after the fat quite so much we use
394 * delayed writes, unless they specfied "synchronous" when the
395 * filesystem was mounted. If synch is asked for then use
396 * bwrite()'s and really slow things down.
398 for (i = 1; i < pmp->pm_FATs; i++) {
399 fatbn += pmp->pm_FATsecs;
400 /* getblk() never fails */
401 bpn = getblk(pmp->pm_devvp, fatbn, bp->b_bcount, 0, 0);
402 bcopy(bp->b_data, bpn->b_data, bp->b_bcount);
403 if (pmp->pm_flags & MSDOSFSMNT_WAITONFAT)
411 * Write out the first (or current) fat last.
413 if (pmp->pm_flags & MSDOSFSMNT_WAITONFAT)
418 * Maybe update fsinfo sector here?
423 * Updating entries in 12 bit fats is a pain in the butt.
425 * The following picture shows where nibbles go when moving from a 12 bit
426 * cluster number into the appropriate bytes in the FAT.
428 * byte m byte m+1 byte m+2
429 * +----+----+ +----+----+ +----+----+
430 * | 0 1 | | 2 3 | | 4 5 | FAT bytes
431 * +----+----+ +----+----+ +----+----+
433 * +----+----+----+ +----+----+----+
434 * | 3 0 1 | | 4 5 2 |
435 * +----+----+----+ +----+----+----+
436 * cluster n cluster n+1
438 * Where n is even. m = n + (n >> 2)
442 usemap_alloc(pmp, cn)
443 struct msdosfsmount *pmp;
447 pmp->pm_inusemap[cn / N_INUSEBITS] |= 1 << (cn % N_INUSEBITS);
448 pmp->pm_freeclustercount--;
453 struct msdosfsmount *pmp;
457 pmp->pm_freeclustercount++;
458 pmp->pm_inusemap[cn / N_INUSEBITS] &= ~(1 << (cn % N_INUSEBITS));
462 clusterfree(pmp, cluster, oldcnp)
463 struct msdosfsmount *pmp;
470 usemap_free(pmp, cluster);
471 error = fatentry(FAT_GET_AND_SET, pmp, cluster, &oldcn, MSDOSFSFREE);
473 usemap_alloc(pmp, cluster);
477 * If the cluster was successfully marked free, then update
478 * the count of free clusters, and turn off the "allocated"
479 * bit in the "in use" cluster bit map.
487 * Get or Set or 'Get and Set' the cluster'th entry in the fat.
489 * function - whether to get or set a fat entry
490 * pmp - address of the msdosfsmount structure for the filesystem
491 * whose fat is to be manipulated.
492 * cn - which cluster is of interest
493 * oldcontents - address of a word that is to receive the contents of the
494 * cluster'th entry if this is a get function
495 * newcontents - the new value to be written into the cluster'th element of
496 * the fat if this is a set function.
498 * This function can also be used to free a cluster by setting the fat entry
499 * for a cluster to 0.
501 * All copies of the fat are updated if this is a set function. NOTE: If
502 * fatentry() marks a cluster as free it does not update the inusemap in
503 * the msdosfsmount structure. This is left to the caller.
506 fatentry(function, pmp, cn, oldcontents, newcontents)
508 struct msdosfsmount *pmp;
515 u_long bn, bo, bsize, byteoffset;
519 printf("fatentry(func %d, pmp %p, clust %lu, oldcon %p, newcon %lx)\n",
520 function, pmp, cn, oldcontents, newcontents);
525 * Be sure they asked us to do something.
527 if ((function & (FAT_SET | FAT_GET)) == 0) {
528 printf("fatentry(): function code doesn't specify get or set\n");
533 * If they asked us to return a cluster number but didn't tell us
534 * where to put it, give them an error.
536 if ((function & FAT_GET) && oldcontents == NULL) {
537 printf("fatentry(): get function with no place to put result\n");
543 * Be sure the requested cluster is in the filesystem.
545 if (cn < CLUST_FIRST || cn > pmp->pm_maxcluster)
548 byteoffset = FATOFS(pmp, cn);
549 fatblock(pmp, byteoffset, &bn, &bsize, &bo);
550 error = bread(pmp->pm_devvp, bn, bsize, NOCRED, &bp);
556 if (function & FAT_GET) {
558 readcn = getulong(&bp->b_data[bo]);
560 readcn = getushort(&bp->b_data[bo]);
561 if (FAT12(pmp) & (cn & 1))
563 readcn &= pmp->pm_fatmask;
564 /* map reserved fat entries to same values for all fats */
565 if ((readcn | ~pmp->pm_fatmask) >= CLUST_RSRVD)
566 readcn |= ~pmp->pm_fatmask;
567 *oldcontents = readcn;
569 if (function & FAT_SET) {
570 switch (pmp->pm_fatmask) {
572 readcn = getushort(&bp->b_data[bo]);
575 readcn |= newcontents << 4;
578 readcn |= newcontents & 0xfff;
580 putushort(&bp->b_data[bo], readcn);
583 putushort(&bp->b_data[bo], newcontents);
587 * According to spec we have to retain the
588 * high order bits of the fat entry.
590 readcn = getulong(&bp->b_data[bo]);
591 readcn &= ~FAT32_MASK;
592 readcn |= newcontents & FAT32_MASK;
593 putulong(&bp->b_data[bo], readcn);
596 updatefats(pmp, bp, bn);
606 * Update a contiguous cluster chain
609 * start - first cluster of chain
610 * count - number of clusters in chain
611 * fillwith - what to write into fat entry of last cluster
614 fatchain(pmp, start, count, fillwith)
615 struct msdosfsmount *pmp;
621 u_long bn, bo, bsize, byteoffset, readcn, newc;
625 printf("fatchain(pmp %p, start %lu, count %lu, fillwith %lx)\n",
626 pmp, start, count, fillwith);
629 * Be sure the clusters are in the filesystem.
631 if (start < CLUST_FIRST || start + count - 1 > pmp->pm_maxcluster)
635 byteoffset = FATOFS(pmp, start);
636 fatblock(pmp, byteoffset, &bn, &bsize, &bo);
637 error = bread(pmp->pm_devvp, bn, bsize, NOCRED, &bp);
644 newc = --count > 0 ? start : fillwith;
645 switch (pmp->pm_fatmask) {
647 readcn = getushort(&bp->b_data[bo]);
650 readcn |= newc & 0xfff;
655 putushort(&bp->b_data[bo], readcn);
661 putushort(&bp->b_data[bo], newc);
665 readcn = getulong(&bp->b_data[bo]);
666 readcn &= ~pmp->pm_fatmask;
667 readcn |= newc & pmp->pm_fatmask;
668 putulong(&bp->b_data[bo], readcn);
675 updatefats(pmp, bp, bn);
682 * Check the length of a free cluster chain starting at start.
685 * start - start of chain
686 * count - maximum interesting length
689 chainlength(pmp, start, count)
690 struct msdosfsmount *pmp;
698 max_idx = pmp->pm_maxcluster / N_INUSEBITS;
699 idx = start / N_INUSEBITS;
700 start %= N_INUSEBITS;
701 map = pmp->pm_inusemap[idx];
702 map &= ~((1 << start) - 1);
704 len = ffs(map) - 1 - start;
705 return (len > count ? count : len);
707 len = N_INUSEBITS - start;
710 while (++idx <= max_idx) {
713 map = pmp->pm_inusemap[idx];
720 return (len > count ? count : len);
724 * Allocate contigous free clusters.
727 * start - start of cluster chain.
728 * count - number of clusters to allocate.
729 * fillwith - put this value into the fat entry for the
730 * last allocated cluster.
731 * retcluster - put the first allocated cluster's number here.
732 * got - how many clusters were actually allocated.
735 chainalloc(pmp, start, count, fillwith, retcluster, got)
736 struct msdosfsmount *pmp;
746 for (cl = start, n = count; n-- > 0;)
747 usemap_alloc(pmp, cl++);
749 error = fatchain(pmp, start, count, fillwith);
753 printf("clusteralloc(): allocated cluster chain at %lu (%lu clusters)\n",
764 * Allocate contiguous free clusters.
767 * start - preferred start of cluster chain.
768 * count - number of clusters requested.
769 * fillwith - put this value into the fat entry for the
770 * last allocated cluster.
771 * retcluster - put the first allocated cluster's number here.
772 * got - how many clusters were actually allocated.
775 clusteralloc(pmp, start, count, fillwith, retcluster, got)
776 struct msdosfsmount *pmp;
784 u_long len, newst, foundl, cn, l;
785 u_long foundcn = 0; /* XXX: foundcn could be used unititialized */
789 printf("clusteralloc(): find %lu clusters\n",count);
792 if ((len = chainlength(pmp, start, count)) >= count)
793 return (chainalloc(pmp, start, count, fillwith, retcluster, got));
798 * Start at a (pseudo) random place to maximize cluster runs
799 * under multiple writers.
801 newst = random() % (pmp->pm_maxcluster + 1);
804 for (cn = newst; cn <= pmp->pm_maxcluster;) {
805 idx = cn / N_INUSEBITS;
806 map = pmp->pm_inusemap[idx];
807 map |= (1 << (cn % N_INUSEBITS)) - 1;
808 if (map != (u_int)-1) {
809 cn = idx * N_INUSEBITS + ffs(map^(u_int)-1) - 1;
810 if ((l = chainlength(pmp, cn, count)) >= count)
811 return (chainalloc(pmp, cn, count, fillwith, retcluster, got));
819 cn += N_INUSEBITS - cn % N_INUSEBITS;
821 for (cn = 0; cn < newst;) {
822 idx = cn / N_INUSEBITS;
823 map = pmp->pm_inusemap[idx];
824 map |= (1 << (cn % N_INUSEBITS)) - 1;
825 if (map != (u_int)-1) {
826 cn = idx * N_INUSEBITS + ffs(map^(u_int)-1) - 1;
827 if ((l = chainlength(pmp, cn, count)) >= count)
828 return (chainalloc(pmp, cn, count, fillwith, retcluster, got));
836 cn += N_INUSEBITS - cn % N_INUSEBITS;
843 return (chainalloc(pmp, start, len, fillwith, retcluster, got));
845 return (chainalloc(pmp, foundcn, foundl, fillwith, retcluster, got));
850 * Free a chain of clusters.
852 * pmp - address of the msdosfs mount structure for the filesystem
853 * containing the cluster chain to be freed.
854 * startcluster - number of the 1st cluster in the chain of clusters to be
858 freeclusterchain(pmp, cluster)
859 struct msdosfsmount *pmp;
863 struct buf *bp = NULL;
864 u_long bn, bo, bsize, byteoffset;
865 u_long readcn, lbn = -1;
867 while (cluster >= CLUST_FIRST && cluster <= pmp->pm_maxcluster) {
868 byteoffset = FATOFS(pmp, cluster);
869 fatblock(pmp, byteoffset, &bn, &bsize, &bo);
872 updatefats(pmp, bp, lbn);
873 error = bread(pmp->pm_devvp, bn, bsize, NOCRED, &bp);
880 usemap_free(pmp, cluster);
881 switch (pmp->pm_fatmask) {
883 readcn = getushort(&bp->b_data[bo]);
885 cluster = readcn >> 4;
887 readcn |= MSDOSFSFREE << 4;
891 readcn |= MSDOSFSFREE & 0xfff;
893 putushort(&bp->b_data[bo], readcn);
896 cluster = getushort(&bp->b_data[bo]);
897 putushort(&bp->b_data[bo], MSDOSFSFREE);
900 cluster = getulong(&bp->b_data[bo]);
901 putulong(&bp->b_data[bo],
902 (MSDOSFSFREE & FAT32_MASK) | (cluster & ~FAT32_MASK));
905 cluster &= pmp->pm_fatmask;
906 if ((cluster | ~pmp->pm_fatmask) >= CLUST_RSRVD)
907 cluster |= pmp->pm_fatmask;
910 updatefats(pmp, bp, bn);
915 * Read in fat blocks looking for free clusters. For every free cluster
916 * found turn off its corresponding bit in the pm_inusemap.
920 struct msdosfsmount *pmp;
922 struct buf *bp = NULL;
925 u_long bn, bo, bsize, byteoffset;
928 * Mark all clusters in use, we mark the free ones in the fat scan
931 for (cn = 0; cn < (pmp->pm_maxcluster + N_INUSEBITS) / N_INUSEBITS; cn++)
932 pmp->pm_inusemap[cn] = (u_int)-1;
935 * Figure how many free clusters are in the filesystem by ripping
936 * through the fat counting the number of entries whose content is
937 * zero. These represent free clusters.
939 pmp->pm_freeclustercount = 0;
940 for (cn = CLUST_FIRST; cn <= pmp->pm_maxcluster; cn++) {
941 byteoffset = FATOFS(pmp, cn);
942 bo = byteoffset % pmp->pm_fatblocksize;
944 /* Read new FAT block */
947 fatblock(pmp, byteoffset, &bn, &bsize, NULL);
948 error = bread(pmp->pm_devvp, bn, bsize, NOCRED, &bp);
955 readcn = getulong(&bp->b_data[bo]);
957 readcn = getushort(&bp->b_data[bo]);
958 if (FAT12(pmp) && (cn & 1))
960 readcn &= pmp->pm_fatmask;
963 usemap_free(pmp, cn);
970 * Allocate a new cluster and chain it onto the end of the file.
972 * dep - the file to extend
973 * count - number of clusters to allocate
974 * bpp - where to return the address of the buf header for the first new
976 * ncp - where to put cluster number of the first newly allocated cluster
977 * If this pointer is 0, do not return the cluster number.
980 * NOTE: This function is not responsible for turning on the DE_UPDATE bit of
981 * the de_flag field of the denode and it does not change the de_FileSize
982 * field. This is left for the caller to do.
985 extendfile(dep, count, bpp, ncp, flags)
995 struct msdosfsmount *pmp = dep->de_pmp;
999 * Don't try to extend the root directory
1001 if (dep->de_StartCluster == MSDOSFSROOT
1002 && (dep->de_Attributes & ATTR_DIRECTORY)) {
1003 printf("extendfile(): attempt to extend root directory\n");
1008 * If the "file's last cluster" cache entry is empty, and the file
1009 * is not empty, then fill the cache entry by calling pcbmap().
1012 if (dep->de_fc[FC_LASTFC].fc_frcn == FCE_EMPTY &&
1013 dep->de_StartCluster != 0) {
1015 error = pcbmap(dep, 0xffff, 0, &cn, 0);
1016 /* we expect it to return E2BIG */
1023 * Allocate a new cluster chain and cat onto the end of the
1024 * file. * If the file is empty we make de_StartCluster point
1025 * to the new block. Note that de_StartCluster being 0 is
1026 * sufficient to be sure the file is empty since we exclude
1027 * attempts to extend the root directory above, and the root
1028 * dir is the only file with a startcluster of 0 that has
1029 * blocks allocated (sort of).
1031 if (dep->de_StartCluster == 0)
1034 cn = dep->de_fc[FC_LASTFC].fc_fsrcn + 1;
1035 error = clusteralloc(pmp, cn, count, CLUST_EOFE, &cn, &got);
1042 * Give them the filesystem relative cluster number if they want
1050 if (dep->de_StartCluster == 0) {
1051 dep->de_StartCluster = cn;
1054 error = fatentry(FAT_SET, pmp,
1055 dep->de_fc[FC_LASTFC].fc_fsrcn,
1058 clusterfree(pmp, cn, NULL);
1061 frcn = dep->de_fc[FC_LASTFC].fc_frcn + 1;
1065 * Update the "last cluster of the file" entry in the denode's fat
1068 fc_setcache(dep, FC_LASTFC, frcn + got - 1, cn + got - 1);
1070 if (flags & DE_CLEAR) {
1073 * Get the buf header for the new block of the file.
1075 if (dep->de_Attributes & ATTR_DIRECTORY)
1076 bp = getblk(pmp->pm_devvp, cntobn(pmp, cn++),
1077 pmp->pm_bpcluster, 0, 0);
1079 bp = getblk(DETOV(dep), de_cn2bn(pmp, frcn++),
1080 pmp->pm_bpcluster, 0, 0);
1082 * Do the bmap now, as in msdosfs_write
1085 de_bn2cn(pmp, bp->b_lblkno),
1086 &bp->b_blkno, 0, 0))
1088 if (bp->b_blkno == -1)
1089 panic("extendfile: pcbmap");