2 * Copyright (c) 2009 Rick Macklem, University of Guelph
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
32 #include <fs/nfs/nfsport.h>
34 struct nfsrv_stablefirst nfsrv_stablefirst;
35 int nfsrv_issuedelegs = 0;
36 int nfsrv_dolocallocks = 1;
37 struct nfsv4lock nfsv4rootfs_lock;
39 extern int newnfs_numnfsd;
40 extern struct nfsstats newnfsstats;
41 extern int nfsrv_lease;
42 extern struct timeval nfsboottime;
43 extern u_int32_t newnfs_true, newnfs_false;
48 * Hash and lru lists for nfs V4.
49 * (Some would put them in the .h file, but I don't like declaring storage
52 struct nfsclienthashhead nfsclienthash[NFSCLIENTHASHSIZE];
53 struct nfslockhashhead nfslockhash[NFSLOCKHASHSIZE];
54 #endif /* !APPLEKEXT */
56 static u_int32_t nfsrv_openpluslock = 0, nfsrv_delegatecnt = 0;
57 static time_t nfsrvboottime;
58 static int nfsrv_writedelegifpos = 1;
59 static int nfsrv_returnoldstateid = 0, nfsrv_clients = 0;
60 static int nfsrv_clienthighwater = NFSRV_CLIENTHIGHWATER;
61 static int nfsrv_nogsscallback = 0;
64 static void nfsrv_dumpaclient(struct nfsclient *clp,
65 struct nfsd_dumpclients *dumpp);
66 static void nfsrv_freeopenowner(struct nfsstate *stp, int cansleep,
68 static int nfsrv_freeopen(struct nfsstate *stp, int *freedlockp,
69 int cansleep, NFSPROC_T *p);
70 static int nfsrv_freelockowner(struct nfsstate *stp, int *freedlockp,
71 int cansleep, NFSPROC_T *p);
72 static int nfsrv_freeallnfslocks(struct nfsstate *stp, int *freedlockp,
73 int cansleep, NFSPROC_T *p);
74 static void nfsrv_freenfslock(struct nfslock *lop);
75 static void nfsrv_freenfslockfile(struct nfslockfile *lfp);
76 static void nfsrv_freedeleg(struct nfsstate *);
77 static int nfsrv_getstate(struct nfsclient *clp, nfsv4stateid_t *stateidp,
78 u_int32_t flags, struct nfsstate **stpp);
79 static void nfsrv_getowner(struct nfsstatehead *hp, struct nfsstate *new_stp,
80 struct nfsstate **stpp);
81 static int nfsrv_getlockfh(vnode_t vp, u_short flags,
82 struct nfslockfile **new_lfpp, fhandle_t *nfhp, NFSPROC_T *p);
83 static int nfsrv_getlockfile(u_short flags,
84 struct nfslockfile **new_lfpp, struct nfslockfile **lfpp, fhandle_t *nfhp);
85 static void nfsrv_insertlock(struct nfslock *new_lop,
86 struct nfslock *insert_lop, struct nfsstate *stp, struct nfslockfile *lfp);
87 static void nfsrv_updatelock(struct nfsstate *stp, struct nfslock **new_lopp,
88 struct nfslock **other_lopp, struct nfslockfile *lfp);
89 static int nfsrv_getipnumber(u_char *cp);
90 static int nfsrv_checkrestart(nfsquad_t clientid, u_int32_t flags,
91 nfsv4stateid_t *stateidp, int specialid);
92 static int nfsrv_checkgrace(u_int32_t flags);
93 static int nfsrv_docallback(struct nfsclient *clp, int procnum,
94 nfsv4stateid_t *stateidp, int trunc, fhandle_t *fhp,
95 struct nfsvattr *nap, nfsattrbit_t *attrbitp, NFSPROC_T *p);
96 static u_int32_t nfsrv_nextclientindex(void);
97 static u_int32_t nfsrv_nextstateindex(struct nfsclient *clp);
98 static void nfsrv_markstable(struct nfsclient *clp);
99 static int nfsrv_checkstable(struct nfsclient *clp);
100 static int nfsrv_clientconflict(struct nfsclient *clp, int *haslockp, struct
101 vnode *vp, NFSPROC_T *p);
102 static int nfsrv_delegconflict(struct nfsstate *stp, int *haslockp,
103 NFSPROC_T *p, vnode_t vp);
104 static int nfsrv_cleandeleg(vnode_t vp, struct nfslockfile *lfp,
105 struct nfsclient *clp, int *haslockp, NFSPROC_T *p);
106 static int nfsrv_notsamecredname(struct nfsrv_descript *nd,
107 struct nfsclient *clp);
108 static time_t nfsrv_leaseexpiry(void);
109 static void nfsrv_delaydelegtimeout(struct nfsstate *stp);
110 static int nfsrv_checkseqid(struct nfsrv_descript *nd, u_int32_t seqid,
111 struct nfsstate *stp, struct nfsrvcache *op);
112 static void nfsrv_locallocks(vnode_t vp, struct nfslockfile *lfp,
114 static int nfsrv_nootherstate(struct nfsstate *stp);
117 * Scan the client list for a match and either return the current one,
118 * create a new entry or return an error.
119 * If returning a non-error, the clp structure must either be linked into
120 * the client list or free'd.
123 nfsrv_setclient(struct nfsrv_descript *nd, struct nfsclient **new_clpp,
124 nfsquad_t *clientidp, nfsquad_t *confirmp, NFSPROC_T *p)
126 struct nfsclient *clp = NULL, *new_clp = *new_clpp;
128 struct nfsstate *stp, *tstp;
129 struct sockaddr_in *sad, *rad;
130 int zapit = 0, gotit, hasstate = 0, igotlock;
131 static u_int64_t confirm_index = 0;
134 * Check for state resource limit exceeded.
136 if (nfsrv_openpluslock > NFSRV_V4STATELIMIT)
137 return (NFSERR_RESOURCE);
139 if ((nd->nd_flag & ND_GSS) && nfsrv_nogsscallback)
141 * Don't do callbacks for AUTH_GSS.
142 * (Since these aren't yet debugged, they might cause the
143 * server to crap out, if they get past the Init call to
146 new_clp->lc_program = 0;
148 /* Lock out other nfsd threads */
149 NFSLOCKV4ROOTMUTEX();
150 nfsv4_relref(&nfsv4rootfs_lock);
152 igotlock = nfsv4_lock(&nfsv4rootfs_lock, 1, NULL,
153 NFSV4ROOTLOCKMUTEXPTR);
155 NFSUNLOCKV4ROOTMUTEX();
156 NFSLOCKSTATE(); /* to avoid a race with */
157 NFSUNLOCKSTATE(); /* nfsrv_servertimer() */
160 * Search for a match in the client list.
163 while (i < NFSCLIENTHASHSIZE && !gotit) {
164 LIST_FOREACH(clp, &nfsclienthash[i], lc_hash) {
165 if (new_clp->lc_idlen == clp->lc_idlen &&
166 !NFSBCMP(new_clp->lc_id, clp->lc_id, clp->lc_idlen)) {
174 (clp->lc_flags & (LCL_NEEDSCONFIRM | LCL_ADMINREVOKED))) {
176 * Get rid of the old one.
178 if (i != NFSCLIENTHASHSIZE) {
179 LIST_REMOVE(clp, lc_hash);
180 nfsrv_cleanclient(clp, p);
181 nfsrv_freedeleglist(&clp->lc_deleg);
182 nfsrv_freedeleglist(&clp->lc_olddeleg);
186 * Add it after assigning a client id to it.
188 new_clp->lc_flags |= LCL_NEEDSCONFIRM;
189 confirmp->qval = new_clp->lc_confirm.qval = ++confirm_index;
190 clientidp->lval[0] = new_clp->lc_clientid.lval[0] =
191 (u_int32_t)nfsrvboottime;
192 clientidp->lval[1] = new_clp->lc_clientid.lval[1] =
193 nfsrv_nextclientindex();
194 new_clp->lc_stateindex = 0;
195 new_clp->lc_statemaxindex = 0;
196 new_clp->lc_cbref = 0;
197 new_clp->lc_expiry = nfsrv_leaseexpiry();
198 LIST_INIT(&new_clp->lc_open);
199 LIST_INIT(&new_clp->lc_deleg);
200 LIST_INIT(&new_clp->lc_olddeleg);
201 for (i = 0; i < NFSSTATEHASHSIZE; i++)
202 LIST_INIT(&new_clp->lc_stateid[i]);
203 LIST_INSERT_HEAD(NFSCLIENTHASH(new_clp->lc_clientid), new_clp,
205 newnfsstats.srvclients++;
206 nfsrv_openpluslock++;
208 NFSLOCKV4ROOTMUTEX();
209 nfsv4_unlock(&nfsv4rootfs_lock, 1);
210 NFSUNLOCKV4ROOTMUTEX();
212 nfsrv_zapclient(clp, p);
218 * Now, handle the cases where the id is already issued.
220 if (nfsrv_notsamecredname(nd, clp)) {
222 * Check to see if there is expired state that should go away.
224 if (clp->lc_expiry < NFSD_MONOSEC &&
225 (!LIST_EMPTY(&clp->lc_open) || !LIST_EMPTY(&clp->lc_deleg))) {
226 nfsrv_cleanclient(clp, p);
227 nfsrv_freedeleglist(&clp->lc_deleg);
231 * If there is outstanding state, then reply NFSERR_CLIDINUSE per
232 * RFC3530 Sec. 8.1.2 last para.
234 if (!LIST_EMPTY(&clp->lc_deleg)) {
236 } else if (LIST_EMPTY(&clp->lc_open)) {
240 /* Look for an Open on the OpenOwner */
241 LIST_FOREACH(stp, &clp->lc_open, ls_list) {
242 if (!LIST_EMPTY(&stp->ls_open)) {
250 * If the uid doesn't match, return NFSERR_CLIDINUSE after
251 * filling out the correct ipaddr and portnum.
253 sad = NFSSOCKADDR(new_clp->lc_req.nr_nam, struct sockaddr_in *);
254 rad = NFSSOCKADDR(clp->lc_req.nr_nam, struct sockaddr_in *);
255 sad->sin_addr.s_addr = rad->sin_addr.s_addr;
256 sad->sin_port = rad->sin_port;
257 NFSLOCKV4ROOTMUTEX();
258 nfsv4_unlock(&nfsv4rootfs_lock, 1);
259 NFSUNLOCKV4ROOTMUTEX();
260 return (NFSERR_CLIDINUSE);
264 if (NFSBCMP(new_clp->lc_verf, clp->lc_verf, NFSX_VERF)) {
266 * If the verifier has changed, the client has rebooted
267 * and a new client id is issued. The old state info
268 * can be thrown away once the SETCLIENTID_CONFIRM occurs.
270 LIST_REMOVE(clp, lc_hash);
271 new_clp->lc_flags |= LCL_NEEDSCONFIRM;
272 confirmp->qval = new_clp->lc_confirm.qval = ++confirm_index;
273 clientidp->lval[0] = new_clp->lc_clientid.lval[0] =
275 clientidp->lval[1] = new_clp->lc_clientid.lval[1] =
276 nfsrv_nextclientindex();
277 new_clp->lc_stateindex = 0;
278 new_clp->lc_statemaxindex = 0;
279 new_clp->lc_cbref = 0;
280 new_clp->lc_expiry = nfsrv_leaseexpiry();
283 * Save the state until confirmed.
285 LIST_NEWHEAD(&new_clp->lc_open, &clp->lc_open, ls_list);
286 LIST_FOREACH(tstp, &new_clp->lc_open, ls_list)
287 tstp->ls_clp = new_clp;
288 LIST_NEWHEAD(&new_clp->lc_deleg, &clp->lc_deleg, ls_list);
289 LIST_FOREACH(tstp, &new_clp->lc_deleg, ls_list)
290 tstp->ls_clp = new_clp;
291 LIST_NEWHEAD(&new_clp->lc_olddeleg, &clp->lc_olddeleg,
293 LIST_FOREACH(tstp, &new_clp->lc_olddeleg, ls_list)
294 tstp->ls_clp = new_clp;
295 for (i = 0; i < NFSSTATEHASHSIZE; i++) {
296 LIST_NEWHEAD(&new_clp->lc_stateid[i],
297 &clp->lc_stateid[i], ls_hash);
298 LIST_FOREACH(tstp, &new_clp->lc_stateid[i], ls_list)
299 tstp->ls_clp = new_clp;
301 LIST_INSERT_HEAD(NFSCLIENTHASH(new_clp->lc_clientid), new_clp,
303 newnfsstats.srvclients++;
304 nfsrv_openpluslock++;
306 NFSLOCKV4ROOTMUTEX();
307 nfsv4_unlock(&nfsv4rootfs_lock, 1);
308 NFSUNLOCKV4ROOTMUTEX();
311 * Must wait until any outstanding callback on the old clp
314 while (clp->lc_cbref) {
315 clp->lc_flags |= LCL_WAKEUPWANTED;
316 (void) tsleep((caddr_t)clp, PZERO - 1,
317 "nfsd clp", 10 * hz);
319 nfsrv_zapclient(clp, p);
324 * id and verifier match, so update the net address info
325 * and get rid of any existing callback authentication
326 * handle, so a new one will be acquired.
328 LIST_REMOVE(clp, lc_hash);
329 new_clp->lc_flags |= (LCL_NEEDSCONFIRM | LCL_DONTCLEAN);
330 new_clp->lc_expiry = nfsrv_leaseexpiry();
331 confirmp->qval = new_clp->lc_confirm.qval = ++confirm_index;
332 clientidp->lval[0] = new_clp->lc_clientid.lval[0] =
333 clp->lc_clientid.lval[0];
334 clientidp->lval[1] = new_clp->lc_clientid.lval[1] =
335 clp->lc_clientid.lval[1];
336 new_clp->lc_delegtime = clp->lc_delegtime;
337 new_clp->lc_stateindex = clp->lc_stateindex;
338 new_clp->lc_statemaxindex = clp->lc_statemaxindex;
339 new_clp->lc_cbref = 0;
340 LIST_NEWHEAD(&new_clp->lc_open, &clp->lc_open, ls_list);
341 LIST_FOREACH(tstp, &new_clp->lc_open, ls_list)
342 tstp->ls_clp = new_clp;
343 LIST_NEWHEAD(&new_clp->lc_deleg, &clp->lc_deleg, ls_list);
344 LIST_FOREACH(tstp, &new_clp->lc_deleg, ls_list)
345 tstp->ls_clp = new_clp;
346 LIST_NEWHEAD(&new_clp->lc_olddeleg, &clp->lc_olddeleg, ls_list);
347 LIST_FOREACH(tstp, &new_clp->lc_olddeleg, ls_list)
348 tstp->ls_clp = new_clp;
349 for (i = 0; i < NFSSTATEHASHSIZE; i++) {
350 LIST_NEWHEAD(&new_clp->lc_stateid[i], &clp->lc_stateid[i],
352 LIST_FOREACH(tstp, &new_clp->lc_stateid[i], ls_list)
353 tstp->ls_clp = new_clp;
355 LIST_INSERT_HEAD(NFSCLIENTHASH(new_clp->lc_clientid), new_clp,
357 newnfsstats.srvclients++;
358 nfsrv_openpluslock++;
360 NFSLOCKV4ROOTMUTEX();
361 nfsv4_unlock(&nfsv4rootfs_lock, 1);
362 NFSUNLOCKV4ROOTMUTEX();
365 * Must wait until any outstanding callback on the old clp
368 while (clp->lc_cbref) {
369 clp->lc_flags |= LCL_WAKEUPWANTED;
370 (void) tsleep((caddr_t)clp, PZERO - 1, "nfsd clp", 10 * hz);
372 nfsrv_zapclient(clp, p);
378 * Check to see if the client id exists and optionally confirm it.
381 nfsrv_getclient(nfsquad_t clientid, int opflags, struct nfsclient **clpp,
382 nfsquad_t confirm, struct nfsrv_descript *nd, NFSPROC_T *p)
384 struct nfsclient *clp;
385 struct nfsstate *stp;
387 struct nfsclienthashhead *hp;
388 int error = 0, igotlock, doneok;
392 if (nfsrvboottime != clientid.lval[0])
393 return (NFSERR_STALECLIENTID);
396 * If called with opflags == CLOPS_RENEW, the State Lock is
397 * already held. Otherwise, we need to get either that or,
398 * for the case of Confirm, lock out the nfsd threads.
400 if (opflags & CLOPS_CONFIRM) {
401 NFSLOCKV4ROOTMUTEX();
402 nfsv4_relref(&nfsv4rootfs_lock);
404 igotlock = nfsv4_lock(&nfsv4rootfs_lock, 1, NULL,
405 NFSV4ROOTLOCKMUTEXPTR);
407 NFSUNLOCKV4ROOTMUTEX();
408 NFSLOCKSTATE(); /* to avoid a race with */
409 NFSUNLOCKSTATE(); /* nfsrv_servertimer() */
410 } else if (opflags != CLOPS_RENEW) {
414 hp = NFSCLIENTHASH(clientid);
415 LIST_FOREACH(clp, hp, lc_hash) {
416 if (clp->lc_clientid.lval[1] == clientid.lval[1])
419 if (clp == LIST_END(hp)) {
420 if (opflags & CLOPS_CONFIRM)
421 error = NFSERR_STALECLIENTID;
423 error = NFSERR_EXPIRED;
424 } else if (clp->lc_flags & LCL_ADMINREVOKED) {
426 * If marked admin revoked, just return the error.
428 error = NFSERR_ADMINREVOKED;
431 if (opflags & CLOPS_CONFIRM) {
432 NFSLOCKV4ROOTMUTEX();
433 nfsv4_unlock(&nfsv4rootfs_lock, 1);
434 NFSUNLOCKV4ROOTMUTEX();
435 } else if (opflags != CLOPS_RENEW) {
442 * Perform any operations specified by the opflags.
444 if (opflags & CLOPS_CONFIRM) {
445 if (clp->lc_confirm.qval != confirm.qval)
446 error = NFSERR_STALECLIENTID;
447 else if (nfsrv_notsamecredname(nd, clp))
448 error = NFSERR_CLIDINUSE;
451 if ((clp->lc_flags & (LCL_NEEDSCONFIRM | LCL_DONTCLEAN)) ==
454 * Hang onto the delegations (as old delegations)
455 * for an Open with CLAIM_DELEGATE_PREV unless in
456 * grace, but get rid of the rest of the state.
458 nfsrv_cleanclient(clp, p);
459 nfsrv_freedeleglist(&clp->lc_olddeleg);
460 if (nfsrv_checkgrace(0)) {
461 /* In grace, so just delete delegations */
462 nfsrv_freedeleglist(&clp->lc_deleg);
464 LIST_FOREACH(stp, &clp->lc_deleg, ls_list)
465 stp->ls_flags |= NFSLCK_OLDDELEG;
466 clp->lc_delegtime = NFSD_MONOSEC +
467 nfsrv_lease + NFSRV_LEASEDELTA;
468 LIST_NEWHEAD(&clp->lc_olddeleg, &clp->lc_deleg,
472 clp->lc_flags &= ~(LCL_NEEDSCONFIRM | LCL_DONTCLEAN);
474 clp->lc_flags |= LCL_NEEDSCBNULL;
476 } else if (clp->lc_flags & LCL_NEEDSCONFIRM) {
477 error = NFSERR_EXPIRED;
481 * If called by the Renew Op, we must check the principal.
483 if (!error && (opflags & CLOPS_RENEWOP)) {
484 if (nfsrv_notsamecredname(nd, clp)) {
486 for (i = 0; i < NFSSTATEHASHSIZE && doneok == 0; i++) {
487 LIST_FOREACH(stp, &clp->lc_stateid[i], ls_hash) {
488 if ((stp->ls_flags & NFSLCK_OPEN) &&
489 stp->ls_uid == nd->nd_cred->cr_uid) {
496 error = NFSERR_ACCES;
498 if (!error && (clp->lc_flags & LCL_CBDOWN))
499 error = NFSERR_CBPATHDOWN;
501 if ((!error || error == NFSERR_CBPATHDOWN) &&
502 (opflags & CLOPS_RENEW)) {
503 clp->lc_expiry = nfsrv_leaseexpiry();
505 if (opflags & CLOPS_CONFIRM) {
506 NFSLOCKV4ROOTMUTEX();
507 nfsv4_unlock(&nfsv4rootfs_lock, 1);
508 NFSUNLOCKV4ROOTMUTEX();
509 } else if (opflags != CLOPS_RENEW) {
518 * Called from the new nfssvc syscall to admin revoke a clientid.
519 * Returns 0 for success, error otherwise.
522 nfsrv_adminrevoke(struct nfsd_clid *revokep, NFSPROC_T *p)
524 struct nfsclient *clp = NULL;
529 * First, lock out the nfsd so that state won't change while the
530 * revocation record is being written to the stable storage restart
533 NFSLOCKV4ROOTMUTEX();
534 nfsv4_relref(&nfsv4rootfs_lock);
536 igotlock = nfsv4_lock(&nfsv4rootfs_lock, 1, NULL,
537 NFSV4ROOTLOCKMUTEXPTR);
539 NFSUNLOCKV4ROOTMUTEX();
540 NFSLOCKSTATE(); /* to avoid a race with */
541 NFSUNLOCKSTATE(); /* nfsrv_servertimer() */
544 * Search for a match in the client list.
547 while (i < NFSCLIENTHASHSIZE && !gotit) {
548 LIST_FOREACH(clp, &nfsclienthash[i], lc_hash) {
549 if (revokep->nclid_idlen == clp->lc_idlen &&
550 !NFSBCMP(revokep->nclid_id, clp->lc_id, clp->lc_idlen)) {
558 NFSLOCKV4ROOTMUTEX();
559 nfsv4_unlock(&nfsv4rootfs_lock, 0);
560 NFSUNLOCKV4ROOTMUTEX();
565 * Now, write out the revocation record
567 nfsrv_writestable(clp->lc_id, clp->lc_idlen, NFSNST_REVOKE, p);
570 * and clear out the state, marking the clientid revoked.
572 clp->lc_flags &= ~LCL_CALLBACKSON;
573 clp->lc_flags |= LCL_ADMINREVOKED;
574 nfsrv_cleanclient(clp, p);
575 nfsrv_freedeleglist(&clp->lc_deleg);
576 nfsrv_freedeleglist(&clp->lc_olddeleg);
577 NFSLOCKV4ROOTMUTEX();
578 nfsv4_unlock(&nfsv4rootfs_lock, 0);
579 NFSUNLOCKV4ROOTMUTEX();
584 * Dump out stats for all clients. Called from nfssvc(2), that is used
588 nfsrv_dumpclients(struct nfsd_dumpclients *dumpp, int maxcnt)
590 struct nfsclient *clp;
595 * Rattle through the client lists until done.
597 while (i < NFSCLIENTHASHSIZE && cnt < maxcnt) {
598 clp = LIST_FIRST(&nfsclienthash[i]);
599 while (clp != LIST_END(&nfsclienthash[i]) && cnt < maxcnt) {
600 nfsrv_dumpaclient(clp, &dumpp[cnt]);
602 clp = LIST_NEXT(clp, lc_hash);
607 dumpp[cnt].ndcl_clid.nclid_idlen = 0;
612 * Dump stats for a client. Must be called with the NFSSTATELOCK and spl'd.
615 nfsrv_dumpaclient(struct nfsclient *clp, struct nfsd_dumpclients *dumpp)
617 struct nfsstate *stp, *openstp, *lckownstp;
619 struct sockaddr *sad;
620 struct sockaddr_in *rad;
621 struct sockaddr_in6 *rad6;
623 dumpp->ndcl_nopenowners = dumpp->ndcl_nlockowners = 0;
624 dumpp->ndcl_nopens = dumpp->ndcl_nlocks = 0;
625 dumpp->ndcl_ndelegs = dumpp->ndcl_nolddelegs = 0;
626 dumpp->ndcl_flags = clp->lc_flags;
627 dumpp->ndcl_clid.nclid_idlen = clp->lc_idlen;
628 NFSBCOPY(clp->lc_id, dumpp->ndcl_clid.nclid_id, clp->lc_idlen);
629 sad = NFSSOCKADDR(clp->lc_req.nr_nam, struct sockaddr *);
630 dumpp->ndcl_addrfam = sad->sa_family;
631 if (sad->sa_family == AF_INET) {
632 rad = (struct sockaddr_in *)sad;
633 dumpp->ndcl_cbaddr.sin_addr = rad->sin_addr;
635 rad6 = (struct sockaddr_in6 *)sad;
636 dumpp->ndcl_cbaddr.sin6_addr = rad6->sin6_addr;
640 * Now, scan the state lists and total up the opens and locks.
642 LIST_FOREACH(stp, &clp->lc_open, ls_list) {
643 dumpp->ndcl_nopenowners++;
644 LIST_FOREACH(openstp, &stp->ls_open, ls_list) {
645 dumpp->ndcl_nopens++;
646 LIST_FOREACH(lckownstp, &openstp->ls_open, ls_list) {
647 dumpp->ndcl_nlockowners++;
648 LIST_FOREACH(lop, &lckownstp->ls_lock, lo_lckowner) {
649 dumpp->ndcl_nlocks++;
656 * and the delegation lists.
658 LIST_FOREACH(stp, &clp->lc_deleg, ls_list) {
659 dumpp->ndcl_ndelegs++;
661 LIST_FOREACH(stp, &clp->lc_olddeleg, ls_list) {
662 dumpp->ndcl_nolddelegs++;
667 * Dump out lock stats for a file.
670 nfsrv_dumplocks(vnode_t vp, struct nfsd_dumplocks *ldumpp, int maxcnt,
673 struct nfsstate *stp;
676 struct nfslockfile *lfp;
677 struct sockaddr *sad;
678 struct sockaddr_in *rad;
679 struct sockaddr_in6 *rad6;
683 ret = nfsrv_getlockfh(vp, 0, NULL, &nfh, p);
686 ret = nfsrv_getlockfile(0, NULL, &lfp, &nfh);
688 ldumpp[0].ndlck_clid.nclid_idlen = 0;
694 * For each open share on file, dump it out.
696 stp = LIST_FIRST(&lfp->lf_open);
697 while (stp != LIST_END(&lfp->lf_open) && cnt < maxcnt) {
698 ldumpp[cnt].ndlck_flags = stp->ls_flags;
699 ldumpp[cnt].ndlck_stateid.seqid = stp->ls_stateid.seqid;
700 ldumpp[cnt].ndlck_stateid.other[0] = stp->ls_stateid.other[0];
701 ldumpp[cnt].ndlck_stateid.other[1] = stp->ls_stateid.other[1];
702 ldumpp[cnt].ndlck_stateid.other[2] = stp->ls_stateid.other[2];
703 ldumpp[cnt].ndlck_owner.nclid_idlen =
704 stp->ls_openowner->ls_ownerlen;
705 NFSBCOPY(stp->ls_openowner->ls_owner,
706 ldumpp[cnt].ndlck_owner.nclid_id,
707 stp->ls_openowner->ls_ownerlen);
708 ldumpp[cnt].ndlck_clid.nclid_idlen = stp->ls_clp->lc_idlen;
709 NFSBCOPY(stp->ls_clp->lc_id, ldumpp[cnt].ndlck_clid.nclid_id,
710 stp->ls_clp->lc_idlen);
711 sad=NFSSOCKADDR(stp->ls_clp->lc_req.nr_nam, struct sockaddr *);
712 ldumpp[cnt].ndlck_addrfam = sad->sa_family;
713 if (sad->sa_family == AF_INET) {
714 rad = (struct sockaddr_in *)sad;
715 ldumpp[cnt].ndlck_cbaddr.sin_addr = rad->sin_addr;
717 rad6 = (struct sockaddr_in6 *)sad;
718 ldumpp[cnt].ndlck_cbaddr.sin6_addr = rad6->sin6_addr;
720 stp = LIST_NEXT(stp, ls_file);
727 lop = LIST_FIRST(&lfp->lf_lock);
728 while (lop != LIST_END(&lfp->lf_lock) && cnt < maxcnt) {
730 ldumpp[cnt].ndlck_flags = lop->lo_flags;
731 ldumpp[cnt].ndlck_first = lop->lo_first;
732 ldumpp[cnt].ndlck_end = lop->lo_end;
733 ldumpp[cnt].ndlck_stateid.seqid = stp->ls_stateid.seqid;
734 ldumpp[cnt].ndlck_stateid.other[0] = stp->ls_stateid.other[0];
735 ldumpp[cnt].ndlck_stateid.other[1] = stp->ls_stateid.other[1];
736 ldumpp[cnt].ndlck_stateid.other[2] = stp->ls_stateid.other[2];
737 ldumpp[cnt].ndlck_owner.nclid_idlen = stp->ls_ownerlen;
738 NFSBCOPY(stp->ls_owner, ldumpp[cnt].ndlck_owner.nclid_id,
740 ldumpp[cnt].ndlck_clid.nclid_idlen = stp->ls_clp->lc_idlen;
741 NFSBCOPY(stp->ls_clp->lc_id, ldumpp[cnt].ndlck_clid.nclid_id,
742 stp->ls_clp->lc_idlen);
743 sad=NFSSOCKADDR(stp->ls_clp->lc_req.nr_nam, struct sockaddr *);
744 ldumpp[cnt].ndlck_addrfam = sad->sa_family;
745 if (sad->sa_family == AF_INET) {
746 rad = (struct sockaddr_in *)sad;
747 ldumpp[cnt].ndlck_cbaddr.sin_addr = rad->sin_addr;
749 rad6 = (struct sockaddr_in6 *)sad;
750 ldumpp[cnt].ndlck_cbaddr.sin6_addr = rad6->sin6_addr;
752 lop = LIST_NEXT(lop, lo_lckfile);
757 * and the delegations.
759 stp = LIST_FIRST(&lfp->lf_deleg);
760 while (stp != LIST_END(&lfp->lf_deleg) && cnt < maxcnt) {
761 ldumpp[cnt].ndlck_flags = stp->ls_flags;
762 ldumpp[cnt].ndlck_stateid.seqid = stp->ls_stateid.seqid;
763 ldumpp[cnt].ndlck_stateid.other[0] = stp->ls_stateid.other[0];
764 ldumpp[cnt].ndlck_stateid.other[1] = stp->ls_stateid.other[1];
765 ldumpp[cnt].ndlck_stateid.other[2] = stp->ls_stateid.other[2];
766 ldumpp[cnt].ndlck_owner.nclid_idlen = 0;
767 ldumpp[cnt].ndlck_clid.nclid_idlen = stp->ls_clp->lc_idlen;
768 NFSBCOPY(stp->ls_clp->lc_id, ldumpp[cnt].ndlck_clid.nclid_id,
769 stp->ls_clp->lc_idlen);
770 sad=NFSSOCKADDR(stp->ls_clp->lc_req.nr_nam, struct sockaddr *);
771 ldumpp[cnt].ndlck_addrfam = sad->sa_family;
772 if (sad->sa_family == AF_INET) {
773 rad = (struct sockaddr_in *)sad;
774 ldumpp[cnt].ndlck_cbaddr.sin_addr = rad->sin_addr;
776 rad6 = (struct sockaddr_in6 *)sad;
777 ldumpp[cnt].ndlck_cbaddr.sin6_addr = rad6->sin6_addr;
779 stp = LIST_NEXT(stp, ls_file);
784 * If list isn't full, mark end of list by setting the client name
788 ldumpp[cnt].ndlck_clid.nclid_idlen = 0;
793 * Server timer routine. It can scan any linked list, so long
794 * as it holds the spin lock and there is no exclusive lock on
796 * Must be called by a kernel thread and not a timer interrupt,
797 * so that it only runs when the nfsd threads are sleeping on a
798 * uniprocessor and uses the State spin lock for an SMP system.
799 * (For OpenBSD, a kthread is ok. For FreeBSD, I think it is ok
800 * to do this from a callout, since the spin locks work. For
801 * Darwin, I'm not sure what will work correctly yet.)
802 * Should be called once per second.
805 nfsrv_servertimer(void)
807 struct nfsclient *clp, *nclp;
808 struct nfsstate *stp, *nstp;
812 * Make sure nfsboottime is set. This is used by V3 as well
813 * as V4. Note that nfsboottime is not nfsrvboottime, which is
814 * only used by the V4 server for leases.
816 if (nfsboottime.tv_sec == 0)
817 NFSSETBOOTTIME(nfsboottime);
820 * If server hasn't started yet, just return.
823 if (nfsrv_stablefirst.nsf_eograce == 0) {
827 if (!(nfsrv_stablefirst.nsf_flags & NFSNSF_UPDATEDONE)) {
828 if (!(nfsrv_stablefirst.nsf_flags & NFSNSF_GRACEOVER) &&
829 NFSD_MONOSEC > nfsrv_stablefirst.nsf_eograce)
830 nfsrv_stablefirst.nsf_flags |=
831 (NFSNSF_GRACEOVER | NFSNSF_NEEDLOCK);
837 * Return now if an nfsd thread has the exclusive lock on
838 * nfsv4rootfs_lock. The dirty trick here is that we have
839 * the spin lock already and the nfsd threads do a:
840 * NFSLOCKSTATE, NFSUNLOCKSTATE after getting the exclusive
841 * lock, so they won't race with code after this check.
843 if (nfsv4rootfs_lock.nfslock_lock & NFSV4LOCK_LOCK) {
851 for (i = 0; i < NFSCLIENTHASHSIZE; i++) {
852 clp = LIST_FIRST(&nfsclienthash[i]);
853 while (clp != LIST_END(&nfsclienthash[i])) {
854 nclp = LIST_NEXT(clp, lc_hash);
855 if (!(clp->lc_flags & LCL_EXPIREIT)) {
856 if (((clp->lc_expiry + NFSRV_STALELEASE) < NFSD_MONOSEC
857 && ((LIST_EMPTY(&clp->lc_deleg)
858 && LIST_EMPTY(&clp->lc_open)) ||
859 nfsrv_clients > nfsrv_clienthighwater)) ||
860 (clp->lc_expiry + NFSRV_MOULDYLEASE) < NFSD_MONOSEC ||
861 (clp->lc_expiry < NFSD_MONOSEC &&
862 (nfsrv_openpluslock * 10 / 9) > NFSRV_V4STATELIMIT)) {
864 * Lease has expired several nfsrv_lease times ago:
866 * - no state is associated with it
868 * - above high water mark for number of clients
869 * (nfsrv_clienthighwater should be large enough
870 * that this only occurs when clients fail to
871 * use the same nfs_client_id4.id. Maybe somewhat
872 * higher that the maximum number of clients that
873 * will mount this server?)
875 * Lease has expired a very long time ago
877 * Lease has expired PLUS the number of opens + locks
878 * has exceeded 90% of capacity
880 * --> Mark for expiry. The actual expiry will be done
881 * by an nfsd sometime soon.
883 clp->lc_flags |= LCL_EXPIREIT;
884 nfsrv_stablefirst.nsf_flags |=
885 (NFSNSF_NEEDLOCK | NFSNSF_EXPIREDCLIENT);
888 * If there are no opens, increment no open tick cnt
889 * If time exceeds NFSNOOPEN, mark it to be thrown away
890 * otherwise, if there is an open, reset no open time
891 * Hopefully, this will avoid excessive re-creation
892 * of open owners and subsequent open confirms.
894 stp = LIST_FIRST(&clp->lc_open);
895 while (stp != LIST_END(&clp->lc_open)) {
896 nstp = LIST_NEXT(stp, ls_list);
897 if (LIST_EMPTY(&stp->ls_open)) {
899 if (stp->ls_noopens > NFSNOOPEN ||
900 (nfsrv_openpluslock * 2) >
902 nfsrv_stablefirst.nsf_flags |=
918 * The following set of functions free up the various data structures.
921 * Clear out all open/lock state related to this nfsclient.
922 * Caller must hold an exclusive lock on nfsv4rootfs_lock, so that
923 * there are no other active nfsd threads.
926 nfsrv_cleanclient(struct nfsclient *clp, NFSPROC_T *p)
928 struct nfsstate *stp, *nstp;
930 LIST_FOREACH_SAFE(stp, &clp->lc_open, ls_list, nstp) {
931 nfsrv_freeopenowner(stp, 1, p);
936 * Free a client that has been cleaned. It should also already have been
937 * removed from the lists.
938 * (Just to be safe w.r.t. newnfs_disconnect(), call this function when
939 * softclock interrupts are enabled.)
942 nfsrv_zapclient(struct nfsclient *clp, NFSPROC_T *p)
946 if ((clp->lc_flags & (LCL_GSS | LCL_CALLBACKSON)) ==
947 (LCL_GSS | LCL_CALLBACKSON) &&
948 (clp->lc_hand.nfsh_flag & NFSG_COMPLETE) &&
949 clp->lc_handlelen > 0) {
950 clp->lc_hand.nfsh_flag &= ~NFSG_COMPLETE;
951 clp->lc_hand.nfsh_flag |= NFSG_DESTROYED;
952 (void) nfsrv_docallback(clp, NFSV4PROC_CBNULL,
953 NULL, 0, NULL, NULL, NULL, p);
956 newnfs_disconnect(&clp->lc_req);
957 NFSSOCKADDRFREE(clp->lc_req.nr_nam);
958 NFSFREEMUTEX(&clp->lc_req.nr_mtx);
959 free((caddr_t)clp, M_NFSDCLIENT);
961 newnfsstats.srvclients--;
962 nfsrv_openpluslock--;
968 * Free a list of delegation state structures.
969 * (This function will also free all nfslockfile structures that no
970 * longer have associated state.)
973 nfsrv_freedeleglist(struct nfsstatehead *sthp)
975 struct nfsstate *stp, *nstp;
977 LIST_FOREACH_SAFE(stp, sthp, ls_list, nstp) {
978 nfsrv_freedeleg(stp);
984 * Free up a delegation.
987 nfsrv_freedeleg(struct nfsstate *stp)
989 struct nfslockfile *lfp;
991 LIST_REMOVE(stp, ls_hash);
992 LIST_REMOVE(stp, ls_list);
993 LIST_REMOVE(stp, ls_file);
995 if (LIST_EMPTY(&lfp->lf_open) &&
996 LIST_EMPTY(&lfp->lf_lock) && LIST_EMPTY(&lfp->lf_deleg))
997 nfsrv_freenfslockfile(lfp);
998 FREE((caddr_t)stp, M_NFSDSTATE);
999 newnfsstats.srvdelegates--;
1000 nfsrv_openpluslock--;
1001 nfsrv_delegatecnt--;
1005 * This function frees an open owner and all associated opens.
1006 * Must be called with soft clock interrupts disabled.
1009 nfsrv_freeopenowner(struct nfsstate *stp, int cansleep, NFSPROC_T *p)
1011 struct nfsstate *nstp, *tstp;
1013 LIST_REMOVE(stp, ls_list);
1015 * Now, free all associated opens.
1017 nstp = LIST_FIRST(&stp->ls_open);
1018 while (nstp != LIST_END(&stp->ls_open)) {
1020 nstp = LIST_NEXT(nstp, ls_list);
1021 (void) nfsrv_freeopen(tstp, NULL, cansleep, p);
1024 nfsrvd_derefcache(stp->ls_op);
1025 FREE((caddr_t)stp, M_NFSDSTATE);
1026 newnfsstats.srvopenowners--;
1027 nfsrv_openpluslock--;
1031 * This function frees an open (nfsstate open structure) with all associated
1032 * lock_owners and locks. It also frees the nfslockfile structure iff there
1033 * are no other opens on the file.
1034 * Must be called with soft clock interrupts disabled.
1035 * Returns 1 if it free'd the nfslockfile, 0 otherwise.
1038 nfsrv_freeopen(struct nfsstate *stp, int *freedlockp, int cansleep,
1041 struct nfsstate *nstp, *tstp;
1042 struct nfslockfile *lfp;
1045 LIST_REMOVE(stp, ls_hash);
1046 LIST_REMOVE(stp, ls_list);
1047 LIST_REMOVE(stp, ls_file);
1051 * The nfslockfile is freed here if there are no locks
1052 * associated with the open.
1053 * If there are locks associated with the open, the
1054 * nfslockfile structure can be freed via nfsrv_freelockowner().
1055 * (That is why the call must be here instead of after the loop.)
1057 if (LIST_EMPTY(&lfp->lf_open) && LIST_EMPTY(&lfp->lf_lock) &&
1058 LIST_EMPTY(&lfp->lf_deleg)) {
1059 nfsrv_freenfslockfile(lfp);
1063 * Now, free all lockowners associated with this open.
1065 nstp = LIST_FIRST(&stp->ls_open);
1066 while (nstp != LIST_END(&stp->ls_open)) {
1068 nstp = LIST_NEXT(nstp, ls_list);
1069 ret2 = nfsrv_freelockowner(tstp, freedlockp, cansleep, p);
1070 if (ret == 0 && ret2 != 0)
1073 FREE((caddr_t)stp, M_NFSDSTATE);
1074 newnfsstats.srvopens--;
1075 nfsrv_openpluslock--;
1080 * Frees a lockowner and all associated locks.
1081 * It also frees the nfslockfile structure, if there are no more
1083 * Must be called with soft clock interrupts disabled.
1084 * Returns 1 if it free'd the nfslockfile structure, 1 otherwise.
1087 nfsrv_freelockowner(struct nfsstate *stp, int *freedlockp, int cansleep,
1092 LIST_REMOVE(stp, ls_hash);
1093 LIST_REMOVE(stp, ls_list);
1094 ret = nfsrv_freeallnfslocks(stp, freedlockp, cansleep, p);
1096 nfsrvd_derefcache(stp->ls_op);
1097 FREE((caddr_t)stp, M_NFSDSTATE);
1098 newnfsstats.srvlockowners--;
1099 nfsrv_openpluslock--;
1104 * Free all the nfs locks on a lockowner.
1105 * Returns 1 if it free'd the nfslockfile structure, 0 otherwise.
1106 * If any byte range lock is free'd, *freedlockp is set to 1.
1109 nfsrv_freeallnfslocks(struct nfsstate *stp, int *freedlockp, int cansleep,
1112 struct nfslock *lop, *nlop;
1113 struct nfslockfile *lfp = NULL, *olfp = NULL;
1116 lop = LIST_FIRST(&stp->ls_lock);
1117 while (lop != LIST_END(&stp->ls_lock)) {
1118 nlop = LIST_NEXT(lop, lo_lckowner);
1120 * Since locks off a lockowner are ordered by
1121 * file, you should update the local locks when
1122 * you hit the next file OR the end of the lock
1123 * list. If there are no locks for other owners,
1124 * it must be done before the lockowner is discarded.
1125 * (All this only applies if cansleep == 1.)
1129 nfsrv_freenfslock(lop);
1132 if (LIST_EMPTY(&lfp->lf_open) && LIST_EMPTY(&lfp->lf_lock) &&
1133 LIST_EMPTY(&lfp->lf_deleg)) {
1135 nfsrv_locallocks(NULL, lfp, p);
1136 nfsrv_freenfslockfile(lfp);
1138 * Set the pointer(s) to this lockowner NULL,
1139 * to indicate it has been free'd and local
1140 * locks discarded already.
1147 if (cansleep && olfp != lfp && olfp != NULL)
1148 nfsrv_locallocks(NULL, olfp, p);
1151 if (cansleep && lfp != NULL)
1152 nfsrv_locallocks(NULL, olfp, p);
1157 * Free an nfslock structure.
1158 * Must be called with soft clock interrupts disabled.
1161 nfsrv_freenfslock(struct nfslock *lop)
1164 LIST_REMOVE(lop, lo_lckfile);
1165 LIST_REMOVE(lop, lo_lckowner);
1166 FREE((caddr_t)lop, M_NFSDLOCK);
1167 newnfsstats.srvlocks--;
1168 nfsrv_openpluslock--;
1172 * This function frees an nfslockfile structure.
1173 * Must be called with soft clock interrupts disabled.
1176 nfsrv_freenfslockfile(struct nfslockfile *lfp)
1179 LIST_REMOVE(lfp, lf_hash);
1180 FREE((caddr_t)lfp, M_NFSDLOCKFILE);
1184 * This function looks up an nfsstate structure via stateid.
1187 nfsrv_getstate(struct nfsclient *clp, nfsv4stateid_t *stateidp, __unused u_int32_t flags,
1188 struct nfsstate **stpp)
1190 struct nfsstate *stp;
1191 struct nfsstatehead *hp;
1194 hp = NFSSTATEHASH(clp, *stateidp);
1195 LIST_FOREACH(stp, hp, ls_hash) {
1196 if (!NFSBCMP(stp->ls_stateid.other, stateidp->other,
1202 * If no state id in list, return NFSERR_BADSTATEID.
1204 if (stp == LIST_END(hp))
1205 return (NFSERR_BADSTATEID);
1211 * This function gets an nfsstate structure via owner string.
1214 nfsrv_getowner(struct nfsstatehead *hp, struct nfsstate *new_stp,
1215 struct nfsstate **stpp)
1217 struct nfsstate *stp;
1220 LIST_FOREACH(stp, hp, ls_list) {
1221 if (new_stp->ls_ownerlen == stp->ls_ownerlen &&
1222 !NFSBCMP(new_stp->ls_owner,stp->ls_owner,stp->ls_ownerlen)) {
1230 * Lock control function called to update lock status.
1231 * Returns 0 upon success, -1 if there is no lock and the flags indicate
1232 * that one isn't to be created and an NFSERR_xxx for other errors.
1233 * The structures new_stp and new_lop are passed in as pointers that should
1234 * be set to NULL if the structure is used and shouldn't be free'd.
1235 * For the NFSLCK_TEST and NFSLCK_CHECK cases, the structures are
1236 * never used and can safely be allocated on the stack. For all other
1237 * cases, *new_stpp and *new_lopp should be malloc'd before the call,
1238 * in case they are used.
1241 nfsrv_lockctrl(vnode_t vp, struct nfsstate **new_stpp,
1242 struct nfslock **new_lopp, struct nfslockconflict *cfp,
1243 nfsquad_t clientid, nfsv4stateid_t *stateidp, __unused struct nfsexstuff *exp,
1244 struct nfsrv_descript *nd, NFSPROC_T *p)
1246 struct nfslock *lop;
1247 struct nfsstate *new_stp = *new_stpp;
1248 struct nfslock *new_lop = *new_lopp;
1249 struct nfsstate *tstp, *mystp, *nstp;
1251 struct nfslockfile *lfp;
1252 struct nfslock *other_lop = NULL;
1253 struct nfsstate *stp, *lckstp = NULL;
1254 struct nfsclient *clp = NULL;
1256 int error = 0, haslock = 0, ret;
1257 int getlckret, delegation = 0;
1260 if (new_stp->ls_flags & (NFSLCK_CHECK | NFSLCK_SETATTR)) {
1262 * Note the special cases of "all 1s" or "all 0s" stateids and
1263 * let reads with all 1s go ahead.
1265 if (new_stp->ls_stateid.seqid == 0x0 &&
1266 new_stp->ls_stateid.other[0] == 0x0 &&
1267 new_stp->ls_stateid.other[1] == 0x0 &&
1268 new_stp->ls_stateid.other[2] == 0x0)
1270 else if (new_stp->ls_stateid.seqid == 0xffffffff &&
1271 new_stp->ls_stateid.other[0] == 0xffffffff &&
1272 new_stp->ls_stateid.other[1] == 0xffffffff &&
1273 new_stp->ls_stateid.other[2] == 0xffffffff)
1278 * Check for restart conditions (client and server).
1280 error = nfsrv_checkrestart(clientid, new_stp->ls_flags,
1281 &new_stp->ls_stateid, specialid);
1286 * Check for state resource limit exceeded.
1288 if ((new_stp->ls_flags & NFSLCK_LOCK) &&
1289 nfsrv_openpluslock > NFSRV_V4STATELIMIT)
1290 return (NFSERR_RESOURCE);
1293 * For Lock, check for a conflict with a lock held by
1294 * a process running locally on the server now, before
1295 * monkeying with nfsd state. Since the vp is locked, any
1296 * other local calls are blocked during this Op.
1298 if (new_stp->ls_flags & NFSLCK_LOCK) {
1299 if (new_lop->lo_flags & NFSLCK_WRITE)
1300 error = nfsvno_localconflict(vp, F_WRLCK,
1301 new_lop->lo_first, new_lop->lo_end, cfp, p);
1303 error = nfsvno_localconflict(vp, F_RDLCK,
1304 new_lop->lo_first, new_lop->lo_end, cfp, p);
1310 * For the lock case, get another nfslock structure,
1311 * just in case we need it.
1312 * Malloc now, before we start sifting through the linked lists,
1313 * in case we have to wait for memory.
1316 if (new_stp->ls_flags & NFSLCK_LOCK)
1317 MALLOC(other_lop, struct nfslock *, sizeof (struct nfslock),
1318 M_NFSDLOCK, M_WAITOK);
1321 * Get the lockfile structure for CFH now, so we can do a sanity
1322 * check against the stateid, before incrementing the seqid#, since
1323 * we want to return NFSERR_BADSTATEID on failure and the seqid#
1324 * shouldn't be incremented for this case.
1325 * If nfsrv_getlockfile() returns -1, it means "not found", which
1326 * will be handled later.
1328 getlckret = nfsrv_getlockfh(vp, new_stp->ls_flags, NULL, &nfh, p);
1331 getlckret = nfsrv_getlockfile(new_stp->ls_flags, NULL,
1333 if (getlckret != 0 && getlckret != -1) {
1336 FREE((caddr_t)other_lop, M_NFSDLOCK);
1338 NFSLOCKV4ROOTMUTEX();
1339 nfsv4_unlock(&nfsv4rootfs_lock, 1);
1340 NFSUNLOCKV4ROOTMUTEX();
1346 * Since the code is manipulating lists that are also
1347 * manipulated by nfsrv_servertimer(), soft clock interrupts
1348 * must be masked off.
1350 if (specialid == 0) {
1351 if (new_stp->ls_flags & NFSLCK_TEST) {
1353 * RFC 3530 does not list LockT as an op that renews a
1354 * lease, but the concensus seems to be that it is ok
1355 * for a server to do so.
1357 error = nfsrv_getclient(clientid, CLOPS_RENEW, &clp,
1358 (nfsquad_t)((u_quad_t)0), NULL, p);
1361 * Since NFSERR_EXPIRED, NFSERR_ADMINREVOKED are not valid
1362 * error returns for LockT, just go ahead and test for a lock,
1363 * since there are no locks for this client, but other locks
1364 * can conflict. (ie. same client will always be false)
1366 if (error == NFSERR_EXPIRED || error == NFSERR_ADMINREVOKED)
1370 error = nfsrv_getclient(clientid, CLOPS_RENEW, &clp,
1371 (nfsquad_t)((u_quad_t)0), NULL, p);
1374 * Look up the stateid
1376 error = nfsrv_getstate(clp, &new_stp->ls_stateid,
1377 new_stp->ls_flags, &stp);
1379 * do some sanity checks for an unconfirmed open or a
1380 * stateid that refers to the wrong file, for an open stateid
1382 if (error == 0 && (stp->ls_flags & NFSLCK_OPEN) &&
1383 ((stp->ls_openowner->ls_flags & NFSLCK_NEEDSCONFIRM) ||
1384 (getlckret != -1 && stp->ls_lfp != lfp)))
1385 error = NFSERR_BADSTATEID;
1387 (stp->ls_flags & (NFSLCK_DELEGREAD | NFSLCK_DELEGWRITE)) &&
1388 getlckret != -1 && stp->ls_lfp != lfp)
1389 error = NFSERR_BADSTATEID;
1392 * If the lockowner stateid doesn't refer to the same file,
1393 * I believe that is considered ok, since some clients will
1394 * only create a single lockowner and use that for all locks
1396 * For now, log it as a diagnostic, instead of considering it
1399 if (error == 0 && (stp->ls_flags &
1400 (NFSLCK_OPEN | NFSLCK_DELEGREAD | NFSLCK_DELEGWRITE)) == 0 &&
1401 getlckret != -1 && stp->ls_lfp != lfp) {
1403 printf("Got a lock statid for different file open\n");
1406 error = NFSERR_BADSTATEID;
1411 if (new_stp->ls_flags & NFSLCK_OPENTOLOCK) {
1413 * If haslock set, we've already checked the seqid.
1416 if (stp->ls_flags & NFSLCK_OPEN)
1417 error = nfsrv_checkseqid(nd, new_stp->ls_seq,
1418 stp->ls_openowner, new_stp->ls_op);
1420 error = NFSERR_BADSTATEID;
1423 nfsrv_getowner(&stp->ls_open, new_stp, &lckstp);
1426 * I believe this should be an error, but it
1427 * isn't obvious what NFSERR_xxx would be
1428 * appropriate, so I'll use NFSERR_INVAL for now.
1430 error = NFSERR_INVAL;
1433 } else if (new_stp->ls_flags&(NFSLCK_LOCK|NFSLCK_UNLOCK)) {
1435 * If haslock set, ditto above.
1438 if (stp->ls_flags & NFSLCK_OPEN)
1439 error = NFSERR_BADSTATEID;
1441 error = nfsrv_checkseqid(nd, new_stp->ls_seq,
1442 stp, new_stp->ls_op);
1450 * If the seqid part of the stateid isn't the same, return
1451 * NFSERR_OLDSTATEID for cases other than I/O Ops.
1452 * For I/O Ops, only return NFSERR_OLDSTATEID if
1453 * nfsrv_returnoldstateid is set. (The concensus on the email
1454 * list was that most clients would prefer to not receive
1455 * NFSERR_OLDSTATEID for I/O Ops, but the RFC suggests that that
1456 * is what will happen, so I use the nfsrv_returnoldstateid to
1457 * allow for either server configuration.)
1459 if (!error && stp->ls_stateid.seqid!=new_stp->ls_stateid.seqid &&
1460 (!(new_stp->ls_flags & NFSLCK_CHECK) ||
1461 nfsrv_returnoldstateid))
1462 error = NFSERR_OLDSTATEID;
1467 * Now we can check for grace.
1470 error = nfsrv_checkgrace(new_stp->ls_flags);
1471 if ((new_stp->ls_flags & NFSLCK_RECLAIM) && !error &&
1472 nfsrv_checkstable(clp))
1473 error = NFSERR_NOGRACE;
1475 * If we successfully Reclaimed state, note that.
1477 if ((new_stp->ls_flags & NFSLCK_RECLAIM) && !error)
1478 nfsrv_markstable(clp);
1481 * If nd_repstat is set, we can return that now, since the
1482 * seqid# has been incremented.
1484 if (nd->nd_repstat && !error)
1485 error = nd->nd_repstat;
1489 FREE((caddr_t)other_lop, M_NFSDLOCK);
1491 NFSLOCKV4ROOTMUTEX();
1492 nfsv4_unlock(&nfsv4rootfs_lock, 1);
1493 NFSUNLOCKV4ROOTMUTEX();
1499 * Check the nfsrv_getlockfile return.
1500 * Returned -1 if no structure found.
1502 if (getlckret == -1) {
1503 error = NFSERR_EXPIRED;
1505 * Called from lockt, so no lock is OK.
1507 if (new_stp->ls_flags & NFSLCK_TEST) {
1509 } else if (new_stp->ls_flags &
1510 (NFSLCK_CHECK | NFSLCK_SETATTR)) {
1512 * Called to check for a lock, OK if the stateid is all
1513 * 1s or all 0s, but there should be an nfsstate
1515 * (ie. If there is no open, I'll assume no share
1521 error = NFSERR_BADSTATEID;
1525 NFSLOCKV4ROOTMUTEX();
1526 nfsv4_unlock(&nfsv4rootfs_lock, 1);
1527 NFSUNLOCKV4ROOTMUTEX();
1530 * Called to lock or unlock, so the lock has gone away.
1536 * For NFSLCK_CHECK and NFSLCK_LOCK, test for a share conflict.
1537 * For NFSLCK_CHECK, allow a read if write access is granted,
1538 * but check for a deny. For NFSLCK_LOCK, require correct access,
1539 * which implies a conflicting deny can't exist.
1541 if (new_stp->ls_flags & (NFSLCK_CHECK | NFSLCK_LOCK)) {
1543 * Four kinds of state id:
1544 * - specialid (all 0s or all 1s), only for NFSLCK_CHECK
1545 * - stateid for an open
1546 * - stateid for a delegation
1547 * - stateid for a lock owner
1550 if (stp->ls_flags & (NFSLCK_DELEGREAD | NFSLCK_DELEGWRITE)) {
1553 nfsrv_delaydelegtimeout(stp);
1554 } else if (stp->ls_flags & NFSLCK_OPEN) {
1557 mystp = stp->ls_openstp;
1560 * If locking or checking, require correct access
1563 if (((new_stp->ls_flags & NFSLCK_LOCK) &&
1564 !((new_lop->lo_flags >> NFSLCK_LOCKSHIFT) &
1565 mystp->ls_flags & NFSLCK_ACCESSBITS)) ||
1566 ((new_stp->ls_flags & (NFSLCK_CHECK|NFSLCK_READACCESS)) ==
1567 (NFSLCK_CHECK | NFSLCK_READACCESS) &&
1568 !(mystp->ls_flags & NFSLCK_READACCESS)) ||
1569 ((new_stp->ls_flags & (NFSLCK_CHECK|NFSLCK_WRITEACCESS)) ==
1570 (NFSLCK_CHECK | NFSLCK_WRITEACCESS) &&
1571 !(mystp->ls_flags & NFSLCK_WRITEACCESS))) {
1574 FREE((caddr_t)other_lop, M_NFSDLOCK);
1576 NFSLOCKV4ROOTMUTEX();
1577 nfsv4_unlock(&nfsv4rootfs_lock, 1);
1578 NFSUNLOCKV4ROOTMUTEX();
1580 return (NFSERR_OPENMODE);
1584 if ((new_stp->ls_flags & NFSLCK_CHECK) && !delegation) {
1586 * Check for a conflicting deny bit.
1588 LIST_FOREACH(tstp, &lfp->lf_open, ls_file) {
1589 if (tstp != mystp) {
1590 bits = tstp->ls_flags;
1591 bits >>= NFSLCK_SHIFT;
1592 if (new_stp->ls_flags & bits & NFSLCK_ACCESSBITS) {
1593 ret = nfsrv_clientconflict(tstp->ls_clp, &haslock,
1597 * nfsrv_clientconflict unlocks state
1598 * when it returns non-zero.
1605 NFSLOCKV4ROOTMUTEX();
1606 nfsv4_unlock(&nfsv4rootfs_lock, 1);
1607 NFSUNLOCKV4ROOTMUTEX();
1609 return (NFSERR_OPENMODE);
1614 /* We're outta here */
1617 NFSLOCKV4ROOTMUTEX();
1618 nfsv4_unlock(&nfsv4rootfs_lock, 1);
1619 NFSUNLOCKV4ROOTMUTEX();
1626 * For setattr, just get rid of all the Delegations for other clients.
1628 if (new_stp->ls_flags & NFSLCK_SETATTR) {
1629 ret = nfsrv_cleandeleg(vp, lfp, clp, &haslock, p);
1632 * nfsrv_cleandeleg() unlocks state when it
1641 if (!(new_stp->ls_flags & NFSLCK_CHECK) ||
1642 (LIST_EMPTY(&lfp->lf_open) && LIST_EMPTY(&lfp->lf_lock) &&
1643 LIST_EMPTY(&lfp->lf_deleg))) {
1646 NFSLOCKV4ROOTMUTEX();
1647 nfsv4_unlock(&nfsv4rootfs_lock, 1);
1648 NFSUNLOCKV4ROOTMUTEX();
1655 * Check for a conflicting delegation. If one is found, call
1656 * nfsrv_delegconflict() to handle it. If the v4root lock hasn't
1657 * been set yet, it will get the lock. Otherwise, it will recall
1658 * the delegation. Then, we try try again...
1659 * I currently believe the conflict algorithm to be:
1660 * For Lock Ops (Lock/LockT/LockU)
1661 * - there is a conflict iff a different client has a write delegation
1662 * For Reading (Read Op)
1663 * - there is a conflict iff a different client has a write delegation
1664 * (the specialids are always a different client)
1665 * For Writing (Write/Setattr of size)
1666 * - there is a conflict if a different client has any delegation
1667 * - there is a conflict if the same client has a read delegation
1668 * (I don't understand why this isn't allowed, but that seems to be
1669 * the current concensus?)
1671 tstp = LIST_FIRST(&lfp->lf_deleg);
1672 while (tstp != LIST_END(&lfp->lf_deleg)) {
1673 nstp = LIST_NEXT(tstp, ls_file);
1674 if ((((new_stp->ls_flags&(NFSLCK_LOCK|NFSLCK_UNLOCK|NFSLCK_TEST))||
1675 ((new_stp->ls_flags & NFSLCK_CHECK) &&
1676 (new_lop->lo_flags & NFSLCK_READ))) &&
1677 clp != tstp->ls_clp &&
1678 (tstp->ls_flags & NFSLCK_DELEGWRITE)) ||
1679 ((new_stp->ls_flags & NFSLCK_CHECK) &&
1680 (new_lop->lo_flags & NFSLCK_WRITE) &&
1681 (clp != tstp->ls_clp ||
1682 (tstp->ls_flags & NFSLCK_DELEGREAD)))) {
1683 ret = nfsrv_delegconflict(tstp, &haslock, p, vp);
1686 * nfsrv_delegconflict unlocks state when it
1690 FREE((caddr_t)other_lop, M_NFSDLOCK);
1704 * Handle the unlock case by calling nfsrv_updatelock().
1705 * (Should I have done some access checking above for unlock? For now,
1706 * just let it happen.)
1708 if (new_stp->ls_flags & NFSLCK_UNLOCK) {
1709 nfsrv_updatelock(stp, new_lopp, &other_lop, lfp);
1710 stateidp->seqid = ++(stp->ls_stateid.seqid);
1711 stateidp->other[0] = stp->ls_stateid.other[0];
1712 stateidp->other[1] = stp->ls_stateid.other[1];
1713 stateidp->other[2] = stp->ls_stateid.other[2];
1715 * For a non-empty flp->lf_lock list, I believe
1716 * nfsrv_locallocks() can safely traverse the list, including
1717 * sleeping, for two reasons:
1718 * 1 - The Lock/LockU/Close Ops all require a locked
1719 * vnode for the file and we currently have that.
1720 * 2 - The only other thing that modifies a non-empty
1721 * list is nfsrv_cleanclient() and it is always
1722 * done with the exclusive nfsv4rootfs_lock held.
1723 * Since this Op in progress holds either a shared or
1724 * exclusive lock on nfsv4rootfs_lock, that can't
1726 * However, the structure pointed to by lfp can go
1727 * in many places for an empty list, so that is handled
1728 * by passing a NULL pointer to nfsrv_locallocks().
1729 * Do that check now, while we are still SMP safe.
1731 if (LIST_EMPTY(&lfp->lf_lock))
1734 nfsrv_locallocks(vp, lfp, p);
1736 NFSLOCKV4ROOTMUTEX();
1737 nfsv4_unlock(&nfsv4rootfs_lock, 1);
1738 NFSUNLOCKV4ROOTMUTEX();
1744 * Search for a conflicting lock. A lock conflicts if:
1745 * - the lock range overlaps and
1746 * - at least one lock is a write lock and
1747 * - it is not owned by the same lock owner
1750 LIST_FOREACH(lop, &lfp->lf_lock, lo_lckfile) {
1751 if (new_lop->lo_end > lop->lo_first &&
1752 new_lop->lo_first < lop->lo_end &&
1753 (new_lop->lo_flags == NFSLCK_WRITE ||
1754 lop->lo_flags == NFSLCK_WRITE) &&
1755 lckstp != lop->lo_stp &&
1756 (lckstp->ls_clp != lop->lo_stp->ls_clp ||
1757 lckstp->ls_ownerlen != lop->lo_stp->ls_ownerlen ||
1758 NFSBCMP(lckstp->ls_owner, lop->lo_stp->ls_owner,
1759 lckstp->ls_ownerlen))) {
1761 FREE((caddr_t)other_lop, M_NFSDLOCK);
1764 ret = nfsrv_clientconflict(lop->lo_stp->ls_clp,&haslock,vp,p);
1767 * nfsrv_clientconflict() unlocks state when it
1774 * Found a conflicting lock, so record the conflict and
1778 cfp->cl_clientid.lval[0]=lop->lo_stp->ls_stateid.other[0];
1779 cfp->cl_clientid.lval[1]=lop->lo_stp->ls_stateid.other[1];
1780 cfp->cl_first = lop->lo_first;
1781 cfp->cl_end = lop->lo_end;
1782 cfp->cl_flags = lop->lo_flags;
1783 cfp->cl_ownerlen = lop->lo_stp->ls_ownerlen;
1784 NFSBCOPY(lop->lo_stp->ls_owner, cfp->cl_owner,
1787 if (new_stp->ls_flags & NFSLCK_RECLAIM)
1788 error = NFSERR_RECLAIMCONFLICT;
1789 else if (new_stp->ls_flags & NFSLCK_CHECK)
1790 error = NFSERR_LOCKED;
1792 error = NFSERR_DENIED;
1795 NFSLOCKV4ROOTMUTEX();
1796 nfsv4_unlock(&nfsv4rootfs_lock, 1);
1797 NFSUNLOCKV4ROOTMUTEX();
1805 * We only get here if there was no lock that conflicted.
1807 if (new_stp->ls_flags & (NFSLCK_TEST | NFSLCK_CHECK)) {
1810 NFSLOCKV4ROOTMUTEX();
1811 nfsv4_unlock(&nfsv4rootfs_lock, 1);
1812 NFSUNLOCKV4ROOTMUTEX();
1818 * We only get here when we are creating or modifying a lock.
1819 * There are two variants:
1820 * - exist_lock_owner where lock_owner exists
1821 * - open_to_lock_owner with new lock_owner
1823 if (!(new_stp->ls_flags & NFSLCK_OPENTOLOCK)) {
1824 nfsrv_updatelock(lckstp, new_lopp, &other_lop, lfp);
1825 stateidp->seqid = ++(lckstp->ls_stateid.seqid);
1826 stateidp->other[0] = lckstp->ls_stateid.other[0];
1827 stateidp->other[1] = lckstp->ls_stateid.other[1];
1828 stateidp->other[2] = lckstp->ls_stateid.other[2];
1831 * The new open_to_lock_owner case.
1832 * Link the new nfsstate into the lists.
1834 new_stp->ls_seq = new_stp->ls_opentolockseq;
1835 nfsrvd_refcache(new_stp->ls_op);
1836 stateidp->seqid = new_stp->ls_stateid.seqid = 0;
1837 stateidp->other[0] = new_stp->ls_stateid.other[0] =
1838 clp->lc_clientid.lval[0];
1839 stateidp->other[1] = new_stp->ls_stateid.other[1] =
1840 clp->lc_clientid.lval[1];
1841 stateidp->other[2] = new_stp->ls_stateid.other[2] =
1842 nfsrv_nextstateindex(clp);
1843 new_stp->ls_clp = clp;
1844 LIST_INIT(&new_stp->ls_lock);
1845 new_stp->ls_openstp = stp;
1846 new_stp->ls_lfp = lfp;
1847 nfsrv_insertlock(new_lop, (struct nfslock *)new_stp, new_stp,
1849 LIST_INSERT_HEAD(NFSSTATEHASH(clp, new_stp->ls_stateid),
1851 LIST_INSERT_HEAD(&stp->ls_open, new_stp, ls_list);
1854 newnfsstats.srvlockowners++;
1855 nfsrv_openpluslock++;
1857 /* See comment above, w.r.t. nfsrv_locallocks(). */
1858 if (LIST_EMPTY(&lfp->lf_lock))
1861 nfsrv_locallocks(vp, lfp, p);
1863 NFSLOCKV4ROOTMUTEX();
1864 nfsv4_unlock(&nfsv4rootfs_lock, 1);
1865 NFSUNLOCKV4ROOTMUTEX();
1868 FREE((caddr_t)other_lop, M_NFSDLOCK);
1873 * Check for state errors for Open.
1874 * repstat is passed back out as an error if more critical errors
1878 nfsrv_opencheck(nfsquad_t clientid, nfsv4stateid_t *stateidp,
1879 struct nfsstate *new_stp, vnode_t vp, struct nfsrv_descript *nd,
1880 NFSPROC_T *p, int repstat)
1882 struct nfsstate *stp, *nstp;
1883 struct nfsclient *clp;
1884 struct nfsstate *ownerstp;
1885 struct nfslockfile *lfp, *new_lfp;
1886 int error, haslock = 0, ret, readonly = 0, getfhret = 0;
1888 if ((new_stp->ls_flags & NFSLCK_SHAREBITS) == NFSLCK_READACCESS)
1891 * Check for restart conditions (client and server).
1893 error = nfsrv_checkrestart(clientid, new_stp->ls_flags,
1894 &new_stp->ls_stateid, 0);
1899 * Check for state resource limit exceeded.
1900 * Technically this should be SMP protected, but the worst
1901 * case error is "out by one or two" on the count when it
1902 * returns NFSERR_RESOURCE and the limit is just a rather
1903 * arbitrary high water mark, so no harm is done.
1905 if (nfsrv_openpluslock > NFSRV_V4STATELIMIT)
1906 return (NFSERR_RESOURCE);
1909 MALLOC(new_lfp, struct nfslockfile *, sizeof (struct nfslockfile),
1910 M_NFSDLOCKFILE, M_WAITOK);
1912 getfhret = nfsrv_getlockfh(vp, new_stp->ls_flags, &new_lfp,
1916 * Get the nfsclient structure.
1917 * Since the code is manipulating lists that are also
1918 * manipulated by nfsrv_servertimer(), soft clock interrupts
1919 * must be masked off.
1921 error = nfsrv_getclient(clientid, CLOPS_RENEW, &clp,
1922 (nfsquad_t)((u_quad_t)0), NULL, p);
1925 * Look up the open owner. See if it needs confirmation and
1926 * check the seq#, as required.
1929 nfsrv_getowner(&clp->lc_open, new_stp, &ownerstp);
1931 if (!error && ownerstp) {
1932 error = nfsrv_checkseqid(nd, new_stp->ls_seq, ownerstp,
1935 * If the OpenOwner hasn't been confirmed, assume the
1936 * old one was a replay and this one is ok.
1937 * See: RFC3530 Sec. 14.2.18.
1939 if (error == NFSERR_BADSEQID &&
1940 (ownerstp->ls_flags & NFSLCK_NEEDSCONFIRM))
1948 error = nfsrv_checkgrace(new_stp->ls_flags);
1949 if ((new_stp->ls_flags & NFSLCK_RECLAIM) && !error &&
1950 nfsrv_checkstable(clp))
1951 error = NFSERR_NOGRACE;
1954 * If none of the above errors occurred, let repstat be
1957 if (repstat && !error)
1962 NFSLOCKV4ROOTMUTEX();
1963 nfsv4_unlock(&nfsv4rootfs_lock, 1);
1964 NFSUNLOCKV4ROOTMUTEX();
1966 free((caddr_t)new_lfp, M_NFSDLOCKFILE);
1971 * If vp == NULL, the file doesn't exist yet, so return ok.
1972 * (This always happens on the first pass, so haslock must be 0.)
1976 FREE((caddr_t)new_lfp, M_NFSDLOCKFILE);
1981 * Get the structure for the underlying file.
1986 error = nfsrv_getlockfile(new_stp->ls_flags, &new_lfp, &lfp,
1989 FREE((caddr_t)new_lfp, M_NFSDLOCKFILE);
1993 NFSLOCKV4ROOTMUTEX();
1994 nfsv4_unlock(&nfsv4rootfs_lock, 1);
1995 NFSUNLOCKV4ROOTMUTEX();
2001 * Search for a conflicting open/share.
2003 if (new_stp->ls_flags & NFSLCK_DELEGCUR) {
2005 * For Delegate_Cur, search for the matching Delegation,
2006 * which indicates no conflict.
2007 * An old delegation should have been recovered by the
2008 * client doing a Claim_DELEGATE_Prev, so I won't let
2009 * it match and return NFSERR_EXPIRED. Should I let it
2012 LIST_FOREACH(stp, &lfp->lf_deleg, ls_file) {
2013 if (!(stp->ls_flags & NFSLCK_OLDDELEG) &&
2014 stateidp->seqid == stp->ls_stateid.seqid &&
2015 !NFSBCMP(stateidp->other, stp->ls_stateid.other,
2019 if (stp == LIST_END(&lfp->lf_deleg) ||
2020 ((new_stp->ls_flags & NFSLCK_WRITEACCESS) &&
2021 (stp->ls_flags & NFSLCK_DELEGREAD))) {
2024 NFSLOCKV4ROOTMUTEX();
2025 nfsv4_unlock(&nfsv4rootfs_lock, 1);
2026 NFSUNLOCKV4ROOTMUTEX();
2028 return (NFSERR_EXPIRED);
2033 * Check for access/deny bit conflicts. I check for the same
2034 * owner as well, in case the client didn't bother.
2036 LIST_FOREACH(stp, &lfp->lf_open, ls_file) {
2037 if (!(new_stp->ls_flags & NFSLCK_DELEGCUR) &&
2038 (((new_stp->ls_flags & NFSLCK_ACCESSBITS) &
2039 ((stp->ls_flags>>NFSLCK_SHIFT) & NFSLCK_ACCESSBITS))||
2040 ((stp->ls_flags & NFSLCK_ACCESSBITS) &
2041 ((new_stp->ls_flags>>NFSLCK_SHIFT)&NFSLCK_ACCESSBITS)))){
2042 ret = nfsrv_clientconflict(stp->ls_clp,&haslock,vp,p);
2045 * nfsrv_clientconflict() unlocks
2046 * state when it returns non-zero.
2050 if (new_stp->ls_flags & NFSLCK_RECLAIM)
2051 error = NFSERR_RECLAIMCONFLICT;
2053 error = NFSERR_SHAREDENIED;
2056 NFSLOCKV4ROOTMUTEX();
2057 nfsv4_unlock(&nfsv4rootfs_lock, 1);
2058 NFSUNLOCKV4ROOTMUTEX();
2065 * Check for a conflicting delegation. If one is found, call
2066 * nfsrv_delegconflict() to handle it. If the v4root lock hasn't
2067 * been set yet, it will get the lock. Otherwise, it will recall
2068 * the delegation. Then, we try try again...
2069 * (If NFSLCK_DELEGCUR is set, it has a delegation, so there
2070 * isn't a conflict.)
2071 * I currently believe the conflict algorithm to be:
2072 * For Open with Read Access and Deny None
2073 * - there is a conflict iff a different client has a write delegation
2074 * For Open with other Write Access or any Deny except None
2075 * - there is a conflict if a different client has any delegation
2076 * - there is a conflict if the same client has a read delegation
2077 * (The current concensus is that this last case should be
2078 * considered a conflict since the client with a read delegation
2079 * could have done an Open with ReadAccess and WriteDeny
2080 * locally and then not have checked for the WriteDeny.)
2081 * Don't check for a Reclaim, since that will be dealt with
2082 * by nfsrv_openctrl().
2084 if (!(new_stp->ls_flags &
2085 (NFSLCK_DELEGPREV | NFSLCK_DELEGCUR | NFSLCK_RECLAIM))) {
2086 stp = LIST_FIRST(&lfp->lf_deleg);
2087 while (stp != LIST_END(&lfp->lf_deleg)) {
2088 nstp = LIST_NEXT(stp, ls_file);
2089 if ((readonly && stp->ls_clp != clp &&
2090 (stp->ls_flags & NFSLCK_DELEGWRITE)) ||
2091 (!readonly && (stp->ls_clp != clp ||
2092 (stp->ls_flags & NFSLCK_DELEGREAD)))) {
2093 ret = nfsrv_delegconflict(stp, &haslock, p, vp);
2096 * nfsrv_delegconflict() unlocks state
2097 * when it returns non-zero.
2109 NFSLOCKV4ROOTMUTEX();
2110 nfsv4_unlock(&nfsv4rootfs_lock, 1);
2111 NFSUNLOCKV4ROOTMUTEX();
2117 * Open control function to create/update open state for an open.
2120 nfsrv_openctrl(struct nfsrv_descript *nd, vnode_t vp,
2121 struct nfsstate **new_stpp, nfsquad_t clientid, nfsv4stateid_t *stateidp,
2122 nfsv4stateid_t *delegstateidp, u_int32_t *rflagsp, struct nfsexstuff *exp,
2123 NFSPROC_T *p, u_quad_t filerev)
2125 struct nfsstate *new_stp = *new_stpp;
2126 struct nfsstate *stp, *nstp;
2127 struct nfsstate *openstp = NULL, *new_open, *ownerstp, *new_deleg;
2128 struct nfslockfile *lfp, *new_lfp;
2129 struct nfsclient *clp;
2130 int error, haslock = 0, ret, delegate = 1, writedeleg = 1;
2131 int readonly = 0, cbret = 1, getfhret = 0;
2133 if ((new_stp->ls_flags & NFSLCK_SHAREBITS) == NFSLCK_READACCESS)
2136 * Check for restart conditions (client and server).
2137 * (Paranoia, should have been detected by nfsrv_opencheck().)
2138 * If an error does show up, return NFSERR_EXPIRED, since the
2139 * the seqid# has already been incremented.
2141 error = nfsrv_checkrestart(clientid, new_stp->ls_flags,
2142 &new_stp->ls_stateid, 0);
2144 printf("Nfsd: openctrl unexpected restart err=%d\n",
2146 return (NFSERR_EXPIRED);
2150 MALLOC(new_lfp, struct nfslockfile *, sizeof (struct nfslockfile),
2151 M_NFSDLOCKFILE, M_WAITOK);
2152 MALLOC(new_open, struct nfsstate *, sizeof (struct nfsstate),
2153 M_NFSDSTATE, M_WAITOK);
2154 MALLOC(new_deleg, struct nfsstate *, sizeof (struct nfsstate),
2155 M_NFSDSTATE, M_WAITOK);
2156 getfhret = nfsrv_getlockfh(vp, new_stp->ls_flags, &new_lfp,
2160 * Get the client structure. Since the linked lists could be changed
2161 * by other nfsd processes if this process does a tsleep(), one of
2162 * two things must be done.
2163 * 1 - don't tsleep()
2165 * 2 - get the nfsv4_lock() { indicated by haslock == 1 }
2166 * before using the lists, since this lock stops the other
2167 * nfsd. This should only be used for rare cases, since it
2168 * essentially single threads the nfsd.
2169 * At this time, it is only done for cases where the stable
2170 * storage file must be written prior to completion of state
2173 error = nfsrv_getclient(clientid, CLOPS_RENEW, &clp,
2174 (nfsquad_t)((u_quad_t)0), NULL, p);
2175 if (!error && (clp->lc_flags & LCL_NEEDSCBNULL) &&
2178 * This happens on the first open for a client
2179 * that supports callbacks.
2183 * Although nfsrv_docallback() will sleep, clp won't
2184 * go away, since they are only removed when the
2185 * nfsv4_lock() has blocked the nfsd threads. The
2186 * fields in clp can change, but having multiple
2187 * threads do this Null callback RPC should be
2190 cbret = nfsrv_docallback(clp, NFSV4PROC_CBNULL,
2191 NULL, 0, NULL, NULL, NULL, p);
2193 clp->lc_flags &= ~LCL_NEEDSCBNULL;
2195 clp->lc_flags |= LCL_CALLBACKSON;
2199 * Look up the open owner. See if it needs confirmation and
2200 * check the seq#, as required.
2203 nfsrv_getowner(&clp->lc_open, new_stp, &ownerstp);
2207 printf("Nfsd: openctrl unexpected state err=%d\n",
2209 free((caddr_t)new_lfp, M_NFSDLOCKFILE);
2210 free((caddr_t)new_open, M_NFSDSTATE);
2211 free((caddr_t)new_deleg, M_NFSDSTATE);
2213 NFSLOCKV4ROOTMUTEX();
2214 nfsv4_unlock(&nfsv4rootfs_lock, 1);
2215 NFSUNLOCKV4ROOTMUTEX();
2217 return (NFSERR_EXPIRED);
2220 if (new_stp->ls_flags & NFSLCK_RECLAIM)
2221 nfsrv_markstable(clp);
2224 * Get the structure for the underlying file.
2229 error = nfsrv_getlockfile(new_stp->ls_flags, &new_lfp, &lfp,
2232 FREE((caddr_t)new_lfp, M_NFSDLOCKFILE);
2235 printf("Nfsd openctrl unexpected getlockfile err=%d\n",
2237 free((caddr_t)new_open, M_NFSDSTATE);
2238 free((caddr_t)new_deleg, M_NFSDSTATE);
2240 NFSLOCKV4ROOTMUTEX();
2241 nfsv4_unlock(&nfsv4rootfs_lock, 1);
2242 NFSUNLOCKV4ROOTMUTEX();
2248 * Search for a conflicting open/share.
2250 if (new_stp->ls_flags & NFSLCK_DELEGCUR) {
2252 * For Delegate_Cur, search for the matching Delegation,
2253 * which indicates no conflict.
2254 * An old delegation should have been recovered by the
2255 * client doing a Claim_DELEGATE_Prev, so I won't let
2256 * it match and return NFSERR_EXPIRED. Should I let it
2259 LIST_FOREACH(stp, &lfp->lf_deleg, ls_file) {
2260 if (!(stp->ls_flags & NFSLCK_OLDDELEG) &&
2261 stateidp->seqid == stp->ls_stateid.seqid &&
2262 !NFSBCMP(stateidp->other, stp->ls_stateid.other,
2266 if (stp == LIST_END(&lfp->lf_deleg) ||
2267 ((new_stp->ls_flags & NFSLCK_WRITEACCESS) &&
2268 (stp->ls_flags & NFSLCK_DELEGREAD))) {
2270 printf("Nfsd openctrl unexpected expiry\n");
2271 free((caddr_t)new_open, M_NFSDSTATE);
2272 free((caddr_t)new_deleg, M_NFSDSTATE);
2274 NFSLOCKV4ROOTMUTEX();
2275 nfsv4_unlock(&nfsv4rootfs_lock, 1);
2276 NFSUNLOCKV4ROOTMUTEX();
2278 return (NFSERR_EXPIRED);
2282 * Don't issue a Delegation, since one already exists and
2283 * delay delegation timeout, as required.
2286 nfsrv_delaydelegtimeout(stp);
2290 * Check for access/deny bit conflicts. I also check for the
2291 * same owner, since the client might not have bothered to check.
2292 * Also, note an open for the same file and owner, if found,
2293 * which is all we do here for Delegate_Cur, since conflict
2294 * checking is already done.
2296 LIST_FOREACH(stp, &lfp->lf_open, ls_file) {
2297 if (ownerstp && stp->ls_openowner == ownerstp)
2299 if (!(new_stp->ls_flags & NFSLCK_DELEGCUR)) {
2301 * If another client has the file open, the only
2302 * delegation that can be issued is a Read delegation
2303 * and only if it is a Read open with Deny none.
2305 if (clp != stp->ls_clp) {
2306 if ((stp->ls_flags & NFSLCK_SHAREBITS) ==
2312 if(((new_stp->ls_flags & NFSLCK_ACCESSBITS) &
2313 ((stp->ls_flags>>NFSLCK_SHIFT) & NFSLCK_ACCESSBITS))||
2314 ((stp->ls_flags & NFSLCK_ACCESSBITS) &
2315 ((new_stp->ls_flags>>NFSLCK_SHIFT)&NFSLCK_ACCESSBITS))){
2316 ret = nfsrv_clientconflict(stp->ls_clp,&haslock,vp,p);
2319 * nfsrv_clientconflict() unlocks state
2320 * when it returns non-zero.
2322 free((caddr_t)new_open, M_NFSDSTATE);
2323 free((caddr_t)new_deleg, M_NFSDSTATE);
2327 if (new_stp->ls_flags & NFSLCK_RECLAIM)
2328 error = NFSERR_RECLAIMCONFLICT;
2330 error = NFSERR_SHAREDENIED;
2333 NFSLOCKV4ROOTMUTEX();
2334 nfsv4_unlock(&nfsv4rootfs_lock, 1);
2335 NFSUNLOCKV4ROOTMUTEX();
2337 free((caddr_t)new_open, M_NFSDSTATE);
2338 free((caddr_t)new_deleg, M_NFSDSTATE);
2339 printf("nfsd openctrl unexpected client cnfl\n");
2346 * Check for a conflicting delegation. If one is found, call
2347 * nfsrv_delegconflict() to handle it. If the v4root lock hasn't
2348 * been set yet, it will get the lock. Otherwise, it will recall
2349 * the delegation. Then, we try try again...
2350 * (If NFSLCK_DELEGCUR is set, it has a delegation, so there
2351 * isn't a conflict.)
2352 * I currently believe the conflict algorithm to be:
2353 * For Open with Read Access and Deny None
2354 * - there is a conflict iff a different client has a write delegation
2355 * For Open with other Write Access or any Deny except None
2356 * - there is a conflict if a different client has any delegation
2357 * - there is a conflict if the same client has a read delegation
2358 * (The current concensus is that this last case should be
2359 * considered a conflict since the client with a read delegation
2360 * could have done an Open with ReadAccess and WriteDeny
2361 * locally and then not have checked for the WriteDeny.)
2363 if (!(new_stp->ls_flags & (NFSLCK_DELEGPREV | NFSLCK_DELEGCUR))) {
2364 stp = LIST_FIRST(&lfp->lf_deleg);
2365 while (stp != LIST_END(&lfp->lf_deleg)) {
2366 nstp = LIST_NEXT(stp, ls_file);
2367 if (stp->ls_clp != clp && (stp->ls_flags & NFSLCK_DELEGREAD))
2371 if ((readonly && stp->ls_clp != clp &&
2372 (stp->ls_flags & NFSLCK_DELEGWRITE)) ||
2373 (!readonly && (stp->ls_clp != clp ||
2374 (stp->ls_flags & NFSLCK_DELEGREAD)))) {
2375 if (new_stp->ls_flags & NFSLCK_RECLAIM) {
2378 ret = nfsrv_delegconflict(stp, &haslock, p, vp);
2381 * nfsrv_delegconflict() unlocks state
2382 * when it returns non-zero.
2384 printf("Nfsd openctrl unexpected deleg cnfl\n");
2385 free((caddr_t)new_open, M_NFSDSTATE);
2386 free((caddr_t)new_deleg, M_NFSDSTATE);
2400 * We only get here if there was no open that conflicted.
2401 * If an open for the owner exists, or in the access/deny bits.
2402 * Otherwise it is a new open. If the open_owner hasn't been
2403 * confirmed, replace the open with the new one needing confirmation,
2404 * otherwise add the open.
2406 if (new_stp->ls_flags & NFSLCK_DELEGPREV) {
2408 * Handle NFSLCK_DELEGPREV by searching the old delegations for
2409 * a match. If found, just move the old delegation to the current
2410 * delegation list and issue open. If not found, return
2413 LIST_FOREACH(stp, &clp->lc_olddeleg, ls_list) {
2414 if (stp->ls_lfp == lfp) {
2416 if (stp->ls_clp != clp)
2417 panic("olddeleg clp");
2418 LIST_REMOVE(stp, ls_list);
2419 LIST_REMOVE(stp, ls_hash);
2420 stp->ls_flags &= ~NFSLCK_OLDDELEG;
2421 stp->ls_stateid.seqid = delegstateidp->seqid = 0;
2422 stp->ls_stateid.other[0] = delegstateidp->other[0] =
2423 clp->lc_clientid.lval[0];
2424 stp->ls_stateid.other[1] = delegstateidp->other[1] =
2425 clp->lc_clientid.lval[1];
2426 stp->ls_stateid.other[2] = delegstateidp->other[2] =
2427 nfsrv_nextstateindex(clp);
2428 stp->ls_compref = nd->nd_compref;
2429 LIST_INSERT_HEAD(&clp->lc_deleg, stp, ls_list);
2430 LIST_INSERT_HEAD(NFSSTATEHASH(clp,
2431 stp->ls_stateid), stp, ls_hash);
2432 if (stp->ls_flags & NFSLCK_DELEGWRITE)
2433 *rflagsp |= NFSV4OPEN_WRITEDELEGATE;
2435 *rflagsp |= NFSV4OPEN_READDELEGATE;
2436 clp->lc_delegtime = NFSD_MONOSEC +
2437 nfsrv_lease + NFSRV_LEASEDELTA;
2440 * Now, do the associated open.
2442 new_open->ls_stateid.seqid = 0;
2443 new_open->ls_stateid.other[0] = clp->lc_clientid.lval[0];
2444 new_open->ls_stateid.other[1] = clp->lc_clientid.lval[1];
2445 new_open->ls_stateid.other[2] = nfsrv_nextstateindex(clp);
2446 new_open->ls_flags = (new_stp->ls_flags&NFSLCK_DENYBITS)|
2448 if (stp->ls_flags & NFSLCK_DELEGWRITE)
2449 new_open->ls_flags |= (NFSLCK_READACCESS |
2450 NFSLCK_WRITEACCESS);
2452 new_open->ls_flags |= NFSLCK_READACCESS;
2453 new_open->ls_uid = new_stp->ls_uid;
2454 new_open->ls_lfp = lfp;
2455 new_open->ls_clp = clp;
2456 LIST_INIT(&new_open->ls_open);
2457 LIST_INSERT_HEAD(&lfp->lf_open, new_open, ls_file);
2458 LIST_INSERT_HEAD(NFSSTATEHASH(clp, new_open->ls_stateid),
2461 * and handle the open owner
2464 new_open->ls_openowner = ownerstp;
2465 LIST_INSERT_HEAD(&ownerstp->ls_open,new_open,ls_list);
2467 new_open->ls_openowner = new_stp;
2468 new_stp->ls_flags = 0;
2469 nfsrvd_refcache(new_stp->ls_op);
2470 new_stp->ls_noopens = 0;
2471 LIST_INIT(&new_stp->ls_open);
2472 LIST_INSERT_HEAD(&new_stp->ls_open, new_open, ls_list);
2473 LIST_INSERT_HEAD(&clp->lc_open, new_stp, ls_list);
2475 newnfsstats.srvopenowners++;
2476 nfsrv_openpluslock++;
2480 newnfsstats.srvopens++;
2481 nfsrv_openpluslock++;
2485 if (stp == LIST_END(&clp->lc_olddeleg))
2486 error = NFSERR_EXPIRED;
2487 } else if (new_stp->ls_flags & (NFSLCK_DELEGREAD | NFSLCK_DELEGWRITE)) {
2489 * Scan to see that no delegation for this client and file
2490 * doesn't already exist.
2491 * There also shouldn't yet be an Open for this file and
2494 LIST_FOREACH(stp, &lfp->lf_deleg, ls_file) {
2495 if (stp->ls_clp == clp)
2498 if (stp == LIST_END(&lfp->lf_deleg) && openstp == NULL) {
2500 * This is the Claim_Previous case with a delegation
2501 * type != Delegate_None.
2504 * First, add the delegation. (Although we must issue the
2505 * delegation, we can also ask for an immediate return.)
2507 new_deleg->ls_stateid.seqid = delegstateidp->seqid = 0;
2508 new_deleg->ls_stateid.other[0] = delegstateidp->other[0] =
2509 clp->lc_clientid.lval[0];
2510 new_deleg->ls_stateid.other[1] = delegstateidp->other[1] =
2511 clp->lc_clientid.lval[1];
2512 new_deleg->ls_stateid.other[2] = delegstateidp->other[2] =
2513 nfsrv_nextstateindex(clp);
2514 if (new_stp->ls_flags & NFSLCK_DELEGWRITE) {
2515 new_deleg->ls_flags = (NFSLCK_DELEGWRITE |
2516 NFSLCK_READACCESS | NFSLCK_WRITEACCESS);
2517 *rflagsp |= NFSV4OPEN_WRITEDELEGATE;
2519 new_deleg->ls_flags = (NFSLCK_DELEGREAD |
2521 *rflagsp |= NFSV4OPEN_READDELEGATE;
2523 new_deleg->ls_uid = new_stp->ls_uid;
2524 new_deleg->ls_lfp = lfp;
2525 new_deleg->ls_clp = clp;
2526 new_deleg->ls_filerev = filerev;
2527 new_deleg->ls_compref = nd->nd_compref;
2528 LIST_INSERT_HEAD(&lfp->lf_deleg, new_deleg, ls_file);
2529 LIST_INSERT_HEAD(NFSSTATEHASH(clp,
2530 new_deleg->ls_stateid), new_deleg, ls_hash);
2531 LIST_INSERT_HEAD(&clp->lc_deleg, new_deleg, ls_list);
2533 if (delegate == 2 || nfsrv_issuedelegs == 0 ||
2534 (clp->lc_flags & (LCL_CALLBACKSON | LCL_CBDOWN)) !=
2536 NFSRV_V4DELEGLIMIT(nfsrv_delegatecnt) ||
2537 !NFSVNO_DELEGOK(vp))
2538 *rflagsp |= NFSV4OPEN_RECALL;
2539 newnfsstats.srvdelegates++;
2540 nfsrv_openpluslock++;
2541 nfsrv_delegatecnt++;
2544 * Now, do the associated open.
2546 new_open->ls_stateid.seqid = 0;
2547 new_open->ls_stateid.other[0] = clp->lc_clientid.lval[0];
2548 new_open->ls_stateid.other[1] = clp->lc_clientid.lval[1];
2549 new_open->ls_stateid.other[2] = nfsrv_nextstateindex(clp);
2550 new_open->ls_flags = (new_stp->ls_flags & NFSLCK_DENYBITS) |
2552 if (new_stp->ls_flags & NFSLCK_DELEGWRITE)
2553 new_open->ls_flags |= (NFSLCK_READACCESS |
2554 NFSLCK_WRITEACCESS);
2556 new_open->ls_flags |= NFSLCK_READACCESS;
2557 new_open->ls_uid = new_stp->ls_uid;
2558 new_open->ls_lfp = lfp;
2559 new_open->ls_clp = clp;
2560 LIST_INIT(&new_open->ls_open);
2561 LIST_INSERT_HEAD(&lfp->lf_open, new_open, ls_file);
2562 LIST_INSERT_HEAD(NFSSTATEHASH(clp, new_open->ls_stateid),
2565 * and handle the open owner
2568 new_open->ls_openowner = ownerstp;
2569 LIST_INSERT_HEAD(&ownerstp->ls_open, new_open, ls_list);
2571 new_open->ls_openowner = new_stp;
2572 new_stp->ls_flags = 0;
2573 nfsrvd_refcache(new_stp->ls_op);
2574 new_stp->ls_noopens = 0;
2575 LIST_INIT(&new_stp->ls_open);
2576 LIST_INSERT_HEAD(&new_stp->ls_open, new_open, ls_list);
2577 LIST_INSERT_HEAD(&clp->lc_open, new_stp, ls_list);
2579 newnfsstats.srvopenowners++;
2580 nfsrv_openpluslock++;
2584 newnfsstats.srvopens++;
2585 nfsrv_openpluslock++;
2587 error = NFSERR_RECLAIMCONFLICT;
2589 } else if (ownerstp) {
2590 if (ownerstp->ls_flags & NFSLCK_NEEDSCONFIRM) {
2591 /* Replace the open */
2592 if (ownerstp->ls_op)
2593 nfsrvd_derefcache(ownerstp->ls_op);
2594 ownerstp->ls_op = new_stp->ls_op;
2595 nfsrvd_refcache(ownerstp->ls_op);
2596 ownerstp->ls_seq = new_stp->ls_seq;
2597 *rflagsp |= NFSV4OPEN_RESULTCONFIRM;
2598 stp = LIST_FIRST(&ownerstp->ls_open);
2599 stp->ls_flags = (new_stp->ls_flags & NFSLCK_SHAREBITS) |
2601 stp->ls_stateid.seqid = 0;
2602 stp->ls_uid = new_stp->ls_uid;
2603 if (lfp != stp->ls_lfp) {
2604 LIST_REMOVE(stp, ls_file);
2605 LIST_INSERT_HEAD(&lfp->lf_open, stp, ls_file);
2609 } else if (openstp) {
2610 openstp->ls_flags |= (new_stp->ls_flags & NFSLCK_SHAREBITS);
2611 openstp->ls_stateid.seqid++;
2614 * This is where we can choose to issue a delegation.
2616 if (delegate && nfsrv_issuedelegs &&
2617 writedeleg && !NFSVNO_EXRDONLY(exp) &&
2618 (nfsrv_writedelegifpos || !readonly) &&
2619 (clp->lc_flags & (LCL_CALLBACKSON | LCL_CBDOWN)) ==
2621 !NFSRV_V4DELEGLIMIT(nfsrv_delegatecnt) &&
2622 NFSVNO_DELEGOK(vp)) {
2623 new_deleg->ls_stateid.seqid = delegstateidp->seqid = 0;
2624 new_deleg->ls_stateid.other[0] = delegstateidp->other[0]
2625 = clp->lc_clientid.lval[0];
2626 new_deleg->ls_stateid.other[1] = delegstateidp->other[1]
2627 = clp->lc_clientid.lval[1];
2628 new_deleg->ls_stateid.other[2] = delegstateidp->other[2]
2629 = nfsrv_nextstateindex(clp);
2630 new_deleg->ls_flags = (NFSLCK_DELEGWRITE |
2631 NFSLCK_READACCESS | NFSLCK_WRITEACCESS);
2632 *rflagsp |= NFSV4OPEN_WRITEDELEGATE;
2633 new_deleg->ls_uid = new_stp->ls_uid;
2634 new_deleg->ls_lfp = lfp;
2635 new_deleg->ls_clp = clp;
2636 new_deleg->ls_filerev = filerev;
2637 new_deleg->ls_compref = nd->nd_compref;
2638 LIST_INSERT_HEAD(&lfp->lf_deleg, new_deleg, ls_file);
2639 LIST_INSERT_HEAD(NFSSTATEHASH(clp,
2640 new_deleg->ls_stateid), new_deleg, ls_hash);
2641 LIST_INSERT_HEAD(&clp->lc_deleg, new_deleg, ls_list);
2643 newnfsstats.srvdelegates++;
2644 nfsrv_openpluslock++;
2645 nfsrv_delegatecnt++;
2648 new_open->ls_stateid.seqid = 0;
2649 new_open->ls_stateid.other[0] = clp->lc_clientid.lval[0];
2650 new_open->ls_stateid.other[1] = clp->lc_clientid.lval[1];
2651 new_open->ls_stateid.other[2] = nfsrv_nextstateindex(clp);
2652 new_open->ls_flags = (new_stp->ls_flags & NFSLCK_SHAREBITS)|
2654 new_open->ls_uid = new_stp->ls_uid;
2655 new_open->ls_openowner = ownerstp;
2656 new_open->ls_lfp = lfp;
2657 new_open->ls_clp = clp;
2658 LIST_INIT(&new_open->ls_open);
2659 LIST_INSERT_HEAD(&lfp->lf_open, new_open, ls_file);
2660 LIST_INSERT_HEAD(&ownerstp->ls_open, new_open, ls_list);
2661 LIST_INSERT_HEAD(NFSSTATEHASH(clp, new_open->ls_stateid),
2665 newnfsstats.srvopens++;
2666 nfsrv_openpluslock++;
2669 * This is where we can choose to issue a delegation.
2671 if (delegate && nfsrv_issuedelegs &&
2672 (writedeleg || readonly) &&
2673 (clp->lc_flags & (LCL_CALLBACKSON | LCL_CBDOWN)) ==
2675 !NFSRV_V4DELEGLIMIT(nfsrv_delegatecnt) &&
2676 NFSVNO_DELEGOK(vp)) {
2677 new_deleg->ls_stateid.seqid = delegstateidp->seqid = 0;
2678 new_deleg->ls_stateid.other[0] = delegstateidp->other[0]
2679 = clp->lc_clientid.lval[0];
2680 new_deleg->ls_stateid.other[1] = delegstateidp->other[1]
2681 = clp->lc_clientid.lval[1];
2682 new_deleg->ls_stateid.other[2] = delegstateidp->other[2]
2683 = nfsrv_nextstateindex(clp);
2684 if (writedeleg && !NFSVNO_EXRDONLY(exp) &&
2685 (nfsrv_writedelegifpos || !readonly)) {
2686 new_deleg->ls_flags = (NFSLCK_DELEGWRITE |
2687 NFSLCK_READACCESS | NFSLCK_WRITEACCESS);
2688 *rflagsp |= NFSV4OPEN_WRITEDELEGATE;
2690 new_deleg->ls_flags = (NFSLCK_DELEGREAD |
2692 *rflagsp |= NFSV4OPEN_READDELEGATE;
2694 new_deleg->ls_uid = new_stp->ls_uid;
2695 new_deleg->ls_lfp = lfp;
2696 new_deleg->ls_clp = clp;
2697 new_deleg->ls_filerev = filerev;
2698 new_deleg->ls_compref = nd->nd_compref;
2699 LIST_INSERT_HEAD(&lfp->lf_deleg, new_deleg, ls_file);
2700 LIST_INSERT_HEAD(NFSSTATEHASH(clp,
2701 new_deleg->ls_stateid), new_deleg, ls_hash);
2702 LIST_INSERT_HEAD(&clp->lc_deleg, new_deleg, ls_list);
2704 newnfsstats.srvdelegates++;
2705 nfsrv_openpluslock++;
2706 nfsrv_delegatecnt++;
2711 * New owner case. Start the open_owner sequence with a
2712 * Needs confirmation (unless a reclaim) and hang the
2715 new_open->ls_stateid.seqid = 0;
2716 new_open->ls_stateid.other[0] = clp->lc_clientid.lval[0];
2717 new_open->ls_stateid.other[1] = clp->lc_clientid.lval[1];
2718 new_open->ls_stateid.other[2] = nfsrv_nextstateindex(clp);
2719 new_open->ls_flags = (new_stp->ls_flags & NFSLCK_SHAREBITS) |
2721 new_open->ls_uid = new_stp->ls_uid;
2722 LIST_INIT(&new_open->ls_open);
2723 new_open->ls_openowner = new_stp;
2724 new_open->ls_lfp = lfp;
2725 new_open->ls_clp = clp;
2726 LIST_INSERT_HEAD(&lfp->lf_open, new_open, ls_file);
2727 if (new_stp->ls_flags & NFSLCK_RECLAIM) {
2728 new_stp->ls_flags = 0;
2730 *rflagsp |= NFSV4OPEN_RESULTCONFIRM;
2731 new_stp->ls_flags = NFSLCK_NEEDSCONFIRM;
2733 nfsrvd_refcache(new_stp->ls_op);
2734 new_stp->ls_noopens = 0;
2735 LIST_INIT(&new_stp->ls_open);
2736 LIST_INSERT_HEAD(&new_stp->ls_open, new_open, ls_list);
2737 LIST_INSERT_HEAD(&clp->lc_open, new_stp, ls_list);
2738 LIST_INSERT_HEAD(NFSSTATEHASH(clp, new_open->ls_stateid),
2743 newnfsstats.srvopens++;
2744 nfsrv_openpluslock++;
2745 newnfsstats.srvopenowners++;
2746 nfsrv_openpluslock++;
2749 stateidp->seqid = openstp->ls_stateid.seqid;
2750 stateidp->other[0] = openstp->ls_stateid.other[0];
2751 stateidp->other[1] = openstp->ls_stateid.other[1];
2752 stateidp->other[2] = openstp->ls_stateid.other[2];
2756 NFSLOCKV4ROOTMUTEX();
2757 nfsv4_unlock(&nfsv4rootfs_lock, 1);
2758 NFSUNLOCKV4ROOTMUTEX();
2761 FREE((caddr_t)new_open, M_NFSDSTATE);
2763 FREE((caddr_t)new_deleg, M_NFSDSTATE);
2768 * Open update. Does the confirm, downgrade and close.
2771 nfsrv_openupdate(vnode_t vp, struct nfsstate *new_stp, nfsquad_t clientid,
2772 nfsv4stateid_t *stateidp, struct nfsrv_descript *nd, NFSPROC_T *p)
2774 struct nfsstate *stp, *ownerstp;
2775 struct nfsclient *clp;
2776 struct nfslockfile *lfp;
2778 int error, gotstate = 0, len = 0, ret, freedlock;
2779 u_char client[NFSV4_OPAQUELIMIT];
2782 * Check for restart conditions (client and server).
2784 error = nfsrv_checkrestart(clientid, new_stp->ls_flags,
2785 &new_stp->ls_stateid, 0);
2791 * Get the open structure via clientid and stateid.
2793 error = nfsrv_getclient(clientid, CLOPS_RENEW, &clp,
2794 (nfsquad_t)((u_quad_t)0), NULL, p);
2796 error = nfsrv_getstate(clp, &new_stp->ls_stateid,
2797 new_stp->ls_flags, &stp);
2800 * Sanity check the open.
2802 if (!error && (!(stp->ls_flags & NFSLCK_OPEN) ||
2803 (!(new_stp->ls_flags & NFSLCK_CONFIRM) &&
2804 (stp->ls_openowner->ls_flags & NFSLCK_NEEDSCONFIRM)) ||
2805 ((new_stp->ls_flags & NFSLCK_CONFIRM) &&
2806 (!(stp->ls_openowner->ls_flags & NFSLCK_NEEDSCONFIRM)))))
2807 error = NFSERR_BADSTATEID;
2810 error = nfsrv_checkseqid(nd, new_stp->ls_seq,
2811 stp->ls_openowner, new_stp->ls_op);
2812 if (!error && stp->ls_stateid.seqid != new_stp->ls_stateid.seqid &&
2813 !(new_stp->ls_flags & NFSLCK_CONFIRM))
2814 error = NFSERR_OLDSTATEID;
2815 if (!error && vnode_vtype(vp) != VREG) {
2816 if (vnode_vtype(vp) == VDIR)
2817 error = NFSERR_ISDIR;
2819 error = NFSERR_INVAL;
2824 * If a client tries to confirm an Open with a bad
2825 * seqid# and there are no byte range locks or other Opens
2826 * on the openowner, just throw it away, so the next use of the
2827 * openowner will start a fresh seq#.
2829 if (error == NFSERR_BADSEQID &&
2830 (new_stp->ls_flags & NFSLCK_CONFIRM) &&
2831 nfsrv_nootherstate(stp))
2832 nfsrv_freeopenowner(stp->ls_openowner, 0, p);
2838 * Set the return stateid.
2840 stateidp->seqid = stp->ls_stateid.seqid + 1;
2841 stateidp->other[0] = stp->ls_stateid.other[0];
2842 stateidp->other[1] = stp->ls_stateid.other[1];
2843 stateidp->other[2] = stp->ls_stateid.other[2];
2845 * Now, handle the three cases.
2847 if (new_stp->ls_flags & NFSLCK_CONFIRM) {
2849 * If the open doesn't need confirmation, it seems to me that
2850 * there is a client error, but I'll just log it and keep going?
2852 if (!(stp->ls_openowner->ls_flags & NFSLCK_NEEDSCONFIRM))
2853 printf("Nfsv4d: stray open confirm\n");
2854 stp->ls_openowner->ls_flags = 0;
2855 stp->ls_stateid.seqid++;
2856 if (!(clp->lc_flags & LCL_STAMPEDSTABLE)) {
2857 clp->lc_flags |= LCL_STAMPEDSTABLE;
2858 len = clp->lc_idlen;
2859 NFSBCOPY(clp->lc_id, client, len);
2863 } else if (new_stp->ls_flags & NFSLCK_CLOSE) {
2864 ownerstp = stp->ls_openowner;
2867 ret = nfsrv_freeopen(stp, &freedlock, 0, p);
2868 /* See comment on nfsrv_lockctrl() w.r.t. locallocks. */
2872 if (LIST_EMPTY(&lfp->lf_lock))
2876 * For now, I won't do this. The openowner should be
2877 * free'd in NFSNOOPEN seconds and it will be deref'd then.
2878 if (LIST_EMPTY(&ownerstp->ls_open) && ownerstp->ls_op) {
2879 nfsrvd_derefcache(ownerstp->ls_op);
2880 ownerstp->ls_op = NULL;
2884 if (freedlock && lfp != NULL)
2885 nfsrv_locallocks(vp, lfp, p);
2888 * Update the share bits, making sure that the new set are a
2889 * subset of the old ones.
2891 bits = (new_stp->ls_flags & NFSLCK_SHAREBITS);
2892 if (~(stp->ls_flags) & bits) {
2894 return (NFSERR_INVAL);
2896 stp->ls_flags = (bits | NFSLCK_OPEN);
2897 stp->ls_stateid.seqid++;
2902 * If the client just confirmed its first open, write a timestamp
2903 * to the stable storage file.
2906 nfsrv_writestable(client, len, NFSNST_NEWSTATE, p);
2911 * Delegation update. Does the purge and return.
2914 nfsrv_delegupdate(nfsquad_t clientid, nfsv4stateid_t *stateidp,
2915 vnode_t vp, int op, struct ucred *cred, NFSPROC_T *p)
2917 struct nfsstate *stp;
2918 struct nfsclient *clp;
2923 * Do a sanity check against the file handle for DelegReturn.
2926 error = nfsvno_getfh(vp, &fh, p);
2931 * Check for restart conditions (client and server).
2933 if (op == NFSV4OP_DELEGRETURN)
2934 error = nfsrv_checkrestart(clientid, NFSLCK_DELEGRETURN,
2937 error = nfsrv_checkrestart(clientid, NFSLCK_DELEGPURGE,
2942 * Get the open structure via clientid and stateid.
2945 error = nfsrv_getclient(clientid, CLOPS_RENEW, &clp,
2946 (nfsquad_t)((u_quad_t)0), NULL, p);
2948 if (error == NFSERR_CBPATHDOWN)
2950 if (error == NFSERR_STALECLIENTID && op == NFSV4OP_DELEGRETURN)
2951 error = NFSERR_STALESTATEID;
2953 if (!error && op == NFSV4OP_DELEGRETURN) {
2954 error = nfsrv_getstate(clp, stateidp, NFSLCK_DELEGRETURN, &stp);
2955 if (!error && stp->ls_stateid.seqid != stateidp->seqid)
2956 error = NFSERR_OLDSTATEID;
2959 * NFSERR_EXPIRED means that the state has gone away,
2960 * so Delegations have been purged. Just return ok.
2962 if (error == NFSERR_EXPIRED && op == NFSV4OP_DELEGPURGE) {
2971 if (op == NFSV4OP_DELEGRETURN) {
2972 if (NFSBCMP((caddr_t)&fh, (caddr_t)&stp->ls_lfp->lf_fh,
2973 sizeof (fhandle_t))) {
2975 return (NFSERR_BADSTATEID);
2977 nfsrv_freedeleg(stp);
2979 nfsrv_freedeleglist(&clp->lc_olddeleg);
2986 * Release lock owner.
2989 nfsrv_releaselckown(struct nfsstate *new_stp, nfsquad_t clientid,
2992 struct nfsstate *stp, *nstp, *openstp, *ownstp;
2993 struct nfsclient *clp;
2997 * Check for restart conditions (client and server).
2999 error = nfsrv_checkrestart(clientid, new_stp->ls_flags,
3000 &new_stp->ls_stateid, 0);
3006 * Get the lock owner by name.
3008 error = nfsrv_getclient(clientid, CLOPS_RENEW, &clp,
3009 (nfsquad_t)((u_quad_t)0), NULL, p);
3014 LIST_FOREACH(ownstp, &clp->lc_open, ls_list) {
3015 LIST_FOREACH(openstp, &ownstp->ls_open, ls_list) {
3016 stp = LIST_FIRST(&openstp->ls_open);
3017 while (stp != LIST_END(&openstp->ls_open)) {
3018 nstp = LIST_NEXT(stp, ls_list);
3020 * If the owner matches, check for locks and
3021 * then free or return an error.
3023 if (stp->ls_ownerlen == new_stp->ls_ownerlen &&
3024 !NFSBCMP(stp->ls_owner, new_stp->ls_owner,
3026 if (LIST_EMPTY(&stp->ls_lock)) {
3027 (void) nfsrv_freelockowner(stp, NULL, 0, p);
3030 return (NFSERR_LOCKSHELD);
3042 * Get the file handle for a lock structure.
3045 nfsrv_getlockfh(vnode_t vp, u_short flags,
3046 struct nfslockfile **new_lfpp, fhandle_t *nfhp, NFSPROC_T *p)
3048 fhandle_t *fhp = NULL;
3049 struct nfslockfile *new_lfp;
3053 * For lock, use the new nfslock structure, otherwise just
3054 * a fhandle_t on the stack.
3056 if (flags & NFSLCK_OPEN) {
3057 new_lfp = *new_lfpp;
3058 fhp = &new_lfp->lf_fh;
3062 panic("nfsrv_getlockfh");
3064 error = nfsvno_getfh(vp, fhp, p);
3069 * Get an nfs lock structure. Allocate one, as required, and return a
3071 * Returns an NFSERR_xxx upon failure or -1 to indicate no current lock.
3074 nfsrv_getlockfile(u_short flags, struct nfslockfile **new_lfpp,
3075 struct nfslockfile **lfpp, fhandle_t *nfhp)
3077 struct nfslockfile *lfp;
3078 fhandle_t *fhp = NULL, *tfhp;
3079 struct nfslockhashhead *hp;
3080 struct nfslockfile *new_lfp = NULL;
3083 * For lock, use the new nfslock structure, otherwise just
3084 * a fhandle_t on the stack.
3086 if (flags & NFSLCK_OPEN) {
3087 new_lfp = *new_lfpp;
3088 fhp = &new_lfp->lf_fh;
3092 panic("nfsrv_getlockfile");
3095 hp = NFSLOCKHASH(fhp);
3096 LIST_FOREACH(lfp, hp, lf_hash) {
3098 if (NFSVNO_CMPFH(fhp, tfhp)) {
3103 if (!(flags & NFSLCK_OPEN))
3107 * No match, so chain the new one into the list.
3109 LIST_INIT(&new_lfp->lf_open);
3110 LIST_INIT(&new_lfp->lf_lock);
3111 LIST_INIT(&new_lfp->lf_deleg);
3112 LIST_INSERT_HEAD(hp, new_lfp, lf_hash);
3119 * This function adds a nfslock lock structure to the list for the associated
3120 * nfsstate and nfslockfile structures. It will be inserted after the
3121 * entry pointed at by insert_lop.
3122 * Must be called with soft clock interrupts disabled.
3125 nfsrv_insertlock(struct nfslock *new_lop, struct nfslock *insert_lop,
3126 struct nfsstate *stp, struct nfslockfile *lfp)
3128 struct nfslock *lop, *nlop;
3130 new_lop->lo_stp = stp;
3131 new_lop->lo_lfp = lfp;
3133 /* Insert in increasing lo_first order */
3134 lop = LIST_FIRST(&lfp->lf_lock);
3135 if (lop == LIST_END(&lfp->lf_lock) ||
3136 new_lop->lo_first <= lop->lo_first) {
3137 LIST_INSERT_HEAD(&lfp->lf_lock, new_lop, lo_lckfile);
3139 nlop = LIST_NEXT(lop, lo_lckfile);
3140 while (nlop != LIST_END(&lfp->lf_lock) &&
3141 nlop->lo_first < new_lop->lo_first) {
3143 nlop = LIST_NEXT(lop, lo_lckfile);
3145 LIST_INSERT_AFTER(lop, new_lop, lo_lckfile);
3149 * Insert after insert_lop, which is overloaded as stp for
3152 if ((struct nfsstate *)insert_lop == stp)
3153 LIST_INSERT_HEAD(&stp->ls_lock, new_lop, lo_lckowner);
3155 LIST_INSERT_AFTER(insert_lop, new_lop, lo_lckowner);
3156 newnfsstats.srvlocks++;
3157 nfsrv_openpluslock++;
3161 * This function updates the locking for a lock owner and given file. It
3162 * maintains a list of lock ranges ordered on increasing file offset that
3163 * are NFSLCK_READ or NFSLCK_WRITE and non-overlapping (aka POSIX style).
3164 * It always adds new_lop to the list and sometimes uses the one pointed
3166 * Must be called with soft clock interrupts disabled.
3169 nfsrv_updatelock(struct nfsstate *stp, struct nfslock **new_lopp,
3170 struct nfslock **other_lopp, struct nfslockfile *lfp)
3172 struct nfslock *new_lop = *new_lopp;
3173 struct nfslock *lop, *tlop, *ilop;
3174 struct nfslock *other_lop = *other_lopp;
3175 int unlock = 0, myfile = 0;
3179 * Work down the list until the lock is merged.
3181 if (new_lop->lo_flags & NFSLCK_UNLOCK)
3183 ilop = (struct nfslock *)stp;
3184 lop = LIST_FIRST(&stp->ls_lock);
3185 while (lop != LIST_END(&stp->ls_lock)) {
3187 * Only check locks for this file that aren't before the start of
3190 if (lop->lo_lfp == lfp) {
3192 if (lop->lo_end >= new_lop->lo_first) {
3193 if (new_lop->lo_end < lop->lo_first) {
3195 * If the new lock ends before the start of the
3196 * current lock's range, no merge, just insert
3201 if (new_lop->lo_flags == lop->lo_flags ||
3202 (new_lop->lo_first <= lop->lo_first &&
3203 new_lop->lo_end >= lop->lo_end)) {
3205 * This lock can be absorbed by the new lock/unlock.
3206 * This happens when it covers the entire range
3207 * of the old lock or is contiguous
3208 * with the old lock and is of the same type or an
3211 if (lop->lo_first < new_lop->lo_first)
3212 new_lop->lo_first = lop->lo_first;
3213 if (lop->lo_end > new_lop->lo_end)
3214 new_lop->lo_end = lop->lo_end;
3216 lop = LIST_NEXT(lop, lo_lckowner);
3217 nfsrv_freenfslock(tlop);
3222 * All these cases are for contiguous locks that are not the
3223 * same type, so they can't be merged.
3225 if (new_lop->lo_first <= lop->lo_first) {
3227 * This case is where the new lock overlaps with the
3228 * first part of the old lock. Move the start of the
3229 * old lock to just past the end of the new lock. The
3230 * new lock will be inserted in front of the old, since
3231 * ilop hasn't been updated. (We are done now.)
3233 lop->lo_first = new_lop->lo_end;
3236 if (new_lop->lo_end >= lop->lo_end) {
3238 * This case is where the new lock overlaps with the
3239 * end of the old lock's range. Move the old lock's
3240 * end to just before the new lock's first and insert
3241 * the new lock after the old lock.
3242 * Might not be done yet, since the new lock could
3243 * overlap further locks with higher ranges.
3245 lop->lo_end = new_lop->lo_first;
3247 lop = LIST_NEXT(lop, lo_lckowner);
3251 * The final case is where the new lock's range is in the
3252 * middle of the current lock's and splits the current lock
3253 * up. Use *other_lopp to handle the second part of the
3254 * split old lock range. (We are done now.)
3255 * For unlock, we use new_lop as other_lop and tmp, since
3256 * other_lop and new_lop are the same for this case.
3257 * We noted the unlock case above, so we don't need
3258 * new_lop->lo_flags any longer.
3260 tmp = new_lop->lo_first;
3261 if (other_lop == NULL) {
3263 panic("nfsd srv update unlock");
3264 other_lop = new_lop;
3267 other_lop->lo_first = new_lop->lo_end;
3268 other_lop->lo_end = lop->lo_end;
3269 other_lop->lo_flags = lop->lo_flags;
3270 other_lop->lo_stp = stp;
3271 other_lop->lo_lfp = lfp;
3273 nfsrv_insertlock(other_lop, lop, stp, lfp);
3280 lop = LIST_NEXT(lop, lo_lckowner);
3281 if (myfile && (lop == LIST_END(&stp->ls_lock) ||
3282 lop->lo_lfp != lfp))
3287 * Insert the new lock in the list at the appropriate place.
3290 nfsrv_insertlock(new_lop, ilop, stp, lfp);
3296 * This function handles sequencing of locks, etc.
3297 * It returns an error that indicates what the caller should do.
3300 nfsrv_checkseqid(struct nfsrv_descript *nd, u_int32_t seqid,
3301 struct nfsstate *stp, struct nfsrvcache *op)
3304 if (op != nd->nd_rp)
3305 panic("nfsrvstate checkseqid");
3306 if (!(op->rc_flag & RC_INPROG))
3307 panic("nfsrvstate not inprog");
3308 if (stp->ls_op && stp->ls_op->rc_refcnt <= 0) {
3309 printf("refcnt=%d\n", stp->ls_op->rc_refcnt);
3310 panic("nfsrvstate op refcnt");
3312 if ((stp->ls_seq + 1) == seqid) {
3314 nfsrvd_derefcache(stp->ls_op);
3316 nfsrvd_refcache(op);
3317 stp->ls_seq = seqid;
3319 } else if (stp->ls_seq == seqid && stp->ls_op &&
3320 op->rc_xid == stp->ls_op->rc_xid &&
3321 op->rc_refcnt == 0 &&
3322 op->rc_reqlen == stp->ls_op->rc_reqlen &&
3323 op->rc_cksum == stp->ls_op->rc_cksum) {
3324 if (stp->ls_op->rc_flag & RC_INPROG)
3325 return (NFSERR_DONTREPLY);
3326 nd->nd_rp = stp->ls_op;
3327 nd->nd_rp->rc_flag |= RC_INPROG;
3328 nfsrvd_delcache(op);
3329 return (NFSERR_REPLYFROMCACHE);
3331 return (NFSERR_BADSEQID);
3335 * Get the client ip address for callbacks. If the strings can't be parsed,
3336 * just set lc_program to 0 to indicate no callbacks are possible.
3337 * (For cases where the address can't be parsed or is 0.0.0.0.0.0, set
3338 * the address to the client's transport address. This won't be used
3339 * for callbacks, but can be printed out by newnfsstats for info.)
3340 * Return error if the xdr can't be parsed, 0 otherwise.
3343 nfsrv_getclientipaddr(struct nfsrv_descript *nd, struct nfsclient *clp)
3348 struct sockaddr_in *rad, *sad;
3349 u_char protocol[5], addr[24];
3350 int error = 0, cantparse = 0;
3360 rad = NFSSOCKADDR(clp->lc_req.nr_nam, struct sockaddr_in *);
3361 rad->sin_family = AF_INET;
3362 rad->sin_len = sizeof (struct sockaddr_in);
3363 rad->sin_addr.s_addr = 0;
3365 clp->lc_req.nr_client = NULL;
3366 clp->lc_req.nr_lock = 0;
3367 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
3368 i = fxdr_unsigned(int, *tl);
3369 if (i >= 3 && i <= 4) {
3370 error = nfsrv_mtostr(nd, protocol, i);
3373 if (!strcmp(protocol, "tcp")) {
3374 clp->lc_flags |= LCL_TCPCALLBACK;
3375 clp->lc_req.nr_sotype = SOCK_STREAM;
3376 clp->lc_req.nr_soproto = IPPROTO_TCP;
3377 } else if (!strcmp(protocol, "udp")) {
3378 clp->lc_req.nr_sotype = SOCK_DGRAM;
3379 clp->lc_req.nr_soproto = IPPROTO_UDP;
3386 error = nfsm_advance(nd, NFSM_RNDUP(i), -1);
3391 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
3392 i = fxdr_unsigned(int, *tl);
3394 error = NFSERR_BADXDR;
3396 } else if (i == 0) {
3398 } else if (!cantparse && i <= 23 && i >= 11) {
3399 error = nfsrv_mtostr(nd, addr, i);
3404 * Parse out the address fields. We expect 6 decimal numbers
3405 * separated by '.'s.
3409 while (*cp && i < 6) {
3411 while (*cp2 && *cp2 != '.')
3419 j = nfsrv_getipnumber(cp);
3424 port.cval[5 - i] = j;
3433 if (ip.ival != 0x0) {
3434 rad->sin_addr.s_addr = htonl(ip.ival);
3435 rad->sin_port = htons(port.sval);
3443 error = nfsm_advance(nd, NFSM_RNDUP(i), -1);
3449 sad = NFSSOCKADDR(nd->nd_nam, struct sockaddr_in *);
3450 rad->sin_addr.s_addr = sad->sin_addr.s_addr;
3451 rad->sin_port = 0x0;
3452 clp->lc_program = 0;
3459 * Turn a string of up to three decimal digits into a number. Return -1 upon
3463 nfsrv_getipnumber(u_char *cp)
3468 if (j > 2 || *cp < '0' || *cp > '9')
3481 * This function checks for restart conditions.
3484 nfsrv_checkrestart(nfsquad_t clientid, u_int32_t flags,
3485 nfsv4stateid_t *stateidp, int specialid)
3490 * First check for a server restart. Open, LockT, ReleaseLockOwner
3491 * and DelegPurge have a clientid, the rest a stateid.
3494 (NFSLCK_OPEN | NFSLCK_TEST | NFSLCK_RELEASE | NFSLCK_DELEGPURGE)) {
3495 if (clientid.lval[0] != nfsrvboottime)
3496 return (NFSERR_STALECLIENTID);
3497 } else if (stateidp->other[0] != nfsrvboottime &&
3499 return (NFSERR_STALESTATEID);
3502 * Read, Write, Setattr and LockT can return NFSERR_GRACE and do
3503 * not use a lock/open owner seqid#, so the check can be done now.
3504 * (The others will be checked, as required, later.)
3506 if (!(flags & (NFSLCK_CHECK | NFSLCK_TEST)))
3510 ret = nfsrv_checkgrace(flags);
3519 nfsrv_checkgrace(u_int32_t flags)
3522 if (nfsrv_stablefirst.nsf_flags & NFSNSF_GRACEOVER) {
3523 if (flags & NFSLCK_RECLAIM)
3524 return (NFSERR_NOGRACE);
3526 if (!(flags & NFSLCK_RECLAIM))
3527 return (NFSERR_GRACE);
3530 * If grace is almost over and we are still getting Reclaims,
3531 * extend grace a bit.
3533 if ((NFSD_MONOSEC + NFSRV_LEASEDELTA) >
3534 nfsrv_stablefirst.nsf_eograce)
3535 nfsrv_stablefirst.nsf_eograce = NFSD_MONOSEC +
3542 * Do a server callback.
3545 nfsrv_docallback(struct nfsclient *clp, int procnum,
3546 nfsv4stateid_t *stateidp, int trunc, fhandle_t *fhp,
3547 struct nfsvattr *nap, nfsattrbit_t *attrbitp, NFSPROC_T *p)
3551 struct nfsrv_descript nfsd, *nd = &nfsd;
3556 cred = newnfs_getcred();
3557 NFSLOCKSTATE(); /* mostly for lc_cbref++ */
3558 if (clp->lc_flags & LCL_NEEDSCONFIRM) {
3565 * Fill the callback program# and version into the request
3566 * structure for newnfs_connect() to use.
3568 clp->lc_req.nr_prog = clp->lc_program;
3569 clp->lc_req.nr_vers = NFSV4_CBVERS;
3572 * First, fill in some of the fields of nd and cr.
3574 nd->nd_flag = ND_NFSV4;
3575 if (clp->lc_flags & LCL_GSS)
3576 nd->nd_flag |= ND_KERBV;
3578 cred->cr_uid = clp->lc_uid;
3579 cred->cr_gid = clp->lc_gid;
3580 callback = clp->lc_callback;
3582 cred->cr_ngroups = 1;
3585 * Get the first mbuf for the request.
3587 MGET(m, M_WAIT, MT_DATA);
3589 nd->nd_mreq = nd->nd_mb = m;
3590 nd->nd_bpos = NFSMTOD(m, caddr_t);
3593 * and build the callback request.
3595 if (procnum == NFSV4OP_CBGETATTR) {
3596 nd->nd_procnum = NFSV4PROC_CBCOMPOUND;
3597 (void) nfsm_strtom(nd, "CB Getattr", 10);
3598 NFSM_BUILD(tl, u_int32_t *, 4 * NFSX_UNSIGNED);
3599 *tl++ = txdr_unsigned(NFSV4_MINORVERSION);
3600 *tl++ = txdr_unsigned(callback);
3601 *tl++ = txdr_unsigned(1);
3602 *tl = txdr_unsigned(NFSV4OP_CBGETATTR);
3603 (void) nfsm_fhtom(nd, (u_int8_t *)fhp, NFSX_MYFH, 0);
3604 (void) nfsrv_putattrbit(nd, attrbitp);
3605 } else if (procnum == NFSV4OP_CBRECALL) {
3606 nd->nd_procnum = NFSV4PROC_CBCOMPOUND;
3607 (void) nfsm_strtom(nd, "CB Recall", 9);
3608 NFSM_BUILD(tl, u_int32_t *, 5 * NFSX_UNSIGNED + NFSX_STATEID);
3609 *tl++ = txdr_unsigned(NFSV4_MINORVERSION);
3610 *tl++ = txdr_unsigned(callback);
3611 *tl++ = txdr_unsigned(1);
3612 *tl++ = txdr_unsigned(NFSV4OP_CBRECALL);
3613 *tl++ = txdr_unsigned(stateidp->seqid);
3614 NFSBCOPY((caddr_t)stateidp->other, (caddr_t)tl,
3616 tl += (NFSX_STATEIDOTHER / NFSX_UNSIGNED);
3621 (void) nfsm_fhtom(nd, (u_int8_t *)fhp, NFSX_MYFH, 0);
3623 nd->nd_procnum = NFSV4PROC_CBNULL;
3627 * Call newnfs_connect(), as required, and then newnfs_request().
3629 (void) newnfs_sndlock(&clp->lc_req.nr_lock);
3630 if (clp->lc_req.nr_client == NULL) {
3631 if (nd->nd_procnum == NFSV4PROC_CBNULL)
3632 error = newnfs_connect(NULL, &clp->lc_req, cred,
3635 error = newnfs_connect(NULL, &clp->lc_req, cred,
3638 newnfs_sndunlock(&clp->lc_req.nr_lock);
3640 error = newnfs_request(nd, NULL, clp, &clp->lc_req, NULL,
3641 NULL, cred, clp->lc_program, NFSV4_CBVERS, NULL, 1, NULL);
3646 * If error is set here, the Callback path isn't working
3647 * properly, so twiddle the appropriate LCL_ flags.
3648 * (nd_repstat != 0 indicates the Callback path is working,
3649 * but the callback failed on the client.)
3653 * Mark the callback pathway down, which disabled issuing
3654 * of delegations and gets Renew to return NFSERR_CBPATHDOWN.
3657 clp->lc_flags |= LCL_CBDOWN;
3661 * Callback worked. If the callback path was down, disable
3662 * callbacks, so no more delegations will be issued. (This
3663 * is done on the assumption that the callback pathway is
3667 if (clp->lc_flags & LCL_CBDOWN)
3668 clp->lc_flags &= ~(LCL_CBDOWN | LCL_CALLBACKSON);
3671 error = nd->nd_repstat;
3672 else if (procnum == NFSV4OP_CBGETATTR)
3673 error = nfsv4_loadattr(nd, NULL, nap, NULL, NULL, 0,
3674 NULL, NULL, NULL, NULL, NULL, 0, NULL, NULL, NULL,
3676 mbuf_freem(nd->nd_mrep);
3680 if ((clp->lc_flags & LCL_WAKEUPWANTED) && clp->lc_cbref == 0) {
3681 clp->lc_flags &= ~LCL_WAKEUPWANTED;
3683 wakeup((caddr_t)clp);
3691 * Return the next index# for a clientid. Mostly just increment and return
3692 * the next one, but... if the 32bit unsigned does actually wrap around,
3693 * it should be rebooted.
3694 * At an average rate of one new client per second, it will wrap around in
3695 * approximately 136 years. (I think the server will have been shut
3696 * down or rebooted before then.)
3699 nfsrv_nextclientindex(void)
3701 static u_int32_t client_index = 0;
3704 if (client_index != 0)
3705 return (client_index);
3707 printf("%s: out of clientids\n", __func__);
3708 return (client_index);
3712 * Return the next index# for a stateid. Mostly just increment and return
3713 * the next one, but... if the 32bit unsigned does actually wrap around
3714 * (will a BSD server stay up that long?), find
3715 * new start and end values.
3718 nfsrv_nextstateindex(struct nfsclient *clp)
3720 struct nfsstate *stp;
3722 u_int32_t canuse, min_index, max_index;
3724 if (!(clp->lc_flags & LCL_INDEXNOTOK)) {
3725 clp->lc_stateindex++;
3726 if (clp->lc_stateindex != clp->lc_statemaxindex)
3727 return (clp->lc_stateindex);
3731 * Yuck, we've hit the end.
3732 * Look for a new min and max.
3735 max_index = 0xffffffff;
3736 for (i = 0; i < NFSSTATEHASHSIZE; i++) {
3737 LIST_FOREACH(stp, &clp->lc_stateid[i], ls_hash) {
3738 if (stp->ls_stateid.other[2] > 0x80000000) {
3739 if (stp->ls_stateid.other[2] < max_index)
3740 max_index = stp->ls_stateid.other[2];
3742 if (stp->ls_stateid.other[2] > min_index)
3743 min_index = stp->ls_stateid.other[2];
3749 * Yikes, highly unlikely, but I'll handle it anyhow.
3751 if (min_index == 0x80000000 && max_index == 0x80000001) {
3754 * Loop around until we find an unused entry. Return that
3755 * and set LCL_INDEXNOTOK, so the search will continue next time.
3756 * (This is one of those rare cases where a goto is the
3757 * cleanest way to code the loop.)
3760 for (i = 0; i < NFSSTATEHASHSIZE; i++) {
3761 LIST_FOREACH(stp, &clp->lc_stateid[i], ls_hash) {
3762 if (stp->ls_stateid.other[2] == canuse) {
3768 clp->lc_flags |= LCL_INDEXNOTOK;
3773 * Ok to start again from min + 1.
3775 clp->lc_stateindex = min_index + 1;
3776 clp->lc_statemaxindex = max_index;
3777 clp->lc_flags &= ~LCL_INDEXNOTOK;
3778 return (clp->lc_stateindex);
3782 * The following functions handle the stable storage file that deals with
3783 * the edge conditions described in RFC3530 Sec. 8.6.3.
3784 * The file is as follows:
3785 * - a single record at the beginning that has the lease time of the
3786 * previous server instance (before the last reboot) and the nfsrvboottime
3787 * values for the previous server boots.
3788 * These previous boot times are used to ensure that the current
3789 * nfsrvboottime does not, somehow, get set to a previous one.
3790 * (This is important so that Stale ClientIDs and StateIDs can
3792 * The number of previous nfsvrboottime values preceeds the list.
3793 * - followed by some number of appended records with:
3794 * - client id string
3795 * - flag that indicates it is a record revoking state via lease
3796 * expiration or similar
3797 * OR has successfully acquired state.
3798 * These structures vary in length, with the client string at the end, up
3799 * to NFSV4_OPAQUELIMIT in size.
3801 * At the end of the grace period, the file is truncated, the first
3802 * record is rewritten with updated information and any acquired state
3803 * records for successful reclaims of state are written.
3805 * Subsequent records are appended when the first state is issued to
3806 * a client and when state is revoked for a client.
3808 * When reading the file in, state issued records that come later in
3809 * the file override older ones, since the append log is in cronological order.
3810 * If, for some reason, the file can't be read, the grace period is
3811 * immediately terminated and all reclaims get NFSERR_NOGRACE.
3815 * Read in the stable storage file. Called by nfssvc() before the nfsd
3816 * processes start servicing requests.
3819 nfsrv_setupstable(NFSPROC_T *p)
3821 struct nfsrv_stablefirst *sf = &nfsrv_stablefirst;
3822 struct nfsrv_stable *sp, *nsp;
3823 struct nfst_rec *tsp;
3824 int error, i, tryagain;
3827 struct timeval curtime;
3830 * If NFSNSF_UPDATEDONE is set, this is a restart of the nfsds without
3831 * a reboot, so state has not been lost.
3833 if (sf->nsf_flags & NFSNSF_UPDATEDONE)
3836 * Set Grace over just until the file reads successfully.
3838 NFSGETTIME(&curtime);
3839 nfsrvboottime = curtime.tv_sec;
3840 LIST_INIT(&sf->nsf_head);
3841 sf->nsf_flags = (NFSNSF_GRACEOVER | NFSNSF_NEEDLOCK);
3842 sf->nsf_eograce = NFSD_MONOSEC + NFSRV_LEASEDELTA;
3843 if (sf->nsf_fp == NULL)
3845 error = NFSD_RDWR(UIO_READ, NFSFPVNODE(sf->nsf_fp),
3846 (caddr_t)&sf->nsf_rec, sizeof (struct nfsf_rec), off, UIO_SYSSPACE,
3847 0, NFSFPCRED(sf->nsf_fp), &aresid, p);
3848 if (error || aresid || sf->nsf_numboots == 0 ||
3849 sf->nsf_numboots > NFSNSF_MAXNUMBOOTS)
3853 * Now, read in the boottimes.
3855 sf->nsf_bootvals = (time_t *)malloc((sf->nsf_numboots + 1) *
3856 sizeof (time_t), M_TEMP, M_WAITOK);
3857 off = sizeof (struct nfsf_rec);
3858 error = NFSD_RDWR(UIO_READ, NFSFPVNODE(sf->nsf_fp),
3859 (caddr_t)sf->nsf_bootvals, sf->nsf_numboots * sizeof (time_t), off,
3860 UIO_SYSSPACE, 0, NFSFPCRED(sf->nsf_fp), &aresid, p);
3861 if (error || aresid) {
3862 free((caddr_t)sf->nsf_bootvals, M_TEMP);
3863 sf->nsf_bootvals = NULL;
3868 * Make sure this nfsrvboottime is different from all recorded
3873 for (i = 0; i < sf->nsf_numboots; i++) {
3874 if (nfsrvboottime == sf->nsf_bootvals[i]) {
3882 sf->nsf_flags |= NFSNSF_OK;
3883 off += (sf->nsf_numboots * sizeof (time_t));
3886 * Read through the file, building a list of records for grace
3888 * Each record is between sizeof (struct nfst_rec) and
3889 * sizeof (struct nfst_rec) + NFSV4_OPAQUELIMIT - 1
3890 * and is actually sizeof (struct nfst_rec) + nst_len - 1.
3892 tsp = (struct nfst_rec *)malloc(sizeof (struct nfst_rec) +
3893 NFSV4_OPAQUELIMIT - 1, M_TEMP, M_WAITOK);
3895 error = NFSD_RDWR(UIO_READ, NFSFPVNODE(sf->nsf_fp),
3896 (caddr_t)tsp, sizeof (struct nfst_rec) + NFSV4_OPAQUELIMIT - 1,
3897 off, UIO_SYSSPACE, 0, NFSFPCRED(sf->nsf_fp), &aresid, p);
3898 len = (sizeof (struct nfst_rec) + NFSV4_OPAQUELIMIT - 1) - aresid;
3899 if (error || (len > 0 && (len < sizeof (struct nfst_rec) ||
3900 len < (sizeof (struct nfst_rec) + tsp->len - 1)))) {
3902 * Yuck, the file has been corrupted, so just return
3903 * after clearing out any restart state, so the grace period
3906 LIST_FOREACH_SAFE(sp, &sf->nsf_head, nst_list, nsp) {
3907 LIST_REMOVE(sp, nst_list);
3908 free((caddr_t)sp, M_TEMP);
3910 free((caddr_t)tsp, M_TEMP);
3911 sf->nsf_flags &= ~NFSNSF_OK;
3912 free((caddr_t)sf->nsf_bootvals, M_TEMP);
3913 sf->nsf_bootvals = NULL;
3917 off += sizeof (struct nfst_rec) + tsp->len - 1;
3919 * Search the list for a matching client.
3921 LIST_FOREACH(sp, &sf->nsf_head, nst_list) {
3922 if (tsp->len == sp->nst_len &&
3923 !NFSBCMP(tsp->client, sp->nst_client, tsp->len))
3926 if (sp == LIST_END(&sf->nsf_head)) {
3927 sp = (struct nfsrv_stable *)malloc(tsp->len +
3928 sizeof (struct nfsrv_stable) - 1, M_TEMP,
3930 NFSBCOPY((caddr_t)tsp, (caddr_t)&sp->nst_rec,
3931 sizeof (struct nfst_rec) + tsp->len - 1);
3932 LIST_INSERT_HEAD(&sf->nsf_head, sp, nst_list);
3934 if (tsp->flag == NFSNST_REVOKE)
3935 sp->nst_flag |= NFSNST_REVOKE;
3938 * A subsequent timestamp indicates the client
3939 * did a setclientid/confirm and any previous
3940 * revoke is no longer relevant.
3942 sp->nst_flag &= ~NFSNST_REVOKE;
3946 free((caddr_t)tsp, M_TEMP);
3947 sf->nsf_flags = NFSNSF_OK;
3948 sf->nsf_eograce = NFSD_MONOSEC + sf->nsf_lease +
3953 * Update the stable storage file, now that the grace period is over.
3956 nfsrv_updatestable(NFSPROC_T *p)
3958 struct nfsrv_stablefirst *sf = &nfsrv_stablefirst;
3959 struct nfsrv_stable *sp, *nsp;
3961 struct nfsvattr nva;
3963 #if defined(__FreeBSD_version) && (__FreeBSD_version >= 500000)
3968 if (sf->nsf_fp == NULL || (sf->nsf_flags & NFSNSF_UPDATEDONE))
3970 sf->nsf_flags |= NFSNSF_UPDATEDONE;
3972 * Ok, we need to rewrite the stable storage file.
3973 * - truncate to 0 length
3974 * - write the new first structure
3975 * - loop through the data structures, writing out any that
3976 * have timestamps older than the old boot
3978 if (sf->nsf_bootvals) {
3980 for (i = sf->nsf_numboots - 2; i >= 0; i--)
3981 sf->nsf_bootvals[i + 1] = sf->nsf_bootvals[i];
3983 sf->nsf_numboots = 1;
3984 sf->nsf_bootvals = (time_t *)malloc(sizeof (time_t),
3987 sf->nsf_bootvals[0] = nfsrvboottime;
3988 sf->nsf_lease = nfsrv_lease;
3989 NFSVNO_ATTRINIT(&nva);
3990 NFSVNO_SETATTRVAL(&nva, size, 0);
3991 vp = NFSFPVNODE(sf->nsf_fp);
3992 NFS_STARTWRITE(vp, &mp);
3993 NFSVOPLOCK(vp, LK_EXCLUSIVE | LK_RETRY, p);
3994 error = nfsvno_setattr(vp, &nva, NFSFPCRED(sf->nsf_fp), p, NULL);
3996 NFSVOPUNLOCK(vp, 0, p);
3998 error = NFSD_RDWR(UIO_WRITE, vp,
3999 (caddr_t)&sf->nsf_rec, sizeof (struct nfsf_rec), (off_t)0,
4000 UIO_SYSSPACE, IO_SYNC, NFSFPCRED(sf->nsf_fp), NULL, p);
4002 error = NFSD_RDWR(UIO_WRITE, vp,
4003 (caddr_t)sf->nsf_bootvals,
4004 sf->nsf_numboots * sizeof (time_t),
4005 (off_t)(sizeof (struct nfsf_rec)),
4006 UIO_SYSSPACE, IO_SYNC, NFSFPCRED(sf->nsf_fp), NULL, p);
4007 free((caddr_t)sf->nsf_bootvals, M_TEMP);
4008 sf->nsf_bootvals = NULL;
4010 sf->nsf_flags &= ~NFSNSF_OK;
4011 printf("EEK! Can't write NfsV4 stable storage file\n");
4014 sf->nsf_flags |= NFSNSF_OK;
4017 * Loop through the list and write out timestamp records for
4018 * any clients that successfully reclaimed state.
4020 LIST_FOREACH_SAFE(sp, &sf->nsf_head, nst_list, nsp) {
4021 if (sp->nst_flag & NFSNST_GOTSTATE) {
4022 nfsrv_writestable(sp->nst_client, sp->nst_len,
4023 NFSNST_NEWSTATE, p);
4024 sp->nst_clp->lc_flags |= LCL_STAMPEDSTABLE;
4026 LIST_REMOVE(sp, nst_list);
4027 free((caddr_t)sp, M_TEMP);
4032 * Append a record to the stable storage file.
4035 nfsrv_writestable(u_char *client, int len, int flag, NFSPROC_T *p)
4037 struct nfsrv_stablefirst *sf = &nfsrv_stablefirst;
4038 struct nfst_rec *sp;
4041 if (!(sf->nsf_flags & NFSNSF_OK) || sf->nsf_fp == NULL)
4043 sp = (struct nfst_rec *)malloc(sizeof (struct nfst_rec) +
4044 len - 1, M_TEMP, M_WAITOK);
4046 NFSBCOPY(client, sp->client, len);
4048 error = NFSD_RDWR(UIO_WRITE, NFSFPVNODE(sf->nsf_fp),
4049 (caddr_t)sp, sizeof (struct nfst_rec) + len - 1, (off_t)0,
4050 UIO_SYSSPACE, (IO_SYNC | IO_APPEND), NFSFPCRED(sf->nsf_fp), NULL, p);
4051 free((caddr_t)sp, M_TEMP);
4053 sf->nsf_flags &= ~NFSNSF_OK;
4054 printf("EEK! Can't write NfsV4 stable storage file\n");
4059 * This function is called during the grace period to mark a client
4060 * that successfully reclaimed state.
4063 nfsrv_markstable(struct nfsclient *clp)
4065 struct nfsrv_stable *sp;
4068 * First find the client structure.
4070 LIST_FOREACH(sp, &nfsrv_stablefirst.nsf_head, nst_list) {
4071 if (sp->nst_len == clp->lc_idlen &&
4072 !NFSBCMP(sp->nst_client, clp->lc_id, sp->nst_len))
4075 if (sp == LIST_END(&nfsrv_stablefirst.nsf_head))
4079 * Now, just mark it and set the nfsclient back pointer.
4081 sp->nst_flag |= NFSNST_GOTSTATE;
4086 * This function is called for a reclaim, to see if it gets grace.
4087 * It returns 0 if a reclaim is allowed, 1 otherwise.
4090 nfsrv_checkstable(struct nfsclient *clp)
4092 struct nfsrv_stable *sp;
4095 * First, find the entry for the client.
4097 LIST_FOREACH(sp, &nfsrv_stablefirst.nsf_head, nst_list) {
4098 if (sp->nst_len == clp->lc_idlen &&
4099 !NFSBCMP(sp->nst_client, clp->lc_id, sp->nst_len))
4104 * If not in the list, state was revoked or no state was issued
4105 * since the previous reboot, a reclaim is denied.
4107 if (sp == LIST_END(&nfsrv_stablefirst.nsf_head) ||
4108 (sp->nst_flag & NFSNST_REVOKE) ||
4109 !(nfsrv_stablefirst.nsf_flags & NFSNSF_OK))
4115 * Test for and try to clear out a conflicting client. This is called by
4116 * nfsrv_lockctrl() and nfsrv_openctrl() when conflicts with other clients
4118 * The trick here is that it can't revoke a conflicting client with an
4119 * expired lease unless it holds the v4root lock, so...
4120 * If no v4root lock, get the lock and return 1 to indicate "try again".
4121 * Return 0 to indicate the conflict can't be revoked and 1 to indicate
4122 * the revocation worked and the conflicting client is "bye, bye", so it
4123 * can be tried again.
4124 * Unlocks State before a non-zero value is returned.
4127 nfsrv_clientconflict(struct nfsclient *clp, int *haslockp, __unused vnode_t vp,
4133 * If lease hasn't expired, we can't fix it.
4135 if (clp->lc_expiry >= NFSD_MONOSEC ||
4136 !(nfsrv_stablefirst.nsf_flags & NFSNSF_UPDATEDONE))
4138 if (*haslockp == 0) {
4140 NFSVOPUNLOCK(vp, 0, p);
4141 NFSLOCKV4ROOTMUTEX();
4142 nfsv4_relref(&nfsv4rootfs_lock);
4144 gotlock = nfsv4_lock(&nfsv4rootfs_lock, 1, NULL,
4145 NFSV4ROOTLOCKMUTEXPTR);
4147 NFSUNLOCKV4ROOTMUTEX();
4148 NFSLOCKSTATE(); /* to avoid a race with */
4149 NFSUNLOCKSTATE(); /* nfsrv_servertimer() */
4151 NFSVOPLOCK(vp, LK_EXCLUSIVE | LK_RETRY, p);
4157 * Ok, we can expire the conflicting client.
4159 nfsrv_writestable(clp->lc_id, clp->lc_idlen, NFSNST_REVOKE, p);
4160 nfsrv_cleanclient(clp, p);
4161 nfsrv_freedeleglist(&clp->lc_deleg);
4162 nfsrv_freedeleglist(&clp->lc_olddeleg);
4163 LIST_REMOVE(clp, lc_hash);
4164 nfsrv_zapclient(clp, p);
4170 * Resolve a delegation conflict.
4171 * Returns 0 to indicate the conflict was resolved without sleeping.
4172 * Return -1 to indicate that the caller should check for conflicts again.
4173 * Return > 0 for an error that should be returned, normally NFSERR_DELAY.
4175 * Also, manipulate the nfsv4root_lock, as required. It isn't changed
4176 * for a return of 0, since there was no sleep and it could be required
4177 * later. It is released for a return of NFSERR_DELAY, since the caller
4178 * will return that error. It is released when a sleep was done waiting
4179 * for the delegation to be returned or expire (so that other nfsds can
4180 * handle ops). Then, it must be acquired for the write to stable storage.
4181 * (This function is somewhat similar to nfsrv_clientconflict(), but
4182 * the semantics differ in a couple of subtle ways. The return of 0
4183 * indicates the conflict was resolved without sleeping here, not
4184 * that the conflict can't be resolved and the handling of nfsv4root_lock
4185 * differs, as noted above.)
4186 * Unlocks State before returning a non-zero value.
4189 nfsrv_delegconflict(struct nfsstate *stp, int *haslockp, NFSPROC_T *p,
4190 __unused vnode_t vp)
4192 struct nfsclient *clp = stp->ls_clp;
4193 int gotlock, error, retrycnt, zapped_clp;
4194 nfsv4stateid_t tstateid;
4198 * If the conflict is with an old delegation...
4200 if (stp->ls_flags & NFSLCK_OLDDELEG) {
4202 * You can delete it, if it has expired.
4204 if (clp->lc_delegtime < NFSD_MONOSEC) {
4205 nfsrv_freedeleg(stp);
4211 * During this delay, the old delegation could expire or it
4212 * could be recovered by the client via an Open with
4213 * CLAIM_DELEGATE_PREV.
4214 * Release the nfsv4root_lock, if held.
4218 NFSLOCKV4ROOTMUTEX();
4219 nfsv4_unlock(&nfsv4rootfs_lock, 1);
4220 NFSUNLOCKV4ROOTMUTEX();
4222 return (NFSERR_DELAY);
4226 * It's a current delegation, so:
4227 * - check to see if the delegation has expired
4228 * - if so, get the v4root lock and then expire it
4230 if (!(stp->ls_flags & NFSLCK_DELEGRECALL)) {
4232 * - do a recall callback, since not yet done
4233 * For now, never allow truncate to be set. To use
4234 * truncate safely, it must be guaranteed that the
4235 * Remove, Rename or Setattr with size of 0 will
4236 * succeed and that would require major changes to
4237 * the VFS/Vnode OPs.
4238 * Set the expiry time large enough so that it won't expire
4239 * until after the callback, then set it correctly, once
4240 * the callback is done. (The delegation will now time
4241 * out whether or not the Recall worked ok. The timeout
4242 * will be extended when ops are done on the delegation
4243 * stateid, up to the timelimit.)
4245 stp->ls_delegtime = NFSD_MONOSEC + (2 * nfsrv_lease) +
4247 stp->ls_delegtimelimit = NFSD_MONOSEC + (6 * nfsrv_lease) +
4249 stp->ls_flags |= NFSLCK_DELEGRECALL;
4252 * Loop NFSRV_CBRETRYCNT times while the CBRecall replies
4253 * NFSERR_BADSTATEID or NFSERR_BADHANDLE. This is done
4254 * in order to try and avoid a race that could happen
4255 * when a CBRecall request passed the Open reply with
4256 * the delegation in it when transitting the network.
4257 * Since nfsrv_docallback will sleep, don't use stp after
4260 NFSBCOPY((caddr_t)&stp->ls_stateid, (caddr_t)&tstateid,
4262 NFSBCOPY((caddr_t)&stp->ls_lfp->lf_fh, (caddr_t)&tfh,
4267 NFSLOCKV4ROOTMUTEX();
4268 nfsv4_unlock(&nfsv4rootfs_lock, 1);
4269 NFSUNLOCKV4ROOTMUTEX();
4273 error = nfsrv_docallback(clp, NFSV4OP_CBRECALL,
4274 &tstateid, 0, &tfh, NULL, NULL, p);
4276 } while ((error == NFSERR_BADSTATEID ||
4277 error == NFSERR_BADHANDLE) && retrycnt < NFSV4_CBRETRYCNT);
4278 return (NFSERR_DELAY);
4281 if (clp->lc_expiry >= NFSD_MONOSEC &&
4282 stp->ls_delegtime >= NFSD_MONOSEC) {
4285 * A recall has been done, but it has not yet expired.
4290 NFSLOCKV4ROOTMUTEX();
4291 nfsv4_unlock(&nfsv4rootfs_lock, 1);
4292 NFSUNLOCKV4ROOTMUTEX();
4294 return (NFSERR_DELAY);
4298 * If we don't yet have the lock, just get it and then return,
4299 * since we need that before deleting expired state, such as
4301 * When getting the lock, unlock the vnode, so other nfsds that
4302 * are in progress, won't get stuck waiting for the vnode lock.
4304 if (*haslockp == 0) {
4306 NFSVOPUNLOCK(vp, 0, p);
4307 NFSLOCKV4ROOTMUTEX();
4308 nfsv4_relref(&nfsv4rootfs_lock);
4310 gotlock = nfsv4_lock(&nfsv4rootfs_lock, 1, NULL,
4311 NFSV4ROOTLOCKMUTEXPTR);
4313 NFSUNLOCKV4ROOTMUTEX();
4314 NFSLOCKSTATE(); /* to avoid a race with */
4315 NFSUNLOCKSTATE(); /* nfsrv_servertimer() */
4317 NFSVOPLOCK(vp, LK_EXCLUSIVE | LK_RETRY, p);
4323 * Ok, we can delete the expired delegation.
4324 * First, write the Revoke record to stable storage and then
4325 * clear out the conflict.
4326 * Since all other nfsd threads are now blocked, we can safely
4327 * sleep without the state changing.
4329 nfsrv_writestable(clp->lc_id, clp->lc_idlen, NFSNST_REVOKE, p);
4330 if (clp->lc_expiry < NFSD_MONOSEC) {
4331 nfsrv_cleanclient(clp, p);
4332 nfsrv_freedeleglist(&clp->lc_deleg);
4333 nfsrv_freedeleglist(&clp->lc_olddeleg);
4334 LIST_REMOVE(clp, lc_hash);
4337 nfsrv_freedeleg(stp);
4341 nfsrv_zapclient(clp, p);
4346 * Check for a remove allowed, if remove is set to 1 and get rid of
4350 nfsrv_checkremove(vnode_t vp, int remove, NFSPROC_T *p)
4352 struct nfsstate *stp;
4353 struct nfslockfile *lfp;
4354 int error, haslock = 0;
4358 * First, get the lock file structure.
4359 * (A return of -1 means no associated state, so remove ok.)
4361 error = nfsrv_getlockfh(vp, NFSLCK_CHECK, NULL, &nfh, p);
4365 error = nfsrv_getlockfile(NFSLCK_CHECK, NULL, &lfp, &nfh);
4369 NFSLOCKV4ROOTMUTEX();
4370 nfsv4_unlock(&nfsv4rootfs_lock, 1);
4371 NFSUNLOCKV4ROOTMUTEX();
4379 * Now, we must Recall any delegations.
4381 error = nfsrv_cleandeleg(vp, lfp, NULL, &haslock, p);
4384 * nfsrv_cleandeleg() unlocks state for non-zero
4390 NFSLOCKV4ROOTMUTEX();
4391 nfsv4_unlock(&nfsv4rootfs_lock, 1);
4392 NFSUNLOCKV4ROOTMUTEX();
4398 * Now, look for a conflicting open share.
4401 LIST_FOREACH(stp, &lfp->lf_open, ls_file) {
4402 if (stp->ls_flags & NFSLCK_WRITEDENY) {
4403 error = NFSERR_FILEOPEN;
4411 NFSLOCKV4ROOTMUTEX();
4412 nfsv4_unlock(&nfsv4rootfs_lock, 1);
4413 NFSUNLOCKV4ROOTMUTEX();
4419 * Clear out all delegations for the file referred to by lfp.
4420 * May return NFSERR_DELAY, if there will be a delay waiting for
4421 * delegations to expire.
4422 * Returns -1 to indicate it slept while recalling a delegation.
4423 * This function has the side effect of deleting the nfslockfile structure,
4424 * if it no longer has associated state and didn't have to sleep.
4425 * Unlocks State before a non-zero value is returned.
4428 nfsrv_cleandeleg(vnode_t vp, struct nfslockfile *lfp,
4429 struct nfsclient *clp, int *haslockp, NFSPROC_T *p)
4431 struct nfsstate *stp, *nstp;
4434 stp = LIST_FIRST(&lfp->lf_deleg);
4435 while (stp != LIST_END(&lfp->lf_deleg)) {
4436 nstp = LIST_NEXT(stp, ls_file);
4437 if (stp->ls_clp != clp) {
4438 ret = nfsrv_delegconflict(stp, haslockp, p, vp);
4441 * nfsrv_delegconflict() unlocks state
4442 * when it returns non-zero.
4453 * There are certain operations that, when being done outside of NFSv4,
4454 * require that any NFSv4 delegation for the file be recalled.
4455 * This function is to be called for those cases:
4456 * VOP_RENAME() - When a delegation is being recalled for any reason,
4457 * the client may have to do Opens against the server, using the file's
4458 * final component name. If the file has been renamed on the server,
4459 * that component name will be incorrect and the Open will fail.
4460 * VOP_REMOVE() - Theoretically, a client could Open a file after it has
4461 * been removed on the server, if there is a delegation issued to
4462 * that client for the file. I say "theoretically" since clients
4463 * normally do an Access Op before the Open and that Access Op will
4464 * fail with ESTALE. Note that NFSv2 and 3 don't even do Opens, so
4465 * they will detect the file's removal in the same manner. (There is
4466 * one case where RFC3530 allows a client to do an Open without first
4467 * doing an Access Op, which is passage of a check against the ACE
4468 * returned with a Write delegation, but current practice is to ignore
4469 * the ACE and always do an Access Op.)
4470 * Since the functions can only be called with an unlocked vnode, this
4471 * can't be done at this time.
4472 * VOP_ADVLOCK() - When a client holds a delegation, it can issue byte range
4473 * locks locally in the client, which are not visible to the server. To
4474 * deal with this, issuing of delegations for a vnode must be disabled
4475 * and all delegations for the vnode recalled. This is done via the
4476 * second function, using the VV_DISABLEDELEG vflag on the vnode.
4479 nfsd_recalldelegation(vnode_t vp, NFSPROC_T *p)
4481 struct timespec mytime;
4485 KASSERT(!VOP_ISLOCKED(vp), ("vp %p is locked", vp));
4488 * First, check to see if the server is currently running and it has
4489 * been called for a regular file when issuing delegations.
4491 if (newnfs_numnfsd == 0 || vp->v_type != VREG ||
4492 nfsrv_issuedelegs == 0)
4496 * Now, call nfsrv_checkremove() in a loop while it returns
4497 * NFSERR_DELAY. Return upon any other error or when timed out.
4499 NFSGETNANOTIME(&mytime);
4500 starttime = (u_int32_t)mytime.tv_sec;
4502 error = nfsrv_checkremove(vp, 0, p);
4503 if (error == NFSERR_DELAY) {
4504 NFSGETNANOTIME(&mytime);
4505 if (((u_int32_t)mytime.tv_sec - starttime) >
4507 ((u_int32_t)mytime.tv_sec - starttime) <
4510 /* Sleep for a short period of time */
4511 (void) nfs_catnap(PZERO, "nfsremove");
4513 } while (error == NFSERR_DELAY);
4517 nfsd_disabledelegation(vnode_t vp, NFSPROC_T *p)
4520 #ifdef VV_DISABLEDELEG
4522 * First, flag issuance of delegations disabled.
4524 atomic_set_long(&vp->v_vflag, VV_DISABLEDELEG);
4528 * Then call nfsd_recalldelegation() to get rid of all extant
4531 nfsd_recalldelegation(vp, p);
4535 * Check for conflicting locks, etc. and then get rid of delegations.
4536 * (At one point I thought that I should get rid of delegations for any
4537 * Setattr, since it could potentially disallow the I/O op (read or write)
4538 * allowed by the delegation. However, Setattr Ops that aren't changing
4539 * the size get a stateid of all 0s, so you can't tell if it is a delegation
4540 * for the same client or a different one, so I decided to only get rid
4541 * of delegations for other clients when the size is being changed.)
4542 * In general, a Setattr can disable NFS I/O Ops that are outstanding, such
4543 * as Write backs, even if there is no delegation, so it really isn't any
4547 nfsrv_checksetattr(vnode_t vp, struct nfsrv_descript *nd,
4548 nfsv4stateid_t *stateidp, struct nfsvattr *nvap, nfsattrbit_t *attrbitp,
4549 struct nfsexstuff *exp, NFSPROC_T *p)
4551 struct nfsstate st, *stp = &st;
4552 struct nfslock lo, *lop = &lo;
4556 if (NFSISSET_ATTRBIT(attrbitp, NFSATTRBIT_SIZE)) {
4557 stp->ls_flags = (NFSLCK_CHECK | NFSLCK_WRITEACCESS);
4558 lop->lo_first = nvap->na_size;
4563 if (NFSISSET_ATTRBIT(attrbitp, NFSATTRBIT_OWNER) ||
4564 NFSISSET_ATTRBIT(attrbitp, NFSATTRBIT_OWNERGROUP) ||
4565 NFSISSET_ATTRBIT(attrbitp, NFSATTRBIT_MODE) ||
4566 NFSISSET_ATTRBIT(attrbitp, NFSATTRBIT_ACL))
4567 stp->ls_flags |= NFSLCK_SETATTR;
4568 if (stp->ls_flags == 0)
4570 lop->lo_end = NFS64BITSSET;
4571 lop->lo_flags = NFSLCK_WRITE;
4572 stp->ls_ownerlen = 0;
4574 stp->ls_uid = nd->nd_cred->cr_uid;
4575 stp->ls_stateid.seqid = stateidp->seqid;
4576 clientid.lval[0] = stp->ls_stateid.other[0] = stateidp->other[0];
4577 clientid.lval[1] = stp->ls_stateid.other[1] = stateidp->other[1];
4578 stp->ls_stateid.other[2] = stateidp->other[2];
4579 error = nfsrv_lockctrl(vp, &stp, &lop, NULL, clientid,
4580 stateidp, exp, nd, p);
4585 * Check for a write delegation and do a CBGETATTR if there is one, updating
4586 * the attributes, as required.
4587 * Should I return an error if I can't get the attributes? (For now, I'll
4591 nfsrv_checkgetattr(struct nfsrv_descript *nd, vnode_t vp,
4592 struct nfsvattr *nvap, nfsattrbit_t *attrbitp, struct ucred *cred,
4595 struct nfsstate *stp;
4596 struct nfslockfile *lfp;
4597 struct nfsclient *clp;
4598 struct nfsvattr nva;
4601 nfsattrbit_t cbbits;
4602 u_quad_t delegfilerev;
4604 NFSCBGETATTR_ATTRBIT(attrbitp, &cbbits);
4605 if (!NFSNONZERO_ATTRBIT(&cbbits))
4609 * Get the lock file structure.
4610 * (A return of -1 means no associated state, so return ok.)
4612 error = nfsrv_getlockfh(vp, NFSLCK_CHECK, NULL, &nfh, p);
4615 error = nfsrv_getlockfile(NFSLCK_CHECK, NULL, &lfp, &nfh);
4624 * Now, look for a write delegation.
4626 LIST_FOREACH(stp, &lfp->lf_deleg, ls_file) {
4627 if (stp->ls_flags & NFSLCK_DELEGWRITE)
4630 if (stp == LIST_END(&lfp->lf_deleg)) {
4635 delegfilerev = stp->ls_filerev;
4638 * If the Write delegation was issued as a part of this Compound RPC
4639 * or if we have an Implied Clientid (used in a previous Op in this
4640 * compound) and it is the client the delegation was issued to,
4642 * I also assume that it is from the same client iff the network
4643 * host IP address is the same as the callback address. (Not
4644 * exactly correct by the RFC, but avoids a lot of Getattr
4647 if (nd->nd_compref == stp->ls_compref ||
4648 ((nd->nd_flag & ND_IMPLIEDCLID) &&
4649 clp->lc_clientid.qval == nd->nd_clientid.qval) ||
4650 nfsaddr2_match(clp->lc_req.nr_nam, nd->nd_nam)) {
4656 * We are now done with the delegation state structure,
4657 * so the statelock can be released and we can now tsleep().
4661 * Now, we must do the CB Getattr callback, to see if Change or Size
4664 if (clp->lc_expiry >= NFSD_MONOSEC) {
4666 NFSVNO_ATTRINIT(&nva);
4667 nva.na_filerev = NFS64BITSSET;
4668 error = nfsrv_docallback(clp, NFSV4OP_CBGETATTR, NULL,
4669 0, &nfh, &nva, &cbbits, p);
4671 if ((nva.na_filerev != NFS64BITSSET &&
4672 nva.na_filerev > delegfilerev) ||
4673 (NFSVNO_ISSETSIZE(&nva) &&
4674 nva.na_size != nvap->na_size)) {
4675 nfsvno_updfilerev(vp, nvap, cred, p);
4676 if (NFSVNO_ISSETSIZE(&nva))
4677 nvap->na_size = nva.na_size;
4687 * This function looks for openowners that haven't had any opens for
4688 * a while and throws them away. Called by an nfsd when NFSNSF_NOOPENS
4692 nfsrv_throwawayopens(NFSPROC_T *p)
4694 struct nfsclient *clp, *nclp;
4695 struct nfsstate *stp, *nstp;
4699 nfsrv_stablefirst.nsf_flags &= ~NFSNSF_NOOPENS;
4701 * For each client...
4703 for (i = 0; i < NFSCLIENTHASHSIZE; i++) {
4704 LIST_FOREACH_SAFE(clp, &nfsclienthash[i], lc_hash, nclp) {
4705 LIST_FOREACH_SAFE(stp, &clp->lc_open, ls_list, nstp) {
4706 if (LIST_EMPTY(&stp->ls_open) &&
4707 (stp->ls_noopens > NFSNOOPEN ||
4708 (nfsrv_openpluslock * 2) >
4709 NFSRV_V4STATELIMIT))
4710 nfsrv_freeopenowner(stp, 0, p);
4718 * This function checks to see if the credentials are the same.
4719 * Returns 1 for not same, 0 otherwise.
4722 nfsrv_notsamecredname(struct nfsrv_descript *nd, struct nfsclient *clp)
4725 if (nd->nd_flag & ND_GSS) {
4726 if (!(clp->lc_flags & LCL_GSS))
4728 if (clp->lc_flags & LCL_NAME) {
4729 if (nd->nd_princlen != clp->lc_namelen ||
4730 NFSBCMP(nd->nd_principal, clp->lc_name,
4736 if (nd->nd_cred->cr_uid == clp->lc_uid)
4740 } else if (clp->lc_flags & LCL_GSS)
4743 * For AUTH_SYS, allow the same uid or root. (This is underspecified
4744 * in RFC3530, which talks about principals, but doesn't say anything
4745 * about uids for AUTH_SYS.)
4747 if (nd->nd_cred->cr_uid == clp->lc_uid || nd->nd_cred->cr_uid == 0)
4754 * Calculate the lease expiry time.
4757 nfsrv_leaseexpiry(void)
4759 struct timeval curtime;
4761 NFSGETTIME(&curtime);
4762 if (nfsrv_stablefirst.nsf_eograce > NFSD_MONOSEC)
4763 return (NFSD_MONOSEC + 2 * (nfsrv_lease + NFSRV_LEASEDELTA));
4764 return (NFSD_MONOSEC + nfsrv_lease + NFSRV_LEASEDELTA);
4768 * Delay the delegation timeout as far as ls_delegtimelimit, as required.
4771 nfsrv_delaydelegtimeout(struct nfsstate *stp)
4774 if ((stp->ls_flags & NFSLCK_DELEGRECALL) == 0)
4777 if ((stp->ls_delegtime + 15) > NFSD_MONOSEC &&
4778 stp->ls_delegtime < stp->ls_delegtimelimit) {
4779 stp->ls_delegtime += nfsrv_lease;
4780 if (stp->ls_delegtime > stp->ls_delegtimelimit)
4781 stp->ls_delegtime = stp->ls_delegtimelimit;
4786 * Go through a lock list and set local locks for all ranges.
4787 * This assumes that the lock list is sorted on increasing
4788 * lo_first and that the list won't change, despite the possibility
4792 nfsrv_locallocks(vnode_t vp, struct nfslockfile *lfp,
4795 struct nfslock *lop, *nlop;
4797 int newcollate, flags = 0;
4798 u_int64_t first = 0x0ull, end = 0x0ull;
4800 if (!nfsrv_dolocallocks)
4803 * If vp is NULL, a vnode must be aquired from the file
4808 panic("nfsrv_locallocks");
4809 tvp = nfsvno_getvp(&lfp->lf_fh);
4817 * If lfp == NULL, the lock list is empty, so just unlock
4821 (void) nfsvno_advlock(tvp, F_UNLCK, (u_int64_t)0,
4823 /* vp can't be NULL */
4827 /* handle whole file case first */
4828 lop = LIST_FIRST(&lfp->lf_lock);
4829 if (lop != LIST_END(&lfp->lf_lock) &&
4830 lop->lo_first == (u_int64_t)0 &&
4831 lop->lo_end == NFS64BITSSET) {
4832 if (lop->lo_flags & NFSLCK_WRITE)
4833 (void) nfsvno_advlock(tvp, F_WRLCK, lop->lo_first,
4836 (void) nfsvno_advlock(tvp, F_RDLCK, lop->lo_first,
4844 * Now, handle the separate byte ranges cases.
4846 (void) nfsvno_advlock(tvp, F_UNLCK, (u_int64_t)0,
4849 while (lop != LIST_END(&lfp->lf_lock)) {
4850 nlop = LIST_NEXT(lop, lo_lckfile);
4852 first = lop->lo_first;
4854 flags = lop->lo_flags;
4857 if (nlop != LIST_END(&lfp->lf_lock) &&
4858 flags == nlop->lo_flags &&
4859 end >= nlop->lo_first) {
4860 /* can collate this one */
4863 /* do the local lock and start again */
4864 if (flags & NFSLCK_WRITE)
4865 (void) nfsvno_advlock(tvp, F_WRLCK, first,
4868 (void) nfsvno_advlock(tvp, F_RDLCK, first,
4879 * This function checks to see if there is any other state associated
4880 * with the openowner for this Open.
4881 * It returns 1 if there is no other state, 0 otherwise.
4884 nfsrv_nootherstate(struct nfsstate *stp)
4886 struct nfsstate *tstp;
4888 LIST_FOREACH(tstp, &stp->ls_openowner->ls_open, ls_list) {
4889 if (tstp != stp || !LIST_EMPTY(&tstp->ls_lock))