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