xref: /minix3/sys/fs/msdosfs/msdosfs_fat.c (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1*0a6a1f1dSLionel Sambuc /*	$NetBSD: msdosfs_fat.c,v 1.29 2015/03/28 19:24:05 maxv Exp $	*/
284d9c625SLionel Sambuc 
384d9c625SLionel Sambuc /*-
484d9c625SLionel Sambuc  * Copyright (C) 1994, 1995, 1997 Wolfgang Solfrank.
584d9c625SLionel Sambuc  * Copyright (C) 1994, 1995, 1997 TooLs GmbH.
684d9c625SLionel Sambuc  * All rights reserved.
784d9c625SLionel Sambuc  * Original code by Paul Popelka (paulp@uts.amdahl.com) (see below).
884d9c625SLionel Sambuc  *
984d9c625SLionel Sambuc  * Redistribution and use in source and binary forms, with or without
1084d9c625SLionel Sambuc  * modification, are permitted provided that the following conditions
1184d9c625SLionel Sambuc  * are met:
1284d9c625SLionel Sambuc  * 1. Redistributions of source code must retain the above copyright
1384d9c625SLionel Sambuc  *    notice, this list of conditions and the following disclaimer.
1484d9c625SLionel Sambuc  * 2. Redistributions in binary form must reproduce the above copyright
1584d9c625SLionel Sambuc  *    notice, this list of conditions and the following disclaimer in the
1684d9c625SLionel Sambuc  *    documentation and/or other materials provided with the distribution.
1784d9c625SLionel Sambuc  * 3. All advertising materials mentioning features or use of this software
1884d9c625SLionel Sambuc  *    must display the following acknowledgement:
1984d9c625SLionel Sambuc  *	This product includes software developed by TooLs GmbH.
2084d9c625SLionel Sambuc  * 4. The name of TooLs GmbH may not be used to endorse or promote products
2184d9c625SLionel Sambuc  *    derived from this software without specific prior written permission.
2284d9c625SLionel Sambuc  *
2384d9c625SLionel Sambuc  * THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``AS IS'' AND ANY EXPRESS OR
2484d9c625SLionel Sambuc  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
2584d9c625SLionel Sambuc  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
2684d9c625SLionel Sambuc  * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
2784d9c625SLionel Sambuc  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
2884d9c625SLionel Sambuc  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
2984d9c625SLionel Sambuc  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
3084d9c625SLionel Sambuc  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
3184d9c625SLionel Sambuc  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
3284d9c625SLionel Sambuc  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
3384d9c625SLionel Sambuc  */
3484d9c625SLionel Sambuc /*
3584d9c625SLionel Sambuc  * Written by Paul Popelka (paulp@uts.amdahl.com)
3684d9c625SLionel Sambuc  *
3784d9c625SLionel Sambuc  * You can do anything you want with this software, just don't say you wrote
3884d9c625SLionel Sambuc  * it, and don't remove this notice.
3984d9c625SLionel Sambuc  *
4084d9c625SLionel Sambuc  * This software is provided "as is".
4184d9c625SLionel Sambuc  *
4284d9c625SLionel Sambuc  * The author supplies this software to be publicly redistributed on the
4384d9c625SLionel Sambuc  * understanding that the author is not responsible for the correct
4484d9c625SLionel Sambuc  * functioning of this software in any circumstances and is not liable for
4584d9c625SLionel Sambuc  * any damages caused by this software.
4684d9c625SLionel Sambuc  *
4784d9c625SLionel Sambuc  * October 1992
4884d9c625SLionel Sambuc  */
4984d9c625SLionel Sambuc 
5084d9c625SLionel Sambuc #if HAVE_NBTOOL_CONFIG_H
5184d9c625SLionel Sambuc #include "nbtool_config.h"
5284d9c625SLionel Sambuc #endif
5384d9c625SLionel Sambuc 
5484d9c625SLionel Sambuc #include <sys/cdefs.h>
55*0a6a1f1dSLionel Sambuc __KERNEL_RCSID(0, "$NetBSD: msdosfs_fat.c,v 1.29 2015/03/28 19:24:05 maxv Exp $");
5684d9c625SLionel Sambuc 
5784d9c625SLionel Sambuc /*
5884d9c625SLionel Sambuc  * kernel include files.
5984d9c625SLionel Sambuc  */
6084d9c625SLionel Sambuc #include <sys/param.h>
6184d9c625SLionel Sambuc #include <sys/file.h>
6284d9c625SLionel Sambuc #ifdef _KERNEL
6384d9c625SLionel Sambuc #include <sys/mount.h>		/* to define statvfs structure */
6484d9c625SLionel Sambuc #include <sys/errno.h>
6584d9c625SLionel Sambuc #include <sys/systm.h>
6684d9c625SLionel Sambuc #include <sys/kauth.h>
6784d9c625SLionel Sambuc #include <sys/dirent.h>
6884d9c625SLionel Sambuc #include <sys/namei.h>
6984d9c625SLionel Sambuc #include <sys/buf.h>
7084d9c625SLionel Sambuc #include <sys/vnode.h>		/* to define vattr structure */
7184d9c625SLionel Sambuc #else
7284d9c625SLionel Sambuc #include <ffs/buf.h>
7384d9c625SLionel Sambuc #endif
7484d9c625SLionel Sambuc 
7584d9c625SLionel Sambuc /*
7684d9c625SLionel Sambuc  * msdosfs include files.
7784d9c625SLionel Sambuc  */
7884d9c625SLionel Sambuc #include <fs/msdosfs/bpb.h>
7984d9c625SLionel Sambuc #include <fs/msdosfs/msdosfsmount.h>
8084d9c625SLionel Sambuc #include <fs/msdosfs/direntry.h>
8184d9c625SLionel Sambuc #include <fs/msdosfs/denode.h>
8284d9c625SLionel Sambuc #include <fs/msdosfs/fat.h>
8384d9c625SLionel Sambuc 
8484d9c625SLionel Sambuc /*
8584d9c625SLionel Sambuc  * Fat cache stats.
8684d9c625SLionel Sambuc  */
8784d9c625SLionel Sambuc int fc_fileextends;		/* # of file extends			 */
8884d9c625SLionel Sambuc int fc_lfcempty;		/* # of time last file cluster cache entry
8984d9c625SLionel Sambuc 				 * was empty */
9084d9c625SLionel Sambuc int fc_bmapcalls;		/* # of times pcbmap was called		 */
9184d9c625SLionel Sambuc 
9284d9c625SLionel Sambuc #define	LMMAX	20
9384d9c625SLionel Sambuc int fc_lmdistance[LMMAX];	/* counters for how far off the last
9484d9c625SLionel Sambuc 				 * cluster mapped entry was. */
9584d9c625SLionel Sambuc int fc_largedistance;		/* off by more than LMMAX		 */
9684d9c625SLionel Sambuc int fc_wherefrom, fc_whereto, fc_lastclust;
9784d9c625SLionel Sambuc int pm_fatblocksize;
9884d9c625SLionel Sambuc 
9984d9c625SLionel Sambuc #ifdef MSDOSFS_DEBUG
10084d9c625SLionel Sambuc #define DPRINTF(a) printf a
10184d9c625SLionel Sambuc #else
10284d9c625SLionel Sambuc #define DPRINTF(a)
10384d9c625SLionel Sambuc #endif
10484d9c625SLionel Sambuc #ifdef MSDOSFS_DEBUG
10584d9c625SLionel Sambuc void print_fat_stats(void);
10684d9c625SLionel Sambuc 
10784d9c625SLionel Sambuc void
print_fat_stats(void)10884d9c625SLionel Sambuc print_fat_stats(void)
10984d9c625SLionel Sambuc {
11084d9c625SLionel Sambuc 	int i;
11184d9c625SLionel Sambuc 
11284d9c625SLionel Sambuc 	printf("fc_fileextends=%d fc_lfcempty=%d fc_bmapcalls=%d "
11384d9c625SLionel Sambuc 	    "fc_largedistance=%d [%d->%d=%d] fc_lastclust=%d pm_fatblocksize=%d\n",
11484d9c625SLionel Sambuc 	    fc_fileextends, fc_lfcempty, fc_bmapcalls, fc_largedistance,
11584d9c625SLionel Sambuc 	    fc_wherefrom, fc_whereto, fc_whereto-fc_wherefrom,
11684d9c625SLionel Sambuc 	    fc_lastclust, pm_fatblocksize);
11784d9c625SLionel Sambuc 
11884d9c625SLionel Sambuc 	fc_fileextends = fc_lfcempty = fc_bmapcalls = 0;
11984d9c625SLionel Sambuc 	fc_wherefrom = fc_whereto = fc_lastclust = 0;
12084d9c625SLionel Sambuc 
12184d9c625SLionel Sambuc 	for (i = 0; i < LMMAX; i++) {
12284d9c625SLionel Sambuc 		printf("%d:%d ", i, fc_lmdistance[i]);
12384d9c625SLionel Sambuc 	fc_lmdistance[i] = 0;
12484d9c625SLionel Sambuc 	}
12584d9c625SLionel Sambuc 
12684d9c625SLionel Sambuc 	printf("\n");
12784d9c625SLionel Sambuc }
12884d9c625SLionel Sambuc #endif
12984d9c625SLionel Sambuc 
13084d9c625SLionel Sambuc static void fatblock(struct msdosfsmount *, u_long, u_long *, u_long *,
13184d9c625SLionel Sambuc 			  u_long *);
13284d9c625SLionel Sambuc void updatefats(struct msdosfsmount *, struct buf *, u_long);
13384d9c625SLionel Sambuc static inline void usemap_free(struct msdosfsmount *, u_long);
13484d9c625SLionel Sambuc static inline void usemap_alloc(struct msdosfsmount *, u_long);
13584d9c625SLionel Sambuc static int fatchain(struct msdosfsmount *, u_long, u_long, u_long);
13684d9c625SLionel Sambuc int chainlength(struct msdosfsmount *, u_long, u_long);
13784d9c625SLionel Sambuc int chainalloc(struct msdosfsmount *, u_long, u_long, u_long, u_long *,
13884d9c625SLionel Sambuc 		    u_long *);
13984d9c625SLionel Sambuc 
14084d9c625SLionel Sambuc static void
fatblock(struct msdosfsmount * pmp,u_long ofs,u_long * bnp,u_long * sizep,u_long * bop)14184d9c625SLionel Sambuc fatblock(struct msdosfsmount *pmp, u_long ofs, u_long *bnp, u_long *sizep, u_long *bop)
14284d9c625SLionel Sambuc {
14384d9c625SLionel Sambuc 	u_long bn, size;
14484d9c625SLionel Sambuc 
14584d9c625SLionel Sambuc 	bn = ofs / pmp->pm_fatblocksize * pmp->pm_fatblocksec;
14684d9c625SLionel Sambuc 	size = min(pmp->pm_fatblocksec, pmp->pm_FATsecs - bn)
14784d9c625SLionel Sambuc 	    * pmp->pm_BytesPerSec;
14884d9c625SLionel Sambuc 	bn += pmp->pm_fatblk + pmp->pm_curfat * pmp->pm_FATsecs;
14984d9c625SLionel Sambuc 
15084d9c625SLionel Sambuc 	DPRINTF(("%s(ofs=%lu bn=%lu, size=%lu, bo=%lu)\n", __func__, ofs, bn,
15184d9c625SLionel Sambuc 	    size, ofs % pmp->pm_fatblocksize));
15284d9c625SLionel Sambuc 	if (bnp)
15384d9c625SLionel Sambuc 		*bnp = bn;
15484d9c625SLionel Sambuc 	if (sizep)
15584d9c625SLionel Sambuc 		*sizep = size;
15684d9c625SLionel Sambuc 	if (bop)
15784d9c625SLionel Sambuc 		*bop = ofs % pmp->pm_fatblocksize;
15884d9c625SLionel Sambuc 
15984d9c625SLionel Sambuc 	pm_fatblocksize = pmp->pm_fatblocksize;
16084d9c625SLionel Sambuc }
16184d9c625SLionel Sambuc 
16284d9c625SLionel Sambuc /*
16384d9c625SLionel Sambuc  * Map the logical cluster number of a file into a physical disk sector
16484d9c625SLionel Sambuc  * that is filesystem relative.
16584d9c625SLionel Sambuc  *
16684d9c625SLionel Sambuc  * dep	  - address of denode representing the file of interest
16784d9c625SLionel Sambuc  * findcn - file relative cluster whose filesystem relative cluster number
16884d9c625SLionel Sambuc  *	    and/or block number are/is to be found
16984d9c625SLionel Sambuc  * bnp	  - address of where to place the file system relative block number.
17084d9c625SLionel Sambuc  *	    If this pointer is null then don't return this quantity.
17184d9c625SLionel Sambuc  * cnp	  - address of where to place the file system relative cluster number.
17284d9c625SLionel Sambuc  *	    If this pointer is null then don't return this quantity.
17384d9c625SLionel Sambuc  *
17484d9c625SLionel Sambuc  * NOTE: Either bnp or cnp must be non-null.
17584d9c625SLionel Sambuc  * This function has one side effect.  If the requested file relative cluster
17684d9c625SLionel Sambuc  * is beyond the end of file, then the actual number of clusters in the file
17784d9c625SLionel Sambuc  * is returned in *cnp.  This is useful for determining how long a directory is.
17884d9c625SLionel Sambuc  *  If cnp is null, nothing is returned.
17984d9c625SLionel Sambuc  */
18084d9c625SLionel Sambuc int
pcbmap(struct denode * dep,u_long findcn,daddr_t * bnp,u_long * cnp,int * sp)18184d9c625SLionel Sambuc pcbmap(struct denode *dep, u_long findcn, daddr_t *bnp, u_long *cnp, int *sp)
18284d9c625SLionel Sambuc 	/* findcn:		 file relative cluster to get		 */
18384d9c625SLionel Sambuc 	/* bnp:		 returned filesys rel sector number	 */
18484d9c625SLionel Sambuc 	/* cnp:		 returned cluster number		 */
18584d9c625SLionel Sambuc 	/* sp:		 returned block size			 */
18684d9c625SLionel Sambuc {
18784d9c625SLionel Sambuc 	int error;
18884d9c625SLionel Sambuc 	u_long i;
18984d9c625SLionel Sambuc 	u_long cn;
19084d9c625SLionel Sambuc 	u_long prevcn = 0; /* XXX: prevcn could be used unititialized */
19184d9c625SLionel Sambuc 	u_long byteoffset;
19284d9c625SLionel Sambuc 	u_long bn;
19384d9c625SLionel Sambuc 	u_long bo;
19484d9c625SLionel Sambuc 	struct buf *bp = NULL;
19584d9c625SLionel Sambuc 	u_long bp_bn = -1;
19684d9c625SLionel Sambuc 	struct msdosfsmount *pmp = dep->de_pmp;
19784d9c625SLionel Sambuc 	u_long bsize;
19884d9c625SLionel Sambuc 
19984d9c625SLionel Sambuc 	fc_bmapcalls++;
20084d9c625SLionel Sambuc 
20184d9c625SLionel Sambuc 	/*
20284d9c625SLionel Sambuc 	 * If they don't give us someplace to return a value then don't
20384d9c625SLionel Sambuc 	 * bother doing anything.
20484d9c625SLionel Sambuc 	 */
20584d9c625SLionel Sambuc 	if (bnp == NULL && cnp == NULL && sp == NULL)
20684d9c625SLionel Sambuc 		return (0);
20784d9c625SLionel Sambuc 
20884d9c625SLionel Sambuc 	cn = dep->de_StartCluster;
20984d9c625SLionel Sambuc 	DPRINTF(("%s(start cluster=%lu)\n", __func__, cn));
21084d9c625SLionel Sambuc 	/*
21184d9c625SLionel Sambuc 	 * The "file" that makes up the root directory is contiguous,
21284d9c625SLionel Sambuc 	 * permanently allocated, of fixed size, and is not made up of
21384d9c625SLionel Sambuc 	 * clusters.  If the cluster number is beyond the end of the root
21484d9c625SLionel Sambuc 	 * directory, then return the number of clusters in the file.
21584d9c625SLionel Sambuc 	 */
21684d9c625SLionel Sambuc 	if (cn == MSDOSFSROOT) {
21784d9c625SLionel Sambuc 		if (dep->de_Attributes & ATTR_DIRECTORY) {
21884d9c625SLionel Sambuc 			if (de_cn2off(pmp, findcn) >= dep->de_FileSize) {
21984d9c625SLionel Sambuc 				if (cnp)
22084d9c625SLionel Sambuc 					*cnp = de_bn2cn(pmp, pmp->pm_rootdirsize);
22184d9c625SLionel Sambuc 				DPRINTF(("%s(root, %lu ETOOBIG)\n", __func__,
22284d9c625SLionel Sambuc 				    de_cn2off(pmp, findcn)));
22384d9c625SLionel Sambuc 				return (E2BIG);
22484d9c625SLionel Sambuc 			}
22584d9c625SLionel Sambuc 			if (bnp)
22684d9c625SLionel Sambuc 				*bnp = pmp->pm_rootdirblk + de_cn2bn(pmp, findcn);
22784d9c625SLionel Sambuc 			if (cnp)
22884d9c625SLionel Sambuc 				*cnp = MSDOSFSROOT;
22984d9c625SLionel Sambuc 			if (sp)
23084d9c625SLionel Sambuc 				*sp = min(pmp->pm_bpcluster,
23184d9c625SLionel Sambuc 				    dep->de_FileSize - de_cn2off(pmp, findcn));
23284d9c625SLionel Sambuc 			DPRINTF(("%s(root, bn=%lu, cn=%u)\n", __func__,
23384d9c625SLionel Sambuc 			    pmp->pm_rootdirblk + de_cn2bn(pmp, findcn),
23484d9c625SLionel Sambuc 			    MSDOSFSROOT));
23584d9c625SLionel Sambuc 			return (0);
23684d9c625SLionel Sambuc 		} else {		/* just an empty file */
23784d9c625SLionel Sambuc 			if (cnp)
23884d9c625SLionel Sambuc 				*cnp = 0;
23984d9c625SLionel Sambuc 			DPRINTF(("%s(root, empty ETOOBIG)\n", __func__));
24084d9c625SLionel Sambuc 			return (E2BIG);
24184d9c625SLionel Sambuc 		}
24284d9c625SLionel Sambuc 	}
24384d9c625SLionel Sambuc 
24484d9c625SLionel Sambuc 	/*
24584d9c625SLionel Sambuc 	 * All other files do I/O in cluster sized blocks
24684d9c625SLionel Sambuc 	 */
24784d9c625SLionel Sambuc 	if (sp)
24884d9c625SLionel Sambuc 		*sp = pmp->pm_bpcluster;
24984d9c625SLionel Sambuc 
25084d9c625SLionel Sambuc 	/*
25184d9c625SLionel Sambuc 	 * Rummage around in the FAT cache, maybe we can avoid tromping
25284d9c625SLionel Sambuc 	 * thru every FAT entry for the file. And, keep track of how far
25384d9c625SLionel Sambuc 	 * off the cache was from where we wanted to be.
25484d9c625SLionel Sambuc 	 */
25584d9c625SLionel Sambuc 	i = 0;
25684d9c625SLionel Sambuc 	fc_lookup(dep, findcn, &i, &cn);
25784d9c625SLionel Sambuc 	DPRINTF(("%s(bpcluster=%lu i=%lu cn=%lu\n", __func__, pmp->pm_bpcluster,
25884d9c625SLionel Sambuc 	    i, cn));
25984d9c625SLionel Sambuc 	if ((bn = findcn - i) >= LMMAX) {
26084d9c625SLionel Sambuc 		fc_largedistance++;
26184d9c625SLionel Sambuc 		fc_wherefrom = i;
26284d9c625SLionel Sambuc 		fc_whereto = findcn;
26384d9c625SLionel Sambuc 		fc_lastclust = dep->de_fc[FC_LASTFC].fc_frcn;
26484d9c625SLionel Sambuc 	} else
26584d9c625SLionel Sambuc 		fc_lmdistance[bn]++;
26684d9c625SLionel Sambuc 
26784d9c625SLionel Sambuc 	/*
26884d9c625SLionel Sambuc 	 * Handle all other files or directories the normal way.
26984d9c625SLionel Sambuc 	 */
27084d9c625SLionel Sambuc 	for (; i < findcn; i++) {
27184d9c625SLionel Sambuc 		/*
27284d9c625SLionel Sambuc 		 * Stop with all reserved clusters, not just with EOF.
27384d9c625SLionel Sambuc 		 */
27484d9c625SLionel Sambuc 		if (cn >= (CLUST_RSRVD & pmp->pm_fatmask))
27584d9c625SLionel Sambuc 			goto hiteof;
27684d9c625SLionel Sambuc 		byteoffset = FATOFS(pmp, cn);
27784d9c625SLionel Sambuc 		fatblock(pmp, byteoffset, &bn, &bsize, &bo);
27884d9c625SLionel Sambuc 		if (bn != bp_bn) {
27984d9c625SLionel Sambuc 			if (bp)
28084d9c625SLionel Sambuc 				brelse(bp, 0);
28184d9c625SLionel Sambuc 			error = bread(pmp->pm_devvp, de_bn2kb(pmp, bn), bsize,
282*0a6a1f1dSLionel Sambuc 			    0, &bp);
28384d9c625SLionel Sambuc 			if (error) {
28484d9c625SLionel Sambuc 				DPRINTF(("%s(bread, %d)\n", __func__, error));
28584d9c625SLionel Sambuc 				return (error);
28684d9c625SLionel Sambuc 			}
28784d9c625SLionel Sambuc 			bp_bn = bn;
28884d9c625SLionel Sambuc 		}
28984d9c625SLionel Sambuc 		prevcn = cn;
29084d9c625SLionel Sambuc 		if (bo >= bsize) {
29184d9c625SLionel Sambuc 			if (bp)
29284d9c625SLionel Sambuc 				brelse(bp, 0);
29384d9c625SLionel Sambuc 			DPRINTF(("%s(block, %lu >= %lu)\n", __func__, bo,
29484d9c625SLionel Sambuc 			    bsize));
29584d9c625SLionel Sambuc 			return (EIO);
29684d9c625SLionel Sambuc 		}
29784d9c625SLionel Sambuc 		KASSERT(bp != NULL);
29884d9c625SLionel Sambuc 		if (FAT32(pmp))
29984d9c625SLionel Sambuc 			cn = getulong((char *)bp->b_data + bo);
30084d9c625SLionel Sambuc 		else
30184d9c625SLionel Sambuc 			cn = getushort((char *)bp->b_data + bo);
30284d9c625SLionel Sambuc 		if (FAT12(pmp) && (prevcn & 1))
30384d9c625SLionel Sambuc 			cn >>= 4;
30484d9c625SLionel Sambuc 		DPRINTF(("%s(cn=%lu masked=%lu)\n", __func__, cn,
30584d9c625SLionel Sambuc 		    cn & pmp->pm_fatmask));
30684d9c625SLionel Sambuc 		cn &= pmp->pm_fatmask;
30784d9c625SLionel Sambuc 	}
30884d9c625SLionel Sambuc 
30984d9c625SLionel Sambuc 	if (!MSDOSFSEOF(cn, pmp->pm_fatmask)) {
31084d9c625SLionel Sambuc 		if (bp)
31184d9c625SLionel Sambuc 			brelse(bp, 0);
31284d9c625SLionel Sambuc 		if (bnp)
31384d9c625SLionel Sambuc 			*bnp = cntobn(pmp, cn);
31484d9c625SLionel Sambuc 		if (cnp)
31584d9c625SLionel Sambuc 			*cnp = cn;
31684d9c625SLionel Sambuc 		DPRINTF(("%s(bn=%lu, cn=%lu)\n", __func__, cntobn(pmp, cn),
31784d9c625SLionel Sambuc 		    cn));
31884d9c625SLionel Sambuc 		fc_setcache(dep, FC_LASTMAP, i, cn);
31984d9c625SLionel Sambuc 		return (0);
32084d9c625SLionel Sambuc 	}
32184d9c625SLionel Sambuc 
32284d9c625SLionel Sambuc hiteof:;
32384d9c625SLionel Sambuc 	if (cnp)
32484d9c625SLionel Sambuc 		*cnp = i;
32584d9c625SLionel Sambuc 	if (bp)
32684d9c625SLionel Sambuc 		brelse(bp, 0);
32784d9c625SLionel Sambuc 	/* update last file cluster entry in the FAT cache */
32884d9c625SLionel Sambuc 	fc_setcache(dep, FC_LASTFC, i - 1, prevcn);
32984d9c625SLionel Sambuc 	DPRINTF(("%s(eof, %lu)\n", __func__, i));
33084d9c625SLionel Sambuc 	return (E2BIG);
33184d9c625SLionel Sambuc }
33284d9c625SLionel Sambuc 
33384d9c625SLionel Sambuc /*
33484d9c625SLionel Sambuc  * Find the closest entry in the FAT cache to the cluster we are looking
33584d9c625SLionel Sambuc  * for.
33684d9c625SLionel Sambuc  */
33784d9c625SLionel Sambuc void
fc_lookup(struct denode * dep,u_long findcn,u_long * frcnp,u_long * fsrcnp)33884d9c625SLionel Sambuc fc_lookup(struct denode *dep, u_long findcn, u_long *frcnp, u_long *fsrcnp)
33984d9c625SLionel Sambuc {
34084d9c625SLionel Sambuc 	int i;
34184d9c625SLionel Sambuc 	u_long cn;
34284d9c625SLionel Sambuc 	struct fatcache *closest = 0;
34384d9c625SLionel Sambuc 
34484d9c625SLionel Sambuc 	for (i = 0; i < FC_SIZE; i++) {
34584d9c625SLionel Sambuc 		cn = dep->de_fc[i].fc_frcn;
34684d9c625SLionel Sambuc 		if (cn != FCE_EMPTY && cn <= findcn) {
34784d9c625SLionel Sambuc 			if (closest == 0 || cn > closest->fc_frcn)
34884d9c625SLionel Sambuc 				closest = &dep->de_fc[i];
34984d9c625SLionel Sambuc 		}
35084d9c625SLionel Sambuc 	}
35184d9c625SLionel Sambuc 	if (closest) {
35284d9c625SLionel Sambuc 		*frcnp = closest->fc_frcn;
35384d9c625SLionel Sambuc 		*fsrcnp = closest->fc_fsrcn;
35484d9c625SLionel Sambuc 	}
35584d9c625SLionel Sambuc }
35684d9c625SLionel Sambuc 
35784d9c625SLionel Sambuc /*
35884d9c625SLionel Sambuc  * Purge the FAT cache in denode dep of all entries relating to file
35984d9c625SLionel Sambuc  * relative cluster frcn and beyond.
36084d9c625SLionel Sambuc  */
36184d9c625SLionel Sambuc void
fc_purge(struct denode * dep,u_int frcn)36284d9c625SLionel Sambuc fc_purge(struct denode *dep, u_int frcn)
36384d9c625SLionel Sambuc {
36484d9c625SLionel Sambuc 	int i;
36584d9c625SLionel Sambuc 	struct fatcache *fcp;
36684d9c625SLionel Sambuc 
36784d9c625SLionel Sambuc 	fcp = dep->de_fc;
36884d9c625SLionel Sambuc 	for (i = 0; i < FC_SIZE; i++, fcp++) {
36984d9c625SLionel Sambuc 		if (fcp->fc_frcn >= frcn)
37084d9c625SLionel Sambuc 			fcp->fc_frcn = FCE_EMPTY;
37184d9c625SLionel Sambuc 	}
37284d9c625SLionel Sambuc }
37384d9c625SLionel Sambuc 
37484d9c625SLionel Sambuc /*
37584d9c625SLionel Sambuc  * Update the FAT.
37684d9c625SLionel Sambuc  * If mirroring the FAT, update all copies, with the first copy as last.
37784d9c625SLionel Sambuc  * Else update only the current FAT (ignoring the others).
37884d9c625SLionel Sambuc  *
37984d9c625SLionel Sambuc  * pmp	 - msdosfsmount structure for filesystem to update
38084d9c625SLionel Sambuc  * bp	 - addr of modified FAT block
38184d9c625SLionel Sambuc  * fatbn - block number relative to begin of filesystem of the modified FAT block.
38284d9c625SLionel Sambuc  */
38384d9c625SLionel Sambuc void
updatefats(struct msdosfsmount * pmp,struct buf * bp,u_long fatbn)38484d9c625SLionel Sambuc updatefats(struct msdosfsmount *pmp, struct buf *bp, u_long fatbn)
38584d9c625SLionel Sambuc {
38684d9c625SLionel Sambuc 	int i;
38784d9c625SLionel Sambuc 	struct buf *bpn;
38884d9c625SLionel Sambuc 
38984d9c625SLionel Sambuc 	DPRINTF(("%s(pmp %p, bp %p, fatbn %lu)\n", __func__, pmp, bp, fatbn));
39084d9c625SLionel Sambuc 
39184d9c625SLionel Sambuc 	/*
39284d9c625SLionel Sambuc 	 * If we have an FSInfo block, update it.
39384d9c625SLionel Sambuc 	 */
39484d9c625SLionel Sambuc 	if (pmp->pm_fsinfo) {
39584d9c625SLionel Sambuc 		u_long cn = pmp->pm_nxtfree;
39684d9c625SLionel Sambuc 
39784d9c625SLionel Sambuc 		if (pmp->pm_freeclustercount
39884d9c625SLionel Sambuc 		    && (pmp->pm_inusemap[cn / N_INUSEBITS]
39984d9c625SLionel Sambuc 			& (1 << (cn % N_INUSEBITS)))) {
40084d9c625SLionel Sambuc 			/*
40184d9c625SLionel Sambuc 			 * The cluster indicated in FSInfo isn't free
40284d9c625SLionel Sambuc 			 * any longer.  Got get a new free one.
40384d9c625SLionel Sambuc 			 */
40484d9c625SLionel Sambuc 			for (cn = 0; cn < pmp->pm_maxcluster; cn++)
40584d9c625SLionel Sambuc 				if (pmp->pm_inusemap[cn / N_INUSEBITS] != (u_int)-1)
40684d9c625SLionel Sambuc 					break;
40784d9c625SLionel Sambuc 			pmp->pm_nxtfree = cn
40884d9c625SLionel Sambuc 				+ ffs(pmp->pm_inusemap[cn / N_INUSEBITS]
40984d9c625SLionel Sambuc 				      ^ (u_int)-1) - 1;
41084d9c625SLionel Sambuc 		}
41184d9c625SLionel Sambuc 		/*
41284d9c625SLionel Sambuc 		 * XXX  If the fsinfo block is stored on media with
41384d9c625SLionel Sambuc 		 *      2KB or larger sectors, is the fsinfo structure
41484d9c625SLionel Sambuc 		 *      padded at the end or in the middle?
41584d9c625SLionel Sambuc 		 */
41684d9c625SLionel Sambuc 		if (bread(pmp->pm_devvp, de_bn2kb(pmp, pmp->pm_fsinfo),
417*0a6a1f1dSLionel Sambuc 		    pmp->pm_BytesPerSec, B_MODIFY, &bpn) != 0) {
41884d9c625SLionel Sambuc 			/*
41984d9c625SLionel Sambuc 			 * Ignore the error, but turn off FSInfo update for the future.
42084d9c625SLionel Sambuc 			 */
42184d9c625SLionel Sambuc 			pmp->pm_fsinfo = 0;
42284d9c625SLionel Sambuc 		} else {
42384d9c625SLionel Sambuc 			struct fsinfo *fp = (struct fsinfo *)bpn->b_data;
42484d9c625SLionel Sambuc 
42584d9c625SLionel Sambuc 			putulong(fp->fsinfree, pmp->pm_freeclustercount);
42684d9c625SLionel Sambuc 			putulong(fp->fsinxtfree, pmp->pm_nxtfree);
42784d9c625SLionel Sambuc 			if (pmp->pm_flags & MSDOSFSMNT_WAITONFAT)
42884d9c625SLionel Sambuc 				bwrite(bpn);
42984d9c625SLionel Sambuc 			else
43084d9c625SLionel Sambuc 				bdwrite(bpn);
43184d9c625SLionel Sambuc 		}
43284d9c625SLionel Sambuc 	}
43384d9c625SLionel Sambuc 
43484d9c625SLionel Sambuc 	if (pmp->pm_flags & MSDOSFS_FATMIRROR) {
43584d9c625SLionel Sambuc 		/*
43684d9c625SLionel Sambuc 		 * Now copy the block(s) of the modified FAT to the other copies of
43784d9c625SLionel Sambuc 		 * the FAT and write them out.  This is faster than reading in the
43884d9c625SLionel Sambuc 		 * other FATs and then writing them back out.  This could tie up
43984d9c625SLionel Sambuc 		 * the FAT for quite a while. Preventing others from accessing it.
44084d9c625SLionel Sambuc 		 * To prevent us from going after the FAT quite so much we use
44184d9c625SLionel Sambuc 		 * delayed writes, unless they specified "synchronous" when the
44284d9c625SLionel Sambuc 		 * filesystem was mounted.  If synch is asked for then use
44384d9c625SLionel Sambuc 		 * bwrite()'s and really slow things down.
44484d9c625SLionel Sambuc 		 */
44584d9c625SLionel Sambuc 		for (i = 1; i < pmp->pm_FATs; i++) {
44684d9c625SLionel Sambuc 			fatbn += pmp->pm_FATsecs;
44784d9c625SLionel Sambuc 			/* getblk() never fails */
44884d9c625SLionel Sambuc 			bpn = getblk(pmp->pm_devvp, de_bn2kb(pmp, fatbn),
44984d9c625SLionel Sambuc 			    bp->b_bcount, 0, 0);
45084d9c625SLionel Sambuc 			memcpy(bpn->b_data, bp->b_data, bp->b_bcount);
45184d9c625SLionel Sambuc 			if (pmp->pm_flags & MSDOSFSMNT_WAITONFAT)
45284d9c625SLionel Sambuc 				bwrite(bpn);
45384d9c625SLionel Sambuc 			else
45484d9c625SLionel Sambuc 				bdwrite(bpn);
45584d9c625SLionel Sambuc 		}
45684d9c625SLionel Sambuc 	}
45784d9c625SLionel Sambuc 
45884d9c625SLionel Sambuc 	/*
45984d9c625SLionel Sambuc 	 * Write out the first (or current) FAT last.
46084d9c625SLionel Sambuc 	 */
46184d9c625SLionel Sambuc 	if (pmp->pm_flags & MSDOSFSMNT_WAITONFAT)
46284d9c625SLionel Sambuc 		bwrite(bp);
46384d9c625SLionel Sambuc 	else
46484d9c625SLionel Sambuc 		bdwrite(bp);
46584d9c625SLionel Sambuc 	/*
46684d9c625SLionel Sambuc 	 * Maybe update fsinfo sector here?
46784d9c625SLionel Sambuc 	 */
46884d9c625SLionel Sambuc }
46984d9c625SLionel Sambuc 
47084d9c625SLionel Sambuc /*
47184d9c625SLionel Sambuc  * Updating entries in 12 bit FATs is a pain in the butt.
47284d9c625SLionel Sambuc  *
47384d9c625SLionel Sambuc  * The following picture shows where nibbles go when moving from a 12 bit
47484d9c625SLionel Sambuc  * cluster number into the appropriate bytes in the FAT.
47584d9c625SLionel Sambuc  *
47684d9c625SLionel Sambuc  *	byte m        byte m+1      byte m+2
47784d9c625SLionel Sambuc  *	+----+----+   +----+----+   +----+----+
47884d9c625SLionel Sambuc  *	|  0    1 |   |  2    3 |   |  4    5 |   FAT bytes
47984d9c625SLionel Sambuc  *	+----+----+   +----+----+   +----+----+
48084d9c625SLionel Sambuc  *
48184d9c625SLionel Sambuc  *	+----+----+----+   +----+----+----+
48284d9c625SLionel Sambuc  *	|  3    0    1 |   |  4    5    2 |
48384d9c625SLionel Sambuc  *	+----+----+----+   +----+----+----+
48484d9c625SLionel Sambuc  *	cluster n  	   cluster n+1
48584d9c625SLionel Sambuc  *
48684d9c625SLionel Sambuc  * Where n is even. m = n + (n >> 2)
48784d9c625SLionel Sambuc  *
48884d9c625SLionel Sambuc  */
48984d9c625SLionel Sambuc static inline void
usemap_alloc(struct msdosfsmount * pmp,u_long cn)49084d9c625SLionel Sambuc usemap_alloc(struct msdosfsmount *pmp, u_long cn)
49184d9c625SLionel Sambuc {
49284d9c625SLionel Sambuc 
49384d9c625SLionel Sambuc 	pmp->pm_inusemap[cn / N_INUSEBITS] |= 1 << (cn % N_INUSEBITS);
49484d9c625SLionel Sambuc 	pmp->pm_freeclustercount--;
49584d9c625SLionel Sambuc }
49684d9c625SLionel Sambuc 
49784d9c625SLionel Sambuc static inline void
usemap_free(struct msdosfsmount * pmp,u_long cn)49884d9c625SLionel Sambuc usemap_free(struct msdosfsmount *pmp, u_long cn)
49984d9c625SLionel Sambuc {
50084d9c625SLionel Sambuc 
50184d9c625SLionel Sambuc 	pmp->pm_freeclustercount++;
50284d9c625SLionel Sambuc 	pmp->pm_inusemap[cn / N_INUSEBITS] &= ~(1 << (cn % N_INUSEBITS));
50384d9c625SLionel Sambuc }
50484d9c625SLionel Sambuc 
50584d9c625SLionel Sambuc int
clusterfree(struct msdosfsmount * pmp,u_long cluster,u_long * oldcnp)50684d9c625SLionel Sambuc clusterfree(struct msdosfsmount *pmp, u_long cluster, u_long *oldcnp)
50784d9c625SLionel Sambuc {
50884d9c625SLionel Sambuc 	int error;
50984d9c625SLionel Sambuc 	u_long oldcn;
51084d9c625SLionel Sambuc 
51184d9c625SLionel Sambuc 	usemap_free(pmp, cluster);
51284d9c625SLionel Sambuc 	error = fatentry(FAT_GET_AND_SET, pmp, cluster, &oldcn, MSDOSFSFREE);
51384d9c625SLionel Sambuc 	if (error) {
51484d9c625SLionel Sambuc 		usemap_alloc(pmp, cluster);
51584d9c625SLionel Sambuc 		return (error);
51684d9c625SLionel Sambuc 	}
51784d9c625SLionel Sambuc 	/*
51884d9c625SLionel Sambuc 	 * If the cluster was successfully marked free, then update
51984d9c625SLionel Sambuc 	 * the count of free clusters, and turn off the "allocated"
52084d9c625SLionel Sambuc 	 * bit in the "in use" cluster bit map.
52184d9c625SLionel Sambuc 	 */
52284d9c625SLionel Sambuc 	if (oldcnp)
52384d9c625SLionel Sambuc 		*oldcnp = oldcn;
52484d9c625SLionel Sambuc 	return (0);
52584d9c625SLionel Sambuc }
52684d9c625SLionel Sambuc 
52784d9c625SLionel Sambuc /*
52884d9c625SLionel Sambuc  * Get or Set or 'Get and Set' the cluster'th entry in the FAT.
52984d9c625SLionel Sambuc  *
53084d9c625SLionel Sambuc  * function	- whether to get or set a fat entry
53184d9c625SLionel Sambuc  * pmp		- address of the msdosfsmount structure for the filesystem
53284d9c625SLionel Sambuc  *		  whose FAT is to be manipulated.
53384d9c625SLionel Sambuc  * cn		- which cluster is of interest
53484d9c625SLionel Sambuc  * oldcontents	- address of a word that is to receive the contents of the
53584d9c625SLionel Sambuc  *		  cluster'th entry if this is a get function
53684d9c625SLionel Sambuc  * newcontents	- the new value to be written into the cluster'th element of
53784d9c625SLionel Sambuc  *		  the FAT if this is a set function.
53884d9c625SLionel Sambuc  *
53984d9c625SLionel Sambuc  * This function can also be used to free a cluster by setting the FAT entry
54084d9c625SLionel Sambuc  * for a cluster to 0.
54184d9c625SLionel Sambuc  *
54284d9c625SLionel Sambuc  * All copies of the FAT are updated if this is a set function. NOTE: If
54384d9c625SLionel Sambuc  * fatentry() marks a cluster as free it does not update the inusemap in
54484d9c625SLionel Sambuc  * the msdosfsmount structure. This is left to the caller.
54584d9c625SLionel Sambuc  */
54684d9c625SLionel Sambuc int
fatentry(int function,struct msdosfsmount * pmp,u_long cn,u_long * oldcontents,u_long newcontents)54784d9c625SLionel Sambuc fatentry(int function, struct msdosfsmount *pmp, u_long cn, u_long *oldcontents, u_long newcontents)
54884d9c625SLionel Sambuc {
54984d9c625SLionel Sambuc 	int error;
55084d9c625SLionel Sambuc 	u_long readcn;
55184d9c625SLionel Sambuc 	u_long bn, bo, bsize, byteoffset;
55284d9c625SLionel Sambuc 	struct buf *bp;
55384d9c625SLionel Sambuc 
55484d9c625SLionel Sambuc 	DPRINTF(("%s(func %d, pmp %p, clust %lu, oldcon %p, newcon " "%lx)\n",
55584d9c625SLionel Sambuc 	    __func__, function, pmp, cn, oldcontents, newcontents));
55684d9c625SLionel Sambuc 
55784d9c625SLionel Sambuc #ifdef DIAGNOSTIC
55884d9c625SLionel Sambuc 	/*
55984d9c625SLionel Sambuc 	 * Be sure they asked us to do something.
56084d9c625SLionel Sambuc 	 */
56184d9c625SLionel Sambuc 	if ((function & (FAT_SET | FAT_GET)) == 0) {
56284d9c625SLionel Sambuc 		DPRINTF(("%s(): function code doesn't specify get or set\n",
56384d9c625SLionel Sambuc 		    __func__));
56484d9c625SLionel Sambuc 		return (EINVAL);
56584d9c625SLionel Sambuc 	}
56684d9c625SLionel Sambuc 
56784d9c625SLionel Sambuc 	/*
56884d9c625SLionel Sambuc 	 * If they asked us to return a cluster number but didn't tell us
56984d9c625SLionel Sambuc 	 * where to put it, give them an error.
57084d9c625SLionel Sambuc 	 */
57184d9c625SLionel Sambuc 	if ((function & FAT_GET) && oldcontents == NULL) {
57284d9c625SLionel Sambuc 		DPRINTF(("%s(): get function with no place to put result\n",
57384d9c625SLionel Sambuc 			__func__));
57484d9c625SLionel Sambuc 		return (EINVAL);
57584d9c625SLionel Sambuc 	}
57684d9c625SLionel Sambuc #endif
57784d9c625SLionel Sambuc 
57884d9c625SLionel Sambuc 	/*
57984d9c625SLionel Sambuc 	 * Be sure the requested cluster is in the filesystem.
58084d9c625SLionel Sambuc 	 */
58184d9c625SLionel Sambuc 	if (cn < CLUST_FIRST || cn > pmp->pm_maxcluster)
58284d9c625SLionel Sambuc 		return (EINVAL);
58384d9c625SLionel Sambuc 
58484d9c625SLionel Sambuc 	byteoffset = FATOFS(pmp, cn);
58584d9c625SLionel Sambuc 	fatblock(pmp, byteoffset, &bn, &bsize, &bo);
586*0a6a1f1dSLionel Sambuc 	if ((error = bread(pmp->pm_devvp, de_bn2kb(pmp, bn), bsize,
58784d9c625SLionel Sambuc 	    0, &bp)) != 0) {
58884d9c625SLionel Sambuc 		return (error);
58984d9c625SLionel Sambuc 	}
59084d9c625SLionel Sambuc 
59184d9c625SLionel Sambuc 	if (function & FAT_GET) {
59284d9c625SLionel Sambuc 		if (FAT32(pmp))
59384d9c625SLionel Sambuc 			readcn = getulong((char *)bp->b_data + bo);
59484d9c625SLionel Sambuc 		else
59584d9c625SLionel Sambuc 			readcn = getushort((char *)bp->b_data + bo);
59684d9c625SLionel Sambuc 		if (FAT12(pmp) & (cn & 1))
59784d9c625SLionel Sambuc 			readcn >>= 4;
59884d9c625SLionel Sambuc 		readcn &= pmp->pm_fatmask;
59984d9c625SLionel Sambuc 		*oldcontents = readcn;
60084d9c625SLionel Sambuc 	}
60184d9c625SLionel Sambuc 	if (function & FAT_SET) {
60284d9c625SLionel Sambuc 		switch (pmp->pm_fatmask) {
60384d9c625SLionel Sambuc 		case FAT12_MASK:
60484d9c625SLionel Sambuc 			readcn = getushort((char *)bp->b_data + bo);
60584d9c625SLionel Sambuc 			if (cn & 1) {
60684d9c625SLionel Sambuc 				readcn &= 0x000f;
60784d9c625SLionel Sambuc 				readcn |= newcontents << 4;
60884d9c625SLionel Sambuc 			} else {
60984d9c625SLionel Sambuc 				readcn &= 0xf000;
61084d9c625SLionel Sambuc 				readcn |= newcontents & 0xfff;
61184d9c625SLionel Sambuc 			}
61284d9c625SLionel Sambuc 			putushort((char *)bp->b_data + bo, readcn);
61384d9c625SLionel Sambuc 			break;
61484d9c625SLionel Sambuc 		case FAT16_MASK:
61584d9c625SLionel Sambuc 			putushort((char *)bp->b_data + bo, newcontents);
61684d9c625SLionel Sambuc 			break;
61784d9c625SLionel Sambuc 		case FAT32_MASK:
61884d9c625SLionel Sambuc 			/*
61984d9c625SLionel Sambuc 			 * According to spec we have to retain the
62084d9c625SLionel Sambuc 			 * high order bits of the FAT entry.
62184d9c625SLionel Sambuc 			 */
62284d9c625SLionel Sambuc 			readcn = getulong((char *)bp->b_data + bo);
62384d9c625SLionel Sambuc 			readcn &= ~FAT32_MASK;
62484d9c625SLionel Sambuc 			readcn |= newcontents & FAT32_MASK;
62584d9c625SLionel Sambuc 			putulong((char *)bp->b_data + bo, readcn);
62684d9c625SLionel Sambuc 			break;
62784d9c625SLionel Sambuc 		}
62884d9c625SLionel Sambuc 		updatefats(pmp, bp, bn);
62984d9c625SLionel Sambuc 		bp = NULL;
63084d9c625SLionel Sambuc 		pmp->pm_fmod = 1;
63184d9c625SLionel Sambuc 	}
63284d9c625SLionel Sambuc 	if (bp)
63384d9c625SLionel Sambuc 		brelse(bp, 0);
63484d9c625SLionel Sambuc 	return (0);
63584d9c625SLionel Sambuc }
63684d9c625SLionel Sambuc 
63784d9c625SLionel Sambuc /*
63884d9c625SLionel Sambuc  * Update a contiguous cluster chain
63984d9c625SLionel Sambuc  *
64084d9c625SLionel Sambuc  * pmp	    - mount point
64184d9c625SLionel Sambuc  * start    - first cluster of chain
64284d9c625SLionel Sambuc  * count    - number of clusters in chain
64384d9c625SLionel Sambuc  * fillwith - what to write into FAT entry of last cluster
64484d9c625SLionel Sambuc  */
64584d9c625SLionel Sambuc static int
fatchain(struct msdosfsmount * pmp,u_long start,u_long count,u_long fillwith)64684d9c625SLionel Sambuc fatchain(struct msdosfsmount *pmp, u_long start, u_long count, u_long fillwith)
64784d9c625SLionel Sambuc {
64884d9c625SLionel Sambuc 	int error;
64984d9c625SLionel Sambuc 	u_long bn, bo, bsize, byteoffset, readcn, newc;
65084d9c625SLionel Sambuc 	struct buf *bp;
65184d9c625SLionel Sambuc 
65284d9c625SLionel Sambuc 	DPRINTF(("%s(pmp %p, start %lu, count %lu, fillwith %lx)\n", __func__,
65384d9c625SLionel Sambuc 	    pmp, start, count, fillwith));
65484d9c625SLionel Sambuc 	/*
65584d9c625SLionel Sambuc 	 * Be sure the clusters are in the filesystem.
65684d9c625SLionel Sambuc 	 */
65784d9c625SLionel Sambuc 	if (start < CLUST_FIRST || start + count - 1 > pmp->pm_maxcluster)
65884d9c625SLionel Sambuc 		return (EINVAL);
65984d9c625SLionel Sambuc 
66084d9c625SLionel Sambuc 	while (count > 0) {
66184d9c625SLionel Sambuc 		byteoffset = FATOFS(pmp, start);
66284d9c625SLionel Sambuc 		fatblock(pmp, byteoffset, &bn, &bsize, &bo);
663*0a6a1f1dSLionel Sambuc 		error = bread(pmp->pm_devvp, de_bn2kb(pmp, bn), bsize,
66484d9c625SLionel Sambuc 		    B_MODIFY, &bp);
66584d9c625SLionel Sambuc 		if (error) {
66684d9c625SLionel Sambuc 			return (error);
66784d9c625SLionel Sambuc 		}
66884d9c625SLionel Sambuc 		while (count > 0) {
66984d9c625SLionel Sambuc 			start++;
67084d9c625SLionel Sambuc 			newc = --count > 0 ? start : fillwith;
67184d9c625SLionel Sambuc 			switch (pmp->pm_fatmask) {
67284d9c625SLionel Sambuc 			case FAT12_MASK:
67384d9c625SLionel Sambuc 				readcn = getushort((char *)bp->b_data + bo);
67484d9c625SLionel Sambuc 				if (start & 1) {
67584d9c625SLionel Sambuc 					readcn &= 0xf000;
67684d9c625SLionel Sambuc 					readcn |= newc & 0xfff;
67784d9c625SLionel Sambuc 				} else {
67884d9c625SLionel Sambuc 					readcn &= 0x000f;
67984d9c625SLionel Sambuc 					readcn |= newc << 4;
68084d9c625SLionel Sambuc 				}
68184d9c625SLionel Sambuc 				putushort((char *)bp->b_data + bo, readcn);
68284d9c625SLionel Sambuc 				bo++;
68384d9c625SLionel Sambuc 				if (!(start & 1))
68484d9c625SLionel Sambuc 					bo++;
68584d9c625SLionel Sambuc 				break;
68684d9c625SLionel Sambuc 			case FAT16_MASK:
68784d9c625SLionel Sambuc 				putushort((char *)bp->b_data + bo, newc);
68884d9c625SLionel Sambuc 				bo += 2;
68984d9c625SLionel Sambuc 				break;
69084d9c625SLionel Sambuc 			case FAT32_MASK:
69184d9c625SLionel Sambuc 				readcn = getulong((char *)bp->b_data + bo);
69284d9c625SLionel Sambuc 				readcn &= ~pmp->pm_fatmask;
69384d9c625SLionel Sambuc 				readcn |= newc & pmp->pm_fatmask;
69484d9c625SLionel Sambuc 				putulong((char *)bp->b_data + bo, readcn);
69584d9c625SLionel Sambuc 				bo += 4;
69684d9c625SLionel Sambuc 				break;
69784d9c625SLionel Sambuc 			}
69884d9c625SLionel Sambuc 			if (bo >= bsize)
69984d9c625SLionel Sambuc 				break;
70084d9c625SLionel Sambuc 		}
70184d9c625SLionel Sambuc 		updatefats(pmp, bp, bn);
70284d9c625SLionel Sambuc 	}
70384d9c625SLionel Sambuc 	pmp->pm_fmod = 1;
70484d9c625SLionel Sambuc 	return (0);
70584d9c625SLionel Sambuc }
70684d9c625SLionel Sambuc 
70784d9c625SLionel Sambuc /*
70884d9c625SLionel Sambuc  * Check the length of a free cluster chain starting at start.
70984d9c625SLionel Sambuc  *
71084d9c625SLionel Sambuc  * pmp	 - mount point
71184d9c625SLionel Sambuc  * start - start of chain
71284d9c625SLionel Sambuc  * count - maximum interesting length
71384d9c625SLionel Sambuc  */
71484d9c625SLionel Sambuc int
chainlength(struct msdosfsmount * pmp,u_long start,u_long count)71584d9c625SLionel Sambuc chainlength(struct msdosfsmount *pmp, u_long start, u_long count)
71684d9c625SLionel Sambuc {
71784d9c625SLionel Sambuc 	u_long idx, max_idx;
71884d9c625SLionel Sambuc 	u_int map;
71984d9c625SLionel Sambuc 	u_long len;
72084d9c625SLionel Sambuc 
72184d9c625SLionel Sambuc 	max_idx = pmp->pm_maxcluster / N_INUSEBITS;
72284d9c625SLionel Sambuc 	idx = start / N_INUSEBITS;
72384d9c625SLionel Sambuc 	start %= N_INUSEBITS;
72484d9c625SLionel Sambuc 	map = pmp->pm_inusemap[idx];
72584d9c625SLionel Sambuc 	map &= ~((1 << start) - 1);
72684d9c625SLionel Sambuc 	if (map) {
72784d9c625SLionel Sambuc 		len = ffs(map) - 1 - start;
72884d9c625SLionel Sambuc 		return (len > count ? count : len);
72984d9c625SLionel Sambuc 	}
73084d9c625SLionel Sambuc 	len = N_INUSEBITS - start;
73184d9c625SLionel Sambuc 	if (len >= count)
73284d9c625SLionel Sambuc 		return (count);
73384d9c625SLionel Sambuc 	while (++idx <= max_idx) {
73484d9c625SLionel Sambuc 		if (len >= count)
73584d9c625SLionel Sambuc 			break;
73684d9c625SLionel Sambuc 		if ((map = pmp->pm_inusemap[idx]) != 0) {
73784d9c625SLionel Sambuc 			len +=  ffs(map) - 1;
73884d9c625SLionel Sambuc 			break;
73984d9c625SLionel Sambuc 		}
74084d9c625SLionel Sambuc 		len += N_INUSEBITS;
74184d9c625SLionel Sambuc 	}
74284d9c625SLionel Sambuc 	return (len > count ? count : len);
74384d9c625SLionel Sambuc }
74484d9c625SLionel Sambuc 
74584d9c625SLionel Sambuc /*
74684d9c625SLionel Sambuc  * Allocate contigous free clusters.
74784d9c625SLionel Sambuc  *
74884d9c625SLionel Sambuc  * pmp	      - mount point.
74984d9c625SLionel Sambuc  * start      - start of cluster chain.
75084d9c625SLionel Sambuc  * count      - number of clusters to allocate.
75184d9c625SLionel Sambuc  * fillwith   - put this value into the FAT entry for the
75284d9c625SLionel Sambuc  *		last allocated cluster.
75384d9c625SLionel Sambuc  * retcluster - put the first allocated cluster's number here.
75484d9c625SLionel Sambuc  * got	      - how many clusters were actually allocated.
75584d9c625SLionel Sambuc  */
75684d9c625SLionel Sambuc int
chainalloc(struct msdosfsmount * pmp,u_long start,u_long count,u_long fillwith,u_long * retcluster,u_long * got)75784d9c625SLionel Sambuc chainalloc(struct msdosfsmount *pmp, u_long start, u_long count, u_long fillwith, u_long *retcluster, u_long *got)
75884d9c625SLionel Sambuc {
75984d9c625SLionel Sambuc 	int error;
76084d9c625SLionel Sambuc 	u_long cl, n;
76184d9c625SLionel Sambuc 
76284d9c625SLionel Sambuc 	for (cl = start, n = count; n-- > 0;)
76384d9c625SLionel Sambuc 		usemap_alloc(pmp, cl++);
76484d9c625SLionel Sambuc 	if ((error = fatchain(pmp, start, count, fillwith)) != 0)
76584d9c625SLionel Sambuc 		return (error);
76684d9c625SLionel Sambuc 
76784d9c625SLionel Sambuc 	DPRINTF(("%s(): allocated cluster chain at %lu (%lu clusters)\n",
76884d9c625SLionel Sambuc 	    __func__, start, count));
76984d9c625SLionel Sambuc 	if (retcluster)
77084d9c625SLionel Sambuc 		*retcluster = start;
77184d9c625SLionel Sambuc 	if (got)
77284d9c625SLionel Sambuc 		*got = count;
77384d9c625SLionel Sambuc 	return (0);
77484d9c625SLionel Sambuc }
77584d9c625SLionel Sambuc 
77684d9c625SLionel Sambuc /*
77784d9c625SLionel Sambuc  * Allocate contiguous free clusters.
77884d9c625SLionel Sambuc  *
77984d9c625SLionel Sambuc  * pmp	      - mount point.
78084d9c625SLionel Sambuc  * start      - preferred start of cluster chain.
78184d9c625SLionel Sambuc  * count      - number of clusters requested.
78284d9c625SLionel Sambuc  * fillwith   - put this value into the FAT entry for the
78384d9c625SLionel Sambuc  *		last allocated cluster.
78484d9c625SLionel Sambuc  * retcluster - put the first allocated cluster's number here.
78584d9c625SLionel Sambuc  * got	      - how many clusters were actually allocated.
78684d9c625SLionel Sambuc  */
78784d9c625SLionel Sambuc int
clusteralloc(struct msdosfsmount * pmp,u_long start,u_long count,u_long * retcluster,u_long * got)78884d9c625SLionel Sambuc clusteralloc(struct msdosfsmount *pmp, u_long start, u_long count, u_long *retcluster, u_long *got)
78984d9c625SLionel Sambuc {
79084d9c625SLionel Sambuc 	u_long idx;
79184d9c625SLionel Sambuc 	u_long len, newst, foundl, cn, l;
79284d9c625SLionel Sambuc 	u_long foundcn = 0; /* XXX: foundcn could be used unititialized */
79384d9c625SLionel Sambuc 	u_long fillwith = CLUST_EOFE;
79484d9c625SLionel Sambuc 	u_int map;
79584d9c625SLionel Sambuc 
79684d9c625SLionel Sambuc 	DPRINTF(("%s(): find %lu clusters\n", __func__, count));
79784d9c625SLionel Sambuc 	if (start) {
79884d9c625SLionel Sambuc 		if ((len = chainlength(pmp, start, count)) >= count)
79984d9c625SLionel Sambuc 			return (chainalloc(pmp, start, count, fillwith, retcluster, got));
80084d9c625SLionel Sambuc 	} else {
80184d9c625SLionel Sambuc 		/*
80284d9c625SLionel Sambuc 		 * This is a new file, initialize start
80384d9c625SLionel Sambuc 		 */
80484d9c625SLionel Sambuc 		struct timeval tv;
80584d9c625SLionel Sambuc 
80684d9c625SLionel Sambuc 		microtime(&tv);
80784d9c625SLionel Sambuc 		start = (tv.tv_usec >> 10) | tv.tv_usec;
80884d9c625SLionel Sambuc 		len = 0;
80984d9c625SLionel Sambuc 	}
81084d9c625SLionel Sambuc 
81184d9c625SLionel Sambuc 	/*
81284d9c625SLionel Sambuc 	 * Start at a (pseudo) random place to maximize cluster runs
81384d9c625SLionel Sambuc 	 * under multiple writers.
81484d9c625SLionel Sambuc 	 */
81584d9c625SLionel Sambuc 	newst = (start * 1103515245 + 12345) % (pmp->pm_maxcluster + 1);
81684d9c625SLionel Sambuc 	foundl = 0;
81784d9c625SLionel Sambuc 
81884d9c625SLionel Sambuc 	for (cn = newst; cn <= pmp->pm_maxcluster;) {
81984d9c625SLionel Sambuc 		idx = cn / N_INUSEBITS;
82084d9c625SLionel Sambuc 		map = pmp->pm_inusemap[idx];
82184d9c625SLionel Sambuc 		map |= (1 << (cn % N_INUSEBITS)) - 1;
82284d9c625SLionel Sambuc 		if (map != (u_int)-1) {
82384d9c625SLionel Sambuc 			cn = idx * N_INUSEBITS + ffs(map^(u_int)-1) - 1;
82484d9c625SLionel Sambuc 			if ((l = chainlength(pmp, cn, count)) >= count)
82584d9c625SLionel Sambuc 				return (chainalloc(pmp, cn, count, fillwith, retcluster, got));
82684d9c625SLionel Sambuc 			if (l > foundl) {
82784d9c625SLionel Sambuc 				foundcn = cn;
82884d9c625SLionel Sambuc 				foundl = l;
82984d9c625SLionel Sambuc 			}
83084d9c625SLionel Sambuc 			cn += l + 1;
83184d9c625SLionel Sambuc 			continue;
83284d9c625SLionel Sambuc 		}
83384d9c625SLionel Sambuc 		cn += N_INUSEBITS - cn % N_INUSEBITS;
83484d9c625SLionel Sambuc 	}
83584d9c625SLionel Sambuc 	for (cn = 0; cn < newst;) {
83684d9c625SLionel Sambuc 		idx = cn / N_INUSEBITS;
83784d9c625SLionel Sambuc 		map = pmp->pm_inusemap[idx];
83884d9c625SLionel Sambuc 		map |= (1 << (cn % N_INUSEBITS)) - 1;
83984d9c625SLionel Sambuc 		if (map != (u_int)-1) {
84084d9c625SLionel Sambuc 			cn = idx * N_INUSEBITS + ffs(map^(u_int)-1) - 1;
84184d9c625SLionel Sambuc 			if ((l = chainlength(pmp, cn, count)) >= count)
84284d9c625SLionel Sambuc 				return (chainalloc(pmp, cn, count, fillwith, retcluster, got));
84384d9c625SLionel Sambuc 			if (l > foundl) {
84484d9c625SLionel Sambuc 				foundcn = cn;
84584d9c625SLionel Sambuc 				foundl = l;
84684d9c625SLionel Sambuc 			}
84784d9c625SLionel Sambuc 			cn += l + 1;
84884d9c625SLionel Sambuc 			continue;
84984d9c625SLionel Sambuc 		}
85084d9c625SLionel Sambuc 		cn += N_INUSEBITS - cn % N_INUSEBITS;
85184d9c625SLionel Sambuc 	}
85284d9c625SLionel Sambuc 
85384d9c625SLionel Sambuc 	if (!foundl)
85484d9c625SLionel Sambuc 		return (ENOSPC);
85584d9c625SLionel Sambuc 
85684d9c625SLionel Sambuc 	if (len)
85784d9c625SLionel Sambuc 		return (chainalloc(pmp, start, len, fillwith, retcluster, got));
85884d9c625SLionel Sambuc 	else
85984d9c625SLionel Sambuc 		return (chainalloc(pmp, foundcn, foundl, fillwith, retcluster, got));
86084d9c625SLionel Sambuc }
86184d9c625SLionel Sambuc 
86284d9c625SLionel Sambuc 
86384d9c625SLionel Sambuc /*
86484d9c625SLionel Sambuc  * Free a chain of clusters.
86584d9c625SLionel Sambuc  *
86684d9c625SLionel Sambuc  * pmp		- address of the msdosfs mount structure for the filesystem
86784d9c625SLionel Sambuc  *		  containing the cluster chain to be freed.
86884d9c625SLionel Sambuc  * startcluster - number of the 1st cluster in the chain of clusters to be
86984d9c625SLionel Sambuc  *		  freed.
87084d9c625SLionel Sambuc  */
87184d9c625SLionel Sambuc int
freeclusterchain(struct msdosfsmount * pmp,u_long cluster)87284d9c625SLionel Sambuc freeclusterchain(struct msdosfsmount *pmp, u_long cluster)
87384d9c625SLionel Sambuc {
87484d9c625SLionel Sambuc 	int error;
87584d9c625SLionel Sambuc 	struct buf *bp = NULL;
87684d9c625SLionel Sambuc 	u_long bn, bo, bsize, byteoffset;
87784d9c625SLionel Sambuc 	u_long readcn, lbn = -1;
87884d9c625SLionel Sambuc 
87984d9c625SLionel Sambuc 	while (cluster >= CLUST_FIRST && cluster <= pmp->pm_maxcluster) {
88084d9c625SLionel Sambuc 		byteoffset = FATOFS(pmp, cluster);
88184d9c625SLionel Sambuc 		fatblock(pmp, byteoffset, &bn, &bsize, &bo);
88284d9c625SLionel Sambuc 		if (lbn != bn) {
88384d9c625SLionel Sambuc 			if (bp)
88484d9c625SLionel Sambuc 				updatefats(pmp, bp, lbn);
88584d9c625SLionel Sambuc 			error = bread(pmp->pm_devvp, de_bn2kb(pmp, bn), bsize,
886*0a6a1f1dSLionel Sambuc 			    B_MODIFY, &bp);
88784d9c625SLionel Sambuc 			if (error) {
88884d9c625SLionel Sambuc 				return (error);
88984d9c625SLionel Sambuc 			}
89084d9c625SLionel Sambuc 			lbn = bn;
89184d9c625SLionel Sambuc 		}
89284d9c625SLionel Sambuc 		usemap_free(pmp, cluster);
89384d9c625SLionel Sambuc 		KASSERT(bp != NULL);
89484d9c625SLionel Sambuc 		switch (pmp->pm_fatmask) {
89584d9c625SLionel Sambuc 		case FAT12_MASK:
89684d9c625SLionel Sambuc 			readcn = getushort((char *)bp->b_data + bo);
89784d9c625SLionel Sambuc 			if (cluster & 1) {
89884d9c625SLionel Sambuc 				cluster = readcn >> 4;
89984d9c625SLionel Sambuc 				readcn &= 0x000f;
90084d9c625SLionel Sambuc 				readcn |= MSDOSFSFREE << 4;
90184d9c625SLionel Sambuc 			} else {
90284d9c625SLionel Sambuc 				cluster = readcn;
90384d9c625SLionel Sambuc 				readcn &= 0xf000;
90484d9c625SLionel Sambuc 				readcn |= MSDOSFSFREE & 0xfff;
90584d9c625SLionel Sambuc 			}
90684d9c625SLionel Sambuc 			putushort((char *)bp->b_data + bo, readcn);
90784d9c625SLionel Sambuc 			break;
90884d9c625SLionel Sambuc 		case FAT16_MASK:
90984d9c625SLionel Sambuc 			cluster = getushort((char *)bp->b_data + bo);
91084d9c625SLionel Sambuc 			putushort((char *)bp->b_data + bo, MSDOSFSFREE);
91184d9c625SLionel Sambuc 			break;
91284d9c625SLionel Sambuc 		case FAT32_MASK:
91384d9c625SLionel Sambuc 			cluster = getulong((char *)bp->b_data + bo);
91484d9c625SLionel Sambuc 			putulong((char *)bp->b_data + bo,
91584d9c625SLionel Sambuc 				 (MSDOSFSFREE & FAT32_MASK) | (cluster & ~FAT32_MASK));
91684d9c625SLionel Sambuc 			break;
91784d9c625SLionel Sambuc 		}
91884d9c625SLionel Sambuc 		cluster &= pmp->pm_fatmask;
91984d9c625SLionel Sambuc 	}
92084d9c625SLionel Sambuc 	if (bp)
92184d9c625SLionel Sambuc 		updatefats(pmp, bp, bn);
92284d9c625SLionel Sambuc 	return (0);
92384d9c625SLionel Sambuc }
92484d9c625SLionel Sambuc 
92584d9c625SLionel Sambuc /*
92684d9c625SLionel Sambuc  * Read in FAT blocks looking for free clusters. For every free cluster
92784d9c625SLionel Sambuc  * found turn off its corresponding bit in the pm_inusemap.
92884d9c625SLionel Sambuc  */
92984d9c625SLionel Sambuc int
fillinusemap(struct msdosfsmount * pmp)93084d9c625SLionel Sambuc fillinusemap(struct msdosfsmount *pmp)
93184d9c625SLionel Sambuc {
93284d9c625SLionel Sambuc 	struct buf *bp = NULL;
93384d9c625SLionel Sambuc 	u_long cn, readcn;
93484d9c625SLionel Sambuc 	int error;
93584d9c625SLionel Sambuc 	u_long bn, bo, bsize, byteoffset;
93684d9c625SLionel Sambuc 
93784d9c625SLionel Sambuc 	/*
93884d9c625SLionel Sambuc 	 * Mark all clusters in use, we mark the free ones in the FAT scan
93984d9c625SLionel Sambuc 	 * loop further down.
94084d9c625SLionel Sambuc 	 */
94184d9c625SLionel Sambuc 	for (cn = 0; cn < (pmp->pm_maxcluster + N_INUSEBITS) / N_INUSEBITS; cn++)
94284d9c625SLionel Sambuc 		pmp->pm_inusemap[cn] = (u_int)-1;
94384d9c625SLionel Sambuc 
94484d9c625SLionel Sambuc 	/*
94584d9c625SLionel Sambuc 	 * Figure how many free clusters are in the filesystem by ripping
94684d9c625SLionel Sambuc 	 * through the FAT counting the number of entries whose content is
94784d9c625SLionel Sambuc 	 * zero.  These represent free clusters.
94884d9c625SLionel Sambuc 	 */
94984d9c625SLionel Sambuc 	pmp->pm_freeclustercount = 0;
95084d9c625SLionel Sambuc 	for (cn = CLUST_FIRST; cn <= pmp->pm_maxcluster; cn++) {
95184d9c625SLionel Sambuc 		byteoffset = FATOFS(pmp, cn);
95284d9c625SLionel Sambuc 		bo = byteoffset % pmp->pm_fatblocksize;
95384d9c625SLionel Sambuc 		if (!bo || !bp) {
95484d9c625SLionel Sambuc 			/* Read new FAT block */
95584d9c625SLionel Sambuc 			if (bp)
95684d9c625SLionel Sambuc 				brelse(bp, 0);
95784d9c625SLionel Sambuc 			fatblock(pmp, byteoffset, &bn, &bsize, NULL);
95884d9c625SLionel Sambuc 			error = bread(pmp->pm_devvp, de_bn2kb(pmp, bn), bsize,
959*0a6a1f1dSLionel Sambuc 			    0, &bp);
96084d9c625SLionel Sambuc 			if (error) {
96184d9c625SLionel Sambuc 				return (error);
96284d9c625SLionel Sambuc 			}
96384d9c625SLionel Sambuc 		}
96484d9c625SLionel Sambuc 		if (FAT32(pmp))
96584d9c625SLionel Sambuc 			readcn = getulong((char *)bp->b_data + bo);
96684d9c625SLionel Sambuc 		else
96784d9c625SLionel Sambuc 			readcn = getushort((char *)bp->b_data + bo);
96884d9c625SLionel Sambuc 		if (FAT12(pmp) && (cn & 1))
96984d9c625SLionel Sambuc 			readcn >>= 4;
97084d9c625SLionel Sambuc 		readcn &= pmp->pm_fatmask;
97184d9c625SLionel Sambuc 
97284d9c625SLionel Sambuc 		if (readcn == 0)
97384d9c625SLionel Sambuc 			usemap_free(pmp, cn);
97484d9c625SLionel Sambuc 	}
97584d9c625SLionel Sambuc 	if (bp)
97684d9c625SLionel Sambuc 		brelse(bp, 0);
97784d9c625SLionel Sambuc 	return (0);
97884d9c625SLionel Sambuc }
97984d9c625SLionel Sambuc 
98084d9c625SLionel Sambuc /*
98184d9c625SLionel Sambuc  * Allocate a new cluster and chain it onto the end of the file.
98284d9c625SLionel Sambuc  *
98384d9c625SLionel Sambuc  * dep	 - the file to extend
98484d9c625SLionel Sambuc  * count - number of clusters to allocate
98584d9c625SLionel Sambuc  * bpp	 - where to return the address of the buf header for the first new
98684d9c625SLionel Sambuc  *	   file block
98784d9c625SLionel Sambuc  * ncp	 - where to put cluster number of the first newly allocated cluster
98884d9c625SLionel Sambuc  *	   If this pointer is 0, do not return the cluster number.
98984d9c625SLionel Sambuc  * flags - see fat.h
99084d9c625SLionel Sambuc  *
99184d9c625SLionel Sambuc  * NOTE: This function is not responsible for turning on the DE_UPDATE bit of
99284d9c625SLionel Sambuc  * the de_flag field of the denode and it does not change the de_FileSize
99384d9c625SLionel Sambuc  * field.  This is left for the caller to do.
99484d9c625SLionel Sambuc  */
99584d9c625SLionel Sambuc 
99684d9c625SLionel Sambuc int
extendfile(struct denode * dep,u_long count,struct buf ** bpp,u_long * ncp,int flags)99784d9c625SLionel Sambuc extendfile(struct denode *dep, u_long count, struct buf **bpp, u_long *ncp, int flags)
99884d9c625SLionel Sambuc {
99984d9c625SLionel Sambuc 	int error;
100084d9c625SLionel Sambuc 	u_long frcn = 0, cn, got;
100184d9c625SLionel Sambuc 	struct msdosfsmount *pmp = dep->de_pmp;
100284d9c625SLionel Sambuc 	struct buf *bp;
100384d9c625SLionel Sambuc 
100484d9c625SLionel Sambuc 	/*
100584d9c625SLionel Sambuc 	 * Don't try to extend the root directory
100684d9c625SLionel Sambuc 	 */
100784d9c625SLionel Sambuc 	if (dep->de_StartCluster == MSDOSFSROOT
100884d9c625SLionel Sambuc 	    && (dep->de_Attributes & ATTR_DIRECTORY)) {
100984d9c625SLionel Sambuc 		DPRINTF(("%s(): attempt to extend root directory\n", __func__));
101084d9c625SLionel Sambuc 		return (ENOSPC);
101184d9c625SLionel Sambuc 	}
101284d9c625SLionel Sambuc 
101384d9c625SLionel Sambuc 	/*
101484d9c625SLionel Sambuc 	 * If the "file's last cluster" cache entry is empty, and the file
101584d9c625SLionel Sambuc 	 * is not empty, then fill the cache entry by calling pcbmap().
101684d9c625SLionel Sambuc 	 */
101784d9c625SLionel Sambuc 	fc_fileextends++;
101884d9c625SLionel Sambuc 	if (dep->de_fc[FC_LASTFC].fc_frcn == FCE_EMPTY &&
101984d9c625SLionel Sambuc 	    dep->de_StartCluster != 0) {
102084d9c625SLionel Sambuc 		fc_lfcempty++;
102184d9c625SLionel Sambuc 		error = pcbmap(dep, CLUST_END, 0, &cn, 0);
102284d9c625SLionel Sambuc 		/* we expect it to return E2BIG */
102384d9c625SLionel Sambuc 		if (error != E2BIG)
102484d9c625SLionel Sambuc 			return (error);
102584d9c625SLionel Sambuc 	}
102684d9c625SLionel Sambuc 
102784d9c625SLionel Sambuc 	fc_last_to_nexttolast(dep);
102884d9c625SLionel Sambuc 
102984d9c625SLionel Sambuc 	while (count > 0) {
103084d9c625SLionel Sambuc 
103184d9c625SLionel Sambuc 		/*
103284d9c625SLionel Sambuc 		 * Allocate a new cluster chain and cat onto the end of the
103384d9c625SLionel Sambuc 		 * file.  If the file is empty we make de_StartCluster point
103484d9c625SLionel Sambuc 		 * to the new block.  Note that de_StartCluster being 0 is
103584d9c625SLionel Sambuc 		 * sufficient to be sure the file is empty since we exclude
103684d9c625SLionel Sambuc 		 * attempts to extend the root directory above, and the root
103784d9c625SLionel Sambuc 		 * dir is the only file with a startcluster of 0 that has
103884d9c625SLionel Sambuc 		 * blocks allocated (sort of).
103984d9c625SLionel Sambuc 		 */
104084d9c625SLionel Sambuc 
104184d9c625SLionel Sambuc 		if (dep->de_StartCluster == 0)
104284d9c625SLionel Sambuc 			cn = 0;
104384d9c625SLionel Sambuc 		else
104484d9c625SLionel Sambuc 			cn = dep->de_fc[FC_LASTFC].fc_fsrcn + 1;
104584d9c625SLionel Sambuc 		error = clusteralloc(pmp, cn, count, &cn, &got);
104684d9c625SLionel Sambuc 		if (error)
104784d9c625SLionel Sambuc 			return (error);
104884d9c625SLionel Sambuc 
104984d9c625SLionel Sambuc 		count -= got;
105084d9c625SLionel Sambuc 
105184d9c625SLionel Sambuc 		/*
105284d9c625SLionel Sambuc 		 * Give them the filesystem relative cluster number if they want
105384d9c625SLionel Sambuc 		 * it.
105484d9c625SLionel Sambuc 		 */
105584d9c625SLionel Sambuc 		if (ncp) {
105684d9c625SLionel Sambuc 			*ncp = cn;
105784d9c625SLionel Sambuc 			ncp = NULL;
105884d9c625SLionel Sambuc 		}
105984d9c625SLionel Sambuc 
106084d9c625SLionel Sambuc 		if (dep->de_StartCluster == 0) {
106184d9c625SLionel Sambuc 			dep->de_StartCluster = cn;
106284d9c625SLionel Sambuc 			frcn = 0;
106384d9c625SLionel Sambuc 		} else {
106484d9c625SLionel Sambuc 			error = fatentry(FAT_SET, pmp,
106584d9c625SLionel Sambuc 					 dep->de_fc[FC_LASTFC].fc_fsrcn,
106684d9c625SLionel Sambuc 					 0, cn);
106784d9c625SLionel Sambuc 			if (error) {
106884d9c625SLionel Sambuc 				clusterfree(pmp, cn, NULL);
106984d9c625SLionel Sambuc 				return (error);
107084d9c625SLionel Sambuc 			}
107184d9c625SLionel Sambuc 			frcn = dep->de_fc[FC_LASTFC].fc_frcn + 1;
107284d9c625SLionel Sambuc 		}
107384d9c625SLionel Sambuc 
107484d9c625SLionel Sambuc 		/*
107584d9c625SLionel Sambuc 		 * Update the "last cluster of the file" entry in the
107684d9c625SLionel Sambuc 		 * denode's FAT cache.
107784d9c625SLionel Sambuc 		 */
107884d9c625SLionel Sambuc 
107984d9c625SLionel Sambuc 		fc_setcache(dep, FC_LASTFC, frcn + got - 1, cn + got - 1);
108084d9c625SLionel Sambuc 		if ((flags & DE_CLEAR) &&
108184d9c625SLionel Sambuc 		    (dep->de_Attributes & ATTR_DIRECTORY)) {
108284d9c625SLionel Sambuc 			while (got-- > 0) {
108384d9c625SLionel Sambuc 				bp = getblk(pmp->pm_devvp,
108484d9c625SLionel Sambuc 				    de_bn2kb(pmp, cntobn(pmp, cn++)),
108584d9c625SLionel Sambuc 				    pmp->pm_bpcluster, 0, 0);
108684d9c625SLionel Sambuc 				clrbuf(bp);
108784d9c625SLionel Sambuc 				if (bpp) {
108884d9c625SLionel Sambuc 					*bpp = bp;
108984d9c625SLionel Sambuc 						bpp = NULL;
109084d9c625SLionel Sambuc 				} else {
109184d9c625SLionel Sambuc 					bdwrite(bp);
109284d9c625SLionel Sambuc 				}
109384d9c625SLionel Sambuc 			}
109484d9c625SLionel Sambuc 		}
109584d9c625SLionel Sambuc 	}
109684d9c625SLionel Sambuc 
109784d9c625SLionel Sambuc 	return (0);
109884d9c625SLionel Sambuc }
1099