1 /* 2 * Copyright (c) 1989, 1993, 1995 3 * The Regents of the University of California. All rights reserved. 4 * 5 * This code is derived from software contributed to Berkeley by 6 * Rick Macklem at The University of Guelph. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 3. All advertising materials mentioning features or use of this software 17 * must display the following acknowledgement: 18 * This product includes software developed by the University of 19 * California, Berkeley and its contributors. 20 * 4. Neither the name of the University nor the names of its contributors 21 * may be used to endorse or promote products derived from this software 22 * without specific prior written permission. 23 * 24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 34 * SUCH DAMAGE. 35 * 36 * @(#)nfs_vfsops.c 8.12 (Berkeley) 5/20/95 37 * $FreeBSD: src/sys/nfs/nfs_vfsops.c,v 1.91.2.7 2003/01/27 20:04:08 dillon Exp $ 38 * $DragonFly: src/sys/vfs/nfs/nfs_vfsops.c,v 1.12 2004/02/24 19:22:38 joerg Exp $ 39 */ 40 41 #include "opt_bootp.h" 42 43 #include <sys/param.h> 44 #include <sys/sockio.h> 45 #include <sys/proc.h> 46 #include <sys/vnode.h> 47 #include <sys/kernel.h> 48 #include <sys/sysctl.h> 49 #include <sys/malloc.h> 50 #include <sys/mount.h> 51 #include <sys/mbuf.h> 52 #include <sys/socket.h> 53 #include <sys/socketvar.h> 54 #include <sys/systm.h> 55 56 #include <vm/vm.h> 57 #include <vm/vm_extern.h> 58 #include <vm/vm_zone.h> 59 60 #include <net/if.h> 61 #include <net/route.h> 62 #include <netinet/in.h> 63 64 #include "rpcv2.h" 65 #include "nfsproto.h" 66 #include "nfs.h" 67 #include "nfsmount.h" 68 #include "nfsnode.h" 69 #include "xdr_subs.h" 70 #include "nfsm_subs.h" 71 #include "nfsdiskless.h" 72 #include "nqnfs.h" 73 74 extern int nfs_mountroot(struct mount *mp); 75 extern void bootpc_init(void); 76 77 extern int nfs_ticks; 78 79 MALLOC_DEFINE(M_NFSREQ, "NFS req", "NFS request header"); 80 MALLOC_DEFINE(M_NFSBIGFH, "NFSV3 bigfh", "NFS version 3 file handle"); 81 MALLOC_DEFINE(M_NFSD, "NFS daemon", "Nfs server daemon structure"); 82 MALLOC_DEFINE(M_NFSDIROFF, "NFSV3 diroff", "NFS directory offset data"); 83 MALLOC_DEFINE(M_NFSRVDESC, "NFSV3 srvdesc", "NFS server socket descriptor"); 84 MALLOC_DEFINE(M_NFSUID, "NFS uid", "Nfs uid mapping structure"); 85 MALLOC_DEFINE(M_NQLEASE, "NQNFS Lease", "Nqnfs lease"); 86 MALLOC_DEFINE(M_NFSHASH, "NFS hash", "NFS hash tables"); 87 88 vm_zone_t nfsmount_zone; 89 90 struct nfsstats nfsstats; 91 SYSCTL_NODE(_vfs, OID_AUTO, nfs, CTLFLAG_RW, 0, "NFS filesystem"); 92 SYSCTL_STRUCT(_vfs_nfs, NFS_NFSSTATS, nfsstats, CTLFLAG_RD, 93 &nfsstats, nfsstats, ""); 94 static int nfs_ip_paranoia = 1; 95 SYSCTL_INT(_vfs_nfs, OID_AUTO, nfs_ip_paranoia, CTLFLAG_RW, 96 &nfs_ip_paranoia, 0, ""); 97 #ifdef NFS_DEBUG 98 int nfs_debug; 99 SYSCTL_INT(_vfs_nfs, OID_AUTO, debug, CTLFLAG_RW, &nfs_debug, 0, ""); 100 #endif 101 102 static int nfs_iosize (struct nfsmount *nmp); 103 static void nfs_decode_args (struct nfsmount *nmp, 104 struct nfs_args *argp); 105 static int mountnfs (struct nfs_args *,struct mount *, 106 struct sockaddr *,char *,char *,struct vnode **); 107 static int nfs_mount ( struct mount *mp, char *path, caddr_t data, 108 struct nameidata *ndp, struct thread *td); 109 static int nfs_unmount ( struct mount *mp, int mntflags, 110 struct thread *td); 111 static int nfs_root ( struct mount *mp, struct vnode **vpp); 112 static int nfs_statfs ( struct mount *mp, struct statfs *sbp, 113 struct thread *td); 114 static int nfs_sync ( struct mount *mp, int waitfor, 115 struct thread *td); 116 117 /* 118 * nfs vfs operations. 119 */ 120 static struct vfsops nfs_vfsops = { 121 nfs_mount, 122 vfs_stdstart, 123 nfs_unmount, 124 nfs_root, 125 vfs_stdquotactl, 126 nfs_statfs, 127 nfs_sync, 128 vfs_stdvget, 129 vfs_stdfhtovp, /* shouldn't happen */ 130 vfs_stdcheckexp, 131 vfs_stdvptofh, /* shouldn't happen */ 132 nfs_init, 133 nfs_uninit, 134 vfs_stdextattrctl, 135 }; 136 VFS_SET(nfs_vfsops, nfs, VFCF_NETWORK); 137 138 /* 139 * This structure must be filled in by a primary bootstrap or bootstrap 140 * server for a diskless/dataless machine. It is initialized below just 141 * to ensure that it is allocated to initialized data (.data not .bss). 142 */ 143 struct nfs_diskless nfs_diskless = { { { 0 } } }; 144 struct nfsv3_diskless nfsv3_diskless = { { { 0 } } }; 145 int nfs_diskless_valid = 0; 146 147 SYSCTL_INT(_vfs_nfs, OID_AUTO, diskless_valid, CTLFLAG_RD, 148 &nfs_diskless_valid, 0, ""); 149 150 SYSCTL_STRING(_vfs_nfs, OID_AUTO, diskless_rootpath, CTLFLAG_RD, 151 nfsv3_diskless.root_hostnam, 0, ""); 152 153 SYSCTL_OPAQUE(_vfs_nfs, OID_AUTO, diskless_rootaddr, CTLFLAG_RD, 154 &nfsv3_diskless.root_saddr, sizeof nfsv3_diskless.root_saddr, 155 "%Ssockaddr_in", ""); 156 157 SYSCTL_STRING(_vfs_nfs, OID_AUTO, diskless_swappath, CTLFLAG_RD, 158 nfsv3_diskless.swap_hostnam, 0, ""); 159 160 SYSCTL_OPAQUE(_vfs_nfs, OID_AUTO, diskless_swapaddr, CTLFLAG_RD, 161 &nfsv3_diskless.swap_saddr, sizeof nfsv3_diskless.swap_saddr, 162 "%Ssockaddr_in",""); 163 164 165 void nfsargs_ntoh (struct nfs_args *); 166 static int nfs_mountdiskless (char *, char *, int, 167 struct sockaddr_in *, struct nfs_args *, 168 struct thread *, struct vnode **, 169 struct mount **); 170 static void nfs_convert_diskless (void); 171 static void nfs_convert_oargs (struct nfs_args *args, 172 struct onfs_args *oargs); 173 174 static int 175 nfs_iosize(nmp) 176 struct nfsmount* nmp; 177 { 178 int iosize; 179 180 /* 181 * Calculate the size used for io buffers. Use the larger 182 * of the two sizes to minimise nfs requests but make sure 183 * that it is at least one VM page to avoid wasting buffer 184 * space. 185 */ 186 iosize = max(nmp->nm_rsize, nmp->nm_wsize); 187 if (iosize < PAGE_SIZE) iosize = PAGE_SIZE; 188 return iosize; 189 } 190 191 static void 192 nfs_convert_oargs(args, oargs) 193 struct nfs_args *args; 194 struct onfs_args *oargs; 195 { 196 args->version = NFS_ARGSVERSION; 197 args->addr = oargs->addr; 198 args->addrlen = oargs->addrlen; 199 args->sotype = oargs->sotype; 200 args->proto = oargs->proto; 201 args->fh = oargs->fh; 202 args->fhsize = oargs->fhsize; 203 args->flags = oargs->flags; 204 args->wsize = oargs->wsize; 205 args->rsize = oargs->rsize; 206 args->readdirsize = oargs->readdirsize; 207 args->timeo = oargs->timeo; 208 args->retrans = oargs->retrans; 209 args->maxgrouplist = oargs->maxgrouplist; 210 args->readahead = oargs->readahead; 211 args->leaseterm = oargs->leaseterm; 212 args->deadthresh = oargs->deadthresh; 213 args->hostname = oargs->hostname; 214 } 215 216 static void 217 nfs_convert_diskless() 218 { 219 bcopy(&nfs_diskless.myif, &nfsv3_diskless.myif, 220 sizeof(struct ifaliasreq)); 221 bcopy(&nfs_diskless.mygateway, &nfsv3_diskless.mygateway, 222 sizeof(struct sockaddr_in)); 223 nfs_convert_oargs(&nfsv3_diskless.swap_args,&nfs_diskless.swap_args); 224 nfsv3_diskless.swap_fhsize = NFSX_V2FH; 225 bcopy(nfs_diskless.swap_fh,nfsv3_diskless.swap_fh,NFSX_V2FH); 226 bcopy(&nfs_diskless.swap_saddr,&nfsv3_diskless.swap_saddr, 227 sizeof(struct sockaddr_in)); 228 bcopy(nfs_diskless.swap_hostnam,nfsv3_diskless.swap_hostnam, MNAMELEN); 229 nfsv3_diskless.swap_nblks = nfs_diskless.swap_nblks; 230 bcopy(&nfs_diskless.swap_ucred, &nfsv3_diskless.swap_ucred, 231 sizeof(struct ucred)); 232 nfs_convert_oargs(&nfsv3_diskless.root_args,&nfs_diskless.root_args); 233 nfsv3_diskless.root_fhsize = NFSX_V2FH; 234 bcopy(nfs_diskless.root_fh,nfsv3_diskless.root_fh,NFSX_V2FH); 235 bcopy(&nfs_diskless.root_saddr,&nfsv3_diskless.root_saddr, 236 sizeof(struct sockaddr_in)); 237 bcopy(nfs_diskless.root_hostnam,nfsv3_diskless.root_hostnam, MNAMELEN); 238 nfsv3_diskless.root_time = nfs_diskless.root_time; 239 bcopy(nfs_diskless.my_hostnam,nfsv3_diskless.my_hostnam, 240 MAXHOSTNAMELEN); 241 nfs_diskless_valid = 3; 242 } 243 244 /* 245 * nfs statfs call 246 */ 247 int 248 nfs_statfs(struct mount *mp, struct statfs *sbp, struct thread *td) 249 { 250 struct vnode *vp; 251 struct nfs_statfs *sfp; 252 caddr_t cp; 253 u_int32_t *tl; 254 int32_t t1, t2; 255 caddr_t bpos, dpos, cp2; 256 struct nfsmount *nmp = VFSTONFS(mp); 257 int error = 0, v3 = (nmp->nm_flag & NFSMNT_NFSV3), retattr; 258 struct mbuf *mreq, *mrep, *md, *mb, *mb2; 259 struct ucred *cred; 260 struct nfsnode *np; 261 u_quad_t tquad; 262 263 #ifndef nolint 264 sfp = (struct nfs_statfs *)0; 265 #endif 266 error = nfs_nget(mp, (nfsfh_t *)nmp->nm_fh, nmp->nm_fhsize, &np); 267 if (error) 268 return (error); 269 vp = NFSTOV(np); 270 cred = crget(); 271 cred->cr_ngroups = 1; 272 if (v3 && (nmp->nm_state & NFSSTA_GOTFSINFO) == 0) 273 (void)nfs_fsinfo(nmp, vp, td); 274 nfsstats.rpccnt[NFSPROC_FSSTAT]++; 275 nfsm_reqhead(vp, NFSPROC_FSSTAT, NFSX_FH(v3)); 276 nfsm_fhtom(vp, v3); 277 nfsm_request(vp, NFSPROC_FSSTAT, td, cred); 278 if (v3) 279 nfsm_postop_attr(vp, retattr); 280 if (error) { 281 if (mrep != NULL) 282 m_freem(mrep); 283 goto nfsmout; 284 } 285 nfsm_dissect(sfp, struct nfs_statfs *, NFSX_STATFS(v3)); 286 sbp->f_flags = nmp->nm_flag; 287 sbp->f_iosize = nfs_iosize(nmp); 288 if (v3) { 289 sbp->f_bsize = NFS_FABLKSIZE; 290 tquad = fxdr_hyper(&sfp->sf_tbytes); 291 sbp->f_blocks = (long)(tquad / ((u_quad_t)NFS_FABLKSIZE)); 292 tquad = fxdr_hyper(&sfp->sf_fbytes); 293 sbp->f_bfree = (long)(tquad / ((u_quad_t)NFS_FABLKSIZE)); 294 tquad = fxdr_hyper(&sfp->sf_abytes); 295 sbp->f_bavail = (long)(tquad / ((u_quad_t)NFS_FABLKSIZE)); 296 sbp->f_files = (fxdr_unsigned(int32_t, 297 sfp->sf_tfiles.nfsuquad[1]) & 0x7fffffff); 298 sbp->f_ffree = (fxdr_unsigned(int32_t, 299 sfp->sf_ffiles.nfsuquad[1]) & 0x7fffffff); 300 } else { 301 sbp->f_bsize = fxdr_unsigned(int32_t, sfp->sf_bsize); 302 sbp->f_blocks = fxdr_unsigned(int32_t, sfp->sf_blocks); 303 sbp->f_bfree = fxdr_unsigned(int32_t, sfp->sf_bfree); 304 sbp->f_bavail = fxdr_unsigned(int32_t, sfp->sf_bavail); 305 sbp->f_files = 0; 306 sbp->f_ffree = 0; 307 } 308 if (sbp != &mp->mnt_stat) { 309 sbp->f_type = mp->mnt_vfc->vfc_typenum; 310 bcopy(mp->mnt_stat.f_mntonname, sbp->f_mntonname, MNAMELEN); 311 bcopy(mp->mnt_stat.f_mntfromname, sbp->f_mntfromname, MNAMELEN); 312 } 313 m_freem(mrep); 314 nfsmout: 315 vput(vp); 316 crfree(cred); 317 return (error); 318 } 319 320 /* 321 * nfs version 3 fsinfo rpc call 322 */ 323 int 324 nfs_fsinfo(struct nfsmount *nmp, struct vnode *vp, struct thread *td) 325 { 326 struct nfsv3_fsinfo *fsp; 327 caddr_t cp; 328 int32_t t1, t2; 329 u_int32_t *tl, pref, max; 330 caddr_t bpos, dpos, cp2; 331 int error = 0, retattr; 332 struct mbuf *mreq, *mrep, *md, *mb, *mb2; 333 u_int64_t maxfsize; 334 335 nfsstats.rpccnt[NFSPROC_FSINFO]++; 336 nfsm_reqhead(vp, NFSPROC_FSINFO, NFSX_FH(1)); 337 nfsm_fhtom(vp, 1); 338 nfsm_request(vp, NFSPROC_FSINFO, td, nfs_vpcred(vp, ND_READ)); 339 nfsm_postop_attr(vp, retattr); 340 if (!error) { 341 nfsm_dissect(fsp, struct nfsv3_fsinfo *, NFSX_V3FSINFO); 342 pref = fxdr_unsigned(u_int32_t, fsp->fs_wtpref); 343 if (pref < nmp->nm_wsize && pref >= NFS_FABLKSIZE) 344 nmp->nm_wsize = (pref + NFS_FABLKSIZE - 1) & 345 ~(NFS_FABLKSIZE - 1); 346 max = fxdr_unsigned(u_int32_t, fsp->fs_wtmax); 347 if (max < nmp->nm_wsize && max > 0) { 348 nmp->nm_wsize = max & ~(NFS_FABLKSIZE - 1); 349 if (nmp->nm_wsize == 0) 350 nmp->nm_wsize = max; 351 } 352 pref = fxdr_unsigned(u_int32_t, fsp->fs_rtpref); 353 if (pref < nmp->nm_rsize && pref >= NFS_FABLKSIZE) 354 nmp->nm_rsize = (pref + NFS_FABLKSIZE - 1) & 355 ~(NFS_FABLKSIZE - 1); 356 max = fxdr_unsigned(u_int32_t, fsp->fs_rtmax); 357 if (max < nmp->nm_rsize && max > 0) { 358 nmp->nm_rsize = max & ~(NFS_FABLKSIZE - 1); 359 if (nmp->nm_rsize == 0) 360 nmp->nm_rsize = max; 361 } 362 pref = fxdr_unsigned(u_int32_t, fsp->fs_dtpref); 363 if (pref < nmp->nm_readdirsize && pref >= NFS_DIRBLKSIZ) 364 nmp->nm_readdirsize = (pref + NFS_DIRBLKSIZ - 1) & 365 ~(NFS_DIRBLKSIZ - 1); 366 if (max < nmp->nm_readdirsize && max > 0) { 367 nmp->nm_readdirsize = max & ~(NFS_DIRBLKSIZ - 1); 368 if (nmp->nm_readdirsize == 0) 369 nmp->nm_readdirsize = max; 370 } 371 maxfsize = fxdr_hyper(&fsp->fs_maxfilesize); 372 if (maxfsize > 0 && maxfsize < nmp->nm_maxfilesize) 373 nmp->nm_maxfilesize = maxfsize; 374 nmp->nm_state |= NFSSTA_GOTFSINFO; 375 } 376 m_freem(mrep); 377 nfsmout: 378 return (error); 379 } 380 381 /* 382 * Mount a remote root fs via. nfs. This depends on the info in the 383 * nfs_diskless structure that has been filled in properly by some primary 384 * bootstrap. 385 * It goes something like this: 386 * - do enough of "ifconfig" by calling ifioctl() so that the system 387 * can talk to the server 388 * - If nfs_diskless.mygateway is filled in, use that address as 389 * a default gateway. 390 * - build the rootfs mount point and call mountnfs() to do the rest. 391 */ 392 int 393 nfs_mountroot(mp) 394 struct mount *mp; 395 { 396 struct mount *swap_mp; 397 struct nfsv3_diskless *nd = &nfsv3_diskless; 398 struct socket *so; 399 struct vnode *vp; 400 struct thread *td = curthread; /* XXX */ 401 int error, i; 402 u_long l; 403 char buf[128]; 404 405 #if defined(BOOTP_NFSROOT) && defined(BOOTP) 406 bootpc_init(); /* use bootp to get nfs_diskless filled in */ 407 #endif 408 409 /* 410 * XXX time must be non-zero when we init the interface or else 411 * the arp code will wedge... 412 */ 413 while (mycpu->gd_time_seconds == 0) 414 tsleep(mycpu, 0, "arpkludge", 10); 415 416 if (nfs_diskless_valid==1) 417 nfs_convert_diskless(); 418 419 /* 420 * XXX splnet, so networks will receive... 421 */ 422 splnet(); 423 424 #ifdef notyet 425 /* Set up swap credentials. */ 426 proc0.p_ucred->cr_uid = ntohl(nd->swap_ucred.cr_uid); 427 proc0.p_ucred->cr_gid = ntohl(nd->swap_ucred.cr_gid); 428 if ((proc0.p_ucred->cr_ngroups = ntohs(nd->swap_ucred.cr_ngroups)) > 429 NGROUPS) 430 proc0.p_ucred->cr_ngroups = NGROUPS; 431 for (i = 0; i < proc0.p_ucred->cr_ngroups; i++) 432 proc0.p_ucred->cr_groups[i] = ntohl(nd->swap_ucred.cr_groups[i]); 433 #endif 434 435 /* 436 * Do enough of ifconfig(8) so that the critical net interface can 437 * talk to the server. 438 */ 439 error = socreate(nd->myif.ifra_addr.sa_family, &so, SOCK_DGRAM, 0, td); 440 if (error) 441 panic("nfs_mountroot: socreate(%04x): %d", 442 nd->myif.ifra_addr.sa_family, error); 443 444 #if 0 /* XXX Bad idea */ 445 /* 446 * We might not have been told the right interface, so we pass 447 * over the first ten interfaces of the same kind, until we get 448 * one of them configured. 449 */ 450 451 for (i = strlen(nd->myif.ifra_name) - 1; 452 nd->myif.ifra_name[i] >= '0' && 453 nd->myif.ifra_name[i] <= '9'; 454 nd->myif.ifra_name[i] ++) { 455 error = ifioctl(so, SIOCAIFADDR, (caddr_t)&nd->myif, td); 456 if(!error) 457 break; 458 } 459 #endif 460 error = ifioctl(so, SIOCAIFADDR, (caddr_t)&nd->myif, td); 461 if (error) 462 panic("nfs_mountroot: SIOCAIFADDR: %d", error); 463 soclose(so); 464 465 /* 466 * If the gateway field is filled in, set it as the default route. 467 */ 468 if (nd->mygateway.sin_len != 0) { 469 struct sockaddr_in mask, sin; 470 471 bzero((caddr_t)&mask, sizeof(mask)); 472 sin = mask; 473 sin.sin_family = AF_INET; 474 sin.sin_len = sizeof(sin); 475 error = rtrequest(RTM_ADD, (struct sockaddr *)&sin, 476 (struct sockaddr *)&nd->mygateway, 477 (struct sockaddr *)&mask, 478 RTF_UP | RTF_GATEWAY, (struct rtentry **)0); 479 if (error) 480 panic("nfs_mountroot: RTM_ADD: %d", error); 481 } 482 483 /* 484 * Create the rootfs mount point. 485 */ 486 nd->root_args.fh = nd->root_fh; 487 nd->root_args.fhsize = nd->root_fhsize; 488 l = ntohl(nd->root_saddr.sin_addr.s_addr); 489 snprintf(buf, sizeof(buf), "%ld.%ld.%ld.%ld:%s", 490 (l >> 24) & 0xff, (l >> 16) & 0xff, 491 (l >> 8) & 0xff, (l >> 0) & 0xff,nd->root_hostnam); 492 printf("NFS ROOT: %s\n",buf); 493 if ((error = nfs_mountdiskless(buf, "/", MNT_RDONLY, 494 &nd->root_saddr, &nd->root_args, td, &vp, &mp)) != 0) { 495 if (swap_mp) { 496 mp->mnt_vfc->vfc_refcount--; 497 free(swap_mp, M_MOUNT); 498 } 499 return (error); 500 } 501 502 swap_mp = NULL; 503 if (nd->swap_nblks) { 504 505 /* Convert to DEV_BSIZE instead of Kilobyte */ 506 nd->swap_nblks *= 2; 507 508 /* 509 * Create a fake mount point just for the swap vnode so that the 510 * swap file can be on a different server from the rootfs. 511 */ 512 nd->swap_args.fh = nd->swap_fh; 513 nd->swap_args.fhsize = nd->swap_fhsize; 514 l = ntohl(nd->swap_saddr.sin_addr.s_addr); 515 snprintf(buf, sizeof(buf), "%ld.%ld.%ld.%ld:%s", 516 (l >> 24) & 0xff, (l >> 16) & 0xff, 517 (l >> 8) & 0xff, (l >> 0) & 0xff,nd->swap_hostnam); 518 printf("NFS SWAP: %s\n",buf); 519 if ((error = nfs_mountdiskless(buf, "/swap", 0, 520 &nd->swap_saddr, &nd->swap_args, td, &vp, &swap_mp)) != 0) 521 return (error); 522 vfs_unbusy(swap_mp, td); 523 524 VTONFS(vp)->n_size = VTONFS(vp)->n_vattr.va_size = 525 nd->swap_nblks * DEV_BSIZE ; 526 527 /* 528 * Since the swap file is not the root dir of a file system, 529 * hack it to a regular file. 530 */ 531 vp->v_type = VREG; 532 vp->v_flag = 0; 533 VREF(vp); 534 swaponvp(td, vp, NODEV, nd->swap_nblks); 535 } 536 537 mp->mnt_flag |= MNT_ROOTFS; 538 mp->mnt_vnodecovered = NULLVP; 539 rootvp = vp; 540 vfs_unbusy(mp, td); 541 542 /* 543 * This is not really an nfs issue, but it is much easier to 544 * set hostname here and then let the "/etc/rc.xxx" files 545 * mount the right /var based upon its preset value. 546 */ 547 bcopy(nd->my_hostnam, hostname, MAXHOSTNAMELEN); 548 hostname[MAXHOSTNAMELEN - 1] = '\0'; 549 for (i = 0; i < MAXHOSTNAMELEN; i++) 550 if (hostname[i] == '\0') 551 break; 552 inittodr(ntohl(nd->root_time)); 553 return (0); 554 } 555 556 /* 557 * Internal version of mount system call for diskless setup. 558 */ 559 static int 560 nfs_mountdiskless(char *path, char *which, int mountflag, 561 struct sockaddr_in *sin, struct nfs_args *args, struct thread *td, 562 struct vnode **vpp, struct mount **mpp) 563 { 564 struct mount *mp; 565 struct sockaddr *nam; 566 int error; 567 int didalloc = 0; 568 569 mp = *mpp; 570 571 if (mp == NULL) { 572 if ((error = vfs_rootmountalloc("nfs", path, &mp)) != 0) { 573 printf("nfs_mountroot: NFS not configured"); 574 return (error); 575 } 576 didalloc = 1; 577 } 578 579 mp->mnt_kern_flag = 0; 580 mp->mnt_flag = mountflag; 581 nam = dup_sockaddr((struct sockaddr *)sin, 1); 582 if ((error = mountnfs(args, mp, nam, which, path, vpp)) != 0) { 583 printf("nfs_mountroot: mount %s on %s: %d", path, which, error); 584 mp->mnt_vfc->vfc_refcount--; 585 vfs_unbusy(mp, td); 586 if (didalloc) 587 free(mp, M_MOUNT); 588 FREE(nam, M_SONAME); 589 return (error); 590 } 591 (void) copystr(which, mp->mnt_stat.f_mntonname, MNAMELEN - 1, 0); 592 *mpp = mp; 593 return (0); 594 } 595 596 static void 597 nfs_decode_args(nmp, argp) 598 struct nfsmount *nmp; 599 struct nfs_args *argp; 600 { 601 int s; 602 int adjsock; 603 int maxio; 604 605 s = splnet(); 606 /* 607 * Silently clear NFSMNT_NOCONN if it's a TCP mount, it makes 608 * no sense in that context. 609 */ 610 if (argp->sotype == SOCK_STREAM) 611 nmp->nm_flag &= ~NFSMNT_NOCONN; 612 613 /* Also clear RDIRPLUS if not NFSv3, it crashes some servers */ 614 if ((argp->flags & NFSMNT_NFSV3) == 0) 615 nmp->nm_flag &= ~NFSMNT_RDIRPLUS; 616 617 /* Re-bind if rsrvd port requested and wasn't on one */ 618 adjsock = !(nmp->nm_flag & NFSMNT_RESVPORT) 619 && (argp->flags & NFSMNT_RESVPORT); 620 /* Also re-bind if we're switching to/from a connected UDP socket */ 621 adjsock |= ((nmp->nm_flag & NFSMNT_NOCONN) != 622 (argp->flags & NFSMNT_NOCONN)); 623 624 /* Update flags atomically. Don't change the lock bits. */ 625 nmp->nm_flag = argp->flags | nmp->nm_flag; 626 splx(s); 627 628 if ((argp->flags & NFSMNT_TIMEO) && argp->timeo > 0) { 629 nmp->nm_timeo = (argp->timeo * NFS_HZ + 5) / 10; 630 if (nmp->nm_timeo < NFS_MINTIMEO) 631 nmp->nm_timeo = NFS_MINTIMEO; 632 else if (nmp->nm_timeo > NFS_MAXTIMEO) 633 nmp->nm_timeo = NFS_MAXTIMEO; 634 } 635 636 if ((argp->flags & NFSMNT_RETRANS) && argp->retrans > 1) { 637 nmp->nm_retry = argp->retrans; 638 if (nmp->nm_retry > NFS_MAXREXMIT) 639 nmp->nm_retry = NFS_MAXREXMIT; 640 } 641 642 if (argp->flags & NFSMNT_NFSV3) { 643 if (argp->sotype == SOCK_DGRAM) 644 maxio = NFS_MAXDGRAMDATA; 645 else 646 maxio = NFS_MAXDATA; 647 } else 648 maxio = NFS_V2MAXDATA; 649 650 if ((argp->flags & NFSMNT_WSIZE) && argp->wsize > 0) { 651 nmp->nm_wsize = argp->wsize; 652 /* Round down to multiple of blocksize */ 653 nmp->nm_wsize &= ~(NFS_FABLKSIZE - 1); 654 if (nmp->nm_wsize <= 0) 655 nmp->nm_wsize = NFS_FABLKSIZE; 656 } 657 if (nmp->nm_wsize > maxio) 658 nmp->nm_wsize = maxio; 659 if (nmp->nm_wsize > MAXBSIZE) 660 nmp->nm_wsize = MAXBSIZE; 661 662 if ((argp->flags & NFSMNT_RSIZE) && argp->rsize > 0) { 663 nmp->nm_rsize = argp->rsize; 664 /* Round down to multiple of blocksize */ 665 nmp->nm_rsize &= ~(NFS_FABLKSIZE - 1); 666 if (nmp->nm_rsize <= 0) 667 nmp->nm_rsize = NFS_FABLKSIZE; 668 } 669 if (nmp->nm_rsize > maxio) 670 nmp->nm_rsize = maxio; 671 if (nmp->nm_rsize > MAXBSIZE) 672 nmp->nm_rsize = MAXBSIZE; 673 674 if ((argp->flags & NFSMNT_READDIRSIZE) && argp->readdirsize > 0) { 675 nmp->nm_readdirsize = argp->readdirsize; 676 } 677 if (nmp->nm_readdirsize > maxio) 678 nmp->nm_readdirsize = maxio; 679 if (nmp->nm_readdirsize > nmp->nm_rsize) 680 nmp->nm_readdirsize = nmp->nm_rsize; 681 682 if ((argp->flags & NFSMNT_ACREGMIN) && argp->acregmin >= 0) 683 nmp->nm_acregmin = argp->acregmin; 684 else 685 nmp->nm_acregmin = NFS_MINATTRTIMO; 686 if ((argp->flags & NFSMNT_ACREGMAX) && argp->acregmax >= 0) 687 nmp->nm_acregmax = argp->acregmax; 688 else 689 nmp->nm_acregmax = NFS_MAXATTRTIMO; 690 if ((argp->flags & NFSMNT_ACDIRMIN) && argp->acdirmin >= 0) 691 nmp->nm_acdirmin = argp->acdirmin; 692 else 693 nmp->nm_acdirmin = NFS_MINDIRATTRTIMO; 694 if ((argp->flags & NFSMNT_ACDIRMAX) && argp->acdirmax >= 0) 695 nmp->nm_acdirmax = argp->acdirmax; 696 else 697 nmp->nm_acdirmax = NFS_MAXDIRATTRTIMO; 698 if (nmp->nm_acdirmin > nmp->nm_acdirmax) 699 nmp->nm_acdirmin = nmp->nm_acdirmax; 700 if (nmp->nm_acregmin > nmp->nm_acregmax) 701 nmp->nm_acregmin = nmp->nm_acregmax; 702 703 if ((argp->flags & NFSMNT_MAXGRPS) && argp->maxgrouplist >= 0) { 704 if (argp->maxgrouplist <= NFS_MAXGRPS) 705 nmp->nm_numgrps = argp->maxgrouplist; 706 else 707 nmp->nm_numgrps = NFS_MAXGRPS; 708 } 709 if ((argp->flags & NFSMNT_READAHEAD) && argp->readahead >= 0) { 710 if (argp->readahead <= NFS_MAXRAHEAD) 711 nmp->nm_readahead = argp->readahead; 712 else 713 nmp->nm_readahead = NFS_MAXRAHEAD; 714 } 715 if ((argp->flags & NFSMNT_LEASETERM) && argp->leaseterm >= 2) { 716 if (argp->leaseterm <= NQ_MAXLEASE) 717 nmp->nm_leaseterm = argp->leaseterm; 718 else 719 nmp->nm_leaseterm = NQ_MAXLEASE; 720 } 721 if ((argp->flags & NFSMNT_DEADTHRESH) && argp->deadthresh >= 1) { 722 if (argp->deadthresh <= NQ_NEVERDEAD) 723 nmp->nm_deadthresh = argp->deadthresh; 724 else 725 nmp->nm_deadthresh = NQ_NEVERDEAD; 726 } 727 728 adjsock |= ((nmp->nm_sotype != argp->sotype) || 729 (nmp->nm_soproto != argp->proto)); 730 nmp->nm_sotype = argp->sotype; 731 nmp->nm_soproto = argp->proto; 732 733 if (nmp->nm_so && adjsock) { 734 nfs_safedisconnect(nmp); 735 if (nmp->nm_sotype == SOCK_DGRAM) 736 while (nfs_connect(nmp, (struct nfsreq *)0)) { 737 printf("nfs_args: retrying connect\n"); 738 (void) tsleep((caddr_t)&lbolt, 0, "nfscon", 0); 739 } 740 } 741 } 742 743 /* 744 * VFS Operations. 745 * 746 * mount system call 747 * It seems a bit dumb to copyinstr() the host and path here and then 748 * bcopy() them in mountnfs(), but I wanted to detect errors before 749 * doing the sockargs() call because sockargs() allocates an mbuf and 750 * an error after that means that I have to release the mbuf. 751 */ 752 /* ARGSUSED */ 753 static int 754 nfs_mount(struct mount *mp, char *path, caddr_t data, 755 struct nameidata *ndp, struct thread *td) 756 { 757 int error; 758 struct nfs_args args; 759 struct sockaddr *nam; 760 struct vnode *vp; 761 char pth[MNAMELEN], hst[MNAMELEN]; 762 size_t len; 763 u_char nfh[NFSX_V3FHMAX]; 764 765 if (path == NULL) { 766 nfs_mountroot(mp); 767 return (0); 768 } 769 error = copyin(data, (caddr_t)&args, sizeof (struct nfs_args)); 770 if (error) 771 return (error); 772 if (args.version != NFS_ARGSVERSION) { 773 #ifdef COMPAT_PRELITE2 774 /* 775 * If the argument version is unknown, then assume the 776 * caller is a pre-lite2 4.4BSD client and convert its 777 * arguments. 778 */ 779 struct onfs_args oargs; 780 error = copyin(data, (caddr_t)&oargs, sizeof (struct onfs_args)); 781 if (error) 782 return (error); 783 nfs_convert_oargs(&args,&oargs); 784 #else /* !COMPAT_PRELITE2 */ 785 return (EPROGMISMATCH); 786 #endif /* COMPAT_PRELITE2 */ 787 } 788 if (mp->mnt_flag & MNT_UPDATE) { 789 struct nfsmount *nmp = VFSTONFS(mp); 790 791 if (nmp == NULL) 792 return (EIO); 793 /* 794 * When doing an update, we can't change from or to 795 * v3 and/or nqnfs, or change cookie translation 796 */ 797 args.flags = (args.flags & 798 ~(NFSMNT_NFSV3|NFSMNT_NQNFS /*|NFSMNT_XLATECOOKIE*/)) | 799 (nmp->nm_flag & 800 (NFSMNT_NFSV3|NFSMNT_NQNFS /*|NFSMNT_XLATECOOKIE*/)); 801 nfs_decode_args(nmp, &args); 802 return (0); 803 } 804 805 /* 806 * Make the nfs_ip_paranoia sysctl serve as the default connection 807 * or no-connection mode for those protocols that support 808 * no-connection mode (the flag will be cleared later for protocols 809 * that do not support no-connection mode). This will allow a client 810 * to receive replies from a different IP then the request was 811 * sent to. Note: default value for nfs_ip_paranoia is 1 (paranoid), 812 * not 0. 813 */ 814 if (nfs_ip_paranoia == 0) 815 args.flags |= NFSMNT_NOCONN; 816 if (args.fhsize < 0 || args.fhsize > NFSX_V3FHMAX) 817 return (EINVAL); 818 error = copyin((caddr_t)args.fh, (caddr_t)nfh, args.fhsize); 819 if (error) 820 return (error); 821 error = copyinstr(path, pth, MNAMELEN-1, &len); 822 if (error) 823 return (error); 824 bzero(&pth[len], MNAMELEN - len); 825 error = copyinstr(args.hostname, hst, MNAMELEN-1, &len); 826 if (error) 827 return (error); 828 bzero(&hst[len], MNAMELEN - len); 829 /* sockargs() call must be after above copyin() calls */ 830 error = getsockaddr(&nam, (caddr_t)args.addr, args.addrlen); 831 if (error) 832 return (error); 833 args.fh = nfh; 834 error = mountnfs(&args, mp, nam, pth, hst, &vp); 835 return (error); 836 } 837 838 /* 839 * Common code for mount and mountroot 840 */ 841 static int 842 mountnfs(struct nfs_args *argp, struct mount *mp, struct sockaddr *nam, 843 char *pth, char *hst, struct vnode **vpp) 844 { 845 struct nfsmount *nmp; 846 struct nfsnode *np; 847 int error; 848 struct vattr attrs; 849 850 if (mp->mnt_flag & MNT_UPDATE) { 851 nmp = VFSTONFS(mp); 852 /* update paths, file handles, etc, here XXX */ 853 FREE(nam, M_SONAME); 854 return (0); 855 } else { 856 nmp = zalloc(nfsmount_zone); 857 bzero((caddr_t)nmp, sizeof (struct nfsmount)); 858 TAILQ_INIT(&nmp->nm_uidlruhead); 859 TAILQ_INIT(&nmp->nm_bufq); 860 mp->mnt_data = (qaddr_t)nmp; 861 } 862 vfs_getnewfsid(mp); 863 nmp->nm_mountp = mp; 864 if (argp->flags & NFSMNT_NQNFS) 865 /* 866 * We have to set mnt_maxsymlink to a non-zero value so 867 * that COMPAT_43 routines will know that we are setting 868 * the d_type field in directories (and can zero it for 869 * unsuspecting binaries). 870 */ 871 mp->mnt_maxsymlinklen = 1; 872 873 /* 874 * V2 can only handle 32 bit filesizes. A 4GB-1 limit may be too 875 * high, depending on whether we end up with negative offsets in 876 * the client or server somewhere. 2GB-1 may be safer. 877 * 878 * For V3, nfs_fsinfo will adjust this as necessary. Assume maximum 879 * that we can handle until we find out otherwise. 880 * XXX Our "safe" limit on the client is what we can store in our 881 * buffer cache using signed(!) block numbers. 882 */ 883 if ((argp->flags & NFSMNT_NFSV3) == 0) 884 nmp->nm_maxfilesize = 0xffffffffLL; 885 else 886 nmp->nm_maxfilesize = (u_int64_t)0x80000000 * DEV_BSIZE - 1; 887 888 nmp->nm_timeo = NFS_TIMEO; 889 nmp->nm_retry = NFS_RETRANS; 890 nmp->nm_wsize = NFS_WSIZE; 891 nmp->nm_rsize = NFS_RSIZE; 892 nmp->nm_readdirsize = NFS_READDIRSIZE; 893 nmp->nm_numgrps = NFS_MAXGRPS; 894 nmp->nm_readahead = NFS_DEFRAHEAD; 895 nmp->nm_leaseterm = NQ_DEFLEASE; 896 nmp->nm_deadthresh = NQ_DEADTHRESH; 897 CIRCLEQ_INIT(&nmp->nm_timerhead); 898 nmp->nm_inprog = NULLVP; 899 nmp->nm_fhsize = argp->fhsize; 900 bcopy((caddr_t)argp->fh, (caddr_t)nmp->nm_fh, argp->fhsize); 901 bcopy(hst, mp->mnt_stat.f_mntfromname, MNAMELEN); 902 bcopy(pth, mp->mnt_stat.f_mntonname, MNAMELEN); 903 nmp->nm_nam = nam; 904 /* Set up the sockets and per-host congestion */ 905 nmp->nm_sotype = argp->sotype; 906 nmp->nm_soproto = argp->proto; 907 nmp->nm_cred = crhold(proc0.p_ucred); 908 909 nfs_decode_args(nmp, argp); 910 911 /* 912 * For Connection based sockets (TCP,...) defer the connect until 913 * the first request, in case the server is not responding. 914 */ 915 if (nmp->nm_sotype == SOCK_DGRAM && 916 (error = nfs_connect(nmp, (struct nfsreq *)0))) 917 goto bad; 918 919 /* 920 * This is silly, but it has to be set so that vinifod() works. 921 * We do not want to do an nfs_statfs() here since we can get 922 * stuck on a dead server and we are holding a lock on the mount 923 * point. 924 */ 925 mp->mnt_stat.f_iosize = nfs_iosize(nmp); 926 /* 927 * A reference count is needed on the nfsnode representing the 928 * remote root. If this object is not persistent, then backward 929 * traversals of the mount point (i.e. "..") will not work if 930 * the nfsnode gets flushed out of the cache. Ufs does not have 931 * this problem, because one can identify root inodes by their 932 * number == ROOTINO (2). 933 */ 934 error = nfs_nget(mp, (nfsfh_t *)nmp->nm_fh, nmp->nm_fhsize, &np); 935 if (error) 936 goto bad; 937 *vpp = NFSTOV(np); 938 939 /* 940 * Get file attributes for the mountpoint. This has the side 941 * effect of filling in (*vpp)->v_type with the correct value. 942 */ 943 VOP_GETATTR(*vpp, &attrs, curthread); 944 945 /* 946 * Lose the lock but keep the ref. 947 */ 948 VOP_UNLOCK(*vpp, 0, curthread); 949 950 return (0); 951 bad: 952 nfs_disconnect(nmp); 953 nfs_free_mount(nmp); 954 FREE(nam, M_SONAME); 955 return (error); 956 } 957 958 /* 959 * unmount system call 960 */ 961 static int 962 nfs_unmount(struct mount *mp, int mntflags, struct thread *td) 963 { 964 struct nfsmount *nmp; 965 int error, flags = 0; 966 967 if (mntflags & MNT_FORCE) 968 flags |= FORCECLOSE; 969 nmp = VFSTONFS(mp); 970 /* 971 * Goes something like this.. 972 * - Call vflush() to clear out vnodes for this file system 973 * - Close the socket 974 * - Free up the data structures 975 */ 976 /* In the forced case, cancel any outstanding requests. */ 977 if (flags & FORCECLOSE) { 978 error = nfs_nmcancelreqs(nmp); 979 if (error) 980 return (error); 981 } 982 /* 983 * Must handshake with nqnfs_clientd() if it is active. 984 */ 985 nmp->nm_state |= NFSSTA_DISMINPROG; 986 while (nmp->nm_inprog != NULLVP) 987 (void) tsleep((caddr_t)&lbolt, 0, "nfsdism", 0); 988 989 /* We hold 1 extra ref on the root vnode; see comment in mountnfs(). */ 990 error = vflush(mp, 1, flags); 991 if (error) { 992 nmp->nm_state &= ~NFSSTA_DISMINPROG; 993 return (error); 994 } 995 996 /* 997 * We are now committed to the unmount. 998 * For NQNFS, let the server daemon free the nfsmount structure. 999 */ 1000 if (nmp->nm_flag & (NFSMNT_NQNFS | NFSMNT_KERB)) 1001 nmp->nm_state |= NFSSTA_DISMNT; 1002 1003 nfs_disconnect(nmp); 1004 FREE(nmp->nm_nam, M_SONAME); 1005 1006 if ((nmp->nm_flag & (NFSMNT_NQNFS | NFSMNT_KERB)) == 0) 1007 nfs_free_mount(nmp); 1008 return (0); 1009 } 1010 1011 void 1012 nfs_free_mount(struct nfsmount *nmp) 1013 { 1014 if (nmp->nm_cred) { 1015 crfree(nmp->nm_cred); 1016 nmp->nm_cred = NULL; 1017 } 1018 zfree(nfsmount_zone, nmp); 1019 } 1020 1021 /* 1022 * Return root of a filesystem 1023 */ 1024 static int 1025 nfs_root(mp, vpp) 1026 struct mount *mp; 1027 struct vnode **vpp; 1028 { 1029 struct vnode *vp; 1030 struct nfsmount *nmp; 1031 struct nfsnode *np; 1032 int error; 1033 1034 nmp = VFSTONFS(mp); 1035 error = nfs_nget(mp, (nfsfh_t *)nmp->nm_fh, nmp->nm_fhsize, &np); 1036 if (error) 1037 return (error); 1038 vp = NFSTOV(np); 1039 if (vp->v_type == VNON) 1040 vp->v_type = VDIR; 1041 vp->v_flag = VROOT; 1042 *vpp = vp; 1043 return (0); 1044 } 1045 1046 extern int syncprt; 1047 1048 /* 1049 * Flush out the buffer cache 1050 */ 1051 /* ARGSUSED */ 1052 static int 1053 nfs_sync(struct mount *mp, int waitfor, struct thread *td) 1054 { 1055 struct vnode *vp; 1056 int error, allerror = 0; 1057 1058 /* 1059 * Force stale buffer cache information to be flushed. 1060 */ 1061 loop: 1062 for (vp = TAILQ_FIRST(&mp->mnt_nvnodelist); 1063 vp != NULL; 1064 vp = TAILQ_NEXT(vp, v_nmntvnodes)) { 1065 /* 1066 * If the vnode that we are about to sync is no longer 1067 * associated with this mount point, start over. 1068 */ 1069 if (vp->v_mount != mp) 1070 goto loop; 1071 if (VOP_ISLOCKED(vp, NULL) || TAILQ_EMPTY(&vp->v_dirtyblkhd) || 1072 waitfor == MNT_LAZY) 1073 continue; 1074 if (vget(vp, LK_EXCLUSIVE, td)) 1075 goto loop; 1076 error = VOP_FSYNC(vp, waitfor, td); 1077 if (error) 1078 allerror = error; 1079 vput(vp); 1080 } 1081 return (allerror); 1082 } 1083 1084