1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD 3 * 4 * Copyright (c) 2009 Rick Macklem, University of Guelph 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26 * SUCH DAMAGE. 27 * 28 */ 29 30 #include <sys/cdefs.h> 31 __FBSDID("$FreeBSD$"); 32 33 /* 34 * These functions implement the client side state handling for NFSv4. 35 * NFSv4 state handling: 36 * - A lockowner is used to determine lock contention, so it 37 * corresponds directly to a Posix pid. (1 to 1 mapping) 38 * - The correct granularity of an OpenOwner is not nearly so 39 * obvious. An OpenOwner does the following: 40 * - provides a serial sequencing of Open/Close/Lock-with-new-lockowner 41 * - is used to check for Open/Share contention (not applicable to 42 * this client, since all Opens are Deny_None) 43 * As such, I considered both extreme. 44 * 1 OpenOwner per ClientID - Simple to manage, but fully serializes 45 * all Open, Close and Lock (with a new lockowner) Ops. 46 * 1 OpenOwner for each Open - This one results in an OpenConfirm for 47 * every Open, for most servers. 48 * So, I chose to use the same mapping as I did for LockOwnwers. 49 * The main concern here is that you can end up with multiple Opens 50 * for the same File Handle, but on different OpenOwners (opens 51 * inherited from parents, grandparents...) and you do not know 52 * which of these the vnodeop close applies to. This is handled by 53 * delaying the Close Op(s) until all of the Opens have been closed. 54 * (It is not yet obvious if this is the correct granularity.) 55 * - How the code handles serialization: 56 * - For the ClientId, it uses an exclusive lock while getting its 57 * SetClientId and during recovery. Otherwise, it uses a shared 58 * lock via a reference count. 59 * - For the rest of the data structures, it uses an SMP mutex 60 * (once the nfs client is SMP safe) and doesn't sleep while 61 * manipulating the linked lists. 62 * - The serialization of Open/Close/Lock/LockU falls out in the 63 * "wash", since OpenOwners and LockOwners are both mapped from 64 * Posix pid. In other words, there is only one Posix pid using 65 * any given owner, so that owner is serialized. (If you change 66 * the granularity of the OpenOwner, then code must be added to 67 * serialize Ops on the OpenOwner.) 68 * - When to get rid of OpenOwners and LockOwners. 69 * - The function nfscl_cleanup_common() is executed after a process exits. 70 * It goes through the client list looking for all Open and Lock Owners. 71 * When one is found, it is marked "defunct" or in the case of 72 * an OpenOwner without any Opens, freed. 73 * The renew thread scans for defunct Owners and gets rid of them, 74 * if it can. The LockOwners will also be deleted when the 75 * associated Open is closed. 76 * - If the LockU or Close Op(s) fail during close in a way 77 * that could be recovered upon retry, they are relinked to the 78 * ClientId's defunct open list and retried by the renew thread 79 * until they succeed or an unmount/recovery occurs. 80 * (Since we are done with them, they do not need to be recovered.) 81 */ 82 83 #include <fs/nfs/nfsport.h> 84 85 /* 86 * Global variables 87 */ 88 extern struct nfsstatsv1 nfsstatsv1; 89 extern struct nfsreqhead nfsd_reqq; 90 extern u_int32_t newnfs_false, newnfs_true; 91 extern int nfscl_debuglevel; 92 extern int nfscl_enablecallb; 93 extern int nfs_numnfscbd; 94 NFSREQSPINLOCK; 95 NFSCLSTATEMUTEX; 96 int nfscl_inited = 0; 97 struct nfsclhead nfsclhead; /* Head of clientid list */ 98 int nfscl_deleghighwater = NFSCLDELEGHIGHWATER; 99 int nfscl_layouthighwater = NFSCLLAYOUTHIGHWATER; 100 101 static int nfscl_delegcnt = 0; 102 static int nfscl_layoutcnt = 0; 103 static int nfscl_getopen(struct nfsclownerhead *, struct nfsclopenhash *, 104 u_int8_t *, int, u_int8_t *, u_int8_t *, u_int32_t, 105 struct nfscllockowner **, struct nfsclopen **); 106 static bool nfscl_checkown(struct nfsclowner *, struct nfsclopen *, uint8_t *, 107 uint8_t *, struct nfscllockowner **, struct nfsclopen **, 108 struct nfsclopen **); 109 static void nfscl_clrelease(struct nfsclclient *); 110 static void nfscl_unlinkopen(struct nfsclopen *); 111 static void nfscl_cleanclient(struct nfsclclient *); 112 static void nfscl_expireclient(struct nfsclclient *, struct nfsmount *, 113 struct ucred *, NFSPROC_T *); 114 static int nfscl_expireopen(struct nfsclclient *, struct nfsclopen *, 115 struct nfsmount *, struct ucred *, NFSPROC_T *); 116 static void nfscl_recover(struct nfsclclient *, bool *, struct ucred *, 117 NFSPROC_T *); 118 static void nfscl_insertlock(struct nfscllockowner *, struct nfscllock *, 119 struct nfscllock *, int); 120 static int nfscl_updatelock(struct nfscllockowner *, struct nfscllock **, 121 struct nfscllock **, int); 122 static void nfscl_delegreturnall(struct nfsclclient *, NFSPROC_T *, 123 struct nfscldeleghead *); 124 static u_int32_t nfscl_nextcbident(void); 125 static mount_t nfscl_getmnt(int, uint8_t *, u_int32_t, struct nfsclclient **); 126 static struct nfsclclient *nfscl_getclnt(u_int32_t); 127 static struct nfsclclient *nfscl_getclntsess(uint8_t *); 128 static struct nfscldeleg *nfscl_finddeleg(struct nfsclclient *, u_int8_t *, 129 int); 130 static void nfscl_retoncloselayout(vnode_t, struct nfsclclient *, uint8_t *, 131 int, struct nfsclrecalllayout **, struct nfscllayout **); 132 static void nfscl_reldevinfo_locked(struct nfscldevinfo *); 133 static struct nfscllayout *nfscl_findlayout(struct nfsclclient *, u_int8_t *, 134 int); 135 static struct nfscldevinfo *nfscl_finddevinfo(struct nfsclclient *, uint8_t *); 136 static int nfscl_checkconflict(struct nfscllockownerhead *, struct nfscllock *, 137 u_int8_t *, struct nfscllock **); 138 static void nfscl_freealllocks(struct nfscllockownerhead *, int); 139 static int nfscl_localconflict(struct nfsclclient *, u_int8_t *, int, 140 struct nfscllock *, u_int8_t *, struct nfscldeleg *, struct nfscllock **); 141 static void nfscl_newopen(struct nfsclclient *, struct nfscldeleg *, 142 struct nfsclowner **, struct nfsclowner **, struct nfsclopen **, 143 struct nfsclopen **, u_int8_t *, u_int8_t *, int, struct ucred *, int *); 144 static int nfscl_moveopen(vnode_t , struct nfsclclient *, 145 struct nfsmount *, struct nfsclopen *, struct nfsclowner *, 146 struct nfscldeleg *, struct ucred *, NFSPROC_T *); 147 static void nfscl_totalrecall(struct nfsclclient *); 148 static int nfscl_relock(vnode_t , struct nfsclclient *, struct nfsmount *, 149 struct nfscllockowner *, struct nfscllock *, struct ucred *, NFSPROC_T *); 150 static int nfscl_tryopen(struct nfsmount *, vnode_t , u_int8_t *, int, 151 u_int8_t *, int, u_int32_t, struct nfsclopen *, u_int8_t *, int, 152 struct nfscldeleg **, int, u_int32_t, struct ucred *, NFSPROC_T *); 153 static int nfscl_trylock(struct nfsmount *, vnode_t , u_int8_t *, 154 int, struct nfscllockowner *, int, int, u_int64_t, u_int64_t, short, 155 struct ucred *, NFSPROC_T *); 156 static int nfsrpc_reopen(struct nfsmount *, u_int8_t *, int, u_int32_t, 157 struct nfsclopen *, struct nfscldeleg **, struct ucred *, NFSPROC_T *); 158 static void nfscl_freedeleg(struct nfscldeleghead *, struct nfscldeleg *, 159 bool); 160 static int nfscl_errmap(struct nfsrv_descript *, u_int32_t); 161 static void nfscl_cleanup_common(struct nfsclclient *, u_int8_t *); 162 static int nfscl_recalldeleg(struct nfsclclient *, struct nfsmount *, 163 struct nfscldeleg *, vnode_t, struct ucred *, NFSPROC_T *, int, 164 vnode_t *); 165 static void nfscl_freeopenowner(struct nfsclowner *, int); 166 static void nfscl_cleandeleg(struct nfscldeleg *); 167 static void nfscl_emptylockowner(struct nfscllockowner *, 168 struct nfscllockownerfhhead *); 169 static void nfscl_mergeflayouts(struct nfsclflayouthead *, 170 struct nfsclflayouthead *); 171 static int nfscl_layoutrecall(int, struct nfscllayout *, uint32_t, uint64_t, 172 uint64_t, uint32_t, uint32_t, uint32_t, char *, struct nfsclrecalllayout *); 173 static int nfscl_seq(uint32_t, uint32_t); 174 static void nfscl_layoutreturn(struct nfsmount *, struct nfscllayout *, 175 struct ucred *, NFSPROC_T *); 176 static void nfscl_dolayoutcommit(struct nfsmount *, struct nfscllayout *, 177 struct ucred *, NFSPROC_T *); 178 179 static short nfscberr_null[] = { 180 0, 181 0, 182 }; 183 184 static short nfscberr_getattr[] = { 185 NFSERR_RESOURCE, 186 NFSERR_BADHANDLE, 187 NFSERR_BADXDR, 188 NFSERR_RESOURCE, 189 NFSERR_SERVERFAULT, 190 0, 191 }; 192 193 static short nfscberr_recall[] = { 194 NFSERR_RESOURCE, 195 NFSERR_BADHANDLE, 196 NFSERR_BADSTATEID, 197 NFSERR_BADXDR, 198 NFSERR_RESOURCE, 199 NFSERR_SERVERFAULT, 200 0, 201 }; 202 203 static short *nfscl_cberrmap[] = { 204 nfscberr_null, 205 nfscberr_null, 206 nfscberr_null, 207 nfscberr_getattr, 208 nfscberr_recall 209 }; 210 211 #define NETFAMILY(clp) \ 212 (((clp)->nfsc_flags & NFSCLFLAGS_AFINET6) ? AF_INET6 : AF_INET) 213 214 /* 215 * Called for an open operation. 216 * If the nfhp argument is NULL, just get an openowner. 217 */ 218 int 219 nfscl_open(vnode_t vp, u_int8_t *nfhp, int fhlen, u_int32_t amode, int usedeleg, 220 struct ucred *cred, NFSPROC_T *p, struct nfsclowner **owpp, 221 struct nfsclopen **opp, int *newonep, int *retp, int lockit, bool firstref) 222 { 223 struct nfsclclient *clp; 224 struct nfsclowner *owp, *nowp; 225 struct nfsclopen *op = NULL, *nop = NULL; 226 struct nfscldeleg *dp; 227 struct nfsclownerhead *ohp; 228 u_int8_t own[NFSV4CL_LOCKNAMELEN]; 229 int ret; 230 231 if (newonep != NULL) 232 *newonep = 0; 233 if (opp != NULL) 234 *opp = NULL; 235 if (owpp != NULL) 236 *owpp = NULL; 237 238 /* 239 * Might need one or both of these, so MALLOC them now, to 240 * avoid a tsleep() in MALLOC later. 241 */ 242 nowp = malloc(sizeof (struct nfsclowner), 243 M_NFSCLOWNER, M_WAITOK); 244 if (nfhp != NULL) { 245 nop = malloc(sizeof (struct nfsclopen) + 246 fhlen - 1, M_NFSCLOPEN, M_WAITOK); 247 nop->nfso_hash.le_prev = NULL; 248 } 249 ret = nfscl_getcl(vp->v_mount, cred, p, false, firstref, &clp); 250 if (ret != 0) { 251 free(nowp, M_NFSCLOWNER); 252 if (nop != NULL) 253 free(nop, M_NFSCLOPEN); 254 return (ret); 255 } 256 257 /* 258 * Get the Open iff it already exists. 259 * If none found, add the new one or return error, depending upon 260 * "create". 261 */ 262 NFSLOCKCLSTATE(); 263 dp = NULL; 264 /* First check the delegation list */ 265 if (nfhp != NULL && usedeleg) { 266 LIST_FOREACH(dp, NFSCLDELEGHASH(clp, nfhp, fhlen), nfsdl_hash) { 267 if (dp->nfsdl_fhlen == fhlen && 268 !NFSBCMP(nfhp, dp->nfsdl_fh, fhlen)) { 269 if (!(amode & NFSV4OPEN_ACCESSWRITE) || 270 (dp->nfsdl_flags & NFSCLDL_WRITE)) 271 break; 272 dp = NULL; 273 break; 274 } 275 } 276 } 277 278 /* For NFSv4.1/4.2 and this option, use a single open_owner. */ 279 if (NFSHASONEOPENOWN(VFSTONFS(vp->v_mount))) 280 nfscl_filllockowner(NULL, own, F_POSIX); 281 else 282 nfscl_filllockowner(p->td_proc, own, F_POSIX); 283 if (dp != NULL) 284 ohp = &dp->nfsdl_owner; 285 else 286 ohp = &clp->nfsc_owner; 287 /* Now, search for an openowner */ 288 LIST_FOREACH(owp, ohp, nfsow_list) { 289 if (!NFSBCMP(owp->nfsow_owner, own, NFSV4CL_LOCKNAMELEN)) 290 break; 291 } 292 293 /* 294 * Create a new open, as required. 295 */ 296 nfscl_newopen(clp, dp, &owp, &nowp, &op, &nop, own, nfhp, fhlen, 297 cred, newonep); 298 299 /* 300 * Now, check the mode on the open and return the appropriate 301 * value. 302 */ 303 if (retp != NULL) { 304 if (nfhp != NULL && dp != NULL && nop == NULL) 305 /* new local open on delegation */ 306 *retp = NFSCLOPEN_SETCRED; 307 else 308 *retp = NFSCLOPEN_OK; 309 } 310 if (op != NULL && (amode & ~(op->nfso_mode))) { 311 op->nfso_mode |= amode; 312 if (retp != NULL && dp == NULL) 313 *retp = NFSCLOPEN_DOOPEN; 314 } 315 316 /* 317 * Serialize modifications to the open owner for multiple threads 318 * within the same process using a read/write sleep lock. 319 * For NFSv4.1 and a single OpenOwner, allow concurrent open operations 320 * by acquiring a shared lock. The close operations still use an 321 * exclusive lock for this case. 322 */ 323 if (lockit != 0) { 324 if (NFSHASONEOPENOWN(VFSTONFS(vp->v_mount))) { 325 /* 326 * Get a shared lock on the OpenOwner, but first 327 * wait for any pending exclusive lock, so that the 328 * exclusive locker gets priority. 329 */ 330 nfsv4_lock(&owp->nfsow_rwlock, 0, NULL, 331 NFSCLSTATEMUTEXPTR, NULL); 332 nfsv4_getref(&owp->nfsow_rwlock, NULL, 333 NFSCLSTATEMUTEXPTR, NULL); 334 } else 335 nfscl_lockexcl(&owp->nfsow_rwlock, NFSCLSTATEMUTEXPTR); 336 } 337 NFSUNLOCKCLSTATE(); 338 if (nowp != NULL) 339 free(nowp, M_NFSCLOWNER); 340 if (nop != NULL) 341 free(nop, M_NFSCLOPEN); 342 if (owpp != NULL) 343 *owpp = owp; 344 if (opp != NULL) 345 *opp = op; 346 return (0); 347 } 348 349 /* 350 * Create a new open, as required. 351 */ 352 static void 353 nfscl_newopen(struct nfsclclient *clp, struct nfscldeleg *dp, 354 struct nfsclowner **owpp, struct nfsclowner **nowpp, struct nfsclopen **opp, 355 struct nfsclopen **nopp, u_int8_t *own, u_int8_t *fhp, int fhlen, 356 struct ucred *cred, int *newonep) 357 { 358 struct nfsclowner *owp = *owpp, *nowp; 359 struct nfsclopen *op, *nop; 360 361 if (nowpp != NULL) 362 nowp = *nowpp; 363 else 364 nowp = NULL; 365 if (nopp != NULL) 366 nop = *nopp; 367 else 368 nop = NULL; 369 if (owp == NULL && nowp != NULL) { 370 NFSBCOPY(own, nowp->nfsow_owner, NFSV4CL_LOCKNAMELEN); 371 LIST_INIT(&nowp->nfsow_open); 372 nowp->nfsow_clp = clp; 373 nowp->nfsow_seqid = 0; 374 nowp->nfsow_defunct = 0; 375 nfscl_lockinit(&nowp->nfsow_rwlock); 376 if (dp != NULL) { 377 nfsstatsv1.cllocalopenowners++; 378 LIST_INSERT_HEAD(&dp->nfsdl_owner, nowp, nfsow_list); 379 } else { 380 nfsstatsv1.clopenowners++; 381 LIST_INSERT_HEAD(&clp->nfsc_owner, nowp, nfsow_list); 382 } 383 owp = *owpp = nowp; 384 *nowpp = NULL; 385 if (newonep != NULL) 386 *newonep = 1; 387 } 388 389 /* If an fhp has been specified, create an Open as well. */ 390 if (fhp != NULL) { 391 /* and look for the correct open, based upon FH */ 392 LIST_FOREACH(op, &owp->nfsow_open, nfso_list) { 393 if (op->nfso_fhlen == fhlen && 394 !NFSBCMP(op->nfso_fh, fhp, fhlen)) 395 break; 396 } 397 if (op == NULL && nop != NULL) { 398 nop->nfso_own = owp; 399 nop->nfso_mode = 0; 400 nop->nfso_opencnt = 0; 401 nop->nfso_posixlock = 1; 402 nop->nfso_fhlen = fhlen; 403 NFSBCOPY(fhp, nop->nfso_fh, fhlen); 404 LIST_INIT(&nop->nfso_lock); 405 nop->nfso_stateid.seqid = 0; 406 nop->nfso_stateid.other[0] = 0; 407 nop->nfso_stateid.other[1] = 0; 408 nop->nfso_stateid.other[2] = 0; 409 KASSERT(cred != NULL, ("%s: cred NULL\n", __func__)); 410 newnfs_copyincred(cred, &nop->nfso_cred); 411 if (dp != NULL) { 412 TAILQ_REMOVE(&clp->nfsc_deleg, dp, nfsdl_list); 413 TAILQ_INSERT_HEAD(&clp->nfsc_deleg, dp, 414 nfsdl_list); 415 dp->nfsdl_timestamp = NFSD_MONOSEC + 120; 416 nfsstatsv1.cllocalopens++; 417 } else { 418 LIST_INSERT_HEAD(NFSCLOPENHASH(clp, fhp, fhlen), 419 nop, nfso_hash); 420 nfsstatsv1.clopens++; 421 } 422 LIST_INSERT_HEAD(&owp->nfsow_open, nop, nfso_list); 423 *opp = nop; 424 *nopp = NULL; 425 if (newonep != NULL) 426 *newonep = 1; 427 } else { 428 *opp = op; 429 } 430 } 431 } 432 433 /* 434 * Called to find/add a delegation to a client. 435 */ 436 int 437 nfscl_deleg(mount_t mp, struct nfsclclient *clp, u_int8_t *nfhp, 438 int fhlen, struct ucred *cred, NFSPROC_T *p, struct nfscldeleg **dpp) 439 { 440 struct nfscldeleg *dp = *dpp, *tdp; 441 struct nfsmount *nmp; 442 443 KASSERT(mp != NULL, ("nfscl_deleg: mp NULL")); 444 nmp = VFSTONFS(mp); 445 /* 446 * First, if we have received a Read delegation for a file on a 447 * read/write file system, just return it, because they aren't 448 * useful, imho. 449 */ 450 if (dp != NULL && !NFSMNT_RDONLY(mp) && 451 (dp->nfsdl_flags & NFSCLDL_READ)) { 452 nfscl_trydelegreturn(dp, cred, nmp, p); 453 free(dp, M_NFSCLDELEG); 454 *dpp = NULL; 455 return (0); 456 } 457 458 /* 459 * Since a delegation might be added to the mount, 460 * set NFSMNTP_DELEGISSUED now. If a delegation already 461 * exagain ists, setting this flag is harmless. 462 */ 463 NFSLOCKMNT(nmp); 464 nmp->nm_privflag |= NFSMNTP_DELEGISSUED; 465 NFSUNLOCKMNT(nmp); 466 467 /* Look for the correct deleg, based upon FH */ 468 NFSLOCKCLSTATE(); 469 tdp = nfscl_finddeleg(clp, nfhp, fhlen); 470 if (tdp == NULL) { 471 if (dp == NULL) { 472 NFSUNLOCKCLSTATE(); 473 return (NFSERR_BADSTATEID); 474 } 475 *dpp = NULL; 476 TAILQ_INSERT_HEAD(&clp->nfsc_deleg, dp, nfsdl_list); 477 LIST_INSERT_HEAD(NFSCLDELEGHASH(clp, nfhp, fhlen), dp, 478 nfsdl_hash); 479 dp->nfsdl_timestamp = NFSD_MONOSEC + 120; 480 nfsstatsv1.cldelegates++; 481 nfscl_delegcnt++; 482 } else { 483 /* 484 * Delegation already exists, what do we do if a new one?? 485 */ 486 if (dp != NULL) { 487 printf("Deleg already exists!\n"); 488 free(dp, M_NFSCLDELEG); 489 *dpp = NULL; 490 } else { 491 *dpp = tdp; 492 } 493 } 494 NFSUNLOCKCLSTATE(); 495 return (0); 496 } 497 498 /* 499 * Find a delegation for this file handle. Return NULL upon failure. 500 */ 501 static struct nfscldeleg * 502 nfscl_finddeleg(struct nfsclclient *clp, u_int8_t *fhp, int fhlen) 503 { 504 struct nfscldeleg *dp; 505 506 LIST_FOREACH(dp, NFSCLDELEGHASH(clp, fhp, fhlen), nfsdl_hash) { 507 if (dp->nfsdl_fhlen == fhlen && 508 !NFSBCMP(dp->nfsdl_fh, fhp, fhlen)) 509 break; 510 } 511 return (dp); 512 } 513 514 /* 515 * Get a stateid for an I/O operation. First, look for an open and iff 516 * found, return either a lockowner stateid or the open stateid. 517 * If no Open is found, just return error and the special stateid of all zeros. 518 */ 519 int 520 nfscl_getstateid(vnode_t vp, u_int8_t *nfhp, int fhlen, u_int32_t mode, 521 int fords, struct ucred *cred, NFSPROC_T *p, nfsv4stateid_t *stateidp, 522 void **lckpp) 523 { 524 struct nfsclclient *clp; 525 struct nfsclopen *op = NULL, *top; 526 struct nfsclopenhash *oph; 527 struct nfscllockowner *lp; 528 struct nfscldeleg *dp; 529 struct nfsnode *np; 530 struct nfsmount *nmp; 531 u_int8_t own[NFSV4CL_LOCKNAMELEN], lockown[NFSV4CL_LOCKNAMELEN]; 532 int error; 533 bool done; 534 535 *lckpp = NULL; 536 /* 537 * Initially, just set the special stateid of all zeros. 538 * (Don't do this for a DS, since the special stateid can't be used.) 539 */ 540 if (fords == 0) { 541 stateidp->seqid = 0; 542 stateidp->other[0] = 0; 543 stateidp->other[1] = 0; 544 stateidp->other[2] = 0; 545 } 546 if (vnode_vtype(vp) != VREG) 547 return (EISDIR); 548 np = VTONFS(vp); 549 nmp = VFSTONFS(vp->v_mount); 550 551 /* 552 * For "oneopenown" mounts, first check for a cached open in the 553 * NFS vnode, that can be used as a stateid. This can only be 554 * done if no delegations have been issued to the mount and no 555 * byte range file locking has been done for the file. 556 */ 557 if (NFSHASNFSV4N(nmp) && NFSHASONEOPENOWN(nmp) && fords == 0) { 558 NFSLOCKMNT(nmp); 559 NFSLOCKNODE(np); 560 if ((nmp->nm_privflag & NFSMNTP_DELEGISSUED) == 0 && 561 (np->n_flag & NMIGHTBELOCKED) == 0 && 562 np->n_openstateid != NULL) { 563 stateidp->seqid = 0; 564 stateidp->other[0] = 565 np->n_openstateid->nfso_stateid.other[0]; 566 stateidp->other[1] = 567 np->n_openstateid->nfso_stateid.other[1]; 568 stateidp->other[2] = 569 np->n_openstateid->nfso_stateid.other[2]; 570 NFSUNLOCKNODE(np); 571 NFSUNLOCKMNT(nmp); 572 return (0); 573 } 574 NFSUNLOCKNODE(np); 575 NFSUNLOCKMNT(nmp); 576 } 577 578 NFSLOCKCLSTATE(); 579 clp = nfscl_findcl(nmp); 580 if (clp == NULL) { 581 NFSUNLOCKCLSTATE(); 582 return (EACCES); 583 } 584 585 /* 586 * Wait for recovery to complete. 587 */ 588 while ((clp->nfsc_flags & NFSCLFLAGS_RECVRINPROG)) 589 (void) nfsmsleep(&clp->nfsc_flags, NFSCLSTATEMUTEXPTR, 590 PZERO, "nfsrecvr", NULL); 591 592 /* 593 * First, look for a delegation. 594 */ 595 LIST_FOREACH(dp, NFSCLDELEGHASH(clp, nfhp, fhlen), nfsdl_hash) { 596 if (dp->nfsdl_fhlen == fhlen && 597 !NFSBCMP(nfhp, dp->nfsdl_fh, fhlen)) { 598 if (!(mode & NFSV4OPEN_ACCESSWRITE) || 599 (dp->nfsdl_flags & NFSCLDL_WRITE)) { 600 if (NFSHASNFSV4N(nmp)) 601 stateidp->seqid = 0; 602 else 603 stateidp->seqid = 604 dp->nfsdl_stateid.seqid; 605 stateidp->other[0] = dp->nfsdl_stateid.other[0]; 606 stateidp->other[1] = dp->nfsdl_stateid.other[1]; 607 stateidp->other[2] = dp->nfsdl_stateid.other[2]; 608 if (!(np->n_flag & NDELEGRECALL)) { 609 TAILQ_REMOVE(&clp->nfsc_deleg, dp, 610 nfsdl_list); 611 TAILQ_INSERT_HEAD(&clp->nfsc_deleg, dp, 612 nfsdl_list); 613 dp->nfsdl_timestamp = NFSD_MONOSEC + 614 120; 615 dp->nfsdl_rwlock.nfslock_usecnt++; 616 *lckpp = (void *)&dp->nfsdl_rwlock; 617 } 618 NFSUNLOCKCLSTATE(); 619 return (0); 620 } 621 break; 622 } 623 } 624 625 if (p != NULL) { 626 /* 627 * If p != NULL, we want to search the parentage tree 628 * for a matching OpenOwner and use that. 629 */ 630 if (NFSHASONEOPENOWN(VFSTONFS(vp->v_mount))) 631 nfscl_filllockowner(NULL, own, F_POSIX); 632 else 633 nfscl_filllockowner(p->td_proc, own, F_POSIX); 634 nfscl_filllockowner(p->td_proc, lockown, F_POSIX); 635 lp = NULL; 636 error = nfscl_getopen(NULL, clp->nfsc_openhash, nfhp, fhlen, 637 own, lockown, mode, &lp, &op); 638 if (error == 0 && lp != NULL && fords == 0) { 639 /* Don't return a lock stateid for a DS. */ 640 if (NFSHASNFSV4N(nmp)) 641 stateidp->seqid = 0; 642 else 643 stateidp->seqid = lp->nfsl_stateid.seqid; 644 stateidp->other[0] = 645 lp->nfsl_stateid.other[0]; 646 stateidp->other[1] = 647 lp->nfsl_stateid.other[1]; 648 stateidp->other[2] = 649 lp->nfsl_stateid.other[2]; 650 NFSUNLOCKCLSTATE(); 651 return (0); 652 } 653 } 654 if (op == NULL) { 655 /* If not found, just look for any OpenOwner that will work. */ 656 top = NULL; 657 done = false; 658 oph = NFSCLOPENHASH(clp, nfhp, fhlen); 659 LIST_FOREACH(op, oph, nfso_hash) { 660 if (op->nfso_fhlen == fhlen && 661 !NFSBCMP(op->nfso_fh, nfhp, fhlen)) { 662 if (top == NULL && (op->nfso_mode & 663 NFSV4OPEN_ACCESSWRITE) != 0 && 664 (mode & NFSV4OPEN_ACCESSREAD) != 0) 665 top = op; 666 if ((mode & op->nfso_mode) == mode) { 667 /* LRU order the hash list. */ 668 LIST_REMOVE(op, nfso_hash); 669 LIST_INSERT_HEAD(oph, op, nfso_hash); 670 done = true; 671 break; 672 } 673 } 674 } 675 if (!done) { 676 NFSCL_DEBUG(2, "openmode top=%p\n", top); 677 if (top == NULL || NFSHASOPENMODE(nmp)) { 678 NFSUNLOCKCLSTATE(); 679 return (ENOENT); 680 } else 681 op = top; 682 } 683 /* 684 * For read aheads or write behinds, use the open cred. 685 * A read ahead or write behind is indicated by p == NULL. 686 */ 687 if (p == NULL) 688 newnfs_copycred(&op->nfso_cred, cred); 689 } 690 691 /* 692 * No lock stateid, so return the open stateid. 693 */ 694 if (NFSHASNFSV4N(nmp)) 695 stateidp->seqid = 0; 696 else 697 stateidp->seqid = op->nfso_stateid.seqid; 698 stateidp->other[0] = op->nfso_stateid.other[0]; 699 stateidp->other[1] = op->nfso_stateid.other[1]; 700 stateidp->other[2] = op->nfso_stateid.other[2]; 701 NFSUNLOCKCLSTATE(); 702 return (0); 703 } 704 705 /* 706 * Search for a matching file, mode and, optionally, lockowner. 707 */ 708 static int 709 nfscl_getopen(struct nfsclownerhead *ohp, struct nfsclopenhash *ohashp, 710 u_int8_t *nfhp, int fhlen, u_int8_t *openown, u_int8_t *lockown, 711 u_int32_t mode, struct nfscllockowner **lpp, struct nfsclopen **opp) 712 { 713 struct nfsclowner *owp; 714 struct nfsclopen *op, *rop, *rop2; 715 struct nfsclopenhash *oph; 716 bool keep_looping; 717 718 KASSERT(ohp == NULL || ohashp == NULL, ("nfscl_getopen: " 719 "only one of ohp and ohashp can be set")); 720 if (lpp != NULL) 721 *lpp = NULL; 722 /* 723 * rop will be set to the open to be returned. There are three 724 * variants of this, all for an open of the correct file: 725 * 1 - A match of lockown. 726 * 2 - A match of the openown, when no lockown match exists. 727 * 3 - A match for any open, if no openown or lockown match exists. 728 * Looking for #2 over #3 probably isn't necessary, but since 729 * RFC3530 is vague w.r.t. the relationship between openowners and 730 * lockowners, I think this is the safer way to go. 731 */ 732 rop = NULL; 733 rop2 = NULL; 734 keep_looping = true; 735 /* Search the client list */ 736 if (ohashp == NULL) { 737 /* Search the local opens on the delegation. */ 738 LIST_FOREACH(owp, ohp, nfsow_list) { 739 /* and look for the correct open */ 740 LIST_FOREACH(op, &owp->nfsow_open, nfso_list) { 741 if (op->nfso_fhlen == fhlen && 742 !NFSBCMP(op->nfso_fh, nfhp, fhlen) 743 && (op->nfso_mode & mode) == mode) 744 keep_looping = nfscl_checkown(owp, op, openown, 745 lockown, lpp, &rop, &rop2); 746 if (!keep_looping) 747 break; 748 } 749 if (!keep_looping) 750 break; 751 } 752 } else { 753 /* Search for matching opens on the hash list. */ 754 oph = &ohashp[NFSCLOPENHASHFUNC(nfhp, fhlen)]; 755 LIST_FOREACH(op, oph, nfso_hash) { 756 if (op->nfso_fhlen == fhlen && 757 !NFSBCMP(op->nfso_fh, nfhp, fhlen) 758 && (op->nfso_mode & mode) == mode) 759 keep_looping = nfscl_checkown(op->nfso_own, op, 760 openown, lockown, lpp, &rop, &rop2); 761 if (!keep_looping) { 762 /* LRU order the hash list. */ 763 LIST_REMOVE(op, nfso_hash); 764 LIST_INSERT_HEAD(oph, op, nfso_hash); 765 break; 766 } 767 } 768 } 769 if (rop == NULL) 770 rop = rop2; 771 if (rop == NULL) 772 return (EBADF); 773 *opp = rop; 774 return (0); 775 } 776 777 /* Check for an owner match. */ 778 static bool 779 nfscl_checkown(struct nfsclowner *owp, struct nfsclopen *op, uint8_t *openown, 780 uint8_t *lockown, struct nfscllockowner **lpp, struct nfsclopen **ropp, 781 struct nfsclopen **ropp2) 782 { 783 struct nfscllockowner *lp; 784 bool keep_looping; 785 786 keep_looping = true; 787 if (lpp != NULL) { 788 /* Now look for a matching lockowner. */ 789 LIST_FOREACH(lp, &op->nfso_lock, nfsl_list) { 790 if (!NFSBCMP(lp->nfsl_owner, lockown, 791 NFSV4CL_LOCKNAMELEN)) { 792 *lpp = lp; 793 *ropp = op; 794 return (false); 795 } 796 } 797 } 798 if (*ropp == NULL && !NFSBCMP(owp->nfsow_owner, openown, 799 NFSV4CL_LOCKNAMELEN)) { 800 *ropp = op; 801 if (lpp == NULL) 802 keep_looping = false; 803 } 804 if (*ropp2 == NULL) 805 *ropp2 = op; 806 return (keep_looping); 807 } 808 809 /* 810 * Release use of an open owner. Called when open operations are done 811 * with the open owner. 812 */ 813 void 814 nfscl_ownerrelease(struct nfsmount *nmp, struct nfsclowner *owp, 815 __unused int error, __unused int candelete, int unlocked) 816 { 817 818 if (owp == NULL) 819 return; 820 NFSLOCKCLSTATE(); 821 if (unlocked == 0) { 822 if (NFSHASONEOPENOWN(nmp)) 823 nfsv4_relref(&owp->nfsow_rwlock); 824 else 825 nfscl_lockunlock(&owp->nfsow_rwlock); 826 } 827 nfscl_clrelease(owp->nfsow_clp); 828 NFSUNLOCKCLSTATE(); 829 } 830 831 /* 832 * Release use of an open structure under an open owner. 833 */ 834 void 835 nfscl_openrelease(struct nfsmount *nmp, struct nfsclopen *op, int error, 836 int candelete) 837 { 838 struct nfsclclient *clp; 839 struct nfsclowner *owp; 840 841 if (op == NULL) 842 return; 843 NFSLOCKCLSTATE(); 844 owp = op->nfso_own; 845 if (NFSHASONEOPENOWN(nmp)) 846 nfsv4_relref(&owp->nfsow_rwlock); 847 else 848 nfscl_lockunlock(&owp->nfsow_rwlock); 849 clp = owp->nfsow_clp; 850 if (error && candelete && op->nfso_opencnt == 0) 851 nfscl_freeopen(op, 0, true); 852 nfscl_clrelease(clp); 853 NFSUNLOCKCLSTATE(); 854 } 855 856 /* 857 * Called to get a clientid structure. It will optionally lock the 858 * client data structures to do the SetClientId/SetClientId_confirm, 859 * but will release that lock and return the clientid with a reference 860 * count on it. 861 * If the "cred" argument is NULL, a new clientid should not be created. 862 * If the "p" argument is NULL, a SetClientID/SetClientIDConfirm cannot 863 * be done. 864 * It always clpp with a reference count on it, unless returning an error. 865 */ 866 int 867 nfscl_getcl(struct mount *mp, struct ucred *cred, NFSPROC_T *p, 868 bool tryminvers, bool firstref, struct nfsclclient **clpp) 869 { 870 struct nfsclclient *clp; 871 struct nfsclclient *newclp = NULL; 872 struct nfsmount *nmp; 873 char uuid[HOSTUUIDLEN]; 874 int igotlock = 0, error, trystalecnt, clidinusedelay, i; 875 u_int16_t idlen = 0; 876 877 nmp = VFSTONFS(mp); 878 if (cred != NULL) { 879 getcredhostuuid(cred, uuid, sizeof uuid); 880 idlen = strlen(uuid); 881 if (idlen > 0) 882 idlen += sizeof (u_int64_t); 883 else 884 idlen += sizeof (u_int64_t) + 16; /* 16 random bytes */ 885 newclp = malloc( 886 sizeof (struct nfsclclient) + idlen - 1, M_NFSCLCLIENT, 887 M_WAITOK | M_ZERO); 888 } 889 NFSLOCKCLSTATE(); 890 /* 891 * If a forced dismount is already in progress, don't 892 * allocate a new clientid and get out now. For the case where 893 * clp != NULL, this is a harmless optimization. 894 */ 895 if (NFSCL_FORCEDISM(mp)) { 896 NFSUNLOCKCLSTATE(); 897 if (newclp != NULL) 898 free(newclp, M_NFSCLCLIENT); 899 return (EBADF); 900 } 901 clp = nmp->nm_clp; 902 if (clp == NULL) { 903 if (newclp == NULL) { 904 NFSUNLOCKCLSTATE(); 905 return (EACCES); 906 } 907 clp = newclp; 908 clp->nfsc_idlen = idlen; 909 LIST_INIT(&clp->nfsc_owner); 910 TAILQ_INIT(&clp->nfsc_deleg); 911 TAILQ_INIT(&clp->nfsc_layout); 912 LIST_INIT(&clp->nfsc_devinfo); 913 for (i = 0; i < NFSCLDELEGHASHSIZE; i++) 914 LIST_INIT(&clp->nfsc_deleghash[i]); 915 for (i = 0; i < NFSCLOPENHASHSIZE; i++) 916 LIST_INIT(&clp->nfsc_openhash[i]); 917 for (i = 0; i < NFSCLLAYOUTHASHSIZE; i++) 918 LIST_INIT(&clp->nfsc_layouthash[i]); 919 clp->nfsc_flags = NFSCLFLAGS_INITED; 920 clp->nfsc_clientidrev = 1; 921 clp->nfsc_cbident = nfscl_nextcbident(); 922 nfscl_fillclid(nmp->nm_clval, uuid, clp->nfsc_id, 923 clp->nfsc_idlen); 924 LIST_INSERT_HEAD(&nfsclhead, clp, nfsc_list); 925 nmp->nm_clp = clp; 926 clp->nfsc_nmp = nmp; 927 } else { 928 if (newclp != NULL) 929 free(newclp, M_NFSCLCLIENT); 930 } 931 while ((clp->nfsc_flags & NFSCLFLAGS_HASCLIENTID) == 0 && !igotlock && 932 !NFSCL_FORCEDISM(mp)) 933 igotlock = nfsv4_lock(&clp->nfsc_lock, 1, NULL, 934 NFSCLSTATEMUTEXPTR, mp); 935 if (igotlock == 0) { 936 /* 937 * Call nfsv4_lock() with "iwantlock == 0" on the firstref so 938 * that it will wait for a pending exclusive lock request. 939 * This gives the exclusive lock request priority over this 940 * shared lock request. 941 * An exclusive lock on nfsc_lock is used mainly for server 942 * crash recoveries and delegation recalls. 943 */ 944 if (firstref) 945 nfsv4_lock(&clp->nfsc_lock, 0, NULL, NFSCLSTATEMUTEXPTR, 946 mp); 947 nfsv4_getref(&clp->nfsc_lock, NULL, NFSCLSTATEMUTEXPTR, mp); 948 } 949 if (igotlock == 0 && NFSCL_FORCEDISM(mp)) { 950 /* 951 * Both nfsv4_lock() and nfsv4_getref() know to check 952 * for NFSCL_FORCEDISM() and return without sleeping to 953 * wait for the exclusive lock to be released, since it 954 * might be held by nfscl_umount() and we need to get out 955 * now for that case and not wait until nfscl_umount() 956 * releases it. 957 */ 958 NFSUNLOCKCLSTATE(); 959 return (EBADF); 960 } 961 NFSUNLOCKCLSTATE(); 962 963 /* 964 * If it needs a clientid, do the setclientid now. 965 */ 966 if ((clp->nfsc_flags & NFSCLFLAGS_HASCLIENTID) == 0) { 967 if (!igotlock) 968 panic("nfscl_clget"); 969 if (p == NULL || cred == NULL) { 970 NFSLOCKCLSTATE(); 971 nfsv4_unlock(&clp->nfsc_lock, 0); 972 NFSUNLOCKCLSTATE(); 973 return (EACCES); 974 } 975 /* 976 * If RFC3530 Sec. 14.2.33 is taken literally, 977 * NFSERR_CLIDINUSE will be returned persistently for the 978 * case where a new mount of the same file system is using 979 * a different principal. In practice, NFSERR_CLIDINUSE is 980 * only returned when there is outstanding unexpired state 981 * on the clientid. As such, try for twice the lease 982 * interval, if we know what that is. Otherwise, make a 983 * wild ass guess. 984 * The case of returning NFSERR_STALECLIENTID is far less 985 * likely, but might occur if there is a significant delay 986 * between doing the SetClientID and SetClientIDConfirm Ops, 987 * such that the server throws away the clientid before 988 * receiving the SetClientIDConfirm. 989 */ 990 if (clp->nfsc_renew > 0) 991 clidinusedelay = NFSCL_LEASE(clp->nfsc_renew) * 2; 992 else 993 clidinusedelay = 120; 994 trystalecnt = 3; 995 do { 996 error = nfsrpc_setclient(nmp, clp, 0, NULL, cred, p); 997 if (error == NFSERR_STALECLIENTID || 998 error == NFSERR_STALEDONTRECOVER || 999 error == NFSERR_BADSESSION || 1000 error == NFSERR_CLIDINUSE) { 1001 (void) nfs_catnap(PZERO, error, "nfs_setcl"); 1002 } else if (error == NFSERR_MINORVERMISMATCH && 1003 tryminvers) { 1004 if (nmp->nm_minorvers > 0) 1005 nmp->nm_minorvers--; 1006 else 1007 tryminvers = false; 1008 } 1009 } while (((error == NFSERR_STALECLIENTID || 1010 error == NFSERR_BADSESSION || 1011 error == NFSERR_STALEDONTRECOVER) && --trystalecnt > 0) || 1012 (error == NFSERR_CLIDINUSE && --clidinusedelay > 0) || 1013 (error == NFSERR_MINORVERMISMATCH && tryminvers)); 1014 if (error) { 1015 NFSLOCKCLSTATE(); 1016 nfsv4_unlock(&clp->nfsc_lock, 0); 1017 NFSUNLOCKCLSTATE(); 1018 return (error); 1019 } 1020 clp->nfsc_flags |= NFSCLFLAGS_HASCLIENTID; 1021 } 1022 if (igotlock) { 1023 NFSLOCKCLSTATE(); 1024 nfsv4_unlock(&clp->nfsc_lock, 1); 1025 NFSUNLOCKCLSTATE(); 1026 } 1027 1028 *clpp = clp; 1029 return (0); 1030 } 1031 1032 /* 1033 * Get a reference to a clientid and return it, if valid. 1034 */ 1035 struct nfsclclient * 1036 nfscl_findcl(struct nfsmount *nmp) 1037 { 1038 struct nfsclclient *clp; 1039 1040 clp = nmp->nm_clp; 1041 if (clp == NULL || !(clp->nfsc_flags & NFSCLFLAGS_HASCLIENTID)) 1042 return (NULL); 1043 return (clp); 1044 } 1045 1046 /* 1047 * Release the clientid structure. It may be locked or reference counted. 1048 */ 1049 static void 1050 nfscl_clrelease(struct nfsclclient *clp) 1051 { 1052 1053 if (clp->nfsc_lock.nfslock_lock & NFSV4LOCK_LOCK) 1054 nfsv4_unlock(&clp->nfsc_lock, 0); 1055 else 1056 nfsv4_relref(&clp->nfsc_lock); 1057 } 1058 1059 /* 1060 * External call for nfscl_clrelease. 1061 */ 1062 void 1063 nfscl_clientrelease(struct nfsclclient *clp) 1064 { 1065 1066 NFSLOCKCLSTATE(); 1067 if (clp->nfsc_lock.nfslock_lock & NFSV4LOCK_LOCK) 1068 nfsv4_unlock(&clp->nfsc_lock, 0); 1069 else 1070 nfsv4_relref(&clp->nfsc_lock); 1071 NFSUNLOCKCLSTATE(); 1072 } 1073 1074 /* 1075 * Called when wanting to lock a byte region. 1076 */ 1077 int 1078 nfscl_getbytelock(vnode_t vp, u_int64_t off, u_int64_t len, 1079 short type, struct ucred *cred, NFSPROC_T *p, struct nfsclclient *rclp, 1080 int recovery, void *id, int flags, u_int8_t *rownp, u_int8_t *ropenownp, 1081 struct nfscllockowner **lpp, int *newonep, int *donelocallyp) 1082 { 1083 struct nfscllockowner *lp; 1084 struct nfsclopen *op; 1085 struct nfsclclient *clp; 1086 struct nfscllockowner *nlp; 1087 struct nfscllock *nlop, *otherlop; 1088 struct nfscldeleg *dp = NULL, *ldp = NULL; 1089 struct nfscllockownerhead *lhp = NULL; 1090 struct nfsnode *np; 1091 u_int8_t own[NFSV4CL_LOCKNAMELEN], *ownp, openown[NFSV4CL_LOCKNAMELEN]; 1092 u_int8_t *openownp; 1093 int error = 0, ret, donelocally = 0; 1094 u_int32_t mode; 1095 1096 /* For Lock Ops, the open mode doesn't matter, so use 0 to match any. */ 1097 mode = 0; 1098 np = VTONFS(vp); 1099 *lpp = NULL; 1100 lp = NULL; 1101 *newonep = 0; 1102 *donelocallyp = 0; 1103 1104 /* 1105 * Might need these, so MALLOC them now, to 1106 * avoid a tsleep() in MALLOC later. 1107 */ 1108 nlp = malloc( 1109 sizeof (struct nfscllockowner), M_NFSCLLOCKOWNER, M_WAITOK); 1110 otherlop = malloc( 1111 sizeof (struct nfscllock), M_NFSCLLOCK, M_WAITOK); 1112 nlop = malloc( 1113 sizeof (struct nfscllock), M_NFSCLLOCK, M_WAITOK); 1114 nlop->nfslo_type = type; 1115 nlop->nfslo_first = off; 1116 if (len == NFS64BITSSET) { 1117 nlop->nfslo_end = NFS64BITSSET; 1118 } else { 1119 nlop->nfslo_end = off + len; 1120 if (nlop->nfslo_end <= nlop->nfslo_first) 1121 error = NFSERR_INVAL; 1122 } 1123 1124 if (!error) { 1125 if (recovery) 1126 clp = rclp; 1127 else 1128 error = nfscl_getcl(vp->v_mount, cred, p, false, true, 1129 &clp); 1130 } 1131 if (error) { 1132 free(nlp, M_NFSCLLOCKOWNER); 1133 free(otherlop, M_NFSCLLOCK); 1134 free(nlop, M_NFSCLLOCK); 1135 return (error); 1136 } 1137 1138 op = NULL; 1139 if (recovery) { 1140 ownp = rownp; 1141 openownp = ropenownp; 1142 } else { 1143 nfscl_filllockowner(id, own, flags); 1144 ownp = own; 1145 if (NFSHASONEOPENOWN(VFSTONFS(vp->v_mount))) 1146 nfscl_filllockowner(NULL, openown, F_POSIX); 1147 else 1148 nfscl_filllockowner(p->td_proc, openown, F_POSIX); 1149 openownp = openown; 1150 } 1151 if (!recovery) { 1152 NFSLOCKCLSTATE(); 1153 /* 1154 * First, search for a delegation. If one exists for this file, 1155 * the lock can be done locally against it, so long as there 1156 * isn't a local lock conflict. 1157 */ 1158 ldp = dp = nfscl_finddeleg(clp, np->n_fhp->nfh_fh, 1159 np->n_fhp->nfh_len); 1160 /* Just sanity check for correct type of delegation */ 1161 if (dp != NULL && ((dp->nfsdl_flags & 1162 (NFSCLDL_RECALL | NFSCLDL_DELEGRET)) != 0 || 1163 (type == F_WRLCK && 1164 (dp->nfsdl_flags & NFSCLDL_WRITE) == 0))) 1165 dp = NULL; 1166 } 1167 if (dp != NULL) { 1168 /* Now, find an open and maybe a lockowner. */ 1169 ret = nfscl_getopen(&dp->nfsdl_owner, NULL, np->n_fhp->nfh_fh, 1170 np->n_fhp->nfh_len, openownp, ownp, mode, NULL, &op); 1171 if (ret) 1172 ret = nfscl_getopen(NULL, clp->nfsc_openhash, 1173 np->n_fhp->nfh_fh, np->n_fhp->nfh_len, openownp, 1174 ownp, mode, NULL, &op); 1175 if (!ret) { 1176 lhp = &dp->nfsdl_lock; 1177 TAILQ_REMOVE(&clp->nfsc_deleg, dp, nfsdl_list); 1178 TAILQ_INSERT_HEAD(&clp->nfsc_deleg, dp, nfsdl_list); 1179 dp->nfsdl_timestamp = NFSD_MONOSEC + 120; 1180 donelocally = 1; 1181 } else { 1182 dp = NULL; 1183 } 1184 } 1185 if (!donelocally) { 1186 /* 1187 * Get the related Open and maybe lockowner. 1188 */ 1189 error = nfscl_getopen(NULL, clp->nfsc_openhash, 1190 np->n_fhp->nfh_fh, np->n_fhp->nfh_len, openownp, 1191 ownp, mode, &lp, &op); 1192 if (!error) 1193 lhp = &op->nfso_lock; 1194 } 1195 if (!error && !recovery) 1196 error = nfscl_localconflict(clp, np->n_fhp->nfh_fh, 1197 np->n_fhp->nfh_len, nlop, ownp, ldp, NULL); 1198 if (error) { 1199 if (!recovery) { 1200 nfscl_clrelease(clp); 1201 NFSUNLOCKCLSTATE(); 1202 } 1203 free(nlp, M_NFSCLLOCKOWNER); 1204 free(otherlop, M_NFSCLLOCK); 1205 free(nlop, M_NFSCLLOCK); 1206 return (error); 1207 } 1208 1209 /* 1210 * Ok, see if a lockowner exists and create one, as required. 1211 */ 1212 if (lp == NULL) 1213 LIST_FOREACH(lp, lhp, nfsl_list) { 1214 if (!NFSBCMP(lp->nfsl_owner, ownp, NFSV4CL_LOCKNAMELEN)) 1215 break; 1216 } 1217 if (lp == NULL) { 1218 NFSBCOPY(ownp, nlp->nfsl_owner, NFSV4CL_LOCKNAMELEN); 1219 if (recovery) 1220 NFSBCOPY(ropenownp, nlp->nfsl_openowner, 1221 NFSV4CL_LOCKNAMELEN); 1222 else 1223 NFSBCOPY(op->nfso_own->nfsow_owner, nlp->nfsl_openowner, 1224 NFSV4CL_LOCKNAMELEN); 1225 nlp->nfsl_seqid = 0; 1226 nlp->nfsl_lockflags = flags; 1227 nlp->nfsl_inprog = NULL; 1228 nfscl_lockinit(&nlp->nfsl_rwlock); 1229 LIST_INIT(&nlp->nfsl_lock); 1230 if (donelocally) { 1231 nlp->nfsl_open = NULL; 1232 nfsstatsv1.cllocallockowners++; 1233 } else { 1234 nlp->nfsl_open = op; 1235 nfsstatsv1.cllockowners++; 1236 } 1237 LIST_INSERT_HEAD(lhp, nlp, nfsl_list); 1238 lp = nlp; 1239 nlp = NULL; 1240 *newonep = 1; 1241 } 1242 1243 /* 1244 * Now, update the byte ranges for locks. 1245 */ 1246 ret = nfscl_updatelock(lp, &nlop, &otherlop, donelocally); 1247 if (!ret) 1248 donelocally = 1; 1249 if (donelocally) { 1250 *donelocallyp = 1; 1251 if (!recovery) 1252 nfscl_clrelease(clp); 1253 } else { 1254 /* 1255 * Serial modifications on the lock owner for multiple threads 1256 * for the same process using a read/write lock. 1257 */ 1258 if (!recovery) 1259 nfscl_lockexcl(&lp->nfsl_rwlock, NFSCLSTATEMUTEXPTR); 1260 } 1261 if (!recovery) 1262 NFSUNLOCKCLSTATE(); 1263 1264 if (nlp) 1265 free(nlp, M_NFSCLLOCKOWNER); 1266 if (nlop) 1267 free(nlop, M_NFSCLLOCK); 1268 if (otherlop) 1269 free(otherlop, M_NFSCLLOCK); 1270 1271 *lpp = lp; 1272 return (0); 1273 } 1274 1275 /* 1276 * Called to unlock a byte range, for LockU. 1277 */ 1278 int 1279 nfscl_relbytelock(vnode_t vp, u_int64_t off, u_int64_t len, 1280 __unused struct ucred *cred, NFSPROC_T *p, int callcnt, 1281 struct nfsclclient *clp, void *id, int flags, 1282 struct nfscllockowner **lpp, int *dorpcp) 1283 { 1284 struct nfscllockowner *lp; 1285 struct nfsclopen *op; 1286 struct nfscllock *nlop, *other_lop = NULL; 1287 struct nfscldeleg *dp; 1288 struct nfsnode *np; 1289 u_int8_t own[NFSV4CL_LOCKNAMELEN]; 1290 int ret = 0, fnd; 1291 1292 np = VTONFS(vp); 1293 *lpp = NULL; 1294 *dorpcp = 0; 1295 1296 /* 1297 * Might need these, so MALLOC them now, to 1298 * avoid a tsleep() in MALLOC later. 1299 */ 1300 nlop = malloc( 1301 sizeof (struct nfscllock), M_NFSCLLOCK, M_WAITOK); 1302 nlop->nfslo_type = F_UNLCK; 1303 nlop->nfslo_first = off; 1304 if (len == NFS64BITSSET) { 1305 nlop->nfslo_end = NFS64BITSSET; 1306 } else { 1307 nlop->nfslo_end = off + len; 1308 if (nlop->nfslo_end <= nlop->nfslo_first) { 1309 free(nlop, M_NFSCLLOCK); 1310 return (NFSERR_INVAL); 1311 } 1312 } 1313 if (callcnt == 0) { 1314 other_lop = malloc( 1315 sizeof (struct nfscllock), M_NFSCLLOCK, M_WAITOK); 1316 *other_lop = *nlop; 1317 } 1318 nfscl_filllockowner(id, own, flags); 1319 dp = NULL; 1320 NFSLOCKCLSTATE(); 1321 if (callcnt == 0) 1322 dp = nfscl_finddeleg(clp, np->n_fhp->nfh_fh, 1323 np->n_fhp->nfh_len); 1324 1325 /* 1326 * First, unlock any local regions on a delegation. 1327 */ 1328 if (dp != NULL) { 1329 /* Look for this lockowner. */ 1330 LIST_FOREACH(lp, &dp->nfsdl_lock, nfsl_list) { 1331 if (!NFSBCMP(lp->nfsl_owner, own, 1332 NFSV4CL_LOCKNAMELEN)) 1333 break; 1334 } 1335 if (lp != NULL) 1336 /* Use other_lop, so nlop is still available */ 1337 (void)nfscl_updatelock(lp, &other_lop, NULL, 1); 1338 } 1339 1340 /* 1341 * Now, find a matching open/lockowner that hasn't already been done, 1342 * as marked by nfsl_inprog. 1343 */ 1344 lp = NULL; 1345 fnd = 0; 1346 LIST_FOREACH(op, NFSCLOPENHASH(clp, np->n_fhp->nfh_fh, 1347 np->n_fhp->nfh_len), nfso_hash) { 1348 if (op->nfso_fhlen == np->n_fhp->nfh_len && 1349 !NFSBCMP(op->nfso_fh, np->n_fhp->nfh_fh, op->nfso_fhlen)) { 1350 LIST_FOREACH(lp, &op->nfso_lock, nfsl_list) { 1351 if (lp->nfsl_inprog == NULL && 1352 !NFSBCMP(lp->nfsl_owner, own, 1353 NFSV4CL_LOCKNAMELEN)) { 1354 fnd = 1; 1355 break; 1356 } 1357 } 1358 } 1359 if (fnd) 1360 break; 1361 } 1362 1363 if (lp != NULL) { 1364 ret = nfscl_updatelock(lp, &nlop, NULL, 0); 1365 if (ret) 1366 *dorpcp = 1; 1367 /* 1368 * Serial modifications on the lock owner for multiple 1369 * threads for the same process using a read/write lock. 1370 */ 1371 lp->nfsl_inprog = p; 1372 nfscl_lockexcl(&lp->nfsl_rwlock, NFSCLSTATEMUTEXPTR); 1373 *lpp = lp; 1374 } 1375 NFSUNLOCKCLSTATE(); 1376 if (nlop) 1377 free(nlop, M_NFSCLLOCK); 1378 if (other_lop) 1379 free(other_lop, M_NFSCLLOCK); 1380 return (0); 1381 } 1382 1383 /* 1384 * Release all lockowners marked in progess for this process and file. 1385 */ 1386 void 1387 nfscl_releasealllocks(struct nfsclclient *clp, vnode_t vp, NFSPROC_T *p, 1388 void *id, int flags) 1389 { 1390 struct nfsclopen *op; 1391 struct nfscllockowner *lp; 1392 struct nfsnode *np; 1393 u_int8_t own[NFSV4CL_LOCKNAMELEN]; 1394 1395 np = VTONFS(vp); 1396 nfscl_filllockowner(id, own, flags); 1397 NFSLOCKCLSTATE(); 1398 LIST_FOREACH(op, NFSCLOPENHASH(clp, np->n_fhp->nfh_fh, 1399 np->n_fhp->nfh_len), nfso_hash) { 1400 if (op->nfso_fhlen == np->n_fhp->nfh_len && 1401 !NFSBCMP(op->nfso_fh, np->n_fhp->nfh_fh, op->nfso_fhlen)) { 1402 LIST_FOREACH(lp, &op->nfso_lock, nfsl_list) { 1403 if (lp->nfsl_inprog == p && 1404 !NFSBCMP(lp->nfsl_owner, own, 1405 NFSV4CL_LOCKNAMELEN)) { 1406 lp->nfsl_inprog = NULL; 1407 nfscl_lockunlock(&lp->nfsl_rwlock); 1408 } 1409 } 1410 } 1411 } 1412 nfscl_clrelease(clp); 1413 NFSUNLOCKCLSTATE(); 1414 } 1415 1416 /* 1417 * Called to find out if any bytes within the byte range specified are 1418 * write locked by the calling process. Used to determine if flushing 1419 * is required before a LockU. 1420 * If in doubt, return 1, so the flush will occur. 1421 */ 1422 int 1423 nfscl_checkwritelocked(vnode_t vp, struct flock *fl, 1424 struct ucred *cred, NFSPROC_T *p, void *id, int flags) 1425 { 1426 struct nfscllockowner *lp; 1427 struct nfsclopen *op; 1428 struct nfsclclient *clp; 1429 struct nfscllock *lop; 1430 struct nfscldeleg *dp; 1431 struct nfsnode *np; 1432 u_int64_t off, end; 1433 u_int8_t own[NFSV4CL_LOCKNAMELEN]; 1434 int error = 0; 1435 1436 np = VTONFS(vp); 1437 switch (fl->l_whence) { 1438 case SEEK_SET: 1439 case SEEK_CUR: 1440 /* 1441 * Caller is responsible for adding any necessary offset 1442 * when SEEK_CUR is used. 1443 */ 1444 off = fl->l_start; 1445 break; 1446 case SEEK_END: 1447 off = np->n_size + fl->l_start; 1448 break; 1449 default: 1450 return (1); 1451 } 1452 if (fl->l_len != 0) { 1453 end = off + fl->l_len; 1454 if (end < off) 1455 return (1); 1456 } else { 1457 end = NFS64BITSSET; 1458 } 1459 1460 error = nfscl_getcl(vp->v_mount, cred, p, false, true, &clp); 1461 if (error) 1462 return (1); 1463 nfscl_filllockowner(id, own, flags); 1464 NFSLOCKCLSTATE(); 1465 1466 /* 1467 * First check the delegation locks. 1468 */ 1469 dp = nfscl_finddeleg(clp, np->n_fhp->nfh_fh, np->n_fhp->nfh_len); 1470 if (dp != NULL) { 1471 LIST_FOREACH(lp, &dp->nfsdl_lock, nfsl_list) { 1472 if (!NFSBCMP(lp->nfsl_owner, own, 1473 NFSV4CL_LOCKNAMELEN)) 1474 break; 1475 } 1476 if (lp != NULL) { 1477 LIST_FOREACH(lop, &lp->nfsl_lock, nfslo_list) { 1478 if (lop->nfslo_first >= end) 1479 break; 1480 if (lop->nfslo_end <= off) 1481 continue; 1482 if (lop->nfslo_type == F_WRLCK) { 1483 nfscl_clrelease(clp); 1484 NFSUNLOCKCLSTATE(); 1485 return (1); 1486 } 1487 } 1488 } 1489 } 1490 1491 /* 1492 * Now, check state against the server. 1493 */ 1494 LIST_FOREACH(op, NFSCLOPENHASH(clp, np->n_fhp->nfh_fh, 1495 np->n_fhp->nfh_len), nfso_hash) { 1496 if (op->nfso_fhlen == np->n_fhp->nfh_len && 1497 !NFSBCMP(op->nfso_fh, np->n_fhp->nfh_fh, op->nfso_fhlen)) { 1498 LIST_FOREACH(lp, &op->nfso_lock, nfsl_list) { 1499 if (!NFSBCMP(lp->nfsl_owner, own, 1500 NFSV4CL_LOCKNAMELEN)) 1501 break; 1502 } 1503 if (lp != NULL) { 1504 LIST_FOREACH(lop, &lp->nfsl_lock, nfslo_list) { 1505 if (lop->nfslo_first >= end) 1506 break; 1507 if (lop->nfslo_end <= off) 1508 continue; 1509 if (lop->nfslo_type == F_WRLCK) { 1510 nfscl_clrelease(clp); 1511 NFSUNLOCKCLSTATE(); 1512 return (1); 1513 } 1514 } 1515 } 1516 } 1517 } 1518 nfscl_clrelease(clp); 1519 NFSUNLOCKCLSTATE(); 1520 return (0); 1521 } 1522 1523 /* 1524 * Release a byte range lock owner structure. 1525 */ 1526 void 1527 nfscl_lockrelease(struct nfscllockowner *lp, int error, int candelete) 1528 { 1529 struct nfsclclient *clp; 1530 1531 if (lp == NULL) 1532 return; 1533 NFSLOCKCLSTATE(); 1534 clp = lp->nfsl_open->nfso_own->nfsow_clp; 1535 if (error != 0 && candelete && 1536 (lp->nfsl_rwlock.nfslock_lock & NFSV4LOCK_WANTED) == 0) 1537 nfscl_freelockowner(lp, 0); 1538 else 1539 nfscl_lockunlock(&lp->nfsl_rwlock); 1540 nfscl_clrelease(clp); 1541 NFSUNLOCKCLSTATE(); 1542 } 1543 1544 /* 1545 * Unlink the open structure. 1546 */ 1547 static void 1548 nfscl_unlinkopen(struct nfsclopen *op) 1549 { 1550 1551 LIST_REMOVE(op, nfso_list); 1552 if (op->nfso_hash.le_prev != NULL) 1553 LIST_REMOVE(op, nfso_hash); 1554 } 1555 1556 /* 1557 * Free up an open structure and any associated byte range lock structures. 1558 */ 1559 void 1560 nfscl_freeopen(struct nfsclopen *op, int local, bool unlink) 1561 { 1562 1563 if (unlink) 1564 nfscl_unlinkopen(op); 1565 nfscl_freealllocks(&op->nfso_lock, local); 1566 free(op, M_NFSCLOPEN); 1567 if (local) 1568 nfsstatsv1.cllocalopens--; 1569 else 1570 nfsstatsv1.clopens--; 1571 } 1572 1573 /* 1574 * Free up all lock owners and associated locks. 1575 */ 1576 static void 1577 nfscl_freealllocks(struct nfscllockownerhead *lhp, int local) 1578 { 1579 struct nfscllockowner *lp, *nlp; 1580 1581 LIST_FOREACH_SAFE(lp, lhp, nfsl_list, nlp) { 1582 if ((lp->nfsl_rwlock.nfslock_lock & NFSV4LOCK_WANTED)) 1583 panic("nfscllckw"); 1584 nfscl_freelockowner(lp, local); 1585 } 1586 } 1587 1588 /* 1589 * Called for an Open when NFSERR_EXPIRED is received from the server. 1590 * If there are no byte range locks nor a Share Deny lost, try to do a 1591 * fresh Open. Otherwise, free the open. 1592 */ 1593 static int 1594 nfscl_expireopen(struct nfsclclient *clp, struct nfsclopen *op, 1595 struct nfsmount *nmp, struct ucred *cred, NFSPROC_T *p) 1596 { 1597 struct nfscllockowner *lp; 1598 struct nfscldeleg *dp; 1599 int mustdelete = 0, error; 1600 1601 /* 1602 * Look for any byte range lock(s). 1603 */ 1604 LIST_FOREACH(lp, &op->nfso_lock, nfsl_list) { 1605 if (!LIST_EMPTY(&lp->nfsl_lock)) { 1606 mustdelete = 1; 1607 break; 1608 } 1609 } 1610 1611 /* 1612 * If no byte range lock(s) nor a Share deny, try to re-open. 1613 */ 1614 if (!mustdelete && (op->nfso_mode & NFSLCK_DENYBITS) == 0) { 1615 newnfs_copycred(&op->nfso_cred, cred); 1616 dp = NULL; 1617 error = nfsrpc_reopen(nmp, op->nfso_fh, 1618 op->nfso_fhlen, op->nfso_mode, op, &dp, cred, p); 1619 if (error) { 1620 mustdelete = 1; 1621 if (dp != NULL) { 1622 free(dp, M_NFSCLDELEG); 1623 dp = NULL; 1624 } 1625 } 1626 if (dp != NULL) 1627 nfscl_deleg(nmp->nm_mountp, clp, op->nfso_fh, 1628 op->nfso_fhlen, cred, p, &dp); 1629 } 1630 1631 /* 1632 * If a byte range lock or Share deny or couldn't re-open, free it. 1633 */ 1634 if (mustdelete) 1635 nfscl_freeopen(op, 0, true); 1636 return (mustdelete); 1637 } 1638 1639 /* 1640 * Free up an open owner structure. 1641 */ 1642 static void 1643 nfscl_freeopenowner(struct nfsclowner *owp, int local) 1644 { 1645 1646 LIST_REMOVE(owp, nfsow_list); 1647 free(owp, M_NFSCLOWNER); 1648 if (local) 1649 nfsstatsv1.cllocalopenowners--; 1650 else 1651 nfsstatsv1.clopenowners--; 1652 } 1653 1654 /* 1655 * Free up a byte range lock owner structure. 1656 */ 1657 void 1658 nfscl_freelockowner(struct nfscllockowner *lp, int local) 1659 { 1660 struct nfscllock *lop, *nlop; 1661 1662 LIST_REMOVE(lp, nfsl_list); 1663 LIST_FOREACH_SAFE(lop, &lp->nfsl_lock, nfslo_list, nlop) { 1664 nfscl_freelock(lop, local); 1665 } 1666 free(lp, M_NFSCLLOCKOWNER); 1667 if (local) 1668 nfsstatsv1.cllocallockowners--; 1669 else 1670 nfsstatsv1.cllockowners--; 1671 } 1672 1673 /* 1674 * Free up a byte range lock structure. 1675 */ 1676 void 1677 nfscl_freelock(struct nfscllock *lop, int local) 1678 { 1679 1680 LIST_REMOVE(lop, nfslo_list); 1681 free(lop, M_NFSCLLOCK); 1682 if (local) 1683 nfsstatsv1.cllocallocks--; 1684 else 1685 nfsstatsv1.cllocks--; 1686 } 1687 1688 /* 1689 * Clean out the state related to a delegation. 1690 */ 1691 static void 1692 nfscl_cleandeleg(struct nfscldeleg *dp) 1693 { 1694 struct nfsclowner *owp, *nowp; 1695 struct nfsclopen *op; 1696 1697 LIST_FOREACH_SAFE(owp, &dp->nfsdl_owner, nfsow_list, nowp) { 1698 op = LIST_FIRST(&owp->nfsow_open); 1699 if (op != NULL) { 1700 if (LIST_NEXT(op, nfso_list) != NULL) 1701 panic("nfscleandel"); 1702 nfscl_freeopen(op, 1, true); 1703 } 1704 nfscl_freeopenowner(owp, 1); 1705 } 1706 nfscl_freealllocks(&dp->nfsdl_lock, 1); 1707 } 1708 1709 /* 1710 * Free a delegation. 1711 */ 1712 static void 1713 nfscl_freedeleg(struct nfscldeleghead *hdp, struct nfscldeleg *dp, bool freeit) 1714 { 1715 1716 TAILQ_REMOVE(hdp, dp, nfsdl_list); 1717 LIST_REMOVE(dp, nfsdl_hash); 1718 if (freeit) 1719 free(dp, M_NFSCLDELEG); 1720 nfsstatsv1.cldelegates--; 1721 nfscl_delegcnt--; 1722 } 1723 1724 /* 1725 * Free up all state related to this client structure. 1726 */ 1727 static void 1728 nfscl_cleanclient(struct nfsclclient *clp) 1729 { 1730 struct nfsclowner *owp, *nowp; 1731 struct nfsclopen *op, *nop; 1732 struct nfscllayout *lyp, *nlyp; 1733 struct nfscldevinfo *dip, *ndip; 1734 1735 TAILQ_FOREACH_SAFE(lyp, &clp->nfsc_layout, nfsly_list, nlyp) 1736 nfscl_freelayout(lyp); 1737 1738 LIST_FOREACH_SAFE(dip, &clp->nfsc_devinfo, nfsdi_list, ndip) 1739 nfscl_freedevinfo(dip); 1740 1741 /* Now, all the OpenOwners, etc. */ 1742 LIST_FOREACH_SAFE(owp, &clp->nfsc_owner, nfsow_list, nowp) { 1743 LIST_FOREACH_SAFE(op, &owp->nfsow_open, nfso_list, nop) { 1744 nfscl_freeopen(op, 0, true); 1745 } 1746 nfscl_freeopenowner(owp, 0); 1747 } 1748 } 1749 1750 /* 1751 * Called when an NFSERR_EXPIRED is received from the server. 1752 */ 1753 static void 1754 nfscl_expireclient(struct nfsclclient *clp, struct nfsmount *nmp, 1755 struct ucred *cred, NFSPROC_T *p) 1756 { 1757 struct nfsclowner *owp, *nowp, *towp; 1758 struct nfsclopen *op, *nop, *top; 1759 struct nfscldeleg *dp, *ndp; 1760 int ret, printed = 0; 1761 1762 /* 1763 * First, merge locally issued Opens into the list for the server. 1764 */ 1765 dp = TAILQ_FIRST(&clp->nfsc_deleg); 1766 while (dp != NULL) { 1767 ndp = TAILQ_NEXT(dp, nfsdl_list); 1768 owp = LIST_FIRST(&dp->nfsdl_owner); 1769 while (owp != NULL) { 1770 nowp = LIST_NEXT(owp, nfsow_list); 1771 op = LIST_FIRST(&owp->nfsow_open); 1772 if (op != NULL) { 1773 if (LIST_NEXT(op, nfso_list) != NULL) 1774 panic("nfsclexp"); 1775 LIST_FOREACH(towp, &clp->nfsc_owner, nfsow_list) { 1776 if (!NFSBCMP(towp->nfsow_owner, owp->nfsow_owner, 1777 NFSV4CL_LOCKNAMELEN)) 1778 break; 1779 } 1780 if (towp != NULL) { 1781 /* Merge opens in */ 1782 LIST_FOREACH(top, &towp->nfsow_open, nfso_list) { 1783 if (top->nfso_fhlen == op->nfso_fhlen && 1784 !NFSBCMP(top->nfso_fh, op->nfso_fh, 1785 op->nfso_fhlen)) { 1786 top->nfso_mode |= op->nfso_mode; 1787 top->nfso_opencnt += op->nfso_opencnt; 1788 break; 1789 } 1790 } 1791 if (top == NULL) { 1792 /* Just add the open to the owner list */ 1793 LIST_REMOVE(op, nfso_list); 1794 op->nfso_own = towp; 1795 LIST_INSERT_HEAD(&towp->nfsow_open, op, nfso_list); 1796 LIST_INSERT_HEAD(NFSCLOPENHASH(clp, op->nfso_fh, 1797 op->nfso_fhlen), op, nfso_hash); 1798 nfsstatsv1.cllocalopens--; 1799 nfsstatsv1.clopens++; 1800 } 1801 } else { 1802 /* Just add the openowner to the client list */ 1803 LIST_REMOVE(owp, nfsow_list); 1804 owp->nfsow_clp = clp; 1805 LIST_INSERT_HEAD(&clp->nfsc_owner, owp, nfsow_list); 1806 LIST_INSERT_HEAD(NFSCLOPENHASH(clp, op->nfso_fh, 1807 op->nfso_fhlen), op, nfso_hash); 1808 nfsstatsv1.cllocalopenowners--; 1809 nfsstatsv1.clopenowners++; 1810 nfsstatsv1.cllocalopens--; 1811 nfsstatsv1.clopens++; 1812 } 1813 } 1814 owp = nowp; 1815 } 1816 if (!printed && !LIST_EMPTY(&dp->nfsdl_lock)) { 1817 printed = 1; 1818 printf("nfsv4 expired locks lost\n"); 1819 } 1820 nfscl_cleandeleg(dp); 1821 nfscl_freedeleg(&clp->nfsc_deleg, dp, true); 1822 dp = ndp; 1823 } 1824 if (!TAILQ_EMPTY(&clp->nfsc_deleg)) 1825 panic("nfsclexp"); 1826 1827 /* 1828 * Now, try and reopen against the server. 1829 */ 1830 LIST_FOREACH_SAFE(owp, &clp->nfsc_owner, nfsow_list, nowp) { 1831 owp->nfsow_seqid = 0; 1832 LIST_FOREACH_SAFE(op, &owp->nfsow_open, nfso_list, nop) { 1833 ret = nfscl_expireopen(clp, op, nmp, cred, p); 1834 if (ret && !printed) { 1835 printed = 1; 1836 printf("nfsv4 expired locks lost\n"); 1837 } 1838 } 1839 if (LIST_EMPTY(&owp->nfsow_open)) 1840 nfscl_freeopenowner(owp, 0); 1841 } 1842 } 1843 1844 /* 1845 * This function must be called after the process represented by "own" has 1846 * exited. Must be called with CLSTATE lock held. 1847 */ 1848 static void 1849 nfscl_cleanup_common(struct nfsclclient *clp, u_int8_t *own) 1850 { 1851 struct nfsclowner *owp, *nowp; 1852 struct nfscllockowner *lp, *nlp; 1853 struct nfscldeleg *dp; 1854 1855 /* First, get rid of local locks on delegations. */ 1856 TAILQ_FOREACH(dp, &clp->nfsc_deleg, nfsdl_list) { 1857 LIST_FOREACH_SAFE(lp, &dp->nfsdl_lock, nfsl_list, nlp) { 1858 if (!NFSBCMP(lp->nfsl_owner, own, NFSV4CL_LOCKNAMELEN)) { 1859 if ((lp->nfsl_rwlock.nfslock_lock & NFSV4LOCK_WANTED)) 1860 panic("nfscllckw"); 1861 nfscl_freelockowner(lp, 1); 1862 } 1863 } 1864 } 1865 owp = LIST_FIRST(&clp->nfsc_owner); 1866 while (owp != NULL) { 1867 nowp = LIST_NEXT(owp, nfsow_list); 1868 if (!NFSBCMP(owp->nfsow_owner, own, 1869 NFSV4CL_LOCKNAMELEN)) { 1870 /* 1871 * If there are children that haven't closed the 1872 * file descriptors yet, the opens will still be 1873 * here. For that case, let the renew thread clear 1874 * out the OpenOwner later. 1875 */ 1876 if (LIST_EMPTY(&owp->nfsow_open)) 1877 nfscl_freeopenowner(owp, 0); 1878 else 1879 owp->nfsow_defunct = 1; 1880 } 1881 owp = nowp; 1882 } 1883 } 1884 1885 /* 1886 * Find open/lock owners for processes that have exited. 1887 */ 1888 static void 1889 nfscl_cleanupkext(struct nfsclclient *clp, struct nfscllockownerfhhead *lhp) 1890 { 1891 struct nfsclowner *owp, *nowp; 1892 struct nfsclopen *op; 1893 struct nfscllockowner *lp, *nlp; 1894 struct nfscldeleg *dp; 1895 1896 /* 1897 * All the pidhash locks must be acquired, since they are sx locks 1898 * and must be acquired before the mutexes. The pid(s) that will 1899 * be used aren't known yet, so all the locks need to be acquired. 1900 * Fortunately, this function is only performed once/sec. 1901 */ 1902 pidhash_slockall(); 1903 NFSLOCKCLSTATE(); 1904 LIST_FOREACH_SAFE(owp, &clp->nfsc_owner, nfsow_list, nowp) { 1905 LIST_FOREACH(op, &owp->nfsow_open, nfso_list) { 1906 LIST_FOREACH_SAFE(lp, &op->nfso_lock, nfsl_list, nlp) { 1907 if (LIST_EMPTY(&lp->nfsl_lock)) 1908 nfscl_emptylockowner(lp, lhp); 1909 } 1910 } 1911 if (nfscl_procdoesntexist(owp->nfsow_owner)) 1912 nfscl_cleanup_common(clp, owp->nfsow_owner); 1913 } 1914 1915 /* 1916 * For the single open_owner case, these lock owners need to be 1917 * checked to see if they still exist separately. 1918 * This is because nfscl_procdoesntexist() never returns true for 1919 * the single open_owner so that the above doesn't ever call 1920 * nfscl_cleanup_common(). 1921 */ 1922 TAILQ_FOREACH(dp, &clp->nfsc_deleg, nfsdl_list) { 1923 LIST_FOREACH_SAFE(lp, &dp->nfsdl_lock, nfsl_list, nlp) { 1924 if (nfscl_procdoesntexist(lp->nfsl_owner)) 1925 nfscl_cleanup_common(clp, lp->nfsl_owner); 1926 } 1927 } 1928 NFSUNLOCKCLSTATE(); 1929 pidhash_sunlockall(); 1930 } 1931 1932 /* 1933 * Take the empty lock owner and move it to the local lhp list if the 1934 * associated process no longer exists. 1935 */ 1936 static void 1937 nfscl_emptylockowner(struct nfscllockowner *lp, 1938 struct nfscllockownerfhhead *lhp) 1939 { 1940 struct nfscllockownerfh *lfhp, *mylfhp; 1941 struct nfscllockowner *nlp; 1942 int fnd_it; 1943 1944 /* If not a Posix lock owner, just return. */ 1945 if ((lp->nfsl_lockflags & F_POSIX) == 0) 1946 return; 1947 1948 fnd_it = 0; 1949 mylfhp = NULL; 1950 /* 1951 * First, search to see if this lock owner is already in the list. 1952 * If it is, then the associated process no longer exists. 1953 */ 1954 SLIST_FOREACH(lfhp, lhp, nfslfh_list) { 1955 if (lfhp->nfslfh_len == lp->nfsl_open->nfso_fhlen && 1956 !NFSBCMP(lfhp->nfslfh_fh, lp->nfsl_open->nfso_fh, 1957 lfhp->nfslfh_len)) 1958 mylfhp = lfhp; 1959 LIST_FOREACH(nlp, &lfhp->nfslfh_lock, nfsl_list) 1960 if (!NFSBCMP(nlp->nfsl_owner, lp->nfsl_owner, 1961 NFSV4CL_LOCKNAMELEN)) 1962 fnd_it = 1; 1963 } 1964 /* If not found, check if process still exists. */ 1965 if (fnd_it == 0 && nfscl_procdoesntexist(lp->nfsl_owner) == 0) 1966 return; 1967 1968 /* Move the lock owner over to the local list. */ 1969 if (mylfhp == NULL) { 1970 mylfhp = malloc(sizeof(struct nfscllockownerfh), M_TEMP, 1971 M_NOWAIT); 1972 if (mylfhp == NULL) 1973 return; 1974 mylfhp->nfslfh_len = lp->nfsl_open->nfso_fhlen; 1975 NFSBCOPY(lp->nfsl_open->nfso_fh, mylfhp->nfslfh_fh, 1976 mylfhp->nfslfh_len); 1977 LIST_INIT(&mylfhp->nfslfh_lock); 1978 SLIST_INSERT_HEAD(lhp, mylfhp, nfslfh_list); 1979 } 1980 LIST_REMOVE(lp, nfsl_list); 1981 LIST_INSERT_HEAD(&mylfhp->nfslfh_lock, lp, nfsl_list); 1982 } 1983 1984 static int fake_global; /* Used to force visibility of MNTK_UNMOUNTF */ 1985 /* 1986 * Called from nfs umount to free up the clientid. 1987 */ 1988 void 1989 nfscl_umount(struct nfsmount *nmp, NFSPROC_T *p, struct nfscldeleghead *dhp) 1990 { 1991 struct nfsclclient *clp; 1992 struct ucred *cred; 1993 int igotlock; 1994 1995 /* 1996 * For the case that matters, this is the thread that set 1997 * MNTK_UNMOUNTF, so it will see it set. The code that follows is 1998 * done to ensure that any thread executing nfscl_getcl() after 1999 * this time, will see MNTK_UNMOUNTF set. nfscl_getcl() uses the 2000 * mutex for NFSLOCKCLSTATE(), so it is "m" for the following 2001 * explanation, courtesy of Alan Cox. 2002 * What follows is a snippet from Alan Cox's email at: 2003 * https://docs.FreeBSD.org/cgi/mid.cgi?BANLkTikR3d65zPHo9==08ZfJ2vmqZucEvw 2004 * 2005 * 1. Set MNTK_UNMOUNTF 2006 * 2. Acquire a standard FreeBSD mutex "m". 2007 * 3. Update some data structures. 2008 * 4. Release mutex "m". 2009 * 2010 * Then, other threads that acquire "m" after step 4 has occurred will 2011 * see MNTK_UNMOUNTF as set. But, other threads that beat thread X to 2012 * step 2 may or may not see MNTK_UNMOUNTF as set. 2013 */ 2014 NFSLOCKCLSTATE(); 2015 if ((nmp->nm_mountp->mnt_kern_flag & MNTK_UNMOUNTF) != 0) { 2016 fake_global++; 2017 NFSUNLOCKCLSTATE(); 2018 NFSLOCKCLSTATE(); 2019 } 2020 2021 clp = nmp->nm_clp; 2022 if (clp != NULL) { 2023 if ((clp->nfsc_flags & NFSCLFLAGS_INITED) == 0) 2024 panic("nfscl umount"); 2025 2026 /* 2027 * First, handshake with the nfscl renew thread, to terminate 2028 * it. 2029 */ 2030 clp->nfsc_flags |= NFSCLFLAGS_UMOUNT; 2031 while (clp->nfsc_flags & NFSCLFLAGS_HASTHREAD) 2032 (void)mtx_sleep(clp, NFSCLSTATEMUTEXPTR, PWAIT, 2033 "nfsclumnt", hz); 2034 2035 /* 2036 * Now, get the exclusive lock on the client state, so 2037 * that no uses of the state are still in progress. 2038 */ 2039 do { 2040 igotlock = nfsv4_lock(&clp->nfsc_lock, 1, NULL, 2041 NFSCLSTATEMUTEXPTR, NULL); 2042 } while (!igotlock); 2043 NFSUNLOCKCLSTATE(); 2044 2045 /* 2046 * Free up all the state. It will expire on the server, but 2047 * maybe we should do a SetClientId/SetClientIdConfirm so 2048 * the server throws it away? 2049 */ 2050 LIST_REMOVE(clp, nfsc_list); 2051 nfscl_delegreturnall(clp, p, dhp); 2052 cred = newnfs_getcred(); 2053 if (NFSHASNFSV4N(nmp)) { 2054 (void)nfsrpc_destroysession(nmp, clp, cred, p); 2055 (void)nfsrpc_destroyclient(nmp, clp, cred, p); 2056 } else 2057 (void)nfsrpc_setclient(nmp, clp, 0, NULL, cred, p); 2058 nfscl_cleanclient(clp); 2059 nmp->nm_clp = NULL; 2060 NFSFREECRED(cred); 2061 free(clp, M_NFSCLCLIENT); 2062 } else 2063 NFSUNLOCKCLSTATE(); 2064 } 2065 2066 /* 2067 * This function is called when a server replies with NFSERR_STALECLIENTID 2068 * NFSERR_STALESTATEID or NFSERR_BADSESSION. It traverses the clientid lists, 2069 * doing Opens and Locks with reclaim. If these fail, it deletes the 2070 * corresponding state. 2071 */ 2072 static void 2073 nfscl_recover(struct nfsclclient *clp, bool *retokp, struct ucred *cred, 2074 NFSPROC_T *p) 2075 { 2076 struct nfsclowner *owp, *nowp; 2077 struct nfsclopen *op, *nop; 2078 struct nfscllockowner *lp, *nlp; 2079 struct nfscllock *lop, *nlop; 2080 struct nfscldeleg *dp, *ndp, *tdp; 2081 struct nfsmount *nmp; 2082 struct ucred *tcred; 2083 struct nfsclopenhead extra_open; 2084 struct nfscldeleghead extra_deleg; 2085 struct nfsreq *rep; 2086 u_int64_t len; 2087 u_int32_t delegtype = NFSV4OPEN_DELEGATEWRITE, mode; 2088 int i, igotlock = 0, error, trycnt, firstlock; 2089 struct nfscllayout *lyp, *nlyp; 2090 bool recovered_one; 2091 2092 /* 2093 * First, lock the client structure, so everyone else will 2094 * block when trying to use state. 2095 */ 2096 NFSLOCKCLSTATE(); 2097 clp->nfsc_flags |= NFSCLFLAGS_RECVRINPROG; 2098 do { 2099 igotlock = nfsv4_lock(&clp->nfsc_lock, 1, NULL, 2100 NFSCLSTATEMUTEXPTR, NULL); 2101 } while (!igotlock); 2102 NFSUNLOCKCLSTATE(); 2103 2104 nmp = clp->nfsc_nmp; 2105 if (nmp == NULL) 2106 panic("nfscl recover"); 2107 2108 /* 2109 * For now, just get rid of all layouts. There may be a need 2110 * to do LayoutCommit Ops with reclaim == true later. 2111 */ 2112 TAILQ_FOREACH_SAFE(lyp, &clp->nfsc_layout, nfsly_list, nlyp) 2113 nfscl_freelayout(lyp); 2114 TAILQ_INIT(&clp->nfsc_layout); 2115 for (i = 0; i < NFSCLLAYOUTHASHSIZE; i++) 2116 LIST_INIT(&clp->nfsc_layouthash[i]); 2117 2118 trycnt = 5; 2119 tcred = NULL; 2120 do { 2121 error = nfsrpc_setclient(nmp, clp, 1, retokp, cred, p); 2122 } while ((error == NFSERR_STALECLIENTID || 2123 error == NFSERR_BADSESSION || 2124 error == NFSERR_STALEDONTRECOVER) && --trycnt > 0); 2125 if (error) { 2126 NFSLOCKCLSTATE(); 2127 clp->nfsc_flags &= ~(NFSCLFLAGS_RECOVER | 2128 NFSCLFLAGS_RECVRINPROG); 2129 wakeup(&clp->nfsc_flags); 2130 nfsv4_unlock(&clp->nfsc_lock, 0); 2131 NFSUNLOCKCLSTATE(); 2132 return; 2133 } 2134 clp->nfsc_flags |= NFSCLFLAGS_HASCLIENTID; 2135 clp->nfsc_flags &= ~NFSCLFLAGS_RECOVER; 2136 2137 /* 2138 * Mark requests already queued on the server, so that they don't 2139 * initiate another recovery cycle. Any requests already in the 2140 * queue that handle state information will have the old stale 2141 * clientid/stateid and will get a NFSERR_STALESTATEID, 2142 * NFSERR_STALECLIENTID or NFSERR_BADSESSION reply from the server. 2143 * This will be translated to NFSERR_STALEDONTRECOVER when 2144 * R_DONTRECOVER is set. 2145 */ 2146 NFSLOCKREQ(); 2147 TAILQ_FOREACH(rep, &nfsd_reqq, r_chain) { 2148 if (rep->r_nmp == nmp) 2149 rep->r_flags |= R_DONTRECOVER; 2150 } 2151 NFSUNLOCKREQ(); 2152 2153 /* 2154 * If nfsrpc_setclient() returns *retokp == true, 2155 * no more recovery is needed. 2156 */ 2157 if (*retokp) 2158 goto out; 2159 2160 /* 2161 * Now, mark all delegations "need reclaim". 2162 */ 2163 TAILQ_FOREACH(dp, &clp->nfsc_deleg, nfsdl_list) 2164 dp->nfsdl_flags |= NFSCLDL_NEEDRECLAIM; 2165 2166 TAILQ_INIT(&extra_deleg); 2167 LIST_INIT(&extra_open); 2168 /* 2169 * Now traverse the state lists, doing Open and Lock Reclaims. 2170 */ 2171 tcred = newnfs_getcred(); 2172 recovered_one = false; 2173 owp = LIST_FIRST(&clp->nfsc_owner); 2174 while (owp != NULL) { 2175 nowp = LIST_NEXT(owp, nfsow_list); 2176 owp->nfsow_seqid = 0; 2177 op = LIST_FIRST(&owp->nfsow_open); 2178 while (op != NULL) { 2179 nop = LIST_NEXT(op, nfso_list); 2180 if (error != NFSERR_NOGRACE && error != NFSERR_BADSESSION) { 2181 /* Search for a delegation to reclaim with the open */ 2182 TAILQ_FOREACH(dp, &clp->nfsc_deleg, nfsdl_list) { 2183 if (!(dp->nfsdl_flags & NFSCLDL_NEEDRECLAIM)) 2184 continue; 2185 if ((dp->nfsdl_flags & NFSCLDL_WRITE)) { 2186 mode = NFSV4OPEN_ACCESSWRITE; 2187 delegtype = NFSV4OPEN_DELEGATEWRITE; 2188 } else { 2189 mode = NFSV4OPEN_ACCESSREAD; 2190 delegtype = NFSV4OPEN_DELEGATEREAD; 2191 } 2192 if ((op->nfso_mode & mode) == mode && 2193 op->nfso_fhlen == dp->nfsdl_fhlen && 2194 !NFSBCMP(op->nfso_fh, dp->nfsdl_fh, op->nfso_fhlen)) 2195 break; 2196 } 2197 ndp = dp; 2198 if (dp == NULL) 2199 delegtype = NFSV4OPEN_DELEGATENONE; 2200 newnfs_copycred(&op->nfso_cred, tcred); 2201 error = nfscl_tryopen(nmp, NULL, op->nfso_fh, 2202 op->nfso_fhlen, op->nfso_fh, op->nfso_fhlen, 2203 op->nfso_mode, op, NULL, 0, &ndp, 1, delegtype, 2204 tcred, p); 2205 if (!error) { 2206 recovered_one = true; 2207 /* Handle any replied delegation */ 2208 if (ndp != NULL && ((ndp->nfsdl_flags & NFSCLDL_WRITE) 2209 || NFSMNT_RDONLY(nmp->nm_mountp))) { 2210 if ((ndp->nfsdl_flags & NFSCLDL_WRITE)) 2211 mode = NFSV4OPEN_ACCESSWRITE; 2212 else 2213 mode = NFSV4OPEN_ACCESSREAD; 2214 TAILQ_FOREACH(dp, &clp->nfsc_deleg, nfsdl_list) { 2215 if (!(dp->nfsdl_flags & NFSCLDL_NEEDRECLAIM)) 2216 continue; 2217 if ((op->nfso_mode & mode) == mode && 2218 op->nfso_fhlen == dp->nfsdl_fhlen && 2219 !NFSBCMP(op->nfso_fh, dp->nfsdl_fh, 2220 op->nfso_fhlen)) { 2221 dp->nfsdl_stateid = ndp->nfsdl_stateid; 2222 dp->nfsdl_sizelimit = ndp->nfsdl_sizelimit; 2223 dp->nfsdl_ace = ndp->nfsdl_ace; 2224 dp->nfsdl_change = ndp->nfsdl_change; 2225 dp->nfsdl_flags &= ~NFSCLDL_NEEDRECLAIM; 2226 if ((ndp->nfsdl_flags & NFSCLDL_RECALL)) 2227 dp->nfsdl_flags |= NFSCLDL_RECALL; 2228 free(ndp, M_NFSCLDELEG); 2229 ndp = NULL; 2230 break; 2231 } 2232 } 2233 } 2234 if (ndp != NULL) 2235 TAILQ_INSERT_HEAD(&extra_deleg, ndp, nfsdl_list); 2236 2237 /* and reclaim all byte range locks */ 2238 lp = LIST_FIRST(&op->nfso_lock); 2239 while (lp != NULL) { 2240 nlp = LIST_NEXT(lp, nfsl_list); 2241 lp->nfsl_seqid = 0; 2242 firstlock = 1; 2243 lop = LIST_FIRST(&lp->nfsl_lock); 2244 while (lop != NULL) { 2245 nlop = LIST_NEXT(lop, nfslo_list); 2246 if (lop->nfslo_end == NFS64BITSSET) 2247 len = NFS64BITSSET; 2248 else 2249 len = lop->nfslo_end - lop->nfslo_first; 2250 error = nfscl_trylock(nmp, NULL, 2251 op->nfso_fh, op->nfso_fhlen, lp, 2252 firstlock, 1, lop->nfslo_first, len, 2253 lop->nfslo_type, tcred, p); 2254 if (error != 0) 2255 nfscl_freelock(lop, 0); 2256 else 2257 firstlock = 0; 2258 lop = nlop; 2259 } 2260 /* If no locks, but a lockowner, just delete it. */ 2261 if (LIST_EMPTY(&lp->nfsl_lock)) 2262 nfscl_freelockowner(lp, 0); 2263 lp = nlp; 2264 } 2265 } else if (error == NFSERR_NOGRACE && !recovered_one && 2266 NFSHASNFSV4N(nmp)) { 2267 /* 2268 * For NFSv4.1/4.2, the NFSERR_EXPIRED case will 2269 * actually end up here, since the client will do 2270 * a recovery for NFSERR_BADSESSION, but will get 2271 * an NFSERR_NOGRACE reply for the first "reclaim" 2272 * attempt. 2273 * So, call nfscl_expireclient() to recover the 2274 * opens as best we can and then do a reclaim 2275 * complete and return. 2276 */ 2277 nfsrpc_reclaimcomplete(nmp, cred, p); 2278 nfscl_expireclient(clp, nmp, tcred, p); 2279 goto out; 2280 } 2281 } 2282 if (error != 0 && error != NFSERR_BADSESSION) 2283 nfscl_freeopen(op, 0, true); 2284 op = nop; 2285 } 2286 owp = nowp; 2287 } 2288 2289 /* 2290 * Now, try and get any delegations not yet reclaimed by cobbling 2291 * to-gether an appropriate open. 2292 */ 2293 nowp = NULL; 2294 dp = TAILQ_FIRST(&clp->nfsc_deleg); 2295 while (dp != NULL) { 2296 ndp = TAILQ_NEXT(dp, nfsdl_list); 2297 if ((dp->nfsdl_flags & NFSCLDL_NEEDRECLAIM)) { 2298 if (nowp == NULL) { 2299 nowp = malloc( 2300 sizeof (struct nfsclowner), M_NFSCLOWNER, M_WAITOK); 2301 /* 2302 * Name must be as long an largest possible 2303 * NFSV4CL_LOCKNAMELEN. 12 for now. 2304 */ 2305 NFSBCOPY("RECLAIMDELEG", nowp->nfsow_owner, 2306 NFSV4CL_LOCKNAMELEN); 2307 LIST_INIT(&nowp->nfsow_open); 2308 nowp->nfsow_clp = clp; 2309 nowp->nfsow_seqid = 0; 2310 nowp->nfsow_defunct = 0; 2311 nfscl_lockinit(&nowp->nfsow_rwlock); 2312 } 2313 nop = NULL; 2314 if (error != NFSERR_NOGRACE && error != NFSERR_BADSESSION) { 2315 nop = malloc(sizeof (struct nfsclopen) + 2316 dp->nfsdl_fhlen - 1, M_NFSCLOPEN, M_WAITOK); 2317 nop->nfso_own = nowp; 2318 if ((dp->nfsdl_flags & NFSCLDL_WRITE)) { 2319 nop->nfso_mode = NFSV4OPEN_ACCESSWRITE; 2320 delegtype = NFSV4OPEN_DELEGATEWRITE; 2321 } else { 2322 nop->nfso_mode = NFSV4OPEN_ACCESSREAD; 2323 delegtype = NFSV4OPEN_DELEGATEREAD; 2324 } 2325 nop->nfso_opencnt = 0; 2326 nop->nfso_posixlock = 1; 2327 nop->nfso_fhlen = dp->nfsdl_fhlen; 2328 NFSBCOPY(dp->nfsdl_fh, nop->nfso_fh, dp->nfsdl_fhlen); 2329 LIST_INIT(&nop->nfso_lock); 2330 nop->nfso_stateid.seqid = 0; 2331 nop->nfso_stateid.other[0] = 0; 2332 nop->nfso_stateid.other[1] = 0; 2333 nop->nfso_stateid.other[2] = 0; 2334 newnfs_copycred(&dp->nfsdl_cred, tcred); 2335 newnfs_copyincred(tcred, &nop->nfso_cred); 2336 tdp = NULL; 2337 error = nfscl_tryopen(nmp, NULL, nop->nfso_fh, 2338 nop->nfso_fhlen, nop->nfso_fh, nop->nfso_fhlen, 2339 nop->nfso_mode, nop, NULL, 0, &tdp, 1, 2340 delegtype, tcred, p); 2341 if (tdp != NULL) { 2342 if ((tdp->nfsdl_flags & NFSCLDL_WRITE)) 2343 mode = NFSV4OPEN_ACCESSWRITE; 2344 else 2345 mode = NFSV4OPEN_ACCESSREAD; 2346 if ((nop->nfso_mode & mode) == mode && 2347 nop->nfso_fhlen == tdp->nfsdl_fhlen && 2348 !NFSBCMP(nop->nfso_fh, tdp->nfsdl_fh, 2349 nop->nfso_fhlen)) { 2350 dp->nfsdl_stateid = tdp->nfsdl_stateid; 2351 dp->nfsdl_sizelimit = tdp->nfsdl_sizelimit; 2352 dp->nfsdl_ace = tdp->nfsdl_ace; 2353 dp->nfsdl_change = tdp->nfsdl_change; 2354 dp->nfsdl_flags &= ~NFSCLDL_NEEDRECLAIM; 2355 if ((tdp->nfsdl_flags & NFSCLDL_RECALL)) 2356 dp->nfsdl_flags |= NFSCLDL_RECALL; 2357 free(tdp, M_NFSCLDELEG); 2358 } else { 2359 TAILQ_INSERT_HEAD(&extra_deleg, tdp, nfsdl_list); 2360 } 2361 } 2362 } 2363 if (error) { 2364 if (nop != NULL) 2365 free(nop, M_NFSCLOPEN); 2366 if (error == NFSERR_NOGRACE && !recovered_one && 2367 NFSHASNFSV4N(nmp)) { 2368 /* 2369 * For NFSv4.1/4.2, the NFSERR_EXPIRED case will 2370 * actually end up here, since the client will do 2371 * a recovery for NFSERR_BADSESSION, but will get 2372 * an NFSERR_NOGRACE reply for the first "reclaim" 2373 * attempt. 2374 * So, call nfscl_expireclient() to recover the 2375 * opens as best we can and then do a reclaim 2376 * complete and return. 2377 */ 2378 nfsrpc_reclaimcomplete(nmp, cred, p); 2379 nfscl_expireclient(clp, nmp, tcred, p); 2380 free(nowp, M_NFSCLOWNER); 2381 goto out; 2382 } 2383 /* 2384 * Couldn't reclaim it, so throw the state 2385 * away. Ouch!! 2386 */ 2387 nfscl_cleandeleg(dp); 2388 nfscl_freedeleg(&clp->nfsc_deleg, dp, true); 2389 } else { 2390 recovered_one = true; 2391 LIST_INSERT_HEAD(&extra_open, nop, nfso_list); 2392 } 2393 } 2394 dp = ndp; 2395 } 2396 2397 /* 2398 * Now, get rid of extra Opens and Delegations. 2399 */ 2400 LIST_FOREACH_SAFE(op, &extra_open, nfso_list, nop) { 2401 do { 2402 newnfs_copycred(&op->nfso_cred, tcred); 2403 error = nfscl_tryclose(op, tcred, nmp, p, true); 2404 if (error == NFSERR_GRACE) 2405 (void) nfs_catnap(PZERO, error, "nfsexcls"); 2406 } while (error == NFSERR_GRACE); 2407 LIST_REMOVE(op, nfso_list); 2408 free(op, M_NFSCLOPEN); 2409 } 2410 if (nowp != NULL) 2411 free(nowp, M_NFSCLOWNER); 2412 2413 TAILQ_FOREACH_SAFE(dp, &extra_deleg, nfsdl_list, ndp) { 2414 do { 2415 newnfs_copycred(&dp->nfsdl_cred, tcred); 2416 error = nfscl_trydelegreturn(dp, tcred, nmp, p); 2417 if (error == NFSERR_GRACE) 2418 (void) nfs_catnap(PZERO, error, "nfsexdlg"); 2419 } while (error == NFSERR_GRACE); 2420 TAILQ_REMOVE(&extra_deleg, dp, nfsdl_list); 2421 free(dp, M_NFSCLDELEG); 2422 } 2423 2424 /* For NFSv4.1 or later, do a RECLAIM_COMPLETE. */ 2425 if (NFSHASNFSV4N(nmp)) 2426 (void)nfsrpc_reclaimcomplete(nmp, cred, p); 2427 2428 out: 2429 NFSLOCKCLSTATE(); 2430 clp->nfsc_flags &= ~NFSCLFLAGS_RECVRINPROG; 2431 wakeup(&clp->nfsc_flags); 2432 nfsv4_unlock(&clp->nfsc_lock, 0); 2433 NFSUNLOCKCLSTATE(); 2434 if (tcred != NULL) 2435 NFSFREECRED(tcred); 2436 } 2437 2438 /* 2439 * This function is called when a server replies with NFSERR_EXPIRED. 2440 * It deletes all state for the client and does a fresh SetClientId/confirm. 2441 * XXX Someday it should post a signal to the process(es) that hold the 2442 * state, so they know that lock state has been lost. 2443 */ 2444 int 2445 nfscl_hasexpired(struct nfsclclient *clp, u_int32_t clidrev, NFSPROC_T *p) 2446 { 2447 struct nfsmount *nmp; 2448 struct ucred *cred; 2449 int igotlock = 0, error, trycnt; 2450 2451 /* 2452 * If the clientid has gone away or a new SetClientid has already 2453 * been done, just return ok. 2454 */ 2455 if (clp == NULL || clidrev != clp->nfsc_clientidrev) 2456 return (0); 2457 2458 /* 2459 * First, lock the client structure, so everyone else will 2460 * block when trying to use state. Also, use NFSCLFLAGS_EXPIREIT so 2461 * that only one thread does the work. 2462 */ 2463 NFSLOCKCLSTATE(); 2464 clp->nfsc_flags |= NFSCLFLAGS_EXPIREIT; 2465 do { 2466 igotlock = nfsv4_lock(&clp->nfsc_lock, 1, NULL, 2467 NFSCLSTATEMUTEXPTR, NULL); 2468 } while (!igotlock && (clp->nfsc_flags & NFSCLFLAGS_EXPIREIT)); 2469 if ((clp->nfsc_flags & NFSCLFLAGS_EXPIREIT) == 0) { 2470 if (igotlock) 2471 nfsv4_unlock(&clp->nfsc_lock, 0); 2472 NFSUNLOCKCLSTATE(); 2473 return (0); 2474 } 2475 clp->nfsc_flags |= NFSCLFLAGS_RECVRINPROG; 2476 NFSUNLOCKCLSTATE(); 2477 2478 nmp = clp->nfsc_nmp; 2479 if (nmp == NULL) 2480 panic("nfscl expired"); 2481 cred = newnfs_getcred(); 2482 trycnt = 5; 2483 do { 2484 error = nfsrpc_setclient(nmp, clp, 0, NULL, cred, p); 2485 } while ((error == NFSERR_STALECLIENTID || 2486 error == NFSERR_BADSESSION || 2487 error == NFSERR_STALEDONTRECOVER) && --trycnt > 0); 2488 if (error) { 2489 NFSLOCKCLSTATE(); 2490 clp->nfsc_flags &= ~NFSCLFLAGS_RECOVER; 2491 } else { 2492 /* 2493 * Expire the state for the client. 2494 */ 2495 nfscl_expireclient(clp, nmp, cred, p); 2496 NFSLOCKCLSTATE(); 2497 clp->nfsc_flags |= NFSCLFLAGS_HASCLIENTID; 2498 clp->nfsc_flags &= ~NFSCLFLAGS_RECOVER; 2499 } 2500 clp->nfsc_flags &= ~(NFSCLFLAGS_EXPIREIT | NFSCLFLAGS_RECVRINPROG); 2501 wakeup(&clp->nfsc_flags); 2502 nfsv4_unlock(&clp->nfsc_lock, 0); 2503 NFSUNLOCKCLSTATE(); 2504 NFSFREECRED(cred); 2505 return (error); 2506 } 2507 2508 /* 2509 * This function inserts a lock in the list after insert_lop. 2510 */ 2511 static void 2512 nfscl_insertlock(struct nfscllockowner *lp, struct nfscllock *new_lop, 2513 struct nfscllock *insert_lop, int local) 2514 { 2515 2516 if ((struct nfscllockowner *)insert_lop == lp) 2517 LIST_INSERT_HEAD(&lp->nfsl_lock, new_lop, nfslo_list); 2518 else 2519 LIST_INSERT_AFTER(insert_lop, new_lop, nfslo_list); 2520 if (local) 2521 nfsstatsv1.cllocallocks++; 2522 else 2523 nfsstatsv1.cllocks++; 2524 } 2525 2526 /* 2527 * This function updates the locking for a lock owner and given file. It 2528 * maintains a list of lock ranges ordered on increasing file offset that 2529 * are NFSCLLOCK_READ or NFSCLLOCK_WRITE and non-overlapping (aka POSIX style). 2530 * It always adds new_lop to the list and sometimes uses the one pointed 2531 * at by other_lopp. 2532 * Returns 1 if the locks were modified, 0 otherwise. 2533 */ 2534 static int 2535 nfscl_updatelock(struct nfscllockowner *lp, struct nfscllock **new_lopp, 2536 struct nfscllock **other_lopp, int local) 2537 { 2538 struct nfscllock *new_lop = *new_lopp; 2539 struct nfscllock *lop, *tlop, *ilop; 2540 struct nfscllock *other_lop; 2541 int unlock = 0, modified = 0; 2542 u_int64_t tmp; 2543 2544 /* 2545 * Work down the list until the lock is merged. 2546 */ 2547 if (new_lop->nfslo_type == F_UNLCK) 2548 unlock = 1; 2549 ilop = (struct nfscllock *)lp; 2550 lop = LIST_FIRST(&lp->nfsl_lock); 2551 while (lop != NULL) { 2552 /* 2553 * Only check locks for this file that aren't before the start of 2554 * new lock's range. 2555 */ 2556 if (lop->nfslo_end >= new_lop->nfslo_first) { 2557 if (new_lop->nfslo_end < lop->nfslo_first) { 2558 /* 2559 * If the new lock ends before the start of the 2560 * current lock's range, no merge, just insert 2561 * the new lock. 2562 */ 2563 break; 2564 } 2565 if (new_lop->nfslo_type == lop->nfslo_type || 2566 (new_lop->nfslo_first <= lop->nfslo_first && 2567 new_lop->nfslo_end >= lop->nfslo_end)) { 2568 /* 2569 * This lock can be absorbed by the new lock/unlock. 2570 * This happens when it covers the entire range 2571 * of the old lock or is contiguous 2572 * with the old lock and is of the same type or an 2573 * unlock. 2574 */ 2575 if (new_lop->nfslo_type != lop->nfslo_type || 2576 new_lop->nfslo_first != lop->nfslo_first || 2577 new_lop->nfslo_end != lop->nfslo_end) 2578 modified = 1; 2579 if (lop->nfslo_first < new_lop->nfslo_first) 2580 new_lop->nfslo_first = lop->nfslo_first; 2581 if (lop->nfslo_end > new_lop->nfslo_end) 2582 new_lop->nfslo_end = lop->nfslo_end; 2583 tlop = lop; 2584 lop = LIST_NEXT(lop, nfslo_list); 2585 nfscl_freelock(tlop, local); 2586 continue; 2587 } 2588 2589 /* 2590 * All these cases are for contiguous locks that are not the 2591 * same type, so they can't be merged. 2592 */ 2593 if (new_lop->nfslo_first <= lop->nfslo_first) { 2594 /* 2595 * This case is where the new lock overlaps with the 2596 * first part of the old lock. Move the start of the 2597 * old lock to just past the end of the new lock. The 2598 * new lock will be inserted in front of the old, since 2599 * ilop hasn't been updated. (We are done now.) 2600 */ 2601 if (lop->nfslo_first != new_lop->nfslo_end) { 2602 lop->nfslo_first = new_lop->nfslo_end; 2603 modified = 1; 2604 } 2605 break; 2606 } 2607 if (new_lop->nfslo_end >= lop->nfslo_end) { 2608 /* 2609 * This case is where the new lock overlaps with the 2610 * end of the old lock's range. Move the old lock's 2611 * end to just before the new lock's first and insert 2612 * the new lock after the old lock. 2613 * Might not be done yet, since the new lock could 2614 * overlap further locks with higher ranges. 2615 */ 2616 if (lop->nfslo_end != new_lop->nfslo_first) { 2617 lop->nfslo_end = new_lop->nfslo_first; 2618 modified = 1; 2619 } 2620 ilop = lop; 2621 lop = LIST_NEXT(lop, nfslo_list); 2622 continue; 2623 } 2624 /* 2625 * The final case is where the new lock's range is in the 2626 * middle of the current lock's and splits the current lock 2627 * up. Use *other_lopp to handle the second part of the 2628 * split old lock range. (We are done now.) 2629 * For unlock, we use new_lop as other_lop and tmp, since 2630 * other_lop and new_lop are the same for this case. 2631 * We noted the unlock case above, so we don't need 2632 * new_lop->nfslo_type any longer. 2633 */ 2634 tmp = new_lop->nfslo_first; 2635 if (unlock) { 2636 other_lop = new_lop; 2637 *new_lopp = NULL; 2638 } else { 2639 other_lop = *other_lopp; 2640 *other_lopp = NULL; 2641 } 2642 other_lop->nfslo_first = new_lop->nfslo_end; 2643 other_lop->nfslo_end = lop->nfslo_end; 2644 other_lop->nfslo_type = lop->nfslo_type; 2645 lop->nfslo_end = tmp; 2646 nfscl_insertlock(lp, other_lop, lop, local); 2647 ilop = lop; 2648 modified = 1; 2649 break; 2650 } 2651 ilop = lop; 2652 lop = LIST_NEXT(lop, nfslo_list); 2653 if (lop == NULL) 2654 break; 2655 } 2656 2657 /* 2658 * Insert the new lock in the list at the appropriate place. 2659 */ 2660 if (!unlock) { 2661 nfscl_insertlock(lp, new_lop, ilop, local); 2662 *new_lopp = NULL; 2663 modified = 1; 2664 } 2665 return (modified); 2666 } 2667 2668 /* 2669 * This function must be run as a kernel thread. 2670 * It does Renew Ops and recovery, when required. 2671 */ 2672 void 2673 nfscl_renewthread(struct nfsclclient *clp, NFSPROC_T *p) 2674 { 2675 struct nfsclowner *owp, *nowp; 2676 struct nfsclopen *op; 2677 struct nfscllockowner *lp, *nlp; 2678 struct nfscldeleghead dh; 2679 struct nfscldeleg *dp, *ndp; 2680 struct ucred *cred; 2681 u_int32_t clidrev; 2682 int error, cbpathdown, islept, igotlock, ret, clearok; 2683 uint32_t recover_done_time = 0; 2684 time_t mytime; 2685 static time_t prevsec = 0; 2686 struct nfscllockownerfh *lfhp, *nlfhp; 2687 struct nfscllockownerfhhead lfh; 2688 struct nfscllayout *lyp, *nlyp; 2689 struct nfscldevinfo *dip, *ndip; 2690 struct nfscllayouthead rlh; 2691 struct nfsclrecalllayout *recallp; 2692 struct nfsclds *dsp; 2693 bool retok; 2694 struct mount *mp; 2695 vnode_t vp; 2696 2697 cred = newnfs_getcred(); 2698 NFSLOCKCLSTATE(); 2699 clp->nfsc_flags |= NFSCLFLAGS_HASTHREAD; 2700 mp = clp->nfsc_nmp->nm_mountp; 2701 NFSUNLOCKCLSTATE(); 2702 for(;;) { 2703 newnfs_setroot(cred); 2704 cbpathdown = 0; 2705 if (clp->nfsc_flags & NFSCLFLAGS_RECOVER) { 2706 /* 2707 * Only allow one full recover within 1/2 of the lease 2708 * duration (nfsc_renew). 2709 * retok is value/result. If passed in set to true, 2710 * it indicates only a CreateSession operation should 2711 * be attempted. 2712 * If it is returned true, it indicates that the 2713 * recovery only required a CreateSession. 2714 */ 2715 retok = true; 2716 if (recover_done_time < NFSD_MONOSEC) { 2717 recover_done_time = NFSD_MONOSEC + 2718 clp->nfsc_renew; 2719 retok = false; 2720 } 2721 NFSCL_DEBUG(1, "Doing recovery, only " 2722 "createsession=%d\n", retok); 2723 nfscl_recover(clp, &retok, cred, p); 2724 } 2725 if (clp->nfsc_expire <= NFSD_MONOSEC && 2726 (clp->nfsc_flags & NFSCLFLAGS_HASCLIENTID)) { 2727 clp->nfsc_expire = NFSD_MONOSEC + clp->nfsc_renew; 2728 clidrev = clp->nfsc_clientidrev; 2729 error = nfsrpc_renew(clp, NULL, cred, p); 2730 if (error == NFSERR_CBPATHDOWN) 2731 cbpathdown = 1; 2732 else if (error == NFSERR_STALECLIENTID || 2733 error == NFSERR_BADSESSION) { 2734 NFSLOCKCLSTATE(); 2735 clp->nfsc_flags |= NFSCLFLAGS_RECOVER; 2736 NFSUNLOCKCLSTATE(); 2737 } else if (error == NFSERR_EXPIRED) 2738 (void) nfscl_hasexpired(clp, clidrev, p); 2739 } 2740 2741 checkdsrenew: 2742 if (NFSHASNFSV4N(clp->nfsc_nmp)) { 2743 /* Do renews for any DS sessions. */ 2744 NFSLOCKMNT(clp->nfsc_nmp); 2745 /* Skip first entry, since the MDS is handled above. */ 2746 dsp = TAILQ_FIRST(&clp->nfsc_nmp->nm_sess); 2747 if (dsp != NULL) 2748 dsp = TAILQ_NEXT(dsp, nfsclds_list); 2749 while (dsp != NULL) { 2750 if (dsp->nfsclds_expire <= NFSD_MONOSEC && 2751 dsp->nfsclds_sess.nfsess_defunct == 0) { 2752 dsp->nfsclds_expire = NFSD_MONOSEC + 2753 clp->nfsc_renew; 2754 NFSUNLOCKMNT(clp->nfsc_nmp); 2755 (void)nfsrpc_renew(clp, dsp, cred, p); 2756 goto checkdsrenew; 2757 } 2758 dsp = TAILQ_NEXT(dsp, nfsclds_list); 2759 } 2760 NFSUNLOCKMNT(clp->nfsc_nmp); 2761 } 2762 2763 TAILQ_INIT(&dh); 2764 NFSLOCKCLSTATE(); 2765 if (cbpathdown) 2766 /* It's a Total Recall! */ 2767 nfscl_totalrecall(clp); 2768 2769 /* 2770 * Now, handle defunct owners. 2771 */ 2772 LIST_FOREACH_SAFE(owp, &clp->nfsc_owner, nfsow_list, nowp) { 2773 if (LIST_EMPTY(&owp->nfsow_open)) { 2774 if (owp->nfsow_defunct != 0) 2775 nfscl_freeopenowner(owp, 0); 2776 } 2777 } 2778 2779 /* 2780 * Do the recall on any delegations. To avoid trouble, always 2781 * come back up here after having slept. 2782 */ 2783 igotlock = 0; 2784 tryagain: 2785 dp = TAILQ_FIRST(&clp->nfsc_deleg); 2786 while (dp != NULL) { 2787 ndp = TAILQ_NEXT(dp, nfsdl_list); 2788 if ((dp->nfsdl_flags & NFSCLDL_RECALL)) { 2789 /* 2790 * Wait for outstanding I/O ops to be done. 2791 */ 2792 if (dp->nfsdl_rwlock.nfslock_usecnt > 0) { 2793 if (igotlock) { 2794 nfsv4_unlock(&clp->nfsc_lock, 0); 2795 igotlock = 0; 2796 } 2797 dp->nfsdl_rwlock.nfslock_lock |= 2798 NFSV4LOCK_WANTED; 2799 msleep(&dp->nfsdl_rwlock, 2800 NFSCLSTATEMUTEXPTR, PVFS, "nfscld", 2801 5 * hz); 2802 if (NFSCL_FORCEDISM(mp)) 2803 goto terminate; 2804 goto tryagain; 2805 } 2806 while (!igotlock) { 2807 igotlock = nfsv4_lock(&clp->nfsc_lock, 1, 2808 &islept, NFSCLSTATEMUTEXPTR, mp); 2809 if (igotlock == 0 && NFSCL_FORCEDISM(mp)) 2810 goto terminate; 2811 if (islept) 2812 goto tryagain; 2813 } 2814 NFSUNLOCKCLSTATE(); 2815 newnfs_copycred(&dp->nfsdl_cred, cred); 2816 ret = nfscl_recalldeleg(clp, clp->nfsc_nmp, dp, 2817 NULL, cred, p, 1, &vp); 2818 if (!ret) { 2819 nfscl_cleandeleg(dp); 2820 TAILQ_REMOVE(&clp->nfsc_deleg, dp, 2821 nfsdl_list); 2822 LIST_REMOVE(dp, nfsdl_hash); 2823 TAILQ_INSERT_HEAD(&dh, dp, nfsdl_list); 2824 nfscl_delegcnt--; 2825 nfsstatsv1.cldelegates--; 2826 } 2827 NFSLOCKCLSTATE(); 2828 /* 2829 * The nfsc_lock must be released before doing 2830 * vrele(), since it might call nfs_inactive(). 2831 * For the unlikely case where the vnode failed 2832 * to be acquired by nfscl_recalldeleg(), a 2833 * VOP_RECLAIM() should be in progress and it 2834 * will return the delegation. 2835 */ 2836 nfsv4_unlock(&clp->nfsc_lock, 0); 2837 igotlock = 0; 2838 if (vp != NULL) { 2839 NFSUNLOCKCLSTATE(); 2840 vrele(vp); 2841 NFSLOCKCLSTATE(); 2842 } 2843 goto tryagain; 2844 } 2845 dp = ndp; 2846 } 2847 2848 /* 2849 * Clear out old delegations, if we are above the high water 2850 * mark. Only clear out ones with no state related to them. 2851 * The tailq list is in LRU order. 2852 */ 2853 dp = TAILQ_LAST(&clp->nfsc_deleg, nfscldeleghead); 2854 while (nfscl_delegcnt > nfscl_deleghighwater && dp != NULL) { 2855 ndp = TAILQ_PREV(dp, nfscldeleghead, nfsdl_list); 2856 if (dp->nfsdl_rwlock.nfslock_usecnt == 0 && 2857 dp->nfsdl_rwlock.nfslock_lock == 0 && 2858 dp->nfsdl_timestamp < NFSD_MONOSEC && 2859 (dp->nfsdl_flags & (NFSCLDL_RECALL | NFSCLDL_ZAPPED | 2860 NFSCLDL_NEEDRECLAIM | NFSCLDL_DELEGRET)) == 0) { 2861 clearok = 1; 2862 LIST_FOREACH(owp, &dp->nfsdl_owner, nfsow_list) { 2863 op = LIST_FIRST(&owp->nfsow_open); 2864 if (op != NULL) { 2865 clearok = 0; 2866 break; 2867 } 2868 } 2869 if (clearok) { 2870 LIST_FOREACH(lp, &dp->nfsdl_lock, nfsl_list) { 2871 if (!LIST_EMPTY(&lp->nfsl_lock)) { 2872 clearok = 0; 2873 break; 2874 } 2875 } 2876 } 2877 if (clearok) { 2878 TAILQ_REMOVE(&clp->nfsc_deleg, dp, nfsdl_list); 2879 LIST_REMOVE(dp, nfsdl_hash); 2880 TAILQ_INSERT_HEAD(&dh, dp, nfsdl_list); 2881 nfscl_delegcnt--; 2882 nfsstatsv1.cldelegates--; 2883 } 2884 } 2885 dp = ndp; 2886 } 2887 if (igotlock) 2888 nfsv4_unlock(&clp->nfsc_lock, 0); 2889 2890 /* 2891 * Do the recall on any layouts. To avoid trouble, always 2892 * come back up here after having slept. 2893 */ 2894 TAILQ_INIT(&rlh); 2895 tryagain2: 2896 TAILQ_FOREACH_SAFE(lyp, &clp->nfsc_layout, nfsly_list, nlyp) { 2897 if ((lyp->nfsly_flags & NFSLY_RECALL) != 0) { 2898 /* 2899 * Wait for outstanding I/O ops to be done. 2900 */ 2901 if (lyp->nfsly_lock.nfslock_usecnt > 0 || 2902 (lyp->nfsly_lock.nfslock_lock & 2903 NFSV4LOCK_LOCK) != 0) { 2904 lyp->nfsly_lock.nfslock_lock |= 2905 NFSV4LOCK_WANTED; 2906 msleep(&lyp->nfsly_lock.nfslock_lock, 2907 NFSCLSTATEMUTEXPTR, PVFS, "nfslyp", 2908 5 * hz); 2909 if (NFSCL_FORCEDISM(mp)) 2910 goto terminate; 2911 goto tryagain2; 2912 } 2913 /* Move the layout to the recall list. */ 2914 TAILQ_REMOVE(&clp->nfsc_layout, lyp, 2915 nfsly_list); 2916 LIST_REMOVE(lyp, nfsly_hash); 2917 TAILQ_INSERT_HEAD(&rlh, lyp, nfsly_list); 2918 2919 /* Handle any layout commits. */ 2920 if (!NFSHASNOLAYOUTCOMMIT(clp->nfsc_nmp) && 2921 (lyp->nfsly_flags & NFSLY_WRITTEN) != 0) { 2922 lyp->nfsly_flags &= ~NFSLY_WRITTEN; 2923 NFSUNLOCKCLSTATE(); 2924 NFSCL_DEBUG(3, "do layoutcommit\n"); 2925 nfscl_dolayoutcommit(clp->nfsc_nmp, lyp, 2926 cred, p); 2927 NFSLOCKCLSTATE(); 2928 goto tryagain2; 2929 } 2930 } 2931 } 2932 2933 /* Now, look for stale layouts. */ 2934 lyp = TAILQ_LAST(&clp->nfsc_layout, nfscllayouthead); 2935 while (lyp != NULL) { 2936 nlyp = TAILQ_PREV(lyp, nfscllayouthead, nfsly_list); 2937 if (lyp->nfsly_timestamp < NFSD_MONOSEC && 2938 (lyp->nfsly_flags & (NFSLY_RECALL | 2939 NFSLY_RETONCLOSE)) == 0 && 2940 lyp->nfsly_lock.nfslock_usecnt == 0 && 2941 lyp->nfsly_lock.nfslock_lock == 0) { 2942 NFSCL_DEBUG(4, "ret stale lay=%d\n", 2943 nfscl_layoutcnt); 2944 recallp = malloc(sizeof(*recallp), 2945 M_NFSLAYRECALL, M_NOWAIT); 2946 if (recallp == NULL) 2947 break; 2948 (void)nfscl_layoutrecall(NFSLAYOUTRETURN_FILE, 2949 lyp, NFSLAYOUTIOMODE_ANY, 0, UINT64_MAX, 2950 lyp->nfsly_stateid.seqid, 0, 0, NULL, 2951 recallp); 2952 } 2953 lyp = nlyp; 2954 } 2955 2956 /* 2957 * Free up any unreferenced device info structures. 2958 */ 2959 LIST_FOREACH_SAFE(dip, &clp->nfsc_devinfo, nfsdi_list, ndip) { 2960 if (dip->nfsdi_layoutrefs == 0 && 2961 dip->nfsdi_refcnt == 0) { 2962 NFSCL_DEBUG(4, "freeing devinfo\n"); 2963 LIST_REMOVE(dip, nfsdi_list); 2964 nfscl_freedevinfo(dip); 2965 } 2966 } 2967 NFSUNLOCKCLSTATE(); 2968 2969 /* Do layout return(s), as required. */ 2970 TAILQ_FOREACH_SAFE(lyp, &rlh, nfsly_list, nlyp) { 2971 TAILQ_REMOVE(&rlh, lyp, nfsly_list); 2972 NFSCL_DEBUG(4, "ret layout\n"); 2973 nfscl_layoutreturn(clp->nfsc_nmp, lyp, cred, p); 2974 if ((lyp->nfsly_flags & NFSLY_RETONCLOSE) != 0) { 2975 NFSLOCKCLSTATE(); 2976 lyp->nfsly_flags |= NFSLY_RETURNED; 2977 wakeup(lyp); 2978 NFSUNLOCKCLSTATE(); 2979 } else 2980 nfscl_freelayout(lyp); 2981 } 2982 2983 /* 2984 * Delegreturn any delegations cleaned out or recalled. 2985 */ 2986 TAILQ_FOREACH_SAFE(dp, &dh, nfsdl_list, ndp) { 2987 newnfs_copycred(&dp->nfsdl_cred, cred); 2988 (void) nfscl_trydelegreturn(dp, cred, clp->nfsc_nmp, p); 2989 TAILQ_REMOVE(&dh, dp, nfsdl_list); 2990 free(dp, M_NFSCLDELEG); 2991 } 2992 2993 SLIST_INIT(&lfh); 2994 /* 2995 * Call nfscl_cleanupkext() once per second to check for 2996 * open/lock owners where the process has exited. 2997 */ 2998 mytime = NFSD_MONOSEC; 2999 if (prevsec != mytime) { 3000 prevsec = mytime; 3001 nfscl_cleanupkext(clp, &lfh); 3002 } 3003 3004 /* 3005 * Do a ReleaseLockOwner for all lock owners where the 3006 * associated process no longer exists, as found by 3007 * nfscl_cleanupkext(). 3008 */ 3009 newnfs_setroot(cred); 3010 SLIST_FOREACH_SAFE(lfhp, &lfh, nfslfh_list, nlfhp) { 3011 LIST_FOREACH_SAFE(lp, &lfhp->nfslfh_lock, nfsl_list, 3012 nlp) { 3013 (void)nfsrpc_rellockown(clp->nfsc_nmp, lp, 3014 lfhp->nfslfh_fh, lfhp->nfslfh_len, cred, 3015 p); 3016 nfscl_freelockowner(lp, 0); 3017 } 3018 free(lfhp, M_TEMP); 3019 } 3020 SLIST_INIT(&lfh); 3021 3022 NFSLOCKCLSTATE(); 3023 if ((clp->nfsc_flags & NFSCLFLAGS_RECOVER) == 0) 3024 (void)mtx_sleep(clp, NFSCLSTATEMUTEXPTR, PWAIT, "nfscl", 3025 hz); 3026 terminate: 3027 if (clp->nfsc_flags & NFSCLFLAGS_UMOUNT) { 3028 clp->nfsc_flags &= ~NFSCLFLAGS_HASTHREAD; 3029 NFSUNLOCKCLSTATE(); 3030 NFSFREECRED(cred); 3031 wakeup((caddr_t)clp); 3032 return; 3033 } 3034 NFSUNLOCKCLSTATE(); 3035 } 3036 } 3037 3038 /* 3039 * Initiate state recovery. Called when NFSERR_STALECLIENTID, 3040 * NFSERR_STALESTATEID or NFSERR_BADSESSION is received. 3041 */ 3042 void 3043 nfscl_initiate_recovery(struct nfsclclient *clp) 3044 { 3045 3046 if (clp == NULL) 3047 return; 3048 NFSLOCKCLSTATE(); 3049 clp->nfsc_flags |= NFSCLFLAGS_RECOVER; 3050 NFSUNLOCKCLSTATE(); 3051 wakeup((caddr_t)clp); 3052 } 3053 3054 /* 3055 * Dump out the state stuff for debugging. 3056 */ 3057 void 3058 nfscl_dumpstate(struct nfsmount *nmp, int openowner, int opens, 3059 int lockowner, int locks) 3060 { 3061 struct nfsclclient *clp; 3062 struct nfsclowner *owp; 3063 struct nfsclopen *op; 3064 struct nfscllockowner *lp; 3065 struct nfscllock *lop; 3066 struct nfscldeleg *dp; 3067 3068 clp = nmp->nm_clp; 3069 if (clp == NULL) { 3070 printf("nfscl dumpstate NULL clp\n"); 3071 return; 3072 } 3073 NFSLOCKCLSTATE(); 3074 TAILQ_FOREACH(dp, &clp->nfsc_deleg, nfsdl_list) { 3075 LIST_FOREACH(owp, &dp->nfsdl_owner, nfsow_list) { 3076 if (openowner && !LIST_EMPTY(&owp->nfsow_open)) 3077 printf("owner=0x%x 0x%x 0x%x 0x%x seqid=%d\n", 3078 owp->nfsow_owner[0], owp->nfsow_owner[1], 3079 owp->nfsow_owner[2], owp->nfsow_owner[3], 3080 owp->nfsow_seqid); 3081 LIST_FOREACH(op, &owp->nfsow_open, nfso_list) { 3082 if (opens) 3083 printf("open st=0x%x 0x%x 0x%x cnt=%d fh12=0x%x\n", 3084 op->nfso_stateid.other[0], op->nfso_stateid.other[1], 3085 op->nfso_stateid.other[2], op->nfso_opencnt, 3086 op->nfso_fh[12]); 3087 LIST_FOREACH(lp, &op->nfso_lock, nfsl_list) { 3088 if (lockowner) 3089 printf("lckown=0x%x 0x%x 0x%x 0x%x seqid=%d st=0x%x 0x%x 0x%x\n", 3090 lp->nfsl_owner[0], lp->nfsl_owner[1], 3091 lp->nfsl_owner[2], lp->nfsl_owner[3], 3092 lp->nfsl_seqid, 3093 lp->nfsl_stateid.other[0], lp->nfsl_stateid.other[1], 3094 lp->nfsl_stateid.other[2]); 3095 LIST_FOREACH(lop, &lp->nfsl_lock, nfslo_list) { 3096 if (locks) 3097 #ifdef __FreeBSD__ 3098 printf("lck typ=%d fst=%ju end=%ju\n", 3099 lop->nfslo_type, (intmax_t)lop->nfslo_first, 3100 (intmax_t)lop->nfslo_end); 3101 #else 3102 printf("lck typ=%d fst=%qd end=%qd\n", 3103 lop->nfslo_type, lop->nfslo_first, 3104 lop->nfslo_end); 3105 #endif 3106 } 3107 } 3108 } 3109 } 3110 } 3111 LIST_FOREACH(owp, &clp->nfsc_owner, nfsow_list) { 3112 if (openowner && !LIST_EMPTY(&owp->nfsow_open)) 3113 printf("owner=0x%x 0x%x 0x%x 0x%x seqid=%d\n", 3114 owp->nfsow_owner[0], owp->nfsow_owner[1], 3115 owp->nfsow_owner[2], owp->nfsow_owner[3], 3116 owp->nfsow_seqid); 3117 LIST_FOREACH(op, &owp->nfsow_open, nfso_list) { 3118 if (opens) 3119 printf("open st=0x%x 0x%x 0x%x cnt=%d fh12=0x%x\n", 3120 op->nfso_stateid.other[0], op->nfso_stateid.other[1], 3121 op->nfso_stateid.other[2], op->nfso_opencnt, 3122 op->nfso_fh[12]); 3123 LIST_FOREACH(lp, &op->nfso_lock, nfsl_list) { 3124 if (lockowner) 3125 printf("lckown=0x%x 0x%x 0x%x 0x%x seqid=%d st=0x%x 0x%x 0x%x\n", 3126 lp->nfsl_owner[0], lp->nfsl_owner[1], 3127 lp->nfsl_owner[2], lp->nfsl_owner[3], 3128 lp->nfsl_seqid, 3129 lp->nfsl_stateid.other[0], lp->nfsl_stateid.other[1], 3130 lp->nfsl_stateid.other[2]); 3131 LIST_FOREACH(lop, &lp->nfsl_lock, nfslo_list) { 3132 if (locks) 3133 #ifdef __FreeBSD__ 3134 printf("lck typ=%d fst=%ju end=%ju\n", 3135 lop->nfslo_type, (intmax_t)lop->nfslo_first, 3136 (intmax_t)lop->nfslo_end); 3137 #else 3138 printf("lck typ=%d fst=%qd end=%qd\n", 3139 lop->nfslo_type, lop->nfslo_first, 3140 lop->nfslo_end); 3141 #endif 3142 } 3143 } 3144 } 3145 } 3146 NFSUNLOCKCLSTATE(); 3147 } 3148 3149 /* 3150 * Check for duplicate open owners and opens. 3151 * (Only used as a diagnostic aid.) 3152 */ 3153 void 3154 nfscl_dupopen(vnode_t vp, int dupopens) 3155 { 3156 struct nfsclclient *clp; 3157 struct nfsclowner *owp, *owp2; 3158 struct nfsclopen *op, *op2; 3159 struct nfsfh *nfhp; 3160 3161 clp = VFSTONFS(vp->v_mount)->nm_clp; 3162 if (clp == NULL) { 3163 printf("nfscl dupopen NULL clp\n"); 3164 return; 3165 } 3166 nfhp = VTONFS(vp)->n_fhp; 3167 NFSLOCKCLSTATE(); 3168 3169 /* 3170 * First, search for duplicate owners. 3171 * These should never happen! 3172 */ 3173 LIST_FOREACH(owp2, &clp->nfsc_owner, nfsow_list) { 3174 LIST_FOREACH(owp, &clp->nfsc_owner, nfsow_list) { 3175 if (owp != owp2 && 3176 !NFSBCMP(owp->nfsow_owner, owp2->nfsow_owner, 3177 NFSV4CL_LOCKNAMELEN)) { 3178 NFSUNLOCKCLSTATE(); 3179 printf("DUP OWNER\n"); 3180 nfscl_dumpstate(VFSTONFS(vp->v_mount), 1, 1, 0, 0); 3181 return; 3182 } 3183 } 3184 } 3185 3186 /* 3187 * Now, search for duplicate stateids. 3188 * These shouldn't happen, either. 3189 */ 3190 LIST_FOREACH(owp2, &clp->nfsc_owner, nfsow_list) { 3191 LIST_FOREACH(op2, &owp2->nfsow_open, nfso_list) { 3192 LIST_FOREACH(owp, &clp->nfsc_owner, nfsow_list) { 3193 LIST_FOREACH(op, &owp->nfsow_open, nfso_list) { 3194 if (op != op2 && 3195 (op->nfso_stateid.other[0] != 0 || 3196 op->nfso_stateid.other[1] != 0 || 3197 op->nfso_stateid.other[2] != 0) && 3198 op->nfso_stateid.other[0] == op2->nfso_stateid.other[0] && 3199 op->nfso_stateid.other[1] == op2->nfso_stateid.other[1] && 3200 op->nfso_stateid.other[2] == op2->nfso_stateid.other[2]) { 3201 NFSUNLOCKCLSTATE(); 3202 printf("DUP STATEID\n"); 3203 nfscl_dumpstate(VFSTONFS(vp->v_mount), 1, 1, 0, 0); 3204 return; 3205 } 3206 } 3207 } 3208 } 3209 } 3210 3211 /* 3212 * Now search for duplicate opens. 3213 * Duplicate opens for the same owner 3214 * should never occur. Other duplicates are 3215 * possible and are checked for if "dupopens" 3216 * is true. 3217 */ 3218 LIST_FOREACH(owp2, &clp->nfsc_owner, nfsow_list) { 3219 LIST_FOREACH(op2, &owp2->nfsow_open, nfso_list) { 3220 if (nfhp->nfh_len == op2->nfso_fhlen && 3221 !NFSBCMP(nfhp->nfh_fh, op2->nfso_fh, nfhp->nfh_len)) { 3222 LIST_FOREACH(owp, &clp->nfsc_owner, nfsow_list) { 3223 LIST_FOREACH(op, &owp->nfsow_open, nfso_list) { 3224 if (op != op2 && nfhp->nfh_len == op->nfso_fhlen && 3225 !NFSBCMP(nfhp->nfh_fh, op->nfso_fh, nfhp->nfh_len) && 3226 (!NFSBCMP(op->nfso_own->nfsow_owner, 3227 op2->nfso_own->nfsow_owner, NFSV4CL_LOCKNAMELEN) || 3228 dupopens)) { 3229 if (!NFSBCMP(op->nfso_own->nfsow_owner, 3230 op2->nfso_own->nfsow_owner, NFSV4CL_LOCKNAMELEN)) { 3231 NFSUNLOCKCLSTATE(); 3232 printf("BADDUP OPEN\n"); 3233 } else { 3234 NFSUNLOCKCLSTATE(); 3235 printf("DUP OPEN\n"); 3236 } 3237 nfscl_dumpstate(VFSTONFS(vp->v_mount), 1, 1, 0, 3238 0); 3239 return; 3240 } 3241 } 3242 } 3243 } 3244 } 3245 } 3246 NFSUNLOCKCLSTATE(); 3247 } 3248 3249 /* 3250 * During close, find an open that needs to be dereferenced and 3251 * dereference it. If there are no more opens for this file, 3252 * log a message to that effect. 3253 * Opens aren't actually Close'd until VOP_INACTIVE() is performed 3254 * on the file's vnode. 3255 * This is the safe way, since it is difficult to identify 3256 * which open the close is for and I/O can be performed after the 3257 * close(2) system call when a file is mmap'd. 3258 * If it returns 0 for success, there will be a referenced 3259 * clp returned via clpp. 3260 */ 3261 int 3262 nfscl_getclose(vnode_t vp, struct nfsclclient **clpp) 3263 { 3264 struct nfsclclient *clp; 3265 struct nfsclowner *owp; 3266 struct nfsclopen *op; 3267 struct nfscldeleg *dp; 3268 struct nfsfh *nfhp; 3269 int error, notdecr; 3270 3271 error = nfscl_getcl(vp->v_mount, NULL, NULL, false, true, &clp); 3272 if (error) 3273 return (error); 3274 *clpp = clp; 3275 3276 nfhp = VTONFS(vp)->n_fhp; 3277 notdecr = 1; 3278 NFSLOCKCLSTATE(); 3279 /* 3280 * First, look for one under a delegation that was locally issued 3281 * and just decrement the opencnt for it. Since all my Opens against 3282 * the server are DENY_NONE, I don't see a problem with hanging 3283 * onto them. (It is much easier to use one of the extant Opens 3284 * that I already have on the server when a Delegation is recalled 3285 * than to do fresh Opens.) Someday, I might need to rethink this, but. 3286 */ 3287 dp = nfscl_finddeleg(clp, nfhp->nfh_fh, nfhp->nfh_len); 3288 if (dp != NULL) { 3289 LIST_FOREACH(owp, &dp->nfsdl_owner, nfsow_list) { 3290 op = LIST_FIRST(&owp->nfsow_open); 3291 if (op != NULL) { 3292 /* 3293 * Since a delegation is for a file, there 3294 * should never be more than one open for 3295 * each openowner. 3296 */ 3297 if (LIST_NEXT(op, nfso_list) != NULL) 3298 panic("nfscdeleg opens"); 3299 if (notdecr && op->nfso_opencnt > 0) { 3300 notdecr = 0; 3301 op->nfso_opencnt--; 3302 break; 3303 } 3304 } 3305 } 3306 } 3307 3308 /* Now process the opens against the server. */ 3309 LIST_FOREACH(op, NFSCLOPENHASH(clp, nfhp->nfh_fh, nfhp->nfh_len), 3310 nfso_hash) { 3311 if (op->nfso_fhlen == nfhp->nfh_len && 3312 !NFSBCMP(op->nfso_fh, nfhp->nfh_fh, 3313 nfhp->nfh_len)) { 3314 /* Found an open, decrement cnt if possible */ 3315 if (notdecr && op->nfso_opencnt > 0) { 3316 notdecr = 0; 3317 op->nfso_opencnt--; 3318 } 3319 /* 3320 * There are more opens, so just return. 3321 */ 3322 if (op->nfso_opencnt > 0) { 3323 NFSUNLOCKCLSTATE(); 3324 return (0); 3325 } 3326 } 3327 } 3328 NFSUNLOCKCLSTATE(); 3329 if (notdecr) 3330 printf("nfscl: never fnd open\n"); 3331 return (0); 3332 } 3333 3334 int 3335 nfscl_doclose(vnode_t vp, struct nfsclclient **clpp, NFSPROC_T *p) 3336 { 3337 struct nfsclclient *clp; 3338 struct nfsmount *nmp; 3339 struct nfsclowner *owp, *nowp; 3340 struct nfsclopen *op, *nop; 3341 struct nfsclopenhead delayed; 3342 struct nfscldeleg *dp; 3343 struct nfsfh *nfhp; 3344 struct nfsclrecalllayout *recallp; 3345 struct nfscllayout *lyp; 3346 int error; 3347 3348 error = nfscl_getcl(vp->v_mount, NULL, NULL, false, true, &clp); 3349 if (error) 3350 return (error); 3351 *clpp = clp; 3352 3353 nmp = VFSTONFS(vp->v_mount); 3354 nfhp = VTONFS(vp)->n_fhp; 3355 recallp = malloc(sizeof(*recallp), M_NFSLAYRECALL, M_WAITOK); 3356 NFSLOCKCLSTATE(); 3357 /* 3358 * First get rid of the local Open structures, which should be no 3359 * longer in use. 3360 */ 3361 dp = nfscl_finddeleg(clp, nfhp->nfh_fh, nfhp->nfh_len); 3362 if (dp != NULL) { 3363 LIST_FOREACH_SAFE(owp, &dp->nfsdl_owner, nfsow_list, nowp) { 3364 op = LIST_FIRST(&owp->nfsow_open); 3365 if (op != NULL) { 3366 KASSERT((op->nfso_opencnt == 0), 3367 ("nfscl: bad open cnt on deleg")); 3368 nfscl_freeopen(op, 1, true); 3369 } 3370 nfscl_freeopenowner(owp, 1); 3371 } 3372 } 3373 3374 /* Return any layouts marked return on close. */ 3375 nfscl_retoncloselayout(vp, clp, nfhp->nfh_fh, nfhp->nfh_len, &recallp, 3376 &lyp); 3377 3378 /* Now process the opens against the server. */ 3379 LIST_INIT(&delayed); 3380 lookformore: 3381 LIST_FOREACH(op, NFSCLOPENHASH(clp, nfhp->nfh_fh, nfhp->nfh_len), 3382 nfso_hash) { 3383 if (op->nfso_fhlen == nfhp->nfh_len && 3384 !NFSBCMP(op->nfso_fh, nfhp->nfh_fh, 3385 nfhp->nfh_len)) { 3386 /* Found an open, close it. */ 3387 #ifdef DIAGNOSTIC 3388 KASSERT((op->nfso_opencnt == 0), 3389 ("nfscl: bad open cnt on server (%d)", 3390 op->nfso_opencnt)); 3391 #endif 3392 NFSUNLOCKCLSTATE(); 3393 if (NFSHASNFSV4N(nmp)) 3394 error = nfsrpc_doclose(nmp, op, p, false, true); 3395 else 3396 error = nfsrpc_doclose(nmp, op, p, true, true); 3397 NFSLOCKCLSTATE(); 3398 if (error == NFSERR_DELAY) { 3399 nfscl_unlinkopen(op); 3400 op->nfso_own = NULL; 3401 LIST_INSERT_HEAD(&delayed, op, nfso_list); 3402 } 3403 goto lookformore; 3404 } 3405 } 3406 nfscl_clrelease(clp); 3407 3408 /* Now, wait for any layout that is returned upon close. */ 3409 if (lyp != NULL) { 3410 while ((lyp->nfsly_flags & NFSLY_RETURNED) == 0) { 3411 if (NFSCL_FORCEDISM(nmp->nm_mountp)) { 3412 lyp = NULL; 3413 break; 3414 } 3415 msleep(lyp, NFSCLSTATEMUTEXPTR, PZERO, "nfslroc", hz); 3416 } 3417 if (lyp != NULL) 3418 nfscl_freelayout(lyp); 3419 } 3420 3421 NFSUNLOCKCLSTATE(); 3422 /* 3423 * recallp has been set NULL by nfscl_retoncloselayout() if it was 3424 * used by the function, but calling free() with a NULL pointer is ok. 3425 */ 3426 free(recallp, M_NFSLAYRECALL); 3427 3428 /* Now, loop retrying the delayed closes. */ 3429 LIST_FOREACH_SAFE(op, &delayed, nfso_list, nop) { 3430 nfsrpc_doclose(nmp, op, p, true, false); 3431 LIST_REMOVE(op, nfso_list); 3432 nfscl_freeopen(op, 0, false); 3433 } 3434 return (0); 3435 } 3436 3437 /* 3438 * Return all delegations on this client. 3439 * (Must be called with client sleep lock.) 3440 */ 3441 static void 3442 nfscl_delegreturnall(struct nfsclclient *clp, NFSPROC_T *p, 3443 struct nfscldeleghead *dhp) 3444 { 3445 struct nfscldeleg *dp, *ndp; 3446 struct ucred *cred; 3447 3448 cred = newnfs_getcred(); 3449 TAILQ_FOREACH_SAFE(dp, &clp->nfsc_deleg, nfsdl_list, ndp) { 3450 nfscl_cleandeleg(dp); 3451 (void) nfscl_trydelegreturn(dp, cred, clp->nfsc_nmp, p); 3452 if (dhp != NULL) { 3453 nfscl_freedeleg(&clp->nfsc_deleg, dp, false); 3454 TAILQ_INSERT_HEAD(dhp, dp, nfsdl_list); 3455 } else 3456 nfscl_freedeleg(&clp->nfsc_deleg, dp, true); 3457 } 3458 NFSFREECRED(cred); 3459 } 3460 3461 /* 3462 * Return any delegation for this vp. 3463 */ 3464 void 3465 nfscl_delegreturnvp(vnode_t vp, NFSPROC_T *p) 3466 { 3467 struct nfsclclient *clp; 3468 struct nfscldeleg *dp; 3469 struct ucred *cred; 3470 struct nfsnode *np; 3471 struct nfsmount *nmp; 3472 3473 nmp = VFSTONFS(vp->v_mount); 3474 NFSLOCKMNT(nmp); 3475 if ((nmp->nm_privflag & NFSMNTP_DELEGISSUED) == 0) { 3476 NFSUNLOCKMNT(nmp); 3477 return; 3478 } 3479 NFSUNLOCKMNT(nmp); 3480 np = VTONFS(vp); 3481 cred = newnfs_getcred(); 3482 dp = NULL; 3483 NFSLOCKCLSTATE(); 3484 clp = nmp->nm_clp; 3485 if (clp != NULL) 3486 dp = nfscl_finddeleg(clp, np->n_fhp->nfh_fh, 3487 np->n_fhp->nfh_len); 3488 if (dp != NULL) { 3489 nfscl_cleandeleg(dp); 3490 nfscl_freedeleg(&clp->nfsc_deleg, dp, false); 3491 NFSUNLOCKCLSTATE(); 3492 newnfs_copycred(&dp->nfsdl_cred, cred); 3493 nfscl_trydelegreturn(dp, cred, clp->nfsc_nmp, p); 3494 free(dp, M_NFSCLDELEG); 3495 } else 3496 NFSUNLOCKCLSTATE(); 3497 NFSFREECRED(cred); 3498 } 3499 3500 /* 3501 * Do a callback RPC. 3502 */ 3503 void 3504 nfscl_docb(struct nfsrv_descript *nd, NFSPROC_T *p) 3505 { 3506 int clist, gotseq_ok, i, j, k, op, rcalls; 3507 u_int32_t *tl; 3508 struct nfsclclient *clp; 3509 struct nfscldeleg *dp = NULL; 3510 int numops, taglen = -1, error = 0, trunc __unused; 3511 u_int32_t minorvers = 0, retops = 0, *retopsp = NULL, *repp, cbident; 3512 u_char tag[NFSV4_SMALLSTR + 1], *tagstr; 3513 vnode_t vp = NULL; 3514 struct nfsnode *np; 3515 struct vattr va; 3516 struct nfsfh *nfhp; 3517 mount_t mp; 3518 nfsattrbit_t attrbits, rattrbits; 3519 nfsv4stateid_t stateid; 3520 uint32_t seqid, slotid = 0, highslot, cachethis __unused; 3521 uint8_t sessionid[NFSX_V4SESSIONID]; 3522 struct mbuf *rep; 3523 struct nfscllayout *lyp; 3524 uint64_t filesid[2], len, off; 3525 int changed, gotone, laytype, recalltype; 3526 uint32_t iomode; 3527 struct nfsclrecalllayout *recallp = NULL; 3528 struct nfsclsession *tsep; 3529 3530 gotseq_ok = 0; 3531 nfsrvd_rephead(nd); 3532 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED); 3533 taglen = fxdr_unsigned(int, *tl); 3534 if (taglen < 0) { 3535 error = EBADRPC; 3536 goto nfsmout; 3537 } 3538 if (taglen <= NFSV4_SMALLSTR) 3539 tagstr = tag; 3540 else 3541 tagstr = malloc(taglen + 1, M_TEMP, M_WAITOK); 3542 error = nfsrv_mtostr(nd, tagstr, taglen); 3543 if (error) { 3544 if (taglen > NFSV4_SMALLSTR) 3545 free(tagstr, M_TEMP); 3546 taglen = -1; 3547 goto nfsmout; 3548 } 3549 (void) nfsm_strtom(nd, tag, taglen); 3550 if (taglen > NFSV4_SMALLSTR) { 3551 free(tagstr, M_TEMP); 3552 } 3553 NFSM_BUILD(retopsp, u_int32_t *, NFSX_UNSIGNED); 3554 NFSM_DISSECT(tl, u_int32_t *, 3 * NFSX_UNSIGNED); 3555 minorvers = fxdr_unsigned(u_int32_t, *tl++); 3556 if (minorvers != NFSV4_MINORVERSION && 3557 minorvers != NFSV41_MINORVERSION && 3558 minorvers != NFSV42_MINORVERSION) 3559 nd->nd_repstat = NFSERR_MINORVERMISMATCH; 3560 cbident = fxdr_unsigned(u_int32_t, *tl++); 3561 if (nd->nd_repstat) 3562 numops = 0; 3563 else 3564 numops = fxdr_unsigned(int, *tl); 3565 /* 3566 * Loop around doing the sub ops. 3567 */ 3568 for (i = 0; i < numops; i++) { 3569 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED); 3570 NFSM_BUILD(repp, u_int32_t *, 2 * NFSX_UNSIGNED); 3571 *repp++ = *tl; 3572 op = fxdr_unsigned(int, *tl); 3573 if (op < NFSV4OP_CBGETATTR || 3574 (op > NFSV4OP_CBRECALL && minorvers == NFSV4_MINORVERSION) || 3575 (op > NFSV4OP_CBNOTIFYDEVID && 3576 minorvers == NFSV41_MINORVERSION) || 3577 (op > NFSV4OP_CBOFFLOAD && 3578 minorvers == NFSV42_MINORVERSION)) { 3579 nd->nd_repstat = NFSERR_OPILLEGAL; 3580 *repp = nfscl_errmap(nd, minorvers); 3581 retops++; 3582 break; 3583 } 3584 nd->nd_procnum = op; 3585 if (op < NFSV42_CBNOPS) 3586 nfsstatsv1.cbrpccnt[nd->nd_procnum]++; 3587 switch (op) { 3588 case NFSV4OP_CBGETATTR: 3589 NFSCL_DEBUG(4, "cbgetattr\n"); 3590 mp = NULL; 3591 vp = NULL; 3592 error = nfsm_getfh(nd, &nfhp); 3593 if (!error) 3594 error = nfsrv_getattrbits(nd, &attrbits, 3595 NULL, NULL); 3596 if (error == 0 && i == 0 && 3597 minorvers != NFSV4_MINORVERSION) 3598 error = NFSERR_OPNOTINSESS; 3599 if (!error) { 3600 mp = nfscl_getmnt(minorvers, sessionid, cbident, 3601 &clp); 3602 if (mp == NULL) 3603 error = NFSERR_SERVERFAULT; 3604 } 3605 if (!error) { 3606 error = nfscl_ngetreopen(mp, nfhp->nfh_fh, 3607 nfhp->nfh_len, p, &np); 3608 if (!error) 3609 vp = NFSTOV(np); 3610 } 3611 if (!error) { 3612 NFSZERO_ATTRBIT(&rattrbits); 3613 NFSLOCKCLSTATE(); 3614 dp = nfscl_finddeleg(clp, nfhp->nfh_fh, 3615 nfhp->nfh_len); 3616 if (dp != NULL) { 3617 if (NFSISSET_ATTRBIT(&attrbits, 3618 NFSATTRBIT_SIZE)) { 3619 if (vp != NULL) 3620 va.va_size = np->n_size; 3621 else 3622 va.va_size = 3623 dp->nfsdl_size; 3624 NFSSETBIT_ATTRBIT(&rattrbits, 3625 NFSATTRBIT_SIZE); 3626 } 3627 if (NFSISSET_ATTRBIT(&attrbits, 3628 NFSATTRBIT_CHANGE)) { 3629 va.va_filerev = 3630 dp->nfsdl_change; 3631 if (vp == NULL || 3632 (np->n_flag & NDELEGMOD)) 3633 va.va_filerev++; 3634 NFSSETBIT_ATTRBIT(&rattrbits, 3635 NFSATTRBIT_CHANGE); 3636 } 3637 } else 3638 error = NFSERR_SERVERFAULT; 3639 NFSUNLOCKCLSTATE(); 3640 } 3641 if (vp != NULL) 3642 vrele(vp); 3643 if (mp != NULL) 3644 vfs_unbusy(mp); 3645 if (nfhp != NULL) 3646 free(nfhp, M_NFSFH); 3647 if (!error) 3648 (void) nfsv4_fillattr(nd, NULL, NULL, NULL, &va, 3649 NULL, 0, &rattrbits, NULL, p, 0, 0, 0, 0, 3650 (uint64_t)0, NULL); 3651 break; 3652 case NFSV4OP_CBRECALL: 3653 NFSCL_DEBUG(4, "cbrecall\n"); 3654 NFSM_DISSECT(tl, u_int32_t *, NFSX_STATEID + 3655 NFSX_UNSIGNED); 3656 stateid.seqid = *tl++; 3657 NFSBCOPY((caddr_t)tl, (caddr_t)stateid.other, 3658 NFSX_STATEIDOTHER); 3659 tl += (NFSX_STATEIDOTHER / NFSX_UNSIGNED); 3660 trunc = fxdr_unsigned(int, *tl); 3661 error = nfsm_getfh(nd, &nfhp); 3662 if (error == 0 && i == 0 && 3663 minorvers != NFSV4_MINORVERSION) 3664 error = NFSERR_OPNOTINSESS; 3665 if (!error) { 3666 NFSLOCKCLSTATE(); 3667 if (minorvers == NFSV4_MINORVERSION) 3668 clp = nfscl_getclnt(cbident); 3669 else 3670 clp = nfscl_getclntsess(sessionid); 3671 if (clp != NULL) { 3672 dp = nfscl_finddeleg(clp, nfhp->nfh_fh, 3673 nfhp->nfh_len); 3674 if (dp != NULL && (dp->nfsdl_flags & 3675 NFSCLDL_DELEGRET) == 0) { 3676 dp->nfsdl_flags |= 3677 NFSCLDL_RECALL; 3678 wakeup((caddr_t)clp); 3679 } 3680 } else { 3681 error = NFSERR_SERVERFAULT; 3682 } 3683 NFSUNLOCKCLSTATE(); 3684 } 3685 if (nfhp != NULL) 3686 free(nfhp, M_NFSFH); 3687 break; 3688 case NFSV4OP_CBLAYOUTRECALL: 3689 NFSCL_DEBUG(4, "cblayrec\n"); 3690 nfhp = NULL; 3691 NFSM_DISSECT(tl, uint32_t *, 4 * NFSX_UNSIGNED); 3692 laytype = fxdr_unsigned(int, *tl++); 3693 iomode = fxdr_unsigned(uint32_t, *tl++); 3694 if (newnfs_true == *tl++) 3695 changed = 1; 3696 else 3697 changed = 0; 3698 recalltype = fxdr_unsigned(int, *tl); 3699 NFSCL_DEBUG(4, "layt=%d iom=%d ch=%d rectyp=%d\n", 3700 laytype, iomode, changed, recalltype); 3701 recallp = malloc(sizeof(*recallp), M_NFSLAYRECALL, 3702 M_WAITOK); 3703 if (laytype != NFSLAYOUT_NFSV4_1_FILES && 3704 laytype != NFSLAYOUT_FLEXFILE) 3705 error = NFSERR_NOMATCHLAYOUT; 3706 else if (recalltype == NFSLAYOUTRETURN_FILE) { 3707 error = nfsm_getfh(nd, &nfhp); 3708 NFSCL_DEBUG(4, "retfile getfh=%d\n", error); 3709 if (error != 0) 3710 goto nfsmout; 3711 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_HYPER + 3712 NFSX_STATEID); 3713 off = fxdr_hyper(tl); tl += 2; 3714 len = fxdr_hyper(tl); tl += 2; 3715 stateid.seqid = fxdr_unsigned(uint32_t, *tl++); 3716 NFSBCOPY(tl, stateid.other, NFSX_STATEIDOTHER); 3717 if (minorvers == NFSV4_MINORVERSION) 3718 error = NFSERR_NOTSUPP; 3719 else if (i == 0) 3720 error = NFSERR_OPNOTINSESS; 3721 NFSCL_DEBUG(4, "off=%ju len=%ju sq=%u err=%d\n", 3722 (uintmax_t)off, (uintmax_t)len, 3723 stateid.seqid, error); 3724 if (error == 0) { 3725 NFSLOCKCLSTATE(); 3726 clp = nfscl_getclntsess(sessionid); 3727 NFSCL_DEBUG(4, "cbly clp=%p\n", clp); 3728 if (clp != NULL) { 3729 lyp = nfscl_findlayout(clp, 3730 nfhp->nfh_fh, 3731 nfhp->nfh_len); 3732 NFSCL_DEBUG(4, "cblyp=%p\n", 3733 lyp); 3734 if (lyp != NULL && 3735 (lyp->nfsly_flags & 3736 (NFSLY_FILES | 3737 NFSLY_FLEXFILE)) != 0 && 3738 !NFSBCMP(stateid.other, 3739 lyp->nfsly_stateid.other, 3740 NFSX_STATEIDOTHER)) { 3741 error = 3742 nfscl_layoutrecall( 3743 recalltype, 3744 lyp, iomode, off, 3745 len, stateid.seqid, 3746 0, 0, NULL, 3747 recallp); 3748 if (error == 0 && 3749 stateid.seqid > 3750 lyp->nfsly_stateid.seqid) 3751 lyp->nfsly_stateid.seqid = 3752 stateid.seqid; 3753 recallp = NULL; 3754 wakeup(clp); 3755 NFSCL_DEBUG(4, 3756 "aft layrcal=%d " 3757 "layseqid=%d\n", 3758 error, 3759 lyp->nfsly_stateid.seqid); 3760 } else 3761 error = 3762 NFSERR_NOMATCHLAYOUT; 3763 } else 3764 error = NFSERR_NOMATCHLAYOUT; 3765 NFSUNLOCKCLSTATE(); 3766 } 3767 free(nfhp, M_NFSFH); 3768 } else if (recalltype == NFSLAYOUTRETURN_FSID) { 3769 NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_HYPER); 3770 filesid[0] = fxdr_hyper(tl); tl += 2; 3771 filesid[1] = fxdr_hyper(tl); tl += 2; 3772 gotone = 0; 3773 NFSLOCKCLSTATE(); 3774 clp = nfscl_getclntsess(sessionid); 3775 if (clp != NULL) { 3776 TAILQ_FOREACH(lyp, &clp->nfsc_layout, 3777 nfsly_list) { 3778 if (lyp->nfsly_filesid[0] == 3779 filesid[0] && 3780 lyp->nfsly_filesid[1] == 3781 filesid[1]) { 3782 error = 3783 nfscl_layoutrecall( 3784 recalltype, 3785 lyp, iomode, 0, 3786 UINT64_MAX, 3787 lyp->nfsly_stateid.seqid, 3788 0, 0, NULL, 3789 recallp); 3790 recallp = NULL; 3791 gotone = 1; 3792 } 3793 } 3794 if (gotone != 0) 3795 wakeup(clp); 3796 else 3797 error = NFSERR_NOMATCHLAYOUT; 3798 } else 3799 error = NFSERR_NOMATCHLAYOUT; 3800 NFSUNLOCKCLSTATE(); 3801 } else if (recalltype == NFSLAYOUTRETURN_ALL) { 3802 gotone = 0; 3803 NFSLOCKCLSTATE(); 3804 clp = nfscl_getclntsess(sessionid); 3805 if (clp != NULL) { 3806 TAILQ_FOREACH(lyp, &clp->nfsc_layout, 3807 nfsly_list) { 3808 error = nfscl_layoutrecall( 3809 recalltype, lyp, iomode, 0, 3810 UINT64_MAX, 3811 lyp->nfsly_stateid.seqid, 3812 0, 0, NULL, recallp); 3813 recallp = NULL; 3814 gotone = 1; 3815 } 3816 if (gotone != 0) 3817 wakeup(clp); 3818 else 3819 error = NFSERR_NOMATCHLAYOUT; 3820 } else 3821 error = NFSERR_NOMATCHLAYOUT; 3822 NFSUNLOCKCLSTATE(); 3823 } else 3824 error = NFSERR_NOMATCHLAYOUT; 3825 if (recallp != NULL) { 3826 free(recallp, M_NFSLAYRECALL); 3827 recallp = NULL; 3828 } 3829 break; 3830 case NFSV4OP_CBSEQUENCE: 3831 NFSM_DISSECT(tl, uint32_t *, NFSX_V4SESSIONID + 3832 5 * NFSX_UNSIGNED); 3833 bcopy(tl, sessionid, NFSX_V4SESSIONID); 3834 tl += NFSX_V4SESSIONID / NFSX_UNSIGNED; 3835 seqid = fxdr_unsigned(uint32_t, *tl++); 3836 slotid = fxdr_unsigned(uint32_t, *tl++); 3837 highslot = fxdr_unsigned(uint32_t, *tl++); 3838 cachethis = *tl++; 3839 /* Throw away the referring call stuff. */ 3840 clist = fxdr_unsigned(int, *tl); 3841 for (j = 0; j < clist; j++) { 3842 NFSM_DISSECT(tl, uint32_t *, NFSX_V4SESSIONID + 3843 NFSX_UNSIGNED); 3844 tl += NFSX_V4SESSIONID / NFSX_UNSIGNED; 3845 rcalls = fxdr_unsigned(int, *tl); 3846 for (k = 0; k < rcalls; k++) { 3847 NFSM_DISSECT(tl, uint32_t *, 3848 2 * NFSX_UNSIGNED); 3849 } 3850 } 3851 NFSLOCKCLSTATE(); 3852 if (i == 0) { 3853 clp = nfscl_getclntsess(sessionid); 3854 if (clp == NULL) 3855 error = NFSERR_SERVERFAULT; 3856 } else 3857 error = NFSERR_SEQUENCEPOS; 3858 if (error == 0) { 3859 tsep = nfsmnt_mdssession(clp->nfsc_nmp); 3860 error = nfsv4_seqsession(seqid, slotid, 3861 highslot, tsep->nfsess_cbslots, &rep, 3862 tsep->nfsess_backslots); 3863 } 3864 NFSUNLOCKCLSTATE(); 3865 if (error == 0 || error == NFSERR_REPLYFROMCACHE) { 3866 gotseq_ok = 1; 3867 if (rep != NULL) { 3868 /* 3869 * Handle a reply for a retried 3870 * callback. The reply will be 3871 * re-inserted in the session cache 3872 * by the nfsv4_seqsess_cacherep() call 3873 * after out: 3874 */ 3875 KASSERT(error == NFSERR_REPLYFROMCACHE, 3876 ("cbsequence: non-NULL rep")); 3877 NFSCL_DEBUG(4, "Got cbretry\n"); 3878 m_freem(nd->nd_mreq); 3879 nd->nd_mreq = rep; 3880 rep = NULL; 3881 goto out; 3882 } 3883 NFSM_BUILD(tl, uint32_t *, 3884 NFSX_V4SESSIONID + 4 * NFSX_UNSIGNED); 3885 bcopy(sessionid, tl, NFSX_V4SESSIONID); 3886 tl += NFSX_V4SESSIONID / NFSX_UNSIGNED; 3887 *tl++ = txdr_unsigned(seqid); 3888 *tl++ = txdr_unsigned(slotid); 3889 *tl++ = txdr_unsigned(NFSV4_CBSLOTS - 1); 3890 *tl = txdr_unsigned(NFSV4_CBSLOTS - 1); 3891 } 3892 break; 3893 default: 3894 if (i == 0 && minorvers != NFSV4_MINORVERSION) 3895 error = NFSERR_OPNOTINSESS; 3896 else { 3897 NFSCL_DEBUG(1, "unsupp callback %d\n", op); 3898 error = NFSERR_NOTSUPP; 3899 } 3900 break; 3901 } 3902 if (error) { 3903 if (error == EBADRPC || error == NFSERR_BADXDR) { 3904 nd->nd_repstat = NFSERR_BADXDR; 3905 } else { 3906 nd->nd_repstat = error; 3907 } 3908 error = 0; 3909 } 3910 retops++; 3911 if (nd->nd_repstat) { 3912 *repp = nfscl_errmap(nd, minorvers); 3913 break; 3914 } else 3915 *repp = 0; /* NFS4_OK */ 3916 } 3917 nfsmout: 3918 if (recallp != NULL) 3919 free(recallp, M_NFSLAYRECALL); 3920 if (error) { 3921 if (error == EBADRPC || error == NFSERR_BADXDR) 3922 nd->nd_repstat = NFSERR_BADXDR; 3923 else 3924 printf("nfsv4 comperr1=%d\n", error); 3925 } 3926 if (taglen == -1) { 3927 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED); 3928 *tl++ = 0; 3929 *tl = 0; 3930 } else { 3931 *retopsp = txdr_unsigned(retops); 3932 } 3933 *nd->nd_errp = nfscl_errmap(nd, minorvers); 3934 out: 3935 if (gotseq_ok != 0) { 3936 rep = m_copym(nd->nd_mreq, 0, M_COPYALL, M_WAITOK); 3937 NFSLOCKCLSTATE(); 3938 clp = nfscl_getclntsess(sessionid); 3939 if (clp != NULL) { 3940 tsep = nfsmnt_mdssession(clp->nfsc_nmp); 3941 nfsv4_seqsess_cacherep(slotid, tsep->nfsess_cbslots, 3942 NFSERR_OK, &rep); 3943 NFSUNLOCKCLSTATE(); 3944 } else { 3945 NFSUNLOCKCLSTATE(); 3946 m_freem(rep); 3947 } 3948 } 3949 } 3950 3951 /* 3952 * Generate the next cbident value. Basically just increment a static value 3953 * and then check that it isn't already in the list, if it has wrapped around. 3954 */ 3955 static u_int32_t 3956 nfscl_nextcbident(void) 3957 { 3958 struct nfsclclient *clp; 3959 int matched; 3960 static u_int32_t nextcbident = 0; 3961 static int haswrapped = 0; 3962 3963 nextcbident++; 3964 if (nextcbident == 0) 3965 haswrapped = 1; 3966 if (haswrapped) { 3967 /* 3968 * Search the clientid list for one already using this cbident. 3969 */ 3970 do { 3971 matched = 0; 3972 NFSLOCKCLSTATE(); 3973 LIST_FOREACH(clp, &nfsclhead, nfsc_list) { 3974 if (clp->nfsc_cbident == nextcbident) { 3975 matched = 1; 3976 break; 3977 } 3978 } 3979 NFSUNLOCKCLSTATE(); 3980 if (matched == 1) 3981 nextcbident++; 3982 } while (matched); 3983 } 3984 return (nextcbident); 3985 } 3986 3987 /* 3988 * Get the mount point related to a given cbident or session and busy it. 3989 */ 3990 static mount_t 3991 nfscl_getmnt(int minorvers, uint8_t *sessionid, u_int32_t cbident, 3992 struct nfsclclient **clpp) 3993 { 3994 struct nfsclclient *clp; 3995 mount_t mp; 3996 int error; 3997 struct nfsclsession *tsep; 3998 3999 *clpp = NULL; 4000 NFSLOCKCLSTATE(); 4001 LIST_FOREACH(clp, &nfsclhead, nfsc_list) { 4002 tsep = nfsmnt_mdssession(clp->nfsc_nmp); 4003 if (minorvers == NFSV4_MINORVERSION) { 4004 if (clp->nfsc_cbident == cbident) 4005 break; 4006 } else if (!NFSBCMP(tsep->nfsess_sessionid, sessionid, 4007 NFSX_V4SESSIONID)) 4008 break; 4009 } 4010 if (clp == NULL) { 4011 NFSUNLOCKCLSTATE(); 4012 return (NULL); 4013 } 4014 mp = clp->nfsc_nmp->nm_mountp; 4015 vfs_ref(mp); 4016 NFSUNLOCKCLSTATE(); 4017 error = vfs_busy(mp, 0); 4018 vfs_rel(mp); 4019 if (error != 0) 4020 return (NULL); 4021 *clpp = clp; 4022 return (mp); 4023 } 4024 4025 /* 4026 * Get the clientid pointer related to a given cbident. 4027 */ 4028 static struct nfsclclient * 4029 nfscl_getclnt(u_int32_t cbident) 4030 { 4031 struct nfsclclient *clp; 4032 4033 LIST_FOREACH(clp, &nfsclhead, nfsc_list) 4034 if (clp->nfsc_cbident == cbident) 4035 break; 4036 return (clp); 4037 } 4038 4039 /* 4040 * Get the clientid pointer related to a given sessionid. 4041 */ 4042 static struct nfsclclient * 4043 nfscl_getclntsess(uint8_t *sessionid) 4044 { 4045 struct nfsclclient *clp; 4046 struct nfsclsession *tsep; 4047 4048 LIST_FOREACH(clp, &nfsclhead, nfsc_list) { 4049 tsep = nfsmnt_mdssession(clp->nfsc_nmp); 4050 if (!NFSBCMP(tsep->nfsess_sessionid, sessionid, 4051 NFSX_V4SESSIONID)) 4052 break; 4053 } 4054 return (clp); 4055 } 4056 4057 /* 4058 * Search for a lock conflict locally on the client. A conflict occurs if 4059 * - not same owner and overlapping byte range and at least one of them is 4060 * a write lock or this is an unlock. 4061 */ 4062 static int 4063 nfscl_localconflict(struct nfsclclient *clp, u_int8_t *fhp, int fhlen, 4064 struct nfscllock *nlop, u_int8_t *own, struct nfscldeleg *dp, 4065 struct nfscllock **lopp) 4066 { 4067 struct nfsclopen *op; 4068 int ret; 4069 4070 if (dp != NULL) { 4071 ret = nfscl_checkconflict(&dp->nfsdl_lock, nlop, own, lopp); 4072 if (ret) 4073 return (ret); 4074 } 4075 LIST_FOREACH(op, NFSCLOPENHASH(clp, fhp, fhlen), nfso_hash) { 4076 if (op->nfso_fhlen == fhlen && 4077 !NFSBCMP(op->nfso_fh, fhp, fhlen)) { 4078 ret = nfscl_checkconflict(&op->nfso_lock, nlop, 4079 own, lopp); 4080 if (ret) 4081 return (ret); 4082 } 4083 } 4084 return (0); 4085 } 4086 4087 static int 4088 nfscl_checkconflict(struct nfscllockownerhead *lhp, struct nfscllock *nlop, 4089 u_int8_t *own, struct nfscllock **lopp) 4090 { 4091 struct nfscllockowner *lp; 4092 struct nfscllock *lop; 4093 4094 LIST_FOREACH(lp, lhp, nfsl_list) { 4095 if (NFSBCMP(lp->nfsl_owner, own, NFSV4CL_LOCKNAMELEN)) { 4096 LIST_FOREACH(lop, &lp->nfsl_lock, nfslo_list) { 4097 if (lop->nfslo_first >= nlop->nfslo_end) 4098 break; 4099 if (lop->nfslo_end <= nlop->nfslo_first) 4100 continue; 4101 if (lop->nfslo_type == F_WRLCK || 4102 nlop->nfslo_type == F_WRLCK || 4103 nlop->nfslo_type == F_UNLCK) { 4104 if (lopp != NULL) 4105 *lopp = lop; 4106 return (NFSERR_DENIED); 4107 } 4108 } 4109 } 4110 } 4111 return (0); 4112 } 4113 4114 /* 4115 * Check for a local conflicting lock. 4116 */ 4117 int 4118 nfscl_lockt(vnode_t vp, struct nfsclclient *clp, u_int64_t off, 4119 u_int64_t len, struct flock *fl, NFSPROC_T *p, void *id, int flags) 4120 { 4121 struct nfscllock *lop, nlck; 4122 struct nfscldeleg *dp; 4123 struct nfsnode *np; 4124 u_int8_t own[NFSV4CL_LOCKNAMELEN]; 4125 int error; 4126 4127 nlck.nfslo_type = fl->l_type; 4128 nlck.nfslo_first = off; 4129 if (len == NFS64BITSSET) { 4130 nlck.nfslo_end = NFS64BITSSET; 4131 } else { 4132 nlck.nfslo_end = off + len; 4133 if (nlck.nfslo_end <= nlck.nfslo_first) 4134 return (NFSERR_INVAL); 4135 } 4136 np = VTONFS(vp); 4137 nfscl_filllockowner(id, own, flags); 4138 NFSLOCKCLSTATE(); 4139 dp = nfscl_finddeleg(clp, np->n_fhp->nfh_fh, np->n_fhp->nfh_len); 4140 error = nfscl_localconflict(clp, np->n_fhp->nfh_fh, np->n_fhp->nfh_len, 4141 &nlck, own, dp, &lop); 4142 if (error != 0) { 4143 fl->l_whence = SEEK_SET; 4144 fl->l_start = lop->nfslo_first; 4145 if (lop->nfslo_end == NFS64BITSSET) 4146 fl->l_len = 0; 4147 else 4148 fl->l_len = lop->nfslo_end - lop->nfslo_first; 4149 fl->l_pid = (pid_t)0; 4150 fl->l_type = lop->nfslo_type; 4151 error = -1; /* no RPC required */ 4152 } else if (dp != NULL && ((dp->nfsdl_flags & NFSCLDL_WRITE) || 4153 fl->l_type == F_RDLCK)) { 4154 /* 4155 * The delegation ensures that there isn't a conflicting 4156 * lock on the server, so return -1 to indicate an RPC 4157 * isn't required. 4158 */ 4159 fl->l_type = F_UNLCK; 4160 error = -1; 4161 } 4162 NFSUNLOCKCLSTATE(); 4163 return (error); 4164 } 4165 4166 /* 4167 * Handle Recall of a delegation. 4168 * The clp must be exclusive locked when this is called. 4169 */ 4170 static int 4171 nfscl_recalldeleg(struct nfsclclient *clp, struct nfsmount *nmp, 4172 struct nfscldeleg *dp, vnode_t vp, struct ucred *cred, NFSPROC_T *p, 4173 int called_from_renewthread, vnode_t *vpp) 4174 { 4175 struct nfsclowner *owp, *lowp, *nowp; 4176 struct nfsclopen *op, *lop; 4177 struct nfscllockowner *lp; 4178 struct nfscllock *lckp; 4179 struct nfsnode *np; 4180 int error = 0, ret; 4181 4182 if (vp == NULL) { 4183 KASSERT(vpp != NULL, ("nfscl_recalldeleg: vpp NULL")); 4184 *vpp = NULL; 4185 /* 4186 * First, get a vnode for the file. This is needed to do RPCs. 4187 */ 4188 ret = nfscl_ngetreopen(nmp->nm_mountp, dp->nfsdl_fh, 4189 dp->nfsdl_fhlen, p, &np); 4190 if (ret) { 4191 /* 4192 * File isn't open, so nothing to move over to the 4193 * server. 4194 */ 4195 return (0); 4196 } 4197 vp = NFSTOV(np); 4198 *vpp = vp; 4199 } else { 4200 np = VTONFS(vp); 4201 } 4202 dp->nfsdl_flags &= ~NFSCLDL_MODTIMESET; 4203 4204 /* 4205 * Ok, if it's a write delegation, flush data to the server, so 4206 * that close/open consistency is retained. 4207 */ 4208 ret = 0; 4209 NFSLOCKNODE(np); 4210 if ((dp->nfsdl_flags & NFSCLDL_WRITE) && (np->n_flag & NMODIFIED)) { 4211 np->n_flag |= NDELEGRECALL; 4212 NFSUNLOCKNODE(np); 4213 ret = ncl_flush(vp, MNT_WAIT, p, 1, called_from_renewthread); 4214 NFSLOCKNODE(np); 4215 np->n_flag &= ~NDELEGRECALL; 4216 } 4217 NFSINVALATTRCACHE(np); 4218 NFSUNLOCKNODE(np); 4219 if (ret == EIO && called_from_renewthread != 0) { 4220 /* 4221 * If the flush failed with EIO for the renew thread, 4222 * return now, so that the dirty buffer will be flushed 4223 * later. 4224 */ 4225 return (ret); 4226 } 4227 4228 /* 4229 * Now, for each openowner with opens issued locally, move them 4230 * over to state against the server. 4231 */ 4232 LIST_FOREACH(lowp, &dp->nfsdl_owner, nfsow_list) { 4233 lop = LIST_FIRST(&lowp->nfsow_open); 4234 if (lop != NULL) { 4235 if (LIST_NEXT(lop, nfso_list) != NULL) 4236 panic("nfsdlg mult opens"); 4237 /* 4238 * Look for the same openowner against the server. 4239 */ 4240 LIST_FOREACH(owp, &clp->nfsc_owner, nfsow_list) { 4241 if (!NFSBCMP(lowp->nfsow_owner, 4242 owp->nfsow_owner, NFSV4CL_LOCKNAMELEN)) { 4243 newnfs_copycred(&dp->nfsdl_cred, cred); 4244 ret = nfscl_moveopen(vp, clp, nmp, lop, 4245 owp, dp, cred, p); 4246 if (ret == NFSERR_STALECLIENTID || 4247 ret == NFSERR_STALEDONTRECOVER || 4248 ret == NFSERR_BADSESSION) 4249 return (ret); 4250 if (ret) { 4251 nfscl_freeopen(lop, 1, true); 4252 if (!error) 4253 error = ret; 4254 } 4255 break; 4256 } 4257 } 4258 4259 /* 4260 * If no openowner found, create one and get an open 4261 * for it. 4262 */ 4263 if (owp == NULL) { 4264 nowp = malloc( 4265 sizeof (struct nfsclowner), M_NFSCLOWNER, 4266 M_WAITOK); 4267 nfscl_newopen(clp, NULL, &owp, &nowp, &op, 4268 NULL, lowp->nfsow_owner, dp->nfsdl_fh, 4269 dp->nfsdl_fhlen, NULL, NULL); 4270 newnfs_copycred(&dp->nfsdl_cred, cred); 4271 ret = nfscl_moveopen(vp, clp, nmp, lop, 4272 owp, dp, cred, p); 4273 if (ret) { 4274 nfscl_freeopenowner(owp, 0); 4275 if (ret == NFSERR_STALECLIENTID || 4276 ret == NFSERR_STALEDONTRECOVER || 4277 ret == NFSERR_BADSESSION) 4278 return (ret); 4279 if (ret) { 4280 nfscl_freeopen(lop, 1, true); 4281 if (!error) 4282 error = ret; 4283 } 4284 } 4285 } 4286 } 4287 } 4288 4289 /* 4290 * Now, get byte range locks for any locks done locally. 4291 */ 4292 LIST_FOREACH(lp, &dp->nfsdl_lock, nfsl_list) { 4293 LIST_FOREACH(lckp, &lp->nfsl_lock, nfslo_list) { 4294 newnfs_copycred(&dp->nfsdl_cred, cred); 4295 ret = nfscl_relock(vp, clp, nmp, lp, lckp, cred, p); 4296 if (ret == NFSERR_STALESTATEID || 4297 ret == NFSERR_STALEDONTRECOVER || 4298 ret == NFSERR_STALECLIENTID || 4299 ret == NFSERR_BADSESSION) 4300 return (ret); 4301 if (ret && !error) 4302 error = ret; 4303 } 4304 } 4305 return (error); 4306 } 4307 4308 /* 4309 * Move a locally issued open over to an owner on the state list. 4310 * SIDE EFFECT: If it needs to sleep (do an rpc), it unlocks clstate and 4311 * returns with it unlocked. 4312 */ 4313 static int 4314 nfscl_moveopen(vnode_t vp, struct nfsclclient *clp, struct nfsmount *nmp, 4315 struct nfsclopen *lop, struct nfsclowner *owp, struct nfscldeleg *dp, 4316 struct ucred *cred, NFSPROC_T *p) 4317 { 4318 struct nfsclopen *op, *nop; 4319 struct nfscldeleg *ndp; 4320 struct nfsnode *np; 4321 int error = 0, newone; 4322 4323 /* 4324 * First, look for an appropriate open, If found, just increment the 4325 * opencnt in it. 4326 */ 4327 LIST_FOREACH(op, &owp->nfsow_open, nfso_list) { 4328 if ((op->nfso_mode & lop->nfso_mode) == lop->nfso_mode && 4329 op->nfso_fhlen == lop->nfso_fhlen && 4330 !NFSBCMP(op->nfso_fh, lop->nfso_fh, op->nfso_fhlen)) { 4331 op->nfso_opencnt += lop->nfso_opencnt; 4332 nfscl_freeopen(lop, 1, true); 4333 return (0); 4334 } 4335 } 4336 4337 /* No appropriate open, so we have to do one against the server. */ 4338 np = VTONFS(vp); 4339 nop = malloc(sizeof (struct nfsclopen) + 4340 lop->nfso_fhlen - 1, M_NFSCLOPEN, M_WAITOK); 4341 nop->nfso_hash.le_prev = NULL; 4342 newone = 0; 4343 nfscl_newopen(clp, NULL, &owp, NULL, &op, &nop, owp->nfsow_owner, 4344 lop->nfso_fh, lop->nfso_fhlen, cred, &newone); 4345 ndp = dp; 4346 error = nfscl_tryopen(nmp, vp, np->n_v4->n4_data, np->n_v4->n4_fhlen, 4347 lop->nfso_fh, lop->nfso_fhlen, lop->nfso_mode, op, 4348 NFS4NODENAME(np->n_v4), np->n_v4->n4_namelen, &ndp, 0, 0, cred, p); 4349 if (error) { 4350 if (newone) 4351 nfscl_freeopen(op, 0, true); 4352 } else { 4353 op->nfso_mode |= lop->nfso_mode; 4354 op->nfso_opencnt += lop->nfso_opencnt; 4355 nfscl_freeopen(lop, 1, true); 4356 } 4357 if (nop != NULL) 4358 free(nop, M_NFSCLOPEN); 4359 if (ndp != NULL) { 4360 /* 4361 * What should I do with the returned delegation, since the 4362 * delegation is being recalled? For now, just printf and 4363 * through it away. 4364 */ 4365 printf("Moveopen returned deleg\n"); 4366 free(ndp, M_NFSCLDELEG); 4367 } 4368 return (error); 4369 } 4370 4371 /* 4372 * Recall all delegations on this client. 4373 */ 4374 static void 4375 nfscl_totalrecall(struct nfsclclient *clp) 4376 { 4377 struct nfscldeleg *dp; 4378 4379 TAILQ_FOREACH(dp, &clp->nfsc_deleg, nfsdl_list) { 4380 if ((dp->nfsdl_flags & NFSCLDL_DELEGRET) == 0) 4381 dp->nfsdl_flags |= NFSCLDL_RECALL; 4382 } 4383 } 4384 4385 /* 4386 * Relock byte ranges. Called for delegation recall and state expiry. 4387 */ 4388 static int 4389 nfscl_relock(vnode_t vp, struct nfsclclient *clp, struct nfsmount *nmp, 4390 struct nfscllockowner *lp, struct nfscllock *lop, struct ucred *cred, 4391 NFSPROC_T *p) 4392 { 4393 struct nfscllockowner *nlp; 4394 struct nfsfh *nfhp; 4395 struct nfsnode *np; 4396 u_int64_t off, len; 4397 int error, newone, donelocally; 4398 4399 if (NFSHASNFSV4N(nmp) && NFSHASONEOPENOWN(nmp)) { 4400 np = VTONFS(vp); 4401 NFSLOCKNODE(np); 4402 np->n_flag |= NMIGHTBELOCKED; 4403 NFSUNLOCKNODE(np); 4404 } 4405 4406 off = lop->nfslo_first; 4407 len = lop->nfslo_end - lop->nfslo_first; 4408 error = nfscl_getbytelock(vp, off, len, lop->nfslo_type, cred, p, 4409 clp, 1, NULL, lp->nfsl_lockflags, lp->nfsl_owner, 4410 lp->nfsl_openowner, &nlp, &newone, &donelocally); 4411 if (error || donelocally) 4412 return (error); 4413 nfhp = VTONFS(vp)->n_fhp; 4414 error = nfscl_trylock(nmp, vp, nfhp->nfh_fh, 4415 nfhp->nfh_len, nlp, newone, 0, off, 4416 len, lop->nfslo_type, cred, p); 4417 if (error) 4418 nfscl_freelockowner(nlp, 0); 4419 return (error); 4420 } 4421 4422 /* 4423 * Called to re-open a file. Basically get a vnode for the file handle 4424 * and then call nfsrpc_openrpc() to do the rest. 4425 */ 4426 static int 4427 nfsrpc_reopen(struct nfsmount *nmp, u_int8_t *fhp, int fhlen, 4428 u_int32_t mode, struct nfsclopen *op, struct nfscldeleg **dpp, 4429 struct ucred *cred, NFSPROC_T *p) 4430 { 4431 struct nfsnode *np; 4432 vnode_t vp; 4433 int error; 4434 4435 error = nfscl_ngetreopen(nmp->nm_mountp, fhp, fhlen, p, &np); 4436 if (error) 4437 return (error); 4438 vp = NFSTOV(np); 4439 if (np->n_v4 != NULL) { 4440 error = nfscl_tryopen(nmp, vp, np->n_v4->n4_data, 4441 np->n_v4->n4_fhlen, fhp, fhlen, mode, op, 4442 NFS4NODENAME(np->n_v4), np->n_v4->n4_namelen, dpp, 0, 0, 4443 cred, p); 4444 } else { 4445 error = EINVAL; 4446 } 4447 vrele(vp); 4448 return (error); 4449 } 4450 4451 /* 4452 * Try an open against the server. Just call nfsrpc_openrpc(), retrying while 4453 * NFSERR_DELAY. Also, try system credentials, if the passed in credentials 4454 * fail. 4455 */ 4456 static int 4457 nfscl_tryopen(struct nfsmount *nmp, vnode_t vp, u_int8_t *fhp, int fhlen, 4458 u_int8_t *newfhp, int newfhlen, u_int32_t mode, struct nfsclopen *op, 4459 u_int8_t *name, int namelen, struct nfscldeleg **ndpp, 4460 int reclaim, u_int32_t delegtype, struct ucred *cred, NFSPROC_T *p) 4461 { 4462 int error; 4463 4464 do { 4465 error = nfsrpc_openrpc(nmp, vp, fhp, fhlen, newfhp, newfhlen, 4466 mode, op, name, namelen, ndpp, reclaim, delegtype, cred, p, 4467 0, 0); 4468 if (error == NFSERR_DELAY) 4469 (void) nfs_catnap(PZERO, error, "nfstryop"); 4470 } while (error == NFSERR_DELAY); 4471 if (error == EAUTH || error == EACCES) { 4472 /* Try again using system credentials */ 4473 newnfs_setroot(cred); 4474 do { 4475 error = nfsrpc_openrpc(nmp, vp, fhp, fhlen, newfhp, 4476 newfhlen, mode, op, name, namelen, ndpp, reclaim, 4477 delegtype, cred, p, 1, 0); 4478 if (error == NFSERR_DELAY) 4479 (void) nfs_catnap(PZERO, error, "nfstryop"); 4480 } while (error == NFSERR_DELAY); 4481 } 4482 return (error); 4483 } 4484 4485 /* 4486 * Try a byte range lock. Just loop on nfsrpc_lock() while it returns 4487 * NFSERR_DELAY. Also, retry with system credentials, if the provided 4488 * cred don't work. 4489 */ 4490 static int 4491 nfscl_trylock(struct nfsmount *nmp, vnode_t vp, u_int8_t *fhp, 4492 int fhlen, struct nfscllockowner *nlp, int newone, int reclaim, 4493 u_int64_t off, u_int64_t len, short type, struct ucred *cred, NFSPROC_T *p) 4494 { 4495 struct nfsrv_descript nfsd, *nd = &nfsd; 4496 int error; 4497 4498 do { 4499 error = nfsrpc_lock(nd, nmp, vp, fhp, fhlen, nlp, newone, 4500 reclaim, off, len, type, cred, p, 0); 4501 if (!error && nd->nd_repstat == NFSERR_DELAY) 4502 (void) nfs_catnap(PZERO, (int)nd->nd_repstat, 4503 "nfstrylck"); 4504 } while (!error && nd->nd_repstat == NFSERR_DELAY); 4505 if (!error) 4506 error = nd->nd_repstat; 4507 if (error == EAUTH || error == EACCES) { 4508 /* Try again using root credentials */ 4509 newnfs_setroot(cred); 4510 do { 4511 error = nfsrpc_lock(nd, nmp, vp, fhp, fhlen, nlp, 4512 newone, reclaim, off, len, type, cred, p, 1); 4513 if (!error && nd->nd_repstat == NFSERR_DELAY) 4514 (void) nfs_catnap(PZERO, (int)nd->nd_repstat, 4515 "nfstrylck"); 4516 } while (!error && nd->nd_repstat == NFSERR_DELAY); 4517 if (!error) 4518 error = nd->nd_repstat; 4519 } 4520 return (error); 4521 } 4522 4523 /* 4524 * Try a delegreturn against the server. Just call nfsrpc_delegreturn(), 4525 * retrying while NFSERR_DELAY. Also, try system credentials, if the passed in 4526 * credentials fail. 4527 */ 4528 int 4529 nfscl_trydelegreturn(struct nfscldeleg *dp, struct ucred *cred, 4530 struct nfsmount *nmp, NFSPROC_T *p) 4531 { 4532 int error; 4533 4534 do { 4535 error = nfsrpc_delegreturn(dp, cred, nmp, p, 0); 4536 if (error == NFSERR_DELAY) 4537 (void) nfs_catnap(PZERO, error, "nfstrydp"); 4538 } while (error == NFSERR_DELAY); 4539 if (error == EAUTH || error == EACCES) { 4540 /* Try again using system credentials */ 4541 newnfs_setroot(cred); 4542 do { 4543 error = nfsrpc_delegreturn(dp, cred, nmp, p, 1); 4544 if (error == NFSERR_DELAY) 4545 (void) nfs_catnap(PZERO, error, "nfstrydp"); 4546 } while (error == NFSERR_DELAY); 4547 } 4548 return (error); 4549 } 4550 4551 /* 4552 * Try a close against the server. Just call nfsrpc_closerpc(), 4553 * retrying while NFSERR_DELAY. Also, try system credentials, if the passed in 4554 * credentials fail. 4555 */ 4556 int 4557 nfscl_tryclose(struct nfsclopen *op, struct ucred *cred, 4558 struct nfsmount *nmp, NFSPROC_T *p, bool loop_on_delayed) 4559 { 4560 struct nfsrv_descript nfsd, *nd = &nfsd; 4561 int error; 4562 4563 do { 4564 error = nfsrpc_closerpc(nd, nmp, op, cred, p, 0); 4565 if (loop_on_delayed && error == NFSERR_DELAY) 4566 (void) nfs_catnap(PZERO, error, "nfstrycl"); 4567 } while (loop_on_delayed && error == NFSERR_DELAY); 4568 if (error == EAUTH || error == EACCES) { 4569 /* Try again using system credentials */ 4570 newnfs_setroot(cred); 4571 do { 4572 error = nfsrpc_closerpc(nd, nmp, op, cred, p, 1); 4573 if (loop_on_delayed && error == NFSERR_DELAY) 4574 (void) nfs_catnap(PZERO, error, "nfstrycl"); 4575 } while (loop_on_delayed && error == NFSERR_DELAY); 4576 } 4577 return (error); 4578 } 4579 4580 /* 4581 * Decide if a delegation on a file permits close without flushing writes 4582 * to the server. This might be a big performance win in some environments. 4583 * (Not useful until the client does caching on local stable storage.) 4584 */ 4585 int 4586 nfscl_mustflush(vnode_t vp) 4587 { 4588 struct nfsclclient *clp; 4589 struct nfscldeleg *dp; 4590 struct nfsnode *np; 4591 struct nfsmount *nmp; 4592 4593 np = VTONFS(vp); 4594 nmp = VFSTONFS(vp->v_mount); 4595 if (!NFSHASNFSV4(nmp)) 4596 return (1); 4597 NFSLOCKMNT(nmp); 4598 if ((nmp->nm_privflag & NFSMNTP_DELEGISSUED) == 0) { 4599 NFSUNLOCKMNT(nmp); 4600 return (1); 4601 } 4602 NFSUNLOCKMNT(nmp); 4603 NFSLOCKCLSTATE(); 4604 clp = nfscl_findcl(nmp); 4605 if (clp == NULL) { 4606 NFSUNLOCKCLSTATE(); 4607 return (1); 4608 } 4609 dp = nfscl_finddeleg(clp, np->n_fhp->nfh_fh, np->n_fhp->nfh_len); 4610 if (dp != NULL && (dp->nfsdl_flags & 4611 (NFSCLDL_WRITE | NFSCLDL_RECALL | NFSCLDL_DELEGRET)) == 4612 NFSCLDL_WRITE && 4613 (dp->nfsdl_sizelimit >= np->n_size || 4614 !NFSHASSTRICT3530(nmp))) { 4615 NFSUNLOCKCLSTATE(); 4616 return (0); 4617 } 4618 NFSUNLOCKCLSTATE(); 4619 return (1); 4620 } 4621 4622 /* 4623 * See if a (write) delegation exists for this file. 4624 */ 4625 int 4626 nfscl_nodeleg(vnode_t vp, int writedeleg) 4627 { 4628 struct nfsclclient *clp; 4629 struct nfscldeleg *dp; 4630 struct nfsnode *np; 4631 struct nfsmount *nmp; 4632 4633 np = VTONFS(vp); 4634 nmp = VFSTONFS(vp->v_mount); 4635 if (!NFSHASNFSV4(nmp)) 4636 return (1); 4637 NFSLOCKMNT(nmp); 4638 if ((nmp->nm_privflag & NFSMNTP_DELEGISSUED) == 0) { 4639 NFSUNLOCKMNT(nmp); 4640 return (1); 4641 } 4642 NFSUNLOCKMNT(nmp); 4643 NFSLOCKCLSTATE(); 4644 clp = nfscl_findcl(nmp); 4645 if (clp == NULL) { 4646 NFSUNLOCKCLSTATE(); 4647 return (1); 4648 } 4649 dp = nfscl_finddeleg(clp, np->n_fhp->nfh_fh, np->n_fhp->nfh_len); 4650 if (dp != NULL && 4651 (dp->nfsdl_flags & (NFSCLDL_RECALL | NFSCLDL_DELEGRET)) == 0 && 4652 (writedeleg == 0 || (dp->nfsdl_flags & NFSCLDL_WRITE) == 4653 NFSCLDL_WRITE)) { 4654 NFSUNLOCKCLSTATE(); 4655 return (0); 4656 } 4657 NFSUNLOCKCLSTATE(); 4658 return (1); 4659 } 4660 4661 /* 4662 * Look for an associated delegation that should be DelegReturned. 4663 */ 4664 int 4665 nfscl_removedeleg(vnode_t vp, NFSPROC_T *p, nfsv4stateid_t *stp) 4666 { 4667 struct nfsclclient *clp; 4668 struct nfscldeleg *dp; 4669 struct nfsclowner *owp; 4670 struct nfscllockowner *lp; 4671 struct nfsmount *nmp; 4672 struct mount *mp; 4673 struct ucred *cred; 4674 struct nfsnode *np; 4675 int igotlock = 0, triedrecall = 0, needsrecall, retcnt = 0, islept; 4676 4677 nmp = VFSTONFS(vp->v_mount); 4678 if (NFSHASPNFS(nmp)) 4679 return (retcnt); 4680 NFSLOCKMNT(nmp); 4681 if ((nmp->nm_privflag & NFSMNTP_DELEGISSUED) == 0) { 4682 NFSUNLOCKMNT(nmp); 4683 return (retcnt); 4684 } 4685 NFSUNLOCKMNT(nmp); 4686 np = VTONFS(vp); 4687 mp = nmp->nm_mountp; 4688 NFSLOCKCLSTATE(); 4689 /* 4690 * Loop around waiting for: 4691 * - outstanding I/O operations on delegations to complete 4692 * - for a delegation on vp that has state, lock the client and 4693 * do a recall 4694 * - return delegation with no state 4695 */ 4696 while (1) { 4697 clp = nfscl_findcl(nmp); 4698 if (clp == NULL) { 4699 NFSUNLOCKCLSTATE(); 4700 return (retcnt); 4701 } 4702 dp = nfscl_finddeleg(clp, np->n_fhp->nfh_fh, 4703 np->n_fhp->nfh_len); 4704 if (dp != NULL) { 4705 /* 4706 * Wait for outstanding I/O ops to be done. 4707 */ 4708 if (dp->nfsdl_rwlock.nfslock_usecnt > 0) { 4709 if (igotlock) { 4710 nfsv4_unlock(&clp->nfsc_lock, 0); 4711 igotlock = 0; 4712 } 4713 dp->nfsdl_rwlock.nfslock_lock |= NFSV4LOCK_WANTED; 4714 msleep(&dp->nfsdl_rwlock, NFSCLSTATEMUTEXPTR, PZERO, 4715 "nfscld", hz); 4716 if (NFSCL_FORCEDISM(mp)) { 4717 dp->nfsdl_flags &= ~NFSCLDL_DELEGRET; 4718 NFSUNLOCKCLSTATE(); 4719 return (0); 4720 } 4721 continue; 4722 } 4723 needsrecall = 0; 4724 LIST_FOREACH(owp, &dp->nfsdl_owner, nfsow_list) { 4725 if (!LIST_EMPTY(&owp->nfsow_open)) { 4726 needsrecall = 1; 4727 break; 4728 } 4729 } 4730 if (!needsrecall) { 4731 LIST_FOREACH(lp, &dp->nfsdl_lock, nfsl_list) { 4732 if (!LIST_EMPTY(&lp->nfsl_lock)) { 4733 needsrecall = 1; 4734 break; 4735 } 4736 } 4737 } 4738 if (needsrecall && !triedrecall) { 4739 dp->nfsdl_flags |= NFSCLDL_DELEGRET; 4740 islept = 0; 4741 while (!igotlock) { 4742 igotlock = nfsv4_lock(&clp->nfsc_lock, 1, 4743 &islept, NFSCLSTATEMUTEXPTR, mp); 4744 if (NFSCL_FORCEDISM(mp)) { 4745 dp->nfsdl_flags &= ~NFSCLDL_DELEGRET; 4746 if (igotlock) 4747 nfsv4_unlock(&clp->nfsc_lock, 0); 4748 NFSUNLOCKCLSTATE(); 4749 return (0); 4750 } 4751 if (islept) 4752 break; 4753 } 4754 if (islept) 4755 continue; 4756 NFSUNLOCKCLSTATE(); 4757 cred = newnfs_getcred(); 4758 newnfs_copycred(&dp->nfsdl_cred, cred); 4759 nfscl_recalldeleg(clp, nmp, dp, vp, cred, p, 0, NULL); 4760 NFSFREECRED(cred); 4761 triedrecall = 1; 4762 NFSLOCKCLSTATE(); 4763 nfsv4_unlock(&clp->nfsc_lock, 0); 4764 igotlock = 0; 4765 continue; 4766 } 4767 *stp = dp->nfsdl_stateid; 4768 retcnt = 1; 4769 nfscl_cleandeleg(dp); 4770 nfscl_freedeleg(&clp->nfsc_deleg, dp, true); 4771 } 4772 if (igotlock) 4773 nfsv4_unlock(&clp->nfsc_lock, 0); 4774 NFSUNLOCKCLSTATE(); 4775 return (retcnt); 4776 } 4777 } 4778 4779 /* 4780 * Look for associated delegation(s) that should be DelegReturned. 4781 */ 4782 int 4783 nfscl_renamedeleg(vnode_t fvp, nfsv4stateid_t *fstp, int *gotfdp, vnode_t tvp, 4784 nfsv4stateid_t *tstp, int *gottdp, NFSPROC_T *p) 4785 { 4786 struct nfsclclient *clp; 4787 struct nfscldeleg *dp; 4788 struct nfsclowner *owp; 4789 struct nfscllockowner *lp; 4790 struct nfsmount *nmp; 4791 struct mount *mp; 4792 struct ucred *cred; 4793 struct nfsnode *np; 4794 int igotlock = 0, triedrecall = 0, needsrecall, retcnt = 0, islept; 4795 4796 nmp = VFSTONFS(fvp->v_mount); 4797 *gotfdp = 0; 4798 *gottdp = 0; 4799 if (NFSHASPNFS(nmp)) 4800 return (retcnt); 4801 NFSLOCKMNT(nmp); 4802 if ((nmp->nm_privflag & NFSMNTP_DELEGISSUED) == 0) { 4803 NFSUNLOCKMNT(nmp); 4804 return (retcnt); 4805 } 4806 NFSUNLOCKMNT(nmp); 4807 mp = nmp->nm_mountp; 4808 NFSLOCKCLSTATE(); 4809 /* 4810 * Loop around waiting for: 4811 * - outstanding I/O operations on delegations to complete 4812 * - for a delegation on fvp that has state, lock the client and 4813 * do a recall 4814 * - return delegation(s) with no state. 4815 */ 4816 while (1) { 4817 clp = nfscl_findcl(nmp); 4818 if (clp == NULL) { 4819 NFSUNLOCKCLSTATE(); 4820 return (retcnt); 4821 } 4822 np = VTONFS(fvp); 4823 dp = nfscl_finddeleg(clp, np->n_fhp->nfh_fh, 4824 np->n_fhp->nfh_len); 4825 if (dp != NULL && *gotfdp == 0) { 4826 /* 4827 * Wait for outstanding I/O ops to be done. 4828 */ 4829 if (dp->nfsdl_rwlock.nfslock_usecnt > 0) { 4830 if (igotlock) { 4831 nfsv4_unlock(&clp->nfsc_lock, 0); 4832 igotlock = 0; 4833 } 4834 dp->nfsdl_rwlock.nfslock_lock |= NFSV4LOCK_WANTED; 4835 msleep(&dp->nfsdl_rwlock, NFSCLSTATEMUTEXPTR, PZERO, 4836 "nfscld", hz); 4837 if (NFSCL_FORCEDISM(mp)) { 4838 dp->nfsdl_flags &= ~NFSCLDL_DELEGRET; 4839 NFSUNLOCKCLSTATE(); 4840 *gotfdp = 0; 4841 *gottdp = 0; 4842 return (0); 4843 } 4844 continue; 4845 } 4846 needsrecall = 0; 4847 LIST_FOREACH(owp, &dp->nfsdl_owner, nfsow_list) { 4848 if (!LIST_EMPTY(&owp->nfsow_open)) { 4849 needsrecall = 1; 4850 break; 4851 } 4852 } 4853 if (!needsrecall) { 4854 LIST_FOREACH(lp, &dp->nfsdl_lock, nfsl_list) { 4855 if (!LIST_EMPTY(&lp->nfsl_lock)) { 4856 needsrecall = 1; 4857 break; 4858 } 4859 } 4860 } 4861 if (needsrecall && !triedrecall) { 4862 dp->nfsdl_flags |= NFSCLDL_DELEGRET; 4863 islept = 0; 4864 while (!igotlock) { 4865 igotlock = nfsv4_lock(&clp->nfsc_lock, 1, 4866 &islept, NFSCLSTATEMUTEXPTR, mp); 4867 if (NFSCL_FORCEDISM(mp)) { 4868 dp->nfsdl_flags &= ~NFSCLDL_DELEGRET; 4869 if (igotlock) 4870 nfsv4_unlock(&clp->nfsc_lock, 0); 4871 NFSUNLOCKCLSTATE(); 4872 *gotfdp = 0; 4873 *gottdp = 0; 4874 return (0); 4875 } 4876 if (islept) 4877 break; 4878 } 4879 if (islept) 4880 continue; 4881 NFSUNLOCKCLSTATE(); 4882 cred = newnfs_getcred(); 4883 newnfs_copycred(&dp->nfsdl_cred, cred); 4884 nfscl_recalldeleg(clp, nmp, dp, fvp, cred, p, 0, NULL); 4885 NFSFREECRED(cred); 4886 triedrecall = 1; 4887 NFSLOCKCLSTATE(); 4888 nfsv4_unlock(&clp->nfsc_lock, 0); 4889 igotlock = 0; 4890 continue; 4891 } 4892 *fstp = dp->nfsdl_stateid; 4893 retcnt++; 4894 *gotfdp = 1; 4895 nfscl_cleandeleg(dp); 4896 nfscl_freedeleg(&clp->nfsc_deleg, dp, true); 4897 } 4898 if (igotlock) { 4899 nfsv4_unlock(&clp->nfsc_lock, 0); 4900 igotlock = 0; 4901 } 4902 if (tvp != NULL) { 4903 np = VTONFS(tvp); 4904 dp = nfscl_finddeleg(clp, np->n_fhp->nfh_fh, 4905 np->n_fhp->nfh_len); 4906 if (dp != NULL && *gottdp == 0) { 4907 /* 4908 * Wait for outstanding I/O ops to be done. 4909 */ 4910 if (dp->nfsdl_rwlock.nfslock_usecnt > 0) { 4911 dp->nfsdl_rwlock.nfslock_lock |= NFSV4LOCK_WANTED; 4912 msleep(&dp->nfsdl_rwlock, NFSCLSTATEMUTEXPTR, PZERO, 4913 "nfscld", hz); 4914 if (NFSCL_FORCEDISM(mp)) { 4915 NFSUNLOCKCLSTATE(); 4916 *gotfdp = 0; 4917 *gottdp = 0; 4918 return (0); 4919 } 4920 continue; 4921 } 4922 LIST_FOREACH(owp, &dp->nfsdl_owner, nfsow_list) { 4923 if (!LIST_EMPTY(&owp->nfsow_open)) { 4924 NFSUNLOCKCLSTATE(); 4925 return (retcnt); 4926 } 4927 } 4928 LIST_FOREACH(lp, &dp->nfsdl_lock, nfsl_list) { 4929 if (!LIST_EMPTY(&lp->nfsl_lock)) { 4930 NFSUNLOCKCLSTATE(); 4931 return (retcnt); 4932 } 4933 } 4934 *tstp = dp->nfsdl_stateid; 4935 retcnt++; 4936 *gottdp = 1; 4937 nfscl_cleandeleg(dp); 4938 nfscl_freedeleg(&clp->nfsc_deleg, dp, true); 4939 } 4940 } 4941 NFSUNLOCKCLSTATE(); 4942 return (retcnt); 4943 } 4944 } 4945 4946 /* 4947 * Get a reference on the clientid associated with the mount point. 4948 * Return 1 if success, 0 otherwise. 4949 */ 4950 int 4951 nfscl_getref(struct nfsmount *nmp) 4952 { 4953 struct nfsclclient *clp; 4954 int ret; 4955 4956 NFSLOCKCLSTATE(); 4957 clp = nfscl_findcl(nmp); 4958 if (clp == NULL) { 4959 NFSUNLOCKCLSTATE(); 4960 return (0); 4961 } 4962 nfsv4_getref(&clp->nfsc_lock, NULL, NFSCLSTATEMUTEXPTR, nmp->nm_mountp); 4963 ret = 1; 4964 if (NFSCL_FORCEDISM(nmp->nm_mountp)) 4965 ret = 0; 4966 NFSUNLOCKCLSTATE(); 4967 return (ret); 4968 } 4969 4970 /* 4971 * Release a reference on a clientid acquired with the above call. 4972 */ 4973 void 4974 nfscl_relref(struct nfsmount *nmp) 4975 { 4976 struct nfsclclient *clp; 4977 4978 NFSLOCKCLSTATE(); 4979 clp = nfscl_findcl(nmp); 4980 if (clp == NULL) { 4981 NFSUNLOCKCLSTATE(); 4982 return; 4983 } 4984 nfsv4_relref(&clp->nfsc_lock); 4985 NFSUNLOCKCLSTATE(); 4986 } 4987 4988 /* 4989 * Save the size attribute in the delegation, since the nfsnode 4990 * is going away. 4991 */ 4992 void 4993 nfscl_reclaimnode(vnode_t vp) 4994 { 4995 struct nfsclclient *clp; 4996 struct nfscldeleg *dp; 4997 struct nfsnode *np = VTONFS(vp); 4998 struct nfsmount *nmp; 4999 5000 nmp = VFSTONFS(vp->v_mount); 5001 if (!NFSHASNFSV4(nmp)) 5002 return; 5003 NFSLOCKCLSTATE(); 5004 clp = nfscl_findcl(nmp); 5005 if (clp == NULL) { 5006 NFSUNLOCKCLSTATE(); 5007 return; 5008 } 5009 dp = nfscl_finddeleg(clp, np->n_fhp->nfh_fh, np->n_fhp->nfh_len); 5010 if (dp != NULL && (dp->nfsdl_flags & NFSCLDL_WRITE)) 5011 dp->nfsdl_size = np->n_size; 5012 NFSUNLOCKCLSTATE(); 5013 } 5014 5015 /* 5016 * Get the saved size attribute in the delegation, since it is a 5017 * newly allocated nfsnode. 5018 */ 5019 void 5020 nfscl_newnode(vnode_t vp) 5021 { 5022 struct nfsclclient *clp; 5023 struct nfscldeleg *dp; 5024 struct nfsnode *np = VTONFS(vp); 5025 struct nfsmount *nmp; 5026 5027 nmp = VFSTONFS(vp->v_mount); 5028 if (!NFSHASNFSV4(nmp)) 5029 return; 5030 NFSLOCKCLSTATE(); 5031 clp = nfscl_findcl(nmp); 5032 if (clp == NULL) { 5033 NFSUNLOCKCLSTATE(); 5034 return; 5035 } 5036 dp = nfscl_finddeleg(clp, np->n_fhp->nfh_fh, np->n_fhp->nfh_len); 5037 if (dp != NULL && (dp->nfsdl_flags & NFSCLDL_WRITE)) 5038 np->n_size = dp->nfsdl_size; 5039 NFSUNLOCKCLSTATE(); 5040 } 5041 5042 /* 5043 * If there is a valid write delegation for this file, set the modtime 5044 * to the local clock time. 5045 */ 5046 void 5047 nfscl_delegmodtime(vnode_t vp) 5048 { 5049 struct nfsclclient *clp; 5050 struct nfscldeleg *dp; 5051 struct nfsnode *np = VTONFS(vp); 5052 struct nfsmount *nmp; 5053 5054 nmp = VFSTONFS(vp->v_mount); 5055 if (!NFSHASNFSV4(nmp)) 5056 return; 5057 NFSLOCKMNT(nmp); 5058 if ((nmp->nm_privflag & NFSMNTP_DELEGISSUED) == 0) { 5059 NFSUNLOCKMNT(nmp); 5060 return; 5061 } 5062 NFSUNLOCKMNT(nmp); 5063 NFSLOCKCLSTATE(); 5064 clp = nfscl_findcl(nmp); 5065 if (clp == NULL) { 5066 NFSUNLOCKCLSTATE(); 5067 return; 5068 } 5069 dp = nfscl_finddeleg(clp, np->n_fhp->nfh_fh, np->n_fhp->nfh_len); 5070 if (dp != NULL && (dp->nfsdl_flags & NFSCLDL_WRITE)) { 5071 nanotime(&dp->nfsdl_modtime); 5072 dp->nfsdl_flags |= NFSCLDL_MODTIMESET; 5073 } 5074 NFSUNLOCKCLSTATE(); 5075 } 5076 5077 /* 5078 * If there is a valid write delegation for this file with a modtime set, 5079 * put that modtime in mtime. 5080 */ 5081 void 5082 nfscl_deleggetmodtime(vnode_t vp, struct timespec *mtime) 5083 { 5084 struct nfsclclient *clp; 5085 struct nfscldeleg *dp; 5086 struct nfsnode *np = VTONFS(vp); 5087 struct nfsmount *nmp; 5088 5089 nmp = VFSTONFS(vp->v_mount); 5090 if (!NFSHASNFSV4(nmp)) 5091 return; 5092 NFSLOCKMNT(nmp); 5093 if ((nmp->nm_privflag & NFSMNTP_DELEGISSUED) == 0) { 5094 NFSUNLOCKMNT(nmp); 5095 return; 5096 } 5097 NFSUNLOCKMNT(nmp); 5098 NFSLOCKCLSTATE(); 5099 clp = nfscl_findcl(nmp); 5100 if (clp == NULL) { 5101 NFSUNLOCKCLSTATE(); 5102 return; 5103 } 5104 dp = nfscl_finddeleg(clp, np->n_fhp->nfh_fh, np->n_fhp->nfh_len); 5105 if (dp != NULL && 5106 (dp->nfsdl_flags & (NFSCLDL_WRITE | NFSCLDL_MODTIMESET)) == 5107 (NFSCLDL_WRITE | NFSCLDL_MODTIMESET)) 5108 *mtime = dp->nfsdl_modtime; 5109 NFSUNLOCKCLSTATE(); 5110 } 5111 5112 static int 5113 nfscl_errmap(struct nfsrv_descript *nd, u_int32_t minorvers) 5114 { 5115 short *defaulterrp, *errp; 5116 5117 if (!nd->nd_repstat) 5118 return (0); 5119 if (nd->nd_procnum == NFSPROC_NOOP) 5120 return (txdr_unsigned(nd->nd_repstat & 0xffff)); 5121 if (nd->nd_repstat == EBADRPC) 5122 return (txdr_unsigned(NFSERR_BADXDR)); 5123 if (nd->nd_repstat == NFSERR_MINORVERMISMATCH || 5124 nd->nd_repstat == NFSERR_OPILLEGAL) 5125 return (txdr_unsigned(nd->nd_repstat)); 5126 if (nd->nd_repstat >= NFSERR_BADIOMODE && nd->nd_repstat < 20000 && 5127 minorvers > NFSV4_MINORVERSION) { 5128 /* NFSv4.n error. */ 5129 return (txdr_unsigned(nd->nd_repstat)); 5130 } 5131 if (nd->nd_procnum < NFSV4OP_CBNOPS) 5132 errp = defaulterrp = nfscl_cberrmap[nd->nd_procnum]; 5133 else 5134 return (txdr_unsigned(nd->nd_repstat)); 5135 while (*++errp) 5136 if (*errp == (short)nd->nd_repstat) 5137 return (txdr_unsigned(nd->nd_repstat)); 5138 return (txdr_unsigned(*defaulterrp)); 5139 } 5140 5141 /* 5142 * Called to find/add a layout to a client. 5143 * This function returns the layout with a refcnt (shared lock) upon 5144 * success (returns 0) or with no lock/refcnt on the layout when an 5145 * error is returned. 5146 * If a layout is passed in via lypp, it is locked (exclusively locked). 5147 */ 5148 int 5149 nfscl_layout(struct nfsmount *nmp, vnode_t vp, u_int8_t *fhp, int fhlen, 5150 nfsv4stateid_t *stateidp, int layouttype, int retonclose, 5151 struct nfsclflayouthead *fhlp, struct nfscllayout **lypp, 5152 struct ucred *cred, NFSPROC_T *p) 5153 { 5154 struct nfsclclient *clp; 5155 struct nfscllayout *lyp, *tlyp; 5156 struct nfsclflayout *flp; 5157 struct nfsnode *np = VTONFS(vp); 5158 mount_t mp; 5159 int layout_passed_in; 5160 5161 mp = nmp->nm_mountp; 5162 layout_passed_in = 1; 5163 tlyp = NULL; 5164 lyp = *lypp; 5165 if (lyp == NULL) { 5166 layout_passed_in = 0; 5167 tlyp = malloc(sizeof(*tlyp) + fhlen - 1, M_NFSLAYOUT, 5168 M_WAITOK | M_ZERO); 5169 } 5170 5171 NFSLOCKCLSTATE(); 5172 clp = nmp->nm_clp; 5173 if (clp == NULL) { 5174 if (layout_passed_in != 0) 5175 nfsv4_unlock(&lyp->nfsly_lock, 0); 5176 NFSUNLOCKCLSTATE(); 5177 if (tlyp != NULL) 5178 free(tlyp, M_NFSLAYOUT); 5179 return (EPERM); 5180 } 5181 if (lyp == NULL) { 5182 /* 5183 * Although no lyp was passed in, another thread might have 5184 * allocated one. If one is found, just increment it's ref 5185 * count and return it. 5186 */ 5187 lyp = nfscl_findlayout(clp, fhp, fhlen); 5188 if (lyp == NULL) { 5189 lyp = tlyp; 5190 tlyp = NULL; 5191 lyp->nfsly_stateid.seqid = stateidp->seqid; 5192 lyp->nfsly_stateid.other[0] = stateidp->other[0]; 5193 lyp->nfsly_stateid.other[1] = stateidp->other[1]; 5194 lyp->nfsly_stateid.other[2] = stateidp->other[2]; 5195 lyp->nfsly_lastbyte = 0; 5196 LIST_INIT(&lyp->nfsly_flayread); 5197 LIST_INIT(&lyp->nfsly_flayrw); 5198 LIST_INIT(&lyp->nfsly_recall); 5199 lyp->nfsly_filesid[0] = np->n_vattr.na_filesid[0]; 5200 lyp->nfsly_filesid[1] = np->n_vattr.na_filesid[1]; 5201 lyp->nfsly_clp = clp; 5202 if (layouttype == NFSLAYOUT_FLEXFILE) 5203 lyp->nfsly_flags = NFSLY_FLEXFILE; 5204 else 5205 lyp->nfsly_flags = NFSLY_FILES; 5206 if (retonclose != 0) 5207 lyp->nfsly_flags |= NFSLY_RETONCLOSE; 5208 lyp->nfsly_fhlen = fhlen; 5209 NFSBCOPY(fhp, lyp->nfsly_fh, fhlen); 5210 TAILQ_INSERT_HEAD(&clp->nfsc_layout, lyp, nfsly_list); 5211 LIST_INSERT_HEAD(NFSCLLAYOUTHASH(clp, fhp, fhlen), lyp, 5212 nfsly_hash); 5213 lyp->nfsly_timestamp = NFSD_MONOSEC + 120; 5214 nfscl_layoutcnt++; 5215 } else { 5216 if (retonclose != 0) 5217 lyp->nfsly_flags |= NFSLY_RETONCLOSE; 5218 if (stateidp->seqid > lyp->nfsly_stateid.seqid) 5219 lyp->nfsly_stateid.seqid = stateidp->seqid; 5220 TAILQ_REMOVE(&clp->nfsc_layout, lyp, nfsly_list); 5221 TAILQ_INSERT_HEAD(&clp->nfsc_layout, lyp, nfsly_list); 5222 lyp->nfsly_timestamp = NFSD_MONOSEC + 120; 5223 } 5224 nfsv4_getref(&lyp->nfsly_lock, NULL, NFSCLSTATEMUTEXPTR, mp); 5225 if (NFSCL_FORCEDISM(mp)) { 5226 NFSUNLOCKCLSTATE(); 5227 if (tlyp != NULL) 5228 free(tlyp, M_NFSLAYOUT); 5229 return (EPERM); 5230 } 5231 *lypp = lyp; 5232 } else if (stateidp->seqid > lyp->nfsly_stateid.seqid) 5233 lyp->nfsly_stateid.seqid = stateidp->seqid; 5234 5235 /* Merge the new list of File Layouts into the list. */ 5236 flp = LIST_FIRST(fhlp); 5237 if (flp != NULL) { 5238 if (flp->nfsfl_iomode == NFSLAYOUTIOMODE_READ) 5239 nfscl_mergeflayouts(&lyp->nfsly_flayread, fhlp); 5240 else 5241 nfscl_mergeflayouts(&lyp->nfsly_flayrw, fhlp); 5242 } 5243 if (layout_passed_in != 0) 5244 nfsv4_unlock(&lyp->nfsly_lock, 1); 5245 NFSUNLOCKCLSTATE(); 5246 if (tlyp != NULL) 5247 free(tlyp, M_NFSLAYOUT); 5248 return (0); 5249 } 5250 5251 /* 5252 * Search for a layout by MDS file handle. 5253 * If one is found, it is returned with a refcnt (shared lock) iff 5254 * retflpp returned non-NULL and locked (exclusive locked) iff retflpp is 5255 * returned NULL. 5256 */ 5257 struct nfscllayout * 5258 nfscl_getlayout(struct nfsclclient *clp, uint8_t *fhp, int fhlen, 5259 uint64_t off, uint32_t rwaccess, struct nfsclflayout **retflpp, 5260 int *recalledp) 5261 { 5262 struct nfscllayout *lyp; 5263 mount_t mp; 5264 int error, igotlock; 5265 5266 mp = clp->nfsc_nmp->nm_mountp; 5267 *recalledp = 0; 5268 *retflpp = NULL; 5269 NFSLOCKCLSTATE(); 5270 lyp = nfscl_findlayout(clp, fhp, fhlen); 5271 if (lyp != NULL) { 5272 if ((lyp->nfsly_flags & NFSLY_RECALL) == 0) { 5273 TAILQ_REMOVE(&clp->nfsc_layout, lyp, nfsly_list); 5274 TAILQ_INSERT_HEAD(&clp->nfsc_layout, lyp, nfsly_list); 5275 lyp->nfsly_timestamp = NFSD_MONOSEC + 120; 5276 error = nfscl_findlayoutforio(lyp, off, rwaccess, 5277 retflpp); 5278 if (error == 0) 5279 nfsv4_getref(&lyp->nfsly_lock, NULL, 5280 NFSCLSTATEMUTEXPTR, mp); 5281 else { 5282 do { 5283 igotlock = nfsv4_lock(&lyp->nfsly_lock, 5284 1, NULL, NFSCLSTATEMUTEXPTR, mp); 5285 } while (igotlock == 0 && !NFSCL_FORCEDISM(mp)); 5286 *retflpp = NULL; 5287 } 5288 if (NFSCL_FORCEDISM(mp)) { 5289 lyp = NULL; 5290 *recalledp = 1; 5291 } 5292 } else { 5293 lyp = NULL; 5294 *recalledp = 1; 5295 } 5296 } 5297 NFSUNLOCKCLSTATE(); 5298 return (lyp); 5299 } 5300 5301 /* 5302 * Search for a layout by MDS file handle. If one is found, mark in to be 5303 * recalled, if it already marked "return on close". 5304 */ 5305 static void 5306 nfscl_retoncloselayout(vnode_t vp, struct nfsclclient *clp, uint8_t *fhp, 5307 int fhlen, struct nfsclrecalllayout **recallpp, struct nfscllayout **lypp) 5308 { 5309 struct nfscllayout *lyp; 5310 uint32_t iomode; 5311 5312 *lypp = NULL; 5313 if (vp->v_type != VREG || !NFSHASPNFS(VFSTONFS(vp->v_mount)) || 5314 nfscl_enablecallb == 0 || nfs_numnfscbd == 0 || 5315 (VTONFS(vp)->n_flag & NNOLAYOUT) != 0) 5316 return; 5317 lyp = nfscl_findlayout(clp, fhp, fhlen); 5318 if (lyp != NULL && (lyp->nfsly_flags & NFSLY_RETONCLOSE) != 0) { 5319 if ((lyp->nfsly_flags & NFSLY_RECALL) == 0) { 5320 iomode = 0; 5321 if (!LIST_EMPTY(&lyp->nfsly_flayread)) 5322 iomode |= NFSLAYOUTIOMODE_READ; 5323 if (!LIST_EMPTY(&lyp->nfsly_flayrw)) 5324 iomode |= NFSLAYOUTIOMODE_RW; 5325 nfscl_layoutrecall(NFSLAYOUTRETURN_FILE, lyp, iomode, 5326 0, UINT64_MAX, lyp->nfsly_stateid.seqid, 0, 0, NULL, 5327 *recallpp); 5328 NFSCL_DEBUG(4, "retoncls recall iomode=%d\n", iomode); 5329 *recallpp = NULL; 5330 } 5331 5332 /* Now, wake up renew thread to do LayoutReturn. */ 5333 wakeup(clp); 5334 *lypp = lyp; 5335 } 5336 } 5337 5338 /* 5339 * Mark the layout to be recalled and with an error. 5340 * Also, disable the dsp from further use. 5341 */ 5342 void 5343 nfscl_dserr(uint32_t op, uint32_t stat, struct nfscldevinfo *dp, 5344 struct nfscllayout *lyp, struct nfsclds *dsp) 5345 { 5346 struct nfsclrecalllayout *recallp; 5347 uint32_t iomode; 5348 5349 printf("DS being disabled, error=%d\n", stat); 5350 /* Set up the return of the layout. */ 5351 recallp = malloc(sizeof(*recallp), M_NFSLAYRECALL, M_WAITOK); 5352 iomode = 0; 5353 NFSLOCKCLSTATE(); 5354 if ((lyp->nfsly_flags & NFSLY_RECALL) == 0) { 5355 if (!LIST_EMPTY(&lyp->nfsly_flayread)) 5356 iomode |= NFSLAYOUTIOMODE_READ; 5357 if (!LIST_EMPTY(&lyp->nfsly_flayrw)) 5358 iomode |= NFSLAYOUTIOMODE_RW; 5359 (void)nfscl_layoutrecall(NFSLAYOUTRETURN_FILE, lyp, iomode, 5360 0, UINT64_MAX, lyp->nfsly_stateid.seqid, stat, op, 5361 dp->nfsdi_deviceid, recallp); 5362 NFSUNLOCKCLSTATE(); 5363 NFSCL_DEBUG(4, "nfscl_dserr recall iomode=%d\n", iomode); 5364 } else { 5365 NFSUNLOCKCLSTATE(); 5366 free(recallp, M_NFSLAYRECALL); 5367 } 5368 5369 /* And shut the TCP connection down. */ 5370 nfscl_cancelreqs(dsp); 5371 } 5372 5373 /* 5374 * Cancel all RPCs for this "dsp" by closing the connection. 5375 * Also, mark the session as defunct. 5376 * If NFSCLDS_SAMECONN is set, the connection is shared with other DSs and 5377 * cannot be shut down. 5378 */ 5379 void 5380 nfscl_cancelreqs(struct nfsclds *dsp) 5381 { 5382 struct __rpc_client *cl; 5383 static int non_event; 5384 5385 NFSLOCKDS(dsp); 5386 if ((dsp->nfsclds_flags & (NFSCLDS_CLOSED | NFSCLDS_SAMECONN)) == 0 && 5387 dsp->nfsclds_sockp != NULL && 5388 dsp->nfsclds_sockp->nr_client != NULL) { 5389 dsp->nfsclds_flags |= NFSCLDS_CLOSED; 5390 cl = dsp->nfsclds_sockp->nr_client; 5391 dsp->nfsclds_sess.nfsess_defunct = 1; 5392 NFSUNLOCKDS(dsp); 5393 CLNT_CLOSE(cl); 5394 /* 5395 * This 1sec sleep is done to reduce the number of reconnect 5396 * attempts made on the DS while it has failed. 5397 */ 5398 tsleep(&non_event, PVFS, "ndscls", hz); 5399 return; 5400 } 5401 NFSUNLOCKDS(dsp); 5402 } 5403 5404 /* 5405 * Dereference a layout. 5406 */ 5407 void 5408 nfscl_rellayout(struct nfscllayout *lyp, int exclocked) 5409 { 5410 5411 NFSLOCKCLSTATE(); 5412 if (exclocked != 0) 5413 nfsv4_unlock(&lyp->nfsly_lock, 0); 5414 else 5415 nfsv4_relref(&lyp->nfsly_lock); 5416 NFSUNLOCKCLSTATE(); 5417 } 5418 5419 /* 5420 * Search for a devinfo by deviceid. If one is found, return it after 5421 * acquiring a reference count on it. 5422 */ 5423 struct nfscldevinfo * 5424 nfscl_getdevinfo(struct nfsclclient *clp, uint8_t *deviceid, 5425 struct nfscldevinfo *dip) 5426 { 5427 5428 NFSLOCKCLSTATE(); 5429 if (dip == NULL) 5430 dip = nfscl_finddevinfo(clp, deviceid); 5431 if (dip != NULL) 5432 dip->nfsdi_refcnt++; 5433 NFSUNLOCKCLSTATE(); 5434 return (dip); 5435 } 5436 5437 /* 5438 * Dereference a devinfo structure. 5439 */ 5440 static void 5441 nfscl_reldevinfo_locked(struct nfscldevinfo *dip) 5442 { 5443 5444 dip->nfsdi_refcnt--; 5445 if (dip->nfsdi_refcnt == 0) 5446 wakeup(&dip->nfsdi_refcnt); 5447 } 5448 5449 /* 5450 * Dereference a devinfo structure. 5451 */ 5452 void 5453 nfscl_reldevinfo(struct nfscldevinfo *dip) 5454 { 5455 5456 NFSLOCKCLSTATE(); 5457 nfscl_reldevinfo_locked(dip); 5458 NFSUNLOCKCLSTATE(); 5459 } 5460 5461 /* 5462 * Find a layout for this file handle. Return NULL upon failure. 5463 */ 5464 static struct nfscllayout * 5465 nfscl_findlayout(struct nfsclclient *clp, u_int8_t *fhp, int fhlen) 5466 { 5467 struct nfscllayout *lyp; 5468 5469 LIST_FOREACH(lyp, NFSCLLAYOUTHASH(clp, fhp, fhlen), nfsly_hash) 5470 if (lyp->nfsly_fhlen == fhlen && 5471 !NFSBCMP(lyp->nfsly_fh, fhp, fhlen)) 5472 break; 5473 return (lyp); 5474 } 5475 5476 /* 5477 * Find a devinfo for this deviceid. Return NULL upon failure. 5478 */ 5479 static struct nfscldevinfo * 5480 nfscl_finddevinfo(struct nfsclclient *clp, uint8_t *deviceid) 5481 { 5482 struct nfscldevinfo *dip; 5483 5484 LIST_FOREACH(dip, &clp->nfsc_devinfo, nfsdi_list) 5485 if (NFSBCMP(dip->nfsdi_deviceid, deviceid, NFSX_V4DEVICEID) 5486 == 0) 5487 break; 5488 return (dip); 5489 } 5490 5491 /* 5492 * Merge the new file layout list into the main one, maintaining it in 5493 * increasing offset order. 5494 */ 5495 static void 5496 nfscl_mergeflayouts(struct nfsclflayouthead *fhlp, 5497 struct nfsclflayouthead *newfhlp) 5498 { 5499 struct nfsclflayout *flp, *nflp, *prevflp, *tflp; 5500 5501 flp = LIST_FIRST(fhlp); 5502 prevflp = NULL; 5503 LIST_FOREACH_SAFE(nflp, newfhlp, nfsfl_list, tflp) { 5504 while (flp != NULL && flp->nfsfl_off < nflp->nfsfl_off) { 5505 prevflp = flp; 5506 flp = LIST_NEXT(flp, nfsfl_list); 5507 } 5508 if (prevflp == NULL) 5509 LIST_INSERT_HEAD(fhlp, nflp, nfsfl_list); 5510 else 5511 LIST_INSERT_AFTER(prevflp, nflp, nfsfl_list); 5512 prevflp = nflp; 5513 } 5514 } 5515 5516 /* 5517 * Add this nfscldevinfo to the client, if it doesn't already exist. 5518 * This function consumes the structure pointed at by dip, if not NULL. 5519 */ 5520 int 5521 nfscl_adddevinfo(struct nfsmount *nmp, struct nfscldevinfo *dip, int ind, 5522 struct nfsclflayout *flp) 5523 { 5524 struct nfsclclient *clp; 5525 struct nfscldevinfo *tdip; 5526 uint8_t *dev; 5527 5528 NFSLOCKCLSTATE(); 5529 clp = nmp->nm_clp; 5530 if (clp == NULL) { 5531 NFSUNLOCKCLSTATE(); 5532 if (dip != NULL) 5533 free(dip, M_NFSDEVINFO); 5534 return (ENODEV); 5535 } 5536 if ((flp->nfsfl_flags & NFSFL_FILE) != 0) 5537 dev = flp->nfsfl_dev; 5538 else 5539 dev = flp->nfsfl_ffm[ind].dev; 5540 tdip = nfscl_finddevinfo(clp, dev); 5541 if (tdip != NULL) { 5542 tdip->nfsdi_layoutrefs++; 5543 if ((flp->nfsfl_flags & NFSFL_FILE) != 0) 5544 flp->nfsfl_devp = tdip; 5545 else 5546 flp->nfsfl_ffm[ind].devp = tdip; 5547 nfscl_reldevinfo_locked(tdip); 5548 NFSUNLOCKCLSTATE(); 5549 if (dip != NULL) 5550 free(dip, M_NFSDEVINFO); 5551 return (0); 5552 } 5553 if (dip != NULL) { 5554 LIST_INSERT_HEAD(&clp->nfsc_devinfo, dip, nfsdi_list); 5555 dip->nfsdi_layoutrefs = 1; 5556 if ((flp->nfsfl_flags & NFSFL_FILE) != 0) 5557 flp->nfsfl_devp = dip; 5558 else 5559 flp->nfsfl_ffm[ind].devp = dip; 5560 } 5561 NFSUNLOCKCLSTATE(); 5562 if (dip == NULL) 5563 return (ENODEV); 5564 return (0); 5565 } 5566 5567 /* 5568 * Free up a layout structure and associated file layout structure(s). 5569 */ 5570 void 5571 nfscl_freelayout(struct nfscllayout *layp) 5572 { 5573 struct nfsclflayout *flp, *nflp; 5574 struct nfsclrecalllayout *rp, *nrp; 5575 5576 LIST_FOREACH_SAFE(flp, &layp->nfsly_flayread, nfsfl_list, nflp) { 5577 LIST_REMOVE(flp, nfsfl_list); 5578 nfscl_freeflayout(flp); 5579 } 5580 LIST_FOREACH_SAFE(flp, &layp->nfsly_flayrw, nfsfl_list, nflp) { 5581 LIST_REMOVE(flp, nfsfl_list); 5582 nfscl_freeflayout(flp); 5583 } 5584 LIST_FOREACH_SAFE(rp, &layp->nfsly_recall, nfsrecly_list, nrp) { 5585 LIST_REMOVE(rp, nfsrecly_list); 5586 free(rp, M_NFSLAYRECALL); 5587 } 5588 nfscl_layoutcnt--; 5589 free(layp, M_NFSLAYOUT); 5590 } 5591 5592 /* 5593 * Free up a file layout structure. 5594 */ 5595 void 5596 nfscl_freeflayout(struct nfsclflayout *flp) 5597 { 5598 int i, j; 5599 5600 if ((flp->nfsfl_flags & NFSFL_FILE) != 0) { 5601 for (i = 0; i < flp->nfsfl_fhcnt; i++) 5602 free(flp->nfsfl_fh[i], M_NFSFH); 5603 if (flp->nfsfl_devp != NULL) 5604 flp->nfsfl_devp->nfsdi_layoutrefs--; 5605 } 5606 if ((flp->nfsfl_flags & NFSFL_FLEXFILE) != 0) 5607 for (i = 0; i < flp->nfsfl_mirrorcnt; i++) { 5608 for (j = 0; j < flp->nfsfl_ffm[i].fhcnt; j++) 5609 free(flp->nfsfl_ffm[i].fh[j], M_NFSFH); 5610 if (flp->nfsfl_ffm[i].devp != NULL) 5611 flp->nfsfl_ffm[i].devp->nfsdi_layoutrefs--; 5612 } 5613 free(flp, M_NFSFLAYOUT); 5614 } 5615 5616 /* 5617 * Free up a file layout devinfo structure. 5618 */ 5619 void 5620 nfscl_freedevinfo(struct nfscldevinfo *dip) 5621 { 5622 5623 free(dip, M_NFSDEVINFO); 5624 } 5625 5626 /* 5627 * Mark any layouts that match as recalled. 5628 */ 5629 static int 5630 nfscl_layoutrecall(int recalltype, struct nfscllayout *lyp, uint32_t iomode, 5631 uint64_t off, uint64_t len, uint32_t stateseqid, uint32_t stat, uint32_t op, 5632 char *devid, struct nfsclrecalllayout *recallp) 5633 { 5634 struct nfsclrecalllayout *rp, *orp; 5635 5636 recallp->nfsrecly_recalltype = recalltype; 5637 recallp->nfsrecly_iomode = iomode; 5638 recallp->nfsrecly_stateseqid = stateseqid; 5639 recallp->nfsrecly_off = off; 5640 recallp->nfsrecly_len = len; 5641 recallp->nfsrecly_stat = stat; 5642 recallp->nfsrecly_op = op; 5643 if (devid != NULL) 5644 NFSBCOPY(devid, recallp->nfsrecly_devid, NFSX_V4DEVICEID); 5645 /* 5646 * Order the list as file returns first, followed by fsid and any 5647 * returns, both in increasing stateseqid order. 5648 * Note that the seqids wrap around, so 1 is after 0xffffffff. 5649 * (I'm not sure this is correct because I find RFC5661 confusing 5650 * on this, but hopefully it will work ok.) 5651 */ 5652 orp = NULL; 5653 LIST_FOREACH(rp, &lyp->nfsly_recall, nfsrecly_list) { 5654 orp = rp; 5655 if ((recalltype == NFSLAYOUTRETURN_FILE && 5656 (rp->nfsrecly_recalltype != NFSLAYOUTRETURN_FILE || 5657 nfscl_seq(stateseqid, rp->nfsrecly_stateseqid) != 0)) || 5658 (recalltype != NFSLAYOUTRETURN_FILE && 5659 rp->nfsrecly_recalltype != NFSLAYOUTRETURN_FILE && 5660 nfscl_seq(stateseqid, rp->nfsrecly_stateseqid) != 0)) { 5661 LIST_INSERT_BEFORE(rp, recallp, nfsrecly_list); 5662 break; 5663 } 5664 5665 /* 5666 * Put any error return on all the file returns that will 5667 * preceed this one. 5668 */ 5669 if (rp->nfsrecly_recalltype == NFSLAYOUTRETURN_FILE && 5670 stat != 0 && rp->nfsrecly_stat == 0) { 5671 rp->nfsrecly_stat = stat; 5672 rp->nfsrecly_op = op; 5673 if (devid != NULL) 5674 NFSBCOPY(devid, rp->nfsrecly_devid, 5675 NFSX_V4DEVICEID); 5676 } 5677 } 5678 if (rp == NULL) { 5679 if (orp == NULL) 5680 LIST_INSERT_HEAD(&lyp->nfsly_recall, recallp, 5681 nfsrecly_list); 5682 else 5683 LIST_INSERT_AFTER(orp, recallp, nfsrecly_list); 5684 } 5685 lyp->nfsly_flags |= NFSLY_RECALL; 5686 wakeup(lyp->nfsly_clp); 5687 return (0); 5688 } 5689 5690 /* 5691 * Compare the two seqids for ordering. The trick is that the seqids can 5692 * wrap around from 0xffffffff->0, so check for the cases where one 5693 * has wrapped around. 5694 * Return 1 if seqid1 comes before seqid2, 0 otherwise. 5695 */ 5696 static int 5697 nfscl_seq(uint32_t seqid1, uint32_t seqid2) 5698 { 5699 5700 if (seqid2 > seqid1 && (seqid2 - seqid1) >= 0x7fffffff) 5701 /* seqid2 has wrapped around. */ 5702 return (0); 5703 if (seqid1 > seqid2 && (seqid1 - seqid2) >= 0x7fffffff) 5704 /* seqid1 has wrapped around. */ 5705 return (1); 5706 if (seqid1 <= seqid2) 5707 return (1); 5708 return (0); 5709 } 5710 5711 /* 5712 * Do a layout return for each of the recalls. 5713 */ 5714 static void 5715 nfscl_layoutreturn(struct nfsmount *nmp, struct nfscllayout *lyp, 5716 struct ucred *cred, NFSPROC_T *p) 5717 { 5718 struct nfsclrecalllayout *rp; 5719 nfsv4stateid_t stateid; 5720 int layouttype; 5721 5722 NFSBCOPY(lyp->nfsly_stateid.other, stateid.other, NFSX_STATEIDOTHER); 5723 stateid.seqid = lyp->nfsly_stateid.seqid; 5724 if ((lyp->nfsly_flags & NFSLY_FILES) != 0) 5725 layouttype = NFSLAYOUT_NFSV4_1_FILES; 5726 else 5727 layouttype = NFSLAYOUT_FLEXFILE; 5728 LIST_FOREACH(rp, &lyp->nfsly_recall, nfsrecly_list) { 5729 (void)nfsrpc_layoutreturn(nmp, lyp->nfsly_fh, 5730 lyp->nfsly_fhlen, 0, layouttype, 5731 rp->nfsrecly_iomode, rp->nfsrecly_recalltype, 5732 rp->nfsrecly_off, rp->nfsrecly_len, 5733 &stateid, cred, p, rp->nfsrecly_stat, rp->nfsrecly_op, 5734 rp->nfsrecly_devid); 5735 } 5736 } 5737 5738 /* 5739 * Do the layout commit for a file layout. 5740 */ 5741 static void 5742 nfscl_dolayoutcommit(struct nfsmount *nmp, struct nfscllayout *lyp, 5743 struct ucred *cred, NFSPROC_T *p) 5744 { 5745 struct nfsclflayout *flp; 5746 uint64_t len; 5747 int error, layouttype; 5748 5749 if ((lyp->nfsly_flags & NFSLY_FILES) != 0) 5750 layouttype = NFSLAYOUT_NFSV4_1_FILES; 5751 else 5752 layouttype = NFSLAYOUT_FLEXFILE; 5753 LIST_FOREACH(flp, &lyp->nfsly_flayrw, nfsfl_list) { 5754 if (layouttype == NFSLAYOUT_FLEXFILE && 5755 (flp->nfsfl_fflags & NFSFLEXFLAG_NO_LAYOUTCOMMIT) != 0) { 5756 NFSCL_DEBUG(4, "Flex file: no layoutcommit\n"); 5757 /* If not supported, don't bother doing it. */ 5758 NFSLOCKMNT(nmp); 5759 nmp->nm_state |= NFSSTA_NOLAYOUTCOMMIT; 5760 NFSUNLOCKMNT(nmp); 5761 break; 5762 } else if (flp->nfsfl_off <= lyp->nfsly_lastbyte) { 5763 len = flp->nfsfl_end - flp->nfsfl_off; 5764 error = nfsrpc_layoutcommit(nmp, lyp->nfsly_fh, 5765 lyp->nfsly_fhlen, 0, flp->nfsfl_off, len, 5766 lyp->nfsly_lastbyte, &lyp->nfsly_stateid, 5767 layouttype, cred, p, NULL); 5768 NFSCL_DEBUG(4, "layoutcommit err=%d\n", error); 5769 if (error == NFSERR_NOTSUPP) { 5770 /* If not supported, don't bother doing it. */ 5771 NFSLOCKMNT(nmp); 5772 nmp->nm_state |= NFSSTA_NOLAYOUTCOMMIT; 5773 NFSUNLOCKMNT(nmp); 5774 break; 5775 } 5776 } 5777 } 5778 } 5779 5780 /* 5781 * Commit all layouts for a file (vnode). 5782 */ 5783 int 5784 nfscl_layoutcommit(vnode_t vp, NFSPROC_T *p) 5785 { 5786 struct nfsclclient *clp; 5787 struct nfscllayout *lyp; 5788 struct nfsnode *np = VTONFS(vp); 5789 mount_t mp; 5790 struct nfsmount *nmp; 5791 5792 mp = vp->v_mount; 5793 nmp = VFSTONFS(mp); 5794 if (NFSHASNOLAYOUTCOMMIT(nmp)) 5795 return (0); 5796 NFSLOCKCLSTATE(); 5797 clp = nmp->nm_clp; 5798 if (clp == NULL) { 5799 NFSUNLOCKCLSTATE(); 5800 return (EPERM); 5801 } 5802 lyp = nfscl_findlayout(clp, np->n_fhp->nfh_fh, np->n_fhp->nfh_len); 5803 if (lyp == NULL) { 5804 NFSUNLOCKCLSTATE(); 5805 return (EPERM); 5806 } 5807 nfsv4_getref(&lyp->nfsly_lock, NULL, NFSCLSTATEMUTEXPTR, mp); 5808 if (NFSCL_FORCEDISM(mp)) { 5809 NFSUNLOCKCLSTATE(); 5810 return (EPERM); 5811 } 5812 tryagain: 5813 if ((lyp->nfsly_flags & NFSLY_WRITTEN) != 0) { 5814 lyp->nfsly_flags &= ~NFSLY_WRITTEN; 5815 NFSUNLOCKCLSTATE(); 5816 NFSCL_DEBUG(4, "do layoutcommit2\n"); 5817 nfscl_dolayoutcommit(clp->nfsc_nmp, lyp, NFSPROCCRED(p), p); 5818 NFSLOCKCLSTATE(); 5819 goto tryagain; 5820 } 5821 nfsv4_relref(&lyp->nfsly_lock); 5822 NFSUNLOCKCLSTATE(); 5823 return (0); 5824 } 5825