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