xref: /dragonfly/sys/vfs/ntfs/ntfs_vnops.c (revision 6c2b3e4e)
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. 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  * $FreeBSD: src/sys/ntfs/ntfs_vnops.c,v 1.9.2.4 2002/08/06 19:35:18 semenu Exp $
35  *
36  */
37 
38 #include <sys/param.h>
39 #include <sys/systm.h>
40 #include <sys/kernel.h>
41 #include <sys/time.h>
42 #include <sys/types.h>
43 #include <sys/stat.h>
44 #include <sys/vnode.h>
45 #include <sys/mount.h>
46 #include <sys/proc.h>
47 #include <sys/namei.h>
48 #include <sys/malloc.h>
49 #include <sys/buf.h>
50 #include <sys/dirent.h>
51 #include <machine/limits.h>
52 
53 #include <vm/vm.h>
54 #include <vm/vm_param.h>
55 #if defined(__NetBSD__)
56 #include <vm/vm_prot.h>
57 #endif
58 #include <vm/vm_page.h>
59 #include <vm/vm_object.h>
60 #include <vm/vm_pager.h>
61 #if defined(__DragonFly__)
62 #include <vm/vnode_pager.h>
63 #endif
64 #include <vm/vm_extern.h>
65 
66 #include <sys/sysctl.h>
67 
68 #include <sys/buf2.h>
69 
70 #include "ntfs.h"
71 #include "ntfs_inode.h"
72 #include "ntfs_subr.h"
73 #if defined(__NetBSD__)
74 #include <miscfs/specfs/specdev.h>
75 #include <miscfs/genfs/genfs.h>
76 #endif
77 
78 #include <sys/unistd.h> /* for pathconf(2) constants */
79 
80 static int	ntfs_read (struct vop_read_args *);
81 static int	ntfs_write (struct vop_write_args *ap);
82 static int	ntfs_getattr (struct vop_getattr_args *ap);
83 static int	ntfs_inactive (struct vop_inactive_args *ap);
84 static int	ntfs_print (struct vop_print_args *ap);
85 static int	ntfs_reclaim (struct vop_reclaim_args *ap);
86 static int	ntfs_strategy (struct vop_strategy_args *ap);
87 static int	ntfs_access (struct vop_access_args *ap);
88 static int	ntfs_open (struct vop_open_args *ap);
89 static int	ntfs_close (struct vop_close_args *ap);
90 static int	ntfs_readdir (struct vop_readdir_args *ap);
91 static int	ntfs_lookup (struct vop_old_lookup_args *ap);
92 static int	ntfs_bmap (struct vop_bmap_args *ap);
93 #if defined(__DragonFly__)
94 static int	ntfs_fsync (struct vop_fsync_args *ap);
95 #else
96 static int	ntfs_bypass (struct vop_generic_args *);
97 #endif
98 static int	ntfs_pathconf (struct vop_pathconf_args *);
99 
100 int	ntfs_prtactive = 1;	/* 1 => print out reclaim of active vnodes */
101 
102 /*
103  * This is a noop, simply returning what one has been given.
104  *
105  * ntfs_bmap(struct vnode *a_vp, off_t a_loffset,
106  *	     daddr_t *a_doffsetp, int *a_runp, int *a_runb)
107  */
108 int
109 ntfs_bmap(struct vop_bmap_args *ap)
110 {
111 	dprintf(("ntfs_bmap: vn: %p, blk: %u\n", ap->a_vp,
112 		(u_int32_t)ap->a_loffset));
113 	if (ap->a_doffsetp != NULL)
114 		*ap->a_doffsetp = ap->a_loffset;
115 	if (ap->a_runp != NULL)
116 		*ap->a_runp = 0;
117 #if !defined(__NetBSD__)
118 	if (ap->a_runb != NULL)
119 		*ap->a_runb = 0;
120 #endif
121 	return (0);
122 }
123 
124 /*
125  * ntfs_read(struct vnode *a_vp, struct uio *a_uio, int a_ioflag,
126  *	     struct ucred *a_cred)
127  */
128 static int
129 ntfs_read(struct vop_read_args *ap)
130 {
131 	struct vnode *vp = ap->a_vp;
132 	struct fnode *fp = VTOF(vp);
133 	struct ntnode *ip = FTONT(fp);
134 	struct uio *uio = ap->a_uio;
135 	struct ntfsmount *ntmp = ip->i_mp;
136 	struct buf *bp;
137 	daddr_t cn;
138 	int resid, off, toread;
139 	int error;
140 
141 	dprintf(("ntfs_read: ino: %ju, off: %u resid: %zd, segflg: %d\n",
142 		(uintmax_t)ip->i_number, (uint32_t)uio->uio_offset,
143 		uio->uio_resid, uio->uio_segflg));
144 
145 	dprintf(("ntfs_read: filesize: %ju", (uintmax_t)fp->f_size));
146 
147 	/* don't allow reading after end of file */
148 	if (uio->uio_offset > fp->f_size)
149 		return (0);
150 
151 	resid = (int)szmin(uio->uio_resid, fp->f_size - uio->uio_offset);
152 
153 	dprintf((", resid: %d\n", resid));
154 
155 	error = 0;
156 	while (resid) {
157 		cn = ntfs_btocn(uio->uio_offset);
158 		off = ntfs_btocnoff(uio->uio_offset);
159 
160 		toread = min(off + resid, ntfs_cntob(1));
161 
162 		error = bread(vp, ntfs_cntodoff(cn), ntfs_cntob(1), &bp);
163 		if (error) {
164 			brelse(bp);
165 			break;
166 		}
167 
168 		error = uiomovebp(bp, bp->b_data + off, toread - off, uio);
169 		if(error) {
170 			brelse(bp);
171 			break;
172 		}
173 		brelse(bp);
174 
175 		resid -= toread - off;
176 	}
177 
178 	return (error);
179 }
180 
181 #if !defined(__DragonFly__)
182 
183 /*
184  * ntfs_bypass()
185  */
186 static int
187 ntfs_bypass(struct vop_generic_args *ap)
188 {
189 	int error = ENOTTY;
190 	dprintf(("ntfs_bypass: %s\n", ap->a_desc->sd_name));
191 	return (error);
192 }
193 
194 #endif
195 
196 /*
197  * ntfs_getattr(struct vnode *a_vp, struct vattr *a_vap)
198  */
199 static int
200 ntfs_getattr(struct vop_getattr_args *ap)
201 {
202 	struct vnode *vp = ap->a_vp;
203 	struct fnode *fp = VTOF(vp);
204 	struct ntnode *ip = FTONT(fp);
205 	struct vattr *vap = ap->a_vap;
206 
207 	dprintf(("ntfs_getattr: %ju, flags: %d\n", (uintmax_t)ip->i_number,
208 		ip->i_flag));
209 
210 #if defined(__DragonFly__)
211 	vap->va_fsid = dev2udev(ip->i_dev);
212 #else /* NetBSD */
213 	vap->va_fsid = ip->i_dev;
214 #endif
215 	vap->va_fileid = ip->i_number;
216 	vap->va_mode = ip->i_mp->ntm_mode;
217 	vap->va_nlink = ip->i_nlink;
218 	vap->va_uid = ip->i_mp->ntm_uid;
219 	vap->va_gid = ip->i_mp->ntm_gid;
220 	vap->va_rmajor = VNOVAL;
221 	vap->va_rminor = VNOVAL;
222 	vap->va_size = fp->f_size;
223 	vap->va_bytes = fp->f_allocated;
224 	vap->va_atime = ntfs_nttimetounix(fp->f_times.t_access);
225 	vap->va_mtime = ntfs_nttimetounix(fp->f_times.t_write);
226 	vap->va_ctime = ntfs_nttimetounix(fp->f_times.t_create);
227 	vap->va_flags = ip->i_flag;
228 	vap->va_gen = 0;
229 	vap->va_blocksize = ip->i_mp->ntm_spc * ip->i_mp->ntm_bps;
230 	vap->va_type = vp->v_type;
231 	vap->va_filerev = 0;
232 	return (0);
233 }
234 
235 
236 /*
237  * Last reference to an ntnode.  If necessary, write or delete it.
238  *
239  * ntfs_inactive(struct vnode *a_vp)
240  */
241 int
242 ntfs_inactive(struct vop_inactive_args *ap)
243 {
244 	struct vnode *vp = ap->a_vp;
245 #ifdef NTFS_DEBUG
246 	struct ntnode *ip = VTONT(vp);
247 #endif
248 
249 	dprintf(("ntfs_inactive: vnode: %p, ntnode: %ju\n", vp, (uintmax_t)ip->i_number));
250 
251 	if (ntfs_prtactive && VREFCNT(vp) > 1)
252 		vprint("ntfs_inactive: pushing active", vp);
253 
254 	/*
255 	 * XXX since we don't support any filesystem changes
256 	 * right now, nothing more needs to be done
257 	 */
258 	return (0);
259 }
260 
261 /*
262  * Reclaim an fnode/ntnode so that it can be used for other purposes.
263  *
264  * ntfs_reclaim(struct vnode *a_vp)
265  */
266 int
267 ntfs_reclaim(struct vop_reclaim_args *ap)
268 {
269 	struct vnode *vp = ap->a_vp;
270 	struct fnode *fp = VTOF(vp);
271 	struct ntnode *ip = FTONT(fp);
272 	int error;
273 
274 	dprintf(("ntfs_reclaim: vnode: %p, ntnode: %ju\n", vp, (uintmax_t)ip->i_number));
275 
276 	if (ntfs_prtactive && VREFCNT(vp) > 1)
277 		vprint("ntfs_reclaim: pushing active", vp);
278 
279 	if ((error = ntfs_ntget(ip)) != 0)
280 		return (error);
281 
282 	ntfs_frele(fp);
283 	ntfs_ntput(ip);
284 	vp->v_data = NULL;
285 
286 	return (0);
287 }
288 
289 /*
290  * ntfs_print(struct vnode *a_vp)
291  */
292 static int
293 ntfs_print(struct vop_print_args *ap)
294 {
295 	return (0);
296 }
297 
298 /*
299  * Calculate the logical to physical mapping if not done already,
300  * then call the device strategy routine.
301  *
302  * ntfs_strategy(struct vnode *a_vp, struct bio *a_bio)
303  */
304 int
305 ntfs_strategy(struct vop_strategy_args *ap)
306 {
307 	struct bio *bio = ap->a_bio;
308 	struct buf *bp = bio->bio_buf;
309 	struct vnode *vp = ap->a_vp;
310 	struct fnode *fp = VTOF(vp);
311 	struct ntnode *ip = FTONT(fp);
312 	struct ntfsmount *ntmp = ip->i_mp;
313 	u_int32_t toread;
314 	u_int32_t towrite;
315 	size_t tmp;
316 	int error;
317 
318 	dprintf(("ntfs_strategy: loffset: %u, doffset: %u\n",
319 		(uint32_t)bp->b_loffset, (uint32_t)bio->bio_offset));
320 
321 	dprintf(("strategy: bcount: %u flags: 0x%x\n",
322 		bp->b_bcount, bp->b_flags));
323 
324 	bp->b_error = 0;
325 
326 	switch(bp->b_cmd) {
327 	case BUF_CMD_READ:
328 		if (bio->bio_offset >= fp->f_size) {
329 			clrbuf(bp);
330 			error = 0;
331 		} else {
332 			toread = min(bp->b_bcount,
333 				 fp->f_size - bio->bio_offset);
334 			dprintf(("ntfs_strategy: toread: %u, fsize: %ju\n",
335 				toread, (uintmax_t)fp->f_size));
336 
337 			error = ntfs_readattr(ntmp, ip, fp->f_attrtype,
338 				fp->f_attrname, bio->bio_offset,
339 				toread, bp->b_data, NULL);
340 
341 			if (error) {
342 				kprintf("ntfs_strategy: ntfs_readattr failed\n");
343 				bp->b_error = error;
344 				bp->b_flags |= B_ERROR;
345 			}
346 
347 			bzero(bp->b_data + toread, bp->b_bcount - toread);
348 		}
349 		break;
350 	case BUF_CMD_WRITE:
351 		if (bio->bio_offset + bp->b_bcount >= fp->f_size) {
352 			kprintf("ntfs_strategy: CAN'T EXTEND FILE\n");
353 			bp->b_error = error = EFBIG;
354 			bp->b_flags |= B_ERROR;
355 		} else {
356 			towrite = min(bp->b_bcount,
357 				      fp->f_size - bio->bio_offset);
358 			dprintf(("ntfs_strategy: towrite: %d, fsize: %ju\n",
359 				towrite, (uintmax_t)fp->f_size));
360 
361 			error = ntfs_writeattr_plain(ntmp, ip, fp->f_attrtype,
362 				fp->f_attrname, bio->bio_offset,towrite,
363 				bp->b_data, &tmp, NULL);
364 
365 			if (error) {
366 				kprintf("ntfs_strategy: ntfs_writeattr fail\n");
367 				bp->b_error = error;
368 				bp->b_flags |= B_ERROR;
369 			}
370 		}
371 		break;
372 	default:
373 		panic("ntfs: bad b_cmd %d", bp->b_cmd);
374 	}
375 	biodone(bio);
376 	return (error);
377 }
378 
379 /*
380  * ntfs_write(struct vnode *a_vp, struct uio *a_uio, int a_ioflag,
381  *	      struct ucred *a_cred)
382  */
383 static int
384 ntfs_write(struct vop_write_args *ap)
385 {
386 	struct vnode *vp = ap->a_vp;
387 	struct fnode *fp = VTOF(vp);
388 	struct ntnode *ip = FTONT(fp);
389 	struct uio *uio = ap->a_uio;
390 	struct ntfsmount *ntmp = ip->i_mp;
391 	size_t towrite;
392 	size_t written;
393 	int error;
394 
395 	dprintf(("ntfs_write: ino: %ju, off: %u resid: %zd, segflg: %d\n",
396 		(uintmax_t)ip->i_number, (uint32_t)uio->uio_offset,
397 		uio->uio_resid, uio->uio_segflg));
398 	dprintf(("ntfs_write: filesize: %ju ", (uintmax_t)fp->f_size));
399 
400 	if (uio->uio_resid + uio->uio_offset > fp->f_size) {
401 		kprintf("ntfs_write: CAN'T WRITE BEYOND END OF FILE\n");
402 		return (EFBIG);
403 	}
404 	if (uio->uio_offset > fp->f_size)
405 		return (EFBIG);
406 
407 	towrite = szmin(uio->uio_resid, fp->f_size - uio->uio_offset);
408 
409 	dprintf((", towrite: %zd\n", towrite));
410 
411 	error = ntfs_writeattr_plain(ntmp, ip, fp->f_attrtype,
412 		fp->f_attrname, uio->uio_offset, towrite, NULL, &written, uio);
413 #ifdef NTFS_DEBUG
414 	if (error)
415 		kprintf("ntfs_write: ntfs_writeattr failed: %d\n", error);
416 #endif
417 
418 	return (error);
419 }
420 
421 /*
422  * ntfs_access(struct vnode *a_vp, int a_mode, struct ucred *a_cred)
423  */
424 int
425 ntfs_access(struct vop_access_args *ap)
426 {
427 	struct vnode *vp = ap->a_vp;
428 	struct ntnode *ip = VTONT(vp);
429 	struct ucred *cred = ap->a_cred;
430 	mode_t mask, mode = ap->a_mode;
431 	gid_t *gp;
432 	int i;
433 #ifdef QUOTA
434 	int error;
435 #endif
436 
437 	dprintf(("ntfs_access: %ju\n", (uintmax_t)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 #ifdef QUOTA
452 			if (error = getinoquota(ip))
453 				return (error);
454 #endif
455 			break;
456 		}
457 	}
458 
459 	/* Otherwise, user id 0 always gets access. */
460 	if (cred->cr_uid == 0)
461 		return (0);
462 
463 	mask = 0;
464 
465 	/* Otherwise, check the owner. */
466 	if (cred->cr_uid == ip->i_mp->ntm_uid) {
467 		if (mode & VEXEC)
468 			mask |= S_IXUSR;
469 		if (mode & VREAD)
470 			mask |= S_IRUSR;
471 		if (mode & VWRITE)
472 			mask |= S_IWUSR;
473 		return ((ip->i_mp->ntm_mode & mask) == mask ? 0 : EACCES);
474 	}
475 
476 	/* Otherwise, check the groups. */
477 	for (i = 0, gp = cred->cr_groups; i < cred->cr_ngroups; i++, gp++)
478 		if (ip->i_mp->ntm_gid == *gp) {
479 			if (mode & VEXEC)
480 				mask |= S_IXGRP;
481 			if (mode & VREAD)
482 				mask |= S_IRGRP;
483 			if (mode & VWRITE)
484 				mask |= S_IWGRP;
485 			return ((ip->i_mp->ntm_mode&mask) == mask ? 0 : EACCES);
486 		}
487 
488 	/* Otherwise, check everyone else. */
489 	if (mode & VEXEC)
490 		mask |= S_IXOTH;
491 	if (mode & VREAD)
492 		mask |= S_IROTH;
493 	if (mode & VWRITE)
494 		mask |= S_IWOTH;
495 	return ((ip->i_mp->ntm_mode & mask) == mask ? 0 : EACCES);
496 }
497 
498 /*
499  * Open called.
500  *
501  * Nothing to do.
502  *
503  * ntfs_open(struct vnode *a_vp, int a_mode, struct ucred *a_cred,
504  *	     struct file *a_fp)
505  */
506 /* ARGSUSED */
507 static int
508 ntfs_open(struct vop_open_args *ap)
509 {
510 	return (vop_stdopen(ap));
511 }
512 
513 /*
514  * Close called.
515  *
516  * Update the times on the inode.
517  *
518  * ntfs_close(struct vnode *a_vp, int a_fflag)
519  */
520 /* ARGSUSED */
521 static int
522 ntfs_close(struct vop_close_args *ap)
523 {
524 #if NTFS_DEBUG
525 	struct vnode *vp = ap->a_vp;
526 	struct ntnode *ip = VTONT(vp);
527 
528 	kprintf("ntfs_close: %ju\n", (uintmax_t)ip->i_number);
529 #endif
530 
531 	return (vop_stdclose(ap));
532 }
533 
534 /*
535  * ntfs_readdir(struct vnode *a_vp, struct uio *a_uio, struct ucred *a_cred,
536  *		int *a_ncookies, off_t **cookies)
537  */
538 int
539 ntfs_readdir(struct vop_readdir_args *ap)
540 {
541 	struct vnode *vp = ap->a_vp;
542 	struct fnode *fp = VTOF(vp);
543 	struct ntnode *ip = FTONT(fp);
544 	struct uio *uio = ap->a_uio;
545 	struct ntfsmount *ntmp = ip->i_mp;
546 	int i, j, error = 0;
547 	wchar c;
548 	u_int32_t faked = 0, num, off;
549 	int ncookies = 0;
550 	char convname[NTFS_MAXFILENAME + 1];
551 
552 	dprintf(("ntfs_readdir %ju off: %u resid: %zd\n",
553 		(uintmax_t)ip->i_number, (uint32_t)uio->uio_offset,
554 		uio->uio_resid));
555 
556 	if (uio->uio_offset < 0 || uio->uio_offset > INT_MAX)
557 		return (EINVAL);
558 	error = vn_lock(vp, LK_EXCLUSIVE | LK_RETRY | LK_FAILRECLAIM);
559 	if (error)
560 		return (error);
561 
562 	/*
563 	 * uio->uio_offset carries the number of the entry
564 	 * where we should start returning dirents.
565 	 *
566 	 * We fake up "." if we're not reading the FS root
567 	 * and we always fake up "..".
568 	 *
569 	 * off contains the entry we are starting at,
570 	 * num increments while we are reading.
571 	 */
572 
573 	off = num = uio->uio_offset;
574 	faked = (ip->i_number == NTFS_ROOTINO) ? 1 : 2;
575 
576 	/* Simulate . in every dir except ROOT */
577 	if (ip->i_number != NTFS_ROOTINO && num == 0) {
578 		if (vop_write_dirent(&error, uio, ip->i_number,
579 		    DT_DIR, 1, "."))
580 			goto done;
581 		if (error)
582 			goto done;
583 
584 		num++;
585 		ncookies++;
586 	}
587 
588 	/* Simulate .. in every dir including ROOT */
589 	if (num == faked - 1) {
590 		/* XXX NTFS_ROOTINO seems to be wrong here */
591 		if (vop_write_dirent(&error, uio, NTFS_ROOTINO,
592 		    DT_DIR, 2, ".."))
593 			goto readdone;
594 		if (error)
595 			goto done;
596 
597 		num++;
598 		ncookies++;
599 	}
600 
601 	for (;;) {
602 		struct attr_indexentry *iep;
603 
604 		/*
605 		 * num is the number of the entry we will return,
606 		 * but ntfs_ntreaddir takes the entry number of the
607 		 * ntfs directory listing, so subtract the faked
608 		 * . and .. entries.
609 		 */
610 		error = ntfs_ntreaddir(ntmp, fp, num - faked, &iep);
611 
612 		if (error)
613 			goto done;
614 
615 		if( NULL == iep )
616 			break;
617 
618 		for (; !(iep->ie_flag & NTFS_IEFLAG_LAST);
619 			iep = NTFS_NEXTREC(iep, struct attr_indexentry *))
620 		{
621 			if(!ntfs_isnamepermitted(ntmp,iep))
622 				continue;
623 			for(i=0, j=0; i < iep->ie_fnamelen; i++, j++) {
624 				c = NTFS_U28(iep->ie_fname[i]);
625 				if (c&0xFF00)
626 					convname[j++] = (char)(c>>8);
627 				convname[j] = (char)c&0xFF;
628 			}
629 			convname[j] = '\0';
630 			if (vop_write_dirent(&error, uio, iep->ie_number,
631 			    (iep->ie_fflag & NTFS_FFLAG_DIR) ? DT_DIR : DT_REG,
632 			    j, convname))
633 				goto readdone;
634 
635 			dprintf(("ntfs_readdir: elem: %d, fname:[%s] type: %d, "
636 				 "flag: %d, %s\n",
637 				 ncookies, convname, iep->ie_fnametype,
638 				 iep->ie_flag,
639 				 (iep->ie_fflag & NTFS_FFLAG_DIR) ?
640 					"dir" : "reg"));
641 
642 			if (error)
643 				goto done;
644 
645 			ncookies++;
646 			num++;
647 		}
648 	}
649 
650 readdone:
651 	uio->uio_offset = num;
652 
653 	dprintf(("ntfs_readdir: %d entries (%d bytes) read\n",
654 		ncookies,(u_int)(uio->uio_offset - off)));
655 	dprintf(("ntfs_readdir: off: %u resid: %zd\n",
656 		(uint32_t)uio->uio_offset, uio->uio_resid));
657 
658 	if (!error && ap->a_ncookies != NULL) {
659 		off_t *cookies;
660 		off_t *cookiep;
661 
662 		ddprintf(("ntfs_readdir: %d cookies\n",ncookies));
663 		if (uio->uio_segflg != UIO_SYSSPACE || uio->uio_iovcnt != 1)
664 			panic("ntfs_readdir: unexpected uio from NFS server");
665 		cookies = kmalloc(ncookies * sizeof(off_t), M_TEMP, M_WAITOK);
666 		cookiep = cookies;
667 		while (off < num)
668 			*cookiep++ = ++off;
669 
670 		*ap->a_ncookies = ncookies;
671 		*ap->a_cookies = cookies;
672 	}
673 /*
674 	if (ap->a_eofflag)
675 	    *ap->a_eofflag = VTONT(vp)->i_size <= uio->uio_offset;
676 */
677 done:
678 	vn_unlock(vp);
679 	return (error);
680 }
681 
682 /*
683  * ntfs_lookup(struct vnode *a_dvp, struct vnode **a_vpp,
684  *		struct componentname *a_cnp)
685  */
686 int
687 ntfs_lookup(struct vop_old_lookup_args *ap)
688 {
689 	struct vnode *dvp = ap->a_dvp;
690 	struct ntnode *dip = VTONT(dvp);
691 	struct ntfsmount *ntmp = dip->i_mp;
692 	struct componentname *cnp = ap->a_cnp;
693 	int error;
694 	int lockparent = cnp->cn_flags & CNP_LOCKPARENT;
695 #if NTFS_DEBUG
696 	int wantparent = cnp->cn_flags & (CNP_LOCKPARENT | CNP_WANTPARENT);
697 #endif
698 	dprintf(("ntfs_lookup: \"%.*s\" (%ld bytes) in %ju, lp: %d, wp: %d \n",
699 		(int)cnp->cn_namelen, cnp->cn_nameptr, cnp->cn_namelen,
700 		(uintmax_t)dip->i_number, lockparent, wantparent));
701 
702 	*ap->a_vpp = NULL;
703 
704 	if (cnp->cn_namelen == 1 && cnp->cn_nameptr[0] == '.') {
705 		dprintf(("ntfs_lookup: faking . directory in %u\n",
706 			(uint32_t)dip->i_number));
707 
708 		vref(dvp);
709 		*ap->a_vpp = dvp;
710 		error = 0;
711 	} else if (cnp->cn_flags & CNP_ISDOTDOT) {
712 		struct ntvattr *vap;
713 
714 		dprintf(("ntfs_lookup: faking .. directory in %d\n",
715 			(uint32_t)dip->i_number));
716 
717 		error = ntfs_ntvattrget(ntmp, dip, NTFS_A_NAME, NULL, 0, &vap);
718 		if(error)
719 			return (error);
720 
721 		VOP__UNLOCK(dvp, 0);
722 		cnp->cn_flags |= CNP_PDIRUNLOCK;
723 
724 		dprintf(("ntfs_lookup: parentdir: %d\n",
725 			 vap->va_a_name->n_pnumber));
726 		error = VFS_VGET(ntmp->ntm_mountp, NULL,
727 				 vap->va_a_name->n_pnumber,ap->a_vpp);
728 		ntfs_ntvattrrele(vap);
729 		if (error) {
730 			if (VOP_LOCK(dvp, LK_EXCLUSIVE | LK_RETRY) == 0)
731 				cnp->cn_flags &= ~CNP_PDIRUNLOCK;
732 			return (error);
733 		}
734 
735 		if (lockparent) {
736 			error = VOP_LOCK(dvp, LK_EXCLUSIVE);
737 			if (error) {
738 				vput(*ap->a_vpp);
739 				*ap->a_vpp = NULL;
740 				return (error);
741 			}
742 			cnp->cn_flags &= ~CNP_PDIRUNLOCK;
743 		}
744 	} else {
745 		error = ntfs_ntlookupfile(ntmp, dvp, cnp, ap->a_vpp);
746 		if (error) {
747 			dprintf(("ntfs_ntlookupfile: returned %d\n", error));
748 			return (error);
749 		}
750 
751 		dprintf(("ntfs_lookup: found ino: %u\n",
752 			(uint32_t)VTONT(*ap->a_vpp)->i_number));
753 
754 		if (!lockparent) {
755 			VOP__UNLOCK(dvp, 0);
756 			cnp->cn_flags |= CNP_PDIRUNLOCK;
757 		}
758 	}
759 	return (error);
760 }
761 
762 #if defined(__DragonFly__)
763 /*
764  * Flush the blocks of a file to disk.
765  *
766  * This function is worthless for vnodes that represent directories. Maybe we
767  * could just do a sync if they try an fsync on a directory file.
768  *
769  * ntfs_fsync(struct vnode *a_vp, int a_waitfor)
770  */
771 static int
772 ntfs_fsync(struct vop_fsync_args *ap)
773 {
774 	return (0);
775 }
776 #endif
777 
778 /*
779  * Return POSIX pathconf information applicable to NTFS filesystem
780  */
781 int
782 ntfs_pathconf(struct vop_pathconf_args *ap)
783 {
784 	switch (ap->a_name) {
785 	case _PC_LINK_MAX:
786 		*ap->a_retval = 1;
787 		return (0);
788 	case _PC_NAME_MAX:
789 		*ap->a_retval = NTFS_MAXFILENAME;
790 		return (0);
791 	case _PC_PATH_MAX:
792 		*ap->a_retval = PATH_MAX;
793 		return (0);
794 	case _PC_CHOWN_RESTRICTED:
795 		*ap->a_retval = 1;
796 		return (0);
797 	case _PC_NO_TRUNC:
798 		*ap->a_retval = 0;
799 		return (0);
800 #if defined(__NetBSD__)
801 	case _PC_SYNC_IO:
802 		*ap->a_retval = 1;
803 		return (0);
804 	case _PC_FILESIZEBITS:
805 		*ap->a_retval = 64;
806 		return (0);
807 #endif
808 	default:
809 		return (EINVAL);
810 	}
811 	/* NOTREACHED */
812 }
813 
814 /*
815  * Global vfs data structures
816  */
817 struct vop_ops ntfs_vnode_vops = {
818 	.vop_default =		vop_defaultop,
819 	.vop_getattr =		ntfs_getattr,
820 	.vop_inactive =		ntfs_inactive,
821 	.vop_reclaim =		ntfs_reclaim,
822 	.vop_print =		ntfs_print,
823 	.vop_pathconf =		ntfs_pathconf,
824 	.vop_old_lookup =	ntfs_lookup,
825 	.vop_access =		ntfs_access,
826 	.vop_close =		ntfs_close,
827 	.vop_open =		ntfs_open,
828 	.vop_readdir =		ntfs_readdir,
829 	.vop_fsync =		ntfs_fsync,
830 	.vop_bmap =		ntfs_bmap,
831 	.vop_getpages =		vop_stdgetpages,
832 	.vop_putpages =		vop_stdputpages,
833 	.vop_strategy =		ntfs_strategy,
834 	.vop_read =		ntfs_read,
835 	.vop_write =		ntfs_write
836 };
837 
838