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.5 1993/11/19 02:32:34 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, curproc)) 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 struct vattr attr; 229 230 if (nfs_dogetattr(vp,&attr,NOCRED,0,0)) { 231 panic("nfs swap"); 232 } 233 swdevt[0].sw_nblks = attr.va_size / DEV_BSIZE; 234 } 235 } 236 237 /* 238 * Create the rootfs mount point. 239 */ 240 mp = (struct mount *)malloc((u_long)sizeof(struct mount), 241 M_MOUNT, M_NOWAIT); 242 if (mp == NULL) 243 panic("nfs root mount2"); 244 mp->mnt_op = &nfs_vfsops; 245 mp->mnt_flag = MNT_RDONLY; 246 mp->mnt_exroot = 0; 247 mp->mnt_mounth = NULLVP; 248 249 /* 250 * Set up the root fs args and call mountnfs() to do the rest. 251 */ 252 nfs_diskless.root_args.fh = (nfsv2fh_t *)nfs_diskless.root_fh; 253 MGET(m, MT_SONAME, M_DONTWAIT); 254 if (m == NULL) 255 panic("nfs root mbuf2"); 256 bcopy((caddr_t)&nfs_diskless.root_saddr, mtod(m, caddr_t), 257 nfs_diskless.root_saddr.sa_len); 258 m->m_len = nfs_diskless.root_saddr.sa_len; 259 if (mountnfs(&nfs_diskless.root_args, mp, m, "/", 260 nfs_diskless.root_hostnam, &vp)) 261 panic("nfs root"); 262 if (vfs_lock(mp)) 263 panic("nfs root2"); 264 rootfs = mp; 265 mp->mnt_next = mp; 266 mp->mnt_prev = mp; 267 mp->mnt_vnodecovered = NULLVP; 268 vfs_unlock(mp); 269 rootvp = vp; 270 inittodr((time_t)0); /* There is no time in the nfs fsstat so ?? */ 271 return (0); 272 } 273 274 /* 275 * VFS Operations. 276 * 277 * mount system call 278 * It seems a bit dumb to copyinstr() the host and path here and then 279 * bcopy() them in mountnfs(), but I wanted to detect errors before 280 * doing the sockargs() call because sockargs() allocates an mbuf and 281 * an error after that means that I have to release the mbuf. 282 */ 283 /* ARGSUSED */ 284 nfs_mount(mp, path, data, ndp, p) 285 struct mount *mp; 286 char *path; 287 caddr_t data; 288 struct nameidata *ndp; 289 struct proc *p; 290 { 291 int error; 292 struct nfs_args args; 293 struct mbuf *nam; 294 struct vnode *vp; 295 char pth[MNAMELEN], hst[MNAMELEN]; 296 u_int len; 297 nfsv2fh_t nfh; 298 299 if (mp->mnt_flag & MNT_UPDATE) 300 return (0); 301 if (error = copyin(data, (caddr_t)&args, sizeof (struct nfs_args))) 302 return (error); 303 if (error = copyin((caddr_t)args.fh, (caddr_t)&nfh, sizeof (nfsv2fh_t))) 304 return (error); 305 if (error = copyinstr(path, pth, MNAMELEN-1, &len)) 306 return (error); 307 bzero(&pth[len], MNAMELEN - len); 308 if (error = copyinstr(args.hostname, hst, MNAMELEN-1, &len)) 309 return (error); 310 bzero(&hst[len], MNAMELEN - len); 311 /* sockargs() call must be after above copyin() calls */ 312 if (error = sockargs(&nam, (caddr_t)args.addr, 313 sizeof (struct sockaddr), MT_SONAME)) 314 return (error); 315 args.fh = &nfh; 316 error = mountnfs(&args, mp, nam, pth, hst, &vp); 317 return (error); 318 } 319 320 /* 321 * Common code for mount and mountroot 322 */ 323 mountnfs(argp, mp, nam, pth, hst, vpp) 324 register struct nfs_args *argp; 325 register struct mount *mp; 326 struct mbuf *nam; 327 char *pth, *hst; 328 struct vnode **vpp; 329 { 330 register struct nfsmount *nmp; 331 struct proc *p = curproc; /* XXX */ 332 struct nfsnode *np; 333 int error; 334 fsid_t tfsid; 335 336 MALLOC(nmp, struct nfsmount *, sizeof *nmp, M_NFSMNT, M_WAITOK); 337 bzero((caddr_t)nmp, sizeof *nmp); 338 mp->mnt_data = (qaddr_t)nmp; 339 340 getnewfsid(mp, MOUNT_NFS); 341 nmp->nm_mountp = mp; 342 nmp->nm_flag = argp->flags; 343 nmp->nm_rto = NFS_TIMEO; 344 nmp->nm_rtt = -1; 345 nmp->nm_rttvar = nmp->nm_rto << 1; 346 nmp->nm_retry = NFS_RETRANS; 347 nmp->nm_wsize = NFS_WSIZE; 348 nmp->nm_rsize = NFS_RSIZE; 349 bcopy((caddr_t)argp->fh, (caddr_t)&nmp->nm_fh, sizeof(nfsv2fh_t)); 350 bcopy(hst, mp->mnt_stat.f_mntfromname, MNAMELEN); 351 bcopy(pth, mp->mnt_stat.f_mntonname, MNAMELEN); 352 nmp->nm_nam = nam; 353 354 if ((argp->flags & NFSMNT_TIMEO) && argp->timeo > 0) { 355 nmp->nm_rto = argp->timeo; 356 /* NFS timeouts are specified in 1/10 sec. */ 357 nmp->nm_rto = (nmp->nm_rto * 10) / NFS_HZ; 358 if (nmp->nm_rto < NFS_MINTIMEO) 359 nmp->nm_rto = NFS_MINTIMEO; 360 else if (nmp->nm_rto > NFS_MAXTIMEO) 361 nmp->nm_rto = NFS_MAXTIMEO; 362 nmp->nm_rttvar = nmp->nm_rto << 1; 363 } 364 365 if ((argp->flags & NFSMNT_RETRANS) && argp->retrans > 1) { 366 nmp->nm_retry = argp->retrans; 367 if (nmp->nm_retry > NFS_MAXREXMIT) 368 nmp->nm_retry = NFS_MAXREXMIT; 369 } 370 371 if ((argp->flags & NFSMNT_WSIZE) && argp->wsize > 0) { 372 nmp->nm_wsize = argp->wsize; 373 /* Round down to multiple of blocksize */ 374 nmp->nm_wsize &= ~0x1ff; 375 if (nmp->nm_wsize <= 0) 376 nmp->nm_wsize = 512; 377 else if (nmp->nm_wsize > NFS_MAXDATA) 378 nmp->nm_wsize = NFS_MAXDATA; 379 } 380 if (nmp->nm_wsize > MAXBSIZE) 381 nmp->nm_wsize = MAXBSIZE; 382 383 if ((argp->flags & NFSMNT_RSIZE) && argp->rsize > 0) { 384 nmp->nm_rsize = argp->rsize; 385 /* Round down to multiple of blocksize */ 386 nmp->nm_rsize &= ~0x1ff; 387 if (nmp->nm_rsize <= 0) 388 nmp->nm_rsize = 512; 389 else if (nmp->nm_rsize > NFS_MAXDATA) 390 nmp->nm_rsize = NFS_MAXDATA; 391 } 392 if (nmp->nm_rsize > MAXBSIZE) 393 nmp->nm_rsize = MAXBSIZE; 394 /* Set up the sockets and per-host congestion */ 395 nmp->nm_sotype = argp->sotype; 396 nmp->nm_soproto = argp->proto; 397 if (error = nfs_connect(nmp)) 398 goto bad; 399 400 if (error = nfs_statfs(mp, &mp->mnt_stat, p)) 401 goto bad; 402 /* 403 * A reference count is needed on the nfsnode representing the 404 * remote root. If this object is not persistent, then backward 405 * traversals of the mount point (i.e. "..") will not work if 406 * the nfsnode gets flushed out of the cache. Ufs does not have 407 * this problem, because one can identify root inodes by their 408 * number == ROOTINO (2). 409 */ 410 if (error = nfs_nget(mp, &nmp->nm_fh, &np)) 411 goto bad; 412 /* 413 * Unlock it, but keep the reference count. 414 */ 415 nfs_unlock(NFSTOV(np)); 416 *vpp = NFSTOV(np); 417 418 return (0); 419 bad: 420 nfs_disconnect(nmp); 421 FREE(nmp, M_NFSMNT); 422 m_freem(nam); 423 return (error); 424 } 425 426 /* 427 * unmount system call 428 */ 429 nfs_unmount(mp, mntflags, p) 430 struct mount *mp; 431 int mntflags; 432 struct proc *p; 433 { 434 register struct nfsmount *nmp; 435 struct nfsnode *np; 436 struct vnode *vp; 437 int error, flags = 0; 438 extern int doforce; 439 440 if (mntflags & MNT_FORCE) { 441 if (!doforce || mp == rootfs) 442 return (EINVAL); 443 flags |= FORCECLOSE; 444 } 445 nmp = VFSTONFS(mp); 446 /* 447 * Clear out the buffer cache 448 */ 449 mntflushbuf(mp, 0); 450 if (mntinvalbuf(mp)) 451 return (EBUSY); 452 /* 453 * Goes something like this.. 454 * - Check for activity on the root vnode (other than ourselves). 455 * - Call vflush() to clear out vnodes for this file system, 456 * except for the root vnode. 457 * - Decrement reference on the vnode representing remote root. 458 * - Close the socket 459 * - Free up the data structures 460 */ 461 /* 462 * We need to decrement the ref. count on the nfsnode representing 463 * the remote root. See comment in mountnfs(). The VFS unmount() 464 * has done vput on this vnode, otherwise we would get deadlock! 465 */ 466 if (error = nfs_nget(mp, &nmp->nm_fh, &np)) 467 return(error); 468 vp = NFSTOV(np); 469 if (vp->v_usecount > 2) { 470 vput(vp); 471 return (EBUSY); 472 } 473 if (error = vflush(mp, vp, flags)) { 474 vput(vp); 475 return (error); 476 } 477 /* 478 * Get rid of two reference counts, and unlock it on the second. 479 */ 480 vrele(vp); 481 vput(vp); 482 nfs_disconnect(nmp); 483 m_freem(nmp->nm_nam); 484 free((caddr_t)nmp, M_NFSMNT); 485 return (0); 486 } 487 488 /* 489 * Return root of a filesystem 490 */ 491 nfs_root(mp, vpp) 492 struct mount *mp; 493 struct vnode **vpp; 494 { 495 register struct vnode *vp; 496 struct nfsmount *nmp; 497 struct nfsnode *np; 498 int error; 499 500 nmp = VFSTONFS(mp); 501 if (error = nfs_nget(mp, &nmp->nm_fh, &np)) 502 return (error); 503 vp = NFSTOV(np); 504 vp->v_type = VDIR; 505 vp->v_flag = VROOT; 506 *vpp = vp; 507 return (0); 508 } 509 510 extern int syncprt; 511 512 /* 513 * Flush out the buffer cache 514 */ 515 /* ARGSUSED */ 516 nfs_sync(mp, waitfor) 517 struct mount *mp; 518 int waitfor; 519 { 520 if (syncprt) 521 bufstats(); 522 /* 523 * Force stale buffer cache information to be flushed. 524 */ 525 mntflushbuf(mp, waitfor == MNT_WAIT ? B_SYNC : 0); 526 return (0); 527 } 528 529 /* 530 * At this point, this should never happen 531 */ 532 /* ARGSUSED */ 533 nfs_fhtovp(mp, fhp, vpp) 534 struct mount *mp; 535 struct fid *fhp; 536 struct vnode **vpp; 537 { 538 539 return (EINVAL); 540 } 541 542 /* 543 * Vnode pointer to File handle, should never happen either 544 */ 545 /* ARGSUSED */ 546 nfs_vptofh(vp, fhp) 547 struct vnode *vp; 548 struct fid *fhp; 549 { 550 551 return (EINVAL); 552 } 553 554 /* 555 * Vfs start routine, a no-op. 556 */ 557 /* ARGSUSED */ 558 nfs_start(mp, flags, p) 559 struct mount *mp; 560 int flags; 561 struct proc *p; 562 { 563 564 return (0); 565 } 566 567 /* 568 * Do operations associated with quotas, not supported 569 */ 570 nfs_quotactl(mp, cmd, uid, arg, p) 571 struct mount *mp; 572 int cmd; 573 uid_t uid; 574 caddr_t arg; 575 struct proc *p; 576 { 577 #ifdef lint 578 mp = mp; cmd = cmd; uid = uid; arg = arg; 579 #endif /* lint */ 580 return (EOPNOTSUPP); 581 } 582