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