1 /* 2 * Copyright (c) 1989 The Regents of the University of California. 3 * All rights reserved. 4 * 5 * This code is derived from software contributed to Berkeley by 6 * Rick Macklem at The University of Guelph. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 3. All advertising materials mentioning features or use of this software 17 * must display the following acknowledgement: 18 * This product includes software developed by the University of 19 * California, Berkeley and its contributors. 20 * 4. Neither the name of the University nor the names of its contributors 21 * may be used to endorse or promote products derived from this software 22 * without specific prior written permission. 23 * 24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 34 * SUCH DAMAGE. 35 * 36 * from: @(#)nfs_vfsops.c 7.31 (Berkeley) 5/6/91 37 * $Id: nfs_vfsops.c,v 1.2 1993/05/20 03:18:53 cgd Exp $ 38 */ 39 40 #include "param.h" 41 #include "conf.h" 42 #include "ioctl.h" 43 #include "signal.h" 44 #include "proc.h" 45 #include "namei.h" 46 #include "vnode.h" 47 #include "mount.h" 48 #include "buf.h" 49 #include "mbuf.h" 50 #include "socket.h" 51 #include "systm.h" 52 53 #include "../net/if.h" 54 #include "../net/route.h" 55 #include "../netinet/in.h" 56 57 #include "nfsv2.h" 58 #include "nfsnode.h" 59 #include "nfsmount.h" 60 #include "nfs.h" 61 #include "xdr_subs.h" 62 #include "nfsm_subs.h" 63 #include "nfsdiskless.h" 64 65 /* 66 * nfs vfs operations. 67 */ 68 struct vfsops nfs_vfsops = { 69 nfs_mount, 70 nfs_start, 71 nfs_unmount, 72 nfs_root, 73 nfs_quotactl, 74 nfs_statfs, 75 nfs_sync, 76 nfs_fhtovp, 77 nfs_vptofh, 78 nfs_init, 79 }; 80 81 static u_char nfs_mntid; 82 extern u_long nfs_procids[NFS_NPROCS]; 83 extern u_long nfs_prog, nfs_vers; 84 struct nfs_diskless nfs_diskless; 85 void nfs_disconnect(); 86 87 #define TRUE 1 88 #define FALSE 0 89 90 /* 91 * nfs statfs call 92 */ 93 nfs_statfs(mp, sbp, p) 94 struct mount *mp; 95 register struct statfs *sbp; 96 struct proc *p; 97 { 98 register struct vnode *vp; 99 register struct nfsv2_statfs *sfp; 100 register caddr_t cp; 101 register long t1; 102 caddr_t bpos, dpos, cp2; 103 u_long xid; 104 int error = 0; 105 struct mbuf *mreq, *mrep, *md, *mb, *mb2; 106 struct nfsmount *nmp; 107 struct ucred *cred; 108 struct nfsnode *np; 109 110 nmp = VFSTONFS(mp); 111 if (error = nfs_nget(mp, &nmp->nm_fh, &np)) 112 return (error); 113 vp = NFSTOV(np); 114 nfsstats.rpccnt[NFSPROC_STATFS]++; 115 cred = crget(); 116 cred->cr_ngroups = 1; 117 nfsm_reqhead(nfs_procids[NFSPROC_STATFS], cred, NFSX_FH); 118 nfsm_fhtom(vp); 119 nfsm_request(vp, NFSPROC_STATFS, p, 0); 120 nfsm_disect(sfp, struct nfsv2_statfs *, NFSX_STATFS); 121 sbp->f_type = MOUNT_NFS; 122 sbp->f_flags = nmp->nm_flag; 123 sbp->f_bsize = fxdr_unsigned(long, sfp->sf_tsize); 124 sbp->f_fsize = fxdr_unsigned(long, sfp->sf_bsize); 125 sbp->f_blocks = fxdr_unsigned(long, sfp->sf_blocks); 126 sbp->f_bfree = fxdr_unsigned(long, sfp->sf_bfree); 127 sbp->f_bavail = fxdr_unsigned(long, sfp->sf_bavail); 128 sbp->f_files = 0; 129 sbp->f_ffree = 0; 130 if (sbp != &mp->mnt_stat) { 131 bcopy(mp->mnt_stat.f_mntonname, sbp->f_mntonname, MNAMELEN); 132 bcopy(mp->mnt_stat.f_mntfromname, sbp->f_mntfromname, MNAMELEN); 133 } 134 nfsm_reqdone; 135 nfs_nput(vp); 136 crfree(cred); 137 return (error); 138 } 139 140 /* 141 * Mount a remote root fs via. nfs. This depends on the info in the 142 * nfs_diskless structure that has been filled in properly by some primary 143 * bootstrap. 144 * It goes something like this: 145 * - do enough of "ifconfig" by calling ifioctl() so that the system 146 * can talk to the server 147 * - If nfs_diskless.mygateway is filled in, use that address as 148 * a default gateway. 149 * (This is done the 4.3 way with rtioctl() and should be changed) 150 * - hand craft the swap nfs vnode hanging off a fake mount point 151 * - build the rootfs mount point and call mountnfs() to do the rest. 152 */ 153 nfs_mountroot() 154 { 155 register struct mount *mp; 156 register struct mbuf *m; 157 struct socket *so; 158 struct vnode *vp; 159 int error; 160 161 /* 162 * Do enough of ifconfig(8) so that critical net interface can 163 * talk to the server. 164 */ 165 if (socreate(nfs_diskless.myif.ifra_addr.sa_family, &so, SOCK_DGRAM, 0)) 166 panic("nfs ifconf"); 167 if (ifioctl(so, SIOCAIFADDR, &nfs_diskless.myif)) 168 panic("nfs ifconf2"); 169 soclose(so); 170 171 /* 172 * If the gateway field is filled in, set it as the default route. 173 */ 174 #ifdef COMPAT_43 175 if (nfs_diskless.mygateway.sa_family == AF_INET) { 176 struct ortentry rt; 177 struct sockaddr_in *sin; 178 179 sin = (struct sockaddr_in *) &rt.rt_dst; 180 sin->sin_len = sizeof (struct sockaddr_in); 181 sin->sin_family = AF_INET; 182 sin->sin_addr.s_addr = 0; /* default */ 183 bcopy((caddr_t)&nfs_diskless.mygateway, (caddr_t)&rt.rt_gateway, 184 sizeof (struct sockaddr_in)); 185 rt.rt_flags = (RTF_UP | RTF_GATEWAY); 186 if (rtioctl(SIOCADDRT, (caddr_t)&rt)) 187 panic("nfs root route"); 188 } 189 #endif /* COMPAT_43 */ 190 191 /* 192 * If swapping to an nfs node (indicated by swdevt[0].sw_dev == NODEV): 193 * Create a fake mount point just for the swap vnode so that the 194 * swap file can be on a different server from the rootfs. 195 */ 196 if (swdevt[0].sw_dev == NODEV) { 197 mp = (struct mount *)malloc((u_long)sizeof(struct mount), 198 M_MOUNT, M_NOWAIT); 199 if (mp == NULL) 200 panic("nfs root mount"); 201 mp->mnt_op = &nfs_vfsops; 202 mp->mnt_flag = 0; 203 mp->mnt_exroot = 0; 204 mp->mnt_mounth = NULLVP; 205 206 /* 207 * Set up the diskless nfs_args for the swap mount point 208 * and then call mountnfs() to mount it. 209 * Since the swap file is not the root dir of a file system, 210 * hack it to a regular file. 211 */ 212 nfs_diskless.swap_args.fh = (nfsv2fh_t *)nfs_diskless.swap_fh; 213 MGET(m, MT_SONAME, M_DONTWAIT); 214 if (m == NULL) 215 panic("nfs root mbuf"); 216 bcopy((caddr_t)&nfs_diskless.swap_saddr, mtod(m, caddr_t), 217 nfs_diskless.swap_saddr.sa_len); 218 m->m_len = nfs_diskless.swap_saddr.sa_len; 219 if (mountnfs(&nfs_diskless.swap_args, mp, m, "/swap", 220 nfs_diskless.swap_hostnam, &vp)) 221 panic("nfs swap"); 222 vp->v_type = VREG; 223 vp->v_flag = 0; 224 swapdev_vp = vp; 225 VREF(vp); 226 swdevt[0].sw_vp = vp; 227 } 228 229 /* 230 * Create the rootfs mount point. 231 */ 232 mp = (struct mount *)malloc((u_long)sizeof(struct mount), 233 M_MOUNT, M_NOWAIT); 234 if (mp == NULL) 235 panic("nfs root mount2"); 236 mp->mnt_op = &nfs_vfsops; 237 mp->mnt_flag = MNT_RDONLY; 238 mp->mnt_exroot = 0; 239 mp->mnt_mounth = NULLVP; 240 241 /* 242 * Set up the root fs args and call mountnfs() to do the rest. 243 */ 244 nfs_diskless.root_args.fh = (nfsv2fh_t *)nfs_diskless.root_fh; 245 MGET(m, MT_SONAME, M_DONTWAIT); 246 if (m == NULL) 247 panic("nfs root mbuf2"); 248 bcopy((caddr_t)&nfs_diskless.root_saddr, mtod(m, caddr_t), 249 nfs_diskless.root_saddr.sa_len); 250 m->m_len = nfs_diskless.root_saddr.sa_len; 251 if (mountnfs(&nfs_diskless.root_args, mp, m, "/", 252 nfs_diskless.root_hostnam, &vp)) 253 panic("nfs root"); 254 if (vfs_lock(mp)) 255 panic("nfs root2"); 256 rootfs = mp; 257 mp->mnt_next = mp; 258 mp->mnt_prev = mp; 259 mp->mnt_vnodecovered = NULLVP; 260 vfs_unlock(mp); 261 rootvp = vp; 262 inittodr((time_t)0); /* There is no time in the nfs fsstat so ?? */ 263 return (0); 264 } 265 266 /* 267 * VFS Operations. 268 * 269 * mount system call 270 * It seems a bit dumb to copyinstr() the host and path here and then 271 * bcopy() them in mountnfs(), but I wanted to detect errors before 272 * doing the sockargs() call because sockargs() allocates an mbuf and 273 * an error after that means that I have to release the mbuf. 274 */ 275 /* ARGSUSED */ 276 nfs_mount(mp, path, data, ndp, p) 277 struct mount *mp; 278 char *path; 279 caddr_t data; 280 struct nameidata *ndp; 281 struct proc *p; 282 { 283 int error; 284 struct nfs_args args; 285 struct mbuf *nam; 286 struct vnode *vp; 287 char pth[MNAMELEN], hst[MNAMELEN]; 288 u_int len; 289 nfsv2fh_t nfh; 290 291 if (mp->mnt_flag & MNT_UPDATE) 292 return (0); 293 if (error = copyin(data, (caddr_t)&args, sizeof (struct nfs_args))) 294 return (error); 295 if (error = copyin((caddr_t)args.fh, (caddr_t)&nfh, sizeof (nfsv2fh_t))) 296 return (error); 297 if (error = copyinstr(path, pth, MNAMELEN-1, &len)) 298 return (error); 299 bzero(&pth[len], MNAMELEN - len); 300 if (error = copyinstr(args.hostname, hst, MNAMELEN-1, &len)) 301 return (error); 302 bzero(&hst[len], MNAMELEN - len); 303 /* sockargs() call must be after above copyin() calls */ 304 if (error = sockargs(&nam, (caddr_t)args.addr, 305 sizeof (struct sockaddr), MT_SONAME)) 306 return (error); 307 args.fh = &nfh; 308 error = mountnfs(&args, mp, nam, pth, hst, &vp); 309 return (error); 310 } 311 312 /* 313 * Common code for mount and mountroot 314 */ 315 mountnfs(argp, mp, nam, pth, hst, vpp) 316 register struct nfs_args *argp; 317 register struct mount *mp; 318 struct mbuf *nam; 319 char *pth, *hst; 320 struct vnode **vpp; 321 { 322 register struct nfsmount *nmp; 323 struct proc *p = curproc; /* XXX */ 324 struct nfsnode *np; 325 int error; 326 fsid_t tfsid; 327 328 MALLOC(nmp, struct nfsmount *, sizeof *nmp, M_NFSMNT, M_WAITOK); 329 bzero((caddr_t)nmp, sizeof *nmp); 330 mp->mnt_data = (qaddr_t)nmp; 331 /* 332 * Generate a unique nfs mount id. The problem is that a dev number 333 * is not unique across multiple systems. The techique is as follows: 334 * 1) Set to nblkdev,0 which will never be used otherwise 335 * 2) Generate a first guess as nblkdev,nfs_mntid where nfs_mntid is 336 * NOT 0 337 * 3) Loop searching the mount list for another one with same id 338 * If a match, increment val[0] and try again 339 * NB: I increment val[0] { a long } instead of nfs_mntid { a u_char } 340 * so that nfs is not limited to 255 mount points 341 * Incrementing the high order bits does no real harm, since it 342 * simply makes the major dev number tick up. The upper bound is 343 * set to major dev 127 to avoid any sign extention problems 344 */ 345 mp->mnt_stat.f_fsid.val[0] = makedev(nblkdev, 0); 346 mp->mnt_stat.f_fsid.val[1] = MOUNT_NFS; 347 if (++nfs_mntid == 0) 348 ++nfs_mntid; 349 tfsid.val[0] = makedev(nblkdev, nfs_mntid); 350 tfsid.val[1] = MOUNT_NFS; 351 while (rootfs && getvfs(&tfsid)) { 352 tfsid.val[0]++; 353 nfs_mntid++; 354 } 355 if (major(tfsid.val[0]) > 127) { 356 error = ENOENT; 357 goto bad; 358 } 359 mp->mnt_stat.f_fsid.val[0] = tfsid.val[0]; 360 nmp->nm_mountp = mp; 361 nmp->nm_flag = argp->flags; 362 nmp->nm_rto = NFS_TIMEO; 363 nmp->nm_rtt = -1; 364 nmp->nm_rttvar = nmp->nm_rto << 1; 365 nmp->nm_retry = NFS_RETRANS; 366 nmp->nm_wsize = NFS_WSIZE; 367 nmp->nm_rsize = NFS_RSIZE; 368 bcopy((caddr_t)argp->fh, (caddr_t)&nmp->nm_fh, sizeof(nfsv2fh_t)); 369 bcopy(hst, mp->mnt_stat.f_mntfromname, MNAMELEN); 370 bcopy(pth, mp->mnt_stat.f_mntonname, MNAMELEN); 371 nmp->nm_nam = nam; 372 373 if ((argp->flags & NFSMNT_TIMEO) && argp->timeo > 0) { 374 nmp->nm_rto = argp->timeo; 375 /* NFS timeouts are specified in 1/10 sec. */ 376 nmp->nm_rto = (nmp->nm_rto * 10) / NFS_HZ; 377 if (nmp->nm_rto < NFS_MINTIMEO) 378 nmp->nm_rto = NFS_MINTIMEO; 379 else if (nmp->nm_rto > NFS_MAXTIMEO) 380 nmp->nm_rto = NFS_MAXTIMEO; 381 nmp->nm_rttvar = nmp->nm_rto << 1; 382 } 383 384 if ((argp->flags & NFSMNT_RETRANS) && argp->retrans > 1) { 385 nmp->nm_retry = argp->retrans; 386 if (nmp->nm_retry > NFS_MAXREXMIT) 387 nmp->nm_retry = NFS_MAXREXMIT; 388 } 389 390 if ((argp->flags & NFSMNT_WSIZE) && argp->wsize > 0) { 391 nmp->nm_wsize = argp->wsize; 392 /* Round down to multiple of blocksize */ 393 nmp->nm_wsize &= ~0x1ff; 394 if (nmp->nm_wsize <= 0) 395 nmp->nm_wsize = 512; 396 else if (nmp->nm_wsize > NFS_MAXDATA) 397 nmp->nm_wsize = NFS_MAXDATA; 398 } 399 if (nmp->nm_wsize > MAXBSIZE) 400 nmp->nm_wsize = MAXBSIZE; 401 402 if ((argp->flags & NFSMNT_RSIZE) && argp->rsize > 0) { 403 nmp->nm_rsize = argp->rsize; 404 /* Round down to multiple of blocksize */ 405 nmp->nm_rsize &= ~0x1ff; 406 if (nmp->nm_rsize <= 0) 407 nmp->nm_rsize = 512; 408 else if (nmp->nm_rsize > NFS_MAXDATA) 409 nmp->nm_rsize = NFS_MAXDATA; 410 } 411 if (nmp->nm_rsize > MAXBSIZE) 412 nmp->nm_rsize = MAXBSIZE; 413 /* Set up the sockets and per-host congestion */ 414 nmp->nm_sotype = argp->sotype; 415 nmp->nm_soproto = argp->proto; 416 if (error = nfs_connect(nmp)) 417 goto bad; 418 419 if (error = nfs_statfs(mp, &mp->mnt_stat, p)) 420 goto bad; 421 /* 422 * A reference count is needed on the nfsnode representing the 423 * remote root. If this object is not persistent, then backward 424 * traversals of the mount point (i.e. "..") will not work if 425 * the nfsnode gets flushed out of the cache. Ufs does not have 426 * this problem, because one can identify root inodes by their 427 * number == ROOTINO (2). 428 */ 429 if (error = nfs_nget(mp, &nmp->nm_fh, &np)) 430 goto bad; 431 /* 432 * Unlock it, but keep the reference count. 433 */ 434 nfs_unlock(NFSTOV(np)); 435 *vpp = NFSTOV(np); 436 437 return (0); 438 bad: 439 nfs_disconnect(nmp); 440 FREE(nmp, M_NFSMNT); 441 m_freem(nam); 442 return (error); 443 } 444 445 /* 446 * unmount system call 447 */ 448 nfs_unmount(mp, mntflags, p) 449 struct mount *mp; 450 int mntflags; 451 struct proc *p; 452 { 453 register struct nfsmount *nmp; 454 struct nfsnode *np; 455 struct vnode *vp; 456 int error, flags = 0; 457 extern int doforce; 458 459 if (mntflags & MNT_FORCE) { 460 if (!doforce || mp == rootfs) 461 return (EINVAL); 462 flags |= FORCECLOSE; 463 } 464 nmp = VFSTONFS(mp); 465 /* 466 * Clear out the buffer cache 467 */ 468 mntflushbuf(mp, 0); 469 if (mntinvalbuf(mp)) 470 return (EBUSY); 471 /* 472 * Goes something like this.. 473 * - Check for activity on the root vnode (other than ourselves). 474 * - Call vflush() to clear out vnodes for this file system, 475 * except for the root vnode. 476 * - Decrement reference on the vnode representing remote root. 477 * - Close the socket 478 * - Free up the data structures 479 */ 480 /* 481 * We need to decrement the ref. count on the nfsnode representing 482 * the remote root. See comment in mountnfs(). The VFS unmount() 483 * has done vput on this vnode, otherwise we would get deadlock! 484 */ 485 if (error = nfs_nget(mp, &nmp->nm_fh, &np)) 486 return(error); 487 vp = NFSTOV(np); 488 if (vp->v_usecount > 2) { 489 vput(vp); 490 return (EBUSY); 491 } 492 if (error = vflush(mp, vp, flags)) { 493 vput(vp); 494 return (error); 495 } 496 /* 497 * Get rid of two reference counts, and unlock it on the second. 498 */ 499 vrele(vp); 500 vput(vp); 501 nfs_disconnect(nmp); 502 m_freem(nmp->nm_nam); 503 free((caddr_t)nmp, M_NFSMNT); 504 return (0); 505 } 506 507 /* 508 * Return root of a filesystem 509 */ 510 nfs_root(mp, vpp) 511 struct mount *mp; 512 struct vnode **vpp; 513 { 514 register struct vnode *vp; 515 struct nfsmount *nmp; 516 struct nfsnode *np; 517 int error; 518 519 nmp = VFSTONFS(mp); 520 if (error = nfs_nget(mp, &nmp->nm_fh, &np)) 521 return (error); 522 vp = NFSTOV(np); 523 vp->v_type = VDIR; 524 vp->v_flag = VROOT; 525 *vpp = vp; 526 return (0); 527 } 528 529 extern int syncprt; 530 531 /* 532 * Flush out the buffer cache 533 */ 534 /* ARGSUSED */ 535 nfs_sync(mp, waitfor) 536 struct mount *mp; 537 int waitfor; 538 { 539 if (syncprt) 540 bufstats(); 541 /* 542 * Force stale buffer cache information to be flushed. 543 */ 544 mntflushbuf(mp, waitfor == MNT_WAIT ? B_SYNC : 0); 545 return (0); 546 } 547 548 /* 549 * At this point, this should never happen 550 */ 551 /* ARGSUSED */ 552 nfs_fhtovp(mp, fhp, vpp) 553 struct mount *mp; 554 struct fid *fhp; 555 struct vnode **vpp; 556 { 557 558 return (EINVAL); 559 } 560 561 /* 562 * Vnode pointer to File handle, should never happen either 563 */ 564 /* ARGSUSED */ 565 nfs_vptofh(vp, fhp) 566 struct vnode *vp; 567 struct fid *fhp; 568 { 569 570 return (EINVAL); 571 } 572 573 /* 574 * Vfs start routine, a no-op. 575 */ 576 /* ARGSUSED */ 577 nfs_start(mp, flags, p) 578 struct mount *mp; 579 int flags; 580 struct proc *p; 581 { 582 583 return (0); 584 } 585 586 /* 587 * Do operations associated with quotas, not supported 588 */ 589 nfs_quotactl(mp, cmd, uid, arg, p) 590 struct mount *mp; 591 int cmd; 592 uid_t uid; 593 caddr_t arg; 594 struct proc *p; 595 { 596 #ifdef lint 597 mp = mp; cmd = cmd; uid = uid; arg = arg; 598 #endif /* lint */ 599 return (EOPNOTSUPP); 600 } 601