1 /* $OpenBSD: nfs_vfsops.c,v 1.93 2011/07/09 00:24:44 beck 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/buf.h> 48 #include <sys/mbuf.h> 49 #include <sys/dirent.h> 50 #include <sys/socket.h> 51 #include <sys/socketvar.h> 52 #include <sys/systm.h> 53 #include <sys/sysctl.h> 54 #include <sys/queue.h> 55 56 #include <net/if.h> 57 #include <net/route.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 /* 238 * Mount a remote root fs via. NFS. It goes like this: 239 * - Call nfs_boot_init() to fill in the nfs_diskless struct 240 * (using RARP, bootparam RPC, mountd RPC) 241 * - hand craft the swap nfs vnode hanging off a fake mount point 242 * if swdevt[0].sw_dev == NODEV 243 * - build the rootfs mount point and call mountnfs() to do the rest. 244 */ 245 int 246 nfs_mountroot(void) 247 { 248 struct nfs_diskless nd; 249 struct vattr attr; 250 struct mount *mp; 251 struct vnode *vp; 252 struct proc *procp; 253 long n; 254 int error; 255 256 procp = curproc; /* XXX */ 257 258 /* 259 * Call nfs_boot_init() to fill in the nfs_diskless struct. 260 * Side effect: Finds and configures a network interface. 261 */ 262 bzero((caddr_t) &nd, sizeof(nd)); 263 nfs_boot_init(&nd, procp); 264 265 /* 266 * Create the root mount point. 267 */ 268 if (nfs_boot_getfh(&nd.nd_boot, "root", &nd.nd_root, -1)) 269 panic("nfs_mountroot: root"); 270 mp = nfs_mount_diskless(&nd.nd_root, "/", 0); 271 nfs_root(mp, &rootvp); 272 printf("root on %s\n", nd.nd_root.ndm_host); 273 274 /* 275 * Link it into the mount list. 276 */ 277 CIRCLEQ_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(nd.swap_ucred.cr_uid); 292 proc0.p_ucred->cr_gid = ntohl(nd.swap_ucred.cr_gid); 293 if ((proc0.p_ucred->cr_ngroups = ntohs(nd.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(nd.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 (bdevvp(swapdev, &swapdev_vp)) 307 panic("nfs_mountroot: can't setup swap vp"); 308 if (swdevt[0].sw_dev != NODEV) { 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(&nd.nd_boot, "swap", &nd.nd_swap, 5); 321 if (!error) { 322 mp = nfs_mount_diskless(&nd.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 * XXX-smurph 337 */ 338 swdevt[0].sw_dev = NETDEV; 339 /* end hack */ 340 swdevt[0].sw_vp = vp; 341 342 /* 343 * Find out how large the swap file is. 344 */ 345 error = VOP_GETATTR(vp, &attr, procp->p_ucred, procp); 346 if (error) 347 printf("nfs_mountroot: getattr for swap\n"); 348 n = (long) (attr.va_size >> DEV_BSHIFT); 349 350 printf("swap on %s\n", nd.nd_swap.ndm_host); 351 #ifdef DEBUG 352 printf("swap size: 0x%lx (blocks)\n", n); 353 #endif 354 return (0); 355 } 356 357 printf("WARNING: no swap\n"); 358 swdevt[0].sw_dev = NODEV; 359 swdevt[0].sw_vp = NULL; 360 361 return (0); 362 } 363 364 /* 365 * Internal version of mount system call for diskless setup. 366 */ 367 struct mount * 368 nfs_mount_diskless(struct nfs_dlmount *ndmntp, char *mntname, int mntflag) 369 { 370 struct nfs_args args; 371 struct mount *mp; 372 struct mbuf *m; 373 int error; 374 375 if (vfs_rootmountalloc("nfs", mntname, &mp)) 376 panic("nfs_mount_diskless: vfs_rootmountalloc failed"); 377 mp->mnt_flag |= mntflag; 378 379 /* Initialize mount args. */ 380 bzero((caddr_t) &args, sizeof(args)); 381 args.addr = (struct sockaddr *)&ndmntp->ndm_saddr; 382 args.addrlen = args.addr->sa_len; 383 args.sotype = SOCK_DGRAM; 384 args.fh = ndmntp->ndm_fh; 385 args.fhsize = NFSX_V2FH; 386 args.hostname = ndmntp->ndm_host; 387 388 #ifdef NFS_BOOT_OPTIONS 389 args.flags |= NFS_BOOT_OPTIONS; 390 #endif 391 #ifdef NFS_BOOT_RWSIZE 392 /* 393 * Reduce rsize,wsize for interfaces that consistently 394 * drop fragments of long UDP messages. (i.e. wd8003). 395 * You can always change these later via remount. 396 */ 397 args.flags |= NFSMNT_WSIZE | NFSMNT_RSIZE; 398 args.wsize = NFS_BOOT_RWSIZE; 399 args.rsize = NFS_BOOT_RWSIZE; 400 #endif 401 402 /* Get mbuf for server sockaddr. */ 403 m = m_get(M_WAIT, MT_SONAME); 404 bcopy((caddr_t)args.addr, mtod(m, caddr_t), 405 (m->m_len = args.addr->sa_len)); 406 407 error = mountnfs(&args, mp, m, mntname, args.hostname); 408 if (error) 409 panic("nfs_mountroot: mount %s failed: %d", mntname, error); 410 411 return (mp); 412 } 413 414 void 415 nfs_decode_args(struct nfsmount *nmp, struct nfs_args *argp, 416 struct nfs_args *nargp) 417 { 418 int s; 419 int adjsock = 0; 420 int maxio; 421 422 s = splsoftnet(); 423 424 #if 0 425 /* Re-bind if rsrvd port requested and wasn't on one */ 426 adjsock = !(nmp->nm_flag & NFSMNT_RESVPORT) 427 && (argp->flags & NFSMNT_RESVPORT); 428 #endif 429 /* Also re-bind if we're switching to/from a connected UDP socket */ 430 adjsock |= ((nmp->nm_flag & NFSMNT_NOCONN) != 431 (argp->flags & NFSMNT_NOCONN)); 432 433 /* Update flags atomically. Don't change the lock bits. */ 434 nmp->nm_flag = 435 (argp->flags & ~NFSMNT_INTERNAL) | (nmp->nm_flag & NFSMNT_INTERNAL); 436 splx(s); 437 438 if ((argp->flags & NFSMNT_TIMEO) && argp->timeo > 0) { 439 nmp->nm_timeo = (argp->timeo * NFS_HZ + 5) / 10; 440 if (nmp->nm_timeo < NFS_MINTIMEO) 441 nmp->nm_timeo = NFS_MINTIMEO; 442 else if (nmp->nm_timeo > NFS_MAXTIMEO) 443 nmp->nm_timeo = NFS_MAXTIMEO; 444 } 445 446 if ((argp->flags & NFSMNT_RETRANS) && argp->retrans > 1) 447 nmp->nm_retry = MIN(argp->retrans, NFS_MAXREXMIT); 448 if (!(nmp->nm_flag & NFSMNT_SOFT)) 449 nmp->nm_retry = NFS_MAXREXMIT + 1; /* past clip limit */ 450 451 if (argp->flags & NFSMNT_NFSV3) { 452 if (argp->sotype == SOCK_DGRAM) 453 maxio = NFS_MAXDGRAMDATA; 454 else 455 maxio = NFS_MAXDATA; 456 } else 457 maxio = NFS_V2MAXDATA; 458 459 if ((argp->flags & NFSMNT_WSIZE) && argp->wsize > 0) { 460 int osize = nmp->nm_wsize; 461 nmp->nm_wsize = argp->wsize; 462 /* Round down to multiple of blocksize */ 463 nmp->nm_wsize &= ~(NFS_FABLKSIZE - 1); 464 if (nmp->nm_wsize <= 0) 465 nmp->nm_wsize = NFS_FABLKSIZE; 466 adjsock |= (nmp->nm_wsize != osize); 467 } 468 if (nmp->nm_wsize > maxio) 469 nmp->nm_wsize = maxio; 470 if (nmp->nm_wsize > MAXBSIZE) 471 nmp->nm_wsize = MAXBSIZE; 472 473 if ((argp->flags & NFSMNT_RSIZE) && argp->rsize > 0) { 474 int osize = nmp->nm_rsize; 475 nmp->nm_rsize = argp->rsize; 476 /* Round down to multiple of blocksize */ 477 nmp->nm_rsize &= ~(NFS_FABLKSIZE - 1); 478 if (nmp->nm_rsize <= 0) 479 nmp->nm_rsize = NFS_FABLKSIZE; 480 adjsock |= (nmp->nm_rsize != osize); 481 } 482 if (nmp->nm_rsize > maxio) 483 nmp->nm_rsize = maxio; 484 if (nmp->nm_rsize > MAXBSIZE) 485 nmp->nm_rsize = MAXBSIZE; 486 487 if ((argp->flags & NFSMNT_READDIRSIZE) && argp->readdirsize > 0) { 488 nmp->nm_readdirsize = argp->readdirsize; 489 /* Round down to multiple of blocksize */ 490 nmp->nm_readdirsize &= ~(NFS_DIRBLKSIZ - 1); 491 if (nmp->nm_readdirsize < NFS_DIRBLKSIZ) 492 nmp->nm_readdirsize = NFS_DIRBLKSIZ; 493 } else if (argp->flags & NFSMNT_RSIZE) 494 nmp->nm_readdirsize = nmp->nm_rsize; 495 496 if (nmp->nm_readdirsize > maxio) 497 nmp->nm_readdirsize = maxio; 498 499 if ((argp->flags & NFSMNT_MAXGRPS) && argp->maxgrouplist >= 0 && 500 argp->maxgrouplist <= NFS_MAXGRPS) 501 nmp->nm_numgrps = argp->maxgrouplist; 502 if ((argp->flags & NFSMNT_READAHEAD) && argp->readahead >= 0 && 503 argp->readahead <= NFS_MAXRAHEAD) 504 nmp->nm_readahead = argp->readahead; 505 if (argp->flags & NFSMNT_ACREGMIN && argp->acregmin >= 0) { 506 if (argp->acregmin > 0xffff) 507 nmp->nm_acregmin = 0xffff; 508 else 509 nmp->nm_acregmin = argp->acregmin; 510 } 511 if (argp->flags & NFSMNT_ACREGMAX && argp->acregmax >= 0) { 512 if (argp->acregmax > 0xffff) 513 nmp->nm_acregmax = 0xffff; 514 else 515 nmp->nm_acregmax = argp->acregmax; 516 } 517 if (nmp->nm_acregmin > nmp->nm_acregmax) 518 nmp->nm_acregmin = nmp->nm_acregmax; 519 520 if (argp->flags & NFSMNT_ACDIRMIN && argp->acdirmin >= 0) { 521 if (argp->acdirmin > 0xffff) 522 nmp->nm_acdirmin = 0xffff; 523 else 524 nmp->nm_acdirmin = argp->acdirmin; 525 } 526 if (argp->flags & NFSMNT_ACDIRMAX && argp->acdirmax >= 0) { 527 if (argp->acdirmax > 0xffff) 528 nmp->nm_acdirmax = 0xffff; 529 else 530 nmp->nm_acdirmax = argp->acdirmax; 531 } 532 if (nmp->nm_acdirmin > nmp->nm_acdirmax) 533 nmp->nm_acdirmin = nmp->nm_acdirmax; 534 535 if (nmp->nm_so && adjsock) { 536 nfs_disconnect(nmp); 537 if (nmp->nm_sotype == SOCK_DGRAM) 538 while (nfs_connect(nmp, NULL)) { 539 printf("nfs_args: retrying connect\n"); 540 (void) tsleep((caddr_t)&lbolt, 541 PSOCK, "nfscon", 0); 542 } 543 } 544 545 /* Update nargp based on nmp */ 546 nargp->wsize = nmp->nm_wsize; 547 nargp->rsize = nmp->nm_rsize; 548 nargp->readdirsize = nmp->nm_readdirsize; 549 nargp->timeo = nmp->nm_timeo; 550 nargp->retrans = nmp->nm_retry; 551 nargp->maxgrouplist = nmp->nm_numgrps; 552 nargp->readahead = nmp->nm_readahead; 553 nargp->acregmin = nmp->nm_acregmin; 554 nargp->acregmax = nmp->nm_acregmax; 555 nargp->acdirmin = nmp->nm_acdirmin; 556 nargp->acdirmax = nmp->nm_acdirmax; 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 * bcopy() 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(struct mount *mp, const char *path, void *data, 571 struct nameidata *ndp, struct proc *p) 572 { 573 int error; 574 struct nfs_args args; 575 struct mbuf *nam; 576 char pth[MNAMELEN], hst[MNAMELEN]; 577 size_t len; 578 u_char nfh[NFSX_V3FHMAX]; 579 580 error = copyin (data, &args, sizeof (args.version)); 581 if (error) 582 return (error); 583 if (args.version == 3) { 584 error = copyin (data, (caddr_t)&args, 585 sizeof (struct nfs_args3)); 586 args.flags &= ~(NFSMNT_INTERNAL|NFSMNT_NOAC); 587 } 588 else if (args.version == NFS_ARGSVERSION) { 589 error = copyin(data, (caddr_t)&args, sizeof (struct nfs_args)); 590 args.flags &= ~NFSMNT_NOAC; /* XXX - compatibility */ 591 } 592 else 593 return (EPROGMISMATCH); 594 if (error) 595 return (error); 596 597 if ((args.flags & (NFSMNT_NFSV3|NFSMNT_RDIRPLUS)) == NFSMNT_RDIRPLUS) 598 return (EINVAL); 599 600 if (nfs_niothreads < 0) { 601 nfs_niothreads = 4; 602 nfs_getset_niothreads(1); 603 } 604 605 if (mp->mnt_flag & MNT_UPDATE) { 606 struct nfsmount *nmp = VFSTONFS(mp); 607 608 if (nmp == NULL) 609 return (EIO); 610 /* 611 * When doing an update, we can't change from or to 612 * v3. 613 */ 614 args.flags = (args.flags & ~(NFSMNT_NFSV3)) | 615 (nmp->nm_flag & (NFSMNT_NFSV3)); 616 nfs_decode_args(nmp, &args, &mp->mnt_stat.mount_info.nfs_args); 617 return (0); 618 } 619 if (args.fhsize < 0 || args.fhsize > NFSX_V3FHMAX) 620 return (EINVAL); 621 error = copyin((caddr_t)args.fh, (caddr_t)nfh, args.fhsize); 622 if (error) 623 return (error); 624 error = copyinstr(path, pth, MNAMELEN-1, &len); 625 if (error) 626 return (error); 627 bzero(&pth[len], MNAMELEN - len); 628 error = copyinstr(args.hostname, hst, MNAMELEN-1, &len); 629 if (error) 630 return (error); 631 bzero(&hst[len], MNAMELEN - len); 632 /* sockargs() call must be after above copyin() calls */ 633 error = sockargs(&nam, args.addr, args.addrlen, MT_SONAME); 634 if (error) 635 return (error); 636 args.fh = nfh; 637 error = mountnfs(&args, mp, nam, pth, hst); 638 return (error); 639 } 640 641 /* 642 * Common code for mount and mountroot 643 */ 644 int 645 mountnfs(struct nfs_args *argp, struct mount *mp, struct mbuf *nam, char *pth, 646 char *hst) 647 { 648 struct nfsmount *nmp; 649 int error; 650 651 if (mp->mnt_flag & MNT_UPDATE) { 652 nmp = VFSTONFS(mp); 653 /* update paths, file handles, etc, here XXX */ 654 m_freem(nam); 655 return (0); 656 } else { 657 nmp = malloc(sizeof(struct nfsmount), M_NFSMNT, 658 M_WAITOK|M_ZERO); 659 mp->mnt_data = (qaddr_t)nmp; 660 } 661 662 vfs_getnewfsid(mp); 663 nmp->nm_mountp = mp; 664 nmp->nm_timeo = NFS_TIMEO; 665 nmp->nm_retry = NFS_RETRANS; 666 nmp->nm_wsize = NFS_WSIZE; 667 nmp->nm_rsize = NFS_RSIZE; 668 nmp->nm_readdirsize = NFS_READDIRSIZE; 669 nmp->nm_numgrps = NFS_MAXGRPS; 670 nmp->nm_readahead = NFS_DEFRAHEAD; 671 nmp->nm_fhsize = argp->fhsize; 672 nmp->nm_acregmin = NFS_MINATTRTIMO; 673 nmp->nm_acregmax = NFS_MAXATTRTIMO; 674 nmp->nm_acdirmin = NFS_MINATTRTIMO; 675 nmp->nm_acdirmax = NFS_MAXATTRTIMO; 676 bcopy((caddr_t)argp->fh, (caddr_t)nmp->nm_fh, argp->fhsize); 677 strncpy(&mp->mnt_stat.f_fstypename[0], mp->mnt_vfc->vfc_name, MFSNAMELEN); 678 bcopy(hst, mp->mnt_stat.f_mntfromname, MNAMELEN); 679 bcopy(pth, mp->mnt_stat.f_mntonname, MNAMELEN); 680 bcopy(argp, &mp->mnt_stat.mount_info.nfs_args, sizeof(*argp)); 681 nmp->nm_nam = nam; 682 nfs_decode_args(nmp, argp, &mp->mnt_stat.mount_info.nfs_args); 683 684 RB_INIT(&nmp->nm_ntree); 685 TAILQ_INIT(&nmp->nm_reqsq); 686 timeout_set(&nmp->nm_rtimeout, nfs_timer, nmp); 687 688 /* Set up the sockets and per-host congestion */ 689 nmp->nm_sotype = argp->sotype; 690 nmp->nm_soproto = argp->proto; 691 692 /* 693 * For Connection based sockets (TCP,...) defer the connect until 694 * the first request, in case the server is not responding. 695 */ 696 if (nmp->nm_sotype == SOCK_DGRAM && 697 (error = nfs_connect(nmp, NULL))) 698 goto bad; 699 700 /* 701 * This is silly, but it has to be set so that vinifod() works. 702 * We do not want to do an nfs_statfs() here since we can get 703 * stuck on a dead server and we are holding a lock on the mount 704 * point. 705 */ 706 mp->mnt_stat.f_iosize = NFS_MAXDGRAMDATA; 707 708 return (0); 709 bad: 710 nfs_disconnect(nmp); 711 free((caddr_t)nmp, M_NFSMNT); 712 m_freem(nam); 713 return (error); 714 } 715 716 /* unmount system call */ 717 int 718 nfs_unmount(struct mount *mp, int mntflags, struct proc *p) 719 { 720 struct nfsmount *nmp; 721 int error, flags; 722 723 nmp = VFSTONFS(mp); 724 flags = 0; 725 726 if (mntflags & MNT_FORCE) 727 flags |= FORCECLOSE; 728 729 error = vflush(mp, NULL, flags); 730 if (error) 731 return (error); 732 733 nfs_disconnect(nmp); 734 m_freem(nmp->nm_nam); 735 timeout_del(&nmp->nm_rtimeout); 736 free(nmp, M_NFSMNT); 737 return (0); 738 } 739 740 /* 741 * Return root of a filesystem 742 */ 743 int 744 nfs_root(struct mount *mp, struct vnode **vpp) 745 { 746 struct nfsmount *nmp; 747 struct nfsnode *np; 748 int error; 749 750 nmp = VFSTONFS(mp); 751 error = nfs_nget(mp, (nfsfh_t *)nmp->nm_fh, nmp->nm_fhsize, &np); 752 if (error) 753 return (error); 754 *vpp = NFSTOV(np); 755 return (0); 756 } 757 758 /* 759 * Flush out the buffer cache 760 */ 761 int 762 nfs_sync(struct mount *mp, int waitfor, struct ucred *cred, struct proc *p) 763 { 764 struct vnode *vp; 765 int error, allerror = 0; 766 767 /* 768 * Don't traverse the vnode list if we want to skip all of them. 769 */ 770 if (waitfor == MNT_LAZY) 771 return (allerror); 772 773 /* 774 * Force stale buffer cache information to be flushed. 775 */ 776 loop: 777 LIST_FOREACH(vp, &mp->mnt_vnodelist, v_mntvnodes) { 778 /* 779 * If the vnode that we are about to sync is no longer 780 * associated with this mount point, start over. 781 */ 782 if (vp->v_mount != mp) 783 goto loop; 784 if (VOP_ISLOCKED(vp) || LIST_FIRST(&vp->v_dirtyblkhd) == NULL) 785 continue; 786 if (vget(vp, LK_EXCLUSIVE, p)) 787 goto loop; 788 error = VOP_FSYNC(vp, cred, waitfor, p); 789 if (error) 790 allerror = error; 791 vput(vp); 792 } 793 794 return (allerror); 795 } 796 797 /* 798 * NFS flat namespace lookup. 799 * Currently unsupported. 800 */ 801 /* ARGSUSED */ 802 int 803 nfs_vget(struct mount *mp, ino_t ino, struct vnode **vpp) 804 { 805 806 return (EOPNOTSUPP); 807 } 808 809 /* 810 * Do that sysctl thang... 811 */ 812 int 813 nfs_sysctl(int *name, u_int namelen, void *oldp, size_t *oldlenp, void *newp, 814 size_t newlen, struct proc *p) 815 { 816 int rv; 817 818 /* 819 * All names at this level are terminal. 820 */ 821 if(namelen > 1) 822 return ENOTDIR; /* overloaded */ 823 824 switch(name[0]) { 825 case NFS_NFSSTATS: 826 if(!oldp) { 827 *oldlenp = sizeof nfsstats; 828 return 0; 829 } 830 831 if(*oldlenp < sizeof nfsstats) { 832 *oldlenp = sizeof nfsstats; 833 return ENOMEM; 834 } 835 836 rv = copyout(&nfsstats, oldp, sizeof nfsstats); 837 if(rv) return rv; 838 839 if(newp && newlen != sizeof nfsstats) 840 return EINVAL; 841 842 if(newp) { 843 return copyin(newp, &nfsstats, sizeof nfsstats); 844 } 845 return 0; 846 847 case NFS_NIOTHREADS: 848 nfs_getset_niothreads(0); 849 850 rv = sysctl_int(oldp, oldlenp, newp, newlen, &nfs_niothreads); 851 if (newp) 852 nfs_getset_niothreads(1); 853 854 return rv; 855 856 default: 857 return EOPNOTSUPP; 858 } 859 } 860 861 862 /* 863 * At this point, this should never happen 864 */ 865 /* ARGSUSED */ 866 int 867 nfs_fhtovp(struct mount *mp, struct fid *fhp, struct vnode **vpp) 868 { 869 return (EINVAL); 870 } 871 872 /* 873 * Vnode pointer to File handle, should never happen either 874 */ 875 /* ARGSUSED */ 876 int 877 nfs_vptofh(struct vnode *vp, struct fid *fhp) 878 { 879 return (EINVAL); 880 } 881 882 /* 883 * Vfs start routine, a no-op. 884 */ 885 /* ARGSUSED */ 886 int 887 nfs_start(struct mount *mp, int flags, struct proc *p) 888 { 889 return (0); 890 } 891 892 /* 893 * Do operations associated with quotas, not supported 894 */ 895 /* ARGSUSED */ 896 int 897 nfs_quotactl(struct mount *mp, int cmd, uid_t uid, caddr_t arg, struct proc *p) 898 { 899 return (EOPNOTSUPP); 900 } 901 902 /* 903 * check export permission, not supported 904 */ 905 /* ARGUSED */ 906 int 907 nfs_checkexp(struct mount *mp, struct mbuf *nam, int *exflagsp, 908 struct ucred **credanonp) 909 { 910 return (EOPNOTSUPP); 911 } 912 913