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