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