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