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