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