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