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