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