xref: /freebsd/usr.sbin/makefs/msdos/msdosfs_fat.c (revision 98dc8da58c698aed1843a912c7c5f5b9dfef3656)
1*98dc8da5SEd Maste /* $FreeBSD$ */
2*98dc8da5SEd Maste /*	$NetBSD: msdosfs_fat.c,v 1.28 1997/11/17 15:36:49 ws Exp $	*/
3*98dc8da5SEd Maste 
4*98dc8da5SEd Maste /*-
5*98dc8da5SEd Maste  * SPDX-License-Identifier: BSD-4-Clause
6*98dc8da5SEd Maste  *
7*98dc8da5SEd Maste  * Copyright (C) 1994, 1995, 1997 Wolfgang Solfrank.
8*98dc8da5SEd Maste  * Copyright (C) 1994, 1995, 1997 TooLs GmbH.
9*98dc8da5SEd Maste  * All rights reserved.
10*98dc8da5SEd Maste  * Original code by Paul Popelka (paulp@uts.amdahl.com) (see below).
11*98dc8da5SEd Maste  *
12*98dc8da5SEd Maste  * Redistribution and use in source and binary forms, with or without
13*98dc8da5SEd Maste  * modification, are permitted provided that the following conditions
14*98dc8da5SEd Maste  * are met:
15*98dc8da5SEd Maste  * 1. Redistributions of source code must retain the above copyright
16*98dc8da5SEd Maste  *    notice, this list of conditions and the following disclaimer.
17*98dc8da5SEd Maste  * 2. Redistributions in binary form must reproduce the above copyright
18*98dc8da5SEd Maste  *    notice, this list of conditions and the following disclaimer in the
19*98dc8da5SEd Maste  *    documentation and/or other materials provided with the distribution.
20*98dc8da5SEd Maste  * 3. All advertising materials mentioning features or use of this software
21*98dc8da5SEd Maste  *    must display the following acknowledgement:
22*98dc8da5SEd Maste  *	This product includes software developed by TooLs GmbH.
23*98dc8da5SEd Maste  * 4. The name of TooLs GmbH may not be used to endorse or promote products
24*98dc8da5SEd Maste  *    derived from this software without specific prior written permission.
25*98dc8da5SEd Maste  *
26*98dc8da5SEd Maste  * THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``AS IS'' AND ANY EXPRESS OR
27*98dc8da5SEd Maste  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
28*98dc8da5SEd Maste  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
29*98dc8da5SEd Maste  * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
30*98dc8da5SEd Maste  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
31*98dc8da5SEd Maste  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
32*98dc8da5SEd Maste  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
33*98dc8da5SEd Maste  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
34*98dc8da5SEd Maste  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
35*98dc8da5SEd Maste  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36*98dc8da5SEd Maste  */
37*98dc8da5SEd Maste /*-
38*98dc8da5SEd Maste  * Written by Paul Popelka (paulp@uts.amdahl.com)
39*98dc8da5SEd Maste  *
40*98dc8da5SEd Maste  * You can do anything you want with this software, just don't say you wrote
41*98dc8da5SEd Maste  * it, and don't remove this notice.
42*98dc8da5SEd Maste  *
43*98dc8da5SEd Maste  * This software is provided "as is".
44*98dc8da5SEd Maste  *
45*98dc8da5SEd Maste  * The author supplies this software to be publicly redistributed on the
46*98dc8da5SEd Maste  * understanding that the author is not responsible for the correct
47*98dc8da5SEd Maste  * functioning of this software in any circumstances and is not liable for
48*98dc8da5SEd Maste  * any damages caused by this software.
49*98dc8da5SEd Maste  *
50*98dc8da5SEd Maste  * October 1992
51*98dc8da5SEd Maste  */
52*98dc8da5SEd Maste 
53*98dc8da5SEd Maste #include <sys/param.h>
54*98dc8da5SEd Maste #include <sys/errno.h>
55*98dc8da5SEd Maste 
56*98dc8da5SEd Maste #include <assert.h>
57*98dc8da5SEd Maste #include <stdio.h>
58*98dc8da5SEd Maste #include <string.h>
59*98dc8da5SEd Maste #include <strings.h>
60*98dc8da5SEd Maste 
61*98dc8da5SEd Maste #include <fs/msdosfs/bpb.h>
62*98dc8da5SEd Maste 
63*98dc8da5SEd Maste #include "ffs/buf.h"
64*98dc8da5SEd Maste 
65*98dc8da5SEd Maste #include "msdos/denode.h"
66*98dc8da5SEd Maste #include "msdos/direntry.h"
67*98dc8da5SEd Maste #include "msdos/fat.h"
68*98dc8da5SEd Maste #include "msdos/msdosfsmount.h"
69*98dc8da5SEd Maste 
70*98dc8da5SEd Maste #include "makefs.h"
71*98dc8da5SEd Maste #include "msdos.h"
72*98dc8da5SEd Maste 
73*98dc8da5SEd Maste #define	FULL_RUN	((u_int)0xffffffff)
74*98dc8da5SEd Maste #define	SYNCHRONOUS_WRITES(pmp)	1
75*98dc8da5SEd Maste 
76*98dc8da5SEd Maste static int	chainalloc(struct msdosfsmount *pmp, u_long start,
77*98dc8da5SEd Maste 		    u_long count, u_long fillwith, u_long *retcluster,
78*98dc8da5SEd Maste 		    u_long *got);
79*98dc8da5SEd Maste static int	chainlength(struct msdosfsmount *pmp, u_long start,
80*98dc8da5SEd Maste 		    u_long count);
81*98dc8da5SEd Maste static void	fatblock(struct msdosfsmount *pmp, u_long ofs, u_long *bnp,
82*98dc8da5SEd Maste 		    u_long *sizep, u_long *bop);
83*98dc8da5SEd Maste static int	fatchain(struct msdosfsmount *pmp, u_long start, u_long count,
84*98dc8da5SEd Maste 		    u_long fillwith);
85*98dc8da5SEd Maste static void	fc_lookup(struct denode *dep, u_long findcn, u_long *frcnp,
86*98dc8da5SEd Maste 		    u_long *fsrcnp);
87*98dc8da5SEd Maste static void	updatefats(struct msdosfsmount *pmp, struct buf *bp,
88*98dc8da5SEd Maste 		    u_long fatbn);
89*98dc8da5SEd Maste static __inline void
90*98dc8da5SEd Maste 		usemap_alloc(struct msdosfsmount *pmp, u_long cn);
91*98dc8da5SEd Maste static __inline void
92*98dc8da5SEd Maste 		usemap_free(struct msdosfsmount *pmp, u_long cn);
93*98dc8da5SEd Maste static int	clusteralloc1(struct msdosfsmount *pmp, u_long start,
94*98dc8da5SEd Maste 		    u_long count, u_long fillwith, u_long *retcluster,
95*98dc8da5SEd Maste 		    u_long *got);
96*98dc8da5SEd Maste 
97*98dc8da5SEd Maste static void
98*98dc8da5SEd Maste fatblock(struct msdosfsmount *pmp, u_long ofs, u_long *bnp, u_long *sizep,
99*98dc8da5SEd Maste     u_long *bop)
100*98dc8da5SEd Maste {
101*98dc8da5SEd Maste 	u_long bn, size;
102*98dc8da5SEd Maste 
103*98dc8da5SEd Maste 	bn = ofs / pmp->pm_fatblocksize * pmp->pm_fatblocksec;
104*98dc8da5SEd Maste 	size = MIN(pmp->pm_fatblocksec, pmp->pm_FATsecs - bn)
105*98dc8da5SEd Maste 	    * DEV_BSIZE;
106*98dc8da5SEd Maste 	bn += pmp->pm_fatblk + pmp->pm_curfat * pmp->pm_FATsecs;
107*98dc8da5SEd Maste 
108*98dc8da5SEd Maste 	if (bnp)
109*98dc8da5SEd Maste 		*bnp = bn;
110*98dc8da5SEd Maste 	if (sizep)
111*98dc8da5SEd Maste 		*sizep = size;
112*98dc8da5SEd Maste 	if (bop)
113*98dc8da5SEd Maste 		*bop = ofs % pmp->pm_fatblocksize;
114*98dc8da5SEd Maste }
115*98dc8da5SEd Maste 
116*98dc8da5SEd Maste /*
117*98dc8da5SEd Maste  * Map the logical cluster number of a file into a physical disk sector
118*98dc8da5SEd Maste  * that is filesystem relative.
119*98dc8da5SEd Maste  *
120*98dc8da5SEd Maste  * dep	  - address of denode representing the file of interest
121*98dc8da5SEd Maste  * findcn - file relative cluster whose filesystem relative cluster number
122*98dc8da5SEd Maste  *	    and/or block number are/is to be found
123*98dc8da5SEd Maste  * bnp	  - address of where to place the filesystem relative block number.
124*98dc8da5SEd Maste  *	    If this pointer is null then don't return this quantity.
125*98dc8da5SEd Maste  * cnp	  - address of where to place the filesystem relative cluster number.
126*98dc8da5SEd Maste  *	    If this pointer is null then don't return this quantity.
127*98dc8da5SEd Maste  * sp     - pointer to returned block size
128*98dc8da5SEd Maste  *
129*98dc8da5SEd Maste  * NOTE: Either bnp or cnp must be non-null.
130*98dc8da5SEd Maste  * This function has one side effect.  If the requested file relative cluster
131*98dc8da5SEd Maste  * is beyond the end of file, then the actual number of clusters in the file
132*98dc8da5SEd Maste  * is returned in *cnp.  This is useful for determining how long a directory is.
133*98dc8da5SEd Maste  *  If cnp is null, nothing is returned.
134*98dc8da5SEd Maste  */
135*98dc8da5SEd Maste int
136*98dc8da5SEd Maste pcbmap(struct denode *dep, u_long findcn, daddr_t *bnp, u_long *cnp, int *sp)
137*98dc8da5SEd Maste {
138*98dc8da5SEd Maste 	int error;
139*98dc8da5SEd Maste 	u_long i;
140*98dc8da5SEd Maste 	u_long cn;
141*98dc8da5SEd Maste 	u_long prevcn = 0; /* XXX: prevcn could be used unititialized */
142*98dc8da5SEd Maste 	u_long byteoffset;
143*98dc8da5SEd Maste 	u_long bn;
144*98dc8da5SEd Maste 	u_long bo;
145*98dc8da5SEd Maste 	struct buf *bp = NULL;
146*98dc8da5SEd Maste 	u_long bp_bn = -1;
147*98dc8da5SEd Maste 	struct msdosfsmount *pmp = dep->de_pmp;
148*98dc8da5SEd Maste 	u_long bsize;
149*98dc8da5SEd Maste 
150*98dc8da5SEd Maste 	assert(bnp != NULL || cnp != NULL || sp != NULL);
151*98dc8da5SEd Maste 
152*98dc8da5SEd Maste 	cn = dep->de_StartCluster;
153*98dc8da5SEd Maste 	/*
154*98dc8da5SEd Maste 	 * The "file" that makes up the root directory is contiguous,
155*98dc8da5SEd Maste 	 * permanently allocated, of fixed size, and is not made up of
156*98dc8da5SEd Maste 	 * clusters.  If the cluster number is beyond the end of the root
157*98dc8da5SEd Maste 	 * directory, then return the number of clusters in the file.
158*98dc8da5SEd Maste 	 */
159*98dc8da5SEd Maste 	if (cn == MSDOSFSROOT) {
160*98dc8da5SEd Maste 		if (dep->de_Attributes & ATTR_DIRECTORY) {
161*98dc8da5SEd Maste 			if (de_cn2off(pmp, findcn) >= dep->de_FileSize) {
162*98dc8da5SEd Maste 				if (cnp)
163*98dc8da5SEd Maste 					*cnp = de_bn2cn(pmp, pmp->pm_rootdirsize);
164*98dc8da5SEd Maste 				return (E2BIG);
165*98dc8da5SEd Maste 			}
166*98dc8da5SEd Maste 			if (bnp)
167*98dc8da5SEd Maste 				*bnp = pmp->pm_rootdirblk + de_cn2bn(pmp, findcn);
168*98dc8da5SEd Maste 			if (cnp)
169*98dc8da5SEd Maste 				*cnp = MSDOSFSROOT;
170*98dc8da5SEd Maste 			if (sp)
171*98dc8da5SEd Maste 				*sp = MIN(pmp->pm_bpcluster,
172*98dc8da5SEd Maste 				    dep->de_FileSize - de_cn2off(pmp, findcn));
173*98dc8da5SEd Maste 			return (0);
174*98dc8da5SEd Maste 		} else {		/* just an empty file */
175*98dc8da5SEd Maste 			if (cnp)
176*98dc8da5SEd Maste 				*cnp = 0;
177*98dc8da5SEd Maste 			return (E2BIG);
178*98dc8da5SEd Maste 		}
179*98dc8da5SEd Maste 	}
180*98dc8da5SEd Maste 
181*98dc8da5SEd Maste 	/*
182*98dc8da5SEd Maste 	 * All other files do I/O in cluster sized blocks
183*98dc8da5SEd Maste 	 */
184*98dc8da5SEd Maste 	if (sp)
185*98dc8da5SEd Maste 		*sp = pmp->pm_bpcluster;
186*98dc8da5SEd Maste 
187*98dc8da5SEd Maste 	/*
188*98dc8da5SEd Maste 	 * Rummage around in the FAT cache, maybe we can avoid tromping
189*98dc8da5SEd Maste 	 * through every FAT entry for the file. And, keep track of how far
190*98dc8da5SEd Maste 	 * off the cache was from where we wanted to be.
191*98dc8da5SEd Maste 	 */
192*98dc8da5SEd Maste 	i = 0;
193*98dc8da5SEd Maste 	fc_lookup(dep, findcn, &i, &cn);
194*98dc8da5SEd Maste 
195*98dc8da5SEd Maste 	/*
196*98dc8da5SEd Maste 	 * Handle all other files or directories the normal way.
197*98dc8da5SEd Maste 	 */
198*98dc8da5SEd Maste 	for (; i < findcn; i++) {
199*98dc8da5SEd Maste 		/*
200*98dc8da5SEd Maste 		 * Stop with all reserved clusters, not just with EOF.
201*98dc8da5SEd Maste 		 */
202*98dc8da5SEd Maste 		if ((cn | ~pmp->pm_fatmask) >= CLUST_RSRVD)
203*98dc8da5SEd Maste 			goto hiteof;
204*98dc8da5SEd Maste 		byteoffset = FATOFS(pmp, cn);
205*98dc8da5SEd Maste 		fatblock(pmp, byteoffset, &bn, &bsize, &bo);
206*98dc8da5SEd Maste 		if (bn != bp_bn) {
207*98dc8da5SEd Maste 			if (bp)
208*98dc8da5SEd Maste 				brelse(bp);
209*98dc8da5SEd Maste 			error = bread(pmp->pm_devvp, bn, bsize, NOCRED, &bp);
210*98dc8da5SEd Maste 			if (error) {
211*98dc8da5SEd Maste 				brelse(bp);
212*98dc8da5SEd Maste 				return (error);
213*98dc8da5SEd Maste 			}
214*98dc8da5SEd Maste 			bp_bn = bn;
215*98dc8da5SEd Maste 		}
216*98dc8da5SEd Maste 		prevcn = cn;
217*98dc8da5SEd Maste 		if (bo >= bsize) {
218*98dc8da5SEd Maste 			if (bp)
219*98dc8da5SEd Maste 				brelse(bp);
220*98dc8da5SEd Maste 			return (EIO);
221*98dc8da5SEd Maste 		}
222*98dc8da5SEd Maste 		if (FAT32(pmp))
223*98dc8da5SEd Maste 			cn = getulong(&bp->b_data[bo]);
224*98dc8da5SEd Maste 		else
225*98dc8da5SEd Maste 			cn = getushort(&bp->b_data[bo]);
226*98dc8da5SEd Maste 		if (FAT12(pmp) && (prevcn & 1))
227*98dc8da5SEd Maste 			cn >>= 4;
228*98dc8da5SEd Maste 		cn &= pmp->pm_fatmask;
229*98dc8da5SEd Maste 
230*98dc8da5SEd Maste 		/*
231*98dc8da5SEd Maste 		 * Force the special cluster numbers
232*98dc8da5SEd Maste 		 * to be the same for all cluster sizes
233*98dc8da5SEd Maste 		 * to let the rest of msdosfs handle
234*98dc8da5SEd Maste 		 * all cases the same.
235*98dc8da5SEd Maste 		 */
236*98dc8da5SEd Maste 		if ((cn | ~pmp->pm_fatmask) >= CLUST_RSRVD)
237*98dc8da5SEd Maste 			cn |= ~pmp->pm_fatmask;
238*98dc8da5SEd Maste 	}
239*98dc8da5SEd Maste 
240*98dc8da5SEd Maste 	if (!MSDOSFSEOF(pmp, cn)) {
241*98dc8da5SEd Maste 		if (bp)
242*98dc8da5SEd Maste 			brelse(bp);
243*98dc8da5SEd Maste 		if (bnp)
244*98dc8da5SEd Maste 			*bnp = cntobn(pmp, cn);
245*98dc8da5SEd Maste 		if (cnp)
246*98dc8da5SEd Maste 			*cnp = cn;
247*98dc8da5SEd Maste 		fc_setcache(dep, FC_LASTMAP, i, cn);
248*98dc8da5SEd Maste 		return (0);
249*98dc8da5SEd Maste 	}
250*98dc8da5SEd Maste 
251*98dc8da5SEd Maste hiteof:;
252*98dc8da5SEd Maste 	if (cnp)
253*98dc8da5SEd Maste 		*cnp = i;
254*98dc8da5SEd Maste 	if (bp)
255*98dc8da5SEd Maste 		brelse(bp);
256*98dc8da5SEd Maste 	/* update last file cluster entry in the FAT cache */
257*98dc8da5SEd Maste 	fc_setcache(dep, FC_LASTFC, i - 1, prevcn);
258*98dc8da5SEd Maste 	return (E2BIG);
259*98dc8da5SEd Maste }
260*98dc8da5SEd Maste 
261*98dc8da5SEd Maste /*
262*98dc8da5SEd Maste  * Find the closest entry in the FAT cache to the cluster we are looking
263*98dc8da5SEd Maste  * for.
264*98dc8da5SEd Maste  */
265*98dc8da5SEd Maste static void
266*98dc8da5SEd Maste fc_lookup(struct denode *dep, u_long findcn, u_long *frcnp, u_long *fsrcnp)
267*98dc8da5SEd Maste {
268*98dc8da5SEd Maste 	int i;
269*98dc8da5SEd Maste 	u_long cn;
270*98dc8da5SEd Maste 	struct fatcache *closest = NULL;
271*98dc8da5SEd Maste 
272*98dc8da5SEd Maste 	for (i = 0; i < FC_SIZE; i++) {
273*98dc8da5SEd Maste 		cn = dep->de_fc[i].fc_frcn;
274*98dc8da5SEd Maste 		if (cn != FCE_EMPTY && cn <= findcn) {
275*98dc8da5SEd Maste 			if (closest == NULL || cn > closest->fc_frcn)
276*98dc8da5SEd Maste 				closest = &dep->de_fc[i];
277*98dc8da5SEd Maste 		}
278*98dc8da5SEd Maste 	}
279*98dc8da5SEd Maste 	if (closest) {
280*98dc8da5SEd Maste 		*frcnp = closest->fc_frcn;
281*98dc8da5SEd Maste 		*fsrcnp = closest->fc_fsrcn;
282*98dc8da5SEd Maste 	}
283*98dc8da5SEd Maste }
284*98dc8da5SEd Maste 
285*98dc8da5SEd Maste /*
286*98dc8da5SEd Maste  * Purge the FAT cache in denode dep of all entries relating to file
287*98dc8da5SEd Maste  * relative cluster frcn and beyond.
288*98dc8da5SEd Maste  */
289*98dc8da5SEd Maste void
290*98dc8da5SEd Maste fc_purge(struct denode *dep, u_int frcn)
291*98dc8da5SEd Maste {
292*98dc8da5SEd Maste 	int i;
293*98dc8da5SEd Maste 	struct fatcache *fcp;
294*98dc8da5SEd Maste 
295*98dc8da5SEd Maste 	fcp = dep->de_fc;
296*98dc8da5SEd Maste 	for (i = 0; i < FC_SIZE; i++, fcp++) {
297*98dc8da5SEd Maste 		if (fcp->fc_frcn >= frcn)
298*98dc8da5SEd Maste 			fcp->fc_frcn = FCE_EMPTY;
299*98dc8da5SEd Maste 	}
300*98dc8da5SEd Maste }
301*98dc8da5SEd Maste 
302*98dc8da5SEd Maste /*
303*98dc8da5SEd Maste  * Update the FAT.
304*98dc8da5SEd Maste  * If mirroring the FAT, update all copies, with the first copy as last.
305*98dc8da5SEd Maste  * Else update only the current FAT (ignoring the others).
306*98dc8da5SEd Maste  *
307*98dc8da5SEd Maste  * pmp	 - msdosfsmount structure for filesystem to update
308*98dc8da5SEd Maste  * bp	 - addr of modified FAT block
309*98dc8da5SEd Maste  * fatbn - block number relative to begin of filesystem of the modified FAT block.
310*98dc8da5SEd Maste  */
311*98dc8da5SEd Maste static void
312*98dc8da5SEd Maste updatefats(struct msdosfsmount *pmp, struct buf *bp, u_long fatbn)
313*98dc8da5SEd Maste {
314*98dc8da5SEd Maste 	struct buf *bpn;
315*98dc8da5SEd Maste 	int cleanfat, i;
316*98dc8da5SEd Maste 
317*98dc8da5SEd Maste #ifdef MSDOSFS_DEBUG
318*98dc8da5SEd Maste 	printf("updatefats(pmp %p, bp %p, fatbn %lu)\n", pmp, bp, fatbn);
319*98dc8da5SEd Maste #endif
320*98dc8da5SEd Maste 
321*98dc8da5SEd Maste 	if (pmp->pm_flags & MSDOSFS_FATMIRROR) {
322*98dc8da5SEd Maste 		/*
323*98dc8da5SEd Maste 		 * Now copy the block(s) of the modified FAT to the other copies of
324*98dc8da5SEd Maste 		 * the FAT and write them out.  This is faster than reading in the
325*98dc8da5SEd Maste 		 * other FATs and then writing them back out.  This could tie up
326*98dc8da5SEd Maste 		 * the FAT for quite a while. Preventing others from accessing it.
327*98dc8da5SEd Maste 		 * To prevent us from going after the FAT quite so much we use
328*98dc8da5SEd Maste 		 * delayed writes, unless they specified "synchronous" when the
329*98dc8da5SEd Maste 		 * filesystem was mounted.  If synch is asked for then use
330*98dc8da5SEd Maste 		 * bwrite()'s and really slow things down.
331*98dc8da5SEd Maste 		 */
332*98dc8da5SEd Maste 		if (fatbn != pmp->pm_fatblk || FAT12(pmp))
333*98dc8da5SEd Maste 			cleanfat = 0;
334*98dc8da5SEd Maste 		else if (FAT16(pmp))
335*98dc8da5SEd Maste 			cleanfat = 16;
336*98dc8da5SEd Maste 		else
337*98dc8da5SEd Maste 			cleanfat = 32;
338*98dc8da5SEd Maste 		for (i = 1; i < pmp->pm_FATs; i++) {
339*98dc8da5SEd Maste 			fatbn += pmp->pm_FATsecs;
340*98dc8da5SEd Maste 			/* getblk() never fails */
341*98dc8da5SEd Maste 			bpn = getblk(pmp->pm_devvp, fatbn, bp->b_bcount,
342*98dc8da5SEd Maste 			    0, 0, 0);
343*98dc8da5SEd Maste 			memcpy(bpn->b_data, bp->b_data, bp->b_bcount);
344*98dc8da5SEd Maste 			/* Force the clean bit on in the other copies. */
345*98dc8da5SEd Maste 			if (cleanfat == 16)
346*98dc8da5SEd Maste 				((uint8_t *)bpn->b_data)[3] |= 0x80;
347*98dc8da5SEd Maste 			else if (cleanfat == 32)
348*98dc8da5SEd Maste 				((uint8_t *)bpn->b_data)[7] |= 0x08;
349*98dc8da5SEd Maste 			if (SYNCHRONOUS_WRITES(pmp))
350*98dc8da5SEd Maste 				bwrite(bpn);
351*98dc8da5SEd Maste 			else
352*98dc8da5SEd Maste 				bdwrite(bpn);
353*98dc8da5SEd Maste 		}
354*98dc8da5SEd Maste 	}
355*98dc8da5SEd Maste 
356*98dc8da5SEd Maste 	/*
357*98dc8da5SEd Maste 	 * Write out the first (or current) FAT last.
358*98dc8da5SEd Maste 	 */
359*98dc8da5SEd Maste 	if (SYNCHRONOUS_WRITES(pmp))
360*98dc8da5SEd Maste 		bwrite(bp);
361*98dc8da5SEd Maste 	else
362*98dc8da5SEd Maste 		bdwrite(bp);
363*98dc8da5SEd Maste }
364*98dc8da5SEd Maste 
365*98dc8da5SEd Maste /*
366*98dc8da5SEd Maste  * Updating entries in 12 bit FATs is a pain in the butt.
367*98dc8da5SEd Maste  *
368*98dc8da5SEd Maste  * The following picture shows where nibbles go when moving from a 12 bit
369*98dc8da5SEd Maste  * cluster number into the appropriate bytes in the FAT.
370*98dc8da5SEd Maste  *
371*98dc8da5SEd Maste  *	byte m        byte m+1      byte m+2
372*98dc8da5SEd Maste  *	+----+----+   +----+----+   +----+----+
373*98dc8da5SEd Maste  *	|  0    1 |   |  2    3 |   |  4    5 |   FAT bytes
374*98dc8da5SEd Maste  *	+----+----+   +----+----+   +----+----+
375*98dc8da5SEd Maste  *
376*98dc8da5SEd Maste  *	+----+----+----+   +----+----+----+
377*98dc8da5SEd Maste  *	|  3    0    1 |   |  4    5    2 |
378*98dc8da5SEd Maste  *	+----+----+----+   +----+----+----+
379*98dc8da5SEd Maste  *	cluster n	   cluster n+1
380*98dc8da5SEd Maste  *
381*98dc8da5SEd Maste  * Where n is even. m = n + (n >> 2)
382*98dc8da5SEd Maste  *
383*98dc8da5SEd Maste  */
384*98dc8da5SEd Maste static __inline void
385*98dc8da5SEd Maste usemap_alloc(struct msdosfsmount *pmp, u_long cn)
386*98dc8da5SEd Maste {
387*98dc8da5SEd Maste 
388*98dc8da5SEd Maste 	assert(cn <= pmp->pm_maxcluster);
389*98dc8da5SEd Maste 	assert((pmp->pm_flags & MSDOSFSMNT_RONLY) == 0);
390*98dc8da5SEd Maste 	assert((pmp->pm_inusemap[cn / N_INUSEBITS] & (1 << (cn % N_INUSEBITS)))
391*98dc8da5SEd Maste 	    == 0);
392*98dc8da5SEd Maste 	assert(pmp->pm_freeclustercount > 0);
393*98dc8da5SEd Maste 
394*98dc8da5SEd Maste 	pmp->pm_inusemap[cn / N_INUSEBITS] |= 1U << (cn % N_INUSEBITS);
395*98dc8da5SEd Maste 	pmp->pm_freeclustercount--;
396*98dc8da5SEd Maste 	pmp->pm_flags |= MSDOSFS_FSIMOD;
397*98dc8da5SEd Maste }
398*98dc8da5SEd Maste 
399*98dc8da5SEd Maste static __inline void
400*98dc8da5SEd Maste usemap_free(struct msdosfsmount *pmp, u_long cn)
401*98dc8da5SEd Maste {
402*98dc8da5SEd Maste 
403*98dc8da5SEd Maste 	assert(cn <= pmp->pm_maxcluster);
404*98dc8da5SEd Maste 	assert((pmp->pm_flags & MSDOSFSMNT_RONLY) == 0);
405*98dc8da5SEd Maste 	assert((pmp->pm_inusemap[cn / N_INUSEBITS] & (1 << (cn % N_INUSEBITS)))
406*98dc8da5SEd Maste 	    != 0);
407*98dc8da5SEd Maste 
408*98dc8da5SEd Maste 	pmp->pm_freeclustercount++;
409*98dc8da5SEd Maste 	pmp->pm_flags |= MSDOSFS_FSIMOD;
410*98dc8da5SEd Maste 	pmp->pm_inusemap[cn / N_INUSEBITS] &= ~(1U << (cn % N_INUSEBITS));
411*98dc8da5SEd Maste }
412*98dc8da5SEd Maste 
413*98dc8da5SEd Maste int
414*98dc8da5SEd Maste clusterfree(struct msdosfsmount *pmp, u_long cluster, u_long *oldcnp)
415*98dc8da5SEd Maste {
416*98dc8da5SEd Maste 	int error;
417*98dc8da5SEd Maste 	u_long oldcn;
418*98dc8da5SEd Maste 
419*98dc8da5SEd Maste 	error = fatentry(FAT_GET_AND_SET, pmp, cluster, &oldcn, MSDOSFSFREE);
420*98dc8da5SEd Maste 	if (error)
421*98dc8da5SEd Maste 		return (error);
422*98dc8da5SEd Maste 	/*
423*98dc8da5SEd Maste 	 * If the cluster was successfully marked free, then update
424*98dc8da5SEd Maste 	 * the count of free clusters, and turn off the "allocated"
425*98dc8da5SEd Maste 	 * bit in the "in use" cluster bit map.
426*98dc8da5SEd Maste 	 */
427*98dc8da5SEd Maste 	usemap_free(pmp, cluster);
428*98dc8da5SEd Maste 	if (oldcnp)
429*98dc8da5SEd Maste 		*oldcnp = oldcn;
430*98dc8da5SEd Maste 	return (0);
431*98dc8da5SEd Maste }
432*98dc8da5SEd Maste 
433*98dc8da5SEd Maste /*
434*98dc8da5SEd Maste  * Get or Set or 'Get and Set' the cluster'th entry in the FAT.
435*98dc8da5SEd Maste  *
436*98dc8da5SEd Maste  * function	- whether to get or set a FAT entry
437*98dc8da5SEd Maste  * pmp		- address of the msdosfsmount structure for the filesystem
438*98dc8da5SEd Maste  *		  whose FAT is to be manipulated.
439*98dc8da5SEd Maste  * cn		- which cluster is of interest
440*98dc8da5SEd Maste  * oldcontents	- address of a word that is to receive the contents of the
441*98dc8da5SEd Maste  *		  cluster'th entry if this is a get function
442*98dc8da5SEd Maste  * newcontents	- the new value to be written into the cluster'th element of
443*98dc8da5SEd Maste  *		  the FAT if this is a set function.
444*98dc8da5SEd Maste  *
445*98dc8da5SEd Maste  * This function can also be used to free a cluster by setting the FAT entry
446*98dc8da5SEd Maste  * for a cluster to 0.
447*98dc8da5SEd Maste  *
448*98dc8da5SEd Maste  * All copies of the FAT are updated if this is a set function. NOTE: If
449*98dc8da5SEd Maste  * fatentry() marks a cluster as free it does not update the inusemap in
450*98dc8da5SEd Maste  * the msdosfsmount structure. This is left to the caller.
451*98dc8da5SEd Maste  */
452*98dc8da5SEd Maste int
453*98dc8da5SEd Maste fatentry(int function, struct msdosfsmount *pmp, u_long cn, u_long *oldcontents,
454*98dc8da5SEd Maste     u_long newcontents)
455*98dc8da5SEd Maste {
456*98dc8da5SEd Maste 	int error;
457*98dc8da5SEd Maste 	u_long readcn;
458*98dc8da5SEd Maste 	u_long bn, bo, bsize, byteoffset;
459*98dc8da5SEd Maste 	struct buf *bp;
460*98dc8da5SEd Maste 
461*98dc8da5SEd Maste #ifdef	MSDOSFS_DEBUG
462*98dc8da5SEd Maste 	printf("fatentry(func %d, pmp %p, clust %lu, oldcon %p, newcon %lx)\n",
463*98dc8da5SEd Maste 	    function, pmp, cn, oldcontents, newcontents);
464*98dc8da5SEd Maste #endif
465*98dc8da5SEd Maste 
466*98dc8da5SEd Maste #ifdef DIAGNOSTIC
467*98dc8da5SEd Maste 	/*
468*98dc8da5SEd Maste 	 * Be sure they asked us to do something.
469*98dc8da5SEd Maste 	 */
470*98dc8da5SEd Maste 	if ((function & (FAT_SET | FAT_GET)) == 0) {
471*98dc8da5SEd Maste #ifdef MSDOSFS_DEBUG
472*98dc8da5SEd Maste 		printf("fatentry(): function code doesn't specify get or set\n");
473*98dc8da5SEd Maste #endif
474*98dc8da5SEd Maste 		return (EINVAL);
475*98dc8da5SEd Maste 	}
476*98dc8da5SEd Maste 
477*98dc8da5SEd Maste 	/*
478*98dc8da5SEd Maste 	 * If they asked us to return a cluster number but didn't tell us
479*98dc8da5SEd Maste 	 * where to put it, give them an error.
480*98dc8da5SEd Maste 	 */
481*98dc8da5SEd Maste 	if ((function & FAT_GET) && oldcontents == NULL) {
482*98dc8da5SEd Maste #ifdef MSDOSFS_DEBUG
483*98dc8da5SEd Maste 		printf("fatentry(): get function with no place to put result\n");
484*98dc8da5SEd Maste #endif
485*98dc8da5SEd Maste 		return (EINVAL);
486*98dc8da5SEd Maste 	}
487*98dc8da5SEd Maste #endif
488*98dc8da5SEd Maste 
489*98dc8da5SEd Maste 	/*
490*98dc8da5SEd Maste 	 * Be sure the requested cluster is in the filesystem.
491*98dc8da5SEd Maste 	 */
492*98dc8da5SEd Maste 	if (cn < CLUST_FIRST || cn > pmp->pm_maxcluster)
493*98dc8da5SEd Maste 		return (EINVAL);
494*98dc8da5SEd Maste 
495*98dc8da5SEd Maste 	byteoffset = FATOFS(pmp, cn);
496*98dc8da5SEd Maste 	fatblock(pmp, byteoffset, &bn, &bsize, &bo);
497*98dc8da5SEd Maste 	error = bread(pmp->pm_devvp, bn, bsize, NOCRED, &bp);
498*98dc8da5SEd Maste 	if (error) {
499*98dc8da5SEd Maste 		brelse(bp);
500*98dc8da5SEd Maste 		return (error);
501*98dc8da5SEd Maste 	}
502*98dc8da5SEd Maste 
503*98dc8da5SEd Maste 	if (function & FAT_GET) {
504*98dc8da5SEd Maste 		if (FAT32(pmp))
505*98dc8da5SEd Maste 			readcn = getulong(&bp->b_data[bo]);
506*98dc8da5SEd Maste 		else
507*98dc8da5SEd Maste 			readcn = getushort(&bp->b_data[bo]);
508*98dc8da5SEd Maste 		if (FAT12(pmp) & (cn & 1))
509*98dc8da5SEd Maste 			readcn >>= 4;
510*98dc8da5SEd Maste 		readcn &= pmp->pm_fatmask;
511*98dc8da5SEd Maste 		/* map reserved FAT entries to same values for all FATs */
512*98dc8da5SEd Maste 		if ((readcn | ~pmp->pm_fatmask) >= CLUST_RSRVD)
513*98dc8da5SEd Maste 			readcn |= ~pmp->pm_fatmask;
514*98dc8da5SEd Maste 		*oldcontents = readcn;
515*98dc8da5SEd Maste 	}
516*98dc8da5SEd Maste 	if (function & FAT_SET) {
517*98dc8da5SEd Maste 		switch (pmp->pm_fatmask) {
518*98dc8da5SEd Maste 		case FAT12_MASK:
519*98dc8da5SEd Maste 			readcn = getushort(&bp->b_data[bo]);
520*98dc8da5SEd Maste 			if (cn & 1) {
521*98dc8da5SEd Maste 				readcn &= 0x000f;
522*98dc8da5SEd Maste 				readcn |= newcontents << 4;
523*98dc8da5SEd Maste 			} else {
524*98dc8da5SEd Maste 				readcn &= 0xf000;
525*98dc8da5SEd Maste 				readcn |= newcontents & 0xfff;
526*98dc8da5SEd Maste 			}
527*98dc8da5SEd Maste 			putushort(&bp->b_data[bo], readcn);
528*98dc8da5SEd Maste 			break;
529*98dc8da5SEd Maste 		case FAT16_MASK:
530*98dc8da5SEd Maste 			putushort(&bp->b_data[bo], newcontents);
531*98dc8da5SEd Maste 			break;
532*98dc8da5SEd Maste 		case FAT32_MASK:
533*98dc8da5SEd Maste 			/*
534*98dc8da5SEd Maste 			 * According to spec we have to retain the
535*98dc8da5SEd Maste 			 * high order bits of the FAT entry.
536*98dc8da5SEd Maste 			 */
537*98dc8da5SEd Maste 			readcn = getulong(&bp->b_data[bo]);
538*98dc8da5SEd Maste 			readcn &= ~FAT32_MASK;
539*98dc8da5SEd Maste 			readcn |= newcontents & FAT32_MASK;
540*98dc8da5SEd Maste 			putulong(&bp->b_data[bo], readcn);
541*98dc8da5SEd Maste 			break;
542*98dc8da5SEd Maste 		}
543*98dc8da5SEd Maste 		updatefats(pmp, bp, bn);
544*98dc8da5SEd Maste 		bp = NULL;
545*98dc8da5SEd Maste 		pmp->pm_fmod = 1;
546*98dc8da5SEd Maste 	}
547*98dc8da5SEd Maste 	if (bp)
548*98dc8da5SEd Maste 		brelse(bp);
549*98dc8da5SEd Maste 	return (0);
550*98dc8da5SEd Maste }
551*98dc8da5SEd Maste 
552*98dc8da5SEd Maste /*
553*98dc8da5SEd Maste  * Update a contiguous cluster chain
554*98dc8da5SEd Maste  *
555*98dc8da5SEd Maste  * pmp	    - mount point
556*98dc8da5SEd Maste  * start    - first cluster of chain
557*98dc8da5SEd Maste  * count    - number of clusters in chain
558*98dc8da5SEd Maste  * fillwith - what to write into FAT entry of last cluster
559*98dc8da5SEd Maste  */
560*98dc8da5SEd Maste static int
561*98dc8da5SEd Maste fatchain(struct msdosfsmount *pmp, u_long start, u_long count, u_long fillwith)
562*98dc8da5SEd Maste {
563*98dc8da5SEd Maste 	int error;
564*98dc8da5SEd Maste 	u_long bn, bo, bsize, byteoffset, readcn, newc;
565*98dc8da5SEd Maste 	struct buf *bp;
566*98dc8da5SEd Maste 
567*98dc8da5SEd Maste #ifdef MSDOSFS_DEBUG
568*98dc8da5SEd Maste 	printf("fatchain(pmp %p, start %lu, count %lu, fillwith %lx)\n",
569*98dc8da5SEd Maste 	    pmp, start, count, fillwith);
570*98dc8da5SEd Maste #endif
571*98dc8da5SEd Maste 	/*
572*98dc8da5SEd Maste 	 * Be sure the clusters are in the filesystem.
573*98dc8da5SEd Maste 	 */
574*98dc8da5SEd Maste 	if (start < CLUST_FIRST || start + count - 1 > pmp->pm_maxcluster)
575*98dc8da5SEd Maste 		return (EINVAL);
576*98dc8da5SEd Maste 
577*98dc8da5SEd Maste 	while (count > 0) {
578*98dc8da5SEd Maste 		byteoffset = FATOFS(pmp, start);
579*98dc8da5SEd Maste 		fatblock(pmp, byteoffset, &bn, &bsize, &bo);
580*98dc8da5SEd Maste 		error = bread(pmp->pm_devvp, bn, bsize, NOCRED, &bp);
581*98dc8da5SEd Maste 		if (error) {
582*98dc8da5SEd Maste 			brelse(bp);
583*98dc8da5SEd Maste 			return (error);
584*98dc8da5SEd Maste 		}
585*98dc8da5SEd Maste 		while (count > 0) {
586*98dc8da5SEd Maste 			start++;
587*98dc8da5SEd Maste 			newc = --count > 0 ? start : fillwith;
588*98dc8da5SEd Maste 			switch (pmp->pm_fatmask) {
589*98dc8da5SEd Maste 			case FAT12_MASK:
590*98dc8da5SEd Maste 				readcn = getushort(&bp->b_data[bo]);
591*98dc8da5SEd Maste 				if (start & 1) {
592*98dc8da5SEd Maste 					readcn &= 0xf000;
593*98dc8da5SEd Maste 					readcn |= newc & 0xfff;
594*98dc8da5SEd Maste 				} else {
595*98dc8da5SEd Maste 					readcn &= 0x000f;
596*98dc8da5SEd Maste 					readcn |= newc << 4;
597*98dc8da5SEd Maste 				}
598*98dc8da5SEd Maste 				putushort(&bp->b_data[bo], readcn);
599*98dc8da5SEd Maste 				bo++;
600*98dc8da5SEd Maste 				if (!(start & 1))
601*98dc8da5SEd Maste 					bo++;
602*98dc8da5SEd Maste 				break;
603*98dc8da5SEd Maste 			case FAT16_MASK:
604*98dc8da5SEd Maste 				putushort(&bp->b_data[bo], newc);
605*98dc8da5SEd Maste 				bo += 2;
606*98dc8da5SEd Maste 				break;
607*98dc8da5SEd Maste 			case FAT32_MASK:
608*98dc8da5SEd Maste 				readcn = getulong(&bp->b_data[bo]);
609*98dc8da5SEd Maste 				readcn &= ~pmp->pm_fatmask;
610*98dc8da5SEd Maste 				readcn |= newc & pmp->pm_fatmask;
611*98dc8da5SEd Maste 				putulong(&bp->b_data[bo], readcn);
612*98dc8da5SEd Maste 				bo += 4;
613*98dc8da5SEd Maste 				break;
614*98dc8da5SEd Maste 			}
615*98dc8da5SEd Maste 			if (bo >= bsize)
616*98dc8da5SEd Maste 				break;
617*98dc8da5SEd Maste 		}
618*98dc8da5SEd Maste 		updatefats(pmp, bp, bn);
619*98dc8da5SEd Maste 	}
620*98dc8da5SEd Maste 	pmp->pm_fmod = 1;
621*98dc8da5SEd Maste 	return (0);
622*98dc8da5SEd Maste }
623*98dc8da5SEd Maste 
624*98dc8da5SEd Maste /*
625*98dc8da5SEd Maste  * Check the length of a free cluster chain starting at start.
626*98dc8da5SEd Maste  *
627*98dc8da5SEd Maste  * pmp	 - mount point
628*98dc8da5SEd Maste  * start - start of chain
629*98dc8da5SEd Maste  * count - maximum interesting length
630*98dc8da5SEd Maste  */
631*98dc8da5SEd Maste static int
632*98dc8da5SEd Maste chainlength(struct msdosfsmount *pmp, u_long start, u_long count)
633*98dc8da5SEd Maste {
634*98dc8da5SEd Maste 	u_long idx, max_idx;
635*98dc8da5SEd Maste 	u_int map;
636*98dc8da5SEd Maste 	u_long len;
637*98dc8da5SEd Maste 
638*98dc8da5SEd Maste 	if (start > pmp->pm_maxcluster)
639*98dc8da5SEd Maste 		return (0);
640*98dc8da5SEd Maste 	max_idx = pmp->pm_maxcluster / N_INUSEBITS;
641*98dc8da5SEd Maste 	idx = start / N_INUSEBITS;
642*98dc8da5SEd Maste 	start %= N_INUSEBITS;
643*98dc8da5SEd Maste 	map = pmp->pm_inusemap[idx];
644*98dc8da5SEd Maste 	map &= ~((1 << start) - 1);
645*98dc8da5SEd Maste 	if (map) {
646*98dc8da5SEd Maste 		len = ffs(map) - 1 - start;
647*98dc8da5SEd Maste 		len = MIN(len, count);
648*98dc8da5SEd Maste 		if (start + len > pmp->pm_maxcluster)
649*98dc8da5SEd Maste 			len = pmp->pm_maxcluster - start + 1;
650*98dc8da5SEd Maste 		return (len);
651*98dc8da5SEd Maste 	}
652*98dc8da5SEd Maste 	len = N_INUSEBITS - start;
653*98dc8da5SEd Maste 	if (len >= count) {
654*98dc8da5SEd Maste 		len = count;
655*98dc8da5SEd Maste 		if (start + len > pmp->pm_maxcluster)
656*98dc8da5SEd Maste 			len = pmp->pm_maxcluster - start + 1;
657*98dc8da5SEd Maste 		return (len);
658*98dc8da5SEd Maste 	}
659*98dc8da5SEd Maste 	while (++idx <= max_idx) {
660*98dc8da5SEd Maste 		if (len >= count)
661*98dc8da5SEd Maste 			break;
662*98dc8da5SEd Maste 		map = pmp->pm_inusemap[idx];
663*98dc8da5SEd Maste 		if (map) {
664*98dc8da5SEd Maste 			len += ffs(map) - 1;
665*98dc8da5SEd Maste 			break;
666*98dc8da5SEd Maste 		}
667*98dc8da5SEd Maste 		len += N_INUSEBITS;
668*98dc8da5SEd Maste 	}
669*98dc8da5SEd Maste 	len = MIN(len, count);
670*98dc8da5SEd Maste 	if (start + len > pmp->pm_maxcluster)
671*98dc8da5SEd Maste 		len = pmp->pm_maxcluster - start + 1;
672*98dc8da5SEd Maste 	return (len);
673*98dc8da5SEd Maste }
674*98dc8da5SEd Maste 
675*98dc8da5SEd Maste /*
676*98dc8da5SEd Maste  * Allocate contigous free clusters.
677*98dc8da5SEd Maste  *
678*98dc8da5SEd Maste  * pmp	      - mount point.
679*98dc8da5SEd Maste  * start      - start of cluster chain.
680*98dc8da5SEd Maste  * count      - number of clusters to allocate.
681*98dc8da5SEd Maste  * fillwith   - put this value into the FAT entry for the
682*98dc8da5SEd Maste  *		last allocated cluster.
683*98dc8da5SEd Maste  * retcluster - put the first allocated cluster's number here.
684*98dc8da5SEd Maste  * got	      - how many clusters were actually allocated.
685*98dc8da5SEd Maste  */
686*98dc8da5SEd Maste static int
687*98dc8da5SEd Maste chainalloc(struct msdosfsmount *pmp, u_long start, u_long count,
688*98dc8da5SEd Maste     u_long fillwith, u_long *retcluster, u_long *got)
689*98dc8da5SEd Maste {
690*98dc8da5SEd Maste 	int error;
691*98dc8da5SEd Maste 	u_long cl, n;
692*98dc8da5SEd Maste 
693*98dc8da5SEd Maste 	assert((pmp->pm_flags & MSDOSFSMNT_RONLY) == 0);
694*98dc8da5SEd Maste 
695*98dc8da5SEd Maste 	for (cl = start, n = count; n-- > 0;)
696*98dc8da5SEd Maste 		usemap_alloc(pmp, cl++);
697*98dc8da5SEd Maste 	pmp->pm_nxtfree = start + count;
698*98dc8da5SEd Maste 	if (pmp->pm_nxtfree > pmp->pm_maxcluster)
699*98dc8da5SEd Maste 		pmp->pm_nxtfree = CLUST_FIRST;
700*98dc8da5SEd Maste 	pmp->pm_flags |= MSDOSFS_FSIMOD;
701*98dc8da5SEd Maste 	error = fatchain(pmp, start, count, fillwith);
702*98dc8da5SEd Maste 	if (error != 0) {
703*98dc8da5SEd Maste 		for (cl = start, n = count; n-- > 0;)
704*98dc8da5SEd Maste 			usemap_free(pmp, cl++);
705*98dc8da5SEd Maste 		return (error);
706*98dc8da5SEd Maste 	}
707*98dc8da5SEd Maste #ifdef MSDOSFS_DEBUG
708*98dc8da5SEd Maste 	printf("clusteralloc(): allocated cluster chain at %lu (%lu clusters)\n",
709*98dc8da5SEd Maste 	    start, count);
710*98dc8da5SEd Maste #endif
711*98dc8da5SEd Maste 	if (retcluster)
712*98dc8da5SEd Maste 		*retcluster = start;
713*98dc8da5SEd Maste 	if (got)
714*98dc8da5SEd Maste 		*got = count;
715*98dc8da5SEd Maste 	return (0);
716*98dc8da5SEd Maste }
717*98dc8da5SEd Maste 
718*98dc8da5SEd Maste /*
719*98dc8da5SEd Maste  * Allocate contiguous free clusters.
720*98dc8da5SEd Maste  *
721*98dc8da5SEd Maste  * pmp	      - mount point.
722*98dc8da5SEd Maste  * start      - preferred start of cluster chain.
723*98dc8da5SEd Maste  * count      - number of clusters requested.
724*98dc8da5SEd Maste  * fillwith   - put this value into the FAT entry for the
725*98dc8da5SEd Maste  *		last allocated cluster.
726*98dc8da5SEd Maste  * retcluster - put the first allocated cluster's number here.
727*98dc8da5SEd Maste  * got	      - how many clusters were actually allocated.
728*98dc8da5SEd Maste  */
729*98dc8da5SEd Maste int
730*98dc8da5SEd Maste clusteralloc(struct msdosfsmount *pmp, u_long start, u_long count,
731*98dc8da5SEd Maste     u_long fillwith, u_long *retcluster, u_long *got)
732*98dc8da5SEd Maste {
733*98dc8da5SEd Maste 	int error;
734*98dc8da5SEd Maste 
735*98dc8da5SEd Maste 	error = clusteralloc1(pmp, start, count, fillwith, retcluster, got);
736*98dc8da5SEd Maste 	return (error);
737*98dc8da5SEd Maste }
738*98dc8da5SEd Maste 
739*98dc8da5SEd Maste static int
740*98dc8da5SEd Maste clusteralloc1(struct msdosfsmount *pmp, u_long start, u_long count,
741*98dc8da5SEd Maste     u_long fillwith, u_long *retcluster, u_long *got)
742*98dc8da5SEd Maste {
743*98dc8da5SEd Maste 	u_long idx;
744*98dc8da5SEd Maste 	u_long len, newst, foundl, cn, l;
745*98dc8da5SEd Maste 	u_long foundcn = 0; /* XXX: foundcn could be used unititialized */
746*98dc8da5SEd Maste 	u_int map;
747*98dc8da5SEd Maste 
748*98dc8da5SEd Maste 	MSDOSFS_DPRINTF(("clusteralloc(): find %lu clusters\n", count));
749*98dc8da5SEd Maste 
750*98dc8da5SEd Maste 	if (start) {
751*98dc8da5SEd Maste 		if ((len = chainlength(pmp, start, count)) >= count)
752*98dc8da5SEd Maste 			return (chainalloc(pmp, start, count, fillwith, retcluster, got));
753*98dc8da5SEd Maste 	} else
754*98dc8da5SEd Maste 		len = 0;
755*98dc8da5SEd Maste 
756*98dc8da5SEd Maste 	newst = pmp->pm_nxtfree;
757*98dc8da5SEd Maste 	foundl = 0;
758*98dc8da5SEd Maste 
759*98dc8da5SEd Maste 	for (cn = newst; cn <= pmp->pm_maxcluster;) {
760*98dc8da5SEd Maste 		idx = cn / N_INUSEBITS;
761*98dc8da5SEd Maste 		map = pmp->pm_inusemap[idx];
762*98dc8da5SEd Maste 		map |= (1U << (cn % N_INUSEBITS)) - 1;
763*98dc8da5SEd Maste 		if (map != FULL_RUN) {
764*98dc8da5SEd Maste 			cn = idx * N_INUSEBITS + ffs(map ^ FULL_RUN) - 1;
765*98dc8da5SEd Maste 			if ((l = chainlength(pmp, cn, count)) >= count)
766*98dc8da5SEd Maste 				return (chainalloc(pmp, cn, count, fillwith, retcluster, got));
767*98dc8da5SEd Maste 			if (l > foundl) {
768*98dc8da5SEd Maste 				foundcn = cn;
769*98dc8da5SEd Maste 				foundl = l;
770*98dc8da5SEd Maste 			}
771*98dc8da5SEd Maste 			cn += l + 1;
772*98dc8da5SEd Maste 			continue;
773*98dc8da5SEd Maste 		}
774*98dc8da5SEd Maste 		cn += N_INUSEBITS - cn % N_INUSEBITS;
775*98dc8da5SEd Maste 	}
776*98dc8da5SEd Maste 	for (cn = 0; cn < newst;) {
777*98dc8da5SEd Maste 		idx = cn / N_INUSEBITS;
778*98dc8da5SEd Maste 		map = pmp->pm_inusemap[idx];
779*98dc8da5SEd Maste 		map |= (1U << (cn % N_INUSEBITS)) - 1;
780*98dc8da5SEd Maste 		if (map != FULL_RUN) {
781*98dc8da5SEd Maste 			cn = idx * N_INUSEBITS + ffs(map ^ FULL_RUN) - 1;
782*98dc8da5SEd Maste 			if ((l = chainlength(pmp, cn, count)) >= count)
783*98dc8da5SEd Maste 				return (chainalloc(pmp, cn, count, fillwith, retcluster, got));
784*98dc8da5SEd Maste 			if (l > foundl) {
785*98dc8da5SEd Maste 				foundcn = cn;
786*98dc8da5SEd Maste 				foundl = l;
787*98dc8da5SEd Maste 			}
788*98dc8da5SEd Maste 			cn += l + 1;
789*98dc8da5SEd Maste 			continue;
790*98dc8da5SEd Maste 		}
791*98dc8da5SEd Maste 		cn += N_INUSEBITS - cn % N_INUSEBITS;
792*98dc8da5SEd Maste 	}
793*98dc8da5SEd Maste 
794*98dc8da5SEd Maste 	if (!foundl)
795*98dc8da5SEd Maste 		return (ENOSPC);
796*98dc8da5SEd Maste 
797*98dc8da5SEd Maste 	if (len)
798*98dc8da5SEd Maste 		return (chainalloc(pmp, start, len, fillwith, retcluster, got));
799*98dc8da5SEd Maste 	else
800*98dc8da5SEd Maste 		return (chainalloc(pmp, foundcn, foundl, fillwith, retcluster, got));
801*98dc8da5SEd Maste }
802*98dc8da5SEd Maste 
803*98dc8da5SEd Maste 
804*98dc8da5SEd Maste /*
805*98dc8da5SEd Maste  * Free a chain of clusters.
806*98dc8da5SEd Maste  *
807*98dc8da5SEd Maste  * pmp		- address of the msdosfs mount structure for the filesystem
808*98dc8da5SEd Maste  *		  containing the cluster chain to be freed.
809*98dc8da5SEd Maste  * startcluster - number of the 1st cluster in the chain of clusters to be
810*98dc8da5SEd Maste  *		  freed.
811*98dc8da5SEd Maste  */
812*98dc8da5SEd Maste int
813*98dc8da5SEd Maste freeclusterchain(struct msdosfsmount *pmp, u_long cluster)
814*98dc8da5SEd Maste {
815*98dc8da5SEd Maste 	int error;
816*98dc8da5SEd Maste 	struct buf *bp = NULL;
817*98dc8da5SEd Maste 	u_long bn, bo, bsize, byteoffset;
818*98dc8da5SEd Maste 	u_long readcn, lbn = -1;
819*98dc8da5SEd Maste 
820*98dc8da5SEd Maste 	while (cluster >= CLUST_FIRST && cluster <= pmp->pm_maxcluster) {
821*98dc8da5SEd Maste 		byteoffset = FATOFS(pmp, cluster);
822*98dc8da5SEd Maste 		fatblock(pmp, byteoffset, &bn, &bsize, &bo);
823*98dc8da5SEd Maste 		if (lbn != bn) {
824*98dc8da5SEd Maste 			if (bp)
825*98dc8da5SEd Maste 				updatefats(pmp, bp, lbn);
826*98dc8da5SEd Maste 			error = bread(pmp->pm_devvp, bn, bsize, NOCRED, &bp);
827*98dc8da5SEd Maste 			if (error) {
828*98dc8da5SEd Maste 				brelse(bp);
829*98dc8da5SEd Maste 				return (error);
830*98dc8da5SEd Maste 			}
831*98dc8da5SEd Maste 			lbn = bn;
832*98dc8da5SEd Maste 		}
833*98dc8da5SEd Maste 		usemap_free(pmp, cluster);
834*98dc8da5SEd Maste 		switch (pmp->pm_fatmask) {
835*98dc8da5SEd Maste 		case FAT12_MASK:
836*98dc8da5SEd Maste 			readcn = getushort(&bp->b_data[bo]);
837*98dc8da5SEd Maste 			if (cluster & 1) {
838*98dc8da5SEd Maste 				cluster = readcn >> 4;
839*98dc8da5SEd Maste 				readcn &= 0x000f;
840*98dc8da5SEd Maste 				readcn |= MSDOSFSFREE << 4;
841*98dc8da5SEd Maste 			} else {
842*98dc8da5SEd Maste 				cluster = readcn;
843*98dc8da5SEd Maste 				readcn &= 0xf000;
844*98dc8da5SEd Maste 				readcn |= MSDOSFSFREE & 0xfff;
845*98dc8da5SEd Maste 			}
846*98dc8da5SEd Maste 			putushort(&bp->b_data[bo], readcn);
847*98dc8da5SEd Maste 			break;
848*98dc8da5SEd Maste 		case FAT16_MASK:
849*98dc8da5SEd Maste 			cluster = getushort(&bp->b_data[bo]);
850*98dc8da5SEd Maste 			putushort(&bp->b_data[bo], MSDOSFSFREE);
851*98dc8da5SEd Maste 			break;
852*98dc8da5SEd Maste 		case FAT32_MASK:
853*98dc8da5SEd Maste 			cluster = getulong(&bp->b_data[bo]);
854*98dc8da5SEd Maste 			putulong(&bp->b_data[bo],
855*98dc8da5SEd Maste 				 (MSDOSFSFREE & FAT32_MASK) | (cluster & ~FAT32_MASK));
856*98dc8da5SEd Maste 			break;
857*98dc8da5SEd Maste 		}
858*98dc8da5SEd Maste 		cluster &= pmp->pm_fatmask;
859*98dc8da5SEd Maste 		if ((cluster | ~pmp->pm_fatmask) >= CLUST_RSRVD)
860*98dc8da5SEd Maste 			cluster |= pmp->pm_fatmask;
861*98dc8da5SEd Maste 	}
862*98dc8da5SEd Maste 	if (bp)
863*98dc8da5SEd Maste 		updatefats(pmp, bp, bn);
864*98dc8da5SEd Maste 	return (0);
865*98dc8da5SEd Maste }
866*98dc8da5SEd Maste 
867*98dc8da5SEd Maste /*
868*98dc8da5SEd Maste  * Read in FAT blocks looking for free clusters. For every free cluster
869*98dc8da5SEd Maste  * found turn off its corresponding bit in the pm_inusemap.
870*98dc8da5SEd Maste  */
871*98dc8da5SEd Maste int
872*98dc8da5SEd Maste fillinusemap(struct msdosfsmount *pmp)
873*98dc8da5SEd Maste {
874*98dc8da5SEd Maste 	struct buf *bp = NULL;
875*98dc8da5SEd Maste 	u_long bn, bo, bsize, byteoffset, cn, readcn;
876*98dc8da5SEd Maste 	int error;
877*98dc8da5SEd Maste 
878*98dc8da5SEd Maste 	/*
879*98dc8da5SEd Maste 	 * Mark all clusters in use, we mark the free ones in the FAT scan
880*98dc8da5SEd Maste 	 * loop further down.
881*98dc8da5SEd Maste 	 */
882*98dc8da5SEd Maste 	for (cn = 0; cn < (pmp->pm_maxcluster + N_INUSEBITS) / N_INUSEBITS; cn++)
883*98dc8da5SEd Maste 		pmp->pm_inusemap[cn] = FULL_RUN;
884*98dc8da5SEd Maste 
885*98dc8da5SEd Maste 	/*
886*98dc8da5SEd Maste 	 * Figure how many free clusters are in the filesystem by ripping
887*98dc8da5SEd Maste 	 * through the FAT counting the number of entries whose content is
888*98dc8da5SEd Maste 	 * zero.  These represent free clusters.
889*98dc8da5SEd Maste 	 */
890*98dc8da5SEd Maste 	pmp->pm_freeclustercount = 0;
891*98dc8da5SEd Maste 	for (cn = CLUST_FIRST; cn <= pmp->pm_maxcluster; cn++) {
892*98dc8da5SEd Maste 		byteoffset = FATOFS(pmp, cn);
893*98dc8da5SEd Maste 		bo = byteoffset % pmp->pm_fatblocksize;
894*98dc8da5SEd Maste 		if (bo == 0 || bp == NULL) {
895*98dc8da5SEd Maste 			/* Read new FAT block */
896*98dc8da5SEd Maste 			if (bp != NULL)
897*98dc8da5SEd Maste 				brelse(bp);
898*98dc8da5SEd Maste 			fatblock(pmp, byteoffset, &bn, &bsize, NULL);
899*98dc8da5SEd Maste 			error = bread(pmp->pm_devvp, bn, bsize, NOCRED, &bp);
900*98dc8da5SEd Maste 			if (error != 0) {
901*98dc8da5SEd Maste 				brelse(bp);
902*98dc8da5SEd Maste 				return (error);
903*98dc8da5SEd Maste 			}
904*98dc8da5SEd Maste 		}
905*98dc8da5SEd Maste 		if (FAT32(pmp))
906*98dc8da5SEd Maste 			readcn = getulong(&bp->b_data[bo]);
907*98dc8da5SEd Maste 		else
908*98dc8da5SEd Maste 			readcn = getushort(&bp->b_data[bo]);
909*98dc8da5SEd Maste 		if (FAT12(pmp) && (cn & 1))
910*98dc8da5SEd Maste 			readcn >>= 4;
911*98dc8da5SEd Maste 		readcn &= pmp->pm_fatmask;
912*98dc8da5SEd Maste 
913*98dc8da5SEd Maste 		if (readcn == CLUST_FREE)
914*98dc8da5SEd Maste 			usemap_free(pmp, cn);
915*98dc8da5SEd Maste 	}
916*98dc8da5SEd Maste 	if (bp != NULL)
917*98dc8da5SEd Maste 		brelse(bp);
918*98dc8da5SEd Maste 
919*98dc8da5SEd Maste 	for (cn = pmp->pm_maxcluster + 1; cn < (pmp->pm_maxcluster +
920*98dc8da5SEd Maste 	    N_INUSEBITS) / N_INUSEBITS; cn++)
921*98dc8da5SEd Maste 		pmp->pm_inusemap[cn / N_INUSEBITS] |= 1U << (cn % N_INUSEBITS);
922*98dc8da5SEd Maste 
923*98dc8da5SEd Maste 	return (0);
924*98dc8da5SEd Maste }
925*98dc8da5SEd Maste 
926*98dc8da5SEd Maste /*
927*98dc8da5SEd Maste  * Allocate a new cluster and chain it onto the end of the file.
928*98dc8da5SEd Maste  *
929*98dc8da5SEd Maste  * dep	 - the file to extend
930*98dc8da5SEd Maste  * count - number of clusters to allocate
931*98dc8da5SEd Maste  * bpp	 - where to return the address of the buf header for the first new
932*98dc8da5SEd Maste  *	   file block
933*98dc8da5SEd Maste  * ncp	 - where to put cluster number of the first newly allocated cluster
934*98dc8da5SEd Maste  *	   If this pointer is 0, do not return the cluster number.
935*98dc8da5SEd Maste  * flags - see fat.h
936*98dc8da5SEd Maste  *
937*98dc8da5SEd Maste  * NOTE: This function is not responsible for turning on the DE_UPDATE bit of
938*98dc8da5SEd Maste  * the de_flag field of the denode and it does not change the de_FileSize
939*98dc8da5SEd Maste  * field.  This is left for the caller to do.
940*98dc8da5SEd Maste  */
941*98dc8da5SEd Maste int
942*98dc8da5SEd Maste extendfile(struct denode *dep, u_long count, struct buf **bpp, u_long *ncp,
943*98dc8da5SEd Maste     int flags)
944*98dc8da5SEd Maste {
945*98dc8da5SEd Maste 	int error;
946*98dc8da5SEd Maste 	u_long frcn;
947*98dc8da5SEd Maste 	u_long cn, got;
948*98dc8da5SEd Maste 	struct msdosfsmount *pmp = dep->de_pmp;
949*98dc8da5SEd Maste 	struct buf *bp;
950*98dc8da5SEd Maste 
951*98dc8da5SEd Maste 	/*
952*98dc8da5SEd Maste 	 * Don't try to extend the root directory
953*98dc8da5SEd Maste 	 */
954*98dc8da5SEd Maste 	if (dep->de_StartCluster == MSDOSFSROOT
955*98dc8da5SEd Maste 	    && (dep->de_Attributes & ATTR_DIRECTORY)) {
956*98dc8da5SEd Maste #ifdef MSDOSFS_DEBUG
957*98dc8da5SEd Maste 		printf("extendfile(): attempt to extend root directory\n");
958*98dc8da5SEd Maste #endif
959*98dc8da5SEd Maste 		return (ENOSPC);
960*98dc8da5SEd Maste 	}
961*98dc8da5SEd Maste 
962*98dc8da5SEd Maste 	/*
963*98dc8da5SEd Maste 	 * If the "file's last cluster" cache entry is empty, and the file
964*98dc8da5SEd Maste 	 * is not empty, then fill the cache entry by calling pcbmap().
965*98dc8da5SEd Maste 	 */
966*98dc8da5SEd Maste 	if (dep->de_fc[FC_LASTFC].fc_frcn == FCE_EMPTY &&
967*98dc8da5SEd Maste 	    dep->de_StartCluster != 0) {
968*98dc8da5SEd Maste 		error = pcbmap(dep, 0xffff, 0, &cn, 0);
969*98dc8da5SEd Maste 		/* we expect it to return E2BIG */
970*98dc8da5SEd Maste 		if (error != E2BIG)
971*98dc8da5SEd Maste 			return (error);
972*98dc8da5SEd Maste 	}
973*98dc8da5SEd Maste 
974*98dc8da5SEd Maste 	dep->de_fc[FC_NEXTTOLASTFC].fc_frcn =
975*98dc8da5SEd Maste 	    dep->de_fc[FC_LASTFC].fc_frcn;
976*98dc8da5SEd Maste 	dep->de_fc[FC_NEXTTOLASTFC].fc_fsrcn =
977*98dc8da5SEd Maste 	    dep->de_fc[FC_LASTFC].fc_fsrcn;
978*98dc8da5SEd Maste 	while (count > 0) {
979*98dc8da5SEd Maste 		/*
980*98dc8da5SEd Maste 		 * Allocate a new cluster chain and cat onto the end of the
981*98dc8da5SEd Maste 		 * file.  If the file is empty we make de_StartCluster point
982*98dc8da5SEd Maste 		 * to the new block.  Note that de_StartCluster being 0 is
983*98dc8da5SEd Maste 		 * sufficient to be sure the file is empty since we exclude
984*98dc8da5SEd Maste 		 * attempts to extend the root directory above, and the root
985*98dc8da5SEd Maste 		 * dir is the only file with a startcluster of 0 that has
986*98dc8da5SEd Maste 		 * blocks allocated (sort of).
987*98dc8da5SEd Maste 		 */
988*98dc8da5SEd Maste 		if (dep->de_StartCluster == 0)
989*98dc8da5SEd Maste 			cn = 0;
990*98dc8da5SEd Maste 		else
991*98dc8da5SEd Maste 			cn = dep->de_fc[FC_LASTFC].fc_fsrcn + 1;
992*98dc8da5SEd Maste 		error = clusteralloc(pmp, cn, count, CLUST_EOFE, &cn, &got);
993*98dc8da5SEd Maste 		if (error)
994*98dc8da5SEd Maste 			return (error);
995*98dc8da5SEd Maste 
996*98dc8da5SEd Maste 		count -= got;
997*98dc8da5SEd Maste 
998*98dc8da5SEd Maste 		/*
999*98dc8da5SEd Maste 		 * Give them the filesystem relative cluster number if they want
1000*98dc8da5SEd Maste 		 * it.
1001*98dc8da5SEd Maste 		 */
1002*98dc8da5SEd Maste 		if (ncp) {
1003*98dc8da5SEd Maste 			*ncp = cn;
1004*98dc8da5SEd Maste 			ncp = NULL;
1005*98dc8da5SEd Maste 		}
1006*98dc8da5SEd Maste 
1007*98dc8da5SEd Maste 		if (dep->de_StartCluster == 0) {
1008*98dc8da5SEd Maste 			dep->de_StartCluster = cn;
1009*98dc8da5SEd Maste 			frcn = 0;
1010*98dc8da5SEd Maste 		} else {
1011*98dc8da5SEd Maste 			error = fatentry(FAT_SET, pmp,
1012*98dc8da5SEd Maste 					 dep->de_fc[FC_LASTFC].fc_fsrcn,
1013*98dc8da5SEd Maste 					 0, cn);
1014*98dc8da5SEd Maste 			if (error) {
1015*98dc8da5SEd Maste 				clusterfree(pmp, cn, NULL);
1016*98dc8da5SEd Maste 				return (error);
1017*98dc8da5SEd Maste 			}
1018*98dc8da5SEd Maste 			frcn = dep->de_fc[FC_LASTFC].fc_frcn + 1;
1019*98dc8da5SEd Maste 		}
1020*98dc8da5SEd Maste 
1021*98dc8da5SEd Maste 		/*
1022*98dc8da5SEd Maste 		 * Update the "last cluster of the file" entry in the
1023*98dc8da5SEd Maste 		 * denode's FAT cache.
1024*98dc8da5SEd Maste 		 */
1025*98dc8da5SEd Maste 		fc_setcache(dep, FC_LASTFC, frcn + got - 1, cn + got - 1);
1026*98dc8da5SEd Maste 
1027*98dc8da5SEd Maste 		if ((flags & DE_CLEAR) &&
1028*98dc8da5SEd Maste 		    (dep->de_Attributes & ATTR_DIRECTORY)) {
1029*98dc8da5SEd Maste 			while (got-- > 0) {
1030*98dc8da5SEd Maste 				bp = getblk(pmp->pm_devvp,
1031*98dc8da5SEd Maste 				    cntobn(pmp, cn++),
1032*98dc8da5SEd Maste 				    pmp->pm_bpcluster, 0, 0, 0);
1033*98dc8da5SEd Maste 				clrbuf(bp);
1034*98dc8da5SEd Maste 				if (bpp) {
1035*98dc8da5SEd Maste 					*bpp = bp;
1036*98dc8da5SEd Maste 					bpp = NULL;
1037*98dc8da5SEd Maste 				} else {
1038*98dc8da5SEd Maste 					bdwrite(bp);
1039*98dc8da5SEd Maste 				}
1040*98dc8da5SEd Maste 			}
1041*98dc8da5SEd Maste 		}
1042*98dc8da5SEd Maste 	}
1043*98dc8da5SEd Maste 
1044*98dc8da5SEd Maste 	return (0);
1045*98dc8da5SEd Maste }
1046