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