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 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 struct nfssessionhash nfssessionhash[NFSSESSIONHASHSIZE];
55 #endif /* !APPLEKEXT */
57 static u_int32_t nfsrv_openpluslock = 0, nfsrv_delegatecnt = 0;
58 static time_t nfsrvboottime;
59 static int nfsrv_writedelegifpos = 1;
60 static int nfsrv_returnoldstateid = 0, nfsrv_clients = 0;
61 static int nfsrv_clienthighwater = NFSRV_CLIENTHIGHWATER;
62 static int nfsrv_nogsscallback = 0;
65 static void nfsrv_dumpaclient(struct nfsclient *clp,
66 struct nfsd_dumpclients *dumpp);
67 static void nfsrv_freeopenowner(struct nfsstate *stp, int cansleep,
69 static int nfsrv_freeopen(struct nfsstate *stp, vnode_t vp, int cansleep,
71 static void nfsrv_freelockowner(struct nfsstate *stp, vnode_t vp, int cansleep,
73 static void nfsrv_freeallnfslocks(struct nfsstate *stp, vnode_t vp,
74 int cansleep, NFSPROC_T *p);
75 static void nfsrv_freenfslock(struct nfslock *lop);
76 static void nfsrv_freenfslockfile(struct nfslockfile *lfp);
77 static void nfsrv_freedeleg(struct nfsstate *);
78 static int nfsrv_getstate(struct nfsclient *clp, nfsv4stateid_t *stateidp,
79 u_int32_t flags, struct nfsstate **stpp);
80 static void nfsrv_getowner(struct nfsstatehead *hp, struct nfsstate *new_stp,
81 struct nfsstate **stpp);
82 static int nfsrv_getlockfh(vnode_t vp, u_short flags,
83 struct nfslockfile *new_lfp, fhandle_t *nfhp, NFSPROC_T *p);
84 static int nfsrv_getlockfile(u_short flags, struct nfslockfile **new_lfpp,
85 struct nfslockfile **lfpp, fhandle_t *nfhp, int lockit);
86 static void nfsrv_insertlock(struct nfslock *new_lop,
87 struct nfslock *insert_lop, struct nfsstate *stp, struct nfslockfile *lfp);
88 static void nfsrv_updatelock(struct nfsstate *stp, struct nfslock **new_lopp,
89 struct nfslock **other_lopp, struct nfslockfile *lfp);
90 static int nfsrv_getipnumber(u_char *cp);
91 static int nfsrv_checkrestart(nfsquad_t clientid, u_int32_t flags,
92 nfsv4stateid_t *stateidp, int specialid);
93 static int nfsrv_checkgrace(struct nfsrv_descript *nd, struct nfsclient *clp,
95 static int nfsrv_docallback(struct nfsclient *clp, int procnum,
96 nfsv4stateid_t *stateidp, int trunc, fhandle_t *fhp,
97 struct nfsvattr *nap, nfsattrbit_t *attrbitp, NFSPROC_T *p);
98 static int nfsrv_cbcallargs(struct nfsrv_descript *nd, struct nfsclient *clp,
99 uint32_t callback, int op, const char *optag, struct nfsdsession **sepp);
100 static u_int32_t nfsrv_nextclientindex(void);
101 static u_int32_t nfsrv_nextstateindex(struct nfsclient *clp);
102 static void nfsrv_markstable(struct nfsclient *clp);
103 static int nfsrv_checkstable(struct nfsclient *clp);
104 static int nfsrv_clientconflict(struct nfsclient *clp, int *haslockp, struct
105 vnode *vp, NFSPROC_T *p);
106 static int nfsrv_delegconflict(struct nfsstate *stp, int *haslockp,
107 NFSPROC_T *p, vnode_t vp);
108 static int nfsrv_cleandeleg(vnode_t vp, struct nfslockfile *lfp,
109 struct nfsclient *clp, int *haslockp, NFSPROC_T *p);
110 static int nfsrv_notsamecredname(struct nfsrv_descript *nd,
111 struct nfsclient *clp);
112 static time_t nfsrv_leaseexpiry(void);
113 static void nfsrv_delaydelegtimeout(struct nfsstate *stp);
114 static int nfsrv_checkseqid(struct nfsrv_descript *nd, u_int32_t seqid,
115 struct nfsstate *stp, struct nfsrvcache *op);
116 static int nfsrv_nootherstate(struct nfsstate *stp);
117 static int nfsrv_locallock(vnode_t vp, struct nfslockfile *lfp, int flags,
118 uint64_t first, uint64_t end, struct nfslockconflict *cfp, NFSPROC_T *p);
119 static void nfsrv_localunlock(vnode_t vp, struct nfslockfile *lfp,
120 uint64_t init_first, uint64_t init_end, NFSPROC_T *p);
121 static int nfsrv_dolocal(vnode_t vp, struct nfslockfile *lfp, int flags,
122 int oldflags, uint64_t first, uint64_t end, struct nfslockconflict *cfp,
124 static void nfsrv_locallock_rollback(vnode_t vp, struct nfslockfile *lfp,
126 static void nfsrv_locallock_commit(struct nfslockfile *lfp, int flags,
127 uint64_t first, uint64_t end);
128 static void nfsrv_locklf(struct nfslockfile *lfp);
129 static void nfsrv_unlocklf(struct nfslockfile *lfp);
130 static struct nfsdsession *nfsrv_findsession(uint8_t *sessionid);
131 static int nfsrv_freesession(struct nfsdsession *sep, uint8_t *sessionid);
132 static int nfsv4_setcbsequence(struct nfsrv_descript *nd, struct nfsclient *clp,
133 int dont_replycache, struct nfsdsession **sepp);
134 static int nfsv4_getcbsession(struct nfsclient *clp, struct nfsdsession **sepp);
137 * Scan the client list for a match and either return the current one,
138 * create a new entry or return an error.
139 * If returning a non-error, the clp structure must either be linked into
140 * the client list or free'd.
143 nfsrv_setclient(struct nfsrv_descript *nd, struct nfsclient **new_clpp,
144 nfsquad_t *clientidp, nfsquad_t *confirmp, NFSPROC_T *p)
146 struct nfsclient *clp = NULL, *new_clp = *new_clpp;
148 struct nfsstate *stp, *tstp;
149 struct sockaddr_in *sad, *rad;
150 int zapit = 0, gotit, hasstate = 0, igotlock;
151 static u_int64_t confirm_index = 0;
154 * Check for state resource limit exceeded.
156 if (nfsrv_openpluslock > NFSRV_V4STATELIMIT) {
157 error = NFSERR_RESOURCE;
161 if (nfsrv_issuedelegs == 0 ||
162 ((nd->nd_flag & ND_GSS) != 0 && nfsrv_nogsscallback != 0))
164 * Don't do callbacks when delegations are disabled or
165 * for AUTH_GSS unless enabled via nfsrv_nogsscallback.
166 * If establishing a callback connection is attempted
167 * when a firewall is blocking the callback path, the
168 * server may wait too long for the connect attempt to
169 * succeed during the Open. Some clients, such as Linux,
170 * may timeout and give up on the Open before the server
171 * replies. Also, since AUTH_GSS callbacks are not
172 * yet interoperability tested, they might cause the
173 * server to crap out, if they get past the Init call to
176 new_clp->lc_program = 0;
178 /* Lock out other nfsd threads */
179 NFSLOCKV4ROOTMUTEX();
180 nfsv4_relref(&nfsv4rootfs_lock);
182 igotlock = nfsv4_lock(&nfsv4rootfs_lock, 1, NULL,
183 NFSV4ROOTLOCKMUTEXPTR, NULL);
185 NFSUNLOCKV4ROOTMUTEX();
188 * Search for a match in the client list.
191 while (i < NFSCLIENTHASHSIZE && !gotit) {
192 LIST_FOREACH(clp, &nfsclienthash[i], lc_hash) {
193 if (new_clp->lc_idlen == clp->lc_idlen &&
194 !NFSBCMP(new_clp->lc_id, clp->lc_id, clp->lc_idlen)) {
202 (clp->lc_flags & (LCL_NEEDSCONFIRM | LCL_ADMINREVOKED))) {
203 if ((nd->nd_flag & ND_NFSV41) != 0 && confirmp->lval[1] != 0) {
205 * For NFSv4.1, if confirmp->lval[1] is non-zero, the
206 * client is trying to update a confirmed clientid.
208 NFSLOCKV4ROOTMUTEX();
209 nfsv4_unlock(&nfsv4rootfs_lock, 1);
210 NFSUNLOCKV4ROOTMUTEX();
211 confirmp->lval[1] = 0;
212 error = NFSERR_NOENT;
216 * Get rid of the old one.
218 if (i != NFSCLIENTHASHSIZE) {
219 LIST_REMOVE(clp, lc_hash);
220 nfsrv_cleanclient(clp, p);
221 nfsrv_freedeleglist(&clp->lc_deleg);
222 nfsrv_freedeleglist(&clp->lc_olddeleg);
226 * Add it after assigning a client id to it.
228 new_clp->lc_flags |= LCL_NEEDSCONFIRM;
229 if ((nd->nd_flag & ND_NFSV41) != 0)
230 new_clp->lc_confirm.lval[0] = confirmp->lval[0] =
233 confirmp->qval = new_clp->lc_confirm.qval =
235 clientidp->lval[0] = new_clp->lc_clientid.lval[0] =
236 (u_int32_t)nfsrvboottime;
237 clientidp->lval[1] = new_clp->lc_clientid.lval[1] =
238 nfsrv_nextclientindex();
239 new_clp->lc_stateindex = 0;
240 new_clp->lc_statemaxindex = 0;
241 new_clp->lc_cbref = 0;
242 new_clp->lc_expiry = nfsrv_leaseexpiry();
243 LIST_INIT(&new_clp->lc_open);
244 LIST_INIT(&new_clp->lc_deleg);
245 LIST_INIT(&new_clp->lc_olddeleg);
246 LIST_INIT(&new_clp->lc_session);
247 for (i = 0; i < NFSSTATEHASHSIZE; i++)
248 LIST_INIT(&new_clp->lc_stateid[i]);
249 LIST_INSERT_HEAD(NFSCLIENTHASH(new_clp->lc_clientid), new_clp,
251 newnfsstats.srvclients++;
252 nfsrv_openpluslock++;
254 NFSLOCKV4ROOTMUTEX();
255 nfsv4_unlock(&nfsv4rootfs_lock, 1);
256 NFSUNLOCKV4ROOTMUTEX();
258 nfsrv_zapclient(clp, p);
264 * Now, handle the cases where the id is already issued.
266 if (nfsrv_notsamecredname(nd, clp)) {
268 * Check to see if there is expired state that should go away.
270 if (clp->lc_expiry < NFSD_MONOSEC &&
271 (!LIST_EMPTY(&clp->lc_open) || !LIST_EMPTY(&clp->lc_deleg))) {
272 nfsrv_cleanclient(clp, p);
273 nfsrv_freedeleglist(&clp->lc_deleg);
277 * If there is outstanding state, then reply NFSERR_CLIDINUSE per
278 * RFC3530 Sec. 8.1.2 last para.
280 if (!LIST_EMPTY(&clp->lc_deleg)) {
282 } else if (LIST_EMPTY(&clp->lc_open)) {
286 /* Look for an Open on the OpenOwner */
287 LIST_FOREACH(stp, &clp->lc_open, ls_list) {
288 if (!LIST_EMPTY(&stp->ls_open)) {
296 * If the uid doesn't match, return NFSERR_CLIDINUSE after
297 * filling out the correct ipaddr and portnum.
299 sad = NFSSOCKADDR(new_clp->lc_req.nr_nam, struct sockaddr_in *);
300 rad = NFSSOCKADDR(clp->lc_req.nr_nam, struct sockaddr_in *);
301 sad->sin_addr.s_addr = rad->sin_addr.s_addr;
302 sad->sin_port = rad->sin_port;
303 NFSLOCKV4ROOTMUTEX();
304 nfsv4_unlock(&nfsv4rootfs_lock, 1);
305 NFSUNLOCKV4ROOTMUTEX();
306 error = NFSERR_CLIDINUSE;
311 if (NFSBCMP(new_clp->lc_verf, clp->lc_verf, NFSX_VERF)) {
313 * If the verifier has changed, the client has rebooted
314 * and a new client id is issued. The old state info
315 * can be thrown away once the SETCLIENTID_CONFIRM occurs.
317 LIST_REMOVE(clp, lc_hash);
318 new_clp->lc_flags |= LCL_NEEDSCONFIRM;
319 if ((nd->nd_flag & ND_NFSV41) != 0)
320 new_clp->lc_confirm.lval[0] = confirmp->lval[0] =
323 confirmp->qval = new_clp->lc_confirm.qval =
325 clientidp->lval[0] = new_clp->lc_clientid.lval[0] =
327 clientidp->lval[1] = new_clp->lc_clientid.lval[1] =
328 nfsrv_nextclientindex();
329 new_clp->lc_stateindex = 0;
330 new_clp->lc_statemaxindex = 0;
331 new_clp->lc_cbref = 0;
332 new_clp->lc_expiry = nfsrv_leaseexpiry();
335 * Save the state until confirmed.
337 LIST_NEWHEAD(&new_clp->lc_open, &clp->lc_open, ls_list);
338 LIST_FOREACH(tstp, &new_clp->lc_open, ls_list)
339 tstp->ls_clp = new_clp;
340 LIST_NEWHEAD(&new_clp->lc_deleg, &clp->lc_deleg, ls_list);
341 LIST_FOREACH(tstp, &new_clp->lc_deleg, ls_list)
342 tstp->ls_clp = new_clp;
343 LIST_NEWHEAD(&new_clp->lc_olddeleg, &clp->lc_olddeleg,
345 LIST_FOREACH(tstp, &new_clp->lc_olddeleg, ls_list)
346 tstp->ls_clp = new_clp;
347 for (i = 0; i < NFSSTATEHASHSIZE; i++) {
348 LIST_NEWHEAD(&new_clp->lc_stateid[i],
349 &clp->lc_stateid[i], ls_hash);
350 LIST_FOREACH(tstp, &new_clp->lc_stateid[i], ls_hash)
351 tstp->ls_clp = new_clp;
353 LIST_INSERT_HEAD(NFSCLIENTHASH(new_clp->lc_clientid), new_clp,
355 newnfsstats.srvclients++;
356 nfsrv_openpluslock++;
358 NFSLOCKV4ROOTMUTEX();
359 nfsv4_unlock(&nfsv4rootfs_lock, 1);
360 NFSUNLOCKV4ROOTMUTEX();
363 * Must wait until any outstanding callback on the old clp
367 while (clp->lc_cbref) {
368 clp->lc_flags |= LCL_WAKEUPWANTED;
369 (void)mtx_sleep(clp, NFSSTATEMUTEXPTR, PZERO - 1,
370 "nfsd clp", 10 * hz);
373 nfsrv_zapclient(clp, p);
378 /* For NFSv4.1, mark that we found a confirmed clientid. */
379 if ((nd->nd_flag & ND_NFSV41) != 0)
380 confirmp->lval[1] = 1;
383 * id and verifier match, so update the net address info
384 * and get rid of any existing callback authentication
385 * handle, so a new one will be acquired.
387 LIST_REMOVE(clp, lc_hash);
388 new_clp->lc_flags |= (LCL_NEEDSCONFIRM | LCL_DONTCLEAN);
389 new_clp->lc_expiry = nfsrv_leaseexpiry();
390 confirmp->qval = new_clp->lc_confirm.qval = ++confirm_index;
391 clientidp->lval[0] = new_clp->lc_clientid.lval[0] =
392 clp->lc_clientid.lval[0];
393 clientidp->lval[1] = new_clp->lc_clientid.lval[1] =
394 clp->lc_clientid.lval[1];
395 new_clp->lc_delegtime = clp->lc_delegtime;
396 new_clp->lc_stateindex = clp->lc_stateindex;
397 new_clp->lc_statemaxindex = clp->lc_statemaxindex;
398 new_clp->lc_cbref = 0;
399 LIST_NEWHEAD(&new_clp->lc_open, &clp->lc_open, ls_list);
400 LIST_FOREACH(tstp, &new_clp->lc_open, ls_list)
401 tstp->ls_clp = new_clp;
402 LIST_NEWHEAD(&new_clp->lc_deleg, &clp->lc_deleg, ls_list);
403 LIST_FOREACH(tstp, &new_clp->lc_deleg, ls_list)
404 tstp->ls_clp = new_clp;
405 LIST_NEWHEAD(&new_clp->lc_olddeleg, &clp->lc_olddeleg, ls_list);
406 LIST_FOREACH(tstp, &new_clp->lc_olddeleg, ls_list)
407 tstp->ls_clp = new_clp;
408 for (i = 0; i < NFSSTATEHASHSIZE; i++) {
409 LIST_NEWHEAD(&new_clp->lc_stateid[i],
410 &clp->lc_stateid[i], ls_hash);
411 LIST_FOREACH(tstp, &new_clp->lc_stateid[i], ls_hash)
412 tstp->ls_clp = new_clp;
414 LIST_INSERT_HEAD(NFSCLIENTHASH(new_clp->lc_clientid), new_clp,
416 newnfsstats.srvclients++;
417 nfsrv_openpluslock++;
420 NFSLOCKV4ROOTMUTEX();
421 nfsv4_unlock(&nfsv4rootfs_lock, 1);
422 NFSUNLOCKV4ROOTMUTEX();
424 if ((nd->nd_flag & ND_NFSV41) == 0) {
426 * Must wait until any outstanding callback on the old clp
430 while (clp->lc_cbref) {
431 clp->lc_flags |= LCL_WAKEUPWANTED;
432 (void)mtx_sleep(clp, NFSSTATEMUTEXPTR, PZERO - 1,
436 nfsrv_zapclient(clp, p);
441 NFSEXITCODE2(error, nd);
446 * Check to see if the client id exists and optionally confirm it.
449 nfsrv_getclient(nfsquad_t clientid, int opflags, struct nfsclient **clpp,
450 struct nfsdsession *nsep, nfsquad_t confirm, uint32_t cbprogram,
451 struct nfsrv_descript *nd, NFSPROC_T *p)
453 struct nfsclient *clp;
454 struct nfsstate *stp;
456 struct nfsclienthashhead *hp;
457 int error = 0, igotlock, doneok;
458 struct nfssessionhash *shp;
459 struct nfsdsession *sep;
461 static uint64_t next_sess = 0;
465 if ((nd == NULL || (nd->nd_flag & ND_NFSV41) == 0 ||
466 opflags != CLOPS_RENEW) && nfsrvboottime != clientid.lval[0]) {
467 error = NFSERR_STALECLIENTID;
472 * If called with opflags == CLOPS_RENEW, the State Lock is
473 * already held. Otherwise, we need to get either that or,
474 * for the case of Confirm, lock out the nfsd threads.
476 if (opflags & CLOPS_CONFIRM) {
477 NFSLOCKV4ROOTMUTEX();
478 nfsv4_relref(&nfsv4rootfs_lock);
480 igotlock = nfsv4_lock(&nfsv4rootfs_lock, 1, NULL,
481 NFSV4ROOTLOCKMUTEXPTR, NULL);
484 * Create a new sessionid here, since we need to do it where
485 * there is a mutex held to serialize update of next_sess.
487 if ((nd->nd_flag & ND_NFSV41) != 0) {
488 sessid[0] = ++next_sess;
489 sessid[1] = clientid.qval;
491 NFSUNLOCKV4ROOTMUTEX();
492 } else if (opflags != CLOPS_RENEW) {
496 /* For NFSv4.1, the clp is acquired from the associated session. */
497 if (nd != NULL && (nd->nd_flag & ND_NFSV41) != 0 &&
498 opflags == CLOPS_RENEW) {
500 if ((nd->nd_flag & ND_HASSEQUENCE) != 0) {
501 shp = NFSSESSIONHASH(nd->nd_sessionid);
503 sep = nfsrv_findsession(nd->nd_sessionid);
506 NFSUNLOCKSESSION(shp);
509 hp = NFSCLIENTHASH(clientid);
510 LIST_FOREACH(clp, hp, lc_hash) {
511 if (clp->lc_clientid.lval[1] == clientid.lval[1])
516 if (opflags & CLOPS_CONFIRM)
517 error = NFSERR_STALECLIENTID;
519 error = NFSERR_EXPIRED;
520 } else if (clp->lc_flags & LCL_ADMINREVOKED) {
522 * If marked admin revoked, just return the error.
524 error = NFSERR_ADMINREVOKED;
527 if (opflags & CLOPS_CONFIRM) {
528 NFSLOCKV4ROOTMUTEX();
529 nfsv4_unlock(&nfsv4rootfs_lock, 1);
530 NFSUNLOCKV4ROOTMUTEX();
531 } else if (opflags != CLOPS_RENEW) {
538 * Perform any operations specified by the opflags.
540 if (opflags & CLOPS_CONFIRM) {
541 if (((nd->nd_flag & ND_NFSV41) != 0 &&
542 clp->lc_confirm.lval[0] != confirm.lval[0]) ||
543 ((nd->nd_flag & ND_NFSV41) == 0 &&
544 clp->lc_confirm.qval != confirm.qval))
545 error = NFSERR_STALECLIENTID;
546 else if (nfsrv_notsamecredname(nd, clp))
547 error = NFSERR_CLIDINUSE;
550 if ((clp->lc_flags & (LCL_NEEDSCONFIRM | LCL_DONTCLEAN)) ==
553 * Hang onto the delegations (as old delegations)
554 * for an Open with CLAIM_DELEGATE_PREV unless in
555 * grace, but get rid of the rest of the state.
557 nfsrv_cleanclient(clp, p);
558 nfsrv_freedeleglist(&clp->lc_olddeleg);
559 if (nfsrv_checkgrace(nd, clp, 0)) {
560 /* In grace, so just delete delegations */
561 nfsrv_freedeleglist(&clp->lc_deleg);
563 LIST_FOREACH(stp, &clp->lc_deleg, ls_list)
564 stp->ls_flags |= NFSLCK_OLDDELEG;
565 clp->lc_delegtime = NFSD_MONOSEC +
566 nfsrv_lease + NFSRV_LEASEDELTA;
567 LIST_NEWHEAD(&clp->lc_olddeleg, &clp->lc_deleg,
570 if ((nd->nd_flag & ND_NFSV41) != 0)
571 clp->lc_program = cbprogram;
573 clp->lc_flags &= ~(LCL_NEEDSCONFIRM | LCL_DONTCLEAN);
575 clp->lc_flags |= LCL_NEEDSCBNULL;
576 /* For NFSv4.1, link the session onto the client. */
578 /* Hold a reference on the xprt for a backchannel. */
579 if ((nsep->sess_crflags & NFSV4CRSESS_CONNBACKCHAN)
580 != 0 && clp->lc_req.nr_client == NULL) {
581 clp->lc_req.nr_client = (struct __rpc_client *)
582 clnt_bck_create(nd->nd_xprt->xp_socket,
583 cbprogram, NFSV4_CBVERS);
584 if (clp->lc_req.nr_client != NULL) {
585 SVC_ACQUIRE(nd->nd_xprt);
587 clp->lc_req.nr_client->cl_private;
588 /* Disable idle timeout. */
589 nd->nd_xprt->xp_idletimeout = 0;
590 nsep->sess_cbsess.nfsess_xprt = nd->nd_xprt;
592 nsep->sess_crflags &= ~NFSV4CRSESS_CONNBACKCHAN;
594 NFSBCOPY(sessid, nsep->sess_sessionid,
596 NFSBCOPY(sessid, nsep->sess_cbsess.nfsess_sessionid,
598 shp = NFSSESSIONHASH(nsep->sess_sessionid);
600 LIST_INSERT_HEAD(&shp->list, nsep, sess_hash);
602 LIST_INSERT_HEAD(&clp->lc_session, nsep, sess_list);
603 nsep->sess_clp = clp;
605 NFSUNLOCKSESSION(shp);
608 } else if (clp->lc_flags & LCL_NEEDSCONFIRM) {
609 error = NFSERR_EXPIRED;
613 * If called by the Renew Op, we must check the principal.
615 if (!error && (opflags & CLOPS_RENEWOP)) {
616 if (nfsrv_notsamecredname(nd, clp)) {
618 for (i = 0; i < NFSSTATEHASHSIZE && doneok == 0; i++) {
619 LIST_FOREACH(stp, &clp->lc_stateid[i], ls_hash) {
620 if ((stp->ls_flags & NFSLCK_OPEN) &&
621 stp->ls_uid == nd->nd_cred->cr_uid) {
628 error = NFSERR_ACCES;
630 if (!error && (clp->lc_flags & LCL_CBDOWN))
631 error = NFSERR_CBPATHDOWN;
633 if ((!error || error == NFSERR_CBPATHDOWN) &&
634 (opflags & CLOPS_RENEW)) {
635 clp->lc_expiry = nfsrv_leaseexpiry();
637 if (opflags & CLOPS_CONFIRM) {
638 NFSLOCKV4ROOTMUTEX();
639 nfsv4_unlock(&nfsv4rootfs_lock, 1);
640 NFSUNLOCKV4ROOTMUTEX();
641 } else if (opflags != CLOPS_RENEW) {
648 NFSEXITCODE2(error, nd);
653 * Perform the NFSv4.1 destroy clientid.
656 nfsrv_destroyclient(nfsquad_t clientid, NFSPROC_T *p)
658 struct nfsclient *clp;
659 struct nfsclienthashhead *hp;
660 int error = 0, i, igotlock;
662 if (nfsrvboottime != clientid.lval[0]) {
663 error = NFSERR_STALECLIENTID;
667 /* Lock out other nfsd threads */
668 NFSLOCKV4ROOTMUTEX();
669 nfsv4_relref(&nfsv4rootfs_lock);
671 igotlock = nfsv4_lock(&nfsv4rootfs_lock, 1, NULL,
672 NFSV4ROOTLOCKMUTEXPTR, NULL);
673 } while (igotlock == 0);
674 NFSUNLOCKV4ROOTMUTEX();
676 hp = NFSCLIENTHASH(clientid);
677 LIST_FOREACH(clp, hp, lc_hash) {
678 if (clp->lc_clientid.lval[1] == clientid.lval[1])
682 NFSLOCKV4ROOTMUTEX();
683 nfsv4_unlock(&nfsv4rootfs_lock, 1);
684 NFSUNLOCKV4ROOTMUTEX();
685 /* Just return ok, since it is gone. */
689 /* Scan for state on the clientid. */
690 for (i = 0; i < NFSSTATEHASHSIZE; i++)
691 if (!LIST_EMPTY(&clp->lc_stateid[i])) {
692 NFSLOCKV4ROOTMUTEX();
693 nfsv4_unlock(&nfsv4rootfs_lock, 1);
694 NFSUNLOCKV4ROOTMUTEX();
695 error = NFSERR_CLIENTIDBUSY;
698 if (!LIST_EMPTY(&clp->lc_session) || !LIST_EMPTY(&clp->lc_deleg)) {
699 NFSLOCKV4ROOTMUTEX();
700 nfsv4_unlock(&nfsv4rootfs_lock, 1);
701 NFSUNLOCKV4ROOTMUTEX();
702 error = NFSERR_CLIENTIDBUSY;
706 /* Destroy the clientid and return ok. */
707 nfsrv_cleanclient(clp, p);
708 nfsrv_freedeleglist(&clp->lc_deleg);
709 nfsrv_freedeleglist(&clp->lc_olddeleg);
710 LIST_REMOVE(clp, lc_hash);
711 NFSLOCKV4ROOTMUTEX();
712 nfsv4_unlock(&nfsv4rootfs_lock, 1);
713 NFSUNLOCKV4ROOTMUTEX();
714 nfsrv_zapclient(clp, p);
716 NFSEXITCODE2(error, nd);
721 * Called from the new nfssvc syscall to admin revoke a clientid.
722 * Returns 0 for success, error otherwise.
725 nfsrv_adminrevoke(struct nfsd_clid *revokep, NFSPROC_T *p)
727 struct nfsclient *clp = NULL;
732 * First, lock out the nfsd so that state won't change while the
733 * revocation record is being written to the stable storage restart
736 NFSLOCKV4ROOTMUTEX();
738 igotlock = nfsv4_lock(&nfsv4rootfs_lock, 1, NULL,
739 NFSV4ROOTLOCKMUTEXPTR, NULL);
741 NFSUNLOCKV4ROOTMUTEX();
744 * Search for a match in the client list.
747 while (i < NFSCLIENTHASHSIZE && !gotit) {
748 LIST_FOREACH(clp, &nfsclienthash[i], lc_hash) {
749 if (revokep->nclid_idlen == clp->lc_idlen &&
750 !NFSBCMP(revokep->nclid_id, clp->lc_id, clp->lc_idlen)) {
758 NFSLOCKV4ROOTMUTEX();
759 nfsv4_unlock(&nfsv4rootfs_lock, 0);
760 NFSUNLOCKV4ROOTMUTEX();
766 * Now, write out the revocation record
768 nfsrv_writestable(clp->lc_id, clp->lc_idlen, NFSNST_REVOKE, p);
769 nfsrv_backupstable();
772 * and clear out the state, marking the clientid revoked.
774 clp->lc_flags &= ~LCL_CALLBACKSON;
775 clp->lc_flags |= LCL_ADMINREVOKED;
776 nfsrv_cleanclient(clp, p);
777 nfsrv_freedeleglist(&clp->lc_deleg);
778 nfsrv_freedeleglist(&clp->lc_olddeleg);
779 NFSLOCKV4ROOTMUTEX();
780 nfsv4_unlock(&nfsv4rootfs_lock, 0);
781 NFSUNLOCKV4ROOTMUTEX();
789 * Dump out stats for all clients. Called from nfssvc(2), that is used
793 nfsrv_dumpclients(struct nfsd_dumpclients *dumpp, int maxcnt)
795 struct nfsclient *clp;
799 * First, get a reference on the nfsv4rootfs_lock so that an
800 * exclusive lock cannot be acquired while dumping the clients.
802 NFSLOCKV4ROOTMUTEX();
803 nfsv4_getref(&nfsv4rootfs_lock, NULL, NFSV4ROOTLOCKMUTEXPTR, NULL);
804 NFSUNLOCKV4ROOTMUTEX();
807 * Rattle through the client lists until done.
809 while (i < NFSCLIENTHASHSIZE && cnt < maxcnt) {
810 clp = LIST_FIRST(&nfsclienthash[i]);
811 while (clp != LIST_END(&nfsclienthash[i]) && cnt < maxcnt) {
812 nfsrv_dumpaclient(clp, &dumpp[cnt]);
814 clp = LIST_NEXT(clp, lc_hash);
819 dumpp[cnt].ndcl_clid.nclid_idlen = 0;
821 NFSLOCKV4ROOTMUTEX();
822 nfsv4_relref(&nfsv4rootfs_lock);
823 NFSUNLOCKV4ROOTMUTEX();
827 * Dump stats for a client. Must be called with the NFSSTATELOCK and spl'd.
830 nfsrv_dumpaclient(struct nfsclient *clp, struct nfsd_dumpclients *dumpp)
832 struct nfsstate *stp, *openstp, *lckownstp;
834 struct sockaddr *sad;
835 struct sockaddr_in *rad;
836 struct sockaddr_in6 *rad6;
838 dumpp->ndcl_nopenowners = dumpp->ndcl_nlockowners = 0;
839 dumpp->ndcl_nopens = dumpp->ndcl_nlocks = 0;
840 dumpp->ndcl_ndelegs = dumpp->ndcl_nolddelegs = 0;
841 dumpp->ndcl_flags = clp->lc_flags;
842 dumpp->ndcl_clid.nclid_idlen = clp->lc_idlen;
843 NFSBCOPY(clp->lc_id, dumpp->ndcl_clid.nclid_id, clp->lc_idlen);
844 sad = NFSSOCKADDR(clp->lc_req.nr_nam, struct sockaddr *);
845 dumpp->ndcl_addrfam = sad->sa_family;
846 if (sad->sa_family == AF_INET) {
847 rad = (struct sockaddr_in *)sad;
848 dumpp->ndcl_cbaddr.sin_addr = rad->sin_addr;
850 rad6 = (struct sockaddr_in6 *)sad;
851 dumpp->ndcl_cbaddr.sin6_addr = rad6->sin6_addr;
855 * Now, scan the state lists and total up the opens and locks.
857 LIST_FOREACH(stp, &clp->lc_open, ls_list) {
858 dumpp->ndcl_nopenowners++;
859 LIST_FOREACH(openstp, &stp->ls_open, ls_list) {
860 dumpp->ndcl_nopens++;
861 LIST_FOREACH(lckownstp, &openstp->ls_open, ls_list) {
862 dumpp->ndcl_nlockowners++;
863 LIST_FOREACH(lop, &lckownstp->ls_lock, lo_lckowner) {
864 dumpp->ndcl_nlocks++;
871 * and the delegation lists.
873 LIST_FOREACH(stp, &clp->lc_deleg, ls_list) {
874 dumpp->ndcl_ndelegs++;
876 LIST_FOREACH(stp, &clp->lc_olddeleg, ls_list) {
877 dumpp->ndcl_nolddelegs++;
882 * Dump out lock stats for a file.
885 nfsrv_dumplocks(vnode_t vp, struct nfsd_dumplocks *ldumpp, int maxcnt,
888 struct nfsstate *stp;
891 struct nfslockfile *lfp;
892 struct sockaddr *sad;
893 struct sockaddr_in *rad;
894 struct sockaddr_in6 *rad6;
898 ret = nfsrv_getlockfh(vp, 0, NULL, &nfh, p);
900 * First, get a reference on the nfsv4rootfs_lock so that an
901 * exclusive lock on it cannot be acquired while dumping the locks.
903 NFSLOCKV4ROOTMUTEX();
904 nfsv4_getref(&nfsv4rootfs_lock, NULL, NFSV4ROOTLOCKMUTEXPTR, NULL);
905 NFSUNLOCKV4ROOTMUTEX();
908 ret = nfsrv_getlockfile(0, NULL, &lfp, &nfh, 0);
910 ldumpp[0].ndlck_clid.nclid_idlen = 0;
912 NFSLOCKV4ROOTMUTEX();
913 nfsv4_relref(&nfsv4rootfs_lock);
914 NFSUNLOCKV4ROOTMUTEX();
919 * For each open share on file, dump it out.
921 stp = LIST_FIRST(&lfp->lf_open);
922 while (stp != LIST_END(&lfp->lf_open) && cnt < maxcnt) {
923 ldumpp[cnt].ndlck_flags = stp->ls_flags;
924 ldumpp[cnt].ndlck_stateid.seqid = stp->ls_stateid.seqid;
925 ldumpp[cnt].ndlck_stateid.other[0] = stp->ls_stateid.other[0];
926 ldumpp[cnt].ndlck_stateid.other[1] = stp->ls_stateid.other[1];
927 ldumpp[cnt].ndlck_stateid.other[2] = stp->ls_stateid.other[2];
928 ldumpp[cnt].ndlck_owner.nclid_idlen =
929 stp->ls_openowner->ls_ownerlen;
930 NFSBCOPY(stp->ls_openowner->ls_owner,
931 ldumpp[cnt].ndlck_owner.nclid_id,
932 stp->ls_openowner->ls_ownerlen);
933 ldumpp[cnt].ndlck_clid.nclid_idlen = stp->ls_clp->lc_idlen;
934 NFSBCOPY(stp->ls_clp->lc_id, ldumpp[cnt].ndlck_clid.nclid_id,
935 stp->ls_clp->lc_idlen);
936 sad=NFSSOCKADDR(stp->ls_clp->lc_req.nr_nam, struct sockaddr *);
937 ldumpp[cnt].ndlck_addrfam = sad->sa_family;
938 if (sad->sa_family == AF_INET) {
939 rad = (struct sockaddr_in *)sad;
940 ldumpp[cnt].ndlck_cbaddr.sin_addr = rad->sin_addr;
942 rad6 = (struct sockaddr_in6 *)sad;
943 ldumpp[cnt].ndlck_cbaddr.sin6_addr = rad6->sin6_addr;
945 stp = LIST_NEXT(stp, ls_file);
952 lop = LIST_FIRST(&lfp->lf_lock);
953 while (lop != LIST_END(&lfp->lf_lock) && cnt < maxcnt) {
955 ldumpp[cnt].ndlck_flags = lop->lo_flags;
956 ldumpp[cnt].ndlck_first = lop->lo_first;
957 ldumpp[cnt].ndlck_end = lop->lo_end;
958 ldumpp[cnt].ndlck_stateid.seqid = stp->ls_stateid.seqid;
959 ldumpp[cnt].ndlck_stateid.other[0] = stp->ls_stateid.other[0];
960 ldumpp[cnt].ndlck_stateid.other[1] = stp->ls_stateid.other[1];
961 ldumpp[cnt].ndlck_stateid.other[2] = stp->ls_stateid.other[2];
962 ldumpp[cnt].ndlck_owner.nclid_idlen = stp->ls_ownerlen;
963 NFSBCOPY(stp->ls_owner, ldumpp[cnt].ndlck_owner.nclid_id,
965 ldumpp[cnt].ndlck_clid.nclid_idlen = stp->ls_clp->lc_idlen;
966 NFSBCOPY(stp->ls_clp->lc_id, ldumpp[cnt].ndlck_clid.nclid_id,
967 stp->ls_clp->lc_idlen);
968 sad=NFSSOCKADDR(stp->ls_clp->lc_req.nr_nam, struct sockaddr *);
969 ldumpp[cnt].ndlck_addrfam = sad->sa_family;
970 if (sad->sa_family == AF_INET) {
971 rad = (struct sockaddr_in *)sad;
972 ldumpp[cnt].ndlck_cbaddr.sin_addr = rad->sin_addr;
974 rad6 = (struct sockaddr_in6 *)sad;
975 ldumpp[cnt].ndlck_cbaddr.sin6_addr = rad6->sin6_addr;
977 lop = LIST_NEXT(lop, lo_lckfile);
982 * and the delegations.
984 stp = LIST_FIRST(&lfp->lf_deleg);
985 while (stp != LIST_END(&lfp->lf_deleg) && cnt < maxcnt) {
986 ldumpp[cnt].ndlck_flags = stp->ls_flags;
987 ldumpp[cnt].ndlck_stateid.seqid = stp->ls_stateid.seqid;
988 ldumpp[cnt].ndlck_stateid.other[0] = stp->ls_stateid.other[0];
989 ldumpp[cnt].ndlck_stateid.other[1] = stp->ls_stateid.other[1];
990 ldumpp[cnt].ndlck_stateid.other[2] = stp->ls_stateid.other[2];
991 ldumpp[cnt].ndlck_owner.nclid_idlen = 0;
992 ldumpp[cnt].ndlck_clid.nclid_idlen = stp->ls_clp->lc_idlen;
993 NFSBCOPY(stp->ls_clp->lc_id, ldumpp[cnt].ndlck_clid.nclid_id,
994 stp->ls_clp->lc_idlen);
995 sad=NFSSOCKADDR(stp->ls_clp->lc_req.nr_nam, struct sockaddr *);
996 ldumpp[cnt].ndlck_addrfam = sad->sa_family;
997 if (sad->sa_family == AF_INET) {
998 rad = (struct sockaddr_in *)sad;
999 ldumpp[cnt].ndlck_cbaddr.sin_addr = rad->sin_addr;
1001 rad6 = (struct sockaddr_in6 *)sad;
1002 ldumpp[cnt].ndlck_cbaddr.sin6_addr = rad6->sin6_addr;
1004 stp = LIST_NEXT(stp, ls_file);
1009 * If list isn't full, mark end of list by setting the client name
1013 ldumpp[cnt].ndlck_clid.nclid_idlen = 0;
1015 NFSLOCKV4ROOTMUTEX();
1016 nfsv4_relref(&nfsv4rootfs_lock);
1017 NFSUNLOCKV4ROOTMUTEX();
1021 * Server timer routine. It can scan any linked list, so long
1022 * as it holds the spin/mutex lock and there is no exclusive lock on
1024 * (For OpenBSD, a kthread is ok. For FreeBSD, I think it is ok
1025 * to do this from a callout, since the spin locks work. For
1026 * Darwin, I'm not sure what will work correctly yet.)
1027 * Should be called once per second.
1030 nfsrv_servertimer(void)
1032 struct nfsclient *clp, *nclp;
1033 struct nfsstate *stp, *nstp;
1037 * Make sure nfsboottime is set. This is used by V3 as well
1038 * as V4. Note that nfsboottime is not nfsrvboottime, which is
1039 * only used by the V4 server for leases.
1041 if (nfsboottime.tv_sec == 0)
1042 NFSSETBOOTTIME(nfsboottime);
1045 * If server hasn't started yet, just return.
1048 if (nfsrv_stablefirst.nsf_eograce == 0) {
1052 if (!(nfsrv_stablefirst.nsf_flags & NFSNSF_UPDATEDONE)) {
1053 if (!(nfsrv_stablefirst.nsf_flags & NFSNSF_GRACEOVER) &&
1054 NFSD_MONOSEC > nfsrv_stablefirst.nsf_eograce)
1055 nfsrv_stablefirst.nsf_flags |=
1056 (NFSNSF_GRACEOVER | NFSNSF_NEEDLOCK);
1062 * Try and get a reference count on the nfsv4rootfs_lock so that
1063 * no nfsd thread can acquire an exclusive lock on it before this
1064 * call is done. If it is already exclusively locked, just return.
1066 NFSLOCKV4ROOTMUTEX();
1067 got_ref = nfsv4_getref_nonblock(&nfsv4rootfs_lock);
1068 NFSUNLOCKV4ROOTMUTEX();
1075 * For each client...
1077 for (i = 0; i < NFSCLIENTHASHSIZE; i++) {
1078 clp = LIST_FIRST(&nfsclienthash[i]);
1079 while (clp != LIST_END(&nfsclienthash[i])) {
1080 nclp = LIST_NEXT(clp, lc_hash);
1081 if (!(clp->lc_flags & LCL_EXPIREIT)) {
1082 if (((clp->lc_expiry + NFSRV_STALELEASE) < NFSD_MONOSEC
1083 && ((LIST_EMPTY(&clp->lc_deleg)
1084 && LIST_EMPTY(&clp->lc_open)) ||
1085 nfsrv_clients > nfsrv_clienthighwater)) ||
1086 (clp->lc_expiry + NFSRV_MOULDYLEASE) < NFSD_MONOSEC ||
1087 (clp->lc_expiry < NFSD_MONOSEC &&
1088 (nfsrv_openpluslock * 10 / 9) > NFSRV_V4STATELIMIT)) {
1090 * Lease has expired several nfsrv_lease times ago:
1092 * - no state is associated with it
1094 * - above high water mark for number of clients
1095 * (nfsrv_clienthighwater should be large enough
1096 * that this only occurs when clients fail to
1097 * use the same nfs_client_id4.id. Maybe somewhat
1098 * higher that the maximum number of clients that
1099 * will mount this server?)
1101 * Lease has expired a very long time ago
1103 * Lease has expired PLUS the number of opens + locks
1104 * has exceeded 90% of capacity
1106 * --> Mark for expiry. The actual expiry will be done
1107 * by an nfsd sometime soon.
1109 clp->lc_flags |= LCL_EXPIREIT;
1110 nfsrv_stablefirst.nsf_flags |=
1111 (NFSNSF_NEEDLOCK | NFSNSF_EXPIREDCLIENT);
1114 * If there are no opens, increment no open tick cnt
1115 * If time exceeds NFSNOOPEN, mark it to be thrown away
1116 * otherwise, if there is an open, reset no open time
1117 * Hopefully, this will avoid excessive re-creation
1118 * of open owners and subsequent open confirms.
1120 stp = LIST_FIRST(&clp->lc_open);
1121 while (stp != LIST_END(&clp->lc_open)) {
1122 nstp = LIST_NEXT(stp, ls_list);
1123 if (LIST_EMPTY(&stp->ls_open)) {
1125 if (stp->ls_noopens > NFSNOOPEN ||
1126 (nfsrv_openpluslock * 2) >
1128 nfsrv_stablefirst.nsf_flags |=
1131 stp->ls_noopens = 0;
1141 NFSLOCKV4ROOTMUTEX();
1142 nfsv4_relref(&nfsv4rootfs_lock);
1143 NFSUNLOCKV4ROOTMUTEX();
1147 * The following set of functions free up the various data structures.
1150 * Clear out all open/lock state related to this nfsclient.
1151 * Caller must hold an exclusive lock on nfsv4rootfs_lock, so that
1152 * there are no other active nfsd threads.
1155 nfsrv_cleanclient(struct nfsclient *clp, NFSPROC_T *p)
1157 struct nfsstate *stp, *nstp;
1158 struct nfsdsession *sep, *nsep;
1160 LIST_FOREACH_SAFE(stp, &clp->lc_open, ls_list, nstp)
1161 nfsrv_freeopenowner(stp, 1, p);
1162 if ((clp->lc_flags & LCL_ADMINREVOKED) == 0)
1163 LIST_FOREACH_SAFE(sep, &clp->lc_session, sess_list, nsep)
1164 (void)nfsrv_freesession(sep, NULL);
1168 * Free a client that has been cleaned. It should also already have been
1169 * removed from the lists.
1170 * (Just to be safe w.r.t. newnfs_disconnect(), call this function when
1171 * softclock interrupts are enabled.)
1174 nfsrv_zapclient(struct nfsclient *clp, NFSPROC_T *p)
1178 if ((clp->lc_flags & (LCL_GSS | LCL_CALLBACKSON)) ==
1179 (LCL_GSS | LCL_CALLBACKSON) &&
1180 (clp->lc_hand.nfsh_flag & NFSG_COMPLETE) &&
1181 clp->lc_handlelen > 0) {
1182 clp->lc_hand.nfsh_flag &= ~NFSG_COMPLETE;
1183 clp->lc_hand.nfsh_flag |= NFSG_DESTROYED;
1184 (void) nfsrv_docallback(clp, NFSV4PROC_CBNULL,
1185 NULL, 0, NULL, NULL, NULL, p);
1188 newnfs_disconnect(&clp->lc_req);
1189 NFSSOCKADDRFREE(clp->lc_req.nr_nam);
1190 NFSFREEMUTEX(&clp->lc_req.nr_mtx);
1191 free((caddr_t)clp, M_NFSDCLIENT);
1193 newnfsstats.srvclients--;
1194 nfsrv_openpluslock--;
1200 * Free a list of delegation state structures.
1201 * (This function will also free all nfslockfile structures that no
1202 * longer have associated state.)
1205 nfsrv_freedeleglist(struct nfsstatehead *sthp)
1207 struct nfsstate *stp, *nstp;
1209 LIST_FOREACH_SAFE(stp, sthp, ls_list, nstp) {
1210 nfsrv_freedeleg(stp);
1216 * Free up a delegation.
1219 nfsrv_freedeleg(struct nfsstate *stp)
1221 struct nfslockfile *lfp;
1223 LIST_REMOVE(stp, ls_hash);
1224 LIST_REMOVE(stp, ls_list);
1225 LIST_REMOVE(stp, ls_file);
1227 if (LIST_EMPTY(&lfp->lf_open) &&
1228 LIST_EMPTY(&lfp->lf_lock) && LIST_EMPTY(&lfp->lf_deleg) &&
1229 LIST_EMPTY(&lfp->lf_locallock) && LIST_EMPTY(&lfp->lf_rollback) &&
1230 lfp->lf_usecount == 0 &&
1231 nfsv4_testlock(&lfp->lf_locallock_lck) == 0)
1232 nfsrv_freenfslockfile(lfp);
1233 FREE((caddr_t)stp, M_NFSDSTATE);
1234 newnfsstats.srvdelegates--;
1235 nfsrv_openpluslock--;
1236 nfsrv_delegatecnt--;
1240 * This function frees an open owner and all associated opens.
1243 nfsrv_freeopenowner(struct nfsstate *stp, int cansleep, NFSPROC_T *p)
1245 struct nfsstate *nstp, *tstp;
1247 LIST_REMOVE(stp, ls_list);
1249 * Now, free all associated opens.
1251 nstp = LIST_FIRST(&stp->ls_open);
1252 while (nstp != LIST_END(&stp->ls_open)) {
1254 nstp = LIST_NEXT(nstp, ls_list);
1255 (void) nfsrv_freeopen(tstp, NULL, cansleep, p);
1258 nfsrvd_derefcache(stp->ls_op);
1259 FREE((caddr_t)stp, M_NFSDSTATE);
1260 newnfsstats.srvopenowners--;
1261 nfsrv_openpluslock--;
1265 * This function frees an open (nfsstate open structure) with all associated
1266 * lock_owners and locks. It also frees the nfslockfile structure iff there
1267 * are no other opens on the file.
1268 * Returns 1 if it free'd the nfslockfile, 0 otherwise.
1271 nfsrv_freeopen(struct nfsstate *stp, vnode_t vp, int cansleep, NFSPROC_T *p)
1273 struct nfsstate *nstp, *tstp;
1274 struct nfslockfile *lfp;
1277 LIST_REMOVE(stp, ls_hash);
1278 LIST_REMOVE(stp, ls_list);
1279 LIST_REMOVE(stp, ls_file);
1283 * Now, free all lockowners associated with this open.
1285 LIST_FOREACH_SAFE(tstp, &stp->ls_open, ls_list, nstp)
1286 nfsrv_freelockowner(tstp, vp, cansleep, p);
1289 * The nfslockfile is freed here if there are no locks
1290 * associated with the open.
1291 * If there are locks associated with the open, the
1292 * nfslockfile structure can be freed via nfsrv_freelockowner().
1293 * Acquire the state mutex to avoid races with calls to
1294 * nfsrv_getlockfile().
1298 if (lfp != NULL && LIST_EMPTY(&lfp->lf_open) &&
1299 LIST_EMPTY(&lfp->lf_deleg) && LIST_EMPTY(&lfp->lf_lock) &&
1300 LIST_EMPTY(&lfp->lf_locallock) && LIST_EMPTY(&lfp->lf_rollback) &&
1301 lfp->lf_usecount == 0 &&
1302 (cansleep != 0 || nfsv4_testlock(&lfp->lf_locallock_lck) == 0)) {
1303 nfsrv_freenfslockfile(lfp);
1309 FREE((caddr_t)stp, M_NFSDSTATE);
1310 newnfsstats.srvopens--;
1311 nfsrv_openpluslock--;
1316 * Frees a lockowner and all associated locks.
1319 nfsrv_freelockowner(struct nfsstate *stp, vnode_t vp, int cansleep,
1323 LIST_REMOVE(stp, ls_hash);
1324 LIST_REMOVE(stp, ls_list);
1325 nfsrv_freeallnfslocks(stp, vp, cansleep, p);
1327 nfsrvd_derefcache(stp->ls_op);
1328 FREE((caddr_t)stp, M_NFSDSTATE);
1329 newnfsstats.srvlockowners--;
1330 nfsrv_openpluslock--;
1334 * Free all the nfs locks on a lockowner.
1337 nfsrv_freeallnfslocks(struct nfsstate *stp, vnode_t vp, int cansleep,
1340 struct nfslock *lop, *nlop;
1341 struct nfsrollback *rlp, *nrlp;
1342 struct nfslockfile *lfp = NULL;
1345 uint64_t first, end;
1347 lop = LIST_FIRST(&stp->ls_lock);
1348 while (lop != LIST_END(&stp->ls_lock)) {
1349 nlop = LIST_NEXT(lop, lo_lckowner);
1351 * Since all locks should be for the same file, lfp should
1356 else if (lfp != lop->lo_lfp)
1357 panic("allnfslocks");
1359 * If vp is NULL and cansleep != 0, a vnode must be acquired
1360 * from the file handle. This only occurs when called from
1361 * nfsrv_cleanclient().
1364 if (nfsrv_dolocallocks == 0)
1366 else if (vp == NULL && cansleep != 0)
1367 tvp = nfsvno_getvp(&lfp->lf_fh);
1376 first = lop->lo_first;
1378 nfsrv_freenfslock(lop);
1379 nfsrv_localunlock(tvp, lfp, first, end, p);
1380 LIST_FOREACH_SAFE(rlp, &lfp->lf_rollback, rlck_list,
1382 free(rlp, M_NFSDROLLBACK);
1383 LIST_INIT(&lfp->lf_rollback);
1385 nfsrv_freenfslock(lop);
1388 if (vp == NULL && tvp != NULL)
1393 * Free an nfslock structure.
1396 nfsrv_freenfslock(struct nfslock *lop)
1399 if (lop->lo_lckfile.le_prev != NULL) {
1400 LIST_REMOVE(lop, lo_lckfile);
1401 newnfsstats.srvlocks--;
1402 nfsrv_openpluslock--;
1404 LIST_REMOVE(lop, lo_lckowner);
1405 FREE((caddr_t)lop, M_NFSDLOCK);
1409 * This function frees an nfslockfile structure.
1412 nfsrv_freenfslockfile(struct nfslockfile *lfp)
1415 LIST_REMOVE(lfp, lf_hash);
1416 FREE((caddr_t)lfp, M_NFSDLOCKFILE);
1420 * This function looks up an nfsstate structure via stateid.
1423 nfsrv_getstate(struct nfsclient *clp, nfsv4stateid_t *stateidp, __unused u_int32_t flags,
1424 struct nfsstate **stpp)
1426 struct nfsstate *stp;
1427 struct nfsstatehead *hp;
1431 hp = NFSSTATEHASH(clp, *stateidp);
1432 LIST_FOREACH(stp, hp, ls_hash) {
1433 if (!NFSBCMP(stp->ls_stateid.other, stateidp->other,
1439 * If no state id in list, return NFSERR_BADSTATEID.
1441 if (stp == LIST_END(hp)) {
1442 error = NFSERR_BADSTATEID;
1453 * This function gets an nfsstate structure via owner string.
1456 nfsrv_getowner(struct nfsstatehead *hp, struct nfsstate *new_stp,
1457 struct nfsstate **stpp)
1459 struct nfsstate *stp;
1462 LIST_FOREACH(stp, hp, ls_list) {
1463 if (new_stp->ls_ownerlen == stp->ls_ownerlen &&
1464 !NFSBCMP(new_stp->ls_owner,stp->ls_owner,stp->ls_ownerlen)) {
1472 * Lock control function called to update lock status.
1473 * Returns 0 upon success, -1 if there is no lock and the flags indicate
1474 * that one isn't to be created and an NFSERR_xxx for other errors.
1475 * The structures new_stp and new_lop are passed in as pointers that should
1476 * be set to NULL if the structure is used and shouldn't be free'd.
1477 * For the NFSLCK_TEST and NFSLCK_CHECK cases, the structures are
1478 * never used and can safely be allocated on the stack. For all other
1479 * cases, *new_stpp and *new_lopp should be malloc'd before the call,
1480 * in case they are used.
1483 nfsrv_lockctrl(vnode_t vp, struct nfsstate **new_stpp,
1484 struct nfslock **new_lopp, struct nfslockconflict *cfp,
1485 nfsquad_t clientid, nfsv4stateid_t *stateidp,
1486 __unused struct nfsexstuff *exp,
1487 struct nfsrv_descript *nd, NFSPROC_T *p)
1489 struct nfslock *lop;
1490 struct nfsstate *new_stp = *new_stpp;
1491 struct nfslock *new_lop = *new_lopp;
1492 struct nfsstate *tstp, *mystp, *nstp;
1494 struct nfslockfile *lfp;
1495 struct nfslock *other_lop = NULL;
1496 struct nfsstate *stp, *lckstp = NULL;
1497 struct nfsclient *clp = NULL;
1499 int error = 0, haslock = 0, ret, reterr;
1500 int getlckret, delegation = 0, filestruct_locked;
1502 uint64_t first, end;
1503 uint32_t lock_flags;
1505 if (new_stp->ls_flags & (NFSLCK_CHECK | NFSLCK_SETATTR)) {
1507 * Note the special cases of "all 1s" or "all 0s" stateids and
1508 * let reads with all 1s go ahead.
1510 if (new_stp->ls_stateid.seqid == 0x0 &&
1511 new_stp->ls_stateid.other[0] == 0x0 &&
1512 new_stp->ls_stateid.other[1] == 0x0 &&
1513 new_stp->ls_stateid.other[2] == 0x0)
1515 else if (new_stp->ls_stateid.seqid == 0xffffffff &&
1516 new_stp->ls_stateid.other[0] == 0xffffffff &&
1517 new_stp->ls_stateid.other[1] == 0xffffffff &&
1518 new_stp->ls_stateid.other[2] == 0xffffffff)
1523 * Check for restart conditions (client and server).
1525 error = nfsrv_checkrestart(clientid, new_stp->ls_flags,
1526 &new_stp->ls_stateid, specialid);
1531 * Check for state resource limit exceeded.
1533 if ((new_stp->ls_flags & NFSLCK_LOCK) &&
1534 nfsrv_openpluslock > NFSRV_V4STATELIMIT) {
1535 error = NFSERR_RESOURCE;
1540 * For the lock case, get another nfslock structure,
1541 * just in case we need it.
1542 * Malloc now, before we start sifting through the linked lists,
1543 * in case we have to wait for memory.
1546 if (new_stp->ls_flags & NFSLCK_LOCK)
1547 MALLOC(other_lop, struct nfslock *, sizeof (struct nfslock),
1548 M_NFSDLOCK, M_WAITOK);
1549 filestruct_locked = 0;
1554 * Get the lockfile structure for CFH now, so we can do a sanity
1555 * check against the stateid, before incrementing the seqid#, since
1556 * we want to return NFSERR_BADSTATEID on failure and the seqid#
1557 * shouldn't be incremented for this case.
1558 * If nfsrv_getlockfile() returns -1, it means "not found", which
1559 * will be handled later.
1560 * If we are doing Lock/LockU and local locking is enabled, sleep
1561 * lock the nfslockfile structure.
1563 getlckret = nfsrv_getlockfh(vp, new_stp->ls_flags, NULL, &nfh, p);
1565 if (getlckret == 0) {
1566 if ((new_stp->ls_flags & (NFSLCK_LOCK | NFSLCK_UNLOCK)) != 0 &&
1567 nfsrv_dolocallocks != 0 && nd->nd_repstat == 0) {
1568 getlckret = nfsrv_getlockfile(new_stp->ls_flags, NULL,
1571 filestruct_locked = 1;
1573 getlckret = nfsrv_getlockfile(new_stp->ls_flags, NULL,
1576 if (getlckret != 0 && getlckret != -1)
1579 if (filestruct_locked != 0) {
1580 LIST_INIT(&lfp->lf_rollback);
1581 if ((new_stp->ls_flags & NFSLCK_LOCK)) {
1583 * For local locking, do the advisory locking now, so
1584 * that any conflict can be detected. A failure later
1585 * can be rolled back locally. If an error is returned,
1586 * struct nfslockfile has been unlocked and any local
1587 * locking rolled back.
1590 reterr = nfsrv_locallock(vp, lfp,
1591 (new_lop->lo_flags & (NFSLCK_READ | NFSLCK_WRITE)),
1592 new_lop->lo_first, new_lop->lo_end, cfp, p);
1597 if (specialid == 0) {
1598 if (new_stp->ls_flags & NFSLCK_TEST) {
1600 * RFC 3530 does not list LockT as an op that renews a
1601 * lease, but the concensus seems to be that it is ok
1602 * for a server to do so.
1604 error = nfsrv_getclient(clientid, CLOPS_RENEW, &clp, NULL,
1605 (nfsquad_t)((u_quad_t)0), 0, nd, p);
1608 * Since NFSERR_EXPIRED, NFSERR_ADMINREVOKED are not valid
1609 * error returns for LockT, just go ahead and test for a lock,
1610 * since there are no locks for this client, but other locks
1611 * can conflict. (ie. same client will always be false)
1613 if (error == NFSERR_EXPIRED || error == NFSERR_ADMINREVOKED)
1617 error = nfsrv_getclient(clientid, CLOPS_RENEW, &clp, NULL,
1618 (nfsquad_t)((u_quad_t)0), 0, nd, p);
1621 * Look up the stateid
1623 error = nfsrv_getstate(clp, &new_stp->ls_stateid,
1624 new_stp->ls_flags, &stp);
1626 * do some sanity checks for an unconfirmed open or a
1627 * stateid that refers to the wrong file, for an open stateid
1629 if (error == 0 && (stp->ls_flags & NFSLCK_OPEN) &&
1630 ((stp->ls_openowner->ls_flags & NFSLCK_NEEDSCONFIRM) ||
1631 (getlckret == 0 && stp->ls_lfp != lfp)))
1632 error = NFSERR_BADSTATEID;
1634 (stp->ls_flags & (NFSLCK_DELEGREAD | NFSLCK_DELEGWRITE)) &&
1635 getlckret == 0 && stp->ls_lfp != lfp)
1636 error = NFSERR_BADSTATEID;
1639 * If the lockowner stateid doesn't refer to the same file,
1640 * I believe that is considered ok, since some clients will
1641 * only create a single lockowner and use that for all locks
1643 * For now, log it as a diagnostic, instead of considering it
1646 if (error == 0 && (stp->ls_flags &
1647 (NFSLCK_OPEN | NFSLCK_DELEGREAD | NFSLCK_DELEGWRITE)) == 0 &&
1648 getlckret == 0 && stp->ls_lfp != lfp) {
1650 printf("Got a lock statid for different file open\n");
1653 error = NFSERR_BADSTATEID;
1658 if (new_stp->ls_flags & NFSLCK_OPENTOLOCK) {
1660 * If haslock set, we've already checked the seqid.
1663 if (stp->ls_flags & NFSLCK_OPEN)
1664 error = nfsrv_checkseqid(nd, new_stp->ls_seq,
1665 stp->ls_openowner, new_stp->ls_op);
1667 error = NFSERR_BADSTATEID;
1670 nfsrv_getowner(&stp->ls_open, new_stp, &lckstp);
1673 * I believe this should be an error, but it
1674 * isn't obvious what NFSERR_xxx would be
1675 * appropriate, so I'll use NFSERR_INVAL for now.
1677 error = NFSERR_INVAL;
1680 } else if (new_stp->ls_flags&(NFSLCK_LOCK|NFSLCK_UNLOCK)) {
1682 * If haslock set, ditto above.
1685 if (stp->ls_flags & NFSLCK_OPEN)
1686 error = NFSERR_BADSTATEID;
1688 error = nfsrv_checkseqid(nd, new_stp->ls_seq,
1689 stp, new_stp->ls_op);
1697 * If the seqid part of the stateid isn't the same, return
1698 * NFSERR_OLDSTATEID for cases other than I/O Ops.
1699 * For I/O Ops, only return NFSERR_OLDSTATEID if
1700 * nfsrv_returnoldstateid is set. (The concensus on the email
1701 * list was that most clients would prefer to not receive
1702 * NFSERR_OLDSTATEID for I/O Ops, but the RFC suggests that that
1703 * is what will happen, so I use the nfsrv_returnoldstateid to
1704 * allow for either server configuration.)
1706 if (!error && stp->ls_stateid.seqid!=new_stp->ls_stateid.seqid &&
1707 (((nd->nd_flag & ND_NFSV41) == 0 &&
1708 (!(new_stp->ls_flags & NFSLCK_CHECK) ||
1709 nfsrv_returnoldstateid)) ||
1710 ((nd->nd_flag & ND_NFSV41) != 0 &&
1711 new_stp->ls_stateid.seqid != 0)))
1712 error = NFSERR_OLDSTATEID;
1717 * Now we can check for grace.
1720 error = nfsrv_checkgrace(nd, clp, new_stp->ls_flags);
1721 if ((new_stp->ls_flags & NFSLCK_RECLAIM) && !error &&
1722 nfsrv_checkstable(clp))
1723 error = NFSERR_NOGRACE;
1725 * If we successfully Reclaimed state, note that.
1727 if ((new_stp->ls_flags & NFSLCK_RECLAIM) && !error)
1728 nfsrv_markstable(clp);
1731 * At this point, either error == NFSERR_BADSTATEID or the
1732 * seqid# has been updated, so we can return any error.
1733 * If error == 0, there may be an error in:
1734 * nd_repstat - Set by the calling function.
1735 * reterr - Set above, if getting the nfslockfile structure
1736 * or acquiring the local lock failed.
1737 * (If both of these are set, nd_repstat should probably be
1738 * returned, since that error was detected before this
1741 if (error != 0 || nd->nd_repstat != 0 || reterr != 0) {
1743 if (nd->nd_repstat != 0)
1744 error = nd->nd_repstat;
1748 if (filestruct_locked != 0) {
1749 /* Roll back local locks. */
1751 nfsrv_locallock_rollback(vp, lfp, p);
1753 nfsrv_unlocklf(lfp);
1760 * Check the nfsrv_getlockfile return.
1761 * Returned -1 if no structure found.
1763 if (getlckret == -1) {
1764 error = NFSERR_EXPIRED;
1766 * Called from lockt, so no lock is OK.
1768 if (new_stp->ls_flags & NFSLCK_TEST) {
1770 } else if (new_stp->ls_flags &
1771 (NFSLCK_CHECK | NFSLCK_SETATTR)) {
1773 * Called to check for a lock, OK if the stateid is all
1774 * 1s or all 0s, but there should be an nfsstate
1776 * (ie. If there is no open, I'll assume no share
1782 error = NFSERR_BADSTATEID;
1789 * For NFSLCK_CHECK and NFSLCK_LOCK, test for a share conflict.
1790 * For NFSLCK_CHECK, allow a read if write access is granted,
1791 * but check for a deny. For NFSLCK_LOCK, require correct access,
1792 * which implies a conflicting deny can't exist.
1794 if (new_stp->ls_flags & (NFSLCK_CHECK | NFSLCK_LOCK)) {
1796 * Four kinds of state id:
1797 * - specialid (all 0s or all 1s), only for NFSLCK_CHECK
1798 * - stateid for an open
1799 * - stateid for a delegation
1800 * - stateid for a lock owner
1803 if (stp->ls_flags & (NFSLCK_DELEGREAD | NFSLCK_DELEGWRITE)) {
1806 nfsrv_delaydelegtimeout(stp);
1807 } else if (stp->ls_flags & NFSLCK_OPEN) {
1810 mystp = stp->ls_openstp;
1813 * If locking or checking, require correct access
1816 if (((new_stp->ls_flags & NFSLCK_LOCK) &&
1817 !((new_lop->lo_flags >> NFSLCK_LOCKSHIFT) &
1818 mystp->ls_flags & NFSLCK_ACCESSBITS)) ||
1819 ((new_stp->ls_flags & (NFSLCK_CHECK|NFSLCK_READACCESS)) ==
1820 (NFSLCK_CHECK | NFSLCK_READACCESS) &&
1821 !(mystp->ls_flags & NFSLCK_READACCESS)) ||
1822 ((new_stp->ls_flags & (NFSLCK_CHECK|NFSLCK_WRITEACCESS)) ==
1823 (NFSLCK_CHECK | NFSLCK_WRITEACCESS) &&
1824 !(mystp->ls_flags & NFSLCK_WRITEACCESS))) {
1825 if (filestruct_locked != 0) {
1826 /* Roll back local locks. */
1828 nfsrv_locallock_rollback(vp, lfp, p);
1830 nfsrv_unlocklf(lfp);
1833 error = NFSERR_OPENMODE;
1838 if ((new_stp->ls_flags & NFSLCK_CHECK) && !delegation) {
1840 * Check for a conflicting deny bit.
1842 LIST_FOREACH(tstp, &lfp->lf_open, ls_file) {
1843 if (tstp != mystp) {
1844 bits = tstp->ls_flags;
1845 bits >>= NFSLCK_SHIFT;
1846 if (new_stp->ls_flags & bits & NFSLCK_ACCESSBITS) {
1847 ret = nfsrv_clientconflict(tstp->ls_clp, &haslock,
1851 * nfsrv_clientconflict unlocks state
1852 * when it returns non-zero.
1860 error = NFSERR_PERM;
1862 error = NFSERR_OPENMODE;
1868 /* We're outta here */
1875 * For setattr, just get rid of all the Delegations for other clients.
1877 if (new_stp->ls_flags & NFSLCK_SETATTR) {
1878 ret = nfsrv_cleandeleg(vp, lfp, clp, &haslock, p);
1881 * nfsrv_cleandeleg() unlocks state when it
1891 if (!(new_stp->ls_flags & NFSLCK_CHECK) ||
1892 (LIST_EMPTY(&lfp->lf_open) && LIST_EMPTY(&lfp->lf_lock) &&
1893 LIST_EMPTY(&lfp->lf_deleg))) {
1900 * Check for a conflicting delegation. If one is found, call
1901 * nfsrv_delegconflict() to handle it. If the v4root lock hasn't
1902 * been set yet, it will get the lock. Otherwise, it will recall
1903 * the delegation. Then, we try try again...
1904 * I currently believe the conflict algorithm to be:
1905 * For Lock Ops (Lock/LockT/LockU)
1906 * - there is a conflict iff a different client has a write delegation
1907 * For Reading (Read Op)
1908 * - there is a conflict iff a different client has a write delegation
1909 * (the specialids are always a different client)
1910 * For Writing (Write/Setattr of size)
1911 * - there is a conflict if a different client has any delegation
1912 * - there is a conflict if the same client has a read delegation
1913 * (I don't understand why this isn't allowed, but that seems to be
1914 * the current concensus?)
1916 tstp = LIST_FIRST(&lfp->lf_deleg);
1917 while (tstp != LIST_END(&lfp->lf_deleg)) {
1918 nstp = LIST_NEXT(tstp, ls_file);
1919 if ((((new_stp->ls_flags&(NFSLCK_LOCK|NFSLCK_UNLOCK|NFSLCK_TEST))||
1920 ((new_stp->ls_flags & NFSLCK_CHECK) &&
1921 (new_lop->lo_flags & NFSLCK_READ))) &&
1922 clp != tstp->ls_clp &&
1923 (tstp->ls_flags & NFSLCK_DELEGWRITE)) ||
1924 ((new_stp->ls_flags & NFSLCK_CHECK) &&
1925 (new_lop->lo_flags & NFSLCK_WRITE) &&
1926 (clp != tstp->ls_clp ||
1927 (tstp->ls_flags & NFSLCK_DELEGREAD)))) {
1928 if (filestruct_locked != 0) {
1929 /* Roll back local locks. */
1931 nfsrv_locallock_rollback(vp, lfp, p);
1933 nfsrv_unlocklf(lfp);
1935 ret = nfsrv_delegconflict(tstp, &haslock, p, vp);
1938 * nfsrv_delegconflict unlocks state when it
1939 * returns non-zero, which it always does.
1942 FREE((caddr_t)other_lop, M_NFSDLOCK);
1952 /* Never gets here. */
1958 * Handle the unlock case by calling nfsrv_updatelock().
1959 * (Should I have done some access checking above for unlock? For now,
1960 * just let it happen.)
1962 if (new_stp->ls_flags & NFSLCK_UNLOCK) {
1963 first = new_lop->lo_first;
1964 end = new_lop->lo_end;
1965 nfsrv_updatelock(stp, new_lopp, &other_lop, lfp);
1966 stateidp->seqid = ++(stp->ls_stateid.seqid);
1967 if ((nd->nd_flag & ND_NFSV41) != 0 && stateidp->seqid == 0)
1968 stateidp->seqid = stp->ls_stateid.seqid = 1;
1969 stateidp->other[0] = stp->ls_stateid.other[0];
1970 stateidp->other[1] = stp->ls_stateid.other[1];
1971 stateidp->other[2] = stp->ls_stateid.other[2];
1972 if (filestruct_locked != 0) {
1974 /* Update the local locks. */
1975 nfsrv_localunlock(vp, lfp, first, end, p);
1977 nfsrv_unlocklf(lfp);
1984 * Search for a conflicting lock. A lock conflicts if:
1985 * - the lock range overlaps and
1986 * - at least one lock is a write lock and
1987 * - it is not owned by the same lock owner
1990 LIST_FOREACH(lop, &lfp->lf_lock, lo_lckfile) {
1991 if (new_lop->lo_end > lop->lo_first &&
1992 new_lop->lo_first < lop->lo_end &&
1993 (new_lop->lo_flags == NFSLCK_WRITE ||
1994 lop->lo_flags == NFSLCK_WRITE) &&
1995 lckstp != lop->lo_stp &&
1996 (clp != lop->lo_stp->ls_clp ||
1997 lckstp->ls_ownerlen != lop->lo_stp->ls_ownerlen ||
1998 NFSBCMP(lckstp->ls_owner, lop->lo_stp->ls_owner,
1999 lckstp->ls_ownerlen))) {
2001 FREE((caddr_t)other_lop, M_NFSDLOCK);
2004 ret = nfsrv_clientconflict(lop->lo_stp->ls_clp,&haslock,vp,p);
2006 if (filestruct_locked != 0) {
2007 /* Roll back local locks. */
2008 nfsrv_locallock_rollback(vp, lfp, p);
2010 nfsrv_unlocklf(lfp);
2014 * nfsrv_clientconflict() unlocks state when it
2021 * Found a conflicting lock, so record the conflict and
2024 if (cfp != NULL && ret == 0) {
2025 cfp->cl_clientid.lval[0]=lop->lo_stp->ls_stateid.other[0];
2026 cfp->cl_clientid.lval[1]=lop->lo_stp->ls_stateid.other[1];
2027 cfp->cl_first = lop->lo_first;
2028 cfp->cl_end = lop->lo_end;
2029 cfp->cl_flags = lop->lo_flags;
2030 cfp->cl_ownerlen = lop->lo_stp->ls_ownerlen;
2031 NFSBCOPY(lop->lo_stp->ls_owner, cfp->cl_owner,
2035 error = NFSERR_PERM;
2036 else if (new_stp->ls_flags & NFSLCK_RECLAIM)
2037 error = NFSERR_RECLAIMCONFLICT;
2038 else if (new_stp->ls_flags & NFSLCK_CHECK)
2039 error = NFSERR_LOCKED;
2041 error = NFSERR_DENIED;
2042 if (filestruct_locked != 0 && ret == 0) {
2043 /* Roll back local locks. */
2045 nfsrv_locallock_rollback(vp, lfp, p);
2047 nfsrv_unlocklf(lfp);
2057 * We only get here if there was no lock that conflicted.
2059 if (new_stp->ls_flags & (NFSLCK_TEST | NFSLCK_CHECK)) {
2065 * We only get here when we are creating or modifying a lock.
2066 * There are two variants:
2067 * - exist_lock_owner where lock_owner exists
2068 * - open_to_lock_owner with new lock_owner
2070 first = new_lop->lo_first;
2071 end = new_lop->lo_end;
2072 lock_flags = new_lop->lo_flags;
2073 if (!(new_stp->ls_flags & NFSLCK_OPENTOLOCK)) {
2074 nfsrv_updatelock(lckstp, new_lopp, &other_lop, lfp);
2075 stateidp->seqid = ++(lckstp->ls_stateid.seqid);
2076 if ((nd->nd_flag & ND_NFSV41) != 0 && stateidp->seqid == 0)
2077 stateidp->seqid = lckstp->ls_stateid.seqid = 1;
2078 stateidp->other[0] = lckstp->ls_stateid.other[0];
2079 stateidp->other[1] = lckstp->ls_stateid.other[1];
2080 stateidp->other[2] = lckstp->ls_stateid.other[2];
2083 * The new open_to_lock_owner case.
2084 * Link the new nfsstate into the lists.
2086 new_stp->ls_seq = new_stp->ls_opentolockseq;
2087 nfsrvd_refcache(new_stp->ls_op);
2088 stateidp->seqid = new_stp->ls_stateid.seqid = 1;
2089 stateidp->other[0] = new_stp->ls_stateid.other[0] =
2090 clp->lc_clientid.lval[0];
2091 stateidp->other[1] = new_stp->ls_stateid.other[1] =
2092 clp->lc_clientid.lval[1];
2093 stateidp->other[2] = new_stp->ls_stateid.other[2] =
2094 nfsrv_nextstateindex(clp);
2095 new_stp->ls_clp = clp;
2096 LIST_INIT(&new_stp->ls_lock);
2097 new_stp->ls_openstp = stp;
2098 new_stp->ls_lfp = lfp;
2099 nfsrv_insertlock(new_lop, (struct nfslock *)new_stp, new_stp,
2101 LIST_INSERT_HEAD(NFSSTATEHASH(clp, new_stp->ls_stateid),
2103 LIST_INSERT_HEAD(&stp->ls_open, new_stp, ls_list);
2106 newnfsstats.srvlockowners++;
2107 nfsrv_openpluslock++;
2109 if (filestruct_locked != 0) {
2111 nfsrv_locallock_commit(lfp, lock_flags, first, end);
2113 nfsrv_unlocklf(lfp);
2119 NFSLOCKV4ROOTMUTEX();
2120 nfsv4_unlock(&nfsv4rootfs_lock, 1);
2121 NFSUNLOCKV4ROOTMUTEX();
2124 FREE((caddr_t)other_lop, M_NFSDLOCK);
2125 NFSEXITCODE2(error, nd);
2130 * Check for state errors for Open.
2131 * repstat is passed back out as an error if more critical errors
2135 nfsrv_opencheck(nfsquad_t clientid, nfsv4stateid_t *stateidp,
2136 struct nfsstate *new_stp, vnode_t vp, struct nfsrv_descript *nd,
2137 NFSPROC_T *p, int repstat)
2139 struct nfsstate *stp, *nstp;
2140 struct nfsclient *clp;
2141 struct nfsstate *ownerstp;
2142 struct nfslockfile *lfp, *new_lfp;
2143 int error = 0, haslock = 0, ret, readonly = 0, getfhret = 0;
2145 if ((new_stp->ls_flags & NFSLCK_SHAREBITS) == NFSLCK_READACCESS)
2148 * Check for restart conditions (client and server).
2150 error = nfsrv_checkrestart(clientid, new_stp->ls_flags,
2151 &new_stp->ls_stateid, 0);
2156 * Check for state resource limit exceeded.
2157 * Technically this should be SMP protected, but the worst
2158 * case error is "out by one or two" on the count when it
2159 * returns NFSERR_RESOURCE and the limit is just a rather
2160 * arbitrary high water mark, so no harm is done.
2162 if (nfsrv_openpluslock > NFSRV_V4STATELIMIT) {
2163 error = NFSERR_RESOURCE;
2168 MALLOC(new_lfp, struct nfslockfile *, sizeof (struct nfslockfile),
2169 M_NFSDLOCKFILE, M_WAITOK);
2171 getfhret = nfsrv_getlockfh(vp, new_stp->ls_flags, new_lfp,
2175 * Get the nfsclient structure.
2177 error = nfsrv_getclient(clientid, CLOPS_RENEW, &clp, NULL,
2178 (nfsquad_t)((u_quad_t)0), 0, nd, p);
2181 * Look up the open owner. See if it needs confirmation and
2182 * check the seq#, as required.
2185 nfsrv_getowner(&clp->lc_open, new_stp, &ownerstp);
2187 if (!error && ownerstp) {
2188 error = nfsrv_checkseqid(nd, new_stp->ls_seq, ownerstp,
2191 * If the OpenOwner hasn't been confirmed, assume the
2192 * old one was a replay and this one is ok.
2193 * See: RFC3530 Sec. 14.2.18.
2195 if (error == NFSERR_BADSEQID &&
2196 (ownerstp->ls_flags & NFSLCK_NEEDSCONFIRM))
2204 error = nfsrv_checkgrace(nd, clp, new_stp->ls_flags);
2205 if ((new_stp->ls_flags & NFSLCK_RECLAIM) && !error &&
2206 nfsrv_checkstable(clp))
2207 error = NFSERR_NOGRACE;
2210 * If none of the above errors occurred, let repstat be
2213 if (repstat && !error)
2218 NFSLOCKV4ROOTMUTEX();
2219 nfsv4_unlock(&nfsv4rootfs_lock, 1);
2220 NFSUNLOCKV4ROOTMUTEX();
2222 free((caddr_t)new_lfp, M_NFSDLOCKFILE);
2227 * If vp == NULL, the file doesn't exist yet, so return ok.
2228 * (This always happens on the first pass, so haslock must be 0.)
2232 FREE((caddr_t)new_lfp, M_NFSDLOCKFILE);
2237 * Get the structure for the underlying file.
2242 error = nfsrv_getlockfile(new_stp->ls_flags, &new_lfp, &lfp,
2245 FREE((caddr_t)new_lfp, M_NFSDLOCKFILE);
2249 NFSLOCKV4ROOTMUTEX();
2250 nfsv4_unlock(&nfsv4rootfs_lock, 1);
2251 NFSUNLOCKV4ROOTMUTEX();
2257 * Search for a conflicting open/share.
2259 if (new_stp->ls_flags & NFSLCK_DELEGCUR) {
2261 * For Delegate_Cur, search for the matching Delegation,
2262 * which indicates no conflict.
2263 * An old delegation should have been recovered by the
2264 * client doing a Claim_DELEGATE_Prev, so I won't let
2265 * it match and return NFSERR_EXPIRED. Should I let it
2268 LIST_FOREACH(stp, &lfp->lf_deleg, ls_file) {
2269 if (!(stp->ls_flags & NFSLCK_OLDDELEG) &&
2270 (((nd->nd_flag & ND_NFSV41) != 0 &&
2271 stateidp->seqid == 0) ||
2272 stateidp->seqid == stp->ls_stateid.seqid) &&
2273 !NFSBCMP(stateidp->other, stp->ls_stateid.other,
2277 if (stp == LIST_END(&lfp->lf_deleg) ||
2278 ((new_stp->ls_flags & NFSLCK_WRITEACCESS) &&
2279 (stp->ls_flags & NFSLCK_DELEGREAD))) {
2282 NFSLOCKV4ROOTMUTEX();
2283 nfsv4_unlock(&nfsv4rootfs_lock, 1);
2284 NFSUNLOCKV4ROOTMUTEX();
2286 error = NFSERR_EXPIRED;
2292 * Check for access/deny bit conflicts. I check for the same
2293 * owner as well, in case the client didn't bother.
2295 LIST_FOREACH(stp, &lfp->lf_open, ls_file) {
2296 if (!(new_stp->ls_flags & NFSLCK_DELEGCUR) &&
2297 (((new_stp->ls_flags & NFSLCK_ACCESSBITS) &
2298 ((stp->ls_flags>>NFSLCK_SHIFT) & NFSLCK_ACCESSBITS))||
2299 ((stp->ls_flags & NFSLCK_ACCESSBITS) &
2300 ((new_stp->ls_flags>>NFSLCK_SHIFT)&NFSLCK_ACCESSBITS)))){
2301 ret = nfsrv_clientconflict(stp->ls_clp,&haslock,vp,p);
2304 * nfsrv_clientconflict() unlocks
2305 * state when it returns non-zero.
2310 error = NFSERR_PERM;
2311 else if (new_stp->ls_flags & NFSLCK_RECLAIM)
2312 error = NFSERR_RECLAIMCONFLICT;
2314 error = NFSERR_SHAREDENIED;
2318 NFSLOCKV4ROOTMUTEX();
2319 nfsv4_unlock(&nfsv4rootfs_lock, 1);
2320 NFSUNLOCKV4ROOTMUTEX();
2327 * Check for a conflicting delegation. If one is found, call
2328 * nfsrv_delegconflict() to handle it. If the v4root lock hasn't
2329 * been set yet, it will get the lock. Otherwise, it will recall
2330 * the delegation. Then, we try try again...
2331 * (If NFSLCK_DELEGCUR is set, it has a delegation, so there
2332 * isn't a conflict.)
2333 * I currently believe the conflict algorithm to be:
2334 * For Open with Read Access and Deny None
2335 * - there is a conflict iff a different client has a write delegation
2336 * For Open with other Write Access or any Deny except None
2337 * - there is a conflict if a different client has any delegation
2338 * - there is a conflict if the same client has a read delegation
2339 * (The current concensus is that this last case should be
2340 * considered a conflict since the client with a read delegation
2341 * could have done an Open with ReadAccess and WriteDeny
2342 * locally and then not have checked for the WriteDeny.)
2343 * Don't check for a Reclaim, since that will be dealt with
2344 * by nfsrv_openctrl().
2346 if (!(new_stp->ls_flags &
2347 (NFSLCK_DELEGPREV | NFSLCK_DELEGCUR | NFSLCK_RECLAIM))) {
2348 stp = LIST_FIRST(&lfp->lf_deleg);
2349 while (stp != LIST_END(&lfp->lf_deleg)) {
2350 nstp = LIST_NEXT(stp, ls_file);
2351 if ((readonly && stp->ls_clp != clp &&
2352 (stp->ls_flags & NFSLCK_DELEGWRITE)) ||
2353 (!readonly && (stp->ls_clp != clp ||
2354 (stp->ls_flags & NFSLCK_DELEGREAD)))) {
2355 ret = nfsrv_delegconflict(stp, &haslock, p, vp);
2358 * nfsrv_delegconflict() unlocks state
2359 * when it returns non-zero.
2372 NFSLOCKV4ROOTMUTEX();
2373 nfsv4_unlock(&nfsv4rootfs_lock, 1);
2374 NFSUNLOCKV4ROOTMUTEX();
2378 NFSEXITCODE2(error, nd);
2383 * Open control function to create/update open state for an open.
2386 nfsrv_openctrl(struct nfsrv_descript *nd, vnode_t vp,
2387 struct nfsstate **new_stpp, nfsquad_t clientid, nfsv4stateid_t *stateidp,
2388 nfsv4stateid_t *delegstateidp, u_int32_t *rflagsp, struct nfsexstuff *exp,
2389 NFSPROC_T *p, u_quad_t filerev)
2391 struct nfsstate *new_stp = *new_stpp;
2392 struct nfsstate *stp, *nstp;
2393 struct nfsstate *openstp = NULL, *new_open, *ownerstp, *new_deleg;
2394 struct nfslockfile *lfp, *new_lfp;
2395 struct nfsclient *clp;
2396 int error = 0, haslock = 0, ret, delegate = 1, writedeleg = 1;
2397 int readonly = 0, cbret = 1, getfhret = 0;
2399 if ((new_stp->ls_flags & NFSLCK_SHAREBITS) == NFSLCK_READACCESS)
2402 * Check for restart conditions (client and server).
2403 * (Paranoia, should have been detected by nfsrv_opencheck().)
2404 * If an error does show up, return NFSERR_EXPIRED, since the
2405 * the seqid# has already been incremented.
2407 error = nfsrv_checkrestart(clientid, new_stp->ls_flags,
2408 &new_stp->ls_stateid, 0);
2410 printf("Nfsd: openctrl unexpected restart err=%d\n",
2412 error = NFSERR_EXPIRED;
2417 MALLOC(new_lfp, struct nfslockfile *, sizeof (struct nfslockfile),
2418 M_NFSDLOCKFILE, M_WAITOK);
2419 MALLOC(new_open, struct nfsstate *, sizeof (struct nfsstate),
2420 M_NFSDSTATE, M_WAITOK);
2421 MALLOC(new_deleg, struct nfsstate *, sizeof (struct nfsstate),
2422 M_NFSDSTATE, M_WAITOK);
2423 getfhret = nfsrv_getlockfh(vp, new_stp->ls_flags, new_lfp,
2427 * Get the client structure. Since the linked lists could be changed
2428 * by other nfsd processes if this process does a tsleep(), one of
2429 * two things must be done.
2430 * 1 - don't tsleep()
2432 * 2 - get the nfsv4_lock() { indicated by haslock == 1 }
2433 * before using the lists, since this lock stops the other
2434 * nfsd. This should only be used for rare cases, since it
2435 * essentially single threads the nfsd.
2436 * At this time, it is only done for cases where the stable
2437 * storage file must be written prior to completion of state
2440 error = nfsrv_getclient(clientid, CLOPS_RENEW, &clp, NULL,
2441 (nfsquad_t)((u_quad_t)0), 0, nd, p);
2442 if (!error && (clp->lc_flags & LCL_NEEDSCBNULL) &&
2445 * This happens on the first open for a client
2446 * that supports callbacks.
2450 * Although nfsrv_docallback() will sleep, clp won't
2451 * go away, since they are only removed when the
2452 * nfsv4_lock() has blocked the nfsd threads. The
2453 * fields in clp can change, but having multiple
2454 * threads do this Null callback RPC should be
2457 cbret = nfsrv_docallback(clp, NFSV4PROC_CBNULL,
2458 NULL, 0, NULL, NULL, NULL, p);
2460 clp->lc_flags &= ~LCL_NEEDSCBNULL;
2462 clp->lc_flags |= LCL_CALLBACKSON;
2466 * Look up the open owner. See if it needs confirmation and
2467 * check the seq#, as required.
2470 nfsrv_getowner(&clp->lc_open, new_stp, &ownerstp);
2474 printf("Nfsd: openctrl unexpected state err=%d\n",
2476 free((caddr_t)new_lfp, M_NFSDLOCKFILE);
2477 free((caddr_t)new_open, M_NFSDSTATE);
2478 free((caddr_t)new_deleg, M_NFSDSTATE);
2480 NFSLOCKV4ROOTMUTEX();
2481 nfsv4_unlock(&nfsv4rootfs_lock, 1);
2482 NFSUNLOCKV4ROOTMUTEX();
2484 error = NFSERR_EXPIRED;
2488 if (new_stp->ls_flags & NFSLCK_RECLAIM)
2489 nfsrv_markstable(clp);
2492 * Get the structure for the underlying file.
2497 error = nfsrv_getlockfile(new_stp->ls_flags, &new_lfp, &lfp,
2500 FREE((caddr_t)new_lfp, M_NFSDLOCKFILE);
2503 printf("Nfsd openctrl unexpected getlockfile err=%d\n",
2505 free((caddr_t)new_open, M_NFSDSTATE);
2506 free((caddr_t)new_deleg, M_NFSDSTATE);
2508 NFSLOCKV4ROOTMUTEX();
2509 nfsv4_unlock(&nfsv4rootfs_lock, 1);
2510 NFSUNLOCKV4ROOTMUTEX();
2516 * Search for a conflicting open/share.
2518 if (new_stp->ls_flags & NFSLCK_DELEGCUR) {
2520 * For Delegate_Cur, search for the matching Delegation,
2521 * which indicates no conflict.
2522 * An old delegation should have been recovered by the
2523 * client doing a Claim_DELEGATE_Prev, so I won't let
2524 * it match and return NFSERR_EXPIRED. Should I let it
2527 LIST_FOREACH(stp, &lfp->lf_deleg, ls_file) {
2528 if (!(stp->ls_flags & NFSLCK_OLDDELEG) &&
2529 (((nd->nd_flag & ND_NFSV41) != 0 &&
2530 stateidp->seqid == 0) ||
2531 stateidp->seqid == stp->ls_stateid.seqid) &&
2532 !NFSBCMP(stateidp->other, stp->ls_stateid.other,
2536 if (stp == LIST_END(&lfp->lf_deleg) ||
2537 ((new_stp->ls_flags & NFSLCK_WRITEACCESS) &&
2538 (stp->ls_flags & NFSLCK_DELEGREAD))) {
2540 printf("Nfsd openctrl unexpected expiry\n");
2541 free((caddr_t)new_open, M_NFSDSTATE);
2542 free((caddr_t)new_deleg, M_NFSDSTATE);
2544 NFSLOCKV4ROOTMUTEX();
2545 nfsv4_unlock(&nfsv4rootfs_lock, 1);
2546 NFSUNLOCKV4ROOTMUTEX();
2548 error = NFSERR_EXPIRED;
2553 * Don't issue a Delegation, since one already exists and
2554 * delay delegation timeout, as required.
2557 nfsrv_delaydelegtimeout(stp);
2561 * Check for access/deny bit conflicts. I also check for the
2562 * same owner, since the client might not have bothered to check.
2563 * Also, note an open for the same file and owner, if found,
2564 * which is all we do here for Delegate_Cur, since conflict
2565 * checking is already done.
2567 LIST_FOREACH(stp, &lfp->lf_open, ls_file) {
2568 if (ownerstp && stp->ls_openowner == ownerstp)
2570 if (!(new_stp->ls_flags & NFSLCK_DELEGCUR)) {
2572 * If another client has the file open, the only
2573 * delegation that can be issued is a Read delegation
2574 * and only if it is a Read open with Deny none.
2576 if (clp != stp->ls_clp) {
2577 if ((stp->ls_flags & NFSLCK_SHAREBITS) ==
2583 if(((new_stp->ls_flags & NFSLCK_ACCESSBITS) &
2584 ((stp->ls_flags>>NFSLCK_SHIFT) & NFSLCK_ACCESSBITS))||
2585 ((stp->ls_flags & NFSLCK_ACCESSBITS) &
2586 ((new_stp->ls_flags>>NFSLCK_SHIFT)&NFSLCK_ACCESSBITS))){
2587 ret = nfsrv_clientconflict(stp->ls_clp,&haslock,vp,p);
2590 * nfsrv_clientconflict() unlocks state
2591 * when it returns non-zero.
2593 free((caddr_t)new_open, M_NFSDSTATE);
2594 free((caddr_t)new_deleg, M_NFSDSTATE);
2599 error = NFSERR_PERM;
2600 else if (new_stp->ls_flags & NFSLCK_RECLAIM)
2601 error = NFSERR_RECLAIMCONFLICT;
2603 error = NFSERR_SHAREDENIED;
2607 NFSLOCKV4ROOTMUTEX();
2608 nfsv4_unlock(&nfsv4rootfs_lock, 1);
2609 NFSUNLOCKV4ROOTMUTEX();
2611 free((caddr_t)new_open, M_NFSDSTATE);
2612 free((caddr_t)new_deleg, M_NFSDSTATE);
2613 printf("nfsd openctrl unexpected client cnfl\n");
2620 * Check for a conflicting delegation. If one is found, call
2621 * nfsrv_delegconflict() to handle it. If the v4root lock hasn't
2622 * been set yet, it will get the lock. Otherwise, it will recall
2623 * the delegation. Then, we try try again...
2624 * (If NFSLCK_DELEGCUR is set, it has a delegation, so there
2625 * isn't a conflict.)
2626 * I currently believe the conflict algorithm to be:
2627 * For Open with Read Access and Deny None
2628 * - there is a conflict iff a different client has a write delegation
2629 * For Open with other Write Access or any Deny except None
2630 * - there is a conflict if a different client has any delegation
2631 * - there is a conflict if the same client has a read delegation
2632 * (The current concensus is that this last case should be
2633 * considered a conflict since the client with a read delegation
2634 * could have done an Open with ReadAccess and WriteDeny
2635 * locally and then not have checked for the WriteDeny.)
2637 if (!(new_stp->ls_flags & (NFSLCK_DELEGPREV | NFSLCK_DELEGCUR))) {
2638 stp = LIST_FIRST(&lfp->lf_deleg);
2639 while (stp != LIST_END(&lfp->lf_deleg)) {
2640 nstp = LIST_NEXT(stp, ls_file);
2641 if (stp->ls_clp != clp && (stp->ls_flags & NFSLCK_DELEGREAD))
2645 if ((readonly && stp->ls_clp != clp &&
2646 (stp->ls_flags & NFSLCK_DELEGWRITE)) ||
2647 (!readonly && (stp->ls_clp != clp ||
2648 (stp->ls_flags & NFSLCK_DELEGREAD)))) {
2649 if (new_stp->ls_flags & NFSLCK_RECLAIM) {
2652 ret = nfsrv_delegconflict(stp, &haslock, p, vp);
2655 * nfsrv_delegconflict() unlocks state
2656 * when it returns non-zero.
2658 printf("Nfsd openctrl unexpected deleg cnfl\n");
2659 free((caddr_t)new_open, M_NFSDSTATE);
2660 free((caddr_t)new_deleg, M_NFSDSTATE);
2675 * We only get here if there was no open that conflicted.
2676 * If an open for the owner exists, or in the access/deny bits.
2677 * Otherwise it is a new open. If the open_owner hasn't been
2678 * confirmed, replace the open with the new one needing confirmation,
2679 * otherwise add the open.
2681 if (new_stp->ls_flags & NFSLCK_DELEGPREV) {
2683 * Handle NFSLCK_DELEGPREV by searching the old delegations for
2684 * a match. If found, just move the old delegation to the current
2685 * delegation list and issue open. If not found, return
2688 LIST_FOREACH(stp, &clp->lc_olddeleg, ls_list) {
2689 if (stp->ls_lfp == lfp) {
2691 if (stp->ls_clp != clp)
2692 panic("olddeleg clp");
2693 LIST_REMOVE(stp, ls_list);
2694 LIST_REMOVE(stp, ls_hash);
2695 stp->ls_flags &= ~NFSLCK_OLDDELEG;
2696 stp->ls_stateid.seqid = delegstateidp->seqid = 1;
2697 stp->ls_stateid.other[0] = delegstateidp->other[0] =
2698 clp->lc_clientid.lval[0];
2699 stp->ls_stateid.other[1] = delegstateidp->other[1] =
2700 clp->lc_clientid.lval[1];
2701 stp->ls_stateid.other[2] = delegstateidp->other[2] =
2702 nfsrv_nextstateindex(clp);
2703 stp->ls_compref = nd->nd_compref;
2704 LIST_INSERT_HEAD(&clp->lc_deleg, stp, ls_list);
2705 LIST_INSERT_HEAD(NFSSTATEHASH(clp,
2706 stp->ls_stateid), stp, ls_hash);
2707 if (stp->ls_flags & NFSLCK_DELEGWRITE)
2708 *rflagsp |= NFSV4OPEN_WRITEDELEGATE;
2710 *rflagsp |= NFSV4OPEN_READDELEGATE;
2711 clp->lc_delegtime = NFSD_MONOSEC +
2712 nfsrv_lease + NFSRV_LEASEDELTA;
2715 * Now, do the associated open.
2717 new_open->ls_stateid.seqid = 1;
2718 new_open->ls_stateid.other[0] = clp->lc_clientid.lval[0];
2719 new_open->ls_stateid.other[1] = clp->lc_clientid.lval[1];
2720 new_open->ls_stateid.other[2] = nfsrv_nextstateindex(clp);
2721 new_open->ls_flags = (new_stp->ls_flags&NFSLCK_DENYBITS)|
2723 if (stp->ls_flags & NFSLCK_DELEGWRITE)
2724 new_open->ls_flags |= (NFSLCK_READACCESS |
2725 NFSLCK_WRITEACCESS);
2727 new_open->ls_flags |= NFSLCK_READACCESS;
2728 new_open->ls_uid = new_stp->ls_uid;
2729 new_open->ls_lfp = lfp;
2730 new_open->ls_clp = clp;
2731 LIST_INIT(&new_open->ls_open);
2732 LIST_INSERT_HEAD(&lfp->lf_open, new_open, ls_file);
2733 LIST_INSERT_HEAD(NFSSTATEHASH(clp, new_open->ls_stateid),
2736 * and handle the open owner
2739 new_open->ls_openowner = ownerstp;
2740 LIST_INSERT_HEAD(&ownerstp->ls_open,new_open,ls_list);
2742 new_open->ls_openowner = new_stp;
2743 new_stp->ls_flags = 0;
2744 nfsrvd_refcache(new_stp->ls_op);
2745 new_stp->ls_noopens = 0;
2746 LIST_INIT(&new_stp->ls_open);
2747 LIST_INSERT_HEAD(&new_stp->ls_open, new_open, ls_list);
2748 LIST_INSERT_HEAD(&clp->lc_open, new_stp, ls_list);
2750 newnfsstats.srvopenowners++;
2751 nfsrv_openpluslock++;
2755 newnfsstats.srvopens++;
2756 nfsrv_openpluslock++;
2760 if (stp == LIST_END(&clp->lc_olddeleg))
2761 error = NFSERR_EXPIRED;
2762 } else if (new_stp->ls_flags & (NFSLCK_DELEGREAD | NFSLCK_DELEGWRITE)) {
2764 * Scan to see that no delegation for this client and file
2765 * doesn't already exist.
2766 * There also shouldn't yet be an Open for this file and
2769 LIST_FOREACH(stp, &lfp->lf_deleg, ls_file) {
2770 if (stp->ls_clp == clp)
2773 if (stp == LIST_END(&lfp->lf_deleg) && openstp == NULL) {
2775 * This is the Claim_Previous case with a delegation
2776 * type != Delegate_None.
2779 * First, add the delegation. (Although we must issue the
2780 * delegation, we can also ask for an immediate return.)
2782 new_deleg->ls_stateid.seqid = delegstateidp->seqid = 1;
2783 new_deleg->ls_stateid.other[0] = delegstateidp->other[0] =
2784 clp->lc_clientid.lval[0];
2785 new_deleg->ls_stateid.other[1] = delegstateidp->other[1] =
2786 clp->lc_clientid.lval[1];
2787 new_deleg->ls_stateid.other[2] = delegstateidp->other[2] =
2788 nfsrv_nextstateindex(clp);
2789 if (new_stp->ls_flags & NFSLCK_DELEGWRITE) {
2790 new_deleg->ls_flags = (NFSLCK_DELEGWRITE |
2791 NFSLCK_READACCESS | NFSLCK_WRITEACCESS);
2792 *rflagsp |= NFSV4OPEN_WRITEDELEGATE;
2794 new_deleg->ls_flags = (NFSLCK_DELEGREAD |
2796 *rflagsp |= NFSV4OPEN_READDELEGATE;
2798 new_deleg->ls_uid = new_stp->ls_uid;
2799 new_deleg->ls_lfp = lfp;
2800 new_deleg->ls_clp = clp;
2801 new_deleg->ls_filerev = filerev;
2802 new_deleg->ls_compref = nd->nd_compref;
2803 LIST_INSERT_HEAD(&lfp->lf_deleg, new_deleg, ls_file);
2804 LIST_INSERT_HEAD(NFSSTATEHASH(clp,
2805 new_deleg->ls_stateid), new_deleg, ls_hash);
2806 LIST_INSERT_HEAD(&clp->lc_deleg, new_deleg, ls_list);
2808 if (delegate == 2 || nfsrv_issuedelegs == 0 ||
2809 (clp->lc_flags & (LCL_CALLBACKSON | LCL_CBDOWN)) !=
2811 NFSRV_V4DELEGLIMIT(nfsrv_delegatecnt) ||
2812 !NFSVNO_DELEGOK(vp))
2813 *rflagsp |= NFSV4OPEN_RECALL;
2814 newnfsstats.srvdelegates++;
2815 nfsrv_openpluslock++;
2816 nfsrv_delegatecnt++;
2819 * Now, do the associated open.
2821 new_open->ls_stateid.seqid = 1;
2822 new_open->ls_stateid.other[0] = clp->lc_clientid.lval[0];
2823 new_open->ls_stateid.other[1] = clp->lc_clientid.lval[1];
2824 new_open->ls_stateid.other[2] = nfsrv_nextstateindex(clp);
2825 new_open->ls_flags = (new_stp->ls_flags & NFSLCK_DENYBITS) |
2827 if (new_stp->ls_flags & NFSLCK_DELEGWRITE)
2828 new_open->ls_flags |= (NFSLCK_READACCESS |
2829 NFSLCK_WRITEACCESS);
2831 new_open->ls_flags |= NFSLCK_READACCESS;
2832 new_open->ls_uid = new_stp->ls_uid;
2833 new_open->ls_lfp = lfp;
2834 new_open->ls_clp = clp;
2835 LIST_INIT(&new_open->ls_open);
2836 LIST_INSERT_HEAD(&lfp->lf_open, new_open, ls_file);
2837 LIST_INSERT_HEAD(NFSSTATEHASH(clp, new_open->ls_stateid),
2840 * and handle the open owner
2843 new_open->ls_openowner = ownerstp;
2844 LIST_INSERT_HEAD(&ownerstp->ls_open, new_open, ls_list);
2846 new_open->ls_openowner = new_stp;
2847 new_stp->ls_flags = 0;
2848 nfsrvd_refcache(new_stp->ls_op);
2849 new_stp->ls_noopens = 0;
2850 LIST_INIT(&new_stp->ls_open);
2851 LIST_INSERT_HEAD(&new_stp->ls_open, new_open, ls_list);
2852 LIST_INSERT_HEAD(&clp->lc_open, new_stp, ls_list);
2854 newnfsstats.srvopenowners++;
2855 nfsrv_openpluslock++;
2859 newnfsstats.srvopens++;
2860 nfsrv_openpluslock++;
2862 error = NFSERR_RECLAIMCONFLICT;
2864 } else if (ownerstp) {
2865 if (ownerstp->ls_flags & NFSLCK_NEEDSCONFIRM) {
2866 /* Replace the open */
2867 if (ownerstp->ls_op)
2868 nfsrvd_derefcache(ownerstp->ls_op);
2869 ownerstp->ls_op = new_stp->ls_op;
2870 nfsrvd_refcache(ownerstp->ls_op);
2871 ownerstp->ls_seq = new_stp->ls_seq;
2872 *rflagsp |= NFSV4OPEN_RESULTCONFIRM;
2873 stp = LIST_FIRST(&ownerstp->ls_open);
2874 stp->ls_flags = (new_stp->ls_flags & NFSLCK_SHAREBITS) |
2876 stp->ls_stateid.seqid = 1;
2877 stp->ls_uid = new_stp->ls_uid;
2878 if (lfp != stp->ls_lfp) {
2879 LIST_REMOVE(stp, ls_file);
2880 LIST_INSERT_HEAD(&lfp->lf_open, stp, ls_file);
2884 } else if (openstp) {
2885 openstp->ls_flags |= (new_stp->ls_flags & NFSLCK_SHAREBITS);
2886 openstp->ls_stateid.seqid++;
2887 if ((nd->nd_flag & ND_NFSV41) != 0 &&
2888 openstp->ls_stateid.seqid == 0)
2889 openstp->ls_stateid.seqid = 1;
2892 * This is where we can choose to issue a delegation.
2894 if (delegate == 0 || writedeleg == 0 ||
2895 NFSVNO_EXRDONLY(exp) || (readonly != 0 &&
2896 nfsrv_writedelegifpos == 0) ||
2897 !NFSVNO_DELEGOK(vp) ||
2898 (new_stp->ls_flags & NFSLCK_WANTRDELEG) != 0 ||
2899 (clp->lc_flags & (LCL_CALLBACKSON | LCL_CBDOWN)) !=
2901 *rflagsp |= NFSV4OPEN_WDCONTENTION;
2902 else if (nfsrv_issuedelegs == 0 ||
2903 NFSRV_V4DELEGLIMIT(nfsrv_delegatecnt))
2904 *rflagsp |= NFSV4OPEN_WDRESOURCE;
2905 else if ((new_stp->ls_flags & NFSLCK_WANTNODELEG) != 0)
2906 *rflagsp |= NFSV4OPEN_WDNOTWANTED;
2908 new_deleg->ls_stateid.seqid = delegstateidp->seqid = 1;
2909 new_deleg->ls_stateid.other[0] = delegstateidp->other[0]
2910 = clp->lc_clientid.lval[0];
2911 new_deleg->ls_stateid.other[1] = delegstateidp->other[1]
2912 = clp->lc_clientid.lval[1];
2913 new_deleg->ls_stateid.other[2] = delegstateidp->other[2]
2914 = nfsrv_nextstateindex(clp);
2915 new_deleg->ls_flags = (NFSLCK_DELEGWRITE |
2916 NFSLCK_READACCESS | NFSLCK_WRITEACCESS);
2917 *rflagsp |= NFSV4OPEN_WRITEDELEGATE;
2918 new_deleg->ls_uid = new_stp->ls_uid;
2919 new_deleg->ls_lfp = lfp;
2920 new_deleg->ls_clp = clp;
2921 new_deleg->ls_filerev = filerev;
2922 new_deleg->ls_compref = nd->nd_compref;
2923 LIST_INSERT_HEAD(&lfp->lf_deleg, new_deleg, ls_file);
2924 LIST_INSERT_HEAD(NFSSTATEHASH(clp,
2925 new_deleg->ls_stateid), new_deleg, ls_hash);
2926 LIST_INSERT_HEAD(&clp->lc_deleg, new_deleg, ls_list);
2928 newnfsstats.srvdelegates++;
2929 nfsrv_openpluslock++;
2930 nfsrv_delegatecnt++;
2933 new_open->ls_stateid.seqid = 1;
2934 new_open->ls_stateid.other[0] = clp->lc_clientid.lval[0];
2935 new_open->ls_stateid.other[1] = clp->lc_clientid.lval[1];
2936 new_open->ls_stateid.other[2] = nfsrv_nextstateindex(clp);
2937 new_open->ls_flags = (new_stp->ls_flags & NFSLCK_SHAREBITS)|
2939 new_open->ls_uid = new_stp->ls_uid;
2940 new_open->ls_openowner = ownerstp;
2941 new_open->ls_lfp = lfp;
2942 new_open->ls_clp = clp;
2943 LIST_INIT(&new_open->ls_open);
2944 LIST_INSERT_HEAD(&lfp->lf_open, new_open, ls_file);
2945 LIST_INSERT_HEAD(&ownerstp->ls_open, new_open, ls_list);
2946 LIST_INSERT_HEAD(NFSSTATEHASH(clp, new_open->ls_stateid),
2950 newnfsstats.srvopens++;
2951 nfsrv_openpluslock++;
2954 * This is where we can choose to issue a delegation.
2956 if (delegate == 0 || (writedeleg == 0 && readonly == 0) ||
2957 !NFSVNO_DELEGOK(vp) ||
2958 (clp->lc_flags & (LCL_CALLBACKSON | LCL_CBDOWN)) !=
2960 *rflagsp |= NFSV4OPEN_WDCONTENTION;
2961 else if (nfsrv_issuedelegs == 0 ||
2962 NFSRV_V4DELEGLIMIT(nfsrv_delegatecnt))
2963 *rflagsp |= NFSV4OPEN_WDRESOURCE;
2964 else if ((new_stp->ls_flags & NFSLCK_WANTNODELEG) != 0)
2965 *rflagsp |= NFSV4OPEN_WDNOTWANTED;
2967 new_deleg->ls_stateid.seqid = delegstateidp->seqid = 1;
2968 new_deleg->ls_stateid.other[0] = delegstateidp->other[0]
2969 = clp->lc_clientid.lval[0];
2970 new_deleg->ls_stateid.other[1] = delegstateidp->other[1]
2971 = clp->lc_clientid.lval[1];
2972 new_deleg->ls_stateid.other[2] = delegstateidp->other[2]
2973 = nfsrv_nextstateindex(clp);
2974 if (writedeleg && !NFSVNO_EXRDONLY(exp) &&
2975 (nfsrv_writedelegifpos || !readonly) &&
2976 (new_stp->ls_flags & NFSLCK_WANTRDELEG) == 0) {
2977 new_deleg->ls_flags = (NFSLCK_DELEGWRITE |
2978 NFSLCK_READACCESS | NFSLCK_WRITEACCESS);
2979 *rflagsp |= NFSV4OPEN_WRITEDELEGATE;
2981 new_deleg->ls_flags = (NFSLCK_DELEGREAD |
2983 *rflagsp |= NFSV4OPEN_READDELEGATE;
2985 new_deleg->ls_uid = new_stp->ls_uid;
2986 new_deleg->ls_lfp = lfp;
2987 new_deleg->ls_clp = clp;
2988 new_deleg->ls_filerev = filerev;
2989 new_deleg->ls_compref = nd->nd_compref;
2990 LIST_INSERT_HEAD(&lfp->lf_deleg, new_deleg, ls_file);
2991 LIST_INSERT_HEAD(NFSSTATEHASH(clp,
2992 new_deleg->ls_stateid), new_deleg, ls_hash);
2993 LIST_INSERT_HEAD(&clp->lc_deleg, new_deleg, ls_list);
2995 newnfsstats.srvdelegates++;
2996 nfsrv_openpluslock++;
2997 nfsrv_delegatecnt++;
3002 * New owner case. Start the open_owner sequence with a
3003 * Needs confirmation (unless a reclaim) and hang the
3006 new_open->ls_stateid.seqid = 1;
3007 new_open->ls_stateid.other[0] = clp->lc_clientid.lval[0];
3008 new_open->ls_stateid.other[1] = clp->lc_clientid.lval[1];
3009 new_open->ls_stateid.other[2] = nfsrv_nextstateindex(clp);
3010 new_open->ls_flags = (new_stp->ls_flags & NFSLCK_SHAREBITS) |
3012 new_open->ls_uid = new_stp->ls_uid;
3013 LIST_INIT(&new_open->ls_open);
3014 new_open->ls_openowner = new_stp;
3015 new_open->ls_lfp = lfp;
3016 new_open->ls_clp = clp;
3017 LIST_INSERT_HEAD(&lfp->lf_open, new_open, ls_file);
3018 if (new_stp->ls_flags & NFSLCK_RECLAIM) {
3019 new_stp->ls_flags = 0;
3020 } else if ((nd->nd_flag & ND_NFSV41) != 0) {
3021 /* NFSv4.1 never needs confirmation. */
3022 new_stp->ls_flags = 0;
3025 * This is where we can choose to issue a delegation.
3027 if (delegate && nfsrv_issuedelegs &&
3028 (writedeleg || readonly) &&
3029 (clp->lc_flags & (LCL_CALLBACKSON | LCL_CBDOWN)) ==
3031 !NFSRV_V4DELEGLIMIT(nfsrv_delegatecnt) &&
3032 NFSVNO_DELEGOK(vp) &&
3033 ((nd->nd_flag & ND_NFSV41) == 0 ||
3034 (new_stp->ls_flags & NFSLCK_WANTNODELEG) == 0)) {
3035 new_deleg->ls_stateid.seqid =
3036 delegstateidp->seqid = 1;
3037 new_deleg->ls_stateid.other[0] =
3038 delegstateidp->other[0]
3039 = clp->lc_clientid.lval[0];
3040 new_deleg->ls_stateid.other[1] =
3041 delegstateidp->other[1]
3042 = clp->lc_clientid.lval[1];
3043 new_deleg->ls_stateid.other[2] =
3044 delegstateidp->other[2]
3045 = nfsrv_nextstateindex(clp);
3046 if (writedeleg && !NFSVNO_EXRDONLY(exp) &&
3047 (nfsrv_writedelegifpos || !readonly) &&
3048 ((nd->nd_flag & ND_NFSV41) == 0 ||
3049 (new_stp->ls_flags & NFSLCK_WANTRDELEG) ==
3051 new_deleg->ls_flags =
3052 (NFSLCK_DELEGWRITE |
3054 NFSLCK_WRITEACCESS);
3055 *rflagsp |= NFSV4OPEN_WRITEDELEGATE;
3057 new_deleg->ls_flags =
3060 *rflagsp |= NFSV4OPEN_READDELEGATE;
3062 new_deleg->ls_uid = new_stp->ls_uid;
3063 new_deleg->ls_lfp = lfp;
3064 new_deleg->ls_clp = clp;
3065 new_deleg->ls_filerev = filerev;
3066 new_deleg->ls_compref = nd->nd_compref;
3067 LIST_INSERT_HEAD(&lfp->lf_deleg, new_deleg,
3069 LIST_INSERT_HEAD(NFSSTATEHASH(clp,
3070 new_deleg->ls_stateid), new_deleg, ls_hash);
3071 LIST_INSERT_HEAD(&clp->lc_deleg, new_deleg,
3074 newnfsstats.srvdelegates++;
3075 nfsrv_openpluslock++;
3076 nfsrv_delegatecnt++;
3079 *rflagsp |= NFSV4OPEN_RESULTCONFIRM;
3080 new_stp->ls_flags = NFSLCK_NEEDSCONFIRM;
3082 nfsrvd_refcache(new_stp->ls_op);
3083 new_stp->ls_noopens = 0;
3084 LIST_INIT(&new_stp->ls_open);
3085 LIST_INSERT_HEAD(&new_stp->ls_open, new_open, ls_list);
3086 LIST_INSERT_HEAD(&clp->lc_open, new_stp, ls_list);
3087 LIST_INSERT_HEAD(NFSSTATEHASH(clp, new_open->ls_stateid),
3092 newnfsstats.srvopens++;
3093 nfsrv_openpluslock++;
3094 newnfsstats.srvopenowners++;
3095 nfsrv_openpluslock++;
3098 stateidp->seqid = openstp->ls_stateid.seqid;
3099 stateidp->other[0] = openstp->ls_stateid.other[0];
3100 stateidp->other[1] = openstp->ls_stateid.other[1];
3101 stateidp->other[2] = openstp->ls_stateid.other[2];
3105 NFSLOCKV4ROOTMUTEX();
3106 nfsv4_unlock(&nfsv4rootfs_lock, 1);
3107 NFSUNLOCKV4ROOTMUTEX();
3110 FREE((caddr_t)new_open, M_NFSDSTATE);
3112 FREE((caddr_t)new_deleg, M_NFSDSTATE);
3115 NFSEXITCODE2(error, nd);
3120 * Open update. Does the confirm, downgrade and close.
3123 nfsrv_openupdate(vnode_t vp, struct nfsstate *new_stp, nfsquad_t clientid,
3124 nfsv4stateid_t *stateidp, struct nfsrv_descript *nd, NFSPROC_T *p)
3126 struct nfsstate *stp, *ownerstp;
3127 struct nfsclient *clp;
3128 struct nfslockfile *lfp;
3130 int error = 0, gotstate = 0, len = 0;
3131 u_char client[NFSV4_OPAQUELIMIT];
3134 * Check for restart conditions (client and server).
3136 error = nfsrv_checkrestart(clientid, new_stp->ls_flags,
3137 &new_stp->ls_stateid, 0);
3143 * Get the open structure via clientid and stateid.
3145 error = nfsrv_getclient(clientid, CLOPS_RENEW, &clp, NULL,
3146 (nfsquad_t)((u_quad_t)0), 0, nd, p);
3148 error = nfsrv_getstate(clp, &new_stp->ls_stateid,
3149 new_stp->ls_flags, &stp);
3152 * Sanity check the open.
3154 if (!error && (!(stp->ls_flags & NFSLCK_OPEN) ||
3155 (!(new_stp->ls_flags & NFSLCK_CONFIRM) &&
3156 (stp->ls_openowner->ls_flags & NFSLCK_NEEDSCONFIRM)) ||
3157 ((new_stp->ls_flags & NFSLCK_CONFIRM) &&
3158 (!(stp->ls_openowner->ls_flags & NFSLCK_NEEDSCONFIRM)))))
3159 error = NFSERR_BADSTATEID;
3162 error = nfsrv_checkseqid(nd, new_stp->ls_seq,
3163 stp->ls_openowner, new_stp->ls_op);
3164 if (!error && stp->ls_stateid.seqid != new_stp->ls_stateid.seqid &&
3165 (((nd->nd_flag & ND_NFSV41) == 0 &&
3166 !(new_stp->ls_flags & NFSLCK_CONFIRM)) ||
3167 ((nd->nd_flag & ND_NFSV41) != 0 &&
3168 new_stp->ls_stateid.seqid != 0)))
3169 error = NFSERR_OLDSTATEID;
3170 if (!error && vnode_vtype(vp) != VREG) {
3171 if (vnode_vtype(vp) == VDIR)
3172 error = NFSERR_ISDIR;
3174 error = NFSERR_INVAL;
3179 * If a client tries to confirm an Open with a bad
3180 * seqid# and there are no byte range locks or other Opens
3181 * on the openowner, just throw it away, so the next use of the
3182 * openowner will start a fresh seq#.
3184 if (error == NFSERR_BADSEQID &&
3185 (new_stp->ls_flags & NFSLCK_CONFIRM) &&
3186 nfsrv_nootherstate(stp))
3187 nfsrv_freeopenowner(stp->ls_openowner, 0, p);
3193 * Set the return stateid.
3195 stateidp->seqid = stp->ls_stateid.seqid + 1;
3196 if ((nd->nd_flag & ND_NFSV41) != 0 && stateidp->seqid == 0)
3197 stateidp->seqid = 1;
3198 stateidp->other[0] = stp->ls_stateid.other[0];
3199 stateidp->other[1] = stp->ls_stateid.other[1];
3200 stateidp->other[2] = stp->ls_stateid.other[2];
3202 * Now, handle the three cases.
3204 if (new_stp->ls_flags & NFSLCK_CONFIRM) {
3206 * If the open doesn't need confirmation, it seems to me that
3207 * there is a client error, but I'll just log it and keep going?
3209 if (!(stp->ls_openowner->ls_flags & NFSLCK_NEEDSCONFIRM))
3210 printf("Nfsv4d: stray open confirm\n");
3211 stp->ls_openowner->ls_flags = 0;
3212 stp->ls_stateid.seqid++;
3213 if ((nd->nd_flag & ND_NFSV41) != 0 &&
3214 stp->ls_stateid.seqid == 0)
3215 stp->ls_stateid.seqid = 1;
3216 if (!(clp->lc_flags & LCL_STAMPEDSTABLE)) {
3217 clp->lc_flags |= LCL_STAMPEDSTABLE;
3218 len = clp->lc_idlen;
3219 NFSBCOPY(clp->lc_id, client, len);
3223 } else if (new_stp->ls_flags & NFSLCK_CLOSE) {
3224 ownerstp = stp->ls_openowner;
3226 if (nfsrv_dolocallocks != 0 && !LIST_EMPTY(&stp->ls_open)) {
3227 /* Get the lf lock */
3230 if (nfsrv_freeopen(stp, vp, 1, p) == 0) {
3232 nfsrv_unlocklf(lfp);
3236 (void) nfsrv_freeopen(stp, NULL, 0, p);
3241 * Update the share bits, making sure that the new set are a
3242 * subset of the old ones.
3244 bits = (new_stp->ls_flags & NFSLCK_SHAREBITS);
3245 if (~(stp->ls_flags) & bits) {
3247 error = NFSERR_INVAL;
3250 stp->ls_flags = (bits | NFSLCK_OPEN);
3251 stp->ls_stateid.seqid++;
3252 if ((nd->nd_flag & ND_NFSV41) != 0 &&
3253 stp->ls_stateid.seqid == 0)
3254 stp->ls_stateid.seqid = 1;
3259 * If the client just confirmed its first open, write a timestamp
3260 * to the stable storage file.
3262 if (gotstate != 0) {
3263 nfsrv_writestable(client, len, NFSNST_NEWSTATE, p);
3264 nfsrv_backupstable();
3268 NFSEXITCODE2(error, nd);
3273 * Delegation update. Does the purge and return.
3276 nfsrv_delegupdate(struct nfsrv_descript *nd, nfsquad_t clientid,
3277 nfsv4stateid_t *stateidp, vnode_t vp, int op, struct ucred *cred,
3280 struct nfsstate *stp;
3281 struct nfsclient *clp;
3286 * Do a sanity check against the file handle for DelegReturn.
3289 error = nfsvno_getfh(vp, &fh, p);
3294 * Check for restart conditions (client and server).
3296 if (op == NFSV4OP_DELEGRETURN)
3297 error = nfsrv_checkrestart(clientid, NFSLCK_DELEGRETURN,
3300 error = nfsrv_checkrestart(clientid, NFSLCK_DELEGPURGE,
3305 * Get the open structure via clientid and stateid.
3308 error = nfsrv_getclient(clientid, CLOPS_RENEW, &clp, NULL,
3309 (nfsquad_t)((u_quad_t)0), 0, nd, p);
3311 if (error == NFSERR_CBPATHDOWN)
3313 if (error == NFSERR_STALECLIENTID && op == NFSV4OP_DELEGRETURN)
3314 error = NFSERR_STALESTATEID;
3316 if (!error && op == NFSV4OP_DELEGRETURN) {
3317 error = nfsrv_getstate(clp, stateidp, NFSLCK_DELEGRETURN, &stp);
3318 if (!error && stp->ls_stateid.seqid != stateidp->seqid &&
3319 ((nd->nd_flag & ND_NFSV41) == 0 || stateidp->seqid != 0))
3320 error = NFSERR_OLDSTATEID;
3323 * NFSERR_EXPIRED means that the state has gone away,
3324 * so Delegations have been purged. Just return ok.
3326 if (error == NFSERR_EXPIRED && op == NFSV4OP_DELEGPURGE) {
3336 if (op == NFSV4OP_DELEGRETURN) {
3337 if (NFSBCMP((caddr_t)&fh, (caddr_t)&stp->ls_lfp->lf_fh,
3338 sizeof (fhandle_t))) {
3340 error = NFSERR_BADSTATEID;
3343 nfsrv_freedeleg(stp);
3345 nfsrv_freedeleglist(&clp->lc_olddeleg);
3356 * Release lock owner.
3359 nfsrv_releaselckown(struct nfsstate *new_stp, nfsquad_t clientid,
3362 struct nfsstate *stp, *nstp, *openstp, *ownstp;
3363 struct nfsclient *clp;
3367 * Check for restart conditions (client and server).
3369 error = nfsrv_checkrestart(clientid, new_stp->ls_flags,
3370 &new_stp->ls_stateid, 0);
3376 * Get the lock owner by name.
3378 error = nfsrv_getclient(clientid, CLOPS_RENEW, &clp, NULL,
3379 (nfsquad_t)((u_quad_t)0), 0, NULL, p);
3384 LIST_FOREACH(ownstp, &clp->lc_open, ls_list) {
3385 LIST_FOREACH(openstp, &ownstp->ls_open, ls_list) {
3386 stp = LIST_FIRST(&openstp->ls_open);
3387 while (stp != LIST_END(&openstp->ls_open)) {
3388 nstp = LIST_NEXT(stp, ls_list);
3390 * If the owner matches, check for locks and
3391 * then free or return an error.
3393 if (stp->ls_ownerlen == new_stp->ls_ownerlen &&
3394 !NFSBCMP(stp->ls_owner, new_stp->ls_owner,
3396 if (LIST_EMPTY(&stp->ls_lock)) {
3397 nfsrv_freelockowner(stp, NULL, 0, p);
3400 error = NFSERR_LOCKSHELD;
3416 * Get the file handle for a lock structure.
3419 nfsrv_getlockfh(vnode_t vp, u_short flags, struct nfslockfile *new_lfp,
3420 fhandle_t *nfhp, NFSPROC_T *p)
3422 fhandle_t *fhp = NULL;
3426 * For lock, use the new nfslock structure, otherwise just
3427 * a fhandle_t on the stack.
3429 if (flags & NFSLCK_OPEN) {
3430 KASSERT(new_lfp != NULL, ("nfsrv_getlockfh: new_lfp NULL"));
3431 fhp = &new_lfp->lf_fh;
3435 panic("nfsrv_getlockfh");
3437 error = nfsvno_getfh(vp, fhp, p);
3443 * Get an nfs lock structure. Allocate one, as required, and return a
3445 * Returns an NFSERR_xxx upon failure or -1 to indicate no current lock.
3448 nfsrv_getlockfile(u_short flags, struct nfslockfile **new_lfpp,
3449 struct nfslockfile **lfpp, fhandle_t *nfhp, int lockit)
3451 struct nfslockfile *lfp;
3452 fhandle_t *fhp = NULL, *tfhp;
3453 struct nfslockhashhead *hp;
3454 struct nfslockfile *new_lfp = NULL;
3457 * For lock, use the new nfslock structure, otherwise just
3458 * a fhandle_t on the stack.
3460 if (flags & NFSLCK_OPEN) {
3461 new_lfp = *new_lfpp;
3462 fhp = &new_lfp->lf_fh;
3466 panic("nfsrv_getlockfile");
3469 hp = NFSLOCKHASH(fhp);
3470 LIST_FOREACH(lfp, hp, lf_hash) {
3472 if (NFSVNO_CMPFH(fhp, tfhp)) {
3479 if (!(flags & NFSLCK_OPEN))
3483 * No match, so chain the new one into the list.
3485 LIST_INIT(&new_lfp->lf_open);
3486 LIST_INIT(&new_lfp->lf_lock);
3487 LIST_INIT(&new_lfp->lf_deleg);
3488 LIST_INIT(&new_lfp->lf_locallock);
3489 LIST_INIT(&new_lfp->lf_rollback);
3490 new_lfp->lf_locallock_lck.nfslock_usecnt = 0;
3491 new_lfp->lf_locallock_lck.nfslock_lock = 0;
3492 new_lfp->lf_usecount = 0;
3493 LIST_INSERT_HEAD(hp, new_lfp, lf_hash);
3500 * This function adds a nfslock lock structure to the list for the associated
3501 * nfsstate and nfslockfile structures. It will be inserted after the
3502 * entry pointed at by insert_lop.
3505 nfsrv_insertlock(struct nfslock *new_lop, struct nfslock *insert_lop,
3506 struct nfsstate *stp, struct nfslockfile *lfp)
3508 struct nfslock *lop, *nlop;
3510 new_lop->lo_stp = stp;
3511 new_lop->lo_lfp = lfp;
3514 /* Insert in increasing lo_first order */
3515 lop = LIST_FIRST(&lfp->lf_lock);
3516 if (lop == LIST_END(&lfp->lf_lock) ||
3517 new_lop->lo_first <= lop->lo_first) {
3518 LIST_INSERT_HEAD(&lfp->lf_lock, new_lop, lo_lckfile);
3520 nlop = LIST_NEXT(lop, lo_lckfile);
3521 while (nlop != LIST_END(&lfp->lf_lock) &&
3522 nlop->lo_first < new_lop->lo_first) {
3524 nlop = LIST_NEXT(lop, lo_lckfile);
3526 LIST_INSERT_AFTER(lop, new_lop, lo_lckfile);
3529 new_lop->lo_lckfile.le_prev = NULL; /* list not used */
3533 * Insert after insert_lop, which is overloaded as stp or lfp for
3536 if (stp == NULL && (struct nfslockfile *)insert_lop == lfp)
3537 LIST_INSERT_HEAD(&lfp->lf_locallock, new_lop, lo_lckowner);
3538 else if ((struct nfsstate *)insert_lop == stp)
3539 LIST_INSERT_HEAD(&stp->ls_lock, new_lop, lo_lckowner);
3541 LIST_INSERT_AFTER(insert_lop, new_lop, lo_lckowner);
3543 newnfsstats.srvlocks++;
3544 nfsrv_openpluslock++;
3549 * This function updates the locking for a lock owner and given file. It
3550 * maintains a list of lock ranges ordered on increasing file offset that
3551 * are NFSLCK_READ or NFSLCK_WRITE and non-overlapping (aka POSIX style).
3552 * It always adds new_lop to the list and sometimes uses the one pointed
3556 nfsrv_updatelock(struct nfsstate *stp, struct nfslock **new_lopp,
3557 struct nfslock **other_lopp, struct nfslockfile *lfp)
3559 struct nfslock *new_lop = *new_lopp;
3560 struct nfslock *lop, *tlop, *ilop;
3561 struct nfslock *other_lop = *other_lopp;
3562 int unlock = 0, myfile = 0;
3566 * Work down the list until the lock is merged.
3568 if (new_lop->lo_flags & NFSLCK_UNLOCK)
3571 ilop = (struct nfslock *)stp;
3572 lop = LIST_FIRST(&stp->ls_lock);
3574 ilop = (struct nfslock *)lfp;
3575 lop = LIST_FIRST(&lfp->lf_locallock);
3577 while (lop != NULL) {
3579 * Only check locks for this file that aren't before the start of
3582 if (lop->lo_lfp == lfp) {
3584 if (lop->lo_end >= new_lop->lo_first) {
3585 if (new_lop->lo_end < lop->lo_first) {
3587 * If the new lock ends before the start of the
3588 * current lock's range, no merge, just insert
3593 if (new_lop->lo_flags == lop->lo_flags ||
3594 (new_lop->lo_first <= lop->lo_first &&
3595 new_lop->lo_end >= lop->lo_end)) {
3597 * This lock can be absorbed by the new lock/unlock.
3598 * This happens when it covers the entire range
3599 * of the old lock or is contiguous
3600 * with the old lock and is of the same type or an
3603 if (lop->lo_first < new_lop->lo_first)
3604 new_lop->lo_first = lop->lo_first;
3605 if (lop->lo_end > new_lop->lo_end)
3606 new_lop->lo_end = lop->lo_end;
3608 lop = LIST_NEXT(lop, lo_lckowner);
3609 nfsrv_freenfslock(tlop);
3614 * All these cases are for contiguous locks that are not the
3615 * same type, so they can't be merged.
3617 if (new_lop->lo_first <= lop->lo_first) {
3619 * This case is where the new lock overlaps with the
3620 * first part of the old lock. Move the start of the
3621 * old lock to just past the end of the new lock. The
3622 * new lock will be inserted in front of the old, since
3623 * ilop hasn't been updated. (We are done now.)
3625 lop->lo_first = new_lop->lo_end;
3628 if (new_lop->lo_end >= lop->lo_end) {
3630 * This case is where the new lock overlaps with the
3631 * end of the old lock's range. Move the old lock's
3632 * end to just before the new lock's first and insert
3633 * the new lock after the old lock.
3634 * Might not be done yet, since the new lock could
3635 * overlap further locks with higher ranges.
3637 lop->lo_end = new_lop->lo_first;
3639 lop = LIST_NEXT(lop, lo_lckowner);
3643 * The final case is where the new lock's range is in the
3644 * middle of the current lock's and splits the current lock
3645 * up. Use *other_lopp to handle the second part of the
3646 * split old lock range. (We are done now.)
3647 * For unlock, we use new_lop as other_lop and tmp, since
3648 * other_lop and new_lop are the same for this case.
3649 * We noted the unlock case above, so we don't need
3650 * new_lop->lo_flags any longer.
3652 tmp = new_lop->lo_first;
3653 if (other_lop == NULL) {
3655 panic("nfsd srv update unlock");
3656 other_lop = new_lop;
3659 other_lop->lo_first = new_lop->lo_end;
3660 other_lop->lo_end = lop->lo_end;
3661 other_lop->lo_flags = lop->lo_flags;
3662 other_lop->lo_stp = stp;
3663 other_lop->lo_lfp = lfp;
3665 nfsrv_insertlock(other_lop, lop, stp, lfp);
3672 lop = LIST_NEXT(lop, lo_lckowner);
3673 if (myfile && (lop == NULL || lop->lo_lfp != lfp))
3678 * Insert the new lock in the list at the appropriate place.
3681 nfsrv_insertlock(new_lop, ilop, stp, lfp);
3687 * This function handles sequencing of locks, etc.
3688 * It returns an error that indicates what the caller should do.
3691 nfsrv_checkseqid(struct nfsrv_descript *nd, u_int32_t seqid,
3692 struct nfsstate *stp, struct nfsrvcache *op)
3696 if ((nd->nd_flag & ND_NFSV41) != 0)
3697 /* NFSv4.1 ignores the open_seqid and lock_seqid. */
3699 if (op != nd->nd_rp)
3700 panic("nfsrvstate checkseqid");
3701 if (!(op->rc_flag & RC_INPROG))
3702 panic("nfsrvstate not inprog");
3703 if (stp->ls_op && stp->ls_op->rc_refcnt <= 0) {
3704 printf("refcnt=%d\n", stp->ls_op->rc_refcnt);
3705 panic("nfsrvstate op refcnt");
3707 if ((stp->ls_seq + 1) == seqid) {
3709 nfsrvd_derefcache(stp->ls_op);
3711 nfsrvd_refcache(op);
3712 stp->ls_seq = seqid;
3714 } else if (stp->ls_seq == seqid && stp->ls_op &&
3715 op->rc_xid == stp->ls_op->rc_xid &&
3716 op->rc_refcnt == 0 &&
3717 op->rc_reqlen == stp->ls_op->rc_reqlen &&
3718 op->rc_cksum == stp->ls_op->rc_cksum) {
3719 if (stp->ls_op->rc_flag & RC_INPROG) {
3720 error = NFSERR_DONTREPLY;
3723 nd->nd_rp = stp->ls_op;
3724 nd->nd_rp->rc_flag |= RC_INPROG;
3725 nfsrvd_delcache(op);
3726 error = NFSERR_REPLYFROMCACHE;
3729 error = NFSERR_BADSEQID;
3732 NFSEXITCODE2(error, nd);
3737 * Get the client ip address for callbacks. If the strings can't be parsed,
3738 * just set lc_program to 0 to indicate no callbacks are possible.
3739 * (For cases where the address can't be parsed or is 0.0.0.0.0.0, set
3740 * the address to the client's transport address. This won't be used
3741 * for callbacks, but can be printed out by newnfsstats for info.)
3742 * Return error if the xdr can't be parsed, 0 otherwise.
3745 nfsrv_getclientipaddr(struct nfsrv_descript *nd, struct nfsclient *clp)
3750 struct sockaddr_in *rad, *sad;
3751 u_char protocol[5], addr[24];
3752 int error = 0, cantparse = 0;
3762 rad = NFSSOCKADDR(clp->lc_req.nr_nam, struct sockaddr_in *);
3763 rad->sin_family = AF_INET;
3764 rad->sin_len = sizeof (struct sockaddr_in);
3765 rad->sin_addr.s_addr = 0;
3767 clp->lc_req.nr_client = NULL;
3768 clp->lc_req.nr_lock = 0;
3769 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
3770 i = fxdr_unsigned(int, *tl);
3771 if (i >= 3 && i <= 4) {
3772 error = nfsrv_mtostr(nd, protocol, i);
3775 if (!strcmp(protocol, "tcp")) {
3776 clp->lc_flags |= LCL_TCPCALLBACK;
3777 clp->lc_req.nr_sotype = SOCK_STREAM;
3778 clp->lc_req.nr_soproto = IPPROTO_TCP;
3779 } else if (!strcmp(protocol, "udp")) {
3780 clp->lc_req.nr_sotype = SOCK_DGRAM;
3781 clp->lc_req.nr_soproto = IPPROTO_UDP;
3788 error = nfsm_advance(nd, NFSM_RNDUP(i), -1);
3793 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
3794 i = fxdr_unsigned(int, *tl);
3796 error = NFSERR_BADXDR;
3798 } else if (i == 0) {
3800 } else if (!cantparse && i <= 23 && i >= 11) {
3801 error = nfsrv_mtostr(nd, addr, i);
3806 * Parse out the address fields. We expect 6 decimal numbers
3807 * separated by '.'s.
3811 while (*cp && i < 6) {
3813 while (*cp2 && *cp2 != '.')
3821 j = nfsrv_getipnumber(cp);
3826 port.cval[5 - i] = j;
3835 if (ip.ival != 0x0) {
3836 rad->sin_addr.s_addr = htonl(ip.ival);
3837 rad->sin_port = htons(port.sval);
3845 error = nfsm_advance(nd, NFSM_RNDUP(i), -1);
3851 sad = NFSSOCKADDR(nd->nd_nam, struct sockaddr_in *);
3852 rad->sin_addr.s_addr = sad->sin_addr.s_addr;
3853 rad->sin_port = 0x0;
3854 clp->lc_program = 0;
3857 NFSEXITCODE2(error, nd);
3862 * Turn a string of up to three decimal digits into a number. Return -1 upon
3866 nfsrv_getipnumber(u_char *cp)
3871 if (j > 2 || *cp < '0' || *cp > '9')
3884 * This function checks for restart conditions.
3887 nfsrv_checkrestart(nfsquad_t clientid, u_int32_t flags,
3888 nfsv4stateid_t *stateidp, int specialid)
3893 * First check for a server restart. Open, LockT, ReleaseLockOwner
3894 * and DelegPurge have a clientid, the rest a stateid.
3897 (NFSLCK_OPEN | NFSLCK_TEST | NFSLCK_RELEASE | NFSLCK_DELEGPURGE)) {
3898 if (clientid.lval[0] != nfsrvboottime) {
3899 ret = NFSERR_STALECLIENTID;
3902 } else if (stateidp->other[0] != nfsrvboottime &&
3904 ret = NFSERR_STALESTATEID;
3909 * Read, Write, Setattr and LockT can return NFSERR_GRACE and do
3910 * not use a lock/open owner seqid#, so the check can be done now.
3911 * (The others will be checked, as required, later.)
3913 if (!(flags & (NFSLCK_CHECK | NFSLCK_TEST)))
3917 ret = nfsrv_checkgrace(NULL, NULL, flags);
3929 nfsrv_checkgrace(struct nfsrv_descript *nd, struct nfsclient *clp,
3934 if ((nfsrv_stablefirst.nsf_flags & NFSNSF_GRACEOVER) != 0) {
3935 if (flags & NFSLCK_RECLAIM) {
3936 error = NFSERR_NOGRACE;
3940 if (!(flags & NFSLCK_RECLAIM)) {
3941 error = NFSERR_GRACE;
3944 if (nd != NULL && clp != NULL &&
3945 (nd->nd_flag & ND_NFSV41) != 0 &&
3946 (clp->lc_flags & LCL_RECLAIMCOMPLETE) != 0) {
3947 error = NFSERR_NOGRACE;
3952 * If grace is almost over and we are still getting Reclaims,
3953 * extend grace a bit.
3955 if ((NFSD_MONOSEC + NFSRV_LEASEDELTA) >
3956 nfsrv_stablefirst.nsf_eograce)
3957 nfsrv_stablefirst.nsf_eograce = NFSD_MONOSEC +
3967 * Do a server callback.
3970 nfsrv_docallback(struct nfsclient *clp, int procnum,
3971 nfsv4stateid_t *stateidp, int trunc, fhandle_t *fhp,
3972 struct nfsvattr *nap, nfsattrbit_t *attrbitp, NFSPROC_T *p)
3976 struct nfsrv_descript nfsd, *nd = &nfsd;
3980 struct nfsdsession *sep = NULL;
3982 cred = newnfs_getcred();
3983 NFSLOCKSTATE(); /* mostly for lc_cbref++ */
3984 if (clp->lc_flags & LCL_NEEDSCONFIRM) {
3991 * Fill the callback program# and version into the request
3992 * structure for newnfs_connect() to use.
3994 clp->lc_req.nr_prog = clp->lc_program;
3996 if ((clp->lc_flags & LCL_NFSV41) != 0)
3997 clp->lc_req.nr_vers = NFSV41_CBVERS;
4000 clp->lc_req.nr_vers = NFSV4_CBVERS;
4003 * First, fill in some of the fields of nd and cr.
4005 nd->nd_flag = ND_NFSV4;
4006 if (clp->lc_flags & LCL_GSS)
4007 nd->nd_flag |= ND_KERBV;
4008 if ((clp->lc_flags & LCL_NFSV41) != 0)
4009 nd->nd_flag |= ND_NFSV41;
4011 cred->cr_uid = clp->lc_uid;
4012 cred->cr_gid = clp->lc_gid;
4013 callback = clp->lc_callback;
4015 cred->cr_ngroups = 1;
4018 * Get the first mbuf for the request.
4020 MGET(m, M_WAITOK, MT_DATA);
4022 nd->nd_mreq = nd->nd_mb = m;
4023 nd->nd_bpos = NFSMTOD(m, caddr_t);
4026 * and build the callback request.
4028 if (procnum == NFSV4OP_CBGETATTR) {
4029 nd->nd_procnum = NFSV4PROC_CBCOMPOUND;
4030 error = nfsrv_cbcallargs(nd, clp, callback, NFSV4OP_CBGETATTR,
4031 "CB Getattr", &sep);
4033 mbuf_freem(nd->nd_mreq);
4036 (void)nfsm_fhtom(nd, (u_int8_t *)fhp, NFSX_MYFH, 0);
4037 (void)nfsrv_putattrbit(nd, attrbitp);
4038 } else if (procnum == NFSV4OP_CBRECALL) {
4039 nd->nd_procnum = NFSV4PROC_CBCOMPOUND;
4040 error = nfsrv_cbcallargs(nd, clp, callback, NFSV4OP_CBRECALL,
4043 mbuf_freem(nd->nd_mreq);
4046 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED + NFSX_STATEID);
4047 *tl++ = txdr_unsigned(stateidp->seqid);
4048 NFSBCOPY((caddr_t)stateidp->other, (caddr_t)tl,
4050 tl += (NFSX_STATEIDOTHER / NFSX_UNSIGNED);
4055 (void)nfsm_fhtom(nd, (u_int8_t *)fhp, NFSX_MYFH, 0);
4056 } else if (procnum == NFSV4PROC_CBNULL) {
4057 nd->nd_procnum = NFSV4PROC_CBNULL;
4058 if ((clp->lc_flags & LCL_NFSV41) != 0) {
4059 error = nfsv4_getcbsession(clp, &sep);
4061 mbuf_freem(nd->nd_mreq);
4066 error = NFSERR_SERVERFAULT;
4067 mbuf_freem(nd->nd_mreq);
4072 * Call newnfs_connect(), as required, and then newnfs_request().
4074 (void) newnfs_sndlock(&clp->lc_req.nr_lock);
4075 if (clp->lc_req.nr_client == NULL) {
4076 if ((clp->lc_flags & LCL_NFSV41) != 0)
4077 error = ECONNREFUSED;
4078 else if (nd->nd_procnum == NFSV4PROC_CBNULL)
4079 error = newnfs_connect(NULL, &clp->lc_req, cred,
4082 error = newnfs_connect(NULL, &clp->lc_req, cred,
4085 newnfs_sndunlock(&clp->lc_req.nr_lock);
4087 if ((nd->nd_flag & ND_NFSV41) != 0) {
4088 KASSERT(sep != NULL, ("sep NULL"));
4089 error = newnfs_request(nd, NULL, clp, &clp->lc_req,
4090 NULL, NULL, cred, clp->lc_program,
4091 clp->lc_req.nr_vers, NULL, 1, NULL,
4093 nfsrv_freesession(sep, NULL);
4095 error = newnfs_request(nd, NULL, clp, &clp->lc_req,
4096 NULL, NULL, cred, clp->lc_program,
4097 clp->lc_req.nr_vers, NULL, 1, NULL, NULL);
4103 * If error is set here, the Callback path isn't working
4104 * properly, so twiddle the appropriate LCL_ flags.
4105 * (nd_repstat != 0 indicates the Callback path is working,
4106 * but the callback failed on the client.)
4110 * Mark the callback pathway down, which disabled issuing
4111 * of delegations and gets Renew to return NFSERR_CBPATHDOWN.
4114 clp->lc_flags |= LCL_CBDOWN;
4118 * Callback worked. If the callback path was down, disable
4119 * callbacks, so no more delegations will be issued. (This
4120 * is done on the assumption that the callback pathway is
4124 if (clp->lc_flags & LCL_CBDOWN)
4125 clp->lc_flags &= ~(LCL_CBDOWN | LCL_CALLBACKSON);
4128 error = nd->nd_repstat;
4129 else if (error == 0 && procnum == NFSV4OP_CBGETATTR)
4130 error = nfsv4_loadattr(nd, NULL, nap, NULL, NULL, 0,
4131 NULL, NULL, NULL, NULL, NULL, 0, NULL, NULL, NULL,
4133 mbuf_freem(nd->nd_mrep);
4137 if ((clp->lc_flags & LCL_WAKEUPWANTED) && clp->lc_cbref == 0) {
4138 clp->lc_flags &= ~LCL_WAKEUPWANTED;
4148 * Set up the compound RPC for the callback.
4151 nfsrv_cbcallargs(struct nfsrv_descript *nd, struct nfsclient *clp,
4152 uint32_t callback, int op, const char *optag, struct nfsdsession **sepp)
4157 len = strlen(optag);
4158 (void)nfsm_strtom(nd, optag, len);
4159 NFSM_BUILD(tl, uint32_t *, 4 * NFSX_UNSIGNED);
4160 if ((nd->nd_flag & ND_NFSV41) != 0) {
4161 *tl++ = txdr_unsigned(NFSV41_MINORVERSION);
4162 *tl++ = txdr_unsigned(callback);
4163 *tl++ = txdr_unsigned(2);
4164 *tl = txdr_unsigned(NFSV4OP_CBSEQUENCE);
4165 error = nfsv4_setcbsequence(nd, clp, 1, sepp);
4168 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
4169 *tl = txdr_unsigned(op);
4171 *tl++ = txdr_unsigned(NFSV4_MINORVERSION);
4172 *tl++ = txdr_unsigned(callback);
4173 *tl++ = txdr_unsigned(1);
4174 *tl = txdr_unsigned(op);
4180 * Return the next index# for a clientid. Mostly just increment and return
4181 * the next one, but... if the 32bit unsigned does actually wrap around,
4182 * it should be rebooted.
4183 * At an average rate of one new client per second, it will wrap around in
4184 * approximately 136 years. (I think the server will have been shut
4185 * down or rebooted before then.)
4188 nfsrv_nextclientindex(void)
4190 static u_int32_t client_index = 0;
4193 if (client_index != 0)
4194 return (client_index);
4196 printf("%s: out of clientids\n", __func__);
4197 return (client_index);
4201 * Return the next index# for a stateid. Mostly just increment and return
4202 * the next one, but... if the 32bit unsigned does actually wrap around
4203 * (will a BSD server stay up that long?), find
4204 * new start and end values.
4207 nfsrv_nextstateindex(struct nfsclient *clp)
4209 struct nfsstate *stp;
4211 u_int32_t canuse, min_index, max_index;
4213 if (!(clp->lc_flags & LCL_INDEXNOTOK)) {
4214 clp->lc_stateindex++;
4215 if (clp->lc_stateindex != clp->lc_statemaxindex)
4216 return (clp->lc_stateindex);
4220 * Yuck, we've hit the end.
4221 * Look for a new min and max.
4224 max_index = 0xffffffff;
4225 for (i = 0; i < NFSSTATEHASHSIZE; i++) {
4226 LIST_FOREACH(stp, &clp->lc_stateid[i], ls_hash) {
4227 if (stp->ls_stateid.other[2] > 0x80000000) {
4228 if (stp->ls_stateid.other[2] < max_index)
4229 max_index = stp->ls_stateid.other[2];
4231 if (stp->ls_stateid.other[2] > min_index)
4232 min_index = stp->ls_stateid.other[2];
4238 * Yikes, highly unlikely, but I'll handle it anyhow.
4240 if (min_index == 0x80000000 && max_index == 0x80000001) {
4243 * Loop around until we find an unused entry. Return that
4244 * and set LCL_INDEXNOTOK, so the search will continue next time.
4245 * (This is one of those rare cases where a goto is the
4246 * cleanest way to code the loop.)
4249 for (i = 0; i < NFSSTATEHASHSIZE; i++) {
4250 LIST_FOREACH(stp, &clp->lc_stateid[i], ls_hash) {
4251 if (stp->ls_stateid.other[2] == canuse) {
4257 clp->lc_flags |= LCL_INDEXNOTOK;
4262 * Ok to start again from min + 1.
4264 clp->lc_stateindex = min_index + 1;
4265 clp->lc_statemaxindex = max_index;
4266 clp->lc_flags &= ~LCL_INDEXNOTOK;
4267 return (clp->lc_stateindex);
4271 * The following functions handle the stable storage file that deals with
4272 * the edge conditions described in RFC3530 Sec. 8.6.3.
4273 * The file is as follows:
4274 * - a single record at the beginning that has the lease time of the
4275 * previous server instance (before the last reboot) and the nfsrvboottime
4276 * values for the previous server boots.
4277 * These previous boot times are used to ensure that the current
4278 * nfsrvboottime does not, somehow, get set to a previous one.
4279 * (This is important so that Stale ClientIDs and StateIDs can
4281 * The number of previous nfsvrboottime values preceeds the list.
4282 * - followed by some number of appended records with:
4283 * - client id string
4284 * - flag that indicates it is a record revoking state via lease
4285 * expiration or similar
4286 * OR has successfully acquired state.
4287 * These structures vary in length, with the client string at the end, up
4288 * to NFSV4_OPAQUELIMIT in size.
4290 * At the end of the grace period, the file is truncated, the first
4291 * record is rewritten with updated information and any acquired state
4292 * records for successful reclaims of state are written.
4294 * Subsequent records are appended when the first state is issued to
4295 * a client and when state is revoked for a client.
4297 * When reading the file in, state issued records that come later in
4298 * the file override older ones, since the append log is in cronological order.
4299 * If, for some reason, the file can't be read, the grace period is
4300 * immediately terminated and all reclaims get NFSERR_NOGRACE.
4304 * Read in the stable storage file. Called by nfssvc() before the nfsd
4305 * processes start servicing requests.
4308 nfsrv_setupstable(NFSPROC_T *p)
4310 struct nfsrv_stablefirst *sf = &nfsrv_stablefirst;
4311 struct nfsrv_stable *sp, *nsp;
4312 struct nfst_rec *tsp;
4313 int error, i, tryagain;
4315 ssize_t aresid, len;
4318 * If NFSNSF_UPDATEDONE is set, this is a restart of the nfsds without
4319 * a reboot, so state has not been lost.
4321 if (sf->nsf_flags & NFSNSF_UPDATEDONE)
4324 * Set Grace over just until the file reads successfully.
4326 nfsrvboottime = time_second;
4327 LIST_INIT(&sf->nsf_head);
4328 sf->nsf_flags = (NFSNSF_GRACEOVER | NFSNSF_NEEDLOCK);
4329 sf->nsf_eograce = NFSD_MONOSEC + NFSRV_LEASEDELTA;
4330 if (sf->nsf_fp == NULL)
4332 error = NFSD_RDWR(UIO_READ, NFSFPVNODE(sf->nsf_fp),
4333 (caddr_t)&sf->nsf_rec, sizeof (struct nfsf_rec), off, UIO_SYSSPACE,
4334 0, NFSFPCRED(sf->nsf_fp), &aresid, p);
4335 if (error || aresid || sf->nsf_numboots == 0 ||
4336 sf->nsf_numboots > NFSNSF_MAXNUMBOOTS)
4340 * Now, read in the boottimes.
4342 sf->nsf_bootvals = (time_t *)malloc((sf->nsf_numboots + 1) *
4343 sizeof (time_t), M_TEMP, M_WAITOK);
4344 off = sizeof (struct nfsf_rec);
4345 error = NFSD_RDWR(UIO_READ, NFSFPVNODE(sf->nsf_fp),
4346 (caddr_t)sf->nsf_bootvals, sf->nsf_numboots * sizeof (time_t), off,
4347 UIO_SYSSPACE, 0, NFSFPCRED(sf->nsf_fp), &aresid, p);
4348 if (error || aresid) {
4349 free((caddr_t)sf->nsf_bootvals, M_TEMP);
4350 sf->nsf_bootvals = NULL;
4355 * Make sure this nfsrvboottime is different from all recorded
4360 for (i = 0; i < sf->nsf_numboots; i++) {
4361 if (nfsrvboottime == sf->nsf_bootvals[i]) {
4369 sf->nsf_flags |= NFSNSF_OK;
4370 off += (sf->nsf_numboots * sizeof (time_t));
4373 * Read through the file, building a list of records for grace
4375 * Each record is between sizeof (struct nfst_rec) and
4376 * sizeof (struct nfst_rec) + NFSV4_OPAQUELIMIT - 1
4377 * and is actually sizeof (struct nfst_rec) + nst_len - 1.
4379 tsp = (struct nfst_rec *)malloc(sizeof (struct nfst_rec) +
4380 NFSV4_OPAQUELIMIT - 1, M_TEMP, M_WAITOK);
4382 error = NFSD_RDWR(UIO_READ, NFSFPVNODE(sf->nsf_fp),
4383 (caddr_t)tsp, sizeof (struct nfst_rec) + NFSV4_OPAQUELIMIT - 1,
4384 off, UIO_SYSSPACE, 0, NFSFPCRED(sf->nsf_fp), &aresid, p);
4385 len = (sizeof (struct nfst_rec) + NFSV4_OPAQUELIMIT - 1) - aresid;
4386 if (error || (len > 0 && (len < sizeof (struct nfst_rec) ||
4387 len < (sizeof (struct nfst_rec) + tsp->len - 1)))) {
4389 * Yuck, the file has been corrupted, so just return
4390 * after clearing out any restart state, so the grace period
4393 LIST_FOREACH_SAFE(sp, &sf->nsf_head, nst_list, nsp) {
4394 LIST_REMOVE(sp, nst_list);
4395 free((caddr_t)sp, M_TEMP);
4397 free((caddr_t)tsp, M_TEMP);
4398 sf->nsf_flags &= ~NFSNSF_OK;
4399 free((caddr_t)sf->nsf_bootvals, M_TEMP);
4400 sf->nsf_bootvals = NULL;
4404 off += sizeof (struct nfst_rec) + tsp->len - 1;
4406 * Search the list for a matching client.
4408 LIST_FOREACH(sp, &sf->nsf_head, nst_list) {
4409 if (tsp->len == sp->nst_len &&
4410 !NFSBCMP(tsp->client, sp->nst_client, tsp->len))
4413 if (sp == LIST_END(&sf->nsf_head)) {
4414 sp = (struct nfsrv_stable *)malloc(tsp->len +
4415 sizeof (struct nfsrv_stable) - 1, M_TEMP,
4417 NFSBCOPY((caddr_t)tsp, (caddr_t)&sp->nst_rec,
4418 sizeof (struct nfst_rec) + tsp->len - 1);
4419 LIST_INSERT_HEAD(&sf->nsf_head, sp, nst_list);
4421 if (tsp->flag == NFSNST_REVOKE)
4422 sp->nst_flag |= NFSNST_REVOKE;
4425 * A subsequent timestamp indicates the client
4426 * did a setclientid/confirm and any previous
4427 * revoke is no longer relevant.
4429 sp->nst_flag &= ~NFSNST_REVOKE;
4433 free((caddr_t)tsp, M_TEMP);
4434 sf->nsf_flags = NFSNSF_OK;
4435 sf->nsf_eograce = NFSD_MONOSEC + sf->nsf_lease +
4440 * Update the stable storage file, now that the grace period is over.
4443 nfsrv_updatestable(NFSPROC_T *p)
4445 struct nfsrv_stablefirst *sf = &nfsrv_stablefirst;
4446 struct nfsrv_stable *sp, *nsp;
4448 struct nfsvattr nva;
4450 #if defined(__FreeBSD_version) && (__FreeBSD_version >= 500000)
4455 if (sf->nsf_fp == NULL || (sf->nsf_flags & NFSNSF_UPDATEDONE))
4457 sf->nsf_flags |= NFSNSF_UPDATEDONE;
4459 * Ok, we need to rewrite the stable storage file.
4460 * - truncate to 0 length
4461 * - write the new first structure
4462 * - loop through the data structures, writing out any that
4463 * have timestamps older than the old boot
4465 if (sf->nsf_bootvals) {
4467 for (i = sf->nsf_numboots - 2; i >= 0; i--)
4468 sf->nsf_bootvals[i + 1] = sf->nsf_bootvals[i];
4470 sf->nsf_numboots = 1;
4471 sf->nsf_bootvals = (time_t *)malloc(sizeof (time_t),
4474 sf->nsf_bootvals[0] = nfsrvboottime;
4475 sf->nsf_lease = nfsrv_lease;
4476 NFSVNO_ATTRINIT(&nva);
4477 NFSVNO_SETATTRVAL(&nva, size, 0);
4478 vp = NFSFPVNODE(sf->nsf_fp);
4479 vn_start_write(vp, &mp, V_WAIT);
4480 if (NFSVOPLOCK(vp, LK_EXCLUSIVE) == 0) {
4481 error = nfsvno_setattr(vp, &nva, NFSFPCRED(sf->nsf_fp), p,
4483 NFSVOPUNLOCK(vp, 0);
4486 vn_finished_write(mp);
4488 error = NFSD_RDWR(UIO_WRITE, vp,
4489 (caddr_t)&sf->nsf_rec, sizeof (struct nfsf_rec), (off_t)0,
4490 UIO_SYSSPACE, IO_SYNC, NFSFPCRED(sf->nsf_fp), NULL, p);
4492 error = NFSD_RDWR(UIO_WRITE, vp,
4493 (caddr_t)sf->nsf_bootvals,
4494 sf->nsf_numboots * sizeof (time_t),
4495 (off_t)(sizeof (struct nfsf_rec)),
4496 UIO_SYSSPACE, IO_SYNC, NFSFPCRED(sf->nsf_fp), NULL, p);
4497 free((caddr_t)sf->nsf_bootvals, M_TEMP);
4498 sf->nsf_bootvals = NULL;
4500 sf->nsf_flags &= ~NFSNSF_OK;
4501 printf("EEK! Can't write NfsV4 stable storage file\n");
4504 sf->nsf_flags |= NFSNSF_OK;
4507 * Loop through the list and write out timestamp records for
4508 * any clients that successfully reclaimed state.
4510 LIST_FOREACH_SAFE(sp, &sf->nsf_head, nst_list, nsp) {
4511 if (sp->nst_flag & NFSNST_GOTSTATE) {
4512 nfsrv_writestable(sp->nst_client, sp->nst_len,
4513 NFSNST_NEWSTATE, p);
4514 sp->nst_clp->lc_flags |= LCL_STAMPEDSTABLE;
4516 LIST_REMOVE(sp, nst_list);
4517 free((caddr_t)sp, M_TEMP);
4519 nfsrv_backupstable();
4523 * Append a record to the stable storage file.
4526 nfsrv_writestable(u_char *client, int len, int flag, NFSPROC_T *p)
4528 struct nfsrv_stablefirst *sf = &nfsrv_stablefirst;
4529 struct nfst_rec *sp;
4532 if (!(sf->nsf_flags & NFSNSF_OK) || sf->nsf_fp == NULL)
4534 sp = (struct nfst_rec *)malloc(sizeof (struct nfst_rec) +
4535 len - 1, M_TEMP, M_WAITOK);
4537 NFSBCOPY(client, sp->client, len);
4539 error = NFSD_RDWR(UIO_WRITE, NFSFPVNODE(sf->nsf_fp),
4540 (caddr_t)sp, sizeof (struct nfst_rec) + len - 1, (off_t)0,
4541 UIO_SYSSPACE, (IO_SYNC | IO_APPEND), NFSFPCRED(sf->nsf_fp), NULL, p);
4542 free((caddr_t)sp, M_TEMP);
4544 sf->nsf_flags &= ~NFSNSF_OK;
4545 printf("EEK! Can't write NfsV4 stable storage file\n");
4550 * This function is called during the grace period to mark a client
4551 * that successfully reclaimed state.
4554 nfsrv_markstable(struct nfsclient *clp)
4556 struct nfsrv_stable *sp;
4559 * First find the client structure.
4561 LIST_FOREACH(sp, &nfsrv_stablefirst.nsf_head, nst_list) {
4562 if (sp->nst_len == clp->lc_idlen &&
4563 !NFSBCMP(sp->nst_client, clp->lc_id, sp->nst_len))
4566 if (sp == LIST_END(&nfsrv_stablefirst.nsf_head))
4570 * Now, just mark it and set the nfsclient back pointer.
4572 sp->nst_flag |= NFSNST_GOTSTATE;
4577 * This function is called for a reclaim, to see if it gets grace.
4578 * It returns 0 if a reclaim is allowed, 1 otherwise.
4581 nfsrv_checkstable(struct nfsclient *clp)
4583 struct nfsrv_stable *sp;
4586 * First, find the entry for the client.
4588 LIST_FOREACH(sp, &nfsrv_stablefirst.nsf_head, nst_list) {
4589 if (sp->nst_len == clp->lc_idlen &&
4590 !NFSBCMP(sp->nst_client, clp->lc_id, sp->nst_len))
4595 * If not in the list, state was revoked or no state was issued
4596 * since the previous reboot, a reclaim is denied.
4598 if (sp == LIST_END(&nfsrv_stablefirst.nsf_head) ||
4599 (sp->nst_flag & NFSNST_REVOKE) ||
4600 !(nfsrv_stablefirst.nsf_flags & NFSNSF_OK))
4606 * Test for and try to clear out a conflicting client. This is called by
4607 * nfsrv_lockctrl() and nfsrv_openctrl() when conflicts with other clients
4609 * The trick here is that it can't revoke a conflicting client with an
4610 * expired lease unless it holds the v4root lock, so...
4611 * If no v4root lock, get the lock and return 1 to indicate "try again".
4612 * Return 0 to indicate the conflict can't be revoked and 1 to indicate
4613 * the revocation worked and the conflicting client is "bye, bye", so it
4614 * can be tried again.
4615 * Return 2 to indicate that the vnode is VI_DOOMED after NFSVOPLOCK().
4616 * Unlocks State before a non-zero value is returned.
4619 nfsrv_clientconflict(struct nfsclient *clp, int *haslockp, vnode_t vp,
4622 int gotlock, lktype;
4625 * If lease hasn't expired, we can't fix it.
4627 if (clp->lc_expiry >= NFSD_MONOSEC ||
4628 !(nfsrv_stablefirst.nsf_flags & NFSNSF_UPDATEDONE))
4630 if (*haslockp == 0) {
4632 lktype = NFSVOPISLOCKED(vp);
4633 NFSVOPUNLOCK(vp, 0);
4634 NFSLOCKV4ROOTMUTEX();
4635 nfsv4_relref(&nfsv4rootfs_lock);
4637 gotlock = nfsv4_lock(&nfsv4rootfs_lock, 1, NULL,
4638 NFSV4ROOTLOCKMUTEXPTR, NULL);
4640 NFSUNLOCKV4ROOTMUTEX();
4642 NFSVOPLOCK(vp, lktype | LK_RETRY);
4643 if ((vp->v_iflag & VI_DOOMED) != 0)
4651 * Ok, we can expire the conflicting client.
4653 nfsrv_writestable(clp->lc_id, clp->lc_idlen, NFSNST_REVOKE, p);
4654 nfsrv_backupstable();
4655 nfsrv_cleanclient(clp, p);
4656 nfsrv_freedeleglist(&clp->lc_deleg);
4657 nfsrv_freedeleglist(&clp->lc_olddeleg);
4658 LIST_REMOVE(clp, lc_hash);
4659 nfsrv_zapclient(clp, p);
4664 * Resolve a delegation conflict.
4665 * Returns 0 to indicate the conflict was resolved without sleeping.
4666 * Return -1 to indicate that the caller should check for conflicts again.
4667 * Return > 0 for an error that should be returned, normally NFSERR_DELAY.
4669 * Also, manipulate the nfsv4root_lock, as required. It isn't changed
4670 * for a return of 0, since there was no sleep and it could be required
4671 * later. It is released for a return of NFSERR_DELAY, since the caller
4672 * will return that error. It is released when a sleep was done waiting
4673 * for the delegation to be returned or expire (so that other nfsds can
4674 * handle ops). Then, it must be acquired for the write to stable storage.
4675 * (This function is somewhat similar to nfsrv_clientconflict(), but
4676 * the semantics differ in a couple of subtle ways. The return of 0
4677 * indicates the conflict was resolved without sleeping here, not
4678 * that the conflict can't be resolved and the handling of nfsv4root_lock
4679 * differs, as noted above.)
4680 * Unlocks State before returning a non-zero value.
4683 nfsrv_delegconflict(struct nfsstate *stp, int *haslockp, NFSPROC_T *p,
4686 struct nfsclient *clp = stp->ls_clp;
4687 int gotlock, error, lktype, retrycnt, zapped_clp;
4688 nfsv4stateid_t tstateid;
4692 * If the conflict is with an old delegation...
4694 if (stp->ls_flags & NFSLCK_OLDDELEG) {
4696 * You can delete it, if it has expired.
4698 if (clp->lc_delegtime < NFSD_MONOSEC) {
4699 nfsrv_freedeleg(stp);
4706 * During this delay, the old delegation could expire or it
4707 * could be recovered by the client via an Open with
4708 * CLAIM_DELEGATE_PREV.
4709 * Release the nfsv4root_lock, if held.
4713 NFSLOCKV4ROOTMUTEX();
4714 nfsv4_unlock(&nfsv4rootfs_lock, 1);
4715 NFSUNLOCKV4ROOTMUTEX();
4717 error = NFSERR_DELAY;
4722 * It's a current delegation, so:
4723 * - check to see if the delegation has expired
4724 * - if so, get the v4root lock and then expire it
4726 if (!(stp->ls_flags & NFSLCK_DELEGRECALL)) {
4728 * - do a recall callback, since not yet done
4729 * For now, never allow truncate to be set. To use
4730 * truncate safely, it must be guaranteed that the
4731 * Remove, Rename or Setattr with size of 0 will
4732 * succeed and that would require major changes to
4733 * the VFS/Vnode OPs.
4734 * Set the expiry time large enough so that it won't expire
4735 * until after the callback, then set it correctly, once
4736 * the callback is done. (The delegation will now time
4737 * out whether or not the Recall worked ok. The timeout
4738 * will be extended when ops are done on the delegation
4739 * stateid, up to the timelimit.)
4741 stp->ls_delegtime = NFSD_MONOSEC + (2 * nfsrv_lease) +
4743 stp->ls_delegtimelimit = NFSD_MONOSEC + (6 * nfsrv_lease) +
4745 stp->ls_flags |= NFSLCK_DELEGRECALL;
4748 * Loop NFSRV_CBRETRYCNT times while the CBRecall replies
4749 * NFSERR_BADSTATEID or NFSERR_BADHANDLE. This is done
4750 * in order to try and avoid a race that could happen
4751 * when a CBRecall request passed the Open reply with
4752 * the delegation in it when transitting the network.
4753 * Since nfsrv_docallback will sleep, don't use stp after
4756 NFSBCOPY((caddr_t)&stp->ls_stateid, (caddr_t)&tstateid,
4758 NFSBCOPY((caddr_t)&stp->ls_lfp->lf_fh, (caddr_t)&tfh,
4763 NFSLOCKV4ROOTMUTEX();
4764 nfsv4_unlock(&nfsv4rootfs_lock, 1);
4765 NFSUNLOCKV4ROOTMUTEX();
4769 error = nfsrv_docallback(clp, NFSV4OP_CBRECALL,
4770 &tstateid, 0, &tfh, NULL, NULL, p);
4772 } while ((error == NFSERR_BADSTATEID ||
4773 error == NFSERR_BADHANDLE) && retrycnt < NFSV4_CBRETRYCNT);
4774 error = NFSERR_DELAY;
4778 if (clp->lc_expiry >= NFSD_MONOSEC &&
4779 stp->ls_delegtime >= NFSD_MONOSEC) {
4782 * A recall has been done, but it has not yet expired.
4787 NFSLOCKV4ROOTMUTEX();
4788 nfsv4_unlock(&nfsv4rootfs_lock, 1);
4789 NFSUNLOCKV4ROOTMUTEX();
4791 error = NFSERR_DELAY;
4796 * If we don't yet have the lock, just get it and then return,
4797 * since we need that before deleting expired state, such as
4799 * When getting the lock, unlock the vnode, so other nfsds that
4800 * are in progress, won't get stuck waiting for the vnode lock.
4802 if (*haslockp == 0) {
4804 lktype = NFSVOPISLOCKED(vp);
4805 NFSVOPUNLOCK(vp, 0);
4806 NFSLOCKV4ROOTMUTEX();
4807 nfsv4_relref(&nfsv4rootfs_lock);
4809 gotlock = nfsv4_lock(&nfsv4rootfs_lock, 1, NULL,
4810 NFSV4ROOTLOCKMUTEXPTR, NULL);
4812 NFSUNLOCKV4ROOTMUTEX();
4814 NFSVOPLOCK(vp, lktype | LK_RETRY);
4815 if ((vp->v_iflag & VI_DOOMED) != 0) {
4817 NFSLOCKV4ROOTMUTEX();
4818 nfsv4_unlock(&nfsv4rootfs_lock, 1);
4819 NFSUNLOCKV4ROOTMUTEX();
4820 error = NFSERR_PERM;
4829 * Ok, we can delete the expired delegation.
4830 * First, write the Revoke record to stable storage and then
4831 * clear out the conflict.
4832 * Since all other nfsd threads are now blocked, we can safely
4833 * sleep without the state changing.
4835 nfsrv_writestable(clp->lc_id, clp->lc_idlen, NFSNST_REVOKE, p);
4836 nfsrv_backupstable();
4837 if (clp->lc_expiry < NFSD_MONOSEC) {
4838 nfsrv_cleanclient(clp, p);
4839 nfsrv_freedeleglist(&clp->lc_deleg);
4840 nfsrv_freedeleglist(&clp->lc_olddeleg);
4841 LIST_REMOVE(clp, lc_hash);
4844 nfsrv_freedeleg(stp);
4848 nfsrv_zapclient(clp, p);
4857 * Check for a remove allowed, if remove is set to 1 and get rid of
4861 nfsrv_checkremove(vnode_t vp, int remove, NFSPROC_T *p)
4863 struct nfsstate *stp;
4864 struct nfslockfile *lfp;
4865 int error, haslock = 0;
4869 * First, get the lock file structure.
4870 * (A return of -1 means no associated state, so remove ok.)
4872 error = nfsrv_getlockfh(vp, NFSLCK_CHECK, NULL, &nfh, p);
4876 error = nfsrv_getlockfile(NFSLCK_CHECK, NULL, &lfp, &nfh, 0);
4880 NFSLOCKV4ROOTMUTEX();
4881 nfsv4_unlock(&nfsv4rootfs_lock, 1);
4882 NFSUNLOCKV4ROOTMUTEX();
4890 * Now, we must Recall any delegations.
4892 error = nfsrv_cleandeleg(vp, lfp, NULL, &haslock, p);
4895 * nfsrv_cleandeleg() unlocks state for non-zero
4901 NFSLOCKV4ROOTMUTEX();
4902 nfsv4_unlock(&nfsv4rootfs_lock, 1);
4903 NFSUNLOCKV4ROOTMUTEX();
4909 * Now, look for a conflicting open share.
4912 LIST_FOREACH(stp, &lfp->lf_open, ls_file) {
4913 if (stp->ls_flags & NFSLCK_WRITEDENY) {
4914 error = NFSERR_FILEOPEN;
4922 NFSLOCKV4ROOTMUTEX();
4923 nfsv4_unlock(&nfsv4rootfs_lock, 1);
4924 NFSUNLOCKV4ROOTMUTEX();
4933 * Clear out all delegations for the file referred to by lfp.
4934 * May return NFSERR_DELAY, if there will be a delay waiting for
4935 * delegations to expire.
4936 * Returns -1 to indicate it slept while recalling a delegation.
4937 * This function has the side effect of deleting the nfslockfile structure,
4938 * if it no longer has associated state and didn't have to sleep.
4939 * Unlocks State before a non-zero value is returned.
4942 nfsrv_cleandeleg(vnode_t vp, struct nfslockfile *lfp,
4943 struct nfsclient *clp, int *haslockp, NFSPROC_T *p)
4945 struct nfsstate *stp, *nstp;
4948 stp = LIST_FIRST(&lfp->lf_deleg);
4949 while (stp != LIST_END(&lfp->lf_deleg)) {
4950 nstp = LIST_NEXT(stp, ls_file);
4951 if (stp->ls_clp != clp) {
4952 ret = nfsrv_delegconflict(stp, haslockp, p, vp);
4955 * nfsrv_delegconflict() unlocks state
4956 * when it returns non-zero.
4969 * There are certain operations that, when being done outside of NFSv4,
4970 * require that any NFSv4 delegation for the file be recalled.
4971 * This function is to be called for those cases:
4972 * VOP_RENAME() - When a delegation is being recalled for any reason,
4973 * the client may have to do Opens against the server, using the file's
4974 * final component name. If the file has been renamed on the server,
4975 * that component name will be incorrect and the Open will fail.
4976 * VOP_REMOVE() - Theoretically, a client could Open a file after it has
4977 * been removed on the server, if there is a delegation issued to
4978 * that client for the file. I say "theoretically" since clients
4979 * normally do an Access Op before the Open and that Access Op will
4980 * fail with ESTALE. Note that NFSv2 and 3 don't even do Opens, so
4981 * they will detect the file's removal in the same manner. (There is
4982 * one case where RFC3530 allows a client to do an Open without first
4983 * doing an Access Op, which is passage of a check against the ACE
4984 * returned with a Write delegation, but current practice is to ignore
4985 * the ACE and always do an Access Op.)
4986 * Since the functions can only be called with an unlocked vnode, this
4987 * can't be done at this time.
4988 * VOP_ADVLOCK() - When a client holds a delegation, it can issue byte range
4989 * locks locally in the client, which are not visible to the server. To
4990 * deal with this, issuing of delegations for a vnode must be disabled
4991 * and all delegations for the vnode recalled. This is done via the
4992 * second function, using the VV_DISABLEDELEG vflag on the vnode.
4995 nfsd_recalldelegation(vnode_t vp, NFSPROC_T *p)
5001 * First, check to see if the server is currently running and it has
5002 * been called for a regular file when issuing delegations.
5004 if (newnfs_numnfsd == 0 || vp->v_type != VREG ||
5005 nfsrv_issuedelegs == 0)
5008 KASSERT((NFSVOPISLOCKED(vp) != LK_EXCLUSIVE), ("vp %p is locked", vp));
5010 * First, get a reference on the nfsv4rootfs_lock so that an
5011 * exclusive lock cannot be acquired by another thread.
5013 NFSLOCKV4ROOTMUTEX();
5014 nfsv4_getref(&nfsv4rootfs_lock, NULL, NFSV4ROOTLOCKMUTEXPTR, NULL);
5015 NFSUNLOCKV4ROOTMUTEX();
5018 * Now, call nfsrv_checkremove() in a loop while it returns
5019 * NFSERR_DELAY. Return upon any other error or when timed out.
5021 starttime = NFSD_MONOSEC;
5023 if (NFSVOPLOCK(vp, LK_EXCLUSIVE) == 0) {
5024 error = nfsrv_checkremove(vp, 0, p);
5025 NFSVOPUNLOCK(vp, 0);
5028 if (error == NFSERR_DELAY) {
5029 if (NFSD_MONOSEC - starttime > NFS_REMOVETIMEO)
5031 /* Sleep for a short period of time */
5032 (void) nfs_catnap(PZERO, 0, "nfsremove");
5034 } while (error == NFSERR_DELAY);
5035 NFSLOCKV4ROOTMUTEX();
5036 nfsv4_relref(&nfsv4rootfs_lock);
5037 NFSUNLOCKV4ROOTMUTEX();
5041 nfsd_disabledelegation(vnode_t vp, NFSPROC_T *p)
5044 #ifdef VV_DISABLEDELEG
5046 * First, flag issuance of delegations disabled.
5048 atomic_set_long(&vp->v_vflag, VV_DISABLEDELEG);
5052 * Then call nfsd_recalldelegation() to get rid of all extant
5055 nfsd_recalldelegation(vp, p);
5059 * Check for conflicting locks, etc. and then get rid of delegations.
5060 * (At one point I thought that I should get rid of delegations for any
5061 * Setattr, since it could potentially disallow the I/O op (read or write)
5062 * allowed by the delegation. However, Setattr Ops that aren't changing
5063 * the size get a stateid of all 0s, so you can't tell if it is a delegation
5064 * for the same client or a different one, so I decided to only get rid
5065 * of delegations for other clients when the size is being changed.)
5066 * In general, a Setattr can disable NFS I/O Ops that are outstanding, such
5067 * as Write backs, even if there is no delegation, so it really isn't any
5071 nfsrv_checksetattr(vnode_t vp, struct nfsrv_descript *nd,
5072 nfsv4stateid_t *stateidp, struct nfsvattr *nvap, nfsattrbit_t *attrbitp,
5073 struct nfsexstuff *exp, NFSPROC_T *p)
5075 struct nfsstate st, *stp = &st;
5076 struct nfslock lo, *lop = &lo;
5080 if (NFSISSET_ATTRBIT(attrbitp, NFSATTRBIT_SIZE)) {
5081 stp->ls_flags = (NFSLCK_CHECK | NFSLCK_WRITEACCESS);
5082 lop->lo_first = nvap->na_size;
5087 if (NFSISSET_ATTRBIT(attrbitp, NFSATTRBIT_OWNER) ||
5088 NFSISSET_ATTRBIT(attrbitp, NFSATTRBIT_OWNERGROUP) ||
5089 NFSISSET_ATTRBIT(attrbitp, NFSATTRBIT_MODE) ||
5090 NFSISSET_ATTRBIT(attrbitp, NFSATTRBIT_ACL))
5091 stp->ls_flags |= NFSLCK_SETATTR;
5092 if (stp->ls_flags == 0)
5094 lop->lo_end = NFS64BITSSET;
5095 lop->lo_flags = NFSLCK_WRITE;
5096 stp->ls_ownerlen = 0;
5098 stp->ls_uid = nd->nd_cred->cr_uid;
5099 stp->ls_stateid.seqid = stateidp->seqid;
5100 clientid.lval[0] = stp->ls_stateid.other[0] = stateidp->other[0];
5101 clientid.lval[1] = stp->ls_stateid.other[1] = stateidp->other[1];
5102 stp->ls_stateid.other[2] = stateidp->other[2];
5103 error = nfsrv_lockctrl(vp, &stp, &lop, NULL, clientid,
5104 stateidp, exp, nd, p);
5107 NFSEXITCODE2(error, nd);
5112 * Check for a write delegation and do a CBGETATTR if there is one, updating
5113 * the attributes, as required.
5114 * Should I return an error if I can't get the attributes? (For now, I'll
5118 nfsrv_checkgetattr(struct nfsrv_descript *nd, vnode_t vp,
5119 struct nfsvattr *nvap, nfsattrbit_t *attrbitp, struct ucred *cred,
5122 struct nfsstate *stp;
5123 struct nfslockfile *lfp;
5124 struct nfsclient *clp;
5125 struct nfsvattr nva;
5128 nfsattrbit_t cbbits;
5129 u_quad_t delegfilerev;
5131 NFSCBGETATTR_ATTRBIT(attrbitp, &cbbits);
5132 if (!NFSNONZERO_ATTRBIT(&cbbits))
5136 * Get the lock file structure.
5137 * (A return of -1 means no associated state, so return ok.)
5139 error = nfsrv_getlockfh(vp, NFSLCK_CHECK, NULL, &nfh, p);
5142 error = nfsrv_getlockfile(NFSLCK_CHECK, NULL, &lfp, &nfh, 0);
5151 * Now, look for a write delegation.
5153 LIST_FOREACH(stp, &lfp->lf_deleg, ls_file) {
5154 if (stp->ls_flags & NFSLCK_DELEGWRITE)
5157 if (stp == LIST_END(&lfp->lf_deleg)) {
5162 delegfilerev = stp->ls_filerev;
5165 * If the Write delegation was issued as a part of this Compound RPC
5166 * or if we have an Implied Clientid (used in a previous Op in this
5167 * compound) and it is the client the delegation was issued to,
5169 * I also assume that it is from the same client iff the network
5170 * host IP address is the same as the callback address. (Not
5171 * exactly correct by the RFC, but avoids a lot of Getattr
5174 if (nd->nd_compref == stp->ls_compref ||
5175 ((nd->nd_flag & ND_IMPLIEDCLID) &&
5176 clp->lc_clientid.qval == nd->nd_clientid.qval) ||
5177 nfsaddr2_match(clp->lc_req.nr_nam, nd->nd_nam)) {
5183 * We are now done with the delegation state structure,
5184 * so the statelock can be released and we can now tsleep().
5188 * Now, we must do the CB Getattr callback, to see if Change or Size
5191 if (clp->lc_expiry >= NFSD_MONOSEC) {
5193 NFSVNO_ATTRINIT(&nva);
5194 nva.na_filerev = NFS64BITSSET;
5195 error = nfsrv_docallback(clp, NFSV4OP_CBGETATTR, NULL,
5196 0, &nfh, &nva, &cbbits, p);
5198 if ((nva.na_filerev != NFS64BITSSET &&
5199 nva.na_filerev > delegfilerev) ||
5200 (NFSVNO_ISSETSIZE(&nva) &&
5201 nva.na_size != nvap->na_size)) {
5202 error = nfsvno_updfilerev(vp, nvap, cred, p);
5203 if (NFSVNO_ISSETSIZE(&nva))
5204 nvap->na_size = nva.na_size;
5207 error = 0; /* Ignore callback errors for now. */
5213 NFSEXITCODE2(error, nd);
5218 * This function looks for openowners that haven't had any opens for
5219 * a while and throws them away. Called by an nfsd when NFSNSF_NOOPENS
5223 nfsrv_throwawayopens(NFSPROC_T *p)
5225 struct nfsclient *clp, *nclp;
5226 struct nfsstate *stp, *nstp;
5230 nfsrv_stablefirst.nsf_flags &= ~NFSNSF_NOOPENS;
5232 * For each client...
5234 for (i = 0; i < NFSCLIENTHASHSIZE; i++) {
5235 LIST_FOREACH_SAFE(clp, &nfsclienthash[i], lc_hash, nclp) {
5236 LIST_FOREACH_SAFE(stp, &clp->lc_open, ls_list, nstp) {
5237 if (LIST_EMPTY(&stp->ls_open) &&
5238 (stp->ls_noopens > NFSNOOPEN ||
5239 (nfsrv_openpluslock * 2) >
5240 NFSRV_V4STATELIMIT))
5241 nfsrv_freeopenowner(stp, 0, p);
5249 * This function checks to see if the credentials are the same.
5250 * Returns 1 for not same, 0 otherwise.
5253 nfsrv_notsamecredname(struct nfsrv_descript *nd, struct nfsclient *clp)
5256 if (nd->nd_flag & ND_GSS) {
5257 if (!(clp->lc_flags & LCL_GSS))
5259 if (clp->lc_flags & LCL_NAME) {
5260 if (nd->nd_princlen != clp->lc_namelen ||
5261 NFSBCMP(nd->nd_principal, clp->lc_name,
5267 if (nd->nd_cred->cr_uid == clp->lc_uid)
5271 } else if (clp->lc_flags & LCL_GSS)
5274 * For AUTH_SYS, allow the same uid or root. (This is underspecified
5275 * in RFC3530, which talks about principals, but doesn't say anything
5276 * about uids for AUTH_SYS.)
5278 if (nd->nd_cred->cr_uid == clp->lc_uid || nd->nd_cred->cr_uid == 0)
5285 * Calculate the lease expiry time.
5288 nfsrv_leaseexpiry(void)
5291 if (nfsrv_stablefirst.nsf_eograce > NFSD_MONOSEC)
5292 return (NFSD_MONOSEC + 2 * (nfsrv_lease + NFSRV_LEASEDELTA));
5293 return (NFSD_MONOSEC + nfsrv_lease + NFSRV_LEASEDELTA);
5297 * Delay the delegation timeout as far as ls_delegtimelimit, as required.
5300 nfsrv_delaydelegtimeout(struct nfsstate *stp)
5303 if ((stp->ls_flags & NFSLCK_DELEGRECALL) == 0)
5306 if ((stp->ls_delegtime + 15) > NFSD_MONOSEC &&
5307 stp->ls_delegtime < stp->ls_delegtimelimit) {
5308 stp->ls_delegtime += nfsrv_lease;
5309 if (stp->ls_delegtime > stp->ls_delegtimelimit)
5310 stp->ls_delegtime = stp->ls_delegtimelimit;
5315 * This function checks to see if there is any other state associated
5316 * with the openowner for this Open.
5317 * It returns 1 if there is no other state, 0 otherwise.
5320 nfsrv_nootherstate(struct nfsstate *stp)
5322 struct nfsstate *tstp;
5324 LIST_FOREACH(tstp, &stp->ls_openowner->ls_open, ls_list) {
5325 if (tstp != stp || !LIST_EMPTY(&tstp->ls_lock))
5332 * Create a list of lock deltas (changes to local byte range locking
5333 * that can be rolled back using the list) and apply the changes via
5334 * nfsvno_advlock(). Optionally, lock the list. It is expected that either
5335 * the rollback or update function will be called after this.
5336 * It returns an error (and rolls back, as required), if any nfsvno_advlock()
5337 * call fails. If it returns an error, it will unlock the list.
5340 nfsrv_locallock(vnode_t vp, struct nfslockfile *lfp, int flags,
5341 uint64_t first, uint64_t end, struct nfslockconflict *cfp, NFSPROC_T *p)
5343 struct nfslock *lop, *nlop;
5346 /* Loop through the list of locks. */
5347 lop = LIST_FIRST(&lfp->lf_locallock);
5348 while (first < end && lop != NULL) {
5349 nlop = LIST_NEXT(lop, lo_lckowner);
5350 if (first >= lop->lo_end) {
5353 } else if (first < lop->lo_first) {
5354 /* new one starts before entry in list */
5355 if (end <= lop->lo_first) {
5356 /* no overlap between old and new */
5357 error = nfsrv_dolocal(vp, lfp, flags,
5358 NFSLCK_UNLOCK, first, end, cfp, p);
5363 /* handle fragment overlapped with new one */
5364 error = nfsrv_dolocal(vp, lfp, flags,
5365 NFSLCK_UNLOCK, first, lop->lo_first, cfp,
5369 first = lop->lo_first;
5372 /* new one overlaps this entry in list */
5373 if (end <= lop->lo_end) {
5374 /* overlaps all of new one */
5375 error = nfsrv_dolocal(vp, lfp, flags,
5376 lop->lo_flags, first, end, cfp, p);
5381 /* handle fragment overlapped with new one */
5382 error = nfsrv_dolocal(vp, lfp, flags,
5383 lop->lo_flags, first, lop->lo_end, cfp, p);
5386 first = lop->lo_end;
5391 if (first < end && error == 0)
5392 /* handle fragment past end of list */
5393 error = nfsrv_dolocal(vp, lfp, flags, NFSLCK_UNLOCK, first,
5401 * Local lock unlock. Unlock all byte ranges that are no longer locked
5402 * by NFSv4. To do this, unlock any subranges of first-->end that
5403 * do not overlap with the byte ranges of any lock in the lfp->lf_lock
5404 * list. This list has all locks for the file held by other
5405 * <clientid, lockowner> tuples. The list is ordered by increasing
5406 * lo_first value, but may have entries that overlap each other, for
5407 * the case of read locks.
5410 nfsrv_localunlock(vnode_t vp, struct nfslockfile *lfp, uint64_t init_first,
5411 uint64_t init_end, NFSPROC_T *p)
5413 struct nfslock *lop;
5414 uint64_t first, end, prevfirst;
5418 while (first < init_end) {
5419 /* Loop through all nfs locks, adjusting first and end */
5421 LIST_FOREACH(lop, &lfp->lf_lock, lo_lckfile) {
5422 KASSERT(prevfirst <= lop->lo_first,
5423 ("nfsv4 locks out of order"));
5424 KASSERT(lop->lo_first < lop->lo_end,
5425 ("nfsv4 bogus lock"));
5426 prevfirst = lop->lo_first;
5427 if (first >= lop->lo_first &&
5428 first < lop->lo_end)
5430 * Overlaps with initial part, so trim
5431 * off that initial part by moving first past
5434 first = lop->lo_end;
5435 else if (end > lop->lo_first &&
5436 lop->lo_first > first) {
5438 * This lock defines the end of the
5439 * segment to unlock, so set end to the
5440 * start of it and break out of the loop.
5442 end = lop->lo_first;
5447 * There is no segment left to do, so
5448 * break out of this loop and then exit
5449 * the outer while() since first will be set
5450 * to end, which must equal init_end here.
5455 /* Unlock this segment */
5456 (void) nfsrv_dolocal(vp, lfp, NFSLCK_UNLOCK,
5457 NFSLCK_READ, first, end, NULL, p);
5458 nfsrv_locallock_commit(lfp, NFSLCK_UNLOCK,
5462 * Now move past this segment and look for any further
5463 * segment in the range, if there is one.
5471 * Do the local lock operation and update the rollback list, as required.
5472 * Perform the rollback and return the error if nfsvno_advlock() fails.
5475 nfsrv_dolocal(vnode_t vp, struct nfslockfile *lfp, int flags, int oldflags,
5476 uint64_t first, uint64_t end, struct nfslockconflict *cfp, NFSPROC_T *p)
5478 struct nfsrollback *rlp;
5479 int error = 0, ltype, oldltype;
5481 if (flags & NFSLCK_WRITE)
5483 else if (flags & NFSLCK_READ)
5487 if (oldflags & NFSLCK_WRITE)
5489 else if (oldflags & NFSLCK_READ)
5493 if (ltype == oldltype || (oldltype == F_WRLCK && ltype == F_RDLCK))
5496 error = nfsvno_advlock(vp, ltype, first, end, p);
5499 cfp->cl_clientid.lval[0] = 0;
5500 cfp->cl_clientid.lval[1] = 0;
5502 cfp->cl_end = NFS64BITSSET;
5503 cfp->cl_flags = NFSLCK_WRITE;
5504 cfp->cl_ownerlen = 5;
5505 NFSBCOPY("LOCAL", cfp->cl_owner, 5);
5507 nfsrv_locallock_rollback(vp, lfp, p);
5508 } else if (ltype != F_UNLCK) {
5509 rlp = malloc(sizeof (struct nfsrollback), M_NFSDROLLBACK,
5511 rlp->rlck_first = first;
5512 rlp->rlck_end = end;
5513 rlp->rlck_type = oldltype;
5514 LIST_INSERT_HEAD(&lfp->lf_rollback, rlp, rlck_list);
5523 * Roll back local lock changes and free up the rollback list.
5526 nfsrv_locallock_rollback(vnode_t vp, struct nfslockfile *lfp, NFSPROC_T *p)
5528 struct nfsrollback *rlp, *nrlp;
5530 LIST_FOREACH_SAFE(rlp, &lfp->lf_rollback, rlck_list, nrlp) {
5531 (void) nfsvno_advlock(vp, rlp->rlck_type, rlp->rlck_first,
5533 free(rlp, M_NFSDROLLBACK);
5535 LIST_INIT(&lfp->lf_rollback);
5539 * Update local lock list and delete rollback list (ie now committed to the
5540 * local locks). Most of the work is done by the internal function.
5543 nfsrv_locallock_commit(struct nfslockfile *lfp, int flags, uint64_t first,
5546 struct nfsrollback *rlp, *nrlp;
5547 struct nfslock *new_lop, *other_lop;
5549 new_lop = malloc(sizeof (struct nfslock), M_NFSDLOCK, M_WAITOK);
5550 if (flags & (NFSLCK_READ | NFSLCK_WRITE))
5551 other_lop = malloc(sizeof (struct nfslock), M_NFSDLOCK,
5555 new_lop->lo_flags = flags;
5556 new_lop->lo_first = first;
5557 new_lop->lo_end = end;
5558 nfsrv_updatelock(NULL, &new_lop, &other_lop, lfp);
5559 if (new_lop != NULL)
5560 free(new_lop, M_NFSDLOCK);
5561 if (other_lop != NULL)
5562 free(other_lop, M_NFSDLOCK);
5564 /* and get rid of the rollback list */
5565 LIST_FOREACH_SAFE(rlp, &lfp->lf_rollback, rlck_list, nrlp)
5566 free(rlp, M_NFSDROLLBACK);
5567 LIST_INIT(&lfp->lf_rollback);
5571 * Lock the struct nfslockfile for local lock updating.
5574 nfsrv_locklf(struct nfslockfile *lfp)
5578 /* lf_usecount ensures *lfp won't be free'd */
5581 gotlock = nfsv4_lock(&lfp->lf_locallock_lck, 1, NULL,
5582 NFSSTATEMUTEXPTR, NULL);
5583 } while (gotlock == 0);
5588 * Unlock the struct nfslockfile after local lock updating.
5591 nfsrv_unlocklf(struct nfslockfile *lfp)
5594 nfsv4_unlock(&lfp->lf_locallock_lck, 0);
5598 * Clear out all state for the NFSv4 server.
5599 * Must be called by a thread that can sleep when no nfsds are running.
5602 nfsrv_throwawayallstate(NFSPROC_T *p)
5604 struct nfsclient *clp, *nclp;
5605 struct nfslockfile *lfp, *nlfp;
5609 * For each client, clean out the state and then free the structure.
5611 for (i = 0; i < NFSCLIENTHASHSIZE; i++) {
5612 LIST_FOREACH_SAFE(clp, &nfsclienthash[i], lc_hash, nclp) {
5613 nfsrv_cleanclient(clp, p);
5614 nfsrv_freedeleglist(&clp->lc_deleg);
5615 nfsrv_freedeleglist(&clp->lc_olddeleg);
5616 free(clp, M_NFSDCLIENT);
5621 * Also, free up any remaining lock file structures.
5623 for (i = 0; i < NFSLOCKHASHSIZE; i++) {
5624 LIST_FOREACH_SAFE(lfp, &nfslockhash[i], lf_hash, nlfp) {
5625 printf("nfsd unload: fnd a lock file struct\n");
5626 nfsrv_freenfslockfile(lfp);
5632 * Check the sequence# for the session and slot provided as an argument.
5633 * Also, renew the lease if the session will return NFS_OK.
5636 nfsrv_checksequence(struct nfsrv_descript *nd, uint32_t sequenceid,
5637 uint32_t *highest_slotidp, uint32_t *target_highest_slotidp, int cache_this,
5638 uint32_t *sflagsp, NFSPROC_T *p)
5640 struct nfsdsession *sep;
5641 struct nfssessionhash *shp;
5645 shp = NFSSESSIONHASH(nd->nd_sessionid);
5646 NFSLOCKSESSION(shp);
5647 sep = nfsrv_findsession(nd->nd_sessionid);
5649 NFSUNLOCKSESSION(shp);
5650 return (NFSERR_BADSESSION);
5652 error = nfsv4_seqsession(sequenceid, nd->nd_slotid, *highest_slotidp,
5653 sep->sess_slots, NULL, NFSV4_SLOTS - 1);
5655 NFSUNLOCKSESSION(shp);
5658 if (cache_this != 0)
5659 nd->nd_flag |= ND_SAVEREPLY;
5660 /* Renew the lease. */
5661 sep->sess_clp->lc_expiry = nfsrv_leaseexpiry();
5662 nd->nd_clientid.qval = sep->sess_clp->lc_clientid.qval;
5663 nd->nd_flag |= ND_IMPLIEDCLID;
5666 * If this session handles the backchannel, save the nd_xprt for this
5667 * RPC, since this is the one being used.
5669 if (sep->sess_cbsess.nfsess_xprt != NULL &&
5670 (sep->sess_crflags & NFSV4CRSESS_CONNBACKCHAN) != 0) {
5671 savxprt = sep->sess_cbsess.nfsess_xprt;
5672 SVC_ACQUIRE(nd->nd_xprt);
5673 nd->nd_xprt->xp_p2 = savxprt->xp_p2;
5674 nd->nd_xprt->xp_idletimeout = 0; /* Disable timeout. */
5675 sep->sess_cbsess.nfsess_xprt = nd->nd_xprt;
5676 SVC_RELEASE(savxprt);
5680 if (sep->sess_clp->lc_req.nr_client == NULL)
5681 *sflagsp |= NFSV4SEQ_CBPATHDOWN;
5682 NFSUNLOCKSESSION(shp);
5683 if (error == NFSERR_EXPIRED) {
5684 *sflagsp |= NFSV4SEQ_EXPIREDALLSTATEREVOKED;
5686 } else if (error == NFSERR_ADMINREVOKED) {
5687 *sflagsp |= NFSV4SEQ_ADMINSTATEREVOKED;
5690 *highest_slotidp = *target_highest_slotidp = NFSV4_SLOTS - 1;
5695 * Check/set reclaim complete for this session/clientid.
5698 nfsrv_checkreclaimcomplete(struct nfsrv_descript *nd)
5700 struct nfsdsession *sep;
5701 struct nfssessionhash *shp;
5704 shp = NFSSESSIONHASH(nd->nd_sessionid);
5706 NFSLOCKSESSION(shp);
5707 sep = nfsrv_findsession(nd->nd_sessionid);
5709 NFSUNLOCKSESSION(shp);
5711 return (NFSERR_BADSESSION);
5714 /* Check to see if reclaim complete has already happened. */
5715 if ((sep->sess_clp->lc_flags & LCL_RECLAIMCOMPLETE) != 0)
5716 error = NFSERR_COMPLETEALREADY;
5718 sep->sess_clp->lc_flags |= LCL_RECLAIMCOMPLETE;
5719 NFSUNLOCKSESSION(shp);
5725 * Cache the reply in a session slot.
5728 nfsrv_cache_session(uint8_t *sessionid, uint32_t slotid, int repstat,
5731 struct nfsdsession *sep;
5732 struct nfssessionhash *shp;
5734 shp = NFSSESSIONHASH(sessionid);
5735 NFSLOCKSESSION(shp);
5736 sep = nfsrv_findsession(sessionid);
5738 NFSUNLOCKSESSION(shp);
5739 printf("nfsrv_cache_session: no session\n");
5743 nfsv4_seqsess_cacherep(slotid, sep->sess_slots, repstat, m);
5744 NFSUNLOCKSESSION(shp);
5748 * Search for a session that matches the sessionid.
5750 static struct nfsdsession *
5751 nfsrv_findsession(uint8_t *sessionid)
5753 struct nfsdsession *sep;
5754 struct nfssessionhash *shp;
5756 shp = NFSSESSIONHASH(sessionid);
5757 LIST_FOREACH(sep, &shp->list, sess_hash) {
5758 if (!NFSBCMP(sessionid, sep->sess_sessionid, NFSX_V4SESSIONID))
5765 * Destroy a session.
5768 nfsrv_destroysession(struct nfsrv_descript *nd, uint8_t *sessionid)
5770 int error, samesess;
5773 if (!NFSBCMP(sessionid, nd->nd_sessionid, NFSX_V4SESSIONID)) {
5775 if ((nd->nd_flag & ND_LASTOP) == 0)
5776 return (NFSERR_BADSESSION);
5778 error = nfsrv_freesession(NULL, sessionid);
5779 if (error == 0 && samesess != 0)
5780 nd->nd_flag &= ~ND_HASSEQUENCE;
5785 * Free up a session structure.
5788 nfsrv_freesession(struct nfsdsession *sep, uint8_t *sessionid)
5790 struct nfssessionhash *shp;
5794 shp = NFSSESSIONHASH(sessionid);
5795 NFSLOCKSESSION(shp);
5796 sep = nfsrv_findsession(sessionid);
5798 shp = NFSSESSIONHASH(sep->sess_sessionid);
5799 NFSLOCKSESSION(shp);
5804 if (sep->sess_refcnt > 0) {
5806 NFSUNLOCKSESSION(shp);
5809 LIST_REMOVE(sep, sess_hash);
5810 LIST_REMOVE(sep, sess_list);
5813 NFSUNLOCKSESSION(shp);
5815 return (NFSERR_BADSESSION);
5816 for (i = 0; i < NFSV4_SLOTS; i++)
5817 if (sep->sess_slots[i].nfssl_reply != NULL)
5818 m_freem(sep->sess_slots[i].nfssl_reply);
5819 if (sep->sess_cbsess.nfsess_xprt != NULL)
5820 SVC_RELEASE(sep->sess_cbsess.nfsess_xprt);
5821 free(sep, M_NFSDSESSION);
5827 * RFC5661 says that it should fail when there are associated opens, locks
5828 * or delegations. Since stateids represent opens, I don't see how you can
5829 * free an open stateid (it will be free'd when closed), so this function
5830 * only works for lock stateids (freeing the lock_owner) or delegations.
5833 nfsrv_freestateid(struct nfsrv_descript *nd, nfsv4stateid_t *stateidp,
5836 struct nfsclient *clp;
5837 struct nfsstate *stp;
5842 * Look up the stateid
5844 error = nfsrv_getclient((nfsquad_t)((u_quad_t)0), CLOPS_RENEW, &clp,
5845 NULL, (nfsquad_t)((u_quad_t)0), 0, nd, p);
5847 /* First, check for a delegation. */
5848 LIST_FOREACH(stp, &clp->lc_deleg, ls_list) {
5849 if (!NFSBCMP(stp->ls_stateid.other, stateidp->other,
5854 nfsrv_freedeleg(stp);
5859 /* Not a delegation, try for a lock_owner. */
5861 error = nfsrv_getstate(clp, stateidp, 0, &stp);
5862 if (error == 0 && ((stp->ls_flags & (NFSLCK_OPEN | NFSLCK_DELEGREAD |
5863 NFSLCK_DELEGWRITE)) != 0 || (stp->ls_flags & NFSLCK_LOCK) == 0))
5864 /* Not a lock_owner stateid. */
5865 error = NFSERR_LOCKSHELD;
5866 if (error == 0 && !LIST_EMPTY(&stp->ls_lock))
5867 error = NFSERR_LOCKSHELD;
5869 nfsrv_freelockowner(stp, NULL, 0, p);
5875 * Generate the xdr for an NFSv4.1 CBSequence Operation.
5878 nfsv4_setcbsequence(struct nfsrv_descript *nd, struct nfsclient *clp,
5879 int dont_replycache, struct nfsdsession **sepp)
5881 struct nfsdsession *sep;
5882 uint32_t *tl, slotseq = 0;
5883 int maxslot, slotpos;
5884 uint8_t sessionid[NFSX_V4SESSIONID];
5887 error = nfsv4_getcbsession(clp, sepp);
5891 (void)nfsv4_sequencelookup(NULL, &sep->sess_cbsess, &slotpos, &maxslot,
5892 &slotseq, sessionid);
5893 KASSERT(maxslot >= 0, ("nfsv4_setcbsequence neg maxslot"));
5895 /* Build the Sequence arguments. */
5896 NFSM_BUILD(tl, uint32_t *, NFSX_V4SESSIONID + 5 * NFSX_UNSIGNED);
5897 bcopy(sessionid, tl, NFSX_V4SESSIONID);
5898 tl += NFSX_V4SESSIONID / NFSX_UNSIGNED;
5899 nd->nd_slotseq = tl;
5900 *tl++ = txdr_unsigned(slotseq);
5901 *tl++ = txdr_unsigned(slotpos);
5902 *tl++ = txdr_unsigned(maxslot);
5903 if (dont_replycache == 0)
5904 *tl++ = newnfs_true;
5906 *tl++ = newnfs_false;
5907 *tl = 0; /* No referring call list, for now. */
5908 nd->nd_flag |= ND_HASSEQUENCE;
5913 * Get a session for the callback.
5916 nfsv4_getcbsession(struct nfsclient *clp, struct nfsdsession **sepp)
5918 struct nfsdsession *sep;
5921 LIST_FOREACH(sep, &clp->lc_session, sess_list) {
5922 if ((sep->sess_crflags & NFSV4CRSESS_CONNBACKCHAN) != 0)
5927 return (NFSERR_BADSESSION);