xref: /dragonfly/sys/vfs/ntfs/ntfs_vfsops.c (revision fb151170)
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 /*#define NTFS_DEBUG 1*/
67 #include "ntfs.h"
68 #include "ntfs_inode.h"
69 #include "ntfs_subr.h"
70 #include "ntfs_vfsops.h"
71 #include "ntfs_ihash.h"
72 #include "ntfsmount.h"
73 
74 extern struct vop_ops ntfs_vnode_vops;
75 
76 #if defined(__DragonFly__)
77 MALLOC_DEFINE(M_NTFSMNT, "NTFS mount", "NTFS mount structure");
78 MALLOC_DEFINE(M_NTFSNTNODE,"NTFS ntnode",  "NTFS ntnode information");
79 MALLOC_DEFINE(M_NTFSFNODE,"NTFS fnode",  "NTFS fnode information");
80 MALLOC_DEFINE(M_NTFSDIR,"NTFS dir",  "NTFS dir buffer");
81 #endif
82 
83 struct iconv_functions *ntfs_iconv = NULL;
84 
85 static int	ntfs_root (struct mount *, struct vnode **);
86 static int	ntfs_statfs (struct mount *, struct statfs *, struct ucred *cred);
87 static int	ntfs_unmount (struct mount *, int);
88 static int	ntfs_vget (struct mount *mp, struct vnode *dvp,
89 				ino_t ino, struct vnode **vpp);
90 static int	ntfs_mountfs (struct vnode *, struct mount *,
91 				struct ntfs_args *, struct ucred *);
92 static int	ntfs_vptofh (struct vnode *, struct fid *);
93 static int	ntfs_fhtovp (struct mount *, struct vnode *rootvp,
94 				struct fid *, struct vnode **);
95 
96 #if !defined (__DragonFly__)
97 static int	ntfs_quotactl (struct mount *, int, uid_t, caddr_t,
98 				   struct thread *);
99 static int	ntfs_start (struct mount *, int, struct thread *);
100 static int	ntfs_sync (struct mount *, int, struct ucred *,
101 			       struct thread *);
102 #endif
103 
104 #if defined(__DragonFly__)
105 struct sockaddr;
106 static int	ntfs_mount (struct mount *, char *, caddr_t, struct ucred *);
107 static int	ntfs_init (struct vfsconf *);
108 static int	ntfs_checkexp (struct mount *, struct sockaddr *,
109 				   int *, struct ucred **);
110 #elif defined(__NetBSD__)
111 static int	ntfs_mount (struct mount *, const char *, void *,
112 				struct nameidata *, struct thread *);
113 static void	ntfs_init (void);
114 static int	ntfs_mountroot (void);
115 static int	ntfs_sysctl (int *, u_int, void *, size_t *, void *,
116 				 size_t, struct thread *);
117 static int	ntfs_checkexp (struct mount *, struct mbuf *,
118 				   int *, struct ucred **);
119 #endif
120 
121 /*
122  * Verify a remote client has export rights and return these rights via.
123  * exflagsp and credanonp.
124  */
125 static int
126 ntfs_checkexp(struct mount *mp,
127 #if defined(__DragonFly__)
128 	      struct sockaddr *nam,
129 #else /* defined(__NetBSD__) */
130 	      struct mbuf *nam,
131 #endif
132 	      int *exflagsp, struct ucred **credanonp)
133 {
134 	struct netcred *np;
135 	struct ntfsmount *ntm = VFSTONTFS(mp);
136 
137 	/*
138 	 * Get the export permission structure for this <mp, client> tuple.
139 	 */
140 	np = vfs_export_lookup(mp, &ntm->ntm_export, nam);
141 	if (np == NULL)
142 		return (EACCES);
143 
144 	*exflagsp = np->netc_exflags;
145 	*credanonp = &np->netc_anon;
146 	return (0);
147 }
148 
149 #if defined(__NetBSD__)
150 /*ARGSUSED*/
151 static int
152 ntfs_sysctl(int *name, u_int namelen, void *oldp, size_t *oldlenp, void *newp,
153 	    size_t newlen, struct thread *td)
154 {
155 	return (EINVAL);
156 }
157 
158 static int
159 ntfs_mountroot(void)
160 {
161 	struct mount *mp;
162 	struct vnode *rootvp;
163 	struct thread *td = curthread;	/* XXX */
164 	struct ntfs_args args;
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 			vsetflags(ntmp->ntm_sysvn[pi[i]], 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 		ntmp->ntm_ad = kmalloc(num * sizeof(struct ntvattrdef),
551 				       M_NTFSMNT, M_WAITOK);
552 
553 		ntmp->ntm_adnum = num;
554 
555 		/* Read them and translate */
556 		for(i=0;i<num;i++){
557 			error = ntfs_readattr(ntmp, VTONT(vp),
558 					NTFS_A_DATA, NULL,
559 					i * sizeof(ad), sizeof(ad),
560 					&ad, NULL);
561 			if (error)
562 				goto out1;
563 			j = 0;
564 			do {
565 				ntmp->ntm_ad[i].ad_name[j] = ad.ad_name[j];
566 			} while(ad.ad_name[j++]);
567 			ntmp->ntm_ad[i].ad_namelen = j - 1;
568 			ntmp->ntm_ad[i].ad_type = ad.ad_type;
569 		}
570 
571 		vput(vp);
572 	}
573 
574 #if defined(__DragonFly__)
575 	mp->mnt_stat.f_fsid.val[0] = dev2udev(dev);
576 	mp->mnt_stat.f_fsid.val[1] = mp->mnt_vfc->vfc_typenum;
577 #else
578 	mp->mnt_stat.f_fsid.val[0] = dev;
579 	mp->mnt_stat.f_fsid.val[1] = makefstype(MOUNT_NTFS);
580 #endif
581 	mp->mnt_maxsymlinklen = 0;
582 	mp->mnt_flag |= MNT_LOCAL;
583 	dev->si_mountpoint = mp;
584 
585 	return (0);
586 
587 out1:
588 	for(i=0;i<NTFS_SYSNODESNUM;i++)
589 		if(ntmp->ntm_sysvn[i]) vrele(ntmp->ntm_sysvn[i]);
590 
591 	if (vflush(mp, 0, 0))
592 		dprintf(("ntfs_mountfs: vflush failed\n"));
593 
594 out:
595 	dev->si_mountpoint = NULL;
596 	if (bp)
597 		brelse(bp);
598 
599 #if defined __NetBSD__
600 	/* lock the device vnode before calling VOP_CLOSE() */
601 	VN_LOCK(devvp, LK_EXCLUSIVE | LK_RETRY);
602 	(void)VOP_CLOSE(devvp, ronly ? FREAD : FREAD|FWRITE);
603 	VOP__UNLOCK(devvp, 0);
604 #else
605 	(void)VOP_CLOSE(devvp, ronly ? FREAD : FREAD|FWRITE);
606 #endif
607 
608 	return (error);
609 }
610 
611 #if !defined(__DragonFly__)
612 static int
613 ntfs_start(struct mount *mp, int flags, struct thread *td)
614 {
615 	return (0);
616 }
617 #endif
618 
619 static int
620 ntfs_unmount(struct mount *mp, int mntflags)
621 {
622 	struct ntfsmount *ntmp;
623 	int error, ronly, flags, i;
624 
625 	dprintf(("ntfs_unmount: unmounting...\n"));
626 	ntmp = VFSTONTFS(mp);
627 
628 	ronly = (mp->mnt_flag & MNT_RDONLY) != 0;
629 	flags = 0;
630 	if(mntflags & MNT_FORCE)
631 		flags |= FORCECLOSE;
632 
633 	dprintf(("ntfs_unmount: vflushing...\n"));
634 	error = vflush(mp, 0, flags | SKIPSYSTEM);
635 	if (error) {
636 		kprintf("ntfs_unmount: vflush failed: %d\n",error);
637 		return (error);
638 	}
639 
640 	/* Check if only system vnodes are left */
641 	for(i=0;i<NTFS_SYSNODESNUM;i++)
642 		 if((ntmp->ntm_sysvn[i]) &&
643 		    (ntmp->ntm_sysvn[i]->v_sysref.refcnt > 1)) return (EBUSY);
644 
645 	/* Dereference all system vnodes */
646 	for(i=0;i<NTFS_SYSNODESNUM;i++)
647 		 if(ntmp->ntm_sysvn[i]) vrele(ntmp->ntm_sysvn[i]);
648 
649 	/* vflush system vnodes */
650 	error = vflush(mp, 0, flags);
651 	if (error)
652 		kprintf("ntfs_unmount: vflush failed(sysnodes): %d\n",error);
653 
654 	/* Check if the type of device node isn't VBAD before
655 	 * touching v_cdevinfo.  If the device vnode is revoked, the
656 	 * field is NULL and touching it causes null pointer derefercence.
657 	 */
658 	if (ntmp->ntm_devvp->v_type != VBAD)
659 		ntmp->ntm_devvp->v_rdev->si_mountpoint = NULL;
660 
661 	vinvalbuf(ntmp->ntm_devvp, V_SAVE, 0, 0);
662 
663 	error = VOP_CLOSE(ntmp->ntm_devvp, ronly ? FREAD : FREAD|FWRITE);
664 
665 	vrele(ntmp->ntm_devvp);
666 
667 	/* free the toupper table, if this has been last mounted ntfs volume */
668 	ntfs_toupper_unuse();
669 
670 	dprintf(("ntfs_umount: freeing memory...\n"));
671 	ntfs_u28_uninit(ntmp);
672 	ntfs_82u_uninit(ntmp);
673 	mp->mnt_data = (qaddr_t)0;
674 	mp->mnt_flag &= ~MNT_LOCAL;
675 	kfree(ntmp->ntm_ad, M_NTFSMNT);
676 	kfree(ntmp, M_NTFSMNT);
677 	return (error);
678 }
679 
680 static int
681 ntfs_root(struct mount *mp, struct vnode **vpp)
682 {
683 	struct vnode *nvp;
684 	int error = 0;
685 
686 	dprintf(("ntfs_root(): sysvn: %p\n",
687 		VFSTONTFS(mp)->ntm_sysvn[NTFS_ROOTINO]));
688 	error = VFS_VGET(mp, NULL, (ino_t)NTFS_ROOTINO, &nvp);
689 	if(error) {
690 		kprintf("ntfs_root: VFS_VGET failed: %d\n",error);
691 		return (error);
692 	}
693 
694 	*vpp = nvp;
695 	return (0);
696 }
697 
698 #if !defined(__DragonFly__)
699 static int
700 ntfs_quotactl(struct mount *mp, int cmds, uid_t uid, caddr_t arg,
701 	      struct thread *td)
702 {
703 	kprintf("\nntfs_quotactl():\n");
704 	return EOPNOTSUPP;
705 }
706 #endif
707 
708 int
709 ntfs_calccfree(struct ntfsmount *ntmp, cn_t *cfreep)
710 {
711 	struct vnode *vp;
712 	u_int8_t *tmp;
713 	int j, error;
714 	long cfree = 0;
715 	size_t bmsize, i;
716 
717 	vp = ntmp->ntm_sysvn[NTFS_BITMAPINO];
718 
719 	bmsize = VTOF(vp)->f_size;
720 
721 	tmp = kmalloc(bmsize, M_TEMP, M_WAITOK);
722 
723 	error = ntfs_readattr(ntmp, VTONT(vp), NTFS_A_DATA, NULL,
724 			       0, bmsize, tmp, NULL);
725 	if (error)
726 		goto out;
727 
728 	for(i=0;i<bmsize;i++)
729 		for(j=0;j<8;j++)
730 			if(~tmp[i] & (1 << j)) cfree++;
731 	*cfreep = cfree;
732 
733     out:
734 	kfree(tmp, M_TEMP);
735 	return(error);
736 }
737 
738 static int
739 ntfs_statfs(struct mount *mp, struct statfs *sbp, struct ucred *cred)
740 {
741 	struct ntfsmount *ntmp = VFSTONTFS(mp);
742 	u_int64_t mftsize,mftallocated;
743 
744 	dprintf(("ntfs_statfs():\n"));
745 
746 	mftsize = VTOF(ntmp->ntm_sysvn[NTFS_MFTINO])->f_size;
747 	mftallocated = VTOF(ntmp->ntm_sysvn[NTFS_MFTINO])->f_allocated;
748 
749 #if defined(__DragonFly__)
750 	sbp->f_type = mp->mnt_vfc->vfc_typenum;
751 #elif defined(__NetBSD__)
752 	sbp->f_type = 0;
753 #else
754 	sbp->f_type = MOUNT_NTFS;
755 #endif
756 	sbp->f_bsize = ntmp->ntm_bps;
757 	sbp->f_iosize = ntmp->ntm_bps * ntmp->ntm_spc;
758 	sbp->f_blocks = ntmp->ntm_bootfile.bf_spv;
759 	sbp->f_bfree = sbp->f_bavail = ntfs_cntobn(ntmp->ntm_cfree);
760 	sbp->f_ffree = sbp->f_bfree / ntmp->ntm_bpmftrec;
761 	sbp->f_files = mftallocated / ntfs_bntob(ntmp->ntm_bpmftrec) +
762 		       sbp->f_ffree;
763 	if (sbp != &mp->mnt_stat) {
764 		bcopy((caddr_t)mp->mnt_stat.f_mntfromname,
765 			(caddr_t)&sbp->f_mntfromname[0], MNAMELEN);
766 	}
767 	sbp->f_flags = mp->mnt_flag;
768 #ifdef __NetBSD__
769 	strncpy(sbp->f_fstypename, mp->mnt_op->vfs_name, MFSNAMELEN);
770 #endif
771 
772 	return (0);
773 }
774 
775 #if !defined(__DragonFly__)
776 static int
777 ntfs_sync(struct mount *mp, int waitfor, struct ucred *cred, struct thread *td)
778 {
779 	/*dprintf(("ntfs_sync():\n"));*/
780 	return (0);
781 }
782 #endif
783 
784 /*ARGSUSED*/
785 static int
786 ntfs_fhtovp(struct mount *mp, struct vnode *rootvp,
787 	    struct fid *fhp, struct vnode **vpp)
788 {
789 	struct vnode *nvp;
790 	struct ntfid *ntfhp = (struct ntfid *)fhp;
791 	int error;
792 
793 	ddprintf(("ntfs_fhtovp(): %d\n", ntfhp->ntfid_ino));
794 
795 	if ((error = VFS_VGET(mp, NULL, ntfhp->ntfid_ino, &nvp)) != 0) {
796 		*vpp = NULLVP;
797 		return (error);
798 	}
799 	/* XXX as unlink/rmdir/mkdir/creat are not currently possible
800 	 * with NTFS, we don't need to check anything else for now */
801 	*vpp = nvp;
802 
803 	return (0);
804 }
805 
806 static int
807 ntfs_vptofh(struct vnode *vp, struct fid *fhp)
808 {
809 	struct ntnode *ntp;
810 	struct ntfid *ntfhp;
811 
812 	ddprintf(("ntfs_fhtovp(): %p\n", vp));
813 
814 	ntp = VTONT(vp);
815 	ntfhp = (struct ntfid *)fhp;
816 	ntfhp->ntfid_len = sizeof(struct ntfid);
817 	ntfhp->ntfid_ino = ntp->i_number;
818 	/* ntfhp->ntfid_gen = ntp->i_gen; */
819 	return (0);
820 }
821 
822 int
823 ntfs_vgetex(struct mount *mp, ino_t ino, u_int32_t attrtype, char *attrname,
824 	    u_long lkflags, u_long flags, struct thread *td,
825 	    struct vnode **vpp)
826 {
827 	int error;
828 	struct ntfsmount *ntmp;
829 	struct ntnode *ip;
830 	struct fnode *fp;
831 	struct vnode *vp;
832 	enum vtype f_type;
833 
834 	dprintf(("ntfs_vgetex: ino: %d, attr: 0x%x:%s, lkf: 0x%lx, f: 0x%lx\n",
835 		ino, attrtype, attrname?attrname:"", (u_long)lkflags,
836 		(u_long)flags ));
837 
838 	ntmp = VFSTONTFS(mp);
839 	*vpp = NULL;
840 
841 	/* Get ntnode */
842 	error = ntfs_ntlookup(ntmp, ino, &ip);
843 	if (error) {
844 		kprintf("ntfs_vget: ntfs_ntget failed\n");
845 		return (error);
846 	}
847 
848 	/* It may be not initialized fully, so force load it */
849 	if (!(flags & VG_DONTLOADIN) && !(ip->i_flag & IN_LOADED)) {
850 		error = ntfs_loadntnode(ntmp, ip);
851 		if(error) {
852 			kprintf("ntfs_vget: CAN'T LOAD ATTRIBUTES FOR INO: %"PRId64"\n",
853 			       ip->i_number);
854 			ntfs_ntput(ip);
855 			return (error);
856 		}
857 	}
858 
859 	error = ntfs_fget(ntmp, ip, attrtype, attrname, &fp);
860 	if (error) {
861 		kprintf("ntfs_vget: ntfs_fget failed\n");
862 		ntfs_ntput(ip);
863 		return (error);
864 	}
865 
866 	f_type = VINT;
867 	if (!(flags & VG_DONTVALIDFN) && !(fp->f_flag & FN_VALID)) {
868 		if ((ip->i_frflag & NTFS_FRFLAG_DIR) &&
869 		    (fp->f_attrtype == NTFS_A_DATA && fp->f_attrname == NULL)) {
870 			f_type = VDIR;
871 		} else if (flags & VG_EXT) {
872 			f_type = VINT;
873 			fp->f_size = fp->f_allocated = 0;
874 		} else {
875 			f_type = VREG;
876 
877 			error = ntfs_filesize(ntmp, fp,
878 					      &fp->f_size, &fp->f_allocated);
879 			if (error) {
880 				ntfs_ntput(ip);
881 				return (error);
882 			}
883 		}
884 
885 		fp->f_flag |= FN_VALID;
886 	}
887 
888 	if (FTOV(fp)) {
889 		VGET(FTOV(fp), lkflags);
890 		*vpp = FTOV(fp);
891 		ntfs_ntput(ip);
892 		return (0);
893 	}
894 
895 	error = getnewvnode(VT_NTFS, ntmp->ntm_mountp, &vp, VLKTIMEOUT, 0);
896 	if(error) {
897 		ntfs_frele(fp);
898 		ntfs_ntput(ip);
899 		return (error);
900 	}
901 	dprintf(("ntfs_vget: vnode: %p for ntnode: %d\n", vp,ino));
902 
903 	fp->f_vp = vp;
904 	vp->v_data = fp;
905 	vp->v_type = f_type;
906 
907 	if (ino == NTFS_ROOTINO)
908 		vsetflags(vp, VROOT);
909 
910 	/*
911 	 * Normal files use the buffer cache
912 	 */
913 	if (f_type == VREG)
914 		vinitvmio(vp, fp->f_size, PAGE_SIZE, -1);
915 
916 	ntfs_ntput(ip);
917 
918 	KKASSERT(lkflags & LK_TYPE_MASK);
919 	/* XXX leave vnode locked exclusively from getnewvnode */
920 	*vpp = vp;
921 	return (0);
922 
923 }
924 
925 static int
926 ntfs_vget(struct mount *mp, struct vnode *dvp, ino_t ino, struct vnode **vpp)
927 {
928 	return ntfs_vgetex(mp, ino, NTFS_A_DATA, NULL,
929 			LK_EXCLUSIVE | LK_RETRY, 0, curthread, vpp);
930 }
931 
932 #if defined(__DragonFly__)
933 static struct vfsops ntfs_vfsops = {
934 	.vfs_mount =    	ntfs_mount,
935 	.vfs_unmount =   	ntfs_unmount,
936 	.vfs_root =     	ntfs_root,
937 	.vfs_statfs =   	ntfs_statfs,
938 	.vfs_sync =      	vfs_stdsync,
939 	.vfs_vget =     	ntfs_vget,
940 	.vfs_fhtovp =   	ntfs_fhtovp,
941 	.vfs_checkexp =   	ntfs_checkexp,
942 	.vfs_vptofh =   	ntfs_vptofh,
943 	.vfs_init =     	ntfs_init,
944 	.vfs_uninit =   	ntfs_nthash_uninit /* see ntfs_ihash.c */
945 };
946 VFS_SET(ntfs_vfsops, ntfs, 0);
947 MODULE_VERSION(ntfs, 1);
948 #elif defined(__NetBSD__)
949 extern struct vnodeopv_desc ntfs_vnodeop_opv_desc;
950 
951 struct vnodeopv_desc *ntfs_vnodeopv_descs[] = {
952 	&ntfs_vnodeop_opv_desc,
953 	NULL,
954 };
955 
956 struct vfsops ntfs_vfsops = {
957 	MOUNT_NTFS,
958 	ntfs_mount,
959 	ntfs_start,
960 	ntfs_unmount,
961 	ntfs_root,
962 	ntfs_quotactl,
963 	ntfs_statfs,
964 	ntfs_sync,
965 	ntfs_vget,
966 	ntfs_fhtovp,
967 	ntfs_vptofh,
968 	ntfs_init,
969 	ntfs_sysctl,
970 	ntfs_mountroot,
971 	ntfs_checkexp,
972 	ntfs_vnodeopv_descs,
973 };
974 #else /* !NetBSD && !FreeBSD */
975 static struct vfsops ntfs_vfsops = {
976 	ntfs_mount,
977 	ntfs_start,
978 	ntfs_unmount,
979 	ntfs_root,
980 	ntfs_quotactl,
981 	ntfs_statfs,
982 	ntfs_sync,
983 	ntfs_vget,
984 	ntfs_fhtovp,
985 	ntfs_vptofh,
986 	ntfs_init,
987 };
988 VFS_SET(ntfs_vfsops, ntfs, MOUNT_NTFS, 0);
989 #endif
990 
991 
992