Home | History | Annotate | Line # | Download | only in nfs
nfs_vfsops.c revision 1.206
      1  1.206    cegger /*	$NetBSD: nfs_vfsops.c,v 1.206 2008/12/17 20:51:38 cegger Exp $	*/
      2   1.26       cgd 
      3    1.1       cgd /*
      4   1.43      fvdl  * Copyright (c) 1989, 1993, 1995
      5   1.21   mycroft  *	The Regents of the University of California.  All rights reserved.
      6    1.1       cgd  *
      7    1.1       cgd  * This code is derived from software contributed to Berkeley by
      8    1.1       cgd  * Rick Macklem at The University of Guelph.
      9    1.1       cgd  *
     10    1.1       cgd  * Redistribution and use in source and binary forms, with or without
     11    1.1       cgd  * modification, are permitted provided that the following conditions
     12    1.1       cgd  * are met:
     13    1.1       cgd  * 1. Redistributions of source code must retain the above copyright
     14    1.1       cgd  *    notice, this list of conditions and the following disclaimer.
     15    1.1       cgd  * 2. Redistributions in binary form must reproduce the above copyright
     16    1.1       cgd  *    notice, this list of conditions and the following disclaimer in the
     17    1.1       cgd  *    documentation and/or other materials provided with the distribution.
     18  1.132       agc  * 3. Neither the name of the University nor the names of its contributors
     19    1.1       cgd  *    may be used to endorse or promote products derived from this software
     20    1.1       cgd  *    without specific prior written permission.
     21    1.1       cgd  *
     22    1.1       cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     23    1.1       cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     24    1.1       cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     25    1.1       cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     26    1.1       cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     27    1.1       cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     28    1.1       cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     29    1.1       cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     30    1.1       cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     31    1.1       cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     32    1.1       cgd  * SUCH DAMAGE.
     33    1.1       cgd  *
     34   1.43      fvdl  *	@(#)nfs_vfsops.c	8.12 (Berkeley) 5/20/95
     35    1.1       cgd  */
     36  1.111     lukem 
     37  1.111     lukem #include <sys/cdefs.h>
     38  1.206    cegger __KERNEL_RCSID(0, "$NetBSD: nfs_vfsops.c,v 1.206 2008/12/17 20:51:38 cegger Exp $");
     39   1.76  jonathan 
     40  1.103       mrg #if defined(_KERNEL_OPT)
     41   1.95     bjh21 #include "opt_nfs.h"
     42   1.76  jonathan #endif
     43   1.74  sommerfe 
     44    1.9   mycroft #include <sys/param.h>
     45    1.9   mycroft #include <sys/ioctl.h>
     46    1.9   mycroft #include <sys/signal.h>
     47    1.9   mycroft #include <sys/proc.h>
     48    1.9   mycroft #include <sys/namei.h>
     49   1.56   thorpej #include <sys/device.h>
     50    1.9   mycroft #include <sys/vnode.h>
     51   1.21   mycroft #include <sys/kernel.h>
     52    1.9   mycroft #include <sys/mount.h>
     53    1.9   mycroft #include <sys/buf.h>
     54    1.9   mycroft #include <sys/mbuf.h>
     55  1.136  christos #include <sys/dirent.h>
     56    1.9   mycroft #include <sys/socket.h>
     57   1.41  christos #include <sys/socketvar.h>
     58   1.89    tsarna #include <sys/sysctl.h>
     59    1.9   mycroft #include <sys/systm.h>
     60  1.157    kardel #include <sys/timetc.h>
     61  1.155      elad #include <sys/kauth.h>
     62  1.200    rumble #include <sys/module.h>
     63    1.1       cgd 
     64    1.9   mycroft #include <net/if.h>
     65    1.9   mycroft #include <net/route.h>
     66    1.9   mycroft #include <netinet/in.h>
     67    1.9   mycroft 
     68   1.21   mycroft #include <nfs/rpcv2.h>
     69   1.43      fvdl #include <nfs/nfsproto.h>
     70    1.9   mycroft #include <nfs/nfsnode.h>
     71   1.43      fvdl #include <nfs/nfs.h>
     72    1.9   mycroft #include <nfs/nfsmount.h>
     73    1.9   mycroft #include <nfs/xdr_subs.h>
     74    1.9   mycroft #include <nfs/nfsm_subs.h>
     75    1.9   mycroft #include <nfs/nfsdiskless.h>
     76   1.41  christos #include <nfs/nfs_var.h>
     77    1.1       cgd 
     78  1.200    rumble MODULE(MODULE_CLASS_VFS, nfs, NULL);
     79  1.200    rumble 
     80   1.54   thorpej extern struct nfsstats nfsstats;
     81   1.43      fvdl extern int nfs_ticks;
     82  1.121   thorpej 
     83  1.153     blymn /*
     84  1.153     blymn  * keep a count of the nfs mounts to generate ficticious drive names
     85  1.153     blymn  * for the per drive stats.
     86  1.153     blymn  */
     87  1.153     blymn unsigned int nfs_mount_count = 0;
     88  1.153     blymn 
     89    1.1       cgd /*
     90    1.1       cgd  * nfs vfs operations.
     91    1.1       cgd  */
     92   1.68   thorpej 
     93   1.99  jdolecek extern const struct vnodeopv_desc nfsv2_vnodeop_opv_desc;
     94   1.99  jdolecek extern const struct vnodeopv_desc spec_nfsv2nodeop_opv_desc;
     95   1.99  jdolecek extern const struct vnodeopv_desc fifo_nfsv2nodeop_opv_desc;
     96   1.68   thorpej 
     97   1.99  jdolecek const struct vnodeopv_desc * const nfs_vnodeopv_descs[] = {
     98   1.68   thorpej 	&nfsv2_vnodeop_opv_desc,
     99   1.68   thorpej 	&spec_nfsv2nodeop_opv_desc,
    100   1.68   thorpej 	&fifo_nfsv2nodeop_opv_desc,
    101   1.68   thorpej 	NULL,
    102   1.68   thorpej };
    103   1.68   thorpej 
    104    1.1       cgd struct vfsops nfs_vfsops = {
    105   1.14       cgd 	MOUNT_NFS,
    106  1.178       dsl 	sizeof (struct nfs_args),
    107    1.1       cgd 	nfs_mount,
    108    1.1       cgd 	nfs_start,
    109    1.1       cgd 	nfs_unmount,
    110    1.1       cgd 	nfs_root,
    111  1.188     pooka 	(void *)eopnotsupp,	/* vfs_quotactl */
    112  1.136  christos 	nfs_statvfs,
    113    1.1       cgd 	nfs_sync,
    114   1.21   mycroft 	nfs_vget,
    115    1.1       cgd 	nfs_fhtovp,
    116    1.1       cgd 	nfs_vptofh,
    117   1.54   thorpej 	nfs_vfs_init,
    118  1.203      matt 	NULL,
    119   1.86  jdolecek 	nfs_vfs_done,
    120   1.56   thorpej 	nfs_mountroot,
    121  1.140   hannken 	(int (*)(struct mount *, struct vnode *, struct timespec *)) eopnotsupp,
    122  1.144   thorpej 	vfs_stdextattrctl,
    123  1.181     pooka 	(void *)eopnotsupp,	/* vfs_suspendctl */
    124  1.193  dholland 	genfs_renamelock_enter,
    125  1.193  dholland 	genfs_renamelock_exit,
    126  1.197        ad 	(void *)eopnotsupp,
    127   1.68   thorpej 	nfs_vnodeopv_descs,
    128  1.162  christos 	0,
    129  1.162  christos 	{ NULL, NULL },
    130    1.1       cgd };
    131  1.200    rumble 
    132  1.205        ad extern u_int32_t nfs_procids[NFS_NPROCS];
    133  1.205        ad extern u_int32_t nfs_prog, nfs_vers;
    134  1.205        ad static struct sysctllog *nfs_clog;
    135  1.205        ad 
    136  1.205        ad static int nfs_mount_diskless __P((struct nfs_dlmount *, const char *,
    137  1.205        ad     struct mount **, struct vnode **, struct lwp *));
    138  1.205        ad static void nfs_sysctl_init(void);
    139  1.205        ad static void nfs_sysctl_fini(void);
    140  1.205        ad 
    141  1.200    rumble static int
    142  1.200    rumble nfs_modcmd(modcmd_t cmd, void *arg)
    143  1.200    rumble {
    144  1.205        ad 	int error;
    145  1.200    rumble 
    146  1.200    rumble 	switch (cmd) {
    147  1.200    rumble 	case MODULE_CMD_INIT:
    148  1.205        ad 		error = vfs_attach(&nfs_vfsops);
    149  1.205        ad 		if (error == 0) {
    150  1.205        ad 			nfs_sysctl_init();
    151  1.205        ad 		}
    152  1.205        ad 		return error;
    153  1.200    rumble 	case MODULE_CMD_FINI:
    154  1.205        ad 		error = vfs_detach(&nfs_vfsops);
    155  1.205        ad 		if (error == 0) {
    156  1.205        ad 			nfs_sysctl_fini();
    157  1.205        ad 		}
    158  1.205        ad 		return error;
    159  1.200    rumble 	default:
    160  1.200    rumble 		return ENOTTY;
    161  1.200    rumble 	}
    162  1.200    rumble }
    163    1.1       cgd 
    164    1.1       cgd /*
    165  1.136  christos  * nfs statvfs call
    166    1.1       cgd  */
    167   1.21   mycroft int
    168  1.188     pooka nfs_statvfs(mp, sbp)
    169    1.1       cgd 	struct mount *mp;
    170  1.136  christos 	struct statvfs *sbp;
    171    1.1       cgd {
    172  1.188     pooka 	struct lwp *l = curlwp;
    173   1.88  augustss 	struct vnode *vp;
    174   1.88  augustss 	struct nfs_statfs *sfp;
    175  1.174  christos 	char *cp;
    176   1.88  augustss 	u_int32_t *tl;
    177   1.88  augustss 	int32_t t1, t2;
    178  1.174  christos 	char *bpos, *dpos, *cp2;
    179   1.43      fvdl 	struct nfsmount *nmp = VFSTONFS(mp);
    180   1.95     bjh21 	int error = 0, retattr;
    181   1.95     bjh21 #ifdef NFS_V2_ONLY
    182   1.95     bjh21 	const int v3 = 0;
    183   1.95     bjh21 #else
    184   1.95     bjh21 	int v3 = (nmp->nm_flag & NFSMNT_NFSV3);
    185   1.95     bjh21 #endif
    186  1.122      matt 	struct mbuf *mreq, *mrep = NULL, *md, *mb;
    187  1.155      elad 	kauth_cred_t cred;
    188   1.43      fvdl 	u_quad_t tquad;
    189  1.126  drochner 	struct nfsnode *np;
    190    1.1       cgd 
    191   1.43      fvdl #ifndef nolint
    192   1.43      fvdl 	sfp = (struct nfs_statfs *)0;
    193   1.43      fvdl #endif
    194  1.101      fvdl 	vp = nmp->nm_vnode;
    195  1.126  drochner 	np = VTONFS(vp);
    196  1.155      elad 	cred = kauth_cred_alloc();
    197  1.112  christos #ifndef NFS_V2_ONLY
    198   1.65      fvdl 	if (v3 && (nmp->nm_iflag & NFSMNT_GOTFSINFO) == 0)
    199  1.151  christos 		(void)nfs_fsinfo(nmp, vp, cred, l);
    200  1.112  christos #endif
    201   1.43      fvdl 	nfsstats.rpccnt[NFSPROC_FSSTAT]++;
    202  1.126  drochner 	nfsm_reqhead(np, NFSPROC_FSSTAT, NFSX_FH(v3));
    203  1.126  drochner 	nfsm_fhtom(np, v3);
    204  1.151  christos 	nfsm_request(np, NFSPROC_FSSTAT, l, cred);
    205   1.43      fvdl 	if (v3)
    206  1.119      yamt 		nfsm_postop_attr(vp, retattr, 0);
    207   1.46      fvdl 	if (error) {
    208  1.133    itojun 		if (mrep != NULL) {
    209  1.133    itojun 			if (mrep->m_next != NULL)
    210  1.136  christos 				printf("nfs_vfsops: nfs_statvfs would lose buffers\n");
    211  1.133    itojun 			m_freem(mrep);
    212  1.133    itojun 		}
    213   1.46      fvdl 		goto nfsmout;
    214   1.46      fvdl 	}
    215   1.46      fvdl 	nfsm_dissect(sfp, struct nfs_statfs *, NFSX_STATFS(v3));
    216  1.136  christos 	sbp->f_flag = nmp->nm_flag;
    217   1.43      fvdl 	sbp->f_iosize = min(nmp->nm_rsize, nmp->nm_wsize);
    218   1.43      fvdl 	if (v3) {
    219  1.136  christos 		sbp->f_frsize = sbp->f_bsize = NFS_FABLKSIZE;
    220   1.83      fair 		tquad = fxdr_hyper(&sfp->sf_tbytes);
    221  1.136  christos 		sbp->f_blocks = ((quad_t)tquad / (quad_t)NFS_FABLKSIZE);
    222   1.83      fair 		tquad = fxdr_hyper(&sfp->sf_fbytes);
    223  1.136  christos 		sbp->f_bfree = ((quad_t)tquad / (quad_t)NFS_FABLKSIZE);
    224   1.83      fair 		tquad = fxdr_hyper(&sfp->sf_abytes);
    225  1.136  christos 		tquad = ((quad_t)tquad / (quad_t)NFS_FABLKSIZE);
    226  1.136  christos 		sbp->f_bresvd = sbp->f_bfree - tquad;
    227  1.136  christos 		sbp->f_bavail = tquad;
    228  1.136  christos 		/* Handle older NFS servers returning negative values */
    229  1.136  christos 		if ((quad_t)sbp->f_bavail < 0)
    230  1.136  christos 			sbp->f_bavail = 0;
    231   1.83      fair 		tquad = fxdr_hyper(&sfp->sf_tfiles);
    232  1.136  christos 		sbp->f_files = tquad;
    233   1.83      fair 		tquad = fxdr_hyper(&sfp->sf_ffiles);
    234  1.136  christos 		sbp->f_ffree = tquad;
    235  1.136  christos 		sbp->f_favail = tquad;
    236  1.136  christos 		sbp->f_fresvd = 0;
    237  1.136  christos 		sbp->f_namemax = MAXNAMLEN;
    238   1.21   mycroft 	} else {
    239  1.136  christos 		sbp->f_bsize = NFS_FABLKSIZE;
    240  1.136  christos 		sbp->f_frsize = fxdr_unsigned(int32_t, sfp->sf_bsize);
    241   1.43      fvdl 		sbp->f_blocks = fxdr_unsigned(int32_t, sfp->sf_blocks);
    242   1.43      fvdl 		sbp->f_bfree = fxdr_unsigned(int32_t, sfp->sf_bfree);
    243   1.43      fvdl 		sbp->f_bavail = fxdr_unsigned(int32_t, sfp->sf_bavail);
    244  1.136  christos 		sbp->f_fresvd = 0;
    245   1.21   mycroft 		sbp->f_files = 0;
    246   1.21   mycroft 		sbp->f_ffree = 0;
    247  1.136  christos 		sbp->f_favail = 0;
    248  1.136  christos 		sbp->f_fresvd = 0;
    249  1.136  christos 		sbp->f_namemax = MAXNAMLEN;
    250   1.21   mycroft 	}
    251  1.136  christos 	copy_statvfs_info(sbp, mp);
    252    1.1       cgd 	nfsm_reqdone;
    253  1.155      elad 	kauth_cred_free(cred);
    254    1.1       cgd 	return (error);
    255    1.1       cgd }
    256    1.1       cgd 
    257   1.95     bjh21 #ifndef NFS_V2_ONLY
    258    1.1       cgd /*
    259   1.43      fvdl  * nfs version 3 fsinfo rpc call
    260   1.43      fvdl  */
    261   1.43      fvdl int
    262  1.151  christos nfs_fsinfo(nmp, vp, cred, l)
    263   1.88  augustss 	struct nfsmount *nmp;
    264   1.88  augustss 	struct vnode *vp;
    265  1.155      elad 	kauth_cred_t cred;
    266  1.151  christos 	struct lwp *l;
    267   1.43      fvdl {
    268   1.88  augustss 	struct nfsv3_fsinfo *fsp;
    269  1.174  christos 	char *cp;
    270   1.88  augustss 	int32_t t1, t2;
    271  1.147  christos 	u_int32_t *tl, pref, xmax;
    272  1.174  christos 	char *bpos, *dpos, *cp2;
    273   1.43      fvdl 	int error = 0, retattr;
    274  1.122      matt 	struct mbuf *mreq, *mrep, *md, *mb;
    275   1.61      fvdl 	u_int64_t maxfsize;
    276  1.126  drochner 	struct nfsnode *np = VTONFS(vp);
    277   1.43      fvdl 
    278   1.43      fvdl 	nfsstats.rpccnt[NFSPROC_FSINFO]++;
    279  1.126  drochner 	nfsm_reqhead(np, NFSPROC_FSINFO, NFSX_FH(1));
    280  1.126  drochner 	nfsm_fhtom(np, 1);
    281  1.151  christos 	nfsm_request(np, NFSPROC_FSINFO, l, cred);
    282  1.119      yamt 	nfsm_postop_attr(vp, retattr, 0);
    283   1.43      fvdl 	if (!error) {
    284   1.43      fvdl 		nfsm_dissect(fsp, struct nfsv3_fsinfo *, NFSX_V3FSINFO);
    285   1.43      fvdl 		pref = fxdr_unsigned(u_int32_t, fsp->fs_wtpref);
    286  1.123      yamt 		if ((nmp->nm_flag & NFSMNT_WSIZE) == 0 &&
    287  1.123      yamt 		    pref < nmp->nm_wsize && pref >= NFS_FABLKSIZE)
    288   1.43      fvdl 			nmp->nm_wsize = (pref + NFS_FABLKSIZE - 1) &
    289   1.43      fvdl 				~(NFS_FABLKSIZE - 1);
    290  1.147  christos 		xmax = fxdr_unsigned(u_int32_t, fsp->fs_wtmax);
    291  1.147  christos 		if (xmax < nmp->nm_wsize && xmax > 0) {
    292  1.147  christos 			nmp->nm_wsize = xmax & ~(NFS_FABLKSIZE - 1);
    293   1.43      fvdl 			if (nmp->nm_wsize == 0)
    294  1.147  christos 				nmp->nm_wsize = xmax;
    295   1.43      fvdl 		}
    296   1.43      fvdl 		pref = fxdr_unsigned(u_int32_t, fsp->fs_rtpref);
    297  1.123      yamt 		if ((nmp->nm_flag & NFSMNT_RSIZE) == 0 &&
    298  1.123      yamt 		    pref < nmp->nm_rsize && pref >= NFS_FABLKSIZE)
    299   1.43      fvdl 			nmp->nm_rsize = (pref + NFS_FABLKSIZE - 1) &
    300   1.43      fvdl 				~(NFS_FABLKSIZE - 1);
    301  1.147  christos 		xmax = fxdr_unsigned(u_int32_t, fsp->fs_rtmax);
    302  1.147  christos 		if (xmax < nmp->nm_rsize && xmax > 0) {
    303  1.147  christos 			nmp->nm_rsize = xmax & ~(NFS_FABLKSIZE - 1);
    304   1.43      fvdl 			if (nmp->nm_rsize == 0)
    305  1.147  christos 				nmp->nm_rsize = xmax;
    306   1.43      fvdl 		}
    307   1.43      fvdl 		pref = fxdr_unsigned(u_int32_t, fsp->fs_dtpref);
    308   1.65      fvdl 		if (pref < nmp->nm_readdirsize && pref >= NFS_DIRFRAGSIZ)
    309   1.65      fvdl 			nmp->nm_readdirsize = (pref + NFS_DIRFRAGSIZ - 1) &
    310   1.65      fvdl 				~(NFS_DIRFRAGSIZ - 1);
    311  1.147  christos 		if (xmax < nmp->nm_readdirsize && xmax > 0) {
    312  1.147  christos 			nmp->nm_readdirsize = xmax & ~(NFS_DIRFRAGSIZ - 1);
    313   1.43      fvdl 			if (nmp->nm_readdirsize == 0)
    314  1.147  christos 				nmp->nm_readdirsize = xmax;
    315   1.43      fvdl 		}
    316   1.61      fvdl 		/* XXX */
    317   1.61      fvdl 		nmp->nm_maxfilesize = (u_int64_t)0x80000000 * DEV_BSIZE - 1;
    318   1.83      fair 		maxfsize = fxdr_hyper(&fsp->fs_maxfilesize);
    319   1.61      fvdl 		if (maxfsize > 0 && maxfsize < nmp->nm_maxfilesize)
    320   1.61      fvdl 			nmp->nm_maxfilesize = maxfsize;
    321  1.142      yamt 		nmp->nm_mountp->mnt_fs_bshift =
    322  1.142      yamt 		    ffs(MIN(nmp->nm_rsize, nmp->nm_wsize)) - 1;
    323   1.65      fvdl 		nmp->nm_iflag |= NFSMNT_GOTFSINFO;
    324   1.43      fvdl 	}
    325   1.43      fvdl 	nfsm_reqdone;
    326   1.43      fvdl 	return (error);
    327   1.43      fvdl }
    328   1.95     bjh21 #endif
    329   1.43      fvdl 
    330   1.43      fvdl /*
    331   1.22       gwr  * Mount a remote root fs via. NFS.  It goes like this:
    332   1.22       gwr  * - Call nfs_boot_init() to fill in the nfs_diskless struct
    333   1.59       gwr  * - build the rootfs mount point and call mountnfs() to do the rest.
    334    1.1       cgd  */
    335   1.21   mycroft int
    336    1.1       cgd nfs_mountroot()
    337    1.1       cgd {
    338  1.157    kardel 	struct timespec ts;
    339   1.63       gwr 	struct nfs_diskless *nd;
    340   1.22       gwr 	struct vattr attr;
    341   1.22       gwr 	struct mount *mp;
    342    1.1       cgd 	struct vnode *vp;
    343  1.151  christos 	struct lwp *l;
    344   1.22       gwr 	long n;
    345   1.22       gwr 	int error;
    346   1.22       gwr 
    347  1.151  christos 	l = curlwp; /* XXX */
    348   1.22       gwr 
    349  1.152   thorpej 	if (device_class(root_device) != DV_IFNET)
    350   1.56   thorpej 		return (ENODEV);
    351   1.56   thorpej 
    352   1.22       gwr 	/*
    353   1.23       gwr 	 * XXX time must be non-zero when we init the interface or else
    354   1.23       gwr 	 * the arp code will wedge.  [Fixed now in if_ether.c]
    355  1.157    kardel 	 * However, the NFS attribute cache gives false "hits" when the
    356  1.187      yamt 	 * current time < nfs_attrtimeo(nmp, np) so keep this in for now.
    357   1.23       gwr 	 */
    358  1.157    kardel 	if (time_second < NFS_MAXATTRTIMO) {
    359  1.157    kardel 		ts.tv_sec = NFS_MAXATTRTIMO;
    360  1.157    kardel 		ts.tv_nsec = 0;
    361  1.157    kardel 		tc_setclock(&ts);
    362  1.157    kardel 	}
    363   1.23       gwr 
    364   1.23       gwr 	/*
    365   1.22       gwr 	 * Call nfs_boot_init() to fill in the nfs_diskless struct.
    366   1.22       gwr 	 * Side effect:  Finds and configures a network interface.
    367   1.22       gwr 	 */
    368  1.195      yamt 	nd = kmem_zalloc(sizeof(*nd), KM_SLEEP);
    369  1.151  christos 	error = nfs_boot_init(nd, l);
    370   1.80  drochner 	if (error) {
    371  1.190      yamt 		kmem_free(nd, sizeof(*nd));
    372   1.80  drochner 		return (error);
    373   1.80  drochner 	}
    374    1.1       cgd 
    375    1.1       cgd 	/*
    376   1.22       gwr 	 * Create the root mount point.
    377    1.1       cgd 	 */
    378  1.151  christos 	error = nfs_mount_diskless(&nd->nd_root, "/", &mp, &vp, l);
    379   1.59       gwr 	if (error)
    380   1.63       gwr 		goto out;
    381   1.63       gwr 	printf("root on %s\n", nd->nd_root.ndm_host);
    382   1.22       gwr 
    383   1.22       gwr 	/*
    384   1.22       gwr 	 * Link it into the mount list.
    385   1.22       gwr 	 */
    386  1.186        ad 	mutex_enter(&mountlist_lock);
    387   1.43      fvdl 	CIRCLEQ_INSERT_TAIL(&mountlist, mp, mnt_list);
    388  1.186        ad 	mutex_exit(&mountlist_lock);
    389   1.43      fvdl 	rootvp = vp;
    390   1.22       gwr 	mp->mnt_vnodecovered = NULLVP;
    391  1.198        ad 	vfs_unbusy(mp, false, NULL);
    392   1.22       gwr 
    393   1.22       gwr 	/* Get root attributes (for the time). */
    394  1.188     pooka 	error = VOP_GETATTR(vp, &attr, l->l_cred);
    395   1.59       gwr 	if (error)
    396   1.59       gwr 		panic("nfs_mountroot: getattr for root");
    397   1.78  drochner 	n = attr.va_atime.tv_sec;
    398   1.23       gwr #ifdef	DEBUG
    399   1.51  christos 	printf("root time: 0x%lx\n", n);
    400   1.23       gwr #endif
    401  1.141        pk 	setrootfstime(n);
    402   1.15     glass 
    403   1.80  drochner out:
    404   1.24        pk 	if (error)
    405  1.151  christos 		nfs_boot_cleanup(nd, l);
    406  1.190      yamt 	kmem_free(nd, sizeof(*nd));
    407   1.63       gwr 	return (error);
    408    1.1       cgd }
    409    1.1       cgd 
    410   1.21   mycroft /*
    411   1.21   mycroft  * Internal version of mount system call for diskless setup.
    412   1.63       gwr  * Separate function because we used to call it twice.
    413   1.63       gwr  * (once for root and once for swap)
    414   1.21   mycroft  */
    415   1.59       gwr static int
    416  1.151  christos nfs_mount_diskless(ndmntp, mntname, mpp, vpp, l)
    417   1.22       gwr 	struct nfs_dlmount *ndmntp;
    418   1.59       gwr 	const char *mntname;	/* mount point name */
    419   1.59       gwr 	struct mount **mpp;
    420   1.22       gwr 	struct vnode **vpp;
    421  1.151  christos 	struct lwp *l;
    422   1.21   mycroft {
    423   1.22       gwr 	struct mount *mp;
    424   1.22       gwr 	struct mbuf *m;
    425   1.22       gwr 	int error;
    426   1.21   mycroft 
    427  1.147  christos 	vfs_rootmountalloc(MOUNT_NFS, mntname, &mp);
    428   1.69      fvdl 
    429   1.21   mycroft 	mp->mnt_op = &nfs_vfsops;
    430   1.71   thorpej 
    431   1.71   thorpej 	/*
    432   1.71   thorpej 	 * Historical practice expects NFS root file systems to
    433   1.71   thorpej 	 * be initially mounted r/w.
    434   1.71   thorpej 	 */
    435   1.71   thorpej 	mp->mnt_flag &= ~MNT_RDONLY;
    436   1.21   mycroft 
    437   1.22       gwr 	/* Get mbuf for server sockaddr. */
    438   1.30       gwr 	m = m_get(M_WAIT, MT_SONAME);
    439   1.52      fvdl 	if (m == NULL)
    440   1.52      fvdl 		panic("nfs_mountroot: mget soname for %s", mntname);
    441  1.122      matt 	MCLAIM(m, &nfs_mowner);
    442  1.174  christos 	memcpy(mtod(m, void *), (void *)ndmntp->ndm_args.addr,
    443   1.52      fvdl 	      (m->m_len = ndmntp->ndm_args.addr->sa_len));
    444   1.22       gwr 
    445   1.52      fvdl 	error = mountnfs(&ndmntp->ndm_args, mp, m, mntname,
    446  1.151  christos 			 ndmntp->ndm_args.hostname, vpp, l);
    447   1.59       gwr 	if (error) {
    448  1.198        ad 		vfs_unbusy(mp, false, NULL);
    449  1.199        ad 		vfs_destroy(mp);
    450   1.59       gwr 		printf("nfs_mountroot: mount %s failed: %d\n",
    451   1.59       gwr 		       mntname, error);
    452   1.59       gwr 	} else
    453   1.59       gwr 		*mpp = mp;
    454   1.21   mycroft 
    455   1.59       gwr 	return (error);
    456   1.21   mycroft }
    457   1.21   mycroft 
    458   1.21   mycroft void
    459  1.151  christos nfs_decode_args(nmp, argp, l)
    460   1.21   mycroft 	struct nfsmount *nmp;
    461   1.21   mycroft 	struct nfs_args *argp;
    462  1.151  christos 	struct lwp *l;
    463   1.21   mycroft {
    464   1.21   mycroft 	int s;
    465   1.32        pk 	int adjsock;
    466   1.43      fvdl 	int maxio;
    467   1.21   mycroft 
    468   1.38   mycroft 	s = splsoftnet();
    469   1.58      fvdl 
    470   1.58      fvdl 	/*
    471   1.58      fvdl 	 * Silently clear NFSMNT_NOCONN if it's a TCP mount, it makes
    472   1.58      fvdl 	 * no sense in that context.
    473   1.58      fvdl 	 */
    474   1.58      fvdl 	if (argp->sotype == SOCK_STREAM)
    475   1.58      fvdl 		argp->flags &= ~NFSMNT_NOCONN;
    476   1.32        pk 
    477   1.66      fvdl 	/*
    478   1.66      fvdl 	 * Cookie translation is not needed for v2, silently ignore it.
    479   1.66      fvdl 	 */
    480   1.66      fvdl 	if ((argp->flags & (NFSMNT_XLATECOOKIE|NFSMNT_NFSV3)) ==
    481   1.66      fvdl 	    NFSMNT_XLATECOOKIE)
    482   1.66      fvdl 		argp->flags &= ~NFSMNT_XLATECOOKIE;
    483   1.66      fvdl 
    484   1.32        pk 	/* Re-bind if rsrvd port requested and wasn't on one */
    485   1.32        pk 	adjsock = !(nmp->nm_flag & NFSMNT_RESVPORT)
    486   1.32        pk 		  && (argp->flags & NFSMNT_RESVPORT);
    487   1.57      fvdl 	/* Also re-bind if we're switching to/from a connected UDP socket */
    488   1.57      fvdl 	adjsock |= ((nmp->nm_flag & NFSMNT_NOCONN) !=
    489   1.57      fvdl 		    (argp->flags & NFSMNT_NOCONN));
    490   1.32        pk 
    491   1.94     enami 	/* Update flags. */
    492   1.94     enami 	nmp->nm_flag = argp->flags;
    493    1.8        pk 	splx(s);
    494    1.6        pk 
    495    1.6        pk 	if ((argp->flags & NFSMNT_TIMEO) && argp->timeo > 0) {
    496   1.21   mycroft 		nmp->nm_timeo = (argp->timeo * NFS_HZ + 5) / 10;
    497   1.21   mycroft 		if (nmp->nm_timeo < NFS_MINTIMEO)
    498   1.21   mycroft 			nmp->nm_timeo = NFS_MINTIMEO;
    499   1.21   mycroft 		else if (nmp->nm_timeo > NFS_MAXTIMEO)
    500   1.21   mycroft 			nmp->nm_timeo = NFS_MAXTIMEO;
    501    1.6        pk 	}
    502    1.6        pk 
    503    1.6        pk 	if ((argp->flags & NFSMNT_RETRANS) && argp->retrans > 1) {
    504    1.6        pk 		nmp->nm_retry = argp->retrans;
    505    1.6        pk 		if (nmp->nm_retry > NFS_MAXREXMIT)
    506    1.6        pk 			nmp->nm_retry = NFS_MAXREXMIT;
    507    1.6        pk 	}
    508    1.6        pk 
    509   1.95     bjh21 #ifndef NFS_V2_ONLY
    510   1.43      fvdl 	if (argp->flags & NFSMNT_NFSV3) {
    511   1.43      fvdl 		if (argp->sotype == SOCK_DGRAM)
    512   1.43      fvdl 			maxio = NFS_MAXDGRAMDATA;
    513   1.43      fvdl 		else
    514   1.43      fvdl 			maxio = NFS_MAXDATA;
    515   1.43      fvdl 	} else
    516   1.95     bjh21 #endif
    517   1.43      fvdl 		maxio = NFS_V2MAXDATA;
    518   1.43      fvdl 
    519    1.6        pk 	if ((argp->flags & NFSMNT_WSIZE) && argp->wsize > 0) {
    520   1.32        pk 		int osize = nmp->nm_wsize;
    521    1.6        pk 		nmp->nm_wsize = argp->wsize;
    522    1.6        pk 		/* Round down to multiple of blocksize */
    523   1.43      fvdl 		nmp->nm_wsize &= ~(NFS_FABLKSIZE - 1);
    524    1.6        pk 		if (nmp->nm_wsize <= 0)
    525   1.43      fvdl 			nmp->nm_wsize = NFS_FABLKSIZE;
    526   1.32        pk 		adjsock |= (nmp->nm_wsize != osize);
    527    1.6        pk 	}
    528   1.43      fvdl 	if (nmp->nm_wsize > maxio)
    529   1.43      fvdl 		nmp->nm_wsize = maxio;
    530    1.6        pk 	if (nmp->nm_wsize > MAXBSIZE)
    531    1.6        pk 		nmp->nm_wsize = MAXBSIZE;
    532    1.6        pk 
    533    1.6        pk 	if ((argp->flags & NFSMNT_RSIZE) && argp->rsize > 0) {
    534   1.32        pk 		int osize = nmp->nm_rsize;
    535    1.6        pk 		nmp->nm_rsize = argp->rsize;
    536    1.6        pk 		/* Round down to multiple of blocksize */
    537   1.43      fvdl 		nmp->nm_rsize &= ~(NFS_FABLKSIZE - 1);
    538    1.6        pk 		if (nmp->nm_rsize <= 0)
    539   1.43      fvdl 			nmp->nm_rsize = NFS_FABLKSIZE;
    540   1.32        pk 		adjsock |= (nmp->nm_rsize != osize);
    541    1.6        pk 	}
    542   1.43      fvdl 	if (nmp->nm_rsize > maxio)
    543   1.43      fvdl 		nmp->nm_rsize = maxio;
    544    1.6        pk 	if (nmp->nm_rsize > MAXBSIZE)
    545    1.6        pk 		nmp->nm_rsize = MAXBSIZE;
    546   1.21   mycroft 
    547   1.43      fvdl 	if ((argp->flags & NFSMNT_READDIRSIZE) && argp->readdirsize > 0) {
    548   1.43      fvdl 		nmp->nm_readdirsize = argp->readdirsize;
    549   1.65      fvdl 		/* Round down to multiple of minimum blocksize */
    550   1.65      fvdl 		nmp->nm_readdirsize &= ~(NFS_DIRFRAGSIZ - 1);
    551   1.65      fvdl 		if (nmp->nm_readdirsize < NFS_DIRFRAGSIZ)
    552   1.65      fvdl 			nmp->nm_readdirsize = NFS_DIRFRAGSIZ;
    553   1.65      fvdl 		/* Bigger than buffer size makes no sense */
    554   1.65      fvdl 		if (nmp->nm_readdirsize > NFS_DIRBLKSIZ)
    555   1.43      fvdl 			nmp->nm_readdirsize = NFS_DIRBLKSIZ;
    556   1.44      fvdl 	} else if (argp->flags & NFSMNT_RSIZE)
    557   1.44      fvdl 		nmp->nm_readdirsize = nmp->nm_rsize;
    558   1.44      fvdl 
    559   1.43      fvdl 	if (nmp->nm_readdirsize > maxio)
    560   1.43      fvdl 		nmp->nm_readdirsize = maxio;
    561   1.43      fvdl 
    562   1.21   mycroft 	if ((argp->flags & NFSMNT_MAXGRPS) && argp->maxgrouplist >= 0 &&
    563   1.21   mycroft 		argp->maxgrouplist <= NFS_MAXGRPS)
    564   1.21   mycroft 		nmp->nm_numgrps = argp->maxgrouplist;
    565   1.21   mycroft 	if ((argp->flags & NFSMNT_READAHEAD) && argp->readahead >= 0 &&
    566   1.21   mycroft 		argp->readahead <= NFS_MAXRAHEAD)
    567   1.21   mycroft 		nmp->nm_readahead = argp->readahead;
    568   1.21   mycroft 	if ((argp->flags & NFSMNT_DEADTHRESH) && argp->deadthresh >= 1 &&
    569  1.169      yamt 		argp->deadthresh <= NFS_NEVERDEAD)
    570   1.21   mycroft 		nmp->nm_deadthresh = argp->deadthresh;
    571   1.32        pk 
    572   1.52      fvdl 	adjsock |= ((nmp->nm_sotype != argp->sotype) ||
    573   1.52      fvdl 		    (nmp->nm_soproto != argp->proto));
    574   1.52      fvdl 	nmp->nm_sotype = argp->sotype;
    575   1.52      fvdl 	nmp->nm_soproto = argp->proto;
    576   1.52      fvdl 
    577   1.32        pk 	if (nmp->nm_so && adjsock) {
    578   1.72      fvdl 		nfs_safedisconnect(nmp);
    579   1.32        pk 		if (nmp->nm_sotype == SOCK_DGRAM)
    580  1.151  christos 			while (nfs_connect(nmp, (struct nfsreq *)0, l)) {
    581   1.51  christos 				printf("nfs_args: retrying connect\n");
    582  1.183      yamt 				kpause("nfscn3", false, hz, NULL);
    583   1.32        pk 			}
    584   1.32        pk 	}
    585    1.6        pk }
    586    1.6        pk 
    587    1.1       cgd /*
    588    1.1       cgd  * VFS Operations.
    589    1.1       cgd  *
    590    1.1       cgd  * mount system call
    591    1.1       cgd  * It seems a bit dumb to copyinstr() the host and path here and then
    592   1.77     perry  * memcpy() them in mountnfs(), but I wanted to detect errors before
    593    1.1       cgd  * doing the sockargs() call because sockargs() allocates an mbuf and
    594    1.1       cgd  * an error after that means that I have to release the mbuf.
    595    1.1       cgd  */
    596    1.1       cgd /* ARGSUSED */
    597   1.21   mycroft int
    598  1.188     pooka nfs_mount(struct mount *mp, const char *path, void *data, size_t *data_len)
    599    1.1       cgd {
    600  1.188     pooka 	struct lwp *l = curlwp;
    601    1.1       cgd 	int error;
    602  1.178       dsl 	struct nfs_args *args = data;
    603    1.1       cgd 	struct mbuf *nam;
    604  1.118     enami 	struct nfsmount *nmp = VFSTONFS(mp);
    605  1.118     enami 	struct sockaddr *sa;
    606    1.1       cgd 	struct vnode *vp;
    607  1.114     enami 	char *pth, *hst;
    608  1.151  christos 	struct proc *p;
    609   1.34   mycroft 	size_t len;
    610  1.114     enami 	u_char *nfh;
    611  1.116  christos 
    612  1.178       dsl 	if (*data_len < sizeof *args)
    613  1.178       dsl 		return EINVAL;
    614  1.118     enami 
    615  1.151  christos 	p = l->l_proc;
    616  1.116  christos 	if (mp->mnt_flag & MNT_GETARGS) {
    617  1.118     enami 
    618  1.116  christos 		if (nmp == NULL)
    619  1.118     enami 			return (EIO);
    620  1.178       dsl 		if (args->addr != NULL) {
    621  1.118     enami 			sa = mtod(nmp->nm_nam, struct sockaddr *);
    622  1.178       dsl 			error = copyout(sa, args->addr, sa->sa_len);
    623  1.118     enami 			if (error)
    624  1.118     enami 				return (error);
    625  1.178       dsl 			args->addrlen = sa->sa_len;
    626  1.120       scw 		} else
    627  1.178       dsl 			args->addrlen = 0;
    628  1.120       scw 
    629  1.178       dsl 		args->version = NFS_ARGSVERSION;
    630  1.178       dsl 		args->sotype = nmp->nm_sotype;
    631  1.178       dsl 		args->proto = nmp->nm_soproto;
    632  1.178       dsl 		args->fh = NULL;
    633  1.178       dsl 		args->fhsize = 0;
    634  1.178       dsl 		args->flags = nmp->nm_flag;
    635  1.178       dsl 		args->wsize = nmp->nm_wsize;
    636  1.178       dsl 		args->rsize = nmp->nm_rsize;
    637  1.178       dsl 		args->readdirsize = nmp->nm_readdirsize;
    638  1.178       dsl 		args->timeo = nmp->nm_timeo;
    639  1.178       dsl 		args->retrans = nmp->nm_retry;
    640  1.178       dsl 		args->maxgrouplist = nmp->nm_numgrps;
    641  1.178       dsl 		args->readahead = nmp->nm_readahead;
    642  1.178       dsl 		args->leaseterm = 0; /* dummy */
    643  1.178       dsl 		args->deadthresh = nmp->nm_deadthresh;
    644  1.178       dsl 		args->hostname = NULL;
    645  1.178       dsl 		*data_len = sizeof *args;
    646  1.178       dsl 		return 0;
    647  1.116  christos 	}
    648    1.1       cgd 
    649  1.178       dsl 	if (args->version != NFS_ARGSVERSION)
    650   1.43      fvdl 		return (EPROGMISMATCH);
    651  1.178       dsl 	if (args->flags & (NFSMNT_NQNFS|NFSMNT_KERB))
    652  1.170      yamt 		return (EPROGUNAVAIL);
    653   1.95     bjh21 #ifdef NFS_V2_ONLY
    654  1.178       dsl 	if (args->flags & NFSMNT_NFSV3)
    655  1.102     bjh21 		return (EPROGMISMATCH);
    656   1.95     bjh21 #endif
    657    1.6        pk 	if (mp->mnt_flag & MNT_UPDATE) {
    658    1.6        pk 		if (nmp == NULL)
    659   1.21   mycroft 			return (EIO);
    660   1.47      fvdl 		/*
    661   1.47      fvdl 		 * When doing an update, we can't change from or to
    662  1.170      yamt 		 * v3, or change cookie translation
    663   1.47      fvdl 		 */
    664  1.178       dsl 		args->flags = (args->flags & ~(NFSMNT_NFSV3|NFSMNT_XLATECOOKIE)) |
    665  1.170      yamt 		    (nmp->nm_flag & (NFSMNT_NFSV3|NFSMNT_XLATECOOKIE));
    666  1.178       dsl 		nfs_decode_args(nmp, args, l);
    667    1.6        pk 		return (0);
    668    1.6        pk 	}
    669  1.178       dsl 	if (args->fhsize < 0 || args->fhsize > NFSX_V3FHMAX)
    670  1.105      fvdl 		return (EINVAL);
    671  1.206    cegger 	nfh = malloc(NFSX_V3FHMAX, M_TEMP, M_WAITOK);
    672  1.178       dsl 	error = copyin(args->fh, nfh, args->fhsize);
    673   1.41  christos 	if (error)
    674  1.185     rmind 		goto free_nfh;
    675  1.206    cegger 	pth = malloc(MNAMELEN, M_TEMP, M_WAITOK);
    676  1.114     enami 	error = copyinstr(path, pth, MNAMELEN - 1, &len);
    677   1.43      fvdl 	if (error)
    678  1.185     rmind 		goto free_pth;
    679   1.77     perry 	memset(&pth[len], 0, MNAMELEN - len);
    680  1.206    cegger 	hst = malloc(MNAMELEN, M_TEMP, M_WAITOK);
    681  1.178       dsl 	error = copyinstr(args->hostname, hst, MNAMELEN - 1, &len);
    682   1.43      fvdl 	if (error)
    683  1.185     rmind 		goto free_hst;
    684   1.77     perry 	memset(&hst[len], 0, MNAMELEN - len);
    685  1.114     enami 	/* sockargs() call must be after above copyin() calls */
    686  1.178       dsl 	error = sockargs(&nam, args->addr, args->addrlen, MT_SONAME);
    687  1.114     enami 	if (error)
    688  1.114     enami 		goto free_hst;
    689  1.122      matt 	MCLAIM(nam, &nfs_mowner);
    690  1.178       dsl 	args->fh = nfh;
    691  1.178       dsl 	error = mountnfs(args, mp, nam, pth, hst, &vp, l);
    692  1.113  jdolecek 
    693  1.114     enami free_hst:
    694  1.206    cegger 	free(hst, M_TEMP);
    695  1.114     enami free_pth:
    696  1.206    cegger 	free(pth, M_TEMP);
    697  1.114     enami free_nfh:
    698  1.206    cegger 	free(nfh, M_TEMP);
    699  1.113  jdolecek 
    700    1.1       cgd 	return (error);
    701    1.1       cgd }
    702    1.1       cgd 
    703    1.1       cgd /*
    704    1.1       cgd  * Common code for mount and mountroot
    705    1.1       cgd  */
    706   1.21   mycroft int
    707  1.151  christos mountnfs(argp, mp, nam, pth, hst, vpp, l)
    708   1.88  augustss 	struct nfs_args *argp;
    709   1.88  augustss 	struct mount *mp;
    710    1.1       cgd 	struct mbuf *nam;
    711   1.55       cgd 	const char *pth, *hst;
    712    1.1       cgd 	struct vnode **vpp;
    713  1.151  christos 	struct lwp *l;
    714    1.1       cgd {
    715   1.88  augustss 	struct nfsmount *nmp;
    716    1.1       cgd 	struct nfsnode *np;
    717  1.201     pooka 	struct vnode *vp;
    718    1.1       cgd 	int error;
    719  1.113  jdolecek 	struct vattr *attrs;
    720  1.155      elad 	kauth_cred_t cr;
    721  1.160  christos 	char iosname[IOSTATNAMELEN];
    722    1.5       cgd 
    723  1.145     perry 	/*
    724   1.90    tsarna 	 * If the number of nfs iothreads to use has never
    725   1.90    tsarna 	 * been set, create a reasonable number of them.
    726   1.90    tsarna 	 */
    727   1.90    tsarna 
    728   1.90    tsarna 	if (nfs_niothreads < 0) {
    729  1.184      yamt 		nfs_set_niothreads(NFS_DEFAULT_NIOTHREADS);
    730   1.90    tsarna 	}
    731  1.145     perry 
    732   1.21   mycroft 	if (mp->mnt_flag & MNT_UPDATE) {
    733   1.21   mycroft 		nmp = VFSTONFS(mp);
    734   1.21   mycroft 		/* update paths, file handles, etc, here	XXX */
    735   1.21   mycroft 		m_freem(nam);
    736   1.21   mycroft 		return (0);
    737   1.21   mycroft 	} else {
    738  1.190      yamt 		nmp = kmem_zalloc(sizeof(*nmp), KM_SLEEP);
    739  1.115     soren 		mp->mnt_data = nmp;
    740   1.43      fvdl 		TAILQ_INIT(&nmp->nm_uidlruhead);
    741   1.53   thorpej 		TAILQ_INIT(&nmp->nm_bufq);
    742  1.172      yamt 		rw_init(&nmp->nm_writeverflock);
    743  1.175      yamt 		mutex_init(&nmp->nm_lock, MUTEX_DEFAULT, IPL_NONE);
    744  1.202      matt 		rw_init(&nmp->nm_rbtlock);
    745  1.175      yamt 		cv_init(&nmp->nm_rcvcv, "nfsrcv");
    746  1.175      yamt 		cv_init(&nmp->nm_sndcv, "nfssnd");
    747  1.175      yamt 		cv_init(&nmp->nm_aiocv, "nfsaio");
    748  1.176      yamt 		cv_init(&nmp->nm_disconcv, "nfsdis");
    749  1.202      matt 		nfs_rbtinit(nmp);
    750   1.21   mycroft 	}
    751   1.91     assar 	vfs_getnewfsid(mp);
    752    1.1       cgd 	nmp->nm_mountp = mp;
    753   1.61      fvdl 
    754   1.95     bjh21 #ifndef NFS_V2_ONLY
    755   1.62  christos 	if ((argp->flags & NFSMNT_NFSV3) == 0)
    756   1.95     bjh21 #endif
    757  1.156      yamt 	{
    758  1.156      yamt 		if (argp->fhsize != NFSX_V2FH) {
    759  1.156      yamt 			return EINVAL;
    760  1.156      yamt 		}
    761  1.156      yamt 	}
    762   1.61      fvdl 
    763  1.196      yamt 	/*
    764  1.196      yamt 	 * V2 can only handle 32 bit filesizes. For v3, nfs_fsinfo
    765  1.196      yamt 	 * will overwrite this.
    766  1.196      yamt 	 */
    767  1.196      yamt 	nmp->nm_maxfilesize = 0xffffffffLL;
    768  1.196      yamt 
    769   1.21   mycroft 	nmp->nm_timeo = NFS_TIMEO;
    770    1.1       cgd 	nmp->nm_retry = NFS_RETRANS;
    771    1.1       cgd 	nmp->nm_wsize = NFS_WSIZE;
    772    1.1       cgd 	nmp->nm_rsize = NFS_RSIZE;
    773   1.43      fvdl 	nmp->nm_readdirsize = NFS_READDIRSIZE;
    774   1.21   mycroft 	nmp->nm_numgrps = NFS_MAXGRPS;
    775   1.21   mycroft 	nmp->nm_readahead = NFS_DEFRAHEAD;
    776  1.169      yamt 	nmp->nm_deadthresh = NFS_DEFDEADTHRESH;
    777  1.179     pooka 	error = set_statvfs_info(pth, UIO_SYSSPACE, hst, UIO_SYSSPACE,
    778  1.179     pooka 	    mp->mnt_op->vfs_name, mp, l);
    779  1.125  christos 	if (error)
    780  1.125  christos 		goto bad;
    781    1.1       cgd 	nmp->nm_nam = nam;
    782    1.1       cgd 
    783    1.1       cgd 	/* Set up the sockets and per-host congestion */
    784    1.1       cgd 	nmp->nm_sotype = argp->sotype;
    785    1.1       cgd 	nmp->nm_soproto = argp->proto;
    786   1.52      fvdl 
    787  1.151  christos 	nfs_decode_args(nmp, argp, l);
    788  1.109       chs 
    789  1.109       chs 	mp->mnt_fs_bshift = ffs(MIN(nmp->nm_rsize, nmp->nm_wsize)) - 1;
    790  1.109       chs 	mp->mnt_dev_bshift = DEV_BSHIFT;
    791   1.21   mycroft 
    792   1.21   mycroft 	/*
    793   1.21   mycroft 	 * For Connection based sockets (TCP,...) defer the connect until
    794   1.21   mycroft 	 * the first request, in case the server is not responding.
    795   1.21   mycroft 	 */
    796   1.21   mycroft 	if (nmp->nm_sotype == SOCK_DGRAM &&
    797  1.151  christos 		(error = nfs_connect(nmp, (struct nfsreq *)0, l)))
    798    1.1       cgd 		goto bad;
    799    1.1       cgd 
    800   1.21   mycroft 	/*
    801   1.21   mycroft 	 * This is silly, but it has to be set so that vinifod() works.
    802  1.136  christos 	 * We do not want to do an nfs_statvfs() here since we can get
    803   1.21   mycroft 	 * stuck on a dead server and we are holding a lock on the mount
    804   1.21   mycroft 	 * point.
    805   1.21   mycroft 	 */
    806   1.21   mycroft 	mp->mnt_stat.f_iosize = NFS_MAXDGRAMDATA;
    807  1.131      fvdl 	error = nfs_nget(mp, (nfsfh_t *)argp->fh, argp->fhsize, &np);
    808   1.43      fvdl 	if (error)
    809    1.1       cgd 		goto bad;
    810  1.201     pooka 	vp = NFSTOV(np);
    811  1.206    cegger 	attrs = malloc(sizeof(struct vattr), M_TEMP, M_WAITOK);
    812  1.201     pooka 	VOP_GETATTR(vp, attrs, l->l_cred);
    813  1.201     pooka 	if ((nmp->nm_flag & NFSMNT_NFSV3) && (vp->v_type == VDIR)) {
    814  1.155      elad 		cr = kauth_cred_alloc();
    815  1.155      elad 		kauth_cred_setuid(cr, attrs->va_uid);
    816  1.155      elad 		kauth_cred_seteuid(cr, attrs->va_uid);
    817  1.155      elad 		kauth_cred_setsvuid(cr, attrs->va_uid);
    818  1.155      elad 		kauth_cred_setgid(cr, attrs->va_gid);
    819  1.155      elad 		kauth_cred_setegid(cr, attrs->va_gid);
    820  1.155      elad 		kauth_cred_setsvgid(cr, attrs->va_gid);
    821  1.201     pooka 		nfs_cookieheuristic(vp, &nmp->nm_iflag, l, cr);
    822  1.155      elad 		kauth_cred_free(cr);
    823   1.65      fvdl 	}
    824  1.206    cegger 	free(attrs, M_TEMP);
    825    1.1       cgd 
    826  1.100      fvdl 	/*
    827  1.100      fvdl 	 * A reference count is needed on the nfsnode representing the
    828  1.100      fvdl 	 * remote root.  If this object is not persistent, then backward
    829  1.100      fvdl 	 * traversals of the mount point (i.e. "..") will not work if
    830  1.100      fvdl 	 * the nfsnode gets flushed out of the cache. Ufs does not have
    831  1.100      fvdl 	 * this problem, because one can identify root inodes by their
    832  1.100      fvdl 	 * number == ROOTINO (2). So, just unlock, but no rele.
    833  1.100      fvdl 	 */
    834  1.100      fvdl 
    835  1.201     pooka 	nmp->nm_vnode = vp;
    836  1.201     pooka 	if (vp->v_type == VNON)
    837  1.201     pooka 		vp->v_type = VDIR;
    838  1.201     pooka 	vp->v_vflag |= VV_ROOT;
    839  1.201     pooka 	VOP_UNLOCK(vp, 0);
    840  1.201     pooka 	*vpp = vp;
    841  1.100      fvdl 
    842  1.160  christos 	snprintf(iosname, sizeof(iosname), "nfs%u", nfs_mount_count++);
    843  1.160  christos 	nmp->nm_stats = iostat_alloc(IOSTAT_NFS, nmp, iosname);
    844  1.153     blymn 
    845    1.1       cgd 	return (0);
    846    1.1       cgd bad:
    847    1.1       cgd 	nfs_disconnect(nmp);
    848  1.172      yamt 	rw_destroy(&nmp->nm_writeverflock);
    849  1.202      matt 	rw_destroy(&nmp->nm_rbtlock);
    850  1.177      yamt 	mutex_destroy(&nmp->nm_lock);
    851  1.177      yamt 	cv_destroy(&nmp->nm_rcvcv);
    852  1.177      yamt 	cv_destroy(&nmp->nm_sndcv);
    853  1.177      yamt 	cv_destroy(&nmp->nm_aiocv);
    854  1.177      yamt 	cv_destroy(&nmp->nm_disconcv);
    855  1.190      yamt 	kmem_free(nmp, sizeof(*nmp));
    856    1.1       cgd 	m_freem(nam);
    857    1.1       cgd 	return (error);
    858    1.1       cgd }
    859    1.1       cgd 
    860    1.1       cgd /*
    861    1.1       cgd  * unmount system call
    862    1.1       cgd  */
    863   1.21   mycroft int
    864  1.188     pooka nfs_unmount(struct mount *mp, int mntflags)
    865    1.1       cgd {
    866   1.88  augustss 	struct nfsmount *nmp;
    867    1.1       cgd 	struct vnode *vp;
    868    1.1       cgd 	int error, flags = 0;
    869    1.1       cgd 
    870   1.33   mycroft 	if (mntflags & MNT_FORCE)
    871    1.1       cgd 		flags |= FORCECLOSE;
    872    1.1       cgd 	nmp = VFSTONFS(mp);
    873    1.1       cgd 	/*
    874    1.1       cgd 	 * Goes something like this..
    875    1.1       cgd 	 * - Check for activity on the root vnode (other than ourselves).
    876    1.1       cgd 	 * - Call vflush() to clear out vnodes for this file system,
    877    1.1       cgd 	 *   except for the root vnode.
    878    1.1       cgd 	 * - Decrement reference on the vnode representing remote root.
    879    1.1       cgd 	 * - Close the socket
    880    1.1       cgd 	 * - Free up the data structures
    881    1.1       cgd 	 */
    882    1.1       cgd 	/*
    883    1.1       cgd 	 * We need to decrement the ref. count on the nfsnode representing
    884    1.1       cgd 	 * the remote root.  See comment in mountnfs().  The VFS unmount()
    885    1.1       cgd 	 * has done vput on this vnode, otherwise we would get deadlock!
    886    1.1       cgd 	 */
    887  1.101      fvdl 	vp = nmp->nm_vnode;
    888  1.130   thorpej 	error = vget(vp, LK_EXCLUSIVE | LK_RETRY);
    889  1.101      fvdl 	if (error != 0)
    890  1.101      fvdl 		return error;
    891  1.101      fvdl 
    892   1.49        pk 	if ((mntflags & MNT_FORCE) == 0 && vp->v_usecount > 2) {
    893    1.1       cgd 		vput(vp);
    894    1.1       cgd 		return (EBUSY);
    895    1.1       cgd 	}
    896   1.21   mycroft 
    897   1.43      fvdl 	error = vflush(mp, vp, flags);
    898   1.43      fvdl 	if (error) {
    899    1.1       cgd 		vput(vp);
    900    1.1       cgd 		return (error);
    901    1.1       cgd 	}
    902   1.21   mycroft 
    903   1.21   mycroft 	/*
    904   1.84  sommerfe 	 * We are now committed to the unmount; mark the mount structure
    905   1.84  sommerfe 	 * as doomed so that any sleepers kicked awake by nfs_disconnect
    906   1.84  sommerfe 	 * will go away cleanly.
    907   1.21   mycroft 	 */
    908   1.84  sommerfe 	nmp->nm_iflag |= NFSMNT_DISMNT;
    909   1.21   mycroft 
    910    1.1       cgd 	/*
    911  1.153     blymn 	 * Clean up the stats... note that we carefully avoid decrementing
    912  1.153     blymn 	 * nfs_mount_count here for good reason - we may not be unmounting
    913  1.153     blymn 	 * the last thing mounted.
    914  1.153     blymn 	 */
    915  1.153     blymn 	iostat_free(nmp->nm_stats);
    916  1.153     blymn 
    917  1.153     blymn 	/*
    918  1.100      fvdl 	 * There are two reference counts to get rid of here
    919  1.100      fvdl 	 * (see comment in mountnfs()).
    920    1.1       cgd 	 */
    921  1.100      fvdl 	vput(vp);
    922   1.13       cgd 	vgone(vp);
    923    1.1       cgd 	nfs_disconnect(nmp);
    924    1.1       cgd 	m_freem(nmp->nm_nam);
    925   1.21   mycroft 
    926  1.172      yamt 	rw_destroy(&nmp->nm_writeverflock);
    927  1.202      matt 	rw_destroy(&nmp->nm_rbtlock);
    928  1.177      yamt 	mutex_destroy(&nmp->nm_lock);
    929  1.177      yamt 	cv_destroy(&nmp->nm_rcvcv);
    930  1.177      yamt 	cv_destroy(&nmp->nm_sndcv);
    931  1.177      yamt 	cv_destroy(&nmp->nm_aiocv);
    932  1.177      yamt 	cv_destroy(&nmp->nm_disconcv);
    933  1.190      yamt 	kmem_free(nmp, sizeof(*nmp));
    934    1.1       cgd 	return (0);
    935    1.1       cgd }
    936    1.1       cgd 
    937    1.1       cgd /*
    938    1.1       cgd  * Return root of a filesystem
    939    1.1       cgd  */
    940   1.21   mycroft int
    941  1.130   thorpej nfs_root(mp, vpp)
    942    1.1       cgd 	struct mount *mp;
    943    1.1       cgd 	struct vnode **vpp;
    944    1.1       cgd {
    945   1.88  augustss 	struct vnode *vp;
    946    1.1       cgd 	struct nfsmount *nmp;
    947    1.1       cgd 	int error;
    948    1.1       cgd 
    949    1.1       cgd 	nmp = VFSTONFS(mp);
    950  1.101      fvdl 	vp = nmp->nm_vnode;
    951  1.130   thorpej 	error = vget(vp, LK_EXCLUSIVE | LK_RETRY);
    952  1.101      fvdl 	if (error != 0)
    953  1.101      fvdl 		return error;
    954    1.1       cgd 	*vpp = vp;
    955    1.1       cgd 	return (0);
    956    1.1       cgd }
    957    1.1       cgd 
    958   1.21   mycroft extern int syncprt;
    959   1.21   mycroft 
    960    1.1       cgd /*
    961    1.1       cgd  * Flush out the buffer cache
    962    1.1       cgd  */
    963    1.1       cgd /* ARGSUSED */
    964   1.21   mycroft int
    965  1.188     pooka nfs_sync(mp, waitfor, cred)
    966    1.1       cgd 	struct mount *mp;
    967    1.1       cgd 	int waitfor;
    968  1.155      elad 	kauth_cred_t cred;
    969    1.1       cgd {
    970  1.189        ad 	struct vnode *vp, *mvp;
    971   1.21   mycroft 	int error, allerror = 0;
    972   1.21   mycroft 
    973    1.1       cgd 	/*
    974    1.1       cgd 	 * Force stale buffer cache information to be flushed.
    975    1.1       cgd 	 */
    976  1.191     pooka 	if ((mvp = vnalloc(mp)) == NULL)
    977  1.189        ad 		return (ENOMEM);
    978   1.21   mycroft loop:
    979  1.167   reinoud 	/*
    980  1.167   reinoud 	 * NOTE: not using the TAILQ_FOREACH here since in this loop vgone()
    981  1.167   reinoud 	 * and vclean() can be called indirectly
    982  1.167   reinoud 	 */
    983  1.189        ad 	mutex_enter(&mntvnode_lock);
    984  1.189        ad 	for (vp = TAILQ_FIRST(&mp->mnt_vnodelist); vp; vp = vunmark(mvp)) {
    985  1.189        ad 		vmark(mvp, vp);
    986  1.189        ad 		if (vp->v_mount != mp || vismarker(vp))
    987  1.189        ad 			continue;
    988  1.189        ad 		mutex_enter(&vp->v_interlock);
    989  1.189        ad 		/* XXX MNT_LAZY cannot be right? */
    990  1.110       chs 		if (waitfor == MNT_LAZY || VOP_ISLOCKED(vp) ||
    991   1.98       chs 		    (LIST_EMPTY(&vp->v_dirtyblkhd) &&
    992  1.189        ad 		     UVM_OBJ_IS_CLEAN(&vp->v_uobj))) {
    993  1.189        ad 			mutex_exit(&vp->v_interlock);
    994   1.21   mycroft 			continue;
    995  1.189        ad 		}
    996  1.189        ad 		mutex_exit(&mntvnode_lock);
    997  1.189        ad 		if (vget(vp, LK_EXCLUSIVE | LK_INTERLOCK)) {
    998  1.189        ad 			(void)vunmark(mvp);
    999   1.21   mycroft 			goto loop;
   1000  1.189        ad 		}
   1001   1.73    kleink 		error = VOP_FSYNC(vp, cred,
   1002  1.188     pooka 		    waitfor == MNT_WAIT ? FSYNC_WAIT : 0, 0, 0);
   1003   1.43      fvdl 		if (error)
   1004   1.21   mycroft 			allerror = error;
   1005   1.21   mycroft 		vput(vp);
   1006  1.189        ad 		mutex_enter(&mntvnode_lock);
   1007   1.21   mycroft 	}
   1008  1.189        ad 	mutex_exit(&mntvnode_lock);
   1009  1.191     pooka 	vnfree(mvp);
   1010   1.21   mycroft 	return (allerror);
   1011   1.21   mycroft }
   1012   1.21   mycroft 
   1013   1.21   mycroft /*
   1014   1.21   mycroft  * NFS flat namespace lookup.
   1015   1.21   mycroft  * Currently unsupported.
   1016   1.21   mycroft  */
   1017   1.21   mycroft /* ARGSUSED */
   1018   1.21   mycroft int
   1019  1.168      yamt nfs_vget(struct mount *mp, ino_t ino, struct vnode **vpp)
   1020   1.21   mycroft {
   1021   1.21   mycroft 
   1022   1.21   mycroft 	return (EOPNOTSUPP);
   1023    1.1       cgd }
   1024   1.43      fvdl 
   1025   1.43      fvdl /*
   1026   1.43      fvdl  * Do that sysctl thang...
   1027   1.43      fvdl  */
   1028  1.134    atatat static int
   1029  1.134    atatat sysctl_vfs_nfs_iothreads(SYSCTLFN_ARGS)
   1030   1.43      fvdl {
   1031  1.184      yamt 	struct sysctlnode node;
   1032  1.184      yamt 	int val;
   1033  1.134    atatat 	int error;
   1034   1.43      fvdl 
   1035  1.184      yamt 	val = nfs_niothreads;
   1036  1.184      yamt 	node = *rnode;
   1037  1.184      yamt 	node.sysctl_data = &val;
   1038  1.184      yamt         error = sysctl_lookup(SYSCTLFN_CALL(&node));
   1039  1.134    atatat 	if (error || newp == NULL)
   1040  1.184      yamt 		return error;
   1041   1.43      fvdl 
   1042  1.184      yamt 	return nfs_set_niothreads(val);
   1043  1.134    atatat }
   1044   1.43      fvdl 
   1045  1.205        ad static void
   1046  1.205        ad nfs_sysctl_init(void)
   1047  1.134    atatat {
   1048   1.43      fvdl 
   1049  1.205        ad 	sysctl_createv(&nfs_clog, 0, NULL, NULL,
   1050  1.135    atatat 		       CTLFLAG_PERMANENT,
   1051  1.134    atatat 		       CTLTYPE_NODE, "vfs", NULL,
   1052  1.134    atatat 		       NULL, 0, NULL, 0,
   1053  1.134    atatat 		       CTL_VFS, CTL_EOL);
   1054  1.205        ad 	sysctl_createv(&nfs_clog, 0, NULL, NULL,
   1055  1.135    atatat 		       CTLFLAG_PERMANENT,
   1056  1.139    atatat 		       CTLTYPE_NODE, "nfs",
   1057  1.139    atatat 		       SYSCTL_DESCR("NFS vfs options"),
   1058  1.134    atatat 		       NULL, 0, NULL, 0,
   1059  1.134    atatat 		       CTL_VFS, 2, CTL_EOL);
   1060  1.134    atatat 	/*
   1061  1.134    atatat 	 * XXX the "2" above could be dynamic, thereby eliminating one
   1062  1.134    atatat 	 * more instance of the "number to vfs" mapping problem, but
   1063  1.134    atatat 	 * "2" is the order as taken from sys/mount.h
   1064  1.134    atatat 	 */
   1065  1.134    atatat 
   1066  1.205        ad 	sysctl_createv(&nfs_clog, 0, NULL, NULL,
   1067  1.135    atatat 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   1068  1.139    atatat 		       CTLTYPE_STRUCT, "nfsstats",
   1069  1.139    atatat 		       SYSCTL_DESCR("NFS operation statistics"),
   1070  1.145     perry 		       NULL, 0, &nfsstats, sizeof(nfsstats),
   1071  1.134    atatat 		       CTL_VFS, 2, NFS_NFSSTATS, CTL_EOL);
   1072  1.205        ad 	sysctl_createv(&nfs_clog, 0, NULL, NULL,
   1073  1.135    atatat 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   1074  1.139    atatat 		       CTLTYPE_INT, "iothreads",
   1075  1.139    atatat 		       SYSCTL_DESCR("Number of NFS client processes desired"),
   1076  1.184      yamt 		       sysctl_vfs_nfs_iothreads, 0, NULL, 0,
   1077  1.134    atatat 		       CTL_VFS, 2, NFS_IOTHREADS, CTL_EOL);
   1078   1.43      fvdl }
   1079    1.1       cgd 
   1080  1.205        ad static void
   1081  1.205        ad nfs_sysctl_fini(void)
   1082  1.205        ad {
   1083  1.205        ad 
   1084  1.205        ad 	sysctl_teardown(&nfs_clog);
   1085  1.205        ad }
   1086  1.205        ad 
   1087    1.1       cgd /* ARGSUSED */
   1088   1.21   mycroft int
   1089  1.163      yamt nfs_fhtovp(struct mount *mp, struct fid *fid, struct vnode **vpp)
   1090   1.81  wrstuden {
   1091  1.163      yamt 	size_t fidsize;
   1092  1.163      yamt 	size_t fhsize;
   1093  1.163      yamt 	struct nfsnode *np;
   1094  1.163      yamt 	int error;
   1095  1.164      yamt 	struct vattr va;
   1096   1.81  wrstuden 
   1097  1.163      yamt 	fidsize = fid->fid_len;
   1098  1.163      yamt 	if (fidsize < sizeof(*fid)) {
   1099  1.163      yamt 		return EINVAL;
   1100  1.163      yamt 	}
   1101  1.163      yamt 	fhsize = fidsize - sizeof(*fid);
   1102  1.163      yamt 	if ((fhsize % NFSX_UNSIGNED) != 0) {
   1103  1.163      yamt 		return EINVAL;
   1104  1.163      yamt 	}
   1105  1.163      yamt 	if ((VFSTONFS(mp)->nm_flag & NFSMNT_NFSV3) != 0) {
   1106  1.163      yamt 		if (fhsize > NFSX_V3FHMAX || fhsize == 0) {
   1107  1.163      yamt 			return EINVAL;
   1108  1.163      yamt 		}
   1109  1.163      yamt 	} else {
   1110  1.163      yamt 		if (fhsize != NFSX_V2FH) {
   1111  1.163      yamt 			return EINVAL;
   1112  1.163      yamt 		}
   1113  1.163      yamt 	}
   1114  1.163      yamt 	error = nfs_nget(mp, (void *)fid->fid_data, fhsize, &np);
   1115  1.163      yamt 	if (error) {
   1116  1.163      yamt 		return error;
   1117  1.163      yamt 	}
   1118  1.163      yamt 	*vpp = NFSTOV(np);
   1119  1.188     pooka 	error = VOP_GETATTR(*vpp, &va, kauth_cred_get());
   1120  1.164      yamt 	if (error != 0) {
   1121  1.164      yamt 		vput(*vpp);
   1122  1.164      yamt 	}
   1123  1.164      yamt 	return error;
   1124   1.81  wrstuden }
   1125   1.81  wrstuden 
   1126    1.1       cgd /* ARGSUSED */
   1127   1.21   mycroft int
   1128  1.163      yamt nfs_vptofh(struct vnode *vp, struct fid *buf, size_t *bufsize)
   1129    1.1       cgd {
   1130  1.163      yamt 	struct nfsnode *np;
   1131  1.163      yamt 	struct fid *fid;
   1132  1.163      yamt 	size_t fidsize;
   1133  1.163      yamt 	int error = 0;
   1134    1.1       cgd 
   1135  1.163      yamt 	np = VTONFS(vp);
   1136  1.163      yamt 	fidsize = sizeof(*fid) + np->n_fhsize;
   1137  1.163      yamt 	if (*bufsize < fidsize) {
   1138  1.163      yamt 		error = E2BIG;
   1139  1.163      yamt 	}
   1140  1.163      yamt 	*bufsize = fidsize;
   1141  1.163      yamt 	if (error == 0) {
   1142  1.163      yamt 		struct fid fid_store;
   1143  1.163      yamt 
   1144  1.163      yamt 		fid = &fid_store;
   1145  1.163      yamt 		memset(fid, 0, sizeof(*fid));
   1146  1.163      yamt 		fid->fid_len = fidsize;
   1147  1.163      yamt 		memcpy(buf, fid, sizeof(*fid));
   1148  1.163      yamt 		memcpy(buf->fid_data, np->n_fhp, np->n_fhsize);
   1149  1.163      yamt 	}
   1150  1.163      yamt 	return error;
   1151    1.1       cgd }
   1152    1.1       cgd 
   1153    1.1       cgd /*
   1154    1.1       cgd  * Vfs start routine, a no-op.
   1155    1.1       cgd  */
   1156    1.1       cgd /* ARGSUSED */
   1157   1.21   mycroft int
   1158  1.188     pooka nfs_start(struct mount *mp, int flags)
   1159    1.1       cgd {
   1160    1.1       cgd 
   1161    1.1       cgd 	return (0);
   1162    1.1       cgd }
   1163