Home | History | Annotate | Line # | Download | only in ntfs
ntfs_vnops.c revision 1.2
      1 /*	$NetBSD: ntfs_vnops.c,v 1.2 2003/04/09 16:02:18 jdolecek Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1992, 1993
      5  *	The Regents of the University of California.  All rights reserved.
      6  *
      7  * This code is derived from software contributed to Berkeley by
      8  * John Heidemann of the UCLA Ficus project.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  * 3. All advertising materials mentioning features or use of this software
     19  *    must display the following acknowledgement:
     20  *	This product includes software developed by the University of
     21  *	California, Berkeley and its contributors.
     22  * 4. Neither the name of the University nor the names of its contributors
     23  *    may be used to endorse or promote products derived from this software
     24  *    without specific prior written permission.
     25  *
     26  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     27  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     28  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     29  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     30  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     31  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     32  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     33  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     34  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     35  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     36  * SUCH DAMAGE.
     37  *
     38  *	Id: ntfs_vnops.c,v 1.5 1999/05/12 09:43:06 semenu Exp
     39  *
     40  */
     41 
     42 #include <sys/cdefs.h>
     43 __KERNEL_RCSID(0, "$NetBSD: ntfs_vnops.c,v 1.2 2003/04/09 16:02:18 jdolecek Exp $");
     44 
     45 #include <sys/param.h>
     46 #include <sys/systm.h>
     47 #include <sys/kernel.h>
     48 #include <sys/time.h>
     49 #include <sys/stat.h>
     50 #include <sys/vnode.h>
     51 #include <sys/mount.h>
     52 #include <sys/namei.h>
     53 #include <sys/malloc.h>
     54 #include <sys/buf.h>
     55 #include <sys/dirent.h>
     56 
     57 #if !defined(__NetBSD__)
     58 #include <vm/vm.h>
     59 #endif
     60 
     61 #if defined(__FreeBSD__)
     62 #include <vm/vnode_pager.h>
     63 #endif
     64 
     65 #include <sys/sysctl.h>
     66 
     67 
     68 /*#define NTFS_DEBUG 1*/
     69 #include <fs/ntfs/ntfs.h>
     70 #include <fs/ntfs/ntfs_inode.h>
     71 #include <fs/ntfs/ntfs_subr.h>
     72 #include <miscfs/specfs/specdev.h>
     73 #include <miscfs/genfs/genfs.h>
     74 
     75 #include <sys/unistd.h> /* for pathconf(2) constants */
     76 
     77 static int	ntfs_bypass __P((struct vop_generic_args *ap));
     78 static int	ntfs_read __P((struct vop_read_args *));
     79 static int	ntfs_write __P((struct vop_write_args *ap));
     80 static int	ntfs_getattr __P((struct vop_getattr_args *ap));
     81 static int	ntfs_inactive __P((struct vop_inactive_args *ap));
     82 static int	ntfs_print __P((struct vop_print_args *ap));
     83 static int	ntfs_reclaim __P((struct vop_reclaim_args *ap));
     84 static int	ntfs_strategy __P((struct vop_strategy_args *ap));
     85 static int	ntfs_access __P((struct vop_access_args *ap));
     86 static int	ntfs_open __P((struct vop_open_args *ap));
     87 static int	ntfs_close __P((struct vop_close_args *ap));
     88 static int	ntfs_readdir __P((struct vop_readdir_args *ap));
     89 static int	ntfs_lookup __P((struct vop_lookup_args *ap));
     90 static int	ntfs_bmap __P((struct vop_bmap_args *ap));
     91 #if defined(__FreeBSD__)
     92 static int	ntfs_getpages __P((struct vop_getpages_args *ap));
     93 static int	ntfs_putpages __P((struct vop_putpages_args *));
     94 static int	ntfs_fsync __P((struct vop_fsync_args *ap));
     95 #endif
     96 static int	ntfs_pathconf __P((void *));
     97 static int	ntfs_remove(void *);
     98 
     99 int	ntfs_prtactive = 1;	/* 1 => print out reclaim of active vnodes */
    100 
    101 #if defined(__FreeBSD__)
    102 int
    103 ntfs_getpages(ap)
    104 	struct vop_getpages_args *ap;
    105 {
    106 	return vnode_pager_generic_getpages(ap->a_vp, ap->a_m, ap->a_count,
    107 		ap->a_reqpage);
    108 }
    109 
    110 int
    111 ntfs_putpages(ap)
    112 	struct vop_putpages_args *ap;
    113 {
    114 	return vnode_pager_generic_putpages(ap->a_vp, ap->a_m, ap->a_count,
    115 		ap->a_sync, ap->a_rtvals);
    116 }
    117 #endif
    118 
    119 /*
    120  * This is a noop, simply returning what one has been given.
    121  */
    122 int
    123 ntfs_bmap(ap)
    124 	struct vop_bmap_args /* {
    125 		struct vnode *a_vp;
    126 		daddr_t  a_bn;
    127 		struct vnode **a_vpp;
    128 		daddr_t *a_bnp;
    129 		int *a_runp;
    130 		int *a_runb;
    131 	} */ *ap;
    132 {
    133 	dprintf(("ntfs_bmap: vn: %p, blk: %d\n", ap->a_vp,(u_int32_t)ap->a_bn));
    134 	if (ap->a_vpp != NULL)
    135 		*ap->a_vpp = ap->a_vp;
    136 	if (ap->a_bnp != NULL)
    137 		*ap->a_bnp = ap->a_bn;
    138 	if (ap->a_runp != NULL)
    139 		*ap->a_runp = 0;
    140 #if !defined(__NetBSD__)
    141 	if (ap->a_runb != NULL)
    142 		*ap->a_runb = 0;
    143 #endif
    144 	return (0);
    145 }
    146 
    147 static int
    148 ntfs_read(ap)
    149 	struct vop_read_args /* {
    150 		struct vnode *a_vp;
    151 		struct uio *a_uio;
    152 		int a_ioflag;
    153 		struct ucred *a_cred;
    154 	} */ *ap;
    155 {
    156 	struct vnode *vp = ap->a_vp;
    157 	struct fnode *fp = VTOF(vp);
    158 	struct ntnode *ip = FTONT(fp);
    159 	struct uio *uio = ap->a_uio;
    160 	struct ntfsmount *ntmp = ip->i_mp;
    161 	u_int64_t toread;
    162 	int error;
    163 
    164 	dprintf(("ntfs_read: ino: %d, off: %d resid: %d, segflg: %d\n",ip->i_number,(u_int32_t)uio->uio_offset,uio->uio_resid,uio->uio_segflg));
    165 
    166 	dprintf(("ntfs_read: filesize: %d",(u_int32_t)fp->f_size));
    167 
    168 	/* don't allow reading after end of file */
    169 	if (uio->uio_offset > fp->f_size)
    170 		toread = 0;
    171 	else
    172 		toread = min( uio->uio_resid, fp->f_size - uio->uio_offset );
    173 
    174 	dprintf((", toread: %d\n",(u_int32_t)toread));
    175 
    176 	if (toread == 0)
    177 		return (0);
    178 
    179 	error = ntfs_readattr(ntmp, ip, fp->f_attrtype,
    180 		fp->f_attrname, uio->uio_offset, toread, NULL, uio);
    181 	if (error) {
    182 		printf("ntfs_read: ntfs_readattr failed: %d\n",error);
    183 		return (error);
    184 	}
    185 
    186 	return (0);
    187 }
    188 
    189 static int
    190 ntfs_bypass(ap)
    191 	struct vop_generic_args /* {
    192 		struct vnodeop_desc *a_desc;
    193 		<other random data follows, presumably>
    194 	} */ *ap;
    195 {
    196 	int error = ENOTTY;
    197 	dprintf(("ntfs_bypass: %s\n", ap->a_desc->vdesc_name));
    198 	return (error);
    199 }
    200 
    201 
    202 static int
    203 ntfs_getattr(ap)
    204 	struct vop_getattr_args /* {
    205 		struct vnode *a_vp;
    206 		struct vattr *a_vap;
    207 		struct ucred *a_cred;
    208 		struct proc *a_p;
    209 	} */ *ap;
    210 {
    211 	struct vnode *vp = ap->a_vp;
    212 	struct fnode *fp = VTOF(vp);
    213 	struct ntnode *ip = FTONT(fp);
    214 	struct vattr *vap = ap->a_vap;
    215 
    216 	dprintf(("ntfs_getattr: %d, flags: %d\n",ip->i_number,ip->i_flag));
    217 
    218 #if defined(__FreeBSD__)
    219 	vap->va_fsid = dev2udev(ip->i_dev);
    220 #else /* NetBSD */
    221 	vap->va_fsid = ip->i_dev;
    222 #endif
    223 	vap->va_fileid = ip->i_number;
    224 	vap->va_mode = ip->i_mp->ntm_mode;
    225 	vap->va_nlink = ip->i_nlink;
    226 	vap->va_uid = ip->i_mp->ntm_uid;
    227 	vap->va_gid = ip->i_mp->ntm_gid;
    228 	vap->va_rdev = 0;				/* XXX UNODEV ? */
    229 	vap->va_size = fp->f_size;
    230 	vap->va_bytes = fp->f_allocated;
    231 	vap->va_atime = ntfs_nttimetounix(fp->f_times.t_access);
    232 	vap->va_mtime = ntfs_nttimetounix(fp->f_times.t_write);
    233 	vap->va_ctime = ntfs_nttimetounix(fp->f_times.t_create);
    234 	vap->va_flags = ip->i_flag;
    235 	vap->va_gen = 0;
    236 	vap->va_blocksize = ip->i_mp->ntm_spc * ip->i_mp->ntm_bps;
    237 	vap->va_type = vp->v_type;
    238 	vap->va_filerev = 0;
    239 	return (0);
    240 }
    241 
    242 
    243 /*
    244  * Last reference to an ntnode.  If necessary, write or delete it.
    245  */
    246 int
    247 ntfs_inactive(ap)
    248 	struct vop_inactive_args /* {
    249 		struct vnode *a_vp;
    250 	} */ *ap;
    251 {
    252 	struct vnode *vp = ap->a_vp;
    253 #ifdef NTFS_DEBUG
    254 	struct ntnode *ip = VTONT(vp);
    255 #endif
    256 
    257 	dprintf(("ntfs_inactive: vnode: %p, ntnode: %d\n", vp, ip->i_number));
    258 
    259 	if (ntfs_prtactive && vp->v_usecount != 0)
    260 		vprint("ntfs_inactive: pushing active", vp);
    261 
    262 	VOP__UNLOCK(vp, 0, ap->a_p);
    263 
    264 	/* XXX since we don't support any filesystem changes
    265 	 * right now, nothing more needs to be done
    266 	 */
    267 	return (0);
    268 }
    269 
    270 /*
    271  * Reclaim an fnode/ntnode so that it can be used for other purposes.
    272  */
    273 int
    274 ntfs_reclaim(ap)
    275 	struct vop_reclaim_args /* {
    276 		struct vnode *a_vp;
    277 	} */ *ap;
    278 {
    279 	struct vnode *vp = ap->a_vp;
    280 	struct fnode *fp = VTOF(vp);
    281 	struct ntnode *ip = FTONT(fp);
    282 	int error;
    283 
    284 	dprintf(("ntfs_reclaim: vnode: %p, ntnode: %d\n", vp, ip->i_number));
    285 
    286 	if (ntfs_prtactive && vp->v_usecount != 0)
    287 		vprint("ntfs_reclaim: pushing active", vp);
    288 
    289 	if ((error = ntfs_ntget(ip)) != 0)
    290 		return (error);
    291 
    292 	/* Purge old data structures associated with the inode. */
    293 	cache_purge(vp);
    294 	if (ip->i_devvp) {
    295 		vrele(ip->i_devvp);
    296 		ip->i_devvp = NULL;
    297 	}
    298 
    299 	ntfs_frele(fp);
    300 	ntfs_ntput(ip);
    301 	vp->v_data = NULL;
    302 
    303 	return (0);
    304 }
    305 
    306 static int
    307 ntfs_print(ap)
    308 	struct vop_print_args /* {
    309 		struct vnode *a_vp;
    310 	} */ *ap;
    311 {
    312 	return (0);
    313 }
    314 
    315 /*
    316  * Calculate the logical to physical mapping if not done already,
    317  * then call the device strategy routine.
    318  */
    319 int
    320 ntfs_strategy(ap)
    321 	struct vop_strategy_args /* {
    322 		struct buf *a_bp;
    323 	} */ *ap;
    324 {
    325 	struct buf *bp = ap->a_bp;
    326 	struct vnode *vp = bp->b_vp;
    327 	struct fnode *fp = VTOF(vp);
    328 	struct ntnode *ip = FTONT(fp);
    329 	struct ntfsmount *ntmp = ip->i_mp;
    330 	int error;
    331 
    332 #ifdef __FreeBSD__
    333 	dprintf(("ntfs_strategy: offset: %d, blkno: %d, lblkno: %d\n",
    334 		(u_int32_t)bp->b_offset,(u_int32_t)bp->b_blkno,
    335 		(u_int32_t)bp->b_lblkno));
    336 #else
    337 	dprintf(("ntfs_strategy: blkno: %d, lblkno: %d\n",
    338 		(u_int32_t)bp->b_blkno,
    339 		(u_int32_t)bp->b_lblkno));
    340 #endif
    341 
    342 	dprintf(("strategy: bcount: %d flags: 0x%lx\n",
    343 		(u_int32_t)bp->b_bcount,bp->b_flags));
    344 
    345 	if (bp->b_flags & B_READ) {
    346 		u_int32_t toread;
    347 
    348 		if (ntfs_cntob(bp->b_blkno) >= fp->f_size) {
    349 			clrbuf(bp);
    350 			error = 0;
    351 		} else {
    352 			toread = min(bp->b_bcount,
    353 				 fp->f_size-ntfs_cntob(bp->b_blkno));
    354 			dprintf(("ntfs_strategy: toread: %d, fsize: %d\n",
    355 				toread,(u_int32_t)fp->f_size));
    356 
    357 			error = ntfs_readattr(ntmp, ip, fp->f_attrtype,
    358 				fp->f_attrname, ntfs_cntob(bp->b_blkno),
    359 				toread, bp->b_data, NULL);
    360 
    361 			if (error) {
    362 				printf("ntfs_strategy: ntfs_readattr failed\n");
    363 				bp->b_error = error;
    364 				bp->b_flags |= B_ERROR;
    365 			}
    366 
    367 			bzero(bp->b_data + toread, bp->b_bcount - toread);
    368 		}
    369 	} else {
    370 		size_t tmp;
    371 		u_int32_t towrite;
    372 
    373 		if (ntfs_cntob(bp->b_blkno) + bp->b_bcount >= fp->f_size) {
    374 			printf("ntfs_strategy: CAN'T EXTEND FILE\n");
    375 			bp->b_error = error = EFBIG;
    376 			bp->b_flags |= B_ERROR;
    377 		} else {
    378 			towrite = min(bp->b_bcount,
    379 				fp->f_size-ntfs_cntob(bp->b_blkno));
    380 			dprintf(("ntfs_strategy: towrite: %d, fsize: %d\n",
    381 				towrite,(u_int32_t)fp->f_size));
    382 
    383 			error = ntfs_writeattr_plain(ntmp, ip, fp->f_attrtype,
    384 				fp->f_attrname, ntfs_cntob(bp->b_blkno),towrite,
    385 				bp->b_data, &tmp, NULL);
    386 
    387 			if (error) {
    388 				printf("ntfs_strategy: ntfs_writeattr fail\n");
    389 				bp->b_error = error;
    390 				bp->b_flags |= B_ERROR;
    391 			}
    392 		}
    393 	}
    394 	biodone(bp);
    395 	return (error);
    396 }
    397 
    398 static int
    399 ntfs_write(ap)
    400 	struct vop_write_args /* {
    401 		struct vnode *a_vp;
    402 		struct uio *a_uio;
    403 		int  a_ioflag;
    404 		struct ucred *a_cred;
    405 	} */ *ap;
    406 {
    407 	struct vnode *vp = ap->a_vp;
    408 	struct fnode *fp = VTOF(vp);
    409 	struct ntnode *ip = FTONT(fp);
    410 	struct uio *uio = ap->a_uio;
    411 	struct ntfsmount *ntmp = ip->i_mp;
    412 	u_int64_t towrite;
    413 	size_t written;
    414 	int error;
    415 
    416 	dprintf(("ntfs_write: ino: %d, off: %d resid: %d, segflg: %d\n",ip->i_number,(u_int32_t)uio->uio_offset,uio->uio_resid,uio->uio_segflg));
    417 	dprintf(("ntfs_write: filesize: %d",(u_int32_t)fp->f_size));
    418 
    419 	if (uio->uio_resid + uio->uio_offset > fp->f_size) {
    420 		printf("ntfs_write: CAN'T WRITE BEYOND END OF FILE\n");
    421 		return (EFBIG);
    422 	}
    423 
    424 	towrite = min(uio->uio_resid, fp->f_size - uio->uio_offset);
    425 
    426 	dprintf((", towrite: %d\n",(u_int32_t)towrite));
    427 
    428 	error = ntfs_writeattr_plain(ntmp, ip, fp->f_attrtype,
    429 		fp->f_attrname, uio->uio_offset, towrite, NULL, &written, uio);
    430 #ifdef NTFS_DEBUG
    431 	if (error)
    432 		printf("ntfs_write: ntfs_writeattr failed: %d\n", error);
    433 #endif
    434 
    435 	return (error);
    436 }
    437 
    438 int
    439 ntfs_remove(void *v)
    440 {
    441 	struct vop_remove_args /* {
    442 		struct vnode *a_dvp;
    443 		struct vnode *a_vp;
    444 		struct componentname *a_cnp;
    445 	} */ *ap = v;
    446 
    447 	if (ap->a_dvp == ap->a_vp)
    448 		vrele(ap->a_vp);
    449 	else
    450 		vput(ap->a_vp);
    451 	vput(ap->a_dvp);
    452 	return (EOPNOTSUPP);
    453 }
    454 
    455 int
    456 ntfs_access(ap)
    457 	struct vop_access_args /* {
    458 		struct vnode *a_vp;
    459 		int  a_mode;
    460 		struct ucred *a_cred;
    461 		struct proc *a_p;
    462 	} */ *ap;
    463 {
    464 	struct vnode *vp = ap->a_vp;
    465 	struct ntnode *ip = VTONT(vp);
    466 	struct ucred *cred = ap->a_cred;
    467 	mode_t mask, mode = ap->a_mode;
    468 	gid_t *gp;
    469 	int i;
    470 #ifdef QUOTA
    471 	int error;
    472 #endif
    473 
    474 	dprintf(("ntfs_access: %d\n",ip->i_number));
    475 
    476 	/*
    477 	 * Disallow write attempts on read-only file systems;
    478 	 * unless the file is a socket, fifo, or a block or
    479 	 * character device resident on the file system.
    480 	 */
    481 	if (mode & VWRITE) {
    482 		switch ((int)vp->v_type) {
    483 		case VDIR:
    484 		case VLNK:
    485 		case VREG:
    486 			if (vp->v_mount->mnt_flag & MNT_RDONLY)
    487 				return (EROFS);
    488 #ifdef QUOTA
    489 			if (error = getinoquota(ip))
    490 				return (error);
    491 #endif
    492 			break;
    493 		}
    494 	}
    495 
    496 	/* Otherwise, user id 0 always gets access. */
    497 	if (cred->cr_uid == 0)
    498 		return (0);
    499 
    500 	mask = 0;
    501 
    502 	/* Otherwise, check the owner. */
    503 	if (cred->cr_uid == ip->i_mp->ntm_uid) {
    504 		if (mode & VEXEC)
    505 			mask |= S_IXUSR;
    506 		if (mode & VREAD)
    507 			mask |= S_IRUSR;
    508 		if (mode & VWRITE)
    509 			mask |= S_IWUSR;
    510 		return ((ip->i_mp->ntm_mode & mask) == mask ? 0 : EACCES);
    511 	}
    512 
    513 	/* Otherwise, check the groups. */
    514 	for (i = 0, gp = cred->cr_groups; i < cred->cr_ngroups; i++, gp++)
    515 		if (ip->i_mp->ntm_gid == *gp) {
    516 			if (mode & VEXEC)
    517 				mask |= S_IXGRP;
    518 			if (mode & VREAD)
    519 				mask |= S_IRGRP;
    520 			if (mode & VWRITE)
    521 				mask |= S_IWGRP;
    522 			return ((ip->i_mp->ntm_mode&mask) == mask ? 0 : EACCES);
    523 		}
    524 
    525 	/* Otherwise, check everyone else. */
    526 	if (mode & VEXEC)
    527 		mask |= S_IXOTH;
    528 	if (mode & VREAD)
    529 		mask |= S_IROTH;
    530 	if (mode & VWRITE)
    531 		mask |= S_IWOTH;
    532 	return ((ip->i_mp->ntm_mode & mask) == mask ? 0 : EACCES);
    533 }
    534 
    535 /*
    536  * Open called.
    537  *
    538  * Nothing to do.
    539  */
    540 /* ARGSUSED */
    541 static int
    542 ntfs_open(ap)
    543 	struct vop_open_args /* {
    544 		struct vnode *a_vp;
    545 		int  a_mode;
    546 		struct ucred *a_cred;
    547 		struct proc *a_p;
    548 	} */ *ap;
    549 {
    550 #if NTFS_DEBUG
    551 	struct vnode *vp = ap->a_vp;
    552 	struct ntnode *ip = VTONT(vp);
    553 
    554 	printf("ntfs_open: %d\n",ip->i_number);
    555 #endif
    556 
    557 	/*
    558 	 * Files marked append-only must be opened for appending.
    559 	 */
    560 
    561 	return (0);
    562 }
    563 
    564 /*
    565  * Close called.
    566  *
    567  * Update the times on the inode.
    568  */
    569 /* ARGSUSED */
    570 static int
    571 ntfs_close(ap)
    572 	struct vop_close_args /* {
    573 		struct vnode *a_vp;
    574 		int  a_fflag;
    575 		struct ucred *a_cred;
    576 		struct proc *a_p;
    577 	} */ *ap;
    578 {
    579 #if NTFS_DEBUG
    580 	struct vnode *vp = ap->a_vp;
    581 	struct ntnode *ip = VTONT(vp);
    582 
    583 	printf("ntfs_close: %d\n",ip->i_number);
    584 #endif
    585 
    586 	return (0);
    587 }
    588 
    589 int
    590 ntfs_readdir(ap)
    591 	struct vop_readdir_args /* {
    592 		struct vnode *a_vp;
    593 		struct uio *a_uio;
    594 		struct ucred *a_cred;
    595 		int *a_ncookies;
    596 		u_int **cookies;
    597 	} */ *ap;
    598 {
    599 	struct vnode *vp = ap->a_vp;
    600 	struct fnode *fp = VTOF(vp);
    601 	struct ntnode *ip = FTONT(fp);
    602 	struct uio *uio = ap->a_uio;
    603 	struct ntfsmount *ntmp = ip->i_mp;
    604 	int i, error = 0;
    605 	u_int32_t faked = 0, num;
    606 	int ncookies = 0;
    607 	struct dirent *cde;
    608 	off_t off;
    609 
    610 	dprintf(("ntfs_readdir %d off: %d resid: %d\n",ip->i_number,(u_int32_t)uio->uio_offset,uio->uio_resid));
    611 
    612 	off = uio->uio_offset;
    613 
    614 	MALLOC(cde, struct dirent *, sizeof(struct dirent), M_TEMP, M_WAITOK);
    615 
    616 	/* Simulate . in every dir except ROOT */
    617 	if (ip->i_number != NTFS_ROOTINO
    618 	    && uio->uio_offset < sizeof(struct dirent)) {
    619 		cde->d_fileno = ip->i_number;
    620 		cde->d_reclen = sizeof(struct dirent);
    621 		cde->d_type = DT_DIR;
    622 		cde->d_namlen = 1;
    623 		strncpy(cde->d_name, ".", 2);
    624 		error = uiomove((void *)cde, sizeof(struct dirent), uio);
    625 		if (error)
    626 			goto out;
    627 
    628 		ncookies++;
    629 	}
    630 
    631 	/* Simulate .. in every dir including ROOT */
    632 	if (uio->uio_offset < 2 * sizeof(struct dirent)) {
    633 		cde->d_fileno = NTFS_ROOTINO;	/* XXX */
    634 		cde->d_reclen = sizeof(struct dirent);
    635 		cde->d_type = DT_DIR;
    636 		cde->d_namlen = 2;
    637 		strncpy(cde->d_name, "..", 3);
    638 
    639 		error = uiomove((void *) cde, sizeof(struct dirent), uio);
    640 		if (error)
    641 			goto out;
    642 
    643 		ncookies++;
    644 	}
    645 
    646 	faked = (ip->i_number == NTFS_ROOTINO) ? 1 : 2;
    647 	num = uio->uio_offset / sizeof(struct dirent) - faked;
    648 
    649 	while (uio->uio_resid >= sizeof(struct dirent)) {
    650 		struct attr_indexentry *iep;
    651 		char *fname;
    652 		size_t remains;
    653 		int sz;
    654 
    655 		error = ntfs_ntreaddir(ntmp, fp, num, &iep);
    656 		if (error)
    657 			goto out;
    658 
    659 		if (NULL == iep)
    660 			break;
    661 
    662 		for(; !(iep->ie_flag & NTFS_IEFLAG_LAST) && (uio->uio_resid >= sizeof(struct dirent));
    663 			iep = NTFS_NEXTREC(iep, struct attr_indexentry *))
    664 		{
    665 			if(!ntfs_isnamepermitted(ntmp,iep))
    666 				continue;
    667 
    668 			remains = sizeof(cde->d_name) - 1;
    669 			fname = cde->d_name;
    670 			for(i=0; i<iep->ie_fnamelen; i++) {
    671 				sz = (*ntmp->ntm_wput)(fname, remains,
    672 						iep->ie_fname[i]);
    673 				fname += sz;
    674 				remains -= sz;
    675 			}
    676 			*fname = '\0';
    677 			dprintf(("ntfs_readdir: elem: %d, fname:[%s] type: %d, flag: %d, ",
    678 				num, cde->d_name, iep->ie_fnametype,
    679 				iep->ie_flag));
    680 			cde->d_namlen = fname - (char *) cde->d_name;
    681 			cde->d_fileno = iep->ie_number;
    682 			cde->d_type = (iep->ie_fflag & NTFS_FFLAG_DIR) ? DT_DIR : DT_REG;
    683 			cde->d_reclen = sizeof(struct dirent);
    684 			dprintf(("%s\n", (cde->d_type == DT_DIR) ? "dir":"reg"));
    685 
    686 			error = uiomove((void *)cde, sizeof(struct dirent), uio);
    687 			if (error)
    688 				goto out;
    689 
    690 			ncookies++;
    691 			num++;
    692 		}
    693 	}
    694 
    695 	dprintf(("ntfs_readdir: %d entries (%d bytes) read\n",
    696 		ncookies,(u_int)(uio->uio_offset - off)));
    697 	dprintf(("ntfs_readdir: off: %d resid: %d\n",
    698 		(u_int32_t)uio->uio_offset,uio->uio_resid));
    699 
    700 	if (!error && ap->a_ncookies != NULL) {
    701 		struct dirent* dpStart;
    702 		struct dirent* dp;
    703 #if defined(__FreeBSD__)
    704 		u_long *cookies;
    705 		u_long *cookiep;
    706 #else /* defined(__NetBSD__) */
    707 		off_t *cookies;
    708 		off_t *cookiep;
    709 #endif
    710 
    711 		printf("ntfs_readdir: %d cookies\n",ncookies);
    712 		if (uio->uio_segflg != UIO_SYSSPACE || uio->uio_iovcnt != 1)
    713 			panic("ntfs_readdir: unexpected uio from NFS server");
    714 		dpStart = (struct dirent *)
    715 		     ((caddr_t)uio->uio_iov->iov_base -
    716 			 (uio->uio_offset - off));
    717 #if defined(__FreeBSD__)
    718 		MALLOC(cookies, u_long *, ncookies * sizeof(u_long),
    719 		       M_TEMP, M_WAITOK);
    720 #else /* defined(__NetBSD__) */
    721 		cookies = malloc(ncookies * sizeof(off_t), M_TEMP, M_WAITOK);
    722 #endif
    723 		for (dp = dpStart, cookiep = cookies, i=0;
    724 		     i < ncookies;
    725 		     dp = (struct dirent *)((caddr_t) dp + dp->d_reclen), i++) {
    726 			off += dp->d_reclen;
    727 			*cookiep++ = (u_int) off;
    728 		}
    729 		*ap->a_ncookies = ncookies;
    730 		*ap->a_cookies = cookies;
    731 	}
    732 /*
    733 	if (ap->a_eofflag)
    734 	    *ap->a_eofflag = VTONT(ap->a_vp)->i_size <= uio->uio_offset;
    735 */
    736     out:
    737 	FREE(cde, M_TEMP);
    738 	return (error);
    739 }
    740 
    741 int
    742 ntfs_lookup(ap)
    743 	struct vop_lookup_args /* {
    744 		struct vnode *a_dvp;
    745 		struct vnode **a_vpp;
    746 		struct componentname *a_cnp;
    747 	} */ *ap;
    748 {
    749 	struct vnode *dvp = ap->a_dvp;
    750 	struct ntnode *dip = VTONT(dvp);
    751 	struct ntfsmount *ntmp = dip->i_mp;
    752 	struct componentname *cnp = ap->a_cnp;
    753 	struct ucred *cred = cnp->cn_cred;
    754 	int error;
    755 	int lockparent = cnp->cn_flags & LOCKPARENT;
    756 #if NTFS_DEBUG
    757 	int wantparent = cnp->cn_flags & (LOCKPARENT|WANTPARENT);
    758 #endif
    759 	dprintf(("ntfs_lookup: \"%.*s\" (%ld bytes) in %d, lp: %d, wp: %d \n",
    760 		(int)cnp->cn_namelen, cnp->cn_nameptr, cnp->cn_namelen,
    761 		dip->i_number, lockparent, wantparent));
    762 
    763 	error = VOP_ACCESS(dvp, VEXEC, cred, cnp->cn_proc);
    764 	if(error)
    765 		return (error);
    766 
    767 	if ((cnp->cn_flags & ISLASTCN) &&
    768 	    (dvp->v_mount->mnt_flag & MNT_RDONLY) &&
    769 	    (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME))
    770 		return (EROFS);
    771 
    772 #ifdef __NetBSD__
    773 	/*
    774 	 * We now have a segment name to search for, and a directory
    775 	 * to search.
    776 	 *
    777 	 * Before tediously performing a linear scan of the directory,
    778 	 * check the name cache to see if the directory/name pair
    779 	 * we are looking for is known already.
    780 	 */
    781 	if ((error = cache_lookup(ap->a_dvp, ap->a_vpp, cnp)) >= 0)
    782 		return (error);
    783 #endif
    784 
    785 	if(cnp->cn_namelen == 1 && cnp->cn_nameptr[0] == '.') {
    786 		dprintf(("ntfs_lookup: faking . directory in %d\n",
    787 			dip->i_number));
    788 
    789 		VREF(dvp);
    790 		*ap->a_vpp = dvp;
    791 		error = 0;
    792 	} else if (cnp->cn_flags & ISDOTDOT) {
    793 		struct ntvattr *vap;
    794 
    795 		dprintf(("ntfs_lookup: faking .. directory in %d\n",
    796 			 dip->i_number));
    797 
    798 		error = ntfs_ntvattrget(ntmp, dip, NTFS_A_NAME, NULL, 0, &vap);
    799 		if(error)
    800 			return (error);
    801 
    802 		VOP__UNLOCK(dvp,0,cnp->cn_proc);
    803 		cnp->cn_flags |= PDIRUNLOCK;
    804 
    805 		dprintf(("ntfs_lookup: parentdir: %d\n",
    806 			 vap->va_a_name->n_pnumber));
    807 		error = VFS_VGET(ntmp->ntm_mountp,
    808 				 vap->va_a_name->n_pnumber,ap->a_vpp);
    809 		ntfs_ntvattrrele(vap);
    810 		if (error) {
    811 			if (VN_LOCK(dvp,LK_EXCLUSIVE|LK_RETRY,cnp->cn_proc)==0)
    812 				cnp->cn_flags &= ~PDIRUNLOCK;
    813 			return (error);
    814 		}
    815 
    816 		if (lockparent && (cnp->cn_flags & ISLASTCN)) {
    817 			error = VN_LOCK(dvp, LK_EXCLUSIVE, cnp->cn_proc);
    818 			if (error) {
    819 				vput( *(ap->a_vpp) );
    820 				return (error);
    821 			}
    822 			cnp->cn_flags &= ~PDIRUNLOCK;
    823 		}
    824 	} else {
    825 		error = ntfs_ntlookupfile(ntmp, dvp, cnp, ap->a_vpp);
    826 		if (error) {
    827 			dprintf(("ntfs_ntlookupfile: returned %d\n", error));
    828 			return (error);
    829 		}
    830 
    831 		dprintf(("ntfs_lookup: found ino: %d\n",
    832 			VTONT(*ap->a_vpp)->i_number));
    833 
    834 		if(!lockparent || !(cnp->cn_flags & ISLASTCN))
    835 			VOP__UNLOCK(dvp, 0, cnp->cn_proc);
    836 	}
    837 
    838 	if (cnp->cn_flags & MAKEENTRY)
    839 		cache_enter(dvp, *ap->a_vpp, cnp);
    840 
    841 	return (error);
    842 }
    843 
    844 #if defined(__FreeBSD__)
    845 /*
    846  * Flush the blocks of a file to disk.
    847  *
    848  * This function is worthless for vnodes that represent directories. Maybe we
    849  * could just do a sync if they try an fsync on a directory file.
    850  */
    851 static int
    852 ntfs_fsync(ap)
    853 	struct vop_fsync_args /* {
    854 		struct vnode *a_vp;
    855 		struct ucred *a_cred;
    856 		int a_waitfor;
    857 		off_t offlo;
    858 		off_t offhi;
    859 		struct proc *a_p;
    860 	} */ *ap;
    861 {
    862 	return (0);
    863 }
    864 #endif
    865 
    866 /*
    867  * Return POSIX pathconf information applicable to NTFS filesystem
    868  */
    869 int
    870 ntfs_pathconf(v)
    871 	void *v;
    872 {
    873 	struct vop_pathconf_args /* {
    874 		struct vnode *a_vp;
    875 		int a_name;
    876 		register_t *a_retval;
    877 	} */ *ap = v;
    878 
    879 	switch (ap->a_name) {
    880 	case _PC_LINK_MAX:
    881 		*ap->a_retval = 1;
    882 		return (0);
    883 	case _PC_NAME_MAX:
    884 		*ap->a_retval = NTFS_MAXFILENAME;
    885 		return (0);
    886 	case _PC_PATH_MAX:
    887 		*ap->a_retval = PATH_MAX;
    888 		return (0);
    889 	case _PC_CHOWN_RESTRICTED:
    890 		*ap->a_retval = 1;
    891 		return (0);
    892 	case _PC_NO_TRUNC:
    893 		*ap->a_retval = 0;
    894 		return (0);
    895 	case _PC_SYNC_IO:
    896 		*ap->a_retval = 1;
    897 		return (0);
    898 	case _PC_FILESIZEBITS:
    899 		*ap->a_retval = 64;
    900 		return (0);
    901 	default:
    902 		return (EINVAL);
    903 	}
    904 	/* NOTREACHED */
    905 }
    906 
    907 /*
    908  * Global vfs data structures
    909  */
    910 vop_t **ntfs_vnodeop_p;
    911 #if defined(__FreeBSD__)
    912 static
    913 struct vnodeopv_entry_desc ntfs_vnodeop_entries[] = {
    914 	{ &vop_default_desc, (vop_t *)ntfs_bypass },
    915 
    916 	{ &vop_getattr_desc, (vop_t *)ntfs_getattr },
    917 	{ &vop_inactive_desc, (vop_t *)ntfs_inactive },
    918 	{ &vop_reclaim_desc, (vop_t *)ntfs_reclaim },
    919 	{ &vop_print_desc, (vop_t *)ntfs_print },
    920 	{ &vop_pathconf_desc, ntfs_pathconf },
    921 
    922 	{ &vop_islocked_desc, (vop_t *)vop_stdislocked },
    923 	{ &vop_unlock_desc, (vop_t *)vop_stdunlock },
    924 	{ &vop_lock_desc, (vop_t *)vop_stdlock },
    925 	{ &vop_cachedlookup_desc, (vop_t *)ntfs_lookup },
    926 	{ &vop_lookup_desc, (vop_t *)vfs_cache_lookup },
    927 
    928 	{ &vop_access_desc, (vop_t *)ntfs_access },
    929 	{ &vop_close_desc, (vop_t *)ntfs_close },
    930 	{ &vop_open_desc, (vop_t *)ntfs_open },
    931 	{ &vop_readdir_desc, (vop_t *)ntfs_readdir },
    932 	{ &vop_fsync_desc, (vop_t *)ntfs_fsync },
    933 
    934 	{ &vop_bmap_desc, (vop_t *)ntfs_bmap },
    935 	{ &vop_getpages_desc, (vop_t *) ntfs_getpages },
    936 	{ &vop_putpages_desc, (vop_t *) ntfs_putpages },
    937 	{ &vop_strategy_desc, (vop_t *)ntfs_strategy },
    938 	{ &vop_bwrite_desc, (vop_t *)vop_stdbwrite },
    939 	{ &vop_read_desc, (vop_t *)ntfs_read },
    940 	{ &vop_write_desc, (vop_t *)ntfs_write },
    941 
    942 	{ NULL, NULL }
    943 };
    944 
    945 static
    946 struct vnodeopv_desc ntfs_vnodeop_opv_desc =
    947 	{ &ntfs_vnodeop_p, ntfs_vnodeop_entries };
    948 
    949 VNODEOP_SET(ntfs_vnodeop_opv_desc);
    950 
    951 #else /* !FreeBSD */
    952 
    953 const struct vnodeopv_entry_desc ntfs_vnodeop_entries[] = {
    954 	{ &vop_default_desc, (vop_t *) ntfs_bypass },
    955 	{ &vop_lookup_desc, (vop_t *) ntfs_lookup },	/* lookup */
    956 	{ &vop_create_desc, genfs_eopnotsupp },		/* create */
    957 	{ &vop_mknod_desc, genfs_eopnotsupp },		/* mknod */
    958 	{ &vop_open_desc, (vop_t *) ntfs_open },	/* open */
    959 	{ &vop_close_desc,(vop_t *)  ntfs_close },	/* close */
    960 	{ &vop_access_desc, (vop_t *) ntfs_access },	/* access */
    961 	{ &vop_getattr_desc, (vop_t *) ntfs_getattr },	/* getattr */
    962 	{ &vop_setattr_desc, genfs_eopnotsupp },	/* setattr */
    963 	{ &vop_read_desc, (vop_t *) ntfs_read },	/* read */
    964 	{ &vop_write_desc, (vop_t *) ntfs_write },	/* write */
    965 	{ &vop_lease_desc, genfs_lease_check },		/* lease */
    966 	{ &vop_fcntl_desc, genfs_fcntl },		/* fcntl */
    967 	{ &vop_ioctl_desc, genfs_enoioctl },		/* ioctl */
    968 	{ &vop_poll_desc, genfs_poll },			/* poll */
    969 	{ &vop_kqfilter_desc, genfs_kqfilter },		/* kqfilter */
    970 	{ &vop_revoke_desc, genfs_revoke },		/* revoke */
    971 	{ &vop_mmap_desc, genfs_mmap },			/* mmap */
    972 	{ &vop_fsync_desc, genfs_fsync },		/* fsync */
    973 	{ &vop_seek_desc, genfs_seek },			/* seek */
    974 	{ &vop_remove_desc, ntfs_remove },		/* remove */
    975 	{ &vop_link_desc, genfs_eopnotsupp },		/* link */
    976 	{ &vop_rename_desc, genfs_eopnotsupp },		/* rename */
    977 	{ &vop_mkdir_desc, genfs_eopnotsupp },		/* mkdir */
    978 	{ &vop_rmdir_desc, genfs_eopnotsupp },		/* rmdir */
    979 	{ &vop_symlink_desc, genfs_eopnotsupp },	/* symlink */
    980 	{ &vop_readdir_desc, (vop_t *) ntfs_readdir },	/* readdir */
    981 	{ &vop_readlink_desc, genfs_eopnotsupp },	/* readlink */
    982 	{ &vop_abortop_desc, genfs_abortop },		/* abortop */
    983 	{ &vop_inactive_desc, (vop_t *) ntfs_inactive },	/* inactive */
    984 	{ &vop_reclaim_desc, (vop_t *) ntfs_reclaim },	/* reclaim */
    985 	{ &vop_lock_desc, genfs_lock },			/* lock */
    986 	{ &vop_unlock_desc, genfs_unlock },		/* unlock */
    987 	{ &vop_bmap_desc, (vop_t *) ntfs_bmap },	/* bmap */
    988 	{ &vop_strategy_desc, (vop_t *) ntfs_strategy },	/* strategy */
    989 	{ &vop_print_desc, (vop_t *) ntfs_print },	/* print */
    990 	{ &vop_islocked_desc, genfs_islocked },		/* islocked */
    991 	{ &vop_pathconf_desc, ntfs_pathconf },		/* pathconf */
    992 	{ &vop_advlock_desc, genfs_nullop },		/* advlock */
    993 	{ &vop_blkatoff_desc, genfs_eopnotsupp },	/* blkatoff */
    994 	{ &vop_valloc_desc, genfs_eopnotsupp },		/* valloc */
    995 	{ &vop_reallocblks_desc, genfs_eopnotsupp },	/* reallocblks */
    996 	{ &vop_vfree_desc, genfs_eopnotsupp },		/* vfree */
    997 	{ &vop_truncate_desc, genfs_eopnotsupp },	/* truncate */
    998 	{ &vop_update_desc, genfs_eopnotsupp },		/* update */
    999 	{ &vop_bwrite_desc, vn_bwrite },		/* bwrite */
   1000 	{ &vop_getpages_desc, genfs_compat_getpages },	/* getpages */
   1001 	{ &vop_putpages_desc, genfs_putpages },		/* putpages */
   1002 	{ NULL, NULL }
   1003 };
   1004 const struct vnodeopv_desc ntfs_vnodeop_opv_desc =
   1005 	{ &ntfs_vnodeop_p, ntfs_vnodeop_entries };
   1006 
   1007 #endif
   1008