1 /* 2 * $Id: nfs_ops.c,v 5.2 90/06/23 22:19:45 jsp Rel $ 3 * 4 * Copyright (c) 1990 Jan-Simon Pendry 5 * Copyright (c) 1990 Imperial College of Science, Technology & Medicine 6 * Copyright (c) 1990 The Regents of the University of California. 7 * All rights reserved. 8 * 9 * This code is derived from software contributed to Berkeley by 10 * Jan-Simon Pendry at Imperial College, London. 11 * 12 * %sccs.include.redist.c% 13 * 14 * @(#)nfs_ops.c 5.1 (Berkeley) 06/29/90 15 */ 16 17 #include "am.h" 18 19 #ifdef HAS_NFS 20 21 #define NFS 22 #define NFSCLIENT 23 #ifdef NFS_3 24 typedef nfs_fh fhandle_t; 25 #endif /* NFS_3 */ 26 #ifdef NFS_HDR 27 #include NFS_HDR 28 #endif /* NFS_HDR */ 29 #include <sys/mount.h> 30 #include "mount.h" 31 32 /* 33 * Network file system 34 */ 35 36 /* 37 * Convert from nfsstat to UN*X error code 38 */ 39 #define unx_error(e) ((int)(e)) 40 41 /* 42 * The NFS layer maintains a cache of file handles. 43 * This is *fundamental* to the implementation and 44 * also allows quick remounting when a filesystem 45 * is accessed soon after timing out. 46 * 47 * The NFS server layer knows to flush this cache 48 * when a server goes down so avoiding stale handles. 49 * 50 * Each cache entry keeps a hard reference to 51 * the corresponding server. This ensures that 52 * the server keepalive information is maintained. 53 * 54 * The copy of the sockaddr_in here is taken so 55 * that the port can be twiddled to talk to mountd 56 * instead of portmap or the NFS server as used 57 * elsewhere. 58 * The port# is flushed if a server goes down. 59 * The IP address is never flushed - we assume 60 * that the address of a mounted machine never 61 * changes. If it does, then you have other 62 * problems... 63 */ 64 typedef struct fh_cache fh_cache; 65 struct fh_cache { 66 qelem fh_q; /* List header */ 67 voidp fh_wchan; /* Wait channel */ 68 int fh_error; /* Valid data? */ 69 int fh_id; /* Unique id */ 70 int fh_cid; /* Callout id */ 71 struct fhstatus fh_handle; /* Handle on filesystem */ 72 struct sockaddr_in fh_sin; /* Address of mountd */ 73 fserver *fh_fs; /* Server holding filesystem */ 74 char *fh_path; /* Filesystem on host */ 75 }; 76 77 /* 78 * FH_TTL is the time a file handle will remain in the cache since 79 * last being used. If the file handle becomes invalid, then it 80 * will be flushed anyway. 81 */ 82 #define FH_TTL (5 * 60) /* five minutes */ 83 #define FH_TTL_ERROR (30) /* 30 seconds */ 84 85 static int fh_id = 0; 86 #define FHID_ALLOC() (++fh_id) 87 extern qelem fh_head; 88 qelem fh_head = { &fh_head, &fh_head }; 89 90 static int call_mountd P((fh_cache*, unsigned long, fwd_fun, voidp)); 91 92 AUTH *nfs_auth; 93 94 static fh_cache *find_nfs_fhandle_cache P((voidp idv, int done)); 95 static fh_cache *find_nfs_fhandle_cache(idv, done) 96 voidp idv; 97 int done; 98 { 99 fh_cache *fp, *fp2 = 0; 100 int id = (int) idv; 101 102 ITER(fp, fh_cache, &fh_head) { 103 if (fp->fh_id == id) { 104 fp2 = fp; 105 break; 106 } 107 } 108 109 #ifdef DEBUG 110 if (fp2) { 111 dlog("fh cache gives fp %#x, fs %s", fp2, fp2->fh_path); 112 } else { 113 dlog("fh cache search failed"); 114 } 115 #endif /* DEBUG */ 116 117 if (fp2 && !done) { 118 fp2->fh_error = ETIMEDOUT; 119 return 0; 120 } 121 122 return fp2; 123 } 124 125 /* 126 * Called when a filehandle appears 127 */ 128 static void got_nfs_fh P((voidp pkt, int len, struct sockaddr_in *sa, 129 struct sockaddr_in *ia, voidp idv, int done)); 130 static void got_nfs_fh(pkt, len, sa, ia, idv, done) 131 voidp pkt; 132 int len; 133 struct sockaddr_in *sa, *ia; 134 voidp idv; 135 int done; 136 { 137 fh_cache *fp = find_nfs_fhandle_cache(idv, done); 138 if (fp) { 139 fp->fh_error = pickup_rpc_reply(pkt, len, (voidp) &fp->fh_handle, xdr_fhstatus); 140 if (!fp->fh_error) { 141 #ifdef DEBUG 142 dlog("got filehandle for %s:%s", fp->fh_fs->fs_host, fp->fh_path); 143 #endif /* DEBUG */ 144 /* 145 * Wakeup anything sleeping on this filehandle 146 */ 147 if (fp->fh_wchan) { 148 #ifdef DEBUG 149 dlog("Calling wakeup on %#x", fp->fh_wchan); 150 #endif /* DEBUG */ 151 wakeup(fp->fh_wchan); 152 } 153 } 154 } 155 } 156 157 void flush_nfs_fhandle_cache P((fserver *fs)); 158 void flush_nfs_fhandle_cache(fs) 159 fserver *fs; 160 { 161 fh_cache *fp; 162 ITER(fp, fh_cache, &fh_head) { 163 if (fp->fh_fs == fs) { 164 fp->fh_sin.sin_port = (u_short) 0; 165 fp->fh_error = -1; 166 } 167 } 168 } 169 170 static void discard_fh P((fh_cache *fp)); 171 static void discard_fh(fp) 172 fh_cache *fp; 173 { 174 rem_que(&fp->fh_q); 175 #ifdef DEBUG 176 dlog("Discarding filehandle for %s:%s", fp->fh_fs->fs_host, fp->fh_path); 177 #endif /* DEBUG */ 178 free_srvr(fp->fh_fs); 179 free((voidp) fp->fh_path); 180 free((voidp) fp); 181 } 182 183 /* 184 * Determine the file handle for a node 185 */ 186 static int prime_nfs_fhandle_cache P((char *path, fserver *fs, struct fhstatus *fhbuf, voidp wchan)); 187 static int prime_nfs_fhandle_cache(path, fs, fhbuf, wchan) 188 char *path; 189 fserver *fs; 190 struct fhstatus *fhbuf; 191 voidp wchan; 192 { 193 fh_cache *fp, *fp_save = 0; 194 int error; 195 int reuse_id = FALSE; 196 197 #ifdef DEBUG 198 dlog("Searching cache for %s:%s", fs->fs_host, path); 199 #endif /* DEBUG */ 200 201 /* 202 * First search the cache 203 */ 204 ITER(fp, fh_cache, &fh_head) { 205 if (fs == fp->fh_fs && strcmp(path, fp->fh_path) == 0) { 206 switch (fp->fh_error) { 207 case 0: 208 error = fp->fh_error = unx_error(fp->fh_handle.fhs_status); 209 if (error == 0) { 210 if (fhbuf) 211 bcopy((voidp) &fp->fh_handle, (voidp) fhbuf, 212 sizeof(fp->fh_handle)); 213 if (fp->fh_cid) 214 untimeout(fp->fh_cid); 215 fp->fh_cid = timeout(FH_TTL, discard_fh, (voidp) fp); 216 } else if (error == EACCES) { 217 /* 218 * Now decode the file handle return code. 219 */ 220 plog(XLOG_INFO, "Filehandle denied for \"%s:%s\"", 221 fs->fs_host, path); 222 } else { 223 errno = error; /* XXX */ 224 plog(XLOG_INFO, "Filehandle error for \"%s:%s\": %m", 225 fs->fs_host, path); 226 } 227 228 /* 229 * The error was returned from the remote mount daemon. 230 * Policy: this error will be cached for now... 231 */ 232 return error; 233 234 case -1: 235 /* 236 * Still thinking about it, but we can re-use. 237 */ 238 fp_save = fp; 239 reuse_id = TRUE; 240 break; 241 242 default: 243 /* 244 * Return the error. 245 * Policy: make sure we recompute if required again 246 * in case this was caused by a network failure. 247 * This can thrash mountd's though... If you find 248 * your mountd going slowly then: 249 * 1. Add a fork() loop to main. 250 * 2. Remove the call to innetgr() and don't use 251 * netgroups, especially if you don't use YP. 252 */ 253 error = fp->fh_error; 254 fp->fh_error = -1; 255 return error; 256 } 257 break; 258 } 259 } 260 261 /* 262 * Not in cache 263 */ 264 if (fp_save) { 265 fp = fp_save; 266 /* 267 * Re-use existing slot 268 */ 269 untimeout(fp->fh_cid); 270 free_srvr(fp->fh_fs); 271 free(fp->fh_path); 272 } else { 273 fp = ALLOC(fh_cache); 274 bzero((voidp) fp, sizeof(*fp)); 275 ins_que(&fp->fh_q, &fh_head); 276 } 277 if (!reuse_id) 278 fp->fh_id = FHID_ALLOC(); 279 fp->fh_wchan = wchan; 280 fp->fh_error = -1; 281 fp->fh_cid = timeout(FH_TTL, discard_fh, (voidp) fp); 282 283 /* 284 * If the address has changed then don't try to re-use the 285 * port information 286 */ 287 if (fp->fh_sin.sin_addr.s_addr != fs->fs_ip->sin_addr.s_addr) { 288 fp->fh_sin = *fs->fs_ip; 289 fp->fh_sin.sin_port = 0; 290 } 291 fp->fh_fs = dup_srvr(fs); 292 fp->fh_path = strdup(path); 293 294 error = call_mountd(fp, MOUNTPROC_MNT, got_nfs_fh, wchan); 295 if (error) { 296 /* 297 * Local error - cache for a short period 298 * just to prevent thrashing. 299 */ 300 untimeout(fp->fh_cid); 301 fp->fh_cid = timeout(error < 0 ? 2 * ALLOWED_MOUNT_TIME : FH_TTL_ERROR, 302 discard_fh, (voidp) fp); 303 fp->fh_error = error; 304 } else { 305 error = fp->fh_error; 306 } 307 return error; 308 } 309 310 static int call_mountd P((fh_cache *fp, u_long proc, fwd_fun f, voidp wchan)); 311 static int call_mountd(fp, proc, f, wchan) 312 fh_cache *fp; 313 u_long proc; 314 fwd_fun f; 315 voidp wchan; 316 { 317 struct rpc_msg mnt_msg; 318 int len; 319 char iobuf[8192]; 320 int error; 321 322 if (!nfs_auth) { 323 nfs_auth = authunix_create_default(); 324 if (!nfs_auth) 325 return ENOBUFS; 326 } 327 328 if (fp->fh_sin.sin_port == 0) { 329 u_short port; 330 error = nfs_srvr_port(fp->fh_fs, &port, wchan); 331 if (error) 332 return error; 333 fp->fh_sin.sin_port = port; 334 } 335 336 rpc_msg_init(&mnt_msg, MOUNTPROG, MOUNTVERS, (unsigned long) 0); 337 len = make_rpc_packet(iobuf, sizeof(iobuf), proc, 338 &mnt_msg, (voidp) &fp->fh_path, xdr_nfspath, nfs_auth); 339 340 if (len > 0) { 341 error = fwd_packet(MK_RPC_XID(RPC_XID_MOUNTD, fp->fh_id), 342 (voidp) iobuf, len, &fp->fh_sin, &fp->fh_sin, (voidp) fp->fh_id, f); 343 } else { 344 error = -len; 345 } 346 return error; 347 } 348 349 /*-------------------------------------------------------------------------*/ 350 351 /* 352 * NFS needs the local filesystem, remote filesystem 353 * remote hostname. 354 * Local filesystem defaults to remote and vice-versa. 355 */ 356 static int nfs_match(fo) 357 am_opts *fo; 358 { 359 if (fo->opt_fs && !fo->opt_rfs) 360 fo->opt_rfs = fo->opt_fs; 361 if (!fo->opt_rfs) { 362 plog(XLOG_USER, "nfs: no remote filesystem specified"); 363 return FALSE; 364 } 365 if (!fo->opt_rhost) { 366 plog(XLOG_USER, "nfs: no remote host specified"); 367 return FALSE; 368 } 369 /* 370 * Determine magic cookie to put in mtab 371 */ 372 fo->fs_mtab = (char *) xrealloc(fo->fs_mtab, strlen(fo->opt_rhost) + 373 strlen(fo->opt_rfs) + 2); 374 sprintf(fo->fs_mtab, "%s:%s", fo->opt_rhost, fo->opt_rfs); 375 #ifdef DEBUG 376 dlog("NFS: mounting remote server \"%s\", remote fs \"%s\" on \"%s\"", 377 fo->opt_rhost, fo->opt_rfs, fo->opt_fs); 378 #endif /* DEBUG */ 379 380 return TRUE; 381 } 382 383 /* 384 * Initialise am structure for nfs 385 */ 386 static int nfs_init(mf) 387 mntfs *mf; 388 { 389 int error; 390 char *colon = strchr(mf->mf_info, ':'); 391 if (colon == 0) 392 return ENOENT; 393 394 error = prime_nfs_fhandle_cache(colon+1, mf->mf_server, (struct fhstatus *) 0, (voidp) mf); 395 396 return error; 397 } 398 399 mount_nfs_fh(fhp, dir, fs_name, opts, mf) 400 struct fhstatus *fhp; 401 char *dir; 402 char *fs_name; 403 char *opts; 404 mntfs *mf; 405 { 406 struct nfs_args nfs_args; 407 struct mntent mnt; 408 int retry; 409 char *colon; 410 char *path; 411 char host[MAXHOSTNAMELEN + MAXPATHLEN + 2]; 412 fserver *fs = mf->mf_server; 413 int flags; 414 #ifdef notdef 415 unsigned short port; 416 #endif /* notdef */ 417 418 MTYPE_TYPE type = MOUNT_TYPE_NFS; 419 420 bzero((voidp) &nfs_args, sizeof(nfs_args)); /* Paranoid */ 421 422 /* 423 * Extract host name to give to kernel 424 */ 425 if (!(colon = strchr(fs_name, ':'))) 426 return ENOENT; 427 #ifndef NFS_ARGS_NEEDS_PATH 428 *colon = '\0'; 429 #endif 430 strncpy(host, fs_name, sizeof(host)); 431 #ifndef NFS_ARGS_NEEDS_PATH 432 *colon = ':'; 433 #endif /* NFS_ARGS_NEEDS_PATH */ 434 path = colon + 1; 435 436 bzero((voidp) &nfs_args, sizeof(nfs_args)); 437 438 mnt.mnt_dir = dir; 439 mnt.mnt_fsname = fs_name; 440 mnt.mnt_type = MTAB_TYPE_NFS; 441 mnt.mnt_opts = opts; 442 mnt.mnt_freq = 0; 443 mnt.mnt_passno = 0; 444 445 retry = hasmntval(&mnt, "retry"); 446 if (retry <= 0) 447 retry = 1; /* XXX */ 448 449 /*again:*/ 450 451 /* 452 * set mount args 453 */ 454 NFS_FH_DREF(nfs_args.fh, (NFS_FH_TYPE) fhp->fhstatus_u.fhs_fhandle); 455 456 #ifdef ULTRIX_HACK 457 nfs_args.optstr = mnt.mnt_opts; 458 #endif /* ULTRIX_HACK */ 459 460 nfs_args.hostname = host; 461 nfs_args.flags |= NFSMNT_HOSTNAME; 462 #ifdef HOSTNAMESZ 463 /* 464 * Most kernels have a name length restriction. 465 */ 466 if (strlen(host) >= HOSTNAMESZ) 467 strcpy(host + HOSTNAMESZ - 3, ".."); 468 #endif /* HOSTNAMESZ */ 469 470 if (nfs_args.rsize = hasmntval(&mnt, "rsize")) 471 nfs_args.flags |= NFSMNT_RSIZE; 472 473 if (nfs_args.wsize = hasmntval(&mnt, "wsize")) 474 nfs_args.flags |= NFSMNT_WSIZE; 475 476 if (nfs_args.timeo = hasmntval(&mnt, "timeo")) 477 nfs_args.flags |= NFSMNT_TIMEO; 478 479 if (nfs_args.retrans = hasmntval(&mnt, "retrans")) 480 nfs_args.flags |= NFSMNT_RETRANS; 481 482 #ifdef NFSMNT_BIODS 483 if (nfs_args.biods = hasmntval(&mnt, "biods")) 484 nfs_args.flags |= NFSMNT_BIODS; 485 486 #endif /* NFSMNT_BIODS */ 487 488 #ifdef notdef 489 /* 490 * This isn't supported by the ping algorithm yet. 491 * In any case, it is all done in nfs_init(). 492 */ 493 if (port = hasmntval(&mnt, "port")) 494 sin.sin_port = htons(port); 495 else 496 sin.sin_port = htons(NFS_PORT); /* XXX should use portmapper */ 497 #endif /* notdef */ 498 499 if (hasmntopt(&mnt, MNTOPT_SOFT) != NULL) 500 nfs_args.flags |= NFSMNT_SOFT; 501 502 #ifdef MNTOPT_INTR 503 if (hasmntopt(&mnt, MNTOPT_INTR) != NULL) 504 nfs_args.flags |= NFSMNT_INT; 505 #endif /* MNTOPT_INTR */ 506 507 #ifdef MNTOPT_NODEVS 508 if (hasmntopt(&mnt, MNTOPT_NODEVS) != NULL) 509 nfs_args.flags |= NFSMNT_NODEVS; 510 #endif /* MNTOPT_NODEVS */ 511 512 #ifdef NFSMNT_PGTHRESH 513 if (nfs_args.pg_thresh = hasmntval(&mnt, "pgthresh")) 514 nfs_args.flags |= NFSMNT_PGTHRESH; 515 #endif /* NFSMNT_PGTHRESH */ 516 517 NFS_SA_DREF(nfs_args, fs->fs_ip); 518 519 flags = compute_mount_flags(&mnt); 520 521 #ifdef HAS_TCP_NFS 522 if (hasmntopt(&mnt, "tcp") != NULL) 523 nfs_args.sotype = SOCK_STREAM; 524 #endif /* HAS_TCP_NFS */ 525 526 527 #ifdef ULTRIX_HACK 528 /* 529 * Ultrix passes the flags argument as part of the 530 * mount data structure, rather than using the 531 * flags argument to the system call. This is 532 * confusing... 533 */ 534 if (!(nfs_args.flags & NFSMNT_PGTHRESH)) { 535 nfs_args.pg_thresh = 64; /* 64k - XXX */ 536 nfs_args.flags |= NFSMNT_PGTHRESH; 537 } 538 nfs_args.gfs_flags = flags; 539 flags &= M_RDONLY; 540 if (flags & M_RDONLY) 541 nfs_args.flags |= NFSMNT_RONLY; 542 #endif /* ULTRIX_HACK */ 543 544 return mount_fs(&mnt, flags, (caddr_t) &nfs_args, retry, type); 545 } 546 547 static mount_nfs(dir, fs_name, opts, mf) 548 char *dir; 549 char *fs_name; 550 char *opts; 551 mntfs *mf; 552 { 553 int error; 554 struct fhstatus fhs; 555 char *colon; 556 557 if (!(colon = strchr(fs_name, ':'))) 558 return ENOENT; 559 560 #ifdef DEBUG 561 dlog("locating fhandle for %s", fs_name); 562 #endif /* DEBUG */ 563 error = prime_nfs_fhandle_cache(colon+1, mf->mf_server, &fhs, (voidp) 0); 564 565 if (error) 566 return error; 567 568 return mount_nfs_fh(&fhs, dir, fs_name, opts, mf); 569 } 570 571 static int nfs_mount(mp) 572 am_node *mp; 573 { 574 mntfs *mf = mp->am_mnt; 575 576 int error = mount_nfs(mf->mf_mount, mf->mf_info, 577 mf->mf_fo->opt_opts, mf); 578 579 #ifdef DEBUG 580 if (error) { 581 errno = error; 582 dlog("mount_nfs: %m"); 583 } 584 #endif /* DEBUG */ 585 return error; 586 } 587 588 static int nfs_umount(mp) 589 am_node *mp; 590 { 591 mntfs *mf = mp->am_mnt; 592 593 int error = UMOUNT_FS(mf->mf_mount); 594 if (error) 595 return error; 596 597 return 0; 598 } 599 600 static void nfs_umounted(mp) 601 am_node *mp; 602 { 603 #ifdef INFORM_MOUNTD 604 /* 605 * Don't bother to inform remote mountd 606 * that we are finished. Until a full 607 * track of filehandles is maintained 608 * the mountd unmount callback cannot 609 * be done correctly anyway... 610 */ 611 612 mntfs *mf = mp->am_mnt; 613 fserver *fs; 614 char *colon, *path; 615 616 if (mf->mf_error || mf->mf_refc > 1) 617 return; 618 619 fs = mf->mf_server; 620 621 /* 622 * Call the mount daemon on the server to 623 * announce that we are not using the fs any more. 624 * 625 * This is *wrong*. The mountd should be called 626 * when the fhandle is flushed from the cache, and 627 * a reference held to the cached entry while the 628 * fs is mounted... 629 */ 630 colon = path = strchr(mf->mf_info, ':'); 631 if (fs && colon) { 632 fh_cache f; 633 #ifdef DEBUG 634 dlog("calling mountd for %s", mf->mf_info); 635 #endif /* DEBUG */ 636 *path++ = '\0'; 637 f.fh_path = path; 638 f.fh_sin = *fs->fs_ip; 639 f.fh_sin.sin_port = (u_short) 0; 640 f.fh_fs = fs; 641 f.fh_id = 0; 642 f.fh_error = 0; 643 (void) prime_nfs_fhandle_cache(colon+1, mf->mf_server, (struct fhstatus *) 0, (voidp) mf); 644 (void) call_mountd(&f, MOUNTPROC_UMNT, (fwd_fun) 0, (voidp) 0); 645 *colon = ':'; 646 } 647 #endif /* INFORM_MOUNTD */ 648 } 649 650 /* 651 * Network file system 652 */ 653 am_ops nfs_ops = { 654 "nfs", 655 nfs_match, 656 nfs_init, 657 nfs_mount, 658 nfs_umount, 659 efs_lookuppn, 660 efs_readdir, 661 0, /* nfs_readlink */ 662 0, /* nfs_mounted */ 663 nfs_umounted, 664 find_nfs_srvr, 665 FS_MKMNT|FS_BACKGROUND|FS_AMQINFO 666 }; 667 668 #endif /* HAS_NFS */ 669