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