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