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 = 0;
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, vnode_t vp, int cansleep,
70 static void nfsrv_freelockowner(struct nfsstate *stp, vnode_t vp, int cansleep,
72 static void nfsrv_freeallnfslocks(struct nfsstate *stp, vnode_t vp,
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, struct nfslockfile **new_lfpp,
84 struct nfslockfile **lfpp, fhandle_t *nfhp, int lockit);
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 int nfsrv_nootherstate(struct nfsstate *stp);
113 static int nfsrv_locallock(vnode_t vp, struct nfslockfile *lfp, int flags,
114 uint64_t first, uint64_t end, struct nfslockconflict *cfp, NFSPROC_T *p);
115 static void nfsrv_localunlock(vnode_t vp, struct nfslockfile *lfp,
116 uint64_t init_first, uint64_t init_end, NFSPROC_T *p);
117 static int nfsrv_dolocal(vnode_t vp, struct nfslockfile *lfp, int flags,
118 int oldflags, uint64_t first, uint64_t end, struct nfslockconflict *cfp,
120 static void nfsrv_locallock_rollback(vnode_t vp, struct nfslockfile *lfp,
122 static void nfsrv_locallock_commit(struct nfslockfile *lfp, int flags,
123 uint64_t first, uint64_t end);
124 static void nfsrv_locklf(struct nfslockfile *lfp);
125 static void nfsrv_unlocklf(struct nfslockfile *lfp);
128 * Scan the client list for a match and either return the current one,
129 * create a new entry or return an error.
130 * If returning a non-error, the clp structure must either be linked into
131 * the client list or free'd.
134 nfsrv_setclient(struct nfsrv_descript *nd, struct nfsclient **new_clpp,
135 nfsquad_t *clientidp, nfsquad_t *confirmp, NFSPROC_T *p)
137 struct nfsclient *clp = NULL, *new_clp = *new_clpp;
139 struct nfsstate *stp, *tstp;
140 struct sockaddr_in *sad, *rad;
141 int zapit = 0, gotit, hasstate = 0, igotlock;
142 static u_int64_t confirm_index = 0;
145 * Check for state resource limit exceeded.
147 if (nfsrv_openpluslock > NFSRV_V4STATELIMIT)
148 return (NFSERR_RESOURCE);
150 if ((nd->nd_flag & ND_GSS) && nfsrv_nogsscallback)
152 * Don't do callbacks for AUTH_GSS.
153 * (Since these aren't yet debugged, they might cause the
154 * server to crap out, if they get past the Init call to
157 new_clp->lc_program = 0;
159 /* Lock out other nfsd threads */
160 NFSLOCKV4ROOTMUTEX();
161 nfsv4_relref(&nfsv4rootfs_lock);
163 igotlock = nfsv4_lock(&nfsv4rootfs_lock, 1, NULL,
164 NFSV4ROOTLOCKMUTEXPTR);
166 NFSUNLOCKV4ROOTMUTEX();
167 NFSLOCKSTATE(); /* to avoid a race with */
168 NFSUNLOCKSTATE(); /* nfsrv_servertimer() */
171 * Search for a match in the client list.
174 while (i < NFSCLIENTHASHSIZE && !gotit) {
175 LIST_FOREACH(clp, &nfsclienthash[i], lc_hash) {
176 if (new_clp->lc_idlen == clp->lc_idlen &&
177 !NFSBCMP(new_clp->lc_id, clp->lc_id, clp->lc_idlen)) {
185 (clp->lc_flags & (LCL_NEEDSCONFIRM | LCL_ADMINREVOKED))) {
187 * Get rid of the old one.
189 if (i != NFSCLIENTHASHSIZE) {
190 LIST_REMOVE(clp, lc_hash);
191 nfsrv_cleanclient(clp, p);
192 nfsrv_freedeleglist(&clp->lc_deleg);
193 nfsrv_freedeleglist(&clp->lc_olddeleg);
197 * Add it after assigning a client id to it.
199 new_clp->lc_flags |= LCL_NEEDSCONFIRM;
200 confirmp->qval = new_clp->lc_confirm.qval = ++confirm_index;
201 clientidp->lval[0] = new_clp->lc_clientid.lval[0] =
202 (u_int32_t)nfsrvboottime;
203 clientidp->lval[1] = new_clp->lc_clientid.lval[1] =
204 nfsrv_nextclientindex();
205 new_clp->lc_stateindex = 0;
206 new_clp->lc_statemaxindex = 0;
207 new_clp->lc_cbref = 0;
208 new_clp->lc_expiry = nfsrv_leaseexpiry();
209 LIST_INIT(&new_clp->lc_open);
210 LIST_INIT(&new_clp->lc_deleg);
211 LIST_INIT(&new_clp->lc_olddeleg);
212 for (i = 0; i < NFSSTATEHASHSIZE; i++)
213 LIST_INIT(&new_clp->lc_stateid[i]);
214 LIST_INSERT_HEAD(NFSCLIENTHASH(new_clp->lc_clientid), new_clp,
216 newnfsstats.srvclients++;
217 nfsrv_openpluslock++;
219 NFSLOCKV4ROOTMUTEX();
220 nfsv4_unlock(&nfsv4rootfs_lock, 1);
221 NFSUNLOCKV4ROOTMUTEX();
223 nfsrv_zapclient(clp, p);
229 * Now, handle the cases where the id is already issued.
231 if (nfsrv_notsamecredname(nd, clp)) {
233 * Check to see if there is expired state that should go away.
235 if (clp->lc_expiry < NFSD_MONOSEC &&
236 (!LIST_EMPTY(&clp->lc_open) || !LIST_EMPTY(&clp->lc_deleg))) {
237 nfsrv_cleanclient(clp, p);
238 nfsrv_freedeleglist(&clp->lc_deleg);
242 * If there is outstanding state, then reply NFSERR_CLIDINUSE per
243 * RFC3530 Sec. 8.1.2 last para.
245 if (!LIST_EMPTY(&clp->lc_deleg)) {
247 } else if (LIST_EMPTY(&clp->lc_open)) {
251 /* Look for an Open on the OpenOwner */
252 LIST_FOREACH(stp, &clp->lc_open, ls_list) {
253 if (!LIST_EMPTY(&stp->ls_open)) {
261 * If the uid doesn't match, return NFSERR_CLIDINUSE after
262 * filling out the correct ipaddr and portnum.
264 sad = NFSSOCKADDR(new_clp->lc_req.nr_nam, struct sockaddr_in *);
265 rad = NFSSOCKADDR(clp->lc_req.nr_nam, struct sockaddr_in *);
266 sad->sin_addr.s_addr = rad->sin_addr.s_addr;
267 sad->sin_port = rad->sin_port;
268 NFSLOCKV4ROOTMUTEX();
269 nfsv4_unlock(&nfsv4rootfs_lock, 1);
270 NFSUNLOCKV4ROOTMUTEX();
271 return (NFSERR_CLIDINUSE);
275 if (NFSBCMP(new_clp->lc_verf, clp->lc_verf, NFSX_VERF)) {
277 * If the verifier has changed, the client has rebooted
278 * and a new client id is issued. The old state info
279 * can be thrown away once the SETCLIENTID_CONFIRM occurs.
281 LIST_REMOVE(clp, lc_hash);
282 new_clp->lc_flags |= LCL_NEEDSCONFIRM;
283 confirmp->qval = new_clp->lc_confirm.qval = ++confirm_index;
284 clientidp->lval[0] = new_clp->lc_clientid.lval[0] =
286 clientidp->lval[1] = new_clp->lc_clientid.lval[1] =
287 nfsrv_nextclientindex();
288 new_clp->lc_stateindex = 0;
289 new_clp->lc_statemaxindex = 0;
290 new_clp->lc_cbref = 0;
291 new_clp->lc_expiry = nfsrv_leaseexpiry();
294 * Save the state until confirmed.
296 LIST_NEWHEAD(&new_clp->lc_open, &clp->lc_open, ls_list);
297 LIST_FOREACH(tstp, &new_clp->lc_open, ls_list)
298 tstp->ls_clp = new_clp;
299 LIST_NEWHEAD(&new_clp->lc_deleg, &clp->lc_deleg, ls_list);
300 LIST_FOREACH(tstp, &new_clp->lc_deleg, ls_list)
301 tstp->ls_clp = new_clp;
302 LIST_NEWHEAD(&new_clp->lc_olddeleg, &clp->lc_olddeleg,
304 LIST_FOREACH(tstp, &new_clp->lc_olddeleg, ls_list)
305 tstp->ls_clp = new_clp;
306 for (i = 0; i < NFSSTATEHASHSIZE; i++) {
307 LIST_NEWHEAD(&new_clp->lc_stateid[i],
308 &clp->lc_stateid[i], ls_hash);
309 LIST_FOREACH(tstp, &new_clp->lc_stateid[i], ls_list)
310 tstp->ls_clp = new_clp;
312 LIST_INSERT_HEAD(NFSCLIENTHASH(new_clp->lc_clientid), new_clp,
314 newnfsstats.srvclients++;
315 nfsrv_openpluslock++;
317 NFSLOCKV4ROOTMUTEX();
318 nfsv4_unlock(&nfsv4rootfs_lock, 1);
319 NFSUNLOCKV4ROOTMUTEX();
322 * Must wait until any outstanding callback on the old clp
325 while (clp->lc_cbref) {
326 clp->lc_flags |= LCL_WAKEUPWANTED;
327 (void) tsleep((caddr_t)clp, PZERO - 1,
328 "nfsd clp", 10 * hz);
330 nfsrv_zapclient(clp, p);
335 * id and verifier match, so update the net address info
336 * and get rid of any existing callback authentication
337 * handle, so a new one will be acquired.
339 LIST_REMOVE(clp, lc_hash);
340 new_clp->lc_flags |= (LCL_NEEDSCONFIRM | LCL_DONTCLEAN);
341 new_clp->lc_expiry = nfsrv_leaseexpiry();
342 confirmp->qval = new_clp->lc_confirm.qval = ++confirm_index;
343 clientidp->lval[0] = new_clp->lc_clientid.lval[0] =
344 clp->lc_clientid.lval[0];
345 clientidp->lval[1] = new_clp->lc_clientid.lval[1] =
346 clp->lc_clientid.lval[1];
347 new_clp->lc_delegtime = clp->lc_delegtime;
348 new_clp->lc_stateindex = clp->lc_stateindex;
349 new_clp->lc_statemaxindex = clp->lc_statemaxindex;
350 new_clp->lc_cbref = 0;
351 LIST_NEWHEAD(&new_clp->lc_open, &clp->lc_open, ls_list);
352 LIST_FOREACH(tstp, &new_clp->lc_open, ls_list)
353 tstp->ls_clp = new_clp;
354 LIST_NEWHEAD(&new_clp->lc_deleg, &clp->lc_deleg, ls_list);
355 LIST_FOREACH(tstp, &new_clp->lc_deleg, ls_list)
356 tstp->ls_clp = new_clp;
357 LIST_NEWHEAD(&new_clp->lc_olddeleg, &clp->lc_olddeleg, ls_list);
358 LIST_FOREACH(tstp, &new_clp->lc_olddeleg, ls_list)
359 tstp->ls_clp = new_clp;
360 for (i = 0; i < NFSSTATEHASHSIZE; i++) {
361 LIST_NEWHEAD(&new_clp->lc_stateid[i], &clp->lc_stateid[i],
363 LIST_FOREACH(tstp, &new_clp->lc_stateid[i], ls_list)
364 tstp->ls_clp = new_clp;
366 LIST_INSERT_HEAD(NFSCLIENTHASH(new_clp->lc_clientid), new_clp,
368 newnfsstats.srvclients++;
369 nfsrv_openpluslock++;
371 NFSLOCKV4ROOTMUTEX();
372 nfsv4_unlock(&nfsv4rootfs_lock, 1);
373 NFSUNLOCKV4ROOTMUTEX();
376 * Must wait until any outstanding callback on the old clp
379 while (clp->lc_cbref) {
380 clp->lc_flags |= LCL_WAKEUPWANTED;
381 (void) tsleep((caddr_t)clp, PZERO - 1, "nfsd clp", 10 * hz);
383 nfsrv_zapclient(clp, p);
389 * Check to see if the client id exists and optionally confirm it.
392 nfsrv_getclient(nfsquad_t clientid, int opflags, struct nfsclient **clpp,
393 nfsquad_t confirm, struct nfsrv_descript *nd, NFSPROC_T *p)
395 struct nfsclient *clp;
396 struct nfsstate *stp;
398 struct nfsclienthashhead *hp;
399 int error = 0, igotlock, doneok;
403 if (nfsrvboottime != clientid.lval[0])
404 return (NFSERR_STALECLIENTID);
407 * If called with opflags == CLOPS_RENEW, the State Lock is
408 * already held. Otherwise, we need to get either that or,
409 * for the case of Confirm, lock out the nfsd threads.
411 if (opflags & CLOPS_CONFIRM) {
412 NFSLOCKV4ROOTMUTEX();
413 nfsv4_relref(&nfsv4rootfs_lock);
415 igotlock = nfsv4_lock(&nfsv4rootfs_lock, 1, NULL,
416 NFSV4ROOTLOCKMUTEXPTR);
418 NFSUNLOCKV4ROOTMUTEX();
419 NFSLOCKSTATE(); /* to avoid a race with */
420 NFSUNLOCKSTATE(); /* nfsrv_servertimer() */
421 } else if (opflags != CLOPS_RENEW) {
425 hp = NFSCLIENTHASH(clientid);
426 LIST_FOREACH(clp, hp, lc_hash) {
427 if (clp->lc_clientid.lval[1] == clientid.lval[1])
430 if (clp == LIST_END(hp)) {
431 if (opflags & CLOPS_CONFIRM)
432 error = NFSERR_STALECLIENTID;
434 error = NFSERR_EXPIRED;
435 } else if (clp->lc_flags & LCL_ADMINREVOKED) {
437 * If marked admin revoked, just return the error.
439 error = NFSERR_ADMINREVOKED;
442 if (opflags & CLOPS_CONFIRM) {
443 NFSLOCKV4ROOTMUTEX();
444 nfsv4_unlock(&nfsv4rootfs_lock, 1);
445 NFSUNLOCKV4ROOTMUTEX();
446 } else if (opflags != CLOPS_RENEW) {
453 * Perform any operations specified by the opflags.
455 if (opflags & CLOPS_CONFIRM) {
456 if (clp->lc_confirm.qval != confirm.qval)
457 error = NFSERR_STALECLIENTID;
458 else if (nfsrv_notsamecredname(nd, clp))
459 error = NFSERR_CLIDINUSE;
462 if ((clp->lc_flags & (LCL_NEEDSCONFIRM | LCL_DONTCLEAN)) ==
465 * Hang onto the delegations (as old delegations)
466 * for an Open with CLAIM_DELEGATE_PREV unless in
467 * grace, but get rid of the rest of the state.
469 nfsrv_cleanclient(clp, p);
470 nfsrv_freedeleglist(&clp->lc_olddeleg);
471 if (nfsrv_checkgrace(0)) {
472 /* In grace, so just delete delegations */
473 nfsrv_freedeleglist(&clp->lc_deleg);
475 LIST_FOREACH(stp, &clp->lc_deleg, ls_list)
476 stp->ls_flags |= NFSLCK_OLDDELEG;
477 clp->lc_delegtime = NFSD_MONOSEC +
478 nfsrv_lease + NFSRV_LEASEDELTA;
479 LIST_NEWHEAD(&clp->lc_olddeleg, &clp->lc_deleg,
483 clp->lc_flags &= ~(LCL_NEEDSCONFIRM | LCL_DONTCLEAN);
485 clp->lc_flags |= LCL_NEEDSCBNULL;
487 } else if (clp->lc_flags & LCL_NEEDSCONFIRM) {
488 error = NFSERR_EXPIRED;
492 * If called by the Renew Op, we must check the principal.
494 if (!error && (opflags & CLOPS_RENEWOP)) {
495 if (nfsrv_notsamecredname(nd, clp)) {
497 for (i = 0; i < NFSSTATEHASHSIZE && doneok == 0; i++) {
498 LIST_FOREACH(stp, &clp->lc_stateid[i], ls_hash) {
499 if ((stp->ls_flags & NFSLCK_OPEN) &&
500 stp->ls_uid == nd->nd_cred->cr_uid) {
507 error = NFSERR_ACCES;
509 if (!error && (clp->lc_flags & LCL_CBDOWN))
510 error = NFSERR_CBPATHDOWN;
512 if ((!error || error == NFSERR_CBPATHDOWN) &&
513 (opflags & CLOPS_RENEW)) {
514 clp->lc_expiry = nfsrv_leaseexpiry();
516 if (opflags & CLOPS_CONFIRM) {
517 NFSLOCKV4ROOTMUTEX();
518 nfsv4_unlock(&nfsv4rootfs_lock, 1);
519 NFSUNLOCKV4ROOTMUTEX();
520 } else if (opflags != CLOPS_RENEW) {
529 * Called from the new nfssvc syscall to admin revoke a clientid.
530 * Returns 0 for success, error otherwise.
533 nfsrv_adminrevoke(struct nfsd_clid *revokep, NFSPROC_T *p)
535 struct nfsclient *clp = NULL;
540 * First, lock out the nfsd so that state won't change while the
541 * revocation record is being written to the stable storage restart
544 NFSLOCKV4ROOTMUTEX();
545 nfsv4_relref(&nfsv4rootfs_lock);
547 igotlock = nfsv4_lock(&nfsv4rootfs_lock, 1, NULL,
548 NFSV4ROOTLOCKMUTEXPTR);
550 NFSUNLOCKV4ROOTMUTEX();
551 NFSLOCKSTATE(); /* to avoid a race with */
552 NFSUNLOCKSTATE(); /* nfsrv_servertimer() */
555 * Search for a match in the client list.
558 while (i < NFSCLIENTHASHSIZE && !gotit) {
559 LIST_FOREACH(clp, &nfsclienthash[i], lc_hash) {
560 if (revokep->nclid_idlen == clp->lc_idlen &&
561 !NFSBCMP(revokep->nclid_id, clp->lc_id, clp->lc_idlen)) {
569 NFSLOCKV4ROOTMUTEX();
570 nfsv4_unlock(&nfsv4rootfs_lock, 0);
571 NFSUNLOCKV4ROOTMUTEX();
576 * Now, write out the revocation record
578 nfsrv_writestable(clp->lc_id, clp->lc_idlen, NFSNST_REVOKE, p);
581 * and clear out the state, marking the clientid revoked.
583 clp->lc_flags &= ~LCL_CALLBACKSON;
584 clp->lc_flags |= LCL_ADMINREVOKED;
585 nfsrv_cleanclient(clp, p);
586 nfsrv_freedeleglist(&clp->lc_deleg);
587 nfsrv_freedeleglist(&clp->lc_olddeleg);
588 NFSLOCKV4ROOTMUTEX();
589 nfsv4_unlock(&nfsv4rootfs_lock, 0);
590 NFSUNLOCKV4ROOTMUTEX();
595 * Dump out stats for all clients. Called from nfssvc(2), that is used
599 nfsrv_dumpclients(struct nfsd_dumpclients *dumpp, int maxcnt)
601 struct nfsclient *clp;
606 * Rattle through the client lists until done.
608 while (i < NFSCLIENTHASHSIZE && cnt < maxcnt) {
609 clp = LIST_FIRST(&nfsclienthash[i]);
610 while (clp != LIST_END(&nfsclienthash[i]) && cnt < maxcnt) {
611 nfsrv_dumpaclient(clp, &dumpp[cnt]);
613 clp = LIST_NEXT(clp, lc_hash);
618 dumpp[cnt].ndcl_clid.nclid_idlen = 0;
623 * Dump stats for a client. Must be called with the NFSSTATELOCK and spl'd.
626 nfsrv_dumpaclient(struct nfsclient *clp, struct nfsd_dumpclients *dumpp)
628 struct nfsstate *stp, *openstp, *lckownstp;
630 struct sockaddr *sad;
631 struct sockaddr_in *rad;
632 struct sockaddr_in6 *rad6;
634 dumpp->ndcl_nopenowners = dumpp->ndcl_nlockowners = 0;
635 dumpp->ndcl_nopens = dumpp->ndcl_nlocks = 0;
636 dumpp->ndcl_ndelegs = dumpp->ndcl_nolddelegs = 0;
637 dumpp->ndcl_flags = clp->lc_flags;
638 dumpp->ndcl_clid.nclid_idlen = clp->lc_idlen;
639 NFSBCOPY(clp->lc_id, dumpp->ndcl_clid.nclid_id, clp->lc_idlen);
640 sad = NFSSOCKADDR(clp->lc_req.nr_nam, struct sockaddr *);
641 dumpp->ndcl_addrfam = sad->sa_family;
642 if (sad->sa_family == AF_INET) {
643 rad = (struct sockaddr_in *)sad;
644 dumpp->ndcl_cbaddr.sin_addr = rad->sin_addr;
646 rad6 = (struct sockaddr_in6 *)sad;
647 dumpp->ndcl_cbaddr.sin6_addr = rad6->sin6_addr;
651 * Now, scan the state lists and total up the opens and locks.
653 LIST_FOREACH(stp, &clp->lc_open, ls_list) {
654 dumpp->ndcl_nopenowners++;
655 LIST_FOREACH(openstp, &stp->ls_open, ls_list) {
656 dumpp->ndcl_nopens++;
657 LIST_FOREACH(lckownstp, &openstp->ls_open, ls_list) {
658 dumpp->ndcl_nlockowners++;
659 LIST_FOREACH(lop, &lckownstp->ls_lock, lo_lckowner) {
660 dumpp->ndcl_nlocks++;
667 * and the delegation lists.
669 LIST_FOREACH(stp, &clp->lc_deleg, ls_list) {
670 dumpp->ndcl_ndelegs++;
672 LIST_FOREACH(stp, &clp->lc_olddeleg, ls_list) {
673 dumpp->ndcl_nolddelegs++;
678 * Dump out lock stats for a file.
681 nfsrv_dumplocks(vnode_t vp, struct nfsd_dumplocks *ldumpp, int maxcnt,
684 struct nfsstate *stp;
687 struct nfslockfile *lfp;
688 struct sockaddr *sad;
689 struct sockaddr_in *rad;
690 struct sockaddr_in6 *rad6;
694 ret = nfsrv_getlockfh(vp, 0, NULL, &nfh, p);
697 ret = nfsrv_getlockfile(0, NULL, &lfp, &nfh, 0);
699 ldumpp[0].ndlck_clid.nclid_idlen = 0;
705 * For each open share on file, dump it out.
707 stp = LIST_FIRST(&lfp->lf_open);
708 while (stp != LIST_END(&lfp->lf_open) && cnt < maxcnt) {
709 ldumpp[cnt].ndlck_flags = stp->ls_flags;
710 ldumpp[cnt].ndlck_stateid.seqid = stp->ls_stateid.seqid;
711 ldumpp[cnt].ndlck_stateid.other[0] = stp->ls_stateid.other[0];
712 ldumpp[cnt].ndlck_stateid.other[1] = stp->ls_stateid.other[1];
713 ldumpp[cnt].ndlck_stateid.other[2] = stp->ls_stateid.other[2];
714 ldumpp[cnt].ndlck_owner.nclid_idlen =
715 stp->ls_openowner->ls_ownerlen;
716 NFSBCOPY(stp->ls_openowner->ls_owner,
717 ldumpp[cnt].ndlck_owner.nclid_id,
718 stp->ls_openowner->ls_ownerlen);
719 ldumpp[cnt].ndlck_clid.nclid_idlen = stp->ls_clp->lc_idlen;
720 NFSBCOPY(stp->ls_clp->lc_id, ldumpp[cnt].ndlck_clid.nclid_id,
721 stp->ls_clp->lc_idlen);
722 sad=NFSSOCKADDR(stp->ls_clp->lc_req.nr_nam, struct sockaddr *);
723 ldumpp[cnt].ndlck_addrfam = sad->sa_family;
724 if (sad->sa_family == AF_INET) {
725 rad = (struct sockaddr_in *)sad;
726 ldumpp[cnt].ndlck_cbaddr.sin_addr = rad->sin_addr;
728 rad6 = (struct sockaddr_in6 *)sad;
729 ldumpp[cnt].ndlck_cbaddr.sin6_addr = rad6->sin6_addr;
731 stp = LIST_NEXT(stp, ls_file);
738 lop = LIST_FIRST(&lfp->lf_lock);
739 while (lop != LIST_END(&lfp->lf_lock) && cnt < maxcnt) {
741 ldumpp[cnt].ndlck_flags = lop->lo_flags;
742 ldumpp[cnt].ndlck_first = lop->lo_first;
743 ldumpp[cnt].ndlck_end = lop->lo_end;
744 ldumpp[cnt].ndlck_stateid.seqid = stp->ls_stateid.seqid;
745 ldumpp[cnt].ndlck_stateid.other[0] = stp->ls_stateid.other[0];
746 ldumpp[cnt].ndlck_stateid.other[1] = stp->ls_stateid.other[1];
747 ldumpp[cnt].ndlck_stateid.other[2] = stp->ls_stateid.other[2];
748 ldumpp[cnt].ndlck_owner.nclid_idlen = stp->ls_ownerlen;
749 NFSBCOPY(stp->ls_owner, ldumpp[cnt].ndlck_owner.nclid_id,
751 ldumpp[cnt].ndlck_clid.nclid_idlen = stp->ls_clp->lc_idlen;
752 NFSBCOPY(stp->ls_clp->lc_id, ldumpp[cnt].ndlck_clid.nclid_id,
753 stp->ls_clp->lc_idlen);
754 sad=NFSSOCKADDR(stp->ls_clp->lc_req.nr_nam, struct sockaddr *);
755 ldumpp[cnt].ndlck_addrfam = sad->sa_family;
756 if (sad->sa_family == AF_INET) {
757 rad = (struct sockaddr_in *)sad;
758 ldumpp[cnt].ndlck_cbaddr.sin_addr = rad->sin_addr;
760 rad6 = (struct sockaddr_in6 *)sad;
761 ldumpp[cnt].ndlck_cbaddr.sin6_addr = rad6->sin6_addr;
763 lop = LIST_NEXT(lop, lo_lckfile);
768 * and the delegations.
770 stp = LIST_FIRST(&lfp->lf_deleg);
771 while (stp != LIST_END(&lfp->lf_deleg) && cnt < maxcnt) {
772 ldumpp[cnt].ndlck_flags = stp->ls_flags;
773 ldumpp[cnt].ndlck_stateid.seqid = stp->ls_stateid.seqid;
774 ldumpp[cnt].ndlck_stateid.other[0] = stp->ls_stateid.other[0];
775 ldumpp[cnt].ndlck_stateid.other[1] = stp->ls_stateid.other[1];
776 ldumpp[cnt].ndlck_stateid.other[2] = stp->ls_stateid.other[2];
777 ldumpp[cnt].ndlck_owner.nclid_idlen = 0;
778 ldumpp[cnt].ndlck_clid.nclid_idlen = stp->ls_clp->lc_idlen;
779 NFSBCOPY(stp->ls_clp->lc_id, ldumpp[cnt].ndlck_clid.nclid_id,
780 stp->ls_clp->lc_idlen);
781 sad=NFSSOCKADDR(stp->ls_clp->lc_req.nr_nam, struct sockaddr *);
782 ldumpp[cnt].ndlck_addrfam = sad->sa_family;
783 if (sad->sa_family == AF_INET) {
784 rad = (struct sockaddr_in *)sad;
785 ldumpp[cnt].ndlck_cbaddr.sin_addr = rad->sin_addr;
787 rad6 = (struct sockaddr_in6 *)sad;
788 ldumpp[cnt].ndlck_cbaddr.sin6_addr = rad6->sin6_addr;
790 stp = LIST_NEXT(stp, ls_file);
795 * If list isn't full, mark end of list by setting the client name
799 ldumpp[cnt].ndlck_clid.nclid_idlen = 0;
804 * Server timer routine. It can scan any linked list, so long
805 * as it holds the spin lock and there is no exclusive lock on
807 * Must be called by a kernel thread and not a timer interrupt,
808 * so that it only runs when the nfsd threads are sleeping on a
809 * uniprocessor and uses the State spin lock for an SMP system.
810 * (For OpenBSD, a kthread is ok. For FreeBSD, I think it is ok
811 * to do this from a callout, since the spin locks work. For
812 * Darwin, I'm not sure what will work correctly yet.)
813 * Should be called once per second.
816 nfsrv_servertimer(void)
818 struct nfsclient *clp, *nclp;
819 struct nfsstate *stp, *nstp;
823 * Make sure nfsboottime is set. This is used by V3 as well
824 * as V4. Note that nfsboottime is not nfsrvboottime, which is
825 * only used by the V4 server for leases.
827 if (nfsboottime.tv_sec == 0)
828 NFSSETBOOTTIME(nfsboottime);
831 * If server hasn't started yet, just return.
834 if (nfsrv_stablefirst.nsf_eograce == 0) {
838 if (!(nfsrv_stablefirst.nsf_flags & NFSNSF_UPDATEDONE)) {
839 if (!(nfsrv_stablefirst.nsf_flags & NFSNSF_GRACEOVER) &&
840 NFSD_MONOSEC > nfsrv_stablefirst.nsf_eograce)
841 nfsrv_stablefirst.nsf_flags |=
842 (NFSNSF_GRACEOVER | NFSNSF_NEEDLOCK);
848 * Return now if an nfsd thread has the exclusive lock on
849 * nfsv4rootfs_lock. The dirty trick here is that we have
850 * the spin lock already and the nfsd threads do a:
851 * NFSLOCKSTATE, NFSUNLOCKSTATE after getting the exclusive
852 * lock, so they won't race with code after this check.
854 if (nfsv4rootfs_lock.nfslock_lock & NFSV4LOCK_LOCK) {
862 for (i = 0; i < NFSCLIENTHASHSIZE; i++) {
863 clp = LIST_FIRST(&nfsclienthash[i]);
864 while (clp != LIST_END(&nfsclienthash[i])) {
865 nclp = LIST_NEXT(clp, lc_hash);
866 if (!(clp->lc_flags & LCL_EXPIREIT)) {
867 if (((clp->lc_expiry + NFSRV_STALELEASE) < NFSD_MONOSEC
868 && ((LIST_EMPTY(&clp->lc_deleg)
869 && LIST_EMPTY(&clp->lc_open)) ||
870 nfsrv_clients > nfsrv_clienthighwater)) ||
871 (clp->lc_expiry + NFSRV_MOULDYLEASE) < NFSD_MONOSEC ||
872 (clp->lc_expiry < NFSD_MONOSEC &&
873 (nfsrv_openpluslock * 10 / 9) > NFSRV_V4STATELIMIT)) {
875 * Lease has expired several nfsrv_lease times ago:
877 * - no state is associated with it
879 * - above high water mark for number of clients
880 * (nfsrv_clienthighwater should be large enough
881 * that this only occurs when clients fail to
882 * use the same nfs_client_id4.id. Maybe somewhat
883 * higher that the maximum number of clients that
884 * will mount this server?)
886 * Lease has expired a very long time ago
888 * Lease has expired PLUS the number of opens + locks
889 * has exceeded 90% of capacity
891 * --> Mark for expiry. The actual expiry will be done
892 * by an nfsd sometime soon.
894 clp->lc_flags |= LCL_EXPIREIT;
895 nfsrv_stablefirst.nsf_flags |=
896 (NFSNSF_NEEDLOCK | NFSNSF_EXPIREDCLIENT);
899 * If there are no opens, increment no open tick cnt
900 * If time exceeds NFSNOOPEN, mark it to be thrown away
901 * otherwise, if there is an open, reset no open time
902 * Hopefully, this will avoid excessive re-creation
903 * of open owners and subsequent open confirms.
905 stp = LIST_FIRST(&clp->lc_open);
906 while (stp != LIST_END(&clp->lc_open)) {
907 nstp = LIST_NEXT(stp, ls_list);
908 if (LIST_EMPTY(&stp->ls_open)) {
910 if (stp->ls_noopens > NFSNOOPEN ||
911 (nfsrv_openpluslock * 2) >
913 nfsrv_stablefirst.nsf_flags |=
929 * The following set of functions free up the various data structures.
932 * Clear out all open/lock state related to this nfsclient.
933 * Caller must hold an exclusive lock on nfsv4rootfs_lock, so that
934 * there are no other active nfsd threads.
937 nfsrv_cleanclient(struct nfsclient *clp, NFSPROC_T *p)
939 struct nfsstate *stp, *nstp;
941 LIST_FOREACH_SAFE(stp, &clp->lc_open, ls_list, nstp)
942 nfsrv_freeopenowner(stp, 1, p);
946 * Free a client that has been cleaned. It should also already have been
947 * removed from the lists.
948 * (Just to be safe w.r.t. newnfs_disconnect(), call this function when
949 * softclock interrupts are enabled.)
952 nfsrv_zapclient(struct nfsclient *clp, NFSPROC_T *p)
956 if ((clp->lc_flags & (LCL_GSS | LCL_CALLBACKSON)) ==
957 (LCL_GSS | LCL_CALLBACKSON) &&
958 (clp->lc_hand.nfsh_flag & NFSG_COMPLETE) &&
959 clp->lc_handlelen > 0) {
960 clp->lc_hand.nfsh_flag &= ~NFSG_COMPLETE;
961 clp->lc_hand.nfsh_flag |= NFSG_DESTROYED;
962 (void) nfsrv_docallback(clp, NFSV4PROC_CBNULL,
963 NULL, 0, NULL, NULL, NULL, p);
966 newnfs_disconnect(&clp->lc_req);
967 NFSSOCKADDRFREE(clp->lc_req.nr_nam);
968 NFSFREEMUTEX(&clp->lc_req.nr_mtx);
969 free((caddr_t)clp, M_NFSDCLIENT);
971 newnfsstats.srvclients--;
972 nfsrv_openpluslock--;
978 * Free a list of delegation state structures.
979 * (This function will also free all nfslockfile structures that no
980 * longer have associated state.)
983 nfsrv_freedeleglist(struct nfsstatehead *sthp)
985 struct nfsstate *stp, *nstp;
987 LIST_FOREACH_SAFE(stp, sthp, ls_list, nstp) {
988 nfsrv_freedeleg(stp);
994 * Free up a delegation.
997 nfsrv_freedeleg(struct nfsstate *stp)
999 struct nfslockfile *lfp;
1001 LIST_REMOVE(stp, ls_hash);
1002 LIST_REMOVE(stp, ls_list);
1003 LIST_REMOVE(stp, ls_file);
1005 if (LIST_EMPTY(&lfp->lf_open) &&
1006 LIST_EMPTY(&lfp->lf_lock) && LIST_EMPTY(&lfp->lf_deleg) &&
1007 LIST_EMPTY(&lfp->lf_locallock) && LIST_EMPTY(&lfp->lf_rollback) &&
1008 lfp->lf_usecount == 0 &&
1009 nfsv4_testlock(&lfp->lf_locallock_lck) == 0)
1010 nfsrv_freenfslockfile(lfp);
1011 FREE((caddr_t)stp, M_NFSDSTATE);
1012 newnfsstats.srvdelegates--;
1013 nfsrv_openpluslock--;
1014 nfsrv_delegatecnt--;
1018 * This function frees an open owner and all associated opens.
1019 * Must be called with soft clock interrupts disabled.
1022 nfsrv_freeopenowner(struct nfsstate *stp, int cansleep, NFSPROC_T *p)
1024 struct nfsstate *nstp, *tstp;
1026 LIST_REMOVE(stp, ls_list);
1028 * Now, free all associated opens.
1030 nstp = LIST_FIRST(&stp->ls_open);
1031 while (nstp != LIST_END(&stp->ls_open)) {
1033 nstp = LIST_NEXT(nstp, ls_list);
1034 (void) nfsrv_freeopen(tstp, NULL, cansleep, p);
1037 nfsrvd_derefcache(stp->ls_op);
1038 FREE((caddr_t)stp, M_NFSDSTATE);
1039 newnfsstats.srvopenowners--;
1040 nfsrv_openpluslock--;
1044 * This function frees an open (nfsstate open structure) with all associated
1045 * lock_owners and locks. It also frees the nfslockfile structure iff there
1046 * are no other opens on the file.
1047 * Returns 1 if it free'd the nfslockfile, 0 otherwise.
1050 nfsrv_freeopen(struct nfsstate *stp, vnode_t vp, int cansleep, NFSPROC_T *p)
1052 struct nfsstate *nstp, *tstp;
1053 struct nfslockfile *lfp;
1056 LIST_REMOVE(stp, ls_hash);
1057 LIST_REMOVE(stp, ls_list);
1058 LIST_REMOVE(stp, ls_file);
1062 * Now, free all lockowners associated with this open.
1064 LIST_FOREACH_SAFE(tstp, &stp->ls_open, ls_list, nstp)
1065 nfsrv_freelockowner(tstp, vp, cansleep, p);
1068 * The nfslockfile is freed here if there are no locks
1069 * associated with the open.
1070 * If there are locks associated with the open, the
1071 * nfslockfile structure can be freed via nfsrv_freelockowner().
1072 * (That is why the call must be here instead of after the loop.)
1074 if (lfp != NULL && LIST_EMPTY(&lfp->lf_open) &&
1075 LIST_EMPTY(&lfp->lf_deleg) && LIST_EMPTY(&lfp->lf_lock) &&
1076 LIST_EMPTY(&lfp->lf_locallock) && LIST_EMPTY(&lfp->lf_rollback) &&
1077 lfp->lf_usecount == 0 &&
1078 (cansleep != 0 || nfsv4_testlock(&lfp->lf_locallock_lck) == 0)) {
1079 nfsrv_freenfslockfile(lfp);
1083 FREE((caddr_t)stp, M_NFSDSTATE);
1084 newnfsstats.srvopens--;
1085 nfsrv_openpluslock--;
1090 * Frees a lockowner and all associated locks.
1093 nfsrv_freelockowner(struct nfsstate *stp, vnode_t vp, int cansleep,
1097 LIST_REMOVE(stp, ls_hash);
1098 LIST_REMOVE(stp, ls_list);
1099 nfsrv_freeallnfslocks(stp, vp, cansleep, p);
1101 nfsrvd_derefcache(stp->ls_op);
1102 FREE((caddr_t)stp, M_NFSDSTATE);
1103 newnfsstats.srvlockowners--;
1104 nfsrv_openpluslock--;
1108 * Free all the nfs locks on a lockowner.
1111 nfsrv_freeallnfslocks(struct nfsstate *stp, vnode_t vp, int cansleep,
1114 struct nfslock *lop, *nlop;
1115 struct nfsrollback *rlp, *nrlp;
1116 struct nfslockfile *lfp = NULL;
1120 lop = LIST_FIRST(&stp->ls_lock);
1121 while (lop != LIST_END(&stp->ls_lock)) {
1122 nlop = LIST_NEXT(lop, lo_lckowner);
1124 * Since all locks should be for the same file, lfp should
1129 else if (lfp != lop->lo_lfp)
1130 panic("allnfslocks");
1132 * If vp is NULL and cansleep != 0, a vnode must be acquired
1133 * from the file handle. This only occurs when called from
1134 * nfsrv_cleanclient().
1137 if (nfsrv_dolocallocks == 0)
1139 else if (vp == NULL && cansleep != 0)
1140 tvp = nfsvno_getvp(&lfp->lf_fh);
1149 nfsrv_localunlock(tvp, lfp, lop->lo_first,
1151 LIST_FOREACH_SAFE(rlp, &lfp->lf_rollback, rlck_list,
1153 free(rlp, M_NFSDROLLBACK);
1154 LIST_INIT(&lfp->lf_rollback);
1156 nfsrv_freenfslock(lop);
1159 if (vp == NULL && tvp != NULL)
1164 * Free an nfslock structure.
1165 * Must be called with soft clock interrupts disabled.
1168 nfsrv_freenfslock(struct nfslock *lop)
1171 if (lop->lo_lckfile.le_prev != NULL) {
1172 LIST_REMOVE(lop, lo_lckfile);
1173 newnfsstats.srvlocks--;
1174 nfsrv_openpluslock--;
1176 LIST_REMOVE(lop, lo_lckowner);
1177 FREE((caddr_t)lop, M_NFSDLOCK);
1181 * This function frees an nfslockfile structure.
1182 * Must be called with soft clock interrupts disabled.
1185 nfsrv_freenfslockfile(struct nfslockfile *lfp)
1188 LIST_REMOVE(lfp, lf_hash);
1189 FREE((caddr_t)lfp, M_NFSDLOCKFILE);
1193 * This function looks up an nfsstate structure via stateid.
1196 nfsrv_getstate(struct nfsclient *clp, nfsv4stateid_t *stateidp, __unused u_int32_t flags,
1197 struct nfsstate **stpp)
1199 struct nfsstate *stp;
1200 struct nfsstatehead *hp;
1203 hp = NFSSTATEHASH(clp, *stateidp);
1204 LIST_FOREACH(stp, hp, ls_hash) {
1205 if (!NFSBCMP(stp->ls_stateid.other, stateidp->other,
1211 * If no state id in list, return NFSERR_BADSTATEID.
1213 if (stp == LIST_END(hp))
1214 return (NFSERR_BADSTATEID);
1220 * This function gets an nfsstate structure via owner string.
1223 nfsrv_getowner(struct nfsstatehead *hp, struct nfsstate *new_stp,
1224 struct nfsstate **stpp)
1226 struct nfsstate *stp;
1229 LIST_FOREACH(stp, hp, ls_list) {
1230 if (new_stp->ls_ownerlen == stp->ls_ownerlen &&
1231 !NFSBCMP(new_stp->ls_owner,stp->ls_owner,stp->ls_ownerlen)) {
1239 * Lock control function called to update lock status.
1240 * Returns 0 upon success, -1 if there is no lock and the flags indicate
1241 * that one isn't to be created and an NFSERR_xxx for other errors.
1242 * The structures new_stp and new_lop are passed in as pointers that should
1243 * be set to NULL if the structure is used and shouldn't be free'd.
1244 * For the NFSLCK_TEST and NFSLCK_CHECK cases, the structures are
1245 * never used and can safely be allocated on the stack. For all other
1246 * cases, *new_stpp and *new_lopp should be malloc'd before the call,
1247 * in case they are used.
1250 nfsrv_lockctrl(vnode_t vp, struct nfsstate **new_stpp,
1251 struct nfslock **new_lopp, struct nfslockconflict *cfp,
1252 nfsquad_t clientid, nfsv4stateid_t *stateidp,
1253 __unused struct nfsexstuff *exp,
1254 struct nfsrv_descript *nd, NFSPROC_T *p)
1256 struct nfslock *lop;
1257 struct nfsstate *new_stp = *new_stpp;
1258 struct nfslock *new_lop = *new_lopp;
1259 struct nfsstate *tstp, *mystp, *nstp;
1261 struct nfslockfile *lfp;
1262 struct nfslock *other_lop = NULL;
1263 struct nfsstate *stp, *lckstp = NULL;
1264 struct nfsclient *clp = NULL;
1266 int error = 0, haslock = 0, ret, reterr;
1267 int getlckret, delegation = 0, filestruct_locked;
1269 uint64_t first, end;
1270 uint32_t lock_flags;
1272 if (new_stp->ls_flags & (NFSLCK_CHECK | NFSLCK_SETATTR)) {
1274 * Note the special cases of "all 1s" or "all 0s" stateids and
1275 * let reads with all 1s go ahead.
1277 if (new_stp->ls_stateid.seqid == 0x0 &&
1278 new_stp->ls_stateid.other[0] == 0x0 &&
1279 new_stp->ls_stateid.other[1] == 0x0 &&
1280 new_stp->ls_stateid.other[2] == 0x0)
1282 else if (new_stp->ls_stateid.seqid == 0xffffffff &&
1283 new_stp->ls_stateid.other[0] == 0xffffffff &&
1284 new_stp->ls_stateid.other[1] == 0xffffffff &&
1285 new_stp->ls_stateid.other[2] == 0xffffffff)
1290 * Check for restart conditions (client and server).
1292 error = nfsrv_checkrestart(clientid, new_stp->ls_flags,
1293 &new_stp->ls_stateid, specialid);
1298 * Check for state resource limit exceeded.
1300 if ((new_stp->ls_flags & NFSLCK_LOCK) &&
1301 nfsrv_openpluslock > NFSRV_V4STATELIMIT)
1302 return (NFSERR_RESOURCE);
1305 * For the lock case, get another nfslock structure,
1306 * just in case we need it.
1307 * Malloc now, before we start sifting through the linked lists,
1308 * in case we have to wait for memory.
1311 if (new_stp->ls_flags & NFSLCK_LOCK)
1312 MALLOC(other_lop, struct nfslock *, sizeof (struct nfslock),
1313 M_NFSDLOCK, M_WAITOK);
1314 filestruct_locked = 0;
1319 * Get the lockfile structure for CFH now, so we can do a sanity
1320 * check against the stateid, before incrementing the seqid#, since
1321 * we want to return NFSERR_BADSTATEID on failure and the seqid#
1322 * shouldn't be incremented for this case.
1323 * If nfsrv_getlockfile() returns -1, it means "not found", which
1324 * will be handled later.
1325 * If we are doing Lock/LockU and local locking is enabled, sleep
1326 * lock the nfslockfile structure.
1328 getlckret = nfsrv_getlockfh(vp, new_stp->ls_flags, NULL, &nfh, p);
1330 if (getlckret == 0) {
1331 if ((new_stp->ls_flags & (NFSLCK_LOCK | NFSLCK_UNLOCK)) != 0 &&
1332 nfsrv_dolocallocks != 0 && nd->nd_repstat == 0) {
1333 getlckret = nfsrv_getlockfile(new_stp->ls_flags, NULL,
1336 filestruct_locked = 1;
1338 getlckret = nfsrv_getlockfile(new_stp->ls_flags, NULL,
1341 if (getlckret != 0 && getlckret != -1)
1344 if (filestruct_locked != 0) {
1345 LIST_INIT(&lfp->lf_rollback);
1346 if ((new_stp->ls_flags & NFSLCK_LOCK)) {
1348 * For local locking, do the advisory locking now, so
1349 * that any conflict can be detected. A failure later
1350 * can be rolled back locally. If an error is returned,
1351 * struct nfslockfile has been unlocked and any local
1352 * locking rolled back.
1355 reterr = nfsrv_locallock(vp, lfp,
1356 (new_lop->lo_flags & (NFSLCK_READ | NFSLCK_WRITE)),
1357 new_lop->lo_first, new_lop->lo_end, cfp, p);
1363 * Since the code is manipulating lists that are also
1364 * manipulated by nfsrv_servertimer(), soft clock interrupts
1365 * must be masked off.
1367 if (specialid == 0) {
1368 if (new_stp->ls_flags & NFSLCK_TEST) {
1370 * RFC 3530 does not list LockT as an op that renews a
1371 * lease, but the concensus seems to be that it is ok
1372 * for a server to do so.
1374 error = nfsrv_getclient(clientid, CLOPS_RENEW, &clp,
1375 (nfsquad_t)((u_quad_t)0), NULL, p);
1378 * Since NFSERR_EXPIRED, NFSERR_ADMINREVOKED are not valid
1379 * error returns for LockT, just go ahead and test for a lock,
1380 * since there are no locks for this client, but other locks
1381 * can conflict. (ie. same client will always be false)
1383 if (error == NFSERR_EXPIRED || error == NFSERR_ADMINREVOKED)
1387 error = nfsrv_getclient(clientid, CLOPS_RENEW, &clp,
1388 (nfsquad_t)((u_quad_t)0), NULL, p);
1391 * Look up the stateid
1393 error = nfsrv_getstate(clp, &new_stp->ls_stateid,
1394 new_stp->ls_flags, &stp);
1396 * do some sanity checks for an unconfirmed open or a
1397 * stateid that refers to the wrong file, for an open stateid
1399 if (error == 0 && (stp->ls_flags & NFSLCK_OPEN) &&
1400 ((stp->ls_openowner->ls_flags & NFSLCK_NEEDSCONFIRM) ||
1401 (getlckret == 0 && stp->ls_lfp != lfp)))
1402 error = NFSERR_BADSTATEID;
1404 (stp->ls_flags & (NFSLCK_DELEGREAD | NFSLCK_DELEGWRITE)) &&
1405 getlckret == 0 && stp->ls_lfp != lfp)
1406 error = NFSERR_BADSTATEID;
1409 * If the lockowner stateid doesn't refer to the same file,
1410 * I believe that is considered ok, since some clients will
1411 * only create a single lockowner and use that for all locks
1413 * For now, log it as a diagnostic, instead of considering it
1416 if (error == 0 && (stp->ls_flags &
1417 (NFSLCK_OPEN | NFSLCK_DELEGREAD | NFSLCK_DELEGWRITE)) == 0 &&
1418 getlckret == 0 && stp->ls_lfp != lfp) {
1420 printf("Got a lock statid for different file open\n");
1423 error = NFSERR_BADSTATEID;
1428 if (new_stp->ls_flags & NFSLCK_OPENTOLOCK) {
1430 * If haslock set, we've already checked the seqid.
1433 if (stp->ls_flags & NFSLCK_OPEN)
1434 error = nfsrv_checkseqid(nd, new_stp->ls_seq,
1435 stp->ls_openowner, new_stp->ls_op);
1437 error = NFSERR_BADSTATEID;
1440 nfsrv_getowner(&stp->ls_open, new_stp, &lckstp);
1443 * I believe this should be an error, but it
1444 * isn't obvious what NFSERR_xxx would be
1445 * appropriate, so I'll use NFSERR_INVAL for now.
1447 error = NFSERR_INVAL;
1450 } else if (new_stp->ls_flags&(NFSLCK_LOCK|NFSLCK_UNLOCK)) {
1452 * If haslock set, ditto above.
1455 if (stp->ls_flags & NFSLCK_OPEN)
1456 error = NFSERR_BADSTATEID;
1458 error = nfsrv_checkseqid(nd, new_stp->ls_seq,
1459 stp, new_stp->ls_op);
1467 * If the seqid part of the stateid isn't the same, return
1468 * NFSERR_OLDSTATEID for cases other than I/O Ops.
1469 * For I/O Ops, only return NFSERR_OLDSTATEID if
1470 * nfsrv_returnoldstateid is set. (The concensus on the email
1471 * list was that most clients would prefer to not receive
1472 * NFSERR_OLDSTATEID for I/O Ops, but the RFC suggests that that
1473 * is what will happen, so I use the nfsrv_returnoldstateid to
1474 * allow for either server configuration.)
1476 if (!error && stp->ls_stateid.seqid!=new_stp->ls_stateid.seqid &&
1477 (!(new_stp->ls_flags & NFSLCK_CHECK) ||
1478 nfsrv_returnoldstateid))
1479 error = NFSERR_OLDSTATEID;
1484 * Now we can check for grace.
1487 error = nfsrv_checkgrace(new_stp->ls_flags);
1488 if ((new_stp->ls_flags & NFSLCK_RECLAIM) && !error &&
1489 nfsrv_checkstable(clp))
1490 error = NFSERR_NOGRACE;
1492 * If we successfully Reclaimed state, note that.
1494 if ((new_stp->ls_flags & NFSLCK_RECLAIM) && !error)
1495 nfsrv_markstable(clp);
1498 * At this point, either error == NFSERR_BADSTATEID or the
1499 * seqid# has been updated, so we can return any error.
1500 * If error == 0, there may be an error in:
1501 * nd_repstat - Set by the calling function.
1502 * reterr - Set above, if getting the nfslockfile structure
1503 * or acquiring the local lock failed.
1504 * (If both of these are set, nd_repstat should probably be
1505 * returned, since that error was detected before this
1508 if (error != 0 || nd->nd_repstat != 0 || reterr != 0) {
1510 if (nd->nd_repstat != 0)
1511 error = nd->nd_repstat;
1515 if (filestruct_locked != 0) {
1516 /* Roll back local locks. */
1518 nfsrv_locallock_rollback(vp, lfp, p);
1520 nfsrv_unlocklf(lfp);
1524 FREE((caddr_t)other_lop, M_NFSDLOCK);
1526 NFSLOCKV4ROOTMUTEX();
1527 nfsv4_unlock(&nfsv4rootfs_lock, 1);
1528 NFSUNLOCKV4ROOTMUTEX();
1534 * Check the nfsrv_getlockfile return.
1535 * Returned -1 if no structure found.
1537 if (getlckret == -1) {
1538 error = NFSERR_EXPIRED;
1540 * Called from lockt, so no lock is OK.
1542 if (new_stp->ls_flags & NFSLCK_TEST) {
1544 } else if (new_stp->ls_flags &
1545 (NFSLCK_CHECK | NFSLCK_SETATTR)) {
1547 * Called to check for a lock, OK if the stateid is all
1548 * 1s or all 0s, but there should be an nfsstate
1550 * (ie. If there is no open, I'll assume no share
1556 error = NFSERR_BADSTATEID;
1560 NFSLOCKV4ROOTMUTEX();
1561 nfsv4_unlock(&nfsv4rootfs_lock, 1);
1562 NFSUNLOCKV4ROOTMUTEX();
1565 * Called to lock or unlock, so the lock has gone away.
1571 * For NFSLCK_CHECK and NFSLCK_LOCK, test for a share conflict.
1572 * For NFSLCK_CHECK, allow a read if write access is granted,
1573 * but check for a deny. For NFSLCK_LOCK, require correct access,
1574 * which implies a conflicting deny can't exist.
1576 if (new_stp->ls_flags & (NFSLCK_CHECK | NFSLCK_LOCK)) {
1578 * Four kinds of state id:
1579 * - specialid (all 0s or all 1s), only for NFSLCK_CHECK
1580 * - stateid for an open
1581 * - stateid for a delegation
1582 * - stateid for a lock owner
1585 if (stp->ls_flags & (NFSLCK_DELEGREAD | NFSLCK_DELEGWRITE)) {
1588 nfsrv_delaydelegtimeout(stp);
1589 } else if (stp->ls_flags & NFSLCK_OPEN) {
1592 mystp = stp->ls_openstp;
1595 * If locking or checking, require correct access
1598 if (((new_stp->ls_flags & NFSLCK_LOCK) &&
1599 !((new_lop->lo_flags >> NFSLCK_LOCKSHIFT) &
1600 mystp->ls_flags & NFSLCK_ACCESSBITS)) ||
1601 ((new_stp->ls_flags & (NFSLCK_CHECK|NFSLCK_READACCESS)) ==
1602 (NFSLCK_CHECK | NFSLCK_READACCESS) &&
1603 !(mystp->ls_flags & NFSLCK_READACCESS)) ||
1604 ((new_stp->ls_flags & (NFSLCK_CHECK|NFSLCK_WRITEACCESS)) ==
1605 (NFSLCK_CHECK | NFSLCK_WRITEACCESS) &&
1606 !(mystp->ls_flags & NFSLCK_WRITEACCESS))) {
1607 if (filestruct_locked != 0) {
1608 /* Roll back local locks. */
1610 nfsrv_locallock_rollback(vp, lfp, p);
1612 nfsrv_unlocklf(lfp);
1616 FREE((caddr_t)other_lop, M_NFSDLOCK);
1618 NFSLOCKV4ROOTMUTEX();
1619 nfsv4_unlock(&nfsv4rootfs_lock, 1);
1620 NFSUNLOCKV4ROOTMUTEX();
1622 return (NFSERR_OPENMODE);
1626 if ((new_stp->ls_flags & NFSLCK_CHECK) && !delegation) {
1628 * Check for a conflicting deny bit.
1630 LIST_FOREACH(tstp, &lfp->lf_open, ls_file) {
1631 if (tstp != mystp) {
1632 bits = tstp->ls_flags;
1633 bits >>= NFSLCK_SHIFT;
1634 if (new_stp->ls_flags & bits & NFSLCK_ACCESSBITS) {
1635 ret = nfsrv_clientconflict(tstp->ls_clp, &haslock,
1639 * nfsrv_clientconflict unlocks state
1640 * when it returns non-zero.
1647 NFSLOCKV4ROOTMUTEX();
1648 nfsv4_unlock(&nfsv4rootfs_lock, 1);
1649 NFSUNLOCKV4ROOTMUTEX();
1651 return (NFSERR_OPENMODE);
1656 /* We're outta here */
1659 NFSLOCKV4ROOTMUTEX();
1660 nfsv4_unlock(&nfsv4rootfs_lock, 1);
1661 NFSUNLOCKV4ROOTMUTEX();
1668 * For setattr, just get rid of all the Delegations for other clients.
1670 if (new_stp->ls_flags & NFSLCK_SETATTR) {
1671 ret = nfsrv_cleandeleg(vp, lfp, clp, &haslock, p);
1674 * nfsrv_cleandeleg() unlocks state when it
1683 if (!(new_stp->ls_flags & NFSLCK_CHECK) ||
1684 (LIST_EMPTY(&lfp->lf_open) && LIST_EMPTY(&lfp->lf_lock) &&
1685 LIST_EMPTY(&lfp->lf_deleg))) {
1688 NFSLOCKV4ROOTMUTEX();
1689 nfsv4_unlock(&nfsv4rootfs_lock, 1);
1690 NFSUNLOCKV4ROOTMUTEX();
1697 * Check for a conflicting delegation. If one is found, call
1698 * nfsrv_delegconflict() to handle it. If the v4root lock hasn't
1699 * been set yet, it will get the lock. Otherwise, it will recall
1700 * the delegation. Then, we try try again...
1701 * I currently believe the conflict algorithm to be:
1702 * For Lock Ops (Lock/LockT/LockU)
1703 * - there is a conflict iff a different client has a write delegation
1704 * For Reading (Read Op)
1705 * - there is a conflict iff a different client has a write delegation
1706 * (the specialids are always a different client)
1707 * For Writing (Write/Setattr of size)
1708 * - there is a conflict if a different client has any delegation
1709 * - there is a conflict if the same client has a read delegation
1710 * (I don't understand why this isn't allowed, but that seems to be
1711 * the current concensus?)
1713 tstp = LIST_FIRST(&lfp->lf_deleg);
1714 while (tstp != LIST_END(&lfp->lf_deleg)) {
1715 nstp = LIST_NEXT(tstp, ls_file);
1716 if ((((new_stp->ls_flags&(NFSLCK_LOCK|NFSLCK_UNLOCK|NFSLCK_TEST))||
1717 ((new_stp->ls_flags & NFSLCK_CHECK) &&
1718 (new_lop->lo_flags & NFSLCK_READ))) &&
1719 clp != tstp->ls_clp &&
1720 (tstp->ls_flags & NFSLCK_DELEGWRITE)) ||
1721 ((new_stp->ls_flags & NFSLCK_CHECK) &&
1722 (new_lop->lo_flags & NFSLCK_WRITE) &&
1723 (clp != tstp->ls_clp ||
1724 (tstp->ls_flags & NFSLCK_DELEGREAD)))) {
1725 if (filestruct_locked != 0) {
1726 /* Roll back local locks. */
1728 nfsrv_locallock_rollback(vp, lfp, p);
1730 nfsrv_unlocklf(lfp);
1732 ret = nfsrv_delegconflict(tstp, &haslock, p, vp);
1735 * nfsrv_delegconflict unlocks state when it
1736 * returns non-zero, which it always does.
1739 FREE((caddr_t)other_lop, M_NFSDLOCK);
1748 /* Never gets here. */
1754 * Handle the unlock case by calling nfsrv_updatelock().
1755 * (Should I have done some access checking above for unlock? For now,
1756 * just let it happen.)
1758 if (new_stp->ls_flags & NFSLCK_UNLOCK) {
1759 first = new_lop->lo_first;
1760 end = new_lop->lo_end;
1761 nfsrv_updatelock(stp, new_lopp, &other_lop, lfp);
1762 stateidp->seqid = ++(stp->ls_stateid.seqid);
1763 stateidp->other[0] = stp->ls_stateid.other[0];
1764 stateidp->other[1] = stp->ls_stateid.other[1];
1765 stateidp->other[2] = stp->ls_stateid.other[2];
1766 if (filestruct_locked != 0) {
1768 /* Update the local locks. */
1769 nfsrv_localunlock(vp, lfp, first, end, p);
1771 nfsrv_unlocklf(lfp);
1775 NFSLOCKV4ROOTMUTEX();
1776 nfsv4_unlock(&nfsv4rootfs_lock, 1);
1777 NFSUNLOCKV4ROOTMUTEX();
1783 * Search for a conflicting lock. A lock conflicts if:
1784 * - the lock range overlaps and
1785 * - at least one lock is a write lock and
1786 * - it is not owned by the same lock owner
1789 LIST_FOREACH(lop, &lfp->lf_lock, lo_lckfile) {
1790 if (new_lop->lo_end > lop->lo_first &&
1791 new_lop->lo_first < lop->lo_end &&
1792 (new_lop->lo_flags == NFSLCK_WRITE ||
1793 lop->lo_flags == NFSLCK_WRITE) &&
1794 lckstp != lop->lo_stp &&
1795 (clp != lop->lo_stp->ls_clp ||
1796 lckstp->ls_ownerlen != lop->lo_stp->ls_ownerlen ||
1797 NFSBCMP(lckstp->ls_owner, lop->lo_stp->ls_owner,
1798 lckstp->ls_ownerlen))) {
1800 FREE((caddr_t)other_lop, M_NFSDLOCK);
1803 ret = nfsrv_clientconflict(lop->lo_stp->ls_clp,&haslock,vp,p);
1805 if (filestruct_locked != 0) {
1806 /* Roll back local locks. */
1807 nfsrv_locallock_rollback(vp, lfp, p);
1809 nfsrv_unlocklf(lfp);
1813 * nfsrv_clientconflict() unlocks state when it
1820 * Found a conflicting lock, so record the conflict and
1824 cfp->cl_clientid.lval[0]=lop->lo_stp->ls_stateid.other[0];
1825 cfp->cl_clientid.lval[1]=lop->lo_stp->ls_stateid.other[1];
1826 cfp->cl_first = lop->lo_first;
1827 cfp->cl_end = lop->lo_end;
1828 cfp->cl_flags = lop->lo_flags;
1829 cfp->cl_ownerlen = lop->lo_stp->ls_ownerlen;
1830 NFSBCOPY(lop->lo_stp->ls_owner, cfp->cl_owner,
1833 if (new_stp->ls_flags & NFSLCK_RECLAIM)
1834 error = NFSERR_RECLAIMCONFLICT;
1835 else if (new_stp->ls_flags & NFSLCK_CHECK)
1836 error = NFSERR_LOCKED;
1838 error = NFSERR_DENIED;
1839 if (filestruct_locked != 0) {
1840 /* Roll back local locks. */
1842 nfsrv_locallock_rollback(vp, lfp, p);
1844 nfsrv_unlocklf(lfp);
1848 NFSLOCKV4ROOTMUTEX();
1849 nfsv4_unlock(&nfsv4rootfs_lock, 1);
1850 NFSUNLOCKV4ROOTMUTEX();
1858 * We only get here if there was no lock that conflicted.
1860 if (new_stp->ls_flags & (NFSLCK_TEST | NFSLCK_CHECK)) {
1863 NFSLOCKV4ROOTMUTEX();
1864 nfsv4_unlock(&nfsv4rootfs_lock, 1);
1865 NFSUNLOCKV4ROOTMUTEX();
1871 * We only get here when we are creating or modifying a lock.
1872 * There are two variants:
1873 * - exist_lock_owner where lock_owner exists
1874 * - open_to_lock_owner with new lock_owner
1876 first = new_lop->lo_first;
1877 end = new_lop->lo_end;
1878 lock_flags = new_lop->lo_flags;
1879 if (!(new_stp->ls_flags & NFSLCK_OPENTOLOCK)) {
1880 nfsrv_updatelock(lckstp, new_lopp, &other_lop, lfp);
1881 stateidp->seqid = ++(lckstp->ls_stateid.seqid);
1882 stateidp->other[0] = lckstp->ls_stateid.other[0];
1883 stateidp->other[1] = lckstp->ls_stateid.other[1];
1884 stateidp->other[2] = lckstp->ls_stateid.other[2];
1887 * The new open_to_lock_owner case.
1888 * Link the new nfsstate into the lists.
1890 new_stp->ls_seq = new_stp->ls_opentolockseq;
1891 nfsrvd_refcache(new_stp->ls_op);
1892 stateidp->seqid = new_stp->ls_stateid.seqid = 0;
1893 stateidp->other[0] = new_stp->ls_stateid.other[0] =
1894 clp->lc_clientid.lval[0];
1895 stateidp->other[1] = new_stp->ls_stateid.other[1] =
1896 clp->lc_clientid.lval[1];
1897 stateidp->other[2] = new_stp->ls_stateid.other[2] =
1898 nfsrv_nextstateindex(clp);
1899 new_stp->ls_clp = clp;
1900 LIST_INIT(&new_stp->ls_lock);
1901 new_stp->ls_openstp = stp;
1902 new_stp->ls_lfp = lfp;
1903 nfsrv_insertlock(new_lop, (struct nfslock *)new_stp, new_stp,
1905 LIST_INSERT_HEAD(NFSSTATEHASH(clp, new_stp->ls_stateid),
1907 LIST_INSERT_HEAD(&stp->ls_open, new_stp, ls_list);
1910 newnfsstats.srvlockowners++;
1911 nfsrv_openpluslock++;
1913 if (filestruct_locked != 0) {
1915 nfsrv_locallock_commit(lfp, lock_flags, first, end);
1917 nfsrv_unlocklf(lfp);
1921 NFSLOCKV4ROOTMUTEX();
1922 nfsv4_unlock(&nfsv4rootfs_lock, 1);
1923 NFSUNLOCKV4ROOTMUTEX();
1926 FREE((caddr_t)other_lop, M_NFSDLOCK);
1931 * Check for state errors for Open.
1932 * repstat is passed back out as an error if more critical errors
1936 nfsrv_opencheck(nfsquad_t clientid, nfsv4stateid_t *stateidp,
1937 struct nfsstate *new_stp, vnode_t vp, struct nfsrv_descript *nd,
1938 NFSPROC_T *p, int repstat)
1940 struct nfsstate *stp, *nstp;
1941 struct nfsclient *clp;
1942 struct nfsstate *ownerstp;
1943 struct nfslockfile *lfp, *new_lfp;
1944 int error, haslock = 0, ret, readonly = 0, getfhret = 0;
1946 if ((new_stp->ls_flags & NFSLCK_SHAREBITS) == NFSLCK_READACCESS)
1949 * Check for restart conditions (client and server).
1951 error = nfsrv_checkrestart(clientid, new_stp->ls_flags,
1952 &new_stp->ls_stateid, 0);
1957 * Check for state resource limit exceeded.
1958 * Technically this should be SMP protected, but the worst
1959 * case error is "out by one or two" on the count when it
1960 * returns NFSERR_RESOURCE and the limit is just a rather
1961 * arbitrary high water mark, so no harm is done.
1963 if (nfsrv_openpluslock > NFSRV_V4STATELIMIT)
1964 return (NFSERR_RESOURCE);
1967 MALLOC(new_lfp, struct nfslockfile *, sizeof (struct nfslockfile),
1968 M_NFSDLOCKFILE, M_WAITOK);
1970 getfhret = nfsrv_getlockfh(vp, new_stp->ls_flags, &new_lfp,
1974 * Get the nfsclient structure.
1975 * Since the code is manipulating lists that are also
1976 * manipulated by nfsrv_servertimer(), soft clock interrupts
1977 * must be masked off.
1979 error = nfsrv_getclient(clientid, CLOPS_RENEW, &clp,
1980 (nfsquad_t)((u_quad_t)0), NULL, p);
1983 * Look up the open owner. See if it needs confirmation and
1984 * check the seq#, as required.
1987 nfsrv_getowner(&clp->lc_open, new_stp, &ownerstp);
1989 if (!error && ownerstp) {
1990 error = nfsrv_checkseqid(nd, new_stp->ls_seq, ownerstp,
1993 * If the OpenOwner hasn't been confirmed, assume the
1994 * old one was a replay and this one is ok.
1995 * See: RFC3530 Sec. 14.2.18.
1997 if (error == NFSERR_BADSEQID &&
1998 (ownerstp->ls_flags & NFSLCK_NEEDSCONFIRM))
2006 error = nfsrv_checkgrace(new_stp->ls_flags);
2007 if ((new_stp->ls_flags & NFSLCK_RECLAIM) && !error &&
2008 nfsrv_checkstable(clp))
2009 error = NFSERR_NOGRACE;
2012 * If none of the above errors occurred, let repstat be
2015 if (repstat && !error)
2020 NFSLOCKV4ROOTMUTEX();
2021 nfsv4_unlock(&nfsv4rootfs_lock, 1);
2022 NFSUNLOCKV4ROOTMUTEX();
2024 free((caddr_t)new_lfp, M_NFSDLOCKFILE);
2029 * If vp == NULL, the file doesn't exist yet, so return ok.
2030 * (This always happens on the first pass, so haslock must be 0.)
2034 FREE((caddr_t)new_lfp, M_NFSDLOCKFILE);
2039 * Get the structure for the underlying file.
2044 error = nfsrv_getlockfile(new_stp->ls_flags, &new_lfp, &lfp,
2047 FREE((caddr_t)new_lfp, M_NFSDLOCKFILE);
2051 NFSLOCKV4ROOTMUTEX();
2052 nfsv4_unlock(&nfsv4rootfs_lock, 1);
2053 NFSUNLOCKV4ROOTMUTEX();
2059 * Search for a conflicting open/share.
2061 if (new_stp->ls_flags & NFSLCK_DELEGCUR) {
2063 * For Delegate_Cur, search for the matching Delegation,
2064 * which indicates no conflict.
2065 * An old delegation should have been recovered by the
2066 * client doing a Claim_DELEGATE_Prev, so I won't let
2067 * it match and return NFSERR_EXPIRED. Should I let it
2070 LIST_FOREACH(stp, &lfp->lf_deleg, ls_file) {
2071 if (!(stp->ls_flags & NFSLCK_OLDDELEG) &&
2072 stateidp->seqid == stp->ls_stateid.seqid &&
2073 !NFSBCMP(stateidp->other, stp->ls_stateid.other,
2077 if (stp == LIST_END(&lfp->lf_deleg) ||
2078 ((new_stp->ls_flags & NFSLCK_WRITEACCESS) &&
2079 (stp->ls_flags & NFSLCK_DELEGREAD))) {
2082 NFSLOCKV4ROOTMUTEX();
2083 nfsv4_unlock(&nfsv4rootfs_lock, 1);
2084 NFSUNLOCKV4ROOTMUTEX();
2086 return (NFSERR_EXPIRED);
2091 * Check for access/deny bit conflicts. I check for the same
2092 * owner as well, in case the client didn't bother.
2094 LIST_FOREACH(stp, &lfp->lf_open, ls_file) {
2095 if (!(new_stp->ls_flags & NFSLCK_DELEGCUR) &&
2096 (((new_stp->ls_flags & NFSLCK_ACCESSBITS) &
2097 ((stp->ls_flags>>NFSLCK_SHIFT) & NFSLCK_ACCESSBITS))||
2098 ((stp->ls_flags & NFSLCK_ACCESSBITS) &
2099 ((new_stp->ls_flags>>NFSLCK_SHIFT)&NFSLCK_ACCESSBITS)))){
2100 ret = nfsrv_clientconflict(stp->ls_clp,&haslock,vp,p);
2103 * nfsrv_clientconflict() unlocks
2104 * state when it returns non-zero.
2108 if (new_stp->ls_flags & NFSLCK_RECLAIM)
2109 error = NFSERR_RECLAIMCONFLICT;
2111 error = NFSERR_SHAREDENIED;
2114 NFSLOCKV4ROOTMUTEX();
2115 nfsv4_unlock(&nfsv4rootfs_lock, 1);
2116 NFSUNLOCKV4ROOTMUTEX();
2123 * Check for a conflicting delegation. If one is found, call
2124 * nfsrv_delegconflict() to handle it. If the v4root lock hasn't
2125 * been set yet, it will get the lock. Otherwise, it will recall
2126 * the delegation. Then, we try try again...
2127 * (If NFSLCK_DELEGCUR is set, it has a delegation, so there
2128 * isn't a conflict.)
2129 * I currently believe the conflict algorithm to be:
2130 * For Open with Read Access and Deny None
2131 * - there is a conflict iff a different client has a write delegation
2132 * For Open with other Write Access or any Deny except None
2133 * - there is a conflict if a different client has any delegation
2134 * - there is a conflict if the same client has a read delegation
2135 * (The current concensus is that this last case should be
2136 * considered a conflict since the client with a read delegation
2137 * could have done an Open with ReadAccess and WriteDeny
2138 * locally and then not have checked for the WriteDeny.)
2139 * Don't check for a Reclaim, since that will be dealt with
2140 * by nfsrv_openctrl().
2142 if (!(new_stp->ls_flags &
2143 (NFSLCK_DELEGPREV | NFSLCK_DELEGCUR | NFSLCK_RECLAIM))) {
2144 stp = LIST_FIRST(&lfp->lf_deleg);
2145 while (stp != LIST_END(&lfp->lf_deleg)) {
2146 nstp = LIST_NEXT(stp, ls_file);
2147 if ((readonly && stp->ls_clp != clp &&
2148 (stp->ls_flags & NFSLCK_DELEGWRITE)) ||
2149 (!readonly && (stp->ls_clp != clp ||
2150 (stp->ls_flags & NFSLCK_DELEGREAD)))) {
2151 ret = nfsrv_delegconflict(stp, &haslock, p, vp);
2154 * nfsrv_delegconflict() unlocks state
2155 * when it returns non-zero.
2167 NFSLOCKV4ROOTMUTEX();
2168 nfsv4_unlock(&nfsv4rootfs_lock, 1);
2169 NFSUNLOCKV4ROOTMUTEX();
2175 * Open control function to create/update open state for an open.
2178 nfsrv_openctrl(struct nfsrv_descript *nd, vnode_t vp,
2179 struct nfsstate **new_stpp, nfsquad_t clientid, nfsv4stateid_t *stateidp,
2180 nfsv4stateid_t *delegstateidp, u_int32_t *rflagsp, struct nfsexstuff *exp,
2181 NFSPROC_T *p, u_quad_t filerev)
2183 struct nfsstate *new_stp = *new_stpp;
2184 struct nfsstate *stp, *nstp;
2185 struct nfsstate *openstp = NULL, *new_open, *ownerstp, *new_deleg;
2186 struct nfslockfile *lfp, *new_lfp;
2187 struct nfsclient *clp;
2188 int error, haslock = 0, ret, delegate = 1, writedeleg = 1;
2189 int readonly = 0, cbret = 1, getfhret = 0;
2191 if ((new_stp->ls_flags & NFSLCK_SHAREBITS) == NFSLCK_READACCESS)
2194 * Check for restart conditions (client and server).
2195 * (Paranoia, should have been detected by nfsrv_opencheck().)
2196 * If an error does show up, return NFSERR_EXPIRED, since the
2197 * the seqid# has already been incremented.
2199 error = nfsrv_checkrestart(clientid, new_stp->ls_flags,
2200 &new_stp->ls_stateid, 0);
2202 printf("Nfsd: openctrl unexpected restart err=%d\n",
2204 return (NFSERR_EXPIRED);
2208 MALLOC(new_lfp, struct nfslockfile *, sizeof (struct nfslockfile),
2209 M_NFSDLOCKFILE, M_WAITOK);
2210 MALLOC(new_open, struct nfsstate *, sizeof (struct nfsstate),
2211 M_NFSDSTATE, M_WAITOK);
2212 MALLOC(new_deleg, struct nfsstate *, sizeof (struct nfsstate),
2213 M_NFSDSTATE, M_WAITOK);
2214 getfhret = nfsrv_getlockfh(vp, new_stp->ls_flags, &new_lfp,
2218 * Get the client structure. Since the linked lists could be changed
2219 * by other nfsd processes if this process does a tsleep(), one of
2220 * two things must be done.
2221 * 1 - don't tsleep()
2223 * 2 - get the nfsv4_lock() { indicated by haslock == 1 }
2224 * before using the lists, since this lock stops the other
2225 * nfsd. This should only be used for rare cases, since it
2226 * essentially single threads the nfsd.
2227 * At this time, it is only done for cases where the stable
2228 * storage file must be written prior to completion of state
2231 error = nfsrv_getclient(clientid, CLOPS_RENEW, &clp,
2232 (nfsquad_t)((u_quad_t)0), NULL, p);
2233 if (!error && (clp->lc_flags & LCL_NEEDSCBNULL) &&
2236 * This happens on the first open for a client
2237 * that supports callbacks.
2241 * Although nfsrv_docallback() will sleep, clp won't
2242 * go away, since they are only removed when the
2243 * nfsv4_lock() has blocked the nfsd threads. The
2244 * fields in clp can change, but having multiple
2245 * threads do this Null callback RPC should be
2248 cbret = nfsrv_docallback(clp, NFSV4PROC_CBNULL,
2249 NULL, 0, NULL, NULL, NULL, p);
2251 clp->lc_flags &= ~LCL_NEEDSCBNULL;
2253 clp->lc_flags |= LCL_CALLBACKSON;
2257 * Look up the open owner. See if it needs confirmation and
2258 * check the seq#, as required.
2261 nfsrv_getowner(&clp->lc_open, new_stp, &ownerstp);
2265 printf("Nfsd: openctrl unexpected state err=%d\n",
2267 free((caddr_t)new_lfp, M_NFSDLOCKFILE);
2268 free((caddr_t)new_open, M_NFSDSTATE);
2269 free((caddr_t)new_deleg, M_NFSDSTATE);
2271 NFSLOCKV4ROOTMUTEX();
2272 nfsv4_unlock(&nfsv4rootfs_lock, 1);
2273 NFSUNLOCKV4ROOTMUTEX();
2275 return (NFSERR_EXPIRED);
2278 if (new_stp->ls_flags & NFSLCK_RECLAIM)
2279 nfsrv_markstable(clp);
2282 * Get the structure for the underlying file.
2287 error = nfsrv_getlockfile(new_stp->ls_flags, &new_lfp, &lfp,
2290 FREE((caddr_t)new_lfp, M_NFSDLOCKFILE);
2293 printf("Nfsd openctrl unexpected getlockfile err=%d\n",
2295 free((caddr_t)new_open, M_NFSDSTATE);
2296 free((caddr_t)new_deleg, M_NFSDSTATE);
2298 NFSLOCKV4ROOTMUTEX();
2299 nfsv4_unlock(&nfsv4rootfs_lock, 1);
2300 NFSUNLOCKV4ROOTMUTEX();
2306 * Search for a conflicting open/share.
2308 if (new_stp->ls_flags & NFSLCK_DELEGCUR) {
2310 * For Delegate_Cur, search for the matching Delegation,
2311 * which indicates no conflict.
2312 * An old delegation should have been recovered by the
2313 * client doing a Claim_DELEGATE_Prev, so I won't let
2314 * it match and return NFSERR_EXPIRED. Should I let it
2317 LIST_FOREACH(stp, &lfp->lf_deleg, ls_file) {
2318 if (!(stp->ls_flags & NFSLCK_OLDDELEG) &&
2319 stateidp->seqid == stp->ls_stateid.seqid &&
2320 !NFSBCMP(stateidp->other, stp->ls_stateid.other,
2324 if (stp == LIST_END(&lfp->lf_deleg) ||
2325 ((new_stp->ls_flags & NFSLCK_WRITEACCESS) &&
2326 (stp->ls_flags & NFSLCK_DELEGREAD))) {
2328 printf("Nfsd openctrl unexpected expiry\n");
2329 free((caddr_t)new_open, M_NFSDSTATE);
2330 free((caddr_t)new_deleg, M_NFSDSTATE);
2332 NFSLOCKV4ROOTMUTEX();
2333 nfsv4_unlock(&nfsv4rootfs_lock, 1);
2334 NFSUNLOCKV4ROOTMUTEX();
2336 return (NFSERR_EXPIRED);
2340 * Don't issue a Delegation, since one already exists and
2341 * delay delegation timeout, as required.
2344 nfsrv_delaydelegtimeout(stp);
2348 * Check for access/deny bit conflicts. I also check for the
2349 * same owner, since the client might not have bothered to check.
2350 * Also, note an open for the same file and owner, if found,
2351 * which is all we do here for Delegate_Cur, since conflict
2352 * checking is already done.
2354 LIST_FOREACH(stp, &lfp->lf_open, ls_file) {
2355 if (ownerstp && stp->ls_openowner == ownerstp)
2357 if (!(new_stp->ls_flags & NFSLCK_DELEGCUR)) {
2359 * If another client has the file open, the only
2360 * delegation that can be issued is a Read delegation
2361 * and only if it is a Read open with Deny none.
2363 if (clp != stp->ls_clp) {
2364 if ((stp->ls_flags & NFSLCK_SHAREBITS) ==
2370 if(((new_stp->ls_flags & NFSLCK_ACCESSBITS) &
2371 ((stp->ls_flags>>NFSLCK_SHIFT) & NFSLCK_ACCESSBITS))||
2372 ((stp->ls_flags & NFSLCK_ACCESSBITS) &
2373 ((new_stp->ls_flags>>NFSLCK_SHIFT)&NFSLCK_ACCESSBITS))){
2374 ret = nfsrv_clientconflict(stp->ls_clp,&haslock,vp,p);
2377 * nfsrv_clientconflict() unlocks state
2378 * when it returns non-zero.
2380 free((caddr_t)new_open, M_NFSDSTATE);
2381 free((caddr_t)new_deleg, M_NFSDSTATE);
2385 if (new_stp->ls_flags & NFSLCK_RECLAIM)
2386 error = NFSERR_RECLAIMCONFLICT;
2388 error = NFSERR_SHAREDENIED;
2391 NFSLOCKV4ROOTMUTEX();
2392 nfsv4_unlock(&nfsv4rootfs_lock, 1);
2393 NFSUNLOCKV4ROOTMUTEX();
2395 free((caddr_t)new_open, M_NFSDSTATE);
2396 free((caddr_t)new_deleg, M_NFSDSTATE);
2397 printf("nfsd openctrl unexpected client cnfl\n");
2404 * Check for a conflicting delegation. If one is found, call
2405 * nfsrv_delegconflict() to handle it. If the v4root lock hasn't
2406 * been set yet, it will get the lock. Otherwise, it will recall
2407 * the delegation. Then, we try try again...
2408 * (If NFSLCK_DELEGCUR is set, it has a delegation, so there
2409 * isn't a conflict.)
2410 * I currently believe the conflict algorithm to be:
2411 * For Open with Read Access and Deny None
2412 * - there is a conflict iff a different client has a write delegation
2413 * For Open with other Write Access or any Deny except None
2414 * - there is a conflict if a different client has any delegation
2415 * - there is a conflict if the same client has a read delegation
2416 * (The current concensus is that this last case should be
2417 * considered a conflict since the client with a read delegation
2418 * could have done an Open with ReadAccess and WriteDeny
2419 * locally and then not have checked for the WriteDeny.)
2421 if (!(new_stp->ls_flags & (NFSLCK_DELEGPREV | NFSLCK_DELEGCUR))) {
2422 stp = LIST_FIRST(&lfp->lf_deleg);
2423 while (stp != LIST_END(&lfp->lf_deleg)) {
2424 nstp = LIST_NEXT(stp, ls_file);
2425 if (stp->ls_clp != clp && (stp->ls_flags & NFSLCK_DELEGREAD))
2429 if ((readonly && stp->ls_clp != clp &&
2430 (stp->ls_flags & NFSLCK_DELEGWRITE)) ||
2431 (!readonly && (stp->ls_clp != clp ||
2432 (stp->ls_flags & NFSLCK_DELEGREAD)))) {
2433 if (new_stp->ls_flags & NFSLCK_RECLAIM) {
2436 ret = nfsrv_delegconflict(stp, &haslock, p, vp);
2439 * nfsrv_delegconflict() unlocks state
2440 * when it returns non-zero.
2442 printf("Nfsd openctrl unexpected deleg cnfl\n");
2443 free((caddr_t)new_open, M_NFSDSTATE);
2444 free((caddr_t)new_deleg, M_NFSDSTATE);
2458 * We only get here if there was no open that conflicted.
2459 * If an open for the owner exists, or in the access/deny bits.
2460 * Otherwise it is a new open. If the open_owner hasn't been
2461 * confirmed, replace the open with the new one needing confirmation,
2462 * otherwise add the open.
2464 if (new_stp->ls_flags & NFSLCK_DELEGPREV) {
2466 * Handle NFSLCK_DELEGPREV by searching the old delegations for
2467 * a match. If found, just move the old delegation to the current
2468 * delegation list and issue open. If not found, return
2471 LIST_FOREACH(stp, &clp->lc_olddeleg, ls_list) {
2472 if (stp->ls_lfp == lfp) {
2474 if (stp->ls_clp != clp)
2475 panic("olddeleg clp");
2476 LIST_REMOVE(stp, ls_list);
2477 LIST_REMOVE(stp, ls_hash);
2478 stp->ls_flags &= ~NFSLCK_OLDDELEG;
2479 stp->ls_stateid.seqid = delegstateidp->seqid = 0;
2480 stp->ls_stateid.other[0] = delegstateidp->other[0] =
2481 clp->lc_clientid.lval[0];
2482 stp->ls_stateid.other[1] = delegstateidp->other[1] =
2483 clp->lc_clientid.lval[1];
2484 stp->ls_stateid.other[2] = delegstateidp->other[2] =
2485 nfsrv_nextstateindex(clp);
2486 stp->ls_compref = nd->nd_compref;
2487 LIST_INSERT_HEAD(&clp->lc_deleg, stp, ls_list);
2488 LIST_INSERT_HEAD(NFSSTATEHASH(clp,
2489 stp->ls_stateid), stp, ls_hash);
2490 if (stp->ls_flags & NFSLCK_DELEGWRITE)
2491 *rflagsp |= NFSV4OPEN_WRITEDELEGATE;
2493 *rflagsp |= NFSV4OPEN_READDELEGATE;
2494 clp->lc_delegtime = NFSD_MONOSEC +
2495 nfsrv_lease + NFSRV_LEASEDELTA;
2498 * Now, do the associated open.
2500 new_open->ls_stateid.seqid = 0;
2501 new_open->ls_stateid.other[0] = clp->lc_clientid.lval[0];
2502 new_open->ls_stateid.other[1] = clp->lc_clientid.lval[1];
2503 new_open->ls_stateid.other[2] = nfsrv_nextstateindex(clp);
2504 new_open->ls_flags = (new_stp->ls_flags&NFSLCK_DENYBITS)|
2506 if (stp->ls_flags & NFSLCK_DELEGWRITE)
2507 new_open->ls_flags |= (NFSLCK_READACCESS |
2508 NFSLCK_WRITEACCESS);
2510 new_open->ls_flags |= NFSLCK_READACCESS;
2511 new_open->ls_uid = new_stp->ls_uid;
2512 new_open->ls_lfp = lfp;
2513 new_open->ls_clp = clp;
2514 LIST_INIT(&new_open->ls_open);
2515 LIST_INSERT_HEAD(&lfp->lf_open, new_open, ls_file);
2516 LIST_INSERT_HEAD(NFSSTATEHASH(clp, new_open->ls_stateid),
2519 * and handle the open owner
2522 new_open->ls_openowner = ownerstp;
2523 LIST_INSERT_HEAD(&ownerstp->ls_open,new_open,ls_list);
2525 new_open->ls_openowner = new_stp;
2526 new_stp->ls_flags = 0;
2527 nfsrvd_refcache(new_stp->ls_op);
2528 new_stp->ls_noopens = 0;
2529 LIST_INIT(&new_stp->ls_open);
2530 LIST_INSERT_HEAD(&new_stp->ls_open, new_open, ls_list);
2531 LIST_INSERT_HEAD(&clp->lc_open, new_stp, ls_list);
2533 newnfsstats.srvopenowners++;
2534 nfsrv_openpluslock++;
2538 newnfsstats.srvopens++;
2539 nfsrv_openpluslock++;
2543 if (stp == LIST_END(&clp->lc_olddeleg))
2544 error = NFSERR_EXPIRED;
2545 } else if (new_stp->ls_flags & (NFSLCK_DELEGREAD | NFSLCK_DELEGWRITE)) {
2547 * Scan to see that no delegation for this client and file
2548 * doesn't already exist.
2549 * There also shouldn't yet be an Open for this file and
2552 LIST_FOREACH(stp, &lfp->lf_deleg, ls_file) {
2553 if (stp->ls_clp == clp)
2556 if (stp == LIST_END(&lfp->lf_deleg) && openstp == NULL) {
2558 * This is the Claim_Previous case with a delegation
2559 * type != Delegate_None.
2562 * First, add the delegation. (Although we must issue the
2563 * delegation, we can also ask for an immediate return.)
2565 new_deleg->ls_stateid.seqid = delegstateidp->seqid = 0;
2566 new_deleg->ls_stateid.other[0] = delegstateidp->other[0] =
2567 clp->lc_clientid.lval[0];
2568 new_deleg->ls_stateid.other[1] = delegstateidp->other[1] =
2569 clp->lc_clientid.lval[1];
2570 new_deleg->ls_stateid.other[2] = delegstateidp->other[2] =
2571 nfsrv_nextstateindex(clp);
2572 if (new_stp->ls_flags & NFSLCK_DELEGWRITE) {
2573 new_deleg->ls_flags = (NFSLCK_DELEGWRITE |
2574 NFSLCK_READACCESS | NFSLCK_WRITEACCESS);
2575 *rflagsp |= NFSV4OPEN_WRITEDELEGATE;
2577 new_deleg->ls_flags = (NFSLCK_DELEGREAD |
2579 *rflagsp |= NFSV4OPEN_READDELEGATE;
2581 new_deleg->ls_uid = new_stp->ls_uid;
2582 new_deleg->ls_lfp = lfp;
2583 new_deleg->ls_clp = clp;
2584 new_deleg->ls_filerev = filerev;
2585 new_deleg->ls_compref = nd->nd_compref;
2586 LIST_INSERT_HEAD(&lfp->lf_deleg, new_deleg, ls_file);
2587 LIST_INSERT_HEAD(NFSSTATEHASH(clp,
2588 new_deleg->ls_stateid), new_deleg, ls_hash);
2589 LIST_INSERT_HEAD(&clp->lc_deleg, new_deleg, ls_list);
2591 if (delegate == 2 || nfsrv_issuedelegs == 0 ||
2592 (clp->lc_flags & (LCL_CALLBACKSON | LCL_CBDOWN)) !=
2594 NFSRV_V4DELEGLIMIT(nfsrv_delegatecnt) ||
2595 !NFSVNO_DELEGOK(vp))
2596 *rflagsp |= NFSV4OPEN_RECALL;
2597 newnfsstats.srvdelegates++;
2598 nfsrv_openpluslock++;
2599 nfsrv_delegatecnt++;
2602 * Now, do the associated open.
2604 new_open->ls_stateid.seqid = 0;
2605 new_open->ls_stateid.other[0] = clp->lc_clientid.lval[0];
2606 new_open->ls_stateid.other[1] = clp->lc_clientid.lval[1];
2607 new_open->ls_stateid.other[2] = nfsrv_nextstateindex(clp);
2608 new_open->ls_flags = (new_stp->ls_flags & NFSLCK_DENYBITS) |
2610 if (new_stp->ls_flags & NFSLCK_DELEGWRITE)
2611 new_open->ls_flags |= (NFSLCK_READACCESS |
2612 NFSLCK_WRITEACCESS);
2614 new_open->ls_flags |= NFSLCK_READACCESS;
2615 new_open->ls_uid = new_stp->ls_uid;
2616 new_open->ls_lfp = lfp;
2617 new_open->ls_clp = clp;
2618 LIST_INIT(&new_open->ls_open);
2619 LIST_INSERT_HEAD(&lfp->lf_open, new_open, ls_file);
2620 LIST_INSERT_HEAD(NFSSTATEHASH(clp, new_open->ls_stateid),
2623 * and handle the open owner
2626 new_open->ls_openowner = ownerstp;
2627 LIST_INSERT_HEAD(&ownerstp->ls_open, new_open, ls_list);
2629 new_open->ls_openowner = new_stp;
2630 new_stp->ls_flags = 0;
2631 nfsrvd_refcache(new_stp->ls_op);
2632 new_stp->ls_noopens = 0;
2633 LIST_INIT(&new_stp->ls_open);
2634 LIST_INSERT_HEAD(&new_stp->ls_open, new_open, ls_list);
2635 LIST_INSERT_HEAD(&clp->lc_open, new_stp, ls_list);
2637 newnfsstats.srvopenowners++;
2638 nfsrv_openpluslock++;
2642 newnfsstats.srvopens++;
2643 nfsrv_openpluslock++;
2645 error = NFSERR_RECLAIMCONFLICT;
2647 } else if (ownerstp) {
2648 if (ownerstp->ls_flags & NFSLCK_NEEDSCONFIRM) {
2649 /* Replace the open */
2650 if (ownerstp->ls_op)
2651 nfsrvd_derefcache(ownerstp->ls_op);
2652 ownerstp->ls_op = new_stp->ls_op;
2653 nfsrvd_refcache(ownerstp->ls_op);
2654 ownerstp->ls_seq = new_stp->ls_seq;
2655 *rflagsp |= NFSV4OPEN_RESULTCONFIRM;
2656 stp = LIST_FIRST(&ownerstp->ls_open);
2657 stp->ls_flags = (new_stp->ls_flags & NFSLCK_SHAREBITS) |
2659 stp->ls_stateid.seqid = 0;
2660 stp->ls_uid = new_stp->ls_uid;
2661 if (lfp != stp->ls_lfp) {
2662 LIST_REMOVE(stp, ls_file);
2663 LIST_INSERT_HEAD(&lfp->lf_open, stp, ls_file);
2667 } else if (openstp) {
2668 openstp->ls_flags |= (new_stp->ls_flags & NFSLCK_SHAREBITS);
2669 openstp->ls_stateid.seqid++;
2672 * This is where we can choose to issue a delegation.
2674 if (delegate && nfsrv_issuedelegs &&
2675 writedeleg && !NFSVNO_EXRDONLY(exp) &&
2676 (nfsrv_writedelegifpos || !readonly) &&
2677 (clp->lc_flags & (LCL_CALLBACKSON | LCL_CBDOWN)) ==
2679 !NFSRV_V4DELEGLIMIT(nfsrv_delegatecnt) &&
2680 NFSVNO_DELEGOK(vp)) {
2681 new_deleg->ls_stateid.seqid = delegstateidp->seqid = 0;
2682 new_deleg->ls_stateid.other[0] = delegstateidp->other[0]
2683 = clp->lc_clientid.lval[0];
2684 new_deleg->ls_stateid.other[1] = delegstateidp->other[1]
2685 = clp->lc_clientid.lval[1];
2686 new_deleg->ls_stateid.other[2] = delegstateidp->other[2]
2687 = nfsrv_nextstateindex(clp);
2688 new_deleg->ls_flags = (NFSLCK_DELEGWRITE |
2689 NFSLCK_READACCESS | NFSLCK_WRITEACCESS);
2690 *rflagsp |= NFSV4OPEN_WRITEDELEGATE;
2691 new_deleg->ls_uid = new_stp->ls_uid;
2692 new_deleg->ls_lfp = lfp;
2693 new_deleg->ls_clp = clp;
2694 new_deleg->ls_filerev = filerev;
2695 new_deleg->ls_compref = nd->nd_compref;
2696 LIST_INSERT_HEAD(&lfp->lf_deleg, new_deleg, ls_file);
2697 LIST_INSERT_HEAD(NFSSTATEHASH(clp,
2698 new_deleg->ls_stateid), new_deleg, ls_hash);
2699 LIST_INSERT_HEAD(&clp->lc_deleg, new_deleg, ls_list);
2701 newnfsstats.srvdelegates++;
2702 nfsrv_openpluslock++;
2703 nfsrv_delegatecnt++;
2706 new_open->ls_stateid.seqid = 0;
2707 new_open->ls_stateid.other[0] = clp->lc_clientid.lval[0];
2708 new_open->ls_stateid.other[1] = clp->lc_clientid.lval[1];
2709 new_open->ls_stateid.other[2] = nfsrv_nextstateindex(clp);
2710 new_open->ls_flags = (new_stp->ls_flags & NFSLCK_SHAREBITS)|
2712 new_open->ls_uid = new_stp->ls_uid;
2713 new_open->ls_openowner = ownerstp;
2714 new_open->ls_lfp = lfp;
2715 new_open->ls_clp = clp;
2716 LIST_INIT(&new_open->ls_open);
2717 LIST_INSERT_HEAD(&lfp->lf_open, new_open, ls_file);
2718 LIST_INSERT_HEAD(&ownerstp->ls_open, new_open, ls_list);
2719 LIST_INSERT_HEAD(NFSSTATEHASH(clp, new_open->ls_stateid),
2723 newnfsstats.srvopens++;
2724 nfsrv_openpluslock++;
2727 * This is where we can choose to issue a delegation.
2729 if (delegate && nfsrv_issuedelegs &&
2730 (writedeleg || readonly) &&
2731 (clp->lc_flags & (LCL_CALLBACKSON | LCL_CBDOWN)) ==
2733 !NFSRV_V4DELEGLIMIT(nfsrv_delegatecnt) &&
2734 NFSVNO_DELEGOK(vp)) {
2735 new_deleg->ls_stateid.seqid = delegstateidp->seqid = 0;
2736 new_deleg->ls_stateid.other[0] = delegstateidp->other[0]
2737 = clp->lc_clientid.lval[0];
2738 new_deleg->ls_stateid.other[1] = delegstateidp->other[1]
2739 = clp->lc_clientid.lval[1];
2740 new_deleg->ls_stateid.other[2] = delegstateidp->other[2]
2741 = nfsrv_nextstateindex(clp);
2742 if (writedeleg && !NFSVNO_EXRDONLY(exp) &&
2743 (nfsrv_writedelegifpos || !readonly)) {
2744 new_deleg->ls_flags = (NFSLCK_DELEGWRITE |
2745 NFSLCK_READACCESS | NFSLCK_WRITEACCESS);
2746 *rflagsp |= NFSV4OPEN_WRITEDELEGATE;
2748 new_deleg->ls_flags = (NFSLCK_DELEGREAD |
2750 *rflagsp |= NFSV4OPEN_READDELEGATE;
2752 new_deleg->ls_uid = new_stp->ls_uid;
2753 new_deleg->ls_lfp = lfp;
2754 new_deleg->ls_clp = clp;
2755 new_deleg->ls_filerev = filerev;
2756 new_deleg->ls_compref = nd->nd_compref;
2757 LIST_INSERT_HEAD(&lfp->lf_deleg, new_deleg, ls_file);
2758 LIST_INSERT_HEAD(NFSSTATEHASH(clp,
2759 new_deleg->ls_stateid), new_deleg, ls_hash);
2760 LIST_INSERT_HEAD(&clp->lc_deleg, new_deleg, ls_list);
2762 newnfsstats.srvdelegates++;
2763 nfsrv_openpluslock++;
2764 nfsrv_delegatecnt++;
2769 * New owner case. Start the open_owner sequence with a
2770 * Needs confirmation (unless a reclaim) and hang the
2773 new_open->ls_stateid.seqid = 0;
2774 new_open->ls_stateid.other[0] = clp->lc_clientid.lval[0];
2775 new_open->ls_stateid.other[1] = clp->lc_clientid.lval[1];
2776 new_open->ls_stateid.other[2] = nfsrv_nextstateindex(clp);
2777 new_open->ls_flags = (new_stp->ls_flags & NFSLCK_SHAREBITS) |
2779 new_open->ls_uid = new_stp->ls_uid;
2780 LIST_INIT(&new_open->ls_open);
2781 new_open->ls_openowner = new_stp;
2782 new_open->ls_lfp = lfp;
2783 new_open->ls_clp = clp;
2784 LIST_INSERT_HEAD(&lfp->lf_open, new_open, ls_file);
2785 if (new_stp->ls_flags & NFSLCK_RECLAIM) {
2786 new_stp->ls_flags = 0;
2788 *rflagsp |= NFSV4OPEN_RESULTCONFIRM;
2789 new_stp->ls_flags = NFSLCK_NEEDSCONFIRM;
2791 nfsrvd_refcache(new_stp->ls_op);
2792 new_stp->ls_noopens = 0;
2793 LIST_INIT(&new_stp->ls_open);
2794 LIST_INSERT_HEAD(&new_stp->ls_open, new_open, ls_list);
2795 LIST_INSERT_HEAD(&clp->lc_open, new_stp, ls_list);
2796 LIST_INSERT_HEAD(NFSSTATEHASH(clp, new_open->ls_stateid),
2801 newnfsstats.srvopens++;
2802 nfsrv_openpluslock++;
2803 newnfsstats.srvopenowners++;
2804 nfsrv_openpluslock++;
2807 stateidp->seqid = openstp->ls_stateid.seqid;
2808 stateidp->other[0] = openstp->ls_stateid.other[0];
2809 stateidp->other[1] = openstp->ls_stateid.other[1];
2810 stateidp->other[2] = openstp->ls_stateid.other[2];
2814 NFSLOCKV4ROOTMUTEX();
2815 nfsv4_unlock(&nfsv4rootfs_lock, 1);
2816 NFSUNLOCKV4ROOTMUTEX();
2819 FREE((caddr_t)new_open, M_NFSDSTATE);
2821 FREE((caddr_t)new_deleg, M_NFSDSTATE);
2826 * Open update. Does the confirm, downgrade and close.
2829 nfsrv_openupdate(vnode_t vp, struct nfsstate *new_stp, nfsquad_t clientid,
2830 nfsv4stateid_t *stateidp, struct nfsrv_descript *nd, NFSPROC_T *p)
2832 struct nfsstate *stp, *ownerstp;
2833 struct nfsclient *clp;
2834 struct nfslockfile *lfp;
2836 int error, gotstate = 0, len = 0;
2837 u_char client[NFSV4_OPAQUELIMIT];
2840 * Check for restart conditions (client and server).
2842 error = nfsrv_checkrestart(clientid, new_stp->ls_flags,
2843 &new_stp->ls_stateid, 0);
2849 * Get the open structure via clientid and stateid.
2851 error = nfsrv_getclient(clientid, CLOPS_RENEW, &clp,
2852 (nfsquad_t)((u_quad_t)0), NULL, p);
2854 error = nfsrv_getstate(clp, &new_stp->ls_stateid,
2855 new_stp->ls_flags, &stp);
2858 * Sanity check the open.
2860 if (!error && (!(stp->ls_flags & NFSLCK_OPEN) ||
2861 (!(new_stp->ls_flags & NFSLCK_CONFIRM) &&
2862 (stp->ls_openowner->ls_flags & NFSLCK_NEEDSCONFIRM)) ||
2863 ((new_stp->ls_flags & NFSLCK_CONFIRM) &&
2864 (!(stp->ls_openowner->ls_flags & NFSLCK_NEEDSCONFIRM)))))
2865 error = NFSERR_BADSTATEID;
2868 error = nfsrv_checkseqid(nd, new_stp->ls_seq,
2869 stp->ls_openowner, new_stp->ls_op);
2870 if (!error && stp->ls_stateid.seqid != new_stp->ls_stateid.seqid &&
2871 !(new_stp->ls_flags & NFSLCK_CONFIRM))
2872 error = NFSERR_OLDSTATEID;
2873 if (!error && vnode_vtype(vp) != VREG) {
2874 if (vnode_vtype(vp) == VDIR)
2875 error = NFSERR_ISDIR;
2877 error = NFSERR_INVAL;
2882 * If a client tries to confirm an Open with a bad
2883 * seqid# and there are no byte range locks or other Opens
2884 * on the openowner, just throw it away, so the next use of the
2885 * openowner will start a fresh seq#.
2887 if (error == NFSERR_BADSEQID &&
2888 (new_stp->ls_flags & NFSLCK_CONFIRM) &&
2889 nfsrv_nootherstate(stp))
2890 nfsrv_freeopenowner(stp->ls_openowner, 0, p);
2896 * Set the return stateid.
2898 stateidp->seqid = stp->ls_stateid.seqid + 1;
2899 stateidp->other[0] = stp->ls_stateid.other[0];
2900 stateidp->other[1] = stp->ls_stateid.other[1];
2901 stateidp->other[2] = stp->ls_stateid.other[2];
2903 * Now, handle the three cases.
2905 if (new_stp->ls_flags & NFSLCK_CONFIRM) {
2907 * If the open doesn't need confirmation, it seems to me that
2908 * there is a client error, but I'll just log it and keep going?
2910 if (!(stp->ls_openowner->ls_flags & NFSLCK_NEEDSCONFIRM))
2911 printf("Nfsv4d: stray open confirm\n");
2912 stp->ls_openowner->ls_flags = 0;
2913 stp->ls_stateid.seqid++;
2914 if (!(clp->lc_flags & LCL_STAMPEDSTABLE)) {
2915 clp->lc_flags |= LCL_STAMPEDSTABLE;
2916 len = clp->lc_idlen;
2917 NFSBCOPY(clp->lc_id, client, len);
2921 } else if (new_stp->ls_flags & NFSLCK_CLOSE) {
2922 ownerstp = stp->ls_openowner;
2924 if (nfsrv_dolocallocks != 0 && !LIST_EMPTY(&stp->ls_open)) {
2925 /* Get the lf lock */
2928 if (nfsrv_freeopen(stp, vp, 1, p) == 0) {
2930 nfsrv_unlocklf(lfp);
2934 (void) nfsrv_freeopen(stp, NULL, 0, p);
2939 * Update the share bits, making sure that the new set are a
2940 * subset of the old ones.
2942 bits = (new_stp->ls_flags & NFSLCK_SHAREBITS);
2943 if (~(stp->ls_flags) & bits) {
2945 return (NFSERR_INVAL);
2947 stp->ls_flags = (bits | NFSLCK_OPEN);
2948 stp->ls_stateid.seqid++;
2953 * If the client just confirmed its first open, write a timestamp
2954 * to the stable storage file.
2957 nfsrv_writestable(client, len, NFSNST_NEWSTATE, p);
2962 * Delegation update. Does the purge and return.
2965 nfsrv_delegupdate(nfsquad_t clientid, nfsv4stateid_t *stateidp,
2966 vnode_t vp, int op, struct ucred *cred, NFSPROC_T *p)
2968 struct nfsstate *stp;
2969 struct nfsclient *clp;
2974 * Do a sanity check against the file handle for DelegReturn.
2977 error = nfsvno_getfh(vp, &fh, p);
2982 * Check for restart conditions (client and server).
2984 if (op == NFSV4OP_DELEGRETURN)
2985 error = nfsrv_checkrestart(clientid, NFSLCK_DELEGRETURN,
2988 error = nfsrv_checkrestart(clientid, NFSLCK_DELEGPURGE,
2993 * Get the open structure via clientid and stateid.
2996 error = nfsrv_getclient(clientid, CLOPS_RENEW, &clp,
2997 (nfsquad_t)((u_quad_t)0), NULL, p);
2999 if (error == NFSERR_CBPATHDOWN)
3001 if (error == NFSERR_STALECLIENTID && op == NFSV4OP_DELEGRETURN)
3002 error = NFSERR_STALESTATEID;
3004 if (!error && op == NFSV4OP_DELEGRETURN) {
3005 error = nfsrv_getstate(clp, stateidp, NFSLCK_DELEGRETURN, &stp);
3006 if (!error && stp->ls_stateid.seqid != stateidp->seqid)
3007 error = NFSERR_OLDSTATEID;
3010 * NFSERR_EXPIRED means that the state has gone away,
3011 * so Delegations have been purged. Just return ok.
3013 if (error == NFSERR_EXPIRED && op == NFSV4OP_DELEGPURGE) {
3022 if (op == NFSV4OP_DELEGRETURN) {
3023 if (NFSBCMP((caddr_t)&fh, (caddr_t)&stp->ls_lfp->lf_fh,
3024 sizeof (fhandle_t))) {
3026 return (NFSERR_BADSTATEID);
3028 nfsrv_freedeleg(stp);
3030 nfsrv_freedeleglist(&clp->lc_olddeleg);
3037 * Release lock owner.
3040 nfsrv_releaselckown(struct nfsstate *new_stp, nfsquad_t clientid,
3043 struct nfsstate *stp, *nstp, *openstp, *ownstp;
3044 struct nfsclient *clp;
3048 * Check for restart conditions (client and server).
3050 error = nfsrv_checkrestart(clientid, new_stp->ls_flags,
3051 &new_stp->ls_stateid, 0);
3057 * Get the lock owner by name.
3059 error = nfsrv_getclient(clientid, CLOPS_RENEW, &clp,
3060 (nfsquad_t)((u_quad_t)0), NULL, p);
3065 LIST_FOREACH(ownstp, &clp->lc_open, ls_list) {
3066 LIST_FOREACH(openstp, &ownstp->ls_open, ls_list) {
3067 stp = LIST_FIRST(&openstp->ls_open);
3068 while (stp != LIST_END(&openstp->ls_open)) {
3069 nstp = LIST_NEXT(stp, ls_list);
3071 * If the owner matches, check for locks and
3072 * then free or return an error.
3074 if (stp->ls_ownerlen == new_stp->ls_ownerlen &&
3075 !NFSBCMP(stp->ls_owner, new_stp->ls_owner,
3077 if (LIST_EMPTY(&stp->ls_lock)) {
3078 nfsrv_freelockowner(stp, NULL, 0, p);
3081 return (NFSERR_LOCKSHELD);
3093 * Get the file handle for a lock structure.
3096 nfsrv_getlockfh(vnode_t vp, u_short flags,
3097 struct nfslockfile **new_lfpp, fhandle_t *nfhp, NFSPROC_T *p)
3099 fhandle_t *fhp = NULL;
3100 struct nfslockfile *new_lfp;
3104 * For lock, use the new nfslock structure, otherwise just
3105 * a fhandle_t on the stack.
3107 if (flags & NFSLCK_OPEN) {
3108 new_lfp = *new_lfpp;
3109 fhp = &new_lfp->lf_fh;
3113 panic("nfsrv_getlockfh");
3115 error = nfsvno_getfh(vp, fhp, p);
3120 * Get an nfs lock structure. Allocate one, as required, and return a
3122 * Returns an NFSERR_xxx upon failure or -1 to indicate no current lock.
3125 nfsrv_getlockfile(u_short flags, struct nfslockfile **new_lfpp,
3126 struct nfslockfile **lfpp, fhandle_t *nfhp, int lockit)
3128 struct nfslockfile *lfp;
3129 fhandle_t *fhp = NULL, *tfhp;
3130 struct nfslockhashhead *hp;
3131 struct nfslockfile *new_lfp = NULL;
3134 * For lock, use the new nfslock structure, otherwise just
3135 * a fhandle_t on the stack.
3137 if (flags & NFSLCK_OPEN) {
3138 new_lfp = *new_lfpp;
3139 fhp = &new_lfp->lf_fh;
3143 panic("nfsrv_getlockfile");
3146 hp = NFSLOCKHASH(fhp);
3147 LIST_FOREACH(lfp, hp, lf_hash) {
3149 if (NFSVNO_CMPFH(fhp, tfhp)) {
3156 if (!(flags & NFSLCK_OPEN))
3160 * No match, so chain the new one into the list.
3162 LIST_INIT(&new_lfp->lf_open);
3163 LIST_INIT(&new_lfp->lf_lock);
3164 LIST_INIT(&new_lfp->lf_deleg);
3165 LIST_INIT(&new_lfp->lf_locallock);
3166 LIST_INIT(&new_lfp->lf_rollback);
3167 new_lfp->lf_locallock_lck.nfslock_usecnt = 0;
3168 new_lfp->lf_locallock_lck.nfslock_lock = 0;
3169 new_lfp->lf_usecount = 0;
3170 LIST_INSERT_HEAD(hp, new_lfp, lf_hash);
3177 * This function adds a nfslock lock structure to the list for the associated
3178 * nfsstate and nfslockfile structures. It will be inserted after the
3179 * entry pointed at by insert_lop.
3180 * Must be called with soft clock interrupts disabled.
3183 nfsrv_insertlock(struct nfslock *new_lop, struct nfslock *insert_lop,
3184 struct nfsstate *stp, struct nfslockfile *lfp)
3186 struct nfslock *lop, *nlop;
3188 new_lop->lo_stp = stp;
3189 new_lop->lo_lfp = lfp;
3192 /* Insert in increasing lo_first order */
3193 lop = LIST_FIRST(&lfp->lf_lock);
3194 if (lop == LIST_END(&lfp->lf_lock) ||
3195 new_lop->lo_first <= lop->lo_first) {
3196 LIST_INSERT_HEAD(&lfp->lf_lock, new_lop, lo_lckfile);
3198 nlop = LIST_NEXT(lop, lo_lckfile);
3199 while (nlop != LIST_END(&lfp->lf_lock) &&
3200 nlop->lo_first < new_lop->lo_first) {
3202 nlop = LIST_NEXT(lop, lo_lckfile);
3204 LIST_INSERT_AFTER(lop, new_lop, lo_lckfile);
3207 new_lop->lo_lckfile.le_prev = NULL; /* list not used */
3211 * Insert after insert_lop, which is overloaded as stp or lfp for
3214 if (stp == NULL && (struct nfslockfile *)insert_lop == lfp)
3215 LIST_INSERT_HEAD(&lfp->lf_locallock, new_lop, lo_lckowner);
3216 else if ((struct nfsstate *)insert_lop == stp)
3217 LIST_INSERT_HEAD(&stp->ls_lock, new_lop, lo_lckowner);
3219 LIST_INSERT_AFTER(insert_lop, new_lop, lo_lckowner);
3221 newnfsstats.srvlocks++;
3222 nfsrv_openpluslock++;
3227 * This function updates the locking for a lock owner and given file. It
3228 * maintains a list of lock ranges ordered on increasing file offset that
3229 * are NFSLCK_READ or NFSLCK_WRITE and non-overlapping (aka POSIX style).
3230 * It always adds new_lop to the list and sometimes uses the one pointed
3232 * Must be called with soft clock interrupts disabled.
3235 nfsrv_updatelock(struct nfsstate *stp, struct nfslock **new_lopp,
3236 struct nfslock **other_lopp, struct nfslockfile *lfp)
3238 struct nfslock *new_lop = *new_lopp;
3239 struct nfslock *lop, *tlop, *ilop;
3240 struct nfslock *other_lop = *other_lopp;
3241 int unlock = 0, myfile = 0;
3245 * Work down the list until the lock is merged.
3247 if (new_lop->lo_flags & NFSLCK_UNLOCK)
3250 ilop = (struct nfslock *)stp;
3251 lop = LIST_FIRST(&stp->ls_lock);
3253 ilop = (struct nfslock *)lfp;
3254 lop = LIST_FIRST(&lfp->lf_locallock);
3256 while (lop != NULL) {
3258 * Only check locks for this file that aren't before the start of
3261 if (lop->lo_lfp == lfp) {
3263 if (lop->lo_end >= new_lop->lo_first) {
3264 if (new_lop->lo_end < lop->lo_first) {
3266 * If the new lock ends before the start of the
3267 * current lock's range, no merge, just insert
3272 if (new_lop->lo_flags == lop->lo_flags ||
3273 (new_lop->lo_first <= lop->lo_first &&
3274 new_lop->lo_end >= lop->lo_end)) {
3276 * This lock can be absorbed by the new lock/unlock.
3277 * This happens when it covers the entire range
3278 * of the old lock or is contiguous
3279 * with the old lock and is of the same type or an
3282 if (lop->lo_first < new_lop->lo_first)
3283 new_lop->lo_first = lop->lo_first;
3284 if (lop->lo_end > new_lop->lo_end)
3285 new_lop->lo_end = lop->lo_end;
3287 lop = LIST_NEXT(lop, lo_lckowner);
3288 nfsrv_freenfslock(tlop);
3293 * All these cases are for contiguous locks that are not the
3294 * same type, so they can't be merged.
3296 if (new_lop->lo_first <= lop->lo_first) {
3298 * This case is where the new lock overlaps with the
3299 * first part of the old lock. Move the start of the
3300 * old lock to just past the end of the new lock. The
3301 * new lock will be inserted in front of the old, since
3302 * ilop hasn't been updated. (We are done now.)
3304 lop->lo_first = new_lop->lo_end;
3307 if (new_lop->lo_end >= lop->lo_end) {
3309 * This case is where the new lock overlaps with the
3310 * end of the old lock's range. Move the old lock's
3311 * end to just before the new lock's first and insert
3312 * the new lock after the old lock.
3313 * Might not be done yet, since the new lock could
3314 * overlap further locks with higher ranges.
3316 lop->lo_end = new_lop->lo_first;
3318 lop = LIST_NEXT(lop, lo_lckowner);
3322 * The final case is where the new lock's range is in the
3323 * middle of the current lock's and splits the current lock
3324 * up. Use *other_lopp to handle the second part of the
3325 * split old lock range. (We are done now.)
3326 * For unlock, we use new_lop as other_lop and tmp, since
3327 * other_lop and new_lop are the same for this case.
3328 * We noted the unlock case above, so we don't need
3329 * new_lop->lo_flags any longer.
3331 tmp = new_lop->lo_first;
3332 if (other_lop == NULL) {
3334 panic("nfsd srv update unlock");
3335 other_lop = new_lop;
3338 other_lop->lo_first = new_lop->lo_end;
3339 other_lop->lo_end = lop->lo_end;
3340 other_lop->lo_flags = lop->lo_flags;
3341 other_lop->lo_stp = stp;
3342 other_lop->lo_lfp = lfp;
3344 nfsrv_insertlock(other_lop, lop, stp, lfp);
3351 lop = LIST_NEXT(lop, lo_lckowner);
3352 if (myfile && (lop == NULL || lop->lo_lfp != lfp))
3357 * Insert the new lock in the list at the appropriate place.
3360 nfsrv_insertlock(new_lop, ilop, stp, lfp);
3366 * This function handles sequencing of locks, etc.
3367 * It returns an error that indicates what the caller should do.
3370 nfsrv_checkseqid(struct nfsrv_descript *nd, u_int32_t seqid,
3371 struct nfsstate *stp, struct nfsrvcache *op)
3374 if (op != nd->nd_rp)
3375 panic("nfsrvstate checkseqid");
3376 if (!(op->rc_flag & RC_INPROG))
3377 panic("nfsrvstate not inprog");
3378 if (stp->ls_op && stp->ls_op->rc_refcnt <= 0) {
3379 printf("refcnt=%d\n", stp->ls_op->rc_refcnt);
3380 panic("nfsrvstate op refcnt");
3382 if ((stp->ls_seq + 1) == seqid) {
3384 nfsrvd_derefcache(stp->ls_op);
3386 nfsrvd_refcache(op);
3387 stp->ls_seq = seqid;
3389 } else if (stp->ls_seq == seqid && stp->ls_op &&
3390 op->rc_xid == stp->ls_op->rc_xid &&
3391 op->rc_refcnt == 0 &&
3392 op->rc_reqlen == stp->ls_op->rc_reqlen &&
3393 op->rc_cksum == stp->ls_op->rc_cksum) {
3394 if (stp->ls_op->rc_flag & RC_INPROG)
3395 return (NFSERR_DONTREPLY);
3396 nd->nd_rp = stp->ls_op;
3397 nd->nd_rp->rc_flag |= RC_INPROG;
3398 nfsrvd_delcache(op);
3399 return (NFSERR_REPLYFROMCACHE);
3401 return (NFSERR_BADSEQID);
3405 * Get the client ip address for callbacks. If the strings can't be parsed,
3406 * just set lc_program to 0 to indicate no callbacks are possible.
3407 * (For cases where the address can't be parsed or is 0.0.0.0.0.0, set
3408 * the address to the client's transport address. This won't be used
3409 * for callbacks, but can be printed out by newnfsstats for info.)
3410 * Return error if the xdr can't be parsed, 0 otherwise.
3413 nfsrv_getclientipaddr(struct nfsrv_descript *nd, struct nfsclient *clp)
3418 struct sockaddr_in *rad, *sad;
3419 u_char protocol[5], addr[24];
3420 int error = 0, cantparse = 0;
3430 rad = NFSSOCKADDR(clp->lc_req.nr_nam, struct sockaddr_in *);
3431 rad->sin_family = AF_INET;
3432 rad->sin_len = sizeof (struct sockaddr_in);
3433 rad->sin_addr.s_addr = 0;
3435 clp->lc_req.nr_client = NULL;
3436 clp->lc_req.nr_lock = 0;
3437 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
3438 i = fxdr_unsigned(int, *tl);
3439 if (i >= 3 && i <= 4) {
3440 error = nfsrv_mtostr(nd, protocol, i);
3443 if (!strcmp(protocol, "tcp")) {
3444 clp->lc_flags |= LCL_TCPCALLBACK;
3445 clp->lc_req.nr_sotype = SOCK_STREAM;
3446 clp->lc_req.nr_soproto = IPPROTO_TCP;
3447 } else if (!strcmp(protocol, "udp")) {
3448 clp->lc_req.nr_sotype = SOCK_DGRAM;
3449 clp->lc_req.nr_soproto = IPPROTO_UDP;
3456 error = nfsm_advance(nd, NFSM_RNDUP(i), -1);
3461 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
3462 i = fxdr_unsigned(int, *tl);
3464 error = NFSERR_BADXDR;
3466 } else if (i == 0) {
3468 } else if (!cantparse && i <= 23 && i >= 11) {
3469 error = nfsrv_mtostr(nd, addr, i);
3474 * Parse out the address fields. We expect 6 decimal numbers
3475 * separated by '.'s.
3479 while (*cp && i < 6) {
3481 while (*cp2 && *cp2 != '.')
3489 j = nfsrv_getipnumber(cp);
3494 port.cval[5 - i] = j;
3503 if (ip.ival != 0x0) {
3504 rad->sin_addr.s_addr = htonl(ip.ival);
3505 rad->sin_port = htons(port.sval);
3513 error = nfsm_advance(nd, NFSM_RNDUP(i), -1);
3519 sad = NFSSOCKADDR(nd->nd_nam, struct sockaddr_in *);
3520 rad->sin_addr.s_addr = sad->sin_addr.s_addr;
3521 rad->sin_port = 0x0;
3522 clp->lc_program = 0;
3529 * Turn a string of up to three decimal digits into a number. Return -1 upon
3533 nfsrv_getipnumber(u_char *cp)
3538 if (j > 2 || *cp < '0' || *cp > '9')
3551 * This function checks for restart conditions.
3554 nfsrv_checkrestart(nfsquad_t clientid, u_int32_t flags,
3555 nfsv4stateid_t *stateidp, int specialid)
3560 * First check for a server restart. Open, LockT, ReleaseLockOwner
3561 * and DelegPurge have a clientid, the rest a stateid.
3564 (NFSLCK_OPEN | NFSLCK_TEST | NFSLCK_RELEASE | NFSLCK_DELEGPURGE)) {
3565 if (clientid.lval[0] != nfsrvboottime)
3566 return (NFSERR_STALECLIENTID);
3567 } else if (stateidp->other[0] != nfsrvboottime &&
3569 return (NFSERR_STALESTATEID);
3572 * Read, Write, Setattr and LockT can return NFSERR_GRACE and do
3573 * not use a lock/open owner seqid#, so the check can be done now.
3574 * (The others will be checked, as required, later.)
3576 if (!(flags & (NFSLCK_CHECK | NFSLCK_TEST)))
3580 ret = nfsrv_checkgrace(flags);
3589 nfsrv_checkgrace(u_int32_t flags)
3592 if (nfsrv_stablefirst.nsf_flags & NFSNSF_GRACEOVER) {
3593 if (flags & NFSLCK_RECLAIM)
3594 return (NFSERR_NOGRACE);
3596 if (!(flags & NFSLCK_RECLAIM))
3597 return (NFSERR_GRACE);
3600 * If grace is almost over and we are still getting Reclaims,
3601 * extend grace a bit.
3603 if ((NFSD_MONOSEC + NFSRV_LEASEDELTA) >
3604 nfsrv_stablefirst.nsf_eograce)
3605 nfsrv_stablefirst.nsf_eograce = NFSD_MONOSEC +
3612 * Do a server callback.
3615 nfsrv_docallback(struct nfsclient *clp, int procnum,
3616 nfsv4stateid_t *stateidp, int trunc, fhandle_t *fhp,
3617 struct nfsvattr *nap, nfsattrbit_t *attrbitp, NFSPROC_T *p)
3621 struct nfsrv_descript nfsd, *nd = &nfsd;
3626 cred = newnfs_getcred();
3627 NFSLOCKSTATE(); /* mostly for lc_cbref++ */
3628 if (clp->lc_flags & LCL_NEEDSCONFIRM) {
3635 * Fill the callback program# and version into the request
3636 * structure for newnfs_connect() to use.
3638 clp->lc_req.nr_prog = clp->lc_program;
3639 clp->lc_req.nr_vers = NFSV4_CBVERS;
3642 * First, fill in some of the fields of nd and cr.
3644 nd->nd_flag = ND_NFSV4;
3645 if (clp->lc_flags & LCL_GSS)
3646 nd->nd_flag |= ND_KERBV;
3648 cred->cr_uid = clp->lc_uid;
3649 cred->cr_gid = clp->lc_gid;
3650 callback = clp->lc_callback;
3652 cred->cr_ngroups = 1;
3655 * Get the first mbuf for the request.
3657 MGET(m, M_WAIT, MT_DATA);
3659 nd->nd_mreq = nd->nd_mb = m;
3660 nd->nd_bpos = NFSMTOD(m, caddr_t);
3663 * and build the callback request.
3665 if (procnum == NFSV4OP_CBGETATTR) {
3666 nd->nd_procnum = NFSV4PROC_CBCOMPOUND;
3667 (void) nfsm_strtom(nd, "CB Getattr", 10);
3668 NFSM_BUILD(tl, u_int32_t *, 4 * NFSX_UNSIGNED);
3669 *tl++ = txdr_unsigned(NFSV4_MINORVERSION);
3670 *tl++ = txdr_unsigned(callback);
3671 *tl++ = txdr_unsigned(1);
3672 *tl = txdr_unsigned(NFSV4OP_CBGETATTR);
3673 (void) nfsm_fhtom(nd, (u_int8_t *)fhp, NFSX_MYFH, 0);
3674 (void) nfsrv_putattrbit(nd, attrbitp);
3675 } else if (procnum == NFSV4OP_CBRECALL) {
3676 nd->nd_procnum = NFSV4PROC_CBCOMPOUND;
3677 (void) nfsm_strtom(nd, "CB Recall", 9);
3678 NFSM_BUILD(tl, u_int32_t *, 5 * NFSX_UNSIGNED + NFSX_STATEID);
3679 *tl++ = txdr_unsigned(NFSV4_MINORVERSION);
3680 *tl++ = txdr_unsigned(callback);
3681 *tl++ = txdr_unsigned(1);
3682 *tl++ = txdr_unsigned(NFSV4OP_CBRECALL);
3683 *tl++ = txdr_unsigned(stateidp->seqid);
3684 NFSBCOPY((caddr_t)stateidp->other, (caddr_t)tl,
3686 tl += (NFSX_STATEIDOTHER / NFSX_UNSIGNED);
3691 (void) nfsm_fhtom(nd, (u_int8_t *)fhp, NFSX_MYFH, 0);
3693 nd->nd_procnum = NFSV4PROC_CBNULL;
3697 * Call newnfs_connect(), as required, and then newnfs_request().
3699 (void) newnfs_sndlock(&clp->lc_req.nr_lock);
3700 if (clp->lc_req.nr_client == NULL) {
3701 if (nd->nd_procnum == NFSV4PROC_CBNULL)
3702 error = newnfs_connect(NULL, &clp->lc_req, cred,
3705 error = newnfs_connect(NULL, &clp->lc_req, cred,
3708 newnfs_sndunlock(&clp->lc_req.nr_lock);
3710 error = newnfs_request(nd, NULL, clp, &clp->lc_req, NULL,
3711 NULL, cred, clp->lc_program, NFSV4_CBVERS, NULL, 1, NULL);
3716 * If error is set here, the Callback path isn't working
3717 * properly, so twiddle the appropriate LCL_ flags.
3718 * (nd_repstat != 0 indicates the Callback path is working,
3719 * but the callback failed on the client.)
3723 * Mark the callback pathway down, which disabled issuing
3724 * of delegations and gets Renew to return NFSERR_CBPATHDOWN.
3727 clp->lc_flags |= LCL_CBDOWN;
3731 * Callback worked. If the callback path was down, disable
3732 * callbacks, so no more delegations will be issued. (This
3733 * is done on the assumption that the callback pathway is
3737 if (clp->lc_flags & LCL_CBDOWN)
3738 clp->lc_flags &= ~(LCL_CBDOWN | LCL_CALLBACKSON);
3741 error = nd->nd_repstat;
3742 else if (procnum == NFSV4OP_CBGETATTR)
3743 error = nfsv4_loadattr(nd, NULL, nap, NULL, NULL, 0,
3744 NULL, NULL, NULL, NULL, NULL, 0, NULL, NULL, NULL,
3746 mbuf_freem(nd->nd_mrep);
3750 if ((clp->lc_flags & LCL_WAKEUPWANTED) && clp->lc_cbref == 0) {
3751 clp->lc_flags &= ~LCL_WAKEUPWANTED;
3753 wakeup((caddr_t)clp);
3761 * Return the next index# for a clientid. Mostly just increment and return
3762 * the next one, but... if the 32bit unsigned does actually wrap around,
3763 * it should be rebooted.
3764 * At an average rate of one new client per second, it will wrap around in
3765 * approximately 136 years. (I think the server will have been shut
3766 * down or rebooted before then.)
3769 nfsrv_nextclientindex(void)
3771 static u_int32_t client_index = 0;
3774 if (client_index != 0)
3775 return (client_index);
3777 printf("%s: out of clientids\n", __func__);
3778 return (client_index);
3782 * Return the next index# for a stateid. Mostly just increment and return
3783 * the next one, but... if the 32bit unsigned does actually wrap around
3784 * (will a BSD server stay up that long?), find
3785 * new start and end values.
3788 nfsrv_nextstateindex(struct nfsclient *clp)
3790 struct nfsstate *stp;
3792 u_int32_t canuse, min_index, max_index;
3794 if (!(clp->lc_flags & LCL_INDEXNOTOK)) {
3795 clp->lc_stateindex++;
3796 if (clp->lc_stateindex != clp->lc_statemaxindex)
3797 return (clp->lc_stateindex);
3801 * Yuck, we've hit the end.
3802 * Look for a new min and max.
3805 max_index = 0xffffffff;
3806 for (i = 0; i < NFSSTATEHASHSIZE; i++) {
3807 LIST_FOREACH(stp, &clp->lc_stateid[i], ls_hash) {
3808 if (stp->ls_stateid.other[2] > 0x80000000) {
3809 if (stp->ls_stateid.other[2] < max_index)
3810 max_index = stp->ls_stateid.other[2];
3812 if (stp->ls_stateid.other[2] > min_index)
3813 min_index = stp->ls_stateid.other[2];
3819 * Yikes, highly unlikely, but I'll handle it anyhow.
3821 if (min_index == 0x80000000 && max_index == 0x80000001) {
3824 * Loop around until we find an unused entry. Return that
3825 * and set LCL_INDEXNOTOK, so the search will continue next time.
3826 * (This is one of those rare cases where a goto is the
3827 * cleanest way to code the loop.)
3830 for (i = 0; i < NFSSTATEHASHSIZE; i++) {
3831 LIST_FOREACH(stp, &clp->lc_stateid[i], ls_hash) {
3832 if (stp->ls_stateid.other[2] == canuse) {
3838 clp->lc_flags |= LCL_INDEXNOTOK;
3843 * Ok to start again from min + 1.
3845 clp->lc_stateindex = min_index + 1;
3846 clp->lc_statemaxindex = max_index;
3847 clp->lc_flags &= ~LCL_INDEXNOTOK;
3848 return (clp->lc_stateindex);
3852 * The following functions handle the stable storage file that deals with
3853 * the edge conditions described in RFC3530 Sec. 8.6.3.
3854 * The file is as follows:
3855 * - a single record at the beginning that has the lease time of the
3856 * previous server instance (before the last reboot) and the nfsrvboottime
3857 * values for the previous server boots.
3858 * These previous boot times are used to ensure that the current
3859 * nfsrvboottime does not, somehow, get set to a previous one.
3860 * (This is important so that Stale ClientIDs and StateIDs can
3862 * The number of previous nfsvrboottime values preceeds the list.
3863 * - followed by some number of appended records with:
3864 * - client id string
3865 * - flag that indicates it is a record revoking state via lease
3866 * expiration or similar
3867 * OR has successfully acquired state.
3868 * These structures vary in length, with the client string at the end, up
3869 * to NFSV4_OPAQUELIMIT in size.
3871 * At the end of the grace period, the file is truncated, the first
3872 * record is rewritten with updated information and any acquired state
3873 * records for successful reclaims of state are written.
3875 * Subsequent records are appended when the first state is issued to
3876 * a client and when state is revoked for a client.
3878 * When reading the file in, state issued records that come later in
3879 * the file override older ones, since the append log is in cronological order.
3880 * If, for some reason, the file can't be read, the grace period is
3881 * immediately terminated and all reclaims get NFSERR_NOGRACE.
3885 * Read in the stable storage file. Called by nfssvc() before the nfsd
3886 * processes start servicing requests.
3889 nfsrv_setupstable(NFSPROC_T *p)
3891 struct nfsrv_stablefirst *sf = &nfsrv_stablefirst;
3892 struct nfsrv_stable *sp, *nsp;
3893 struct nfst_rec *tsp;
3894 int error, i, tryagain;
3897 struct timeval curtime;
3900 * If NFSNSF_UPDATEDONE is set, this is a restart of the nfsds without
3901 * a reboot, so state has not been lost.
3903 if (sf->nsf_flags & NFSNSF_UPDATEDONE)
3906 * Set Grace over just until the file reads successfully.
3908 NFSGETTIME(&curtime);
3909 nfsrvboottime = curtime.tv_sec;
3910 LIST_INIT(&sf->nsf_head);
3911 sf->nsf_flags = (NFSNSF_GRACEOVER | NFSNSF_NEEDLOCK);
3912 sf->nsf_eograce = NFSD_MONOSEC + NFSRV_LEASEDELTA;
3913 if (sf->nsf_fp == NULL)
3915 error = NFSD_RDWR(UIO_READ, NFSFPVNODE(sf->nsf_fp),
3916 (caddr_t)&sf->nsf_rec, sizeof (struct nfsf_rec), off, UIO_SYSSPACE,
3917 0, NFSFPCRED(sf->nsf_fp), &aresid, p);
3918 if (error || aresid || sf->nsf_numboots == 0 ||
3919 sf->nsf_numboots > NFSNSF_MAXNUMBOOTS)
3923 * Now, read in the boottimes.
3925 sf->nsf_bootvals = (time_t *)malloc((sf->nsf_numboots + 1) *
3926 sizeof (time_t), M_TEMP, M_WAITOK);
3927 off = sizeof (struct nfsf_rec);
3928 error = NFSD_RDWR(UIO_READ, NFSFPVNODE(sf->nsf_fp),
3929 (caddr_t)sf->nsf_bootvals, sf->nsf_numboots * sizeof (time_t), off,
3930 UIO_SYSSPACE, 0, NFSFPCRED(sf->nsf_fp), &aresid, p);
3931 if (error || aresid) {
3932 free((caddr_t)sf->nsf_bootvals, M_TEMP);
3933 sf->nsf_bootvals = NULL;
3938 * Make sure this nfsrvboottime is different from all recorded
3943 for (i = 0; i < sf->nsf_numboots; i++) {
3944 if (nfsrvboottime == sf->nsf_bootvals[i]) {
3952 sf->nsf_flags |= NFSNSF_OK;
3953 off += (sf->nsf_numboots * sizeof (time_t));
3956 * Read through the file, building a list of records for grace
3958 * Each record is between sizeof (struct nfst_rec) and
3959 * sizeof (struct nfst_rec) + NFSV4_OPAQUELIMIT - 1
3960 * and is actually sizeof (struct nfst_rec) + nst_len - 1.
3962 tsp = (struct nfst_rec *)malloc(sizeof (struct nfst_rec) +
3963 NFSV4_OPAQUELIMIT - 1, M_TEMP, M_WAITOK);
3965 error = NFSD_RDWR(UIO_READ, NFSFPVNODE(sf->nsf_fp),
3966 (caddr_t)tsp, sizeof (struct nfst_rec) + NFSV4_OPAQUELIMIT - 1,
3967 off, UIO_SYSSPACE, 0, NFSFPCRED(sf->nsf_fp), &aresid, p);
3968 len = (sizeof (struct nfst_rec) + NFSV4_OPAQUELIMIT - 1) - aresid;
3969 if (error || (len > 0 && (len < sizeof (struct nfst_rec) ||
3970 len < (sizeof (struct nfst_rec) + tsp->len - 1)))) {
3972 * Yuck, the file has been corrupted, so just return
3973 * after clearing out any restart state, so the grace period
3976 LIST_FOREACH_SAFE(sp, &sf->nsf_head, nst_list, nsp) {
3977 LIST_REMOVE(sp, nst_list);
3978 free((caddr_t)sp, M_TEMP);
3980 free((caddr_t)tsp, M_TEMP);
3981 sf->nsf_flags &= ~NFSNSF_OK;
3982 free((caddr_t)sf->nsf_bootvals, M_TEMP);
3983 sf->nsf_bootvals = NULL;
3987 off += sizeof (struct nfst_rec) + tsp->len - 1;
3989 * Search the list for a matching client.
3991 LIST_FOREACH(sp, &sf->nsf_head, nst_list) {
3992 if (tsp->len == sp->nst_len &&
3993 !NFSBCMP(tsp->client, sp->nst_client, tsp->len))
3996 if (sp == LIST_END(&sf->nsf_head)) {
3997 sp = (struct nfsrv_stable *)malloc(tsp->len +
3998 sizeof (struct nfsrv_stable) - 1, M_TEMP,
4000 NFSBCOPY((caddr_t)tsp, (caddr_t)&sp->nst_rec,
4001 sizeof (struct nfst_rec) + tsp->len - 1);
4002 LIST_INSERT_HEAD(&sf->nsf_head, sp, nst_list);
4004 if (tsp->flag == NFSNST_REVOKE)
4005 sp->nst_flag |= NFSNST_REVOKE;
4008 * A subsequent timestamp indicates the client
4009 * did a setclientid/confirm and any previous
4010 * revoke is no longer relevant.
4012 sp->nst_flag &= ~NFSNST_REVOKE;
4016 free((caddr_t)tsp, M_TEMP);
4017 sf->nsf_flags = NFSNSF_OK;
4018 sf->nsf_eograce = NFSD_MONOSEC + sf->nsf_lease +
4023 * Update the stable storage file, now that the grace period is over.
4026 nfsrv_updatestable(NFSPROC_T *p)
4028 struct nfsrv_stablefirst *sf = &nfsrv_stablefirst;
4029 struct nfsrv_stable *sp, *nsp;
4031 struct nfsvattr nva;
4033 #if defined(__FreeBSD_version) && (__FreeBSD_version >= 500000)
4038 if (sf->nsf_fp == NULL || (sf->nsf_flags & NFSNSF_UPDATEDONE))
4040 sf->nsf_flags |= NFSNSF_UPDATEDONE;
4042 * Ok, we need to rewrite the stable storage file.
4043 * - truncate to 0 length
4044 * - write the new first structure
4045 * - loop through the data structures, writing out any that
4046 * have timestamps older than the old boot
4048 if (sf->nsf_bootvals) {
4050 for (i = sf->nsf_numboots - 2; i >= 0; i--)
4051 sf->nsf_bootvals[i + 1] = sf->nsf_bootvals[i];
4053 sf->nsf_numboots = 1;
4054 sf->nsf_bootvals = (time_t *)malloc(sizeof (time_t),
4057 sf->nsf_bootvals[0] = nfsrvboottime;
4058 sf->nsf_lease = nfsrv_lease;
4059 NFSVNO_ATTRINIT(&nva);
4060 NFSVNO_SETATTRVAL(&nva, size, 0);
4061 vp = NFSFPVNODE(sf->nsf_fp);
4062 NFS_STARTWRITE(vp, &mp);
4063 NFSVOPLOCK(vp, LK_EXCLUSIVE | LK_RETRY, p);
4064 error = nfsvno_setattr(vp, &nva, NFSFPCRED(sf->nsf_fp), p, NULL);
4066 NFSVOPUNLOCK(vp, 0, p);
4068 error = NFSD_RDWR(UIO_WRITE, vp,
4069 (caddr_t)&sf->nsf_rec, sizeof (struct nfsf_rec), (off_t)0,
4070 UIO_SYSSPACE, IO_SYNC, NFSFPCRED(sf->nsf_fp), NULL, p);
4072 error = NFSD_RDWR(UIO_WRITE, vp,
4073 (caddr_t)sf->nsf_bootvals,
4074 sf->nsf_numboots * sizeof (time_t),
4075 (off_t)(sizeof (struct nfsf_rec)),
4076 UIO_SYSSPACE, IO_SYNC, NFSFPCRED(sf->nsf_fp), NULL, p);
4077 free((caddr_t)sf->nsf_bootvals, M_TEMP);
4078 sf->nsf_bootvals = NULL;
4080 sf->nsf_flags &= ~NFSNSF_OK;
4081 printf("EEK! Can't write NfsV4 stable storage file\n");
4084 sf->nsf_flags |= NFSNSF_OK;
4087 * Loop through the list and write out timestamp records for
4088 * any clients that successfully reclaimed state.
4090 LIST_FOREACH_SAFE(sp, &sf->nsf_head, nst_list, nsp) {
4091 if (sp->nst_flag & NFSNST_GOTSTATE) {
4092 nfsrv_writestable(sp->nst_client, sp->nst_len,
4093 NFSNST_NEWSTATE, p);
4094 sp->nst_clp->lc_flags |= LCL_STAMPEDSTABLE;
4096 LIST_REMOVE(sp, nst_list);
4097 free((caddr_t)sp, M_TEMP);
4102 * Append a record to the stable storage file.
4105 nfsrv_writestable(u_char *client, int len, int flag, NFSPROC_T *p)
4107 struct nfsrv_stablefirst *sf = &nfsrv_stablefirst;
4108 struct nfst_rec *sp;
4111 if (!(sf->nsf_flags & NFSNSF_OK) || sf->nsf_fp == NULL)
4113 sp = (struct nfst_rec *)malloc(sizeof (struct nfst_rec) +
4114 len - 1, M_TEMP, M_WAITOK);
4116 NFSBCOPY(client, sp->client, len);
4118 error = NFSD_RDWR(UIO_WRITE, NFSFPVNODE(sf->nsf_fp),
4119 (caddr_t)sp, sizeof (struct nfst_rec) + len - 1, (off_t)0,
4120 UIO_SYSSPACE, (IO_SYNC | IO_APPEND), NFSFPCRED(sf->nsf_fp), NULL, p);
4121 free((caddr_t)sp, M_TEMP);
4123 sf->nsf_flags &= ~NFSNSF_OK;
4124 printf("EEK! Can't write NfsV4 stable storage file\n");
4129 * This function is called during the grace period to mark a client
4130 * that successfully reclaimed state.
4133 nfsrv_markstable(struct nfsclient *clp)
4135 struct nfsrv_stable *sp;
4138 * First find the client structure.
4140 LIST_FOREACH(sp, &nfsrv_stablefirst.nsf_head, nst_list) {
4141 if (sp->nst_len == clp->lc_idlen &&
4142 !NFSBCMP(sp->nst_client, clp->lc_id, sp->nst_len))
4145 if (sp == LIST_END(&nfsrv_stablefirst.nsf_head))
4149 * Now, just mark it and set the nfsclient back pointer.
4151 sp->nst_flag |= NFSNST_GOTSTATE;
4156 * This function is called for a reclaim, to see if it gets grace.
4157 * It returns 0 if a reclaim is allowed, 1 otherwise.
4160 nfsrv_checkstable(struct nfsclient *clp)
4162 struct nfsrv_stable *sp;
4165 * First, find the entry for the client.
4167 LIST_FOREACH(sp, &nfsrv_stablefirst.nsf_head, nst_list) {
4168 if (sp->nst_len == clp->lc_idlen &&
4169 !NFSBCMP(sp->nst_client, clp->lc_id, sp->nst_len))
4174 * If not in the list, state was revoked or no state was issued
4175 * since the previous reboot, a reclaim is denied.
4177 if (sp == LIST_END(&nfsrv_stablefirst.nsf_head) ||
4178 (sp->nst_flag & NFSNST_REVOKE) ||
4179 !(nfsrv_stablefirst.nsf_flags & NFSNSF_OK))
4185 * Test for and try to clear out a conflicting client. This is called by
4186 * nfsrv_lockctrl() and nfsrv_openctrl() when conflicts with other clients
4188 * The trick here is that it can't revoke a conflicting client with an
4189 * expired lease unless it holds the v4root lock, so...
4190 * If no v4root lock, get the lock and return 1 to indicate "try again".
4191 * Return 0 to indicate the conflict can't be revoked and 1 to indicate
4192 * the revocation worked and the conflicting client is "bye, bye", so it
4193 * can be tried again.
4194 * Unlocks State before a non-zero value is returned.
4197 nfsrv_clientconflict(struct nfsclient *clp, int *haslockp, __unused vnode_t vp,
4203 * If lease hasn't expired, we can't fix it.
4205 if (clp->lc_expiry >= NFSD_MONOSEC ||
4206 !(nfsrv_stablefirst.nsf_flags & NFSNSF_UPDATEDONE))
4208 if (*haslockp == 0) {
4210 NFSVOPUNLOCK(vp, 0, p);
4211 NFSLOCKV4ROOTMUTEX();
4212 nfsv4_relref(&nfsv4rootfs_lock);
4214 gotlock = nfsv4_lock(&nfsv4rootfs_lock, 1, NULL,
4215 NFSV4ROOTLOCKMUTEXPTR);
4217 NFSUNLOCKV4ROOTMUTEX();
4218 NFSLOCKSTATE(); /* to avoid a race with */
4219 NFSUNLOCKSTATE(); /* nfsrv_servertimer() */
4221 NFSVOPLOCK(vp, LK_EXCLUSIVE | LK_RETRY, p);
4227 * Ok, we can expire the conflicting client.
4229 nfsrv_writestable(clp->lc_id, clp->lc_idlen, NFSNST_REVOKE, p);
4230 nfsrv_cleanclient(clp, p);
4231 nfsrv_freedeleglist(&clp->lc_deleg);
4232 nfsrv_freedeleglist(&clp->lc_olddeleg);
4233 LIST_REMOVE(clp, lc_hash);
4234 nfsrv_zapclient(clp, p);
4240 * Resolve a delegation conflict.
4241 * Returns 0 to indicate the conflict was resolved without sleeping.
4242 * Return -1 to indicate that the caller should check for conflicts again.
4243 * Return > 0 for an error that should be returned, normally NFSERR_DELAY.
4245 * Also, manipulate the nfsv4root_lock, as required. It isn't changed
4246 * for a return of 0, since there was no sleep and it could be required
4247 * later. It is released for a return of NFSERR_DELAY, since the caller
4248 * will return that error. It is released when a sleep was done waiting
4249 * for the delegation to be returned or expire (so that other nfsds can
4250 * handle ops). Then, it must be acquired for the write to stable storage.
4251 * (This function is somewhat similar to nfsrv_clientconflict(), but
4252 * the semantics differ in a couple of subtle ways. The return of 0
4253 * indicates the conflict was resolved without sleeping here, not
4254 * that the conflict can't be resolved and the handling of nfsv4root_lock
4255 * differs, as noted above.)
4256 * Unlocks State before returning a non-zero value.
4259 nfsrv_delegconflict(struct nfsstate *stp, int *haslockp, NFSPROC_T *p,
4260 __unused vnode_t vp)
4262 struct nfsclient *clp = stp->ls_clp;
4263 int gotlock, error, retrycnt, zapped_clp;
4264 nfsv4stateid_t tstateid;
4268 * If the conflict is with an old delegation...
4270 if (stp->ls_flags & NFSLCK_OLDDELEG) {
4272 * You can delete it, if it has expired.
4274 if (clp->lc_delegtime < NFSD_MONOSEC) {
4275 nfsrv_freedeleg(stp);
4281 * During this delay, the old delegation could expire or it
4282 * could be recovered by the client via an Open with
4283 * CLAIM_DELEGATE_PREV.
4284 * Release the nfsv4root_lock, if held.
4288 NFSLOCKV4ROOTMUTEX();
4289 nfsv4_unlock(&nfsv4rootfs_lock, 1);
4290 NFSUNLOCKV4ROOTMUTEX();
4292 return (NFSERR_DELAY);
4296 * It's a current delegation, so:
4297 * - check to see if the delegation has expired
4298 * - if so, get the v4root lock and then expire it
4300 if (!(stp->ls_flags & NFSLCK_DELEGRECALL)) {
4302 * - do a recall callback, since not yet done
4303 * For now, never allow truncate to be set. To use
4304 * truncate safely, it must be guaranteed that the
4305 * Remove, Rename or Setattr with size of 0 will
4306 * succeed and that would require major changes to
4307 * the VFS/Vnode OPs.
4308 * Set the expiry time large enough so that it won't expire
4309 * until after the callback, then set it correctly, once
4310 * the callback is done. (The delegation will now time
4311 * out whether or not the Recall worked ok. The timeout
4312 * will be extended when ops are done on the delegation
4313 * stateid, up to the timelimit.)
4315 stp->ls_delegtime = NFSD_MONOSEC + (2 * nfsrv_lease) +
4317 stp->ls_delegtimelimit = NFSD_MONOSEC + (6 * nfsrv_lease) +
4319 stp->ls_flags |= NFSLCK_DELEGRECALL;
4322 * Loop NFSRV_CBRETRYCNT times while the CBRecall replies
4323 * NFSERR_BADSTATEID or NFSERR_BADHANDLE. This is done
4324 * in order to try and avoid a race that could happen
4325 * when a CBRecall request passed the Open reply with
4326 * the delegation in it when transitting the network.
4327 * Since nfsrv_docallback will sleep, don't use stp after
4330 NFSBCOPY((caddr_t)&stp->ls_stateid, (caddr_t)&tstateid,
4332 NFSBCOPY((caddr_t)&stp->ls_lfp->lf_fh, (caddr_t)&tfh,
4337 NFSLOCKV4ROOTMUTEX();
4338 nfsv4_unlock(&nfsv4rootfs_lock, 1);
4339 NFSUNLOCKV4ROOTMUTEX();
4343 error = nfsrv_docallback(clp, NFSV4OP_CBRECALL,
4344 &tstateid, 0, &tfh, NULL, NULL, p);
4346 } while ((error == NFSERR_BADSTATEID ||
4347 error == NFSERR_BADHANDLE) && retrycnt < NFSV4_CBRETRYCNT);
4348 return (NFSERR_DELAY);
4351 if (clp->lc_expiry >= NFSD_MONOSEC &&
4352 stp->ls_delegtime >= NFSD_MONOSEC) {
4355 * A recall has been done, but it has not yet expired.
4360 NFSLOCKV4ROOTMUTEX();
4361 nfsv4_unlock(&nfsv4rootfs_lock, 1);
4362 NFSUNLOCKV4ROOTMUTEX();
4364 return (NFSERR_DELAY);
4368 * If we don't yet have the lock, just get it and then return,
4369 * since we need that before deleting expired state, such as
4371 * When getting the lock, unlock the vnode, so other nfsds that
4372 * are in progress, won't get stuck waiting for the vnode lock.
4374 if (*haslockp == 0) {
4376 NFSVOPUNLOCK(vp, 0, p);
4377 NFSLOCKV4ROOTMUTEX();
4378 nfsv4_relref(&nfsv4rootfs_lock);
4380 gotlock = nfsv4_lock(&nfsv4rootfs_lock, 1, NULL,
4381 NFSV4ROOTLOCKMUTEXPTR);
4383 NFSUNLOCKV4ROOTMUTEX();
4384 NFSLOCKSTATE(); /* to avoid a race with */
4385 NFSUNLOCKSTATE(); /* nfsrv_servertimer() */
4387 NFSVOPLOCK(vp, LK_EXCLUSIVE | LK_RETRY, p);
4393 * Ok, we can delete the expired delegation.
4394 * First, write the Revoke record to stable storage and then
4395 * clear out the conflict.
4396 * Since all other nfsd threads are now blocked, we can safely
4397 * sleep without the state changing.
4399 nfsrv_writestable(clp->lc_id, clp->lc_idlen, NFSNST_REVOKE, p);
4400 if (clp->lc_expiry < NFSD_MONOSEC) {
4401 nfsrv_cleanclient(clp, p);
4402 nfsrv_freedeleglist(&clp->lc_deleg);
4403 nfsrv_freedeleglist(&clp->lc_olddeleg);
4404 LIST_REMOVE(clp, lc_hash);
4407 nfsrv_freedeleg(stp);
4411 nfsrv_zapclient(clp, p);
4416 * Check for a remove allowed, if remove is set to 1 and get rid of
4420 nfsrv_checkremove(vnode_t vp, int remove, NFSPROC_T *p)
4422 struct nfsstate *stp;
4423 struct nfslockfile *lfp;
4424 int error, haslock = 0;
4428 * First, get the lock file structure.
4429 * (A return of -1 means no associated state, so remove ok.)
4431 error = nfsrv_getlockfh(vp, NFSLCK_CHECK, NULL, &nfh, p);
4435 error = nfsrv_getlockfile(NFSLCK_CHECK, NULL, &lfp, &nfh, 0);
4439 NFSLOCKV4ROOTMUTEX();
4440 nfsv4_unlock(&nfsv4rootfs_lock, 1);
4441 NFSUNLOCKV4ROOTMUTEX();
4449 * Now, we must Recall any delegations.
4451 error = nfsrv_cleandeleg(vp, lfp, NULL, &haslock, p);
4454 * nfsrv_cleandeleg() unlocks state for non-zero
4460 NFSLOCKV4ROOTMUTEX();
4461 nfsv4_unlock(&nfsv4rootfs_lock, 1);
4462 NFSUNLOCKV4ROOTMUTEX();
4468 * Now, look for a conflicting open share.
4471 LIST_FOREACH(stp, &lfp->lf_open, ls_file) {
4472 if (stp->ls_flags & NFSLCK_WRITEDENY) {
4473 error = NFSERR_FILEOPEN;
4481 NFSLOCKV4ROOTMUTEX();
4482 nfsv4_unlock(&nfsv4rootfs_lock, 1);
4483 NFSUNLOCKV4ROOTMUTEX();
4489 * Clear out all delegations for the file referred to by lfp.
4490 * May return NFSERR_DELAY, if there will be a delay waiting for
4491 * delegations to expire.
4492 * Returns -1 to indicate it slept while recalling a delegation.
4493 * This function has the side effect of deleting the nfslockfile structure,
4494 * if it no longer has associated state and didn't have to sleep.
4495 * Unlocks State before a non-zero value is returned.
4498 nfsrv_cleandeleg(vnode_t vp, struct nfslockfile *lfp,
4499 struct nfsclient *clp, int *haslockp, NFSPROC_T *p)
4501 struct nfsstate *stp, *nstp;
4504 stp = LIST_FIRST(&lfp->lf_deleg);
4505 while (stp != LIST_END(&lfp->lf_deleg)) {
4506 nstp = LIST_NEXT(stp, ls_file);
4507 if (stp->ls_clp != clp) {
4508 ret = nfsrv_delegconflict(stp, haslockp, p, vp);
4511 * nfsrv_delegconflict() unlocks state
4512 * when it returns non-zero.
4523 * There are certain operations that, when being done outside of NFSv4,
4524 * require that any NFSv4 delegation for the file be recalled.
4525 * This function is to be called for those cases:
4526 * VOP_RENAME() - When a delegation is being recalled for any reason,
4527 * the client may have to do Opens against the server, using the file's
4528 * final component name. If the file has been renamed on the server,
4529 * that component name will be incorrect and the Open will fail.
4530 * VOP_REMOVE() - Theoretically, a client could Open a file after it has
4531 * been removed on the server, if there is a delegation issued to
4532 * that client for the file. I say "theoretically" since clients
4533 * normally do an Access Op before the Open and that Access Op will
4534 * fail with ESTALE. Note that NFSv2 and 3 don't even do Opens, so
4535 * they will detect the file's removal in the same manner. (There is
4536 * one case where RFC3530 allows a client to do an Open without first
4537 * doing an Access Op, which is passage of a check against the ACE
4538 * returned with a Write delegation, but current practice is to ignore
4539 * the ACE and always do an Access Op.)
4540 * Since the functions can only be called with an unlocked vnode, this
4541 * can't be done at this time.
4542 * VOP_ADVLOCK() - When a client holds a delegation, it can issue byte range
4543 * locks locally in the client, which are not visible to the server. To
4544 * deal with this, issuing of delegations for a vnode must be disabled
4545 * and all delegations for the vnode recalled. This is done via the
4546 * second function, using the VV_DISABLEDELEG vflag on the vnode.
4549 nfsd_recalldelegation(vnode_t vp, NFSPROC_T *p)
4551 struct timespec mytime;
4555 KASSERT(!VOP_ISLOCKED(vp), ("vp %p is locked", vp));
4558 * First, check to see if the server is currently running and it has
4559 * been called for a regular file when issuing delegations.
4561 if (newnfs_numnfsd == 0 || vp->v_type != VREG ||
4562 nfsrv_issuedelegs == 0)
4566 * Now, call nfsrv_checkremove() in a loop while it returns
4567 * NFSERR_DELAY. Return upon any other error or when timed out.
4569 NFSGETNANOTIME(&mytime);
4570 starttime = (u_int32_t)mytime.tv_sec;
4572 error = nfsrv_checkremove(vp, 0, p);
4573 if (error == NFSERR_DELAY) {
4574 NFSGETNANOTIME(&mytime);
4575 if (((u_int32_t)mytime.tv_sec - starttime) >
4577 ((u_int32_t)mytime.tv_sec - starttime) <
4580 /* Sleep for a short period of time */
4581 (void) nfs_catnap(PZERO, 0, "nfsremove");
4583 } while (error == NFSERR_DELAY);
4587 nfsd_disabledelegation(vnode_t vp, NFSPROC_T *p)
4590 #ifdef VV_DISABLEDELEG
4592 * First, flag issuance of delegations disabled.
4594 atomic_set_long(&vp->v_vflag, VV_DISABLEDELEG);
4598 * Then call nfsd_recalldelegation() to get rid of all extant
4601 nfsd_recalldelegation(vp, p);
4605 * Check for conflicting locks, etc. and then get rid of delegations.
4606 * (At one point I thought that I should get rid of delegations for any
4607 * Setattr, since it could potentially disallow the I/O op (read or write)
4608 * allowed by the delegation. However, Setattr Ops that aren't changing
4609 * the size get a stateid of all 0s, so you can't tell if it is a delegation
4610 * for the same client or a different one, so I decided to only get rid
4611 * of delegations for other clients when the size is being changed.)
4612 * In general, a Setattr can disable NFS I/O Ops that are outstanding, such
4613 * as Write backs, even if there is no delegation, so it really isn't any
4617 nfsrv_checksetattr(vnode_t vp, struct nfsrv_descript *nd,
4618 nfsv4stateid_t *stateidp, struct nfsvattr *nvap, nfsattrbit_t *attrbitp,
4619 struct nfsexstuff *exp, NFSPROC_T *p)
4621 struct nfsstate st, *stp = &st;
4622 struct nfslock lo, *lop = &lo;
4626 if (NFSISSET_ATTRBIT(attrbitp, NFSATTRBIT_SIZE)) {
4627 stp->ls_flags = (NFSLCK_CHECK | NFSLCK_WRITEACCESS);
4628 lop->lo_first = nvap->na_size;
4633 if (NFSISSET_ATTRBIT(attrbitp, NFSATTRBIT_OWNER) ||
4634 NFSISSET_ATTRBIT(attrbitp, NFSATTRBIT_OWNERGROUP) ||
4635 NFSISSET_ATTRBIT(attrbitp, NFSATTRBIT_MODE) ||
4636 NFSISSET_ATTRBIT(attrbitp, NFSATTRBIT_ACL))
4637 stp->ls_flags |= NFSLCK_SETATTR;
4638 if (stp->ls_flags == 0)
4640 lop->lo_end = NFS64BITSSET;
4641 lop->lo_flags = NFSLCK_WRITE;
4642 stp->ls_ownerlen = 0;
4644 stp->ls_uid = nd->nd_cred->cr_uid;
4645 stp->ls_stateid.seqid = stateidp->seqid;
4646 clientid.lval[0] = stp->ls_stateid.other[0] = stateidp->other[0];
4647 clientid.lval[1] = stp->ls_stateid.other[1] = stateidp->other[1];
4648 stp->ls_stateid.other[2] = stateidp->other[2];
4649 error = nfsrv_lockctrl(vp, &stp, &lop, NULL, clientid,
4650 stateidp, exp, nd, p);
4655 * Check for a write delegation and do a CBGETATTR if there is one, updating
4656 * the attributes, as required.
4657 * Should I return an error if I can't get the attributes? (For now, I'll
4661 nfsrv_checkgetattr(struct nfsrv_descript *nd, vnode_t vp,
4662 struct nfsvattr *nvap, nfsattrbit_t *attrbitp, struct ucred *cred,
4665 struct nfsstate *stp;
4666 struct nfslockfile *lfp;
4667 struct nfsclient *clp;
4668 struct nfsvattr nva;
4671 nfsattrbit_t cbbits;
4672 u_quad_t delegfilerev;
4674 NFSCBGETATTR_ATTRBIT(attrbitp, &cbbits);
4675 if (!NFSNONZERO_ATTRBIT(&cbbits))
4679 * Get the lock file structure.
4680 * (A return of -1 means no associated state, so return ok.)
4682 error = nfsrv_getlockfh(vp, NFSLCK_CHECK, NULL, &nfh, p);
4685 error = nfsrv_getlockfile(NFSLCK_CHECK, NULL, &lfp, &nfh, 0);
4694 * Now, look for a write delegation.
4696 LIST_FOREACH(stp, &lfp->lf_deleg, ls_file) {
4697 if (stp->ls_flags & NFSLCK_DELEGWRITE)
4700 if (stp == LIST_END(&lfp->lf_deleg)) {
4705 delegfilerev = stp->ls_filerev;
4708 * If the Write delegation was issued as a part of this Compound RPC
4709 * or if we have an Implied Clientid (used in a previous Op in this
4710 * compound) and it is the client the delegation was issued to,
4712 * I also assume that it is from the same client iff the network
4713 * host IP address is the same as the callback address. (Not
4714 * exactly correct by the RFC, but avoids a lot of Getattr
4717 if (nd->nd_compref == stp->ls_compref ||
4718 ((nd->nd_flag & ND_IMPLIEDCLID) &&
4719 clp->lc_clientid.qval == nd->nd_clientid.qval) ||
4720 nfsaddr2_match(clp->lc_req.nr_nam, nd->nd_nam)) {
4726 * We are now done with the delegation state structure,
4727 * so the statelock can be released and we can now tsleep().
4731 * Now, we must do the CB Getattr callback, to see if Change or Size
4734 if (clp->lc_expiry >= NFSD_MONOSEC) {
4736 NFSVNO_ATTRINIT(&nva);
4737 nva.na_filerev = NFS64BITSSET;
4738 error = nfsrv_docallback(clp, NFSV4OP_CBGETATTR, NULL,
4739 0, &nfh, &nva, &cbbits, p);
4741 if ((nva.na_filerev != NFS64BITSSET &&
4742 nva.na_filerev > delegfilerev) ||
4743 (NFSVNO_ISSETSIZE(&nva) &&
4744 nva.na_size != nvap->na_size)) {
4745 nfsvno_updfilerev(vp, nvap, cred, p);
4746 if (NFSVNO_ISSETSIZE(&nva))
4747 nvap->na_size = nva.na_size;
4757 * This function looks for openowners that haven't had any opens for
4758 * a while and throws them away. Called by an nfsd when NFSNSF_NOOPENS
4762 nfsrv_throwawayopens(NFSPROC_T *p)
4764 struct nfsclient *clp, *nclp;
4765 struct nfsstate *stp, *nstp;
4769 nfsrv_stablefirst.nsf_flags &= ~NFSNSF_NOOPENS;
4771 * For each client...
4773 for (i = 0; i < NFSCLIENTHASHSIZE; i++) {
4774 LIST_FOREACH_SAFE(clp, &nfsclienthash[i], lc_hash, nclp) {
4775 LIST_FOREACH_SAFE(stp, &clp->lc_open, ls_list, nstp) {
4776 if (LIST_EMPTY(&stp->ls_open) &&
4777 (stp->ls_noopens > NFSNOOPEN ||
4778 (nfsrv_openpluslock * 2) >
4779 NFSRV_V4STATELIMIT))
4780 nfsrv_freeopenowner(stp, 0, p);
4788 * This function checks to see if the credentials are the same.
4789 * Returns 1 for not same, 0 otherwise.
4792 nfsrv_notsamecredname(struct nfsrv_descript *nd, struct nfsclient *clp)
4795 if (nd->nd_flag & ND_GSS) {
4796 if (!(clp->lc_flags & LCL_GSS))
4798 if (clp->lc_flags & LCL_NAME) {
4799 if (nd->nd_princlen != clp->lc_namelen ||
4800 NFSBCMP(nd->nd_principal, clp->lc_name,
4806 if (nd->nd_cred->cr_uid == clp->lc_uid)
4810 } else if (clp->lc_flags & LCL_GSS)
4813 * For AUTH_SYS, allow the same uid or root. (This is underspecified
4814 * in RFC3530, which talks about principals, but doesn't say anything
4815 * about uids for AUTH_SYS.)
4817 if (nd->nd_cred->cr_uid == clp->lc_uid || nd->nd_cred->cr_uid == 0)
4824 * Calculate the lease expiry time.
4827 nfsrv_leaseexpiry(void)
4829 struct timeval curtime;
4831 NFSGETTIME(&curtime);
4832 if (nfsrv_stablefirst.nsf_eograce > NFSD_MONOSEC)
4833 return (NFSD_MONOSEC + 2 * (nfsrv_lease + NFSRV_LEASEDELTA));
4834 return (NFSD_MONOSEC + nfsrv_lease + NFSRV_LEASEDELTA);
4838 * Delay the delegation timeout as far as ls_delegtimelimit, as required.
4841 nfsrv_delaydelegtimeout(struct nfsstate *stp)
4844 if ((stp->ls_flags & NFSLCK_DELEGRECALL) == 0)
4847 if ((stp->ls_delegtime + 15) > NFSD_MONOSEC &&
4848 stp->ls_delegtime < stp->ls_delegtimelimit) {
4849 stp->ls_delegtime += nfsrv_lease;
4850 if (stp->ls_delegtime > stp->ls_delegtimelimit)
4851 stp->ls_delegtime = stp->ls_delegtimelimit;
4856 * This function checks to see if there is any other state associated
4857 * with the openowner for this Open.
4858 * It returns 1 if there is no other state, 0 otherwise.
4861 nfsrv_nootherstate(struct nfsstate *stp)
4863 struct nfsstate *tstp;
4865 LIST_FOREACH(tstp, &stp->ls_openowner->ls_open, ls_list) {
4866 if (tstp != stp || !LIST_EMPTY(&tstp->ls_lock))
4873 * Create a list of lock deltas (changes to local byte range locking
4874 * that can be rolled back using the list) and apply the changes via
4875 * nfsvno_advlock(). Optionally, lock the list. It is expected that either
4876 * the rollback or update function will be called after this.
4877 * It returns an error (and rolls back, as required), if any nfsvno_advlock()
4878 * call fails. If it returns an error, it will unlock the list.
4881 nfsrv_locallock(vnode_t vp, struct nfslockfile *lfp, int flags,
4882 uint64_t first, uint64_t end, struct nfslockconflict *cfp, NFSPROC_T *p)
4884 struct nfslock *lop, *nlop;
4887 /* Loop through the list of locks. */
4888 lop = LIST_FIRST(&lfp->lf_locallock);
4889 while (first < end && lop != NULL) {
4890 nlop = LIST_NEXT(lop, lo_lckowner);
4891 if (first >= lop->lo_end) {
4894 } else if (first < lop->lo_first) {
4895 /* new one starts before entry in list */
4896 if (end <= lop->lo_first) {
4897 /* no overlap between old and new */
4898 error = nfsrv_dolocal(vp, lfp, flags,
4899 NFSLCK_UNLOCK, first, end, cfp, p);
4904 /* handle fragment overlapped with new one */
4905 error = nfsrv_dolocal(vp, lfp, flags,
4906 NFSLCK_UNLOCK, first, lop->lo_first, cfp,
4910 first = lop->lo_first;
4913 /* new one overlaps this entry in list */
4914 if (end <= lop->lo_end) {
4915 /* overlaps all of new one */
4916 error = nfsrv_dolocal(vp, lfp, flags,
4917 lop->lo_flags, first, end, cfp, p);
4922 /* handle fragment overlapped with new one */
4923 error = nfsrv_dolocal(vp, lfp, flags,
4924 lop->lo_flags, first, lop->lo_end, cfp, p);
4927 first = lop->lo_end;
4932 if (first < end && error == 0)
4933 /* handle fragment past end of list */
4934 error = nfsrv_dolocal(vp, lfp, flags, NFSLCK_UNLOCK, first,
4940 * Local lock unlock. Unlock all byte ranges that are no longer locked
4944 nfsrv_localunlock(vnode_t vp, struct nfslockfile *lfp, uint64_t init_first,
4945 uint64_t init_end, NFSPROC_T *p)
4947 struct nfslock *lop;
4949 uint64_t first, end;
4953 while (first < init_end) {
4954 /* Loop through all nfs locks, adjusting first and end */
4955 LIST_FOREACH(lop, &lfp->lf_lock, lo_lckfile) {
4956 if (first >= lop->lo_first &&
4957 first < lop->lo_end)
4958 /* Overlaps initial part */
4959 first = lop->lo_end;
4960 else if (end > lop->lo_first &&
4961 lop->lo_first >= first)
4962 /* Begins before end and past first */
4963 end = lop->lo_first;
4965 /* shrunk to 0 so this iteration is done */
4969 /* Unlock this segment */
4970 (void) nfsrv_dolocal(vp, lfp, NFSLCK_UNLOCK,
4971 NFSLCK_READ, first, end, NULL, p);
4972 nfsrv_locallock_commit(lfp, NFSLCK_UNLOCK,
4975 /* and move on to the rest of the range */
4982 * Do the local lock operation and update the rollback list, as required.
4983 * Perform the rollback and return the error if nfsvno_advlock() fails.
4986 nfsrv_dolocal(vnode_t vp, struct nfslockfile *lfp, int flags, int oldflags,
4987 uint64_t first, uint64_t end, struct nfslockconflict *cfp, NFSPROC_T *p)
4989 struct nfsrollback *rlp;
4990 int error, ltype, oldltype;
4992 if (flags & NFSLCK_WRITE)
4994 else if (flags & NFSLCK_READ)
4998 if (oldflags & NFSLCK_WRITE)
5000 else if (oldflags & NFSLCK_READ)
5004 if (ltype == oldltype || (oldltype == F_WRLCK && ltype == F_RDLCK))
5007 error = nfsvno_advlock(vp, ltype, first, end, p);
5010 cfp->cl_clientid.lval[0] = 0;
5011 cfp->cl_clientid.lval[1] = 0;
5013 cfp->cl_end = NFS64BITSSET;
5014 cfp->cl_flags = NFSLCK_WRITE;
5015 cfp->cl_ownerlen = 5;
5016 NFSBCOPY("LOCAL", cfp->cl_owner, 5);
5018 nfsrv_locallock_rollback(vp, lfp, p);
5019 } else if (ltype != F_UNLCK) {
5020 rlp = malloc(sizeof (struct nfsrollback), M_NFSDROLLBACK,
5022 rlp->rlck_first = first;
5023 rlp->rlck_end = end;
5024 rlp->rlck_type = oldltype;
5025 LIST_INSERT_HEAD(&lfp->lf_rollback, rlp, rlck_list);
5031 * Roll back local lock changes and free up the rollback list.
5034 nfsrv_locallock_rollback(vnode_t vp, struct nfslockfile *lfp, NFSPROC_T *p)
5036 struct nfsrollback *rlp, *nrlp;
5038 LIST_FOREACH_SAFE(rlp, &lfp->lf_rollback, rlck_list, nrlp) {
5039 (void) nfsvno_advlock(vp, rlp->rlck_type, rlp->rlck_first,
5041 free(rlp, M_NFSDROLLBACK);
5043 LIST_INIT(&lfp->lf_rollback);
5047 * Update local lock list and delete rollback list (ie now committed to the
5048 * local locks). Most of the work is done by the internal function.
5051 nfsrv_locallock_commit(struct nfslockfile *lfp, int flags, uint64_t first,
5054 struct nfsrollback *rlp, *nrlp;
5055 struct nfslock *new_lop, *other_lop;
5057 new_lop = malloc(sizeof (struct nfslock), M_NFSDLOCK, M_WAITOK);
5058 if (flags & (NFSLCK_READ | NFSLCK_WRITE))
5059 other_lop = malloc(sizeof (struct nfslock), M_NFSDLOCK,
5063 new_lop->lo_flags = flags;
5064 new_lop->lo_first = first;
5065 new_lop->lo_end = end;
5066 nfsrv_updatelock(NULL, &new_lop, &other_lop, lfp);
5067 if (new_lop != NULL)
5068 free(new_lop, M_NFSDLOCK);
5069 if (other_lop != NULL)
5070 free(other_lop, M_NFSDLOCK);
5072 /* and get rid of the rollback list */
5073 LIST_FOREACH_SAFE(rlp, &lfp->lf_rollback, rlck_list, nrlp)
5074 free(rlp, M_NFSDROLLBACK);
5075 LIST_INIT(&lfp->lf_rollback);
5079 * Lock the struct nfslockfile for local lock updating.
5082 nfsrv_locklf(struct nfslockfile *lfp)
5086 /* lf_usecount ensures *lfp won't be free'd */
5089 gotlock = nfsv4_lock(&lfp->lf_locallock_lck, 1, NULL,
5091 } while (gotlock == 0);
5096 * Unlock the struct nfslockfile after local lock updating.
5099 nfsrv_unlocklf(struct nfslockfile *lfp)
5102 nfsv4_unlock(&lfp->lf_locallock_lck, 0);