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