xref: /freebsd/sys/fs/msdosfs/msdosfs_fat.c (revision 10c9700f3e2d1fe250984ffe0c2ba308254d758e)
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
85*10c9700fSEd Maste fatblock(struct msdosfsmount *pmp, u_long ofs, u_long *bnp, u_long *sizep,
86*10c9700fSEd Maste     u_long *bop)
8727a0bc89SDoug Rabson {
8827a0bc89SDoug Rabson 	u_long bn, size;
8927a0bc89SDoug Rabson 
9027a0bc89SDoug Rabson 	bn = ofs / pmp->pm_fatblocksize * pmp->pm_fatblocksec;
9127a0bc89SDoug Rabson 	size = min(pmp->pm_fatblocksec, pmp->pm_FATsecs - bn)
9201f6cfbaSYoshihiro Takahashi 	    * DEV_BSIZE;
93952a6212SJordan K. Hubbard 	bn += pmp->pm_fatblk + pmp->pm_curfat * pmp->pm_FATsecs;
94952a6212SJordan K. Hubbard 
9527a0bc89SDoug Rabson 	if (bnp)
9627a0bc89SDoug Rabson 		*bnp = bn;
9727a0bc89SDoug Rabson 	if (sizep)
9827a0bc89SDoug Rabson 		*sizep = size;
9927a0bc89SDoug Rabson 	if (bop)
10027a0bc89SDoug Rabson 		*bop = ofs % pmp->pm_fatblocksize;
10127a0bc89SDoug Rabson }
10227a0bc89SDoug Rabson 
10327a0bc89SDoug Rabson /*
10427a0bc89SDoug Rabson  * Map the logical cluster number of a file into a physical disk sector
10527a0bc89SDoug Rabson  * that is filesystem relative.
10627a0bc89SDoug Rabson  *
10727a0bc89SDoug Rabson  * dep	  - address of denode representing the file of interest
10827a0bc89SDoug Rabson  * findcn - file relative cluster whose filesystem relative cluster number
10927a0bc89SDoug Rabson  *	    and/or block number are/is to be found
11027a0bc89SDoug Rabson  * bnp	  - address of where to place the filesystem relative block number.
11127a0bc89SDoug Rabson  *	    If this pointer is null then don't return this quantity.
11227a0bc89SDoug Rabson  * cnp	  - address of where to place the filesystem relative cluster number.
11327a0bc89SDoug Rabson  *	    If this pointer is null then don't return this quantity.
11427a0bc89SDoug Rabson  *
11527a0bc89SDoug Rabson  * NOTE: Either bnp or cnp must be non-null.
11627a0bc89SDoug Rabson  * This function has one side effect.  If the requested file relative cluster
11727a0bc89SDoug Rabson  * is beyond the end of file, then the actual number of clusters in the file
11827a0bc89SDoug Rabson  * is returned in *cnp.  This is useful for determining how long a directory is.
11927a0bc89SDoug Rabson  *  If cnp is null, nothing is returned.
12027a0bc89SDoug Rabson  */
12127a0bc89SDoug Rabson int
122*10c9700fSEd Maste pcbmap(struct denode *dep, u_long findcn, daddr_t *bnp, u_long *cnp, int *sp)
12327a0bc89SDoug Rabson {
12427a0bc89SDoug Rabson 	int error;
12527a0bc89SDoug Rabson 	u_long i;
12627a0bc89SDoug Rabson 	u_long cn;
127952a6212SJordan K. Hubbard 	u_long prevcn = 0; /* XXX: prevcn could be used unititialized */
12827a0bc89SDoug Rabson 	u_long byteoffset;
12927a0bc89SDoug Rabson 	u_long bn;
13027a0bc89SDoug Rabson 	u_long bo;
13127a0bc89SDoug Rabson 	struct buf *bp = NULL;
13227a0bc89SDoug Rabson 	u_long bp_bn = -1;
13327a0bc89SDoug Rabson 	struct msdosfsmount *pmp = dep->de_pmp;
13427a0bc89SDoug Rabson 	u_long bsize;
13527a0bc89SDoug Rabson 
136ef6a2be3SKonstantin Belousov 	KASSERT(bnp != NULL || cnp != NULL || sp != NULL,
137ef6a2be3SKonstantin Belousov 	    ("pcbmap: extra call"));
138ef6a2be3SKonstantin Belousov 	ASSERT_VOP_ELOCKED(DETOV(dep), "pcbmap");
13927a0bc89SDoug Rabson 
14027a0bc89SDoug Rabson 	cn = dep->de_StartCluster;
14127a0bc89SDoug Rabson 	/*
14227a0bc89SDoug Rabson 	 * The "file" that makes up the root directory is contiguous,
14327a0bc89SDoug Rabson 	 * permanently allocated, of fixed size, and is not made up of
14427a0bc89SDoug Rabson 	 * clusters.  If the cluster number is beyond the end of the root
14527a0bc89SDoug Rabson 	 * directory, then return the number of clusters in the file.
14627a0bc89SDoug Rabson 	 */
14727a0bc89SDoug Rabson 	if (cn == MSDOSFSROOT) {
14827a0bc89SDoug Rabson 		if (dep->de_Attributes & ATTR_DIRECTORY) {
149952a6212SJordan K. Hubbard 			if (de_cn2off(pmp, findcn) >= dep->de_FileSize) {
15027a0bc89SDoug Rabson 				if (cnp)
151952a6212SJordan K. Hubbard 					*cnp = de_bn2cn(pmp, pmp->pm_rootdirsize);
152952a6212SJordan K. Hubbard 				return (E2BIG);
15327a0bc89SDoug Rabson 			}
15427a0bc89SDoug Rabson 			if (bnp)
155952a6212SJordan K. Hubbard 				*bnp = pmp->pm_rootdirblk + de_cn2bn(pmp, findcn);
15627a0bc89SDoug Rabson 			if (cnp)
15727a0bc89SDoug Rabson 				*cnp = MSDOSFSROOT;
158952a6212SJordan K. Hubbard 			if (sp)
159952a6212SJordan K. Hubbard 				*sp = min(pmp->pm_bpcluster,
160952a6212SJordan K. Hubbard 				    dep->de_FileSize - de_cn2off(pmp, findcn));
161952a6212SJordan K. Hubbard 			return (0);
16227a0bc89SDoug Rabson 		} else {		/* just an empty file */
16327a0bc89SDoug Rabson 			if (cnp)
16427a0bc89SDoug Rabson 				*cnp = 0;
165952a6212SJordan K. Hubbard 			return (E2BIG);
16627a0bc89SDoug Rabson 		}
16727a0bc89SDoug Rabson 	}
16827a0bc89SDoug Rabson 
16927a0bc89SDoug Rabson 	/*
170952a6212SJordan K. Hubbard 	 * All other files do I/O in cluster sized blocks
171952a6212SJordan K. Hubbard 	 */
172952a6212SJordan K. Hubbard 	if (sp)
173952a6212SJordan K. Hubbard 		*sp = pmp->pm_bpcluster;
174952a6212SJordan K. Hubbard 
175952a6212SJordan K. Hubbard 	/*
17627a0bc89SDoug Rabson 	 * Rummage around in the fat cache, maybe we can avoid tromping
17727a0bc89SDoug Rabson 	 * thru every fat entry for the file. And, keep track of how far
17827a0bc89SDoug Rabson 	 * off the cache was from where we wanted to be.
17927a0bc89SDoug Rabson 	 */
18027a0bc89SDoug Rabson 	i = 0;
18127a0bc89SDoug Rabson 	fc_lookup(dep, findcn, &i, &cn);
18227a0bc89SDoug Rabson 
18327a0bc89SDoug Rabson 	/*
18427a0bc89SDoug Rabson 	 * Handle all other files or directories the normal way.
18527a0bc89SDoug Rabson 	 */
18627a0bc89SDoug Rabson 	for (; i < findcn; i++) {
187952a6212SJordan K. Hubbard 		/*
188952a6212SJordan K. Hubbard 		 * Stop with all reserved clusters, not just with EOF.
189952a6212SJordan K. Hubbard 		 */
190952a6212SJordan K. Hubbard 		if ((cn | ~pmp->pm_fatmask) >= CLUST_RSRVD)
19127a0bc89SDoug Rabson 			goto hiteof;
19227a0bc89SDoug Rabson 		byteoffset = FATOFS(pmp, cn);
19327a0bc89SDoug Rabson 		fatblock(pmp, byteoffset, &bn, &bsize, &bo);
19427a0bc89SDoug Rabson 		if (bn != bp_bn) {
19527a0bc89SDoug Rabson 			if (bp)
19627a0bc89SDoug Rabson 				brelse(bp);
19727a0bc89SDoug Rabson 			error = bread(pmp->pm_devvp, bn, bsize, NOCRED, &bp);
198952a6212SJordan K. Hubbard 			if (error) {
199952a6212SJordan K. Hubbard 				brelse(bp);
200952a6212SJordan K. Hubbard 				return (error);
201952a6212SJordan K. Hubbard 			}
20227a0bc89SDoug Rabson 			bp_bn = bn;
20327a0bc89SDoug Rabson 		}
20427a0bc89SDoug Rabson 		prevcn = cn;
205d23af19aSTim J. Robbins 		if (bo >= bsize) {
206d23af19aSTim J. Robbins 			if (bp)
207d23af19aSTim J. Robbins 				brelse(bp);
208d23af19aSTim J. Robbins 			return (EIO);
209d23af19aSTim J. Robbins 		}
210952a6212SJordan K. Hubbard 		if (FAT32(pmp))
211952a6212SJordan K. Hubbard 			cn = getulong(&bp->b_data[bo]);
212952a6212SJordan K. Hubbard 		else
21327a0bc89SDoug Rabson 			cn = getushort(&bp->b_data[bo]);
214952a6212SJordan K. Hubbard 		if (FAT12(pmp) && (prevcn & 1))
21527a0bc89SDoug Rabson 			cn >>= 4;
216952a6212SJordan K. Hubbard 		cn &= pmp->pm_fatmask;
217952a6212SJordan K. Hubbard 
21827a0bc89SDoug Rabson 		/*
219952a6212SJordan K. Hubbard 		 * Force the special cluster numbers
220952a6212SJordan K. Hubbard 		 * to be the same for all cluster sizes
221952a6212SJordan K. Hubbard 		 * to let the rest of msdosfs handle
222952a6212SJordan K. Hubbard 		 * all cases the same.
22327a0bc89SDoug Rabson 		 */
224952a6212SJordan K. Hubbard 		if ((cn | ~pmp->pm_fatmask) >= CLUST_RSRVD)
225952a6212SJordan K. Hubbard 			cn |= ~pmp->pm_fatmask;
22627a0bc89SDoug Rabson 	}
22727a0bc89SDoug Rabson 
228952a6212SJordan K. Hubbard 	if (!MSDOSFSEOF(pmp, cn)) {
22927a0bc89SDoug Rabson 		if (bp)
23027a0bc89SDoug Rabson 			brelse(bp);
23127a0bc89SDoug Rabson 		if (bnp)
23227a0bc89SDoug Rabson 			*bnp = cntobn(pmp, cn);
23327a0bc89SDoug Rabson 		if (cnp)
23427a0bc89SDoug Rabson 			*cnp = cn;
23527a0bc89SDoug Rabson 		fc_setcache(dep, FC_LASTMAP, i, cn);
236952a6212SJordan K. Hubbard 		return (0);
23727a0bc89SDoug Rabson 	}
23827a0bc89SDoug Rabson 
23927a0bc89SDoug Rabson hiteof:;
24027a0bc89SDoug Rabson 	if (cnp)
24127a0bc89SDoug Rabson 		*cnp = i;
24227a0bc89SDoug Rabson 	if (bp)
24327a0bc89SDoug Rabson 		brelse(bp);
24427a0bc89SDoug Rabson 	/* update last file cluster entry in the fat cache */
24527a0bc89SDoug Rabson 	fc_setcache(dep, FC_LASTFC, i - 1, prevcn);
246952a6212SJordan K. Hubbard 	return (E2BIG);
24727a0bc89SDoug Rabson }
24827a0bc89SDoug Rabson 
24927a0bc89SDoug Rabson /*
25027a0bc89SDoug Rabson  * Find the closest entry in the fat cache to the cluster we are looking
25127a0bc89SDoug Rabson  * for.
25227a0bc89SDoug Rabson  */
2537fefffeeSPoul-Henning Kamp static void
254*10c9700fSEd Maste fc_lookup(struct denode *dep, u_long findcn, u_long *frcnp, u_long *fsrcnp)
25527a0bc89SDoug Rabson {
25627a0bc89SDoug Rabson 	int i;
25727a0bc89SDoug Rabson 	u_long cn;
25827a0bc89SDoug Rabson 	struct fatcache *closest = 0;
25927a0bc89SDoug Rabson 
260ef6a2be3SKonstantin Belousov 	ASSERT_VOP_LOCKED(DETOV(dep), "fc_lookup");
261ef6a2be3SKonstantin Belousov 
26227a0bc89SDoug Rabson 	for (i = 0; i < FC_SIZE; i++) {
26327a0bc89SDoug Rabson 		cn = dep->de_fc[i].fc_frcn;
26427a0bc89SDoug Rabson 		if (cn != FCE_EMPTY && cn <= findcn) {
26527a0bc89SDoug Rabson 			if (closest == 0 || cn > closest->fc_frcn)
26627a0bc89SDoug Rabson 				closest = &dep->de_fc[i];
26727a0bc89SDoug Rabson 		}
26827a0bc89SDoug Rabson 	}
26927a0bc89SDoug Rabson 	if (closest) {
27027a0bc89SDoug Rabson 		*frcnp = closest->fc_frcn;
27127a0bc89SDoug Rabson 		*fsrcnp = closest->fc_fsrcn;
27227a0bc89SDoug Rabson 	}
27327a0bc89SDoug Rabson }
27427a0bc89SDoug Rabson 
27527a0bc89SDoug Rabson /*
27627a0bc89SDoug Rabson  * Purge the fat cache in denode dep of all entries relating to file
27727a0bc89SDoug Rabson  * relative cluster frcn and beyond.
27827a0bc89SDoug Rabson  */
279952a6212SJordan K. Hubbard void
280*10c9700fSEd Maste fc_purge(struct denode *dep, u_int frcn)
28127a0bc89SDoug Rabson {
28227a0bc89SDoug Rabson 	int i;
28327a0bc89SDoug Rabson 	struct fatcache *fcp;
28427a0bc89SDoug Rabson 
285ef6a2be3SKonstantin Belousov 	ASSERT_VOP_ELOCKED(DETOV(dep), "fc_purge");
286ef6a2be3SKonstantin Belousov 
28727a0bc89SDoug Rabson 	fcp = dep->de_fc;
28827a0bc89SDoug Rabson 	for (i = 0; i < FC_SIZE; i++, fcp++) {
28927a0bc89SDoug Rabson 		if (fcp->fc_frcn >= frcn)
29027a0bc89SDoug Rabson 			fcp->fc_frcn = FCE_EMPTY;
29127a0bc89SDoug Rabson 	}
29227a0bc89SDoug Rabson }
29327a0bc89SDoug Rabson 
29427a0bc89SDoug Rabson /*
295952a6212SJordan K. Hubbard  * Update the fat.
296952a6212SJordan K. Hubbard  * If mirroring the fat, update all copies, with the first copy as last.
297952a6212SJordan K. Hubbard  * Else update only the current fat (ignoring the others).
29827a0bc89SDoug Rabson  *
29927a0bc89SDoug Rabson  * pmp	 - msdosfsmount structure for filesystem to update
30027a0bc89SDoug Rabson  * bp	 - addr of modified fat block
30127a0bc89SDoug Rabson  * fatbn - block number relative to begin of filesystem of the modified fat block.
30227a0bc89SDoug Rabson  */
303a98ca469SPoul-Henning Kamp static void
304*10c9700fSEd Maste updatefats(struct msdosfsmount *pmp, struct buf *bp, u_long fatbn)
30527a0bc89SDoug Rabson {
30627a0bc89SDoug Rabson 	struct buf *bpn;
30779fb7dd1SKonstantin Belousov 	int cleanfat, i;
30827a0bc89SDoug Rabson 
30927a0bc89SDoug Rabson #ifdef MSDOSFS_DEBUG
310952a6212SJordan K. Hubbard 	printf("updatefats(pmp %p, bp %p, fatbn %lu)\n", pmp, bp, fatbn);
31127a0bc89SDoug Rabson #endif
31227a0bc89SDoug Rabson 
313952a6212SJordan K. Hubbard 	if (pmp->pm_flags & MSDOSFS_FATMIRROR) {
314952a6212SJordan K. Hubbard 		/*
31527a0bc89SDoug Rabson 		 * Now copy the block(s) of the modified fat to the other copies of
31627a0bc89SDoug Rabson 		 * the fat and write them out.  This is faster than reading in the
31727a0bc89SDoug Rabson 		 * other fats and then writing them back out.  This could tie up
31827a0bc89SDoug Rabson 		 * the fat for quite a while. Preventing others from accessing it.
31927a0bc89SDoug Rabson 		 * To prevent us from going after the fat quite so much we use
32027a0bc89SDoug Rabson 		 * delayed writes, unless they specfied "synchronous" when the
32127a0bc89SDoug Rabson 		 * filesystem was mounted.  If synch is asked for then use
32227a0bc89SDoug Rabson 		 * bwrite()'s and really slow things down.
32327a0bc89SDoug Rabson 		 */
32479fb7dd1SKonstantin Belousov 		if (fatbn != pmp->pm_fatblk || FAT12(pmp))
32579fb7dd1SKonstantin Belousov 			cleanfat = 0;
32679fb7dd1SKonstantin Belousov 		else if (FAT16(pmp))
32779fb7dd1SKonstantin Belousov 			cleanfat = 16;
32879fb7dd1SKonstantin Belousov 		else
32979fb7dd1SKonstantin Belousov 			cleanfat = 32;
33027a0bc89SDoug Rabson 		for (i = 1; i < pmp->pm_FATs; i++) {
33127a0bc89SDoug Rabson 			fatbn += pmp->pm_FATsecs;
33227a0bc89SDoug Rabson 			/* getblk() never fails */
3337261f5f6SJeff Roberson 			bpn = getblk(pmp->pm_devvp, fatbn, bp->b_bcount,
3347261f5f6SJeff Roberson 			    0, 0, 0);
33527a0bc89SDoug Rabson 			bcopy(bp->b_data, bpn->b_data, bp->b_bcount);
33679fb7dd1SKonstantin Belousov 			/* Force the clean bit on in the other copies. */
33779fb7dd1SKonstantin Belousov 			if (cleanfat == 16)
33879fb7dd1SKonstantin Belousov 				((u_int8_t *)bpn->b_data)[3] |= 0x80;
33979fb7dd1SKonstantin Belousov 			else if (cleanfat == 32)
34079fb7dd1SKonstantin Belousov 				((u_int8_t *)bpn->b_data)[7] |= 0x08;
34111fca81cSKonstantin Belousov 			if (pmp->pm_mountp->mnt_flag & MNT_SYNCHRONOUS)
34227a0bc89SDoug Rabson 				bwrite(bpn);
34327a0bc89SDoug Rabson 			else
34427a0bc89SDoug Rabson 				bdwrite(bpn);
34527a0bc89SDoug Rabson 		}
346952a6212SJordan K. Hubbard 	}
347952a6212SJordan K. Hubbard 
34827a0bc89SDoug Rabson 	/*
349952a6212SJordan K. Hubbard 	 * Write out the first (or current) fat last.
35027a0bc89SDoug Rabson 	 */
35111fca81cSKonstantin Belousov 	if (pmp->pm_mountp->mnt_flag & MNT_SYNCHRONOUS)
35227a0bc89SDoug Rabson 		bwrite(bp);
35327a0bc89SDoug Rabson 	else
35427a0bc89SDoug Rabson 		bdwrite(bp);
35527a0bc89SDoug Rabson }
35627a0bc89SDoug Rabson 
35727a0bc89SDoug Rabson /*
35827a0bc89SDoug Rabson  * Updating entries in 12 bit fats is a pain in the butt.
35927a0bc89SDoug Rabson  *
36027a0bc89SDoug Rabson  * The following picture shows where nibbles go when moving from a 12 bit
36127a0bc89SDoug Rabson  * cluster number into the appropriate bytes in the FAT.
36227a0bc89SDoug Rabson  *
36327a0bc89SDoug Rabson  *	byte m        byte m+1      byte m+2
36427a0bc89SDoug Rabson  *	+----+----+   +----+----+   +----+----+
36527a0bc89SDoug Rabson  *	|  0    1 |   |  2    3 |   |  4    5 |   FAT bytes
36627a0bc89SDoug Rabson  *	+----+----+   +----+----+   +----+----+
36727a0bc89SDoug Rabson  *
36827a0bc89SDoug Rabson  *	+----+----+----+   +----+----+----+
36927a0bc89SDoug Rabson  *	|  3    0    1 |   |  4    5    2 |
37027a0bc89SDoug Rabson  *	+----+----+----+   +----+----+----+
37127a0bc89SDoug Rabson  *	cluster n  	   cluster n+1
37227a0bc89SDoug Rabson  *
37327a0bc89SDoug Rabson  * Where n is even. m = n + (n >> 2)
37427a0bc89SDoug Rabson  *
37527a0bc89SDoug Rabson  */
376c1087c13SBruce Evans static __inline void
377*10c9700fSEd Maste usemap_alloc(struct msdosfsmount *pmp, u_long cn)
37827a0bc89SDoug Rabson {
379952a6212SJordan K. Hubbard 
3806be1a4ccSKonstantin Belousov 	MSDOSFS_ASSERT_MP_LOCKED(pmp);
381eb739c7cSKonstantin Belousov 
382eb739c7cSKonstantin Belousov 	KASSERT((pmp->pm_inusemap[cn / N_INUSEBITS] & (1 << (cn % N_INUSEBITS)))
383eb739c7cSKonstantin Belousov 	    == 0, ("Allocating used sector %ld %ld %x", cn, cn % N_INUSEBITS,
384eb739c7cSKonstantin Belousov 		(unsigned)pmp->pm_inusemap[cn / N_INUSEBITS]));
385952a6212SJordan K. Hubbard 	pmp->pm_inusemap[cn / N_INUSEBITS] |= 1 << (cn % N_INUSEBITS);
386eb739c7cSKonstantin Belousov 	KASSERT(pmp->pm_freeclustercount > 0, ("usemap_alloc: too little"));
38727a0bc89SDoug Rabson 	pmp->pm_freeclustercount--;
388bb7ca822SKonstantin Belousov 	pmp->pm_flags |= MSDOSFS_FSIMOD;
38927a0bc89SDoug Rabson }
39027a0bc89SDoug Rabson 
391c1087c13SBruce Evans static __inline void
392*10c9700fSEd Maste usemap_free(struct msdosfsmount *pmp, u_long cn)
39327a0bc89SDoug Rabson {
394952a6212SJordan K. Hubbard 
3956be1a4ccSKonstantin Belousov 	MSDOSFS_ASSERT_MP_LOCKED(pmp);
39627a0bc89SDoug Rabson 	pmp->pm_freeclustercount++;
397bb7ca822SKonstantin Belousov 	pmp->pm_flags |= MSDOSFS_FSIMOD;
398eb739c7cSKonstantin Belousov 	KASSERT((pmp->pm_inusemap[cn / N_INUSEBITS] & (1 << (cn % N_INUSEBITS)))
399eb739c7cSKonstantin Belousov 	    != 0, ("Freeing unused sector %ld %ld %x", cn, cn % N_INUSEBITS,
400eb739c7cSKonstantin Belousov 		(unsigned)pmp->pm_inusemap[cn / N_INUSEBITS]));
40127a0bc89SDoug Rabson 	pmp->pm_inusemap[cn / N_INUSEBITS] &= ~(1 << (cn % N_INUSEBITS));
40227a0bc89SDoug Rabson }
40327a0bc89SDoug Rabson 
40427a0bc89SDoug Rabson int
405*10c9700fSEd Maste clusterfree(struct msdosfsmount *pmp, u_long cluster, u_long *oldcnp)
40627a0bc89SDoug Rabson {
40727a0bc89SDoug Rabson 	int error;
40827a0bc89SDoug Rabson 	u_long oldcn;
40927a0bc89SDoug Rabson 
41027a0bc89SDoug Rabson 	error = fatentry(FAT_GET_AND_SET, pmp, cluster, &oldcn, MSDOSFSFREE);
4116be1a4ccSKonstantin Belousov 	if (error)
412952a6212SJordan K. Hubbard 		return (error);
41327a0bc89SDoug Rabson 	/*
41427a0bc89SDoug Rabson 	 * If the cluster was successfully marked free, then update
41527a0bc89SDoug Rabson 	 * the count of free clusters, and turn off the "allocated"
41627a0bc89SDoug Rabson 	 * bit in the "in use" cluster bit map.
41727a0bc89SDoug Rabson 	 */
4186be1a4ccSKonstantin Belousov 	MSDOSFS_LOCK_MP(pmp);
4196be1a4ccSKonstantin Belousov 	usemap_free(pmp, cluster);
4206be1a4ccSKonstantin Belousov 	MSDOSFS_UNLOCK_MP(pmp);
42127a0bc89SDoug Rabson 	if (oldcnp)
42227a0bc89SDoug Rabson 		*oldcnp = oldcn;
423952a6212SJordan K. Hubbard 	return (0);
42427a0bc89SDoug Rabson }
42527a0bc89SDoug Rabson 
42627a0bc89SDoug Rabson /*
42727a0bc89SDoug Rabson  * Get or Set or 'Get and Set' the cluster'th entry in the fat.
42827a0bc89SDoug Rabson  *
42927a0bc89SDoug Rabson  * function	- whether to get or set a fat entry
43027a0bc89SDoug Rabson  * pmp		- address of the msdosfsmount structure for the filesystem
43127a0bc89SDoug Rabson  *		  whose fat is to be manipulated.
43227a0bc89SDoug Rabson  * cn		- which cluster is of interest
43327a0bc89SDoug Rabson  * oldcontents	- address of a word that is to receive the contents of the
43427a0bc89SDoug Rabson  *		  cluster'th entry if this is a get function
43527a0bc89SDoug Rabson  * newcontents	- the new value to be written into the cluster'th element of
43627a0bc89SDoug Rabson  *		  the fat if this is a set function.
43727a0bc89SDoug Rabson  *
43827a0bc89SDoug Rabson  * This function can also be used to free a cluster by setting the fat entry
43927a0bc89SDoug Rabson  * for a cluster to 0.
44027a0bc89SDoug Rabson  *
44127a0bc89SDoug Rabson  * All copies of the fat are updated if this is a set function. NOTE: If
44227a0bc89SDoug Rabson  * fatentry() marks a cluster as free it does not update the inusemap in
44327a0bc89SDoug Rabson  * the msdosfsmount structure. This is left to the caller.
44427a0bc89SDoug Rabson  */
44527a0bc89SDoug Rabson int
446*10c9700fSEd Maste fatentry(int function, struct msdosfsmount *pmp, u_long cn, u_long *oldcontents,   u_long  newcontents)
44727a0bc89SDoug Rabson {
44827a0bc89SDoug Rabson 	int error;
44927a0bc89SDoug Rabson 	u_long readcn;
45027a0bc89SDoug Rabson 	u_long bn, bo, bsize, byteoffset;
45127a0bc89SDoug Rabson 	struct buf *bp;
45227a0bc89SDoug Rabson 
453952a6212SJordan K. Hubbard #ifdef	MSDOSFS_DEBUG
454952a6212SJordan K. Hubbard 	printf("fatentry(func %d, pmp %p, clust %lu, oldcon %p, newcon %lx)\n",
455952a6212SJordan K. Hubbard 	    function, pmp, cn, oldcontents, newcontents);
456952a6212SJordan K. Hubbard #endif
45727a0bc89SDoug Rabson 
45827a0bc89SDoug Rabson #ifdef DIAGNOSTIC
45927a0bc89SDoug Rabson 	/*
46027a0bc89SDoug Rabson 	 * Be sure they asked us to do something.
46127a0bc89SDoug Rabson 	 */
46227a0bc89SDoug Rabson 	if ((function & (FAT_SET | FAT_GET)) == 0) {
46354cf9198SKonstantin Belousov #ifdef MSDOSFS_DEBUG
46427a0bc89SDoug Rabson 		printf("fatentry(): function code doesn't specify get or set\n");
46554cf9198SKonstantin Belousov #endif
466952a6212SJordan K. Hubbard 		return (EINVAL);
46727a0bc89SDoug Rabson 	}
46827a0bc89SDoug Rabson 
46927a0bc89SDoug Rabson 	/*
47027a0bc89SDoug Rabson 	 * If they asked us to return a cluster number but didn't tell us
47127a0bc89SDoug Rabson 	 * where to put it, give them an error.
47227a0bc89SDoug Rabson 	 */
47327a0bc89SDoug Rabson 	if ((function & FAT_GET) && oldcontents == NULL) {
47454cf9198SKonstantin Belousov #ifdef MSDOSFS_DEBUG
47527a0bc89SDoug Rabson 		printf("fatentry(): get function with no place to put result\n");
47654cf9198SKonstantin Belousov #endif
477952a6212SJordan K. Hubbard 		return (EINVAL);
47827a0bc89SDoug Rabson 	}
47927a0bc89SDoug Rabson #endif
48027a0bc89SDoug Rabson 
48127a0bc89SDoug Rabson 	/*
48227a0bc89SDoug Rabson 	 * Be sure the requested cluster is in the filesystem.
48327a0bc89SDoug Rabson 	 */
48427a0bc89SDoug Rabson 	if (cn < CLUST_FIRST || cn > pmp->pm_maxcluster)
485952a6212SJordan K. Hubbard 		return (EINVAL);
48627a0bc89SDoug Rabson 
48727a0bc89SDoug Rabson 	byteoffset = FATOFS(pmp, cn);
48827a0bc89SDoug Rabson 	fatblock(pmp, byteoffset, &bn, &bsize, &bo);
489c3c6d51eSPoul-Henning Kamp 	error = bread(pmp->pm_devvp, bn, bsize, NOCRED, &bp);
490952a6212SJordan K. Hubbard 	if (error) {
491952a6212SJordan K. Hubbard 		brelse(bp);
492952a6212SJordan K. Hubbard 		return (error);
493952a6212SJordan K. Hubbard 	}
49427a0bc89SDoug Rabson 
49527a0bc89SDoug Rabson 	if (function & FAT_GET) {
496952a6212SJordan K. Hubbard 		if (FAT32(pmp))
497952a6212SJordan K. Hubbard 			readcn = getulong(&bp->b_data[bo]);
498952a6212SJordan K. Hubbard 		else
49927a0bc89SDoug Rabson 			readcn = getushort(&bp->b_data[bo]);
500952a6212SJordan K. Hubbard 		if (FAT12(pmp) & (cn & 1))
50127a0bc89SDoug Rabson 			readcn >>= 4;
502952a6212SJordan K. Hubbard 		readcn &= pmp->pm_fatmask;
503952a6212SJordan K. Hubbard 		/* map reserved fat entries to same values for all fats */
504952a6212SJordan K. Hubbard 		if ((readcn | ~pmp->pm_fatmask) >= CLUST_RSRVD)
505952a6212SJordan K. Hubbard 			readcn |= ~pmp->pm_fatmask;
50627a0bc89SDoug Rabson 		*oldcontents = readcn;
50727a0bc89SDoug Rabson 	}
50827a0bc89SDoug Rabson 	if (function & FAT_SET) {
509952a6212SJordan K. Hubbard 		switch (pmp->pm_fatmask) {
510952a6212SJordan K. Hubbard 		case FAT12_MASK:
51127a0bc89SDoug Rabson 			readcn = getushort(&bp->b_data[bo]);
51227a0bc89SDoug Rabson 			if (cn & 1) {
51327a0bc89SDoug Rabson 				readcn &= 0x000f;
51427a0bc89SDoug Rabson 				readcn |= newcontents << 4;
51527a0bc89SDoug Rabson 			} else {
51627a0bc89SDoug Rabson 				readcn &= 0xf000;
51727a0bc89SDoug Rabson 				readcn |= newcontents & 0xfff;
51827a0bc89SDoug Rabson 			}
51927a0bc89SDoug Rabson 			putushort(&bp->b_data[bo], readcn);
520952a6212SJordan K. Hubbard 			break;
521952a6212SJordan K. Hubbard 		case FAT16_MASK:
52227a0bc89SDoug Rabson 			putushort(&bp->b_data[bo], newcontents);
523952a6212SJordan K. Hubbard 			break;
524952a6212SJordan K. Hubbard 		case FAT32_MASK:
525952a6212SJordan K. Hubbard 			/*
526952a6212SJordan K. Hubbard 			 * According to spec we have to retain the
527952a6212SJordan K. Hubbard 			 * high order bits of the fat entry.
528952a6212SJordan K. Hubbard 			 */
529952a6212SJordan K. Hubbard 			readcn = getulong(&bp->b_data[bo]);
530952a6212SJordan K. Hubbard 			readcn &= ~FAT32_MASK;
531952a6212SJordan K. Hubbard 			readcn |= newcontents & FAT32_MASK;
532952a6212SJordan K. Hubbard 			putulong(&bp->b_data[bo], readcn);
533952a6212SJordan K. Hubbard 			break;
534952a6212SJordan K. Hubbard 		}
53527a0bc89SDoug Rabson 		updatefats(pmp, bp, bn);
53627a0bc89SDoug Rabson 		bp = NULL;
53727a0bc89SDoug Rabson 		pmp->pm_fmod = 1;
53827a0bc89SDoug Rabson 	}
53927a0bc89SDoug Rabson 	if (bp)
54027a0bc89SDoug Rabson 		brelse(bp);
541952a6212SJordan K. Hubbard 	return (0);
54227a0bc89SDoug Rabson }
54327a0bc89SDoug Rabson 
54427a0bc89SDoug Rabson /*
54527a0bc89SDoug Rabson  * Update a contiguous cluster chain
54627a0bc89SDoug Rabson  *
54727a0bc89SDoug Rabson  * pmp	    - mount point
54827a0bc89SDoug Rabson  * start    - first cluster of chain
54927a0bc89SDoug Rabson  * count    - number of clusters in chain
55027a0bc89SDoug Rabson  * fillwith - what to write into fat entry of last cluster
55127a0bc89SDoug Rabson  */
55227a0bc89SDoug Rabson static int
553*10c9700fSEd Maste fatchain(struct msdosfsmount *pmp, u_long start, u_long count, u_long fillwith)
55427a0bc89SDoug Rabson {
55527a0bc89SDoug Rabson 	int error;
55627a0bc89SDoug Rabson 	u_long bn, bo, bsize, byteoffset, readcn, newc;
55727a0bc89SDoug Rabson 	struct buf *bp;
55827a0bc89SDoug Rabson 
55927a0bc89SDoug Rabson #ifdef MSDOSFS_DEBUG
560952a6212SJordan K. Hubbard 	printf("fatchain(pmp %p, start %lu, count %lu, fillwith %lx)\n",
56127a0bc89SDoug Rabson 	    pmp, start, count, fillwith);
56227a0bc89SDoug Rabson #endif
56327a0bc89SDoug Rabson 	/*
56427a0bc89SDoug Rabson 	 * Be sure the clusters are in the filesystem.
56527a0bc89SDoug Rabson 	 */
56627a0bc89SDoug Rabson 	if (start < CLUST_FIRST || start + count - 1 > pmp->pm_maxcluster)
567952a6212SJordan K. Hubbard 		return (EINVAL);
56827a0bc89SDoug Rabson 
56927a0bc89SDoug Rabson 	while (count > 0) {
57027a0bc89SDoug Rabson 		byteoffset = FATOFS(pmp, start);
57127a0bc89SDoug Rabson 		fatblock(pmp, byteoffset, &bn, &bsize, &bo);
572c3c6d51eSPoul-Henning Kamp 		error = bread(pmp->pm_devvp, bn, bsize, NOCRED, &bp);
573952a6212SJordan K. Hubbard 		if (error) {
574952a6212SJordan K. Hubbard 			brelse(bp);
575952a6212SJordan K. Hubbard 			return (error);
576952a6212SJordan K. Hubbard 		}
57727a0bc89SDoug Rabson 		while (count > 0) {
57827a0bc89SDoug Rabson 			start++;
57927a0bc89SDoug Rabson 			newc = --count > 0 ? start : fillwith;
580952a6212SJordan K. Hubbard 			switch (pmp->pm_fatmask) {
581952a6212SJordan K. Hubbard 			case FAT12_MASK:
58227a0bc89SDoug Rabson 				readcn = getushort(&bp->b_data[bo]);
58327a0bc89SDoug Rabson 				if (start & 1) {
58427a0bc89SDoug Rabson 					readcn &= 0xf000;
58527a0bc89SDoug Rabson 					readcn |= newc & 0xfff;
58627a0bc89SDoug Rabson 				} else {
58727a0bc89SDoug Rabson 					readcn &= 0x000f;
58827a0bc89SDoug Rabson 					readcn |= newc << 4;
58927a0bc89SDoug Rabson 				}
59027a0bc89SDoug Rabson 				putushort(&bp->b_data[bo], readcn);
59127a0bc89SDoug Rabson 				bo++;
59227a0bc89SDoug Rabson 				if (!(start & 1))
59327a0bc89SDoug Rabson 					bo++;
594952a6212SJordan K. Hubbard 				break;
595952a6212SJordan K. Hubbard 			case FAT16_MASK:
59627a0bc89SDoug Rabson 				putushort(&bp->b_data[bo], newc);
59727a0bc89SDoug Rabson 				bo += 2;
598952a6212SJordan K. Hubbard 				break;
599952a6212SJordan K. Hubbard 			case FAT32_MASK:
600952a6212SJordan K. Hubbard 				readcn = getulong(&bp->b_data[bo]);
601952a6212SJordan K. Hubbard 				readcn &= ~pmp->pm_fatmask;
602952a6212SJordan K. Hubbard 				readcn |= newc & pmp->pm_fatmask;
603952a6212SJordan K. Hubbard 				putulong(&bp->b_data[bo], readcn);
604952a6212SJordan K. Hubbard 				bo += 4;
605952a6212SJordan K. Hubbard 				break;
60627a0bc89SDoug Rabson 			}
60727a0bc89SDoug Rabson 			if (bo >= bsize)
60827a0bc89SDoug Rabson 				break;
60927a0bc89SDoug Rabson 		}
61027a0bc89SDoug Rabson 		updatefats(pmp, bp, bn);
61127a0bc89SDoug Rabson 	}
61227a0bc89SDoug Rabson 	pmp->pm_fmod = 1;
613952a6212SJordan K. Hubbard 	return (0);
61427a0bc89SDoug Rabson }
61527a0bc89SDoug Rabson 
61627a0bc89SDoug Rabson /*
61727a0bc89SDoug Rabson  * Check the length of a free cluster chain starting at start.
61827a0bc89SDoug Rabson  *
61927a0bc89SDoug Rabson  * pmp	 - mount point
62027a0bc89SDoug Rabson  * start - start of chain
62127a0bc89SDoug Rabson  * count - maximum interesting length
62227a0bc89SDoug Rabson  */
6237fefffeeSPoul-Henning Kamp static int
624*10c9700fSEd Maste chainlength(struct msdosfsmount *pmp, u_long start, u_long count)
62527a0bc89SDoug Rabson {
62627a0bc89SDoug Rabson 	u_long idx, max_idx;
62727a0bc89SDoug Rabson 	u_int map;
62827a0bc89SDoug Rabson 	u_long len;
62927a0bc89SDoug Rabson 
6306be1a4ccSKonstantin Belousov 	MSDOSFS_ASSERT_MP_LOCKED(pmp);
6316be1a4ccSKonstantin Belousov 
63227a0bc89SDoug Rabson 	max_idx = pmp->pm_maxcluster / N_INUSEBITS;
63327a0bc89SDoug Rabson 	idx = start / N_INUSEBITS;
63427a0bc89SDoug Rabson 	start %= N_INUSEBITS;
63527a0bc89SDoug Rabson 	map = pmp->pm_inusemap[idx];
63627a0bc89SDoug Rabson 	map &= ~((1 << start) - 1);
63727a0bc89SDoug Rabson 	if (map) {
63827a0bc89SDoug Rabson 		len = ffs(map) - 1 - start;
639952a6212SJordan K. Hubbard 		return (len > count ? count : len);
64027a0bc89SDoug Rabson 	}
64127a0bc89SDoug Rabson 	len = N_INUSEBITS - start;
64227a0bc89SDoug Rabson 	if (len >= count)
643952a6212SJordan K. Hubbard 		return (count);
64427a0bc89SDoug Rabson 	while (++idx <= max_idx) {
64527a0bc89SDoug Rabson 		if (len >= count)
64627a0bc89SDoug Rabson 			break;
647c3c6d51eSPoul-Henning Kamp 		map = pmp->pm_inusemap[idx];
648c3c6d51eSPoul-Henning Kamp 		if (map) {
64927a0bc89SDoug Rabson 			len += ffs(map) - 1;
65027a0bc89SDoug Rabson 			break;
65127a0bc89SDoug Rabson 		}
65227a0bc89SDoug Rabson 		len += N_INUSEBITS;
65327a0bc89SDoug Rabson 	}
654952a6212SJordan K. Hubbard 	return (len > count ? count : len);
65527a0bc89SDoug Rabson }
65627a0bc89SDoug Rabson 
65727a0bc89SDoug Rabson /*
65827a0bc89SDoug Rabson  * Allocate contigous free clusters.
65927a0bc89SDoug Rabson  *
66027a0bc89SDoug Rabson  * pmp	      - mount point.
66127a0bc89SDoug Rabson  * start      - start of cluster chain.
66227a0bc89SDoug Rabson  * count      - number of clusters to allocate.
66327a0bc89SDoug Rabson  * fillwith   - put this value into the fat entry for the
66427a0bc89SDoug Rabson  *		last allocated cluster.
66527a0bc89SDoug Rabson  * retcluster - put the first allocated cluster's number here.
66627a0bc89SDoug Rabson  * got	      - how many clusters were actually allocated.
66727a0bc89SDoug Rabson  */
6687fefffeeSPoul-Henning Kamp static int
669*10c9700fSEd Maste chainalloc(struct msdosfsmount *pmp, u_long start, u_long count,
670*10c9700fSEd Maste     u_long fillwith, u_long *retcluster, u_long *got)
67127a0bc89SDoug Rabson {
67227a0bc89SDoug Rabson 	int error;
673952a6212SJordan K. Hubbard 	u_long cl, n;
674952a6212SJordan K. Hubbard 
6756be1a4ccSKonstantin Belousov 	MSDOSFS_ASSERT_MP_LOCKED(pmp);
6766be1a4ccSKonstantin Belousov 
677952a6212SJordan K. Hubbard 	for (cl = start, n = count; n-- > 0;)
678952a6212SJordan K. Hubbard 		usemap_alloc(pmp, cl++);
679bb7ca822SKonstantin Belousov 	pmp->pm_nxtfree = start + count;
680bb7ca822SKonstantin Belousov 	if (pmp->pm_nxtfree > pmp->pm_maxcluster)
681bb7ca822SKonstantin Belousov 		pmp->pm_nxtfree = CLUST_FIRST;
682bb7ca822SKonstantin Belousov 	pmp->pm_flags |= MSDOSFS_FSIMOD;
68327a0bc89SDoug Rabson 	error = fatchain(pmp, start, count, fillwith);
684952a6212SJordan K. Hubbard 	if (error != 0)
685952a6212SJordan K. Hubbard 		return (error);
68627a0bc89SDoug Rabson #ifdef MSDOSFS_DEBUG
687952a6212SJordan K. Hubbard 	printf("clusteralloc(): allocated cluster chain at %lu (%lu clusters)\n",
68827a0bc89SDoug Rabson 	    start, count);
68927a0bc89SDoug Rabson #endif
69027a0bc89SDoug Rabson 	if (retcluster)
69127a0bc89SDoug Rabson 		*retcluster = start;
69227a0bc89SDoug Rabson 	if (got)
69327a0bc89SDoug Rabson 		*got = count;
694952a6212SJordan K. Hubbard 	return (0);
69527a0bc89SDoug Rabson }
69627a0bc89SDoug Rabson 
69727a0bc89SDoug Rabson /*
69827a0bc89SDoug Rabson  * Allocate contiguous free clusters.
69927a0bc89SDoug Rabson  *
70027a0bc89SDoug Rabson  * pmp	      - mount point.
70127a0bc89SDoug Rabson  * start      - preferred start of cluster chain.
70227a0bc89SDoug Rabson  * count      - number of clusters requested.
70327a0bc89SDoug Rabson  * fillwith   - put this value into the fat entry for the
70427a0bc89SDoug Rabson  *		last allocated cluster.
70527a0bc89SDoug Rabson  * retcluster - put the first allocated cluster's number here.
70627a0bc89SDoug Rabson  * got	      - how many clusters were actually allocated.
70727a0bc89SDoug Rabson  */
70827a0bc89SDoug Rabson int
7096be1a4ccSKonstantin Belousov clusteralloc(struct msdosfsmount *pmp, u_long start, u_long count,
7106be1a4ccSKonstantin Belousov     u_long fillwith, u_long *retcluster, u_long *got)
7116be1a4ccSKonstantin Belousov {
7126be1a4ccSKonstantin Belousov 	int error;
7136be1a4ccSKonstantin Belousov 
7146be1a4ccSKonstantin Belousov 	MSDOSFS_LOCK_MP(pmp);
7156be1a4ccSKonstantin Belousov 	error = clusteralloc1(pmp, start, count, fillwith, retcluster, got);
7166be1a4ccSKonstantin Belousov 	MSDOSFS_UNLOCK_MP(pmp);
7176be1a4ccSKonstantin Belousov 	return (error);
7186be1a4ccSKonstantin Belousov }
7196be1a4ccSKonstantin Belousov 
7206be1a4ccSKonstantin Belousov static int
7216be1a4ccSKonstantin Belousov clusteralloc1(struct msdosfsmount *pmp, u_long start, u_long count,
7226be1a4ccSKonstantin Belousov     u_long fillwith, u_long *retcluster, u_long *got)
72327a0bc89SDoug Rabson {
72427a0bc89SDoug Rabson 	u_long idx;
725952a6212SJordan K. Hubbard 	u_long len, newst, foundl, cn, l;
726952a6212SJordan K. Hubbard 	u_long foundcn = 0; /* XXX: foundcn could be used unititialized */
72727a0bc89SDoug Rabson 	u_int map;
72827a0bc89SDoug Rabson 
7296be1a4ccSKonstantin Belousov 	MSDOSFS_ASSERT_MP_LOCKED(pmp);
7306be1a4ccSKonstantin Belousov 
73127a0bc89SDoug Rabson #ifdef MSDOSFS_DEBUG
732952a6212SJordan K. Hubbard 	printf("clusteralloc(): find %lu clusters\n", count);
73327a0bc89SDoug Rabson #endif
73427a0bc89SDoug Rabson 	if (start) {
73527a0bc89SDoug Rabson 		if ((len = chainlength(pmp, start, count)) >= count)
736952a6212SJordan K. Hubbard 			return (chainalloc(pmp, start, count, fillwith, retcluster, got));
7377cd5051bSPoul-Henning Kamp 	} else
73827a0bc89SDoug Rabson 		len = 0;
73927a0bc89SDoug Rabson 
7408e55bfafSBruce Evans 	newst = pmp->pm_nxtfree;
74127a0bc89SDoug Rabson 	foundl = 0;
74227a0bc89SDoug Rabson 
74327a0bc89SDoug Rabson 	for (cn = newst; cn <= pmp->pm_maxcluster;) {
74427a0bc89SDoug Rabson 		idx = cn / N_INUSEBITS;
74527a0bc89SDoug Rabson 		map = pmp->pm_inusemap[idx];
74627a0bc89SDoug Rabson 		map |= (1 << (cn % N_INUSEBITS)) - 1;
74727a0bc89SDoug Rabson 		if (map != (u_int)-1) {
74827a0bc89SDoug Rabson 			cn = idx * N_INUSEBITS + ffs(map^(u_int)-1) - 1;
74927a0bc89SDoug Rabson 			if ((l = chainlength(pmp, cn, count)) >= count)
750952a6212SJordan K. Hubbard 				return (chainalloc(pmp, cn, count, fillwith, retcluster, got));
75127a0bc89SDoug Rabson 			if (l > foundl) {
75227a0bc89SDoug Rabson 				foundcn = cn;
75327a0bc89SDoug Rabson 				foundl = l;
75427a0bc89SDoug Rabson 			}
75527a0bc89SDoug Rabson 			cn += l + 1;
75627a0bc89SDoug Rabson 			continue;
75727a0bc89SDoug Rabson 		}
75827a0bc89SDoug Rabson 		cn += N_INUSEBITS - cn % N_INUSEBITS;
75927a0bc89SDoug Rabson 	}
76027a0bc89SDoug Rabson 	for (cn = 0; cn < newst;) {
76127a0bc89SDoug Rabson 		idx = cn / N_INUSEBITS;
76227a0bc89SDoug Rabson 		map = pmp->pm_inusemap[idx];
76327a0bc89SDoug Rabson 		map |= (1 << (cn % N_INUSEBITS)) - 1;
76427a0bc89SDoug Rabson 		if (map != (u_int)-1) {
76527a0bc89SDoug Rabson 			cn = idx * N_INUSEBITS + ffs(map^(u_int)-1) - 1;
76627a0bc89SDoug Rabson 			if ((l = chainlength(pmp, cn, count)) >= count)
767952a6212SJordan K. Hubbard 				return (chainalloc(pmp, cn, count, fillwith, retcluster, got));
76827a0bc89SDoug Rabson 			if (l > foundl) {
76927a0bc89SDoug Rabson 				foundcn = cn;
77027a0bc89SDoug Rabson 				foundl = l;
77127a0bc89SDoug Rabson 			}
77227a0bc89SDoug Rabson 			cn += l + 1;
77327a0bc89SDoug Rabson 			continue;
77427a0bc89SDoug Rabson 		}
77527a0bc89SDoug Rabson 		cn += N_INUSEBITS - cn % N_INUSEBITS;
77627a0bc89SDoug Rabson 	}
77727a0bc89SDoug Rabson 
77827a0bc89SDoug Rabson 	if (!foundl)
779952a6212SJordan K. Hubbard 		return (ENOSPC);
78027a0bc89SDoug Rabson 
78127a0bc89SDoug Rabson 	if (len)
782952a6212SJordan K. Hubbard 		return (chainalloc(pmp, start, len, fillwith, retcluster, got));
78327a0bc89SDoug Rabson 	else
784952a6212SJordan K. Hubbard 		return (chainalloc(pmp, foundcn, foundl, fillwith, retcluster, got));
78527a0bc89SDoug Rabson }
78627a0bc89SDoug Rabson 
78727a0bc89SDoug Rabson 
78827a0bc89SDoug Rabson /*
78927a0bc89SDoug Rabson  * Free a chain of clusters.
79027a0bc89SDoug Rabson  *
79127a0bc89SDoug Rabson  * pmp		- address of the msdosfs mount structure for the filesystem
79227a0bc89SDoug Rabson  *		  containing the cluster chain to be freed.
79327a0bc89SDoug Rabson  * startcluster - number of the 1st cluster in the chain of clusters to be
79427a0bc89SDoug Rabson  *		  freed.
79527a0bc89SDoug Rabson  */
79627a0bc89SDoug Rabson int
797*10c9700fSEd Maste freeclusterchain(struct msdosfsmount *pmp, u_long cluster)
79827a0bc89SDoug Rabson {
799952a6212SJordan K. Hubbard 	int error;
80027a0bc89SDoug Rabson 	struct buf *bp = NULL;
80127a0bc89SDoug Rabson 	u_long bn, bo, bsize, byteoffset;
80227a0bc89SDoug Rabson 	u_long readcn, lbn = -1;
80327a0bc89SDoug Rabson 
8046be1a4ccSKonstantin Belousov 	MSDOSFS_LOCK_MP(pmp);
80527a0bc89SDoug Rabson 	while (cluster >= CLUST_FIRST && cluster <= pmp->pm_maxcluster) {
80627a0bc89SDoug Rabson 		byteoffset = FATOFS(pmp, cluster);
80727a0bc89SDoug Rabson 		fatblock(pmp, byteoffset, &bn, &bsize, &bo);
80827a0bc89SDoug Rabson 		if (lbn != bn) {
80927a0bc89SDoug Rabson 			if (bp)
81024d4540cSBruce Evans 				updatefats(pmp, bp, lbn);
811c3c6d51eSPoul-Henning Kamp 			error = bread(pmp->pm_devvp, bn, bsize, NOCRED, &bp);
812952a6212SJordan K. Hubbard 			if (error) {
813952a6212SJordan K. Hubbard 				brelse(bp);
8146be1a4ccSKonstantin Belousov 				MSDOSFS_UNLOCK_MP(pmp);
815952a6212SJordan K. Hubbard 				return (error);
816952a6212SJordan K. Hubbard 			}
81727a0bc89SDoug Rabson 			lbn = bn;
81827a0bc89SDoug Rabson 		}
81927a0bc89SDoug Rabson 		usemap_free(pmp, cluster);
820952a6212SJordan K. Hubbard 		switch (pmp->pm_fatmask) {
821952a6212SJordan K. Hubbard 		case FAT12_MASK:
82227a0bc89SDoug Rabson 			readcn = getushort(&bp->b_data[bo]);
82327a0bc89SDoug Rabson 			if (cluster & 1) {
82427a0bc89SDoug Rabson 				cluster = readcn >> 4;
82527a0bc89SDoug Rabson 				readcn &= 0x000f;
82627a0bc89SDoug Rabson 				readcn |= MSDOSFSFREE << 4;
82727a0bc89SDoug Rabson 			} else {
82827a0bc89SDoug Rabson 				cluster = readcn;
82927a0bc89SDoug Rabson 				readcn &= 0xf000;
83027a0bc89SDoug Rabson 				readcn |= MSDOSFSFREE & 0xfff;
83127a0bc89SDoug Rabson 			}
83227a0bc89SDoug Rabson 			putushort(&bp->b_data[bo], readcn);
833952a6212SJordan K. Hubbard 			break;
834952a6212SJordan K. Hubbard 		case FAT16_MASK:
835952a6212SJordan K. Hubbard 			cluster = getushort(&bp->b_data[bo]);
83627a0bc89SDoug Rabson 			putushort(&bp->b_data[bo], MSDOSFSFREE);
837952a6212SJordan K. Hubbard 			break;
838952a6212SJordan K. Hubbard 		case FAT32_MASK:
839952a6212SJordan K. Hubbard 			cluster = getulong(&bp->b_data[bo]);
840952a6212SJordan K. Hubbard 			putulong(&bp->b_data[bo],
841952a6212SJordan K. Hubbard 				 (MSDOSFSFREE & FAT32_MASK) | (cluster & ~FAT32_MASK));
842952a6212SJordan K. Hubbard 			break;
84327a0bc89SDoug Rabson 		}
844952a6212SJordan K. Hubbard 		cluster &= pmp->pm_fatmask;
845952a6212SJordan K. Hubbard 		if ((cluster | ~pmp->pm_fatmask) >= CLUST_RSRVD)
846952a6212SJordan K. Hubbard 			cluster |= pmp->pm_fatmask;
84727a0bc89SDoug Rabson 	}
84827a0bc89SDoug Rabson 	if (bp)
84927a0bc89SDoug Rabson 		updatefats(pmp, bp, bn);
8506be1a4ccSKonstantin Belousov 	MSDOSFS_UNLOCK_MP(pmp);
851952a6212SJordan K. Hubbard 	return (0);
85227a0bc89SDoug Rabson }
85327a0bc89SDoug Rabson 
85427a0bc89SDoug Rabson /*
85527a0bc89SDoug Rabson  * Read in fat blocks looking for free clusters. For every free cluster
85627a0bc89SDoug Rabson  * found turn off its corresponding bit in the pm_inusemap.
85727a0bc89SDoug Rabson  */
85827a0bc89SDoug Rabson int
859*10c9700fSEd Maste fillinusemap(struct msdosfsmount *pmp)
86027a0bc89SDoug Rabson {
86127a0bc89SDoug Rabson 	struct buf *bp = NULL;
86227a0bc89SDoug Rabson 	u_long cn, readcn;
86327a0bc89SDoug Rabson 	int error;
86427a0bc89SDoug Rabson 	u_long bn, bo, bsize, byteoffset;
86527a0bc89SDoug Rabson 
8666be1a4ccSKonstantin Belousov 	MSDOSFS_ASSERT_MP_LOCKED(pmp);
8676be1a4ccSKonstantin Belousov 
86827a0bc89SDoug Rabson 	/*
86927a0bc89SDoug Rabson 	 * Mark all clusters in use, we mark the free ones in the fat scan
87027a0bc89SDoug Rabson 	 * loop further down.
87127a0bc89SDoug Rabson 	 */
87227a0bc89SDoug Rabson 	for (cn = 0; cn < (pmp->pm_maxcluster + N_INUSEBITS) / N_INUSEBITS; cn++)
87327a0bc89SDoug Rabson 		pmp->pm_inusemap[cn] = (u_int)-1;
87427a0bc89SDoug Rabson 
87527a0bc89SDoug Rabson 	/*
87627a0bc89SDoug Rabson 	 * Figure how many free clusters are in the filesystem by ripping
87727a0bc89SDoug Rabson 	 * through the fat counting the number of entries whose content is
87827a0bc89SDoug Rabson 	 * zero.  These represent free clusters.
87927a0bc89SDoug Rabson 	 */
88027a0bc89SDoug Rabson 	pmp->pm_freeclustercount = 0;
88127a0bc89SDoug Rabson 	for (cn = CLUST_FIRST; cn <= pmp->pm_maxcluster; cn++) {
88227a0bc89SDoug Rabson 		byteoffset = FATOFS(pmp, cn);
88327a0bc89SDoug Rabson 		bo = byteoffset % pmp->pm_fatblocksize;
88427a0bc89SDoug Rabson 		if (!bo || !bp) {
88527a0bc89SDoug Rabson 			/* Read new FAT block */
88627a0bc89SDoug Rabson 			if (bp)
88727a0bc89SDoug Rabson 				brelse(bp);
88827a0bc89SDoug Rabson 			fatblock(pmp, byteoffset, &bn, &bsize, NULL);
88927a0bc89SDoug Rabson 			error = bread(pmp->pm_devvp, bn, bsize, NOCRED, &bp);
890952a6212SJordan K. Hubbard 			if (error) {
891952a6212SJordan K. Hubbard 				brelse(bp);
892952a6212SJordan K. Hubbard 				return (error);
89327a0bc89SDoug Rabson 			}
894952a6212SJordan K. Hubbard 		}
895952a6212SJordan K. Hubbard 		if (FAT32(pmp))
896952a6212SJordan K. Hubbard 			readcn = getulong(&bp->b_data[bo]);
897952a6212SJordan K. Hubbard 		else
89827a0bc89SDoug Rabson 			readcn = getushort(&bp->b_data[bo]);
899952a6212SJordan K. Hubbard 		if (FAT12(pmp) && (cn & 1))
90027a0bc89SDoug Rabson 			readcn >>= 4;
901952a6212SJordan K. Hubbard 		readcn &= pmp->pm_fatmask;
90227a0bc89SDoug Rabson 
90327a0bc89SDoug Rabson 		if (readcn == 0)
90427a0bc89SDoug Rabson 			usemap_free(pmp, cn);
90527a0bc89SDoug Rabson 	}
9063c8b687fSKonstantin Belousov 	if (bp != NULL)
90727a0bc89SDoug Rabson 		brelse(bp);
908952a6212SJordan K. Hubbard 	return (0);
90927a0bc89SDoug Rabson }
91027a0bc89SDoug Rabson 
91127a0bc89SDoug Rabson /*
91227a0bc89SDoug Rabson  * Allocate a new cluster and chain it onto the end of the file.
91327a0bc89SDoug Rabson  *
91427a0bc89SDoug Rabson  * dep	 - the file to extend
91527a0bc89SDoug Rabson  * count - number of clusters to allocate
91627a0bc89SDoug Rabson  * bpp	 - where to return the address of the buf header for the first new
91727a0bc89SDoug Rabson  *	   file block
91827a0bc89SDoug Rabson  * ncp	 - where to put cluster number of the first newly allocated cluster
91927a0bc89SDoug Rabson  *	   If this pointer is 0, do not return the cluster number.
92027a0bc89SDoug Rabson  * flags - see fat.h
92127a0bc89SDoug Rabson  *
92227a0bc89SDoug Rabson  * NOTE: This function is not responsible for turning on the DE_UPDATE bit of
92327a0bc89SDoug Rabson  * the de_flag field of the denode and it does not change the de_FileSize
92427a0bc89SDoug Rabson  * field.  This is left for the caller to do.
92527a0bc89SDoug Rabson  */
92627a0bc89SDoug Rabson int
927*10c9700fSEd Maste extendfile(struct denode *dep, u_long count, struct buf **bpp, u_long *ncp,
928*10c9700fSEd Maste     int flags)
92927a0bc89SDoug Rabson {
930952a6212SJordan K. Hubbard 	int error;
93127a0bc89SDoug Rabson 	u_long frcn;
93227a0bc89SDoug Rabson 	u_long cn, got;
93327a0bc89SDoug Rabson 	struct msdosfsmount *pmp = dep->de_pmp;
93427a0bc89SDoug Rabson 	struct buf *bp;
9350d2af521SKirk McKusick 	daddr_t blkno;
93627a0bc89SDoug Rabson 
93727a0bc89SDoug Rabson 	/*
93827a0bc89SDoug Rabson 	 * Don't try to extend the root directory
93927a0bc89SDoug Rabson 	 */
940952a6212SJordan K. Hubbard 	if (dep->de_StartCluster == MSDOSFSROOT
941952a6212SJordan K. Hubbard 	    && (dep->de_Attributes & ATTR_DIRECTORY)) {
94254cf9198SKonstantin Belousov #ifdef MSDOSFS_DEBUG
94327a0bc89SDoug Rabson 		printf("extendfile(): attempt to extend root directory\n");
94454cf9198SKonstantin Belousov #endif
945952a6212SJordan K. Hubbard 		return (ENOSPC);
94627a0bc89SDoug Rabson 	}
94727a0bc89SDoug Rabson 
94827a0bc89SDoug Rabson 	/*
94927a0bc89SDoug Rabson 	 * If the "file's last cluster" cache entry is empty, and the file
95027a0bc89SDoug Rabson 	 * is not empty, then fill the cache entry by calling pcbmap().
95127a0bc89SDoug Rabson 	 */
95227a0bc89SDoug Rabson 	if (dep->de_fc[FC_LASTFC].fc_frcn == FCE_EMPTY &&
95327a0bc89SDoug Rabson 	    dep->de_StartCluster != 0) {
954952a6212SJordan K. Hubbard 		error = pcbmap(dep, 0xffff, 0, &cn, 0);
95527a0bc89SDoug Rabson 		/* we expect it to return E2BIG */
95627a0bc89SDoug Rabson 		if (error != E2BIG)
957952a6212SJordan K. Hubbard 			return (error);
95827a0bc89SDoug Rabson 	}
95927a0bc89SDoug Rabson 
960ededffc0STom Rhodes 	dep->de_fc[FC_NEXTTOLASTFC].fc_frcn =
961ededffc0STom Rhodes 	    dep->de_fc[FC_LASTFC].fc_frcn;
962ededffc0STom Rhodes 	dep->de_fc[FC_NEXTTOLASTFC].fc_fsrcn =
963ededffc0STom Rhodes 	    dep->de_fc[FC_LASTFC].fc_fsrcn;
96427a0bc89SDoug Rabson 	while (count > 0) {
96527a0bc89SDoug Rabson 		/*
966952a6212SJordan K. Hubbard 		 * Allocate a new cluster chain and cat onto the end of the
967952a6212SJordan K. Hubbard 		 * file.  * If the file is empty we make de_StartCluster point
968952a6212SJordan K. Hubbard 		 * to the new block.  Note that de_StartCluster being 0 is
969952a6212SJordan K. Hubbard 		 * sufficient to be sure the file is empty since we exclude
970952a6212SJordan K. Hubbard 		 * attempts to extend the root directory above, and the root
971952a6212SJordan K. Hubbard 		 * dir is the only file with a startcluster of 0 that has
972952a6212SJordan K. Hubbard 		 * blocks allocated (sort of).
97327a0bc89SDoug Rabson 		 */
97427a0bc89SDoug Rabson 		if (dep->de_StartCluster == 0)
97527a0bc89SDoug Rabson 			cn = 0;
97627a0bc89SDoug Rabson 		else
97727a0bc89SDoug Rabson 			cn = dep->de_fc[FC_LASTFC].fc_fsrcn + 1;
978c3c6d51eSPoul-Henning Kamp 		error = clusteralloc(pmp, cn, count, CLUST_EOFE, &cn, &got);
979c3c6d51eSPoul-Henning Kamp 		if (error)
980952a6212SJordan K. Hubbard 			return (error);
98127a0bc89SDoug Rabson 
98227a0bc89SDoug Rabson 		count -= got;
98327a0bc89SDoug Rabson 
98427a0bc89SDoug Rabson 		/*
98527a0bc89SDoug Rabson 		 * Give them the filesystem relative cluster number if they want
98627a0bc89SDoug Rabson 		 * it.
98727a0bc89SDoug Rabson 		 */
98827a0bc89SDoug Rabson 		if (ncp) {
98927a0bc89SDoug Rabson 			*ncp = cn;
99027a0bc89SDoug Rabson 			ncp = NULL;
99127a0bc89SDoug Rabson 		}
99227a0bc89SDoug Rabson 
99327a0bc89SDoug Rabson 		if (dep->de_StartCluster == 0) {
99427a0bc89SDoug Rabson 			dep->de_StartCluster = cn;
99527a0bc89SDoug Rabson 			frcn = 0;
99627a0bc89SDoug Rabson 		} else {
997952a6212SJordan K. Hubbard 			error = fatentry(FAT_SET, pmp,
998952a6212SJordan K. Hubbard 					 dep->de_fc[FC_LASTFC].fc_fsrcn,
99927a0bc89SDoug Rabson 					 0, cn);
100027a0bc89SDoug Rabson 			if (error) {
100127a0bc89SDoug Rabson 				clusterfree(pmp, cn, NULL);
1002952a6212SJordan K. Hubbard 				return (error);
100327a0bc89SDoug Rabson 			}
100427a0bc89SDoug Rabson 			frcn = dep->de_fc[FC_LASTFC].fc_frcn + 1;
100527a0bc89SDoug Rabson 		}
100627a0bc89SDoug Rabson 
100727a0bc89SDoug Rabson 		/*
100827a0bc89SDoug Rabson 		 * Update the "last cluster of the file" entry in the denode's fat
100927a0bc89SDoug Rabson 		 * cache.
101027a0bc89SDoug Rabson 		 */
101127a0bc89SDoug Rabson 		fc_setcache(dep, FC_LASTFC, frcn + got - 1, cn + got - 1);
101227a0bc89SDoug Rabson 
101327a0bc89SDoug Rabson 		if (flags & DE_CLEAR) {
101427a0bc89SDoug Rabson 			while (got-- > 0) {
101527a0bc89SDoug Rabson 				/*
101627a0bc89SDoug Rabson 				 * Get the buf header for the new block of the file.
101727a0bc89SDoug Rabson 				 */
101827a0bc89SDoug Rabson 				if (dep->de_Attributes & ATTR_DIRECTORY)
10197261f5f6SJeff Roberson 					bp = getblk(pmp->pm_devvp,
10207261f5f6SJeff Roberson 					    cntobn(pmp, cn++),
10217261f5f6SJeff Roberson 					    pmp->pm_bpcluster, 0, 0, 0);
102227a0bc89SDoug Rabson 				else {
10237261f5f6SJeff Roberson 					bp = getblk(DETOV(dep),
102467c7bbf3SKonstantin Belousov 					    frcn++,
10257261f5f6SJeff Roberson 					    pmp->pm_bpcluster, 0, 0, 0);
102627a0bc89SDoug Rabson 					/*
102727a0bc89SDoug Rabson 					 * Do the bmap now, as in msdosfs_write
102827a0bc89SDoug Rabson 					 */
1029952a6212SJordan K. Hubbard 					if (pcbmap(dep,
103067c7bbf3SKonstantin Belousov 					    bp->b_lblkno,
10310d2af521SKirk McKusick 					    &blkno, 0, 0))
103227a0bc89SDoug Rabson 						bp->b_blkno = -1;
103327a0bc89SDoug Rabson 					if (bp->b_blkno == -1)
103427a0bc89SDoug Rabson 						panic("extendfile: pcbmap");
10350d2af521SKirk McKusick 					else
10360d2af521SKirk McKusick 						bp->b_blkno = blkno;
103727a0bc89SDoug Rabson 				}
1038d34b0a1bSBruce Evans 				vfs_bio_clrbuf(bp);
103927a0bc89SDoug Rabson 				if (bpp) {
104027a0bc89SDoug Rabson 					*bpp = bp;
104127a0bc89SDoug Rabson 					bpp = NULL;
1042952a6212SJordan K. Hubbard 				} else
1043952a6212SJordan K. Hubbard 					bdwrite(bp);
104427a0bc89SDoug Rabson 			}
104527a0bc89SDoug Rabson 		}
104627a0bc89SDoug Rabson 	}
104727a0bc89SDoug Rabson 
1048952a6212SJordan K. Hubbard 	return (0);
104927a0bc89SDoug Rabson }
1050cede1f56STom Rhodes 
1051392dbea3SBruce Evans /*-
1052392dbea3SBruce Evans  * Routine to mark a FAT16 or FAT32 volume as "clean" or "dirty" by
1053392dbea3SBruce Evans  * manipulating the upper bit of the FAT entry for cluster 1.  Note that
1054392dbea3SBruce Evans  * this bit is not defined for FAT12 volumes, which are always assumed to
1055a26b949fSKonstantin Belousov  * be clean.
1056cede1f56STom Rhodes  *
1057392dbea3SBruce Evans  * The fatentry() routine only works on cluster numbers that a file could
1058392dbea3SBruce Evans  * occupy, so it won't manipulate the entry for cluster 1.  So we have to do
1059392dbea3SBruce Evans  * it here.  The code was stolen from fatentry() and tailored for cluster 1.
1060cede1f56STom Rhodes  *
1061cede1f56STom Rhodes  * Inputs:
1062cede1f56STom Rhodes  *	pmp	The MS-DOS volume to mark
1063392dbea3SBruce Evans  *	dirty	Non-zero if the volume should be marked dirty; zero if it
1064392dbea3SBruce Evans  *		should be marked clean
1065cede1f56STom Rhodes  *
1066cede1f56STom Rhodes  * Result:
1067cede1f56STom Rhodes  *	0	Success
1068cede1f56STom Rhodes  *	EROFS	Volume is read-only
1069cede1f56STom Rhodes  *	?	(other errors from called routines)
1070cede1f56STom Rhodes  */
1071392dbea3SBruce Evans int
1072392dbea3SBruce Evans markvoldirty(struct msdosfsmount *pmp, int dirty)
1073cede1f56STom Rhodes {
1074cede1f56STom Rhodes 	struct buf *bp;
1075392dbea3SBruce Evans 	u_long bn, bo, bsize, byteoffset, fatval;
1076392dbea3SBruce Evans 	int error;
1077cede1f56STom Rhodes 
1078392dbea3SBruce Evans 	/*
1079392dbea3SBruce Evans 	 * FAT12 does not support a "clean" bit, so don't do anything for
1080392dbea3SBruce Evans 	 * FAT12.
1081392dbea3SBruce Evans 	 */
1082cede1f56STom Rhodes 	if (FAT12(pmp))
1083392dbea3SBruce Evans 		return (0);
1084cede1f56STom Rhodes 
1085392dbea3SBruce Evans 	/* Can't change the bit on a read-only filesystem. */
1086cede1f56STom Rhodes 	if (pmp->pm_flags & MSDOSFSMNT_RONLY)
1087392dbea3SBruce Evans 		return (EROFS);
1088cede1f56STom Rhodes 
1089392dbea3SBruce Evans 	/*
1090392dbea3SBruce Evans 	 * Fetch the block containing the FAT entry.  It is given by the
1091392dbea3SBruce Evans 	 * pseudo-cluster 1.
1092392dbea3SBruce Evans 	 */
1093392dbea3SBruce Evans 	byteoffset = FATOFS(pmp, 1);
1094cede1f56STom Rhodes 	fatblock(pmp, byteoffset, &bn, &bsize, &bo);
1095cede1f56STom Rhodes 	error = bread(pmp->pm_devvp, bn, bsize, NOCRED, &bp);
1096cede1f56STom Rhodes 	if (error) {
1097cede1f56STom Rhodes 		brelse(bp);
1098cede1f56STom Rhodes 		return (error);
1099cede1f56STom Rhodes 	}
1100cede1f56STom Rhodes 
1101392dbea3SBruce Evans 	/*
1102392dbea3SBruce Evans 	 * Get the current value of the FAT entry and set/clear the relevant
1103392dbea3SBruce Evans 	 * bit.  Dirty means clear the "clean" bit; clean means set the
1104392dbea3SBruce Evans 	 * "clean" bit.
1105392dbea3SBruce Evans 	 */
1106cede1f56STom Rhodes 	if (FAT32(pmp)) {
1107392dbea3SBruce Evans 		/* FAT32 uses bit 27. */
1108cede1f56STom Rhodes 		fatval = getulong(&bp->b_data[bo]);
1109cede1f56STom Rhodes 		if (dirty)
1110392dbea3SBruce Evans 			fatval &= 0xF7FFFFFF;
1111cede1f56STom Rhodes 		else
1112392dbea3SBruce Evans 			fatval |= 0x08000000;
1113cede1f56STom Rhodes 		putulong(&bp->b_data[bo], fatval);
1114392dbea3SBruce Evans 	} else {
1115392dbea3SBruce Evans 		/* Must be FAT16; use bit 15. */
1116cede1f56STom Rhodes 		fatval = getushort(&bp->b_data[bo]);
1117cede1f56STom Rhodes 		if (dirty)
1118392dbea3SBruce Evans 			fatval &= 0x7FFF;
1119cede1f56STom Rhodes 		else
1120392dbea3SBruce Evans 			fatval |= 0x8000;
1121cede1f56STom Rhodes 		putushort(&bp->b_data[bo], fatval);
1122cede1f56STom Rhodes 	}
1123cede1f56STom Rhodes 
1124392dbea3SBruce Evans 	/* Write out the modified FAT block synchronously. */
1125392dbea3SBruce Evans 	return (bwrite(bp));
1126cede1f56STom Rhodes }
1127