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