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