1 /* $NetBSD: nfs_vfsops.c,v 1.131 2003/06/29 22:32:19 fvdl Exp $ */ 2 3 /* 4 * Copyright (c) 1989, 1993, 1995 5 * The Regents of the University of California. All rights reserved. 6 * 7 * This code is derived from software contributed to Berkeley by 8 * Rick Macklem at The University of Guelph. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 3. All advertising materials mentioning features or use of this software 19 * must display the following acknowledgement: 20 * This product includes software developed by the University of 21 * California, Berkeley and its contributors. 22 * 4. Neither the name of the University nor the names of its contributors 23 * may be used to endorse or promote products derived from this software 24 * without specific prior written permission. 25 * 26 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 29 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 36 * SUCH DAMAGE. 37 * 38 * @(#)nfs_vfsops.c 8.12 (Berkeley) 5/20/95 39 */ 40 41 #include <sys/cdefs.h> 42 __KERNEL_RCSID(0, "$NetBSD: nfs_vfsops.c,v 1.131 2003/06/29 22:32:19 fvdl Exp $"); 43 44 #if defined(_KERNEL_OPT) 45 #include "opt_compat_netbsd.h" 46 #include "opt_nfs.h" 47 #endif 48 49 #include <sys/param.h> 50 #include <sys/ioctl.h> 51 #include <sys/signal.h> 52 #include <sys/proc.h> 53 #include <sys/namei.h> 54 #include <sys/device.h> 55 #include <sys/vnode.h> 56 #include <sys/kernel.h> 57 #include <sys/mount.h> 58 #include <sys/buf.h> 59 #include <sys/mbuf.h> 60 #include <sys/socket.h> 61 #include <sys/socketvar.h> 62 #include <sys/sysctl.h> 63 #include <sys/systm.h> 64 65 #include <net/if.h> 66 #include <net/route.h> 67 #include <netinet/in.h> 68 69 #include <nfs/rpcv2.h> 70 #include <nfs/nfsproto.h> 71 #include <nfs/nfsnode.h> 72 #include <nfs/nfs.h> 73 #include <nfs/nfsmount.h> 74 #include <nfs/xdr_subs.h> 75 #include <nfs/nfsm_subs.h> 76 #include <nfs/nfsdiskless.h> 77 #include <nfs/nqnfs.h> 78 #include <nfs/nfs_var.h> 79 80 extern struct nfsstats nfsstats; 81 extern int nfs_ticks; 82 83 MALLOC_DEFINE(M_NFSMNT, "NFS mount", "NFS mount structure"); 84 85 int nfs_sysctl __P((int *, u_int, void *, size_t *, void *, size_t, 86 struct proc *)); 87 88 /* 89 * nfs vfs operations. 90 */ 91 92 extern const struct vnodeopv_desc nfsv2_vnodeop_opv_desc; 93 extern const struct vnodeopv_desc spec_nfsv2nodeop_opv_desc; 94 extern const struct vnodeopv_desc fifo_nfsv2nodeop_opv_desc; 95 96 const struct vnodeopv_desc * const nfs_vnodeopv_descs[] = { 97 &nfsv2_vnodeop_opv_desc, 98 &spec_nfsv2nodeop_opv_desc, 99 &fifo_nfsv2nodeop_opv_desc, 100 NULL, 101 }; 102 103 struct vfsops nfs_vfsops = { 104 MOUNT_NFS, 105 nfs_mount, 106 nfs_start, 107 nfs_unmount, 108 nfs_root, 109 nfs_quotactl, 110 nfs_statfs, 111 nfs_sync, 112 nfs_vget, 113 nfs_fhtovp, 114 nfs_vptofh, 115 nfs_vfs_init, 116 nfs_vfs_reinit, 117 nfs_vfs_done, 118 nfs_sysctl, 119 nfs_mountroot, 120 nfs_checkexp, 121 nfs_vnodeopv_descs, 122 }; 123 124 extern u_int32_t nfs_procids[NFS_NPROCS]; 125 extern u_int32_t nfs_prog, nfs_vers; 126 127 static int nfs_mount_diskless __P((struct nfs_dlmount *, const char *, 128 struct mount **, struct vnode **, struct proc *)); 129 130 /* 131 * nfs statfs call 132 */ 133 int 134 nfs_statfs(mp, sbp, p) 135 struct mount *mp; 136 struct statfs *sbp; 137 struct proc *p; 138 { 139 struct vnode *vp; 140 struct nfs_statfs *sfp; 141 caddr_t cp; 142 u_int32_t *tl; 143 int32_t t1, t2; 144 caddr_t bpos, dpos, cp2; 145 struct nfsmount *nmp = VFSTONFS(mp); 146 int error = 0, retattr; 147 #ifdef NFS_V2_ONLY 148 const int v3 = 0; 149 #else 150 int v3 = (nmp->nm_flag & NFSMNT_NFSV3); 151 #endif 152 struct mbuf *mreq, *mrep = NULL, *md, *mb; 153 struct ucred *cred; 154 u_quad_t tquad; 155 struct nfsnode *np; 156 157 #ifndef nolint 158 sfp = (struct nfs_statfs *)0; 159 #endif 160 vp = nmp->nm_vnode; 161 np = VTONFS(vp); 162 cred = crget(); 163 cred->cr_ngroups = 0; 164 #ifndef NFS_V2_ONLY 165 if (v3 && (nmp->nm_iflag & NFSMNT_GOTFSINFO) == 0) 166 (void)nfs_fsinfo(nmp, vp, cred, p); 167 #endif 168 nfsstats.rpccnt[NFSPROC_FSSTAT]++; 169 nfsm_reqhead(np, NFSPROC_FSSTAT, NFSX_FH(v3)); 170 nfsm_fhtom(np, v3); 171 nfsm_request(np, NFSPROC_FSSTAT, p, cred); 172 if (v3) 173 nfsm_postop_attr(vp, retattr, 0); 174 if (error) { 175 if (mrep != NULL) 176 m_free(mrep); 177 goto nfsmout; 178 } 179 nfsm_dissect(sfp, struct nfs_statfs *, NFSX_STATFS(v3)); 180 #ifdef COMPAT_09 181 sbp->f_type = 2; 182 #else 183 sbp->f_type = 0; 184 #endif 185 sbp->f_flags = nmp->nm_flag; 186 sbp->f_iosize = min(nmp->nm_rsize, nmp->nm_wsize); 187 if (v3) { 188 sbp->f_bsize = NFS_FABLKSIZE; 189 tquad = fxdr_hyper(&sfp->sf_tbytes); 190 sbp->f_blocks = (long)((quad_t)tquad / (quad_t)NFS_FABLKSIZE); 191 tquad = fxdr_hyper(&sfp->sf_fbytes); 192 sbp->f_bfree = (long)((quad_t)tquad / (quad_t)NFS_FABLKSIZE); 193 tquad = fxdr_hyper(&sfp->sf_abytes); 194 sbp->f_bavail = (long)((quad_t)tquad / (quad_t)NFS_FABLKSIZE); 195 tquad = fxdr_hyper(&sfp->sf_tfiles); 196 sbp->f_files = (long)tquad; 197 tquad = fxdr_hyper(&sfp->sf_ffiles); 198 sbp->f_ffree = (long)tquad; 199 } else { 200 sbp->f_bsize = fxdr_unsigned(int32_t, sfp->sf_bsize); 201 sbp->f_blocks = fxdr_unsigned(int32_t, sfp->sf_blocks); 202 sbp->f_bfree = fxdr_unsigned(int32_t, sfp->sf_bfree); 203 sbp->f_bavail = fxdr_unsigned(int32_t, sfp->sf_bavail); 204 sbp->f_files = 0; 205 sbp->f_ffree = 0; 206 } 207 copy_statfs_info(sbp, mp); 208 nfsm_reqdone; 209 crfree(cred); 210 return (error); 211 } 212 213 #ifndef NFS_V2_ONLY 214 /* 215 * nfs version 3 fsinfo rpc call 216 */ 217 int 218 nfs_fsinfo(nmp, vp, cred, p) 219 struct nfsmount *nmp; 220 struct vnode *vp; 221 struct ucred *cred; 222 struct proc *p; 223 { 224 struct nfsv3_fsinfo *fsp; 225 caddr_t cp; 226 int32_t t1, t2; 227 u_int32_t *tl, pref, max; 228 caddr_t bpos, dpos, cp2; 229 int error = 0, retattr; 230 struct mbuf *mreq, *mrep, *md, *mb; 231 u_int64_t maxfsize; 232 struct nfsnode *np = VTONFS(vp); 233 234 nfsstats.rpccnt[NFSPROC_FSINFO]++; 235 nfsm_reqhead(np, NFSPROC_FSINFO, NFSX_FH(1)); 236 nfsm_fhtom(np, 1); 237 nfsm_request(np, NFSPROC_FSINFO, p, cred); 238 nfsm_postop_attr(vp, retattr, 0); 239 if (!error) { 240 nfsm_dissect(fsp, struct nfsv3_fsinfo *, NFSX_V3FSINFO); 241 pref = fxdr_unsigned(u_int32_t, fsp->fs_wtpref); 242 if ((nmp->nm_flag & NFSMNT_WSIZE) == 0 && 243 pref < nmp->nm_wsize && pref >= NFS_FABLKSIZE) 244 nmp->nm_wsize = (pref + NFS_FABLKSIZE - 1) & 245 ~(NFS_FABLKSIZE - 1); 246 max = fxdr_unsigned(u_int32_t, fsp->fs_wtmax); 247 if (max < nmp->nm_wsize && max > 0) { 248 nmp->nm_wsize = max & ~(NFS_FABLKSIZE - 1); 249 if (nmp->nm_wsize == 0) 250 nmp->nm_wsize = max; 251 } 252 pref = fxdr_unsigned(u_int32_t, fsp->fs_rtpref); 253 if ((nmp->nm_flag & NFSMNT_RSIZE) == 0 && 254 pref < nmp->nm_rsize && pref >= NFS_FABLKSIZE) 255 nmp->nm_rsize = (pref + NFS_FABLKSIZE - 1) & 256 ~(NFS_FABLKSIZE - 1); 257 max = fxdr_unsigned(u_int32_t, fsp->fs_rtmax); 258 if (max < nmp->nm_rsize && max > 0) { 259 nmp->nm_rsize = max & ~(NFS_FABLKSIZE - 1); 260 if (nmp->nm_rsize == 0) 261 nmp->nm_rsize = max; 262 } 263 pref = fxdr_unsigned(u_int32_t, fsp->fs_dtpref); 264 if (pref < nmp->nm_readdirsize && pref >= NFS_DIRFRAGSIZ) 265 nmp->nm_readdirsize = (pref + NFS_DIRFRAGSIZ - 1) & 266 ~(NFS_DIRFRAGSIZ - 1); 267 if (max < nmp->nm_readdirsize && max > 0) { 268 nmp->nm_readdirsize = max & ~(NFS_DIRFRAGSIZ - 1); 269 if (nmp->nm_readdirsize == 0) 270 nmp->nm_readdirsize = max; 271 } 272 /* XXX */ 273 nmp->nm_maxfilesize = (u_int64_t)0x80000000 * DEV_BSIZE - 1; 274 maxfsize = fxdr_hyper(&fsp->fs_maxfilesize); 275 if (maxfsize > 0 && maxfsize < nmp->nm_maxfilesize) 276 nmp->nm_maxfilesize = maxfsize; 277 nmp->nm_iflag |= NFSMNT_GOTFSINFO; 278 } 279 nfsm_reqdone; 280 return (error); 281 } 282 #endif 283 284 /* 285 * Mount a remote root fs via. NFS. It goes like this: 286 * - Call nfs_boot_init() to fill in the nfs_diskless struct 287 * - build the rootfs mount point and call mountnfs() to do the rest. 288 */ 289 int 290 nfs_mountroot() 291 { 292 struct nfs_diskless *nd; 293 struct vattr attr; 294 struct mount *mp; 295 struct vnode *vp; 296 struct proc *procp; 297 long n; 298 int error; 299 300 procp = curproc; /* XXX */ 301 302 if (root_device->dv_class != DV_IFNET) 303 return (ENODEV); 304 305 /* 306 * XXX time must be non-zero when we init the interface or else 307 * the arp code will wedge. [Fixed now in if_ether.c] 308 * However, the NFS attribute cache gives false "hits" when 309 * time.tv_sec < NFS_ATTRTIMEO(np) so keep this in for now. 310 */ 311 if (time.tv_sec < NFS_MAXATTRTIMO) 312 time.tv_sec = NFS_MAXATTRTIMO; 313 314 /* 315 * Call nfs_boot_init() to fill in the nfs_diskless struct. 316 * Side effect: Finds and configures a network interface. 317 */ 318 nd = malloc(sizeof(*nd), M_NFSMNT, M_WAITOK); 319 memset((caddr_t)nd, 0, sizeof(*nd)); 320 error = nfs_boot_init(nd, procp); 321 if (error) { 322 free(nd, M_NFSMNT); 323 return (error); 324 } 325 326 /* 327 * Create the root mount point. 328 */ 329 error = nfs_mount_diskless(&nd->nd_root, "/", &mp, &vp, procp); 330 if (error) 331 goto out; 332 printf("root on %s\n", nd->nd_root.ndm_host); 333 334 /* 335 * Link it into the mount list. 336 */ 337 simple_lock(&mountlist_slock); 338 CIRCLEQ_INSERT_TAIL(&mountlist, mp, mnt_list); 339 simple_unlock(&mountlist_slock); 340 rootvp = vp; 341 mp->mnt_vnodecovered = NULLVP; 342 vfs_unbusy(mp); 343 344 /* Get root attributes (for the time). */ 345 error = VOP_GETATTR(vp, &attr, procp->p_ucred, procp); 346 if (error) 347 panic("nfs_mountroot: getattr for root"); 348 n = attr.va_atime.tv_sec; 349 #ifdef DEBUG 350 printf("root time: 0x%lx\n", n); 351 #endif 352 inittodr(n); 353 354 out: 355 if (error) 356 nfs_boot_cleanup(nd, procp); 357 free(nd, M_NFSMNT); 358 return (error); 359 } 360 361 /* 362 * Internal version of mount system call for diskless setup. 363 * Separate function because we used to call it twice. 364 * (once for root and once for swap) 365 */ 366 static int 367 nfs_mount_diskless(ndmntp, mntname, mpp, vpp, p) 368 struct nfs_dlmount *ndmntp; 369 const char *mntname; /* mount point name */ 370 struct mount **mpp; 371 struct vnode **vpp; 372 struct proc *p; 373 { 374 struct mount *mp; 375 struct mbuf *m; 376 int error; 377 378 vfs_rootmountalloc(MOUNT_NFS, (char *)mntname, &mp); 379 380 mp->mnt_op = &nfs_vfsops; 381 382 /* 383 * Historical practice expects NFS root file systems to 384 * be initially mounted r/w. 385 */ 386 mp->mnt_flag &= ~MNT_RDONLY; 387 388 /* Get mbuf for server sockaddr. */ 389 m = m_get(M_WAIT, MT_SONAME); 390 if (m == NULL) 391 panic("nfs_mountroot: mget soname for %s", mntname); 392 MCLAIM(m, &nfs_mowner); 393 memcpy(mtod(m, caddr_t), (caddr_t)ndmntp->ndm_args.addr, 394 (m->m_len = ndmntp->ndm_args.addr->sa_len)); 395 396 error = mountnfs(&ndmntp->ndm_args, mp, m, mntname, 397 ndmntp->ndm_args.hostname, vpp, p); 398 if (error) { 399 mp->mnt_op->vfs_refcount--; 400 vfs_unbusy(mp); 401 printf("nfs_mountroot: mount %s failed: %d\n", 402 mntname, error); 403 free(mp, M_MOUNT); 404 } else 405 *mpp = mp; 406 407 return (error); 408 } 409 410 void 411 nfs_decode_args(nmp, argp) 412 struct nfsmount *nmp; 413 struct nfs_args *argp; 414 { 415 int s; 416 int adjsock; 417 int maxio; 418 419 s = splsoftnet(); 420 421 /* 422 * Silently clear NFSMNT_NOCONN if it's a TCP mount, it makes 423 * no sense in that context. 424 */ 425 if (argp->sotype == SOCK_STREAM) 426 argp->flags &= ~NFSMNT_NOCONN; 427 428 /* 429 * Cookie translation is not needed for v2, silently ignore it. 430 */ 431 if ((argp->flags & (NFSMNT_XLATECOOKIE|NFSMNT_NFSV3)) == 432 NFSMNT_XLATECOOKIE) 433 argp->flags &= ~NFSMNT_XLATECOOKIE; 434 435 /* Re-bind if rsrvd port requested and wasn't on one */ 436 adjsock = !(nmp->nm_flag & NFSMNT_RESVPORT) 437 && (argp->flags & NFSMNT_RESVPORT); 438 /* Also re-bind if we're switching to/from a connected UDP socket */ 439 adjsock |= ((nmp->nm_flag & NFSMNT_NOCONN) != 440 (argp->flags & NFSMNT_NOCONN)); 441 442 /* Update flags. */ 443 nmp->nm_flag = argp->flags; 444 splx(s); 445 446 if ((argp->flags & NFSMNT_TIMEO) && argp->timeo > 0) { 447 nmp->nm_timeo = (argp->timeo * NFS_HZ + 5) / 10; 448 if (nmp->nm_timeo < NFS_MINTIMEO) 449 nmp->nm_timeo = NFS_MINTIMEO; 450 else if (nmp->nm_timeo > NFS_MAXTIMEO) 451 nmp->nm_timeo = NFS_MAXTIMEO; 452 } 453 454 if ((argp->flags & NFSMNT_RETRANS) && argp->retrans > 1) { 455 nmp->nm_retry = argp->retrans; 456 if (nmp->nm_retry > NFS_MAXREXMIT) 457 nmp->nm_retry = NFS_MAXREXMIT; 458 } 459 460 #ifndef NFS_V2_ONLY 461 if (argp->flags & NFSMNT_NFSV3) { 462 if (argp->sotype == SOCK_DGRAM) 463 maxio = NFS_MAXDGRAMDATA; 464 else 465 maxio = NFS_MAXDATA; 466 } else 467 #endif 468 maxio = NFS_V2MAXDATA; 469 470 if ((argp->flags & NFSMNT_WSIZE) && argp->wsize > 0) { 471 int osize = nmp->nm_wsize; 472 nmp->nm_wsize = argp->wsize; 473 /* Round down to multiple of blocksize */ 474 nmp->nm_wsize &= ~(NFS_FABLKSIZE - 1); 475 if (nmp->nm_wsize <= 0) 476 nmp->nm_wsize = NFS_FABLKSIZE; 477 adjsock |= (nmp->nm_wsize != osize); 478 } 479 if (nmp->nm_wsize > maxio) 480 nmp->nm_wsize = maxio; 481 if (nmp->nm_wsize > MAXBSIZE) 482 nmp->nm_wsize = MAXBSIZE; 483 484 if ((argp->flags & NFSMNT_RSIZE) && argp->rsize > 0) { 485 int osize = nmp->nm_rsize; 486 nmp->nm_rsize = argp->rsize; 487 /* Round down to multiple of blocksize */ 488 nmp->nm_rsize &= ~(NFS_FABLKSIZE - 1); 489 if (nmp->nm_rsize <= 0) 490 nmp->nm_rsize = NFS_FABLKSIZE; 491 adjsock |= (nmp->nm_rsize != osize); 492 } 493 if (nmp->nm_rsize > maxio) 494 nmp->nm_rsize = maxio; 495 if (nmp->nm_rsize > MAXBSIZE) 496 nmp->nm_rsize = MAXBSIZE; 497 498 if ((argp->flags & NFSMNT_READDIRSIZE) && argp->readdirsize > 0) { 499 nmp->nm_readdirsize = argp->readdirsize; 500 /* Round down to multiple of minimum blocksize */ 501 nmp->nm_readdirsize &= ~(NFS_DIRFRAGSIZ - 1); 502 if (nmp->nm_readdirsize < NFS_DIRFRAGSIZ) 503 nmp->nm_readdirsize = NFS_DIRFRAGSIZ; 504 /* Bigger than buffer size makes no sense */ 505 if (nmp->nm_readdirsize > NFS_DIRBLKSIZ) 506 nmp->nm_readdirsize = NFS_DIRBLKSIZ; 507 } else if (argp->flags & NFSMNT_RSIZE) 508 nmp->nm_readdirsize = nmp->nm_rsize; 509 510 if (nmp->nm_readdirsize > maxio) 511 nmp->nm_readdirsize = maxio; 512 513 if ((argp->flags & NFSMNT_MAXGRPS) && argp->maxgrouplist >= 0 && 514 argp->maxgrouplist <= NFS_MAXGRPS) 515 nmp->nm_numgrps = argp->maxgrouplist; 516 if ((argp->flags & NFSMNT_READAHEAD) && argp->readahead >= 0 && 517 argp->readahead <= NFS_MAXRAHEAD) 518 nmp->nm_readahead = argp->readahead; 519 if ((argp->flags & NFSMNT_LEASETERM) && argp->leaseterm >= 2 && 520 argp->leaseterm <= NQ_MAXLEASE) 521 nmp->nm_leaseterm = argp->leaseterm; 522 if ((argp->flags & NFSMNT_DEADTHRESH) && argp->deadthresh >= 1 && 523 argp->deadthresh <= NQ_NEVERDEAD) 524 nmp->nm_deadthresh = argp->deadthresh; 525 526 adjsock |= ((nmp->nm_sotype != argp->sotype) || 527 (nmp->nm_soproto != argp->proto)); 528 nmp->nm_sotype = argp->sotype; 529 nmp->nm_soproto = argp->proto; 530 531 if (nmp->nm_so && adjsock) { 532 nfs_safedisconnect(nmp); 533 if (nmp->nm_sotype == SOCK_DGRAM) 534 while (nfs_connect(nmp, (struct nfsreq *)0)) { 535 printf("nfs_args: retrying connect\n"); 536 (void) tsleep((caddr_t)&lbolt, 537 PSOCK, "nfscn3", 0); 538 } 539 } 540 } 541 542 /* 543 * VFS Operations. 544 * 545 * mount system call 546 * It seems a bit dumb to copyinstr() the host and path here and then 547 * memcpy() them in mountnfs(), but I wanted to detect errors before 548 * doing the sockargs() call because sockargs() allocates an mbuf and 549 * an error after that means that I have to release the mbuf. 550 */ 551 /* ARGSUSED */ 552 int 553 nfs_mount(mp, path, data, ndp, p) 554 struct mount *mp; 555 const char *path; 556 void *data; 557 struct nameidata *ndp; 558 struct proc *p; 559 { 560 int error; 561 struct nfs_args args; 562 struct mbuf *nam; 563 struct nfsmount *nmp = VFSTONFS(mp); 564 struct sockaddr *sa; 565 struct vnode *vp; 566 char *pth, *hst; 567 size_t len; 568 u_char *nfh; 569 570 error = copyin(data, (caddr_t)&args, sizeof (struct nfs_args)); 571 if (error) 572 return (error); 573 574 if (mp->mnt_flag & MNT_GETARGS) { 575 576 if (nmp == NULL) 577 return (EIO); 578 if (args.addr != NULL) { 579 sa = mtod(nmp->nm_nam, struct sockaddr *); 580 error = copyout(sa, args.addr, sa->sa_len); 581 if (error) 582 return (error); 583 args.addrlen = sa->sa_len; 584 } else 585 args.addrlen = 0; 586 587 args.version = NFS_ARGSVERSION; 588 args.sotype = nmp->nm_sotype; 589 args.proto = nmp->nm_soproto; 590 args.fh = NULL; 591 args.fhsize = 0; 592 args.flags = nmp->nm_flag; 593 args.wsize = nmp->nm_wsize; 594 args.rsize = nmp->nm_rsize; 595 args.readdirsize = nmp->nm_readdirsize; 596 args.timeo = nmp->nm_timeo; 597 args.retrans = nmp->nm_retry; 598 args.maxgrouplist = nmp->nm_numgrps; 599 args.readahead = nmp->nm_readahead; 600 args.leaseterm = nmp->nm_leaseterm; 601 args.deadthresh = nmp->nm_deadthresh; 602 args.hostname = NULL; 603 return (copyout(&args, data, sizeof(args))); 604 } 605 606 if (args.version != NFS_ARGSVERSION) 607 return (EPROGMISMATCH); 608 #ifdef NFS_V2_ONLY 609 if (args.flags & NFSMNT_NQNFS) 610 return (EPROGUNAVAIL); 611 if (args.flags & NFSMNT_NFSV3) 612 return (EPROGMISMATCH); 613 #endif 614 if (mp->mnt_flag & MNT_UPDATE) { 615 if (nmp == NULL) 616 return (EIO); 617 /* 618 * When doing an update, we can't change from or to 619 * v3 and/or nqnfs, or change cookie translation 620 */ 621 args.flags = (args.flags & 622 ~(NFSMNT_NFSV3|NFSMNT_NQNFS|NFSMNT_XLATECOOKIE)) | 623 (nmp->nm_flag & 624 (NFSMNT_NFSV3|NFSMNT_NQNFS|NFSMNT_XLATECOOKIE)); 625 nfs_decode_args(nmp, &args); 626 return (0); 627 } 628 if (args.fhsize < 0 || args.fhsize > NFSX_V3FHMAX) 629 return (EINVAL); 630 MALLOC(nfh, u_char *, NFSX_V3FHMAX, M_TEMP, M_WAITOK); 631 error = copyin((caddr_t)args.fh, (caddr_t)nfh, args.fhsize); 632 if (error) 633 return (error); 634 MALLOC(pth, char *, MNAMELEN, M_TEMP, M_WAITOK); 635 error = copyinstr(path, pth, MNAMELEN - 1, &len); 636 if (error) 637 goto free_nfh; 638 memset(&pth[len], 0, MNAMELEN - len); 639 MALLOC(hst, char *, MNAMELEN, M_TEMP, M_WAITOK); 640 error = copyinstr(args.hostname, hst, MNAMELEN - 1, &len); 641 if (error) 642 goto free_pth; 643 memset(&hst[len], 0, MNAMELEN - len); 644 /* sockargs() call must be after above copyin() calls */ 645 error = sockargs(&nam, (caddr_t)args.addr, args.addrlen, MT_SONAME); 646 if (error) 647 goto free_hst; 648 MCLAIM(nam, &nfs_mowner); 649 args.fh = nfh; 650 error = mountnfs(&args, mp, nam, pth, hst, &vp, p); 651 652 free_hst: 653 FREE(hst, M_TEMP); 654 free_pth: 655 FREE(pth, M_TEMP); 656 free_nfh: 657 FREE(nfh, M_TEMP); 658 659 return (error); 660 } 661 662 /* 663 * Common code for mount and mountroot 664 */ 665 int 666 mountnfs(argp, mp, nam, pth, hst, vpp, p) 667 struct nfs_args *argp; 668 struct mount *mp; 669 struct mbuf *nam; 670 const char *pth, *hst; 671 struct vnode **vpp; 672 struct proc *p; 673 { 674 struct nfsmount *nmp; 675 struct nfsnode *np; 676 int error; 677 struct vattr *attrs; 678 struct ucred *cr; 679 680 /* 681 * If the number of nfs iothreads to use has never 682 * been set, create a reasonable number of them. 683 */ 684 685 if (nfs_niothreads < 0) { 686 nfs_niothreads = NFS_DEFAULT_NIOTHREADS; 687 nfs_getset_niothreads(TRUE); 688 } 689 690 if (mp->mnt_flag & MNT_UPDATE) { 691 nmp = VFSTONFS(mp); 692 /* update paths, file handles, etc, here XXX */ 693 m_freem(nam); 694 return (0); 695 } else { 696 MALLOC(nmp, struct nfsmount *, sizeof (struct nfsmount), 697 M_NFSMNT, M_WAITOK); 698 memset((caddr_t)nmp, 0, sizeof (struct nfsmount)); 699 mp->mnt_data = nmp; 700 TAILQ_INIT(&nmp->nm_uidlruhead); 701 TAILQ_INIT(&nmp->nm_bufq); 702 lockinit(&nmp->nm_writeverflock, PRIBIO, "nfswverf", 0, 0); 703 simple_lock_init(&nmp->nm_slock); 704 } 705 vfs_getnewfsid(mp); 706 nmp->nm_mountp = mp; 707 708 #ifndef NFS_V2_ONLY 709 if (argp->flags & NFSMNT_NQNFS) 710 /* 711 * We have to set mnt_maxsymlink to a non-zero value so 712 * that COMPAT_43 routines will know that we are setting 713 * the d_type field in directories (and can zero it for 714 * unsuspecting binaries). 715 */ 716 mp->mnt_maxsymlinklen = 1; 717 #endif 718 719 #ifndef NFS_V2_ONLY 720 if ((argp->flags & NFSMNT_NFSV3) == 0) 721 #endif 722 /* 723 * V2 can only handle 32 bit filesizes. For v3, nfs_fsinfo 724 * will fill this in. 725 */ 726 nmp->nm_maxfilesize = 0xffffffffLL; 727 728 nmp->nm_timeo = NFS_TIMEO; 729 nmp->nm_retry = NFS_RETRANS; 730 nmp->nm_wsize = NFS_WSIZE; 731 nmp->nm_rsize = NFS_RSIZE; 732 nmp->nm_readdirsize = NFS_READDIRSIZE; 733 nmp->nm_numgrps = NFS_MAXGRPS; 734 nmp->nm_readahead = NFS_DEFRAHEAD; 735 nmp->nm_leaseterm = NQ_DEFLEASE; 736 nmp->nm_deadthresh = NQ_DEADTHRESH; 737 CIRCLEQ_INIT(&nmp->nm_timerhead); 738 nmp->nm_inprog = NULLVP; 739 #ifdef COMPAT_09 740 mp->mnt_stat.f_type = 2; 741 #else 742 mp->mnt_stat.f_type = 0; 743 #endif 744 error = set_statfs_info(pth, UIO_SYSSPACE, hst, UIO_SYSSPACE, mp, p); 745 if (error) 746 goto bad; 747 nmp->nm_nam = nam; 748 749 /* Set up the sockets and per-host congestion */ 750 nmp->nm_sotype = argp->sotype; 751 nmp->nm_soproto = argp->proto; 752 753 nfs_decode_args(nmp, argp); 754 755 mp->mnt_fs_bshift = ffs(MIN(nmp->nm_rsize, nmp->nm_wsize)) - 1; 756 mp->mnt_dev_bshift = DEV_BSHIFT; 757 758 /* 759 * For Connection based sockets (TCP,...) defer the connect until 760 * the first request, in case the server is not responding. 761 */ 762 if (nmp->nm_sotype == SOCK_DGRAM && 763 (error = nfs_connect(nmp, (struct nfsreq *)0))) 764 goto bad; 765 766 /* 767 * This is silly, but it has to be set so that vinifod() works. 768 * We do not want to do an nfs_statfs() here since we can get 769 * stuck on a dead server and we are holding a lock on the mount 770 * point. 771 */ 772 mp->mnt_stat.f_iosize = NFS_MAXDGRAMDATA; 773 error = nfs_nget(mp, (nfsfh_t *)argp->fh, argp->fhsize, &np); 774 if (error) 775 goto bad; 776 *vpp = NFSTOV(np); 777 MALLOC(attrs, struct vattr *, sizeof(struct vattr), M_TEMP, M_WAITOK); 778 VOP_GETATTR(*vpp, attrs, p->p_ucred, p); 779 if ((nmp->nm_flag & NFSMNT_NFSV3) && ((*vpp)->v_type == VDIR)) { 780 cr = crget(); 781 cr->cr_uid = attrs->va_uid; 782 cr->cr_gid = attrs->va_gid; 783 cr->cr_ngroups = 0; 784 nfs_cookieheuristic(*vpp, &nmp->nm_iflag, p, cr); 785 crfree(cr); 786 } 787 FREE(attrs, M_TEMP); 788 789 /* 790 * A reference count is needed on the nfsnode representing the 791 * remote root. If this object is not persistent, then backward 792 * traversals of the mount point (i.e. "..") will not work if 793 * the nfsnode gets flushed out of the cache. Ufs does not have 794 * this problem, because one can identify root inodes by their 795 * number == ROOTINO (2). So, just unlock, but no rele. 796 */ 797 798 nmp->nm_vnode = *vpp; 799 VOP_UNLOCK(*vpp, 0); 800 801 return (0); 802 bad: 803 nfs_disconnect(nmp); 804 free((caddr_t)nmp, M_NFSMNT); 805 m_freem(nam); 806 return (error); 807 } 808 809 /* 810 * unmount system call 811 */ 812 int 813 nfs_unmount(mp, mntflags, p) 814 struct mount *mp; 815 int mntflags; 816 struct proc *p; 817 { 818 struct nfsmount *nmp; 819 struct vnode *vp; 820 int error, flags = 0; 821 822 if (mntflags & MNT_FORCE) 823 flags |= FORCECLOSE; 824 nmp = VFSTONFS(mp); 825 /* 826 * Goes something like this.. 827 * - Check for activity on the root vnode (other than ourselves). 828 * - Call vflush() to clear out vnodes for this file system, 829 * except for the root vnode. 830 * - Decrement reference on the vnode representing remote root. 831 * - Close the socket 832 * - Free up the data structures 833 */ 834 /* 835 * We need to decrement the ref. count on the nfsnode representing 836 * the remote root. See comment in mountnfs(). The VFS unmount() 837 * has done vput on this vnode, otherwise we would get deadlock! 838 */ 839 vp = nmp->nm_vnode; 840 error = vget(vp, LK_EXCLUSIVE | LK_RETRY); 841 if (error != 0) 842 return error; 843 844 if ((mntflags & MNT_FORCE) == 0 && vp->v_usecount > 2) { 845 vput(vp); 846 return (EBUSY); 847 } 848 849 /* 850 * Must handshake with nqnfs_clientd() if it is active. 851 */ 852 nmp->nm_iflag |= NFSMNT_DISMINPROG; 853 while (nmp->nm_inprog != NULLVP) 854 (void) tsleep((caddr_t)&lbolt, PSOCK, "nfsdism", 0); 855 error = vflush(mp, vp, flags); 856 if (error) { 857 vput(vp); 858 nmp->nm_iflag &= ~NFSMNT_DISMINPROG; 859 return (error); 860 } 861 862 /* 863 * We are now committed to the unmount; mark the mount structure 864 * as doomed so that any sleepers kicked awake by nfs_disconnect 865 * will go away cleanly. 866 */ 867 nmp->nm_iflag |= NFSMNT_DISMNT; 868 869 /* 870 * There are two reference counts to get rid of here 871 * (see comment in mountnfs()). 872 */ 873 vrele(vp); 874 vput(vp); 875 vgone(vp); 876 nfs_disconnect(nmp); 877 m_freem(nmp->nm_nam); 878 879 /* 880 * For NQNFS, let the server daemon free the nfsmount structure. 881 */ 882 if ((nmp->nm_flag & (NFSMNT_NQNFS | NFSMNT_KERB)) == 0) 883 free((caddr_t)nmp, M_NFSMNT); 884 return (0); 885 } 886 887 /* 888 * Return root of a filesystem 889 */ 890 int 891 nfs_root(mp, vpp) 892 struct mount *mp; 893 struct vnode **vpp; 894 { 895 struct vnode *vp; 896 struct nfsmount *nmp; 897 int error; 898 899 nmp = VFSTONFS(mp); 900 vp = nmp->nm_vnode; 901 error = vget(vp, LK_EXCLUSIVE | LK_RETRY); 902 if (error != 0) 903 return error; 904 if (vp->v_type == VNON) 905 vp->v_type = VDIR; 906 vp->v_flag = VROOT; 907 *vpp = vp; 908 return (0); 909 } 910 911 extern int syncprt; 912 913 /* 914 * Flush out the buffer cache 915 */ 916 /* ARGSUSED */ 917 int 918 nfs_sync(mp, waitfor, cred, p) 919 struct mount *mp; 920 int waitfor; 921 struct ucred *cred; 922 struct proc *p; 923 { 924 struct vnode *vp; 925 int error, allerror = 0; 926 927 /* 928 * Force stale buffer cache information to be flushed. 929 */ 930 loop: 931 LIST_FOREACH(vp, &mp->mnt_vnodelist, v_mntvnodes) { 932 /* 933 * If the vnode that we are about to sync is no longer 934 * associated with this mount point, start over. 935 */ 936 if (vp->v_mount != mp) 937 goto loop; 938 if (waitfor == MNT_LAZY || VOP_ISLOCKED(vp) || 939 (LIST_EMPTY(&vp->v_dirtyblkhd) && 940 vp->v_uobj.uo_npages == 0)) 941 continue; 942 if (vget(vp, LK_EXCLUSIVE)) 943 goto loop; 944 error = VOP_FSYNC(vp, cred, 945 waitfor == MNT_WAIT ? FSYNC_WAIT : 0, 0, 0, p); 946 if (error) 947 allerror = error; 948 vput(vp); 949 } 950 return (allerror); 951 } 952 953 /* 954 * NFS flat namespace lookup. 955 * Currently unsupported. 956 */ 957 /* ARGSUSED */ 958 int 959 nfs_vget(mp, ino, vpp) 960 struct mount *mp; 961 ino_t ino; 962 struct vnode **vpp; 963 { 964 965 return (EOPNOTSUPP); 966 } 967 968 /* 969 * Do that sysctl thang... 970 */ 971 int 972 nfs_sysctl(name, namelen, oldp, oldlenp, newp, newlen, p) 973 int *name; 974 u_int namelen; 975 void *oldp; 976 size_t *oldlenp; 977 void *newp; 978 size_t newlen; 979 struct proc *p; 980 { 981 int rv; 982 983 /* 984 * All names at this level are terminal. 985 */ 986 if(namelen > 1) 987 return ENOTDIR; /* overloaded */ 988 989 switch(name[0]) { 990 case NFS_NFSSTATS: 991 if(!oldp) { 992 *oldlenp = sizeof nfsstats; 993 return 0; 994 } 995 996 if(*oldlenp < sizeof nfsstats) { 997 *oldlenp = sizeof nfsstats; 998 return ENOMEM; 999 } 1000 1001 rv = copyout(&nfsstats, oldp, sizeof nfsstats); 1002 if(rv) return rv; 1003 1004 if(newp && newlen != sizeof nfsstats) 1005 return EINVAL; 1006 1007 if(newp) { 1008 return copyin(newp, &nfsstats, sizeof nfsstats); 1009 } 1010 return 0; 1011 1012 case NFS_IOTHREADS: 1013 nfs_getset_niothreads(0); 1014 1015 rv = (sysctl_int(oldp, oldlenp, newp, newlen, 1016 &nfs_niothreads)); 1017 1018 if (newp) 1019 nfs_getset_niothreads(1); 1020 1021 return rv; 1022 1023 default: 1024 return EOPNOTSUPP; 1025 } 1026 } 1027 1028 1029 /* 1030 * At this point, this should never happen 1031 */ 1032 /* ARGSUSED */ 1033 int 1034 nfs_fhtovp(mp, fhp, vpp) 1035 struct mount *mp; 1036 struct fid *fhp; 1037 struct vnode **vpp; 1038 { 1039 1040 return (EINVAL); 1041 } 1042 1043 /* ARGSUSED */ 1044 int 1045 nfs_checkexp(mp, nam, exflagsp, credanonp) 1046 struct mount *mp; 1047 struct mbuf *nam; 1048 int *exflagsp; 1049 struct ucred **credanonp; 1050 { 1051 1052 return (EINVAL); 1053 } 1054 1055 /* 1056 * Vnode pointer to File handle, should never happen either 1057 */ 1058 /* ARGSUSED */ 1059 int 1060 nfs_vptofh(vp, fhp) 1061 struct vnode *vp; 1062 struct fid *fhp; 1063 { 1064 1065 return (EINVAL); 1066 } 1067 1068 /* 1069 * Vfs start routine, a no-op. 1070 */ 1071 /* ARGSUSED */ 1072 int 1073 nfs_start(mp, flags, p) 1074 struct mount *mp; 1075 int flags; 1076 struct proc *p; 1077 { 1078 1079 return (0); 1080 } 1081 1082 /* 1083 * Do operations associated with quotas, not supported 1084 */ 1085 /* ARGSUSED */ 1086 int 1087 nfs_quotactl(mp, cmd, uid, arg, p) 1088 struct mount *mp; 1089 int cmd; 1090 uid_t uid; 1091 caddr_t arg; 1092 struct proc *p; 1093 { 1094 1095 return (EOPNOTSUPP); 1096 } 1097