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