1 /* 2 * CDDL HEADER START 3 * 4 * The contents of this file are subject to the terms of the 5 * Common Development and Distribution License, Version 1.0 only 6 * (the "License"). You may not use this file except in compliance 7 * with the License. 8 * 9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 10 * or http://www.opensolaris.org/os/licensing. 11 * See the License for the specific language governing permissions 12 * and limitations under the License. 13 * 14 * When distributing Covered Code, include this CDDL HEADER in each 15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 16 * If applicable, add the following below this CDDL HEADER, with the 17 * fields enclosed by brackets "[]" replaced with your own identifying 18 * information: Portions Copyright [yyyy] [name of copyright owner] 19 * 20 * CDDL HEADER END 21 */ 22 /* 23 * Copyright 2005 Sun Microsystems, Inc. All rights reserved. 24 * Use is subject to license terms. 25 */ 26 27 #pragma ident "%Z%%M% %I% %E% SMI" 28 29 /* 30 * NFS Version 4 state recovery code. 31 */ 32 33 #include <nfs/nfs4_clnt.h> 34 #include <nfs/nfs4.h> 35 #include <nfs/rnode4.h> 36 #include <sys/cmn_err.h> 37 #include <sys/cred.h> 38 #include <sys/systm.h> 39 #include <sys/flock.h> 40 #include <sys/dnlc.h> 41 #include <sys/ddi.h> 42 #include <sys/disp.h> 43 #include <sys/list.h> 44 #include <sys/sdt.h> 45 46 extern r4hashq_t *rtable4; 47 48 /* 49 * Information that describes what needs to be done for recovery. It is 50 * passed to a client recovery thread as well as passed to various recovery 51 * routines. rc_mi, rc_vp1, and rc_vp2 refer to the filesystem and 52 * vnode(s) affected by recovery. rc_vp1 and rc_vp2 are references (use 53 * VN_HOLD) or NULL. rc_lost_rqst contains information about the lost 54 * lock or open/close request, and it holds reference counts for the 55 * various objects (vnode, etc.). The recovery thread also uses flags set 56 * in the mntinfo4_t or vnode_t to tell it what to do. rc_error is used 57 * to save the error that originally triggered the recovery event -- will 58 * later be used to set mi_error if recovery doesn't work. rc_bseqid_rqst 59 * contains information about the request that got NFS4ERR_BAD_SEQID, and 60 * it holds reference count for the various objects (vnode, open owner, 61 * open stream, lock owner). 62 */ 63 64 typedef struct { 65 mntinfo4_t *rc_mi; 66 vnode_t *rc_vp1; 67 vnode_t *rc_vp2; 68 nfs4_recov_t rc_action; 69 stateid4 rc_stateid; 70 bool_t rc_srv_reboot; /* server has rebooted */ 71 nfs4_lost_rqst_t *rc_lost_rqst; 72 nfs4_error_t rc_orig_errors; /* original errors causing recovery */ 73 int rc_error; 74 nfs4_bseqid_entry_t *rc_bseqid_rqst; 75 } recov_info_t; 76 77 /* 78 * How long to wait before trying again if there is an error doing 79 * recovery, in seconds. 80 */ 81 82 static int recov_err_delay = 1; 83 84 /* 85 * How long to wait when processing NFS4ERR_GRACE or NFS4ERR_DELAY 86 * errors. Expressed in seconds. Default is defined as 87 * NFS4ERR_DELAY_TIME and this variable is initialized in nfs4_subr_init() 88 */ 89 time_t nfs4err_delay_time = 0; 90 91 /* 92 * Tuneable to limit how many time "exempt" ops go OTW 93 * after a recovery error. Exempt op hints are OH_CLOSE, 94 * OH_LOCKU, OH_DELEGRETURN. These previously always went 95 * OTW even after rnode was "dead" due to recovery errors. 96 * 97 * The tuneable below limits the number of times a start_fop 98 * invocation will retry the exempt hints. After the limit 99 * is reached, nfs4_start_fop will return an error just like 100 * it would for non-exempt op hints. 101 */ 102 int nfs4_max_recov_error_retry = 3; 103 104 /* 105 * Number of seconds the recovery thread should pause before retry when the 106 * filesystem has been forcibly unmounted. 107 */ 108 109 int nfs4_unmount_delay = 1; 110 111 #ifdef DEBUG 112 113 /* 114 * How long to wait (in seconds) between recovery operations on a given 115 * file. Normally zero, but could be set longer for testing purposes. 116 */ 117 static int nfs4_recovdelay = 0; 118 119 /* 120 * Switch that controls whether to go into the debugger when recovery 121 * fails. 122 */ 123 static int nfs4_fail_recov_stop = 0; 124 125 /* 126 * Tuneables to debug client namespace interaction with server 127 * mount points: 128 * 129 * nfs4_srvmnt_fail_cnt: 130 * number of times EACCES returned because client 131 * attempted to cross server mountpoint 132 * 133 * nfs4_srvmnt_debug: 134 * trigger console printf whenever client attempts 135 * to cross server mountpoint 136 */ 137 int nfs4_srvmnt_fail_cnt = 0; 138 int nfs4_srvmnt_debug = 0; 139 #endif 140 141 /* forward references, in alphabetic order */ 142 static void close_after_open_resend(vnode_t *, cred_t *, uint32_t, 143 nfs4_error_t *); 144 static void errs_to_action(recov_info_t *, 145 nfs4_server_t *, mntinfo4_t *, stateid4 *, nfs4_lost_rqst_t *, int, 146 nfs_opnum4, nfs4_bseqid_entry_t *); 147 static void flush_reinstate(nfs4_lost_rqst_t *); 148 static void free_milist(mntinfo4_t **, int); 149 static mntinfo4_t **make_milist(nfs4_server_t *, int *); 150 static int nfs4_check_recov_err(vnode_t *, nfs4_op_hint_t, 151 nfs4_recov_state_t *, int, char *); 152 static int nfs4_check_srvstub(vnode_t *vp, rnode4_t *rp, nfs4_op_hint_t op); 153 static char *nfs4_getsrvnames(mntinfo4_t *, size_t *); 154 static void nfs4_recov_fh_fail(vnode_t *, int, nfsstat4); 155 static void nfs4_recov_thread(recov_info_t *); 156 static void nfs4_remove_lost_rqsts(mntinfo4_t *, nfs4_server_t *); 157 static void nfs4_resend_lost_rqsts(recov_info_t *, nfs4_server_t *); 158 static cred_t *pid_to_cr(pid_t); 159 static void reclaim_one_lock(vnode_t *, flock64_t *, nfs4_error_t *, int *); 160 static void recov_bad_seqid(recov_info_t *); 161 static void recov_badstate(recov_info_t *, vnode_t *, nfsstat4); 162 static void recov_clientid(recov_info_t *, nfs4_server_t *); 163 static void recov_done(mntinfo4_t *, recov_info_t *); 164 static void recov_filehandle(nfs4_recov_t, mntinfo4_t *, vnode_t *); 165 static void recov_newserver(recov_info_t *, nfs4_server_t **, bool_t *); 166 static void recov_openfiles(recov_info_t *, nfs4_server_t *); 167 static void recov_stale(mntinfo4_t *, vnode_t *); 168 static void nfs4_free_lost_rqst(nfs4_lost_rqst_t *, nfs4_server_t *); 169 static void recov_throttle(recov_info_t *, vnode_t *); 170 static void relock_skip_pid(locklist_t *, pid_t); 171 static void resend_lock(nfs4_lost_rqst_t *, nfs4_error_t *); 172 static void resend_one_op(nfs4_lost_rqst_t *, nfs4_error_t *, mntinfo4_t *, 173 nfs4_server_t *); 174 static void save_bseqid_rqst(nfs4_bseqid_entry_t *, recov_info_t *); 175 static void start_recovery(recov_info_t *, mntinfo4_t *, vnode_t *, vnode_t *, 176 nfs4_server_t *); 177 static void start_recovery_action(nfs4_recov_t, bool_t, mntinfo4_t *, vnode_t *, 178 vnode_t *); 179 static int wait_for_recovery(mntinfo4_t *, nfs4_op_hint_t); 180 181 /* 182 * Return non-zero if the given errno, status, and rpc status codes 183 * in the nfs4_error_t indicate that client recovery is needed. 184 * "stateful" indicates whether the call that got the error establishes or 185 * removes state on the server (open, close, lock, unlock, delegreturn). 186 */ 187 188 int 189 nfs4_needs_recovery(nfs4_error_t *ep, bool_t stateful, vfs_t *vfsp) 190 { 191 int recov = 0; 192 mntinfo4_t *mi; 193 194 /* 195 * Try failover if the error values justify it and if 196 * it's a failover mount. Don't try if the mount is in 197 * progress, failures are handled explicitly by nfs4rootvp. 198 */ 199 if (nfs4_try_failover(ep)) { 200 mi = VFTOMI4(vfsp); 201 mutex_enter(&mi->mi_lock); 202 recov = FAILOVER_MOUNT4(mi) && !(mi->mi_flags & MI4_MOUNTING); 203 mutex_exit(&mi->mi_lock); 204 if (recov) 205 return (recov); 206 } 207 208 if (ep->error == EINTR || NFS4_FRC_UNMT_ERR(ep->error, vfsp)) { 209 /* 210 * The server may have gotten the request, so for stateful 211 * ops we need to resynchronize and possibly back out the 212 * op. 213 */ 214 return (stateful); 215 } 216 if (ep->error != 0) 217 return (0); 218 219 /* stat values are listed alphabetically */ 220 /* 221 * There are two lists here: the errors for which we have code, and 222 * the errors for which we plan to have code before FCS. For the 223 * second list, print a warning message but don't attempt recovery. 224 */ 225 switch (ep->stat) { 226 case NFS4ERR_BADHANDLE: 227 case NFS4ERR_BAD_SEQID: 228 case NFS4ERR_BAD_STATEID: 229 case NFS4ERR_DELAY: 230 case NFS4ERR_EXPIRED: 231 case NFS4ERR_FHEXPIRED: 232 case NFS4ERR_GRACE: 233 case NFS4ERR_OLD_STATEID: 234 case NFS4ERR_RESOURCE: 235 case NFS4ERR_STALE_CLIENTID: 236 case NFS4ERR_STALE_STATEID: 237 case NFS4ERR_WRONGSEC: 238 case NFS4ERR_STALE: 239 recov = 1; 240 break; 241 #ifdef DEBUG 242 case NFS4ERR_LEASE_MOVED: 243 case NFS4ERR_MOVED: 244 zcmn_err(VFTOMI4(vfsp)->mi_zone->zone_id, 245 CE_WARN, "!Can't yet recover from NFS status %d", 246 ep->stat); 247 break; 248 #endif 249 } 250 251 return (recov); 252 } 253 254 /* 255 * Some operations such as DELEGRETURN want to avoid invoking 256 * recovery actions that will only mark the file dead. If 257 * better handlers are invoked for any of these errors, this 258 * routine should be modified. 259 */ 260 int 261 nfs4_recov_marks_dead(nfsstat4 status) 262 { 263 if (status == NFS4ERR_BAD_SEQID || 264 status == NFS4ERR_EXPIRED || 265 status == NFS4ERR_BAD_STATEID || 266 status == NFS4ERR_OLD_STATEID) 267 return (1); 268 return (0); 269 } 270 271 /* 272 * Transfer the state recovery information in recovp to mi's resend queue, 273 * and mark mi as having a lost state request. 274 */ 275 static void 276 nfs4_enqueue_lost_rqst(recov_info_t *recovp, mntinfo4_t *mi) 277 { 278 nfs4_lost_rqst_t *lrp = recovp->rc_lost_rqst; 279 280 ASSERT(nfs_rw_lock_held(&mi->mi_recovlock, RW_READER) || 281 nfs_rw_lock_held(&mi->mi_recovlock, RW_WRITER)); 282 283 ASSERT(lrp != NULL && lrp->lr_op != 0); 284 285 NFS4_DEBUG(nfs4_lost_rqst_debug, (CE_NOTE, 286 "nfs4_enqueue_lost_rqst %p, op %d", 287 (void *)lrp, lrp->lr_op)); 288 289 mutex_enter(&mi->mi_lock); 290 mi->mi_recovflags |= MI4R_LOST_STATE; 291 if (lrp->lr_putfirst) 292 list_insert_head(&mi->mi_lost_state, lrp); 293 else 294 list_insert_tail(&mi->mi_lost_state, lrp); 295 recovp->rc_lost_rqst = NULL; 296 mutex_exit(&mi->mi_lock); 297 298 nfs4_queue_event(RE_LOST_STATE, mi, NULL, lrp->lr_op, lrp->lr_vp, 299 lrp->lr_dvp, 0, NULL, 0, TAG_NONE, TAG_NONE, 0, 0); 300 } 301 302 /* 303 * Transfer the bad seqid recovery information in recovp to mi's 304 * bad seqid queue, and mark mi as having a bad seqid request. 305 */ 306 void 307 enqueue_bseqid_rqst(recov_info_t *recovp, mntinfo4_t *mi) 308 { 309 ASSERT(nfs_rw_lock_held(&mi->mi_recovlock, RW_READER) || 310 nfs_rw_lock_held(&mi->mi_recovlock, RW_WRITER)); 311 ASSERT(recovp->rc_bseqid_rqst != NULL); 312 313 mutex_enter(&mi->mi_lock); 314 mi->mi_recovflags |= MI4R_BAD_SEQID; 315 list_insert_tail(&mi->mi_bseqid_list, recovp->rc_bseqid_rqst); 316 recovp->rc_bseqid_rqst = NULL; 317 mutex_exit(&mi->mi_lock); 318 } 319 320 /* 321 * Initiate recovery. 322 * 323 * The nfs4_error_t contains the return codes that triggered a recovery 324 * attempt. mi, vp1, and vp2 refer to the filesystem and files that were 325 * being operated on. vp1 and vp2 may be NULL. 326 * 327 * Multiple calls are okay. If recovery is already underway, the call 328 * updates the information about what state needs recovery but does not 329 * start a new thread. The caller should hold mi->mi_recovlock as a reader 330 * for proper synchronization with any recovery thread. 331 * 332 * This will return TRUE if recovery was aborted, and FALSE otherwise. 333 */ 334 bool_t 335 nfs4_start_recovery(nfs4_error_t *ep, mntinfo4_t *mi, vnode_t *vp1, 336 vnode_t *vp2, stateid4 *sid, nfs4_lost_rqst_t *lost_rqstp, nfs_opnum4 op, 337 nfs4_bseqid_entry_t *bsep) 338 { 339 recov_info_t *recovp; 340 nfs4_server_t *sp; 341 bool_t abort = FALSE; 342 bool_t gone = FALSE; 343 344 ASSERT(curproc->p_zone == mi->mi_zone); 345 mutex_enter(&mi->mi_lock); 346 /* 347 * If there is lost state, we need to kick off recovery even if the 348 * filesystem has been unmounted or the zone is shutting down. 349 */ 350 gone = FS_OR_ZONE_GONE4(mi->mi_vfsp); 351 if (gone) { 352 ASSERT(ep->error != EINTR || lost_rqstp != NULL); 353 if (ep->error == EIO && lost_rqstp == NULL) { 354 /* failed due to forced unmount, no new lost state */ 355 abort = TRUE; 356 } 357 if ((ep->error == 0 || ep->error == ETIMEDOUT) && 358 !(mi->mi_recovflags & MI4R_LOST_STATE)) { 359 /* some other failure, no existing lost state */ 360 abort = TRUE; 361 } 362 if (abort) { 363 mutex_exit(&mi->mi_lock); 364 NFS4_DEBUG(nfs4_client_recov_debug, (CE_NOTE, 365 "nfs4_start_recovery: fs unmounted")); 366 return (TRUE); 367 } 368 } 369 mi->mi_in_recovery++; 370 mutex_exit(&mi->mi_lock); 371 372 recovp = kmem_alloc(sizeof (recov_info_t), KM_SLEEP); 373 recovp->rc_orig_errors = *ep; 374 sp = find_nfs4_server(mi); 375 errs_to_action(recovp, sp, mi, sid, lost_rqstp, 376 gone, op, bsep); 377 if (sp != NULL) 378 mutex_exit(&sp->s_lock); 379 start_recovery(recovp, mi, vp1, vp2, sp); 380 if (sp != NULL) 381 nfs4_server_rele(sp); 382 return (FALSE); 383 } 384 385 /* 386 * Internal version of nfs4_start_recovery. The difference is that the 387 * caller specifies the recovery action, rather than the errors leading to 388 * recovery. 389 */ 390 static void 391 start_recovery_action(nfs4_recov_t what, bool_t reboot, mntinfo4_t *mi, 392 vnode_t *vp1, vnode_t *vp2) 393 { 394 recov_info_t *recovp; 395 396 ASSERT(curproc->p_zone == mi->mi_zone); 397 mutex_enter(&mi->mi_lock); 398 mi->mi_in_recovery++; 399 mutex_exit(&mi->mi_lock); 400 401 recovp = kmem_zalloc(sizeof (recov_info_t), KM_SLEEP); 402 recovp->rc_action = what; 403 recovp->rc_srv_reboot = reboot; 404 recovp->rc_error = EIO; 405 start_recovery(recovp, mi, vp1, vp2, NULL); 406 } 407 408 static void 409 start_recovery(recov_info_t *recovp, mntinfo4_t *mi, 410 vnode_t *vp1, vnode_t *vp2, nfs4_server_t *sp) 411 { 412 NFS4_DEBUG(nfs4_client_recov_debug, (CE_NOTE, 413 "start_recovery: mi %p, what %s", (void*)mi, 414 nfs4_recov_action_to_str(recovp->rc_action))); 415 416 /* 417 * Bump the reference on the vfs so that we can pass it to the 418 * recovery thread. 419 */ 420 VFS_HOLD(mi->mi_vfsp); 421 422 again: 423 switch (recovp->rc_action) { 424 case NR_FAILOVER: 425 ASSERT(nfs_rw_lock_held(&mi->mi_recovlock, RW_READER) || 426 nfs_rw_lock_held(&mi->mi_recovlock, RW_WRITER)); 427 if (mi->mi_servers->sv_next == NULL) 428 goto out_no_thread; 429 mutex_enter(&mi->mi_lock); 430 mi->mi_recovflags |= MI4R_NEED_NEW_SERVER; 431 mutex_exit(&mi->mi_lock); 432 433 if (recovp->rc_lost_rqst != NULL) 434 nfs4_enqueue_lost_rqst(recovp, mi); 435 break; 436 437 case NR_CLIENTID: 438 /* 439 * If the filesystem has been unmounted, punt. 440 */ 441 if (sp == NULL) 442 goto out_no_thread; 443 444 /* 445 * If nobody else is working on the clientid, mark the 446 * clientid as being no longer set. Then mark the specific 447 * filesystem being worked on. 448 */ 449 if (!nfs4_server_in_recovery(sp)) { 450 mutex_enter(&sp->s_lock); 451 sp->s_flags &= ~N4S_CLIENTID_SET; 452 mutex_exit(&sp->s_lock); 453 } 454 ASSERT(nfs_rw_lock_held(&mi->mi_recovlock, RW_READER) || 455 nfs_rw_lock_held(&mi->mi_recovlock, RW_WRITER)); 456 mutex_enter(&mi->mi_lock); 457 mi->mi_recovflags |= MI4R_NEED_CLIENTID; 458 if (recovp->rc_srv_reboot) 459 mi->mi_recovflags |= MI4R_SRV_REBOOT; 460 mutex_exit(&mi->mi_lock); 461 break; 462 463 case NR_OPENFILES: 464 ASSERT(nfs_rw_lock_held(&mi->mi_recovlock, RW_READER) || 465 nfs_rw_lock_held(&mi->mi_recovlock, RW_WRITER)); 466 mutex_enter(&mi->mi_lock); 467 mi->mi_recovflags |= MI4R_REOPEN_FILES; 468 if (recovp->rc_srv_reboot) 469 mi->mi_recovflags |= MI4R_SRV_REBOOT; 470 mutex_exit(&mi->mi_lock); 471 break; 472 473 case NR_WRONGSEC: 474 ASSERT(nfs_rw_lock_held(&mi->mi_recovlock, RW_READER) || 475 nfs_rw_lock_held(&mi->mi_recovlock, RW_WRITER)); 476 mutex_enter(&mi->mi_lock); 477 mi->mi_recovflags |= MI4R_NEED_SECINFO; 478 mutex_exit(&mi->mi_lock); 479 break; 480 481 case NR_EXPIRED: 482 if (vp1 != NULL) 483 recov_badstate(recovp, vp1, NFS4ERR_EXPIRED); 484 if (vp2 != NULL) 485 recov_badstate(recovp, vp2, NFS4ERR_EXPIRED); 486 goto out_no_thread; /* no further recovery possible */ 487 488 case NR_BAD_STATEID: 489 if (vp1 != NULL) 490 recov_badstate(recovp, vp1, NFS4ERR_BAD_STATEID); 491 if (vp2 != NULL) 492 recov_badstate(recovp, vp2, NFS4ERR_BAD_STATEID); 493 goto out_no_thread; /* no further recovery possible */ 494 495 case NR_FHEXPIRED: 496 case NR_BADHANDLE: 497 if (vp1 != NULL) 498 recov_throttle(recovp, vp1); 499 if (vp2 != NULL) 500 recov_throttle(recovp, vp2); 501 /* 502 * Recover the filehandle now, rather than using a 503 * separate thread. We can do this because filehandle 504 * recovery is independent of any other state, and because 505 * we know that we are not competing with the recovery 506 * thread at this time. recov_filehandle will deal with 507 * threads that are competing to recover this filehandle. 508 */ 509 ASSERT(nfs_rw_lock_held(&mi->mi_recovlock, RW_READER) || 510 nfs_rw_lock_held(&mi->mi_recovlock, RW_WRITER)); 511 if (vp1 != NULL) 512 recov_filehandle(recovp->rc_action, mi, vp1); 513 if (vp2 != NULL) 514 recov_filehandle(recovp->rc_action, mi, vp2); 515 goto out_no_thread; /* no further recovery needed */ 516 517 case NR_STALE: 518 /* 519 * NFS4ERR_STALE handling 520 * recov_stale() could set MI4R_NEED_NEW_SERVER to 521 * indicate that we can and should failover. 522 */ 523 ASSERT(nfs_rw_lock_held(&mi->mi_recovlock, RW_READER) || 524 nfs_rw_lock_held(&mi->mi_recovlock, RW_WRITER)); 525 526 if (vp1 != NULL) 527 recov_stale(mi, vp1); 528 if (vp2 != NULL) 529 recov_stale(mi, vp2); 530 mutex_enter(&mi->mi_lock); 531 if ((mi->mi_recovflags & MI4R_NEED_NEW_SERVER) == 0) { 532 mutex_exit(&mi->mi_lock); 533 goto out_no_thread; 534 } 535 mutex_exit(&mi->mi_lock); 536 recovp->rc_action = NR_FAILOVER; 537 goto again; 538 539 case NR_BAD_SEQID: 540 if (recovp->rc_bseqid_rqst) { 541 enqueue_bseqid_rqst(recovp, mi); 542 break; 543 } 544 545 if (vp1 != NULL) 546 recov_badstate(recovp, vp1, NFS4ERR_BAD_SEQID); 547 if (vp2 != NULL) 548 recov_badstate(recovp, vp2, NFS4ERR_BAD_SEQID); 549 goto out_no_thread; /* no further recovery possible */ 550 551 case NR_OLDSTATEID: 552 if (vp1 != NULL) 553 recov_badstate(recovp, vp1, NFS4ERR_OLD_STATEID); 554 if (vp2 != NULL) 555 recov_badstate(recovp, vp2, NFS4ERR_OLD_STATEID); 556 goto out_no_thread; /* no further recovery possible */ 557 558 case NR_GRACE: 559 nfs4_set_grace_wait(mi); 560 goto out_no_thread; /* no further action required for GRACE */ 561 562 case NR_DELAY: 563 if (vp1) 564 nfs4_set_delay_wait(vp1); 565 goto out_no_thread; /* no further action required for DELAY */ 566 567 case NR_LOST_STATE_RQST: 568 case NR_LOST_LOCK: 569 nfs4_enqueue_lost_rqst(recovp, mi); 570 break; 571 572 default: 573 nfs4_queue_event(RE_UNEXPECTED_ACTION, mi, NULL, 574 recovp->rc_action, NULL, NULL, 0, NULL, 0, TAG_NONE, 575 TAG_NONE, 0, 0); 576 goto out_no_thread; 577 } 578 579 /* 580 * If either file recently went through the same recovery, wait 581 * awhile. This is in case there is some sort of bug; we might not 582 * be able to recover properly, but at least we won't bombard the 583 * server with calls, and we won't tie up the client. 584 */ 585 if (vp1 != NULL) 586 recov_throttle(recovp, vp1); 587 if (vp2 != NULL) 588 recov_throttle(recovp, vp2); 589 590 /* 591 * If there's already a recovery thread, don't start another one. 592 */ 593 594 mutex_enter(&mi->mi_lock); 595 if (mi->mi_flags & MI4_RECOV_ACTIV) { 596 mutex_exit(&mi->mi_lock); 597 goto out_no_thread; 598 } 599 mi->mi_flags |= MI4_RECOV_ACTIV; 600 mutex_exit(&mi->mi_lock); 601 NFS4_DEBUG(nfs4_client_recov_debug, (CE_NOTE, 602 "start_recovery: starting new thread for mi %p", (void*)mi)); 603 604 recovp->rc_mi = mi; 605 recovp->rc_vp1 = vp1; 606 if (vp1 != NULL) { 607 ASSERT(VTOMI4(vp1) == mi); 608 VN_HOLD(recovp->rc_vp1); 609 } 610 recovp->rc_vp2 = vp2; 611 if (vp2 != NULL) { 612 ASSERT(VTOMI4(vp2) == mi); 613 VN_HOLD(recovp->rc_vp2); 614 } 615 616 (void) zthread_create(NULL, 0, nfs4_recov_thread, recovp, 0, 617 minclsyspri); 618 return; 619 620 /* not reached by thread creating call */ 621 out_no_thread: 622 mutex_enter(&mi->mi_lock); 623 mi->mi_in_recovery--; 624 cv_broadcast(&mi->mi_cv_in_recov); 625 mutex_exit(&mi->mi_lock); 626 627 VFS_RELE(mi->mi_vfsp); 628 /* 629 * Free up resources that were allocated for us. 630 */ 631 kmem_free(recovp, sizeof (recov_info_t)); 632 } 633 634 static int 635 nfs4_check_srvstub(vnode_t *vp, rnode4_t *rp, nfs4_op_hint_t op) 636 { 637 int err = 0; 638 639 /* 640 * If tuneable does not allow client to cross srv mountpoints and 641 * object is a stub, then check check op hint and return EACCES for 642 * any hint other than access, rddir, getattr, lookup. 643 */ 644 if (rp->r_flags & R4SRVSTUB && op != OH_ACCESS && op != OH_GETACL && 645 op != OH_GETATTR && op != OH_READDIR && op != OH_LOOKUP) { 646 err = EACCES; 647 #ifdef DEBUG 648 NFS4_DEBUG(nfs4_srvmnt_debug, (CE_NOTE, 649 "nfs4_check_srvstub: op=%d err=%d rp=%p vp=%p\n" 650 "va_nod=%llx r_mntd_fid=%llx\n" 651 "sv_fsid=(%llx:%llx) r_srv_fsid=(%llx:%llx)", 652 op, err, (void *)rp, (void *)vp, 653 (u_longlong_t)rp->r_attr.va_nodeid, 654 (u_longlong_t)rp->r_mntd_fid, 655 (u_longlong_t)rp->r_server->sv_fsid.major, 656 (u_longlong_t)rp->r_server->sv_fsid.minor, 657 (u_longlong_t)rp->r_srv_fsid.major, 658 (u_longlong_t)rp->r_srv_fsid.minor)); 659 #endif 660 } 661 662 return (err); 663 } 664 665 static int 666 nfs4_check_recov_err(vnode_t *vp, nfs4_op_hint_t op, 667 nfs4_recov_state_t *rsp, int retry_err_cnt, char *str) 668 { 669 rnode4_t *rp; 670 int error = 0; 671 int exempt; 672 673 if (vp == NULL) 674 return (0); 675 676 exempt = (op == OH_CLOSE || op == OH_LOCKU || op == OH_DELEGRETURN); 677 rp = VTOR4(vp); 678 mutex_enter(&rp->r_statelock); 679 680 /* 681 * If there was a recovery error, then allow op hints "exempt" from 682 * recov errors to retry (currently 3 times). Either r_error or 683 * EIO is returned for non-exempt op hints. 684 * 685 * Error heirarchy: 686 * a) check for R4ERECOVERR 687 * b) check for R4SRVSTUB (only if R4RECOVERR is not set). 688 */ 689 if (rp->r_flags & R4RECOVERR) { 690 if (exempt && rsp->rs_num_retry_despite_err <= 691 nfs4_max_recov_error_retry) { 692 693 /* 694 * Check to make sure that we haven't already inc'd 695 * rs_num_retry_despite_err for current nfs4_start_fop 696 * instance. We don't want to double inc (if we were 697 * called with vp2, then the vp1 call could have 698 * already incremented. 699 */ 700 if (retry_err_cnt == rsp->rs_num_retry_despite_err) 701 rsp->rs_num_retry_despite_err++; 702 703 NFS4_DEBUG(nfs4_client_recov_debug, (CE_NOTE, 704 "nfs4_start_fop: %s %p DEAD, cnt=%d", str, 705 (void *)vp, rsp->rs_num_retry_despite_err)); 706 } else { 707 error = (rp->r_error ? rp->r_error : EIO); 708 /* 709 * An ESTALE error on a non-regular file is not 710 * "sticky". Return the ESTALE error once, but 711 * clear the condition to allow future operations 712 * to go OTW. This will allow the client to 713 * recover if the server has merely unshared then 714 * re-shared the file system. For regular files, 715 * the unshare has destroyed the open state at the 716 * server and we aren't willing to do a reopen (yet). 717 */ 718 if (error == ESTALE && vp->v_type != VREG) { 719 rp->r_flags &= 720 ~(R4RECOVERR|R4RECOVERRP|R4STALE); 721 rp->r_error = 0; 722 error = ESTALE; 723 } 724 NFS4_DEBUG(nfs4_client_recov_debug, (CE_NOTE, 725 "nfs4_start_fop: %s %p DEAD, cnt=%d error=%d", 726 str, (void *)vp, 727 rsp->rs_num_retry_despite_err, error)); 728 } 729 } else { 730 error = nfs4_check_srvstub(vp, rp, op); 731 NFS4_DEBUG(nfs4_client_recov_stub_debug, (CE_NOTE, 732 "nfs4_start_fop: %s %p SRVSTUB, error=%d", str, 733 (void *)vp, error)); 734 } 735 mutex_exit(&rp->r_statelock); 736 return (error); 737 } 738 739 /* 740 * Initial setup code that every operation should call if it might invoke 741 * client recovery. Can block waiting for recovery to finish on a 742 * filesystem. Either vnode ptr can be NULL. 743 * 744 * Returns 0 if there are no outstanding errors. Can return an 745 * errno value under various circumstances (e.g., failed recovery, or 746 * interrupted while waiting for recovery to finish). 747 * 748 * There must be a corresponding call to nfs4_end_op() to free up any locks 749 * or resources allocated by this call (assuming this call succeeded), 750 * using the same rsp that's passed in here. 751 * 752 * The open and lock seqid synchronization must be stopped before calling this 753 * function, as it could lead to deadlock when trying to reopen a file or 754 * reclaim a lock. The synchronization is obtained with calls to: 755 * nfs4_start_open_seqid_sync() 756 * nfs4_start_lock_seqid_sync() 757 * 758 * *startrecovp is set TRUE if the caller should not bother with the 759 * over-the-wire call, and just initiate recovery for the given request. 760 * This is typically used for state-releasing ops if the filesystem has 761 * been forcibly unmounted. startrecovp may be NULL for 762 * non-state-releasing ops. 763 */ 764 765 int 766 nfs4_start_fop(mntinfo4_t *mi, vnode_t *vp1, vnode_t *vp2, nfs4_op_hint_t op, 767 nfs4_recov_state_t *rsp, bool_t *startrecovp) 768 { 769 int error = 0, rerr_cnt; 770 nfs4_server_t *sp = NULL; 771 nfs4_server_t *tsp; 772 nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS }; 773 time_t droplock_time; 774 #ifdef DEBUG 775 void *fop_caller; 776 #endif 777 778 ASSERT(vp1 == NULL || vp1->v_vfsp == mi->mi_vfsp); 779 ASSERT(vp2 == NULL || vp2->v_vfsp == mi->mi_vfsp); 780 781 #ifdef DEBUG 782 if ((fop_caller = tsd_get(nfs4_tsd_key)) != NULL) { 783 cmn_err(CE_PANIC, "Missing nfs4_end_fop: last caller %p", 784 fop_caller); 785 } 786 (void) tsd_set(nfs4_tsd_key, caller()); 787 #endif 788 789 rsp->rs_sp = NULL; 790 rsp->rs_flags &= ~NFS4_RS_RENAME_HELD; 791 rerr_cnt = rsp->rs_num_retry_despite_err; 792 793 /* 794 * Process the items that may delay() based on server response 795 */ 796 error = nfs4_wait_for_grace(mi, rsp); 797 if (error) 798 goto out; 799 800 if (vp1 != NULL) { 801 error = nfs4_wait_for_delay(vp1, rsp); 802 if (error) 803 goto out; 804 } 805 806 /* Wait for a delegation recall to complete. */ 807 808 error = wait_for_recall(vp1, vp2, op, rsp); 809 if (error) 810 goto out; 811 812 /* 813 * Wait for any current recovery actions to finish. Note that a 814 * recovery thread can still start up after wait_for_recovery() 815 * finishes. We don't block out recovery operations until we 816 * acquire s_recovlock and mi_recovlock. 817 */ 818 error = wait_for_recovery(mi, op); 819 if (error) 820 goto out; 821 822 /* 823 * Check to see if the rnode is already marked with a 824 * recovery error. If so, return it immediately. But 825 * always pass CLOSE, LOCKU, and DELEGRETURN so we can 826 * clean up state on the server. 827 */ 828 829 if (vp1 != NULL) { 830 if (error = nfs4_check_recov_err(vp1, op, rsp, rerr_cnt, "vp1")) 831 goto out; 832 nfs4_check_remap(mi, vp1, NFS4_REMAP_CKATTRS, &e); 833 } 834 835 if (vp2 != NULL) { 836 if (error = nfs4_check_recov_err(vp2, op, rsp, rerr_cnt, "vp2")) 837 goto out; 838 nfs4_check_remap(mi, vp2, NFS4_REMAP_CKATTRS, &e); 839 } 840 841 /* 842 * The lock order calls for us to acquire s_recovlock before 843 * mi_recovlock, but we have to hold mi_recovlock to look up sp (to 844 * prevent races with the failover/migration code). So acquire 845 * mi_recovlock, look up sp, drop mi_recovlock, acquire 846 * s_recovlock and mi_recovlock, then verify that sp is still the 847 * right object. XXX Can we find a simpler way to deal with this? 848 */ 849 if (nfs_rw_enter_sig(&mi->mi_recovlock, RW_READER, 850 mi->mi_flags & MI4_INT)) { 851 error = EINTR; 852 goto out; 853 } 854 get_sp: 855 sp = find_nfs4_server(mi); 856 if (sp != NULL) { 857 sp->s_otw_call_count++; 858 droplock_time = gethrestime_sec(); 859 mutex_exit(&sp->s_lock); 860 } 861 nfs_rw_exit(&mi->mi_recovlock); 862 863 if (sp != NULL) { 864 if (nfs_rw_enter_sig(&sp->s_recovlock, RW_READER, 865 mi->mi_flags & MI4_INT)) { 866 error = EINTR; 867 goto out; 868 } 869 } 870 if (nfs_rw_enter_sig(&mi->mi_recovlock, RW_READER, 871 mi->mi_flags & MI4_INT)) { 872 if (sp != NULL) 873 nfs_rw_exit(&sp->s_recovlock); 874 error = EINTR; 875 goto out; 876 } 877 /* 878 * If the mntinfo4_t hasn't changed nfs4_sever_ts then 879 * there's no point in double checking to make sure it 880 * has switched. 881 * XXX is there a better lock to use for mi_srvsettime 882 * instead of s_lock? mi_recovlock as READER would 883 * be perfect. 884 */ 885 if (sp != NULL) 886 mutex_enter(&sp->s_lock); 887 if (sp == NULL || droplock_time < mi->mi_srvsettime) { 888 if (sp != NULL) 889 mutex_exit(&sp->s_lock); 890 tsp = find_nfs4_server(mi); 891 if (tsp != sp) { 892 /* try again */ 893 if (tsp != NULL) { 894 mutex_exit(&tsp->s_lock); 895 nfs4_server_rele(tsp); 896 tsp = NULL; 897 } 898 if (sp != NULL) { 899 nfs_rw_exit(&sp->s_recovlock); 900 mutex_enter(&sp->s_lock); 901 sp->s_otw_call_count--; 902 mutex_exit(&sp->s_lock); 903 nfs4_server_rele(sp); 904 sp = NULL; 905 } 906 goto get_sp; 907 } else { 908 if (tsp != NULL) { 909 mutex_exit(&tsp->s_lock); 910 nfs4_server_rele(tsp); 911 tsp = NULL; 912 } 913 } 914 } else { 915 if (sp != NULL) 916 mutex_exit(&sp->s_lock); 917 } 918 919 if (sp != NULL) { 920 rsp->rs_sp = sp; 921 } 922 923 /* 924 * If the fileystem uses volatile filehandles, obtain a lock so 925 * that we synchronize with renames. Exception: mount operations 926 * can change mi_fh_expire_type, which could be a problem, since 927 * the end_op code needs to be consistent with the start_op code 928 * about mi_rename_lock. Since mounts don't compete with renames, 929 * it's simpler to just not acquire the rename lock for mounts. 930 */ 931 if (NFS4_VOLATILE_FH(mi) && op != OH_MOUNT) { 932 if (nfs_rw_enter_sig(&mi->mi_rename_lock, 933 op == OH_VFH_RENAME ? RW_WRITER : RW_READER, 934 mi->mi_flags & MI4_INT)) { 935 nfs_rw_exit(&mi->mi_recovlock); 936 if (sp != NULL) 937 nfs_rw_exit(&sp->s_recovlock); 938 error = EINTR; 939 goto out; 940 } 941 rsp->rs_flags |= NFS4_RS_RENAME_HELD; 942 } 943 944 if (OH_IS_STATE_RELE(op)) { 945 /* 946 * For forced unmount, letting the request proceed will 947 * almost always delay response to the user, so hand it off 948 * to the recovery thread. For exiting lwp's, we don't 949 * have a good way to tell if the request will hang. We 950 * generally want processes to handle their own requests so 951 * that they can be done in parallel, but if there is 952 * already a recovery thread, hand the request off to it. 953 * This will improve user response at no cost to overall 954 * system throughput. For zone shutdown, we'd prefer 955 * the recovery thread to handle this as well. 956 */ 957 ASSERT(startrecovp != NULL); 958 mutex_enter(&mi->mi_lock); 959 if (FS_OR_ZONE_GONE4(mi->mi_vfsp)) 960 *startrecovp = TRUE; 961 else if ((curthread->t_proc_flag & TP_LWPEXIT) && 962 (mi->mi_flags & MI4_RECOV_ACTIV)) 963 *startrecovp = TRUE; 964 else 965 *startrecovp = FALSE; 966 mutex_exit(&mi->mi_lock); 967 } else 968 if (startrecovp != NULL) 969 *startrecovp = FALSE; 970 971 ASSERT(error == 0); 972 return (error); 973 974 out: 975 ASSERT(error != 0); 976 if (sp != NULL) { 977 mutex_enter(&sp->s_lock); 978 sp->s_otw_call_count--; 979 mutex_exit(&sp->s_lock); 980 nfs4_server_rele(sp); 981 rsp->rs_sp = NULL; 982 } 983 nfs4_end_op_recall(vp1, vp2, rsp); 984 985 #ifdef DEBUG 986 (void) tsd_set(nfs4_tsd_key, NULL); 987 #endif 988 return (error); 989 } 990 991 /* 992 * It is up to the caller to determine if rsp->rs_sp being NULL 993 * is detrimental or not. 994 */ 995 int 996 nfs4_start_op(mntinfo4_t *mi, vnode_t *vp1, vnode_t *vp2, 997 nfs4_recov_state_t *rsp) 998 { 999 ASSERT(rsp->rs_num_retry_despite_err == 0); 1000 rsp->rs_num_retry_despite_err = 0; 1001 return (nfs4_start_fop(mi, vp1, vp2, OH_OTHER, rsp, NULL)); 1002 } 1003 1004 /* 1005 * Release any resources acquired by nfs4_start_op(). 1006 * 'sp' should be the nfs4_server pointer returned by nfs4_start_op(). 1007 * 1008 * The operation hint is used to avoid a deadlock by bypassing delegation 1009 * return logic for writes, which are done while returning a delegation. 1010 */ 1011 1012 void 1013 nfs4_end_fop(mntinfo4_t *mi, vnode_t *vp1, vnode_t *vp2, nfs4_op_hint_t op, 1014 nfs4_recov_state_t *rsp, bool_t needs_recov) 1015 { 1016 nfs4_server_t *sp = rsp->rs_sp; 1017 rnode4_t *rp = NULL; 1018 1019 #ifdef lint 1020 /* 1021 * The op hint isn't used any more, but might be in 1022 * the future. 1023 */ 1024 op = op; 1025 #endif 1026 1027 #ifdef DEBUG 1028 ASSERT(tsd_get(nfs4_tsd_key) != NULL); 1029 (void) tsd_set(nfs4_tsd_key, NULL); 1030 #endif 1031 1032 nfs4_end_op_recall(vp1, vp2, rsp); 1033 1034 if (rsp->rs_flags & NFS4_RS_RENAME_HELD) 1035 nfs_rw_exit(&mi->mi_rename_lock); 1036 1037 if (!needs_recov) { 1038 if (rsp->rs_flags & NFS4_RS_DELAY_MSG) { 1039 /* may need to clear the delay interval */ 1040 if (vp1 != NULL) { 1041 rp = VTOR4(vp1); 1042 mutex_enter(&rp->r_statelock); 1043 rp->r_delay_interval = 0; 1044 mutex_exit(&rp->r_statelock); 1045 } 1046 } 1047 rsp->rs_flags &= ~(NFS4_RS_GRACE_MSG|NFS4_RS_DELAY_MSG); 1048 } 1049 1050 /* 1051 * If the corresponding nfs4_start_op() found a sp, 1052 * then there must still be a sp. 1053 */ 1054 if (sp != NULL) { 1055 nfs_rw_exit(&mi->mi_recovlock); 1056 nfs_rw_exit(&sp->s_recovlock); 1057 mutex_enter(&sp->s_lock); 1058 sp->s_otw_call_count--; 1059 cv_broadcast(&sp->s_cv_otw_count); 1060 mutex_exit(&sp->s_lock); 1061 nfs4_server_rele(sp); 1062 } else { 1063 nfs_rw_exit(&mi->mi_recovlock); 1064 } 1065 } 1066 1067 void 1068 nfs4_end_op(mntinfo4_t *mi, vnode_t *vp1, vnode_t *vp2, 1069 nfs4_recov_state_t *rsp, bool_t needrecov) 1070 { 1071 nfs4_end_fop(mi, vp1, vp2, OH_OTHER, rsp, needrecov); 1072 } 1073 1074 /* 1075 * If the filesystem is going through client recovery, block until 1076 * finished. 1077 * Exceptions: 1078 * - state-releasing ops (CLOSE, LOCKU, DELEGRETURN) are allowed to proceed 1079 * if the filesystem has been forcibly unmounted or the lwp is exiting. 1080 * 1081 * Return value: 1082 * - 0 if no errors 1083 * - EINTR if the call was interrupted 1084 * - EIO if the filesystem has been forcibly unmounted (non-state-releasing 1085 * op) 1086 * - the errno value from the recovery thread, if recovery failed 1087 */ 1088 1089 static int 1090 wait_for_recovery(mntinfo4_t *mi, nfs4_op_hint_t op_hint) 1091 { 1092 int error = 0; 1093 1094 mutex_enter(&mi->mi_lock); 1095 1096 while (mi->mi_recovflags != 0) { 1097 klwp_t *lwp = ttolwp(curthread); 1098 1099 if (mi->mi_flags & MI4_RECOV_FAIL) 1100 break; 1101 if (mi->mi_vfsp->vfs_flag & VFS_UNMOUNTED) 1102 break; 1103 if (OH_IS_STATE_RELE(op_hint) && 1104 (curthread->t_proc_flag & TP_LWPEXIT)) 1105 break; 1106 1107 if (lwp != NULL) 1108 lwp->lwp_nostop++; 1109 /* XXX - use different cv? */ 1110 if (cv_wait_sig(&mi->mi_failover_cv, &mi->mi_lock) == 0) { 1111 error = EINTR; 1112 if (lwp != NULL) 1113 lwp->lwp_nostop--; 1114 break; 1115 } 1116 if (lwp != NULL) 1117 lwp->lwp_nostop--; 1118 } 1119 1120 if (mi->mi_flags & MI4_RECOV_FAIL) { 1121 NFS4_DEBUG(nfs4_client_recov_debug, (CE_NOTE, 1122 "wait_for_recovery: fail since RECOV FAIL")); 1123 error = mi->mi_error; 1124 } else if ((mi->mi_vfsp->vfs_flag & VFS_UNMOUNTED) && 1125 !OH_IS_STATE_RELE(op_hint)) { 1126 NFS4_DEBUG(nfs4_client_recov_debug, (CE_NOTE, 1127 "wait_for_recovery: forced unmount")); 1128 error = EIO; 1129 } 1130 1131 mutex_exit(&mi->mi_lock); 1132 1133 return (error); 1134 } 1135 1136 /* 1137 * If the client received NFS4ERR_GRACE for this particular mount, 1138 * the client blocks here until it is time to try again. 1139 * 1140 * Return value: 1141 * - 0 if wait was successful 1142 * - EINTR if the call was interrupted 1143 */ 1144 1145 int 1146 nfs4_wait_for_grace(mntinfo4_t *mi, nfs4_recov_state_t *rsp) 1147 { 1148 int error = 0; 1149 time_t curtime, time_to_wait; 1150 1151 /* do a unprotected check to reduce mi_lock contention */ 1152 if (mi->mi_grace_wait != 0) { 1153 mutex_enter(&mi->mi_lock); 1154 1155 if (mi->mi_grace_wait != 0) { 1156 if (!(rsp->rs_flags & NFS4_RS_GRACE_MSG)) 1157 rsp->rs_flags |= NFS4_RS_GRACE_MSG; 1158 1159 curtime = gethrestime_sec(); 1160 1161 if (curtime < mi->mi_grace_wait) { 1162 1163 time_to_wait = mi->mi_grace_wait - curtime; 1164 1165 mutex_exit(&mi->mi_lock); 1166 1167 delay(SEC_TO_TICK(time_to_wait)); 1168 1169 curtime = gethrestime_sec(); 1170 1171 mutex_enter(&mi->mi_lock); 1172 1173 if (curtime >= mi->mi_grace_wait) 1174 mi->mi_grace_wait = 0; 1175 } else { 1176 mi->mi_grace_wait = 0; 1177 } 1178 } 1179 mutex_exit(&mi->mi_lock); 1180 } 1181 1182 return (error); 1183 } 1184 1185 /* 1186 * If the client received NFS4ERR_DELAY for an operation on a vnode, 1187 * the client blocks here until it is time to try again. 1188 * 1189 * Return value: 1190 * - 0 if wait was successful 1191 * - EINTR if the call was interrupted 1192 */ 1193 1194 int 1195 nfs4_wait_for_delay(vnode_t *vp, nfs4_recov_state_t *rsp) 1196 { 1197 int error = 0; 1198 time_t curtime, time_to_wait; 1199 rnode4_t *rp; 1200 1201 ASSERT(vp != NULL); 1202 1203 rp = VTOR4(vp); 1204 1205 /* do a unprotected check to reduce r_statelock contention */ 1206 if (rp->r_delay_wait != 0) { 1207 mutex_enter(&rp->r_statelock); 1208 1209 if (rp->r_delay_wait != 0) { 1210 1211 if (!(rsp->rs_flags & NFS4_RS_DELAY_MSG)) { 1212 rsp->rs_flags |= NFS4_RS_DELAY_MSG; 1213 nfs4_mi_kstat_inc_delay(VTOMI4(vp)); 1214 } 1215 1216 curtime = gethrestime_sec(); 1217 1218 if (curtime < rp->r_delay_wait) { 1219 1220 time_to_wait = rp->r_delay_wait - curtime; 1221 1222 mutex_exit(&rp->r_statelock); 1223 1224 delay(SEC_TO_TICK(time_to_wait)); 1225 1226 curtime = gethrestime_sec(); 1227 1228 mutex_enter(&rp->r_statelock); 1229 1230 if (curtime >= rp->r_delay_wait) 1231 rp->r_delay_wait = 0; 1232 } else { 1233 rp->r_delay_wait = 0; 1234 } 1235 } 1236 mutex_exit(&rp->r_statelock); 1237 } 1238 1239 return (error); 1240 } 1241 1242 /* 1243 * The recovery thread. 1244 */ 1245 1246 static void 1247 nfs4_recov_thread(recov_info_t *recovp) 1248 { 1249 mntinfo4_t *mi = recovp->rc_mi; 1250 nfs4_server_t *sp; 1251 int done = 0, error = 0; 1252 bool_t recov_fail = FALSE; 1253 callb_cpr_t cpr_info; 1254 kmutex_t cpr_lock; 1255 1256 nfs4_queue_event(RE_START, mi, NULL, mi->mi_recovflags, 1257 recovp->rc_vp1, recovp->rc_vp2, 0, NULL, 0, TAG_NONE, TAG_NONE, 1258 0, 0); 1259 1260 mutex_init(&cpr_lock, NULL, MUTEX_DEFAULT, NULL); 1261 CALLB_CPR_INIT(&cpr_info, &cpr_lock, callb_generic_cpr, "nfsv4Recov"); 1262 1263 mutex_enter(&mi->mi_lock); 1264 mi->mi_recovthread = curthread; 1265 mutex_exit(&mi->mi_lock); 1266 1267 /* 1268 * We don't really need protection here against failover or 1269 * migration, since the current thread is the one that would make 1270 * any changes, but hold mi_recovlock anyway for completeness (and 1271 * to satisfy any ASSERTs). 1272 */ 1273 (void) nfs_rw_enter_sig(&mi->mi_recovlock, RW_READER, 0); 1274 sp = find_nfs4_server(mi); 1275 if (sp != NULL) 1276 mutex_exit(&sp->s_lock); 1277 nfs_rw_exit(&mi->mi_recovlock); 1278 1279 /* 1280 * Do any necessary recovery, based on the information in recovp 1281 * and any recovery flags. 1282 */ 1283 1284 do { 1285 mutex_enter(&mi->mi_lock); 1286 if (FS_OR_ZONE_GONE4(mi->mi_vfsp)) { 1287 bool_t activesrv; 1288 1289 NFS4_DEBUG(nfs4_client_recov_debug && 1290 mi->mi_vfsp->vfs_flag & VFS_UNMOUNTED, (CE_NOTE, 1291 "nfs4_recov_thread: file system has been " 1292 "unmounted")); 1293 NFS4_DEBUG(nfs4_client_recov_debug && 1294 zone_status_get(curproc->p_zone) >= 1295 ZONE_IS_SHUTTING_DOWN, (CE_NOTE, 1296 "nfs4_recov_thread: zone shutting down")); 1297 /* 1298 * If the server has lost its state for us and 1299 * the filesystem is unmounted, then the filesystem 1300 * can be tossed, even if there are lost lock or 1301 * lost state calls in the recovery queue. 1302 */ 1303 if (mi->mi_recovflags & 1304 (MI4R_NEED_CLIENTID | MI4R_REOPEN_FILES)) { 1305 NFS4_DEBUG(nfs4_client_recov_debug, (CE_NOTE, 1306 "nfs4_recov_thread: bailing out")); 1307 mi->mi_flags |= MI4_RECOV_FAIL; 1308 mi->mi_error = recovp->rc_error; 1309 recov_fail = TRUE; 1310 } 1311 /* 1312 * We don't know if the server has any state for 1313 * us, and the filesystem has been unmounted. If 1314 * there are "lost state" recovery items, keep 1315 * trying to process them until there are no more 1316 * mounted filesystems for the server. Otherwise, 1317 * bail out. The reason we don't mark the 1318 * filesystem as failing recovery is in case we 1319 * have to do "lost state" recovery later (e.g., a 1320 * user process exits). 1321 */ 1322 if (!(mi->mi_recovflags & MI4R_LOST_STATE)) { 1323 recov_done(mi, recovp); 1324 mutex_exit(&mi->mi_lock); 1325 break; 1326 } 1327 mutex_exit(&mi->mi_lock); 1328 1329 if (sp == NULL) 1330 activesrv = FALSE; 1331 else { 1332 mutex_enter(&sp->s_lock); 1333 activesrv = nfs4_fs_active(sp); 1334 } 1335 if (!activesrv) { 1336 NFS4_DEBUG(nfs4_client_recov_debug, (CE_NOTE, 1337 "no active fs for server %p", 1338 (void *)sp)); 1339 mutex_enter(&mi->mi_lock); 1340 mi->mi_flags |= MI4_RECOV_FAIL; 1341 mi->mi_error = recovp->rc_error; 1342 mutex_exit(&mi->mi_lock); 1343 recov_fail = TRUE; 1344 if (sp != NULL) { 1345 /* 1346 * Mark the server instance as 1347 * dead, so that nobody will attach 1348 * a new filesystem. 1349 */ 1350 nfs4_mark_srv_dead(sp); 1351 } 1352 } 1353 if (sp != NULL) 1354 mutex_exit(&sp->s_lock); 1355 } else { 1356 mutex_exit(&mi->mi_lock); 1357 } 1358 1359 /* 1360 * Check if we need to select a new server for a 1361 * failover. Choosing a new server will force at 1362 * least a check of the clientid. 1363 */ 1364 mutex_enter(&mi->mi_lock); 1365 if (!recov_fail && 1366 (mi->mi_recovflags & MI4R_NEED_NEW_SERVER)) { 1367 mutex_exit(&mi->mi_lock); 1368 recov_newserver(recovp, &sp, &recov_fail); 1369 } else 1370 mutex_exit(&mi->mi_lock); 1371 1372 /* 1373 * Check if we need to recover the clientid. This 1374 * must be done before file and lock recovery, and it 1375 * potentially affects the recovery threads for other 1376 * filesystems, so it gets special treatment. 1377 */ 1378 if (sp != NULL && recov_fail == FALSE) { 1379 mutex_enter(&sp->s_lock); 1380 if (!(sp->s_flags & N4S_CLIENTID_SET)) { 1381 mutex_exit(&sp->s_lock); 1382 recov_clientid(recovp, sp); 1383 } else { 1384 /* 1385 * Unset this flag in case another recovery 1386 * thread successfully recovered the clientid 1387 * for us already. 1388 */ 1389 mutex_enter(&mi->mi_lock); 1390 mi->mi_recovflags &= ~MI4R_NEED_CLIENTID; 1391 mutex_exit(&mi->mi_lock); 1392 mutex_exit(&sp->s_lock); 1393 } 1394 } 1395 1396 /* 1397 * Check if we need to get the security information. 1398 */ 1399 mutex_enter(&mi->mi_lock); 1400 if ((mi->mi_recovflags & MI4R_NEED_SECINFO) && 1401 !(mi->mi_flags & MI4_RECOV_FAIL)) { 1402 mutex_exit(&mi->mi_lock); 1403 (void) nfs_rw_enter_sig(&mi->mi_recovlock, 1404 RW_WRITER, 0); 1405 error = nfs4_secinfo_recov(recovp->rc_mi, 1406 recovp->rc_vp1, recovp->rc_vp2); 1407 /* 1408 * If error, nothing more can be done, stop 1409 * the recovery. 1410 */ 1411 if (error) { 1412 mutex_enter(&mi->mi_lock); 1413 mi->mi_flags |= MI4_RECOV_FAIL; 1414 mi->mi_error = recovp->rc_error; 1415 mutex_exit(&mi->mi_lock); 1416 nfs4_queue_event(RE_WRONGSEC, mi, NULL, 1417 error, recovp->rc_vp1, recovp->rc_vp2, 1418 0, NULL, 0, TAG_NONE, TAG_NONE, 0, 0); 1419 } 1420 nfs_rw_exit(&mi->mi_recovlock); 1421 } else 1422 mutex_exit(&mi->mi_lock); 1423 1424 /* 1425 * Check if there's a bad seqid to recover. 1426 */ 1427 mutex_enter(&mi->mi_lock); 1428 if ((mi->mi_recovflags & MI4R_BAD_SEQID) && 1429 !(mi->mi_flags & MI4_RECOV_FAIL)) { 1430 mutex_exit(&mi->mi_lock); 1431 (void) nfs_rw_enter_sig(&mi->mi_recovlock, 1432 RW_WRITER, 0); 1433 recov_bad_seqid(recovp); 1434 nfs_rw_exit(&mi->mi_recovlock); 1435 } else 1436 mutex_exit(&mi->mi_lock); 1437 1438 /* 1439 * Next check for recovery that affects the entire 1440 * filesystem. 1441 */ 1442 if (sp != NULL) { 1443 mutex_enter(&mi->mi_lock); 1444 if ((mi->mi_recovflags & MI4R_REOPEN_FILES) && 1445 !(mi->mi_flags & MI4_RECOV_FAIL)) { 1446 mutex_exit(&mi->mi_lock); 1447 recov_openfiles(recovp, sp); 1448 } else 1449 mutex_exit(&mi->mi_lock); 1450 } 1451 1452 /* 1453 * Send any queued state recovery requests. 1454 */ 1455 mutex_enter(&mi->mi_lock); 1456 if (sp != NULL && 1457 (mi->mi_recovflags & MI4R_LOST_STATE) && 1458 !(mi->mi_flags & MI4_RECOV_FAIL)) { 1459 mutex_exit(&mi->mi_lock); 1460 (void) nfs_rw_enter_sig(&mi->mi_recovlock, 1461 RW_WRITER, 0); 1462 nfs4_resend_lost_rqsts(recovp, sp); 1463 if (list_head(&mi->mi_lost_state) == NULL) { 1464 /* done */ 1465 mutex_enter(&mi->mi_lock); 1466 mi->mi_recovflags &= ~MI4R_LOST_STATE; 1467 mutex_exit(&mi->mi_lock); 1468 } 1469 nfs_rw_exit(&mi->mi_recovlock); 1470 } else { 1471 mutex_exit(&mi->mi_lock); 1472 } 1473 1474 /* 1475 * See if there is anything more to do. If not, announce 1476 * that we are done and exit. 1477 * 1478 * Need mi_recovlock to keep 'sp' valid. Must grab 1479 * mi_recovlock before mi_lock to preserve lock ordering. 1480 */ 1481 (void) nfs_rw_enter_sig(&mi->mi_recovlock, RW_READER, 0); 1482 mutex_enter(&mi->mi_lock); 1483 if ((mi->mi_recovflags & ~MI4R_SRV_REBOOT) == 0 || 1484 (mi->mi_flags & MI4_RECOV_FAIL)) { 1485 list_t local_lost_state; 1486 nfs4_lost_rqst_t *lrp; 1487 1488 /* 1489 * We need to remove the lost requests before we 1490 * unmark the mi as no longer doing recovery to 1491 * avoid a race with a new thread putting new lost 1492 * requests on the same mi (and the going away 1493 * thread would remove the new lost requests). 1494 * 1495 * Move the lost requests to a local list since 1496 * nfs4_remove_lost_rqst() drops mi_lock, and 1497 * dropping the mi_lock would make our check to 1498 * see if recovery is done no longer valid. 1499 */ 1500 list_create(&local_lost_state, 1501 sizeof (nfs4_lost_rqst_t), 1502 offsetof(nfs4_lost_rqst_t, lr_node)); 1503 list_move_tail(&local_lost_state, &mi->mi_lost_state); 1504 1505 done = 1; 1506 recov_done(mi, recovp); 1507 mutex_exit(&mi->mi_lock); 1508 /* 1509 * Now officially free the "moved" 1510 * lost requests. 1511 */ 1512 while ((lrp = list_head(&local_lost_state)) != NULL) { 1513 list_remove(&local_lost_state, lrp); 1514 nfs4_free_lost_rqst(lrp, sp); 1515 } 1516 list_destroy(&local_lost_state); 1517 } else 1518 mutex_exit(&mi->mi_lock); 1519 nfs_rw_exit(&mi->mi_recovlock); 1520 1521 /* 1522 * If the filesystem has been forcibly unmounted, there is 1523 * probably no point in retrying immediately. Furthermore, 1524 * there might be user processes waiting for a chance to 1525 * queue up "lost state" requests, so that they can exit. 1526 * So pause here for a moment. Same logic for zone shutdown. 1527 */ 1528 if (!done && FS_OR_ZONE_GONE4(mi->mi_vfsp)) { 1529 mutex_enter(&mi->mi_lock); 1530 cv_broadcast(&mi->mi_failover_cv); 1531 mutex_exit(&mi->mi_lock); 1532 delay(SEC_TO_TICK(nfs4_unmount_delay)); 1533 } 1534 1535 } while (!done); 1536 1537 mutex_enter(&mi->mi_lock); 1538 mi->mi_in_recovery--; 1539 cv_broadcast(&mi->mi_cv_in_recov); 1540 mutex_exit(&mi->mi_lock); 1541 1542 if (sp != NULL) 1543 nfs4_server_rele(sp); 1544 1545 /* 1546 * Return all recalled delegations 1547 */ 1548 nfs4_dlistclean(); 1549 1550 /* 1551 * Free up resources that were allocated for us. 1552 */ 1553 if (recovp->rc_vp1 != NULL) 1554 VN_RELE(recovp->rc_vp1); 1555 if (recovp->rc_vp2 != NULL) 1556 VN_RELE(recovp->rc_vp2); 1557 VFS_RELE(mi->mi_vfsp); 1558 kmem_free(recovp, sizeof (recov_info_t)); 1559 mutex_enter(&cpr_lock); 1560 CALLB_CPR_EXIT(&cpr_info); 1561 mutex_destroy(&cpr_lock); 1562 zthread_exit(); 1563 } 1564 1565 /* 1566 * Log the end of recovery and notify any waiting threads. 1567 */ 1568 1569 static void 1570 recov_done(mntinfo4_t *mi, recov_info_t *recovp) 1571 { 1572 1573 ASSERT(MUTEX_HELD(&mi->mi_lock)); 1574 1575 nfs4_queue_event(RE_END, mi, NULL, 0, recovp->rc_vp1, 1576 recovp->rc_vp2, 0, NULL, 0, TAG_NONE, TAG_NONE, 0, 0); 1577 mi->mi_recovthread = NULL; 1578 mi->mi_flags &= ~MI4_RECOV_ACTIV; 1579 mi->mi_recovflags &= ~MI4R_SRV_REBOOT; 1580 cv_broadcast(&mi->mi_failover_cv); 1581 } 1582 1583 /* 1584 * State-specific recovery routines, by state. 1585 */ 1586 1587 /* 1588 * Failover. 1589 * 1590 * Replaces *spp with a reference to the new server, which must 1591 * eventually be freed. 1592 */ 1593 1594 static void 1595 recov_newserver(recov_info_t *recovp, nfs4_server_t **spp, bool_t *recov_fail) 1596 { 1597 mntinfo4_t *mi = recovp->rc_mi; 1598 servinfo4_t *svp = NULL; 1599 nfs4_server_t *osp = *spp; 1600 CLIENT *cl; 1601 enum clnt_stat status; 1602 struct timeval tv; 1603 int error; 1604 int oncethru = 0; 1605 rnode4_t *rp; 1606 int index; 1607 nfs_fh4 fh; 1608 char *snames; 1609 size_t len; 1610 1611 (void) nfs_rw_enter_sig(&mi->mi_recovlock, RW_WRITER, 0); 1612 1613 tv.tv_sec = 2; 1614 tv.tv_usec = 0; 1615 1616 #ifdef lint 1617 /* 1618 * Lint can't follow the logic, so thinks that snames and len 1619 * can be used before being set. They can't, but lint can't 1620 * figure it out. To address the lint warning, initialize 1621 * snames and len for lint. 1622 */ 1623 snames = NULL; 1624 len = 0; 1625 #endif 1626 1627 /* 1628 * Ping the null NFS procedure of every server in 1629 * the list until one responds. We always start 1630 * at the head of the list and always skip the one 1631 * that is current, since it's caused us a problem. 1632 */ 1633 while (svp == NULL) { 1634 for (svp = mi->mi_servers; svp; svp = svp->sv_next) { 1635 1636 mutex_enter(&mi->mi_lock); 1637 if (FS_OR_ZONE_GONE4(mi->mi_vfsp)) { 1638 mi->mi_flags |= MI4_RECOV_FAIL; 1639 mutex_exit(&mi->mi_lock); 1640 (void) nfs_rw_exit(&mi->mi_recovlock); 1641 *recov_fail = TRUE; 1642 if (oncethru) 1643 kmem_free(snames, len); 1644 return; 1645 } 1646 mutex_exit(&mi->mi_lock); 1647 1648 (void) nfs_rw_enter_sig(&svp->sv_lock, RW_READER, 0); 1649 if (svp->sv_flags & SV4_NOTINUSE) { 1650 nfs_rw_exit(&svp->sv_lock); 1651 continue; 1652 } 1653 nfs_rw_exit(&svp->sv_lock); 1654 1655 if (!oncethru && svp == mi->mi_curr_serv) 1656 continue; 1657 1658 error = clnt_tli_kcreate(svp->sv_knconf, &svp->sv_addr, 1659 NFS_PROGRAM, NFS_V4, 0, 1, CRED(), &cl); 1660 if (error) 1661 continue; 1662 1663 if (!(mi->mi_flags & MI4_INT)) 1664 cl->cl_nosignal = TRUE; 1665 status = CLNT_CALL(cl, RFS_NULL, xdr_void, NULL, 1666 xdr_void, NULL, tv); 1667 if (!(mi->mi_flags & MI4_INT)) 1668 cl->cl_nosignal = FALSE; 1669 AUTH_DESTROY(cl->cl_auth); 1670 CLNT_DESTROY(cl); 1671 if (status == RPC_SUCCESS) { 1672 nfs4_queue_event(RE_FAILOVER, mi, 1673 svp == mi->mi_curr_serv ? NULL : 1674 svp->sv_hostname, 0, NULL, NULL, 0, 1675 NULL, 0, TAG_NONE, TAG_NONE, 0, 0); 1676 break; 1677 } 1678 } 1679 1680 if (svp == NULL) { 1681 if (!oncethru) { 1682 snames = nfs4_getsrvnames(mi, &len); 1683 nfs4_queue_fact(RF_SRVS_NOT_RESPOND, mi, 1684 0, 0, 0, FALSE, snames, 0, NULL); 1685 oncethru = 1; 1686 } 1687 delay(hz); 1688 } 1689 } 1690 1691 if (oncethru) { 1692 nfs4_queue_fact(RF_SRVS_OK, mi, 0, 0, 0, FALSE, snames, 1693 0, NULL); 1694 kmem_free(snames, len); 1695 } 1696 1697 #if DEBUG 1698 (void) nfs_rw_enter_sig(&svp->sv_lock, RW_READER, 0); 1699 ASSERT((svp->sv_flags & SV4_NOTINUSE) == 0); 1700 nfs_rw_exit(&svp->sv_lock); 1701 #endif 1702 1703 mutex_enter(&mi->mi_lock); 1704 mi->mi_recovflags &= ~MI4R_NEED_NEW_SERVER; 1705 if (svp != mi->mi_curr_serv) { 1706 servinfo4_t *osvp = mi->mi_curr_serv; 1707 1708 mutex_exit(&mi->mi_lock); 1709 1710 /* 1711 * Update server-dependent fields in the root vnode. 1712 */ 1713 index = rtable4hash(mi->mi_rootfh); 1714 rw_enter(&rtable4[index].r_lock, RW_WRITER); 1715 1716 rp = r4find(&rtable4[index], mi->mi_rootfh, mi->mi_vfsp); 1717 if (rp != NULL) { 1718 NFS4_DEBUG(nfs4_client_failover_debug, (CE_NOTE, 1719 "recov_newserver: remapping %s", rnode4info(rp))); 1720 mutex_enter(&rp->r_statelock); 1721 rp->r_server = svp; 1722 PURGE_ATTRCACHE4_LOCKED(rp); 1723 mutex_exit(&rp->r_statelock); 1724 (void) nfs4_free_data_reclaim(rp); 1725 nfs4_purge_rddir_cache(RTOV4(rp)); 1726 rw_exit(&rtable4[index].r_lock); 1727 NFS4_DEBUG(nfs4_client_failover_debug, (CE_NOTE, 1728 "recov_newserver: done with %s", 1729 rnode4info(rp))); 1730 VN_RELE(RTOV4(rp)); 1731 } else 1732 rw_exit(&rtable4[index].r_lock); 1733 (void) dnlc_purge_vfsp(mi->mi_vfsp, 0); 1734 1735 mutex_enter(&mi->mi_lock); 1736 mi->mi_recovflags |= MI4R_REOPEN_FILES | MI4R_REMAP_FILES; 1737 if (recovp->rc_srv_reboot) 1738 mi->mi_recovflags |= MI4R_SRV_REBOOT; 1739 mi->mi_curr_serv = svp; 1740 mi->mi_failover++; 1741 mi->mi_flags &= ~MI4_BADOWNER_DEBUG; 1742 mutex_exit(&mi->mi_lock); 1743 1744 (void) nfs_rw_enter_sig(&svp->sv_lock, RW_READER, 0); 1745 fh.nfs_fh4_len = svp->sv_fhandle.fh_len; 1746 fh.nfs_fh4_val = svp->sv_fhandle.fh_buf; 1747 sfh4_update(mi->mi_rootfh, &fh); 1748 fh.nfs_fh4_len = svp->sv_pfhandle.fh_len; 1749 fh.nfs_fh4_val = svp->sv_pfhandle.fh_buf; 1750 sfh4_update(mi->mi_srvparentfh, &fh); 1751 nfs_rw_exit(&svp->sv_lock); 1752 1753 *spp = nfs4_move_mi(mi, osvp, svp); 1754 if (osp != NULL) 1755 nfs4_server_rele(osp); 1756 } else 1757 mutex_exit(&mi->mi_lock); 1758 (void) nfs_rw_exit(&mi->mi_recovlock); 1759 } 1760 1761 /* 1762 * Clientid. 1763 */ 1764 1765 static void 1766 recov_clientid(recov_info_t *recovp, nfs4_server_t *sp) 1767 { 1768 mntinfo4_t *mi = recovp->rc_mi; 1769 int error = 0; 1770 int still_stale; 1771 int need_new_s; 1772 1773 ASSERT(sp != NULL); 1774 1775 /* 1776 * Acquire the recovery lock and then verify that the clientid 1777 * still needs to be recovered. (Note that s_recovlock is supposed 1778 * to be acquired before s_lock.) Since the thread holds the 1779 * recovery lock, no other thread will recover the clientid. 1780 */ 1781 (void) nfs_rw_enter_sig(&sp->s_recovlock, RW_WRITER, 0); 1782 (void) nfs_rw_enter_sig(&mi->mi_recovlock, RW_WRITER, 0); 1783 mutex_enter(&sp->s_lock); 1784 still_stale = ((sp->s_flags & N4S_CLIENTID_SET) == 0); 1785 mutex_exit(&sp->s_lock); 1786 1787 if (still_stale) { 1788 nfs4_error_t n4e; 1789 1790 nfs4_error_zinit(&n4e); 1791 nfs4setclientid(mi, kcred, TRUE, &n4e); 1792 error = n4e.error; 1793 if (error != 0) { 1794 1795 /* 1796 * nfs4setclientid may have set MI4R_NEED_NEW_SERVER, 1797 * if so, just return and let recov_thread drive 1798 * failover. 1799 */ 1800 mutex_enter(&mi->mi_lock); 1801 need_new_s = mi->mi_recovflags & MI4R_NEED_NEW_SERVER; 1802 mutex_exit(&mi->mi_lock); 1803 1804 if (need_new_s) { 1805 nfs_rw_exit(&mi->mi_recovlock); 1806 nfs_rw_exit(&sp->s_recovlock); 1807 return; 1808 } 1809 1810 nfs4_queue_event(RE_CLIENTID, mi, NULL, n4e.error, NULL, 1811 NULL, n4e.stat, NULL, 0, TAG_NONE, TAG_NONE, 0, 0); 1812 mutex_enter(&mi->mi_lock); 1813 mi->mi_flags |= MI4_RECOV_FAIL; 1814 mi->mi_error = recovp->rc_error; 1815 mutex_exit(&mi->mi_lock); 1816 /* don't destroy the nfs4_server, let umount do it */ 1817 } 1818 } 1819 1820 if (error == 0) { 1821 mutex_enter(&mi->mi_lock); 1822 mi->mi_recovflags &= ~MI4R_NEED_CLIENTID; 1823 /* 1824 * If still_stale isn't true, then another thread already 1825 * recovered the clientid. And that thread that set the 1826 * clientid will have initiated reopening files on all the 1827 * filesystems for the server, so we should not initiate 1828 * reopening for this filesystem here. 1829 */ 1830 if (still_stale) { 1831 mi->mi_recovflags |= MI4R_REOPEN_FILES; 1832 if (recovp->rc_srv_reboot) 1833 mi->mi_recovflags |= MI4R_SRV_REBOOT; 1834 } 1835 mutex_exit(&mi->mi_lock); 1836 } 1837 1838 nfs_rw_exit(&mi->mi_recovlock); 1839 1840 if (error != 0) { 1841 nfs_rw_exit(&sp->s_recovlock); 1842 mutex_enter(&mi->mi_lock); 1843 if ((mi->mi_flags & MI4_RECOV_FAIL) == 0) 1844 delay(SEC_TO_TICK(recov_err_delay)); 1845 mutex_exit(&mi->mi_lock); 1846 } else { 1847 mntinfo4_t **milist; 1848 mntinfo4_t *tmi; 1849 int nummi, i; 1850 1851 /* 1852 * Initiate recovery of open files for other filesystems. 1853 * We create an array of filesystems, rather than just 1854 * walking the filesystem list, to avoid deadlock issues 1855 * with s_lock and mi_recovlock. 1856 */ 1857 milist = make_milist(sp, &nummi); 1858 for (i = 0; i < nummi; i++) { 1859 tmi = milist[i]; 1860 if (tmi != mi) { 1861 (void) nfs_rw_enter_sig(&tmi->mi_recovlock, 1862 RW_READER, 0); 1863 start_recovery_action(NR_OPENFILES, TRUE, tmi, 1864 NULL, NULL); 1865 nfs_rw_exit(&tmi->mi_recovlock); 1866 } 1867 } 1868 free_milist(milist, nummi); 1869 1870 nfs_rw_exit(&sp->s_recovlock); 1871 } 1872 } 1873 1874 /* 1875 * Return an array of filesystems associated with the given server. The 1876 * caller should call free_milist() to free the references and memory. 1877 */ 1878 1879 static mntinfo4_t ** 1880 make_milist(nfs4_server_t *sp, int *nummip) 1881 { 1882 int nummi, i; 1883 mntinfo4_t **milist; 1884 mntinfo4_t *tmi; 1885 1886 mutex_enter(&sp->s_lock); 1887 nummi = 0; 1888 for (tmi = sp->mntinfo4_list; tmi != NULL; tmi = tmi->mi_clientid_next) 1889 nummi++; 1890 1891 milist = kmem_alloc(nummi * sizeof (mntinfo4_t *), KM_NOSLEEP); 1892 1893 for (i = 0, tmi = sp->mntinfo4_list; tmi != NULL; i++, 1894 tmi = tmi->mi_clientid_next) { 1895 milist[i] = tmi; 1896 VFS_HOLD(tmi->mi_vfsp); 1897 } 1898 mutex_exit(&sp->s_lock); 1899 1900 *nummip = nummi; 1901 return (milist); 1902 } 1903 1904 /* 1905 * Free the filesystem list created by make_milist(). 1906 */ 1907 1908 static void 1909 free_milist(mntinfo4_t **milist, int nummi) 1910 { 1911 mntinfo4_t *tmi; 1912 int i; 1913 1914 for (i = 0; i < nummi; i++) { 1915 tmi = milist[i]; 1916 VFS_RELE(tmi->mi_vfsp); 1917 } 1918 kmem_free(milist, nummi * sizeof (mntinfo4_t *)); 1919 } 1920 1921 /* 1922 * Filehandle 1923 */ 1924 1925 /* 1926 * Lookup the filehandle for the given vnode and update the rnode if it has 1927 * changed. 1928 * 1929 * Errors: 1930 * - if the filehandle could not be updated because of an error that 1931 * requires further recovery, initiate that recovery and return. 1932 * - if the filehandle could not be updated because of a signal, pretend we 1933 * succeeded and let someone else deal with it. 1934 * - if the filehandle could not be updated and the filesystem has been 1935 * forcibly unmounted, pretend we succeeded, and let the caller deal with 1936 * the forced unmount (to retry or not to retry, that is the question). 1937 * - if the filehandle could not be updated because of some other error, 1938 * mark the rnode bad and return. 1939 */ 1940 static void 1941 recov_filehandle(nfs4_recov_t action, mntinfo4_t *mi, vnode_t *vp) 1942 { 1943 rnode4_t *rp = VTOR4(vp); 1944 nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS }; 1945 bool_t needrecov; 1946 1947 mutex_enter(&rp->r_statelock); 1948 1949 if (rp->r_flags & R4RECOVERR) { 1950 mutex_exit(&rp->r_statelock); 1951 return; 1952 } 1953 1954 /* 1955 * If someone else is updating the filehandle, wait for them to 1956 * finish and then let our caller retry. 1957 */ 1958 if (rp->r_flags & R4RECEXPFH) { 1959 while (rp->r_flags & R4RECEXPFH) { 1960 cv_wait(&rp->r_cv, &rp->r_statelock); 1961 } 1962 mutex_exit(&rp->r_statelock); 1963 return; 1964 } 1965 rp->r_flags |= R4RECEXPFH; 1966 mutex_exit(&rp->r_statelock); 1967 1968 if (action == NR_BADHANDLE) { 1969 /* shouldn't happen */ 1970 nfs4_queue_event(RE_BADHANDLE, mi, NULL, 0, 1971 vp, NULL, 0, NULL, 0, TAG_NONE, TAG_NONE, 0, 0); 1972 } 1973 1974 nfs4_remap_file(mi, vp, 0, &e); 1975 needrecov = nfs4_needs_recovery(&e, FALSE, mi->mi_vfsp); 1976 1977 /* 1978 * If we get BADHANDLE or FHEXPIRED in their handler, something is 1979 * broken. Don't try to recover, just mark the file dead. 1980 */ 1981 if (needrecov && e.error == 0 && 1982 (e.stat == NFS4ERR_BADHANDLE || e.stat == NFS4ERR_FHEXPIRED)) 1983 needrecov = FALSE; 1984 if (needrecov) { 1985 (void) nfs4_start_recovery(&e, mi, vp, 1986 NULL, NULL, NULL, OP_LOOKUP, NULL); 1987 } else if (e.error != EINTR && 1988 !NFS4_FRC_UNMT_ERR(e.error, mi->mi_vfsp) && 1989 (e.error != 0 || e.stat != NFS4_OK)) { 1990 nfs4_recov_fh_fail(vp, e.error, e.stat); 1991 /* 1992 * Don't set r_error to ESTALE. Higher-level code (e.g., 1993 * cstatat_getvp()) retries on ESTALE, which would cause 1994 * an infinite loop. 1995 */ 1996 } 1997 1998 mutex_enter(&rp->r_statelock); 1999 rp->r_flags &= ~R4RECEXPFH; 2000 cv_broadcast(&rp->r_cv); 2001 mutex_exit(&rp->r_statelock); 2002 } 2003 2004 /* 2005 * Stale Filehandle 2006 */ 2007 2008 /* 2009 * A stale filehandle can happen when an individual file has 2010 * been removed, or when an entire filesystem has been taken 2011 * offline. To distinguish these cases, we do this: 2012 * - if a GETATTR with the current filehandle is okay, we do 2013 * nothing (this can happen with two-filehandle ops) 2014 * - if the GETATTR fails, but a GETATTR of the root filehandle 2015 * succeeds, mark the rnode with R4STALE, which will stop use 2016 * - if the GETATTR fails, and a GETATTR of the root filehandle 2017 * also fails, we consider the problem filesystem-wide, so: 2018 * - if we can failover, we should 2019 * - if we can't failover, we should mark both the original 2020 * vnode and the root bad 2021 */ 2022 static void 2023 recov_stale(mntinfo4_t *mi, vnode_t *vp) 2024 { 2025 rnode4_t *rp = VTOR4(vp); 2026 vnode_t *rootvp = NULL; 2027 nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS }; 2028 nfs4_ga_res_t gar; 2029 char *fail_msg = "failed to recover from NFS4ERR_STALE"; 2030 bool_t needrecov; 2031 2032 mutex_enter(&rp->r_statelock); 2033 2034 if (rp->r_flags & R4RECOVERR) { 2035 mutex_exit(&rp->r_statelock); 2036 NFS4_DEBUG(nfs4_client_recov_debug, (CE_NOTE, 2037 "recov_stale: already marked dead, rp %s", 2038 rnode4info(rp))); 2039 return; 2040 } 2041 2042 if (rp->r_flags & R4STALE) { 2043 mutex_exit(&rp->r_statelock); 2044 NFS4_DEBUG(nfs4_client_recov_debug, (CE_NOTE, 2045 "recov_stale: already marked stale, rp %s", 2046 rnode4info(rp))); 2047 return; 2048 } 2049 2050 mutex_exit(&rp->r_statelock); 2051 2052 /* Try a GETATTR on this vnode */ 2053 nfs4_getattr_otw_norecovery(vp, &gar, &e, CRED(), 0); 2054 2055 /* 2056 * Handle non-STALE recoverable errors 2057 */ 2058 needrecov = nfs4_needs_recovery(&e, FALSE, vp->v_vfsp); 2059 if (needrecov && (e.error != 0 || e.stat != NFS4ERR_STALE)) { 2060 (void) nfs4_start_recovery(&e, mi, vp, 2061 NULL, NULL, NULL, OP_GETATTR, NULL); 2062 NFS4_DEBUG(nfs4_client_recov_debug, (CE_NOTE, 2063 "recov_stale: error=%d, stat=%d seen on rp %s", 2064 e.error, e.stat, rnode4info(rp))); 2065 goto out; 2066 } 2067 2068 /* Are things OK for this vnode? */ 2069 if (!e.error && e.stat == NFS4_OK) { 2070 NFS4_DEBUG(nfs4_client_recov_debug, (CE_NOTE, 2071 "recov_stale: file appears fine, rp %s", 2072 rnode4info(rp))); 2073 goto out; 2074 } 2075 2076 /* Did we get an unrelated non-recoverable error? */ 2077 if (e.error || e.stat != NFS4ERR_STALE) { 2078 nfs4_fail_recov(vp, fail_msg, e.error, e.stat); 2079 NFS4_DEBUG(nfs4_client_recov_debug, (CE_NOTE, 2080 "recov_stale: unrelated fatal error, rp %s", 2081 rnode4info(rp))); 2082 goto out; 2083 } 2084 2085 /* 2086 * If we don't appear to be dealing with the root node, find it. 2087 */ 2088 if ((vp->v_flag & VROOT) == 0) { 2089 nfs4_error_zinit(&e); 2090 e.error = VFS_ROOT(vp->v_vfsp, &rootvp); 2091 if (e.error) { 2092 nfs4_fail_recov(vp, fail_msg, 0, NFS4ERR_STALE); 2093 NFS4_DEBUG(nfs4_client_recov_debug, (CE_NOTE, 2094 "recov_stale: can't find root node for rp %s", 2095 rnode4info(rp))); 2096 goto out; 2097 } 2098 } 2099 2100 /* Try a GETATTR on the root vnode */ 2101 if (rootvp != NULL) { 2102 nfs4_error_zinit(&e); 2103 nfs4_getattr_otw_norecovery(rootvp, &gar, &e, CRED(), 0); 2104 2105 /* Try recovery? */ 2106 if (e.error != 0 || e.stat != NFS4ERR_STALE) { 2107 needrecov = nfs4_needs_recovery(&e, FALSE, vp->v_vfsp); 2108 if (needrecov) { 2109 (void) nfs4_start_recovery(&e, 2110 mi, rootvp, NULL, NULL, NULL, 2111 OP_GETATTR, NULL); 2112 NFS4_DEBUG(nfs4_client_recov_debug, (CE_NOTE, 2113 "recov_stale: error=%d, stat=%d seen " 2114 "on rp %s", e.error, e.stat, 2115 rnode4info(rp))); 2116 } 2117 } 2118 2119 /* 2120 * Check to see if a failover attempt is warranted 2121 * NB: nfs4_try_failover doesn't check for STALE 2122 * because recov_stale gets a shot first. Now that 2123 * recov_stale has failed, go ahead and try failover. 2124 * 2125 * If the getattr on the root filehandle was successful, 2126 * then mark recovery as failed for 'vp' and exit. 2127 */ 2128 if (nfs4_try_failover(&e) == 0 && e.stat != NFS4ERR_STALE) { 2129 /* 2130 * pass the original error to fail_recov, not 2131 * the one from trying the root vnode. 2132 */ 2133 nfs4_fail_recov(vp, fail_msg, 0, NFS4ERR_STALE); 2134 NFS4_DEBUG(nfs4_client_recov_debug, (CE_NOTE, 2135 "recov_stale: root node OK, marking " 2136 "dead rp %s", rnode4info(rp))); 2137 goto out; 2138 } 2139 } 2140 2141 /* 2142 * Here, we know that both the original file and the 2143 * root filehandle (which may be the same) are stale. 2144 * We want to fail over if we can, and if we can't, we 2145 * want to mark everything in sight bad. 2146 */ 2147 if (FAILOVER_MOUNT4(mi)) { 2148 mutex_enter(&mi->mi_lock); 2149 mi->mi_recovflags |= MI4R_NEED_NEW_SERVER; 2150 NFS4_DEBUG(nfs4_client_recov_debug, (CE_NOTE, 2151 "recov_stale: failing over due to rp %s", 2152 rnode4info(rp))); 2153 mutex_exit(&mi->mi_lock); 2154 } else { 2155 rnode4_t *rootrp; 2156 servinfo4_t *svp; 2157 2158 /* 2159 * Can't fail over, so mark things dead. 2160 * 2161 * If rootvp is set, we know we have a distinct 2162 * non-root vnode which can be marked dead in 2163 * the usual way. 2164 * 2165 * Then we want to mark the root vnode dead. 2166 * Note that if rootvp wasn't set, our vp is 2167 * actually the root vnode. 2168 */ 2169 if (rootvp != NULL) { 2170 NFS4_DEBUG(nfs4_client_recov_debug, (CE_NOTE, 2171 "recov_stale: can't fail over, marking dead rp %s", 2172 rnode4info(rp))); 2173 nfs4_fail_recov(vp, fail_msg, 0, NFS4ERR_STALE); 2174 } else { 2175 rootvp = vp; 2176 VN_HOLD(rootvp); 2177 } 2178 2179 /* 2180 * Mark root dead, but quietly - since 2181 * the root rnode is frequently recreated, 2182 * we can encounter this at every access. 2183 * Also mark recovery as failed on this VFS. 2184 */ 2185 rootrp = VTOR4(rootvp); 2186 NFS4_DEBUG(nfs4_client_recov_debug, (CE_CONT, 2187 "recov_stale: marking dead root rp %s", 2188 rnode4info(rootrp))); 2189 mutex_enter(&rootrp->r_statelock); 2190 rootrp->r_flags |= (R4RECOVERR | R4STALE); 2191 rootrp->r_error = ESTALE; 2192 mutex_exit(&rootrp->r_statelock); 2193 mutex_enter(&mi->mi_lock); 2194 mi->mi_error = ESTALE; 2195 mutex_exit(&mi->mi_lock); 2196 2197 svp = mi->mi_curr_serv; 2198 (void) nfs_rw_enter_sig(&svp->sv_lock, RW_WRITER, 0); 2199 svp->sv_flags |= SV4_ROOT_STALE; 2200 nfs_rw_exit(&svp->sv_lock); 2201 } 2202 2203 out: 2204 if (rootvp) 2205 VN_RELE(rootvp); 2206 } 2207 2208 /* 2209 * Locks. 2210 */ 2211 2212 /* 2213 * Reclaim all the active (acquired) locks for the given file. 2214 * If a process lost a lock, the process is sent a SIGLOST. This is not 2215 * considered an error. 2216 * 2217 * Return values: 2218 * Errors and status are returned via the nfs4_error_t parameter 2219 * If an error indicates that recovery is needed, the caller is responsible 2220 * for dealing with it. 2221 */ 2222 2223 static void 2224 relock_file(vnode_t *vp, mntinfo4_t *mi, nfs4_error_t *ep, 2225 fattr4_change pre_change) 2226 { 2227 locklist_t *locks, *llp; 2228 rnode4_t *rp; 2229 2230 ASSERT(ep != NULL); 2231 nfs4_error_zinit(ep); 2232 2233 if (VTOMI4(vp)->mi_flags & MI4_LLOCK) 2234 return; 2235 2236 nfs4_flush_lock_owners(VTOR4(vp)); 2237 2238 /* 2239 * If we get an error that requires recovery actions, just bail out 2240 * and let the top-level recovery code handle it. 2241 * 2242 * If we get some other error, kill the process that owned the lock 2243 * and mark its remaining locks (if any) as belonging to NOPID, so 2244 * that we don't make any more reclaim requests for that process. 2245 */ 2246 2247 rp = VTOR4(vp); 2248 locks = flk_active_locks_for_vp(vp); 2249 for (llp = locks; llp != NULL; llp = llp->ll_next) { 2250 int did_reclaim = 1; 2251 2252 ASSERT(llp->ll_vp == vp); 2253 if (llp->ll_flock.l_pid == NOPID) 2254 continue; 2255 reclaim_one_lock(vp, &llp->ll_flock, ep, &did_reclaim); 2256 /* 2257 * If we need to restart recovery, stop processing the 2258 * list. Some errors would be recoverable under other 2259 * circumstances, but if they happen here we just give up 2260 * on the lock. 2261 */ 2262 if (nfs4_needs_recovery(ep, TRUE, vp->v_vfsp)) { 2263 if (ep->error != 0) 2264 break; 2265 if (!nfs4_recov_marks_dead(ep->stat)) 2266 break; 2267 } 2268 /* 2269 * In case the server isn't offering us a grace period, or 2270 * if we missed it, we might have opened & locked from scratch, 2271 * rather than reopened/reclaimed. 2272 * We need to ensure that the object hadn't been otherwise 2273 * changed during this time, by comparing the changeinfo. 2274 * We get passed the changeinfo from before the reopen by our 2275 * caller, in pre_change. 2276 * The changeinfo from after the reopen is in rp->r_change, 2277 * courtesy of the GETATTR in the reopen. 2278 * If they're different, then the file has changed, and we 2279 * have to SIGLOST the app. 2280 */ 2281 if (ep->error == 0 && ep->stat == NFS4_OK && !did_reclaim) { 2282 mutex_enter(&rp->r_statelock); 2283 if (pre_change != rp->r_change) 2284 ep->stat = NFS4ERR_NO_GRACE; 2285 mutex_exit(&rp->r_statelock); 2286 } 2287 if (ep->error != 0 || ep->stat != NFS4_OK) { 2288 if (ep->error != 0) 2289 nfs4_queue_event(RE_FAIL_RELOCK, mi, 2290 NULL, ep->error, vp, NULL, 0, NULL, 2291 llp->ll_flock.l_pid, TAG_NONE, TAG_NONE, 2292 0, 0); 2293 else 2294 nfs4_queue_event(RE_FAIL_RELOCK, mi, 2295 NULL, 0, vp, NULL, ep->stat, NULL, 2296 llp->ll_flock.l_pid, TAG_NONE, TAG_NONE, 2297 0, 0); 2298 nfs4_send_siglost(llp->ll_flock.l_pid, mi, vp, TRUE, 2299 ep->error, ep->stat); 2300 relock_skip_pid(llp, llp->ll_flock.l_pid); 2301 2302 /* Reinitialize the nfs4_error and continue */ 2303 nfs4_error_zinit(ep); 2304 } 2305 } 2306 2307 if (locks != NULL) 2308 flk_free_locklist(locks); 2309 } 2310 2311 /* 2312 * Reclaim the given lock. 2313 * If the lock can't be reclaimed, the process is sent SIGLOST, but this is 2314 * not considered an error. 2315 * 2316 * Errors are returned via the nfs4_error_t parameter. 2317 */ 2318 static void 2319 reclaim_one_lock(vnode_t *vp, flock64_t *flk, nfs4_error_t *ep, 2320 int *did_reclaimp) 2321 { 2322 cred_t *cr; 2323 rnode4_t *rp = VTOR4(vp); 2324 2325 cr = pid_to_cr(flk->l_pid); 2326 if (cr == NULL) { 2327 nfs4_error_zinit(ep); 2328 ep->error = ESRCH; 2329 return; 2330 } 2331 2332 do { 2333 mutex_enter(&rp->r_statelock); 2334 if (rp->r_flags & R4RECOVERR) { 2335 /* 2336 * This shouldn't affect other reclaims, so don't 2337 * return an error. 2338 */ 2339 mutex_exit(&rp->r_statelock); 2340 break; 2341 } 2342 mutex_exit(&rp->r_statelock); 2343 2344 nfs4frlock(NFS4_LCK_CTYPE_RECLAIM, vp, F_SETLK, flk, 2345 FREAD|FWRITE, 0, cr, ep, NULL, did_reclaimp); 2346 if (ep->error == 0 && ep->stat == NFS4ERR_FHEXPIRED) 2347 start_recovery_action(NR_FHEXPIRED, TRUE, VTOMI4(vp), 2348 vp, NULL); 2349 } while (ep->error == 0 && ep->stat == NFS4ERR_FHEXPIRED); 2350 2351 crfree(cr); 2352 } 2353 2354 /* 2355 * Open files. 2356 */ 2357 2358 /* 2359 * Verifies if the nfsstat4 is a valid error for marking this vnode dead. 2360 * Returns 1 if the error is valid; 0 otherwise. 2361 */ 2362 static int 2363 nfs4_valid_recov_err_for_vp(vnode_t *vp, nfsstat4 stat) 2364 { 2365 /* 2366 * We should not be marking non-regular files as dead, 2367 * except in very rare cases (eg: BADHANDLE or NFS4ERR_BADNAME). 2368 */ 2369 if (vp->v_type != VREG && stat != NFS4ERR_BADHANDLE && 2370 stat != NFS4ERR_BADNAME) 2371 return (0); 2372 2373 return (1); 2374 } 2375 2376 /* 2377 * Failed attempting to recover a filehandle. If 'stat' is valid for 'vp', 2378 * then mark the object dead. Since we've had to do a lookup for 2379 * filehandle recovery, we will mark the object dead if we got NOENT. 2380 */ 2381 static void 2382 nfs4_recov_fh_fail(vnode_t *vp, int error, nfsstat4 stat) 2383 { 2384 ASSERT(vp != NULL); 2385 2386 if ((error == 0) && (stat != NFS4ERR_NOENT) && 2387 (!nfs4_valid_recov_err_for_vp(vp, stat))) 2388 return; 2389 2390 nfs4_fail_recov(vp, "can't recover filehandle", error, stat); 2391 } 2392 2393 /* 2394 * Recovery from a "shouldn't happen" error. In the long term, we'd like 2395 * to mark only the data structure(s) that provided the bad value as being 2396 * bad. But for now we'll just mark the entire file. 2397 */ 2398 2399 static void 2400 recov_badstate(recov_info_t *recovp, vnode_t *vp, nfsstat4 stat) 2401 { 2402 ASSERT(vp != NULL); 2403 recov_throttle(recovp, vp); 2404 2405 if (!nfs4_valid_recov_err_for_vp(vp, stat)) 2406 return; 2407 2408 nfs4_fail_recov(vp, "", 0, stat); 2409 } 2410 2411 /* 2412 * Free up the information saved for a lost state request. 2413 */ 2414 static void 2415 nfs4_free_lost_rqst(nfs4_lost_rqst_t *lrp, nfs4_server_t *sp) 2416 { 2417 component4 *filep; 2418 nfs4_open_stream_t *osp; 2419 int have_sync_lock; 2420 2421 NFS4_DEBUG(nfs4_lost_rqst_debug, 2422 (CE_NOTE, "nfs4_free_lost_rqst:")); 2423 2424 switch (lrp->lr_op) { 2425 case OP_OPEN: 2426 filep = &lrp->lr_ofile; 2427 if (filep->utf8string_val) { 2428 kmem_free(filep->utf8string_val, filep->utf8string_len); 2429 filep->utf8string_val = NULL; 2430 } 2431 break; 2432 case OP_DELEGRETURN: 2433 nfs4delegreturn_cleanup(VTOR4(lrp->lr_vp), sp); 2434 break; 2435 case OP_CLOSE: 2436 osp = lrp->lr_osp; 2437 ASSERT(osp != NULL); 2438 mutex_enter(&osp->os_sync_lock); 2439 have_sync_lock = 1; 2440 if (osp->os_pending_close) { 2441 /* clean up the open file state. */ 2442 osp->os_pending_close = 0; 2443 nfs4close_notw(lrp->lr_vp, osp, &have_sync_lock); 2444 } 2445 if (have_sync_lock) 2446 mutex_exit(&osp->os_sync_lock); 2447 break; 2448 } 2449 2450 lrp->lr_op = 0; 2451 if (lrp->lr_oop != NULL) { 2452 open_owner_rele(lrp->lr_oop); 2453 lrp->lr_oop = NULL; 2454 } 2455 if (lrp->lr_osp != NULL) { 2456 open_stream_rele(lrp->lr_osp, VTOR4(lrp->lr_vp)); 2457 lrp->lr_osp = NULL; 2458 } 2459 if (lrp->lr_lop != NULL) { 2460 lock_owner_rele(lrp->lr_lop); 2461 lrp->lr_lop = NULL; 2462 } 2463 if (lrp->lr_flk != NULL) { 2464 kmem_free(lrp->lr_flk, sizeof (flock64_t)); 2465 lrp->lr_flk = NULL; 2466 } 2467 if (lrp->lr_vp != NULL) { 2468 VN_RELE(lrp->lr_vp); 2469 lrp->lr_vp = NULL; 2470 } 2471 if (lrp->lr_dvp != NULL) { 2472 VN_RELE(lrp->lr_dvp); 2473 lrp->lr_dvp = NULL; 2474 } 2475 if (lrp->lr_cr != NULL) { 2476 crfree(lrp->lr_cr); 2477 lrp->lr_cr = NULL; 2478 } 2479 2480 kmem_free(lrp, sizeof (nfs4_lost_rqst_t)); 2481 } 2482 2483 /* 2484 * Remove any lost state requests and free them. 2485 */ 2486 static void 2487 nfs4_remove_lost_rqsts(mntinfo4_t *mi, nfs4_server_t *sp) 2488 { 2489 nfs4_lost_rqst_t *lrp; 2490 2491 mutex_enter(&mi->mi_lock); 2492 while ((lrp = list_head(&mi->mi_lost_state)) != NULL) { 2493 list_remove(&mi->mi_lost_state, lrp); 2494 mutex_exit(&mi->mi_lock); 2495 nfs4_free_lost_rqst(lrp, sp); 2496 mutex_enter(&mi->mi_lock); 2497 } 2498 mutex_exit(&mi->mi_lock); 2499 } 2500 2501 /* 2502 * Reopen all the files for the given filesystem and reclaim any locks. 2503 */ 2504 2505 static void 2506 recov_openfiles(recov_info_t *recovp, nfs4_server_t *sp) 2507 { 2508 mntinfo4_t *mi = recovp->rc_mi; 2509 nfs4_opinst_t *reopenlist = NULL, *rep; 2510 nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS }; 2511 open_claim_type4 claim; 2512 int remap; 2513 char *fail_msg = "No such file or directory on replica"; 2514 rnode4_t *rp; 2515 fattr4_change pre_change; 2516 2517 ASSERT(sp != NULL); 2518 2519 /* 2520 * This check is to allow a 10ms pause before we reopen files 2521 * it should allow the server time to have received the CB_NULL 2522 * reply and update its internal structures such that (if 2523 * applicable) we are granted a delegation on reopened files. 2524 */ 2525 mutex_enter(&sp->s_lock); 2526 if ((sp->s_flags & (N4S_CB_PINGED | N4S_CB_WAITER)) == 0) { 2527 sp->s_flags |= N4S_CB_WAITER; 2528 (void) cv_timedwait(&sp->wait_cb_null, &sp->s_lock, 2529 (lbolt+drv_usectohz(N4S_CB_PAUSE_TIME))); 2530 } 2531 mutex_exit(&sp->s_lock); 2532 2533 (void) nfs_rw_enter_sig(&sp->s_recovlock, RW_READER, 0); 2534 (void) nfs_rw_enter_sig(&mi->mi_recovlock, RW_WRITER, 0); 2535 2536 if (NFS4_VOLATILE_FH(mi)) { 2537 nfs4_remap_root(mi, &e, 0); 2538 if (nfs4_needs_recovery(&e, FALSE, mi->mi_vfsp)) { 2539 (void) nfs4_start_recovery(&e, mi, NULL, 2540 NULL, NULL, NULL, OP_LOOKUP, NULL); 2541 } 2542 } 2543 2544 mutex_enter(&mi->mi_lock); 2545 if (recovp->rc_srv_reboot || (mi->mi_recovflags & MI4R_SRV_REBOOT)) 2546 claim = CLAIM_PREVIOUS; 2547 else 2548 claim = CLAIM_NULL; 2549 mutex_exit(&mi->mi_lock); 2550 2551 if (e.error == 0 && e.stat == NFS4_OK) { 2552 /* 2553 * Get a snapshot of open files in the filesystem. Note 2554 * that new opens will stall until the server's grace 2555 * period is done. 2556 */ 2557 reopenlist = r4mkopenlist(mi); 2558 2559 mutex_enter(&mi->mi_lock); 2560 remap = mi->mi_recovflags & MI4R_REMAP_FILES; 2561 mutex_exit(&mi->mi_lock); 2562 /* 2563 * Since we are re-establishing state on the 2564 * server, its ok to blow away the saved lost 2565 * requests since we don't need to reissue it. 2566 */ 2567 nfs4_remove_lost_rqsts(mi, sp); 2568 2569 for (rep = reopenlist; rep; rep = rep->re_next) { 2570 2571 if (remap) { 2572 nfs4_remap_file(mi, rep->re_vp, 2573 NFS4_REMAP_CKATTRS, &e); 2574 } 2575 if (e.error == ENOENT || e.stat == NFS4ERR_NOENT) { 2576 /* 2577 * The current server does not have the file 2578 * that is to be remapped. This is most 2579 * likely due to an improperly maintained 2580 * replica. The files that are missing from 2581 * the server will be marked dead and logged 2582 * in order to make sys admins aware of the 2583 * problem. 2584 */ 2585 nfs4_fail_recov(rep->re_vp, 2586 fail_msg, e.error, e.stat); 2587 /* 2588 * We've already handled the error so clear it. 2589 */ 2590 nfs4_error_zinit(&e); 2591 continue; 2592 } else if (e.error == 0 && e.stat == NFS4_OK) { 2593 int j; 2594 2595 rp = VTOR4(rep->re_vp); 2596 mutex_enter(&rp->r_statelock); 2597 pre_change = rp->r_change; 2598 mutex_exit(&rp->r_statelock); 2599 2600 for (j = 0; j < rep->re_numosp; j++) { 2601 nfs4_reopen(rep->re_vp, rep->re_osp[j], 2602 &e, claim, FALSE, TRUE); 2603 if (e.error != 0 || e.stat != NFS4_OK) 2604 break; 2605 } 2606 if (nfs4_needs_recovery(&e, TRUE, 2607 mi->mi_vfsp)) { 2608 (void) nfs4_start_recovery(&e, mi, 2609 rep->re_vp, NULL, NULL, NULL, 2610 OP_OPEN, NULL); 2611 break; 2612 } 2613 } 2614 #ifdef DEBUG 2615 if (nfs4_recovdelay > 0) 2616 delay(MSEC_TO_TICK(nfs4_recovdelay * 1000)); 2617 #endif 2618 if (e.error == 0 && e.stat == NFS4_OK) 2619 relock_file(rep->re_vp, mi, &e, pre_change); 2620 2621 if (nfs4_needs_recovery(&e, TRUE, mi->mi_vfsp)) 2622 (void) nfs4_start_recovery(&e, mi, 2623 rep->re_vp, NULL, NULL, NULL, OP_LOCK, 2624 NULL); 2625 if (e.error != 0 || e.stat != NFS4_OK) 2626 break; 2627 } 2628 2629 /* 2630 * Check to see if we need to remap files passed in 2631 * via the recovery arguments; this will have been 2632 * done for open files. A failure here is not fatal. 2633 */ 2634 if (remap) { 2635 nfs4_error_t ignore; 2636 nfs4_check_remap(mi, recovp->rc_vp1, NFS4_REMAP_CKATTRS, 2637 &ignore); 2638 nfs4_check_remap(mi, recovp->rc_vp2, NFS4_REMAP_CKATTRS, 2639 &ignore); 2640 } 2641 } 2642 2643 if (e.error == 0 && e.stat == NFS4_OK) { 2644 mutex_enter(&mi->mi_lock); 2645 mi->mi_recovflags &= ~(MI4R_REOPEN_FILES | MI4R_REMAP_FILES); 2646 mutex_exit(&mi->mi_lock); 2647 } 2648 2649 nfs_rw_exit(&mi->mi_recovlock); 2650 nfs_rw_exit(&sp->s_recovlock); 2651 2652 if (reopenlist != NULL) 2653 r4releopenlist(reopenlist); 2654 } 2655 2656 /* 2657 * Resend the queued state recovery requests in "rqsts". 2658 */ 2659 2660 static void 2661 nfs4_resend_lost_rqsts(recov_info_t *recovp, nfs4_server_t *sp) 2662 { 2663 nfs4_lost_rqst_t *lrp, *tlrp; 2664 mntinfo4_t *mi = recovp->rc_mi; 2665 nfs4_error_t e; 2666 #ifdef NOTYET 2667 uint32_t deny_bits = 0; 2668 #endif 2669 2670 NFS4_DEBUG(nfs4_lost_rqst_debug, (CE_NOTE, "nfs4_resend_lost_rqsts")); 2671 2672 ASSERT(mi != NULL); 2673 ASSERT(nfs_rw_lock_held(&mi->mi_recovlock, RW_WRITER)); 2674 2675 mutex_enter(&mi->mi_lock); 2676 lrp = list_head(&mi->mi_lost_state); 2677 mutex_exit(&mi->mi_lock); 2678 while (lrp != NULL) { 2679 resend_one_op(lrp, &e, mi, sp); 2680 NFS4_DEBUG(nfs4_lost_rqst_debug, (CE_NOTE, 2681 "nfs4_resend_lost_rqsts: resend request: for vp %p got " 2682 "error %d stat %d", (void *)lrp->lr_vp, e.error, e.stat)); 2683 2684 /* 2685 * If we get a recovery error that we can actually 2686 * recover from (such as ETIMEDOUT, FHEXPIRED), we 2687 * return and let the recovery thread redrive the call. 2688 * Don't requeue unless the zone is still healthy. 2689 */ 2690 if (zone_status_get(curproc->p_zone) < ZONE_IS_SHUTTING_DOWN && 2691 nfs4_needs_recovery(&e, TRUE, mi->mi_vfsp) && 2692 (nfs4_try_failover(&e) || 2693 NFS4_FRC_UNMT_ERR(e.error, mi->mi_vfsp) || 2694 (e.error == 0 && e.stat != NFS4ERR_BADHANDLE && 2695 !nfs4_recov_marks_dead(e.stat)))) { 2696 /* 2697 * For these three errors, we want to delay a bit 2698 * instead of pounding the server into submission. 2699 * We have to do this manually; the normal 2700 * processing for these errors only works for 2701 * non-recovery requests. 2702 */ 2703 if ((e.error == 0 && e.stat == NFS4ERR_DELAY) || 2704 (e.error == 0 && e.stat == NFS4ERR_GRACE) || 2705 (e.error == 0 && e.stat == NFS4ERR_RESOURCE) || 2706 NFS4_FRC_UNMT_ERR(e.error, mi->mi_vfsp)) { 2707 delay(SEC_TO_TICK(nfs4err_delay_time)); 2708 } else { 2709 (void) nfs4_start_recovery(&e, 2710 mi, lrp->lr_dvp, lrp->lr_vp, NULL, NULL, 2711 lrp->lr_op, NULL); 2712 } 2713 return; 2714 } 2715 2716 mutex_enter(&mi->mi_lock); 2717 list_remove(&mi->mi_lost_state, lrp); 2718 tlrp = lrp; 2719 lrp = list_head(&mi->mi_lost_state); 2720 mutex_exit(&mi->mi_lock); 2721 nfs4_free_lost_rqst(tlrp, sp); 2722 } 2723 } 2724 2725 /* 2726 * Resend the given op, and issue any necessary undo call. 2727 * errors are returned via the nfs4_error_t parameter. 2728 */ 2729 2730 static void 2731 resend_one_op(nfs4_lost_rqst_t *lrp, nfs4_error_t *ep, 2732 mntinfo4_t *mi, nfs4_server_t *sp) 2733 { 2734 vnode_t *vp; 2735 nfs4_open_stream_t *osp; 2736 cred_t *cr; 2737 uint32_t acc_bits; 2738 2739 vp = lrp->lr_vp; 2740 NFS4_DEBUG(nfs4_lost_rqst_debug, (CE_NOTE, "resend_one_op: " 2741 "have a lost open/close request for vp %p", (void *)vp)); 2742 2743 switch (lrp->lr_op) { 2744 case OP_OPEN: 2745 nfs4_resend_open_otw(&vp, lrp, ep); 2746 break; 2747 case OP_OPEN_DOWNGRADE: 2748 ASSERT(lrp->lr_oop != NULL); 2749 ep->error = nfs4_start_open_seqid_sync(lrp->lr_oop, mi); 2750 ASSERT(!ep->error); /* recov thread always succeeds */ 2751 ASSERT(lrp->lr_osp != NULL); 2752 mutex_enter(&lrp->lr_osp->os_sync_lock); 2753 nfs4_open_downgrade(lrp->lr_dg_acc, lrp->lr_dg_deny, 2754 lrp->lr_oop, lrp->lr_osp, vp, lrp->lr_cr, lrp, 2755 ep, NULL, NULL); 2756 mutex_exit(&lrp->lr_osp->os_sync_lock); 2757 nfs4_end_open_seqid_sync(lrp->lr_oop); 2758 break; 2759 case OP_CLOSE: 2760 osp = lrp->lr_osp; 2761 cr = lrp->lr_cr; 2762 acc_bits = 0; 2763 mutex_enter(&osp->os_sync_lock); 2764 if (osp->os_share_acc_read) 2765 acc_bits |= OPEN4_SHARE_ACCESS_READ; 2766 if (osp->os_share_acc_write) 2767 acc_bits |= OPEN4_SHARE_ACCESS_WRITE; 2768 mutex_exit(&osp->os_sync_lock); 2769 nfs4close_one(vp, osp, cr, acc_bits, lrp, ep, 2770 CLOSE_RESEND, 0, 0, 0); 2771 break; 2772 case OP_LOCK: 2773 case OP_LOCKU: 2774 resend_lock(lrp, ep); 2775 goto done; 2776 case OP_DELEGRETURN: 2777 nfs4_resend_delegreturn(lrp, ep, sp); 2778 goto done; 2779 default: 2780 #ifdef DEBUG 2781 cmn_err(CE_PANIC, "resend_one_op: unexpected op: %d", 2782 lrp->lr_op); 2783 #endif 2784 nfs4_queue_event(RE_LOST_STATE_BAD_OP, mi, NULL, 2785 lrp->lr_op, lrp->lr_vp, lrp->lr_dvp, NFS4_OK, NULL, 0, 2786 TAG_NONE, TAG_NONE, 0, 0); 2787 nfs4_error_init(ep, EINVAL); 2788 return; 2789 } 2790 2791 /* 2792 * No need to retry nor send an "undo" CLOSE in the 2793 * event the server rebooted. 2794 */ 2795 if (ep->error == 0 && (ep->stat == NFS4ERR_STALE_CLIENTID || 2796 ep->stat == NFS4ERR_STALE_STATEID || ep->stat == NFS4ERR_EXPIRED)) 2797 goto done; 2798 2799 /* 2800 * If we resent a CLOSE or OPEN_DOWNGRADE, there's nothing 2801 * to undo. Undoing locking operations was handled by 2802 * resend_lock(). 2803 */ 2804 if (lrp->lr_op == OP_OPEN_DOWNGRADE || lrp->lr_op == OP_CLOSE) 2805 goto done; 2806 2807 /* 2808 * If we get any other error for OPEN, then don't attempt 2809 * to undo the resend of the open (since it was never 2810 * successful!). 2811 */ 2812 ASSERT(lrp->lr_op == OP_OPEN); 2813 if (ep->error || ep->stat != NFS4_OK) 2814 goto done; 2815 2816 /* 2817 * Now let's undo our OPEN. 2818 */ 2819 nfs4_error_zinit(ep); 2820 close_after_open_resend(vp, lrp->lr_cr, lrp->lr_oacc, ep); 2821 NFS4_DEBUG(nfs4_lost_rqst_debug, (CE_NOTE, "resend_one_op: " 2822 "nfs4close_one: for vp %p got error %d stat %d", 2823 (void *)vp, ep->error, ep->stat)); 2824 2825 done: 2826 if (vp != lrp->lr_vp) 2827 VN_RELE(vp); 2828 } 2829 2830 /* 2831 * Close a file that was opened via a resent OPEN. 2832 * Most errors are passed back to the caller (via the return value and 2833 * *statp), except for FHEXPIRED, which is retried. 2834 * 2835 * It might be conceptually cleaner to push the CLOSE request onto the 2836 * front of the resend queue, rather than sending it here. That would 2837 * match the way we undo lost lock requests. On the other 2838 * hand, we've already got something that works, and there's no reason to 2839 * change it at this time. 2840 */ 2841 2842 static void 2843 close_after_open_resend(vnode_t *vp, cred_t *cr, uint32_t acc_bits, 2844 nfs4_error_t *ep) 2845 { 2846 2847 for (;;) { 2848 nfs4close_one(vp, NULL, cr, acc_bits, NULL, ep, 2849 CLOSE_AFTER_RESEND, 0, 0, 0); 2850 if (ep->error == 0 && ep->stat == NFS4_OK) 2851 break; /* success; done */ 2852 if (ep->error != 0 || ep->stat != NFS4ERR_FHEXPIRED) 2853 break; 2854 /* else retry FHEXPIRED */ 2855 } 2856 2857 } 2858 2859 /* 2860 * Resend the given lost lock request. Return an errno value. If zero, 2861 * *statp is set to the NFS status code for the call. 2862 * 2863 * Issue a SIGLOST and mark the rnode dead if we get a non-recovery error or 2864 * a recovery error that we don't actually recover from yet (eg: BAD_SEQID). 2865 * Let the recovery thread redrive the call if we get a recovery error that 2866 * we can actually recover from. 2867 */ 2868 static void 2869 resend_lock(nfs4_lost_rqst_t *lrp, nfs4_error_t *ep) 2870 { 2871 bool_t send_siglost = FALSE; 2872 vnode_t *vp = lrp->lr_vp; 2873 2874 NFS4_DEBUG(nfs4_lost_rqst_debug, (CE_NOTE, "resend_lock:")); 2875 ASSERT(lrp->lr_ctype == NFS4_LCK_CTYPE_REINSTATE || 2876 lrp->lr_ctype == NFS4_LCK_CTYPE_RESEND); 2877 2878 nfs4frlock(lrp->lr_ctype, vp, F_SETLK, 2879 lrp->lr_flk, FREAD|FWRITE, 0, lrp->lr_cr, ep, lrp, NULL); 2880 2881 NFS4_DEBUG(nfs4_lost_rqst_debug, (CE_NOTE, "resend_lock: " 2882 "nfs4frlock for vp %p returned error %d, stat %d", 2883 (void *)vp, ep->error, ep->stat)); 2884 2885 if (ep->error == 0 && ep->stat == 0) 2886 goto done; 2887 if (ep->error == 0 && ep->stat == NFS4ERR_DENIED && 2888 lrp->lr_ctype == NFS4_LCK_CTYPE_RESEND) 2889 goto done; 2890 2891 /* 2892 * If we failed with a non-recovery error, send SIGLOST and 2893 * mark the file dead. 2894 */ 2895 if (!nfs4_needs_recovery(ep, TRUE, vp->v_vfsp)) 2896 send_siglost = TRUE; 2897 else { 2898 /* 2899 * Done with recovering LOST LOCK in the event the 2900 * server rebooted or we've lost the lease. 2901 */ 2902 if (ep->error == 0 && (ep->stat == NFS4ERR_STALE_CLIENTID || 2903 ep->stat == NFS4ERR_STALE_STATEID || 2904 ep->stat == NFS4ERR_EXPIRED)) { 2905 goto done; 2906 } 2907 2908 /* 2909 * BAD_STATEID on an unlock indicates that the server has 2910 * forgotten about the lock anyway, so act like the call 2911 * was successful. 2912 */ 2913 if (ep->error == 0 && ep->stat == NFS4ERR_BAD_STATEID && 2914 lrp->lr_op == OP_LOCKU) 2915 goto done; 2916 2917 /* 2918 * If we got a recovery error that we don't actually 2919 * recover from, send SIGLOST. If the filesystem was 2920 * forcibly unmounted, we skip the SIGLOST because (a) it's 2921 * unnecessary noise, and (b) there could be a new process 2922 * with the same pid as the one that had generated the lost 2923 * state request. 2924 */ 2925 if (ep->error == 0 && (ep->stat == NFS4ERR_BADHANDLE || 2926 nfs4_recov_marks_dead(ep->stat))) { 2927 if (!(vp->v_vfsp->vfs_flag & VFS_UNMOUNTED)) 2928 send_siglost = TRUE; 2929 goto done; 2930 } 2931 2932 /* 2933 * If the filesystem was forcibly unmounted, we 2934 * still need to synchronize with the server and 2935 * release state. Try again later. 2936 */ 2937 if (NFS4_FRC_UNMT_ERR(ep->error, vp->v_vfsp)) 2938 goto done; 2939 2940 /* 2941 * If we get a recovery error that we can actually 2942 * recover from (such as ETIMEDOUT, FHEXPIRED), 2943 * return and let the recovery thread redrive the call. 2944 * 2945 * For the three errors below, we want to delay a bit 2946 * instead of pounding the server into submission. 2947 */ 2948 if ((ep->error == 0 && ep->stat == NFS4ERR_DELAY) || 2949 (ep->error == 0 && ep->stat == NFS4ERR_GRACE) || 2950 (ep->error == 0 && ep->stat == NFS4ERR_RESOURCE)) 2951 delay(SEC_TO_TICK(recov_err_delay)); 2952 goto done; 2953 } 2954 2955 done: 2956 if (send_siglost) { 2957 cred_t *sv_cred; 2958 2959 /* 2960 * Must be root or the actual thread being issued the 2961 * SIGLOST for this to work, so just become root. 2962 */ 2963 sv_cred = curthread->t_cred; 2964 curthread->t_cred = kcred; 2965 nfs4_send_siglost(lrp->lr_flk->l_pid, VTOMI4(vp), vp, FALSE, 2966 ep->error, ep->stat); 2967 curthread->t_cred = sv_cred; 2968 2969 /* 2970 * Flush any additional reinstantiation requests for 2971 * this operation. Sending multiple SIGLOSTs to the user 2972 * process is unlikely to help and may cause trouble. 2973 */ 2974 if (lrp->lr_ctype == NFS4_LCK_CTYPE_REINSTATE) 2975 flush_reinstate(lrp); 2976 } 2977 } 2978 2979 /* 2980 * Remove any lock reinstantiation requests that correspond to the given 2981 * lost request. We only remove items that follow lrp in the queue, 2982 * assuming that lrp will be removed by the generic lost state code. 2983 */ 2984 2985 static void 2986 flush_reinstate(nfs4_lost_rqst_t *lrp) 2987 { 2988 vnode_t *vp; 2989 pid_t pid; 2990 mntinfo4_t *mi; 2991 nfs4_lost_rqst_t *nlrp; 2992 2993 vp = lrp->lr_vp; 2994 mi = VTOMI4(vp); 2995 pid = lrp->lr_flk->l_pid; 2996 2997 /* 2998 * If there are any more reinstantation requests to get rid of, 2999 * they should all be clustered at the front of the lost state 3000 * queue. 3001 */ 3002 mutex_enter(&mi->mi_lock); 3003 for (lrp = list_next(&mi->mi_lost_state, lrp); lrp != NULL; 3004 lrp = nlrp) { 3005 nlrp = list_next(&mi->mi_lost_state, lrp); 3006 if (lrp->lr_op != OP_LOCK && lrp->lr_op != OP_LOCKU) 3007 break; 3008 if (lrp->lr_ctype != NFS4_LCK_CTYPE_REINSTATE) 3009 break; 3010 ASSERT(lrp->lr_vp == vp); 3011 ASSERT(lrp->lr_flk->l_pid == pid); 3012 NFS4_DEBUG(nfs4_lost_rqst_debug, (CE_NOTE, 3013 "remove reinstantiation %p", (void *)lrp)); 3014 list_remove(&mi->mi_lost_state, lrp); 3015 nfs4_free_lost_rqst(lrp, NULL); 3016 } 3017 mutex_exit(&mi->mi_lock); 3018 } 3019 3020 /* 3021 * End of state-specific recovery routines. 3022 */ 3023 3024 /* 3025 * Allocate a lost request struct, initialize it from lost_rqstp (including 3026 * bumping the reference counts for the referenced vnode, etc.), and hang 3027 * it off of recovp. 3028 */ 3029 3030 static void 3031 nfs4_save_lost_rqst(nfs4_lost_rqst_t *lost_rqstp, recov_info_t *recovp, 3032 nfs4_recov_t *action, mntinfo4_t *mi) 3033 { 3034 nfs4_lost_rqst_t *destp; 3035 3036 ASSERT(recovp->rc_lost_rqst == NULL); 3037 3038 destp = kmem_alloc(sizeof (nfs4_lost_rqst_t), KM_SLEEP); 3039 recovp->rc_lost_rqst = destp; 3040 3041 if (lost_rqstp->lr_op == OP_LOCK || 3042 lost_rqstp->lr_op == OP_LOCKU) { 3043 ASSERT(lost_rqstp->lr_lop); 3044 *action = NR_LOST_LOCK; 3045 destp->lr_ctype = lost_rqstp->lr_ctype; 3046 destp->lr_locktype = lost_rqstp->lr_locktype; 3047 } else if (lost_rqstp->lr_op == OP_OPEN) { 3048 component4 *srcfp, *destfp; 3049 3050 destp->lr_oacc = lost_rqstp->lr_oacc; 3051 destp->lr_odeny = lost_rqstp->lr_odeny; 3052 destp->lr_oclaim = lost_rqstp->lr_oclaim; 3053 if (lost_rqstp->lr_oclaim == CLAIM_DELEGATE_CUR) 3054 destp->lr_ostateid = lost_rqstp->lr_ostateid; 3055 3056 srcfp = &lost_rqstp->lr_ofile; 3057 destfp = &destp->lr_ofile; 3058 /* 3059 * Consume caller's utf8string 3060 */ 3061 destfp->utf8string_len = srcfp->utf8string_len; 3062 destfp->utf8string_val = srcfp->utf8string_val; 3063 srcfp->utf8string_len = 0; 3064 srcfp->utf8string_val = NULL; /* make sure not reused */ 3065 3066 *action = NR_LOST_STATE_RQST; 3067 } else if (lost_rqstp->lr_op == OP_OPEN_DOWNGRADE) { 3068 destp->lr_dg_acc = lost_rqstp->lr_dg_acc; 3069 destp->lr_dg_deny = lost_rqstp->lr_dg_deny; 3070 3071 *action = NR_LOST_STATE_RQST; 3072 } else if (lost_rqstp->lr_op == OP_CLOSE) { 3073 ASSERT(lost_rqstp->lr_oop); 3074 *action = NR_LOST_STATE_RQST; 3075 } else if (lost_rqstp->lr_op == OP_DELEGRETURN) { 3076 *action = NR_LOST_STATE_RQST; 3077 } else { 3078 #ifdef DEBUG 3079 cmn_err(CE_PANIC, "nfs4_save_lost_rqst: bad op %d", 3080 lost_rqstp->lr_op); 3081 #endif 3082 nfs4_queue_event(RE_LOST_STATE_BAD_OP, mi, NULL, 3083 lost_rqstp->lr_op, lost_rqstp->lr_vp, lost_rqstp->lr_dvp, 3084 NFS4_OK, NULL, curproc->p_pid, TAG_NONE, TAG_NONE, 0, 0); 3085 *action = NR_UNUSED; 3086 recovp->rc_lost_rqst = NULL; 3087 kmem_free(destp, sizeof (nfs4_lost_rqst_t)); 3088 return; 3089 } 3090 3091 destp->lr_op = lost_rqstp->lr_op; 3092 destp->lr_vp = lost_rqstp->lr_vp; 3093 if (destp->lr_vp) 3094 VN_HOLD(destp->lr_vp); 3095 destp->lr_dvp = lost_rqstp->lr_dvp; 3096 if (destp->lr_dvp) 3097 VN_HOLD(destp->lr_dvp); 3098 destp->lr_oop = lost_rqstp->lr_oop; 3099 if (destp->lr_oop) 3100 open_owner_hold(destp->lr_oop); 3101 destp->lr_osp = lost_rqstp->lr_osp; 3102 if (destp->lr_osp) 3103 open_stream_hold(destp->lr_osp); 3104 destp->lr_lop = lost_rqstp->lr_lop; 3105 if (destp->lr_lop) 3106 lock_owner_hold(destp->lr_lop); 3107 destp->lr_cr = lost_rqstp->lr_cr; 3108 if (destp->lr_cr) 3109 crhold(destp->lr_cr); 3110 if (lost_rqstp->lr_flk == NULL) 3111 destp->lr_flk = NULL; 3112 else { 3113 destp->lr_flk = kmem_alloc(sizeof (flock64_t), KM_SLEEP); 3114 *destp->lr_flk = *lost_rqstp->lr_flk; 3115 } 3116 destp->lr_putfirst = lost_rqstp->lr_putfirst; 3117 } 3118 3119 /* 3120 * Map the given return values (errno and nfs4 status code) to a recovery 3121 * action and fill in the following fields of recovp: rc_action, 3122 * rc_srv_reboot, rc_stateid, rc_lost_rqst. 3123 */ 3124 3125 void 3126 errs_to_action(recov_info_t *recovp, 3127 nfs4_server_t *sp, mntinfo4_t *mi, stateid4 *sidp, 3128 nfs4_lost_rqst_t *lost_rqstp, int unmounted, nfs_opnum4 op, 3129 nfs4_bseqid_entry_t *bsep) 3130 { 3131 nfs4_recov_t action = NR_UNUSED; 3132 bool_t reboot = FALSE; 3133 int try_f; 3134 int error = recovp->rc_orig_errors.error; 3135 nfsstat4 stat = recovp->rc_orig_errors.stat; 3136 3137 bzero(&recovp->rc_stateid, sizeof (stateid4)); 3138 recovp->rc_lost_rqst = NULL; 3139 recovp->rc_bseqid_rqst = NULL; 3140 3141 try_f = nfs4_try_failover(&recovp->rc_orig_errors) && 3142 FAILOVER_MOUNT4(mi); 3143 3144 /* 3145 * We start recovery for EINTR only in the lost lock 3146 * or lost open/close case. 3147 */ 3148 3149 if (try_f || error == EINTR || (error == EIO && unmounted)) { 3150 recovp->rc_error = (error != 0 ? error : geterrno4(stat)); 3151 if (lost_rqstp) { 3152 ASSERT(lost_rqstp->lr_op != 0); 3153 nfs4_save_lost_rqst(lost_rqstp, recovp, &action, mi); 3154 } 3155 if (try_f) 3156 action = NR_FAILOVER; 3157 } else if (error != 0) { 3158 recovp->rc_error = error; 3159 nfs4_queue_event(RE_UNEXPECTED_ERRNO, mi, NULL, error, NULL, 3160 NULL, 0, NULL, 0, TAG_NONE, TAG_NONE, 0, 0); 3161 action = NR_CLIENTID; 3162 } else { 3163 recovp->rc_error = geterrno4(stat); 3164 switch (stat) { 3165 #ifdef notyet 3166 case NFS4ERR_LEASE_MOVED: 3167 action = xxx; 3168 break; 3169 case NFS4ERR_MOVED: 3170 action = xxx; 3171 break; 3172 #endif 3173 case NFS4ERR_BADHANDLE: 3174 action = NR_BADHANDLE; 3175 break; 3176 case NFS4ERR_BAD_SEQID: 3177 if (bsep) 3178 save_bseqid_rqst(bsep, recovp); 3179 action = NR_BAD_SEQID; 3180 break; 3181 case NFS4ERR_OLD_STATEID: 3182 action = NR_OLDSTATEID; 3183 break; 3184 case NFS4ERR_WRONGSEC: 3185 action = NR_WRONGSEC; 3186 break; 3187 case NFS4ERR_FHEXPIRED: 3188 action = NR_FHEXPIRED; 3189 break; 3190 case NFS4ERR_BAD_STATEID: 3191 if (sp == NULL || (sp != NULL && inlease(sp))) { 3192 3193 action = NR_BAD_STATEID; 3194 if (sidp) 3195 recovp->rc_stateid = *sidp; 3196 } else 3197 action = NR_CLIENTID; 3198 break; 3199 case NFS4ERR_EXPIRED: 3200 /* 3201 * The client's lease has expired, either due 3202 * to a network partition or perhaps a client 3203 * error. In either case, try an NR_CLIENTID 3204 * style recovery. reboot remains false, since 3205 * there is no evidence the server has rebooted. 3206 * This will cause CLAIM_NULL opens and lock 3207 * requests without the reclaim bit. 3208 */ 3209 action = NR_CLIENTID; 3210 3211 DTRACE_PROBE4(nfs4__expired, 3212 nfs4_server_t *, sp, 3213 mntinfo4_t *, mi, 3214 stateid4 *, sidp, int, op); 3215 3216 break; 3217 case NFS4ERR_STALE_CLIENTID: 3218 case NFS4ERR_STALE_STATEID: 3219 action = NR_CLIENTID; 3220 reboot = TRUE; 3221 break; 3222 case NFS4ERR_RESOURCE: 3223 /* 3224 * If this had been a FAILOVER mount, then 3225 * we'd have tried failover. Since it's not, 3226 * just delay a while and retry. 3227 */ 3228 action = NR_DELAY; 3229 break; 3230 case NFS4ERR_GRACE: 3231 action = NR_GRACE; 3232 break; 3233 case NFS4ERR_DELAY: 3234 action = NR_DELAY; 3235 break; 3236 case NFS4ERR_STALE: 3237 action = NR_STALE; 3238 break; 3239 default: 3240 nfs4_queue_event(RE_UNEXPECTED_STATUS, mi, NULL, 0, 3241 NULL, NULL, stat, NULL, 0, TAG_NONE, TAG_NONE, 3242 0, 0); 3243 action = NR_CLIENTID; 3244 break; 3245 } 3246 } 3247 3248 /* make sure action got set */ 3249 ASSERT(action != NR_UNUSED); 3250 recovp->rc_srv_reboot = reboot; 3251 recovp->rc_action = action; 3252 nfs4_queue_fact(RF_ERR, mi, stat, action, op, reboot, NULL, error, 3253 NULL); 3254 } 3255 3256 /* 3257 * Return the (held) credential for the process with the given pid. 3258 * May return NULL (e.g., process not found). 3259 */ 3260 3261 static cred_t * 3262 pid_to_cr(pid_t pid) 3263 { 3264 proc_t *p; 3265 cred_t *cr; 3266 3267 mutex_enter(&pidlock); 3268 if ((p = prfind(pid)) == NULL) { 3269 mutex_exit(&pidlock); 3270 return (NULL); 3271 } 3272 3273 mutex_enter(&p->p_crlock); 3274 crhold(cr = p->p_cred); 3275 mutex_exit(&p->p_crlock); 3276 mutex_exit(&pidlock); 3277 3278 return (cr); 3279 } 3280 3281 /* 3282 * Send SIGLOST to the given process and queue the event. 3283 * 3284 * The 'dump' boolean tells us whether this action should dump the 3285 * in-kernel queue of recovery messages or not. 3286 */ 3287 3288 void 3289 nfs4_send_siglost(pid_t pid, mntinfo4_t *mi, vnode_t *vp, bool_t dump, 3290 int error, nfsstat4 stat) 3291 { 3292 proc_t *p; 3293 3294 mutex_enter(&pidlock); 3295 p = prfind(pid); 3296 if (p) 3297 psignal(p, SIGLOST); 3298 mutex_exit(&pidlock); 3299 nfs4_queue_event(dump ? RE_SIGLOST : RE_SIGLOST_NO_DUMP, mi, 3300 NULL, error, vp, NULL, stat, NULL, pid, TAG_NONE, TAG_NONE, 0, 0); 3301 } 3302 3303 /* 3304 * Scan the lock list for entries that match the given pid. Change the 3305 * pid in those that do to NOPID. 3306 */ 3307 3308 static void 3309 relock_skip_pid(locklist_t *llp, pid_t pid) 3310 { 3311 for (; llp != NULL; llp = llp->ll_next) { 3312 if (llp->ll_flock.l_pid == pid) 3313 llp->ll_flock.l_pid = NOPID; 3314 } 3315 } 3316 3317 /* 3318 * Mark a file as having failed recovery, after making a last-ditch effort 3319 * to return any delegation. 3320 * 3321 * Sets r_error to EIO or ESTALE for the given vnode. 3322 */ 3323 void 3324 nfs4_fail_recov(vnode_t *vp, char *why, int error, nfsstat4 stat) 3325 { 3326 rnode4_t *rp = VTOR4(vp); 3327 3328 #ifdef DEBUG 3329 if (nfs4_fail_recov_stop) 3330 debug_enter("nfs4_fail_recov"); 3331 #endif 3332 3333 mutex_enter(&rp->r_statelock); 3334 if (rp->r_flags & (R4RECOVERR|R4RECOVERRP)) { 3335 mutex_exit(&rp->r_statelock); 3336 return; 3337 } 3338 3339 /* 3340 * Set R4RECOVERRP to indicate that a recovery error is in 3341 * progress. This will shut down reads and writes at the top 3342 * half. Don't set R4RECOVERR until after we've returned the 3343 * delegation, otherwise it will fail. 3344 */ 3345 3346 rp->r_flags |= R4RECOVERRP; 3347 mutex_exit(&rp->r_statelock); 3348 3349 nfs4delegabandon(rp); 3350 3351 mutex_enter(&rp->r_statelock); 3352 rp->r_flags |= (R4RECOVERR | R4STALE); 3353 rp->r_error = (error == 0 && stat == NFS4ERR_STALE) ? ESTALE : EIO; 3354 PURGE_ATTRCACHE4_LOCKED(rp); 3355 if (!(vp->v_vfsp->vfs_flag & VFS_UNMOUNTED)) 3356 nfs4_queue_event(RE_DEAD_FILE, VTOMI4(vp), NULL, error, 3357 vp, NULL, stat, why, 0, TAG_NONE, TAG_NONE, 0, 0); 3358 mutex_exit(&rp->r_statelock); 3359 3360 dnlc_purge_vp(vp); 3361 } 3362 3363 /* 3364 * recov_throttle: if the file had the same recovery action within the 3365 * throttle interval, wait for the throttle interval to finish before 3366 * proceeding. 3367 * 3368 * Side effects: updates the rnode with the current recovery information. 3369 */ 3370 3371 static void 3372 recov_throttle(recov_info_t *recovp, vnode_t *vp) 3373 { 3374 time_t curtime, time_to_wait; 3375 rnode4_t *rp = VTOR4(vp); 3376 3377 curtime = gethrestime_sec(); 3378 3379 mutex_enter(&rp->r_statelock); 3380 NFS4_DEBUG(nfs4_client_recov_debug, (CE_NOTE, 3381 "recov_throttle: now: (%d, %ld), last: (%d, %ld)", 3382 recovp->rc_action, curtime, 3383 rp->r_recov_act, rp->r_last_recov)); 3384 if (recovp->rc_action == rp->r_recov_act && 3385 rp->r_last_recov + recov_err_delay > curtime) { 3386 time_to_wait = rp->r_last_recov + recov_err_delay - curtime; 3387 mutex_exit(&rp->r_statelock); 3388 delay(SEC_TO_TICK(time_to_wait)); 3389 curtime = gethrestime_sec(); 3390 mutex_enter(&rp->r_statelock); 3391 } 3392 3393 rp->r_last_recov = curtime; 3394 rp->r_recov_act = recovp->rc_action; 3395 mutex_exit(&rp->r_statelock); 3396 } 3397 3398 /* 3399 * React to NFS4ERR_GRACE by setting the time we'll permit 3400 * the next call to this filesystem. 3401 */ 3402 void 3403 nfs4_set_grace_wait(mntinfo4_t *mi) 3404 { 3405 mutex_enter(&mi->mi_lock); 3406 /* Mark the time for the future */ 3407 mi->mi_grace_wait = gethrestime_sec() + nfs4err_delay_time; 3408 mutex_exit(&mi->mi_lock); 3409 } 3410 3411 /* 3412 * React to MFS4ERR_DELAY by setting the time we'll permit 3413 * the next call to this vnode. 3414 */ 3415 void 3416 nfs4_set_delay_wait(vnode_t *vp) 3417 { 3418 rnode4_t *rp = VTOR4(vp); 3419 3420 mutex_enter(&rp->r_statelock); 3421 /* 3422 * Calculate amount we should delay, initial 3423 * delay will be short and then we will back off. 3424 */ 3425 if (rp->r_delay_interval == 0) 3426 rp->r_delay_interval = NFS4_INITIAL_DELAY_INTERVAL; 3427 else 3428 /* calculate next interval value */ 3429 rp->r_delay_interval = 3430 MIN(NFS4_MAX_DELAY_INTERVAL, (rp->r_delay_interval << 1)); 3431 rp->r_delay_wait = gethrestime_sec() + rp->r_delay_interval; 3432 mutex_exit(&rp->r_statelock); 3433 } 3434 3435 /* 3436 * The caller is responsible for freeing the returned string. 3437 */ 3438 static char * 3439 nfs4_getsrvnames(mntinfo4_t *mi, size_t *len) 3440 { 3441 servinfo4_t *svp; 3442 char *srvnames; 3443 char *namep; 3444 size_t length; 3445 3446 /* 3447 * Calculate the length of the string required to hold all 3448 * of the server names plus either a comma or a null 3449 * character following each individual one. 3450 */ 3451 length = 0; 3452 for (svp = mi->mi_servers; svp != NULL; svp = svp->sv_next) { 3453 (void) nfs_rw_enter_sig(&svp->sv_lock, RW_READER, 0); 3454 if (svp->sv_flags & SV4_NOTINUSE) { 3455 nfs_rw_exit(&svp->sv_lock); 3456 continue; 3457 } 3458 nfs_rw_exit(&svp->sv_lock); 3459 length += svp->sv_hostnamelen; 3460 } 3461 3462 srvnames = kmem_alloc(length, KM_SLEEP); 3463 3464 namep = srvnames; 3465 for (svp = mi->mi_servers; svp != NULL; svp = svp->sv_next) { 3466 (void) nfs_rw_enter_sig(&svp->sv_lock, RW_READER, 0); 3467 if (svp->sv_flags & SV4_NOTINUSE) { 3468 nfs_rw_exit(&svp->sv_lock); 3469 continue; 3470 } 3471 nfs_rw_exit(&svp->sv_lock); 3472 (void) strcpy(namep, svp->sv_hostname); 3473 namep += svp->sv_hostnamelen - 1; 3474 *namep++ = ','; 3475 } 3476 *--namep = '\0'; 3477 3478 *len = length; 3479 3480 return (srvnames); 3481 } 3482 3483 static void 3484 save_bseqid_rqst(nfs4_bseqid_entry_t *bsep, recov_info_t *recovp) 3485 { 3486 nfs4_bseqid_entry_t *destp; 3487 3488 destp = kmem_alloc(sizeof (nfs4_bseqid_entry_t), KM_SLEEP); 3489 recovp->rc_bseqid_rqst = destp; 3490 3491 if (bsep->bs_oop) 3492 open_owner_hold(bsep->bs_oop); 3493 destp->bs_oop = bsep->bs_oop; 3494 if (bsep->bs_lop) 3495 lock_owner_hold(bsep->bs_lop); 3496 destp->bs_lop = bsep->bs_lop; 3497 if (bsep->bs_vp) 3498 VN_HOLD(bsep->bs_vp); 3499 destp->bs_vp = bsep->bs_vp; 3500 destp->bs_pid = bsep->bs_pid; 3501 destp->bs_tag = bsep->bs_tag; 3502 destp->bs_seqid = bsep->bs_seqid; 3503 } 3504 3505 static void 3506 free_bseqid_rqst(nfs4_bseqid_entry_t *bsep) 3507 { 3508 if (bsep->bs_oop) 3509 open_owner_rele(bsep->bs_oop); 3510 if (bsep->bs_lop) 3511 lock_owner_rele(bsep->bs_lop); 3512 if (bsep->bs_vp) 3513 VN_RELE(bsep->bs_vp); 3514 kmem_free(bsep, sizeof (nfs4_bseqid_entry_t)); 3515 } 3516 3517 /* 3518 * We don't actually fully recover from NFS4ERR_BAD_SEQID. We 3519 * simply mark the open owner and open stream (if provided) as "bad". 3520 * Then future uses of these data structures will be limited to basically 3521 * just cleaning up the internal client state (no going OTW). 3522 * 3523 * The result of this is to return errors back to the app/usr when 3524 * we receive NFS4ERR_BAD_SEQID, but also allow future/new calls to 3525 * succeed so progress can be made. 3526 */ 3527 void 3528 recov_bad_seqid(recov_info_t *recovp) 3529 { 3530 mntinfo4_t *mi = recovp->rc_mi; 3531 nfs4_open_owner_t *bad_oop; 3532 nfs4_lock_owner_t *bad_lop; 3533 vnode_t *vp; 3534 rnode4_t *rp = NULL; 3535 pid_t pid; 3536 nfs4_bseqid_entry_t *bsep, *tbsep; 3537 int error; 3538 3539 ASSERT(mi != NULL); 3540 ASSERT(nfs_rw_lock_held(&mi->mi_recovlock, RW_WRITER)); 3541 3542 mutex_enter(&mi->mi_lock); 3543 bsep = list_head(&mi->mi_bseqid_list); 3544 mutex_exit(&mi->mi_lock); 3545 3546 /* 3547 * Handle all the bad seqid entries on mi's list. 3548 */ 3549 while (bsep != NULL) { 3550 bad_oop = bsep->bs_oop; 3551 bad_lop = bsep->bs_lop; 3552 vp = bsep->bs_vp; 3553 pid = bsep->bs_pid; 3554 3555 NFS4_DEBUG(nfs4_client_recov_debug, (CE_NOTE, 3556 "recov_bad_seqid: mark oop %p lop %p as bad for " 3557 "vp %p tag %s pid %d: last good seqid %d for tag %s", 3558 (void *)bad_oop, (void *)bad_lop, (void *)vp, 3559 nfs4_ctags[bsep->bs_tag].ct_str, pid, 3560 bad_oop ? bad_oop->oo_last_good_seqid : 0, 3561 bad_oop ? nfs4_ctags[bad_oop->oo_last_good_op].ct_str : 3562 nfs4_ctags[TAG_NONE].ct_str)); 3563 3564 nfs4_queue_event(RE_BAD_SEQID, mi, NULL, 3565 0, vp, NULL, NFS4ERR_BAD_SEQID, NULL, pid, bsep->bs_tag, 3566 bad_oop ? bad_oop->oo_last_good_op : TAG_NONE, 3567 bsep->bs_seqid, bad_oop ? bad_oop->oo_last_good_seqid : 0); 3568 3569 if (bad_oop) { 3570 /* essentially reset the open owner */ 3571 error = nfs4_start_open_seqid_sync(bad_oop, mi); 3572 ASSERT(!error); /* recov thread always succeeds */ 3573 bad_oop->oo_name = nfs4_get_new_oo_name(); 3574 bad_oop->oo_seqid = 0; 3575 nfs4_end_open_seqid_sync(bad_oop); 3576 } 3577 3578 if (bad_lop) { 3579 mutex_enter(&bad_lop->lo_lock); 3580 bad_lop->lo_flags |= NFS4_BAD_SEQID_LOCK; 3581 mutex_exit(&bad_lop->lo_lock); 3582 3583 ASSERT(vp != NULL); 3584 rp = VTOR4(vp); 3585 mutex_enter(&rp->r_statelock); 3586 rp->r_flags |= R4LODANGLERS; 3587 mutex_exit(&rp->r_statelock); 3588 3589 nfs4_send_siglost(pid, mi, vp, TRUE, 3590 0, NFS4ERR_BAD_SEQID); 3591 } 3592 3593 mutex_enter(&mi->mi_lock); 3594 list_remove(&mi->mi_bseqid_list, bsep); 3595 tbsep = bsep; 3596 bsep = list_head(&mi->mi_bseqid_list); 3597 mutex_exit(&mi->mi_lock); 3598 free_bseqid_rqst(tbsep); 3599 } 3600 3601 mutex_enter(&mi->mi_lock); 3602 mi->mi_recovflags &= ~MI4R_BAD_SEQID; 3603 mutex_exit(&mi->mi_lock); 3604 } 3605