xref: /netbsd-src/sys/fs/ntfs/ntfs_vfsops.c (revision 0bac62655e97d9a3b86dd2669185f4ffb896a53c)
1 /*	$NetBSD: ntfs_vfsops.c,v 1.97 2014/11/13 16:51:53 hannken 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.97 2014/11/13 16:51:53 hannken 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/sysctl.h>
45 #include <sys/device.h>
46 #include <sys/conf.h>
47 #include <sys/kauth.h>
48 #include <sys/module.h>
49 
50 #include <uvm/uvm_extern.h>
51 
52 #include <miscfs/genfs/genfs.h>
53 #include <miscfs/specfs/specdev.h>
54 
55 #include <fs/ntfs/ntfs.h>
56 #include <fs/ntfs/ntfs_inode.h>
57 #include <fs/ntfs/ntfs_subr.h>
58 #include <fs/ntfs/ntfs_vfsops.h>
59 #include <fs/ntfs/ntfs_ihash.h>
60 #include <fs/ntfs/ntfsmount.h>
61 
62 MODULE(MODULE_CLASS_VFS, ntfs, NULL);
63 
64 MALLOC_JUSTDEFINE(M_NTFSMNT, "NTFS mount", "NTFS mount structure");
65 MALLOC_JUSTDEFINE(M_NTFSNTNODE,"NTFS ntnode",  "NTFS ntnode information");
66 MALLOC_JUSTDEFINE(M_NTFSDIR,"NTFS dir",  "NTFS dir buffer");
67 
68 static int	ntfs_mount(struct mount *, const char *, void *, size_t *);
69 static int	ntfs_root(struct mount *, struct vnode **);
70 static int	ntfs_start(struct mount *, int);
71 static int	ntfs_statvfs(struct mount *, struct statvfs *);
72 static int	ntfs_sync(struct mount *, int, kauth_cred_t);
73 static int	ntfs_unmount(struct mount *, int);
74 static int	ntfs_vget(struct mount *mp, ino_t ino,
75 			       struct vnode **vpp);
76 static int	ntfs_loadvnode(struct mount *, struct vnode *,
77 		                    const void *, size_t, const void **);
78 static int	ntfs_mountfs(struct vnode *, struct mount *,
79 				  struct ntfs_args *, struct lwp *);
80 static int	ntfs_vptofh(struct vnode *, struct fid *, size_t *);
81 
82 static void     ntfs_init(void);
83 static void     ntfs_reinit(void);
84 static void     ntfs_done(void);
85 static int      ntfs_fhtovp(struct mount *, struct fid *,
86 				struct vnode **);
87 static int      ntfs_mountroot(void);
88 
89 static const struct genfs_ops ntfs_genfsops = {
90 	.gop_write = genfs_compat_gop_write,
91 };
92 
93 static struct sysctllog *ntfs_sysctl_log;
94 
95 static int
96 ntfs_mountroot(void)
97 {
98 	struct mount *mp;
99 	struct lwp *l = curlwp;	/* XXX */
100 	int error;
101 	struct ntfs_args args;
102 
103 	if (device_class(root_device) != DV_DISK)
104 		return (ENODEV);
105 
106 	if ((error = vfs_rootmountalloc(MOUNT_NTFS, "root_device", &mp))) {
107 		vrele(rootvp);
108 		return (error);
109 	}
110 
111 	args.flag = 0;
112 	args.uid = 0;
113 	args.gid = 0;
114 	args.mode = 0777;
115 
116 	if ((error = ntfs_mountfs(rootvp, mp, &args, l)) != 0) {
117 		vfs_unbusy(mp, false, NULL);
118 		vfs_destroy(mp);
119 		return (error);
120 	}
121 
122 	mountlist_append(mp);
123 	(void)ntfs_statvfs(mp, &mp->mnt_stat);
124 	vfs_unbusy(mp, false, NULL);
125 	return (0);
126 }
127 
128 static void
129 ntfs_init(void)
130 {
131 
132 	malloc_type_attach(M_NTFSMNT);
133 	malloc_type_attach(M_NTFSNTNODE);
134 	malloc_type_attach(M_NTFSDIR);
135 	malloc_type_attach(M_NTFSNTVATTR);
136 	malloc_type_attach(M_NTFSRDATA);
137 	malloc_type_attach(M_NTFSDECOMP);
138 	malloc_type_attach(M_NTFSRUN);
139 	ntfs_nthashinit();
140 	ntfs_toupper_init();
141 }
142 
143 static void
144 ntfs_reinit(void)
145 {
146 	ntfs_nthashreinit();
147 }
148 
149 static void
150 ntfs_done(void)
151 {
152 	ntfs_nthashdone();
153 	malloc_type_detach(M_NTFSMNT);
154 	malloc_type_detach(M_NTFSNTNODE);
155 	malloc_type_detach(M_NTFSDIR);
156 	malloc_type_detach(M_NTFSNTVATTR);
157 	malloc_type_detach(M_NTFSRDATA);
158 	malloc_type_detach(M_NTFSDECOMP);
159 	malloc_type_detach(M_NTFSRUN);
160 }
161 
162 static int
163 ntfs_mount (
164 	struct mount *mp,
165 	const char *path,
166 	void *data,
167 	size_t *data_len)
168 {
169 	struct lwp *l = curlwp;
170 	int		err = 0, flags;
171 	struct vnode	*devvp;
172 	struct ntfs_args *args = data;
173 
174 	if (args == NULL)
175 		return EINVAL;
176 	if (*data_len < sizeof *args)
177 		return EINVAL;
178 
179 	if (mp->mnt_flag & MNT_GETARGS) {
180 		struct ntfsmount *ntmp = VFSTONTFS(mp);
181 		if (ntmp == NULL)
182 			return EIO;
183 		args->fspec = NULL;
184 		args->uid = ntmp->ntm_uid;
185 		args->gid = ntmp->ntm_gid;
186 		args->mode = ntmp->ntm_mode;
187 		args->flag = ntmp->ntm_flag;
188 		*data_len = sizeof *args;
189 		return 0;
190 	}
191 	/*
192 	 ***
193 	 * Mounting non-root file system or updating a file system
194 	 ***
195 	 */
196 
197 	/*
198 	 * If updating, check whether changing from read-only to
199 	 * read/write; if there is no device name, that's all we do.
200 	 */
201 	if (mp->mnt_flag & MNT_UPDATE) {
202 		printf("ntfs_mount(): MNT_UPDATE not supported\n");
203 		return (EINVAL);
204 	}
205 
206 	/*
207 	 * Not an update, or updating the name: look up the name
208 	 * and verify that it refers to a sensible block device.
209 	 */
210 	err = namei_simple_user(args->fspec,
211 				NSM_FOLLOW_NOEMULROOT, &devvp);
212 	if (err) {
213 		/* can't get devvp!*/
214 		return (err);
215 	}
216 
217 	if (devvp->v_type != VBLK) {
218 		err = ENOTBLK;
219 		goto fail;
220 	}
221 	if (bdevsw_lookup(devvp->v_rdev) == NULL) {
222 		err = ENXIO;
223 		goto fail;
224 	}
225 	if (mp->mnt_flag & MNT_UPDATE) {
226 #if 0
227 		/*
228 		 ********************
229 		 * UPDATE
230 		 ********************
231 		 */
232 
233 		if (devvp != ntmp->um_devvp) {
234 			err = EINVAL;	/* needs translation */
235 			goto fail;
236 		}
237 
238 		/*
239 		 * Update device name only on success
240 		 */
241 		err = set_statvfs_info(NULL, UIO_USERSPACE, args->fspec,
242 		    UIO_USERSPACE, mp->mnt_op->vfs_name, mp, p);
243 		if (err)
244 			goto fail;
245 
246 		vrele(devvp);
247 #endif
248 	} else {
249 		/*
250 		 ********************
251 		 * NEW MOUNT
252 		 ********************
253 		 */
254 
255 		/*
256 		 * Since this is a new mount, we want the names for
257 		 * the device and the mount point copied in.  If an
258 		 * error occurs,  the mountpoint is discarded by the
259 		 * upper level code.
260 		 */
261 
262 		/* Save "last mounted on" info for mount point (NULL pad)*/
263 		err = set_statvfs_info(path, UIO_USERSPACE, args->fspec,
264 		    UIO_USERSPACE, mp->mnt_op->vfs_name, mp, l);
265 		if (err)
266 			goto fail;
267 
268 		if (mp->mnt_flag & MNT_RDONLY)
269 			flags = FREAD;
270 		else
271 			flags = FREAD|FWRITE;
272 		vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
273 		err = VOP_OPEN(devvp, flags, FSCRED);
274 		VOP_UNLOCK(devvp);
275 		if (err)
276 			goto fail;
277 		err = ntfs_mountfs(devvp, mp, args, l);
278 		if (err) {
279 			vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
280 			(void)VOP_CLOSE(devvp, flags, NOCRED);
281 			VOP_UNLOCK(devvp);
282 			goto fail;
283 		}
284 	}
285 
286 	/*
287 	 * Initialize FS stat information in mount struct; uses both
288 	 * mp->mnt_stat.f_mntonname and mp->mnt_stat.f_mntfromname
289 	 *
290 	 * This code is common to root and non-root mounts
291 	 */
292 	(void)VFS_STATVFS(mp, &mp->mnt_stat);
293 	return (err);
294 
295 fail:
296 	vrele(devvp);
297 	return (err);
298 }
299 
300 /*
301  * Common code for mount and mountroot
302  */
303 int
304 ntfs_mountfs(struct vnode *devvp, struct mount *mp, struct ntfs_args *argsp, struct lwp *l)
305 {
306 	struct buf *bp;
307 	struct ntfsmount *ntmp;
308 	dev_t dev = devvp->v_rdev;
309 	int error, i;
310 	struct vnode *vp;
311 
312 	ntmp = NULL;
313 
314 	/*
315 	 * Flush out any old buffers remaining from a previous use.
316 	 */
317 	vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
318 	error = vinvalbuf(devvp, V_SAVE, l->l_cred, l, 0, 0);
319 	VOP_UNLOCK(devvp);
320 	if (error)
321 		return (error);
322 
323 	bp = NULL;
324 
325 	error = bread(devvp, BBLOCK, BBSIZE, NOCRED, 0, &bp);
326 	if (error)
327 		goto out;
328 	ntmp = malloc( sizeof *ntmp, M_NTFSMNT, M_WAITOK|M_ZERO);
329 	memcpy( &ntmp->ntm_bootfile,  bp->b_data, sizeof(struct bootfile) );
330 	brelse( bp , 0 );
331 	bp = NULL;
332 
333 	if (strncmp(ntmp->ntm_bootfile.bf_sysid, NTFS_BBID, NTFS_BBIDLEN)) {
334 		error = EINVAL;
335 		dprintf(("ntfs_mountfs: invalid boot block\n"));
336 		goto out;
337 	}
338 
339 	{
340 		int8_t cpr = ntmp->ntm_mftrecsz;
341 		if( cpr > 0 )
342 			ntmp->ntm_bpmftrec = ntmp->ntm_spc * cpr;
343 		else
344 			ntmp->ntm_bpmftrec = (1 << (-cpr)) / ntmp->ntm_bps;
345 	}
346 	dprintf(("ntfs_mountfs(): bps: %d, spc: %d, media: %x, mftrecsz: %d (%d sects)\n",
347 		ntmp->ntm_bps,ntmp->ntm_spc,ntmp->ntm_bootfile.bf_media,
348 		ntmp->ntm_mftrecsz,ntmp->ntm_bpmftrec));
349 	dprintf(("ntfs_mountfs(): mftcn: 0x%x|0x%x\n",
350 		(u_int32_t)ntmp->ntm_mftcn,(u_int32_t)ntmp->ntm_mftmirrcn));
351 
352 	ntmp->ntm_mountp = mp;
353 	ntmp->ntm_dev = dev;
354 	ntmp->ntm_devvp = devvp;
355 	ntmp->ntm_uid = argsp->uid;
356 	ntmp->ntm_gid = argsp->gid;
357 	ntmp->ntm_mode = argsp->mode;
358 	ntmp->ntm_flag = argsp->flag;
359 	mp->mnt_data = ntmp;
360 
361 	/* set file name encode/decode hooks XXX utf-8 only for now */
362 	ntmp->ntm_wget = ntfs_utf8_wget;
363 	ntmp->ntm_wput = ntfs_utf8_wput;
364 	ntmp->ntm_wcmp = ntfs_utf8_wcmp;
365 
366 	dprintf(("ntfs_mountfs(): case-%s,%s uid: %d, gid: %d, mode: %o\n",
367 		(ntmp->ntm_flag & NTFS_MFLAG_CASEINS)?"insens.":"sens.",
368 		(ntmp->ntm_flag & NTFS_MFLAG_ALLNAMES)?" allnames,":"",
369 		ntmp->ntm_uid, ntmp->ntm_gid, ntmp->ntm_mode));
370 
371 	/*
372 	 * We read in some system nodes to do not allow
373 	 * reclaim them and to have everytime access to them.
374 	 */
375 	{
376 		int pi[3] = { NTFS_MFTINO, NTFS_ROOTINO, NTFS_BITMAPINO };
377 		for (i=0; i<3; i++) {
378 			error = VFS_VGET(mp, pi[i], &(ntmp->ntm_sysvn[pi[i]]));
379 			if(error)
380 				goto out1;
381 			ntmp->ntm_sysvn[pi[i]]->v_vflag |= VV_SYSTEM;
382 			vref(ntmp->ntm_sysvn[pi[i]]);
383 			vput(ntmp->ntm_sysvn[pi[i]]);
384 		}
385 	}
386 
387 	/* read the Unicode lowercase --> uppercase translation table,
388 	 * if necessary */
389 	if ((error = ntfs_toupper_use(mp, ntmp)))
390 		goto out1;
391 
392 	/*
393 	 * Scan $BitMap and count free clusters
394 	 */
395 	error = ntfs_calccfree(ntmp, &ntmp->ntm_cfree);
396 	if(error)
397 		goto out1;
398 
399 	/*
400 	 * Read and translate to internal format attribute
401 	 * definition file.
402 	 */
403 	{
404 		int num,j;
405 		struct attrdef ad;
406 
407 		/* Open $AttrDef */
408 		error = VFS_VGET(mp, NTFS_ATTRDEFINO, &vp );
409 		if(error)
410 			goto out1;
411 
412 		/* Count valid entries */
413 		for(num=0;;num++) {
414 			error = ntfs_readattr(ntmp, VTONT(vp),
415 					NTFS_A_DATA, NULL,
416 					num * sizeof(ad), sizeof(ad),
417 					&ad, NULL);
418 			if (error)
419 				goto out1;
420 			if (ad.ad_name[0] == 0)
421 				break;
422 		}
423 
424 		/* Alloc memory for attribute definitions */
425 		ntmp->ntm_ad = (struct ntvattrdef *) malloc(
426 			num * sizeof(struct ntvattrdef),
427 			M_NTFSMNT, M_WAITOK);
428 
429 		ntmp->ntm_adnum = num;
430 
431 		/* Read them and translate */
432 		for(i=0;i<num;i++){
433 			error = ntfs_readattr(ntmp, VTONT(vp),
434 					NTFS_A_DATA, NULL,
435 					i * sizeof(ad), sizeof(ad),
436 					&ad, NULL);
437 			if (error)
438 				goto out1;
439 			j = 0;
440 			do {
441 				ntmp->ntm_ad[i].ad_name[j] = ad.ad_name[j];
442 			} while(ad.ad_name[j++]);
443 			ntmp->ntm_ad[i].ad_namelen = j - 1;
444 			ntmp->ntm_ad[i].ad_type = ad.ad_type;
445 		}
446 
447 		vput(vp);
448 	}
449 
450 	mp->mnt_stat.f_fsidx.__fsid_val[0] = dev;
451 	mp->mnt_stat.f_fsidx.__fsid_val[1] = makefstype(MOUNT_NTFS);
452 	mp->mnt_stat.f_fsid = mp->mnt_stat.f_fsidx.__fsid_val[0];
453 	mp->mnt_stat.f_namemax = NTFS_MAXFILENAME;
454 	mp->mnt_flag |= MNT_LOCAL;
455 	spec_node_setmountedfs(devvp, mp);
456 	return (0);
457 
458 out1:
459 	for(i=0;i<NTFS_SYSNODESNUM;i++)
460 		if(ntmp->ntm_sysvn[i]) vrele(ntmp->ntm_sysvn[i]);
461 
462 	if (vflush(mp,NULLVP,0)) {
463 		dprintf(("ntfs_mountfs: vflush failed\n"));
464 	}
465 out:
466 	spec_node_setmountedfs(devvp, NULL);
467 	if (bp)
468 		brelse(bp, 0);
469 
470 	if (error) {
471 		if (ntmp) {
472 			if (ntmp->ntm_ad)
473 				free(ntmp->ntm_ad, M_NTFSMNT);
474 			free(ntmp, M_NTFSMNT);
475 		}
476 	}
477 
478 	return (error);
479 }
480 
481 static int
482 ntfs_start (
483 	struct mount *mp,
484 	int flags)
485 {
486 	return (0);
487 }
488 
489 static int
490 ntfs_unmount(
491 	struct mount *mp,
492 	int mntflags)
493 {
494 	struct lwp *l = curlwp;
495 	struct ntfsmount *ntmp;
496 	int error, ronly = 0, flags, i;
497 
498 	dprintf(("ntfs_unmount: unmounting...\n"));
499 	ntmp = VFSTONTFS(mp);
500 
501 	flags = 0;
502 	if(mntflags & MNT_FORCE)
503 		flags |= FORCECLOSE;
504 
505 	dprintf(("ntfs_unmount: vflushing...\n"));
506 	error = vflush(mp,NULLVP,flags | SKIPSYSTEM);
507 	if (error) {
508 		dprintf(("ntfs_unmount: vflush failed: %d\n",error));
509 		return (error);
510 	}
511 
512 	/* Check if only system vnodes are rest */
513 	for(i=0;i<NTFS_SYSNODESNUM;i++)
514 		 if((ntmp->ntm_sysvn[i]) &&
515 		    (ntmp->ntm_sysvn[i]->v_usecount > 1)) return (EBUSY);
516 
517 	/* Dereference all system vnodes */
518 	for(i=0;i<NTFS_SYSNODESNUM;i++)
519 		 if(ntmp->ntm_sysvn[i]) vrele(ntmp->ntm_sysvn[i]);
520 
521 	/* vflush system vnodes */
522 	error = vflush(mp,NULLVP,flags);
523 	if (error) {
524 		panic("ntfs_unmount: vflush failed(sysnodes): %d\n",error);
525 	}
526 
527 	/* Check if the type of device node isn't VBAD before
528 	 * touching v_specinfo.  If the device vnode is revoked, the
529 	 * field is NULL and touching it causes null pointer derefercence.
530 	 */
531 	if (ntmp->ntm_devvp->v_type != VBAD)
532 		spec_node_setmountedfs(ntmp->ntm_devvp, NULL);
533 
534 	error = vinvalbuf(ntmp->ntm_devvp, V_SAVE, NOCRED, l, 0, 0);
535 	KASSERT(error == 0);
536 
537 	/* lock the device vnode before calling VOP_CLOSE() */
538 	vn_lock(ntmp->ntm_devvp, LK_EXCLUSIVE | LK_RETRY);
539 	error = VOP_CLOSE(ntmp->ntm_devvp, ronly ? FREAD : FREAD|FWRITE,
540 		NOCRED);
541 	KASSERT(error == 0);
542 	VOP_UNLOCK(ntmp->ntm_devvp);
543 
544 	vrele(ntmp->ntm_devvp);
545 
546 	/* free the toupper table, if this has been last mounted ntfs volume */
547 	ntfs_toupper_unuse();
548 
549 	dprintf(("ntfs_umount: freeing memory...\n"));
550 	mp->mnt_data = NULL;
551 	mp->mnt_flag &= ~MNT_LOCAL;
552 	free(ntmp->ntm_ad, M_NTFSMNT);
553 	free(ntmp, M_NTFSMNT);
554 	return (0);
555 }
556 
557 static int
558 ntfs_root(
559 	struct mount *mp,
560 	struct vnode **vpp)
561 {
562 	struct vnode *nvp;
563 	int error = 0;
564 
565 	dprintf(("ntfs_root(): sysvn: %p\n",
566 		VFSTONTFS(mp)->ntm_sysvn[NTFS_ROOTINO]));
567 	error = VFS_VGET(mp, (ino_t)NTFS_ROOTINO, &nvp);
568 	if(error) {
569 		printf("ntfs_root: VFS_VGET failed: %d\n",error);
570 		return (error);
571 	}
572 
573 	*vpp = nvp;
574 	return (0);
575 }
576 
577 int
578 ntfs_calccfree(
579 	struct ntfsmount *ntmp,
580 	cn_t *cfreep)
581 {
582 	struct vnode *vp;
583 	u_int8_t *tmp;
584 	int j, error;
585 	cn_t cfree = 0;
586 	size_t bmsize, i;
587 
588 	vp = ntmp->ntm_sysvn[NTFS_BITMAPINO];
589 
590 	bmsize = VTOF(vp)->f_size;
591 
592 	tmp = (u_int8_t *) malloc(bmsize, M_TEMP, M_WAITOK);
593 
594 	error = ntfs_readattr(ntmp, VTONT(vp), NTFS_A_DATA, NULL,
595 			       0, bmsize, tmp, NULL);
596 	if (error)
597 		goto out;
598 
599 	for(i=0;i<bmsize;i++)
600 		for(j=0;j<8;j++)
601 			if(~tmp[i] & (1 << j)) cfree++;
602 	*cfreep = cfree;
603 
604     out:
605 	free(tmp, M_TEMP);
606 	return(error);
607 }
608 
609 static int
610 ntfs_statvfs(
611 	struct mount *mp,
612 	struct statvfs *sbp)
613 {
614 	struct ntfsmount *ntmp = VFSTONTFS(mp);
615 	u_int64_t mftallocated;
616 
617 	dprintf(("ntfs_statvfs():\n"));
618 
619 	mftallocated = VTOF(ntmp->ntm_sysvn[NTFS_MFTINO])->f_allocated;
620 
621 	sbp->f_bsize = ntmp->ntm_bps;
622 	sbp->f_frsize = sbp->f_bsize; /* XXX */
623 	sbp->f_iosize = ntmp->ntm_bps * ntmp->ntm_spc;
624 	sbp->f_blocks = ntmp->ntm_bootfile.bf_spv;
625 	sbp->f_bfree = sbp->f_bavail = ntfs_cntobn(ntmp->ntm_cfree);
626 	sbp->f_ffree = sbp->f_favail = sbp->f_bfree / ntmp->ntm_bpmftrec;
627 	sbp->f_files = mftallocated / ntfs_bntob(ntmp->ntm_bpmftrec) +
628 	    sbp->f_ffree;
629 	sbp->f_fresvd = sbp->f_bresvd = 0; /* XXX */
630 	sbp->f_flag = mp->mnt_flag;
631 	copy_statvfs_info(sbp, mp);
632 	return (0);
633 }
634 
635 static int
636 ntfs_sync (
637 	struct mount *mp,
638 	int waitfor,
639 	kauth_cred_t cred)
640 {
641 	/*dprintf(("ntfs_sync():\n"));*/
642 	return (0);
643 }
644 
645 /*ARGSUSED*/
646 static int
647 ntfs_fhtovp(
648 	struct mount *mp,
649 	struct fid *fhp,
650 	struct vnode **vpp)
651 {
652 	struct ntfid ntfh;
653 	int error;
654 
655 	if (fhp->fid_len != sizeof(struct ntfid))
656 		return EINVAL;
657 	memcpy(&ntfh, fhp, sizeof(ntfh));
658 	ddprintf(("ntfs_fhtovp(): %s: %llu\n", mp->mnt_stat.f_mntonname,
659 	    (unsigned long long)ntfh.ntfid_ino));
660 
661 	error = ntfs_vgetex(mp, ntfh.ntfid_ino, ntfh.ntfid_attr, "",
662 			LK_EXCLUSIVE, vpp);
663 	if (error != 0) {
664 		*vpp = NULLVP;
665 		return (error);
666 	}
667 
668 	/* XXX as unlink/rmdir/mkdir/creat are not currently possible
669 	 * with NTFS, we don't need to check anything else for now */
670 	return (0);
671 }
672 
673 static int
674 ntfs_vptofh(
675 	struct vnode *vp,
676 	struct fid *fhp,
677 	size_t *fh_size)
678 {
679 	struct ntnode *ntp;
680 	struct ntfid ntfh;
681 	struct fnode *fn;
682 
683 	if (*fh_size < sizeof(struct ntfid)) {
684 		*fh_size = sizeof(struct ntfid);
685 		return E2BIG;
686 	}
687 	*fh_size = sizeof(struct ntfid);
688 
689 	ddprintf(("ntfs_fhtovp(): %s: %p\n", vp->v_mount->mnt_stat.f_mntonname,
690 		vp));
691 
692 	fn = VTOF(vp);
693 	ntp = VTONT(vp);
694 	memset(&ntfh, 0, sizeof(ntfh));
695 	ntfh.ntfid_len = sizeof(struct ntfid);
696 	ntfh.ntfid_ino = ntp->i_number;
697 	ntfh.ntfid_attr = fn->f_attrtype;
698 #ifdef notyet
699 	ntfh.ntfid_gen = ntp->i_gen;
700 #endif
701 	memcpy(fhp, &ntfh, sizeof(ntfh));
702 	return (0);
703 }
704 
705 static int
706 ntfs_loadvnode(struct mount *mp, struct vnode *vp,
707     const void *key, size_t key_len, const void **new_key)
708 {
709 	int error;
710 	struct ntvattr *vap;
711 	struct ntkey small_key, *ntkey;
712 	struct ntfsmount *ntmp;
713 	struct ntnode *ip;
714 	struct fnode *fp = NULL;
715 	enum vtype f_type = VBAD;
716 
717 	if (key_len <= sizeof(small_key))
718 		ntkey = &small_key;
719 	else
720 		ntkey = kmem_alloc(key_len, KM_SLEEP);
721 	memcpy(ntkey, key, key_len);
722 
723 	dprintf(("ntfs_loadvnode: ino: %llu, attr: 0x%x:%s",
724 	    (unsigned long long)ntkey->k_ino,
725 	    ntkey->k_attrtype, ntkey->k_attrname));
726 
727 	ntmp = VFSTONTFS(mp);
728 
729 	/* Get ntnode */
730 	error = ntfs_ntlookup(ntmp, ntkey->k_ino, &ip);
731 	if (error) {
732 		printf("ntfs_loadvnode: ntfs_ntget failed\n");
733 		goto out;
734 	}
735 	/* It may be not initialized fully, so force load it */
736 	if (!(ip->i_flag & IN_LOADED)) {
737 	       error = ntfs_loadntnode(ntmp, ip);
738 	       if(error) {
739 		       printf("ntfs_loadvnode: CAN'T LOAD ATTRIBUTES FOR INO:"
740 			   " %llu\n", (unsigned long long)ip->i_number);
741 		       ntfs_ntput(ip);
742 		       goto out;
743 	       }
744 	}
745 
746 	/* Setup fnode */
747 	fp = kmem_zalloc(sizeof(*fp), KM_SLEEP);
748 	dprintf(("%s: allocating fnode: %p\n", __func__, fp));
749 
750 	error = ntfs_ntvattrget(ntmp, ip, NTFS_A_NAME, NULL, 0, &vap);
751 	if (error) {
752 		ntfs_ntput(ip);
753 		goto out;
754 	}
755 	fp->f_fflag = vap->va_a_name->n_flag;
756 	fp->f_pnumber = vap->va_a_name->n_pnumber;
757 	fp->f_times = vap->va_a_name->n_times;
758 	ntfs_ntvattrrele(vap);
759 
760 	if ((ip->i_frflag & NTFS_FRFLAG_DIR) &&
761 	    (ntkey->k_attrtype == NTFS_A_DATA &&
762 	    strcmp(ntkey->k_attrname, "") == 0)) {
763                         f_type = VDIR;
764 	} else {
765 		f_type = VREG;
766 		error = ntfs_ntvattrget(ntmp, ip,
767 		    ntkey->k_attrtype, ntkey->k_attrname, 0, &vap);
768 		if (error == 0) {
769 			fp->f_size = vap->va_datalen;
770 			fp->f_allocated = vap->va_allocated;
771 			ntfs_ntvattrrele(vap);
772 		} else if (ntkey->k_attrtype == NTFS_A_DATA &&
773 		    strcmp(ntkey->k_attrname, "") == 0 &&
774 		    error == ENOENT) {
775 			fp->f_size = 0;
776 			fp->f_allocated = 0;
777 			error = 0;
778 		} else
779 			goto out;
780 	}
781 
782 	if (key_len <= sizeof(fp->f_smallkey))
783 		fp->f_key = &fp->f_smallkey;
784 	else
785 		fp->f_key = kmem_alloc(key_len, KM_SLEEP);
786 	fp->f_ip = ip;
787 	fp->f_ino = ip->i_number;
788 	strcpy(fp->f_attrname, ntkey->k_attrname);
789 	fp->f_attrtype = ntkey->k_attrtype;
790 	fp->f_vp = vp;
791 	vp->v_data = fp;
792 
793 	vp->v_tag = VT_NTFS;
794 	vp->v_type = f_type;
795 	vp->v_op = ntfs_vnodeop_p;
796 	ntfs_ntref(ip);
797 	vref(ip->i_devvp);
798 	genfs_node_init(vp, &ntfs_genfsops);
799 
800 	if (ip->i_number == NTFS_ROOTINO)
801 		vp->v_vflag |= VV_ROOT;
802 
803 	uvm_vnp_setsize(vp, fp->f_size);
804 	ntfs_ntput(ip);
805 
806 	*new_key = fp->f_key;
807 
808 	fp = NULL;
809 
810 out:
811 	if (ntkey != &small_key)
812 		kmem_free(ntkey, key_len);
813 	if (fp)
814 		kmem_free(fp, sizeof(*fp));
815 
816 	return error;
817 }
818 
819 static int
820 ntfs_vget(
821 	struct mount *mp,
822 	ino_t ino,
823 	struct vnode **vpp)
824 {
825 	return ntfs_vgetex(mp, ino, NTFS_A_DATA, "", LK_EXCLUSIVE, vpp);
826 }
827 
828 int
829 ntfs_vgetex(
830 	struct mount *mp,
831 	ino_t ino,
832 	u_int32_t attrtype,
833 	const char *attrname,
834 	u_long lkflags,
835 	struct vnode **vpp)
836 {
837 	const int attrlen = strlen(attrname);
838 	int error;
839 	struct ntkey small_key, *ntkey;
840 
841 	if (NTKEY_SIZE(attrlen) <= sizeof(small_key))
842 		ntkey = &small_key;
843 	else
844 		ntkey = malloc(NTKEY_SIZE(attrlen), M_TEMP, M_WAITOK);
845 	ntkey->k_ino = ino;
846 	ntkey->k_attrtype = attrtype;
847 	strcpy(ntkey->k_attrname, attrname);
848 
849 	error = vcache_get(mp, ntkey, NTKEY_SIZE(attrlen), vpp);
850 	if (error)
851 		goto out;
852 
853 	if ((lkflags & (LK_SHARED | LK_EXCLUSIVE)) != 0) {
854 		error = vn_lock(*vpp, lkflags);
855 		if (error) {
856 			vrele(*vpp);
857 			*vpp = NULL;
858 		}
859 	}
860 
861 out:
862 	if (ntkey != &small_key)
863 		free(ntkey, M_TEMP);
864 	return error;
865 }
866 
867 extern const struct vnodeopv_desc ntfs_vnodeop_opv_desc;
868 
869 const struct vnodeopv_desc * const ntfs_vnodeopv_descs[] = {
870 	&ntfs_vnodeop_opv_desc,
871 	NULL,
872 };
873 
874 struct vfsops ntfs_vfsops = {
875 	.vfs_name = MOUNT_NTFS,
876 	.vfs_min_mount_data = sizeof (struct ntfs_args),
877 	.vfs_mount = ntfs_mount,
878 	.vfs_start = ntfs_start,
879 	.vfs_unmount = ntfs_unmount,
880 	.vfs_root = ntfs_root,
881 	.vfs_quotactl = (void *)eopnotsupp,
882 	.vfs_statvfs = ntfs_statvfs,
883 	.vfs_sync = ntfs_sync,
884 	.vfs_vget = ntfs_vget,
885 	.vfs_loadvnode = ntfs_loadvnode,
886 	.vfs_fhtovp = ntfs_fhtovp,
887 	.vfs_vptofh = ntfs_vptofh,
888 	.vfs_init = ntfs_init,
889 	.vfs_reinit = ntfs_reinit,
890 	.vfs_done = ntfs_done,
891 	.vfs_mountroot = ntfs_mountroot,
892 	.vfs_snapshot = (void *)eopnotsupp,
893 	.vfs_extattrctl = vfs_stdextattrctl,
894 	.vfs_suspendctl = (void *)eopnotsupp,
895 	.vfs_renamelock_enter = genfs_renamelock_enter,
896 	.vfs_renamelock_exit = genfs_renamelock_exit,
897 	.vfs_fsync = (void *)eopnotsupp,
898 	.vfs_opv_descs = ntfs_vnodeopv_descs
899 };
900 
901 static int
902 ntfs_modcmd(modcmd_t cmd, void *arg)
903 {
904 	int error;
905 
906 	switch (cmd) {
907 	case MODULE_CMD_INIT:
908 		error = vfs_attach(&ntfs_vfsops);
909 		if (error != 0)
910 			break;
911 		sysctl_createv(&ntfs_sysctl_log, 0, NULL, NULL,
912 			       CTLFLAG_PERMANENT,
913 			       CTLTYPE_NODE, "ntfs",
914 			       SYSCTL_DESCR("NTFS file system"),
915 			       NULL, 0, NULL, 0,
916 			       CTL_VFS, 20, CTL_EOL);
917 		/*
918 		 * XXX the "20" above could be dynamic, thereby eliminating
919 		 * one more instance of the "number to vfs" mapping problem,
920 		 * but "20" is the order as taken from sys/mount.h
921 		 */
922 		break;
923 	case MODULE_CMD_FINI:
924 		error = vfs_detach(&ntfs_vfsops);
925 		if (error != 0)
926 			break;
927 		sysctl_teardown(&ntfs_sysctl_log);
928 		break;
929 	default:
930 		error = ENOTTY;
931 		break;
932 	}
933 
934 	return (error);
935 }
936