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