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