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