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 int owned; 1646 1647 /* 1648 * Make sure the NFSCLSTATE mutex is held, to avoid races with 1649 * calls in nfscl_renewthread() that do not hold a reference 1650 * count on the nfsclclient and just the mutex. 1651 * The mutex will not be held for calls done with the exclusive 1652 * nfsclclient lock held, in particular, nfscl_hasexpired() 1653 * and nfscl_recalldeleg() might do this. 1654 */ 1655 owned = mtx_owned(NFSCLSTATEMUTEXPTR); 1656 if (owned == 0) 1657 NFSLOCKCLSTATE(); 1658 LIST_REMOVE(owp, nfsow_list); 1659 if (owned == 0) 1660 NFSUNLOCKCLSTATE(); 1661 free(owp, M_NFSCLOWNER); 1662 if (local) 1663 nfsstatsv1.cllocalopenowners--; 1664 else 1665 nfsstatsv1.clopenowners--; 1666 } 1667 1668 /* 1669 * Free up a byte range lock owner structure. 1670 */ 1671 void 1672 nfscl_freelockowner(struct nfscllockowner *lp, int local) 1673 { 1674 struct nfscllock *lop, *nlop; 1675 int owned; 1676 1677 /* 1678 * Make sure the NFSCLSTATE mutex is held, to avoid races with 1679 * calls in nfscl_renewthread() that do not hold a reference 1680 * count on the nfsclclient and just the mutex. 1681 * The mutex will not be held for calls done with the exclusive 1682 * nfsclclient lock held, in particular, nfscl_hasexpired() 1683 * and nfscl_recalldeleg() might do this. 1684 */ 1685 owned = mtx_owned(NFSCLSTATEMUTEXPTR); 1686 if (owned == 0) 1687 NFSLOCKCLSTATE(); 1688 LIST_REMOVE(lp, nfsl_list); 1689 if (owned == 0) 1690 NFSUNLOCKCLSTATE(); 1691 LIST_FOREACH_SAFE(lop, &lp->nfsl_lock, nfslo_list, nlop) { 1692 nfscl_freelock(lop, local); 1693 } 1694 free(lp, M_NFSCLLOCKOWNER); 1695 if (local) 1696 nfsstatsv1.cllocallockowners--; 1697 else 1698 nfsstatsv1.cllockowners--; 1699 } 1700 1701 /* 1702 * Free up a byte range lock structure. 1703 */ 1704 void 1705 nfscl_freelock(struct nfscllock *lop, int local) 1706 { 1707 1708 LIST_REMOVE(lop, nfslo_list); 1709 free(lop, M_NFSCLLOCK); 1710 if (local) 1711 nfsstatsv1.cllocallocks--; 1712 else 1713 nfsstatsv1.cllocks--; 1714 } 1715 1716 /* 1717 * Clean out the state related to a delegation. 1718 */ 1719 static void 1720 nfscl_cleandeleg(struct nfscldeleg *dp) 1721 { 1722 struct nfsclowner *owp, *nowp; 1723 struct nfsclopen *op; 1724 1725 LIST_FOREACH_SAFE(owp, &dp->nfsdl_owner, nfsow_list, nowp) { 1726 op = LIST_FIRST(&owp->nfsow_open); 1727 if (op != NULL) { 1728 if (LIST_NEXT(op, nfso_list) != NULL) 1729 panic("nfscleandel"); 1730 nfscl_freeopen(op, 1, true); 1731 } 1732 nfscl_freeopenowner(owp, 1); 1733 } 1734 nfscl_freealllocks(&dp->nfsdl_lock, 1); 1735 } 1736 1737 /* 1738 * Free a delegation. 1739 */ 1740 static void 1741 nfscl_freedeleg(struct nfscldeleghead *hdp, struct nfscldeleg *dp, bool freeit) 1742 { 1743 1744 TAILQ_REMOVE(hdp, dp, nfsdl_list); 1745 LIST_REMOVE(dp, nfsdl_hash); 1746 if (freeit) 1747 free(dp, M_NFSCLDELEG); 1748 nfsstatsv1.cldelegates--; 1749 nfscl_delegcnt--; 1750 } 1751 1752 /* 1753 * Free up all state related to this client structure. 1754 */ 1755 static void 1756 nfscl_cleanclient(struct nfsclclient *clp) 1757 { 1758 struct nfsclowner *owp, *nowp; 1759 struct nfsclopen *op, *nop; 1760 struct nfscllayout *lyp, *nlyp; 1761 struct nfscldevinfo *dip, *ndip; 1762 1763 TAILQ_FOREACH_SAFE(lyp, &clp->nfsc_layout, nfsly_list, nlyp) 1764 nfscl_freelayout(lyp); 1765 1766 LIST_FOREACH_SAFE(dip, &clp->nfsc_devinfo, nfsdi_list, ndip) 1767 nfscl_freedevinfo(dip); 1768 1769 /* Now, all the OpenOwners, etc. */ 1770 LIST_FOREACH_SAFE(owp, &clp->nfsc_owner, nfsow_list, nowp) { 1771 LIST_FOREACH_SAFE(op, &owp->nfsow_open, nfso_list, nop) { 1772 nfscl_freeopen(op, 0, true); 1773 } 1774 nfscl_freeopenowner(owp, 0); 1775 } 1776 } 1777 1778 /* 1779 * Called when an NFSERR_EXPIRED is received from the server. 1780 */ 1781 static void 1782 nfscl_expireclient(struct nfsclclient *clp, struct nfsmount *nmp, 1783 struct ucred *cred, NFSPROC_T *p) 1784 { 1785 struct nfsclowner *owp, *nowp, *towp; 1786 struct nfsclopen *op, *nop, *top; 1787 struct nfscldeleg *dp, *ndp; 1788 int ret, printed = 0; 1789 1790 /* 1791 * First, merge locally issued Opens into the list for the server. 1792 */ 1793 dp = TAILQ_FIRST(&clp->nfsc_deleg); 1794 while (dp != NULL) { 1795 ndp = TAILQ_NEXT(dp, nfsdl_list); 1796 owp = LIST_FIRST(&dp->nfsdl_owner); 1797 while (owp != NULL) { 1798 nowp = LIST_NEXT(owp, nfsow_list); 1799 op = LIST_FIRST(&owp->nfsow_open); 1800 if (op != NULL) { 1801 if (LIST_NEXT(op, nfso_list) != NULL) 1802 panic("nfsclexp"); 1803 LIST_FOREACH(towp, &clp->nfsc_owner, nfsow_list) { 1804 if (!NFSBCMP(towp->nfsow_owner, owp->nfsow_owner, 1805 NFSV4CL_LOCKNAMELEN)) 1806 break; 1807 } 1808 if (towp != NULL) { 1809 /* Merge opens in */ 1810 LIST_FOREACH(top, &towp->nfsow_open, nfso_list) { 1811 if (top->nfso_fhlen == op->nfso_fhlen && 1812 !NFSBCMP(top->nfso_fh, op->nfso_fh, 1813 op->nfso_fhlen)) { 1814 top->nfso_mode |= op->nfso_mode; 1815 top->nfso_opencnt += op->nfso_opencnt; 1816 break; 1817 } 1818 } 1819 if (top == NULL) { 1820 /* Just add the open to the owner list */ 1821 LIST_REMOVE(op, nfso_list); 1822 op->nfso_own = towp; 1823 LIST_INSERT_HEAD(&towp->nfsow_open, op, nfso_list); 1824 LIST_INSERT_HEAD(NFSCLOPENHASH(clp, op->nfso_fh, 1825 op->nfso_fhlen), op, nfso_hash); 1826 nfsstatsv1.cllocalopens--; 1827 nfsstatsv1.clopens++; 1828 } 1829 } else { 1830 /* Just add the openowner to the client list */ 1831 LIST_REMOVE(owp, nfsow_list); 1832 owp->nfsow_clp = clp; 1833 LIST_INSERT_HEAD(&clp->nfsc_owner, owp, nfsow_list); 1834 LIST_INSERT_HEAD(NFSCLOPENHASH(clp, op->nfso_fh, 1835 op->nfso_fhlen), op, nfso_hash); 1836 nfsstatsv1.cllocalopenowners--; 1837 nfsstatsv1.clopenowners++; 1838 nfsstatsv1.cllocalopens--; 1839 nfsstatsv1.clopens++; 1840 } 1841 } 1842 owp = nowp; 1843 } 1844 if (!printed && !LIST_EMPTY(&dp->nfsdl_lock)) { 1845 printed = 1; 1846 printf("nfsv4 expired locks lost\n"); 1847 } 1848 nfscl_cleandeleg(dp); 1849 nfscl_freedeleg(&clp->nfsc_deleg, dp, true); 1850 dp = ndp; 1851 } 1852 if (!TAILQ_EMPTY(&clp->nfsc_deleg)) 1853 panic("nfsclexp"); 1854 1855 /* 1856 * Now, try and reopen against the server. 1857 */ 1858 LIST_FOREACH_SAFE(owp, &clp->nfsc_owner, nfsow_list, nowp) { 1859 owp->nfsow_seqid = 0; 1860 LIST_FOREACH_SAFE(op, &owp->nfsow_open, nfso_list, nop) { 1861 ret = nfscl_expireopen(clp, op, nmp, cred, p); 1862 if (ret && !printed) { 1863 printed = 1; 1864 printf("nfsv4 expired locks lost\n"); 1865 } 1866 } 1867 if (LIST_EMPTY(&owp->nfsow_open)) 1868 nfscl_freeopenowner(owp, 0); 1869 } 1870 } 1871 1872 /* 1873 * This function must be called after the process represented by "own" has 1874 * exited. Must be called with CLSTATE lock held. 1875 */ 1876 static void 1877 nfscl_cleanup_common(struct nfsclclient *clp, u_int8_t *own) 1878 { 1879 struct nfsclowner *owp, *nowp; 1880 struct nfscllockowner *lp; 1881 struct nfscldeleg *dp; 1882 1883 /* First, get rid of local locks on delegations. */ 1884 TAILQ_FOREACH(dp, &clp->nfsc_deleg, nfsdl_list) { 1885 LIST_FOREACH(lp, &dp->nfsdl_lock, nfsl_list) { 1886 if (!NFSBCMP(lp->nfsl_owner, own, NFSV4CL_LOCKNAMELEN)) { 1887 if ((lp->nfsl_rwlock.nfslock_lock & NFSV4LOCK_WANTED)) 1888 panic("nfscllckw"); 1889 nfscl_freelockowner(lp, 1); 1890 break; 1891 } 1892 } 1893 } 1894 owp = LIST_FIRST(&clp->nfsc_owner); 1895 while (owp != NULL) { 1896 nowp = LIST_NEXT(owp, nfsow_list); 1897 if (!NFSBCMP(owp->nfsow_owner, own, 1898 NFSV4CL_LOCKNAMELEN)) { 1899 /* 1900 * If there are children that haven't closed the 1901 * file descriptors yet, the opens will still be 1902 * here. For that case, let the renew thread clear 1903 * out the OpenOwner later. 1904 */ 1905 if (LIST_EMPTY(&owp->nfsow_open)) 1906 nfscl_freeopenowner(owp, 0); 1907 else 1908 owp->nfsow_defunct = 1; 1909 break; 1910 } 1911 owp = nowp; 1912 } 1913 } 1914 1915 /* 1916 * Find open/lock owners for processes that have exited. 1917 */ 1918 static void 1919 nfscl_cleanupkext(struct nfsclclient *clp, struct nfscllockownerfhhead *lhp) 1920 { 1921 struct nfsclowner *owp, *nowp; 1922 struct nfsclopen *op; 1923 struct nfscllockowner *lp, *nlp; 1924 struct nfscldeleg *dp; 1925 uint8_t own[NFSV4CL_LOCKNAMELEN]; 1926 1927 /* 1928 * All the pidhash locks must be acquired, since they are sx locks 1929 * and must be acquired before the mutexes. The pid(s) that will 1930 * be used aren't known yet, so all the locks need to be acquired. 1931 * Fortunately, this function is only performed once/sec. 1932 */ 1933 pidhash_slockall(); 1934 NFSLOCKCLSTATE(); 1935 LIST_FOREACH_SAFE(owp, &clp->nfsc_owner, nfsow_list, nowp) { 1936 LIST_FOREACH(op, &owp->nfsow_open, nfso_list) { 1937 LIST_FOREACH_SAFE(lp, &op->nfso_lock, nfsl_list, nlp) { 1938 if (LIST_EMPTY(&lp->nfsl_lock)) 1939 nfscl_emptylockowner(lp, lhp); 1940 } 1941 } 1942 if (nfscl_procdoesntexist(owp->nfsow_owner)) { 1943 memcpy(own, owp->nfsow_owner, NFSV4CL_LOCKNAMELEN); 1944 nfscl_cleanup_common(clp, own); 1945 } 1946 } 1947 1948 /* 1949 * For the single open_owner case, these lock owners need to be 1950 * checked to see if they still exist separately. 1951 * This is because nfscl_procdoesntexist() never returns true for 1952 * the single open_owner so that the above doesn't ever call 1953 * nfscl_cleanup_common(). 1954 */ 1955 TAILQ_FOREACH(dp, &clp->nfsc_deleg, nfsdl_list) { 1956 LIST_FOREACH_SAFE(lp, &dp->nfsdl_lock, nfsl_list, nlp) { 1957 if (nfscl_procdoesntexist(lp->nfsl_owner)) { 1958 memcpy(own, lp->nfsl_owner, 1959 NFSV4CL_LOCKNAMELEN); 1960 nfscl_cleanup_common(clp, own); 1961 } 1962 } 1963 } 1964 NFSUNLOCKCLSTATE(); 1965 pidhash_sunlockall(); 1966 } 1967 1968 /* 1969 * Take the empty lock owner and move it to the local lhp list if the 1970 * associated process no longer exists. 1971 */ 1972 static void 1973 nfscl_emptylockowner(struct nfscllockowner *lp, 1974 struct nfscllockownerfhhead *lhp) 1975 { 1976 struct nfscllockownerfh *lfhp, *mylfhp; 1977 struct nfscllockowner *nlp; 1978 int fnd_it; 1979 1980 /* If not a Posix lock owner, just return. */ 1981 if ((lp->nfsl_lockflags & F_POSIX) == 0) 1982 return; 1983 1984 fnd_it = 0; 1985 mylfhp = NULL; 1986 /* 1987 * First, search to see if this lock owner is already in the list. 1988 * If it is, then the associated process no longer exists. 1989 */ 1990 SLIST_FOREACH(lfhp, lhp, nfslfh_list) { 1991 if (lfhp->nfslfh_len == lp->nfsl_open->nfso_fhlen && 1992 !NFSBCMP(lfhp->nfslfh_fh, lp->nfsl_open->nfso_fh, 1993 lfhp->nfslfh_len)) 1994 mylfhp = lfhp; 1995 LIST_FOREACH(nlp, &lfhp->nfslfh_lock, nfsl_list) 1996 if (!NFSBCMP(nlp->nfsl_owner, lp->nfsl_owner, 1997 NFSV4CL_LOCKNAMELEN)) 1998 fnd_it = 1; 1999 } 2000 /* If not found, check if process still exists. */ 2001 if (fnd_it == 0 && nfscl_procdoesntexist(lp->nfsl_owner) == 0) 2002 return; 2003 2004 /* Move the lock owner over to the local list. */ 2005 if (mylfhp == NULL) { 2006 mylfhp = malloc(sizeof(struct nfscllockownerfh), M_TEMP, 2007 M_NOWAIT); 2008 if (mylfhp == NULL) 2009 return; 2010 mylfhp->nfslfh_len = lp->nfsl_open->nfso_fhlen; 2011 NFSBCOPY(lp->nfsl_open->nfso_fh, mylfhp->nfslfh_fh, 2012 mylfhp->nfslfh_len); 2013 LIST_INIT(&mylfhp->nfslfh_lock); 2014 SLIST_INSERT_HEAD(lhp, mylfhp, nfslfh_list); 2015 } 2016 LIST_REMOVE(lp, nfsl_list); 2017 LIST_INSERT_HEAD(&mylfhp->nfslfh_lock, lp, nfsl_list); 2018 } 2019 2020 static int fake_global; /* Used to force visibility of MNTK_UNMOUNTF */ 2021 /* 2022 * Called from nfs umount to free up the clientid. 2023 */ 2024 void 2025 nfscl_umount(struct nfsmount *nmp, NFSPROC_T *p, struct nfscldeleghead *dhp) 2026 { 2027 struct nfsclclient *clp; 2028 struct ucred *cred; 2029 int igotlock; 2030 2031 /* 2032 * For the case that matters, this is the thread that set 2033 * MNTK_UNMOUNTF, so it will see it set. The code that follows is 2034 * done to ensure that any thread executing nfscl_getcl() after 2035 * this time, will see MNTK_UNMOUNTF set. nfscl_getcl() uses the 2036 * mutex for NFSLOCKCLSTATE(), so it is "m" for the following 2037 * explanation, courtesy of Alan Cox. 2038 * What follows is a snippet from Alan Cox's email at: 2039 * https://docs.FreeBSD.org/cgi/mid.cgi?BANLkTikR3d65zPHo9==08ZfJ2vmqZucEvw 2040 * 2041 * 1. Set MNTK_UNMOUNTF 2042 * 2. Acquire a standard FreeBSD mutex "m". 2043 * 3. Update some data structures. 2044 * 4. Release mutex "m". 2045 * 2046 * Then, other threads that acquire "m" after step 4 has occurred will 2047 * see MNTK_UNMOUNTF as set. But, other threads that beat thread X to 2048 * step 2 may or may not see MNTK_UNMOUNTF as set. 2049 */ 2050 NFSLOCKCLSTATE(); 2051 if ((nmp->nm_mountp->mnt_kern_flag & MNTK_UNMOUNTF) != 0) { 2052 fake_global++; 2053 NFSUNLOCKCLSTATE(); 2054 NFSLOCKCLSTATE(); 2055 } 2056 2057 clp = nmp->nm_clp; 2058 if (clp != NULL) { 2059 if ((clp->nfsc_flags & NFSCLFLAGS_INITED) == 0) 2060 panic("nfscl umount"); 2061 2062 /* 2063 * First, handshake with the nfscl renew thread, to terminate 2064 * it. 2065 */ 2066 clp->nfsc_flags |= NFSCLFLAGS_UMOUNT; 2067 while (clp->nfsc_flags & NFSCLFLAGS_HASTHREAD) 2068 (void)mtx_sleep(clp, NFSCLSTATEMUTEXPTR, PWAIT, 2069 "nfsclumnt", hz); 2070 2071 /* 2072 * Now, get the exclusive lock on the client state, so 2073 * that no uses of the state are still in progress. 2074 */ 2075 do { 2076 igotlock = nfsv4_lock(&clp->nfsc_lock, 1, NULL, 2077 NFSCLSTATEMUTEXPTR, NULL); 2078 } while (!igotlock); 2079 NFSUNLOCKCLSTATE(); 2080 2081 /* 2082 * Free up all the state. It will expire on the server, but 2083 * maybe we should do a SetClientId/SetClientIdConfirm so 2084 * the server throws it away? 2085 */ 2086 LIST_REMOVE(clp, nfsc_list); 2087 nfscl_delegreturnall(clp, p, dhp); 2088 cred = newnfs_getcred(); 2089 if (NFSHASNFSV4N(nmp)) { 2090 (void)nfsrpc_destroysession(nmp, clp, cred, p); 2091 (void)nfsrpc_destroyclient(nmp, clp, cred, p); 2092 } else 2093 (void)nfsrpc_setclient(nmp, clp, 0, NULL, cred, p); 2094 nfscl_cleanclient(clp); 2095 nmp->nm_clp = NULL; 2096 NFSFREECRED(cred); 2097 free(clp, M_NFSCLCLIENT); 2098 } else 2099 NFSUNLOCKCLSTATE(); 2100 } 2101 2102 /* 2103 * This function is called when a server replies with NFSERR_STALECLIENTID 2104 * NFSERR_STALESTATEID or NFSERR_BADSESSION. It traverses the clientid lists, 2105 * doing Opens and Locks with reclaim. If these fail, it deletes the 2106 * corresponding state. 2107 */ 2108 static void 2109 nfscl_recover(struct nfsclclient *clp, bool *retokp, struct ucred *cred, 2110 NFSPROC_T *p) 2111 { 2112 struct nfsclowner *owp, *nowp; 2113 struct nfsclopen *op, *nop; 2114 struct nfscllockowner *lp, *nlp; 2115 struct nfscllock *lop, *nlop; 2116 struct nfscldeleg *dp, *ndp, *tdp; 2117 struct nfsmount *nmp; 2118 struct ucred *tcred; 2119 struct nfsclopenhead extra_open; 2120 struct nfscldeleghead extra_deleg; 2121 struct nfsreq *rep; 2122 u_int64_t len; 2123 u_int32_t delegtype = NFSV4OPEN_DELEGATEWRITE, mode; 2124 int i, igotlock = 0, error, trycnt, firstlock; 2125 struct nfscllayout *lyp, *nlyp; 2126 bool recovered_one; 2127 2128 /* 2129 * First, lock the client structure, so everyone else will 2130 * block when trying to use state. 2131 */ 2132 NFSLOCKCLSTATE(); 2133 clp->nfsc_flags |= NFSCLFLAGS_RECVRINPROG; 2134 do { 2135 igotlock = nfsv4_lock(&clp->nfsc_lock, 1, NULL, 2136 NFSCLSTATEMUTEXPTR, NULL); 2137 } while (!igotlock); 2138 NFSUNLOCKCLSTATE(); 2139 2140 nmp = clp->nfsc_nmp; 2141 if (nmp == NULL) 2142 panic("nfscl recover"); 2143 2144 /* 2145 * For now, just get rid of all layouts. There may be a need 2146 * to do LayoutCommit Ops with reclaim == true later. 2147 */ 2148 TAILQ_FOREACH_SAFE(lyp, &clp->nfsc_layout, nfsly_list, nlyp) 2149 nfscl_freelayout(lyp); 2150 TAILQ_INIT(&clp->nfsc_layout); 2151 for (i = 0; i < NFSCLLAYOUTHASHSIZE; i++) 2152 LIST_INIT(&clp->nfsc_layouthash[i]); 2153 2154 trycnt = 5; 2155 tcred = NULL; 2156 do { 2157 error = nfsrpc_setclient(nmp, clp, 1, retokp, cred, p); 2158 } while ((error == NFSERR_STALECLIENTID || 2159 error == NFSERR_BADSESSION || 2160 error == NFSERR_STALEDONTRECOVER) && --trycnt > 0); 2161 if (error) { 2162 NFSLOCKCLSTATE(); 2163 clp->nfsc_flags &= ~(NFSCLFLAGS_RECOVER | 2164 NFSCLFLAGS_RECVRINPROG); 2165 wakeup(&clp->nfsc_flags); 2166 nfsv4_unlock(&clp->nfsc_lock, 0); 2167 NFSUNLOCKCLSTATE(); 2168 return; 2169 } 2170 clp->nfsc_flags |= NFSCLFLAGS_HASCLIENTID; 2171 clp->nfsc_flags &= ~NFSCLFLAGS_RECOVER; 2172 2173 /* 2174 * Mark requests already queued on the server, so that they don't 2175 * initiate another recovery cycle. Any requests already in the 2176 * queue that handle state information will have the old stale 2177 * clientid/stateid and will get a NFSERR_STALESTATEID, 2178 * NFSERR_STALECLIENTID or NFSERR_BADSESSION reply from the server. 2179 * This will be translated to NFSERR_STALEDONTRECOVER when 2180 * R_DONTRECOVER is set. 2181 */ 2182 NFSLOCKREQ(); 2183 TAILQ_FOREACH(rep, &nfsd_reqq, r_chain) { 2184 if (rep->r_nmp == nmp) 2185 rep->r_flags |= R_DONTRECOVER; 2186 } 2187 NFSUNLOCKREQ(); 2188 2189 /* 2190 * If nfsrpc_setclient() returns *retokp == true, 2191 * no more recovery is needed. 2192 */ 2193 if (*retokp) 2194 goto out; 2195 2196 /* 2197 * Now, mark all delegations "need reclaim". 2198 */ 2199 TAILQ_FOREACH(dp, &clp->nfsc_deleg, nfsdl_list) 2200 dp->nfsdl_flags |= NFSCLDL_NEEDRECLAIM; 2201 2202 TAILQ_INIT(&extra_deleg); 2203 LIST_INIT(&extra_open); 2204 /* 2205 * Now traverse the state lists, doing Open and Lock Reclaims. 2206 */ 2207 tcred = newnfs_getcred(); 2208 recovered_one = false; 2209 owp = LIST_FIRST(&clp->nfsc_owner); 2210 while (owp != NULL) { 2211 nowp = LIST_NEXT(owp, nfsow_list); 2212 owp->nfsow_seqid = 0; 2213 op = LIST_FIRST(&owp->nfsow_open); 2214 while (op != NULL) { 2215 nop = LIST_NEXT(op, nfso_list); 2216 if (error != NFSERR_NOGRACE && error != NFSERR_BADSESSION) { 2217 /* Search for a delegation to reclaim with the open */ 2218 TAILQ_FOREACH(dp, &clp->nfsc_deleg, nfsdl_list) { 2219 if (!(dp->nfsdl_flags & NFSCLDL_NEEDRECLAIM)) 2220 continue; 2221 if ((dp->nfsdl_flags & NFSCLDL_WRITE)) { 2222 mode = NFSV4OPEN_ACCESSWRITE; 2223 delegtype = NFSV4OPEN_DELEGATEWRITE; 2224 } else { 2225 mode = NFSV4OPEN_ACCESSREAD; 2226 delegtype = NFSV4OPEN_DELEGATEREAD; 2227 } 2228 if ((op->nfso_mode & mode) == mode && 2229 op->nfso_fhlen == dp->nfsdl_fhlen && 2230 !NFSBCMP(op->nfso_fh, dp->nfsdl_fh, op->nfso_fhlen)) 2231 break; 2232 } 2233 ndp = dp; 2234 if (dp == NULL) 2235 delegtype = NFSV4OPEN_DELEGATENONE; 2236 newnfs_copycred(&op->nfso_cred, tcred); 2237 error = nfscl_tryopen(nmp, NULL, op->nfso_fh, 2238 op->nfso_fhlen, op->nfso_fh, op->nfso_fhlen, 2239 op->nfso_mode, op, NULL, 0, &ndp, 1, delegtype, 2240 tcred, p); 2241 if (!error) { 2242 recovered_one = true; 2243 /* Handle any replied delegation */ 2244 if (ndp != NULL && ((ndp->nfsdl_flags & NFSCLDL_WRITE) 2245 || NFSMNT_RDONLY(nmp->nm_mountp))) { 2246 if ((ndp->nfsdl_flags & NFSCLDL_WRITE)) 2247 mode = NFSV4OPEN_ACCESSWRITE; 2248 else 2249 mode = NFSV4OPEN_ACCESSREAD; 2250 TAILQ_FOREACH(dp, &clp->nfsc_deleg, nfsdl_list) { 2251 if (!(dp->nfsdl_flags & NFSCLDL_NEEDRECLAIM)) 2252 continue; 2253 if ((op->nfso_mode & mode) == mode && 2254 op->nfso_fhlen == dp->nfsdl_fhlen && 2255 !NFSBCMP(op->nfso_fh, dp->nfsdl_fh, 2256 op->nfso_fhlen)) { 2257 dp->nfsdl_stateid = ndp->nfsdl_stateid; 2258 dp->nfsdl_sizelimit = ndp->nfsdl_sizelimit; 2259 dp->nfsdl_ace = ndp->nfsdl_ace; 2260 dp->nfsdl_change = ndp->nfsdl_change; 2261 dp->nfsdl_flags &= ~NFSCLDL_NEEDRECLAIM; 2262 if ((ndp->nfsdl_flags & NFSCLDL_RECALL)) 2263 dp->nfsdl_flags |= NFSCLDL_RECALL; 2264 free(ndp, M_NFSCLDELEG); 2265 ndp = NULL; 2266 break; 2267 } 2268 } 2269 } 2270 if (ndp != NULL) 2271 TAILQ_INSERT_HEAD(&extra_deleg, ndp, nfsdl_list); 2272 2273 /* and reclaim all byte range locks */ 2274 lp = LIST_FIRST(&op->nfso_lock); 2275 while (lp != NULL) { 2276 nlp = LIST_NEXT(lp, nfsl_list); 2277 lp->nfsl_seqid = 0; 2278 firstlock = 1; 2279 lop = LIST_FIRST(&lp->nfsl_lock); 2280 while (lop != NULL) { 2281 nlop = LIST_NEXT(lop, nfslo_list); 2282 if (lop->nfslo_end == NFS64BITSSET) 2283 len = NFS64BITSSET; 2284 else 2285 len = lop->nfslo_end - lop->nfslo_first; 2286 error = nfscl_trylock(nmp, NULL, 2287 op->nfso_fh, op->nfso_fhlen, lp, 2288 firstlock, 1, lop->nfslo_first, len, 2289 lop->nfslo_type, tcred, p); 2290 if (error != 0) 2291 nfscl_freelock(lop, 0); 2292 else 2293 firstlock = 0; 2294 lop = nlop; 2295 } 2296 /* If no locks, but a lockowner, just delete it. */ 2297 if (LIST_EMPTY(&lp->nfsl_lock)) 2298 nfscl_freelockowner(lp, 0); 2299 lp = nlp; 2300 } 2301 } else if (error == NFSERR_NOGRACE && !recovered_one && 2302 NFSHASNFSV4N(nmp)) { 2303 /* 2304 * For NFSv4.1/4.2, the NFSERR_EXPIRED case will 2305 * actually end up here, since the client will do 2306 * a recovery for NFSERR_BADSESSION, but will get 2307 * an NFSERR_NOGRACE reply for the first "reclaim" 2308 * attempt. 2309 * So, call nfscl_expireclient() to recover the 2310 * opens as best we can and then do a reclaim 2311 * complete and return. 2312 */ 2313 nfsrpc_reclaimcomplete(nmp, cred, p); 2314 nfscl_expireclient(clp, nmp, tcred, p); 2315 goto out; 2316 } 2317 } 2318 if (error != 0 && error != NFSERR_BADSESSION) 2319 nfscl_freeopen(op, 0, true); 2320 op = nop; 2321 } 2322 owp = nowp; 2323 } 2324 2325 /* 2326 * Now, try and get any delegations not yet reclaimed by cobbling 2327 * to-gether an appropriate open. 2328 */ 2329 nowp = NULL; 2330 dp = TAILQ_FIRST(&clp->nfsc_deleg); 2331 while (dp != NULL) { 2332 ndp = TAILQ_NEXT(dp, nfsdl_list); 2333 if ((dp->nfsdl_flags & NFSCLDL_NEEDRECLAIM)) { 2334 if (nowp == NULL) { 2335 nowp = malloc( 2336 sizeof (struct nfsclowner), M_NFSCLOWNER, M_WAITOK); 2337 /* 2338 * Name must be as long an largest possible 2339 * NFSV4CL_LOCKNAMELEN. 12 for now. 2340 */ 2341 NFSBCOPY("RECLAIMDELEG", nowp->nfsow_owner, 2342 NFSV4CL_LOCKNAMELEN); 2343 LIST_INIT(&nowp->nfsow_open); 2344 nowp->nfsow_clp = clp; 2345 nowp->nfsow_seqid = 0; 2346 nowp->nfsow_defunct = 0; 2347 nfscl_lockinit(&nowp->nfsow_rwlock); 2348 } 2349 nop = NULL; 2350 if (error != NFSERR_NOGRACE && error != NFSERR_BADSESSION) { 2351 nop = malloc(sizeof (struct nfsclopen) + 2352 dp->nfsdl_fhlen - 1, M_NFSCLOPEN, M_WAITOK); 2353 nop->nfso_own = nowp; 2354 if ((dp->nfsdl_flags & NFSCLDL_WRITE)) { 2355 nop->nfso_mode = NFSV4OPEN_ACCESSWRITE; 2356 delegtype = NFSV4OPEN_DELEGATEWRITE; 2357 } else { 2358 nop->nfso_mode = NFSV4OPEN_ACCESSREAD; 2359 delegtype = NFSV4OPEN_DELEGATEREAD; 2360 } 2361 nop->nfso_opencnt = 0; 2362 nop->nfso_posixlock = 1; 2363 nop->nfso_fhlen = dp->nfsdl_fhlen; 2364 NFSBCOPY(dp->nfsdl_fh, nop->nfso_fh, dp->nfsdl_fhlen); 2365 LIST_INIT(&nop->nfso_lock); 2366 nop->nfso_stateid.seqid = 0; 2367 nop->nfso_stateid.other[0] = 0; 2368 nop->nfso_stateid.other[1] = 0; 2369 nop->nfso_stateid.other[2] = 0; 2370 newnfs_copycred(&dp->nfsdl_cred, tcred); 2371 newnfs_copyincred(tcred, &nop->nfso_cred); 2372 tdp = NULL; 2373 error = nfscl_tryopen(nmp, NULL, nop->nfso_fh, 2374 nop->nfso_fhlen, nop->nfso_fh, nop->nfso_fhlen, 2375 nop->nfso_mode, nop, NULL, 0, &tdp, 1, 2376 delegtype, tcred, p); 2377 if (tdp != NULL) { 2378 if ((tdp->nfsdl_flags & NFSCLDL_WRITE)) 2379 mode = NFSV4OPEN_ACCESSWRITE; 2380 else 2381 mode = NFSV4OPEN_ACCESSREAD; 2382 if ((nop->nfso_mode & mode) == mode && 2383 nop->nfso_fhlen == tdp->nfsdl_fhlen && 2384 !NFSBCMP(nop->nfso_fh, tdp->nfsdl_fh, 2385 nop->nfso_fhlen)) { 2386 dp->nfsdl_stateid = tdp->nfsdl_stateid; 2387 dp->nfsdl_sizelimit = tdp->nfsdl_sizelimit; 2388 dp->nfsdl_ace = tdp->nfsdl_ace; 2389 dp->nfsdl_change = tdp->nfsdl_change; 2390 dp->nfsdl_flags &= ~NFSCLDL_NEEDRECLAIM; 2391 if ((tdp->nfsdl_flags & NFSCLDL_RECALL)) 2392 dp->nfsdl_flags |= NFSCLDL_RECALL; 2393 free(tdp, M_NFSCLDELEG); 2394 } else { 2395 TAILQ_INSERT_HEAD(&extra_deleg, tdp, nfsdl_list); 2396 } 2397 } 2398 } 2399 if (error) { 2400 if (nop != NULL) 2401 free(nop, M_NFSCLOPEN); 2402 if (error == NFSERR_NOGRACE && !recovered_one && 2403 NFSHASNFSV4N(nmp)) { 2404 /* 2405 * For NFSv4.1/4.2, the NFSERR_EXPIRED case will 2406 * actually end up here, since the client will do 2407 * a recovery for NFSERR_BADSESSION, but will get 2408 * an NFSERR_NOGRACE reply for the first "reclaim" 2409 * attempt. 2410 * So, call nfscl_expireclient() to recover the 2411 * opens as best we can and then do a reclaim 2412 * complete and return. 2413 */ 2414 nfsrpc_reclaimcomplete(nmp, cred, p); 2415 nfscl_expireclient(clp, nmp, tcred, p); 2416 free(nowp, M_NFSCLOWNER); 2417 goto out; 2418 } 2419 /* 2420 * Couldn't reclaim it, so throw the state 2421 * away. Ouch!! 2422 */ 2423 nfscl_cleandeleg(dp); 2424 nfscl_freedeleg(&clp->nfsc_deleg, dp, true); 2425 } else { 2426 recovered_one = true; 2427 LIST_INSERT_HEAD(&extra_open, nop, nfso_list); 2428 } 2429 } 2430 dp = ndp; 2431 } 2432 2433 /* 2434 * Now, get rid of extra Opens and Delegations. 2435 */ 2436 LIST_FOREACH_SAFE(op, &extra_open, nfso_list, nop) { 2437 do { 2438 newnfs_copycred(&op->nfso_cred, tcred); 2439 error = nfscl_tryclose(op, tcred, nmp, p, true); 2440 if (error == NFSERR_GRACE) 2441 (void) nfs_catnap(PZERO, error, "nfsexcls"); 2442 } while (error == NFSERR_GRACE); 2443 LIST_REMOVE(op, nfso_list); 2444 free(op, M_NFSCLOPEN); 2445 } 2446 if (nowp != NULL) 2447 free(nowp, M_NFSCLOWNER); 2448 2449 TAILQ_FOREACH_SAFE(dp, &extra_deleg, nfsdl_list, ndp) { 2450 do { 2451 newnfs_copycred(&dp->nfsdl_cred, tcred); 2452 error = nfscl_trydelegreturn(dp, tcred, nmp, p); 2453 if (error == NFSERR_GRACE) 2454 (void) nfs_catnap(PZERO, error, "nfsexdlg"); 2455 } while (error == NFSERR_GRACE); 2456 TAILQ_REMOVE(&extra_deleg, dp, nfsdl_list); 2457 free(dp, M_NFSCLDELEG); 2458 } 2459 2460 /* For NFSv4.1 or later, do a RECLAIM_COMPLETE. */ 2461 if (NFSHASNFSV4N(nmp)) 2462 (void)nfsrpc_reclaimcomplete(nmp, cred, p); 2463 2464 out: 2465 NFSLOCKCLSTATE(); 2466 clp->nfsc_flags &= ~NFSCLFLAGS_RECVRINPROG; 2467 wakeup(&clp->nfsc_flags); 2468 nfsv4_unlock(&clp->nfsc_lock, 0); 2469 NFSUNLOCKCLSTATE(); 2470 if (tcred != NULL) 2471 NFSFREECRED(tcred); 2472 } 2473 2474 /* 2475 * This function is called when a server replies with NFSERR_EXPIRED. 2476 * It deletes all state for the client and does a fresh SetClientId/confirm. 2477 * XXX Someday it should post a signal to the process(es) that hold the 2478 * state, so they know that lock state has been lost. 2479 */ 2480 int 2481 nfscl_hasexpired(struct nfsclclient *clp, u_int32_t clidrev, NFSPROC_T *p) 2482 { 2483 struct nfsmount *nmp; 2484 struct ucred *cred; 2485 int igotlock = 0, error, trycnt; 2486 2487 /* 2488 * If the clientid has gone away or a new SetClientid has already 2489 * been done, just return ok. 2490 */ 2491 if (clp == NULL || clidrev != clp->nfsc_clientidrev) 2492 return (0); 2493 2494 /* 2495 * First, lock the client structure, so everyone else will 2496 * block when trying to use state. Also, use NFSCLFLAGS_EXPIREIT so 2497 * that only one thread does the work. 2498 */ 2499 NFSLOCKCLSTATE(); 2500 clp->nfsc_flags |= NFSCLFLAGS_EXPIREIT; 2501 do { 2502 igotlock = nfsv4_lock(&clp->nfsc_lock, 1, NULL, 2503 NFSCLSTATEMUTEXPTR, NULL); 2504 } while (!igotlock && (clp->nfsc_flags & NFSCLFLAGS_EXPIREIT)); 2505 if ((clp->nfsc_flags & NFSCLFLAGS_EXPIREIT) == 0) { 2506 if (igotlock) 2507 nfsv4_unlock(&clp->nfsc_lock, 0); 2508 NFSUNLOCKCLSTATE(); 2509 return (0); 2510 } 2511 clp->nfsc_flags |= NFSCLFLAGS_RECVRINPROG; 2512 NFSUNLOCKCLSTATE(); 2513 2514 nmp = clp->nfsc_nmp; 2515 if (nmp == NULL) 2516 panic("nfscl expired"); 2517 cred = newnfs_getcred(); 2518 trycnt = 5; 2519 do { 2520 error = nfsrpc_setclient(nmp, clp, 0, NULL, cred, p); 2521 } while ((error == NFSERR_STALECLIENTID || 2522 error == NFSERR_BADSESSION || 2523 error == NFSERR_STALEDONTRECOVER) && --trycnt > 0); 2524 if (error) { 2525 NFSLOCKCLSTATE(); 2526 clp->nfsc_flags &= ~NFSCLFLAGS_RECOVER; 2527 } else { 2528 /* 2529 * Expire the state for the client. 2530 */ 2531 nfscl_expireclient(clp, nmp, cred, p); 2532 NFSLOCKCLSTATE(); 2533 clp->nfsc_flags |= NFSCLFLAGS_HASCLIENTID; 2534 clp->nfsc_flags &= ~NFSCLFLAGS_RECOVER; 2535 } 2536 clp->nfsc_flags &= ~(NFSCLFLAGS_EXPIREIT | NFSCLFLAGS_RECVRINPROG); 2537 wakeup(&clp->nfsc_flags); 2538 nfsv4_unlock(&clp->nfsc_lock, 0); 2539 NFSUNLOCKCLSTATE(); 2540 NFSFREECRED(cred); 2541 return (error); 2542 } 2543 2544 /* 2545 * This function inserts a lock in the list after insert_lop. 2546 */ 2547 static void 2548 nfscl_insertlock(struct nfscllockowner *lp, struct nfscllock *new_lop, 2549 struct nfscllock *insert_lop, int local) 2550 { 2551 2552 if ((struct nfscllockowner *)insert_lop == lp) 2553 LIST_INSERT_HEAD(&lp->nfsl_lock, new_lop, nfslo_list); 2554 else 2555 LIST_INSERT_AFTER(insert_lop, new_lop, nfslo_list); 2556 if (local) 2557 nfsstatsv1.cllocallocks++; 2558 else 2559 nfsstatsv1.cllocks++; 2560 } 2561 2562 /* 2563 * This function updates the locking for a lock owner and given file. It 2564 * maintains a list of lock ranges ordered on increasing file offset that 2565 * are NFSCLLOCK_READ or NFSCLLOCK_WRITE and non-overlapping (aka POSIX style). 2566 * It always adds new_lop to the list and sometimes uses the one pointed 2567 * at by other_lopp. 2568 * Returns 1 if the locks were modified, 0 otherwise. 2569 */ 2570 static int 2571 nfscl_updatelock(struct nfscllockowner *lp, struct nfscllock **new_lopp, 2572 struct nfscllock **other_lopp, int local) 2573 { 2574 struct nfscllock *new_lop = *new_lopp; 2575 struct nfscllock *lop, *tlop, *ilop; 2576 struct nfscllock *other_lop; 2577 int unlock = 0, modified = 0; 2578 u_int64_t tmp; 2579 2580 /* 2581 * Work down the list until the lock is merged. 2582 */ 2583 if (new_lop->nfslo_type == F_UNLCK) 2584 unlock = 1; 2585 ilop = (struct nfscllock *)lp; 2586 lop = LIST_FIRST(&lp->nfsl_lock); 2587 while (lop != NULL) { 2588 /* 2589 * Only check locks for this file that aren't before the start of 2590 * new lock's range. 2591 */ 2592 if (lop->nfslo_end >= new_lop->nfslo_first) { 2593 if (new_lop->nfslo_end < lop->nfslo_first) { 2594 /* 2595 * If the new lock ends before the start of the 2596 * current lock's range, no merge, just insert 2597 * the new lock. 2598 */ 2599 break; 2600 } 2601 if (new_lop->nfslo_type == lop->nfslo_type || 2602 (new_lop->nfslo_first <= lop->nfslo_first && 2603 new_lop->nfslo_end >= lop->nfslo_end)) { 2604 /* 2605 * This lock can be absorbed by the new lock/unlock. 2606 * This happens when it covers the entire range 2607 * of the old lock or is contiguous 2608 * with the old lock and is of the same type or an 2609 * unlock. 2610 */ 2611 if (new_lop->nfslo_type != lop->nfslo_type || 2612 new_lop->nfslo_first != lop->nfslo_first || 2613 new_lop->nfslo_end != lop->nfslo_end) 2614 modified = 1; 2615 if (lop->nfslo_first < new_lop->nfslo_first) 2616 new_lop->nfslo_first = lop->nfslo_first; 2617 if (lop->nfslo_end > new_lop->nfslo_end) 2618 new_lop->nfslo_end = lop->nfslo_end; 2619 tlop = lop; 2620 lop = LIST_NEXT(lop, nfslo_list); 2621 nfscl_freelock(tlop, local); 2622 continue; 2623 } 2624 2625 /* 2626 * All these cases are for contiguous locks that are not the 2627 * same type, so they can't be merged. 2628 */ 2629 if (new_lop->nfslo_first <= lop->nfslo_first) { 2630 /* 2631 * This case is where the new lock overlaps with the 2632 * first part of the old lock. Move the start of the 2633 * old lock to just past the end of the new lock. The 2634 * new lock will be inserted in front of the old, since 2635 * ilop hasn't been updated. (We are done now.) 2636 */ 2637 if (lop->nfslo_first != new_lop->nfslo_end) { 2638 lop->nfslo_first = new_lop->nfslo_end; 2639 modified = 1; 2640 } 2641 break; 2642 } 2643 if (new_lop->nfslo_end >= lop->nfslo_end) { 2644 /* 2645 * This case is where the new lock overlaps with the 2646 * end of the old lock's range. Move the old lock's 2647 * end to just before the new lock's first and insert 2648 * the new lock after the old lock. 2649 * Might not be done yet, since the new lock could 2650 * overlap further locks with higher ranges. 2651 */ 2652 if (lop->nfslo_end != new_lop->nfslo_first) { 2653 lop->nfslo_end = new_lop->nfslo_first; 2654 modified = 1; 2655 } 2656 ilop = lop; 2657 lop = LIST_NEXT(lop, nfslo_list); 2658 continue; 2659 } 2660 /* 2661 * The final case is where the new lock's range is in the 2662 * middle of the current lock's and splits the current lock 2663 * up. Use *other_lopp to handle the second part of the 2664 * split old lock range. (We are done now.) 2665 * For unlock, we use new_lop as other_lop and tmp, since 2666 * other_lop and new_lop are the same for this case. 2667 * We noted the unlock case above, so we don't need 2668 * new_lop->nfslo_type any longer. 2669 */ 2670 tmp = new_lop->nfslo_first; 2671 if (unlock) { 2672 other_lop = new_lop; 2673 *new_lopp = NULL; 2674 } else { 2675 other_lop = *other_lopp; 2676 *other_lopp = NULL; 2677 } 2678 other_lop->nfslo_first = new_lop->nfslo_end; 2679 other_lop->nfslo_end = lop->nfslo_end; 2680 other_lop->nfslo_type = lop->nfslo_type; 2681 lop->nfslo_end = tmp; 2682 nfscl_insertlock(lp, other_lop, lop, local); 2683 ilop = lop; 2684 modified = 1; 2685 break; 2686 } 2687 ilop = lop; 2688 lop = LIST_NEXT(lop, nfslo_list); 2689 if (lop == NULL) 2690 break; 2691 } 2692 2693 /* 2694 * Insert the new lock in the list at the appropriate place. 2695 */ 2696 if (!unlock) { 2697 nfscl_insertlock(lp, new_lop, ilop, local); 2698 *new_lopp = NULL; 2699 modified = 1; 2700 } 2701 return (modified); 2702 } 2703 2704 /* 2705 * This function must be run as a kernel thread. 2706 * It does Renew Ops and recovery, when required. 2707 */ 2708 void 2709 nfscl_renewthread(struct nfsclclient *clp, NFSPROC_T *p) 2710 { 2711 struct nfsclowner *owp, *nowp; 2712 struct nfsclopen *op; 2713 struct nfscllockowner *lp, *nlp; 2714 struct nfscldeleghead dh; 2715 struct nfscldeleg *dp, *ndp; 2716 struct ucred *cred; 2717 u_int32_t clidrev; 2718 int error, cbpathdown, islept, igotlock, ret, clearok; 2719 uint32_t recover_done_time = 0; 2720 time_t mytime; 2721 static time_t prevsec = 0; 2722 struct nfscllockownerfh *lfhp, *nlfhp; 2723 struct nfscllockownerfhhead lfh; 2724 struct nfscllayout *lyp, *nlyp; 2725 struct nfscldevinfo *dip, *ndip; 2726 struct nfscllayouthead rlh; 2727 struct nfsclrecalllayout *recallp; 2728 struct nfsclds *dsp; 2729 bool retok; 2730 struct mount *mp; 2731 vnode_t vp; 2732 2733 cred = newnfs_getcred(); 2734 NFSLOCKCLSTATE(); 2735 clp->nfsc_flags |= NFSCLFLAGS_HASTHREAD; 2736 mp = clp->nfsc_nmp->nm_mountp; 2737 NFSUNLOCKCLSTATE(); 2738 for(;;) { 2739 newnfs_setroot(cred); 2740 cbpathdown = 0; 2741 if (clp->nfsc_flags & NFSCLFLAGS_RECOVER) { 2742 /* 2743 * Only allow one full recover within 1/2 of the lease 2744 * duration (nfsc_renew). 2745 * retok is value/result. If passed in set to true, 2746 * it indicates only a CreateSession operation should 2747 * be attempted. 2748 * If it is returned true, it indicates that the 2749 * recovery only required a CreateSession. 2750 */ 2751 retok = true; 2752 if (recover_done_time < NFSD_MONOSEC) { 2753 recover_done_time = NFSD_MONOSEC + 2754 clp->nfsc_renew; 2755 retok = false; 2756 } 2757 NFSCL_DEBUG(1, "Doing recovery, only " 2758 "createsession=%d\n", retok); 2759 nfscl_recover(clp, &retok, cred, p); 2760 } 2761 if (clp->nfsc_expire <= NFSD_MONOSEC && 2762 (clp->nfsc_flags & NFSCLFLAGS_HASCLIENTID)) { 2763 clp->nfsc_expire = NFSD_MONOSEC + clp->nfsc_renew; 2764 clidrev = clp->nfsc_clientidrev; 2765 error = nfsrpc_renew(clp, NULL, cred, p); 2766 if (error == NFSERR_CBPATHDOWN) 2767 cbpathdown = 1; 2768 else if (error == NFSERR_STALECLIENTID) { 2769 NFSLOCKCLSTATE(); 2770 clp->nfsc_flags |= NFSCLFLAGS_RECOVER; 2771 NFSUNLOCKCLSTATE(); 2772 } else if (error == NFSERR_EXPIRED) 2773 (void) nfscl_hasexpired(clp, clidrev, p); 2774 } 2775 2776 checkdsrenew: 2777 if (NFSHASNFSV4N(clp->nfsc_nmp)) { 2778 /* Do renews for any DS sessions. */ 2779 NFSLOCKMNT(clp->nfsc_nmp); 2780 /* Skip first entry, since the MDS is handled above. */ 2781 dsp = TAILQ_FIRST(&clp->nfsc_nmp->nm_sess); 2782 if (dsp != NULL) 2783 dsp = TAILQ_NEXT(dsp, nfsclds_list); 2784 while (dsp != NULL) { 2785 if (dsp->nfsclds_expire <= NFSD_MONOSEC && 2786 dsp->nfsclds_sess.nfsess_defunct == 0) { 2787 dsp->nfsclds_expire = NFSD_MONOSEC + 2788 clp->nfsc_renew; 2789 NFSUNLOCKMNT(clp->nfsc_nmp); 2790 (void)nfsrpc_renew(clp, dsp, cred, p); 2791 goto checkdsrenew; 2792 } 2793 dsp = TAILQ_NEXT(dsp, nfsclds_list); 2794 } 2795 NFSUNLOCKMNT(clp->nfsc_nmp); 2796 } 2797 2798 TAILQ_INIT(&dh); 2799 NFSLOCKCLSTATE(); 2800 if (cbpathdown) 2801 /* It's a Total Recall! */ 2802 nfscl_totalrecall(clp); 2803 2804 /* 2805 * Now, handle defunct owners. 2806 */ 2807 LIST_FOREACH_SAFE(owp, &clp->nfsc_owner, nfsow_list, nowp) { 2808 if (LIST_EMPTY(&owp->nfsow_open)) { 2809 if (owp->nfsow_defunct != 0) 2810 nfscl_freeopenowner(owp, 0); 2811 } 2812 } 2813 2814 /* 2815 * Do the recall on any delegations. To avoid trouble, always 2816 * come back up here after having slept. 2817 */ 2818 igotlock = 0; 2819 tryagain: 2820 dp = TAILQ_FIRST(&clp->nfsc_deleg); 2821 while (dp != NULL) { 2822 ndp = TAILQ_NEXT(dp, nfsdl_list); 2823 if ((dp->nfsdl_flags & NFSCLDL_RECALL)) { 2824 /* 2825 * Wait for outstanding I/O ops to be done. 2826 */ 2827 if (dp->nfsdl_rwlock.nfslock_usecnt > 0) { 2828 if (igotlock) { 2829 nfsv4_unlock(&clp->nfsc_lock, 0); 2830 igotlock = 0; 2831 } 2832 dp->nfsdl_rwlock.nfslock_lock |= 2833 NFSV4LOCK_WANTED; 2834 msleep(&dp->nfsdl_rwlock, 2835 NFSCLSTATEMUTEXPTR, PVFS, "nfscld", 2836 5 * hz); 2837 if (NFSCL_FORCEDISM(mp)) 2838 goto terminate; 2839 goto tryagain; 2840 } 2841 while (!igotlock) { 2842 igotlock = nfsv4_lock(&clp->nfsc_lock, 1, 2843 &islept, NFSCLSTATEMUTEXPTR, mp); 2844 if (igotlock == 0 && NFSCL_FORCEDISM(mp)) 2845 goto terminate; 2846 if (islept) 2847 goto tryagain; 2848 } 2849 NFSUNLOCKCLSTATE(); 2850 newnfs_copycred(&dp->nfsdl_cred, cred); 2851 ret = nfscl_recalldeleg(clp, clp->nfsc_nmp, dp, 2852 NULL, cred, p, 1, &vp); 2853 if (!ret) { 2854 nfscl_cleandeleg(dp); 2855 TAILQ_REMOVE(&clp->nfsc_deleg, dp, 2856 nfsdl_list); 2857 LIST_REMOVE(dp, nfsdl_hash); 2858 TAILQ_INSERT_HEAD(&dh, dp, nfsdl_list); 2859 nfscl_delegcnt--; 2860 nfsstatsv1.cldelegates--; 2861 } 2862 NFSLOCKCLSTATE(); 2863 /* 2864 * The nfsc_lock must be released before doing 2865 * vrele(), since it might call nfs_inactive(). 2866 * For the unlikely case where the vnode failed 2867 * to be acquired by nfscl_recalldeleg(), a 2868 * VOP_RECLAIM() should be in progress and it 2869 * will return the delegation. 2870 */ 2871 nfsv4_unlock(&clp->nfsc_lock, 0); 2872 igotlock = 0; 2873 if (vp != NULL) { 2874 NFSUNLOCKCLSTATE(); 2875 vrele(vp); 2876 NFSLOCKCLSTATE(); 2877 } 2878 goto tryagain; 2879 } 2880 dp = ndp; 2881 } 2882 2883 /* 2884 * Clear out old delegations, if we are above the high water 2885 * mark. Only clear out ones with no state related to them. 2886 * The tailq list is in LRU order. 2887 */ 2888 dp = TAILQ_LAST(&clp->nfsc_deleg, nfscldeleghead); 2889 while (nfscl_delegcnt > nfscl_deleghighwater && dp != NULL) { 2890 ndp = TAILQ_PREV(dp, nfscldeleghead, nfsdl_list); 2891 if (dp->nfsdl_rwlock.nfslock_usecnt == 0 && 2892 dp->nfsdl_rwlock.nfslock_lock == 0 && 2893 dp->nfsdl_timestamp < NFSD_MONOSEC && 2894 (dp->nfsdl_flags & (NFSCLDL_RECALL | NFSCLDL_ZAPPED | 2895 NFSCLDL_NEEDRECLAIM | NFSCLDL_DELEGRET)) == 0) { 2896 clearok = 1; 2897 LIST_FOREACH(owp, &dp->nfsdl_owner, nfsow_list) { 2898 op = LIST_FIRST(&owp->nfsow_open); 2899 if (op != NULL) { 2900 clearok = 0; 2901 break; 2902 } 2903 } 2904 if (clearok) { 2905 LIST_FOREACH(lp, &dp->nfsdl_lock, nfsl_list) { 2906 if (!LIST_EMPTY(&lp->nfsl_lock)) { 2907 clearok = 0; 2908 break; 2909 } 2910 } 2911 } 2912 if (clearok) { 2913 TAILQ_REMOVE(&clp->nfsc_deleg, dp, nfsdl_list); 2914 LIST_REMOVE(dp, nfsdl_hash); 2915 TAILQ_INSERT_HEAD(&dh, dp, nfsdl_list); 2916 nfscl_delegcnt--; 2917 nfsstatsv1.cldelegates--; 2918 } 2919 } 2920 dp = ndp; 2921 } 2922 if (igotlock) 2923 nfsv4_unlock(&clp->nfsc_lock, 0); 2924 2925 /* 2926 * Do the recall on any layouts. To avoid trouble, always 2927 * come back up here after having slept. 2928 */ 2929 TAILQ_INIT(&rlh); 2930 tryagain2: 2931 TAILQ_FOREACH_SAFE(lyp, &clp->nfsc_layout, nfsly_list, nlyp) { 2932 if ((lyp->nfsly_flags & NFSLY_RECALL) != 0) { 2933 /* 2934 * Wait for outstanding I/O ops to be done. 2935 */ 2936 if (lyp->nfsly_lock.nfslock_usecnt > 0 || 2937 (lyp->nfsly_lock.nfslock_lock & 2938 NFSV4LOCK_LOCK) != 0) { 2939 lyp->nfsly_lock.nfslock_lock |= 2940 NFSV4LOCK_WANTED; 2941 msleep(&lyp->nfsly_lock.nfslock_lock, 2942 NFSCLSTATEMUTEXPTR, PVFS, "nfslyp", 2943 5 * hz); 2944 if (NFSCL_FORCEDISM(mp)) 2945 goto terminate; 2946 goto tryagain2; 2947 } 2948 /* Move the layout to the recall list. */ 2949 TAILQ_REMOVE(&clp->nfsc_layout, lyp, 2950 nfsly_list); 2951 LIST_REMOVE(lyp, nfsly_hash); 2952 TAILQ_INSERT_HEAD(&rlh, lyp, nfsly_list); 2953 2954 /* Handle any layout commits. */ 2955 if (!NFSHASNOLAYOUTCOMMIT(clp->nfsc_nmp) && 2956 (lyp->nfsly_flags & NFSLY_WRITTEN) != 0) { 2957 lyp->nfsly_flags &= ~NFSLY_WRITTEN; 2958 NFSUNLOCKCLSTATE(); 2959 NFSCL_DEBUG(3, "do layoutcommit\n"); 2960 nfscl_dolayoutcommit(clp->nfsc_nmp, lyp, 2961 cred, p); 2962 NFSLOCKCLSTATE(); 2963 goto tryagain2; 2964 } 2965 } 2966 } 2967 2968 /* Now, look for stale layouts. */ 2969 lyp = TAILQ_LAST(&clp->nfsc_layout, nfscllayouthead); 2970 while (lyp != NULL) { 2971 nlyp = TAILQ_PREV(lyp, nfscllayouthead, nfsly_list); 2972 if (lyp->nfsly_timestamp < NFSD_MONOSEC && 2973 (lyp->nfsly_flags & (NFSLY_RECALL | 2974 NFSLY_RETONCLOSE)) == 0 && 2975 lyp->nfsly_lock.nfslock_usecnt == 0 && 2976 lyp->nfsly_lock.nfslock_lock == 0) { 2977 NFSCL_DEBUG(4, "ret stale lay=%d\n", 2978 nfscl_layoutcnt); 2979 recallp = malloc(sizeof(*recallp), 2980 M_NFSLAYRECALL, M_NOWAIT); 2981 if (recallp == NULL) 2982 break; 2983 (void)nfscl_layoutrecall(NFSLAYOUTRETURN_FILE, 2984 lyp, NFSLAYOUTIOMODE_ANY, 0, UINT64_MAX, 2985 lyp->nfsly_stateid.seqid, 0, 0, NULL, 2986 recallp); 2987 } 2988 lyp = nlyp; 2989 } 2990 2991 /* 2992 * Free up any unreferenced device info structures. 2993 */ 2994 LIST_FOREACH_SAFE(dip, &clp->nfsc_devinfo, nfsdi_list, ndip) { 2995 if (dip->nfsdi_layoutrefs == 0 && 2996 dip->nfsdi_refcnt == 0) { 2997 NFSCL_DEBUG(4, "freeing devinfo\n"); 2998 LIST_REMOVE(dip, nfsdi_list); 2999 nfscl_freedevinfo(dip); 3000 } 3001 } 3002 NFSUNLOCKCLSTATE(); 3003 3004 /* Do layout return(s), as required. */ 3005 TAILQ_FOREACH_SAFE(lyp, &rlh, nfsly_list, nlyp) { 3006 TAILQ_REMOVE(&rlh, lyp, nfsly_list); 3007 NFSCL_DEBUG(4, "ret layout\n"); 3008 nfscl_layoutreturn(clp->nfsc_nmp, lyp, cred, p); 3009 if ((lyp->nfsly_flags & NFSLY_RETONCLOSE) != 0) { 3010 NFSLOCKCLSTATE(); 3011 lyp->nfsly_flags |= NFSLY_RETURNED; 3012 wakeup(lyp); 3013 NFSUNLOCKCLSTATE(); 3014 } else 3015 nfscl_freelayout(lyp); 3016 } 3017 3018 /* 3019 * Delegreturn any delegations cleaned out or recalled. 3020 */ 3021 TAILQ_FOREACH_SAFE(dp, &dh, nfsdl_list, ndp) { 3022 newnfs_copycred(&dp->nfsdl_cred, cred); 3023 (void) nfscl_trydelegreturn(dp, cred, clp->nfsc_nmp, p); 3024 TAILQ_REMOVE(&dh, dp, nfsdl_list); 3025 free(dp, M_NFSCLDELEG); 3026 } 3027 3028 SLIST_INIT(&lfh); 3029 /* 3030 * Call nfscl_cleanupkext() once per second to check for 3031 * open/lock owners where the process has exited. 3032 */ 3033 mytime = NFSD_MONOSEC; 3034 if (prevsec != mytime) { 3035 prevsec = mytime; 3036 nfscl_cleanupkext(clp, &lfh); 3037 } 3038 3039 /* 3040 * Do a ReleaseLockOwner for all lock owners where the 3041 * associated process no longer exists, as found by 3042 * nfscl_cleanupkext(). 3043 */ 3044 newnfs_setroot(cred); 3045 SLIST_FOREACH_SAFE(lfhp, &lfh, nfslfh_list, nlfhp) { 3046 LIST_FOREACH_SAFE(lp, &lfhp->nfslfh_lock, nfsl_list, 3047 nlp) { 3048 (void)nfsrpc_rellockown(clp->nfsc_nmp, lp, 3049 lfhp->nfslfh_fh, lfhp->nfslfh_len, cred, 3050 p); 3051 nfscl_freelockowner(lp, 0); 3052 } 3053 free(lfhp, M_TEMP); 3054 } 3055 SLIST_INIT(&lfh); 3056 3057 NFSLOCKCLSTATE(); 3058 if ((clp->nfsc_flags & NFSCLFLAGS_RECOVER) == 0) 3059 (void)mtx_sleep(clp, NFSCLSTATEMUTEXPTR, PWAIT, "nfscl", 3060 hz); 3061 terminate: 3062 if (clp->nfsc_flags & NFSCLFLAGS_UMOUNT) { 3063 clp->nfsc_flags &= ~NFSCLFLAGS_HASTHREAD; 3064 NFSUNLOCKCLSTATE(); 3065 NFSFREECRED(cred); 3066 wakeup((caddr_t)clp); 3067 return; 3068 } 3069 NFSUNLOCKCLSTATE(); 3070 } 3071 } 3072 3073 /* 3074 * Initiate state recovery. Called when NFSERR_STALECLIENTID, 3075 * NFSERR_STALESTATEID or NFSERR_BADSESSION is received. 3076 */ 3077 void 3078 nfscl_initiate_recovery(struct nfsclclient *clp) 3079 { 3080 3081 if (clp == NULL) 3082 return; 3083 NFSLOCKCLSTATE(); 3084 clp->nfsc_flags |= NFSCLFLAGS_RECOVER; 3085 NFSUNLOCKCLSTATE(); 3086 wakeup((caddr_t)clp); 3087 } 3088 3089 /* 3090 * Dump out the state stuff for debugging. 3091 */ 3092 void 3093 nfscl_dumpstate(struct nfsmount *nmp, int openowner, int opens, 3094 int lockowner, int locks) 3095 { 3096 struct nfsclclient *clp; 3097 struct nfsclowner *owp; 3098 struct nfsclopen *op; 3099 struct nfscllockowner *lp; 3100 struct nfscllock *lop; 3101 struct nfscldeleg *dp; 3102 3103 clp = nmp->nm_clp; 3104 if (clp == NULL) { 3105 printf("nfscl dumpstate NULL clp\n"); 3106 return; 3107 } 3108 NFSLOCKCLSTATE(); 3109 TAILQ_FOREACH(dp, &clp->nfsc_deleg, nfsdl_list) { 3110 LIST_FOREACH(owp, &dp->nfsdl_owner, nfsow_list) { 3111 if (openowner && !LIST_EMPTY(&owp->nfsow_open)) 3112 printf("owner=0x%x 0x%x 0x%x 0x%x seqid=%d\n", 3113 owp->nfsow_owner[0], owp->nfsow_owner[1], 3114 owp->nfsow_owner[2], owp->nfsow_owner[3], 3115 owp->nfsow_seqid); 3116 LIST_FOREACH(op, &owp->nfsow_open, nfso_list) { 3117 if (opens) 3118 printf("open st=0x%x 0x%x 0x%x cnt=%d fh12=0x%x\n", 3119 op->nfso_stateid.other[0], op->nfso_stateid.other[1], 3120 op->nfso_stateid.other[2], op->nfso_opencnt, 3121 op->nfso_fh[12]); 3122 LIST_FOREACH(lp, &op->nfso_lock, nfsl_list) { 3123 if (lockowner) 3124 printf("lckown=0x%x 0x%x 0x%x 0x%x seqid=%d st=0x%x 0x%x 0x%x\n", 3125 lp->nfsl_owner[0], lp->nfsl_owner[1], 3126 lp->nfsl_owner[2], lp->nfsl_owner[3], 3127 lp->nfsl_seqid, 3128 lp->nfsl_stateid.other[0], lp->nfsl_stateid.other[1], 3129 lp->nfsl_stateid.other[2]); 3130 LIST_FOREACH(lop, &lp->nfsl_lock, nfslo_list) { 3131 if (locks) 3132 #ifdef __FreeBSD__ 3133 printf("lck typ=%d fst=%ju end=%ju\n", 3134 lop->nfslo_type, (intmax_t)lop->nfslo_first, 3135 (intmax_t)lop->nfslo_end); 3136 #else 3137 printf("lck typ=%d fst=%qd end=%qd\n", 3138 lop->nfslo_type, lop->nfslo_first, 3139 lop->nfslo_end); 3140 #endif 3141 } 3142 } 3143 } 3144 } 3145 } 3146 LIST_FOREACH(owp, &clp->nfsc_owner, nfsow_list) { 3147 if (openowner && !LIST_EMPTY(&owp->nfsow_open)) 3148 printf("owner=0x%x 0x%x 0x%x 0x%x seqid=%d\n", 3149 owp->nfsow_owner[0], owp->nfsow_owner[1], 3150 owp->nfsow_owner[2], owp->nfsow_owner[3], 3151 owp->nfsow_seqid); 3152 LIST_FOREACH(op, &owp->nfsow_open, nfso_list) { 3153 if (opens) 3154 printf("open st=0x%x 0x%x 0x%x cnt=%d fh12=0x%x\n", 3155 op->nfso_stateid.other[0], op->nfso_stateid.other[1], 3156 op->nfso_stateid.other[2], op->nfso_opencnt, 3157 op->nfso_fh[12]); 3158 LIST_FOREACH(lp, &op->nfso_lock, nfsl_list) { 3159 if (lockowner) 3160 printf("lckown=0x%x 0x%x 0x%x 0x%x seqid=%d st=0x%x 0x%x 0x%x\n", 3161 lp->nfsl_owner[0], lp->nfsl_owner[1], 3162 lp->nfsl_owner[2], lp->nfsl_owner[3], 3163 lp->nfsl_seqid, 3164 lp->nfsl_stateid.other[0], lp->nfsl_stateid.other[1], 3165 lp->nfsl_stateid.other[2]); 3166 LIST_FOREACH(lop, &lp->nfsl_lock, nfslo_list) { 3167 if (locks) 3168 #ifdef __FreeBSD__ 3169 printf("lck typ=%d fst=%ju end=%ju\n", 3170 lop->nfslo_type, (intmax_t)lop->nfslo_first, 3171 (intmax_t)lop->nfslo_end); 3172 #else 3173 printf("lck typ=%d fst=%qd end=%qd\n", 3174 lop->nfslo_type, lop->nfslo_first, 3175 lop->nfslo_end); 3176 #endif 3177 } 3178 } 3179 } 3180 } 3181 NFSUNLOCKCLSTATE(); 3182 } 3183 3184 /* 3185 * Check for duplicate open owners and opens. 3186 * (Only used as a diagnostic aid.) 3187 */ 3188 void 3189 nfscl_dupopen(vnode_t vp, int dupopens) 3190 { 3191 struct nfsclclient *clp; 3192 struct nfsclowner *owp, *owp2; 3193 struct nfsclopen *op, *op2; 3194 struct nfsfh *nfhp; 3195 3196 clp = VFSTONFS(vp->v_mount)->nm_clp; 3197 if (clp == NULL) { 3198 printf("nfscl dupopen NULL clp\n"); 3199 return; 3200 } 3201 nfhp = VTONFS(vp)->n_fhp; 3202 NFSLOCKCLSTATE(); 3203 3204 /* 3205 * First, search for duplicate owners. 3206 * These should never happen! 3207 */ 3208 LIST_FOREACH(owp2, &clp->nfsc_owner, nfsow_list) { 3209 LIST_FOREACH(owp, &clp->nfsc_owner, nfsow_list) { 3210 if (owp != owp2 && 3211 !NFSBCMP(owp->nfsow_owner, owp2->nfsow_owner, 3212 NFSV4CL_LOCKNAMELEN)) { 3213 NFSUNLOCKCLSTATE(); 3214 printf("DUP OWNER\n"); 3215 nfscl_dumpstate(VFSTONFS(vp->v_mount), 1, 1, 0, 0); 3216 return; 3217 } 3218 } 3219 } 3220 3221 /* 3222 * Now, search for duplicate stateids. 3223 * These shouldn't happen, either. 3224 */ 3225 LIST_FOREACH(owp2, &clp->nfsc_owner, nfsow_list) { 3226 LIST_FOREACH(op2, &owp2->nfsow_open, nfso_list) { 3227 LIST_FOREACH(owp, &clp->nfsc_owner, nfsow_list) { 3228 LIST_FOREACH(op, &owp->nfsow_open, nfso_list) { 3229 if (op != op2 && 3230 (op->nfso_stateid.other[0] != 0 || 3231 op->nfso_stateid.other[1] != 0 || 3232 op->nfso_stateid.other[2] != 0) && 3233 op->nfso_stateid.other[0] == op2->nfso_stateid.other[0] && 3234 op->nfso_stateid.other[1] == op2->nfso_stateid.other[1] && 3235 op->nfso_stateid.other[2] == op2->nfso_stateid.other[2]) { 3236 NFSUNLOCKCLSTATE(); 3237 printf("DUP STATEID\n"); 3238 nfscl_dumpstate(VFSTONFS(vp->v_mount), 1, 1, 0, 0); 3239 return; 3240 } 3241 } 3242 } 3243 } 3244 } 3245 3246 /* 3247 * Now search for duplicate opens. 3248 * Duplicate opens for the same owner 3249 * should never occur. Other duplicates are 3250 * possible and are checked for if "dupopens" 3251 * is true. 3252 */ 3253 LIST_FOREACH(owp2, &clp->nfsc_owner, nfsow_list) { 3254 LIST_FOREACH(op2, &owp2->nfsow_open, nfso_list) { 3255 if (nfhp->nfh_len == op2->nfso_fhlen && 3256 !NFSBCMP(nfhp->nfh_fh, op2->nfso_fh, nfhp->nfh_len)) { 3257 LIST_FOREACH(owp, &clp->nfsc_owner, nfsow_list) { 3258 LIST_FOREACH(op, &owp->nfsow_open, nfso_list) { 3259 if (op != op2 && nfhp->nfh_len == op->nfso_fhlen && 3260 !NFSBCMP(nfhp->nfh_fh, op->nfso_fh, nfhp->nfh_len) && 3261 (!NFSBCMP(op->nfso_own->nfsow_owner, 3262 op2->nfso_own->nfsow_owner, NFSV4CL_LOCKNAMELEN) || 3263 dupopens)) { 3264 if (!NFSBCMP(op->nfso_own->nfsow_owner, 3265 op2->nfso_own->nfsow_owner, NFSV4CL_LOCKNAMELEN)) { 3266 NFSUNLOCKCLSTATE(); 3267 printf("BADDUP OPEN\n"); 3268 } else { 3269 NFSUNLOCKCLSTATE(); 3270 printf("DUP OPEN\n"); 3271 } 3272 nfscl_dumpstate(VFSTONFS(vp->v_mount), 1, 1, 0, 3273 0); 3274 return; 3275 } 3276 } 3277 } 3278 } 3279 } 3280 } 3281 NFSUNLOCKCLSTATE(); 3282 } 3283 3284 /* 3285 * During close, find an open that needs to be dereferenced and 3286 * dereference it. If there are no more opens for this file, 3287 * log a message to that effect. 3288 * Opens aren't actually Close'd until VOP_INACTIVE() is performed 3289 * on the file's vnode. 3290 * This is the safe way, since it is difficult to identify 3291 * which open the close is for and I/O can be performed after the 3292 * close(2) system call when a file is mmap'd. 3293 * If it returns 0 for success, there will be a referenced 3294 * clp returned via clpp. 3295 */ 3296 int 3297 nfscl_getclose(vnode_t vp, struct nfsclclient **clpp) 3298 { 3299 struct nfsclclient *clp; 3300 struct nfsclowner *owp; 3301 struct nfsclopen *op; 3302 struct nfscldeleg *dp; 3303 struct nfsfh *nfhp; 3304 int error, notdecr; 3305 3306 error = nfscl_getcl(vp->v_mount, NULL, NULL, false, true, &clp); 3307 if (error) 3308 return (error); 3309 *clpp = clp; 3310 3311 nfhp = VTONFS(vp)->n_fhp; 3312 notdecr = 1; 3313 NFSLOCKCLSTATE(); 3314 /* 3315 * First, look for one under a delegation that was locally issued 3316 * and just decrement the opencnt for it. Since all my Opens against 3317 * the server are DENY_NONE, I don't see a problem with hanging 3318 * onto them. (It is much easier to use one of the extant Opens 3319 * that I already have on the server when a Delegation is recalled 3320 * than to do fresh Opens.) Someday, I might need to rethink this, but. 3321 */ 3322 dp = nfscl_finddeleg(clp, nfhp->nfh_fh, nfhp->nfh_len); 3323 if (dp != NULL) { 3324 LIST_FOREACH(owp, &dp->nfsdl_owner, nfsow_list) { 3325 op = LIST_FIRST(&owp->nfsow_open); 3326 if (op != NULL) { 3327 /* 3328 * Since a delegation is for a file, there 3329 * should never be more than one open for 3330 * each openowner. 3331 */ 3332 if (LIST_NEXT(op, nfso_list) != NULL) 3333 panic("nfscdeleg opens"); 3334 if (notdecr && op->nfso_opencnt > 0) { 3335 notdecr = 0; 3336 op->nfso_opencnt--; 3337 break; 3338 } 3339 } 3340 } 3341 } 3342 3343 /* Now process the opens against the server. */ 3344 LIST_FOREACH(op, NFSCLOPENHASH(clp, nfhp->nfh_fh, nfhp->nfh_len), 3345 nfso_hash) { 3346 if (op->nfso_fhlen == nfhp->nfh_len && 3347 !NFSBCMP(op->nfso_fh, nfhp->nfh_fh, 3348 nfhp->nfh_len)) { 3349 /* Found an open, decrement cnt if possible */ 3350 if (notdecr && op->nfso_opencnt > 0) { 3351 notdecr = 0; 3352 op->nfso_opencnt--; 3353 } 3354 /* 3355 * There are more opens, so just return. 3356 */ 3357 if (op->nfso_opencnt > 0) { 3358 NFSUNLOCKCLSTATE(); 3359 return (0); 3360 } 3361 } 3362 } 3363 NFSUNLOCKCLSTATE(); 3364 if (notdecr) 3365 printf("nfscl: never fnd open\n"); 3366 return (0); 3367 } 3368 3369 int 3370 nfscl_doclose(vnode_t vp, struct nfsclclient **clpp, NFSPROC_T *p) 3371 { 3372 struct nfsclclient *clp; 3373 struct nfsmount *nmp; 3374 struct nfsclowner *owp, *nowp; 3375 struct nfsclopen *op, *nop; 3376 struct nfsclopenhead delayed; 3377 struct nfscldeleg *dp; 3378 struct nfsfh *nfhp; 3379 struct nfsclrecalllayout *recallp; 3380 struct nfscllayout *lyp; 3381 int error; 3382 3383 error = nfscl_getcl(vp->v_mount, NULL, NULL, false, true, &clp); 3384 if (error) 3385 return (error); 3386 *clpp = clp; 3387 3388 nmp = VFSTONFS(vp->v_mount); 3389 nfhp = VTONFS(vp)->n_fhp; 3390 recallp = malloc(sizeof(*recallp), M_NFSLAYRECALL, M_WAITOK); 3391 NFSLOCKCLSTATE(); 3392 /* 3393 * First get rid of the local Open structures, which should be no 3394 * longer in use. 3395 */ 3396 dp = nfscl_finddeleg(clp, nfhp->nfh_fh, nfhp->nfh_len); 3397 if (dp != NULL) { 3398 LIST_FOREACH_SAFE(owp, &dp->nfsdl_owner, nfsow_list, nowp) { 3399 op = LIST_FIRST(&owp->nfsow_open); 3400 if (op != NULL) { 3401 KASSERT((op->nfso_opencnt == 0), 3402 ("nfscl: bad open cnt on deleg")); 3403 nfscl_freeopen(op, 1, true); 3404 } 3405 nfscl_freeopenowner(owp, 1); 3406 } 3407 } 3408 3409 /* Return any layouts marked return on close. */ 3410 nfscl_retoncloselayout(vp, clp, nfhp->nfh_fh, nfhp->nfh_len, &recallp, 3411 &lyp); 3412 3413 /* Now process the opens against the server. */ 3414 LIST_INIT(&delayed); 3415 lookformore: 3416 LIST_FOREACH(op, NFSCLOPENHASH(clp, nfhp->nfh_fh, nfhp->nfh_len), 3417 nfso_hash) { 3418 if (op->nfso_fhlen == nfhp->nfh_len && 3419 !NFSBCMP(op->nfso_fh, nfhp->nfh_fh, 3420 nfhp->nfh_len)) { 3421 /* Found an open, close it. */ 3422 #ifdef DIAGNOSTIC 3423 KASSERT((op->nfso_opencnt == 0), 3424 ("nfscl: bad open cnt on server (%d)", 3425 op->nfso_opencnt)); 3426 #endif 3427 NFSUNLOCKCLSTATE(); 3428 if (NFSHASNFSV4N(nmp)) 3429 error = nfsrpc_doclose(nmp, op, p, false, true); 3430 else 3431 error = nfsrpc_doclose(nmp, op, p, true, true); 3432 NFSLOCKCLSTATE(); 3433 if (error == NFSERR_DELAY) { 3434 nfscl_unlinkopen(op); 3435 op->nfso_own = NULL; 3436 LIST_INSERT_HEAD(&delayed, op, nfso_list); 3437 } 3438 goto lookformore; 3439 } 3440 } 3441 nfscl_clrelease(clp); 3442 3443 /* Now, wait for any layout that is returned upon close. */ 3444 if (lyp != NULL) { 3445 while ((lyp->nfsly_flags & NFSLY_RETURNED) == 0) { 3446 if (NFSCL_FORCEDISM(nmp->nm_mountp)) { 3447 lyp = NULL; 3448 break; 3449 } 3450 msleep(lyp, NFSCLSTATEMUTEXPTR, PZERO, "nfslroc", hz); 3451 } 3452 if (lyp != NULL) 3453 nfscl_freelayout(lyp); 3454 } 3455 3456 NFSUNLOCKCLSTATE(); 3457 /* 3458 * recallp has been set NULL by nfscl_retoncloselayout() if it was 3459 * used by the function, but calling free() with a NULL pointer is ok. 3460 */ 3461 free(recallp, M_NFSLAYRECALL); 3462 3463 /* Now, loop retrying the delayed closes. */ 3464 LIST_FOREACH_SAFE(op, &delayed, nfso_list, nop) { 3465 nfsrpc_doclose(nmp, op, p, true, false); 3466 LIST_REMOVE(op, nfso_list); 3467 nfscl_freeopen(op, 0, false); 3468 } 3469 return (0); 3470 } 3471 3472 /* 3473 * Return all delegations on this client. 3474 * (Must be called with client sleep lock.) 3475 */ 3476 static void 3477 nfscl_delegreturnall(struct nfsclclient *clp, NFSPROC_T *p, 3478 struct nfscldeleghead *dhp) 3479 { 3480 struct nfscldeleg *dp, *ndp; 3481 struct ucred *cred; 3482 3483 cred = newnfs_getcred(); 3484 TAILQ_FOREACH_SAFE(dp, &clp->nfsc_deleg, nfsdl_list, ndp) { 3485 nfscl_cleandeleg(dp); 3486 (void) nfscl_trydelegreturn(dp, cred, clp->nfsc_nmp, p); 3487 if (dhp != NULL) { 3488 nfscl_freedeleg(&clp->nfsc_deleg, dp, false); 3489 TAILQ_INSERT_HEAD(dhp, dp, nfsdl_list); 3490 } else 3491 nfscl_freedeleg(&clp->nfsc_deleg, dp, true); 3492 } 3493 NFSFREECRED(cred); 3494 } 3495 3496 /* 3497 * Return any delegation for this vp. 3498 */ 3499 void 3500 nfscl_delegreturnvp(vnode_t vp, NFSPROC_T *p) 3501 { 3502 struct nfsclclient *clp; 3503 struct nfscldeleg *dp; 3504 struct ucred *cred; 3505 struct nfsnode *np; 3506 struct nfsmount *nmp; 3507 3508 nmp = VFSTONFS(vp->v_mount); 3509 NFSLOCKMNT(nmp); 3510 if ((nmp->nm_privflag & NFSMNTP_DELEGISSUED) == 0) { 3511 NFSUNLOCKMNT(nmp); 3512 return; 3513 } 3514 NFSUNLOCKMNT(nmp); 3515 np = VTONFS(vp); 3516 cred = newnfs_getcred(); 3517 dp = NULL; 3518 NFSLOCKCLSTATE(); 3519 clp = nmp->nm_clp; 3520 if (clp != NULL) 3521 dp = nfscl_finddeleg(clp, np->n_fhp->nfh_fh, 3522 np->n_fhp->nfh_len); 3523 if (dp != NULL) { 3524 nfscl_cleandeleg(dp); 3525 nfscl_freedeleg(&clp->nfsc_deleg, dp, false); 3526 NFSUNLOCKCLSTATE(); 3527 newnfs_copycred(&dp->nfsdl_cred, cred); 3528 nfscl_trydelegreturn(dp, cred, clp->nfsc_nmp, p); 3529 free(dp, M_NFSCLDELEG); 3530 } else 3531 NFSUNLOCKCLSTATE(); 3532 NFSFREECRED(cred); 3533 } 3534 3535 /* 3536 * Do a callback RPC. 3537 */ 3538 void 3539 nfscl_docb(struct nfsrv_descript *nd, NFSPROC_T *p) 3540 { 3541 int clist, gotseq_ok, i, j, k, op, rcalls; 3542 u_int32_t *tl; 3543 struct nfsclclient *clp; 3544 struct nfscldeleg *dp = NULL; 3545 int numops, taglen = -1, error = 0, trunc __unused; 3546 u_int32_t minorvers = 0, retops = 0, *retopsp = NULL, *repp, cbident; 3547 u_char tag[NFSV4_SMALLSTR + 1], *tagstr; 3548 vnode_t vp = NULL; 3549 struct nfsnode *np; 3550 struct vattr va; 3551 struct nfsfh *nfhp; 3552 mount_t mp; 3553 nfsattrbit_t attrbits, rattrbits; 3554 nfsv4stateid_t stateid; 3555 uint32_t seqid, slotid = 0, highslot, cachethis __unused; 3556 uint8_t sessionid[NFSX_V4SESSIONID]; 3557 struct mbuf *rep; 3558 struct nfscllayout *lyp; 3559 uint64_t filesid[2], len, off; 3560 int changed, gotone, laytype, recalltype; 3561 uint32_t iomode; 3562 struct nfsclrecalllayout *recallp = NULL; 3563 struct nfsclsession *tsep; 3564 3565 gotseq_ok = 0; 3566 nfsrvd_rephead(nd); 3567 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED); 3568 taglen = fxdr_unsigned(int, *tl); 3569 if (taglen < 0 || taglen > NFSV4_OPAQUELIMIT) { 3570 error = EBADRPC; 3571 taglen = -1; 3572 goto nfsmout; 3573 } 3574 if (taglen <= NFSV4_SMALLSTR) 3575 tagstr = tag; 3576 else 3577 tagstr = malloc(taglen + 1, M_TEMP, M_WAITOK); 3578 error = nfsrv_mtostr(nd, tagstr, taglen); 3579 if (error) { 3580 if (taglen > NFSV4_SMALLSTR) 3581 free(tagstr, M_TEMP); 3582 taglen = -1; 3583 goto nfsmout; 3584 } 3585 (void) nfsm_strtom(nd, tag, taglen); 3586 if (taglen > NFSV4_SMALLSTR) { 3587 free(tagstr, M_TEMP); 3588 } 3589 NFSM_BUILD(retopsp, u_int32_t *, NFSX_UNSIGNED); 3590 NFSM_DISSECT(tl, u_int32_t *, 3 * NFSX_UNSIGNED); 3591 minorvers = fxdr_unsigned(u_int32_t, *tl++); 3592 if (minorvers != NFSV4_MINORVERSION && 3593 minorvers != NFSV41_MINORVERSION && 3594 minorvers != NFSV42_MINORVERSION) 3595 nd->nd_repstat = NFSERR_MINORVERMISMATCH; 3596 cbident = fxdr_unsigned(u_int32_t, *tl++); 3597 if (nd->nd_repstat) 3598 numops = 0; 3599 else 3600 numops = fxdr_unsigned(int, *tl); 3601 /* 3602 * Loop around doing the sub ops. 3603 */ 3604 for (i = 0; i < numops; i++) { 3605 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED); 3606 NFSM_BUILD(repp, u_int32_t *, 2 * NFSX_UNSIGNED); 3607 *repp++ = *tl; 3608 op = fxdr_unsigned(int, *tl); 3609 nd->nd_procnum = op; 3610 if (i == 0 && op != NFSV4OP_CBSEQUENCE && minorvers != 3611 NFSV4_MINORVERSION) { 3612 nd->nd_repstat = NFSERR_OPNOTINSESS; 3613 *repp = nfscl_errmap(nd, minorvers); 3614 retops++; 3615 break; 3616 } 3617 if (op < NFSV4OP_CBGETATTR || 3618 (op > NFSV4OP_CBRECALL && minorvers == NFSV4_MINORVERSION) || 3619 (op > NFSV4OP_CBNOTIFYDEVID && 3620 minorvers == NFSV41_MINORVERSION) || 3621 (op > NFSV4OP_CBOFFLOAD && 3622 minorvers == NFSV42_MINORVERSION)) { 3623 nd->nd_repstat = NFSERR_OPILLEGAL; 3624 *repp = nfscl_errmap(nd, minorvers); 3625 retops++; 3626 break; 3627 } 3628 if (op < NFSV42_CBNOPS) 3629 nfsstatsv1.cbrpccnt[nd->nd_procnum]++; 3630 switch (op) { 3631 case NFSV4OP_CBGETATTR: 3632 NFSCL_DEBUG(4, "cbgetattr\n"); 3633 mp = NULL; 3634 vp = NULL; 3635 error = nfsm_getfh(nd, &nfhp); 3636 if (!error) 3637 error = nfsrv_getattrbits(nd, &attrbits, 3638 NULL, NULL); 3639 if (!error) { 3640 mp = nfscl_getmnt(minorvers, sessionid, cbident, 3641 &clp); 3642 if (mp == NULL) 3643 error = NFSERR_SERVERFAULT; 3644 } 3645 if (!error) { 3646 error = nfscl_ngetreopen(mp, nfhp->nfh_fh, 3647 nfhp->nfh_len, p, &np); 3648 if (!error) 3649 vp = NFSTOV(np); 3650 } 3651 if (!error) { 3652 NFSZERO_ATTRBIT(&rattrbits); 3653 NFSLOCKCLSTATE(); 3654 dp = nfscl_finddeleg(clp, nfhp->nfh_fh, 3655 nfhp->nfh_len); 3656 if (dp != NULL) { 3657 if (NFSISSET_ATTRBIT(&attrbits, 3658 NFSATTRBIT_SIZE)) { 3659 if (vp != NULL) 3660 va.va_size = np->n_size; 3661 else 3662 va.va_size = 3663 dp->nfsdl_size; 3664 NFSSETBIT_ATTRBIT(&rattrbits, 3665 NFSATTRBIT_SIZE); 3666 } 3667 if (NFSISSET_ATTRBIT(&attrbits, 3668 NFSATTRBIT_CHANGE)) { 3669 va.va_filerev = 3670 dp->nfsdl_change; 3671 if (vp == NULL || 3672 (np->n_flag & NDELEGMOD)) 3673 va.va_filerev++; 3674 NFSSETBIT_ATTRBIT(&rattrbits, 3675 NFSATTRBIT_CHANGE); 3676 } 3677 } else 3678 error = NFSERR_SERVERFAULT; 3679 NFSUNLOCKCLSTATE(); 3680 } 3681 if (vp != NULL) 3682 vrele(vp); 3683 if (mp != NULL) 3684 vfs_unbusy(mp); 3685 if (nfhp != NULL) 3686 free(nfhp, M_NFSFH); 3687 if (!error) 3688 (void) nfsv4_fillattr(nd, NULL, NULL, NULL, &va, 3689 NULL, 0, &rattrbits, NULL, p, 0, 0, 0, 0, 3690 (uint64_t)0, NULL); 3691 break; 3692 case NFSV4OP_CBRECALL: 3693 NFSCL_DEBUG(4, "cbrecall\n"); 3694 NFSM_DISSECT(tl, u_int32_t *, NFSX_STATEID + 3695 NFSX_UNSIGNED); 3696 stateid.seqid = *tl++; 3697 NFSBCOPY((caddr_t)tl, (caddr_t)stateid.other, 3698 NFSX_STATEIDOTHER); 3699 tl += (NFSX_STATEIDOTHER / NFSX_UNSIGNED); 3700 trunc = fxdr_unsigned(int, *tl); 3701 error = nfsm_getfh(nd, &nfhp); 3702 if (!error) { 3703 NFSLOCKCLSTATE(); 3704 if (minorvers == NFSV4_MINORVERSION) 3705 clp = nfscl_getclnt(cbident); 3706 else 3707 clp = nfscl_getclntsess(sessionid); 3708 if (clp != NULL) { 3709 dp = nfscl_finddeleg(clp, nfhp->nfh_fh, 3710 nfhp->nfh_len); 3711 if (dp != NULL && (dp->nfsdl_flags & 3712 NFSCLDL_DELEGRET) == 0) { 3713 dp->nfsdl_flags |= 3714 NFSCLDL_RECALL; 3715 wakeup((caddr_t)clp); 3716 } 3717 } else { 3718 error = NFSERR_SERVERFAULT; 3719 } 3720 NFSUNLOCKCLSTATE(); 3721 } 3722 if (nfhp != NULL) 3723 free(nfhp, M_NFSFH); 3724 break; 3725 case NFSV4OP_CBLAYOUTRECALL: 3726 NFSCL_DEBUG(4, "cblayrec\n"); 3727 nfhp = NULL; 3728 NFSM_DISSECT(tl, uint32_t *, 4 * NFSX_UNSIGNED); 3729 laytype = fxdr_unsigned(int, *tl++); 3730 iomode = fxdr_unsigned(uint32_t, *tl++); 3731 if (newnfs_true == *tl++) 3732 changed = 1; 3733 else 3734 changed = 0; 3735 recalltype = fxdr_unsigned(int, *tl); 3736 NFSCL_DEBUG(4, "layt=%d iom=%d ch=%d rectyp=%d\n", 3737 laytype, iomode, changed, recalltype); 3738 recallp = malloc(sizeof(*recallp), M_NFSLAYRECALL, 3739 M_WAITOK); 3740 if (laytype != NFSLAYOUT_NFSV4_1_FILES && 3741 laytype != NFSLAYOUT_FLEXFILE) 3742 error = NFSERR_NOMATCHLAYOUT; 3743 else if (recalltype == NFSLAYOUTRETURN_FILE) { 3744 error = nfsm_getfh(nd, &nfhp); 3745 NFSCL_DEBUG(4, "retfile getfh=%d\n", error); 3746 if (error != 0) 3747 goto nfsmout; 3748 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_HYPER + 3749 NFSX_STATEID); 3750 off = fxdr_hyper(tl); tl += 2; 3751 len = fxdr_hyper(tl); tl += 2; 3752 stateid.seqid = fxdr_unsigned(uint32_t, *tl++); 3753 NFSBCOPY(tl, stateid.other, NFSX_STATEIDOTHER); 3754 if (minorvers == NFSV4_MINORVERSION) 3755 error = NFSERR_NOTSUPP; 3756 NFSCL_DEBUG(4, "off=%ju len=%ju sq=%u err=%d\n", 3757 (uintmax_t)off, (uintmax_t)len, 3758 stateid.seqid, error); 3759 if (error == 0) { 3760 NFSLOCKCLSTATE(); 3761 clp = nfscl_getclntsess(sessionid); 3762 NFSCL_DEBUG(4, "cbly clp=%p\n", clp); 3763 if (clp != NULL) { 3764 lyp = nfscl_findlayout(clp, 3765 nfhp->nfh_fh, 3766 nfhp->nfh_len); 3767 NFSCL_DEBUG(4, "cblyp=%p\n", 3768 lyp); 3769 if (lyp != NULL && 3770 (lyp->nfsly_flags & 3771 (NFSLY_FILES | 3772 NFSLY_FLEXFILE)) != 0 && 3773 !NFSBCMP(stateid.other, 3774 lyp->nfsly_stateid.other, 3775 NFSX_STATEIDOTHER)) { 3776 error = 3777 nfscl_layoutrecall( 3778 recalltype, 3779 lyp, iomode, off, 3780 len, stateid.seqid, 3781 0, 0, NULL, 3782 recallp); 3783 if (error == 0 && 3784 stateid.seqid > 3785 lyp->nfsly_stateid.seqid) 3786 lyp->nfsly_stateid.seqid = 3787 stateid.seqid; 3788 recallp = NULL; 3789 wakeup(clp); 3790 NFSCL_DEBUG(4, 3791 "aft layrcal=%d " 3792 "layseqid=%d\n", 3793 error, 3794 lyp->nfsly_stateid.seqid); 3795 } else 3796 error = 3797 NFSERR_NOMATCHLAYOUT; 3798 } else 3799 error = NFSERR_NOMATCHLAYOUT; 3800 NFSUNLOCKCLSTATE(); 3801 } 3802 free(nfhp, M_NFSFH); 3803 } else if (recalltype == NFSLAYOUTRETURN_FSID) { 3804 NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_HYPER); 3805 filesid[0] = fxdr_hyper(tl); tl += 2; 3806 filesid[1] = fxdr_hyper(tl); tl += 2; 3807 gotone = 0; 3808 NFSLOCKCLSTATE(); 3809 clp = nfscl_getclntsess(sessionid); 3810 if (clp != NULL) { 3811 TAILQ_FOREACH(lyp, &clp->nfsc_layout, 3812 nfsly_list) { 3813 if (lyp->nfsly_filesid[0] == 3814 filesid[0] && 3815 lyp->nfsly_filesid[1] == 3816 filesid[1]) { 3817 error = 3818 nfscl_layoutrecall( 3819 recalltype, 3820 lyp, iomode, 0, 3821 UINT64_MAX, 3822 lyp->nfsly_stateid.seqid, 3823 0, 0, NULL, 3824 recallp); 3825 recallp = NULL; 3826 gotone = 1; 3827 } 3828 } 3829 if (gotone != 0) 3830 wakeup(clp); 3831 else 3832 error = NFSERR_NOMATCHLAYOUT; 3833 } else 3834 error = NFSERR_NOMATCHLAYOUT; 3835 NFSUNLOCKCLSTATE(); 3836 } else if (recalltype == NFSLAYOUTRETURN_ALL) { 3837 gotone = 0; 3838 NFSLOCKCLSTATE(); 3839 clp = nfscl_getclntsess(sessionid); 3840 if (clp != NULL) { 3841 TAILQ_FOREACH(lyp, &clp->nfsc_layout, 3842 nfsly_list) { 3843 error = nfscl_layoutrecall( 3844 recalltype, lyp, iomode, 0, 3845 UINT64_MAX, 3846 lyp->nfsly_stateid.seqid, 3847 0, 0, NULL, recallp); 3848 recallp = NULL; 3849 gotone = 1; 3850 } 3851 if (gotone != 0) 3852 wakeup(clp); 3853 else 3854 error = NFSERR_NOMATCHLAYOUT; 3855 } else 3856 error = NFSERR_NOMATCHLAYOUT; 3857 NFSUNLOCKCLSTATE(); 3858 } else 3859 error = NFSERR_NOMATCHLAYOUT; 3860 if (recallp != NULL) { 3861 free(recallp, M_NFSLAYRECALL); 3862 recallp = NULL; 3863 } 3864 break; 3865 case NFSV4OP_CBSEQUENCE: 3866 if (i != 0) { 3867 error = NFSERR_SEQUENCEPOS; 3868 break; 3869 } 3870 NFSM_DISSECT(tl, uint32_t *, NFSX_V4SESSIONID + 3871 5 * NFSX_UNSIGNED); 3872 bcopy(tl, sessionid, NFSX_V4SESSIONID); 3873 tl += NFSX_V4SESSIONID / NFSX_UNSIGNED; 3874 seqid = fxdr_unsigned(uint32_t, *tl++); 3875 slotid = fxdr_unsigned(uint32_t, *tl++); 3876 highslot = fxdr_unsigned(uint32_t, *tl++); 3877 cachethis = *tl++; 3878 /* Throw away the referring call stuff. */ 3879 clist = fxdr_unsigned(int, *tl); 3880 for (j = 0; j < clist; j++) { 3881 NFSM_DISSECT(tl, uint32_t *, NFSX_V4SESSIONID + 3882 NFSX_UNSIGNED); 3883 tl += NFSX_V4SESSIONID / NFSX_UNSIGNED; 3884 rcalls = fxdr_unsigned(int, *tl); 3885 for (k = 0; k < rcalls; k++) { 3886 NFSM_DISSECT(tl, uint32_t *, 3887 2 * NFSX_UNSIGNED); 3888 } 3889 } 3890 NFSLOCKCLSTATE(); 3891 clp = nfscl_getclntsess(sessionid); 3892 if (clp == NULL) 3893 error = NFSERR_SERVERFAULT; 3894 if (error == 0) { 3895 tsep = nfsmnt_mdssession(clp->nfsc_nmp); 3896 error = nfsv4_seqsession(seqid, slotid, 3897 highslot, tsep->nfsess_cbslots, &rep, 3898 tsep->nfsess_backslots); 3899 } 3900 NFSUNLOCKCLSTATE(); 3901 if (error == 0 || error == NFSERR_REPLYFROMCACHE) { 3902 gotseq_ok = 1; 3903 if (rep != NULL) { 3904 /* 3905 * Handle a reply for a retried 3906 * callback. The reply will be 3907 * re-inserted in the session cache 3908 * by the nfsv4_seqsess_cacherep() call 3909 * after out: 3910 */ 3911 KASSERT(error == NFSERR_REPLYFROMCACHE, 3912 ("cbsequence: non-NULL rep")); 3913 NFSCL_DEBUG(4, "Got cbretry\n"); 3914 m_freem(nd->nd_mreq); 3915 nd->nd_mreq = rep; 3916 rep = NULL; 3917 goto out; 3918 } 3919 NFSM_BUILD(tl, uint32_t *, 3920 NFSX_V4SESSIONID + 4 * NFSX_UNSIGNED); 3921 bcopy(sessionid, tl, NFSX_V4SESSIONID); 3922 tl += NFSX_V4SESSIONID / NFSX_UNSIGNED; 3923 *tl++ = txdr_unsigned(seqid); 3924 *tl++ = txdr_unsigned(slotid); 3925 *tl++ = txdr_unsigned(NFSV4_CBSLOTS - 1); 3926 *tl = txdr_unsigned(NFSV4_CBSLOTS - 1); 3927 } 3928 break; 3929 default: 3930 if (i == 0 && minorvers != NFSV4_MINORVERSION) 3931 error = NFSERR_OPNOTINSESS; 3932 else { 3933 NFSCL_DEBUG(1, "unsupp callback %d\n", op); 3934 error = NFSERR_NOTSUPP; 3935 } 3936 break; 3937 } 3938 if (error) { 3939 if (error == EBADRPC || error == NFSERR_BADXDR) { 3940 nd->nd_repstat = NFSERR_BADXDR; 3941 } else { 3942 nd->nd_repstat = error; 3943 } 3944 error = 0; 3945 } 3946 retops++; 3947 if (nd->nd_repstat) { 3948 *repp = nfscl_errmap(nd, minorvers); 3949 break; 3950 } else 3951 *repp = 0; /* NFS4_OK */ 3952 } 3953 nfsmout: 3954 if (recallp != NULL) 3955 free(recallp, M_NFSLAYRECALL); 3956 if (error) { 3957 if (error == EBADRPC || error == NFSERR_BADXDR) 3958 nd->nd_repstat = NFSERR_BADXDR; 3959 else 3960 printf("nfsv4 comperr1=%d\n", error); 3961 } 3962 if (taglen == -1) { 3963 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED); 3964 *tl++ = 0; 3965 *tl = 0; 3966 } else { 3967 *retopsp = txdr_unsigned(retops); 3968 } 3969 *nd->nd_errp = nfscl_errmap(nd, minorvers); 3970 out: 3971 if (gotseq_ok != 0) { 3972 rep = m_copym(nd->nd_mreq, 0, M_COPYALL, M_WAITOK); 3973 NFSLOCKCLSTATE(); 3974 clp = nfscl_getclntsess(sessionid); 3975 if (clp != NULL) { 3976 tsep = nfsmnt_mdssession(clp->nfsc_nmp); 3977 nfsv4_seqsess_cacherep(slotid, tsep->nfsess_cbslots, 3978 NFSERR_OK, &rep); 3979 NFSUNLOCKCLSTATE(); 3980 } else { 3981 NFSUNLOCKCLSTATE(); 3982 m_freem(rep); 3983 } 3984 } 3985 } 3986 3987 /* 3988 * Generate the next cbident value. Basically just increment a static value 3989 * and then check that it isn't already in the list, if it has wrapped around. 3990 */ 3991 static u_int32_t 3992 nfscl_nextcbident(void) 3993 { 3994 struct nfsclclient *clp; 3995 int matched; 3996 static u_int32_t nextcbident = 0; 3997 static int haswrapped = 0; 3998 3999 nextcbident++; 4000 if (nextcbident == 0) 4001 haswrapped = 1; 4002 if (haswrapped) { 4003 /* 4004 * Search the clientid list for one already using this cbident. 4005 */ 4006 do { 4007 matched = 0; 4008 NFSLOCKCLSTATE(); 4009 LIST_FOREACH(clp, &nfsclhead, nfsc_list) { 4010 if (clp->nfsc_cbident == nextcbident) { 4011 matched = 1; 4012 break; 4013 } 4014 } 4015 NFSUNLOCKCLSTATE(); 4016 if (matched == 1) 4017 nextcbident++; 4018 } while (matched); 4019 } 4020 return (nextcbident); 4021 } 4022 4023 /* 4024 * Get the mount point related to a given cbident or session and busy it. 4025 */ 4026 static mount_t 4027 nfscl_getmnt(int minorvers, uint8_t *sessionid, u_int32_t cbident, 4028 struct nfsclclient **clpp) 4029 { 4030 struct nfsclclient *clp; 4031 mount_t mp; 4032 int error; 4033 struct nfsclsession *tsep; 4034 4035 *clpp = NULL; 4036 NFSLOCKCLSTATE(); 4037 LIST_FOREACH(clp, &nfsclhead, nfsc_list) { 4038 tsep = nfsmnt_mdssession(clp->nfsc_nmp); 4039 if (minorvers == NFSV4_MINORVERSION) { 4040 if (clp->nfsc_cbident == cbident) 4041 break; 4042 } else if (!NFSBCMP(tsep->nfsess_sessionid, sessionid, 4043 NFSX_V4SESSIONID)) 4044 break; 4045 } 4046 if (clp == NULL) { 4047 NFSUNLOCKCLSTATE(); 4048 return (NULL); 4049 } 4050 mp = clp->nfsc_nmp->nm_mountp; 4051 vfs_ref(mp); 4052 NFSUNLOCKCLSTATE(); 4053 error = vfs_busy(mp, 0); 4054 vfs_rel(mp); 4055 if (error != 0) 4056 return (NULL); 4057 *clpp = clp; 4058 return (mp); 4059 } 4060 4061 /* 4062 * Get the clientid pointer related to a given cbident. 4063 */ 4064 static struct nfsclclient * 4065 nfscl_getclnt(u_int32_t cbident) 4066 { 4067 struct nfsclclient *clp; 4068 4069 LIST_FOREACH(clp, &nfsclhead, nfsc_list) 4070 if (clp->nfsc_cbident == cbident) 4071 break; 4072 return (clp); 4073 } 4074 4075 /* 4076 * Get the clientid pointer related to a given sessionid. 4077 */ 4078 static struct nfsclclient * 4079 nfscl_getclntsess(uint8_t *sessionid) 4080 { 4081 struct nfsclclient *clp; 4082 struct nfsclsession *tsep; 4083 4084 LIST_FOREACH(clp, &nfsclhead, nfsc_list) { 4085 tsep = nfsmnt_mdssession(clp->nfsc_nmp); 4086 if (!NFSBCMP(tsep->nfsess_sessionid, sessionid, 4087 NFSX_V4SESSIONID)) 4088 break; 4089 } 4090 return (clp); 4091 } 4092 4093 /* 4094 * Search for a lock conflict locally on the client. A conflict occurs if 4095 * - not same owner and overlapping byte range and at least one of them is 4096 * a write lock or this is an unlock. 4097 */ 4098 static int 4099 nfscl_localconflict(struct nfsclclient *clp, u_int8_t *fhp, int fhlen, 4100 struct nfscllock *nlop, u_int8_t *own, struct nfscldeleg *dp, 4101 struct nfscllock **lopp) 4102 { 4103 struct nfsclopen *op; 4104 int ret; 4105 4106 if (dp != NULL) { 4107 ret = nfscl_checkconflict(&dp->nfsdl_lock, nlop, own, lopp); 4108 if (ret) 4109 return (ret); 4110 } 4111 LIST_FOREACH(op, NFSCLOPENHASH(clp, fhp, fhlen), nfso_hash) { 4112 if (op->nfso_fhlen == fhlen && 4113 !NFSBCMP(op->nfso_fh, fhp, fhlen)) { 4114 ret = nfscl_checkconflict(&op->nfso_lock, nlop, 4115 own, lopp); 4116 if (ret) 4117 return (ret); 4118 } 4119 } 4120 return (0); 4121 } 4122 4123 static int 4124 nfscl_checkconflict(struct nfscllockownerhead *lhp, struct nfscllock *nlop, 4125 u_int8_t *own, struct nfscllock **lopp) 4126 { 4127 struct nfscllockowner *lp; 4128 struct nfscllock *lop; 4129 4130 LIST_FOREACH(lp, lhp, nfsl_list) { 4131 if (NFSBCMP(lp->nfsl_owner, own, NFSV4CL_LOCKNAMELEN)) { 4132 LIST_FOREACH(lop, &lp->nfsl_lock, nfslo_list) { 4133 if (lop->nfslo_first >= nlop->nfslo_end) 4134 break; 4135 if (lop->nfslo_end <= nlop->nfslo_first) 4136 continue; 4137 if (lop->nfslo_type == F_WRLCK || 4138 nlop->nfslo_type == F_WRLCK || 4139 nlop->nfslo_type == F_UNLCK) { 4140 if (lopp != NULL) 4141 *lopp = lop; 4142 return (NFSERR_DENIED); 4143 } 4144 } 4145 } 4146 } 4147 return (0); 4148 } 4149 4150 /* 4151 * Check for a local conflicting lock. 4152 */ 4153 int 4154 nfscl_lockt(vnode_t vp, struct nfsclclient *clp, u_int64_t off, 4155 u_int64_t len, struct flock *fl, NFSPROC_T *p, void *id, int flags) 4156 { 4157 struct nfscllock *lop, nlck; 4158 struct nfscldeleg *dp; 4159 struct nfsnode *np; 4160 u_int8_t own[NFSV4CL_LOCKNAMELEN]; 4161 int error; 4162 4163 nlck.nfslo_type = fl->l_type; 4164 nlck.nfslo_first = off; 4165 if (len == NFS64BITSSET) { 4166 nlck.nfslo_end = NFS64BITSSET; 4167 } else { 4168 nlck.nfslo_end = off + len; 4169 if (nlck.nfslo_end <= nlck.nfslo_first) 4170 return (NFSERR_INVAL); 4171 } 4172 np = VTONFS(vp); 4173 nfscl_filllockowner(id, own, flags); 4174 NFSLOCKCLSTATE(); 4175 dp = nfscl_finddeleg(clp, np->n_fhp->nfh_fh, np->n_fhp->nfh_len); 4176 error = nfscl_localconflict(clp, np->n_fhp->nfh_fh, np->n_fhp->nfh_len, 4177 &nlck, own, dp, &lop); 4178 if (error != 0) { 4179 fl->l_whence = SEEK_SET; 4180 fl->l_start = lop->nfslo_first; 4181 if (lop->nfslo_end == NFS64BITSSET) 4182 fl->l_len = 0; 4183 else 4184 fl->l_len = lop->nfslo_end - lop->nfslo_first; 4185 fl->l_pid = (pid_t)0; 4186 fl->l_type = lop->nfslo_type; 4187 error = -1; /* no RPC required */ 4188 } else if (dp != NULL && ((dp->nfsdl_flags & NFSCLDL_WRITE) || 4189 fl->l_type == F_RDLCK)) { 4190 /* 4191 * The delegation ensures that there isn't a conflicting 4192 * lock on the server, so return -1 to indicate an RPC 4193 * isn't required. 4194 */ 4195 fl->l_type = F_UNLCK; 4196 error = -1; 4197 } 4198 NFSUNLOCKCLSTATE(); 4199 return (error); 4200 } 4201 4202 /* 4203 * Handle Recall of a delegation. 4204 * The clp must be exclusive locked when this is called. 4205 */ 4206 static int 4207 nfscl_recalldeleg(struct nfsclclient *clp, struct nfsmount *nmp, 4208 struct nfscldeleg *dp, vnode_t vp, struct ucred *cred, NFSPROC_T *p, 4209 int called_from_renewthread, vnode_t *vpp) 4210 { 4211 struct nfsclowner *owp, *lowp, *nowp; 4212 struct nfsclopen *op, *lop; 4213 struct nfscllockowner *lp; 4214 struct nfscllock *lckp; 4215 struct nfsnode *np; 4216 int error = 0, ret; 4217 4218 if (vp == NULL) { 4219 KASSERT(vpp != NULL, ("nfscl_recalldeleg: vpp NULL")); 4220 *vpp = NULL; 4221 /* 4222 * First, get a vnode for the file. This is needed to do RPCs. 4223 */ 4224 ret = nfscl_ngetreopen(nmp->nm_mountp, dp->nfsdl_fh, 4225 dp->nfsdl_fhlen, p, &np); 4226 if (ret) { 4227 /* 4228 * File isn't open, so nothing to move over to the 4229 * server. 4230 */ 4231 return (0); 4232 } 4233 vp = NFSTOV(np); 4234 *vpp = vp; 4235 } else { 4236 np = VTONFS(vp); 4237 } 4238 dp->nfsdl_flags &= ~NFSCLDL_MODTIMESET; 4239 4240 /* 4241 * Ok, if it's a write delegation, flush data to the server, so 4242 * that close/open consistency is retained. 4243 */ 4244 ret = 0; 4245 NFSLOCKNODE(np); 4246 if ((dp->nfsdl_flags & NFSCLDL_WRITE) && (np->n_flag & NMODIFIED)) { 4247 np->n_flag |= NDELEGRECALL; 4248 NFSUNLOCKNODE(np); 4249 ret = ncl_flush(vp, MNT_WAIT, p, 1, called_from_renewthread); 4250 NFSLOCKNODE(np); 4251 np->n_flag &= ~NDELEGRECALL; 4252 } 4253 NFSINVALATTRCACHE(np); 4254 NFSUNLOCKNODE(np); 4255 if (ret == EIO && called_from_renewthread != 0) { 4256 /* 4257 * If the flush failed with EIO for the renew thread, 4258 * return now, so that the dirty buffer will be flushed 4259 * later. 4260 */ 4261 return (ret); 4262 } 4263 4264 /* 4265 * Now, for each openowner with opens issued locally, move them 4266 * over to state against the server. 4267 */ 4268 LIST_FOREACH(lowp, &dp->nfsdl_owner, nfsow_list) { 4269 lop = LIST_FIRST(&lowp->nfsow_open); 4270 if (lop != NULL) { 4271 if (LIST_NEXT(lop, nfso_list) != NULL) 4272 panic("nfsdlg mult opens"); 4273 /* 4274 * Look for the same openowner against the server. 4275 */ 4276 LIST_FOREACH(owp, &clp->nfsc_owner, nfsow_list) { 4277 if (!NFSBCMP(lowp->nfsow_owner, 4278 owp->nfsow_owner, NFSV4CL_LOCKNAMELEN)) { 4279 newnfs_copycred(&dp->nfsdl_cred, cred); 4280 ret = nfscl_moveopen(vp, clp, nmp, lop, 4281 owp, dp, cred, p); 4282 if (ret == NFSERR_STALECLIENTID || 4283 ret == NFSERR_STALEDONTRECOVER || 4284 ret == NFSERR_BADSESSION) 4285 return (ret); 4286 if (ret) { 4287 nfscl_freeopen(lop, 1, true); 4288 if (!error) 4289 error = ret; 4290 } 4291 break; 4292 } 4293 } 4294 4295 /* 4296 * If no openowner found, create one and get an open 4297 * for it. 4298 */ 4299 if (owp == NULL) { 4300 nowp = malloc( 4301 sizeof (struct nfsclowner), M_NFSCLOWNER, 4302 M_WAITOK); 4303 nfscl_newopen(clp, NULL, &owp, &nowp, &op, 4304 NULL, lowp->nfsow_owner, dp->nfsdl_fh, 4305 dp->nfsdl_fhlen, NULL, NULL); 4306 newnfs_copycred(&dp->nfsdl_cred, cred); 4307 ret = nfscl_moveopen(vp, clp, nmp, lop, 4308 owp, dp, cred, p); 4309 if (ret) { 4310 nfscl_freeopenowner(owp, 0); 4311 if (ret == NFSERR_STALECLIENTID || 4312 ret == NFSERR_STALEDONTRECOVER || 4313 ret == NFSERR_BADSESSION) 4314 return (ret); 4315 if (ret) { 4316 nfscl_freeopen(lop, 1, true); 4317 if (!error) 4318 error = ret; 4319 } 4320 } 4321 } 4322 } 4323 } 4324 4325 /* 4326 * Now, get byte range locks for any locks done locally. 4327 */ 4328 LIST_FOREACH(lp, &dp->nfsdl_lock, nfsl_list) { 4329 LIST_FOREACH(lckp, &lp->nfsl_lock, nfslo_list) { 4330 newnfs_copycred(&dp->nfsdl_cred, cred); 4331 ret = nfscl_relock(vp, clp, nmp, lp, lckp, cred, p); 4332 if (ret == NFSERR_STALESTATEID || 4333 ret == NFSERR_STALEDONTRECOVER || 4334 ret == NFSERR_STALECLIENTID || 4335 ret == NFSERR_BADSESSION) 4336 return (ret); 4337 if (ret && !error) 4338 error = ret; 4339 } 4340 } 4341 return (error); 4342 } 4343 4344 /* 4345 * Move a locally issued open over to an owner on the state list. 4346 * SIDE EFFECT: If it needs to sleep (do an rpc), it unlocks clstate and 4347 * returns with it unlocked. 4348 */ 4349 static int 4350 nfscl_moveopen(vnode_t vp, struct nfsclclient *clp, struct nfsmount *nmp, 4351 struct nfsclopen *lop, struct nfsclowner *owp, struct nfscldeleg *dp, 4352 struct ucred *cred, NFSPROC_T *p) 4353 { 4354 struct nfsclopen *op, *nop; 4355 struct nfscldeleg *ndp; 4356 struct nfsnode *np; 4357 int error = 0, newone; 4358 4359 /* 4360 * First, look for an appropriate open, If found, just increment the 4361 * opencnt in it. 4362 */ 4363 LIST_FOREACH(op, &owp->nfsow_open, nfso_list) { 4364 if ((op->nfso_mode & lop->nfso_mode) == lop->nfso_mode && 4365 op->nfso_fhlen == lop->nfso_fhlen && 4366 !NFSBCMP(op->nfso_fh, lop->nfso_fh, op->nfso_fhlen)) { 4367 op->nfso_opencnt += lop->nfso_opencnt; 4368 nfscl_freeopen(lop, 1, true); 4369 return (0); 4370 } 4371 } 4372 4373 /* No appropriate open, so we have to do one against the server. */ 4374 np = VTONFS(vp); 4375 nop = malloc(sizeof (struct nfsclopen) + 4376 lop->nfso_fhlen - 1, M_NFSCLOPEN, M_WAITOK); 4377 nop->nfso_hash.le_prev = NULL; 4378 newone = 0; 4379 nfscl_newopen(clp, NULL, &owp, NULL, &op, &nop, owp->nfsow_owner, 4380 lop->nfso_fh, lop->nfso_fhlen, cred, &newone); 4381 ndp = dp; 4382 error = nfscl_tryopen(nmp, vp, np->n_v4->n4_data, np->n_v4->n4_fhlen, 4383 lop->nfso_fh, lop->nfso_fhlen, lop->nfso_mode, op, 4384 NFS4NODENAME(np->n_v4), np->n_v4->n4_namelen, &ndp, 0, 0, cred, p); 4385 if (error) { 4386 if (newone) 4387 nfscl_freeopen(op, 0, true); 4388 } else { 4389 op->nfso_mode |= lop->nfso_mode; 4390 op->nfso_opencnt += lop->nfso_opencnt; 4391 nfscl_freeopen(lop, 1, true); 4392 } 4393 if (nop != NULL) 4394 free(nop, M_NFSCLOPEN); 4395 if (ndp != NULL) { 4396 /* 4397 * What should I do with the returned delegation, since the 4398 * delegation is being recalled? For now, just printf and 4399 * through it away. 4400 */ 4401 printf("Moveopen returned deleg\n"); 4402 free(ndp, M_NFSCLDELEG); 4403 } 4404 return (error); 4405 } 4406 4407 /* 4408 * Recall all delegations on this client. 4409 */ 4410 static void 4411 nfscl_totalrecall(struct nfsclclient *clp) 4412 { 4413 struct nfscldeleg *dp; 4414 4415 TAILQ_FOREACH(dp, &clp->nfsc_deleg, nfsdl_list) { 4416 if ((dp->nfsdl_flags & NFSCLDL_DELEGRET) == 0) 4417 dp->nfsdl_flags |= NFSCLDL_RECALL; 4418 } 4419 } 4420 4421 /* 4422 * Relock byte ranges. Called for delegation recall and state expiry. 4423 */ 4424 static int 4425 nfscl_relock(vnode_t vp, struct nfsclclient *clp, struct nfsmount *nmp, 4426 struct nfscllockowner *lp, struct nfscllock *lop, struct ucred *cred, 4427 NFSPROC_T *p) 4428 { 4429 struct nfscllockowner *nlp; 4430 struct nfsfh *nfhp; 4431 struct nfsnode *np; 4432 u_int64_t off, len; 4433 int error, newone, donelocally; 4434 4435 if (NFSHASNFSV4N(nmp) && NFSHASONEOPENOWN(nmp)) { 4436 np = VTONFS(vp); 4437 NFSLOCKNODE(np); 4438 np->n_flag |= NMIGHTBELOCKED; 4439 NFSUNLOCKNODE(np); 4440 } 4441 4442 off = lop->nfslo_first; 4443 len = lop->nfslo_end - lop->nfslo_first; 4444 error = nfscl_getbytelock(vp, off, len, lop->nfslo_type, cred, p, 4445 clp, 1, NULL, lp->nfsl_lockflags, lp->nfsl_owner, 4446 lp->nfsl_openowner, &nlp, &newone, &donelocally); 4447 if (error || donelocally) 4448 return (error); 4449 nfhp = VTONFS(vp)->n_fhp; 4450 error = nfscl_trylock(nmp, vp, nfhp->nfh_fh, 4451 nfhp->nfh_len, nlp, newone, 0, off, 4452 len, lop->nfslo_type, cred, p); 4453 if (error) 4454 nfscl_freelockowner(nlp, 0); 4455 return (error); 4456 } 4457 4458 /* 4459 * Called to re-open a file. Basically get a vnode for the file handle 4460 * and then call nfsrpc_openrpc() to do the rest. 4461 */ 4462 static int 4463 nfsrpc_reopen(struct nfsmount *nmp, u_int8_t *fhp, int fhlen, 4464 u_int32_t mode, struct nfsclopen *op, struct nfscldeleg **dpp, 4465 struct ucred *cred, NFSPROC_T *p) 4466 { 4467 struct nfsnode *np; 4468 vnode_t vp; 4469 int error; 4470 4471 error = nfscl_ngetreopen(nmp->nm_mountp, fhp, fhlen, p, &np); 4472 if (error) 4473 return (error); 4474 vp = NFSTOV(np); 4475 if (np->n_v4 != NULL) { 4476 error = nfscl_tryopen(nmp, vp, np->n_v4->n4_data, 4477 np->n_v4->n4_fhlen, fhp, fhlen, mode, op, 4478 NFS4NODENAME(np->n_v4), np->n_v4->n4_namelen, dpp, 0, 0, 4479 cred, p); 4480 } else { 4481 error = EINVAL; 4482 } 4483 vrele(vp); 4484 return (error); 4485 } 4486 4487 /* 4488 * Try an open against the server. Just call nfsrpc_openrpc(), retrying while 4489 * NFSERR_DELAY. Also, try system credentials, if the passed in credentials 4490 * fail. 4491 */ 4492 static int 4493 nfscl_tryopen(struct nfsmount *nmp, vnode_t vp, u_int8_t *fhp, int fhlen, 4494 u_int8_t *newfhp, int newfhlen, u_int32_t mode, struct nfsclopen *op, 4495 u_int8_t *name, int namelen, struct nfscldeleg **ndpp, 4496 int reclaim, u_int32_t delegtype, struct ucred *cred, NFSPROC_T *p) 4497 { 4498 int error; 4499 4500 do { 4501 error = nfsrpc_openrpc(nmp, vp, fhp, fhlen, newfhp, newfhlen, 4502 mode, op, name, namelen, ndpp, reclaim, delegtype, cred, p, 4503 0, 0); 4504 if (error == NFSERR_DELAY) 4505 (void) nfs_catnap(PZERO, error, "nfstryop"); 4506 } while (error == NFSERR_DELAY); 4507 if (error == EAUTH || error == EACCES) { 4508 /* Try again using system credentials */ 4509 newnfs_setroot(cred); 4510 do { 4511 error = nfsrpc_openrpc(nmp, vp, fhp, fhlen, newfhp, 4512 newfhlen, mode, op, name, namelen, ndpp, reclaim, 4513 delegtype, cred, p, 1, 0); 4514 if (error == NFSERR_DELAY) 4515 (void) nfs_catnap(PZERO, error, "nfstryop"); 4516 } while (error == NFSERR_DELAY); 4517 } 4518 return (error); 4519 } 4520 4521 /* 4522 * Try a byte range lock. Just loop on nfsrpc_lock() while it returns 4523 * NFSERR_DELAY. Also, retry with system credentials, if the provided 4524 * cred don't work. 4525 */ 4526 static int 4527 nfscl_trylock(struct nfsmount *nmp, vnode_t vp, u_int8_t *fhp, 4528 int fhlen, struct nfscllockowner *nlp, int newone, int reclaim, 4529 u_int64_t off, u_int64_t len, short type, struct ucred *cred, NFSPROC_T *p) 4530 { 4531 struct nfsrv_descript nfsd, *nd = &nfsd; 4532 int error; 4533 4534 do { 4535 error = nfsrpc_lock(nd, nmp, vp, fhp, fhlen, nlp, newone, 4536 reclaim, off, len, type, cred, p, 0); 4537 if (!error && nd->nd_repstat == NFSERR_DELAY) 4538 (void) nfs_catnap(PZERO, (int)nd->nd_repstat, 4539 "nfstrylck"); 4540 } while (!error && nd->nd_repstat == NFSERR_DELAY); 4541 if (!error) 4542 error = nd->nd_repstat; 4543 if (error == EAUTH || error == EACCES) { 4544 /* Try again using root credentials */ 4545 newnfs_setroot(cred); 4546 do { 4547 error = nfsrpc_lock(nd, nmp, vp, fhp, fhlen, nlp, 4548 newone, reclaim, off, len, type, cred, p, 1); 4549 if (!error && nd->nd_repstat == NFSERR_DELAY) 4550 (void) nfs_catnap(PZERO, (int)nd->nd_repstat, 4551 "nfstrylck"); 4552 } while (!error && nd->nd_repstat == NFSERR_DELAY); 4553 if (!error) 4554 error = nd->nd_repstat; 4555 } 4556 return (error); 4557 } 4558 4559 /* 4560 * Try a delegreturn against the server. Just call nfsrpc_delegreturn(), 4561 * retrying while NFSERR_DELAY. Also, try system credentials, if the passed in 4562 * credentials fail. 4563 */ 4564 int 4565 nfscl_trydelegreturn(struct nfscldeleg *dp, struct ucred *cred, 4566 struct nfsmount *nmp, NFSPROC_T *p) 4567 { 4568 int error; 4569 4570 do { 4571 error = nfsrpc_delegreturn(dp, cred, nmp, p, 0); 4572 if (error == NFSERR_DELAY) 4573 (void) nfs_catnap(PZERO, error, "nfstrydp"); 4574 } while (error == NFSERR_DELAY); 4575 if (error == EAUTH || error == EACCES) { 4576 /* Try again using system credentials */ 4577 newnfs_setroot(cred); 4578 do { 4579 error = nfsrpc_delegreturn(dp, cred, nmp, p, 1); 4580 if (error == NFSERR_DELAY) 4581 (void) nfs_catnap(PZERO, error, "nfstrydp"); 4582 } while (error == NFSERR_DELAY); 4583 } 4584 return (error); 4585 } 4586 4587 /* 4588 * Try a close against the server. Just call nfsrpc_closerpc(), 4589 * retrying while NFSERR_DELAY. Also, try system credentials, if the passed in 4590 * credentials fail. 4591 */ 4592 int 4593 nfscl_tryclose(struct nfsclopen *op, struct ucred *cred, 4594 struct nfsmount *nmp, NFSPROC_T *p, bool loop_on_delayed) 4595 { 4596 struct nfsrv_descript nfsd, *nd = &nfsd; 4597 int error; 4598 4599 do { 4600 error = nfsrpc_closerpc(nd, nmp, op, cred, p, 0); 4601 if (loop_on_delayed && error == NFSERR_DELAY) 4602 (void) nfs_catnap(PZERO, error, "nfstrycl"); 4603 } while (loop_on_delayed && error == NFSERR_DELAY); 4604 if (error == EAUTH || error == EACCES) { 4605 /* Try again using system credentials */ 4606 newnfs_setroot(cred); 4607 do { 4608 error = nfsrpc_closerpc(nd, nmp, op, cred, p, 1); 4609 if (loop_on_delayed && error == NFSERR_DELAY) 4610 (void) nfs_catnap(PZERO, error, "nfstrycl"); 4611 } while (loop_on_delayed && error == NFSERR_DELAY); 4612 } 4613 return (error); 4614 } 4615 4616 /* 4617 * Decide if a delegation on a file permits close without flushing writes 4618 * to the server. This might be a big performance win in some environments. 4619 * (Not useful until the client does caching on local stable storage.) 4620 */ 4621 int 4622 nfscl_mustflush(vnode_t vp) 4623 { 4624 struct nfsclclient *clp; 4625 struct nfscldeleg *dp; 4626 struct nfsnode *np; 4627 struct nfsmount *nmp; 4628 4629 np = VTONFS(vp); 4630 nmp = VFSTONFS(vp->v_mount); 4631 if (!NFSHASNFSV4(nmp)) 4632 return (1); 4633 NFSLOCKMNT(nmp); 4634 if ((nmp->nm_privflag & NFSMNTP_DELEGISSUED) == 0) { 4635 NFSUNLOCKMNT(nmp); 4636 return (1); 4637 } 4638 NFSUNLOCKMNT(nmp); 4639 NFSLOCKCLSTATE(); 4640 clp = nfscl_findcl(nmp); 4641 if (clp == NULL) { 4642 NFSUNLOCKCLSTATE(); 4643 return (1); 4644 } 4645 dp = nfscl_finddeleg(clp, np->n_fhp->nfh_fh, np->n_fhp->nfh_len); 4646 if (dp != NULL && (dp->nfsdl_flags & 4647 (NFSCLDL_WRITE | NFSCLDL_RECALL | NFSCLDL_DELEGRET)) == 4648 NFSCLDL_WRITE && 4649 (dp->nfsdl_sizelimit >= np->n_size || 4650 !NFSHASSTRICT3530(nmp))) { 4651 NFSUNLOCKCLSTATE(); 4652 return (0); 4653 } 4654 NFSUNLOCKCLSTATE(); 4655 return (1); 4656 } 4657 4658 /* 4659 * See if a (write) delegation exists for this file. 4660 */ 4661 int 4662 nfscl_nodeleg(vnode_t vp, int writedeleg) 4663 { 4664 struct nfsclclient *clp; 4665 struct nfscldeleg *dp; 4666 struct nfsnode *np; 4667 struct nfsmount *nmp; 4668 4669 np = VTONFS(vp); 4670 nmp = VFSTONFS(vp->v_mount); 4671 if (!NFSHASNFSV4(nmp)) 4672 return (1); 4673 NFSLOCKMNT(nmp); 4674 if ((nmp->nm_privflag & NFSMNTP_DELEGISSUED) == 0) { 4675 NFSUNLOCKMNT(nmp); 4676 return (1); 4677 } 4678 NFSUNLOCKMNT(nmp); 4679 NFSLOCKCLSTATE(); 4680 clp = nfscl_findcl(nmp); 4681 if (clp == NULL) { 4682 NFSUNLOCKCLSTATE(); 4683 return (1); 4684 } 4685 dp = nfscl_finddeleg(clp, np->n_fhp->nfh_fh, np->n_fhp->nfh_len); 4686 if (dp != NULL && 4687 (dp->nfsdl_flags & (NFSCLDL_RECALL | NFSCLDL_DELEGRET)) == 0 && 4688 (writedeleg == 0 || (dp->nfsdl_flags & NFSCLDL_WRITE) == 4689 NFSCLDL_WRITE)) { 4690 NFSUNLOCKCLSTATE(); 4691 return (0); 4692 } 4693 NFSUNLOCKCLSTATE(); 4694 return (1); 4695 } 4696 4697 /* 4698 * Look for an associated delegation that should be DelegReturned. 4699 */ 4700 int 4701 nfscl_removedeleg(vnode_t vp, NFSPROC_T *p, nfsv4stateid_t *stp) 4702 { 4703 struct nfsclclient *clp; 4704 struct nfscldeleg *dp; 4705 struct nfsclowner *owp; 4706 struct nfscllockowner *lp; 4707 struct nfsmount *nmp; 4708 struct mount *mp; 4709 struct ucred *cred; 4710 struct nfsnode *np; 4711 int igotlock = 0, triedrecall = 0, needsrecall, retcnt = 0, islept; 4712 4713 nmp = VFSTONFS(vp->v_mount); 4714 if (NFSHASPNFS(nmp)) 4715 return (retcnt); 4716 NFSLOCKMNT(nmp); 4717 if ((nmp->nm_privflag & NFSMNTP_DELEGISSUED) == 0) { 4718 NFSUNLOCKMNT(nmp); 4719 return (retcnt); 4720 } 4721 NFSUNLOCKMNT(nmp); 4722 np = VTONFS(vp); 4723 mp = nmp->nm_mountp; 4724 NFSLOCKCLSTATE(); 4725 /* 4726 * Loop around waiting for: 4727 * - outstanding I/O operations on delegations to complete 4728 * - for a delegation on vp that has state, lock the client and 4729 * do a recall 4730 * - return delegation with no state 4731 */ 4732 while (1) { 4733 clp = nfscl_findcl(nmp); 4734 if (clp == NULL) { 4735 NFSUNLOCKCLSTATE(); 4736 return (retcnt); 4737 } 4738 dp = nfscl_finddeleg(clp, np->n_fhp->nfh_fh, 4739 np->n_fhp->nfh_len); 4740 if (dp != NULL) { 4741 /* 4742 * Wait for outstanding I/O ops to be done. 4743 */ 4744 if (dp->nfsdl_rwlock.nfslock_usecnt > 0) { 4745 if (igotlock) { 4746 nfsv4_unlock(&clp->nfsc_lock, 0); 4747 igotlock = 0; 4748 } 4749 dp->nfsdl_rwlock.nfslock_lock |= NFSV4LOCK_WANTED; 4750 msleep(&dp->nfsdl_rwlock, NFSCLSTATEMUTEXPTR, PZERO, 4751 "nfscld", hz); 4752 if (NFSCL_FORCEDISM(mp)) { 4753 dp->nfsdl_flags &= ~NFSCLDL_DELEGRET; 4754 NFSUNLOCKCLSTATE(); 4755 return (0); 4756 } 4757 continue; 4758 } 4759 needsrecall = 0; 4760 LIST_FOREACH(owp, &dp->nfsdl_owner, nfsow_list) { 4761 if (!LIST_EMPTY(&owp->nfsow_open)) { 4762 needsrecall = 1; 4763 break; 4764 } 4765 } 4766 if (!needsrecall) { 4767 LIST_FOREACH(lp, &dp->nfsdl_lock, nfsl_list) { 4768 if (!LIST_EMPTY(&lp->nfsl_lock)) { 4769 needsrecall = 1; 4770 break; 4771 } 4772 } 4773 } 4774 if (needsrecall && !triedrecall) { 4775 dp->nfsdl_flags |= NFSCLDL_DELEGRET; 4776 islept = 0; 4777 while (!igotlock) { 4778 igotlock = nfsv4_lock(&clp->nfsc_lock, 1, 4779 &islept, NFSCLSTATEMUTEXPTR, mp); 4780 if (NFSCL_FORCEDISM(mp)) { 4781 dp->nfsdl_flags &= ~NFSCLDL_DELEGRET; 4782 if (igotlock) 4783 nfsv4_unlock(&clp->nfsc_lock, 0); 4784 NFSUNLOCKCLSTATE(); 4785 return (0); 4786 } 4787 if (islept) 4788 break; 4789 } 4790 if (islept) 4791 continue; 4792 NFSUNLOCKCLSTATE(); 4793 cred = newnfs_getcred(); 4794 newnfs_copycred(&dp->nfsdl_cred, cred); 4795 nfscl_recalldeleg(clp, nmp, dp, vp, cred, p, 0, NULL); 4796 NFSFREECRED(cred); 4797 triedrecall = 1; 4798 NFSLOCKCLSTATE(); 4799 nfsv4_unlock(&clp->nfsc_lock, 0); 4800 igotlock = 0; 4801 continue; 4802 } 4803 *stp = dp->nfsdl_stateid; 4804 retcnt = 1; 4805 nfscl_cleandeleg(dp); 4806 nfscl_freedeleg(&clp->nfsc_deleg, dp, true); 4807 } 4808 if (igotlock) 4809 nfsv4_unlock(&clp->nfsc_lock, 0); 4810 NFSUNLOCKCLSTATE(); 4811 return (retcnt); 4812 } 4813 } 4814 4815 /* 4816 * Look for associated delegation(s) that should be DelegReturned. 4817 */ 4818 int 4819 nfscl_renamedeleg(vnode_t fvp, nfsv4stateid_t *fstp, int *gotfdp, vnode_t tvp, 4820 nfsv4stateid_t *tstp, int *gottdp, NFSPROC_T *p) 4821 { 4822 struct nfsclclient *clp; 4823 struct nfscldeleg *dp; 4824 struct nfsclowner *owp; 4825 struct nfscllockowner *lp; 4826 struct nfsmount *nmp; 4827 struct mount *mp; 4828 struct ucred *cred; 4829 struct nfsnode *np; 4830 int igotlock = 0, triedrecall = 0, needsrecall, retcnt = 0, islept; 4831 4832 nmp = VFSTONFS(fvp->v_mount); 4833 *gotfdp = 0; 4834 *gottdp = 0; 4835 if (NFSHASPNFS(nmp)) 4836 return (retcnt); 4837 NFSLOCKMNT(nmp); 4838 if ((nmp->nm_privflag & NFSMNTP_DELEGISSUED) == 0) { 4839 NFSUNLOCKMNT(nmp); 4840 return (retcnt); 4841 } 4842 NFSUNLOCKMNT(nmp); 4843 mp = nmp->nm_mountp; 4844 NFSLOCKCLSTATE(); 4845 /* 4846 * Loop around waiting for: 4847 * - outstanding I/O operations on delegations to complete 4848 * - for a delegation on fvp that has state, lock the client and 4849 * do a recall 4850 * - return delegation(s) with no state. 4851 */ 4852 while (1) { 4853 clp = nfscl_findcl(nmp); 4854 if (clp == NULL) { 4855 NFSUNLOCKCLSTATE(); 4856 return (retcnt); 4857 } 4858 np = VTONFS(fvp); 4859 dp = nfscl_finddeleg(clp, np->n_fhp->nfh_fh, 4860 np->n_fhp->nfh_len); 4861 if (dp != NULL && *gotfdp == 0) { 4862 /* 4863 * Wait for outstanding I/O ops to be done. 4864 */ 4865 if (dp->nfsdl_rwlock.nfslock_usecnt > 0) { 4866 if (igotlock) { 4867 nfsv4_unlock(&clp->nfsc_lock, 0); 4868 igotlock = 0; 4869 } 4870 dp->nfsdl_rwlock.nfslock_lock |= NFSV4LOCK_WANTED; 4871 msleep(&dp->nfsdl_rwlock, NFSCLSTATEMUTEXPTR, PZERO, 4872 "nfscld", hz); 4873 if (NFSCL_FORCEDISM(mp)) { 4874 dp->nfsdl_flags &= ~NFSCLDL_DELEGRET; 4875 NFSUNLOCKCLSTATE(); 4876 *gotfdp = 0; 4877 *gottdp = 0; 4878 return (0); 4879 } 4880 continue; 4881 } 4882 needsrecall = 0; 4883 LIST_FOREACH(owp, &dp->nfsdl_owner, nfsow_list) { 4884 if (!LIST_EMPTY(&owp->nfsow_open)) { 4885 needsrecall = 1; 4886 break; 4887 } 4888 } 4889 if (!needsrecall) { 4890 LIST_FOREACH(lp, &dp->nfsdl_lock, nfsl_list) { 4891 if (!LIST_EMPTY(&lp->nfsl_lock)) { 4892 needsrecall = 1; 4893 break; 4894 } 4895 } 4896 } 4897 if (needsrecall && !triedrecall) { 4898 dp->nfsdl_flags |= NFSCLDL_DELEGRET; 4899 islept = 0; 4900 while (!igotlock) { 4901 igotlock = nfsv4_lock(&clp->nfsc_lock, 1, 4902 &islept, NFSCLSTATEMUTEXPTR, mp); 4903 if (NFSCL_FORCEDISM(mp)) { 4904 dp->nfsdl_flags &= ~NFSCLDL_DELEGRET; 4905 if (igotlock) 4906 nfsv4_unlock(&clp->nfsc_lock, 0); 4907 NFSUNLOCKCLSTATE(); 4908 *gotfdp = 0; 4909 *gottdp = 0; 4910 return (0); 4911 } 4912 if (islept) 4913 break; 4914 } 4915 if (islept) 4916 continue; 4917 NFSUNLOCKCLSTATE(); 4918 cred = newnfs_getcred(); 4919 newnfs_copycred(&dp->nfsdl_cred, cred); 4920 nfscl_recalldeleg(clp, nmp, dp, fvp, cred, p, 0, NULL); 4921 NFSFREECRED(cred); 4922 triedrecall = 1; 4923 NFSLOCKCLSTATE(); 4924 nfsv4_unlock(&clp->nfsc_lock, 0); 4925 igotlock = 0; 4926 continue; 4927 } 4928 *fstp = dp->nfsdl_stateid; 4929 retcnt++; 4930 *gotfdp = 1; 4931 nfscl_cleandeleg(dp); 4932 nfscl_freedeleg(&clp->nfsc_deleg, dp, true); 4933 } 4934 if (igotlock) { 4935 nfsv4_unlock(&clp->nfsc_lock, 0); 4936 igotlock = 0; 4937 } 4938 if (tvp != NULL) { 4939 np = VTONFS(tvp); 4940 dp = nfscl_finddeleg(clp, np->n_fhp->nfh_fh, 4941 np->n_fhp->nfh_len); 4942 if (dp != NULL && *gottdp == 0) { 4943 /* 4944 * Wait for outstanding I/O ops to be done. 4945 */ 4946 if (dp->nfsdl_rwlock.nfslock_usecnt > 0) { 4947 dp->nfsdl_rwlock.nfslock_lock |= NFSV4LOCK_WANTED; 4948 msleep(&dp->nfsdl_rwlock, NFSCLSTATEMUTEXPTR, PZERO, 4949 "nfscld", hz); 4950 if (NFSCL_FORCEDISM(mp)) { 4951 NFSUNLOCKCLSTATE(); 4952 *gotfdp = 0; 4953 *gottdp = 0; 4954 return (0); 4955 } 4956 continue; 4957 } 4958 LIST_FOREACH(owp, &dp->nfsdl_owner, nfsow_list) { 4959 if (!LIST_EMPTY(&owp->nfsow_open)) { 4960 NFSUNLOCKCLSTATE(); 4961 return (retcnt); 4962 } 4963 } 4964 LIST_FOREACH(lp, &dp->nfsdl_lock, nfsl_list) { 4965 if (!LIST_EMPTY(&lp->nfsl_lock)) { 4966 NFSUNLOCKCLSTATE(); 4967 return (retcnt); 4968 } 4969 } 4970 *tstp = dp->nfsdl_stateid; 4971 retcnt++; 4972 *gottdp = 1; 4973 nfscl_cleandeleg(dp); 4974 nfscl_freedeleg(&clp->nfsc_deleg, dp, true); 4975 } 4976 } 4977 NFSUNLOCKCLSTATE(); 4978 return (retcnt); 4979 } 4980 } 4981 4982 /* 4983 * Get a reference on the clientid associated with the mount point. 4984 * Return 1 if success, 0 otherwise. 4985 */ 4986 int 4987 nfscl_getref(struct nfsmount *nmp) 4988 { 4989 struct nfsclclient *clp; 4990 int ret; 4991 4992 NFSLOCKCLSTATE(); 4993 clp = nfscl_findcl(nmp); 4994 if (clp == NULL) { 4995 NFSUNLOCKCLSTATE(); 4996 return (0); 4997 } 4998 nfsv4_getref(&clp->nfsc_lock, NULL, NFSCLSTATEMUTEXPTR, nmp->nm_mountp); 4999 ret = 1; 5000 if (NFSCL_FORCEDISM(nmp->nm_mountp)) 5001 ret = 0; 5002 NFSUNLOCKCLSTATE(); 5003 return (ret); 5004 } 5005 5006 /* 5007 * Release a reference on a clientid acquired with the above call. 5008 */ 5009 void 5010 nfscl_relref(struct nfsmount *nmp) 5011 { 5012 struct nfsclclient *clp; 5013 5014 NFSLOCKCLSTATE(); 5015 clp = nfscl_findcl(nmp); 5016 if (clp == NULL) { 5017 NFSUNLOCKCLSTATE(); 5018 return; 5019 } 5020 nfsv4_relref(&clp->nfsc_lock); 5021 NFSUNLOCKCLSTATE(); 5022 } 5023 5024 /* 5025 * Save the size attribute in the delegation, since the nfsnode 5026 * is going away. 5027 */ 5028 void 5029 nfscl_reclaimnode(vnode_t vp) 5030 { 5031 struct nfsclclient *clp; 5032 struct nfscldeleg *dp; 5033 struct nfsnode *np = VTONFS(vp); 5034 struct nfsmount *nmp; 5035 5036 nmp = VFSTONFS(vp->v_mount); 5037 if (!NFSHASNFSV4(nmp)) 5038 return; 5039 NFSLOCKCLSTATE(); 5040 clp = nfscl_findcl(nmp); 5041 if (clp == NULL) { 5042 NFSUNLOCKCLSTATE(); 5043 return; 5044 } 5045 dp = nfscl_finddeleg(clp, np->n_fhp->nfh_fh, np->n_fhp->nfh_len); 5046 if (dp != NULL && (dp->nfsdl_flags & NFSCLDL_WRITE)) 5047 dp->nfsdl_size = np->n_size; 5048 NFSUNLOCKCLSTATE(); 5049 } 5050 5051 /* 5052 * Get the saved size attribute in the delegation, since it is a 5053 * newly allocated nfsnode. 5054 */ 5055 void 5056 nfscl_newnode(vnode_t vp) 5057 { 5058 struct nfsclclient *clp; 5059 struct nfscldeleg *dp; 5060 struct nfsnode *np = VTONFS(vp); 5061 struct nfsmount *nmp; 5062 5063 nmp = VFSTONFS(vp->v_mount); 5064 if (!NFSHASNFSV4(nmp)) 5065 return; 5066 NFSLOCKCLSTATE(); 5067 clp = nfscl_findcl(nmp); 5068 if (clp == NULL) { 5069 NFSUNLOCKCLSTATE(); 5070 return; 5071 } 5072 dp = nfscl_finddeleg(clp, np->n_fhp->nfh_fh, np->n_fhp->nfh_len); 5073 if (dp != NULL && (dp->nfsdl_flags & NFSCLDL_WRITE)) 5074 np->n_size = dp->nfsdl_size; 5075 NFSUNLOCKCLSTATE(); 5076 } 5077 5078 /* 5079 * If there is a valid write delegation for this file, set the modtime 5080 * to the local clock time. 5081 */ 5082 void 5083 nfscl_delegmodtime(vnode_t vp) 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 && (dp->nfsdl_flags & NFSCLDL_WRITE)) { 5107 nanotime(&dp->nfsdl_modtime); 5108 dp->nfsdl_flags |= NFSCLDL_MODTIMESET; 5109 } 5110 NFSUNLOCKCLSTATE(); 5111 } 5112 5113 /* 5114 * If there is a valid write delegation for this file with a modtime set, 5115 * put that modtime in mtime. 5116 */ 5117 void 5118 nfscl_deleggetmodtime(vnode_t vp, struct timespec *mtime) 5119 { 5120 struct nfsclclient *clp; 5121 struct nfscldeleg *dp; 5122 struct nfsnode *np = VTONFS(vp); 5123 struct nfsmount *nmp; 5124 5125 nmp = VFSTONFS(vp->v_mount); 5126 if (!NFSHASNFSV4(nmp)) 5127 return; 5128 NFSLOCKMNT(nmp); 5129 if ((nmp->nm_privflag & NFSMNTP_DELEGISSUED) == 0) { 5130 NFSUNLOCKMNT(nmp); 5131 return; 5132 } 5133 NFSUNLOCKMNT(nmp); 5134 NFSLOCKCLSTATE(); 5135 clp = nfscl_findcl(nmp); 5136 if (clp == NULL) { 5137 NFSUNLOCKCLSTATE(); 5138 return; 5139 } 5140 dp = nfscl_finddeleg(clp, np->n_fhp->nfh_fh, np->n_fhp->nfh_len); 5141 if (dp != NULL && 5142 (dp->nfsdl_flags & (NFSCLDL_WRITE | NFSCLDL_MODTIMESET)) == 5143 (NFSCLDL_WRITE | NFSCLDL_MODTIMESET)) 5144 *mtime = dp->nfsdl_modtime; 5145 NFSUNLOCKCLSTATE(); 5146 } 5147 5148 static int 5149 nfscl_errmap(struct nfsrv_descript *nd, u_int32_t minorvers) 5150 { 5151 short *defaulterrp, *errp; 5152 5153 if (!nd->nd_repstat) 5154 return (0); 5155 if (nd->nd_procnum == NFSPROC_NOOP) 5156 return (txdr_unsigned(nd->nd_repstat & 0xffff)); 5157 if (nd->nd_repstat == EBADRPC) 5158 return (txdr_unsigned(NFSERR_BADXDR)); 5159 if (nd->nd_repstat == NFSERR_MINORVERMISMATCH || 5160 nd->nd_repstat == NFSERR_OPILLEGAL) 5161 return (txdr_unsigned(nd->nd_repstat)); 5162 if (nd->nd_repstat >= NFSERR_BADIOMODE && nd->nd_repstat < 20000 && 5163 minorvers > NFSV4_MINORVERSION) { 5164 /* NFSv4.n error. */ 5165 return (txdr_unsigned(nd->nd_repstat)); 5166 } 5167 if (nd->nd_procnum < NFSV4OP_CBNOPS) 5168 errp = defaulterrp = nfscl_cberrmap[nd->nd_procnum]; 5169 else 5170 return (txdr_unsigned(nd->nd_repstat)); 5171 while (*++errp) 5172 if (*errp == (short)nd->nd_repstat) 5173 return (txdr_unsigned(nd->nd_repstat)); 5174 return (txdr_unsigned(*defaulterrp)); 5175 } 5176 5177 /* 5178 * Called to find/add a layout to a client. 5179 * This function returns the layout with a refcnt (shared lock) upon 5180 * success (returns 0) or with no lock/refcnt on the layout when an 5181 * error is returned. 5182 * If a layout is passed in via lypp, it is locked (exclusively locked). 5183 */ 5184 int 5185 nfscl_layout(struct nfsmount *nmp, vnode_t vp, u_int8_t *fhp, int fhlen, 5186 nfsv4stateid_t *stateidp, int layouttype, int retonclose, 5187 struct nfsclflayouthead *fhlp, struct nfscllayout **lypp, 5188 struct ucred *cred, NFSPROC_T *p) 5189 { 5190 struct nfsclclient *clp; 5191 struct nfscllayout *lyp, *tlyp; 5192 struct nfsclflayout *flp; 5193 struct nfsnode *np = VTONFS(vp); 5194 mount_t mp; 5195 int layout_passed_in; 5196 5197 mp = nmp->nm_mountp; 5198 layout_passed_in = 1; 5199 tlyp = NULL; 5200 lyp = *lypp; 5201 if (lyp == NULL) { 5202 layout_passed_in = 0; 5203 tlyp = malloc(sizeof(*tlyp) + fhlen - 1, M_NFSLAYOUT, 5204 M_WAITOK | M_ZERO); 5205 } 5206 5207 NFSLOCKCLSTATE(); 5208 clp = nmp->nm_clp; 5209 if (clp == NULL) { 5210 if (layout_passed_in != 0) 5211 nfsv4_unlock(&lyp->nfsly_lock, 0); 5212 NFSUNLOCKCLSTATE(); 5213 if (tlyp != NULL) 5214 free(tlyp, M_NFSLAYOUT); 5215 return (EPERM); 5216 } 5217 if (lyp == NULL) { 5218 /* 5219 * Although no lyp was passed in, another thread might have 5220 * allocated one. If one is found, just increment it's ref 5221 * count and return it. 5222 */ 5223 lyp = nfscl_findlayout(clp, fhp, fhlen); 5224 if (lyp == NULL) { 5225 lyp = tlyp; 5226 tlyp = NULL; 5227 lyp->nfsly_stateid.seqid = stateidp->seqid; 5228 lyp->nfsly_stateid.other[0] = stateidp->other[0]; 5229 lyp->nfsly_stateid.other[1] = stateidp->other[1]; 5230 lyp->nfsly_stateid.other[2] = stateidp->other[2]; 5231 lyp->nfsly_lastbyte = 0; 5232 LIST_INIT(&lyp->nfsly_flayread); 5233 LIST_INIT(&lyp->nfsly_flayrw); 5234 LIST_INIT(&lyp->nfsly_recall); 5235 lyp->nfsly_filesid[0] = np->n_vattr.na_filesid[0]; 5236 lyp->nfsly_filesid[1] = np->n_vattr.na_filesid[1]; 5237 lyp->nfsly_clp = clp; 5238 if (layouttype == NFSLAYOUT_FLEXFILE) 5239 lyp->nfsly_flags = NFSLY_FLEXFILE; 5240 else 5241 lyp->nfsly_flags = NFSLY_FILES; 5242 if (retonclose != 0) 5243 lyp->nfsly_flags |= NFSLY_RETONCLOSE; 5244 lyp->nfsly_fhlen = fhlen; 5245 NFSBCOPY(fhp, lyp->nfsly_fh, fhlen); 5246 TAILQ_INSERT_HEAD(&clp->nfsc_layout, lyp, nfsly_list); 5247 LIST_INSERT_HEAD(NFSCLLAYOUTHASH(clp, fhp, fhlen), lyp, 5248 nfsly_hash); 5249 lyp->nfsly_timestamp = NFSD_MONOSEC + 120; 5250 nfscl_layoutcnt++; 5251 nfsstatsv1.cllayouts++; 5252 } else { 5253 if (retonclose != 0) 5254 lyp->nfsly_flags |= NFSLY_RETONCLOSE; 5255 if (stateidp->seqid > lyp->nfsly_stateid.seqid) 5256 lyp->nfsly_stateid.seqid = stateidp->seqid; 5257 TAILQ_REMOVE(&clp->nfsc_layout, lyp, nfsly_list); 5258 TAILQ_INSERT_HEAD(&clp->nfsc_layout, lyp, nfsly_list); 5259 lyp->nfsly_timestamp = NFSD_MONOSEC + 120; 5260 } 5261 nfsv4_getref(&lyp->nfsly_lock, NULL, NFSCLSTATEMUTEXPTR, mp); 5262 if (NFSCL_FORCEDISM(mp)) { 5263 NFSUNLOCKCLSTATE(); 5264 if (tlyp != NULL) 5265 free(tlyp, M_NFSLAYOUT); 5266 return (EPERM); 5267 } 5268 *lypp = lyp; 5269 } else if (stateidp->seqid > lyp->nfsly_stateid.seqid) 5270 lyp->nfsly_stateid.seqid = stateidp->seqid; 5271 5272 /* Merge the new list of File Layouts into the list. */ 5273 flp = LIST_FIRST(fhlp); 5274 if (flp != NULL) { 5275 if (flp->nfsfl_iomode == NFSLAYOUTIOMODE_READ) 5276 nfscl_mergeflayouts(&lyp->nfsly_flayread, fhlp); 5277 else 5278 nfscl_mergeflayouts(&lyp->nfsly_flayrw, fhlp); 5279 } 5280 if (layout_passed_in != 0) 5281 nfsv4_unlock(&lyp->nfsly_lock, 1); 5282 NFSUNLOCKCLSTATE(); 5283 if (tlyp != NULL) 5284 free(tlyp, M_NFSLAYOUT); 5285 return (0); 5286 } 5287 5288 /* 5289 * Search for a layout by MDS file handle. 5290 * If one is found, it is returned with a refcnt (shared lock) iff 5291 * retflpp returned non-NULL and locked (exclusive locked) iff retflpp is 5292 * returned NULL. 5293 */ 5294 struct nfscllayout * 5295 nfscl_getlayout(struct nfsclclient *clp, uint8_t *fhp, int fhlen, 5296 uint64_t off, uint32_t rwaccess, struct nfsclflayout **retflpp, 5297 int *recalledp) 5298 { 5299 struct nfscllayout *lyp; 5300 mount_t mp; 5301 int error, igotlock; 5302 5303 mp = clp->nfsc_nmp->nm_mountp; 5304 *recalledp = 0; 5305 *retflpp = NULL; 5306 NFSLOCKCLSTATE(); 5307 lyp = nfscl_findlayout(clp, fhp, fhlen); 5308 if (lyp != NULL) { 5309 if ((lyp->nfsly_flags & NFSLY_RECALL) == 0) { 5310 TAILQ_REMOVE(&clp->nfsc_layout, lyp, nfsly_list); 5311 TAILQ_INSERT_HEAD(&clp->nfsc_layout, lyp, nfsly_list); 5312 lyp->nfsly_timestamp = NFSD_MONOSEC + 120; 5313 error = nfscl_findlayoutforio(lyp, off, rwaccess, 5314 retflpp); 5315 if (error == 0) 5316 nfsv4_getref(&lyp->nfsly_lock, NULL, 5317 NFSCLSTATEMUTEXPTR, mp); 5318 else { 5319 do { 5320 igotlock = nfsv4_lock(&lyp->nfsly_lock, 5321 1, NULL, NFSCLSTATEMUTEXPTR, mp); 5322 } while (igotlock == 0 && !NFSCL_FORCEDISM(mp)); 5323 *retflpp = NULL; 5324 } 5325 if (NFSCL_FORCEDISM(mp)) { 5326 lyp = NULL; 5327 *recalledp = 1; 5328 } 5329 } else { 5330 lyp = NULL; 5331 *recalledp = 1; 5332 } 5333 } 5334 NFSUNLOCKCLSTATE(); 5335 return (lyp); 5336 } 5337 5338 /* 5339 * Search for a layout by MDS file handle. If one is found, mark in to be 5340 * recalled, if it already marked "return on close". 5341 */ 5342 static void 5343 nfscl_retoncloselayout(vnode_t vp, struct nfsclclient *clp, uint8_t *fhp, 5344 int fhlen, struct nfsclrecalllayout **recallpp, struct nfscllayout **lypp) 5345 { 5346 struct nfscllayout *lyp; 5347 uint32_t iomode; 5348 5349 *lypp = NULL; 5350 if (vp->v_type != VREG || !NFSHASPNFS(VFSTONFS(vp->v_mount)) || 5351 nfscl_enablecallb == 0 || nfs_numnfscbd == 0 || 5352 (VTONFS(vp)->n_flag & NNOLAYOUT) != 0) 5353 return; 5354 lyp = nfscl_findlayout(clp, fhp, fhlen); 5355 if (lyp != NULL && (lyp->nfsly_flags & NFSLY_RETONCLOSE) != 0) { 5356 if ((lyp->nfsly_flags & NFSLY_RECALL) == 0) { 5357 iomode = 0; 5358 if (!LIST_EMPTY(&lyp->nfsly_flayread)) 5359 iomode |= NFSLAYOUTIOMODE_READ; 5360 if (!LIST_EMPTY(&lyp->nfsly_flayrw)) 5361 iomode |= NFSLAYOUTIOMODE_RW; 5362 nfscl_layoutrecall(NFSLAYOUTRETURN_FILE, lyp, iomode, 5363 0, UINT64_MAX, lyp->nfsly_stateid.seqid, 0, 0, NULL, 5364 *recallpp); 5365 NFSCL_DEBUG(4, "retoncls recall iomode=%d\n", iomode); 5366 *recallpp = NULL; 5367 } 5368 5369 /* Now, wake up renew thread to do LayoutReturn. */ 5370 wakeup(clp); 5371 *lypp = lyp; 5372 } 5373 } 5374 5375 /* 5376 * Mark the layout to be recalled and with an error. 5377 * Also, disable the dsp from further use. 5378 */ 5379 void 5380 nfscl_dserr(uint32_t op, uint32_t stat, struct nfscldevinfo *dp, 5381 struct nfscllayout *lyp, struct nfsclds *dsp) 5382 { 5383 struct nfsclrecalllayout *recallp; 5384 uint32_t iomode; 5385 5386 printf("DS being disabled, error=%d\n", stat); 5387 /* Set up the return of the layout. */ 5388 recallp = malloc(sizeof(*recallp), M_NFSLAYRECALL, M_WAITOK); 5389 iomode = 0; 5390 NFSLOCKCLSTATE(); 5391 if ((lyp->nfsly_flags & NFSLY_RECALL) == 0) { 5392 if (!LIST_EMPTY(&lyp->nfsly_flayread)) 5393 iomode |= NFSLAYOUTIOMODE_READ; 5394 if (!LIST_EMPTY(&lyp->nfsly_flayrw)) 5395 iomode |= NFSLAYOUTIOMODE_RW; 5396 (void)nfscl_layoutrecall(NFSLAYOUTRETURN_FILE, lyp, iomode, 5397 0, UINT64_MAX, lyp->nfsly_stateid.seqid, stat, op, 5398 dp->nfsdi_deviceid, recallp); 5399 NFSUNLOCKCLSTATE(); 5400 NFSCL_DEBUG(4, "nfscl_dserr recall iomode=%d\n", iomode); 5401 } else { 5402 NFSUNLOCKCLSTATE(); 5403 free(recallp, M_NFSLAYRECALL); 5404 } 5405 5406 /* And shut the TCP connection down. */ 5407 nfscl_cancelreqs(dsp); 5408 } 5409 5410 /* 5411 * Cancel all RPCs for this "dsp" by closing the connection. 5412 * Also, mark the session as defunct. 5413 * If NFSCLDS_SAMECONN is set, the connection is shared with other DSs and 5414 * cannot be shut down. 5415 */ 5416 void 5417 nfscl_cancelreqs(struct nfsclds *dsp) 5418 { 5419 struct __rpc_client *cl; 5420 static int non_event; 5421 5422 NFSLOCKDS(dsp); 5423 if ((dsp->nfsclds_flags & (NFSCLDS_CLOSED | NFSCLDS_SAMECONN)) == 0 && 5424 dsp->nfsclds_sockp != NULL && 5425 dsp->nfsclds_sockp->nr_client != NULL) { 5426 dsp->nfsclds_flags |= NFSCLDS_CLOSED; 5427 cl = dsp->nfsclds_sockp->nr_client; 5428 dsp->nfsclds_sess.nfsess_defunct = 1; 5429 NFSUNLOCKDS(dsp); 5430 CLNT_CLOSE(cl); 5431 /* 5432 * This 1sec sleep is done to reduce the number of reconnect 5433 * attempts made on the DS while it has failed. 5434 */ 5435 tsleep(&non_event, PVFS, "ndscls", hz); 5436 return; 5437 } 5438 NFSUNLOCKDS(dsp); 5439 } 5440 5441 /* 5442 * Dereference a layout. 5443 */ 5444 void 5445 nfscl_rellayout(struct nfscllayout *lyp, int exclocked) 5446 { 5447 5448 NFSLOCKCLSTATE(); 5449 if (exclocked != 0) 5450 nfsv4_unlock(&lyp->nfsly_lock, 0); 5451 else 5452 nfsv4_relref(&lyp->nfsly_lock); 5453 NFSUNLOCKCLSTATE(); 5454 } 5455 5456 /* 5457 * Search for a devinfo by deviceid. If one is found, return it after 5458 * acquiring a reference count on it. 5459 */ 5460 struct nfscldevinfo * 5461 nfscl_getdevinfo(struct nfsclclient *clp, uint8_t *deviceid, 5462 struct nfscldevinfo *dip) 5463 { 5464 5465 NFSLOCKCLSTATE(); 5466 if (dip == NULL) 5467 dip = nfscl_finddevinfo(clp, deviceid); 5468 if (dip != NULL) 5469 dip->nfsdi_refcnt++; 5470 NFSUNLOCKCLSTATE(); 5471 return (dip); 5472 } 5473 5474 /* 5475 * Dereference a devinfo structure. 5476 */ 5477 static void 5478 nfscl_reldevinfo_locked(struct nfscldevinfo *dip) 5479 { 5480 5481 dip->nfsdi_refcnt--; 5482 if (dip->nfsdi_refcnt == 0) 5483 wakeup(&dip->nfsdi_refcnt); 5484 } 5485 5486 /* 5487 * Dereference a devinfo structure. 5488 */ 5489 void 5490 nfscl_reldevinfo(struct nfscldevinfo *dip) 5491 { 5492 5493 NFSLOCKCLSTATE(); 5494 nfscl_reldevinfo_locked(dip); 5495 NFSUNLOCKCLSTATE(); 5496 } 5497 5498 /* 5499 * Find a layout for this file handle. Return NULL upon failure. 5500 */ 5501 static struct nfscllayout * 5502 nfscl_findlayout(struct nfsclclient *clp, u_int8_t *fhp, int fhlen) 5503 { 5504 struct nfscllayout *lyp; 5505 5506 LIST_FOREACH(lyp, NFSCLLAYOUTHASH(clp, fhp, fhlen), nfsly_hash) 5507 if (lyp->nfsly_fhlen == fhlen && 5508 !NFSBCMP(lyp->nfsly_fh, fhp, fhlen)) 5509 break; 5510 return (lyp); 5511 } 5512 5513 /* 5514 * Find a devinfo for this deviceid. Return NULL upon failure. 5515 */ 5516 static struct nfscldevinfo * 5517 nfscl_finddevinfo(struct nfsclclient *clp, uint8_t *deviceid) 5518 { 5519 struct nfscldevinfo *dip; 5520 5521 LIST_FOREACH(dip, &clp->nfsc_devinfo, nfsdi_list) 5522 if (NFSBCMP(dip->nfsdi_deviceid, deviceid, NFSX_V4DEVICEID) 5523 == 0) 5524 break; 5525 return (dip); 5526 } 5527 5528 /* 5529 * Merge the new file layout list into the main one, maintaining it in 5530 * increasing offset order. 5531 */ 5532 static void 5533 nfscl_mergeflayouts(struct nfsclflayouthead *fhlp, 5534 struct nfsclflayouthead *newfhlp) 5535 { 5536 struct nfsclflayout *flp, *nflp, *prevflp, *tflp; 5537 5538 flp = LIST_FIRST(fhlp); 5539 prevflp = NULL; 5540 LIST_FOREACH_SAFE(nflp, newfhlp, nfsfl_list, tflp) { 5541 while (flp != NULL && flp->nfsfl_off < nflp->nfsfl_off) { 5542 prevflp = flp; 5543 flp = LIST_NEXT(flp, nfsfl_list); 5544 } 5545 if (prevflp == NULL) 5546 LIST_INSERT_HEAD(fhlp, nflp, nfsfl_list); 5547 else 5548 LIST_INSERT_AFTER(prevflp, nflp, nfsfl_list); 5549 prevflp = nflp; 5550 } 5551 } 5552 5553 /* 5554 * Add this nfscldevinfo to the client, if it doesn't already exist. 5555 * This function consumes the structure pointed at by dip, if not NULL. 5556 */ 5557 int 5558 nfscl_adddevinfo(struct nfsmount *nmp, struct nfscldevinfo *dip, int ind, 5559 struct nfsclflayout *flp) 5560 { 5561 struct nfsclclient *clp; 5562 struct nfscldevinfo *tdip; 5563 uint8_t *dev; 5564 5565 NFSLOCKCLSTATE(); 5566 clp = nmp->nm_clp; 5567 if (clp == NULL) { 5568 NFSUNLOCKCLSTATE(); 5569 if (dip != NULL) 5570 free(dip, M_NFSDEVINFO); 5571 return (ENODEV); 5572 } 5573 if ((flp->nfsfl_flags & NFSFL_FILE) != 0) 5574 dev = flp->nfsfl_dev; 5575 else 5576 dev = flp->nfsfl_ffm[ind].dev; 5577 tdip = nfscl_finddevinfo(clp, dev); 5578 if (tdip != NULL) { 5579 tdip->nfsdi_layoutrefs++; 5580 if ((flp->nfsfl_flags & NFSFL_FILE) != 0) 5581 flp->nfsfl_devp = tdip; 5582 else 5583 flp->nfsfl_ffm[ind].devp = tdip; 5584 nfscl_reldevinfo_locked(tdip); 5585 NFSUNLOCKCLSTATE(); 5586 if (dip != NULL) 5587 free(dip, M_NFSDEVINFO); 5588 return (0); 5589 } 5590 if (dip != NULL) { 5591 LIST_INSERT_HEAD(&clp->nfsc_devinfo, dip, nfsdi_list); 5592 dip->nfsdi_layoutrefs = 1; 5593 if ((flp->nfsfl_flags & NFSFL_FILE) != 0) 5594 flp->nfsfl_devp = dip; 5595 else 5596 flp->nfsfl_ffm[ind].devp = dip; 5597 } 5598 NFSUNLOCKCLSTATE(); 5599 if (dip == NULL) 5600 return (ENODEV); 5601 return (0); 5602 } 5603 5604 /* 5605 * Free up a layout structure and associated file layout structure(s). 5606 */ 5607 void 5608 nfscl_freelayout(struct nfscllayout *layp) 5609 { 5610 struct nfsclflayout *flp, *nflp; 5611 struct nfsclrecalllayout *rp, *nrp; 5612 5613 LIST_FOREACH_SAFE(flp, &layp->nfsly_flayread, nfsfl_list, nflp) { 5614 LIST_REMOVE(flp, nfsfl_list); 5615 nfscl_freeflayout(flp); 5616 } 5617 LIST_FOREACH_SAFE(flp, &layp->nfsly_flayrw, nfsfl_list, nflp) { 5618 LIST_REMOVE(flp, nfsfl_list); 5619 nfscl_freeflayout(flp); 5620 } 5621 LIST_FOREACH_SAFE(rp, &layp->nfsly_recall, nfsrecly_list, nrp) { 5622 LIST_REMOVE(rp, nfsrecly_list); 5623 free(rp, M_NFSLAYRECALL); 5624 } 5625 nfscl_layoutcnt--; 5626 nfsstatsv1.cllayouts--; 5627 free(layp, M_NFSLAYOUT); 5628 } 5629 5630 /* 5631 * Free up a file layout structure. 5632 */ 5633 void 5634 nfscl_freeflayout(struct nfsclflayout *flp) 5635 { 5636 int i, j; 5637 5638 if ((flp->nfsfl_flags & NFSFL_FILE) != 0) { 5639 for (i = 0; i < flp->nfsfl_fhcnt; i++) 5640 free(flp->nfsfl_fh[i], M_NFSFH); 5641 if (flp->nfsfl_devp != NULL) 5642 flp->nfsfl_devp->nfsdi_layoutrefs--; 5643 } 5644 if ((flp->nfsfl_flags & NFSFL_FLEXFILE) != 0) 5645 for (i = 0; i < flp->nfsfl_mirrorcnt; i++) { 5646 for (j = 0; j < flp->nfsfl_ffm[i].fhcnt; j++) 5647 free(flp->nfsfl_ffm[i].fh[j], M_NFSFH); 5648 if (flp->nfsfl_ffm[i].devp != NULL) 5649 flp->nfsfl_ffm[i].devp->nfsdi_layoutrefs--; 5650 } 5651 free(flp, M_NFSFLAYOUT); 5652 } 5653 5654 /* 5655 * Free up a file layout devinfo structure. 5656 */ 5657 void 5658 nfscl_freedevinfo(struct nfscldevinfo *dip) 5659 { 5660 5661 free(dip, M_NFSDEVINFO); 5662 } 5663 5664 /* 5665 * Mark any layouts that match as recalled. 5666 */ 5667 static int 5668 nfscl_layoutrecall(int recalltype, struct nfscllayout *lyp, uint32_t iomode, 5669 uint64_t off, uint64_t len, uint32_t stateseqid, uint32_t stat, uint32_t op, 5670 char *devid, struct nfsclrecalllayout *recallp) 5671 { 5672 struct nfsclrecalllayout *rp, *orp; 5673 5674 recallp->nfsrecly_recalltype = recalltype; 5675 recallp->nfsrecly_iomode = iomode; 5676 recallp->nfsrecly_stateseqid = stateseqid; 5677 recallp->nfsrecly_off = off; 5678 recallp->nfsrecly_len = len; 5679 recallp->nfsrecly_stat = stat; 5680 recallp->nfsrecly_op = op; 5681 if (devid != NULL) 5682 NFSBCOPY(devid, recallp->nfsrecly_devid, NFSX_V4DEVICEID); 5683 /* 5684 * Order the list as file returns first, followed by fsid and any 5685 * returns, both in increasing stateseqid order. 5686 * Note that the seqids wrap around, so 1 is after 0xffffffff. 5687 * (I'm not sure this is correct because I find RFC5661 confusing 5688 * on this, but hopefully it will work ok.) 5689 */ 5690 orp = NULL; 5691 LIST_FOREACH(rp, &lyp->nfsly_recall, nfsrecly_list) { 5692 orp = rp; 5693 if ((recalltype == NFSLAYOUTRETURN_FILE && 5694 (rp->nfsrecly_recalltype != NFSLAYOUTRETURN_FILE || 5695 nfscl_seq(stateseqid, rp->nfsrecly_stateseqid) != 0)) || 5696 (recalltype != NFSLAYOUTRETURN_FILE && 5697 rp->nfsrecly_recalltype != NFSLAYOUTRETURN_FILE && 5698 nfscl_seq(stateseqid, rp->nfsrecly_stateseqid) != 0)) { 5699 LIST_INSERT_BEFORE(rp, recallp, nfsrecly_list); 5700 break; 5701 } 5702 5703 /* 5704 * Put any error return on all the file returns that will 5705 * preceed this one. 5706 */ 5707 if (rp->nfsrecly_recalltype == NFSLAYOUTRETURN_FILE && 5708 stat != 0 && rp->nfsrecly_stat == 0) { 5709 rp->nfsrecly_stat = stat; 5710 rp->nfsrecly_op = op; 5711 if (devid != NULL) 5712 NFSBCOPY(devid, rp->nfsrecly_devid, 5713 NFSX_V4DEVICEID); 5714 } 5715 } 5716 if (rp == NULL) { 5717 if (orp == NULL) 5718 LIST_INSERT_HEAD(&lyp->nfsly_recall, recallp, 5719 nfsrecly_list); 5720 else 5721 LIST_INSERT_AFTER(orp, recallp, nfsrecly_list); 5722 } 5723 lyp->nfsly_flags |= NFSLY_RECALL; 5724 wakeup(lyp->nfsly_clp); 5725 return (0); 5726 } 5727 5728 /* 5729 * Compare the two seqids for ordering. The trick is that the seqids can 5730 * wrap around from 0xffffffff->0, so check for the cases where one 5731 * has wrapped around. 5732 * Return 1 if seqid1 comes before seqid2, 0 otherwise. 5733 */ 5734 static int 5735 nfscl_seq(uint32_t seqid1, uint32_t seqid2) 5736 { 5737 5738 if (seqid2 > seqid1 && (seqid2 - seqid1) >= 0x7fffffff) 5739 /* seqid2 has wrapped around. */ 5740 return (0); 5741 if (seqid1 > seqid2 && (seqid1 - seqid2) >= 0x7fffffff) 5742 /* seqid1 has wrapped around. */ 5743 return (1); 5744 if (seqid1 <= seqid2) 5745 return (1); 5746 return (0); 5747 } 5748 5749 /* 5750 * Do a layout return for each of the recalls. 5751 */ 5752 static void 5753 nfscl_layoutreturn(struct nfsmount *nmp, struct nfscllayout *lyp, 5754 struct ucred *cred, NFSPROC_T *p) 5755 { 5756 struct nfsclrecalllayout *rp; 5757 nfsv4stateid_t stateid; 5758 int layouttype; 5759 5760 NFSBCOPY(lyp->nfsly_stateid.other, stateid.other, NFSX_STATEIDOTHER); 5761 stateid.seqid = lyp->nfsly_stateid.seqid; 5762 if ((lyp->nfsly_flags & NFSLY_FILES) != 0) 5763 layouttype = NFSLAYOUT_NFSV4_1_FILES; 5764 else 5765 layouttype = NFSLAYOUT_FLEXFILE; 5766 LIST_FOREACH(rp, &lyp->nfsly_recall, nfsrecly_list) { 5767 (void)nfsrpc_layoutreturn(nmp, lyp->nfsly_fh, 5768 lyp->nfsly_fhlen, 0, layouttype, 5769 rp->nfsrecly_iomode, rp->nfsrecly_recalltype, 5770 rp->nfsrecly_off, rp->nfsrecly_len, 5771 &stateid, cred, p, rp->nfsrecly_stat, rp->nfsrecly_op, 5772 rp->nfsrecly_devid); 5773 } 5774 } 5775 5776 /* 5777 * Do the layout commit for a file layout. 5778 */ 5779 static void 5780 nfscl_dolayoutcommit(struct nfsmount *nmp, struct nfscllayout *lyp, 5781 struct ucred *cred, NFSPROC_T *p) 5782 { 5783 struct nfsclflayout *flp; 5784 uint64_t len; 5785 int error, layouttype; 5786 5787 if ((lyp->nfsly_flags & NFSLY_FILES) != 0) 5788 layouttype = NFSLAYOUT_NFSV4_1_FILES; 5789 else 5790 layouttype = NFSLAYOUT_FLEXFILE; 5791 LIST_FOREACH(flp, &lyp->nfsly_flayrw, nfsfl_list) { 5792 if (layouttype == NFSLAYOUT_FLEXFILE && 5793 (flp->nfsfl_fflags & NFSFLEXFLAG_NO_LAYOUTCOMMIT) != 0) { 5794 NFSCL_DEBUG(4, "Flex file: no layoutcommit\n"); 5795 /* If not supported, don't bother doing it. */ 5796 NFSLOCKMNT(nmp); 5797 nmp->nm_state |= NFSSTA_NOLAYOUTCOMMIT; 5798 NFSUNLOCKMNT(nmp); 5799 break; 5800 } else if (flp->nfsfl_off <= lyp->nfsly_lastbyte) { 5801 len = flp->nfsfl_end - flp->nfsfl_off; 5802 error = nfsrpc_layoutcommit(nmp, lyp->nfsly_fh, 5803 lyp->nfsly_fhlen, 0, flp->nfsfl_off, len, 5804 lyp->nfsly_lastbyte, &lyp->nfsly_stateid, 5805 layouttype, cred, p, NULL); 5806 NFSCL_DEBUG(4, "layoutcommit err=%d\n", error); 5807 if (error == NFSERR_NOTSUPP) { 5808 /* If not supported, don't bother doing it. */ 5809 NFSLOCKMNT(nmp); 5810 nmp->nm_state |= NFSSTA_NOLAYOUTCOMMIT; 5811 NFSUNLOCKMNT(nmp); 5812 break; 5813 } 5814 } 5815 } 5816 } 5817 5818 /* 5819 * Commit all layouts for a file (vnode). 5820 */ 5821 int 5822 nfscl_layoutcommit(vnode_t vp, NFSPROC_T *p) 5823 { 5824 struct nfsclclient *clp; 5825 struct nfscllayout *lyp; 5826 struct nfsnode *np = VTONFS(vp); 5827 mount_t mp; 5828 struct nfsmount *nmp; 5829 5830 mp = vp->v_mount; 5831 nmp = VFSTONFS(mp); 5832 if (NFSHASNOLAYOUTCOMMIT(nmp)) 5833 return (0); 5834 NFSLOCKCLSTATE(); 5835 clp = nmp->nm_clp; 5836 if (clp == NULL) { 5837 NFSUNLOCKCLSTATE(); 5838 return (EPERM); 5839 } 5840 lyp = nfscl_findlayout(clp, np->n_fhp->nfh_fh, np->n_fhp->nfh_len); 5841 if (lyp == NULL) { 5842 NFSUNLOCKCLSTATE(); 5843 return (EPERM); 5844 } 5845 nfsv4_getref(&lyp->nfsly_lock, NULL, NFSCLSTATEMUTEXPTR, mp); 5846 if (NFSCL_FORCEDISM(mp)) { 5847 NFSUNLOCKCLSTATE(); 5848 return (EPERM); 5849 } 5850 tryagain: 5851 if ((lyp->nfsly_flags & NFSLY_WRITTEN) != 0) { 5852 lyp->nfsly_flags &= ~NFSLY_WRITTEN; 5853 NFSUNLOCKCLSTATE(); 5854 NFSCL_DEBUG(4, "do layoutcommit2\n"); 5855 nfscl_dolayoutcommit(clp->nfsc_nmp, lyp, NFSPROCCRED(p), p); 5856 NFSLOCKCLSTATE(); 5857 goto tryagain; 5858 } 5859 nfsv4_relref(&lyp->nfsly_lock); 5860 NFSUNLOCKCLSTATE(); 5861 return (0); 5862 } 5863