xref: /freebsd/sys/fs/msdosfs/msdosfs_fat.c (revision a26b949f2dbfe46a83fd90a95e6853c7a86c408c)
1c3aac50fSPeter Wemm /* $FreeBSD$ */
2952a6212SJordan K. Hubbard /*	$NetBSD: msdosfs_fat.c,v 1.28 1997/11/17 15:36:49 ws Exp $	*/
327a0bc89SDoug Rabson 
427a0bc89SDoug Rabson /*-
5952a6212SJordan K. Hubbard  * Copyright (C) 1994, 1995, 1997 Wolfgang Solfrank.
6952a6212SJordan K. Hubbard  * Copyright (C) 1994, 1995, 1997 TooLs GmbH.
727a0bc89SDoug Rabson  * All rights reserved.
827a0bc89SDoug Rabson  * Original code by Paul Popelka (paulp@uts.amdahl.com) (see below).
927a0bc89SDoug Rabson  *
1027a0bc89SDoug Rabson  * Redistribution and use in source and binary forms, with or without
1127a0bc89SDoug Rabson  * modification, are permitted provided that the following conditions
1227a0bc89SDoug Rabson  * are met:
1327a0bc89SDoug Rabson  * 1. Redistributions of source code must retain the above copyright
1427a0bc89SDoug Rabson  *    notice, this list of conditions and the following disclaimer.
1527a0bc89SDoug Rabson  * 2. Redistributions in binary form must reproduce the above copyright
1627a0bc89SDoug Rabson  *    notice, this list of conditions and the following disclaimer in the
1727a0bc89SDoug Rabson  *    documentation and/or other materials provided with the distribution.
1827a0bc89SDoug Rabson  * 3. All advertising materials mentioning features or use of this software
1927a0bc89SDoug Rabson  *    must display the following acknowledgement:
2027a0bc89SDoug Rabson  *	This product includes software developed by TooLs GmbH.
2127a0bc89SDoug Rabson  * 4. The name of TooLs GmbH may not be used to endorse or promote products
2227a0bc89SDoug Rabson  *    derived from this software without specific prior written permission.
2327a0bc89SDoug Rabson  *
2427a0bc89SDoug Rabson  * THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``AS IS'' AND ANY EXPRESS OR
2527a0bc89SDoug Rabson  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
2627a0bc89SDoug Rabson  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
2727a0bc89SDoug Rabson  * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
2827a0bc89SDoug Rabson  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
2927a0bc89SDoug Rabson  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
3027a0bc89SDoug Rabson  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
3127a0bc89SDoug Rabson  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
3227a0bc89SDoug Rabson  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
3327a0bc89SDoug Rabson  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
3427a0bc89SDoug Rabson  */
35d167cf6fSWarner Losh /*-
3627a0bc89SDoug Rabson  * Written by Paul Popelka (paulp@uts.amdahl.com)
3727a0bc89SDoug Rabson  *
3827a0bc89SDoug Rabson  * You can do anything you want with this software, just don't say you wrote
3927a0bc89SDoug Rabson  * it, and don't remove this notice.
4027a0bc89SDoug Rabson  *
4127a0bc89SDoug Rabson  * This software is provided "as is".
4227a0bc89SDoug Rabson  *
4327a0bc89SDoug Rabson  * The author supplies this software to be publicly redistributed on the
4427a0bc89SDoug Rabson  * understanding that the author is not responsible for the correct
4527a0bc89SDoug Rabson  * functioning of this software in any circumstances and is not liable for
4627a0bc89SDoug Rabson  * any damages caused by this software.
4727a0bc89SDoug Rabson  *
4827a0bc89SDoug Rabson  * October 1992
4927a0bc89SDoug Rabson  */
5027a0bc89SDoug Rabson 
5127a0bc89SDoug Rabson #include <sys/param.h>
5227a0bc89SDoug Rabson #include <sys/systm.h>
5327a0bc89SDoug Rabson #include <sys/buf.h>
545696c6e0SBruce Evans #include <sys/mount.h>
555696c6e0SBruce Evans #include <sys/vnode.h>
5627a0bc89SDoug Rabson 
571166fb51SRuslan Ermilov #include <fs/msdosfs/bpb.h>
581166fb51SRuslan Ermilov #include <fs/msdosfs/direntry.h>
591166fb51SRuslan Ermilov #include <fs/msdosfs/denode.h>
601166fb51SRuslan Ermilov #include <fs/msdosfs/fat.h>
615696c6e0SBruce Evans #include <fs/msdosfs/msdosfsmount.h>
6227a0bc89SDoug Rabson 
6311caded3SAlfred Perlstein static int	chainalloc(struct msdosfsmount *pmp, u_long start,
64b76d0b32SBruce Evans 		    u_long count, u_long fillwith, u_long *retcluster,
65b76d0b32SBruce Evans 		    u_long *got);
6611caded3SAlfred Perlstein static int	chainlength(struct msdosfsmount *pmp, u_long start,
6711caded3SAlfred Perlstein 		    u_long count);
68b76d0b32SBruce Evans static void	fatblock(struct msdosfsmount *pmp, u_long ofs, u_long *bnp,
69b76d0b32SBruce Evans 		    u_long *sizep, u_long *bop);
70b76d0b32SBruce Evans static int	fatchain(struct msdosfsmount *pmp, u_long start, u_long count,
71b76d0b32SBruce Evans 		    u_long fillwith);
72b76d0b32SBruce Evans static void	fc_lookup(struct denode *dep, u_long findcn, u_long *frcnp,
73b76d0b32SBruce Evans 		    u_long *fsrcnp);
7411caded3SAlfred Perlstein static void	updatefats(struct msdosfsmount *pmp, struct buf *bp,
7511caded3SAlfred Perlstein 		    u_long fatbn);
76c1087c13SBruce Evans static __inline void
7711caded3SAlfred Perlstein 		usemap_alloc(struct msdosfsmount *pmp, u_long cn);
78c1087c13SBruce Evans static __inline void
7911caded3SAlfred Perlstein 		usemap_free(struct msdosfsmount *pmp, u_long cn);
806be1a4ccSKonstantin Belousov static int	clusteralloc1(struct msdosfsmount *pmp, u_long start,
816be1a4ccSKonstantin Belousov 		    u_long count, u_long fillwith, u_long *retcluster,
826be1a4ccSKonstantin Belousov 		    u_long *got);
8358c27bcfSBruce Evans 
8427a0bc89SDoug Rabson static void
8527a0bc89SDoug Rabson fatblock(pmp, ofs, bnp, sizep, bop)
8627a0bc89SDoug Rabson 	struct msdosfsmount *pmp;
8727a0bc89SDoug Rabson 	u_long ofs;
8827a0bc89SDoug Rabson 	u_long *bnp;
8927a0bc89SDoug Rabson 	u_long *sizep;
9027a0bc89SDoug Rabson 	u_long *bop;
9127a0bc89SDoug Rabson {
9227a0bc89SDoug Rabson 	u_long bn, size;
9327a0bc89SDoug Rabson 
9427a0bc89SDoug Rabson 	bn = ofs / pmp->pm_fatblocksize * pmp->pm_fatblocksec;
9527a0bc89SDoug Rabson 	size = min(pmp->pm_fatblocksec, pmp->pm_FATsecs - bn)
9601f6cfbaSYoshihiro Takahashi 	    * DEV_BSIZE;
97952a6212SJordan K. Hubbard 	bn += pmp->pm_fatblk + pmp->pm_curfat * pmp->pm_FATsecs;
98952a6212SJordan K. Hubbard 
9927a0bc89SDoug Rabson 	if (bnp)
10027a0bc89SDoug Rabson 		*bnp = bn;
10127a0bc89SDoug Rabson 	if (sizep)
10227a0bc89SDoug Rabson 		*sizep = size;
10327a0bc89SDoug Rabson 	if (bop)
10427a0bc89SDoug Rabson 		*bop = ofs % pmp->pm_fatblocksize;
10527a0bc89SDoug Rabson }
10627a0bc89SDoug Rabson 
10727a0bc89SDoug Rabson /*
10827a0bc89SDoug Rabson  * Map the logical cluster number of a file into a physical disk sector
10927a0bc89SDoug Rabson  * that is filesystem relative.
11027a0bc89SDoug Rabson  *
11127a0bc89SDoug Rabson  * dep	  - address of denode representing the file of interest
11227a0bc89SDoug Rabson  * findcn - file relative cluster whose filesystem relative cluster number
11327a0bc89SDoug Rabson  *	    and/or block number are/is to be found
11427a0bc89SDoug Rabson  * bnp	  - address of where to place the filesystem relative block number.
11527a0bc89SDoug Rabson  *	    If this pointer is null then don't return this quantity.
11627a0bc89SDoug Rabson  * cnp	  - address of where to place the filesystem relative cluster number.
11727a0bc89SDoug Rabson  *	    If this pointer is null then don't return this quantity.
11827a0bc89SDoug Rabson  *
11927a0bc89SDoug Rabson  * NOTE: Either bnp or cnp must be non-null.
12027a0bc89SDoug Rabson  * This function has one side effect.  If the requested file relative cluster
12127a0bc89SDoug Rabson  * is beyond the end of file, then the actual number of clusters in the file
12227a0bc89SDoug Rabson  * is returned in *cnp.  This is useful for determining how long a directory is.
12327a0bc89SDoug Rabson  *  If cnp is null, nothing is returned.
12427a0bc89SDoug Rabson  */
12527a0bc89SDoug Rabson int
126952a6212SJordan K. Hubbard pcbmap(dep, findcn, bnp, cnp, sp)
12727a0bc89SDoug Rabson 	struct denode *dep;
12827a0bc89SDoug Rabson 	u_long findcn;		/* file relative cluster to get		 */
12927a0bc89SDoug Rabson 	daddr_t *bnp;		/* returned filesys relative blk number	 */
13027a0bc89SDoug Rabson 	u_long *cnp;		/* returned cluster number		 */
131952a6212SJordan K. Hubbard 	int *sp;		/* returned block size			 */
13227a0bc89SDoug Rabson {
13327a0bc89SDoug Rabson 	int error;
13427a0bc89SDoug Rabson 	u_long i;
13527a0bc89SDoug Rabson 	u_long cn;
136952a6212SJordan K. Hubbard 	u_long prevcn = 0; /* XXX: prevcn could be used unititialized */
13727a0bc89SDoug Rabson 	u_long byteoffset;
13827a0bc89SDoug Rabson 	u_long bn;
13927a0bc89SDoug Rabson 	u_long bo;
14027a0bc89SDoug Rabson 	struct buf *bp = NULL;
14127a0bc89SDoug Rabson 	u_long bp_bn = -1;
14227a0bc89SDoug Rabson 	struct msdosfsmount *pmp = dep->de_pmp;
14327a0bc89SDoug Rabson 	u_long bsize;
14427a0bc89SDoug Rabson 
145ef6a2be3SKonstantin Belousov 	KASSERT(bnp != NULL || cnp != NULL || sp != NULL,
146ef6a2be3SKonstantin Belousov 	    ("pcbmap: extra call"));
147ef6a2be3SKonstantin Belousov 	ASSERT_VOP_ELOCKED(DETOV(dep), "pcbmap");
14827a0bc89SDoug Rabson 
14927a0bc89SDoug Rabson 	cn = dep->de_StartCluster;
15027a0bc89SDoug Rabson 	/*
15127a0bc89SDoug Rabson 	 * The "file" that makes up the root directory is contiguous,
15227a0bc89SDoug Rabson 	 * permanently allocated, of fixed size, and is not made up of
15327a0bc89SDoug Rabson 	 * clusters.  If the cluster number is beyond the end of the root
15427a0bc89SDoug Rabson 	 * directory, then return the number of clusters in the file.
15527a0bc89SDoug Rabson 	 */
15627a0bc89SDoug Rabson 	if (cn == MSDOSFSROOT) {
15727a0bc89SDoug Rabson 		if (dep->de_Attributes & ATTR_DIRECTORY) {
158952a6212SJordan K. Hubbard 			if (de_cn2off(pmp, findcn) >= dep->de_FileSize) {
15927a0bc89SDoug Rabson 				if (cnp)
160952a6212SJordan K. Hubbard 					*cnp = de_bn2cn(pmp, pmp->pm_rootdirsize);
161952a6212SJordan K. Hubbard 				return (E2BIG);
16227a0bc89SDoug Rabson 			}
16327a0bc89SDoug Rabson 			if (bnp)
164952a6212SJordan K. Hubbard 				*bnp = pmp->pm_rootdirblk + de_cn2bn(pmp, findcn);
16527a0bc89SDoug Rabson 			if (cnp)
16627a0bc89SDoug Rabson 				*cnp = MSDOSFSROOT;
167952a6212SJordan K. Hubbard 			if (sp)
168952a6212SJordan K. Hubbard 				*sp = min(pmp->pm_bpcluster,
169952a6212SJordan K. Hubbard 				    dep->de_FileSize - de_cn2off(pmp, findcn));
170952a6212SJordan K. Hubbard 			return (0);
17127a0bc89SDoug Rabson 		} else {		/* just an empty file */
17227a0bc89SDoug Rabson 			if (cnp)
17327a0bc89SDoug Rabson 				*cnp = 0;
174952a6212SJordan K. Hubbard 			return (E2BIG);
17527a0bc89SDoug Rabson 		}
17627a0bc89SDoug Rabson 	}
17727a0bc89SDoug Rabson 
17827a0bc89SDoug Rabson 	/*
179952a6212SJordan K. Hubbard 	 * All other files do I/O in cluster sized blocks
180952a6212SJordan K. Hubbard 	 */
181952a6212SJordan K. Hubbard 	if (sp)
182952a6212SJordan K. Hubbard 		*sp = pmp->pm_bpcluster;
183952a6212SJordan K. Hubbard 
184952a6212SJordan K. Hubbard 	/*
18527a0bc89SDoug Rabson 	 * Rummage around in the fat cache, maybe we can avoid tromping
18627a0bc89SDoug Rabson 	 * thru every fat entry for the file. And, keep track of how far
18727a0bc89SDoug Rabson 	 * off the cache was from where we wanted to be.
18827a0bc89SDoug Rabson 	 */
18927a0bc89SDoug Rabson 	i = 0;
19027a0bc89SDoug Rabson 	fc_lookup(dep, findcn, &i, &cn);
19127a0bc89SDoug Rabson 
19227a0bc89SDoug Rabson 	/*
19327a0bc89SDoug Rabson 	 * Handle all other files or directories the normal way.
19427a0bc89SDoug Rabson 	 */
19527a0bc89SDoug Rabson 	for (; i < findcn; i++) {
196952a6212SJordan K. Hubbard 		/*
197952a6212SJordan K. Hubbard 		 * Stop with all reserved clusters, not just with EOF.
198952a6212SJordan K. Hubbard 		 */
199952a6212SJordan K. Hubbard 		if ((cn | ~pmp->pm_fatmask) >= CLUST_RSRVD)
20027a0bc89SDoug Rabson 			goto hiteof;
20127a0bc89SDoug Rabson 		byteoffset = FATOFS(pmp, cn);
20227a0bc89SDoug Rabson 		fatblock(pmp, byteoffset, &bn, &bsize, &bo);
20327a0bc89SDoug Rabson 		if (bn != bp_bn) {
20427a0bc89SDoug Rabson 			if (bp)
20527a0bc89SDoug Rabson 				brelse(bp);
20627a0bc89SDoug Rabson 			error = bread(pmp->pm_devvp, bn, bsize, NOCRED, &bp);
207952a6212SJordan K. Hubbard 			if (error) {
208952a6212SJordan K. Hubbard 				brelse(bp);
209952a6212SJordan K. Hubbard 				return (error);
210952a6212SJordan K. Hubbard 			}
21127a0bc89SDoug Rabson 			bp_bn = bn;
21227a0bc89SDoug Rabson 		}
21327a0bc89SDoug Rabson 		prevcn = cn;
214d23af19aSTim J. Robbins 		if (bo >= bsize) {
215d23af19aSTim J. Robbins 			if (bp)
216d23af19aSTim J. Robbins 				brelse(bp);
217d23af19aSTim J. Robbins 			return (EIO);
218d23af19aSTim J. Robbins 		}
219952a6212SJordan K. Hubbard 		if (FAT32(pmp))
220952a6212SJordan K. Hubbard 			cn = getulong(&bp->b_data[bo]);
221952a6212SJordan K. Hubbard 		else
22227a0bc89SDoug Rabson 			cn = getushort(&bp->b_data[bo]);
223952a6212SJordan K. Hubbard 		if (FAT12(pmp) && (prevcn & 1))
22427a0bc89SDoug Rabson 			cn >>= 4;
225952a6212SJordan K. Hubbard 		cn &= pmp->pm_fatmask;
226952a6212SJordan K. Hubbard 
22727a0bc89SDoug Rabson 		/*
228952a6212SJordan K. Hubbard 		 * Force the special cluster numbers
229952a6212SJordan K. Hubbard 		 * to be the same for all cluster sizes
230952a6212SJordan K. Hubbard 		 * to let the rest of msdosfs handle
231952a6212SJordan K. Hubbard 		 * all cases the same.
23227a0bc89SDoug Rabson 		 */
233952a6212SJordan K. Hubbard 		if ((cn | ~pmp->pm_fatmask) >= CLUST_RSRVD)
234952a6212SJordan K. Hubbard 			cn |= ~pmp->pm_fatmask;
23527a0bc89SDoug Rabson 	}
23627a0bc89SDoug Rabson 
237952a6212SJordan K. Hubbard 	if (!MSDOSFSEOF(pmp, cn)) {
23827a0bc89SDoug Rabson 		if (bp)
23927a0bc89SDoug Rabson 			brelse(bp);
24027a0bc89SDoug Rabson 		if (bnp)
24127a0bc89SDoug Rabson 			*bnp = cntobn(pmp, cn);
24227a0bc89SDoug Rabson 		if (cnp)
24327a0bc89SDoug Rabson 			*cnp = cn;
24427a0bc89SDoug Rabson 		fc_setcache(dep, FC_LASTMAP, i, cn);
245952a6212SJordan K. Hubbard 		return (0);
24627a0bc89SDoug Rabson 	}
24727a0bc89SDoug Rabson 
24827a0bc89SDoug Rabson hiteof:;
24927a0bc89SDoug Rabson 	if (cnp)
25027a0bc89SDoug Rabson 		*cnp = i;
25127a0bc89SDoug Rabson 	if (bp)
25227a0bc89SDoug Rabson 		brelse(bp);
25327a0bc89SDoug Rabson 	/* update last file cluster entry in the fat cache */
25427a0bc89SDoug Rabson 	fc_setcache(dep, FC_LASTFC, i - 1, prevcn);
255952a6212SJordan K. Hubbard 	return (E2BIG);
25627a0bc89SDoug Rabson }
25727a0bc89SDoug Rabson 
25827a0bc89SDoug Rabson /*
25927a0bc89SDoug Rabson  * Find the closest entry in the fat cache to the cluster we are looking
26027a0bc89SDoug Rabson  * for.
26127a0bc89SDoug Rabson  */
2627fefffeeSPoul-Henning Kamp static void
263c3c6d51eSPoul-Henning Kamp fc_lookup(dep, findcn, frcnp, fsrcnp)
26427a0bc89SDoug Rabson 	struct denode *dep;
26527a0bc89SDoug Rabson 	u_long findcn;
26627a0bc89SDoug Rabson 	u_long *frcnp;
26727a0bc89SDoug Rabson 	u_long *fsrcnp;
26827a0bc89SDoug Rabson {
26927a0bc89SDoug Rabson 	int i;
27027a0bc89SDoug Rabson 	u_long cn;
27127a0bc89SDoug Rabson 	struct fatcache *closest = 0;
27227a0bc89SDoug Rabson 
273ef6a2be3SKonstantin Belousov 	ASSERT_VOP_LOCKED(DETOV(dep), "fc_lookup");
274ef6a2be3SKonstantin Belousov 
27527a0bc89SDoug Rabson 	for (i = 0; i < FC_SIZE; i++) {
27627a0bc89SDoug Rabson 		cn = dep->de_fc[i].fc_frcn;
27727a0bc89SDoug Rabson 		if (cn != FCE_EMPTY && cn <= findcn) {
27827a0bc89SDoug Rabson 			if (closest == 0 || cn > closest->fc_frcn)
27927a0bc89SDoug Rabson 				closest = &dep->de_fc[i];
28027a0bc89SDoug Rabson 		}
28127a0bc89SDoug Rabson 	}
28227a0bc89SDoug Rabson 	if (closest) {
28327a0bc89SDoug Rabson 		*frcnp = closest->fc_frcn;
28427a0bc89SDoug Rabson 		*fsrcnp = closest->fc_fsrcn;
28527a0bc89SDoug Rabson 	}
28627a0bc89SDoug Rabson }
28727a0bc89SDoug Rabson 
28827a0bc89SDoug Rabson /*
28927a0bc89SDoug Rabson  * Purge the fat cache in denode dep of all entries relating to file
29027a0bc89SDoug Rabson  * relative cluster frcn and beyond.
29127a0bc89SDoug Rabson  */
292952a6212SJordan K. Hubbard void
293952a6212SJordan K. Hubbard fc_purge(dep, frcn)
29427a0bc89SDoug Rabson 	struct denode *dep;
29527a0bc89SDoug Rabson 	u_int frcn;
29627a0bc89SDoug Rabson {
29727a0bc89SDoug Rabson 	int i;
29827a0bc89SDoug Rabson 	struct fatcache *fcp;
29927a0bc89SDoug Rabson 
300ef6a2be3SKonstantin Belousov 	ASSERT_VOP_ELOCKED(DETOV(dep), "fc_purge");
301ef6a2be3SKonstantin Belousov 
30227a0bc89SDoug Rabson 	fcp = dep->de_fc;
30327a0bc89SDoug Rabson 	for (i = 0; i < FC_SIZE; i++, fcp++) {
30427a0bc89SDoug Rabson 		if (fcp->fc_frcn >= frcn)
30527a0bc89SDoug Rabson 			fcp->fc_frcn = FCE_EMPTY;
30627a0bc89SDoug Rabson 	}
30727a0bc89SDoug Rabson }
30827a0bc89SDoug Rabson 
30927a0bc89SDoug Rabson /*
310952a6212SJordan K. Hubbard  * Update the fat.
311952a6212SJordan K. Hubbard  * If mirroring the fat, update all copies, with the first copy as last.
312952a6212SJordan K. Hubbard  * Else update only the current fat (ignoring the others).
31327a0bc89SDoug Rabson  *
31427a0bc89SDoug Rabson  * pmp	 - msdosfsmount structure for filesystem to update
31527a0bc89SDoug Rabson  * bp	 - addr of modified fat block
31627a0bc89SDoug Rabson  * fatbn - block number relative to begin of filesystem of the modified fat block.
31727a0bc89SDoug Rabson  */
318a98ca469SPoul-Henning Kamp static void
31927a0bc89SDoug Rabson updatefats(pmp, bp, fatbn)
32027a0bc89SDoug Rabson 	struct msdosfsmount *pmp;
32127a0bc89SDoug Rabson 	struct buf *bp;
32227a0bc89SDoug Rabson 	u_long fatbn;
32327a0bc89SDoug Rabson {
32427a0bc89SDoug Rabson 	int i;
32527a0bc89SDoug Rabson 	struct buf *bpn;
32627a0bc89SDoug Rabson 
32727a0bc89SDoug Rabson #ifdef MSDOSFS_DEBUG
328952a6212SJordan K. Hubbard 	printf("updatefats(pmp %p, bp %p, fatbn %lu)\n", pmp, bp, fatbn);
32927a0bc89SDoug Rabson #endif
33027a0bc89SDoug Rabson 
33127a0bc89SDoug Rabson 	/*
332952a6212SJordan K. Hubbard 	 * If we have an FSInfo block, update it.
333952a6212SJordan K. Hubbard 	 */
334952a6212SJordan K. Hubbard 	if (pmp->pm_fsinfo) {
33537269429SBruce Evans 		if (bread(pmp->pm_devvp, pmp->pm_fsinfo, pmp->pm_BytesPerSec,
33601f6cfbaSYoshihiro Takahashi 		    NOCRED, &bpn) != 0) {
337952a6212SJordan K. Hubbard 			/*
338952a6212SJordan K. Hubbard 			 * Ignore the error, but turn off FSInfo update for the future.
339952a6212SJordan K. Hubbard 			 */
340952a6212SJordan K. Hubbard 			pmp->pm_fsinfo = 0;
341952a6212SJordan K. Hubbard 			brelse(bpn);
342952a6212SJordan K. Hubbard 		} else {
343952a6212SJordan K. Hubbard 			struct fsinfo *fp = (struct fsinfo *)bpn->b_data;
344952a6212SJordan K. Hubbard 
345952a6212SJordan K. Hubbard 			putulong(fp->fsinfree, pmp->pm_freeclustercount);
346952a6212SJordan K. Hubbard 			putulong(fp->fsinxtfree, pmp->pm_nxtfree);
347952a6212SJordan K. Hubbard 			if (pmp->pm_flags & MSDOSFSMNT_WAITONFAT)
348952a6212SJordan K. Hubbard 				bwrite(bpn);
349952a6212SJordan K. Hubbard 			else
350952a6212SJordan K. Hubbard 				bdwrite(bpn);
351952a6212SJordan K. Hubbard 		}
352952a6212SJordan K. Hubbard 	}
353952a6212SJordan K. Hubbard 
354952a6212SJordan K. Hubbard 	if (pmp->pm_flags & MSDOSFS_FATMIRROR) {
355952a6212SJordan K. Hubbard 		/*
35627a0bc89SDoug Rabson 		 * Now copy the block(s) of the modified fat to the other copies of
35727a0bc89SDoug Rabson 		 * the fat and write them out.  This is faster than reading in the
35827a0bc89SDoug Rabson 		 * other fats and then writing them back out.  This could tie up
35927a0bc89SDoug Rabson 		 * the fat for quite a while. Preventing others from accessing it.
36027a0bc89SDoug Rabson 		 * To prevent us from going after the fat quite so much we use
36127a0bc89SDoug Rabson 		 * delayed writes, unless they specfied "synchronous" when the
36227a0bc89SDoug Rabson 		 * filesystem was mounted.  If synch is asked for then use
36327a0bc89SDoug Rabson 		 * bwrite()'s and really slow things down.
36427a0bc89SDoug Rabson 		 */
36527a0bc89SDoug Rabson 		for (i = 1; i < pmp->pm_FATs; i++) {
36627a0bc89SDoug Rabson 			fatbn += pmp->pm_FATsecs;
36727a0bc89SDoug Rabson 			/* getblk() never fails */
3687261f5f6SJeff Roberson 			bpn = getblk(pmp->pm_devvp, fatbn, bp->b_bcount,
3697261f5f6SJeff Roberson 			    0, 0, 0);
37027a0bc89SDoug Rabson 			bcopy(bp->b_data, bpn->b_data, bp->b_bcount);
371952a6212SJordan K. Hubbard 			if (pmp->pm_flags & MSDOSFSMNT_WAITONFAT)
37227a0bc89SDoug Rabson 				bwrite(bpn);
37327a0bc89SDoug Rabson 			else
37427a0bc89SDoug Rabson 				bdwrite(bpn);
37527a0bc89SDoug Rabson 		}
376952a6212SJordan K. Hubbard 	}
377952a6212SJordan K. Hubbard 
37827a0bc89SDoug Rabson 	/*
379952a6212SJordan K. Hubbard 	 * Write out the first (or current) fat last.
38027a0bc89SDoug Rabson 	 */
381952a6212SJordan K. Hubbard 	if (pmp->pm_flags & MSDOSFSMNT_WAITONFAT)
38227a0bc89SDoug Rabson 		bwrite(bp);
38327a0bc89SDoug Rabson 	else
38427a0bc89SDoug Rabson 		bdwrite(bp);
385952a6212SJordan K. Hubbard 	/*
386952a6212SJordan K. Hubbard 	 * Maybe update fsinfo sector here?
387952a6212SJordan K. Hubbard 	 */
38827a0bc89SDoug Rabson }
38927a0bc89SDoug Rabson 
39027a0bc89SDoug Rabson /*
39127a0bc89SDoug Rabson  * Updating entries in 12 bit fats is a pain in the butt.
39227a0bc89SDoug Rabson  *
39327a0bc89SDoug Rabson  * The following picture shows where nibbles go when moving from a 12 bit
39427a0bc89SDoug Rabson  * cluster number into the appropriate bytes in the FAT.
39527a0bc89SDoug Rabson  *
39627a0bc89SDoug Rabson  *	byte m        byte m+1      byte m+2
39727a0bc89SDoug Rabson  *	+----+----+   +----+----+   +----+----+
39827a0bc89SDoug Rabson  *	|  0    1 |   |  2    3 |   |  4    5 |   FAT bytes
39927a0bc89SDoug Rabson  *	+----+----+   +----+----+   +----+----+
40027a0bc89SDoug Rabson  *
40127a0bc89SDoug Rabson  *	+----+----+----+   +----+----+----+
40227a0bc89SDoug Rabson  *	|  3    0    1 |   |  4    5    2 |
40327a0bc89SDoug Rabson  *	+----+----+----+   +----+----+----+
40427a0bc89SDoug Rabson  *	cluster n  	   cluster n+1
40527a0bc89SDoug Rabson  *
40627a0bc89SDoug Rabson  * Where n is even. m = n + (n >> 2)
40727a0bc89SDoug Rabson  *
40827a0bc89SDoug Rabson  */
409c1087c13SBruce Evans static __inline void
41027a0bc89SDoug Rabson usemap_alloc(pmp, cn)
41127a0bc89SDoug Rabson 	struct msdosfsmount *pmp;
41227a0bc89SDoug Rabson 	u_long cn;
41327a0bc89SDoug Rabson {
414952a6212SJordan K. Hubbard 
4156be1a4ccSKonstantin Belousov 	MSDOSFS_ASSERT_MP_LOCKED(pmp);
416eb739c7cSKonstantin Belousov 
417eb739c7cSKonstantin Belousov 	KASSERT((pmp->pm_inusemap[cn / N_INUSEBITS] & (1 << (cn % N_INUSEBITS)))
418eb739c7cSKonstantin Belousov 	    == 0, ("Allocating used sector %ld %ld %x", cn, cn % N_INUSEBITS,
419eb739c7cSKonstantin Belousov 		(unsigned)pmp->pm_inusemap[cn / N_INUSEBITS]));
420952a6212SJordan K. Hubbard 	pmp->pm_inusemap[cn / N_INUSEBITS] |= 1 << (cn % N_INUSEBITS);
421eb739c7cSKonstantin Belousov 	KASSERT(pmp->pm_freeclustercount > 0, ("usemap_alloc: too little"));
42227a0bc89SDoug Rabson 	pmp->pm_freeclustercount--;
42327a0bc89SDoug Rabson }
42427a0bc89SDoug Rabson 
425c1087c13SBruce Evans static __inline void
42627a0bc89SDoug Rabson usemap_free(pmp, cn)
42727a0bc89SDoug Rabson 	struct msdosfsmount *pmp;
42827a0bc89SDoug Rabson 	u_long cn;
42927a0bc89SDoug Rabson {
430952a6212SJordan K. Hubbard 
4316be1a4ccSKonstantin Belousov 	MSDOSFS_ASSERT_MP_LOCKED(pmp);
43227a0bc89SDoug Rabson 	pmp->pm_freeclustercount++;
433eb739c7cSKonstantin Belousov 	KASSERT((pmp->pm_inusemap[cn / N_INUSEBITS] & (1 << (cn % N_INUSEBITS)))
434eb739c7cSKonstantin Belousov 	    != 0, ("Freeing unused sector %ld %ld %x", cn, cn % N_INUSEBITS,
435eb739c7cSKonstantin Belousov 		(unsigned)pmp->pm_inusemap[cn / N_INUSEBITS]));
43627a0bc89SDoug Rabson 	pmp->pm_inusemap[cn / N_INUSEBITS] &= ~(1 << (cn % N_INUSEBITS));
43727a0bc89SDoug Rabson }
43827a0bc89SDoug Rabson 
43927a0bc89SDoug Rabson int
44027a0bc89SDoug Rabson clusterfree(pmp, cluster, oldcnp)
44127a0bc89SDoug Rabson 	struct msdosfsmount *pmp;
44227a0bc89SDoug Rabson 	u_long cluster;
44327a0bc89SDoug Rabson 	u_long *oldcnp;
44427a0bc89SDoug Rabson {
44527a0bc89SDoug Rabson 	int error;
44627a0bc89SDoug Rabson 	u_long oldcn;
44727a0bc89SDoug Rabson 
44827a0bc89SDoug Rabson 	error = fatentry(FAT_GET_AND_SET, pmp, cluster, &oldcn, MSDOSFSFREE);
4496be1a4ccSKonstantin Belousov 	if (error)
450952a6212SJordan K. Hubbard 		return (error);
45127a0bc89SDoug Rabson 	/*
45227a0bc89SDoug Rabson 	 * If the cluster was successfully marked free, then update
45327a0bc89SDoug Rabson 	 * the count of free clusters, and turn off the "allocated"
45427a0bc89SDoug Rabson 	 * bit in the "in use" cluster bit map.
45527a0bc89SDoug Rabson 	 */
4566be1a4ccSKonstantin Belousov 	MSDOSFS_LOCK_MP(pmp);
4576be1a4ccSKonstantin Belousov 	usemap_free(pmp, cluster);
4586be1a4ccSKonstantin Belousov 	MSDOSFS_UNLOCK_MP(pmp);
45927a0bc89SDoug Rabson 	if (oldcnp)
46027a0bc89SDoug Rabson 		*oldcnp = oldcn;
461952a6212SJordan K. Hubbard 	return (0);
46227a0bc89SDoug Rabson }
46327a0bc89SDoug Rabson 
46427a0bc89SDoug Rabson /*
46527a0bc89SDoug Rabson  * Get or Set or 'Get and Set' the cluster'th entry in the fat.
46627a0bc89SDoug Rabson  *
46727a0bc89SDoug Rabson  * function	- whether to get or set a fat entry
46827a0bc89SDoug Rabson  * pmp		- address of the msdosfsmount structure for the filesystem
46927a0bc89SDoug Rabson  *		  whose fat is to be manipulated.
47027a0bc89SDoug Rabson  * cn		- which cluster is of interest
47127a0bc89SDoug Rabson  * oldcontents	- address of a word that is to receive the contents of the
47227a0bc89SDoug Rabson  *		  cluster'th entry if this is a get function
47327a0bc89SDoug Rabson  * newcontents	- the new value to be written into the cluster'th element of
47427a0bc89SDoug Rabson  *		  the fat if this is a set function.
47527a0bc89SDoug Rabson  *
47627a0bc89SDoug Rabson  * This function can also be used to free a cluster by setting the fat entry
47727a0bc89SDoug Rabson  * for a cluster to 0.
47827a0bc89SDoug Rabson  *
47927a0bc89SDoug Rabson  * All copies of the fat are updated if this is a set function. NOTE: If
48027a0bc89SDoug Rabson  * fatentry() marks a cluster as free it does not update the inusemap in
48127a0bc89SDoug Rabson  * the msdosfsmount structure. This is left to the caller.
48227a0bc89SDoug Rabson  */
48327a0bc89SDoug Rabson int
48427a0bc89SDoug Rabson fatentry(function, pmp, cn, oldcontents, newcontents)
48527a0bc89SDoug Rabson 	int function;
48627a0bc89SDoug Rabson 	struct msdosfsmount *pmp;
48727a0bc89SDoug Rabson 	u_long cn;
48827a0bc89SDoug Rabson 	u_long *oldcontents;
48927a0bc89SDoug Rabson 	u_long newcontents;
49027a0bc89SDoug Rabson {
49127a0bc89SDoug Rabson 	int error;
49227a0bc89SDoug Rabson 	u_long readcn;
49327a0bc89SDoug Rabson 	u_long bn, bo, bsize, byteoffset;
49427a0bc89SDoug Rabson 	struct buf *bp;
49527a0bc89SDoug Rabson 
496952a6212SJordan K. Hubbard #ifdef	MSDOSFS_DEBUG
497952a6212SJordan K. Hubbard 	printf("fatentry(func %d, pmp %p, clust %lu, oldcon %p, newcon %lx)\n",
498952a6212SJordan K. Hubbard 	    function, pmp, cn, oldcontents, newcontents);
499952a6212SJordan K. Hubbard #endif
50027a0bc89SDoug Rabson 
50127a0bc89SDoug Rabson #ifdef DIAGNOSTIC
50227a0bc89SDoug Rabson 	/*
50327a0bc89SDoug Rabson 	 * Be sure they asked us to do something.
50427a0bc89SDoug Rabson 	 */
50527a0bc89SDoug Rabson 	if ((function & (FAT_SET | FAT_GET)) == 0) {
50654cf9198SKonstantin Belousov #ifdef MSDOSFS_DEBUG
50727a0bc89SDoug Rabson 		printf("fatentry(): function code doesn't specify get or set\n");
50854cf9198SKonstantin Belousov #endif
509952a6212SJordan K. Hubbard 		return (EINVAL);
51027a0bc89SDoug Rabson 	}
51127a0bc89SDoug Rabson 
51227a0bc89SDoug Rabson 	/*
51327a0bc89SDoug Rabson 	 * If they asked us to return a cluster number but didn't tell us
51427a0bc89SDoug Rabson 	 * where to put it, give them an error.
51527a0bc89SDoug Rabson 	 */
51627a0bc89SDoug Rabson 	if ((function & FAT_GET) && oldcontents == NULL) {
51754cf9198SKonstantin Belousov #ifdef MSDOSFS_DEBUG
51827a0bc89SDoug Rabson 		printf("fatentry(): get function with no place to put result\n");
51954cf9198SKonstantin Belousov #endif
520952a6212SJordan K. Hubbard 		return (EINVAL);
52127a0bc89SDoug Rabson 	}
52227a0bc89SDoug Rabson #endif
52327a0bc89SDoug Rabson 
52427a0bc89SDoug Rabson 	/*
52527a0bc89SDoug Rabson 	 * Be sure the requested cluster is in the filesystem.
52627a0bc89SDoug Rabson 	 */
52727a0bc89SDoug Rabson 	if (cn < CLUST_FIRST || cn > pmp->pm_maxcluster)
528952a6212SJordan K. Hubbard 		return (EINVAL);
52927a0bc89SDoug Rabson 
53027a0bc89SDoug Rabson 	byteoffset = FATOFS(pmp, cn);
53127a0bc89SDoug Rabson 	fatblock(pmp, byteoffset, &bn, &bsize, &bo);
532c3c6d51eSPoul-Henning Kamp 	error = bread(pmp->pm_devvp, bn, bsize, NOCRED, &bp);
533952a6212SJordan K. Hubbard 	if (error) {
534952a6212SJordan K. Hubbard 		brelse(bp);
535952a6212SJordan K. Hubbard 		return (error);
536952a6212SJordan K. Hubbard 	}
53727a0bc89SDoug Rabson 
53827a0bc89SDoug Rabson 	if (function & FAT_GET) {
539952a6212SJordan K. Hubbard 		if (FAT32(pmp))
540952a6212SJordan K. Hubbard 			readcn = getulong(&bp->b_data[bo]);
541952a6212SJordan K. Hubbard 		else
54227a0bc89SDoug Rabson 			readcn = getushort(&bp->b_data[bo]);
543952a6212SJordan K. Hubbard 		if (FAT12(pmp) & (cn & 1))
54427a0bc89SDoug Rabson 			readcn >>= 4;
545952a6212SJordan K. Hubbard 		readcn &= pmp->pm_fatmask;
546952a6212SJordan K. Hubbard 		/* map reserved fat entries to same values for all fats */
547952a6212SJordan K. Hubbard 		if ((readcn | ~pmp->pm_fatmask) >= CLUST_RSRVD)
548952a6212SJordan K. Hubbard 			readcn |= ~pmp->pm_fatmask;
54927a0bc89SDoug Rabson 		*oldcontents = readcn;
55027a0bc89SDoug Rabson 	}
55127a0bc89SDoug Rabson 	if (function & FAT_SET) {
552952a6212SJordan K. Hubbard 		switch (pmp->pm_fatmask) {
553952a6212SJordan K. Hubbard 		case FAT12_MASK:
55427a0bc89SDoug Rabson 			readcn = getushort(&bp->b_data[bo]);
55527a0bc89SDoug Rabson 			if (cn & 1) {
55627a0bc89SDoug Rabson 				readcn &= 0x000f;
55727a0bc89SDoug Rabson 				readcn |= newcontents << 4;
55827a0bc89SDoug Rabson 			} else {
55927a0bc89SDoug Rabson 				readcn &= 0xf000;
56027a0bc89SDoug Rabson 				readcn |= newcontents & 0xfff;
56127a0bc89SDoug Rabson 			}
56227a0bc89SDoug Rabson 			putushort(&bp->b_data[bo], readcn);
563952a6212SJordan K. Hubbard 			break;
564952a6212SJordan K. Hubbard 		case FAT16_MASK:
56527a0bc89SDoug Rabson 			putushort(&bp->b_data[bo], newcontents);
566952a6212SJordan K. Hubbard 			break;
567952a6212SJordan K. Hubbard 		case FAT32_MASK:
568952a6212SJordan K. Hubbard 			/*
569952a6212SJordan K. Hubbard 			 * According to spec we have to retain the
570952a6212SJordan K. Hubbard 			 * high order bits of the fat entry.
571952a6212SJordan K. Hubbard 			 */
572952a6212SJordan K. Hubbard 			readcn = getulong(&bp->b_data[bo]);
573952a6212SJordan K. Hubbard 			readcn &= ~FAT32_MASK;
574952a6212SJordan K. Hubbard 			readcn |= newcontents & FAT32_MASK;
575952a6212SJordan K. Hubbard 			putulong(&bp->b_data[bo], readcn);
576952a6212SJordan K. Hubbard 			break;
577952a6212SJordan K. Hubbard 		}
57827a0bc89SDoug Rabson 		updatefats(pmp, bp, bn);
57927a0bc89SDoug Rabson 		bp = NULL;
58027a0bc89SDoug Rabson 		pmp->pm_fmod = 1;
58127a0bc89SDoug Rabson 	}
58227a0bc89SDoug Rabson 	if (bp)
58327a0bc89SDoug Rabson 		brelse(bp);
584952a6212SJordan K. Hubbard 	return (0);
58527a0bc89SDoug Rabson }
58627a0bc89SDoug Rabson 
58727a0bc89SDoug Rabson /*
58827a0bc89SDoug Rabson  * Update a contiguous cluster chain
58927a0bc89SDoug Rabson  *
59027a0bc89SDoug Rabson  * pmp	    - mount point
59127a0bc89SDoug Rabson  * start    - first cluster of chain
59227a0bc89SDoug Rabson  * count    - number of clusters in chain
59327a0bc89SDoug Rabson  * fillwith - what to write into fat entry of last cluster
59427a0bc89SDoug Rabson  */
59527a0bc89SDoug Rabson static int
59627a0bc89SDoug Rabson fatchain(pmp, start, count, fillwith)
59727a0bc89SDoug Rabson 	struct msdosfsmount *pmp;
59827a0bc89SDoug Rabson 	u_long start;
59927a0bc89SDoug Rabson 	u_long count;
60027a0bc89SDoug Rabson 	u_long fillwith;
60127a0bc89SDoug Rabson {
60227a0bc89SDoug Rabson 	int error;
60327a0bc89SDoug Rabson 	u_long bn, bo, bsize, byteoffset, readcn, newc;
60427a0bc89SDoug Rabson 	struct buf *bp;
60527a0bc89SDoug Rabson 
60627a0bc89SDoug Rabson #ifdef MSDOSFS_DEBUG
607952a6212SJordan K. Hubbard 	printf("fatchain(pmp %p, start %lu, count %lu, fillwith %lx)\n",
60827a0bc89SDoug Rabson 	    pmp, start, count, fillwith);
60927a0bc89SDoug Rabson #endif
61027a0bc89SDoug Rabson 	/*
61127a0bc89SDoug Rabson 	 * Be sure the clusters are in the filesystem.
61227a0bc89SDoug Rabson 	 */
61327a0bc89SDoug Rabson 	if (start < CLUST_FIRST || start + count - 1 > pmp->pm_maxcluster)
614952a6212SJordan K. Hubbard 		return (EINVAL);
61527a0bc89SDoug Rabson 
61627a0bc89SDoug Rabson 	while (count > 0) {
61727a0bc89SDoug Rabson 		byteoffset = FATOFS(pmp, start);
61827a0bc89SDoug Rabson 		fatblock(pmp, byteoffset, &bn, &bsize, &bo);
619c3c6d51eSPoul-Henning Kamp 		error = bread(pmp->pm_devvp, bn, bsize, NOCRED, &bp);
620952a6212SJordan K. Hubbard 		if (error) {
621952a6212SJordan K. Hubbard 			brelse(bp);
622952a6212SJordan K. Hubbard 			return (error);
623952a6212SJordan K. Hubbard 		}
62427a0bc89SDoug Rabson 		while (count > 0) {
62527a0bc89SDoug Rabson 			start++;
62627a0bc89SDoug Rabson 			newc = --count > 0 ? start : fillwith;
627952a6212SJordan K. Hubbard 			switch (pmp->pm_fatmask) {
628952a6212SJordan K. Hubbard 			case FAT12_MASK:
62927a0bc89SDoug Rabson 				readcn = getushort(&bp->b_data[bo]);
63027a0bc89SDoug Rabson 				if (start & 1) {
63127a0bc89SDoug Rabson 					readcn &= 0xf000;
63227a0bc89SDoug Rabson 					readcn |= newc & 0xfff;
63327a0bc89SDoug Rabson 				} else {
63427a0bc89SDoug Rabson 					readcn &= 0x000f;
63527a0bc89SDoug Rabson 					readcn |= newc << 4;
63627a0bc89SDoug Rabson 				}
63727a0bc89SDoug Rabson 				putushort(&bp->b_data[bo], readcn);
63827a0bc89SDoug Rabson 				bo++;
63927a0bc89SDoug Rabson 				if (!(start & 1))
64027a0bc89SDoug Rabson 					bo++;
641952a6212SJordan K. Hubbard 				break;
642952a6212SJordan K. Hubbard 			case FAT16_MASK:
64327a0bc89SDoug Rabson 				putushort(&bp->b_data[bo], newc);
64427a0bc89SDoug Rabson 				bo += 2;
645952a6212SJordan K. Hubbard 				break;
646952a6212SJordan K. Hubbard 			case FAT32_MASK:
647952a6212SJordan K. Hubbard 				readcn = getulong(&bp->b_data[bo]);
648952a6212SJordan K. Hubbard 				readcn &= ~pmp->pm_fatmask;
649952a6212SJordan K. Hubbard 				readcn |= newc & pmp->pm_fatmask;
650952a6212SJordan K. Hubbard 				putulong(&bp->b_data[bo], readcn);
651952a6212SJordan K. Hubbard 				bo += 4;
652952a6212SJordan K. Hubbard 				break;
65327a0bc89SDoug Rabson 			}
65427a0bc89SDoug Rabson 			if (bo >= bsize)
65527a0bc89SDoug Rabson 				break;
65627a0bc89SDoug Rabson 		}
65727a0bc89SDoug Rabson 		updatefats(pmp, bp, bn);
65827a0bc89SDoug Rabson 	}
65927a0bc89SDoug Rabson 	pmp->pm_fmod = 1;
660952a6212SJordan K. Hubbard 	return (0);
66127a0bc89SDoug Rabson }
66227a0bc89SDoug Rabson 
66327a0bc89SDoug Rabson /*
66427a0bc89SDoug Rabson  * Check the length of a free cluster chain starting at start.
66527a0bc89SDoug Rabson  *
66627a0bc89SDoug Rabson  * pmp	 - mount point
66727a0bc89SDoug Rabson  * start - start of chain
66827a0bc89SDoug Rabson  * count - maximum interesting length
66927a0bc89SDoug Rabson  */
6707fefffeeSPoul-Henning Kamp static int
67127a0bc89SDoug Rabson chainlength(pmp, start, count)
67227a0bc89SDoug Rabson 	struct msdosfsmount *pmp;
67327a0bc89SDoug Rabson 	u_long start;
67427a0bc89SDoug Rabson 	u_long count;
67527a0bc89SDoug Rabson {
67627a0bc89SDoug Rabson 	u_long idx, max_idx;
67727a0bc89SDoug Rabson 	u_int map;
67827a0bc89SDoug Rabson 	u_long len;
67927a0bc89SDoug Rabson 
6806be1a4ccSKonstantin Belousov 	MSDOSFS_ASSERT_MP_LOCKED(pmp);
6816be1a4ccSKonstantin Belousov 
68227a0bc89SDoug Rabson 	max_idx = pmp->pm_maxcluster / N_INUSEBITS;
68327a0bc89SDoug Rabson 	idx = start / N_INUSEBITS;
68427a0bc89SDoug Rabson 	start %= N_INUSEBITS;
68527a0bc89SDoug Rabson 	map = pmp->pm_inusemap[idx];
68627a0bc89SDoug Rabson 	map &= ~((1 << start) - 1);
68727a0bc89SDoug Rabson 	if (map) {
68827a0bc89SDoug Rabson 		len = ffs(map) - 1 - start;
689952a6212SJordan K. Hubbard 		return (len > count ? count : len);
69027a0bc89SDoug Rabson 	}
69127a0bc89SDoug Rabson 	len = N_INUSEBITS - start;
69227a0bc89SDoug Rabson 	if (len >= count)
693952a6212SJordan K. Hubbard 		return (count);
69427a0bc89SDoug Rabson 	while (++idx <= max_idx) {
69527a0bc89SDoug Rabson 		if (len >= count)
69627a0bc89SDoug Rabson 			break;
697c3c6d51eSPoul-Henning Kamp 		map = pmp->pm_inusemap[idx];
698c3c6d51eSPoul-Henning Kamp 		if (map) {
69927a0bc89SDoug Rabson 			len += ffs(map) - 1;
70027a0bc89SDoug Rabson 			break;
70127a0bc89SDoug Rabson 		}
70227a0bc89SDoug Rabson 		len += N_INUSEBITS;
70327a0bc89SDoug Rabson 	}
704952a6212SJordan K. Hubbard 	return (len > count ? count : len);
70527a0bc89SDoug Rabson }
70627a0bc89SDoug Rabson 
70727a0bc89SDoug Rabson /*
70827a0bc89SDoug Rabson  * Allocate contigous free clusters.
70927a0bc89SDoug Rabson  *
71027a0bc89SDoug Rabson  * pmp	      - mount point.
71127a0bc89SDoug Rabson  * start      - start of cluster chain.
71227a0bc89SDoug Rabson  * count      - number of clusters to allocate.
71327a0bc89SDoug Rabson  * fillwith   - put this value into the fat entry for the
71427a0bc89SDoug Rabson  *		last allocated cluster.
71527a0bc89SDoug Rabson  * retcluster - put the first allocated cluster's number here.
71627a0bc89SDoug Rabson  * got	      - how many clusters were actually allocated.
71727a0bc89SDoug Rabson  */
7187fefffeeSPoul-Henning Kamp static int
71927a0bc89SDoug Rabson chainalloc(pmp, start, count, fillwith, retcluster, got)
72027a0bc89SDoug Rabson 	struct msdosfsmount *pmp;
72127a0bc89SDoug Rabson 	u_long start;
72227a0bc89SDoug Rabson 	u_long count;
72327a0bc89SDoug Rabson 	u_long fillwith;
72427a0bc89SDoug Rabson 	u_long *retcluster;
72527a0bc89SDoug Rabson 	u_long *got;
72627a0bc89SDoug Rabson {
72727a0bc89SDoug Rabson 	int error;
728952a6212SJordan K. Hubbard 	u_long cl, n;
729952a6212SJordan K. Hubbard 
7306be1a4ccSKonstantin Belousov 	MSDOSFS_ASSERT_MP_LOCKED(pmp);
7316be1a4ccSKonstantin Belousov 
732952a6212SJordan K. Hubbard 	for (cl = start, n = count; n-- > 0;)
733952a6212SJordan K. Hubbard 		usemap_alloc(pmp, cl++);
73427a0bc89SDoug Rabson 
73527a0bc89SDoug Rabson 	error = fatchain(pmp, start, count, fillwith);
736952a6212SJordan K. Hubbard 	if (error != 0)
737952a6212SJordan K. Hubbard 		return (error);
73827a0bc89SDoug Rabson #ifdef MSDOSFS_DEBUG
739952a6212SJordan K. Hubbard 	printf("clusteralloc(): allocated cluster chain at %lu (%lu clusters)\n",
74027a0bc89SDoug Rabson 	    start, count);
74127a0bc89SDoug Rabson #endif
74227a0bc89SDoug Rabson 	if (retcluster)
74327a0bc89SDoug Rabson 		*retcluster = start;
74427a0bc89SDoug Rabson 	if (got)
74527a0bc89SDoug Rabson 		*got = count;
7468e55bfafSBruce Evans 	pmp->pm_nxtfree = start + count;
7478e55bfafSBruce Evans 	if (pmp->pm_nxtfree > pmp->pm_maxcluster)
7488e55bfafSBruce Evans 		pmp->pm_nxtfree = CLUST_FIRST;
749952a6212SJordan K. Hubbard 	return (0);
75027a0bc89SDoug Rabson }
75127a0bc89SDoug Rabson 
75227a0bc89SDoug Rabson /*
75327a0bc89SDoug Rabson  * Allocate contiguous free clusters.
75427a0bc89SDoug Rabson  *
75527a0bc89SDoug Rabson  * pmp	      - mount point.
75627a0bc89SDoug Rabson  * start      - preferred start of cluster chain.
75727a0bc89SDoug Rabson  * count      - number of clusters requested.
75827a0bc89SDoug Rabson  * fillwith   - put this value into the fat entry for the
75927a0bc89SDoug Rabson  *		last allocated cluster.
76027a0bc89SDoug Rabson  * retcluster - put the first allocated cluster's number here.
76127a0bc89SDoug Rabson  * got	      - how many clusters were actually allocated.
76227a0bc89SDoug Rabson  */
76327a0bc89SDoug Rabson int
7646be1a4ccSKonstantin Belousov clusteralloc(struct msdosfsmount *pmp, u_long start, u_long count,
7656be1a4ccSKonstantin Belousov     u_long fillwith, u_long *retcluster, u_long *got)
7666be1a4ccSKonstantin Belousov {
7676be1a4ccSKonstantin Belousov 	int error;
7686be1a4ccSKonstantin Belousov 
7696be1a4ccSKonstantin Belousov 	MSDOSFS_LOCK_MP(pmp);
7706be1a4ccSKonstantin Belousov 	error = clusteralloc1(pmp, start, count, fillwith, retcluster, got);
7716be1a4ccSKonstantin Belousov 	MSDOSFS_UNLOCK_MP(pmp);
7726be1a4ccSKonstantin Belousov 	return (error);
7736be1a4ccSKonstantin Belousov }
7746be1a4ccSKonstantin Belousov 
7756be1a4ccSKonstantin Belousov static int
7766be1a4ccSKonstantin Belousov clusteralloc1(struct msdosfsmount *pmp, u_long start, u_long count,
7776be1a4ccSKonstantin Belousov     u_long fillwith, u_long *retcluster, u_long *got)
77827a0bc89SDoug Rabson {
77927a0bc89SDoug Rabson 	u_long idx;
780952a6212SJordan K. Hubbard 	u_long len, newst, foundl, cn, l;
781952a6212SJordan K. Hubbard 	u_long foundcn = 0; /* XXX: foundcn could be used unititialized */
78227a0bc89SDoug Rabson 	u_int map;
78327a0bc89SDoug Rabson 
7846be1a4ccSKonstantin Belousov 	MSDOSFS_ASSERT_MP_LOCKED(pmp);
7856be1a4ccSKonstantin Belousov 
78627a0bc89SDoug Rabson #ifdef MSDOSFS_DEBUG
787952a6212SJordan K. Hubbard 	printf("clusteralloc(): find %lu clusters\n", count);
78827a0bc89SDoug Rabson #endif
78927a0bc89SDoug Rabson 	if (start) {
79027a0bc89SDoug Rabson 		if ((len = chainlength(pmp, start, count)) >= count)
791952a6212SJordan K. Hubbard 			return (chainalloc(pmp, start, count, fillwith, retcluster, got));
7927cd5051bSPoul-Henning Kamp 	} else
79327a0bc89SDoug Rabson 		len = 0;
79427a0bc89SDoug Rabson 
7958e55bfafSBruce Evans 	newst = pmp->pm_nxtfree;
79627a0bc89SDoug Rabson 	foundl = 0;
79727a0bc89SDoug Rabson 
79827a0bc89SDoug Rabson 	for (cn = newst; cn <= pmp->pm_maxcluster;) {
79927a0bc89SDoug Rabson 		idx = cn / N_INUSEBITS;
80027a0bc89SDoug Rabson 		map = pmp->pm_inusemap[idx];
80127a0bc89SDoug Rabson 		map |= (1 << (cn % N_INUSEBITS)) - 1;
80227a0bc89SDoug Rabson 		if (map != (u_int)-1) {
80327a0bc89SDoug Rabson 			cn = idx * N_INUSEBITS + ffs(map^(u_int)-1) - 1;
80427a0bc89SDoug Rabson 			if ((l = chainlength(pmp, cn, count)) >= count)
805952a6212SJordan K. Hubbard 				return (chainalloc(pmp, cn, count, fillwith, retcluster, got));
80627a0bc89SDoug Rabson 			if (l > foundl) {
80727a0bc89SDoug Rabson 				foundcn = cn;
80827a0bc89SDoug Rabson 				foundl = l;
80927a0bc89SDoug Rabson 			}
81027a0bc89SDoug Rabson 			cn += l + 1;
81127a0bc89SDoug Rabson 			continue;
81227a0bc89SDoug Rabson 		}
81327a0bc89SDoug Rabson 		cn += N_INUSEBITS - cn % N_INUSEBITS;
81427a0bc89SDoug Rabson 	}
81527a0bc89SDoug Rabson 	for (cn = 0; cn < newst;) {
81627a0bc89SDoug Rabson 		idx = cn / N_INUSEBITS;
81727a0bc89SDoug Rabson 		map = pmp->pm_inusemap[idx];
81827a0bc89SDoug Rabson 		map |= (1 << (cn % N_INUSEBITS)) - 1;
81927a0bc89SDoug Rabson 		if (map != (u_int)-1) {
82027a0bc89SDoug Rabson 			cn = idx * N_INUSEBITS + ffs(map^(u_int)-1) - 1;
82127a0bc89SDoug Rabson 			if ((l = chainlength(pmp, cn, count)) >= count)
822952a6212SJordan K. Hubbard 				return (chainalloc(pmp, cn, count, fillwith, retcluster, got));
82327a0bc89SDoug Rabson 			if (l > foundl) {
82427a0bc89SDoug Rabson 				foundcn = cn;
82527a0bc89SDoug Rabson 				foundl = l;
82627a0bc89SDoug Rabson 			}
82727a0bc89SDoug Rabson 			cn += l + 1;
82827a0bc89SDoug Rabson 			continue;
82927a0bc89SDoug Rabson 		}
83027a0bc89SDoug Rabson 		cn += N_INUSEBITS - cn % N_INUSEBITS;
83127a0bc89SDoug Rabson 	}
83227a0bc89SDoug Rabson 
83327a0bc89SDoug Rabson 	if (!foundl)
834952a6212SJordan K. Hubbard 		return (ENOSPC);
83527a0bc89SDoug Rabson 
83627a0bc89SDoug Rabson 	if (len)
837952a6212SJordan K. Hubbard 		return (chainalloc(pmp, start, len, fillwith, retcluster, got));
83827a0bc89SDoug Rabson 	else
839952a6212SJordan K. Hubbard 		return (chainalloc(pmp, foundcn, foundl, fillwith, retcluster, got));
84027a0bc89SDoug Rabson }
84127a0bc89SDoug Rabson 
84227a0bc89SDoug Rabson 
84327a0bc89SDoug Rabson /*
84427a0bc89SDoug Rabson  * Free a chain of clusters.
84527a0bc89SDoug Rabson  *
84627a0bc89SDoug Rabson  * pmp		- address of the msdosfs mount structure for the filesystem
84727a0bc89SDoug Rabson  *		  containing the cluster chain to be freed.
84827a0bc89SDoug Rabson  * startcluster - number of the 1st cluster in the chain of clusters to be
84927a0bc89SDoug Rabson  *		  freed.
85027a0bc89SDoug Rabson  */
85127a0bc89SDoug Rabson int
85227a0bc89SDoug Rabson freeclusterchain(pmp, cluster)
85327a0bc89SDoug Rabson 	struct msdosfsmount *pmp;
85427a0bc89SDoug Rabson 	u_long cluster;
85527a0bc89SDoug Rabson {
856952a6212SJordan K. Hubbard 	int error;
85727a0bc89SDoug Rabson 	struct buf *bp = NULL;
85827a0bc89SDoug Rabson 	u_long bn, bo, bsize, byteoffset;
85927a0bc89SDoug Rabson 	u_long readcn, lbn = -1;
86027a0bc89SDoug Rabson 
8616be1a4ccSKonstantin Belousov 	MSDOSFS_LOCK_MP(pmp);
86227a0bc89SDoug Rabson 	while (cluster >= CLUST_FIRST && cluster <= pmp->pm_maxcluster) {
86327a0bc89SDoug Rabson 		byteoffset = FATOFS(pmp, cluster);
86427a0bc89SDoug Rabson 		fatblock(pmp, byteoffset, &bn, &bsize, &bo);
86527a0bc89SDoug Rabson 		if (lbn != bn) {
86627a0bc89SDoug Rabson 			if (bp)
86724d4540cSBruce Evans 				updatefats(pmp, bp, lbn);
868c3c6d51eSPoul-Henning Kamp 			error = bread(pmp->pm_devvp, bn, bsize, NOCRED, &bp);
869952a6212SJordan K. Hubbard 			if (error) {
870952a6212SJordan K. Hubbard 				brelse(bp);
8716be1a4ccSKonstantin Belousov 				MSDOSFS_UNLOCK_MP(pmp);
872952a6212SJordan K. Hubbard 				return (error);
873952a6212SJordan K. Hubbard 			}
87427a0bc89SDoug Rabson 			lbn = bn;
87527a0bc89SDoug Rabson 		}
87627a0bc89SDoug Rabson 		usemap_free(pmp, cluster);
877952a6212SJordan K. Hubbard 		switch (pmp->pm_fatmask) {
878952a6212SJordan K. Hubbard 		case FAT12_MASK:
87927a0bc89SDoug Rabson 			readcn = getushort(&bp->b_data[bo]);
88027a0bc89SDoug Rabson 			if (cluster & 1) {
88127a0bc89SDoug Rabson 				cluster = readcn >> 4;
88227a0bc89SDoug Rabson 				readcn &= 0x000f;
88327a0bc89SDoug Rabson 				readcn |= MSDOSFSFREE << 4;
88427a0bc89SDoug Rabson 			} else {
88527a0bc89SDoug Rabson 				cluster = readcn;
88627a0bc89SDoug Rabson 				readcn &= 0xf000;
88727a0bc89SDoug Rabson 				readcn |= MSDOSFSFREE & 0xfff;
88827a0bc89SDoug Rabson 			}
88927a0bc89SDoug Rabson 			putushort(&bp->b_data[bo], readcn);
890952a6212SJordan K. Hubbard 			break;
891952a6212SJordan K. Hubbard 		case FAT16_MASK:
892952a6212SJordan K. Hubbard 			cluster = getushort(&bp->b_data[bo]);
89327a0bc89SDoug Rabson 			putushort(&bp->b_data[bo], MSDOSFSFREE);
894952a6212SJordan K. Hubbard 			break;
895952a6212SJordan K. Hubbard 		case FAT32_MASK:
896952a6212SJordan K. Hubbard 			cluster = getulong(&bp->b_data[bo]);
897952a6212SJordan K. Hubbard 			putulong(&bp->b_data[bo],
898952a6212SJordan K. Hubbard 				 (MSDOSFSFREE & FAT32_MASK) | (cluster & ~FAT32_MASK));
899952a6212SJordan K. Hubbard 			break;
90027a0bc89SDoug Rabson 		}
901952a6212SJordan K. Hubbard 		cluster &= pmp->pm_fatmask;
902952a6212SJordan K. Hubbard 		if ((cluster | ~pmp->pm_fatmask) >= CLUST_RSRVD)
903952a6212SJordan K. Hubbard 			cluster |= pmp->pm_fatmask;
90427a0bc89SDoug Rabson 	}
90527a0bc89SDoug Rabson 	if (bp)
90627a0bc89SDoug Rabson 		updatefats(pmp, bp, bn);
9076be1a4ccSKonstantin Belousov 	MSDOSFS_UNLOCK_MP(pmp);
908952a6212SJordan K. Hubbard 	return (0);
90927a0bc89SDoug Rabson }
91027a0bc89SDoug Rabson 
91127a0bc89SDoug Rabson /*
91227a0bc89SDoug Rabson  * Read in fat blocks looking for free clusters. For every free cluster
91327a0bc89SDoug Rabson  * found turn off its corresponding bit in the pm_inusemap.
91427a0bc89SDoug Rabson  */
91527a0bc89SDoug Rabson int
91627a0bc89SDoug Rabson fillinusemap(pmp)
91727a0bc89SDoug Rabson 	struct msdosfsmount *pmp;
91827a0bc89SDoug Rabson {
91927a0bc89SDoug Rabson 	struct buf *bp = NULL;
92027a0bc89SDoug Rabson 	u_long cn, readcn;
92127a0bc89SDoug Rabson 	int error;
92227a0bc89SDoug Rabson 	u_long bn, bo, bsize, byteoffset;
92327a0bc89SDoug Rabson 
9246be1a4ccSKonstantin Belousov 	MSDOSFS_ASSERT_MP_LOCKED(pmp);
9256be1a4ccSKonstantin Belousov 
92627a0bc89SDoug Rabson 	/*
92727a0bc89SDoug Rabson 	 * Mark all clusters in use, we mark the free ones in the fat scan
92827a0bc89SDoug Rabson 	 * loop further down.
92927a0bc89SDoug Rabson 	 */
93027a0bc89SDoug Rabson 	for (cn = 0; cn < (pmp->pm_maxcluster + N_INUSEBITS) / N_INUSEBITS; cn++)
93127a0bc89SDoug Rabson 		pmp->pm_inusemap[cn] = (u_int)-1;
93227a0bc89SDoug Rabson 
93327a0bc89SDoug Rabson 	/*
93427a0bc89SDoug Rabson 	 * Figure how many free clusters are in the filesystem by ripping
93527a0bc89SDoug Rabson 	 * through the fat counting the number of entries whose content is
93627a0bc89SDoug Rabson 	 * zero.  These represent free clusters.
93727a0bc89SDoug Rabson 	 */
93827a0bc89SDoug Rabson 	pmp->pm_freeclustercount = 0;
93927a0bc89SDoug Rabson 	for (cn = CLUST_FIRST; cn <= pmp->pm_maxcluster; cn++) {
94027a0bc89SDoug Rabson 		byteoffset = FATOFS(pmp, cn);
94127a0bc89SDoug Rabson 		bo = byteoffset % pmp->pm_fatblocksize;
94227a0bc89SDoug Rabson 		if (!bo || !bp) {
94327a0bc89SDoug Rabson 			/* Read new FAT block */
94427a0bc89SDoug Rabson 			if (bp)
94527a0bc89SDoug Rabson 				brelse(bp);
94627a0bc89SDoug Rabson 			fatblock(pmp, byteoffset, &bn, &bsize, NULL);
94727a0bc89SDoug Rabson 			error = bread(pmp->pm_devvp, bn, bsize, NOCRED, &bp);
948952a6212SJordan K. Hubbard 			if (error) {
949952a6212SJordan K. Hubbard 				brelse(bp);
950952a6212SJordan K. Hubbard 				return (error);
95127a0bc89SDoug Rabson 			}
952952a6212SJordan K. Hubbard 		}
953952a6212SJordan K. Hubbard 		if (FAT32(pmp))
954952a6212SJordan K. Hubbard 			readcn = getulong(&bp->b_data[bo]);
955952a6212SJordan K. Hubbard 		else
95627a0bc89SDoug Rabson 			readcn = getushort(&bp->b_data[bo]);
957952a6212SJordan K. Hubbard 		if (FAT12(pmp) && (cn & 1))
95827a0bc89SDoug Rabson 			readcn >>= 4;
959952a6212SJordan K. Hubbard 		readcn &= pmp->pm_fatmask;
96027a0bc89SDoug Rabson 
96127a0bc89SDoug Rabson 		if (readcn == 0)
96227a0bc89SDoug Rabson 			usemap_free(pmp, cn);
96327a0bc89SDoug Rabson 	}
9643c8b687fSKonstantin Belousov 	if (bp != NULL)
96527a0bc89SDoug Rabson 		brelse(bp);
966952a6212SJordan K. Hubbard 	return (0);
96727a0bc89SDoug Rabson }
96827a0bc89SDoug Rabson 
96927a0bc89SDoug Rabson /*
97027a0bc89SDoug Rabson  * Allocate a new cluster and chain it onto the end of the file.
97127a0bc89SDoug Rabson  *
97227a0bc89SDoug Rabson  * dep	 - the file to extend
97327a0bc89SDoug Rabson  * count - number of clusters to allocate
97427a0bc89SDoug Rabson  * bpp	 - where to return the address of the buf header for the first new
97527a0bc89SDoug Rabson  *	   file block
97627a0bc89SDoug Rabson  * ncp	 - where to put cluster number of the first newly allocated cluster
97727a0bc89SDoug Rabson  *	   If this pointer is 0, do not return the cluster number.
97827a0bc89SDoug Rabson  * flags - see fat.h
97927a0bc89SDoug Rabson  *
98027a0bc89SDoug Rabson  * NOTE: This function is not responsible for turning on the DE_UPDATE bit of
98127a0bc89SDoug Rabson  * the de_flag field of the denode and it does not change the de_FileSize
98227a0bc89SDoug Rabson  * field.  This is left for the caller to do.
98327a0bc89SDoug Rabson  */
98427a0bc89SDoug Rabson int
98527a0bc89SDoug Rabson extendfile(dep, count, bpp, ncp, flags)
98627a0bc89SDoug Rabson 	struct denode *dep;
98727a0bc89SDoug Rabson 	u_long count;
98827a0bc89SDoug Rabson 	struct buf **bpp;
98927a0bc89SDoug Rabson 	u_long *ncp;
99027a0bc89SDoug Rabson 	int flags;
99127a0bc89SDoug Rabson {
992952a6212SJordan K. Hubbard 	int error;
99327a0bc89SDoug Rabson 	u_long frcn;
99427a0bc89SDoug Rabson 	u_long cn, got;
99527a0bc89SDoug Rabson 	struct msdosfsmount *pmp = dep->de_pmp;
99627a0bc89SDoug Rabson 	struct buf *bp;
9970d2af521SKirk McKusick 	daddr_t blkno;
99827a0bc89SDoug Rabson 
99927a0bc89SDoug Rabson 	/*
100027a0bc89SDoug Rabson 	 * Don't try to extend the root directory
100127a0bc89SDoug Rabson 	 */
1002952a6212SJordan K. Hubbard 	if (dep->de_StartCluster == MSDOSFSROOT
1003952a6212SJordan K. Hubbard 	    && (dep->de_Attributes & ATTR_DIRECTORY)) {
100454cf9198SKonstantin Belousov #ifdef MSDOSFS_DEBUG
100527a0bc89SDoug Rabson 		printf("extendfile(): attempt to extend root directory\n");
100654cf9198SKonstantin Belousov #endif
1007952a6212SJordan K. Hubbard 		return (ENOSPC);
100827a0bc89SDoug Rabson 	}
100927a0bc89SDoug Rabson 
101027a0bc89SDoug Rabson 	/*
101127a0bc89SDoug Rabson 	 * If the "file's last cluster" cache entry is empty, and the file
101227a0bc89SDoug Rabson 	 * is not empty, then fill the cache entry by calling pcbmap().
101327a0bc89SDoug Rabson 	 */
101427a0bc89SDoug Rabson 	if (dep->de_fc[FC_LASTFC].fc_frcn == FCE_EMPTY &&
101527a0bc89SDoug Rabson 	    dep->de_StartCluster != 0) {
1016952a6212SJordan K. Hubbard 		error = pcbmap(dep, 0xffff, 0, &cn, 0);
101727a0bc89SDoug Rabson 		/* we expect it to return E2BIG */
101827a0bc89SDoug Rabson 		if (error != E2BIG)
1019952a6212SJordan K. Hubbard 			return (error);
102027a0bc89SDoug Rabson 	}
102127a0bc89SDoug Rabson 
1022ededffc0STom Rhodes 	dep->de_fc[FC_NEXTTOLASTFC].fc_frcn =
1023ededffc0STom Rhodes 	    dep->de_fc[FC_LASTFC].fc_frcn;
1024ededffc0STom Rhodes 	dep->de_fc[FC_NEXTTOLASTFC].fc_fsrcn =
1025ededffc0STom Rhodes 	    dep->de_fc[FC_LASTFC].fc_fsrcn;
102627a0bc89SDoug Rabson 	while (count > 0) {
102727a0bc89SDoug Rabson 		/*
1028952a6212SJordan K. Hubbard 		 * Allocate a new cluster chain and cat onto the end of the
1029952a6212SJordan K. Hubbard 		 * file.  * If the file is empty we make de_StartCluster point
1030952a6212SJordan K. Hubbard 		 * to the new block.  Note that de_StartCluster being 0 is
1031952a6212SJordan K. Hubbard 		 * sufficient to be sure the file is empty since we exclude
1032952a6212SJordan K. Hubbard 		 * attempts to extend the root directory above, and the root
1033952a6212SJordan K. Hubbard 		 * dir is the only file with a startcluster of 0 that has
1034952a6212SJordan K. Hubbard 		 * blocks allocated (sort of).
103527a0bc89SDoug Rabson 		 */
103627a0bc89SDoug Rabson 		if (dep->de_StartCluster == 0)
103727a0bc89SDoug Rabson 			cn = 0;
103827a0bc89SDoug Rabson 		else
103927a0bc89SDoug Rabson 			cn = dep->de_fc[FC_LASTFC].fc_fsrcn + 1;
1040c3c6d51eSPoul-Henning Kamp 		error = clusteralloc(pmp, cn, count, CLUST_EOFE, &cn, &got);
1041c3c6d51eSPoul-Henning Kamp 		if (error)
1042952a6212SJordan K. Hubbard 			return (error);
104327a0bc89SDoug Rabson 
104427a0bc89SDoug Rabson 		count -= got;
104527a0bc89SDoug Rabson 
104627a0bc89SDoug Rabson 		/*
104727a0bc89SDoug Rabson 		 * Give them the filesystem relative cluster number if they want
104827a0bc89SDoug Rabson 		 * it.
104927a0bc89SDoug Rabson 		 */
105027a0bc89SDoug Rabson 		if (ncp) {
105127a0bc89SDoug Rabson 			*ncp = cn;
105227a0bc89SDoug Rabson 			ncp = NULL;
105327a0bc89SDoug Rabson 		}
105427a0bc89SDoug Rabson 
105527a0bc89SDoug Rabson 		if (dep->de_StartCluster == 0) {
105627a0bc89SDoug Rabson 			dep->de_StartCluster = cn;
105727a0bc89SDoug Rabson 			frcn = 0;
105827a0bc89SDoug Rabson 		} else {
1059952a6212SJordan K. Hubbard 			error = fatentry(FAT_SET, pmp,
1060952a6212SJordan K. Hubbard 					 dep->de_fc[FC_LASTFC].fc_fsrcn,
106127a0bc89SDoug Rabson 					 0, cn);
106227a0bc89SDoug Rabson 			if (error) {
106327a0bc89SDoug Rabson 				clusterfree(pmp, cn, NULL);
1064952a6212SJordan K. Hubbard 				return (error);
106527a0bc89SDoug Rabson 			}
106627a0bc89SDoug Rabson 			frcn = dep->de_fc[FC_LASTFC].fc_frcn + 1;
106727a0bc89SDoug Rabson 		}
106827a0bc89SDoug Rabson 
106927a0bc89SDoug Rabson 		/*
107027a0bc89SDoug Rabson 		 * Update the "last cluster of the file" entry in the denode's fat
107127a0bc89SDoug Rabson 		 * cache.
107227a0bc89SDoug Rabson 		 */
107327a0bc89SDoug Rabson 		fc_setcache(dep, FC_LASTFC, frcn + got - 1, cn + got - 1);
107427a0bc89SDoug Rabson 
107527a0bc89SDoug Rabson 		if (flags & DE_CLEAR) {
107627a0bc89SDoug Rabson 			while (got-- > 0) {
107727a0bc89SDoug Rabson 				/*
107827a0bc89SDoug Rabson 				 * Get the buf header for the new block of the file.
107927a0bc89SDoug Rabson 				 */
108027a0bc89SDoug Rabson 				if (dep->de_Attributes & ATTR_DIRECTORY)
10817261f5f6SJeff Roberson 					bp = getblk(pmp->pm_devvp,
10827261f5f6SJeff Roberson 					    cntobn(pmp, cn++),
10837261f5f6SJeff Roberson 					    pmp->pm_bpcluster, 0, 0, 0);
108427a0bc89SDoug Rabson 				else {
10857261f5f6SJeff Roberson 					bp = getblk(DETOV(dep),
108667c7bbf3SKonstantin Belousov 					    frcn++,
10877261f5f6SJeff Roberson 					    pmp->pm_bpcluster, 0, 0, 0);
108827a0bc89SDoug Rabson 					/*
108927a0bc89SDoug Rabson 					 * Do the bmap now, as in msdosfs_write
109027a0bc89SDoug Rabson 					 */
1091952a6212SJordan K. Hubbard 					if (pcbmap(dep,
109267c7bbf3SKonstantin Belousov 					    bp->b_lblkno,
10930d2af521SKirk McKusick 					    &blkno, 0, 0))
109427a0bc89SDoug Rabson 						bp->b_blkno = -1;
109527a0bc89SDoug Rabson 					if (bp->b_blkno == -1)
109627a0bc89SDoug Rabson 						panic("extendfile: pcbmap");
10970d2af521SKirk McKusick 					else
10980d2af521SKirk McKusick 						bp->b_blkno = blkno;
109927a0bc89SDoug Rabson 				}
1100d34b0a1bSBruce Evans 				vfs_bio_clrbuf(bp);
110127a0bc89SDoug Rabson 				if (bpp) {
110227a0bc89SDoug Rabson 					*bpp = bp;
110327a0bc89SDoug Rabson 					bpp = NULL;
1104952a6212SJordan K. Hubbard 				} else
1105952a6212SJordan K. Hubbard 					bdwrite(bp);
110627a0bc89SDoug Rabson 			}
110727a0bc89SDoug Rabson 		}
110827a0bc89SDoug Rabson 	}
110927a0bc89SDoug Rabson 
1110952a6212SJordan K. Hubbard 	return (0);
111127a0bc89SDoug Rabson }
1112cede1f56STom Rhodes 
1113392dbea3SBruce Evans /*-
1114392dbea3SBruce Evans  * Routine to mark a FAT16 or FAT32 volume as "clean" or "dirty" by
1115392dbea3SBruce Evans  * manipulating the upper bit of the FAT entry for cluster 1.  Note that
1116392dbea3SBruce Evans  * this bit is not defined for FAT12 volumes, which are always assumed to
1117*a26b949fSKonstantin Belousov  * be clean.
1118cede1f56STom Rhodes  *
1119392dbea3SBruce Evans  * The fatentry() routine only works on cluster numbers that a file could
1120392dbea3SBruce Evans  * occupy, so it won't manipulate the entry for cluster 1.  So we have to do
1121392dbea3SBruce Evans  * it here.  The code was stolen from fatentry() and tailored for cluster 1.
1122cede1f56STom Rhodes  *
1123cede1f56STom Rhodes  * Inputs:
1124cede1f56STom Rhodes  *	pmp	The MS-DOS volume to mark
1125392dbea3SBruce Evans  *	dirty	Non-zero if the volume should be marked dirty; zero if it
1126392dbea3SBruce Evans  *		should be marked clean
1127cede1f56STom Rhodes  *
1128cede1f56STom Rhodes  * Result:
1129cede1f56STom Rhodes  *	0	Success
1130cede1f56STom Rhodes  *	EROFS	Volume is read-only
1131cede1f56STom Rhodes  *	?	(other errors from called routines)
1132cede1f56STom Rhodes  */
1133392dbea3SBruce Evans int
1134392dbea3SBruce Evans markvoldirty(struct msdosfsmount *pmp, int dirty)
1135cede1f56STom Rhodes {
1136cede1f56STom Rhodes 	struct buf *bp;
1137392dbea3SBruce Evans 	u_long bn, bo, bsize, byteoffset, fatval;
1138392dbea3SBruce Evans 	int error;
1139cede1f56STom Rhodes 
1140392dbea3SBruce Evans 	/*
1141392dbea3SBruce Evans 	 * FAT12 does not support a "clean" bit, so don't do anything for
1142392dbea3SBruce Evans 	 * FAT12.
1143392dbea3SBruce Evans 	 */
1144cede1f56STom Rhodes 	if (FAT12(pmp))
1145392dbea3SBruce Evans 		return (0);
1146cede1f56STom Rhodes 
1147392dbea3SBruce Evans 	/* Can't change the bit on a read-only filesystem. */
1148cede1f56STom Rhodes 	if (pmp->pm_flags & MSDOSFSMNT_RONLY)
1149392dbea3SBruce Evans 		return (EROFS);
1150cede1f56STom Rhodes 
1151392dbea3SBruce Evans 	/*
1152392dbea3SBruce Evans 	 * Fetch the block containing the FAT entry.  It is given by the
1153392dbea3SBruce Evans 	 * pseudo-cluster 1.
1154392dbea3SBruce Evans 	 */
1155392dbea3SBruce Evans 	byteoffset = FATOFS(pmp, 1);
1156cede1f56STom Rhodes 	fatblock(pmp, byteoffset, &bn, &bsize, &bo);
1157cede1f56STom Rhodes 	error = bread(pmp->pm_devvp, bn, bsize, NOCRED, &bp);
1158cede1f56STom Rhodes 	if (error) {
1159cede1f56STom Rhodes 		brelse(bp);
1160cede1f56STom Rhodes 		return (error);
1161cede1f56STom Rhodes 	}
1162cede1f56STom Rhodes 
1163392dbea3SBruce Evans 	/*
1164392dbea3SBruce Evans 	 * Get the current value of the FAT entry and set/clear the relevant
1165392dbea3SBruce Evans 	 * bit.  Dirty means clear the "clean" bit; clean means set the
1166392dbea3SBruce Evans 	 * "clean" bit.
1167392dbea3SBruce Evans 	 */
1168cede1f56STom Rhodes 	if (FAT32(pmp)) {
1169392dbea3SBruce Evans 		/* FAT32 uses bit 27. */
1170cede1f56STom Rhodes 		fatval = getulong(&bp->b_data[bo]);
1171cede1f56STom Rhodes 		if (dirty)
1172392dbea3SBruce Evans 			fatval &= 0xF7FFFFFF;
1173cede1f56STom Rhodes 		else
1174392dbea3SBruce Evans 			fatval |= 0x08000000;
1175cede1f56STom Rhodes 		putulong(&bp->b_data[bo], fatval);
1176392dbea3SBruce Evans 	} else {
1177392dbea3SBruce Evans 		/* Must be FAT16; use bit 15. */
1178cede1f56STom Rhodes 		fatval = getushort(&bp->b_data[bo]);
1179cede1f56STom Rhodes 		if (dirty)
1180392dbea3SBruce Evans 			fatval &= 0x7FFF;
1181cede1f56STom Rhodes 		else
1182392dbea3SBruce Evans 			fatval |= 0x8000;
1183cede1f56STom Rhodes 		putushort(&bp->b_data[bo], fatval);
1184cede1f56STom Rhodes 	}
1185cede1f56STom Rhodes 
1186392dbea3SBruce Evans 	/* Write out the modified FAT block synchronously. */
1187392dbea3SBruce Evans 	return (bwrite(bp));
1188cede1f56STom Rhodes }
1189