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