Home | History | Annotate | Line # | Download | only in adosfs
advnops.c revision 1.17
      1 /*	$NetBSD: advnops.c,v 1.17 2005/11/29 22:52:02 yamt Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1994 Christian E. Hopps
      5  * Copyright (c) 1996 Matthias Scheler
      6  * All rights reserved.
      7  *
      8  * Redistribution and use in source and binary forms, with or without
      9  * modification, are permitted provided that the following conditions
     10  * are met:
     11  * 1. Redistributions of source code must retain the above copyright
     12  *    notice, this list of conditions and the following disclaimer.
     13  * 2. Redistributions in binary form must reproduce the above copyright
     14  *    notice, this list of conditions and the following disclaimer in the
     15  *    documentation and/or other materials provided with the distribution.
     16  * 3. All advertising materials mentioning features or use of this software
     17  *    must display the following acknowledgement:
     18  *      This product includes software developed by Christian E. Hopps.
     19  * 4. The name of the author may not be used to endorse or promote products
     20  *    derived from this software without specific prior written permission
     21  *
     22  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     23  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     24  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     25  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     26  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     27  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     28  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     29  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     30  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     31  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     32  */
     33 
     34 #include <sys/cdefs.h>
     35 __KERNEL_RCSID(0, "$NetBSD: advnops.c,v 1.17 2005/11/29 22:52:02 yamt Exp $");
     36 
     37 #if defined(_KERNEL_OPT)
     38 #include "opt_quota.h"
     39 #endif
     40 
     41 #include <sys/param.h>
     42 #include <sys/systm.h>
     43 #include <sys/vnode.h>
     44 #include <sys/mount.h>
     45 #include <sys/time.h>
     46 #include <sys/queue.h>
     47 #include <sys/namei.h>
     48 #include <sys/buf.h>
     49 #include <sys/dirent.h>
     50 #include <sys/malloc.h>
     51 #include <sys/pool.h>
     52 #include <sys/stat.h>
     53 #include <sys/unistd.h>
     54 #include <sys/proc.h>
     55 
     56 #include <miscfs/genfs/genfs.h>
     57 #include <miscfs/specfs/specdev.h>
     58 #include <fs/adosfs/adosfs.h>
     59 
     60 extern struct vnodeops adosfs_vnodeops;
     61 
     62 #define	adosfs_open	genfs_nullop
     63 int	adosfs_getattr	__P((void *));
     64 int	adosfs_read	__P((void *));
     65 int	adosfs_write	__P((void *));
     66 #define	adosfs_fcntl	genfs_fcntl
     67 #define	adosfs_ioctl	genfs_enoioctl
     68 #define	adosfs_poll	genfs_poll
     69 int	adosfs_strategy	__P((void *));
     70 int	adosfs_link	__P((void *));
     71 int	adosfs_symlink	__P((void *));
     72 #define	adosfs_abortop	genfs_abortop
     73 int	adosfs_bmap	__P((void *));
     74 int	adosfs_print	__P((void *));
     75 int	adosfs_readdir	__P((void *));
     76 int	adosfs_access	__P((void *));
     77 int	adosfs_readlink	__P((void *));
     78 int	adosfs_inactive	__P((void *));
     79 int	adosfs_reclaim	__P((void *));
     80 int	adosfs_pathconf	__P((void *));
     81 
     82 #define adosfs_close 	genfs_nullop
     83 #define adosfs_fsync 	genfs_nullop
     84 #define	adosfs_lease_check	genfs_lease_check
     85 #define adosfs_seek 	genfs_seek
     86 
     87 #define adosfs_advlock 	genfs_einval
     88 #define adosfs_bwrite 	genfs_eopnotsupp
     89 #define adosfs_create 	genfs_eopnotsupp
     90 #define adosfs_mkdir 	genfs_eopnotsupp
     91 #define adosfs_mknod 	genfs_eopnotsupp
     92 #define adosfs_revoke	genfs_revoke
     93 #define adosfs_mmap 	genfs_mmap
     94 #define adosfs_remove 	genfs_eopnotsupp
     95 #define adosfs_rename 	genfs_eopnotsupp
     96 #define adosfs_rmdir 	genfs_eopnotsupp
     97 #define adosfs_setattr 	genfs_eopnotsupp
     98 
     99 const struct vnodeopv_entry_desc adosfs_vnodeop_entries[] = {
    100 	{ &vop_default_desc, vn_default_error },
    101 	{ &vop_lookup_desc, adosfs_lookup },		/* lookup */
    102 	{ &vop_create_desc, adosfs_create },		/* create */
    103 	{ &vop_mknod_desc, adosfs_mknod },		/* mknod */
    104 	{ &vop_open_desc, adosfs_open },		/* open */
    105 	{ &vop_close_desc, adosfs_close },		/* close */
    106 	{ &vop_access_desc, adosfs_access },		/* access */
    107 	{ &vop_getattr_desc, adosfs_getattr },		/* getattr */
    108 	{ &vop_setattr_desc, adosfs_setattr },		/* setattr */
    109 	{ &vop_read_desc, adosfs_read },		/* read */
    110 	{ &vop_write_desc, adosfs_write },		/* write */
    111 	{ &vop_lease_desc, adosfs_lease_check },	/* lease */
    112 	{ &vop_fcntl_desc, adosfs_fcntl },		/* fcntl */
    113 	{ &vop_ioctl_desc, adosfs_ioctl },		/* ioctl */
    114 	{ &vop_poll_desc, adosfs_poll },		/* poll */
    115 	{ &vop_kqfilter_desc, genfs_kqfilter },		/* kqfilter */
    116 	{ &vop_revoke_desc, adosfs_revoke },		/* revoke */
    117 	{ &vop_mmap_desc, adosfs_mmap },		/* mmap */
    118 	{ &vop_fsync_desc, adosfs_fsync },		/* fsync */
    119 	{ &vop_seek_desc, adosfs_seek },		/* seek */
    120 	{ &vop_remove_desc, adosfs_remove },		/* remove */
    121 	{ &vop_link_desc, adosfs_link },		/* link */
    122 	{ &vop_rename_desc, adosfs_rename },		/* rename */
    123 	{ &vop_mkdir_desc, adosfs_mkdir },		/* mkdir */
    124 	{ &vop_rmdir_desc, adosfs_rmdir },		/* rmdir */
    125 	{ &vop_symlink_desc, adosfs_symlink },		/* symlink */
    126 	{ &vop_readdir_desc, adosfs_readdir },		/* readdir */
    127 	{ &vop_readlink_desc, adosfs_readlink },	/* readlink */
    128 	{ &vop_abortop_desc, adosfs_abortop },		/* abortop */
    129 	{ &vop_inactive_desc, adosfs_inactive },	/* inactive */
    130 	{ &vop_reclaim_desc, adosfs_reclaim },		/* reclaim */
    131 	{ &vop_lock_desc, genfs_lock },			/* lock */
    132 	{ &vop_unlock_desc, genfs_unlock },		/* unlock */
    133 	{ &vop_bmap_desc, adosfs_bmap },		/* bmap */
    134 	{ &vop_strategy_desc, adosfs_strategy },	/* strategy */
    135 	{ &vop_print_desc, adosfs_print },		/* print */
    136 	{ &vop_islocked_desc, genfs_islocked },		/* islocked */
    137 	{ &vop_pathconf_desc, adosfs_pathconf },	/* pathconf */
    138 	{ &vop_advlock_desc, adosfs_advlock },		/* advlock */
    139 	{ &vop_bwrite_desc, adosfs_bwrite },		/* bwrite */
    140 	{ &vop_getpages_desc, genfs_getpages },		/* getpages */
    141 	{ &vop_putpages_desc, genfs_putpages },		/* putpages */
    142 	{ NULL, NULL }
    143 };
    144 
    145 const struct vnodeopv_desc adosfs_vnodeop_opv_desc =
    146 	{ &adosfs_vnodeop_p, adosfs_vnodeop_entries };
    147 
    148 int
    149 adosfs_getattr(v)
    150 	void *v;
    151 {
    152 	struct vop_getattr_args /* {
    153 		struct vnode *a_vp;
    154 		struct vattr *a_vap;
    155 		struct ucred *a_cred;
    156 		struct proc *a_p;
    157 	} */ *sp = v;
    158 	struct vattr *vap;
    159 	struct adosfsmount *amp;
    160 	struct anode *ap;
    161 	u_long fblks;
    162 
    163 #ifdef ADOSFS_DIAGNOSTIC
    164 	advopprint(sp);
    165 #endif
    166 	vap = sp->a_vap;
    167 	ap = VTOA(sp->a_vp);
    168 	amp = ap->amp;
    169 	vattr_null(vap);
    170 	vap->va_uid = ap->uid;
    171 	vap->va_gid = ap->gid;
    172 	vap->va_fsid = sp->a_vp->v_mount->mnt_stat.f_fsidx.__fsid_val[0];
    173 	vap->va_atime.tv_sec = vap->va_mtime.tv_sec = vap->va_ctime.tv_sec =
    174 		ap->mtime.days * 24 * 60 * 60 + ap->mtime.mins * 60 +
    175 		ap->mtime.ticks / 50 + (8 * 365 + 2) * 24 * 60 * 60;
    176 	vap->va_atime.tv_nsec = vap->va_mtime.tv_nsec = vap->va_ctime.tv_nsec = 0;
    177 	vap->va_gen = 0;
    178 	vap->va_flags = 0;
    179 	vap->va_rdev = NODEV;
    180 	vap->va_fileid = ap->block;
    181 	vap->va_type = sp->a_vp->v_type;
    182 	vap->va_mode = adunixprot(ap->adprot) & amp->mask;
    183 	if (sp->a_vp->v_type == VDIR) {
    184 		vap->va_nlink = 1;	/* XXX bogus, oh well */
    185 		vap->va_bytes = amp->bsize;
    186 		vap->va_size = amp->bsize;
    187 	} else {
    188 		/*
    189 		 * XXX actually we can track this if we were to walk the list
    190 		 * of links if it exists.
    191 		 * XXX for now, just set nlink to 2 if this is a hard link
    192 		 * to a file, or a file with a hard link.
    193 		 */
    194 		vap->va_nlink = 1 + (ap->linkto != 0);
    195 		/*
    196 		 * round up to nearest blocks add number of file list
    197 		 * blocks needed and mutiply by number of bytes per block.
    198 		 */
    199 		fblks = howmany(ap->fsize, amp->dbsize);
    200 		fblks += howmany(fblks, ANODENDATBLKENT(ap));
    201 		vap->va_bytes = fblks * amp->dbsize;
    202 		vap->va_size = ap->fsize;
    203 
    204 		vap->va_blocksize = amp->dbsize;
    205 	}
    206 #ifdef ADOSFS_DIAGNOSTIC
    207 	printf(" 0)");
    208 #endif
    209 	return(0);
    210 }
    211 /*
    212  * are things locked??? they need to be to avoid this being
    213  * deleted or changed (data block pointer blocks moving about.)
    214  */
    215 int
    216 adosfs_read(v)
    217 	void *v;
    218 {
    219 	struct vop_read_args /* {
    220 		struct vnode *a_vp;
    221 		struct uio *a_uio;
    222 		int a_ioflag;
    223 		struct ucred *a_cred;
    224 	} */ *sp = v;
    225 	struct vnode *vp = sp->a_vp;
    226 	struct adosfsmount *amp;
    227 	struct anode *ap;
    228 	struct uio *uio;
    229 	struct buf *bp;
    230 	daddr_t lbn;
    231 	int size, diff, error;
    232 	long n, on;
    233 
    234 #ifdef ADOSFS_DIAGNOSTIC
    235 	advopprint(sp);
    236 #endif
    237 	error = 0;
    238 	uio = sp->a_uio;
    239 	ap = VTOA(sp->a_vp);
    240 	amp = ap->amp;
    241 	/*
    242 	 * Return EOF for character devices, EIO for others
    243 	 */
    244 	if (sp->a_vp->v_type != VREG) {
    245 		error = EIO;
    246 		goto reterr;
    247 	}
    248 	if (uio->uio_resid == 0)
    249 		goto reterr;
    250 	if (uio->uio_offset < 0) {
    251 		error = EINVAL;
    252 		goto reterr;
    253 	}
    254 
    255 	/*
    256 	 * to expensive to let general algorithm figure out that
    257 	 * we are beyond the file.  Do it now.
    258 	 */
    259 	if (uio->uio_offset >= ap->fsize)
    260 		goto reterr;
    261 
    262 	/*
    263 	 * taken from ufs_read()
    264 	 */
    265 
    266 	if (vp->v_type == VREG && IS_FFS(amp)) {
    267 		const int advice = IO_ADV_DECODE(sp->a_ioflag);
    268 		error = 0;
    269 
    270 		while (uio->uio_resid > 0) {
    271 			void *win;
    272 			int flags;
    273 			vsize_t bytelen = MIN(ap->fsize - uio->uio_offset,
    274 					      uio->uio_resid);
    275 
    276 			if (bytelen == 0) {
    277 				break;
    278 			}
    279 			win = ubc_alloc(&vp->v_uobj, uio->uio_offset,
    280 					&bytelen, advice, UBC_READ);
    281 			error = uiomove(win, bytelen, uio);
    282 			flags = UBC_WANT_UNMAP(vp) ? UBC_UNMAP : 0;
    283 			ubc_release(win, flags);
    284 			if (error) {
    285 				break;
    286 			}
    287 		}
    288 		goto out;
    289 	}
    290 
    291 	do {
    292 		size = amp->dbsize;
    293 		lbn = uio->uio_offset / size;
    294 		on = uio->uio_offset % size;
    295 		n = MIN(size - on, uio->uio_resid);
    296 		diff = ap->fsize - uio->uio_offset;
    297 		/*
    298 		 * check for EOF
    299 		 */
    300 		if (diff <= 0)
    301 			return(0);
    302 		if (diff < n)
    303 			n = diff;
    304 		/*
    305 		 * read ahead could possibly be worth something
    306 		 * but not much as ados makes little attempt to
    307 		 * make things contigous
    308 		 */
    309 		error = bread(sp->a_vp, lbn, amp->bsize, NOCRED, &bp);
    310 		if (error) {
    311 			brelse(bp);
    312 			goto reterr;
    313 		}
    314 		if (!IS_FFS(amp)) {
    315 			if (bp->b_resid > 0)
    316 				error = EIO; /* OFS needs the complete block */
    317 			else if (adoswordn(bp, 0) != BPT_DATA) {
    318 #ifdef DIAGNOSTIC
    319 				printf("adosfs: bad primary type blk %" PRId64 "\n",
    320 				    bp->b_blkno / (amp->bsize / DEV_BSIZE));
    321 #endif
    322 				error = EINVAL;
    323 			} else if (adoscksum(bp, ap->nwords)) {
    324 #ifdef DIAGNOSTIC
    325 				printf("adosfs: blk %" PRId64 " failed cksum.\n",
    326 				    bp->b_blkno / (amp->bsize / DEV_BSIZE));
    327 #endif
    328 				error = EINVAL;
    329 			}
    330 		}
    331 
    332 		if (error) {
    333 			brelse(bp);
    334 			goto reterr;
    335 		}
    336 #ifdef ADOSFS_DIAGNOSTIC
    337 		printf(" %d+%d-%d+%d", lbn, on, lbn, n);
    338 #endif
    339 		n = MIN(n, size - bp->b_resid);
    340 		error = uiomove(bp->b_data + on +
    341 				amp->bsize - amp->dbsize, (int)n, uio);
    342 		brelse(bp);
    343 	} while (error == 0 && uio->uio_resid > 0 && n != 0);
    344 
    345 out:
    346 reterr:
    347 #ifdef ADOSFS_DIAGNOSTIC
    348 	printf(" %d)", error);
    349 #endif
    350 	return(error);
    351 }
    352 
    353 int
    354 adosfs_write(v)
    355 	void *v;
    356 {
    357 #ifdef ADOSFS_DIAGNOSTIC
    358 	struct vop_write_args /* {
    359 		struct vnode *a_vp;
    360 		struct uio *a_uio;
    361 		int a_ioflag;
    362 		struct ucred *a_cred;
    363 	} */ *sp = v;
    364 	advopprint(sp);
    365 	printf(" EOPNOTSUPP)");
    366 #endif
    367 	return(EOPNOTSUPP);
    368 }
    369 
    370 /*
    371  * Just call the device strategy routine
    372  */
    373 int
    374 adosfs_strategy(v)
    375 	void *v;
    376 {
    377 	struct vop_strategy_args /* {
    378 		struct vnode *a_vp;
    379 		struct buf *a_bp;
    380 	} */ *sp = v;
    381 	struct buf *bp;
    382 	struct anode *ap;
    383 	struct vnode *vp;
    384 	int error;
    385 
    386 #ifdef ADOSFS_DIAGNOSTIC
    387 	advopprint(sp);
    388 #endif
    389 	bp = sp->a_bp;
    390 	if (bp->b_vp == NULL) {
    391 		bp->b_flags |= B_ERROR;
    392 		biodone(bp);
    393 		error = EIO;
    394 		goto reterr;
    395 	}
    396 	vp = sp->a_vp;
    397 	ap = VTOA(vp);
    398 	if (bp->b_blkno == bp->b_lblkno) {
    399 		error = VOP_BMAP(vp, bp->b_lblkno, NULL, &bp->b_blkno, NULL);
    400 		if (error) {
    401 			bp->b_flags |= B_ERROR;
    402 			biodone(bp);
    403 			goto reterr;
    404 		}
    405 	}
    406 	if ((long)bp->b_blkno == -1) {
    407 		biodone(bp);
    408 		error = 0;
    409 		goto reterr;
    410 	}
    411 	vp = ap->amp->devvp;
    412 	error = VOP_STRATEGY(vp, bp);
    413 reterr:
    414 #ifdef ADOSFS_DIAGNOSTIC
    415 	printf(" %d)", error);
    416 #endif
    417 	return(error);
    418 }
    419 
    420 int
    421 adosfs_link(v)
    422 	void *v;
    423 {
    424 	struct vop_link_args /* {
    425 		struct vnode *a_dvp;
    426 		struct vnode *a_vp;
    427 		struct componentname *a_cnp;
    428 	} */ *ap = v;
    429 
    430 	VOP_ABORTOP(ap->a_dvp, ap->a_cnp);
    431 	vput(ap->a_dvp);
    432 	return (EROFS);
    433 }
    434 
    435 int
    436 adosfs_symlink(v)
    437 	void *v;
    438 {
    439 	struct vop_symlink_args /* {
    440 		struct vnode *a_dvp;
    441 		struct vnode **a_vpp;
    442 		struct componentname *a_cnp;
    443 		struct vattr *a_vap;
    444 		char *a_target;
    445 	} */ *ap = v;
    446 
    447 	VOP_ABORTOP(ap->a_dvp, ap->a_cnp);
    448 	vput(ap->a_dvp);
    449 	return (EROFS);
    450 }
    451 
    452 /*
    453  * Wait until the vnode has finished changing state.
    454  */
    455 int
    456 adosfs_bmap(v)
    457 	void *v;
    458 {
    459 	struct vop_bmap_args /* {
    460 		struct vnode *a_vp;
    461 		daddr_t  a_bn;
    462 		struct vnode **a_vpp;
    463 		daddr_t *a_bnp;
    464 		int *a_runp;
    465 	} */ *sp = v;
    466 	struct anode *ap;
    467 	struct buf *flbp;
    468 	long nb, flblk, flblkoff, fcnt;
    469 	daddr_t *bnp;
    470 	daddr_t bn;
    471 	int error;
    472 
    473 #ifdef ADOSFS_DIAGNOSTIC
    474 	advopprint(sp);
    475 #endif
    476 	ap = VTOA(sp->a_vp);
    477 	bn = sp->a_bn;
    478 	bnp = sp->a_bnp;
    479 	if (sp->a_runp) {
    480 		*sp->a_runp = 0;
    481 	}
    482 	error = 0;
    483 
    484 	if (sp->a_vpp != NULL)
    485 		*sp->a_vpp = ap->amp->devvp;
    486 	if (bnp == NULL)
    487 		goto reterr;
    488 	if (bn < 0) {
    489 		error = EFBIG;
    490 		goto reterr;
    491 	}
    492 	if (sp->a_vp->v_type != VREG) {
    493 		error = EINVAL;
    494 		goto reterr;
    495 	}
    496 
    497 	/*
    498 	 * walk the chain of file list blocks until we find
    499 	 * the one that will yield the block pointer we need.
    500 	 */
    501 	if (ap->type == AFILE)
    502 		nb = ap->block;			/* pointer to ourself */
    503 	else if (ap->type == ALFILE)
    504 		nb = ap->linkto;		/* pointer to real file */
    505 	else {
    506 		error = EINVAL;
    507 		goto reterr;
    508 	}
    509 
    510 	flblk = bn / ANODENDATBLKENT(ap);
    511 	flbp = NULL;
    512 
    513 	/*
    514 	 * check last indirect block cache
    515 	 */
    516 	if (flblk < ap->lastlindblk)
    517 		fcnt = 0;
    518 	else {
    519 		flblk -= ap->lastlindblk;
    520 		fcnt = ap->lastlindblk;
    521 		nb = ap->lastindblk;
    522 	}
    523 	while (flblk >= 0) {
    524 		if (flbp)
    525 			brelse(flbp);
    526 		if (nb == 0) {
    527 #ifdef DIAGNOSTIC
    528 			printf("adosfs: bad file list chain.\n");
    529 #endif
    530 			error = EINVAL;
    531 			goto reterr;
    532 		}
    533 		error = bread(ap->amp->devvp, nb * ap->amp->bsize / DEV_BSIZE,
    534 			      ap->amp->bsize, NOCRED, &flbp);
    535 		if (error) {
    536 			brelse(flbp);
    537 			goto reterr;
    538 		}
    539 		if (adoscksum(flbp, ap->nwords)) {
    540 #ifdef DIAGNOSTIC
    541 			printf("adosfs: blk %ld failed cksum.\n", nb);
    542 #endif
    543 			brelse(flbp);
    544 			error = EINVAL;
    545 			goto reterr;
    546 		}
    547 		/*
    548 		 * update last indirect block cache
    549 		 */
    550 		ap->lastlindblk = fcnt++;
    551 		ap->lastindblk = nb;
    552 
    553 		nb = adoswordn(flbp, ap->nwords - 2);
    554 		flblk--;
    555 	}
    556 	/*
    557 	 * calculate offset of block number in table.  The table starts
    558 	 * at nwords - 51 and goes to offset 6 or less if indicated by the
    559 	 * valid table entries stored at offset ADBI_NBLKTABENT.
    560 	 */
    561 	flblkoff = bn % ANODENDATBLKENT(ap);
    562 	if (flblkoff < adoswordn(flbp, 2 /* ADBI_NBLKTABENT */)) {
    563 		flblkoff = (ap->nwords - 51) - flblkoff;
    564 		*bnp = adoswordn(flbp, flblkoff) * ap->amp->bsize / DEV_BSIZE;
    565 	} else {
    566 #ifdef DIAGNOSTIC
    567 		printf("flblk offset %ld too large in lblk %ld blk %" PRId64 "\n",
    568 		    flblkoff, (long)bn, flbp->b_blkno);
    569 #endif
    570 		error = EINVAL;
    571 	}
    572 	brelse(flbp);
    573 reterr:
    574 #ifdef ADOSFS_DIAGNOSTIC
    575 	if (error == 0 && bnp)
    576 		printf(" %lld => %lld", (long long)bn, (long long)*bnp);
    577 	printf(" %d)\n", error);
    578 #endif
    579 	return(error);
    580 }
    581 
    582 /*
    583  * Print out the contents of a adosfs vnode.
    584  */
    585 /* ARGSUSED */
    586 int
    587 adosfs_print(v)
    588 	void *v;
    589 {
    590 #if 0
    591 	struct vop_print_args /* {
    592 		struct vnode *a_vp;
    593 	} */ *sp = v;
    594 #endif
    595 	return(0);
    596 }
    597 
    598 struct adirent {
    599 	u_long  fileno;
    600 	u_short reclen;
    601 	char    type;
    602 	char    namlen;
    603 	char    name[ADMAXNAMELEN+2];	/* maxlen plus 2 NUL's */
    604 };
    605 
    606 int
    607 adosfs_readdir(v)
    608 	void *v;
    609 {
    610 	struct vop_readdir_args /* {
    611 		struct vnode *a_vp;
    612 		struct uio *a_uio;
    613 		struct ucred *a_cred;
    614 		int *a_eofflag;
    615 		off_t **a_cookies;
    616 		int *a_ncookies;
    617 	} */ *sp = v;
    618 	int error, useri, chainc, hashi, scanned, uavail;
    619 	struct adirent ad, *adp;
    620 	struct anode *pap, *ap;
    621 	struct adosfsmount *amp;
    622 	struct vnode *vp;
    623 	struct uio *uio;
    624 	u_long nextbn;
    625 	off_t uoff, *cookies = NULL;
    626 	int ncookies = 0;
    627 
    628 #ifdef ADOSFS_DIAGNOSTIC
    629 	advopprint(sp);
    630 #endif
    631 	if (sp->a_vp->v_type != VDIR) {
    632 		error = ENOTDIR;
    633 		goto reterr;
    634 	}
    635 
    636 	uio = sp->a_uio;
    637 	uoff = uio->uio_offset;
    638 	if (uoff < 0) {
    639 		error = EINVAL;
    640 		goto reterr;
    641 	}
    642 
    643 	pap = VTOA(sp->a_vp);
    644 	amp = pap->amp;
    645 	adp = &ad;
    646 	error = nextbn = hashi = chainc = scanned = 0;
    647 	uavail = uio->uio_resid / sizeof(ad);
    648 	useri = uoff / sizeof(ad);
    649 
    650 	/*
    651 	 * if no slots available or offset requested is not on a slot boundry
    652 	 */
    653 	if (uavail < 1 || uoff % sizeof(ad)) {
    654 		error = EINVAL;
    655 		goto reterr;
    656 	}
    657 
    658 	if (sp->a_ncookies) {
    659 		ncookies = 0;
    660 		cookies = malloc(sizeof (off_t) * uavail, M_TEMP, M_WAITOK);
    661 		*sp->a_cookies = cookies;
    662 	}
    663 
    664 	while (uavail) {
    665 		if (hashi == pap->ntabent) {
    666 			*sp->a_eofflag = 1;
    667 			break;
    668 		}
    669 		if (pap->tab[hashi] == 0) {
    670 			hashi++;
    671 			continue;
    672 		}
    673 		if (nextbn == 0)
    674 			nextbn = pap->tab[hashi];
    675 
    676 		/*
    677 		 * first determine if we can skip this chain
    678 		 */
    679 		if (chainc == 0) {
    680 			int skip;
    681 
    682 			skip = useri - scanned;
    683 			if (pap->tabi[hashi] > 0 && pap->tabi[hashi] <= skip) {
    684 				scanned += pap->tabi[hashi];
    685 				hashi++;
    686 				nextbn = 0;
    687 				continue;
    688 			}
    689 		}
    690 
    691 		/*
    692 		 * now [continue to] walk the chain
    693 		 */
    694 		ap = NULL;
    695 		do {
    696 			error = VFS_VGET(amp->mp, (ino_t)nextbn, &vp);
    697 			if (error)
    698 				goto reterr;
    699 			ap = VTOA(vp);
    700 			scanned++;
    701 			chainc++;
    702 			nextbn = ap->hashf;
    703 
    704 			/*
    705 			 * check for end of chain.
    706 			 */
    707 			if (nextbn == 0) {
    708 				pap->tabi[hashi] = chainc;
    709 				hashi++;
    710 				chainc = 0;
    711 			} else if (pap->tabi[hashi] <= 0 &&
    712 			    -chainc < pap->tabi[hashi])
    713 				pap->tabi[hashi] = -chainc;
    714 
    715 			if (useri >= scanned) {
    716 				vput(vp);
    717 				ap = NULL;
    718 			}
    719 		} while (ap == NULL && nextbn != 0);
    720 
    721 		/*
    722 		 * we left the loop but without a result so do main over.
    723 		 */
    724 		if (ap == NULL)
    725 			continue;
    726 		/*
    727 		 * Fill in dirent record
    728 		 */
    729 		memset(adp, 0, sizeof(struct adirent));
    730 		adp->fileno = ap->block;
    731 		/*
    732 		 * this deserves an function in kern/vfs_subr.c
    733 		 */
    734 		switch (ATOV(ap)->v_type) {
    735 		case VREG:
    736 			adp->type = DT_REG;
    737 			break;
    738 		case VDIR:
    739 			adp->type = DT_DIR;
    740 			break;
    741 		case VLNK:
    742 			adp->type = DT_LNK;
    743 			break;
    744 		default:
    745 			adp->type = DT_UNKNOWN;
    746 			break;
    747 		}
    748 		adp->reclen = sizeof(struct adirent);
    749 		adp->namlen = strlen(ap->name);
    750 		memcpy(adp->name, ap->name, adp->namlen);
    751 		vput(vp);
    752 
    753 		error = uiomove(adp, sizeof(struct adirent), uio);
    754 		if (error)
    755 			break;
    756 		if (sp->a_ncookies) {
    757 			*cookies++ = uoff;
    758 			ncookies++;
    759 		}
    760 		uoff += sizeof(struct adirent);
    761 		useri++;
    762 		uavail--;
    763 	}
    764 #if doesnt_uiomove_handle_this
    765 	uio->uio_offset = uoff;
    766 #endif
    767 reterr:
    768 #ifdef ADOSFS_DIAGNOSTIC
    769 	printf(" %d)", error);
    770 #endif
    771 	if (sp->a_ncookies) {
    772 		if (error) {
    773 			free(*sp->a_cookies, M_TEMP);
    774 			*sp->a_ncookies = 0;
    775 			*sp->a_cookies = NULL;
    776 		} else
    777 			*sp->a_ncookies = ncookies;
    778 	}
    779 	return(error);
    780 }
    781 
    782 
    783 int
    784 adosfs_access(v)
    785 	void *v;
    786 {
    787 	struct vop_access_args /* {
    788 		struct vnode *a_vp;
    789 		int  a_mode;
    790 		struct ucred *a_cred;
    791 		struct proc *a_p;
    792 	} */ *sp = v;
    793 	struct anode *ap;
    794 	struct vnode *vp = sp->a_vp;
    795 	int error;
    796 
    797 #ifdef ADOSFS_DIAGNOSTIC
    798 	advopprint(sp);
    799 #endif
    800 
    801 	ap = VTOA(vp);
    802 #ifdef DIAGNOSTIC
    803 	if (!VOP_ISLOCKED(vp)) {
    804 		vprint("adosfs_access: not locked", sp->a_vp);
    805 		panic("adosfs_access: not locked");
    806 	}
    807 #endif
    808 	/*
    809 	 * Disallow write attempts unless the file is a socket,
    810 	 * fifo, or a block or character device resident on the
    811 	 * file system.
    812 	 */
    813 	if (sp->a_mode & VWRITE) {
    814 		switch (vp->v_type) {
    815 		case VDIR:
    816 		case VLNK:
    817 		case VREG:
    818 			return (EROFS);
    819 		default:
    820 			break;
    821 		}
    822 	}
    823 #ifdef QUOTA
    824 #endif
    825 	error = vaccess(sp->a_vp->v_type, adunixprot(ap->adprot) & ap->amp->mask,
    826 	    ap->uid, ap->gid, sp->a_mode, sp->a_cred);
    827 #ifdef ADOSFS_DIAGNOSTIC
    828 	printf(" %d)", error);
    829 #endif
    830 	return(error);
    831 }
    832 
    833 int
    834 adosfs_readlink(v)
    835 	void *v;
    836 {
    837 	struct vop_readlink_args /* {
    838 		struct vnode *a_vp;
    839 		struct uio *a_uio;
    840 		struct ucred *a_cred;
    841 	} */ *sp = v;
    842 	struct anode *ap;
    843 	int error;
    844 
    845 #ifdef ADOSFS_DIAGNOSTIC
    846 	advopprint(sp);
    847 #endif
    848 	ap = VTOA(sp->a_vp);
    849 	error = uiomove(ap->slinkto, strlen(ap->slinkto), sp->a_uio);
    850 #ifdef ADOSFS_DIAGNOSTIC
    851 	printf(" %d)", error);
    852 #endif
    853 	return (error);
    854 }
    855 
    856 /*ARGSUSED*/
    857 int
    858 adosfs_inactive(v)
    859 	void *v;
    860 {
    861 	struct vop_inactive_args /* {
    862 		struct vnode *a_vp;
    863 		struct proc *a_p;
    864 	} */ *sp = v;
    865 	struct vnode *vp = sp->a_vp;
    866 	struct proc *p = sp->a_p;
    867 #ifdef ADOSFS_DIAGNOSTIC
    868 	advopprint(sp);
    869 #endif
    870 	VOP_UNLOCK(vp, 0);
    871 	/* XXX this needs to check if file was deleted */
    872 	vrecycle(vp, NULL, p);
    873 
    874 #ifdef ADOSFS_DIAGNOSTIC
    875 	printf(" 0)");
    876 #endif
    877 	return(0);
    878 }
    879 
    880 /*
    881  * the kernel wants its vnode back.
    882  * no lock needed we are being called from vclean()
    883  */
    884 int
    885 adosfs_reclaim(v)
    886 	void *v;
    887 {
    888 	struct vop_reclaim_args /* {
    889 		struct vnode *a_vp;
    890 	} */ *sp = v;
    891 	struct vnode *vp;
    892 	struct anode *ap;
    893 
    894 #ifdef ADOSFS_DIAGNOSTIC
    895 	printf("(reclaim 0)");
    896 #endif
    897 	vp = sp->a_vp;
    898 	ap = VTOA(vp);
    899 	LIST_REMOVE(ap, link);
    900 	cache_purge(vp);
    901 	if (vp->v_type == VDIR && ap->tab)
    902 		free(ap->tab, M_ANODE);
    903 	else if (vp->v_type == VLNK && ap->slinkto)
    904 		free(ap->slinkto, M_ANODE);
    905 	pool_put(&adosfs_node_pool, ap);
    906 	vp->v_data = NULL;
    907 	return(0);
    908 }
    909 
    910 /*
    911  * POSIX pathconf info, grabbed from kern/u fs, probably need to
    912  * investigate exactly what each return type means as they are probably
    913  * not valid currently
    914  */
    915 int
    916 adosfs_pathconf(v)
    917 	void *v;
    918 {
    919 	struct vop_pathconf_args /* {
    920 		struct vnode *a_vp;
    921 		int a_name;
    922 		register_t *a_retval;
    923 	} */ *ap = v;
    924 
    925 	switch (ap->a_name) {
    926 	case _PC_LINK_MAX:
    927 		*ap->a_retval = LINK_MAX;
    928 		return (0);
    929 	case _PC_NAME_MAX:
    930 		*ap->a_retval = ap->a_vp->v_mount->mnt_stat.f_namemax;
    931 		return (0);
    932 	case _PC_PATH_MAX:
    933 		*ap->a_retval = PATH_MAX;
    934 		return (0);
    935 	case _PC_PIPE_BUF:
    936 		*ap->a_retval = PIPE_BUF;
    937 		return (0);
    938 	case _PC_CHOWN_RESTRICTED:
    939 		*ap->a_retval = 1;
    940 		return (0);
    941 	case _PC_VDISABLE:
    942 		*ap->a_retval = _POSIX_VDISABLE;
    943 		return (0);
    944 	case _PC_SYNC_IO:
    945 		*ap->a_retval = 1;
    946 		return (0);
    947 	case _PC_FILESIZEBITS:
    948 		*ap->a_retval = 32;
    949 		return (0);
    950 	default:
    951 		return (EINVAL);
    952 	}
    953 	/* NOTREACHED */
    954 }
    955