xref: /dragonfly/sys/vfs/ntfs/ntfs_vfsops.c (revision 8accc937)
1 /*	$NetBSD: ntfs_vfsops.c,v 1.23 1999/11/15 19:38:14 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  * $FreeBSD: src/sys/ntfs/ntfs_vfsops.c,v 1.20.2.5 2001/12/25 01:44:45 dillon Exp $
29  */
30 
31 
32 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/conf.h>
35 #include <sys/proc.h>
36 #include <sys/nlookup.h>
37 #include <sys/kernel.h>
38 #include <sys/vnode.h>
39 #include <sys/mount.h>
40 #include <sys/buf.h>
41 #include <sys/fcntl.h>
42 #include <sys/malloc.h>
43 #include <sys/systm.h>
44 #if defined(__NetBSD__)
45 #include <sys/device.h>
46 #endif
47 
48 #include <machine/inttypes.h>
49 
50 #include <vm/vm.h>
51 #include <vm/vm_param.h>
52 #if defined(__NetBSD__)
53 #include <vm/vm_prot.h>
54 #endif
55 #include <vm/vm_page.h>
56 #include <vm/vm_object.h>
57 #include <vm/vm_extern.h>
58 #include <vm/vm_zone.h>
59 
60 #if defined(__NetBSD__)
61 #include <miscfs/specfs/specdev.h>
62 #endif
63 
64 #include <sys/buf2.h>
65 
66 #include "ntfs.h"
67 #include "ntfs_inode.h"
68 #include "ntfs_subr.h"
69 #include "ntfs_vfsops.h"
70 #include "ntfs_ihash.h"
71 #include "ntfsmount.h"
72 
73 extern struct vop_ops ntfs_vnode_vops;
74 
75 #if defined(__DragonFly__)
76 MALLOC_DEFINE(M_NTFSMNT, "NTFS mount", "NTFS mount structure");
77 MALLOC_DEFINE(M_NTFSNTNODE,"NTFS ntnode",  "NTFS ntnode information");
78 MALLOC_DEFINE(M_NTFSFNODE,"NTFS fnode",  "NTFS fnode information");
79 MALLOC_DEFINE(M_NTFSDIR,"NTFS dir",  "NTFS dir buffer");
80 #endif
81 
82 struct iconv_functions *ntfs_iconv = NULL;
83 
84 static int	ntfs_root (struct mount *, struct vnode **);
85 static int	ntfs_statfs (struct mount *, struct statfs *, struct ucred *cred);
86 static int	ntfs_unmount (struct mount *, int);
87 static int	ntfs_vget (struct mount *mp, struct vnode *dvp,
88 				ino_t ino, struct vnode **vpp);
89 static int	ntfs_mountfs (struct vnode *, struct mount *,
90 				struct ntfs_args *, struct ucred *);
91 static int	ntfs_vptofh (struct vnode *, struct fid *);
92 static int	ntfs_fhtovp (struct mount *, struct vnode *rootvp,
93 				struct fid *, struct vnode **);
94 
95 #if !defined (__DragonFly__)
96 static int	ntfs_quotactl (struct mount *, int, uid_t, caddr_t,
97 				   struct thread *);
98 static int	ntfs_start (struct mount *, int, struct thread *);
99 static int	ntfs_sync (struct mount *, int, struct ucred *,
100 			       struct thread *);
101 #endif
102 
103 #if defined(__DragonFly__)
104 struct sockaddr;
105 static int	ntfs_mount (struct mount *, char *, caddr_t, struct ucred *);
106 static int	ntfs_init (struct vfsconf *);
107 static int	ntfs_checkexp (struct mount *, struct sockaddr *,
108 				   int *, struct ucred **);
109 #elif defined(__NetBSD__)
110 static int	ntfs_mount (struct mount *, const char *, void *,
111 				struct nameidata *, struct thread *);
112 static void	ntfs_init (void);
113 static int	ntfs_mountroot (void);
114 static int	ntfs_sysctl (int *, u_int, void *, size_t *, void *,
115 				 size_t, struct thread *);
116 static int	ntfs_checkexp (struct mount *, struct mbuf *,
117 				   int *, struct ucred **);
118 #endif
119 
120 /*
121  * Verify a remote client has export rights and return these rights via.
122  * exflagsp and credanonp.
123  */
124 static int
125 ntfs_checkexp(struct mount *mp,
126 #if defined(__DragonFly__)
127 	      struct sockaddr *nam,
128 #else /* defined(__NetBSD__) */
129 	      struct mbuf *nam,
130 #endif
131 	      int *exflagsp, struct ucred **credanonp)
132 {
133 	struct netcred *np;
134 	struct ntfsmount *ntm = VFSTONTFS(mp);
135 
136 	/*
137 	 * Get the export permission structure for this <mp, client> tuple.
138 	 */
139 	np = vfs_export_lookup(mp, &ntm->ntm_export, nam);
140 	if (np == NULL)
141 		return (EACCES);
142 
143 	*exflagsp = np->netc_exflags;
144 	*credanonp = &np->netc_anon;
145 	return (0);
146 }
147 
148 #if defined(__NetBSD__)
149 /*ARGSUSED*/
150 static int
151 ntfs_sysctl(int *name, u_int namelen, void *oldp, size_t *oldlenp, void *newp,
152 	    size_t newlen, struct thread *td)
153 {
154 	return (EINVAL);
155 }
156 
157 static int
158 ntfs_mountroot(void)
159 {
160 	struct mount *mp;
161 	struct vnode *rootvp;
162 	struct thread *td = curthread;	/* XXX */
163 	struct ntfs_args args;
164 	int error;
165 
166 	if (root_device->dv_class != DV_DISK)
167 		return (ENODEV);
168 
169 	/*
170 	 * Get vnodes for rootdev.
171 	 */
172 	if (bdevvp(rootdev, &rootvp))
173 		panic("ntfs_mountroot: can't setup rootvp");
174 
175 	if ((error = vfs_rootmountalloc(MOUNT_NTFS, "root_device", &mp))) {
176 		vrele(rootvp);
177 		return (error);
178 	}
179 
180 	args.flag = 0;
181 	args.uid = 0;
182 	args.gid = 0;
183 	args.mode = 0777;
184 
185 	if ((error = ntfs_mountfs(rootvp, mp, &args, proc0.p_ucred)) != 0) {
186 		mp->mnt_op->vfs_refcount--;
187 		vfs_unbusy(mp);
188 		kfree(mp, M_MOUNT);
189 		vrele(rootvp);
190 		return (error);
191 	}
192 	mountlist_insert(mp, MNTINS_LAST);
193 	(void)ntfs_statfs(mp, &mp->mnt_stat, proc0.p_ucred);
194 	vfs_unbusy(mp);
195 	return (0);
196 }
197 
198 static void
199 ntfs_init(void)
200 {
201 	ntfs_nthashinit();
202 	ntfs_toupper_init();
203 }
204 
205 #elif defined(__DragonFly__)
206 
207 static int
208 ntfs_init(struct vfsconf *vcp)
209 {
210 	ntfs_nthashinit();
211 	ntfs_toupper_init();
212 	return 0;
213 }
214 
215 #endif /* NetBSD */
216 
217 static int
218 ntfs_mount(struct mount *mp, char *path, caddr_t data, struct ucred *cred)
219 {
220 	size_t		size;
221 	int		error;
222 	struct vnode	*devvp;
223 	struct ntfs_args args;
224 	struct nlookupdata nd;
225 	struct vnode *rootvp;
226 
227 	error = 0;
228 #ifdef __DragonFly__
229 	/*
230 	 * Use NULL path to flag a root mount
231 	 */
232 	if( path == NULL) {
233 		/*
234 		 ***
235 		 * Mounting root file system
236 		 ***
237 		 */
238 
239 		/* Get vnode for root device*/
240 		if( bdevvp( rootdev, &rootvp))
241 			panic("ffs_mountroot: can't setup bdevvp for root");
242 
243 		/*
244 		 * FS specific handling
245 		 */
246 		mp->mnt_flag |= MNT_RDONLY;	/* XXX globally applicable?*/
247 
248 		/*
249 		 * Attempt mount
250 		 */
251 		if( ( error = ntfs_mountfs(rootvp, mp, &args, cred)) != 0) {
252 			/* fs specific cleanup (if any)*/
253 			goto error_1;
254 		}
255 
256 		goto dostatfs;		/* success*/
257 
258 	}
259 #endif /* FreeBSD */
260 
261 	/*
262 	 ***
263 	 * Mounting non-root file system or updating a file system
264 	 ***
265 	 */
266 
267 	/* copy in user arguments*/
268 	error = copyin(data, (caddr_t)&args, sizeof (struct ntfs_args));
269 	if (error)
270 		goto error_1;		/* can't get arguments*/
271 
272 	/*
273 	 * If updating, check whether changing from read-only to
274 	 * read/write; if there is no device name, that's all we do.
275 	 */
276 	if (mp->mnt_flag & MNT_UPDATE) {
277 		/* if not updating name...*/
278 		if (args.fspec == NULL) {
279 			/*
280 			 * Process export requests.  Jumping to "success"
281 			 * will return the vfs_export() error code.
282 			 */
283 			struct ntfsmount *ntm = VFSTONTFS(mp);
284 			error = vfs_export(mp, &ntm->ntm_export, &args.export);
285 			goto success;
286 		}
287 
288 		kprintf("ntfs_mount(): MNT_UPDATE not supported\n");
289 		error = EINVAL;
290 		goto error_1;
291 	}
292 
293 	/*
294 	 * Not an update, or updating the name: look up the name
295 	 * and verify that it refers to a sensible block device.
296 	 */
297 	devvp = NULL;
298 	error = nlookup_init(&nd, args.fspec, UIO_USERSPACE, NLC_FOLLOW);
299 	if (error == 0)
300 		error = nlookup(&nd);
301 	if (error == 0)
302 		error = cache_vref(&nd.nl_nch, nd.nl_cred, &devvp);
303 	nlookup_done(&nd);
304 	if (error)
305 		goto error_1;
306 
307 #if defined(__DragonFly__)
308 	if (!vn_isdisk(devvp, &error))
309 		goto error_2;
310 #else
311 	if (devvp->v_type != VBLK) {
312 		error = ENOTBLK;
313 		goto error_2;
314 	}
315 	if (devvp->v_umajor >= nblkdev) {
316 		error = ENXIO;
317 		goto error_2;
318 	}
319 #endif
320 	if (mp->mnt_flag & MNT_UPDATE) {
321 #if 0
322 		/*
323 		 ********************
324 		 * UPDATE
325 		 ********************
326 		 */
327 
328 		if (devvp != ntmp->um_devvp)
329 			error = EINVAL;	/* needs translation */
330 		else
331 			vrele(devvp);
332 		/*
333 		 * Update device name only on success
334 		 */
335 		if( !error) {
336 			/* Save "mounted from" info for mount point (NULL pad)*/
337 			copyinstr(	args.fspec,
338 					mp->mnt_stat.f_mntfromname,
339 					MNAMELEN - 1,
340 					&size);
341 			bzero( mp->mnt_stat.f_mntfromname + size, MNAMELEN - size);
342 		}
343 #endif
344 	} else {
345 		/*
346 		 ********************
347 		 * NEW MOUNT
348 		 ********************
349 		 */
350 
351 		/* Save "mounted from" info for mount point (NULL pad)*/
352 		copyinstr(	args.fspec,			/* device name*/
353 				mp->mnt_stat.f_mntfromname,	/* save area*/
354 				MNAMELEN - 1,			/* max size*/
355 				&size);				/* real size*/
356 		bzero( mp->mnt_stat.f_mntfromname + size, MNAMELEN - size);
357 
358 		error = ntfs_mountfs(devvp, mp, &args, cred);
359 	}
360 	if (error) {
361 		goto error_2;
362 	}
363 
364 #ifdef __DragonFly__
365 dostatfs:
366 #endif
367 	/*
368 	 * Initialize FS stat information in mount struct; uses
369 	 * mp->mnt_stat.f_mntfromname.
370 	 *
371 	 * This code is common to root and non-root mounts
372 	 */
373 	(void)VFS_STATFS(mp, &mp->mnt_stat, cred);
374 
375 	goto success;
376 
377 
378 error_2:	/* error with devvp held*/
379 
380 	/* release devvp before failing*/
381 	vrele(devvp);
382 
383 error_1:	/* no state to back out*/
384 
385 success:
386 	return(error);
387 }
388 
389 /*
390  * Common code for mount and mountroot
391  */
392 int
393 ntfs_mountfs(struct vnode *devvp, struct mount *mp, struct ntfs_args *argsp,
394 	     struct ucred *cred)
395 {
396 	struct buf *bp;
397 	struct ntfsmount *ntmp;
398 	cdev_t dev;
399 	int error, ronly, ncount, i;
400 	struct vnode *vp;
401 	char cs_local[ICONV_CSNMAXLEN];
402 	char cs_ntfs[ICONV_CSNMAXLEN];
403 
404 	/*
405 	 * Disallow multiple mounts of the same device.
406 	 * Disallow mounting of a device that is currently in use
407 	 * (except for root, which might share swap device for miniroot).
408 	 * Flush out any old buffers remaining from a previous use.
409 	 */
410 	error = vfs_mountedon(devvp);
411 	if (error)
412 		return (error);
413 	ncount = vcount(devvp);
414 #if defined(__DragonFly__)
415 	if (devvp->v_object)
416 		ncount -= 1;
417 #endif
418 	if (ncount > 1)
419 		return (EBUSY);
420 #if defined(__DragonFly__)
421 	VN_LOCK(devvp, LK_EXCLUSIVE | LK_RETRY);
422 	error = vinvalbuf(devvp, V_SAVE, 0, 0);
423 	VOP__UNLOCK(devvp, 0);
424 #else
425 	error = vinvalbuf(devvp, V_SAVE, 0, 0);
426 #endif
427 	if (error)
428 		return (error);
429 
430 	ronly = (mp->mnt_flag & MNT_RDONLY) != 0;
431 	VN_LOCK(devvp, LK_EXCLUSIVE | LK_RETRY);
432 	error = VOP_OPEN(devvp, ronly ? FREAD : FREAD|FWRITE, FSCRED, NULL);
433 	VOP__UNLOCK(devvp, 0);
434 	if (error)
435 		return (error);
436 	dev = devvp->v_rdev;
437 
438 	bp = NULL;
439 
440 	error = bread(devvp, BBLOCK, BBSIZE, &bp);
441 	if (error)
442 		goto out;
443 	ntmp = kmalloc(sizeof *ntmp, M_NTFSMNT, M_WAITOK | M_ZERO);
444 	bcopy( bp->b_data, &ntmp->ntm_bootfile, sizeof(struct bootfile) );
445 	brelse( bp );
446 	bp = NULL;
447 
448 	if (strncmp(ntmp->ntm_bootfile.bf_sysid, NTFS_BBID, NTFS_BBIDLEN)) {
449 		error = EINVAL;
450 		dprintf(("ntfs_mountfs: invalid boot block\n"));
451 		goto out;
452 	}
453 
454 	{
455 		int8_t cpr = ntmp->ntm_mftrecsz;
456 		if( cpr > 0 )
457 			ntmp->ntm_bpmftrec = ntmp->ntm_spc * cpr;
458 		else
459 			ntmp->ntm_bpmftrec = (1 << (-cpr)) / ntmp->ntm_bps;
460 	}
461 	dprintf(("ntfs_mountfs(): bps: %d, spc: %d, media: %x, mftrecsz: %d (%d sects)\n",
462 		ntmp->ntm_bps,ntmp->ntm_spc,ntmp->ntm_bootfile.bf_media,
463 		ntmp->ntm_mftrecsz,ntmp->ntm_bpmftrec));
464 	dprintf(("ntfs_mountfs(): mftcn: 0x%x|0x%x\n",
465 		(u_int32_t)ntmp->ntm_mftcn,(u_int32_t)ntmp->ntm_mftmirrcn));
466 
467 	ntmp->ntm_mountp = mp;
468 	ntmp->ntm_dev = dev;
469 	ntmp->ntm_devvp = devvp;
470 	ntmp->ntm_uid = argsp->uid;
471 	ntmp->ntm_gid = argsp->gid;
472 	ntmp->ntm_mode = argsp->mode;
473 	ntmp->ntm_flag = argsp->flag;
474 
475 	if (argsp->flag & NTFS_MFLAG_KICONV && ntfs_iconv) {
476 		bcopy(argsp->cs_local, cs_local, sizeof(cs_local));
477 		bcopy(argsp->cs_ntfs, cs_ntfs, sizeof(cs_ntfs));
478 		ntfs_82u_init(ntmp, cs_local, cs_ntfs);
479 		ntfs_u28_init(ntmp, NULL, cs_local, cs_ntfs);
480 	} else {
481 		ntfs_82u_init(ntmp, NULL, NULL);
482 		ntfs_u28_init(ntmp, ntmp->ntm_82u, NULL, NULL);
483 	}
484 
485 	mp->mnt_data = (qaddr_t)ntmp;
486 
487 	dprintf(("ntfs_mountfs(): case-%s,%s uid: %d, gid: %d, mode: %o\n",
488 		(ntmp->ntm_flag & NTFS_MFLAG_CASEINS)?"insens.":"sens.",
489 		(ntmp->ntm_flag & NTFS_MFLAG_ALLNAMES)?" allnames,":"",
490 		ntmp->ntm_uid, ntmp->ntm_gid, ntmp->ntm_mode));
491 
492 	vfs_add_vnodeops(mp, &ntfs_vnode_vops, &mp->mnt_vn_norm_ops);
493 
494 	/*
495 	 * We read in some system nodes to do not allow
496 	 * reclaim them and to have everytime access to them.
497 	 */
498 	{
499 		int pi[3] = { NTFS_MFTINO, NTFS_ROOTINO, NTFS_BITMAPINO };
500 		for (i=0; i<3; i++) {
501 			error = VFS_VGET(mp, NULL,
502 					 pi[i], &(ntmp->ntm_sysvn[pi[i]]));
503 			if(error)
504 				goto out1;
505 			vsetflags(ntmp->ntm_sysvn[pi[i]], VSYSTEM);
506 			vref(ntmp->ntm_sysvn[pi[i]]);
507 			vput(ntmp->ntm_sysvn[pi[i]]);
508 		}
509 	}
510 
511 	/* read the Unicode lowercase --> uppercase translation table,
512 	 * if necessary */
513 	if ((error = ntfs_toupper_use(mp, ntmp)))
514 		goto out1;
515 
516 	/*
517 	 * Scan $BitMap and count free clusters
518 	 */
519 	error = ntfs_calccfree(ntmp, &ntmp->ntm_cfree);
520 	if(error)
521 		goto out1;
522 
523 	/*
524 	 * Read and translate to internal format attribute
525 	 * definition file.
526 	 */
527 	{
528 		int num,j;
529 		struct attrdef ad;
530 
531 		/* Open $AttrDef */
532 		error = VFS_VGET(mp, NULL, NTFS_ATTRDEFINO, &vp);
533 		if(error)
534 			goto out1;
535 
536 		/* Count valid entries */
537 		for(num=0;;num++) {
538 			error = ntfs_readattr(ntmp, VTONT(vp),
539 					NTFS_A_DATA, NULL,
540 					num * sizeof(ad), sizeof(ad),
541 					&ad, NULL);
542 			if (error)
543 				goto out1;
544 			if (ad.ad_name[0] == 0)
545 				break;
546 		}
547 
548 		/* Alloc memory for attribute definitions */
549 		ntmp->ntm_ad = kmalloc(num * sizeof(struct ntvattrdef),
550 				       M_NTFSMNT, M_WAITOK);
551 
552 		ntmp->ntm_adnum = num;
553 
554 		/* Read them and translate */
555 		for(i=0;i<num;i++){
556 			error = ntfs_readattr(ntmp, VTONT(vp),
557 					NTFS_A_DATA, NULL,
558 					i * sizeof(ad), sizeof(ad),
559 					&ad, NULL);
560 			if (error)
561 				goto out1;
562 			j = 0;
563 			do {
564 				ntmp->ntm_ad[i].ad_name[j] = ad.ad_name[j];
565 			} while(ad.ad_name[j++]);
566 			ntmp->ntm_ad[i].ad_namelen = j - 1;
567 			ntmp->ntm_ad[i].ad_type = ad.ad_type;
568 		}
569 
570 		vput(vp);
571 	}
572 
573 #if defined(__DragonFly__)
574 	mp->mnt_stat.f_fsid.val[0] = dev2udev(dev);
575 	mp->mnt_stat.f_fsid.val[1] = mp->mnt_vfc->vfc_typenum;
576 #else
577 	mp->mnt_stat.f_fsid.val[0] = dev;
578 	mp->mnt_stat.f_fsid.val[1] = makefstype(MOUNT_NTFS);
579 #endif
580 	mp->mnt_maxsymlinklen = 0;
581 	mp->mnt_flag |= MNT_LOCAL;
582 	dev->si_mountpoint = mp;
583 
584 	return (0);
585 
586 out1:
587 	for(i=0;i<NTFS_SYSNODESNUM;i++)
588 		if(ntmp->ntm_sysvn[i]) vrele(ntmp->ntm_sysvn[i]);
589 
590 	if (vflush(mp, 0, 0))
591 		dprintf(("ntfs_mountfs: vflush failed\n"));
592 
593 out:
594 	dev->si_mountpoint = NULL;
595 	if (bp)
596 		brelse(bp);
597 
598 #if defined __NetBSD__
599 	/* lock the device vnode before calling VOP_CLOSE() */
600 	VN_LOCK(devvp, LK_EXCLUSIVE | LK_RETRY);
601 	(void)VOP_CLOSE(devvp, ronly ? FREAD : FREAD|FWRITE);
602 	VOP__UNLOCK(devvp, 0);
603 #else
604 	(void)VOP_CLOSE(devvp, ronly ? FREAD : FREAD|FWRITE);
605 #endif
606 
607 	return (error);
608 }
609 
610 #if !defined(__DragonFly__)
611 static int
612 ntfs_start(struct mount *mp, int flags, struct thread *td)
613 {
614 	return (0);
615 }
616 #endif
617 
618 static int
619 ntfs_unmount(struct mount *mp, int mntflags)
620 {
621 	struct ntfsmount *ntmp;
622 	int error, ronly, flags, i;
623 
624 	dprintf(("ntfs_unmount: unmounting...\n"));
625 	ntmp = VFSTONTFS(mp);
626 
627 	ronly = (mp->mnt_flag & MNT_RDONLY) != 0;
628 	flags = 0;
629 	if(mntflags & MNT_FORCE)
630 		flags |= FORCECLOSE;
631 
632 	dprintf(("ntfs_unmount: vflushing...\n"));
633 	error = vflush(mp, 0, flags | SKIPSYSTEM);
634 	if (error) {
635 		kprintf("ntfs_unmount: vflush failed: %d\n",error);
636 		return (error);
637 	}
638 
639 	/* Check if only system vnodes are left */
640 	for(i=0;i<NTFS_SYSNODESNUM;i++)
641 		 if((ntmp->ntm_sysvn[i]) &&
642 		    (ntmp->ntm_sysvn[i]->v_sysref.refcnt > 1)) return (EBUSY);
643 
644 	/* Dereference all system vnodes */
645 	for(i=0;i<NTFS_SYSNODESNUM;i++)
646 		 if(ntmp->ntm_sysvn[i]) vrele(ntmp->ntm_sysvn[i]);
647 
648 	/* vflush system vnodes */
649 	error = vflush(mp, 0, flags);
650 	if (error)
651 		kprintf("ntfs_unmount: vflush failed(sysnodes): %d\n",error);
652 
653 	/* Check if the type of device node isn't VBAD before
654 	 * touching v_cdevinfo.  If the device vnode is revoked, the
655 	 * field is NULL and touching it causes null pointer derefercence.
656 	 */
657 	if (ntmp->ntm_devvp->v_type != VBAD)
658 		ntmp->ntm_devvp->v_rdev->si_mountpoint = NULL;
659 
660 	vinvalbuf(ntmp->ntm_devvp, V_SAVE, 0, 0);
661 
662 	error = VOP_CLOSE(ntmp->ntm_devvp, ronly ? FREAD : FREAD|FWRITE);
663 
664 	vrele(ntmp->ntm_devvp);
665 
666 	/* free the toupper table, if this has been last mounted ntfs volume */
667 	ntfs_toupper_unuse();
668 
669 	dprintf(("ntfs_umount: freeing memory...\n"));
670 	ntfs_u28_uninit(ntmp);
671 	ntfs_82u_uninit(ntmp);
672 	mp->mnt_data = (qaddr_t)0;
673 	mp->mnt_flag &= ~MNT_LOCAL;
674 	kfree(ntmp->ntm_ad, M_NTFSMNT);
675 	kfree(ntmp, M_NTFSMNT);
676 	return (error);
677 }
678 
679 static int
680 ntfs_root(struct mount *mp, struct vnode **vpp)
681 {
682 	struct vnode *nvp;
683 	int error = 0;
684 
685 	dprintf(("ntfs_root(): sysvn: %p\n",
686 		VFSTONTFS(mp)->ntm_sysvn[NTFS_ROOTINO]));
687 	error = VFS_VGET(mp, NULL, (ino_t)NTFS_ROOTINO, &nvp);
688 	if(error) {
689 		kprintf("ntfs_root: VFS_VGET failed: %d\n",error);
690 		return (error);
691 	}
692 
693 	*vpp = nvp;
694 	return (0);
695 }
696 
697 #if !defined(__DragonFly__)
698 static int
699 ntfs_quotactl(struct mount *mp, int cmds, uid_t uid, caddr_t arg,
700 	      struct thread *td)
701 {
702 	kprintf("\nntfs_quotactl():\n");
703 	return EOPNOTSUPP;
704 }
705 #endif
706 
707 int
708 ntfs_calccfree(struct ntfsmount *ntmp, cn_t *cfreep)
709 {
710 	struct vnode *vp;
711 	u_int8_t *tmp;
712 	int j, error;
713 	long cfree = 0;
714 	size_t bmsize, i;
715 
716 	vp = ntmp->ntm_sysvn[NTFS_BITMAPINO];
717 
718 	bmsize = VTOF(vp)->f_size;
719 
720 	tmp = kmalloc(bmsize, M_TEMP, M_WAITOK);
721 
722 	error = ntfs_readattr(ntmp, VTONT(vp), NTFS_A_DATA, NULL,
723 			       0, bmsize, tmp, NULL);
724 	if (error)
725 		goto out;
726 
727 	for(i=0;i<bmsize;i++)
728 		for(j=0;j<8;j++)
729 			if(~tmp[i] & (1 << j)) cfree++;
730 	*cfreep = cfree;
731 
732     out:
733 	kfree(tmp, M_TEMP);
734 	return(error);
735 }
736 
737 static int
738 ntfs_statfs(struct mount *mp, struct statfs *sbp, struct ucred *cred)
739 {
740 	struct ntfsmount *ntmp = VFSTONTFS(mp);
741 	u_int64_t mftsize,mftallocated;
742 
743 	dprintf(("ntfs_statfs():\n"));
744 
745 	mftsize = VTOF(ntmp->ntm_sysvn[NTFS_MFTINO])->f_size;
746 	mftallocated = VTOF(ntmp->ntm_sysvn[NTFS_MFTINO])->f_allocated;
747 
748 #if defined(__DragonFly__)
749 	sbp->f_type = mp->mnt_vfc->vfc_typenum;
750 #elif defined(__NetBSD__)
751 	sbp->f_type = 0;
752 #else
753 	sbp->f_type = MOUNT_NTFS;
754 #endif
755 	sbp->f_bsize = ntmp->ntm_bps;
756 	sbp->f_iosize = ntmp->ntm_bps * ntmp->ntm_spc;
757 	sbp->f_blocks = ntmp->ntm_bootfile.bf_spv;
758 	sbp->f_bfree = sbp->f_bavail = ntfs_cntobn(ntmp->ntm_cfree);
759 	sbp->f_ffree = sbp->f_bfree / ntmp->ntm_bpmftrec;
760 	sbp->f_files = mftallocated / ntfs_bntob(ntmp->ntm_bpmftrec) +
761 		       sbp->f_ffree;
762 	if (sbp != &mp->mnt_stat) {
763 		bcopy((caddr_t)mp->mnt_stat.f_mntfromname,
764 			(caddr_t)&sbp->f_mntfromname[0], MNAMELEN);
765 	}
766 	sbp->f_flags = mp->mnt_flag;
767 #ifdef __NetBSD__
768 	strncpy(sbp->f_fstypename, mp->mnt_op->vfs_name, MFSNAMELEN);
769 #endif
770 
771 	return (0);
772 }
773 
774 #if !defined(__DragonFly__)
775 static int
776 ntfs_sync(struct mount *mp, int waitfor, struct ucred *cred, struct thread *td)
777 {
778 	/*dprintf(("ntfs_sync():\n"));*/
779 	return (0);
780 }
781 #endif
782 
783 /*ARGSUSED*/
784 static int
785 ntfs_fhtovp(struct mount *mp, struct vnode *rootvp,
786 	    struct fid *fhp, struct vnode **vpp)
787 {
788 	struct vnode *nvp;
789 	struct ntfid *ntfhp = (struct ntfid *)fhp;
790 	int error;
791 
792 	ddprintf(("ntfs_fhtovp(): %d\n", ntfhp->ntfid_ino));
793 
794 	if ((error = VFS_VGET(mp, NULL, ntfhp->ntfid_ino, &nvp)) != 0) {
795 		*vpp = NULLVP;
796 		return (error);
797 	}
798 	/* XXX as unlink/rmdir/mkdir/creat are not currently possible
799 	 * with NTFS, we don't need to check anything else for now */
800 	*vpp = nvp;
801 
802 	return (0);
803 }
804 
805 static int
806 ntfs_vptofh(struct vnode *vp, struct fid *fhp)
807 {
808 	struct ntnode *ntp;
809 	struct ntfid *ntfhp;
810 
811 	ddprintf(("ntfs_fhtovp(): %p\n", vp));
812 
813 	ntp = VTONT(vp);
814 	ntfhp = (struct ntfid *)fhp;
815 	ntfhp->ntfid_len = sizeof(struct ntfid);
816 	ntfhp->ntfid_ino = ntp->i_number;
817 	/* ntfhp->ntfid_gen = ntp->i_gen; */
818 	return (0);
819 }
820 
821 int
822 ntfs_vgetex(struct mount *mp, ino_t ino, u_int32_t attrtype, char *attrname,
823 	    u_long lkflags, u_long flags, struct thread *td,
824 	    struct vnode **vpp)
825 {
826 	int error;
827 	struct ntfsmount *ntmp;
828 	struct ntnode *ip;
829 	struct fnode *fp;
830 	struct vnode *vp;
831 	enum vtype f_type;
832 
833 	dprintf(("ntfs_vgetex: ino: %ju, attr: 0x%x:%s, lkf: 0x%lx, f: 0x%lx\n",
834 		(uintmax_t) ino, attrtype, attrname?attrname:"", lkflags, flags));
835 
836 	ntmp = VFSTONTFS(mp);
837 	*vpp = NULL;
838 
839 	/* Get ntnode */
840 	error = ntfs_ntlookup(ntmp, ino, &ip);
841 	if (error) {
842 		kprintf("ntfs_vget: ntfs_ntget failed\n");
843 		return (error);
844 	}
845 
846 	/* It may be not initialized fully, so force load it */
847 	if (!(flags & VG_DONTLOADIN) && !(ip->i_flag & IN_LOADED)) {
848 		error = ntfs_loadntnode(ntmp, ip);
849 		if(error) {
850 			kprintf("ntfs_vget: CAN'T LOAD ATTRIBUTES FOR INO: %"PRId64"\n",
851 			       ip->i_number);
852 			ntfs_ntput(ip);
853 			return (error);
854 		}
855 	}
856 
857 	error = ntfs_fget(ntmp, ip, attrtype, attrname, &fp);
858 	if (error) {
859 		kprintf("ntfs_vget: ntfs_fget failed\n");
860 		ntfs_ntput(ip);
861 		return (error);
862 	}
863 
864 	f_type = VINT;
865 	if (!(flags & VG_DONTVALIDFN) && !(fp->f_flag & FN_VALID)) {
866 		if ((ip->i_frflag & NTFS_FRFLAG_DIR) &&
867 		    (fp->f_attrtype == NTFS_A_DATA && fp->f_attrname == NULL)) {
868 			f_type = VDIR;
869 		} else if (flags & VG_EXT) {
870 			f_type = VINT;
871 			fp->f_size = fp->f_allocated = 0;
872 		} else {
873 			f_type = VREG;
874 
875 			error = ntfs_filesize(ntmp, fp,
876 					      &fp->f_size, &fp->f_allocated);
877 			if (error) {
878 				ntfs_ntput(ip);
879 				return (error);
880 			}
881 		}
882 
883 		fp->f_flag |= FN_VALID;
884 	}
885 
886 	if (FTOV(fp)) {
887 		VGET(FTOV(fp), lkflags);
888 		*vpp = FTOV(fp);
889 		ntfs_ntput(ip);
890 		return (0);
891 	}
892 
893 	error = getnewvnode(VT_NTFS, ntmp->ntm_mountp, &vp, VLKTIMEOUT, 0);
894 	if(error) {
895 		ntfs_frele(fp);
896 		ntfs_ntput(ip);
897 		return (error);
898 	}
899 	dprintf(("ntfs_vget: vnode: %p for ntnode: %ju\n", vp, (uintmax_t)ino));
900 
901 	fp->f_vp = vp;
902 	vp->v_data = fp;
903 	vp->v_type = f_type;
904 
905 	if (ino == NTFS_ROOTINO)
906 		vsetflags(vp, VROOT);
907 
908 	/*
909 	 * Normal files use the buffer cache
910 	 */
911 	if (f_type == VREG)
912 		vinitvmio(vp, fp->f_size, PAGE_SIZE, -1);
913 
914 	ntfs_ntput(ip);
915 
916 	KKASSERT(lkflags & LK_TYPE_MASK);
917 	/* XXX leave vnode locked exclusively from getnewvnode */
918 	*vpp = vp;
919 	return (0);
920 
921 }
922 
923 static int
924 ntfs_vget(struct mount *mp, struct vnode *dvp, ino_t ino, struct vnode **vpp)
925 {
926 	return ntfs_vgetex(mp, ino, NTFS_A_DATA, NULL,
927 			LK_EXCLUSIVE | LK_RETRY, 0, curthread, vpp);
928 }
929 
930 #if defined(__DragonFly__)
931 static struct vfsops ntfs_vfsops = {
932 	.vfs_mount =    	ntfs_mount,
933 	.vfs_unmount =   	ntfs_unmount,
934 	.vfs_root =     	ntfs_root,
935 	.vfs_statfs =   	ntfs_statfs,
936 	.vfs_sync =      	vfs_stdsync,
937 	.vfs_vget =     	ntfs_vget,
938 	.vfs_fhtovp =   	ntfs_fhtovp,
939 	.vfs_checkexp =   	ntfs_checkexp,
940 	.vfs_vptofh =   	ntfs_vptofh,
941 	.vfs_init =     	ntfs_init,
942 	.vfs_uninit =   	ntfs_nthash_uninit /* see ntfs_ihash.c */
943 };
944 VFS_SET(ntfs_vfsops, ntfs, 0);
945 MODULE_VERSION(ntfs, 1);
946 #elif defined(__NetBSD__)
947 extern struct vnodeopv_desc ntfs_vnodeop_opv_desc;
948 
949 struct vnodeopv_desc *ntfs_vnodeopv_descs[] = {
950 	&ntfs_vnodeop_opv_desc,
951 	NULL,
952 };
953 
954 struct vfsops ntfs_vfsops = {
955 	MOUNT_NTFS,
956 	ntfs_mount,
957 	ntfs_start,
958 	ntfs_unmount,
959 	ntfs_root,
960 	ntfs_quotactl,
961 	ntfs_statfs,
962 	ntfs_sync,
963 	ntfs_vget,
964 	ntfs_fhtovp,
965 	ntfs_vptofh,
966 	ntfs_init,
967 	ntfs_sysctl,
968 	ntfs_mountroot,
969 	ntfs_checkexp,
970 	ntfs_vnodeopv_descs,
971 };
972 #else /* !NetBSD && !FreeBSD */
973 static struct vfsops ntfs_vfsops = {
974 	ntfs_mount,
975 	ntfs_start,
976 	ntfs_unmount,
977 	ntfs_root,
978 	ntfs_quotactl,
979 	ntfs_statfs,
980 	ntfs_sync,
981 	ntfs_vget,
982 	ntfs_fhtovp,
983 	ntfs_vptofh,
984 	ntfs_init,
985 };
986 VFS_SET(ntfs_vfsops, ntfs, MOUNT_NTFS, 0);
987 #endif
988 
989 
990