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 || taglen > NFSV4_OPAQUELIMIT) { 3535 error = EBADRPC; 3536 taglen = -1; 3537 goto nfsmout; 3538 } 3539 if (taglen <= NFSV4_SMALLSTR) 3540 tagstr = tag; 3541 else 3542 tagstr = malloc(taglen + 1, M_TEMP, M_WAITOK); 3543 error = nfsrv_mtostr(nd, tagstr, taglen); 3544 if (error) { 3545 if (taglen > NFSV4_SMALLSTR) 3546 free(tagstr, M_TEMP); 3547 taglen = -1; 3548 goto nfsmout; 3549 } 3550 (void) nfsm_strtom(nd, tag, taglen); 3551 if (taglen > NFSV4_SMALLSTR) { 3552 free(tagstr, M_TEMP); 3553 } 3554 NFSM_BUILD(retopsp, u_int32_t *, NFSX_UNSIGNED); 3555 NFSM_DISSECT(tl, u_int32_t *, 3 * NFSX_UNSIGNED); 3556 minorvers = fxdr_unsigned(u_int32_t, *tl++); 3557 if (minorvers != NFSV4_MINORVERSION && 3558 minorvers != NFSV41_MINORVERSION && 3559 minorvers != NFSV42_MINORVERSION) 3560 nd->nd_repstat = NFSERR_MINORVERMISMATCH; 3561 cbident = fxdr_unsigned(u_int32_t, *tl++); 3562 if (nd->nd_repstat) 3563 numops = 0; 3564 else 3565 numops = fxdr_unsigned(int, *tl); 3566 /* 3567 * Loop around doing the sub ops. 3568 */ 3569 for (i = 0; i < numops; i++) { 3570 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED); 3571 NFSM_BUILD(repp, u_int32_t *, 2 * NFSX_UNSIGNED); 3572 *repp++ = *tl; 3573 op = fxdr_unsigned(int, *tl); 3574 nd->nd_procnum = op; 3575 if (i == 0 && op != NFSV4OP_CBSEQUENCE && minorvers != 3576 NFSV4_MINORVERSION) { 3577 nd->nd_repstat = NFSERR_OPNOTINSESS; 3578 *repp = nfscl_errmap(nd, minorvers); 3579 retops++; 3580 break; 3581 } 3582 if (op < NFSV4OP_CBGETATTR || 3583 (op > NFSV4OP_CBRECALL && minorvers == NFSV4_MINORVERSION) || 3584 (op > NFSV4OP_CBNOTIFYDEVID && 3585 minorvers == NFSV41_MINORVERSION) || 3586 (op > NFSV4OP_CBOFFLOAD && 3587 minorvers == NFSV42_MINORVERSION)) { 3588 nd->nd_repstat = NFSERR_OPILLEGAL; 3589 *repp = nfscl_errmap(nd, minorvers); 3590 retops++; 3591 break; 3592 } 3593 if (op < NFSV42_CBNOPS) 3594 nfsstatsv1.cbrpccnt[nd->nd_procnum]++; 3595 switch (op) { 3596 case NFSV4OP_CBGETATTR: 3597 NFSCL_DEBUG(4, "cbgetattr\n"); 3598 mp = NULL; 3599 vp = NULL; 3600 error = nfsm_getfh(nd, &nfhp); 3601 if (!error) 3602 error = nfsrv_getattrbits(nd, &attrbits, 3603 NULL, NULL); 3604 if (!error) { 3605 mp = nfscl_getmnt(minorvers, sessionid, cbident, 3606 &clp); 3607 if (mp == NULL) 3608 error = NFSERR_SERVERFAULT; 3609 } 3610 if (!error) { 3611 error = nfscl_ngetreopen(mp, nfhp->nfh_fh, 3612 nfhp->nfh_len, p, &np); 3613 if (!error) 3614 vp = NFSTOV(np); 3615 } 3616 if (!error) { 3617 NFSZERO_ATTRBIT(&rattrbits); 3618 NFSLOCKCLSTATE(); 3619 dp = nfscl_finddeleg(clp, nfhp->nfh_fh, 3620 nfhp->nfh_len); 3621 if (dp != NULL) { 3622 if (NFSISSET_ATTRBIT(&attrbits, 3623 NFSATTRBIT_SIZE)) { 3624 if (vp != NULL) 3625 va.va_size = np->n_size; 3626 else 3627 va.va_size = 3628 dp->nfsdl_size; 3629 NFSSETBIT_ATTRBIT(&rattrbits, 3630 NFSATTRBIT_SIZE); 3631 } 3632 if (NFSISSET_ATTRBIT(&attrbits, 3633 NFSATTRBIT_CHANGE)) { 3634 va.va_filerev = 3635 dp->nfsdl_change; 3636 if (vp == NULL || 3637 (np->n_flag & NDELEGMOD)) 3638 va.va_filerev++; 3639 NFSSETBIT_ATTRBIT(&rattrbits, 3640 NFSATTRBIT_CHANGE); 3641 } 3642 } else 3643 error = NFSERR_SERVERFAULT; 3644 NFSUNLOCKCLSTATE(); 3645 } 3646 if (vp != NULL) 3647 vrele(vp); 3648 if (mp != NULL) 3649 vfs_unbusy(mp); 3650 if (nfhp != NULL) 3651 free(nfhp, M_NFSFH); 3652 if (!error) 3653 (void) nfsv4_fillattr(nd, NULL, NULL, NULL, &va, 3654 NULL, 0, &rattrbits, NULL, p, 0, 0, 0, 0, 3655 (uint64_t)0, NULL); 3656 break; 3657 case NFSV4OP_CBRECALL: 3658 NFSCL_DEBUG(4, "cbrecall\n"); 3659 NFSM_DISSECT(tl, u_int32_t *, NFSX_STATEID + 3660 NFSX_UNSIGNED); 3661 stateid.seqid = *tl++; 3662 NFSBCOPY((caddr_t)tl, (caddr_t)stateid.other, 3663 NFSX_STATEIDOTHER); 3664 tl += (NFSX_STATEIDOTHER / NFSX_UNSIGNED); 3665 trunc = fxdr_unsigned(int, *tl); 3666 error = nfsm_getfh(nd, &nfhp); 3667 if (!error) { 3668 NFSLOCKCLSTATE(); 3669 if (minorvers == NFSV4_MINORVERSION) 3670 clp = nfscl_getclnt(cbident); 3671 else 3672 clp = nfscl_getclntsess(sessionid); 3673 if (clp != NULL) { 3674 dp = nfscl_finddeleg(clp, nfhp->nfh_fh, 3675 nfhp->nfh_len); 3676 if (dp != NULL && (dp->nfsdl_flags & 3677 NFSCLDL_DELEGRET) == 0) { 3678 dp->nfsdl_flags |= 3679 NFSCLDL_RECALL; 3680 wakeup((caddr_t)clp); 3681 } 3682 } else { 3683 error = NFSERR_SERVERFAULT; 3684 } 3685 NFSUNLOCKCLSTATE(); 3686 } 3687 if (nfhp != NULL) 3688 free(nfhp, M_NFSFH); 3689 break; 3690 case NFSV4OP_CBLAYOUTRECALL: 3691 NFSCL_DEBUG(4, "cblayrec\n"); 3692 nfhp = NULL; 3693 NFSM_DISSECT(tl, uint32_t *, 4 * NFSX_UNSIGNED); 3694 laytype = fxdr_unsigned(int, *tl++); 3695 iomode = fxdr_unsigned(uint32_t, *tl++); 3696 if (newnfs_true == *tl++) 3697 changed = 1; 3698 else 3699 changed = 0; 3700 recalltype = fxdr_unsigned(int, *tl); 3701 NFSCL_DEBUG(4, "layt=%d iom=%d ch=%d rectyp=%d\n", 3702 laytype, iomode, changed, recalltype); 3703 recallp = malloc(sizeof(*recallp), M_NFSLAYRECALL, 3704 M_WAITOK); 3705 if (laytype != NFSLAYOUT_NFSV4_1_FILES && 3706 laytype != NFSLAYOUT_FLEXFILE) 3707 error = NFSERR_NOMATCHLAYOUT; 3708 else if (recalltype == NFSLAYOUTRETURN_FILE) { 3709 error = nfsm_getfh(nd, &nfhp); 3710 NFSCL_DEBUG(4, "retfile getfh=%d\n", error); 3711 if (error != 0) 3712 goto nfsmout; 3713 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_HYPER + 3714 NFSX_STATEID); 3715 off = fxdr_hyper(tl); tl += 2; 3716 len = fxdr_hyper(tl); tl += 2; 3717 stateid.seqid = fxdr_unsigned(uint32_t, *tl++); 3718 NFSBCOPY(tl, stateid.other, NFSX_STATEIDOTHER); 3719 if (minorvers == NFSV4_MINORVERSION) 3720 error = NFSERR_NOTSUPP; 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 if (i != 0) { 3832 error = NFSERR_SEQUENCEPOS; 3833 break; 3834 } 3835 NFSM_DISSECT(tl, uint32_t *, NFSX_V4SESSIONID + 3836 5 * NFSX_UNSIGNED); 3837 bcopy(tl, sessionid, NFSX_V4SESSIONID); 3838 tl += NFSX_V4SESSIONID / NFSX_UNSIGNED; 3839 seqid = fxdr_unsigned(uint32_t, *tl++); 3840 slotid = fxdr_unsigned(uint32_t, *tl++); 3841 highslot = fxdr_unsigned(uint32_t, *tl++); 3842 cachethis = *tl++; 3843 /* Throw away the referring call stuff. */ 3844 clist = fxdr_unsigned(int, *tl); 3845 for (j = 0; j < clist; j++) { 3846 NFSM_DISSECT(tl, uint32_t *, NFSX_V4SESSIONID + 3847 NFSX_UNSIGNED); 3848 tl += NFSX_V4SESSIONID / NFSX_UNSIGNED; 3849 rcalls = fxdr_unsigned(int, *tl); 3850 for (k = 0; k < rcalls; k++) { 3851 NFSM_DISSECT(tl, uint32_t *, 3852 2 * NFSX_UNSIGNED); 3853 } 3854 } 3855 NFSLOCKCLSTATE(); 3856 clp = nfscl_getclntsess(sessionid); 3857 if (clp == NULL) 3858 error = NFSERR_SERVERFAULT; 3859 if (error == 0) { 3860 tsep = nfsmnt_mdssession(clp->nfsc_nmp); 3861 error = nfsv4_seqsession(seqid, slotid, 3862 highslot, tsep->nfsess_cbslots, &rep, 3863 tsep->nfsess_backslots); 3864 } 3865 NFSUNLOCKCLSTATE(); 3866 if (error == 0 || error == NFSERR_REPLYFROMCACHE) { 3867 gotseq_ok = 1; 3868 if (rep != NULL) { 3869 /* 3870 * Handle a reply for a retried 3871 * callback. The reply will be 3872 * re-inserted in the session cache 3873 * by the nfsv4_seqsess_cacherep() call 3874 * after out: 3875 */ 3876 KASSERT(error == NFSERR_REPLYFROMCACHE, 3877 ("cbsequence: non-NULL rep")); 3878 NFSCL_DEBUG(4, "Got cbretry\n"); 3879 m_freem(nd->nd_mreq); 3880 nd->nd_mreq = rep; 3881 rep = NULL; 3882 goto out; 3883 } 3884 NFSM_BUILD(tl, uint32_t *, 3885 NFSX_V4SESSIONID + 4 * NFSX_UNSIGNED); 3886 bcopy(sessionid, tl, NFSX_V4SESSIONID); 3887 tl += NFSX_V4SESSIONID / NFSX_UNSIGNED; 3888 *tl++ = txdr_unsigned(seqid); 3889 *tl++ = txdr_unsigned(slotid); 3890 *tl++ = txdr_unsigned(NFSV4_CBSLOTS - 1); 3891 *tl = txdr_unsigned(NFSV4_CBSLOTS - 1); 3892 } 3893 break; 3894 default: 3895 if (i == 0 && minorvers != NFSV4_MINORVERSION) 3896 error = NFSERR_OPNOTINSESS; 3897 else { 3898 NFSCL_DEBUG(1, "unsupp callback %d\n", op); 3899 error = NFSERR_NOTSUPP; 3900 } 3901 break; 3902 } 3903 if (error) { 3904 if (error == EBADRPC || error == NFSERR_BADXDR) { 3905 nd->nd_repstat = NFSERR_BADXDR; 3906 } else { 3907 nd->nd_repstat = error; 3908 } 3909 error = 0; 3910 } 3911 retops++; 3912 if (nd->nd_repstat) { 3913 *repp = nfscl_errmap(nd, minorvers); 3914 break; 3915 } else 3916 *repp = 0; /* NFS4_OK */ 3917 } 3918 nfsmout: 3919 if (recallp != NULL) 3920 free(recallp, M_NFSLAYRECALL); 3921 if (error) { 3922 if (error == EBADRPC || error == NFSERR_BADXDR) 3923 nd->nd_repstat = NFSERR_BADXDR; 3924 else 3925 printf("nfsv4 comperr1=%d\n", error); 3926 } 3927 if (taglen == -1) { 3928 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED); 3929 *tl++ = 0; 3930 *tl = 0; 3931 } else { 3932 *retopsp = txdr_unsigned(retops); 3933 } 3934 *nd->nd_errp = nfscl_errmap(nd, minorvers); 3935 out: 3936 if (gotseq_ok != 0) { 3937 rep = m_copym(nd->nd_mreq, 0, M_COPYALL, M_WAITOK); 3938 NFSLOCKCLSTATE(); 3939 clp = nfscl_getclntsess(sessionid); 3940 if (clp != NULL) { 3941 tsep = nfsmnt_mdssession(clp->nfsc_nmp); 3942 nfsv4_seqsess_cacherep(slotid, tsep->nfsess_cbslots, 3943 NFSERR_OK, &rep); 3944 NFSUNLOCKCLSTATE(); 3945 } else { 3946 NFSUNLOCKCLSTATE(); 3947 m_freem(rep); 3948 } 3949 } 3950 } 3951 3952 /* 3953 * Generate the next cbident value. Basically just increment a static value 3954 * and then check that it isn't already in the list, if it has wrapped around. 3955 */ 3956 static u_int32_t 3957 nfscl_nextcbident(void) 3958 { 3959 struct nfsclclient *clp; 3960 int matched; 3961 static u_int32_t nextcbident = 0; 3962 static int haswrapped = 0; 3963 3964 nextcbident++; 3965 if (nextcbident == 0) 3966 haswrapped = 1; 3967 if (haswrapped) { 3968 /* 3969 * Search the clientid list for one already using this cbident. 3970 */ 3971 do { 3972 matched = 0; 3973 NFSLOCKCLSTATE(); 3974 LIST_FOREACH(clp, &nfsclhead, nfsc_list) { 3975 if (clp->nfsc_cbident == nextcbident) { 3976 matched = 1; 3977 break; 3978 } 3979 } 3980 NFSUNLOCKCLSTATE(); 3981 if (matched == 1) 3982 nextcbident++; 3983 } while (matched); 3984 } 3985 return (nextcbident); 3986 } 3987 3988 /* 3989 * Get the mount point related to a given cbident or session and busy it. 3990 */ 3991 static mount_t 3992 nfscl_getmnt(int minorvers, uint8_t *sessionid, u_int32_t cbident, 3993 struct nfsclclient **clpp) 3994 { 3995 struct nfsclclient *clp; 3996 mount_t mp; 3997 int error; 3998 struct nfsclsession *tsep; 3999 4000 *clpp = NULL; 4001 NFSLOCKCLSTATE(); 4002 LIST_FOREACH(clp, &nfsclhead, nfsc_list) { 4003 tsep = nfsmnt_mdssession(clp->nfsc_nmp); 4004 if (minorvers == NFSV4_MINORVERSION) { 4005 if (clp->nfsc_cbident == cbident) 4006 break; 4007 } else if (!NFSBCMP(tsep->nfsess_sessionid, sessionid, 4008 NFSX_V4SESSIONID)) 4009 break; 4010 } 4011 if (clp == NULL) { 4012 NFSUNLOCKCLSTATE(); 4013 return (NULL); 4014 } 4015 mp = clp->nfsc_nmp->nm_mountp; 4016 vfs_ref(mp); 4017 NFSUNLOCKCLSTATE(); 4018 error = vfs_busy(mp, 0); 4019 vfs_rel(mp); 4020 if (error != 0) 4021 return (NULL); 4022 *clpp = clp; 4023 return (mp); 4024 } 4025 4026 /* 4027 * Get the clientid pointer related to a given cbident. 4028 */ 4029 static struct nfsclclient * 4030 nfscl_getclnt(u_int32_t cbident) 4031 { 4032 struct nfsclclient *clp; 4033 4034 LIST_FOREACH(clp, &nfsclhead, nfsc_list) 4035 if (clp->nfsc_cbident == cbident) 4036 break; 4037 return (clp); 4038 } 4039 4040 /* 4041 * Get the clientid pointer related to a given sessionid. 4042 */ 4043 static struct nfsclclient * 4044 nfscl_getclntsess(uint8_t *sessionid) 4045 { 4046 struct nfsclclient *clp; 4047 struct nfsclsession *tsep; 4048 4049 LIST_FOREACH(clp, &nfsclhead, nfsc_list) { 4050 tsep = nfsmnt_mdssession(clp->nfsc_nmp); 4051 if (!NFSBCMP(tsep->nfsess_sessionid, sessionid, 4052 NFSX_V4SESSIONID)) 4053 break; 4054 } 4055 return (clp); 4056 } 4057 4058 /* 4059 * Search for a lock conflict locally on the client. A conflict occurs if 4060 * - not same owner and overlapping byte range and at least one of them is 4061 * a write lock or this is an unlock. 4062 */ 4063 static int 4064 nfscl_localconflict(struct nfsclclient *clp, u_int8_t *fhp, int fhlen, 4065 struct nfscllock *nlop, u_int8_t *own, struct nfscldeleg *dp, 4066 struct nfscllock **lopp) 4067 { 4068 struct nfsclopen *op; 4069 int ret; 4070 4071 if (dp != NULL) { 4072 ret = nfscl_checkconflict(&dp->nfsdl_lock, nlop, own, lopp); 4073 if (ret) 4074 return (ret); 4075 } 4076 LIST_FOREACH(op, NFSCLOPENHASH(clp, fhp, fhlen), nfso_hash) { 4077 if (op->nfso_fhlen == fhlen && 4078 !NFSBCMP(op->nfso_fh, fhp, fhlen)) { 4079 ret = nfscl_checkconflict(&op->nfso_lock, nlop, 4080 own, lopp); 4081 if (ret) 4082 return (ret); 4083 } 4084 } 4085 return (0); 4086 } 4087 4088 static int 4089 nfscl_checkconflict(struct nfscllockownerhead *lhp, struct nfscllock *nlop, 4090 u_int8_t *own, struct nfscllock **lopp) 4091 { 4092 struct nfscllockowner *lp; 4093 struct nfscllock *lop; 4094 4095 LIST_FOREACH(lp, lhp, nfsl_list) { 4096 if (NFSBCMP(lp->nfsl_owner, own, NFSV4CL_LOCKNAMELEN)) { 4097 LIST_FOREACH(lop, &lp->nfsl_lock, nfslo_list) { 4098 if (lop->nfslo_first >= nlop->nfslo_end) 4099 break; 4100 if (lop->nfslo_end <= nlop->nfslo_first) 4101 continue; 4102 if (lop->nfslo_type == F_WRLCK || 4103 nlop->nfslo_type == F_WRLCK || 4104 nlop->nfslo_type == F_UNLCK) { 4105 if (lopp != NULL) 4106 *lopp = lop; 4107 return (NFSERR_DENIED); 4108 } 4109 } 4110 } 4111 } 4112 return (0); 4113 } 4114 4115 /* 4116 * Check for a local conflicting lock. 4117 */ 4118 int 4119 nfscl_lockt(vnode_t vp, struct nfsclclient *clp, u_int64_t off, 4120 u_int64_t len, struct flock *fl, NFSPROC_T *p, void *id, int flags) 4121 { 4122 struct nfscllock *lop, nlck; 4123 struct nfscldeleg *dp; 4124 struct nfsnode *np; 4125 u_int8_t own[NFSV4CL_LOCKNAMELEN]; 4126 int error; 4127 4128 nlck.nfslo_type = fl->l_type; 4129 nlck.nfslo_first = off; 4130 if (len == NFS64BITSSET) { 4131 nlck.nfslo_end = NFS64BITSSET; 4132 } else { 4133 nlck.nfslo_end = off + len; 4134 if (nlck.nfslo_end <= nlck.nfslo_first) 4135 return (NFSERR_INVAL); 4136 } 4137 np = VTONFS(vp); 4138 nfscl_filllockowner(id, own, flags); 4139 NFSLOCKCLSTATE(); 4140 dp = nfscl_finddeleg(clp, np->n_fhp->nfh_fh, np->n_fhp->nfh_len); 4141 error = nfscl_localconflict(clp, np->n_fhp->nfh_fh, np->n_fhp->nfh_len, 4142 &nlck, own, dp, &lop); 4143 if (error != 0) { 4144 fl->l_whence = SEEK_SET; 4145 fl->l_start = lop->nfslo_first; 4146 if (lop->nfslo_end == NFS64BITSSET) 4147 fl->l_len = 0; 4148 else 4149 fl->l_len = lop->nfslo_end - lop->nfslo_first; 4150 fl->l_pid = (pid_t)0; 4151 fl->l_type = lop->nfslo_type; 4152 error = -1; /* no RPC required */ 4153 } else if (dp != NULL && ((dp->nfsdl_flags & NFSCLDL_WRITE) || 4154 fl->l_type == F_RDLCK)) { 4155 /* 4156 * The delegation ensures that there isn't a conflicting 4157 * lock on the server, so return -1 to indicate an RPC 4158 * isn't required. 4159 */ 4160 fl->l_type = F_UNLCK; 4161 error = -1; 4162 } 4163 NFSUNLOCKCLSTATE(); 4164 return (error); 4165 } 4166 4167 /* 4168 * Handle Recall of a delegation. 4169 * The clp must be exclusive locked when this is called. 4170 */ 4171 static int 4172 nfscl_recalldeleg(struct nfsclclient *clp, struct nfsmount *nmp, 4173 struct nfscldeleg *dp, vnode_t vp, struct ucred *cred, NFSPROC_T *p, 4174 int called_from_renewthread, vnode_t *vpp) 4175 { 4176 struct nfsclowner *owp, *lowp, *nowp; 4177 struct nfsclopen *op, *lop; 4178 struct nfscllockowner *lp; 4179 struct nfscllock *lckp; 4180 struct nfsnode *np; 4181 int error = 0, ret; 4182 4183 if (vp == NULL) { 4184 KASSERT(vpp != NULL, ("nfscl_recalldeleg: vpp NULL")); 4185 *vpp = NULL; 4186 /* 4187 * First, get a vnode for the file. This is needed to do RPCs. 4188 */ 4189 ret = nfscl_ngetreopen(nmp->nm_mountp, dp->nfsdl_fh, 4190 dp->nfsdl_fhlen, p, &np); 4191 if (ret) { 4192 /* 4193 * File isn't open, so nothing to move over to the 4194 * server. 4195 */ 4196 return (0); 4197 } 4198 vp = NFSTOV(np); 4199 *vpp = vp; 4200 } else { 4201 np = VTONFS(vp); 4202 } 4203 dp->nfsdl_flags &= ~NFSCLDL_MODTIMESET; 4204 4205 /* 4206 * Ok, if it's a write delegation, flush data to the server, so 4207 * that close/open consistency is retained. 4208 */ 4209 ret = 0; 4210 NFSLOCKNODE(np); 4211 if ((dp->nfsdl_flags & NFSCLDL_WRITE) && (np->n_flag & NMODIFIED)) { 4212 np->n_flag |= NDELEGRECALL; 4213 NFSUNLOCKNODE(np); 4214 ret = ncl_flush(vp, MNT_WAIT, p, 1, called_from_renewthread); 4215 NFSLOCKNODE(np); 4216 np->n_flag &= ~NDELEGRECALL; 4217 } 4218 NFSINVALATTRCACHE(np); 4219 NFSUNLOCKNODE(np); 4220 if (ret == EIO && called_from_renewthread != 0) { 4221 /* 4222 * If the flush failed with EIO for the renew thread, 4223 * return now, so that the dirty buffer will be flushed 4224 * later. 4225 */ 4226 return (ret); 4227 } 4228 4229 /* 4230 * Now, for each openowner with opens issued locally, move them 4231 * over to state against the server. 4232 */ 4233 LIST_FOREACH(lowp, &dp->nfsdl_owner, nfsow_list) { 4234 lop = LIST_FIRST(&lowp->nfsow_open); 4235 if (lop != NULL) { 4236 if (LIST_NEXT(lop, nfso_list) != NULL) 4237 panic("nfsdlg mult opens"); 4238 /* 4239 * Look for the same openowner against the server. 4240 */ 4241 LIST_FOREACH(owp, &clp->nfsc_owner, nfsow_list) { 4242 if (!NFSBCMP(lowp->nfsow_owner, 4243 owp->nfsow_owner, NFSV4CL_LOCKNAMELEN)) { 4244 newnfs_copycred(&dp->nfsdl_cred, cred); 4245 ret = nfscl_moveopen(vp, clp, nmp, lop, 4246 owp, dp, cred, p); 4247 if (ret == NFSERR_STALECLIENTID || 4248 ret == NFSERR_STALEDONTRECOVER || 4249 ret == NFSERR_BADSESSION) 4250 return (ret); 4251 if (ret) { 4252 nfscl_freeopen(lop, 1, true); 4253 if (!error) 4254 error = ret; 4255 } 4256 break; 4257 } 4258 } 4259 4260 /* 4261 * If no openowner found, create one and get an open 4262 * for it. 4263 */ 4264 if (owp == NULL) { 4265 nowp = malloc( 4266 sizeof (struct nfsclowner), M_NFSCLOWNER, 4267 M_WAITOK); 4268 nfscl_newopen(clp, NULL, &owp, &nowp, &op, 4269 NULL, lowp->nfsow_owner, dp->nfsdl_fh, 4270 dp->nfsdl_fhlen, NULL, NULL); 4271 newnfs_copycred(&dp->nfsdl_cred, cred); 4272 ret = nfscl_moveopen(vp, clp, nmp, lop, 4273 owp, dp, cred, p); 4274 if (ret) { 4275 nfscl_freeopenowner(owp, 0); 4276 if (ret == NFSERR_STALECLIENTID || 4277 ret == NFSERR_STALEDONTRECOVER || 4278 ret == NFSERR_BADSESSION) 4279 return (ret); 4280 if (ret) { 4281 nfscl_freeopen(lop, 1, true); 4282 if (!error) 4283 error = ret; 4284 } 4285 } 4286 } 4287 } 4288 } 4289 4290 /* 4291 * Now, get byte range locks for any locks done locally. 4292 */ 4293 LIST_FOREACH(lp, &dp->nfsdl_lock, nfsl_list) { 4294 LIST_FOREACH(lckp, &lp->nfsl_lock, nfslo_list) { 4295 newnfs_copycred(&dp->nfsdl_cred, cred); 4296 ret = nfscl_relock(vp, clp, nmp, lp, lckp, cred, p); 4297 if (ret == NFSERR_STALESTATEID || 4298 ret == NFSERR_STALEDONTRECOVER || 4299 ret == NFSERR_STALECLIENTID || 4300 ret == NFSERR_BADSESSION) 4301 return (ret); 4302 if (ret && !error) 4303 error = ret; 4304 } 4305 } 4306 return (error); 4307 } 4308 4309 /* 4310 * Move a locally issued open over to an owner on the state list. 4311 * SIDE EFFECT: If it needs to sleep (do an rpc), it unlocks clstate and 4312 * returns with it unlocked. 4313 */ 4314 static int 4315 nfscl_moveopen(vnode_t vp, struct nfsclclient *clp, struct nfsmount *nmp, 4316 struct nfsclopen *lop, struct nfsclowner *owp, struct nfscldeleg *dp, 4317 struct ucred *cred, NFSPROC_T *p) 4318 { 4319 struct nfsclopen *op, *nop; 4320 struct nfscldeleg *ndp; 4321 struct nfsnode *np; 4322 int error = 0, newone; 4323 4324 /* 4325 * First, look for an appropriate open, If found, just increment the 4326 * opencnt in it. 4327 */ 4328 LIST_FOREACH(op, &owp->nfsow_open, nfso_list) { 4329 if ((op->nfso_mode & lop->nfso_mode) == lop->nfso_mode && 4330 op->nfso_fhlen == lop->nfso_fhlen && 4331 !NFSBCMP(op->nfso_fh, lop->nfso_fh, op->nfso_fhlen)) { 4332 op->nfso_opencnt += lop->nfso_opencnt; 4333 nfscl_freeopen(lop, 1, true); 4334 return (0); 4335 } 4336 } 4337 4338 /* No appropriate open, so we have to do one against the server. */ 4339 np = VTONFS(vp); 4340 nop = malloc(sizeof (struct nfsclopen) + 4341 lop->nfso_fhlen - 1, M_NFSCLOPEN, M_WAITOK); 4342 nop->nfso_hash.le_prev = NULL; 4343 newone = 0; 4344 nfscl_newopen(clp, NULL, &owp, NULL, &op, &nop, owp->nfsow_owner, 4345 lop->nfso_fh, lop->nfso_fhlen, cred, &newone); 4346 ndp = dp; 4347 error = nfscl_tryopen(nmp, vp, np->n_v4->n4_data, np->n_v4->n4_fhlen, 4348 lop->nfso_fh, lop->nfso_fhlen, lop->nfso_mode, op, 4349 NFS4NODENAME(np->n_v4), np->n_v4->n4_namelen, &ndp, 0, 0, cred, p); 4350 if (error) { 4351 if (newone) 4352 nfscl_freeopen(op, 0, true); 4353 } else { 4354 op->nfso_mode |= lop->nfso_mode; 4355 op->nfso_opencnt += lop->nfso_opencnt; 4356 nfscl_freeopen(lop, 1, true); 4357 } 4358 if (nop != NULL) 4359 free(nop, M_NFSCLOPEN); 4360 if (ndp != NULL) { 4361 /* 4362 * What should I do with the returned delegation, since the 4363 * delegation is being recalled? For now, just printf and 4364 * through it away. 4365 */ 4366 printf("Moveopen returned deleg\n"); 4367 free(ndp, M_NFSCLDELEG); 4368 } 4369 return (error); 4370 } 4371 4372 /* 4373 * Recall all delegations on this client. 4374 */ 4375 static void 4376 nfscl_totalrecall(struct nfsclclient *clp) 4377 { 4378 struct nfscldeleg *dp; 4379 4380 TAILQ_FOREACH(dp, &clp->nfsc_deleg, nfsdl_list) { 4381 if ((dp->nfsdl_flags & NFSCLDL_DELEGRET) == 0) 4382 dp->nfsdl_flags |= NFSCLDL_RECALL; 4383 } 4384 } 4385 4386 /* 4387 * Relock byte ranges. Called for delegation recall and state expiry. 4388 */ 4389 static int 4390 nfscl_relock(vnode_t vp, struct nfsclclient *clp, struct nfsmount *nmp, 4391 struct nfscllockowner *lp, struct nfscllock *lop, struct ucred *cred, 4392 NFSPROC_T *p) 4393 { 4394 struct nfscllockowner *nlp; 4395 struct nfsfh *nfhp; 4396 struct nfsnode *np; 4397 u_int64_t off, len; 4398 int error, newone, donelocally; 4399 4400 if (NFSHASNFSV4N(nmp) && NFSHASONEOPENOWN(nmp)) { 4401 np = VTONFS(vp); 4402 NFSLOCKNODE(np); 4403 np->n_flag |= NMIGHTBELOCKED; 4404 NFSUNLOCKNODE(np); 4405 } 4406 4407 off = lop->nfslo_first; 4408 len = lop->nfslo_end - lop->nfslo_first; 4409 error = nfscl_getbytelock(vp, off, len, lop->nfslo_type, cred, p, 4410 clp, 1, NULL, lp->nfsl_lockflags, lp->nfsl_owner, 4411 lp->nfsl_openowner, &nlp, &newone, &donelocally); 4412 if (error || donelocally) 4413 return (error); 4414 nfhp = VTONFS(vp)->n_fhp; 4415 error = nfscl_trylock(nmp, vp, nfhp->nfh_fh, 4416 nfhp->nfh_len, nlp, newone, 0, off, 4417 len, lop->nfslo_type, cred, p); 4418 if (error) 4419 nfscl_freelockowner(nlp, 0); 4420 return (error); 4421 } 4422 4423 /* 4424 * Called to re-open a file. Basically get a vnode for the file handle 4425 * and then call nfsrpc_openrpc() to do the rest. 4426 */ 4427 static int 4428 nfsrpc_reopen(struct nfsmount *nmp, u_int8_t *fhp, int fhlen, 4429 u_int32_t mode, struct nfsclopen *op, struct nfscldeleg **dpp, 4430 struct ucred *cred, NFSPROC_T *p) 4431 { 4432 struct nfsnode *np; 4433 vnode_t vp; 4434 int error; 4435 4436 error = nfscl_ngetreopen(nmp->nm_mountp, fhp, fhlen, p, &np); 4437 if (error) 4438 return (error); 4439 vp = NFSTOV(np); 4440 if (np->n_v4 != NULL) { 4441 error = nfscl_tryopen(nmp, vp, np->n_v4->n4_data, 4442 np->n_v4->n4_fhlen, fhp, fhlen, mode, op, 4443 NFS4NODENAME(np->n_v4), np->n_v4->n4_namelen, dpp, 0, 0, 4444 cred, p); 4445 } else { 4446 error = EINVAL; 4447 } 4448 vrele(vp); 4449 return (error); 4450 } 4451 4452 /* 4453 * Try an open against the server. Just call nfsrpc_openrpc(), retrying while 4454 * NFSERR_DELAY. Also, try system credentials, if the passed in credentials 4455 * fail. 4456 */ 4457 static int 4458 nfscl_tryopen(struct nfsmount *nmp, vnode_t vp, u_int8_t *fhp, int fhlen, 4459 u_int8_t *newfhp, int newfhlen, u_int32_t mode, struct nfsclopen *op, 4460 u_int8_t *name, int namelen, struct nfscldeleg **ndpp, 4461 int reclaim, u_int32_t delegtype, struct ucred *cred, NFSPROC_T *p) 4462 { 4463 int error; 4464 4465 do { 4466 error = nfsrpc_openrpc(nmp, vp, fhp, fhlen, newfhp, newfhlen, 4467 mode, op, name, namelen, ndpp, reclaim, delegtype, cred, p, 4468 0, 0); 4469 if (error == NFSERR_DELAY) 4470 (void) nfs_catnap(PZERO, error, "nfstryop"); 4471 } while (error == NFSERR_DELAY); 4472 if (error == EAUTH || error == EACCES) { 4473 /* Try again using system credentials */ 4474 newnfs_setroot(cred); 4475 do { 4476 error = nfsrpc_openrpc(nmp, vp, fhp, fhlen, newfhp, 4477 newfhlen, mode, op, name, namelen, ndpp, reclaim, 4478 delegtype, cred, p, 1, 0); 4479 if (error == NFSERR_DELAY) 4480 (void) nfs_catnap(PZERO, error, "nfstryop"); 4481 } while (error == NFSERR_DELAY); 4482 } 4483 return (error); 4484 } 4485 4486 /* 4487 * Try a byte range lock. Just loop on nfsrpc_lock() while it returns 4488 * NFSERR_DELAY. Also, retry with system credentials, if the provided 4489 * cred don't work. 4490 */ 4491 static int 4492 nfscl_trylock(struct nfsmount *nmp, vnode_t vp, u_int8_t *fhp, 4493 int fhlen, struct nfscllockowner *nlp, int newone, int reclaim, 4494 u_int64_t off, u_int64_t len, short type, struct ucred *cred, NFSPROC_T *p) 4495 { 4496 struct nfsrv_descript nfsd, *nd = &nfsd; 4497 int error; 4498 4499 do { 4500 error = nfsrpc_lock(nd, nmp, vp, fhp, fhlen, nlp, newone, 4501 reclaim, off, len, type, cred, p, 0); 4502 if (!error && nd->nd_repstat == NFSERR_DELAY) 4503 (void) nfs_catnap(PZERO, (int)nd->nd_repstat, 4504 "nfstrylck"); 4505 } while (!error && nd->nd_repstat == NFSERR_DELAY); 4506 if (!error) 4507 error = nd->nd_repstat; 4508 if (error == EAUTH || error == EACCES) { 4509 /* Try again using root credentials */ 4510 newnfs_setroot(cred); 4511 do { 4512 error = nfsrpc_lock(nd, nmp, vp, fhp, fhlen, nlp, 4513 newone, reclaim, off, len, type, cred, p, 1); 4514 if (!error && nd->nd_repstat == NFSERR_DELAY) 4515 (void) nfs_catnap(PZERO, (int)nd->nd_repstat, 4516 "nfstrylck"); 4517 } while (!error && nd->nd_repstat == NFSERR_DELAY); 4518 if (!error) 4519 error = nd->nd_repstat; 4520 } 4521 return (error); 4522 } 4523 4524 /* 4525 * Try a delegreturn against the server. Just call nfsrpc_delegreturn(), 4526 * retrying while NFSERR_DELAY. Also, try system credentials, if the passed in 4527 * credentials fail. 4528 */ 4529 int 4530 nfscl_trydelegreturn(struct nfscldeleg *dp, struct ucred *cred, 4531 struct nfsmount *nmp, NFSPROC_T *p) 4532 { 4533 int error; 4534 4535 do { 4536 error = nfsrpc_delegreturn(dp, cred, nmp, p, 0); 4537 if (error == NFSERR_DELAY) 4538 (void) nfs_catnap(PZERO, error, "nfstrydp"); 4539 } while (error == NFSERR_DELAY); 4540 if (error == EAUTH || error == EACCES) { 4541 /* Try again using system credentials */ 4542 newnfs_setroot(cred); 4543 do { 4544 error = nfsrpc_delegreturn(dp, cred, nmp, p, 1); 4545 if (error == NFSERR_DELAY) 4546 (void) nfs_catnap(PZERO, error, "nfstrydp"); 4547 } while (error == NFSERR_DELAY); 4548 } 4549 return (error); 4550 } 4551 4552 /* 4553 * Try a close against the server. Just call nfsrpc_closerpc(), 4554 * retrying while NFSERR_DELAY. Also, try system credentials, if the passed in 4555 * credentials fail. 4556 */ 4557 int 4558 nfscl_tryclose(struct nfsclopen *op, struct ucred *cred, 4559 struct nfsmount *nmp, NFSPROC_T *p, bool loop_on_delayed) 4560 { 4561 struct nfsrv_descript nfsd, *nd = &nfsd; 4562 int error; 4563 4564 do { 4565 error = nfsrpc_closerpc(nd, nmp, op, cred, p, 0); 4566 if (loop_on_delayed && error == NFSERR_DELAY) 4567 (void) nfs_catnap(PZERO, error, "nfstrycl"); 4568 } while (loop_on_delayed && error == NFSERR_DELAY); 4569 if (error == EAUTH || error == EACCES) { 4570 /* Try again using system credentials */ 4571 newnfs_setroot(cred); 4572 do { 4573 error = nfsrpc_closerpc(nd, nmp, op, cred, p, 1); 4574 if (loop_on_delayed && error == NFSERR_DELAY) 4575 (void) nfs_catnap(PZERO, error, "nfstrycl"); 4576 } while (loop_on_delayed && error == NFSERR_DELAY); 4577 } 4578 return (error); 4579 } 4580 4581 /* 4582 * Decide if a delegation on a file permits close without flushing writes 4583 * to the server. This might be a big performance win in some environments. 4584 * (Not useful until the client does caching on local stable storage.) 4585 */ 4586 int 4587 nfscl_mustflush(vnode_t vp) 4588 { 4589 struct nfsclclient *clp; 4590 struct nfscldeleg *dp; 4591 struct nfsnode *np; 4592 struct nfsmount *nmp; 4593 4594 np = VTONFS(vp); 4595 nmp = VFSTONFS(vp->v_mount); 4596 if (!NFSHASNFSV4(nmp)) 4597 return (1); 4598 NFSLOCKMNT(nmp); 4599 if ((nmp->nm_privflag & NFSMNTP_DELEGISSUED) == 0) { 4600 NFSUNLOCKMNT(nmp); 4601 return (1); 4602 } 4603 NFSUNLOCKMNT(nmp); 4604 NFSLOCKCLSTATE(); 4605 clp = nfscl_findcl(nmp); 4606 if (clp == NULL) { 4607 NFSUNLOCKCLSTATE(); 4608 return (1); 4609 } 4610 dp = nfscl_finddeleg(clp, np->n_fhp->nfh_fh, np->n_fhp->nfh_len); 4611 if (dp != NULL && (dp->nfsdl_flags & 4612 (NFSCLDL_WRITE | NFSCLDL_RECALL | NFSCLDL_DELEGRET)) == 4613 NFSCLDL_WRITE && 4614 (dp->nfsdl_sizelimit >= np->n_size || 4615 !NFSHASSTRICT3530(nmp))) { 4616 NFSUNLOCKCLSTATE(); 4617 return (0); 4618 } 4619 NFSUNLOCKCLSTATE(); 4620 return (1); 4621 } 4622 4623 /* 4624 * See if a (write) delegation exists for this file. 4625 */ 4626 int 4627 nfscl_nodeleg(vnode_t vp, int writedeleg) 4628 { 4629 struct nfsclclient *clp; 4630 struct nfscldeleg *dp; 4631 struct nfsnode *np; 4632 struct nfsmount *nmp; 4633 4634 np = VTONFS(vp); 4635 nmp = VFSTONFS(vp->v_mount); 4636 if (!NFSHASNFSV4(nmp)) 4637 return (1); 4638 NFSLOCKMNT(nmp); 4639 if ((nmp->nm_privflag & NFSMNTP_DELEGISSUED) == 0) { 4640 NFSUNLOCKMNT(nmp); 4641 return (1); 4642 } 4643 NFSUNLOCKMNT(nmp); 4644 NFSLOCKCLSTATE(); 4645 clp = nfscl_findcl(nmp); 4646 if (clp == NULL) { 4647 NFSUNLOCKCLSTATE(); 4648 return (1); 4649 } 4650 dp = nfscl_finddeleg(clp, np->n_fhp->nfh_fh, np->n_fhp->nfh_len); 4651 if (dp != NULL && 4652 (dp->nfsdl_flags & (NFSCLDL_RECALL | NFSCLDL_DELEGRET)) == 0 && 4653 (writedeleg == 0 || (dp->nfsdl_flags & NFSCLDL_WRITE) == 4654 NFSCLDL_WRITE)) { 4655 NFSUNLOCKCLSTATE(); 4656 return (0); 4657 } 4658 NFSUNLOCKCLSTATE(); 4659 return (1); 4660 } 4661 4662 /* 4663 * Look for an associated delegation that should be DelegReturned. 4664 */ 4665 int 4666 nfscl_removedeleg(vnode_t vp, NFSPROC_T *p, nfsv4stateid_t *stp) 4667 { 4668 struct nfsclclient *clp; 4669 struct nfscldeleg *dp; 4670 struct nfsclowner *owp; 4671 struct nfscllockowner *lp; 4672 struct nfsmount *nmp; 4673 struct mount *mp; 4674 struct ucred *cred; 4675 struct nfsnode *np; 4676 int igotlock = 0, triedrecall = 0, needsrecall, retcnt = 0, islept; 4677 4678 nmp = VFSTONFS(vp->v_mount); 4679 if (NFSHASPNFS(nmp)) 4680 return (retcnt); 4681 NFSLOCKMNT(nmp); 4682 if ((nmp->nm_privflag & NFSMNTP_DELEGISSUED) == 0) { 4683 NFSUNLOCKMNT(nmp); 4684 return (retcnt); 4685 } 4686 NFSUNLOCKMNT(nmp); 4687 np = VTONFS(vp); 4688 mp = nmp->nm_mountp; 4689 NFSLOCKCLSTATE(); 4690 /* 4691 * Loop around waiting for: 4692 * - outstanding I/O operations on delegations to complete 4693 * - for a delegation on vp that has state, lock the client and 4694 * do a recall 4695 * - return delegation with no state 4696 */ 4697 while (1) { 4698 clp = nfscl_findcl(nmp); 4699 if (clp == NULL) { 4700 NFSUNLOCKCLSTATE(); 4701 return (retcnt); 4702 } 4703 dp = nfscl_finddeleg(clp, np->n_fhp->nfh_fh, 4704 np->n_fhp->nfh_len); 4705 if (dp != NULL) { 4706 /* 4707 * Wait for outstanding I/O ops to be done. 4708 */ 4709 if (dp->nfsdl_rwlock.nfslock_usecnt > 0) { 4710 if (igotlock) { 4711 nfsv4_unlock(&clp->nfsc_lock, 0); 4712 igotlock = 0; 4713 } 4714 dp->nfsdl_rwlock.nfslock_lock |= NFSV4LOCK_WANTED; 4715 msleep(&dp->nfsdl_rwlock, NFSCLSTATEMUTEXPTR, PZERO, 4716 "nfscld", hz); 4717 if (NFSCL_FORCEDISM(mp)) { 4718 dp->nfsdl_flags &= ~NFSCLDL_DELEGRET; 4719 NFSUNLOCKCLSTATE(); 4720 return (0); 4721 } 4722 continue; 4723 } 4724 needsrecall = 0; 4725 LIST_FOREACH(owp, &dp->nfsdl_owner, nfsow_list) { 4726 if (!LIST_EMPTY(&owp->nfsow_open)) { 4727 needsrecall = 1; 4728 break; 4729 } 4730 } 4731 if (!needsrecall) { 4732 LIST_FOREACH(lp, &dp->nfsdl_lock, nfsl_list) { 4733 if (!LIST_EMPTY(&lp->nfsl_lock)) { 4734 needsrecall = 1; 4735 break; 4736 } 4737 } 4738 } 4739 if (needsrecall && !triedrecall) { 4740 dp->nfsdl_flags |= NFSCLDL_DELEGRET; 4741 islept = 0; 4742 while (!igotlock) { 4743 igotlock = nfsv4_lock(&clp->nfsc_lock, 1, 4744 &islept, NFSCLSTATEMUTEXPTR, mp); 4745 if (NFSCL_FORCEDISM(mp)) { 4746 dp->nfsdl_flags &= ~NFSCLDL_DELEGRET; 4747 if (igotlock) 4748 nfsv4_unlock(&clp->nfsc_lock, 0); 4749 NFSUNLOCKCLSTATE(); 4750 return (0); 4751 } 4752 if (islept) 4753 break; 4754 } 4755 if (islept) 4756 continue; 4757 NFSUNLOCKCLSTATE(); 4758 cred = newnfs_getcred(); 4759 newnfs_copycred(&dp->nfsdl_cred, cred); 4760 nfscl_recalldeleg(clp, nmp, dp, vp, cred, p, 0, NULL); 4761 NFSFREECRED(cred); 4762 triedrecall = 1; 4763 NFSLOCKCLSTATE(); 4764 nfsv4_unlock(&clp->nfsc_lock, 0); 4765 igotlock = 0; 4766 continue; 4767 } 4768 *stp = dp->nfsdl_stateid; 4769 retcnt = 1; 4770 nfscl_cleandeleg(dp); 4771 nfscl_freedeleg(&clp->nfsc_deleg, dp, true); 4772 } 4773 if (igotlock) 4774 nfsv4_unlock(&clp->nfsc_lock, 0); 4775 NFSUNLOCKCLSTATE(); 4776 return (retcnt); 4777 } 4778 } 4779 4780 /* 4781 * Look for associated delegation(s) that should be DelegReturned. 4782 */ 4783 int 4784 nfscl_renamedeleg(vnode_t fvp, nfsv4stateid_t *fstp, int *gotfdp, vnode_t tvp, 4785 nfsv4stateid_t *tstp, int *gottdp, NFSPROC_T *p) 4786 { 4787 struct nfsclclient *clp; 4788 struct nfscldeleg *dp; 4789 struct nfsclowner *owp; 4790 struct nfscllockowner *lp; 4791 struct nfsmount *nmp; 4792 struct mount *mp; 4793 struct ucred *cred; 4794 struct nfsnode *np; 4795 int igotlock = 0, triedrecall = 0, needsrecall, retcnt = 0, islept; 4796 4797 nmp = VFSTONFS(fvp->v_mount); 4798 *gotfdp = 0; 4799 *gottdp = 0; 4800 if (NFSHASPNFS(nmp)) 4801 return (retcnt); 4802 NFSLOCKMNT(nmp); 4803 if ((nmp->nm_privflag & NFSMNTP_DELEGISSUED) == 0) { 4804 NFSUNLOCKMNT(nmp); 4805 return (retcnt); 4806 } 4807 NFSUNLOCKMNT(nmp); 4808 mp = nmp->nm_mountp; 4809 NFSLOCKCLSTATE(); 4810 /* 4811 * Loop around waiting for: 4812 * - outstanding I/O operations on delegations to complete 4813 * - for a delegation on fvp that has state, lock the client and 4814 * do a recall 4815 * - return delegation(s) with no state. 4816 */ 4817 while (1) { 4818 clp = nfscl_findcl(nmp); 4819 if (clp == NULL) { 4820 NFSUNLOCKCLSTATE(); 4821 return (retcnt); 4822 } 4823 np = VTONFS(fvp); 4824 dp = nfscl_finddeleg(clp, np->n_fhp->nfh_fh, 4825 np->n_fhp->nfh_len); 4826 if (dp != NULL && *gotfdp == 0) { 4827 /* 4828 * Wait for outstanding I/O ops to be done. 4829 */ 4830 if (dp->nfsdl_rwlock.nfslock_usecnt > 0) { 4831 if (igotlock) { 4832 nfsv4_unlock(&clp->nfsc_lock, 0); 4833 igotlock = 0; 4834 } 4835 dp->nfsdl_rwlock.nfslock_lock |= NFSV4LOCK_WANTED; 4836 msleep(&dp->nfsdl_rwlock, NFSCLSTATEMUTEXPTR, PZERO, 4837 "nfscld", hz); 4838 if (NFSCL_FORCEDISM(mp)) { 4839 dp->nfsdl_flags &= ~NFSCLDL_DELEGRET; 4840 NFSUNLOCKCLSTATE(); 4841 *gotfdp = 0; 4842 *gottdp = 0; 4843 return (0); 4844 } 4845 continue; 4846 } 4847 needsrecall = 0; 4848 LIST_FOREACH(owp, &dp->nfsdl_owner, nfsow_list) { 4849 if (!LIST_EMPTY(&owp->nfsow_open)) { 4850 needsrecall = 1; 4851 break; 4852 } 4853 } 4854 if (!needsrecall) { 4855 LIST_FOREACH(lp, &dp->nfsdl_lock, nfsl_list) { 4856 if (!LIST_EMPTY(&lp->nfsl_lock)) { 4857 needsrecall = 1; 4858 break; 4859 } 4860 } 4861 } 4862 if (needsrecall && !triedrecall) { 4863 dp->nfsdl_flags |= NFSCLDL_DELEGRET; 4864 islept = 0; 4865 while (!igotlock) { 4866 igotlock = nfsv4_lock(&clp->nfsc_lock, 1, 4867 &islept, NFSCLSTATEMUTEXPTR, mp); 4868 if (NFSCL_FORCEDISM(mp)) { 4869 dp->nfsdl_flags &= ~NFSCLDL_DELEGRET; 4870 if (igotlock) 4871 nfsv4_unlock(&clp->nfsc_lock, 0); 4872 NFSUNLOCKCLSTATE(); 4873 *gotfdp = 0; 4874 *gottdp = 0; 4875 return (0); 4876 } 4877 if (islept) 4878 break; 4879 } 4880 if (islept) 4881 continue; 4882 NFSUNLOCKCLSTATE(); 4883 cred = newnfs_getcred(); 4884 newnfs_copycred(&dp->nfsdl_cred, cred); 4885 nfscl_recalldeleg(clp, nmp, dp, fvp, cred, p, 0, NULL); 4886 NFSFREECRED(cred); 4887 triedrecall = 1; 4888 NFSLOCKCLSTATE(); 4889 nfsv4_unlock(&clp->nfsc_lock, 0); 4890 igotlock = 0; 4891 continue; 4892 } 4893 *fstp = dp->nfsdl_stateid; 4894 retcnt++; 4895 *gotfdp = 1; 4896 nfscl_cleandeleg(dp); 4897 nfscl_freedeleg(&clp->nfsc_deleg, dp, true); 4898 } 4899 if (igotlock) { 4900 nfsv4_unlock(&clp->nfsc_lock, 0); 4901 igotlock = 0; 4902 } 4903 if (tvp != NULL) { 4904 np = VTONFS(tvp); 4905 dp = nfscl_finddeleg(clp, np->n_fhp->nfh_fh, 4906 np->n_fhp->nfh_len); 4907 if (dp != NULL && *gottdp == 0) { 4908 /* 4909 * Wait for outstanding I/O ops to be done. 4910 */ 4911 if (dp->nfsdl_rwlock.nfslock_usecnt > 0) { 4912 dp->nfsdl_rwlock.nfslock_lock |= NFSV4LOCK_WANTED; 4913 msleep(&dp->nfsdl_rwlock, NFSCLSTATEMUTEXPTR, PZERO, 4914 "nfscld", hz); 4915 if (NFSCL_FORCEDISM(mp)) { 4916 NFSUNLOCKCLSTATE(); 4917 *gotfdp = 0; 4918 *gottdp = 0; 4919 return (0); 4920 } 4921 continue; 4922 } 4923 LIST_FOREACH(owp, &dp->nfsdl_owner, nfsow_list) { 4924 if (!LIST_EMPTY(&owp->nfsow_open)) { 4925 NFSUNLOCKCLSTATE(); 4926 return (retcnt); 4927 } 4928 } 4929 LIST_FOREACH(lp, &dp->nfsdl_lock, nfsl_list) { 4930 if (!LIST_EMPTY(&lp->nfsl_lock)) { 4931 NFSUNLOCKCLSTATE(); 4932 return (retcnt); 4933 } 4934 } 4935 *tstp = dp->nfsdl_stateid; 4936 retcnt++; 4937 *gottdp = 1; 4938 nfscl_cleandeleg(dp); 4939 nfscl_freedeleg(&clp->nfsc_deleg, dp, true); 4940 } 4941 } 4942 NFSUNLOCKCLSTATE(); 4943 return (retcnt); 4944 } 4945 } 4946 4947 /* 4948 * Get a reference on the clientid associated with the mount point. 4949 * Return 1 if success, 0 otherwise. 4950 */ 4951 int 4952 nfscl_getref(struct nfsmount *nmp) 4953 { 4954 struct nfsclclient *clp; 4955 int ret; 4956 4957 NFSLOCKCLSTATE(); 4958 clp = nfscl_findcl(nmp); 4959 if (clp == NULL) { 4960 NFSUNLOCKCLSTATE(); 4961 return (0); 4962 } 4963 nfsv4_getref(&clp->nfsc_lock, NULL, NFSCLSTATEMUTEXPTR, nmp->nm_mountp); 4964 ret = 1; 4965 if (NFSCL_FORCEDISM(nmp->nm_mountp)) 4966 ret = 0; 4967 NFSUNLOCKCLSTATE(); 4968 return (ret); 4969 } 4970 4971 /* 4972 * Release a reference on a clientid acquired with the above call. 4973 */ 4974 void 4975 nfscl_relref(struct nfsmount *nmp) 4976 { 4977 struct nfsclclient *clp; 4978 4979 NFSLOCKCLSTATE(); 4980 clp = nfscl_findcl(nmp); 4981 if (clp == NULL) { 4982 NFSUNLOCKCLSTATE(); 4983 return; 4984 } 4985 nfsv4_relref(&clp->nfsc_lock); 4986 NFSUNLOCKCLSTATE(); 4987 } 4988 4989 /* 4990 * Save the size attribute in the delegation, since the nfsnode 4991 * is going away. 4992 */ 4993 void 4994 nfscl_reclaimnode(vnode_t vp) 4995 { 4996 struct nfsclclient *clp; 4997 struct nfscldeleg *dp; 4998 struct nfsnode *np = VTONFS(vp); 4999 struct nfsmount *nmp; 5000 5001 nmp = VFSTONFS(vp->v_mount); 5002 if (!NFSHASNFSV4(nmp)) 5003 return; 5004 NFSLOCKCLSTATE(); 5005 clp = nfscl_findcl(nmp); 5006 if (clp == NULL) { 5007 NFSUNLOCKCLSTATE(); 5008 return; 5009 } 5010 dp = nfscl_finddeleg(clp, np->n_fhp->nfh_fh, np->n_fhp->nfh_len); 5011 if (dp != NULL && (dp->nfsdl_flags & NFSCLDL_WRITE)) 5012 dp->nfsdl_size = np->n_size; 5013 NFSUNLOCKCLSTATE(); 5014 } 5015 5016 /* 5017 * Get the saved size attribute in the delegation, since it is a 5018 * newly allocated nfsnode. 5019 */ 5020 void 5021 nfscl_newnode(vnode_t vp) 5022 { 5023 struct nfsclclient *clp; 5024 struct nfscldeleg *dp; 5025 struct nfsnode *np = VTONFS(vp); 5026 struct nfsmount *nmp; 5027 5028 nmp = VFSTONFS(vp->v_mount); 5029 if (!NFSHASNFSV4(nmp)) 5030 return; 5031 NFSLOCKCLSTATE(); 5032 clp = nfscl_findcl(nmp); 5033 if (clp == NULL) { 5034 NFSUNLOCKCLSTATE(); 5035 return; 5036 } 5037 dp = nfscl_finddeleg(clp, np->n_fhp->nfh_fh, np->n_fhp->nfh_len); 5038 if (dp != NULL && (dp->nfsdl_flags & NFSCLDL_WRITE)) 5039 np->n_size = dp->nfsdl_size; 5040 NFSUNLOCKCLSTATE(); 5041 } 5042 5043 /* 5044 * If there is a valid write delegation for this file, set the modtime 5045 * to the local clock time. 5046 */ 5047 void 5048 nfscl_delegmodtime(vnode_t vp) 5049 { 5050 struct nfsclclient *clp; 5051 struct nfscldeleg *dp; 5052 struct nfsnode *np = VTONFS(vp); 5053 struct nfsmount *nmp; 5054 5055 nmp = VFSTONFS(vp->v_mount); 5056 if (!NFSHASNFSV4(nmp)) 5057 return; 5058 NFSLOCKMNT(nmp); 5059 if ((nmp->nm_privflag & NFSMNTP_DELEGISSUED) == 0) { 5060 NFSUNLOCKMNT(nmp); 5061 return; 5062 } 5063 NFSUNLOCKMNT(nmp); 5064 NFSLOCKCLSTATE(); 5065 clp = nfscl_findcl(nmp); 5066 if (clp == NULL) { 5067 NFSUNLOCKCLSTATE(); 5068 return; 5069 } 5070 dp = nfscl_finddeleg(clp, np->n_fhp->nfh_fh, np->n_fhp->nfh_len); 5071 if (dp != NULL && (dp->nfsdl_flags & NFSCLDL_WRITE)) { 5072 nanotime(&dp->nfsdl_modtime); 5073 dp->nfsdl_flags |= NFSCLDL_MODTIMESET; 5074 } 5075 NFSUNLOCKCLSTATE(); 5076 } 5077 5078 /* 5079 * If there is a valid write delegation for this file with a modtime set, 5080 * put that modtime in mtime. 5081 */ 5082 void 5083 nfscl_deleggetmodtime(vnode_t vp, struct timespec *mtime) 5084 { 5085 struct nfsclclient *clp; 5086 struct nfscldeleg *dp; 5087 struct nfsnode *np = VTONFS(vp); 5088 struct nfsmount *nmp; 5089 5090 nmp = VFSTONFS(vp->v_mount); 5091 if (!NFSHASNFSV4(nmp)) 5092 return; 5093 NFSLOCKMNT(nmp); 5094 if ((nmp->nm_privflag & NFSMNTP_DELEGISSUED) == 0) { 5095 NFSUNLOCKMNT(nmp); 5096 return; 5097 } 5098 NFSUNLOCKMNT(nmp); 5099 NFSLOCKCLSTATE(); 5100 clp = nfscl_findcl(nmp); 5101 if (clp == NULL) { 5102 NFSUNLOCKCLSTATE(); 5103 return; 5104 } 5105 dp = nfscl_finddeleg(clp, np->n_fhp->nfh_fh, np->n_fhp->nfh_len); 5106 if (dp != NULL && 5107 (dp->nfsdl_flags & (NFSCLDL_WRITE | NFSCLDL_MODTIMESET)) == 5108 (NFSCLDL_WRITE | NFSCLDL_MODTIMESET)) 5109 *mtime = dp->nfsdl_modtime; 5110 NFSUNLOCKCLSTATE(); 5111 } 5112 5113 static int 5114 nfscl_errmap(struct nfsrv_descript *nd, u_int32_t minorvers) 5115 { 5116 short *defaulterrp, *errp; 5117 5118 if (!nd->nd_repstat) 5119 return (0); 5120 if (nd->nd_procnum == NFSPROC_NOOP) 5121 return (txdr_unsigned(nd->nd_repstat & 0xffff)); 5122 if (nd->nd_repstat == EBADRPC) 5123 return (txdr_unsigned(NFSERR_BADXDR)); 5124 if (nd->nd_repstat == NFSERR_MINORVERMISMATCH || 5125 nd->nd_repstat == NFSERR_OPILLEGAL) 5126 return (txdr_unsigned(nd->nd_repstat)); 5127 if (nd->nd_repstat >= NFSERR_BADIOMODE && nd->nd_repstat < 20000 && 5128 minorvers > NFSV4_MINORVERSION) { 5129 /* NFSv4.n error. */ 5130 return (txdr_unsigned(nd->nd_repstat)); 5131 } 5132 if (nd->nd_procnum < NFSV4OP_CBNOPS) 5133 errp = defaulterrp = nfscl_cberrmap[nd->nd_procnum]; 5134 else 5135 return (txdr_unsigned(nd->nd_repstat)); 5136 while (*++errp) 5137 if (*errp == (short)nd->nd_repstat) 5138 return (txdr_unsigned(nd->nd_repstat)); 5139 return (txdr_unsigned(*defaulterrp)); 5140 } 5141 5142 /* 5143 * Called to find/add a layout to a client. 5144 * This function returns the layout with a refcnt (shared lock) upon 5145 * success (returns 0) or with no lock/refcnt on the layout when an 5146 * error is returned. 5147 * If a layout is passed in via lypp, it is locked (exclusively locked). 5148 */ 5149 int 5150 nfscl_layout(struct nfsmount *nmp, vnode_t vp, u_int8_t *fhp, int fhlen, 5151 nfsv4stateid_t *stateidp, int layouttype, int retonclose, 5152 struct nfsclflayouthead *fhlp, struct nfscllayout **lypp, 5153 struct ucred *cred, NFSPROC_T *p) 5154 { 5155 struct nfsclclient *clp; 5156 struct nfscllayout *lyp, *tlyp; 5157 struct nfsclflayout *flp; 5158 struct nfsnode *np = VTONFS(vp); 5159 mount_t mp; 5160 int layout_passed_in; 5161 5162 mp = nmp->nm_mountp; 5163 layout_passed_in = 1; 5164 tlyp = NULL; 5165 lyp = *lypp; 5166 if (lyp == NULL) { 5167 layout_passed_in = 0; 5168 tlyp = malloc(sizeof(*tlyp) + fhlen - 1, M_NFSLAYOUT, 5169 M_WAITOK | M_ZERO); 5170 } 5171 5172 NFSLOCKCLSTATE(); 5173 clp = nmp->nm_clp; 5174 if (clp == NULL) { 5175 if (layout_passed_in != 0) 5176 nfsv4_unlock(&lyp->nfsly_lock, 0); 5177 NFSUNLOCKCLSTATE(); 5178 if (tlyp != NULL) 5179 free(tlyp, M_NFSLAYOUT); 5180 return (EPERM); 5181 } 5182 if (lyp == NULL) { 5183 /* 5184 * Although no lyp was passed in, another thread might have 5185 * allocated one. If one is found, just increment it's ref 5186 * count and return it. 5187 */ 5188 lyp = nfscl_findlayout(clp, fhp, fhlen); 5189 if (lyp == NULL) { 5190 lyp = tlyp; 5191 tlyp = NULL; 5192 lyp->nfsly_stateid.seqid = stateidp->seqid; 5193 lyp->nfsly_stateid.other[0] = stateidp->other[0]; 5194 lyp->nfsly_stateid.other[1] = stateidp->other[1]; 5195 lyp->nfsly_stateid.other[2] = stateidp->other[2]; 5196 lyp->nfsly_lastbyte = 0; 5197 LIST_INIT(&lyp->nfsly_flayread); 5198 LIST_INIT(&lyp->nfsly_flayrw); 5199 LIST_INIT(&lyp->nfsly_recall); 5200 lyp->nfsly_filesid[0] = np->n_vattr.na_filesid[0]; 5201 lyp->nfsly_filesid[1] = np->n_vattr.na_filesid[1]; 5202 lyp->nfsly_clp = clp; 5203 if (layouttype == NFSLAYOUT_FLEXFILE) 5204 lyp->nfsly_flags = NFSLY_FLEXFILE; 5205 else 5206 lyp->nfsly_flags = NFSLY_FILES; 5207 if (retonclose != 0) 5208 lyp->nfsly_flags |= NFSLY_RETONCLOSE; 5209 lyp->nfsly_fhlen = fhlen; 5210 NFSBCOPY(fhp, lyp->nfsly_fh, fhlen); 5211 TAILQ_INSERT_HEAD(&clp->nfsc_layout, lyp, nfsly_list); 5212 LIST_INSERT_HEAD(NFSCLLAYOUTHASH(clp, fhp, fhlen), lyp, 5213 nfsly_hash); 5214 lyp->nfsly_timestamp = NFSD_MONOSEC + 120; 5215 nfscl_layoutcnt++; 5216 nfsstatsv1.cllayouts++; 5217 } else { 5218 if (retonclose != 0) 5219 lyp->nfsly_flags |= NFSLY_RETONCLOSE; 5220 if (stateidp->seqid > lyp->nfsly_stateid.seqid) 5221 lyp->nfsly_stateid.seqid = stateidp->seqid; 5222 TAILQ_REMOVE(&clp->nfsc_layout, lyp, nfsly_list); 5223 TAILQ_INSERT_HEAD(&clp->nfsc_layout, lyp, nfsly_list); 5224 lyp->nfsly_timestamp = NFSD_MONOSEC + 120; 5225 } 5226 nfsv4_getref(&lyp->nfsly_lock, NULL, NFSCLSTATEMUTEXPTR, mp); 5227 if (NFSCL_FORCEDISM(mp)) { 5228 NFSUNLOCKCLSTATE(); 5229 if (tlyp != NULL) 5230 free(tlyp, M_NFSLAYOUT); 5231 return (EPERM); 5232 } 5233 *lypp = lyp; 5234 } else if (stateidp->seqid > lyp->nfsly_stateid.seqid) 5235 lyp->nfsly_stateid.seqid = stateidp->seqid; 5236 5237 /* Merge the new list of File Layouts into the list. */ 5238 flp = LIST_FIRST(fhlp); 5239 if (flp != NULL) { 5240 if (flp->nfsfl_iomode == NFSLAYOUTIOMODE_READ) 5241 nfscl_mergeflayouts(&lyp->nfsly_flayread, fhlp); 5242 else 5243 nfscl_mergeflayouts(&lyp->nfsly_flayrw, fhlp); 5244 } 5245 if (layout_passed_in != 0) 5246 nfsv4_unlock(&lyp->nfsly_lock, 1); 5247 NFSUNLOCKCLSTATE(); 5248 if (tlyp != NULL) 5249 free(tlyp, M_NFSLAYOUT); 5250 return (0); 5251 } 5252 5253 /* 5254 * Search for a layout by MDS file handle. 5255 * If one is found, it is returned with a refcnt (shared lock) iff 5256 * retflpp returned non-NULL and locked (exclusive locked) iff retflpp is 5257 * returned NULL. 5258 */ 5259 struct nfscllayout * 5260 nfscl_getlayout(struct nfsclclient *clp, uint8_t *fhp, int fhlen, 5261 uint64_t off, uint32_t rwaccess, struct nfsclflayout **retflpp, 5262 int *recalledp) 5263 { 5264 struct nfscllayout *lyp; 5265 mount_t mp; 5266 int error, igotlock; 5267 5268 mp = clp->nfsc_nmp->nm_mountp; 5269 *recalledp = 0; 5270 *retflpp = NULL; 5271 NFSLOCKCLSTATE(); 5272 lyp = nfscl_findlayout(clp, fhp, fhlen); 5273 if (lyp != NULL) { 5274 if ((lyp->nfsly_flags & NFSLY_RECALL) == 0) { 5275 TAILQ_REMOVE(&clp->nfsc_layout, lyp, nfsly_list); 5276 TAILQ_INSERT_HEAD(&clp->nfsc_layout, lyp, nfsly_list); 5277 lyp->nfsly_timestamp = NFSD_MONOSEC + 120; 5278 error = nfscl_findlayoutforio(lyp, off, rwaccess, 5279 retflpp); 5280 if (error == 0) 5281 nfsv4_getref(&lyp->nfsly_lock, NULL, 5282 NFSCLSTATEMUTEXPTR, mp); 5283 else { 5284 do { 5285 igotlock = nfsv4_lock(&lyp->nfsly_lock, 5286 1, NULL, NFSCLSTATEMUTEXPTR, mp); 5287 } while (igotlock == 0 && !NFSCL_FORCEDISM(mp)); 5288 *retflpp = NULL; 5289 } 5290 if (NFSCL_FORCEDISM(mp)) { 5291 lyp = NULL; 5292 *recalledp = 1; 5293 } 5294 } else { 5295 lyp = NULL; 5296 *recalledp = 1; 5297 } 5298 } 5299 NFSUNLOCKCLSTATE(); 5300 return (lyp); 5301 } 5302 5303 /* 5304 * Search for a layout by MDS file handle. If one is found, mark in to be 5305 * recalled, if it already marked "return on close". 5306 */ 5307 static void 5308 nfscl_retoncloselayout(vnode_t vp, struct nfsclclient *clp, uint8_t *fhp, 5309 int fhlen, struct nfsclrecalllayout **recallpp, struct nfscllayout **lypp) 5310 { 5311 struct nfscllayout *lyp; 5312 uint32_t iomode; 5313 5314 *lypp = NULL; 5315 if (vp->v_type != VREG || !NFSHASPNFS(VFSTONFS(vp->v_mount)) || 5316 nfscl_enablecallb == 0 || nfs_numnfscbd == 0 || 5317 (VTONFS(vp)->n_flag & NNOLAYOUT) != 0) 5318 return; 5319 lyp = nfscl_findlayout(clp, fhp, fhlen); 5320 if (lyp != NULL && (lyp->nfsly_flags & NFSLY_RETONCLOSE) != 0) { 5321 if ((lyp->nfsly_flags & NFSLY_RECALL) == 0) { 5322 iomode = 0; 5323 if (!LIST_EMPTY(&lyp->nfsly_flayread)) 5324 iomode |= NFSLAYOUTIOMODE_READ; 5325 if (!LIST_EMPTY(&lyp->nfsly_flayrw)) 5326 iomode |= NFSLAYOUTIOMODE_RW; 5327 nfscl_layoutrecall(NFSLAYOUTRETURN_FILE, lyp, iomode, 5328 0, UINT64_MAX, lyp->nfsly_stateid.seqid, 0, 0, NULL, 5329 *recallpp); 5330 NFSCL_DEBUG(4, "retoncls recall iomode=%d\n", iomode); 5331 *recallpp = NULL; 5332 } 5333 5334 /* Now, wake up renew thread to do LayoutReturn. */ 5335 wakeup(clp); 5336 *lypp = lyp; 5337 } 5338 } 5339 5340 /* 5341 * Mark the layout to be recalled and with an error. 5342 * Also, disable the dsp from further use. 5343 */ 5344 void 5345 nfscl_dserr(uint32_t op, uint32_t stat, struct nfscldevinfo *dp, 5346 struct nfscllayout *lyp, struct nfsclds *dsp) 5347 { 5348 struct nfsclrecalllayout *recallp; 5349 uint32_t iomode; 5350 5351 printf("DS being disabled, error=%d\n", stat); 5352 /* Set up the return of the layout. */ 5353 recallp = malloc(sizeof(*recallp), M_NFSLAYRECALL, M_WAITOK); 5354 iomode = 0; 5355 NFSLOCKCLSTATE(); 5356 if ((lyp->nfsly_flags & NFSLY_RECALL) == 0) { 5357 if (!LIST_EMPTY(&lyp->nfsly_flayread)) 5358 iomode |= NFSLAYOUTIOMODE_READ; 5359 if (!LIST_EMPTY(&lyp->nfsly_flayrw)) 5360 iomode |= NFSLAYOUTIOMODE_RW; 5361 (void)nfscl_layoutrecall(NFSLAYOUTRETURN_FILE, lyp, iomode, 5362 0, UINT64_MAX, lyp->nfsly_stateid.seqid, stat, op, 5363 dp->nfsdi_deviceid, recallp); 5364 NFSUNLOCKCLSTATE(); 5365 NFSCL_DEBUG(4, "nfscl_dserr recall iomode=%d\n", iomode); 5366 } else { 5367 NFSUNLOCKCLSTATE(); 5368 free(recallp, M_NFSLAYRECALL); 5369 } 5370 5371 /* And shut the TCP connection down. */ 5372 nfscl_cancelreqs(dsp); 5373 } 5374 5375 /* 5376 * Cancel all RPCs for this "dsp" by closing the connection. 5377 * Also, mark the session as defunct. 5378 * If NFSCLDS_SAMECONN is set, the connection is shared with other DSs and 5379 * cannot be shut down. 5380 */ 5381 void 5382 nfscl_cancelreqs(struct nfsclds *dsp) 5383 { 5384 struct __rpc_client *cl; 5385 static int non_event; 5386 5387 NFSLOCKDS(dsp); 5388 if ((dsp->nfsclds_flags & (NFSCLDS_CLOSED | NFSCLDS_SAMECONN)) == 0 && 5389 dsp->nfsclds_sockp != NULL && 5390 dsp->nfsclds_sockp->nr_client != NULL) { 5391 dsp->nfsclds_flags |= NFSCLDS_CLOSED; 5392 cl = dsp->nfsclds_sockp->nr_client; 5393 dsp->nfsclds_sess.nfsess_defunct = 1; 5394 NFSUNLOCKDS(dsp); 5395 CLNT_CLOSE(cl); 5396 /* 5397 * This 1sec sleep is done to reduce the number of reconnect 5398 * attempts made on the DS while it has failed. 5399 */ 5400 tsleep(&non_event, PVFS, "ndscls", hz); 5401 return; 5402 } 5403 NFSUNLOCKDS(dsp); 5404 } 5405 5406 /* 5407 * Dereference a layout. 5408 */ 5409 void 5410 nfscl_rellayout(struct nfscllayout *lyp, int exclocked) 5411 { 5412 5413 NFSLOCKCLSTATE(); 5414 if (exclocked != 0) 5415 nfsv4_unlock(&lyp->nfsly_lock, 0); 5416 else 5417 nfsv4_relref(&lyp->nfsly_lock); 5418 NFSUNLOCKCLSTATE(); 5419 } 5420 5421 /* 5422 * Search for a devinfo by deviceid. If one is found, return it after 5423 * acquiring a reference count on it. 5424 */ 5425 struct nfscldevinfo * 5426 nfscl_getdevinfo(struct nfsclclient *clp, uint8_t *deviceid, 5427 struct nfscldevinfo *dip) 5428 { 5429 5430 NFSLOCKCLSTATE(); 5431 if (dip == NULL) 5432 dip = nfscl_finddevinfo(clp, deviceid); 5433 if (dip != NULL) 5434 dip->nfsdi_refcnt++; 5435 NFSUNLOCKCLSTATE(); 5436 return (dip); 5437 } 5438 5439 /* 5440 * Dereference a devinfo structure. 5441 */ 5442 static void 5443 nfscl_reldevinfo_locked(struct nfscldevinfo *dip) 5444 { 5445 5446 dip->nfsdi_refcnt--; 5447 if (dip->nfsdi_refcnt == 0) 5448 wakeup(&dip->nfsdi_refcnt); 5449 } 5450 5451 /* 5452 * Dereference a devinfo structure. 5453 */ 5454 void 5455 nfscl_reldevinfo(struct nfscldevinfo *dip) 5456 { 5457 5458 NFSLOCKCLSTATE(); 5459 nfscl_reldevinfo_locked(dip); 5460 NFSUNLOCKCLSTATE(); 5461 } 5462 5463 /* 5464 * Find a layout for this file handle. Return NULL upon failure. 5465 */ 5466 static struct nfscllayout * 5467 nfscl_findlayout(struct nfsclclient *clp, u_int8_t *fhp, int fhlen) 5468 { 5469 struct nfscllayout *lyp; 5470 5471 LIST_FOREACH(lyp, NFSCLLAYOUTHASH(clp, fhp, fhlen), nfsly_hash) 5472 if (lyp->nfsly_fhlen == fhlen && 5473 !NFSBCMP(lyp->nfsly_fh, fhp, fhlen)) 5474 break; 5475 return (lyp); 5476 } 5477 5478 /* 5479 * Find a devinfo for this deviceid. Return NULL upon failure. 5480 */ 5481 static struct nfscldevinfo * 5482 nfscl_finddevinfo(struct nfsclclient *clp, uint8_t *deviceid) 5483 { 5484 struct nfscldevinfo *dip; 5485 5486 LIST_FOREACH(dip, &clp->nfsc_devinfo, nfsdi_list) 5487 if (NFSBCMP(dip->nfsdi_deviceid, deviceid, NFSX_V4DEVICEID) 5488 == 0) 5489 break; 5490 return (dip); 5491 } 5492 5493 /* 5494 * Merge the new file layout list into the main one, maintaining it in 5495 * increasing offset order. 5496 */ 5497 static void 5498 nfscl_mergeflayouts(struct nfsclflayouthead *fhlp, 5499 struct nfsclflayouthead *newfhlp) 5500 { 5501 struct nfsclflayout *flp, *nflp, *prevflp, *tflp; 5502 5503 flp = LIST_FIRST(fhlp); 5504 prevflp = NULL; 5505 LIST_FOREACH_SAFE(nflp, newfhlp, nfsfl_list, tflp) { 5506 while (flp != NULL && flp->nfsfl_off < nflp->nfsfl_off) { 5507 prevflp = flp; 5508 flp = LIST_NEXT(flp, nfsfl_list); 5509 } 5510 if (prevflp == NULL) 5511 LIST_INSERT_HEAD(fhlp, nflp, nfsfl_list); 5512 else 5513 LIST_INSERT_AFTER(prevflp, nflp, nfsfl_list); 5514 prevflp = nflp; 5515 } 5516 } 5517 5518 /* 5519 * Add this nfscldevinfo to the client, if it doesn't already exist. 5520 * This function consumes the structure pointed at by dip, if not NULL. 5521 */ 5522 int 5523 nfscl_adddevinfo(struct nfsmount *nmp, struct nfscldevinfo *dip, int ind, 5524 struct nfsclflayout *flp) 5525 { 5526 struct nfsclclient *clp; 5527 struct nfscldevinfo *tdip; 5528 uint8_t *dev; 5529 5530 NFSLOCKCLSTATE(); 5531 clp = nmp->nm_clp; 5532 if (clp == NULL) { 5533 NFSUNLOCKCLSTATE(); 5534 if (dip != NULL) 5535 free(dip, M_NFSDEVINFO); 5536 return (ENODEV); 5537 } 5538 if ((flp->nfsfl_flags & NFSFL_FILE) != 0) 5539 dev = flp->nfsfl_dev; 5540 else 5541 dev = flp->nfsfl_ffm[ind].dev; 5542 tdip = nfscl_finddevinfo(clp, dev); 5543 if (tdip != NULL) { 5544 tdip->nfsdi_layoutrefs++; 5545 if ((flp->nfsfl_flags & NFSFL_FILE) != 0) 5546 flp->nfsfl_devp = tdip; 5547 else 5548 flp->nfsfl_ffm[ind].devp = tdip; 5549 nfscl_reldevinfo_locked(tdip); 5550 NFSUNLOCKCLSTATE(); 5551 if (dip != NULL) 5552 free(dip, M_NFSDEVINFO); 5553 return (0); 5554 } 5555 if (dip != NULL) { 5556 LIST_INSERT_HEAD(&clp->nfsc_devinfo, dip, nfsdi_list); 5557 dip->nfsdi_layoutrefs = 1; 5558 if ((flp->nfsfl_flags & NFSFL_FILE) != 0) 5559 flp->nfsfl_devp = dip; 5560 else 5561 flp->nfsfl_ffm[ind].devp = dip; 5562 } 5563 NFSUNLOCKCLSTATE(); 5564 if (dip == NULL) 5565 return (ENODEV); 5566 return (0); 5567 } 5568 5569 /* 5570 * Free up a layout structure and associated file layout structure(s). 5571 */ 5572 void 5573 nfscl_freelayout(struct nfscllayout *layp) 5574 { 5575 struct nfsclflayout *flp, *nflp; 5576 struct nfsclrecalllayout *rp, *nrp; 5577 5578 LIST_FOREACH_SAFE(flp, &layp->nfsly_flayread, nfsfl_list, nflp) { 5579 LIST_REMOVE(flp, nfsfl_list); 5580 nfscl_freeflayout(flp); 5581 } 5582 LIST_FOREACH_SAFE(flp, &layp->nfsly_flayrw, nfsfl_list, nflp) { 5583 LIST_REMOVE(flp, nfsfl_list); 5584 nfscl_freeflayout(flp); 5585 } 5586 LIST_FOREACH_SAFE(rp, &layp->nfsly_recall, nfsrecly_list, nrp) { 5587 LIST_REMOVE(rp, nfsrecly_list); 5588 free(rp, M_NFSLAYRECALL); 5589 } 5590 nfscl_layoutcnt--; 5591 nfsstatsv1.cllayouts--; 5592 free(layp, M_NFSLAYOUT); 5593 } 5594 5595 /* 5596 * Free up a file layout structure. 5597 */ 5598 void 5599 nfscl_freeflayout(struct nfsclflayout *flp) 5600 { 5601 int i, j; 5602 5603 if ((flp->nfsfl_flags & NFSFL_FILE) != 0) { 5604 for (i = 0; i < flp->nfsfl_fhcnt; i++) 5605 free(flp->nfsfl_fh[i], M_NFSFH); 5606 if (flp->nfsfl_devp != NULL) 5607 flp->nfsfl_devp->nfsdi_layoutrefs--; 5608 } 5609 if ((flp->nfsfl_flags & NFSFL_FLEXFILE) != 0) 5610 for (i = 0; i < flp->nfsfl_mirrorcnt; i++) { 5611 for (j = 0; j < flp->nfsfl_ffm[i].fhcnt; j++) 5612 free(flp->nfsfl_ffm[i].fh[j], M_NFSFH); 5613 if (flp->nfsfl_ffm[i].devp != NULL) 5614 flp->nfsfl_ffm[i].devp->nfsdi_layoutrefs--; 5615 } 5616 free(flp, M_NFSFLAYOUT); 5617 } 5618 5619 /* 5620 * Free up a file layout devinfo structure. 5621 */ 5622 void 5623 nfscl_freedevinfo(struct nfscldevinfo *dip) 5624 { 5625 5626 free(dip, M_NFSDEVINFO); 5627 } 5628 5629 /* 5630 * Mark any layouts that match as recalled. 5631 */ 5632 static int 5633 nfscl_layoutrecall(int recalltype, struct nfscllayout *lyp, uint32_t iomode, 5634 uint64_t off, uint64_t len, uint32_t stateseqid, uint32_t stat, uint32_t op, 5635 char *devid, struct nfsclrecalllayout *recallp) 5636 { 5637 struct nfsclrecalllayout *rp, *orp; 5638 5639 recallp->nfsrecly_recalltype = recalltype; 5640 recallp->nfsrecly_iomode = iomode; 5641 recallp->nfsrecly_stateseqid = stateseqid; 5642 recallp->nfsrecly_off = off; 5643 recallp->nfsrecly_len = len; 5644 recallp->nfsrecly_stat = stat; 5645 recallp->nfsrecly_op = op; 5646 if (devid != NULL) 5647 NFSBCOPY(devid, recallp->nfsrecly_devid, NFSX_V4DEVICEID); 5648 /* 5649 * Order the list as file returns first, followed by fsid and any 5650 * returns, both in increasing stateseqid order. 5651 * Note that the seqids wrap around, so 1 is after 0xffffffff. 5652 * (I'm not sure this is correct because I find RFC5661 confusing 5653 * on this, but hopefully it will work ok.) 5654 */ 5655 orp = NULL; 5656 LIST_FOREACH(rp, &lyp->nfsly_recall, nfsrecly_list) { 5657 orp = rp; 5658 if ((recalltype == NFSLAYOUTRETURN_FILE && 5659 (rp->nfsrecly_recalltype != NFSLAYOUTRETURN_FILE || 5660 nfscl_seq(stateseqid, rp->nfsrecly_stateseqid) != 0)) || 5661 (recalltype != NFSLAYOUTRETURN_FILE && 5662 rp->nfsrecly_recalltype != NFSLAYOUTRETURN_FILE && 5663 nfscl_seq(stateseqid, rp->nfsrecly_stateseqid) != 0)) { 5664 LIST_INSERT_BEFORE(rp, recallp, nfsrecly_list); 5665 break; 5666 } 5667 5668 /* 5669 * Put any error return on all the file returns that will 5670 * preceed this one. 5671 */ 5672 if (rp->nfsrecly_recalltype == NFSLAYOUTRETURN_FILE && 5673 stat != 0 && rp->nfsrecly_stat == 0) { 5674 rp->nfsrecly_stat = stat; 5675 rp->nfsrecly_op = op; 5676 if (devid != NULL) 5677 NFSBCOPY(devid, rp->nfsrecly_devid, 5678 NFSX_V4DEVICEID); 5679 } 5680 } 5681 if (rp == NULL) { 5682 if (orp == NULL) 5683 LIST_INSERT_HEAD(&lyp->nfsly_recall, recallp, 5684 nfsrecly_list); 5685 else 5686 LIST_INSERT_AFTER(orp, recallp, nfsrecly_list); 5687 } 5688 lyp->nfsly_flags |= NFSLY_RECALL; 5689 wakeup(lyp->nfsly_clp); 5690 return (0); 5691 } 5692 5693 /* 5694 * Compare the two seqids for ordering. The trick is that the seqids can 5695 * wrap around from 0xffffffff->0, so check for the cases where one 5696 * has wrapped around. 5697 * Return 1 if seqid1 comes before seqid2, 0 otherwise. 5698 */ 5699 static int 5700 nfscl_seq(uint32_t seqid1, uint32_t seqid2) 5701 { 5702 5703 if (seqid2 > seqid1 && (seqid2 - seqid1) >= 0x7fffffff) 5704 /* seqid2 has wrapped around. */ 5705 return (0); 5706 if (seqid1 > seqid2 && (seqid1 - seqid2) >= 0x7fffffff) 5707 /* seqid1 has wrapped around. */ 5708 return (1); 5709 if (seqid1 <= seqid2) 5710 return (1); 5711 return (0); 5712 } 5713 5714 /* 5715 * Do a layout return for each of the recalls. 5716 */ 5717 static void 5718 nfscl_layoutreturn(struct nfsmount *nmp, struct nfscllayout *lyp, 5719 struct ucred *cred, NFSPROC_T *p) 5720 { 5721 struct nfsclrecalllayout *rp; 5722 nfsv4stateid_t stateid; 5723 int layouttype; 5724 5725 NFSBCOPY(lyp->nfsly_stateid.other, stateid.other, NFSX_STATEIDOTHER); 5726 stateid.seqid = lyp->nfsly_stateid.seqid; 5727 if ((lyp->nfsly_flags & NFSLY_FILES) != 0) 5728 layouttype = NFSLAYOUT_NFSV4_1_FILES; 5729 else 5730 layouttype = NFSLAYOUT_FLEXFILE; 5731 LIST_FOREACH(rp, &lyp->nfsly_recall, nfsrecly_list) { 5732 (void)nfsrpc_layoutreturn(nmp, lyp->nfsly_fh, 5733 lyp->nfsly_fhlen, 0, layouttype, 5734 rp->nfsrecly_iomode, rp->nfsrecly_recalltype, 5735 rp->nfsrecly_off, rp->nfsrecly_len, 5736 &stateid, cred, p, rp->nfsrecly_stat, rp->nfsrecly_op, 5737 rp->nfsrecly_devid); 5738 } 5739 } 5740 5741 /* 5742 * Do the layout commit for a file layout. 5743 */ 5744 static void 5745 nfscl_dolayoutcommit(struct nfsmount *nmp, struct nfscllayout *lyp, 5746 struct ucred *cred, NFSPROC_T *p) 5747 { 5748 struct nfsclflayout *flp; 5749 uint64_t len; 5750 int error, layouttype; 5751 5752 if ((lyp->nfsly_flags & NFSLY_FILES) != 0) 5753 layouttype = NFSLAYOUT_NFSV4_1_FILES; 5754 else 5755 layouttype = NFSLAYOUT_FLEXFILE; 5756 LIST_FOREACH(flp, &lyp->nfsly_flayrw, nfsfl_list) { 5757 if (layouttype == NFSLAYOUT_FLEXFILE && 5758 (flp->nfsfl_fflags & NFSFLEXFLAG_NO_LAYOUTCOMMIT) != 0) { 5759 NFSCL_DEBUG(4, "Flex file: no layoutcommit\n"); 5760 /* If not supported, don't bother doing it. */ 5761 NFSLOCKMNT(nmp); 5762 nmp->nm_state |= NFSSTA_NOLAYOUTCOMMIT; 5763 NFSUNLOCKMNT(nmp); 5764 break; 5765 } else if (flp->nfsfl_off <= lyp->nfsly_lastbyte) { 5766 len = flp->nfsfl_end - flp->nfsfl_off; 5767 error = nfsrpc_layoutcommit(nmp, lyp->nfsly_fh, 5768 lyp->nfsly_fhlen, 0, flp->nfsfl_off, len, 5769 lyp->nfsly_lastbyte, &lyp->nfsly_stateid, 5770 layouttype, cred, p, NULL); 5771 NFSCL_DEBUG(4, "layoutcommit err=%d\n", error); 5772 if (error == NFSERR_NOTSUPP) { 5773 /* If not supported, don't bother doing it. */ 5774 NFSLOCKMNT(nmp); 5775 nmp->nm_state |= NFSSTA_NOLAYOUTCOMMIT; 5776 NFSUNLOCKMNT(nmp); 5777 break; 5778 } 5779 } 5780 } 5781 } 5782 5783 /* 5784 * Commit all layouts for a file (vnode). 5785 */ 5786 int 5787 nfscl_layoutcommit(vnode_t vp, NFSPROC_T *p) 5788 { 5789 struct nfsclclient *clp; 5790 struct nfscllayout *lyp; 5791 struct nfsnode *np = VTONFS(vp); 5792 mount_t mp; 5793 struct nfsmount *nmp; 5794 5795 mp = vp->v_mount; 5796 nmp = VFSTONFS(mp); 5797 if (NFSHASNOLAYOUTCOMMIT(nmp)) 5798 return (0); 5799 NFSLOCKCLSTATE(); 5800 clp = nmp->nm_clp; 5801 if (clp == NULL) { 5802 NFSUNLOCKCLSTATE(); 5803 return (EPERM); 5804 } 5805 lyp = nfscl_findlayout(clp, np->n_fhp->nfh_fh, np->n_fhp->nfh_len); 5806 if (lyp == NULL) { 5807 NFSUNLOCKCLSTATE(); 5808 return (EPERM); 5809 } 5810 nfsv4_getref(&lyp->nfsly_lock, NULL, NFSCLSTATEMUTEXPTR, mp); 5811 if (NFSCL_FORCEDISM(mp)) { 5812 NFSUNLOCKCLSTATE(); 5813 return (EPERM); 5814 } 5815 tryagain: 5816 if ((lyp->nfsly_flags & NFSLY_WRITTEN) != 0) { 5817 lyp->nfsly_flags &= ~NFSLY_WRITTEN; 5818 NFSUNLOCKCLSTATE(); 5819 NFSCL_DEBUG(4, "do layoutcommit2\n"); 5820 nfscl_dolayoutcommit(clp->nfsc_nmp, lyp, NFSPROCCRED(p), p); 5821 NFSLOCKCLSTATE(); 5822 goto tryagain; 5823 } 5824 nfsv4_relref(&lyp->nfsly_lock); 5825 NFSUNLOCKCLSTATE(); 5826 return (0); 5827 } 5828