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