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