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