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