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