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