xref: /freebsd/sys/fs/msdosfs/msdosfs_fat.c (revision a98ca4699e496aa92e57b376defd0647b9ed5628)
1a98ca469SPoul-Henning Kamp /*	$Id: msdosfs_fat.c,v 1.7 1995/05/30 08:07:40 rgrimes Exp $ */
227a0bc89SDoug Rabson /*	$NetBSD: msdosfs_fat.c,v 1.12 1994/08/21 18:44:04 ws Exp $	*/
327a0bc89SDoug Rabson 
427a0bc89SDoug Rabson /*-
527a0bc89SDoug Rabson  * Copyright (C) 1994 Wolfgang Solfrank.
627a0bc89SDoug Rabson  * Copyright (C) 1994 TooLs GmbH.
727a0bc89SDoug Rabson  * All rights reserved.
827a0bc89SDoug Rabson  * Original code by Paul Popelka (paulp@uts.amdahl.com) (see below).
927a0bc89SDoug Rabson  *
1027a0bc89SDoug Rabson  * Redistribution and use in source and binary forms, with or without
1127a0bc89SDoug Rabson  * modification, are permitted provided that the following conditions
1227a0bc89SDoug Rabson  * are met:
1327a0bc89SDoug Rabson  * 1. Redistributions of source code must retain the above copyright
1427a0bc89SDoug Rabson  *    notice, this list of conditions and the following disclaimer.
1527a0bc89SDoug Rabson  * 2. Redistributions in binary form must reproduce the above copyright
1627a0bc89SDoug Rabson  *    notice, this list of conditions and the following disclaimer in the
1727a0bc89SDoug Rabson  *    documentation and/or other materials provided with the distribution.
1827a0bc89SDoug Rabson  * 3. All advertising materials mentioning features or use of this software
1927a0bc89SDoug Rabson  *    must display the following acknowledgement:
2027a0bc89SDoug Rabson  *	This product includes software developed by TooLs GmbH.
2127a0bc89SDoug Rabson  * 4. The name of TooLs GmbH may not be used to endorse or promote products
2227a0bc89SDoug Rabson  *    derived from this software without specific prior written permission.
2327a0bc89SDoug Rabson  *
2427a0bc89SDoug Rabson  * THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``AS IS'' AND ANY EXPRESS OR
2527a0bc89SDoug Rabson  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
2627a0bc89SDoug Rabson  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
2727a0bc89SDoug Rabson  * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
2827a0bc89SDoug Rabson  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
2927a0bc89SDoug Rabson  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
3027a0bc89SDoug Rabson  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
3127a0bc89SDoug Rabson  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
3227a0bc89SDoug Rabson  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
3327a0bc89SDoug Rabson  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
3427a0bc89SDoug Rabson  */
3527a0bc89SDoug Rabson /*
3627a0bc89SDoug Rabson  * Written by Paul Popelka (paulp@uts.amdahl.com)
3727a0bc89SDoug Rabson  *
3827a0bc89SDoug Rabson  * You can do anything you want with this software, just don't say you wrote
3927a0bc89SDoug Rabson  * it, and don't remove this notice.
4027a0bc89SDoug Rabson  *
4127a0bc89SDoug Rabson  * This software is provided "as is".
4227a0bc89SDoug Rabson  *
4327a0bc89SDoug Rabson  * The author supplies this software to be publicly redistributed on the
4427a0bc89SDoug Rabson  * understanding that the author is not responsible for the correct
4527a0bc89SDoug Rabson  * functioning of this software in any circumstances and is not liable for
4627a0bc89SDoug Rabson  * any damages caused by this software.
4727a0bc89SDoug Rabson  *
4827a0bc89SDoug Rabson  * October 1992
4927a0bc89SDoug Rabson  */
5027a0bc89SDoug Rabson 
5127a0bc89SDoug Rabson /*
5227a0bc89SDoug Rabson  * kernel include files.
5327a0bc89SDoug Rabson  */
5427a0bc89SDoug Rabson #include <sys/param.h>
5527a0bc89SDoug Rabson #include <sys/systm.h>
5627a0bc89SDoug Rabson #include <sys/buf.h>
5727a0bc89SDoug Rabson #include <sys/file.h>
5827a0bc89SDoug Rabson #include <sys/namei.h>
5927a0bc89SDoug Rabson #include <sys/mount.h>		/* to define statfs structure */
6027a0bc89SDoug Rabson #include <sys/vnode.h>		/* to define vattr structure */
6127a0bc89SDoug Rabson #include <sys/errno.h>
6227a0bc89SDoug Rabson 
6327a0bc89SDoug Rabson /*
6427a0bc89SDoug Rabson  * msdosfs include files.
6527a0bc89SDoug Rabson  */
6627a0bc89SDoug Rabson #include <msdosfs/bpb.h>
6727a0bc89SDoug Rabson #include <msdosfs/msdosfsmount.h>
6827a0bc89SDoug Rabson #include <msdosfs/direntry.h>
6927a0bc89SDoug Rabson #include <msdosfs/denode.h>
7027a0bc89SDoug Rabson #include <msdosfs/fat.h>
7127a0bc89SDoug Rabson 
7227a0bc89SDoug Rabson /*
7327a0bc89SDoug Rabson  * Fat cache stats.
7427a0bc89SDoug Rabson  */
7527a0bc89SDoug Rabson int fc_fileextends;		/* # of file extends			 */
7627a0bc89SDoug Rabson int fc_lfcempty;		/* # of time last file cluster cache entry
7727a0bc89SDoug Rabson 				 * was empty */
7827a0bc89SDoug Rabson int fc_bmapcalls;		/* # of times pcbmap was called		 */
7927a0bc89SDoug Rabson 
8027a0bc89SDoug Rabson #define	LMMAX	20
8127a0bc89SDoug Rabson int fc_lmdistance[LMMAX];	/* counters for how far off the last
8227a0bc89SDoug Rabson 				 * cluster mapped entry was. */
8327a0bc89SDoug Rabson int fc_largedistance;		/* off by more than LMMAX		 */
8427a0bc89SDoug Rabson 
8527a0bc89SDoug Rabson /* Byte offset in FAT on filesystem pmp, cluster cn */
8627a0bc89SDoug Rabson #define	FATOFS(pmp, cn)	(FAT12(pmp) ? (cn) * 3 / 2 : (cn) * 2)
8727a0bc89SDoug Rabson 
8827a0bc89SDoug Rabson static void
8927a0bc89SDoug Rabson fatblock(pmp, ofs, bnp, sizep, bop)
9027a0bc89SDoug Rabson 	struct msdosfsmount *pmp;
9127a0bc89SDoug Rabson 	u_long ofs;
9227a0bc89SDoug Rabson 	u_long *bnp;
9327a0bc89SDoug Rabson 	u_long *sizep;
9427a0bc89SDoug Rabson 	u_long *bop;
9527a0bc89SDoug Rabson {
9627a0bc89SDoug Rabson 	u_long bn, size;
9727a0bc89SDoug Rabson 
9827a0bc89SDoug Rabson 	bn = ofs / pmp->pm_fatblocksize * pmp->pm_fatblocksec;
9927a0bc89SDoug Rabson 	size = min(pmp->pm_fatblocksec, pmp->pm_FATsecs - bn)
10027a0bc89SDoug Rabson 	    * pmp->pm_BytesPerSec;
10127a0bc89SDoug Rabson 	bn += pmp->pm_fatblk;
10227a0bc89SDoug Rabson 	if (bnp)
10327a0bc89SDoug Rabson 		*bnp = bn;
10427a0bc89SDoug Rabson 	if (sizep)
10527a0bc89SDoug Rabson 		*sizep = size;
10627a0bc89SDoug Rabson 	if (bop)
10727a0bc89SDoug Rabson 		*bop = ofs % pmp->pm_fatblocksize;
10827a0bc89SDoug Rabson }
10927a0bc89SDoug Rabson 
11027a0bc89SDoug Rabson /*
11127a0bc89SDoug Rabson  * Map the logical cluster number of a file into a physical disk sector
11227a0bc89SDoug Rabson  * that is filesystem relative.
11327a0bc89SDoug Rabson  *
11427a0bc89SDoug Rabson  * dep	  - address of denode representing the file of interest
11527a0bc89SDoug Rabson  * findcn - file relative cluster whose filesystem relative cluster number
11627a0bc89SDoug Rabson  *	    and/or block number are/is to be found
11727a0bc89SDoug Rabson  * bnp	  - address of where to place the file system relative block number.
11827a0bc89SDoug Rabson  *	    If this pointer is null then don't return this quantity.
11927a0bc89SDoug Rabson  * cnp	  - address of where to place the file system relative cluster number.
12027a0bc89SDoug Rabson  *	    If this pointer is null then don't return this quantity.
12127a0bc89SDoug Rabson  *
12227a0bc89SDoug Rabson  * NOTE: Either bnp or cnp must be non-null.
12327a0bc89SDoug Rabson  * This function has one side effect.  If the requested file relative cluster
12427a0bc89SDoug Rabson  * is beyond the end of file, then the actual number of clusters in the file
12527a0bc89SDoug Rabson  * is returned in *cnp.  This is useful for determining how long a directory is.
12627a0bc89SDoug Rabson  *  If cnp is null, nothing is returned.
12727a0bc89SDoug Rabson  */
12827a0bc89SDoug Rabson int
12927a0bc89SDoug Rabson pcbmap(dep, findcn, bnp, cnp)
13027a0bc89SDoug Rabson 	struct denode *dep;
13127a0bc89SDoug Rabson 	u_long findcn;		/* file relative cluster to get		 */
13227a0bc89SDoug Rabson 	daddr_t *bnp;		/* returned filesys relative blk number	 */
13327a0bc89SDoug Rabson 	u_long *cnp;		/* returned cluster number		 */
13427a0bc89SDoug Rabson {
13527a0bc89SDoug Rabson 	int error;
13627a0bc89SDoug Rabson 	u_long i;
13727a0bc89SDoug Rabson 	u_long cn;
13827a0bc89SDoug Rabson 	u_long prevcn;
13927a0bc89SDoug Rabson 	u_long byteoffset;
14027a0bc89SDoug Rabson 	u_long bn;
14127a0bc89SDoug Rabson 	u_long bo;
14227a0bc89SDoug Rabson 	struct buf *bp = NULL;
14327a0bc89SDoug Rabson 	u_long bp_bn = -1;
14427a0bc89SDoug Rabson 	struct msdosfsmount *pmp = dep->de_pmp;
14527a0bc89SDoug Rabson 	u_long bsize;
14627a0bc89SDoug Rabson 	int fat12 = FAT12(pmp);	/* 12 bit fat	 */
14727a0bc89SDoug Rabson 
14827a0bc89SDoug Rabson 	fc_bmapcalls++;
14927a0bc89SDoug Rabson 
15027a0bc89SDoug Rabson 	/*
15127a0bc89SDoug Rabson 	 * If they don't give us someplace to return a value then don't
15227a0bc89SDoug Rabson 	 * bother doing anything.
15327a0bc89SDoug Rabson 	 */
15427a0bc89SDoug Rabson 	if (bnp == NULL && cnp == NULL)
15527a0bc89SDoug Rabson 		return 0;
15627a0bc89SDoug Rabson 
15727a0bc89SDoug Rabson 	cn = dep->de_StartCluster;
15827a0bc89SDoug Rabson 	/*
15927a0bc89SDoug Rabson 	 * The "file" that makes up the root directory is contiguous,
16027a0bc89SDoug Rabson 	 * permanently allocated, of fixed size, and is not made up of
16127a0bc89SDoug Rabson 	 * clusters.  If the cluster number is beyond the end of the root
16227a0bc89SDoug Rabson 	 * directory, then return the number of clusters in the file.
16327a0bc89SDoug Rabson 	 */
16427a0bc89SDoug Rabson 	if (cn == MSDOSFSROOT) {
16527a0bc89SDoug Rabson 		if (dep->de_Attributes & ATTR_DIRECTORY) {
16682f19691SBruce Evans 			if (findcn * pmp->pm_SectPerClust >= pmp->pm_rootdirsize) {
16727a0bc89SDoug Rabson 				if (cnp)
16827a0bc89SDoug Rabson 					*cnp = pmp->pm_rootdirsize / pmp->pm_SectPerClust;
16927a0bc89SDoug Rabson 				return E2BIG;
17027a0bc89SDoug Rabson 			}
17127a0bc89SDoug Rabson 			if (bnp)
17227a0bc89SDoug Rabson 				*bnp = pmp->pm_rootdirblk + (findcn * pmp->pm_SectPerClust);
17327a0bc89SDoug Rabson 			if (cnp)
17427a0bc89SDoug Rabson 				*cnp = MSDOSFSROOT;
17527a0bc89SDoug Rabson 			return 0;
17627a0bc89SDoug Rabson 		} else {		/* just an empty file */
17727a0bc89SDoug Rabson 			if (cnp)
17827a0bc89SDoug Rabson 				*cnp = 0;
17927a0bc89SDoug Rabson 			return E2BIG;
18027a0bc89SDoug Rabson 		}
18127a0bc89SDoug Rabson 	}
18227a0bc89SDoug Rabson 
18327a0bc89SDoug Rabson 	/*
18427a0bc89SDoug Rabson 	 * Rummage around in the fat cache, maybe we can avoid tromping
18527a0bc89SDoug Rabson 	 * thru every fat entry for the file. And, keep track of how far
18627a0bc89SDoug Rabson 	 * off the cache was from where we wanted to be.
18727a0bc89SDoug Rabson 	 */
18827a0bc89SDoug Rabson 	i = 0;
18927a0bc89SDoug Rabson 	fc_lookup(dep, findcn, &i, &cn);
19027a0bc89SDoug Rabson 	if ((bn = findcn - i) >= LMMAX)
19127a0bc89SDoug Rabson 		fc_largedistance++;
19227a0bc89SDoug Rabson 	else
19327a0bc89SDoug Rabson 		fc_lmdistance[bn]++;
19427a0bc89SDoug Rabson 
19527a0bc89SDoug Rabson 	/*
19627a0bc89SDoug Rabson 	 * Handle all other files or directories the normal way.
19727a0bc89SDoug Rabson 	 */
19827a0bc89SDoug Rabson 	prevcn = 0;
19927a0bc89SDoug Rabson 	for (; i < findcn; i++) {
20027a0bc89SDoug Rabson 		if (MSDOSFSEOF(cn))
20127a0bc89SDoug Rabson 			goto hiteof;
20227a0bc89SDoug Rabson 		byteoffset = FATOFS(pmp, cn);
20327a0bc89SDoug Rabson 		fatblock(pmp, byteoffset, &bn, &bsize, &bo);
20427a0bc89SDoug Rabson 		if (bn != bp_bn) {
20527a0bc89SDoug Rabson 			if (bp)
20627a0bc89SDoug Rabson 				brelse(bp);
20727a0bc89SDoug Rabson 			error = bread(pmp->pm_devvp, bn, bsize, NOCRED, &bp);
20827a0bc89SDoug Rabson 			if (error)
20927a0bc89SDoug Rabson 				return error;
21027a0bc89SDoug Rabson 			bp_bn = bn;
21127a0bc89SDoug Rabson 		}
21227a0bc89SDoug Rabson 		prevcn = cn;
21327a0bc89SDoug Rabson 		cn = getushort(&bp->b_data[bo]);
21427a0bc89SDoug Rabson 		if (fat12) {
21527a0bc89SDoug Rabson 			if (prevcn & 1)
21627a0bc89SDoug Rabson 				cn >>= 4;
21727a0bc89SDoug Rabson 			cn &= 0x0fff;
21827a0bc89SDoug Rabson 			/*
21927a0bc89SDoug Rabson 			 * Force the special cluster numbers in the range
22027a0bc89SDoug Rabson 			 * 0x0ff0-0x0fff to be the same as for 16 bit
22127a0bc89SDoug Rabson 			 * cluster numbers to let the rest of msdosfs think
22227a0bc89SDoug Rabson 			 * it is always dealing with 16 bit fats.
22327a0bc89SDoug Rabson 			 */
22427a0bc89SDoug Rabson 			if ((cn & 0x0ff0) == 0x0ff0)
22527a0bc89SDoug Rabson 				cn |= 0xf000;
22627a0bc89SDoug Rabson 		}
22727a0bc89SDoug Rabson 	}
22827a0bc89SDoug Rabson 
22927a0bc89SDoug Rabson 	if (!MSDOSFSEOF(cn)) {
23027a0bc89SDoug Rabson 		if (bp)
23127a0bc89SDoug Rabson 			brelse(bp);
23227a0bc89SDoug Rabson 		if (bnp)
23327a0bc89SDoug Rabson 			*bnp = cntobn(pmp, cn);
23427a0bc89SDoug Rabson 		if (cnp)
23527a0bc89SDoug Rabson 			*cnp = cn;
23627a0bc89SDoug Rabson 		fc_setcache(dep, FC_LASTMAP, i, cn);
23727a0bc89SDoug Rabson 		return 0;
23827a0bc89SDoug Rabson 	}
23927a0bc89SDoug Rabson 
24027a0bc89SDoug Rabson hiteof:;
24127a0bc89SDoug Rabson 	if (cnp)
24227a0bc89SDoug Rabson 		*cnp = i;
24327a0bc89SDoug Rabson 	if (bp)
24427a0bc89SDoug Rabson 		brelse(bp);
24527a0bc89SDoug Rabson 	/* update last file cluster entry in the fat cache */
24627a0bc89SDoug Rabson 	fc_setcache(dep, FC_LASTFC, i - 1, prevcn);
24727a0bc89SDoug Rabson 	return E2BIG;
24827a0bc89SDoug Rabson }
24927a0bc89SDoug Rabson 
25027a0bc89SDoug Rabson /*
25127a0bc89SDoug Rabson  * Find the closest entry in the fat cache to the cluster we are looking
25227a0bc89SDoug Rabson  * for.
25327a0bc89SDoug Rabson  */
254c3c6d51eSPoul-Henning Kamp void
255c3c6d51eSPoul-Henning Kamp fc_lookup(dep, findcn, frcnp, fsrcnp)
25627a0bc89SDoug Rabson 	struct denode *dep;
25727a0bc89SDoug Rabson 	u_long findcn;
25827a0bc89SDoug Rabson 	u_long *frcnp;
25927a0bc89SDoug Rabson 	u_long *fsrcnp;
26027a0bc89SDoug Rabson {
26127a0bc89SDoug Rabson 	int i;
26227a0bc89SDoug Rabson 	u_long cn;
26327a0bc89SDoug Rabson 	struct fatcache *closest = 0;
26427a0bc89SDoug Rabson 
26527a0bc89SDoug Rabson 	for (i = 0; i < FC_SIZE; i++) {
26627a0bc89SDoug Rabson 		cn = dep->de_fc[i].fc_frcn;
26727a0bc89SDoug Rabson 		if (cn != FCE_EMPTY && cn <= findcn) {
26827a0bc89SDoug Rabson 			if (closest == 0 || cn > closest->fc_frcn)
26927a0bc89SDoug Rabson 				closest = &dep->de_fc[i];
27027a0bc89SDoug Rabson 		}
27127a0bc89SDoug Rabson 	}
27227a0bc89SDoug Rabson 	if (closest) {
27327a0bc89SDoug Rabson 		*frcnp = closest->fc_frcn;
27427a0bc89SDoug Rabson 		*fsrcnp = closest->fc_fsrcn;
27527a0bc89SDoug Rabson 	}
27627a0bc89SDoug Rabson }
27727a0bc89SDoug Rabson 
27827a0bc89SDoug Rabson /*
27927a0bc89SDoug Rabson  * Purge the fat cache in denode dep of all entries relating to file
28027a0bc89SDoug Rabson  * relative cluster frcn and beyond.
28127a0bc89SDoug Rabson  */
28227a0bc89SDoug Rabson void fc_purge(dep, frcn)
28327a0bc89SDoug Rabson 	struct denode *dep;
28427a0bc89SDoug Rabson 	u_int frcn;
28527a0bc89SDoug Rabson {
28627a0bc89SDoug Rabson 	int i;
28727a0bc89SDoug Rabson 	struct fatcache *fcp;
28827a0bc89SDoug Rabson 
28927a0bc89SDoug Rabson 	fcp = dep->de_fc;
29027a0bc89SDoug Rabson 	for (i = 0; i < FC_SIZE; i++, fcp++) {
29127a0bc89SDoug Rabson 		if (fcp->fc_frcn >= frcn)
29227a0bc89SDoug Rabson 			fcp->fc_frcn = FCE_EMPTY;
29327a0bc89SDoug Rabson 	}
29427a0bc89SDoug Rabson }
29527a0bc89SDoug Rabson 
29627a0bc89SDoug Rabson /*
29727a0bc89SDoug Rabson  * Update all copies of the fat. The first copy is updated last.
29827a0bc89SDoug Rabson  *
29927a0bc89SDoug Rabson  * pmp	 - msdosfsmount structure for filesystem to update
30027a0bc89SDoug Rabson  * bp	 - addr of modified fat block
30127a0bc89SDoug Rabson  * fatbn - block number relative to begin of filesystem of the modified fat block.
30227a0bc89SDoug Rabson  */
303a98ca469SPoul-Henning Kamp static void
30427a0bc89SDoug Rabson updatefats(pmp, bp, fatbn)
30527a0bc89SDoug Rabson 	struct msdosfsmount *pmp;
30627a0bc89SDoug Rabson 	struct buf *bp;
30727a0bc89SDoug Rabson 	u_long fatbn;
30827a0bc89SDoug Rabson {
30927a0bc89SDoug Rabson 	int i;
31027a0bc89SDoug Rabson 	struct buf *bpn;
31127a0bc89SDoug Rabson 
31227a0bc89SDoug Rabson #ifdef MSDOSFS_DEBUG
313f0707215SPoul-Henning Kamp 	printf("updatefats(pmp %p, bp %p, fatbn %ld)\n", pmp, bp, fatbn);
31427a0bc89SDoug Rabson #endif
31527a0bc89SDoug Rabson 
31627a0bc89SDoug Rabson 	/*
31727a0bc89SDoug Rabson 	 * Now copy the block(s) of the modified fat to the other copies of
31827a0bc89SDoug Rabson 	 * the fat and write them out.  This is faster than reading in the
31927a0bc89SDoug Rabson 	 * other fats and then writing them back out.  This could tie up
32027a0bc89SDoug Rabson 	 * the fat for quite a while. Preventing others from accessing it.
32127a0bc89SDoug Rabson 	 * To prevent us from going after the fat quite so much we use
32227a0bc89SDoug Rabson 	 * delayed writes, unless they specfied "synchronous" when the
32327a0bc89SDoug Rabson 	 * filesystem was mounted.  If synch is asked for then use
32427a0bc89SDoug Rabson 	 * bwrite()'s and really slow things down.
32527a0bc89SDoug Rabson 	 */
32627a0bc89SDoug Rabson 	for (i = 1; i < pmp->pm_FATs; i++) {
32727a0bc89SDoug Rabson 		fatbn += pmp->pm_FATsecs;
32827a0bc89SDoug Rabson 		/* getblk() never fails */
32927a0bc89SDoug Rabson 		bpn = getblk(pmp->pm_devvp, fatbn, bp->b_bcount, 0, 0);
33027a0bc89SDoug Rabson 		bcopy(bp->b_data, bpn->b_data, bp->b_bcount);
33127a0bc89SDoug Rabson 		if (pmp->pm_waitonfat)
33227a0bc89SDoug Rabson 			bwrite(bpn);
33327a0bc89SDoug Rabson 		else
33427a0bc89SDoug Rabson 			bdwrite(bpn);
33527a0bc89SDoug Rabson 	}
33627a0bc89SDoug Rabson 	/*
33727a0bc89SDoug Rabson 	 * Write out the first fat last.
33827a0bc89SDoug Rabson 	 */
33927a0bc89SDoug Rabson 	if (pmp->pm_waitonfat)
34027a0bc89SDoug Rabson 		bwrite(bp);
34127a0bc89SDoug Rabson 	else
34227a0bc89SDoug Rabson 		bdwrite(bp);
34327a0bc89SDoug Rabson }
34427a0bc89SDoug Rabson 
34527a0bc89SDoug Rabson /*
34627a0bc89SDoug Rabson  * Updating entries in 12 bit fats is a pain in the butt.
34727a0bc89SDoug Rabson  *
34827a0bc89SDoug Rabson  * The following picture shows where nibbles go when moving from a 12 bit
34927a0bc89SDoug Rabson  * cluster number into the appropriate bytes in the FAT.
35027a0bc89SDoug Rabson  *
35127a0bc89SDoug Rabson  *	byte m        byte m+1      byte m+2
35227a0bc89SDoug Rabson  *	+----+----+   +----+----+   +----+----+
35327a0bc89SDoug Rabson  *	|  0    1 |   |  2    3 |   |  4    5 |   FAT bytes
35427a0bc89SDoug Rabson  *	+----+----+   +----+----+   +----+----+
35527a0bc89SDoug Rabson  *
35627a0bc89SDoug Rabson  *	+----+----+----+   +----+----+----+
35727a0bc89SDoug Rabson  *	|  3    0    1 |   |  4    5    2 |
35827a0bc89SDoug Rabson  *	+----+----+----+   +----+----+----+
35927a0bc89SDoug Rabson  *	cluster n  	   cluster n+1
36027a0bc89SDoug Rabson  *
36127a0bc89SDoug Rabson  * Where n is even. m = n + (n >> 2)
36227a0bc89SDoug Rabson  *
36327a0bc89SDoug Rabson  */
36463e4f22aSBruce Evans static inline void
36527a0bc89SDoug Rabson usemap_alloc(pmp, cn)
36627a0bc89SDoug Rabson 	struct msdosfsmount *pmp;
36727a0bc89SDoug Rabson 	u_long cn;
36827a0bc89SDoug Rabson {
36927a0bc89SDoug Rabson 	pmp->pm_inusemap[cn / N_INUSEBITS]
37027a0bc89SDoug Rabson 			 |= 1 << (cn % N_INUSEBITS);
37127a0bc89SDoug Rabson 	pmp->pm_freeclustercount--;
37227a0bc89SDoug Rabson }
37327a0bc89SDoug Rabson 
37463e4f22aSBruce Evans static inline void
37527a0bc89SDoug Rabson usemap_free(pmp, cn)
37627a0bc89SDoug Rabson 	struct msdosfsmount *pmp;
37727a0bc89SDoug Rabson 	u_long cn;
37827a0bc89SDoug Rabson {
37927a0bc89SDoug Rabson 	pmp->pm_freeclustercount++;
38027a0bc89SDoug Rabson 	pmp->pm_inusemap[cn / N_INUSEBITS] &= ~(1 << (cn % N_INUSEBITS));
38127a0bc89SDoug Rabson }
38227a0bc89SDoug Rabson 
38327a0bc89SDoug Rabson int
38427a0bc89SDoug Rabson clusterfree(pmp, cluster, oldcnp)
38527a0bc89SDoug Rabson 	struct msdosfsmount *pmp;
38627a0bc89SDoug Rabson 	u_long cluster;
38727a0bc89SDoug Rabson 	u_long *oldcnp;
38827a0bc89SDoug Rabson {
38927a0bc89SDoug Rabson 	int error;
39027a0bc89SDoug Rabson 	u_long oldcn;
39127a0bc89SDoug Rabson 
39227a0bc89SDoug Rabson 	error = fatentry(FAT_GET_AND_SET, pmp, cluster, &oldcn, MSDOSFSFREE);
39327a0bc89SDoug Rabson 	if (error == 0) {
39427a0bc89SDoug Rabson 		/*
39527a0bc89SDoug Rabson 		 * If the cluster was successfully marked free, then update
39627a0bc89SDoug Rabson 		 * the count of free clusters, and turn off the "allocated"
39727a0bc89SDoug Rabson 		 * bit in the "in use" cluster bit map.
39827a0bc89SDoug Rabson 		 */
39927a0bc89SDoug Rabson 		usemap_free(pmp, cluster);
40027a0bc89SDoug Rabson 		if (oldcnp)
40127a0bc89SDoug Rabson 			*oldcnp = oldcn;
40227a0bc89SDoug Rabson 	}
40327a0bc89SDoug Rabson 	return error;
40427a0bc89SDoug Rabson }
40527a0bc89SDoug Rabson 
40627a0bc89SDoug Rabson /*
40727a0bc89SDoug Rabson  * Get or Set or 'Get and Set' the cluster'th entry in the fat.
40827a0bc89SDoug Rabson  *
40927a0bc89SDoug Rabson  * function	- whether to get or set a fat entry
41027a0bc89SDoug Rabson  * pmp		- address of the msdosfsmount structure for the filesystem
41127a0bc89SDoug Rabson  *		  whose fat is to be manipulated.
41227a0bc89SDoug Rabson  * cn		- which cluster is of interest
41327a0bc89SDoug Rabson  * oldcontents	- address of a word that is to receive the contents of the
41427a0bc89SDoug Rabson  *		  cluster'th entry if this is a get function
41527a0bc89SDoug Rabson  * newcontents	- the new value to be written into the cluster'th element of
41627a0bc89SDoug Rabson  *		  the fat if this is a set function.
41727a0bc89SDoug Rabson  *
41827a0bc89SDoug Rabson  * This function can also be used to free a cluster by setting the fat entry
41927a0bc89SDoug Rabson  * for a cluster to 0.
42027a0bc89SDoug Rabson  *
42127a0bc89SDoug Rabson  * All copies of the fat are updated if this is a set function. NOTE: If
42227a0bc89SDoug Rabson  * fatentry() marks a cluster as free it does not update the inusemap in
42327a0bc89SDoug Rabson  * the msdosfsmount structure. This is left to the caller.
42427a0bc89SDoug Rabson  */
42527a0bc89SDoug Rabson int
42627a0bc89SDoug Rabson fatentry(function, pmp, cn, oldcontents, newcontents)
42727a0bc89SDoug Rabson 	int function;
42827a0bc89SDoug Rabson 	struct msdosfsmount *pmp;
42927a0bc89SDoug Rabson 	u_long cn;
43027a0bc89SDoug Rabson 	u_long *oldcontents;
43127a0bc89SDoug Rabson 	u_long newcontents;
43227a0bc89SDoug Rabson {
43327a0bc89SDoug Rabson 	int error;
43427a0bc89SDoug Rabson 	u_long readcn;
43527a0bc89SDoug Rabson 	u_long bn, bo, bsize, byteoffset;
43627a0bc89SDoug Rabson 	struct buf *bp;
43727a0bc89SDoug Rabson 
43827a0bc89SDoug Rabson 	/*
43927a0bc89SDoug Rabson 	 * printf("fatentry(func %d, pmp %08x, clust %d, oldcon %08x, newcon %d)\n",
44027a0bc89SDoug Rabson 	 *	  function, pmp, cluster, oldcontents, newcontents);
44127a0bc89SDoug Rabson 	 */
44227a0bc89SDoug Rabson 
44327a0bc89SDoug Rabson #ifdef DIAGNOSTIC
44427a0bc89SDoug Rabson 	/*
44527a0bc89SDoug Rabson 	 * Be sure they asked us to do something.
44627a0bc89SDoug Rabson 	 */
44727a0bc89SDoug Rabson 	if ((function & (FAT_SET | FAT_GET)) == 0) {
44827a0bc89SDoug Rabson 		printf("fatentry(): function code doesn't specify get or set\n");
44927a0bc89SDoug Rabson 		return EINVAL;
45027a0bc89SDoug Rabson 	}
45127a0bc89SDoug Rabson 
45227a0bc89SDoug Rabson 	/*
45327a0bc89SDoug Rabson 	 * If they asked us to return a cluster number but didn't tell us
45427a0bc89SDoug Rabson 	 * where to put it, give them an error.
45527a0bc89SDoug Rabson 	 */
45627a0bc89SDoug Rabson 	if ((function & FAT_GET) && oldcontents == NULL) {
45727a0bc89SDoug Rabson 		printf("fatentry(): get function with no place to put result\n");
45827a0bc89SDoug Rabson 		return EINVAL;
45927a0bc89SDoug Rabson 	}
46027a0bc89SDoug Rabson #endif
46127a0bc89SDoug Rabson 
46227a0bc89SDoug Rabson 	/*
46327a0bc89SDoug Rabson 	 * Be sure the requested cluster is in the filesystem.
46427a0bc89SDoug Rabson 	 */
46527a0bc89SDoug Rabson 	if (cn < CLUST_FIRST || cn > pmp->pm_maxcluster)
46627a0bc89SDoug Rabson 		return EINVAL;
46727a0bc89SDoug Rabson 
46827a0bc89SDoug Rabson 	byteoffset = FATOFS(pmp, cn);
46927a0bc89SDoug Rabson 	fatblock(pmp, byteoffset, &bn, &bsize, &bo);
470c3c6d51eSPoul-Henning Kamp 	error = bread(pmp->pm_devvp, bn, bsize, NOCRED, &bp);
471c3c6d51eSPoul-Henning Kamp 	if (error)
47227a0bc89SDoug Rabson 		return error;
47327a0bc89SDoug Rabson 
47427a0bc89SDoug Rabson 	if (function & FAT_GET) {
47527a0bc89SDoug Rabson 		readcn = getushort(&bp->b_data[bo]);
47627a0bc89SDoug Rabson 		if (FAT12(pmp)) {
47727a0bc89SDoug Rabson 			if (cn & 1)
47827a0bc89SDoug Rabson 				readcn >>= 4;
47927a0bc89SDoug Rabson 			readcn &= 0x0fff;
48027a0bc89SDoug Rabson 			/* map certain 12 bit fat entries to 16 bit */
48127a0bc89SDoug Rabson 			if ((readcn & 0x0ff0) == 0x0ff0)
48227a0bc89SDoug Rabson 				readcn |= 0xf000;
48327a0bc89SDoug Rabson 		}
48427a0bc89SDoug Rabson 		*oldcontents = readcn;
48527a0bc89SDoug Rabson 	}
48627a0bc89SDoug Rabson 	if (function & FAT_SET) {
48727a0bc89SDoug Rabson 		if (FAT12(pmp)) {
48827a0bc89SDoug Rabson 			readcn = getushort(&bp->b_data[bo]);
48927a0bc89SDoug Rabson 			if (cn & 1) {
49027a0bc89SDoug Rabson 				readcn &= 0x000f;
49127a0bc89SDoug Rabson 				readcn |= newcontents << 4;
49227a0bc89SDoug Rabson 			} else {
49327a0bc89SDoug Rabson 				readcn &= 0xf000;
49427a0bc89SDoug Rabson 				readcn |= newcontents & 0xfff;
49527a0bc89SDoug Rabson 			}
49627a0bc89SDoug Rabson 			putushort(&bp->b_data[bo], readcn);
49727a0bc89SDoug Rabson 		} else
49827a0bc89SDoug Rabson 			putushort(&bp->b_data[bo], newcontents);
49927a0bc89SDoug Rabson 		updatefats(pmp, bp, bn);
50027a0bc89SDoug Rabson 		bp = NULL;
50127a0bc89SDoug Rabson 		pmp->pm_fmod = 1;
50227a0bc89SDoug Rabson 	}
50327a0bc89SDoug Rabson 	if (bp)
50427a0bc89SDoug Rabson 		brelse(bp);
50527a0bc89SDoug Rabson 	return 0;
50627a0bc89SDoug Rabson }
50727a0bc89SDoug Rabson 
50827a0bc89SDoug Rabson /*
50927a0bc89SDoug Rabson  * Update a contiguous cluster chain
51027a0bc89SDoug Rabson  *
51127a0bc89SDoug Rabson  * pmp	    - mount point
51227a0bc89SDoug Rabson  * start    - first cluster of chain
51327a0bc89SDoug Rabson  * count    - number of clusters in chain
51427a0bc89SDoug Rabson  * fillwith - what to write into fat entry of last cluster
51527a0bc89SDoug Rabson  */
51627a0bc89SDoug Rabson static int
51727a0bc89SDoug Rabson fatchain(pmp, start, count, fillwith)
51827a0bc89SDoug Rabson 	struct msdosfsmount *pmp;
51927a0bc89SDoug Rabson 	u_long start;
52027a0bc89SDoug Rabson 	u_long count;
52127a0bc89SDoug Rabson 	u_long fillwith;
52227a0bc89SDoug Rabson {
52327a0bc89SDoug Rabson 	int error;
52427a0bc89SDoug Rabson 	u_long bn, bo, bsize, byteoffset, readcn, newc;
52527a0bc89SDoug Rabson 	struct buf *bp;
52627a0bc89SDoug Rabson 
52727a0bc89SDoug Rabson #ifdef MSDOSFS_DEBUG
528f0707215SPoul-Henning Kamp 	printf("fatchain(pmp %p, start %ld, count %ld, fillwith %ld)\n",
52927a0bc89SDoug Rabson 	       pmp, start, count, fillwith);
53027a0bc89SDoug Rabson #endif
53127a0bc89SDoug Rabson 	/*
53227a0bc89SDoug Rabson 	 * Be sure the clusters are in the filesystem.
53327a0bc89SDoug Rabson 	 */
53427a0bc89SDoug Rabson 	if (start < CLUST_FIRST || start + count - 1 > pmp->pm_maxcluster)
53527a0bc89SDoug Rabson 		return EINVAL;
53627a0bc89SDoug Rabson 
53727a0bc89SDoug Rabson 	while (count > 0) {
53827a0bc89SDoug Rabson 		byteoffset = FATOFS(pmp, start);
53927a0bc89SDoug Rabson 		fatblock(pmp, byteoffset, &bn, &bsize, &bo);
540c3c6d51eSPoul-Henning Kamp 		error = bread(pmp->pm_devvp, bn, bsize, NOCRED, &bp);
541c3c6d51eSPoul-Henning Kamp 		if (error)
54227a0bc89SDoug Rabson 			return error;
54327a0bc89SDoug Rabson 		while (count > 0) {
54427a0bc89SDoug Rabson 			start++;
54527a0bc89SDoug Rabson 			newc = --count > 0 ? start : fillwith;
54627a0bc89SDoug Rabson 			if (FAT12(pmp)) {
54727a0bc89SDoug Rabson 				readcn = getushort(&bp->b_data[bo]);
54827a0bc89SDoug Rabson 				if (start & 1) {
54927a0bc89SDoug Rabson 					readcn &= 0xf000;
55027a0bc89SDoug Rabson 					readcn |= newc & 0xfff;
55127a0bc89SDoug Rabson 				} else {
55227a0bc89SDoug Rabson 					readcn &= 0x000f;
55327a0bc89SDoug Rabson 					readcn |= newc << 4;
55427a0bc89SDoug Rabson 				}
55527a0bc89SDoug Rabson 				putushort(&bp->b_data[bo], readcn);
55627a0bc89SDoug Rabson 				bo++;
55727a0bc89SDoug Rabson 				if (!(start & 1))
55827a0bc89SDoug Rabson 					bo++;
55927a0bc89SDoug Rabson 			} else {
56027a0bc89SDoug Rabson 				putushort(&bp->b_data[bo], newc);
56127a0bc89SDoug Rabson 				bo += 2;
56227a0bc89SDoug Rabson 			}
56327a0bc89SDoug Rabson 			if (bo >= bsize)
56427a0bc89SDoug Rabson 				break;
56527a0bc89SDoug Rabson 		}
56627a0bc89SDoug Rabson 		updatefats(pmp, bp, bn);
56727a0bc89SDoug Rabson 	}
56827a0bc89SDoug Rabson 	pmp->pm_fmod = 1;
56927a0bc89SDoug Rabson 	return 0;
57027a0bc89SDoug Rabson }
57127a0bc89SDoug Rabson 
57227a0bc89SDoug Rabson /*
57327a0bc89SDoug Rabson  * Check the length of a free cluster chain starting at start.
57427a0bc89SDoug Rabson  *
57527a0bc89SDoug Rabson  * pmp	 - mount point
57627a0bc89SDoug Rabson  * start - start of chain
57727a0bc89SDoug Rabson  * count - maximum interesting length
57827a0bc89SDoug Rabson  */
57927a0bc89SDoug Rabson int
58027a0bc89SDoug Rabson chainlength(pmp, start, count)
58127a0bc89SDoug Rabson 	struct msdosfsmount *pmp;
58227a0bc89SDoug Rabson 	u_long start;
58327a0bc89SDoug Rabson 	u_long count;
58427a0bc89SDoug Rabson {
58527a0bc89SDoug Rabson 	u_long idx, max_idx;
58627a0bc89SDoug Rabson 	u_int map;
58727a0bc89SDoug Rabson 	u_long len;
58827a0bc89SDoug Rabson 
58927a0bc89SDoug Rabson 	max_idx = pmp->pm_maxcluster / N_INUSEBITS;
59027a0bc89SDoug Rabson 	idx = start / N_INUSEBITS;
59127a0bc89SDoug Rabson 	start %= N_INUSEBITS;
59227a0bc89SDoug Rabson 	map = pmp->pm_inusemap[idx];
59327a0bc89SDoug Rabson 	map &= ~((1 << start) - 1);
59427a0bc89SDoug Rabson 	if (map) {
59527a0bc89SDoug Rabson 		len = ffs(map) - 1 - start;
59627a0bc89SDoug Rabson 		return len > count ? count : len;
59727a0bc89SDoug Rabson 	}
59827a0bc89SDoug Rabson 	len = N_INUSEBITS - start;
59927a0bc89SDoug Rabson 	if (len >= count)
60027a0bc89SDoug Rabson 		return count;
60127a0bc89SDoug Rabson 	while (++idx <= max_idx) {
60227a0bc89SDoug Rabson 		if (len >= count)
60327a0bc89SDoug Rabson 			break;
604c3c6d51eSPoul-Henning Kamp 		map = pmp->pm_inusemap[idx];
605c3c6d51eSPoul-Henning Kamp 		if (map) {
60627a0bc89SDoug Rabson 			len +=  ffs(map) - 1;
60727a0bc89SDoug Rabson 			break;
60827a0bc89SDoug Rabson 		}
60927a0bc89SDoug Rabson 		len += N_INUSEBITS;
61027a0bc89SDoug Rabson 	}
61127a0bc89SDoug Rabson 	return len > count ? count : len;
61227a0bc89SDoug Rabson }
61327a0bc89SDoug Rabson 
61427a0bc89SDoug Rabson /*
61527a0bc89SDoug Rabson  * Allocate contigous free clusters.
61627a0bc89SDoug Rabson  *
61727a0bc89SDoug Rabson  * pmp	      - mount point.
61827a0bc89SDoug Rabson  * start      - start of cluster chain.
61927a0bc89SDoug Rabson  * count      - number of clusters to allocate.
62027a0bc89SDoug Rabson  * fillwith   - put this value into the fat entry for the
62127a0bc89SDoug Rabson  *		last allocated cluster.
62227a0bc89SDoug Rabson  * retcluster - put the first allocated cluster's number here.
62327a0bc89SDoug Rabson  * got	      - how many clusters were actually allocated.
62427a0bc89SDoug Rabson  */
62527a0bc89SDoug Rabson int
62627a0bc89SDoug Rabson chainalloc(pmp, start, count, fillwith, retcluster, got)
62727a0bc89SDoug Rabson 	struct msdosfsmount *pmp;
62827a0bc89SDoug Rabson 	u_long start;
62927a0bc89SDoug Rabson 	u_long count;
63027a0bc89SDoug Rabson 	u_long fillwith;
63127a0bc89SDoug Rabson 	u_long *retcluster;
63227a0bc89SDoug Rabson 	u_long *got;
63327a0bc89SDoug Rabson {
63427a0bc89SDoug Rabson 	int error;
63527a0bc89SDoug Rabson 
63627a0bc89SDoug Rabson 	error = fatchain(pmp, start, count, fillwith);
63727a0bc89SDoug Rabson 	if (error == 0) {
63827a0bc89SDoug Rabson #ifdef MSDOSFS_DEBUG
639f0707215SPoul-Henning Kamp 		printf("clusteralloc(): allocated cluster chain at %ld (%ld clusters)\n",
64027a0bc89SDoug Rabson 		       start, count);
64127a0bc89SDoug Rabson #endif
64227a0bc89SDoug Rabson 		if (retcluster)
64327a0bc89SDoug Rabson 			*retcluster = start;
64427a0bc89SDoug Rabson 		if (got)
64527a0bc89SDoug Rabson 			*got = count;
64627a0bc89SDoug Rabson 		while (count-- > 0)
64727a0bc89SDoug Rabson 			usemap_alloc(pmp, start++);
64827a0bc89SDoug Rabson 	}
64927a0bc89SDoug Rabson 	return error;
65027a0bc89SDoug Rabson }
65127a0bc89SDoug Rabson 
65227a0bc89SDoug Rabson /*
65327a0bc89SDoug Rabson  * Allocate contiguous free clusters.
65427a0bc89SDoug Rabson  *
65527a0bc89SDoug Rabson  * pmp	      - mount point.
65627a0bc89SDoug Rabson  * start      - preferred start of cluster chain.
65727a0bc89SDoug Rabson  * count      - number of clusters requested.
65827a0bc89SDoug Rabson  * fillwith   - put this value into the fat entry for the
65927a0bc89SDoug Rabson  *		last allocated cluster.
66027a0bc89SDoug Rabson  * retcluster - put the first allocated cluster's number here.
66127a0bc89SDoug Rabson  * got	      - how many clusters were actually allocated.
66227a0bc89SDoug Rabson  */
66327a0bc89SDoug Rabson int
66427a0bc89SDoug Rabson clusteralloc(pmp, start, count, fillwith, retcluster, got)
66527a0bc89SDoug Rabson 	struct msdosfsmount *pmp;
66627a0bc89SDoug Rabson 	u_long start;
66727a0bc89SDoug Rabson 	u_long count;
66827a0bc89SDoug Rabson 	u_long fillwith;
66927a0bc89SDoug Rabson 	u_long *retcluster;
67027a0bc89SDoug Rabson 	u_long *got;
67127a0bc89SDoug Rabson {
67227a0bc89SDoug Rabson 	u_long idx;
67327a0bc89SDoug Rabson 	u_long len, newst, foundcn, foundl, cn, l;
67427a0bc89SDoug Rabson 	u_int map;
67527a0bc89SDoug Rabson 
67627a0bc89SDoug Rabson #ifdef MSDOSFS_DEBUG
67727a0bc89SDoug Rabson 	printf("clusteralloc(): find %d clusters\n",count);
67827a0bc89SDoug Rabson #endif
67927a0bc89SDoug Rabson 	if (start) {
68027a0bc89SDoug Rabson 		if ((len = chainlength(pmp, start, count)) >= count)
68127a0bc89SDoug Rabson 			return chainalloc(pmp, start, count, fillwith, retcluster, got);
68227a0bc89SDoug Rabson 	} else {
68327a0bc89SDoug Rabson 		/*
68427a0bc89SDoug Rabson 		 * This is a new file, initialize start
68527a0bc89SDoug Rabson 		 */
68627a0bc89SDoug Rabson 		struct timeval tv;
68727a0bc89SDoug Rabson 
68827a0bc89SDoug Rabson 		microtime(&tv);
68927a0bc89SDoug Rabson 		start = (tv.tv_usec >> 10)|tv.tv_usec;
69027a0bc89SDoug Rabson 		len = 0;
69127a0bc89SDoug Rabson 	}
69227a0bc89SDoug Rabson 
69327a0bc89SDoug Rabson 	/*
69427a0bc89SDoug Rabson 	 * Start at a (pseudo) random place to maximize cluster runs
69527a0bc89SDoug Rabson 	 * under multiple writers.
69627a0bc89SDoug Rabson 	 */
69727a0bc89SDoug Rabson 	foundcn = newst = (start * 1103515245 + 12345) % (pmp->pm_maxcluster + 1);
69827a0bc89SDoug Rabson 	foundl = 0;
69927a0bc89SDoug Rabson 
70027a0bc89SDoug Rabson 	for (cn = newst; cn <= pmp->pm_maxcluster;) {
70127a0bc89SDoug Rabson 		idx = cn / N_INUSEBITS;
70227a0bc89SDoug Rabson 		map = pmp->pm_inusemap[idx];
70327a0bc89SDoug Rabson 		map |= (1 << (cn % N_INUSEBITS)) - 1;
70427a0bc89SDoug Rabson 		if (map != (u_int)-1) {
70527a0bc89SDoug Rabson 			cn = idx * N_INUSEBITS + ffs(map^(u_int)-1) - 1;
70627a0bc89SDoug Rabson 			if ((l = chainlength(pmp, cn, count)) >= count)
70727a0bc89SDoug Rabson 				return chainalloc(pmp, cn, count, fillwith, retcluster, got);
70827a0bc89SDoug Rabson 			if (l > foundl) {
70927a0bc89SDoug Rabson 				foundcn = cn;
71027a0bc89SDoug Rabson 				foundl = l;
71127a0bc89SDoug Rabson 			}
71227a0bc89SDoug Rabson 			cn += l + 1;
71327a0bc89SDoug Rabson 			continue;
71427a0bc89SDoug Rabson 		}
71527a0bc89SDoug Rabson 		cn += N_INUSEBITS - cn % N_INUSEBITS;
71627a0bc89SDoug Rabson 	}
71727a0bc89SDoug Rabson 	for (cn = 0; cn < newst;) {
71827a0bc89SDoug Rabson 		idx = cn / N_INUSEBITS;
71927a0bc89SDoug Rabson 		map = pmp->pm_inusemap[idx];
72027a0bc89SDoug Rabson 		map |= (1 << (cn % N_INUSEBITS)) - 1;
72127a0bc89SDoug Rabson 		if (map != (u_int)-1) {
72227a0bc89SDoug Rabson 			cn = idx * N_INUSEBITS + ffs(map^(u_int)-1) - 1;
72327a0bc89SDoug Rabson 			if ((l = chainlength(pmp, cn, count)) >= count)
72427a0bc89SDoug Rabson 				return chainalloc(pmp, cn, count, fillwith, retcluster, got);
72527a0bc89SDoug Rabson 			if (l > foundl) {
72627a0bc89SDoug Rabson 				foundcn = cn;
72727a0bc89SDoug Rabson 				foundl = l;
72827a0bc89SDoug Rabson 			}
72927a0bc89SDoug Rabson 			cn += l + 1;
73027a0bc89SDoug Rabson 			continue;
73127a0bc89SDoug Rabson 		}
73227a0bc89SDoug Rabson 		cn += N_INUSEBITS - cn % N_INUSEBITS;
73327a0bc89SDoug Rabson 	}
73427a0bc89SDoug Rabson 
73527a0bc89SDoug Rabson 	if (!foundl)
73627a0bc89SDoug Rabson 		return ENOSPC;
73727a0bc89SDoug Rabson 
73827a0bc89SDoug Rabson 	if (len)
73927a0bc89SDoug Rabson 		return chainalloc(pmp, start, len, fillwith, retcluster, got);
74027a0bc89SDoug Rabson 	else
74127a0bc89SDoug Rabson 		return chainalloc(pmp, foundcn, foundl, fillwith, retcluster, got);
74227a0bc89SDoug Rabson }
74327a0bc89SDoug Rabson 
74427a0bc89SDoug Rabson 
74527a0bc89SDoug Rabson /*
74627a0bc89SDoug Rabson  * Free a chain of clusters.
74727a0bc89SDoug Rabson  *
74827a0bc89SDoug Rabson  * pmp		- address of the msdosfs mount structure for the filesystem
74927a0bc89SDoug Rabson  *		  containing the cluster chain to be freed.
75027a0bc89SDoug Rabson  * startcluster - number of the 1st cluster in the chain of clusters to be
75127a0bc89SDoug Rabson  *		  freed.
75227a0bc89SDoug Rabson  */
75327a0bc89SDoug Rabson int
75427a0bc89SDoug Rabson freeclusterchain(pmp, cluster)
75527a0bc89SDoug Rabson 	struct msdosfsmount *pmp;
75627a0bc89SDoug Rabson 	u_long cluster;
75727a0bc89SDoug Rabson {
75827a0bc89SDoug Rabson 	int error = 0;
75927a0bc89SDoug Rabson 	struct buf *bp = NULL;
76027a0bc89SDoug Rabson 	u_long bn, bo, bsize, byteoffset;
76127a0bc89SDoug Rabson 	u_long readcn, lbn = -1;
76227a0bc89SDoug Rabson 
76327a0bc89SDoug Rabson 	while (cluster >= CLUST_FIRST && cluster <= pmp->pm_maxcluster) {
76427a0bc89SDoug Rabson 		byteoffset = FATOFS(pmp, cluster);
76527a0bc89SDoug Rabson 		fatblock(pmp, byteoffset, &bn, &bsize, &bo);
76627a0bc89SDoug Rabson 		if (lbn != bn) {
76727a0bc89SDoug Rabson 			if (bp)
76824d4540cSBruce Evans 				updatefats(pmp, bp, lbn);
769c3c6d51eSPoul-Henning Kamp 			error = bread(pmp->pm_devvp, bn, bsize, NOCRED, &bp);
770c3c6d51eSPoul-Henning Kamp 			if (error)
77127a0bc89SDoug Rabson 				return error;
77227a0bc89SDoug Rabson 			lbn = bn;
77327a0bc89SDoug Rabson 		}
77427a0bc89SDoug Rabson 		usemap_free(pmp, cluster);
77527a0bc89SDoug Rabson 		readcn = getushort(&bp->b_data[bo]);
77627a0bc89SDoug Rabson 		if (FAT12(pmp)) {
77727a0bc89SDoug Rabson 			if (cluster & 1) {
77827a0bc89SDoug Rabson 				cluster = readcn >> 4;
77927a0bc89SDoug Rabson 				readcn &= 0x000f;
78027a0bc89SDoug Rabson 				readcn |= MSDOSFSFREE << 4;
78127a0bc89SDoug Rabson 			} else {
78227a0bc89SDoug Rabson 				cluster = readcn;
78327a0bc89SDoug Rabson 				readcn &= 0xf000;
78427a0bc89SDoug Rabson 				readcn |= MSDOSFSFREE & 0xfff;
78527a0bc89SDoug Rabson 			}
78627a0bc89SDoug Rabson 			putushort(&bp->b_data[bo], readcn);
78727a0bc89SDoug Rabson 			cluster &= 0x0fff;
78827a0bc89SDoug Rabson 			if ((cluster&0x0ff0) == 0x0ff0)
78927a0bc89SDoug Rabson 				cluster |= 0xf000;
79027a0bc89SDoug Rabson 		} else {
79127a0bc89SDoug Rabson 			cluster = readcn;
79227a0bc89SDoug Rabson 			putushort(&bp->b_data[bo], MSDOSFSFREE);
79327a0bc89SDoug Rabson 		}
79427a0bc89SDoug Rabson 	}
79527a0bc89SDoug Rabson 	if (bp)
79627a0bc89SDoug Rabson 		updatefats(pmp, bp, bn);
79727a0bc89SDoug Rabson 	return error;
79827a0bc89SDoug Rabson }
79927a0bc89SDoug Rabson 
80027a0bc89SDoug Rabson /*
80127a0bc89SDoug Rabson  * Read in fat blocks looking for free clusters. For every free cluster
80227a0bc89SDoug Rabson  * found turn off its corresponding bit in the pm_inusemap.
80327a0bc89SDoug Rabson  */
80427a0bc89SDoug Rabson int
80527a0bc89SDoug Rabson fillinusemap(pmp)
80627a0bc89SDoug Rabson 	struct msdosfsmount *pmp;
80727a0bc89SDoug Rabson {
80827a0bc89SDoug Rabson 	struct buf *bp = NULL;
80927a0bc89SDoug Rabson 	u_long cn, readcn;
81027a0bc89SDoug Rabson 	int error;
81127a0bc89SDoug Rabson 	int fat12 = FAT12(pmp);
81227a0bc89SDoug Rabson 	u_long bn, bo, bsize, byteoffset;
81327a0bc89SDoug Rabson 
81427a0bc89SDoug Rabson 	/*
81527a0bc89SDoug Rabson 	 * Mark all clusters in use, we mark the free ones in the fat scan
81627a0bc89SDoug Rabson 	 * loop further down.
81727a0bc89SDoug Rabson 	 */
81827a0bc89SDoug Rabson 	for (cn = 0; cn < (pmp->pm_maxcluster + N_INUSEBITS) / N_INUSEBITS; cn++)
81927a0bc89SDoug Rabson 		pmp->pm_inusemap[cn] = (u_int)-1;
82027a0bc89SDoug Rabson 
82127a0bc89SDoug Rabson 	/*
82227a0bc89SDoug Rabson 	 * Figure how many free clusters are in the filesystem by ripping
82327a0bc89SDoug Rabson 	 * through the fat counting the number of entries whose content is
82427a0bc89SDoug Rabson 	 * zero.  These represent free clusters.
82527a0bc89SDoug Rabson 	 */
82627a0bc89SDoug Rabson 	pmp->pm_freeclustercount = 0;
82727a0bc89SDoug Rabson 	for (cn = CLUST_FIRST; cn <= pmp->pm_maxcluster; cn++) {
82827a0bc89SDoug Rabson 		byteoffset = FATOFS(pmp, cn);
82927a0bc89SDoug Rabson 		bo = byteoffset % pmp->pm_fatblocksize;
83027a0bc89SDoug Rabson 		if (!bo || !bp) {
83127a0bc89SDoug Rabson 			/* Read new FAT block */
83227a0bc89SDoug Rabson 			if (bp)
83327a0bc89SDoug Rabson 				brelse(bp);
83427a0bc89SDoug Rabson 			fatblock(pmp, byteoffset, &bn, &bsize, NULL);
83527a0bc89SDoug Rabson 			error = bread(pmp->pm_devvp, bn, bsize, NOCRED, &bp);
83627a0bc89SDoug Rabson 			if (error)
83727a0bc89SDoug Rabson 				return error;
83827a0bc89SDoug Rabson 		}
83927a0bc89SDoug Rabson 		readcn = getushort(&bp->b_data[bo]);
84027a0bc89SDoug Rabson 		if (fat12) {
84127a0bc89SDoug Rabson 			if (cn & 1)
84227a0bc89SDoug Rabson 				readcn >>= 4;
84327a0bc89SDoug Rabson 			readcn &= 0x0fff;
84427a0bc89SDoug Rabson 		}
84527a0bc89SDoug Rabson 
84627a0bc89SDoug Rabson 		if (readcn == 0)
84727a0bc89SDoug Rabson 			usemap_free(pmp, cn);
84827a0bc89SDoug Rabson 	}
84927a0bc89SDoug Rabson 	brelse(bp);
85027a0bc89SDoug Rabson 	return 0;
85127a0bc89SDoug Rabson }
85227a0bc89SDoug Rabson 
85327a0bc89SDoug Rabson /*
85427a0bc89SDoug Rabson  * Allocate a new cluster and chain it onto the end of the file.
85527a0bc89SDoug Rabson  *
85627a0bc89SDoug Rabson  * dep	 - the file to extend
85727a0bc89SDoug Rabson  * count - number of clusters to allocate
85827a0bc89SDoug Rabson  * bpp	 - where to return the address of the buf header for the first new
85927a0bc89SDoug Rabson  *	   file block
86027a0bc89SDoug Rabson  * ncp	 - where to put cluster number of the first newly allocated cluster
86127a0bc89SDoug Rabson  *	   If this pointer is 0, do not return the cluster number.
86227a0bc89SDoug Rabson  * flags - see fat.h
86327a0bc89SDoug Rabson  *
86427a0bc89SDoug Rabson  * NOTE: This function is not responsible for turning on the DE_UPDATE bit of
86527a0bc89SDoug Rabson  * the de_flag field of the denode and it does not change the de_FileSize
86627a0bc89SDoug Rabson  * field.  This is left for the caller to do.
86727a0bc89SDoug Rabson  */
86827a0bc89SDoug Rabson int
86927a0bc89SDoug Rabson extendfile(dep, count, bpp, ncp, flags)
87027a0bc89SDoug Rabson 	struct denode *dep;
87127a0bc89SDoug Rabson 	u_long count;
87227a0bc89SDoug Rabson 	struct buf **bpp;
87327a0bc89SDoug Rabson 	u_long *ncp;
87427a0bc89SDoug Rabson 	int flags;
87527a0bc89SDoug Rabson {
87627a0bc89SDoug Rabson 	int error = 0;
87727a0bc89SDoug Rabson 	u_long frcn;
87827a0bc89SDoug Rabson 	u_long cn, got;
87927a0bc89SDoug Rabson 	struct msdosfsmount *pmp = dep->de_pmp;
88027a0bc89SDoug Rabson 	struct buf *bp;
88127a0bc89SDoug Rabson 
88227a0bc89SDoug Rabson 	/*
88327a0bc89SDoug Rabson 	 * Don't try to extend the root directory
88427a0bc89SDoug Rabson 	 */
88527a0bc89SDoug Rabson 	if (DETOV(dep)->v_flag & VROOT) {
88627a0bc89SDoug Rabson 		printf("extendfile(): attempt to extend root directory\n");
88727a0bc89SDoug Rabson 		return ENOSPC;
88827a0bc89SDoug Rabson 	}
88927a0bc89SDoug Rabson 
89027a0bc89SDoug Rabson 	/*
89127a0bc89SDoug Rabson 	 * If the "file's last cluster" cache entry is empty, and the file
89227a0bc89SDoug Rabson 	 * is not empty, then fill the cache entry by calling pcbmap().
89327a0bc89SDoug Rabson 	 */
89427a0bc89SDoug Rabson 	fc_fileextends++;
89527a0bc89SDoug Rabson 	if (dep->de_fc[FC_LASTFC].fc_frcn == FCE_EMPTY &&
89627a0bc89SDoug Rabson 	    dep->de_StartCluster != 0) {
89727a0bc89SDoug Rabson 		fc_lfcempty++;
89827a0bc89SDoug Rabson 		error = pcbmap(dep, 0xffff, 0, &cn);
89927a0bc89SDoug Rabson 		/* we expect it to return E2BIG */
90027a0bc89SDoug Rabson 		if (error != E2BIG)
90127a0bc89SDoug Rabson 			return error;
90227a0bc89SDoug Rabson 		error = 0;
90327a0bc89SDoug Rabson 	}
90427a0bc89SDoug Rabson 
90527a0bc89SDoug Rabson 	while (count > 0) {
90627a0bc89SDoug Rabson 		/*
90727a0bc89SDoug Rabson 		 * Allocate a new cluster chain and cat onto the end of the file.
90827a0bc89SDoug Rabson 		 * If the file is empty we make de_StartCluster point to the new
90927a0bc89SDoug Rabson 		 * block.  Note that de_StartCluster being 0 is sufficient to be
91027a0bc89SDoug Rabson 		 * sure the file is empty since we exclude attempts to extend the
91127a0bc89SDoug Rabson 		 * root directory above, and the root dir is the only file with a
91227a0bc89SDoug Rabson 		 * startcluster of 0 that has blocks allocated (sort of).
91327a0bc89SDoug Rabson 		 */
91427a0bc89SDoug Rabson 		if (dep->de_StartCluster == 0)
91527a0bc89SDoug Rabson 			cn = 0;
91627a0bc89SDoug Rabson 		else
91727a0bc89SDoug Rabson 			cn = dep->de_fc[FC_LASTFC].fc_fsrcn + 1;
918c3c6d51eSPoul-Henning Kamp 		error = clusteralloc(pmp, cn, count, CLUST_EOFE, &cn, &got);
919c3c6d51eSPoul-Henning Kamp 		if (error)
92027a0bc89SDoug Rabson 			return error;
92127a0bc89SDoug Rabson 
92227a0bc89SDoug Rabson 		count -= got;
92327a0bc89SDoug Rabson 
92427a0bc89SDoug Rabson 		/*
92527a0bc89SDoug Rabson 		 * Give them the filesystem relative cluster number if they want
92627a0bc89SDoug Rabson 		 * it.
92727a0bc89SDoug Rabson 		 */
92827a0bc89SDoug Rabson 		if (ncp) {
92927a0bc89SDoug Rabson 			*ncp = cn;
93027a0bc89SDoug Rabson 			ncp = NULL;
93127a0bc89SDoug Rabson 		}
93227a0bc89SDoug Rabson 
93327a0bc89SDoug Rabson 		if (dep->de_StartCluster == 0) {
93427a0bc89SDoug Rabson 			dep->de_StartCluster = cn;
93527a0bc89SDoug Rabson 			frcn = 0;
93627a0bc89SDoug Rabson 		} else {
93727a0bc89SDoug Rabson 			error = fatentry(FAT_SET, pmp, dep->de_fc[FC_LASTFC].fc_fsrcn,
93827a0bc89SDoug Rabson 					 0, cn);
93927a0bc89SDoug Rabson 			if (error) {
94027a0bc89SDoug Rabson 				clusterfree(pmp, cn, NULL);
94127a0bc89SDoug Rabson 				return error;
94227a0bc89SDoug Rabson 			}
94327a0bc89SDoug Rabson 
94427a0bc89SDoug Rabson 			frcn = dep->de_fc[FC_LASTFC].fc_frcn + 1;
94527a0bc89SDoug Rabson 		}
94627a0bc89SDoug Rabson 
94727a0bc89SDoug Rabson 		/*
94827a0bc89SDoug Rabson 		 * Update the "last cluster of the file" entry in the denode's fat
94927a0bc89SDoug Rabson 		 * cache.
95027a0bc89SDoug Rabson 		 */
95127a0bc89SDoug Rabson 		fc_setcache(dep, FC_LASTFC, frcn + got - 1, cn + got - 1);
95227a0bc89SDoug Rabson 
95327a0bc89SDoug Rabson 		if (flags & DE_CLEAR) {
95427a0bc89SDoug Rabson 			while (got-- > 0) {
95527a0bc89SDoug Rabson 				/*
95627a0bc89SDoug Rabson 				 * Get the buf header for the new block of the file.
95727a0bc89SDoug Rabson 				 */
95827a0bc89SDoug Rabson 				if (dep->de_Attributes & ATTR_DIRECTORY)
95927a0bc89SDoug Rabson 					bp = getblk(pmp->pm_devvp, cntobn(pmp, cn++),
96027a0bc89SDoug Rabson 						    pmp->pm_bpcluster, 0, 0);
96127a0bc89SDoug Rabson 				else {
96227a0bc89SDoug Rabson 					bp = getblk(DETOV(dep), frcn++, pmp->pm_bpcluster, 0, 0);
96327a0bc89SDoug Rabson 					/*
96427a0bc89SDoug Rabson 					 * Do the bmap now, as in msdosfs_write
96527a0bc89SDoug Rabson 					 */
96627a0bc89SDoug Rabson 					if (pcbmap(dep, bp->b_lblkno, &bp->b_blkno, 0))
96727a0bc89SDoug Rabson 						bp->b_blkno = -1;
96827a0bc89SDoug Rabson 					if (bp->b_blkno == -1)
96927a0bc89SDoug Rabson 						panic("extendfile: pcbmap");
97027a0bc89SDoug Rabson 				}
97127a0bc89SDoug Rabson 				clrbuf(bp);
97227a0bc89SDoug Rabson 				if (bpp) {
97327a0bc89SDoug Rabson 					*bpp = bp;
97427a0bc89SDoug Rabson 					bpp = NULL;
97527a0bc89SDoug Rabson 				} else {
97627a0bc89SDoug Rabson 					bp->b_flags |= B_AGE;
97727a0bc89SDoug Rabson 					bawrite(bp);
97827a0bc89SDoug Rabson 				}
97927a0bc89SDoug Rabson 			}
98027a0bc89SDoug Rabson 		}
98127a0bc89SDoug Rabson 	}
98227a0bc89SDoug Rabson 
98327a0bc89SDoug Rabson 	return 0;
98427a0bc89SDoug Rabson }
985