xref: /freebsd/usr.sbin/makefs/msdos/msdosfs_fat.c (revision d485c77f203fb0f4cdc08dea5ff81631b51d8809)
198dc8da5SEd Maste /* $FreeBSD$ */
298dc8da5SEd Maste /*	$NetBSD: msdosfs_fat.c,v 1.28 1997/11/17 15:36:49 ws Exp $	*/
398dc8da5SEd Maste 
498dc8da5SEd Maste /*-
598dc8da5SEd Maste  * SPDX-License-Identifier: BSD-4-Clause
698dc8da5SEd Maste  *
798dc8da5SEd Maste  * Copyright (C) 1994, 1995, 1997 Wolfgang Solfrank.
898dc8da5SEd Maste  * Copyright (C) 1994, 1995, 1997 TooLs GmbH.
998dc8da5SEd Maste  * All rights reserved.
1098dc8da5SEd Maste  * Original code by Paul Popelka (paulp@uts.amdahl.com) (see below).
1198dc8da5SEd Maste  *
1298dc8da5SEd Maste  * Redistribution and use in source and binary forms, with or without
1398dc8da5SEd Maste  * modification, are permitted provided that the following conditions
1498dc8da5SEd Maste  * are met:
1598dc8da5SEd Maste  * 1. Redistributions of source code must retain the above copyright
1698dc8da5SEd Maste  *    notice, this list of conditions and the following disclaimer.
1798dc8da5SEd Maste  * 2. Redistributions in binary form must reproduce the above copyright
1898dc8da5SEd Maste  *    notice, this list of conditions and the following disclaimer in the
1998dc8da5SEd Maste  *    documentation and/or other materials provided with the distribution.
2098dc8da5SEd Maste  * 3. All advertising materials mentioning features or use of this software
2198dc8da5SEd Maste  *    must display the following acknowledgement:
2298dc8da5SEd Maste  *	This product includes software developed by TooLs GmbH.
2398dc8da5SEd Maste  * 4. The name of TooLs GmbH may not be used to endorse or promote products
2498dc8da5SEd Maste  *    derived from this software without specific prior written permission.
2598dc8da5SEd Maste  *
2698dc8da5SEd Maste  * THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``AS IS'' AND ANY EXPRESS OR
2798dc8da5SEd Maste  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
2898dc8da5SEd Maste  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
2998dc8da5SEd Maste  * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
3098dc8da5SEd Maste  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
3198dc8da5SEd Maste  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
3298dc8da5SEd Maste  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
3398dc8da5SEd Maste  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
3498dc8da5SEd Maste  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
3598dc8da5SEd Maste  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
3698dc8da5SEd Maste  */
3798dc8da5SEd Maste /*-
3898dc8da5SEd Maste  * Written by Paul Popelka (paulp@uts.amdahl.com)
3998dc8da5SEd Maste  *
4098dc8da5SEd Maste  * You can do anything you want with this software, just don't say you wrote
4198dc8da5SEd Maste  * it, and don't remove this notice.
4298dc8da5SEd Maste  *
4398dc8da5SEd Maste  * This software is provided "as is".
4498dc8da5SEd Maste  *
4598dc8da5SEd Maste  * The author supplies this software to be publicly redistributed on the
4698dc8da5SEd Maste  * understanding that the author is not responsible for the correct
4798dc8da5SEd Maste  * functioning of this software in any circumstances and is not liable for
4898dc8da5SEd Maste  * any damages caused by this software.
4998dc8da5SEd Maste  *
5098dc8da5SEd Maste  * October 1992
5198dc8da5SEd Maste  */
5298dc8da5SEd Maste 
5398dc8da5SEd Maste #include <sys/param.h>
5498dc8da5SEd Maste #include <sys/errno.h>
5598dc8da5SEd Maste 
5698dc8da5SEd Maste #include <assert.h>
57aaa38524SConrad Meyer #include <stdbool.h>
5898dc8da5SEd Maste #include <stdio.h>
5998dc8da5SEd Maste #include <string.h>
6098dc8da5SEd Maste #include <strings.h>
6198dc8da5SEd Maste 
62476b0ab7SEd Maste #include <fs/msdosfs/bpb.h>
6398dc8da5SEd Maste #include "msdos/direntry.h"
64476b0ab7SEd Maste #include <fs/msdosfs/denode.h>
65476b0ab7SEd Maste #include <fs/msdosfs/fat.h>
66840aca28SEd Maste #include <fs/msdosfs/msdosfsmount.h>
6798dc8da5SEd Maste 
68*d485c77fSKonstantin Belousov #undef clrbuf
69*d485c77fSKonstantin Belousov #include "ffs/buf.h"
7098dc8da5SEd Maste #include "makefs.h"
7198dc8da5SEd Maste #include "msdos.h"
7298dc8da5SEd Maste 
7398dc8da5SEd Maste #define	FULL_RUN	((u_int)0xffffffff)
7498dc8da5SEd Maste #define	SYNCHRONOUS_WRITES(pmp)	1
7598dc8da5SEd Maste 
7698dc8da5SEd Maste static int	chainalloc(struct msdosfsmount *pmp, u_long start,
7798dc8da5SEd Maste 		    u_long count, u_long fillwith, u_long *retcluster,
7898dc8da5SEd Maste 		    u_long *got);
7998dc8da5SEd Maste static int	chainlength(struct msdosfsmount *pmp, u_long start,
8098dc8da5SEd Maste 		    u_long count);
8198dc8da5SEd Maste static void	fatblock(struct msdosfsmount *pmp, u_long ofs, u_long *bnp,
8298dc8da5SEd Maste 		    u_long *sizep, u_long *bop);
8398dc8da5SEd Maste static int	fatchain(struct msdosfsmount *pmp, u_long start, u_long count,
8498dc8da5SEd Maste 		    u_long fillwith);
8598dc8da5SEd Maste static void	fc_lookup(struct denode *dep, u_long findcn, u_long *frcnp,
8698dc8da5SEd Maste 		    u_long *fsrcnp);
87*d485c77fSKonstantin Belousov static void	updatefats(struct msdosfsmount *pmp, struct m_buf *bp,
8898dc8da5SEd Maste 		    u_long fatbn);
8998dc8da5SEd Maste static __inline void
9098dc8da5SEd Maste 		usemap_alloc(struct msdosfsmount *pmp, u_long cn);
9198dc8da5SEd Maste static __inline void
9298dc8da5SEd Maste 		usemap_free(struct msdosfsmount *pmp, u_long cn);
9398dc8da5SEd Maste static int	clusteralloc1(struct msdosfsmount *pmp, u_long start,
9498dc8da5SEd Maste 		    u_long count, u_long fillwith, u_long *retcluster,
9598dc8da5SEd Maste 		    u_long *got);
9698dc8da5SEd Maste 
9798dc8da5SEd Maste static void
9898dc8da5SEd Maste fatblock(struct msdosfsmount *pmp, u_long ofs, u_long *bnp, u_long *sizep,
9998dc8da5SEd Maste     u_long *bop)
10098dc8da5SEd Maste {
10198dc8da5SEd Maste 	u_long bn, size;
10298dc8da5SEd Maste 
10398dc8da5SEd Maste 	bn = ofs / pmp->pm_fatblocksize * pmp->pm_fatblocksec;
10498dc8da5SEd Maste 	size = MIN(pmp->pm_fatblocksec, pmp->pm_FATsecs - bn)
10598dc8da5SEd Maste 	    * DEV_BSIZE;
10698dc8da5SEd Maste 	bn += pmp->pm_fatblk + pmp->pm_curfat * pmp->pm_FATsecs;
10798dc8da5SEd Maste 
10898dc8da5SEd Maste 	if (bnp)
10998dc8da5SEd Maste 		*bnp = bn;
11098dc8da5SEd Maste 	if (sizep)
11198dc8da5SEd Maste 		*sizep = size;
11298dc8da5SEd Maste 	if (bop)
11398dc8da5SEd Maste 		*bop = ofs % pmp->pm_fatblocksize;
11498dc8da5SEd Maste }
11598dc8da5SEd Maste 
11698dc8da5SEd Maste /*
11798dc8da5SEd Maste  * Map the logical cluster number of a file into a physical disk sector
11898dc8da5SEd Maste  * that is filesystem relative.
11998dc8da5SEd Maste  *
12098dc8da5SEd Maste  * dep	  - address of denode representing the file of interest
12198dc8da5SEd Maste  * findcn - file relative cluster whose filesystem relative cluster number
12298dc8da5SEd Maste  *	    and/or block number are/is to be found
12398dc8da5SEd Maste  * bnp	  - address of where to place the filesystem relative block number.
12498dc8da5SEd Maste  *	    If this pointer is null then don't return this quantity.
12598dc8da5SEd Maste  * cnp	  - address of where to place the filesystem relative cluster number.
12698dc8da5SEd Maste  *	    If this pointer is null then don't return this quantity.
12798dc8da5SEd Maste  * sp     - pointer to returned block size
12898dc8da5SEd Maste  *
12998dc8da5SEd Maste  * NOTE: Either bnp or cnp must be non-null.
13098dc8da5SEd Maste  * This function has one side effect.  If the requested file relative cluster
13198dc8da5SEd Maste  * is beyond the end of file, then the actual number of clusters in the file
13298dc8da5SEd Maste  * is returned in *cnp.  This is useful for determining how long a directory is.
13398dc8da5SEd Maste  *  If cnp is null, nothing is returned.
13498dc8da5SEd Maste  */
13598dc8da5SEd Maste int
13698dc8da5SEd Maste pcbmap(struct denode *dep, u_long findcn, daddr_t *bnp, u_long *cnp, int *sp)
13798dc8da5SEd Maste {
13898dc8da5SEd Maste 	int error;
13998dc8da5SEd Maste 	u_long i;
14098dc8da5SEd Maste 	u_long cn;
14198dc8da5SEd Maste 	u_long prevcn = 0; /* XXX: prevcn could be used unititialized */
14298dc8da5SEd Maste 	u_long byteoffset;
14398dc8da5SEd Maste 	u_long bn;
14498dc8da5SEd Maste 	u_long bo;
145*d485c77fSKonstantin Belousov 	struct m_buf *bp = NULL;
14698dc8da5SEd Maste 	u_long bp_bn = -1;
14798dc8da5SEd Maste 	struct msdosfsmount *pmp = dep->de_pmp;
14898dc8da5SEd Maste 	u_long bsize;
14998dc8da5SEd Maste 
15098dc8da5SEd Maste 	assert(bnp != NULL || cnp != NULL || sp != NULL);
15198dc8da5SEd Maste 
15298dc8da5SEd Maste 	cn = dep->de_StartCluster;
15398dc8da5SEd Maste 	/*
15498dc8da5SEd Maste 	 * The "file" that makes up the root directory is contiguous,
15598dc8da5SEd Maste 	 * permanently allocated, of fixed size, and is not made up of
15698dc8da5SEd Maste 	 * clusters.  If the cluster number is beyond the end of the root
15798dc8da5SEd Maste 	 * directory, then return the number of clusters in the file.
15898dc8da5SEd Maste 	 */
15998dc8da5SEd Maste 	if (cn == MSDOSFSROOT) {
16098dc8da5SEd Maste 		if (dep->de_Attributes & ATTR_DIRECTORY) {
16198dc8da5SEd Maste 			if (de_cn2off(pmp, findcn) >= dep->de_FileSize) {
16298dc8da5SEd Maste 				if (cnp)
16398dc8da5SEd Maste 					*cnp = de_bn2cn(pmp, pmp->pm_rootdirsize);
16498dc8da5SEd Maste 				return (E2BIG);
16598dc8da5SEd Maste 			}
16698dc8da5SEd Maste 			if (bnp)
16798dc8da5SEd Maste 				*bnp = pmp->pm_rootdirblk + de_cn2bn(pmp, findcn);
16898dc8da5SEd Maste 			if (cnp)
16998dc8da5SEd Maste 				*cnp = MSDOSFSROOT;
17098dc8da5SEd Maste 			if (sp)
17198dc8da5SEd Maste 				*sp = MIN(pmp->pm_bpcluster,
17298dc8da5SEd Maste 				    dep->de_FileSize - de_cn2off(pmp, findcn));
17398dc8da5SEd Maste 			return (0);
17498dc8da5SEd Maste 		} else {		/* just an empty file */
17598dc8da5SEd Maste 			if (cnp)
17698dc8da5SEd Maste 				*cnp = 0;
17798dc8da5SEd Maste 			return (E2BIG);
17898dc8da5SEd Maste 		}
17998dc8da5SEd Maste 	}
18098dc8da5SEd Maste 
18198dc8da5SEd Maste 	/*
18298dc8da5SEd Maste 	 * All other files do I/O in cluster sized blocks
18398dc8da5SEd Maste 	 */
18498dc8da5SEd Maste 	if (sp)
18598dc8da5SEd Maste 		*sp = pmp->pm_bpcluster;
18698dc8da5SEd Maste 
18798dc8da5SEd Maste 	/*
18898dc8da5SEd Maste 	 * Rummage around in the FAT cache, maybe we can avoid tromping
18998dc8da5SEd Maste 	 * through every FAT entry for the file. And, keep track of how far
19098dc8da5SEd Maste 	 * off the cache was from where we wanted to be.
19198dc8da5SEd Maste 	 */
19298dc8da5SEd Maste 	i = 0;
19398dc8da5SEd Maste 	fc_lookup(dep, findcn, &i, &cn);
19498dc8da5SEd Maste 
19598dc8da5SEd Maste 	/*
19698dc8da5SEd Maste 	 * Handle all other files or directories the normal way.
19798dc8da5SEd Maste 	 */
19898dc8da5SEd Maste 	for (; i < findcn; i++) {
19998dc8da5SEd Maste 		/*
20098dc8da5SEd Maste 		 * Stop with all reserved clusters, not just with EOF.
20198dc8da5SEd Maste 		 */
20298dc8da5SEd Maste 		if ((cn | ~pmp->pm_fatmask) >= CLUST_RSRVD)
20398dc8da5SEd Maste 			goto hiteof;
20498dc8da5SEd Maste 		byteoffset = FATOFS(pmp, cn);
20598dc8da5SEd Maste 		fatblock(pmp, byteoffset, &bn, &bsize, &bo);
20698dc8da5SEd Maste 		if (bn != bp_bn) {
20798dc8da5SEd Maste 			if (bp)
20898dc8da5SEd Maste 				brelse(bp);
209*d485c77fSKonstantin Belousov 			error = bread((void *)pmp->pm_devvp, bn, bsize,
210*d485c77fSKonstantin Belousov 			    NOCRED, &bp);
21198dc8da5SEd Maste 			if (error) {
21298dc8da5SEd Maste 				brelse(bp);
21398dc8da5SEd Maste 				return (error);
21498dc8da5SEd Maste 			}
21598dc8da5SEd Maste 			bp_bn = bn;
21698dc8da5SEd Maste 		}
21798dc8da5SEd Maste 		prevcn = cn;
21898dc8da5SEd Maste 		if (bo >= bsize) {
21998dc8da5SEd Maste 			if (bp)
22098dc8da5SEd Maste 				brelse(bp);
22198dc8da5SEd Maste 			return (EIO);
22298dc8da5SEd Maste 		}
22398dc8da5SEd Maste 		if (FAT32(pmp))
224ef633620SEd Maste 			cn = getulong(bp->b_data + bo);
22598dc8da5SEd Maste 		else
226ef633620SEd Maste 			cn = getushort(bp->b_data + bo);
22798dc8da5SEd Maste 		if (FAT12(pmp) && (prevcn & 1))
22898dc8da5SEd Maste 			cn >>= 4;
22998dc8da5SEd Maste 		cn &= pmp->pm_fatmask;
23098dc8da5SEd Maste 
23198dc8da5SEd Maste 		/*
23298dc8da5SEd Maste 		 * Force the special cluster numbers
23398dc8da5SEd Maste 		 * to be the same for all cluster sizes
23498dc8da5SEd Maste 		 * to let the rest of msdosfs handle
23598dc8da5SEd Maste 		 * all cases the same.
23698dc8da5SEd Maste 		 */
23798dc8da5SEd Maste 		if ((cn | ~pmp->pm_fatmask) >= CLUST_RSRVD)
23898dc8da5SEd Maste 			cn |= ~pmp->pm_fatmask;
23998dc8da5SEd Maste 	}
24098dc8da5SEd Maste 
24198dc8da5SEd Maste 	if (!MSDOSFSEOF(pmp, cn)) {
24298dc8da5SEd Maste 		if (bp)
24398dc8da5SEd Maste 			brelse(bp);
24498dc8da5SEd Maste 		if (bnp)
24598dc8da5SEd Maste 			*bnp = cntobn(pmp, cn);
24698dc8da5SEd Maste 		if (cnp)
24798dc8da5SEd Maste 			*cnp = cn;
24898dc8da5SEd Maste 		fc_setcache(dep, FC_LASTMAP, i, cn);
24998dc8da5SEd Maste 		return (0);
25098dc8da5SEd Maste 	}
25198dc8da5SEd Maste 
25298dc8da5SEd Maste hiteof:;
25398dc8da5SEd Maste 	if (cnp)
25498dc8da5SEd Maste 		*cnp = i;
25598dc8da5SEd Maste 	if (bp)
25698dc8da5SEd Maste 		brelse(bp);
25798dc8da5SEd Maste 	/* update last file cluster entry in the FAT cache */
25898dc8da5SEd Maste 	fc_setcache(dep, FC_LASTFC, i - 1, prevcn);
25998dc8da5SEd Maste 	return (E2BIG);
26098dc8da5SEd Maste }
26198dc8da5SEd Maste 
26298dc8da5SEd Maste /*
26398dc8da5SEd Maste  * Find the closest entry in the FAT cache to the cluster we are looking
26498dc8da5SEd Maste  * for.
26598dc8da5SEd Maste  */
26698dc8da5SEd Maste static void
26798dc8da5SEd Maste fc_lookup(struct denode *dep, u_long findcn, u_long *frcnp, u_long *fsrcnp)
26898dc8da5SEd Maste {
26998dc8da5SEd Maste 	int i;
27098dc8da5SEd Maste 	u_long cn;
27198dc8da5SEd Maste 	struct fatcache *closest = NULL;
27298dc8da5SEd Maste 
27398dc8da5SEd Maste 	for (i = 0; i < FC_SIZE; i++) {
27498dc8da5SEd Maste 		cn = dep->de_fc[i].fc_frcn;
27598dc8da5SEd Maste 		if (cn != FCE_EMPTY && cn <= findcn) {
27698dc8da5SEd Maste 			if (closest == NULL || cn > closest->fc_frcn)
27798dc8da5SEd Maste 				closest = &dep->de_fc[i];
27898dc8da5SEd Maste 		}
27998dc8da5SEd Maste 	}
28098dc8da5SEd Maste 	if (closest) {
28198dc8da5SEd Maste 		*frcnp = closest->fc_frcn;
28298dc8da5SEd Maste 		*fsrcnp = closest->fc_fsrcn;
28398dc8da5SEd Maste 	}
28498dc8da5SEd Maste }
28598dc8da5SEd Maste 
28698dc8da5SEd Maste /*
28798dc8da5SEd Maste  * Purge the FAT cache in denode dep of all entries relating to file
28898dc8da5SEd Maste  * relative cluster frcn and beyond.
28998dc8da5SEd Maste  */
29098dc8da5SEd Maste void
29198dc8da5SEd Maste fc_purge(struct denode *dep, u_int frcn)
29298dc8da5SEd Maste {
29398dc8da5SEd Maste 	int i;
29498dc8da5SEd Maste 	struct fatcache *fcp;
29598dc8da5SEd Maste 
29698dc8da5SEd Maste 	fcp = dep->de_fc;
29798dc8da5SEd Maste 	for (i = 0; i < FC_SIZE; i++, fcp++) {
29898dc8da5SEd Maste 		if (fcp->fc_frcn >= frcn)
29998dc8da5SEd Maste 			fcp->fc_frcn = FCE_EMPTY;
30098dc8da5SEd Maste 	}
30198dc8da5SEd Maste }
30298dc8da5SEd Maste 
30398dc8da5SEd Maste /*
30498dc8da5SEd Maste  * Update the FAT.
30598dc8da5SEd Maste  * If mirroring the FAT, update all copies, with the first copy as last.
30698dc8da5SEd Maste  * Else update only the current FAT (ignoring the others).
30798dc8da5SEd Maste  *
30898dc8da5SEd Maste  * pmp	 - msdosfsmount structure for filesystem to update
30998dc8da5SEd Maste  * bp	 - addr of modified FAT block
31098dc8da5SEd Maste  * fatbn - block number relative to begin of filesystem of the modified FAT block.
31198dc8da5SEd Maste  */
31298dc8da5SEd Maste static void
313*d485c77fSKonstantin Belousov updatefats(struct msdosfsmount *pmp, struct m_buf *bp, u_long fatbn)
31498dc8da5SEd Maste {
315*d485c77fSKonstantin Belousov 	struct m_buf *bpn;
31698dc8da5SEd Maste 	int cleanfat, i;
31798dc8da5SEd Maste 
31898dc8da5SEd Maste #ifdef MSDOSFS_DEBUG
31998dc8da5SEd Maste 	printf("updatefats(pmp %p, bp %p, fatbn %lu)\n", pmp, bp, fatbn);
32098dc8da5SEd Maste #endif
32198dc8da5SEd Maste 
32298dc8da5SEd Maste 	if (pmp->pm_flags & MSDOSFS_FATMIRROR) {
32398dc8da5SEd Maste 		/*
32498dc8da5SEd Maste 		 * Now copy the block(s) of the modified FAT to the other copies of
32598dc8da5SEd Maste 		 * the FAT and write them out.  This is faster than reading in the
32698dc8da5SEd Maste 		 * other FATs and then writing them back out.  This could tie up
32798dc8da5SEd Maste 		 * the FAT for quite a while. Preventing others from accessing it.
32898dc8da5SEd Maste 		 * To prevent us from going after the FAT quite so much we use
32998dc8da5SEd Maste 		 * delayed writes, unless they specified "synchronous" when the
33098dc8da5SEd Maste 		 * filesystem was mounted.  If synch is asked for then use
33198dc8da5SEd Maste 		 * bwrite()'s and really slow things down.
33298dc8da5SEd Maste 		 */
33398dc8da5SEd Maste 		if (fatbn != pmp->pm_fatblk || FAT12(pmp))
33498dc8da5SEd Maste 			cleanfat = 0;
33598dc8da5SEd Maste 		else if (FAT16(pmp))
33698dc8da5SEd Maste 			cleanfat = 16;
33798dc8da5SEd Maste 		else
33898dc8da5SEd Maste 			cleanfat = 32;
33998dc8da5SEd Maste 		for (i = 1; i < pmp->pm_FATs; i++) {
34098dc8da5SEd Maste 			fatbn += pmp->pm_FATsecs;
34198dc8da5SEd Maste 			/* getblk() never fails */
342*d485c77fSKonstantin Belousov 			bpn = getblk((void *)pmp->pm_devvp, fatbn,
343*d485c77fSKonstantin Belousov 			    bp->b_bcount, 0, 0, 0);
34498dc8da5SEd Maste 			memcpy(bpn->b_data, bp->b_data, bp->b_bcount);
34598dc8da5SEd Maste 			/* Force the clean bit on in the other copies. */
34698dc8da5SEd Maste 			if (cleanfat == 16)
34798dc8da5SEd Maste 				((uint8_t *)bpn->b_data)[3] |= 0x80;
34898dc8da5SEd Maste 			else if (cleanfat == 32)
34998dc8da5SEd Maste 				((uint8_t *)bpn->b_data)[7] |= 0x08;
35098dc8da5SEd Maste 			if (SYNCHRONOUS_WRITES(pmp))
35198dc8da5SEd Maste 				bwrite(bpn);
35298dc8da5SEd Maste 			else
35398dc8da5SEd Maste 				bdwrite(bpn);
35498dc8da5SEd Maste 		}
35598dc8da5SEd Maste 	}
35698dc8da5SEd Maste 
35798dc8da5SEd Maste 	/*
35898dc8da5SEd Maste 	 * Write out the first (or current) FAT last.
35998dc8da5SEd Maste 	 */
36098dc8da5SEd Maste 	if (SYNCHRONOUS_WRITES(pmp))
36198dc8da5SEd Maste 		bwrite(bp);
36298dc8da5SEd Maste 	else
36398dc8da5SEd Maste 		bdwrite(bp);
36498dc8da5SEd Maste }
36598dc8da5SEd Maste 
36698dc8da5SEd Maste /*
36798dc8da5SEd Maste  * Updating entries in 12 bit FATs is a pain in the butt.
36898dc8da5SEd Maste  *
36998dc8da5SEd Maste  * The following picture shows where nibbles go when moving from a 12 bit
37098dc8da5SEd Maste  * cluster number into the appropriate bytes in the FAT.
37198dc8da5SEd Maste  *
37298dc8da5SEd Maste  *	byte m        byte m+1      byte m+2
37398dc8da5SEd Maste  *	+----+----+   +----+----+   +----+----+
37498dc8da5SEd Maste  *	|  0    1 |   |  2    3 |   |  4    5 |   FAT bytes
37598dc8da5SEd Maste  *	+----+----+   +----+----+   +----+----+
37698dc8da5SEd Maste  *
37798dc8da5SEd Maste  *	+----+----+----+   +----+----+----+
37898dc8da5SEd Maste  *	|  3    0    1 |   |  4    5    2 |
37998dc8da5SEd Maste  *	+----+----+----+   +----+----+----+
38098dc8da5SEd Maste  *	cluster n	   cluster n+1
38198dc8da5SEd Maste  *
38298dc8da5SEd Maste  * Where n is even. m = n + (n >> 2)
38398dc8da5SEd Maste  *
38498dc8da5SEd Maste  */
38598dc8da5SEd Maste static __inline void
38698dc8da5SEd Maste usemap_alloc(struct msdosfsmount *pmp, u_long cn)
38798dc8da5SEd Maste {
38898dc8da5SEd Maste 
38998dc8da5SEd Maste 	assert(cn <= pmp->pm_maxcluster);
39098dc8da5SEd Maste 	assert((pmp->pm_flags & MSDOSFSMNT_RONLY) == 0);
39198dc8da5SEd Maste 	assert((pmp->pm_inusemap[cn / N_INUSEBITS] & (1 << (cn % N_INUSEBITS)))
39298dc8da5SEd Maste 	    == 0);
39398dc8da5SEd Maste 	assert(pmp->pm_freeclustercount > 0);
39498dc8da5SEd Maste 
39598dc8da5SEd Maste 	pmp->pm_inusemap[cn / N_INUSEBITS] |= 1U << (cn % N_INUSEBITS);
39698dc8da5SEd Maste 	pmp->pm_freeclustercount--;
39798dc8da5SEd Maste 	pmp->pm_flags |= MSDOSFS_FSIMOD;
39898dc8da5SEd Maste }
39998dc8da5SEd Maste 
40098dc8da5SEd Maste static __inline void
40198dc8da5SEd Maste usemap_free(struct msdosfsmount *pmp, u_long cn)
40298dc8da5SEd Maste {
40398dc8da5SEd Maste 
40498dc8da5SEd Maste 	assert(cn <= pmp->pm_maxcluster);
40598dc8da5SEd Maste 	assert((pmp->pm_flags & MSDOSFSMNT_RONLY) == 0);
40698dc8da5SEd Maste 	assert((pmp->pm_inusemap[cn / N_INUSEBITS] & (1 << (cn % N_INUSEBITS)))
40798dc8da5SEd Maste 	    != 0);
40898dc8da5SEd Maste 
40998dc8da5SEd Maste 	pmp->pm_freeclustercount++;
41098dc8da5SEd Maste 	pmp->pm_flags |= MSDOSFS_FSIMOD;
41198dc8da5SEd Maste 	pmp->pm_inusemap[cn / N_INUSEBITS] &= ~(1U << (cn % N_INUSEBITS));
41298dc8da5SEd Maste }
41398dc8da5SEd Maste 
41498dc8da5SEd Maste int
41598dc8da5SEd Maste clusterfree(struct msdosfsmount *pmp, u_long cluster, u_long *oldcnp)
41698dc8da5SEd Maste {
41798dc8da5SEd Maste 	int error;
41898dc8da5SEd Maste 	u_long oldcn;
41998dc8da5SEd Maste 
42098dc8da5SEd Maste 	error = fatentry(FAT_GET_AND_SET, pmp, cluster, &oldcn, MSDOSFSFREE);
42198dc8da5SEd Maste 	if (error)
42298dc8da5SEd Maste 		return (error);
42398dc8da5SEd Maste 	/*
42498dc8da5SEd Maste 	 * If the cluster was successfully marked free, then update
42598dc8da5SEd Maste 	 * the count of free clusters, and turn off the "allocated"
42698dc8da5SEd Maste 	 * bit in the "in use" cluster bit map.
42798dc8da5SEd Maste 	 */
42898dc8da5SEd Maste 	usemap_free(pmp, cluster);
42998dc8da5SEd Maste 	if (oldcnp)
43098dc8da5SEd Maste 		*oldcnp = oldcn;
43198dc8da5SEd Maste 	return (0);
43298dc8da5SEd Maste }
43398dc8da5SEd Maste 
43498dc8da5SEd Maste /*
43598dc8da5SEd Maste  * Get or Set or 'Get and Set' the cluster'th entry in the FAT.
43698dc8da5SEd Maste  *
43798dc8da5SEd Maste  * function	- whether to get or set a FAT entry
43898dc8da5SEd Maste  * pmp		- address of the msdosfsmount structure for the filesystem
43998dc8da5SEd Maste  *		  whose FAT is to be manipulated.
44098dc8da5SEd Maste  * cn		- which cluster is of interest
44198dc8da5SEd Maste  * oldcontents	- address of a word that is to receive the contents of the
44298dc8da5SEd Maste  *		  cluster'th entry if this is a get function
44398dc8da5SEd Maste  * newcontents	- the new value to be written into the cluster'th element of
44498dc8da5SEd Maste  *		  the FAT if this is a set function.
44598dc8da5SEd Maste  *
44698dc8da5SEd Maste  * This function can also be used to free a cluster by setting the FAT entry
44798dc8da5SEd Maste  * for a cluster to 0.
44898dc8da5SEd Maste  *
44998dc8da5SEd Maste  * All copies of the FAT are updated if this is a set function. NOTE: If
45098dc8da5SEd Maste  * fatentry() marks a cluster as free it does not update the inusemap in
45198dc8da5SEd Maste  * the msdosfsmount structure. This is left to the caller.
45298dc8da5SEd Maste  */
45398dc8da5SEd Maste int
45498dc8da5SEd Maste fatentry(int function, struct msdosfsmount *pmp, u_long cn, u_long *oldcontents,
45598dc8da5SEd Maste     u_long newcontents)
45698dc8da5SEd Maste {
45798dc8da5SEd Maste 	int error;
45898dc8da5SEd Maste 	u_long readcn;
45998dc8da5SEd Maste 	u_long bn, bo, bsize, byteoffset;
460*d485c77fSKonstantin Belousov 	struct m_buf *bp;
46198dc8da5SEd Maste 
46298dc8da5SEd Maste #ifdef	MSDOSFS_DEBUG
46398dc8da5SEd Maste 	printf("fatentry(func %d, pmp %p, clust %lu, oldcon %p, newcon %lx)\n",
46498dc8da5SEd Maste 	    function, pmp, cn, oldcontents, newcontents);
46598dc8da5SEd Maste #endif
46698dc8da5SEd Maste 
46798dc8da5SEd Maste #ifdef DIAGNOSTIC
46898dc8da5SEd Maste 	/*
46998dc8da5SEd Maste 	 * Be sure they asked us to do something.
47098dc8da5SEd Maste 	 */
47198dc8da5SEd Maste 	if ((function & (FAT_SET | FAT_GET)) == 0) {
47298dc8da5SEd Maste #ifdef MSDOSFS_DEBUG
47398dc8da5SEd Maste 		printf("fatentry(): function code doesn't specify get or set\n");
47498dc8da5SEd Maste #endif
47598dc8da5SEd Maste 		return (EINVAL);
47698dc8da5SEd Maste 	}
47798dc8da5SEd Maste 
47898dc8da5SEd Maste 	/*
47998dc8da5SEd Maste 	 * If they asked us to return a cluster number but didn't tell us
48098dc8da5SEd Maste 	 * where to put it, give them an error.
48198dc8da5SEd Maste 	 */
48298dc8da5SEd Maste 	if ((function & FAT_GET) && oldcontents == NULL) {
48398dc8da5SEd Maste #ifdef MSDOSFS_DEBUG
48498dc8da5SEd Maste 		printf("fatentry(): get function with no place to put result\n");
48598dc8da5SEd Maste #endif
48698dc8da5SEd Maste 		return (EINVAL);
48798dc8da5SEd Maste 	}
48898dc8da5SEd Maste #endif
48998dc8da5SEd Maste 
49098dc8da5SEd Maste 	/*
49198dc8da5SEd Maste 	 * Be sure the requested cluster is in the filesystem.
49298dc8da5SEd Maste 	 */
49398dc8da5SEd Maste 	if (cn < CLUST_FIRST || cn > pmp->pm_maxcluster)
49498dc8da5SEd Maste 		return (EINVAL);
49598dc8da5SEd Maste 
49698dc8da5SEd Maste 	byteoffset = FATOFS(pmp, cn);
49798dc8da5SEd Maste 	fatblock(pmp, byteoffset, &bn, &bsize, &bo);
498*d485c77fSKonstantin Belousov 	error = bread((void *)pmp->pm_devvp, bn, bsize, NOCRED, &bp);
49998dc8da5SEd Maste 	if (error) {
50098dc8da5SEd Maste 		brelse(bp);
50198dc8da5SEd Maste 		return (error);
50298dc8da5SEd Maste 	}
50398dc8da5SEd Maste 
50498dc8da5SEd Maste 	if (function & FAT_GET) {
50598dc8da5SEd Maste 		if (FAT32(pmp))
506ef633620SEd Maste 			readcn = getulong(bp->b_data + bo);
50798dc8da5SEd Maste 		else
508ef633620SEd Maste 			readcn = getushort(bp->b_data + bo);
50998dc8da5SEd Maste 		if (FAT12(pmp) & (cn & 1))
51098dc8da5SEd Maste 			readcn >>= 4;
51198dc8da5SEd Maste 		readcn &= pmp->pm_fatmask;
51298dc8da5SEd Maste 		/* map reserved FAT entries to same values for all FATs */
51398dc8da5SEd Maste 		if ((readcn | ~pmp->pm_fatmask) >= CLUST_RSRVD)
51498dc8da5SEd Maste 			readcn |= ~pmp->pm_fatmask;
51598dc8da5SEd Maste 		*oldcontents = readcn;
51698dc8da5SEd Maste 	}
51798dc8da5SEd Maste 	if (function & FAT_SET) {
51898dc8da5SEd Maste 		switch (pmp->pm_fatmask) {
51998dc8da5SEd Maste 		case FAT12_MASK:
520ef633620SEd Maste 			readcn = getushort(bp->b_data + bo);
52198dc8da5SEd Maste 			if (cn & 1) {
52298dc8da5SEd Maste 				readcn &= 0x000f;
52398dc8da5SEd Maste 				readcn |= newcontents << 4;
52498dc8da5SEd Maste 			} else {
52598dc8da5SEd Maste 				readcn &= 0xf000;
52698dc8da5SEd Maste 				readcn |= newcontents & 0xfff;
52798dc8da5SEd Maste 			}
528ef633620SEd Maste 			putushort(bp->b_data + bo, readcn);
52998dc8da5SEd Maste 			break;
53098dc8da5SEd Maste 		case FAT16_MASK:
531ef633620SEd Maste 			putushort(bp->b_data + bo, newcontents);
53298dc8da5SEd Maste 			break;
53398dc8da5SEd Maste 		case FAT32_MASK:
53498dc8da5SEd Maste 			/*
53598dc8da5SEd Maste 			 * According to spec we have to retain the
53698dc8da5SEd Maste 			 * high order bits of the FAT entry.
53798dc8da5SEd Maste 			 */
538ef633620SEd Maste 			readcn = getulong(bp->b_data + bo);
53998dc8da5SEd Maste 			readcn &= ~FAT32_MASK;
54098dc8da5SEd Maste 			readcn |= newcontents & FAT32_MASK;
541ef633620SEd Maste 			putulong(bp->b_data + bo, readcn);
54298dc8da5SEd Maste 			break;
54398dc8da5SEd Maste 		}
54498dc8da5SEd Maste 		updatefats(pmp, bp, bn);
54598dc8da5SEd Maste 		bp = NULL;
54698dc8da5SEd Maste 		pmp->pm_fmod = 1;
54798dc8da5SEd Maste 	}
54898dc8da5SEd Maste 	if (bp)
54998dc8da5SEd Maste 		brelse(bp);
55098dc8da5SEd Maste 	return (0);
55198dc8da5SEd Maste }
55298dc8da5SEd Maste 
55398dc8da5SEd Maste /*
55498dc8da5SEd Maste  * Update a contiguous cluster chain
55598dc8da5SEd Maste  *
55698dc8da5SEd Maste  * pmp	    - mount point
55798dc8da5SEd Maste  * start    - first cluster of chain
55898dc8da5SEd Maste  * count    - number of clusters in chain
55998dc8da5SEd Maste  * fillwith - what to write into FAT entry of last cluster
56098dc8da5SEd Maste  */
56198dc8da5SEd Maste static int
56298dc8da5SEd Maste fatchain(struct msdosfsmount *pmp, u_long start, u_long count, u_long fillwith)
56398dc8da5SEd Maste {
56498dc8da5SEd Maste 	int error;
56598dc8da5SEd Maste 	u_long bn, bo, bsize, byteoffset, readcn, newc;
566*d485c77fSKonstantin Belousov 	struct m_buf *bp;
56798dc8da5SEd Maste 
56898dc8da5SEd Maste #ifdef MSDOSFS_DEBUG
56998dc8da5SEd Maste 	printf("fatchain(pmp %p, start %lu, count %lu, fillwith %lx)\n",
57098dc8da5SEd Maste 	    pmp, start, count, fillwith);
57198dc8da5SEd Maste #endif
57298dc8da5SEd Maste 	/*
57398dc8da5SEd Maste 	 * Be sure the clusters are in the filesystem.
57498dc8da5SEd Maste 	 */
57598dc8da5SEd Maste 	if (start < CLUST_FIRST || start + count - 1 > pmp->pm_maxcluster)
57698dc8da5SEd Maste 		return (EINVAL);
57798dc8da5SEd Maste 
57898dc8da5SEd Maste 	while (count > 0) {
57998dc8da5SEd Maste 		byteoffset = FATOFS(pmp, start);
58098dc8da5SEd Maste 		fatblock(pmp, byteoffset, &bn, &bsize, &bo);
581*d485c77fSKonstantin Belousov 		error = bread((void *)pmp->pm_devvp, bn, bsize, NOCRED, &bp);
58298dc8da5SEd Maste 		if (error) {
58398dc8da5SEd Maste 			brelse(bp);
58498dc8da5SEd Maste 			return (error);
58598dc8da5SEd Maste 		}
58698dc8da5SEd Maste 		while (count > 0) {
58798dc8da5SEd Maste 			start++;
58898dc8da5SEd Maste 			newc = --count > 0 ? start : fillwith;
58998dc8da5SEd Maste 			switch (pmp->pm_fatmask) {
59098dc8da5SEd Maste 			case FAT12_MASK:
591ef633620SEd Maste 				readcn = getushort(bp->b_data + bo);
59298dc8da5SEd Maste 				if (start & 1) {
59398dc8da5SEd Maste 					readcn &= 0xf000;
59498dc8da5SEd Maste 					readcn |= newc & 0xfff;
59598dc8da5SEd Maste 				} else {
59698dc8da5SEd Maste 					readcn &= 0x000f;
59798dc8da5SEd Maste 					readcn |= newc << 4;
59898dc8da5SEd Maste 				}
599ef633620SEd Maste 				putushort(bp->b_data + bo, readcn);
60098dc8da5SEd Maste 				bo++;
60198dc8da5SEd Maste 				if (!(start & 1))
60298dc8da5SEd Maste 					bo++;
60398dc8da5SEd Maste 				break;
60498dc8da5SEd Maste 			case FAT16_MASK:
605ef633620SEd Maste 				putushort(bp->b_data + bo, newc);
60698dc8da5SEd Maste 				bo += 2;
60798dc8da5SEd Maste 				break;
60898dc8da5SEd Maste 			case FAT32_MASK:
609ef633620SEd Maste 				readcn = getulong(bp->b_data + bo);
61098dc8da5SEd Maste 				readcn &= ~pmp->pm_fatmask;
61198dc8da5SEd Maste 				readcn |= newc & pmp->pm_fatmask;
612ef633620SEd Maste 				putulong(bp->b_data + bo, readcn);
61398dc8da5SEd Maste 				bo += 4;
61498dc8da5SEd Maste 				break;
61598dc8da5SEd Maste 			}
61698dc8da5SEd Maste 			if (bo >= bsize)
61798dc8da5SEd Maste 				break;
61898dc8da5SEd Maste 		}
61998dc8da5SEd Maste 		updatefats(pmp, bp, bn);
62098dc8da5SEd Maste 	}
62198dc8da5SEd Maste 	pmp->pm_fmod = 1;
62298dc8da5SEd Maste 	return (0);
62398dc8da5SEd Maste }
62498dc8da5SEd Maste 
62598dc8da5SEd Maste /*
62698dc8da5SEd Maste  * Check the length of a free cluster chain starting at start.
62798dc8da5SEd Maste  *
62898dc8da5SEd Maste  * pmp	 - mount point
62998dc8da5SEd Maste  * start - start of chain
63098dc8da5SEd Maste  * count - maximum interesting length
63198dc8da5SEd Maste  */
63298dc8da5SEd Maste static int
63398dc8da5SEd Maste chainlength(struct msdosfsmount *pmp, u_long start, u_long count)
63498dc8da5SEd Maste {
63598dc8da5SEd Maste 	u_long idx, max_idx;
63698dc8da5SEd Maste 	u_int map;
63798dc8da5SEd Maste 	u_long len;
63898dc8da5SEd Maste 
63998dc8da5SEd Maste 	if (start > pmp->pm_maxcluster)
64098dc8da5SEd Maste 		return (0);
64198dc8da5SEd Maste 	max_idx = pmp->pm_maxcluster / N_INUSEBITS;
64298dc8da5SEd Maste 	idx = start / N_INUSEBITS;
64398dc8da5SEd Maste 	start %= N_INUSEBITS;
64498dc8da5SEd Maste 	map = pmp->pm_inusemap[idx];
64598dc8da5SEd Maste 	map &= ~((1 << start) - 1);
64698dc8da5SEd Maste 	if (map) {
64798dc8da5SEd Maste 		len = ffs(map) - 1 - start;
64898dc8da5SEd Maste 		len = MIN(len, count);
64998dc8da5SEd Maste 		if (start + len > pmp->pm_maxcluster)
65098dc8da5SEd Maste 			len = pmp->pm_maxcluster - start + 1;
65198dc8da5SEd Maste 		return (len);
65298dc8da5SEd Maste 	}
65398dc8da5SEd Maste 	len = N_INUSEBITS - start;
65498dc8da5SEd Maste 	if (len >= count) {
65598dc8da5SEd Maste 		len = count;
65698dc8da5SEd Maste 		if (start + len > pmp->pm_maxcluster)
65798dc8da5SEd Maste 			len = pmp->pm_maxcluster - start + 1;
65898dc8da5SEd Maste 		return (len);
65998dc8da5SEd Maste 	}
66098dc8da5SEd Maste 	while (++idx <= max_idx) {
66198dc8da5SEd Maste 		if (len >= count)
66298dc8da5SEd Maste 			break;
66398dc8da5SEd Maste 		map = pmp->pm_inusemap[idx];
66498dc8da5SEd Maste 		if (map) {
66598dc8da5SEd Maste 			len += ffs(map) - 1;
66698dc8da5SEd Maste 			break;
66798dc8da5SEd Maste 		}
66898dc8da5SEd Maste 		len += N_INUSEBITS;
66998dc8da5SEd Maste 	}
67098dc8da5SEd Maste 	len = MIN(len, count);
67198dc8da5SEd Maste 	if (start + len > pmp->pm_maxcluster)
67298dc8da5SEd Maste 		len = pmp->pm_maxcluster - start + 1;
67398dc8da5SEd Maste 	return (len);
67498dc8da5SEd Maste }
67598dc8da5SEd Maste 
67698dc8da5SEd Maste /*
67798dc8da5SEd Maste  * Allocate contigous free clusters.
67898dc8da5SEd Maste  *
67998dc8da5SEd Maste  * pmp	      - mount point.
68098dc8da5SEd Maste  * start      - start of cluster chain.
68198dc8da5SEd Maste  * count      - number of clusters to allocate.
68298dc8da5SEd Maste  * fillwith   - put this value into the FAT entry for the
68398dc8da5SEd Maste  *		last allocated cluster.
68498dc8da5SEd Maste  * retcluster - put the first allocated cluster's number here.
68598dc8da5SEd Maste  * got	      - how many clusters were actually allocated.
68698dc8da5SEd Maste  */
68798dc8da5SEd Maste static int
68898dc8da5SEd Maste chainalloc(struct msdosfsmount *pmp, u_long start, u_long count,
68998dc8da5SEd Maste     u_long fillwith, u_long *retcluster, u_long *got)
69098dc8da5SEd Maste {
69198dc8da5SEd Maste 	int error;
69298dc8da5SEd Maste 	u_long cl, n;
69398dc8da5SEd Maste 
69498dc8da5SEd Maste 	assert((pmp->pm_flags & MSDOSFSMNT_RONLY) == 0);
69598dc8da5SEd Maste 
69698dc8da5SEd Maste 	for (cl = start, n = count; n-- > 0;)
69798dc8da5SEd Maste 		usemap_alloc(pmp, cl++);
69898dc8da5SEd Maste 	pmp->pm_nxtfree = start + count;
69998dc8da5SEd Maste 	if (pmp->pm_nxtfree > pmp->pm_maxcluster)
70098dc8da5SEd Maste 		pmp->pm_nxtfree = CLUST_FIRST;
70198dc8da5SEd Maste 	pmp->pm_flags |= MSDOSFS_FSIMOD;
70298dc8da5SEd Maste 	error = fatchain(pmp, start, count, fillwith);
70398dc8da5SEd Maste 	if (error != 0) {
70498dc8da5SEd Maste 		for (cl = start, n = count; n-- > 0;)
70598dc8da5SEd Maste 			usemap_free(pmp, cl++);
70698dc8da5SEd Maste 		return (error);
70798dc8da5SEd Maste 	}
70898dc8da5SEd Maste #ifdef MSDOSFS_DEBUG
70998dc8da5SEd Maste 	printf("clusteralloc(): allocated cluster chain at %lu (%lu clusters)\n",
71098dc8da5SEd Maste 	    start, count);
71198dc8da5SEd Maste #endif
71298dc8da5SEd Maste 	if (retcluster)
71398dc8da5SEd Maste 		*retcluster = start;
71498dc8da5SEd Maste 	if (got)
71598dc8da5SEd Maste 		*got = count;
71698dc8da5SEd Maste 	return (0);
71798dc8da5SEd Maste }
71898dc8da5SEd Maste 
71998dc8da5SEd Maste /*
72098dc8da5SEd Maste  * Allocate contiguous free clusters.
72198dc8da5SEd Maste  *
72298dc8da5SEd Maste  * pmp	      - mount point.
72398dc8da5SEd Maste  * start      - preferred start of cluster chain.
72498dc8da5SEd Maste  * count      - number of clusters requested.
72598dc8da5SEd Maste  * fillwith   - put this value into the FAT entry for the
72698dc8da5SEd Maste  *		last allocated cluster.
72798dc8da5SEd Maste  * retcluster - put the first allocated cluster's number here.
72898dc8da5SEd Maste  * got	      - how many clusters were actually allocated.
72998dc8da5SEd Maste  */
73098dc8da5SEd Maste int
73198dc8da5SEd Maste clusteralloc(struct msdosfsmount *pmp, u_long start, u_long count,
73298dc8da5SEd Maste     u_long fillwith, u_long *retcluster, u_long *got)
73398dc8da5SEd Maste {
73498dc8da5SEd Maste 	int error;
73598dc8da5SEd Maste 
73698dc8da5SEd Maste 	error = clusteralloc1(pmp, start, count, fillwith, retcluster, got);
73798dc8da5SEd Maste 	return (error);
73898dc8da5SEd Maste }
73998dc8da5SEd Maste 
74098dc8da5SEd Maste static int
74198dc8da5SEd Maste clusteralloc1(struct msdosfsmount *pmp, u_long start, u_long count,
74298dc8da5SEd Maste     u_long fillwith, u_long *retcluster, u_long *got)
74398dc8da5SEd Maste {
74498dc8da5SEd Maste 	u_long idx;
74598dc8da5SEd Maste 	u_long len, newst, foundl, cn, l;
74698dc8da5SEd Maste 	u_long foundcn = 0; /* XXX: foundcn could be used unititialized */
74798dc8da5SEd Maste 	u_int map;
74898dc8da5SEd Maste 
74998dc8da5SEd Maste 	MSDOSFS_DPRINTF(("clusteralloc(): find %lu clusters\n", count));
75098dc8da5SEd Maste 
75198dc8da5SEd Maste 	if (start) {
75298dc8da5SEd Maste 		if ((len = chainlength(pmp, start, count)) >= count)
75398dc8da5SEd Maste 			return (chainalloc(pmp, start, count, fillwith, retcluster, got));
75498dc8da5SEd Maste 	} else
75598dc8da5SEd Maste 		len = 0;
75698dc8da5SEd Maste 
75798dc8da5SEd Maste 	newst = pmp->pm_nxtfree;
75898dc8da5SEd Maste 	foundl = 0;
75998dc8da5SEd Maste 
76098dc8da5SEd Maste 	for (cn = newst; cn <= pmp->pm_maxcluster;) {
76198dc8da5SEd Maste 		idx = cn / N_INUSEBITS;
76298dc8da5SEd Maste 		map = pmp->pm_inusemap[idx];
76398dc8da5SEd Maste 		map |= (1U << (cn % N_INUSEBITS)) - 1;
76498dc8da5SEd Maste 		if (map != FULL_RUN) {
76598dc8da5SEd Maste 			cn = idx * N_INUSEBITS + ffs(map ^ FULL_RUN) - 1;
76698dc8da5SEd Maste 			if ((l = chainlength(pmp, cn, count)) >= count)
76798dc8da5SEd Maste 				return (chainalloc(pmp, cn, count, fillwith, retcluster, got));
76898dc8da5SEd Maste 			if (l > foundl) {
76998dc8da5SEd Maste 				foundcn = cn;
77098dc8da5SEd Maste 				foundl = l;
77198dc8da5SEd Maste 			}
77298dc8da5SEd Maste 			cn += l + 1;
77398dc8da5SEd Maste 			continue;
77498dc8da5SEd Maste 		}
77598dc8da5SEd Maste 		cn += N_INUSEBITS - cn % N_INUSEBITS;
77698dc8da5SEd Maste 	}
77798dc8da5SEd Maste 	for (cn = 0; cn < newst;) {
77898dc8da5SEd Maste 		idx = cn / N_INUSEBITS;
77998dc8da5SEd Maste 		map = pmp->pm_inusemap[idx];
78098dc8da5SEd Maste 		map |= (1U << (cn % N_INUSEBITS)) - 1;
78198dc8da5SEd Maste 		if (map != FULL_RUN) {
78298dc8da5SEd Maste 			cn = idx * N_INUSEBITS + ffs(map ^ FULL_RUN) - 1;
78398dc8da5SEd Maste 			if ((l = chainlength(pmp, cn, count)) >= count)
78498dc8da5SEd Maste 				return (chainalloc(pmp, cn, count, fillwith, retcluster, got));
78598dc8da5SEd Maste 			if (l > foundl) {
78698dc8da5SEd Maste 				foundcn = cn;
78798dc8da5SEd Maste 				foundl = l;
78898dc8da5SEd Maste 			}
78998dc8da5SEd Maste 			cn += l + 1;
79098dc8da5SEd Maste 			continue;
79198dc8da5SEd Maste 		}
79298dc8da5SEd Maste 		cn += N_INUSEBITS - cn % N_INUSEBITS;
79398dc8da5SEd Maste 	}
79498dc8da5SEd Maste 
79598dc8da5SEd Maste 	if (!foundl)
79698dc8da5SEd Maste 		return (ENOSPC);
79798dc8da5SEd Maste 
79898dc8da5SEd Maste 	if (len)
79998dc8da5SEd Maste 		return (chainalloc(pmp, start, len, fillwith, retcluster, got));
80098dc8da5SEd Maste 	else
80198dc8da5SEd Maste 		return (chainalloc(pmp, foundcn, foundl, fillwith, retcluster, got));
80298dc8da5SEd Maste }
80398dc8da5SEd Maste 
80498dc8da5SEd Maste 
80598dc8da5SEd Maste /*
80698dc8da5SEd Maste  * Free a chain of clusters.
80798dc8da5SEd Maste  *
80898dc8da5SEd Maste  * pmp		- address of the msdosfs mount structure for the filesystem
80998dc8da5SEd Maste  *		  containing the cluster chain to be freed.
81098dc8da5SEd Maste  * startcluster - number of the 1st cluster in the chain of clusters to be
81198dc8da5SEd Maste  *		  freed.
81298dc8da5SEd Maste  */
81398dc8da5SEd Maste int
81498dc8da5SEd Maste freeclusterchain(struct msdosfsmount *pmp, u_long cluster)
81598dc8da5SEd Maste {
81698dc8da5SEd Maste 	int error;
817*d485c77fSKonstantin Belousov 	struct m_buf *bp = NULL;
81898dc8da5SEd Maste 	u_long bn, bo, bsize, byteoffset;
81998dc8da5SEd Maste 	u_long readcn, lbn = -1;
82098dc8da5SEd Maste 
82198dc8da5SEd Maste 	while (cluster >= CLUST_FIRST && cluster <= pmp->pm_maxcluster) {
82298dc8da5SEd Maste 		byteoffset = FATOFS(pmp, cluster);
82398dc8da5SEd Maste 		fatblock(pmp, byteoffset, &bn, &bsize, &bo);
82498dc8da5SEd Maste 		if (lbn != bn) {
82598dc8da5SEd Maste 			if (bp)
82698dc8da5SEd Maste 				updatefats(pmp, bp, lbn);
827*d485c77fSKonstantin Belousov 			error = bread((void *)pmp->pm_devvp, bn, bsize,
828*d485c77fSKonstantin Belousov 			    NOCRED, &bp);
82998dc8da5SEd Maste 			if (error) {
83098dc8da5SEd Maste 				brelse(bp);
83198dc8da5SEd Maste 				return (error);
83298dc8da5SEd Maste 			}
83398dc8da5SEd Maste 			lbn = bn;
83498dc8da5SEd Maste 		}
83598dc8da5SEd Maste 		usemap_free(pmp, cluster);
83698dc8da5SEd Maste 		switch (pmp->pm_fatmask) {
83798dc8da5SEd Maste 		case FAT12_MASK:
838ef633620SEd Maste 			readcn = getushort(bp->b_data + bo);
83998dc8da5SEd Maste 			if (cluster & 1) {
84098dc8da5SEd Maste 				cluster = readcn >> 4;
84198dc8da5SEd Maste 				readcn &= 0x000f;
84298dc8da5SEd Maste 				readcn |= MSDOSFSFREE << 4;
84398dc8da5SEd Maste 			} else {
84498dc8da5SEd Maste 				cluster = readcn;
84598dc8da5SEd Maste 				readcn &= 0xf000;
84698dc8da5SEd Maste 				readcn |= MSDOSFSFREE & 0xfff;
84798dc8da5SEd Maste 			}
848ef633620SEd Maste 			putushort(bp->b_data + bo, readcn);
84998dc8da5SEd Maste 			break;
85098dc8da5SEd Maste 		case FAT16_MASK:
851ef633620SEd Maste 			cluster = getushort(bp->b_data + bo);
852ef633620SEd Maste 			putushort(bp->b_data + bo, MSDOSFSFREE);
85398dc8da5SEd Maste 			break;
85498dc8da5SEd Maste 		case FAT32_MASK:
855ef633620SEd Maste 			cluster = getulong(bp->b_data + bo);
856ef633620SEd Maste 			putulong(bp->b_data + bo,
85798dc8da5SEd Maste 				 (MSDOSFSFREE & FAT32_MASK) | (cluster & ~FAT32_MASK));
85898dc8da5SEd Maste 			break;
85998dc8da5SEd Maste 		}
86098dc8da5SEd Maste 		cluster &= pmp->pm_fatmask;
86198dc8da5SEd Maste 		if ((cluster | ~pmp->pm_fatmask) >= CLUST_RSRVD)
86298dc8da5SEd Maste 			cluster |= pmp->pm_fatmask;
86398dc8da5SEd Maste 	}
86498dc8da5SEd Maste 	if (bp)
86598dc8da5SEd Maste 		updatefats(pmp, bp, bn);
86698dc8da5SEd Maste 	return (0);
86798dc8da5SEd Maste }
86898dc8da5SEd Maste 
86998dc8da5SEd Maste /*
87098dc8da5SEd Maste  * Read in FAT blocks looking for free clusters. For every free cluster
87198dc8da5SEd Maste  * found turn off its corresponding bit in the pm_inusemap.
87298dc8da5SEd Maste  */
87398dc8da5SEd Maste int
87498dc8da5SEd Maste fillinusemap(struct msdosfsmount *pmp)
87598dc8da5SEd Maste {
876*d485c77fSKonstantin Belousov 	struct m_buf *bp;
87798dc8da5SEd Maste 	u_long bn, bo, bsize, byteoffset, cn, readcn;
87898dc8da5SEd Maste 	int error;
87998dc8da5SEd Maste 
8802037e988SEd Maste 	bp = NULL;
8812037e988SEd Maste 
88298dc8da5SEd Maste 	/*
88398dc8da5SEd Maste 	 * Mark all clusters in use, we mark the free ones in the FAT scan
88498dc8da5SEd Maste 	 * loop further down.
88598dc8da5SEd Maste 	 */
88698dc8da5SEd Maste 	for (cn = 0; cn < (pmp->pm_maxcluster + N_INUSEBITS) / N_INUSEBITS; cn++)
88798dc8da5SEd Maste 		pmp->pm_inusemap[cn] = FULL_RUN;
88898dc8da5SEd Maste 
88998dc8da5SEd Maste 	/*
89098dc8da5SEd Maste 	 * Figure how many free clusters are in the filesystem by ripping
89198dc8da5SEd Maste 	 * through the FAT counting the number of entries whose content is
89298dc8da5SEd Maste 	 * zero.  These represent free clusters.
89398dc8da5SEd Maste 	 */
89498dc8da5SEd Maste 	pmp->pm_freeclustercount = 0;
89559e9b369SEd Maste 	for (cn = 0; cn <= pmp->pm_maxcluster; cn++) {
89698dc8da5SEd Maste 		byteoffset = FATOFS(pmp, cn);
89798dc8da5SEd Maste 		bo = byteoffset % pmp->pm_fatblocksize;
89859e9b369SEd Maste 		if (bo == 0) {
89998dc8da5SEd Maste 			/* Read new FAT block */
90098dc8da5SEd Maste 			if (bp != NULL)
90198dc8da5SEd Maste 				brelse(bp);
90298dc8da5SEd Maste 			fatblock(pmp, byteoffset, &bn, &bsize, NULL);
903*d485c77fSKonstantin Belousov 			error = bread((void *)pmp->pm_devvp, bn, bsize,
904*d485c77fSKonstantin Belousov 			    NOCRED, &bp);
90559e9b369SEd Maste 			if (error != 0)
90698dc8da5SEd Maste 				return (error);
90798dc8da5SEd Maste 		}
90898dc8da5SEd Maste 		if (FAT32(pmp))
909ef633620SEd Maste 			readcn = getulong(bp->b_data + bo);
91098dc8da5SEd Maste 		else
911ef633620SEd Maste 			readcn = getushort(bp->b_data + bo);
91298dc8da5SEd Maste 		if (FAT12(pmp) && (cn & 1))
91398dc8da5SEd Maste 			readcn >>= 4;
91498dc8da5SEd Maste 		readcn &= pmp->pm_fatmask;
91598dc8da5SEd Maste 
91659e9b369SEd Maste 		/*
91759e9b369SEd Maste 		 * Check if the FAT ID matches the BPB's media descriptor and
91859e9b369SEd Maste 		 * all other bits are set to 1.
91959e9b369SEd Maste 		 */
92059e9b369SEd Maste 		if (cn == 0 && readcn != ((pmp->pm_fatmask & 0xffffff00) |
92159e9b369SEd Maste 		    pmp->pm_bpb.bpbMedia)) {
92259e9b369SEd Maste #ifdef MSDOSFS_DEBUG
92359e9b369SEd Maste 			printf("mountmsdosfs(): Media descriptor in BPB"
92459e9b369SEd Maste 			    "does not match FAT ID\n");
92559e9b369SEd Maste #endif
92659e9b369SEd Maste 			brelse(bp);
92759e9b369SEd Maste 			return (EINVAL);
92859e9b369SEd Maste 		} else if (readcn == CLUST_FREE)
92998dc8da5SEd Maste 			usemap_free(pmp, cn);
93098dc8da5SEd Maste 	}
93198dc8da5SEd Maste 	if (bp != NULL)
93298dc8da5SEd Maste 		brelse(bp);
93398dc8da5SEd Maste 
93498dc8da5SEd Maste 	for (cn = pmp->pm_maxcluster + 1; cn < (pmp->pm_maxcluster +
93598dc8da5SEd Maste 	    N_INUSEBITS) / N_INUSEBITS; cn++)
93698dc8da5SEd Maste 		pmp->pm_inusemap[cn / N_INUSEBITS] |= 1U << (cn % N_INUSEBITS);
93798dc8da5SEd Maste 
93898dc8da5SEd Maste 	return (0);
93998dc8da5SEd Maste }
94098dc8da5SEd Maste 
94198dc8da5SEd Maste /*
94298dc8da5SEd Maste  * Allocate a new cluster and chain it onto the end of the file.
94398dc8da5SEd Maste  *
94498dc8da5SEd Maste  * dep	 - the file to extend
94598dc8da5SEd Maste  * count - number of clusters to allocate
94698dc8da5SEd Maste  * bpp	 - where to return the address of the buf header for the first new
94798dc8da5SEd Maste  *	   file block
94898dc8da5SEd Maste  * ncp	 - where to put cluster number of the first newly allocated cluster
94998dc8da5SEd Maste  *	   If this pointer is 0, do not return the cluster number.
95098dc8da5SEd Maste  * flags - see fat.h
95198dc8da5SEd Maste  *
95298dc8da5SEd Maste  * NOTE: This function is not responsible for turning on the DE_UPDATE bit of
95398dc8da5SEd Maste  * the de_flag field of the denode and it does not change the de_FileSize
95498dc8da5SEd Maste  * field.  This is left for the caller to do.
95598dc8da5SEd Maste  */
95698dc8da5SEd Maste int
957*d485c77fSKonstantin Belousov m_extendfile(struct denode *dep, u_long count, struct m_buf **bpp, u_long *ncp,
95898dc8da5SEd Maste     int flags)
95998dc8da5SEd Maste {
96098dc8da5SEd Maste 	int error;
96198dc8da5SEd Maste 	u_long frcn;
96298dc8da5SEd Maste 	u_long cn, got;
96398dc8da5SEd Maste 	struct msdosfsmount *pmp = dep->de_pmp;
964*d485c77fSKonstantin Belousov 	struct m_buf *bp;
96598dc8da5SEd Maste 
96698dc8da5SEd Maste 	/*
96798dc8da5SEd Maste 	 * Don't try to extend the root directory
96898dc8da5SEd Maste 	 */
96998dc8da5SEd Maste 	if (dep->de_StartCluster == MSDOSFSROOT
97098dc8da5SEd Maste 	    && (dep->de_Attributes & ATTR_DIRECTORY)) {
97198dc8da5SEd Maste #ifdef MSDOSFS_DEBUG
97298dc8da5SEd Maste 		printf("extendfile(): attempt to extend root directory\n");
97398dc8da5SEd Maste #endif
97498dc8da5SEd Maste 		return (ENOSPC);
97598dc8da5SEd Maste 	}
97698dc8da5SEd Maste 
97798dc8da5SEd Maste 	/*
97898dc8da5SEd Maste 	 * If the "file's last cluster" cache entry is empty, and the file
97998dc8da5SEd Maste 	 * is not empty, then fill the cache entry by calling pcbmap().
98098dc8da5SEd Maste 	 */
98198dc8da5SEd Maste 	if (dep->de_fc[FC_LASTFC].fc_frcn == FCE_EMPTY &&
98298dc8da5SEd Maste 	    dep->de_StartCluster != 0) {
98398dc8da5SEd Maste 		error = pcbmap(dep, 0xffff, 0, &cn, 0);
98498dc8da5SEd Maste 		/* we expect it to return E2BIG */
98598dc8da5SEd Maste 		if (error != E2BIG)
98698dc8da5SEd Maste 			return (error);
98798dc8da5SEd Maste 	}
98898dc8da5SEd Maste 
98998dc8da5SEd Maste 	dep->de_fc[FC_NEXTTOLASTFC].fc_frcn =
99098dc8da5SEd Maste 	    dep->de_fc[FC_LASTFC].fc_frcn;
99198dc8da5SEd Maste 	dep->de_fc[FC_NEXTTOLASTFC].fc_fsrcn =
99298dc8da5SEd Maste 	    dep->de_fc[FC_LASTFC].fc_fsrcn;
99398dc8da5SEd Maste 	while (count > 0) {
99498dc8da5SEd Maste 		/*
99598dc8da5SEd Maste 		 * Allocate a new cluster chain and cat onto the end of the
99698dc8da5SEd Maste 		 * file.  If the file is empty we make de_StartCluster point
99798dc8da5SEd Maste 		 * to the new block.  Note that de_StartCluster being 0 is
99898dc8da5SEd Maste 		 * sufficient to be sure the file is empty since we exclude
99998dc8da5SEd Maste 		 * attempts to extend the root directory above, and the root
100098dc8da5SEd Maste 		 * dir is the only file with a startcluster of 0 that has
100198dc8da5SEd Maste 		 * blocks allocated (sort of).
100298dc8da5SEd Maste 		 */
100398dc8da5SEd Maste 		if (dep->de_StartCluster == 0)
100498dc8da5SEd Maste 			cn = 0;
100598dc8da5SEd Maste 		else
100698dc8da5SEd Maste 			cn = dep->de_fc[FC_LASTFC].fc_fsrcn + 1;
100798dc8da5SEd Maste 		error = clusteralloc(pmp, cn, count, CLUST_EOFE, &cn, &got);
100898dc8da5SEd Maste 		if (error)
100998dc8da5SEd Maste 			return (error);
101098dc8da5SEd Maste 
101198dc8da5SEd Maste 		count -= got;
101298dc8da5SEd Maste 
101398dc8da5SEd Maste 		/*
101498dc8da5SEd Maste 		 * Give them the filesystem relative cluster number if they want
101598dc8da5SEd Maste 		 * it.
101698dc8da5SEd Maste 		 */
101798dc8da5SEd Maste 		if (ncp) {
101898dc8da5SEd Maste 			*ncp = cn;
101998dc8da5SEd Maste 			ncp = NULL;
102098dc8da5SEd Maste 		}
102198dc8da5SEd Maste 
102298dc8da5SEd Maste 		if (dep->de_StartCluster == 0) {
102398dc8da5SEd Maste 			dep->de_StartCluster = cn;
102498dc8da5SEd Maste 			frcn = 0;
102598dc8da5SEd Maste 		} else {
102698dc8da5SEd Maste 			error = fatentry(FAT_SET, pmp,
102798dc8da5SEd Maste 					 dep->de_fc[FC_LASTFC].fc_fsrcn,
102898dc8da5SEd Maste 					 0, cn);
102998dc8da5SEd Maste 			if (error) {
103098dc8da5SEd Maste 				clusterfree(pmp, cn, NULL);
103198dc8da5SEd Maste 				return (error);
103298dc8da5SEd Maste 			}
103398dc8da5SEd Maste 			frcn = dep->de_fc[FC_LASTFC].fc_frcn + 1;
103498dc8da5SEd Maste 		}
103598dc8da5SEd Maste 
103698dc8da5SEd Maste 		/*
103798dc8da5SEd Maste 		 * Update the "last cluster of the file" entry in the
103898dc8da5SEd Maste 		 * denode's FAT cache.
103998dc8da5SEd Maste 		 */
104098dc8da5SEd Maste 		fc_setcache(dep, FC_LASTFC, frcn + got - 1, cn + got - 1);
104198dc8da5SEd Maste 
104298dc8da5SEd Maste 		if ((flags & DE_CLEAR) &&
104398dc8da5SEd Maste 		    (dep->de_Attributes & ATTR_DIRECTORY)) {
104498dc8da5SEd Maste 			while (got-- > 0) {
1045*d485c77fSKonstantin Belousov 				bp = getblk((void *)pmp->pm_devvp,
104698dc8da5SEd Maste 				    cntobn(pmp, cn++),
104798dc8da5SEd Maste 				    pmp->pm_bpcluster, 0, 0, 0);
104898dc8da5SEd Maste 				clrbuf(bp);
104998dc8da5SEd Maste 				if (bpp) {
105098dc8da5SEd Maste 					*bpp = bp;
105198dc8da5SEd Maste 					bpp = NULL;
105298dc8da5SEd Maste 				} else {
105398dc8da5SEd Maste 					bdwrite(bp);
105498dc8da5SEd Maste 				}
105598dc8da5SEd Maste 			}
105698dc8da5SEd Maste 		}
105798dc8da5SEd Maste 	}
105898dc8da5SEd Maste 
105998dc8da5SEd Maste 	return (0);
106098dc8da5SEd Maste }
1061