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