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