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