#include <sys/param.h>
#include <sys/systm.h>
#include <sys/buf.h>
#include <sys/clock.h>
#include <sys/malloc.h>
#include <sys/mount.h>
#include <sys/vmmeter.h>
#include <sys/vnode.h>
#include <vm/vm_extern.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>
static int
de_vncmpf(struct vnode *vp, void *arg)
{
struct denode *de;
uint64_t *a;
a = arg;
de = VTODE(vp);
return (de->de_inode != *a) || (de->de_refcnt <= 0);
}
int
deget(struct msdosfsmount *pmp, u_long dirclust, u_long diroffset,
int lkflags, struct denode **depp)
{
int error;
uint64_t inode;
struct mount *mntp = pmp->pm_mountp;
struct direntry *direntptr;
struct denode *ldep;
struct vnode *nvp, *xvp;
struct buf *bp;
#ifdef MSDOSFS_DEBUG
printf("deget(pmp %p, dirclust %lu, diroffset %lx, flags %#x, "
"depp %p)\n",
pmp, dirclust, diroffset, lkflags, depp);
#endif
MPASS((lkflags & LK_TYPE_MASK) == LK_EXCLUSIVE);
if (FAT32(pmp) && dirclust == MSDOSFSROOT)
dirclust = pmp->pm_rootdirblk;
inode = DETOI(pmp, dirclust, diroffset);
error = vfs_hash_get(mntp, inode, lkflags, curthread, &nvp,
de_vncmpf, &inode);
#ifdef MSDOSFS_DEBUG
printf("vfs_hash_get(inode %lu) error %d\n", inode, error);
#endif
if (error)
return (error);
if (nvp != NULL) {
*depp = VTODE(nvp);
if ((*depp)->de_dirclust != dirclust) {
printf("%s: wrong dir cluster %lu %lu\n",
pmp->pm_mountp->mnt_stat.f_mntonname,
(*depp)->de_dirclust, dirclust);
goto badoff;
}
if ((*depp)->de_diroffset != diroffset) {
printf("%s: wrong dir offset %lu %lu\n",
pmp->pm_mountp->mnt_stat.f_mntonname,
(*depp)->de_diroffset, diroffset);
goto badoff;
}
return (0);
badoff:
vgone(nvp);
vput(nvp);
msdosfs_integrity_error(pmp);
return (EBADF);
}
ldep = malloc(sizeof(struct denode), M_MSDOSFSNODE, M_WAITOK | M_ZERO);
error = getnewvnode("msdosfs", mntp, &msdosfs_vnodeops, &nvp);
if (error) {
*depp = NULL;
free(ldep, M_MSDOSFSNODE);
return error;
}
nvp->v_data = ldep;
ldep->de_vnode = nvp;
ldep->de_flag = 0;
ldep->de_dirclust = dirclust;
ldep->de_diroffset = diroffset;
ldep->de_inode = inode;
cluster_init_vn(&ldep->de_clusterw);
lockmgr(nvp->v_vnlock, LK_EXCLUSIVE | LK_NOWITNESS, NULL);
VN_LOCK_AREC(nvp);
fc_purge(ldep, 0);
error = insmntque(nvp, mntp);
if (error != 0) {
free(ldep, M_MSDOSFSNODE);
*depp = NULL;
return (error);
}
error = vfs_hash_insert(nvp, inode, lkflags, curthread, &xvp,
de_vncmpf, &inode);
#ifdef MSDOSFS_DEBUG
printf("vfs_hash_insert(inode %lu) error %d\n", inode, error);
#endif
if (error) {
*depp = NULL;
return (error);
}
if (xvp != NULL) {
*depp = xvp->v_data;
return (0);
}
ldep->de_pmp = pmp;
ldep->de_refcnt = 1;
if ((dirclust == MSDOSFSROOT ||
(FAT32(pmp) && dirclust == pmp->pm_rootdirblk)) &&
diroffset == MSDOSFSROOT_OFS) {
nvp->v_vflag |= VV_ROOT;
ldep->de_Attributes = ATTR_DIRECTORY;
ldep->de_LowerCase = 0;
if (FAT32(pmp))
ldep->de_StartCluster = pmp->pm_rootdirblk;
else {
ldep->de_StartCluster = MSDOSFSROOT;
ldep->de_FileSize = pmp->pm_rootdirsize * DEV_BSIZE;
}
ldep->de_CHun = 0;
ldep->de_CTime = 0x0000;
ldep->de_CDate = (0 << DD_YEAR_SHIFT) | (1 << DD_MONTH_SHIFT)
| (1 << DD_DAY_SHIFT);
ldep->de_ADate = ldep->de_CDate;
ldep->de_MTime = ldep->de_CTime;
ldep->de_MDate = ldep->de_CDate;
} else {
error = readep(pmp, dirclust, diroffset, &bp, &direntptr);
if (error) {
ldep->de_Name[0] = SLOT_DELETED;
vgone(nvp);
vput(nvp);
*depp = NULL;
return (error);
}
(void)DE_INTERNALIZE(ldep, direntptr);
brelse(bp);
}
if (ldep->de_Attributes & ATTR_DIRECTORY) {
u_long size;
if (diroffset == 0 && ldep->de_StartCluster != dirclust) {
#ifdef MSDOSFS_DEBUG
printf("deget(): \".\" entry at clust %lu != %lu\n",
dirclust, ldep->de_StartCluster);
#endif
ldep->de_StartCluster = dirclust;
}
nvp->v_type = VDIR;
if (ldep->de_StartCluster != MSDOSFSROOT) {
error = pcbmap(ldep, 0xffff, 0, &size, 0);
if (error == E2BIG) {
ldep->de_FileSize = de_cn2off(pmp, size);
error = 0;
} else {
#ifdef MSDOSFS_DEBUG
printf("deget(): pcbmap returned %d\n", error);
#endif
}
}
} else
nvp->v_type = VREG;
vn_set_state(nvp, VSTATE_CONSTRUCTED);
ldep->de_modrev = init_va_filerev();
*depp = ldep;
return (0);
}
int
deupdat(struct denode *dep, int waitfor)
{
struct direntry dir;
struct timespec ts;
struct buf *bp;
struct direntry *dirp;
int error;
if (DETOV(dep)->v_mount->mnt_flag & MNT_RDONLY) {
dep->de_flag &= ~(DE_UPDATE | DE_CREATE | DE_ACCESS |
DE_MODIFIED);
return (0);
}
vfs_timestamp(&ts);
DETIMES(dep, &ts, &ts, &ts);
if ((dep->de_flag & DE_MODIFIED) == 0 && waitfor == 0)
return (0);
dep->de_flag &= ~DE_MODIFIED;
if (DETOV(dep)->v_vflag & VV_ROOT)
return (EINVAL);
if (dep->de_refcnt <= 0)
return (0);
error = readde(dep, &bp, &dirp);
if (error)
return (error);
DE_EXTERNALIZE(&dir, dep);
if (bcmp(dirp, &dir, sizeof(dir)) == 0) {
if (waitfor == 0 || (bp->b_flags & B_DELWRI) == 0) {
brelse(bp);
return (0);
}
} else
*dirp = dir;
if ((DETOV(dep)->v_mount->mnt_flag & MNT_NOCLUSTERW) == 0)
bp->b_flags |= B_CLUSTEROK;
if (waitfor)
error = bwrite(bp);
else if (vm_page_count_severe() || buf_dirty_count_severe())
bawrite(bp);
else
bdwrite(bp);
return (error);
}
int
detrunc(struct denode *dep, u_long length, int flags, struct ucred *cred)
{
int error;
int allerror;
u_long eofentry;
u_long chaintofree;
daddr_t bn;
int boff;
int isadir = dep->de_Attributes & ATTR_DIRECTORY;
struct buf *bp;
struct msdosfsmount *pmp = dep->de_pmp;
#ifdef MSDOSFS_DEBUG
printf("detrunc(): file %s, length %lu, flags %x\n", dep->de_Name, length, flags);
#endif
if ((DETOV(dep)->v_vflag & VV_ROOT) && !FAT32(pmp)) {
#ifdef MSDOSFS_DEBUG
printf("detrunc(): can't truncate root directory, clust %ld, offset %ld\n",
dep->de_dirclust, dep->de_diroffset);
#endif
return (EINVAL);
}
if (dep->de_FileSize < length)
return (deextend(dep, length, cred));
if (length == 0) {
chaintofree = dep->de_StartCluster;
dep->de_StartCluster = 0;
eofentry = ~0;
} else {
error = pcbmap(dep, de_clcount(pmp, length) - 1, 0,
&eofentry, 0);
if (error) {
#ifdef MSDOSFS_DEBUG
printf("detrunc(): pcbmap fails %d\n", error);
#endif
return (error);
}
}
fc_purge(dep, de_clcount(pmp, length));
if ((boff = length & pmp->pm_crbomask) != 0) {
if (isadir) {
bn = cntobn(pmp, eofentry);
error = bread(pmp->pm_devvp, bn, pmp->pm_bpcluster,
NOCRED, &bp);
} else {
error = bread(DETOV(dep), de_cluster(pmp, length),
pmp->pm_bpcluster, cred, &bp);
}
if (error) {
#ifdef MSDOSFS_DEBUG
printf("detrunc(): bread fails %d\n", error);
#endif
return (error);
}
memset(bp->b_data + boff, 0, pmp->pm_bpcluster - boff);
if ((flags & IO_SYNC) != 0)
bwrite(bp);
else
bdwrite(bp);
}
dep->de_FileSize = length;
if (!isadir)
dep->de_flag |= DE_UPDATE | DE_MODIFIED;
allerror = vtruncbuf(DETOV(dep), length, pmp->pm_bpcluster);
#ifdef MSDOSFS_DEBUG
if (allerror)
printf("detrunc(): vtruncbuf error %d\n", allerror);
#endif
error = deupdat(dep, !DOINGASYNC((DETOV(dep))));
if (error != 0 && allerror == 0)
allerror = error;
#ifdef MSDOSFS_DEBUG
printf("detrunc(): allerror %d, eofentry %lu\n",
allerror, eofentry);
#endif
if (eofentry != (u_long)~0) {
error = fatentry(FAT_GET_AND_SET, pmp, eofentry,
&chaintofree, CLUST_EOFE);
if (error) {
#ifdef MSDOSFS_DEBUG
printf("detrunc(): fatentry errors %d\n", error);
#endif
return (error);
}
fc_setcache(dep, FC_LASTFC, de_cluster(pmp, length - 1),
eofentry);
}
if (chaintofree != 0 && !MSDOSFSEOF(pmp, chaintofree))
freeclusterchain(pmp, chaintofree);
return (allerror);
}
int
deextend(struct denode *dep, u_long length, struct ucred *cred)
{
struct msdosfsmount *pmp = dep->de_pmp;
struct vnode *vp = DETOV(dep);
struct buf *bp;
off_t eof_clusteroff;
u_long count;
int error;
if ((vp->v_vflag & VV_ROOT) != 0 && !FAT32(pmp))
return (EINVAL);
if (dep->de_Attributes & ATTR_DIRECTORY)
return (EISDIR);
if (length <= dep->de_FileSize)
panic("deextend: file too large");
count = de_clcount(pmp, length) - de_clcount(pmp, dep->de_FileSize);
if (count > 0) {
if (count > pmp->pm_freeclustercount)
return (ENOSPC);
error = extendfile(dep, count, NULL, NULL, DE_CLEAR);
if (error != 0)
goto rewind;
}
error = bread(vp, de_cluster(pmp, dep->de_FileSize), pmp->pm_bpcluster,
NOCRED, &bp);
if (error != 0)
goto rewind;
vfs_bio_clrbuf(bp);
eof_clusteroff = de_cn2off(pmp, de_cluster(pmp, dep->de_FileSize));
vfs_bio_bzero_buf(bp, dep->de_FileSize - eof_clusteroff,
pmp->pm_bpcluster - dep->de_FileSize + eof_clusteroff);
if (!DOINGASYNC(vp))
(void)bwrite(bp);
else if (vm_page_count_severe() || buf_dirty_count_severe())
bawrite(bp);
else
bdwrite(bp);
vnode_pager_setsize(vp, length);
dep->de_FileSize = length;
dep->de_flag |= DE_UPDATE | DE_MODIFIED;
return (deupdat(dep, !DOINGASYNC(vp)));
rewind:
(void)detrunc(dep, dep->de_FileSize, 0, cred);
return (error);
}
#if 0
void
reinsert(struct denode *dep)
{
struct vnode *vp;
#if 0
if (dep->de_Attributes & ATTR_DIRECTORY)
return;
#endif
vp = DETOV(dep);
dep->de_inode = DETOI(dep->de_pmp, dep->de_dirclust, dep->de_diroffset);
#ifdef MSDOSFS_DEBUG
printf("vfs_hash_rehash(inode %lu, refcnt %lu, vp %p)\n",
dep->de_inode, dep->de_refcnt, vp);
#endif
vfs_hash_rehash(vp, dep->de_inode);
}
#endif
#if 0
int
msdosfs_reclaim(struct vop_reclaim_args *ap)
{
struct vnode *vp = ap->a_vp;
struct denode *dep = VTODE(vp);
#ifdef MSDOSFS_DEBUG
printf("msdosfs_reclaim(): dep %p, file %s, refcnt %ld\n",
dep, dep->de_Name, dep->de_refcnt);
#endif
#ifdef MSDOSFS_DEBUG
printf("vfs_hash_remove(inode %lu, refcnt %lu, vp %p)\n",
dep->de_inode, dep->de_refcnt, vp);
#endif
vfs_hash_remove(vp);
#if 0
dep->de_flag = 0;
#endif
free(dep, M_MSDOSFSNODE);
vp->v_data = NULL;
return (0);
}
#endif
#if 0
int
msdosfs_inactive(struct vop_inactive_args *ap)
{
struct vnode *vp = ap->a_vp;
struct denode *dep = VTODE(vp);
int error = 0;
#ifdef MSDOSFS_DEBUG
printf("msdosfs_inactive(): dep %p, de_Name[0] %x\n", dep, dep->de_Name[0]);
#endif
if (dep->de_Name[0] == SLOT_DELETED || dep->de_Name[0] == SLOT_EMPTY)
goto out;
#ifdef MSDOSFS_DEBUG
printf("msdosfs_inactive(): dep %p, refcnt %ld, mntflag %llx, MNT_RDONLY %llx\n",
dep, dep->de_refcnt, (unsigned long long)vp->v_mount->mnt_flag,
(unsigned long long)MNT_RDONLY);
#endif
if (dep->de_refcnt <= 0 && (vp->v_mount->mnt_flag & MNT_RDONLY) == 0) {
error = detrunc(dep, (u_long) 0, 0, NOCRED);
dep->de_flag |= DE_UPDATE;
dep->de_Name[0] = SLOT_DELETED;
}
deupdat(dep, 0);
out:
#ifdef MSDOSFS_DEBUG
printf("msdosfs_inactive(): v_usecount %d, de_Name[0] %x\n",
vrefcnt(vp), dep->de_Name[0]);
#endif
if (dep->de_Name[0] == SLOT_DELETED || dep->de_Name[0] == SLOT_EMPTY)
vrecycle(vp);
return (error);
}
#endif