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