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