xref: /freebsd/sys/fs/msdosfs/msdosfs_fat.c (revision 9287dbaae93fa66cf9c61e6e1a15c703d5eec27c)
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 
63f33d62b2SKonstantin Belousov #define	FULL_RUN	((u_int)0xffffffff)
64f33d62b2SKonstantin Belousov 
6511caded3SAlfred Perlstein static int	chainalloc(struct msdosfsmount *pmp, u_long start,
66b76d0b32SBruce Evans 		    u_long count, u_long fillwith, u_long *retcluster,
67b76d0b32SBruce Evans 		    u_long *got);
6811caded3SAlfred Perlstein static int	chainlength(struct msdosfsmount *pmp, u_long start,
6911caded3SAlfred Perlstein 		    u_long count);
70b76d0b32SBruce Evans static void	fatblock(struct msdosfsmount *pmp, u_long ofs, u_long *bnp,
71b76d0b32SBruce Evans 		    u_long *sizep, u_long *bop);
72b76d0b32SBruce Evans static int	fatchain(struct msdosfsmount *pmp, u_long start, u_long count,
73b76d0b32SBruce Evans 		    u_long fillwith);
74b76d0b32SBruce Evans static void	fc_lookup(struct denode *dep, u_long findcn, u_long *frcnp,
75b76d0b32SBruce Evans 		    u_long *fsrcnp);
7611caded3SAlfred Perlstein static void	updatefats(struct msdosfsmount *pmp, struct buf *bp,
7711caded3SAlfred Perlstein 		    u_long fatbn);
78c1087c13SBruce Evans static __inline void
7911caded3SAlfred Perlstein 		usemap_alloc(struct msdosfsmount *pmp, u_long cn);
80c1087c13SBruce Evans static __inline void
8111caded3SAlfred Perlstein 		usemap_free(struct msdosfsmount *pmp, u_long cn);
826be1a4ccSKonstantin Belousov static int	clusteralloc1(struct msdosfsmount *pmp, u_long start,
836be1a4ccSKonstantin Belousov 		    u_long count, u_long fillwith, u_long *retcluster,
846be1a4ccSKonstantin Belousov 		    u_long *got);
8558c27bcfSBruce Evans 
8627a0bc89SDoug Rabson static void
8710c9700fSEd Maste fatblock(struct msdosfsmount *pmp, u_long ofs, u_long *bnp, u_long *sizep,
8810c9700fSEd Maste     u_long *bop)
8927a0bc89SDoug Rabson {
9027a0bc89SDoug Rabson 	u_long bn, size;
9127a0bc89SDoug Rabson 
9227a0bc89SDoug Rabson 	bn = ofs / pmp->pm_fatblocksize * pmp->pm_fatblocksec;
9327a0bc89SDoug Rabson 	size = min(pmp->pm_fatblocksec, pmp->pm_FATsecs - bn)
9401f6cfbaSYoshihiro Takahashi 	    * DEV_BSIZE;
95952a6212SJordan K. Hubbard 	bn += pmp->pm_fatblk + pmp->pm_curfat * pmp->pm_FATsecs;
96952a6212SJordan K. Hubbard 
9727a0bc89SDoug Rabson 	if (bnp)
9827a0bc89SDoug Rabson 		*bnp = bn;
9927a0bc89SDoug Rabson 	if (sizep)
10027a0bc89SDoug Rabson 		*sizep = size;
10127a0bc89SDoug Rabson 	if (bop)
10227a0bc89SDoug Rabson 		*bop = ofs % pmp->pm_fatblocksize;
10327a0bc89SDoug Rabson }
10427a0bc89SDoug Rabson 
10527a0bc89SDoug Rabson /*
10627a0bc89SDoug Rabson  * Map the logical cluster number of a file into a physical disk sector
10727a0bc89SDoug Rabson  * that is filesystem relative.
10827a0bc89SDoug Rabson  *
10927a0bc89SDoug Rabson  * dep	  - address of denode representing the file of interest
11027a0bc89SDoug Rabson  * findcn - file relative cluster whose filesystem relative cluster number
11127a0bc89SDoug Rabson  *	    and/or block number are/is to be found
11227a0bc89SDoug Rabson  * bnp	  - address of where to place the filesystem relative block number.
11327a0bc89SDoug Rabson  *	    If this pointer is null then don't return this quantity.
11427a0bc89SDoug Rabson  * cnp	  - address of where to place the filesystem relative cluster number.
11527a0bc89SDoug Rabson  *	    If this pointer is null then don't return this quantity.
1164882501bSEd Maste  * sp     - pointer to returned block size
11727a0bc89SDoug Rabson  *
11827a0bc89SDoug Rabson  * NOTE: Either bnp or cnp must be non-null.
11927a0bc89SDoug Rabson  * This function has one side effect.  If the requested file relative cluster
12027a0bc89SDoug Rabson  * is beyond the end of file, then the actual number of clusters in the file
12127a0bc89SDoug Rabson  * is returned in *cnp.  This is useful for determining how long a directory is.
12227a0bc89SDoug Rabson  *  If cnp is null, nothing is returned.
12327a0bc89SDoug Rabson  */
12427a0bc89SDoug Rabson int
12510c9700fSEd Maste pcbmap(struct denode *dep, u_long findcn, daddr_t *bnp, u_long *cnp, int *sp)
12627a0bc89SDoug Rabson {
12727a0bc89SDoug Rabson 	int error;
12827a0bc89SDoug Rabson 	u_long i;
12927a0bc89SDoug Rabson 	u_long cn;
130952a6212SJordan K. Hubbard 	u_long prevcn = 0; /* XXX: prevcn could be used unititialized */
13127a0bc89SDoug Rabson 	u_long byteoffset;
13227a0bc89SDoug Rabson 	u_long bn;
13327a0bc89SDoug Rabson 	u_long bo;
13427a0bc89SDoug Rabson 	struct buf *bp = NULL;
13527a0bc89SDoug Rabson 	u_long bp_bn = -1;
13627a0bc89SDoug Rabson 	struct msdosfsmount *pmp = dep->de_pmp;
13727a0bc89SDoug Rabson 	u_long bsize;
13827a0bc89SDoug Rabson 
139ef6a2be3SKonstantin Belousov 	KASSERT(bnp != NULL || cnp != NULL || sp != NULL,
140ef6a2be3SKonstantin Belousov 	    ("pcbmap: extra call"));
141ef6a2be3SKonstantin Belousov 	ASSERT_VOP_ELOCKED(DETOV(dep), "pcbmap");
14227a0bc89SDoug Rabson 
14327a0bc89SDoug Rabson 	cn = dep->de_StartCluster;
14427a0bc89SDoug Rabson 	/*
14527a0bc89SDoug Rabson 	 * The "file" that makes up the root directory is contiguous,
14627a0bc89SDoug Rabson 	 * permanently allocated, of fixed size, and is not made up of
14727a0bc89SDoug Rabson 	 * clusters.  If the cluster number is beyond the end of the root
14827a0bc89SDoug Rabson 	 * directory, then return the number of clusters in the file.
14927a0bc89SDoug Rabson 	 */
15027a0bc89SDoug Rabson 	if (cn == MSDOSFSROOT) {
15127a0bc89SDoug Rabson 		if (dep->de_Attributes & ATTR_DIRECTORY) {
152952a6212SJordan K. Hubbard 			if (de_cn2off(pmp, findcn) >= dep->de_FileSize) {
15327a0bc89SDoug Rabson 				if (cnp)
154952a6212SJordan K. Hubbard 					*cnp = de_bn2cn(pmp, pmp->pm_rootdirsize);
155952a6212SJordan K. Hubbard 				return (E2BIG);
15627a0bc89SDoug Rabson 			}
15727a0bc89SDoug Rabson 			if (bnp)
158952a6212SJordan K. Hubbard 				*bnp = pmp->pm_rootdirblk + de_cn2bn(pmp, findcn);
15927a0bc89SDoug Rabson 			if (cnp)
16027a0bc89SDoug Rabson 				*cnp = MSDOSFSROOT;
161952a6212SJordan K. Hubbard 			if (sp)
162952a6212SJordan K. Hubbard 				*sp = min(pmp->pm_bpcluster,
163952a6212SJordan K. Hubbard 				    dep->de_FileSize - de_cn2off(pmp, findcn));
164952a6212SJordan K. Hubbard 			return (0);
16527a0bc89SDoug Rabson 		} else {		/* just an empty file */
16627a0bc89SDoug Rabson 			if (cnp)
16727a0bc89SDoug Rabson 				*cnp = 0;
168952a6212SJordan K. Hubbard 			return (E2BIG);
16927a0bc89SDoug Rabson 		}
17027a0bc89SDoug Rabson 	}
17127a0bc89SDoug Rabson 
17227a0bc89SDoug Rabson 	/*
173952a6212SJordan K. Hubbard 	 * All other files do I/O in cluster sized blocks
174952a6212SJordan K. Hubbard 	 */
175952a6212SJordan K. Hubbard 	if (sp)
176952a6212SJordan K. Hubbard 		*sp = pmp->pm_bpcluster;
177952a6212SJordan K. Hubbard 
178952a6212SJordan K. Hubbard 	/*
179*9287dbaaSEd Maste 	 * Rummage around in the FAT cache, maybe we can avoid tromping
180*9287dbaaSEd Maste 	 * through every FAT entry for the file. And, keep track of how far
18127a0bc89SDoug Rabson 	 * off the cache was from where we wanted to be.
18227a0bc89SDoug Rabson 	 */
18327a0bc89SDoug Rabson 	i = 0;
18427a0bc89SDoug Rabson 	fc_lookup(dep, findcn, &i, &cn);
18527a0bc89SDoug Rabson 
18627a0bc89SDoug Rabson 	/*
18727a0bc89SDoug Rabson 	 * Handle all other files or directories the normal way.
18827a0bc89SDoug Rabson 	 */
18927a0bc89SDoug Rabson 	for (; i < findcn; i++) {
190952a6212SJordan K. Hubbard 		/*
191952a6212SJordan K. Hubbard 		 * Stop with all reserved clusters, not just with EOF.
192952a6212SJordan K. Hubbard 		 */
193952a6212SJordan K. Hubbard 		if ((cn | ~pmp->pm_fatmask) >= CLUST_RSRVD)
19427a0bc89SDoug Rabson 			goto hiteof;
19527a0bc89SDoug Rabson 		byteoffset = FATOFS(pmp, cn);
19627a0bc89SDoug Rabson 		fatblock(pmp, byteoffset, &bn, &bsize, &bo);
19727a0bc89SDoug Rabson 		if (bn != bp_bn) {
19827a0bc89SDoug Rabson 			if (bp)
19927a0bc89SDoug Rabson 				brelse(bp);
20027a0bc89SDoug Rabson 			error = bread(pmp->pm_devvp, bn, bsize, NOCRED, &bp);
201952a6212SJordan K. Hubbard 			if (error) {
202952a6212SJordan K. Hubbard 				brelse(bp);
203952a6212SJordan K. Hubbard 				return (error);
204952a6212SJordan K. Hubbard 			}
20527a0bc89SDoug Rabson 			bp_bn = bn;
20627a0bc89SDoug Rabson 		}
20727a0bc89SDoug Rabson 		prevcn = cn;
208d23af19aSTim J. Robbins 		if (bo >= bsize) {
209d23af19aSTim J. Robbins 			if (bp)
210d23af19aSTim J. Robbins 				brelse(bp);
211d23af19aSTim J. Robbins 			return (EIO);
212d23af19aSTim J. Robbins 		}
213952a6212SJordan K. Hubbard 		if (FAT32(pmp))
214952a6212SJordan K. Hubbard 			cn = getulong(&bp->b_data[bo]);
215952a6212SJordan K. Hubbard 		else
21627a0bc89SDoug Rabson 			cn = getushort(&bp->b_data[bo]);
217952a6212SJordan K. Hubbard 		if (FAT12(pmp) && (prevcn & 1))
21827a0bc89SDoug Rabson 			cn >>= 4;
219952a6212SJordan K. Hubbard 		cn &= pmp->pm_fatmask;
220952a6212SJordan K. Hubbard 
22127a0bc89SDoug Rabson 		/*
222952a6212SJordan K. Hubbard 		 * Force the special cluster numbers
223952a6212SJordan K. Hubbard 		 * to be the same for all cluster sizes
224952a6212SJordan K. Hubbard 		 * to let the rest of msdosfs handle
225952a6212SJordan K. Hubbard 		 * all cases the same.
22627a0bc89SDoug Rabson 		 */
227952a6212SJordan K. Hubbard 		if ((cn | ~pmp->pm_fatmask) >= CLUST_RSRVD)
228952a6212SJordan K. Hubbard 			cn |= ~pmp->pm_fatmask;
22927a0bc89SDoug Rabson 	}
23027a0bc89SDoug Rabson 
231952a6212SJordan K. Hubbard 	if (!MSDOSFSEOF(pmp, cn)) {
23227a0bc89SDoug Rabson 		if (bp)
23327a0bc89SDoug Rabson 			brelse(bp);
23427a0bc89SDoug Rabson 		if (bnp)
23527a0bc89SDoug Rabson 			*bnp = cntobn(pmp, cn);
23627a0bc89SDoug Rabson 		if (cnp)
23727a0bc89SDoug Rabson 			*cnp = cn;
23827a0bc89SDoug Rabson 		fc_setcache(dep, FC_LASTMAP, i, cn);
239952a6212SJordan K. Hubbard 		return (0);
24027a0bc89SDoug Rabson 	}
24127a0bc89SDoug Rabson 
24227a0bc89SDoug Rabson hiteof:;
24327a0bc89SDoug Rabson 	if (cnp)
24427a0bc89SDoug Rabson 		*cnp = i;
24527a0bc89SDoug Rabson 	if (bp)
24627a0bc89SDoug Rabson 		brelse(bp);
247*9287dbaaSEd Maste 	/* update last file cluster entry in the FAT cache */
24827a0bc89SDoug Rabson 	fc_setcache(dep, FC_LASTFC, i - 1, prevcn);
249952a6212SJordan K. Hubbard 	return (E2BIG);
25027a0bc89SDoug Rabson }
25127a0bc89SDoug Rabson 
25227a0bc89SDoug Rabson /*
253*9287dbaaSEd Maste  * Find the closest entry in the FAT cache to the cluster we are looking
25427a0bc89SDoug Rabson  * for.
25527a0bc89SDoug Rabson  */
2567fefffeeSPoul-Henning Kamp static void
25710c9700fSEd Maste fc_lookup(struct denode *dep, u_long findcn, u_long *frcnp, u_long *fsrcnp)
25827a0bc89SDoug Rabson {
25927a0bc89SDoug Rabson 	int i;
26027a0bc89SDoug Rabson 	u_long cn;
2610d3e502fSPedro F. Giffuni 	struct fatcache *closest = NULL;
26227a0bc89SDoug Rabson 
263ef6a2be3SKonstantin Belousov 	ASSERT_VOP_LOCKED(DETOV(dep), "fc_lookup");
264ef6a2be3SKonstantin Belousov 
26527a0bc89SDoug Rabson 	for (i = 0; i < FC_SIZE; i++) {
26627a0bc89SDoug Rabson 		cn = dep->de_fc[i].fc_frcn;
26727a0bc89SDoug Rabson 		if (cn != FCE_EMPTY && cn <= findcn) {
2680d3e502fSPedro F. Giffuni 			if (closest == NULL || cn > closest->fc_frcn)
26927a0bc89SDoug Rabson 				closest = &dep->de_fc[i];
27027a0bc89SDoug Rabson 		}
27127a0bc89SDoug Rabson 	}
27227a0bc89SDoug Rabson 	if (closest) {
27327a0bc89SDoug Rabson 		*frcnp = closest->fc_frcn;
27427a0bc89SDoug Rabson 		*fsrcnp = closest->fc_fsrcn;
27527a0bc89SDoug Rabson 	}
27627a0bc89SDoug Rabson }
27727a0bc89SDoug Rabson 
27827a0bc89SDoug Rabson /*
279*9287dbaaSEd Maste  * Purge the FAT cache in denode dep of all entries relating to file
28027a0bc89SDoug Rabson  * relative cluster frcn and beyond.
28127a0bc89SDoug Rabson  */
282952a6212SJordan K. Hubbard void
28310c9700fSEd Maste fc_purge(struct denode *dep, u_int frcn)
28427a0bc89SDoug Rabson {
28527a0bc89SDoug Rabson 	int i;
28627a0bc89SDoug Rabson 	struct fatcache *fcp;
28727a0bc89SDoug Rabson 
288ef6a2be3SKonstantin Belousov 	ASSERT_VOP_ELOCKED(DETOV(dep), "fc_purge");
289ef6a2be3SKonstantin Belousov 
29027a0bc89SDoug Rabson 	fcp = dep->de_fc;
29127a0bc89SDoug Rabson 	for (i = 0; i < FC_SIZE; i++, fcp++) {
29227a0bc89SDoug Rabson 		if (fcp->fc_frcn >= frcn)
29327a0bc89SDoug Rabson 			fcp->fc_frcn = FCE_EMPTY;
29427a0bc89SDoug Rabson 	}
29527a0bc89SDoug Rabson }
29627a0bc89SDoug Rabson 
29727a0bc89SDoug Rabson /*
298*9287dbaaSEd Maste  * Update the FAT.
299*9287dbaaSEd Maste  * If mirroring the FAT, update all copies, with the first copy as last.
300*9287dbaaSEd Maste  * Else update only the current FAT (ignoring the others).
30127a0bc89SDoug Rabson  *
30227a0bc89SDoug Rabson  * pmp	 - msdosfsmount structure for filesystem to update
303*9287dbaaSEd Maste  * bp	 - addr of modified FAT block
304*9287dbaaSEd Maste  * fatbn - block number relative to begin of filesystem of the modified FAT block.
30527a0bc89SDoug Rabson  */
306a98ca469SPoul-Henning Kamp static void
30710c9700fSEd Maste updatefats(struct msdosfsmount *pmp, struct buf *bp, u_long fatbn)
30827a0bc89SDoug Rabson {
30927a0bc89SDoug Rabson 	struct buf *bpn;
31079fb7dd1SKonstantin Belousov 	int cleanfat, i;
31127a0bc89SDoug Rabson 
31227a0bc89SDoug Rabson #ifdef MSDOSFS_DEBUG
313952a6212SJordan K. Hubbard 	printf("updatefats(pmp %p, bp %p, fatbn %lu)\n", pmp, bp, fatbn);
31427a0bc89SDoug Rabson #endif
31527a0bc89SDoug Rabson 
316952a6212SJordan K. Hubbard 	if (pmp->pm_flags & MSDOSFS_FATMIRROR) {
317952a6212SJordan K. Hubbard 		/*
318*9287dbaaSEd Maste 		 * Now copy the block(s) of the modified FAT to the other copies of
319*9287dbaaSEd Maste 		 * the FAT and write them out.  This is faster than reading in the
320*9287dbaaSEd Maste 		 * other FATs and then writing them back out.  This could tie up
321*9287dbaaSEd Maste 		 * the FAT for quite a while. Preventing others from accessing it.
322*9287dbaaSEd Maste 		 * To prevent us from going after the FAT quite so much we use
323*9287dbaaSEd Maste 		 * delayed writes, unless they specified "synchronous" when the
32427a0bc89SDoug Rabson 		 * filesystem was mounted.  If synch is asked for then use
32527a0bc89SDoug Rabson 		 * bwrite()'s and really slow things down.
32627a0bc89SDoug Rabson 		 */
32779fb7dd1SKonstantin Belousov 		if (fatbn != pmp->pm_fatblk || FAT12(pmp))
32879fb7dd1SKonstantin Belousov 			cleanfat = 0;
32979fb7dd1SKonstantin Belousov 		else if (FAT16(pmp))
33079fb7dd1SKonstantin Belousov 			cleanfat = 16;
33179fb7dd1SKonstantin Belousov 		else
33279fb7dd1SKonstantin Belousov 			cleanfat = 32;
33327a0bc89SDoug Rabson 		for (i = 1; i < pmp->pm_FATs; i++) {
33427a0bc89SDoug Rabson 			fatbn += pmp->pm_FATsecs;
33527a0bc89SDoug Rabson 			/* getblk() never fails */
3367261f5f6SJeff Roberson 			bpn = getblk(pmp->pm_devvp, fatbn, bp->b_bcount,
3377261f5f6SJeff Roberson 			    0, 0, 0);
33827a0bc89SDoug Rabson 			bcopy(bp->b_data, bpn->b_data, bp->b_bcount);
33979fb7dd1SKonstantin Belousov 			/* Force the clean bit on in the other copies. */
34079fb7dd1SKonstantin Belousov 			if (cleanfat == 16)
34123c53312SEd Maste 				((uint8_t *)bpn->b_data)[3] |= 0x80;
34279fb7dd1SKonstantin Belousov 			else if (cleanfat == 32)
34323c53312SEd Maste 				((uint8_t *)bpn->b_data)[7] |= 0x08;
34411fca81cSKonstantin Belousov 			if (pmp->pm_mountp->mnt_flag & MNT_SYNCHRONOUS)
34527a0bc89SDoug Rabson 				bwrite(bpn);
34627a0bc89SDoug Rabson 			else
34727a0bc89SDoug Rabson 				bdwrite(bpn);
34827a0bc89SDoug Rabson 		}
349952a6212SJordan K. Hubbard 	}
350952a6212SJordan K. Hubbard 
35127a0bc89SDoug Rabson 	/*
352*9287dbaaSEd Maste 	 * Write out the first (or current) FAT last.
35327a0bc89SDoug Rabson 	 */
35411fca81cSKonstantin Belousov 	if (pmp->pm_mountp->mnt_flag & MNT_SYNCHRONOUS)
35527a0bc89SDoug Rabson 		bwrite(bp);
35627a0bc89SDoug Rabson 	else
35727a0bc89SDoug Rabson 		bdwrite(bp);
35827a0bc89SDoug Rabson }
35927a0bc89SDoug Rabson 
36027a0bc89SDoug Rabson /*
361*9287dbaaSEd Maste  * Updating entries in 12 bit FATs is a pain in the butt.
36227a0bc89SDoug Rabson  *
36327a0bc89SDoug Rabson  * The following picture shows where nibbles go when moving from a 12 bit
36427a0bc89SDoug Rabson  * cluster number into the appropriate bytes in the FAT.
36527a0bc89SDoug Rabson  *
36627a0bc89SDoug Rabson  *	byte m        byte m+1      byte m+2
36727a0bc89SDoug Rabson  *	+----+----+   +----+----+   +----+----+
36827a0bc89SDoug Rabson  *	|  0    1 |   |  2    3 |   |  4    5 |   FAT bytes
36927a0bc89SDoug Rabson  *	+----+----+   +----+----+   +----+----+
37027a0bc89SDoug Rabson  *
37127a0bc89SDoug Rabson  *	+----+----+----+   +----+----+----+
37227a0bc89SDoug Rabson  *	|  3    0    1 |   |  4    5    2 |
37327a0bc89SDoug Rabson  *	+----+----+----+   +----+----+----+
37427a0bc89SDoug Rabson  *	cluster n  	   cluster n+1
37527a0bc89SDoug Rabson  *
37627a0bc89SDoug Rabson  * Where n is even. m = n + (n >> 2)
37727a0bc89SDoug Rabson  *
37827a0bc89SDoug Rabson  */
379c1087c13SBruce Evans static __inline void
38010c9700fSEd Maste usemap_alloc(struct msdosfsmount *pmp, u_long cn)
38127a0bc89SDoug Rabson {
382952a6212SJordan K. Hubbard 
3836be1a4ccSKonstantin Belousov 	MSDOSFS_ASSERT_MP_LOCKED(pmp);
384eb739c7cSKonstantin Belousov 
385b05088aeSKonstantin Belousov 	KASSERT(cn <= pmp->pm_maxcluster, ("cn too large %lu %lu", cn,
386b05088aeSKonstantin Belousov 	    pmp->pm_maxcluster));
387420d65d9SKonstantin Belousov 	KASSERT((pmp->pm_flags & MSDOSFSMNT_RONLY) == 0,
388420d65d9SKonstantin Belousov 	    ("usemap_alloc on ro msdosfs mount"));
389eb739c7cSKonstantin Belousov 	KASSERT((pmp->pm_inusemap[cn / N_INUSEBITS] & (1 << (cn % N_INUSEBITS)))
390eb739c7cSKonstantin Belousov 	    == 0, ("Allocating used sector %ld %ld %x", cn, cn % N_INUSEBITS,
391eb739c7cSKonstantin Belousov 		(unsigned)pmp->pm_inusemap[cn / N_INUSEBITS]));
392952a6212SJordan K. Hubbard 	pmp->pm_inusemap[cn / N_INUSEBITS] |= 1 << (cn % N_INUSEBITS);
393eb739c7cSKonstantin Belousov 	KASSERT(pmp->pm_freeclustercount > 0, ("usemap_alloc: too little"));
39427a0bc89SDoug Rabson 	pmp->pm_freeclustercount--;
395bb7ca822SKonstantin Belousov 	pmp->pm_flags |= MSDOSFS_FSIMOD;
39627a0bc89SDoug Rabson }
39727a0bc89SDoug Rabson 
398c1087c13SBruce Evans static __inline void
39910c9700fSEd Maste usemap_free(struct msdosfsmount *pmp, u_long cn)
40027a0bc89SDoug Rabson {
401952a6212SJordan K. Hubbard 
4026be1a4ccSKonstantin Belousov 	MSDOSFS_ASSERT_MP_LOCKED(pmp);
403b05088aeSKonstantin Belousov 
404b05088aeSKonstantin Belousov 	KASSERT(cn <= pmp->pm_maxcluster, ("cn too large %lu %lu", cn,
405b05088aeSKonstantin Belousov 	    pmp->pm_maxcluster));
406420d65d9SKonstantin Belousov 	KASSERT((pmp->pm_flags & MSDOSFSMNT_RONLY) == 0,
407420d65d9SKonstantin Belousov 	    ("usemap_free on ro msdosfs mount"));
40827a0bc89SDoug Rabson 	pmp->pm_freeclustercount++;
409bb7ca822SKonstantin Belousov 	pmp->pm_flags |= MSDOSFS_FSIMOD;
410eb739c7cSKonstantin Belousov 	KASSERT((pmp->pm_inusemap[cn / N_INUSEBITS] & (1 << (cn % N_INUSEBITS)))
411eb739c7cSKonstantin Belousov 	    != 0, ("Freeing unused sector %ld %ld %x", cn, cn % N_INUSEBITS,
412eb739c7cSKonstantin Belousov 		(unsigned)pmp->pm_inusemap[cn / N_INUSEBITS]));
41327a0bc89SDoug Rabson 	pmp->pm_inusemap[cn / N_INUSEBITS] &= ~(1 << (cn % N_INUSEBITS));
41427a0bc89SDoug Rabson }
41527a0bc89SDoug Rabson 
41627a0bc89SDoug Rabson int
41710c9700fSEd Maste clusterfree(struct msdosfsmount *pmp, u_long cluster, u_long *oldcnp)
41827a0bc89SDoug Rabson {
41927a0bc89SDoug Rabson 	int error;
42027a0bc89SDoug Rabson 	u_long oldcn;
42127a0bc89SDoug Rabson 
42227a0bc89SDoug Rabson 	error = fatentry(FAT_GET_AND_SET, pmp, cluster, &oldcn, MSDOSFSFREE);
4236be1a4ccSKonstantin Belousov 	if (error)
424952a6212SJordan K. Hubbard 		return (error);
42527a0bc89SDoug Rabson 	/*
42627a0bc89SDoug Rabson 	 * If the cluster was successfully marked free, then update
42727a0bc89SDoug Rabson 	 * the count of free clusters, and turn off the "allocated"
42827a0bc89SDoug Rabson 	 * bit in the "in use" cluster bit map.
42927a0bc89SDoug Rabson 	 */
4306be1a4ccSKonstantin Belousov 	MSDOSFS_LOCK_MP(pmp);
4316be1a4ccSKonstantin Belousov 	usemap_free(pmp, cluster);
4326be1a4ccSKonstantin Belousov 	MSDOSFS_UNLOCK_MP(pmp);
43327a0bc89SDoug Rabson 	if (oldcnp)
43427a0bc89SDoug Rabson 		*oldcnp = oldcn;
435952a6212SJordan K. Hubbard 	return (0);
43627a0bc89SDoug Rabson }
43727a0bc89SDoug Rabson 
43827a0bc89SDoug Rabson /*
439*9287dbaaSEd Maste  * Get or Set or 'Get and Set' the cluster'th entry in the FAT.
44027a0bc89SDoug Rabson  *
441*9287dbaaSEd Maste  * function	- whether to get or set a FAT entry
44227a0bc89SDoug Rabson  * pmp		- address of the msdosfsmount structure for the filesystem
443*9287dbaaSEd Maste  *		  whose FAT is to be manipulated.
44427a0bc89SDoug Rabson  * cn		- which cluster is of interest
44527a0bc89SDoug Rabson  * oldcontents	- address of a word that is to receive the contents of the
44627a0bc89SDoug Rabson  *		  cluster'th entry if this is a get function
44727a0bc89SDoug Rabson  * newcontents	- the new value to be written into the cluster'th element of
448*9287dbaaSEd Maste  *		  the FAT if this is a set function.
44927a0bc89SDoug Rabson  *
450*9287dbaaSEd Maste  * This function can also be used to free a cluster by setting the FAT entry
45127a0bc89SDoug Rabson  * for a cluster to 0.
45227a0bc89SDoug Rabson  *
453*9287dbaaSEd Maste  * All copies of the FAT are updated if this is a set function. NOTE: If
45427a0bc89SDoug Rabson  * fatentry() marks a cluster as free it does not update the inusemap in
45527a0bc89SDoug Rabson  * the msdosfsmount structure. This is left to the caller.
45627a0bc89SDoug Rabson  */
45727a0bc89SDoug Rabson int
4584882501bSEd Maste fatentry(int function, struct msdosfsmount *pmp, u_long cn, u_long *oldcontents,
4594882501bSEd Maste     u_long newcontents)
46027a0bc89SDoug Rabson {
46127a0bc89SDoug Rabson 	int error;
46227a0bc89SDoug Rabson 	u_long readcn;
46327a0bc89SDoug Rabson 	u_long bn, bo, bsize, byteoffset;
46427a0bc89SDoug Rabson 	struct buf *bp;
46527a0bc89SDoug Rabson 
466952a6212SJordan K. Hubbard #ifdef	MSDOSFS_DEBUG
467952a6212SJordan K. Hubbard 	printf("fatentry(func %d, pmp %p, clust %lu, oldcon %p, newcon %lx)\n",
468952a6212SJordan K. Hubbard 	    function, pmp, cn, oldcontents, newcontents);
469952a6212SJordan K. Hubbard #endif
47027a0bc89SDoug Rabson 
47127a0bc89SDoug Rabson #ifdef DIAGNOSTIC
47227a0bc89SDoug Rabson 	/*
47327a0bc89SDoug Rabson 	 * Be sure they asked us to do something.
47427a0bc89SDoug Rabson 	 */
47527a0bc89SDoug Rabson 	if ((function & (FAT_SET | FAT_GET)) == 0) {
47654cf9198SKonstantin Belousov #ifdef MSDOSFS_DEBUG
47727a0bc89SDoug Rabson 		printf("fatentry(): function code doesn't specify get or set\n");
47854cf9198SKonstantin Belousov #endif
479952a6212SJordan K. Hubbard 		return (EINVAL);
48027a0bc89SDoug Rabson 	}
48127a0bc89SDoug Rabson 
48227a0bc89SDoug Rabson 	/*
48327a0bc89SDoug Rabson 	 * If they asked us to return a cluster number but didn't tell us
48427a0bc89SDoug Rabson 	 * where to put it, give them an error.
48527a0bc89SDoug Rabson 	 */
48627a0bc89SDoug Rabson 	if ((function & FAT_GET) && oldcontents == NULL) {
48754cf9198SKonstantin Belousov #ifdef MSDOSFS_DEBUG
48827a0bc89SDoug Rabson 		printf("fatentry(): get function with no place to put result\n");
48954cf9198SKonstantin Belousov #endif
490952a6212SJordan K. Hubbard 		return (EINVAL);
49127a0bc89SDoug Rabson 	}
49227a0bc89SDoug Rabson #endif
49327a0bc89SDoug Rabson 
49427a0bc89SDoug Rabson 	/*
49527a0bc89SDoug Rabson 	 * Be sure the requested cluster is in the filesystem.
49627a0bc89SDoug Rabson 	 */
49727a0bc89SDoug Rabson 	if (cn < CLUST_FIRST || cn > pmp->pm_maxcluster)
498952a6212SJordan K. Hubbard 		return (EINVAL);
49927a0bc89SDoug Rabson 
50027a0bc89SDoug Rabson 	byteoffset = FATOFS(pmp, cn);
50127a0bc89SDoug Rabson 	fatblock(pmp, byteoffset, &bn, &bsize, &bo);
502c3c6d51eSPoul-Henning Kamp 	error = bread(pmp->pm_devvp, bn, bsize, NOCRED, &bp);
503952a6212SJordan K. Hubbard 	if (error) {
504952a6212SJordan K. Hubbard 		brelse(bp);
505952a6212SJordan K. Hubbard 		return (error);
506952a6212SJordan K. Hubbard 	}
50727a0bc89SDoug Rabson 
50827a0bc89SDoug Rabson 	if (function & FAT_GET) {
509952a6212SJordan K. Hubbard 		if (FAT32(pmp))
510952a6212SJordan K. Hubbard 			readcn = getulong(&bp->b_data[bo]);
511952a6212SJordan K. Hubbard 		else
51227a0bc89SDoug Rabson 			readcn = getushort(&bp->b_data[bo]);
513952a6212SJordan K. Hubbard 		if (FAT12(pmp) & (cn & 1))
51427a0bc89SDoug Rabson 			readcn >>= 4;
515952a6212SJordan K. Hubbard 		readcn &= pmp->pm_fatmask;
516*9287dbaaSEd Maste 		/* map reserved FAT entries to same values for all FATs */
517952a6212SJordan K. Hubbard 		if ((readcn | ~pmp->pm_fatmask) >= CLUST_RSRVD)
518952a6212SJordan K. Hubbard 			readcn |= ~pmp->pm_fatmask;
51927a0bc89SDoug Rabson 		*oldcontents = readcn;
52027a0bc89SDoug Rabson 	}
52127a0bc89SDoug Rabson 	if (function & FAT_SET) {
522952a6212SJordan K. Hubbard 		switch (pmp->pm_fatmask) {
523952a6212SJordan K. Hubbard 		case FAT12_MASK:
52427a0bc89SDoug Rabson 			readcn = getushort(&bp->b_data[bo]);
52527a0bc89SDoug Rabson 			if (cn & 1) {
52627a0bc89SDoug Rabson 				readcn &= 0x000f;
52727a0bc89SDoug Rabson 				readcn |= newcontents << 4;
52827a0bc89SDoug Rabson 			} else {
52927a0bc89SDoug Rabson 				readcn &= 0xf000;
53027a0bc89SDoug Rabson 				readcn |= newcontents & 0xfff;
53127a0bc89SDoug Rabson 			}
53227a0bc89SDoug Rabson 			putushort(&bp->b_data[bo], readcn);
533952a6212SJordan K. Hubbard 			break;
534952a6212SJordan K. Hubbard 		case FAT16_MASK:
53527a0bc89SDoug Rabson 			putushort(&bp->b_data[bo], newcontents);
536952a6212SJordan K. Hubbard 			break;
537952a6212SJordan K. Hubbard 		case FAT32_MASK:
538952a6212SJordan K. Hubbard 			/*
539952a6212SJordan K. Hubbard 			 * According to spec we have to retain the
540*9287dbaaSEd Maste 			 * high order bits of the FAT entry.
541952a6212SJordan K. Hubbard 			 */
542952a6212SJordan K. Hubbard 			readcn = getulong(&bp->b_data[bo]);
543952a6212SJordan K. Hubbard 			readcn &= ~FAT32_MASK;
544952a6212SJordan K. Hubbard 			readcn |= newcontents & FAT32_MASK;
545952a6212SJordan K. Hubbard 			putulong(&bp->b_data[bo], readcn);
546952a6212SJordan K. Hubbard 			break;
547952a6212SJordan K. Hubbard 		}
54827a0bc89SDoug Rabson 		updatefats(pmp, bp, bn);
54927a0bc89SDoug Rabson 		bp = NULL;
55027a0bc89SDoug Rabson 		pmp->pm_fmod = 1;
55127a0bc89SDoug Rabson 	}
55227a0bc89SDoug Rabson 	if (bp)
55327a0bc89SDoug Rabson 		brelse(bp);
554952a6212SJordan K. Hubbard 	return (0);
55527a0bc89SDoug Rabson }
55627a0bc89SDoug Rabson 
55727a0bc89SDoug Rabson /*
55827a0bc89SDoug Rabson  * Update a contiguous cluster chain
55927a0bc89SDoug Rabson  *
56027a0bc89SDoug Rabson  * pmp	    - mount point
56127a0bc89SDoug Rabson  * start    - first cluster of chain
56227a0bc89SDoug Rabson  * count    - number of clusters in chain
563*9287dbaaSEd Maste  * fillwith - what to write into FAT entry of last cluster
56427a0bc89SDoug Rabson  */
56527a0bc89SDoug Rabson static int
56610c9700fSEd Maste fatchain(struct msdosfsmount *pmp, u_long start, u_long count, u_long fillwith)
56727a0bc89SDoug Rabson {
56827a0bc89SDoug Rabson 	int error;
56927a0bc89SDoug Rabson 	u_long bn, bo, bsize, byteoffset, readcn, newc;
57027a0bc89SDoug Rabson 	struct buf *bp;
57127a0bc89SDoug Rabson 
57227a0bc89SDoug Rabson #ifdef MSDOSFS_DEBUG
573952a6212SJordan K. Hubbard 	printf("fatchain(pmp %p, start %lu, count %lu, fillwith %lx)\n",
57427a0bc89SDoug Rabson 	    pmp, start, count, fillwith);
57527a0bc89SDoug Rabson #endif
57627a0bc89SDoug Rabson 	/*
57727a0bc89SDoug Rabson 	 * Be sure the clusters are in the filesystem.
57827a0bc89SDoug Rabson 	 */
57927a0bc89SDoug Rabson 	if (start < CLUST_FIRST || start + count - 1 > pmp->pm_maxcluster)
580952a6212SJordan K. Hubbard 		return (EINVAL);
58127a0bc89SDoug Rabson 
58227a0bc89SDoug Rabson 	while (count > 0) {
58327a0bc89SDoug Rabson 		byteoffset = FATOFS(pmp, start);
58427a0bc89SDoug Rabson 		fatblock(pmp, byteoffset, &bn, &bsize, &bo);
585c3c6d51eSPoul-Henning Kamp 		error = bread(pmp->pm_devvp, bn, bsize, NOCRED, &bp);
586952a6212SJordan K. Hubbard 		if (error) {
587952a6212SJordan K. Hubbard 			brelse(bp);
588952a6212SJordan K. Hubbard 			return (error);
589952a6212SJordan K. Hubbard 		}
59027a0bc89SDoug Rabson 		while (count > 0) {
59127a0bc89SDoug Rabson 			start++;
59227a0bc89SDoug Rabson 			newc = --count > 0 ? start : fillwith;
593952a6212SJordan K. Hubbard 			switch (pmp->pm_fatmask) {
594952a6212SJordan K. Hubbard 			case FAT12_MASK:
59527a0bc89SDoug Rabson 				readcn = getushort(&bp->b_data[bo]);
59627a0bc89SDoug Rabson 				if (start & 1) {
59727a0bc89SDoug Rabson 					readcn &= 0xf000;
59827a0bc89SDoug Rabson 					readcn |= newc & 0xfff;
59927a0bc89SDoug Rabson 				} else {
60027a0bc89SDoug Rabson 					readcn &= 0x000f;
60127a0bc89SDoug Rabson 					readcn |= newc << 4;
60227a0bc89SDoug Rabson 				}
60327a0bc89SDoug Rabson 				putushort(&bp->b_data[bo], readcn);
60427a0bc89SDoug Rabson 				bo++;
60527a0bc89SDoug Rabson 				if (!(start & 1))
60627a0bc89SDoug Rabson 					bo++;
607952a6212SJordan K. Hubbard 				break;
608952a6212SJordan K. Hubbard 			case FAT16_MASK:
60927a0bc89SDoug Rabson 				putushort(&bp->b_data[bo], newc);
61027a0bc89SDoug Rabson 				bo += 2;
611952a6212SJordan K. Hubbard 				break;
612952a6212SJordan K. Hubbard 			case FAT32_MASK:
613952a6212SJordan K. Hubbard 				readcn = getulong(&bp->b_data[bo]);
614952a6212SJordan K. Hubbard 				readcn &= ~pmp->pm_fatmask;
615952a6212SJordan K. Hubbard 				readcn |= newc & pmp->pm_fatmask;
616952a6212SJordan K. Hubbard 				putulong(&bp->b_data[bo], readcn);
617952a6212SJordan K. Hubbard 				bo += 4;
618952a6212SJordan K. Hubbard 				break;
61927a0bc89SDoug Rabson 			}
62027a0bc89SDoug Rabson 			if (bo >= bsize)
62127a0bc89SDoug Rabson 				break;
62227a0bc89SDoug Rabson 		}
62327a0bc89SDoug Rabson 		updatefats(pmp, bp, bn);
62427a0bc89SDoug Rabson 	}
62527a0bc89SDoug Rabson 	pmp->pm_fmod = 1;
626952a6212SJordan K. Hubbard 	return (0);
62727a0bc89SDoug Rabson }
62827a0bc89SDoug Rabson 
62927a0bc89SDoug Rabson /*
63027a0bc89SDoug Rabson  * Check the length of a free cluster chain starting at start.
63127a0bc89SDoug Rabson  *
63227a0bc89SDoug Rabson  * pmp	 - mount point
63327a0bc89SDoug Rabson  * start - start of chain
63427a0bc89SDoug Rabson  * count - maximum interesting length
63527a0bc89SDoug Rabson  */
6367fefffeeSPoul-Henning Kamp static int
63710c9700fSEd Maste chainlength(struct msdosfsmount *pmp, u_long start, u_long count)
63827a0bc89SDoug Rabson {
63927a0bc89SDoug Rabson 	u_long idx, max_idx;
64027a0bc89SDoug Rabson 	u_int map;
64127a0bc89SDoug Rabson 	u_long len;
64227a0bc89SDoug Rabson 
6436be1a4ccSKonstantin Belousov 	MSDOSFS_ASSERT_MP_LOCKED(pmp);
6446be1a4ccSKonstantin Belousov 
645b05088aeSKonstantin Belousov 	if (start > pmp->pm_maxcluster)
646b05088aeSKonstantin Belousov 		return (0);
64727a0bc89SDoug Rabson 	max_idx = pmp->pm_maxcluster / N_INUSEBITS;
64827a0bc89SDoug Rabson 	idx = start / N_INUSEBITS;
64927a0bc89SDoug Rabson 	start %= N_INUSEBITS;
65027a0bc89SDoug Rabson 	map = pmp->pm_inusemap[idx];
65127a0bc89SDoug Rabson 	map &= ~((1 << start) - 1);
65227a0bc89SDoug Rabson 	if (map) {
65327a0bc89SDoug Rabson 		len = ffs(map) - 1 - start;
654b05088aeSKonstantin Belousov 		len = MIN(len, count);
655b05088aeSKonstantin Belousov 		if (start + len > pmp->pm_maxcluster)
656b05088aeSKonstantin Belousov 			len = pmp->pm_maxcluster - start + 1;
657b05088aeSKonstantin Belousov 		return (len);
65827a0bc89SDoug Rabson 	}
65927a0bc89SDoug Rabson 	len = N_INUSEBITS - start;
660b05088aeSKonstantin Belousov 	if (len >= count) {
661b05088aeSKonstantin Belousov 		len = count;
662b05088aeSKonstantin Belousov 		if (start + len > pmp->pm_maxcluster)
663b05088aeSKonstantin Belousov 			len = pmp->pm_maxcluster - start + 1;
664b05088aeSKonstantin Belousov 		return (len);
665b05088aeSKonstantin Belousov 	}
66627a0bc89SDoug Rabson 	while (++idx <= max_idx) {
66727a0bc89SDoug Rabson 		if (len >= count)
66827a0bc89SDoug Rabson 			break;
669c3c6d51eSPoul-Henning Kamp 		map = pmp->pm_inusemap[idx];
670c3c6d51eSPoul-Henning Kamp 		if (map) {
67127a0bc89SDoug Rabson 			len += ffs(map) - 1;
67227a0bc89SDoug Rabson 			break;
67327a0bc89SDoug Rabson 		}
67427a0bc89SDoug Rabson 		len += N_INUSEBITS;
67527a0bc89SDoug Rabson 	}
676b05088aeSKonstantin Belousov 	len = MIN(len, count);
677b05088aeSKonstantin Belousov 	if (start + len > pmp->pm_maxcluster)
678b05088aeSKonstantin Belousov 		len = pmp->pm_maxcluster - start + 1;
679b05088aeSKonstantin Belousov 	return (len);
68027a0bc89SDoug Rabson }
68127a0bc89SDoug Rabson 
68227a0bc89SDoug Rabson /*
68327a0bc89SDoug Rabson  * Allocate contigous free clusters.
68427a0bc89SDoug Rabson  *
68527a0bc89SDoug Rabson  * pmp	      - mount point.
68627a0bc89SDoug Rabson  * start      - start of cluster chain.
68727a0bc89SDoug Rabson  * count      - number of clusters to allocate.
688*9287dbaaSEd Maste  * fillwith   - put this value into the FAT entry for the
68927a0bc89SDoug Rabson  *		last allocated cluster.
69027a0bc89SDoug Rabson  * retcluster - put the first allocated cluster's number here.
69127a0bc89SDoug Rabson  * got	      - how many clusters were actually allocated.
69227a0bc89SDoug Rabson  */
6937fefffeeSPoul-Henning Kamp static int
69410c9700fSEd Maste chainalloc(struct msdosfsmount *pmp, u_long start, u_long count,
69510c9700fSEd Maste     u_long fillwith, u_long *retcluster, u_long *got)
69627a0bc89SDoug Rabson {
69727a0bc89SDoug Rabson 	int error;
698952a6212SJordan K. Hubbard 	u_long cl, n;
699952a6212SJordan K. Hubbard 
7006be1a4ccSKonstantin Belousov 	MSDOSFS_ASSERT_MP_LOCKED(pmp);
701420d65d9SKonstantin Belousov 	KASSERT((pmp->pm_flags & MSDOSFSMNT_RONLY) == 0,
702420d65d9SKonstantin Belousov 	    ("chainalloc on ro msdosfs mount"));
7036be1a4ccSKonstantin Belousov 
704952a6212SJordan K. Hubbard 	for (cl = start, n = count; n-- > 0;)
705952a6212SJordan K. Hubbard 		usemap_alloc(pmp, cl++);
706bb7ca822SKonstantin Belousov 	pmp->pm_nxtfree = start + count;
707bb7ca822SKonstantin Belousov 	if (pmp->pm_nxtfree > pmp->pm_maxcluster)
708bb7ca822SKonstantin Belousov 		pmp->pm_nxtfree = CLUST_FIRST;
709bb7ca822SKonstantin Belousov 	pmp->pm_flags |= MSDOSFS_FSIMOD;
71027a0bc89SDoug Rabson 	error = fatchain(pmp, start, count, fillwith);
71103b8a419SKonstantin Belousov 	if (error != 0) {
71203b8a419SKonstantin Belousov 		for (cl = start, n = count; n-- > 0;)
71303b8a419SKonstantin Belousov 			usemap_free(pmp, cl++);
714952a6212SJordan K. Hubbard 		return (error);
71503b8a419SKonstantin Belousov 	}
71627a0bc89SDoug Rabson #ifdef MSDOSFS_DEBUG
717952a6212SJordan K. Hubbard 	printf("clusteralloc(): allocated cluster chain at %lu (%lu clusters)\n",
71827a0bc89SDoug Rabson 	    start, count);
71927a0bc89SDoug Rabson #endif
72027a0bc89SDoug Rabson 	if (retcluster)
72127a0bc89SDoug Rabson 		*retcluster = start;
72227a0bc89SDoug Rabson 	if (got)
72327a0bc89SDoug Rabson 		*got = count;
724952a6212SJordan K. Hubbard 	return (0);
72527a0bc89SDoug Rabson }
72627a0bc89SDoug Rabson 
72727a0bc89SDoug Rabson /*
72827a0bc89SDoug Rabson  * Allocate contiguous free clusters.
72927a0bc89SDoug Rabson  *
73027a0bc89SDoug Rabson  * pmp	      - mount point.
73127a0bc89SDoug Rabson  * start      - preferred start of cluster chain.
73227a0bc89SDoug Rabson  * count      - number of clusters requested.
733*9287dbaaSEd Maste  * fillwith   - put this value into the FAT entry for the
73427a0bc89SDoug Rabson  *		last allocated cluster.
73527a0bc89SDoug Rabson  * retcluster - put the first allocated cluster's number here.
73627a0bc89SDoug Rabson  * got	      - how many clusters were actually allocated.
73727a0bc89SDoug Rabson  */
73827a0bc89SDoug Rabson int
7396be1a4ccSKonstantin Belousov clusteralloc(struct msdosfsmount *pmp, u_long start, u_long count,
7406be1a4ccSKonstantin Belousov     u_long fillwith, u_long *retcluster, u_long *got)
7416be1a4ccSKonstantin Belousov {
7426be1a4ccSKonstantin Belousov 	int error;
7436be1a4ccSKonstantin Belousov 
7446be1a4ccSKonstantin Belousov 	MSDOSFS_LOCK_MP(pmp);
7456be1a4ccSKonstantin Belousov 	error = clusteralloc1(pmp, start, count, fillwith, retcluster, got);
7466be1a4ccSKonstantin Belousov 	MSDOSFS_UNLOCK_MP(pmp);
7476be1a4ccSKonstantin Belousov 	return (error);
7486be1a4ccSKonstantin Belousov }
7496be1a4ccSKonstantin Belousov 
7506be1a4ccSKonstantin Belousov static int
7516be1a4ccSKonstantin Belousov clusteralloc1(struct msdosfsmount *pmp, u_long start, u_long count,
7526be1a4ccSKonstantin Belousov     u_long fillwith, u_long *retcluster, u_long *got)
75327a0bc89SDoug Rabson {
75427a0bc89SDoug Rabson 	u_long idx;
755952a6212SJordan K. Hubbard 	u_long len, newst, foundl, cn, l;
756952a6212SJordan K. Hubbard 	u_long foundcn = 0; /* XXX: foundcn could be used unititialized */
75727a0bc89SDoug Rabson 	u_int map;
75827a0bc89SDoug Rabson 
7596be1a4ccSKonstantin Belousov 	MSDOSFS_ASSERT_MP_LOCKED(pmp);
7606be1a4ccSKonstantin Belousov 
76127a0bc89SDoug Rabson #ifdef MSDOSFS_DEBUG
762952a6212SJordan K. Hubbard 	printf("clusteralloc(): find %lu clusters\n", count);
76327a0bc89SDoug Rabson #endif
76427a0bc89SDoug Rabson 	if (start) {
76527a0bc89SDoug Rabson 		if ((len = chainlength(pmp, start, count)) >= count)
766952a6212SJordan K. Hubbard 			return (chainalloc(pmp, start, count, fillwith, retcluster, got));
7677cd5051bSPoul-Henning Kamp 	} else
76827a0bc89SDoug Rabson 		len = 0;
76927a0bc89SDoug Rabson 
7708e55bfafSBruce Evans 	newst = pmp->pm_nxtfree;
77127a0bc89SDoug Rabson 	foundl = 0;
77227a0bc89SDoug Rabson 
77327a0bc89SDoug Rabson 	for (cn = newst; cn <= pmp->pm_maxcluster;) {
77427a0bc89SDoug Rabson 		idx = cn / N_INUSEBITS;
77527a0bc89SDoug Rabson 		map = pmp->pm_inusemap[idx];
77627a0bc89SDoug Rabson 		map |= (1 << (cn % N_INUSEBITS)) - 1;
777f33d62b2SKonstantin Belousov 		if (map != FULL_RUN) {
778f33d62b2SKonstantin Belousov 			cn = idx * N_INUSEBITS + ffs(map ^ FULL_RUN) - 1;
77927a0bc89SDoug Rabson 			if ((l = chainlength(pmp, cn, count)) >= count)
780952a6212SJordan K. Hubbard 				return (chainalloc(pmp, cn, count, fillwith, retcluster, got));
78127a0bc89SDoug Rabson 			if (l > foundl) {
78227a0bc89SDoug Rabson 				foundcn = cn;
78327a0bc89SDoug Rabson 				foundl = l;
78427a0bc89SDoug Rabson 			}
78527a0bc89SDoug Rabson 			cn += l + 1;
78627a0bc89SDoug Rabson 			continue;
78727a0bc89SDoug Rabson 		}
78827a0bc89SDoug Rabson 		cn += N_INUSEBITS - cn % N_INUSEBITS;
78927a0bc89SDoug Rabson 	}
79027a0bc89SDoug Rabson 	for (cn = 0; cn < newst;) {
79127a0bc89SDoug Rabson 		idx = cn / N_INUSEBITS;
79227a0bc89SDoug Rabson 		map = pmp->pm_inusemap[idx];
79327a0bc89SDoug Rabson 		map |= (1 << (cn % N_INUSEBITS)) - 1;
794f33d62b2SKonstantin Belousov 		if (map != FULL_RUN) {
795f33d62b2SKonstantin Belousov 			cn = idx * N_INUSEBITS + ffs(map ^ FULL_RUN) - 1;
79627a0bc89SDoug Rabson 			if ((l = chainlength(pmp, cn, count)) >= count)
797952a6212SJordan K. Hubbard 				return (chainalloc(pmp, cn, count, fillwith, retcluster, got));
79827a0bc89SDoug Rabson 			if (l > foundl) {
79927a0bc89SDoug Rabson 				foundcn = cn;
80027a0bc89SDoug Rabson 				foundl = l;
80127a0bc89SDoug Rabson 			}
80227a0bc89SDoug Rabson 			cn += l + 1;
80327a0bc89SDoug Rabson 			continue;
80427a0bc89SDoug Rabson 		}
80527a0bc89SDoug Rabson 		cn += N_INUSEBITS - cn % N_INUSEBITS;
80627a0bc89SDoug Rabson 	}
80727a0bc89SDoug Rabson 
80827a0bc89SDoug Rabson 	if (!foundl)
809952a6212SJordan K. Hubbard 		return (ENOSPC);
81027a0bc89SDoug Rabson 
81127a0bc89SDoug Rabson 	if (len)
812952a6212SJordan K. Hubbard 		return (chainalloc(pmp, start, len, fillwith, retcluster, got));
81327a0bc89SDoug Rabson 	else
814952a6212SJordan K. Hubbard 		return (chainalloc(pmp, foundcn, foundl, fillwith, retcluster, got));
81527a0bc89SDoug Rabson }
81627a0bc89SDoug Rabson 
81727a0bc89SDoug Rabson 
81827a0bc89SDoug Rabson /*
81927a0bc89SDoug Rabson  * Free a chain of clusters.
82027a0bc89SDoug Rabson  *
82127a0bc89SDoug Rabson  * pmp		- address of the msdosfs mount structure for the filesystem
82227a0bc89SDoug Rabson  *		  containing the cluster chain to be freed.
82327a0bc89SDoug Rabson  * startcluster - number of the 1st cluster in the chain of clusters to be
82427a0bc89SDoug Rabson  *		  freed.
82527a0bc89SDoug Rabson  */
82627a0bc89SDoug Rabson int
82710c9700fSEd Maste freeclusterchain(struct msdosfsmount *pmp, u_long cluster)
82827a0bc89SDoug Rabson {
829952a6212SJordan K. Hubbard 	int error;
83027a0bc89SDoug Rabson 	struct buf *bp = NULL;
83127a0bc89SDoug Rabson 	u_long bn, bo, bsize, byteoffset;
83227a0bc89SDoug Rabson 	u_long readcn, lbn = -1;
83327a0bc89SDoug Rabson 
8346be1a4ccSKonstantin Belousov 	MSDOSFS_LOCK_MP(pmp);
83527a0bc89SDoug Rabson 	while (cluster >= CLUST_FIRST && cluster <= pmp->pm_maxcluster) {
83627a0bc89SDoug Rabson 		byteoffset = FATOFS(pmp, cluster);
83727a0bc89SDoug Rabson 		fatblock(pmp, byteoffset, &bn, &bsize, &bo);
83827a0bc89SDoug Rabson 		if (lbn != bn) {
83927a0bc89SDoug Rabson 			if (bp)
84024d4540cSBruce Evans 				updatefats(pmp, bp, lbn);
841c3c6d51eSPoul-Henning Kamp 			error = bread(pmp->pm_devvp, bn, bsize, NOCRED, &bp);
842952a6212SJordan K. Hubbard 			if (error) {
843952a6212SJordan K. Hubbard 				brelse(bp);
8446be1a4ccSKonstantin Belousov 				MSDOSFS_UNLOCK_MP(pmp);
845952a6212SJordan K. Hubbard 				return (error);
846952a6212SJordan K. Hubbard 			}
84727a0bc89SDoug Rabson 			lbn = bn;
84827a0bc89SDoug Rabson 		}
84927a0bc89SDoug Rabson 		usemap_free(pmp, cluster);
850952a6212SJordan K. Hubbard 		switch (pmp->pm_fatmask) {
851952a6212SJordan K. Hubbard 		case FAT12_MASK:
85227a0bc89SDoug Rabson 			readcn = getushort(&bp->b_data[bo]);
85327a0bc89SDoug Rabson 			if (cluster & 1) {
85427a0bc89SDoug Rabson 				cluster = readcn >> 4;
85527a0bc89SDoug Rabson 				readcn &= 0x000f;
85627a0bc89SDoug Rabson 				readcn |= MSDOSFSFREE << 4;
85727a0bc89SDoug Rabson 			} else {
85827a0bc89SDoug Rabson 				cluster = readcn;
85927a0bc89SDoug Rabson 				readcn &= 0xf000;
86027a0bc89SDoug Rabson 				readcn |= MSDOSFSFREE & 0xfff;
86127a0bc89SDoug Rabson 			}
86227a0bc89SDoug Rabson 			putushort(&bp->b_data[bo], readcn);
863952a6212SJordan K. Hubbard 			break;
864952a6212SJordan K. Hubbard 		case FAT16_MASK:
865952a6212SJordan K. Hubbard 			cluster = getushort(&bp->b_data[bo]);
86627a0bc89SDoug Rabson 			putushort(&bp->b_data[bo], MSDOSFSFREE);
867952a6212SJordan K. Hubbard 			break;
868952a6212SJordan K. Hubbard 		case FAT32_MASK:
869952a6212SJordan K. Hubbard 			cluster = getulong(&bp->b_data[bo]);
870952a6212SJordan K. Hubbard 			putulong(&bp->b_data[bo],
871952a6212SJordan K. Hubbard 				 (MSDOSFSFREE & FAT32_MASK) | (cluster & ~FAT32_MASK));
872952a6212SJordan K. Hubbard 			break;
87327a0bc89SDoug Rabson 		}
874952a6212SJordan K. Hubbard 		cluster &= pmp->pm_fatmask;
875952a6212SJordan K. Hubbard 		if ((cluster | ~pmp->pm_fatmask) >= CLUST_RSRVD)
876952a6212SJordan K. Hubbard 			cluster |= pmp->pm_fatmask;
87727a0bc89SDoug Rabson 	}
87827a0bc89SDoug Rabson 	if (bp)
87927a0bc89SDoug Rabson 		updatefats(pmp, bp, bn);
8806be1a4ccSKonstantin Belousov 	MSDOSFS_UNLOCK_MP(pmp);
881952a6212SJordan K. Hubbard 	return (0);
88227a0bc89SDoug Rabson }
88327a0bc89SDoug Rabson 
88427a0bc89SDoug Rabson /*
885*9287dbaaSEd Maste  * Read in FAT blocks looking for free clusters. For every free cluster
88627a0bc89SDoug Rabson  * found turn off its corresponding bit in the pm_inusemap.
88727a0bc89SDoug Rabson  */
88827a0bc89SDoug Rabson int
88910c9700fSEd Maste fillinusemap(struct msdosfsmount *pmp)
89027a0bc89SDoug Rabson {
89127a0bc89SDoug Rabson 	struct buf *bp = NULL;
89227a0bc89SDoug Rabson 	u_long cn, readcn;
89327a0bc89SDoug Rabson 	int error;
89427a0bc89SDoug Rabson 	u_long bn, bo, bsize, byteoffset;
89527a0bc89SDoug Rabson 
8966be1a4ccSKonstantin Belousov 	MSDOSFS_ASSERT_MP_LOCKED(pmp);
8976be1a4ccSKonstantin Belousov 
89827a0bc89SDoug Rabson 	/*
899*9287dbaaSEd Maste 	 * Mark all clusters in use, we mark the free ones in the FAT scan
90027a0bc89SDoug Rabson 	 * loop further down.
90127a0bc89SDoug Rabson 	 */
90227a0bc89SDoug Rabson 	for (cn = 0; cn < (pmp->pm_maxcluster + N_INUSEBITS) / N_INUSEBITS; cn++)
903f33d62b2SKonstantin Belousov 		pmp->pm_inusemap[cn] = FULL_RUN;
90427a0bc89SDoug Rabson 
90527a0bc89SDoug Rabson 	/*
90627a0bc89SDoug Rabson 	 * Figure how many free clusters are in the filesystem by ripping
907*9287dbaaSEd Maste 	 * through the FAT counting the number of entries whose content is
90827a0bc89SDoug Rabson 	 * zero.  These represent free clusters.
90927a0bc89SDoug Rabson 	 */
91027a0bc89SDoug Rabson 	pmp->pm_freeclustercount = 0;
91127a0bc89SDoug Rabson 	for (cn = CLUST_FIRST; cn <= pmp->pm_maxcluster; cn++) {
91227a0bc89SDoug Rabson 		byteoffset = FATOFS(pmp, cn);
91327a0bc89SDoug Rabson 		bo = byteoffset % pmp->pm_fatblocksize;
91427a0bc89SDoug Rabson 		if (!bo || !bp) {
91527a0bc89SDoug Rabson 			/* Read new FAT block */
91627a0bc89SDoug Rabson 			if (bp)
91727a0bc89SDoug Rabson 				brelse(bp);
91827a0bc89SDoug Rabson 			fatblock(pmp, byteoffset, &bn, &bsize, NULL);
91927a0bc89SDoug Rabson 			error = bread(pmp->pm_devvp, bn, bsize, NOCRED, &bp);
920952a6212SJordan K. Hubbard 			if (error) {
921952a6212SJordan K. Hubbard 				brelse(bp);
922952a6212SJordan K. Hubbard 				return (error);
92327a0bc89SDoug Rabson 			}
924952a6212SJordan K. Hubbard 		}
925952a6212SJordan K. Hubbard 		if (FAT32(pmp))
926952a6212SJordan K. Hubbard 			readcn = getulong(&bp->b_data[bo]);
927952a6212SJordan K. Hubbard 		else
92827a0bc89SDoug Rabson 			readcn = getushort(&bp->b_data[bo]);
929952a6212SJordan K. Hubbard 		if (FAT12(pmp) && (cn & 1))
93027a0bc89SDoug Rabson 			readcn >>= 4;
931952a6212SJordan K. Hubbard 		readcn &= pmp->pm_fatmask;
93227a0bc89SDoug Rabson 
9331c4ec415SKonstantin Belousov 		if (readcn == CLUST_FREE)
93427a0bc89SDoug Rabson 			usemap_free(pmp, cn);
93527a0bc89SDoug Rabson 	}
9363c8b687fSKonstantin Belousov 	if (bp != NULL)
93727a0bc89SDoug Rabson 		brelse(bp);
938b05088aeSKonstantin Belousov 
939b05088aeSKonstantin Belousov 	for (cn = pmp->pm_maxcluster + 1; cn < (pmp->pm_maxcluster +
940b05088aeSKonstantin Belousov 	    N_INUSEBITS) / N_INUSEBITS; cn++)
941b05088aeSKonstantin Belousov 		pmp->pm_inusemap[cn / N_INUSEBITS] |= 1 << (cn % N_INUSEBITS);
942b05088aeSKonstantin Belousov 
943952a6212SJordan K. Hubbard 	return (0);
94427a0bc89SDoug Rabson }
94527a0bc89SDoug Rabson 
94627a0bc89SDoug Rabson /*
94727a0bc89SDoug Rabson  * Allocate a new cluster and chain it onto the end of the file.
94827a0bc89SDoug Rabson  *
94927a0bc89SDoug Rabson  * dep	 - the file to extend
95027a0bc89SDoug Rabson  * count - number of clusters to allocate
95127a0bc89SDoug Rabson  * bpp	 - where to return the address of the buf header for the first new
95227a0bc89SDoug Rabson  *	   file block
95327a0bc89SDoug Rabson  * ncp	 - where to put cluster number of the first newly allocated cluster
95427a0bc89SDoug Rabson  *	   If this pointer is 0, do not return the cluster number.
95527a0bc89SDoug Rabson  * flags - see fat.h
95627a0bc89SDoug Rabson  *
95727a0bc89SDoug Rabson  * NOTE: This function is not responsible for turning on the DE_UPDATE bit of
95827a0bc89SDoug Rabson  * the de_flag field of the denode and it does not change the de_FileSize
95927a0bc89SDoug Rabson  * field.  This is left for the caller to do.
96027a0bc89SDoug Rabson  */
96127a0bc89SDoug Rabson int
96210c9700fSEd Maste extendfile(struct denode *dep, u_long count, struct buf **bpp, u_long *ncp,
96310c9700fSEd Maste     int flags)
96427a0bc89SDoug Rabson {
965952a6212SJordan K. Hubbard 	int error;
96627a0bc89SDoug Rabson 	u_long frcn;
96727a0bc89SDoug Rabson 	u_long cn, got;
96827a0bc89SDoug Rabson 	struct msdosfsmount *pmp = dep->de_pmp;
96927a0bc89SDoug Rabson 	struct buf *bp;
9700d2af521SKirk McKusick 	daddr_t blkno;
97127a0bc89SDoug Rabson 
97227a0bc89SDoug Rabson 	/*
97327a0bc89SDoug Rabson 	 * Don't try to extend the root directory
97427a0bc89SDoug Rabson 	 */
975952a6212SJordan K. Hubbard 	if (dep->de_StartCluster == MSDOSFSROOT
976952a6212SJordan K. Hubbard 	    && (dep->de_Attributes & ATTR_DIRECTORY)) {
97754cf9198SKonstantin Belousov #ifdef MSDOSFS_DEBUG
97827a0bc89SDoug Rabson 		printf("extendfile(): attempt to extend root directory\n");
97954cf9198SKonstantin Belousov #endif
980952a6212SJordan K. Hubbard 		return (ENOSPC);
98127a0bc89SDoug Rabson 	}
98227a0bc89SDoug Rabson 
98327a0bc89SDoug Rabson 	/*
98427a0bc89SDoug Rabson 	 * If the "file's last cluster" cache entry is empty, and the file
98527a0bc89SDoug Rabson 	 * is not empty, then fill the cache entry by calling pcbmap().
98627a0bc89SDoug Rabson 	 */
98727a0bc89SDoug Rabson 	if (dep->de_fc[FC_LASTFC].fc_frcn == FCE_EMPTY &&
98827a0bc89SDoug Rabson 	    dep->de_StartCluster != 0) {
989952a6212SJordan K. Hubbard 		error = pcbmap(dep, 0xffff, 0, &cn, 0);
99027a0bc89SDoug Rabson 		/* we expect it to return E2BIG */
99127a0bc89SDoug Rabson 		if (error != E2BIG)
992952a6212SJordan K. Hubbard 			return (error);
99327a0bc89SDoug Rabson 	}
99427a0bc89SDoug Rabson 
995ededffc0STom Rhodes 	dep->de_fc[FC_NEXTTOLASTFC].fc_frcn =
996ededffc0STom Rhodes 	    dep->de_fc[FC_LASTFC].fc_frcn;
997ededffc0STom Rhodes 	dep->de_fc[FC_NEXTTOLASTFC].fc_fsrcn =
998ededffc0STom Rhodes 	    dep->de_fc[FC_LASTFC].fc_fsrcn;
99927a0bc89SDoug Rabson 	while (count > 0) {
100027a0bc89SDoug Rabson 		/*
1001952a6212SJordan K. Hubbard 		 * Allocate a new cluster chain and cat onto the end of the
1002f220587dSKonstantin Belousov 		 * file.
1003f220587dSKonstantin Belousov 		 * If the file is empty we make de_StartCluster point
1004f220587dSKonstantin Belousov 		 * to the new block.  Note that de_StartCluster being
1005f220587dSKonstantin Belousov 		 * 0 is sufficient to be sure the file is empty since
1006f220587dSKonstantin Belousov 		 * we exclude attempts to extend the root directory
1007f220587dSKonstantin Belousov 		 * above, and the root dir is the only file with a
1008f220587dSKonstantin Belousov 		 * startcluster of 0 that has blocks allocated (sort
1009f220587dSKonstantin Belousov 		 * of).
101027a0bc89SDoug Rabson 		 */
101127a0bc89SDoug Rabson 		if (dep->de_StartCluster == 0)
101227a0bc89SDoug Rabson 			cn = 0;
101327a0bc89SDoug Rabson 		else
101427a0bc89SDoug Rabson 			cn = dep->de_fc[FC_LASTFC].fc_fsrcn + 1;
1015c3c6d51eSPoul-Henning Kamp 		error = clusteralloc(pmp, cn, count, CLUST_EOFE, &cn, &got);
1016c3c6d51eSPoul-Henning Kamp 		if (error)
1017952a6212SJordan K. Hubbard 			return (error);
101827a0bc89SDoug Rabson 
101927a0bc89SDoug Rabson 		count -= got;
102027a0bc89SDoug Rabson 
102127a0bc89SDoug Rabson 		/*
102227a0bc89SDoug Rabson 		 * Give them the filesystem relative cluster number if they want
102327a0bc89SDoug Rabson 		 * it.
102427a0bc89SDoug Rabson 		 */
102527a0bc89SDoug Rabson 		if (ncp) {
102627a0bc89SDoug Rabson 			*ncp = cn;
102727a0bc89SDoug Rabson 			ncp = NULL;
102827a0bc89SDoug Rabson 		}
102927a0bc89SDoug Rabson 
103027a0bc89SDoug Rabson 		if (dep->de_StartCluster == 0) {
103127a0bc89SDoug Rabson 			dep->de_StartCluster = cn;
103227a0bc89SDoug Rabson 			frcn = 0;
103327a0bc89SDoug Rabson 		} else {
1034952a6212SJordan K. Hubbard 			error = fatentry(FAT_SET, pmp,
1035952a6212SJordan K. Hubbard 					 dep->de_fc[FC_LASTFC].fc_fsrcn,
103627a0bc89SDoug Rabson 					 0, cn);
103727a0bc89SDoug Rabson 			if (error) {
103827a0bc89SDoug Rabson 				clusterfree(pmp, cn, NULL);
1039952a6212SJordan K. Hubbard 				return (error);
104027a0bc89SDoug Rabson 			}
104127a0bc89SDoug Rabson 			frcn = dep->de_fc[FC_LASTFC].fc_frcn + 1;
104227a0bc89SDoug Rabson 		}
104327a0bc89SDoug Rabson 
104427a0bc89SDoug Rabson 		/*
1045*9287dbaaSEd Maste 		 * Update the "last cluster of the file" entry in the
1046*9287dbaaSEd Maste 		 * denode's FAT cache.
104727a0bc89SDoug Rabson 		 */
104827a0bc89SDoug Rabson 		fc_setcache(dep, FC_LASTFC, frcn + got - 1, cn + got - 1);
104927a0bc89SDoug Rabson 
105027a0bc89SDoug Rabson 		if (flags & DE_CLEAR) {
105127a0bc89SDoug Rabson 			while (got-- > 0) {
105227a0bc89SDoug Rabson 				/*
105327a0bc89SDoug Rabson 				 * Get the buf header for the new block of the file.
105427a0bc89SDoug Rabson 				 */
105527a0bc89SDoug Rabson 				if (dep->de_Attributes & ATTR_DIRECTORY)
10567261f5f6SJeff Roberson 					bp = getblk(pmp->pm_devvp,
10577261f5f6SJeff Roberson 					    cntobn(pmp, cn++),
10587261f5f6SJeff Roberson 					    pmp->pm_bpcluster, 0, 0, 0);
105927a0bc89SDoug Rabson 				else {
10607261f5f6SJeff Roberson 					bp = getblk(DETOV(dep),
106167c7bbf3SKonstantin Belousov 					    frcn++,
10627261f5f6SJeff Roberson 					    pmp->pm_bpcluster, 0, 0, 0);
106327a0bc89SDoug Rabson 					/*
106427a0bc89SDoug Rabson 					 * Do the bmap now, as in msdosfs_write
106527a0bc89SDoug Rabson 					 */
1066952a6212SJordan K. Hubbard 					if (pcbmap(dep,
106767c7bbf3SKonstantin Belousov 					    bp->b_lblkno,
10680d2af521SKirk McKusick 					    &blkno, 0, 0))
106927a0bc89SDoug Rabson 						bp->b_blkno = -1;
107027a0bc89SDoug Rabson 					if (bp->b_blkno == -1)
107127a0bc89SDoug Rabson 						panic("extendfile: pcbmap");
10720d2af521SKirk McKusick 					else
10730d2af521SKirk McKusick 						bp->b_blkno = blkno;
107427a0bc89SDoug Rabson 				}
1075d34b0a1bSBruce Evans 				vfs_bio_clrbuf(bp);
107627a0bc89SDoug Rabson 				if (bpp) {
107727a0bc89SDoug Rabson 					*bpp = bp;
107827a0bc89SDoug Rabson 					bpp = NULL;
1079952a6212SJordan K. Hubbard 				} else
1080952a6212SJordan K. Hubbard 					bdwrite(bp);
108127a0bc89SDoug Rabson 			}
108227a0bc89SDoug Rabson 		}
108327a0bc89SDoug Rabson 	}
108427a0bc89SDoug Rabson 
1085952a6212SJordan K. Hubbard 	return (0);
108627a0bc89SDoug Rabson }
1087cede1f56STom Rhodes 
1088392dbea3SBruce Evans /*-
1089392dbea3SBruce Evans  * Routine to mark a FAT16 or FAT32 volume as "clean" or "dirty" by
1090392dbea3SBruce Evans  * manipulating the upper bit of the FAT entry for cluster 1.  Note that
1091392dbea3SBruce Evans  * this bit is not defined for FAT12 volumes, which are always assumed to
1092a26b949fSKonstantin Belousov  * be clean.
1093cede1f56STom Rhodes  *
1094392dbea3SBruce Evans  * The fatentry() routine only works on cluster numbers that a file could
1095392dbea3SBruce Evans  * occupy, so it won't manipulate the entry for cluster 1.  So we have to do
1096392dbea3SBruce Evans  * it here.  The code was stolen from fatentry() and tailored for cluster 1.
1097cede1f56STom Rhodes  *
1098cede1f56STom Rhodes  * Inputs:
1099cede1f56STom Rhodes  *	pmp	The MS-DOS volume to mark
1100392dbea3SBruce Evans  *	dirty	Non-zero if the volume should be marked dirty; zero if it
1101392dbea3SBruce Evans  *		should be marked clean
1102cede1f56STom Rhodes  *
1103cede1f56STom Rhodes  * Result:
1104cede1f56STom Rhodes  *	0	Success
1105cede1f56STom Rhodes  *	EROFS	Volume is read-only
1106cede1f56STom Rhodes  *	?	(other errors from called routines)
1107cede1f56STom Rhodes  */
1108392dbea3SBruce Evans int
1109392dbea3SBruce Evans markvoldirty(struct msdosfsmount *pmp, int dirty)
1110cede1f56STom Rhodes {
1111cede1f56STom Rhodes 	struct buf *bp;
1112392dbea3SBruce Evans 	u_long bn, bo, bsize, byteoffset, fatval;
1113392dbea3SBruce Evans 	int error;
1114cede1f56STom Rhodes 
1115392dbea3SBruce Evans 	/*
1116392dbea3SBruce Evans 	 * FAT12 does not support a "clean" bit, so don't do anything for
1117392dbea3SBruce Evans 	 * FAT12.
1118392dbea3SBruce Evans 	 */
1119cede1f56STom Rhodes 	if (FAT12(pmp))
1120392dbea3SBruce Evans 		return (0);
1121cede1f56STom Rhodes 
1122392dbea3SBruce Evans 	/* Can't change the bit on a read-only filesystem. */
1123cede1f56STom Rhodes 	if (pmp->pm_flags & MSDOSFSMNT_RONLY)
1124392dbea3SBruce Evans 		return (EROFS);
1125cede1f56STom Rhodes 
1126392dbea3SBruce Evans 	/*
1127392dbea3SBruce Evans 	 * Fetch the block containing the FAT entry.  It is given by the
1128392dbea3SBruce Evans 	 * pseudo-cluster 1.
1129392dbea3SBruce Evans 	 */
1130392dbea3SBruce Evans 	byteoffset = FATOFS(pmp, 1);
1131cede1f56STom Rhodes 	fatblock(pmp, byteoffset, &bn, &bsize, &bo);
1132cede1f56STom Rhodes 	error = bread(pmp->pm_devvp, bn, bsize, NOCRED, &bp);
1133cede1f56STom Rhodes 	if (error) {
1134cede1f56STom Rhodes 		brelse(bp);
1135cede1f56STom Rhodes 		return (error);
1136cede1f56STom Rhodes 	}
1137cede1f56STom Rhodes 
1138392dbea3SBruce Evans 	/*
1139392dbea3SBruce Evans 	 * Get the current value of the FAT entry and set/clear the relevant
1140392dbea3SBruce Evans 	 * bit.  Dirty means clear the "clean" bit; clean means set the
1141392dbea3SBruce Evans 	 * "clean" bit.
1142392dbea3SBruce Evans 	 */
1143cede1f56STom Rhodes 	if (FAT32(pmp)) {
1144392dbea3SBruce Evans 		/* FAT32 uses bit 27. */
1145cede1f56STom Rhodes 		fatval = getulong(&bp->b_data[bo]);
1146cede1f56STom Rhodes 		if (dirty)
1147392dbea3SBruce Evans 			fatval &= 0xF7FFFFFF;
1148cede1f56STom Rhodes 		else
1149392dbea3SBruce Evans 			fatval |= 0x08000000;
1150cede1f56STom Rhodes 		putulong(&bp->b_data[bo], fatval);
1151392dbea3SBruce Evans 	} else {
1152392dbea3SBruce Evans 		/* Must be FAT16; use bit 15. */
1153cede1f56STom Rhodes 		fatval = getushort(&bp->b_data[bo]);
1154cede1f56STom Rhodes 		if (dirty)
1155392dbea3SBruce Evans 			fatval &= 0x7FFF;
1156cede1f56STom Rhodes 		else
1157392dbea3SBruce Evans 			fatval |= 0x8000;
1158cede1f56STom Rhodes 		putushort(&bp->b_data[bo], fatval);
1159cede1f56STom Rhodes 	}
1160cede1f56STom Rhodes 
1161392dbea3SBruce Evans 	/* Write out the modified FAT block synchronously. */
1162392dbea3SBruce Evans 	return (bwrite(bp));
1163cede1f56STom Rhodes }
1164