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