/*
* Copyright (c) 1994 Christian E. Hopps
* Copyright (c) 1996 Matthias Scheler
* All rights reserved.
*
* 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 Christian E. Hopps.
* 4. The name of the author may not be used to endorse or promote products
* derived from this software without specific prior written permission
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``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 THE AUTHOR 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.
*/
int adosfs_getattr(void *);
int adosfs_read(void *);
int adosfs_write(void *);
int adosfs_strategy(void *);
int adosfs_bmap(void *);
int adosfs_print(void *);
int adosfs_readdir(void *);
int adosfs_access(void *);
int adosfs_readlink(void *);
int adosfs_inactive(void *);
int adosfs_reclaim(void *);
int adosfs_pathconf(void *);
#ifdef ADOSFS_DIAGNOSTIC
advopprint(sp);
#endif
vap = sp->a_vap;
ap = VTOA(sp->a_vp);
amp = ap->amp;
vattr_null(vap);
vap->va_uid = ap->uid;
vap->va_gid = ap->gid;
vap->va_fsid = sp->a_vp->v_mount->mnt_stat.f_fsidx.__fsid_val[0];
vap->va_atime.tv_sec = vap->va_mtime.tv_sec = vap->va_ctime.tv_sec =
ap->mtime.days * 24 * 60 * 60 + ap->mtime.mins * 60 +
ap->mtime.ticks / 50 + (8 * 365 + 2) * 24 * 60 * 60;
vap->va_atime.tv_nsec = vap->va_mtime.tv_nsec = vap->va_ctime.tv_nsec = 0;
vap->va_gen = 0;
vap->va_flags = 0;
vap->va_rdev = NODEV;
vap->va_fileid = ap->block;
vap->va_type = sp->a_vp->v_type;
vap->va_mode = adunixprot(ap->adprot) & amp->mask;
if (sp->a_vp->v_type == VDIR) {
vap->va_nlink = 1; /* XXX bogus, oh well */
vap->va_bytes = amp->bsize;
vap->va_size = amp->bsize;
} else {
/*
* XXX actually we can track this if we were to walk the list
* of links if it exists.
* XXX for now, just set nlink to 2 if this is a hard link
* to a file, or a file with a hard link.
*/
vap->va_nlink = 1 + (ap->linkto != 0);
/*
* round up to nearest blocks add number of file list
* blocks needed and multiply by number of bytes per block.
*/
fblks = howmany(ap->fsize, amp->dbsize);
fblks += howmany(fblks, ANODENDATBLKENT(ap));
vap->va_bytes = fblks * amp->dbsize;
vap->va_size = ap->fsize;
vap->va_blocksize = amp->dbsize;
}
#ifdef ADOSFS_DIAGNOSTIC
printf(" 0)");
#endif
return(0);
}
/*
* are things locked??? they need to be to avoid this being
* deleted or changed (data block pointer blocks moving about.)
*/
int
adosfs_read(void *v)
{
struct vop_read_args /* {
struct vnode *a_vp;
struct uio *a_uio;
int a_ioflag;
kauth_cred_t a_cred;
} */ *sp = v;
struct vnode *vp = sp->a_vp;
struct adosfsmount *amp;
struct anode *ap;
struct uio *uio;
struct buf *bp;
daddr_t lbn;
int size, diff, error;
long n, on;
#ifdef ADOSFS_DIAGNOSTIC
advopprint(sp);
#endif
error = 0;
uio = sp->a_uio;
ap = VTOA(sp->a_vp);
amp = ap->amp;
/*
* Return EOF for character devices, EIO for others
*/
if (sp->a_vp->v_type != VREG) {
error = EIO;
goto reterr;
}
if (uio->uio_resid == 0)
goto reterr;
if (uio->uio_offset < 0) {
error = EINVAL;
goto reterr;
}
/*
* to expensive to let general algorithm figure out that
* we are beyond the file. Do it now.
*/
if (uio->uio_offset >= ap->fsize)
goto reterr;
/*
* taken from ufs_read()
*/
if (vp->v_type == VREG && IS_FFS(amp)) {
const int advice = IO_ADV_DECODE(sp->a_ioflag);
error = 0;
if (sp->a_vpp != NULL)
*sp->a_vpp = ap->amp->devvp;
if (bnp == NULL)
goto reterr;
if (bn < 0) {
error = EFBIG;
goto reterr;
}
if (sp->a_vp->v_type != VREG) {
error = EINVAL;
goto reterr;
}
/*
* walk the chain of file list blocks until we find
* the one that will yield the block pointer we need.
*/
if (ap->type == AFILE)
nb = ap->block; /* pointer to ourself */
else if (ap->type == ALFILE)
nb = ap->linkto; /* pointer to real file */
else {
error = EINVAL;
goto reterr;
}
flblk = bn / ANODENDATBLKENT(ap);
flbp = NULL;
/*
* check last indirect block cache
*/
if (flblk < ap->lastlindblk)
fcnt = 0;
else {
flblk -= ap->lastlindblk;
fcnt = ap->lastlindblk;
nb = ap->lastindblk;
}
while (flblk >= 0) {
if (flbp)
brelse(flbp, 0);
if (nb == 0) {
#ifdef DIAGNOSTIC
printf("adosfs: bad file list chain.\n");
#endif
error = EINVAL;
goto reterr;
}
error = bread(ap->amp->devvp, nb * ap->amp->bsize / DEV_BSIZE,
ap->amp->bsize, 0, &flbp);
if (error) {
goto reterr;
}
if (adoscksum(flbp, ap->nwords)) {
#ifdef DIAGNOSTIC
printf("adosfs: blk %ld failed cksum.\n", nb);
#endif
brelse(flbp, 0);
error = EINVAL;
goto reterr;
}
/*
* update last indirect block cache
*/
ap->lastlindblk = fcnt++;
ap->lastindblk = nb;
nb = adoswordn(flbp, ap->nwords - 2);
flblk--;
}
/*
* calculate offset of block number in table. The table starts
* at nwords - 51 and goes to offset 6 or less if indicated by the
* valid table entries stored at offset ADBI_NBLKTABENT.
*/
flblkoff = bn % ANODENDATBLKENT(ap);
if (flblkoff < adoswordn(flbp, 2 /* ADBI_NBLKTABENT */)) {
flblkoff = (ap->nwords - 51) - flblkoff;
*bnp = adoswordn(flbp, flblkoff) * ap->amp->bsize / DEV_BSIZE;
} else {
#ifdef DIAGNOSTIC
printf("flblk offset %ld too large in lblk %ld blk %" PRId64 "\n",
flblkoff, (long)bn, flbp->b_blkno);
#endif
error = EINVAL;
}
brelse(flbp, 0);
reterr:
#ifdef ADOSFS_DIAGNOSTIC
if (error == 0 && bnp)
printf(" %lld => %lld", (long long)bn, (long long)*bnp);
printf(" %d)\n", error);
#endif
return(error);
}
/*
* Print out the contents of a adosfs vnode.
*/
/* ARGSUSED */
int
adosfs_print(void *v)
{
#if 0
struct vop_print_args /* {
struct vnode *a_vp;
} */ *sp = v;
#endif
return(0);
}
/*
* Now [continue to] walk the chain
*/
ap = NULL;
do {
error = VFS_VGET(pap->amp->mp, (ino_t)nextbn,
LK_EXCLUSIVE, &vp);
if (error)
goto reterr;
ap = VTOA(vp);
scanned++;
chainc++;
nextbn = ap->hashf;
/*
* check for end of chain.
*/
if (nextbn == 0) {
pap->tabi[hashi] = chainc;
hashi++;
chainc = 0;
} else if (pap->tabi[hashi] <= 0 &&
-chainc < pap->tabi[hashi])
pap->tabi[hashi] = -chainc;
if (useri >= scanned) {
vput(vp);
ap = NULL;
}
} while (ap == NULL && nextbn != 0);
/*
* We left the loop but without a result so do main over.
*/
if (ap == NULL)
continue;
/*
* Fill in dirent record
*/
memset(adp, 0, sizeof *adp);
adp->d_fileno = ap->block;
/*
* This deserves a function in kern/vfs_subr.c
*/
switch (ATOV(ap)->v_type) {
case VREG:
adp->d_type = DT_REG;
break;
case VDIR:
adp->d_type = DT_DIR;
break;
case VLNK:
adp->d_type = DT_LNK;
break;
default:
adp->d_type = DT_UNKNOWN;
break;
}
adp->d_namlen = strlen(ap->name);
memcpy(adp->d_name, ap->name, adp->d_namlen);
adp->d_reclen = _DIRENT_SIZE(adp);
vput(vp);
if (adp->d_reclen > uio->uio_resid) {
if (useri == first) /* no room for even one entry */
error = EINVAL;
break;
}
error = uiomove(adp, adp->d_reclen, uio);
if (error)
break;
useri++;
}
ncookies = useri - first;
uio->uio_offset = uoff + ncookies * sizeof ad;
reterr:
#ifdef ADOSFS_DIAGNOSTIC
printf(" %d)", error);
#endif
if (sp->a_ncookies != NULL) {
*sp->a_ncookies = ncookies;
if (!error) {
*sp->a_cookies = cookies =
malloc(ncookies * sizeof *cookies, M_TEMP, M_WAITOK);
/*
* Disallow write attempts unless the file is a socket,
* fifo, or a block or character device resident on the
* file system.
*/
if (accmode & VWRITE) {
switch (vp->v_type) {
case VDIR:
case VLNK:
case VREG:
return (EROFS);
default:
break;
}
}
/*
* the kernel wants its vnode back.
* no lock needed we are being called from vclean()
*/
int
adosfs_reclaim(void *v)
{
struct vop_reclaim_v2_args /* {
struct vnode *a_vp;
} */ *sp = v;
struct vnode *vp;
struct anode *ap;
/*
* POSIX pathconf info, grabbed from kern/u fs, probably need to
* investigate exactly what each return type means as they are probably
* not valid currently
*/
int
adosfs_pathconf(void *v)
{
struct vop_pathconf_args /* {
struct vnode *a_vp;
int a_name;
register_t *a_retval;
} */ *ap = v;
switch (ap->a_name) {
case _PC_LINK_MAX:
*ap->a_retval = LINK_MAX;
return (0);
case _PC_NAME_MAX:
*ap->a_retval = ap->a_vp->v_mount->mnt_stat.f_namemax;
return (0);
case _PC_PATH_MAX:
*ap->a_retval = PATH_MAX;
return (0);
case _PC_PIPE_BUF:
*ap->a_retval = PIPE_BUF;
return (0);
case _PC_CHOWN_RESTRICTED:
*ap->a_retval = 1;
return (0);
case _PC_VDISABLE:
*ap->a_retval = _POSIX_VDISABLE;
return (0);
case _PC_SYNC_IO:
*ap->a_retval = 1;
return (0);
case _PC_FILESIZEBITS:
*ap->a_retval = 32;
return (0);
default:
return genfs_pathconf(ap);
}
/* NOTREACHED */
}