#include <sys/types.h>
#include <sys/time.h>
#include "ffs/buf.h"
#include <msdosfs/bpb.h>
#include "msdos/msdosfsmount.h"
#include "msdos/direntry.h"
#include "msdos/denode.h"
#include "msdos/fat.h"
#include "makefs.h"
int fc_fileextends;
int fc_lfcempty;
int fc_bmapcalls;
#define LMMAX 20
int fc_lmdistance[LMMAX];
int fc_largedistance;
int fc_wherefrom, fc_whereto, fc_lastclust;
int pm_fatblocksize;
#ifdef MSDOSFS_DEBUG
#define DPRINTF(a) printf a
#else
#define DPRINTF(a)
#endif
static void fatblock(struct msdosfsmount *, u_long, u_long *, u_long *,
u_long *);
void updatefats(struct msdosfsmount *, struct mkfsbuf *, u_long);
static inline void usemap_free(struct msdosfsmount *, u_long);
static inline void usemap_alloc(struct msdosfsmount *, u_long);
static int fatchain(struct msdosfsmount *, u_long, u_long, u_long);
int chainlength(struct msdosfsmount *, u_long, u_long);
int chainalloc(struct msdosfsmount *, u_long, u_long, u_long, u_long *,
u_long *);
static void
fatblock(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 = MINIMUM(pmp->pm_fatblocksec, pmp->pm_FATsecs - bn)
* pmp->pm_BytesPerSec;
bn += pmp->pm_fatblk + pmp->pm_curfat * pmp->pm_FATsecs;
DPRINTF(("%s(ofs=%lu bn=%lu, size=%lu, bo=%lu)\n", __func__, ofs, bn,
size, ofs % pmp->pm_fatblocksize));
if (bnp)
*bnp = bn;
if (sizep)
*sizep = size;
if (bop)
*bop = ofs % pmp->pm_fatblocksize;
pm_fatblocksize = pmp->pm_fatblocksize;
}
int
pcbmap(struct denode *dep, u_long findcn, daddr_t *bnp, u_long *cnp, int *sp)
{
int error;
u_long i;
u_long cn;
u_long prevcn = 0;
u_long byteoffset;
u_long bn;
u_long bo;
struct mkfsbuf *bp = NULL;
u_long bp_bn = -1;
struct msdosfsmount *pmp = dep->de_pmp;
u_long bsize;
fc_bmapcalls++;
if (bnp == NULL && cnp == NULL && sp == NULL)
return (0);
cn = dep->de_StartCluster;
DPRINTF(("%s(start cluster=%lu)\n", __func__, cn));
if (cn == MSDOSFSROOT) {
if (dep->de_Attributes & ATTR_DIRECTORY) {
if (de_cn2off(pmp, findcn) >= dep->de_FileSize) {
if (cnp)
*cnp = de_bn2cn(pmp, pmp->pm_rootdirsize);
DPRINTF(("%s(root, %lu ETOOBIG)\n", __func__,
de_cn2off(pmp, findcn)));
return (E2BIG);
}
if (bnp)
*bnp = pmp->pm_rootdirblk + de_cn2bn(pmp, findcn);
if (cnp)
*cnp = MSDOSFSROOT;
if (sp)
*sp = MINIMUM(pmp->pm_bpcluster,
dep->de_FileSize - de_cn2off(pmp, findcn));
DPRINTF(("%s(root, bn=%lu, cn=%u)\n", __func__,
pmp->pm_rootdirblk + de_cn2bn(pmp, findcn),
MSDOSFSROOT));
return (0);
} else {
if (cnp)
*cnp = 0;
DPRINTF(("%s(root, empty ETOOBIG)\n", __func__));
return (E2BIG);
}
}
if (sp)
*sp = pmp->pm_bpcluster;
i = 0;
fc_lookup(dep, findcn, &i, &cn);
DPRINTF(("%s(bpcluster=%lu i=%lu cn=%lu\n", __func__, pmp->pm_bpcluster,
i, cn));
if ((bn = findcn - i) >= LMMAX) {
fc_largedistance++;
fc_wherefrom = i;
fc_whereto = findcn;
fc_lastclust = dep->de_fc[FC_LASTFC].fc_frcn;
} else
fc_lmdistance[bn]++;
for (; i < findcn; i++) {
if (cn >= (CLUST_RSRVD & pmp->pm_fatmask))
goto hiteof;
if (cn < CLUST_FIRST || cn > pmp->pm_maxcluster) {
DPRINTF(("%s(cn, %lu not in %lu..%lu)\n", __func__,
cn, (u_long)CLUST_FIRST, pmp->pm_maxcluster));
if (bp)
brelse(bp, 0);
return (EINVAL);
}
byteoffset = FATOFS(pmp, cn);
fatblock(pmp, byteoffset, &bn, &bsize, &bo);
if (bn != bp_bn) {
if (bp)
brelse(bp, 0);
error = bread(pmp->pm_devvp, de_bn2kb(pmp, bn), bsize,
0, &bp);
if (error) {
DPRINTF(("%s(bread, %d)\n", __func__, error));
return (error);
}
bp_bn = bn;
}
prevcn = cn;
if (bo >= bsize) {
if (bp)
brelse(bp, 0);
DPRINTF(("%s(block, %lu >= %lu)\n", __func__, bo,
bsize));
return (EIO);
}
KASSERT(bp != NULL);
if (FAT32(pmp))
cn = getulong((char *)bp->b_data + bo);
else
cn = getushort((char *)bp->b_data + bo);
if (FAT12(pmp) && (prevcn & 1))
cn >>= 4;
DPRINTF(("%s(cn=%lu masked=%lu)\n", __func__, cn,
cn & pmp->pm_fatmask));
cn &= pmp->pm_fatmask;
}
if (!MSDOSFSEOF(cn, pmp->pm_fatmask)) {
if (bp)
brelse(bp, 0);
if (bnp)
*bnp = cntobn(pmp, cn);
if (cnp)
*cnp = cn;
DPRINTF(("%s(bn=%lu, cn=%lu)\n", __func__, cntobn(pmp, cn),
cn));
fc_setcache(dep, FC_LASTMAP, i, cn);
return (0);
}
hiteof:;
if (cnp)
*cnp = i;
if (bp)
brelse(bp, 0);
fc_setcache(dep, FC_LASTFC, i - 1, prevcn);
DPRINTF(("%s(eof, %lu)\n", __func__, i));
return (E2BIG);
}
void
fc_lookup(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;
}
}
void
fc_purge(struct denode *dep, u_int frcn)
{
int i;
struct fatcache *fcp;
fcp = dep->de_fc;
for (i = 0; i < FC_SIZE; i++, fcp++) {
if (fcp->fc_frcn >= frcn)
fcp->fc_frcn = FCE_EMPTY;
}
}
void
updatefats(struct msdosfsmount *pmp, struct mkfsbuf *bp, u_long fatbn)
{
int i, error;
struct mkfsbuf *bpn;
DPRINTF(("%s(pmp %p, bp %p, fatbn %lu)\n", __func__, pmp, bp, fatbn));
if (pmp->pm_fsinfo) {
u_long cn = pmp->pm_nxtfree;
if (pmp->pm_freeclustercount
&& (pmp->pm_inusemap[cn / N_INUSEBITS]
& (1 << (cn % N_INUSEBITS)))) {
for (cn = 0; cn < pmp->pm_maxcluster; cn++)
if (pmp->pm_inusemap[cn / N_INUSEBITS] != (u_int)-1)
break;
pmp->pm_nxtfree = cn
+ ffs(pmp->pm_inusemap[cn / N_INUSEBITS]
^ (u_int)-1) - 1;
}
if (bread(pmp->pm_devvp, de_bn2kb(pmp, pmp->pm_fsinfo),
pmp->pm_BytesPerSec, B_MODIFY, &bpn) != 0) {
pmp->pm_fsinfo = 0;
} else {
struct fsinfo *fp = (struct fsinfo *)bpn->b_data;
putulong(fp->fsinfree, pmp->pm_freeclustercount);
putulong(fp->fsinxtfree, pmp->pm_nxtfree);
if (pmp->pm_flags & MSDOSFSMNT_WAITONFAT)
bwrite(bpn);
else
bdwrite(bpn);
}
}
if (pmp->pm_flags & MSDOSFS_FATMIRROR) {
for (i = 1; i < pmp->pm_FATs; i++) {
fatbn += pmp->pm_FATsecs;
bpn = getblk(pmp->pm_devvp, de_bn2kb(pmp, fatbn),
bp->b_bcount, 0, 0);
memcpy(bpn->b_data, bp->b_data, bp->b_bcount);
if (pmp->pm_flags & MSDOSFSMNT_WAITONFAT) {
error = bwrite(bpn);
if (error)
printf("%s: copy FAT %d (error=%d)\n",
__func__, i, error);
} else
bdwrite(bpn);
}
}
if (pmp->pm_flags & MSDOSFSMNT_WAITONFAT) {
error = bwrite(bp);
if (error)
printf("%s: write FAT (error=%d)\n",
__func__, error);
} else
bdwrite(bp);
}
static inline void
usemap_alloc(struct msdosfsmount *pmp, u_long cn)
{
pmp->pm_inusemap[cn / N_INUSEBITS] |= 1 << (cn % N_INUSEBITS);
pmp->pm_freeclustercount--;
}
static inline void
usemap_free(struct msdosfsmount *pmp, u_long cn)
{
pmp->pm_freeclustercount++;
pmp->pm_inusemap[cn / N_INUSEBITS] &= ~(1 << (cn % N_INUSEBITS));
}
int
clusterfree(struct msdosfsmount *pmp, u_long cluster, u_long *oldcnp)
{
int error;
u_long oldcn;
usemap_free(pmp, cluster);
error = fatentry(FAT_GET_AND_SET, pmp, cluster, &oldcn, MSDOSFSFREE);
if (error) {
usemap_alloc(pmp, cluster);
return (error);
}
if (oldcnp)
*oldcnp = oldcn;
return (0);
}
int
fatentry(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 mkfsbuf *bp;
DPRINTF(("%s(func %d, pmp %p, clust %lu, oldcon %p, newcon " "%lx)\n",
__func__, function, pmp, cn, oldcontents, newcontents));
#ifdef DIAGNOSTIC
if ((function & (FAT_SET | FAT_GET)) == 0) {
DPRINTF(("%s(): function code doesn't specify get or set\n",
__func__));
return (EINVAL);
}
if ((function & FAT_GET) && oldcontents == NULL) {
DPRINTF(("%s(): get function with no place to put result\n",
__func__));
return (EINVAL);
}
#endif
if (cn < CLUST_FIRST || cn > pmp->pm_maxcluster)
return (EINVAL);
byteoffset = FATOFS(pmp, cn);
fatblock(pmp, byteoffset, &bn, &bsize, &bo);
if ((error = bread(pmp->pm_devvp, de_bn2kb(pmp, bn), bsize,
0, &bp)) != 0) {
return (error);
}
if (function & FAT_GET) {
if (FAT32(pmp))
readcn = getulong((char *)bp->b_data + bo);
else
readcn = getushort((char *)bp->b_data + bo);
if (FAT12(pmp) & (cn & 1))
readcn >>= 4;
readcn &= pmp->pm_fatmask;
*oldcontents = readcn;
}
if (function & FAT_SET) {
switch (pmp->pm_fatmask) {
case FAT12_MASK:
readcn = getushort((char *)bp->b_data + bo);
if (cn & 1) {
readcn &= 0x000f;
readcn |= newcontents << 4;
} else {
readcn &= 0xf000;
readcn |= newcontents & 0xfff;
}
putushort((char *)bp->b_data + bo, readcn);
break;
case FAT16_MASK:
putushort((char *)bp->b_data + bo, newcontents);
break;
case FAT32_MASK:
readcn = getulong((char *)bp->b_data + bo);
readcn &= ~FAT32_MASK;
readcn |= newcontents & FAT32_MASK;
putulong((char *)bp->b_data + bo, readcn);
break;
}
updatefats(pmp, bp, bn);
bp = NULL;
pmp->pm_fmod = 1;
}
if (bp)
brelse(bp, 0);
return (0);
}
static int
fatchain(struct msdosfsmount *pmp, u_long start, u_long count, u_long fillwith)
{
int error;
u_long bn, bo, bsize, byteoffset, readcn, newc;
struct mkfsbuf *bp;
DPRINTF(("%s(pmp %p, start %lu, count %lu, fillwith %lx)\n", __func__,
pmp, start, count, fillwith));
if (start < CLUST_FIRST || start + count - 1 > pmp->pm_maxcluster)
return (EINVAL);
while (count > 0) {
byteoffset = FATOFS(pmp, start);
fatblock(pmp, byteoffset, &bn, &bsize, &bo);
error = bread(pmp->pm_devvp, de_bn2kb(pmp, bn), bsize,
B_MODIFY, &bp);
if (error) {
return (error);
}
while (count > 0) {
start++;
newc = --count > 0 ? start : fillwith;
switch (pmp->pm_fatmask) {
case FAT12_MASK:
readcn = getushort((char *)bp->b_data + bo);
if (start & 1) {
readcn &= 0xf000;
readcn |= newc & 0xfff;
} else {
readcn &= 0x000f;
readcn |= newc << 4;
}
putushort((char *)bp->b_data + bo, readcn);
bo++;
if (!(start & 1))
bo++;
break;
case FAT16_MASK:
putushort((char *)bp->b_data + bo, newc);
bo += 2;
break;
case FAT32_MASK:
readcn = getulong((char *)bp->b_data + bo);
readcn &= ~pmp->pm_fatmask;
readcn |= newc & pmp->pm_fatmask;
putulong((char *)bp->b_data + bo, readcn);
bo += 4;
break;
}
if (bo >= bsize)
break;
}
updatefats(pmp, bp, bn);
}
pmp->pm_fmod = 1;
return (0);
}
int
chainlength(struct msdosfsmount *pmp, u_long start, u_long count)
{
u_long idx, max_idx;
u_int map;
u_long len;
max_idx = pmp->pm_maxcluster / N_INUSEBITS;
idx = start / N_INUSEBITS;
start %= N_INUSEBITS;
map = pmp->pm_inusemap[idx];
map &= ~((1 << start) - 1);
if (map) {
len = ffs(map) - 1 - start;
return (len > count ? count : len);
}
len = N_INUSEBITS - start;
if (len >= count)
return (count);
while (++idx <= max_idx) {
if (len >= count)
break;
if ((map = pmp->pm_inusemap[idx]) != 0) {
len += ffs(map) - 1;
break;
}
len += N_INUSEBITS;
}
return (len > count ? count : len);
}
int
chainalloc(struct msdosfsmount *pmp, u_long start, u_long count, u_long fillwith, u_long *retcluster, u_long *got)
{
int error;
u_long cl, n;
for (cl = start, n = count; n-- > 0;)
usemap_alloc(pmp, cl++);
if ((error = fatchain(pmp, start, count, fillwith)) != 0)
return (error);
DPRINTF(("%s(): allocated cluster chain at %lu (%lu clusters)\n",
__func__, start, count));
if (retcluster)
*retcluster = start;
if (got)
*got = count;
return (0);
}
int
clusteralloc(struct msdosfsmount *pmp, u_long start, u_long count, u_long *retcluster, u_long *got)
{
u_long idx;
u_long len, newst, foundl, cn, l;
u_long foundcn = 0;
u_long fillwith = CLUST_EOFE;
u_int map;
DPRINTF(("%s(): find %lu clusters\n", __func__, count));
if (start) {
if ((len = chainlength(pmp, start, count)) >= count)
return (chainalloc(pmp, start, count, fillwith, retcluster, got));
} else {
struct timeval tv;
microtime(&tv);
start = (tv.tv_usec >> 10) | tv.tv_usec;
len = 0;
}
newst = (start * 1103515245 + 12345) % (pmp->pm_maxcluster + 1);
foundl = 0;
for (cn = newst; cn <= pmp->pm_maxcluster;) {
idx = cn / N_INUSEBITS;
map = pmp->pm_inusemap[idx];
map |= (1 << (cn % N_INUSEBITS)) - 1;
if (map != (u_int)-1) {
cn = idx * N_INUSEBITS + ffs(map^(u_int)-1) - 1;
if ((l = chainlength(pmp, cn, count)) >= count)
return (chainalloc(pmp, cn, count, fillwith, retcluster, got));
if (l > foundl) {
foundcn = cn;
foundl = l;
}
cn += l + 1;
continue;
}
cn += N_INUSEBITS - cn % N_INUSEBITS;
}
for (cn = 0; cn < newst;) {
idx = cn / N_INUSEBITS;
map = pmp->pm_inusemap[idx];
map |= (1 << (cn % N_INUSEBITS)) - 1;
if (map != (u_int)-1) {
cn = idx * N_INUSEBITS + ffs(map^(u_int)-1) - 1;
if ((l = chainlength(pmp, cn, count)) >= count)
return (chainalloc(pmp, cn, count, fillwith, retcluster, got));
if (l > foundl) {
foundcn = cn;
foundl = l;
}
cn += l + 1;
continue;
}
cn += N_INUSEBITS - cn % N_INUSEBITS;
}
if (!foundl)
return (ENOSPC);
if (len)
return (chainalloc(pmp, start, len, fillwith, retcluster, got));
else
return (chainalloc(pmp, foundcn, foundl, fillwith, retcluster, got));
}
int
freeclusterchain(struct msdosfsmount *pmp, u_long cluster)
{
int error;
struct mkfsbuf *bp = NULL;
u_long bn, bo, bsize, byteoffset;
u_long readcn, lbn = -1;
while (cluster >= CLUST_FIRST && cluster <= pmp->pm_maxcluster) {
byteoffset = FATOFS(pmp, cluster);
fatblock(pmp, byteoffset, &bn, &bsize, &bo);
if (lbn != bn) {
if (bp)
updatefats(pmp, bp, lbn);
error = bread(pmp->pm_devvp, de_bn2kb(pmp, bn), bsize,
B_MODIFY, &bp);
if (error) {
return (error);
}
lbn = bn;
}
usemap_free(pmp, cluster);
KASSERT(bp != NULL);
switch (pmp->pm_fatmask) {
case FAT12_MASK:
readcn = getushort((char *)bp->b_data + bo);
if (cluster & 1) {
cluster = readcn >> 4;
readcn &= 0x000f;
readcn |= MSDOSFSFREE << 4;
} else {
cluster = readcn;
readcn &= 0xf000;
readcn |= MSDOSFSFREE & 0xfff;
}
putushort((char *)bp->b_data + bo, readcn);
break;
case FAT16_MASK:
cluster = getushort((char *)bp->b_data + bo);
putushort((char *)bp->b_data + bo, MSDOSFSFREE);
break;
case FAT32_MASK:
cluster = getulong((char *)bp->b_data + bo);
putulong((char *)bp->b_data + bo,
(MSDOSFSFREE & FAT32_MASK) | (cluster & ~FAT32_MASK));
break;
}
cluster &= pmp->pm_fatmask;
}
if (bp)
updatefats(pmp, bp, bn);
return (0);
}
int
fillinusemap(struct msdosfsmount *pmp)
{
struct mkfsbuf *bp = NULL;
u_long cn, readcn;
int error;
u_long bn, bo, bsize, byteoffset;
for (cn = 0; cn < (pmp->pm_maxcluster + N_INUSEBITS) / N_INUSEBITS; cn++)
pmp->pm_inusemap[cn] = (u_int)-1;
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) {
if (bp)
brelse(bp, 0);
fatblock(pmp, byteoffset, &bn, &bsize, NULL);
error = bread(pmp->pm_devvp, de_bn2kb(pmp, bn), bsize,
0, &bp);
if (error) {
return (error);
}
}
if (FAT32(pmp))
readcn = getulong((char *)bp->b_data + bo);
else
readcn = getushort((char *)bp->b_data + bo);
if (FAT12(pmp) && (cn & 1))
readcn >>= 4;
readcn &= pmp->pm_fatmask;
if (readcn == 0)
usemap_free(pmp, cn);
}
if (bp)
brelse(bp, 0);
return (0);
}
int
extendfile(struct denode *dep, u_long count, struct mkfsbuf **bpp, u_long *ncp, int flags)
{
int error;
u_long frcn = 0, cn, got;
struct msdosfsmount *pmp = dep->de_pmp;
struct mkfsbuf *bp;
if (dep->de_StartCluster == MSDOSFSROOT
&& (dep->de_Attributes & ATTR_DIRECTORY)) {
DPRINTF(("%s(): attempt to extend root directory\n", __func__));
return (ENOSPC);
}
fc_fileextends++;
if (dep->de_fc[FC_LASTFC].fc_frcn == FCE_EMPTY &&
dep->de_StartCluster != 0) {
fc_lfcempty++;
error = pcbmap(dep, CLUST_END, 0, &cn, 0);
if (error != E2BIG)
return (error);
}
fc_last_to_nexttolast(dep);
while (count > 0) {
if (dep->de_StartCluster == 0)
cn = 0;
else
cn = dep->de_fc[FC_LASTFC].fc_fsrcn + 1;
error = clusteralloc(pmp, cn, count, &cn, &got);
if (error)
return (error);
count -= got;
if (ncp) {
*ncp = cn;
ncp = NULL;
}
if (dep->de_StartCluster == 0) {
dep->de_StartCluster = cn;
frcn = 0;
} else {
error = fatentry(FAT_SET, pmp,
dep->de_fc[FC_LASTFC].fc_fsrcn,
0, cn);
if (error) {
clusterfree(pmp, cn, NULL);
return (error);
}
frcn = dep->de_fc[FC_LASTFC].fc_frcn + 1;
}
fc_setcache(dep, FC_LASTFC, frcn + got - 1, cn + got - 1);
if ((flags & DE_CLEAR) &&
(dep->de_Attributes & ATTR_DIRECTORY)) {
while (got-- > 0) {
bp = getblk(pmp->pm_devvp,
de_bn2kb(pmp, cntobn(pmp, cn++)),
pmp->pm_bpcluster, 0, 0);
clrbuf(bp);
if (bpp) {
*bpp = bp;
bpp = NULL;
} else {
bdwrite(bp);
}
}
}
}
return (0);
}