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