1 /* $NetBSD: nfs_vfsops.c,v 1.112 2001/12/04 18:38:08 christos 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.112 2001/12/04 18:38:08 christos 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 int nfs_sysctl __P((int *, u_int, void *, size_t *, void *, size_t, 84 struct proc *)); 85 86 /* 87 * nfs vfs operations. 88 */ 89 90 extern const struct vnodeopv_desc nfsv2_vnodeop_opv_desc; 91 extern const struct vnodeopv_desc spec_nfsv2nodeop_opv_desc; 92 extern const struct vnodeopv_desc fifo_nfsv2nodeop_opv_desc; 93 94 const struct vnodeopv_desc * const nfs_vnodeopv_descs[] = { 95 &nfsv2_vnodeop_opv_desc, 96 &spec_nfsv2nodeop_opv_desc, 97 &fifo_nfsv2nodeop_opv_desc, 98 NULL, 99 }; 100 101 struct vfsops nfs_vfsops = { 102 MOUNT_NFS, 103 nfs_mount, 104 nfs_start, 105 nfs_unmount, 106 nfs_root, 107 nfs_quotactl, 108 nfs_statfs, 109 nfs_sync, 110 nfs_vget, 111 nfs_fhtovp, 112 nfs_vptofh, 113 nfs_vfs_init, 114 nfs_vfs_reinit, 115 nfs_vfs_done, 116 nfs_sysctl, 117 nfs_mountroot, 118 nfs_checkexp, 119 nfs_vnodeopv_descs, 120 }; 121 122 extern u_int32_t nfs_procids[NFS_NPROCS]; 123 extern u_int32_t nfs_prog, nfs_vers; 124 125 static int nfs_mount_diskless __P((struct nfs_dlmount *, const char *, 126 struct mount **, struct vnode **, struct proc *)); 127 128 #define TRUE 1 129 #define FALSE 0 130 131 /* 132 * nfs statfs call 133 */ 134 int 135 nfs_statfs(mp, sbp, p) 136 struct mount *mp; 137 struct statfs *sbp; 138 struct proc *p; 139 { 140 struct vnode *vp; 141 struct nfs_statfs *sfp; 142 caddr_t cp; 143 u_int32_t *tl; 144 int32_t t1, t2; 145 caddr_t bpos, dpos, cp2; 146 struct nfsmount *nmp = VFSTONFS(mp); 147 int error = 0, retattr; 148 #ifdef NFS_V2_ONLY 149 const int v3 = 0; 150 #else 151 int v3 = (nmp->nm_flag & NFSMNT_NFSV3); 152 #endif 153 struct mbuf *mreq, *mrep = NULL, *md, *mb, *mb2; 154 struct ucred *cred; 155 u_quad_t tquad; 156 157 #ifndef nolint 158 sfp = (struct nfs_statfs *)0; 159 #endif 160 vp = nmp->nm_vnode; 161 cred = crget(); 162 cred->cr_ngroups = 0; 163 #ifndef NFS_V2_ONLY 164 if (v3 && (nmp->nm_iflag & NFSMNT_GOTFSINFO) == 0) 165 (void)nfs_fsinfo(nmp, vp, cred, p); 166 #endif 167 nfsstats.rpccnt[NFSPROC_FSSTAT]++; 168 nfsm_reqhead(vp, NFSPROC_FSSTAT, NFSX_FH(v3)); 169 nfsm_fhtom(vp, v3); 170 nfsm_request(vp, NFSPROC_FSSTAT, p, cred); 171 if (v3) 172 nfsm_postop_attr(vp, retattr); 173 if (error) { 174 if (mrep != NULL) 175 m_free(mrep); 176 goto nfsmout; 177 } 178 nfsm_dissect(sfp, struct nfs_statfs *, NFSX_STATFS(v3)); 179 #ifdef COMPAT_09 180 sbp->f_type = 2; 181 #else 182 sbp->f_type = 0; 183 #endif 184 sbp->f_flags = nmp->nm_flag; 185 sbp->f_iosize = min(nmp->nm_rsize, nmp->nm_wsize); 186 if (v3) { 187 sbp->f_bsize = NFS_FABLKSIZE; 188 tquad = fxdr_hyper(&sfp->sf_tbytes); 189 sbp->f_blocks = (long)((quad_t)tquad / (quad_t)NFS_FABLKSIZE); 190 tquad = fxdr_hyper(&sfp->sf_fbytes); 191 sbp->f_bfree = (long)((quad_t)tquad / (quad_t)NFS_FABLKSIZE); 192 tquad = fxdr_hyper(&sfp->sf_abytes); 193 sbp->f_bavail = (long)((quad_t)tquad / (quad_t)NFS_FABLKSIZE); 194 tquad = fxdr_hyper(&sfp->sf_tfiles); 195 sbp->f_files = (long)tquad; 196 tquad = fxdr_hyper(&sfp->sf_ffiles); 197 sbp->f_ffree = (long)tquad; 198 } else { 199 sbp->f_bsize = fxdr_unsigned(int32_t, sfp->sf_bsize); 200 sbp->f_blocks = fxdr_unsigned(int32_t, sfp->sf_blocks); 201 sbp->f_bfree = fxdr_unsigned(int32_t, sfp->sf_bfree); 202 sbp->f_bavail = fxdr_unsigned(int32_t, sfp->sf_bavail); 203 sbp->f_files = 0; 204 sbp->f_ffree = 0; 205 } 206 if (sbp != &mp->mnt_stat) { 207 memcpy(sbp->f_mntonname, mp->mnt_stat.f_mntonname, MNAMELEN); 208 memcpy(sbp->f_mntfromname, mp->mnt_stat.f_mntfromname, MNAMELEN); 209 } 210 strncpy(&sbp->f_fstypename[0], mp->mnt_op->vfs_name, MFSNAMELEN); 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, *mb2; 234 u_int64_t maxfsize; 235 236 nfsstats.rpccnt[NFSPROC_FSINFO]++; 237 nfsm_reqhead(vp, NFSPROC_FSINFO, NFSX_FH(1)); 238 nfsm_fhtom(vp, 1); 239 nfsm_request(vp, NFSPROC_FSINFO, p, cred); 240 nfsm_postop_attr(vp, retattr); 241 if (!error) { 242 nfsm_dissect(fsp, struct nfsv3_fsinfo *, NFSX_V3FSINFO); 243 pref = fxdr_unsigned(u_int32_t, fsp->fs_wtpref); 244 if (pref < nmp->nm_wsize && pref >= NFS_FABLKSIZE) 245 nmp->nm_wsize = (pref + NFS_FABLKSIZE - 1) & 246 ~(NFS_FABLKSIZE - 1); 247 max = fxdr_unsigned(u_int32_t, fsp->fs_wtmax); 248 if (max < nmp->nm_wsize && max > 0) { 249 nmp->nm_wsize = max & ~(NFS_FABLKSIZE - 1); 250 if (nmp->nm_wsize == 0) 251 nmp->nm_wsize = max; 252 } 253 pref = fxdr_unsigned(u_int32_t, fsp->fs_rtpref); 254 if (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 memcpy(mtod(m, caddr_t), (caddr_t)ndmntp->ndm_args.addr, 393 (m->m_len = ndmntp->ndm_args.addr->sa_len)); 394 395 error = mountnfs(&ndmntp->ndm_args, mp, m, mntname, 396 ndmntp->ndm_args.hostname, vpp, p); 397 if (error) { 398 mp->mnt_op->vfs_refcount--; 399 vfs_unbusy(mp); 400 printf("nfs_mountroot: mount %s failed: %d\n", 401 mntname, error); 402 free(mp, M_MOUNT); 403 } else 404 *mpp = mp; 405 406 return (error); 407 } 408 409 void 410 nfs_decode_args(nmp, argp) 411 struct nfsmount *nmp; 412 struct nfs_args *argp; 413 { 414 int s; 415 int adjsock; 416 int maxio; 417 418 s = splsoftnet(); 419 420 /* 421 * Silently clear NFSMNT_NOCONN if it's a TCP mount, it makes 422 * no sense in that context. 423 */ 424 if (argp->sotype == SOCK_STREAM) 425 argp->flags &= ~NFSMNT_NOCONN; 426 427 /* 428 * Cookie translation is not needed for v2, silently ignore it. 429 */ 430 if ((argp->flags & (NFSMNT_XLATECOOKIE|NFSMNT_NFSV3)) == 431 NFSMNT_XLATECOOKIE) 432 argp->flags &= ~NFSMNT_XLATECOOKIE; 433 434 /* Re-bind if rsrvd port requested and wasn't on one */ 435 adjsock = !(nmp->nm_flag & NFSMNT_RESVPORT) 436 && (argp->flags & NFSMNT_RESVPORT); 437 /* Also re-bind if we're switching to/from a connected UDP socket */ 438 adjsock |= ((nmp->nm_flag & NFSMNT_NOCONN) != 439 (argp->flags & NFSMNT_NOCONN)); 440 441 /* Update flags. */ 442 nmp->nm_flag = argp->flags; 443 splx(s); 444 445 if ((argp->flags & NFSMNT_TIMEO) && argp->timeo > 0) { 446 nmp->nm_timeo = (argp->timeo * NFS_HZ + 5) / 10; 447 if (nmp->nm_timeo < NFS_MINTIMEO) 448 nmp->nm_timeo = NFS_MINTIMEO; 449 else if (nmp->nm_timeo > NFS_MAXTIMEO) 450 nmp->nm_timeo = NFS_MAXTIMEO; 451 } 452 453 if ((argp->flags & NFSMNT_RETRANS) && argp->retrans > 1) { 454 nmp->nm_retry = argp->retrans; 455 if (nmp->nm_retry > NFS_MAXREXMIT) 456 nmp->nm_retry = NFS_MAXREXMIT; 457 } 458 459 #ifndef NFS_V2_ONLY 460 if (argp->flags & NFSMNT_NFSV3) { 461 if (argp->sotype == SOCK_DGRAM) 462 maxio = NFS_MAXDGRAMDATA; 463 else 464 maxio = NFS_MAXDATA; 465 } else 466 #endif 467 maxio = NFS_V2MAXDATA; 468 469 if ((argp->flags & NFSMNT_WSIZE) && argp->wsize > 0) { 470 int osize = nmp->nm_wsize; 471 nmp->nm_wsize = argp->wsize; 472 /* Round down to multiple of blocksize */ 473 nmp->nm_wsize &= ~(NFS_FABLKSIZE - 1); 474 if (nmp->nm_wsize <= 0) 475 nmp->nm_wsize = NFS_FABLKSIZE; 476 adjsock |= (nmp->nm_wsize != osize); 477 } 478 if (nmp->nm_wsize > maxio) 479 nmp->nm_wsize = maxio; 480 if (nmp->nm_wsize > MAXBSIZE) 481 nmp->nm_wsize = MAXBSIZE; 482 483 if ((argp->flags & NFSMNT_RSIZE) && argp->rsize > 0) { 484 int osize = nmp->nm_rsize; 485 nmp->nm_rsize = argp->rsize; 486 /* Round down to multiple of blocksize */ 487 nmp->nm_rsize &= ~(NFS_FABLKSIZE - 1); 488 if (nmp->nm_rsize <= 0) 489 nmp->nm_rsize = NFS_FABLKSIZE; 490 adjsock |= (nmp->nm_rsize != osize); 491 } 492 if (nmp->nm_rsize > maxio) 493 nmp->nm_rsize = maxio; 494 if (nmp->nm_rsize > MAXBSIZE) 495 nmp->nm_rsize = MAXBSIZE; 496 497 if ((argp->flags & NFSMNT_READDIRSIZE) && argp->readdirsize > 0) { 498 nmp->nm_readdirsize = argp->readdirsize; 499 /* Round down to multiple of minimum blocksize */ 500 nmp->nm_readdirsize &= ~(NFS_DIRFRAGSIZ - 1); 501 if (nmp->nm_readdirsize < NFS_DIRFRAGSIZ) 502 nmp->nm_readdirsize = NFS_DIRFRAGSIZ; 503 /* Bigger than buffer size makes no sense */ 504 if (nmp->nm_readdirsize > NFS_DIRBLKSIZ) 505 nmp->nm_readdirsize = NFS_DIRBLKSIZ; 506 } else if (argp->flags & NFSMNT_RSIZE) 507 nmp->nm_readdirsize = nmp->nm_rsize; 508 509 if (nmp->nm_readdirsize > maxio) 510 nmp->nm_readdirsize = maxio; 511 512 if ((argp->flags & NFSMNT_MAXGRPS) && argp->maxgrouplist >= 0 && 513 argp->maxgrouplist <= NFS_MAXGRPS) 514 nmp->nm_numgrps = argp->maxgrouplist; 515 if ((argp->flags & NFSMNT_READAHEAD) && argp->readahead >= 0 && 516 argp->readahead <= NFS_MAXRAHEAD) 517 nmp->nm_readahead = argp->readahead; 518 if ((argp->flags & NFSMNT_LEASETERM) && argp->leaseterm >= 2 && 519 argp->leaseterm <= NQ_MAXLEASE) 520 nmp->nm_leaseterm = argp->leaseterm; 521 if ((argp->flags & NFSMNT_DEADTHRESH) && argp->deadthresh >= 1 && 522 argp->deadthresh <= NQ_NEVERDEAD) 523 nmp->nm_deadthresh = argp->deadthresh; 524 525 adjsock |= ((nmp->nm_sotype != argp->sotype) || 526 (nmp->nm_soproto != argp->proto)); 527 nmp->nm_sotype = argp->sotype; 528 nmp->nm_soproto = argp->proto; 529 530 if (nmp->nm_so && adjsock) { 531 nfs_safedisconnect(nmp); 532 if (nmp->nm_sotype == SOCK_DGRAM) 533 while (nfs_connect(nmp, (struct nfsreq *)0)) { 534 printf("nfs_args: retrying connect\n"); 535 (void) tsleep((caddr_t)&lbolt, 536 PSOCK, "nfscn3", 0); 537 } 538 } 539 } 540 541 /* 542 * VFS Operations. 543 * 544 * mount system call 545 * It seems a bit dumb to copyinstr() the host and path here and then 546 * memcpy() them in mountnfs(), but I wanted to detect errors before 547 * doing the sockargs() call because sockargs() allocates an mbuf and 548 * an error after that means that I have to release the mbuf. 549 */ 550 /* ARGSUSED */ 551 int 552 nfs_mount(mp, path, data, ndp, p) 553 struct mount *mp; 554 const char *path; 555 void *data; 556 struct nameidata *ndp; 557 struct proc *p; 558 { 559 int error; 560 struct nfs_args args; 561 struct mbuf *nam; 562 struct vnode *vp; 563 char pth[MNAMELEN], hst[MNAMELEN]; 564 size_t len; 565 u_char nfh[NFSX_V3FHMAX]; 566 567 error = copyin(data, (caddr_t)&args, sizeof (struct nfs_args)); 568 if (error) 569 return (error); 570 if (args.version != NFS_ARGSVERSION) 571 return (EPROGMISMATCH); 572 #ifdef NFS_V2_ONLY 573 if (args.flags & NFSMNT_NQNFS) 574 return (EPROGUNAVAIL); 575 if (args.flags & NFSMNT_NFSV3) 576 return (EPROGMISMATCH); 577 #endif 578 if (mp->mnt_flag & MNT_UPDATE) { 579 struct nfsmount *nmp = VFSTONFS(mp); 580 581 if (nmp == NULL) 582 return (EIO); 583 /* 584 * When doing an update, we can't change from or to 585 * v3 and/or nqnfs, or change cookie translation 586 */ 587 args.flags = (args.flags & 588 ~(NFSMNT_NFSV3|NFSMNT_NQNFS|NFSMNT_XLATECOOKIE)) | 589 (nmp->nm_flag & 590 (NFSMNT_NFSV3|NFSMNT_NQNFS|NFSMNT_XLATECOOKIE)); 591 nfs_decode_args(nmp, &args); 592 return (0); 593 } 594 if (args.fhsize < 0 || args.fhsize > NFSX_V3FHMAX) 595 return (EINVAL); 596 error = copyin((caddr_t)args.fh, (caddr_t)nfh, args.fhsize); 597 if (error) 598 return (error); 599 error = copyinstr(path, pth, MNAMELEN-1, &len); 600 if (error) 601 return (error); 602 memset(&pth[len], 0, MNAMELEN - len); 603 error = copyinstr(args.hostname, hst, MNAMELEN-1, &len); 604 if (error) 605 return (error); 606 memset(&hst[len], 0, MNAMELEN - len); 607 /* sockargs() call must be after above copyin() calls */ 608 error = sockargs(&nam, (caddr_t)args.addr, args.addrlen, MT_SONAME); 609 if (error) 610 return (error); 611 args.fh = nfh; 612 error = mountnfs(&args, mp, nam, pth, hst, &vp, p); 613 return (error); 614 } 615 616 /* 617 * Common code for mount and mountroot 618 */ 619 int 620 mountnfs(argp, mp, nam, pth, hst, vpp, p) 621 struct nfs_args *argp; 622 struct mount *mp; 623 struct mbuf *nam; 624 const char *pth, *hst; 625 struct vnode **vpp; 626 struct proc *p; 627 { 628 struct nfsmount *nmp; 629 struct nfsnode *np; 630 int error; 631 struct vattr attrs; 632 struct ucred *cr; 633 634 /* 635 * If the number of nfs iothreads to use has never 636 * been set, create a reasonable number of them. 637 */ 638 639 if (nfs_niothreads < 0) { 640 nfs_niothreads = NFS_DEFAULT_NIOTHREADS; 641 nfs_getset_niothreads(TRUE); 642 } 643 644 if (mp->mnt_flag & MNT_UPDATE) { 645 nmp = VFSTONFS(mp); 646 /* update paths, file handles, etc, here XXX */ 647 m_freem(nam); 648 return (0); 649 } else { 650 MALLOC(nmp, struct nfsmount *, sizeof (struct nfsmount), 651 M_NFSMNT, M_WAITOK); 652 memset((caddr_t)nmp, 0, sizeof (struct nfsmount)); 653 mp->mnt_data = (qaddr_t)nmp; 654 TAILQ_INIT(&nmp->nm_uidlruhead); 655 TAILQ_INIT(&nmp->nm_bufq); 656 } 657 vfs_getnewfsid(mp); 658 nmp->nm_mountp = mp; 659 660 #ifndef NFS_V2_ONLY 661 if (argp->flags & NFSMNT_NQNFS) 662 /* 663 * We have to set mnt_maxsymlink to a non-zero value so 664 * that COMPAT_43 routines will know that we are setting 665 * the d_type field in directories (and can zero it for 666 * unsuspecting binaries). 667 */ 668 mp->mnt_maxsymlinklen = 1; 669 #endif 670 671 #ifndef NFS_V2_ONLY 672 if ((argp->flags & NFSMNT_NFSV3) == 0) 673 #endif 674 /* 675 * V2 can only handle 32 bit filesizes. For v3, nfs_fsinfo 676 * will fill this in. 677 */ 678 nmp->nm_maxfilesize = 0xffffffffLL; 679 680 nmp->nm_timeo = NFS_TIMEO; 681 nmp->nm_retry = NFS_RETRANS; 682 nmp->nm_wsize = NFS_WSIZE; 683 nmp->nm_rsize = NFS_RSIZE; 684 nmp->nm_readdirsize = NFS_READDIRSIZE; 685 nmp->nm_numgrps = NFS_MAXGRPS; 686 nmp->nm_readahead = NFS_DEFRAHEAD; 687 nmp->nm_leaseterm = NQ_DEFLEASE; 688 nmp->nm_deadthresh = NQ_DEADTHRESH; 689 CIRCLEQ_INIT(&nmp->nm_timerhead); 690 nmp->nm_inprog = NULLVP; 691 #ifdef COMPAT_09 692 mp->mnt_stat.f_type = 2; 693 #else 694 mp->mnt_stat.f_type = 0; 695 #endif 696 strncpy(&mp->mnt_stat.f_fstypename[0], mp->mnt_op->vfs_name, 697 MFSNAMELEN); 698 memcpy(mp->mnt_stat.f_mntfromname, hst, MNAMELEN); 699 memcpy(mp->mnt_stat.f_mntonname, pth, MNAMELEN); 700 nmp->nm_nam = nam; 701 702 /* Set up the sockets and per-host congestion */ 703 nmp->nm_sotype = argp->sotype; 704 nmp->nm_soproto = argp->proto; 705 706 nfs_decode_args(nmp, argp); 707 708 mp->mnt_fs_bshift = ffs(MIN(nmp->nm_rsize, nmp->nm_wsize)) - 1; 709 mp->mnt_dev_bshift = DEV_BSHIFT; 710 711 /* 712 * For Connection based sockets (TCP,...) defer the connect until 713 * the first request, in case the server is not responding. 714 */ 715 if (nmp->nm_sotype == SOCK_DGRAM && 716 (error = nfs_connect(nmp, (struct nfsreq *)0))) 717 goto bad; 718 719 /* 720 * This is silly, but it has to be set so that vinifod() works. 721 * We do not want to do an nfs_statfs() here since we can get 722 * stuck on a dead server and we are holding a lock on the mount 723 * point. 724 */ 725 mp->mnt_stat.f_iosize = NFS_MAXDGRAMDATA; 726 error = nfs_nget(mp, (nfsfh_t *)argp->fh, argp->fhsize, &np); 727 if (error) 728 goto bad; 729 *vpp = NFSTOV(np); 730 VOP_GETATTR(*vpp, &attrs, p->p_ucred, p); 731 if ((nmp->nm_flag & NFSMNT_NFSV3) && ((*vpp)->v_type == VDIR)) { 732 cr = crget(); 733 cr->cr_uid = attrs.va_uid; 734 cr->cr_gid = attrs.va_gid; 735 cr->cr_ngroups = 0; 736 nfs_cookieheuristic(*vpp, &nmp->nm_iflag, p, cr); 737 crfree(cr); 738 } 739 740 /* 741 * A reference count is needed on the nfsnode representing the 742 * remote root. If this object is not persistent, then backward 743 * traversals of the mount point (i.e. "..") will not work if 744 * the nfsnode gets flushed out of the cache. Ufs does not have 745 * this problem, because one can identify root inodes by their 746 * number == ROOTINO (2). So, just unlock, but no rele. 747 */ 748 749 nmp->nm_vnode = *vpp; 750 VOP_UNLOCK(*vpp, 0); 751 752 return (0); 753 bad: 754 nfs_disconnect(nmp); 755 free((caddr_t)nmp, M_NFSMNT); 756 m_freem(nam); 757 return (error); 758 } 759 760 /* 761 * unmount system call 762 */ 763 int 764 nfs_unmount(mp, mntflags, p) 765 struct mount *mp; 766 int mntflags; 767 struct proc *p; 768 { 769 struct nfsmount *nmp; 770 struct vnode *vp; 771 int error, flags = 0; 772 773 if (mntflags & MNT_FORCE) 774 flags |= FORCECLOSE; 775 nmp = VFSTONFS(mp); 776 /* 777 * Goes something like this.. 778 * - Check for activity on the root vnode (other than ourselves). 779 * - Call vflush() to clear out vnodes for this file system, 780 * except for the root vnode. 781 * - Decrement reference on the vnode representing remote root. 782 * - Close the socket 783 * - Free up the data structures 784 */ 785 /* 786 * We need to decrement the ref. count on the nfsnode representing 787 * the remote root. See comment in mountnfs(). The VFS unmount() 788 * has done vput on this vnode, otherwise we would get deadlock! 789 */ 790 vp = nmp->nm_vnode; 791 error = vget(vp, LK_EXCLUSIVE | LK_RETRY); 792 if (error != 0) 793 return error; 794 795 if ((mntflags & MNT_FORCE) == 0 && vp->v_usecount > 2) { 796 vput(vp); 797 return (EBUSY); 798 } 799 800 /* 801 * Must handshake with nqnfs_clientd() if it is active. 802 */ 803 nmp->nm_iflag |= NFSMNT_DISMINPROG; 804 while (nmp->nm_inprog != NULLVP) 805 (void) tsleep((caddr_t)&lbolt, PSOCK, "nfsdism", 0); 806 error = vflush(mp, vp, flags); 807 if (error) { 808 vput(vp); 809 nmp->nm_iflag &= ~NFSMNT_DISMINPROG; 810 return (error); 811 } 812 813 /* 814 * We are now committed to the unmount; mark the mount structure 815 * as doomed so that any sleepers kicked awake by nfs_disconnect 816 * will go away cleanly. 817 */ 818 nmp->nm_iflag |= NFSMNT_DISMNT; 819 820 /* 821 * There are two reference counts to get rid of here 822 * (see comment in mountnfs()). 823 */ 824 vrele(vp); 825 vput(vp); 826 vgone(vp); 827 nfs_disconnect(nmp); 828 m_freem(nmp->nm_nam); 829 830 /* 831 * For NQNFS, let the server daemon free the nfsmount structure. 832 */ 833 if ((nmp->nm_flag & (NFSMNT_NQNFS | NFSMNT_KERB)) == 0) 834 free((caddr_t)nmp, M_NFSMNT); 835 return (0); 836 } 837 838 /* 839 * Return root of a filesystem 840 */ 841 int 842 nfs_root(mp, vpp) 843 struct mount *mp; 844 struct vnode **vpp; 845 { 846 struct vnode *vp; 847 struct nfsmount *nmp; 848 int error; 849 850 nmp = VFSTONFS(mp); 851 vp = nmp->nm_vnode; 852 error = vget(vp, LK_EXCLUSIVE | LK_RETRY); 853 if (error != 0) 854 return error; 855 if (vp->v_type == VNON) 856 vp->v_type = VDIR; 857 vp->v_flag = VROOT; 858 *vpp = vp; 859 return (0); 860 } 861 862 extern int syncprt; 863 864 /* 865 * Flush out the buffer cache 866 */ 867 /* ARGSUSED */ 868 int 869 nfs_sync(mp, waitfor, cred, p) 870 struct mount *mp; 871 int waitfor; 872 struct ucred *cred; 873 struct proc *p; 874 { 875 struct vnode *vp; 876 int error, allerror = 0; 877 878 /* 879 * Force stale buffer cache information to be flushed. 880 */ 881 loop: 882 for (vp = mp->mnt_vnodelist.lh_first; 883 vp != NULL; 884 vp = vp->v_mntvnodes.le_next) { 885 /* 886 * If the vnode that we are about to sync is no longer 887 * associated with this mount point, start over. 888 */ 889 if (vp->v_mount != mp) 890 goto loop; 891 if (waitfor == MNT_LAZY || VOP_ISLOCKED(vp) || 892 (LIST_EMPTY(&vp->v_dirtyblkhd) && 893 vp->v_uobj.uo_npages == 0)) 894 continue; 895 if (vget(vp, LK_EXCLUSIVE)) 896 goto loop; 897 error = VOP_FSYNC(vp, cred, 898 waitfor == MNT_WAIT ? FSYNC_WAIT : 0, 0, 0, p); 899 if (error) 900 allerror = error; 901 vput(vp); 902 } 903 return (allerror); 904 } 905 906 /* 907 * NFS flat namespace lookup. 908 * Currently unsupported. 909 */ 910 /* ARGSUSED */ 911 int 912 nfs_vget(mp, ino, vpp) 913 struct mount *mp; 914 ino_t ino; 915 struct vnode **vpp; 916 { 917 918 return (EOPNOTSUPP); 919 } 920 921 /* 922 * Do that sysctl thang... 923 */ 924 int 925 nfs_sysctl(name, namelen, oldp, oldlenp, newp, newlen, p) 926 int *name; 927 u_int namelen; 928 void *oldp; 929 size_t *oldlenp; 930 void *newp; 931 size_t newlen; 932 struct proc *p; 933 { 934 int rv; 935 936 /* 937 * All names at this level are terminal. 938 */ 939 if(namelen > 1) 940 return ENOTDIR; /* overloaded */ 941 942 switch(name[0]) { 943 case NFS_NFSSTATS: 944 if(!oldp) { 945 *oldlenp = sizeof nfsstats; 946 return 0; 947 } 948 949 if(*oldlenp < sizeof nfsstats) { 950 *oldlenp = sizeof nfsstats; 951 return ENOMEM; 952 } 953 954 rv = copyout(&nfsstats, oldp, sizeof nfsstats); 955 if(rv) return rv; 956 957 if(newp && newlen != sizeof nfsstats) 958 return EINVAL; 959 960 if(newp) { 961 return copyin(newp, &nfsstats, sizeof nfsstats); 962 } 963 return 0; 964 965 case NFS_IOTHREADS: 966 nfs_getset_niothreads(0); 967 968 rv = (sysctl_int(oldp, oldlenp, newp, newlen, 969 &nfs_niothreads)); 970 971 if (newp) 972 nfs_getset_niothreads(1); 973 974 return rv; 975 976 default: 977 return EOPNOTSUPP; 978 } 979 } 980 981 982 /* 983 * At this point, this should never happen 984 */ 985 /* ARGSUSED */ 986 int 987 nfs_fhtovp(mp, fhp, vpp) 988 struct mount *mp; 989 struct fid *fhp; 990 struct vnode **vpp; 991 { 992 993 return (EINVAL); 994 } 995 996 /* ARGSUSED */ 997 int 998 nfs_checkexp(mp, nam, exflagsp, credanonp) 999 struct mount *mp; 1000 struct mbuf *nam; 1001 int *exflagsp; 1002 struct ucred **credanonp; 1003 { 1004 1005 return (EINVAL); 1006 } 1007 1008 /* 1009 * Vnode pointer to File handle, should never happen either 1010 */ 1011 /* ARGSUSED */ 1012 int 1013 nfs_vptofh(vp, fhp) 1014 struct vnode *vp; 1015 struct fid *fhp; 1016 { 1017 1018 return (EINVAL); 1019 } 1020 1021 /* 1022 * Vfs start routine, a no-op. 1023 */ 1024 /* ARGSUSED */ 1025 int 1026 nfs_start(mp, flags, p) 1027 struct mount *mp; 1028 int flags; 1029 struct proc *p; 1030 { 1031 1032 return (0); 1033 } 1034 1035 /* 1036 * Do operations associated with quotas, not supported 1037 */ 1038 /* ARGSUSED */ 1039 int 1040 nfs_quotactl(mp, cmd, uid, arg, p) 1041 struct mount *mp; 1042 int cmd; 1043 uid_t uid; 1044 caddr_t arg; 1045 struct proc *p; 1046 { 1047 1048 return (EOPNOTSUPP); 1049 } 1050