1996-10-13 08:10:34 +04:00
|
|
|
/* $NetBSD: msdosfs_fat.c,v 1.24 1996/10/13 04:16:32 christos Exp $ */
|
1994-06-29 10:29:24 +04:00
|
|
|
|
1994-07-19 01:38:08 +04:00
|
|
|
/*-
|
1995-10-15 18:34:19 +03:00
|
|
|
* Copyright (C) 1994, 1995 Wolfgang Solfrank.
|
|
|
|
* Copyright (C) 1994, 1995 TooLs GmbH.
|
1994-07-19 01:38:08 +04:00
|
|
|
* All rights reserved.
|
1994-08-21 22:43:49 +04:00
|
|
|
* Original code by Paul Popelka (paulp@uts.amdahl.com) (see below).
|
1994-07-19 01:38:08 +04:00
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
* 3. All advertising materials mentioning features or use of this software
|
|
|
|
* must display the following acknowledgement:
|
|
|
|
* This product includes software developed by TooLs GmbH.
|
|
|
|
* 4. The name of TooLs GmbH may not be used to endorse or promote products
|
|
|
|
* derived from this software without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``AS IS'' AND ANY EXPRESS OR
|
|
|
|
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
|
|
|
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
|
|
|
* IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
|
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
|
|
|
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
|
|
|
|
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
|
|
|
|
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
|
|
|
|
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
|
|
|
|
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
1993-08-13 15:35:13 +04:00
|
|
|
/*
|
|
|
|
* Written by Paul Popelka (paulp@uts.amdahl.com)
|
|
|
|
*
|
|
|
|
* You can do anything you want with this software, just don't say you wrote
|
|
|
|
* it, and don't remove this notice.
|
|
|
|
*
|
|
|
|
* This software is provided "as is".
|
|
|
|
*
|
|
|
|
* The author supplies this software to be publicly redistributed on the
|
|
|
|
* understanding that the author is not responsible for the correct
|
|
|
|
* functioning of this software in any circumstances and is not liable for
|
|
|
|
* any damages caused by this software.
|
|
|
|
*
|
|
|
|
* October 1992
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* kernel include files.
|
|
|
|
*/
|
1993-12-18 03:50:51 +03:00
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/systm.h>
|
|
|
|
#include <sys/buf.h>
|
|
|
|
#include <sys/file.h>
|
|
|
|
#include <sys/namei.h>
|
|
|
|
#include <sys/mount.h> /* to define statfs structure */
|
|
|
|
#include <sys/vnode.h> /* to define vattr structure */
|
|
|
|
#include <sys/errno.h>
|
1995-10-15 18:34:19 +03:00
|
|
|
#include <sys/dirent.h>
|
1993-08-13 15:35:13 +04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* msdosfs include files.
|
|
|
|
*/
|
1993-12-18 03:50:51 +03:00
|
|
|
#include <msdosfs/bpb.h>
|
|
|
|
#include <msdosfs/msdosfsmount.h>
|
|
|
|
#include <msdosfs/direntry.h>
|
|
|
|
#include <msdosfs/denode.h>
|
|
|
|
#include <msdosfs/fat.h>
|
1993-08-13 15:35:13 +04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Fat cache stats.
|
|
|
|
*/
|
|
|
|
int fc_fileextends; /* # of file extends */
|
|
|
|
int fc_lfcempty; /* # of time last file cluster cache entry
|
|
|
|
* was empty */
|
|
|
|
int fc_bmapcalls; /* # of times pcbmap was called */
|
|
|
|
|
|
|
|
#define LMMAX 20
|
|
|
|
int fc_lmdistance[LMMAX]; /* counters for how far off the last
|
|
|
|
* cluster mapped entry was. */
|
|
|
|
int fc_largedistance; /* off by more than LMMAX */
|
|
|
|
|
|
|
|
/* Byte offset in FAT on filesystem pmp, cluster cn */
|
|
|
|
#define FATOFS(pmp, cn) (FAT12(pmp) ? (cn) * 3 / 2 : (cn) * 2)
|
|
|
|
|
1996-02-09 22:13:39 +03:00
|
|
|
static void fatblock __P((struct msdosfsmount *, u_long, u_long *, u_long *,
|
|
|
|
u_long *));
|
|
|
|
void updatefats __P((struct msdosfsmount *, struct buf *, u_long));
|
|
|
|
static __inline void usemap_free __P((struct msdosfsmount *, u_long));
|
|
|
|
static __inline void usemap_alloc __P((struct msdosfsmount *, u_long));
|
|
|
|
static int fatchain __P((struct msdosfsmount *, u_long, u_long, u_long));
|
|
|
|
int chainlength __P((struct msdosfsmount *, u_long, u_long));
|
|
|
|
int chainalloc __P((struct msdosfsmount *, u_long, u_long, u_long, u_long *,
|
|
|
|
u_long *));
|
|
|
|
|
1993-08-13 15:35:13 +04:00
|
|
|
static void
|
|
|
|
fatblock(pmp, ofs, bnp, sizep, bop)
|
|
|
|
struct msdosfsmount *pmp;
|
|
|
|
u_long ofs;
|
|
|
|
u_long *bnp;
|
|
|
|
u_long *sizep;
|
|
|
|
u_long *bop;
|
|
|
|
{
|
|
|
|
u_long bn, size;
|
|
|
|
|
|
|
|
bn = ofs / pmp->pm_fatblocksize * pmp->pm_fatblocksec;
|
|
|
|
size = min(pmp->pm_fatblocksec, pmp->pm_FATsecs - bn)
|
|
|
|
* pmp->pm_BytesPerSec;
|
|
|
|
bn += pmp->pm_fatblk;
|
|
|
|
if (bnp)
|
|
|
|
*bnp = bn;
|
|
|
|
if (sizep)
|
|
|
|
*sizep = size;
|
|
|
|
if (bop)
|
|
|
|
*bop = ofs % pmp->pm_fatblocksize;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Map the logical cluster number of a file into a physical disk sector
|
1994-07-17 01:32:06 +04:00
|
|
|
* that is filesystem relative.
|
|
|
|
*
|
|
|
|
* dep - address of denode representing the file of interest
|
|
|
|
* findcn - file relative cluster whose filesystem relative cluster number
|
|
|
|
* and/or block number are/is to be found
|
|
|
|
* bnp - address of where to place the file system relative block number.
|
|
|
|
* If this pointer is null then don't return this quantity.
|
|
|
|
* cnp - address of where to place the file system relative cluster number.
|
|
|
|
* If this pointer is null then don't return this quantity.
|
|
|
|
*
|
|
|
|
* NOTE: Either bnp or cnp must be non-null.
|
|
|
|
* This function has one side effect. If the requested file relative cluster
|
|
|
|
* is beyond the end of file, then the actual number of clusters in the file
|
|
|
|
* is returned in *cnp. This is useful for determining how long a directory is.
|
|
|
|
* If cnp is null, nothing is returned.
|
1993-08-13 15:35:13 +04:00
|
|
|
*/
|
|
|
|
int
|
1995-09-09 23:38:00 +04:00
|
|
|
pcbmap(dep, findcn, bnp, cnp, sp)
|
1993-08-13 15:35:13 +04:00
|
|
|
struct denode *dep;
|
|
|
|
u_long findcn; /* file relative cluster to get */
|
|
|
|
daddr_t *bnp; /* returned filesys relative blk number */
|
|
|
|
u_long *cnp; /* returned cluster number */
|
1995-09-09 23:38:00 +04:00
|
|
|
int *sp; /* returned block size */
|
1993-08-13 15:35:13 +04:00
|
|
|
{
|
|
|
|
int error;
|
|
|
|
u_long i;
|
|
|
|
u_long cn;
|
1996-02-09 22:13:39 +03:00
|
|
|
u_long prevcn = 0; /* XXX: prevcn could be used unititialized */
|
1993-08-13 15:35:13 +04:00
|
|
|
u_long byteoffset;
|
|
|
|
u_long bn;
|
|
|
|
u_long bo;
|
|
|
|
struct buf *bp = NULL;
|
|
|
|
u_long bp_bn = -1;
|
|
|
|
struct msdosfsmount *pmp = dep->de_pmp;
|
|
|
|
u_long bsize;
|
|
|
|
int fat12 = FAT12(pmp); /* 12 bit fat */
|
|
|
|
|
|
|
|
fc_bmapcalls++;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If they don't give us someplace to return a value then don't
|
|
|
|
* bother doing anything.
|
|
|
|
*/
|
1995-09-09 23:38:00 +04:00
|
|
|
if (bnp == NULL && cnp == NULL && sp == NULL)
|
1994-09-28 14:31:23 +03:00
|
|
|
return (0);
|
1993-08-13 15:35:13 +04:00
|
|
|
|
|
|
|
cn = dep->de_StartCluster;
|
|
|
|
/*
|
|
|
|
* The "file" that makes up the root directory is contiguous,
|
|
|
|
* permanently allocated, of fixed size, and is not made up of
|
|
|
|
* clusters. If the cluster number is beyond the end of the root
|
|
|
|
* directory, then return the number of clusters in the file.
|
|
|
|
*/
|
|
|
|
if (cn == MSDOSFSROOT) {
|
|
|
|
if (dep->de_Attributes & ATTR_DIRECTORY) {
|
1995-10-15 18:34:19 +03:00
|
|
|
if (de_cn2off(pmp, findcn) >= dep->de_FileSize) {
|
1993-08-13 15:35:13 +04:00
|
|
|
if (cnp)
|
1995-10-15 18:34:19 +03:00
|
|
|
*cnp = de_bn2cn(pmp, pmp->pm_rootdirsize);
|
1994-09-28 14:31:23 +03:00
|
|
|
return (E2BIG);
|
1993-08-13 15:35:13 +04:00
|
|
|
}
|
|
|
|
if (bnp)
|
1995-10-15 18:34:19 +03:00
|
|
|
*bnp = pmp->pm_rootdirblk + de_cn2bn(pmp, findcn);
|
1993-08-13 15:35:13 +04:00
|
|
|
if (cnp)
|
|
|
|
*cnp = MSDOSFSROOT;
|
1995-09-09 23:38:00 +04:00
|
|
|
if (sp)
|
|
|
|
*sp = min(pmp->pm_bpcluster,
|
1995-10-15 18:34:19 +03:00
|
|
|
dep->de_FileSize - de_cn2off(pmp, findcn));
|
1994-09-28 14:31:23 +03:00
|
|
|
return (0);
|
1994-04-21 11:47:31 +04:00
|
|
|
} else { /* just an empty file */
|
1993-08-13 15:35:13 +04:00
|
|
|
if (cnp)
|
|
|
|
*cnp = 0;
|
1994-09-28 14:31:23 +03:00
|
|
|
return (E2BIG);
|
1993-08-13 15:35:13 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1995-09-09 23:38:00 +04:00
|
|
|
/*
|
|
|
|
* All other files do I/O in cluster sized blocks
|
|
|
|
*/
|
|
|
|
if (sp)
|
|
|
|
*sp = pmp->pm_bpcluster;
|
|
|
|
|
1993-08-13 15:35:13 +04:00
|
|
|
/*
|
|
|
|
* Rummage around in the fat cache, maybe we can avoid tromping
|
|
|
|
* thru every fat entry for the file. And, keep track of how far
|
|
|
|
* off the cache was from where we wanted to be.
|
|
|
|
*/
|
|
|
|
i = 0;
|
|
|
|
fc_lookup(dep, findcn, &i, &cn);
|
|
|
|
if ((bn = findcn - i) >= LMMAX)
|
|
|
|
fc_largedistance++;
|
|
|
|
else
|
|
|
|
fc_lmdistance[bn]++;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Handle all other files or directories the normal way.
|
|
|
|
*/
|
|
|
|
for (; i < findcn; i++) {
|
|
|
|
if (MSDOSFSEOF(cn))
|
|
|
|
goto hiteof;
|
|
|
|
byteoffset = FATOFS(pmp, cn);
|
|
|
|
fatblock(pmp, byteoffset, &bn, &bsize, &bo);
|
|
|
|
if (bn != bp_bn) {
|
|
|
|
if (bp)
|
|
|
|
brelse(bp);
|
1996-02-09 22:13:39 +03:00
|
|
|
error = bread(pmp->pm_devvp, bn, bsize, NOCRED, &bp);
|
|
|
|
if (error) {
|
1995-10-15 18:34:19 +03:00
|
|
|
brelse(bp);
|
1994-09-28 14:31:23 +03:00
|
|
|
return (error);
|
1995-10-15 18:34:19 +03:00
|
|
|
}
|
1993-08-13 15:35:13 +04:00
|
|
|
bp_bn = bn;
|
|
|
|
}
|
|
|
|
prevcn = cn;
|
1994-07-17 01:32:06 +04:00
|
|
|
cn = getushort(&bp->b_data[bo]);
|
1993-08-13 15:35:13 +04:00
|
|
|
if (fat12) {
|
|
|
|
if (prevcn & 1)
|
|
|
|
cn >>= 4;
|
|
|
|
cn &= 0x0fff;
|
|
|
|
/*
|
|
|
|
* Force the special cluster numbers in the range
|
|
|
|
* 0x0ff0-0x0fff to be the same as for 16 bit
|
|
|
|
* cluster numbers to let the rest of msdosfs think
|
|
|
|
* it is always dealing with 16 bit fats.
|
|
|
|
*/
|
|
|
|
if ((cn & 0x0ff0) == 0x0ff0)
|
|
|
|
cn |= 0xf000;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!MSDOSFSEOF(cn)) {
|
|
|
|
if (bp)
|
|
|
|
brelse(bp);
|
|
|
|
if (bnp)
|
|
|
|
*bnp = cntobn(pmp, cn);
|
|
|
|
if (cnp)
|
|
|
|
*cnp = cn;
|
|
|
|
fc_setcache(dep, FC_LASTMAP, i, cn);
|
1994-09-28 14:31:23 +03:00
|
|
|
return (0);
|
1993-08-13 15:35:13 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
hiteof:;
|
|
|
|
if (cnp)
|
|
|
|
*cnp = i;
|
|
|
|
if (bp)
|
|
|
|
brelse(bp);
|
|
|
|
/* update last file cluster entry in the fat cache */
|
|
|
|
fc_setcache(dep, FC_LASTFC, i - 1, prevcn);
|
1994-09-28 14:31:23 +03:00
|
|
|
return (E2BIG);
|
1993-08-13 15:35:13 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Find the closest entry in the fat cache to the cluster we are looking
|
|
|
|
* for.
|
|
|
|
*/
|
1994-09-28 14:31:23 +03:00
|
|
|
void
|
1993-08-13 15:35:13 +04:00
|
|
|
fc_lookup(dep, findcn, frcnp, fsrcnp)
|
|
|
|
struct denode *dep;
|
|
|
|
u_long findcn;
|
|
|
|
u_long *frcnp;
|
|
|
|
u_long *fsrcnp;
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
u_long cn;
|
|
|
|
struct fatcache *closest = 0;
|
|
|
|
|
|
|
|
for (i = 0; i < FC_SIZE; i++) {
|
|
|
|
cn = dep->de_fc[i].fc_frcn;
|
|
|
|
if (cn != FCE_EMPTY && cn <= findcn) {
|
|
|
|
if (closest == 0 || cn > closest->fc_frcn)
|
|
|
|
closest = &dep->de_fc[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (closest) {
|
|
|
|
*frcnp = closest->fc_frcn;
|
|
|
|
*fsrcnp = closest->fc_fsrcn;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Purge the fat cache in denode dep of all entries relating to file
|
|
|
|
* relative cluster frcn and beyond.
|
|
|
|
*/
|
1994-09-28 14:31:23 +03:00
|
|
|
void
|
1993-08-13 15:35:13 +04:00
|
|
|
fc_purge(dep, frcn)
|
|
|
|
struct denode *dep;
|
|
|
|
u_int frcn;
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
struct fatcache *fcp;
|
|
|
|
|
|
|
|
fcp = dep->de_fc;
|
|
|
|
for (i = 0; i < FC_SIZE; i++, fcp++) {
|
1994-07-17 01:32:06 +04:00
|
|
|
if (fcp->fc_frcn >= frcn)
|
1993-08-13 15:35:13 +04:00
|
|
|
fcp->fc_frcn = FCE_EMPTY;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
1994-07-19 01:38:08 +04:00
|
|
|
* Update all copies of the fat. The first copy is updated last.
|
1994-07-17 01:32:06 +04:00
|
|
|
*
|
|
|
|
* pmp - msdosfsmount structure for filesystem to update
|
|
|
|
* bp - addr of modified fat block
|
|
|
|
* fatbn - block number relative to begin of filesystem of the modified fat block.
|
1993-08-13 15:35:13 +04:00
|
|
|
*/
|
|
|
|
void
|
1994-07-19 01:38:08 +04:00
|
|
|
updatefats(pmp, bp, fatbn)
|
1993-08-13 15:35:13 +04:00
|
|
|
struct msdosfsmount *pmp;
|
|
|
|
struct buf *bp;
|
|
|
|
u_long fatbn;
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
struct buf *bpn;
|
|
|
|
|
1994-07-19 01:38:08 +04:00
|
|
|
#ifdef MSDOSFS_DEBUG
|
1996-10-13 08:10:34 +04:00
|
|
|
printf("updatefats(pmp %08x, bp %08x, fatbn %d)\n",
|
1996-10-11 02:46:11 +04:00
|
|
|
pmp, bp, fatbn);
|
1994-07-19 01:38:08 +04:00
|
|
|
#endif
|
1993-08-13 15:35:13 +04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Now copy the block(s) of the modified fat to the other copies of
|
|
|
|
* the fat and write them out. This is faster than reading in the
|
|
|
|
* other fats and then writing them back out. This could tie up
|
|
|
|
* the fat for quite a while. Preventing others from accessing it.
|
|
|
|
* To prevent us from going after the fat quite so much we use
|
|
|
|
* delayed writes, unless they specfied "synchronous" when the
|
|
|
|
* filesystem was mounted. If synch is asked for then use
|
|
|
|
* bwrite()'s and really slow things down.
|
|
|
|
*/
|
|
|
|
for (i = 1; i < pmp->pm_FATs; i++) {
|
|
|
|
fatbn += pmp->pm_FATsecs;
|
|
|
|
/* getblk() never fails */
|
1994-04-21 11:47:31 +04:00
|
|
|
bpn = getblk(pmp->pm_devvp, fatbn, bp->b_bcount, 0, 0);
|
1994-07-17 01:32:06 +04:00
|
|
|
bcopy(bp->b_data, bpn->b_data, bp->b_bcount);
|
1995-10-15 18:34:19 +03:00
|
|
|
if (pmp->pm_flags & MSDOSFSMNT_WAITONFAT)
|
1993-08-13 15:35:13 +04:00
|
|
|
bwrite(bpn);
|
|
|
|
else
|
|
|
|
bdwrite(bpn);
|
|
|
|
}
|
1994-07-19 01:38:08 +04:00
|
|
|
/*
|
|
|
|
* Write out the first fat last.
|
|
|
|
*/
|
1995-10-15 18:34:19 +03:00
|
|
|
if (pmp->pm_flags & MSDOSFSMNT_WAITONFAT)
|
1994-07-19 01:38:08 +04:00
|
|
|
bwrite(bp);
|
|
|
|
else
|
|
|
|
bdwrite(bp);
|
1993-08-13 15:35:13 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Updating entries in 12 bit fats is a pain in the butt.
|
|
|
|
*
|
|
|
|
* The following picture shows where nibbles go when moving from a 12 bit
|
|
|
|
* cluster number into the appropriate bytes in the FAT.
|
|
|
|
*
|
1994-07-17 01:32:06 +04:00
|
|
|
* byte m byte m+1 byte m+2
|
|
|
|
* +----+----+ +----+----+ +----+----+
|
|
|
|
* | 0 1 | | 2 3 | | 4 5 | FAT bytes
|
|
|
|
* +----+----+ +----+----+ +----+----+
|
1993-08-13 15:35:13 +04:00
|
|
|
*
|
1994-07-17 01:32:06 +04:00
|
|
|
* +----+----+----+ +----+----+----+
|
|
|
|
* | 3 0 1 | | 4 5 2 |
|
|
|
|
* +----+----+----+ +----+----+----+
|
|
|
|
* cluster n cluster n+1
|
1993-08-13 15:35:13 +04:00
|
|
|
*
|
|
|
|
* Where n is even. m = n + (n >> 2)
|
|
|
|
*
|
|
|
|
*/
|
1995-01-04 09:32:19 +03:00
|
|
|
static __inline void
|
1994-06-30 11:25:23 +04:00
|
|
|
usemap_alloc(pmp, cn)
|
|
|
|
struct msdosfsmount *pmp;
|
|
|
|
u_long cn;
|
1993-08-13 15:35:13 +04:00
|
|
|
{
|
1994-09-28 14:31:23 +03:00
|
|
|
|
|
|
|
pmp->pm_inusemap[cn / N_INUSEBITS] |= 1 << (cn % N_INUSEBITS);
|
1993-08-13 15:35:13 +04:00
|
|
|
pmp->pm_freeclustercount--;
|
|
|
|
}
|
|
|
|
|
1995-01-04 09:32:19 +03:00
|
|
|
static __inline void
|
1994-06-30 11:25:23 +04:00
|
|
|
usemap_free(pmp, cn)
|
|
|
|
struct msdosfsmount *pmp;
|
|
|
|
u_long cn;
|
1993-08-13 15:35:13 +04:00
|
|
|
{
|
1994-09-28 14:31:23 +03:00
|
|
|
|
1993-08-13 15:35:13 +04:00
|
|
|
pmp->pm_freeclustercount++;
|
1994-07-19 01:38:08 +04:00
|
|
|
pmp->pm_inusemap[cn / N_INUSEBITS] &= ~(1 << (cn % N_INUSEBITS));
|
1993-08-13 15:35:13 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
clusterfree(pmp, cluster, oldcnp)
|
|
|
|
struct msdosfsmount *pmp;
|
|
|
|
u_long cluster;
|
|
|
|
u_long *oldcnp;
|
|
|
|
{
|
|
|
|
int error;
|
|
|
|
u_long oldcn;
|
|
|
|
|
1996-02-09 22:13:39 +03:00
|
|
|
error = fatentry(FAT_GET_AND_SET, pmp, cluster, &oldcn, MSDOSFSFREE);
|
|
|
|
if (error)
|
1994-09-28 14:31:23 +03:00
|
|
|
return (error);
|
|
|
|
/*
|
|
|
|
* If the cluster was successfully marked free, then update
|
|
|
|
* the count of free clusters, and turn off the "allocated"
|
|
|
|
* bit in the "in use" cluster bit map.
|
|
|
|
*/
|
|
|
|
usemap_free(pmp, cluster);
|
|
|
|
if (oldcnp)
|
|
|
|
*oldcnp = oldcn;
|
|
|
|
return (0);
|
1993-08-13 15:35:13 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
1994-07-17 01:32:06 +04:00
|
|
|
* Get or Set or 'Get and Set' the cluster'th entry in the fat.
|
|
|
|
*
|
|
|
|
* function - whether to get or set a fat entry
|
|
|
|
* pmp - address of the msdosfsmount structure for the filesystem
|
|
|
|
* whose fat is to be manipulated.
|
|
|
|
* cn - which cluster is of interest
|
|
|
|
* oldcontents - address of a word that is to receive the contents of the
|
|
|
|
* cluster'th entry if this is a get function
|
|
|
|
* newcontents - the new value to be written into the cluster'th element of
|
|
|
|
* the fat if this is a set function.
|
1993-08-13 15:35:13 +04:00
|
|
|
*
|
|
|
|
* This function can also be used to free a cluster by setting the fat entry
|
|
|
|
* for a cluster to 0.
|
|
|
|
*
|
|
|
|
* All copies of the fat are updated if this is a set function. NOTE: If
|
|
|
|
* fatentry() marks a cluster as free it does not update the inusemap in
|
|
|
|
* the msdosfsmount structure. This is left to the caller.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
fatentry(function, pmp, cn, oldcontents, newcontents)
|
|
|
|
int function;
|
|
|
|
struct msdosfsmount *pmp;
|
|
|
|
u_long cn;
|
|
|
|
u_long *oldcontents;
|
|
|
|
u_long newcontents;
|
|
|
|
{
|
|
|
|
int error;
|
|
|
|
u_long readcn;
|
|
|
|
u_long bn, bo, bsize, byteoffset;
|
|
|
|
struct buf *bp;
|
|
|
|
|
1996-10-11 02:46:11 +04:00
|
|
|
#if 0
|
1996-10-13 08:10:34 +04:00
|
|
|
printf("fatentry(func %d, pmp %08x, clust %d, oldcon %08x, newcon %d)\n",
|
1996-10-11 02:46:11 +04:00
|
|
|
function, pmp, cluster, oldcontents, newcontents);
|
|
|
|
#endif
|
1993-08-13 15:35:13 +04:00
|
|
|
|
|
|
|
#ifdef DIAGNOSTIC
|
|
|
|
/*
|
|
|
|
* Be sure they asked us to do something.
|
|
|
|
*/
|
|
|
|
if ((function & (FAT_SET | FAT_GET)) == 0) {
|
1996-10-13 08:10:34 +04:00
|
|
|
printf("fatentry(): function code doesn't specify get or set\n");
|
1994-09-28 14:31:23 +03:00
|
|
|
return (EINVAL);
|
1993-08-13 15:35:13 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If they asked us to return a cluster number but didn't tell us
|
|
|
|
* where to put it, give them an error.
|
|
|
|
*/
|
|
|
|
if ((function & FAT_GET) && oldcontents == NULL) {
|
1996-10-13 08:10:34 +04:00
|
|
|
printf("fatentry(): get function with no place to put result\n");
|
1994-09-28 14:31:23 +03:00
|
|
|
return (EINVAL);
|
1993-08-13 15:35:13 +04:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Be sure the requested cluster is in the filesystem.
|
|
|
|
*/
|
|
|
|
if (cn < CLUST_FIRST || cn > pmp->pm_maxcluster)
|
1994-09-28 14:31:23 +03:00
|
|
|
return (EINVAL);
|
1993-08-13 15:35:13 +04:00
|
|
|
|
|
|
|
byteoffset = FATOFS(pmp, cn);
|
|
|
|
fatblock(pmp, byteoffset, &bn, &bsize, &bo);
|
1996-02-09 22:13:39 +03:00
|
|
|
if ((error = bread(pmp->pm_devvp, bn, bsize, NOCRED, &bp)) != 0) {
|
1995-10-15 18:34:19 +03:00
|
|
|
brelse(bp);
|
1994-09-28 14:31:23 +03:00
|
|
|
return (error);
|
1995-10-15 18:34:19 +03:00
|
|
|
}
|
1994-07-19 01:38:08 +04:00
|
|
|
|
1993-08-13 15:35:13 +04:00
|
|
|
if (function & FAT_GET) {
|
1994-07-17 01:32:06 +04:00
|
|
|
readcn = getushort(&bp->b_data[bo]);
|
1993-08-13 15:35:13 +04:00
|
|
|
if (FAT12(pmp)) {
|
|
|
|
if (cn & 1)
|
|
|
|
readcn >>= 4;
|
|
|
|
readcn &= 0x0fff;
|
|
|
|
/* map certain 12 bit fat entries to 16 bit */
|
|
|
|
if ((readcn & 0x0ff0) == 0x0ff0)
|
|
|
|
readcn |= 0xf000;
|
|
|
|
}
|
|
|
|
*oldcontents = readcn;
|
|
|
|
}
|
|
|
|
if (function & FAT_SET) {
|
|
|
|
if (FAT12(pmp)) {
|
1994-07-17 01:32:06 +04:00
|
|
|
readcn = getushort(&bp->b_data[bo]);
|
1993-08-13 15:35:13 +04:00
|
|
|
if (cn & 1) {
|
|
|
|
readcn &= 0x000f;
|
1994-07-17 01:32:06 +04:00
|
|
|
readcn |= newcontents << 4;
|
1994-04-21 11:47:31 +04:00
|
|
|
} else {
|
1993-08-13 15:35:13 +04:00
|
|
|
readcn &= 0xf000;
|
1994-07-17 01:32:06 +04:00
|
|
|
readcn |= newcontents & 0xfff;
|
1993-08-13 15:35:13 +04:00
|
|
|
}
|
1994-07-17 01:32:06 +04:00
|
|
|
putushort(&bp->b_data[bo], readcn);
|
1994-04-21 11:47:31 +04:00
|
|
|
} else
|
1994-07-17 01:32:06 +04:00
|
|
|
putushort(&bp->b_data[bo], newcontents);
|
1994-07-19 01:38:08 +04:00
|
|
|
updatefats(pmp, bp, bn);
|
1993-08-13 15:35:13 +04:00
|
|
|
bp = NULL;
|
1994-02-08 02:14:16 +03:00
|
|
|
pmp->pm_fmod = 1;
|
1993-08-13 15:35:13 +04:00
|
|
|
}
|
|
|
|
if (bp)
|
|
|
|
brelse(bp);
|
1994-09-28 14:31:23 +03:00
|
|
|
return (0);
|
1993-08-13 15:35:13 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
1994-07-19 01:38:08 +04:00
|
|
|
* Update a contiguous cluster chain
|
1994-07-17 01:32:06 +04:00
|
|
|
*
|
1994-07-19 01:38:08 +04:00
|
|
|
* pmp - mount point
|
|
|
|
* start - first cluster of chain
|
|
|
|
* count - number of clusters in chain
|
|
|
|
* fillwith - what to write into fat entry of last cluster
|
1993-08-13 15:35:13 +04:00
|
|
|
*/
|
1994-07-19 01:38:08 +04:00
|
|
|
static int
|
|
|
|
fatchain(pmp, start, count, fillwith)
|
1993-08-13 15:35:13 +04:00
|
|
|
struct msdosfsmount *pmp;
|
1994-07-19 01:38:08 +04:00
|
|
|
u_long start;
|
|
|
|
u_long count;
|
1993-08-13 15:35:13 +04:00
|
|
|
u_long fillwith;
|
|
|
|
{
|
|
|
|
int error;
|
1994-07-19 01:38:08 +04:00
|
|
|
u_long bn, bo, bsize, byteoffset, readcn, newc;
|
|
|
|
struct buf *bp;
|
|
|
|
|
|
|
|
#ifdef MSDOSFS_DEBUG
|
1996-10-13 08:10:34 +04:00
|
|
|
printf("fatchain(pmp %08x, start %d, count %d, fillwith %d)\n",
|
1996-10-11 02:46:11 +04:00
|
|
|
pmp, start, count, fillwith);
|
1994-07-19 01:38:08 +04:00
|
|
|
#endif
|
|
|
|
/*
|
|
|
|
* Be sure the clusters are in the filesystem.
|
|
|
|
*/
|
|
|
|
if (start < CLUST_FIRST || start + count - 1 > pmp->pm_maxcluster)
|
1994-09-28 14:31:23 +03:00
|
|
|
return (EINVAL);
|
1994-07-19 01:38:08 +04:00
|
|
|
|
|
|
|
while (count > 0) {
|
|
|
|
byteoffset = FATOFS(pmp, start);
|
|
|
|
fatblock(pmp, byteoffset, &bn, &bsize, &bo);
|
1996-02-09 22:13:39 +03:00
|
|
|
error = bread(pmp->pm_devvp, bn, bsize, NOCRED, &bp);
|
|
|
|
if (error) {
|
1995-10-15 18:34:19 +03:00
|
|
|
brelse(bp);
|
1994-09-28 14:31:23 +03:00
|
|
|
return (error);
|
1995-10-15 18:34:19 +03:00
|
|
|
}
|
1994-07-19 01:38:08 +04:00
|
|
|
while (count > 0) {
|
|
|
|
start++;
|
|
|
|
newc = --count > 0 ? start : fillwith;
|
|
|
|
if (FAT12(pmp)) {
|
|
|
|
readcn = getushort(&bp->b_data[bo]);
|
|
|
|
if (start & 1) {
|
|
|
|
readcn &= 0xf000;
|
|
|
|
readcn |= newc & 0xfff;
|
|
|
|
} else {
|
|
|
|
readcn &= 0x000f;
|
|
|
|
readcn |= newc << 4;
|
1993-08-13 15:35:13 +04:00
|
|
|
}
|
1994-07-19 01:38:08 +04:00
|
|
|
putushort(&bp->b_data[bo], readcn);
|
|
|
|
bo++;
|
|
|
|
if (!(start & 1))
|
|
|
|
bo++;
|
|
|
|
} else {
|
|
|
|
putushort(&bp->b_data[bo], newc);
|
|
|
|
bo += 2;
|
1993-08-13 15:35:13 +04:00
|
|
|
}
|
1994-07-19 01:38:08 +04:00
|
|
|
if (bo >= bsize)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
updatefats(pmp, bp, bn);
|
|
|
|
}
|
|
|
|
pmp->pm_fmod = 1;
|
1994-09-28 14:31:23 +03:00
|
|
|
return (0);
|
1994-07-19 01:38:08 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Check the length of a free cluster chain starting at start.
|
|
|
|
*
|
|
|
|
* pmp - mount point
|
|
|
|
* start - start of chain
|
|
|
|
* count - maximum interesting length
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
chainlength(pmp, start, count)
|
|
|
|
struct msdosfsmount *pmp;
|
|
|
|
u_long start;
|
|
|
|
u_long count;
|
|
|
|
{
|
|
|
|
u_long idx, max_idx;
|
|
|
|
u_int map;
|
|
|
|
u_long len;
|
|
|
|
|
1994-08-09 21:51:29 +04:00
|
|
|
max_idx = pmp->pm_maxcluster / N_INUSEBITS;
|
1994-07-19 01:38:08 +04:00
|
|
|
idx = start / N_INUSEBITS;
|
|
|
|
start %= N_INUSEBITS;
|
|
|
|
map = pmp->pm_inusemap[idx];
|
|
|
|
map &= ~((1 << start) - 1);
|
|
|
|
if (map) {
|
|
|
|
len = ffs(map) - 1 - start;
|
1994-09-28 14:31:23 +03:00
|
|
|
return (len > count ? count : len);
|
1994-07-19 01:38:08 +04:00
|
|
|
}
|
|
|
|
len = N_INUSEBITS - start;
|
|
|
|
if (len >= count)
|
1994-09-28 14:31:23 +03:00
|
|
|
return (count);
|
1994-07-19 01:38:08 +04:00
|
|
|
while (++idx <= max_idx) {
|
|
|
|
if (len >= count)
|
|
|
|
break;
|
1996-02-09 22:13:39 +03:00
|
|
|
if ((map = pmp->pm_inusemap[idx]) != NULL) {
|
1994-07-19 01:38:08 +04:00
|
|
|
len += ffs(map) - 1;
|
|
|
|
break;
|
1993-08-13 15:35:13 +04:00
|
|
|
}
|
1994-07-19 01:38:08 +04:00
|
|
|
len += N_INUSEBITS;
|
1993-08-13 15:35:13 +04:00
|
|
|
}
|
1994-09-28 14:31:23 +03:00
|
|
|
return (len > count ? count : len);
|
1994-07-19 01:38:08 +04:00
|
|
|
}
|
1993-08-13 15:35:13 +04:00
|
|
|
|
1994-07-19 01:38:08 +04:00
|
|
|
/*
|
|
|
|
* Allocate contigous free clusters.
|
|
|
|
*
|
|
|
|
* pmp - mount point.
|
|
|
|
* start - start of cluster chain.
|
|
|
|
* count - number of clusters to allocate.
|
|
|
|
* fillwith - put this value into the fat entry for the
|
|
|
|
* last allocated cluster.
|
|
|
|
* retcluster - put the first allocated cluster's number here.
|
|
|
|
* got - how many clusters were actually allocated.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
chainalloc(pmp, start, count, fillwith, retcluster, got)
|
|
|
|
struct msdosfsmount *pmp;
|
|
|
|
u_long start;
|
|
|
|
u_long count;
|
|
|
|
u_long fillwith;
|
|
|
|
u_long *retcluster;
|
|
|
|
u_long *got;
|
|
|
|
{
|
|
|
|
int error;
|
|
|
|
|
1996-02-09 22:13:39 +03:00
|
|
|
if ((error = fatchain(pmp, start, count, fillwith)) != 0)
|
1994-09-28 14:31:23 +03:00
|
|
|
return (error);
|
1994-07-19 01:38:08 +04:00
|
|
|
#ifdef MSDOSFS_DEBUG
|
1996-10-13 08:10:34 +04:00
|
|
|
printf("clusteralloc(): allocated cluster chain at %d (%d clusters)\n",
|
1996-10-11 02:46:11 +04:00
|
|
|
start, count);
|
1994-07-19 01:38:08 +04:00
|
|
|
#endif
|
1994-09-28 14:31:23 +03:00
|
|
|
if (retcluster)
|
|
|
|
*retcluster = start;
|
|
|
|
if (got)
|
|
|
|
*got = count;
|
|
|
|
while (count-- > 0)
|
|
|
|
usemap_alloc(pmp, start++);
|
|
|
|
return (0);
|
1993-08-13 15:35:13 +04:00
|
|
|
}
|
|
|
|
|
1994-07-19 01:38:08 +04:00
|
|
|
/*
|
|
|
|
* Allocate contiguous free clusters.
|
|
|
|
*
|
|
|
|
* pmp - mount point.
|
|
|
|
* start - preferred start of cluster chain.
|
|
|
|
* count - number of clusters requested.
|
|
|
|
* fillwith - put this value into the fat entry for the
|
|
|
|
* last allocated cluster.
|
|
|
|
* retcluster - put the first allocated cluster's number here.
|
|
|
|
* got - how many clusters were actually allocated.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
clusteralloc(pmp, start, count, fillwith, retcluster, got)
|
|
|
|
struct msdosfsmount *pmp;
|
|
|
|
u_long start;
|
|
|
|
u_long count;
|
|
|
|
u_long fillwith;
|
|
|
|
u_long *retcluster;
|
|
|
|
u_long *got;
|
|
|
|
{
|
1994-08-04 20:32:29 +04:00
|
|
|
u_long idx;
|
1996-02-09 22:13:39 +03:00
|
|
|
u_long len, newst, foundl, cn, l;
|
|
|
|
u_long foundcn = 0; /* XXX: foundcn could be used unititialized */
|
1994-07-19 01:38:08 +04:00
|
|
|
u_int map;
|
|
|
|
|
|
|
|
#ifdef MSDOSFS_DEBUG
|
1996-10-13 08:10:34 +04:00
|
|
|
printf("clusteralloc(): find %d clusters\n",count);
|
1994-07-19 01:38:08 +04:00
|
|
|
#endif
|
|
|
|
if (start) {
|
|
|
|
if ((len = chainlength(pmp, start, count)) >= count)
|
1994-09-28 14:31:23 +03:00
|
|
|
return (chainalloc(pmp, start, count, fillwith, retcluster, got));
|
1994-07-19 01:38:08 +04:00
|
|
|
} else {
|
|
|
|
/*
|
|
|
|
* This is a new file, initialize start
|
|
|
|
*/
|
|
|
|
struct timeval tv;
|
|
|
|
|
|
|
|
microtime(&tv);
|
1994-09-28 14:31:23 +03:00
|
|
|
start = (tv.tv_usec >> 10) | tv.tv_usec;
|
1994-07-19 01:38:08 +04:00
|
|
|
len = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Start at a (pseudo) random place to maximize cluster runs
|
|
|
|
* under multiple writers.
|
|
|
|
*/
|
1995-09-09 23:38:00 +04:00
|
|
|
newst = (start * 1103515245 + 12345) % (pmp->pm_maxcluster + 1);
|
1994-07-19 01:38:08 +04:00
|
|
|
foundl = 0;
|
|
|
|
|
1994-08-04 20:32:29 +04:00
|
|
|
for (cn = newst; cn <= pmp->pm_maxcluster;) {
|
|
|
|
idx = cn / N_INUSEBITS;
|
|
|
|
map = pmp->pm_inusemap[idx];
|
|
|
|
map |= (1 << (cn % N_INUSEBITS)) - 1;
|
1994-07-19 01:38:08 +04:00
|
|
|
if (map != (u_int)-1) {
|
|
|
|
cn = idx * N_INUSEBITS + ffs(map^(u_int)-1) - 1;
|
|
|
|
if ((l = chainlength(pmp, cn, count)) >= count)
|
1994-09-28 14:31:23 +03:00
|
|
|
return (chainalloc(pmp, cn, count, fillwith, retcluster, got));
|
1994-07-19 01:38:08 +04:00
|
|
|
if (l > foundl) {
|
|
|
|
foundcn = cn;
|
|
|
|
foundl = l;
|
|
|
|
}
|
|
|
|
cn += l + 1;
|
|
|
|
continue;
|
|
|
|
}
|
1994-08-04 20:32:29 +04:00
|
|
|
cn += N_INUSEBITS - cn % N_INUSEBITS;
|
1994-07-19 01:38:08 +04:00
|
|
|
}
|
1994-08-04 20:32:29 +04:00
|
|
|
for (cn = 0; cn < newst;) {
|
|
|
|
idx = cn / N_INUSEBITS;
|
1994-07-19 01:38:08 +04:00
|
|
|
map = pmp->pm_inusemap[idx];
|
1994-08-04 20:32:29 +04:00
|
|
|
map |= (1 << (cn % N_INUSEBITS)) - 1;
|
1994-07-19 01:38:08 +04:00
|
|
|
if (map != (u_int)-1) {
|
|
|
|
cn = idx * N_INUSEBITS + ffs(map^(u_int)-1) - 1;
|
|
|
|
if ((l = chainlength(pmp, cn, count)) >= count)
|
1994-09-28 14:31:23 +03:00
|
|
|
return (chainalloc(pmp, cn, count, fillwith, retcluster, got));
|
1994-07-19 01:38:08 +04:00
|
|
|
if (l > foundl) {
|
|
|
|
foundcn = cn;
|
|
|
|
foundl = l;
|
|
|
|
}
|
|
|
|
cn += l + 1;
|
|
|
|
continue;
|
|
|
|
}
|
1994-08-04 20:32:29 +04:00
|
|
|
cn += N_INUSEBITS - cn % N_INUSEBITS;
|
1994-07-19 01:38:08 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!foundl)
|
1994-09-28 14:31:23 +03:00
|
|
|
return (ENOSPC);
|
1994-07-19 01:38:08 +04:00
|
|
|
|
|
|
|
if (len)
|
1994-09-28 14:31:23 +03:00
|
|
|
return (chainalloc(pmp, start, len, fillwith, retcluster, got));
|
1994-07-19 01:38:08 +04:00
|
|
|
else
|
1994-09-28 14:31:23 +03:00
|
|
|
return (chainalloc(pmp, foundcn, foundl, fillwith, retcluster, got));
|
1994-07-19 01:38:08 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
1993-08-13 15:35:13 +04:00
|
|
|
/*
|
1994-07-17 01:32:06 +04:00
|
|
|
* Free a chain of clusters.
|
|
|
|
*
|
|
|
|
* pmp - address of the msdosfs mount structure for the filesystem
|
|
|
|
* containing the cluster chain to be freed.
|
1993-08-13 15:35:13 +04:00
|
|
|
* startcluster - number of the 1st cluster in the chain of clusters to be
|
1994-07-17 01:32:06 +04:00
|
|
|
* freed.
|
1993-08-13 15:35:13 +04:00
|
|
|
*/
|
|
|
|
int
|
1994-07-19 01:38:08 +04:00
|
|
|
freeclusterchain(pmp, cluster)
|
1993-08-13 15:35:13 +04:00
|
|
|
struct msdosfsmount *pmp;
|
1994-07-19 01:38:08 +04:00
|
|
|
u_long cluster;
|
1993-08-13 15:35:13 +04:00
|
|
|
{
|
1994-09-28 14:31:23 +03:00
|
|
|
int error;
|
1994-07-19 01:38:08 +04:00
|
|
|
struct buf *bp = NULL;
|
|
|
|
u_long bn, bo, bsize, byteoffset;
|
|
|
|
u_long readcn, lbn = -1;
|
1994-09-28 14:31:23 +03:00
|
|
|
|
1994-07-19 01:38:08 +04:00
|
|
|
while (cluster >= CLUST_FIRST && cluster <= pmp->pm_maxcluster) {
|
|
|
|
byteoffset = FATOFS(pmp, cluster);
|
|
|
|
fatblock(pmp, byteoffset, &bn, &bsize, &bo);
|
|
|
|
if (lbn != bn) {
|
|
|
|
if (bp)
|
1995-03-19 23:52:36 +03:00
|
|
|
updatefats(pmp, bp, lbn);
|
1996-02-09 22:13:39 +03:00
|
|
|
error = bread(pmp->pm_devvp, bn, bsize, NOCRED, &bp);
|
|
|
|
if (error) {
|
1995-10-15 18:34:19 +03:00
|
|
|
brelse(bp);
|
1994-09-28 14:31:23 +03:00
|
|
|
return (error);
|
1995-10-15 18:34:19 +03:00
|
|
|
}
|
1994-07-19 01:38:08 +04:00
|
|
|
lbn = bn;
|
|
|
|
}
|
|
|
|
usemap_free(pmp, cluster);
|
|
|
|
readcn = getushort(&bp->b_data[bo]);
|
|
|
|
if (FAT12(pmp)) {
|
|
|
|
if (cluster & 1) {
|
|
|
|
cluster = readcn >> 4;
|
|
|
|
readcn &= 0x000f;
|
|
|
|
readcn |= MSDOSFSFREE << 4;
|
|
|
|
} else {
|
|
|
|
cluster = readcn;
|
|
|
|
readcn &= 0xf000;
|
|
|
|
readcn |= MSDOSFSFREE & 0xfff;
|
|
|
|
}
|
|
|
|
putushort(&bp->b_data[bo], readcn);
|
|
|
|
cluster &= 0x0fff;
|
|
|
|
if ((cluster&0x0ff0) == 0x0ff0)
|
|
|
|
cluster |= 0xf000;
|
|
|
|
} else {
|
|
|
|
cluster = readcn;
|
|
|
|
putushort(&bp->b_data[bo], MSDOSFSFREE);
|
1993-08-13 15:35:13 +04:00
|
|
|
}
|
|
|
|
}
|
1994-07-19 01:38:08 +04:00
|
|
|
if (bp)
|
|
|
|
updatefats(pmp, bp, bn);
|
1994-09-28 14:31:23 +03:00
|
|
|
return (0);
|
1993-08-13 15:35:13 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Read in fat blocks looking for free clusters. For every free cluster
|
|
|
|
* found turn off its corresponding bit in the pm_inusemap.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
fillinusemap(pmp)
|
|
|
|
struct msdosfsmount *pmp;
|
|
|
|
{
|
|
|
|
struct buf *bp = NULL;
|
|
|
|
u_long cn, readcn;
|
|
|
|
int error;
|
|
|
|
int fat12 = FAT12(pmp);
|
|
|
|
u_long bn, bo, bsize, byteoffset;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Mark all clusters in use, we mark the free ones in the fat scan
|
|
|
|
* loop further down.
|
|
|
|
*/
|
1994-08-09 21:51:29 +04:00
|
|
|
for (cn = 0; cn < (pmp->pm_maxcluster + N_INUSEBITS) / N_INUSEBITS; cn++)
|
1994-07-19 01:38:08 +04:00
|
|
|
pmp->pm_inusemap[cn] = (u_int)-1;
|
1993-08-13 15:35:13 +04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Figure how many free clusters are in the filesystem by ripping
|
1994-07-17 01:32:06 +04:00
|
|
|
* through the fat counting the number of entries whose content is
|
1993-08-13 15:35:13 +04:00
|
|
|
* zero. These represent free clusters.
|
|
|
|
*/
|
|
|
|
pmp->pm_freeclustercount = 0;
|
|
|
|
for (cn = CLUST_FIRST; cn <= pmp->pm_maxcluster; cn++) {
|
|
|
|
byteoffset = FATOFS(pmp, cn);
|
|
|
|
bo = byteoffset % pmp->pm_fatblocksize;
|
|
|
|
if (!bo || !bp) {
|
|
|
|
/* Read new FAT block */
|
|
|
|
if (bp)
|
|
|
|
brelse(bp);
|
|
|
|
fatblock(pmp, byteoffset, &bn, &bsize, NULL);
|
1996-02-09 22:13:39 +03:00
|
|
|
error = bread(pmp->pm_devvp, bn, bsize, NOCRED, &bp);
|
|
|
|
if (error) {
|
1995-10-15 18:34:19 +03:00
|
|
|
brelse(bp);
|
1994-09-28 14:31:23 +03:00
|
|
|
return (error);
|
1995-10-15 18:34:19 +03:00
|
|
|
}
|
1993-08-13 15:35:13 +04:00
|
|
|
}
|
1994-07-17 01:32:06 +04:00
|
|
|
readcn = getushort(&bp->b_data[bo]);
|
1993-08-13 15:35:13 +04:00
|
|
|
if (fat12) {
|
|
|
|
if (cn & 1)
|
|
|
|
readcn >>= 4;
|
|
|
|
readcn &= 0x0fff;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (readcn == 0)
|
|
|
|
usemap_free(pmp, cn);
|
|
|
|
}
|
|
|
|
brelse(bp);
|
1994-09-28 14:31:23 +03:00
|
|
|
return (0);
|
1993-08-13 15:35:13 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
1994-07-17 01:32:06 +04:00
|
|
|
* Allocate a new cluster and chain it onto the end of the file.
|
|
|
|
*
|
1994-07-19 01:38:08 +04:00
|
|
|
* dep - the file to extend
|
|
|
|
* count - number of clusters to allocate
|
|
|
|
* bpp - where to return the address of the buf header for the first new
|
|
|
|
* file block
|
|
|
|
* ncp - where to put cluster number of the first newly allocated cluster
|
|
|
|
* If this pointer is 0, do not return the cluster number.
|
|
|
|
* flags - see fat.h
|
|
|
|
*
|
1994-07-19 08:29:55 +04:00
|
|
|
* NOTE: This function is not responsible for turning on the DE_UPDATE bit of
|
|
|
|
* the de_flag field of the denode and it does not change the de_FileSize
|
1993-08-13 15:35:13 +04:00
|
|
|
* field. This is left for the caller to do.
|
|
|
|
*/
|
|
|
|
int
|
1994-07-19 01:38:08 +04:00
|
|
|
extendfile(dep, count, bpp, ncp, flags)
|
1993-08-13 15:35:13 +04:00
|
|
|
struct denode *dep;
|
1994-07-19 01:38:08 +04:00
|
|
|
u_long count;
|
1993-08-13 15:35:13 +04:00
|
|
|
struct buf **bpp;
|
1994-07-19 01:38:08 +04:00
|
|
|
u_long *ncp;
|
|
|
|
int flags;
|
1993-08-13 15:35:13 +04:00
|
|
|
{
|
1994-09-28 14:31:23 +03:00
|
|
|
int error;
|
1993-08-13 15:35:13 +04:00
|
|
|
u_long frcn;
|
1994-07-19 01:38:08 +04:00
|
|
|
u_long cn, got;
|
1993-08-13 15:35:13 +04:00
|
|
|
struct msdosfsmount *pmp = dep->de_pmp;
|
1994-07-19 01:38:08 +04:00
|
|
|
struct buf *bp;
|
|
|
|
|
1993-08-13 15:35:13 +04:00
|
|
|
/*
|
|
|
|
* Don't try to extend the root directory
|
|
|
|
*/
|
|
|
|
if (DETOV(dep)->v_flag & VROOT) {
|
1996-10-13 08:10:34 +04:00
|
|
|
printf("extendfile(): attempt to extend root directory\n");
|
1994-09-28 14:31:23 +03:00
|
|
|
return (ENOSPC);
|
1993-08-13 15:35:13 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If the "file's last cluster" cache entry is empty, and the file
|
|
|
|
* is not empty, then fill the cache entry by calling pcbmap().
|
|
|
|
*/
|
|
|
|
fc_fileextends++;
|
|
|
|
if (dep->de_fc[FC_LASTFC].fc_frcn == FCE_EMPTY &&
|
|
|
|
dep->de_StartCluster != 0) {
|
|
|
|
fc_lfcempty++;
|
1995-09-09 23:38:00 +04:00
|
|
|
error = pcbmap(dep, 0xffff, 0, &cn, 0);
|
1993-08-13 15:35:13 +04:00
|
|
|
/* we expect it to return E2BIG */
|
|
|
|
if (error != E2BIG)
|
1994-09-28 14:31:23 +03:00
|
|
|
return (error);
|
1993-08-13 15:35:13 +04:00
|
|
|
}
|
1994-09-28 14:31:23 +03:00
|
|
|
|
1994-07-19 01:38:08 +04:00
|
|
|
while (count > 0) {
|
|
|
|
/*
|
1996-02-09 22:13:39 +03:00
|
|
|
* Allocate a new cluster chain and cat onto the end of the
|
|
|
|
* file. * If the file is empty we make de_StartCluster point
|
|
|
|
* to the new block. Note that de_StartCluster being 0 is
|
|
|
|
* sufficient to be sure the file is empty since we exclude
|
|
|
|
* attempts to extend the root directory above, and the root
|
|
|
|
* dir is the only file with a startcluster of 0 that has
|
|
|
|
* blocks allocated (sort of).
|
1994-07-19 01:38:08 +04:00
|
|
|
*/
|
|
|
|
if (dep->de_StartCluster == 0)
|
|
|
|
cn = 0;
|
|
|
|
else
|
|
|
|
cn = dep->de_fc[FC_LASTFC].fc_fsrcn + 1;
|
1996-02-09 22:13:39 +03:00
|
|
|
error = clusteralloc(pmp, cn, count, CLUST_EOFE, &cn, &got);
|
|
|
|
if (error)
|
1994-09-28 14:31:23 +03:00
|
|
|
return (error);
|
1994-07-19 01:38:08 +04:00
|
|
|
|
|
|
|
count -= got;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Give them the filesystem relative cluster number if they want
|
|
|
|
* it.
|
|
|
|
*/
|
|
|
|
if (ncp) {
|
|
|
|
*ncp = cn;
|
|
|
|
ncp = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (dep->de_StartCluster == 0) {
|
|
|
|
dep->de_StartCluster = cn;
|
|
|
|
frcn = 0;
|
|
|
|
} else {
|
1996-02-09 22:13:39 +03:00
|
|
|
error = fatentry(FAT_SET, pmp,
|
|
|
|
dep->de_fc[FC_LASTFC].fc_fsrcn,
|
|
|
|
0, cn);
|
|
|
|
if (error) {
|
1994-07-19 01:38:08 +04:00
|
|
|
clusterfree(pmp, cn, NULL);
|
1994-09-28 14:31:23 +03:00
|
|
|
return (error);
|
1994-07-19 01:38:08 +04:00
|
|
|
}
|
|
|
|
frcn = dep->de_fc[FC_LASTFC].fc_frcn + 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Update the "last cluster of the file" entry in the denode's fat
|
|
|
|
* cache.
|
|
|
|
*/
|
|
|
|
fc_setcache(dep, FC_LASTFC, frcn + got - 1, cn + got - 1);
|
|
|
|
|
|
|
|
if (flags & DE_CLEAR) {
|
|
|
|
while (got-- > 0) {
|
|
|
|
/*
|
|
|
|
* Get the buf header for the new block of the file.
|
|
|
|
*/
|
|
|
|
if (dep->de_Attributes & ATTR_DIRECTORY)
|
|
|
|
bp = getblk(pmp->pm_devvp, cntobn(pmp, cn++),
|
|
|
|
pmp->pm_bpcluster, 0, 0);
|
|
|
|
else {
|
1995-11-05 21:47:48 +03:00
|
|
|
bp = getblk(DETOV(dep), de_cn2bn(pmp, frcn++),
|
|
|
|
pmp->pm_bpcluster, 0, 0);
|
1994-07-19 01:38:08 +04:00
|
|
|
/*
|
|
|
|
* Do the bmap now, as in msdosfs_write
|
|
|
|
*/
|
1995-11-05 21:47:48 +03:00
|
|
|
if (pcbmap(dep,
|
|
|
|
de_bn2cn(pmp, bp->b_lblkno),
|
|
|
|
&bp->b_blkno, 0, 0))
|
1994-07-19 01:38:08 +04:00
|
|
|
bp->b_blkno = -1;
|
|
|
|
if (bp->b_blkno == -1)
|
|
|
|
panic("extendfile: pcbmap");
|
|
|
|
}
|
|
|
|
clrbuf(bp);
|
|
|
|
if (bpp) {
|
|
|
|
*bpp = bp;
|
|
|
|
bpp = NULL;
|
1995-07-25 01:19:27 +04:00
|
|
|
} else
|
|
|
|
bdwrite(bp);
|
1994-07-19 01:38:08 +04:00
|
|
|
}
|
1993-08-13 15:35:13 +04:00
|
|
|
}
|
|
|
|
}
|
1994-07-19 01:38:08 +04:00
|
|
|
|
1994-09-28 14:31:23 +03:00
|
|
|
return (0);
|
1993-08-13 15:35:13 +04:00
|
|
|
}
|