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