#include <sys/param.h>
#include <sys/systm.h>
#include <sys/buf.h>
#include <sys/mount.h>
#include <sys/vmmeter.h>
#include <sys/vnode.h>
#include <fs/msdosfs/bpb.h>
#include <fs/msdosfs/direntry.h>
#include <fs/msdosfs/denode.h>
#include <fs/msdosfs/fat.h>
#include <fs/msdosfs/msdosfsmount.h>
#define FULL_RUN ((u_int)0xffffffff)
static int chainalloc(struct msdosfsmount *pmp, u_long start,
u_long count, u_long fillwith, u_long *retcluster,
u_long *got);
static int chainlength(struct msdosfsmount *pmp, u_long start,
u_long count);
static void fatblock(struct msdosfsmount *pmp, u_long ofs, u_long *bnp,
u_long *sizep, u_long *bop);
static int fatchain(struct msdosfsmount *pmp, u_long start, u_long count,
u_long fillwith);
static void fc_lookup(struct denode *dep, u_long findcn, u_long *frcnp,
u_long *fsrcnp);
static void updatefats(struct msdosfsmount *pmp, struct buf *bp,
u_long fatbn);
static __inline void
usemap_alloc(struct msdosfsmount *pmp, u_long cn);
static int usemap_free(struct msdosfsmount *pmp, u_long cn);
static int clusteralloc1(struct msdosfsmount *pmp, u_long start,
u_long count, u_long fillwith, u_long *retcluster,
u_long *got);
static void
fatblock(struct msdosfsmount *pmp, u_long ofs, u_long *bnp, u_long *sizep,
u_long *bop)
{
u_long bn, size, fatblocksec;
fatblocksec = pmp->pm_fatblocksec;
if (FAT12(pmp) && fatblocksec % 3 != 0) {
fatblocksec *= 3;
if (fatblocksec % 6 == 0)
fatblocksec /= 2;
}
bn = ofs / pmp->pm_fatblocksize * pmp->pm_fatblocksec;
size = roundup(ulmin(fatblocksec, pmp->pm_FATsecs - bn) * DEV_BSIZE,
pmp->pm_BlkPerSec * DEV_BSIZE);
bn += pmp->pm_fatblk + pmp->pm_curfat * pmp->pm_FATsecs;
if (bnp)
*bnp = bn;
if (sizep)
*sizep = size;
if (bop)
*bop = ofs % 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 buf *bp = NULL;
u_long bp_bn = -1;
struct msdosfsmount *pmp = dep->de_pmp;
u_long bsize;
KASSERT(bnp != NULL || cnp != NULL || sp != NULL,
("pcbmap: extra call"));
ASSERT_VOP_ELOCKED(DETOV(dep), "pcbmap");
cn = dep->de_StartCluster;
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);
return (E2BIG);
}
if (bnp)
*bnp = pmp->pm_rootdirblk + de_cn2bn(pmp, findcn);
if (cnp)
*cnp = MSDOSFSROOT;
if (sp)
*sp = min(pmp->pm_bpcluster,
dep->de_FileSize - de_cn2off(pmp, findcn));
return (0);
} else {
if (cnp)
*cnp = 0;
return (E2BIG);
}
}
if (sp)
*sp = pmp->pm_bpcluster;
i = 0;
fc_lookup(dep, findcn, &i, &cn);
for (; i < findcn; i++) {
if ((cn | ~pmp->pm_fatmask) >= CLUST_RSRVD)
goto hiteof;
byteoffset = FATOFS(pmp, cn);
fatblock(pmp, byteoffset, &bn, &bsize, &bo);
if (bn != bp_bn) {
if (bp)
brelse(bp);
error = bread(pmp->pm_devvp, bn, bsize, NOCRED, &bp);
if (error) {
return (error);
}
bp_bn = bn;
}
prevcn = cn;
if (bo >= bsize) {
if (bp)
brelse(bp);
return (EIO);
}
if (FAT32(pmp))
cn = getulong(bp->b_data + bo);
else
cn = getushort(bp->b_data + bo);
if (FAT12(pmp) && (prevcn & 1))
cn >>= 4;
cn &= pmp->pm_fatmask;
if ((cn | ~pmp->pm_fatmask) >= CLUST_RSRVD)
cn |= ~pmp->pm_fatmask;
}
if (!MSDOSFSEOF(pmp, cn)) {
if (bp)
brelse(bp);
if (bnp)
*bnp = cntobn(pmp, cn);
if (cnp)
*cnp = cn;
fc_setcache(dep, FC_LASTMAP, i, cn);
return (0);
}
hiteof:;
if (cnp)
*cnp = i;
if (bp)
brelse(bp);
fc_setcache(dep, FC_LASTFC, i - 1, prevcn);
return (E2BIG);
}
static void
fc_lookup(struct denode *dep, u_long findcn, u_long *frcnp, u_long *fsrcnp)
{
int i;
u_long cn;
struct fatcache *closest = NULL;
ASSERT_VOP_LOCKED(DETOV(dep), "fc_lookup");
for (i = 0; i < FC_SIZE; i++) {
cn = dep->de_fc[i].fc_frcn;
if (cn != FCE_EMPTY && cn <= findcn) {
if (closest == NULL || 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;
ASSERT_VOP_ELOCKED(DETOV(dep), "fc_purge");
fcp = dep->de_fc;
for (i = 0; i < FC_SIZE; i++, fcp++) {
if (fcp->fc_frcn >= frcn)
fcp->fc_frcn = FCE_EMPTY;
}
}
static void
updatefats(struct msdosfsmount *pmp, struct buf *bp, u_long fatbn)
{
struct buf *bpn;
int cleanfat, i;
#ifdef MSDOSFS_DEBUG
printf("updatefats(pmp %p, bp %p, fatbn %lu)\n", pmp, bp, fatbn);
#endif
if (pmp->pm_flags & MSDOSFS_FATMIRROR) {
if (fatbn != pmp->pm_fatblk || FAT12(pmp))
cleanfat = 0;
else if (FAT16(pmp))
cleanfat = 16;
else
cleanfat = 32;
for (i = 1; i < pmp->pm_FATs; i++) {
fatbn += pmp->pm_FATsecs;
bpn = getblk(pmp->pm_devvp, fatbn, bp->b_bcount,
0, 0, 0);
memcpy(bpn->b_data, bp->b_data, bp->b_bcount);
if (cleanfat == 16)
((uint8_t *)bpn->b_data)[3] |= 0x80;
else if (cleanfat == 32)
((uint8_t *)bpn->b_data)[7] |= 0x08;
if (pmp->pm_mountp->mnt_flag & MNT_SYNCHRONOUS)
bwrite(bpn);
else
bdwrite(bpn);
}
}
if (pmp->pm_mountp->mnt_flag & MNT_SYNCHRONOUS)
bwrite(bp);
else
bdwrite(bp);
}
static __inline void
usemap_alloc(struct msdosfsmount *pmp, u_long cn)
{
MSDOSFS_ASSERT_MP_LOCKED(pmp);
KASSERT(cn <= pmp->pm_maxcluster, ("cn too large %lu %lu", cn,
pmp->pm_maxcluster));
KASSERT((pmp->pm_flags & MSDOSFSMNT_RONLY) == 0,
("usemap_alloc on ro msdosfs mount"));
KASSERT((pmp->pm_inusemap[cn / N_INUSEBITS] &
(1U << (cn % N_INUSEBITS))) == 0,
("Allocating used sector %ld %ld %x", cn, cn % N_INUSEBITS,
(unsigned)pmp->pm_inusemap[cn / N_INUSEBITS]));
pmp->pm_inusemap[cn / N_INUSEBITS] |= 1U << (cn % N_INUSEBITS);
KASSERT(pmp->pm_freeclustercount > 0, ("usemap_alloc: too little"));
pmp->pm_freeclustercount--;
pmp->pm_flags |= MSDOSFS_FSIMOD;
}
static int
usemap_free(struct msdosfsmount *pmp, u_long cn)
{
MSDOSFS_ASSERT_MP_LOCKED(pmp);
KASSERT(cn <= pmp->pm_maxcluster, ("cn too large %lu %lu", cn,
pmp->pm_maxcluster));
KASSERT((pmp->pm_flags & MSDOSFSMNT_RONLY) == 0,
("usemap_free on ro msdosfs mount"));
if ((pmp->pm_inusemap[cn / N_INUSEBITS] &
(1U << (cn % N_INUSEBITS))) == 0) {
printf("%s: Freeing unused sector %ld %ld %x\n",
pmp->pm_mountp->mnt_stat.f_mntonname, cn, cn % N_INUSEBITS,
(unsigned)pmp->pm_inusemap[cn / N_INUSEBITS]);
msdosfs_integrity_error(pmp);
return (EINTEGRITY);
}
pmp->pm_freeclustercount++;
pmp->pm_flags |= MSDOSFS_FSIMOD;
pmp->pm_inusemap[cn / N_INUSEBITS] &= ~(1U << (cn % N_INUSEBITS));
return (0);
}
void
clusterfree(struct msdosfsmount *pmp, u_long cluster)
{
int error;
u_long oldcn;
error = fatentry(FAT_GET_AND_SET, pmp, cluster, &oldcn, MSDOSFSFREE);
if (error != 0)
return;
MSDOSFS_LOCK_MP(pmp);
error = usemap_free(pmp, cluster);
MSDOSFS_UNLOCK_MP(pmp);
}
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 buf *bp;
#ifdef MSDOSFS_DEBUG
printf("fatentry(func %d, pmp %p, clust %lu, oldcon %p, newcon %lx)\n",
function, pmp, cn, oldcontents, newcontents);
#endif
#ifdef DIAGNOSTIC
if ((function & (FAT_SET | FAT_GET)) == 0) {
#ifdef MSDOSFS_DEBUG
printf("fatentry(): function code doesn't specify get or set\n");
#endif
return (EINVAL);
}
if ((function & FAT_GET) && oldcontents == NULL) {
#ifdef MSDOSFS_DEBUG
printf("fatentry(): get function with no place to put result\n");
#endif
return (EINVAL);
}
#endif
if (cn < CLUST_FIRST || cn > pmp->pm_maxcluster)
return (EINVAL);
byteoffset = FATOFS(pmp, cn);
fatblock(pmp, byteoffset, &bn, &bsize, &bo);
error = bread(pmp->pm_devvp, bn, bsize, NOCRED, &bp);
if (error) {
return (error);
}
if (function & FAT_GET) {
if (FAT32(pmp))
readcn = getulong(bp->b_data + bo);
else
readcn = getushort(bp->b_data + bo);
if (FAT12(pmp) & (cn & 1))
readcn >>= 4;
readcn &= pmp->pm_fatmask;
if ((readcn | ~pmp->pm_fatmask) >= CLUST_RSRVD)
readcn |= ~pmp->pm_fatmask;
*oldcontents = readcn;
}
if (function & FAT_SET) {
switch (pmp->pm_fatmask) {
case FAT12_MASK:
readcn = getushort(bp->b_data + bo);
if (cn & 1) {
readcn &= 0x000f;
readcn |= newcontents << 4;
} else {
readcn &= 0xf000;
readcn |= newcontents & 0xfff;
}
putushort(bp->b_data + bo, readcn);
break;
case FAT16_MASK:
putushort(bp->b_data + bo, newcontents);
break;
case FAT32_MASK:
readcn = getulong(bp->b_data + bo);
readcn &= ~FAT32_MASK;
readcn |= newcontents & FAT32_MASK;
putulong(bp->b_data + bo, readcn);
break;
}
updatefats(pmp, bp, bn);
bp = NULL;
pmp->pm_fmod = 1;
}
if (bp)
brelse(bp);
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 buf *bp;
#ifdef MSDOSFS_DEBUG
printf("fatchain(pmp %p, start %lu, count %lu, fillwith %lx)\n",
pmp, start, count, fillwith);
#endif
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, bn, bsize, NOCRED, &bp);
if (error) {
return (error);
}
while (count > 0) {
start++;
newc = --count > 0 ? start : fillwith;
switch (pmp->pm_fatmask) {
case FAT12_MASK:
readcn = getushort(bp->b_data + bo);
if (start & 1) {
readcn &= 0xf000;
readcn |= newc & 0xfff;
} else {
readcn &= 0x000f;
readcn |= newc << 4;
}
putushort(bp->b_data + bo, readcn);
bo++;
if (!(start & 1))
bo++;
break;
case FAT16_MASK:
putushort(bp->b_data + bo, newc);
bo += 2;
break;
case FAT32_MASK:
readcn = getulong(bp->b_data + bo);
readcn &= ~pmp->pm_fatmask;
readcn |= newc & pmp->pm_fatmask;
putulong(bp->b_data + bo, readcn);
bo += 4;
break;
}
if (bo >= bsize)
break;
}
updatefats(pmp, bp, bn);
}
pmp->pm_fmod = 1;
return (0);
}
static int
chainlength(struct msdosfsmount *pmp, u_long start, u_long count)
{
u_long idx, max_idx;
u_int map;
u_long len;
MSDOSFS_ASSERT_MP_LOCKED(pmp);
if (start > pmp->pm_maxcluster)
return (0);
max_idx = pmp->pm_maxcluster / N_INUSEBITS;
idx = start / N_INUSEBITS;
start %= N_INUSEBITS;
map = pmp->pm_inusemap[idx];
map &= ~((1U << start) - 1);
if (map) {
len = ffs(map) - 1 - start;
len = MIN(len, count);
if (start + len > pmp->pm_maxcluster)
len = pmp->pm_maxcluster - start + 1;
return (len);
}
len = N_INUSEBITS - start;
if (len >= count) {
len = count;
if (start + len > pmp->pm_maxcluster)
len = pmp->pm_maxcluster - start + 1;
return (len);
}
while (++idx <= max_idx) {
if (len >= count)
break;
map = pmp->pm_inusemap[idx];
if (map) {
len += ffs(map) - 1;
break;
}
len += N_INUSEBITS;
}
len = MIN(len, count);
if (start + len > pmp->pm_maxcluster)
len = pmp->pm_maxcluster - start + 1;
return (len);
}
static 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;
MSDOSFS_ASSERT_MP_LOCKED(pmp);
KASSERT((pmp->pm_flags & MSDOSFSMNT_RONLY) == 0,
("chainalloc on ro msdosfs mount"));
for (cl = start, n = count; n-- > 0;)
usemap_alloc(pmp, cl++);
pmp->pm_nxtfree = start + count;
if (pmp->pm_nxtfree > pmp->pm_maxcluster)
pmp->pm_nxtfree = CLUST_FIRST;
pmp->pm_flags |= MSDOSFS_FSIMOD;
error = fatchain(pmp, start, count, fillwith);
if (error != 0) {
for (cl = start, n = count; n-- > 0;)
(void)usemap_free(pmp, cl++);
return (error);
}
#ifdef MSDOSFS_DEBUG
printf("clusteralloc(): allocated cluster chain at %lu (%lu clusters)\n",
start, count);
#endif
if (retcluster)
*retcluster = start;
if (got)
*got = count;
return (0);
}
int
clusteralloc(struct msdosfsmount *pmp, u_long start, u_long count,
u_long fillwith, u_long *retcluster, u_long *got)
{
int error;
MSDOSFS_LOCK_MP(pmp);
error = clusteralloc1(pmp, start, count, fillwith, retcluster, got);
MSDOSFS_UNLOCK_MP(pmp);
return (error);
}
static int
clusteralloc1(struct msdosfsmount *pmp, u_long start, u_long count,
u_long fillwith, u_long *retcluster, u_long *got)
{
u_long idx;
u_long len, newst, foundl, cn, l;
u_long foundcn = 0;
u_int map;
MSDOSFS_ASSERT_MP_LOCKED(pmp);
#ifdef MSDOSFS_DEBUG
printf("clusteralloc(): find %lu clusters\n", count);
#endif
if (start) {
if ((len = chainlength(pmp, start, count)) >= count)
return (chainalloc(pmp, start, count, fillwith, retcluster, got));
} else
len = 0;
newst = pmp->pm_nxtfree;
foundl = 0;
for (cn = newst; cn <= pmp->pm_maxcluster;) {
idx = cn / N_INUSEBITS;
map = pmp->pm_inusemap[idx];
map |= (1U << (cn % N_INUSEBITS)) - 1;
if (map != FULL_RUN) {
cn = idx * N_INUSEBITS + ffs(map ^ FULL_RUN) - 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 |= (1U << (cn % N_INUSEBITS)) - 1;
if (map != FULL_RUN) {
cn = idx * N_INUSEBITS + ffs(map ^ FULL_RUN) - 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 buf *bp = NULL;
u_long bn, bo, bsize, byteoffset;
u_long readcn, lbn = -1;
MSDOSFS_LOCK_MP(pmp);
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, bn, bsize, NOCRED, &bp);
if (error) {
MSDOSFS_UNLOCK_MP(pmp);
return (error);
}
lbn = bn;
}
error = usemap_free(pmp, cluster);
if (error != 0) {
updatefats(pmp, bp, lbn);
MSDOSFS_UNLOCK_MP(pmp);
return (error);
}
switch (pmp->pm_fatmask) {
case FAT12_MASK:
readcn = getushort(bp->b_data + bo);
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);
break;
case FAT16_MASK:
cluster = getushort(bp->b_data + bo);
putushort(bp->b_data + bo, MSDOSFSFREE);
break;
case FAT32_MASK:
cluster = getulong(bp->b_data + bo);
putulong(bp->b_data + bo,
(MSDOSFSFREE & FAT32_MASK) | (cluster & ~FAT32_MASK));
break;
}
cluster &= pmp->pm_fatmask;
if ((cluster | ~pmp->pm_fatmask) >= CLUST_RSRVD)
cluster |= pmp->pm_fatmask;
}
if (bp)
updatefats(pmp, bp, bn);
MSDOSFS_UNLOCK_MP(pmp);
return (0);
}
int
fillinusemap(struct msdosfsmount *pmp)
{
struct buf *bp;
u_long bn, bo, bsize, byteoffset, cn, readcn;
int error;
MSDOSFS_ASSERT_MP_LOCKED(pmp);
bp = NULL;
for (cn = 0; cn < (pmp->pm_maxcluster + N_INUSEBITS) / N_INUSEBITS; cn++)
pmp->pm_inusemap[cn] = FULL_RUN;
pmp->pm_freeclustercount = 0;
for (cn = 0; cn <= pmp->pm_maxcluster; cn++) {
byteoffset = FATOFS(pmp, cn);
bo = byteoffset % pmp->pm_fatblocksize;
if (bo == 0) {
if (bp != NULL)
brelse(bp);
fatblock(pmp, byteoffset, &bn, &bsize, NULL);
error = bread(pmp->pm_devvp, bn, bsize, NOCRED, &bp);
if (error != 0)
return (error);
}
if (FAT32(pmp))
readcn = getulong(bp->b_data + bo);
else
readcn = getushort(bp->b_data + bo);
if (FAT12(pmp) && (cn & 1))
readcn >>= 4;
readcn &= pmp->pm_fatmask;
if (cn == 0 && readcn != ((pmp->pm_fatmask & 0xffffff00) |
pmp->pm_bpb.bpbMedia)) {
#ifdef MSDOSFS_DEBUG
printf("mountmsdosfs(): Media descriptor in BPB"
"does not match FAT ID\n");
#endif
brelse(bp);
return (EINVAL);
} else if (readcn == CLUST_FREE) {
error = usemap_free(pmp, cn);
if (error != 0) {
brelse(bp);
return (error);
}
}
}
if (bp != NULL)
brelse(bp);
for (cn = pmp->pm_maxcluster + 1; cn < (pmp->pm_maxcluster +
N_INUSEBITS) / N_INUSEBITS; cn++)
pmp->pm_inusemap[cn / N_INUSEBITS] |= 1U << (cn % N_INUSEBITS);
return (0);
}
int
extendfile(struct denode *dep, u_long count, struct buf **bpp, u_long *ncp,
int flags)
{
int error;
u_long frcn;
u_long cn, got;
struct msdosfsmount *pmp = dep->de_pmp;
struct buf *bp;
daddr_t blkno;
if (dep->de_StartCluster == MSDOSFSROOT
&& (dep->de_Attributes & ATTR_DIRECTORY)) {
#ifdef MSDOSFS_DEBUG
printf("extendfile(): attempt to extend root directory\n");
#endif
return (ENOSPC);
}
if (dep->de_fc[FC_LASTFC].fc_frcn == FCE_EMPTY &&
dep->de_StartCluster != 0) {
error = pcbmap(dep, 0xffff, 0, &cn, 0);
if (error != E2BIG)
return (error);
}
dep->de_fc[FC_NEXTTOLASTFC].fc_frcn =
dep->de_fc[FC_LASTFC].fc_frcn;
dep->de_fc[FC_NEXTTOLASTFC].fc_fsrcn =
dep->de_fc[FC_LASTFC].fc_fsrcn;
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, CLUST_EOFE, &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);
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) {
while (got-- > 0) {
if (dep->de_Attributes & ATTR_DIRECTORY)
bp = getblk(pmp->pm_devvp,
cntobn(pmp, cn++),
pmp->pm_bpcluster, 0, 0, 0);
else {
bp = getblk(DETOV(dep),
frcn++,
pmp->pm_bpcluster, 0, 0, 0);
if (pcbmap(dep,
bp->b_lblkno,
&blkno, 0, 0))
bp->b_blkno = -1;
if (bp->b_blkno == -1)
panic("extendfile: pcbmap");
else
bp->b_blkno = blkno;
}
clrbuf(bp);
if (bpp) {
*bpp = bp;
bpp = NULL;
} else {
bdwrite(bp);
}
if (vm_page_count_severe() ||
buf_dirty_count_severe())
vn_fsync_buf(DETOV(dep), MNT_WAIT);
}
}
}
return (0);
}
int
markvoldirty_upgrade(struct msdosfsmount *pmp, bool dirty, bool rw_upgrade)
{
struct buf *bp;
u_long bn, bo, bsize, byteoffset, fatval;
int error;
if (FAT12(pmp))
return (0);
if ((pmp->pm_flags & MSDOSFSMNT_RONLY) != 0 && !rw_upgrade)
return (EROFS);
byteoffset = FATOFS(pmp, 1);
fatblock(pmp, byteoffset, &bn, &bsize, &bo);
error = bread(pmp->pm_devvp, bn, bsize, NOCRED, &bp);
if (error)
return (error);
if (FAT32(pmp)) {
fatval = getulong(&bp->b_data[bo]);
if (dirty)
fatval &= 0xF7FFFFFF;
else
fatval |= 0x08000000;
putulong(&bp->b_data[bo], fatval);
} else {
fatval = getushort(&bp->b_data[bo]);
if (dirty)
fatval &= 0x7FFF;
else
fatval |= 0x8000;
putushort(&bp->b_data[bo], fatval);
}
bp->b_flags |= B_INVALONERR;
return (bwrite(bp));
}