xref: /freebsd/sys/fs/msdosfs/msdosfs_fat.c (revision d167cf6f3a474d20001eddfb1d7c672407ef2630)
1c3aac50fSPeter Wemm /* $FreeBSD$ */
2952a6212SJordan K. Hubbard /*	$NetBSD: msdosfs_fat.c,v 1.28 1997/11/17 15:36:49 ws Exp $	*/
327a0bc89SDoug Rabson 
427a0bc89SDoug Rabson /*-
5952a6212SJordan K. Hubbard  * Copyright (C) 1994, 1995, 1997 Wolfgang Solfrank.
6952a6212SJordan K. Hubbard  * Copyright (C) 1994, 1995, 1997 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  */
35d167cf6fSWarner Losh /*-
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>
569626b608SPoul-Henning Kamp #include <sys/bio.h>
5727a0bc89SDoug Rabson #include <sys/buf.h>
5827a0bc89SDoug Rabson #include <sys/mount.h>		/* to define statfs structure */
5927a0bc89SDoug Rabson #include <sys/vnode.h>		/* to define vattr structure */
6027a0bc89SDoug Rabson 
6127a0bc89SDoug Rabson /*
6227a0bc89SDoug Rabson  * msdosfs include files.
6327a0bc89SDoug Rabson  */
641166fb51SRuslan Ermilov #include <fs/msdosfs/bpb.h>
651166fb51SRuslan Ermilov #include <fs/msdosfs/msdosfsmount.h>
661166fb51SRuslan Ermilov #include <fs/msdosfs/direntry.h>
671166fb51SRuslan Ermilov #include <fs/msdosfs/denode.h>
681166fb51SRuslan Ermilov #include <fs/msdosfs/fat.h>
6927a0bc89SDoug Rabson 
7027a0bc89SDoug Rabson /*
7127a0bc89SDoug Rabson  * Fat cache stats.
7227a0bc89SDoug Rabson  */
73303b270bSEivind Eklund static int fc_fileextends;	/* # of file extends			 */
74303b270bSEivind Eklund static int fc_lfcempty;		/* # of time last file cluster cache entry
7527a0bc89SDoug Rabson 				 * was empty */
76303b270bSEivind Eklund static int fc_bmapcalls;		/* # of times pcbmap was called		 */
7727a0bc89SDoug Rabson 
7827a0bc89SDoug Rabson #define	LMMAX	20
79303b270bSEivind Eklund static int fc_lmdistance[LMMAX];/* counters for how far off the last
8027a0bc89SDoug Rabson 				 * cluster mapped entry was. */
81303b270bSEivind Eklund static int fc_largedistance;	/* off by more than LMMAX		 */
8227a0bc89SDoug Rabson 
8311caded3SAlfred Perlstein static int	chainalloc(struct msdosfsmount *pmp, u_long start,
84b76d0b32SBruce Evans 		    u_long count, u_long fillwith, u_long *retcluster,
85b76d0b32SBruce Evans 		    u_long *got);
8611caded3SAlfred Perlstein static int	chainlength(struct msdosfsmount *pmp, u_long start,
8711caded3SAlfred Perlstein 		    u_long count);
88b76d0b32SBruce Evans static void	fatblock(struct msdosfsmount *pmp, u_long ofs, u_long *bnp,
89b76d0b32SBruce Evans 		    u_long *sizep, u_long *bop);
90b76d0b32SBruce Evans static int	fatchain(struct msdosfsmount *pmp, u_long start, u_long count,
91b76d0b32SBruce Evans 		    u_long fillwith);
92b76d0b32SBruce Evans static void	fc_lookup(struct denode *dep, u_long findcn, u_long *frcnp,
93b76d0b32SBruce Evans 		    u_long *fsrcnp);
9411caded3SAlfred Perlstein static void	updatefats(struct msdosfsmount *pmp, struct buf *bp,
9511caded3SAlfred Perlstein 		    u_long fatbn);
96c1087c13SBruce Evans static __inline void
9711caded3SAlfred Perlstein 		usemap_alloc(struct msdosfsmount *pmp, u_long cn);
98c1087c13SBruce Evans static __inline void
9911caded3SAlfred Perlstein 		usemap_free(struct msdosfsmount *pmp, u_long cn);
10058c27bcfSBruce Evans 
10127a0bc89SDoug Rabson static void
10227a0bc89SDoug Rabson fatblock(pmp, ofs, bnp, sizep, bop)
10327a0bc89SDoug Rabson 	struct msdosfsmount *pmp;
10427a0bc89SDoug Rabson 	u_long ofs;
10527a0bc89SDoug Rabson 	u_long *bnp;
10627a0bc89SDoug Rabson 	u_long *sizep;
10727a0bc89SDoug Rabson 	u_long *bop;
10827a0bc89SDoug Rabson {
10927a0bc89SDoug Rabson 	u_long bn, size;
11027a0bc89SDoug Rabson 
11127a0bc89SDoug Rabson 	bn = ofs / pmp->pm_fatblocksize * pmp->pm_fatblocksec;
11227a0bc89SDoug Rabson 	size = min(pmp->pm_fatblocksec, pmp->pm_FATsecs - bn)
11301f6cfbaSYoshihiro Takahashi 	    * DEV_BSIZE;
114952a6212SJordan K. Hubbard 	bn += pmp->pm_fatblk + pmp->pm_curfat * pmp->pm_FATsecs;
115952a6212SJordan K. Hubbard 
11627a0bc89SDoug Rabson 	if (bnp)
11727a0bc89SDoug Rabson 		*bnp = bn;
11827a0bc89SDoug Rabson 	if (sizep)
11927a0bc89SDoug Rabson 		*sizep = size;
12027a0bc89SDoug Rabson 	if (bop)
12127a0bc89SDoug Rabson 		*bop = ofs % pmp->pm_fatblocksize;
12227a0bc89SDoug Rabson }
12327a0bc89SDoug Rabson 
12427a0bc89SDoug Rabson /*
12527a0bc89SDoug Rabson  * Map the logical cluster number of a file into a physical disk sector
12627a0bc89SDoug Rabson  * that is filesystem relative.
12727a0bc89SDoug Rabson  *
12827a0bc89SDoug Rabson  * dep	  - address of denode representing the file of interest
12927a0bc89SDoug Rabson  * findcn - file relative cluster whose filesystem relative cluster number
13027a0bc89SDoug Rabson  *	    and/or block number are/is to be found
13127a0bc89SDoug Rabson  * bnp	  - address of where to place the filesystem relative block number.
13227a0bc89SDoug Rabson  *	    If this pointer is null then don't return this quantity.
13327a0bc89SDoug Rabson  * cnp	  - address of where to place the filesystem relative cluster number.
13427a0bc89SDoug Rabson  *	    If this pointer is null then don't return this quantity.
13527a0bc89SDoug Rabson  *
13627a0bc89SDoug Rabson  * NOTE: Either bnp or cnp must be non-null.
13727a0bc89SDoug Rabson  * This function has one side effect.  If the requested file relative cluster
13827a0bc89SDoug Rabson  * is beyond the end of file, then the actual number of clusters in the file
13927a0bc89SDoug Rabson  * is returned in *cnp.  This is useful for determining how long a directory is.
14027a0bc89SDoug Rabson  *  If cnp is null, nothing is returned.
14127a0bc89SDoug Rabson  */
14227a0bc89SDoug Rabson int
143952a6212SJordan K. Hubbard pcbmap(dep, findcn, bnp, cnp, sp)
14427a0bc89SDoug Rabson 	struct denode *dep;
14527a0bc89SDoug Rabson 	u_long findcn;		/* file relative cluster to get		 */
14627a0bc89SDoug Rabson 	daddr_t *bnp;		/* returned filesys relative blk number	 */
14727a0bc89SDoug Rabson 	u_long *cnp;		/* returned cluster number		 */
148952a6212SJordan K. Hubbard 	int *sp;		/* returned block size			 */
14927a0bc89SDoug Rabson {
15027a0bc89SDoug Rabson 	int error;
15127a0bc89SDoug Rabson 	u_long i;
15227a0bc89SDoug Rabson 	u_long cn;
153952a6212SJordan K. Hubbard 	u_long prevcn = 0; /* XXX: prevcn could be used unititialized */
15427a0bc89SDoug Rabson 	u_long byteoffset;
15527a0bc89SDoug Rabson 	u_long bn;
15627a0bc89SDoug Rabson 	u_long bo;
15727a0bc89SDoug Rabson 	struct buf *bp = NULL;
15827a0bc89SDoug Rabson 	u_long bp_bn = -1;
15927a0bc89SDoug Rabson 	struct msdosfsmount *pmp = dep->de_pmp;
16027a0bc89SDoug Rabson 	u_long bsize;
16127a0bc89SDoug Rabson 
16227a0bc89SDoug Rabson 	fc_bmapcalls++;
16327a0bc89SDoug Rabson 
16427a0bc89SDoug Rabson 	/*
16527a0bc89SDoug Rabson 	 * If they don't give us someplace to return a value then don't
16627a0bc89SDoug Rabson 	 * bother doing anything.
16727a0bc89SDoug Rabson 	 */
168952a6212SJordan K. Hubbard 	if (bnp == NULL && cnp == NULL && sp == NULL)
169952a6212SJordan K. Hubbard 		return (0);
17027a0bc89SDoug Rabson 
17127a0bc89SDoug Rabson 	cn = dep->de_StartCluster;
17227a0bc89SDoug Rabson 	/*
17327a0bc89SDoug Rabson 	 * The "file" that makes up the root directory is contiguous,
17427a0bc89SDoug Rabson 	 * permanently allocated, of fixed size, and is not made up of
17527a0bc89SDoug Rabson 	 * clusters.  If the cluster number is beyond the end of the root
17627a0bc89SDoug Rabson 	 * directory, then return the number of clusters in the file.
17727a0bc89SDoug Rabson 	 */
17827a0bc89SDoug Rabson 	if (cn == MSDOSFSROOT) {
17927a0bc89SDoug Rabson 		if (dep->de_Attributes & ATTR_DIRECTORY) {
180952a6212SJordan K. Hubbard 			if (de_cn2off(pmp, findcn) >= dep->de_FileSize) {
18127a0bc89SDoug Rabson 				if (cnp)
182952a6212SJordan K. Hubbard 					*cnp = de_bn2cn(pmp, pmp->pm_rootdirsize);
183952a6212SJordan K. Hubbard 				return (E2BIG);
18427a0bc89SDoug Rabson 			}
18527a0bc89SDoug Rabson 			if (bnp)
186952a6212SJordan K. Hubbard 				*bnp = pmp->pm_rootdirblk + de_cn2bn(pmp, findcn);
18727a0bc89SDoug Rabson 			if (cnp)
18827a0bc89SDoug Rabson 				*cnp = MSDOSFSROOT;
189952a6212SJordan K. Hubbard 			if (sp)
190952a6212SJordan K. Hubbard 				*sp = min(pmp->pm_bpcluster,
191952a6212SJordan K. Hubbard 				    dep->de_FileSize - de_cn2off(pmp, findcn));
192952a6212SJordan K. Hubbard 			return (0);
19327a0bc89SDoug Rabson 		} else {		/* just an empty file */
19427a0bc89SDoug Rabson 			if (cnp)
19527a0bc89SDoug Rabson 				*cnp = 0;
196952a6212SJordan K. Hubbard 			return (E2BIG);
19727a0bc89SDoug Rabson 		}
19827a0bc89SDoug Rabson 	}
19927a0bc89SDoug Rabson 
20027a0bc89SDoug Rabson 	/*
201952a6212SJordan K. Hubbard 	 * All other files do I/O in cluster sized blocks
202952a6212SJordan K. Hubbard 	 */
203952a6212SJordan K. Hubbard 	if (sp)
204952a6212SJordan K. Hubbard 		*sp = pmp->pm_bpcluster;
205952a6212SJordan K. Hubbard 
206952a6212SJordan K. Hubbard 	/*
20727a0bc89SDoug Rabson 	 * Rummage around in the fat cache, maybe we can avoid tromping
20827a0bc89SDoug Rabson 	 * thru every fat entry for the file. And, keep track of how far
20927a0bc89SDoug Rabson 	 * off the cache was from where we wanted to be.
21027a0bc89SDoug Rabson 	 */
21127a0bc89SDoug Rabson 	i = 0;
21227a0bc89SDoug Rabson 	fc_lookup(dep, findcn, &i, &cn);
21327a0bc89SDoug Rabson 	if ((bn = findcn - i) >= LMMAX)
21427a0bc89SDoug Rabson 		fc_largedistance++;
21527a0bc89SDoug Rabson 	else
21627a0bc89SDoug Rabson 		fc_lmdistance[bn]++;
21727a0bc89SDoug Rabson 
21827a0bc89SDoug Rabson 	/*
21927a0bc89SDoug Rabson 	 * Handle all other files or directories the normal way.
22027a0bc89SDoug Rabson 	 */
22127a0bc89SDoug Rabson 	for (; i < findcn; i++) {
222952a6212SJordan K. Hubbard 		/*
223952a6212SJordan K. Hubbard 		 * Stop with all reserved clusters, not just with EOF.
224952a6212SJordan K. Hubbard 		 */
225952a6212SJordan K. Hubbard 		if ((cn | ~pmp->pm_fatmask) >= CLUST_RSRVD)
22627a0bc89SDoug Rabson 			goto hiteof;
22727a0bc89SDoug Rabson 		byteoffset = FATOFS(pmp, cn);
22827a0bc89SDoug Rabson 		fatblock(pmp, byteoffset, &bn, &bsize, &bo);
22927a0bc89SDoug Rabson 		if (bn != bp_bn) {
23027a0bc89SDoug Rabson 			if (bp)
23127a0bc89SDoug Rabson 				brelse(bp);
23227a0bc89SDoug Rabson 			error = bread(pmp->pm_devvp, bn, bsize, NOCRED, &bp);
233952a6212SJordan K. Hubbard 			if (error) {
234952a6212SJordan K. Hubbard 				brelse(bp);
235952a6212SJordan K. Hubbard 				return (error);
236952a6212SJordan K. Hubbard 			}
23727a0bc89SDoug Rabson 			bp_bn = bn;
23827a0bc89SDoug Rabson 		}
23927a0bc89SDoug Rabson 		prevcn = cn;
240d23af19aSTim J. Robbins 		if (bo >= bsize) {
241d23af19aSTim J. Robbins 			if (bp)
242d23af19aSTim J. Robbins 				brelse(bp);
243d23af19aSTim J. Robbins 			return (EIO);
244d23af19aSTim J. Robbins 		}
245952a6212SJordan K. Hubbard 		if (FAT32(pmp))
246952a6212SJordan K. Hubbard 			cn = getulong(&bp->b_data[bo]);
247952a6212SJordan K. Hubbard 		else
24827a0bc89SDoug Rabson 			cn = getushort(&bp->b_data[bo]);
249952a6212SJordan K. Hubbard 		if (FAT12(pmp) && (prevcn & 1))
25027a0bc89SDoug Rabson 			cn >>= 4;
251952a6212SJordan K. Hubbard 		cn &= pmp->pm_fatmask;
252952a6212SJordan K. Hubbard 
25327a0bc89SDoug Rabson 		/*
254952a6212SJordan K. Hubbard 		 * Force the special cluster numbers
255952a6212SJordan K. Hubbard 		 * to be the same for all cluster sizes
256952a6212SJordan K. Hubbard 		 * to let the rest of msdosfs handle
257952a6212SJordan K. Hubbard 		 * all cases the same.
25827a0bc89SDoug Rabson 		 */
259952a6212SJordan K. Hubbard 		if ((cn | ~pmp->pm_fatmask) >= CLUST_RSRVD)
260952a6212SJordan K. Hubbard 			cn |= ~pmp->pm_fatmask;
26127a0bc89SDoug Rabson 	}
26227a0bc89SDoug Rabson 
263952a6212SJordan K. Hubbard 	if (!MSDOSFSEOF(pmp, cn)) {
26427a0bc89SDoug Rabson 		if (bp)
26527a0bc89SDoug Rabson 			brelse(bp);
26627a0bc89SDoug Rabson 		if (bnp)
26727a0bc89SDoug Rabson 			*bnp = cntobn(pmp, cn);
26827a0bc89SDoug Rabson 		if (cnp)
26927a0bc89SDoug Rabson 			*cnp = cn;
27027a0bc89SDoug Rabson 		fc_setcache(dep, FC_LASTMAP, i, cn);
271952a6212SJordan K. Hubbard 		return (0);
27227a0bc89SDoug Rabson 	}
27327a0bc89SDoug Rabson 
27427a0bc89SDoug Rabson hiteof:;
27527a0bc89SDoug Rabson 	if (cnp)
27627a0bc89SDoug Rabson 		*cnp = i;
27727a0bc89SDoug Rabson 	if (bp)
27827a0bc89SDoug Rabson 		brelse(bp);
27927a0bc89SDoug Rabson 	/* update last file cluster entry in the fat cache */
28027a0bc89SDoug Rabson 	fc_setcache(dep, FC_LASTFC, i - 1, prevcn);
281952a6212SJordan K. Hubbard 	return (E2BIG);
28227a0bc89SDoug Rabson }
28327a0bc89SDoug Rabson 
28427a0bc89SDoug Rabson /*
28527a0bc89SDoug Rabson  * Find the closest entry in the fat cache to the cluster we are looking
28627a0bc89SDoug Rabson  * for.
28727a0bc89SDoug Rabson  */
2887fefffeeSPoul-Henning Kamp static void
289c3c6d51eSPoul-Henning Kamp fc_lookup(dep, findcn, frcnp, fsrcnp)
29027a0bc89SDoug Rabson 	struct denode *dep;
29127a0bc89SDoug Rabson 	u_long findcn;
29227a0bc89SDoug Rabson 	u_long *frcnp;
29327a0bc89SDoug Rabson 	u_long *fsrcnp;
29427a0bc89SDoug Rabson {
29527a0bc89SDoug Rabson 	int i;
29627a0bc89SDoug Rabson 	u_long cn;
29727a0bc89SDoug Rabson 	struct fatcache *closest = 0;
29827a0bc89SDoug Rabson 
29927a0bc89SDoug Rabson 	for (i = 0; i < FC_SIZE; i++) {
30027a0bc89SDoug Rabson 		cn = dep->de_fc[i].fc_frcn;
30127a0bc89SDoug Rabson 		if (cn != FCE_EMPTY && cn <= findcn) {
30227a0bc89SDoug Rabson 			if (closest == 0 || cn > closest->fc_frcn)
30327a0bc89SDoug Rabson 				closest = &dep->de_fc[i];
30427a0bc89SDoug Rabson 		}
30527a0bc89SDoug Rabson 	}
30627a0bc89SDoug Rabson 	if (closest) {
30727a0bc89SDoug Rabson 		*frcnp = closest->fc_frcn;
30827a0bc89SDoug Rabson 		*fsrcnp = closest->fc_fsrcn;
30927a0bc89SDoug Rabson 	}
31027a0bc89SDoug Rabson }
31127a0bc89SDoug Rabson 
31227a0bc89SDoug Rabson /*
31327a0bc89SDoug Rabson  * Purge the fat cache in denode dep of all entries relating to file
31427a0bc89SDoug Rabson  * relative cluster frcn and beyond.
31527a0bc89SDoug Rabson  */
316952a6212SJordan K. Hubbard void
317952a6212SJordan K. Hubbard fc_purge(dep, frcn)
31827a0bc89SDoug Rabson 	struct denode *dep;
31927a0bc89SDoug Rabson 	u_int frcn;
32027a0bc89SDoug Rabson {
32127a0bc89SDoug Rabson 	int i;
32227a0bc89SDoug Rabson 	struct fatcache *fcp;
32327a0bc89SDoug Rabson 
32427a0bc89SDoug Rabson 	fcp = dep->de_fc;
32527a0bc89SDoug Rabson 	for (i = 0; i < FC_SIZE; i++, fcp++) {
32627a0bc89SDoug Rabson 		if (fcp->fc_frcn >= frcn)
32727a0bc89SDoug Rabson 			fcp->fc_frcn = FCE_EMPTY;
32827a0bc89SDoug Rabson 	}
32927a0bc89SDoug Rabson }
33027a0bc89SDoug Rabson 
33127a0bc89SDoug Rabson /*
332952a6212SJordan K. Hubbard  * Update the fat.
333952a6212SJordan K. Hubbard  * If mirroring the fat, update all copies, with the first copy as last.
334952a6212SJordan K. Hubbard  * Else update only the current fat (ignoring the others).
33527a0bc89SDoug Rabson  *
33627a0bc89SDoug Rabson  * pmp	 - msdosfsmount structure for filesystem to update
33727a0bc89SDoug Rabson  * bp	 - addr of modified fat block
33827a0bc89SDoug Rabson  * fatbn - block number relative to begin of filesystem of the modified fat block.
33927a0bc89SDoug Rabson  */
340a98ca469SPoul-Henning Kamp static void
34127a0bc89SDoug Rabson updatefats(pmp, bp, fatbn)
34227a0bc89SDoug Rabson 	struct msdosfsmount *pmp;
34327a0bc89SDoug Rabson 	struct buf *bp;
34427a0bc89SDoug Rabson 	u_long fatbn;
34527a0bc89SDoug Rabson {
34627a0bc89SDoug Rabson 	int i;
34727a0bc89SDoug Rabson 	struct buf *bpn;
34827a0bc89SDoug Rabson 
34927a0bc89SDoug Rabson #ifdef MSDOSFS_DEBUG
350952a6212SJordan K. Hubbard 	printf("updatefats(pmp %p, bp %p, fatbn %lu)\n", pmp, bp, fatbn);
35127a0bc89SDoug Rabson #endif
35227a0bc89SDoug Rabson 
35327a0bc89SDoug Rabson 	/*
354952a6212SJordan K. Hubbard 	 * If we have an FSInfo block, update it.
355952a6212SJordan K. Hubbard 	 */
356952a6212SJordan K. Hubbard 	if (pmp->pm_fsinfo) {
357952a6212SJordan K. Hubbard 		u_long cn = pmp->pm_nxtfree;
358952a6212SJordan K. Hubbard 
359952a6212SJordan K. Hubbard 		if (pmp->pm_freeclustercount
360952a6212SJordan K. Hubbard 		    && (pmp->pm_inusemap[cn / N_INUSEBITS]
361952a6212SJordan K. Hubbard 			& (1 << (cn % N_INUSEBITS)))) {
362952a6212SJordan K. Hubbard 			/*
363952a6212SJordan K. Hubbard 			 * The cluster indicated in FSInfo isn't free
364952a6212SJordan K. Hubbard 			 * any longer.  Got get a new free one.
365952a6212SJordan K. Hubbard 			 */
36632d3966fSAndrey A. Chernov 			for (cn = 0; cn < pmp->pm_maxcluster; cn += N_INUSEBITS)
367952a6212SJordan K. Hubbard 				if (pmp->pm_inusemap[cn / N_INUSEBITS] != (u_int)-1)
368952a6212SJordan K. Hubbard 					break;
369952a6212SJordan K. Hubbard 			pmp->pm_nxtfree = cn
370952a6212SJordan K. Hubbard 				+ ffs(pmp->pm_inusemap[cn / N_INUSEBITS]
371952a6212SJordan K. Hubbard 				      ^ (u_int)-1) - 1;
372952a6212SJordan K. Hubbard 		}
37301f6cfbaSYoshihiro Takahashi 		if (bread(pmp->pm_devvp, pmp->pm_fsinfo, fsi_size(pmp),
37401f6cfbaSYoshihiro Takahashi 		    NOCRED, &bpn) != 0) {
375952a6212SJordan K. Hubbard 			/*
376952a6212SJordan K. Hubbard 			 * Ignore the error, but turn off FSInfo update for the future.
377952a6212SJordan K. Hubbard 			 */
378952a6212SJordan K. Hubbard 			pmp->pm_fsinfo = 0;
379952a6212SJordan K. Hubbard 			brelse(bpn);
380952a6212SJordan K. Hubbard 		} else {
381952a6212SJordan K. Hubbard 			struct fsinfo *fp = (struct fsinfo *)bpn->b_data;
382952a6212SJordan K. Hubbard 
383952a6212SJordan K. Hubbard 			putulong(fp->fsinfree, pmp->pm_freeclustercount);
384952a6212SJordan K. Hubbard 			putulong(fp->fsinxtfree, pmp->pm_nxtfree);
385952a6212SJordan K. Hubbard 			if (pmp->pm_flags & MSDOSFSMNT_WAITONFAT)
386952a6212SJordan K. Hubbard 				bwrite(bpn);
387952a6212SJordan K. Hubbard 			else
388952a6212SJordan K. Hubbard 				bdwrite(bpn);
389952a6212SJordan K. Hubbard 		}
390952a6212SJordan K. Hubbard 	}
391952a6212SJordan K. Hubbard 
392952a6212SJordan K. Hubbard 	if (pmp->pm_flags & MSDOSFS_FATMIRROR) {
393952a6212SJordan K. Hubbard 		/*
39427a0bc89SDoug Rabson 		 * Now copy the block(s) of the modified fat to the other copies of
39527a0bc89SDoug Rabson 		 * the fat and write them out.  This is faster than reading in the
39627a0bc89SDoug Rabson 		 * other fats and then writing them back out.  This could tie up
39727a0bc89SDoug Rabson 		 * the fat for quite a while. Preventing others from accessing it.
39827a0bc89SDoug Rabson 		 * To prevent us from going after the fat quite so much we use
39927a0bc89SDoug Rabson 		 * delayed writes, unless they specfied "synchronous" when the
40027a0bc89SDoug Rabson 		 * filesystem was mounted.  If synch is asked for then use
40127a0bc89SDoug Rabson 		 * bwrite()'s and really slow things down.
40227a0bc89SDoug Rabson 		 */
40327a0bc89SDoug Rabson 		for (i = 1; i < pmp->pm_FATs; i++) {
40427a0bc89SDoug Rabson 			fatbn += pmp->pm_FATsecs;
40527a0bc89SDoug Rabson 			/* getblk() never fails */
4067261f5f6SJeff Roberson 			bpn = getblk(pmp->pm_devvp, fatbn, bp->b_bcount,
4077261f5f6SJeff Roberson 			    0, 0, 0);
40827a0bc89SDoug Rabson 			bcopy(bp->b_data, bpn->b_data, bp->b_bcount);
409952a6212SJordan K. Hubbard 			if (pmp->pm_flags & MSDOSFSMNT_WAITONFAT)
41027a0bc89SDoug Rabson 				bwrite(bpn);
41127a0bc89SDoug Rabson 			else
41227a0bc89SDoug Rabson 				bdwrite(bpn);
41327a0bc89SDoug Rabson 		}
414952a6212SJordan K. Hubbard 	}
415952a6212SJordan K. Hubbard 
41627a0bc89SDoug Rabson 	/*
417952a6212SJordan K. Hubbard 	 * Write out the first (or current) fat last.
41827a0bc89SDoug Rabson 	 */
419952a6212SJordan K. Hubbard 	if (pmp->pm_flags & MSDOSFSMNT_WAITONFAT)
42027a0bc89SDoug Rabson 		bwrite(bp);
42127a0bc89SDoug Rabson 	else
42227a0bc89SDoug Rabson 		bdwrite(bp);
423952a6212SJordan K. Hubbard 	/*
424952a6212SJordan K. Hubbard 	 * Maybe update fsinfo sector here?
425952a6212SJordan K. Hubbard 	 */
42627a0bc89SDoug Rabson }
42727a0bc89SDoug Rabson 
42827a0bc89SDoug Rabson /*
42927a0bc89SDoug Rabson  * Updating entries in 12 bit fats is a pain in the butt.
43027a0bc89SDoug Rabson  *
43127a0bc89SDoug Rabson  * The following picture shows where nibbles go when moving from a 12 bit
43227a0bc89SDoug Rabson  * cluster number into the appropriate bytes in the FAT.
43327a0bc89SDoug Rabson  *
43427a0bc89SDoug Rabson  *	byte m        byte m+1      byte m+2
43527a0bc89SDoug Rabson  *	+----+----+   +----+----+   +----+----+
43627a0bc89SDoug Rabson  *	|  0    1 |   |  2    3 |   |  4    5 |   FAT bytes
43727a0bc89SDoug Rabson  *	+----+----+   +----+----+   +----+----+
43827a0bc89SDoug Rabson  *
43927a0bc89SDoug Rabson  *	+----+----+----+   +----+----+----+
44027a0bc89SDoug Rabson  *	|  3    0    1 |   |  4    5    2 |
44127a0bc89SDoug Rabson  *	+----+----+----+   +----+----+----+
44227a0bc89SDoug Rabson  *	cluster n  	   cluster n+1
44327a0bc89SDoug Rabson  *
44427a0bc89SDoug Rabson  * Where n is even. m = n + (n >> 2)
44527a0bc89SDoug Rabson  *
44627a0bc89SDoug Rabson  */
447c1087c13SBruce Evans static __inline void
44827a0bc89SDoug Rabson usemap_alloc(pmp, cn)
44927a0bc89SDoug Rabson 	struct msdosfsmount *pmp;
45027a0bc89SDoug Rabson 	u_long cn;
45127a0bc89SDoug Rabson {
452952a6212SJordan K. Hubbard 
453952a6212SJordan K. Hubbard 	pmp->pm_inusemap[cn / N_INUSEBITS] |= 1 << (cn % N_INUSEBITS);
45427a0bc89SDoug Rabson 	pmp->pm_freeclustercount--;
45527a0bc89SDoug Rabson }
45627a0bc89SDoug Rabson 
457c1087c13SBruce Evans static __inline void
45827a0bc89SDoug Rabson usemap_free(pmp, cn)
45927a0bc89SDoug Rabson 	struct msdosfsmount *pmp;
46027a0bc89SDoug Rabson 	u_long cn;
46127a0bc89SDoug Rabson {
462952a6212SJordan K. Hubbard 
46327a0bc89SDoug Rabson 	pmp->pm_freeclustercount++;
46427a0bc89SDoug Rabson 	pmp->pm_inusemap[cn / N_INUSEBITS] &= ~(1 << (cn % N_INUSEBITS));
46527a0bc89SDoug Rabson }
46627a0bc89SDoug Rabson 
46727a0bc89SDoug Rabson int
46827a0bc89SDoug Rabson clusterfree(pmp, cluster, oldcnp)
46927a0bc89SDoug Rabson 	struct msdosfsmount *pmp;
47027a0bc89SDoug Rabson 	u_long cluster;
47127a0bc89SDoug Rabson 	u_long *oldcnp;
47227a0bc89SDoug Rabson {
47327a0bc89SDoug Rabson 	int error;
47427a0bc89SDoug Rabson 	u_long oldcn;
47527a0bc89SDoug Rabson 
476952a6212SJordan K. Hubbard 	usemap_free(pmp, cluster);
47727a0bc89SDoug Rabson 	error = fatentry(FAT_GET_AND_SET, pmp, cluster, &oldcn, MSDOSFSFREE);
478952a6212SJordan K. Hubbard 	if (error) {
479952a6212SJordan K. Hubbard 		usemap_alloc(pmp, cluster);
480952a6212SJordan K. Hubbard 		return (error);
481952a6212SJordan K. Hubbard 	}
48227a0bc89SDoug Rabson 	/*
48327a0bc89SDoug Rabson 	 * If the cluster was successfully marked free, then update
48427a0bc89SDoug Rabson 	 * the count of free clusters, and turn off the "allocated"
48527a0bc89SDoug Rabson 	 * bit in the "in use" cluster bit map.
48627a0bc89SDoug Rabson 	 */
48727a0bc89SDoug Rabson 	if (oldcnp)
48827a0bc89SDoug Rabson 		*oldcnp = oldcn;
489952a6212SJordan K. Hubbard 	return (0);
49027a0bc89SDoug Rabson }
49127a0bc89SDoug Rabson 
49227a0bc89SDoug Rabson /*
49327a0bc89SDoug Rabson  * Get or Set or 'Get and Set' the cluster'th entry in the fat.
49427a0bc89SDoug Rabson  *
49527a0bc89SDoug Rabson  * function	- whether to get or set a fat entry
49627a0bc89SDoug Rabson  * pmp		- address of the msdosfsmount structure for the filesystem
49727a0bc89SDoug Rabson  *		  whose fat is to be manipulated.
49827a0bc89SDoug Rabson  * cn		- which cluster is of interest
49927a0bc89SDoug Rabson  * oldcontents	- address of a word that is to receive the contents of the
50027a0bc89SDoug Rabson  *		  cluster'th entry if this is a get function
50127a0bc89SDoug Rabson  * newcontents	- the new value to be written into the cluster'th element of
50227a0bc89SDoug Rabson  *		  the fat if this is a set function.
50327a0bc89SDoug Rabson  *
50427a0bc89SDoug Rabson  * This function can also be used to free a cluster by setting the fat entry
50527a0bc89SDoug Rabson  * for a cluster to 0.
50627a0bc89SDoug Rabson  *
50727a0bc89SDoug Rabson  * All copies of the fat are updated if this is a set function. NOTE: If
50827a0bc89SDoug Rabson  * fatentry() marks a cluster as free it does not update the inusemap in
50927a0bc89SDoug Rabson  * the msdosfsmount structure. This is left to the caller.
51027a0bc89SDoug Rabson  */
51127a0bc89SDoug Rabson int
51227a0bc89SDoug Rabson fatentry(function, pmp, cn, oldcontents, newcontents)
51327a0bc89SDoug Rabson 	int function;
51427a0bc89SDoug Rabson 	struct msdosfsmount *pmp;
51527a0bc89SDoug Rabson 	u_long cn;
51627a0bc89SDoug Rabson 	u_long *oldcontents;
51727a0bc89SDoug Rabson 	u_long newcontents;
51827a0bc89SDoug Rabson {
51927a0bc89SDoug Rabson 	int error;
52027a0bc89SDoug Rabson 	u_long readcn;
52127a0bc89SDoug Rabson 	u_long bn, bo, bsize, byteoffset;
52227a0bc89SDoug Rabson 	struct buf *bp;
52327a0bc89SDoug Rabson 
524952a6212SJordan K. Hubbard #ifdef	MSDOSFS_DEBUG
525952a6212SJordan K. Hubbard 	printf("fatentry(func %d, pmp %p, clust %lu, oldcon %p, newcon %lx)\n",
526952a6212SJordan K. Hubbard 	     function, pmp, cn, oldcontents, newcontents);
527952a6212SJordan K. Hubbard #endif
52827a0bc89SDoug Rabson 
52927a0bc89SDoug Rabson #ifdef DIAGNOSTIC
53027a0bc89SDoug Rabson 	/*
53127a0bc89SDoug Rabson 	 * Be sure they asked us to do something.
53227a0bc89SDoug Rabson 	 */
53327a0bc89SDoug Rabson 	if ((function & (FAT_SET | FAT_GET)) == 0) {
53427a0bc89SDoug Rabson 		printf("fatentry(): function code doesn't specify get or set\n");
535952a6212SJordan K. Hubbard 		return (EINVAL);
53627a0bc89SDoug Rabson 	}
53727a0bc89SDoug Rabson 
53827a0bc89SDoug Rabson 	/*
53927a0bc89SDoug Rabson 	 * If they asked us to return a cluster number but didn't tell us
54027a0bc89SDoug Rabson 	 * where to put it, give them an error.
54127a0bc89SDoug Rabson 	 */
54227a0bc89SDoug Rabson 	if ((function & FAT_GET) && oldcontents == NULL) {
54327a0bc89SDoug Rabson 		printf("fatentry(): get function with no place to put result\n");
544952a6212SJordan K. Hubbard 		return (EINVAL);
54527a0bc89SDoug Rabson 	}
54627a0bc89SDoug Rabson #endif
54727a0bc89SDoug Rabson 
54827a0bc89SDoug Rabson 	/*
54927a0bc89SDoug Rabson 	 * Be sure the requested cluster is in the filesystem.
55027a0bc89SDoug Rabson 	 */
55127a0bc89SDoug Rabson 	if (cn < CLUST_FIRST || cn > pmp->pm_maxcluster)
552952a6212SJordan K. Hubbard 		return (EINVAL);
55327a0bc89SDoug Rabson 
55427a0bc89SDoug Rabson 	byteoffset = FATOFS(pmp, cn);
55527a0bc89SDoug Rabson 	fatblock(pmp, byteoffset, &bn, &bsize, &bo);
556c3c6d51eSPoul-Henning Kamp 	error = bread(pmp->pm_devvp, bn, bsize, NOCRED, &bp);
557952a6212SJordan K. Hubbard 	if (error) {
558952a6212SJordan K. Hubbard 		brelse(bp);
559952a6212SJordan K. Hubbard 		return (error);
560952a6212SJordan K. Hubbard 	}
56127a0bc89SDoug Rabson 
56227a0bc89SDoug Rabson 	if (function & FAT_GET) {
563952a6212SJordan K. Hubbard 		if (FAT32(pmp))
564952a6212SJordan K. Hubbard 			readcn = getulong(&bp->b_data[bo]);
565952a6212SJordan K. Hubbard 		else
56627a0bc89SDoug Rabson 			readcn = getushort(&bp->b_data[bo]);
567952a6212SJordan K. Hubbard 		if (FAT12(pmp) & (cn & 1))
56827a0bc89SDoug Rabson 			readcn >>= 4;
569952a6212SJordan K. Hubbard 		readcn &= pmp->pm_fatmask;
570952a6212SJordan K. Hubbard 		/* map reserved fat entries to same values for all fats */
571952a6212SJordan K. Hubbard 		if ((readcn | ~pmp->pm_fatmask) >= CLUST_RSRVD)
572952a6212SJordan K. Hubbard 			readcn |= ~pmp->pm_fatmask;
57327a0bc89SDoug Rabson 		*oldcontents = readcn;
57427a0bc89SDoug Rabson 	}
57527a0bc89SDoug Rabson 	if (function & FAT_SET) {
576952a6212SJordan K. Hubbard 		switch (pmp->pm_fatmask) {
577952a6212SJordan K. Hubbard 		case FAT12_MASK:
57827a0bc89SDoug Rabson 			readcn = getushort(&bp->b_data[bo]);
57927a0bc89SDoug Rabson 			if (cn & 1) {
58027a0bc89SDoug Rabson 				readcn &= 0x000f;
58127a0bc89SDoug Rabson 				readcn |= newcontents << 4;
58227a0bc89SDoug Rabson 			} else {
58327a0bc89SDoug Rabson 				readcn &= 0xf000;
58427a0bc89SDoug Rabson 				readcn |= newcontents & 0xfff;
58527a0bc89SDoug Rabson 			}
58627a0bc89SDoug Rabson 			putushort(&bp->b_data[bo], readcn);
587952a6212SJordan K. Hubbard 			break;
588952a6212SJordan K. Hubbard 		case FAT16_MASK:
58927a0bc89SDoug Rabson 			putushort(&bp->b_data[bo], newcontents);
590952a6212SJordan K. Hubbard 			break;
591952a6212SJordan K. Hubbard 		case FAT32_MASK:
592952a6212SJordan K. Hubbard 			/*
593952a6212SJordan K. Hubbard 			 * According to spec we have to retain the
594952a6212SJordan K. Hubbard 			 * high order bits of the fat entry.
595952a6212SJordan K. Hubbard 			 */
596952a6212SJordan K. Hubbard 			readcn = getulong(&bp->b_data[bo]);
597952a6212SJordan K. Hubbard 			readcn &= ~FAT32_MASK;
598952a6212SJordan K. Hubbard 			readcn |= newcontents & FAT32_MASK;
599952a6212SJordan K. Hubbard 			putulong(&bp->b_data[bo], readcn);
600952a6212SJordan K. Hubbard 			break;
601952a6212SJordan K. Hubbard 		}
60227a0bc89SDoug Rabson 		updatefats(pmp, bp, bn);
60327a0bc89SDoug Rabson 		bp = NULL;
60427a0bc89SDoug Rabson 		pmp->pm_fmod = 1;
60527a0bc89SDoug Rabson 	}
60627a0bc89SDoug Rabson 	if (bp)
60727a0bc89SDoug Rabson 		brelse(bp);
608952a6212SJordan K. Hubbard 	return (0);
60927a0bc89SDoug Rabson }
61027a0bc89SDoug Rabson 
61127a0bc89SDoug Rabson /*
61227a0bc89SDoug Rabson  * Update a contiguous cluster chain
61327a0bc89SDoug Rabson  *
61427a0bc89SDoug Rabson  * pmp	    - mount point
61527a0bc89SDoug Rabson  * start    - first cluster of chain
61627a0bc89SDoug Rabson  * count    - number of clusters in chain
61727a0bc89SDoug Rabson  * fillwith - what to write into fat entry of last cluster
61827a0bc89SDoug Rabson  */
61927a0bc89SDoug Rabson static int
62027a0bc89SDoug Rabson fatchain(pmp, start, count, fillwith)
62127a0bc89SDoug Rabson 	struct msdosfsmount *pmp;
62227a0bc89SDoug Rabson 	u_long start;
62327a0bc89SDoug Rabson 	u_long count;
62427a0bc89SDoug Rabson 	u_long fillwith;
62527a0bc89SDoug Rabson {
62627a0bc89SDoug Rabson 	int error;
62727a0bc89SDoug Rabson 	u_long bn, bo, bsize, byteoffset, readcn, newc;
62827a0bc89SDoug Rabson 	struct buf *bp;
62927a0bc89SDoug Rabson 
63027a0bc89SDoug Rabson #ifdef MSDOSFS_DEBUG
631952a6212SJordan K. Hubbard 	printf("fatchain(pmp %p, start %lu, count %lu, fillwith %lx)\n",
63227a0bc89SDoug Rabson 	    pmp, start, count, fillwith);
63327a0bc89SDoug Rabson #endif
63427a0bc89SDoug Rabson 	/*
63527a0bc89SDoug Rabson 	 * Be sure the clusters are in the filesystem.
63627a0bc89SDoug Rabson 	 */
63727a0bc89SDoug Rabson 	if (start < CLUST_FIRST || start + count - 1 > pmp->pm_maxcluster)
638952a6212SJordan K. Hubbard 		return (EINVAL);
63927a0bc89SDoug Rabson 
64027a0bc89SDoug Rabson 	while (count > 0) {
64127a0bc89SDoug Rabson 		byteoffset = FATOFS(pmp, start);
64227a0bc89SDoug Rabson 		fatblock(pmp, byteoffset, &bn, &bsize, &bo);
643c3c6d51eSPoul-Henning Kamp 		error = bread(pmp->pm_devvp, bn, bsize, NOCRED, &bp);
644952a6212SJordan K. Hubbard 		if (error) {
645952a6212SJordan K. Hubbard 			brelse(bp);
646952a6212SJordan K. Hubbard 			return (error);
647952a6212SJordan K. Hubbard 		}
64827a0bc89SDoug Rabson 		while (count > 0) {
64927a0bc89SDoug Rabson 			start++;
65027a0bc89SDoug Rabson 			newc = --count > 0 ? start : fillwith;
651952a6212SJordan K. Hubbard 			switch (pmp->pm_fatmask) {
652952a6212SJordan K. Hubbard 			case FAT12_MASK:
65327a0bc89SDoug Rabson 				readcn = getushort(&bp->b_data[bo]);
65427a0bc89SDoug Rabson 				if (start & 1) {
65527a0bc89SDoug Rabson 					readcn &= 0xf000;
65627a0bc89SDoug Rabson 					readcn |= newc & 0xfff;
65727a0bc89SDoug Rabson 				} else {
65827a0bc89SDoug Rabson 					readcn &= 0x000f;
65927a0bc89SDoug Rabson 					readcn |= newc << 4;
66027a0bc89SDoug Rabson 				}
66127a0bc89SDoug Rabson 				putushort(&bp->b_data[bo], readcn);
66227a0bc89SDoug Rabson 				bo++;
66327a0bc89SDoug Rabson 				if (!(start & 1))
66427a0bc89SDoug Rabson 					bo++;
665952a6212SJordan K. Hubbard 				break;
666952a6212SJordan K. Hubbard 			case FAT16_MASK:
66727a0bc89SDoug Rabson 				putushort(&bp->b_data[bo], newc);
66827a0bc89SDoug Rabson 				bo += 2;
669952a6212SJordan K. Hubbard 				break;
670952a6212SJordan K. Hubbard 			case FAT32_MASK:
671952a6212SJordan K. Hubbard 				readcn = getulong(&bp->b_data[bo]);
672952a6212SJordan K. Hubbard 				readcn &= ~pmp->pm_fatmask;
673952a6212SJordan K. Hubbard 				readcn |= newc & pmp->pm_fatmask;
674952a6212SJordan K. Hubbard 				putulong(&bp->b_data[bo], readcn);
675952a6212SJordan K. Hubbard 				bo += 4;
676952a6212SJordan K. Hubbard 				break;
67727a0bc89SDoug Rabson 			}
67827a0bc89SDoug Rabson 			if (bo >= bsize)
67927a0bc89SDoug Rabson 				break;
68027a0bc89SDoug Rabson 		}
68127a0bc89SDoug Rabson 		updatefats(pmp, bp, bn);
68227a0bc89SDoug Rabson 	}
68327a0bc89SDoug Rabson 	pmp->pm_fmod = 1;
684952a6212SJordan K. Hubbard 	return (0);
68527a0bc89SDoug Rabson }
68627a0bc89SDoug Rabson 
68727a0bc89SDoug Rabson /*
68827a0bc89SDoug Rabson  * Check the length of a free cluster chain starting at start.
68927a0bc89SDoug Rabson  *
69027a0bc89SDoug Rabson  * pmp	 - mount point
69127a0bc89SDoug Rabson  * start - start of chain
69227a0bc89SDoug Rabson  * count - maximum interesting length
69327a0bc89SDoug Rabson  */
6947fefffeeSPoul-Henning Kamp static int
69527a0bc89SDoug Rabson chainlength(pmp, start, count)
69627a0bc89SDoug Rabson 	struct msdosfsmount *pmp;
69727a0bc89SDoug Rabson 	u_long start;
69827a0bc89SDoug Rabson 	u_long count;
69927a0bc89SDoug Rabson {
70027a0bc89SDoug Rabson 	u_long idx, max_idx;
70127a0bc89SDoug Rabson 	u_int map;
70227a0bc89SDoug Rabson 	u_long len;
70327a0bc89SDoug Rabson 
70427a0bc89SDoug Rabson 	max_idx = pmp->pm_maxcluster / N_INUSEBITS;
70527a0bc89SDoug Rabson 	idx = start / N_INUSEBITS;
70627a0bc89SDoug Rabson 	start %= N_INUSEBITS;
70727a0bc89SDoug Rabson 	map = pmp->pm_inusemap[idx];
70827a0bc89SDoug Rabson 	map &= ~((1 << start) - 1);
70927a0bc89SDoug Rabson 	if (map) {
71027a0bc89SDoug Rabson 		len = ffs(map) - 1 - start;
711952a6212SJordan K. Hubbard 		return (len > count ? count : len);
71227a0bc89SDoug Rabson 	}
71327a0bc89SDoug Rabson 	len = N_INUSEBITS - start;
71427a0bc89SDoug Rabson 	if (len >= count)
715952a6212SJordan K. Hubbard 		return (count);
71627a0bc89SDoug Rabson 	while (++idx <= max_idx) {
71727a0bc89SDoug Rabson 		if (len >= count)
71827a0bc89SDoug Rabson 			break;
719c3c6d51eSPoul-Henning Kamp 		map = pmp->pm_inusemap[idx];
720c3c6d51eSPoul-Henning Kamp 		if (map) {
72127a0bc89SDoug Rabson 			len +=  ffs(map) - 1;
72227a0bc89SDoug Rabson 			break;
72327a0bc89SDoug Rabson 		}
72427a0bc89SDoug Rabson 		len += N_INUSEBITS;
72527a0bc89SDoug Rabson 	}
726952a6212SJordan K. Hubbard 	return (len > count ? count : len);
72727a0bc89SDoug Rabson }
72827a0bc89SDoug Rabson 
72927a0bc89SDoug Rabson /*
73027a0bc89SDoug Rabson  * Allocate contigous free clusters.
73127a0bc89SDoug Rabson  *
73227a0bc89SDoug Rabson  * pmp	      - mount point.
73327a0bc89SDoug Rabson  * start      - start of cluster chain.
73427a0bc89SDoug Rabson  * count      - number of clusters to allocate.
73527a0bc89SDoug Rabson  * fillwith   - put this value into the fat entry for the
73627a0bc89SDoug Rabson  *		last allocated cluster.
73727a0bc89SDoug Rabson  * retcluster - put the first allocated cluster's number here.
73827a0bc89SDoug Rabson  * got	      - how many clusters were actually allocated.
73927a0bc89SDoug Rabson  */
7407fefffeeSPoul-Henning Kamp static int
74127a0bc89SDoug Rabson chainalloc(pmp, start, count, fillwith, retcluster, got)
74227a0bc89SDoug Rabson 	struct msdosfsmount *pmp;
74327a0bc89SDoug Rabson 	u_long start;
74427a0bc89SDoug Rabson 	u_long count;
74527a0bc89SDoug Rabson 	u_long fillwith;
74627a0bc89SDoug Rabson 	u_long *retcluster;
74727a0bc89SDoug Rabson 	u_long *got;
74827a0bc89SDoug Rabson {
74927a0bc89SDoug Rabson 	int error;
750952a6212SJordan K. Hubbard 	u_long cl, n;
751952a6212SJordan K. Hubbard 
752952a6212SJordan K. Hubbard 	for (cl = start, n = count; n-- > 0;)
753952a6212SJordan K. Hubbard 		usemap_alloc(pmp, cl++);
75427a0bc89SDoug Rabson 
75527a0bc89SDoug Rabson 	error = fatchain(pmp, start, count, fillwith);
756952a6212SJordan K. Hubbard 	if (error != 0)
757952a6212SJordan K. Hubbard 		return (error);
75827a0bc89SDoug Rabson #ifdef MSDOSFS_DEBUG
759952a6212SJordan K. Hubbard 	printf("clusteralloc(): allocated cluster chain at %lu (%lu clusters)\n",
76027a0bc89SDoug Rabson 	    start, count);
76127a0bc89SDoug Rabson #endif
76227a0bc89SDoug Rabson 	if (retcluster)
76327a0bc89SDoug Rabson 		*retcluster = start;
76427a0bc89SDoug Rabson 	if (got)
76527a0bc89SDoug Rabson 		*got = count;
766952a6212SJordan K. Hubbard 	return (0);
76727a0bc89SDoug Rabson }
76827a0bc89SDoug Rabson 
76927a0bc89SDoug Rabson /*
77027a0bc89SDoug Rabson  * Allocate contiguous free clusters.
77127a0bc89SDoug Rabson  *
77227a0bc89SDoug Rabson  * pmp	      - mount point.
77327a0bc89SDoug Rabson  * start      - preferred start of cluster chain.
77427a0bc89SDoug Rabson  * count      - number of clusters requested.
77527a0bc89SDoug Rabson  * fillwith   - put this value into the fat entry for the
77627a0bc89SDoug Rabson  *		last allocated cluster.
77727a0bc89SDoug Rabson  * retcluster - put the first allocated cluster's number here.
77827a0bc89SDoug Rabson  * got	      - how many clusters were actually allocated.
77927a0bc89SDoug Rabson  */
78027a0bc89SDoug Rabson int
78127a0bc89SDoug Rabson clusteralloc(pmp, start, count, fillwith, retcluster, got)
78227a0bc89SDoug Rabson 	struct msdosfsmount *pmp;
78327a0bc89SDoug Rabson 	u_long start;
78427a0bc89SDoug Rabson 	u_long count;
78527a0bc89SDoug Rabson 	u_long fillwith;
78627a0bc89SDoug Rabson 	u_long *retcluster;
78727a0bc89SDoug Rabson 	u_long *got;
78827a0bc89SDoug Rabson {
78927a0bc89SDoug Rabson 	u_long idx;
790952a6212SJordan K. Hubbard 	u_long len, newst, foundl, cn, l;
791952a6212SJordan K. Hubbard 	u_long foundcn = 0; /* XXX: foundcn could be used unititialized */
79227a0bc89SDoug Rabson 	u_int map;
79327a0bc89SDoug Rabson 
79427a0bc89SDoug Rabson #ifdef MSDOSFS_DEBUG
795952a6212SJordan K. Hubbard 	printf("clusteralloc(): find %lu clusters\n",count);
79627a0bc89SDoug Rabson #endif
79727a0bc89SDoug Rabson 	if (start) {
79827a0bc89SDoug Rabson 		if ((len = chainlength(pmp, start, count)) >= count)
799952a6212SJordan K. Hubbard 			return (chainalloc(pmp, start, count, fillwith, retcluster, got));
8007cd5051bSPoul-Henning Kamp 	} else
80127a0bc89SDoug Rabson 		len = 0;
80227a0bc89SDoug Rabson 
80327a0bc89SDoug Rabson 	/*
80427a0bc89SDoug Rabson 	 * Start at a (pseudo) random place to maximize cluster runs
80527a0bc89SDoug Rabson 	 * under multiple writers.
80627a0bc89SDoug Rabson 	 */
8077cd5051bSPoul-Henning Kamp 	newst = random() % (pmp->pm_maxcluster + 1);
80827a0bc89SDoug Rabson 	foundl = 0;
80927a0bc89SDoug Rabson 
81027a0bc89SDoug Rabson 	for (cn = newst; cn <= pmp->pm_maxcluster;) {
81127a0bc89SDoug Rabson 		idx = cn / N_INUSEBITS;
81227a0bc89SDoug Rabson 		map = pmp->pm_inusemap[idx];
81327a0bc89SDoug Rabson 		map |= (1 << (cn % N_INUSEBITS)) - 1;
81427a0bc89SDoug Rabson 		if (map != (u_int)-1) {
81527a0bc89SDoug Rabson 			cn = idx * N_INUSEBITS + ffs(map^(u_int)-1) - 1;
81627a0bc89SDoug Rabson 			if ((l = chainlength(pmp, cn, count)) >= count)
817952a6212SJordan K. Hubbard 				return (chainalloc(pmp, cn, count, fillwith, retcluster, got));
81827a0bc89SDoug Rabson 			if (l > foundl) {
81927a0bc89SDoug Rabson 				foundcn = cn;
82027a0bc89SDoug Rabson 				foundl = l;
82127a0bc89SDoug Rabson 			}
82227a0bc89SDoug Rabson 			cn += l + 1;
82327a0bc89SDoug Rabson 			continue;
82427a0bc89SDoug Rabson 		}
82527a0bc89SDoug Rabson 		cn += N_INUSEBITS - cn % N_INUSEBITS;
82627a0bc89SDoug Rabson 	}
82727a0bc89SDoug Rabson 	for (cn = 0; cn < newst;) {
82827a0bc89SDoug Rabson 		idx = cn / N_INUSEBITS;
82927a0bc89SDoug Rabson 		map = pmp->pm_inusemap[idx];
83027a0bc89SDoug Rabson 		map |= (1 << (cn % N_INUSEBITS)) - 1;
83127a0bc89SDoug Rabson 		if (map != (u_int)-1) {
83227a0bc89SDoug Rabson 			cn = idx * N_INUSEBITS + ffs(map^(u_int)-1) - 1;
83327a0bc89SDoug Rabson 			if ((l = chainlength(pmp, cn, count)) >= count)
834952a6212SJordan K. Hubbard 				return (chainalloc(pmp, cn, count, fillwith, retcluster, got));
83527a0bc89SDoug Rabson 			if (l > foundl) {
83627a0bc89SDoug Rabson 				foundcn = cn;
83727a0bc89SDoug Rabson 				foundl = l;
83827a0bc89SDoug Rabson 			}
83927a0bc89SDoug Rabson 			cn += l + 1;
84027a0bc89SDoug Rabson 			continue;
84127a0bc89SDoug Rabson 		}
84227a0bc89SDoug Rabson 		cn += N_INUSEBITS - cn % N_INUSEBITS;
84327a0bc89SDoug Rabson 	}
84427a0bc89SDoug Rabson 
84527a0bc89SDoug Rabson 	if (!foundl)
846952a6212SJordan K. Hubbard 		return (ENOSPC);
84727a0bc89SDoug Rabson 
84827a0bc89SDoug Rabson 	if (len)
849952a6212SJordan K. Hubbard 		return (chainalloc(pmp, start, len, fillwith, retcluster, got));
85027a0bc89SDoug Rabson 	else
851952a6212SJordan K. Hubbard 		return (chainalloc(pmp, foundcn, foundl, fillwith, retcluster, got));
85227a0bc89SDoug Rabson }
85327a0bc89SDoug Rabson 
85427a0bc89SDoug Rabson 
85527a0bc89SDoug Rabson /*
85627a0bc89SDoug Rabson  * Free a chain of clusters.
85727a0bc89SDoug Rabson  *
85827a0bc89SDoug Rabson  * pmp		- address of the msdosfs mount structure for the filesystem
85927a0bc89SDoug Rabson  *		  containing the cluster chain to be freed.
86027a0bc89SDoug Rabson  * startcluster - number of the 1st cluster in the chain of clusters to be
86127a0bc89SDoug Rabson  *		  freed.
86227a0bc89SDoug Rabson  */
86327a0bc89SDoug Rabson int
86427a0bc89SDoug Rabson freeclusterchain(pmp, cluster)
86527a0bc89SDoug Rabson 	struct msdosfsmount *pmp;
86627a0bc89SDoug Rabson 	u_long cluster;
86727a0bc89SDoug Rabson {
868952a6212SJordan K. Hubbard 	int error;
86927a0bc89SDoug Rabson 	struct buf *bp = NULL;
87027a0bc89SDoug Rabson 	u_long bn, bo, bsize, byteoffset;
87127a0bc89SDoug Rabson 	u_long readcn, lbn = -1;
87227a0bc89SDoug Rabson 
87327a0bc89SDoug Rabson 	while (cluster >= CLUST_FIRST && cluster <= pmp->pm_maxcluster) {
87427a0bc89SDoug Rabson 		byteoffset = FATOFS(pmp, cluster);
87527a0bc89SDoug Rabson 		fatblock(pmp, byteoffset, &bn, &bsize, &bo);
87627a0bc89SDoug Rabson 		if (lbn != bn) {
87727a0bc89SDoug Rabson 			if (bp)
87824d4540cSBruce Evans 				updatefats(pmp, bp, lbn);
879c3c6d51eSPoul-Henning Kamp 			error = bread(pmp->pm_devvp, bn, bsize, NOCRED, &bp);
880952a6212SJordan K. Hubbard 			if (error) {
881952a6212SJordan K. Hubbard 				brelse(bp);
882952a6212SJordan K. Hubbard 				return (error);
883952a6212SJordan K. Hubbard 			}
88427a0bc89SDoug Rabson 			lbn = bn;
88527a0bc89SDoug Rabson 		}
88627a0bc89SDoug Rabson 		usemap_free(pmp, cluster);
887952a6212SJordan K. Hubbard 		switch (pmp->pm_fatmask) {
888952a6212SJordan K. Hubbard 		case FAT12_MASK:
88927a0bc89SDoug Rabson 			readcn = getushort(&bp->b_data[bo]);
89027a0bc89SDoug Rabson 			if (cluster & 1) {
89127a0bc89SDoug Rabson 				cluster = readcn >> 4;
89227a0bc89SDoug Rabson 				readcn &= 0x000f;
89327a0bc89SDoug Rabson 				readcn |= MSDOSFSFREE << 4;
89427a0bc89SDoug Rabson 			} else {
89527a0bc89SDoug Rabson 				cluster = readcn;
89627a0bc89SDoug Rabson 				readcn &= 0xf000;
89727a0bc89SDoug Rabson 				readcn |= MSDOSFSFREE & 0xfff;
89827a0bc89SDoug Rabson 			}
89927a0bc89SDoug Rabson 			putushort(&bp->b_data[bo], readcn);
900952a6212SJordan K. Hubbard 			break;
901952a6212SJordan K. Hubbard 		case FAT16_MASK:
902952a6212SJordan K. Hubbard 			cluster = getushort(&bp->b_data[bo]);
90327a0bc89SDoug Rabson 			putushort(&bp->b_data[bo], MSDOSFSFREE);
904952a6212SJordan K. Hubbard 			break;
905952a6212SJordan K. Hubbard 		case FAT32_MASK:
906952a6212SJordan K. Hubbard 			cluster = getulong(&bp->b_data[bo]);
907952a6212SJordan K. Hubbard 			putulong(&bp->b_data[bo],
908952a6212SJordan K. Hubbard 				 (MSDOSFSFREE & FAT32_MASK) | (cluster & ~FAT32_MASK));
909952a6212SJordan K. Hubbard 			break;
91027a0bc89SDoug Rabson 		}
911952a6212SJordan K. Hubbard 		cluster &= pmp->pm_fatmask;
912952a6212SJordan K. Hubbard 		if ((cluster | ~pmp->pm_fatmask) >= CLUST_RSRVD)
913952a6212SJordan K. Hubbard 			cluster |= pmp->pm_fatmask;
91427a0bc89SDoug Rabson 	}
91527a0bc89SDoug Rabson 	if (bp)
91627a0bc89SDoug Rabson 		updatefats(pmp, bp, bn);
917952a6212SJordan K. Hubbard 	return (0);
91827a0bc89SDoug Rabson }
91927a0bc89SDoug Rabson 
92027a0bc89SDoug Rabson /*
92127a0bc89SDoug Rabson  * Read in fat blocks looking for free clusters. For every free cluster
92227a0bc89SDoug Rabson  * found turn off its corresponding bit in the pm_inusemap.
92327a0bc89SDoug Rabson  */
92427a0bc89SDoug Rabson int
92527a0bc89SDoug Rabson fillinusemap(pmp)
92627a0bc89SDoug Rabson 	struct msdosfsmount *pmp;
92727a0bc89SDoug Rabson {
92827a0bc89SDoug Rabson 	struct buf *bp = NULL;
92927a0bc89SDoug Rabson 	u_long cn, readcn;
93027a0bc89SDoug Rabson 	int error;
93127a0bc89SDoug Rabson 	u_long bn, bo, bsize, byteoffset;
93227a0bc89SDoug Rabson 
93327a0bc89SDoug Rabson 	/*
93427a0bc89SDoug Rabson 	 * Mark all clusters in use, we mark the free ones in the fat scan
93527a0bc89SDoug Rabson 	 * loop further down.
93627a0bc89SDoug Rabson 	 */
93727a0bc89SDoug Rabson 	for (cn = 0; cn < (pmp->pm_maxcluster + N_INUSEBITS) / N_INUSEBITS; cn++)
93827a0bc89SDoug Rabson 		pmp->pm_inusemap[cn] = (u_int)-1;
93927a0bc89SDoug Rabson 
94027a0bc89SDoug Rabson 	/*
94127a0bc89SDoug Rabson 	 * Figure how many free clusters are in the filesystem by ripping
94227a0bc89SDoug Rabson 	 * through the fat counting the number of entries whose content is
94327a0bc89SDoug Rabson 	 * zero.  These represent free clusters.
94427a0bc89SDoug Rabson 	 */
94527a0bc89SDoug Rabson 	pmp->pm_freeclustercount = 0;
94627a0bc89SDoug Rabson 	for (cn = CLUST_FIRST; cn <= pmp->pm_maxcluster; cn++) {
94727a0bc89SDoug Rabson 		byteoffset = FATOFS(pmp, cn);
94827a0bc89SDoug Rabson 		bo = byteoffset % pmp->pm_fatblocksize;
94927a0bc89SDoug Rabson 		if (!bo || !bp) {
95027a0bc89SDoug Rabson 			/* Read new FAT block */
95127a0bc89SDoug Rabson 			if (bp)
95227a0bc89SDoug Rabson 				brelse(bp);
95327a0bc89SDoug Rabson 			fatblock(pmp, byteoffset, &bn, &bsize, NULL);
95427a0bc89SDoug Rabson 			error = bread(pmp->pm_devvp, bn, bsize, NOCRED, &bp);
955952a6212SJordan K. Hubbard 			if (error) {
956952a6212SJordan K. Hubbard 				brelse(bp);
957952a6212SJordan K. Hubbard 				return (error);
95827a0bc89SDoug Rabson 			}
959952a6212SJordan K. Hubbard 		}
960952a6212SJordan K. Hubbard 		if (FAT32(pmp))
961952a6212SJordan K. Hubbard 			readcn = getulong(&bp->b_data[bo]);
962952a6212SJordan K. Hubbard 		else
96327a0bc89SDoug Rabson 			readcn = getushort(&bp->b_data[bo]);
964952a6212SJordan K. Hubbard 		if (FAT12(pmp) && (cn & 1))
96527a0bc89SDoug Rabson 			readcn >>= 4;
966952a6212SJordan K. Hubbard 		readcn &= pmp->pm_fatmask;
96727a0bc89SDoug Rabson 
96827a0bc89SDoug Rabson 		if (readcn == 0)
96927a0bc89SDoug Rabson 			usemap_free(pmp, cn);
97027a0bc89SDoug Rabson 	}
97127a0bc89SDoug Rabson 	brelse(bp);
972952a6212SJordan K. Hubbard 	return (0);
97327a0bc89SDoug Rabson }
97427a0bc89SDoug Rabson 
97527a0bc89SDoug Rabson /*
97627a0bc89SDoug Rabson  * Allocate a new cluster and chain it onto the end of the file.
97727a0bc89SDoug Rabson  *
97827a0bc89SDoug Rabson  * dep	 - the file to extend
97927a0bc89SDoug Rabson  * count - number of clusters to allocate
98027a0bc89SDoug Rabson  * bpp	 - where to return the address of the buf header for the first new
98127a0bc89SDoug Rabson  *	   file block
98227a0bc89SDoug Rabson  * ncp	 - where to put cluster number of the first newly allocated cluster
98327a0bc89SDoug Rabson  *	   If this pointer is 0, do not return the cluster number.
98427a0bc89SDoug Rabson  * flags - see fat.h
98527a0bc89SDoug Rabson  *
98627a0bc89SDoug Rabson  * NOTE: This function is not responsible for turning on the DE_UPDATE bit of
98727a0bc89SDoug Rabson  * the de_flag field of the denode and it does not change the de_FileSize
98827a0bc89SDoug Rabson  * field.  This is left for the caller to do.
98927a0bc89SDoug Rabson  */
99027a0bc89SDoug Rabson int
99127a0bc89SDoug Rabson extendfile(dep, count, bpp, ncp, flags)
99227a0bc89SDoug Rabson 	struct denode *dep;
99327a0bc89SDoug Rabson 	u_long count;
99427a0bc89SDoug Rabson 	struct buf **bpp;
99527a0bc89SDoug Rabson 	u_long *ncp;
99627a0bc89SDoug Rabson 	int flags;
99727a0bc89SDoug Rabson {
998952a6212SJordan K. Hubbard 	int error;
99927a0bc89SDoug Rabson 	u_long frcn;
100027a0bc89SDoug Rabson 	u_long cn, got;
100127a0bc89SDoug Rabson 	struct msdosfsmount *pmp = dep->de_pmp;
100227a0bc89SDoug Rabson 	struct buf *bp;
10030d2af521SKirk McKusick 	daddr_t blkno;
100427a0bc89SDoug Rabson 
100527a0bc89SDoug Rabson 	/*
100627a0bc89SDoug Rabson 	 * Don't try to extend the root directory
100727a0bc89SDoug Rabson 	 */
1008952a6212SJordan K. Hubbard 	if (dep->de_StartCluster == MSDOSFSROOT
1009952a6212SJordan K. Hubbard 	    && (dep->de_Attributes & ATTR_DIRECTORY)) {
101027a0bc89SDoug Rabson 		printf("extendfile(): attempt to extend root directory\n");
1011952a6212SJordan K. Hubbard 		return (ENOSPC);
101227a0bc89SDoug Rabson 	}
101327a0bc89SDoug Rabson 
101427a0bc89SDoug Rabson 	/*
101527a0bc89SDoug Rabson 	 * If the "file's last cluster" cache entry is empty, and the file
101627a0bc89SDoug Rabson 	 * is not empty, then fill the cache entry by calling pcbmap().
101727a0bc89SDoug Rabson 	 */
101827a0bc89SDoug Rabson 	fc_fileextends++;
101927a0bc89SDoug Rabson 	if (dep->de_fc[FC_LASTFC].fc_frcn == FCE_EMPTY &&
102027a0bc89SDoug Rabson 	    dep->de_StartCluster != 0) {
102127a0bc89SDoug Rabson 		fc_lfcempty++;
1022952a6212SJordan K. Hubbard 		error = pcbmap(dep, 0xffff, 0, &cn, 0);
102327a0bc89SDoug Rabson 		/* we expect it to return E2BIG */
102427a0bc89SDoug Rabson 		if (error != E2BIG)
1025952a6212SJordan K. Hubbard 			return (error);
102627a0bc89SDoug Rabson 	}
102727a0bc89SDoug Rabson 
102827a0bc89SDoug Rabson 	while (count > 0) {
102927a0bc89SDoug Rabson 		/*
1030952a6212SJordan K. Hubbard 		 * Allocate a new cluster chain and cat onto the end of the
1031952a6212SJordan K. Hubbard 		 * file.  * If the file is empty we make de_StartCluster point
1032952a6212SJordan K. Hubbard 		 * to the new block.  Note that de_StartCluster being 0 is
1033952a6212SJordan K. Hubbard 		 * sufficient to be sure the file is empty since we exclude
1034952a6212SJordan K. Hubbard 		 * attempts to extend the root directory above, and the root
1035952a6212SJordan K. Hubbard 		 * dir is the only file with a startcluster of 0 that has
1036952a6212SJordan K. Hubbard 		 * blocks allocated (sort of).
103727a0bc89SDoug Rabson 		 */
103827a0bc89SDoug Rabson 		if (dep->de_StartCluster == 0)
103927a0bc89SDoug Rabson 			cn = 0;
104027a0bc89SDoug Rabson 		else
104127a0bc89SDoug Rabson 			cn = dep->de_fc[FC_LASTFC].fc_fsrcn + 1;
1042c3c6d51eSPoul-Henning Kamp 		error = clusteralloc(pmp, cn, count, CLUST_EOFE, &cn, &got);
1043c3c6d51eSPoul-Henning Kamp 		if (error)
1044952a6212SJordan K. Hubbard 			return (error);
104527a0bc89SDoug Rabson 
104627a0bc89SDoug Rabson 		count -= got;
104727a0bc89SDoug Rabson 
104827a0bc89SDoug Rabson 		/*
104927a0bc89SDoug Rabson 		 * Give them the filesystem relative cluster number if they want
105027a0bc89SDoug Rabson 		 * it.
105127a0bc89SDoug Rabson 		 */
105227a0bc89SDoug Rabson 		if (ncp) {
105327a0bc89SDoug Rabson 			*ncp = cn;
105427a0bc89SDoug Rabson 			ncp = NULL;
105527a0bc89SDoug Rabson 		}
105627a0bc89SDoug Rabson 
105727a0bc89SDoug Rabson 		if (dep->de_StartCluster == 0) {
105827a0bc89SDoug Rabson 			dep->de_StartCluster = cn;
105927a0bc89SDoug Rabson 			frcn = 0;
106027a0bc89SDoug Rabson 		} else {
1061952a6212SJordan K. Hubbard 			error = fatentry(FAT_SET, pmp,
1062952a6212SJordan K. Hubbard 					 dep->de_fc[FC_LASTFC].fc_fsrcn,
106327a0bc89SDoug Rabson 					 0, cn);
106427a0bc89SDoug Rabson 			if (error) {
106527a0bc89SDoug Rabson 				clusterfree(pmp, cn, NULL);
1066952a6212SJordan K. Hubbard 				return (error);
106727a0bc89SDoug Rabson 			}
106827a0bc89SDoug Rabson 			frcn = dep->de_fc[FC_LASTFC].fc_frcn + 1;
106927a0bc89SDoug Rabson 		}
107027a0bc89SDoug Rabson 
107127a0bc89SDoug Rabson 		/*
107227a0bc89SDoug Rabson 		 * Update the "last cluster of the file" entry in the denode's fat
107327a0bc89SDoug Rabson 		 * cache.
107427a0bc89SDoug Rabson 		 */
107527a0bc89SDoug Rabson 		fc_setcache(dep, FC_LASTFC, frcn + got - 1, cn + got - 1);
107627a0bc89SDoug Rabson 
107727a0bc89SDoug Rabson 		if (flags & DE_CLEAR) {
107827a0bc89SDoug Rabson 			while (got-- > 0) {
107927a0bc89SDoug Rabson 				/*
108027a0bc89SDoug Rabson 				 * Get the buf header for the new block of the file.
108127a0bc89SDoug Rabson 				 */
108227a0bc89SDoug Rabson 				if (dep->de_Attributes & ATTR_DIRECTORY)
10837261f5f6SJeff Roberson 					bp = getblk(pmp->pm_devvp,
10847261f5f6SJeff Roberson 						    cntobn(pmp, cn++),
10857261f5f6SJeff Roberson 						    pmp->pm_bpcluster, 0, 0, 0);
108627a0bc89SDoug Rabson 				else {
10877261f5f6SJeff Roberson 					bp = getblk(DETOV(dep),
10887261f5f6SJeff Roberson 					    de_cn2bn(pmp, frcn++),
10897261f5f6SJeff Roberson 					    pmp->pm_bpcluster, 0, 0, 0);
109027a0bc89SDoug Rabson 					/*
109127a0bc89SDoug Rabson 					 * Do the bmap now, as in msdosfs_write
109227a0bc89SDoug Rabson 					 */
1093952a6212SJordan K. Hubbard 					if (pcbmap(dep,
1094952a6212SJordan K. Hubbard 					    de_bn2cn(pmp, bp->b_lblkno),
10950d2af521SKirk McKusick 					    &blkno, 0, 0))
109627a0bc89SDoug Rabson 						bp->b_blkno = -1;
109727a0bc89SDoug Rabson 					if (bp->b_blkno == -1)
109827a0bc89SDoug Rabson 						panic("extendfile: pcbmap");
10990d2af521SKirk McKusick 					else
11000d2af521SKirk McKusick 						bp->b_blkno = blkno;
110127a0bc89SDoug Rabson 				}
110227a0bc89SDoug Rabson 				clrbuf(bp);
110327a0bc89SDoug Rabson 				if (bpp) {
110427a0bc89SDoug Rabson 					*bpp = bp;
110527a0bc89SDoug Rabson 					bpp = NULL;
1106952a6212SJordan K. Hubbard 				} else
1107952a6212SJordan K. Hubbard 					bdwrite(bp);
110827a0bc89SDoug Rabson 			}
110927a0bc89SDoug Rabson 		}
111027a0bc89SDoug Rabson 	}
111127a0bc89SDoug Rabson 
1112952a6212SJordan K. Hubbard 	return (0);
111327a0bc89SDoug Rabson }
1114cede1f56STom Rhodes 
1115392dbea3SBruce Evans /*-
1116392dbea3SBruce Evans  * Routine to mark a FAT16 or FAT32 volume as "clean" or "dirty" by
1117392dbea3SBruce Evans  * manipulating the upper bit of the FAT entry for cluster 1.  Note that
1118392dbea3SBruce Evans  * this bit is not defined for FAT12 volumes, which are always assumed to
1119392dbea3SBruce Evans  * be dirty.
1120cede1f56STom Rhodes  *
1121392dbea3SBruce Evans  * The fatentry() routine only works on cluster numbers that a file could
1122392dbea3SBruce Evans  * occupy, so it won't manipulate the entry for cluster 1.  So we have to do
1123392dbea3SBruce Evans  * it here.  The code was stolen from fatentry() and tailored for cluster 1.
1124cede1f56STom Rhodes  *
1125cede1f56STom Rhodes  * Inputs:
1126cede1f56STom Rhodes  *	pmp	The MS-DOS volume to mark
1127392dbea3SBruce Evans  *	dirty	Non-zero if the volume should be marked dirty; zero if it
1128392dbea3SBruce Evans  *		should be marked clean
1129cede1f56STom Rhodes  *
1130cede1f56STom Rhodes  * Result:
1131cede1f56STom Rhodes  *	0	Success
1132cede1f56STom Rhodes  *	EROFS	Volume is read-only
1133cede1f56STom Rhodes  *	?	(other errors from called routines)
1134cede1f56STom Rhodes  */
1135392dbea3SBruce Evans int
1136392dbea3SBruce Evans markvoldirty(struct msdosfsmount *pmp, int dirty)
1137cede1f56STom Rhodes {
1138cede1f56STom Rhodes 	struct buf *bp;
1139392dbea3SBruce Evans 	u_long bn, bo, bsize, byteoffset, fatval;
1140392dbea3SBruce Evans 	int error;
1141cede1f56STom Rhodes 
1142392dbea3SBruce Evans 	/*
1143392dbea3SBruce Evans 	 * FAT12 does not support a "clean" bit, so don't do anything for
1144392dbea3SBruce Evans 	 * FAT12.
1145392dbea3SBruce Evans 	 */
1146cede1f56STom Rhodes 	if (FAT12(pmp))
1147392dbea3SBruce Evans 		return (0);
1148cede1f56STom Rhodes 
1149392dbea3SBruce Evans 	/* Can't change the bit on a read-only filesystem. */
1150cede1f56STom Rhodes 	if (pmp->pm_flags & MSDOSFSMNT_RONLY)
1151392dbea3SBruce Evans 		return (EROFS);
1152cede1f56STom Rhodes 
1153392dbea3SBruce Evans 	/*
1154392dbea3SBruce Evans 	 * Fetch the block containing the FAT entry.  It is given by the
1155392dbea3SBruce Evans 	 * pseudo-cluster 1.
1156392dbea3SBruce Evans 	 */
1157392dbea3SBruce Evans 	byteoffset = FATOFS(pmp, 1);
1158cede1f56STom Rhodes 	fatblock(pmp, byteoffset, &bn, &bsize, &bo);
1159cede1f56STom Rhodes 	error = bread(pmp->pm_devvp, bn, bsize, NOCRED, &bp);
1160cede1f56STom Rhodes 	if (error) {
1161cede1f56STom Rhodes 		brelse(bp);
1162cede1f56STom Rhodes 		return (error);
1163cede1f56STom Rhodes 	}
1164cede1f56STom Rhodes 
1165392dbea3SBruce Evans 	/*
1166392dbea3SBruce Evans 	 * Get the current value of the FAT entry and set/clear the relevant
1167392dbea3SBruce Evans 	 * bit.  Dirty means clear the "clean" bit; clean means set the
1168392dbea3SBruce Evans 	 * "clean" bit.
1169392dbea3SBruce Evans 	 */
1170cede1f56STom Rhodes 	if (FAT32(pmp)) {
1171392dbea3SBruce Evans 		/* FAT32 uses bit 27. */
1172cede1f56STom Rhodes 		fatval = getulong(&bp->b_data[bo]);
1173cede1f56STom Rhodes 		if (dirty)
1174392dbea3SBruce Evans 			fatval &= 0xF7FFFFFF;
1175cede1f56STom Rhodes 		else
1176392dbea3SBruce Evans 			fatval |= 0x08000000;
1177cede1f56STom Rhodes 		putulong(&bp->b_data[bo], fatval);
1178392dbea3SBruce Evans 	} else {
1179392dbea3SBruce Evans 		/* Must be FAT16; use bit 15. */
1180cede1f56STom Rhodes 		fatval = getushort(&bp->b_data[bo]);
1181cede1f56STom Rhodes 		if (dirty)
1182392dbea3SBruce Evans 			fatval &= 0x7FFF;
1183cede1f56STom Rhodes 		else
1184392dbea3SBruce Evans 			fatval |= 0x8000;
1185cede1f56STom Rhodes 		putushort(&bp->b_data[bo], fatval);
1186cede1f56STom Rhodes 	}
1187cede1f56STom Rhodes 
1188392dbea3SBruce Evans 	/* Write out the modified FAT block synchronously. */
1189392dbea3SBruce Evans 	return (bwrite(bp));
1190cede1f56STom Rhodes }
1191