1 /*- 2 * Copyright (c) 2009 Rick Macklem, University of Guelph 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24 * SUCH DAMAGE. 25 * 26 */ 27 28 #include <sys/cdefs.h> 29 __FBSDID("$FreeBSD$"); 30 31 #ifndef APPLEKEXT 32 #include <fs/nfs/nfsport.h> 33 34 struct nfsrv_stablefirst nfsrv_stablefirst; 35 int nfsrv_issuedelegs = 0; 36 int nfsrv_dolocallocks = 0; 37 struct nfsv4lock nfsv4rootfs_lock; 38 39 extern int newnfs_numnfsd; 40 extern struct nfsstats newnfsstats; 41 extern int nfsrv_lease; 42 extern struct timeval nfsboottime; 43 extern u_int32_t newnfs_true, newnfs_false; 44 NFSV4ROOTLOCKMUTEX; 45 NFSSTATESPINLOCK; 46 47 /* 48 * Hash and lru lists for nfs V4. 49 * (Some would put them in the .h file, but I don't like declaring storage 50 * in a .h) 51 */ 52 struct nfsclienthashhead nfsclienthash[NFSCLIENTHASHSIZE]; 53 struct nfslockhashhead nfslockhash[NFSLOCKHASHSIZE]; 54 #endif /* !APPLEKEXT */ 55 56 static u_int32_t nfsrv_openpluslock = 0, nfsrv_delegatecnt = 0; 57 static time_t nfsrvboottime; 58 static int nfsrv_writedelegifpos = 1; 59 static int nfsrv_returnoldstateid = 0, nfsrv_clients = 0; 60 static int nfsrv_clienthighwater = NFSRV_CLIENTHIGHWATER; 61 static int nfsrv_nogsscallback = 0; 62 63 /* local functions */ 64 static void nfsrv_dumpaclient(struct nfsclient *clp, 65 struct nfsd_dumpclients *dumpp); 66 static void nfsrv_freeopenowner(struct nfsstate *stp, int cansleep, 67 NFSPROC_T *p); 68 static int nfsrv_freeopen(struct nfsstate *stp, vnode_t vp, int cansleep, 69 NFSPROC_T *p); 70 static void nfsrv_freelockowner(struct nfsstate *stp, vnode_t vp, int cansleep, 71 NFSPROC_T *p); 72 static void nfsrv_freeallnfslocks(struct nfsstate *stp, vnode_t vp, 73 int cansleep, NFSPROC_T *p); 74 static void nfsrv_freenfslock(struct nfslock *lop); 75 static void nfsrv_freenfslockfile(struct nfslockfile *lfp); 76 static void nfsrv_freedeleg(struct nfsstate *); 77 static int nfsrv_getstate(struct nfsclient *clp, nfsv4stateid_t *stateidp, 78 u_int32_t flags, struct nfsstate **stpp); 79 static void nfsrv_getowner(struct nfsstatehead *hp, struct nfsstate *new_stp, 80 struct nfsstate **stpp); 81 static int nfsrv_getlockfh(vnode_t vp, u_short flags, 82 struct nfslockfile **new_lfpp, fhandle_t *nfhp, NFSPROC_T *p); 83 static int nfsrv_getlockfile(u_short flags, struct nfslockfile **new_lfpp, 84 struct nfslockfile **lfpp, fhandle_t *nfhp, int lockit); 85 static void nfsrv_insertlock(struct nfslock *new_lop, 86 struct nfslock *insert_lop, struct nfsstate *stp, struct nfslockfile *lfp); 87 static void nfsrv_updatelock(struct nfsstate *stp, struct nfslock **new_lopp, 88 struct nfslock **other_lopp, struct nfslockfile *lfp); 89 static int nfsrv_getipnumber(u_char *cp); 90 static int nfsrv_checkrestart(nfsquad_t clientid, u_int32_t flags, 91 nfsv4stateid_t *stateidp, int specialid); 92 static int nfsrv_checkgrace(u_int32_t flags); 93 static int nfsrv_docallback(struct nfsclient *clp, int procnum, 94 nfsv4stateid_t *stateidp, int trunc, fhandle_t *fhp, 95 struct nfsvattr *nap, nfsattrbit_t *attrbitp, NFSPROC_T *p); 96 static u_int32_t nfsrv_nextclientindex(void); 97 static u_int32_t nfsrv_nextstateindex(struct nfsclient *clp); 98 static void nfsrv_markstable(struct nfsclient *clp); 99 static int nfsrv_checkstable(struct nfsclient *clp); 100 static int nfsrv_clientconflict(struct nfsclient *clp, int *haslockp, struct 101 vnode *vp, NFSPROC_T *p); 102 static int nfsrv_delegconflict(struct nfsstate *stp, int *haslockp, 103 NFSPROC_T *p, vnode_t vp); 104 static int nfsrv_cleandeleg(vnode_t vp, struct nfslockfile *lfp, 105 struct nfsclient *clp, int *haslockp, NFSPROC_T *p); 106 static int nfsrv_notsamecredname(struct nfsrv_descript *nd, 107 struct nfsclient *clp); 108 static time_t nfsrv_leaseexpiry(void); 109 static void nfsrv_delaydelegtimeout(struct nfsstate *stp); 110 static int nfsrv_checkseqid(struct nfsrv_descript *nd, u_int32_t seqid, 111 struct nfsstate *stp, struct nfsrvcache *op); 112 static int nfsrv_nootherstate(struct nfsstate *stp); 113 static int nfsrv_locallock(vnode_t vp, struct nfslockfile *lfp, int flags, 114 uint64_t first, uint64_t end, struct nfslockconflict *cfp, NFSPROC_T *p); 115 static void nfsrv_localunlock(vnode_t vp, struct nfslockfile *lfp, 116 uint64_t init_first, uint64_t init_end, NFSPROC_T *p); 117 static int nfsrv_dolocal(vnode_t vp, struct nfslockfile *lfp, int flags, 118 int oldflags, uint64_t first, uint64_t end, struct nfslockconflict *cfp, 119 NFSPROC_T *p); 120 static void nfsrv_locallock_rollback(vnode_t vp, struct nfslockfile *lfp, 121 NFSPROC_T *p); 122 static void nfsrv_locallock_commit(struct nfslockfile *lfp, int flags, 123 uint64_t first, uint64_t end); 124 static void nfsrv_locklf(struct nfslockfile *lfp); 125 static void nfsrv_unlocklf(struct nfslockfile *lfp); 126 127 /* 128 * Scan the client list for a match and either return the current one, 129 * create a new entry or return an error. 130 * If returning a non-error, the clp structure must either be linked into 131 * the client list or free'd. 132 */ 133 APPLESTATIC int 134 nfsrv_setclient(struct nfsrv_descript *nd, struct nfsclient **new_clpp, 135 nfsquad_t *clientidp, nfsquad_t *confirmp, NFSPROC_T *p) 136 { 137 struct nfsclient *clp = NULL, *new_clp = *new_clpp; 138 int i; 139 struct nfsstate *stp, *tstp; 140 struct sockaddr_in *sad, *rad; 141 int zapit = 0, gotit, hasstate = 0, igotlock; 142 static u_int64_t confirm_index = 0; 143 144 /* 145 * Check for state resource limit exceeded. 146 */ 147 if (nfsrv_openpluslock > NFSRV_V4STATELIMIT) 148 return (NFSERR_RESOURCE); 149 150 if ((nd->nd_flag & ND_GSS) && nfsrv_nogsscallback) 151 /* 152 * Don't do callbacks for AUTH_GSS. 153 * (Since these aren't yet debugged, they might cause the 154 * server to crap out, if they get past the Init call to 155 * the client.) 156 */ 157 new_clp->lc_program = 0; 158 159 /* Lock out other nfsd threads */ 160 NFSLOCKV4ROOTMUTEX(); 161 nfsv4_relref(&nfsv4rootfs_lock); 162 do { 163 igotlock = nfsv4_lock(&nfsv4rootfs_lock, 1, NULL, 164 NFSV4ROOTLOCKMUTEXPTR); 165 } while (!igotlock); 166 NFSUNLOCKV4ROOTMUTEX(); 167 NFSLOCKSTATE(); /* to avoid a race with */ 168 NFSUNLOCKSTATE(); /* nfsrv_servertimer() */ 169 170 /* 171 * Search for a match in the client list. 172 */ 173 gotit = i = 0; 174 while (i < NFSCLIENTHASHSIZE && !gotit) { 175 LIST_FOREACH(clp, &nfsclienthash[i], lc_hash) { 176 if (new_clp->lc_idlen == clp->lc_idlen && 177 !NFSBCMP(new_clp->lc_id, clp->lc_id, clp->lc_idlen)) { 178 gotit = 1; 179 break; 180 } 181 } 182 i++; 183 } 184 if (!gotit || 185 (clp->lc_flags & (LCL_NEEDSCONFIRM | LCL_ADMINREVOKED))) { 186 /* 187 * Get rid of the old one. 188 */ 189 if (i != NFSCLIENTHASHSIZE) { 190 LIST_REMOVE(clp, lc_hash); 191 nfsrv_cleanclient(clp, p); 192 nfsrv_freedeleglist(&clp->lc_deleg); 193 nfsrv_freedeleglist(&clp->lc_olddeleg); 194 zapit = 1; 195 } 196 /* 197 * Add it after assigning a client id to it. 198 */ 199 new_clp->lc_flags |= LCL_NEEDSCONFIRM; 200 confirmp->qval = new_clp->lc_confirm.qval = ++confirm_index; 201 clientidp->lval[0] = new_clp->lc_clientid.lval[0] = 202 (u_int32_t)nfsrvboottime; 203 clientidp->lval[1] = new_clp->lc_clientid.lval[1] = 204 nfsrv_nextclientindex(); 205 new_clp->lc_stateindex = 0; 206 new_clp->lc_statemaxindex = 0; 207 new_clp->lc_cbref = 0; 208 new_clp->lc_expiry = nfsrv_leaseexpiry(); 209 LIST_INIT(&new_clp->lc_open); 210 LIST_INIT(&new_clp->lc_deleg); 211 LIST_INIT(&new_clp->lc_olddeleg); 212 for (i = 0; i < NFSSTATEHASHSIZE; i++) 213 LIST_INIT(&new_clp->lc_stateid[i]); 214 LIST_INSERT_HEAD(NFSCLIENTHASH(new_clp->lc_clientid), new_clp, 215 lc_hash); 216 newnfsstats.srvclients++; 217 nfsrv_openpluslock++; 218 nfsrv_clients++; 219 NFSLOCKV4ROOTMUTEX(); 220 nfsv4_unlock(&nfsv4rootfs_lock, 1); 221 NFSUNLOCKV4ROOTMUTEX(); 222 if (zapit) 223 nfsrv_zapclient(clp, p); 224 *new_clpp = NULL; 225 return (0); 226 } 227 228 /* 229 * Now, handle the cases where the id is already issued. 230 */ 231 if (nfsrv_notsamecredname(nd, clp)) { 232 /* 233 * Check to see if there is expired state that should go away. 234 */ 235 if (clp->lc_expiry < NFSD_MONOSEC && 236 (!LIST_EMPTY(&clp->lc_open) || !LIST_EMPTY(&clp->lc_deleg))) { 237 nfsrv_cleanclient(clp, p); 238 nfsrv_freedeleglist(&clp->lc_deleg); 239 } 240 241 /* 242 * If there is outstanding state, then reply NFSERR_CLIDINUSE per 243 * RFC3530 Sec. 8.1.2 last para. 244 */ 245 if (!LIST_EMPTY(&clp->lc_deleg)) { 246 hasstate = 1; 247 } else if (LIST_EMPTY(&clp->lc_open)) { 248 hasstate = 0; 249 } else { 250 hasstate = 0; 251 /* Look for an Open on the OpenOwner */ 252 LIST_FOREACH(stp, &clp->lc_open, ls_list) { 253 if (!LIST_EMPTY(&stp->ls_open)) { 254 hasstate = 1; 255 break; 256 } 257 } 258 } 259 if (hasstate) { 260 /* 261 * If the uid doesn't match, return NFSERR_CLIDINUSE after 262 * filling out the correct ipaddr and portnum. 263 */ 264 sad = NFSSOCKADDR(new_clp->lc_req.nr_nam, struct sockaddr_in *); 265 rad = NFSSOCKADDR(clp->lc_req.nr_nam, struct sockaddr_in *); 266 sad->sin_addr.s_addr = rad->sin_addr.s_addr; 267 sad->sin_port = rad->sin_port; 268 NFSLOCKV4ROOTMUTEX(); 269 nfsv4_unlock(&nfsv4rootfs_lock, 1); 270 NFSUNLOCKV4ROOTMUTEX(); 271 return (NFSERR_CLIDINUSE); 272 } 273 } 274 275 if (NFSBCMP(new_clp->lc_verf, clp->lc_verf, NFSX_VERF)) { 276 /* 277 * If the verifier has changed, the client has rebooted 278 * and a new client id is issued. The old state info 279 * can be thrown away once the SETCLIENTID_CONFIRM occurs. 280 */ 281 LIST_REMOVE(clp, lc_hash); 282 new_clp->lc_flags |= LCL_NEEDSCONFIRM; 283 confirmp->qval = new_clp->lc_confirm.qval = ++confirm_index; 284 clientidp->lval[0] = new_clp->lc_clientid.lval[0] = 285 nfsrvboottime; 286 clientidp->lval[1] = new_clp->lc_clientid.lval[1] = 287 nfsrv_nextclientindex(); 288 new_clp->lc_stateindex = 0; 289 new_clp->lc_statemaxindex = 0; 290 new_clp->lc_cbref = 0; 291 new_clp->lc_expiry = nfsrv_leaseexpiry(); 292 293 /* 294 * Save the state until confirmed. 295 */ 296 LIST_NEWHEAD(&new_clp->lc_open, &clp->lc_open, ls_list); 297 LIST_FOREACH(tstp, &new_clp->lc_open, ls_list) 298 tstp->ls_clp = new_clp; 299 LIST_NEWHEAD(&new_clp->lc_deleg, &clp->lc_deleg, ls_list); 300 LIST_FOREACH(tstp, &new_clp->lc_deleg, ls_list) 301 tstp->ls_clp = new_clp; 302 LIST_NEWHEAD(&new_clp->lc_olddeleg, &clp->lc_olddeleg, 303 ls_list); 304 LIST_FOREACH(tstp, &new_clp->lc_olddeleg, ls_list) 305 tstp->ls_clp = new_clp; 306 for (i = 0; i < NFSSTATEHASHSIZE; i++) { 307 LIST_NEWHEAD(&new_clp->lc_stateid[i], 308 &clp->lc_stateid[i], ls_hash); 309 LIST_FOREACH(tstp, &new_clp->lc_stateid[i], ls_list) 310 tstp->ls_clp = new_clp; 311 } 312 LIST_INSERT_HEAD(NFSCLIENTHASH(new_clp->lc_clientid), new_clp, 313 lc_hash); 314 newnfsstats.srvclients++; 315 nfsrv_openpluslock++; 316 nfsrv_clients++; 317 NFSLOCKV4ROOTMUTEX(); 318 nfsv4_unlock(&nfsv4rootfs_lock, 1); 319 NFSUNLOCKV4ROOTMUTEX(); 320 321 /* 322 * Must wait until any outstanding callback on the old clp 323 * completes. 324 */ 325 while (clp->lc_cbref) { 326 clp->lc_flags |= LCL_WAKEUPWANTED; 327 (void) tsleep((caddr_t)clp, PZERO - 1, 328 "nfsd clp", 10 * hz); 329 } 330 nfsrv_zapclient(clp, p); 331 *new_clpp = NULL; 332 return (0); 333 } 334 /* 335 * id and verifier match, so update the net address info 336 * and get rid of any existing callback authentication 337 * handle, so a new one will be acquired. 338 */ 339 LIST_REMOVE(clp, lc_hash); 340 new_clp->lc_flags |= (LCL_NEEDSCONFIRM | LCL_DONTCLEAN); 341 new_clp->lc_expiry = nfsrv_leaseexpiry(); 342 confirmp->qval = new_clp->lc_confirm.qval = ++confirm_index; 343 clientidp->lval[0] = new_clp->lc_clientid.lval[0] = 344 clp->lc_clientid.lval[0]; 345 clientidp->lval[1] = new_clp->lc_clientid.lval[1] = 346 clp->lc_clientid.lval[1]; 347 new_clp->lc_delegtime = clp->lc_delegtime; 348 new_clp->lc_stateindex = clp->lc_stateindex; 349 new_clp->lc_statemaxindex = clp->lc_statemaxindex; 350 new_clp->lc_cbref = 0; 351 LIST_NEWHEAD(&new_clp->lc_open, &clp->lc_open, ls_list); 352 LIST_FOREACH(tstp, &new_clp->lc_open, ls_list) 353 tstp->ls_clp = new_clp; 354 LIST_NEWHEAD(&new_clp->lc_deleg, &clp->lc_deleg, ls_list); 355 LIST_FOREACH(tstp, &new_clp->lc_deleg, ls_list) 356 tstp->ls_clp = new_clp; 357 LIST_NEWHEAD(&new_clp->lc_olddeleg, &clp->lc_olddeleg, ls_list); 358 LIST_FOREACH(tstp, &new_clp->lc_olddeleg, ls_list) 359 tstp->ls_clp = new_clp; 360 for (i = 0; i < NFSSTATEHASHSIZE; i++) { 361 LIST_NEWHEAD(&new_clp->lc_stateid[i], &clp->lc_stateid[i], 362 ls_hash); 363 LIST_FOREACH(tstp, &new_clp->lc_stateid[i], ls_list) 364 tstp->ls_clp = new_clp; 365 } 366 LIST_INSERT_HEAD(NFSCLIENTHASH(new_clp->lc_clientid), new_clp, 367 lc_hash); 368 newnfsstats.srvclients++; 369 nfsrv_openpluslock++; 370 nfsrv_clients++; 371 NFSLOCKV4ROOTMUTEX(); 372 nfsv4_unlock(&nfsv4rootfs_lock, 1); 373 NFSUNLOCKV4ROOTMUTEX(); 374 375 /* 376 * Must wait until any outstanding callback on the old clp 377 * completes. 378 */ 379 while (clp->lc_cbref) { 380 clp->lc_flags |= LCL_WAKEUPWANTED; 381 (void) tsleep((caddr_t)clp, PZERO - 1, "nfsd clp", 10 * hz); 382 } 383 nfsrv_zapclient(clp, p); 384 *new_clpp = NULL; 385 return (0); 386 } 387 388 /* 389 * Check to see if the client id exists and optionally confirm it. 390 */ 391 APPLESTATIC int 392 nfsrv_getclient(nfsquad_t clientid, int opflags, struct nfsclient **clpp, 393 nfsquad_t confirm, struct nfsrv_descript *nd, NFSPROC_T *p) 394 { 395 struct nfsclient *clp; 396 struct nfsstate *stp; 397 int i; 398 struct nfsclienthashhead *hp; 399 int error = 0, igotlock, doneok; 400 401 if (clpp) 402 *clpp = NULL; 403 if (nfsrvboottime != clientid.lval[0]) 404 return (NFSERR_STALECLIENTID); 405 406 /* 407 * If called with opflags == CLOPS_RENEW, the State Lock is 408 * already held. Otherwise, we need to get either that or, 409 * for the case of Confirm, lock out the nfsd threads. 410 */ 411 if (opflags & CLOPS_CONFIRM) { 412 NFSLOCKV4ROOTMUTEX(); 413 nfsv4_relref(&nfsv4rootfs_lock); 414 do { 415 igotlock = nfsv4_lock(&nfsv4rootfs_lock, 1, NULL, 416 NFSV4ROOTLOCKMUTEXPTR); 417 } while (!igotlock); 418 NFSUNLOCKV4ROOTMUTEX(); 419 NFSLOCKSTATE(); /* to avoid a race with */ 420 NFSUNLOCKSTATE(); /* nfsrv_servertimer() */ 421 } else if (opflags != CLOPS_RENEW) { 422 NFSLOCKSTATE(); 423 } 424 425 hp = NFSCLIENTHASH(clientid); 426 LIST_FOREACH(clp, hp, lc_hash) { 427 if (clp->lc_clientid.lval[1] == clientid.lval[1]) 428 break; 429 } 430 if (clp == LIST_END(hp)) { 431 if (opflags & CLOPS_CONFIRM) 432 error = NFSERR_STALECLIENTID; 433 else 434 error = NFSERR_EXPIRED; 435 } else if (clp->lc_flags & LCL_ADMINREVOKED) { 436 /* 437 * If marked admin revoked, just return the error. 438 */ 439 error = NFSERR_ADMINREVOKED; 440 } 441 if (error) { 442 if (opflags & CLOPS_CONFIRM) { 443 NFSLOCKV4ROOTMUTEX(); 444 nfsv4_unlock(&nfsv4rootfs_lock, 1); 445 NFSUNLOCKV4ROOTMUTEX(); 446 } else if (opflags != CLOPS_RENEW) { 447 NFSUNLOCKSTATE(); 448 } 449 return (error); 450 } 451 452 /* 453 * Perform any operations specified by the opflags. 454 */ 455 if (opflags & CLOPS_CONFIRM) { 456 if (clp->lc_confirm.qval != confirm.qval) 457 error = NFSERR_STALECLIENTID; 458 else if (nfsrv_notsamecredname(nd, clp)) 459 error = NFSERR_CLIDINUSE; 460 461 if (!error) { 462 if ((clp->lc_flags & (LCL_NEEDSCONFIRM | LCL_DONTCLEAN)) == 463 LCL_NEEDSCONFIRM) { 464 /* 465 * Hang onto the delegations (as old delegations) 466 * for an Open with CLAIM_DELEGATE_PREV unless in 467 * grace, but get rid of the rest of the state. 468 */ 469 nfsrv_cleanclient(clp, p); 470 nfsrv_freedeleglist(&clp->lc_olddeleg); 471 if (nfsrv_checkgrace(0)) { 472 /* In grace, so just delete delegations */ 473 nfsrv_freedeleglist(&clp->lc_deleg); 474 } else { 475 LIST_FOREACH(stp, &clp->lc_deleg, ls_list) 476 stp->ls_flags |= NFSLCK_OLDDELEG; 477 clp->lc_delegtime = NFSD_MONOSEC + 478 nfsrv_lease + NFSRV_LEASEDELTA; 479 LIST_NEWHEAD(&clp->lc_olddeleg, &clp->lc_deleg, 480 ls_list); 481 } 482 } 483 clp->lc_flags &= ~(LCL_NEEDSCONFIRM | LCL_DONTCLEAN); 484 if (clp->lc_program) 485 clp->lc_flags |= LCL_NEEDSCBNULL; 486 } 487 } else if (clp->lc_flags & LCL_NEEDSCONFIRM) { 488 error = NFSERR_EXPIRED; 489 } 490 491 /* 492 * If called by the Renew Op, we must check the principal. 493 */ 494 if (!error && (opflags & CLOPS_RENEWOP)) { 495 if (nfsrv_notsamecredname(nd, clp)) { 496 doneok = 0; 497 for (i = 0; i < NFSSTATEHASHSIZE && doneok == 0; i++) { 498 LIST_FOREACH(stp, &clp->lc_stateid[i], ls_hash) { 499 if ((stp->ls_flags & NFSLCK_OPEN) && 500 stp->ls_uid == nd->nd_cred->cr_uid) { 501 doneok = 1; 502 break; 503 } 504 } 505 } 506 if (!doneok) 507 error = NFSERR_ACCES; 508 } 509 if (!error && (clp->lc_flags & LCL_CBDOWN)) 510 error = NFSERR_CBPATHDOWN; 511 } 512 if ((!error || error == NFSERR_CBPATHDOWN) && 513 (opflags & CLOPS_RENEW)) { 514 clp->lc_expiry = nfsrv_leaseexpiry(); 515 } 516 if (opflags & CLOPS_CONFIRM) { 517 NFSLOCKV4ROOTMUTEX(); 518 nfsv4_unlock(&nfsv4rootfs_lock, 1); 519 NFSUNLOCKV4ROOTMUTEX(); 520 } else if (opflags != CLOPS_RENEW) { 521 NFSUNLOCKSTATE(); 522 } 523 if (clpp) 524 *clpp = clp; 525 return (error); 526 } 527 528 /* 529 * Called from the new nfssvc syscall to admin revoke a clientid. 530 * Returns 0 for success, error otherwise. 531 */ 532 APPLESTATIC int 533 nfsrv_adminrevoke(struct nfsd_clid *revokep, NFSPROC_T *p) 534 { 535 struct nfsclient *clp = NULL; 536 int i; 537 int gotit, igotlock; 538 539 /* 540 * First, lock out the nfsd so that state won't change while the 541 * revocation record is being written to the stable storage restart 542 * file. 543 */ 544 NFSLOCKV4ROOTMUTEX(); 545 nfsv4_relref(&nfsv4rootfs_lock); 546 do { 547 igotlock = nfsv4_lock(&nfsv4rootfs_lock, 1, NULL, 548 NFSV4ROOTLOCKMUTEXPTR); 549 } while (!igotlock); 550 NFSUNLOCKV4ROOTMUTEX(); 551 NFSLOCKSTATE(); /* to avoid a race with */ 552 NFSUNLOCKSTATE(); /* nfsrv_servertimer() */ 553 554 /* 555 * Search for a match in the client list. 556 */ 557 gotit = i = 0; 558 while (i < NFSCLIENTHASHSIZE && !gotit) { 559 LIST_FOREACH(clp, &nfsclienthash[i], lc_hash) { 560 if (revokep->nclid_idlen == clp->lc_idlen && 561 !NFSBCMP(revokep->nclid_id, clp->lc_id, clp->lc_idlen)) { 562 gotit = 1; 563 break; 564 } 565 } 566 i++; 567 } 568 if (!gotit) { 569 NFSLOCKV4ROOTMUTEX(); 570 nfsv4_unlock(&nfsv4rootfs_lock, 0); 571 NFSUNLOCKV4ROOTMUTEX(); 572 return (EPERM); 573 } 574 575 /* 576 * Now, write out the revocation record 577 */ 578 nfsrv_writestable(clp->lc_id, clp->lc_idlen, NFSNST_REVOKE, p); 579 580 /* 581 * and clear out the state, marking the clientid revoked. 582 */ 583 clp->lc_flags &= ~LCL_CALLBACKSON; 584 clp->lc_flags |= LCL_ADMINREVOKED; 585 nfsrv_cleanclient(clp, p); 586 nfsrv_freedeleglist(&clp->lc_deleg); 587 nfsrv_freedeleglist(&clp->lc_olddeleg); 588 NFSLOCKV4ROOTMUTEX(); 589 nfsv4_unlock(&nfsv4rootfs_lock, 0); 590 NFSUNLOCKV4ROOTMUTEX(); 591 return (0); 592 } 593 594 /* 595 * Dump out stats for all clients. Called from nfssvc(2), that is used 596 * newnfsstats. 597 */ 598 APPLESTATIC void 599 nfsrv_dumpclients(struct nfsd_dumpclients *dumpp, int maxcnt) 600 { 601 struct nfsclient *clp; 602 int i = 0, cnt = 0; 603 604 NFSLOCKSTATE(); 605 /* 606 * Rattle through the client lists until done. 607 */ 608 while (i < NFSCLIENTHASHSIZE && cnt < maxcnt) { 609 clp = LIST_FIRST(&nfsclienthash[i]); 610 while (clp != LIST_END(&nfsclienthash[i]) && cnt < maxcnt) { 611 nfsrv_dumpaclient(clp, &dumpp[cnt]); 612 cnt++; 613 clp = LIST_NEXT(clp, lc_hash); 614 } 615 i++; 616 } 617 if (cnt < maxcnt) 618 dumpp[cnt].ndcl_clid.nclid_idlen = 0; 619 NFSUNLOCKSTATE(); 620 } 621 622 /* 623 * Dump stats for a client. Must be called with the NFSSTATELOCK and spl'd. 624 */ 625 static void 626 nfsrv_dumpaclient(struct nfsclient *clp, struct nfsd_dumpclients *dumpp) 627 { 628 struct nfsstate *stp, *openstp, *lckownstp; 629 struct nfslock *lop; 630 struct sockaddr *sad; 631 struct sockaddr_in *rad; 632 struct sockaddr_in6 *rad6; 633 634 dumpp->ndcl_nopenowners = dumpp->ndcl_nlockowners = 0; 635 dumpp->ndcl_nopens = dumpp->ndcl_nlocks = 0; 636 dumpp->ndcl_ndelegs = dumpp->ndcl_nolddelegs = 0; 637 dumpp->ndcl_flags = clp->lc_flags; 638 dumpp->ndcl_clid.nclid_idlen = clp->lc_idlen; 639 NFSBCOPY(clp->lc_id, dumpp->ndcl_clid.nclid_id, clp->lc_idlen); 640 sad = NFSSOCKADDR(clp->lc_req.nr_nam, struct sockaddr *); 641 dumpp->ndcl_addrfam = sad->sa_family; 642 if (sad->sa_family == AF_INET) { 643 rad = (struct sockaddr_in *)sad; 644 dumpp->ndcl_cbaddr.sin_addr = rad->sin_addr; 645 } else { 646 rad6 = (struct sockaddr_in6 *)sad; 647 dumpp->ndcl_cbaddr.sin6_addr = rad6->sin6_addr; 648 } 649 650 /* 651 * Now, scan the state lists and total up the opens and locks. 652 */ 653 LIST_FOREACH(stp, &clp->lc_open, ls_list) { 654 dumpp->ndcl_nopenowners++; 655 LIST_FOREACH(openstp, &stp->ls_open, ls_list) { 656 dumpp->ndcl_nopens++; 657 LIST_FOREACH(lckownstp, &openstp->ls_open, ls_list) { 658 dumpp->ndcl_nlockowners++; 659 LIST_FOREACH(lop, &lckownstp->ls_lock, lo_lckowner) { 660 dumpp->ndcl_nlocks++; 661 } 662 } 663 } 664 } 665 666 /* 667 * and the delegation lists. 668 */ 669 LIST_FOREACH(stp, &clp->lc_deleg, ls_list) { 670 dumpp->ndcl_ndelegs++; 671 } 672 LIST_FOREACH(stp, &clp->lc_olddeleg, ls_list) { 673 dumpp->ndcl_nolddelegs++; 674 } 675 } 676 677 /* 678 * Dump out lock stats for a file. 679 */ 680 APPLESTATIC void 681 nfsrv_dumplocks(vnode_t vp, struct nfsd_dumplocks *ldumpp, int maxcnt, 682 NFSPROC_T *p) 683 { 684 struct nfsstate *stp; 685 struct nfslock *lop; 686 int cnt = 0; 687 struct nfslockfile *lfp; 688 struct sockaddr *sad; 689 struct sockaddr_in *rad; 690 struct sockaddr_in6 *rad6; 691 int ret; 692 fhandle_t nfh; 693 694 ret = nfsrv_getlockfh(vp, 0, NULL, &nfh, p); 695 NFSLOCKSTATE(); 696 if (!ret) 697 ret = nfsrv_getlockfile(0, NULL, &lfp, &nfh, 0); 698 if (ret) { 699 ldumpp[0].ndlck_clid.nclid_idlen = 0; 700 NFSUNLOCKSTATE(); 701 return; 702 } 703 704 /* 705 * For each open share on file, dump it out. 706 */ 707 stp = LIST_FIRST(&lfp->lf_open); 708 while (stp != LIST_END(&lfp->lf_open) && cnt < maxcnt) { 709 ldumpp[cnt].ndlck_flags = stp->ls_flags; 710 ldumpp[cnt].ndlck_stateid.seqid = stp->ls_stateid.seqid; 711 ldumpp[cnt].ndlck_stateid.other[0] = stp->ls_stateid.other[0]; 712 ldumpp[cnt].ndlck_stateid.other[1] = stp->ls_stateid.other[1]; 713 ldumpp[cnt].ndlck_stateid.other[2] = stp->ls_stateid.other[2]; 714 ldumpp[cnt].ndlck_owner.nclid_idlen = 715 stp->ls_openowner->ls_ownerlen; 716 NFSBCOPY(stp->ls_openowner->ls_owner, 717 ldumpp[cnt].ndlck_owner.nclid_id, 718 stp->ls_openowner->ls_ownerlen); 719 ldumpp[cnt].ndlck_clid.nclid_idlen = stp->ls_clp->lc_idlen; 720 NFSBCOPY(stp->ls_clp->lc_id, ldumpp[cnt].ndlck_clid.nclid_id, 721 stp->ls_clp->lc_idlen); 722 sad=NFSSOCKADDR(stp->ls_clp->lc_req.nr_nam, struct sockaddr *); 723 ldumpp[cnt].ndlck_addrfam = sad->sa_family; 724 if (sad->sa_family == AF_INET) { 725 rad = (struct sockaddr_in *)sad; 726 ldumpp[cnt].ndlck_cbaddr.sin_addr = rad->sin_addr; 727 } else { 728 rad6 = (struct sockaddr_in6 *)sad; 729 ldumpp[cnt].ndlck_cbaddr.sin6_addr = rad6->sin6_addr; 730 } 731 stp = LIST_NEXT(stp, ls_file); 732 cnt++; 733 } 734 735 /* 736 * and all locks. 737 */ 738 lop = LIST_FIRST(&lfp->lf_lock); 739 while (lop != LIST_END(&lfp->lf_lock) && cnt < maxcnt) { 740 stp = lop->lo_stp; 741 ldumpp[cnt].ndlck_flags = lop->lo_flags; 742 ldumpp[cnt].ndlck_first = lop->lo_first; 743 ldumpp[cnt].ndlck_end = lop->lo_end; 744 ldumpp[cnt].ndlck_stateid.seqid = stp->ls_stateid.seqid; 745 ldumpp[cnt].ndlck_stateid.other[0] = stp->ls_stateid.other[0]; 746 ldumpp[cnt].ndlck_stateid.other[1] = stp->ls_stateid.other[1]; 747 ldumpp[cnt].ndlck_stateid.other[2] = stp->ls_stateid.other[2]; 748 ldumpp[cnt].ndlck_owner.nclid_idlen = stp->ls_ownerlen; 749 NFSBCOPY(stp->ls_owner, ldumpp[cnt].ndlck_owner.nclid_id, 750 stp->ls_ownerlen); 751 ldumpp[cnt].ndlck_clid.nclid_idlen = stp->ls_clp->lc_idlen; 752 NFSBCOPY(stp->ls_clp->lc_id, ldumpp[cnt].ndlck_clid.nclid_id, 753 stp->ls_clp->lc_idlen); 754 sad=NFSSOCKADDR(stp->ls_clp->lc_req.nr_nam, struct sockaddr *); 755 ldumpp[cnt].ndlck_addrfam = sad->sa_family; 756 if (sad->sa_family == AF_INET) { 757 rad = (struct sockaddr_in *)sad; 758 ldumpp[cnt].ndlck_cbaddr.sin_addr = rad->sin_addr; 759 } else { 760 rad6 = (struct sockaddr_in6 *)sad; 761 ldumpp[cnt].ndlck_cbaddr.sin6_addr = rad6->sin6_addr; 762 } 763 lop = LIST_NEXT(lop, lo_lckfile); 764 cnt++; 765 } 766 767 /* 768 * and the delegations. 769 */ 770 stp = LIST_FIRST(&lfp->lf_deleg); 771 while (stp != LIST_END(&lfp->lf_deleg) && cnt < maxcnt) { 772 ldumpp[cnt].ndlck_flags = stp->ls_flags; 773 ldumpp[cnt].ndlck_stateid.seqid = stp->ls_stateid.seqid; 774 ldumpp[cnt].ndlck_stateid.other[0] = stp->ls_stateid.other[0]; 775 ldumpp[cnt].ndlck_stateid.other[1] = stp->ls_stateid.other[1]; 776 ldumpp[cnt].ndlck_stateid.other[2] = stp->ls_stateid.other[2]; 777 ldumpp[cnt].ndlck_owner.nclid_idlen = 0; 778 ldumpp[cnt].ndlck_clid.nclid_idlen = stp->ls_clp->lc_idlen; 779 NFSBCOPY(stp->ls_clp->lc_id, ldumpp[cnt].ndlck_clid.nclid_id, 780 stp->ls_clp->lc_idlen); 781 sad=NFSSOCKADDR(stp->ls_clp->lc_req.nr_nam, struct sockaddr *); 782 ldumpp[cnt].ndlck_addrfam = sad->sa_family; 783 if (sad->sa_family == AF_INET) { 784 rad = (struct sockaddr_in *)sad; 785 ldumpp[cnt].ndlck_cbaddr.sin_addr = rad->sin_addr; 786 } else { 787 rad6 = (struct sockaddr_in6 *)sad; 788 ldumpp[cnt].ndlck_cbaddr.sin6_addr = rad6->sin6_addr; 789 } 790 stp = LIST_NEXT(stp, ls_file); 791 cnt++; 792 } 793 794 /* 795 * If list isn't full, mark end of list by setting the client name 796 * to zero length. 797 */ 798 if (cnt < maxcnt) 799 ldumpp[cnt].ndlck_clid.nclid_idlen = 0; 800 NFSUNLOCKSTATE(); 801 } 802 803 /* 804 * Server timer routine. It can scan any linked list, so long 805 * as it holds the spin lock and there is no exclusive lock on 806 * nfsv4rootfs_lock. 807 * Must be called by a kernel thread and not a timer interrupt, 808 * so that it only runs when the nfsd threads are sleeping on a 809 * uniprocessor and uses the State spin lock for an SMP system. 810 * (For OpenBSD, a kthread is ok. For FreeBSD, I think it is ok 811 * to do this from a callout, since the spin locks work. For 812 * Darwin, I'm not sure what will work correctly yet.) 813 * Should be called once per second. 814 */ 815 APPLESTATIC void 816 nfsrv_servertimer(void) 817 { 818 struct nfsclient *clp, *nclp; 819 struct nfsstate *stp, *nstp; 820 int i; 821 822 /* 823 * Make sure nfsboottime is set. This is used by V3 as well 824 * as V4. Note that nfsboottime is not nfsrvboottime, which is 825 * only used by the V4 server for leases. 826 */ 827 if (nfsboottime.tv_sec == 0) 828 NFSSETBOOTTIME(nfsboottime); 829 830 /* 831 * If server hasn't started yet, just return. 832 */ 833 NFSLOCKSTATE(); 834 if (nfsrv_stablefirst.nsf_eograce == 0) { 835 NFSUNLOCKSTATE(); 836 return; 837 } 838 if (!(nfsrv_stablefirst.nsf_flags & NFSNSF_UPDATEDONE)) { 839 if (!(nfsrv_stablefirst.nsf_flags & NFSNSF_GRACEOVER) && 840 NFSD_MONOSEC > nfsrv_stablefirst.nsf_eograce) 841 nfsrv_stablefirst.nsf_flags |= 842 (NFSNSF_GRACEOVER | NFSNSF_NEEDLOCK); 843 NFSUNLOCKSTATE(); 844 return; 845 } 846 847 /* 848 * Return now if an nfsd thread has the exclusive lock on 849 * nfsv4rootfs_lock. The dirty trick here is that we have 850 * the spin lock already and the nfsd threads do a: 851 * NFSLOCKSTATE, NFSUNLOCKSTATE after getting the exclusive 852 * lock, so they won't race with code after this check. 853 */ 854 if (nfsv4rootfs_lock.nfslock_lock & NFSV4LOCK_LOCK) { 855 NFSUNLOCKSTATE(); 856 return; 857 } 858 859 /* 860 * For each client... 861 */ 862 for (i = 0; i < NFSCLIENTHASHSIZE; i++) { 863 clp = LIST_FIRST(&nfsclienthash[i]); 864 while (clp != LIST_END(&nfsclienthash[i])) { 865 nclp = LIST_NEXT(clp, lc_hash); 866 if (!(clp->lc_flags & LCL_EXPIREIT)) { 867 if (((clp->lc_expiry + NFSRV_STALELEASE) < NFSD_MONOSEC 868 && ((LIST_EMPTY(&clp->lc_deleg) 869 && LIST_EMPTY(&clp->lc_open)) || 870 nfsrv_clients > nfsrv_clienthighwater)) || 871 (clp->lc_expiry + NFSRV_MOULDYLEASE) < NFSD_MONOSEC || 872 (clp->lc_expiry < NFSD_MONOSEC && 873 (nfsrv_openpluslock * 10 / 9) > NFSRV_V4STATELIMIT)) { 874 /* 875 * Lease has expired several nfsrv_lease times ago: 876 * PLUS 877 * - no state is associated with it 878 * OR 879 * - above high water mark for number of clients 880 * (nfsrv_clienthighwater should be large enough 881 * that this only occurs when clients fail to 882 * use the same nfs_client_id4.id. Maybe somewhat 883 * higher that the maximum number of clients that 884 * will mount this server?) 885 * OR 886 * Lease has expired a very long time ago 887 * OR 888 * Lease has expired PLUS the number of opens + locks 889 * has exceeded 90% of capacity 890 * 891 * --> Mark for expiry. The actual expiry will be done 892 * by an nfsd sometime soon. 893 */ 894 clp->lc_flags |= LCL_EXPIREIT; 895 nfsrv_stablefirst.nsf_flags |= 896 (NFSNSF_NEEDLOCK | NFSNSF_EXPIREDCLIENT); 897 } else { 898 /* 899 * If there are no opens, increment no open tick cnt 900 * If time exceeds NFSNOOPEN, mark it to be thrown away 901 * otherwise, if there is an open, reset no open time 902 * Hopefully, this will avoid excessive re-creation 903 * of open owners and subsequent open confirms. 904 */ 905 stp = LIST_FIRST(&clp->lc_open); 906 while (stp != LIST_END(&clp->lc_open)) { 907 nstp = LIST_NEXT(stp, ls_list); 908 if (LIST_EMPTY(&stp->ls_open)) { 909 stp->ls_noopens++; 910 if (stp->ls_noopens > NFSNOOPEN || 911 (nfsrv_openpluslock * 2) > 912 NFSRV_V4STATELIMIT) 913 nfsrv_stablefirst.nsf_flags |= 914 NFSNSF_NOOPENS; 915 } else { 916 stp->ls_noopens = 0; 917 } 918 stp = nstp; 919 } 920 } 921 } 922 clp = nclp; 923 } 924 } 925 NFSUNLOCKSTATE(); 926 } 927 928 /* 929 * The following set of functions free up the various data structures. 930 */ 931 /* 932 * Clear out all open/lock state related to this nfsclient. 933 * Caller must hold an exclusive lock on nfsv4rootfs_lock, so that 934 * there are no other active nfsd threads. 935 */ 936 APPLESTATIC void 937 nfsrv_cleanclient(struct nfsclient *clp, NFSPROC_T *p) 938 { 939 struct nfsstate *stp, *nstp; 940 941 LIST_FOREACH_SAFE(stp, &clp->lc_open, ls_list, nstp) 942 nfsrv_freeopenowner(stp, 1, p); 943 } 944 945 /* 946 * Free a client that has been cleaned. It should also already have been 947 * removed from the lists. 948 * (Just to be safe w.r.t. newnfs_disconnect(), call this function when 949 * softclock interrupts are enabled.) 950 */ 951 APPLESTATIC void 952 nfsrv_zapclient(struct nfsclient *clp, NFSPROC_T *p) 953 { 954 955 #ifdef notyet 956 if ((clp->lc_flags & (LCL_GSS | LCL_CALLBACKSON)) == 957 (LCL_GSS | LCL_CALLBACKSON) && 958 (clp->lc_hand.nfsh_flag & NFSG_COMPLETE) && 959 clp->lc_handlelen > 0) { 960 clp->lc_hand.nfsh_flag &= ~NFSG_COMPLETE; 961 clp->lc_hand.nfsh_flag |= NFSG_DESTROYED; 962 (void) nfsrv_docallback(clp, NFSV4PROC_CBNULL, 963 NULL, 0, NULL, NULL, NULL, p); 964 } 965 #endif 966 newnfs_disconnect(&clp->lc_req); 967 NFSSOCKADDRFREE(clp->lc_req.nr_nam); 968 NFSFREEMUTEX(&clp->lc_req.nr_mtx); 969 free((caddr_t)clp, M_NFSDCLIENT); 970 NFSLOCKSTATE(); 971 newnfsstats.srvclients--; 972 nfsrv_openpluslock--; 973 nfsrv_clients--; 974 NFSUNLOCKSTATE(); 975 } 976 977 /* 978 * Free a list of delegation state structures. 979 * (This function will also free all nfslockfile structures that no 980 * longer have associated state.) 981 */ 982 APPLESTATIC void 983 nfsrv_freedeleglist(struct nfsstatehead *sthp) 984 { 985 struct nfsstate *stp, *nstp; 986 987 LIST_FOREACH_SAFE(stp, sthp, ls_list, nstp) { 988 nfsrv_freedeleg(stp); 989 } 990 LIST_INIT(sthp); 991 } 992 993 /* 994 * Free up a delegation. 995 */ 996 static void 997 nfsrv_freedeleg(struct nfsstate *stp) 998 { 999 struct nfslockfile *lfp; 1000 1001 LIST_REMOVE(stp, ls_hash); 1002 LIST_REMOVE(stp, ls_list); 1003 LIST_REMOVE(stp, ls_file); 1004 lfp = stp->ls_lfp; 1005 if (LIST_EMPTY(&lfp->lf_open) && 1006 LIST_EMPTY(&lfp->lf_lock) && LIST_EMPTY(&lfp->lf_deleg) && 1007 LIST_EMPTY(&lfp->lf_locallock) && LIST_EMPTY(&lfp->lf_rollback) && 1008 lfp->lf_usecount == 0 && 1009 nfsv4_testlock(&lfp->lf_locallock_lck) == 0) 1010 nfsrv_freenfslockfile(lfp); 1011 FREE((caddr_t)stp, M_NFSDSTATE); 1012 newnfsstats.srvdelegates--; 1013 nfsrv_openpluslock--; 1014 nfsrv_delegatecnt--; 1015 } 1016 1017 /* 1018 * This function frees an open owner and all associated opens. 1019 * Must be called with soft clock interrupts disabled. 1020 */ 1021 static void 1022 nfsrv_freeopenowner(struct nfsstate *stp, int cansleep, NFSPROC_T *p) 1023 { 1024 struct nfsstate *nstp, *tstp; 1025 1026 LIST_REMOVE(stp, ls_list); 1027 /* 1028 * Now, free all associated opens. 1029 */ 1030 nstp = LIST_FIRST(&stp->ls_open); 1031 while (nstp != LIST_END(&stp->ls_open)) { 1032 tstp = nstp; 1033 nstp = LIST_NEXT(nstp, ls_list); 1034 (void) nfsrv_freeopen(tstp, NULL, cansleep, p); 1035 } 1036 if (stp->ls_op) 1037 nfsrvd_derefcache(stp->ls_op); 1038 FREE((caddr_t)stp, M_NFSDSTATE); 1039 newnfsstats.srvopenowners--; 1040 nfsrv_openpluslock--; 1041 } 1042 1043 /* 1044 * This function frees an open (nfsstate open structure) with all associated 1045 * lock_owners and locks. It also frees the nfslockfile structure iff there 1046 * are no other opens on the file. 1047 * Returns 1 if it free'd the nfslockfile, 0 otherwise. 1048 */ 1049 static int 1050 nfsrv_freeopen(struct nfsstate *stp, vnode_t vp, int cansleep, NFSPROC_T *p) 1051 { 1052 struct nfsstate *nstp, *tstp; 1053 struct nfslockfile *lfp; 1054 int ret; 1055 1056 LIST_REMOVE(stp, ls_hash); 1057 LIST_REMOVE(stp, ls_list); 1058 LIST_REMOVE(stp, ls_file); 1059 1060 lfp = stp->ls_lfp; 1061 /* 1062 * Now, free all lockowners associated with this open. 1063 */ 1064 LIST_FOREACH_SAFE(tstp, &stp->ls_open, ls_list, nstp) 1065 nfsrv_freelockowner(tstp, vp, cansleep, p); 1066 1067 /* 1068 * The nfslockfile is freed here if there are no locks 1069 * associated with the open. 1070 * If there are locks associated with the open, the 1071 * nfslockfile structure can be freed via nfsrv_freelockowner(). 1072 * (That is why the call must be here instead of after the loop.) 1073 */ 1074 if (lfp != NULL && LIST_EMPTY(&lfp->lf_open) && 1075 LIST_EMPTY(&lfp->lf_deleg) && LIST_EMPTY(&lfp->lf_lock) && 1076 LIST_EMPTY(&lfp->lf_locallock) && LIST_EMPTY(&lfp->lf_rollback) && 1077 lfp->lf_usecount == 0 && 1078 (cansleep != 0 || nfsv4_testlock(&lfp->lf_locallock_lck) == 0)) { 1079 nfsrv_freenfslockfile(lfp); 1080 ret = 1; 1081 } else 1082 ret = 0; 1083 FREE((caddr_t)stp, M_NFSDSTATE); 1084 newnfsstats.srvopens--; 1085 nfsrv_openpluslock--; 1086 return (ret); 1087 } 1088 1089 /* 1090 * Frees a lockowner and all associated locks. 1091 */ 1092 static void 1093 nfsrv_freelockowner(struct nfsstate *stp, vnode_t vp, int cansleep, 1094 NFSPROC_T *p) 1095 { 1096 1097 LIST_REMOVE(stp, ls_hash); 1098 LIST_REMOVE(stp, ls_list); 1099 nfsrv_freeallnfslocks(stp, vp, cansleep, p); 1100 if (stp->ls_op) 1101 nfsrvd_derefcache(stp->ls_op); 1102 FREE((caddr_t)stp, M_NFSDSTATE); 1103 newnfsstats.srvlockowners--; 1104 nfsrv_openpluslock--; 1105 } 1106 1107 /* 1108 * Free all the nfs locks on a lockowner. 1109 */ 1110 static void 1111 nfsrv_freeallnfslocks(struct nfsstate *stp, vnode_t vp, int cansleep, 1112 NFSPROC_T *p) 1113 { 1114 struct nfslock *lop, *nlop; 1115 struct nfsrollback *rlp, *nrlp; 1116 struct nfslockfile *lfp = NULL; 1117 int gottvp = 0; 1118 vnode_t tvp = NULL; 1119 1120 lop = LIST_FIRST(&stp->ls_lock); 1121 while (lop != LIST_END(&stp->ls_lock)) { 1122 nlop = LIST_NEXT(lop, lo_lckowner); 1123 /* 1124 * Since all locks should be for the same file, lfp should 1125 * not change. 1126 */ 1127 if (lfp == NULL) 1128 lfp = lop->lo_lfp; 1129 else if (lfp != lop->lo_lfp) 1130 panic("allnfslocks"); 1131 /* 1132 * If vp is NULL and cansleep != 0, a vnode must be acquired 1133 * from the file handle. This only occurs when called from 1134 * nfsrv_cleanclient(). 1135 */ 1136 if (gottvp == 0) { 1137 if (nfsrv_dolocallocks == 0) 1138 tvp = NULL; 1139 else if (vp == NULL && cansleep != 0) 1140 tvp = nfsvno_getvp(&lfp->lf_fh); 1141 else 1142 tvp = vp; 1143 gottvp = 1; 1144 } 1145 1146 if (tvp != NULL) { 1147 if (cansleep == 0) 1148 panic("allnfs2"); 1149 nfsrv_localunlock(tvp, lfp, lop->lo_first, 1150 lop->lo_end, p); 1151 LIST_FOREACH_SAFE(rlp, &lfp->lf_rollback, rlck_list, 1152 nrlp) 1153 free(rlp, M_NFSDROLLBACK); 1154 LIST_INIT(&lfp->lf_rollback); 1155 } 1156 nfsrv_freenfslock(lop); 1157 lop = nlop; 1158 } 1159 if (vp == NULL && tvp != NULL) 1160 vput(tvp); 1161 } 1162 1163 /* 1164 * Free an nfslock structure. 1165 * Must be called with soft clock interrupts disabled. 1166 */ 1167 static void 1168 nfsrv_freenfslock(struct nfslock *lop) 1169 { 1170 1171 if (lop->lo_lckfile.le_prev != NULL) { 1172 LIST_REMOVE(lop, lo_lckfile); 1173 newnfsstats.srvlocks--; 1174 nfsrv_openpluslock--; 1175 } 1176 LIST_REMOVE(lop, lo_lckowner); 1177 FREE((caddr_t)lop, M_NFSDLOCK); 1178 } 1179 1180 /* 1181 * This function frees an nfslockfile structure. 1182 * Must be called with soft clock interrupts disabled. 1183 */ 1184 static void 1185 nfsrv_freenfslockfile(struct nfslockfile *lfp) 1186 { 1187 1188 LIST_REMOVE(lfp, lf_hash); 1189 FREE((caddr_t)lfp, M_NFSDLOCKFILE); 1190 } 1191 1192 /* 1193 * This function looks up an nfsstate structure via stateid. 1194 */ 1195 static int 1196 nfsrv_getstate(struct nfsclient *clp, nfsv4stateid_t *stateidp, __unused u_int32_t flags, 1197 struct nfsstate **stpp) 1198 { 1199 struct nfsstate *stp; 1200 struct nfsstatehead *hp; 1201 1202 *stpp = NULL; 1203 hp = NFSSTATEHASH(clp, *stateidp); 1204 LIST_FOREACH(stp, hp, ls_hash) { 1205 if (!NFSBCMP(stp->ls_stateid.other, stateidp->other, 1206 NFSX_STATEIDOTHER)) 1207 break; 1208 } 1209 1210 /* 1211 * If no state id in list, return NFSERR_BADSTATEID. 1212 */ 1213 if (stp == LIST_END(hp)) 1214 return (NFSERR_BADSTATEID); 1215 *stpp = stp; 1216 return (0); 1217 } 1218 1219 /* 1220 * This function gets an nfsstate structure via owner string. 1221 */ 1222 static void 1223 nfsrv_getowner(struct nfsstatehead *hp, struct nfsstate *new_stp, 1224 struct nfsstate **stpp) 1225 { 1226 struct nfsstate *stp; 1227 1228 *stpp = NULL; 1229 LIST_FOREACH(stp, hp, ls_list) { 1230 if (new_stp->ls_ownerlen == stp->ls_ownerlen && 1231 !NFSBCMP(new_stp->ls_owner,stp->ls_owner,stp->ls_ownerlen)) { 1232 *stpp = stp; 1233 return; 1234 } 1235 } 1236 } 1237 1238 /* 1239 * Lock control function called to update lock status. 1240 * Returns 0 upon success, -1 if there is no lock and the flags indicate 1241 * that one isn't to be created and an NFSERR_xxx for other errors. 1242 * The structures new_stp and new_lop are passed in as pointers that should 1243 * be set to NULL if the structure is used and shouldn't be free'd. 1244 * For the NFSLCK_TEST and NFSLCK_CHECK cases, the structures are 1245 * never used and can safely be allocated on the stack. For all other 1246 * cases, *new_stpp and *new_lopp should be malloc'd before the call, 1247 * in case they are used. 1248 */ 1249 APPLESTATIC int 1250 nfsrv_lockctrl(vnode_t vp, struct nfsstate **new_stpp, 1251 struct nfslock **new_lopp, struct nfslockconflict *cfp, 1252 nfsquad_t clientid, nfsv4stateid_t *stateidp, 1253 __unused struct nfsexstuff *exp, 1254 struct nfsrv_descript *nd, NFSPROC_T *p) 1255 { 1256 struct nfslock *lop; 1257 struct nfsstate *new_stp = *new_stpp; 1258 struct nfslock *new_lop = *new_lopp; 1259 struct nfsstate *tstp, *mystp, *nstp; 1260 int specialid = 0; 1261 struct nfslockfile *lfp; 1262 struct nfslock *other_lop = NULL; 1263 struct nfsstate *stp, *lckstp = NULL; 1264 struct nfsclient *clp = NULL; 1265 u_int32_t bits; 1266 int error = 0, haslock = 0, ret, reterr; 1267 int getlckret, delegation = 0, filestruct_locked; 1268 fhandle_t nfh; 1269 uint64_t first, end; 1270 uint32_t lock_flags; 1271 1272 if (new_stp->ls_flags & (NFSLCK_CHECK | NFSLCK_SETATTR)) { 1273 /* 1274 * Note the special cases of "all 1s" or "all 0s" stateids and 1275 * let reads with all 1s go ahead. 1276 */ 1277 if (new_stp->ls_stateid.seqid == 0x0 && 1278 new_stp->ls_stateid.other[0] == 0x0 && 1279 new_stp->ls_stateid.other[1] == 0x0 && 1280 new_stp->ls_stateid.other[2] == 0x0) 1281 specialid = 1; 1282 else if (new_stp->ls_stateid.seqid == 0xffffffff && 1283 new_stp->ls_stateid.other[0] == 0xffffffff && 1284 new_stp->ls_stateid.other[1] == 0xffffffff && 1285 new_stp->ls_stateid.other[2] == 0xffffffff) 1286 specialid = 2; 1287 } 1288 1289 /* 1290 * Check for restart conditions (client and server). 1291 */ 1292 error = nfsrv_checkrestart(clientid, new_stp->ls_flags, 1293 &new_stp->ls_stateid, specialid); 1294 if (error) 1295 return (error); 1296 1297 /* 1298 * Check for state resource limit exceeded. 1299 */ 1300 if ((new_stp->ls_flags & NFSLCK_LOCK) && 1301 nfsrv_openpluslock > NFSRV_V4STATELIMIT) 1302 return (NFSERR_RESOURCE); 1303 1304 /* 1305 * For the lock case, get another nfslock structure, 1306 * just in case we need it. 1307 * Malloc now, before we start sifting through the linked lists, 1308 * in case we have to wait for memory. 1309 */ 1310 tryagain: 1311 if (new_stp->ls_flags & NFSLCK_LOCK) 1312 MALLOC(other_lop, struct nfslock *, sizeof (struct nfslock), 1313 M_NFSDLOCK, M_WAITOK); 1314 filestruct_locked = 0; 1315 reterr = 0; 1316 lfp = NULL; 1317 1318 /* 1319 * Get the lockfile structure for CFH now, so we can do a sanity 1320 * check against the stateid, before incrementing the seqid#, since 1321 * we want to return NFSERR_BADSTATEID on failure and the seqid# 1322 * shouldn't be incremented for this case. 1323 * If nfsrv_getlockfile() returns -1, it means "not found", which 1324 * will be handled later. 1325 * If we are doing Lock/LockU and local locking is enabled, sleep 1326 * lock the nfslockfile structure. 1327 */ 1328 getlckret = nfsrv_getlockfh(vp, new_stp->ls_flags, NULL, &nfh, p); 1329 NFSLOCKSTATE(); 1330 if (getlckret == 0) { 1331 if ((new_stp->ls_flags & (NFSLCK_LOCK | NFSLCK_UNLOCK)) != 0 && 1332 nfsrv_dolocallocks != 0 && nd->nd_repstat == 0) { 1333 getlckret = nfsrv_getlockfile(new_stp->ls_flags, NULL, 1334 &lfp, &nfh, 1); 1335 if (getlckret == 0) 1336 filestruct_locked = 1; 1337 } else 1338 getlckret = nfsrv_getlockfile(new_stp->ls_flags, NULL, 1339 &lfp, &nfh, 0); 1340 } 1341 if (getlckret != 0 && getlckret != -1) 1342 reterr = getlckret; 1343 1344 if (filestruct_locked != 0) { 1345 LIST_INIT(&lfp->lf_rollback); 1346 if ((new_stp->ls_flags & NFSLCK_LOCK)) { 1347 /* 1348 * For local locking, do the advisory locking now, so 1349 * that any conflict can be detected. A failure later 1350 * can be rolled back locally. If an error is returned, 1351 * struct nfslockfile has been unlocked and any local 1352 * locking rolled back. 1353 */ 1354 NFSUNLOCKSTATE(); 1355 reterr = nfsrv_locallock(vp, lfp, 1356 (new_lop->lo_flags & (NFSLCK_READ | NFSLCK_WRITE)), 1357 new_lop->lo_first, new_lop->lo_end, cfp, p); 1358 NFSLOCKSTATE(); 1359 } 1360 } 1361 1362 /* 1363 * Since the code is manipulating lists that are also 1364 * manipulated by nfsrv_servertimer(), soft clock interrupts 1365 * must be masked off. 1366 */ 1367 if (specialid == 0) { 1368 if (new_stp->ls_flags & NFSLCK_TEST) { 1369 /* 1370 * RFC 3530 does not list LockT as an op that renews a 1371 * lease, but the concensus seems to be that it is ok 1372 * for a server to do so. 1373 */ 1374 error = nfsrv_getclient(clientid, CLOPS_RENEW, &clp, 1375 (nfsquad_t)((u_quad_t)0), NULL, p); 1376 1377 /* 1378 * Since NFSERR_EXPIRED, NFSERR_ADMINREVOKED are not valid 1379 * error returns for LockT, just go ahead and test for a lock, 1380 * since there are no locks for this client, but other locks 1381 * can conflict. (ie. same client will always be false) 1382 */ 1383 if (error == NFSERR_EXPIRED || error == NFSERR_ADMINREVOKED) 1384 error = 0; 1385 lckstp = new_stp; 1386 } else { 1387 error = nfsrv_getclient(clientid, CLOPS_RENEW, &clp, 1388 (nfsquad_t)((u_quad_t)0), NULL, p); 1389 if (error == 0) 1390 /* 1391 * Look up the stateid 1392 */ 1393 error = nfsrv_getstate(clp, &new_stp->ls_stateid, 1394 new_stp->ls_flags, &stp); 1395 /* 1396 * do some sanity checks for an unconfirmed open or a 1397 * stateid that refers to the wrong file, for an open stateid 1398 */ 1399 if (error == 0 && (stp->ls_flags & NFSLCK_OPEN) && 1400 ((stp->ls_openowner->ls_flags & NFSLCK_NEEDSCONFIRM) || 1401 (getlckret == 0 && stp->ls_lfp != lfp))) 1402 error = NFSERR_BADSTATEID; 1403 if (error == 0 && 1404 (stp->ls_flags & (NFSLCK_DELEGREAD | NFSLCK_DELEGWRITE)) && 1405 getlckret == 0 && stp->ls_lfp != lfp) 1406 error = NFSERR_BADSTATEID; 1407 1408 /* 1409 * If the lockowner stateid doesn't refer to the same file, 1410 * I believe that is considered ok, since some clients will 1411 * only create a single lockowner and use that for all locks 1412 * on all files. 1413 * For now, log it as a diagnostic, instead of considering it 1414 * a BadStateid. 1415 */ 1416 if (error == 0 && (stp->ls_flags & 1417 (NFSLCK_OPEN | NFSLCK_DELEGREAD | NFSLCK_DELEGWRITE)) == 0 && 1418 getlckret == 0 && stp->ls_lfp != lfp) { 1419 #ifdef DIAGNOSTIC 1420 printf("Got a lock statid for different file open\n"); 1421 #endif 1422 /* 1423 error = NFSERR_BADSTATEID; 1424 */ 1425 } 1426 1427 if (error == 0) { 1428 if (new_stp->ls_flags & NFSLCK_OPENTOLOCK) { 1429 /* 1430 * If haslock set, we've already checked the seqid. 1431 */ 1432 if (!haslock) { 1433 if (stp->ls_flags & NFSLCK_OPEN) 1434 error = nfsrv_checkseqid(nd, new_stp->ls_seq, 1435 stp->ls_openowner, new_stp->ls_op); 1436 else 1437 error = NFSERR_BADSTATEID; 1438 } 1439 if (!error) 1440 nfsrv_getowner(&stp->ls_open, new_stp, &lckstp); 1441 if (lckstp) 1442 /* 1443 * I believe this should be an error, but it 1444 * isn't obvious what NFSERR_xxx would be 1445 * appropriate, so I'll use NFSERR_INVAL for now. 1446 */ 1447 error = NFSERR_INVAL; 1448 else 1449 lckstp = new_stp; 1450 } else if (new_stp->ls_flags&(NFSLCK_LOCK|NFSLCK_UNLOCK)) { 1451 /* 1452 * If haslock set, ditto above. 1453 */ 1454 if (!haslock) { 1455 if (stp->ls_flags & NFSLCK_OPEN) 1456 error = NFSERR_BADSTATEID; 1457 else 1458 error = nfsrv_checkseqid(nd, new_stp->ls_seq, 1459 stp, new_stp->ls_op); 1460 } 1461 lckstp = stp; 1462 } else { 1463 lckstp = stp; 1464 } 1465 } 1466 /* 1467 * If the seqid part of the stateid isn't the same, return 1468 * NFSERR_OLDSTATEID for cases other than I/O Ops. 1469 * For I/O Ops, only return NFSERR_OLDSTATEID if 1470 * nfsrv_returnoldstateid is set. (The concensus on the email 1471 * list was that most clients would prefer to not receive 1472 * NFSERR_OLDSTATEID for I/O Ops, but the RFC suggests that that 1473 * is what will happen, so I use the nfsrv_returnoldstateid to 1474 * allow for either server configuration.) 1475 */ 1476 if (!error && stp->ls_stateid.seqid!=new_stp->ls_stateid.seqid && 1477 (!(new_stp->ls_flags & NFSLCK_CHECK) || 1478 nfsrv_returnoldstateid)) 1479 error = NFSERR_OLDSTATEID; 1480 } 1481 } 1482 1483 /* 1484 * Now we can check for grace. 1485 */ 1486 if (!error) 1487 error = nfsrv_checkgrace(new_stp->ls_flags); 1488 if ((new_stp->ls_flags & NFSLCK_RECLAIM) && !error && 1489 nfsrv_checkstable(clp)) 1490 error = NFSERR_NOGRACE; 1491 /* 1492 * If we successfully Reclaimed state, note that. 1493 */ 1494 if ((new_stp->ls_flags & NFSLCK_RECLAIM) && !error) 1495 nfsrv_markstable(clp); 1496 1497 /* 1498 * At this point, either error == NFSERR_BADSTATEID or the 1499 * seqid# has been updated, so we can return any error. 1500 * If error == 0, there may be an error in: 1501 * nd_repstat - Set by the calling function. 1502 * reterr - Set above, if getting the nfslockfile structure 1503 * or acquiring the local lock failed. 1504 * (If both of these are set, nd_repstat should probably be 1505 * returned, since that error was detected before this 1506 * function call.) 1507 */ 1508 if (error != 0 || nd->nd_repstat != 0 || reterr != 0) { 1509 if (error == 0) { 1510 if (nd->nd_repstat != 0) 1511 error = nd->nd_repstat; 1512 else 1513 error = reterr; 1514 } 1515 if (filestruct_locked != 0) { 1516 /* Roll back local locks. */ 1517 NFSUNLOCKSTATE(); 1518 nfsrv_locallock_rollback(vp, lfp, p); 1519 NFSLOCKSTATE(); 1520 nfsrv_unlocklf(lfp); 1521 } 1522 NFSUNLOCKSTATE(); 1523 if (other_lop) 1524 FREE((caddr_t)other_lop, M_NFSDLOCK); 1525 if (haslock) { 1526 NFSLOCKV4ROOTMUTEX(); 1527 nfsv4_unlock(&nfsv4rootfs_lock, 1); 1528 NFSUNLOCKV4ROOTMUTEX(); 1529 } 1530 return (error); 1531 } 1532 1533 /* 1534 * Check the nfsrv_getlockfile return. 1535 * Returned -1 if no structure found. 1536 */ 1537 if (getlckret == -1) { 1538 error = NFSERR_EXPIRED; 1539 /* 1540 * Called from lockt, so no lock is OK. 1541 */ 1542 if (new_stp->ls_flags & NFSLCK_TEST) { 1543 error = 0; 1544 } else if (new_stp->ls_flags & 1545 (NFSLCK_CHECK | NFSLCK_SETATTR)) { 1546 /* 1547 * Called to check for a lock, OK if the stateid is all 1548 * 1s or all 0s, but there should be an nfsstate 1549 * otherwise. 1550 * (ie. If there is no open, I'll assume no share 1551 * deny bits.) 1552 */ 1553 if (specialid) 1554 error = 0; 1555 else 1556 error = NFSERR_BADSTATEID; 1557 } 1558 NFSUNLOCKSTATE(); 1559 if (haslock) { 1560 NFSLOCKV4ROOTMUTEX(); 1561 nfsv4_unlock(&nfsv4rootfs_lock, 1); 1562 NFSUNLOCKV4ROOTMUTEX(); 1563 } 1564 /* 1565 * Called to lock or unlock, so the lock has gone away. 1566 */ 1567 return (error); 1568 } 1569 1570 /* 1571 * For NFSLCK_CHECK and NFSLCK_LOCK, test for a share conflict. 1572 * For NFSLCK_CHECK, allow a read if write access is granted, 1573 * but check for a deny. For NFSLCK_LOCK, require correct access, 1574 * which implies a conflicting deny can't exist. 1575 */ 1576 if (new_stp->ls_flags & (NFSLCK_CHECK | NFSLCK_LOCK)) { 1577 /* 1578 * Four kinds of state id: 1579 * - specialid (all 0s or all 1s), only for NFSLCK_CHECK 1580 * - stateid for an open 1581 * - stateid for a delegation 1582 * - stateid for a lock owner 1583 */ 1584 if (!specialid) { 1585 if (stp->ls_flags & (NFSLCK_DELEGREAD | NFSLCK_DELEGWRITE)) { 1586 delegation = 1; 1587 mystp = stp; 1588 nfsrv_delaydelegtimeout(stp); 1589 } else if (stp->ls_flags & NFSLCK_OPEN) { 1590 mystp = stp; 1591 } else { 1592 mystp = stp->ls_openstp; 1593 } 1594 /* 1595 * If locking or checking, require correct access 1596 * bit set. 1597 */ 1598 if (((new_stp->ls_flags & NFSLCK_LOCK) && 1599 !((new_lop->lo_flags >> NFSLCK_LOCKSHIFT) & 1600 mystp->ls_flags & NFSLCK_ACCESSBITS)) || 1601 ((new_stp->ls_flags & (NFSLCK_CHECK|NFSLCK_READACCESS)) == 1602 (NFSLCK_CHECK | NFSLCK_READACCESS) && 1603 !(mystp->ls_flags & NFSLCK_READACCESS)) || 1604 ((new_stp->ls_flags & (NFSLCK_CHECK|NFSLCK_WRITEACCESS)) == 1605 (NFSLCK_CHECK | NFSLCK_WRITEACCESS) && 1606 !(mystp->ls_flags & NFSLCK_WRITEACCESS))) { 1607 if (filestruct_locked != 0) { 1608 /* Roll back local locks. */ 1609 NFSUNLOCKSTATE(); 1610 nfsrv_locallock_rollback(vp, lfp, p); 1611 NFSLOCKSTATE(); 1612 nfsrv_unlocklf(lfp); 1613 } 1614 NFSUNLOCKSTATE(); 1615 if (other_lop) 1616 FREE((caddr_t)other_lop, M_NFSDLOCK); 1617 if (haslock) { 1618 NFSLOCKV4ROOTMUTEX(); 1619 nfsv4_unlock(&nfsv4rootfs_lock, 1); 1620 NFSUNLOCKV4ROOTMUTEX(); 1621 } 1622 return (NFSERR_OPENMODE); 1623 } 1624 } else 1625 mystp = NULL; 1626 if ((new_stp->ls_flags & NFSLCK_CHECK) && !delegation) { 1627 /* 1628 * Check for a conflicting deny bit. 1629 */ 1630 LIST_FOREACH(tstp, &lfp->lf_open, ls_file) { 1631 if (tstp != mystp) { 1632 bits = tstp->ls_flags; 1633 bits >>= NFSLCK_SHIFT; 1634 if (new_stp->ls_flags & bits & NFSLCK_ACCESSBITS) { 1635 ret = nfsrv_clientconflict(tstp->ls_clp, &haslock, 1636 vp, p); 1637 if (ret) { 1638 /* 1639 * nfsrv_clientconflict unlocks state 1640 * when it returns non-zero. 1641 */ 1642 lckstp = NULL; 1643 goto tryagain; 1644 } 1645 NFSUNLOCKSTATE(); 1646 if (haslock) { 1647 NFSLOCKV4ROOTMUTEX(); 1648 nfsv4_unlock(&nfsv4rootfs_lock, 1); 1649 NFSUNLOCKV4ROOTMUTEX(); 1650 } 1651 return (NFSERR_OPENMODE); 1652 } 1653 } 1654 } 1655 1656 /* We're outta here */ 1657 NFSUNLOCKSTATE(); 1658 if (haslock) { 1659 NFSLOCKV4ROOTMUTEX(); 1660 nfsv4_unlock(&nfsv4rootfs_lock, 1); 1661 NFSUNLOCKV4ROOTMUTEX(); 1662 } 1663 return (0); 1664 } 1665 } 1666 1667 /* 1668 * For setattr, just get rid of all the Delegations for other clients. 1669 */ 1670 if (new_stp->ls_flags & NFSLCK_SETATTR) { 1671 ret = nfsrv_cleandeleg(vp, lfp, clp, &haslock, p); 1672 if (ret) { 1673 /* 1674 * nfsrv_cleandeleg() unlocks state when it 1675 * returns non-zero. 1676 */ 1677 if (ret == -1) { 1678 lckstp = NULL; 1679 goto tryagain; 1680 } 1681 return (ret); 1682 } 1683 if (!(new_stp->ls_flags & NFSLCK_CHECK) || 1684 (LIST_EMPTY(&lfp->lf_open) && LIST_EMPTY(&lfp->lf_lock) && 1685 LIST_EMPTY(&lfp->lf_deleg))) { 1686 NFSUNLOCKSTATE(); 1687 if (haslock) { 1688 NFSLOCKV4ROOTMUTEX(); 1689 nfsv4_unlock(&nfsv4rootfs_lock, 1); 1690 NFSUNLOCKV4ROOTMUTEX(); 1691 } 1692 return (0); 1693 } 1694 } 1695 1696 /* 1697 * Check for a conflicting delegation. If one is found, call 1698 * nfsrv_delegconflict() to handle it. If the v4root lock hasn't 1699 * been set yet, it will get the lock. Otherwise, it will recall 1700 * the delegation. Then, we try try again... 1701 * I currently believe the conflict algorithm to be: 1702 * For Lock Ops (Lock/LockT/LockU) 1703 * - there is a conflict iff a different client has a write delegation 1704 * For Reading (Read Op) 1705 * - there is a conflict iff a different client has a write delegation 1706 * (the specialids are always a different client) 1707 * For Writing (Write/Setattr of size) 1708 * - there is a conflict if a different client has any delegation 1709 * - there is a conflict if the same client has a read delegation 1710 * (I don't understand why this isn't allowed, but that seems to be 1711 * the current concensus?) 1712 */ 1713 tstp = LIST_FIRST(&lfp->lf_deleg); 1714 while (tstp != LIST_END(&lfp->lf_deleg)) { 1715 nstp = LIST_NEXT(tstp, ls_file); 1716 if ((((new_stp->ls_flags&(NFSLCK_LOCK|NFSLCK_UNLOCK|NFSLCK_TEST))|| 1717 ((new_stp->ls_flags & NFSLCK_CHECK) && 1718 (new_lop->lo_flags & NFSLCK_READ))) && 1719 clp != tstp->ls_clp && 1720 (tstp->ls_flags & NFSLCK_DELEGWRITE)) || 1721 ((new_stp->ls_flags & NFSLCK_CHECK) && 1722 (new_lop->lo_flags & NFSLCK_WRITE) && 1723 (clp != tstp->ls_clp || 1724 (tstp->ls_flags & NFSLCK_DELEGREAD)))) { 1725 if (filestruct_locked != 0) { 1726 /* Roll back local locks. */ 1727 NFSUNLOCKSTATE(); 1728 nfsrv_locallock_rollback(vp, lfp, p); 1729 NFSLOCKSTATE(); 1730 nfsrv_unlocklf(lfp); 1731 } 1732 ret = nfsrv_delegconflict(tstp, &haslock, p, vp); 1733 if (ret) { 1734 /* 1735 * nfsrv_delegconflict unlocks state when it 1736 * returns non-zero, which it always does. 1737 */ 1738 if (other_lop) { 1739 FREE((caddr_t)other_lop, M_NFSDLOCK); 1740 other_lop = NULL; 1741 } 1742 if (ret == -1) { 1743 lckstp = NULL; 1744 goto tryagain; 1745 } 1746 return (ret); 1747 } 1748 /* Never gets here. */ 1749 } 1750 tstp = nstp; 1751 } 1752 1753 /* 1754 * Handle the unlock case by calling nfsrv_updatelock(). 1755 * (Should I have done some access checking above for unlock? For now, 1756 * just let it happen.) 1757 */ 1758 if (new_stp->ls_flags & NFSLCK_UNLOCK) { 1759 first = new_lop->lo_first; 1760 end = new_lop->lo_end; 1761 nfsrv_updatelock(stp, new_lopp, &other_lop, lfp); 1762 stateidp->seqid = ++(stp->ls_stateid.seqid); 1763 stateidp->other[0] = stp->ls_stateid.other[0]; 1764 stateidp->other[1] = stp->ls_stateid.other[1]; 1765 stateidp->other[2] = stp->ls_stateid.other[2]; 1766 if (filestruct_locked != 0) { 1767 NFSUNLOCKSTATE(); 1768 /* Update the local locks. */ 1769 nfsrv_localunlock(vp, lfp, first, end, p); 1770 NFSLOCKSTATE(); 1771 nfsrv_unlocklf(lfp); 1772 } 1773 NFSUNLOCKSTATE(); 1774 if (haslock) { 1775 NFSLOCKV4ROOTMUTEX(); 1776 nfsv4_unlock(&nfsv4rootfs_lock, 1); 1777 NFSUNLOCKV4ROOTMUTEX(); 1778 } 1779 return (0); 1780 } 1781 1782 /* 1783 * Search for a conflicting lock. A lock conflicts if: 1784 * - the lock range overlaps and 1785 * - at least one lock is a write lock and 1786 * - it is not owned by the same lock owner 1787 */ 1788 if (!delegation) { 1789 LIST_FOREACH(lop, &lfp->lf_lock, lo_lckfile) { 1790 if (new_lop->lo_end > lop->lo_first && 1791 new_lop->lo_first < lop->lo_end && 1792 (new_lop->lo_flags == NFSLCK_WRITE || 1793 lop->lo_flags == NFSLCK_WRITE) && 1794 lckstp != lop->lo_stp && 1795 (clp != lop->lo_stp->ls_clp || 1796 lckstp->ls_ownerlen != lop->lo_stp->ls_ownerlen || 1797 NFSBCMP(lckstp->ls_owner, lop->lo_stp->ls_owner, 1798 lckstp->ls_ownerlen))) { 1799 if (other_lop) { 1800 FREE((caddr_t)other_lop, M_NFSDLOCK); 1801 other_lop = NULL; 1802 } 1803 ret = nfsrv_clientconflict(lop->lo_stp->ls_clp,&haslock,vp,p); 1804 if (ret) { 1805 if (filestruct_locked != 0) { 1806 /* Roll back local locks. */ 1807 nfsrv_locallock_rollback(vp, lfp, p); 1808 NFSLOCKSTATE(); 1809 nfsrv_unlocklf(lfp); 1810 NFSUNLOCKSTATE(); 1811 } 1812 /* 1813 * nfsrv_clientconflict() unlocks state when it 1814 * returns non-zero. 1815 */ 1816 lckstp = NULL; 1817 goto tryagain; 1818 } 1819 /* 1820 * Found a conflicting lock, so record the conflict and 1821 * return the error. 1822 */ 1823 if (cfp) { 1824 cfp->cl_clientid.lval[0]=lop->lo_stp->ls_stateid.other[0]; 1825 cfp->cl_clientid.lval[1]=lop->lo_stp->ls_stateid.other[1]; 1826 cfp->cl_first = lop->lo_first; 1827 cfp->cl_end = lop->lo_end; 1828 cfp->cl_flags = lop->lo_flags; 1829 cfp->cl_ownerlen = lop->lo_stp->ls_ownerlen; 1830 NFSBCOPY(lop->lo_stp->ls_owner, cfp->cl_owner, 1831 cfp->cl_ownerlen); 1832 } 1833 if (new_stp->ls_flags & NFSLCK_RECLAIM) 1834 error = NFSERR_RECLAIMCONFLICT; 1835 else if (new_stp->ls_flags & NFSLCK_CHECK) 1836 error = NFSERR_LOCKED; 1837 else 1838 error = NFSERR_DENIED; 1839 if (filestruct_locked != 0) { 1840 /* Roll back local locks. */ 1841 NFSUNLOCKSTATE(); 1842 nfsrv_locallock_rollback(vp, lfp, p); 1843 NFSLOCKSTATE(); 1844 nfsrv_unlocklf(lfp); 1845 } 1846 NFSUNLOCKSTATE(); 1847 if (haslock) { 1848 NFSLOCKV4ROOTMUTEX(); 1849 nfsv4_unlock(&nfsv4rootfs_lock, 1); 1850 NFSUNLOCKV4ROOTMUTEX(); 1851 } 1852 return (error); 1853 } 1854 } 1855 } 1856 1857 /* 1858 * We only get here if there was no lock that conflicted. 1859 */ 1860 if (new_stp->ls_flags & (NFSLCK_TEST | NFSLCK_CHECK)) { 1861 NFSUNLOCKSTATE(); 1862 if (haslock) { 1863 NFSLOCKV4ROOTMUTEX(); 1864 nfsv4_unlock(&nfsv4rootfs_lock, 1); 1865 NFSUNLOCKV4ROOTMUTEX(); 1866 } 1867 return (0); 1868 } 1869 1870 /* 1871 * We only get here when we are creating or modifying a lock. 1872 * There are two variants: 1873 * - exist_lock_owner where lock_owner exists 1874 * - open_to_lock_owner with new lock_owner 1875 */ 1876 first = new_lop->lo_first; 1877 end = new_lop->lo_end; 1878 lock_flags = new_lop->lo_flags; 1879 if (!(new_stp->ls_flags & NFSLCK_OPENTOLOCK)) { 1880 nfsrv_updatelock(lckstp, new_lopp, &other_lop, lfp); 1881 stateidp->seqid = ++(lckstp->ls_stateid.seqid); 1882 stateidp->other[0] = lckstp->ls_stateid.other[0]; 1883 stateidp->other[1] = lckstp->ls_stateid.other[1]; 1884 stateidp->other[2] = lckstp->ls_stateid.other[2]; 1885 } else { 1886 /* 1887 * The new open_to_lock_owner case. 1888 * Link the new nfsstate into the lists. 1889 */ 1890 new_stp->ls_seq = new_stp->ls_opentolockseq; 1891 nfsrvd_refcache(new_stp->ls_op); 1892 stateidp->seqid = new_stp->ls_stateid.seqid = 0; 1893 stateidp->other[0] = new_stp->ls_stateid.other[0] = 1894 clp->lc_clientid.lval[0]; 1895 stateidp->other[1] = new_stp->ls_stateid.other[1] = 1896 clp->lc_clientid.lval[1]; 1897 stateidp->other[2] = new_stp->ls_stateid.other[2] = 1898 nfsrv_nextstateindex(clp); 1899 new_stp->ls_clp = clp; 1900 LIST_INIT(&new_stp->ls_lock); 1901 new_stp->ls_openstp = stp; 1902 new_stp->ls_lfp = lfp; 1903 nfsrv_insertlock(new_lop, (struct nfslock *)new_stp, new_stp, 1904 lfp); 1905 LIST_INSERT_HEAD(NFSSTATEHASH(clp, new_stp->ls_stateid), 1906 new_stp, ls_hash); 1907 LIST_INSERT_HEAD(&stp->ls_open, new_stp, ls_list); 1908 *new_lopp = NULL; 1909 *new_stpp = NULL; 1910 newnfsstats.srvlockowners++; 1911 nfsrv_openpluslock++; 1912 } 1913 if (filestruct_locked != 0) { 1914 NFSUNLOCKSTATE(); 1915 nfsrv_locallock_commit(lfp, lock_flags, first, end); 1916 NFSLOCKSTATE(); 1917 nfsrv_unlocklf(lfp); 1918 } 1919 NFSUNLOCKSTATE(); 1920 if (haslock) { 1921 NFSLOCKV4ROOTMUTEX(); 1922 nfsv4_unlock(&nfsv4rootfs_lock, 1); 1923 NFSUNLOCKV4ROOTMUTEX(); 1924 } 1925 if (other_lop) 1926 FREE((caddr_t)other_lop, M_NFSDLOCK); 1927 return (0); 1928 } 1929 1930 /* 1931 * Check for state errors for Open. 1932 * repstat is passed back out as an error if more critical errors 1933 * are not detected. 1934 */ 1935 APPLESTATIC int 1936 nfsrv_opencheck(nfsquad_t clientid, nfsv4stateid_t *stateidp, 1937 struct nfsstate *new_stp, vnode_t vp, struct nfsrv_descript *nd, 1938 NFSPROC_T *p, int repstat) 1939 { 1940 struct nfsstate *stp, *nstp; 1941 struct nfsclient *clp; 1942 struct nfsstate *ownerstp; 1943 struct nfslockfile *lfp, *new_lfp; 1944 int error, haslock = 0, ret, readonly = 0, getfhret = 0; 1945 1946 if ((new_stp->ls_flags & NFSLCK_SHAREBITS) == NFSLCK_READACCESS) 1947 readonly = 1; 1948 /* 1949 * Check for restart conditions (client and server). 1950 */ 1951 error = nfsrv_checkrestart(clientid, new_stp->ls_flags, 1952 &new_stp->ls_stateid, 0); 1953 if (error) 1954 return (error); 1955 1956 /* 1957 * Check for state resource limit exceeded. 1958 * Technically this should be SMP protected, but the worst 1959 * case error is "out by one or two" on the count when it 1960 * returns NFSERR_RESOURCE and the limit is just a rather 1961 * arbitrary high water mark, so no harm is done. 1962 */ 1963 if (nfsrv_openpluslock > NFSRV_V4STATELIMIT) 1964 return (NFSERR_RESOURCE); 1965 1966 tryagain: 1967 MALLOC(new_lfp, struct nfslockfile *, sizeof (struct nfslockfile), 1968 M_NFSDLOCKFILE, M_WAITOK); 1969 if (vp) 1970 getfhret = nfsrv_getlockfh(vp, new_stp->ls_flags, &new_lfp, 1971 NULL, p); 1972 NFSLOCKSTATE(); 1973 /* 1974 * Get the nfsclient structure. 1975 * Since the code is manipulating lists that are also 1976 * manipulated by nfsrv_servertimer(), soft clock interrupts 1977 * must be masked off. 1978 */ 1979 error = nfsrv_getclient(clientid, CLOPS_RENEW, &clp, 1980 (nfsquad_t)((u_quad_t)0), NULL, p); 1981 1982 /* 1983 * Look up the open owner. See if it needs confirmation and 1984 * check the seq#, as required. 1985 */ 1986 if (!error) 1987 nfsrv_getowner(&clp->lc_open, new_stp, &ownerstp); 1988 1989 if (!error && ownerstp) { 1990 error = nfsrv_checkseqid(nd, new_stp->ls_seq, ownerstp, 1991 new_stp->ls_op); 1992 /* 1993 * If the OpenOwner hasn't been confirmed, assume the 1994 * old one was a replay and this one is ok. 1995 * See: RFC3530 Sec. 14.2.18. 1996 */ 1997 if (error == NFSERR_BADSEQID && 1998 (ownerstp->ls_flags & NFSLCK_NEEDSCONFIRM)) 1999 error = 0; 2000 } 2001 2002 /* 2003 * Check for grace. 2004 */ 2005 if (!error) 2006 error = nfsrv_checkgrace(new_stp->ls_flags); 2007 if ((new_stp->ls_flags & NFSLCK_RECLAIM) && !error && 2008 nfsrv_checkstable(clp)) 2009 error = NFSERR_NOGRACE; 2010 2011 /* 2012 * If none of the above errors occurred, let repstat be 2013 * returned. 2014 */ 2015 if (repstat && !error) 2016 error = repstat; 2017 if (error) { 2018 NFSUNLOCKSTATE(); 2019 if (haslock) { 2020 NFSLOCKV4ROOTMUTEX(); 2021 nfsv4_unlock(&nfsv4rootfs_lock, 1); 2022 NFSUNLOCKV4ROOTMUTEX(); 2023 } 2024 free((caddr_t)new_lfp, M_NFSDLOCKFILE); 2025 return (error); 2026 } 2027 2028 /* 2029 * If vp == NULL, the file doesn't exist yet, so return ok. 2030 * (This always happens on the first pass, so haslock must be 0.) 2031 */ 2032 if (vp == NULL) { 2033 NFSUNLOCKSTATE(); 2034 FREE((caddr_t)new_lfp, M_NFSDLOCKFILE); 2035 return (0); 2036 } 2037 2038 /* 2039 * Get the structure for the underlying file. 2040 */ 2041 if (getfhret) 2042 error = getfhret; 2043 else 2044 error = nfsrv_getlockfile(new_stp->ls_flags, &new_lfp, &lfp, 2045 NULL, 0); 2046 if (new_lfp) 2047 FREE((caddr_t)new_lfp, M_NFSDLOCKFILE); 2048 if (error) { 2049 NFSUNLOCKSTATE(); 2050 if (haslock) { 2051 NFSLOCKV4ROOTMUTEX(); 2052 nfsv4_unlock(&nfsv4rootfs_lock, 1); 2053 NFSUNLOCKV4ROOTMUTEX(); 2054 } 2055 return (error); 2056 } 2057 2058 /* 2059 * Search for a conflicting open/share. 2060 */ 2061 if (new_stp->ls_flags & NFSLCK_DELEGCUR) { 2062 /* 2063 * For Delegate_Cur, search for the matching Delegation, 2064 * which indicates no conflict. 2065 * An old delegation should have been recovered by the 2066 * client doing a Claim_DELEGATE_Prev, so I won't let 2067 * it match and return NFSERR_EXPIRED. Should I let it 2068 * match? 2069 */ 2070 LIST_FOREACH(stp, &lfp->lf_deleg, ls_file) { 2071 if (!(stp->ls_flags & NFSLCK_OLDDELEG) && 2072 stateidp->seqid == stp->ls_stateid.seqid && 2073 !NFSBCMP(stateidp->other, stp->ls_stateid.other, 2074 NFSX_STATEIDOTHER)) 2075 break; 2076 } 2077 if (stp == LIST_END(&lfp->lf_deleg) || 2078 ((new_stp->ls_flags & NFSLCK_WRITEACCESS) && 2079 (stp->ls_flags & NFSLCK_DELEGREAD))) { 2080 NFSUNLOCKSTATE(); 2081 if (haslock) { 2082 NFSLOCKV4ROOTMUTEX(); 2083 nfsv4_unlock(&nfsv4rootfs_lock, 1); 2084 NFSUNLOCKV4ROOTMUTEX(); 2085 } 2086 return (NFSERR_EXPIRED); 2087 } 2088 } 2089 2090 /* 2091 * Check for access/deny bit conflicts. I check for the same 2092 * owner as well, in case the client didn't bother. 2093 */ 2094 LIST_FOREACH(stp, &lfp->lf_open, ls_file) { 2095 if (!(new_stp->ls_flags & NFSLCK_DELEGCUR) && 2096 (((new_stp->ls_flags & NFSLCK_ACCESSBITS) & 2097 ((stp->ls_flags>>NFSLCK_SHIFT) & NFSLCK_ACCESSBITS))|| 2098 ((stp->ls_flags & NFSLCK_ACCESSBITS) & 2099 ((new_stp->ls_flags>>NFSLCK_SHIFT)&NFSLCK_ACCESSBITS)))){ 2100 ret = nfsrv_clientconflict(stp->ls_clp,&haslock,vp,p); 2101 if (ret) { 2102 /* 2103 * nfsrv_clientconflict() unlocks 2104 * state when it returns non-zero. 2105 */ 2106 goto tryagain; 2107 } 2108 if (new_stp->ls_flags & NFSLCK_RECLAIM) 2109 error = NFSERR_RECLAIMCONFLICT; 2110 else 2111 error = NFSERR_SHAREDENIED; 2112 NFSUNLOCKSTATE(); 2113 if (haslock) { 2114 NFSLOCKV4ROOTMUTEX(); 2115 nfsv4_unlock(&nfsv4rootfs_lock, 1); 2116 NFSUNLOCKV4ROOTMUTEX(); 2117 } 2118 return (error); 2119 } 2120 } 2121 2122 /* 2123 * Check for a conflicting delegation. If one is found, call 2124 * nfsrv_delegconflict() to handle it. If the v4root lock hasn't 2125 * been set yet, it will get the lock. Otherwise, it will recall 2126 * the delegation. Then, we try try again... 2127 * (If NFSLCK_DELEGCUR is set, it has a delegation, so there 2128 * isn't a conflict.) 2129 * I currently believe the conflict algorithm to be: 2130 * For Open with Read Access and Deny None 2131 * - there is a conflict iff a different client has a write delegation 2132 * For Open with other Write Access or any Deny except None 2133 * - there is a conflict if a different client has any delegation 2134 * - there is a conflict if the same client has a read delegation 2135 * (The current concensus is that this last case should be 2136 * considered a conflict since the client with a read delegation 2137 * could have done an Open with ReadAccess and WriteDeny 2138 * locally and then not have checked for the WriteDeny.) 2139 * Don't check for a Reclaim, since that will be dealt with 2140 * by nfsrv_openctrl(). 2141 */ 2142 if (!(new_stp->ls_flags & 2143 (NFSLCK_DELEGPREV | NFSLCK_DELEGCUR | NFSLCK_RECLAIM))) { 2144 stp = LIST_FIRST(&lfp->lf_deleg); 2145 while (stp != LIST_END(&lfp->lf_deleg)) { 2146 nstp = LIST_NEXT(stp, ls_file); 2147 if ((readonly && stp->ls_clp != clp && 2148 (stp->ls_flags & NFSLCK_DELEGWRITE)) || 2149 (!readonly && (stp->ls_clp != clp || 2150 (stp->ls_flags & NFSLCK_DELEGREAD)))) { 2151 ret = nfsrv_delegconflict(stp, &haslock, p, vp); 2152 if (ret) { 2153 /* 2154 * nfsrv_delegconflict() unlocks state 2155 * when it returns non-zero. 2156 */ 2157 if (ret == -1) 2158 goto tryagain; 2159 return (ret); 2160 } 2161 } 2162 stp = nstp; 2163 } 2164 } 2165 NFSUNLOCKSTATE(); 2166 if (haslock) { 2167 NFSLOCKV4ROOTMUTEX(); 2168 nfsv4_unlock(&nfsv4rootfs_lock, 1); 2169 NFSUNLOCKV4ROOTMUTEX(); 2170 } 2171 return (0); 2172 } 2173 2174 /* 2175 * Open control function to create/update open state for an open. 2176 */ 2177 APPLESTATIC int 2178 nfsrv_openctrl(struct nfsrv_descript *nd, vnode_t vp, 2179 struct nfsstate **new_stpp, nfsquad_t clientid, nfsv4stateid_t *stateidp, 2180 nfsv4stateid_t *delegstateidp, u_int32_t *rflagsp, struct nfsexstuff *exp, 2181 NFSPROC_T *p, u_quad_t filerev) 2182 { 2183 struct nfsstate *new_stp = *new_stpp; 2184 struct nfsstate *stp, *nstp; 2185 struct nfsstate *openstp = NULL, *new_open, *ownerstp, *new_deleg; 2186 struct nfslockfile *lfp, *new_lfp; 2187 struct nfsclient *clp; 2188 int error, haslock = 0, ret, delegate = 1, writedeleg = 1; 2189 int readonly = 0, cbret = 1, getfhret = 0; 2190 2191 if ((new_stp->ls_flags & NFSLCK_SHAREBITS) == NFSLCK_READACCESS) 2192 readonly = 1; 2193 /* 2194 * Check for restart conditions (client and server). 2195 * (Paranoia, should have been detected by nfsrv_opencheck().) 2196 * If an error does show up, return NFSERR_EXPIRED, since the 2197 * the seqid# has already been incremented. 2198 */ 2199 error = nfsrv_checkrestart(clientid, new_stp->ls_flags, 2200 &new_stp->ls_stateid, 0); 2201 if (error) { 2202 printf("Nfsd: openctrl unexpected restart err=%d\n", 2203 error); 2204 return (NFSERR_EXPIRED); 2205 } 2206 2207 tryagain: 2208 MALLOC(new_lfp, struct nfslockfile *, sizeof (struct nfslockfile), 2209 M_NFSDLOCKFILE, M_WAITOK); 2210 MALLOC(new_open, struct nfsstate *, sizeof (struct nfsstate), 2211 M_NFSDSTATE, M_WAITOK); 2212 MALLOC(new_deleg, struct nfsstate *, sizeof (struct nfsstate), 2213 M_NFSDSTATE, M_WAITOK); 2214 getfhret = nfsrv_getlockfh(vp, new_stp->ls_flags, &new_lfp, 2215 NULL, p); 2216 NFSLOCKSTATE(); 2217 /* 2218 * Get the client structure. Since the linked lists could be changed 2219 * by other nfsd processes if this process does a tsleep(), one of 2220 * two things must be done. 2221 * 1 - don't tsleep() 2222 * or 2223 * 2 - get the nfsv4_lock() { indicated by haslock == 1 } 2224 * before using the lists, since this lock stops the other 2225 * nfsd. This should only be used for rare cases, since it 2226 * essentially single threads the nfsd. 2227 * At this time, it is only done for cases where the stable 2228 * storage file must be written prior to completion of state 2229 * expiration. 2230 */ 2231 error = nfsrv_getclient(clientid, CLOPS_RENEW, &clp, 2232 (nfsquad_t)((u_quad_t)0), NULL, p); 2233 if (!error && (clp->lc_flags & LCL_NEEDSCBNULL) && 2234 clp->lc_program) { 2235 /* 2236 * This happens on the first open for a client 2237 * that supports callbacks. 2238 */ 2239 NFSUNLOCKSTATE(); 2240 /* 2241 * Although nfsrv_docallback() will sleep, clp won't 2242 * go away, since they are only removed when the 2243 * nfsv4_lock() has blocked the nfsd threads. The 2244 * fields in clp can change, but having multiple 2245 * threads do this Null callback RPC should be 2246 * harmless. 2247 */ 2248 cbret = nfsrv_docallback(clp, NFSV4PROC_CBNULL, 2249 NULL, 0, NULL, NULL, NULL, p); 2250 NFSLOCKSTATE(); 2251 clp->lc_flags &= ~LCL_NEEDSCBNULL; 2252 if (!cbret) 2253 clp->lc_flags |= LCL_CALLBACKSON; 2254 } 2255 2256 /* 2257 * Look up the open owner. See if it needs confirmation and 2258 * check the seq#, as required. 2259 */ 2260 if (!error) 2261 nfsrv_getowner(&clp->lc_open, new_stp, &ownerstp); 2262 2263 if (error) { 2264 NFSUNLOCKSTATE(); 2265 printf("Nfsd: openctrl unexpected state err=%d\n", 2266 error); 2267 free((caddr_t)new_lfp, M_NFSDLOCKFILE); 2268 free((caddr_t)new_open, M_NFSDSTATE); 2269 free((caddr_t)new_deleg, M_NFSDSTATE); 2270 if (haslock) { 2271 NFSLOCKV4ROOTMUTEX(); 2272 nfsv4_unlock(&nfsv4rootfs_lock, 1); 2273 NFSUNLOCKV4ROOTMUTEX(); 2274 } 2275 return (NFSERR_EXPIRED); 2276 } 2277 2278 if (new_stp->ls_flags & NFSLCK_RECLAIM) 2279 nfsrv_markstable(clp); 2280 2281 /* 2282 * Get the structure for the underlying file. 2283 */ 2284 if (getfhret) 2285 error = getfhret; 2286 else 2287 error = nfsrv_getlockfile(new_stp->ls_flags, &new_lfp, &lfp, 2288 NULL, 0); 2289 if (new_lfp) 2290 FREE((caddr_t)new_lfp, M_NFSDLOCKFILE); 2291 if (error) { 2292 NFSUNLOCKSTATE(); 2293 printf("Nfsd openctrl unexpected getlockfile err=%d\n", 2294 error); 2295 free((caddr_t)new_open, M_NFSDSTATE); 2296 free((caddr_t)new_deleg, M_NFSDSTATE); 2297 if (haslock) { 2298 NFSLOCKV4ROOTMUTEX(); 2299 nfsv4_unlock(&nfsv4rootfs_lock, 1); 2300 NFSUNLOCKV4ROOTMUTEX(); 2301 } 2302 return (error); 2303 } 2304 2305 /* 2306 * Search for a conflicting open/share. 2307 */ 2308 if (new_stp->ls_flags & NFSLCK_DELEGCUR) { 2309 /* 2310 * For Delegate_Cur, search for the matching Delegation, 2311 * which indicates no conflict. 2312 * An old delegation should have been recovered by the 2313 * client doing a Claim_DELEGATE_Prev, so I won't let 2314 * it match and return NFSERR_EXPIRED. Should I let it 2315 * match? 2316 */ 2317 LIST_FOREACH(stp, &lfp->lf_deleg, ls_file) { 2318 if (!(stp->ls_flags & NFSLCK_OLDDELEG) && 2319 stateidp->seqid == stp->ls_stateid.seqid && 2320 !NFSBCMP(stateidp->other, stp->ls_stateid.other, 2321 NFSX_STATEIDOTHER)) 2322 break; 2323 } 2324 if (stp == LIST_END(&lfp->lf_deleg) || 2325 ((new_stp->ls_flags & NFSLCK_WRITEACCESS) && 2326 (stp->ls_flags & NFSLCK_DELEGREAD))) { 2327 NFSUNLOCKSTATE(); 2328 printf("Nfsd openctrl unexpected expiry\n"); 2329 free((caddr_t)new_open, M_NFSDSTATE); 2330 free((caddr_t)new_deleg, M_NFSDSTATE); 2331 if (haslock) { 2332 NFSLOCKV4ROOTMUTEX(); 2333 nfsv4_unlock(&nfsv4rootfs_lock, 1); 2334 NFSUNLOCKV4ROOTMUTEX(); 2335 } 2336 return (NFSERR_EXPIRED); 2337 } 2338 2339 /* 2340 * Don't issue a Delegation, since one already exists and 2341 * delay delegation timeout, as required. 2342 */ 2343 delegate = 0; 2344 nfsrv_delaydelegtimeout(stp); 2345 } 2346 2347 /* 2348 * Check for access/deny bit conflicts. I also check for the 2349 * same owner, since the client might not have bothered to check. 2350 * Also, note an open for the same file and owner, if found, 2351 * which is all we do here for Delegate_Cur, since conflict 2352 * checking is already done. 2353 */ 2354 LIST_FOREACH(stp, &lfp->lf_open, ls_file) { 2355 if (ownerstp && stp->ls_openowner == ownerstp) 2356 openstp = stp; 2357 if (!(new_stp->ls_flags & NFSLCK_DELEGCUR)) { 2358 /* 2359 * If another client has the file open, the only 2360 * delegation that can be issued is a Read delegation 2361 * and only if it is a Read open with Deny none. 2362 */ 2363 if (clp != stp->ls_clp) { 2364 if ((stp->ls_flags & NFSLCK_SHAREBITS) == 2365 NFSLCK_READACCESS) 2366 writedeleg = 0; 2367 else 2368 delegate = 0; 2369 } 2370 if(((new_stp->ls_flags & NFSLCK_ACCESSBITS) & 2371 ((stp->ls_flags>>NFSLCK_SHIFT) & NFSLCK_ACCESSBITS))|| 2372 ((stp->ls_flags & NFSLCK_ACCESSBITS) & 2373 ((new_stp->ls_flags>>NFSLCK_SHIFT)&NFSLCK_ACCESSBITS))){ 2374 ret = nfsrv_clientconflict(stp->ls_clp,&haslock,vp,p); 2375 if (ret) { 2376 /* 2377 * nfsrv_clientconflict() unlocks state 2378 * when it returns non-zero. 2379 */ 2380 free((caddr_t)new_open, M_NFSDSTATE); 2381 free((caddr_t)new_deleg, M_NFSDSTATE); 2382 openstp = NULL; 2383 goto tryagain; 2384 } 2385 if (new_stp->ls_flags & NFSLCK_RECLAIM) 2386 error = NFSERR_RECLAIMCONFLICT; 2387 else 2388 error = NFSERR_SHAREDENIED; 2389 NFSUNLOCKSTATE(); 2390 if (haslock) { 2391 NFSLOCKV4ROOTMUTEX(); 2392 nfsv4_unlock(&nfsv4rootfs_lock, 1); 2393 NFSUNLOCKV4ROOTMUTEX(); 2394 } 2395 free((caddr_t)new_open, M_NFSDSTATE); 2396 free((caddr_t)new_deleg, M_NFSDSTATE); 2397 printf("nfsd openctrl unexpected client cnfl\n"); 2398 return (error); 2399 } 2400 } 2401 } 2402 2403 /* 2404 * Check for a conflicting delegation. If one is found, call 2405 * nfsrv_delegconflict() to handle it. If the v4root lock hasn't 2406 * been set yet, it will get the lock. Otherwise, it will recall 2407 * the delegation. Then, we try try again... 2408 * (If NFSLCK_DELEGCUR is set, it has a delegation, so there 2409 * isn't a conflict.) 2410 * I currently believe the conflict algorithm to be: 2411 * For Open with Read Access and Deny None 2412 * - there is a conflict iff a different client has a write delegation 2413 * For Open with other Write Access or any Deny except None 2414 * - there is a conflict if a different client has any delegation 2415 * - there is a conflict if the same client has a read delegation 2416 * (The current concensus is that this last case should be 2417 * considered a conflict since the client with a read delegation 2418 * could have done an Open with ReadAccess and WriteDeny 2419 * locally and then not have checked for the WriteDeny.) 2420 */ 2421 if (!(new_stp->ls_flags & (NFSLCK_DELEGPREV | NFSLCK_DELEGCUR))) { 2422 stp = LIST_FIRST(&lfp->lf_deleg); 2423 while (stp != LIST_END(&lfp->lf_deleg)) { 2424 nstp = LIST_NEXT(stp, ls_file); 2425 if (stp->ls_clp != clp && (stp->ls_flags & NFSLCK_DELEGREAD)) 2426 writedeleg = 0; 2427 else 2428 delegate = 0; 2429 if ((readonly && stp->ls_clp != clp && 2430 (stp->ls_flags & NFSLCK_DELEGWRITE)) || 2431 (!readonly && (stp->ls_clp != clp || 2432 (stp->ls_flags & NFSLCK_DELEGREAD)))) { 2433 if (new_stp->ls_flags & NFSLCK_RECLAIM) { 2434 delegate = 2; 2435 } else { 2436 ret = nfsrv_delegconflict(stp, &haslock, p, vp); 2437 if (ret) { 2438 /* 2439 * nfsrv_delegconflict() unlocks state 2440 * when it returns non-zero. 2441 */ 2442 printf("Nfsd openctrl unexpected deleg cnfl\n"); 2443 free((caddr_t)new_open, M_NFSDSTATE); 2444 free((caddr_t)new_deleg, M_NFSDSTATE); 2445 if (ret == -1) { 2446 openstp = NULL; 2447 goto tryagain; 2448 } 2449 return (ret); 2450 } 2451 } 2452 } 2453 stp = nstp; 2454 } 2455 } 2456 2457 /* 2458 * We only get here if there was no open that conflicted. 2459 * If an open for the owner exists, or in the access/deny bits. 2460 * Otherwise it is a new open. If the open_owner hasn't been 2461 * confirmed, replace the open with the new one needing confirmation, 2462 * otherwise add the open. 2463 */ 2464 if (new_stp->ls_flags & NFSLCK_DELEGPREV) { 2465 /* 2466 * Handle NFSLCK_DELEGPREV by searching the old delegations for 2467 * a match. If found, just move the old delegation to the current 2468 * delegation list and issue open. If not found, return 2469 * NFSERR_EXPIRED. 2470 */ 2471 LIST_FOREACH(stp, &clp->lc_olddeleg, ls_list) { 2472 if (stp->ls_lfp == lfp) { 2473 /* Found it */ 2474 if (stp->ls_clp != clp) 2475 panic("olddeleg clp"); 2476 LIST_REMOVE(stp, ls_list); 2477 LIST_REMOVE(stp, ls_hash); 2478 stp->ls_flags &= ~NFSLCK_OLDDELEG; 2479 stp->ls_stateid.seqid = delegstateidp->seqid = 0; 2480 stp->ls_stateid.other[0] = delegstateidp->other[0] = 2481 clp->lc_clientid.lval[0]; 2482 stp->ls_stateid.other[1] = delegstateidp->other[1] = 2483 clp->lc_clientid.lval[1]; 2484 stp->ls_stateid.other[2] = delegstateidp->other[2] = 2485 nfsrv_nextstateindex(clp); 2486 stp->ls_compref = nd->nd_compref; 2487 LIST_INSERT_HEAD(&clp->lc_deleg, stp, ls_list); 2488 LIST_INSERT_HEAD(NFSSTATEHASH(clp, 2489 stp->ls_stateid), stp, ls_hash); 2490 if (stp->ls_flags & NFSLCK_DELEGWRITE) 2491 *rflagsp |= NFSV4OPEN_WRITEDELEGATE; 2492 else 2493 *rflagsp |= NFSV4OPEN_READDELEGATE; 2494 clp->lc_delegtime = NFSD_MONOSEC + 2495 nfsrv_lease + NFSRV_LEASEDELTA; 2496 2497 /* 2498 * Now, do the associated open. 2499 */ 2500 new_open->ls_stateid.seqid = 0; 2501 new_open->ls_stateid.other[0] = clp->lc_clientid.lval[0]; 2502 new_open->ls_stateid.other[1] = clp->lc_clientid.lval[1]; 2503 new_open->ls_stateid.other[2] = nfsrv_nextstateindex(clp); 2504 new_open->ls_flags = (new_stp->ls_flags&NFSLCK_DENYBITS)| 2505 NFSLCK_OPEN; 2506 if (stp->ls_flags & NFSLCK_DELEGWRITE) 2507 new_open->ls_flags |= (NFSLCK_READACCESS | 2508 NFSLCK_WRITEACCESS); 2509 else 2510 new_open->ls_flags |= NFSLCK_READACCESS; 2511 new_open->ls_uid = new_stp->ls_uid; 2512 new_open->ls_lfp = lfp; 2513 new_open->ls_clp = clp; 2514 LIST_INIT(&new_open->ls_open); 2515 LIST_INSERT_HEAD(&lfp->lf_open, new_open, ls_file); 2516 LIST_INSERT_HEAD(NFSSTATEHASH(clp, new_open->ls_stateid), 2517 new_open, ls_hash); 2518 /* 2519 * and handle the open owner 2520 */ 2521 if (ownerstp) { 2522 new_open->ls_openowner = ownerstp; 2523 LIST_INSERT_HEAD(&ownerstp->ls_open,new_open,ls_list); 2524 } else { 2525 new_open->ls_openowner = new_stp; 2526 new_stp->ls_flags = 0; 2527 nfsrvd_refcache(new_stp->ls_op); 2528 new_stp->ls_noopens = 0; 2529 LIST_INIT(&new_stp->ls_open); 2530 LIST_INSERT_HEAD(&new_stp->ls_open, new_open, ls_list); 2531 LIST_INSERT_HEAD(&clp->lc_open, new_stp, ls_list); 2532 *new_stpp = NULL; 2533 newnfsstats.srvopenowners++; 2534 nfsrv_openpluslock++; 2535 } 2536 openstp = new_open; 2537 new_open = NULL; 2538 newnfsstats.srvopens++; 2539 nfsrv_openpluslock++; 2540 break; 2541 } 2542 } 2543 if (stp == LIST_END(&clp->lc_olddeleg)) 2544 error = NFSERR_EXPIRED; 2545 } else if (new_stp->ls_flags & (NFSLCK_DELEGREAD | NFSLCK_DELEGWRITE)) { 2546 /* 2547 * Scan to see that no delegation for this client and file 2548 * doesn't already exist. 2549 * There also shouldn't yet be an Open for this file and 2550 * openowner. 2551 */ 2552 LIST_FOREACH(stp, &lfp->lf_deleg, ls_file) { 2553 if (stp->ls_clp == clp) 2554 break; 2555 } 2556 if (stp == LIST_END(&lfp->lf_deleg) && openstp == NULL) { 2557 /* 2558 * This is the Claim_Previous case with a delegation 2559 * type != Delegate_None. 2560 */ 2561 /* 2562 * First, add the delegation. (Although we must issue the 2563 * delegation, we can also ask for an immediate return.) 2564 */ 2565 new_deleg->ls_stateid.seqid = delegstateidp->seqid = 0; 2566 new_deleg->ls_stateid.other[0] = delegstateidp->other[0] = 2567 clp->lc_clientid.lval[0]; 2568 new_deleg->ls_stateid.other[1] = delegstateidp->other[1] = 2569 clp->lc_clientid.lval[1]; 2570 new_deleg->ls_stateid.other[2] = delegstateidp->other[2] = 2571 nfsrv_nextstateindex(clp); 2572 if (new_stp->ls_flags & NFSLCK_DELEGWRITE) { 2573 new_deleg->ls_flags = (NFSLCK_DELEGWRITE | 2574 NFSLCK_READACCESS | NFSLCK_WRITEACCESS); 2575 *rflagsp |= NFSV4OPEN_WRITEDELEGATE; 2576 } else { 2577 new_deleg->ls_flags = (NFSLCK_DELEGREAD | 2578 NFSLCK_READACCESS); 2579 *rflagsp |= NFSV4OPEN_READDELEGATE; 2580 } 2581 new_deleg->ls_uid = new_stp->ls_uid; 2582 new_deleg->ls_lfp = lfp; 2583 new_deleg->ls_clp = clp; 2584 new_deleg->ls_filerev = filerev; 2585 new_deleg->ls_compref = nd->nd_compref; 2586 LIST_INSERT_HEAD(&lfp->lf_deleg, new_deleg, ls_file); 2587 LIST_INSERT_HEAD(NFSSTATEHASH(clp, 2588 new_deleg->ls_stateid), new_deleg, ls_hash); 2589 LIST_INSERT_HEAD(&clp->lc_deleg, new_deleg, ls_list); 2590 new_deleg = NULL; 2591 if (delegate == 2 || nfsrv_issuedelegs == 0 || 2592 (clp->lc_flags & (LCL_CALLBACKSON | LCL_CBDOWN)) != 2593 LCL_CALLBACKSON || 2594 NFSRV_V4DELEGLIMIT(nfsrv_delegatecnt) || 2595 !NFSVNO_DELEGOK(vp)) 2596 *rflagsp |= NFSV4OPEN_RECALL; 2597 newnfsstats.srvdelegates++; 2598 nfsrv_openpluslock++; 2599 nfsrv_delegatecnt++; 2600 2601 /* 2602 * Now, do the associated open. 2603 */ 2604 new_open->ls_stateid.seqid = 0; 2605 new_open->ls_stateid.other[0] = clp->lc_clientid.lval[0]; 2606 new_open->ls_stateid.other[1] = clp->lc_clientid.lval[1]; 2607 new_open->ls_stateid.other[2] = nfsrv_nextstateindex(clp); 2608 new_open->ls_flags = (new_stp->ls_flags & NFSLCK_DENYBITS) | 2609 NFSLCK_OPEN; 2610 if (new_stp->ls_flags & NFSLCK_DELEGWRITE) 2611 new_open->ls_flags |= (NFSLCK_READACCESS | 2612 NFSLCK_WRITEACCESS); 2613 else 2614 new_open->ls_flags |= NFSLCK_READACCESS; 2615 new_open->ls_uid = new_stp->ls_uid; 2616 new_open->ls_lfp = lfp; 2617 new_open->ls_clp = clp; 2618 LIST_INIT(&new_open->ls_open); 2619 LIST_INSERT_HEAD(&lfp->lf_open, new_open, ls_file); 2620 LIST_INSERT_HEAD(NFSSTATEHASH(clp, new_open->ls_stateid), 2621 new_open, ls_hash); 2622 /* 2623 * and handle the open owner 2624 */ 2625 if (ownerstp) { 2626 new_open->ls_openowner = ownerstp; 2627 LIST_INSERT_HEAD(&ownerstp->ls_open, new_open, ls_list); 2628 } else { 2629 new_open->ls_openowner = new_stp; 2630 new_stp->ls_flags = 0; 2631 nfsrvd_refcache(new_stp->ls_op); 2632 new_stp->ls_noopens = 0; 2633 LIST_INIT(&new_stp->ls_open); 2634 LIST_INSERT_HEAD(&new_stp->ls_open, new_open, ls_list); 2635 LIST_INSERT_HEAD(&clp->lc_open, new_stp, ls_list); 2636 *new_stpp = NULL; 2637 newnfsstats.srvopenowners++; 2638 nfsrv_openpluslock++; 2639 } 2640 openstp = new_open; 2641 new_open = NULL; 2642 newnfsstats.srvopens++; 2643 nfsrv_openpluslock++; 2644 } else { 2645 error = NFSERR_RECLAIMCONFLICT; 2646 } 2647 } else if (ownerstp) { 2648 if (ownerstp->ls_flags & NFSLCK_NEEDSCONFIRM) { 2649 /* Replace the open */ 2650 if (ownerstp->ls_op) 2651 nfsrvd_derefcache(ownerstp->ls_op); 2652 ownerstp->ls_op = new_stp->ls_op; 2653 nfsrvd_refcache(ownerstp->ls_op); 2654 ownerstp->ls_seq = new_stp->ls_seq; 2655 *rflagsp |= NFSV4OPEN_RESULTCONFIRM; 2656 stp = LIST_FIRST(&ownerstp->ls_open); 2657 stp->ls_flags = (new_stp->ls_flags & NFSLCK_SHAREBITS) | 2658 NFSLCK_OPEN; 2659 stp->ls_stateid.seqid = 0; 2660 stp->ls_uid = new_stp->ls_uid; 2661 if (lfp != stp->ls_lfp) { 2662 LIST_REMOVE(stp, ls_file); 2663 LIST_INSERT_HEAD(&lfp->lf_open, stp, ls_file); 2664 stp->ls_lfp = lfp; 2665 } 2666 openstp = stp; 2667 } else if (openstp) { 2668 openstp->ls_flags |= (new_stp->ls_flags & NFSLCK_SHAREBITS); 2669 openstp->ls_stateid.seqid++; 2670 2671 /* 2672 * This is where we can choose to issue a delegation. 2673 */ 2674 if (delegate && nfsrv_issuedelegs && 2675 writedeleg && !NFSVNO_EXRDONLY(exp) && 2676 (nfsrv_writedelegifpos || !readonly) && 2677 (clp->lc_flags & (LCL_CALLBACKSON | LCL_CBDOWN)) == 2678 LCL_CALLBACKSON && 2679 !NFSRV_V4DELEGLIMIT(nfsrv_delegatecnt) && 2680 NFSVNO_DELEGOK(vp)) { 2681 new_deleg->ls_stateid.seqid = delegstateidp->seqid = 0; 2682 new_deleg->ls_stateid.other[0] = delegstateidp->other[0] 2683 = clp->lc_clientid.lval[0]; 2684 new_deleg->ls_stateid.other[1] = delegstateidp->other[1] 2685 = clp->lc_clientid.lval[1]; 2686 new_deleg->ls_stateid.other[2] = delegstateidp->other[2] 2687 = nfsrv_nextstateindex(clp); 2688 new_deleg->ls_flags = (NFSLCK_DELEGWRITE | 2689 NFSLCK_READACCESS | NFSLCK_WRITEACCESS); 2690 *rflagsp |= NFSV4OPEN_WRITEDELEGATE; 2691 new_deleg->ls_uid = new_stp->ls_uid; 2692 new_deleg->ls_lfp = lfp; 2693 new_deleg->ls_clp = clp; 2694 new_deleg->ls_filerev = filerev; 2695 new_deleg->ls_compref = nd->nd_compref; 2696 LIST_INSERT_HEAD(&lfp->lf_deleg, new_deleg, ls_file); 2697 LIST_INSERT_HEAD(NFSSTATEHASH(clp, 2698 new_deleg->ls_stateid), new_deleg, ls_hash); 2699 LIST_INSERT_HEAD(&clp->lc_deleg, new_deleg, ls_list); 2700 new_deleg = NULL; 2701 newnfsstats.srvdelegates++; 2702 nfsrv_openpluslock++; 2703 nfsrv_delegatecnt++; 2704 } 2705 } else { 2706 new_open->ls_stateid.seqid = 0; 2707 new_open->ls_stateid.other[0] = clp->lc_clientid.lval[0]; 2708 new_open->ls_stateid.other[1] = clp->lc_clientid.lval[1]; 2709 new_open->ls_stateid.other[2] = nfsrv_nextstateindex(clp); 2710 new_open->ls_flags = (new_stp->ls_flags & NFSLCK_SHAREBITS)| 2711 NFSLCK_OPEN; 2712 new_open->ls_uid = new_stp->ls_uid; 2713 new_open->ls_openowner = ownerstp; 2714 new_open->ls_lfp = lfp; 2715 new_open->ls_clp = clp; 2716 LIST_INIT(&new_open->ls_open); 2717 LIST_INSERT_HEAD(&lfp->lf_open, new_open, ls_file); 2718 LIST_INSERT_HEAD(&ownerstp->ls_open, new_open, ls_list); 2719 LIST_INSERT_HEAD(NFSSTATEHASH(clp, new_open->ls_stateid), 2720 new_open, ls_hash); 2721 openstp = new_open; 2722 new_open = NULL; 2723 newnfsstats.srvopens++; 2724 nfsrv_openpluslock++; 2725 2726 /* 2727 * This is where we can choose to issue a delegation. 2728 */ 2729 if (delegate && nfsrv_issuedelegs && 2730 (writedeleg || readonly) && 2731 (clp->lc_flags & (LCL_CALLBACKSON | LCL_CBDOWN)) == 2732 LCL_CALLBACKSON && 2733 !NFSRV_V4DELEGLIMIT(nfsrv_delegatecnt) && 2734 NFSVNO_DELEGOK(vp)) { 2735 new_deleg->ls_stateid.seqid = delegstateidp->seqid = 0; 2736 new_deleg->ls_stateid.other[0] = delegstateidp->other[0] 2737 = clp->lc_clientid.lval[0]; 2738 new_deleg->ls_stateid.other[1] = delegstateidp->other[1] 2739 = clp->lc_clientid.lval[1]; 2740 new_deleg->ls_stateid.other[2] = delegstateidp->other[2] 2741 = nfsrv_nextstateindex(clp); 2742 if (writedeleg && !NFSVNO_EXRDONLY(exp) && 2743 (nfsrv_writedelegifpos || !readonly)) { 2744 new_deleg->ls_flags = (NFSLCK_DELEGWRITE | 2745 NFSLCK_READACCESS | NFSLCK_WRITEACCESS); 2746 *rflagsp |= NFSV4OPEN_WRITEDELEGATE; 2747 } else { 2748 new_deleg->ls_flags = (NFSLCK_DELEGREAD | 2749 NFSLCK_READACCESS); 2750 *rflagsp |= NFSV4OPEN_READDELEGATE; 2751 } 2752 new_deleg->ls_uid = new_stp->ls_uid; 2753 new_deleg->ls_lfp = lfp; 2754 new_deleg->ls_clp = clp; 2755 new_deleg->ls_filerev = filerev; 2756 new_deleg->ls_compref = nd->nd_compref; 2757 LIST_INSERT_HEAD(&lfp->lf_deleg, new_deleg, ls_file); 2758 LIST_INSERT_HEAD(NFSSTATEHASH(clp, 2759 new_deleg->ls_stateid), new_deleg, ls_hash); 2760 LIST_INSERT_HEAD(&clp->lc_deleg, new_deleg, ls_list); 2761 new_deleg = NULL; 2762 newnfsstats.srvdelegates++; 2763 nfsrv_openpluslock++; 2764 nfsrv_delegatecnt++; 2765 } 2766 } 2767 } else { 2768 /* 2769 * New owner case. Start the open_owner sequence with a 2770 * Needs confirmation (unless a reclaim) and hang the 2771 * new open off it. 2772 */ 2773 new_open->ls_stateid.seqid = 0; 2774 new_open->ls_stateid.other[0] = clp->lc_clientid.lval[0]; 2775 new_open->ls_stateid.other[1] = clp->lc_clientid.lval[1]; 2776 new_open->ls_stateid.other[2] = nfsrv_nextstateindex(clp); 2777 new_open->ls_flags = (new_stp->ls_flags & NFSLCK_SHAREBITS) | 2778 NFSLCK_OPEN; 2779 new_open->ls_uid = new_stp->ls_uid; 2780 LIST_INIT(&new_open->ls_open); 2781 new_open->ls_openowner = new_stp; 2782 new_open->ls_lfp = lfp; 2783 new_open->ls_clp = clp; 2784 LIST_INSERT_HEAD(&lfp->lf_open, new_open, ls_file); 2785 if (new_stp->ls_flags & NFSLCK_RECLAIM) { 2786 new_stp->ls_flags = 0; 2787 } else { 2788 *rflagsp |= NFSV4OPEN_RESULTCONFIRM; 2789 new_stp->ls_flags = NFSLCK_NEEDSCONFIRM; 2790 } 2791 nfsrvd_refcache(new_stp->ls_op); 2792 new_stp->ls_noopens = 0; 2793 LIST_INIT(&new_stp->ls_open); 2794 LIST_INSERT_HEAD(&new_stp->ls_open, new_open, ls_list); 2795 LIST_INSERT_HEAD(&clp->lc_open, new_stp, ls_list); 2796 LIST_INSERT_HEAD(NFSSTATEHASH(clp, new_open->ls_stateid), 2797 new_open, ls_hash); 2798 openstp = new_open; 2799 new_open = NULL; 2800 *new_stpp = NULL; 2801 newnfsstats.srvopens++; 2802 nfsrv_openpluslock++; 2803 newnfsstats.srvopenowners++; 2804 nfsrv_openpluslock++; 2805 } 2806 if (!error) { 2807 stateidp->seqid = openstp->ls_stateid.seqid; 2808 stateidp->other[0] = openstp->ls_stateid.other[0]; 2809 stateidp->other[1] = openstp->ls_stateid.other[1]; 2810 stateidp->other[2] = openstp->ls_stateid.other[2]; 2811 } 2812 NFSUNLOCKSTATE(); 2813 if (haslock) { 2814 NFSLOCKV4ROOTMUTEX(); 2815 nfsv4_unlock(&nfsv4rootfs_lock, 1); 2816 NFSUNLOCKV4ROOTMUTEX(); 2817 } 2818 if (new_open) 2819 FREE((caddr_t)new_open, M_NFSDSTATE); 2820 if (new_deleg) 2821 FREE((caddr_t)new_deleg, M_NFSDSTATE); 2822 return (error); 2823 } 2824 2825 /* 2826 * Open update. Does the confirm, downgrade and close. 2827 */ 2828 APPLESTATIC int 2829 nfsrv_openupdate(vnode_t vp, struct nfsstate *new_stp, nfsquad_t clientid, 2830 nfsv4stateid_t *stateidp, struct nfsrv_descript *nd, NFSPROC_T *p) 2831 { 2832 struct nfsstate *stp, *ownerstp; 2833 struct nfsclient *clp; 2834 struct nfslockfile *lfp; 2835 u_int32_t bits; 2836 int error, gotstate = 0, len = 0; 2837 u_char client[NFSV4_OPAQUELIMIT]; 2838 2839 /* 2840 * Check for restart conditions (client and server). 2841 */ 2842 error = nfsrv_checkrestart(clientid, new_stp->ls_flags, 2843 &new_stp->ls_stateid, 0); 2844 if (error) 2845 return (error); 2846 2847 NFSLOCKSTATE(); 2848 /* 2849 * Get the open structure via clientid and stateid. 2850 */ 2851 error = nfsrv_getclient(clientid, CLOPS_RENEW, &clp, 2852 (nfsquad_t)((u_quad_t)0), NULL, p); 2853 if (!error) 2854 error = nfsrv_getstate(clp, &new_stp->ls_stateid, 2855 new_stp->ls_flags, &stp); 2856 2857 /* 2858 * Sanity check the open. 2859 */ 2860 if (!error && (!(stp->ls_flags & NFSLCK_OPEN) || 2861 (!(new_stp->ls_flags & NFSLCK_CONFIRM) && 2862 (stp->ls_openowner->ls_flags & NFSLCK_NEEDSCONFIRM)) || 2863 ((new_stp->ls_flags & NFSLCK_CONFIRM) && 2864 (!(stp->ls_openowner->ls_flags & NFSLCK_NEEDSCONFIRM))))) 2865 error = NFSERR_BADSTATEID; 2866 2867 if (!error) 2868 error = nfsrv_checkseqid(nd, new_stp->ls_seq, 2869 stp->ls_openowner, new_stp->ls_op); 2870 if (!error && stp->ls_stateid.seqid != new_stp->ls_stateid.seqid && 2871 !(new_stp->ls_flags & NFSLCK_CONFIRM)) 2872 error = NFSERR_OLDSTATEID; 2873 if (!error && vnode_vtype(vp) != VREG) { 2874 if (vnode_vtype(vp) == VDIR) 2875 error = NFSERR_ISDIR; 2876 else 2877 error = NFSERR_INVAL; 2878 } 2879 2880 if (error) { 2881 /* 2882 * If a client tries to confirm an Open with a bad 2883 * seqid# and there are no byte range locks or other Opens 2884 * on the openowner, just throw it away, so the next use of the 2885 * openowner will start a fresh seq#. 2886 */ 2887 if (error == NFSERR_BADSEQID && 2888 (new_stp->ls_flags & NFSLCK_CONFIRM) && 2889 nfsrv_nootherstate(stp)) 2890 nfsrv_freeopenowner(stp->ls_openowner, 0, p); 2891 NFSUNLOCKSTATE(); 2892 return (error); 2893 } 2894 2895 /* 2896 * Set the return stateid. 2897 */ 2898 stateidp->seqid = stp->ls_stateid.seqid + 1; 2899 stateidp->other[0] = stp->ls_stateid.other[0]; 2900 stateidp->other[1] = stp->ls_stateid.other[1]; 2901 stateidp->other[2] = stp->ls_stateid.other[2]; 2902 /* 2903 * Now, handle the three cases. 2904 */ 2905 if (new_stp->ls_flags & NFSLCK_CONFIRM) { 2906 /* 2907 * If the open doesn't need confirmation, it seems to me that 2908 * there is a client error, but I'll just log it and keep going? 2909 */ 2910 if (!(stp->ls_openowner->ls_flags & NFSLCK_NEEDSCONFIRM)) 2911 printf("Nfsv4d: stray open confirm\n"); 2912 stp->ls_openowner->ls_flags = 0; 2913 stp->ls_stateid.seqid++; 2914 if (!(clp->lc_flags & LCL_STAMPEDSTABLE)) { 2915 clp->lc_flags |= LCL_STAMPEDSTABLE; 2916 len = clp->lc_idlen; 2917 NFSBCOPY(clp->lc_id, client, len); 2918 gotstate = 1; 2919 } 2920 NFSUNLOCKSTATE(); 2921 } else if (new_stp->ls_flags & NFSLCK_CLOSE) { 2922 ownerstp = stp->ls_openowner; 2923 lfp = stp->ls_lfp; 2924 if (nfsrv_dolocallocks != 0 && !LIST_EMPTY(&stp->ls_open)) { 2925 /* Get the lf lock */ 2926 nfsrv_locklf(lfp); 2927 NFSUNLOCKSTATE(); 2928 if (nfsrv_freeopen(stp, vp, 1, p) == 0) { 2929 NFSLOCKSTATE(); 2930 nfsrv_unlocklf(lfp); 2931 NFSUNLOCKSTATE(); 2932 } 2933 } else { 2934 (void) nfsrv_freeopen(stp, NULL, 0, p); 2935 NFSUNLOCKSTATE(); 2936 } 2937 } else { 2938 /* 2939 * Update the share bits, making sure that the new set are a 2940 * subset of the old ones. 2941 */ 2942 bits = (new_stp->ls_flags & NFSLCK_SHAREBITS); 2943 if (~(stp->ls_flags) & bits) { 2944 NFSUNLOCKSTATE(); 2945 return (NFSERR_INVAL); 2946 } 2947 stp->ls_flags = (bits | NFSLCK_OPEN); 2948 stp->ls_stateid.seqid++; 2949 NFSUNLOCKSTATE(); 2950 } 2951 2952 /* 2953 * If the client just confirmed its first open, write a timestamp 2954 * to the stable storage file. 2955 */ 2956 if (gotstate) 2957 nfsrv_writestable(client, len, NFSNST_NEWSTATE, p); 2958 return (error); 2959 } 2960 2961 /* 2962 * Delegation update. Does the purge and return. 2963 */ 2964 APPLESTATIC int 2965 nfsrv_delegupdate(nfsquad_t clientid, nfsv4stateid_t *stateidp, 2966 vnode_t vp, int op, struct ucred *cred, NFSPROC_T *p) 2967 { 2968 struct nfsstate *stp; 2969 struct nfsclient *clp; 2970 int error; 2971 fhandle_t fh; 2972 2973 /* 2974 * Do a sanity check against the file handle for DelegReturn. 2975 */ 2976 if (vp) { 2977 error = nfsvno_getfh(vp, &fh, p); 2978 if (error) 2979 return (error); 2980 } 2981 /* 2982 * Check for restart conditions (client and server). 2983 */ 2984 if (op == NFSV4OP_DELEGRETURN) 2985 error = nfsrv_checkrestart(clientid, NFSLCK_DELEGRETURN, 2986 stateidp, 0); 2987 else 2988 error = nfsrv_checkrestart(clientid, NFSLCK_DELEGPURGE, 2989 stateidp, 0); 2990 2991 NFSLOCKSTATE(); 2992 /* 2993 * Get the open structure via clientid and stateid. 2994 */ 2995 if (!error) 2996 error = nfsrv_getclient(clientid, CLOPS_RENEW, &clp, 2997 (nfsquad_t)((u_quad_t)0), NULL, p); 2998 if (error) { 2999 if (error == NFSERR_CBPATHDOWN) 3000 error = 0; 3001 if (error == NFSERR_STALECLIENTID && op == NFSV4OP_DELEGRETURN) 3002 error = NFSERR_STALESTATEID; 3003 } 3004 if (!error && op == NFSV4OP_DELEGRETURN) { 3005 error = nfsrv_getstate(clp, stateidp, NFSLCK_DELEGRETURN, &stp); 3006 if (!error && stp->ls_stateid.seqid != stateidp->seqid) 3007 error = NFSERR_OLDSTATEID; 3008 } 3009 /* 3010 * NFSERR_EXPIRED means that the state has gone away, 3011 * so Delegations have been purged. Just return ok. 3012 */ 3013 if (error == NFSERR_EXPIRED && op == NFSV4OP_DELEGPURGE) { 3014 NFSUNLOCKSTATE(); 3015 return (0); 3016 } 3017 if (error) { 3018 NFSUNLOCKSTATE(); 3019 return (error); 3020 } 3021 3022 if (op == NFSV4OP_DELEGRETURN) { 3023 if (NFSBCMP((caddr_t)&fh, (caddr_t)&stp->ls_lfp->lf_fh, 3024 sizeof (fhandle_t))) { 3025 NFSUNLOCKSTATE(); 3026 return (NFSERR_BADSTATEID); 3027 } 3028 nfsrv_freedeleg(stp); 3029 } else { 3030 nfsrv_freedeleglist(&clp->lc_olddeleg); 3031 } 3032 NFSUNLOCKSTATE(); 3033 return (0); 3034 } 3035 3036 /* 3037 * Release lock owner. 3038 */ 3039 APPLESTATIC int 3040 nfsrv_releaselckown(struct nfsstate *new_stp, nfsquad_t clientid, 3041 NFSPROC_T *p) 3042 { 3043 struct nfsstate *stp, *nstp, *openstp, *ownstp; 3044 struct nfsclient *clp; 3045 int error; 3046 3047 /* 3048 * Check for restart conditions (client and server). 3049 */ 3050 error = nfsrv_checkrestart(clientid, new_stp->ls_flags, 3051 &new_stp->ls_stateid, 0); 3052 if (error) 3053 return (error); 3054 3055 NFSLOCKSTATE(); 3056 /* 3057 * Get the lock owner by name. 3058 */ 3059 error = nfsrv_getclient(clientid, CLOPS_RENEW, &clp, 3060 (nfsquad_t)((u_quad_t)0), NULL, p); 3061 if (error) { 3062 NFSUNLOCKSTATE(); 3063 return (error); 3064 } 3065 LIST_FOREACH(ownstp, &clp->lc_open, ls_list) { 3066 LIST_FOREACH(openstp, &ownstp->ls_open, ls_list) { 3067 stp = LIST_FIRST(&openstp->ls_open); 3068 while (stp != LIST_END(&openstp->ls_open)) { 3069 nstp = LIST_NEXT(stp, ls_list); 3070 /* 3071 * If the owner matches, check for locks and 3072 * then free or return an error. 3073 */ 3074 if (stp->ls_ownerlen == new_stp->ls_ownerlen && 3075 !NFSBCMP(stp->ls_owner, new_stp->ls_owner, 3076 stp->ls_ownerlen)){ 3077 if (LIST_EMPTY(&stp->ls_lock)) { 3078 nfsrv_freelockowner(stp, NULL, 0, p); 3079 } else { 3080 NFSUNLOCKSTATE(); 3081 return (NFSERR_LOCKSHELD); 3082 } 3083 } 3084 stp = nstp; 3085 } 3086 } 3087 } 3088 NFSUNLOCKSTATE(); 3089 return (0); 3090 } 3091 3092 /* 3093 * Get the file handle for a lock structure. 3094 */ 3095 static int 3096 nfsrv_getlockfh(vnode_t vp, u_short flags, 3097 struct nfslockfile **new_lfpp, fhandle_t *nfhp, NFSPROC_T *p) 3098 { 3099 fhandle_t *fhp = NULL; 3100 struct nfslockfile *new_lfp; 3101 int error; 3102 3103 /* 3104 * For lock, use the new nfslock structure, otherwise just 3105 * a fhandle_t on the stack. 3106 */ 3107 if (flags & NFSLCK_OPEN) { 3108 new_lfp = *new_lfpp; 3109 fhp = &new_lfp->lf_fh; 3110 } else if (nfhp) { 3111 fhp = nfhp; 3112 } else { 3113 panic("nfsrv_getlockfh"); 3114 } 3115 error = nfsvno_getfh(vp, fhp, p); 3116 return (error); 3117 } 3118 3119 /* 3120 * Get an nfs lock structure. Allocate one, as required, and return a 3121 * pointer to it. 3122 * Returns an NFSERR_xxx upon failure or -1 to indicate no current lock. 3123 */ 3124 static int 3125 nfsrv_getlockfile(u_short flags, struct nfslockfile **new_lfpp, 3126 struct nfslockfile **lfpp, fhandle_t *nfhp, int lockit) 3127 { 3128 struct nfslockfile *lfp; 3129 fhandle_t *fhp = NULL, *tfhp; 3130 struct nfslockhashhead *hp; 3131 struct nfslockfile *new_lfp = NULL; 3132 3133 /* 3134 * For lock, use the new nfslock structure, otherwise just 3135 * a fhandle_t on the stack. 3136 */ 3137 if (flags & NFSLCK_OPEN) { 3138 new_lfp = *new_lfpp; 3139 fhp = &new_lfp->lf_fh; 3140 } else if (nfhp) { 3141 fhp = nfhp; 3142 } else { 3143 panic("nfsrv_getlockfile"); 3144 } 3145 3146 hp = NFSLOCKHASH(fhp); 3147 LIST_FOREACH(lfp, hp, lf_hash) { 3148 tfhp = &lfp->lf_fh; 3149 if (NFSVNO_CMPFH(fhp, tfhp)) { 3150 if (lockit) 3151 nfsrv_locklf(lfp); 3152 *lfpp = lfp; 3153 return (0); 3154 } 3155 } 3156 if (!(flags & NFSLCK_OPEN)) 3157 return (-1); 3158 3159 /* 3160 * No match, so chain the new one into the list. 3161 */ 3162 LIST_INIT(&new_lfp->lf_open); 3163 LIST_INIT(&new_lfp->lf_lock); 3164 LIST_INIT(&new_lfp->lf_deleg); 3165 LIST_INIT(&new_lfp->lf_locallock); 3166 LIST_INIT(&new_lfp->lf_rollback); 3167 new_lfp->lf_locallock_lck.nfslock_usecnt = 0; 3168 new_lfp->lf_locallock_lck.nfslock_lock = 0; 3169 new_lfp->lf_usecount = 0; 3170 LIST_INSERT_HEAD(hp, new_lfp, lf_hash); 3171 *lfpp = new_lfp; 3172 *new_lfpp = NULL; 3173 return (0); 3174 } 3175 3176 /* 3177 * This function adds a nfslock lock structure to the list for the associated 3178 * nfsstate and nfslockfile structures. It will be inserted after the 3179 * entry pointed at by insert_lop. 3180 * Must be called with soft clock interrupts disabled. 3181 */ 3182 static void 3183 nfsrv_insertlock(struct nfslock *new_lop, struct nfslock *insert_lop, 3184 struct nfsstate *stp, struct nfslockfile *lfp) 3185 { 3186 struct nfslock *lop, *nlop; 3187 3188 new_lop->lo_stp = stp; 3189 new_lop->lo_lfp = lfp; 3190 3191 if (stp != NULL) { 3192 /* Insert in increasing lo_first order */ 3193 lop = LIST_FIRST(&lfp->lf_lock); 3194 if (lop == LIST_END(&lfp->lf_lock) || 3195 new_lop->lo_first <= lop->lo_first) { 3196 LIST_INSERT_HEAD(&lfp->lf_lock, new_lop, lo_lckfile); 3197 } else { 3198 nlop = LIST_NEXT(lop, lo_lckfile); 3199 while (nlop != LIST_END(&lfp->lf_lock) && 3200 nlop->lo_first < new_lop->lo_first) { 3201 lop = nlop; 3202 nlop = LIST_NEXT(lop, lo_lckfile); 3203 } 3204 LIST_INSERT_AFTER(lop, new_lop, lo_lckfile); 3205 } 3206 } else { 3207 new_lop->lo_lckfile.le_prev = NULL; /* list not used */ 3208 } 3209 3210 /* 3211 * Insert after insert_lop, which is overloaded as stp or lfp for 3212 * an empty list. 3213 */ 3214 if (stp == NULL && (struct nfslockfile *)insert_lop == lfp) 3215 LIST_INSERT_HEAD(&lfp->lf_locallock, new_lop, lo_lckowner); 3216 else if ((struct nfsstate *)insert_lop == stp) 3217 LIST_INSERT_HEAD(&stp->ls_lock, new_lop, lo_lckowner); 3218 else 3219 LIST_INSERT_AFTER(insert_lop, new_lop, lo_lckowner); 3220 if (stp != NULL) { 3221 newnfsstats.srvlocks++; 3222 nfsrv_openpluslock++; 3223 } 3224 } 3225 3226 /* 3227 * This function updates the locking for a lock owner and given file. It 3228 * maintains a list of lock ranges ordered on increasing file offset that 3229 * are NFSLCK_READ or NFSLCK_WRITE and non-overlapping (aka POSIX style). 3230 * It always adds new_lop to the list and sometimes uses the one pointed 3231 * at by other_lopp. 3232 * Must be called with soft clock interrupts disabled. 3233 */ 3234 static void 3235 nfsrv_updatelock(struct nfsstate *stp, struct nfslock **new_lopp, 3236 struct nfslock **other_lopp, struct nfslockfile *lfp) 3237 { 3238 struct nfslock *new_lop = *new_lopp; 3239 struct nfslock *lop, *tlop, *ilop; 3240 struct nfslock *other_lop = *other_lopp; 3241 int unlock = 0, myfile = 0; 3242 u_int64_t tmp; 3243 3244 /* 3245 * Work down the list until the lock is merged. 3246 */ 3247 if (new_lop->lo_flags & NFSLCK_UNLOCK) 3248 unlock = 1; 3249 if (stp != NULL) { 3250 ilop = (struct nfslock *)stp; 3251 lop = LIST_FIRST(&stp->ls_lock); 3252 } else { 3253 ilop = (struct nfslock *)lfp; 3254 lop = LIST_FIRST(&lfp->lf_locallock); 3255 } 3256 while (lop != NULL) { 3257 /* 3258 * Only check locks for this file that aren't before the start of 3259 * new lock's range. 3260 */ 3261 if (lop->lo_lfp == lfp) { 3262 myfile = 1; 3263 if (lop->lo_end >= new_lop->lo_first) { 3264 if (new_lop->lo_end < lop->lo_first) { 3265 /* 3266 * If the new lock ends before the start of the 3267 * current lock's range, no merge, just insert 3268 * the new lock. 3269 */ 3270 break; 3271 } 3272 if (new_lop->lo_flags == lop->lo_flags || 3273 (new_lop->lo_first <= lop->lo_first && 3274 new_lop->lo_end >= lop->lo_end)) { 3275 /* 3276 * This lock can be absorbed by the new lock/unlock. 3277 * This happens when it covers the entire range 3278 * of the old lock or is contiguous 3279 * with the old lock and is of the same type or an 3280 * unlock. 3281 */ 3282 if (lop->lo_first < new_lop->lo_first) 3283 new_lop->lo_first = lop->lo_first; 3284 if (lop->lo_end > new_lop->lo_end) 3285 new_lop->lo_end = lop->lo_end; 3286 tlop = lop; 3287 lop = LIST_NEXT(lop, lo_lckowner); 3288 nfsrv_freenfslock(tlop); 3289 continue; 3290 } 3291 3292 /* 3293 * All these cases are for contiguous locks that are not the 3294 * same type, so they can't be merged. 3295 */ 3296 if (new_lop->lo_first <= lop->lo_first) { 3297 /* 3298 * This case is where the new lock overlaps with the 3299 * first part of the old lock. Move the start of the 3300 * old lock to just past the end of the new lock. The 3301 * new lock will be inserted in front of the old, since 3302 * ilop hasn't been updated. (We are done now.) 3303 */ 3304 lop->lo_first = new_lop->lo_end; 3305 break; 3306 } 3307 if (new_lop->lo_end >= lop->lo_end) { 3308 /* 3309 * This case is where the new lock overlaps with the 3310 * end of the old lock's range. Move the old lock's 3311 * end to just before the new lock's first and insert 3312 * the new lock after the old lock. 3313 * Might not be done yet, since the new lock could 3314 * overlap further locks with higher ranges. 3315 */ 3316 lop->lo_end = new_lop->lo_first; 3317 ilop = lop; 3318 lop = LIST_NEXT(lop, lo_lckowner); 3319 continue; 3320 } 3321 /* 3322 * The final case is where the new lock's range is in the 3323 * middle of the current lock's and splits the current lock 3324 * up. Use *other_lopp to handle the second part of the 3325 * split old lock range. (We are done now.) 3326 * For unlock, we use new_lop as other_lop and tmp, since 3327 * other_lop and new_lop are the same for this case. 3328 * We noted the unlock case above, so we don't need 3329 * new_lop->lo_flags any longer. 3330 */ 3331 tmp = new_lop->lo_first; 3332 if (other_lop == NULL) { 3333 if (!unlock) 3334 panic("nfsd srv update unlock"); 3335 other_lop = new_lop; 3336 *new_lopp = NULL; 3337 } 3338 other_lop->lo_first = new_lop->lo_end; 3339 other_lop->lo_end = lop->lo_end; 3340 other_lop->lo_flags = lop->lo_flags; 3341 other_lop->lo_stp = stp; 3342 other_lop->lo_lfp = lfp; 3343 lop->lo_end = tmp; 3344 nfsrv_insertlock(other_lop, lop, stp, lfp); 3345 *other_lopp = NULL; 3346 ilop = lop; 3347 break; 3348 } 3349 } 3350 ilop = lop; 3351 lop = LIST_NEXT(lop, lo_lckowner); 3352 if (myfile && (lop == NULL || lop->lo_lfp != lfp)) 3353 break; 3354 } 3355 3356 /* 3357 * Insert the new lock in the list at the appropriate place. 3358 */ 3359 if (!unlock) { 3360 nfsrv_insertlock(new_lop, ilop, stp, lfp); 3361 *new_lopp = NULL; 3362 } 3363 } 3364 3365 /* 3366 * This function handles sequencing of locks, etc. 3367 * It returns an error that indicates what the caller should do. 3368 */ 3369 static int 3370 nfsrv_checkseqid(struct nfsrv_descript *nd, u_int32_t seqid, 3371 struct nfsstate *stp, struct nfsrvcache *op) 3372 { 3373 3374 if (op != nd->nd_rp) 3375 panic("nfsrvstate checkseqid"); 3376 if (!(op->rc_flag & RC_INPROG)) 3377 panic("nfsrvstate not inprog"); 3378 if (stp->ls_op && stp->ls_op->rc_refcnt <= 0) { 3379 printf("refcnt=%d\n", stp->ls_op->rc_refcnt); 3380 panic("nfsrvstate op refcnt"); 3381 } 3382 if ((stp->ls_seq + 1) == seqid) { 3383 if (stp->ls_op) 3384 nfsrvd_derefcache(stp->ls_op); 3385 stp->ls_op = op; 3386 nfsrvd_refcache(op); 3387 stp->ls_seq = seqid; 3388 return (0); 3389 } else if (stp->ls_seq == seqid && stp->ls_op && 3390 op->rc_xid == stp->ls_op->rc_xid && 3391 op->rc_refcnt == 0 && 3392 op->rc_reqlen == stp->ls_op->rc_reqlen && 3393 op->rc_cksum == stp->ls_op->rc_cksum) { 3394 if (stp->ls_op->rc_flag & RC_INPROG) 3395 return (NFSERR_DONTREPLY); 3396 nd->nd_rp = stp->ls_op; 3397 nd->nd_rp->rc_flag |= RC_INPROG; 3398 nfsrvd_delcache(op); 3399 return (NFSERR_REPLYFROMCACHE); 3400 } 3401 return (NFSERR_BADSEQID); 3402 } 3403 3404 /* 3405 * Get the client ip address for callbacks. If the strings can't be parsed, 3406 * just set lc_program to 0 to indicate no callbacks are possible. 3407 * (For cases where the address can't be parsed or is 0.0.0.0.0.0, set 3408 * the address to the client's transport address. This won't be used 3409 * for callbacks, but can be printed out by newnfsstats for info.) 3410 * Return error if the xdr can't be parsed, 0 otherwise. 3411 */ 3412 APPLESTATIC int 3413 nfsrv_getclientipaddr(struct nfsrv_descript *nd, struct nfsclient *clp) 3414 { 3415 u_int32_t *tl; 3416 u_char *cp, *cp2; 3417 int i, j; 3418 struct sockaddr_in *rad, *sad; 3419 u_char protocol[5], addr[24]; 3420 int error = 0, cantparse = 0; 3421 union { 3422 u_long ival; 3423 u_char cval[4]; 3424 } ip; 3425 union { 3426 u_short sval; 3427 u_char cval[2]; 3428 } port; 3429 3430 rad = NFSSOCKADDR(clp->lc_req.nr_nam, struct sockaddr_in *); 3431 rad->sin_family = AF_INET; 3432 rad->sin_len = sizeof (struct sockaddr_in); 3433 rad->sin_addr.s_addr = 0; 3434 rad->sin_port = 0; 3435 clp->lc_req.nr_client = NULL; 3436 clp->lc_req.nr_lock = 0; 3437 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED); 3438 i = fxdr_unsigned(int, *tl); 3439 if (i >= 3 && i <= 4) { 3440 error = nfsrv_mtostr(nd, protocol, i); 3441 if (error) 3442 goto nfsmout; 3443 if (!strcmp(protocol, "tcp")) { 3444 clp->lc_flags |= LCL_TCPCALLBACK; 3445 clp->lc_req.nr_sotype = SOCK_STREAM; 3446 clp->lc_req.nr_soproto = IPPROTO_TCP; 3447 } else if (!strcmp(protocol, "udp")) { 3448 clp->lc_req.nr_sotype = SOCK_DGRAM; 3449 clp->lc_req.nr_soproto = IPPROTO_UDP; 3450 } else { 3451 cantparse = 1; 3452 } 3453 } else { 3454 cantparse = 1; 3455 if (i > 0) { 3456 error = nfsm_advance(nd, NFSM_RNDUP(i), -1); 3457 if (error) 3458 goto nfsmout; 3459 } 3460 } 3461 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED); 3462 i = fxdr_unsigned(int, *tl); 3463 if (i < 0) { 3464 error = NFSERR_BADXDR; 3465 goto nfsmout; 3466 } else if (i == 0) { 3467 cantparse = 1; 3468 } else if (!cantparse && i <= 23 && i >= 11) { 3469 error = nfsrv_mtostr(nd, addr, i); 3470 if (error) 3471 goto nfsmout; 3472 3473 /* 3474 * Parse out the address fields. We expect 6 decimal numbers 3475 * separated by '.'s. 3476 */ 3477 cp = addr; 3478 i = 0; 3479 while (*cp && i < 6) { 3480 cp2 = cp; 3481 while (*cp2 && *cp2 != '.') 3482 cp2++; 3483 if (*cp2) 3484 *cp2++ = '\0'; 3485 else if (i != 5) { 3486 cantparse = 1; 3487 break; 3488 } 3489 j = nfsrv_getipnumber(cp); 3490 if (j >= 0) { 3491 if (i < 4) 3492 ip.cval[3 - i] = j; 3493 else 3494 port.cval[5 - i] = j; 3495 } else { 3496 cantparse = 1; 3497 break; 3498 } 3499 cp = cp2; 3500 i++; 3501 } 3502 if (!cantparse) { 3503 if (ip.ival != 0x0) { 3504 rad->sin_addr.s_addr = htonl(ip.ival); 3505 rad->sin_port = htons(port.sval); 3506 } else { 3507 cantparse = 1; 3508 } 3509 } 3510 } else { 3511 cantparse = 1; 3512 if (i > 0) { 3513 error = nfsm_advance(nd, NFSM_RNDUP(i), -1); 3514 if (error) 3515 goto nfsmout; 3516 } 3517 } 3518 if (cantparse) { 3519 sad = NFSSOCKADDR(nd->nd_nam, struct sockaddr_in *); 3520 rad->sin_addr.s_addr = sad->sin_addr.s_addr; 3521 rad->sin_port = 0x0; 3522 clp->lc_program = 0; 3523 } 3524 nfsmout: 3525 return (error); 3526 } 3527 3528 /* 3529 * Turn a string of up to three decimal digits into a number. Return -1 upon 3530 * error. 3531 */ 3532 static int 3533 nfsrv_getipnumber(u_char *cp) 3534 { 3535 int i = 0, j = 0; 3536 3537 while (*cp) { 3538 if (j > 2 || *cp < '0' || *cp > '9') 3539 return (-1); 3540 i *= 10; 3541 i += (*cp - '0'); 3542 cp++; 3543 j++; 3544 } 3545 if (i < 256) 3546 return (i); 3547 return (-1); 3548 } 3549 3550 /* 3551 * This function checks for restart conditions. 3552 */ 3553 static int 3554 nfsrv_checkrestart(nfsquad_t clientid, u_int32_t flags, 3555 nfsv4stateid_t *stateidp, int specialid) 3556 { 3557 int ret; 3558 3559 /* 3560 * First check for a server restart. Open, LockT, ReleaseLockOwner 3561 * and DelegPurge have a clientid, the rest a stateid. 3562 */ 3563 if (flags & 3564 (NFSLCK_OPEN | NFSLCK_TEST | NFSLCK_RELEASE | NFSLCK_DELEGPURGE)) { 3565 if (clientid.lval[0] != nfsrvboottime) 3566 return (NFSERR_STALECLIENTID); 3567 } else if (stateidp->other[0] != nfsrvboottime && 3568 specialid == 0) 3569 return (NFSERR_STALESTATEID); 3570 3571 /* 3572 * Read, Write, Setattr and LockT can return NFSERR_GRACE and do 3573 * not use a lock/open owner seqid#, so the check can be done now. 3574 * (The others will be checked, as required, later.) 3575 */ 3576 if (!(flags & (NFSLCK_CHECK | NFSLCK_TEST))) 3577 return (0); 3578 3579 NFSLOCKSTATE(); 3580 ret = nfsrv_checkgrace(flags); 3581 NFSUNLOCKSTATE(); 3582 return (ret); 3583 } 3584 3585 /* 3586 * Check for grace. 3587 */ 3588 static int 3589 nfsrv_checkgrace(u_int32_t flags) 3590 { 3591 3592 if (nfsrv_stablefirst.nsf_flags & NFSNSF_GRACEOVER) { 3593 if (flags & NFSLCK_RECLAIM) 3594 return (NFSERR_NOGRACE); 3595 } else { 3596 if (!(flags & NFSLCK_RECLAIM)) 3597 return (NFSERR_GRACE); 3598 3599 /* 3600 * If grace is almost over and we are still getting Reclaims, 3601 * extend grace a bit. 3602 */ 3603 if ((NFSD_MONOSEC + NFSRV_LEASEDELTA) > 3604 nfsrv_stablefirst.nsf_eograce) 3605 nfsrv_stablefirst.nsf_eograce = NFSD_MONOSEC + 3606 NFSRV_LEASEDELTA; 3607 } 3608 return (0); 3609 } 3610 3611 /* 3612 * Do a server callback. 3613 */ 3614 static int 3615 nfsrv_docallback(struct nfsclient *clp, int procnum, 3616 nfsv4stateid_t *stateidp, int trunc, fhandle_t *fhp, 3617 struct nfsvattr *nap, nfsattrbit_t *attrbitp, NFSPROC_T *p) 3618 { 3619 mbuf_t m; 3620 u_int32_t *tl; 3621 struct nfsrv_descript nfsd, *nd = &nfsd; 3622 struct ucred *cred; 3623 int error = 0; 3624 u_int32_t callback; 3625 3626 cred = newnfs_getcred(); 3627 NFSLOCKSTATE(); /* mostly for lc_cbref++ */ 3628 if (clp->lc_flags & LCL_NEEDSCONFIRM) { 3629 NFSUNLOCKSTATE(); 3630 panic("docallb"); 3631 } 3632 clp->lc_cbref++; 3633 3634 /* 3635 * Fill the callback program# and version into the request 3636 * structure for newnfs_connect() to use. 3637 */ 3638 clp->lc_req.nr_prog = clp->lc_program; 3639 clp->lc_req.nr_vers = NFSV4_CBVERS; 3640 3641 /* 3642 * First, fill in some of the fields of nd and cr. 3643 */ 3644 nd->nd_flag = ND_NFSV4; 3645 if (clp->lc_flags & LCL_GSS) 3646 nd->nd_flag |= ND_KERBV; 3647 nd->nd_repstat = 0; 3648 cred->cr_uid = clp->lc_uid; 3649 cred->cr_gid = clp->lc_gid; 3650 callback = clp->lc_callback; 3651 NFSUNLOCKSTATE(); 3652 cred->cr_ngroups = 1; 3653 3654 /* 3655 * Get the first mbuf for the request. 3656 */ 3657 MGET(m, M_WAIT, MT_DATA); 3658 mbuf_setlen(m, 0); 3659 nd->nd_mreq = nd->nd_mb = m; 3660 nd->nd_bpos = NFSMTOD(m, caddr_t); 3661 3662 /* 3663 * and build the callback request. 3664 */ 3665 if (procnum == NFSV4OP_CBGETATTR) { 3666 nd->nd_procnum = NFSV4PROC_CBCOMPOUND; 3667 (void) nfsm_strtom(nd, "CB Getattr", 10); 3668 NFSM_BUILD(tl, u_int32_t *, 4 * NFSX_UNSIGNED); 3669 *tl++ = txdr_unsigned(NFSV4_MINORVERSION); 3670 *tl++ = txdr_unsigned(callback); 3671 *tl++ = txdr_unsigned(1); 3672 *tl = txdr_unsigned(NFSV4OP_CBGETATTR); 3673 (void) nfsm_fhtom(nd, (u_int8_t *)fhp, NFSX_MYFH, 0); 3674 (void) nfsrv_putattrbit(nd, attrbitp); 3675 } else if (procnum == NFSV4OP_CBRECALL) { 3676 nd->nd_procnum = NFSV4PROC_CBCOMPOUND; 3677 (void) nfsm_strtom(nd, "CB Recall", 9); 3678 NFSM_BUILD(tl, u_int32_t *, 5 * NFSX_UNSIGNED + NFSX_STATEID); 3679 *tl++ = txdr_unsigned(NFSV4_MINORVERSION); 3680 *tl++ = txdr_unsigned(callback); 3681 *tl++ = txdr_unsigned(1); 3682 *tl++ = txdr_unsigned(NFSV4OP_CBRECALL); 3683 *tl++ = txdr_unsigned(stateidp->seqid); 3684 NFSBCOPY((caddr_t)stateidp->other, (caddr_t)tl, 3685 NFSX_STATEIDOTHER); 3686 tl += (NFSX_STATEIDOTHER / NFSX_UNSIGNED); 3687 if (trunc) 3688 *tl = newnfs_true; 3689 else 3690 *tl = newnfs_false; 3691 (void) nfsm_fhtom(nd, (u_int8_t *)fhp, NFSX_MYFH, 0); 3692 } else { 3693 nd->nd_procnum = NFSV4PROC_CBNULL; 3694 } 3695 3696 /* 3697 * Call newnfs_connect(), as required, and then newnfs_request(). 3698 */ 3699 (void) newnfs_sndlock(&clp->lc_req.nr_lock); 3700 if (clp->lc_req.nr_client == NULL) { 3701 if (nd->nd_procnum == NFSV4PROC_CBNULL) 3702 error = newnfs_connect(NULL, &clp->lc_req, cred, 3703 NULL, 1); 3704 else 3705 error = newnfs_connect(NULL, &clp->lc_req, cred, 3706 NULL, 3); 3707 } 3708 newnfs_sndunlock(&clp->lc_req.nr_lock); 3709 if (!error) { 3710 error = newnfs_request(nd, NULL, clp, &clp->lc_req, NULL, 3711 NULL, cred, clp->lc_program, NFSV4_CBVERS, NULL, 1, NULL); 3712 } 3713 NFSFREECRED(cred); 3714 3715 /* 3716 * If error is set here, the Callback path isn't working 3717 * properly, so twiddle the appropriate LCL_ flags. 3718 * (nd_repstat != 0 indicates the Callback path is working, 3719 * but the callback failed on the client.) 3720 */ 3721 if (error) { 3722 /* 3723 * Mark the callback pathway down, which disabled issuing 3724 * of delegations and gets Renew to return NFSERR_CBPATHDOWN. 3725 */ 3726 NFSLOCKSTATE(); 3727 clp->lc_flags |= LCL_CBDOWN; 3728 NFSUNLOCKSTATE(); 3729 } else { 3730 /* 3731 * Callback worked. If the callback path was down, disable 3732 * callbacks, so no more delegations will be issued. (This 3733 * is done on the assumption that the callback pathway is 3734 * flakey.) 3735 */ 3736 NFSLOCKSTATE(); 3737 if (clp->lc_flags & LCL_CBDOWN) 3738 clp->lc_flags &= ~(LCL_CBDOWN | LCL_CALLBACKSON); 3739 NFSUNLOCKSTATE(); 3740 if (nd->nd_repstat) 3741 error = nd->nd_repstat; 3742 else if (procnum == NFSV4OP_CBGETATTR) 3743 error = nfsv4_loadattr(nd, NULL, nap, NULL, NULL, 0, 3744 NULL, NULL, NULL, NULL, NULL, 0, NULL, NULL, NULL, 3745 p, NULL); 3746 mbuf_freem(nd->nd_mrep); 3747 } 3748 NFSLOCKSTATE(); 3749 clp->lc_cbref--; 3750 if ((clp->lc_flags & LCL_WAKEUPWANTED) && clp->lc_cbref == 0) { 3751 clp->lc_flags &= ~LCL_WAKEUPWANTED; 3752 NFSUNLOCKSTATE(); 3753 wakeup((caddr_t)clp); 3754 } else { 3755 NFSUNLOCKSTATE(); 3756 } 3757 return (error); 3758 } 3759 3760 /* 3761 * Return the next index# for a clientid. Mostly just increment and return 3762 * the next one, but... if the 32bit unsigned does actually wrap around, 3763 * it should be rebooted. 3764 * At an average rate of one new client per second, it will wrap around in 3765 * approximately 136 years. (I think the server will have been shut 3766 * down or rebooted before then.) 3767 */ 3768 static u_int32_t 3769 nfsrv_nextclientindex(void) 3770 { 3771 static u_int32_t client_index = 0; 3772 3773 client_index++; 3774 if (client_index != 0) 3775 return (client_index); 3776 3777 printf("%s: out of clientids\n", __func__); 3778 return (client_index); 3779 } 3780 3781 /* 3782 * Return the next index# for a stateid. Mostly just increment and return 3783 * the next one, but... if the 32bit unsigned does actually wrap around 3784 * (will a BSD server stay up that long?), find 3785 * new start and end values. 3786 */ 3787 static u_int32_t 3788 nfsrv_nextstateindex(struct nfsclient *clp) 3789 { 3790 struct nfsstate *stp; 3791 int i; 3792 u_int32_t canuse, min_index, max_index; 3793 3794 if (!(clp->lc_flags & LCL_INDEXNOTOK)) { 3795 clp->lc_stateindex++; 3796 if (clp->lc_stateindex != clp->lc_statemaxindex) 3797 return (clp->lc_stateindex); 3798 } 3799 3800 /* 3801 * Yuck, we've hit the end. 3802 * Look for a new min and max. 3803 */ 3804 min_index = 0; 3805 max_index = 0xffffffff; 3806 for (i = 0; i < NFSSTATEHASHSIZE; i++) { 3807 LIST_FOREACH(stp, &clp->lc_stateid[i], ls_hash) { 3808 if (stp->ls_stateid.other[2] > 0x80000000) { 3809 if (stp->ls_stateid.other[2] < max_index) 3810 max_index = stp->ls_stateid.other[2]; 3811 } else { 3812 if (stp->ls_stateid.other[2] > min_index) 3813 min_index = stp->ls_stateid.other[2]; 3814 } 3815 } 3816 } 3817 3818 /* 3819 * Yikes, highly unlikely, but I'll handle it anyhow. 3820 */ 3821 if (min_index == 0x80000000 && max_index == 0x80000001) { 3822 canuse = 0; 3823 /* 3824 * Loop around until we find an unused entry. Return that 3825 * and set LCL_INDEXNOTOK, so the search will continue next time. 3826 * (This is one of those rare cases where a goto is the 3827 * cleanest way to code the loop.) 3828 */ 3829 tryagain: 3830 for (i = 0; i < NFSSTATEHASHSIZE; i++) { 3831 LIST_FOREACH(stp, &clp->lc_stateid[i], ls_hash) { 3832 if (stp->ls_stateid.other[2] == canuse) { 3833 canuse++; 3834 goto tryagain; 3835 } 3836 } 3837 } 3838 clp->lc_flags |= LCL_INDEXNOTOK; 3839 return (canuse); 3840 } 3841 3842 /* 3843 * Ok to start again from min + 1. 3844 */ 3845 clp->lc_stateindex = min_index + 1; 3846 clp->lc_statemaxindex = max_index; 3847 clp->lc_flags &= ~LCL_INDEXNOTOK; 3848 return (clp->lc_stateindex); 3849 } 3850 3851 /* 3852 * The following functions handle the stable storage file that deals with 3853 * the edge conditions described in RFC3530 Sec. 8.6.3. 3854 * The file is as follows: 3855 * - a single record at the beginning that has the lease time of the 3856 * previous server instance (before the last reboot) and the nfsrvboottime 3857 * values for the previous server boots. 3858 * These previous boot times are used to ensure that the current 3859 * nfsrvboottime does not, somehow, get set to a previous one. 3860 * (This is important so that Stale ClientIDs and StateIDs can 3861 * be recognized.) 3862 * The number of previous nfsvrboottime values preceeds the list. 3863 * - followed by some number of appended records with: 3864 * - client id string 3865 * - flag that indicates it is a record revoking state via lease 3866 * expiration or similar 3867 * OR has successfully acquired state. 3868 * These structures vary in length, with the client string at the end, up 3869 * to NFSV4_OPAQUELIMIT in size. 3870 * 3871 * At the end of the grace period, the file is truncated, the first 3872 * record is rewritten with updated information and any acquired state 3873 * records for successful reclaims of state are written. 3874 * 3875 * Subsequent records are appended when the first state is issued to 3876 * a client and when state is revoked for a client. 3877 * 3878 * When reading the file in, state issued records that come later in 3879 * the file override older ones, since the append log is in cronological order. 3880 * If, for some reason, the file can't be read, the grace period is 3881 * immediately terminated and all reclaims get NFSERR_NOGRACE. 3882 */ 3883 3884 /* 3885 * Read in the stable storage file. Called by nfssvc() before the nfsd 3886 * processes start servicing requests. 3887 */ 3888 APPLESTATIC void 3889 nfsrv_setupstable(NFSPROC_T *p) 3890 { 3891 struct nfsrv_stablefirst *sf = &nfsrv_stablefirst; 3892 struct nfsrv_stable *sp, *nsp; 3893 struct nfst_rec *tsp; 3894 int error, i, tryagain; 3895 off_t off = 0; 3896 int aresid, len; 3897 struct timeval curtime; 3898 3899 /* 3900 * If NFSNSF_UPDATEDONE is set, this is a restart of the nfsds without 3901 * a reboot, so state has not been lost. 3902 */ 3903 if (sf->nsf_flags & NFSNSF_UPDATEDONE) 3904 return; 3905 /* 3906 * Set Grace over just until the file reads successfully. 3907 */ 3908 NFSGETTIME(&curtime); 3909 nfsrvboottime = curtime.tv_sec; 3910 LIST_INIT(&sf->nsf_head); 3911 sf->nsf_flags = (NFSNSF_GRACEOVER | NFSNSF_NEEDLOCK); 3912 sf->nsf_eograce = NFSD_MONOSEC + NFSRV_LEASEDELTA; 3913 if (sf->nsf_fp == NULL) 3914 return; 3915 error = NFSD_RDWR(UIO_READ, NFSFPVNODE(sf->nsf_fp), 3916 (caddr_t)&sf->nsf_rec, sizeof (struct nfsf_rec), off, UIO_SYSSPACE, 3917 0, NFSFPCRED(sf->nsf_fp), &aresid, p); 3918 if (error || aresid || sf->nsf_numboots == 0 || 3919 sf->nsf_numboots > NFSNSF_MAXNUMBOOTS) 3920 return; 3921 3922 /* 3923 * Now, read in the boottimes. 3924 */ 3925 sf->nsf_bootvals = (time_t *)malloc((sf->nsf_numboots + 1) * 3926 sizeof (time_t), M_TEMP, M_WAITOK); 3927 off = sizeof (struct nfsf_rec); 3928 error = NFSD_RDWR(UIO_READ, NFSFPVNODE(sf->nsf_fp), 3929 (caddr_t)sf->nsf_bootvals, sf->nsf_numboots * sizeof (time_t), off, 3930 UIO_SYSSPACE, 0, NFSFPCRED(sf->nsf_fp), &aresid, p); 3931 if (error || aresid) { 3932 free((caddr_t)sf->nsf_bootvals, M_TEMP); 3933 sf->nsf_bootvals = NULL; 3934 return; 3935 } 3936 3937 /* 3938 * Make sure this nfsrvboottime is different from all recorded 3939 * previous ones. 3940 */ 3941 do { 3942 tryagain = 0; 3943 for (i = 0; i < sf->nsf_numboots; i++) { 3944 if (nfsrvboottime == sf->nsf_bootvals[i]) { 3945 nfsrvboottime++; 3946 tryagain = 1; 3947 break; 3948 } 3949 } 3950 } while (tryagain); 3951 3952 sf->nsf_flags |= NFSNSF_OK; 3953 off += (sf->nsf_numboots * sizeof (time_t)); 3954 3955 /* 3956 * Read through the file, building a list of records for grace 3957 * checking. 3958 * Each record is between sizeof (struct nfst_rec) and 3959 * sizeof (struct nfst_rec) + NFSV4_OPAQUELIMIT - 1 3960 * and is actually sizeof (struct nfst_rec) + nst_len - 1. 3961 */ 3962 tsp = (struct nfst_rec *)malloc(sizeof (struct nfst_rec) + 3963 NFSV4_OPAQUELIMIT - 1, M_TEMP, M_WAITOK); 3964 do { 3965 error = NFSD_RDWR(UIO_READ, NFSFPVNODE(sf->nsf_fp), 3966 (caddr_t)tsp, sizeof (struct nfst_rec) + NFSV4_OPAQUELIMIT - 1, 3967 off, UIO_SYSSPACE, 0, NFSFPCRED(sf->nsf_fp), &aresid, p); 3968 len = (sizeof (struct nfst_rec) + NFSV4_OPAQUELIMIT - 1) - aresid; 3969 if (error || (len > 0 && (len < sizeof (struct nfst_rec) || 3970 len < (sizeof (struct nfst_rec) + tsp->len - 1)))) { 3971 /* 3972 * Yuck, the file has been corrupted, so just return 3973 * after clearing out any restart state, so the grace period 3974 * is over. 3975 */ 3976 LIST_FOREACH_SAFE(sp, &sf->nsf_head, nst_list, nsp) { 3977 LIST_REMOVE(sp, nst_list); 3978 free((caddr_t)sp, M_TEMP); 3979 } 3980 free((caddr_t)tsp, M_TEMP); 3981 sf->nsf_flags &= ~NFSNSF_OK; 3982 free((caddr_t)sf->nsf_bootvals, M_TEMP); 3983 sf->nsf_bootvals = NULL; 3984 return; 3985 } 3986 if (len > 0) { 3987 off += sizeof (struct nfst_rec) + tsp->len - 1; 3988 /* 3989 * Search the list for a matching client. 3990 */ 3991 LIST_FOREACH(sp, &sf->nsf_head, nst_list) { 3992 if (tsp->len == sp->nst_len && 3993 !NFSBCMP(tsp->client, sp->nst_client, tsp->len)) 3994 break; 3995 } 3996 if (sp == LIST_END(&sf->nsf_head)) { 3997 sp = (struct nfsrv_stable *)malloc(tsp->len + 3998 sizeof (struct nfsrv_stable) - 1, M_TEMP, 3999 M_WAITOK); 4000 NFSBCOPY((caddr_t)tsp, (caddr_t)&sp->nst_rec, 4001 sizeof (struct nfst_rec) + tsp->len - 1); 4002 LIST_INSERT_HEAD(&sf->nsf_head, sp, nst_list); 4003 } else { 4004 if (tsp->flag == NFSNST_REVOKE) 4005 sp->nst_flag |= NFSNST_REVOKE; 4006 else 4007 /* 4008 * A subsequent timestamp indicates the client 4009 * did a setclientid/confirm and any previous 4010 * revoke is no longer relevant. 4011 */ 4012 sp->nst_flag &= ~NFSNST_REVOKE; 4013 } 4014 } 4015 } while (len > 0); 4016 free((caddr_t)tsp, M_TEMP); 4017 sf->nsf_flags = NFSNSF_OK; 4018 sf->nsf_eograce = NFSD_MONOSEC + sf->nsf_lease + 4019 NFSRV_LEASEDELTA; 4020 } 4021 4022 /* 4023 * Update the stable storage file, now that the grace period is over. 4024 */ 4025 APPLESTATIC void 4026 nfsrv_updatestable(NFSPROC_T *p) 4027 { 4028 struct nfsrv_stablefirst *sf = &nfsrv_stablefirst; 4029 struct nfsrv_stable *sp, *nsp; 4030 int i; 4031 struct nfsvattr nva; 4032 vnode_t vp; 4033 #if defined(__FreeBSD_version) && (__FreeBSD_version >= 500000) 4034 mount_t mp = NULL; 4035 #endif 4036 int error; 4037 4038 if (sf->nsf_fp == NULL || (sf->nsf_flags & NFSNSF_UPDATEDONE)) 4039 return; 4040 sf->nsf_flags |= NFSNSF_UPDATEDONE; 4041 /* 4042 * Ok, we need to rewrite the stable storage file. 4043 * - truncate to 0 length 4044 * - write the new first structure 4045 * - loop through the data structures, writing out any that 4046 * have timestamps older than the old boot 4047 */ 4048 if (sf->nsf_bootvals) { 4049 sf->nsf_numboots++; 4050 for (i = sf->nsf_numboots - 2; i >= 0; i--) 4051 sf->nsf_bootvals[i + 1] = sf->nsf_bootvals[i]; 4052 } else { 4053 sf->nsf_numboots = 1; 4054 sf->nsf_bootvals = (time_t *)malloc(sizeof (time_t), 4055 M_TEMP, M_WAITOK); 4056 } 4057 sf->nsf_bootvals[0] = nfsrvboottime; 4058 sf->nsf_lease = nfsrv_lease; 4059 NFSVNO_ATTRINIT(&nva); 4060 NFSVNO_SETATTRVAL(&nva, size, 0); 4061 vp = NFSFPVNODE(sf->nsf_fp); 4062 NFS_STARTWRITE(vp, &mp); 4063 NFSVOPLOCK(vp, LK_EXCLUSIVE | LK_RETRY, p); 4064 error = nfsvno_setattr(vp, &nva, NFSFPCRED(sf->nsf_fp), p, NULL); 4065 NFS_ENDWRITE(mp); 4066 NFSVOPUNLOCK(vp, 0, p); 4067 if (!error) 4068 error = NFSD_RDWR(UIO_WRITE, vp, 4069 (caddr_t)&sf->nsf_rec, sizeof (struct nfsf_rec), (off_t)0, 4070 UIO_SYSSPACE, IO_SYNC, NFSFPCRED(sf->nsf_fp), NULL, p); 4071 if (!error) 4072 error = NFSD_RDWR(UIO_WRITE, vp, 4073 (caddr_t)sf->nsf_bootvals, 4074 sf->nsf_numboots * sizeof (time_t), 4075 (off_t)(sizeof (struct nfsf_rec)), 4076 UIO_SYSSPACE, IO_SYNC, NFSFPCRED(sf->nsf_fp), NULL, p); 4077 free((caddr_t)sf->nsf_bootvals, M_TEMP); 4078 sf->nsf_bootvals = NULL; 4079 if (error) { 4080 sf->nsf_flags &= ~NFSNSF_OK; 4081 printf("EEK! Can't write NfsV4 stable storage file\n"); 4082 return; 4083 } 4084 sf->nsf_flags |= NFSNSF_OK; 4085 4086 /* 4087 * Loop through the list and write out timestamp records for 4088 * any clients that successfully reclaimed state. 4089 */ 4090 LIST_FOREACH_SAFE(sp, &sf->nsf_head, nst_list, nsp) { 4091 if (sp->nst_flag & NFSNST_GOTSTATE) { 4092 nfsrv_writestable(sp->nst_client, sp->nst_len, 4093 NFSNST_NEWSTATE, p); 4094 sp->nst_clp->lc_flags |= LCL_STAMPEDSTABLE; 4095 } 4096 LIST_REMOVE(sp, nst_list); 4097 free((caddr_t)sp, M_TEMP); 4098 } 4099 } 4100 4101 /* 4102 * Append a record to the stable storage file. 4103 */ 4104 APPLESTATIC void 4105 nfsrv_writestable(u_char *client, int len, int flag, NFSPROC_T *p) 4106 { 4107 struct nfsrv_stablefirst *sf = &nfsrv_stablefirst; 4108 struct nfst_rec *sp; 4109 int error; 4110 4111 if (!(sf->nsf_flags & NFSNSF_OK) || sf->nsf_fp == NULL) 4112 return; 4113 sp = (struct nfst_rec *)malloc(sizeof (struct nfst_rec) + 4114 len - 1, M_TEMP, M_WAITOK); 4115 sp->len = len; 4116 NFSBCOPY(client, sp->client, len); 4117 sp->flag = flag; 4118 error = NFSD_RDWR(UIO_WRITE, NFSFPVNODE(sf->nsf_fp), 4119 (caddr_t)sp, sizeof (struct nfst_rec) + len - 1, (off_t)0, 4120 UIO_SYSSPACE, (IO_SYNC | IO_APPEND), NFSFPCRED(sf->nsf_fp), NULL, p); 4121 free((caddr_t)sp, M_TEMP); 4122 if (error) { 4123 sf->nsf_flags &= ~NFSNSF_OK; 4124 printf("EEK! Can't write NfsV4 stable storage file\n"); 4125 } 4126 } 4127 4128 /* 4129 * This function is called during the grace period to mark a client 4130 * that successfully reclaimed state. 4131 */ 4132 static void 4133 nfsrv_markstable(struct nfsclient *clp) 4134 { 4135 struct nfsrv_stable *sp; 4136 4137 /* 4138 * First find the client structure. 4139 */ 4140 LIST_FOREACH(sp, &nfsrv_stablefirst.nsf_head, nst_list) { 4141 if (sp->nst_len == clp->lc_idlen && 4142 !NFSBCMP(sp->nst_client, clp->lc_id, sp->nst_len)) 4143 break; 4144 } 4145 if (sp == LIST_END(&nfsrv_stablefirst.nsf_head)) 4146 return; 4147 4148 /* 4149 * Now, just mark it and set the nfsclient back pointer. 4150 */ 4151 sp->nst_flag |= NFSNST_GOTSTATE; 4152 sp->nst_clp = clp; 4153 } 4154 4155 /* 4156 * This function is called for a reclaim, to see if it gets grace. 4157 * It returns 0 if a reclaim is allowed, 1 otherwise. 4158 */ 4159 static int 4160 nfsrv_checkstable(struct nfsclient *clp) 4161 { 4162 struct nfsrv_stable *sp; 4163 4164 /* 4165 * First, find the entry for the client. 4166 */ 4167 LIST_FOREACH(sp, &nfsrv_stablefirst.nsf_head, nst_list) { 4168 if (sp->nst_len == clp->lc_idlen && 4169 !NFSBCMP(sp->nst_client, clp->lc_id, sp->nst_len)) 4170 break; 4171 } 4172 4173 /* 4174 * If not in the list, state was revoked or no state was issued 4175 * since the previous reboot, a reclaim is denied. 4176 */ 4177 if (sp == LIST_END(&nfsrv_stablefirst.nsf_head) || 4178 (sp->nst_flag & NFSNST_REVOKE) || 4179 !(nfsrv_stablefirst.nsf_flags & NFSNSF_OK)) 4180 return (1); 4181 return (0); 4182 } 4183 4184 /* 4185 * Test for and try to clear out a conflicting client. This is called by 4186 * nfsrv_lockctrl() and nfsrv_openctrl() when conflicts with other clients 4187 * a found. 4188 * The trick here is that it can't revoke a conflicting client with an 4189 * expired lease unless it holds the v4root lock, so... 4190 * If no v4root lock, get the lock and return 1 to indicate "try again". 4191 * Return 0 to indicate the conflict can't be revoked and 1 to indicate 4192 * the revocation worked and the conflicting client is "bye, bye", so it 4193 * can be tried again. 4194 * Unlocks State before a non-zero value is returned. 4195 */ 4196 static int 4197 nfsrv_clientconflict(struct nfsclient *clp, int *haslockp, __unused vnode_t vp, 4198 NFSPROC_T *p) 4199 { 4200 int gotlock; 4201 4202 /* 4203 * If lease hasn't expired, we can't fix it. 4204 */ 4205 if (clp->lc_expiry >= NFSD_MONOSEC || 4206 !(nfsrv_stablefirst.nsf_flags & NFSNSF_UPDATEDONE)) 4207 return (0); 4208 if (*haslockp == 0) { 4209 NFSUNLOCKSTATE(); 4210 NFSVOPUNLOCK(vp, 0, p); 4211 NFSLOCKV4ROOTMUTEX(); 4212 nfsv4_relref(&nfsv4rootfs_lock); 4213 do { 4214 gotlock = nfsv4_lock(&nfsv4rootfs_lock, 1, NULL, 4215 NFSV4ROOTLOCKMUTEXPTR); 4216 } while (!gotlock); 4217 NFSUNLOCKV4ROOTMUTEX(); 4218 NFSLOCKSTATE(); /* to avoid a race with */ 4219 NFSUNLOCKSTATE(); /* nfsrv_servertimer() */ 4220 *haslockp = 1; 4221 NFSVOPLOCK(vp, LK_EXCLUSIVE | LK_RETRY, p); 4222 return (1); 4223 } 4224 NFSUNLOCKSTATE(); 4225 4226 /* 4227 * Ok, we can expire the conflicting client. 4228 */ 4229 nfsrv_writestable(clp->lc_id, clp->lc_idlen, NFSNST_REVOKE, p); 4230 nfsrv_cleanclient(clp, p); 4231 nfsrv_freedeleglist(&clp->lc_deleg); 4232 nfsrv_freedeleglist(&clp->lc_olddeleg); 4233 LIST_REMOVE(clp, lc_hash); 4234 nfsrv_zapclient(clp, p); 4235 return (1); 4236 } 4237 4238 4239 /* 4240 * Resolve a delegation conflict. 4241 * Returns 0 to indicate the conflict was resolved without sleeping. 4242 * Return -1 to indicate that the caller should check for conflicts again. 4243 * Return > 0 for an error that should be returned, normally NFSERR_DELAY. 4244 * 4245 * Also, manipulate the nfsv4root_lock, as required. It isn't changed 4246 * for a return of 0, since there was no sleep and it could be required 4247 * later. It is released for a return of NFSERR_DELAY, since the caller 4248 * will return that error. It is released when a sleep was done waiting 4249 * for the delegation to be returned or expire (so that other nfsds can 4250 * handle ops). Then, it must be acquired for the write to stable storage. 4251 * (This function is somewhat similar to nfsrv_clientconflict(), but 4252 * the semantics differ in a couple of subtle ways. The return of 0 4253 * indicates the conflict was resolved without sleeping here, not 4254 * that the conflict can't be resolved and the handling of nfsv4root_lock 4255 * differs, as noted above.) 4256 * Unlocks State before returning a non-zero value. 4257 */ 4258 static int 4259 nfsrv_delegconflict(struct nfsstate *stp, int *haslockp, NFSPROC_T *p, 4260 __unused vnode_t vp) 4261 { 4262 struct nfsclient *clp = stp->ls_clp; 4263 int gotlock, error, retrycnt, zapped_clp; 4264 nfsv4stateid_t tstateid; 4265 fhandle_t tfh; 4266 4267 /* 4268 * If the conflict is with an old delegation... 4269 */ 4270 if (stp->ls_flags & NFSLCK_OLDDELEG) { 4271 /* 4272 * You can delete it, if it has expired. 4273 */ 4274 if (clp->lc_delegtime < NFSD_MONOSEC) { 4275 nfsrv_freedeleg(stp); 4276 NFSUNLOCKSTATE(); 4277 return (-1); 4278 } 4279 NFSUNLOCKSTATE(); 4280 /* 4281 * During this delay, the old delegation could expire or it 4282 * could be recovered by the client via an Open with 4283 * CLAIM_DELEGATE_PREV. 4284 * Release the nfsv4root_lock, if held. 4285 */ 4286 if (*haslockp) { 4287 *haslockp = 0; 4288 NFSLOCKV4ROOTMUTEX(); 4289 nfsv4_unlock(&nfsv4rootfs_lock, 1); 4290 NFSUNLOCKV4ROOTMUTEX(); 4291 } 4292 return (NFSERR_DELAY); 4293 } 4294 4295 /* 4296 * It's a current delegation, so: 4297 * - check to see if the delegation has expired 4298 * - if so, get the v4root lock and then expire it 4299 */ 4300 if (!(stp->ls_flags & NFSLCK_DELEGRECALL)) { 4301 /* 4302 * - do a recall callback, since not yet done 4303 * For now, never allow truncate to be set. To use 4304 * truncate safely, it must be guaranteed that the 4305 * Remove, Rename or Setattr with size of 0 will 4306 * succeed and that would require major changes to 4307 * the VFS/Vnode OPs. 4308 * Set the expiry time large enough so that it won't expire 4309 * until after the callback, then set it correctly, once 4310 * the callback is done. (The delegation will now time 4311 * out whether or not the Recall worked ok. The timeout 4312 * will be extended when ops are done on the delegation 4313 * stateid, up to the timelimit.) 4314 */ 4315 stp->ls_delegtime = NFSD_MONOSEC + (2 * nfsrv_lease) + 4316 NFSRV_LEASEDELTA; 4317 stp->ls_delegtimelimit = NFSD_MONOSEC + (6 * nfsrv_lease) + 4318 NFSRV_LEASEDELTA; 4319 stp->ls_flags |= NFSLCK_DELEGRECALL; 4320 4321 /* 4322 * Loop NFSRV_CBRETRYCNT times while the CBRecall replies 4323 * NFSERR_BADSTATEID or NFSERR_BADHANDLE. This is done 4324 * in order to try and avoid a race that could happen 4325 * when a CBRecall request passed the Open reply with 4326 * the delegation in it when transitting the network. 4327 * Since nfsrv_docallback will sleep, don't use stp after 4328 * the call. 4329 */ 4330 NFSBCOPY((caddr_t)&stp->ls_stateid, (caddr_t)&tstateid, 4331 sizeof (tstateid)); 4332 NFSBCOPY((caddr_t)&stp->ls_lfp->lf_fh, (caddr_t)&tfh, 4333 sizeof (tfh)); 4334 NFSUNLOCKSTATE(); 4335 if (*haslockp) { 4336 *haslockp = 0; 4337 NFSLOCKV4ROOTMUTEX(); 4338 nfsv4_unlock(&nfsv4rootfs_lock, 1); 4339 NFSUNLOCKV4ROOTMUTEX(); 4340 } 4341 retrycnt = 0; 4342 do { 4343 error = nfsrv_docallback(clp, NFSV4OP_CBRECALL, 4344 &tstateid, 0, &tfh, NULL, NULL, p); 4345 retrycnt++; 4346 } while ((error == NFSERR_BADSTATEID || 4347 error == NFSERR_BADHANDLE) && retrycnt < NFSV4_CBRETRYCNT); 4348 return (NFSERR_DELAY); 4349 } 4350 4351 if (clp->lc_expiry >= NFSD_MONOSEC && 4352 stp->ls_delegtime >= NFSD_MONOSEC) { 4353 NFSUNLOCKSTATE(); 4354 /* 4355 * A recall has been done, but it has not yet expired. 4356 * So, RETURN_DELAY. 4357 */ 4358 if (*haslockp) { 4359 *haslockp = 0; 4360 NFSLOCKV4ROOTMUTEX(); 4361 nfsv4_unlock(&nfsv4rootfs_lock, 1); 4362 NFSUNLOCKV4ROOTMUTEX(); 4363 } 4364 return (NFSERR_DELAY); 4365 } 4366 4367 /* 4368 * If we don't yet have the lock, just get it and then return, 4369 * since we need that before deleting expired state, such as 4370 * this delegation. 4371 * When getting the lock, unlock the vnode, so other nfsds that 4372 * are in progress, won't get stuck waiting for the vnode lock. 4373 */ 4374 if (*haslockp == 0) { 4375 NFSUNLOCKSTATE(); 4376 NFSVOPUNLOCK(vp, 0, p); 4377 NFSLOCKV4ROOTMUTEX(); 4378 nfsv4_relref(&nfsv4rootfs_lock); 4379 do { 4380 gotlock = nfsv4_lock(&nfsv4rootfs_lock, 1, NULL, 4381 NFSV4ROOTLOCKMUTEXPTR); 4382 } while (!gotlock); 4383 NFSUNLOCKV4ROOTMUTEX(); 4384 NFSLOCKSTATE(); /* to avoid a race with */ 4385 NFSUNLOCKSTATE(); /* nfsrv_servertimer() */ 4386 *haslockp = 1; 4387 NFSVOPLOCK(vp, LK_EXCLUSIVE | LK_RETRY, p); 4388 return (-1); 4389 } 4390 4391 NFSUNLOCKSTATE(); 4392 /* 4393 * Ok, we can delete the expired delegation. 4394 * First, write the Revoke record to stable storage and then 4395 * clear out the conflict. 4396 * Since all other nfsd threads are now blocked, we can safely 4397 * sleep without the state changing. 4398 */ 4399 nfsrv_writestable(clp->lc_id, clp->lc_idlen, NFSNST_REVOKE, p); 4400 if (clp->lc_expiry < NFSD_MONOSEC) { 4401 nfsrv_cleanclient(clp, p); 4402 nfsrv_freedeleglist(&clp->lc_deleg); 4403 nfsrv_freedeleglist(&clp->lc_olddeleg); 4404 LIST_REMOVE(clp, lc_hash); 4405 zapped_clp = 1; 4406 } else { 4407 nfsrv_freedeleg(stp); 4408 zapped_clp = 0; 4409 } 4410 if (zapped_clp) 4411 nfsrv_zapclient(clp, p); 4412 return (-1); 4413 } 4414 4415 /* 4416 * Check for a remove allowed, if remove is set to 1 and get rid of 4417 * delegations. 4418 */ 4419 APPLESTATIC int 4420 nfsrv_checkremove(vnode_t vp, int remove, NFSPROC_T *p) 4421 { 4422 struct nfsstate *stp; 4423 struct nfslockfile *lfp; 4424 int error, haslock = 0; 4425 fhandle_t nfh; 4426 4427 /* 4428 * First, get the lock file structure. 4429 * (A return of -1 means no associated state, so remove ok.) 4430 */ 4431 error = nfsrv_getlockfh(vp, NFSLCK_CHECK, NULL, &nfh, p); 4432 tryagain: 4433 NFSLOCKSTATE(); 4434 if (!error) 4435 error = nfsrv_getlockfile(NFSLCK_CHECK, NULL, &lfp, &nfh, 0); 4436 if (error) { 4437 NFSUNLOCKSTATE(); 4438 if (haslock) { 4439 NFSLOCKV4ROOTMUTEX(); 4440 nfsv4_unlock(&nfsv4rootfs_lock, 1); 4441 NFSUNLOCKV4ROOTMUTEX(); 4442 } 4443 if (error == -1) 4444 return (0); 4445 return (error); 4446 } 4447 4448 /* 4449 * Now, we must Recall any delegations. 4450 */ 4451 error = nfsrv_cleandeleg(vp, lfp, NULL, &haslock, p); 4452 if (error) { 4453 /* 4454 * nfsrv_cleandeleg() unlocks state for non-zero 4455 * return. 4456 */ 4457 if (error == -1) 4458 goto tryagain; 4459 if (haslock) { 4460 NFSLOCKV4ROOTMUTEX(); 4461 nfsv4_unlock(&nfsv4rootfs_lock, 1); 4462 NFSUNLOCKV4ROOTMUTEX(); 4463 } 4464 return (error); 4465 } 4466 4467 /* 4468 * Now, look for a conflicting open share. 4469 */ 4470 if (remove) { 4471 LIST_FOREACH(stp, &lfp->lf_open, ls_file) { 4472 if (stp->ls_flags & NFSLCK_WRITEDENY) { 4473 error = NFSERR_FILEOPEN; 4474 break; 4475 } 4476 } 4477 } 4478 4479 NFSUNLOCKSTATE(); 4480 if (haslock) { 4481 NFSLOCKV4ROOTMUTEX(); 4482 nfsv4_unlock(&nfsv4rootfs_lock, 1); 4483 NFSUNLOCKV4ROOTMUTEX(); 4484 } 4485 return (error); 4486 } 4487 4488 /* 4489 * Clear out all delegations for the file referred to by lfp. 4490 * May return NFSERR_DELAY, if there will be a delay waiting for 4491 * delegations to expire. 4492 * Returns -1 to indicate it slept while recalling a delegation. 4493 * This function has the side effect of deleting the nfslockfile structure, 4494 * if it no longer has associated state and didn't have to sleep. 4495 * Unlocks State before a non-zero value is returned. 4496 */ 4497 static int 4498 nfsrv_cleandeleg(vnode_t vp, struct nfslockfile *lfp, 4499 struct nfsclient *clp, int *haslockp, NFSPROC_T *p) 4500 { 4501 struct nfsstate *stp, *nstp; 4502 int ret; 4503 4504 stp = LIST_FIRST(&lfp->lf_deleg); 4505 while (stp != LIST_END(&lfp->lf_deleg)) { 4506 nstp = LIST_NEXT(stp, ls_file); 4507 if (stp->ls_clp != clp) { 4508 ret = nfsrv_delegconflict(stp, haslockp, p, vp); 4509 if (ret) { 4510 /* 4511 * nfsrv_delegconflict() unlocks state 4512 * when it returns non-zero. 4513 */ 4514 return (ret); 4515 } 4516 } 4517 stp = nstp; 4518 } 4519 return (0); 4520 } 4521 4522 /* 4523 * There are certain operations that, when being done outside of NFSv4, 4524 * require that any NFSv4 delegation for the file be recalled. 4525 * This function is to be called for those cases: 4526 * VOP_RENAME() - When a delegation is being recalled for any reason, 4527 * the client may have to do Opens against the server, using the file's 4528 * final component name. If the file has been renamed on the server, 4529 * that component name will be incorrect and the Open will fail. 4530 * VOP_REMOVE() - Theoretically, a client could Open a file after it has 4531 * been removed on the server, if there is a delegation issued to 4532 * that client for the file. I say "theoretically" since clients 4533 * normally do an Access Op before the Open and that Access Op will 4534 * fail with ESTALE. Note that NFSv2 and 3 don't even do Opens, so 4535 * they will detect the file's removal in the same manner. (There is 4536 * one case where RFC3530 allows a client to do an Open without first 4537 * doing an Access Op, which is passage of a check against the ACE 4538 * returned with a Write delegation, but current practice is to ignore 4539 * the ACE and always do an Access Op.) 4540 * Since the functions can only be called with an unlocked vnode, this 4541 * can't be done at this time. 4542 * VOP_ADVLOCK() - When a client holds a delegation, it can issue byte range 4543 * locks locally in the client, which are not visible to the server. To 4544 * deal with this, issuing of delegations for a vnode must be disabled 4545 * and all delegations for the vnode recalled. This is done via the 4546 * second function, using the VV_DISABLEDELEG vflag on the vnode. 4547 */ 4548 APPLESTATIC void 4549 nfsd_recalldelegation(vnode_t vp, NFSPROC_T *p) 4550 { 4551 struct timespec mytime; 4552 int32_t starttime; 4553 int error; 4554 4555 KASSERT(!VOP_ISLOCKED(vp), ("vp %p is locked", vp)); 4556 4557 /* 4558 * First, check to see if the server is currently running and it has 4559 * been called for a regular file when issuing delegations. 4560 */ 4561 if (newnfs_numnfsd == 0 || vp->v_type != VREG || 4562 nfsrv_issuedelegs == 0) 4563 return; 4564 4565 /* 4566 * Now, call nfsrv_checkremove() in a loop while it returns 4567 * NFSERR_DELAY. Return upon any other error or when timed out. 4568 */ 4569 NFSGETNANOTIME(&mytime); 4570 starttime = (u_int32_t)mytime.tv_sec; 4571 do { 4572 error = nfsrv_checkremove(vp, 0, p); 4573 if (error == NFSERR_DELAY) { 4574 NFSGETNANOTIME(&mytime); 4575 if (((u_int32_t)mytime.tv_sec - starttime) > 4576 NFS_REMOVETIMEO && 4577 ((u_int32_t)mytime.tv_sec - starttime) < 4578 100000) 4579 return; 4580 /* Sleep for a short period of time */ 4581 (void) nfs_catnap(PZERO, 0, "nfsremove"); 4582 } 4583 } while (error == NFSERR_DELAY); 4584 } 4585 4586 APPLESTATIC void 4587 nfsd_disabledelegation(vnode_t vp, NFSPROC_T *p) 4588 { 4589 4590 #ifdef VV_DISABLEDELEG 4591 /* 4592 * First, flag issuance of delegations disabled. 4593 */ 4594 atomic_set_long(&vp->v_vflag, VV_DISABLEDELEG); 4595 #endif 4596 4597 /* 4598 * Then call nfsd_recalldelegation() to get rid of all extant 4599 * delegations. 4600 */ 4601 nfsd_recalldelegation(vp, p); 4602 } 4603 4604 /* 4605 * Check for conflicting locks, etc. and then get rid of delegations. 4606 * (At one point I thought that I should get rid of delegations for any 4607 * Setattr, since it could potentially disallow the I/O op (read or write) 4608 * allowed by the delegation. However, Setattr Ops that aren't changing 4609 * the size get a stateid of all 0s, so you can't tell if it is a delegation 4610 * for the same client or a different one, so I decided to only get rid 4611 * of delegations for other clients when the size is being changed.) 4612 * In general, a Setattr can disable NFS I/O Ops that are outstanding, such 4613 * as Write backs, even if there is no delegation, so it really isn't any 4614 * different?) 4615 */ 4616 APPLESTATIC int 4617 nfsrv_checksetattr(vnode_t vp, struct nfsrv_descript *nd, 4618 nfsv4stateid_t *stateidp, struct nfsvattr *nvap, nfsattrbit_t *attrbitp, 4619 struct nfsexstuff *exp, NFSPROC_T *p) 4620 { 4621 struct nfsstate st, *stp = &st; 4622 struct nfslock lo, *lop = &lo; 4623 int error = 0; 4624 nfsquad_t clientid; 4625 4626 if (NFSISSET_ATTRBIT(attrbitp, NFSATTRBIT_SIZE)) { 4627 stp->ls_flags = (NFSLCK_CHECK | NFSLCK_WRITEACCESS); 4628 lop->lo_first = nvap->na_size; 4629 } else { 4630 stp->ls_flags = 0; 4631 lop->lo_first = 0; 4632 } 4633 if (NFSISSET_ATTRBIT(attrbitp, NFSATTRBIT_OWNER) || 4634 NFSISSET_ATTRBIT(attrbitp, NFSATTRBIT_OWNERGROUP) || 4635 NFSISSET_ATTRBIT(attrbitp, NFSATTRBIT_MODE) || 4636 NFSISSET_ATTRBIT(attrbitp, NFSATTRBIT_ACL)) 4637 stp->ls_flags |= NFSLCK_SETATTR; 4638 if (stp->ls_flags == 0) 4639 return (0); 4640 lop->lo_end = NFS64BITSSET; 4641 lop->lo_flags = NFSLCK_WRITE; 4642 stp->ls_ownerlen = 0; 4643 stp->ls_op = NULL; 4644 stp->ls_uid = nd->nd_cred->cr_uid; 4645 stp->ls_stateid.seqid = stateidp->seqid; 4646 clientid.lval[0] = stp->ls_stateid.other[0] = stateidp->other[0]; 4647 clientid.lval[1] = stp->ls_stateid.other[1] = stateidp->other[1]; 4648 stp->ls_stateid.other[2] = stateidp->other[2]; 4649 error = nfsrv_lockctrl(vp, &stp, &lop, NULL, clientid, 4650 stateidp, exp, nd, p); 4651 return (error); 4652 } 4653 4654 /* 4655 * Check for a write delegation and do a CBGETATTR if there is one, updating 4656 * the attributes, as required. 4657 * Should I return an error if I can't get the attributes? (For now, I'll 4658 * just return ok. 4659 */ 4660 APPLESTATIC int 4661 nfsrv_checkgetattr(struct nfsrv_descript *nd, vnode_t vp, 4662 struct nfsvattr *nvap, nfsattrbit_t *attrbitp, struct ucred *cred, 4663 NFSPROC_T *p) 4664 { 4665 struct nfsstate *stp; 4666 struct nfslockfile *lfp; 4667 struct nfsclient *clp; 4668 struct nfsvattr nva; 4669 fhandle_t nfh; 4670 int error; 4671 nfsattrbit_t cbbits; 4672 u_quad_t delegfilerev; 4673 4674 NFSCBGETATTR_ATTRBIT(attrbitp, &cbbits); 4675 if (!NFSNONZERO_ATTRBIT(&cbbits)) 4676 return (0); 4677 4678 /* 4679 * Get the lock file structure. 4680 * (A return of -1 means no associated state, so return ok.) 4681 */ 4682 error = nfsrv_getlockfh(vp, NFSLCK_CHECK, NULL, &nfh, p); 4683 NFSLOCKSTATE(); 4684 if (!error) 4685 error = nfsrv_getlockfile(NFSLCK_CHECK, NULL, &lfp, &nfh, 0); 4686 if (error) { 4687 NFSUNLOCKSTATE(); 4688 if (error == -1) 4689 return (0); 4690 return (error); 4691 } 4692 4693 /* 4694 * Now, look for a write delegation. 4695 */ 4696 LIST_FOREACH(stp, &lfp->lf_deleg, ls_file) { 4697 if (stp->ls_flags & NFSLCK_DELEGWRITE) 4698 break; 4699 } 4700 if (stp == LIST_END(&lfp->lf_deleg)) { 4701 NFSUNLOCKSTATE(); 4702 return (0); 4703 } 4704 clp = stp->ls_clp; 4705 delegfilerev = stp->ls_filerev; 4706 4707 /* 4708 * If the Write delegation was issued as a part of this Compound RPC 4709 * or if we have an Implied Clientid (used in a previous Op in this 4710 * compound) and it is the client the delegation was issued to, 4711 * just return ok. 4712 * I also assume that it is from the same client iff the network 4713 * host IP address is the same as the callback address. (Not 4714 * exactly correct by the RFC, but avoids a lot of Getattr 4715 * callbacks.) 4716 */ 4717 if (nd->nd_compref == stp->ls_compref || 4718 ((nd->nd_flag & ND_IMPLIEDCLID) && 4719 clp->lc_clientid.qval == nd->nd_clientid.qval) || 4720 nfsaddr2_match(clp->lc_req.nr_nam, nd->nd_nam)) { 4721 NFSUNLOCKSTATE(); 4722 return (0); 4723 } 4724 4725 /* 4726 * We are now done with the delegation state structure, 4727 * so the statelock can be released and we can now tsleep(). 4728 */ 4729 4730 /* 4731 * Now, we must do the CB Getattr callback, to see if Change or Size 4732 * has changed. 4733 */ 4734 if (clp->lc_expiry >= NFSD_MONOSEC) { 4735 NFSUNLOCKSTATE(); 4736 NFSVNO_ATTRINIT(&nva); 4737 nva.na_filerev = NFS64BITSSET; 4738 error = nfsrv_docallback(clp, NFSV4OP_CBGETATTR, NULL, 4739 0, &nfh, &nva, &cbbits, p); 4740 if (!error) { 4741 if ((nva.na_filerev != NFS64BITSSET && 4742 nva.na_filerev > delegfilerev) || 4743 (NFSVNO_ISSETSIZE(&nva) && 4744 nva.na_size != nvap->na_size)) { 4745 nfsvno_updfilerev(vp, nvap, cred, p); 4746 if (NFSVNO_ISSETSIZE(&nva)) 4747 nvap->na_size = nva.na_size; 4748 } 4749 } 4750 } else { 4751 NFSUNLOCKSTATE(); 4752 } 4753 return (0); 4754 } 4755 4756 /* 4757 * This function looks for openowners that haven't had any opens for 4758 * a while and throws them away. Called by an nfsd when NFSNSF_NOOPENS 4759 * is set. 4760 */ 4761 APPLESTATIC void 4762 nfsrv_throwawayopens(NFSPROC_T *p) 4763 { 4764 struct nfsclient *clp, *nclp; 4765 struct nfsstate *stp, *nstp; 4766 int i; 4767 4768 NFSLOCKSTATE(); 4769 nfsrv_stablefirst.nsf_flags &= ~NFSNSF_NOOPENS; 4770 /* 4771 * For each client... 4772 */ 4773 for (i = 0; i < NFSCLIENTHASHSIZE; i++) { 4774 LIST_FOREACH_SAFE(clp, &nfsclienthash[i], lc_hash, nclp) { 4775 LIST_FOREACH_SAFE(stp, &clp->lc_open, ls_list, nstp) { 4776 if (LIST_EMPTY(&stp->ls_open) && 4777 (stp->ls_noopens > NFSNOOPEN || 4778 (nfsrv_openpluslock * 2) > 4779 NFSRV_V4STATELIMIT)) 4780 nfsrv_freeopenowner(stp, 0, p); 4781 } 4782 } 4783 } 4784 NFSUNLOCKSTATE(); 4785 } 4786 4787 /* 4788 * This function checks to see if the credentials are the same. 4789 * Returns 1 for not same, 0 otherwise. 4790 */ 4791 static int 4792 nfsrv_notsamecredname(struct nfsrv_descript *nd, struct nfsclient *clp) 4793 { 4794 4795 if (nd->nd_flag & ND_GSS) { 4796 if (!(clp->lc_flags & LCL_GSS)) 4797 return (1); 4798 if (clp->lc_flags & LCL_NAME) { 4799 if (nd->nd_princlen != clp->lc_namelen || 4800 NFSBCMP(nd->nd_principal, clp->lc_name, 4801 clp->lc_namelen)) 4802 return (1); 4803 else 4804 return (0); 4805 } 4806 if (nd->nd_cred->cr_uid == clp->lc_uid) 4807 return (0); 4808 else 4809 return (1); 4810 } else if (clp->lc_flags & LCL_GSS) 4811 return (1); 4812 /* 4813 * For AUTH_SYS, allow the same uid or root. (This is underspecified 4814 * in RFC3530, which talks about principals, but doesn't say anything 4815 * about uids for AUTH_SYS.) 4816 */ 4817 if (nd->nd_cred->cr_uid == clp->lc_uid || nd->nd_cred->cr_uid == 0) 4818 return (0); 4819 else 4820 return (1); 4821 } 4822 4823 /* 4824 * Calculate the lease expiry time. 4825 */ 4826 static time_t 4827 nfsrv_leaseexpiry(void) 4828 { 4829 struct timeval curtime; 4830 4831 NFSGETTIME(&curtime); 4832 if (nfsrv_stablefirst.nsf_eograce > NFSD_MONOSEC) 4833 return (NFSD_MONOSEC + 2 * (nfsrv_lease + NFSRV_LEASEDELTA)); 4834 return (NFSD_MONOSEC + nfsrv_lease + NFSRV_LEASEDELTA); 4835 } 4836 4837 /* 4838 * Delay the delegation timeout as far as ls_delegtimelimit, as required. 4839 */ 4840 static void 4841 nfsrv_delaydelegtimeout(struct nfsstate *stp) 4842 { 4843 4844 if ((stp->ls_flags & NFSLCK_DELEGRECALL) == 0) 4845 return; 4846 4847 if ((stp->ls_delegtime + 15) > NFSD_MONOSEC && 4848 stp->ls_delegtime < stp->ls_delegtimelimit) { 4849 stp->ls_delegtime += nfsrv_lease; 4850 if (stp->ls_delegtime > stp->ls_delegtimelimit) 4851 stp->ls_delegtime = stp->ls_delegtimelimit; 4852 } 4853 } 4854 4855 /* 4856 * This function checks to see if there is any other state associated 4857 * with the openowner for this Open. 4858 * It returns 1 if there is no other state, 0 otherwise. 4859 */ 4860 static int 4861 nfsrv_nootherstate(struct nfsstate *stp) 4862 { 4863 struct nfsstate *tstp; 4864 4865 LIST_FOREACH(tstp, &stp->ls_openowner->ls_open, ls_list) { 4866 if (tstp != stp || !LIST_EMPTY(&tstp->ls_lock)) 4867 return (0); 4868 } 4869 return (1); 4870 } 4871 4872 /* 4873 * Create a list of lock deltas (changes to local byte range locking 4874 * that can be rolled back using the list) and apply the changes via 4875 * nfsvno_advlock(). Optionally, lock the list. It is expected that either 4876 * the rollback or update function will be called after this. 4877 * It returns an error (and rolls back, as required), if any nfsvno_advlock() 4878 * call fails. If it returns an error, it will unlock the list. 4879 */ 4880 static int 4881 nfsrv_locallock(vnode_t vp, struct nfslockfile *lfp, int flags, 4882 uint64_t first, uint64_t end, struct nfslockconflict *cfp, NFSPROC_T *p) 4883 { 4884 struct nfslock *lop, *nlop; 4885 int error = 0; 4886 4887 /* Loop through the list of locks. */ 4888 lop = LIST_FIRST(&lfp->lf_locallock); 4889 while (first < end && lop != NULL) { 4890 nlop = LIST_NEXT(lop, lo_lckowner); 4891 if (first >= lop->lo_end) { 4892 /* not there yet */ 4893 lop = nlop; 4894 } else if (first < lop->lo_first) { 4895 /* new one starts before entry in list */ 4896 if (end <= lop->lo_first) { 4897 /* no overlap between old and new */ 4898 error = nfsrv_dolocal(vp, lfp, flags, 4899 NFSLCK_UNLOCK, first, end, cfp, p); 4900 if (error != 0) 4901 break; 4902 first = end; 4903 } else { 4904 /* handle fragment overlapped with new one */ 4905 error = nfsrv_dolocal(vp, lfp, flags, 4906 NFSLCK_UNLOCK, first, lop->lo_first, cfp, 4907 p); 4908 if (error != 0) 4909 break; 4910 first = lop->lo_first; 4911 } 4912 } else { 4913 /* new one overlaps this entry in list */ 4914 if (end <= lop->lo_end) { 4915 /* overlaps all of new one */ 4916 error = nfsrv_dolocal(vp, lfp, flags, 4917 lop->lo_flags, first, end, cfp, p); 4918 if (error != 0) 4919 break; 4920 first = end; 4921 } else { 4922 /* handle fragment overlapped with new one */ 4923 error = nfsrv_dolocal(vp, lfp, flags, 4924 lop->lo_flags, first, lop->lo_end, cfp, p); 4925 if (error != 0) 4926 break; 4927 first = lop->lo_end; 4928 lop = nlop; 4929 } 4930 } 4931 } 4932 if (first < end && error == 0) 4933 /* handle fragment past end of list */ 4934 error = nfsrv_dolocal(vp, lfp, flags, NFSLCK_UNLOCK, first, 4935 end, cfp, p); 4936 return (error); 4937 } 4938 4939 /* 4940 * Local lock unlock. Unlock all byte ranges that are no longer locked 4941 * by NFSv4. 4942 */ 4943 static void 4944 nfsrv_localunlock(vnode_t vp, struct nfslockfile *lfp, uint64_t init_first, 4945 uint64_t init_end, NFSPROC_T *p) 4946 { 4947 struct nfslock *lop; 4948 4949 uint64_t first, end; 4950 4951 first = init_first; 4952 end = init_end; 4953 while (first < init_end) { 4954 /* Loop through all nfs locks, adjusting first and end */ 4955 LIST_FOREACH(lop, &lfp->lf_lock, lo_lckfile) { 4956 if (first >= lop->lo_first && 4957 first < lop->lo_end) 4958 /* Overlaps initial part */ 4959 first = lop->lo_end; 4960 else if (end > lop->lo_first && 4961 lop->lo_first >= first) 4962 /* Begins before end and past first */ 4963 end = lop->lo_first; 4964 if (first >= end) 4965 /* shrunk to 0 so this iteration is done */ 4966 break; 4967 } 4968 if (first < end) { 4969 /* Unlock this segment */ 4970 (void) nfsrv_dolocal(vp, lfp, NFSLCK_UNLOCK, 4971 NFSLCK_READ, first, end, NULL, p); 4972 nfsrv_locallock_commit(lfp, NFSLCK_UNLOCK, 4973 first, end); 4974 } 4975 /* and move on to the rest of the range */ 4976 first = end; 4977 end = init_end; 4978 } 4979 } 4980 4981 /* 4982 * Do the local lock operation and update the rollback list, as required. 4983 * Perform the rollback and return the error if nfsvno_advlock() fails. 4984 */ 4985 static int 4986 nfsrv_dolocal(vnode_t vp, struct nfslockfile *lfp, int flags, int oldflags, 4987 uint64_t first, uint64_t end, struct nfslockconflict *cfp, NFSPROC_T *p) 4988 { 4989 struct nfsrollback *rlp; 4990 int error, ltype, oldltype; 4991 4992 if (flags & NFSLCK_WRITE) 4993 ltype = F_WRLCK; 4994 else if (flags & NFSLCK_READ) 4995 ltype = F_RDLCK; 4996 else 4997 ltype = F_UNLCK; 4998 if (oldflags & NFSLCK_WRITE) 4999 oldltype = F_WRLCK; 5000 else if (oldflags & NFSLCK_READ) 5001 oldltype = F_RDLCK; 5002 else 5003 oldltype = F_UNLCK; 5004 if (ltype == oldltype || (oldltype == F_WRLCK && ltype == F_RDLCK)) 5005 /* nothing to do */ 5006 return (0); 5007 error = nfsvno_advlock(vp, ltype, first, end, p); 5008 if (error != 0) { 5009 if (cfp != NULL) { 5010 cfp->cl_clientid.lval[0] = 0; 5011 cfp->cl_clientid.lval[1] = 0; 5012 cfp->cl_first = 0; 5013 cfp->cl_end = NFS64BITSSET; 5014 cfp->cl_flags = NFSLCK_WRITE; 5015 cfp->cl_ownerlen = 5; 5016 NFSBCOPY("LOCAL", cfp->cl_owner, 5); 5017 } 5018 nfsrv_locallock_rollback(vp, lfp, p); 5019 } else if (ltype != F_UNLCK) { 5020 rlp = malloc(sizeof (struct nfsrollback), M_NFSDROLLBACK, 5021 M_WAITOK); 5022 rlp->rlck_first = first; 5023 rlp->rlck_end = end; 5024 rlp->rlck_type = oldltype; 5025 LIST_INSERT_HEAD(&lfp->lf_rollback, rlp, rlck_list); 5026 } 5027 return (error); 5028 } 5029 5030 /* 5031 * Roll back local lock changes and free up the rollback list. 5032 */ 5033 static void 5034 nfsrv_locallock_rollback(vnode_t vp, struct nfslockfile *lfp, NFSPROC_T *p) 5035 { 5036 struct nfsrollback *rlp, *nrlp; 5037 5038 LIST_FOREACH_SAFE(rlp, &lfp->lf_rollback, rlck_list, nrlp) { 5039 (void) nfsvno_advlock(vp, rlp->rlck_type, rlp->rlck_first, 5040 rlp->rlck_end, p); 5041 free(rlp, M_NFSDROLLBACK); 5042 } 5043 LIST_INIT(&lfp->lf_rollback); 5044 } 5045 5046 /* 5047 * Update local lock list and delete rollback list (ie now committed to the 5048 * local locks). Most of the work is done by the internal function. 5049 */ 5050 static void 5051 nfsrv_locallock_commit(struct nfslockfile *lfp, int flags, uint64_t first, 5052 uint64_t end) 5053 { 5054 struct nfsrollback *rlp, *nrlp; 5055 struct nfslock *new_lop, *other_lop; 5056 5057 new_lop = malloc(sizeof (struct nfslock), M_NFSDLOCK, M_WAITOK); 5058 if (flags & (NFSLCK_READ | NFSLCK_WRITE)) 5059 other_lop = malloc(sizeof (struct nfslock), M_NFSDLOCK, 5060 M_WAITOK); 5061 else 5062 other_lop = NULL; 5063 new_lop->lo_flags = flags; 5064 new_lop->lo_first = first; 5065 new_lop->lo_end = end; 5066 nfsrv_updatelock(NULL, &new_lop, &other_lop, lfp); 5067 if (new_lop != NULL) 5068 free(new_lop, M_NFSDLOCK); 5069 if (other_lop != NULL) 5070 free(other_lop, M_NFSDLOCK); 5071 5072 /* and get rid of the rollback list */ 5073 LIST_FOREACH_SAFE(rlp, &lfp->lf_rollback, rlck_list, nrlp) 5074 free(rlp, M_NFSDROLLBACK); 5075 LIST_INIT(&lfp->lf_rollback); 5076 } 5077 5078 /* 5079 * Lock the struct nfslockfile for local lock updating. 5080 */ 5081 static void 5082 nfsrv_locklf(struct nfslockfile *lfp) 5083 { 5084 int gotlock; 5085 5086 /* lf_usecount ensures *lfp won't be free'd */ 5087 lfp->lf_usecount++; 5088 do { 5089 gotlock = nfsv4_lock(&lfp->lf_locallock_lck, 1, NULL, 5090 NFSSTATEMUTEXPTR); 5091 } while (gotlock == 0); 5092 lfp->lf_usecount--; 5093 } 5094 5095 /* 5096 * Unlock the struct nfslockfile after local lock updating. 5097 */ 5098 static void 5099 nfsrv_unlocklf(struct nfslockfile *lfp) 5100 { 5101 5102 nfsv4_unlock(&lfp->lf_locallock_lck, 0); 5103 } 5104 5105