1 /* $OpenBSD: nfs_vfsops.c,v 1.103 2014/11/15 00:03:12 tedu Exp $ */ 2 /* $NetBSD: nfs_vfsops.c,v 1.46.4.1 1996/05/25 22:40:35 fvdl Exp $ */ 3 4 /* 5 * Copyright (c) 1989, 1993, 1995 6 * The Regents of the University of California. All rights reserved. 7 * 8 * This code is derived from software contributed to Berkeley by 9 * Rick Macklem at The University of Guelph. 10 * 11 * Redistribution and use in source and binary forms, with or without 12 * modification, are permitted provided that the following conditions 13 * are met: 14 * 1. Redistributions of source code must retain the above copyright 15 * notice, this list of conditions and the following disclaimer. 16 * 2. Redistributions in binary form must reproduce the above copyright 17 * notice, this list of conditions and the following disclaimer in the 18 * documentation and/or other materials provided with the distribution. 19 * 3. Neither the name of the University nor the names of its contributors 20 * may be used to endorse or promote products derived from this software 21 * without specific prior written permission. 22 * 23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 33 * SUCH DAMAGE. 34 * 35 * @(#)nfs_vfsops.c 8.12 (Berkeley) 5/20/95 36 */ 37 38 #include <sys/param.h> 39 #include <sys/conf.h> 40 #include <sys/ioctl.h> 41 #include <sys/signal.h> 42 #include <sys/proc.h> 43 #include <sys/namei.h> 44 #include <sys/vnode.h> 45 #include <sys/kernel.h> 46 #include <sys/mount.h> 47 #include <sys/swap.h> 48 #include <sys/buf.h> 49 #include <sys/mbuf.h> 50 #include <sys/dirent.h> 51 #include <sys/socket.h> 52 #include <sys/socketvar.h> 53 #include <sys/systm.h> 54 #include <sys/sysctl.h> 55 #include <sys/queue.h> 56 57 #include <net/if.h> 58 #include <netinet/in.h> 59 60 #include <nfs/rpcv2.h> 61 #include <nfs/nfsproto.h> 62 #include <nfs/nfsnode.h> 63 #include <nfs/nfs.h> 64 #include <nfs/nfsmount.h> 65 #include <nfs/xdr_subs.h> 66 #include <nfs/nfsm_subs.h> 67 #include <nfs/nfsdiskless.h> 68 #include <nfs/nfs_var.h> 69 70 extern struct nfsstats nfsstats; 71 extern int nfs_ticks; 72 extern u_int32_t nfs_procids[NFS_NPROCS]; 73 74 int nfs_sysctl(int *, u_int, void *, size_t *, void *, size_t, struct proc *); 75 int nfs_checkexp(struct mount *, struct mbuf *, int *, struct ucred **); 76 struct mount *nfs_mount_diskless(struct nfs_dlmount *, char *, int); 77 78 /* 79 * nfs vfs operations. 80 */ 81 const struct vfsops nfs_vfsops = { 82 nfs_mount, 83 nfs_start, 84 nfs_unmount, 85 nfs_root, 86 nfs_quotactl, 87 nfs_statfs, 88 nfs_sync, 89 nfs_vget, 90 nfs_fhtovp, 91 nfs_vptofh, 92 nfs_vfs_init, 93 nfs_sysctl, 94 nfs_checkexp 95 }; 96 97 /* 98 * nfs statfs call 99 */ 100 int 101 nfs_statfs(struct mount *mp, struct statfs *sbp, struct proc *p) 102 { 103 struct vnode *vp; 104 struct nfs_statfs *sfp = NULL; 105 struct nfsm_info info; 106 u_int32_t *tl; 107 int32_t t1; 108 caddr_t cp2; 109 struct nfsmount *nmp = VFSTONFS(mp); 110 int error = 0, retattr; 111 struct ucred *cred; 112 struct nfsnode *np; 113 u_quad_t tquad; 114 115 info.nmi_v3 = (nmp->nm_flag & NFSMNT_NFSV3); 116 117 error = nfs_nget(mp, (nfsfh_t *)nmp->nm_fh, nmp->nm_fhsize, &np); 118 if (error) 119 return (error); 120 vp = NFSTOV(np); 121 cred = crget(); 122 cred->cr_ngroups = 0; 123 if (info.nmi_v3 && (nmp->nm_flag & NFSMNT_GOTFSINFO) == 0) 124 (void)nfs_fsinfo(nmp, vp, cred, p); 125 nfsstats.rpccnt[NFSPROC_FSSTAT]++; 126 info.nmi_mb = info.nmi_mreq = nfsm_reqhead(NFSX_FH(info.nmi_v3)); 127 nfsm_fhtom(&info, vp, info.nmi_v3); 128 129 info.nmi_procp = p; 130 info.nmi_cred = cred; 131 error = nfs_request(vp, NFSPROC_FSSTAT, &info); 132 if (info.nmi_v3) 133 nfsm_postop_attr(vp, retattr); 134 if (error) { 135 m_freem(info.nmi_mrep); 136 goto nfsmout; 137 } 138 139 nfsm_dissect(sfp, struct nfs_statfs *, NFSX_STATFS(info.nmi_v3)); 140 sbp->f_iosize = min(nmp->nm_rsize, nmp->nm_wsize); 141 if (info.nmi_v3) { 142 sbp->f_bsize = NFS_FABLKSIZE; 143 tquad = fxdr_hyper(&sfp->sf_tbytes); 144 sbp->f_blocks = tquad / (u_quad_t)NFS_FABLKSIZE; 145 tquad = fxdr_hyper(&sfp->sf_fbytes); 146 sbp->f_bfree = tquad / (u_quad_t)NFS_FABLKSIZE; 147 tquad = fxdr_hyper(&sfp->sf_abytes); 148 sbp->f_bavail = (quad_t)tquad / (quad_t)NFS_FABLKSIZE; 149 150 tquad = fxdr_hyper(&sfp->sf_tfiles); 151 sbp->f_files = tquad; 152 tquad = fxdr_hyper(&sfp->sf_ffiles); 153 sbp->f_ffree = tquad; 154 sbp->f_favail = tquad; 155 sbp->f_namemax = MAXNAMLEN; 156 } else { 157 sbp->f_bsize = fxdr_unsigned(int32_t, sfp->sf_bsize); 158 sbp->f_blocks = fxdr_unsigned(int32_t, sfp->sf_blocks); 159 sbp->f_bfree = fxdr_unsigned(int32_t, sfp->sf_bfree); 160 sbp->f_bavail = fxdr_unsigned(int32_t, sfp->sf_bavail); 161 sbp->f_files = 0; 162 sbp->f_ffree = 0; 163 } 164 copy_statfs_info(sbp, mp); 165 m_freem(info.nmi_mrep); 166 nfsmout: 167 vrele(vp); 168 crfree(cred); 169 return (error); 170 } 171 172 /* 173 * nfs version 3 fsinfo rpc call 174 */ 175 int 176 nfs_fsinfo(struct nfsmount *nmp, struct vnode *vp, struct ucred *cred, 177 struct proc *p) 178 { 179 struct nfsv3_fsinfo *fsp; 180 struct nfsm_info info; 181 int32_t t1; 182 u_int32_t *tl, pref, max; 183 caddr_t cp2; 184 int error = 0, retattr; 185 186 nfsstats.rpccnt[NFSPROC_FSINFO]++; 187 info.nmi_mb = info.nmi_mreq = nfsm_reqhead(NFSX_FH(1)); 188 nfsm_fhtom(&info, vp, 1); 189 190 info.nmi_procp = p; 191 info.nmi_cred = cred; 192 error = nfs_request(vp, NFSPROC_FSINFO, &info); 193 194 nfsm_postop_attr(vp, retattr); 195 if (error) { 196 m_freem(info.nmi_mrep); 197 goto nfsmout; 198 } 199 200 nfsm_dissect(fsp, struct nfsv3_fsinfo *, NFSX_V3FSINFO); 201 pref = fxdr_unsigned(u_int32_t, fsp->fs_wtpref); 202 if (pref < nmp->nm_wsize) 203 nmp->nm_wsize = (pref + NFS_FABLKSIZE - 1) & 204 ~(NFS_FABLKSIZE - 1); 205 max = fxdr_unsigned(u_int32_t, fsp->fs_wtmax); 206 if (max < nmp->nm_wsize) { 207 nmp->nm_wsize = max & ~(NFS_FABLKSIZE - 1); 208 if (nmp->nm_wsize == 0) 209 nmp->nm_wsize = max; 210 } 211 pref = fxdr_unsigned(u_int32_t, fsp->fs_rtpref); 212 if (pref < nmp->nm_rsize) 213 nmp->nm_rsize = (pref + NFS_FABLKSIZE - 1) & 214 ~(NFS_FABLKSIZE - 1); 215 max = fxdr_unsigned(u_int32_t, fsp->fs_rtmax); 216 if (max < nmp->nm_rsize) { 217 nmp->nm_rsize = max & ~(NFS_FABLKSIZE - 1); 218 if (nmp->nm_rsize == 0) 219 nmp->nm_rsize = max; 220 } 221 pref = fxdr_unsigned(u_int32_t, fsp->fs_dtpref); 222 if (pref < nmp->nm_readdirsize) 223 nmp->nm_readdirsize = (pref + NFS_DIRBLKSIZ - 1) & 224 ~(NFS_DIRBLKSIZ - 1); 225 if (max < nmp->nm_readdirsize) { 226 nmp->nm_readdirsize = max & ~(NFS_DIRBLKSIZ - 1); 227 if (nmp->nm_readdirsize == 0) 228 nmp->nm_readdirsize = max; 229 } 230 nmp->nm_flag |= NFSMNT_GOTFSINFO; 231 232 m_freem(info.nmi_mrep); 233 nfsmout: 234 return (error); 235 } 236 237 struct nfs_diskless nfs_diskless; 238 239 /* 240 * Mount a remote root fs via. NFS. It goes like this: 241 * - Call nfs_boot_init() to fill in the nfs_diskless struct 242 * (using RARP, bootparam RPC, mountd RPC) 243 * - hand craft the swap nfs vnode hanging off a fake mount point 244 * if swdevt[0].sw_dev == NODEV 245 * - build the rootfs mount point and call mountnfs() to do the rest. 246 */ 247 int 248 nfs_mountroot(void) 249 { 250 struct vattr attr; 251 struct mount *mp; 252 struct vnode *vp; 253 struct proc *procp; 254 long n; 255 int error; 256 257 procp = curproc; /* XXX */ 258 259 /* 260 * Call nfs_boot_init() to fill in the nfs_diskless struct. 261 * Side effect: Finds and configures a network interface. 262 */ 263 nfs_boot_init(&nfs_diskless, procp); 264 265 /* 266 * Create the root mount point. 267 */ 268 if (nfs_boot_getfh(&nfs_diskless.nd_boot, "root", &nfs_diskless.nd_root, -1)) 269 panic("nfs_mountroot: root"); 270 mp = nfs_mount_diskless(&nfs_diskless.nd_root, "/", 0); 271 nfs_root(mp, &rootvp); 272 printf("root on %s\n", nfs_diskless.nd_root.ndm_host); 273 274 /* 275 * Link it into the mount list. 276 */ 277 TAILQ_INSERT_TAIL(&mountlist, mp, mnt_list); 278 vfs_unbusy(mp); 279 280 /* Get root attributes (for the time). */ 281 error = VOP_GETATTR(rootvp, &attr, procp->p_ucred, procp); 282 if (error) panic("nfs_mountroot: getattr for root"); 283 n = attr.va_atime.tv_sec; 284 #ifdef DEBUG 285 printf("root time: 0x%lx\n", n); 286 #endif 287 inittodr(n); 288 289 #ifdef notyet 290 /* Set up swap credentials. */ 291 proc0.p_ucred->cr_uid = ntohl(nfs_diskless.swap_ucred.cr_uid); 292 proc0.p_ucred->cr_gid = ntohl(nfs_diskless.swap_ucred.cr_gid); 293 if ((proc0.p_ucred->cr_ngroups = ntohs(nfs_diskless.swap_ucred.cr_ngroups)) > 294 NGROUPS) 295 proc0.p_ucred->cr_ngroups = NGROUPS; 296 for (i = 0; i < proc0.p_ucred->cr_ngroups; i++) 297 proc0.p_ucred->cr_groups[i] = ntohl(nfs_diskless.swap_ucred.cr_groups[i]); 298 #endif 299 300 /* 301 * "Mount" the swap device. 302 * 303 * On a "dataless" configuration (swap on disk) we will have: 304 * (swdevt[0].sw_dev != NODEV) identifying the swap device. 305 */ 306 if (swdevt[0].sw_dev != NODEV) { 307 if (bdevvp(swapdev, &swapdev_vp)) 308 panic("nfs_mountroot: can't setup swap vp"); 309 printf("swap on device 0x%x\n", swdevt[0].sw_dev); 310 return (0); 311 } 312 313 /* 314 * If swapping to an nfs node: (swdevt[0].sw_dev == NODEV) 315 * Create a fake mount point just for the swap vnode so that the 316 * swap file can be on a different server from the rootfs. 317 * 318 * Wait 5 retries, finally no swap is cool. -mickey 319 */ 320 error = nfs_boot_getfh(&nfs_diskless.nd_boot, "swap", &nfs_diskless.nd_swap, 5); 321 if (!error) { 322 mp = nfs_mount_diskless(&nfs_diskless.nd_swap, "/swap", 0); 323 nfs_root(mp, &vp); 324 vfs_unbusy(mp); 325 326 /* 327 * Since the swap file is not the root dir of a file system, 328 * hack it to a regular file. 329 */ 330 vp->v_type = VREG; 331 vp->v_flag = 0; 332 333 /* 334 * Next line is a hack to make swapmount() work on NFS 335 * swap files. 336 */ 337 swdevt[0].sw_dev = NETDEV; 338 /* end hack */ 339 nfs_diskless.sw_vp = vp; 340 341 /* 342 * Find out how large the swap file is. 343 */ 344 error = VOP_GETATTR(vp, &attr, procp->p_ucred, procp); 345 if (error) 346 printf("nfs_mountroot: getattr for swap\n"); 347 n = (long) (attr.va_size >> DEV_BSHIFT); 348 349 printf("swap on %s\n", nfs_diskless.nd_swap.ndm_host); 350 #ifdef DEBUG 351 printf("swap size: 0x%lx (blocks)\n", n); 352 #endif 353 return (0); 354 } 355 356 printf("WARNING: no swap\n"); 357 swdevt[0].sw_dev = NODEV; 358 return (0); 359 } 360 361 /* 362 * Internal version of mount system call for diskless setup. 363 */ 364 struct mount * 365 nfs_mount_diskless(struct nfs_dlmount *ndmntp, char *mntname, int mntflag) 366 { 367 struct mount *mp; 368 struct mbuf *m; 369 int error; 370 371 if (vfs_rootmountalloc("nfs", mntname, &mp)) 372 panic("nfs_mount_diskless: vfs_rootmountalloc failed"); 373 mp->mnt_flag |= mntflag; 374 375 /* Get mbuf for server sockaddr. */ 376 m = m_get(M_WAIT, MT_SONAME); 377 bcopy((caddr_t)ndmntp->ndm_args.addr, mtod(m, caddr_t), 378 (m->m_len = ndmntp->ndm_args.addr->sa_len)); 379 380 error = mountnfs(&ndmntp->ndm_args, mp, m, mntname, 381 ndmntp->ndm_args.hostname); 382 if (error) 383 panic("nfs_mountroot: mount %s failed: %d", mntname, error); 384 385 return (mp); 386 } 387 388 void 389 nfs_decode_args(struct nfsmount *nmp, struct nfs_args *argp, 390 struct nfs_args *nargp) 391 { 392 int s; 393 int adjsock = 0; 394 int maxio; 395 396 s = splsoftnet(); 397 398 #if 0 399 /* Re-bind if rsrvd port requested and wasn't on one */ 400 adjsock = !(nmp->nm_flag & NFSMNT_RESVPORT) 401 && (argp->flags & NFSMNT_RESVPORT); 402 #endif 403 /* Also re-bind if we're switching to/from a connected UDP socket */ 404 adjsock |= ((nmp->nm_flag & NFSMNT_NOCONN) != 405 (argp->flags & NFSMNT_NOCONN)); 406 407 /* Update flags atomically. Don't change the lock bits. */ 408 nmp->nm_flag = 409 (argp->flags & ~NFSMNT_INTERNAL) | (nmp->nm_flag & NFSMNT_INTERNAL); 410 splx(s); 411 412 if ((argp->flags & NFSMNT_TIMEO) && argp->timeo > 0) { 413 nmp->nm_timeo = (argp->timeo * NFS_HZ + 5) / 10; 414 if (nmp->nm_timeo < NFS_MINTIMEO) 415 nmp->nm_timeo = NFS_MINTIMEO; 416 else if (nmp->nm_timeo > NFS_MAXTIMEO) 417 nmp->nm_timeo = NFS_MAXTIMEO; 418 } 419 420 if ((argp->flags & NFSMNT_RETRANS) && argp->retrans > 1) 421 nmp->nm_retry = MIN(argp->retrans, NFS_MAXREXMIT); 422 if (!(nmp->nm_flag & NFSMNT_SOFT)) 423 nmp->nm_retry = NFS_MAXREXMIT + 1; /* past clip limit */ 424 425 if (argp->flags & NFSMNT_NFSV3) { 426 if (argp->sotype == SOCK_DGRAM) 427 maxio = NFS_MAXDGRAMDATA; 428 else 429 maxio = NFS_MAXDATA; 430 } else 431 maxio = NFS_V2MAXDATA; 432 433 if ((argp->flags & NFSMNT_WSIZE) && argp->wsize > 0) { 434 int osize = nmp->nm_wsize; 435 nmp->nm_wsize = argp->wsize; 436 /* Round down to multiple of blocksize */ 437 nmp->nm_wsize &= ~(NFS_FABLKSIZE - 1); 438 if (nmp->nm_wsize <= 0) 439 nmp->nm_wsize = NFS_FABLKSIZE; 440 adjsock |= (nmp->nm_wsize != osize); 441 } 442 if (nmp->nm_wsize > maxio) 443 nmp->nm_wsize = maxio; 444 if (nmp->nm_wsize > MAXBSIZE) 445 nmp->nm_wsize = MAXBSIZE; 446 447 if ((argp->flags & NFSMNT_RSIZE) && argp->rsize > 0) { 448 int osize = nmp->nm_rsize; 449 nmp->nm_rsize = argp->rsize; 450 /* Round down to multiple of blocksize */ 451 nmp->nm_rsize &= ~(NFS_FABLKSIZE - 1); 452 if (nmp->nm_rsize <= 0) 453 nmp->nm_rsize = NFS_FABLKSIZE; 454 adjsock |= (nmp->nm_rsize != osize); 455 } 456 if (nmp->nm_rsize > maxio) 457 nmp->nm_rsize = maxio; 458 if (nmp->nm_rsize > MAXBSIZE) 459 nmp->nm_rsize = MAXBSIZE; 460 461 if ((argp->flags & NFSMNT_READDIRSIZE) && argp->readdirsize > 0) { 462 nmp->nm_readdirsize = argp->readdirsize; 463 /* Round down to multiple of blocksize */ 464 nmp->nm_readdirsize &= ~(NFS_DIRBLKSIZ - 1); 465 if (nmp->nm_readdirsize < NFS_DIRBLKSIZ) 466 nmp->nm_readdirsize = NFS_DIRBLKSIZ; 467 } else if (argp->flags & NFSMNT_RSIZE) 468 nmp->nm_readdirsize = nmp->nm_rsize; 469 470 if (nmp->nm_readdirsize > maxio) 471 nmp->nm_readdirsize = maxio; 472 473 if ((argp->flags & NFSMNT_MAXGRPS) && argp->maxgrouplist >= 0 && 474 argp->maxgrouplist <= NFS_MAXGRPS) 475 nmp->nm_numgrps = argp->maxgrouplist; 476 if ((argp->flags & NFSMNT_READAHEAD) && argp->readahead >= 0 && 477 argp->readahead <= NFS_MAXRAHEAD) 478 nmp->nm_readahead = argp->readahead; 479 if (argp->flags & NFSMNT_ACREGMIN && argp->acregmin >= 0) { 480 if (argp->acregmin > 0xffff) 481 nmp->nm_acregmin = 0xffff; 482 else 483 nmp->nm_acregmin = argp->acregmin; 484 } 485 if (argp->flags & NFSMNT_ACREGMAX && argp->acregmax >= 0) { 486 if (argp->acregmax > 0xffff) 487 nmp->nm_acregmax = 0xffff; 488 else 489 nmp->nm_acregmax = argp->acregmax; 490 } 491 if (nmp->nm_acregmin > nmp->nm_acregmax) 492 nmp->nm_acregmin = nmp->nm_acregmax; 493 494 if (argp->flags & NFSMNT_ACDIRMIN && argp->acdirmin >= 0) { 495 if (argp->acdirmin > 0xffff) 496 nmp->nm_acdirmin = 0xffff; 497 else 498 nmp->nm_acdirmin = argp->acdirmin; 499 } 500 if (argp->flags & NFSMNT_ACDIRMAX && argp->acdirmax >= 0) { 501 if (argp->acdirmax > 0xffff) 502 nmp->nm_acdirmax = 0xffff; 503 else 504 nmp->nm_acdirmax = argp->acdirmax; 505 } 506 if (nmp->nm_acdirmin > nmp->nm_acdirmax) 507 nmp->nm_acdirmin = nmp->nm_acdirmax; 508 509 if (nmp->nm_so && adjsock) { 510 nfs_disconnect(nmp); 511 if (nmp->nm_sotype == SOCK_DGRAM) 512 while (nfs_connect(nmp, NULL)) { 513 printf("nfs_args: retrying connect\n"); 514 (void) tsleep((caddr_t)&lbolt, 515 PSOCK, "nfscon", 0); 516 } 517 } 518 519 /* Update nargp based on nmp */ 520 nargp->wsize = nmp->nm_wsize; 521 nargp->rsize = nmp->nm_rsize; 522 nargp->readdirsize = nmp->nm_readdirsize; 523 nargp->timeo = nmp->nm_timeo; 524 nargp->retrans = nmp->nm_retry; 525 nargp->maxgrouplist = nmp->nm_numgrps; 526 nargp->readahead = nmp->nm_readahead; 527 nargp->acregmin = nmp->nm_acregmin; 528 nargp->acregmax = nmp->nm_acregmax; 529 nargp->acdirmin = nmp->nm_acdirmin; 530 nargp->acdirmax = nmp->nm_acdirmax; 531 } 532 533 /* 534 * VFS Operations. 535 * 536 * mount system call 537 * It seems a bit dumb to copyinstr() the host here and then 538 * bcopy() it in mountnfs(), but I wanted to detect errors before 539 * doing the sockargs() call because sockargs() allocates an mbuf and 540 * an error after that means that I have to release the mbuf. 541 */ 542 /* ARGSUSED */ 543 int 544 nfs_mount(struct mount *mp, const char *path, void *data, 545 struct nameidata *ndp, struct proc *p) 546 { 547 int error; 548 struct nfs_args args; 549 struct mbuf *nam; 550 char hst[MNAMELEN]; 551 size_t len; 552 u_char nfh[NFSX_V3FHMAX]; 553 554 error = copyin(data, &args, sizeof(args.version)); 555 if (error) 556 return (error); 557 if (args.version == 3) { 558 error = copyin (data, (caddr_t)&args, 559 sizeof (struct nfs_args3)); 560 args.flags &= ~(NFSMNT_INTERNAL|NFSMNT_NOAC); 561 } else if (args.version == NFS_ARGSVERSION) { 562 error = copyin(data, (caddr_t)&args, sizeof (struct nfs_args)); 563 args.flags &= ~NFSMNT_NOAC; /* XXX - compatibility */ 564 } else 565 return (EPROGMISMATCH); 566 if (error) 567 return (error); 568 569 if ((args.flags & (NFSMNT_NFSV3|NFSMNT_RDIRPLUS)) == NFSMNT_RDIRPLUS) 570 return (EINVAL); 571 572 if (nfs_niothreads < 0) { 573 nfs_niothreads = 4; 574 nfs_getset_niothreads(1); 575 } 576 577 if (mp->mnt_flag & MNT_UPDATE) { 578 struct nfsmount *nmp = VFSTONFS(mp); 579 580 if (nmp == NULL) 581 return (EIO); 582 /* 583 * When doing an update, we can't change from or to 584 * v3. 585 */ 586 args.flags = (args.flags & ~(NFSMNT_NFSV3)) | 587 (nmp->nm_flag & (NFSMNT_NFSV3)); 588 nfs_decode_args(nmp, &args, &mp->mnt_stat.mount_info.nfs_args); 589 return (0); 590 } 591 if (args.fhsize < 0 || args.fhsize > NFSX_V3FHMAX) 592 return (EINVAL); 593 error = copyin((caddr_t)args.fh, (caddr_t)nfh, args.fhsize); 594 if (error) 595 return (error); 596 error = copyinstr(args.hostname, hst, MNAMELEN-1, &len); 597 if (error) 598 return (error); 599 memset(&hst[len], 0, MNAMELEN - len); 600 /* sockargs() call must be after above copyin() calls */ 601 error = sockargs(&nam, args.addr, args.addrlen, MT_SONAME); 602 if (error) 603 return (error); 604 args.fh = nfh; 605 error = mountnfs(&args, mp, nam, path, hst); 606 return (error); 607 } 608 609 /* 610 * Common code for mount and mountroot 611 */ 612 int 613 mountnfs(struct nfs_args *argp, struct mount *mp, struct mbuf *nam, 614 const char *pth, char *hst) 615 { 616 struct nfsmount *nmp; 617 int error; 618 619 if (mp->mnt_flag & MNT_UPDATE) { 620 nmp = VFSTONFS(mp); 621 /* update paths, file handles, etc, here XXX */ 622 m_freem(nam); 623 return (0); 624 } else { 625 nmp = malloc(sizeof(*nmp), M_NFSMNT, 626 M_WAITOK|M_ZERO); 627 mp->mnt_data = (qaddr_t)nmp; 628 } 629 630 vfs_getnewfsid(mp); 631 nmp->nm_mountp = mp; 632 nmp->nm_timeo = NFS_TIMEO; 633 nmp->nm_retry = NFS_RETRANS; 634 nmp->nm_wsize = NFS_WSIZE; 635 nmp->nm_rsize = NFS_RSIZE; 636 nmp->nm_readdirsize = NFS_READDIRSIZE; 637 nmp->nm_numgrps = NFS_MAXGRPS; 638 nmp->nm_readahead = NFS_DEFRAHEAD; 639 nmp->nm_fhsize = argp->fhsize; 640 nmp->nm_acregmin = NFS_MINATTRTIMO; 641 nmp->nm_acregmax = NFS_MAXATTRTIMO; 642 nmp->nm_acdirmin = NFS_MINATTRTIMO; 643 nmp->nm_acdirmax = NFS_MAXATTRTIMO; 644 bcopy((caddr_t)argp->fh, (caddr_t)nmp->nm_fh, argp->fhsize); 645 strncpy(&mp->mnt_stat.f_fstypename[0], mp->mnt_vfc->vfc_name, MFSNAMELEN); 646 bcopy(pth, mp->mnt_stat.f_mntonname, MNAMELEN); 647 bcopy(hst, mp->mnt_stat.f_mntfromname, MNAMELEN); 648 bcopy(hst, mp->mnt_stat.f_mntfromspec, MNAMELEN); 649 bcopy(argp, &mp->mnt_stat.mount_info.nfs_args, sizeof(*argp)); 650 nmp->nm_nam = nam; 651 nfs_decode_args(nmp, argp, &mp->mnt_stat.mount_info.nfs_args); 652 653 RB_INIT(&nmp->nm_ntree); 654 TAILQ_INIT(&nmp->nm_reqsq); 655 timeout_set(&nmp->nm_rtimeout, nfs_timer, nmp); 656 657 /* Set up the sockets and per-host congestion */ 658 nmp->nm_sotype = argp->sotype; 659 nmp->nm_soproto = argp->proto; 660 661 /* 662 * For Connection based sockets (TCP,...) defer the connect until 663 * the first request, in case the server is not responding. 664 */ 665 if (nmp->nm_sotype == SOCK_DGRAM && 666 (error = nfs_connect(nmp, NULL))) 667 goto bad; 668 669 /* 670 * This is silly, but it has to be set so that vinifod() works. 671 * We do not want to do an nfs_statfs() here since we can get 672 * stuck on a dead server and we are holding a lock on the mount 673 * point. 674 */ 675 mp->mnt_stat.f_iosize = NFS_MAXDGRAMDATA; 676 677 return (0); 678 bad: 679 nfs_disconnect(nmp); 680 free(nmp, M_NFSMNT, sizeof(*nmp)); 681 m_freem(nam); 682 return (error); 683 } 684 685 /* unmount system call */ 686 int 687 nfs_unmount(struct mount *mp, int mntflags, struct proc *p) 688 { 689 struct nfsmount *nmp; 690 int error, flags; 691 692 nmp = VFSTONFS(mp); 693 flags = 0; 694 695 if (mntflags & MNT_FORCE) 696 flags |= FORCECLOSE; 697 698 error = vflush(mp, NULL, flags); 699 if (error) 700 return (error); 701 702 nfs_disconnect(nmp); 703 m_freem(nmp->nm_nam); 704 timeout_del(&nmp->nm_rtimeout); 705 free(nmp, M_NFSMNT, sizeof(*nmp)); 706 return (0); 707 } 708 709 /* 710 * Return root of a filesystem 711 */ 712 int 713 nfs_root(struct mount *mp, struct vnode **vpp) 714 { 715 struct nfsmount *nmp; 716 struct nfsnode *np; 717 int error; 718 719 nmp = VFSTONFS(mp); 720 error = nfs_nget(mp, (nfsfh_t *)nmp->nm_fh, nmp->nm_fhsize, &np); 721 if (error) 722 return (error); 723 *vpp = NFSTOV(np); 724 return (0); 725 } 726 727 /* 728 * Flush out the buffer cache 729 */ 730 int 731 nfs_sync(struct mount *mp, int waitfor, struct ucred *cred, struct proc *p) 732 { 733 struct vnode *vp; 734 int error, allerror = 0; 735 736 /* 737 * Don't traverse the vnode list if we want to skip all of them. 738 */ 739 if (waitfor == MNT_LAZY) 740 return (allerror); 741 742 /* 743 * Force stale buffer cache information to be flushed. 744 */ 745 loop: 746 LIST_FOREACH(vp, &mp->mnt_vnodelist, v_mntvnodes) { 747 /* 748 * If the vnode that we are about to sync is no longer 749 * associated with this mount point, start over. 750 */ 751 if (vp->v_mount != mp) 752 goto loop; 753 if (VOP_ISLOCKED(vp) || LIST_FIRST(&vp->v_dirtyblkhd) == NULL) 754 continue; 755 if (vget(vp, LK_EXCLUSIVE, p)) 756 goto loop; 757 error = VOP_FSYNC(vp, cred, waitfor, p); 758 if (error) 759 allerror = error; 760 vput(vp); 761 } 762 763 return (allerror); 764 } 765 766 /* 767 * NFS flat namespace lookup. 768 * Currently unsupported. 769 */ 770 /* ARGSUSED */ 771 int 772 nfs_vget(struct mount *mp, ino_t ino, struct vnode **vpp) 773 { 774 775 return (EOPNOTSUPP); 776 } 777 778 /* 779 * Do that sysctl thang... 780 */ 781 int 782 nfs_sysctl(int *name, u_int namelen, void *oldp, size_t *oldlenp, void *newp, 783 size_t newlen, struct proc *p) 784 { 785 int rv; 786 787 /* 788 * All names at this level are terminal. 789 */ 790 if(namelen > 1) 791 return ENOTDIR; /* overloaded */ 792 793 switch(name[0]) { 794 case NFS_NFSSTATS: 795 if(!oldp) { 796 *oldlenp = sizeof nfsstats; 797 return 0; 798 } 799 800 if(*oldlenp < sizeof nfsstats) { 801 *oldlenp = sizeof nfsstats; 802 return ENOMEM; 803 } 804 805 rv = copyout(&nfsstats, oldp, sizeof nfsstats); 806 if(rv) return rv; 807 808 if(newp && newlen != sizeof nfsstats) 809 return EINVAL; 810 811 if(newp) { 812 return copyin(newp, &nfsstats, sizeof nfsstats); 813 } 814 return 0; 815 816 case NFS_NIOTHREADS: 817 nfs_getset_niothreads(0); 818 819 rv = sysctl_int(oldp, oldlenp, newp, newlen, &nfs_niothreads); 820 if (newp) 821 nfs_getset_niothreads(1); 822 823 return rv; 824 825 default: 826 return EOPNOTSUPP; 827 } 828 } 829 830 831 /* 832 * At this point, this should never happen 833 */ 834 /* ARGSUSED */ 835 int 836 nfs_fhtovp(struct mount *mp, struct fid *fhp, struct vnode **vpp) 837 { 838 return (EINVAL); 839 } 840 841 /* 842 * Vnode pointer to File handle, should never happen either 843 */ 844 /* ARGSUSED */ 845 int 846 nfs_vptofh(struct vnode *vp, struct fid *fhp) 847 { 848 return (EINVAL); 849 } 850 851 /* 852 * Vfs start routine, a no-op. 853 */ 854 /* ARGSUSED */ 855 int 856 nfs_start(struct mount *mp, int flags, struct proc *p) 857 { 858 return (0); 859 } 860 861 /* 862 * Do operations associated with quotas, not supported 863 */ 864 /* ARGSUSED */ 865 int 866 nfs_quotactl(struct mount *mp, int cmd, uid_t uid, caddr_t arg, struct proc *p) 867 { 868 return (EOPNOTSUPP); 869 } 870 871 /* 872 * check export permission, not supported 873 */ 874 /* ARGUSED */ 875 int 876 nfs_checkexp(struct mount *mp, struct mbuf *nam, int *exflagsp, 877 struct ucred **credanonp) 878 { 879 return (EOPNOTSUPP); 880 } 881 882