xref: /openbsd-src/sys/ntfs/ntfs_vfsops.c (revision f2da64fbbbf1b03f09f390ab01267c93dfd77c4c)
1 /*	$OpenBSD: ntfs_vfsops.c,v 1.55 2016/09/07 17:30:12 natano Exp $	*/
2 /*	$NetBSD: ntfs_vfsops.c,v 1.7 2003/04/24 07:50:19 christos Exp $	*/
3 
4 /*-
5  * Copyright (c) 1998, 1999 Semen Ustimenko
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  *
29  *	Id: ntfs_vfsops.c,v 1.7 1999/05/31 11:28:30 phk Exp
30  */
31 
32 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/namei.h>
35 #include <sys/proc.h>
36 #include <sys/kernel.h>
37 #include <sys/vnode.h>
38 #include <sys/lock.h>
39 #include <sys/mount.h>
40 #include <sys/buf.h>
41 #include <sys/disk.h>
42 #include <sys/fcntl.h>
43 #include <sys/malloc.h>
44 #include <sys/device.h>
45 #include <sys/conf.h>
46 #include <sys/specdev.h>
47 
48 /*#define NTFS_DEBUG 1*/
49 #include <ntfs/ntfs.h>
50 #include <ntfs/ntfs_inode.h>
51 #include <ntfs/ntfs_subr.h>
52 #include <ntfs/ntfs_vfsops.h>
53 #include <ntfs/ntfs_ihash.h>
54 
55 int	ntfs_mount(struct mount *, const char *, void *,
56 				struct nameidata *, struct proc *);
57 int	ntfs_quotactl(struct mount *, int, uid_t, caddr_t,
58 				   struct proc *);
59 int	ntfs_root(struct mount *, struct vnode **);
60 int	ntfs_start(struct mount *, int, struct proc *);
61 int	ntfs_statfs(struct mount *, struct statfs *,
62 				 struct proc *);
63 int	ntfs_sync(struct mount *, int, struct ucred *,
64 			       struct proc *);
65 int	ntfs_unmount(struct mount *, int, struct proc *);
66 int	ntfs_vget(struct mount *mp, ino_t ino,
67 			       struct vnode **vpp);
68 int	ntfs_mountfs(struct vnode *, struct mount *,
69 				  struct ntfs_args *, struct proc *);
70 int	ntfs_vptofh(struct vnode *, struct fid *);
71 
72 int	ntfs_init(struct vfsconf *);
73 int	ntfs_fhtovp(struct mount *, struct fid *,
74    			     struct vnode **);
75 int	ntfs_checkexp(struct mount *, struct mbuf *,
76 			       int *, struct ucred **);
77 int	ntfs_sysctl(int *, u_int, void *, size_t *, void *,
78  			     size_t, struct proc *);
79 
80 /*
81  * Verify a remote client has export rights and return these rights via.
82  * exflagsp and credanonp.
83  */
84 int
85 ntfs_checkexp(struct mount *mp, struct mbuf *nam, int *exflagsp,
86     struct ucred **credanonp)
87 {
88 	struct netcred *np;
89 	struct ntfsmount *ntm = VFSTONTFS(mp);
90 
91 	/*
92 	 * Get the export permission structure for this <mp, client> tuple.
93 	 */
94 	np = vfs_export_lookup(mp, &ntm->ntm_export, nam);
95 	if (np == NULL)
96 		return (EACCES);
97 
98 	*exflagsp = np->netc_exflags;
99 	*credanonp = &np->netc_anon;
100 	return (0);
101 }
102 
103 int
104 ntfs_sysctl(int *name, u_int namelen, void *oldp, size_t *oldlenp, void *newp,
105     size_t newlen, struct proc *p)
106 {
107 	return (EINVAL);
108 }
109 
110 int
111 ntfs_init(struct vfsconf *vcp)
112 {
113 	return 0;
114 }
115 
116 int
117 ntfs_mount(struct mount *mp, const char *path, void *data,
118     struct nameidata *ndp, struct proc *p)
119 {
120 	int		err = 0;
121 	struct vnode	*devvp;
122 	struct ntfs_args args;
123 	char fname[MNAMELEN];
124 	char fspec[MNAMELEN];
125 
126 	ntfs_nthashinit();
127 
128 	/*
129 	 ***
130 	 * Mounting non-root file system or updating a file system
131 	 ***
132 	 */
133 
134 	/* copy in user arguments*/
135 	err = copyin(data, (caddr_t)&args, sizeof (struct ntfs_args));
136 	if (err)
137 		goto error_1;		/* can't get arguments*/
138 
139 	/*
140 	 * If updating, check whether changing from read-only to
141 	 * read/write; if there is no device name, that's all we do.
142 	 */
143 	if (mp->mnt_flag & MNT_UPDATE) {
144 		/* if not updating name...*/
145 		if (args.fspec == NULL) {
146 			/*
147 			 * Process export requests.  Jumping to "success"
148 			 * will return the vfs_export() error code.
149 			 */
150 			struct ntfsmount *ntm = VFSTONTFS(mp);
151 			err = vfs_export(mp, &ntm->ntm_export, &args.export_info);
152 			goto success;
153 		}
154 
155 		printf("ntfs_mount(): MNT_UPDATE not supported\n");
156 		err = EINVAL;
157 		goto error_1;
158 	}
159 
160 	/*
161 	 * Not an update, or updating the name: look up the name
162 	 * and verify that it refers to a sensible block device.
163 	 */
164 	err = copyinstr(args.fspec, fspec, sizeof(fspec), NULL);
165 	if (err)
166 		goto error_1;
167 
168 	if (disk_map(fspec, fname, sizeof(fname), DM_OPENBLCK) == -1)
169 		bcopy(fspec, fname, sizeof(fname));
170 
171 	NDINIT(ndp, LOOKUP, FOLLOW, UIO_SYSSPACE, fname, p);
172 	err = namei(ndp);
173 	if (err) {
174 		/* can't get devvp!*/
175 		goto error_1;
176 	}
177 
178 	devvp = ndp->ni_vp;
179 
180 	if (devvp->v_type != VBLK) {
181 		err = ENOTBLK;
182 		goto error_2;
183 	}
184 
185 	if (major(devvp->v_rdev) >= nblkdev) {
186 		err = ENXIO;
187 		goto error_2;
188 	}
189 
190 	if (mp->mnt_flag & MNT_UPDATE) {
191 #if 0
192 		/*
193 		 ********************
194 		 * UPDATE
195 		 ********************
196 		 */
197 
198 		if (devvp != ntmp->um_devvp)
199 			err = EINVAL;	/* needs translation */
200 		else
201 			vrele(devvp);
202 		/*
203 		 * Update device name only on success
204 		 */
205 		if( !err) {
206 			err = set_statfs_info(NULL, UIO_USERSPACE, args.fspec,
207 			    UIO_USERSPACE, mp, p);
208 		}
209 #endif
210 	} else {
211 		/*
212 		 ********************
213 		 * NEW MOUNT
214 		 ********************
215 		 */
216 
217 		/*
218 		 * Since this is a new mount, we want the names for
219 		 * the device and the mount point copied in.  If an
220 		 * error occurs,  the mountpoint is discarded by the
221 		 * upper level code.
222 		 */
223 		/* Save "last mounted on" info for mount point (NULL pad)*/
224 		bzero(mp->mnt_stat.f_mntonname, MNAMELEN);
225 		strlcpy(mp->mnt_stat.f_mntonname, path, MNAMELEN);
226 		bzero(mp->mnt_stat.f_mntfromname, MNAMELEN);
227 		strlcpy(mp->mnt_stat.f_mntfromname, fname, MNAMELEN);
228 		bzero(mp->mnt_stat.f_mntfromspec, MNAMELEN);
229 		strlcpy(mp->mnt_stat.f_mntfromspec, fspec, MNAMELEN);
230 		bcopy(&args, &mp->mnt_stat.mount_info.ntfs_args, sizeof(args));
231 		if ( !err) {
232 			err = ntfs_mountfs(devvp, mp, &args, p);
233 		}
234 	}
235 	if (err) {
236 		goto error_2;
237 	}
238 
239 	/*
240 	 * Initialize FS stat information in mount struct; uses both
241 	 * mp->mnt_stat.f_mntonname and mp->mnt_stat.f_mntfromname
242 	 *
243 	 * This code is common to root and non-root mounts
244 	 */
245 	(void)VFS_STATFS(mp, &mp->mnt_stat, p);
246 
247 	goto success;
248 
249 
250 error_2:	/* error with devvp held*/
251 
252 	/* release devvp before failing*/
253 	vrele(devvp);
254 
255 error_1:	/* no state to back out*/
256 
257 success:
258 	return(err);
259 }
260 
261 /*
262  * Common code for mount and mountroot
263  */
264 int
265 ntfs_mountfs(struct vnode *devvp, struct mount *mp, struct ntfs_args *argsp,
266     struct proc *p)
267 {
268 	struct buf *bp;
269 	struct ntfsmount *ntmp = NULL;
270 	dev_t dev = devvp->v_rdev;
271 	int error, ncount, i;
272 	struct vnode *vp;
273 
274 	/*
275 	 * Disallow multiple mounts of the same device.
276 	 * Disallow mounting of a device that is currently in use
277 	 * (except for root, which might share swap device for miniroot).
278 	 * Flush out any old buffers remaining from a previous use.
279 	 */
280 	error = vfs_mountedon(devvp);
281 	if (error)
282 		return (error);
283 	ncount = vcount(devvp);
284 	if (ncount > 1 && devvp != rootvp)
285 		return (EBUSY);
286 	vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY, p);
287 	error = vinvalbuf(devvp, V_SAVE, p->p_ucred, p, 0, 0);
288 	VOP_UNLOCK(devvp, p);
289 	if (error)
290 		return (error);
291 
292 	error = VOP_OPEN(devvp, FREAD, FSCRED, p);
293 	if (error)
294 		return (error);
295 
296 	bp = NULL;
297 
298 	error = bread(devvp, BBLOCK, BBSIZE, &bp);
299 	if (error)
300 		goto out;
301 	ntmp = malloc(sizeof *ntmp, M_NTFSMNT, M_WAITOK | M_ZERO);
302 	bcopy(bp->b_data, &ntmp->ntm_bootfile, sizeof(struct bootfile));
303 	brelse(bp);
304 	bp = NULL;
305 
306 	if (strncmp(ntmp->ntm_bootfile.bf_sysid, NTFS_BBID, NTFS_BBIDLEN)) {
307 		error = EINVAL;
308 		DPRINTF("ntfs_mountfs: invalid boot block\n");
309 		goto out;
310 	}
311 
312 	{
313 		int8_t cpr = ntmp->ntm_mftrecsz;
314 		if( cpr > 0 )
315 			ntmp->ntm_bpmftrec = ntmp->ntm_spc * cpr;
316 		else
317 			ntmp->ntm_bpmftrec = (1 << (-cpr)) / ntmp->ntm_bps;
318 	}
319 	DPRINTF("ntfs_mountfs(): bps: %u, spc: %u, media: %x, "
320 	    "mftrecsz: %u (%u sects)\n", ntmp->ntm_bps, ntmp->ntm_spc,
321 	    ntmp->ntm_bootfile.bf_media, ntmp->ntm_mftrecsz,
322 	    ntmp->ntm_bpmftrec);
323 	DPRINTF("ntfs_mountfs(): mftcn: 0x%llx|0x%llx\n",
324 	    ntmp->ntm_mftcn, ntmp->ntm_mftmirrcn);
325 
326 	ntmp->ntm_mountp = mp;
327 	ntmp->ntm_dev = dev;
328 	ntmp->ntm_devvp = devvp;
329 	ntmp->ntm_uid = argsp->uid;
330 	ntmp->ntm_gid = argsp->gid;
331 	ntmp->ntm_mode = argsp->mode;
332 	ntmp->ntm_flag = argsp->flag;
333 	mp->mnt_data = ntmp;
334 	TAILQ_INIT(&ntmp->ntm_ntnodeq);
335 
336 	/* set file name encode/decode hooks XXX utf-8 only for now */
337 	ntmp->ntm_wget = ntfs_utf8_wget;
338 	ntmp->ntm_wput = ntfs_utf8_wput;
339 	ntmp->ntm_wcmp = ntfs_utf8_wcmp;
340 
341 	DPRINTF("ntfs_mountfs(): case-%s,%s uid: %d, gid: %d, mode: %o\n",
342 	    (ntmp->ntm_flag & NTFS_MFLAG_CASEINS) ? "insens." : "sens.",
343 	    (ntmp->ntm_flag & NTFS_MFLAG_ALLNAMES) ? " allnames," : "",
344 	    ntmp->ntm_uid, ntmp->ntm_gid, ntmp->ntm_mode);
345 
346 	/*
347 	 * We read in some system nodes to do not allow
348 	 * reclaim them and to have everytime access to them.
349 	 */
350 	{
351 		int pi[3] = { NTFS_MFTINO, NTFS_ROOTINO, NTFS_BITMAPINO };
352 		for (i=0; i<3; i++) {
353 			error = VFS_VGET(mp, pi[i], &(ntmp->ntm_sysvn[pi[i]]));
354 			if(error)
355 				goto out1;
356 			ntmp->ntm_sysvn[pi[i]]->v_flag |= VSYSTEM;
357 			vref(ntmp->ntm_sysvn[pi[i]]);
358 			vput(ntmp->ntm_sysvn[pi[i]]);
359 		}
360 	}
361 
362 	/* read the Unicode lowercase --> uppercase translation table,
363 	 * if necessary */
364 	if ((error = ntfs_toupper_use(mp, ntmp, p)))
365 		goto out1;
366 
367 	/*
368 	 * Scan $BitMap and count free clusters
369 	 */
370 	error = ntfs_calccfree(ntmp, &ntmp->ntm_cfree);
371 	if(error)
372 		goto out1;
373 
374 	/*
375 	 * Read and translate to internal format attribute
376 	 * definition file.
377 	 */
378 	{
379 		int num,j;
380 		struct attrdef ad;
381 
382 		/* Open $AttrDef */
383 		error = VFS_VGET(mp, NTFS_ATTRDEFINO, &vp );
384 		if(error)
385 			goto out1;
386 
387 		/* Count valid entries */
388 		for(num = 0; ; num++) {
389 			error = ntfs_readattr(ntmp, VTONT(vp),
390 			    NTFS_A_DATA, NULL, num * sizeof(ad), sizeof(ad),
391 			    &ad, NULL);
392 			if (error)
393 				goto out1;
394 			if (ad.ad_name[0] == 0)
395 				break;
396 		}
397 
398 		/* Alloc memory for attribute definitions */
399 		ntmp->ntm_ad = mallocarray(num, sizeof(struct ntvattrdef),
400 		    M_NTFSMNT, M_WAITOK);
401 
402 		ntmp->ntm_adnum = num;
403 
404 		/* Read them and translate */
405 		for(i = 0; i < num; i++){
406 			error = ntfs_readattr(ntmp, VTONT(vp),
407 			    NTFS_A_DATA, NULL, i * sizeof(ad), sizeof(ad),
408 			    &ad, NULL);
409 			if (error)
410 				goto out1;
411 			j = 0;
412 			do {
413 				ntmp->ntm_ad[i].ad_name[j] = ad.ad_name[j];
414 			} while(ad.ad_name[j++]);
415 			ntmp->ntm_ad[i].ad_namelen = j - 1;
416 			ntmp->ntm_ad[i].ad_type = ad.ad_type;
417 		}
418 
419 		vput(vp);
420 	}
421 
422 	mp->mnt_stat.f_fsid.val[0] = dev;
423 	mp->mnt_stat.f_fsid.val[1] = mp->mnt_vfc->vfc_typenum;
424 	mp->mnt_stat.f_namemax = NTFS_MAXFILENAME;
425 	mp->mnt_flag |= MNT_LOCAL;
426 	devvp->v_specmountpoint = mp;
427 	return (0);
428 
429 out1:
430 	for (i = 0; i < NTFS_SYSNODESNUM; i++)
431 		if (ntmp->ntm_sysvn[i])
432 			vrele(ntmp->ntm_sysvn[i]);
433 
434 	if (vflush(mp,NULLVP,0))
435 		DPRINTF("ntfs_mountfs: vflush failed\n");
436 
437 out:
438 	if (devvp->v_specinfo)
439 		devvp->v_specmountpoint = NULL;
440 	if (bp)
441 		brelse(bp);
442 
443 	if (ntmp != NULL) {
444 		if (ntmp->ntm_ad != NULL)
445 			free(ntmp->ntm_ad, M_NTFSMNT, 0);
446 		free(ntmp, M_NTFSMNT, 0);
447 		mp->mnt_data = NULL;
448 	}
449 
450 	/* lock the device vnode before calling VOP_CLOSE() */
451 	vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY, p);
452 	(void)VOP_CLOSE(devvp, FREAD, NOCRED, p);
453 	VOP_UNLOCK(devvp, p);
454 
455 	return (error);
456 }
457 
458 int
459 ntfs_start(struct mount *mp, int flags, struct proc *p)
460 {
461 	return (0);
462 }
463 
464 int
465 ntfs_unmount(struct mount *mp, int mntflags, struct proc *p)
466 {
467 	struct ntfsmount *ntmp;
468 	int error, flags, i;
469 
470 	DPRINTF("ntfs_unmount: unmounting...\n");
471 	ntmp = VFSTONTFS(mp);
472 
473 	flags = 0;
474 	if(mntflags & MNT_FORCE)
475 		flags |= FORCECLOSE;
476 
477 	DPRINTF("ntfs_unmount: vflushing...\n");
478 	error = vflush(mp,NULLVP,flags | SKIPSYSTEM);
479 	if (error) {
480 		DPRINTF("ntfs_unmount: vflush failed: %d\n", error);
481 		return (error);
482 	}
483 
484 	/* Check if system vnodes are still referenced */
485 	for(i=0;i<NTFS_SYSNODESNUM;i++) {
486 		if(((mntflags & MNT_FORCE) == 0) && (ntmp->ntm_sysvn[i] &&
487 		    ntmp->ntm_sysvn[i]->v_usecount > 1))
488 			return (EBUSY);
489 	}
490 
491 	/* Dereference all system vnodes */
492 	for(i=0;i<NTFS_SYSNODESNUM;i++)
493 		 if(ntmp->ntm_sysvn[i]) vrele(ntmp->ntm_sysvn[i]);
494 
495 	/* vflush system vnodes */
496 	error = vflush(mp,NULLVP,flags);
497 	if (error) {
498 		/* XXX should this be panic() ? */
499 		printf("ntfs_unmount: vflush failed(sysnodes): %d\n",error);
500 	}
501 
502 	/* Check if the type of device node isn't VBAD before
503 	 * touching v_specinfo.  If the device vnode is revoked, the
504 	 * field is NULL and touching it causes null pointer derefercence.
505 	 */
506 	if (ntmp->ntm_devvp->v_type != VBAD)
507 		ntmp->ntm_devvp->v_specmountpoint = NULL;
508 
509 	/* lock the device vnode before calling VOP_CLOSE() */
510 	vn_lock(ntmp->ntm_devvp, LK_EXCLUSIVE | LK_RETRY, p);
511 	vinvalbuf(ntmp->ntm_devvp, V_SAVE, NOCRED, p, 0, 0);
512 	(void)VOP_CLOSE(ntmp->ntm_devvp, FREAD, NOCRED, p);
513 	vput(ntmp->ntm_devvp);
514 
515 	/* free the toupper table, if this has been last mounted ntfs volume */
516 	ntfs_toupper_unuse(p);
517 
518 	DPRINTF("ntfs_unmount: freeing memory...\n");
519 	free(ntmp->ntm_ad, M_NTFSMNT, 0);
520 	free(ntmp, M_NTFSMNT, 0);
521 	mp->mnt_data = NULL;
522 	mp->mnt_flag &= ~MNT_LOCAL;
523 	return (0);
524 }
525 
526 int
527 ntfs_root(struct mount *mp, struct vnode **vpp)
528 {
529 	struct vnode *nvp;
530 	int error = 0;
531 
532 	DPRINTF("ntfs_root(): sysvn: %p\n",
533 	    VFSTONTFS(mp)->ntm_sysvn[NTFS_ROOTINO]);
534 	error = VFS_VGET(mp, (ino_t)NTFS_ROOTINO, &nvp);
535 	if(error) {
536 		printf("ntfs_root: VFS_VGET failed: %d\n",error);
537 		return (error);
538 	}
539 
540 	*vpp = nvp;
541 	return (0);
542 }
543 
544 /*
545  * Do operations associated with quotas, not supported
546  */
547 int
548 ntfs_quotactl(struct mount *mp, int cmds, uid_t uid, caddr_t arg,
549     struct proc *p)
550 {
551 	return EOPNOTSUPP;
552 }
553 
554 int
555 ntfs_calccfree(struct ntfsmount *ntmp, cn_t *cfreep)
556 {
557 	struct vnode *vp;
558 	u_int8_t *tmp;
559 	int j, error;
560 	cn_t cfree = 0;
561 	size_t bmsize, i;
562 
563 	vp = ntmp->ntm_sysvn[NTFS_BITMAPINO];
564 
565 	bmsize = VTOF(vp)->f_size;
566 
567 	tmp = malloc(bmsize, M_TEMP, M_WAITOK);
568 
569 	error = ntfs_readattr(ntmp, VTONT(vp), NTFS_A_DATA, NULL,
570 			       0, bmsize, tmp, NULL);
571 	if (error)
572 		goto out;
573 
574 	for(i=0;i<bmsize;i++)
575 		for(j=0;j<8;j++)
576 			if(~tmp[i] & (1 << j)) cfree++;
577 	*cfreep = cfree;
578 
579     out:
580 	free(tmp, M_TEMP, 0);
581 	return(error);
582 }
583 
584 int
585 ntfs_statfs(struct mount *mp, struct statfs *sbp, struct proc *p)
586 {
587 	struct ntfsmount *ntmp = VFSTONTFS(mp);
588 	u_int64_t mftallocated;
589 
590 	DPRINTF("ntfs_statfs():\n");
591 
592 	mftallocated = VTOF(ntmp->ntm_sysvn[NTFS_MFTINO])->f_allocated;
593 
594 	sbp->f_bsize = ntmp->ntm_bps;
595 	sbp->f_iosize = ntmp->ntm_bps * ntmp->ntm_spc;
596 	sbp->f_blocks = ntmp->ntm_bootfile.bf_spv;
597 	sbp->f_bfree = sbp->f_bavail = ntfs_cntobn(ntmp->ntm_cfree);
598 	sbp->f_ffree = sbp->f_favail = sbp->f_bfree / ntmp->ntm_bpmftrec;
599 	sbp->f_files = mftallocated / ntfs_bntob(ntmp->ntm_bpmftrec) +
600 		       sbp->f_ffree;
601 	copy_statfs_info(sbp, mp);
602 
603 	return (0);
604 }
605 
606 int
607 ntfs_sync(struct mount *mp, int waitfor, struct ucred *cred, struct proc *p)
608 {
609 	/*DPRINTF("ntfs_sync():\n");*/
610 	return (0);
611 }
612 
613 int
614 ntfs_fhtovp(struct mount *mp, struct fid *fhp, struct vnode **vpp)
615 {
616 	struct ntfid *ntfhp = (struct ntfid *)fhp;
617 	int error;
618 
619 	DDPRINTF("ntfs_fhtovp(): %s: %u\n",
620 	    mp->mnt_stat.f_mntonname, ntfhp->ntfid_ino);
621 
622 	error = ntfs_vgetex(mp, ntfhp->ntfid_ino, ntfhp->ntfid_attr, NULL,
623 			LK_EXCLUSIVE | LK_RETRY, 0, curproc, vpp); /* XXX */
624 	if (error != 0) {
625 		*vpp = NULLVP;
626 		return (error);
627 	}
628 
629 	/* XXX as unlink/rmdir/mkdir/creat are not currently possible
630 	 * with NTFS, we don't need to check anything else for now */
631 	return (0);
632 }
633 
634 int
635 ntfs_vptofh(struct vnode *vp, struct fid *fhp)
636 {
637 	struct ntnode *ntp;
638 	struct ntfid *ntfhp;
639 	struct fnode *fn;
640 
641 	DDPRINTF("ntfs_fhtovp(): %s: %p\n",
642 	    vp->v_mount->mnt_stat.f_mntonname, vp);
643 
644 	fn = VTOF(vp);
645 	ntp = VTONT(vp);
646 	ntfhp = (struct ntfid *)fhp;
647 	ntfhp->ntfid_len = sizeof(struct ntfid);
648 	ntfhp->ntfid_ino = ntp->i_number;
649 	ntfhp->ntfid_attr = fn->f_attrtype;
650 #ifdef notyet
651 	ntfhp->ntfid_gen = ntp->i_gen;
652 #endif
653 	return (0);
654 }
655 
656 int
657 ntfs_vgetex(struct mount *mp, ntfsino_t ino, u_int32_t attrtype, char *attrname,
658     u_long lkflags, u_long flags, struct proc *p, struct vnode **vpp)
659 {
660 	int error;
661 	struct ntfsmount *ntmp;
662 	struct ntnode *ip;
663 	struct fnode *fp;
664 	struct vnode *vp;
665 	enum vtype f_type;
666 
667 	DPRINTF("ntfs_vgetex: ino: %u, attr: 0x%x:%s, lkf: 0x%lx, f: 0x%lx\n",
668 	    ino, attrtype, attrname ? attrname : "", lkflags, flags);
669 
670 	ntmp = VFSTONTFS(mp);
671 	*vpp = NULL;
672 
673 	/* Get ntnode */
674 	error = ntfs_ntlookup(ntmp, ino, &ip, p);
675 	if (error) {
676 		printf("ntfs_vget: ntfs_ntget failed\n");
677 		return (error);
678 	}
679 
680 	/* It may be not initialized fully, so force load it */
681 	if (!(flags & VG_DONTLOADIN) && !(ip->i_flag & IN_LOADED)) {
682 		error = ntfs_loadntnode(ntmp, ip);
683 		if(error) {
684 			printf("ntfs_vget: CAN'T LOAD ATTRIBUTES FOR INO: %d\n",
685 			       ip->i_number);
686 			ntfs_ntput(ip, p);
687 
688 			return (error);
689 		}
690 	}
691 
692 	error = ntfs_fget(ntmp, ip, attrtype, attrname, &fp);
693 	if (error) {
694 		printf("ntfs_vget: ntfs_fget failed\n");
695 		ntfs_ntput(ip, p);
696 
697 		return (error);
698 	}
699 
700 	if (!(flags & VG_DONTVALIDFN) && !(fp->f_flag & FN_VALID)) {
701 		if ((ip->i_frflag & NTFS_FRFLAG_DIR) &&
702 		    (fp->f_attrtype == NTFS_A_DATA && fp->f_attrname == NULL)) {
703 			f_type = VDIR;
704 		} else if (flags & VG_EXT) {
705 			f_type = VNON;
706 			fp->f_size = fp->f_allocated = 0;
707 		} else {
708 			f_type = VREG;
709 
710 			error = ntfs_filesize(ntmp, fp,
711 					      &fp->f_size, &fp->f_allocated);
712 			if (error) {
713 				ntfs_ntput(ip, p);
714 
715 				return (error);
716 			}
717 		}
718 
719 		fp->f_flag |= FN_VALID;
720 	}
721 
722 	/*
723 	 * We may be calling vget() now. To avoid potential deadlock, we need
724 	 * to release ntnode lock, since due to locking order vnode
725 	 * lock has to be acquired first.
726 	 * ntfs_fget() bumped ntnode usecount, so ntnode won't be recycled
727 	 * prematurely.
728 	 */
729 	ntfs_ntput(ip, p);
730 
731 	if (FTOV(fp)) {
732 		/* vget() returns error if the vnode has been recycled */
733 		if (vget(FTOV(fp), lkflags, p) == 0) {
734 			*vpp = FTOV(fp);
735 			return (0);
736 		}
737 	}
738 
739 	error = getnewvnode(VT_NTFS, ntmp->ntm_mountp, &ntfs_vops, &vp);
740 	if(error) {
741 		ntfs_frele(fp);
742 		ntfs_ntput(ip, p);
743 
744 		return (error);
745 	}
746 	DPRINTF("ntfs_vget: vnode: %p for ntnode: %u\n", vp, ino);
747 
748 	fp->f_vp = vp;
749 	vp->v_data = fp;
750 	vp->v_type = f_type;
751 
752 	if (ino == NTFS_ROOTINO)
753 		vp->v_flag |= VROOT;
754 
755 	if (lkflags & LK_TYPE_MASK) {
756 		error = vn_lock(vp, lkflags, p);
757 		if (error) {
758 			vput(vp);
759 			return (error);
760 		}
761 	}
762 
763 	*vpp = vp;
764 	return (0);
765 }
766 
767 int
768 ntfs_vget(struct mount *mp, ino_t ino, struct vnode **vpp)
769 {
770 	if (ino > (ntfsino_t)-1)
771 		panic("ntfs_vget: alien ino_t %llu", (unsigned long long)ino);
772 	return ntfs_vgetex(mp, ino, NTFS_A_DATA, NULL,
773 			LK_EXCLUSIVE | LK_RETRY, 0, curproc, vpp); /* XXX */
774 }
775 
776 const struct vfsops ntfs_vfsops = {
777 	ntfs_mount,
778 	ntfs_start,
779 	ntfs_unmount,
780 	ntfs_root,
781 	ntfs_quotactl,
782 	ntfs_statfs,
783 	ntfs_sync,
784 	ntfs_vget,
785 	ntfs_fhtovp,
786 	ntfs_vptofh,
787 	ntfs_init,
788 	ntfs_sysctl,
789 	ntfs_checkexp,
790 };
791