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