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