1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD 3 * 4 * Copyright (c) 2009 Rick Macklem, University of Guelph 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26 * SUCH DAMAGE. 27 * 28 */ 29 30 #include <sys/cdefs.h> 31 __FBSDID("$FreeBSD$"); 32 33 #include "opt_inet.h" 34 #include "opt_inet6.h" 35 #include <sys/extattr.h> 36 #include <fs/nfs/nfsport.h> 37 38 struct nfsrv_stablefirst nfsrv_stablefirst; 39 int nfsrv_issuedelegs = 0; 40 int nfsrv_dolocallocks = 0; 41 struct nfsv4lock nfsv4rootfs_lock; 42 time_t nfsdev_time = 0; 43 int nfsrv_layouthashsize; 44 volatile int nfsrv_layoutcnt = 0; 45 extern uint32_t nfs_srvmaxio; 46 47 extern int newnfs_numnfsd; 48 extern struct nfsstatsv1 nfsstatsv1; 49 extern int nfsrv_lease; 50 extern struct timeval nfsboottime; 51 extern u_int32_t newnfs_true, newnfs_false; 52 extern struct mtx nfsrv_dslock_mtx; 53 extern struct mtx nfsrv_recalllock_mtx; 54 extern struct mtx nfsrv_dontlistlock_mtx; 55 extern int nfsd_debuglevel; 56 extern u_int nfsrv_dsdirsize; 57 extern struct nfsdevicehead nfsrv_devidhead; 58 extern int nfsrv_doflexfile; 59 extern int nfsrv_maxpnfsmirror; 60 NFSV4ROOTLOCKMUTEX; 61 NFSSTATESPINLOCK; 62 extern struct nfsdontlisthead nfsrv_dontlisthead; 63 extern volatile int nfsrv_devidcnt; 64 extern struct nfslayouthead nfsrv_recalllisthead; 65 extern char *nfsrv_zeropnfsdat; 66 67 SYSCTL_DECL(_vfs_nfsd); 68 int nfsrv_statehashsize = NFSSTATEHASHSIZE; 69 SYSCTL_INT(_vfs_nfsd, OID_AUTO, statehashsize, CTLFLAG_RDTUN, 70 &nfsrv_statehashsize, 0, 71 "Size of state hash table set via loader.conf"); 72 73 int nfsrv_clienthashsize = NFSCLIENTHASHSIZE; 74 SYSCTL_INT(_vfs_nfsd, OID_AUTO, clienthashsize, CTLFLAG_RDTUN, 75 &nfsrv_clienthashsize, 0, 76 "Size of client hash table set via loader.conf"); 77 78 int nfsrv_lockhashsize = NFSLOCKHASHSIZE; 79 SYSCTL_INT(_vfs_nfsd, OID_AUTO, fhhashsize, CTLFLAG_RDTUN, 80 &nfsrv_lockhashsize, 0, 81 "Size of file handle hash table set via loader.conf"); 82 83 int nfsrv_sessionhashsize = NFSSESSIONHASHSIZE; 84 SYSCTL_INT(_vfs_nfsd, OID_AUTO, sessionhashsize, CTLFLAG_RDTUN, 85 &nfsrv_sessionhashsize, 0, 86 "Size of session hash table set via loader.conf"); 87 88 int nfsrv_layouthighwater = NFSLAYOUTHIGHWATER; 89 SYSCTL_INT(_vfs_nfsd, OID_AUTO, layouthighwater, CTLFLAG_RDTUN, 90 &nfsrv_layouthighwater, 0, 91 "High water mark for number of layouts set via loader.conf"); 92 93 static int nfsrv_v4statelimit = NFSRV_V4STATELIMIT; 94 SYSCTL_INT(_vfs_nfsd, OID_AUTO, v4statelimit, CTLFLAG_RWTUN, 95 &nfsrv_v4statelimit, 0, 96 "High water limit for NFSv4 opens+locks+delegations"); 97 98 static int nfsrv_writedelegifpos = 0; 99 SYSCTL_INT(_vfs_nfsd, OID_AUTO, writedelegifpos, CTLFLAG_RW, 100 &nfsrv_writedelegifpos, 0, 101 "Issue a write delegation for read opens if possible"); 102 103 static int nfsrv_allowreadforwriteopen = 1; 104 SYSCTL_INT(_vfs_nfsd, OID_AUTO, allowreadforwriteopen, CTLFLAG_RW, 105 &nfsrv_allowreadforwriteopen, 0, 106 "Allow Reads to be done with Write Access StateIDs"); 107 108 int nfsrv_pnfsatime = 0; 109 SYSCTL_INT(_vfs_nfsd, OID_AUTO, pnfsstrictatime, CTLFLAG_RW, 110 &nfsrv_pnfsatime, 0, 111 "For pNFS service, do Getattr ops to keep atime up-to-date"); 112 113 int nfsrv_flexlinuxhack = 0; 114 SYSCTL_INT(_vfs_nfsd, OID_AUTO, flexlinuxhack, CTLFLAG_RW, 115 &nfsrv_flexlinuxhack, 0, 116 "For Linux clients, hack around Flex File Layout bug"); 117 118 /* 119 * Hash lists for nfs V4. 120 */ 121 struct nfsclienthashhead *nfsclienthash; 122 struct nfslockhashhead *nfslockhash; 123 struct nfssessionhash *nfssessionhash; 124 struct nfslayouthash *nfslayouthash; 125 volatile int nfsrv_dontlistlen = 0; 126 127 static u_int32_t nfsrv_openpluslock = 0, nfsrv_delegatecnt = 0; 128 static time_t nfsrvboottime; 129 static int nfsrv_returnoldstateid = 0, nfsrv_clients = 0; 130 static int nfsrv_clienthighwater = NFSRV_CLIENTHIGHWATER; 131 static int nfsrv_nogsscallback = 0; 132 static volatile int nfsrv_writedelegcnt = 0; 133 static int nfsrv_faildscnt; 134 135 /* local functions */ 136 static void nfsrv_dumpaclient(struct nfsclient *clp, 137 struct nfsd_dumpclients *dumpp); 138 static void nfsrv_freeopenowner(struct nfsstate *stp, int cansleep, 139 NFSPROC_T *p); 140 static int nfsrv_freeopen(struct nfsstate *stp, vnode_t vp, int cansleep, 141 NFSPROC_T *p); 142 static void nfsrv_freelockowner(struct nfsstate *stp, vnode_t vp, int cansleep, 143 NFSPROC_T *p); 144 static void nfsrv_freeallnfslocks(struct nfsstate *stp, vnode_t vp, 145 int cansleep, NFSPROC_T *p); 146 static void nfsrv_freenfslock(struct nfslock *lop); 147 static void nfsrv_freenfslockfile(struct nfslockfile *lfp); 148 static void nfsrv_freedeleg(struct nfsstate *); 149 static int nfsrv_getstate(struct nfsclient *clp, nfsv4stateid_t *stateidp, 150 u_int32_t flags, struct nfsstate **stpp); 151 static void nfsrv_getowner(struct nfsstatehead *hp, struct nfsstate *new_stp, 152 struct nfsstate **stpp); 153 static int nfsrv_getlockfh(vnode_t vp, u_short flags, 154 struct nfslockfile *new_lfp, fhandle_t *nfhp, NFSPROC_T *p); 155 static int nfsrv_getlockfile(u_short flags, struct nfslockfile **new_lfpp, 156 struct nfslockfile **lfpp, fhandle_t *nfhp, int lockit); 157 static void nfsrv_insertlock(struct nfslock *new_lop, 158 struct nfslock *insert_lop, struct nfsstate *stp, struct nfslockfile *lfp); 159 static void nfsrv_updatelock(struct nfsstate *stp, struct nfslock **new_lopp, 160 struct nfslock **other_lopp, struct nfslockfile *lfp); 161 static int nfsrv_getipnumber(u_char *cp); 162 static int nfsrv_checkrestart(nfsquad_t clientid, u_int32_t flags, 163 nfsv4stateid_t *stateidp, int specialid); 164 static int nfsrv_checkgrace(struct nfsrv_descript *nd, struct nfsclient *clp, 165 u_int32_t flags); 166 static int nfsrv_docallback(struct nfsclient *clp, int procnum, 167 nfsv4stateid_t *stateidp, int trunc, fhandle_t *fhp, 168 struct nfsvattr *nap, nfsattrbit_t *attrbitp, int laytype, NFSPROC_T *p); 169 static int nfsrv_cbcallargs(struct nfsrv_descript *nd, struct nfsclient *clp, 170 uint32_t callback, int op, const char *optag, struct nfsdsession **sepp, 171 int *slotposp); 172 static u_int32_t nfsrv_nextclientindex(void); 173 static u_int32_t nfsrv_nextstateindex(struct nfsclient *clp); 174 static void nfsrv_markstable(struct nfsclient *clp); 175 static void nfsrv_markreclaim(struct nfsclient *clp); 176 static int nfsrv_checkstable(struct nfsclient *clp); 177 static int nfsrv_clientconflict(struct nfsclient *clp, int *haslockp, struct 178 vnode *vp, NFSPROC_T *p); 179 static int nfsrv_delegconflict(struct nfsstate *stp, int *haslockp, 180 NFSPROC_T *p, vnode_t vp); 181 static int nfsrv_cleandeleg(vnode_t vp, struct nfslockfile *lfp, 182 struct nfsclient *clp, int *haslockp, NFSPROC_T *p); 183 static int nfsrv_notsamecredname(struct nfsrv_descript *nd, 184 struct nfsclient *clp); 185 static time_t nfsrv_leaseexpiry(void); 186 static void nfsrv_delaydelegtimeout(struct nfsstate *stp); 187 static int nfsrv_checkseqid(struct nfsrv_descript *nd, u_int32_t seqid, 188 struct nfsstate *stp, struct nfsrvcache *op); 189 static int nfsrv_nootherstate(struct nfsstate *stp); 190 static int nfsrv_locallock(vnode_t vp, struct nfslockfile *lfp, int flags, 191 uint64_t first, uint64_t end, struct nfslockconflict *cfp, NFSPROC_T *p); 192 static void nfsrv_localunlock(vnode_t vp, struct nfslockfile *lfp, 193 uint64_t init_first, uint64_t init_end, NFSPROC_T *p); 194 static int nfsrv_dolocal(vnode_t vp, struct nfslockfile *lfp, int flags, 195 int oldflags, uint64_t first, uint64_t end, struct nfslockconflict *cfp, 196 NFSPROC_T *p); 197 static void nfsrv_locallock_rollback(vnode_t vp, struct nfslockfile *lfp, 198 NFSPROC_T *p); 199 static void nfsrv_locallock_commit(struct nfslockfile *lfp, int flags, 200 uint64_t first, uint64_t end); 201 static void nfsrv_locklf(struct nfslockfile *lfp); 202 static void nfsrv_unlocklf(struct nfslockfile *lfp); 203 static struct nfsdsession *nfsrv_findsession(uint8_t *sessionid); 204 static int nfsrv_freesession(struct nfsdsession *sep, uint8_t *sessionid); 205 static int nfsv4_setcbsequence(struct nfsrv_descript *nd, struct nfsclient *clp, 206 int dont_replycache, struct nfsdsession **sepp, int *slotposp); 207 static int nfsv4_getcbsession(struct nfsclient *clp, struct nfsdsession **sepp); 208 static int nfsrv_addlayout(struct nfsrv_descript *nd, struct nfslayout **lypp, 209 nfsv4stateid_t *stateidp, char *layp, int *layoutlenp, NFSPROC_T *p); 210 static void nfsrv_freelayout(struct nfslayouthead *lhp, struct nfslayout *lyp); 211 static void nfsrv_freelayoutlist(nfsquad_t clientid); 212 static void nfsrv_freelayouts(nfsquad_t *clid, fsid_t *fs, int laytype, 213 int iomode); 214 static void nfsrv_freealllayouts(void); 215 static void nfsrv_freedevid(struct nfsdevice *ds); 216 static int nfsrv_setdsserver(char *dspathp, char *mdspathp, NFSPROC_T *p, 217 struct nfsdevice **dsp); 218 static void nfsrv_deleteds(struct nfsdevice *fndds); 219 static void nfsrv_allocdevid(struct nfsdevice *ds, char *addr, char *dnshost); 220 static void nfsrv_freealldevids(void); 221 static void nfsrv_flexlayouterr(struct nfsrv_descript *nd, uint32_t *layp, 222 int maxcnt, NFSPROC_T *p); 223 static int nfsrv_recalllayout(nfsquad_t clid, nfsv4stateid_t *stateidp, 224 fhandle_t *fhp, struct nfslayout *lyp, int changed, int laytype, 225 NFSPROC_T *p); 226 static int nfsrv_findlayout(nfsquad_t *clientidp, fhandle_t *fhp, int laytype, 227 NFSPROC_T *, struct nfslayout **lypp); 228 static int nfsrv_fndclid(nfsquad_t *clidvec, nfsquad_t clid, int clidcnt); 229 static struct nfslayout *nfsrv_filelayout(struct nfsrv_descript *nd, int iomode, 230 fhandle_t *fhp, fhandle_t *dsfhp, char *devid, fsid_t fs); 231 static struct nfslayout *nfsrv_flexlayout(struct nfsrv_descript *nd, int iomode, 232 int mirrorcnt, fhandle_t *fhp, fhandle_t *dsfhp, char *devid, fsid_t fs); 233 static int nfsrv_dontlayout(fhandle_t *fhp); 234 static int nfsrv_createdsfile(vnode_t vp, fhandle_t *fhp, struct pnfsdsfile *pf, 235 vnode_t dvp, struct nfsdevice *ds, struct ucred *cred, NFSPROC_T *p, 236 vnode_t *tvpp); 237 static struct nfsdevice *nfsrv_findmirroredds(struct nfsmount *nmp); 238 239 /* 240 * Scan the client list for a match and either return the current one, 241 * create a new entry or return an error. 242 * If returning a non-error, the clp structure must either be linked into 243 * the client list or free'd. 244 */ 245 int 246 nfsrv_setclient(struct nfsrv_descript *nd, struct nfsclient **new_clpp, 247 nfsquad_t *clientidp, nfsquad_t *confirmp, NFSPROC_T *p) 248 { 249 struct nfsclient *clp = NULL, *new_clp = *new_clpp; 250 int i, error = 0, ret; 251 struct nfsstate *stp, *tstp; 252 #ifdef INET 253 struct sockaddr_in *sin, *rin; 254 #endif 255 #ifdef INET6 256 struct sockaddr_in6 *sin6, *rin6; 257 #endif 258 struct nfsdsession *sep, *nsep; 259 int zapit = 0, gotit, hasstate = 0, igotlock; 260 static u_int64_t confirm_index = 0; 261 262 /* 263 * Check for state resource limit exceeded. 264 */ 265 if (nfsrv_openpluslock > nfsrv_v4statelimit) { 266 error = NFSERR_RESOURCE; 267 goto out; 268 } 269 270 if (nfsrv_issuedelegs == 0 || 271 ((nd->nd_flag & ND_GSS) != 0 && nfsrv_nogsscallback != 0)) 272 /* 273 * Don't do callbacks when delegations are disabled or 274 * for AUTH_GSS unless enabled via nfsrv_nogsscallback. 275 * If establishing a callback connection is attempted 276 * when a firewall is blocking the callback path, the 277 * server may wait too long for the connect attempt to 278 * succeed during the Open. Some clients, such as Linux, 279 * may timeout and give up on the Open before the server 280 * replies. Also, since AUTH_GSS callbacks are not 281 * yet interoperability tested, they might cause the 282 * server to crap out, if they get past the Init call to 283 * the client. 284 */ 285 new_clp->lc_program = 0; 286 287 /* Lock out other nfsd threads */ 288 NFSLOCKV4ROOTMUTEX(); 289 nfsv4_relref(&nfsv4rootfs_lock); 290 do { 291 igotlock = nfsv4_lock(&nfsv4rootfs_lock, 1, NULL, 292 NFSV4ROOTLOCKMUTEXPTR, NULL); 293 } while (!igotlock); 294 NFSUNLOCKV4ROOTMUTEX(); 295 296 /* 297 * Search for a match in the client list. 298 */ 299 gotit = i = 0; 300 while (i < nfsrv_clienthashsize && !gotit) { 301 LIST_FOREACH(clp, &nfsclienthash[i], lc_hash) { 302 if (new_clp->lc_idlen == clp->lc_idlen && 303 !NFSBCMP(new_clp->lc_id, clp->lc_id, clp->lc_idlen)) { 304 gotit = 1; 305 break; 306 } 307 } 308 if (gotit == 0) 309 i++; 310 } 311 if (!gotit || 312 (clp->lc_flags & (LCL_NEEDSCONFIRM | LCL_ADMINREVOKED))) { 313 if ((nd->nd_flag & ND_NFSV41) != 0 && confirmp->lval[1] != 0) { 314 /* 315 * For NFSv4.1, if confirmp->lval[1] is non-zero, the 316 * client is trying to update a confirmed clientid. 317 */ 318 NFSLOCKV4ROOTMUTEX(); 319 nfsv4_unlock(&nfsv4rootfs_lock, 1); 320 NFSUNLOCKV4ROOTMUTEX(); 321 confirmp->lval[1] = 0; 322 error = NFSERR_NOENT; 323 goto out; 324 } 325 /* 326 * Get rid of the old one. 327 */ 328 if (i != nfsrv_clienthashsize) { 329 LIST_REMOVE(clp, lc_hash); 330 nfsrv_cleanclient(clp, p); 331 nfsrv_freedeleglist(&clp->lc_deleg); 332 nfsrv_freedeleglist(&clp->lc_olddeleg); 333 zapit = 1; 334 } 335 /* 336 * Add it after assigning a client id to it. 337 */ 338 new_clp->lc_flags |= LCL_NEEDSCONFIRM; 339 if ((nd->nd_flag & ND_NFSV41) != 0) 340 new_clp->lc_confirm.lval[0] = confirmp->lval[0] = 341 ++confirm_index; 342 else 343 confirmp->qval = new_clp->lc_confirm.qval = 344 ++confirm_index; 345 clientidp->lval[0] = new_clp->lc_clientid.lval[0] = 346 (u_int32_t)nfsrvboottime; 347 clientidp->lval[1] = new_clp->lc_clientid.lval[1] = 348 nfsrv_nextclientindex(); 349 new_clp->lc_stateindex = 0; 350 new_clp->lc_statemaxindex = 0; 351 new_clp->lc_cbref = 0; 352 new_clp->lc_expiry = nfsrv_leaseexpiry(); 353 LIST_INIT(&new_clp->lc_open); 354 LIST_INIT(&new_clp->lc_deleg); 355 LIST_INIT(&new_clp->lc_olddeleg); 356 LIST_INIT(&new_clp->lc_session); 357 for (i = 0; i < nfsrv_statehashsize; i++) 358 LIST_INIT(&new_clp->lc_stateid[i]); 359 LIST_INSERT_HEAD(NFSCLIENTHASH(new_clp->lc_clientid), new_clp, 360 lc_hash); 361 nfsstatsv1.srvclients++; 362 nfsrv_openpluslock++; 363 nfsrv_clients++; 364 NFSLOCKV4ROOTMUTEX(); 365 nfsv4_unlock(&nfsv4rootfs_lock, 1); 366 NFSUNLOCKV4ROOTMUTEX(); 367 if (zapit) 368 nfsrv_zapclient(clp, p); 369 *new_clpp = NULL; 370 goto out; 371 } 372 373 /* 374 * Now, handle the cases where the id is already issued. 375 */ 376 if (nfsrv_notsamecredname(nd, clp)) { 377 /* 378 * Check to see if there is expired state that should go away. 379 */ 380 if (clp->lc_expiry < NFSD_MONOSEC && 381 (!LIST_EMPTY(&clp->lc_open) || !LIST_EMPTY(&clp->lc_deleg))) { 382 nfsrv_cleanclient(clp, p); 383 nfsrv_freedeleglist(&clp->lc_deleg); 384 } 385 386 /* 387 * If there is outstanding state, then reply NFSERR_CLIDINUSE per 388 * RFC3530 Sec. 8.1.2 last para. 389 */ 390 if (!LIST_EMPTY(&clp->lc_deleg)) { 391 hasstate = 1; 392 } else if (LIST_EMPTY(&clp->lc_open)) { 393 hasstate = 0; 394 } else { 395 hasstate = 0; 396 /* Look for an Open on the OpenOwner */ 397 LIST_FOREACH(stp, &clp->lc_open, ls_list) { 398 if (!LIST_EMPTY(&stp->ls_open)) { 399 hasstate = 1; 400 break; 401 } 402 } 403 } 404 if (hasstate) { 405 /* 406 * If the uid doesn't match, return NFSERR_CLIDINUSE after 407 * filling out the correct ipaddr and portnum. 408 */ 409 switch (clp->lc_req.nr_nam->sa_family) { 410 #ifdef INET 411 case AF_INET: 412 sin = (struct sockaddr_in *)new_clp->lc_req.nr_nam; 413 rin = (struct sockaddr_in *)clp->lc_req.nr_nam; 414 sin->sin_addr.s_addr = rin->sin_addr.s_addr; 415 sin->sin_port = rin->sin_port; 416 break; 417 #endif 418 #ifdef INET6 419 case AF_INET6: 420 sin6 = (struct sockaddr_in6 *)new_clp->lc_req.nr_nam; 421 rin6 = (struct sockaddr_in6 *)clp->lc_req.nr_nam; 422 sin6->sin6_addr = rin6->sin6_addr; 423 sin6->sin6_port = rin6->sin6_port; 424 break; 425 #endif 426 } 427 NFSLOCKV4ROOTMUTEX(); 428 nfsv4_unlock(&nfsv4rootfs_lock, 1); 429 NFSUNLOCKV4ROOTMUTEX(); 430 error = NFSERR_CLIDINUSE; 431 goto out; 432 } 433 } 434 435 if (NFSBCMP(new_clp->lc_verf, clp->lc_verf, NFSX_VERF)) { 436 /* 437 * If the verifier has changed, the client has rebooted 438 * and a new client id is issued. The old state info 439 * can be thrown away once the SETCLIENTID_CONFIRM occurs. 440 */ 441 LIST_REMOVE(clp, lc_hash); 442 443 /* Get rid of all sessions on this clientid. */ 444 LIST_FOREACH_SAFE(sep, &clp->lc_session, sess_list, nsep) { 445 ret = nfsrv_freesession(sep, NULL); 446 if (ret != 0) 447 printf("nfsrv_setclient: verifier changed free" 448 " session failed=%d\n", ret); 449 } 450 451 new_clp->lc_flags |= LCL_NEEDSCONFIRM; 452 if ((nd->nd_flag & ND_NFSV41) != 0) 453 new_clp->lc_confirm.lval[0] = confirmp->lval[0] = 454 ++confirm_index; 455 else 456 confirmp->qval = new_clp->lc_confirm.qval = 457 ++confirm_index; 458 clientidp->lval[0] = new_clp->lc_clientid.lval[0] = 459 nfsrvboottime; 460 clientidp->lval[1] = new_clp->lc_clientid.lval[1] = 461 nfsrv_nextclientindex(); 462 new_clp->lc_stateindex = 0; 463 new_clp->lc_statemaxindex = 0; 464 new_clp->lc_cbref = 0; 465 new_clp->lc_expiry = nfsrv_leaseexpiry(); 466 467 /* 468 * Save the state until confirmed. 469 */ 470 LIST_NEWHEAD(&new_clp->lc_open, &clp->lc_open, ls_list); 471 LIST_FOREACH(tstp, &new_clp->lc_open, ls_list) 472 tstp->ls_clp = new_clp; 473 LIST_NEWHEAD(&new_clp->lc_deleg, &clp->lc_deleg, ls_list); 474 LIST_FOREACH(tstp, &new_clp->lc_deleg, ls_list) 475 tstp->ls_clp = new_clp; 476 LIST_NEWHEAD(&new_clp->lc_olddeleg, &clp->lc_olddeleg, 477 ls_list); 478 LIST_FOREACH(tstp, &new_clp->lc_olddeleg, ls_list) 479 tstp->ls_clp = new_clp; 480 for (i = 0; i < nfsrv_statehashsize; i++) { 481 LIST_NEWHEAD(&new_clp->lc_stateid[i], 482 &clp->lc_stateid[i], ls_hash); 483 LIST_FOREACH(tstp, &new_clp->lc_stateid[i], ls_hash) 484 tstp->ls_clp = new_clp; 485 } 486 LIST_INIT(&new_clp->lc_session); 487 LIST_INSERT_HEAD(NFSCLIENTHASH(new_clp->lc_clientid), new_clp, 488 lc_hash); 489 nfsstatsv1.srvclients++; 490 nfsrv_openpluslock++; 491 nfsrv_clients++; 492 NFSLOCKV4ROOTMUTEX(); 493 nfsv4_unlock(&nfsv4rootfs_lock, 1); 494 NFSUNLOCKV4ROOTMUTEX(); 495 496 /* 497 * Must wait until any outstanding callback on the old clp 498 * completes. 499 */ 500 NFSLOCKSTATE(); 501 while (clp->lc_cbref) { 502 clp->lc_flags |= LCL_WAKEUPWANTED; 503 (void)mtx_sleep(clp, NFSSTATEMUTEXPTR, PZERO - 1, 504 "nfsd clp", 10 * hz); 505 } 506 NFSUNLOCKSTATE(); 507 nfsrv_zapclient(clp, p); 508 *new_clpp = NULL; 509 goto out; 510 } 511 512 /* For NFSv4.1, mark that we found a confirmed clientid. */ 513 if ((nd->nd_flag & ND_NFSV41) != 0) { 514 clientidp->lval[0] = clp->lc_clientid.lval[0]; 515 clientidp->lval[1] = clp->lc_clientid.lval[1]; 516 confirmp->lval[0] = 0; /* Ignored by client */ 517 confirmp->lval[1] = 1; 518 } else { 519 /* 520 * id and verifier match, so update the net address info 521 * and get rid of any existing callback authentication 522 * handle, so a new one will be acquired. 523 */ 524 LIST_REMOVE(clp, lc_hash); 525 new_clp->lc_flags |= (LCL_NEEDSCONFIRM | LCL_DONTCLEAN); 526 new_clp->lc_expiry = nfsrv_leaseexpiry(); 527 confirmp->qval = new_clp->lc_confirm.qval = ++confirm_index; 528 clientidp->lval[0] = new_clp->lc_clientid.lval[0] = 529 clp->lc_clientid.lval[0]; 530 clientidp->lval[1] = new_clp->lc_clientid.lval[1] = 531 clp->lc_clientid.lval[1]; 532 new_clp->lc_delegtime = clp->lc_delegtime; 533 new_clp->lc_stateindex = clp->lc_stateindex; 534 new_clp->lc_statemaxindex = clp->lc_statemaxindex; 535 new_clp->lc_cbref = 0; 536 LIST_NEWHEAD(&new_clp->lc_open, &clp->lc_open, ls_list); 537 LIST_FOREACH(tstp, &new_clp->lc_open, ls_list) 538 tstp->ls_clp = new_clp; 539 LIST_NEWHEAD(&new_clp->lc_deleg, &clp->lc_deleg, ls_list); 540 LIST_FOREACH(tstp, &new_clp->lc_deleg, ls_list) 541 tstp->ls_clp = new_clp; 542 LIST_NEWHEAD(&new_clp->lc_olddeleg, &clp->lc_olddeleg, ls_list); 543 LIST_FOREACH(tstp, &new_clp->lc_olddeleg, ls_list) 544 tstp->ls_clp = new_clp; 545 for (i = 0; i < nfsrv_statehashsize; i++) { 546 LIST_NEWHEAD(&new_clp->lc_stateid[i], 547 &clp->lc_stateid[i], ls_hash); 548 LIST_FOREACH(tstp, &new_clp->lc_stateid[i], ls_hash) 549 tstp->ls_clp = new_clp; 550 } 551 LIST_INIT(&new_clp->lc_session); 552 LIST_INSERT_HEAD(NFSCLIENTHASH(new_clp->lc_clientid), new_clp, 553 lc_hash); 554 nfsstatsv1.srvclients++; 555 nfsrv_openpluslock++; 556 nfsrv_clients++; 557 } 558 NFSLOCKV4ROOTMUTEX(); 559 nfsv4_unlock(&nfsv4rootfs_lock, 1); 560 NFSUNLOCKV4ROOTMUTEX(); 561 562 if ((nd->nd_flag & ND_NFSV41) == 0) { 563 /* 564 * Must wait until any outstanding callback on the old clp 565 * completes. 566 */ 567 NFSLOCKSTATE(); 568 while (clp->lc_cbref) { 569 clp->lc_flags |= LCL_WAKEUPWANTED; 570 (void)mtx_sleep(clp, NFSSTATEMUTEXPTR, PZERO - 1, 571 "nfsdclp", 10 * hz); 572 } 573 NFSUNLOCKSTATE(); 574 nfsrv_zapclient(clp, p); 575 *new_clpp = NULL; 576 } 577 578 out: 579 NFSEXITCODE2(error, nd); 580 return (error); 581 } 582 583 /* 584 * Check to see if the client id exists and optionally confirm it. 585 */ 586 int 587 nfsrv_getclient(nfsquad_t clientid, int opflags, struct nfsclient **clpp, 588 struct nfsdsession *nsep, nfsquad_t confirm, uint32_t cbprogram, 589 struct nfsrv_descript *nd, NFSPROC_T *p) 590 { 591 struct nfsclient *clp; 592 struct nfsstate *stp; 593 int i; 594 struct nfsclienthashhead *hp; 595 int error = 0, igotlock, doneok; 596 struct nfssessionhash *shp; 597 struct nfsdsession *sep; 598 uint64_t sessid[2]; 599 static uint64_t next_sess = 0; 600 601 if (clpp) 602 *clpp = NULL; 603 if ((nd == NULL || (nd->nd_flag & ND_NFSV41) == 0 || 604 opflags != CLOPS_RENEW) && nfsrvboottime != clientid.lval[0]) { 605 error = NFSERR_STALECLIENTID; 606 goto out; 607 } 608 609 /* 610 * If called with opflags == CLOPS_RENEW, the State Lock is 611 * already held. Otherwise, we need to get either that or, 612 * for the case of Confirm, lock out the nfsd threads. 613 */ 614 if (opflags & CLOPS_CONFIRM) { 615 NFSLOCKV4ROOTMUTEX(); 616 nfsv4_relref(&nfsv4rootfs_lock); 617 do { 618 igotlock = nfsv4_lock(&nfsv4rootfs_lock, 1, NULL, 619 NFSV4ROOTLOCKMUTEXPTR, NULL); 620 } while (!igotlock); 621 /* 622 * Create a new sessionid here, since we need to do it where 623 * there is a mutex held to serialize update of next_sess. 624 */ 625 if ((nd->nd_flag & ND_NFSV41) != 0) { 626 sessid[0] = ++next_sess; 627 sessid[1] = clientid.qval; 628 } 629 NFSUNLOCKV4ROOTMUTEX(); 630 } else if (opflags != CLOPS_RENEW) { 631 NFSLOCKSTATE(); 632 } 633 634 /* For NFSv4.1, the clp is acquired from the associated session. */ 635 if (nd != NULL && (nd->nd_flag & ND_NFSV41) != 0 && 636 opflags == CLOPS_RENEW) { 637 clp = NULL; 638 if ((nd->nd_flag & ND_HASSEQUENCE) != 0) { 639 shp = NFSSESSIONHASH(nd->nd_sessionid); 640 NFSLOCKSESSION(shp); 641 sep = nfsrv_findsession(nd->nd_sessionid); 642 if (sep != NULL) 643 clp = sep->sess_clp; 644 NFSUNLOCKSESSION(shp); 645 } 646 } else { 647 hp = NFSCLIENTHASH(clientid); 648 LIST_FOREACH(clp, hp, lc_hash) { 649 if (clp->lc_clientid.lval[1] == clientid.lval[1]) 650 break; 651 } 652 } 653 if (clp == NULL) { 654 if (opflags & CLOPS_CONFIRM) 655 error = NFSERR_STALECLIENTID; 656 else 657 error = NFSERR_EXPIRED; 658 } else if (clp->lc_flags & LCL_ADMINREVOKED) { 659 /* 660 * If marked admin revoked, just return the error. 661 */ 662 error = NFSERR_ADMINREVOKED; 663 } 664 if (error) { 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 goto out; 673 } 674 675 /* 676 * Perform any operations specified by the opflags. 677 */ 678 if (opflags & CLOPS_CONFIRM) { 679 if ((nd->nd_flag & ND_NFSV41) != 0 && 680 clp->lc_confirm.lval[0] != confirm.lval[0]) 681 error = NFSERR_SEQMISORDERED; 682 else if ((nd->nd_flag & ND_NFSV41) == 0 && 683 clp->lc_confirm.qval != confirm.qval) 684 error = NFSERR_STALECLIENTID; 685 else if (nfsrv_notsamecredname(nd, clp)) 686 error = NFSERR_CLIDINUSE; 687 688 if (!error) { 689 if ((clp->lc_flags & (LCL_NEEDSCONFIRM | LCL_DONTCLEAN)) == 690 LCL_NEEDSCONFIRM) { 691 /* 692 * Hang onto the delegations (as old delegations) 693 * for an Open with CLAIM_DELEGATE_PREV unless in 694 * grace, but get rid of the rest of the state. 695 */ 696 nfsrv_cleanclient(clp, p); 697 nfsrv_freedeleglist(&clp->lc_olddeleg); 698 if (nfsrv_checkgrace(nd, clp, 0)) { 699 /* In grace, so just delete delegations */ 700 nfsrv_freedeleglist(&clp->lc_deleg); 701 } else { 702 LIST_FOREACH(stp, &clp->lc_deleg, ls_list) 703 stp->ls_flags |= NFSLCK_OLDDELEG; 704 clp->lc_delegtime = NFSD_MONOSEC + 705 nfsrv_lease + NFSRV_LEASEDELTA; 706 LIST_NEWHEAD(&clp->lc_olddeleg, &clp->lc_deleg, 707 ls_list); 708 } 709 if ((nd->nd_flag & ND_NFSV41) != 0) 710 clp->lc_program = cbprogram; 711 } 712 clp->lc_flags &= ~(LCL_NEEDSCONFIRM | LCL_DONTCLEAN); 713 if (clp->lc_program) 714 clp->lc_flags |= LCL_NEEDSCBNULL; 715 /* For NFSv4.1, link the session onto the client. */ 716 if (nsep != NULL) { 717 /* Hold a reference on the xprt for a backchannel. */ 718 if ((nsep->sess_crflags & NFSV4CRSESS_CONNBACKCHAN) 719 != 0) { 720 if (clp->lc_req.nr_client == NULL) 721 clp->lc_req.nr_client = (struct __rpc_client *) 722 clnt_bck_create(nd->nd_xprt->xp_socket, 723 cbprogram, NFSV4_CBVERS); 724 if (clp->lc_req.nr_client != NULL) { 725 SVC_ACQUIRE(nd->nd_xprt); 726 CLNT_ACQUIRE(clp->lc_req.nr_client); 727 nd->nd_xprt->xp_p2 = clp->lc_req.nr_client; 728 /* Disable idle timeout. */ 729 nd->nd_xprt->xp_idletimeout = 0; 730 nsep->sess_cbsess.nfsess_xprt = nd->nd_xprt; 731 } else 732 nsep->sess_crflags &= ~NFSV4CRSESS_CONNBACKCHAN; 733 } 734 NFSBCOPY(sessid, nsep->sess_sessionid, 735 NFSX_V4SESSIONID); 736 NFSBCOPY(sessid, nsep->sess_cbsess.nfsess_sessionid, 737 NFSX_V4SESSIONID); 738 shp = NFSSESSIONHASH(nsep->sess_sessionid); 739 NFSLOCKSTATE(); 740 NFSLOCKSESSION(shp); 741 LIST_INSERT_HEAD(&shp->list, nsep, sess_hash); 742 LIST_INSERT_HEAD(&clp->lc_session, nsep, sess_list); 743 nsep->sess_clp = clp; 744 NFSUNLOCKSESSION(shp); 745 NFSUNLOCKSTATE(); 746 } 747 } 748 } else if (clp->lc_flags & LCL_NEEDSCONFIRM) { 749 error = NFSERR_EXPIRED; 750 } 751 752 /* 753 * If called by the Renew Op, we must check the principal. 754 */ 755 if (!error && (opflags & CLOPS_RENEWOP)) { 756 if (nfsrv_notsamecredname(nd, clp)) { 757 doneok = 0; 758 for (i = 0; i < nfsrv_statehashsize && doneok == 0; i++) { 759 LIST_FOREACH(stp, &clp->lc_stateid[i], ls_hash) { 760 if ((stp->ls_flags & NFSLCK_OPEN) && 761 stp->ls_uid == nd->nd_cred->cr_uid) { 762 doneok = 1; 763 break; 764 } 765 } 766 } 767 if (!doneok) 768 error = NFSERR_ACCES; 769 } 770 if (!error && (clp->lc_flags & LCL_CBDOWN)) 771 error = NFSERR_CBPATHDOWN; 772 } 773 if ((!error || error == NFSERR_CBPATHDOWN) && 774 (opflags & CLOPS_RENEW)) { 775 clp->lc_expiry = nfsrv_leaseexpiry(); 776 } 777 if (opflags & CLOPS_CONFIRM) { 778 NFSLOCKV4ROOTMUTEX(); 779 nfsv4_unlock(&nfsv4rootfs_lock, 1); 780 NFSUNLOCKV4ROOTMUTEX(); 781 } else if (opflags != CLOPS_RENEW) { 782 NFSUNLOCKSTATE(); 783 } 784 if (clpp) 785 *clpp = clp; 786 787 out: 788 NFSEXITCODE2(error, nd); 789 return (error); 790 } 791 792 /* 793 * Perform the NFSv4.1 destroy clientid. 794 */ 795 int 796 nfsrv_destroyclient(nfsquad_t clientid, NFSPROC_T *p) 797 { 798 struct nfsclient *clp; 799 struct nfsclienthashhead *hp; 800 int error = 0, i, igotlock; 801 802 if (nfsrvboottime != clientid.lval[0]) { 803 error = NFSERR_STALECLIENTID; 804 goto out; 805 } 806 807 /* Lock out other nfsd threads */ 808 NFSLOCKV4ROOTMUTEX(); 809 nfsv4_relref(&nfsv4rootfs_lock); 810 do { 811 igotlock = nfsv4_lock(&nfsv4rootfs_lock, 1, NULL, 812 NFSV4ROOTLOCKMUTEXPTR, NULL); 813 } while (igotlock == 0); 814 NFSUNLOCKV4ROOTMUTEX(); 815 816 hp = NFSCLIENTHASH(clientid); 817 LIST_FOREACH(clp, hp, lc_hash) { 818 if (clp->lc_clientid.lval[1] == clientid.lval[1]) 819 break; 820 } 821 if (clp == NULL) { 822 NFSLOCKV4ROOTMUTEX(); 823 nfsv4_unlock(&nfsv4rootfs_lock, 1); 824 NFSUNLOCKV4ROOTMUTEX(); 825 /* Just return ok, since it is gone. */ 826 goto out; 827 } 828 829 /* 830 * Free up all layouts on the clientid. Should the client return the 831 * layouts? 832 */ 833 nfsrv_freelayoutlist(clientid); 834 835 /* Scan for state on the clientid. */ 836 for (i = 0; i < nfsrv_statehashsize; i++) 837 if (!LIST_EMPTY(&clp->lc_stateid[i])) { 838 NFSLOCKV4ROOTMUTEX(); 839 nfsv4_unlock(&nfsv4rootfs_lock, 1); 840 NFSUNLOCKV4ROOTMUTEX(); 841 error = NFSERR_CLIENTIDBUSY; 842 goto out; 843 } 844 if (!LIST_EMPTY(&clp->lc_session) || !LIST_EMPTY(&clp->lc_deleg)) { 845 NFSLOCKV4ROOTMUTEX(); 846 nfsv4_unlock(&nfsv4rootfs_lock, 1); 847 NFSUNLOCKV4ROOTMUTEX(); 848 error = NFSERR_CLIENTIDBUSY; 849 goto out; 850 } 851 852 /* Destroy the clientid and return ok. */ 853 nfsrv_cleanclient(clp, p); 854 nfsrv_freedeleglist(&clp->lc_deleg); 855 nfsrv_freedeleglist(&clp->lc_olddeleg); 856 LIST_REMOVE(clp, lc_hash); 857 NFSLOCKV4ROOTMUTEX(); 858 nfsv4_unlock(&nfsv4rootfs_lock, 1); 859 NFSUNLOCKV4ROOTMUTEX(); 860 nfsrv_zapclient(clp, p); 861 out: 862 NFSEXITCODE2(error, nd); 863 return (error); 864 } 865 866 /* 867 * Called from the new nfssvc syscall to admin revoke a clientid. 868 * Returns 0 for success, error otherwise. 869 */ 870 int 871 nfsrv_adminrevoke(struct nfsd_clid *revokep, NFSPROC_T *p) 872 { 873 struct nfsclient *clp = NULL; 874 int i, error = 0; 875 int gotit, igotlock; 876 877 /* 878 * First, lock out the nfsd so that state won't change while the 879 * revocation record is being written to the stable storage restart 880 * file. 881 */ 882 NFSLOCKV4ROOTMUTEX(); 883 do { 884 igotlock = nfsv4_lock(&nfsv4rootfs_lock, 1, NULL, 885 NFSV4ROOTLOCKMUTEXPTR, NULL); 886 } while (!igotlock); 887 NFSUNLOCKV4ROOTMUTEX(); 888 889 /* 890 * Search for a match in the client list. 891 */ 892 gotit = i = 0; 893 while (i < nfsrv_clienthashsize && !gotit) { 894 LIST_FOREACH(clp, &nfsclienthash[i], lc_hash) { 895 if (revokep->nclid_idlen == clp->lc_idlen && 896 !NFSBCMP(revokep->nclid_id, clp->lc_id, clp->lc_idlen)) { 897 gotit = 1; 898 break; 899 } 900 } 901 i++; 902 } 903 if (!gotit) { 904 NFSLOCKV4ROOTMUTEX(); 905 nfsv4_unlock(&nfsv4rootfs_lock, 0); 906 NFSUNLOCKV4ROOTMUTEX(); 907 error = EPERM; 908 goto out; 909 } 910 911 /* 912 * Now, write out the revocation record 913 */ 914 nfsrv_writestable(clp->lc_id, clp->lc_idlen, NFSNST_REVOKE, p); 915 nfsrv_backupstable(); 916 917 /* 918 * and clear out the state, marking the clientid revoked. 919 */ 920 clp->lc_flags &= ~LCL_CALLBACKSON; 921 clp->lc_flags |= LCL_ADMINREVOKED; 922 nfsrv_cleanclient(clp, p); 923 nfsrv_freedeleglist(&clp->lc_deleg); 924 nfsrv_freedeleglist(&clp->lc_olddeleg); 925 NFSLOCKV4ROOTMUTEX(); 926 nfsv4_unlock(&nfsv4rootfs_lock, 0); 927 NFSUNLOCKV4ROOTMUTEX(); 928 929 out: 930 NFSEXITCODE(error); 931 return (error); 932 } 933 934 /* 935 * Dump out stats for all clients. Called from nfssvc(2), that is used 936 * nfsstatsv1. 937 */ 938 void 939 nfsrv_dumpclients(struct nfsd_dumpclients *dumpp, int maxcnt) 940 { 941 struct nfsclient *clp; 942 int i = 0, cnt = 0; 943 944 /* 945 * First, get a reference on the nfsv4rootfs_lock so that an 946 * exclusive lock cannot be acquired while dumping the clients. 947 */ 948 NFSLOCKV4ROOTMUTEX(); 949 nfsv4_getref(&nfsv4rootfs_lock, NULL, NFSV4ROOTLOCKMUTEXPTR, NULL); 950 NFSUNLOCKV4ROOTMUTEX(); 951 NFSLOCKSTATE(); 952 /* 953 * Rattle through the client lists until done. 954 */ 955 while (i < nfsrv_clienthashsize && cnt < maxcnt) { 956 clp = LIST_FIRST(&nfsclienthash[i]); 957 while (clp != LIST_END(&nfsclienthash[i]) && cnt < maxcnt) { 958 nfsrv_dumpaclient(clp, &dumpp[cnt]); 959 cnt++; 960 clp = LIST_NEXT(clp, lc_hash); 961 } 962 i++; 963 } 964 if (cnt < maxcnt) 965 dumpp[cnt].ndcl_clid.nclid_idlen = 0; 966 NFSUNLOCKSTATE(); 967 NFSLOCKV4ROOTMUTEX(); 968 nfsv4_relref(&nfsv4rootfs_lock); 969 NFSUNLOCKV4ROOTMUTEX(); 970 } 971 972 /* 973 * Dump stats for a client. Must be called with the NFSSTATELOCK and spl'd. 974 */ 975 static void 976 nfsrv_dumpaclient(struct nfsclient *clp, struct nfsd_dumpclients *dumpp) 977 { 978 struct nfsstate *stp, *openstp, *lckownstp; 979 struct nfslock *lop; 980 sa_family_t af; 981 #ifdef INET 982 struct sockaddr_in *rin; 983 #endif 984 #ifdef INET6 985 struct sockaddr_in6 *rin6; 986 #endif 987 988 dumpp->ndcl_nopenowners = dumpp->ndcl_nlockowners = 0; 989 dumpp->ndcl_nopens = dumpp->ndcl_nlocks = 0; 990 dumpp->ndcl_ndelegs = dumpp->ndcl_nolddelegs = 0; 991 dumpp->ndcl_flags = clp->lc_flags; 992 dumpp->ndcl_clid.nclid_idlen = clp->lc_idlen; 993 NFSBCOPY(clp->lc_id, dumpp->ndcl_clid.nclid_id, clp->lc_idlen); 994 af = clp->lc_req.nr_nam->sa_family; 995 dumpp->ndcl_addrfam = af; 996 switch (af) { 997 #ifdef INET 998 case AF_INET: 999 rin = (struct sockaddr_in *)clp->lc_req.nr_nam; 1000 dumpp->ndcl_cbaddr.sin_addr = rin->sin_addr; 1001 break; 1002 #endif 1003 #ifdef INET6 1004 case AF_INET6: 1005 rin6 = (struct sockaddr_in6 *)clp->lc_req.nr_nam; 1006 dumpp->ndcl_cbaddr.sin6_addr = rin6->sin6_addr; 1007 break; 1008 #endif 1009 } 1010 1011 /* 1012 * Now, scan the state lists and total up the opens and locks. 1013 */ 1014 LIST_FOREACH(stp, &clp->lc_open, ls_list) { 1015 dumpp->ndcl_nopenowners++; 1016 LIST_FOREACH(openstp, &stp->ls_open, ls_list) { 1017 dumpp->ndcl_nopens++; 1018 LIST_FOREACH(lckownstp, &openstp->ls_open, ls_list) { 1019 dumpp->ndcl_nlockowners++; 1020 LIST_FOREACH(lop, &lckownstp->ls_lock, lo_lckowner) { 1021 dumpp->ndcl_nlocks++; 1022 } 1023 } 1024 } 1025 } 1026 1027 /* 1028 * and the delegation lists. 1029 */ 1030 LIST_FOREACH(stp, &clp->lc_deleg, ls_list) { 1031 dumpp->ndcl_ndelegs++; 1032 } 1033 LIST_FOREACH(stp, &clp->lc_olddeleg, ls_list) { 1034 dumpp->ndcl_nolddelegs++; 1035 } 1036 } 1037 1038 /* 1039 * Dump out lock stats for a file. 1040 */ 1041 void 1042 nfsrv_dumplocks(vnode_t vp, struct nfsd_dumplocks *ldumpp, int maxcnt, 1043 NFSPROC_T *p) 1044 { 1045 struct nfsstate *stp; 1046 struct nfslock *lop; 1047 int cnt = 0; 1048 struct nfslockfile *lfp; 1049 sa_family_t af; 1050 #ifdef INET 1051 struct sockaddr_in *rin; 1052 #endif 1053 #ifdef INET6 1054 struct sockaddr_in6 *rin6; 1055 #endif 1056 int ret; 1057 fhandle_t nfh; 1058 1059 ret = nfsrv_getlockfh(vp, 0, NULL, &nfh, p); 1060 /* 1061 * First, get a reference on the nfsv4rootfs_lock so that an 1062 * exclusive lock on it cannot be acquired while dumping the locks. 1063 */ 1064 NFSLOCKV4ROOTMUTEX(); 1065 nfsv4_getref(&nfsv4rootfs_lock, NULL, NFSV4ROOTLOCKMUTEXPTR, NULL); 1066 NFSUNLOCKV4ROOTMUTEX(); 1067 NFSLOCKSTATE(); 1068 if (!ret) 1069 ret = nfsrv_getlockfile(0, NULL, &lfp, &nfh, 0); 1070 if (ret) { 1071 ldumpp[0].ndlck_clid.nclid_idlen = 0; 1072 NFSUNLOCKSTATE(); 1073 NFSLOCKV4ROOTMUTEX(); 1074 nfsv4_relref(&nfsv4rootfs_lock); 1075 NFSUNLOCKV4ROOTMUTEX(); 1076 return; 1077 } 1078 1079 /* 1080 * For each open share on file, dump it out. 1081 */ 1082 stp = LIST_FIRST(&lfp->lf_open); 1083 while (stp != LIST_END(&lfp->lf_open) && cnt < maxcnt) { 1084 ldumpp[cnt].ndlck_flags = stp->ls_flags; 1085 ldumpp[cnt].ndlck_stateid.seqid = stp->ls_stateid.seqid; 1086 ldumpp[cnt].ndlck_stateid.other[0] = stp->ls_stateid.other[0]; 1087 ldumpp[cnt].ndlck_stateid.other[1] = stp->ls_stateid.other[1]; 1088 ldumpp[cnt].ndlck_stateid.other[2] = stp->ls_stateid.other[2]; 1089 ldumpp[cnt].ndlck_owner.nclid_idlen = 1090 stp->ls_openowner->ls_ownerlen; 1091 NFSBCOPY(stp->ls_openowner->ls_owner, 1092 ldumpp[cnt].ndlck_owner.nclid_id, 1093 stp->ls_openowner->ls_ownerlen); 1094 ldumpp[cnt].ndlck_clid.nclid_idlen = stp->ls_clp->lc_idlen; 1095 NFSBCOPY(stp->ls_clp->lc_id, ldumpp[cnt].ndlck_clid.nclid_id, 1096 stp->ls_clp->lc_idlen); 1097 af = stp->ls_clp->lc_req.nr_nam->sa_family; 1098 ldumpp[cnt].ndlck_addrfam = af; 1099 switch (af) { 1100 #ifdef INET 1101 case AF_INET: 1102 rin = (struct sockaddr_in *)stp->ls_clp->lc_req.nr_nam; 1103 ldumpp[cnt].ndlck_cbaddr.sin_addr = rin->sin_addr; 1104 break; 1105 #endif 1106 #ifdef INET6 1107 case AF_INET6: 1108 rin6 = (struct sockaddr_in6 *) 1109 stp->ls_clp->lc_req.nr_nam; 1110 ldumpp[cnt].ndlck_cbaddr.sin6_addr = rin6->sin6_addr; 1111 break; 1112 #endif 1113 } 1114 stp = LIST_NEXT(stp, ls_file); 1115 cnt++; 1116 } 1117 1118 /* 1119 * and all locks. 1120 */ 1121 lop = LIST_FIRST(&lfp->lf_lock); 1122 while (lop != LIST_END(&lfp->lf_lock) && cnt < maxcnt) { 1123 stp = lop->lo_stp; 1124 ldumpp[cnt].ndlck_flags = lop->lo_flags; 1125 ldumpp[cnt].ndlck_first = lop->lo_first; 1126 ldumpp[cnt].ndlck_end = lop->lo_end; 1127 ldumpp[cnt].ndlck_stateid.seqid = stp->ls_stateid.seqid; 1128 ldumpp[cnt].ndlck_stateid.other[0] = stp->ls_stateid.other[0]; 1129 ldumpp[cnt].ndlck_stateid.other[1] = stp->ls_stateid.other[1]; 1130 ldumpp[cnt].ndlck_stateid.other[2] = stp->ls_stateid.other[2]; 1131 ldumpp[cnt].ndlck_owner.nclid_idlen = stp->ls_ownerlen; 1132 NFSBCOPY(stp->ls_owner, ldumpp[cnt].ndlck_owner.nclid_id, 1133 stp->ls_ownerlen); 1134 ldumpp[cnt].ndlck_clid.nclid_idlen = stp->ls_clp->lc_idlen; 1135 NFSBCOPY(stp->ls_clp->lc_id, ldumpp[cnt].ndlck_clid.nclid_id, 1136 stp->ls_clp->lc_idlen); 1137 af = stp->ls_clp->lc_req.nr_nam->sa_family; 1138 ldumpp[cnt].ndlck_addrfam = af; 1139 switch (af) { 1140 #ifdef INET 1141 case AF_INET: 1142 rin = (struct sockaddr_in *)stp->ls_clp->lc_req.nr_nam; 1143 ldumpp[cnt].ndlck_cbaddr.sin_addr = rin->sin_addr; 1144 break; 1145 #endif 1146 #ifdef INET6 1147 case AF_INET6: 1148 rin6 = (struct sockaddr_in6 *) 1149 stp->ls_clp->lc_req.nr_nam; 1150 ldumpp[cnt].ndlck_cbaddr.sin6_addr = rin6->sin6_addr; 1151 break; 1152 #endif 1153 } 1154 lop = LIST_NEXT(lop, lo_lckfile); 1155 cnt++; 1156 } 1157 1158 /* 1159 * and the delegations. 1160 */ 1161 stp = LIST_FIRST(&lfp->lf_deleg); 1162 while (stp != LIST_END(&lfp->lf_deleg) && cnt < maxcnt) { 1163 ldumpp[cnt].ndlck_flags = stp->ls_flags; 1164 ldumpp[cnt].ndlck_stateid.seqid = stp->ls_stateid.seqid; 1165 ldumpp[cnt].ndlck_stateid.other[0] = stp->ls_stateid.other[0]; 1166 ldumpp[cnt].ndlck_stateid.other[1] = stp->ls_stateid.other[1]; 1167 ldumpp[cnt].ndlck_stateid.other[2] = stp->ls_stateid.other[2]; 1168 ldumpp[cnt].ndlck_owner.nclid_idlen = 0; 1169 ldumpp[cnt].ndlck_clid.nclid_idlen = stp->ls_clp->lc_idlen; 1170 NFSBCOPY(stp->ls_clp->lc_id, ldumpp[cnt].ndlck_clid.nclid_id, 1171 stp->ls_clp->lc_idlen); 1172 af = stp->ls_clp->lc_req.nr_nam->sa_family; 1173 ldumpp[cnt].ndlck_addrfam = af; 1174 switch (af) { 1175 #ifdef INET 1176 case AF_INET: 1177 rin = (struct sockaddr_in *)stp->ls_clp->lc_req.nr_nam; 1178 ldumpp[cnt].ndlck_cbaddr.sin_addr = rin->sin_addr; 1179 break; 1180 #endif 1181 #ifdef INET6 1182 case AF_INET6: 1183 rin6 = (struct sockaddr_in6 *) 1184 stp->ls_clp->lc_req.nr_nam; 1185 ldumpp[cnt].ndlck_cbaddr.sin6_addr = rin6->sin6_addr; 1186 break; 1187 #endif 1188 } 1189 stp = LIST_NEXT(stp, ls_file); 1190 cnt++; 1191 } 1192 1193 /* 1194 * If list isn't full, mark end of list by setting the client name 1195 * to zero length. 1196 */ 1197 if (cnt < maxcnt) 1198 ldumpp[cnt].ndlck_clid.nclid_idlen = 0; 1199 NFSUNLOCKSTATE(); 1200 NFSLOCKV4ROOTMUTEX(); 1201 nfsv4_relref(&nfsv4rootfs_lock); 1202 NFSUNLOCKV4ROOTMUTEX(); 1203 } 1204 1205 /* 1206 * Server timer routine. It can scan any linked list, so long 1207 * as it holds the spin/mutex lock and there is no exclusive lock on 1208 * nfsv4rootfs_lock. 1209 * (For OpenBSD, a kthread is ok. For FreeBSD, I think it is ok 1210 * to do this from a callout, since the spin locks work. For 1211 * Darwin, I'm not sure what will work correctly yet.) 1212 * Should be called once per second. 1213 */ 1214 void 1215 nfsrv_servertimer(void) 1216 { 1217 struct nfsclient *clp, *nclp; 1218 struct nfsstate *stp, *nstp; 1219 int got_ref, i; 1220 1221 /* 1222 * Make sure nfsboottime is set. This is used by V3 as well 1223 * as V4. Note that nfsboottime is not nfsrvboottime, which is 1224 * only used by the V4 server for leases. 1225 */ 1226 if (nfsboottime.tv_sec == 0) 1227 NFSSETBOOTTIME(nfsboottime); 1228 1229 /* 1230 * If server hasn't started yet, just return. 1231 */ 1232 NFSLOCKSTATE(); 1233 if (nfsrv_stablefirst.nsf_eograce == 0) { 1234 NFSUNLOCKSTATE(); 1235 return; 1236 } 1237 if (!(nfsrv_stablefirst.nsf_flags & NFSNSF_UPDATEDONE)) { 1238 if (!(nfsrv_stablefirst.nsf_flags & NFSNSF_GRACEOVER) && 1239 NFSD_MONOSEC > nfsrv_stablefirst.nsf_eograce) 1240 nfsrv_stablefirst.nsf_flags |= 1241 (NFSNSF_GRACEOVER | NFSNSF_NEEDLOCK); 1242 NFSUNLOCKSTATE(); 1243 return; 1244 } 1245 1246 /* 1247 * Try and get a reference count on the nfsv4rootfs_lock so that 1248 * no nfsd thread can acquire an exclusive lock on it before this 1249 * call is done. If it is already exclusively locked, just return. 1250 */ 1251 NFSLOCKV4ROOTMUTEX(); 1252 got_ref = nfsv4_getref_nonblock(&nfsv4rootfs_lock); 1253 NFSUNLOCKV4ROOTMUTEX(); 1254 if (got_ref == 0) { 1255 NFSUNLOCKSTATE(); 1256 return; 1257 } 1258 1259 /* 1260 * For each client... 1261 */ 1262 for (i = 0; i < nfsrv_clienthashsize; i++) { 1263 clp = LIST_FIRST(&nfsclienthash[i]); 1264 while (clp != LIST_END(&nfsclienthash[i])) { 1265 nclp = LIST_NEXT(clp, lc_hash); 1266 if (!(clp->lc_flags & LCL_EXPIREIT)) { 1267 if (((clp->lc_expiry + NFSRV_STALELEASE) < NFSD_MONOSEC 1268 && ((LIST_EMPTY(&clp->lc_deleg) 1269 && LIST_EMPTY(&clp->lc_open)) || 1270 nfsrv_clients > nfsrv_clienthighwater)) || 1271 (clp->lc_expiry + NFSRV_MOULDYLEASE) < NFSD_MONOSEC || 1272 (clp->lc_expiry < NFSD_MONOSEC && 1273 (nfsrv_openpluslock * 10 / 9) > nfsrv_v4statelimit)) { 1274 /* 1275 * Lease has expired several nfsrv_lease times ago: 1276 * PLUS 1277 * - no state is associated with it 1278 * OR 1279 * - above high water mark for number of clients 1280 * (nfsrv_clienthighwater should be large enough 1281 * that this only occurs when clients fail to 1282 * use the same nfs_client_id4.id. Maybe somewhat 1283 * higher that the maximum number of clients that 1284 * will mount this server?) 1285 * OR 1286 * Lease has expired a very long time ago 1287 * OR 1288 * Lease has expired PLUS the number of opens + locks 1289 * has exceeded 90% of capacity 1290 * 1291 * --> Mark for expiry. The actual expiry will be done 1292 * by an nfsd sometime soon. 1293 */ 1294 clp->lc_flags |= LCL_EXPIREIT; 1295 nfsrv_stablefirst.nsf_flags |= 1296 (NFSNSF_NEEDLOCK | NFSNSF_EXPIREDCLIENT); 1297 } else { 1298 /* 1299 * If there are no opens, increment no open tick cnt 1300 * If time exceeds NFSNOOPEN, mark it to be thrown away 1301 * otherwise, if there is an open, reset no open time 1302 * Hopefully, this will avoid excessive re-creation 1303 * of open owners and subsequent open confirms. 1304 */ 1305 stp = LIST_FIRST(&clp->lc_open); 1306 while (stp != LIST_END(&clp->lc_open)) { 1307 nstp = LIST_NEXT(stp, ls_list); 1308 if (LIST_EMPTY(&stp->ls_open)) { 1309 stp->ls_noopens++; 1310 if (stp->ls_noopens > NFSNOOPEN || 1311 (nfsrv_openpluslock * 2) > 1312 nfsrv_v4statelimit) 1313 nfsrv_stablefirst.nsf_flags |= 1314 NFSNSF_NOOPENS; 1315 } else { 1316 stp->ls_noopens = 0; 1317 } 1318 stp = nstp; 1319 } 1320 } 1321 } 1322 clp = nclp; 1323 } 1324 } 1325 NFSUNLOCKSTATE(); 1326 NFSLOCKV4ROOTMUTEX(); 1327 nfsv4_relref(&nfsv4rootfs_lock); 1328 NFSUNLOCKV4ROOTMUTEX(); 1329 } 1330 1331 /* 1332 * The following set of functions free up the various data structures. 1333 */ 1334 /* 1335 * Clear out all open/lock state related to this nfsclient. 1336 * Caller must hold an exclusive lock on nfsv4rootfs_lock, so that 1337 * there are no other active nfsd threads. 1338 */ 1339 void 1340 nfsrv_cleanclient(struct nfsclient *clp, NFSPROC_T *p) 1341 { 1342 struct nfsstate *stp, *nstp; 1343 struct nfsdsession *sep, *nsep; 1344 1345 LIST_FOREACH_SAFE(stp, &clp->lc_open, ls_list, nstp) 1346 nfsrv_freeopenowner(stp, 1, p); 1347 if ((clp->lc_flags & LCL_ADMINREVOKED) == 0) 1348 LIST_FOREACH_SAFE(sep, &clp->lc_session, sess_list, nsep) 1349 (void)nfsrv_freesession(sep, NULL); 1350 } 1351 1352 /* 1353 * Free a client that has been cleaned. It should also already have been 1354 * removed from the lists. 1355 * (Just to be safe w.r.t. newnfs_disconnect(), call this function when 1356 * softclock interrupts are enabled.) 1357 */ 1358 void 1359 nfsrv_zapclient(struct nfsclient *clp, NFSPROC_T *p) 1360 { 1361 1362 #ifdef notyet 1363 if ((clp->lc_flags & (LCL_GSS | LCL_CALLBACKSON)) == 1364 (LCL_GSS | LCL_CALLBACKSON) && 1365 (clp->lc_hand.nfsh_flag & NFSG_COMPLETE) && 1366 clp->lc_handlelen > 0) { 1367 clp->lc_hand.nfsh_flag &= ~NFSG_COMPLETE; 1368 clp->lc_hand.nfsh_flag |= NFSG_DESTROYED; 1369 (void) nfsrv_docallback(clp, NFSV4PROC_CBNULL, 1370 NULL, 0, NULL, NULL, NULL, 0, p); 1371 } 1372 #endif 1373 newnfs_disconnect(NULL, &clp->lc_req); 1374 free(clp->lc_req.nr_nam, M_SONAME); 1375 NFSFREEMUTEX(&clp->lc_req.nr_mtx); 1376 free(clp->lc_stateid, M_NFSDCLIENT); 1377 free(clp, M_NFSDCLIENT); 1378 NFSLOCKSTATE(); 1379 nfsstatsv1.srvclients--; 1380 nfsrv_openpluslock--; 1381 nfsrv_clients--; 1382 NFSUNLOCKSTATE(); 1383 } 1384 1385 /* 1386 * Free a list of delegation state structures. 1387 * (This function will also free all nfslockfile structures that no 1388 * longer have associated state.) 1389 */ 1390 void 1391 nfsrv_freedeleglist(struct nfsstatehead *sthp) 1392 { 1393 struct nfsstate *stp, *nstp; 1394 1395 LIST_FOREACH_SAFE(stp, sthp, ls_list, nstp) { 1396 nfsrv_freedeleg(stp); 1397 } 1398 LIST_INIT(sthp); 1399 } 1400 1401 /* 1402 * Free up a delegation. 1403 */ 1404 static void 1405 nfsrv_freedeleg(struct nfsstate *stp) 1406 { 1407 struct nfslockfile *lfp; 1408 1409 LIST_REMOVE(stp, ls_hash); 1410 LIST_REMOVE(stp, ls_list); 1411 LIST_REMOVE(stp, ls_file); 1412 if ((stp->ls_flags & NFSLCK_DELEGWRITE) != 0) 1413 nfsrv_writedelegcnt--; 1414 lfp = stp->ls_lfp; 1415 if (LIST_EMPTY(&lfp->lf_open) && 1416 LIST_EMPTY(&lfp->lf_lock) && LIST_EMPTY(&lfp->lf_deleg) && 1417 LIST_EMPTY(&lfp->lf_locallock) && LIST_EMPTY(&lfp->lf_rollback) && 1418 lfp->lf_usecount == 0 && 1419 nfsv4_testlock(&lfp->lf_locallock_lck) == 0) 1420 nfsrv_freenfslockfile(lfp); 1421 free(stp, M_NFSDSTATE); 1422 nfsstatsv1.srvdelegates--; 1423 nfsrv_openpluslock--; 1424 nfsrv_delegatecnt--; 1425 } 1426 1427 /* 1428 * This function frees an open owner and all associated opens. 1429 */ 1430 static void 1431 nfsrv_freeopenowner(struct nfsstate *stp, int cansleep, NFSPROC_T *p) 1432 { 1433 struct nfsstate *nstp, *tstp; 1434 1435 LIST_REMOVE(stp, ls_list); 1436 /* 1437 * Now, free all associated opens. 1438 */ 1439 nstp = LIST_FIRST(&stp->ls_open); 1440 while (nstp != LIST_END(&stp->ls_open)) { 1441 tstp = nstp; 1442 nstp = LIST_NEXT(nstp, ls_list); 1443 (void) nfsrv_freeopen(tstp, NULL, cansleep, p); 1444 } 1445 if (stp->ls_op) 1446 nfsrvd_derefcache(stp->ls_op); 1447 free(stp, M_NFSDSTATE); 1448 nfsstatsv1.srvopenowners--; 1449 nfsrv_openpluslock--; 1450 } 1451 1452 /* 1453 * This function frees an open (nfsstate open structure) with all associated 1454 * lock_owners and locks. It also frees the nfslockfile structure iff there 1455 * are no other opens on the file. 1456 * Returns 1 if it free'd the nfslockfile, 0 otherwise. 1457 */ 1458 static int 1459 nfsrv_freeopen(struct nfsstate *stp, vnode_t vp, int cansleep, NFSPROC_T *p) 1460 { 1461 struct nfsstate *nstp, *tstp; 1462 struct nfslockfile *lfp; 1463 int ret; 1464 1465 LIST_REMOVE(stp, ls_hash); 1466 LIST_REMOVE(stp, ls_list); 1467 LIST_REMOVE(stp, ls_file); 1468 1469 lfp = stp->ls_lfp; 1470 /* 1471 * Now, free all lockowners associated with this open. 1472 */ 1473 LIST_FOREACH_SAFE(tstp, &stp->ls_open, ls_list, nstp) 1474 nfsrv_freelockowner(tstp, vp, cansleep, p); 1475 1476 /* 1477 * The nfslockfile is freed here if there are no locks 1478 * associated with the open. 1479 * If there are locks associated with the open, the 1480 * nfslockfile structure can be freed via nfsrv_freelockowner(). 1481 * Acquire the state mutex to avoid races with calls to 1482 * nfsrv_getlockfile(). 1483 */ 1484 if (cansleep != 0) 1485 NFSLOCKSTATE(); 1486 if (lfp != NULL && LIST_EMPTY(&lfp->lf_open) && 1487 LIST_EMPTY(&lfp->lf_deleg) && LIST_EMPTY(&lfp->lf_lock) && 1488 LIST_EMPTY(&lfp->lf_locallock) && LIST_EMPTY(&lfp->lf_rollback) && 1489 lfp->lf_usecount == 0 && 1490 (cansleep != 0 || nfsv4_testlock(&lfp->lf_locallock_lck) == 0)) { 1491 nfsrv_freenfslockfile(lfp); 1492 ret = 1; 1493 } else 1494 ret = 0; 1495 if (cansleep != 0) 1496 NFSUNLOCKSTATE(); 1497 free(stp, M_NFSDSTATE); 1498 nfsstatsv1.srvopens--; 1499 nfsrv_openpluslock--; 1500 return (ret); 1501 } 1502 1503 /* 1504 * Frees a lockowner and all associated locks. 1505 */ 1506 static void 1507 nfsrv_freelockowner(struct nfsstate *stp, vnode_t vp, int cansleep, 1508 NFSPROC_T *p) 1509 { 1510 1511 LIST_REMOVE(stp, ls_hash); 1512 LIST_REMOVE(stp, ls_list); 1513 nfsrv_freeallnfslocks(stp, vp, cansleep, p); 1514 if (stp->ls_op) 1515 nfsrvd_derefcache(stp->ls_op); 1516 free(stp, M_NFSDSTATE); 1517 nfsstatsv1.srvlockowners--; 1518 nfsrv_openpluslock--; 1519 } 1520 1521 /* 1522 * Free all the nfs locks on a lockowner. 1523 */ 1524 static void 1525 nfsrv_freeallnfslocks(struct nfsstate *stp, vnode_t vp, int cansleep, 1526 NFSPROC_T *p) 1527 { 1528 struct nfslock *lop, *nlop; 1529 struct nfsrollback *rlp, *nrlp; 1530 struct nfslockfile *lfp = NULL; 1531 int gottvp = 0; 1532 vnode_t tvp = NULL; 1533 uint64_t first, end; 1534 1535 if (vp != NULL) 1536 ASSERT_VOP_UNLOCKED(vp, "nfsrv_freeallnfslocks: vnode locked"); 1537 lop = LIST_FIRST(&stp->ls_lock); 1538 while (lop != LIST_END(&stp->ls_lock)) { 1539 nlop = LIST_NEXT(lop, lo_lckowner); 1540 /* 1541 * Since all locks should be for the same file, lfp should 1542 * not change. 1543 */ 1544 if (lfp == NULL) 1545 lfp = lop->lo_lfp; 1546 else if (lfp != lop->lo_lfp) 1547 panic("allnfslocks"); 1548 /* 1549 * If vp is NULL and cansleep != 0, a vnode must be acquired 1550 * from the file handle. This only occurs when called from 1551 * nfsrv_cleanclient(). 1552 */ 1553 if (gottvp == 0) { 1554 if (nfsrv_dolocallocks == 0) 1555 tvp = NULL; 1556 else if (vp == NULL && cansleep != 0) { 1557 tvp = nfsvno_getvp(&lfp->lf_fh); 1558 if (tvp != NULL) 1559 NFSVOPUNLOCK(tvp); 1560 } else 1561 tvp = vp; 1562 gottvp = 1; 1563 } 1564 1565 if (tvp != NULL) { 1566 if (cansleep == 0) 1567 panic("allnfs2"); 1568 first = lop->lo_first; 1569 end = lop->lo_end; 1570 nfsrv_freenfslock(lop); 1571 nfsrv_localunlock(tvp, lfp, first, end, p); 1572 LIST_FOREACH_SAFE(rlp, &lfp->lf_rollback, rlck_list, 1573 nrlp) 1574 free(rlp, M_NFSDROLLBACK); 1575 LIST_INIT(&lfp->lf_rollback); 1576 } else 1577 nfsrv_freenfslock(lop); 1578 lop = nlop; 1579 } 1580 if (vp == NULL && tvp != NULL) 1581 vrele(tvp); 1582 } 1583 1584 /* 1585 * Free an nfslock structure. 1586 */ 1587 static void 1588 nfsrv_freenfslock(struct nfslock *lop) 1589 { 1590 1591 if (lop->lo_lckfile.le_prev != NULL) { 1592 LIST_REMOVE(lop, lo_lckfile); 1593 nfsstatsv1.srvlocks--; 1594 nfsrv_openpluslock--; 1595 } 1596 LIST_REMOVE(lop, lo_lckowner); 1597 free(lop, M_NFSDLOCK); 1598 } 1599 1600 /* 1601 * This function frees an nfslockfile structure. 1602 */ 1603 static void 1604 nfsrv_freenfslockfile(struct nfslockfile *lfp) 1605 { 1606 1607 LIST_REMOVE(lfp, lf_hash); 1608 free(lfp, M_NFSDLOCKFILE); 1609 } 1610 1611 /* 1612 * This function looks up an nfsstate structure via stateid. 1613 */ 1614 static int 1615 nfsrv_getstate(struct nfsclient *clp, nfsv4stateid_t *stateidp, __unused u_int32_t flags, 1616 struct nfsstate **stpp) 1617 { 1618 struct nfsstate *stp; 1619 struct nfsstatehead *hp; 1620 int error = 0; 1621 1622 *stpp = NULL; 1623 hp = NFSSTATEHASH(clp, *stateidp); 1624 LIST_FOREACH(stp, hp, ls_hash) { 1625 if (!NFSBCMP(stp->ls_stateid.other, stateidp->other, 1626 NFSX_STATEIDOTHER)) 1627 break; 1628 } 1629 1630 /* 1631 * If no state id in list, return NFSERR_BADSTATEID. 1632 */ 1633 if (stp == LIST_END(hp)) { 1634 error = NFSERR_BADSTATEID; 1635 goto out; 1636 } 1637 *stpp = stp; 1638 1639 out: 1640 NFSEXITCODE(error); 1641 return (error); 1642 } 1643 1644 /* 1645 * This function gets an nfsstate structure via owner string. 1646 */ 1647 static void 1648 nfsrv_getowner(struct nfsstatehead *hp, struct nfsstate *new_stp, 1649 struct nfsstate **stpp) 1650 { 1651 struct nfsstate *stp; 1652 1653 *stpp = NULL; 1654 LIST_FOREACH(stp, hp, ls_list) { 1655 if (new_stp->ls_ownerlen == stp->ls_ownerlen && 1656 !NFSBCMP(new_stp->ls_owner,stp->ls_owner,stp->ls_ownerlen)) { 1657 *stpp = stp; 1658 return; 1659 } 1660 } 1661 } 1662 1663 /* 1664 * Lock control function called to update lock status. 1665 * Returns 0 upon success, -1 if there is no lock and the flags indicate 1666 * that one isn't to be created and an NFSERR_xxx for other errors. 1667 * The structures new_stp and new_lop are passed in as pointers that should 1668 * be set to NULL if the structure is used and shouldn't be free'd. 1669 * For the NFSLCK_TEST and NFSLCK_CHECK cases, the structures are 1670 * never used and can safely be allocated on the stack. For all other 1671 * cases, *new_stpp and *new_lopp should be malloc'd before the call, 1672 * in case they are used. 1673 */ 1674 int 1675 nfsrv_lockctrl(vnode_t vp, struct nfsstate **new_stpp, 1676 struct nfslock **new_lopp, struct nfslockconflict *cfp, 1677 nfsquad_t clientid, nfsv4stateid_t *stateidp, 1678 __unused struct nfsexstuff *exp, 1679 struct nfsrv_descript *nd, NFSPROC_T *p) 1680 { 1681 struct nfslock *lop; 1682 struct nfsstate *new_stp = *new_stpp; 1683 struct nfslock *new_lop = *new_lopp; 1684 struct nfsstate *tstp, *mystp, *nstp; 1685 int specialid = 0; 1686 struct nfslockfile *lfp; 1687 struct nfslock *other_lop = NULL; 1688 struct nfsstate *stp, *lckstp = NULL; 1689 struct nfsclient *clp = NULL; 1690 u_int32_t bits; 1691 int error = 0, haslock = 0, ret, reterr; 1692 int getlckret, delegation = 0, filestruct_locked, vnode_unlocked = 0; 1693 fhandle_t nfh; 1694 uint64_t first, end; 1695 uint32_t lock_flags; 1696 1697 if (new_stp->ls_flags & (NFSLCK_CHECK | NFSLCK_SETATTR)) { 1698 /* 1699 * Note the special cases of "all 1s" or "all 0s" stateids and 1700 * let reads with all 1s go ahead. 1701 */ 1702 if (new_stp->ls_stateid.seqid == 0x0 && 1703 new_stp->ls_stateid.other[0] == 0x0 && 1704 new_stp->ls_stateid.other[1] == 0x0 && 1705 new_stp->ls_stateid.other[2] == 0x0) 1706 specialid = 1; 1707 else if (new_stp->ls_stateid.seqid == 0xffffffff && 1708 new_stp->ls_stateid.other[0] == 0xffffffff && 1709 new_stp->ls_stateid.other[1] == 0xffffffff && 1710 new_stp->ls_stateid.other[2] == 0xffffffff) 1711 specialid = 2; 1712 } 1713 1714 /* 1715 * Check for restart conditions (client and server). 1716 */ 1717 error = nfsrv_checkrestart(clientid, new_stp->ls_flags, 1718 &new_stp->ls_stateid, specialid); 1719 if (error) 1720 goto out; 1721 1722 /* 1723 * Check for state resource limit exceeded. 1724 */ 1725 if ((new_stp->ls_flags & NFSLCK_LOCK) && 1726 nfsrv_openpluslock > nfsrv_v4statelimit) { 1727 error = NFSERR_RESOURCE; 1728 goto out; 1729 } 1730 1731 /* 1732 * For the lock case, get another nfslock structure, 1733 * just in case we need it. 1734 * Malloc now, before we start sifting through the linked lists, 1735 * in case we have to wait for memory. 1736 */ 1737 tryagain: 1738 if (new_stp->ls_flags & NFSLCK_LOCK) 1739 other_lop = malloc(sizeof (struct nfslock), 1740 M_NFSDLOCK, M_WAITOK); 1741 filestruct_locked = 0; 1742 reterr = 0; 1743 lfp = NULL; 1744 1745 /* 1746 * Get the lockfile structure for CFH now, so we can do a sanity 1747 * check against the stateid, before incrementing the seqid#, since 1748 * we want to return NFSERR_BADSTATEID on failure and the seqid# 1749 * shouldn't be incremented for this case. 1750 * If nfsrv_getlockfile() returns -1, it means "not found", which 1751 * will be handled later. 1752 * If we are doing Lock/LockU and local locking is enabled, sleep 1753 * lock the nfslockfile structure. 1754 */ 1755 getlckret = nfsrv_getlockfh(vp, new_stp->ls_flags, NULL, &nfh, p); 1756 NFSLOCKSTATE(); 1757 if (getlckret == 0) { 1758 if ((new_stp->ls_flags & (NFSLCK_LOCK | NFSLCK_UNLOCK)) != 0 && 1759 nfsrv_dolocallocks != 0 && nd->nd_repstat == 0) { 1760 getlckret = nfsrv_getlockfile(new_stp->ls_flags, NULL, 1761 &lfp, &nfh, 1); 1762 if (getlckret == 0) 1763 filestruct_locked = 1; 1764 } else 1765 getlckret = nfsrv_getlockfile(new_stp->ls_flags, NULL, 1766 &lfp, &nfh, 0); 1767 } 1768 if (getlckret != 0 && getlckret != -1) 1769 reterr = getlckret; 1770 1771 if (filestruct_locked != 0) { 1772 LIST_INIT(&lfp->lf_rollback); 1773 if ((new_stp->ls_flags & NFSLCK_LOCK)) { 1774 /* 1775 * For local locking, do the advisory locking now, so 1776 * that any conflict can be detected. A failure later 1777 * can be rolled back locally. If an error is returned, 1778 * struct nfslockfile has been unlocked and any local 1779 * locking rolled back. 1780 */ 1781 NFSUNLOCKSTATE(); 1782 if (vnode_unlocked == 0) { 1783 ASSERT_VOP_ELOCKED(vp, "nfsrv_lockctrl1"); 1784 vnode_unlocked = 1; 1785 NFSVOPUNLOCK(vp); 1786 } 1787 reterr = nfsrv_locallock(vp, lfp, 1788 (new_lop->lo_flags & (NFSLCK_READ | NFSLCK_WRITE)), 1789 new_lop->lo_first, new_lop->lo_end, cfp, p); 1790 NFSLOCKSTATE(); 1791 } 1792 } 1793 1794 if (specialid == 0) { 1795 if (new_stp->ls_flags & NFSLCK_TEST) { 1796 /* 1797 * RFC 3530 does not list LockT as an op that renews a 1798 * lease, but the consensus seems to be that it is ok 1799 * for a server to do so. 1800 */ 1801 error = nfsrv_getclient(clientid, CLOPS_RENEW, &clp, NULL, 1802 (nfsquad_t)((u_quad_t)0), 0, nd, p); 1803 1804 /* 1805 * Since NFSERR_EXPIRED, NFSERR_ADMINREVOKED are not valid 1806 * error returns for LockT, just go ahead and test for a lock, 1807 * since there are no locks for this client, but other locks 1808 * can conflict. (ie. same client will always be false) 1809 */ 1810 if (error == NFSERR_EXPIRED || error == NFSERR_ADMINREVOKED) 1811 error = 0; 1812 lckstp = new_stp; 1813 } else { 1814 error = nfsrv_getclient(clientid, CLOPS_RENEW, &clp, NULL, 1815 (nfsquad_t)((u_quad_t)0), 0, nd, p); 1816 if (error == 0) 1817 /* 1818 * Look up the stateid 1819 */ 1820 error = nfsrv_getstate(clp, &new_stp->ls_stateid, 1821 new_stp->ls_flags, &stp); 1822 /* 1823 * do some sanity checks for an unconfirmed open or a 1824 * stateid that refers to the wrong file, for an open stateid 1825 */ 1826 if (error == 0 && (stp->ls_flags & NFSLCK_OPEN) && 1827 ((stp->ls_openowner->ls_flags & NFSLCK_NEEDSCONFIRM) || 1828 (getlckret == 0 && stp->ls_lfp != lfp))){ 1829 /* 1830 * NFSLCK_SETATTR should return OK rather than NFSERR_BADSTATEID 1831 * The only exception is using SETATTR with SIZE. 1832 * */ 1833 if ((new_stp->ls_flags & 1834 (NFSLCK_SETATTR | NFSLCK_CHECK)) != NFSLCK_SETATTR) 1835 error = NFSERR_BADSTATEID; 1836 } 1837 1838 if (error == 0 && 1839 (stp->ls_flags & (NFSLCK_DELEGREAD | NFSLCK_DELEGWRITE)) && 1840 getlckret == 0 && stp->ls_lfp != lfp) 1841 error = NFSERR_BADSTATEID; 1842 1843 /* 1844 * If the lockowner stateid doesn't refer to the same file, 1845 * I believe that is considered ok, since some clients will 1846 * only create a single lockowner and use that for all locks 1847 * on all files. 1848 * For now, log it as a diagnostic, instead of considering it 1849 * a BadStateid. 1850 */ 1851 if (error == 0 && (stp->ls_flags & 1852 (NFSLCK_OPEN | NFSLCK_DELEGREAD | NFSLCK_DELEGWRITE)) == 0 && 1853 getlckret == 0 && stp->ls_lfp != lfp) { 1854 #ifdef DIAGNOSTIC 1855 printf("Got a lock statid for different file open\n"); 1856 #endif 1857 /* 1858 error = NFSERR_BADSTATEID; 1859 */ 1860 } 1861 1862 if (error == 0) { 1863 if (new_stp->ls_flags & NFSLCK_OPENTOLOCK) { 1864 /* 1865 * If haslock set, we've already checked the seqid. 1866 */ 1867 if (!haslock) { 1868 if (stp->ls_flags & NFSLCK_OPEN) 1869 error = nfsrv_checkseqid(nd, new_stp->ls_seq, 1870 stp->ls_openowner, new_stp->ls_op); 1871 else 1872 error = NFSERR_BADSTATEID; 1873 } 1874 if (!error) 1875 nfsrv_getowner(&stp->ls_open, new_stp, &lckstp); 1876 if (lckstp) { 1877 /* 1878 * For NFSv4.1 and NFSv4.2 allow an 1879 * open_to_lock_owner when the lock_owner already 1880 * exists. Just clear NFSLCK_OPENTOLOCK so that 1881 * a new lock_owner will not be created. 1882 * RFC7530 states that the error for NFSv4.0 1883 * is NFS4ERR_BAD_SEQID. 1884 */ 1885 if ((nd->nd_flag & ND_NFSV41) != 0) 1886 new_stp->ls_flags &= ~NFSLCK_OPENTOLOCK; 1887 else 1888 error = NFSERR_BADSEQID; 1889 } else 1890 lckstp = new_stp; 1891 } else if (new_stp->ls_flags&(NFSLCK_LOCK|NFSLCK_UNLOCK)) { 1892 /* 1893 * If haslock set, ditto above. 1894 */ 1895 if (!haslock) { 1896 if (stp->ls_flags & NFSLCK_OPEN) 1897 error = NFSERR_BADSTATEID; 1898 else 1899 error = nfsrv_checkseqid(nd, new_stp->ls_seq, 1900 stp, new_stp->ls_op); 1901 } 1902 lckstp = stp; 1903 } else { 1904 lckstp = stp; 1905 } 1906 } 1907 /* 1908 * If the seqid part of the stateid isn't the same, return 1909 * NFSERR_OLDSTATEID for cases other than I/O Ops. 1910 * For I/O Ops, only return NFSERR_OLDSTATEID if 1911 * nfsrv_returnoldstateid is set. (The consensus on the email 1912 * list was that most clients would prefer to not receive 1913 * NFSERR_OLDSTATEID for I/O Ops, but the RFC suggests that that 1914 * is what will happen, so I use the nfsrv_returnoldstateid to 1915 * allow for either server configuration.) 1916 */ 1917 if (!error && stp->ls_stateid.seqid!=new_stp->ls_stateid.seqid && 1918 (((nd->nd_flag & ND_NFSV41) == 0 && 1919 (!(new_stp->ls_flags & NFSLCK_CHECK) || 1920 nfsrv_returnoldstateid)) || 1921 ((nd->nd_flag & ND_NFSV41) != 0 && 1922 new_stp->ls_stateid.seqid != 0))) 1923 error = NFSERR_OLDSTATEID; 1924 } 1925 } 1926 1927 /* 1928 * Now we can check for grace. 1929 */ 1930 if (!error) 1931 error = nfsrv_checkgrace(nd, clp, new_stp->ls_flags); 1932 if ((new_stp->ls_flags & NFSLCK_RECLAIM) && !error && 1933 nfsrv_checkstable(clp)) 1934 error = NFSERR_NOGRACE; 1935 /* 1936 * If we successfully Reclaimed state, note that. 1937 */ 1938 if ((new_stp->ls_flags & NFSLCK_RECLAIM) && !error) 1939 nfsrv_markstable(clp); 1940 1941 /* 1942 * At this point, either error == NFSERR_BADSTATEID or the 1943 * seqid# has been updated, so we can return any error. 1944 * If error == 0, there may be an error in: 1945 * nd_repstat - Set by the calling function. 1946 * reterr - Set above, if getting the nfslockfile structure 1947 * or acquiring the local lock failed. 1948 * (If both of these are set, nd_repstat should probably be 1949 * returned, since that error was detected before this 1950 * function call.) 1951 */ 1952 if (error != 0 || nd->nd_repstat != 0 || reterr != 0) { 1953 if (error == 0) { 1954 if (nd->nd_repstat != 0) 1955 error = nd->nd_repstat; 1956 else 1957 error = reterr; 1958 } 1959 if (filestruct_locked != 0) { 1960 /* Roll back local locks. */ 1961 NFSUNLOCKSTATE(); 1962 if (vnode_unlocked == 0) { 1963 ASSERT_VOP_ELOCKED(vp, "nfsrv_lockctrl2"); 1964 vnode_unlocked = 1; 1965 NFSVOPUNLOCK(vp); 1966 } 1967 nfsrv_locallock_rollback(vp, lfp, p); 1968 NFSLOCKSTATE(); 1969 nfsrv_unlocklf(lfp); 1970 } 1971 NFSUNLOCKSTATE(); 1972 goto out; 1973 } 1974 1975 /* 1976 * Check the nfsrv_getlockfile return. 1977 * Returned -1 if no structure found. 1978 */ 1979 if (getlckret == -1) { 1980 error = NFSERR_EXPIRED; 1981 /* 1982 * Called from lockt, so no lock is OK. 1983 */ 1984 if (new_stp->ls_flags & NFSLCK_TEST) { 1985 error = 0; 1986 } else if (new_stp->ls_flags & 1987 (NFSLCK_CHECK | NFSLCK_SETATTR)) { 1988 /* 1989 * Called to check for a lock, OK if the stateid is all 1990 * 1s or all 0s, but there should be an nfsstate 1991 * otherwise. 1992 * (ie. If there is no open, I'll assume no share 1993 * deny bits.) 1994 */ 1995 if (specialid) 1996 error = 0; 1997 else 1998 error = NFSERR_BADSTATEID; 1999 } 2000 NFSUNLOCKSTATE(); 2001 goto out; 2002 } 2003 2004 /* 2005 * For NFSLCK_CHECK and NFSLCK_LOCK, test for a share conflict. 2006 * For NFSLCK_CHECK, allow a read if write access is granted, 2007 * but check for a deny. For NFSLCK_LOCK, require correct access, 2008 * which implies a conflicting deny can't exist. 2009 */ 2010 if (new_stp->ls_flags & (NFSLCK_CHECK | NFSLCK_LOCK)) { 2011 /* 2012 * Four kinds of state id: 2013 * - specialid (all 0s or all 1s), only for NFSLCK_CHECK 2014 * - stateid for an open 2015 * - stateid for a delegation 2016 * - stateid for a lock owner 2017 */ 2018 if (!specialid) { 2019 if (stp->ls_flags & (NFSLCK_DELEGREAD | NFSLCK_DELEGWRITE)) { 2020 delegation = 1; 2021 mystp = stp; 2022 nfsrv_delaydelegtimeout(stp); 2023 } else if (stp->ls_flags & NFSLCK_OPEN) { 2024 mystp = stp; 2025 } else { 2026 mystp = stp->ls_openstp; 2027 } 2028 /* 2029 * If locking or checking, require correct access 2030 * bit set. 2031 */ 2032 if (((new_stp->ls_flags & NFSLCK_LOCK) && 2033 !((new_lop->lo_flags >> NFSLCK_LOCKSHIFT) & 2034 mystp->ls_flags & NFSLCK_ACCESSBITS)) || 2035 ((new_stp->ls_flags & (NFSLCK_CHECK|NFSLCK_READACCESS)) == 2036 (NFSLCK_CHECK | NFSLCK_READACCESS) && 2037 !(mystp->ls_flags & NFSLCK_READACCESS) && 2038 nfsrv_allowreadforwriteopen == 0) || 2039 ((new_stp->ls_flags & (NFSLCK_CHECK|NFSLCK_WRITEACCESS)) == 2040 (NFSLCK_CHECK | NFSLCK_WRITEACCESS) && 2041 !(mystp->ls_flags & NFSLCK_WRITEACCESS))) { 2042 if (filestruct_locked != 0) { 2043 /* Roll back local locks. */ 2044 NFSUNLOCKSTATE(); 2045 if (vnode_unlocked == 0) { 2046 ASSERT_VOP_ELOCKED(vp, 2047 "nfsrv_lockctrl3"); 2048 vnode_unlocked = 1; 2049 NFSVOPUNLOCK(vp); 2050 } 2051 nfsrv_locallock_rollback(vp, lfp, p); 2052 NFSLOCKSTATE(); 2053 nfsrv_unlocklf(lfp); 2054 } 2055 NFSUNLOCKSTATE(); 2056 error = NFSERR_OPENMODE; 2057 goto out; 2058 } 2059 } else 2060 mystp = NULL; 2061 if ((new_stp->ls_flags & NFSLCK_CHECK) && !delegation) { 2062 /* 2063 * Check for a conflicting deny bit. 2064 */ 2065 LIST_FOREACH(tstp, &lfp->lf_open, ls_file) { 2066 if (tstp != mystp) { 2067 bits = tstp->ls_flags; 2068 bits >>= NFSLCK_SHIFT; 2069 if (new_stp->ls_flags & bits & NFSLCK_ACCESSBITS) { 2070 KASSERT(vnode_unlocked == 0, 2071 ("nfsrv_lockctrl: vnode unlocked1")); 2072 ret = nfsrv_clientconflict(tstp->ls_clp, &haslock, 2073 vp, p); 2074 if (ret == 1) { 2075 /* 2076 * nfsrv_clientconflict unlocks state 2077 * when it returns non-zero. 2078 */ 2079 lckstp = NULL; 2080 goto tryagain; 2081 } 2082 if (ret == 0) 2083 NFSUNLOCKSTATE(); 2084 if (ret == 2) 2085 error = NFSERR_PERM; 2086 else 2087 error = NFSERR_OPENMODE; 2088 goto out; 2089 } 2090 } 2091 } 2092 2093 /* We're outta here */ 2094 NFSUNLOCKSTATE(); 2095 goto out; 2096 } 2097 } 2098 2099 /* 2100 * For setattr, just get rid of all the Delegations for other clients. 2101 */ 2102 if (new_stp->ls_flags & NFSLCK_SETATTR) { 2103 KASSERT(vnode_unlocked == 0, 2104 ("nfsrv_lockctrl: vnode unlocked2")); 2105 ret = nfsrv_cleandeleg(vp, lfp, clp, &haslock, p); 2106 if (ret) { 2107 /* 2108 * nfsrv_cleandeleg() unlocks state when it 2109 * returns non-zero. 2110 */ 2111 if (ret == -1) { 2112 lckstp = NULL; 2113 goto tryagain; 2114 } 2115 error = ret; 2116 goto out; 2117 } 2118 if (!(new_stp->ls_flags & NFSLCK_CHECK) || 2119 (LIST_EMPTY(&lfp->lf_open) && LIST_EMPTY(&lfp->lf_lock) && 2120 LIST_EMPTY(&lfp->lf_deleg))) { 2121 NFSUNLOCKSTATE(); 2122 goto out; 2123 } 2124 } 2125 2126 /* 2127 * Check for a conflicting delegation. If one is found, call 2128 * nfsrv_delegconflict() to handle it. If the v4root lock hasn't 2129 * been set yet, it will get the lock. Otherwise, it will recall 2130 * the delegation. Then, we try try again... 2131 * I currently believe the conflict algorithm to be: 2132 * For Lock Ops (Lock/LockT/LockU) 2133 * - there is a conflict iff a different client has a write delegation 2134 * For Reading (Read Op) 2135 * - there is a conflict iff a different client has a write delegation 2136 * (the specialids are always a different client) 2137 * For Writing (Write/Setattr of size) 2138 * - there is a conflict if a different client has any delegation 2139 * - there is a conflict if the same client has a read delegation 2140 * (I don't understand why this isn't allowed, but that seems to be 2141 * the current consensus?) 2142 */ 2143 tstp = LIST_FIRST(&lfp->lf_deleg); 2144 while (tstp != LIST_END(&lfp->lf_deleg)) { 2145 nstp = LIST_NEXT(tstp, ls_file); 2146 if ((((new_stp->ls_flags&(NFSLCK_LOCK|NFSLCK_UNLOCK|NFSLCK_TEST))|| 2147 ((new_stp->ls_flags & NFSLCK_CHECK) && 2148 (new_lop->lo_flags & NFSLCK_READ))) && 2149 clp != tstp->ls_clp && 2150 (tstp->ls_flags & NFSLCK_DELEGWRITE)) || 2151 ((new_stp->ls_flags & NFSLCK_CHECK) && 2152 (new_lop->lo_flags & NFSLCK_WRITE) && 2153 (clp != tstp->ls_clp || 2154 (tstp->ls_flags & NFSLCK_DELEGREAD)))) { 2155 ret = 0; 2156 if (filestruct_locked != 0) { 2157 /* Roll back local locks. */ 2158 NFSUNLOCKSTATE(); 2159 if (vnode_unlocked == 0) { 2160 ASSERT_VOP_ELOCKED(vp, "nfsrv_lockctrl4"); 2161 NFSVOPUNLOCK(vp); 2162 } 2163 nfsrv_locallock_rollback(vp, lfp, p); 2164 NFSLOCKSTATE(); 2165 nfsrv_unlocklf(lfp); 2166 NFSUNLOCKSTATE(); 2167 NFSVOPLOCK(vp, LK_EXCLUSIVE | LK_RETRY); 2168 vnode_unlocked = 0; 2169 if (VN_IS_DOOMED(vp)) 2170 ret = NFSERR_SERVERFAULT; 2171 NFSLOCKSTATE(); 2172 } 2173 if (ret == 0) 2174 ret = nfsrv_delegconflict(tstp, &haslock, p, vp); 2175 if (ret) { 2176 /* 2177 * nfsrv_delegconflict unlocks state when it 2178 * returns non-zero, which it always does. 2179 */ 2180 if (other_lop) { 2181 free(other_lop, M_NFSDLOCK); 2182 other_lop = NULL; 2183 } 2184 if (ret == -1) { 2185 lckstp = NULL; 2186 goto tryagain; 2187 } 2188 error = ret; 2189 goto out; 2190 } 2191 /* Never gets here. */ 2192 } 2193 tstp = nstp; 2194 } 2195 2196 /* 2197 * Handle the unlock case by calling nfsrv_updatelock(). 2198 * (Should I have done some access checking above for unlock? For now, 2199 * just let it happen.) 2200 */ 2201 if (new_stp->ls_flags & NFSLCK_UNLOCK) { 2202 first = new_lop->lo_first; 2203 end = new_lop->lo_end; 2204 nfsrv_updatelock(stp, new_lopp, &other_lop, lfp); 2205 stateidp->seqid = ++(stp->ls_stateid.seqid); 2206 if ((nd->nd_flag & ND_NFSV41) != 0 && stateidp->seqid == 0) 2207 stateidp->seqid = stp->ls_stateid.seqid = 1; 2208 stateidp->other[0] = stp->ls_stateid.other[0]; 2209 stateidp->other[1] = stp->ls_stateid.other[1]; 2210 stateidp->other[2] = stp->ls_stateid.other[2]; 2211 if (filestruct_locked != 0) { 2212 NFSUNLOCKSTATE(); 2213 if (vnode_unlocked == 0) { 2214 ASSERT_VOP_ELOCKED(vp, "nfsrv_lockctrl5"); 2215 vnode_unlocked = 1; 2216 NFSVOPUNLOCK(vp); 2217 } 2218 /* Update the local locks. */ 2219 nfsrv_localunlock(vp, lfp, first, end, p); 2220 NFSLOCKSTATE(); 2221 nfsrv_unlocklf(lfp); 2222 } 2223 NFSUNLOCKSTATE(); 2224 goto out; 2225 } 2226 2227 /* 2228 * Search for a conflicting lock. A lock conflicts if: 2229 * - the lock range overlaps and 2230 * - at least one lock is a write lock and 2231 * - it is not owned by the same lock owner 2232 */ 2233 if (!delegation) { 2234 LIST_FOREACH(lop, &lfp->lf_lock, lo_lckfile) { 2235 if (new_lop->lo_end > lop->lo_first && 2236 new_lop->lo_first < lop->lo_end && 2237 (new_lop->lo_flags == NFSLCK_WRITE || 2238 lop->lo_flags == NFSLCK_WRITE) && 2239 lckstp != lop->lo_stp && 2240 (clp != lop->lo_stp->ls_clp || 2241 lckstp->ls_ownerlen != lop->lo_stp->ls_ownerlen || 2242 NFSBCMP(lckstp->ls_owner, lop->lo_stp->ls_owner, 2243 lckstp->ls_ownerlen))) { 2244 if (other_lop) { 2245 free(other_lop, M_NFSDLOCK); 2246 other_lop = NULL; 2247 } 2248 if (vnode_unlocked != 0) 2249 ret = nfsrv_clientconflict(lop->lo_stp->ls_clp, &haslock, 2250 NULL, p); 2251 else 2252 ret = nfsrv_clientconflict(lop->lo_stp->ls_clp, &haslock, 2253 vp, p); 2254 if (ret == 1) { 2255 if (filestruct_locked != 0) { 2256 if (vnode_unlocked == 0) { 2257 ASSERT_VOP_ELOCKED(vp, "nfsrv_lockctrl6"); 2258 NFSVOPUNLOCK(vp); 2259 } 2260 /* Roll back local locks. */ 2261 nfsrv_locallock_rollback(vp, lfp, p); 2262 NFSLOCKSTATE(); 2263 nfsrv_unlocklf(lfp); 2264 NFSUNLOCKSTATE(); 2265 NFSVOPLOCK(vp, LK_EXCLUSIVE | LK_RETRY); 2266 vnode_unlocked = 0; 2267 if (VN_IS_DOOMED(vp)) { 2268 error = NFSERR_SERVERFAULT; 2269 goto out; 2270 } 2271 } 2272 /* 2273 * nfsrv_clientconflict() unlocks state when it 2274 * returns non-zero. 2275 */ 2276 lckstp = NULL; 2277 goto tryagain; 2278 } 2279 /* 2280 * Found a conflicting lock, so record the conflict and 2281 * return the error. 2282 */ 2283 if (cfp != NULL && ret == 0) { 2284 cfp->cl_clientid.lval[0]=lop->lo_stp->ls_stateid.other[0]; 2285 cfp->cl_clientid.lval[1]=lop->lo_stp->ls_stateid.other[1]; 2286 cfp->cl_first = lop->lo_first; 2287 cfp->cl_end = lop->lo_end; 2288 cfp->cl_flags = lop->lo_flags; 2289 cfp->cl_ownerlen = lop->lo_stp->ls_ownerlen; 2290 NFSBCOPY(lop->lo_stp->ls_owner, cfp->cl_owner, 2291 cfp->cl_ownerlen); 2292 } 2293 if (ret == 2) 2294 error = NFSERR_PERM; 2295 else if (new_stp->ls_flags & NFSLCK_RECLAIM) 2296 error = NFSERR_RECLAIMCONFLICT; 2297 else if (new_stp->ls_flags & NFSLCK_CHECK) 2298 error = NFSERR_LOCKED; 2299 else 2300 error = NFSERR_DENIED; 2301 if (filestruct_locked != 0 && ret == 0) { 2302 /* Roll back local locks. */ 2303 NFSUNLOCKSTATE(); 2304 if (vnode_unlocked == 0) { 2305 ASSERT_VOP_ELOCKED(vp, "nfsrv_lockctrl7"); 2306 vnode_unlocked = 1; 2307 NFSVOPUNLOCK(vp); 2308 } 2309 nfsrv_locallock_rollback(vp, lfp, p); 2310 NFSLOCKSTATE(); 2311 nfsrv_unlocklf(lfp); 2312 } 2313 if (ret == 0) 2314 NFSUNLOCKSTATE(); 2315 goto out; 2316 } 2317 } 2318 } 2319 2320 /* 2321 * We only get here if there was no lock that conflicted. 2322 */ 2323 if (new_stp->ls_flags & (NFSLCK_TEST | NFSLCK_CHECK)) { 2324 NFSUNLOCKSTATE(); 2325 goto out; 2326 } 2327 2328 /* 2329 * We only get here when we are creating or modifying a lock. 2330 * There are two variants: 2331 * - exist_lock_owner where lock_owner exists 2332 * - open_to_lock_owner with new lock_owner 2333 */ 2334 first = new_lop->lo_first; 2335 end = new_lop->lo_end; 2336 lock_flags = new_lop->lo_flags; 2337 if (!(new_stp->ls_flags & NFSLCK_OPENTOLOCK)) { 2338 nfsrv_updatelock(lckstp, new_lopp, &other_lop, lfp); 2339 stateidp->seqid = ++(lckstp->ls_stateid.seqid); 2340 if ((nd->nd_flag & ND_NFSV41) != 0 && stateidp->seqid == 0) 2341 stateidp->seqid = lckstp->ls_stateid.seqid = 1; 2342 stateidp->other[0] = lckstp->ls_stateid.other[0]; 2343 stateidp->other[1] = lckstp->ls_stateid.other[1]; 2344 stateidp->other[2] = lckstp->ls_stateid.other[2]; 2345 } else { 2346 /* 2347 * The new open_to_lock_owner case. 2348 * Link the new nfsstate into the lists. 2349 */ 2350 new_stp->ls_seq = new_stp->ls_opentolockseq; 2351 nfsrvd_refcache(new_stp->ls_op); 2352 stateidp->seqid = new_stp->ls_stateid.seqid = 1; 2353 stateidp->other[0] = new_stp->ls_stateid.other[0] = 2354 clp->lc_clientid.lval[0]; 2355 stateidp->other[1] = new_stp->ls_stateid.other[1] = 2356 clp->lc_clientid.lval[1]; 2357 stateidp->other[2] = new_stp->ls_stateid.other[2] = 2358 nfsrv_nextstateindex(clp); 2359 new_stp->ls_clp = clp; 2360 LIST_INIT(&new_stp->ls_lock); 2361 new_stp->ls_openstp = stp; 2362 new_stp->ls_lfp = lfp; 2363 nfsrv_insertlock(new_lop, (struct nfslock *)new_stp, new_stp, 2364 lfp); 2365 LIST_INSERT_HEAD(NFSSTATEHASH(clp, new_stp->ls_stateid), 2366 new_stp, ls_hash); 2367 LIST_INSERT_HEAD(&stp->ls_open, new_stp, ls_list); 2368 *new_lopp = NULL; 2369 *new_stpp = NULL; 2370 nfsstatsv1.srvlockowners++; 2371 nfsrv_openpluslock++; 2372 } 2373 if (filestruct_locked != 0) { 2374 NFSUNLOCKSTATE(); 2375 nfsrv_locallock_commit(lfp, lock_flags, first, end); 2376 NFSLOCKSTATE(); 2377 nfsrv_unlocklf(lfp); 2378 } 2379 NFSUNLOCKSTATE(); 2380 2381 out: 2382 if (haslock) { 2383 NFSLOCKV4ROOTMUTEX(); 2384 nfsv4_unlock(&nfsv4rootfs_lock, 1); 2385 NFSUNLOCKV4ROOTMUTEX(); 2386 } 2387 if (vnode_unlocked != 0) { 2388 NFSVOPLOCK(vp, LK_EXCLUSIVE | LK_RETRY); 2389 if (error == 0 && VN_IS_DOOMED(vp)) 2390 error = NFSERR_SERVERFAULT; 2391 } 2392 if (other_lop) 2393 free(other_lop, M_NFSDLOCK); 2394 NFSEXITCODE2(error, nd); 2395 return (error); 2396 } 2397 2398 /* 2399 * Check for state errors for Open. 2400 * repstat is passed back out as an error if more critical errors 2401 * are not detected. 2402 */ 2403 int 2404 nfsrv_opencheck(nfsquad_t clientid, nfsv4stateid_t *stateidp, 2405 struct nfsstate *new_stp, vnode_t vp, struct nfsrv_descript *nd, 2406 NFSPROC_T *p, int repstat) 2407 { 2408 struct nfsstate *stp, *nstp; 2409 struct nfsclient *clp; 2410 struct nfsstate *ownerstp; 2411 struct nfslockfile *lfp, *new_lfp; 2412 int error = 0, haslock = 0, ret, readonly = 0, getfhret = 0; 2413 2414 if ((new_stp->ls_flags & NFSLCK_SHAREBITS) == NFSLCK_READACCESS) 2415 readonly = 1; 2416 /* 2417 * Check for restart conditions (client and server). 2418 */ 2419 error = nfsrv_checkrestart(clientid, new_stp->ls_flags, 2420 &new_stp->ls_stateid, 0); 2421 if (error) 2422 goto out; 2423 2424 /* 2425 * Check for state resource limit exceeded. 2426 * Technically this should be SMP protected, but the worst 2427 * case error is "out by one or two" on the count when it 2428 * returns NFSERR_RESOURCE and the limit is just a rather 2429 * arbitrary high water mark, so no harm is done. 2430 */ 2431 if (nfsrv_openpluslock > nfsrv_v4statelimit) { 2432 error = NFSERR_RESOURCE; 2433 goto out; 2434 } 2435 2436 tryagain: 2437 new_lfp = malloc(sizeof (struct nfslockfile), 2438 M_NFSDLOCKFILE, M_WAITOK); 2439 if (vp) 2440 getfhret = nfsrv_getlockfh(vp, new_stp->ls_flags, new_lfp, 2441 NULL, p); 2442 NFSLOCKSTATE(); 2443 /* 2444 * Get the nfsclient structure. 2445 */ 2446 error = nfsrv_getclient(clientid, CLOPS_RENEW, &clp, NULL, 2447 (nfsquad_t)((u_quad_t)0), 0, nd, p); 2448 2449 /* 2450 * Look up the open owner. See if it needs confirmation and 2451 * check the seq#, as required. 2452 */ 2453 if (!error) 2454 nfsrv_getowner(&clp->lc_open, new_stp, &ownerstp); 2455 2456 if (!error && ownerstp) { 2457 error = nfsrv_checkseqid(nd, new_stp->ls_seq, ownerstp, 2458 new_stp->ls_op); 2459 /* 2460 * If the OpenOwner hasn't been confirmed, assume the 2461 * old one was a replay and this one is ok. 2462 * See: RFC3530 Sec. 14.2.18. 2463 */ 2464 if (error == NFSERR_BADSEQID && 2465 (ownerstp->ls_flags & NFSLCK_NEEDSCONFIRM)) 2466 error = 0; 2467 } 2468 2469 /* 2470 * Check for grace. 2471 */ 2472 if (!error) 2473 error = nfsrv_checkgrace(nd, clp, new_stp->ls_flags); 2474 if ((new_stp->ls_flags & NFSLCK_RECLAIM) && !error && 2475 nfsrv_checkstable(clp)) 2476 error = NFSERR_NOGRACE; 2477 2478 /* 2479 * If none of the above errors occurred, let repstat be 2480 * returned. 2481 */ 2482 if (repstat && !error) 2483 error = repstat; 2484 if (error) { 2485 NFSUNLOCKSTATE(); 2486 if (haslock) { 2487 NFSLOCKV4ROOTMUTEX(); 2488 nfsv4_unlock(&nfsv4rootfs_lock, 1); 2489 NFSUNLOCKV4ROOTMUTEX(); 2490 } 2491 free(new_lfp, M_NFSDLOCKFILE); 2492 goto out; 2493 } 2494 2495 /* 2496 * If vp == NULL, the file doesn't exist yet, so return ok. 2497 * (This always happens on the first pass, so haslock must be 0.) 2498 */ 2499 if (vp == NULL) { 2500 NFSUNLOCKSTATE(); 2501 free(new_lfp, M_NFSDLOCKFILE); 2502 goto out; 2503 } 2504 2505 /* 2506 * Get the structure for the underlying file. 2507 */ 2508 if (getfhret) 2509 error = getfhret; 2510 else 2511 error = nfsrv_getlockfile(new_stp->ls_flags, &new_lfp, &lfp, 2512 NULL, 0); 2513 if (new_lfp) 2514 free(new_lfp, M_NFSDLOCKFILE); 2515 if (error) { 2516 NFSUNLOCKSTATE(); 2517 if (haslock) { 2518 NFSLOCKV4ROOTMUTEX(); 2519 nfsv4_unlock(&nfsv4rootfs_lock, 1); 2520 NFSUNLOCKV4ROOTMUTEX(); 2521 } 2522 goto out; 2523 } 2524 2525 /* 2526 * Search for a conflicting open/share. 2527 */ 2528 if (new_stp->ls_flags & NFSLCK_DELEGCUR) { 2529 /* 2530 * For Delegate_Cur, search for the matching Delegation, 2531 * which indicates no conflict. 2532 * An old delegation should have been recovered by the 2533 * client doing a Claim_DELEGATE_Prev, so I won't let 2534 * it match and return NFSERR_EXPIRED. Should I let it 2535 * match? 2536 */ 2537 LIST_FOREACH(stp, &lfp->lf_deleg, ls_file) { 2538 if (!(stp->ls_flags & NFSLCK_OLDDELEG) && 2539 (((nd->nd_flag & ND_NFSV41) != 0 && 2540 stateidp->seqid == 0) || 2541 stateidp->seqid == stp->ls_stateid.seqid) && 2542 !NFSBCMP(stateidp->other, stp->ls_stateid.other, 2543 NFSX_STATEIDOTHER)) 2544 break; 2545 } 2546 if (stp == LIST_END(&lfp->lf_deleg) || 2547 ((new_stp->ls_flags & NFSLCK_WRITEACCESS) && 2548 (stp->ls_flags & NFSLCK_DELEGREAD))) { 2549 NFSUNLOCKSTATE(); 2550 if (haslock) { 2551 NFSLOCKV4ROOTMUTEX(); 2552 nfsv4_unlock(&nfsv4rootfs_lock, 1); 2553 NFSUNLOCKV4ROOTMUTEX(); 2554 } 2555 error = NFSERR_EXPIRED; 2556 goto out; 2557 } 2558 } 2559 2560 /* 2561 * Check for access/deny bit conflicts. I check for the same 2562 * owner as well, in case the client didn't bother. 2563 */ 2564 LIST_FOREACH(stp, &lfp->lf_open, ls_file) { 2565 if (!(new_stp->ls_flags & NFSLCK_DELEGCUR) && 2566 (((new_stp->ls_flags & NFSLCK_ACCESSBITS) & 2567 ((stp->ls_flags>>NFSLCK_SHIFT) & NFSLCK_ACCESSBITS))|| 2568 ((stp->ls_flags & NFSLCK_ACCESSBITS) & 2569 ((new_stp->ls_flags>>NFSLCK_SHIFT)&NFSLCK_ACCESSBITS)))){ 2570 ret = nfsrv_clientconflict(stp->ls_clp,&haslock,vp,p); 2571 if (ret == 1) { 2572 /* 2573 * nfsrv_clientconflict() unlocks 2574 * state when it returns non-zero. 2575 */ 2576 goto tryagain; 2577 } 2578 if (ret == 2) 2579 error = NFSERR_PERM; 2580 else if (new_stp->ls_flags & NFSLCK_RECLAIM) 2581 error = NFSERR_RECLAIMCONFLICT; 2582 else 2583 error = NFSERR_SHAREDENIED; 2584 if (ret == 0) 2585 NFSUNLOCKSTATE(); 2586 if (haslock) { 2587 NFSLOCKV4ROOTMUTEX(); 2588 nfsv4_unlock(&nfsv4rootfs_lock, 1); 2589 NFSUNLOCKV4ROOTMUTEX(); 2590 } 2591 goto out; 2592 } 2593 } 2594 2595 /* 2596 * Check for a conflicting delegation. If one is found, call 2597 * nfsrv_delegconflict() to handle it. If the v4root lock hasn't 2598 * been set yet, it will get the lock. Otherwise, it will recall 2599 * the delegation. Then, we try try again... 2600 * (If NFSLCK_DELEGCUR is set, it has a delegation, so there 2601 * isn't a conflict.) 2602 * I currently believe the conflict algorithm to be: 2603 * For Open with Read Access and Deny None 2604 * - there is a conflict iff a different client has a write delegation 2605 * For Open with other Write Access or any Deny except None 2606 * - there is a conflict if a different client has any delegation 2607 * - there is a conflict if the same client has a read delegation 2608 * (The current consensus is that this last case should be 2609 * considered a conflict since the client with a read delegation 2610 * could have done an Open with ReadAccess and WriteDeny 2611 * locally and then not have checked for the WriteDeny.) 2612 * Don't check for a Reclaim, since that will be dealt with 2613 * by nfsrv_openctrl(). 2614 */ 2615 if (!(new_stp->ls_flags & 2616 (NFSLCK_DELEGPREV | NFSLCK_DELEGCUR | NFSLCK_RECLAIM))) { 2617 stp = LIST_FIRST(&lfp->lf_deleg); 2618 while (stp != LIST_END(&lfp->lf_deleg)) { 2619 nstp = LIST_NEXT(stp, ls_file); 2620 if ((readonly && stp->ls_clp != clp && 2621 (stp->ls_flags & NFSLCK_DELEGWRITE)) || 2622 (!readonly && (stp->ls_clp != clp || 2623 (stp->ls_flags & NFSLCK_DELEGREAD)))) { 2624 ret = nfsrv_delegconflict(stp, &haslock, p, vp); 2625 if (ret) { 2626 /* 2627 * nfsrv_delegconflict() unlocks state 2628 * when it returns non-zero. 2629 */ 2630 if (ret == -1) 2631 goto tryagain; 2632 error = ret; 2633 goto out; 2634 } 2635 } 2636 stp = nstp; 2637 } 2638 } 2639 NFSUNLOCKSTATE(); 2640 if (haslock) { 2641 NFSLOCKV4ROOTMUTEX(); 2642 nfsv4_unlock(&nfsv4rootfs_lock, 1); 2643 NFSUNLOCKV4ROOTMUTEX(); 2644 } 2645 2646 out: 2647 NFSEXITCODE2(error, nd); 2648 return (error); 2649 } 2650 2651 /* 2652 * Open control function to create/update open state for an open. 2653 */ 2654 int 2655 nfsrv_openctrl(struct nfsrv_descript *nd, vnode_t vp, 2656 struct nfsstate **new_stpp, nfsquad_t clientid, nfsv4stateid_t *stateidp, 2657 nfsv4stateid_t *delegstateidp, u_int32_t *rflagsp, struct nfsexstuff *exp, 2658 NFSPROC_T *p, u_quad_t filerev) 2659 { 2660 struct nfsstate *new_stp = *new_stpp; 2661 struct nfsstate *stp, *nstp; 2662 struct nfsstate *openstp = NULL, *new_open, *ownerstp, *new_deleg; 2663 struct nfslockfile *lfp, *new_lfp; 2664 struct nfsclient *clp; 2665 int error = 0, haslock = 0, ret, delegate = 1, writedeleg = 1; 2666 int readonly = 0, cbret = 1, getfhret = 0; 2667 int gotstate = 0, len = 0; 2668 u_char *clidp = NULL; 2669 2670 if ((new_stp->ls_flags & NFSLCK_SHAREBITS) == NFSLCK_READACCESS) 2671 readonly = 1; 2672 /* 2673 * Check for restart conditions (client and server). 2674 * (Paranoia, should have been detected by nfsrv_opencheck().) 2675 * If an error does show up, return NFSERR_EXPIRED, since the 2676 * the seqid# has already been incremented. 2677 */ 2678 error = nfsrv_checkrestart(clientid, new_stp->ls_flags, 2679 &new_stp->ls_stateid, 0); 2680 if (error) { 2681 printf("Nfsd: openctrl unexpected restart err=%d\n", 2682 error); 2683 error = NFSERR_EXPIRED; 2684 goto out; 2685 } 2686 2687 clidp = malloc(NFSV4_OPAQUELIMIT, M_TEMP, M_WAITOK); 2688 tryagain: 2689 new_lfp = malloc(sizeof (struct nfslockfile), 2690 M_NFSDLOCKFILE, M_WAITOK); 2691 new_open = malloc(sizeof (struct nfsstate), 2692 M_NFSDSTATE, M_WAITOK); 2693 new_deleg = malloc(sizeof (struct nfsstate), 2694 M_NFSDSTATE, M_WAITOK); 2695 getfhret = nfsrv_getlockfh(vp, new_stp->ls_flags, new_lfp, 2696 NULL, p); 2697 NFSLOCKSTATE(); 2698 /* 2699 * Get the client structure. Since the linked lists could be changed 2700 * by other nfsd processes if this process does a tsleep(), one of 2701 * two things must be done. 2702 * 1 - don't tsleep() 2703 * or 2704 * 2 - get the nfsv4_lock() { indicated by haslock == 1 } 2705 * before using the lists, since this lock stops the other 2706 * nfsd. This should only be used for rare cases, since it 2707 * essentially single threads the nfsd. 2708 * At this time, it is only done for cases where the stable 2709 * storage file must be written prior to completion of state 2710 * expiration. 2711 */ 2712 error = nfsrv_getclient(clientid, CLOPS_RENEW, &clp, NULL, 2713 (nfsquad_t)((u_quad_t)0), 0, nd, p); 2714 if (!error && (clp->lc_flags & LCL_NEEDSCBNULL) && 2715 clp->lc_program) { 2716 /* 2717 * This happens on the first open for a client 2718 * that supports callbacks. 2719 */ 2720 NFSUNLOCKSTATE(); 2721 /* 2722 * Although nfsrv_docallback() will sleep, clp won't 2723 * go away, since they are only removed when the 2724 * nfsv4_lock() has blocked the nfsd threads. The 2725 * fields in clp can change, but having multiple 2726 * threads do this Null callback RPC should be 2727 * harmless. 2728 */ 2729 cbret = nfsrv_docallback(clp, NFSV4PROC_CBNULL, 2730 NULL, 0, NULL, NULL, NULL, 0, p); 2731 NFSLOCKSTATE(); 2732 clp->lc_flags &= ~LCL_NEEDSCBNULL; 2733 if (!cbret) 2734 clp->lc_flags |= LCL_CALLBACKSON; 2735 } 2736 2737 /* 2738 * Look up the open owner. See if it needs confirmation and 2739 * check the seq#, as required. 2740 */ 2741 if (!error) 2742 nfsrv_getowner(&clp->lc_open, new_stp, &ownerstp); 2743 2744 if (error) { 2745 NFSUNLOCKSTATE(); 2746 printf("Nfsd: openctrl unexpected state err=%d\n", 2747 error); 2748 free(new_lfp, M_NFSDLOCKFILE); 2749 free(new_open, M_NFSDSTATE); 2750 free(new_deleg, M_NFSDSTATE); 2751 if (haslock) { 2752 NFSLOCKV4ROOTMUTEX(); 2753 nfsv4_unlock(&nfsv4rootfs_lock, 1); 2754 NFSUNLOCKV4ROOTMUTEX(); 2755 } 2756 error = NFSERR_EXPIRED; 2757 goto out; 2758 } 2759 2760 if (new_stp->ls_flags & NFSLCK_RECLAIM) 2761 nfsrv_markstable(clp); 2762 2763 /* 2764 * Get the structure for the underlying file. 2765 */ 2766 if (getfhret) 2767 error = getfhret; 2768 else 2769 error = nfsrv_getlockfile(new_stp->ls_flags, &new_lfp, &lfp, 2770 NULL, 0); 2771 if (new_lfp) 2772 free(new_lfp, M_NFSDLOCKFILE); 2773 if (error) { 2774 NFSUNLOCKSTATE(); 2775 printf("Nfsd openctrl unexpected getlockfile err=%d\n", 2776 error); 2777 free(new_open, M_NFSDSTATE); 2778 free(new_deleg, M_NFSDSTATE); 2779 if (haslock) { 2780 NFSLOCKV4ROOTMUTEX(); 2781 nfsv4_unlock(&nfsv4rootfs_lock, 1); 2782 NFSUNLOCKV4ROOTMUTEX(); 2783 } 2784 goto out; 2785 } 2786 2787 /* 2788 * Search for a conflicting open/share. 2789 */ 2790 if (new_stp->ls_flags & NFSLCK_DELEGCUR) { 2791 /* 2792 * For Delegate_Cur, search for the matching Delegation, 2793 * which indicates no conflict. 2794 * An old delegation should have been recovered by the 2795 * client doing a Claim_DELEGATE_Prev, so I won't let 2796 * it match and return NFSERR_EXPIRED. Should I let it 2797 * match? 2798 */ 2799 LIST_FOREACH(stp, &lfp->lf_deleg, ls_file) { 2800 if (!(stp->ls_flags & NFSLCK_OLDDELEG) && 2801 (((nd->nd_flag & ND_NFSV41) != 0 && 2802 stateidp->seqid == 0) || 2803 stateidp->seqid == stp->ls_stateid.seqid) && 2804 !NFSBCMP(stateidp->other, stp->ls_stateid.other, 2805 NFSX_STATEIDOTHER)) 2806 break; 2807 } 2808 if (stp == LIST_END(&lfp->lf_deleg) || 2809 ((new_stp->ls_flags & NFSLCK_WRITEACCESS) && 2810 (stp->ls_flags & NFSLCK_DELEGREAD))) { 2811 NFSUNLOCKSTATE(); 2812 printf("Nfsd openctrl unexpected expiry\n"); 2813 free(new_open, M_NFSDSTATE); 2814 free(new_deleg, M_NFSDSTATE); 2815 if (haslock) { 2816 NFSLOCKV4ROOTMUTEX(); 2817 nfsv4_unlock(&nfsv4rootfs_lock, 1); 2818 NFSUNLOCKV4ROOTMUTEX(); 2819 } 2820 error = NFSERR_EXPIRED; 2821 goto out; 2822 } 2823 2824 /* 2825 * Don't issue a Delegation, since one already exists and 2826 * delay delegation timeout, as required. 2827 */ 2828 delegate = 0; 2829 nfsrv_delaydelegtimeout(stp); 2830 } 2831 2832 /* 2833 * Check for access/deny bit conflicts. I also check for the 2834 * same owner, since the client might not have bothered to check. 2835 * Also, note an open for the same file and owner, if found, 2836 * which is all we do here for Delegate_Cur, since conflict 2837 * checking is already done. 2838 */ 2839 LIST_FOREACH(stp, &lfp->lf_open, ls_file) { 2840 if (ownerstp && stp->ls_openowner == ownerstp) 2841 openstp = stp; 2842 if (!(new_stp->ls_flags & NFSLCK_DELEGCUR)) { 2843 /* 2844 * If another client has the file open, the only 2845 * delegation that can be issued is a Read delegation 2846 * and only if it is a Read open with Deny none. 2847 */ 2848 if (clp != stp->ls_clp) { 2849 if ((stp->ls_flags & NFSLCK_SHAREBITS) == 2850 NFSLCK_READACCESS) 2851 writedeleg = 0; 2852 else 2853 delegate = 0; 2854 } 2855 if(((new_stp->ls_flags & NFSLCK_ACCESSBITS) & 2856 ((stp->ls_flags>>NFSLCK_SHIFT) & NFSLCK_ACCESSBITS))|| 2857 ((stp->ls_flags & NFSLCK_ACCESSBITS) & 2858 ((new_stp->ls_flags>>NFSLCK_SHIFT)&NFSLCK_ACCESSBITS))){ 2859 ret = nfsrv_clientconflict(stp->ls_clp,&haslock,vp,p); 2860 if (ret == 1) { 2861 /* 2862 * nfsrv_clientconflict() unlocks state 2863 * when it returns non-zero. 2864 */ 2865 free(new_open, M_NFSDSTATE); 2866 free(new_deleg, M_NFSDSTATE); 2867 openstp = NULL; 2868 goto tryagain; 2869 } 2870 if (ret == 2) 2871 error = NFSERR_PERM; 2872 else if (new_stp->ls_flags & NFSLCK_RECLAIM) 2873 error = NFSERR_RECLAIMCONFLICT; 2874 else 2875 error = NFSERR_SHAREDENIED; 2876 if (ret == 0) 2877 NFSUNLOCKSTATE(); 2878 if (haslock) { 2879 NFSLOCKV4ROOTMUTEX(); 2880 nfsv4_unlock(&nfsv4rootfs_lock, 1); 2881 NFSUNLOCKV4ROOTMUTEX(); 2882 } 2883 free(new_open, M_NFSDSTATE); 2884 free(new_deleg, M_NFSDSTATE); 2885 printf("nfsd openctrl unexpected client cnfl\n"); 2886 goto out; 2887 } 2888 } 2889 } 2890 2891 /* 2892 * Check for a conflicting delegation. If one is found, call 2893 * nfsrv_delegconflict() to handle it. If the v4root lock hasn't 2894 * been set yet, it will get the lock. Otherwise, it will recall 2895 * the delegation. Then, we try try again... 2896 * (If NFSLCK_DELEGCUR is set, it has a delegation, so there 2897 * isn't a conflict.) 2898 * I currently believe the conflict algorithm to be: 2899 * For Open with Read Access and Deny None 2900 * - there is a conflict iff a different client has a write delegation 2901 * For Open with other Write Access or any Deny except None 2902 * - there is a conflict if a different client has any delegation 2903 * - there is a conflict if the same client has a read delegation 2904 * (The current consensus is that this last case should be 2905 * considered a conflict since the client with a read delegation 2906 * could have done an Open with ReadAccess and WriteDeny 2907 * locally and then not have checked for the WriteDeny.) 2908 */ 2909 if (!(new_stp->ls_flags & (NFSLCK_DELEGPREV | NFSLCK_DELEGCUR))) { 2910 stp = LIST_FIRST(&lfp->lf_deleg); 2911 while (stp != LIST_END(&lfp->lf_deleg)) { 2912 nstp = LIST_NEXT(stp, ls_file); 2913 if (stp->ls_clp != clp && (stp->ls_flags & NFSLCK_DELEGREAD)) 2914 writedeleg = 0; 2915 else 2916 delegate = 0; 2917 if ((readonly && stp->ls_clp != clp && 2918 (stp->ls_flags & NFSLCK_DELEGWRITE)) || 2919 (!readonly && (stp->ls_clp != clp || 2920 (stp->ls_flags & NFSLCK_DELEGREAD)))) { 2921 if (new_stp->ls_flags & NFSLCK_RECLAIM) { 2922 delegate = 2; 2923 } else { 2924 ret = nfsrv_delegconflict(stp, &haslock, p, vp); 2925 if (ret) { 2926 /* 2927 * nfsrv_delegconflict() unlocks state 2928 * when it returns non-zero. 2929 */ 2930 printf("Nfsd openctrl unexpected deleg cnfl\n"); 2931 free(new_open, M_NFSDSTATE); 2932 free(new_deleg, M_NFSDSTATE); 2933 if (ret == -1) { 2934 openstp = NULL; 2935 goto tryagain; 2936 } 2937 error = ret; 2938 goto out; 2939 } 2940 } 2941 } 2942 stp = nstp; 2943 } 2944 } 2945 2946 /* 2947 * We only get here if there was no open that conflicted. 2948 * If an open for the owner exists, or in the access/deny bits. 2949 * Otherwise it is a new open. If the open_owner hasn't been 2950 * confirmed, replace the open with the new one needing confirmation, 2951 * otherwise add the open. 2952 */ 2953 if (new_stp->ls_flags & NFSLCK_DELEGPREV) { 2954 /* 2955 * Handle NFSLCK_DELEGPREV by searching the old delegations for 2956 * a match. If found, just move the old delegation to the current 2957 * delegation list and issue open. If not found, return 2958 * NFSERR_EXPIRED. 2959 */ 2960 LIST_FOREACH(stp, &clp->lc_olddeleg, ls_list) { 2961 if (stp->ls_lfp == lfp) { 2962 /* Found it */ 2963 if (stp->ls_clp != clp) 2964 panic("olddeleg clp"); 2965 LIST_REMOVE(stp, ls_list); 2966 LIST_REMOVE(stp, ls_hash); 2967 stp->ls_flags &= ~NFSLCK_OLDDELEG; 2968 stp->ls_stateid.seqid = delegstateidp->seqid = 1; 2969 stp->ls_stateid.other[0] = delegstateidp->other[0] = 2970 clp->lc_clientid.lval[0]; 2971 stp->ls_stateid.other[1] = delegstateidp->other[1] = 2972 clp->lc_clientid.lval[1]; 2973 stp->ls_stateid.other[2] = delegstateidp->other[2] = 2974 nfsrv_nextstateindex(clp); 2975 stp->ls_compref = nd->nd_compref; 2976 LIST_INSERT_HEAD(&clp->lc_deleg, stp, ls_list); 2977 LIST_INSERT_HEAD(NFSSTATEHASH(clp, 2978 stp->ls_stateid), stp, ls_hash); 2979 if (stp->ls_flags & NFSLCK_DELEGWRITE) 2980 *rflagsp |= NFSV4OPEN_WRITEDELEGATE; 2981 else 2982 *rflagsp |= NFSV4OPEN_READDELEGATE; 2983 clp->lc_delegtime = NFSD_MONOSEC + 2984 nfsrv_lease + NFSRV_LEASEDELTA; 2985 2986 /* 2987 * Now, do the associated open. 2988 */ 2989 new_open->ls_stateid.seqid = 1; 2990 new_open->ls_stateid.other[0] = clp->lc_clientid.lval[0]; 2991 new_open->ls_stateid.other[1] = clp->lc_clientid.lval[1]; 2992 new_open->ls_stateid.other[2] = nfsrv_nextstateindex(clp); 2993 new_open->ls_flags = (new_stp->ls_flags&NFSLCK_DENYBITS)| 2994 NFSLCK_OPEN; 2995 if (stp->ls_flags & NFSLCK_DELEGWRITE) 2996 new_open->ls_flags |= (NFSLCK_READACCESS | 2997 NFSLCK_WRITEACCESS); 2998 else 2999 new_open->ls_flags |= NFSLCK_READACCESS; 3000 new_open->ls_uid = new_stp->ls_uid; 3001 new_open->ls_lfp = lfp; 3002 new_open->ls_clp = clp; 3003 LIST_INIT(&new_open->ls_open); 3004 LIST_INSERT_HEAD(&lfp->lf_open, new_open, ls_file); 3005 LIST_INSERT_HEAD(NFSSTATEHASH(clp, new_open->ls_stateid), 3006 new_open, ls_hash); 3007 /* 3008 * and handle the open owner 3009 */ 3010 if (ownerstp) { 3011 new_open->ls_openowner = ownerstp; 3012 LIST_INSERT_HEAD(&ownerstp->ls_open,new_open,ls_list); 3013 } else { 3014 new_open->ls_openowner = new_stp; 3015 new_stp->ls_flags = 0; 3016 nfsrvd_refcache(new_stp->ls_op); 3017 new_stp->ls_noopens = 0; 3018 LIST_INIT(&new_stp->ls_open); 3019 LIST_INSERT_HEAD(&new_stp->ls_open, new_open, ls_list); 3020 LIST_INSERT_HEAD(&clp->lc_open, new_stp, ls_list); 3021 *new_stpp = NULL; 3022 nfsstatsv1.srvopenowners++; 3023 nfsrv_openpluslock++; 3024 } 3025 openstp = new_open; 3026 new_open = NULL; 3027 nfsstatsv1.srvopens++; 3028 nfsrv_openpluslock++; 3029 break; 3030 } 3031 } 3032 if (stp == LIST_END(&clp->lc_olddeleg)) 3033 error = NFSERR_EXPIRED; 3034 } else if (new_stp->ls_flags & (NFSLCK_DELEGREAD | NFSLCK_DELEGWRITE)) { 3035 /* 3036 * Scan to see that no delegation for this client and file 3037 * doesn't already exist. 3038 * There also shouldn't yet be an Open for this file and 3039 * openowner. 3040 */ 3041 LIST_FOREACH(stp, &lfp->lf_deleg, ls_file) { 3042 if (stp->ls_clp == clp) 3043 break; 3044 } 3045 if (stp == LIST_END(&lfp->lf_deleg) && openstp == NULL) { 3046 /* 3047 * This is the Claim_Previous case with a delegation 3048 * type != Delegate_None. 3049 */ 3050 /* 3051 * First, add the delegation. (Although we must issue the 3052 * delegation, we can also ask for an immediate return.) 3053 */ 3054 new_deleg->ls_stateid.seqid = delegstateidp->seqid = 1; 3055 new_deleg->ls_stateid.other[0] = delegstateidp->other[0] = 3056 clp->lc_clientid.lval[0]; 3057 new_deleg->ls_stateid.other[1] = delegstateidp->other[1] = 3058 clp->lc_clientid.lval[1]; 3059 new_deleg->ls_stateid.other[2] = delegstateidp->other[2] = 3060 nfsrv_nextstateindex(clp); 3061 if (new_stp->ls_flags & NFSLCK_DELEGWRITE) { 3062 new_deleg->ls_flags = (NFSLCK_DELEGWRITE | 3063 NFSLCK_READACCESS | NFSLCK_WRITEACCESS); 3064 *rflagsp |= NFSV4OPEN_WRITEDELEGATE; 3065 nfsrv_writedelegcnt++; 3066 } else { 3067 new_deleg->ls_flags = (NFSLCK_DELEGREAD | 3068 NFSLCK_READACCESS); 3069 *rflagsp |= NFSV4OPEN_READDELEGATE; 3070 } 3071 new_deleg->ls_uid = new_stp->ls_uid; 3072 new_deleg->ls_lfp = lfp; 3073 new_deleg->ls_clp = clp; 3074 new_deleg->ls_filerev = filerev; 3075 new_deleg->ls_compref = nd->nd_compref; 3076 new_deleg->ls_lastrecall = 0; 3077 LIST_INSERT_HEAD(&lfp->lf_deleg, new_deleg, ls_file); 3078 LIST_INSERT_HEAD(NFSSTATEHASH(clp, 3079 new_deleg->ls_stateid), new_deleg, ls_hash); 3080 LIST_INSERT_HEAD(&clp->lc_deleg, new_deleg, ls_list); 3081 new_deleg = NULL; 3082 if (delegate == 2 || nfsrv_issuedelegs == 0 || 3083 (clp->lc_flags & (LCL_CALLBACKSON | LCL_CBDOWN)) != 3084 LCL_CALLBACKSON || 3085 NFSRV_V4DELEGLIMIT(nfsrv_delegatecnt) || 3086 !NFSVNO_DELEGOK(vp)) 3087 *rflagsp |= NFSV4OPEN_RECALL; 3088 nfsstatsv1.srvdelegates++; 3089 nfsrv_openpluslock++; 3090 nfsrv_delegatecnt++; 3091 3092 /* 3093 * Now, do the associated open. 3094 */ 3095 new_open->ls_stateid.seqid = 1; 3096 new_open->ls_stateid.other[0] = clp->lc_clientid.lval[0]; 3097 new_open->ls_stateid.other[1] = clp->lc_clientid.lval[1]; 3098 new_open->ls_stateid.other[2] = nfsrv_nextstateindex(clp); 3099 new_open->ls_flags = (new_stp->ls_flags & NFSLCK_DENYBITS) | 3100 NFSLCK_OPEN; 3101 if (new_stp->ls_flags & NFSLCK_DELEGWRITE) 3102 new_open->ls_flags |= (NFSLCK_READACCESS | 3103 NFSLCK_WRITEACCESS); 3104 else 3105 new_open->ls_flags |= NFSLCK_READACCESS; 3106 new_open->ls_uid = new_stp->ls_uid; 3107 new_open->ls_lfp = lfp; 3108 new_open->ls_clp = clp; 3109 LIST_INIT(&new_open->ls_open); 3110 LIST_INSERT_HEAD(&lfp->lf_open, new_open, ls_file); 3111 LIST_INSERT_HEAD(NFSSTATEHASH(clp, new_open->ls_stateid), 3112 new_open, ls_hash); 3113 /* 3114 * and handle the open owner 3115 */ 3116 if (ownerstp) { 3117 new_open->ls_openowner = ownerstp; 3118 LIST_INSERT_HEAD(&ownerstp->ls_open, new_open, ls_list); 3119 } else { 3120 new_open->ls_openowner = new_stp; 3121 new_stp->ls_flags = 0; 3122 nfsrvd_refcache(new_stp->ls_op); 3123 new_stp->ls_noopens = 0; 3124 LIST_INIT(&new_stp->ls_open); 3125 LIST_INSERT_HEAD(&new_stp->ls_open, new_open, ls_list); 3126 LIST_INSERT_HEAD(&clp->lc_open, new_stp, ls_list); 3127 *new_stpp = NULL; 3128 nfsstatsv1.srvopenowners++; 3129 nfsrv_openpluslock++; 3130 } 3131 openstp = new_open; 3132 new_open = NULL; 3133 nfsstatsv1.srvopens++; 3134 nfsrv_openpluslock++; 3135 } else { 3136 error = NFSERR_RECLAIMCONFLICT; 3137 } 3138 } else if (ownerstp) { 3139 if (ownerstp->ls_flags & NFSLCK_NEEDSCONFIRM) { 3140 /* Replace the open */ 3141 if (ownerstp->ls_op) 3142 nfsrvd_derefcache(ownerstp->ls_op); 3143 ownerstp->ls_op = new_stp->ls_op; 3144 nfsrvd_refcache(ownerstp->ls_op); 3145 ownerstp->ls_seq = new_stp->ls_seq; 3146 *rflagsp |= NFSV4OPEN_RESULTCONFIRM; 3147 stp = LIST_FIRST(&ownerstp->ls_open); 3148 stp->ls_flags = (new_stp->ls_flags & NFSLCK_SHAREBITS) | 3149 NFSLCK_OPEN; 3150 stp->ls_stateid.seqid = 1; 3151 stp->ls_uid = new_stp->ls_uid; 3152 if (lfp != stp->ls_lfp) { 3153 LIST_REMOVE(stp, ls_file); 3154 LIST_INSERT_HEAD(&lfp->lf_open, stp, ls_file); 3155 stp->ls_lfp = lfp; 3156 } 3157 openstp = stp; 3158 } else if (openstp) { 3159 openstp->ls_flags |= (new_stp->ls_flags & NFSLCK_SHAREBITS); 3160 openstp->ls_stateid.seqid++; 3161 if ((nd->nd_flag & ND_NFSV41) != 0 && 3162 openstp->ls_stateid.seqid == 0) 3163 openstp->ls_stateid.seqid = 1; 3164 3165 /* 3166 * This is where we can choose to issue a delegation. 3167 */ 3168 if ((new_stp->ls_flags & NFSLCK_WANTNODELEG) != 0) 3169 *rflagsp |= NFSV4OPEN_WDNOTWANTED; 3170 else if (nfsrv_issuedelegs == 0) 3171 *rflagsp |= NFSV4OPEN_WDSUPPFTYPE; 3172 else if (NFSRV_V4DELEGLIMIT(nfsrv_delegatecnt)) 3173 *rflagsp |= NFSV4OPEN_WDRESOURCE; 3174 else if (delegate == 0 || writedeleg == 0 || 3175 NFSVNO_EXRDONLY(exp) || (readonly != 0 && 3176 nfsrv_writedelegifpos == 0) || 3177 !NFSVNO_DELEGOK(vp) || 3178 (new_stp->ls_flags & NFSLCK_WANTRDELEG) != 0 || 3179 (clp->lc_flags & (LCL_CALLBACKSON | LCL_CBDOWN)) != 3180 LCL_CALLBACKSON) 3181 *rflagsp |= NFSV4OPEN_WDCONTENTION; 3182 else { 3183 new_deleg->ls_stateid.seqid = delegstateidp->seqid = 1; 3184 new_deleg->ls_stateid.other[0] = delegstateidp->other[0] 3185 = clp->lc_clientid.lval[0]; 3186 new_deleg->ls_stateid.other[1] = delegstateidp->other[1] 3187 = clp->lc_clientid.lval[1]; 3188 new_deleg->ls_stateid.other[2] = delegstateidp->other[2] 3189 = nfsrv_nextstateindex(clp); 3190 new_deleg->ls_flags = (NFSLCK_DELEGWRITE | 3191 NFSLCK_READACCESS | NFSLCK_WRITEACCESS); 3192 *rflagsp |= NFSV4OPEN_WRITEDELEGATE; 3193 new_deleg->ls_uid = new_stp->ls_uid; 3194 new_deleg->ls_lfp = lfp; 3195 new_deleg->ls_clp = clp; 3196 new_deleg->ls_filerev = filerev; 3197 new_deleg->ls_compref = nd->nd_compref; 3198 new_deleg->ls_lastrecall = 0; 3199 nfsrv_writedelegcnt++; 3200 LIST_INSERT_HEAD(&lfp->lf_deleg, new_deleg, ls_file); 3201 LIST_INSERT_HEAD(NFSSTATEHASH(clp, 3202 new_deleg->ls_stateid), new_deleg, ls_hash); 3203 LIST_INSERT_HEAD(&clp->lc_deleg, new_deleg, ls_list); 3204 new_deleg = NULL; 3205 nfsstatsv1.srvdelegates++; 3206 nfsrv_openpluslock++; 3207 nfsrv_delegatecnt++; 3208 } 3209 } else { 3210 new_open->ls_stateid.seqid = 1; 3211 new_open->ls_stateid.other[0] = clp->lc_clientid.lval[0]; 3212 new_open->ls_stateid.other[1] = clp->lc_clientid.lval[1]; 3213 new_open->ls_stateid.other[2] = nfsrv_nextstateindex(clp); 3214 new_open->ls_flags = (new_stp->ls_flags & NFSLCK_SHAREBITS)| 3215 NFSLCK_OPEN; 3216 new_open->ls_uid = new_stp->ls_uid; 3217 new_open->ls_openowner = ownerstp; 3218 new_open->ls_lfp = lfp; 3219 new_open->ls_clp = clp; 3220 LIST_INIT(&new_open->ls_open); 3221 LIST_INSERT_HEAD(&lfp->lf_open, new_open, ls_file); 3222 LIST_INSERT_HEAD(&ownerstp->ls_open, new_open, ls_list); 3223 LIST_INSERT_HEAD(NFSSTATEHASH(clp, new_open->ls_stateid), 3224 new_open, ls_hash); 3225 openstp = new_open; 3226 new_open = NULL; 3227 nfsstatsv1.srvopens++; 3228 nfsrv_openpluslock++; 3229 3230 /* 3231 * This is where we can choose to issue a delegation. 3232 */ 3233 if ((new_stp->ls_flags & NFSLCK_WANTNODELEG) != 0) 3234 *rflagsp |= NFSV4OPEN_WDNOTWANTED; 3235 else if (nfsrv_issuedelegs == 0) 3236 *rflagsp |= NFSV4OPEN_WDSUPPFTYPE; 3237 else if (NFSRV_V4DELEGLIMIT(nfsrv_delegatecnt)) 3238 *rflagsp |= NFSV4OPEN_WDRESOURCE; 3239 else if (delegate == 0 || (writedeleg == 0 && 3240 readonly == 0) || !NFSVNO_DELEGOK(vp) || 3241 (clp->lc_flags & (LCL_CALLBACKSON | LCL_CBDOWN)) != 3242 LCL_CALLBACKSON) 3243 *rflagsp |= NFSV4OPEN_WDCONTENTION; 3244 else { 3245 new_deleg->ls_stateid.seqid = delegstateidp->seqid = 1; 3246 new_deleg->ls_stateid.other[0] = delegstateidp->other[0] 3247 = clp->lc_clientid.lval[0]; 3248 new_deleg->ls_stateid.other[1] = delegstateidp->other[1] 3249 = clp->lc_clientid.lval[1]; 3250 new_deleg->ls_stateid.other[2] = delegstateidp->other[2] 3251 = nfsrv_nextstateindex(clp); 3252 if (writedeleg && !NFSVNO_EXRDONLY(exp) && 3253 (nfsrv_writedelegifpos || !readonly) && 3254 (new_stp->ls_flags & NFSLCK_WANTRDELEG) == 0) { 3255 new_deleg->ls_flags = (NFSLCK_DELEGWRITE | 3256 NFSLCK_READACCESS | NFSLCK_WRITEACCESS); 3257 *rflagsp |= NFSV4OPEN_WRITEDELEGATE; 3258 nfsrv_writedelegcnt++; 3259 } else { 3260 new_deleg->ls_flags = (NFSLCK_DELEGREAD | 3261 NFSLCK_READACCESS); 3262 *rflagsp |= NFSV4OPEN_READDELEGATE; 3263 } 3264 new_deleg->ls_uid = new_stp->ls_uid; 3265 new_deleg->ls_lfp = lfp; 3266 new_deleg->ls_clp = clp; 3267 new_deleg->ls_filerev = filerev; 3268 new_deleg->ls_compref = nd->nd_compref; 3269 new_deleg->ls_lastrecall = 0; 3270 LIST_INSERT_HEAD(&lfp->lf_deleg, new_deleg, ls_file); 3271 LIST_INSERT_HEAD(NFSSTATEHASH(clp, 3272 new_deleg->ls_stateid), new_deleg, ls_hash); 3273 LIST_INSERT_HEAD(&clp->lc_deleg, new_deleg, ls_list); 3274 new_deleg = NULL; 3275 nfsstatsv1.srvdelegates++; 3276 nfsrv_openpluslock++; 3277 nfsrv_delegatecnt++; 3278 } 3279 } 3280 } else { 3281 /* 3282 * New owner case. Start the open_owner sequence with a 3283 * Needs confirmation (unless a reclaim) and hang the 3284 * new open off it. 3285 */ 3286 new_open->ls_stateid.seqid = 1; 3287 new_open->ls_stateid.other[0] = clp->lc_clientid.lval[0]; 3288 new_open->ls_stateid.other[1] = clp->lc_clientid.lval[1]; 3289 new_open->ls_stateid.other[2] = nfsrv_nextstateindex(clp); 3290 new_open->ls_flags = (new_stp->ls_flags & NFSLCK_SHAREBITS) | 3291 NFSLCK_OPEN; 3292 new_open->ls_uid = new_stp->ls_uid; 3293 LIST_INIT(&new_open->ls_open); 3294 new_open->ls_openowner = new_stp; 3295 new_open->ls_lfp = lfp; 3296 new_open->ls_clp = clp; 3297 LIST_INSERT_HEAD(&lfp->lf_open, new_open, ls_file); 3298 if (new_stp->ls_flags & NFSLCK_RECLAIM) { 3299 new_stp->ls_flags = 0; 3300 } else if ((nd->nd_flag & ND_NFSV41) != 0) { 3301 /* NFSv4.1 never needs confirmation. */ 3302 new_stp->ls_flags = 0; 3303 3304 /* 3305 * This is where we can choose to issue a delegation. 3306 */ 3307 if (delegate && nfsrv_issuedelegs && 3308 (writedeleg || readonly) && 3309 (clp->lc_flags & (LCL_CALLBACKSON | LCL_CBDOWN)) == 3310 LCL_CALLBACKSON && 3311 !NFSRV_V4DELEGLIMIT(nfsrv_delegatecnt) && 3312 NFSVNO_DELEGOK(vp) && 3313 ((nd->nd_flag & ND_NFSV41) == 0 || 3314 (new_stp->ls_flags & NFSLCK_WANTNODELEG) == 0)) { 3315 new_deleg->ls_stateid.seqid = 3316 delegstateidp->seqid = 1; 3317 new_deleg->ls_stateid.other[0] = 3318 delegstateidp->other[0] 3319 = clp->lc_clientid.lval[0]; 3320 new_deleg->ls_stateid.other[1] = 3321 delegstateidp->other[1] 3322 = clp->lc_clientid.lval[1]; 3323 new_deleg->ls_stateid.other[2] = 3324 delegstateidp->other[2] 3325 = nfsrv_nextstateindex(clp); 3326 if (writedeleg && !NFSVNO_EXRDONLY(exp) && 3327 (nfsrv_writedelegifpos || !readonly) && 3328 ((nd->nd_flag & ND_NFSV41) == 0 || 3329 (new_stp->ls_flags & NFSLCK_WANTRDELEG) == 3330 0)) { 3331 new_deleg->ls_flags = 3332 (NFSLCK_DELEGWRITE | 3333 NFSLCK_READACCESS | 3334 NFSLCK_WRITEACCESS); 3335 *rflagsp |= NFSV4OPEN_WRITEDELEGATE; 3336 nfsrv_writedelegcnt++; 3337 } else { 3338 new_deleg->ls_flags = 3339 (NFSLCK_DELEGREAD | 3340 NFSLCK_READACCESS); 3341 *rflagsp |= NFSV4OPEN_READDELEGATE; 3342 } 3343 new_deleg->ls_uid = new_stp->ls_uid; 3344 new_deleg->ls_lfp = lfp; 3345 new_deleg->ls_clp = clp; 3346 new_deleg->ls_filerev = filerev; 3347 new_deleg->ls_compref = nd->nd_compref; 3348 new_deleg->ls_lastrecall = 0; 3349 LIST_INSERT_HEAD(&lfp->lf_deleg, new_deleg, 3350 ls_file); 3351 LIST_INSERT_HEAD(NFSSTATEHASH(clp, 3352 new_deleg->ls_stateid), new_deleg, ls_hash); 3353 LIST_INSERT_HEAD(&clp->lc_deleg, new_deleg, 3354 ls_list); 3355 new_deleg = NULL; 3356 nfsstatsv1.srvdelegates++; 3357 nfsrv_openpluslock++; 3358 nfsrv_delegatecnt++; 3359 } 3360 /* 3361 * Since NFSv4.1 never does an OpenConfirm, the first 3362 * open state will be acquired here. 3363 */ 3364 if (!(clp->lc_flags & LCL_STAMPEDSTABLE)) { 3365 clp->lc_flags |= LCL_STAMPEDSTABLE; 3366 len = clp->lc_idlen; 3367 NFSBCOPY(clp->lc_id, clidp, len); 3368 gotstate = 1; 3369 } 3370 } else { 3371 *rflagsp |= NFSV4OPEN_RESULTCONFIRM; 3372 new_stp->ls_flags = NFSLCK_NEEDSCONFIRM; 3373 } 3374 nfsrvd_refcache(new_stp->ls_op); 3375 new_stp->ls_noopens = 0; 3376 LIST_INIT(&new_stp->ls_open); 3377 LIST_INSERT_HEAD(&new_stp->ls_open, new_open, ls_list); 3378 LIST_INSERT_HEAD(&clp->lc_open, new_stp, ls_list); 3379 LIST_INSERT_HEAD(NFSSTATEHASH(clp, new_open->ls_stateid), 3380 new_open, ls_hash); 3381 openstp = new_open; 3382 new_open = NULL; 3383 *new_stpp = NULL; 3384 nfsstatsv1.srvopens++; 3385 nfsrv_openpluslock++; 3386 nfsstatsv1.srvopenowners++; 3387 nfsrv_openpluslock++; 3388 } 3389 if (!error) { 3390 stateidp->seqid = openstp->ls_stateid.seqid; 3391 stateidp->other[0] = openstp->ls_stateid.other[0]; 3392 stateidp->other[1] = openstp->ls_stateid.other[1]; 3393 stateidp->other[2] = openstp->ls_stateid.other[2]; 3394 } 3395 NFSUNLOCKSTATE(); 3396 if (haslock) { 3397 NFSLOCKV4ROOTMUTEX(); 3398 nfsv4_unlock(&nfsv4rootfs_lock, 1); 3399 NFSUNLOCKV4ROOTMUTEX(); 3400 } 3401 if (new_open) 3402 free(new_open, M_NFSDSTATE); 3403 if (new_deleg) 3404 free(new_deleg, M_NFSDSTATE); 3405 3406 /* 3407 * If the NFSv4.1 client just acquired its first open, write a timestamp 3408 * to the stable storage file. 3409 */ 3410 if (gotstate != 0) { 3411 nfsrv_writestable(clidp, len, NFSNST_NEWSTATE, p); 3412 nfsrv_backupstable(); 3413 } 3414 3415 out: 3416 free(clidp, M_TEMP); 3417 NFSEXITCODE2(error, nd); 3418 return (error); 3419 } 3420 3421 /* 3422 * Open update. Does the confirm, downgrade and close. 3423 */ 3424 int 3425 nfsrv_openupdate(vnode_t vp, struct nfsstate *new_stp, nfsquad_t clientid, 3426 nfsv4stateid_t *stateidp, struct nfsrv_descript *nd, NFSPROC_T *p, 3427 int *retwriteaccessp) 3428 { 3429 struct nfsstate *stp; 3430 struct nfsclient *clp; 3431 struct nfslockfile *lfp; 3432 u_int32_t bits; 3433 int error = 0, gotstate = 0, len = 0; 3434 u_char *clidp = NULL; 3435 3436 /* 3437 * Check for restart conditions (client and server). 3438 */ 3439 error = nfsrv_checkrestart(clientid, new_stp->ls_flags, 3440 &new_stp->ls_stateid, 0); 3441 if (error) 3442 goto out; 3443 3444 clidp = malloc(NFSV4_OPAQUELIMIT, M_TEMP, M_WAITOK); 3445 NFSLOCKSTATE(); 3446 /* 3447 * Get the open structure via clientid and stateid. 3448 */ 3449 error = nfsrv_getclient(clientid, CLOPS_RENEW, &clp, NULL, 3450 (nfsquad_t)((u_quad_t)0), 0, nd, p); 3451 if (!error) 3452 error = nfsrv_getstate(clp, &new_stp->ls_stateid, 3453 new_stp->ls_flags, &stp); 3454 3455 /* 3456 * Sanity check the open. 3457 */ 3458 if (!error && (!(stp->ls_flags & NFSLCK_OPEN) || 3459 (!(new_stp->ls_flags & NFSLCK_CONFIRM) && 3460 (stp->ls_openowner->ls_flags & NFSLCK_NEEDSCONFIRM)) || 3461 ((new_stp->ls_flags & NFSLCK_CONFIRM) && 3462 (!(stp->ls_openowner->ls_flags & NFSLCK_NEEDSCONFIRM))))) 3463 error = NFSERR_BADSTATEID; 3464 3465 if (!error) 3466 error = nfsrv_checkseqid(nd, new_stp->ls_seq, 3467 stp->ls_openowner, new_stp->ls_op); 3468 if (!error && stp->ls_stateid.seqid != new_stp->ls_stateid.seqid && 3469 (((nd->nd_flag & ND_NFSV41) == 0 && 3470 !(new_stp->ls_flags & NFSLCK_CONFIRM)) || 3471 ((nd->nd_flag & ND_NFSV41) != 0 && 3472 new_stp->ls_stateid.seqid != 0))) 3473 error = NFSERR_OLDSTATEID; 3474 if (!error && vp->v_type != VREG) { 3475 if (vp->v_type == VDIR) 3476 error = NFSERR_ISDIR; 3477 else 3478 error = NFSERR_INVAL; 3479 } 3480 3481 if (error) { 3482 /* 3483 * If a client tries to confirm an Open with a bad 3484 * seqid# and there are no byte range locks or other Opens 3485 * on the openowner, just throw it away, so the next use of the 3486 * openowner will start a fresh seq#. 3487 */ 3488 if (error == NFSERR_BADSEQID && 3489 (new_stp->ls_flags & NFSLCK_CONFIRM) && 3490 nfsrv_nootherstate(stp)) 3491 nfsrv_freeopenowner(stp->ls_openowner, 0, p); 3492 NFSUNLOCKSTATE(); 3493 goto out; 3494 } 3495 3496 /* 3497 * Set the return stateid. 3498 */ 3499 stateidp->seqid = stp->ls_stateid.seqid + 1; 3500 if ((nd->nd_flag & ND_NFSV41) != 0 && stateidp->seqid == 0) 3501 stateidp->seqid = 1; 3502 stateidp->other[0] = stp->ls_stateid.other[0]; 3503 stateidp->other[1] = stp->ls_stateid.other[1]; 3504 stateidp->other[2] = stp->ls_stateid.other[2]; 3505 /* 3506 * Now, handle the three cases. 3507 */ 3508 if (new_stp->ls_flags & NFSLCK_CONFIRM) { 3509 /* 3510 * If the open doesn't need confirmation, it seems to me that 3511 * there is a client error, but I'll just log it and keep going? 3512 */ 3513 if (!(stp->ls_openowner->ls_flags & NFSLCK_NEEDSCONFIRM)) 3514 printf("Nfsv4d: stray open confirm\n"); 3515 stp->ls_openowner->ls_flags = 0; 3516 stp->ls_stateid.seqid++; 3517 if ((nd->nd_flag & ND_NFSV41) != 0 && 3518 stp->ls_stateid.seqid == 0) 3519 stp->ls_stateid.seqid = 1; 3520 if (!(clp->lc_flags & LCL_STAMPEDSTABLE)) { 3521 clp->lc_flags |= LCL_STAMPEDSTABLE; 3522 len = clp->lc_idlen; 3523 NFSBCOPY(clp->lc_id, clidp, len); 3524 gotstate = 1; 3525 } 3526 NFSUNLOCKSTATE(); 3527 } else if (new_stp->ls_flags & NFSLCK_CLOSE) { 3528 lfp = stp->ls_lfp; 3529 if (retwriteaccessp != NULL) { 3530 if ((stp->ls_flags & NFSLCK_WRITEACCESS) != 0) 3531 *retwriteaccessp = 1; 3532 else 3533 *retwriteaccessp = 0; 3534 } 3535 if (nfsrv_dolocallocks != 0 && !LIST_EMPTY(&stp->ls_open)) { 3536 /* Get the lf lock */ 3537 nfsrv_locklf(lfp); 3538 NFSUNLOCKSTATE(); 3539 ASSERT_VOP_ELOCKED(vp, "nfsrv_openupdate"); 3540 NFSVOPUNLOCK(vp); 3541 if (nfsrv_freeopen(stp, vp, 1, p) == 0) { 3542 NFSLOCKSTATE(); 3543 nfsrv_unlocklf(lfp); 3544 NFSUNLOCKSTATE(); 3545 } 3546 NFSVOPLOCK(vp, LK_EXCLUSIVE | LK_RETRY); 3547 } else { 3548 (void) nfsrv_freeopen(stp, NULL, 0, p); 3549 NFSUNLOCKSTATE(); 3550 } 3551 } else { 3552 /* 3553 * Update the share bits, making sure that the new set are a 3554 * subset of the old ones. 3555 */ 3556 bits = (new_stp->ls_flags & NFSLCK_SHAREBITS); 3557 if (~(stp->ls_flags) & bits) { 3558 NFSUNLOCKSTATE(); 3559 error = NFSERR_INVAL; 3560 goto out; 3561 } 3562 stp->ls_flags = (bits | NFSLCK_OPEN); 3563 stp->ls_stateid.seqid++; 3564 if ((nd->nd_flag & ND_NFSV41) != 0 && 3565 stp->ls_stateid.seqid == 0) 3566 stp->ls_stateid.seqid = 1; 3567 NFSUNLOCKSTATE(); 3568 } 3569 3570 /* 3571 * If the client just confirmed its first open, write a timestamp 3572 * to the stable storage file. 3573 */ 3574 if (gotstate != 0) { 3575 nfsrv_writestable(clidp, len, NFSNST_NEWSTATE, p); 3576 nfsrv_backupstable(); 3577 } 3578 3579 out: 3580 free(clidp, M_TEMP); 3581 NFSEXITCODE2(error, nd); 3582 return (error); 3583 } 3584 3585 /* 3586 * Delegation update. Does the purge and return. 3587 */ 3588 int 3589 nfsrv_delegupdate(struct nfsrv_descript *nd, nfsquad_t clientid, 3590 nfsv4stateid_t *stateidp, vnode_t vp, int op, struct ucred *cred, 3591 NFSPROC_T *p, int *retwriteaccessp) 3592 { 3593 struct nfsstate *stp; 3594 struct nfsclient *clp; 3595 int error = 0; 3596 fhandle_t fh; 3597 3598 /* 3599 * Do a sanity check against the file handle for DelegReturn. 3600 */ 3601 if (vp) { 3602 error = nfsvno_getfh(vp, &fh, p); 3603 if (error) 3604 goto out; 3605 } 3606 /* 3607 * Check for restart conditions (client and server). 3608 */ 3609 if (op == NFSV4OP_DELEGRETURN) 3610 error = nfsrv_checkrestart(clientid, NFSLCK_DELEGRETURN, 3611 stateidp, 0); 3612 else 3613 error = nfsrv_checkrestart(clientid, NFSLCK_DELEGPURGE, 3614 stateidp, 0); 3615 3616 NFSLOCKSTATE(); 3617 /* 3618 * Get the open structure via clientid and stateid. 3619 */ 3620 if (!error) 3621 error = nfsrv_getclient(clientid, CLOPS_RENEW, &clp, NULL, 3622 (nfsquad_t)((u_quad_t)0), 0, nd, p); 3623 if (error) { 3624 if (error == NFSERR_CBPATHDOWN) 3625 error = 0; 3626 if (error == NFSERR_STALECLIENTID && op == NFSV4OP_DELEGRETURN) 3627 error = NFSERR_STALESTATEID; 3628 } 3629 if (!error && op == NFSV4OP_DELEGRETURN) { 3630 error = nfsrv_getstate(clp, stateidp, NFSLCK_DELEGRETURN, &stp); 3631 if (!error && stp->ls_stateid.seqid != stateidp->seqid && 3632 ((nd->nd_flag & ND_NFSV41) == 0 || stateidp->seqid != 0)) 3633 error = NFSERR_OLDSTATEID; 3634 } 3635 /* 3636 * NFSERR_EXPIRED means that the state has gone away, 3637 * so Delegations have been purged. Just return ok. 3638 */ 3639 if (error == NFSERR_EXPIRED && op == NFSV4OP_DELEGPURGE) { 3640 NFSUNLOCKSTATE(); 3641 error = 0; 3642 goto out; 3643 } 3644 if (error) { 3645 NFSUNLOCKSTATE(); 3646 goto out; 3647 } 3648 3649 if (op == NFSV4OP_DELEGRETURN) { 3650 if (NFSBCMP((caddr_t)&fh, (caddr_t)&stp->ls_lfp->lf_fh, 3651 sizeof (fhandle_t))) { 3652 NFSUNLOCKSTATE(); 3653 error = NFSERR_BADSTATEID; 3654 goto out; 3655 } 3656 if (retwriteaccessp != NULL) { 3657 if ((stp->ls_flags & NFSLCK_DELEGWRITE) != 0) 3658 *retwriteaccessp = 1; 3659 else 3660 *retwriteaccessp = 0; 3661 } 3662 nfsrv_freedeleg(stp); 3663 } else { 3664 nfsrv_freedeleglist(&clp->lc_olddeleg); 3665 } 3666 NFSUNLOCKSTATE(); 3667 error = 0; 3668 3669 out: 3670 NFSEXITCODE(error); 3671 return (error); 3672 } 3673 3674 /* 3675 * Release lock owner. 3676 */ 3677 int 3678 nfsrv_releaselckown(struct nfsstate *new_stp, nfsquad_t clientid, 3679 NFSPROC_T *p) 3680 { 3681 struct nfsstate *stp, *nstp, *openstp, *ownstp; 3682 struct nfsclient *clp; 3683 int error = 0; 3684 3685 /* 3686 * Check for restart conditions (client and server). 3687 */ 3688 error = nfsrv_checkrestart(clientid, new_stp->ls_flags, 3689 &new_stp->ls_stateid, 0); 3690 if (error) 3691 goto out; 3692 3693 NFSLOCKSTATE(); 3694 /* 3695 * Get the lock owner by name. 3696 */ 3697 error = nfsrv_getclient(clientid, CLOPS_RENEW, &clp, NULL, 3698 (nfsquad_t)((u_quad_t)0), 0, NULL, p); 3699 if (error) { 3700 NFSUNLOCKSTATE(); 3701 goto out; 3702 } 3703 LIST_FOREACH(ownstp, &clp->lc_open, ls_list) { 3704 LIST_FOREACH(openstp, &ownstp->ls_open, ls_list) { 3705 stp = LIST_FIRST(&openstp->ls_open); 3706 while (stp != LIST_END(&openstp->ls_open)) { 3707 nstp = LIST_NEXT(stp, ls_list); 3708 /* 3709 * If the owner matches, check for locks and 3710 * then free or return an error. 3711 */ 3712 if (stp->ls_ownerlen == new_stp->ls_ownerlen && 3713 !NFSBCMP(stp->ls_owner, new_stp->ls_owner, 3714 stp->ls_ownerlen)){ 3715 if (LIST_EMPTY(&stp->ls_lock)) { 3716 nfsrv_freelockowner(stp, NULL, 0, p); 3717 } else { 3718 NFSUNLOCKSTATE(); 3719 error = NFSERR_LOCKSHELD; 3720 goto out; 3721 } 3722 } 3723 stp = nstp; 3724 } 3725 } 3726 } 3727 NFSUNLOCKSTATE(); 3728 3729 out: 3730 NFSEXITCODE(error); 3731 return (error); 3732 } 3733 3734 /* 3735 * Get the file handle for a lock structure. 3736 */ 3737 static int 3738 nfsrv_getlockfh(vnode_t vp, u_short flags, struct nfslockfile *new_lfp, 3739 fhandle_t *nfhp, NFSPROC_T *p) 3740 { 3741 fhandle_t *fhp = NULL; 3742 int error; 3743 3744 /* 3745 * For lock, use the new nfslock structure, otherwise just 3746 * a fhandle_t on the stack. 3747 */ 3748 if (flags & NFSLCK_OPEN) { 3749 KASSERT(new_lfp != NULL, ("nfsrv_getlockfh: new_lfp NULL")); 3750 fhp = &new_lfp->lf_fh; 3751 } else if (nfhp) { 3752 fhp = nfhp; 3753 } else { 3754 panic("nfsrv_getlockfh"); 3755 } 3756 error = nfsvno_getfh(vp, fhp, p); 3757 NFSEXITCODE(error); 3758 return (error); 3759 } 3760 3761 /* 3762 * Get an nfs lock structure. Allocate one, as required, and return a 3763 * pointer to it. 3764 * Returns an NFSERR_xxx upon failure or -1 to indicate no current lock. 3765 */ 3766 static int 3767 nfsrv_getlockfile(u_short flags, struct nfslockfile **new_lfpp, 3768 struct nfslockfile **lfpp, fhandle_t *nfhp, int lockit) 3769 { 3770 struct nfslockfile *lfp; 3771 fhandle_t *fhp = NULL, *tfhp; 3772 struct nfslockhashhead *hp; 3773 struct nfslockfile *new_lfp = NULL; 3774 3775 /* 3776 * For lock, use the new nfslock structure, otherwise just 3777 * a fhandle_t on the stack. 3778 */ 3779 if (flags & NFSLCK_OPEN) { 3780 new_lfp = *new_lfpp; 3781 fhp = &new_lfp->lf_fh; 3782 } else if (nfhp) { 3783 fhp = nfhp; 3784 } else { 3785 panic("nfsrv_getlockfile"); 3786 } 3787 3788 hp = NFSLOCKHASH(fhp); 3789 LIST_FOREACH(lfp, hp, lf_hash) { 3790 tfhp = &lfp->lf_fh; 3791 if (NFSVNO_CMPFH(fhp, tfhp)) { 3792 if (lockit) 3793 nfsrv_locklf(lfp); 3794 *lfpp = lfp; 3795 return (0); 3796 } 3797 } 3798 if (!(flags & NFSLCK_OPEN)) 3799 return (-1); 3800 3801 /* 3802 * No match, so chain the new one into the list. 3803 */ 3804 LIST_INIT(&new_lfp->lf_open); 3805 LIST_INIT(&new_lfp->lf_lock); 3806 LIST_INIT(&new_lfp->lf_deleg); 3807 LIST_INIT(&new_lfp->lf_locallock); 3808 LIST_INIT(&new_lfp->lf_rollback); 3809 new_lfp->lf_locallock_lck.nfslock_usecnt = 0; 3810 new_lfp->lf_locallock_lck.nfslock_lock = 0; 3811 new_lfp->lf_usecount = 0; 3812 LIST_INSERT_HEAD(hp, new_lfp, lf_hash); 3813 *lfpp = new_lfp; 3814 *new_lfpp = NULL; 3815 return (0); 3816 } 3817 3818 /* 3819 * This function adds a nfslock lock structure to the list for the associated 3820 * nfsstate and nfslockfile structures. It will be inserted after the 3821 * entry pointed at by insert_lop. 3822 */ 3823 static void 3824 nfsrv_insertlock(struct nfslock *new_lop, struct nfslock *insert_lop, 3825 struct nfsstate *stp, struct nfslockfile *lfp) 3826 { 3827 struct nfslock *lop, *nlop; 3828 3829 new_lop->lo_stp = stp; 3830 new_lop->lo_lfp = lfp; 3831 3832 if (stp != NULL) { 3833 /* Insert in increasing lo_first order */ 3834 lop = LIST_FIRST(&lfp->lf_lock); 3835 if (lop == LIST_END(&lfp->lf_lock) || 3836 new_lop->lo_first <= lop->lo_first) { 3837 LIST_INSERT_HEAD(&lfp->lf_lock, new_lop, lo_lckfile); 3838 } else { 3839 nlop = LIST_NEXT(lop, lo_lckfile); 3840 while (nlop != LIST_END(&lfp->lf_lock) && 3841 nlop->lo_first < new_lop->lo_first) { 3842 lop = nlop; 3843 nlop = LIST_NEXT(lop, lo_lckfile); 3844 } 3845 LIST_INSERT_AFTER(lop, new_lop, lo_lckfile); 3846 } 3847 } else { 3848 new_lop->lo_lckfile.le_prev = NULL; /* list not used */ 3849 } 3850 3851 /* 3852 * Insert after insert_lop, which is overloaded as stp or lfp for 3853 * an empty list. 3854 */ 3855 if (stp == NULL && (struct nfslockfile *)insert_lop == lfp) 3856 LIST_INSERT_HEAD(&lfp->lf_locallock, new_lop, lo_lckowner); 3857 else if ((struct nfsstate *)insert_lop == stp) 3858 LIST_INSERT_HEAD(&stp->ls_lock, new_lop, lo_lckowner); 3859 else 3860 LIST_INSERT_AFTER(insert_lop, new_lop, lo_lckowner); 3861 if (stp != NULL) { 3862 nfsstatsv1.srvlocks++; 3863 nfsrv_openpluslock++; 3864 } 3865 } 3866 3867 /* 3868 * This function updates the locking for a lock owner and given file. It 3869 * maintains a list of lock ranges ordered on increasing file offset that 3870 * are NFSLCK_READ or NFSLCK_WRITE and non-overlapping (aka POSIX style). 3871 * It always adds new_lop to the list and sometimes uses the one pointed 3872 * at by other_lopp. 3873 */ 3874 static void 3875 nfsrv_updatelock(struct nfsstate *stp, struct nfslock **new_lopp, 3876 struct nfslock **other_lopp, struct nfslockfile *lfp) 3877 { 3878 struct nfslock *new_lop = *new_lopp; 3879 struct nfslock *lop, *tlop, *ilop; 3880 struct nfslock *other_lop = *other_lopp; 3881 int unlock = 0, myfile = 0; 3882 u_int64_t tmp; 3883 3884 /* 3885 * Work down the list until the lock is merged. 3886 */ 3887 if (new_lop->lo_flags & NFSLCK_UNLOCK) 3888 unlock = 1; 3889 if (stp != NULL) { 3890 ilop = (struct nfslock *)stp; 3891 lop = LIST_FIRST(&stp->ls_lock); 3892 } else { 3893 ilop = (struct nfslock *)lfp; 3894 lop = LIST_FIRST(&lfp->lf_locallock); 3895 } 3896 while (lop != NULL) { 3897 /* 3898 * Only check locks for this file that aren't before the start of 3899 * new lock's range. 3900 */ 3901 if (lop->lo_lfp == lfp) { 3902 myfile = 1; 3903 if (lop->lo_end >= new_lop->lo_first) { 3904 if (new_lop->lo_end < lop->lo_first) { 3905 /* 3906 * If the new lock ends before the start of the 3907 * current lock's range, no merge, just insert 3908 * the new lock. 3909 */ 3910 break; 3911 } 3912 if (new_lop->lo_flags == lop->lo_flags || 3913 (new_lop->lo_first <= lop->lo_first && 3914 new_lop->lo_end >= lop->lo_end)) { 3915 /* 3916 * This lock can be absorbed by the new lock/unlock. 3917 * This happens when it covers the entire range 3918 * of the old lock or is contiguous 3919 * with the old lock and is of the same type or an 3920 * unlock. 3921 */ 3922 if (lop->lo_first < new_lop->lo_first) 3923 new_lop->lo_first = lop->lo_first; 3924 if (lop->lo_end > new_lop->lo_end) 3925 new_lop->lo_end = lop->lo_end; 3926 tlop = lop; 3927 lop = LIST_NEXT(lop, lo_lckowner); 3928 nfsrv_freenfslock(tlop); 3929 continue; 3930 } 3931 3932 /* 3933 * All these cases are for contiguous locks that are not the 3934 * same type, so they can't be merged. 3935 */ 3936 if (new_lop->lo_first <= lop->lo_first) { 3937 /* 3938 * This case is where the new lock overlaps with the 3939 * first part of the old lock. Move the start of the 3940 * old lock to just past the end of the new lock. The 3941 * new lock will be inserted in front of the old, since 3942 * ilop hasn't been updated. (We are done now.) 3943 */ 3944 lop->lo_first = new_lop->lo_end; 3945 break; 3946 } 3947 if (new_lop->lo_end >= lop->lo_end) { 3948 /* 3949 * This case is where the new lock overlaps with the 3950 * end of the old lock's range. Move the old lock's 3951 * end to just before the new lock's first and insert 3952 * the new lock after the old lock. 3953 * Might not be done yet, since the new lock could 3954 * overlap further locks with higher ranges. 3955 */ 3956 lop->lo_end = new_lop->lo_first; 3957 ilop = lop; 3958 lop = LIST_NEXT(lop, lo_lckowner); 3959 continue; 3960 } 3961 /* 3962 * The final case is where the new lock's range is in the 3963 * middle of the current lock's and splits the current lock 3964 * up. Use *other_lopp to handle the second part of the 3965 * split old lock range. (We are done now.) 3966 * For unlock, we use new_lop as other_lop and tmp, since 3967 * other_lop and new_lop are the same for this case. 3968 * We noted the unlock case above, so we don't need 3969 * new_lop->lo_flags any longer. 3970 */ 3971 tmp = new_lop->lo_first; 3972 if (other_lop == NULL) { 3973 if (!unlock) 3974 panic("nfsd srv update unlock"); 3975 other_lop = new_lop; 3976 *new_lopp = NULL; 3977 } 3978 other_lop->lo_first = new_lop->lo_end; 3979 other_lop->lo_end = lop->lo_end; 3980 other_lop->lo_flags = lop->lo_flags; 3981 other_lop->lo_stp = stp; 3982 other_lop->lo_lfp = lfp; 3983 lop->lo_end = tmp; 3984 nfsrv_insertlock(other_lop, lop, stp, lfp); 3985 *other_lopp = NULL; 3986 ilop = lop; 3987 break; 3988 } 3989 } 3990 ilop = lop; 3991 lop = LIST_NEXT(lop, lo_lckowner); 3992 if (myfile && (lop == NULL || lop->lo_lfp != lfp)) 3993 break; 3994 } 3995 3996 /* 3997 * Insert the new lock in the list at the appropriate place. 3998 */ 3999 if (!unlock) { 4000 nfsrv_insertlock(new_lop, ilop, stp, lfp); 4001 *new_lopp = NULL; 4002 } 4003 } 4004 4005 /* 4006 * This function handles sequencing of locks, etc. 4007 * It returns an error that indicates what the caller should do. 4008 */ 4009 static int 4010 nfsrv_checkseqid(struct nfsrv_descript *nd, u_int32_t seqid, 4011 struct nfsstate *stp, struct nfsrvcache *op) 4012 { 4013 int error = 0; 4014 4015 if ((nd->nd_flag & ND_NFSV41) != 0) 4016 /* NFSv4.1 ignores the open_seqid and lock_seqid. */ 4017 goto out; 4018 if (op != nd->nd_rp) 4019 panic("nfsrvstate checkseqid"); 4020 if (!(op->rc_flag & RC_INPROG)) 4021 panic("nfsrvstate not inprog"); 4022 if (stp->ls_op && stp->ls_op->rc_refcnt <= 0) { 4023 printf("refcnt=%d\n", stp->ls_op->rc_refcnt); 4024 panic("nfsrvstate op refcnt"); 4025 } 4026 4027 /* If ND_ERELOOKUP is set, the seqid has already been handled. */ 4028 if ((nd->nd_flag & ND_ERELOOKUP) != 0) 4029 goto out; 4030 4031 if ((stp->ls_seq + 1) == seqid) { 4032 if (stp->ls_op) 4033 nfsrvd_derefcache(stp->ls_op); 4034 stp->ls_op = op; 4035 nfsrvd_refcache(op); 4036 stp->ls_seq = seqid; 4037 goto out; 4038 } else if (stp->ls_seq == seqid && stp->ls_op && 4039 op->rc_xid == stp->ls_op->rc_xid && 4040 op->rc_refcnt == 0 && 4041 op->rc_reqlen == stp->ls_op->rc_reqlen && 4042 op->rc_cksum == stp->ls_op->rc_cksum) { 4043 if (stp->ls_op->rc_flag & RC_INPROG) { 4044 error = NFSERR_DONTREPLY; 4045 goto out; 4046 } 4047 nd->nd_rp = stp->ls_op; 4048 nd->nd_rp->rc_flag |= RC_INPROG; 4049 nfsrvd_delcache(op); 4050 error = NFSERR_REPLYFROMCACHE; 4051 goto out; 4052 } 4053 error = NFSERR_BADSEQID; 4054 4055 out: 4056 NFSEXITCODE2(error, nd); 4057 return (error); 4058 } 4059 4060 /* 4061 * Get the client ip address for callbacks. If the strings can't be parsed, 4062 * just set lc_program to 0 to indicate no callbacks are possible. 4063 * (For cases where the address can't be parsed or is 0.0.0.0.0.0, set 4064 * the address to the client's transport address. This won't be used 4065 * for callbacks, but can be printed out by nfsstats for info.) 4066 * Return error if the xdr can't be parsed, 0 otherwise. 4067 */ 4068 int 4069 nfsrv_getclientipaddr(struct nfsrv_descript *nd, struct nfsclient *clp) 4070 { 4071 u_int32_t *tl; 4072 u_char *cp, *cp2; 4073 int i, j, maxalen = 0, minalen = 0; 4074 sa_family_t af; 4075 #ifdef INET 4076 struct sockaddr_in *rin = NULL, *sin; 4077 #endif 4078 #ifdef INET6 4079 struct sockaddr_in6 *rin6 = NULL, *sin6; 4080 #endif 4081 u_char *addr; 4082 int error = 0, cantparse = 0; 4083 union { 4084 in_addr_t ival; 4085 u_char cval[4]; 4086 } ip; 4087 union { 4088 in_port_t sval; 4089 u_char cval[2]; 4090 } port; 4091 4092 /* 8 is the maximum length of the port# string. */ 4093 addr = malloc(INET6_ADDRSTRLEN + 8, M_TEMP, M_WAITOK); 4094 clp->lc_req.nr_client = NULL; 4095 clp->lc_req.nr_lock = 0; 4096 af = AF_UNSPEC; 4097 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED); 4098 i = fxdr_unsigned(int, *tl); 4099 if (i >= 3 && i <= 4) { 4100 error = nfsrv_mtostr(nd, addr, i); 4101 if (error) 4102 goto nfsmout; 4103 #ifdef INET 4104 if (!strcmp(addr, "tcp")) { 4105 clp->lc_flags |= LCL_TCPCALLBACK; 4106 clp->lc_req.nr_sotype = SOCK_STREAM; 4107 clp->lc_req.nr_soproto = IPPROTO_TCP; 4108 af = AF_INET; 4109 } else if (!strcmp(addr, "udp")) { 4110 clp->lc_req.nr_sotype = SOCK_DGRAM; 4111 clp->lc_req.nr_soproto = IPPROTO_UDP; 4112 af = AF_INET; 4113 } 4114 #endif 4115 #ifdef INET6 4116 if (af == AF_UNSPEC) { 4117 if (!strcmp(addr, "tcp6")) { 4118 clp->lc_flags |= LCL_TCPCALLBACK; 4119 clp->lc_req.nr_sotype = SOCK_STREAM; 4120 clp->lc_req.nr_soproto = IPPROTO_TCP; 4121 af = AF_INET6; 4122 } else if (!strcmp(addr, "udp6")) { 4123 clp->lc_req.nr_sotype = SOCK_DGRAM; 4124 clp->lc_req.nr_soproto = IPPROTO_UDP; 4125 af = AF_INET6; 4126 } 4127 } 4128 #endif 4129 if (af == AF_UNSPEC) { 4130 cantparse = 1; 4131 } 4132 } else { 4133 cantparse = 1; 4134 if (i > 0) { 4135 error = nfsm_advance(nd, NFSM_RNDUP(i), -1); 4136 if (error) 4137 goto nfsmout; 4138 } 4139 } 4140 /* 4141 * The caller has allocated clp->lc_req.nr_nam to be large enough 4142 * for either AF_INET or AF_INET6 and zeroed out the contents. 4143 * maxalen is set to the maximum length of the host IP address string 4144 * plus 8 for the maximum length of the port#. 4145 * minalen is set to the minimum length of the host IP address string 4146 * plus 4 for the minimum length of the port#. 4147 * These lengths do not include NULL termination, 4148 * so INET[6]_ADDRSTRLEN - 1 is used in the calculations. 4149 */ 4150 switch (af) { 4151 #ifdef INET 4152 case AF_INET: 4153 rin = (struct sockaddr_in *)clp->lc_req.nr_nam; 4154 rin->sin_family = AF_INET; 4155 rin->sin_len = sizeof(struct sockaddr_in); 4156 maxalen = INET_ADDRSTRLEN - 1 + 8; 4157 minalen = 7 + 4; 4158 break; 4159 #endif 4160 #ifdef INET6 4161 case AF_INET6: 4162 rin6 = (struct sockaddr_in6 *)clp->lc_req.nr_nam; 4163 rin6->sin6_family = AF_INET6; 4164 rin6->sin6_len = sizeof(struct sockaddr_in6); 4165 maxalen = INET6_ADDRSTRLEN - 1 + 8; 4166 minalen = 3 + 4; 4167 break; 4168 #endif 4169 } 4170 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED); 4171 i = fxdr_unsigned(int, *tl); 4172 if (i < 0) { 4173 error = NFSERR_BADXDR; 4174 goto nfsmout; 4175 } else if (i == 0) { 4176 cantparse = 1; 4177 } else if (!cantparse && i <= maxalen && i >= minalen) { 4178 error = nfsrv_mtostr(nd, addr, i); 4179 if (error) 4180 goto nfsmout; 4181 4182 /* 4183 * Parse out the address fields. We expect 6 decimal numbers 4184 * separated by '.'s for AF_INET and two decimal numbers 4185 * preceeded by '.'s for AF_INET6. 4186 */ 4187 cp = NULL; 4188 switch (af) { 4189 #ifdef INET6 4190 /* 4191 * For AF_INET6, first parse the host address. 4192 */ 4193 case AF_INET6: 4194 cp = strchr(addr, '.'); 4195 if (cp != NULL) { 4196 *cp++ = '\0'; 4197 if (inet_pton(af, addr, &rin6->sin6_addr) == 1) 4198 i = 4; 4199 else { 4200 cp = NULL; 4201 cantparse = 1; 4202 } 4203 } 4204 break; 4205 #endif 4206 #ifdef INET 4207 case AF_INET: 4208 cp = addr; 4209 i = 0; 4210 break; 4211 #endif 4212 } 4213 while (cp != NULL && *cp && i < 6) { 4214 cp2 = cp; 4215 while (*cp2 && *cp2 != '.') 4216 cp2++; 4217 if (*cp2) 4218 *cp2++ = '\0'; 4219 else if (i != 5) { 4220 cantparse = 1; 4221 break; 4222 } 4223 j = nfsrv_getipnumber(cp); 4224 if (j >= 0) { 4225 if (i < 4) 4226 ip.cval[3 - i] = j; 4227 else 4228 port.cval[5 - i] = j; 4229 } else { 4230 cantparse = 1; 4231 break; 4232 } 4233 cp = cp2; 4234 i++; 4235 } 4236 if (!cantparse) { 4237 /* 4238 * The host address INADDR_ANY is (mis)used to indicate 4239 * "there is no valid callback address". 4240 */ 4241 switch (af) { 4242 #ifdef INET6 4243 case AF_INET6: 4244 if (!IN6_ARE_ADDR_EQUAL(&rin6->sin6_addr, 4245 &in6addr_any)) 4246 rin6->sin6_port = htons(port.sval); 4247 else 4248 cantparse = 1; 4249 break; 4250 #endif 4251 #ifdef INET 4252 case AF_INET: 4253 if (ip.ival != INADDR_ANY) { 4254 rin->sin_addr.s_addr = htonl(ip.ival); 4255 rin->sin_port = htons(port.sval); 4256 } else { 4257 cantparse = 1; 4258 } 4259 break; 4260 #endif 4261 } 4262 } 4263 } else { 4264 cantparse = 1; 4265 if (i > 0) { 4266 error = nfsm_advance(nd, NFSM_RNDUP(i), -1); 4267 if (error) 4268 goto nfsmout; 4269 } 4270 } 4271 if (cantparse) { 4272 switch (nd->nd_nam->sa_family) { 4273 #ifdef INET 4274 case AF_INET: 4275 sin = (struct sockaddr_in *)nd->nd_nam; 4276 rin = (struct sockaddr_in *)clp->lc_req.nr_nam; 4277 rin->sin_family = AF_INET; 4278 rin->sin_len = sizeof(struct sockaddr_in); 4279 rin->sin_addr.s_addr = sin->sin_addr.s_addr; 4280 rin->sin_port = 0x0; 4281 break; 4282 #endif 4283 #ifdef INET6 4284 case AF_INET6: 4285 sin6 = (struct sockaddr_in6 *)nd->nd_nam; 4286 rin6 = (struct sockaddr_in6 *)clp->lc_req.nr_nam; 4287 rin6->sin6_family = AF_INET6; 4288 rin6->sin6_len = sizeof(struct sockaddr_in6); 4289 rin6->sin6_addr = sin6->sin6_addr; 4290 rin6->sin6_port = 0x0; 4291 break; 4292 #endif 4293 } 4294 clp->lc_program = 0; 4295 } 4296 nfsmout: 4297 free(addr, M_TEMP); 4298 NFSEXITCODE2(error, nd); 4299 return (error); 4300 } 4301 4302 /* 4303 * Turn a string of up to three decimal digits into a number. Return -1 upon 4304 * error. 4305 */ 4306 static int 4307 nfsrv_getipnumber(u_char *cp) 4308 { 4309 int i = 0, j = 0; 4310 4311 while (*cp) { 4312 if (j > 2 || *cp < '0' || *cp > '9') 4313 return (-1); 4314 i *= 10; 4315 i += (*cp - '0'); 4316 cp++; 4317 j++; 4318 } 4319 if (i < 256) 4320 return (i); 4321 return (-1); 4322 } 4323 4324 /* 4325 * This function checks for restart conditions. 4326 */ 4327 static int 4328 nfsrv_checkrestart(nfsquad_t clientid, u_int32_t flags, 4329 nfsv4stateid_t *stateidp, int specialid) 4330 { 4331 int ret = 0; 4332 4333 /* 4334 * First check for a server restart. Open, LockT, ReleaseLockOwner 4335 * and DelegPurge have a clientid, the rest a stateid. 4336 */ 4337 if (flags & 4338 (NFSLCK_OPEN | NFSLCK_TEST | NFSLCK_RELEASE | NFSLCK_DELEGPURGE)) { 4339 if (clientid.lval[0] != nfsrvboottime) { 4340 ret = NFSERR_STALECLIENTID; 4341 goto out; 4342 } 4343 } else if (stateidp->other[0] != nfsrvboottime && 4344 specialid == 0) { 4345 ret = NFSERR_STALESTATEID; 4346 goto out; 4347 } 4348 4349 /* 4350 * Read, Write, Setattr and LockT can return NFSERR_GRACE and do 4351 * not use a lock/open owner seqid#, so the check can be done now. 4352 * (The others will be checked, as required, later.) 4353 */ 4354 if (!(flags & (NFSLCK_CHECK | NFSLCK_TEST))) 4355 goto out; 4356 4357 NFSLOCKSTATE(); 4358 ret = nfsrv_checkgrace(NULL, NULL, flags); 4359 NFSUNLOCKSTATE(); 4360 4361 out: 4362 NFSEXITCODE(ret); 4363 return (ret); 4364 } 4365 4366 /* 4367 * Check for grace. 4368 */ 4369 static int 4370 nfsrv_checkgrace(struct nfsrv_descript *nd, struct nfsclient *clp, 4371 u_int32_t flags) 4372 { 4373 int error = 0, notreclaimed; 4374 struct nfsrv_stable *sp; 4375 4376 if ((nfsrv_stablefirst.nsf_flags & (NFSNSF_UPDATEDONE | 4377 NFSNSF_GRACEOVER)) == 0) { 4378 /* 4379 * First, check to see if all of the clients have done a 4380 * ReclaimComplete. If so, grace can end now. 4381 */ 4382 notreclaimed = 0; 4383 LIST_FOREACH(sp, &nfsrv_stablefirst.nsf_head, nst_list) { 4384 if ((sp->nst_flag & NFSNST_RECLAIMED) == 0) { 4385 notreclaimed = 1; 4386 break; 4387 } 4388 } 4389 if (notreclaimed == 0) 4390 nfsrv_stablefirst.nsf_flags |= (NFSNSF_GRACEOVER | 4391 NFSNSF_NEEDLOCK); 4392 } 4393 4394 if ((nfsrv_stablefirst.nsf_flags & NFSNSF_GRACEOVER) != 0) { 4395 if (flags & NFSLCK_RECLAIM) { 4396 error = NFSERR_NOGRACE; 4397 goto out; 4398 } 4399 } else { 4400 if (!(flags & NFSLCK_RECLAIM)) { 4401 error = NFSERR_GRACE; 4402 goto out; 4403 } 4404 if (nd != NULL && clp != NULL && 4405 (nd->nd_flag & ND_NFSV41) != 0 && 4406 (clp->lc_flags & LCL_RECLAIMCOMPLETE) != 0) { 4407 error = NFSERR_NOGRACE; 4408 goto out; 4409 } 4410 4411 /* 4412 * If grace is almost over and we are still getting Reclaims, 4413 * extend grace a bit. 4414 */ 4415 if ((NFSD_MONOSEC + NFSRV_LEASEDELTA) > 4416 nfsrv_stablefirst.nsf_eograce) 4417 nfsrv_stablefirst.nsf_eograce = NFSD_MONOSEC + 4418 NFSRV_LEASEDELTA; 4419 } 4420 4421 out: 4422 NFSEXITCODE(error); 4423 return (error); 4424 } 4425 4426 /* 4427 * Do a server callback. 4428 * The "trunc" argument is slightly overloaded and refers to different 4429 * boolean arguments for CBRECALL and CBLAYOUTRECALL. 4430 */ 4431 static int 4432 nfsrv_docallback(struct nfsclient *clp, int procnum, nfsv4stateid_t *stateidp, 4433 int trunc, fhandle_t *fhp, struct nfsvattr *nap, nfsattrbit_t *attrbitp, 4434 int laytype, NFSPROC_T *p) 4435 { 4436 struct mbuf *m; 4437 u_int32_t *tl; 4438 struct nfsrv_descript *nd; 4439 struct ucred *cred; 4440 int error = 0, slotpos; 4441 u_int32_t callback; 4442 struct nfsdsession *sep = NULL; 4443 uint64_t tval; 4444 bool dotls; 4445 4446 nd = malloc(sizeof(*nd), M_TEMP, M_WAITOK | M_ZERO); 4447 cred = newnfs_getcred(); 4448 NFSLOCKSTATE(); /* mostly for lc_cbref++ */ 4449 if (clp->lc_flags & LCL_NEEDSCONFIRM) { 4450 NFSUNLOCKSTATE(); 4451 panic("docallb"); 4452 } 4453 clp->lc_cbref++; 4454 4455 /* 4456 * Fill the callback program# and version into the request 4457 * structure for newnfs_connect() to use. 4458 */ 4459 clp->lc_req.nr_prog = clp->lc_program; 4460 #ifdef notnow 4461 if ((clp->lc_flags & LCL_NFSV41) != 0) 4462 clp->lc_req.nr_vers = NFSV41_CBVERS; 4463 else 4464 #endif 4465 clp->lc_req.nr_vers = NFSV4_CBVERS; 4466 4467 /* 4468 * First, fill in some of the fields of nd and cr. 4469 */ 4470 nd->nd_flag = ND_NFSV4; 4471 if (clp->lc_flags & LCL_GSS) 4472 nd->nd_flag |= ND_KERBV; 4473 if ((clp->lc_flags & LCL_NFSV41) != 0) 4474 nd->nd_flag |= ND_NFSV41; 4475 if ((clp->lc_flags & LCL_NFSV42) != 0) 4476 nd->nd_flag |= ND_NFSV42; 4477 nd->nd_repstat = 0; 4478 cred->cr_uid = clp->lc_uid; 4479 cred->cr_gid = clp->lc_gid; 4480 callback = clp->lc_callback; 4481 NFSUNLOCKSTATE(); 4482 cred->cr_ngroups = 1; 4483 4484 /* 4485 * Get the first mbuf for the request. 4486 */ 4487 MGET(m, M_WAITOK, MT_DATA); 4488 m->m_len = 0; 4489 nd->nd_mreq = nd->nd_mb = m; 4490 nd->nd_bpos = mtod(m, caddr_t); 4491 4492 /* 4493 * and build the callback request. 4494 */ 4495 if (procnum == NFSV4OP_CBGETATTR) { 4496 nd->nd_procnum = NFSV4PROC_CBCOMPOUND; 4497 error = nfsrv_cbcallargs(nd, clp, callback, NFSV4OP_CBGETATTR, 4498 "CB Getattr", &sep, &slotpos); 4499 if (error != 0) { 4500 m_freem(nd->nd_mreq); 4501 goto errout; 4502 } 4503 (void)nfsm_fhtom(nd, (u_int8_t *)fhp, NFSX_MYFH, 0); 4504 (void)nfsrv_putattrbit(nd, attrbitp); 4505 } else if (procnum == NFSV4OP_CBRECALL) { 4506 nd->nd_procnum = NFSV4PROC_CBCOMPOUND; 4507 error = nfsrv_cbcallargs(nd, clp, callback, NFSV4OP_CBRECALL, 4508 "CB Recall", &sep, &slotpos); 4509 if (error != 0) { 4510 m_freem(nd->nd_mreq); 4511 goto errout; 4512 } 4513 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED + NFSX_STATEID); 4514 *tl++ = txdr_unsigned(stateidp->seqid); 4515 NFSBCOPY((caddr_t)stateidp->other, (caddr_t)tl, 4516 NFSX_STATEIDOTHER); 4517 tl += (NFSX_STATEIDOTHER / NFSX_UNSIGNED); 4518 if (trunc) 4519 *tl = newnfs_true; 4520 else 4521 *tl = newnfs_false; 4522 (void)nfsm_fhtom(nd, (u_int8_t *)fhp, NFSX_MYFH, 0); 4523 } else if (procnum == NFSV4OP_CBLAYOUTRECALL) { 4524 NFSD_DEBUG(4, "docallback layout recall\n"); 4525 nd->nd_procnum = NFSV4PROC_CBCOMPOUND; 4526 error = nfsrv_cbcallargs(nd, clp, callback, 4527 NFSV4OP_CBLAYOUTRECALL, "CB Reclayout", &sep, &slotpos); 4528 NFSD_DEBUG(4, "aft cbcallargs=%d\n", error); 4529 if (error != 0) { 4530 m_freem(nd->nd_mreq); 4531 goto errout; 4532 } 4533 NFSM_BUILD(tl, u_int32_t *, 4 * NFSX_UNSIGNED); 4534 *tl++ = txdr_unsigned(laytype); 4535 *tl++ = txdr_unsigned(NFSLAYOUTIOMODE_ANY); 4536 if (trunc) 4537 *tl++ = newnfs_true; 4538 else 4539 *tl++ = newnfs_false; 4540 *tl = txdr_unsigned(NFSV4LAYOUTRET_FILE); 4541 nfsm_fhtom(nd, (uint8_t *)fhp, NFSX_MYFH, 0); 4542 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_HYPER + NFSX_STATEID); 4543 tval = 0; 4544 txdr_hyper(tval, tl); tl += 2; 4545 tval = UINT64_MAX; 4546 txdr_hyper(tval, tl); tl += 2; 4547 *tl++ = txdr_unsigned(stateidp->seqid); 4548 NFSBCOPY(stateidp->other, tl, NFSX_STATEIDOTHER); 4549 tl += (NFSX_STATEIDOTHER / NFSX_UNSIGNED); 4550 NFSD_DEBUG(4, "aft args\n"); 4551 } else if (procnum == NFSV4PROC_CBNULL) { 4552 nd->nd_procnum = NFSV4PROC_CBNULL; 4553 if ((clp->lc_flags & LCL_NFSV41) != 0) { 4554 error = nfsv4_getcbsession(clp, &sep); 4555 if (error != 0) { 4556 m_freem(nd->nd_mreq); 4557 goto errout; 4558 } 4559 } 4560 } else { 4561 error = NFSERR_SERVERFAULT; 4562 m_freem(nd->nd_mreq); 4563 goto errout; 4564 } 4565 4566 /* 4567 * Call newnfs_connect(), as required, and then newnfs_request(). 4568 */ 4569 dotls = false; 4570 if ((clp->lc_flags & LCL_TLSCB) != 0) 4571 dotls = true; 4572 (void) newnfs_sndlock(&clp->lc_req.nr_lock); 4573 if (clp->lc_req.nr_client == NULL) { 4574 if ((clp->lc_flags & LCL_NFSV41) != 0) { 4575 error = ECONNREFUSED; 4576 if (procnum != NFSV4PROC_CBNULL) 4577 nfsv4_freeslot(&sep->sess_cbsess, slotpos, 4578 true); 4579 nfsrv_freesession(sep, NULL); 4580 } else if (nd->nd_procnum == NFSV4PROC_CBNULL) 4581 error = newnfs_connect(NULL, &clp->lc_req, cred, 4582 NULL, 1, dotls, &clp->lc_req.nr_client); 4583 else 4584 error = newnfs_connect(NULL, &clp->lc_req, cred, 4585 NULL, 3, dotls, &clp->lc_req.nr_client); 4586 } 4587 newnfs_sndunlock(&clp->lc_req.nr_lock); 4588 NFSD_DEBUG(4, "aft sndunlock=%d\n", error); 4589 if (!error) { 4590 if ((nd->nd_flag & ND_NFSV41) != 0) { 4591 KASSERT(sep != NULL, ("sep NULL")); 4592 if (sep->sess_cbsess.nfsess_xprt != NULL) 4593 error = newnfs_request(nd, NULL, clp, 4594 &clp->lc_req, NULL, NULL, cred, 4595 clp->lc_program, clp->lc_req.nr_vers, NULL, 4596 1, NULL, &sep->sess_cbsess); 4597 else { 4598 /* 4599 * This should probably never occur, but if a 4600 * client somehow does an RPC without a 4601 * SequenceID Op that causes a callback just 4602 * after the nfsd threads have been terminated 4603 * and restared we could conceivably get here 4604 * without a backchannel xprt. 4605 */ 4606 printf("nfsrv_docallback: no xprt\n"); 4607 error = ECONNREFUSED; 4608 } 4609 NFSD_DEBUG(4, "aft newnfs_request=%d\n", error); 4610 if (error != 0 && procnum != NFSV4PROC_CBNULL) { 4611 /* 4612 * It is likely that the callback was never 4613 * processed by the client and, as such, 4614 * the sequence# for the session slot needs 4615 * to be backed up by one to avoid a 4616 * NFSERR_SEQMISORDERED error reply. 4617 * For the unlikely case where the callback 4618 * was processed by the client, this will 4619 * make the next callback on the slot 4620 * appear to be a retry. 4621 * Since callbacks never specify that the 4622 * reply be cached, this "apparent retry" 4623 * should not be a problem. 4624 */ 4625 nfsv4_freeslot(&sep->sess_cbsess, slotpos, 4626 true); 4627 } 4628 nfsrv_freesession(sep, NULL); 4629 } else 4630 error = newnfs_request(nd, NULL, clp, &clp->lc_req, 4631 NULL, NULL, cred, clp->lc_program, 4632 clp->lc_req.nr_vers, NULL, 1, NULL, NULL); 4633 } 4634 errout: 4635 NFSFREECRED(cred); 4636 4637 /* 4638 * If error is set here, the Callback path isn't working 4639 * properly, so twiddle the appropriate LCL_ flags. 4640 * (nd_repstat != 0 indicates the Callback path is working, 4641 * but the callback failed on the client.) 4642 */ 4643 if (error) { 4644 /* 4645 * Mark the callback pathway down, which disabled issuing 4646 * of delegations and gets Renew to return NFSERR_CBPATHDOWN. 4647 */ 4648 NFSLOCKSTATE(); 4649 clp->lc_flags |= LCL_CBDOWN; 4650 NFSUNLOCKSTATE(); 4651 } else { 4652 /* 4653 * Callback worked. If the callback path was down, disable 4654 * callbacks, so no more delegations will be issued. (This 4655 * is done on the assumption that the callback pathway is 4656 * flakey.) 4657 */ 4658 NFSLOCKSTATE(); 4659 if (clp->lc_flags & LCL_CBDOWN) 4660 clp->lc_flags &= ~(LCL_CBDOWN | LCL_CALLBACKSON); 4661 NFSUNLOCKSTATE(); 4662 if (nd->nd_repstat) { 4663 error = nd->nd_repstat; 4664 NFSD_DEBUG(1, "nfsrv_docallback op=%d err=%d\n", 4665 procnum, error); 4666 } else if (error == 0 && procnum == NFSV4OP_CBGETATTR) 4667 error = nfsv4_loadattr(nd, NULL, nap, NULL, NULL, 0, 4668 NULL, NULL, NULL, NULL, NULL, 0, NULL, NULL, NULL, 4669 p, NULL); 4670 m_freem(nd->nd_mrep); 4671 } 4672 NFSLOCKSTATE(); 4673 clp->lc_cbref--; 4674 if ((clp->lc_flags & LCL_WAKEUPWANTED) && clp->lc_cbref == 0) { 4675 clp->lc_flags &= ~LCL_WAKEUPWANTED; 4676 wakeup(clp); 4677 } 4678 NFSUNLOCKSTATE(); 4679 4680 free(nd, M_TEMP); 4681 NFSEXITCODE(error); 4682 return (error); 4683 } 4684 4685 /* 4686 * Set up the compound RPC for the callback. 4687 */ 4688 static int 4689 nfsrv_cbcallargs(struct nfsrv_descript *nd, struct nfsclient *clp, 4690 uint32_t callback, int op, const char *optag, struct nfsdsession **sepp, 4691 int *slotposp) 4692 { 4693 uint32_t *tl; 4694 int error, len; 4695 4696 len = strlen(optag); 4697 (void)nfsm_strtom(nd, optag, len); 4698 NFSM_BUILD(tl, uint32_t *, 4 * NFSX_UNSIGNED); 4699 if ((nd->nd_flag & ND_NFSV41) != 0) { 4700 if ((nd->nd_flag & ND_NFSV42) != 0) 4701 *tl++ = txdr_unsigned(NFSV42_MINORVERSION); 4702 else 4703 *tl++ = txdr_unsigned(NFSV41_MINORVERSION); 4704 *tl++ = txdr_unsigned(callback); 4705 *tl++ = txdr_unsigned(2); 4706 *tl = txdr_unsigned(NFSV4OP_CBSEQUENCE); 4707 error = nfsv4_setcbsequence(nd, clp, 1, sepp, slotposp); 4708 if (error != 0) 4709 return (error); 4710 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED); 4711 *tl = txdr_unsigned(op); 4712 } else { 4713 *tl++ = txdr_unsigned(NFSV4_MINORVERSION); 4714 *tl++ = txdr_unsigned(callback); 4715 *tl++ = txdr_unsigned(1); 4716 *tl = txdr_unsigned(op); 4717 } 4718 return (0); 4719 } 4720 4721 /* 4722 * Return the next index# for a clientid. Mostly just increment and return 4723 * the next one, but... if the 32bit unsigned does actually wrap around, 4724 * it should be rebooted. 4725 * At an average rate of one new client per second, it will wrap around in 4726 * approximately 136 years. (I think the server will have been shut 4727 * down or rebooted before then.) 4728 */ 4729 static u_int32_t 4730 nfsrv_nextclientindex(void) 4731 { 4732 static u_int32_t client_index = 0; 4733 4734 client_index++; 4735 if (client_index != 0) 4736 return (client_index); 4737 4738 printf("%s: out of clientids\n", __func__); 4739 return (client_index); 4740 } 4741 4742 /* 4743 * Return the next index# for a stateid. Mostly just increment and return 4744 * the next one, but... if the 32bit unsigned does actually wrap around 4745 * (will a BSD server stay up that long?), find 4746 * new start and end values. 4747 */ 4748 static u_int32_t 4749 nfsrv_nextstateindex(struct nfsclient *clp) 4750 { 4751 struct nfsstate *stp; 4752 int i; 4753 u_int32_t canuse, min_index, max_index; 4754 4755 if (!(clp->lc_flags & LCL_INDEXNOTOK)) { 4756 clp->lc_stateindex++; 4757 if (clp->lc_stateindex != clp->lc_statemaxindex) 4758 return (clp->lc_stateindex); 4759 } 4760 4761 /* 4762 * Yuck, we've hit the end. 4763 * Look for a new min and max. 4764 */ 4765 min_index = 0; 4766 max_index = 0xffffffff; 4767 for (i = 0; i < nfsrv_statehashsize; i++) { 4768 LIST_FOREACH(stp, &clp->lc_stateid[i], ls_hash) { 4769 if (stp->ls_stateid.other[2] > 0x80000000) { 4770 if (stp->ls_stateid.other[2] < max_index) 4771 max_index = stp->ls_stateid.other[2]; 4772 } else { 4773 if (stp->ls_stateid.other[2] > min_index) 4774 min_index = stp->ls_stateid.other[2]; 4775 } 4776 } 4777 } 4778 4779 /* 4780 * Yikes, highly unlikely, but I'll handle it anyhow. 4781 */ 4782 if (min_index == 0x80000000 && max_index == 0x80000001) { 4783 canuse = 0; 4784 /* 4785 * Loop around until we find an unused entry. Return that 4786 * and set LCL_INDEXNOTOK, so the search will continue next time. 4787 * (This is one of those rare cases where a goto is the 4788 * cleanest way to code the loop.) 4789 */ 4790 tryagain: 4791 for (i = 0; i < nfsrv_statehashsize; i++) { 4792 LIST_FOREACH(stp, &clp->lc_stateid[i], ls_hash) { 4793 if (stp->ls_stateid.other[2] == canuse) { 4794 canuse++; 4795 goto tryagain; 4796 } 4797 } 4798 } 4799 clp->lc_flags |= LCL_INDEXNOTOK; 4800 return (canuse); 4801 } 4802 4803 /* 4804 * Ok to start again from min + 1. 4805 */ 4806 clp->lc_stateindex = min_index + 1; 4807 clp->lc_statemaxindex = max_index; 4808 clp->lc_flags &= ~LCL_INDEXNOTOK; 4809 return (clp->lc_stateindex); 4810 } 4811 4812 /* 4813 * The following functions handle the stable storage file that deals with 4814 * the edge conditions described in RFC3530 Sec. 8.6.3. 4815 * The file is as follows: 4816 * - a single record at the beginning that has the lease time of the 4817 * previous server instance (before the last reboot) and the nfsrvboottime 4818 * values for the previous server boots. 4819 * These previous boot times are used to ensure that the current 4820 * nfsrvboottime does not, somehow, get set to a previous one. 4821 * (This is important so that Stale ClientIDs and StateIDs can 4822 * be recognized.) 4823 * The number of previous nfsvrboottime values precedes the list. 4824 * - followed by some number of appended records with: 4825 * - client id string 4826 * - flag that indicates it is a record revoking state via lease 4827 * expiration or similar 4828 * OR has successfully acquired state. 4829 * These structures vary in length, with the client string at the end, up 4830 * to NFSV4_OPAQUELIMIT in size. 4831 * 4832 * At the end of the grace period, the file is truncated, the first 4833 * record is rewritten with updated information and any acquired state 4834 * records for successful reclaims of state are written. 4835 * 4836 * Subsequent records are appended when the first state is issued to 4837 * a client and when state is revoked for a client. 4838 * 4839 * When reading the file in, state issued records that come later in 4840 * the file override older ones, since the append log is in cronological order. 4841 * If, for some reason, the file can't be read, the grace period is 4842 * immediately terminated and all reclaims get NFSERR_NOGRACE. 4843 */ 4844 4845 /* 4846 * Read in the stable storage file. Called by nfssvc() before the nfsd 4847 * processes start servicing requests. 4848 */ 4849 void 4850 nfsrv_setupstable(NFSPROC_T *p) 4851 { 4852 struct nfsrv_stablefirst *sf = &nfsrv_stablefirst; 4853 struct nfsrv_stable *sp, *nsp; 4854 struct nfst_rec *tsp; 4855 int error, i, tryagain; 4856 off_t off = 0; 4857 ssize_t aresid, len; 4858 4859 /* 4860 * If NFSNSF_UPDATEDONE is set, this is a restart of the nfsds without 4861 * a reboot, so state has not been lost. 4862 */ 4863 if (sf->nsf_flags & NFSNSF_UPDATEDONE) 4864 return; 4865 /* 4866 * Set Grace over just until the file reads successfully. 4867 */ 4868 nfsrvboottime = time_second; 4869 LIST_INIT(&sf->nsf_head); 4870 sf->nsf_flags = (NFSNSF_GRACEOVER | NFSNSF_NEEDLOCK); 4871 sf->nsf_eograce = NFSD_MONOSEC + NFSRV_LEASEDELTA; 4872 if (sf->nsf_fp == NULL) 4873 return; 4874 error = NFSD_RDWR(UIO_READ, NFSFPVNODE(sf->nsf_fp), 4875 (caddr_t)&sf->nsf_rec, sizeof (struct nfsf_rec), off, UIO_SYSSPACE, 4876 0, NFSFPCRED(sf->nsf_fp), &aresid, p); 4877 if (error || aresid || sf->nsf_numboots == 0 || 4878 sf->nsf_numboots > NFSNSF_MAXNUMBOOTS) 4879 return; 4880 4881 /* 4882 * Now, read in the boottimes. 4883 */ 4884 sf->nsf_bootvals = (time_t *)malloc((sf->nsf_numboots + 1) * 4885 sizeof (time_t), M_TEMP, M_WAITOK); 4886 off = sizeof (struct nfsf_rec); 4887 error = NFSD_RDWR(UIO_READ, NFSFPVNODE(sf->nsf_fp), 4888 (caddr_t)sf->nsf_bootvals, sf->nsf_numboots * sizeof (time_t), off, 4889 UIO_SYSSPACE, 0, NFSFPCRED(sf->nsf_fp), &aresid, p); 4890 if (error || aresid) { 4891 free(sf->nsf_bootvals, M_TEMP); 4892 sf->nsf_bootvals = NULL; 4893 return; 4894 } 4895 4896 /* 4897 * Make sure this nfsrvboottime is different from all recorded 4898 * previous ones. 4899 */ 4900 do { 4901 tryagain = 0; 4902 for (i = 0; i < sf->nsf_numboots; i++) { 4903 if (nfsrvboottime == sf->nsf_bootvals[i]) { 4904 nfsrvboottime++; 4905 tryagain = 1; 4906 break; 4907 } 4908 } 4909 } while (tryagain); 4910 4911 sf->nsf_flags |= NFSNSF_OK; 4912 off += (sf->nsf_numboots * sizeof (time_t)); 4913 4914 /* 4915 * Read through the file, building a list of records for grace 4916 * checking. 4917 * Each record is between sizeof (struct nfst_rec) and 4918 * sizeof (struct nfst_rec) + NFSV4_OPAQUELIMIT - 1 4919 * and is actually sizeof (struct nfst_rec) + nst_len - 1. 4920 */ 4921 tsp = (struct nfst_rec *)malloc(sizeof (struct nfst_rec) + 4922 NFSV4_OPAQUELIMIT - 1, M_TEMP, M_WAITOK); 4923 do { 4924 error = NFSD_RDWR(UIO_READ, NFSFPVNODE(sf->nsf_fp), 4925 (caddr_t)tsp, sizeof (struct nfst_rec) + NFSV4_OPAQUELIMIT - 1, 4926 off, UIO_SYSSPACE, 0, NFSFPCRED(sf->nsf_fp), &aresid, p); 4927 len = (sizeof (struct nfst_rec) + NFSV4_OPAQUELIMIT - 1) - aresid; 4928 if (error || (len > 0 && (len < sizeof (struct nfst_rec) || 4929 len < (sizeof (struct nfst_rec) + tsp->len - 1)))) { 4930 /* 4931 * Yuck, the file has been corrupted, so just return 4932 * after clearing out any restart state, so the grace period 4933 * is over. 4934 */ 4935 LIST_FOREACH_SAFE(sp, &sf->nsf_head, nst_list, nsp) { 4936 LIST_REMOVE(sp, nst_list); 4937 free(sp, M_TEMP); 4938 } 4939 free(tsp, M_TEMP); 4940 sf->nsf_flags &= ~NFSNSF_OK; 4941 free(sf->nsf_bootvals, M_TEMP); 4942 sf->nsf_bootvals = NULL; 4943 return; 4944 } 4945 if (len > 0) { 4946 off += sizeof (struct nfst_rec) + tsp->len - 1; 4947 /* 4948 * Search the list for a matching client. 4949 */ 4950 LIST_FOREACH(sp, &sf->nsf_head, nst_list) { 4951 if (tsp->len == sp->nst_len && 4952 !NFSBCMP(tsp->client, sp->nst_client, tsp->len)) 4953 break; 4954 } 4955 if (sp == LIST_END(&sf->nsf_head)) { 4956 sp = (struct nfsrv_stable *)malloc(tsp->len + 4957 sizeof (struct nfsrv_stable) - 1, M_TEMP, 4958 M_WAITOK); 4959 NFSBCOPY((caddr_t)tsp, (caddr_t)&sp->nst_rec, 4960 sizeof (struct nfst_rec) + tsp->len - 1); 4961 LIST_INSERT_HEAD(&sf->nsf_head, sp, nst_list); 4962 } else { 4963 if (tsp->flag == NFSNST_REVOKE) 4964 sp->nst_flag |= NFSNST_REVOKE; 4965 else 4966 /* 4967 * A subsequent timestamp indicates the client 4968 * did a setclientid/confirm and any previous 4969 * revoke is no longer relevant. 4970 */ 4971 sp->nst_flag &= ~NFSNST_REVOKE; 4972 } 4973 } 4974 } while (len > 0); 4975 free(tsp, M_TEMP); 4976 sf->nsf_flags = NFSNSF_OK; 4977 sf->nsf_eograce = NFSD_MONOSEC + sf->nsf_lease + 4978 NFSRV_LEASEDELTA; 4979 } 4980 4981 /* 4982 * Update the stable storage file, now that the grace period is over. 4983 */ 4984 void 4985 nfsrv_updatestable(NFSPROC_T *p) 4986 { 4987 struct nfsrv_stablefirst *sf = &nfsrv_stablefirst; 4988 struct nfsrv_stable *sp, *nsp; 4989 int i; 4990 struct nfsvattr nva; 4991 vnode_t vp; 4992 #if defined(__FreeBSD_version) && (__FreeBSD_version >= 500000) 4993 mount_t mp = NULL; 4994 #endif 4995 int error; 4996 4997 if (sf->nsf_fp == NULL || (sf->nsf_flags & NFSNSF_UPDATEDONE)) 4998 return; 4999 sf->nsf_flags |= NFSNSF_UPDATEDONE; 5000 /* 5001 * Ok, we need to rewrite the stable storage file. 5002 * - truncate to 0 length 5003 * - write the new first structure 5004 * - loop through the data structures, writing out any that 5005 * have timestamps older than the old boot 5006 */ 5007 if (sf->nsf_bootvals) { 5008 sf->nsf_numboots++; 5009 for (i = sf->nsf_numboots - 2; i >= 0; i--) 5010 sf->nsf_bootvals[i + 1] = sf->nsf_bootvals[i]; 5011 } else { 5012 sf->nsf_numboots = 1; 5013 sf->nsf_bootvals = (time_t *)malloc(sizeof (time_t), 5014 M_TEMP, M_WAITOK); 5015 } 5016 sf->nsf_bootvals[0] = nfsrvboottime; 5017 sf->nsf_lease = nfsrv_lease; 5018 NFSVNO_ATTRINIT(&nva); 5019 NFSVNO_SETATTRVAL(&nva, size, 0); 5020 vp = NFSFPVNODE(sf->nsf_fp); 5021 vn_start_write(vp, &mp, V_WAIT); 5022 if (NFSVOPLOCK(vp, LK_EXCLUSIVE) == 0) { 5023 error = nfsvno_setattr(vp, &nva, NFSFPCRED(sf->nsf_fp), p, 5024 NULL); 5025 NFSVOPUNLOCK(vp); 5026 } else 5027 error = EPERM; 5028 vn_finished_write(mp); 5029 if (!error) 5030 error = NFSD_RDWR(UIO_WRITE, vp, 5031 (caddr_t)&sf->nsf_rec, sizeof (struct nfsf_rec), (off_t)0, 5032 UIO_SYSSPACE, IO_SYNC, NFSFPCRED(sf->nsf_fp), NULL, p); 5033 if (!error) 5034 error = NFSD_RDWR(UIO_WRITE, vp, 5035 (caddr_t)sf->nsf_bootvals, 5036 sf->nsf_numboots * sizeof (time_t), 5037 (off_t)(sizeof (struct nfsf_rec)), 5038 UIO_SYSSPACE, IO_SYNC, NFSFPCRED(sf->nsf_fp), NULL, p); 5039 free(sf->nsf_bootvals, M_TEMP); 5040 sf->nsf_bootvals = NULL; 5041 if (error) { 5042 sf->nsf_flags &= ~NFSNSF_OK; 5043 printf("EEK! Can't write NfsV4 stable storage file\n"); 5044 return; 5045 } 5046 sf->nsf_flags |= NFSNSF_OK; 5047 5048 /* 5049 * Loop through the list and write out timestamp records for 5050 * any clients that successfully reclaimed state. 5051 */ 5052 LIST_FOREACH_SAFE(sp, &sf->nsf_head, nst_list, nsp) { 5053 if (sp->nst_flag & NFSNST_GOTSTATE) { 5054 nfsrv_writestable(sp->nst_client, sp->nst_len, 5055 NFSNST_NEWSTATE, p); 5056 sp->nst_clp->lc_flags |= LCL_STAMPEDSTABLE; 5057 } 5058 LIST_REMOVE(sp, nst_list); 5059 free(sp, M_TEMP); 5060 } 5061 nfsrv_backupstable(); 5062 } 5063 5064 /* 5065 * Append a record to the stable storage file. 5066 */ 5067 void 5068 nfsrv_writestable(u_char *client, int len, int flag, NFSPROC_T *p) 5069 { 5070 struct nfsrv_stablefirst *sf = &nfsrv_stablefirst; 5071 struct nfst_rec *sp; 5072 int error; 5073 5074 if (!(sf->nsf_flags & NFSNSF_OK) || sf->nsf_fp == NULL) 5075 return; 5076 sp = (struct nfst_rec *)malloc(sizeof (struct nfst_rec) + 5077 len - 1, M_TEMP, M_WAITOK); 5078 sp->len = len; 5079 NFSBCOPY(client, sp->client, len); 5080 sp->flag = flag; 5081 error = NFSD_RDWR(UIO_WRITE, NFSFPVNODE(sf->nsf_fp), 5082 (caddr_t)sp, sizeof (struct nfst_rec) + len - 1, (off_t)0, 5083 UIO_SYSSPACE, (IO_SYNC | IO_APPEND), NFSFPCRED(sf->nsf_fp), NULL, p); 5084 free(sp, M_TEMP); 5085 if (error) { 5086 sf->nsf_flags &= ~NFSNSF_OK; 5087 printf("EEK! Can't write NfsV4 stable storage file\n"); 5088 } 5089 } 5090 5091 /* 5092 * This function is called during the grace period to mark a client 5093 * that successfully reclaimed state. 5094 */ 5095 static void 5096 nfsrv_markstable(struct nfsclient *clp) 5097 { 5098 struct nfsrv_stable *sp; 5099 5100 /* 5101 * First find the client structure. 5102 */ 5103 LIST_FOREACH(sp, &nfsrv_stablefirst.nsf_head, nst_list) { 5104 if (sp->nst_len == clp->lc_idlen && 5105 !NFSBCMP(sp->nst_client, clp->lc_id, sp->nst_len)) 5106 break; 5107 } 5108 if (sp == LIST_END(&nfsrv_stablefirst.nsf_head)) 5109 return; 5110 5111 /* 5112 * Now, just mark it and set the nfsclient back pointer. 5113 */ 5114 sp->nst_flag |= NFSNST_GOTSTATE; 5115 sp->nst_clp = clp; 5116 } 5117 5118 /* 5119 * This function is called when a NFSv4.1 client does a ReclaimComplete. 5120 * Very similar to nfsrv_markstable(), except for the flag being set. 5121 */ 5122 static void 5123 nfsrv_markreclaim(struct nfsclient *clp) 5124 { 5125 struct nfsrv_stable *sp; 5126 5127 /* 5128 * First find the client structure. 5129 */ 5130 LIST_FOREACH(sp, &nfsrv_stablefirst.nsf_head, nst_list) { 5131 if (sp->nst_len == clp->lc_idlen && 5132 !NFSBCMP(sp->nst_client, clp->lc_id, sp->nst_len)) 5133 break; 5134 } 5135 if (sp == LIST_END(&nfsrv_stablefirst.nsf_head)) 5136 return; 5137 5138 /* 5139 * Now, just set the flag. 5140 */ 5141 sp->nst_flag |= NFSNST_RECLAIMED; 5142 } 5143 5144 /* 5145 * This function is called for a reclaim, to see if it gets grace. 5146 * It returns 0 if a reclaim is allowed, 1 otherwise. 5147 */ 5148 static int 5149 nfsrv_checkstable(struct nfsclient *clp) 5150 { 5151 struct nfsrv_stable *sp; 5152 5153 /* 5154 * First, find the entry for the client. 5155 */ 5156 LIST_FOREACH(sp, &nfsrv_stablefirst.nsf_head, nst_list) { 5157 if (sp->nst_len == clp->lc_idlen && 5158 !NFSBCMP(sp->nst_client, clp->lc_id, sp->nst_len)) 5159 break; 5160 } 5161 5162 /* 5163 * If not in the list, state was revoked or no state was issued 5164 * since the previous reboot, a reclaim is denied. 5165 */ 5166 if (sp == LIST_END(&nfsrv_stablefirst.nsf_head) || 5167 (sp->nst_flag & NFSNST_REVOKE) || 5168 !(nfsrv_stablefirst.nsf_flags & NFSNSF_OK)) 5169 return (1); 5170 return (0); 5171 } 5172 5173 /* 5174 * Test for and try to clear out a conflicting client. This is called by 5175 * nfsrv_lockctrl() and nfsrv_openctrl() when conflicts with other clients 5176 * a found. 5177 * The trick here is that it can't revoke a conflicting client with an 5178 * expired lease unless it holds the v4root lock, so... 5179 * If no v4root lock, get the lock and return 1 to indicate "try again". 5180 * Return 0 to indicate the conflict can't be revoked and 1 to indicate 5181 * the revocation worked and the conflicting client is "bye, bye", so it 5182 * can be tried again. 5183 * Return 2 to indicate that the vnode is VIRF_DOOMED after NFSVOPLOCK(). 5184 * Unlocks State before a non-zero value is returned. 5185 */ 5186 static int 5187 nfsrv_clientconflict(struct nfsclient *clp, int *haslockp, vnode_t vp, 5188 NFSPROC_T *p) 5189 { 5190 int gotlock, lktype = 0; 5191 5192 /* 5193 * If lease hasn't expired, we can't fix it. 5194 */ 5195 if (clp->lc_expiry >= NFSD_MONOSEC || 5196 !(nfsrv_stablefirst.nsf_flags & NFSNSF_UPDATEDONE)) 5197 return (0); 5198 if (*haslockp == 0) { 5199 NFSUNLOCKSTATE(); 5200 if (vp != NULL) { 5201 lktype = NFSVOPISLOCKED(vp); 5202 NFSVOPUNLOCK(vp); 5203 } 5204 NFSLOCKV4ROOTMUTEX(); 5205 nfsv4_relref(&nfsv4rootfs_lock); 5206 do { 5207 gotlock = nfsv4_lock(&nfsv4rootfs_lock, 1, NULL, 5208 NFSV4ROOTLOCKMUTEXPTR, NULL); 5209 } while (!gotlock); 5210 NFSUNLOCKV4ROOTMUTEX(); 5211 *haslockp = 1; 5212 if (vp != NULL) { 5213 NFSVOPLOCK(vp, lktype | LK_RETRY); 5214 if (VN_IS_DOOMED(vp)) 5215 return (2); 5216 } 5217 return (1); 5218 } 5219 NFSUNLOCKSTATE(); 5220 5221 /* 5222 * Ok, we can expire the conflicting client. 5223 */ 5224 nfsrv_writestable(clp->lc_id, clp->lc_idlen, NFSNST_REVOKE, p); 5225 nfsrv_backupstable(); 5226 nfsrv_cleanclient(clp, p); 5227 nfsrv_freedeleglist(&clp->lc_deleg); 5228 nfsrv_freedeleglist(&clp->lc_olddeleg); 5229 LIST_REMOVE(clp, lc_hash); 5230 nfsrv_zapclient(clp, p); 5231 return (1); 5232 } 5233 5234 /* 5235 * Resolve a delegation conflict. 5236 * Returns 0 to indicate the conflict was resolved without sleeping. 5237 * Return -1 to indicate that the caller should check for conflicts again. 5238 * Return > 0 for an error that should be returned, normally NFSERR_DELAY. 5239 * 5240 * Also, manipulate the nfsv4root_lock, as required. It isn't changed 5241 * for a return of 0, since there was no sleep and it could be required 5242 * later. It is released for a return of NFSERR_DELAY, since the caller 5243 * will return that error. It is released when a sleep was done waiting 5244 * for the delegation to be returned or expire (so that other nfsds can 5245 * handle ops). Then, it must be acquired for the write to stable storage. 5246 * (This function is somewhat similar to nfsrv_clientconflict(), but 5247 * the semantics differ in a couple of subtle ways. The return of 0 5248 * indicates the conflict was resolved without sleeping here, not 5249 * that the conflict can't be resolved and the handling of nfsv4root_lock 5250 * differs, as noted above.) 5251 * Unlocks State before returning a non-zero value. 5252 */ 5253 static int 5254 nfsrv_delegconflict(struct nfsstate *stp, int *haslockp, NFSPROC_T *p, 5255 vnode_t vp) 5256 { 5257 struct nfsclient *clp = stp->ls_clp; 5258 int gotlock, error, lktype = 0, retrycnt, zapped_clp; 5259 nfsv4stateid_t tstateid; 5260 fhandle_t tfh; 5261 5262 /* 5263 * If the conflict is with an old delegation... 5264 */ 5265 if (stp->ls_flags & NFSLCK_OLDDELEG) { 5266 /* 5267 * You can delete it, if it has expired. 5268 */ 5269 if (clp->lc_delegtime < NFSD_MONOSEC) { 5270 nfsrv_freedeleg(stp); 5271 NFSUNLOCKSTATE(); 5272 error = -1; 5273 goto out; 5274 } 5275 NFSUNLOCKSTATE(); 5276 /* 5277 * During this delay, the old delegation could expire or it 5278 * could be recovered by the client via an Open with 5279 * CLAIM_DELEGATE_PREV. 5280 * Release the nfsv4root_lock, if held. 5281 */ 5282 if (*haslockp) { 5283 *haslockp = 0; 5284 NFSLOCKV4ROOTMUTEX(); 5285 nfsv4_unlock(&nfsv4rootfs_lock, 1); 5286 NFSUNLOCKV4ROOTMUTEX(); 5287 } 5288 error = NFSERR_DELAY; 5289 goto out; 5290 } 5291 5292 /* 5293 * It's a current delegation, so: 5294 * - check to see if the delegation has expired 5295 * - if so, get the v4root lock and then expire it 5296 */ 5297 if ((stp->ls_flags & NFSLCK_DELEGRECALL) == 0 || (stp->ls_lastrecall < 5298 NFSD_MONOSEC && clp->lc_expiry >= NFSD_MONOSEC && 5299 stp->ls_delegtime >= NFSD_MONOSEC)) { 5300 /* 5301 * - do a recall callback, since not yet done 5302 * For now, never allow truncate to be set. To use 5303 * truncate safely, it must be guaranteed that the 5304 * Remove, Rename or Setattr with size of 0 will 5305 * succeed and that would require major changes to 5306 * the VFS/Vnode OPs. 5307 * Set the expiry time large enough so that it won't expire 5308 * until after the callback, then set it correctly, once 5309 * the callback is done. (The delegation will now time 5310 * out whether or not the Recall worked ok. The timeout 5311 * will be extended when ops are done on the delegation 5312 * stateid, up to the timelimit.) 5313 */ 5314 if ((stp->ls_flags & NFSLCK_DELEGRECALL) == 0) { 5315 stp->ls_delegtime = NFSD_MONOSEC + (2 * nfsrv_lease) + 5316 NFSRV_LEASEDELTA; 5317 stp->ls_delegtimelimit = NFSD_MONOSEC + (6 * 5318 nfsrv_lease) + NFSRV_LEASEDELTA; 5319 stp->ls_flags |= NFSLCK_DELEGRECALL; 5320 } 5321 stp->ls_lastrecall = time_uptime + 1; 5322 5323 /* 5324 * Loop NFSRV_CBRETRYCNT times while the CBRecall replies 5325 * NFSERR_BADSTATEID or NFSERR_BADHANDLE. This is done 5326 * in order to try and avoid a race that could happen 5327 * when a CBRecall request passed the Open reply with 5328 * the delegation in it when transitting the network. 5329 * Since nfsrv_docallback will sleep, don't use stp after 5330 * the call. 5331 */ 5332 NFSBCOPY((caddr_t)&stp->ls_stateid, (caddr_t)&tstateid, 5333 sizeof (tstateid)); 5334 NFSBCOPY((caddr_t)&stp->ls_lfp->lf_fh, (caddr_t)&tfh, 5335 sizeof (tfh)); 5336 NFSUNLOCKSTATE(); 5337 if (*haslockp) { 5338 *haslockp = 0; 5339 NFSLOCKV4ROOTMUTEX(); 5340 nfsv4_unlock(&nfsv4rootfs_lock, 1); 5341 NFSUNLOCKV4ROOTMUTEX(); 5342 } 5343 retrycnt = 0; 5344 do { 5345 error = nfsrv_docallback(clp, NFSV4OP_CBRECALL, 5346 &tstateid, 0, &tfh, NULL, NULL, 0, p); 5347 retrycnt++; 5348 } while ((error == NFSERR_BADSTATEID || 5349 error == NFSERR_BADHANDLE) && retrycnt < NFSV4_CBRETRYCNT); 5350 error = NFSERR_DELAY; 5351 goto out; 5352 } 5353 5354 if (clp->lc_expiry >= NFSD_MONOSEC && 5355 stp->ls_delegtime >= NFSD_MONOSEC) { 5356 NFSUNLOCKSTATE(); 5357 /* 5358 * A recall has been done, but it has not yet expired. 5359 * So, RETURN_DELAY. 5360 */ 5361 if (*haslockp) { 5362 *haslockp = 0; 5363 NFSLOCKV4ROOTMUTEX(); 5364 nfsv4_unlock(&nfsv4rootfs_lock, 1); 5365 NFSUNLOCKV4ROOTMUTEX(); 5366 } 5367 error = NFSERR_DELAY; 5368 goto out; 5369 } 5370 5371 /* 5372 * If we don't yet have the lock, just get it and then return, 5373 * since we need that before deleting expired state, such as 5374 * this delegation. 5375 * When getting the lock, unlock the vnode, so other nfsds that 5376 * are in progress, won't get stuck waiting for the vnode lock. 5377 */ 5378 if (*haslockp == 0) { 5379 NFSUNLOCKSTATE(); 5380 if (vp != NULL) { 5381 lktype = NFSVOPISLOCKED(vp); 5382 NFSVOPUNLOCK(vp); 5383 } 5384 NFSLOCKV4ROOTMUTEX(); 5385 nfsv4_relref(&nfsv4rootfs_lock); 5386 do { 5387 gotlock = nfsv4_lock(&nfsv4rootfs_lock, 1, NULL, 5388 NFSV4ROOTLOCKMUTEXPTR, NULL); 5389 } while (!gotlock); 5390 NFSUNLOCKV4ROOTMUTEX(); 5391 *haslockp = 1; 5392 if (vp != NULL) { 5393 NFSVOPLOCK(vp, lktype | LK_RETRY); 5394 if (VN_IS_DOOMED(vp)) { 5395 *haslockp = 0; 5396 NFSLOCKV4ROOTMUTEX(); 5397 nfsv4_unlock(&nfsv4rootfs_lock, 1); 5398 NFSUNLOCKV4ROOTMUTEX(); 5399 error = NFSERR_PERM; 5400 goto out; 5401 } 5402 } 5403 error = -1; 5404 goto out; 5405 } 5406 5407 NFSUNLOCKSTATE(); 5408 /* 5409 * Ok, we can delete the expired delegation. 5410 * First, write the Revoke record to stable storage and then 5411 * clear out the conflict. 5412 * Since all other nfsd threads are now blocked, we can safely 5413 * sleep without the state changing. 5414 */ 5415 nfsrv_writestable(clp->lc_id, clp->lc_idlen, NFSNST_REVOKE, p); 5416 nfsrv_backupstable(); 5417 if (clp->lc_expiry < NFSD_MONOSEC) { 5418 nfsrv_cleanclient(clp, p); 5419 nfsrv_freedeleglist(&clp->lc_deleg); 5420 nfsrv_freedeleglist(&clp->lc_olddeleg); 5421 LIST_REMOVE(clp, lc_hash); 5422 zapped_clp = 1; 5423 } else { 5424 nfsrv_freedeleg(stp); 5425 zapped_clp = 0; 5426 } 5427 if (zapped_clp) 5428 nfsrv_zapclient(clp, p); 5429 error = -1; 5430 5431 out: 5432 NFSEXITCODE(error); 5433 return (error); 5434 } 5435 5436 /* 5437 * Check for a remove allowed, if remove is set to 1 and get rid of 5438 * delegations. 5439 */ 5440 int 5441 nfsrv_checkremove(vnode_t vp, int remove, struct nfsrv_descript *nd, 5442 nfsquad_t clientid, NFSPROC_T *p) 5443 { 5444 struct nfsclient *clp; 5445 struct nfsstate *stp; 5446 struct nfslockfile *lfp; 5447 int error, haslock = 0; 5448 fhandle_t nfh; 5449 5450 clp = NULL; 5451 /* 5452 * First, get the lock file structure. 5453 * (A return of -1 means no associated state, so remove ok.) 5454 */ 5455 error = nfsrv_getlockfh(vp, NFSLCK_CHECK, NULL, &nfh, p); 5456 tryagain: 5457 NFSLOCKSTATE(); 5458 if (error == 0 && clientid.qval != 0) 5459 error = nfsrv_getclient(clientid, CLOPS_RENEW, &clp, NULL, 5460 (nfsquad_t)((u_quad_t)0), 0, nd, p); 5461 if (!error) 5462 error = nfsrv_getlockfile(NFSLCK_CHECK, NULL, &lfp, &nfh, 0); 5463 if (error) { 5464 NFSUNLOCKSTATE(); 5465 if (haslock) { 5466 NFSLOCKV4ROOTMUTEX(); 5467 nfsv4_unlock(&nfsv4rootfs_lock, 1); 5468 NFSUNLOCKV4ROOTMUTEX(); 5469 } 5470 if (error == -1) 5471 error = 0; 5472 goto out; 5473 } 5474 5475 /* 5476 * Now, we must Recall any delegations. 5477 */ 5478 error = nfsrv_cleandeleg(vp, lfp, clp, &haslock, p); 5479 if (error) { 5480 /* 5481 * nfsrv_cleandeleg() unlocks state for non-zero 5482 * return. 5483 */ 5484 if (error == -1) 5485 goto tryagain; 5486 if (haslock) { 5487 NFSLOCKV4ROOTMUTEX(); 5488 nfsv4_unlock(&nfsv4rootfs_lock, 1); 5489 NFSUNLOCKV4ROOTMUTEX(); 5490 } 5491 goto out; 5492 } 5493 5494 /* 5495 * Now, look for a conflicting open share. 5496 */ 5497 if (remove) { 5498 /* 5499 * If the entry in the directory was the last reference to the 5500 * corresponding filesystem object, the object can be destroyed 5501 * */ 5502 if(lfp->lf_usecount>1) 5503 LIST_FOREACH(stp, &lfp->lf_open, ls_file) { 5504 if (stp->ls_flags & NFSLCK_WRITEDENY) { 5505 error = NFSERR_FILEOPEN; 5506 break; 5507 } 5508 } 5509 } 5510 5511 NFSUNLOCKSTATE(); 5512 if (haslock) { 5513 NFSLOCKV4ROOTMUTEX(); 5514 nfsv4_unlock(&nfsv4rootfs_lock, 1); 5515 NFSUNLOCKV4ROOTMUTEX(); 5516 } 5517 5518 out: 5519 NFSEXITCODE(error); 5520 return (error); 5521 } 5522 5523 /* 5524 * Clear out all delegations for the file referred to by lfp. 5525 * May return NFSERR_DELAY, if there will be a delay waiting for 5526 * delegations to expire. 5527 * Returns -1 to indicate it slept while recalling a delegation. 5528 * This function has the side effect of deleting the nfslockfile structure, 5529 * if it no longer has associated state and didn't have to sleep. 5530 * Unlocks State before a non-zero value is returned. 5531 */ 5532 static int 5533 nfsrv_cleandeleg(vnode_t vp, struct nfslockfile *lfp, 5534 struct nfsclient *clp, int *haslockp, NFSPROC_T *p) 5535 { 5536 struct nfsstate *stp, *nstp; 5537 int ret = 0; 5538 5539 stp = LIST_FIRST(&lfp->lf_deleg); 5540 while (stp != LIST_END(&lfp->lf_deleg)) { 5541 nstp = LIST_NEXT(stp, ls_file); 5542 if (stp->ls_clp != clp) { 5543 ret = nfsrv_delegconflict(stp, haslockp, p, vp); 5544 if (ret) { 5545 /* 5546 * nfsrv_delegconflict() unlocks state 5547 * when it returns non-zero. 5548 */ 5549 goto out; 5550 } 5551 } 5552 stp = nstp; 5553 } 5554 out: 5555 NFSEXITCODE(ret); 5556 return (ret); 5557 } 5558 5559 /* 5560 * There are certain operations that, when being done outside of NFSv4, 5561 * require that any NFSv4 delegation for the file be recalled. 5562 * This function is to be called for those cases: 5563 * VOP_RENAME() - When a delegation is being recalled for any reason, 5564 * the client may have to do Opens against the server, using the file's 5565 * final component name. If the file has been renamed on the server, 5566 * that component name will be incorrect and the Open will fail. 5567 * VOP_REMOVE() - Theoretically, a client could Open a file after it has 5568 * been removed on the server, if there is a delegation issued to 5569 * that client for the file. I say "theoretically" since clients 5570 * normally do an Access Op before the Open and that Access Op will 5571 * fail with ESTALE. Note that NFSv2 and 3 don't even do Opens, so 5572 * they will detect the file's removal in the same manner. (There is 5573 * one case where RFC3530 allows a client to do an Open without first 5574 * doing an Access Op, which is passage of a check against the ACE 5575 * returned with a Write delegation, but current practice is to ignore 5576 * the ACE and always do an Access Op.) 5577 * Since the functions can only be called with an unlocked vnode, this 5578 * can't be done at this time. 5579 * VOP_ADVLOCK() - When a client holds a delegation, it can issue byte range 5580 * locks locally in the client, which are not visible to the server. To 5581 * deal with this, issuing of delegations for a vnode must be disabled 5582 * and all delegations for the vnode recalled. This is done via the 5583 * second function, using the VV_DISABLEDELEG vflag on the vnode. 5584 */ 5585 void 5586 nfsd_recalldelegation(vnode_t vp, NFSPROC_T *p) 5587 { 5588 time_t starttime; 5589 int error; 5590 5591 /* 5592 * First, check to see if the server is currently running and it has 5593 * been called for a regular file when issuing delegations. 5594 */ 5595 if (newnfs_numnfsd == 0 || vp->v_type != VREG || 5596 nfsrv_issuedelegs == 0) 5597 return; 5598 5599 KASSERT((NFSVOPISLOCKED(vp) != LK_EXCLUSIVE), ("vp %p is locked", vp)); 5600 /* 5601 * First, get a reference on the nfsv4rootfs_lock so that an 5602 * exclusive lock cannot be acquired by another thread. 5603 */ 5604 NFSLOCKV4ROOTMUTEX(); 5605 nfsv4_getref(&nfsv4rootfs_lock, NULL, NFSV4ROOTLOCKMUTEXPTR, NULL); 5606 NFSUNLOCKV4ROOTMUTEX(); 5607 5608 /* 5609 * Now, call nfsrv_checkremove() in a loop while it returns 5610 * NFSERR_DELAY. Return upon any other error or when timed out. 5611 */ 5612 starttime = NFSD_MONOSEC; 5613 do { 5614 if (NFSVOPLOCK(vp, LK_EXCLUSIVE) == 0) { 5615 error = nfsrv_checkremove(vp, 0, NULL, 5616 (nfsquad_t)((u_quad_t)0), p); 5617 NFSVOPUNLOCK(vp); 5618 } else 5619 error = EPERM; 5620 if (error == NFSERR_DELAY) { 5621 if (NFSD_MONOSEC - starttime > NFS_REMOVETIMEO) 5622 break; 5623 /* Sleep for a short period of time */ 5624 (void) nfs_catnap(PZERO, 0, "nfsremove"); 5625 } 5626 } while (error == NFSERR_DELAY); 5627 NFSLOCKV4ROOTMUTEX(); 5628 nfsv4_relref(&nfsv4rootfs_lock); 5629 NFSUNLOCKV4ROOTMUTEX(); 5630 } 5631 5632 void 5633 nfsd_disabledelegation(vnode_t vp, NFSPROC_T *p) 5634 { 5635 5636 #ifdef VV_DISABLEDELEG 5637 /* 5638 * First, flag issuance of delegations disabled. 5639 */ 5640 atomic_set_long(&vp->v_vflag, VV_DISABLEDELEG); 5641 #endif 5642 5643 /* 5644 * Then call nfsd_recalldelegation() to get rid of all extant 5645 * delegations. 5646 */ 5647 nfsd_recalldelegation(vp, p); 5648 } 5649 5650 /* 5651 * Check for conflicting locks, etc. and then get rid of delegations. 5652 * (At one point I thought that I should get rid of delegations for any 5653 * Setattr, since it could potentially disallow the I/O op (read or write) 5654 * allowed by the delegation. However, Setattr Ops that aren't changing 5655 * the size get a stateid of all 0s, so you can't tell if it is a delegation 5656 * for the same client or a different one, so I decided to only get rid 5657 * of delegations for other clients when the size is being changed.) 5658 * In general, a Setattr can disable NFS I/O Ops that are outstanding, such 5659 * as Write backs, even if there is no delegation, so it really isn't any 5660 * different?) 5661 */ 5662 int 5663 nfsrv_checksetattr(vnode_t vp, struct nfsrv_descript *nd, 5664 nfsv4stateid_t *stateidp, struct nfsvattr *nvap, nfsattrbit_t *attrbitp, 5665 struct nfsexstuff *exp, NFSPROC_T *p) 5666 { 5667 struct nfsstate st, *stp = &st; 5668 struct nfslock lo, *lop = &lo; 5669 int error = 0; 5670 nfsquad_t clientid; 5671 5672 if (NFSISSET_ATTRBIT(attrbitp, NFSATTRBIT_SIZE)) { 5673 stp->ls_flags = (NFSLCK_CHECK | NFSLCK_WRITEACCESS); 5674 lop->lo_first = nvap->na_size; 5675 } else { 5676 stp->ls_flags = 0; 5677 lop->lo_first = 0; 5678 } 5679 if (NFSISSET_ATTRBIT(attrbitp, NFSATTRBIT_OWNER) || 5680 NFSISSET_ATTRBIT(attrbitp, NFSATTRBIT_OWNERGROUP) || 5681 NFSISSET_ATTRBIT(attrbitp, NFSATTRBIT_MODE) || 5682 NFSISSET_ATTRBIT(attrbitp, NFSATTRBIT_ACL)) 5683 stp->ls_flags |= NFSLCK_SETATTR; 5684 if (stp->ls_flags == 0) 5685 goto out; 5686 lop->lo_end = NFS64BITSSET; 5687 lop->lo_flags = NFSLCK_WRITE; 5688 stp->ls_ownerlen = 0; 5689 stp->ls_op = NULL; 5690 stp->ls_uid = nd->nd_cred->cr_uid; 5691 stp->ls_stateid.seqid = stateidp->seqid; 5692 clientid.lval[0] = stp->ls_stateid.other[0] = stateidp->other[0]; 5693 clientid.lval[1] = stp->ls_stateid.other[1] = stateidp->other[1]; 5694 stp->ls_stateid.other[2] = stateidp->other[2]; 5695 error = nfsrv_lockctrl(vp, &stp, &lop, NULL, clientid, 5696 stateidp, exp, nd, p); 5697 5698 out: 5699 NFSEXITCODE2(error, nd); 5700 return (error); 5701 } 5702 5703 /* 5704 * Check for a write delegation and do a CBGETATTR if there is one, updating 5705 * the attributes, as required. 5706 * Should I return an error if I can't get the attributes? (For now, I'll 5707 * just return ok. 5708 */ 5709 int 5710 nfsrv_checkgetattr(struct nfsrv_descript *nd, vnode_t vp, 5711 struct nfsvattr *nvap, nfsattrbit_t *attrbitp, NFSPROC_T *p) 5712 { 5713 struct nfsstate *stp; 5714 struct nfslockfile *lfp; 5715 struct nfsclient *clp; 5716 struct nfsvattr nva; 5717 fhandle_t nfh; 5718 int error = 0; 5719 nfsattrbit_t cbbits; 5720 u_quad_t delegfilerev; 5721 5722 NFSCBGETATTR_ATTRBIT(attrbitp, &cbbits); 5723 if (!NFSNONZERO_ATTRBIT(&cbbits)) 5724 goto out; 5725 if (nfsrv_writedelegcnt == 0) 5726 goto out; 5727 5728 /* 5729 * Get the lock file structure. 5730 * (A return of -1 means no associated state, so return ok.) 5731 */ 5732 error = nfsrv_getlockfh(vp, NFSLCK_CHECK, NULL, &nfh, p); 5733 NFSLOCKSTATE(); 5734 if (!error) 5735 error = nfsrv_getlockfile(NFSLCK_CHECK, NULL, &lfp, &nfh, 0); 5736 if (error) { 5737 NFSUNLOCKSTATE(); 5738 if (error == -1) 5739 error = 0; 5740 goto out; 5741 } 5742 5743 /* 5744 * Now, look for a write delegation. 5745 */ 5746 LIST_FOREACH(stp, &lfp->lf_deleg, ls_file) { 5747 if (stp->ls_flags & NFSLCK_DELEGWRITE) 5748 break; 5749 } 5750 if (stp == LIST_END(&lfp->lf_deleg)) { 5751 NFSUNLOCKSTATE(); 5752 goto out; 5753 } 5754 clp = stp->ls_clp; 5755 5756 /* If the clientid is not confirmed, ignore the delegation. */ 5757 if (clp->lc_flags & LCL_NEEDSCONFIRM) { 5758 NFSUNLOCKSTATE(); 5759 goto out; 5760 } 5761 5762 delegfilerev = stp->ls_filerev; 5763 /* 5764 * If the Write delegation was issued as a part of this Compound RPC 5765 * or if we have an Implied Clientid (used in a previous Op in this 5766 * compound) and it is the client the delegation was issued to, 5767 * just return ok. 5768 * I also assume that it is from the same client iff the network 5769 * host IP address is the same as the callback address. (Not 5770 * exactly correct by the RFC, but avoids a lot of Getattr 5771 * callbacks.) 5772 */ 5773 if (nd->nd_compref == stp->ls_compref || 5774 ((nd->nd_flag & ND_IMPLIEDCLID) && 5775 clp->lc_clientid.qval == nd->nd_clientid.qval) || 5776 nfsaddr2_match(clp->lc_req.nr_nam, nd->nd_nam)) { 5777 NFSUNLOCKSTATE(); 5778 goto out; 5779 } 5780 5781 /* 5782 * We are now done with the delegation state structure, 5783 * so the statelock can be released and we can now tsleep(). 5784 */ 5785 5786 /* 5787 * Now, we must do the CB Getattr callback, to see if Change or Size 5788 * has changed. 5789 */ 5790 if (clp->lc_expiry >= NFSD_MONOSEC) { 5791 NFSUNLOCKSTATE(); 5792 NFSVNO_ATTRINIT(&nva); 5793 nva.na_filerev = NFS64BITSSET; 5794 error = nfsrv_docallback(clp, NFSV4OP_CBGETATTR, NULL, 5795 0, &nfh, &nva, &cbbits, 0, p); 5796 if (!error) { 5797 if ((nva.na_filerev != NFS64BITSSET && 5798 nva.na_filerev > delegfilerev) || 5799 (NFSVNO_ISSETSIZE(&nva) && 5800 nva.na_size != nvap->na_size)) { 5801 error = nfsvno_updfilerev(vp, nvap, nd, p); 5802 if (NFSVNO_ISSETSIZE(&nva)) 5803 nvap->na_size = nva.na_size; 5804 } 5805 } else 5806 error = 0; /* Ignore callback errors for now. */ 5807 } else { 5808 NFSUNLOCKSTATE(); 5809 } 5810 5811 out: 5812 NFSEXITCODE2(error, nd); 5813 return (error); 5814 } 5815 5816 /* 5817 * This function looks for openowners that haven't had any opens for 5818 * a while and throws them away. Called by an nfsd when NFSNSF_NOOPENS 5819 * is set. 5820 */ 5821 void 5822 nfsrv_throwawayopens(NFSPROC_T *p) 5823 { 5824 struct nfsclient *clp, *nclp; 5825 struct nfsstate *stp, *nstp; 5826 int i; 5827 5828 NFSLOCKSTATE(); 5829 nfsrv_stablefirst.nsf_flags &= ~NFSNSF_NOOPENS; 5830 /* 5831 * For each client... 5832 */ 5833 for (i = 0; i < nfsrv_clienthashsize; i++) { 5834 LIST_FOREACH_SAFE(clp, &nfsclienthash[i], lc_hash, nclp) { 5835 LIST_FOREACH_SAFE(stp, &clp->lc_open, ls_list, nstp) { 5836 if (LIST_EMPTY(&stp->ls_open) && 5837 (stp->ls_noopens > NFSNOOPEN || 5838 (nfsrv_openpluslock * 2) > 5839 nfsrv_v4statelimit)) 5840 nfsrv_freeopenowner(stp, 0, p); 5841 } 5842 } 5843 } 5844 NFSUNLOCKSTATE(); 5845 } 5846 5847 /* 5848 * This function checks to see if the credentials are the same. 5849 * Returns 1 for not same, 0 otherwise. 5850 */ 5851 static int 5852 nfsrv_notsamecredname(struct nfsrv_descript *nd, struct nfsclient *clp) 5853 { 5854 5855 if (nd->nd_flag & ND_GSS) { 5856 if (!(clp->lc_flags & LCL_GSS)) 5857 return (1); 5858 if (clp->lc_flags & LCL_NAME) { 5859 if (nd->nd_princlen != clp->lc_namelen || 5860 NFSBCMP(nd->nd_principal, clp->lc_name, 5861 clp->lc_namelen)) 5862 return (1); 5863 else 5864 return (0); 5865 } 5866 if (nd->nd_cred->cr_uid == clp->lc_uid) 5867 return (0); 5868 else 5869 return (1); 5870 } else if (clp->lc_flags & LCL_GSS) 5871 return (1); 5872 /* 5873 * For AUTH_SYS, allow the same uid or root. (This is underspecified 5874 * in RFC3530, which talks about principals, but doesn't say anything 5875 * about uids for AUTH_SYS.) 5876 */ 5877 if (nd->nd_cred->cr_uid == clp->lc_uid || nd->nd_cred->cr_uid == 0) 5878 return (0); 5879 else 5880 return (1); 5881 } 5882 5883 /* 5884 * Calculate the lease expiry time. 5885 */ 5886 static time_t 5887 nfsrv_leaseexpiry(void) 5888 { 5889 5890 if (nfsrv_stablefirst.nsf_eograce > NFSD_MONOSEC) 5891 return (NFSD_MONOSEC + 2 * (nfsrv_lease + NFSRV_LEASEDELTA)); 5892 return (NFSD_MONOSEC + nfsrv_lease + NFSRV_LEASEDELTA); 5893 } 5894 5895 /* 5896 * Delay the delegation timeout as far as ls_delegtimelimit, as required. 5897 */ 5898 static void 5899 nfsrv_delaydelegtimeout(struct nfsstate *stp) 5900 { 5901 5902 if ((stp->ls_flags & NFSLCK_DELEGRECALL) == 0) 5903 return; 5904 5905 if ((stp->ls_delegtime + 15) > NFSD_MONOSEC && 5906 stp->ls_delegtime < stp->ls_delegtimelimit) { 5907 stp->ls_delegtime += nfsrv_lease; 5908 if (stp->ls_delegtime > stp->ls_delegtimelimit) 5909 stp->ls_delegtime = stp->ls_delegtimelimit; 5910 } 5911 } 5912 5913 /* 5914 * This function checks to see if there is any other state associated 5915 * with the openowner for this Open. 5916 * It returns 1 if there is no other state, 0 otherwise. 5917 */ 5918 static int 5919 nfsrv_nootherstate(struct nfsstate *stp) 5920 { 5921 struct nfsstate *tstp; 5922 5923 LIST_FOREACH(tstp, &stp->ls_openowner->ls_open, ls_list) { 5924 if (tstp != stp || !LIST_EMPTY(&tstp->ls_lock)) 5925 return (0); 5926 } 5927 return (1); 5928 } 5929 5930 /* 5931 * Create a list of lock deltas (changes to local byte range locking 5932 * that can be rolled back using the list) and apply the changes via 5933 * nfsvno_advlock(). Optionally, lock the list. It is expected that either 5934 * the rollback or update function will be called after this. 5935 * It returns an error (and rolls back, as required), if any nfsvno_advlock() 5936 * call fails. If it returns an error, it will unlock the list. 5937 */ 5938 static int 5939 nfsrv_locallock(vnode_t vp, struct nfslockfile *lfp, int flags, 5940 uint64_t first, uint64_t end, struct nfslockconflict *cfp, NFSPROC_T *p) 5941 { 5942 struct nfslock *lop, *nlop; 5943 int error = 0; 5944 5945 /* Loop through the list of locks. */ 5946 lop = LIST_FIRST(&lfp->lf_locallock); 5947 while (first < end && lop != NULL) { 5948 nlop = LIST_NEXT(lop, lo_lckowner); 5949 if (first >= lop->lo_end) { 5950 /* not there yet */ 5951 lop = nlop; 5952 } else if (first < lop->lo_first) { 5953 /* new one starts before entry in list */ 5954 if (end <= lop->lo_first) { 5955 /* no overlap between old and new */ 5956 error = nfsrv_dolocal(vp, lfp, flags, 5957 NFSLCK_UNLOCK, first, end, cfp, p); 5958 if (error != 0) 5959 break; 5960 first = end; 5961 } else { 5962 /* handle fragment overlapped with new one */ 5963 error = nfsrv_dolocal(vp, lfp, flags, 5964 NFSLCK_UNLOCK, first, lop->lo_first, cfp, 5965 p); 5966 if (error != 0) 5967 break; 5968 first = lop->lo_first; 5969 } 5970 } else { 5971 /* new one overlaps this entry in list */ 5972 if (end <= lop->lo_end) { 5973 /* overlaps all of new one */ 5974 error = nfsrv_dolocal(vp, lfp, flags, 5975 lop->lo_flags, first, end, cfp, p); 5976 if (error != 0) 5977 break; 5978 first = end; 5979 } else { 5980 /* handle fragment overlapped with new one */ 5981 error = nfsrv_dolocal(vp, lfp, flags, 5982 lop->lo_flags, first, lop->lo_end, cfp, p); 5983 if (error != 0) 5984 break; 5985 first = lop->lo_end; 5986 lop = nlop; 5987 } 5988 } 5989 } 5990 if (first < end && error == 0) 5991 /* handle fragment past end of list */ 5992 error = nfsrv_dolocal(vp, lfp, flags, NFSLCK_UNLOCK, first, 5993 end, cfp, p); 5994 5995 NFSEXITCODE(error); 5996 return (error); 5997 } 5998 5999 /* 6000 * Local lock unlock. Unlock all byte ranges that are no longer locked 6001 * by NFSv4. To do this, unlock any subranges of first-->end that 6002 * do not overlap with the byte ranges of any lock in the lfp->lf_lock 6003 * list. This list has all locks for the file held by other 6004 * <clientid, lockowner> tuples. The list is ordered by increasing 6005 * lo_first value, but may have entries that overlap each other, for 6006 * the case of read locks. 6007 */ 6008 static void 6009 nfsrv_localunlock(vnode_t vp, struct nfslockfile *lfp, uint64_t init_first, 6010 uint64_t init_end, NFSPROC_T *p) 6011 { 6012 struct nfslock *lop; 6013 uint64_t first, end, prevfirst __unused; 6014 6015 first = init_first; 6016 end = init_end; 6017 while (first < init_end) { 6018 /* Loop through all nfs locks, adjusting first and end */ 6019 prevfirst = 0; 6020 LIST_FOREACH(lop, &lfp->lf_lock, lo_lckfile) { 6021 KASSERT(prevfirst <= lop->lo_first, 6022 ("nfsv4 locks out of order")); 6023 KASSERT(lop->lo_first < lop->lo_end, 6024 ("nfsv4 bogus lock")); 6025 prevfirst = lop->lo_first; 6026 if (first >= lop->lo_first && 6027 first < lop->lo_end) 6028 /* 6029 * Overlaps with initial part, so trim 6030 * off that initial part by moving first past 6031 * it. 6032 */ 6033 first = lop->lo_end; 6034 else if (end > lop->lo_first && 6035 lop->lo_first > first) { 6036 /* 6037 * This lock defines the end of the 6038 * segment to unlock, so set end to the 6039 * start of it and break out of the loop. 6040 */ 6041 end = lop->lo_first; 6042 break; 6043 } 6044 if (first >= end) 6045 /* 6046 * There is no segment left to do, so 6047 * break out of this loop and then exit 6048 * the outer while() since first will be set 6049 * to end, which must equal init_end here. 6050 */ 6051 break; 6052 } 6053 if (first < end) { 6054 /* Unlock this segment */ 6055 (void) nfsrv_dolocal(vp, lfp, NFSLCK_UNLOCK, 6056 NFSLCK_READ, first, end, NULL, p); 6057 nfsrv_locallock_commit(lfp, NFSLCK_UNLOCK, 6058 first, end); 6059 } 6060 /* 6061 * Now move past this segment and look for any further 6062 * segment in the range, if there is one. 6063 */ 6064 first = end; 6065 end = init_end; 6066 } 6067 } 6068 6069 /* 6070 * Do the local lock operation and update the rollback list, as required. 6071 * Perform the rollback and return the error if nfsvno_advlock() fails. 6072 */ 6073 static int 6074 nfsrv_dolocal(vnode_t vp, struct nfslockfile *lfp, int flags, int oldflags, 6075 uint64_t first, uint64_t end, struct nfslockconflict *cfp, NFSPROC_T *p) 6076 { 6077 struct nfsrollback *rlp; 6078 int error = 0, ltype, oldltype; 6079 6080 if (flags & NFSLCK_WRITE) 6081 ltype = F_WRLCK; 6082 else if (flags & NFSLCK_READ) 6083 ltype = F_RDLCK; 6084 else 6085 ltype = F_UNLCK; 6086 if (oldflags & NFSLCK_WRITE) 6087 oldltype = F_WRLCK; 6088 else if (oldflags & NFSLCK_READ) 6089 oldltype = F_RDLCK; 6090 else 6091 oldltype = F_UNLCK; 6092 if (ltype == oldltype || (oldltype == F_WRLCK && ltype == F_RDLCK)) 6093 /* nothing to do */ 6094 goto out; 6095 error = nfsvno_advlock(vp, ltype, first, end, p); 6096 if (error != 0) { 6097 if (cfp != NULL) { 6098 cfp->cl_clientid.lval[0] = 0; 6099 cfp->cl_clientid.lval[1] = 0; 6100 cfp->cl_first = 0; 6101 cfp->cl_end = NFS64BITSSET; 6102 cfp->cl_flags = NFSLCK_WRITE; 6103 cfp->cl_ownerlen = 5; 6104 NFSBCOPY("LOCAL", cfp->cl_owner, 5); 6105 } 6106 nfsrv_locallock_rollback(vp, lfp, p); 6107 } else if (ltype != F_UNLCK) { 6108 rlp = malloc(sizeof (struct nfsrollback), M_NFSDROLLBACK, 6109 M_WAITOK); 6110 rlp->rlck_first = first; 6111 rlp->rlck_end = end; 6112 rlp->rlck_type = oldltype; 6113 LIST_INSERT_HEAD(&lfp->lf_rollback, rlp, rlck_list); 6114 } 6115 6116 out: 6117 NFSEXITCODE(error); 6118 return (error); 6119 } 6120 6121 /* 6122 * Roll back local lock changes and free up the rollback list. 6123 */ 6124 static void 6125 nfsrv_locallock_rollback(vnode_t vp, struct nfslockfile *lfp, NFSPROC_T *p) 6126 { 6127 struct nfsrollback *rlp, *nrlp; 6128 6129 LIST_FOREACH_SAFE(rlp, &lfp->lf_rollback, rlck_list, nrlp) { 6130 (void) nfsvno_advlock(vp, rlp->rlck_type, rlp->rlck_first, 6131 rlp->rlck_end, p); 6132 free(rlp, M_NFSDROLLBACK); 6133 } 6134 LIST_INIT(&lfp->lf_rollback); 6135 } 6136 6137 /* 6138 * Update local lock list and delete rollback list (ie now committed to the 6139 * local locks). Most of the work is done by the internal function. 6140 */ 6141 static void 6142 nfsrv_locallock_commit(struct nfslockfile *lfp, int flags, uint64_t first, 6143 uint64_t end) 6144 { 6145 struct nfsrollback *rlp, *nrlp; 6146 struct nfslock *new_lop, *other_lop; 6147 6148 new_lop = malloc(sizeof (struct nfslock), M_NFSDLOCK, M_WAITOK); 6149 if (flags & (NFSLCK_READ | NFSLCK_WRITE)) 6150 other_lop = malloc(sizeof (struct nfslock), M_NFSDLOCK, 6151 M_WAITOK); 6152 else 6153 other_lop = NULL; 6154 new_lop->lo_flags = flags; 6155 new_lop->lo_first = first; 6156 new_lop->lo_end = end; 6157 nfsrv_updatelock(NULL, &new_lop, &other_lop, lfp); 6158 if (new_lop != NULL) 6159 free(new_lop, M_NFSDLOCK); 6160 if (other_lop != NULL) 6161 free(other_lop, M_NFSDLOCK); 6162 6163 /* and get rid of the rollback list */ 6164 LIST_FOREACH_SAFE(rlp, &lfp->lf_rollback, rlck_list, nrlp) 6165 free(rlp, M_NFSDROLLBACK); 6166 LIST_INIT(&lfp->lf_rollback); 6167 } 6168 6169 /* 6170 * Lock the struct nfslockfile for local lock updating. 6171 */ 6172 static void 6173 nfsrv_locklf(struct nfslockfile *lfp) 6174 { 6175 int gotlock; 6176 6177 /* lf_usecount ensures *lfp won't be free'd */ 6178 lfp->lf_usecount++; 6179 do { 6180 gotlock = nfsv4_lock(&lfp->lf_locallock_lck, 1, NULL, 6181 NFSSTATEMUTEXPTR, NULL); 6182 } while (gotlock == 0); 6183 lfp->lf_usecount--; 6184 } 6185 6186 /* 6187 * Unlock the struct nfslockfile after local lock updating. 6188 */ 6189 static void 6190 nfsrv_unlocklf(struct nfslockfile *lfp) 6191 { 6192 6193 nfsv4_unlock(&lfp->lf_locallock_lck, 0); 6194 } 6195 6196 /* 6197 * Clear out all state for the NFSv4 server. 6198 * Must be called by a thread that can sleep when no nfsds are running. 6199 */ 6200 void 6201 nfsrv_throwawayallstate(NFSPROC_T *p) 6202 { 6203 struct nfsclient *clp, *nclp; 6204 struct nfslockfile *lfp, *nlfp; 6205 int i; 6206 6207 /* 6208 * For each client, clean out the state and then free the structure. 6209 */ 6210 for (i = 0; i < nfsrv_clienthashsize; i++) { 6211 LIST_FOREACH_SAFE(clp, &nfsclienthash[i], lc_hash, nclp) { 6212 nfsrv_cleanclient(clp, p); 6213 nfsrv_freedeleglist(&clp->lc_deleg); 6214 nfsrv_freedeleglist(&clp->lc_olddeleg); 6215 free(clp->lc_stateid, M_NFSDCLIENT); 6216 free(clp, M_NFSDCLIENT); 6217 } 6218 } 6219 6220 /* 6221 * Also, free up any remaining lock file structures. 6222 */ 6223 for (i = 0; i < nfsrv_lockhashsize; i++) { 6224 LIST_FOREACH_SAFE(lfp, &nfslockhash[i], lf_hash, nlfp) { 6225 printf("nfsd unload: fnd a lock file struct\n"); 6226 nfsrv_freenfslockfile(lfp); 6227 } 6228 } 6229 6230 /* And get rid of the deviceid structures and layouts. */ 6231 nfsrv_freealllayoutsanddevids(); 6232 } 6233 6234 /* 6235 * Check the sequence# for the session and slot provided as an argument. 6236 * Also, renew the lease if the session will return NFS_OK. 6237 */ 6238 int 6239 nfsrv_checksequence(struct nfsrv_descript *nd, uint32_t sequenceid, 6240 uint32_t *highest_slotidp, uint32_t *target_highest_slotidp, int cache_this, 6241 uint32_t *sflagsp, NFSPROC_T *p) 6242 { 6243 struct nfsdsession *sep; 6244 struct nfssessionhash *shp; 6245 int error; 6246 6247 shp = NFSSESSIONHASH(nd->nd_sessionid); 6248 NFSLOCKSESSION(shp); 6249 sep = nfsrv_findsession(nd->nd_sessionid); 6250 if (sep == NULL) { 6251 NFSUNLOCKSESSION(shp); 6252 return (NFSERR_BADSESSION); 6253 } 6254 error = nfsv4_seqsession(sequenceid, nd->nd_slotid, *highest_slotidp, 6255 sep->sess_slots, NULL, NFSV4_SLOTS - 1); 6256 if (error != 0) { 6257 NFSUNLOCKSESSION(shp); 6258 return (error); 6259 } 6260 if (cache_this != 0) 6261 nd->nd_flag |= ND_SAVEREPLY; 6262 /* Renew the lease. */ 6263 sep->sess_clp->lc_expiry = nfsrv_leaseexpiry(); 6264 nd->nd_clientid.qval = sep->sess_clp->lc_clientid.qval; 6265 nd->nd_flag |= ND_IMPLIEDCLID; 6266 6267 /* Save maximum request and reply sizes. */ 6268 nd->nd_maxreq = sep->sess_maxreq; 6269 nd->nd_maxresp = sep->sess_maxresp; 6270 6271 *sflagsp = 0; 6272 if (sep->sess_clp->lc_req.nr_client == NULL || 6273 (sep->sess_clp->lc_flags & LCL_CBDOWN) != 0) 6274 *sflagsp |= NFSV4SEQ_CBPATHDOWN; 6275 NFSUNLOCKSESSION(shp); 6276 if (error == NFSERR_EXPIRED) { 6277 *sflagsp |= NFSV4SEQ_EXPIREDALLSTATEREVOKED; 6278 error = 0; 6279 } else if (error == NFSERR_ADMINREVOKED) { 6280 *sflagsp |= NFSV4SEQ_ADMINSTATEREVOKED; 6281 error = 0; 6282 } 6283 *highest_slotidp = *target_highest_slotidp = NFSV4_SLOTS - 1; 6284 return (0); 6285 } 6286 6287 /* 6288 * Check/set reclaim complete for this session/clientid. 6289 */ 6290 int 6291 nfsrv_checkreclaimcomplete(struct nfsrv_descript *nd, int onefs) 6292 { 6293 struct nfsdsession *sep; 6294 struct nfssessionhash *shp; 6295 int error = 0; 6296 6297 shp = NFSSESSIONHASH(nd->nd_sessionid); 6298 NFSLOCKSTATE(); 6299 NFSLOCKSESSION(shp); 6300 sep = nfsrv_findsession(nd->nd_sessionid); 6301 if (sep == NULL) { 6302 NFSUNLOCKSESSION(shp); 6303 NFSUNLOCKSTATE(); 6304 return (NFSERR_BADSESSION); 6305 } 6306 6307 if (onefs != 0) 6308 sep->sess_clp->lc_flags |= LCL_RECLAIMONEFS; 6309 /* Check to see if reclaim complete has already happened. */ 6310 else if ((sep->sess_clp->lc_flags & LCL_RECLAIMCOMPLETE) != 0) 6311 error = NFSERR_COMPLETEALREADY; 6312 else { 6313 sep->sess_clp->lc_flags |= LCL_RECLAIMCOMPLETE; 6314 nfsrv_markreclaim(sep->sess_clp); 6315 } 6316 NFSUNLOCKSESSION(shp); 6317 NFSUNLOCKSTATE(); 6318 return (error); 6319 } 6320 6321 /* 6322 * Cache the reply in a session slot. 6323 */ 6324 void 6325 nfsrv_cache_session(struct nfsrv_descript *nd, struct mbuf **m) 6326 { 6327 struct nfsdsession *sep; 6328 struct nfssessionhash *shp; 6329 char *buf, *cp; 6330 #ifdef INET 6331 struct sockaddr_in *sin; 6332 #endif 6333 #ifdef INET6 6334 struct sockaddr_in6 *sin6; 6335 #endif 6336 6337 shp = NFSSESSIONHASH(nd->nd_sessionid); 6338 NFSLOCKSESSION(shp); 6339 sep = nfsrv_findsession(nd->nd_sessionid); 6340 if (sep == NULL) { 6341 NFSUNLOCKSESSION(shp); 6342 if ((nfsrv_stablefirst.nsf_flags & NFSNSF_GRACEOVER) != 0) { 6343 buf = malloc(INET6_ADDRSTRLEN, M_TEMP, M_WAITOK); 6344 switch (nd->nd_nam->sa_family) { 6345 #ifdef INET 6346 case AF_INET: 6347 sin = (struct sockaddr_in *)nd->nd_nam; 6348 cp = inet_ntop(sin->sin_family, 6349 &sin->sin_addr.s_addr, buf, 6350 INET6_ADDRSTRLEN); 6351 break; 6352 #endif 6353 #ifdef INET6 6354 case AF_INET6: 6355 sin6 = (struct sockaddr_in6 *)nd->nd_nam; 6356 cp = inet_ntop(sin6->sin6_family, 6357 &sin6->sin6_addr, buf, INET6_ADDRSTRLEN); 6358 break; 6359 #endif 6360 default: 6361 cp = NULL; 6362 } 6363 if (cp != NULL) 6364 printf("nfsrv_cache_session: no session " 6365 "IPaddr=%s\n", cp); 6366 else 6367 printf("nfsrv_cache_session: no session\n"); 6368 free(buf, M_TEMP); 6369 } 6370 m_freem(*m); 6371 return; 6372 } 6373 nfsv4_seqsess_cacherep(nd->nd_slotid, sep->sess_slots, nd->nd_repstat, 6374 m); 6375 NFSUNLOCKSESSION(shp); 6376 } 6377 6378 /* 6379 * Search for a session that matches the sessionid. 6380 */ 6381 static struct nfsdsession * 6382 nfsrv_findsession(uint8_t *sessionid) 6383 { 6384 struct nfsdsession *sep; 6385 struct nfssessionhash *shp; 6386 6387 shp = NFSSESSIONHASH(sessionid); 6388 LIST_FOREACH(sep, &shp->list, sess_hash) { 6389 if (!NFSBCMP(sessionid, sep->sess_sessionid, NFSX_V4SESSIONID)) 6390 break; 6391 } 6392 return (sep); 6393 } 6394 6395 /* 6396 * Destroy a session. 6397 */ 6398 int 6399 nfsrv_destroysession(struct nfsrv_descript *nd, uint8_t *sessionid) 6400 { 6401 int error, igotlock, samesess; 6402 6403 samesess = 0; 6404 if (!NFSBCMP(sessionid, nd->nd_sessionid, NFSX_V4SESSIONID) && 6405 (nd->nd_flag & ND_HASSEQUENCE) != 0) { 6406 samesess = 1; 6407 if ((nd->nd_flag & ND_LASTOP) == 0) 6408 return (NFSERR_BADSESSION); 6409 } 6410 6411 /* Lock out other nfsd threads */ 6412 NFSLOCKV4ROOTMUTEX(); 6413 nfsv4_relref(&nfsv4rootfs_lock); 6414 do { 6415 igotlock = nfsv4_lock(&nfsv4rootfs_lock, 1, NULL, 6416 NFSV4ROOTLOCKMUTEXPTR, NULL); 6417 } while (igotlock == 0); 6418 NFSUNLOCKV4ROOTMUTEX(); 6419 6420 error = nfsrv_freesession(NULL, sessionid); 6421 if (error == 0 && samesess != 0) 6422 nd->nd_flag &= ~ND_HASSEQUENCE; 6423 6424 NFSLOCKV4ROOTMUTEX(); 6425 nfsv4_unlock(&nfsv4rootfs_lock, 1); 6426 NFSUNLOCKV4ROOTMUTEX(); 6427 return (error); 6428 } 6429 6430 /* 6431 * Bind a connection to a session. 6432 * For now, only certain variants are supported, since the current session 6433 * structure can only handle a single backchannel entry, which will be 6434 * applied to all connections if it is set. 6435 */ 6436 int 6437 nfsrv_bindconnsess(struct nfsrv_descript *nd, uint8_t *sessionid, int *foreaftp) 6438 { 6439 struct nfssessionhash *shp; 6440 struct nfsdsession *sep; 6441 struct nfsclient *clp; 6442 SVCXPRT *savxprt; 6443 int error; 6444 6445 error = 0; 6446 savxprt = NULL; 6447 shp = NFSSESSIONHASH(sessionid); 6448 NFSLOCKSTATE(); 6449 NFSLOCKSESSION(shp); 6450 sep = nfsrv_findsession(sessionid); 6451 if (sep != NULL) { 6452 clp = sep->sess_clp; 6453 if (*foreaftp == NFSCDFC4_BACK || 6454 *foreaftp == NFSCDFC4_BACK_OR_BOTH || 6455 *foreaftp == NFSCDFC4_FORE_OR_BOTH) { 6456 /* Try to set up a backchannel. */ 6457 if (clp->lc_req.nr_client == NULL) { 6458 NFSD_DEBUG(2, "nfsrv_bindconnsess: acquire " 6459 "backchannel\n"); 6460 clp->lc_req.nr_client = (struct __rpc_client *) 6461 clnt_bck_create(nd->nd_xprt->xp_socket, 6462 sep->sess_cbprogram, NFSV4_CBVERS); 6463 } 6464 if (clp->lc_req.nr_client != NULL) { 6465 NFSD_DEBUG(2, "nfsrv_bindconnsess: set up " 6466 "backchannel\n"); 6467 savxprt = sep->sess_cbsess.nfsess_xprt; 6468 SVC_ACQUIRE(nd->nd_xprt); 6469 CLNT_ACQUIRE(clp->lc_req.nr_client); 6470 nd->nd_xprt->xp_p2 = clp->lc_req.nr_client; 6471 /* Disable idle timeout. */ 6472 nd->nd_xprt->xp_idletimeout = 0; 6473 sep->sess_cbsess.nfsess_xprt = nd->nd_xprt; 6474 sep->sess_crflags |= NFSV4CRSESS_CONNBACKCHAN; 6475 clp->lc_flags |= LCL_DONEBINDCONN | 6476 LCL_NEEDSCBNULL; 6477 clp->lc_flags &= ~LCL_CBDOWN; 6478 if (*foreaftp == NFSCDFS4_BACK) 6479 *foreaftp = NFSCDFS4_BACK; 6480 else 6481 *foreaftp = NFSCDFS4_BOTH; 6482 } else if (*foreaftp != NFSCDFC4_BACK) { 6483 NFSD_DEBUG(2, "nfsrv_bindconnsess: can't set " 6484 "up backchannel\n"); 6485 sep->sess_crflags &= ~NFSV4CRSESS_CONNBACKCHAN; 6486 clp->lc_flags |= LCL_DONEBINDCONN; 6487 *foreaftp = NFSCDFS4_FORE; 6488 } else { 6489 error = NFSERR_NOTSUPP; 6490 printf("nfsrv_bindconnsess: Can't add " 6491 "backchannel\n"); 6492 } 6493 } else { 6494 NFSD_DEBUG(2, "nfsrv_bindconnsess: Set forechannel\n"); 6495 clp->lc_flags |= LCL_DONEBINDCONN; 6496 *foreaftp = NFSCDFS4_FORE; 6497 } 6498 } else 6499 error = NFSERR_BADSESSION; 6500 NFSUNLOCKSESSION(shp); 6501 NFSUNLOCKSTATE(); 6502 if (savxprt != NULL) 6503 SVC_RELEASE(savxprt); 6504 return (error); 6505 } 6506 6507 /* 6508 * Free up a session structure. 6509 */ 6510 static int 6511 nfsrv_freesession(struct nfsdsession *sep, uint8_t *sessionid) 6512 { 6513 struct nfssessionhash *shp; 6514 int i; 6515 6516 NFSLOCKSTATE(); 6517 if (sep == NULL) { 6518 shp = NFSSESSIONHASH(sessionid); 6519 NFSLOCKSESSION(shp); 6520 sep = nfsrv_findsession(sessionid); 6521 } else { 6522 shp = NFSSESSIONHASH(sep->sess_sessionid); 6523 NFSLOCKSESSION(shp); 6524 } 6525 if (sep != NULL) { 6526 sep->sess_refcnt--; 6527 if (sep->sess_refcnt > 0) { 6528 NFSUNLOCKSESSION(shp); 6529 NFSUNLOCKSTATE(); 6530 return (NFSERR_BACKCHANBUSY); 6531 } 6532 LIST_REMOVE(sep, sess_hash); 6533 LIST_REMOVE(sep, sess_list); 6534 } 6535 NFSUNLOCKSESSION(shp); 6536 NFSUNLOCKSTATE(); 6537 if (sep == NULL) 6538 return (NFSERR_BADSESSION); 6539 for (i = 0; i < NFSV4_SLOTS; i++) 6540 if (sep->sess_slots[i].nfssl_reply != NULL) 6541 m_freem(sep->sess_slots[i].nfssl_reply); 6542 if (sep->sess_cbsess.nfsess_xprt != NULL) 6543 SVC_RELEASE(sep->sess_cbsess.nfsess_xprt); 6544 free(sep, M_NFSDSESSION); 6545 return (0); 6546 } 6547 6548 /* 6549 * Free a stateid. 6550 * RFC5661 says that it should fail when there are associated opens, locks 6551 * or delegations. Since stateids represent opens, I don't see how you can 6552 * free an open stateid (it will be free'd when closed), so this function 6553 * only works for lock stateids (freeing the lock_owner) or delegations. 6554 */ 6555 int 6556 nfsrv_freestateid(struct nfsrv_descript *nd, nfsv4stateid_t *stateidp, 6557 NFSPROC_T *p) 6558 { 6559 struct nfsclient *clp; 6560 struct nfsstate *stp; 6561 int error; 6562 6563 NFSLOCKSTATE(); 6564 /* 6565 * Look up the stateid 6566 */ 6567 error = nfsrv_getclient((nfsquad_t)((u_quad_t)0), CLOPS_RENEW, &clp, 6568 NULL, (nfsquad_t)((u_quad_t)0), 0, nd, p); 6569 if (error == 0) { 6570 /* First, check for a delegation. */ 6571 LIST_FOREACH(stp, &clp->lc_deleg, ls_list) { 6572 if (!NFSBCMP(stp->ls_stateid.other, stateidp->other, 6573 NFSX_STATEIDOTHER)) 6574 break; 6575 } 6576 if (stp != NULL) { 6577 nfsrv_freedeleg(stp); 6578 NFSUNLOCKSTATE(); 6579 return (error); 6580 } 6581 } 6582 /* Not a delegation, try for a lock_owner. */ 6583 if (error == 0) 6584 error = nfsrv_getstate(clp, stateidp, 0, &stp); 6585 if (error == 0 && ((stp->ls_flags & (NFSLCK_OPEN | NFSLCK_DELEGREAD | 6586 NFSLCK_DELEGWRITE)) != 0 || (stp->ls_flags & NFSLCK_LOCK) == 0)) 6587 /* Not a lock_owner stateid. */ 6588 error = NFSERR_LOCKSHELD; 6589 if (error == 0 && !LIST_EMPTY(&stp->ls_lock)) 6590 error = NFSERR_LOCKSHELD; 6591 if (error == 0) 6592 nfsrv_freelockowner(stp, NULL, 0, p); 6593 NFSUNLOCKSTATE(); 6594 return (error); 6595 } 6596 6597 /* 6598 * Test a stateid. 6599 */ 6600 int 6601 nfsrv_teststateid(struct nfsrv_descript *nd, nfsv4stateid_t *stateidp, 6602 NFSPROC_T *p) 6603 { 6604 struct nfsclient *clp; 6605 struct nfsstate *stp; 6606 int error; 6607 6608 NFSLOCKSTATE(); 6609 /* 6610 * Look up the stateid 6611 */ 6612 error = nfsrv_getclient((nfsquad_t)((u_quad_t)0), CLOPS_RENEW, &clp, 6613 NULL, (nfsquad_t)((u_quad_t)0), 0, nd, p); 6614 if (error == 0) 6615 error = nfsrv_getstate(clp, stateidp, 0, &stp); 6616 if (error == 0 && stateidp->seqid != 0 && 6617 SEQ_LT(stateidp->seqid, stp->ls_stateid.seqid)) 6618 error = NFSERR_OLDSTATEID; 6619 NFSUNLOCKSTATE(); 6620 return (error); 6621 } 6622 6623 /* 6624 * Generate the xdr for an NFSv4.1 CBSequence Operation. 6625 */ 6626 static int 6627 nfsv4_setcbsequence(struct nfsrv_descript *nd, struct nfsclient *clp, 6628 int dont_replycache, struct nfsdsession **sepp, int *slotposp) 6629 { 6630 struct nfsdsession *sep; 6631 uint32_t *tl, slotseq = 0; 6632 int maxslot; 6633 uint8_t sessionid[NFSX_V4SESSIONID]; 6634 int error; 6635 6636 error = nfsv4_getcbsession(clp, sepp); 6637 if (error != 0) 6638 return (error); 6639 sep = *sepp; 6640 (void)nfsv4_sequencelookup(NULL, &sep->sess_cbsess, slotposp, &maxslot, 6641 &slotseq, sessionid); 6642 KASSERT(maxslot >= 0, ("nfsv4_setcbsequence neg maxslot")); 6643 6644 /* Build the Sequence arguments. */ 6645 NFSM_BUILD(tl, uint32_t *, NFSX_V4SESSIONID + 5 * NFSX_UNSIGNED); 6646 bcopy(sessionid, tl, NFSX_V4SESSIONID); 6647 tl += NFSX_V4SESSIONID / NFSX_UNSIGNED; 6648 nd->nd_slotseq = tl; 6649 nd->nd_slotid = *slotposp; 6650 nd->nd_flag |= ND_HASSLOTID; 6651 *tl++ = txdr_unsigned(slotseq); 6652 *tl++ = txdr_unsigned(*slotposp); 6653 *tl++ = txdr_unsigned(maxslot); 6654 if (dont_replycache == 0) 6655 *tl++ = newnfs_true; 6656 else 6657 *tl++ = newnfs_false; 6658 *tl = 0; /* No referring call list, for now. */ 6659 nd->nd_flag |= ND_HASSEQUENCE; 6660 return (0); 6661 } 6662 6663 /* 6664 * Get a session for the callback. 6665 */ 6666 static int 6667 nfsv4_getcbsession(struct nfsclient *clp, struct nfsdsession **sepp) 6668 { 6669 struct nfsdsession *sep; 6670 6671 NFSLOCKSTATE(); 6672 LIST_FOREACH(sep, &clp->lc_session, sess_list) { 6673 if ((sep->sess_crflags & NFSV4CRSESS_CONNBACKCHAN) != 0) 6674 break; 6675 } 6676 if (sep == NULL) { 6677 NFSUNLOCKSTATE(); 6678 return (NFSERR_BADSESSION); 6679 } 6680 sep->sess_refcnt++; 6681 *sepp = sep; 6682 NFSUNLOCKSTATE(); 6683 return (0); 6684 } 6685 6686 /* 6687 * Free up all backchannel xprts. This needs to be done when the nfsd threads 6688 * exit, since those transports will all be going away. 6689 * This is only called after all the nfsd threads are done performing RPCs, 6690 * so locking shouldn't be an issue. 6691 */ 6692 void 6693 nfsrv_freeallbackchannel_xprts(void) 6694 { 6695 struct nfsdsession *sep; 6696 struct nfsclient *clp; 6697 SVCXPRT *xprt; 6698 int i; 6699 6700 for (i = 0; i < nfsrv_clienthashsize; i++) { 6701 LIST_FOREACH(clp, &nfsclienthash[i], lc_hash) { 6702 LIST_FOREACH(sep, &clp->lc_session, sess_list) { 6703 xprt = sep->sess_cbsess.nfsess_xprt; 6704 sep->sess_cbsess.nfsess_xprt = NULL; 6705 if (xprt != NULL) 6706 SVC_RELEASE(xprt); 6707 } 6708 } 6709 } 6710 } 6711 6712 /* 6713 * Do a layout commit. Actually just call nfsrv_updatemdsattr(). 6714 * I have no idea if the rest of these arguments will ever be useful? 6715 */ 6716 int 6717 nfsrv_layoutcommit(struct nfsrv_descript *nd, vnode_t vp, int layouttype, 6718 int hasnewoff, uint64_t newoff, uint64_t offset, uint64_t len, 6719 int hasnewmtime, struct timespec *newmtimep, int reclaim, 6720 nfsv4stateid_t *stateidp, int maxcnt, char *layp, int *hasnewsizep, 6721 uint64_t *newsizep, struct ucred *cred, NFSPROC_T *p) 6722 { 6723 struct nfsvattr na; 6724 int error; 6725 6726 error = nfsrv_updatemdsattr(vp, &na, p); 6727 if (error == 0) { 6728 *hasnewsizep = 1; 6729 *newsizep = na.na_size; 6730 } 6731 return (error); 6732 } 6733 6734 /* 6735 * Try and get a layout. 6736 */ 6737 int 6738 nfsrv_layoutget(struct nfsrv_descript *nd, vnode_t vp, struct nfsexstuff *exp, 6739 int layouttype, int *iomode, uint64_t *offset, uint64_t *len, 6740 uint64_t minlen, nfsv4stateid_t *stateidp, int maxcnt, int *retonclose, 6741 int *layoutlenp, char *layp, struct ucred *cred, NFSPROC_T *p) 6742 { 6743 struct nfslayouthash *lhyp; 6744 struct nfslayout *lyp; 6745 char *devid; 6746 fhandle_t fh, *dsfhp; 6747 int error, mirrorcnt; 6748 6749 if (nfsrv_devidcnt == 0) 6750 return (NFSERR_UNKNLAYOUTTYPE); 6751 6752 if (*offset != 0) 6753 printf("nfsrv_layoutget: off=%ju len=%ju\n", (uintmax_t)*offset, 6754 (uintmax_t)*len); 6755 error = nfsvno_getfh(vp, &fh, p); 6756 NFSD_DEBUG(4, "layoutget getfh=%d\n", error); 6757 if (error != 0) 6758 return (error); 6759 6760 /* 6761 * For now, all layouts are for entire files. 6762 * Only issue Read/Write layouts if requested for a non-readonly fs. 6763 */ 6764 if (NFSVNO_EXRDONLY(exp)) { 6765 if (*iomode == NFSLAYOUTIOMODE_RW) 6766 return (NFSERR_LAYOUTTRYLATER); 6767 *iomode = NFSLAYOUTIOMODE_READ; 6768 } 6769 if (*iomode != NFSLAYOUTIOMODE_RW) 6770 *iomode = NFSLAYOUTIOMODE_READ; 6771 6772 /* 6773 * Check to see if a write layout can be issued for this file. 6774 * This is used during mirror recovery to avoid RW layouts being 6775 * issued for a file while it is being copied to the recovered 6776 * mirror. 6777 */ 6778 if (*iomode == NFSLAYOUTIOMODE_RW && nfsrv_dontlayout(&fh) != 0) 6779 return (NFSERR_LAYOUTTRYLATER); 6780 6781 *retonclose = 0; 6782 *offset = 0; 6783 *len = UINT64_MAX; 6784 6785 /* First, see if a layout already exists and return if found. */ 6786 lhyp = NFSLAYOUTHASH(&fh); 6787 NFSLOCKLAYOUT(lhyp); 6788 error = nfsrv_findlayout(&nd->nd_clientid, &fh, layouttype, p, &lyp); 6789 NFSD_DEBUG(4, "layoutget findlay=%d\n", error); 6790 /* 6791 * Not sure if the seqid must be the same, so I won't check it. 6792 */ 6793 if (error == 0 && (stateidp->other[0] != lyp->lay_stateid.other[0] || 6794 stateidp->other[1] != lyp->lay_stateid.other[1] || 6795 stateidp->other[2] != lyp->lay_stateid.other[2])) { 6796 if ((lyp->lay_flags & NFSLAY_CALLB) == 0) { 6797 NFSUNLOCKLAYOUT(lhyp); 6798 NFSD_DEBUG(1, "ret bad stateid\n"); 6799 return (NFSERR_BADSTATEID); 6800 } 6801 /* 6802 * I believe we get here because there is a race between 6803 * the client processing the CBLAYOUTRECALL and the layout 6804 * being deleted here on the server. 6805 * The client has now done a LayoutGet with a non-layout 6806 * stateid, as it would when there is no layout. 6807 * As such, free this layout and set error == NFSERR_BADSTATEID 6808 * so the code below will create a new layout structure as 6809 * would happen if no layout was found. 6810 * "lyp" will be set before being used below, but set it NULL 6811 * as a safety belt. 6812 */ 6813 nfsrv_freelayout(&lhyp->list, lyp); 6814 lyp = NULL; 6815 error = NFSERR_BADSTATEID; 6816 } 6817 if (error == 0) { 6818 if (lyp->lay_layoutlen > maxcnt) { 6819 NFSUNLOCKLAYOUT(lhyp); 6820 NFSD_DEBUG(1, "ret layout too small\n"); 6821 return (NFSERR_TOOSMALL); 6822 } 6823 if (*iomode == NFSLAYOUTIOMODE_RW) { 6824 if ((lyp->lay_flags & NFSLAY_NOSPC) != 0) { 6825 NFSUNLOCKLAYOUT(lhyp); 6826 NFSD_DEBUG(1, "ret layout nospace\n"); 6827 return (NFSERR_NOSPC); 6828 } 6829 lyp->lay_flags |= NFSLAY_RW; 6830 } else 6831 lyp->lay_flags |= NFSLAY_READ; 6832 NFSBCOPY(lyp->lay_xdr, layp, lyp->lay_layoutlen); 6833 *layoutlenp = lyp->lay_layoutlen; 6834 if (++lyp->lay_stateid.seqid == 0) 6835 lyp->lay_stateid.seqid = 1; 6836 stateidp->seqid = lyp->lay_stateid.seqid; 6837 NFSUNLOCKLAYOUT(lhyp); 6838 NFSD_DEBUG(4, "ret fnd layout\n"); 6839 return (0); 6840 } 6841 NFSUNLOCKLAYOUT(lhyp); 6842 6843 /* Find the device id and file handle. */ 6844 dsfhp = malloc(sizeof(fhandle_t) * NFSDEV_MAXMIRRORS, M_TEMP, M_WAITOK); 6845 devid = malloc(NFSX_V4DEVICEID * NFSDEV_MAXMIRRORS, M_TEMP, M_WAITOK); 6846 error = nfsrv_dsgetdevandfh(vp, p, &mirrorcnt, dsfhp, devid); 6847 NFSD_DEBUG(4, "layoutget devandfh=%d\n", error); 6848 if (error == 0) { 6849 if (layouttype == NFSLAYOUT_NFSV4_1_FILES) { 6850 if (NFSX_V4FILELAYOUT > maxcnt) 6851 error = NFSERR_TOOSMALL; 6852 else 6853 lyp = nfsrv_filelayout(nd, *iomode, &fh, dsfhp, 6854 devid, vp->v_mount->mnt_stat.f_fsid); 6855 } else { 6856 if (NFSX_V4FLEXLAYOUT(mirrorcnt) > maxcnt) 6857 error = NFSERR_TOOSMALL; 6858 else 6859 lyp = nfsrv_flexlayout(nd, *iomode, mirrorcnt, 6860 &fh, dsfhp, devid, 6861 vp->v_mount->mnt_stat.f_fsid); 6862 } 6863 } 6864 free(dsfhp, M_TEMP); 6865 free(devid, M_TEMP); 6866 if (error != 0) 6867 return (error); 6868 6869 /* 6870 * Now, add this layout to the list. 6871 */ 6872 error = nfsrv_addlayout(nd, &lyp, stateidp, layp, layoutlenp, p); 6873 NFSD_DEBUG(4, "layoutget addl=%d\n", error); 6874 /* 6875 * The lyp will be set to NULL by nfsrv_addlayout() if it 6876 * linked the new structure into the lists. 6877 */ 6878 free(lyp, M_NFSDSTATE); 6879 return (error); 6880 } 6881 6882 /* 6883 * Generate a File Layout. 6884 */ 6885 static struct nfslayout * 6886 nfsrv_filelayout(struct nfsrv_descript *nd, int iomode, fhandle_t *fhp, 6887 fhandle_t *dsfhp, char *devid, fsid_t fs) 6888 { 6889 uint32_t *tl; 6890 struct nfslayout *lyp; 6891 uint64_t pattern_offset; 6892 6893 lyp = malloc(sizeof(struct nfslayout) + NFSX_V4FILELAYOUT, M_NFSDSTATE, 6894 M_WAITOK | M_ZERO); 6895 lyp->lay_type = NFSLAYOUT_NFSV4_1_FILES; 6896 if (iomode == NFSLAYOUTIOMODE_RW) 6897 lyp->lay_flags = NFSLAY_RW; 6898 else 6899 lyp->lay_flags = NFSLAY_READ; 6900 NFSBCOPY(fhp, &lyp->lay_fh, sizeof(*fhp)); 6901 lyp->lay_clientid.qval = nd->nd_clientid.qval; 6902 lyp->lay_fsid = fs; 6903 NFSBCOPY(devid, lyp->lay_deviceid, NFSX_V4DEVICEID); 6904 6905 /* Fill in the xdr for the files layout. */ 6906 tl = (uint32_t *)lyp->lay_xdr; 6907 NFSBCOPY(devid, tl, NFSX_V4DEVICEID); /* Device ID. */ 6908 tl += (NFSX_V4DEVICEID / NFSX_UNSIGNED); 6909 6910 /* Set the stripe size to the maximum I/O size. */ 6911 *tl++ = txdr_unsigned(nfs_srvmaxio & NFSFLAYUTIL_STRIPE_MASK); 6912 *tl++ = 0; /* 1st stripe index. */ 6913 pattern_offset = 0; 6914 txdr_hyper(pattern_offset, tl); tl += 2; /* Pattern offset. */ 6915 *tl++ = txdr_unsigned(1); /* 1 file handle. */ 6916 *tl++ = txdr_unsigned(NFSX_V4PNFSFH); 6917 NFSBCOPY(dsfhp, tl, sizeof(*dsfhp)); 6918 lyp->lay_layoutlen = NFSX_V4FILELAYOUT; 6919 return (lyp); 6920 } 6921 6922 #define FLEX_OWNERID "999" 6923 #define FLEX_UID0 "0" 6924 /* 6925 * Generate a Flex File Layout. 6926 * The FLEX_OWNERID can be any string of 3 decimal digits. Although this 6927 * string goes on the wire, it isn't supposed to be used by the client, 6928 * since this server uses tight coupling. 6929 * Although not recommended by the spec., if vfs.nfsd.flexlinuxhack=1 use 6930 * a string of "0". This works around the Linux Flex File Layout driver bug 6931 * which uses the synthetic uid/gid strings for the "tightly coupled" case. 6932 */ 6933 static struct nfslayout * 6934 nfsrv_flexlayout(struct nfsrv_descript *nd, int iomode, int mirrorcnt, 6935 fhandle_t *fhp, fhandle_t *dsfhp, char *devid, fsid_t fs) 6936 { 6937 uint32_t *tl; 6938 struct nfslayout *lyp; 6939 uint64_t lenval; 6940 int i; 6941 6942 lyp = malloc(sizeof(struct nfslayout) + NFSX_V4FLEXLAYOUT(mirrorcnt), 6943 M_NFSDSTATE, M_WAITOK | M_ZERO); 6944 lyp->lay_type = NFSLAYOUT_FLEXFILE; 6945 if (iomode == NFSLAYOUTIOMODE_RW) 6946 lyp->lay_flags = NFSLAY_RW; 6947 else 6948 lyp->lay_flags = NFSLAY_READ; 6949 NFSBCOPY(fhp, &lyp->lay_fh, sizeof(*fhp)); 6950 lyp->lay_clientid.qval = nd->nd_clientid.qval; 6951 lyp->lay_fsid = fs; 6952 lyp->lay_mirrorcnt = mirrorcnt; 6953 NFSBCOPY(devid, lyp->lay_deviceid, NFSX_V4DEVICEID); 6954 6955 /* Fill in the xdr for the files layout. */ 6956 tl = (uint32_t *)lyp->lay_xdr; 6957 lenval = 0; 6958 txdr_hyper(lenval, tl); tl += 2; /* Stripe unit. */ 6959 *tl++ = txdr_unsigned(mirrorcnt); /* # of mirrors. */ 6960 for (i = 0; i < mirrorcnt; i++) { 6961 *tl++ = txdr_unsigned(1); /* One stripe. */ 6962 NFSBCOPY(devid, tl, NFSX_V4DEVICEID); /* Device ID. */ 6963 tl += (NFSX_V4DEVICEID / NFSX_UNSIGNED); 6964 devid += NFSX_V4DEVICEID; 6965 *tl++ = txdr_unsigned(1); /* Efficiency. */ 6966 *tl++ = 0; /* Proxy Stateid. */ 6967 *tl++ = 0x55555555; 6968 *tl++ = 0x55555555; 6969 *tl++ = 0x55555555; 6970 *tl++ = txdr_unsigned(1); /* 1 file handle. */ 6971 *tl++ = txdr_unsigned(NFSX_V4PNFSFH); 6972 NFSBCOPY(dsfhp, tl, sizeof(*dsfhp)); 6973 tl += (NFSM_RNDUP(NFSX_V4PNFSFH) / NFSX_UNSIGNED); 6974 dsfhp++; 6975 if (nfsrv_flexlinuxhack != 0) { 6976 *tl++ = txdr_unsigned(strlen(FLEX_UID0)); 6977 *tl = 0; /* 0 pad string. */ 6978 NFSBCOPY(FLEX_UID0, tl++, strlen(FLEX_UID0)); 6979 *tl++ = txdr_unsigned(strlen(FLEX_UID0)); 6980 *tl = 0; /* 0 pad string. */ 6981 NFSBCOPY(FLEX_UID0, tl++, strlen(FLEX_UID0)); 6982 } else { 6983 *tl++ = txdr_unsigned(strlen(FLEX_OWNERID)); 6984 NFSBCOPY(FLEX_OWNERID, tl++, NFSX_UNSIGNED); 6985 *tl++ = txdr_unsigned(strlen(FLEX_OWNERID)); 6986 NFSBCOPY(FLEX_OWNERID, tl++, NFSX_UNSIGNED); 6987 } 6988 } 6989 *tl++ = txdr_unsigned(0); /* ff_flags. */ 6990 *tl = txdr_unsigned(60); /* Status interval hint. */ 6991 lyp->lay_layoutlen = NFSX_V4FLEXLAYOUT(mirrorcnt); 6992 return (lyp); 6993 } 6994 6995 /* 6996 * Parse and process Flex File errors returned via LayoutReturn. 6997 */ 6998 static void 6999 nfsrv_flexlayouterr(struct nfsrv_descript *nd, uint32_t *layp, int maxcnt, 7000 NFSPROC_T *p) 7001 { 7002 uint32_t *tl; 7003 int cnt, errcnt, i, j, opnum, stat; 7004 char devid[NFSX_V4DEVICEID]; 7005 7006 tl = layp; 7007 maxcnt -= NFSX_UNSIGNED; 7008 if (maxcnt > 0) 7009 cnt = fxdr_unsigned(int, *tl++); 7010 else 7011 cnt = 0; 7012 NFSD_DEBUG(4, "flexlayouterr cnt=%d\n", cnt); 7013 for (i = 0; i < cnt; i++) { 7014 maxcnt -= NFSX_STATEID + 2 * NFSX_HYPER + 7015 NFSX_UNSIGNED; 7016 if (maxcnt <= 0) 7017 break; 7018 /* Skip offset, length and stateid for now. */ 7019 tl += (4 + NFSX_STATEID / NFSX_UNSIGNED); 7020 errcnt = fxdr_unsigned(int, *tl++); 7021 NFSD_DEBUG(4, "flexlayouterr errcnt=%d\n", errcnt); 7022 for (j = 0; j < errcnt; j++) { 7023 maxcnt -= NFSX_V4DEVICEID + 2 * NFSX_UNSIGNED; 7024 if (maxcnt < 0) 7025 break; 7026 NFSBCOPY(tl, devid, NFSX_V4DEVICEID); 7027 tl += (NFSX_V4DEVICEID / NFSX_UNSIGNED); 7028 stat = fxdr_unsigned(int, *tl++); 7029 opnum = fxdr_unsigned(int, *tl++); 7030 NFSD_DEBUG(4, "flexlayouterr op=%d stat=%d\n", opnum, 7031 stat); 7032 /* 7033 * Except for NFSERR_ACCES, NFSERR_STALE and 7034 * NFSERR_NOSPC errors, disable the mirror. 7035 */ 7036 if (stat != NFSERR_ACCES && stat != NFSERR_STALE && 7037 stat != NFSERR_NOSPC) 7038 nfsrv_delds(devid, p); 7039 7040 /* For NFSERR_NOSPC, mark all devids and layouts. */ 7041 if (stat == NFSERR_NOSPC) 7042 nfsrv_marknospc(devid, true); 7043 } 7044 } 7045 } 7046 7047 /* 7048 * This function removes all flex file layouts which has a mirror with 7049 * a device id that matches the argument. 7050 * Called when the DS represented by the device id has failed. 7051 */ 7052 void 7053 nfsrv_flexmirrordel(char *devid, NFSPROC_T *p) 7054 { 7055 uint32_t *tl; 7056 struct nfslayout *lyp, *nlyp; 7057 struct nfslayouthash *lhyp; 7058 struct nfslayouthead loclyp; 7059 int i, j; 7060 7061 NFSD_DEBUG(4, "flexmirrordel\n"); 7062 /* Move all layouts found onto a local list. */ 7063 TAILQ_INIT(&loclyp); 7064 for (i = 0; i < nfsrv_layouthashsize; i++) { 7065 lhyp = &nfslayouthash[i]; 7066 NFSLOCKLAYOUT(lhyp); 7067 TAILQ_FOREACH_SAFE(lyp, &lhyp->list, lay_list, nlyp) { 7068 if (lyp->lay_type == NFSLAYOUT_FLEXFILE && 7069 lyp->lay_mirrorcnt > 1) { 7070 NFSD_DEBUG(4, "possible match\n"); 7071 tl = lyp->lay_xdr; 7072 tl += 3; 7073 for (j = 0; j < lyp->lay_mirrorcnt; j++) { 7074 tl++; 7075 if (NFSBCMP(devid, tl, NFSX_V4DEVICEID) 7076 == 0) { 7077 /* Found one. */ 7078 NFSD_DEBUG(4, "fnd one\n"); 7079 TAILQ_REMOVE(&lhyp->list, lyp, 7080 lay_list); 7081 TAILQ_INSERT_HEAD(&loclyp, lyp, 7082 lay_list); 7083 break; 7084 } 7085 tl += (NFSX_V4DEVICEID / NFSX_UNSIGNED + 7086 NFSM_RNDUP(NFSX_V4PNFSFH) / 7087 NFSX_UNSIGNED + 11 * NFSX_UNSIGNED); 7088 } 7089 } 7090 } 7091 NFSUNLOCKLAYOUT(lhyp); 7092 } 7093 7094 /* Now, try to do a Layout recall for each one found. */ 7095 TAILQ_FOREACH_SAFE(lyp, &loclyp, lay_list, nlyp) { 7096 NFSD_DEBUG(4, "do layout recall\n"); 7097 /* 7098 * The layout stateid.seqid needs to be incremented 7099 * before doing a LAYOUT_RECALL callback. 7100 */ 7101 if (++lyp->lay_stateid.seqid == 0) 7102 lyp->lay_stateid.seqid = 1; 7103 nfsrv_recalllayout(lyp->lay_clientid, &lyp->lay_stateid, 7104 &lyp->lay_fh, lyp, 1, lyp->lay_type, p); 7105 nfsrv_freelayout(&loclyp, lyp); 7106 } 7107 } 7108 7109 /* 7110 * Do a recall callback to the client for this layout. 7111 */ 7112 static int 7113 nfsrv_recalllayout(nfsquad_t clid, nfsv4stateid_t *stateidp, fhandle_t *fhp, 7114 struct nfslayout *lyp, int changed, int laytype, NFSPROC_T *p) 7115 { 7116 struct nfsclient *clp; 7117 int error; 7118 7119 NFSD_DEBUG(4, "nfsrv_recalllayout\n"); 7120 error = nfsrv_getclient(clid, 0, &clp, NULL, (nfsquad_t)((u_quad_t)0), 7121 0, NULL, p); 7122 NFSD_DEBUG(4, "aft nfsrv_getclient=%d\n", error); 7123 if (error != 0) { 7124 printf("nfsrv_recalllayout: getclient err=%d\n", error); 7125 return (error); 7126 } 7127 if ((clp->lc_flags & LCL_NFSV41) != 0) { 7128 error = nfsrv_docallback(clp, NFSV4OP_CBLAYOUTRECALL, 7129 stateidp, changed, fhp, NULL, NULL, laytype, p); 7130 /* If lyp != NULL, handle an error return here. */ 7131 if (error != 0 && lyp != NULL) { 7132 NFSDRECALLLOCK(); 7133 /* 7134 * Mark it returned, since no layout recall 7135 * has been done. 7136 * All errors seem to be non-recoverable, although 7137 * NFSERR_NOMATCHLAYOUT is a normal event. 7138 */ 7139 if ((lyp->lay_flags & NFSLAY_RECALL) != 0) { 7140 lyp->lay_flags |= NFSLAY_RETURNED; 7141 wakeup(lyp); 7142 } 7143 NFSDRECALLUNLOCK(); 7144 if (error != NFSERR_NOMATCHLAYOUT) 7145 printf("nfsrv_recalllayout: err=%d\n", error); 7146 } 7147 } else 7148 printf("nfsrv_recalllayout: clp not NFSv4.1\n"); 7149 return (error); 7150 } 7151 7152 /* 7153 * Find a layout to recall when we exceed our high water mark. 7154 */ 7155 void 7156 nfsrv_recalloldlayout(NFSPROC_T *p) 7157 { 7158 struct nfslayouthash *lhyp; 7159 struct nfslayout *lyp; 7160 nfsquad_t clientid; 7161 nfsv4stateid_t stateid; 7162 fhandle_t fh; 7163 int error, laytype = 0, ret; 7164 7165 lhyp = &nfslayouthash[arc4random() % nfsrv_layouthashsize]; 7166 NFSLOCKLAYOUT(lhyp); 7167 TAILQ_FOREACH_REVERSE(lyp, &lhyp->list, nfslayouthead, lay_list) { 7168 if ((lyp->lay_flags & NFSLAY_CALLB) == 0) { 7169 lyp->lay_flags |= NFSLAY_CALLB; 7170 /* 7171 * The layout stateid.seqid needs to be incremented 7172 * before doing a LAYOUT_RECALL callback. 7173 */ 7174 if (++lyp->lay_stateid.seqid == 0) 7175 lyp->lay_stateid.seqid = 1; 7176 clientid = lyp->lay_clientid; 7177 stateid = lyp->lay_stateid; 7178 NFSBCOPY(&lyp->lay_fh, &fh, sizeof(fh)); 7179 laytype = lyp->lay_type; 7180 break; 7181 } 7182 } 7183 NFSUNLOCKLAYOUT(lhyp); 7184 if (lyp != NULL) { 7185 error = nfsrv_recalllayout(clientid, &stateid, &fh, NULL, 0, 7186 laytype, p); 7187 if (error != 0 && error != NFSERR_NOMATCHLAYOUT) 7188 NFSD_DEBUG(4, "recallold=%d\n", error); 7189 if (error != 0) { 7190 NFSLOCKLAYOUT(lhyp); 7191 /* 7192 * Since the hash list was unlocked, we need to 7193 * find it again. 7194 */ 7195 ret = nfsrv_findlayout(&clientid, &fh, laytype, p, 7196 &lyp); 7197 if (ret == 0 && 7198 (lyp->lay_flags & NFSLAY_CALLB) != 0 && 7199 lyp->lay_stateid.other[0] == stateid.other[0] && 7200 lyp->lay_stateid.other[1] == stateid.other[1] && 7201 lyp->lay_stateid.other[2] == stateid.other[2]) { 7202 /* 7203 * The client no longer knows this layout, so 7204 * it can be free'd now. 7205 */ 7206 if (error == NFSERR_NOMATCHLAYOUT) 7207 nfsrv_freelayout(&lhyp->list, lyp); 7208 else { 7209 /* 7210 * Leave it to be tried later by 7211 * clearing NFSLAY_CALLB and moving 7212 * it to the head of the list, so it 7213 * won't be tried again for a while. 7214 */ 7215 lyp->lay_flags &= ~NFSLAY_CALLB; 7216 TAILQ_REMOVE(&lhyp->list, lyp, 7217 lay_list); 7218 TAILQ_INSERT_HEAD(&lhyp->list, lyp, 7219 lay_list); 7220 } 7221 } 7222 NFSUNLOCKLAYOUT(lhyp); 7223 } 7224 } 7225 } 7226 7227 /* 7228 * Try and return layout(s). 7229 */ 7230 int 7231 nfsrv_layoutreturn(struct nfsrv_descript *nd, vnode_t vp, 7232 int layouttype, int iomode, uint64_t offset, uint64_t len, int reclaim, 7233 int kind, nfsv4stateid_t *stateidp, int maxcnt, uint32_t *layp, int *fndp, 7234 struct ucred *cred, NFSPROC_T *p) 7235 { 7236 struct nfsvattr na; 7237 struct nfslayouthash *lhyp; 7238 struct nfslayout *lyp; 7239 fhandle_t fh; 7240 int error = 0; 7241 7242 *fndp = 0; 7243 if (kind == NFSV4LAYOUTRET_FILE) { 7244 error = nfsvno_getfh(vp, &fh, p); 7245 if (error == 0) { 7246 error = nfsrv_updatemdsattr(vp, &na, p); 7247 if (error != 0) 7248 printf("nfsrv_layoutreturn: updatemdsattr" 7249 " failed=%d\n", error); 7250 } 7251 if (error == 0) { 7252 if (reclaim == newnfs_true) { 7253 error = nfsrv_checkgrace(NULL, NULL, 7254 NFSLCK_RECLAIM); 7255 if (error != NFSERR_NOGRACE) 7256 error = 0; 7257 return (error); 7258 } 7259 lhyp = NFSLAYOUTHASH(&fh); 7260 NFSDRECALLLOCK(); 7261 NFSLOCKLAYOUT(lhyp); 7262 error = nfsrv_findlayout(&nd->nd_clientid, &fh, 7263 layouttype, p, &lyp); 7264 NFSD_DEBUG(4, "layoutret findlay=%d\n", error); 7265 if (error == 0 && 7266 stateidp->other[0] == lyp->lay_stateid.other[0] && 7267 stateidp->other[1] == lyp->lay_stateid.other[1] && 7268 stateidp->other[2] == lyp->lay_stateid.other[2]) { 7269 NFSD_DEBUG(4, "nfsrv_layoutreturn: stateid %d" 7270 " %x %x %x laystateid %d %x %x %x" 7271 " off=%ju len=%ju flgs=0x%x\n", 7272 stateidp->seqid, stateidp->other[0], 7273 stateidp->other[1], stateidp->other[2], 7274 lyp->lay_stateid.seqid, 7275 lyp->lay_stateid.other[0], 7276 lyp->lay_stateid.other[1], 7277 lyp->lay_stateid.other[2], 7278 (uintmax_t)offset, (uintmax_t)len, 7279 lyp->lay_flags); 7280 if (++lyp->lay_stateid.seqid == 0) 7281 lyp->lay_stateid.seqid = 1; 7282 stateidp->seqid = lyp->lay_stateid.seqid; 7283 if (offset == 0 && len == UINT64_MAX) { 7284 if ((iomode & NFSLAYOUTIOMODE_READ) != 7285 0) 7286 lyp->lay_flags &= ~NFSLAY_READ; 7287 if ((iomode & NFSLAYOUTIOMODE_RW) != 0) 7288 lyp->lay_flags &= ~NFSLAY_RW; 7289 if ((lyp->lay_flags & (NFSLAY_READ | 7290 NFSLAY_RW)) == 0) 7291 nfsrv_freelayout(&lhyp->list, 7292 lyp); 7293 else 7294 *fndp = 1; 7295 } else 7296 *fndp = 1; 7297 } 7298 NFSUNLOCKLAYOUT(lhyp); 7299 /* Search the nfsrv_recalllist for a match. */ 7300 TAILQ_FOREACH(lyp, &nfsrv_recalllisthead, lay_list) { 7301 if (NFSBCMP(&lyp->lay_fh, &fh, 7302 sizeof(fh)) == 0 && 7303 lyp->lay_clientid.qval == 7304 nd->nd_clientid.qval && 7305 stateidp->other[0] == 7306 lyp->lay_stateid.other[0] && 7307 stateidp->other[1] == 7308 lyp->lay_stateid.other[1] && 7309 stateidp->other[2] == 7310 lyp->lay_stateid.other[2]) { 7311 lyp->lay_flags |= NFSLAY_RETURNED; 7312 wakeup(lyp); 7313 error = 0; 7314 } 7315 } 7316 NFSDRECALLUNLOCK(); 7317 } 7318 if (layouttype == NFSLAYOUT_FLEXFILE && layp != NULL) 7319 nfsrv_flexlayouterr(nd, layp, maxcnt, p); 7320 } else if (kind == NFSV4LAYOUTRET_FSID) 7321 nfsrv_freelayouts(&nd->nd_clientid, 7322 &vp->v_mount->mnt_stat.f_fsid, layouttype, iomode); 7323 else if (kind == NFSV4LAYOUTRET_ALL) 7324 nfsrv_freelayouts(&nd->nd_clientid, NULL, layouttype, iomode); 7325 else 7326 error = NFSERR_INVAL; 7327 if (error == -1) 7328 error = 0; 7329 return (error); 7330 } 7331 7332 /* 7333 * Look for an existing layout. 7334 */ 7335 static int 7336 nfsrv_findlayout(nfsquad_t *clientidp, fhandle_t *fhp, int laytype, 7337 NFSPROC_T *p, struct nfslayout **lypp) 7338 { 7339 struct nfslayouthash *lhyp; 7340 struct nfslayout *lyp; 7341 int ret; 7342 7343 *lypp = NULL; 7344 ret = 0; 7345 lhyp = NFSLAYOUTHASH(fhp); 7346 TAILQ_FOREACH(lyp, &lhyp->list, lay_list) { 7347 if (NFSBCMP(&lyp->lay_fh, fhp, sizeof(*fhp)) == 0 && 7348 lyp->lay_clientid.qval == clientidp->qval && 7349 lyp->lay_type == laytype) 7350 break; 7351 } 7352 if (lyp != NULL) 7353 *lypp = lyp; 7354 else 7355 ret = -1; 7356 return (ret); 7357 } 7358 7359 /* 7360 * Add the new layout, as required. 7361 */ 7362 static int 7363 nfsrv_addlayout(struct nfsrv_descript *nd, struct nfslayout **lypp, 7364 nfsv4stateid_t *stateidp, char *layp, int *layoutlenp, NFSPROC_T *p) 7365 { 7366 struct nfsclient *clp; 7367 struct nfslayouthash *lhyp; 7368 struct nfslayout *lyp, *nlyp; 7369 fhandle_t *fhp; 7370 int error; 7371 7372 KASSERT((nd->nd_flag & ND_IMPLIEDCLID) != 0, 7373 ("nfsrv_layoutget: no nd_clientid\n")); 7374 lyp = *lypp; 7375 fhp = &lyp->lay_fh; 7376 NFSLOCKSTATE(); 7377 error = nfsrv_getclient((nfsquad_t)((u_quad_t)0), CLOPS_RENEW, &clp, 7378 NULL, (nfsquad_t)((u_quad_t)0), 0, nd, p); 7379 if (error != 0) { 7380 NFSUNLOCKSTATE(); 7381 return (error); 7382 } 7383 lyp->lay_stateid.seqid = stateidp->seqid = 1; 7384 lyp->lay_stateid.other[0] = stateidp->other[0] = 7385 clp->lc_clientid.lval[0]; 7386 lyp->lay_stateid.other[1] = stateidp->other[1] = 7387 clp->lc_clientid.lval[1]; 7388 lyp->lay_stateid.other[2] = stateidp->other[2] = 7389 nfsrv_nextstateindex(clp); 7390 NFSUNLOCKSTATE(); 7391 7392 lhyp = NFSLAYOUTHASH(fhp); 7393 NFSLOCKLAYOUT(lhyp); 7394 TAILQ_FOREACH(nlyp, &lhyp->list, lay_list) { 7395 if (NFSBCMP(&nlyp->lay_fh, fhp, sizeof(*fhp)) == 0 && 7396 nlyp->lay_clientid.qval == nd->nd_clientid.qval) 7397 break; 7398 } 7399 if (nlyp != NULL) { 7400 /* A layout already exists, so use it. */ 7401 nlyp->lay_flags |= (lyp->lay_flags & (NFSLAY_READ | NFSLAY_RW)); 7402 NFSBCOPY(nlyp->lay_xdr, layp, nlyp->lay_layoutlen); 7403 *layoutlenp = nlyp->lay_layoutlen; 7404 if (++nlyp->lay_stateid.seqid == 0) 7405 nlyp->lay_stateid.seqid = 1; 7406 stateidp->seqid = nlyp->lay_stateid.seqid; 7407 stateidp->other[0] = nlyp->lay_stateid.other[0]; 7408 stateidp->other[1] = nlyp->lay_stateid.other[1]; 7409 stateidp->other[2] = nlyp->lay_stateid.other[2]; 7410 NFSUNLOCKLAYOUT(lhyp); 7411 return (0); 7412 } 7413 7414 /* Insert the new layout in the lists. */ 7415 *lypp = NULL; 7416 atomic_add_int(&nfsrv_layoutcnt, 1); 7417 nfsstatsv1.srvlayouts++; 7418 NFSBCOPY(lyp->lay_xdr, layp, lyp->lay_layoutlen); 7419 *layoutlenp = lyp->lay_layoutlen; 7420 TAILQ_INSERT_HEAD(&lhyp->list, lyp, lay_list); 7421 NFSUNLOCKLAYOUT(lhyp); 7422 return (0); 7423 } 7424 7425 /* 7426 * Get the devinfo for a deviceid. 7427 */ 7428 int 7429 nfsrv_getdevinfo(char *devid, int layouttype, uint32_t *maxcnt, 7430 uint32_t *notify, int *devaddrlen, char **devaddr) 7431 { 7432 struct nfsdevice *ds; 7433 7434 if ((layouttype != NFSLAYOUT_NFSV4_1_FILES && layouttype != 7435 NFSLAYOUT_FLEXFILE) || 7436 (nfsrv_maxpnfsmirror > 1 && layouttype == NFSLAYOUT_NFSV4_1_FILES)) 7437 return (NFSERR_UNKNLAYOUTTYPE); 7438 7439 /* 7440 * Now, search for the device id. Note that the structures won't go 7441 * away, but the order changes in the list. As such, the lock only 7442 * needs to be held during the search through the list. 7443 */ 7444 NFSDDSLOCK(); 7445 TAILQ_FOREACH(ds, &nfsrv_devidhead, nfsdev_list) { 7446 if (NFSBCMP(devid, ds->nfsdev_deviceid, NFSX_V4DEVICEID) == 0 && 7447 ds->nfsdev_nmp != NULL) 7448 break; 7449 } 7450 NFSDDSUNLOCK(); 7451 if (ds == NULL) 7452 return (NFSERR_NOENT); 7453 7454 /* If the correct nfsdev_XXXXaddrlen is > 0, we have the device info. */ 7455 *devaddrlen = 0; 7456 if (layouttype == NFSLAYOUT_NFSV4_1_FILES) { 7457 *devaddrlen = ds->nfsdev_fileaddrlen; 7458 *devaddr = ds->nfsdev_fileaddr; 7459 } else if (layouttype == NFSLAYOUT_FLEXFILE) { 7460 *devaddrlen = ds->nfsdev_flexaddrlen; 7461 *devaddr = ds->nfsdev_flexaddr; 7462 } 7463 if (*devaddrlen == 0) 7464 return (NFSERR_UNKNLAYOUTTYPE); 7465 7466 /* 7467 * The XDR overhead is 3 unsigned values: layout_type, 7468 * length_of_address and notify bitmap. 7469 * If the notify array is changed to not all zeros, the 7470 * count of unsigned values must be increased. 7471 */ 7472 if (*maxcnt > 0 && *maxcnt < NFSM_RNDUP(*devaddrlen) + 7473 3 * NFSX_UNSIGNED) { 7474 *maxcnt = NFSM_RNDUP(*devaddrlen) + 3 * NFSX_UNSIGNED; 7475 return (NFSERR_TOOSMALL); 7476 } 7477 return (0); 7478 } 7479 7480 /* 7481 * Free a list of layout state structures. 7482 */ 7483 static void 7484 nfsrv_freelayoutlist(nfsquad_t clientid) 7485 { 7486 struct nfslayouthash *lhyp; 7487 struct nfslayout *lyp, *nlyp; 7488 int i; 7489 7490 for (i = 0; i < nfsrv_layouthashsize; i++) { 7491 lhyp = &nfslayouthash[i]; 7492 NFSLOCKLAYOUT(lhyp); 7493 TAILQ_FOREACH_SAFE(lyp, &lhyp->list, lay_list, nlyp) { 7494 if (lyp->lay_clientid.qval == clientid.qval) 7495 nfsrv_freelayout(&lhyp->list, lyp); 7496 } 7497 NFSUNLOCKLAYOUT(lhyp); 7498 } 7499 } 7500 7501 /* 7502 * Free up a layout. 7503 */ 7504 static void 7505 nfsrv_freelayout(struct nfslayouthead *lhp, struct nfslayout *lyp) 7506 { 7507 7508 NFSD_DEBUG(4, "Freelayout=%p\n", lyp); 7509 atomic_add_int(&nfsrv_layoutcnt, -1); 7510 nfsstatsv1.srvlayouts--; 7511 TAILQ_REMOVE(lhp, lyp, lay_list); 7512 free(lyp, M_NFSDSTATE); 7513 } 7514 7515 /* 7516 * Free up a device id. 7517 */ 7518 void 7519 nfsrv_freeonedevid(struct nfsdevice *ds) 7520 { 7521 int i; 7522 7523 atomic_add_int(&nfsrv_devidcnt, -1); 7524 vrele(ds->nfsdev_dvp); 7525 for (i = 0; i < nfsrv_dsdirsize; i++) 7526 if (ds->nfsdev_dsdir[i] != NULL) 7527 vrele(ds->nfsdev_dsdir[i]); 7528 free(ds->nfsdev_fileaddr, M_NFSDSTATE); 7529 free(ds->nfsdev_flexaddr, M_NFSDSTATE); 7530 free(ds->nfsdev_host, M_NFSDSTATE); 7531 free(ds, M_NFSDSTATE); 7532 } 7533 7534 /* 7535 * Free up a device id and its mirrors. 7536 */ 7537 static void 7538 nfsrv_freedevid(struct nfsdevice *ds) 7539 { 7540 7541 TAILQ_REMOVE(&nfsrv_devidhead, ds, nfsdev_list); 7542 nfsrv_freeonedevid(ds); 7543 } 7544 7545 /* 7546 * Free all layouts and device ids. 7547 * Done when the nfsd threads are shut down since there may be a new 7548 * modified device id list created when the nfsd is restarted. 7549 */ 7550 void 7551 nfsrv_freealllayoutsanddevids(void) 7552 { 7553 struct nfsdontlist *mrp, *nmrp; 7554 struct nfslayout *lyp, *nlyp; 7555 7556 /* Get rid of the deviceid structures. */ 7557 nfsrv_freealldevids(); 7558 TAILQ_INIT(&nfsrv_devidhead); 7559 nfsrv_devidcnt = 0; 7560 7561 /* Get rid of all layouts. */ 7562 nfsrv_freealllayouts(); 7563 7564 /* Get rid of any nfsdontlist entries. */ 7565 LIST_FOREACH_SAFE(mrp, &nfsrv_dontlisthead, nfsmr_list, nmrp) 7566 free(mrp, M_NFSDSTATE); 7567 LIST_INIT(&nfsrv_dontlisthead); 7568 nfsrv_dontlistlen = 0; 7569 7570 /* Free layouts in the recall list. */ 7571 TAILQ_FOREACH_SAFE(lyp, &nfsrv_recalllisthead, lay_list, nlyp) 7572 nfsrv_freelayout(&nfsrv_recalllisthead, lyp); 7573 TAILQ_INIT(&nfsrv_recalllisthead); 7574 } 7575 7576 /* 7577 * Free layouts that match the arguments. 7578 */ 7579 static void 7580 nfsrv_freelayouts(nfsquad_t *clid, fsid_t *fs, int laytype, int iomode) 7581 { 7582 struct nfslayouthash *lhyp; 7583 struct nfslayout *lyp, *nlyp; 7584 int i; 7585 7586 for (i = 0; i < nfsrv_layouthashsize; i++) { 7587 lhyp = &nfslayouthash[i]; 7588 NFSLOCKLAYOUT(lhyp); 7589 TAILQ_FOREACH_SAFE(lyp, &lhyp->list, lay_list, nlyp) { 7590 if (clid->qval != lyp->lay_clientid.qval) 7591 continue; 7592 if (fs != NULL && fsidcmp(fs, &lyp->lay_fsid) != 0) 7593 continue; 7594 if (laytype != lyp->lay_type) 7595 continue; 7596 if ((iomode & NFSLAYOUTIOMODE_READ) != 0) 7597 lyp->lay_flags &= ~NFSLAY_READ; 7598 if ((iomode & NFSLAYOUTIOMODE_RW) != 0) 7599 lyp->lay_flags &= ~NFSLAY_RW; 7600 if ((lyp->lay_flags & (NFSLAY_READ | NFSLAY_RW)) == 0) 7601 nfsrv_freelayout(&lhyp->list, lyp); 7602 } 7603 NFSUNLOCKLAYOUT(lhyp); 7604 } 7605 } 7606 7607 /* 7608 * Free all layouts for the argument file. 7609 */ 7610 void 7611 nfsrv_freefilelayouts(fhandle_t *fhp) 7612 { 7613 struct nfslayouthash *lhyp; 7614 struct nfslayout *lyp, *nlyp; 7615 7616 lhyp = NFSLAYOUTHASH(fhp); 7617 NFSLOCKLAYOUT(lhyp); 7618 TAILQ_FOREACH_SAFE(lyp, &lhyp->list, lay_list, nlyp) { 7619 if (NFSBCMP(&lyp->lay_fh, fhp, sizeof(*fhp)) == 0) 7620 nfsrv_freelayout(&lhyp->list, lyp); 7621 } 7622 NFSUNLOCKLAYOUT(lhyp); 7623 } 7624 7625 /* 7626 * Free all layouts. 7627 */ 7628 static void 7629 nfsrv_freealllayouts(void) 7630 { 7631 struct nfslayouthash *lhyp; 7632 struct nfslayout *lyp, *nlyp; 7633 int i; 7634 7635 for (i = 0; i < nfsrv_layouthashsize; i++) { 7636 lhyp = &nfslayouthash[i]; 7637 NFSLOCKLAYOUT(lhyp); 7638 TAILQ_FOREACH_SAFE(lyp, &lhyp->list, lay_list, nlyp) 7639 nfsrv_freelayout(&lhyp->list, lyp); 7640 NFSUNLOCKLAYOUT(lhyp); 7641 } 7642 } 7643 7644 /* 7645 * Look up the mount path for the DS server. 7646 */ 7647 static int 7648 nfsrv_setdsserver(char *dspathp, char *mdspathp, NFSPROC_T *p, 7649 struct nfsdevice **dsp) 7650 { 7651 struct nameidata nd; 7652 struct nfsdevice *ds; 7653 struct mount *mp; 7654 int error, i; 7655 char *dsdirpath; 7656 size_t dsdirsize; 7657 7658 NFSD_DEBUG(4, "setdssrv path=%s\n", dspathp); 7659 *dsp = NULL; 7660 NDINIT(&nd, LOOKUP, FOLLOW | LOCKSHARED | LOCKLEAF, UIO_SYSSPACE, 7661 dspathp); 7662 error = namei(&nd); 7663 NFSD_DEBUG(4, "lookup=%d\n", error); 7664 if (error != 0) 7665 return (error); 7666 if (nd.ni_vp->v_type != VDIR) { 7667 vput(nd.ni_vp); 7668 NFSD_DEBUG(4, "dspath not dir\n"); 7669 return (ENOTDIR); 7670 } 7671 if (strcmp(nd.ni_vp->v_mount->mnt_vfc->vfc_name, "nfs") != 0) { 7672 vput(nd.ni_vp); 7673 NFSD_DEBUG(4, "dspath not an NFS mount\n"); 7674 return (ENXIO); 7675 } 7676 7677 /* 7678 * Allocate a DS server structure with the NFS mounted directory 7679 * vnode reference counted, so that a non-forced dismount will 7680 * fail with EBUSY. 7681 * This structure is always linked into the list, even if an error 7682 * is being returned. The caller will free the entire list upon 7683 * an error return. 7684 */ 7685 *dsp = ds = malloc(sizeof(*ds) + nfsrv_dsdirsize * sizeof(vnode_t), 7686 M_NFSDSTATE, M_WAITOK | M_ZERO); 7687 ds->nfsdev_dvp = nd.ni_vp; 7688 ds->nfsdev_nmp = VFSTONFS(nd.ni_vp->v_mount); 7689 NFSVOPUNLOCK(nd.ni_vp); 7690 7691 dsdirsize = strlen(dspathp) + 16; 7692 dsdirpath = malloc(dsdirsize, M_TEMP, M_WAITOK); 7693 /* Now, create the DS directory structures. */ 7694 for (i = 0; i < nfsrv_dsdirsize; i++) { 7695 snprintf(dsdirpath, dsdirsize, "%s/ds%d", dspathp, i); 7696 NDINIT(&nd, LOOKUP, FOLLOW | LOCKSHARED | LOCKLEAF, 7697 UIO_SYSSPACE, dsdirpath); 7698 error = namei(&nd); 7699 NFSD_DEBUG(4, "dsdirpath=%s lookup=%d\n", dsdirpath, error); 7700 if (error != 0) 7701 break; 7702 if (nd.ni_vp->v_type != VDIR) { 7703 vput(nd.ni_vp); 7704 error = ENOTDIR; 7705 NFSD_DEBUG(4, "dsdirpath not a VDIR\n"); 7706 break; 7707 } 7708 if (strcmp(nd.ni_vp->v_mount->mnt_vfc->vfc_name, "nfs") != 0) { 7709 vput(nd.ni_vp); 7710 error = ENXIO; 7711 NFSD_DEBUG(4, "dsdirpath not an NFS mount\n"); 7712 break; 7713 } 7714 ds->nfsdev_dsdir[i] = nd.ni_vp; 7715 NFSVOPUNLOCK(nd.ni_vp); 7716 } 7717 free(dsdirpath, M_TEMP); 7718 7719 if (strlen(mdspathp) > 0) { 7720 /* 7721 * This DS stores file for a specific MDS exported file 7722 * system. 7723 */ 7724 NDINIT(&nd, LOOKUP, FOLLOW | LOCKSHARED | LOCKLEAF, 7725 UIO_SYSSPACE, mdspathp); 7726 error = namei(&nd); 7727 NFSD_DEBUG(4, "mds lookup=%d\n", error); 7728 if (error != 0) 7729 goto out; 7730 if (nd.ni_vp->v_type != VDIR) { 7731 vput(nd.ni_vp); 7732 error = ENOTDIR; 7733 NFSD_DEBUG(4, "mdspath not dir\n"); 7734 goto out; 7735 } 7736 mp = nd.ni_vp->v_mount; 7737 if ((mp->mnt_flag & MNT_EXPORTED) == 0) { 7738 vput(nd.ni_vp); 7739 error = ENXIO; 7740 NFSD_DEBUG(4, "mdspath not an exported fs\n"); 7741 goto out; 7742 } 7743 ds->nfsdev_mdsfsid = mp->mnt_stat.f_fsid; 7744 ds->nfsdev_mdsisset = 1; 7745 vput(nd.ni_vp); 7746 } 7747 7748 out: 7749 TAILQ_INSERT_TAIL(&nfsrv_devidhead, ds, nfsdev_list); 7750 atomic_add_int(&nfsrv_devidcnt, 1); 7751 return (error); 7752 } 7753 7754 /* 7755 * Look up the mount path for the DS server and delete it. 7756 */ 7757 int 7758 nfsrv_deldsserver(int op, char *dspathp, NFSPROC_T *p) 7759 { 7760 struct mount *mp; 7761 struct nfsmount *nmp; 7762 struct nfsdevice *ds; 7763 int error; 7764 7765 NFSD_DEBUG(4, "deldssrv path=%s\n", dspathp); 7766 /* 7767 * Search for the path in the mount list. Avoid looking the path 7768 * up, since this mount point may be hung, with associated locked 7769 * vnodes, etc. 7770 * Set NFSMNTP_CANCELRPCS so that any forced dismount will be blocked 7771 * until this completes. 7772 * As noted in the man page, this should be done before any forced 7773 * dismount on the mount point, but at least the handshake on 7774 * NFSMNTP_CANCELRPCS should make it safe. 7775 */ 7776 error = 0; 7777 ds = NULL; 7778 nmp = NULL; 7779 mtx_lock(&mountlist_mtx); 7780 TAILQ_FOREACH(mp, &mountlist, mnt_list) { 7781 if (strcmp(mp->mnt_stat.f_mntonname, dspathp) == 0 && 7782 strcmp(mp->mnt_stat.f_fstypename, "nfs") == 0 && 7783 mp->mnt_data != NULL) { 7784 nmp = VFSTONFS(mp); 7785 NFSLOCKMNT(nmp); 7786 if ((nmp->nm_privflag & (NFSMNTP_FORCEDISM | 7787 NFSMNTP_CANCELRPCS)) == 0) { 7788 nmp->nm_privflag |= NFSMNTP_CANCELRPCS; 7789 NFSUNLOCKMNT(nmp); 7790 } else { 7791 NFSUNLOCKMNT(nmp); 7792 nmp = NULL; 7793 } 7794 break; 7795 } 7796 } 7797 mtx_unlock(&mountlist_mtx); 7798 7799 if (nmp != NULL) { 7800 ds = nfsrv_deldsnmp(op, nmp, p); 7801 NFSD_DEBUG(4, "deldsnmp=%p\n", ds); 7802 if (ds != NULL) { 7803 nfsrv_killrpcs(nmp); 7804 NFSD_DEBUG(4, "aft killrpcs\n"); 7805 } else 7806 error = ENXIO; 7807 NFSLOCKMNT(nmp); 7808 nmp->nm_privflag &= ~NFSMNTP_CANCELRPCS; 7809 wakeup(nmp); 7810 NFSUNLOCKMNT(nmp); 7811 } else 7812 error = EINVAL; 7813 return (error); 7814 } 7815 7816 /* 7817 * Search for and remove a DS entry which matches the "nmp" argument. 7818 * The nfsdevice structure pointer is returned so that the caller can 7819 * free it via nfsrv_freeonedevid(). 7820 * For the forced case, do not try to do LayoutRecalls, since the server 7821 * must be shut down now anyhow. 7822 */ 7823 struct nfsdevice * 7824 nfsrv_deldsnmp(int op, struct nfsmount *nmp, NFSPROC_T *p) 7825 { 7826 struct nfsdevice *fndds; 7827 7828 NFSD_DEBUG(4, "deldsdvp\n"); 7829 NFSDDSLOCK(); 7830 if (op == PNFSDOP_FORCEDELDS) 7831 fndds = nfsv4_findmirror(nmp); 7832 else 7833 fndds = nfsrv_findmirroredds(nmp); 7834 if (fndds != NULL) 7835 nfsrv_deleteds(fndds); 7836 NFSDDSUNLOCK(); 7837 if (fndds != NULL) { 7838 if (op != PNFSDOP_FORCEDELDS) 7839 nfsrv_flexmirrordel(fndds->nfsdev_deviceid, p); 7840 printf("pNFS server: mirror %s failed\n", fndds->nfsdev_host); 7841 } 7842 return (fndds); 7843 } 7844 7845 /* 7846 * Similar to nfsrv_deldsnmp(), except that the DS is indicated by deviceid. 7847 * This function also calls nfsrv_killrpcs() to unblock RPCs on the mount 7848 * point. 7849 * Also, returns an error instead of the nfsdevice found. 7850 */ 7851 int 7852 nfsrv_delds(char *devid, NFSPROC_T *p) 7853 { 7854 struct nfsdevice *ds, *fndds; 7855 struct nfsmount *nmp; 7856 int fndmirror; 7857 7858 NFSD_DEBUG(4, "delds\n"); 7859 /* 7860 * Search the DS server list for a match with devid. 7861 * Remove the DS entry if found and there is a mirror. 7862 */ 7863 fndds = NULL; 7864 nmp = NULL; 7865 fndmirror = 0; 7866 NFSDDSLOCK(); 7867 TAILQ_FOREACH(ds, &nfsrv_devidhead, nfsdev_list) { 7868 if (NFSBCMP(ds->nfsdev_deviceid, devid, NFSX_V4DEVICEID) == 0 && 7869 ds->nfsdev_nmp != NULL) { 7870 NFSD_DEBUG(4, "fnd main ds\n"); 7871 fndds = ds; 7872 break; 7873 } 7874 } 7875 if (fndds == NULL) { 7876 NFSDDSUNLOCK(); 7877 return (ENXIO); 7878 } 7879 if (fndds->nfsdev_mdsisset == 0 && nfsrv_faildscnt > 0) 7880 fndmirror = 1; 7881 else if (fndds->nfsdev_mdsisset != 0) { 7882 /* For the fsid is set case, search for a mirror. */ 7883 TAILQ_FOREACH(ds, &nfsrv_devidhead, nfsdev_list) { 7884 if (ds != fndds && ds->nfsdev_nmp != NULL && 7885 ds->nfsdev_mdsisset != 0 && 7886 fsidcmp(&ds->nfsdev_mdsfsid, 7887 &fndds->nfsdev_mdsfsid) == 0) { 7888 fndmirror = 1; 7889 break; 7890 } 7891 } 7892 } 7893 if (fndmirror != 0) { 7894 nmp = fndds->nfsdev_nmp; 7895 NFSLOCKMNT(nmp); 7896 if ((nmp->nm_privflag & (NFSMNTP_FORCEDISM | 7897 NFSMNTP_CANCELRPCS)) == 0) { 7898 nmp->nm_privflag |= NFSMNTP_CANCELRPCS; 7899 NFSUNLOCKMNT(nmp); 7900 nfsrv_deleteds(fndds); 7901 } else { 7902 NFSUNLOCKMNT(nmp); 7903 nmp = NULL; 7904 } 7905 } 7906 NFSDDSUNLOCK(); 7907 if (nmp != NULL) { 7908 nfsrv_flexmirrordel(fndds->nfsdev_deviceid, p); 7909 printf("pNFS server: mirror %s failed\n", fndds->nfsdev_host); 7910 nfsrv_killrpcs(nmp); 7911 NFSLOCKMNT(nmp); 7912 nmp->nm_privflag &= ~NFSMNTP_CANCELRPCS; 7913 wakeup(nmp); 7914 NFSUNLOCKMNT(nmp); 7915 return (0); 7916 } 7917 return (ENXIO); 7918 } 7919 7920 /* 7921 * Mark a DS as disabled by setting nfsdev_nmp = NULL. 7922 */ 7923 static void 7924 nfsrv_deleteds(struct nfsdevice *fndds) 7925 { 7926 7927 NFSD_DEBUG(4, "deleteds: deleting a mirror\n"); 7928 fndds->nfsdev_nmp = NULL; 7929 if (fndds->nfsdev_mdsisset == 0) 7930 nfsrv_faildscnt--; 7931 } 7932 7933 /* 7934 * Fill in the addr structures for the File and Flex File layouts. 7935 */ 7936 static void 7937 nfsrv_allocdevid(struct nfsdevice *ds, char *addr, char *dnshost) 7938 { 7939 uint32_t *tl; 7940 char *netprot; 7941 int addrlen; 7942 static uint64_t new_devid = 0; 7943 7944 if (strchr(addr, ':') != NULL) 7945 netprot = "tcp6"; 7946 else 7947 netprot = "tcp"; 7948 7949 /* Fill in the device id. */ 7950 NFSBCOPY(&nfsdev_time, ds->nfsdev_deviceid, sizeof(nfsdev_time)); 7951 new_devid++; 7952 NFSBCOPY(&new_devid, &ds->nfsdev_deviceid[sizeof(nfsdev_time)], 7953 sizeof(new_devid)); 7954 7955 /* 7956 * Fill in the file addr (actually the nfsv4_file_layout_ds_addr4 7957 * as defined in RFC5661) in XDR. 7958 */ 7959 addrlen = NFSM_RNDUP(strlen(addr)) + NFSM_RNDUP(strlen(netprot)) + 7960 6 * NFSX_UNSIGNED; 7961 NFSD_DEBUG(4, "hn=%s addr=%s netprot=%s\n", dnshost, addr, netprot); 7962 ds->nfsdev_fileaddrlen = addrlen; 7963 tl = malloc(addrlen, M_NFSDSTATE, M_WAITOK | M_ZERO); 7964 ds->nfsdev_fileaddr = (char *)tl; 7965 *tl++ = txdr_unsigned(1); /* One stripe with index 0. */ 7966 *tl++ = 0; 7967 *tl++ = txdr_unsigned(1); /* One multipath list */ 7968 *tl++ = txdr_unsigned(1); /* with one entry in it. */ 7969 /* The netaddr for this one entry. */ 7970 *tl++ = txdr_unsigned(strlen(netprot)); 7971 NFSBCOPY(netprot, tl, strlen(netprot)); 7972 tl += (NFSM_RNDUP(strlen(netprot)) / NFSX_UNSIGNED); 7973 *tl++ = txdr_unsigned(strlen(addr)); 7974 NFSBCOPY(addr, tl, strlen(addr)); 7975 7976 /* 7977 * Fill in the flex file addr (actually the ff_device_addr4 7978 * as defined for Flexible File Layout) in XDR. 7979 */ 7980 addrlen = NFSM_RNDUP(strlen(addr)) + NFSM_RNDUP(strlen(netprot)) + 7981 14 * NFSX_UNSIGNED; 7982 ds->nfsdev_flexaddrlen = addrlen; 7983 tl = malloc(addrlen, M_NFSDSTATE, M_WAITOK | M_ZERO); 7984 ds->nfsdev_flexaddr = (char *)tl; 7985 *tl++ = txdr_unsigned(1); /* One multipath entry. */ 7986 /* The netaddr for this one entry. */ 7987 *tl++ = txdr_unsigned(strlen(netprot)); 7988 NFSBCOPY(netprot, tl, strlen(netprot)); 7989 tl += (NFSM_RNDUP(strlen(netprot)) / NFSX_UNSIGNED); 7990 *tl++ = txdr_unsigned(strlen(addr)); 7991 NFSBCOPY(addr, tl, strlen(addr)); 7992 tl += (NFSM_RNDUP(strlen(addr)) / NFSX_UNSIGNED); 7993 *tl++ = txdr_unsigned(2); /* Two NFS Versions. */ 7994 *tl++ = txdr_unsigned(NFS_VER4); /* NFSv4. */ 7995 *tl++ = txdr_unsigned(NFSV42_MINORVERSION); /* Minor version 2. */ 7996 *tl++ = txdr_unsigned(nfs_srvmaxio); /* DS max rsize. */ 7997 *tl++ = txdr_unsigned(nfs_srvmaxio); /* DS max wsize. */ 7998 *tl++ = newnfs_true; /* Tightly coupled. */ 7999 *tl++ = txdr_unsigned(NFS_VER4); /* NFSv4. */ 8000 *tl++ = txdr_unsigned(NFSV41_MINORVERSION); /* Minor version 1. */ 8001 *tl++ = txdr_unsigned(nfs_srvmaxio); /* DS max rsize. */ 8002 *tl++ = txdr_unsigned(nfs_srvmaxio); /* DS max wsize. */ 8003 *tl = newnfs_true; /* Tightly coupled. */ 8004 8005 ds->nfsdev_hostnamelen = strlen(dnshost); 8006 ds->nfsdev_host = malloc(ds->nfsdev_hostnamelen + 1, M_NFSDSTATE, 8007 M_WAITOK); 8008 NFSBCOPY(dnshost, ds->nfsdev_host, ds->nfsdev_hostnamelen + 1); 8009 } 8010 8011 /* 8012 * Create the device id list. 8013 * Return 0 if the nfsd threads are to run and ENXIO if the "-p" argument 8014 * is misconfigured. 8015 */ 8016 int 8017 nfsrv_createdevids(struct nfsd_nfsd_args *args, NFSPROC_T *p) 8018 { 8019 struct nfsdevice *ds; 8020 char *addrp, *dnshostp, *dspathp, *mdspathp; 8021 int error, i; 8022 8023 addrp = args->addr; 8024 dnshostp = args->dnshost; 8025 dspathp = args->dspath; 8026 mdspathp = args->mdspath; 8027 nfsrv_maxpnfsmirror = args->mirrorcnt; 8028 if (addrp == NULL || dnshostp == NULL || dspathp == NULL || 8029 mdspathp == NULL) 8030 return (0); 8031 8032 /* 8033 * Loop around for each nul-terminated string in args->addr, 8034 * args->dnshost, args->dnspath and args->mdspath. 8035 */ 8036 while (addrp < (args->addr + args->addrlen) && 8037 dnshostp < (args->dnshost + args->dnshostlen) && 8038 dspathp < (args->dspath + args->dspathlen) && 8039 mdspathp < (args->mdspath + args->mdspathlen)) { 8040 error = nfsrv_setdsserver(dspathp, mdspathp, p, &ds); 8041 if (error != 0) { 8042 /* Free all DS servers. */ 8043 nfsrv_freealldevids(); 8044 nfsrv_devidcnt = 0; 8045 return (ENXIO); 8046 } 8047 nfsrv_allocdevid(ds, addrp, dnshostp); 8048 addrp += (strlen(addrp) + 1); 8049 dnshostp += (strlen(dnshostp) + 1); 8050 dspathp += (strlen(dspathp) + 1); 8051 mdspathp += (strlen(mdspathp) + 1); 8052 } 8053 if (nfsrv_devidcnt < nfsrv_maxpnfsmirror) { 8054 /* Free all DS servers. */ 8055 nfsrv_freealldevids(); 8056 nfsrv_devidcnt = 0; 8057 nfsrv_maxpnfsmirror = 1; 8058 return (ENXIO); 8059 } 8060 /* We can fail at most one less DS than the mirror level. */ 8061 nfsrv_faildscnt = nfsrv_maxpnfsmirror - 1; 8062 8063 /* 8064 * Allocate the nfslayout hash table now, since this is a pNFS server. 8065 * Make it 1% of the high water mark and at least 100. 8066 */ 8067 if (nfslayouthash == NULL) { 8068 nfsrv_layouthashsize = nfsrv_layouthighwater / 100; 8069 if (nfsrv_layouthashsize < 100) 8070 nfsrv_layouthashsize = 100; 8071 nfslayouthash = mallocarray(nfsrv_layouthashsize, 8072 sizeof(struct nfslayouthash), M_NFSDSESSION, M_WAITOK | 8073 M_ZERO); 8074 for (i = 0; i < nfsrv_layouthashsize; i++) { 8075 mtx_init(&nfslayouthash[i].mtx, "nfslm", NULL, MTX_DEF); 8076 TAILQ_INIT(&nfslayouthash[i].list); 8077 } 8078 } 8079 return (0); 8080 } 8081 8082 /* 8083 * Free all device ids. 8084 */ 8085 static void 8086 nfsrv_freealldevids(void) 8087 { 8088 struct nfsdevice *ds, *nds; 8089 8090 TAILQ_FOREACH_SAFE(ds, &nfsrv_devidhead, nfsdev_list, nds) 8091 nfsrv_freedevid(ds); 8092 } 8093 8094 /* 8095 * Check to see if there is a Read/Write Layout plus either: 8096 * - A Write Delegation 8097 * or 8098 * - An Open with Write_access. 8099 * Return 1 if this is the case and 0 otherwise. 8100 * This function is used by nfsrv_proxyds() to decide if doing a Proxy 8101 * Getattr RPC to the Data Server (DS) is necessary. 8102 */ 8103 #define NFSCLIDVECSIZE 6 8104 int 8105 nfsrv_checkdsattr(vnode_t vp, NFSPROC_T *p) 8106 { 8107 fhandle_t fh, *tfhp; 8108 struct nfsstate *stp; 8109 struct nfslayout *lyp; 8110 struct nfslayouthash *lhyp; 8111 struct nfslockhashhead *hp; 8112 struct nfslockfile *lfp; 8113 nfsquad_t clid[NFSCLIDVECSIZE]; 8114 int clidcnt, ret; 8115 8116 ret = nfsvno_getfh(vp, &fh, p); 8117 if (ret != 0) 8118 return (0); 8119 8120 /* First check for a Read/Write Layout. */ 8121 clidcnt = 0; 8122 lhyp = NFSLAYOUTHASH(&fh); 8123 NFSLOCKLAYOUT(lhyp); 8124 TAILQ_FOREACH(lyp, &lhyp->list, lay_list) { 8125 if (NFSBCMP(&lyp->lay_fh, &fh, sizeof(fh)) == 0 && 8126 ((lyp->lay_flags & NFSLAY_RW) != 0 || 8127 ((lyp->lay_flags & NFSLAY_READ) != 0 && 8128 nfsrv_pnfsatime != 0))) { 8129 if (clidcnt < NFSCLIDVECSIZE) 8130 clid[clidcnt].qval = lyp->lay_clientid.qval; 8131 clidcnt++; 8132 } 8133 } 8134 NFSUNLOCKLAYOUT(lhyp); 8135 if (clidcnt == 0) { 8136 /* None found, so return 0. */ 8137 return (0); 8138 } 8139 8140 /* Get the nfslockfile for this fh. */ 8141 NFSLOCKSTATE(); 8142 hp = NFSLOCKHASH(&fh); 8143 LIST_FOREACH(lfp, hp, lf_hash) { 8144 tfhp = &lfp->lf_fh; 8145 if (NFSVNO_CMPFH(&fh, tfhp)) 8146 break; 8147 } 8148 if (lfp == NULL) { 8149 /* None found, so return 0. */ 8150 NFSUNLOCKSTATE(); 8151 return (0); 8152 } 8153 8154 /* Now, look for a Write delegation for this clientid. */ 8155 LIST_FOREACH(stp, &lfp->lf_deleg, ls_file) { 8156 if ((stp->ls_flags & NFSLCK_DELEGWRITE) != 0 && 8157 nfsrv_fndclid(clid, stp->ls_clp->lc_clientid, clidcnt) != 0) 8158 break; 8159 } 8160 if (stp != NULL) { 8161 /* Found one, so return 1. */ 8162 NFSUNLOCKSTATE(); 8163 return (1); 8164 } 8165 8166 /* No Write delegation, so look for an Open with Write_access. */ 8167 LIST_FOREACH(stp, &lfp->lf_open, ls_file) { 8168 KASSERT((stp->ls_flags & NFSLCK_OPEN) != 0, 8169 ("nfsrv_checkdsattr: Non-open in Open list\n")); 8170 if ((stp->ls_flags & NFSLCK_WRITEACCESS) != 0 && 8171 nfsrv_fndclid(clid, stp->ls_clp->lc_clientid, clidcnt) != 0) 8172 break; 8173 } 8174 NFSUNLOCKSTATE(); 8175 if (stp != NULL) 8176 return (1); 8177 return (0); 8178 } 8179 8180 /* 8181 * Look for a matching clientid in the vector. Return 1 if one might match. 8182 */ 8183 static int 8184 nfsrv_fndclid(nfsquad_t *clidvec, nfsquad_t clid, int clidcnt) 8185 { 8186 int i; 8187 8188 /* If too many for the vector, return 1 since there might be a match. */ 8189 if (clidcnt > NFSCLIDVECSIZE) 8190 return (1); 8191 8192 for (i = 0; i < clidcnt; i++) 8193 if (clidvec[i].qval == clid.qval) 8194 return (1); 8195 return (0); 8196 } 8197 8198 /* 8199 * Check the don't list for "vp" and see if issuing an rw layout is allowed. 8200 * Return 1 if issuing an rw layout isn't allowed, 0 otherwise. 8201 */ 8202 static int 8203 nfsrv_dontlayout(fhandle_t *fhp) 8204 { 8205 struct nfsdontlist *mrp; 8206 int ret; 8207 8208 if (nfsrv_dontlistlen == 0) 8209 return (0); 8210 ret = 0; 8211 NFSDDONTLISTLOCK(); 8212 LIST_FOREACH(mrp, &nfsrv_dontlisthead, nfsmr_list) { 8213 if (NFSBCMP(fhp, &mrp->nfsmr_fh, sizeof(*fhp)) == 0 && 8214 (mrp->nfsmr_flags & NFSMR_DONTLAYOUT) != 0) { 8215 ret = 1; 8216 break; 8217 } 8218 } 8219 NFSDDONTLISTUNLOCK(); 8220 return (ret); 8221 } 8222 8223 #define PNFSDS_COPYSIZ 65536 8224 /* 8225 * Create a new file on a DS and copy the contents of an extant DS file to it. 8226 * This can be used for recovery of a DS file onto a recovered DS. 8227 * The steps are: 8228 * - When called, the MDS file's vnode is locked, blocking LayoutGet operations. 8229 * - Disable issuing of read/write layouts for the file via the nfsdontlist, 8230 * so that they will be disabled after the MDS file's vnode is unlocked. 8231 * - Set up the nfsrv_recalllist so that recall of read/write layouts can 8232 * be done. 8233 * - Unlock the MDS file's vnode, so that the client(s) can perform proxied 8234 * writes, LayoutCommits and LayoutReturns for the file when completing the 8235 * LayoutReturn requested by the LayoutRecall callback. 8236 * - Issue a LayoutRecall callback for all read/write layouts and wait for 8237 * them to be returned. (If the LayoutRecall callback replies 8238 * NFSERR_NOMATCHLAYOUT, they are gone and no LayoutReturn is needed.) 8239 * - Exclusively lock the MDS file's vnode. This ensures that no proxied 8240 * writes are in progress or can occur during the DS file copy. 8241 * It also blocks Setattr operations. 8242 * - Create the file on the recovered mirror. 8243 * - Copy the file from the operational DS. 8244 * - Copy any ACL from the MDS file to the new DS file. 8245 * - Set the modify time of the new DS file to that of the MDS file. 8246 * - Update the extended attribute for the MDS file. 8247 * - Enable issuing of rw layouts by deleting the nfsdontlist entry. 8248 * - The caller will unlock the MDS file's vnode allowing operations 8249 * to continue normally, since it is now on the mirror again. 8250 */ 8251 int 8252 nfsrv_copymr(vnode_t vp, vnode_t fvp, vnode_t dvp, struct nfsdevice *ds, 8253 struct pnfsdsfile *pf, struct pnfsdsfile *wpf, int mirrorcnt, 8254 struct ucred *cred, NFSPROC_T *p) 8255 { 8256 struct nfsdontlist *mrp, *nmrp; 8257 struct nfslayouthash *lhyp; 8258 struct nfslayout *lyp, *nlyp; 8259 struct nfslayouthead thl; 8260 struct mount *mp, *tvmp; 8261 struct acl *aclp; 8262 struct vattr va; 8263 struct timespec mtime; 8264 fhandle_t fh; 8265 vnode_t tvp; 8266 off_t rdpos, wrpos; 8267 ssize_t aresid; 8268 char *dat; 8269 int didprintf, ret, retacl, xfer; 8270 8271 ASSERT_VOP_LOCKED(fvp, "nfsrv_copymr fvp"); 8272 ASSERT_VOP_LOCKED(vp, "nfsrv_copymr vp"); 8273 /* 8274 * Allocate a nfsdontlist entry and set the NFSMR_DONTLAYOUT flag 8275 * so that no more RW layouts will get issued. 8276 */ 8277 ret = nfsvno_getfh(vp, &fh, p); 8278 if (ret != 0) { 8279 NFSD_DEBUG(4, "nfsrv_copymr: getfh=%d\n", ret); 8280 return (ret); 8281 } 8282 nmrp = malloc(sizeof(*nmrp), M_NFSDSTATE, M_WAITOK); 8283 nmrp->nfsmr_flags = NFSMR_DONTLAYOUT; 8284 NFSBCOPY(&fh, &nmrp->nfsmr_fh, sizeof(fh)); 8285 NFSDDONTLISTLOCK(); 8286 LIST_FOREACH(mrp, &nfsrv_dontlisthead, nfsmr_list) { 8287 if (NFSBCMP(&fh, &mrp->nfsmr_fh, sizeof(fh)) == 0) 8288 break; 8289 } 8290 if (mrp == NULL) { 8291 LIST_INSERT_HEAD(&nfsrv_dontlisthead, nmrp, nfsmr_list); 8292 mrp = nmrp; 8293 nmrp = NULL; 8294 nfsrv_dontlistlen++; 8295 NFSD_DEBUG(4, "nfsrv_copymr: in dontlist\n"); 8296 } else { 8297 NFSDDONTLISTUNLOCK(); 8298 free(nmrp, M_NFSDSTATE); 8299 NFSD_DEBUG(4, "nfsrv_copymr: dup dontlist\n"); 8300 return (ENXIO); 8301 } 8302 NFSDDONTLISTUNLOCK(); 8303 8304 /* 8305 * Search for all RW layouts for this file. Move them to the 8306 * recall list, so they can be recalled and their return noted. 8307 */ 8308 lhyp = NFSLAYOUTHASH(&fh); 8309 NFSDRECALLLOCK(); 8310 NFSLOCKLAYOUT(lhyp); 8311 TAILQ_FOREACH_SAFE(lyp, &lhyp->list, lay_list, nlyp) { 8312 if (NFSBCMP(&lyp->lay_fh, &fh, sizeof(fh)) == 0 && 8313 (lyp->lay_flags & NFSLAY_RW) != 0) { 8314 TAILQ_REMOVE(&lhyp->list, lyp, lay_list); 8315 TAILQ_INSERT_HEAD(&nfsrv_recalllisthead, lyp, lay_list); 8316 lyp->lay_trycnt = 0; 8317 } 8318 } 8319 NFSUNLOCKLAYOUT(lhyp); 8320 NFSDRECALLUNLOCK(); 8321 8322 ret = 0; 8323 mp = tvmp = NULL; 8324 didprintf = 0; 8325 TAILQ_INIT(&thl); 8326 /* Unlock the MDS vp, so that a LayoutReturn can be done on it. */ 8327 NFSVOPUNLOCK(vp); 8328 /* Now, do a recall for all layouts not yet recalled. */ 8329 tryagain: 8330 NFSDRECALLLOCK(); 8331 TAILQ_FOREACH(lyp, &nfsrv_recalllisthead, lay_list) { 8332 if (NFSBCMP(&lyp->lay_fh, &fh, sizeof(fh)) == 0 && 8333 (lyp->lay_flags & NFSLAY_RECALL) == 0) { 8334 lyp->lay_flags |= NFSLAY_RECALL; 8335 /* 8336 * The layout stateid.seqid needs to be incremented 8337 * before doing a LAYOUT_RECALL callback. 8338 */ 8339 if (++lyp->lay_stateid.seqid == 0) 8340 lyp->lay_stateid.seqid = 1; 8341 NFSDRECALLUNLOCK(); 8342 nfsrv_recalllayout(lyp->lay_clientid, &lyp->lay_stateid, 8343 &lyp->lay_fh, lyp, 0, lyp->lay_type, p); 8344 NFSD_DEBUG(4, "nfsrv_copymr: recalled layout\n"); 8345 goto tryagain; 8346 } 8347 } 8348 8349 /* Now wait for them to be returned. */ 8350 tryagain2: 8351 TAILQ_FOREACH(lyp, &nfsrv_recalllisthead, lay_list) { 8352 if (NFSBCMP(&lyp->lay_fh, &fh, sizeof(fh)) == 0) { 8353 if ((lyp->lay_flags & NFSLAY_RETURNED) != 0) { 8354 TAILQ_REMOVE(&nfsrv_recalllisthead, lyp, 8355 lay_list); 8356 TAILQ_INSERT_HEAD(&thl, lyp, lay_list); 8357 NFSD_DEBUG(4, 8358 "nfsrv_copymr: layout returned\n"); 8359 } else { 8360 lyp->lay_trycnt++; 8361 ret = mtx_sleep(lyp, NFSDRECALLMUTEXPTR, 8362 PVFS | PCATCH, "nfsmrl", hz); 8363 NFSD_DEBUG(4, "nfsrv_copymr: aft sleep=%d\n", 8364 ret); 8365 if (ret == EINTR || ret == ERESTART) 8366 break; 8367 if ((lyp->lay_flags & NFSLAY_RETURNED) == 0) { 8368 /* 8369 * Give up after 60sec and return 8370 * ENXIO, failing the copymr. 8371 * This layout will remain on the 8372 * recalllist. It can only be cleared 8373 * by restarting the nfsd. 8374 * This seems the safe way to handle 8375 * it, since it cannot be safely copied 8376 * with an outstanding RW layout. 8377 */ 8378 if (lyp->lay_trycnt >= 60) { 8379 ret = ENXIO; 8380 break; 8381 } 8382 if (didprintf == 0) { 8383 printf("nfsrv_copymr: layout " 8384 "not returned\n"); 8385 didprintf = 1; 8386 } 8387 } 8388 } 8389 goto tryagain2; 8390 } 8391 } 8392 NFSDRECALLUNLOCK(); 8393 /* We can now get rid of the layouts that have been returned. */ 8394 TAILQ_FOREACH_SAFE(lyp, &thl, lay_list, nlyp) 8395 nfsrv_freelayout(&thl, lyp); 8396 8397 /* 8398 * Do the vn_start_write() calls here, before the MDS vnode is 8399 * locked and the tvp is created (locked) in the NFS file system 8400 * that dvp is in. 8401 * For tvmp, this probably isn't necessary, since it will be an 8402 * NFS mount and they are not suspendable at this time. 8403 */ 8404 if (ret == 0) 8405 ret = vn_start_write(vp, &mp, V_WAIT | PCATCH); 8406 if (ret == 0) { 8407 tvmp = dvp->v_mount; 8408 ret = vn_start_write(NULL, &tvmp, V_WAIT | PCATCH); 8409 } 8410 8411 /* 8412 * LK_EXCLUSIVE lock the MDS vnode, so that any 8413 * proxied writes through the MDS will be blocked until we have 8414 * completed the copy and update of the extended attributes. 8415 * This will also ensure that any attributes and ACL will not be 8416 * changed until the copy is complete. 8417 */ 8418 NFSVOPLOCK(vp, LK_EXCLUSIVE | LK_RETRY); 8419 if (ret == 0 && VN_IS_DOOMED(vp)) { 8420 NFSD_DEBUG(4, "nfsrv_copymr: lk_exclusive doomed\n"); 8421 ret = ESTALE; 8422 } 8423 8424 /* Create the data file on the recovered DS. */ 8425 if (ret == 0) 8426 ret = nfsrv_createdsfile(vp, &fh, pf, dvp, ds, cred, p, &tvp); 8427 8428 /* Copy the DS file, if created successfully. */ 8429 if (ret == 0) { 8430 /* 8431 * Get any NFSv4 ACL on the MDS file, so that it can be set 8432 * on the new DS file. 8433 */ 8434 aclp = acl_alloc(M_WAITOK | M_ZERO); 8435 retacl = VOP_GETACL(vp, ACL_TYPE_NFS4, aclp, cred, p); 8436 if (retacl != 0 && retacl != ENOATTR) 8437 NFSD_DEBUG(1, "nfsrv_copymr: vop_getacl=%d\n", retacl); 8438 dat = malloc(PNFSDS_COPYSIZ, M_TEMP, M_WAITOK); 8439 /* Malloc a block of 0s used to check for holes. */ 8440 if (nfsrv_zeropnfsdat == NULL) 8441 nfsrv_zeropnfsdat = malloc(PNFSDS_COPYSIZ, M_TEMP, 8442 M_WAITOK | M_ZERO); 8443 rdpos = wrpos = 0; 8444 ret = VOP_GETATTR(fvp, &va, cred); 8445 aresid = 0; 8446 while (ret == 0 && aresid == 0) { 8447 ret = vn_rdwr(UIO_READ, fvp, dat, PNFSDS_COPYSIZ, 8448 rdpos, UIO_SYSSPACE, IO_NODELOCKED, cred, NULL, 8449 &aresid, p); 8450 xfer = PNFSDS_COPYSIZ - aresid; 8451 if (ret == 0 && xfer > 0) { 8452 rdpos += xfer; 8453 /* 8454 * Skip the write for holes, except for the 8455 * last block. 8456 */ 8457 if (xfer < PNFSDS_COPYSIZ || rdpos == 8458 va.va_size || NFSBCMP(dat, 8459 nfsrv_zeropnfsdat, PNFSDS_COPYSIZ) != 0) 8460 ret = vn_rdwr(UIO_WRITE, tvp, dat, xfer, 8461 wrpos, UIO_SYSSPACE, IO_NODELOCKED, 8462 cred, NULL, NULL, p); 8463 if (ret == 0) 8464 wrpos += xfer; 8465 } 8466 } 8467 8468 /* If there is an ACL and the copy succeeded, set the ACL. */ 8469 if (ret == 0 && retacl == 0) { 8470 ret = VOP_SETACL(tvp, ACL_TYPE_NFS4, aclp, cred, p); 8471 /* 8472 * Don't consider these as errors, since VOP_GETACL() 8473 * can return an ACL when they are not actually 8474 * supported. For example, for UFS, VOP_GETACL() 8475 * will return a trivial ACL based on the uid/gid/mode 8476 * when there is no ACL on the file. 8477 * This case should be recognized as a trivial ACL 8478 * by UFS's VOP_SETACL() and succeed, but... 8479 */ 8480 if (ret == ENOATTR || ret == EOPNOTSUPP || ret == EPERM) 8481 ret = 0; 8482 } 8483 8484 if (ret == 0) 8485 ret = VOP_FSYNC(tvp, MNT_WAIT, p); 8486 8487 /* Set the DS data file's modify time that of the MDS file. */ 8488 if (ret == 0) 8489 ret = VOP_GETATTR(vp, &va, cred); 8490 if (ret == 0) { 8491 mtime = va.va_mtime; 8492 VATTR_NULL(&va); 8493 va.va_mtime = mtime; 8494 ret = VOP_SETATTR(tvp, &va, cred); 8495 } 8496 8497 vput(tvp); 8498 acl_free(aclp); 8499 free(dat, M_TEMP); 8500 } 8501 if (tvmp != NULL) 8502 vn_finished_write(tvmp); 8503 8504 /* Update the extended attributes for the newly created DS file. */ 8505 if (ret == 0) 8506 ret = vn_extattr_set(vp, IO_NODELOCKED, 8507 EXTATTR_NAMESPACE_SYSTEM, "pnfsd.dsfile", 8508 sizeof(*wpf) * mirrorcnt, (char *)wpf, p); 8509 if (mp != NULL) 8510 vn_finished_write(mp); 8511 8512 /* Get rid of the dontlist entry, so that Layouts can be issued. */ 8513 NFSDDONTLISTLOCK(); 8514 LIST_REMOVE(mrp, nfsmr_list); 8515 NFSDDONTLISTUNLOCK(); 8516 free(mrp, M_NFSDSTATE); 8517 return (ret); 8518 } 8519 8520 /* 8521 * Create a data storage file on the recovered DS. 8522 */ 8523 static int 8524 nfsrv_createdsfile(vnode_t vp, fhandle_t *fhp, struct pnfsdsfile *pf, 8525 vnode_t dvp, struct nfsdevice *ds, struct ucred *cred, NFSPROC_T *p, 8526 vnode_t *tvpp) 8527 { 8528 struct vattr va, nva; 8529 int error; 8530 8531 /* Make data file name based on FH. */ 8532 error = VOP_GETATTR(vp, &va, cred); 8533 if (error == 0) { 8534 /* Set the attributes for "vp" to Setattr the DS vp. */ 8535 VATTR_NULL(&nva); 8536 nva.va_uid = va.va_uid; 8537 nva.va_gid = va.va_gid; 8538 nva.va_mode = va.va_mode; 8539 nva.va_size = 0; 8540 VATTR_NULL(&va); 8541 va.va_type = VREG; 8542 va.va_mode = nva.va_mode; 8543 NFSD_DEBUG(4, "nfsrv_dscreatefile: dvp=%p pf=%p\n", dvp, pf); 8544 error = nfsrv_dscreate(dvp, &va, &nva, fhp, pf, NULL, 8545 pf->dsf_filename, cred, p, tvpp); 8546 } 8547 return (error); 8548 } 8549 8550 /* 8551 * Look up the MDS file shared locked, and then get the extended attribute 8552 * to find the extant DS file to be copied to the new mirror. 8553 * If successful, *vpp is set to the MDS file's vp and *nvpp is 8554 * set to a DS data file for the MDS file, both exclusively locked. 8555 * The "buf" argument has the pnfsdsfile structure from the MDS file 8556 * in it and buflen is set to its length. 8557 */ 8558 int 8559 nfsrv_mdscopymr(char *mdspathp, char *dspathp, char *curdspathp, char *buf, 8560 int *buflenp, char *fname, NFSPROC_T *p, struct vnode **vpp, 8561 struct vnode **nvpp, struct pnfsdsfile **pfp, struct nfsdevice **dsp, 8562 struct nfsdevice **fdsp) 8563 { 8564 struct nameidata nd; 8565 struct vnode *vp, *curvp; 8566 struct pnfsdsfile *pf; 8567 struct nfsmount *nmp, *curnmp; 8568 int dsdir, error, mirrorcnt, ippos; 8569 8570 vp = NULL; 8571 curvp = NULL; 8572 curnmp = NULL; 8573 *dsp = NULL; 8574 *fdsp = NULL; 8575 if (dspathp == NULL && curdspathp != NULL) 8576 return (EPERM); 8577 8578 /* 8579 * Look up the MDS file shared locked. The lock will be upgraded 8580 * to an exclusive lock after any rw layouts have been returned. 8581 */ 8582 NFSD_DEBUG(4, "mdsopen path=%s\n", mdspathp); 8583 NDINIT(&nd, LOOKUP, FOLLOW | LOCKSHARED | LOCKLEAF, UIO_SYSSPACE, 8584 mdspathp); 8585 error = namei(&nd); 8586 NFSD_DEBUG(4, "lookup=%d\n", error); 8587 if (error != 0) 8588 return (error); 8589 if (nd.ni_vp->v_type != VREG) { 8590 vput(nd.ni_vp); 8591 NFSD_DEBUG(4, "mdspath not reg\n"); 8592 return (EISDIR); 8593 } 8594 vp = nd.ni_vp; 8595 8596 if (curdspathp != NULL) { 8597 /* 8598 * Look up the current DS path and find the nfsdev structure for 8599 * it. 8600 */ 8601 NFSD_DEBUG(4, "curmdsdev path=%s\n", curdspathp); 8602 NDINIT(&nd, LOOKUP, FOLLOW | LOCKSHARED | LOCKLEAF, 8603 UIO_SYSSPACE, curdspathp); 8604 error = namei(&nd); 8605 NFSD_DEBUG(4, "ds lookup=%d\n", error); 8606 if (error != 0) { 8607 vput(vp); 8608 return (error); 8609 } 8610 if (nd.ni_vp->v_type != VDIR) { 8611 vput(nd.ni_vp); 8612 vput(vp); 8613 NFSD_DEBUG(4, "curdspath not dir\n"); 8614 return (ENOTDIR); 8615 } 8616 if (strcmp(nd.ni_vp->v_mount->mnt_vfc->vfc_name, "nfs") != 0) { 8617 vput(nd.ni_vp); 8618 vput(vp); 8619 NFSD_DEBUG(4, "curdspath not an NFS mount\n"); 8620 return (ENXIO); 8621 } 8622 curnmp = VFSTONFS(nd.ni_vp->v_mount); 8623 8624 /* Search the nfsdev list for a match. */ 8625 NFSDDSLOCK(); 8626 *fdsp = nfsv4_findmirror(curnmp); 8627 NFSDDSUNLOCK(); 8628 if (*fdsp == NULL) 8629 curnmp = NULL; 8630 if (curnmp == NULL) { 8631 vput(nd.ni_vp); 8632 vput(vp); 8633 NFSD_DEBUG(4, "mdscopymr: no current ds\n"); 8634 return (ENXIO); 8635 } 8636 curvp = nd.ni_vp; 8637 } 8638 8639 if (dspathp != NULL) { 8640 /* Look up the nfsdev path and find the nfsdev structure. */ 8641 NFSD_DEBUG(4, "mdsdev path=%s\n", dspathp); 8642 NDINIT(&nd, LOOKUP, FOLLOW | LOCKSHARED | LOCKLEAF, 8643 UIO_SYSSPACE, dspathp); 8644 error = namei(&nd); 8645 NFSD_DEBUG(4, "ds lookup=%d\n", error); 8646 if (error != 0) { 8647 vput(vp); 8648 if (curvp != NULL) 8649 vput(curvp); 8650 return (error); 8651 } 8652 if (nd.ni_vp->v_type != VDIR || nd.ni_vp == curvp) { 8653 vput(nd.ni_vp); 8654 vput(vp); 8655 if (curvp != NULL) 8656 vput(curvp); 8657 NFSD_DEBUG(4, "dspath not dir\n"); 8658 if (nd.ni_vp == curvp) 8659 return (EPERM); 8660 return (ENOTDIR); 8661 } 8662 if (strcmp(nd.ni_vp->v_mount->mnt_vfc->vfc_name, "nfs") != 0) { 8663 vput(nd.ni_vp); 8664 vput(vp); 8665 if (curvp != NULL) 8666 vput(curvp); 8667 NFSD_DEBUG(4, "dspath not an NFS mount\n"); 8668 return (ENXIO); 8669 } 8670 nmp = VFSTONFS(nd.ni_vp->v_mount); 8671 8672 /* 8673 * Search the nfsdevice list for a match. If curnmp == NULL, 8674 * this is a recovery and there must be a mirror. 8675 */ 8676 NFSDDSLOCK(); 8677 if (curnmp == NULL) 8678 *dsp = nfsrv_findmirroredds(nmp); 8679 else 8680 *dsp = nfsv4_findmirror(nmp); 8681 NFSDDSUNLOCK(); 8682 if (*dsp == NULL) { 8683 vput(nd.ni_vp); 8684 vput(vp); 8685 if (curvp != NULL) 8686 vput(curvp); 8687 NFSD_DEBUG(4, "mdscopymr: no ds\n"); 8688 return (ENXIO); 8689 } 8690 } else { 8691 nd.ni_vp = NULL; 8692 nmp = NULL; 8693 } 8694 8695 /* 8696 * Get a vp for an available DS data file using the extended 8697 * attribute on the MDS file. 8698 * If there is a valid entry for the new DS in the extended attribute 8699 * on the MDS file (as checked via the nmp argument), 8700 * nfsrv_dsgetsockmnt() returns EEXIST, so no copying will occur. 8701 */ 8702 error = nfsrv_dsgetsockmnt(vp, 0, buf, buflenp, &mirrorcnt, p, 8703 NULL, NULL, NULL, fname, nvpp, &nmp, curnmp, &ippos, &dsdir); 8704 if (curvp != NULL) 8705 vput(curvp); 8706 if (nd.ni_vp == NULL) { 8707 if (error == 0 && nmp != NULL) { 8708 /* Search the nfsdev list for a match. */ 8709 NFSDDSLOCK(); 8710 *dsp = nfsrv_findmirroredds(nmp); 8711 NFSDDSUNLOCK(); 8712 } 8713 if (error == 0 && (nmp == NULL || *dsp == NULL)) { 8714 if (nvpp != NULL && *nvpp != NULL) { 8715 vput(*nvpp); 8716 *nvpp = NULL; 8717 } 8718 error = ENXIO; 8719 } 8720 } else 8721 vput(nd.ni_vp); 8722 8723 /* 8724 * When dspathp != NULL and curdspathp == NULL, this is a recovery 8725 * and is only allowed if there is a 0.0.0.0 IP address entry. 8726 * When curdspathp != NULL, the ippos will be set to that entry. 8727 */ 8728 if (error == 0 && dspathp != NULL && ippos == -1) { 8729 if (nvpp != NULL && *nvpp != NULL) { 8730 vput(*nvpp); 8731 *nvpp = NULL; 8732 } 8733 error = ENXIO; 8734 } 8735 if (error == 0) { 8736 *vpp = vp; 8737 8738 pf = (struct pnfsdsfile *)buf; 8739 if (ippos == -1) { 8740 /* If no zeroip pnfsdsfile, add one. */ 8741 ippos = *buflenp / sizeof(*pf); 8742 *buflenp += sizeof(*pf); 8743 pf += ippos; 8744 pf->dsf_dir = dsdir; 8745 strlcpy(pf->dsf_filename, fname, 8746 sizeof(pf->dsf_filename)); 8747 } else 8748 pf += ippos; 8749 *pfp = pf; 8750 } else 8751 vput(vp); 8752 return (error); 8753 } 8754 8755 /* 8756 * Search for a matching pnfsd mirror device structure, base on the nmp arg. 8757 * Return one if found, NULL otherwise. 8758 */ 8759 static struct nfsdevice * 8760 nfsrv_findmirroredds(struct nfsmount *nmp) 8761 { 8762 struct nfsdevice *ds, *fndds; 8763 int fndmirror; 8764 8765 mtx_assert(NFSDDSMUTEXPTR, MA_OWNED); 8766 /* 8767 * Search the DS server list for a match with nmp. 8768 * Remove the DS entry if found and there is a mirror. 8769 */ 8770 fndds = NULL; 8771 fndmirror = 0; 8772 if (nfsrv_devidcnt == 0) 8773 return (fndds); 8774 TAILQ_FOREACH(ds, &nfsrv_devidhead, nfsdev_list) { 8775 if (ds->nfsdev_nmp == nmp) { 8776 NFSD_DEBUG(4, "nfsrv_findmirroredds: fnd main ds\n"); 8777 fndds = ds; 8778 break; 8779 } 8780 } 8781 if (fndds == NULL) 8782 return (fndds); 8783 if (fndds->nfsdev_mdsisset == 0 && nfsrv_faildscnt > 0) 8784 fndmirror = 1; 8785 else if (fndds->nfsdev_mdsisset != 0) { 8786 /* For the fsid is set case, search for a mirror. */ 8787 TAILQ_FOREACH(ds, &nfsrv_devidhead, nfsdev_list) { 8788 if (ds != fndds && ds->nfsdev_nmp != NULL && 8789 ds->nfsdev_mdsisset != 0 && 8790 fsidcmp(&ds->nfsdev_mdsfsid, 8791 &fndds->nfsdev_mdsfsid) == 0) { 8792 fndmirror = 1; 8793 break; 8794 } 8795 } 8796 } 8797 if (fndmirror == 0) { 8798 NFSD_DEBUG(4, "nfsrv_findmirroredds: no mirror for DS\n"); 8799 return (NULL); 8800 } 8801 return (fndds); 8802 } 8803 8804 /* 8805 * Mark the appropriate devid and all associated layout as "out of space". 8806 */ 8807 void 8808 nfsrv_marknospc(char *devid, bool setit) 8809 { 8810 struct nfsdevice *ds; 8811 struct nfslayout *lyp; 8812 struct nfslayouthash *lhyp; 8813 int i; 8814 8815 NFSDDSLOCK(); 8816 TAILQ_FOREACH(ds, &nfsrv_devidhead, nfsdev_list) { 8817 if (NFSBCMP(ds->nfsdev_deviceid, devid, NFSX_V4DEVICEID) == 0) { 8818 NFSD_DEBUG(1, "nfsrv_marknospc: devid %d\n", setit); 8819 ds->nfsdev_nospc = setit; 8820 } 8821 } 8822 NFSDDSUNLOCK(); 8823 8824 for (i = 0; i < nfsrv_layouthashsize; i++) { 8825 lhyp = &nfslayouthash[i]; 8826 NFSLOCKLAYOUT(lhyp); 8827 TAILQ_FOREACH(lyp, &lhyp->list, lay_list) { 8828 if (NFSBCMP(lyp->lay_deviceid, devid, 8829 NFSX_V4DEVICEID) == 0) { 8830 NFSD_DEBUG(1, "nfsrv_marknospc: layout %d\n", 8831 setit); 8832 if (setit) 8833 lyp->lay_flags |= NFSLAY_NOSPC; 8834 else 8835 lyp->lay_flags &= ~NFSLAY_NOSPC; 8836 } 8837 } 8838 NFSUNLOCKLAYOUT(lhyp); 8839 } 8840 } 8841