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