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