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