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