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