Home | History | Annotate | Line # | Download | only in ntfs
ntfs_vnops.c revision 1.3
      1 /*	$NetBSD: ntfs_vnops.c,v 1.3 2003/04/09 16:12: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.3 2003/04/09 16:12: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);
    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 	struct ntnode *ip = VTONT(ap->a_vp);
    313 
    314 	printf("tag VT_NTFS, ino %u, flag %#x, usecount %d, nlink %ld\n",
    315 	    ip->i_number, ip->i_flag, ip->i_usecount, ip->i_nlink);
    316 	printf("       ");
    317 	lockmgr_printinfo(ap->a_vp->v_vnlock);
    318 	printf("\n");
    319 	return (0);
    320 }
    321 
    322 /*
    323  * Calculate the logical to physical mapping if not done already,
    324  * then call the device strategy routine.
    325  */
    326 int
    327 ntfs_strategy(ap)
    328 	struct vop_strategy_args /* {
    329 		struct buf *a_bp;
    330 	} */ *ap;
    331 {
    332 	struct buf *bp = ap->a_bp;
    333 	struct vnode *vp = bp->b_vp;
    334 	struct fnode *fp = VTOF(vp);
    335 	struct ntnode *ip = FTONT(fp);
    336 	struct ntfsmount *ntmp = ip->i_mp;
    337 	int error;
    338 
    339 #ifdef __FreeBSD__
    340 	dprintf(("ntfs_strategy: offset: %d, blkno: %d, lblkno: %d\n",
    341 		(u_int32_t)bp->b_offset,(u_int32_t)bp->b_blkno,
    342 		(u_int32_t)bp->b_lblkno));
    343 #else
    344 	dprintf(("ntfs_strategy: blkno: %d, lblkno: %d\n",
    345 		(u_int32_t)bp->b_blkno,
    346 		(u_int32_t)bp->b_lblkno));
    347 #endif
    348 
    349 	dprintf(("strategy: bcount: %d flags: 0x%lx\n",
    350 		(u_int32_t)bp->b_bcount,bp->b_flags));
    351 
    352 	if (bp->b_flags & B_READ) {
    353 		u_int32_t toread;
    354 
    355 		if (ntfs_cntob(bp->b_blkno) >= fp->f_size) {
    356 			clrbuf(bp);
    357 			error = 0;
    358 		} else {
    359 			toread = min(bp->b_bcount,
    360 				 fp->f_size-ntfs_cntob(bp->b_blkno));
    361 			dprintf(("ntfs_strategy: toread: %d, fsize: %d\n",
    362 				toread,(u_int32_t)fp->f_size));
    363 
    364 			error = ntfs_readattr(ntmp, ip, fp->f_attrtype,
    365 				fp->f_attrname, ntfs_cntob(bp->b_blkno),
    366 				toread, bp->b_data, NULL);
    367 
    368 			if (error) {
    369 				printf("ntfs_strategy: ntfs_readattr failed\n");
    370 				bp->b_error = error;
    371 				bp->b_flags |= B_ERROR;
    372 			}
    373 
    374 			bzero(bp->b_data + toread, bp->b_bcount - toread);
    375 		}
    376 	} else {
    377 		size_t tmp;
    378 		u_int32_t towrite;
    379 
    380 		if (ntfs_cntob(bp->b_blkno) + bp->b_bcount >= fp->f_size) {
    381 			printf("ntfs_strategy: CAN'T EXTEND FILE\n");
    382 			bp->b_error = error = EFBIG;
    383 			bp->b_flags |= B_ERROR;
    384 		} else {
    385 			towrite = min(bp->b_bcount,
    386 				fp->f_size-ntfs_cntob(bp->b_blkno));
    387 			dprintf(("ntfs_strategy: towrite: %d, fsize: %d\n",
    388 				towrite,(u_int32_t)fp->f_size));
    389 
    390 			error = ntfs_writeattr_plain(ntmp, ip, fp->f_attrtype,
    391 				fp->f_attrname, ntfs_cntob(bp->b_blkno),towrite,
    392 				bp->b_data, &tmp, NULL);
    393 
    394 			if (error) {
    395 				printf("ntfs_strategy: ntfs_writeattr fail\n");
    396 				bp->b_error = error;
    397 				bp->b_flags |= B_ERROR;
    398 			}
    399 		}
    400 	}
    401 	biodone(bp);
    402 	return (error);
    403 }
    404 
    405 static int
    406 ntfs_write(ap)
    407 	struct vop_write_args /* {
    408 		struct vnode *a_vp;
    409 		struct uio *a_uio;
    410 		int  a_ioflag;
    411 		struct ucred *a_cred;
    412 	} */ *ap;
    413 {
    414 	struct vnode *vp = ap->a_vp;
    415 	struct fnode *fp = VTOF(vp);
    416 	struct ntnode *ip = FTONT(fp);
    417 	struct uio *uio = ap->a_uio;
    418 	struct ntfsmount *ntmp = ip->i_mp;
    419 	u_int64_t towrite;
    420 	size_t written;
    421 	int error;
    422 
    423 	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));
    424 	dprintf(("ntfs_write: filesize: %d",(u_int32_t)fp->f_size));
    425 
    426 	if (uio->uio_resid + uio->uio_offset > fp->f_size) {
    427 		printf("ntfs_write: CAN'T WRITE BEYOND END OF FILE\n");
    428 		return (EFBIG);
    429 	}
    430 
    431 	towrite = min(uio->uio_resid, fp->f_size - uio->uio_offset);
    432 
    433 	dprintf((", towrite: %d\n",(u_int32_t)towrite));
    434 
    435 	error = ntfs_writeattr_plain(ntmp, ip, fp->f_attrtype,
    436 		fp->f_attrname, uio->uio_offset, towrite, NULL, &written, uio);
    437 #ifdef NTFS_DEBUG
    438 	if (error)
    439 		printf("ntfs_write: ntfs_writeattr failed: %d\n", error);
    440 #endif
    441 
    442 	return (error);
    443 }
    444 
    445 int
    446 ntfs_remove(void *v)
    447 {
    448 	struct vop_remove_args /* {
    449 		struct vnode *a_dvp;
    450 		struct vnode *a_vp;
    451 		struct componentname *a_cnp;
    452 	} */ *ap = v;
    453 
    454 	if (ap->a_dvp == ap->a_vp)
    455 		vrele(ap->a_vp);
    456 	else
    457 		vput(ap->a_vp);
    458 	vput(ap->a_dvp);
    459 	return (EOPNOTSUPP);
    460 }
    461 
    462 int
    463 ntfs_access(ap)
    464 	struct vop_access_args /* {
    465 		struct vnode *a_vp;
    466 		int  a_mode;
    467 		struct ucred *a_cred;
    468 		struct proc *a_p;
    469 	} */ *ap;
    470 {
    471 	struct vnode *vp = ap->a_vp;
    472 	struct ntnode *ip = VTONT(vp);
    473 	struct ucred *cred = ap->a_cred;
    474 	mode_t mask, mode = ap->a_mode;
    475 	gid_t *gp;
    476 	int i;
    477 #ifdef QUOTA
    478 	int error;
    479 #endif
    480 
    481 	dprintf(("ntfs_access: %d\n",ip->i_number));
    482 
    483 	/*
    484 	 * Disallow write attempts on read-only file systems;
    485 	 * unless the file is a socket, fifo, or a block or
    486 	 * character device resident on the file system.
    487 	 */
    488 	if (mode & VWRITE) {
    489 		switch ((int)vp->v_type) {
    490 		case VDIR:
    491 		case VLNK:
    492 		case VREG:
    493 			if (vp->v_mount->mnt_flag & MNT_RDONLY)
    494 				return (EROFS);
    495 #ifdef QUOTA
    496 			if (error = getinoquota(ip))
    497 				return (error);
    498 #endif
    499 			break;
    500 		}
    501 	}
    502 
    503 	/* Otherwise, user id 0 always gets access. */
    504 	if (cred->cr_uid == 0)
    505 		return (0);
    506 
    507 	mask = 0;
    508 
    509 	/* Otherwise, check the owner. */
    510 	if (cred->cr_uid == ip->i_mp->ntm_uid) {
    511 		if (mode & VEXEC)
    512 			mask |= S_IXUSR;
    513 		if (mode & VREAD)
    514 			mask |= S_IRUSR;
    515 		if (mode & VWRITE)
    516 			mask |= S_IWUSR;
    517 		return ((ip->i_mp->ntm_mode & mask) == mask ? 0 : EACCES);
    518 	}
    519 
    520 	/* Otherwise, check the groups. */
    521 	for (i = 0, gp = cred->cr_groups; i < cred->cr_ngroups; i++, gp++)
    522 		if (ip->i_mp->ntm_gid == *gp) {
    523 			if (mode & VEXEC)
    524 				mask |= S_IXGRP;
    525 			if (mode & VREAD)
    526 				mask |= S_IRGRP;
    527 			if (mode & VWRITE)
    528 				mask |= S_IWGRP;
    529 			return ((ip->i_mp->ntm_mode&mask) == mask ? 0 : EACCES);
    530 		}
    531 
    532 	/* Otherwise, check everyone else. */
    533 	if (mode & VEXEC)
    534 		mask |= S_IXOTH;
    535 	if (mode & VREAD)
    536 		mask |= S_IROTH;
    537 	if (mode & VWRITE)
    538 		mask |= S_IWOTH;
    539 	return ((ip->i_mp->ntm_mode & mask) == mask ? 0 : EACCES);
    540 }
    541 
    542 /*
    543  * Open called.
    544  *
    545  * Nothing to do.
    546  */
    547 /* ARGSUSED */
    548 static int
    549 ntfs_open(ap)
    550 	struct vop_open_args /* {
    551 		struct vnode *a_vp;
    552 		int  a_mode;
    553 		struct ucred *a_cred;
    554 		struct proc *a_p;
    555 	} */ *ap;
    556 {
    557 #if NTFS_DEBUG
    558 	struct vnode *vp = ap->a_vp;
    559 	struct ntnode *ip = VTONT(vp);
    560 
    561 	printf("ntfs_open: %d\n",ip->i_number);
    562 #endif
    563 
    564 	/*
    565 	 * Files marked append-only must be opened for appending.
    566 	 */
    567 
    568 	return (0);
    569 }
    570 
    571 /*
    572  * Close called.
    573  *
    574  * Update the times on the inode.
    575  */
    576 /* ARGSUSED */
    577 static int
    578 ntfs_close(ap)
    579 	struct vop_close_args /* {
    580 		struct vnode *a_vp;
    581 		int  a_fflag;
    582 		struct ucred *a_cred;
    583 		struct proc *a_p;
    584 	} */ *ap;
    585 {
    586 #if NTFS_DEBUG
    587 	struct vnode *vp = ap->a_vp;
    588 	struct ntnode *ip = VTONT(vp);
    589 
    590 	printf("ntfs_close: %d\n",ip->i_number);
    591 #endif
    592 
    593 	return (0);
    594 }
    595 
    596 int
    597 ntfs_readdir(ap)
    598 	struct vop_readdir_args /* {
    599 		struct vnode *a_vp;
    600 		struct uio *a_uio;
    601 		struct ucred *a_cred;
    602 		int *a_ncookies;
    603 		u_int **cookies;
    604 	} */ *ap;
    605 {
    606 	struct vnode *vp = ap->a_vp;
    607 	struct fnode *fp = VTOF(vp);
    608 	struct ntnode *ip = FTONT(fp);
    609 	struct uio *uio = ap->a_uio;
    610 	struct ntfsmount *ntmp = ip->i_mp;
    611 	int i, error = 0;
    612 	u_int32_t faked = 0, num;
    613 	int ncookies = 0;
    614 	struct dirent *cde;
    615 	off_t off;
    616 
    617 	dprintf(("ntfs_readdir %d off: %d resid: %d\n",ip->i_number,(u_int32_t)uio->uio_offset,uio->uio_resid));
    618 
    619 	off = uio->uio_offset;
    620 
    621 	MALLOC(cde, struct dirent *, sizeof(struct dirent), M_TEMP, M_WAITOK);
    622 
    623 	/* Simulate . in every dir except ROOT */
    624 	if (ip->i_number != NTFS_ROOTINO
    625 	    && uio->uio_offset < sizeof(struct dirent)) {
    626 		cde->d_fileno = ip->i_number;
    627 		cde->d_reclen = sizeof(struct dirent);
    628 		cde->d_type = DT_DIR;
    629 		cde->d_namlen = 1;
    630 		strncpy(cde->d_name, ".", 2);
    631 		error = uiomove((void *)cde, sizeof(struct dirent), uio);
    632 		if (error)
    633 			goto out;
    634 
    635 		ncookies++;
    636 	}
    637 
    638 	/* Simulate .. in every dir including ROOT */
    639 	if (uio->uio_offset < 2 * sizeof(struct dirent)) {
    640 		cde->d_fileno = NTFS_ROOTINO;	/* XXX */
    641 		cde->d_reclen = sizeof(struct dirent);
    642 		cde->d_type = DT_DIR;
    643 		cde->d_namlen = 2;
    644 		strncpy(cde->d_name, "..", 3);
    645 
    646 		error = uiomove((void *) cde, sizeof(struct dirent), uio);
    647 		if (error)
    648 			goto out;
    649 
    650 		ncookies++;
    651 	}
    652 
    653 	faked = (ip->i_number == NTFS_ROOTINO) ? 1 : 2;
    654 	num = uio->uio_offset / sizeof(struct dirent) - faked;
    655 
    656 	while (uio->uio_resid >= sizeof(struct dirent)) {
    657 		struct attr_indexentry *iep;
    658 		char *fname;
    659 		size_t remains;
    660 		int sz;
    661 
    662 		error = ntfs_ntreaddir(ntmp, fp, num, &iep);
    663 		if (error)
    664 			goto out;
    665 
    666 		if (NULL == iep)
    667 			break;
    668 
    669 		for(; !(iep->ie_flag & NTFS_IEFLAG_LAST) && (uio->uio_resid >= sizeof(struct dirent));
    670 			iep = NTFS_NEXTREC(iep, struct attr_indexentry *))
    671 		{
    672 			if(!ntfs_isnamepermitted(ntmp,iep))
    673 				continue;
    674 
    675 			remains = sizeof(cde->d_name) - 1;
    676 			fname = cde->d_name;
    677 			for(i=0; i<iep->ie_fnamelen; i++) {
    678 				sz = (*ntmp->ntm_wput)(fname, remains,
    679 						iep->ie_fname[i]);
    680 				fname += sz;
    681 				remains -= sz;
    682 			}
    683 			*fname = '\0';
    684 			dprintf(("ntfs_readdir: elem: %d, fname:[%s] type: %d, flag: %d, ",
    685 				num, cde->d_name, iep->ie_fnametype,
    686 				iep->ie_flag));
    687 			cde->d_namlen = fname - (char *) cde->d_name;
    688 			cde->d_fileno = iep->ie_number;
    689 			cde->d_type = (iep->ie_fflag & NTFS_FFLAG_DIR) ? DT_DIR : DT_REG;
    690 			cde->d_reclen = sizeof(struct dirent);
    691 			dprintf(("%s\n", (cde->d_type == DT_DIR) ? "dir":"reg"));
    692 
    693 			error = uiomove((void *)cde, sizeof(struct dirent), uio);
    694 			if (error)
    695 				goto out;
    696 
    697 			ncookies++;
    698 			num++;
    699 		}
    700 	}
    701 
    702 	dprintf(("ntfs_readdir: %d entries (%d bytes) read\n",
    703 		ncookies,(u_int)(uio->uio_offset - off)));
    704 	dprintf(("ntfs_readdir: off: %d resid: %d\n",
    705 		(u_int32_t)uio->uio_offset,uio->uio_resid));
    706 
    707 	if (!error && ap->a_ncookies != NULL) {
    708 		struct dirent* dpStart;
    709 		struct dirent* dp;
    710 #if defined(__FreeBSD__)
    711 		u_long *cookies;
    712 		u_long *cookiep;
    713 #else /* defined(__NetBSD__) */
    714 		off_t *cookies;
    715 		off_t *cookiep;
    716 #endif
    717 
    718 		printf("ntfs_readdir: %d cookies\n",ncookies);
    719 		if (uio->uio_segflg != UIO_SYSSPACE || uio->uio_iovcnt != 1)
    720 			panic("ntfs_readdir: unexpected uio from NFS server");
    721 		dpStart = (struct dirent *)
    722 		     ((caddr_t)uio->uio_iov->iov_base -
    723 			 (uio->uio_offset - off));
    724 #if defined(__FreeBSD__)
    725 		MALLOC(cookies, u_long *, ncookies * sizeof(u_long),
    726 		       M_TEMP, M_WAITOK);
    727 #else /* defined(__NetBSD__) */
    728 		cookies = malloc(ncookies * sizeof(off_t), M_TEMP, M_WAITOK);
    729 #endif
    730 		for (dp = dpStart, cookiep = cookies, i=0;
    731 		     i < ncookies;
    732 		     dp = (struct dirent *)((caddr_t) dp + dp->d_reclen), i++) {
    733 			off += dp->d_reclen;
    734 			*cookiep++ = (u_int) off;
    735 		}
    736 		*ap->a_ncookies = ncookies;
    737 		*ap->a_cookies = cookies;
    738 	}
    739 /*
    740 	if (ap->a_eofflag)
    741 	    *ap->a_eofflag = VTONT(ap->a_vp)->i_size <= uio->uio_offset;
    742 */
    743     out:
    744 	FREE(cde, M_TEMP);
    745 	return (error);
    746 }
    747 
    748 int
    749 ntfs_lookup(ap)
    750 	struct vop_lookup_args /* {
    751 		struct vnode *a_dvp;
    752 		struct vnode **a_vpp;
    753 		struct componentname *a_cnp;
    754 	} */ *ap;
    755 {
    756 	struct vnode *dvp = ap->a_dvp;
    757 	struct ntnode *dip = VTONT(dvp);
    758 	struct ntfsmount *ntmp = dip->i_mp;
    759 	struct componentname *cnp = ap->a_cnp;
    760 	struct ucred *cred = cnp->cn_cred;
    761 	int error;
    762 	int lockparent = cnp->cn_flags & LOCKPARENT;
    763 #if NTFS_DEBUG
    764 	int wantparent = cnp->cn_flags & (LOCKPARENT|WANTPARENT);
    765 #endif
    766 	dprintf(("ntfs_lookup: \"%.*s\" (%ld bytes) in %d, lp: %d, wp: %d \n",
    767 		(int)cnp->cn_namelen, cnp->cn_nameptr, cnp->cn_namelen,
    768 		dip->i_number, lockparent, wantparent));
    769 
    770 	error = VOP_ACCESS(dvp, VEXEC, cred, cnp->cn_proc);
    771 	if(error)
    772 		return (error);
    773 
    774 	if ((cnp->cn_flags & ISLASTCN) &&
    775 	    (dvp->v_mount->mnt_flag & MNT_RDONLY) &&
    776 	    (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME))
    777 		return (EROFS);
    778 
    779 #ifdef __NetBSD__
    780 	/*
    781 	 * We now have a segment name to search for, and a directory
    782 	 * to search.
    783 	 *
    784 	 * Before tediously performing a linear scan of the directory,
    785 	 * check the name cache to see if the directory/name pair
    786 	 * we are looking for is known already.
    787 	 */
    788 	if ((error = cache_lookup(ap->a_dvp, ap->a_vpp, cnp)) >= 0)
    789 		return (error);
    790 #endif
    791 
    792 	if(cnp->cn_namelen == 1 && cnp->cn_nameptr[0] == '.') {
    793 		dprintf(("ntfs_lookup: faking . directory in %d\n",
    794 			dip->i_number));
    795 
    796 		VREF(dvp);
    797 		*ap->a_vpp = dvp;
    798 		error = 0;
    799 	} else if (cnp->cn_flags & ISDOTDOT) {
    800 		struct ntvattr *vap;
    801 
    802 		dprintf(("ntfs_lookup: faking .. directory in %d\n",
    803 			 dip->i_number));
    804 
    805 		VOP_UNLOCK(dvp, 0);
    806 		cnp->cn_flags |= PDIRUNLOCK;
    807 
    808 		error = ntfs_ntvattrget(ntmp, dip, NTFS_A_NAME, NULL, 0, &vap);
    809 		if(error)
    810 			return (error);
    811 
    812 		dprintf(("ntfs_lookup: parentdir: %d\n",
    813 			 vap->va_a_name->n_pnumber));
    814 		error = VFS_VGET(ntmp->ntm_mountp,
    815 				 vap->va_a_name->n_pnumber,ap->a_vpp);
    816 		ntfs_ntvattrrele(vap);
    817 		if (error) {
    818 			if (vn_lock(dvp, LK_EXCLUSIVE | LK_RETRY) == 0)
    819 				cnp->cn_flags &= ~PDIRUNLOCK;
    820 			return (error);
    821 		}
    822 
    823 		if (lockparent && (cnp->cn_flags & ISLASTCN)) {
    824 			error = vn_lock(dvp, LK_EXCLUSIVE);
    825 			if (error) {
    826 				vput( *(ap->a_vpp) );
    827 				return (error);
    828 			}
    829 			cnp->cn_flags &= ~PDIRUNLOCK;
    830 		}
    831 	} else {
    832 		error = ntfs_ntlookupfile(ntmp, dvp, cnp, ap->a_vpp);
    833 		if (error) {
    834 			dprintf(("ntfs_ntlookupfile: returned %d\n", error));
    835 			return (error);
    836 		}
    837 
    838 		dprintf(("ntfs_lookup: found ino: %d\n",
    839 			VTONT(*ap->a_vpp)->i_number));
    840 
    841 		if(!lockparent || (cnp->cn_flags & ISLASTCN) == 0) {
    842 			VOP_UNLOCK(dvp, 0);
    843 			cnp->cn_flags |= PDIRUNLOCK;
    844 		}
    845 	}
    846 
    847 	if (cnp->cn_flags & MAKEENTRY)
    848 		cache_enter(dvp, *ap->a_vpp, cnp);
    849 
    850 	return (error);
    851 }
    852 
    853 #if defined(__FreeBSD__)
    854 /*
    855  * Flush the blocks of a file to disk.
    856  *
    857  * This function is worthless for vnodes that represent directories. Maybe we
    858  * could just do a sync if they try an fsync on a directory file.
    859  */
    860 static int
    861 ntfs_fsync(ap)
    862 	struct vop_fsync_args /* {
    863 		struct vnode *a_vp;
    864 		struct ucred *a_cred;
    865 		int a_waitfor;
    866 		off_t offlo;
    867 		off_t offhi;
    868 		struct proc *a_p;
    869 	} */ *ap;
    870 {
    871 	return (0);
    872 }
    873 #endif
    874 
    875 /*
    876  * Return POSIX pathconf information applicable to NTFS filesystem
    877  */
    878 int
    879 ntfs_pathconf(v)
    880 	void *v;
    881 {
    882 	struct vop_pathconf_args /* {
    883 		struct vnode *a_vp;
    884 		int a_name;
    885 		register_t *a_retval;
    886 	} */ *ap = v;
    887 
    888 	switch (ap->a_name) {
    889 	case _PC_LINK_MAX:
    890 		*ap->a_retval = 1;
    891 		return (0);
    892 	case _PC_NAME_MAX:
    893 		*ap->a_retval = NTFS_MAXFILENAME;
    894 		return (0);
    895 	case _PC_PATH_MAX:
    896 		*ap->a_retval = PATH_MAX;
    897 		return (0);
    898 	case _PC_CHOWN_RESTRICTED:
    899 		*ap->a_retval = 1;
    900 		return (0);
    901 	case _PC_NO_TRUNC:
    902 		*ap->a_retval = 0;
    903 		return (0);
    904 	case _PC_SYNC_IO:
    905 		*ap->a_retval = 1;
    906 		return (0);
    907 	case _PC_FILESIZEBITS:
    908 		*ap->a_retval = 64;
    909 		return (0);
    910 	default:
    911 		return (EINVAL);
    912 	}
    913 	/* NOTREACHED */
    914 }
    915 
    916 /*
    917  * Global vfs data structures
    918  */
    919 vop_t **ntfs_vnodeop_p;
    920 #if defined(__FreeBSD__)
    921 static
    922 struct vnodeopv_entry_desc ntfs_vnodeop_entries[] = {
    923 	{ &vop_default_desc, (vop_t *)ntfs_bypass },
    924 
    925 	{ &vop_getattr_desc, (vop_t *)ntfs_getattr },
    926 	{ &vop_inactive_desc, (vop_t *)ntfs_inactive },
    927 	{ &vop_reclaim_desc, (vop_t *)ntfs_reclaim },
    928 	{ &vop_print_desc, (vop_t *)ntfs_print },
    929 	{ &vop_pathconf_desc, ntfs_pathconf },
    930 
    931 	{ &vop_islocked_desc, (vop_t *)vop_stdislocked },
    932 	{ &vop_unlock_desc, (vop_t *)vop_stdunlock },
    933 	{ &vop_lock_desc, (vop_t *)vop_stdlock },
    934 	{ &vop_cachedlookup_desc, (vop_t *)ntfs_lookup },
    935 	{ &vop_lookup_desc, (vop_t *)vfs_cache_lookup },
    936 
    937 	{ &vop_access_desc, (vop_t *)ntfs_access },
    938 	{ &vop_close_desc, (vop_t *)ntfs_close },
    939 	{ &vop_open_desc, (vop_t *)ntfs_open },
    940 	{ &vop_readdir_desc, (vop_t *)ntfs_readdir },
    941 	{ &vop_fsync_desc, (vop_t *)ntfs_fsync },
    942 
    943 	{ &vop_bmap_desc, (vop_t *)ntfs_bmap },
    944 	{ &vop_getpages_desc, (vop_t *) ntfs_getpages },
    945 	{ &vop_putpages_desc, (vop_t *) ntfs_putpages },
    946 	{ &vop_strategy_desc, (vop_t *)ntfs_strategy },
    947 	{ &vop_bwrite_desc, (vop_t *)vop_stdbwrite },
    948 	{ &vop_read_desc, (vop_t *)ntfs_read },
    949 	{ &vop_write_desc, (vop_t *)ntfs_write },
    950 
    951 	{ NULL, NULL }
    952 };
    953 
    954 static
    955 struct vnodeopv_desc ntfs_vnodeop_opv_desc =
    956 	{ &ntfs_vnodeop_p, ntfs_vnodeop_entries };
    957 
    958 VNODEOP_SET(ntfs_vnodeop_opv_desc);
    959 
    960 #else /* !FreeBSD */
    961 
    962 const struct vnodeopv_entry_desc ntfs_vnodeop_entries[] = {
    963 	{ &vop_default_desc, (vop_t *) ntfs_bypass },
    964 	{ &vop_lookup_desc, (vop_t *) ntfs_lookup },	/* lookup */
    965 	{ &vop_create_desc, genfs_eopnotsupp },		/* create */
    966 	{ &vop_mknod_desc, genfs_eopnotsupp },		/* mknod */
    967 	{ &vop_open_desc, (vop_t *) ntfs_open },	/* open */
    968 	{ &vop_close_desc,(vop_t *)  ntfs_close },	/* close */
    969 	{ &vop_access_desc, (vop_t *) ntfs_access },	/* access */
    970 	{ &vop_getattr_desc, (vop_t *) ntfs_getattr },	/* getattr */
    971 	{ &vop_setattr_desc, genfs_eopnotsupp },	/* setattr */
    972 	{ &vop_read_desc, (vop_t *) ntfs_read },	/* read */
    973 	{ &vop_write_desc, (vop_t *) ntfs_write },	/* write */
    974 	{ &vop_lease_desc, genfs_lease_check },		/* lease */
    975 	{ &vop_fcntl_desc, genfs_fcntl },		/* fcntl */
    976 	{ &vop_ioctl_desc, genfs_enoioctl },		/* ioctl */
    977 	{ &vop_poll_desc, genfs_poll },			/* poll */
    978 	{ &vop_kqfilter_desc, genfs_kqfilter },		/* kqfilter */
    979 	{ &vop_revoke_desc, genfs_revoke },		/* revoke */
    980 	{ &vop_mmap_desc, genfs_mmap },			/* mmap */
    981 	{ &vop_fsync_desc, genfs_fsync },		/* fsync */
    982 	{ &vop_seek_desc, genfs_seek },			/* seek */
    983 	{ &vop_remove_desc, ntfs_remove },		/* remove */
    984 	{ &vop_link_desc, genfs_eopnotsupp },		/* link */
    985 	{ &vop_rename_desc, genfs_eopnotsupp },		/* rename */
    986 	{ &vop_mkdir_desc, genfs_eopnotsupp },		/* mkdir */
    987 	{ &vop_rmdir_desc, genfs_eopnotsupp },		/* rmdir */
    988 	{ &vop_symlink_desc, genfs_eopnotsupp },	/* symlink */
    989 	{ &vop_readdir_desc, (vop_t *) ntfs_readdir },	/* readdir */
    990 	{ &vop_readlink_desc, genfs_eopnotsupp },	/* readlink */
    991 	{ &vop_abortop_desc, genfs_abortop },		/* abortop */
    992 	{ &vop_inactive_desc, (vop_t *) ntfs_inactive },	/* inactive */
    993 	{ &vop_reclaim_desc, (vop_t *) ntfs_reclaim },	/* reclaim */
    994 	{ &vop_lock_desc, genfs_lock },			/* lock */
    995 	{ &vop_unlock_desc, genfs_unlock },		/* unlock */
    996 	{ &vop_bmap_desc, (vop_t *) ntfs_bmap },	/* bmap */
    997 	{ &vop_strategy_desc, (vop_t *) ntfs_strategy },	/* strategy */
    998 	{ &vop_print_desc, (vop_t *) ntfs_print },	/* print */
    999 	{ &vop_islocked_desc, genfs_islocked },		/* islocked */
   1000 	{ &vop_pathconf_desc, ntfs_pathconf },		/* pathconf */
   1001 	{ &vop_advlock_desc, genfs_nullop },		/* advlock */
   1002 	{ &vop_blkatoff_desc, genfs_eopnotsupp },	/* blkatoff */
   1003 	{ &vop_valloc_desc, genfs_eopnotsupp },		/* valloc */
   1004 	{ &vop_reallocblks_desc, genfs_eopnotsupp },	/* reallocblks */
   1005 	{ &vop_vfree_desc, genfs_eopnotsupp },		/* vfree */
   1006 	{ &vop_truncate_desc, genfs_eopnotsupp },	/* truncate */
   1007 	{ &vop_update_desc, genfs_eopnotsupp },		/* update */
   1008 	{ &vop_bwrite_desc, vn_bwrite },		/* bwrite */
   1009 	{ &vop_getpages_desc, genfs_compat_getpages },	/* getpages */
   1010 	{ &vop_putpages_desc, genfs_putpages },		/* putpages */
   1011 	{ NULL, NULL }
   1012 };
   1013 const struct vnodeopv_desc ntfs_vnodeop_opv_desc =
   1014 	{ &ntfs_vnodeop_p, ntfs_vnodeop_entries };
   1015 
   1016 #endif
   1017