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