Home | History | Annotate | Line # | Download | only in ntfs
ntfs_vnops.c revision 1.39
      1 /*	$NetBSD: ntfs_vnops.c,v 1.39 2008/01/29 18:21:10 pooka Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1992, 1993
      5  *	The Regents of the University of California.  All rights reserved.
      6  *
      7  * This code is derived from software contributed to Berkeley by
      8  * John Heidemann of the UCLA Ficus project.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  * 3. Neither the name of the University nor the names of its contributors
     19  *    may be used to endorse or promote products derived from this software
     20  *    without specific prior written permission.
     21  *
     22  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     23  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     25  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     26  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     27  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     28  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     29  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     31  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     32  * SUCH DAMAGE.
     33  *
     34  *	Id: ntfs_vnops.c,v 1.5 1999/05/12 09:43:06 semenu Exp
     35  *
     36  */
     37 
     38 #include <sys/cdefs.h>
     39 __KERNEL_RCSID(0, "$NetBSD: ntfs_vnops.c,v 1.39 2008/01/29 18:21:10 pooka Exp $");
     40 
     41 #include <sys/param.h>
     42 #include <sys/systm.h>
     43 #include <sys/kernel.h>
     44 #include <sys/time.h>
     45 #include <sys/stat.h>
     46 #include <sys/vnode.h>
     47 #include <sys/mount.h>
     48 #include <sys/namei.h>
     49 #include <sys/malloc.h>
     50 #include <sys/buf.h>
     51 #include <sys/dirent.h>
     52 #include <sys/kauth.h>
     53 #include <sys/sysctl.h>
     54 
     55 
     56 #include <fs/ntfs/ntfs.h>
     57 #include <fs/ntfs/ntfs_inode.h>
     58 #include <fs/ntfs/ntfs_subr.h>
     59 #include <miscfs/specfs/specdev.h>
     60 #include <miscfs/genfs/genfs.h>
     61 
     62 #include <sys/unistd.h> /* for pathconf(2) constants */
     63 
     64 static int	ntfs_bypass(void *);
     65 static int	ntfs_read(void *);
     66 static int	ntfs_write(void *);
     67 static int	ntfs_getattr(void *);
     68 static int	ntfs_inactive(void *);
     69 static int	ntfs_print(void *);
     70 static int	ntfs_reclaim(void *);
     71 static int	ntfs_strategy(void *);
     72 static int	ntfs_access(void *);
     73 static int	ntfs_open(void *);
     74 static int	ntfs_close(void *);
     75 static int	ntfs_readdir(void *);
     76 static int	ntfs_lookup(void *);
     77 static int	ntfs_bmap(void *);
     78 static int	ntfs_fsync(void *);
     79 static int	ntfs_pathconf(void *);
     80 
     81 extern int prtactive;
     82 
     83 /*
     84  * This is a noop, simply returning what one has been given.
     85  */
     86 int
     87 ntfs_bmap(void *v)
     88 {
     89 	struct vop_bmap_args /* {
     90 		struct vnode *a_vp;
     91 		daddr_t  a_bn;
     92 		struct vnode **a_vpp;
     93 		daddr_t *a_bnp;
     94 		int *a_runp;
     95 		int *a_runb;
     96 	} */ *ap = v;
     97 	dprintf(("ntfs_bmap: vn: %p, blk: %d\n", ap->a_vp,(u_int32_t)ap->a_bn));
     98 	if (ap->a_vpp != NULL)
     99 		*ap->a_vpp = ap->a_vp;
    100 	if (ap->a_bnp != NULL)
    101 		*ap->a_bnp = ap->a_bn;
    102 	if (ap->a_runp != NULL)
    103 		*ap->a_runp = 0;
    104 	return (0);
    105 }
    106 
    107 static int
    108 ntfs_read(void *v)
    109 {
    110 	struct vop_read_args /* {
    111 		struct vnode *a_vp;
    112 		struct uio *a_uio;
    113 		int a_ioflag;
    114 		kauth_cred_t a_cred;
    115 	} */ *ap = v;
    116 	struct vnode *vp = ap->a_vp;
    117 	struct fnode *fp = VTOF(vp);
    118 	struct ntnode *ip = FTONT(fp);
    119 	struct uio *uio = ap->a_uio;
    120 	struct ntfsmount *ntmp = ip->i_mp;
    121 	u_int64_t toread;
    122 	int error;
    123 
    124 	dprintf(("ntfs_read: ino: %llu, off: %qd resid: %qd\n",
    125 	    (unsigned long long)ip->i_number, (long long)uio->uio_offset,
    126 	    (long long)uio->uio_resid));
    127 
    128 	dprintf(("ntfs_read: filesize: %qu",(long long)fp->f_size));
    129 
    130 	/* don't allow reading after end of file */
    131 	if (uio->uio_offset > fp->f_size)
    132 		toread = 0;
    133 	else
    134 		toread = MIN(uio->uio_resid, fp->f_size - uio->uio_offset );
    135 
    136 	dprintf((", toread: %qu\n",(long long)toread));
    137 
    138 	if (toread == 0)
    139 		return (0);
    140 
    141 	error = ntfs_readattr(ntmp, ip, fp->f_attrtype,
    142 		fp->f_attrname, uio->uio_offset, toread, NULL, uio);
    143 	if (error) {
    144 		printf("ntfs_read: ntfs_readattr failed: %d\n",error);
    145 		return (error);
    146 	}
    147 
    148 	return (0);
    149 }
    150 
    151 static int
    152 ntfs_bypass(void *v)
    153 {
    154 	struct vop_generic_args /* {
    155 		struct vnodeop_desc *a_desc;
    156 		<other random data follows, presumably>
    157 	} */ *ap __unused = v;
    158 	int error = ENOTTY;
    159 	dprintf(("ntfs_bypass: %s\n", ap->a_desc->vdesc_name));
    160 	return (error);
    161 }
    162 
    163 
    164 static int
    165 ntfs_getattr(void *v)
    166 {
    167 	struct vop_getattr_args /* {
    168 		struct vnode *a_vp;
    169 		struct vattr *a_vap;
    170 		kauth_cred_t a_cred;
    171 	} */ *ap = v;
    172 	struct vnode *vp = ap->a_vp;
    173 	struct fnode *fp = VTOF(vp);
    174 	struct ntnode *ip = FTONT(fp);
    175 	struct vattr *vap = ap->a_vap;
    176 
    177 	dprintf(("ntfs_getattr: %llu, flags: %d\n",
    178 	    (unsigned long long)ip->i_number, ip->i_flag));
    179 
    180 	vap->va_fsid = ip->i_dev;
    181 	vap->va_fileid = ip->i_number;
    182 	vap->va_mode = ip->i_mp->ntm_mode;
    183 	vap->va_nlink = ip->i_nlink;
    184 	vap->va_uid = ip->i_mp->ntm_uid;
    185 	vap->va_gid = ip->i_mp->ntm_gid;
    186 	vap->va_rdev = 0;				/* XXX UNODEV ? */
    187 	vap->va_size = fp->f_size;
    188 	vap->va_bytes = fp->f_allocated;
    189 	vap->va_atime = ntfs_nttimetounix(fp->f_times.t_access);
    190 	vap->va_mtime = ntfs_nttimetounix(fp->f_times.t_write);
    191 	vap->va_ctime = ntfs_nttimetounix(fp->f_times.t_create);
    192 	vap->va_flags = ip->i_flag;
    193 	vap->va_gen = 0;
    194 	vap->va_blocksize = ip->i_mp->ntm_spc * ip->i_mp->ntm_bps;
    195 	vap->va_type = vp->v_type;
    196 	vap->va_filerev = 0;
    197 	return (0);
    198 }
    199 
    200 
    201 /*
    202  * Last reference to an ntnode.  If necessary, write or delete it.
    203  */
    204 int
    205 ntfs_inactive(void *v)
    206 {
    207 	struct vop_inactive_args /* {
    208 		struct vnode *a_vp;
    209 	} */ *ap = v;
    210 	struct vnode *vp = ap->a_vp;
    211 #ifdef NTFS_DEBUG
    212 	struct ntnode *ip = VTONT(vp);
    213 #endif
    214 
    215 	dprintf(("ntfs_inactive: vnode: %p, ntnode: %llu\n", vp,
    216 	    (unsigned long long)ip->i_number));
    217 
    218 	VOP_UNLOCK(vp, 0);
    219 
    220 	/* XXX since we don't support any filesystem changes
    221 	 * right now, nothing more needs to be done
    222 	 */
    223 	return (0);
    224 }
    225 
    226 /*
    227  * Reclaim an fnode/ntnode so that it can be used for other purposes.
    228  */
    229 int
    230 ntfs_reclaim(void *v)
    231 {
    232 	struct vop_reclaim_args /* {
    233 		struct vnode *a_vp;
    234 	} */ *ap = v;
    235 	struct vnode *vp = ap->a_vp;
    236 	struct fnode *fp = VTOF(vp);
    237 	struct ntnode *ip = FTONT(fp);
    238 	int error;
    239 
    240 	dprintf(("ntfs_reclaim: vnode: %p, ntnode: %llu\n", vp,
    241 	    (unsigned long long)ip->i_number));
    242 
    243 	if (prtactive && vp->v_usecount > 1)
    244 		vprint("ntfs_reclaim: pushing active", vp);
    245 
    246 	if ((error = ntfs_ntget(ip)) != 0)
    247 		return (error);
    248 
    249 	/* Purge old data structures associated with the inode. */
    250 	cache_purge(vp);
    251 	if (ip->i_devvp) {
    252 		vrele(ip->i_devvp);
    253 		ip->i_devvp = NULL;
    254 	}
    255 
    256 	genfs_node_destroy(vp);
    257 	ntfs_frele(fp);
    258 	ntfs_ntput(ip);
    259 	vp->v_data = NULL;
    260 
    261 	return (0);
    262 }
    263 
    264 static int
    265 ntfs_print(void *v)
    266 {
    267 	struct vop_print_args /* {
    268 		struct vnode *a_vp;
    269 	} */ *ap = v;
    270 	struct ntnode *ip = VTONT(ap->a_vp);
    271 
    272 	printf("tag VT_NTFS, ino %llu, flag %#x, usecount %d, nlink %ld\n",
    273 	    (unsigned long long)ip->i_number, ip->i_flag, ip->i_usecount,
    274 	    ip->i_nlink);
    275 	printf("       ");
    276 	lockmgr_printinfo(ap->a_vp->v_vnlock);
    277 	printf("\n");
    278 	return (0);
    279 }
    280 
    281 /*
    282  * Calculate the logical to physical mapping if not done already,
    283  * then call the device strategy routine.
    284  */
    285 int
    286 ntfs_strategy(void *v)
    287 {
    288 	struct vop_strategy_args /* {
    289 		struct vnode *a_vp;
    290 		struct buf *a_bp;
    291 	} */ *ap = v;
    292 	struct buf *bp = ap->a_bp;
    293 	struct vnode *vp = ap->a_vp;
    294 	struct fnode *fp = VTOF(vp);
    295 	struct ntnode *ip = FTONT(fp);
    296 	struct ntfsmount *ntmp = ip->i_mp;
    297 	int error;
    298 
    299 	dprintf(("ntfs_strategy: blkno: %d, lblkno: %d\n",
    300 		(u_int32_t)bp->b_blkno,
    301 		(u_int32_t)bp->b_lblkno));
    302 
    303 	dprintf(("strategy: bcount: %u flags: 0x%x\n",
    304 		(u_int32_t)bp->b_bcount,bp->b_flags));
    305 
    306 	if (bp->b_flags & B_READ) {
    307 		u_int32_t toread;
    308 
    309 		if (ntfs_cntob(bp->b_blkno) >= fp->f_size) {
    310 			clrbuf(bp);
    311 			error = 0;
    312 		} else {
    313 			toread = MIN(bp->b_bcount,
    314 				 fp->f_size - ntfs_cntob(bp->b_blkno));
    315 			dprintf(("ntfs_strategy: toread: %d, fsize: %d\n",
    316 				toread,(u_int32_t)fp->f_size));
    317 
    318 			error = ntfs_readattr(ntmp, ip, fp->f_attrtype,
    319 				fp->f_attrname, ntfs_cntob(bp->b_blkno),
    320 				toread, bp->b_data, NULL);
    321 
    322 			if (error) {
    323 				printf("ntfs_strategy: ntfs_readattr failed\n");
    324 				bp->b_error = error;
    325 			}
    326 
    327 			memset((char *)bp->b_data + toread, 0,
    328 			    bp->b_bcount - toread);
    329 		}
    330 	} else {
    331 		size_t tmp;
    332 		u_int32_t towrite;
    333 
    334 		if (ntfs_cntob(bp->b_blkno) + bp->b_bcount >= fp->f_size) {
    335 			printf("ntfs_strategy: CAN'T EXTEND FILE\n");
    336 			bp->b_error = error = EFBIG;
    337 		} else {
    338 			towrite = MIN(bp->b_bcount,
    339 				fp->f_size - ntfs_cntob(bp->b_blkno));
    340 			dprintf(("ntfs_strategy: towrite: %d, fsize: %d\n",
    341 				towrite,(u_int32_t)fp->f_size));
    342 
    343 			error = ntfs_writeattr_plain(ntmp, ip, fp->f_attrtype,
    344 				fp->f_attrname, ntfs_cntob(bp->b_blkno),towrite,
    345 				bp->b_data, &tmp, NULL);
    346 
    347 			if (error) {
    348 				printf("ntfs_strategy: ntfs_writeattr fail\n");
    349 				bp->b_error = error;
    350 			}
    351 		}
    352 	}
    353 	biodone(bp);
    354 	return (error);
    355 }
    356 
    357 static int
    358 ntfs_write(void *v)
    359 {
    360 	struct vop_write_args /* {
    361 		struct vnode *a_vp;
    362 		struct uio *a_uio;
    363 		int  a_ioflag;
    364 		kauth_cred_t a_cred;
    365 	} */ *ap = v;
    366 	struct vnode *vp = ap->a_vp;
    367 	struct fnode *fp = VTOF(vp);
    368 	struct ntnode *ip = FTONT(fp);
    369 	struct uio *uio = ap->a_uio;
    370 	struct ntfsmount *ntmp = ip->i_mp;
    371 	u_int64_t towrite;
    372 	size_t written;
    373 	int error;
    374 
    375 	dprintf(("ntfs_write: ino: %llu, off: %qd resid: %qd\n",
    376 	    (unsigned long long)ip->i_number, (long long)uio->uio_offset,
    377 	    (long long)uio->uio_resid));
    378 	dprintf(("ntfs_write: filesize: %qu",(long long)fp->f_size));
    379 
    380 	if (uio->uio_resid + uio->uio_offset > fp->f_size) {
    381 		printf("ntfs_write: CAN'T WRITE BEYOND END OF FILE\n");
    382 		return (EFBIG);
    383 	}
    384 
    385 	towrite = MIN(uio->uio_resid, fp->f_size - uio->uio_offset);
    386 
    387 	dprintf((", towrite: %qu\n",(long long)towrite));
    388 
    389 	error = ntfs_writeattr_plain(ntmp, ip, fp->f_attrtype,
    390 		fp->f_attrname, uio->uio_offset, towrite, NULL, &written, uio);
    391 #ifdef NTFS_DEBUG
    392 	if (error)
    393 		printf("ntfs_write: ntfs_writeattr failed: %d\n", error);
    394 #endif
    395 
    396 	return (error);
    397 }
    398 
    399 int
    400 ntfs_access(void *v)
    401 {
    402 	struct vop_access_args /* {
    403 		struct vnode *a_vp;
    404 		int  a_mode;
    405 		kauth_cred_t a_cred;
    406 	} */ *ap = v;
    407 	struct vnode *vp = ap->a_vp;
    408 	struct ntnode *ip = VTONT(vp);
    409 	kauth_cred_t cred = ap->a_cred;
    410 	mode_t mask, mode = ap->a_mode;
    411 	gid_t grp;
    412 	int i;
    413 	uint16_t ngroups;
    414 
    415 	dprintf(("ntfs_access: %llu\n", (unsigned long long)ip->i_number));
    416 
    417 	/*
    418 	 * Disallow write attempts on read-only file systems;
    419 	 * unless the file is a socket, fifo, or a block or
    420 	 * character device resident on the file system.
    421 	 */
    422 	if (mode & VWRITE) {
    423 		switch ((int)vp->v_type) {
    424 		case VDIR:
    425 		case VLNK:
    426 		case VREG:
    427 			if (vp->v_mount->mnt_flag & MNT_RDONLY)
    428 				return (EROFS);
    429 			break;
    430 		}
    431 	}
    432 
    433 	/* Otherwise, user id 0 always gets access. */
    434 	if (kauth_authorize_generic(cred, KAUTH_GENERIC_ISSUSER, NULL) == 0)
    435 		return (0);
    436 
    437 	mask = 0;
    438 
    439 	/* Otherwise, check the owner. */
    440 	if (kauth_cred_geteuid(cred) == ip->i_mp->ntm_uid) {
    441 		if (mode & VEXEC)
    442 			mask |= S_IXUSR;
    443 		if (mode & VREAD)
    444 			mask |= S_IRUSR;
    445 		if (mode & VWRITE)
    446 			mask |= S_IWUSR;
    447 		return ((ip->i_mp->ntm_mode & mask) == mask ? 0 : EACCES);
    448 	}
    449 
    450 	/* Otherwise, check the groups. */
    451 	ngroups = kauth_cred_ngroups(cred);
    452 	for (i = 0; i < ngroups; i++) {
    453 		grp = kauth_cred_group(cred, i);
    454 		if (ip->i_mp->ntm_gid == grp) {
    455 			if (mode & VEXEC)
    456 				mask |= S_IXGRP;
    457 			if (mode & VREAD)
    458 				mask |= S_IRGRP;
    459 			if (mode & VWRITE)
    460 				mask |= S_IWGRP;
    461 			return ((ip->i_mp->ntm_mode&mask) == mask ? 0 : EACCES);
    462 		}
    463 	}
    464 
    465 	/* Otherwise, check everyone else. */
    466 	if (mode & VEXEC)
    467 		mask |= S_IXOTH;
    468 	if (mode & VREAD)
    469 		mask |= S_IROTH;
    470 	if (mode & VWRITE)
    471 		mask |= S_IWOTH;
    472 	return ((ip->i_mp->ntm_mode & mask) == mask ? 0 : EACCES);
    473 }
    474 
    475 /*
    476  * Open called.
    477  *
    478  * Nothing to do.
    479  */
    480 /* ARGSUSED */
    481 static int
    482 ntfs_open(void *v)
    483 {
    484 	struct vop_open_args /* {
    485 		struct vnode *a_vp;
    486 		int  a_mode;
    487 		kauth_cred_t a_cred;
    488 	} */ *ap __unused = v;
    489 #ifdef NTFS_DEBUG
    490 	struct vnode *vp = ap->a_vp;
    491 	struct ntnode *ip = VTONT(vp);
    492 
    493 	printf("ntfs_open: %llu\n", (unsigned long long)ip->i_number);
    494 #endif
    495 
    496 	/*
    497 	 * Files marked append-only must be opened for appending.
    498 	 */
    499 
    500 	return (0);
    501 }
    502 
    503 /*
    504  * Close called.
    505  *
    506  * Update the times on the inode.
    507  */
    508 /* ARGSUSED */
    509 static int
    510 ntfs_close(void *v)
    511 {
    512 	struct vop_close_args /* {
    513 		struct vnode *a_vp;
    514 		int  a_fflag;
    515 		kauth_cred_t a_cred;
    516 	} */ *ap __unused = v;
    517 #ifdef NTFS_DEBUG
    518 	struct vnode *vp = ap->a_vp;
    519 	struct ntnode *ip = VTONT(vp);
    520 
    521 	printf("ntfs_close: %llu\n", (unsigned long long)ip->i_number);
    522 #endif
    523 
    524 	return (0);
    525 }
    526 
    527 int
    528 ntfs_readdir(void *v)
    529 {
    530 	struct vop_readdir_args /* {
    531 		struct vnode *a_vp;
    532 		struct uio *a_uio;
    533 		kauth_cred_t a_cred;
    534 		int *a_ncookies;
    535 		u_int **cookies;
    536 	} */ *ap = v;
    537 	struct vnode *vp = ap->a_vp;
    538 	struct fnode *fp = VTOF(vp);
    539 	struct ntnode *ip = FTONT(fp);
    540 	struct uio *uio = ap->a_uio;
    541 	struct ntfsmount *ntmp = ip->i_mp;
    542 	int i, error = 0;
    543 	u_int32_t faked = 0, num;
    544 	int ncookies = 0;
    545 	struct dirent *cde;
    546 	off_t off;
    547 
    548 	dprintf(("ntfs_readdir %llu off: %qd resid: %qd\n",
    549 	    (unsigned long long)ip->i_number, (long long)uio->uio_offset,
    550 	    (long long)uio->uio_resid));
    551 
    552 	off = uio->uio_offset;
    553 
    554 	MALLOC(cde, struct dirent *, sizeof(struct dirent), M_TEMP, M_WAITOK);
    555 
    556 	/* Simulate . in every dir except ROOT */
    557 	if (ip->i_number != NTFS_ROOTINO
    558 	    && uio->uio_offset < sizeof(struct dirent)) {
    559 		cde->d_fileno = ip->i_number;
    560 		cde->d_reclen = sizeof(struct dirent);
    561 		cde->d_type = DT_DIR;
    562 		cde->d_namlen = 1;
    563 		strncpy(cde->d_name, ".", 2);
    564 		error = uiomove((void *)cde, sizeof(struct dirent), uio);
    565 		if (error)
    566 			goto out;
    567 
    568 		ncookies++;
    569 	}
    570 
    571 	/* Simulate .. in every dir including ROOT */
    572 	if (uio->uio_offset < 2 * sizeof(struct dirent)) {
    573 		cde->d_fileno = NTFS_ROOTINO;	/* XXX */
    574 		cde->d_reclen = sizeof(struct dirent);
    575 		cde->d_type = DT_DIR;
    576 		cde->d_namlen = 2;
    577 		strncpy(cde->d_name, "..", 3);
    578 
    579 		error = uiomove((void *) cde, sizeof(struct dirent), uio);
    580 		if (error)
    581 			goto out;
    582 
    583 		ncookies++;
    584 	}
    585 
    586 	faked = (ip->i_number == NTFS_ROOTINO) ? 1 : 2;
    587 	num = uio->uio_offset / sizeof(struct dirent) - faked;
    588 
    589 	while (uio->uio_resid >= sizeof(struct dirent)) {
    590 		struct attr_indexentry *iep;
    591 		char *fname;
    592 		size_t remains;
    593 		int sz;
    594 
    595 		error = ntfs_ntreaddir(ntmp, fp, num, &iep);
    596 		if (error)
    597 			goto out;
    598 
    599 		if (NULL == iep)
    600 			break;
    601 
    602 		for(; !(iep->ie_flag & NTFS_IEFLAG_LAST) && (uio->uio_resid >= sizeof(struct dirent));
    603 			iep = NTFS_NEXTREC(iep, struct attr_indexentry *))
    604 		{
    605 			if(!ntfs_isnamepermitted(ntmp,iep))
    606 				continue;
    607 
    608 			remains = sizeof(cde->d_name) - 1;
    609 			fname = cde->d_name;
    610 			for(i=0; i<iep->ie_fnamelen; i++) {
    611 				sz = (*ntmp->ntm_wput)(fname, remains,
    612 						iep->ie_fname[i]);
    613 				fname += sz;
    614 				remains -= sz;
    615 			}
    616 			*fname = '\0';
    617 			dprintf(("ntfs_readdir: elem: %d, fname:[%s] type: %d, flag: %d, ",
    618 				num, cde->d_name, iep->ie_fnametype,
    619 				iep->ie_flag));
    620 			cde->d_namlen = fname - (char *) cde->d_name;
    621 			cde->d_fileno = iep->ie_number;
    622 			cde->d_type = (iep->ie_fflag & NTFS_FFLAG_DIR) ? DT_DIR : DT_REG;
    623 			cde->d_reclen = sizeof(struct dirent);
    624 			dprintf(("%s\n", (cde->d_type == DT_DIR) ? "dir":"reg"));
    625 
    626 			error = uiomove((void *)cde, sizeof(struct dirent), uio);
    627 			if (error)
    628 				goto out;
    629 
    630 			ncookies++;
    631 			num++;
    632 		}
    633 	}
    634 
    635 	dprintf(("ntfs_readdir: %d entries (%d bytes) read\n",
    636 		ncookies,(u_int)(uio->uio_offset - off)));
    637 	dprintf(("ntfs_readdir: off: %qd resid: %qu\n",
    638 		(long long)uio->uio_offset,(long long)uio->uio_resid));
    639 
    640 	if (!error && ap->a_ncookies != NULL) {
    641 		struct dirent* dpStart;
    642 		struct dirent* dp;
    643 		off_t *cookies;
    644 		off_t *cookiep;
    645 
    646 		dprintf(("ntfs_readdir: %d cookies\n",ncookies));
    647 		dpStart = (struct dirent *)
    648 		     ((char *)uio->uio_iov->iov_base -
    649 			 (uio->uio_offset - off));
    650 		cookies = malloc(ncookies * sizeof(off_t), M_TEMP, M_WAITOK);
    651 		for (dp = dpStart, cookiep = cookies, i=0;
    652 		     i < ncookies;
    653 		     dp = (struct dirent *)((char *) dp + dp->d_reclen), i++) {
    654 			off += dp->d_reclen;
    655 			*cookiep++ = (u_int) off;
    656 		}
    657 		*ap->a_ncookies = ncookies;
    658 		*ap->a_cookies = cookies;
    659 	}
    660 /*
    661 	if (ap->a_eofflag)
    662 	    *ap->a_eofflag = VTONT(ap->a_vp)->i_size <= uio->uio_offset;
    663 */
    664     out:
    665 	FREE(cde, M_TEMP);
    666 	return (error);
    667 }
    668 
    669 int
    670 ntfs_lookup(void *v)
    671 {
    672 	struct vop_lookup_args /* {
    673 		struct vnode *a_dvp;
    674 		struct vnode **a_vpp;
    675 		struct componentname *a_cnp;
    676 	} */ *ap = v;
    677 	struct vnode *dvp = ap->a_dvp;
    678 	struct ntnode *dip = VTONT(dvp);
    679 	struct ntfsmount *ntmp = dip->i_mp;
    680 	struct componentname *cnp = ap->a_cnp;
    681 	kauth_cred_t cred = cnp->cn_cred;
    682 	int error;
    683 
    684 	dprintf(("ntfs_lookup: \"%.*s\" (%ld bytes) in %llu\n",
    685 	    (int)cnp->cn_namelen, cnp->cn_nameptr, cnp->cn_namelen,
    686 	    (unsigned long long)dip->i_number));
    687 
    688 	error = VOP_ACCESS(dvp, VEXEC, cred);
    689 	if(error)
    690 		return (error);
    691 
    692 	if ((cnp->cn_flags & ISLASTCN) &&
    693 	    (dvp->v_mount->mnt_flag & MNT_RDONLY) &&
    694 	    (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME))
    695 		return (EROFS);
    696 
    697 	/*
    698 	 * We now have a segment name to search for, and a directory
    699 	 * to search.
    700 	 *
    701 	 * Before tediously performing a linear scan of the directory,
    702 	 * check the name cache to see if the directory/name pair
    703 	 * we are looking for is known already.
    704 	 */
    705 	if ((error = cache_lookup(ap->a_dvp, ap->a_vpp, cnp)) >= 0)
    706 		return (error);
    707 
    708 	if(cnp->cn_namelen == 1 && cnp->cn_nameptr[0] == '.') {
    709 		dprintf(("ntfs_lookup: faking . directory in %llu\n",
    710 		    (unsigned long long)dip->i_number));
    711 
    712 		VREF(dvp);
    713 		*ap->a_vpp = dvp;
    714 		error = 0;
    715 	} else if (cnp->cn_flags & ISDOTDOT) {
    716 		struct ntvattr *vap;
    717 
    718 		dprintf(("ntfs_lookup: faking .. directory in %llu\n",
    719 		    (unsigned long long)dip->i_number));
    720 
    721 		VOP_UNLOCK(dvp, 0);
    722 		error = ntfs_ntvattrget(ntmp, dip, NTFS_A_NAME, NULL, 0, &vap);
    723 		if (error) {
    724 			vn_lock(dvp, LK_EXCLUSIVE | LK_RETRY);
    725 			return (error);
    726 		}
    727 
    728 		dprintf(("ntfs_lookup: parentdir: %d\n",
    729 			 vap->va_a_name->n_pnumber));
    730 		error = VFS_VGET(ntmp->ntm_mountp,
    731 				 vap->va_a_name->n_pnumber,ap->a_vpp);
    732 		ntfs_ntvattrrele(vap);
    733 		vn_lock(dvp, LK_EXCLUSIVE | LK_RETRY);
    734 		if (error) {
    735 			return (error);
    736 		}
    737 	} else {
    738 		error = ntfs_ntlookupfile(ntmp, dvp, cnp, ap->a_vpp);
    739 		if (error) {
    740 			dprintf(("ntfs_ntlookupfile: returned %d\n", error));
    741 			return (error);
    742 		}
    743 
    744 		dprintf(("ntfs_lookup: found ino: %llu\n",
    745 		    (unsigned long long)VTONT(*ap->a_vpp)->i_number));
    746 	}
    747 
    748 	if (cnp->cn_flags & MAKEENTRY)
    749 		cache_enter(dvp, *ap->a_vpp, cnp);
    750 
    751 	return (error);
    752 }
    753 
    754 /*
    755  * Flush the blocks of a file to disk.
    756  *
    757  * This function is worthless for vnodes that represent directories. Maybe we
    758  * could just do a sync if they try an fsync on a directory file.
    759  */
    760 static int
    761 ntfs_fsync(void *v)
    762 {
    763 	struct vop_fsync_args /* {
    764 		struct vnode *a_vp;
    765 		kauth_cred_t a_cred;
    766 		int a_flags;
    767 		off_t offlo;
    768 		off_t offhi;
    769 	} */ *ap = v;
    770 	struct vnode *vp = ap->a_vp;
    771 	int wait;
    772 
    773 	if (ap->a_flags & FSYNC_CACHE) {
    774 		return EOPNOTSUPP;
    775 	}
    776 
    777 	wait = (ap->a_flags & FSYNC_WAIT) != 0;
    778 	vflushbuf(vp, wait);
    779 
    780 	return 0;
    781 }
    782 
    783 /*
    784  * Return POSIX pathconf information applicable to NTFS filesystem
    785  */
    786 static int
    787 ntfs_pathconf(void *v)
    788 {
    789 	struct vop_pathconf_args /* {
    790 		struct vnode *a_vp;
    791 		int a_name;
    792 		register_t *a_retval;
    793 	} */ *ap = v;
    794 
    795 	switch (ap->a_name) {
    796 	case _PC_LINK_MAX:
    797 		*ap->a_retval = 1;
    798 		return (0);
    799 	case _PC_NAME_MAX:
    800 		*ap->a_retval = ap->a_vp->v_mount->mnt_stat.f_namemax;
    801 		return (0);
    802 	case _PC_PATH_MAX:
    803 		*ap->a_retval = PATH_MAX;
    804 		return (0);
    805 	case _PC_CHOWN_RESTRICTED:
    806 		*ap->a_retval = 1;
    807 		return (0);
    808 	case _PC_NO_TRUNC:
    809 		*ap->a_retval = 0;
    810 		return (0);
    811 	case _PC_SYNC_IO:
    812 		*ap->a_retval = 1;
    813 		return (0);
    814 	case _PC_FILESIZEBITS:
    815 		*ap->a_retval = 64;
    816 		return (0);
    817 	default:
    818 		return (EINVAL);
    819 	}
    820 	/* NOTREACHED */
    821 }
    822 
    823 /*
    824  * Global vfs data structures
    825  */
    826 vop_t **ntfs_vnodeop_p;
    827 
    828 const struct vnodeopv_entry_desc ntfs_vnodeop_entries[] = {
    829 	{ &vop_default_desc, (vop_t *) ntfs_bypass },
    830 	{ &vop_lookup_desc, (vop_t *) ntfs_lookup },	/* lookup */
    831 	{ &vop_create_desc, genfs_eopnotsupp },		/* create */
    832 	{ &vop_mknod_desc, genfs_eopnotsupp },		/* mknod */
    833 	{ &vop_open_desc, (vop_t *) ntfs_open },	/* open */
    834 	{ &vop_close_desc,(vop_t *)  ntfs_close },	/* close */
    835 	{ &vop_access_desc, (vop_t *) ntfs_access },	/* access */
    836 	{ &vop_getattr_desc, (vop_t *) ntfs_getattr },	/* getattr */
    837 	{ &vop_setattr_desc, genfs_eopnotsupp },	/* setattr */
    838 	{ &vop_read_desc, (vop_t *) ntfs_read },	/* read */
    839 	{ &vop_write_desc, (vop_t *) ntfs_write },	/* write */
    840 	{ &vop_fcntl_desc, genfs_fcntl },		/* fcntl */
    841 	{ &vop_ioctl_desc, genfs_enoioctl },		/* ioctl */
    842 	{ &vop_poll_desc, genfs_poll },			/* poll */
    843 	{ &vop_kqfilter_desc, genfs_kqfilter },		/* kqfilter */
    844 	{ &vop_revoke_desc, genfs_revoke },		/* revoke */
    845 	{ &vop_mmap_desc, genfs_mmap },			/* mmap */
    846 	{ &vop_fsync_desc, (vop_t *) ntfs_fsync },	/* fsync */
    847 	{ &vop_seek_desc, genfs_seek },			/* seek */
    848 	{ &vop_remove_desc, genfs_eopnotsupp },		/* remove */
    849 	{ &vop_link_desc, genfs_eopnotsupp },		/* link */
    850 	{ &vop_rename_desc, genfs_eopnotsupp },		/* rename */
    851 	{ &vop_mkdir_desc, genfs_eopnotsupp },		/* mkdir */
    852 	{ &vop_rmdir_desc, genfs_eopnotsupp },		/* rmdir */
    853 	{ &vop_symlink_desc, genfs_eopnotsupp },	/* symlink */
    854 	{ &vop_readdir_desc, (vop_t *) ntfs_readdir },	/* readdir */
    855 	{ &vop_readlink_desc, genfs_eopnotsupp },	/* readlink */
    856 	{ &vop_abortop_desc, genfs_abortop },		/* abortop */
    857 	{ &vop_inactive_desc, (vop_t *) ntfs_inactive },	/* inactive */
    858 	{ &vop_reclaim_desc, (vop_t *) ntfs_reclaim },	/* reclaim */
    859 	{ &vop_lock_desc, genfs_lock },			/* lock */
    860 	{ &vop_unlock_desc, genfs_unlock },		/* unlock */
    861 	{ &vop_bmap_desc, (vop_t *) ntfs_bmap },	/* bmap */
    862 	{ &vop_strategy_desc, (vop_t *) ntfs_strategy },	/* strategy */
    863 	{ &vop_print_desc, (vop_t *) ntfs_print },	/* print */
    864 	{ &vop_islocked_desc, genfs_islocked },		/* islocked */
    865 	{ &vop_pathconf_desc, ntfs_pathconf },		/* pathconf */
    866 	{ &vop_advlock_desc, genfs_nullop },		/* advlock */
    867 	{ &vop_bwrite_desc, vn_bwrite },		/* bwrite */
    868 	{ &vop_getpages_desc, genfs_compat_getpages },	/* getpages */
    869 	{ &vop_putpages_desc, genfs_putpages },		/* putpages */
    870 	{ NULL, NULL }
    871 };
    872 const struct vnodeopv_desc ntfs_vnodeop_opv_desc =
    873 	{ &ntfs_vnodeop_p, ntfs_vnodeop_entries };
    874