xref: /netbsd-src/sys/fs/ntfs/ntfs_vfsops.c (revision 75219f3a016dfaad1cb304eb017f9787b1de8292)
1 /*	$NetBSD: ntfs_vfsops.c,v 1.90 2013/11/23 13:35:36 christos Exp $	*/
2 
3 /*-
4  * Copyright (c) 1998, 1999 Semen Ustimenko
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  *
28  *	Id: ntfs_vfsops.c,v 1.7 1999/05/31 11:28:30 phk Exp
29  */
30 
31 #include <sys/cdefs.h>
32 __KERNEL_RCSID(0, "$NetBSD: ntfs_vfsops.c,v 1.90 2013/11/23 13:35:36 christos Exp $");
33 
34 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/namei.h>
37 #include <sys/proc.h>
38 #include <sys/kernel.h>
39 #include <sys/vnode.h>
40 #include <sys/mount.h>
41 #include <sys/buf.h>
42 #include <sys/fcntl.h>
43 #include <sys/malloc.h>
44 #include <sys/sysctl.h>
45 #include <sys/device.h>
46 #include <sys/conf.h>
47 #include <sys/kauth.h>
48 #include <sys/module.h>
49 
50 #include <uvm/uvm_extern.h>
51 
52 #include <miscfs/genfs/genfs.h>
53 #include <miscfs/specfs/specdev.h>
54 
55 #include <fs/ntfs/ntfs.h>
56 #include <fs/ntfs/ntfs_inode.h>
57 #include <fs/ntfs/ntfs_subr.h>
58 #include <fs/ntfs/ntfs_vfsops.h>
59 #include <fs/ntfs/ntfs_ihash.h>
60 #include <fs/ntfs/ntfsmount.h>
61 
62 MODULE(MODULE_CLASS_VFS, ntfs, NULL);
63 
64 MALLOC_JUSTDEFINE(M_NTFSMNT, "NTFS mount", "NTFS mount structure");
65 MALLOC_JUSTDEFINE(M_NTFSNTNODE,"NTFS ntnode",  "NTFS ntnode information");
66 MALLOC_JUSTDEFINE(M_NTFSFNODE,"NTFS fnode",  "NTFS fnode information");
67 MALLOC_JUSTDEFINE(M_NTFSDIR,"NTFS dir",  "NTFS dir buffer");
68 
69 static int	ntfs_mount(struct mount *, const char *, void *, size_t *);
70 static int	ntfs_root(struct mount *, struct vnode **);
71 static int	ntfs_start(struct mount *, int);
72 static int	ntfs_statvfs(struct mount *, struct statvfs *);
73 static int	ntfs_sync(struct mount *, int, kauth_cred_t);
74 static int	ntfs_unmount(struct mount *, int);
75 static int	ntfs_vget(struct mount *mp, ino_t ino,
76 			       struct vnode **vpp);
77 static int	ntfs_mountfs(struct vnode *, struct mount *,
78 				  struct ntfs_args *, struct lwp *);
79 static int	ntfs_vptofh(struct vnode *, struct fid *, size_t *);
80 
81 static void     ntfs_init(void);
82 static void     ntfs_reinit(void);
83 static void     ntfs_done(void);
84 static int      ntfs_fhtovp(struct mount *, struct fid *,
85 				struct vnode **);
86 static int      ntfs_mountroot(void);
87 
88 static const struct genfs_ops ntfs_genfsops = {
89 	.gop_write = genfs_compat_gop_write,
90 };
91 
92 static struct sysctllog *ntfs_sysctl_log;
93 
94 static int
95 ntfs_mountroot(void)
96 {
97 	struct mount *mp;
98 	struct lwp *l = curlwp;	/* XXX */
99 	int error;
100 	struct ntfs_args args;
101 
102 	if (device_class(root_device) != DV_DISK)
103 		return (ENODEV);
104 
105 	if ((error = vfs_rootmountalloc(MOUNT_NTFS, "root_device", &mp))) {
106 		vrele(rootvp);
107 		return (error);
108 	}
109 
110 	args.flag = 0;
111 	args.uid = 0;
112 	args.gid = 0;
113 	args.mode = 0777;
114 
115 	if ((error = ntfs_mountfs(rootvp, mp, &args, l)) != 0) {
116 		vfs_unbusy(mp, false, NULL);
117 		vfs_destroy(mp);
118 		return (error);
119 	}
120 
121 	mountlist_append(mp);
122 	(void)ntfs_statvfs(mp, &mp->mnt_stat);
123 	vfs_unbusy(mp, false, NULL);
124 	return (0);
125 }
126 
127 static void
128 ntfs_init(void)
129 {
130 
131 	malloc_type_attach(M_NTFSMNT);
132 	malloc_type_attach(M_NTFSNTNODE);
133 	malloc_type_attach(M_NTFSFNODE);
134 	malloc_type_attach(M_NTFSDIR);
135 	malloc_type_attach(M_NTFSNTVATTR);
136 	malloc_type_attach(M_NTFSRDATA);
137 	malloc_type_attach(M_NTFSDECOMP);
138 	malloc_type_attach(M_NTFSRUN);
139 	ntfs_nthashinit();
140 	ntfs_toupper_init();
141 }
142 
143 static void
144 ntfs_reinit(void)
145 {
146 	ntfs_nthashreinit();
147 }
148 
149 static void
150 ntfs_done(void)
151 {
152 	ntfs_nthashdone();
153 	malloc_type_detach(M_NTFSMNT);
154 	malloc_type_detach(M_NTFSNTNODE);
155 	malloc_type_detach(M_NTFSFNODE);
156 	malloc_type_detach(M_NTFSDIR);
157 	malloc_type_detach(M_NTFSNTVATTR);
158 	malloc_type_detach(M_NTFSRDATA);
159 	malloc_type_detach(M_NTFSDECOMP);
160 	malloc_type_detach(M_NTFSRUN);
161 }
162 
163 static int
164 ntfs_mount (
165 	struct mount *mp,
166 	const char *path,
167 	void *data,
168 	size_t *data_len)
169 {
170 	struct lwp *l = curlwp;
171 	int		err = 0, flags;
172 	struct vnode	*devvp;
173 	struct ntfs_args *args = data;
174 
175 	if (*data_len < sizeof *args)
176 		return EINVAL;
177 
178 	if (mp->mnt_flag & MNT_GETARGS) {
179 		struct ntfsmount *ntmp = VFSTONTFS(mp);
180 		if (ntmp == NULL)
181 			return EIO;
182 		args->fspec = NULL;
183 		args->uid = ntmp->ntm_uid;
184 		args->gid = ntmp->ntm_gid;
185 		args->mode = ntmp->ntm_mode;
186 		args->flag = ntmp->ntm_flag;
187 		*data_len = sizeof *args;
188 		return 0;
189 	}
190 	/*
191 	 ***
192 	 * Mounting non-root file system or updating a file system
193 	 ***
194 	 */
195 
196 	/*
197 	 * If updating, check whether changing from read-only to
198 	 * read/write; if there is no device name, that's all we do.
199 	 */
200 	if (mp->mnt_flag & MNT_UPDATE) {
201 		printf("ntfs_mount(): MNT_UPDATE not supported\n");
202 		return (EINVAL);
203 	}
204 
205 	/*
206 	 * Not an update, or updating the name: look up the name
207 	 * and verify that it refers to a sensible block device.
208 	 */
209 	err = namei_simple_user(args->fspec,
210 				NSM_FOLLOW_NOEMULROOT, &devvp);
211 	if (err) {
212 		/* can't get devvp!*/
213 		return (err);
214 	}
215 
216 	if (devvp->v_type != VBLK) {
217 		err = ENOTBLK;
218 		goto fail;
219 	}
220 	if (bdevsw_lookup(devvp->v_rdev) == NULL) {
221 		err = ENXIO;
222 		goto fail;
223 	}
224 	if (mp->mnt_flag & MNT_UPDATE) {
225 #if 0
226 		/*
227 		 ********************
228 		 * UPDATE
229 		 ********************
230 		 */
231 
232 		if (devvp != ntmp->um_devvp) {
233 			err = EINVAL;	/* needs translation */
234 			goto fail;
235 		}
236 
237 		/*
238 		 * Update device name only on success
239 		 */
240 		err = set_statvfs_info(NULL, UIO_USERSPACE, args->fspec,
241 		    UIO_USERSPACE, mp->mnt_op->vfs_name, mp, p);
242 		if (err)
243 			goto fail;
244 
245 		vrele(devvp);
246 #endif
247 	} else {
248 		/*
249 		 ********************
250 		 * NEW MOUNT
251 		 ********************
252 		 */
253 
254 		/*
255 		 * Since this is a new mount, we want the names for
256 		 * the device and the mount point copied in.  If an
257 		 * error occurs,  the mountpoint is discarded by the
258 		 * upper level code.
259 		 */
260 
261 		/* Save "last mounted on" info for mount point (NULL pad)*/
262 		err = set_statvfs_info(path, UIO_USERSPACE, args->fspec,
263 		    UIO_USERSPACE, mp->mnt_op->vfs_name, mp, l);
264 		if (err)
265 			goto fail;
266 
267 		if (mp->mnt_flag & MNT_RDONLY)
268 			flags = FREAD;
269 		else
270 			flags = FREAD|FWRITE;
271 		vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
272 		err = VOP_OPEN(devvp, flags, FSCRED);
273 		VOP_UNLOCK(devvp);
274 		if (err)
275 			goto fail;
276 		err = ntfs_mountfs(devvp, mp, args, l);
277 		if (err) {
278 			vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
279 			(void)VOP_CLOSE(devvp, flags, NOCRED);
280 			VOP_UNLOCK(devvp);
281 			goto fail;
282 		}
283 	}
284 
285 	/*
286 	 * Initialize FS stat information in mount struct; uses both
287 	 * mp->mnt_stat.f_mntonname and mp->mnt_stat.f_mntfromname
288 	 *
289 	 * This code is common to root and non-root mounts
290 	 */
291 	(void)VFS_STATVFS(mp, &mp->mnt_stat);
292 	return (err);
293 
294 fail:
295 	vrele(devvp);
296 	return (err);
297 }
298 
299 /*
300  * Common code for mount and mountroot
301  */
302 int
303 ntfs_mountfs(struct vnode *devvp, struct mount *mp, struct ntfs_args *argsp, struct lwp *l)
304 {
305 	struct buf *bp;
306 	struct ntfsmount *ntmp;
307 	dev_t dev = devvp->v_rdev;
308 	int error, i;
309 	struct vnode *vp;
310 
311 	ntmp = NULL;
312 
313 	/*
314 	 * Flush out any old buffers remaining from a previous use.
315 	 */
316 	vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
317 	error = vinvalbuf(devvp, V_SAVE, l->l_cred, l, 0, 0);
318 	VOP_UNLOCK(devvp);
319 	if (error)
320 		return (error);
321 
322 	bp = NULL;
323 
324 	error = bread(devvp, BBLOCK, BBSIZE, NOCRED, 0, &bp);
325 	if (error)
326 		goto out;
327 	ntmp = malloc( sizeof *ntmp, M_NTFSMNT, M_WAITOK|M_ZERO);
328 	memcpy( &ntmp->ntm_bootfile,  bp->b_data, sizeof(struct bootfile) );
329 	brelse( bp , 0 );
330 	bp = NULL;
331 
332 	if (strncmp(ntmp->ntm_bootfile.bf_sysid, NTFS_BBID, NTFS_BBIDLEN)) {
333 		error = EINVAL;
334 		dprintf(("ntfs_mountfs: invalid boot block\n"));
335 		goto out;
336 	}
337 
338 	{
339 		int8_t cpr = ntmp->ntm_mftrecsz;
340 		if( cpr > 0 )
341 			ntmp->ntm_bpmftrec = ntmp->ntm_spc * cpr;
342 		else
343 			ntmp->ntm_bpmftrec = (1 << (-cpr)) / ntmp->ntm_bps;
344 	}
345 	dprintf(("ntfs_mountfs(): bps: %d, spc: %d, media: %x, mftrecsz: %d (%d sects)\n",
346 		ntmp->ntm_bps,ntmp->ntm_spc,ntmp->ntm_bootfile.bf_media,
347 		ntmp->ntm_mftrecsz,ntmp->ntm_bpmftrec));
348 	dprintf(("ntfs_mountfs(): mftcn: 0x%x|0x%x\n",
349 		(u_int32_t)ntmp->ntm_mftcn,(u_int32_t)ntmp->ntm_mftmirrcn));
350 
351 	ntmp->ntm_mountp = mp;
352 	ntmp->ntm_dev = dev;
353 	ntmp->ntm_devvp = devvp;
354 	ntmp->ntm_uid = argsp->uid;
355 	ntmp->ntm_gid = argsp->gid;
356 	ntmp->ntm_mode = argsp->mode;
357 	ntmp->ntm_flag = argsp->flag;
358 	mp->mnt_data = ntmp;
359 
360 	/* set file name encode/decode hooks XXX utf-8 only for now */
361 	ntmp->ntm_wget = ntfs_utf8_wget;
362 	ntmp->ntm_wput = ntfs_utf8_wput;
363 	ntmp->ntm_wcmp = ntfs_utf8_wcmp;
364 
365 	dprintf(("ntfs_mountfs(): case-%s,%s uid: %d, gid: %d, mode: %o\n",
366 		(ntmp->ntm_flag & NTFS_MFLAG_CASEINS)?"insens.":"sens.",
367 		(ntmp->ntm_flag & NTFS_MFLAG_ALLNAMES)?" allnames,":"",
368 		ntmp->ntm_uid, ntmp->ntm_gid, ntmp->ntm_mode));
369 
370 	/*
371 	 * We read in some system nodes to do not allow
372 	 * reclaim them and to have everytime access to them.
373 	 */
374 	{
375 		int pi[3] = { NTFS_MFTINO, NTFS_ROOTINO, NTFS_BITMAPINO };
376 		for (i=0; i<3; i++) {
377 			error = VFS_VGET(mp, pi[i], &(ntmp->ntm_sysvn[pi[i]]));
378 			if(error)
379 				goto out1;
380 			ntmp->ntm_sysvn[pi[i]]->v_vflag |= VV_SYSTEM;
381 			vref(ntmp->ntm_sysvn[pi[i]]);
382 			vput(ntmp->ntm_sysvn[pi[i]]);
383 		}
384 	}
385 
386 	/* read the Unicode lowercase --> uppercase translation table,
387 	 * if necessary */
388 	if ((error = ntfs_toupper_use(mp, ntmp)))
389 		goto out1;
390 
391 	/*
392 	 * Scan $BitMap and count free clusters
393 	 */
394 	error = ntfs_calccfree(ntmp, &ntmp->ntm_cfree);
395 	if(error)
396 		goto out1;
397 
398 	/*
399 	 * Read and translate to internal format attribute
400 	 * definition file.
401 	 */
402 	{
403 		int num,j;
404 		struct attrdef ad;
405 
406 		/* Open $AttrDef */
407 		error = VFS_VGET(mp, NTFS_ATTRDEFINO, &vp );
408 		if(error)
409 			goto out1;
410 
411 		/* Count valid entries */
412 		for(num=0;;num++) {
413 			error = ntfs_readattr(ntmp, VTONT(vp),
414 					NTFS_A_DATA, NULL,
415 					num * sizeof(ad), sizeof(ad),
416 					&ad, NULL);
417 			if (error)
418 				goto out1;
419 			if (ad.ad_name[0] == 0)
420 				break;
421 		}
422 
423 		/* Alloc memory for attribute definitions */
424 		ntmp->ntm_ad = (struct ntvattrdef *) malloc(
425 			num * sizeof(struct ntvattrdef),
426 			M_NTFSMNT, M_WAITOK);
427 
428 		ntmp->ntm_adnum = num;
429 
430 		/* Read them and translate */
431 		for(i=0;i<num;i++){
432 			error = ntfs_readattr(ntmp, VTONT(vp),
433 					NTFS_A_DATA, NULL,
434 					i * sizeof(ad), sizeof(ad),
435 					&ad, NULL);
436 			if (error)
437 				goto out1;
438 			j = 0;
439 			do {
440 				ntmp->ntm_ad[i].ad_name[j] = ad.ad_name[j];
441 			} while(ad.ad_name[j++]);
442 			ntmp->ntm_ad[i].ad_namelen = j - 1;
443 			ntmp->ntm_ad[i].ad_type = ad.ad_type;
444 		}
445 
446 		vput(vp);
447 	}
448 
449 	mp->mnt_stat.f_fsidx.__fsid_val[0] = dev;
450 	mp->mnt_stat.f_fsidx.__fsid_val[1] = makefstype(MOUNT_NTFS);
451 	mp->mnt_stat.f_fsid = mp->mnt_stat.f_fsidx.__fsid_val[0];
452 	mp->mnt_stat.f_namemax = NTFS_MAXFILENAME;
453 	mp->mnt_flag |= MNT_LOCAL;
454 	spec_node_setmountedfs(devvp, mp);
455 	return (0);
456 
457 out1:
458 	for(i=0;i<NTFS_SYSNODESNUM;i++)
459 		if(ntmp->ntm_sysvn[i]) vrele(ntmp->ntm_sysvn[i]);
460 
461 	if (vflush(mp,NULLVP,0)) {
462 		dprintf(("ntfs_mountfs: vflush failed\n"));
463 	}
464 out:
465 	spec_node_setmountedfs(devvp, NULL);
466 	if (bp)
467 		brelse(bp, 0);
468 
469 	if (error) {
470 		if (ntmp) {
471 			if (ntmp->ntm_ad)
472 				free(ntmp->ntm_ad, M_NTFSMNT);
473 			free(ntmp, M_NTFSMNT);
474 		}
475 	}
476 
477 	return (error);
478 }
479 
480 static int
481 ntfs_start (
482 	struct mount *mp,
483 	int flags)
484 {
485 	return (0);
486 }
487 
488 static int
489 ntfs_unmount(
490 	struct mount *mp,
491 	int mntflags)
492 {
493 	struct lwp *l = curlwp;
494 	struct ntfsmount *ntmp;
495 	int error, ronly = 0, flags, i;
496 
497 	dprintf(("ntfs_unmount: unmounting...\n"));
498 	ntmp = VFSTONTFS(mp);
499 
500 	flags = 0;
501 	if(mntflags & MNT_FORCE)
502 		flags |= FORCECLOSE;
503 
504 	dprintf(("ntfs_unmount: vflushing...\n"));
505 	error = vflush(mp,NULLVP,flags | SKIPSYSTEM);
506 	if (error) {
507 		dprintf(("ntfs_unmount: vflush failed: %d\n",error));
508 		return (error);
509 	}
510 
511 	/* Check if only system vnodes are rest */
512 	for(i=0;i<NTFS_SYSNODESNUM;i++)
513 		 if((ntmp->ntm_sysvn[i]) &&
514 		    (ntmp->ntm_sysvn[i]->v_usecount > 1)) return (EBUSY);
515 
516 	/* Dereference all system vnodes */
517 	for(i=0;i<NTFS_SYSNODESNUM;i++)
518 		 if(ntmp->ntm_sysvn[i]) vrele(ntmp->ntm_sysvn[i]);
519 
520 	/* vflush system vnodes */
521 	error = vflush(mp,NULLVP,flags);
522 	if (error) {
523 		panic("ntfs_unmount: vflush failed(sysnodes): %d\n",error);
524 	}
525 
526 	/* Check if the type of device node isn't VBAD before
527 	 * touching v_specinfo.  If the device vnode is revoked, the
528 	 * field is NULL and touching it causes null pointer derefercence.
529 	 */
530 	if (ntmp->ntm_devvp->v_type != VBAD)
531 		spec_node_setmountedfs(ntmp->ntm_devvp, NULL);
532 
533 	vinvalbuf(ntmp->ntm_devvp, V_SAVE, NOCRED, l, 0, 0);
534 
535 	/* lock the device vnode before calling VOP_CLOSE() */
536 	vn_lock(ntmp->ntm_devvp, LK_EXCLUSIVE | LK_RETRY);
537 	error = VOP_CLOSE(ntmp->ntm_devvp, ronly ? FREAD : FREAD|FWRITE,
538 		NOCRED);
539 	KASSERT(error == 0);
540 	VOP_UNLOCK(ntmp->ntm_devvp);
541 
542 	vrele(ntmp->ntm_devvp);
543 
544 	/* free the toupper table, if this has been last mounted ntfs volume */
545 	ntfs_toupper_unuse();
546 
547 	dprintf(("ntfs_umount: freeing memory...\n"));
548 	mp->mnt_data = NULL;
549 	mp->mnt_flag &= ~MNT_LOCAL;
550 	free(ntmp->ntm_ad, M_NTFSMNT);
551 	free(ntmp, M_NTFSMNT);
552 	return (0);
553 }
554 
555 static int
556 ntfs_root(
557 	struct mount *mp,
558 	struct vnode **vpp)
559 {
560 	struct vnode *nvp;
561 	int error = 0;
562 
563 	dprintf(("ntfs_root(): sysvn: %p\n",
564 		VFSTONTFS(mp)->ntm_sysvn[NTFS_ROOTINO]));
565 	error = VFS_VGET(mp, (ino_t)NTFS_ROOTINO, &nvp);
566 	if(error) {
567 		printf("ntfs_root: VFS_VGET failed: %d\n",error);
568 		return (error);
569 	}
570 
571 	*vpp = nvp;
572 	return (0);
573 }
574 
575 int
576 ntfs_calccfree(
577 	struct ntfsmount *ntmp,
578 	cn_t *cfreep)
579 {
580 	struct vnode *vp;
581 	u_int8_t *tmp;
582 	int j, error;
583 	cn_t cfree = 0;
584 	size_t bmsize, i;
585 
586 	vp = ntmp->ntm_sysvn[NTFS_BITMAPINO];
587 
588 	bmsize = VTOF(vp)->f_size;
589 
590 	tmp = (u_int8_t *) malloc(bmsize, M_TEMP, M_WAITOK);
591 
592 	error = ntfs_readattr(ntmp, VTONT(vp), NTFS_A_DATA, NULL,
593 			       0, bmsize, tmp, NULL);
594 	if (error)
595 		goto out;
596 
597 	for(i=0;i<bmsize;i++)
598 		for(j=0;j<8;j++)
599 			if(~tmp[i] & (1 << j)) cfree++;
600 	*cfreep = cfree;
601 
602     out:
603 	free(tmp, M_TEMP);
604 	return(error);
605 }
606 
607 static int
608 ntfs_statvfs(
609 	struct mount *mp,
610 	struct statvfs *sbp)
611 {
612 	struct ntfsmount *ntmp = VFSTONTFS(mp);
613 	u_int64_t mftallocated;
614 
615 	dprintf(("ntfs_statvfs():\n"));
616 
617 	mftallocated = VTOF(ntmp->ntm_sysvn[NTFS_MFTINO])->f_allocated;
618 
619 	sbp->f_bsize = ntmp->ntm_bps;
620 	sbp->f_frsize = sbp->f_bsize; /* XXX */
621 	sbp->f_iosize = ntmp->ntm_bps * ntmp->ntm_spc;
622 	sbp->f_blocks = ntmp->ntm_bootfile.bf_spv;
623 	sbp->f_bfree = sbp->f_bavail = ntfs_cntobn(ntmp->ntm_cfree);
624 	sbp->f_ffree = sbp->f_favail = sbp->f_bfree / ntmp->ntm_bpmftrec;
625 	sbp->f_files = mftallocated / ntfs_bntob(ntmp->ntm_bpmftrec) +
626 	    sbp->f_ffree;
627 	sbp->f_fresvd = sbp->f_bresvd = 0; /* XXX */
628 	sbp->f_flag = mp->mnt_flag;
629 	copy_statvfs_info(sbp, mp);
630 	return (0);
631 }
632 
633 static int
634 ntfs_sync (
635 	struct mount *mp,
636 	int waitfor,
637 	kauth_cred_t cred)
638 {
639 	/*dprintf(("ntfs_sync():\n"));*/
640 	return (0);
641 }
642 
643 /*ARGSUSED*/
644 static int
645 ntfs_fhtovp(
646 	struct mount *mp,
647 	struct fid *fhp,
648 	struct vnode **vpp)
649 {
650 	struct ntfid ntfh;
651 	int error;
652 
653 	if (fhp->fid_len != sizeof(struct ntfid))
654 		return EINVAL;
655 	memcpy(&ntfh, fhp, sizeof(ntfh));
656 	ddprintf(("ntfs_fhtovp(): %s: %llu\n", mp->mnt_stat.f_mntonname,
657 	    (unsigned long long)ntfh.ntfid_ino));
658 
659 	error = ntfs_vgetex(mp, ntfh.ntfid_ino, ntfh.ntfid_attr, NULL,
660 			LK_EXCLUSIVE, 0, vpp);
661 	if (error != 0) {
662 		*vpp = NULLVP;
663 		return (error);
664 	}
665 
666 	/* XXX as unlink/rmdir/mkdir/creat are not currently possible
667 	 * with NTFS, we don't need to check anything else for now */
668 	return (0);
669 }
670 
671 static int
672 ntfs_vptofh(
673 	struct vnode *vp,
674 	struct fid *fhp,
675 	size_t *fh_size)
676 {
677 	struct ntnode *ntp;
678 	struct ntfid ntfh;
679 	struct fnode *fn;
680 
681 	if (*fh_size < sizeof(struct ntfid)) {
682 		*fh_size = sizeof(struct ntfid);
683 		return E2BIG;
684 	}
685 	*fh_size = sizeof(struct ntfid);
686 
687 	ddprintf(("ntfs_fhtovp(): %s: %p\n", vp->v_mount->mnt_stat.f_mntonname,
688 		vp));
689 
690 	fn = VTOF(vp);
691 	ntp = VTONT(vp);
692 	memset(&ntfh, 0, sizeof(ntfh));
693 	ntfh.ntfid_len = sizeof(struct ntfid);
694 	ntfh.ntfid_ino = ntp->i_number;
695 	ntfh.ntfid_attr = fn->f_attrtype;
696 #ifdef notyet
697 	ntfh.ntfid_gen = ntp->i_gen;
698 #endif
699 	memcpy(fhp, &ntfh, sizeof(ntfh));
700 	return (0);
701 }
702 
703 int
704 ntfs_vgetex(
705 	struct mount *mp,
706 	ino_t ino,
707 	u_int32_t attrtype,
708 	char *attrname,
709 	u_long lkflags,
710 	u_long flags,
711 	struct vnode **vpp)
712 {
713 	int error;
714 	struct ntfsmount *ntmp;
715 	struct ntnode *ip;
716 	struct fnode *fp;
717 	struct vnode *vp;
718 	enum vtype f_type = VBAD;
719 
720 	dprintf(("ntfs_vgetex: ino: %llu, attr: 0x%x:%s, lkf: 0x%lx, f:"
721 	    " 0x%lx\n", (unsigned long long)ino, attrtype,
722 	    attrname ? attrname : "", (u_long)lkflags, (u_long)flags));
723 
724 	ntmp = VFSTONTFS(mp);
725 	*vpp = NULL;
726 
727 loop:
728 	/* Get ntnode */
729 	error = ntfs_ntlookup(ntmp, ino, &ip);
730 	if (error) {
731 		printf("ntfs_vget: ntfs_ntget failed\n");
732 		return (error);
733 	}
734 
735 	/* It may be not initialized fully, so force load it */
736 	if (!(flags & VG_DONTLOADIN) && !(ip->i_flag & IN_LOADED)) {
737 		error = ntfs_loadntnode(ntmp, ip);
738 		if(error) {
739 			printf("ntfs_vget: CAN'T LOAD ATTRIBUTES FOR INO:"
740 			    " %llu\n", (unsigned long long)ip->i_number);
741 			ntfs_ntput(ip);
742 			return (error);
743 		}
744 	}
745 
746 	error = ntfs_fget(ntmp, ip, attrtype, attrname, &fp);
747 	if (error) {
748 		printf("ntfs_vget: ntfs_fget failed\n");
749 		ntfs_ntput(ip);
750 		return (error);
751 	}
752 
753 	if (!(flags & VG_DONTVALIDFN) && !(fp->f_flag & FN_VALID)) {
754 		if ((ip->i_frflag & NTFS_FRFLAG_DIR) &&
755 		    (fp->f_attrtype == NTFS_A_DATA && fp->f_attrname == NULL)) {
756 			f_type = VDIR;
757 		} else if (flags & VG_EXT) {
758 			f_type = VNON;
759 			fp->f_size = fp->f_allocated = 0;
760 		} else {
761 			f_type = VREG;
762 
763 			error = ntfs_filesize(ntmp, fp,
764 					      &fp->f_size, &fp->f_allocated);
765 			if (error) {
766 				ntfs_ntput(ip);
767 				return (error);
768 			}
769 		}
770 
771 		fp->f_flag |= FN_VALID;
772 	}
773 
774 	/*
775 	 * We may be calling vget() now. To avoid potential deadlock, we need
776 	 * to release ntnode lock, since due to locking order vnode
777 	 * lock has to be acquired first.
778 	 * ntfs_fget() bumped ntnode usecount, so ntnode won't be recycled
779 	 * prematurely.
780 	 * Take v_interlock before releasing ntnode lock to avoid races.
781 	 */
782 	vp = FTOV(fp);
783 	if (vp) {
784 		mutex_enter(vp->v_interlock);
785 		ntfs_ntput(ip);
786 		if (vget(vp, lkflags) != 0)
787 			goto loop;
788 		*vpp = vp;
789 		return 0;
790 	}
791 	ntfs_ntput(ip);
792 
793 	error = getnewvnode(VT_NTFS, ntmp->ntm_mountp, ntfs_vnodeop_p,
794 	    NULL, &vp);
795 	if(error) {
796 		ntfs_frele(fp);
797 		return (error);
798 	}
799 	ntfs_ntget(ip);
800 	error = ntfs_fget(ntmp, ip, attrtype, attrname, &fp);
801 	if (error) {
802 		printf("ntfs_vget: ntfs_fget failed\n");
803 		ntfs_ntput(ip);
804 		return (error);
805 	}
806 	if (FTOV(fp)) {
807 		/*
808 		 * Another thread beat us, put back freshly allocated
809 		 * vnode and retry.
810 		 */
811 		ntfs_ntput(ip);
812 		ungetnewvnode(vp);
813 		goto loop;
814 	}
815 	dprintf(("ntfs_vget: vnode: %p for ntnode: %llu\n", vp,
816 	    (unsigned long long)ino));
817 
818 	fp->f_vp = vp;
819 	vp->v_data = fp;
820 	if (f_type != VBAD)
821 		vp->v_type = f_type;
822 	genfs_node_init(vp, &ntfs_genfsops);
823 
824 	if (ino == NTFS_ROOTINO)
825 		vp->v_vflag |= VV_ROOT;
826 
827 	ntfs_ntput(ip);
828 
829 	if (lkflags & (LK_EXCLUSIVE | LK_SHARED)) {
830 		error = vn_lock(vp, lkflags);
831 		if (error) {
832 			vput(vp);
833 			return (error);
834 		}
835 	}
836 
837 	uvm_vnp_setsize(vp, fp->f_size); /* XXX: mess, cf. ntfs_lookupfile() */
838 	vref(ip->i_devvp);
839 	*vpp = vp;
840 	return (0);
841 }
842 
843 static int
844 ntfs_vget(
845 	struct mount *mp,
846 	ino_t ino,
847 	struct vnode **vpp)
848 {
849 	return ntfs_vgetex(mp, ino, NTFS_A_DATA, NULL, LK_EXCLUSIVE, 0, vpp);
850 }
851 
852 extern const struct vnodeopv_desc ntfs_vnodeop_opv_desc;
853 
854 const struct vnodeopv_desc * const ntfs_vnodeopv_descs[] = {
855 	&ntfs_vnodeop_opv_desc,
856 	NULL,
857 };
858 
859 struct vfsops ntfs_vfsops = {
860 	MOUNT_NTFS,
861 	sizeof (struct ntfs_args),
862 	ntfs_mount,
863 	ntfs_start,
864 	ntfs_unmount,
865 	ntfs_root,
866 	(void *)eopnotsupp,	/* vfs_quotactl */
867 	ntfs_statvfs,
868 	ntfs_sync,
869 	ntfs_vget,
870 	ntfs_fhtovp,
871 	ntfs_vptofh,
872 	ntfs_init,
873 	ntfs_reinit,
874 	ntfs_done,
875 	ntfs_mountroot,
876 	(int (*)(struct mount *, struct vnode *, struct timespec *)) eopnotsupp,
877 	vfs_stdextattrctl,
878 	(void *)eopnotsupp,		/* vfs_suspendctl */
879 	genfs_renamelock_enter,
880 	genfs_renamelock_exit,
881 	(void *)eopnotsupp,
882 	ntfs_vnodeopv_descs,
883 	0,
884 	{ NULL, NULL },
885 };
886 
887 static int
888 ntfs_modcmd(modcmd_t cmd, void *arg)
889 {
890 	int error;
891 
892 	switch (cmd) {
893 	case MODULE_CMD_INIT:
894 		error = vfs_attach(&ntfs_vfsops);
895 		if (error != 0)
896 			break;
897 		sysctl_createv(&ntfs_sysctl_log, 0, NULL, NULL,
898 			       CTLFLAG_PERMANENT,
899 			       CTLTYPE_NODE, "vfs", NULL,
900 			       NULL, 0, NULL, 0,
901 			       CTL_VFS, CTL_EOL);
902 		sysctl_createv(&ntfs_sysctl_log, 0, NULL, NULL,
903 			       CTLFLAG_PERMANENT,
904 			       CTLTYPE_NODE, "ntfs",
905 			       SYSCTL_DESCR("NTFS file system"),
906 			       NULL, 0, NULL, 0,
907 			       CTL_VFS, 20, CTL_EOL);
908 		/*
909 		 * XXX the "20" above could be dynamic, thereby eliminating
910 		 * one more instance of the "number to vfs" mapping problem,
911 		 * but "20" is the order as taken from sys/mount.h
912 		 */
913 		break;
914 	case MODULE_CMD_FINI:
915 		error = vfs_detach(&ntfs_vfsops);
916 		if (error != 0)
917 			break;
918 		sysctl_teardown(&ntfs_sysctl_log);
919 		break;
920 	default:
921 		error = ENOTTY;
922 		break;
923 	}
924 
925 	return (error);
926 }
927