1 /* $NetBSD: nfs_vfsops.c,v 1.115 2002/07/30 07:40:13 soren 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.115 2002/07/30 07:40:13 soren 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, *hst; 564 size_t len; 565 u_char *nfh; 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 MALLOC(nfh, u_char *, NFSX_V3FHMAX, M_TEMP, M_WAITOK); 597 error = copyin((caddr_t)args.fh, (caddr_t)nfh, args.fhsize); 598 if (error) 599 return (error); 600 MALLOC(pth, char *, MNAMELEN, M_TEMP, M_WAITOK); 601 error = copyinstr(path, pth, MNAMELEN - 1, &len); 602 if (error) 603 goto free_nfh; 604 memset(&pth[len], 0, MNAMELEN - len); 605 MALLOC(hst, char *, MNAMELEN, M_TEMP, M_WAITOK); 606 error = copyinstr(args.hostname, hst, MNAMELEN - 1, &len); 607 if (error) 608 goto free_pth; 609 memset(&hst[len], 0, MNAMELEN - len); 610 /* sockargs() call must be after above copyin() calls */ 611 error = sockargs(&nam, (caddr_t)args.addr, args.addrlen, MT_SONAME); 612 if (error) 613 goto free_hst; 614 args.fh = nfh; 615 error = mountnfs(&args, mp, nam, pth, hst, &vp, p); 616 617 free_hst: 618 FREE(hst, M_TEMP); 619 free_pth: 620 FREE(pth, M_TEMP); 621 free_nfh: 622 FREE(nfh, M_TEMP); 623 624 return (error); 625 } 626 627 /* 628 * Common code for mount and mountroot 629 */ 630 int 631 mountnfs(argp, mp, nam, pth, hst, vpp, p) 632 struct nfs_args *argp; 633 struct mount *mp; 634 struct mbuf *nam; 635 const char *pth, *hst; 636 struct vnode **vpp; 637 struct proc *p; 638 { 639 struct nfsmount *nmp; 640 struct nfsnode *np; 641 int error; 642 struct vattr *attrs; 643 struct ucred *cr; 644 645 /* 646 * If the number of nfs iothreads to use has never 647 * been set, create a reasonable number of them. 648 */ 649 650 if (nfs_niothreads < 0) { 651 nfs_niothreads = NFS_DEFAULT_NIOTHREADS; 652 nfs_getset_niothreads(TRUE); 653 } 654 655 if (mp->mnt_flag & MNT_UPDATE) { 656 nmp = VFSTONFS(mp); 657 /* update paths, file handles, etc, here XXX */ 658 m_freem(nam); 659 return (0); 660 } else { 661 MALLOC(nmp, struct nfsmount *, sizeof (struct nfsmount), 662 M_NFSMNT, M_WAITOK); 663 memset((caddr_t)nmp, 0, sizeof (struct nfsmount)); 664 mp->mnt_data = nmp; 665 TAILQ_INIT(&nmp->nm_uidlruhead); 666 TAILQ_INIT(&nmp->nm_bufq); 667 } 668 vfs_getnewfsid(mp); 669 nmp->nm_mountp = mp; 670 671 #ifndef NFS_V2_ONLY 672 if (argp->flags & NFSMNT_NQNFS) 673 /* 674 * We have to set mnt_maxsymlink to a non-zero value so 675 * that COMPAT_43 routines will know that we are setting 676 * the d_type field in directories (and can zero it for 677 * unsuspecting binaries). 678 */ 679 mp->mnt_maxsymlinklen = 1; 680 #endif 681 682 #ifndef NFS_V2_ONLY 683 if ((argp->flags & NFSMNT_NFSV3) == 0) 684 #endif 685 /* 686 * V2 can only handle 32 bit filesizes. For v3, nfs_fsinfo 687 * will fill this in. 688 */ 689 nmp->nm_maxfilesize = 0xffffffffLL; 690 691 nmp->nm_timeo = NFS_TIMEO; 692 nmp->nm_retry = NFS_RETRANS; 693 nmp->nm_wsize = NFS_WSIZE; 694 nmp->nm_rsize = NFS_RSIZE; 695 nmp->nm_readdirsize = NFS_READDIRSIZE; 696 nmp->nm_numgrps = NFS_MAXGRPS; 697 nmp->nm_readahead = NFS_DEFRAHEAD; 698 nmp->nm_leaseterm = NQ_DEFLEASE; 699 nmp->nm_deadthresh = NQ_DEADTHRESH; 700 CIRCLEQ_INIT(&nmp->nm_timerhead); 701 nmp->nm_inprog = NULLVP; 702 #ifdef COMPAT_09 703 mp->mnt_stat.f_type = 2; 704 #else 705 mp->mnt_stat.f_type = 0; 706 #endif 707 strncpy(&mp->mnt_stat.f_fstypename[0], mp->mnt_op->vfs_name, 708 MFSNAMELEN); 709 memcpy(mp->mnt_stat.f_mntfromname, hst, MNAMELEN); 710 memcpy(mp->mnt_stat.f_mntonname, pth, MNAMELEN); 711 nmp->nm_nam = nam; 712 713 /* Set up the sockets and per-host congestion */ 714 nmp->nm_sotype = argp->sotype; 715 nmp->nm_soproto = argp->proto; 716 717 nfs_decode_args(nmp, argp); 718 719 mp->mnt_fs_bshift = ffs(MIN(nmp->nm_rsize, nmp->nm_wsize)) - 1; 720 mp->mnt_dev_bshift = DEV_BSHIFT; 721 722 /* 723 * For Connection based sockets (TCP,...) defer the connect until 724 * the first request, in case the server is not responding. 725 */ 726 if (nmp->nm_sotype == SOCK_DGRAM && 727 (error = nfs_connect(nmp, (struct nfsreq *)0))) 728 goto bad; 729 730 /* 731 * This is silly, but it has to be set so that vinifod() works. 732 * We do not want to do an nfs_statfs() here since we can get 733 * stuck on a dead server and we are holding a lock on the mount 734 * point. 735 */ 736 mp->mnt_stat.f_iosize = NFS_MAXDGRAMDATA; 737 error = nfs_nget(mp, (nfsfh_t *)argp->fh, argp->fhsize, &np); 738 if (error) 739 goto bad; 740 *vpp = NFSTOV(np); 741 MALLOC(attrs, struct vattr *, sizeof(struct vattr), M_TEMP, M_WAITOK); 742 VOP_GETATTR(*vpp, attrs, p->p_ucred, p); 743 if ((nmp->nm_flag & NFSMNT_NFSV3) && ((*vpp)->v_type == VDIR)) { 744 cr = crget(); 745 cr->cr_uid = attrs->va_uid; 746 cr->cr_gid = attrs->va_gid; 747 cr->cr_ngroups = 0; 748 nfs_cookieheuristic(*vpp, &nmp->nm_iflag, p, cr); 749 crfree(cr); 750 } 751 FREE(attrs, M_TEMP); 752 753 /* 754 * A reference count is needed on the nfsnode representing the 755 * remote root. If this object is not persistent, then backward 756 * traversals of the mount point (i.e. "..") will not work if 757 * the nfsnode gets flushed out of the cache. Ufs does not have 758 * this problem, because one can identify root inodes by their 759 * number == ROOTINO (2). So, just unlock, but no rele. 760 */ 761 762 nmp->nm_vnode = *vpp; 763 VOP_UNLOCK(*vpp, 0); 764 765 return (0); 766 bad: 767 nfs_disconnect(nmp); 768 free((caddr_t)nmp, M_NFSMNT); 769 m_freem(nam); 770 return (error); 771 } 772 773 /* 774 * unmount system call 775 */ 776 int 777 nfs_unmount(mp, mntflags, p) 778 struct mount *mp; 779 int mntflags; 780 struct proc *p; 781 { 782 struct nfsmount *nmp; 783 struct vnode *vp; 784 int error, flags = 0; 785 786 if (mntflags & MNT_FORCE) 787 flags |= FORCECLOSE; 788 nmp = VFSTONFS(mp); 789 /* 790 * Goes something like this.. 791 * - Check for activity on the root vnode (other than ourselves). 792 * - Call vflush() to clear out vnodes for this file system, 793 * except for the root vnode. 794 * - Decrement reference on the vnode representing remote root. 795 * - Close the socket 796 * - Free up the data structures 797 */ 798 /* 799 * We need to decrement the ref. count on the nfsnode representing 800 * the remote root. See comment in mountnfs(). The VFS unmount() 801 * has done vput on this vnode, otherwise we would get deadlock! 802 */ 803 vp = nmp->nm_vnode; 804 error = vget(vp, LK_EXCLUSIVE | LK_RETRY); 805 if (error != 0) 806 return error; 807 808 if ((mntflags & MNT_FORCE) == 0 && vp->v_usecount > 2) { 809 vput(vp); 810 return (EBUSY); 811 } 812 813 /* 814 * Must handshake with nqnfs_clientd() if it is active. 815 */ 816 nmp->nm_iflag |= NFSMNT_DISMINPROG; 817 while (nmp->nm_inprog != NULLVP) 818 (void) tsleep((caddr_t)&lbolt, PSOCK, "nfsdism", 0); 819 error = vflush(mp, vp, flags); 820 if (error) { 821 vput(vp); 822 nmp->nm_iflag &= ~NFSMNT_DISMINPROG; 823 return (error); 824 } 825 826 /* 827 * We are now committed to the unmount; mark the mount structure 828 * as doomed so that any sleepers kicked awake by nfs_disconnect 829 * will go away cleanly. 830 */ 831 nmp->nm_iflag |= NFSMNT_DISMNT; 832 833 /* 834 * There are two reference counts to get rid of here 835 * (see comment in mountnfs()). 836 */ 837 vrele(vp); 838 vput(vp); 839 vgone(vp); 840 nfs_disconnect(nmp); 841 m_freem(nmp->nm_nam); 842 843 /* 844 * For NQNFS, let the server daemon free the nfsmount structure. 845 */ 846 if ((nmp->nm_flag & (NFSMNT_NQNFS | NFSMNT_KERB)) == 0) 847 free((caddr_t)nmp, M_NFSMNT); 848 return (0); 849 } 850 851 /* 852 * Return root of a filesystem 853 */ 854 int 855 nfs_root(mp, vpp) 856 struct mount *mp; 857 struct vnode **vpp; 858 { 859 struct vnode *vp; 860 struct nfsmount *nmp; 861 int error; 862 863 nmp = VFSTONFS(mp); 864 vp = nmp->nm_vnode; 865 error = vget(vp, LK_EXCLUSIVE | LK_RETRY); 866 if (error != 0) 867 return error; 868 if (vp->v_type == VNON) 869 vp->v_type = VDIR; 870 vp->v_flag = VROOT; 871 *vpp = vp; 872 return (0); 873 } 874 875 extern int syncprt; 876 877 /* 878 * Flush out the buffer cache 879 */ 880 /* ARGSUSED */ 881 int 882 nfs_sync(mp, waitfor, cred, p) 883 struct mount *mp; 884 int waitfor; 885 struct ucred *cred; 886 struct proc *p; 887 { 888 struct vnode *vp; 889 int error, allerror = 0; 890 891 /* 892 * Force stale buffer cache information to be flushed. 893 */ 894 loop: 895 for (vp = mp->mnt_vnodelist.lh_first; 896 vp != NULL; 897 vp = vp->v_mntvnodes.le_next) { 898 /* 899 * If the vnode that we are about to sync is no longer 900 * associated with this mount point, start over. 901 */ 902 if (vp->v_mount != mp) 903 goto loop; 904 if (waitfor == MNT_LAZY || VOP_ISLOCKED(vp) || 905 (LIST_EMPTY(&vp->v_dirtyblkhd) && 906 vp->v_uobj.uo_npages == 0)) 907 continue; 908 if (vget(vp, LK_EXCLUSIVE)) 909 goto loop; 910 error = VOP_FSYNC(vp, cred, 911 waitfor == MNT_WAIT ? FSYNC_WAIT : 0, 0, 0, p); 912 if (error) 913 allerror = error; 914 vput(vp); 915 } 916 return (allerror); 917 } 918 919 /* 920 * NFS flat namespace lookup. 921 * Currently unsupported. 922 */ 923 /* ARGSUSED */ 924 int 925 nfs_vget(mp, ino, vpp) 926 struct mount *mp; 927 ino_t ino; 928 struct vnode **vpp; 929 { 930 931 return (EOPNOTSUPP); 932 } 933 934 /* 935 * Do that sysctl thang... 936 */ 937 int 938 nfs_sysctl(name, namelen, oldp, oldlenp, newp, newlen, p) 939 int *name; 940 u_int namelen; 941 void *oldp; 942 size_t *oldlenp; 943 void *newp; 944 size_t newlen; 945 struct proc *p; 946 { 947 int rv; 948 949 /* 950 * All names at this level are terminal. 951 */ 952 if(namelen > 1) 953 return ENOTDIR; /* overloaded */ 954 955 switch(name[0]) { 956 case NFS_NFSSTATS: 957 if(!oldp) { 958 *oldlenp = sizeof nfsstats; 959 return 0; 960 } 961 962 if(*oldlenp < sizeof nfsstats) { 963 *oldlenp = sizeof nfsstats; 964 return ENOMEM; 965 } 966 967 rv = copyout(&nfsstats, oldp, sizeof nfsstats); 968 if(rv) return rv; 969 970 if(newp && newlen != sizeof nfsstats) 971 return EINVAL; 972 973 if(newp) { 974 return copyin(newp, &nfsstats, sizeof nfsstats); 975 } 976 return 0; 977 978 case NFS_IOTHREADS: 979 nfs_getset_niothreads(0); 980 981 rv = (sysctl_int(oldp, oldlenp, newp, newlen, 982 &nfs_niothreads)); 983 984 if (newp) 985 nfs_getset_niothreads(1); 986 987 return rv; 988 989 default: 990 return EOPNOTSUPP; 991 } 992 } 993 994 995 /* 996 * At this point, this should never happen 997 */ 998 /* ARGSUSED */ 999 int 1000 nfs_fhtovp(mp, fhp, vpp) 1001 struct mount *mp; 1002 struct fid *fhp; 1003 struct vnode **vpp; 1004 { 1005 1006 return (EINVAL); 1007 } 1008 1009 /* ARGSUSED */ 1010 int 1011 nfs_checkexp(mp, nam, exflagsp, credanonp) 1012 struct mount *mp; 1013 struct mbuf *nam; 1014 int *exflagsp; 1015 struct ucred **credanonp; 1016 { 1017 1018 return (EINVAL); 1019 } 1020 1021 /* 1022 * Vnode pointer to File handle, should never happen either 1023 */ 1024 /* ARGSUSED */ 1025 int 1026 nfs_vptofh(vp, fhp) 1027 struct vnode *vp; 1028 struct fid *fhp; 1029 { 1030 1031 return (EINVAL); 1032 } 1033 1034 /* 1035 * Vfs start routine, a no-op. 1036 */ 1037 /* ARGSUSED */ 1038 int 1039 nfs_start(mp, flags, p) 1040 struct mount *mp; 1041 int flags; 1042 struct proc *p; 1043 { 1044 1045 return (0); 1046 } 1047 1048 /* 1049 * Do operations associated with quotas, not supported 1050 */ 1051 /* ARGSUSED */ 1052 int 1053 nfs_quotactl(mp, cmd, uid, arg, p) 1054 struct mount *mp; 1055 int cmd; 1056 uid_t uid; 1057 caddr_t arg; 1058 struct proc *p; 1059 { 1060 1061 return (EOPNOTSUPP); 1062 } 1063