Home | History | Annotate | Line # | Download | only in msdosfs
msdosfs_fat.c revision 1.31.10.1
      1 /*	$NetBSD: msdosfs_fat.c,v 1.31.10.1 2018/02/04 12:10:48 martin Exp $	*/
      2 
      3 /*-
      4  * Copyright (C) 1994, 1995, 1997 Wolfgang Solfrank.
      5  * Copyright (C) 1994, 1995, 1997 TooLs GmbH.
      6  * All rights reserved.
      7  * Original code by Paul Popelka (paulp (at) uts.amdahl.com) (see below).
      8  *
      9  * Redistribution and use in source and binary forms, with or without
     10  * modification, are permitted provided that the following conditions
     11  * are met:
     12  * 1. Redistributions of source code must retain the above copyright
     13  *    notice, this list of conditions and the following disclaimer.
     14  * 2. Redistributions in binary form must reproduce the above copyright
     15  *    notice, this list of conditions and the following disclaimer in the
     16  *    documentation and/or other materials provided with the distribution.
     17  * 3. All advertising materials mentioning features or use of this software
     18  *    must display the following acknowledgement:
     19  *	This product includes software developed by TooLs GmbH.
     20  * 4. The name of TooLs GmbH may not be used to endorse or promote products
     21  *    derived from this software without specific prior written permission.
     22  *
     23  * THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``AS IS'' AND ANY EXPRESS OR
     24  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     25  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     26  * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
     27  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
     28  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
     29  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
     30  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
     31  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
     32  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     33  */
     34 /*
     35  * Written by Paul Popelka (paulp (at) uts.amdahl.com)
     36  *
     37  * You can do anything you want with this software, just don't say you wrote
     38  * it, and don't remove this notice.
     39  *
     40  * This software is provided "as is".
     41  *
     42  * The author supplies this software to be publicly redistributed on the
     43  * understanding that the author is not responsible for the correct
     44  * functioning of this software in any circumstances and is not liable for
     45  * any damages caused by this software.
     46  *
     47  * October 1992
     48  */
     49 
     50 #if HAVE_NBTOOL_CONFIG_H
     51 #include "nbtool_config.h"
     52 #endif
     53 
     54 #include <sys/cdefs.h>
     55 __KERNEL_RCSID(0, "$NetBSD: msdosfs_fat.c,v 1.31.10.1 2018/02/04 12:10:48 martin Exp $");
     56 
     57 /*
     58  * kernel include files.
     59  */
     60 #include <sys/param.h>
     61 #include <sys/file.h>
     62 #ifdef _KERNEL
     63 #include <sys/mount.h>		/* to define statvfs structure */
     64 #include <sys/errno.h>
     65 #include <sys/systm.h>
     66 #include <sys/kauth.h>
     67 #include <sys/dirent.h>
     68 #include <sys/namei.h>
     69 #include <sys/buf.h>
     70 #include <sys/vnode.h>		/* to define vattr structure */
     71 #else
     72 #include <strings.h>
     73 #include <ffs/buf.h>
     74 #endif
     75 
     76 /*
     77  * msdosfs include files.
     78  */
     79 #include <fs/msdosfs/bpb.h>
     80 #include <fs/msdosfs/msdosfsmount.h>
     81 #include <fs/msdosfs/direntry.h>
     82 #include <fs/msdosfs/denode.h>
     83 #include <fs/msdosfs/fat.h>
     84 
     85 /*
     86  * Fat cache stats.
     87  */
     88 int fc_fileextends;		/* # of file extends			 */
     89 int fc_lfcempty;		/* # of time last file cluster cache entry
     90 				 * was empty */
     91 int fc_bmapcalls;		/* # of times pcbmap was called		 */
     92 
     93 #define	LMMAX	20
     94 int fc_lmdistance[LMMAX];	/* counters for how far off the last
     95 				 * cluster mapped entry was. */
     96 int fc_largedistance;		/* off by more than LMMAX		 */
     97 int fc_wherefrom, fc_whereto, fc_lastclust;
     98 int pm_fatblocksize;
     99 
    100 #ifdef MSDOSFS_DEBUG
    101 #define DPRINTF(a) printf a
    102 #else
    103 #define DPRINTF(a)
    104 #endif
    105 #ifdef MSDOSFS_DEBUG
    106 void print_fat_stats(void);
    107 
    108 void
    109 print_fat_stats(void)
    110 {
    111 	int i;
    112 
    113 	printf("fc_fileextends=%d fc_lfcempty=%d fc_bmapcalls=%d "
    114 	    "fc_largedistance=%d [%d->%d=%d] fc_lastclust=%d pm_fatblocksize=%d\n",
    115 	    fc_fileextends, fc_lfcempty, fc_bmapcalls, fc_largedistance,
    116 	    fc_wherefrom, fc_whereto, fc_whereto-fc_wherefrom,
    117 	    fc_lastclust, pm_fatblocksize);
    118 
    119 	fc_fileextends = fc_lfcempty = fc_bmapcalls = 0;
    120 	fc_wherefrom = fc_whereto = fc_lastclust = 0;
    121 
    122 	for (i = 0; i < LMMAX; i++) {
    123 		printf("%d:%d ", i, fc_lmdistance[i]);
    124 	fc_lmdistance[i] = 0;
    125 	}
    126 
    127 	printf("\n");
    128 }
    129 #endif
    130 
    131 static void fatblock(struct msdosfsmount *, u_long, u_long *, u_long *,
    132 			  u_long *);
    133 void updatefats(struct msdosfsmount *, struct buf *, u_long);
    134 static inline void usemap_free(struct msdosfsmount *, u_long);
    135 static inline void usemap_alloc(struct msdosfsmount *, u_long);
    136 static int fatchain(struct msdosfsmount *, u_long, u_long, u_long);
    137 int chainlength(struct msdosfsmount *, u_long, u_long);
    138 int chainalloc(struct msdosfsmount *, u_long, u_long, u_long, u_long *,
    139 		    u_long *);
    140 
    141 static void
    142 fatblock(struct msdosfsmount *pmp, u_long ofs, u_long *bnp, u_long *sizep, u_long *bop)
    143 {
    144 	u_long bn, size;
    145 
    146 	bn = ofs / pmp->pm_fatblocksize * pmp->pm_fatblocksec;
    147 	size = min(pmp->pm_fatblocksec, pmp->pm_FATsecs - bn)
    148 	    * pmp->pm_BytesPerSec;
    149 	bn += pmp->pm_fatblk + pmp->pm_curfat * pmp->pm_FATsecs;
    150 
    151 	DPRINTF(("%s(ofs=%lu bn=%lu, size=%lu, bo=%lu)\n", __func__, ofs, bn,
    152 	    size, ofs % pmp->pm_fatblocksize));
    153 	if (bnp)
    154 		*bnp = bn;
    155 	if (sizep)
    156 		*sizep = size;
    157 	if (bop)
    158 		*bop = ofs % pmp->pm_fatblocksize;
    159 
    160 	pm_fatblocksize = pmp->pm_fatblocksize;
    161 }
    162 
    163 /*
    164  * Map the logical cluster number of a file into a physical disk sector
    165  * that is filesystem relative.
    166  *
    167  * dep	  - address of denode representing the file of interest
    168  * findcn - file relative cluster whose filesystem relative cluster number
    169  *	    and/or block number are/is to be found
    170  * bnp	  - address of where to place the file system relative block number.
    171  *	    If this pointer is null then don't return this quantity.
    172  * cnp	  - address of where to place the file system relative cluster number.
    173  *	    If this pointer is null then don't return this quantity.
    174  *
    175  * NOTE: Either bnp or cnp must be non-null.
    176  * This function has one side effect.  If the requested file relative cluster
    177  * is beyond the end of file, then the actual number of clusters in the file
    178  * is returned in *cnp.  This is useful for determining how long a directory is.
    179  *  If cnp is null, nothing is returned.
    180  */
    181 int
    182 pcbmap(struct denode *dep, u_long findcn, daddr_t *bnp, u_long *cnp, int *sp)
    183 	/* findcn:		 file relative cluster to get		 */
    184 	/* bnp:		 returned filesys rel sector number	 */
    185 	/* cnp:		 returned cluster number		 */
    186 	/* sp:		 returned block size			 */
    187 {
    188 	int error;
    189 	u_long i;
    190 	u_long cn;
    191 	u_long prevcn = 0; /* XXX: prevcn could be used unititialized */
    192 	u_long byteoffset;
    193 	u_long bn;
    194 	u_long bo;
    195 	struct buf *bp = NULL;
    196 	u_long bp_bn = -1;
    197 	struct msdosfsmount *pmp = dep->de_pmp;
    198 	u_long bsize;
    199 
    200 	fc_bmapcalls++;
    201 
    202 	/*
    203 	 * If they don't give us someplace to return a value then don't
    204 	 * bother doing anything.
    205 	 */
    206 	if (bnp == NULL && cnp == NULL && sp == NULL)
    207 		return (0);
    208 
    209 	cn = dep->de_StartCluster;
    210 	DPRINTF(("%s(start cluster=%lu)\n", __func__, cn));
    211 	/*
    212 	 * The "file" that makes up the root directory is contiguous,
    213 	 * permanently allocated, of fixed size, and is not made up of
    214 	 * clusters.  If the cluster number is beyond the end of the root
    215 	 * directory, then return the number of clusters in the file.
    216 	 */
    217 	if (cn == MSDOSFSROOT) {
    218 		if (dep->de_Attributes & ATTR_DIRECTORY) {
    219 			if (de_cn2off(pmp, findcn) >= dep->de_FileSize) {
    220 				if (cnp)
    221 					*cnp = de_bn2cn(pmp, pmp->pm_rootdirsize);
    222 				DPRINTF(("%s(root, %lu ETOOBIG)\n", __func__,
    223 				    de_cn2off(pmp, findcn)));
    224 				return (E2BIG);
    225 			}
    226 			if (bnp)
    227 				*bnp = pmp->pm_rootdirblk + de_cn2bn(pmp, findcn);
    228 			if (cnp)
    229 				*cnp = MSDOSFSROOT;
    230 			if (sp)
    231 				*sp = min(pmp->pm_bpcluster,
    232 				    dep->de_FileSize - de_cn2off(pmp, findcn));
    233 			DPRINTF(("%s(root, bn=%lu, cn=%u)\n", __func__,
    234 			    pmp->pm_rootdirblk + de_cn2bn(pmp, findcn),
    235 			    MSDOSFSROOT));
    236 			return (0);
    237 		} else {		/* just an empty file */
    238 			if (cnp)
    239 				*cnp = 0;
    240 			DPRINTF(("%s(root, empty ETOOBIG)\n", __func__));
    241 			return (E2BIG);
    242 		}
    243 	}
    244 
    245 	/*
    246 	 * All other files do I/O in cluster sized blocks
    247 	 */
    248 	if (sp)
    249 		*sp = pmp->pm_bpcluster;
    250 
    251 	/*
    252 	 * Rummage around in the FAT cache, maybe we can avoid tromping
    253 	 * thru every FAT entry for the file. And, keep track of how far
    254 	 * off the cache was from where we wanted to be.
    255 	 */
    256 	i = 0;
    257 	fc_lookup(dep, findcn, &i, &cn);
    258 	DPRINTF(("%s(bpcluster=%lu i=%lu cn=%lu\n", __func__, pmp->pm_bpcluster,
    259 	    i, cn));
    260 	if ((bn = findcn - i) >= LMMAX) {
    261 		fc_largedistance++;
    262 		fc_wherefrom = i;
    263 		fc_whereto = findcn;
    264 		fc_lastclust = dep->de_fc[FC_LASTFC].fc_frcn;
    265 	} else
    266 		fc_lmdistance[bn]++;
    267 
    268 	/*
    269 	 * Handle all other files or directories the normal way.
    270 	 */
    271 	for (; i < findcn; i++) {
    272 		/*
    273 		 * Stop with all reserved clusters, not just with EOF.
    274 		 */
    275 		if (cn >= (CLUST_RSRVD & pmp->pm_fatmask))
    276 			goto hiteof;
    277 
    278 		/*
    279 		 * Also stop when cluster is not in the filesystem
    280 		 */
    281 		if (cn < CLUST_FIRST || cn > pmp->pm_maxcluster) {
    282 			DPRINTF(("%s(cn, %lu not in %lu..%lu)\n", __func__,
    283 				cn, (u_long)CLUST_FIRST, pmp->pm_maxcluster));
    284 			if (bp)
    285 				brelse(bp, 0);
    286 			return (EINVAL);
    287 		}
    288 
    289 		byteoffset = FATOFS(pmp, cn);
    290 		fatblock(pmp, byteoffset, &bn, &bsize, &bo);
    291 		if (bn != bp_bn) {
    292 			if (bp)
    293 				brelse(bp, 0);
    294 			error = bread(pmp->pm_devvp, de_bn2kb(pmp, bn), bsize,
    295 			    0, &bp);
    296 			if (error) {
    297 				DPRINTF(("%s(bread, %d)\n", __func__, error));
    298 				return (error);
    299 			}
    300 			bp_bn = bn;
    301 		}
    302 		prevcn = cn;
    303 		if (bo >= bsize) {
    304 			if (bp)
    305 				brelse(bp, 0);
    306 			DPRINTF(("%s(block, %lu >= %lu)\n", __func__, bo,
    307 			    bsize));
    308 			return (EIO);
    309 		}
    310 		KASSERT(bp != NULL);
    311 		if (FAT32(pmp))
    312 			cn = getulong((char *)bp->b_data + bo);
    313 		else
    314 			cn = getushort((char *)bp->b_data + bo);
    315 		if (FAT12(pmp) && (prevcn & 1))
    316 			cn >>= 4;
    317 		DPRINTF(("%s(cn=%lu masked=%lu)\n", __func__, cn,
    318 		    cn & pmp->pm_fatmask));
    319 		cn &= pmp->pm_fatmask;
    320 	}
    321 
    322 	if (!MSDOSFSEOF(cn, pmp->pm_fatmask)) {
    323 		if (bp)
    324 			brelse(bp, 0);
    325 		if (bnp)
    326 			*bnp = cntobn(pmp, cn);
    327 		if (cnp)
    328 			*cnp = cn;
    329 		DPRINTF(("%s(bn=%lu, cn=%lu)\n", __func__, cntobn(pmp, cn),
    330 		    cn));
    331 		fc_setcache(dep, FC_LASTMAP, i, cn);
    332 		return (0);
    333 	}
    334 
    335 hiteof:;
    336 	if (cnp)
    337 		*cnp = i;
    338 	if (bp)
    339 		brelse(bp, 0);
    340 	/* update last file cluster entry in the FAT cache */
    341 	fc_setcache(dep, FC_LASTFC, i - 1, prevcn);
    342 	DPRINTF(("%s(eof, %lu)\n", __func__, i));
    343 	return (E2BIG);
    344 }
    345 
    346 /*
    347  * Find the closest entry in the FAT cache to the cluster we are looking
    348  * for.
    349  */
    350 void
    351 fc_lookup(struct denode *dep, u_long findcn, u_long *frcnp, u_long *fsrcnp)
    352 {
    353 	int i;
    354 	u_long cn;
    355 	struct fatcache *closest = 0;
    356 
    357 	for (i = 0; i < FC_SIZE; i++) {
    358 		cn = dep->de_fc[i].fc_frcn;
    359 		if (cn != FCE_EMPTY && cn <= findcn) {
    360 			if (closest == 0 || cn > closest->fc_frcn)
    361 				closest = &dep->de_fc[i];
    362 		}
    363 	}
    364 	if (closest) {
    365 		*frcnp = closest->fc_frcn;
    366 		*fsrcnp = closest->fc_fsrcn;
    367 	}
    368 }
    369 
    370 /*
    371  * Purge the FAT cache in denode dep of all entries relating to file
    372  * relative cluster frcn and beyond.
    373  */
    374 void
    375 fc_purge(struct denode *dep, u_int frcn)
    376 {
    377 	int i;
    378 	struct fatcache *fcp;
    379 
    380 	fcp = dep->de_fc;
    381 	for (i = 0; i < FC_SIZE; i++, fcp++) {
    382 		if (fcp->fc_frcn >= frcn)
    383 			fcp->fc_frcn = FCE_EMPTY;
    384 	}
    385 }
    386 
    387 /*
    388  * Update the FAT.
    389  * If mirroring the FAT, update all copies, with the first copy as last.
    390  * Else update only the current FAT (ignoring the others).
    391  *
    392  * pmp	 - msdosfsmount structure for filesystem to update
    393  * bp	 - addr of modified FAT block
    394  * fatbn - block number relative to begin of filesystem of the modified FAT block.
    395  */
    396 void
    397 updatefats(struct msdosfsmount *pmp, struct buf *bp, u_long fatbn)
    398 {
    399 	int i, error;
    400 	struct buf *bpn;
    401 
    402 	DPRINTF(("%s(pmp %p, bp %p, fatbn %lu)\n", __func__, pmp, bp, fatbn));
    403 
    404 	/*
    405 	 * If we have an FSInfo block, update it.
    406 	 */
    407 	if (pmp->pm_fsinfo) {
    408 		u_long cn = pmp->pm_nxtfree;
    409 
    410 		if (pmp->pm_freeclustercount
    411 		    && (pmp->pm_inusemap[cn / N_INUSEBITS]
    412 			& (1 << (cn % N_INUSEBITS)))) {
    413 			/*
    414 			 * The cluster indicated in FSInfo isn't free
    415 			 * any longer.  Got get a new free one.
    416 			 */
    417 			for (cn = 0; cn < pmp->pm_maxcluster; cn++)
    418 				if (pmp->pm_inusemap[cn / N_INUSEBITS] != (u_int)-1)
    419 					break;
    420 			pmp->pm_nxtfree = cn
    421 				+ ffs(pmp->pm_inusemap[cn / N_INUSEBITS]
    422 				      ^ (u_int)-1) - 1;
    423 		}
    424 		/*
    425 		 * XXX  If the fsinfo block is stored on media with
    426 		 *      2KB or larger sectors, is the fsinfo structure
    427 		 *      padded at the end or in the middle?
    428 		 */
    429 		if (bread(pmp->pm_devvp, de_bn2kb(pmp, pmp->pm_fsinfo),
    430 		    pmp->pm_BytesPerSec, B_MODIFY, &bpn) != 0) {
    431 			/*
    432 			 * Ignore the error, but turn off FSInfo update for the future.
    433 			 */
    434 			pmp->pm_fsinfo = 0;
    435 		} else {
    436 			struct fsinfo *fp = (struct fsinfo *)bpn->b_data;
    437 
    438 			putulong(fp->fsinfree, pmp->pm_freeclustercount);
    439 			putulong(fp->fsinxtfree, pmp->pm_nxtfree);
    440 			if (pmp->pm_flags & MSDOSFSMNT_WAITONFAT)
    441 				bwrite(bpn);
    442 			else
    443 				bdwrite(bpn);
    444 		}
    445 	}
    446 
    447 	if (pmp->pm_flags & MSDOSFS_FATMIRROR) {
    448 		/*
    449 		 * Now copy the block(s) of the modified FAT to the other copies of
    450 		 * the FAT and write them out.  This is faster than reading in the
    451 		 * other FATs and then writing them back out.  This could tie up
    452 		 * the FAT for quite a while. Preventing others from accessing it.
    453 		 * To prevent us from going after the FAT quite so much we use
    454 		 * delayed writes, unless they specified "synchronous" when the
    455 		 * filesystem was mounted.  If synch is asked for then use
    456 		 * bwrite()'s and really slow things down.
    457 		 */
    458 		for (i = 1; i < pmp->pm_FATs; i++) {
    459 			fatbn += pmp->pm_FATsecs;
    460 			/* getblk() never fails */
    461 			bpn = getblk(pmp->pm_devvp, de_bn2kb(pmp, fatbn),
    462 			    bp->b_bcount, 0, 0);
    463 			memcpy(bpn->b_data, bp->b_data, bp->b_bcount);
    464 			if (pmp->pm_flags & MSDOSFSMNT_WAITONFAT) {
    465 				error = bwrite(bpn);
    466 				if (error)
    467 					printf("%s: copy FAT %d (error=%d)\n",
    468 						 __func__, i, error);
    469 			} else
    470 				bdwrite(bpn);
    471 		}
    472 	}
    473 
    474 	/*
    475 	 * Write out the first (or current) FAT last.
    476 	 */
    477 	if (pmp->pm_flags & MSDOSFSMNT_WAITONFAT) {
    478 		error =  bwrite(bp);
    479 		if (error)
    480 			printf("%s: write FAT (error=%d)\n",
    481 				__func__, error);
    482 	} else
    483 		bdwrite(bp);
    484 	/*
    485 	 * Maybe update fsinfo sector here?
    486 	 */
    487 }
    488 
    489 /*
    490  * Updating entries in 12 bit FATs is a pain in the butt.
    491  *
    492  * The following picture shows where nibbles go when moving from a 12 bit
    493  * cluster number into the appropriate bytes in the FAT.
    494  *
    495  *	byte m        byte m+1      byte m+2
    496  *	+----+----+   +----+----+   +----+----+
    497  *	|  0    1 |   |  2    3 |   |  4    5 |   FAT bytes
    498  *	+----+----+   +----+----+   +----+----+
    499  *
    500  *	+----+----+----+   +----+----+----+
    501  *	|  3    0    1 |   |  4    5    2 |
    502  *	+----+----+----+   +----+----+----+
    503  *	cluster n  	   cluster n+1
    504  *
    505  * Where n is even. m = n + (n >> 2)
    506  *
    507  */
    508 static inline void
    509 usemap_alloc(struct msdosfsmount *pmp, u_long cn)
    510 {
    511 
    512 	pmp->pm_inusemap[cn / N_INUSEBITS] |= 1 << (cn % N_INUSEBITS);
    513 	pmp->pm_freeclustercount--;
    514 }
    515 
    516 static inline void
    517 usemap_free(struct msdosfsmount *pmp, u_long cn)
    518 {
    519 
    520 	pmp->pm_freeclustercount++;
    521 	pmp->pm_inusemap[cn / N_INUSEBITS] &= ~(1 << (cn % N_INUSEBITS));
    522 }
    523 
    524 int
    525 clusterfree(struct msdosfsmount *pmp, u_long cluster, u_long *oldcnp)
    526 {
    527 	int error;
    528 	u_long oldcn;
    529 
    530 	usemap_free(pmp, cluster);
    531 	error = fatentry(FAT_GET_AND_SET, pmp, cluster, &oldcn, MSDOSFSFREE);
    532 	if (error) {
    533 		usemap_alloc(pmp, cluster);
    534 		return (error);
    535 	}
    536 	/*
    537 	 * If the cluster was successfully marked free, then update
    538 	 * the count of free clusters, and turn off the "allocated"
    539 	 * bit in the "in use" cluster bit map.
    540 	 */
    541 	if (oldcnp)
    542 		*oldcnp = oldcn;
    543 	return (0);
    544 }
    545 
    546 /*
    547  * Get or Set or 'Get and Set' the cluster'th entry in the FAT.
    548  *
    549  * function	- whether to get or set a fat entry
    550  * pmp		- address of the msdosfsmount structure for the filesystem
    551  *		  whose FAT is to be manipulated.
    552  * cn		- which cluster is of interest
    553  * oldcontents	- address of a word that is to receive the contents of the
    554  *		  cluster'th entry if this is a get function
    555  * newcontents	- the new value to be written into the cluster'th element of
    556  *		  the FAT if this is a set function.
    557  *
    558  * This function can also be used to free a cluster by setting the FAT entry
    559  * for a cluster to 0.
    560  *
    561  * All copies of the FAT are updated if this is a set function. NOTE: If
    562  * fatentry() marks a cluster as free it does not update the inusemap in
    563  * the msdosfsmount structure. This is left to the caller.
    564  */
    565 int
    566 fatentry(int function, struct msdosfsmount *pmp, u_long cn, u_long *oldcontents, u_long newcontents)
    567 {
    568 	int error;
    569 	u_long readcn;
    570 	u_long bn, bo, bsize, byteoffset;
    571 	struct buf *bp;
    572 
    573 	DPRINTF(("%s(func %d, pmp %p, clust %lu, oldcon %p, newcon " "%lx)\n",
    574 	    __func__, function, pmp, cn, oldcontents, newcontents));
    575 
    576 #ifdef DIAGNOSTIC
    577 	/*
    578 	 * Be sure they asked us to do something.
    579 	 */
    580 	if ((function & (FAT_SET | FAT_GET)) == 0) {
    581 		DPRINTF(("%s(): function code doesn't specify get or set\n",
    582 		    __func__));
    583 		return (EINVAL);
    584 	}
    585 
    586 	/*
    587 	 * If they asked us to return a cluster number but didn't tell us
    588 	 * where to put it, give them an error.
    589 	 */
    590 	if ((function & FAT_GET) && oldcontents == NULL) {
    591 		DPRINTF(("%s(): get function with no place to put result\n",
    592 			__func__));
    593 		return (EINVAL);
    594 	}
    595 #endif
    596 
    597 	/*
    598 	 * Be sure the requested cluster is in the filesystem.
    599 	 */
    600 	if (cn < CLUST_FIRST || cn > pmp->pm_maxcluster)
    601 		return (EINVAL);
    602 
    603 	byteoffset = FATOFS(pmp, cn);
    604 	fatblock(pmp, byteoffset, &bn, &bsize, &bo);
    605 	if ((error = bread(pmp->pm_devvp, de_bn2kb(pmp, bn), bsize,
    606 	    0, &bp)) != 0) {
    607 		return (error);
    608 	}
    609 
    610 	if (function & FAT_GET) {
    611 		if (FAT32(pmp))
    612 			readcn = getulong((char *)bp->b_data + bo);
    613 		else
    614 			readcn = getushort((char *)bp->b_data + bo);
    615 		if (FAT12(pmp) & (cn & 1))
    616 			readcn >>= 4;
    617 		readcn &= pmp->pm_fatmask;
    618 		*oldcontents = readcn;
    619 	}
    620 	if (function & FAT_SET) {
    621 		switch (pmp->pm_fatmask) {
    622 		case FAT12_MASK:
    623 			readcn = getushort((char *)bp->b_data + bo);
    624 			if (cn & 1) {
    625 				readcn &= 0x000f;
    626 				readcn |= newcontents << 4;
    627 			} else {
    628 				readcn &= 0xf000;
    629 				readcn |= newcontents & 0xfff;
    630 			}
    631 			putushort((char *)bp->b_data + bo, readcn);
    632 			break;
    633 		case FAT16_MASK:
    634 			putushort((char *)bp->b_data + bo, newcontents);
    635 			break;
    636 		case FAT32_MASK:
    637 			/*
    638 			 * According to spec we have to retain the
    639 			 * high order bits of the FAT entry.
    640 			 */
    641 			readcn = getulong((char *)bp->b_data + bo);
    642 			readcn &= ~FAT32_MASK;
    643 			readcn |= newcontents & FAT32_MASK;
    644 			putulong((char *)bp->b_data + bo, readcn);
    645 			break;
    646 		}
    647 		updatefats(pmp, bp, bn);
    648 		bp = NULL;
    649 		pmp->pm_fmod = 1;
    650 	}
    651 	if (bp)
    652 		brelse(bp, 0);
    653 	return (0);
    654 }
    655 
    656 /*
    657  * Update a contiguous cluster chain
    658  *
    659  * pmp	    - mount point
    660  * start    - first cluster of chain
    661  * count    - number of clusters in chain
    662  * fillwith - what to write into FAT entry of last cluster
    663  */
    664 static int
    665 fatchain(struct msdosfsmount *pmp, u_long start, u_long count, u_long fillwith)
    666 {
    667 	int error;
    668 	u_long bn, bo, bsize, byteoffset, readcn, newc;
    669 	struct buf *bp;
    670 
    671 	DPRINTF(("%s(pmp %p, start %lu, count %lu, fillwith %lx)\n", __func__,
    672 	    pmp, start, count, fillwith));
    673 	/*
    674 	 * Be sure the clusters are in the filesystem.
    675 	 */
    676 	if (start < CLUST_FIRST || start + count - 1 > pmp->pm_maxcluster)
    677 		return (EINVAL);
    678 
    679 	while (count > 0) {
    680 		byteoffset = FATOFS(pmp, start);
    681 		fatblock(pmp, byteoffset, &bn, &bsize, &bo);
    682 		error = bread(pmp->pm_devvp, de_bn2kb(pmp, bn), bsize,
    683 		    B_MODIFY, &bp);
    684 		if (error) {
    685 			return (error);
    686 		}
    687 		while (count > 0) {
    688 			start++;
    689 			newc = --count > 0 ? start : fillwith;
    690 			switch (pmp->pm_fatmask) {
    691 			case FAT12_MASK:
    692 				readcn = getushort((char *)bp->b_data + bo);
    693 				if (start & 1) {
    694 					readcn &= 0xf000;
    695 					readcn |= newc & 0xfff;
    696 				} else {
    697 					readcn &= 0x000f;
    698 					readcn |= newc << 4;
    699 				}
    700 				putushort((char *)bp->b_data + bo, readcn);
    701 				bo++;
    702 				if (!(start & 1))
    703 					bo++;
    704 				break;
    705 			case FAT16_MASK:
    706 				putushort((char *)bp->b_data + bo, newc);
    707 				bo += 2;
    708 				break;
    709 			case FAT32_MASK:
    710 				readcn = getulong((char *)bp->b_data + bo);
    711 				readcn &= ~pmp->pm_fatmask;
    712 				readcn |= newc & pmp->pm_fatmask;
    713 				putulong((char *)bp->b_data + bo, readcn);
    714 				bo += 4;
    715 				break;
    716 			}
    717 			if (bo >= bsize)
    718 				break;
    719 		}
    720 		updatefats(pmp, bp, bn);
    721 	}
    722 	pmp->pm_fmod = 1;
    723 	return (0);
    724 }
    725 
    726 /*
    727  * Check the length of a free cluster chain starting at start.
    728  *
    729  * pmp	 - mount point
    730  * start - start of chain
    731  * count - maximum interesting length
    732  */
    733 int
    734 chainlength(struct msdosfsmount *pmp, u_long start, u_long count)
    735 {
    736 	u_long idx, max_idx;
    737 	u_int map;
    738 	u_long len;
    739 
    740 	max_idx = pmp->pm_maxcluster / N_INUSEBITS;
    741 	idx = start / N_INUSEBITS;
    742 	start %= N_INUSEBITS;
    743 	map = pmp->pm_inusemap[idx];
    744 	map &= ~((1 << start) - 1);
    745 	if (map) {
    746 		len = ffs(map) - 1 - start;
    747 		return (len > count ? count : len);
    748 	}
    749 	len = N_INUSEBITS - start;
    750 	if (len >= count)
    751 		return (count);
    752 	while (++idx <= max_idx) {
    753 		if (len >= count)
    754 			break;
    755 		if ((map = pmp->pm_inusemap[idx]) != 0) {
    756 			len +=  ffs(map) - 1;
    757 			break;
    758 		}
    759 		len += N_INUSEBITS;
    760 	}
    761 	return (len > count ? count : len);
    762 }
    763 
    764 /*
    765  * Allocate contigous free clusters.
    766  *
    767  * pmp	      - mount point.
    768  * start      - start of cluster chain.
    769  * count      - number of clusters to allocate.
    770  * fillwith   - put this value into the FAT entry for the
    771  *		last allocated cluster.
    772  * retcluster - put the first allocated cluster's number here.
    773  * got	      - how many clusters were actually allocated.
    774  */
    775 int
    776 chainalloc(struct msdosfsmount *pmp, u_long start, u_long count, u_long fillwith, u_long *retcluster, u_long *got)
    777 {
    778 	int error;
    779 	u_long cl, n;
    780 
    781 	for (cl = start, n = count; n-- > 0;)
    782 		usemap_alloc(pmp, cl++);
    783 	if ((error = fatchain(pmp, start, count, fillwith)) != 0)
    784 		return (error);
    785 
    786 	DPRINTF(("%s(): allocated cluster chain at %lu (%lu clusters)\n",
    787 	    __func__, start, count));
    788 	if (retcluster)
    789 		*retcluster = start;
    790 	if (got)
    791 		*got = count;
    792 	return (0);
    793 }
    794 
    795 /*
    796  * Allocate contiguous free clusters.
    797  *
    798  * pmp	      - mount point.
    799  * start      - preferred start of cluster chain.
    800  * count      - number of clusters requested.
    801  * fillwith   - put this value into the FAT entry for the
    802  *		last allocated cluster.
    803  * retcluster - put the first allocated cluster's number here.
    804  * got	      - how many clusters were actually allocated.
    805  */
    806 int
    807 clusteralloc(struct msdosfsmount *pmp, u_long start, u_long count, u_long *retcluster, u_long *got)
    808 {
    809 	u_long idx;
    810 	u_long len, newst, foundl, cn, l;
    811 	u_long foundcn = 0; /* XXX: foundcn could be used unititialized */
    812 	u_long fillwith = CLUST_EOFE;
    813 	u_int map;
    814 
    815 	DPRINTF(("%s(): find %lu clusters\n", __func__, count));
    816 	if (start) {
    817 		if ((len = chainlength(pmp, start, count)) >= count)
    818 			return (chainalloc(pmp, start, count, fillwith, retcluster, got));
    819 	} else {
    820 		/*
    821 		 * This is a new file, initialize start
    822 		 */
    823 		struct timeval tv;
    824 
    825 		microtime(&tv);
    826 		start = (tv.tv_usec >> 10) | tv.tv_usec;
    827 		len = 0;
    828 	}
    829 
    830 	/*
    831 	 * Start at a (pseudo) random place to maximize cluster runs
    832 	 * under multiple writers.
    833 	 */
    834 	newst = (start * 1103515245 + 12345) % (pmp->pm_maxcluster + 1);
    835 	foundl = 0;
    836 
    837 	for (cn = newst; cn <= pmp->pm_maxcluster;) {
    838 		idx = cn / N_INUSEBITS;
    839 		map = pmp->pm_inusemap[idx];
    840 		map |= (1 << (cn % N_INUSEBITS)) - 1;
    841 		if (map != (u_int)-1) {
    842 			cn = idx * N_INUSEBITS + ffs(map^(u_int)-1) - 1;
    843 			if ((l = chainlength(pmp, cn, count)) >= count)
    844 				return (chainalloc(pmp, cn, count, fillwith, retcluster, got));
    845 			if (l > foundl) {
    846 				foundcn = cn;
    847 				foundl = l;
    848 			}
    849 			cn += l + 1;
    850 			continue;
    851 		}
    852 		cn += N_INUSEBITS - cn % N_INUSEBITS;
    853 	}
    854 	for (cn = 0; cn < newst;) {
    855 		idx = cn / N_INUSEBITS;
    856 		map = pmp->pm_inusemap[idx];
    857 		map |= (1 << (cn % N_INUSEBITS)) - 1;
    858 		if (map != (u_int)-1) {
    859 			cn = idx * N_INUSEBITS + ffs(map^(u_int)-1) - 1;
    860 			if ((l = chainlength(pmp, cn, count)) >= count)
    861 				return (chainalloc(pmp, cn, count, fillwith, retcluster, got));
    862 			if (l > foundl) {
    863 				foundcn = cn;
    864 				foundl = l;
    865 			}
    866 			cn += l + 1;
    867 			continue;
    868 		}
    869 		cn += N_INUSEBITS - cn % N_INUSEBITS;
    870 	}
    871 
    872 	if (!foundl)
    873 		return (ENOSPC);
    874 
    875 	if (len)
    876 		return (chainalloc(pmp, start, len, fillwith, retcluster, got));
    877 	else
    878 		return (chainalloc(pmp, foundcn, foundl, fillwith, retcluster, got));
    879 }
    880 
    881 
    882 /*
    883  * Free a chain of clusters.
    884  *
    885  * pmp		- address of the msdosfs mount structure for the filesystem
    886  *		  containing the cluster chain to be freed.
    887  * startcluster - number of the 1st cluster in the chain of clusters to be
    888  *		  freed.
    889  */
    890 int
    891 freeclusterchain(struct msdosfsmount *pmp, u_long cluster)
    892 {
    893 	int error;
    894 	struct buf *bp = NULL;
    895 	u_long bn, bo, bsize, byteoffset;
    896 	u_long readcn, lbn = -1;
    897 
    898 	while (cluster >= CLUST_FIRST && cluster <= pmp->pm_maxcluster) {
    899 		byteoffset = FATOFS(pmp, cluster);
    900 		fatblock(pmp, byteoffset, &bn, &bsize, &bo);
    901 		if (lbn != bn) {
    902 			if (bp)
    903 				updatefats(pmp, bp, lbn);
    904 			error = bread(pmp->pm_devvp, de_bn2kb(pmp, bn), bsize,
    905 			    B_MODIFY, &bp);
    906 			if (error) {
    907 				return (error);
    908 			}
    909 			lbn = bn;
    910 		}
    911 		usemap_free(pmp, cluster);
    912 		KASSERT(bp != NULL);
    913 		switch (pmp->pm_fatmask) {
    914 		case FAT12_MASK:
    915 			readcn = getushort((char *)bp->b_data + bo);
    916 			if (cluster & 1) {
    917 				cluster = readcn >> 4;
    918 				readcn &= 0x000f;
    919 				readcn |= MSDOSFSFREE << 4;
    920 			} else {
    921 				cluster = readcn;
    922 				readcn &= 0xf000;
    923 				readcn |= MSDOSFSFREE & 0xfff;
    924 			}
    925 			putushort((char *)bp->b_data + bo, readcn);
    926 			break;
    927 		case FAT16_MASK:
    928 			cluster = getushort((char *)bp->b_data + bo);
    929 			putushort((char *)bp->b_data + bo, MSDOSFSFREE);
    930 			break;
    931 		case FAT32_MASK:
    932 			cluster = getulong((char *)bp->b_data + bo);
    933 			putulong((char *)bp->b_data + bo,
    934 				 (MSDOSFSFREE & FAT32_MASK) | (cluster & ~FAT32_MASK));
    935 			break;
    936 		}
    937 		cluster &= pmp->pm_fatmask;
    938 	}
    939 	if (bp)
    940 		updatefats(pmp, bp, bn);
    941 	return (0);
    942 }
    943 
    944 /*
    945  * Read in FAT blocks looking for free clusters. For every free cluster
    946  * found turn off its corresponding bit in the pm_inusemap.
    947  */
    948 int
    949 fillinusemap(struct msdosfsmount *pmp)
    950 {
    951 	struct buf *bp = NULL;
    952 	u_long cn, readcn;
    953 	int error;
    954 	u_long bn, bo, bsize, byteoffset;
    955 
    956 	/*
    957 	 * Mark all clusters in use, we mark the free ones in the FAT scan
    958 	 * loop further down.
    959 	 */
    960 	for (cn = 0; cn < (pmp->pm_maxcluster + N_INUSEBITS) / N_INUSEBITS; cn++)
    961 		pmp->pm_inusemap[cn] = (u_int)-1;
    962 
    963 	/*
    964 	 * Figure how many free clusters are in the filesystem by ripping
    965 	 * through the FAT counting the number of entries whose content is
    966 	 * zero.  These represent free clusters.
    967 	 */
    968 	pmp->pm_freeclustercount = 0;
    969 	for (cn = CLUST_FIRST; cn <= pmp->pm_maxcluster; cn++) {
    970 		byteoffset = FATOFS(pmp, cn);
    971 		bo = byteoffset % pmp->pm_fatblocksize;
    972 		if (!bo || !bp) {
    973 			/* Read new FAT block */
    974 			if (bp)
    975 				brelse(bp, 0);
    976 			fatblock(pmp, byteoffset, &bn, &bsize, NULL);
    977 			error = bread(pmp->pm_devvp, de_bn2kb(pmp, bn), bsize,
    978 			    0, &bp);
    979 			if (error) {
    980 				return (error);
    981 			}
    982 		}
    983 		if (FAT32(pmp))
    984 			readcn = getulong((char *)bp->b_data + bo);
    985 		else
    986 			readcn = getushort((char *)bp->b_data + bo);
    987 		if (FAT12(pmp) && (cn & 1))
    988 			readcn >>= 4;
    989 		readcn &= pmp->pm_fatmask;
    990 
    991 		if (readcn == 0)
    992 			usemap_free(pmp, cn);
    993 	}
    994 	if (bp)
    995 		brelse(bp, 0);
    996 	return (0);
    997 }
    998 
    999 /*
   1000  * Allocate a new cluster and chain it onto the end of the file.
   1001  *
   1002  * dep	 - the file to extend
   1003  * count - number of clusters to allocate
   1004  * bpp	 - where to return the address of the buf header for the first new
   1005  *	   file block
   1006  * ncp	 - where to put cluster number of the first newly allocated cluster
   1007  *	   If this pointer is 0, do not return the cluster number.
   1008  * flags - see fat.h
   1009  *
   1010  * NOTE: This function is not responsible for turning on the DE_UPDATE bit of
   1011  * the de_flag field of the denode and it does not change the de_FileSize
   1012  * field.  This is left for the caller to do.
   1013  */
   1014 
   1015 int
   1016 extendfile(struct denode *dep, u_long count, struct buf **bpp, u_long *ncp, int flags)
   1017 {
   1018 	int error;
   1019 	u_long frcn = 0, cn, got;
   1020 	struct msdosfsmount *pmp = dep->de_pmp;
   1021 	struct buf *bp;
   1022 
   1023 	/*
   1024 	 * Don't try to extend the root directory
   1025 	 */
   1026 	if (dep->de_StartCluster == MSDOSFSROOT
   1027 	    && (dep->de_Attributes & ATTR_DIRECTORY)) {
   1028 		DPRINTF(("%s(): attempt to extend root directory\n", __func__));
   1029 		return (ENOSPC);
   1030 	}
   1031 
   1032 	/*
   1033 	 * If the "file's last cluster" cache entry is empty, and the file
   1034 	 * is not empty, then fill the cache entry by calling pcbmap().
   1035 	 */
   1036 	fc_fileextends++;
   1037 	if (dep->de_fc[FC_LASTFC].fc_frcn == FCE_EMPTY &&
   1038 	    dep->de_StartCluster != 0) {
   1039 		fc_lfcempty++;
   1040 		error = pcbmap(dep, CLUST_END, 0, &cn, 0);
   1041 		/* we expect it to return E2BIG */
   1042 		if (error != E2BIG)
   1043 			return (error);
   1044 	}
   1045 
   1046 	fc_last_to_nexttolast(dep);
   1047 
   1048 	while (count > 0) {
   1049 
   1050 		/*
   1051 		 * Allocate a new cluster chain and cat onto the end of the
   1052 		 * file.  If the file is empty we make de_StartCluster point
   1053 		 * to the new block.  Note that de_StartCluster being 0 is
   1054 		 * sufficient to be sure the file is empty since we exclude
   1055 		 * attempts to extend the root directory above, and the root
   1056 		 * dir is the only file with a startcluster of 0 that has
   1057 		 * blocks allocated (sort of).
   1058 		 */
   1059 
   1060 		if (dep->de_StartCluster == 0)
   1061 			cn = 0;
   1062 		else
   1063 			cn = dep->de_fc[FC_LASTFC].fc_fsrcn + 1;
   1064 		error = clusteralloc(pmp, cn, count, &cn, &got);
   1065 		if (error)
   1066 			return (error);
   1067 
   1068 		count -= got;
   1069 
   1070 		/*
   1071 		 * Give them the filesystem relative cluster number if they want
   1072 		 * it.
   1073 		 */
   1074 		if (ncp) {
   1075 			*ncp = cn;
   1076 			ncp = NULL;
   1077 		}
   1078 
   1079 		if (dep->de_StartCluster == 0) {
   1080 			dep->de_StartCluster = cn;
   1081 			frcn = 0;
   1082 		} else {
   1083 			error = fatentry(FAT_SET, pmp,
   1084 					 dep->de_fc[FC_LASTFC].fc_fsrcn,
   1085 					 0, cn);
   1086 			if (error) {
   1087 				clusterfree(pmp, cn, NULL);
   1088 				return (error);
   1089 			}
   1090 			frcn = dep->de_fc[FC_LASTFC].fc_frcn + 1;
   1091 		}
   1092 
   1093 		/*
   1094 		 * Update the "last cluster of the file" entry in the
   1095 		 * denode's FAT cache.
   1096 		 */
   1097 
   1098 		fc_setcache(dep, FC_LASTFC, frcn + got - 1, cn + got - 1);
   1099 		if ((flags & DE_CLEAR) &&
   1100 		    (dep->de_Attributes & ATTR_DIRECTORY)) {
   1101 			while (got-- > 0) {
   1102 				bp = getblk(pmp->pm_devvp,
   1103 				    de_bn2kb(pmp, cntobn(pmp, cn++)),
   1104 				    pmp->pm_bpcluster, 0, 0);
   1105 				clrbuf(bp);
   1106 				if (bpp) {
   1107 					*bpp = bp;
   1108 						bpp = NULL;
   1109 				} else {
   1110 					bdwrite(bp);
   1111 				}
   1112 			}
   1113 		}
   1114 	}
   1115 
   1116 	return (0);
   1117 }
   1118