xref: /freebsd/usr.sbin/makefs/msdos/msdosfs_fat.c (revision 476b0ab758b96103c246aefd2fee215e9544caa1)
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>
5798dc8da5SEd Maste #include <stdio.h>
5898dc8da5SEd Maste #include <string.h>
5998dc8da5SEd Maste #include <strings.h>
6098dc8da5SEd Maste 
6198dc8da5SEd Maste #include "ffs/buf.h"
6298dc8da5SEd Maste 
63*476b0ab7SEd Maste #include <fs/msdosfs/bpb.h>
6498dc8da5SEd Maste #include "msdos/direntry.h"
65*476b0ab7SEd Maste #include <fs/msdosfs/denode.h>
66*476b0ab7SEd Maste #include <fs/msdosfs/fat.h>
67*476b0ab7SEd Maste #include "msdos/msdosfsmount.h"
6898dc8da5SEd Maste 
6998dc8da5SEd Maste #include "makefs.h"
7098dc8da5SEd Maste #include "msdos.h"
7198dc8da5SEd Maste 
7298dc8da5SEd Maste #define	FULL_RUN	((u_int)0xffffffff)
7398dc8da5SEd Maste #define	SYNCHRONOUS_WRITES(pmp)	1
7498dc8da5SEd Maste 
7598dc8da5SEd Maste static int	chainalloc(struct msdosfsmount *pmp, u_long start,
7698dc8da5SEd Maste 		    u_long count, u_long fillwith, u_long *retcluster,
7798dc8da5SEd Maste 		    u_long *got);
7898dc8da5SEd Maste static int	chainlength(struct msdosfsmount *pmp, u_long start,
7998dc8da5SEd Maste 		    u_long count);
8098dc8da5SEd Maste static void	fatblock(struct msdosfsmount *pmp, u_long ofs, u_long *bnp,
8198dc8da5SEd Maste 		    u_long *sizep, u_long *bop);
8298dc8da5SEd Maste static int	fatchain(struct msdosfsmount *pmp, u_long start, u_long count,
8398dc8da5SEd Maste 		    u_long fillwith);
8498dc8da5SEd Maste static void	fc_lookup(struct denode *dep, u_long findcn, u_long *frcnp,
8598dc8da5SEd Maste 		    u_long *fsrcnp);
8698dc8da5SEd Maste static void	updatefats(struct msdosfsmount *pmp, struct buf *bp,
8798dc8da5SEd Maste 		    u_long fatbn);
8898dc8da5SEd Maste static __inline void
8998dc8da5SEd Maste 		usemap_alloc(struct msdosfsmount *pmp, u_long cn);
9098dc8da5SEd Maste static __inline void
9198dc8da5SEd Maste 		usemap_free(struct msdosfsmount *pmp, u_long cn);
9298dc8da5SEd Maste static int	clusteralloc1(struct msdosfsmount *pmp, u_long start,
9398dc8da5SEd Maste 		    u_long count, u_long fillwith, u_long *retcluster,
9498dc8da5SEd Maste 		    u_long *got);
9598dc8da5SEd Maste 
9698dc8da5SEd Maste static void
9798dc8da5SEd Maste fatblock(struct msdosfsmount *pmp, u_long ofs, u_long *bnp, u_long *sizep,
9898dc8da5SEd Maste     u_long *bop)
9998dc8da5SEd Maste {
10098dc8da5SEd Maste 	u_long bn, size;
10198dc8da5SEd Maste 
10298dc8da5SEd Maste 	bn = ofs / pmp->pm_fatblocksize * pmp->pm_fatblocksec;
10398dc8da5SEd Maste 	size = MIN(pmp->pm_fatblocksec, pmp->pm_FATsecs - bn)
10498dc8da5SEd Maste 	    * DEV_BSIZE;
10598dc8da5SEd Maste 	bn += pmp->pm_fatblk + pmp->pm_curfat * pmp->pm_FATsecs;
10698dc8da5SEd Maste 
10798dc8da5SEd Maste 	if (bnp)
10898dc8da5SEd Maste 		*bnp = bn;
10998dc8da5SEd Maste 	if (sizep)
11098dc8da5SEd Maste 		*sizep = size;
11198dc8da5SEd Maste 	if (bop)
11298dc8da5SEd Maste 		*bop = ofs % pmp->pm_fatblocksize;
11398dc8da5SEd Maste }
11498dc8da5SEd Maste 
11598dc8da5SEd Maste /*
11698dc8da5SEd Maste  * Map the logical cluster number of a file into a physical disk sector
11798dc8da5SEd Maste  * that is filesystem relative.
11898dc8da5SEd Maste  *
11998dc8da5SEd Maste  * dep	  - address of denode representing the file of interest
12098dc8da5SEd Maste  * findcn - file relative cluster whose filesystem relative cluster number
12198dc8da5SEd Maste  *	    and/or block number are/is to be found
12298dc8da5SEd Maste  * bnp	  - address of where to place the filesystem relative block number.
12398dc8da5SEd Maste  *	    If this pointer is null then don't return this quantity.
12498dc8da5SEd Maste  * cnp	  - address of where to place the filesystem relative cluster number.
12598dc8da5SEd Maste  *	    If this pointer is null then don't return this quantity.
12698dc8da5SEd Maste  * sp     - pointer to returned block size
12798dc8da5SEd Maste  *
12898dc8da5SEd Maste  * NOTE: Either bnp or cnp must be non-null.
12998dc8da5SEd Maste  * This function has one side effect.  If the requested file relative cluster
13098dc8da5SEd Maste  * is beyond the end of file, then the actual number of clusters in the file
13198dc8da5SEd Maste  * is returned in *cnp.  This is useful for determining how long a directory is.
13298dc8da5SEd Maste  *  If cnp is null, nothing is returned.
13398dc8da5SEd Maste  */
13498dc8da5SEd Maste int
13598dc8da5SEd Maste pcbmap(struct denode *dep, u_long findcn, daddr_t *bnp, u_long *cnp, int *sp)
13698dc8da5SEd Maste {
13798dc8da5SEd Maste 	int error;
13898dc8da5SEd Maste 	u_long i;
13998dc8da5SEd Maste 	u_long cn;
14098dc8da5SEd Maste 	u_long prevcn = 0; /* XXX: prevcn could be used unititialized */
14198dc8da5SEd Maste 	u_long byteoffset;
14298dc8da5SEd Maste 	u_long bn;
14398dc8da5SEd Maste 	u_long bo;
14498dc8da5SEd Maste 	struct buf *bp = NULL;
14598dc8da5SEd Maste 	u_long bp_bn = -1;
14698dc8da5SEd Maste 	struct msdosfsmount *pmp = dep->de_pmp;
14798dc8da5SEd Maste 	u_long bsize;
14898dc8da5SEd Maste 
14998dc8da5SEd Maste 	assert(bnp != NULL || cnp != NULL || sp != NULL);
15098dc8da5SEd Maste 
15198dc8da5SEd Maste 	cn = dep->de_StartCluster;
15298dc8da5SEd Maste 	/*
15398dc8da5SEd Maste 	 * The "file" that makes up the root directory is contiguous,
15498dc8da5SEd Maste 	 * permanently allocated, of fixed size, and is not made up of
15598dc8da5SEd Maste 	 * clusters.  If the cluster number is beyond the end of the root
15698dc8da5SEd Maste 	 * directory, then return the number of clusters in the file.
15798dc8da5SEd Maste 	 */
15898dc8da5SEd Maste 	if (cn == MSDOSFSROOT) {
15998dc8da5SEd Maste 		if (dep->de_Attributes & ATTR_DIRECTORY) {
16098dc8da5SEd Maste 			if (de_cn2off(pmp, findcn) >= dep->de_FileSize) {
16198dc8da5SEd Maste 				if (cnp)
16298dc8da5SEd Maste 					*cnp = de_bn2cn(pmp, pmp->pm_rootdirsize);
16398dc8da5SEd Maste 				return (E2BIG);
16498dc8da5SEd Maste 			}
16598dc8da5SEd Maste 			if (bnp)
16698dc8da5SEd Maste 				*bnp = pmp->pm_rootdirblk + de_cn2bn(pmp, findcn);
16798dc8da5SEd Maste 			if (cnp)
16898dc8da5SEd Maste 				*cnp = MSDOSFSROOT;
16998dc8da5SEd Maste 			if (sp)
17098dc8da5SEd Maste 				*sp = MIN(pmp->pm_bpcluster,
17198dc8da5SEd Maste 				    dep->de_FileSize - de_cn2off(pmp, findcn));
17298dc8da5SEd Maste 			return (0);
17398dc8da5SEd Maste 		} else {		/* just an empty file */
17498dc8da5SEd Maste 			if (cnp)
17598dc8da5SEd Maste 				*cnp = 0;
17698dc8da5SEd Maste 			return (E2BIG);
17798dc8da5SEd Maste 		}
17898dc8da5SEd Maste 	}
17998dc8da5SEd Maste 
18098dc8da5SEd Maste 	/*
18198dc8da5SEd Maste 	 * All other files do I/O in cluster sized blocks
18298dc8da5SEd Maste 	 */
18398dc8da5SEd Maste 	if (sp)
18498dc8da5SEd Maste 		*sp = pmp->pm_bpcluster;
18598dc8da5SEd Maste 
18698dc8da5SEd Maste 	/*
18798dc8da5SEd Maste 	 * Rummage around in the FAT cache, maybe we can avoid tromping
18898dc8da5SEd Maste 	 * through every FAT entry for the file. And, keep track of how far
18998dc8da5SEd Maste 	 * off the cache was from where we wanted to be.
19098dc8da5SEd Maste 	 */
19198dc8da5SEd Maste 	i = 0;
19298dc8da5SEd Maste 	fc_lookup(dep, findcn, &i, &cn);
19398dc8da5SEd Maste 
19498dc8da5SEd Maste 	/*
19598dc8da5SEd Maste 	 * Handle all other files or directories the normal way.
19698dc8da5SEd Maste 	 */
19798dc8da5SEd Maste 	for (; i < findcn; i++) {
19898dc8da5SEd Maste 		/*
19998dc8da5SEd Maste 		 * Stop with all reserved clusters, not just with EOF.
20098dc8da5SEd Maste 		 */
20198dc8da5SEd Maste 		if ((cn | ~pmp->pm_fatmask) >= CLUST_RSRVD)
20298dc8da5SEd Maste 			goto hiteof;
20398dc8da5SEd Maste 		byteoffset = FATOFS(pmp, cn);
20498dc8da5SEd Maste 		fatblock(pmp, byteoffset, &bn, &bsize, &bo);
20598dc8da5SEd Maste 		if (bn != bp_bn) {
20698dc8da5SEd Maste 			if (bp)
20798dc8da5SEd Maste 				brelse(bp);
20898dc8da5SEd Maste 			error = bread(pmp->pm_devvp, bn, bsize, NOCRED, &bp);
20998dc8da5SEd Maste 			if (error) {
21098dc8da5SEd Maste 				brelse(bp);
21198dc8da5SEd Maste 				return (error);
21298dc8da5SEd Maste 			}
21398dc8da5SEd Maste 			bp_bn = bn;
21498dc8da5SEd Maste 		}
21598dc8da5SEd Maste 		prevcn = cn;
21698dc8da5SEd Maste 		if (bo >= bsize) {
21798dc8da5SEd Maste 			if (bp)
21898dc8da5SEd Maste 				brelse(bp);
21998dc8da5SEd Maste 			return (EIO);
22098dc8da5SEd Maste 		}
22198dc8da5SEd Maste 		if (FAT32(pmp))
222ef633620SEd Maste 			cn = getulong(bp->b_data + bo);
22398dc8da5SEd Maste 		else
224ef633620SEd Maste 			cn = getushort(bp->b_data + bo);
22598dc8da5SEd Maste 		if (FAT12(pmp) && (prevcn & 1))
22698dc8da5SEd Maste 			cn >>= 4;
22798dc8da5SEd Maste 		cn &= pmp->pm_fatmask;
22898dc8da5SEd Maste 
22998dc8da5SEd Maste 		/*
23098dc8da5SEd Maste 		 * Force the special cluster numbers
23198dc8da5SEd Maste 		 * to be the same for all cluster sizes
23298dc8da5SEd Maste 		 * to let the rest of msdosfs handle
23398dc8da5SEd Maste 		 * all cases the same.
23498dc8da5SEd Maste 		 */
23598dc8da5SEd Maste 		if ((cn | ~pmp->pm_fatmask) >= CLUST_RSRVD)
23698dc8da5SEd Maste 			cn |= ~pmp->pm_fatmask;
23798dc8da5SEd Maste 	}
23898dc8da5SEd Maste 
23998dc8da5SEd Maste 	if (!MSDOSFSEOF(pmp, cn)) {
24098dc8da5SEd Maste 		if (bp)
24198dc8da5SEd Maste 			brelse(bp);
24298dc8da5SEd Maste 		if (bnp)
24398dc8da5SEd Maste 			*bnp = cntobn(pmp, cn);
24498dc8da5SEd Maste 		if (cnp)
24598dc8da5SEd Maste 			*cnp = cn;
24698dc8da5SEd Maste 		fc_setcache(dep, FC_LASTMAP, i, cn);
24798dc8da5SEd Maste 		return (0);
24898dc8da5SEd Maste 	}
24998dc8da5SEd Maste 
25098dc8da5SEd Maste hiteof:;
25198dc8da5SEd Maste 	if (cnp)
25298dc8da5SEd Maste 		*cnp = i;
25398dc8da5SEd Maste 	if (bp)
25498dc8da5SEd Maste 		brelse(bp);
25598dc8da5SEd Maste 	/* update last file cluster entry in the FAT cache */
25698dc8da5SEd Maste 	fc_setcache(dep, FC_LASTFC, i - 1, prevcn);
25798dc8da5SEd Maste 	return (E2BIG);
25898dc8da5SEd Maste }
25998dc8da5SEd Maste 
26098dc8da5SEd Maste /*
26198dc8da5SEd Maste  * Find the closest entry in the FAT cache to the cluster we are looking
26298dc8da5SEd Maste  * for.
26398dc8da5SEd Maste  */
26498dc8da5SEd Maste static void
26598dc8da5SEd Maste fc_lookup(struct denode *dep, u_long findcn, u_long *frcnp, u_long *fsrcnp)
26698dc8da5SEd Maste {
26798dc8da5SEd Maste 	int i;
26898dc8da5SEd Maste 	u_long cn;
26998dc8da5SEd Maste 	struct fatcache *closest = NULL;
27098dc8da5SEd Maste 
27198dc8da5SEd Maste 	for (i = 0; i < FC_SIZE; i++) {
27298dc8da5SEd Maste 		cn = dep->de_fc[i].fc_frcn;
27398dc8da5SEd Maste 		if (cn != FCE_EMPTY && cn <= findcn) {
27498dc8da5SEd Maste 			if (closest == NULL || cn > closest->fc_frcn)
27598dc8da5SEd Maste 				closest = &dep->de_fc[i];
27698dc8da5SEd Maste 		}
27798dc8da5SEd Maste 	}
27898dc8da5SEd Maste 	if (closest) {
27998dc8da5SEd Maste 		*frcnp = closest->fc_frcn;
28098dc8da5SEd Maste 		*fsrcnp = closest->fc_fsrcn;
28198dc8da5SEd Maste 	}
28298dc8da5SEd Maste }
28398dc8da5SEd Maste 
28498dc8da5SEd Maste /*
28598dc8da5SEd Maste  * Purge the FAT cache in denode dep of all entries relating to file
28698dc8da5SEd Maste  * relative cluster frcn and beyond.
28798dc8da5SEd Maste  */
28898dc8da5SEd Maste void
28998dc8da5SEd Maste fc_purge(struct denode *dep, u_int frcn)
29098dc8da5SEd Maste {
29198dc8da5SEd Maste 	int i;
29298dc8da5SEd Maste 	struct fatcache *fcp;
29398dc8da5SEd Maste 
29498dc8da5SEd Maste 	fcp = dep->de_fc;
29598dc8da5SEd Maste 	for (i = 0; i < FC_SIZE; i++, fcp++) {
29698dc8da5SEd Maste 		if (fcp->fc_frcn >= frcn)
29798dc8da5SEd Maste 			fcp->fc_frcn = FCE_EMPTY;
29898dc8da5SEd Maste 	}
29998dc8da5SEd Maste }
30098dc8da5SEd Maste 
30198dc8da5SEd Maste /*
30298dc8da5SEd Maste  * Update the FAT.
30398dc8da5SEd Maste  * If mirroring the FAT, update all copies, with the first copy as last.
30498dc8da5SEd Maste  * Else update only the current FAT (ignoring the others).
30598dc8da5SEd Maste  *
30698dc8da5SEd Maste  * pmp	 - msdosfsmount structure for filesystem to update
30798dc8da5SEd Maste  * bp	 - addr of modified FAT block
30898dc8da5SEd Maste  * fatbn - block number relative to begin of filesystem of the modified FAT block.
30998dc8da5SEd Maste  */
31098dc8da5SEd Maste static void
31198dc8da5SEd Maste updatefats(struct msdosfsmount *pmp, struct buf *bp, u_long fatbn)
31298dc8da5SEd Maste {
31398dc8da5SEd Maste 	struct buf *bpn;
31498dc8da5SEd Maste 	int cleanfat, i;
31598dc8da5SEd Maste 
31698dc8da5SEd Maste #ifdef MSDOSFS_DEBUG
31798dc8da5SEd Maste 	printf("updatefats(pmp %p, bp %p, fatbn %lu)\n", pmp, bp, fatbn);
31898dc8da5SEd Maste #endif
31998dc8da5SEd Maste 
32098dc8da5SEd Maste 	if (pmp->pm_flags & MSDOSFS_FATMIRROR) {
32198dc8da5SEd Maste 		/*
32298dc8da5SEd Maste 		 * Now copy the block(s) of the modified FAT to the other copies of
32398dc8da5SEd Maste 		 * the FAT and write them out.  This is faster than reading in the
32498dc8da5SEd Maste 		 * other FATs and then writing them back out.  This could tie up
32598dc8da5SEd Maste 		 * the FAT for quite a while. Preventing others from accessing it.
32698dc8da5SEd Maste 		 * To prevent us from going after the FAT quite so much we use
32798dc8da5SEd Maste 		 * delayed writes, unless they specified "synchronous" when the
32898dc8da5SEd Maste 		 * filesystem was mounted.  If synch is asked for then use
32998dc8da5SEd Maste 		 * bwrite()'s and really slow things down.
33098dc8da5SEd Maste 		 */
33198dc8da5SEd Maste 		if (fatbn != pmp->pm_fatblk || FAT12(pmp))
33298dc8da5SEd Maste 			cleanfat = 0;
33398dc8da5SEd Maste 		else if (FAT16(pmp))
33498dc8da5SEd Maste 			cleanfat = 16;
33598dc8da5SEd Maste 		else
33698dc8da5SEd Maste 			cleanfat = 32;
33798dc8da5SEd Maste 		for (i = 1; i < pmp->pm_FATs; i++) {
33898dc8da5SEd Maste 			fatbn += pmp->pm_FATsecs;
33998dc8da5SEd Maste 			/* getblk() never fails */
34098dc8da5SEd Maste 			bpn = getblk(pmp->pm_devvp, fatbn, bp->b_bcount,
34198dc8da5SEd Maste 			    0, 0, 0);
34298dc8da5SEd Maste 			memcpy(bpn->b_data, bp->b_data, bp->b_bcount);
34398dc8da5SEd Maste 			/* Force the clean bit on in the other copies. */
34498dc8da5SEd Maste 			if (cleanfat == 16)
34598dc8da5SEd Maste 				((uint8_t *)bpn->b_data)[3] |= 0x80;
34698dc8da5SEd Maste 			else if (cleanfat == 32)
34798dc8da5SEd Maste 				((uint8_t *)bpn->b_data)[7] |= 0x08;
34898dc8da5SEd Maste 			if (SYNCHRONOUS_WRITES(pmp))
34998dc8da5SEd Maste 				bwrite(bpn);
35098dc8da5SEd Maste 			else
35198dc8da5SEd Maste 				bdwrite(bpn);
35298dc8da5SEd Maste 		}
35398dc8da5SEd Maste 	}
35498dc8da5SEd Maste 
35598dc8da5SEd Maste 	/*
35698dc8da5SEd Maste 	 * Write out the first (or current) FAT last.
35798dc8da5SEd Maste 	 */
35898dc8da5SEd Maste 	if (SYNCHRONOUS_WRITES(pmp))
35998dc8da5SEd Maste 		bwrite(bp);
36098dc8da5SEd Maste 	else
36198dc8da5SEd Maste 		bdwrite(bp);
36298dc8da5SEd Maste }
36398dc8da5SEd Maste 
36498dc8da5SEd Maste /*
36598dc8da5SEd Maste  * Updating entries in 12 bit FATs is a pain in the butt.
36698dc8da5SEd Maste  *
36798dc8da5SEd Maste  * The following picture shows where nibbles go when moving from a 12 bit
36898dc8da5SEd Maste  * cluster number into the appropriate bytes in the FAT.
36998dc8da5SEd Maste  *
37098dc8da5SEd Maste  *	byte m        byte m+1      byte m+2
37198dc8da5SEd Maste  *	+----+----+   +----+----+   +----+----+
37298dc8da5SEd Maste  *	|  0    1 |   |  2    3 |   |  4    5 |   FAT bytes
37398dc8da5SEd Maste  *	+----+----+   +----+----+   +----+----+
37498dc8da5SEd Maste  *
37598dc8da5SEd Maste  *	+----+----+----+   +----+----+----+
37698dc8da5SEd Maste  *	|  3    0    1 |   |  4    5    2 |
37798dc8da5SEd Maste  *	+----+----+----+   +----+----+----+
37898dc8da5SEd Maste  *	cluster n	   cluster n+1
37998dc8da5SEd Maste  *
38098dc8da5SEd Maste  * Where n is even. m = n + (n >> 2)
38198dc8da5SEd Maste  *
38298dc8da5SEd Maste  */
38398dc8da5SEd Maste static __inline void
38498dc8da5SEd Maste usemap_alloc(struct msdosfsmount *pmp, u_long cn)
38598dc8da5SEd Maste {
38698dc8da5SEd Maste 
38798dc8da5SEd Maste 	assert(cn <= pmp->pm_maxcluster);
38898dc8da5SEd Maste 	assert((pmp->pm_flags & MSDOSFSMNT_RONLY) == 0);
38998dc8da5SEd Maste 	assert((pmp->pm_inusemap[cn / N_INUSEBITS] & (1 << (cn % N_INUSEBITS)))
39098dc8da5SEd Maste 	    == 0);
39198dc8da5SEd Maste 	assert(pmp->pm_freeclustercount > 0);
39298dc8da5SEd Maste 
39398dc8da5SEd Maste 	pmp->pm_inusemap[cn / N_INUSEBITS] |= 1U << (cn % N_INUSEBITS);
39498dc8da5SEd Maste 	pmp->pm_freeclustercount--;
39598dc8da5SEd Maste 	pmp->pm_flags |= MSDOSFS_FSIMOD;
39698dc8da5SEd Maste }
39798dc8da5SEd Maste 
39898dc8da5SEd Maste static __inline void
39998dc8da5SEd Maste usemap_free(struct msdosfsmount *pmp, u_long cn)
40098dc8da5SEd Maste {
40198dc8da5SEd Maste 
40298dc8da5SEd Maste 	assert(cn <= pmp->pm_maxcluster);
40398dc8da5SEd Maste 	assert((pmp->pm_flags & MSDOSFSMNT_RONLY) == 0);
40498dc8da5SEd Maste 	assert((pmp->pm_inusemap[cn / N_INUSEBITS] & (1 << (cn % N_INUSEBITS)))
40598dc8da5SEd Maste 	    != 0);
40698dc8da5SEd Maste 
40798dc8da5SEd Maste 	pmp->pm_freeclustercount++;
40898dc8da5SEd Maste 	pmp->pm_flags |= MSDOSFS_FSIMOD;
40998dc8da5SEd Maste 	pmp->pm_inusemap[cn / N_INUSEBITS] &= ~(1U << (cn % N_INUSEBITS));
41098dc8da5SEd Maste }
41198dc8da5SEd Maste 
41298dc8da5SEd Maste int
41398dc8da5SEd Maste clusterfree(struct msdosfsmount *pmp, u_long cluster, u_long *oldcnp)
41498dc8da5SEd Maste {
41598dc8da5SEd Maste 	int error;
41698dc8da5SEd Maste 	u_long oldcn;
41798dc8da5SEd Maste 
41898dc8da5SEd Maste 	error = fatentry(FAT_GET_AND_SET, pmp, cluster, &oldcn, MSDOSFSFREE);
41998dc8da5SEd Maste 	if (error)
42098dc8da5SEd Maste 		return (error);
42198dc8da5SEd Maste 	/*
42298dc8da5SEd Maste 	 * If the cluster was successfully marked free, then update
42398dc8da5SEd Maste 	 * the count of free clusters, and turn off the "allocated"
42498dc8da5SEd Maste 	 * bit in the "in use" cluster bit map.
42598dc8da5SEd Maste 	 */
42698dc8da5SEd Maste 	usemap_free(pmp, cluster);
42798dc8da5SEd Maste 	if (oldcnp)
42898dc8da5SEd Maste 		*oldcnp = oldcn;
42998dc8da5SEd Maste 	return (0);
43098dc8da5SEd Maste }
43198dc8da5SEd Maste 
43298dc8da5SEd Maste /*
43398dc8da5SEd Maste  * Get or Set or 'Get and Set' the cluster'th entry in the FAT.
43498dc8da5SEd Maste  *
43598dc8da5SEd Maste  * function	- whether to get or set a FAT entry
43698dc8da5SEd Maste  * pmp		- address of the msdosfsmount structure for the filesystem
43798dc8da5SEd Maste  *		  whose FAT is to be manipulated.
43898dc8da5SEd Maste  * cn		- which cluster is of interest
43998dc8da5SEd Maste  * oldcontents	- address of a word that is to receive the contents of the
44098dc8da5SEd Maste  *		  cluster'th entry if this is a get function
44198dc8da5SEd Maste  * newcontents	- the new value to be written into the cluster'th element of
44298dc8da5SEd Maste  *		  the FAT if this is a set function.
44398dc8da5SEd Maste  *
44498dc8da5SEd Maste  * This function can also be used to free a cluster by setting the FAT entry
44598dc8da5SEd Maste  * for a cluster to 0.
44698dc8da5SEd Maste  *
44798dc8da5SEd Maste  * All copies of the FAT are updated if this is a set function. NOTE: If
44898dc8da5SEd Maste  * fatentry() marks a cluster as free it does not update the inusemap in
44998dc8da5SEd Maste  * the msdosfsmount structure. This is left to the caller.
45098dc8da5SEd Maste  */
45198dc8da5SEd Maste int
45298dc8da5SEd Maste fatentry(int function, struct msdosfsmount *pmp, u_long cn, u_long *oldcontents,
45398dc8da5SEd Maste     u_long newcontents)
45498dc8da5SEd Maste {
45598dc8da5SEd Maste 	int error;
45698dc8da5SEd Maste 	u_long readcn;
45798dc8da5SEd Maste 	u_long bn, bo, bsize, byteoffset;
45898dc8da5SEd Maste 	struct buf *bp;
45998dc8da5SEd Maste 
46098dc8da5SEd Maste #ifdef	MSDOSFS_DEBUG
46198dc8da5SEd Maste 	printf("fatentry(func %d, pmp %p, clust %lu, oldcon %p, newcon %lx)\n",
46298dc8da5SEd Maste 	    function, pmp, cn, oldcontents, newcontents);
46398dc8da5SEd Maste #endif
46498dc8da5SEd Maste 
46598dc8da5SEd Maste #ifdef DIAGNOSTIC
46698dc8da5SEd Maste 	/*
46798dc8da5SEd Maste 	 * Be sure they asked us to do something.
46898dc8da5SEd Maste 	 */
46998dc8da5SEd Maste 	if ((function & (FAT_SET | FAT_GET)) == 0) {
47098dc8da5SEd Maste #ifdef MSDOSFS_DEBUG
47198dc8da5SEd Maste 		printf("fatentry(): function code doesn't specify get or set\n");
47298dc8da5SEd Maste #endif
47398dc8da5SEd Maste 		return (EINVAL);
47498dc8da5SEd Maste 	}
47598dc8da5SEd Maste 
47698dc8da5SEd Maste 	/*
47798dc8da5SEd Maste 	 * If they asked us to return a cluster number but didn't tell us
47898dc8da5SEd Maste 	 * where to put it, give them an error.
47998dc8da5SEd Maste 	 */
48098dc8da5SEd Maste 	if ((function & FAT_GET) && oldcontents == NULL) {
48198dc8da5SEd Maste #ifdef MSDOSFS_DEBUG
48298dc8da5SEd Maste 		printf("fatentry(): get function with no place to put result\n");
48398dc8da5SEd Maste #endif
48498dc8da5SEd Maste 		return (EINVAL);
48598dc8da5SEd Maste 	}
48698dc8da5SEd Maste #endif
48798dc8da5SEd Maste 
48898dc8da5SEd Maste 	/*
48998dc8da5SEd Maste 	 * Be sure the requested cluster is in the filesystem.
49098dc8da5SEd Maste 	 */
49198dc8da5SEd Maste 	if (cn < CLUST_FIRST || cn > pmp->pm_maxcluster)
49298dc8da5SEd Maste 		return (EINVAL);
49398dc8da5SEd Maste 
49498dc8da5SEd Maste 	byteoffset = FATOFS(pmp, cn);
49598dc8da5SEd Maste 	fatblock(pmp, byteoffset, &bn, &bsize, &bo);
49698dc8da5SEd Maste 	error = bread(pmp->pm_devvp, bn, bsize, NOCRED, &bp);
49798dc8da5SEd Maste 	if (error) {
49898dc8da5SEd Maste 		brelse(bp);
49998dc8da5SEd Maste 		return (error);
50098dc8da5SEd Maste 	}
50198dc8da5SEd Maste 
50298dc8da5SEd Maste 	if (function & FAT_GET) {
50398dc8da5SEd Maste 		if (FAT32(pmp))
504ef633620SEd Maste 			readcn = getulong(bp->b_data + bo);
50598dc8da5SEd Maste 		else
506ef633620SEd Maste 			readcn = getushort(bp->b_data + bo);
50798dc8da5SEd Maste 		if (FAT12(pmp) & (cn & 1))
50898dc8da5SEd Maste 			readcn >>= 4;
50998dc8da5SEd Maste 		readcn &= pmp->pm_fatmask;
51098dc8da5SEd Maste 		/* map reserved FAT entries to same values for all FATs */
51198dc8da5SEd Maste 		if ((readcn | ~pmp->pm_fatmask) >= CLUST_RSRVD)
51298dc8da5SEd Maste 			readcn |= ~pmp->pm_fatmask;
51398dc8da5SEd Maste 		*oldcontents = readcn;
51498dc8da5SEd Maste 	}
51598dc8da5SEd Maste 	if (function & FAT_SET) {
51698dc8da5SEd Maste 		switch (pmp->pm_fatmask) {
51798dc8da5SEd Maste 		case FAT12_MASK:
518ef633620SEd Maste 			readcn = getushort(bp->b_data + bo);
51998dc8da5SEd Maste 			if (cn & 1) {
52098dc8da5SEd Maste 				readcn &= 0x000f;
52198dc8da5SEd Maste 				readcn |= newcontents << 4;
52298dc8da5SEd Maste 			} else {
52398dc8da5SEd Maste 				readcn &= 0xf000;
52498dc8da5SEd Maste 				readcn |= newcontents & 0xfff;
52598dc8da5SEd Maste 			}
526ef633620SEd Maste 			putushort(bp->b_data + bo, readcn);
52798dc8da5SEd Maste 			break;
52898dc8da5SEd Maste 		case FAT16_MASK:
529ef633620SEd Maste 			putushort(bp->b_data + bo, newcontents);
53098dc8da5SEd Maste 			break;
53198dc8da5SEd Maste 		case FAT32_MASK:
53298dc8da5SEd Maste 			/*
53398dc8da5SEd Maste 			 * According to spec we have to retain the
53498dc8da5SEd Maste 			 * high order bits of the FAT entry.
53598dc8da5SEd Maste 			 */
536ef633620SEd Maste 			readcn = getulong(bp->b_data + bo);
53798dc8da5SEd Maste 			readcn &= ~FAT32_MASK;
53898dc8da5SEd Maste 			readcn |= newcontents & FAT32_MASK;
539ef633620SEd Maste 			putulong(bp->b_data + bo, readcn);
54098dc8da5SEd Maste 			break;
54198dc8da5SEd Maste 		}
54298dc8da5SEd Maste 		updatefats(pmp, bp, bn);
54398dc8da5SEd Maste 		bp = NULL;
54498dc8da5SEd Maste 		pmp->pm_fmod = 1;
54598dc8da5SEd Maste 	}
54698dc8da5SEd Maste 	if (bp)
54798dc8da5SEd Maste 		brelse(bp);
54898dc8da5SEd Maste 	return (0);
54998dc8da5SEd Maste }
55098dc8da5SEd Maste 
55198dc8da5SEd Maste /*
55298dc8da5SEd Maste  * Update a contiguous cluster chain
55398dc8da5SEd Maste  *
55498dc8da5SEd Maste  * pmp	    - mount point
55598dc8da5SEd Maste  * start    - first cluster of chain
55698dc8da5SEd Maste  * count    - number of clusters in chain
55798dc8da5SEd Maste  * fillwith - what to write into FAT entry of last cluster
55898dc8da5SEd Maste  */
55998dc8da5SEd Maste static int
56098dc8da5SEd Maste fatchain(struct msdosfsmount *pmp, u_long start, u_long count, u_long fillwith)
56198dc8da5SEd Maste {
56298dc8da5SEd Maste 	int error;
56398dc8da5SEd Maste 	u_long bn, bo, bsize, byteoffset, readcn, newc;
56498dc8da5SEd Maste 	struct buf *bp;
56598dc8da5SEd Maste 
56698dc8da5SEd Maste #ifdef MSDOSFS_DEBUG
56798dc8da5SEd Maste 	printf("fatchain(pmp %p, start %lu, count %lu, fillwith %lx)\n",
56898dc8da5SEd Maste 	    pmp, start, count, fillwith);
56998dc8da5SEd Maste #endif
57098dc8da5SEd Maste 	/*
57198dc8da5SEd Maste 	 * Be sure the clusters are in the filesystem.
57298dc8da5SEd Maste 	 */
57398dc8da5SEd Maste 	if (start < CLUST_FIRST || start + count - 1 > pmp->pm_maxcluster)
57498dc8da5SEd Maste 		return (EINVAL);
57598dc8da5SEd Maste 
57698dc8da5SEd Maste 	while (count > 0) {
57798dc8da5SEd Maste 		byteoffset = FATOFS(pmp, start);
57898dc8da5SEd Maste 		fatblock(pmp, byteoffset, &bn, &bsize, &bo);
57998dc8da5SEd Maste 		error = bread(pmp->pm_devvp, bn, bsize, NOCRED, &bp);
58098dc8da5SEd Maste 		if (error) {
58198dc8da5SEd Maste 			brelse(bp);
58298dc8da5SEd Maste 			return (error);
58398dc8da5SEd Maste 		}
58498dc8da5SEd Maste 		while (count > 0) {
58598dc8da5SEd Maste 			start++;
58698dc8da5SEd Maste 			newc = --count > 0 ? start : fillwith;
58798dc8da5SEd Maste 			switch (pmp->pm_fatmask) {
58898dc8da5SEd Maste 			case FAT12_MASK:
589ef633620SEd Maste 				readcn = getushort(bp->b_data + bo);
59098dc8da5SEd Maste 				if (start & 1) {
59198dc8da5SEd Maste 					readcn &= 0xf000;
59298dc8da5SEd Maste 					readcn |= newc & 0xfff;
59398dc8da5SEd Maste 				} else {
59498dc8da5SEd Maste 					readcn &= 0x000f;
59598dc8da5SEd Maste 					readcn |= newc << 4;
59698dc8da5SEd Maste 				}
597ef633620SEd Maste 				putushort(bp->b_data + bo, readcn);
59898dc8da5SEd Maste 				bo++;
59998dc8da5SEd Maste 				if (!(start & 1))
60098dc8da5SEd Maste 					bo++;
60198dc8da5SEd Maste 				break;
60298dc8da5SEd Maste 			case FAT16_MASK:
603ef633620SEd Maste 				putushort(bp->b_data + bo, newc);
60498dc8da5SEd Maste 				bo += 2;
60598dc8da5SEd Maste 				break;
60698dc8da5SEd Maste 			case FAT32_MASK:
607ef633620SEd Maste 				readcn = getulong(bp->b_data + bo);
60898dc8da5SEd Maste 				readcn &= ~pmp->pm_fatmask;
60998dc8da5SEd Maste 				readcn |= newc & pmp->pm_fatmask;
610ef633620SEd Maste 				putulong(bp->b_data + bo, readcn);
61198dc8da5SEd Maste 				bo += 4;
61298dc8da5SEd Maste 				break;
61398dc8da5SEd Maste 			}
61498dc8da5SEd Maste 			if (bo >= bsize)
61598dc8da5SEd Maste 				break;
61698dc8da5SEd Maste 		}
61798dc8da5SEd Maste 		updatefats(pmp, bp, bn);
61898dc8da5SEd Maste 	}
61998dc8da5SEd Maste 	pmp->pm_fmod = 1;
62098dc8da5SEd Maste 	return (0);
62198dc8da5SEd Maste }
62298dc8da5SEd Maste 
62398dc8da5SEd Maste /*
62498dc8da5SEd Maste  * Check the length of a free cluster chain starting at start.
62598dc8da5SEd Maste  *
62698dc8da5SEd Maste  * pmp	 - mount point
62798dc8da5SEd Maste  * start - start of chain
62898dc8da5SEd Maste  * count - maximum interesting length
62998dc8da5SEd Maste  */
63098dc8da5SEd Maste static int
63198dc8da5SEd Maste chainlength(struct msdosfsmount *pmp, u_long start, u_long count)
63298dc8da5SEd Maste {
63398dc8da5SEd Maste 	u_long idx, max_idx;
63498dc8da5SEd Maste 	u_int map;
63598dc8da5SEd Maste 	u_long len;
63698dc8da5SEd Maste 
63798dc8da5SEd Maste 	if (start > pmp->pm_maxcluster)
63898dc8da5SEd Maste 		return (0);
63998dc8da5SEd Maste 	max_idx = pmp->pm_maxcluster / N_INUSEBITS;
64098dc8da5SEd Maste 	idx = start / N_INUSEBITS;
64198dc8da5SEd Maste 	start %= N_INUSEBITS;
64298dc8da5SEd Maste 	map = pmp->pm_inusemap[idx];
64398dc8da5SEd Maste 	map &= ~((1 << start) - 1);
64498dc8da5SEd Maste 	if (map) {
64598dc8da5SEd Maste 		len = ffs(map) - 1 - start;
64698dc8da5SEd Maste 		len = MIN(len, count);
64798dc8da5SEd Maste 		if (start + len > pmp->pm_maxcluster)
64898dc8da5SEd Maste 			len = pmp->pm_maxcluster - start + 1;
64998dc8da5SEd Maste 		return (len);
65098dc8da5SEd Maste 	}
65198dc8da5SEd Maste 	len = N_INUSEBITS - start;
65298dc8da5SEd Maste 	if (len >= count) {
65398dc8da5SEd Maste 		len = count;
65498dc8da5SEd Maste 		if (start + len > pmp->pm_maxcluster)
65598dc8da5SEd Maste 			len = pmp->pm_maxcluster - start + 1;
65698dc8da5SEd Maste 		return (len);
65798dc8da5SEd Maste 	}
65898dc8da5SEd Maste 	while (++idx <= max_idx) {
65998dc8da5SEd Maste 		if (len >= count)
66098dc8da5SEd Maste 			break;
66198dc8da5SEd Maste 		map = pmp->pm_inusemap[idx];
66298dc8da5SEd Maste 		if (map) {
66398dc8da5SEd Maste 			len += ffs(map) - 1;
66498dc8da5SEd Maste 			break;
66598dc8da5SEd Maste 		}
66698dc8da5SEd Maste 		len += N_INUSEBITS;
66798dc8da5SEd Maste 	}
66898dc8da5SEd Maste 	len = MIN(len, count);
66998dc8da5SEd Maste 	if (start + len > pmp->pm_maxcluster)
67098dc8da5SEd Maste 		len = pmp->pm_maxcluster - start + 1;
67198dc8da5SEd Maste 	return (len);
67298dc8da5SEd Maste }
67398dc8da5SEd Maste 
67498dc8da5SEd Maste /*
67598dc8da5SEd Maste  * Allocate contigous free clusters.
67698dc8da5SEd Maste  *
67798dc8da5SEd Maste  * pmp	      - mount point.
67898dc8da5SEd Maste  * start      - start of cluster chain.
67998dc8da5SEd Maste  * count      - number of clusters to allocate.
68098dc8da5SEd Maste  * fillwith   - put this value into the FAT entry for the
68198dc8da5SEd Maste  *		last allocated cluster.
68298dc8da5SEd Maste  * retcluster - put the first allocated cluster's number here.
68398dc8da5SEd Maste  * got	      - how many clusters were actually allocated.
68498dc8da5SEd Maste  */
68598dc8da5SEd Maste static int
68698dc8da5SEd Maste chainalloc(struct msdosfsmount *pmp, u_long start, u_long count,
68798dc8da5SEd Maste     u_long fillwith, u_long *retcluster, u_long *got)
68898dc8da5SEd Maste {
68998dc8da5SEd Maste 	int error;
69098dc8da5SEd Maste 	u_long cl, n;
69198dc8da5SEd Maste 
69298dc8da5SEd Maste 	assert((pmp->pm_flags & MSDOSFSMNT_RONLY) == 0);
69398dc8da5SEd Maste 
69498dc8da5SEd Maste 	for (cl = start, n = count; n-- > 0;)
69598dc8da5SEd Maste 		usemap_alloc(pmp, cl++);
69698dc8da5SEd Maste 	pmp->pm_nxtfree = start + count;
69798dc8da5SEd Maste 	if (pmp->pm_nxtfree > pmp->pm_maxcluster)
69898dc8da5SEd Maste 		pmp->pm_nxtfree = CLUST_FIRST;
69998dc8da5SEd Maste 	pmp->pm_flags |= MSDOSFS_FSIMOD;
70098dc8da5SEd Maste 	error = fatchain(pmp, start, count, fillwith);
70198dc8da5SEd Maste 	if (error != 0) {
70298dc8da5SEd Maste 		for (cl = start, n = count; n-- > 0;)
70398dc8da5SEd Maste 			usemap_free(pmp, cl++);
70498dc8da5SEd Maste 		return (error);
70598dc8da5SEd Maste 	}
70698dc8da5SEd Maste #ifdef MSDOSFS_DEBUG
70798dc8da5SEd Maste 	printf("clusteralloc(): allocated cluster chain at %lu (%lu clusters)\n",
70898dc8da5SEd Maste 	    start, count);
70998dc8da5SEd Maste #endif
71098dc8da5SEd Maste 	if (retcluster)
71198dc8da5SEd Maste 		*retcluster = start;
71298dc8da5SEd Maste 	if (got)
71398dc8da5SEd Maste 		*got = count;
71498dc8da5SEd Maste 	return (0);
71598dc8da5SEd Maste }
71698dc8da5SEd Maste 
71798dc8da5SEd Maste /*
71898dc8da5SEd Maste  * Allocate contiguous free clusters.
71998dc8da5SEd Maste  *
72098dc8da5SEd Maste  * pmp	      - mount point.
72198dc8da5SEd Maste  * start      - preferred start of cluster chain.
72298dc8da5SEd Maste  * count      - number of clusters requested.
72398dc8da5SEd Maste  * fillwith   - put this value into the FAT entry for the
72498dc8da5SEd Maste  *		last allocated cluster.
72598dc8da5SEd Maste  * retcluster - put the first allocated cluster's number here.
72698dc8da5SEd Maste  * got	      - how many clusters were actually allocated.
72798dc8da5SEd Maste  */
72898dc8da5SEd Maste int
72998dc8da5SEd Maste clusteralloc(struct msdosfsmount *pmp, u_long start, u_long count,
73098dc8da5SEd Maste     u_long fillwith, u_long *retcluster, u_long *got)
73198dc8da5SEd Maste {
73298dc8da5SEd Maste 	int error;
73398dc8da5SEd Maste 
73498dc8da5SEd Maste 	error = clusteralloc1(pmp, start, count, fillwith, retcluster, got);
73598dc8da5SEd Maste 	return (error);
73698dc8da5SEd Maste }
73798dc8da5SEd Maste 
73898dc8da5SEd Maste static int
73998dc8da5SEd Maste clusteralloc1(struct msdosfsmount *pmp, u_long start, u_long count,
74098dc8da5SEd Maste     u_long fillwith, u_long *retcluster, u_long *got)
74198dc8da5SEd Maste {
74298dc8da5SEd Maste 	u_long idx;
74398dc8da5SEd Maste 	u_long len, newst, foundl, cn, l;
74498dc8da5SEd Maste 	u_long foundcn = 0; /* XXX: foundcn could be used unititialized */
74598dc8da5SEd Maste 	u_int map;
74698dc8da5SEd Maste 
74798dc8da5SEd Maste 	MSDOSFS_DPRINTF(("clusteralloc(): find %lu clusters\n", count));
74898dc8da5SEd Maste 
74998dc8da5SEd Maste 	if (start) {
75098dc8da5SEd Maste 		if ((len = chainlength(pmp, start, count)) >= count)
75198dc8da5SEd Maste 			return (chainalloc(pmp, start, count, fillwith, retcluster, got));
75298dc8da5SEd Maste 	} else
75398dc8da5SEd Maste 		len = 0;
75498dc8da5SEd Maste 
75598dc8da5SEd Maste 	newst = pmp->pm_nxtfree;
75698dc8da5SEd Maste 	foundl = 0;
75798dc8da5SEd Maste 
75898dc8da5SEd Maste 	for (cn = newst; cn <= pmp->pm_maxcluster;) {
75998dc8da5SEd Maste 		idx = cn / N_INUSEBITS;
76098dc8da5SEd Maste 		map = pmp->pm_inusemap[idx];
76198dc8da5SEd Maste 		map |= (1U << (cn % N_INUSEBITS)) - 1;
76298dc8da5SEd Maste 		if (map != FULL_RUN) {
76398dc8da5SEd Maste 			cn = idx * N_INUSEBITS + ffs(map ^ FULL_RUN) - 1;
76498dc8da5SEd Maste 			if ((l = chainlength(pmp, cn, count)) >= count)
76598dc8da5SEd Maste 				return (chainalloc(pmp, cn, count, fillwith, retcluster, got));
76698dc8da5SEd Maste 			if (l > foundl) {
76798dc8da5SEd Maste 				foundcn = cn;
76898dc8da5SEd Maste 				foundl = l;
76998dc8da5SEd Maste 			}
77098dc8da5SEd Maste 			cn += l + 1;
77198dc8da5SEd Maste 			continue;
77298dc8da5SEd Maste 		}
77398dc8da5SEd Maste 		cn += N_INUSEBITS - cn % N_INUSEBITS;
77498dc8da5SEd Maste 	}
77598dc8da5SEd Maste 	for (cn = 0; cn < newst;) {
77698dc8da5SEd Maste 		idx = cn / N_INUSEBITS;
77798dc8da5SEd Maste 		map = pmp->pm_inusemap[idx];
77898dc8da5SEd Maste 		map |= (1U << (cn % N_INUSEBITS)) - 1;
77998dc8da5SEd Maste 		if (map != FULL_RUN) {
78098dc8da5SEd Maste 			cn = idx * N_INUSEBITS + ffs(map ^ FULL_RUN) - 1;
78198dc8da5SEd Maste 			if ((l = chainlength(pmp, cn, count)) >= count)
78298dc8da5SEd Maste 				return (chainalloc(pmp, cn, count, fillwith, retcluster, got));
78398dc8da5SEd Maste 			if (l > foundl) {
78498dc8da5SEd Maste 				foundcn = cn;
78598dc8da5SEd Maste 				foundl = l;
78698dc8da5SEd Maste 			}
78798dc8da5SEd Maste 			cn += l + 1;
78898dc8da5SEd Maste 			continue;
78998dc8da5SEd Maste 		}
79098dc8da5SEd Maste 		cn += N_INUSEBITS - cn % N_INUSEBITS;
79198dc8da5SEd Maste 	}
79298dc8da5SEd Maste 
79398dc8da5SEd Maste 	if (!foundl)
79498dc8da5SEd Maste 		return (ENOSPC);
79598dc8da5SEd Maste 
79698dc8da5SEd Maste 	if (len)
79798dc8da5SEd Maste 		return (chainalloc(pmp, start, len, fillwith, retcluster, got));
79898dc8da5SEd Maste 	else
79998dc8da5SEd Maste 		return (chainalloc(pmp, foundcn, foundl, fillwith, retcluster, got));
80098dc8da5SEd Maste }
80198dc8da5SEd Maste 
80298dc8da5SEd Maste 
80398dc8da5SEd Maste /*
80498dc8da5SEd Maste  * Free a chain of clusters.
80598dc8da5SEd Maste  *
80698dc8da5SEd Maste  * pmp		- address of the msdosfs mount structure for the filesystem
80798dc8da5SEd Maste  *		  containing the cluster chain to be freed.
80898dc8da5SEd Maste  * startcluster - number of the 1st cluster in the chain of clusters to be
80998dc8da5SEd Maste  *		  freed.
81098dc8da5SEd Maste  */
81198dc8da5SEd Maste int
81298dc8da5SEd Maste freeclusterchain(struct msdosfsmount *pmp, u_long cluster)
81398dc8da5SEd Maste {
81498dc8da5SEd Maste 	int error;
81598dc8da5SEd Maste 	struct buf *bp = NULL;
81698dc8da5SEd Maste 	u_long bn, bo, bsize, byteoffset;
81798dc8da5SEd Maste 	u_long readcn, lbn = -1;
81898dc8da5SEd Maste 
81998dc8da5SEd Maste 	while (cluster >= CLUST_FIRST && cluster <= pmp->pm_maxcluster) {
82098dc8da5SEd Maste 		byteoffset = FATOFS(pmp, cluster);
82198dc8da5SEd Maste 		fatblock(pmp, byteoffset, &bn, &bsize, &bo);
82298dc8da5SEd Maste 		if (lbn != bn) {
82398dc8da5SEd Maste 			if (bp)
82498dc8da5SEd Maste 				updatefats(pmp, bp, lbn);
82598dc8da5SEd Maste 			error = bread(pmp->pm_devvp, bn, bsize, NOCRED, &bp);
82698dc8da5SEd Maste 			if (error) {
82798dc8da5SEd Maste 				brelse(bp);
82898dc8da5SEd Maste 				return (error);
82998dc8da5SEd Maste 			}
83098dc8da5SEd Maste 			lbn = bn;
83198dc8da5SEd Maste 		}
83298dc8da5SEd Maste 		usemap_free(pmp, cluster);
83398dc8da5SEd Maste 		switch (pmp->pm_fatmask) {
83498dc8da5SEd Maste 		case FAT12_MASK:
835ef633620SEd Maste 			readcn = getushort(bp->b_data + bo);
83698dc8da5SEd Maste 			if (cluster & 1) {
83798dc8da5SEd Maste 				cluster = readcn >> 4;
83898dc8da5SEd Maste 				readcn &= 0x000f;
83998dc8da5SEd Maste 				readcn |= MSDOSFSFREE << 4;
84098dc8da5SEd Maste 			} else {
84198dc8da5SEd Maste 				cluster = readcn;
84298dc8da5SEd Maste 				readcn &= 0xf000;
84398dc8da5SEd Maste 				readcn |= MSDOSFSFREE & 0xfff;
84498dc8da5SEd Maste 			}
845ef633620SEd Maste 			putushort(bp->b_data + bo, readcn);
84698dc8da5SEd Maste 			break;
84798dc8da5SEd Maste 		case FAT16_MASK:
848ef633620SEd Maste 			cluster = getushort(bp->b_data + bo);
849ef633620SEd Maste 			putushort(bp->b_data + bo, MSDOSFSFREE);
85098dc8da5SEd Maste 			break;
85198dc8da5SEd Maste 		case FAT32_MASK:
852ef633620SEd Maste 			cluster = getulong(bp->b_data + bo);
853ef633620SEd Maste 			putulong(bp->b_data + bo,
85498dc8da5SEd Maste 				 (MSDOSFSFREE & FAT32_MASK) | (cluster & ~FAT32_MASK));
85598dc8da5SEd Maste 			break;
85698dc8da5SEd Maste 		}
85798dc8da5SEd Maste 		cluster &= pmp->pm_fatmask;
85898dc8da5SEd Maste 		if ((cluster | ~pmp->pm_fatmask) >= CLUST_RSRVD)
85998dc8da5SEd Maste 			cluster |= pmp->pm_fatmask;
86098dc8da5SEd Maste 	}
86198dc8da5SEd Maste 	if (bp)
86298dc8da5SEd Maste 		updatefats(pmp, bp, bn);
86398dc8da5SEd Maste 	return (0);
86498dc8da5SEd Maste }
86598dc8da5SEd Maste 
86698dc8da5SEd Maste /*
86798dc8da5SEd Maste  * Read in FAT blocks looking for free clusters. For every free cluster
86898dc8da5SEd Maste  * found turn off its corresponding bit in the pm_inusemap.
86998dc8da5SEd Maste  */
87098dc8da5SEd Maste int
87198dc8da5SEd Maste fillinusemap(struct msdosfsmount *pmp)
87298dc8da5SEd Maste {
87398dc8da5SEd Maste 	struct buf *bp = NULL;
87498dc8da5SEd Maste 	u_long bn, bo, bsize, byteoffset, cn, readcn;
87598dc8da5SEd Maste 	int error;
87698dc8da5SEd Maste 
87798dc8da5SEd Maste 	/*
87898dc8da5SEd Maste 	 * Mark all clusters in use, we mark the free ones in the FAT scan
87998dc8da5SEd Maste 	 * loop further down.
88098dc8da5SEd Maste 	 */
88198dc8da5SEd Maste 	for (cn = 0; cn < (pmp->pm_maxcluster + N_INUSEBITS) / N_INUSEBITS; cn++)
88298dc8da5SEd Maste 		pmp->pm_inusemap[cn] = FULL_RUN;
88398dc8da5SEd Maste 
88498dc8da5SEd Maste 	/*
88598dc8da5SEd Maste 	 * Figure how many free clusters are in the filesystem by ripping
88698dc8da5SEd Maste 	 * through the FAT counting the number of entries whose content is
88798dc8da5SEd Maste 	 * zero.  These represent free clusters.
88898dc8da5SEd Maste 	 */
88998dc8da5SEd Maste 	pmp->pm_freeclustercount = 0;
89098dc8da5SEd Maste 	for (cn = CLUST_FIRST; cn <= pmp->pm_maxcluster; cn++) {
89198dc8da5SEd Maste 		byteoffset = FATOFS(pmp, cn);
89298dc8da5SEd Maste 		bo = byteoffset % pmp->pm_fatblocksize;
89398dc8da5SEd Maste 		if (bo == 0 || bp == NULL) {
89498dc8da5SEd Maste 			/* Read new FAT block */
89598dc8da5SEd Maste 			if (bp != NULL)
89698dc8da5SEd Maste 				brelse(bp);
89798dc8da5SEd Maste 			fatblock(pmp, byteoffset, &bn, &bsize, NULL);
89898dc8da5SEd Maste 			error = bread(pmp->pm_devvp, bn, bsize, NOCRED, &bp);
89998dc8da5SEd Maste 			if (error != 0) {
90098dc8da5SEd Maste 				brelse(bp);
90198dc8da5SEd Maste 				return (error);
90298dc8da5SEd Maste 			}
90398dc8da5SEd Maste 		}
90498dc8da5SEd Maste 		if (FAT32(pmp))
905ef633620SEd Maste 			readcn = getulong(bp->b_data + bo);
90698dc8da5SEd Maste 		else
907ef633620SEd Maste 			readcn = getushort(bp->b_data + bo);
90898dc8da5SEd Maste 		if (FAT12(pmp) && (cn & 1))
90998dc8da5SEd Maste 			readcn >>= 4;
91098dc8da5SEd Maste 		readcn &= pmp->pm_fatmask;
91198dc8da5SEd Maste 
91298dc8da5SEd Maste 		if (readcn == CLUST_FREE)
91398dc8da5SEd Maste 			usemap_free(pmp, cn);
91498dc8da5SEd Maste 	}
91598dc8da5SEd Maste 	if (bp != NULL)
91698dc8da5SEd Maste 		brelse(bp);
91798dc8da5SEd Maste 
91898dc8da5SEd Maste 	for (cn = pmp->pm_maxcluster + 1; cn < (pmp->pm_maxcluster +
91998dc8da5SEd Maste 	    N_INUSEBITS) / N_INUSEBITS; cn++)
92098dc8da5SEd Maste 		pmp->pm_inusemap[cn / N_INUSEBITS] |= 1U << (cn % N_INUSEBITS);
92198dc8da5SEd Maste 
92298dc8da5SEd Maste 	return (0);
92398dc8da5SEd Maste }
92498dc8da5SEd Maste 
92598dc8da5SEd Maste /*
92698dc8da5SEd Maste  * Allocate a new cluster and chain it onto the end of the file.
92798dc8da5SEd Maste  *
92898dc8da5SEd Maste  * dep	 - the file to extend
92998dc8da5SEd Maste  * count - number of clusters to allocate
93098dc8da5SEd Maste  * bpp	 - where to return the address of the buf header for the first new
93198dc8da5SEd Maste  *	   file block
93298dc8da5SEd Maste  * ncp	 - where to put cluster number of the first newly allocated cluster
93398dc8da5SEd Maste  *	   If this pointer is 0, do not return the cluster number.
93498dc8da5SEd Maste  * flags - see fat.h
93598dc8da5SEd Maste  *
93698dc8da5SEd Maste  * NOTE: This function is not responsible for turning on the DE_UPDATE bit of
93798dc8da5SEd Maste  * the de_flag field of the denode and it does not change the de_FileSize
93898dc8da5SEd Maste  * field.  This is left for the caller to do.
93998dc8da5SEd Maste  */
94098dc8da5SEd Maste int
94198dc8da5SEd Maste extendfile(struct denode *dep, u_long count, struct buf **bpp, u_long *ncp,
94298dc8da5SEd Maste     int flags)
94398dc8da5SEd Maste {
94498dc8da5SEd Maste 	int error;
94598dc8da5SEd Maste 	u_long frcn;
94698dc8da5SEd Maste 	u_long cn, got;
94798dc8da5SEd Maste 	struct msdosfsmount *pmp = dep->de_pmp;
94898dc8da5SEd Maste 	struct buf *bp;
94998dc8da5SEd Maste 
95098dc8da5SEd Maste 	/*
95198dc8da5SEd Maste 	 * Don't try to extend the root directory
95298dc8da5SEd Maste 	 */
95398dc8da5SEd Maste 	if (dep->de_StartCluster == MSDOSFSROOT
95498dc8da5SEd Maste 	    && (dep->de_Attributes & ATTR_DIRECTORY)) {
95598dc8da5SEd Maste #ifdef MSDOSFS_DEBUG
95698dc8da5SEd Maste 		printf("extendfile(): attempt to extend root directory\n");
95798dc8da5SEd Maste #endif
95898dc8da5SEd Maste 		return (ENOSPC);
95998dc8da5SEd Maste 	}
96098dc8da5SEd Maste 
96198dc8da5SEd Maste 	/*
96298dc8da5SEd Maste 	 * If the "file's last cluster" cache entry is empty, and the file
96398dc8da5SEd Maste 	 * is not empty, then fill the cache entry by calling pcbmap().
96498dc8da5SEd Maste 	 */
96598dc8da5SEd Maste 	if (dep->de_fc[FC_LASTFC].fc_frcn == FCE_EMPTY &&
96698dc8da5SEd Maste 	    dep->de_StartCluster != 0) {
96798dc8da5SEd Maste 		error = pcbmap(dep, 0xffff, 0, &cn, 0);
96898dc8da5SEd Maste 		/* we expect it to return E2BIG */
96998dc8da5SEd Maste 		if (error != E2BIG)
97098dc8da5SEd Maste 			return (error);
97198dc8da5SEd Maste 	}
97298dc8da5SEd Maste 
97398dc8da5SEd Maste 	dep->de_fc[FC_NEXTTOLASTFC].fc_frcn =
97498dc8da5SEd Maste 	    dep->de_fc[FC_LASTFC].fc_frcn;
97598dc8da5SEd Maste 	dep->de_fc[FC_NEXTTOLASTFC].fc_fsrcn =
97698dc8da5SEd Maste 	    dep->de_fc[FC_LASTFC].fc_fsrcn;
97798dc8da5SEd Maste 	while (count > 0) {
97898dc8da5SEd Maste 		/*
97998dc8da5SEd Maste 		 * Allocate a new cluster chain and cat onto the end of the
98098dc8da5SEd Maste 		 * file.  If the file is empty we make de_StartCluster point
98198dc8da5SEd Maste 		 * to the new block.  Note that de_StartCluster being 0 is
98298dc8da5SEd Maste 		 * sufficient to be sure the file is empty since we exclude
98398dc8da5SEd Maste 		 * attempts to extend the root directory above, and the root
98498dc8da5SEd Maste 		 * dir is the only file with a startcluster of 0 that has
98598dc8da5SEd Maste 		 * blocks allocated (sort of).
98698dc8da5SEd Maste 		 */
98798dc8da5SEd Maste 		if (dep->de_StartCluster == 0)
98898dc8da5SEd Maste 			cn = 0;
98998dc8da5SEd Maste 		else
99098dc8da5SEd Maste 			cn = dep->de_fc[FC_LASTFC].fc_fsrcn + 1;
99198dc8da5SEd Maste 		error = clusteralloc(pmp, cn, count, CLUST_EOFE, &cn, &got);
99298dc8da5SEd Maste 		if (error)
99398dc8da5SEd Maste 			return (error);
99498dc8da5SEd Maste 
99598dc8da5SEd Maste 		count -= got;
99698dc8da5SEd Maste 
99798dc8da5SEd Maste 		/*
99898dc8da5SEd Maste 		 * Give them the filesystem relative cluster number if they want
99998dc8da5SEd Maste 		 * it.
100098dc8da5SEd Maste 		 */
100198dc8da5SEd Maste 		if (ncp) {
100298dc8da5SEd Maste 			*ncp = cn;
100398dc8da5SEd Maste 			ncp = NULL;
100498dc8da5SEd Maste 		}
100598dc8da5SEd Maste 
100698dc8da5SEd Maste 		if (dep->de_StartCluster == 0) {
100798dc8da5SEd Maste 			dep->de_StartCluster = cn;
100898dc8da5SEd Maste 			frcn = 0;
100998dc8da5SEd Maste 		} else {
101098dc8da5SEd Maste 			error = fatentry(FAT_SET, pmp,
101198dc8da5SEd Maste 					 dep->de_fc[FC_LASTFC].fc_fsrcn,
101298dc8da5SEd Maste 					 0, cn);
101398dc8da5SEd Maste 			if (error) {
101498dc8da5SEd Maste 				clusterfree(pmp, cn, NULL);
101598dc8da5SEd Maste 				return (error);
101698dc8da5SEd Maste 			}
101798dc8da5SEd Maste 			frcn = dep->de_fc[FC_LASTFC].fc_frcn + 1;
101898dc8da5SEd Maste 		}
101998dc8da5SEd Maste 
102098dc8da5SEd Maste 		/*
102198dc8da5SEd Maste 		 * Update the "last cluster of the file" entry in the
102298dc8da5SEd Maste 		 * denode's FAT cache.
102398dc8da5SEd Maste 		 */
102498dc8da5SEd Maste 		fc_setcache(dep, FC_LASTFC, frcn + got - 1, cn + got - 1);
102598dc8da5SEd Maste 
102698dc8da5SEd Maste 		if ((flags & DE_CLEAR) &&
102798dc8da5SEd Maste 		    (dep->de_Attributes & ATTR_DIRECTORY)) {
102898dc8da5SEd Maste 			while (got-- > 0) {
102998dc8da5SEd Maste 				bp = getblk(pmp->pm_devvp,
103098dc8da5SEd Maste 				    cntobn(pmp, cn++),
103198dc8da5SEd Maste 				    pmp->pm_bpcluster, 0, 0, 0);
103298dc8da5SEd Maste 				clrbuf(bp);
103398dc8da5SEd Maste 				if (bpp) {
103498dc8da5SEd Maste 					*bpp = bp;
103598dc8da5SEd Maste 					bpp = NULL;
103698dc8da5SEd Maste 				} else {
103798dc8da5SEd Maste 					bdwrite(bp);
103898dc8da5SEd Maste 				}
103998dc8da5SEd Maste 			}
104098dc8da5SEd Maste 		}
104198dc8da5SEd Maste 	}
104298dc8da5SEd Maste 
104398dc8da5SEd Maste 	return (0);
104498dc8da5SEd Maste }
1045