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