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 "opt_inet6.h"
34 #include <fs/nfs/nfsport.h>
36 struct nfsrv_stablefirst nfsrv_stablefirst;
37 int nfsrv_issuedelegs = 0;
38 int nfsrv_dolocallocks = 0;
39 struct nfsv4lock nfsv4rootfs_lock;
41 extern int newnfs_numnfsd;
42 extern struct nfsstats newnfsstats;
43 extern int nfsrv_lease;
44 extern struct timeval nfsboottime;
45 extern u_int32_t newnfs_true, newnfs_false;
46 extern int nfsd_debuglevel;
50 SYSCTL_DECL(_vfs_nfsd);
51 int nfsrv_statehashsize = NFSSTATEHASHSIZE;
52 TUNABLE_INT("vfs.nfsd.statehashsize", &nfsrv_statehashsize);
53 SYSCTL_INT(_vfs_nfsd, OID_AUTO, statehashsize, CTLFLAG_RDTUN,
54 &nfsrv_statehashsize, 0,
55 "Size of state hash table set via loader.conf");
57 int nfsrv_clienthashsize = NFSCLIENTHASHSIZE;
58 TUNABLE_INT("vfs.nfsd.clienthashsize", &nfsrv_clienthashsize);
59 SYSCTL_INT(_vfs_nfsd, OID_AUTO, clienthashsize, CTLFLAG_RDTUN,
60 &nfsrv_clienthashsize, 0,
61 "Size of client hash table set via loader.conf");
63 int nfsrv_lockhashsize = NFSLOCKHASHSIZE;
64 TUNABLE_INT("vfs.nfsd.fhhashsize", &nfsrv_lockhashsize);
65 SYSCTL_INT(_vfs_nfsd, OID_AUTO, fhhashsize, CTLFLAG_RDTUN,
66 &nfsrv_lockhashsize, 0,
67 "Size of file handle hash table set via loader.conf");
69 int nfsrv_sessionhashsize = NFSSESSIONHASHSIZE;
70 TUNABLE_INT("vfs.nfsd.sessionhashsize", &nfsrv_sessionhashsize);
71 SYSCTL_INT(_vfs_nfsd, OID_AUTO, sessionhashsize, CTLFLAG_RDTUN,
72 &nfsrv_sessionhashsize, 0,
73 "Size of session hash table set via loader.conf");
75 static int nfsrv_v4statelimit = NFSRV_V4STATELIMIT;
76 TUNABLE_INT("vfs.nfsd.v4statelimit", &nfsrv_v4statelimit);
77 SYSCTL_INT(_vfs_nfsd, OID_AUTO, v4statelimit, CTLFLAG_RWTUN,
78 &nfsrv_v4statelimit, 0,
79 "High water limit for NFSv4 opens+locks+delegations");
81 static int nfsrv_writedelegifpos = 0;
82 SYSCTL_INT(_vfs_nfsd, OID_AUTO, writedelegifpos, CTLFLAG_RW,
83 &nfsrv_writedelegifpos, 0,
84 "Issue a write delegation for read opens if possible");
86 static int nfsrv_allowreadforwriteopen = 1;
87 SYSCTL_INT(_vfs_nfsd, OID_AUTO, allowreadforwriteopen, CTLFLAG_RW,
88 &nfsrv_allowreadforwriteopen, 0,
89 "Allow Reads to be done with Write Access StateIDs");
92 * Hash lists for nfs V4.
94 struct nfsclienthashhead *nfsclienthash;
95 struct nfslockhashhead *nfslockhash;
96 struct nfssessionhash *nfssessionhash;
97 #endif /* !APPLEKEXT */
99 static u_int32_t nfsrv_openpluslock = 0, nfsrv_delegatecnt = 0;
100 static time_t nfsrvboottime;
101 static int nfsrv_returnoldstateid = 0, nfsrv_clients = 0;
102 static int nfsrv_clienthighwater = NFSRV_CLIENTHIGHWATER;
103 static int nfsrv_nogsscallback = 0;
104 static volatile int nfsrv_writedelegcnt = 0;
106 /* local functions */
107 static void nfsrv_dumpaclient(struct nfsclient *clp,
108 struct nfsd_dumpclients *dumpp);
109 static void nfsrv_freeopenowner(struct nfsstate *stp, int cansleep,
111 static int nfsrv_freeopen(struct nfsstate *stp, vnode_t vp, int cansleep,
113 static void nfsrv_freelockowner(struct nfsstate *stp, vnode_t vp, int cansleep,
115 static void nfsrv_freeallnfslocks(struct nfsstate *stp, vnode_t vp,
116 int cansleep, NFSPROC_T *p);
117 static void nfsrv_freenfslock(struct nfslock *lop);
118 static void nfsrv_freenfslockfile(struct nfslockfile *lfp);
119 static void nfsrv_freedeleg(struct nfsstate *);
120 static int nfsrv_getstate(struct nfsclient *clp, nfsv4stateid_t *stateidp,
121 u_int32_t flags, struct nfsstate **stpp);
122 static void nfsrv_getowner(struct nfsstatehead *hp, struct nfsstate *new_stp,
123 struct nfsstate **stpp);
124 static int nfsrv_getlockfh(vnode_t vp, u_short flags,
125 struct nfslockfile *new_lfp, fhandle_t *nfhp, NFSPROC_T *p);
126 static int nfsrv_getlockfile(u_short flags, struct nfslockfile **new_lfpp,
127 struct nfslockfile **lfpp, fhandle_t *nfhp, int lockit);
128 static void nfsrv_insertlock(struct nfslock *new_lop,
129 struct nfslock *insert_lop, struct nfsstate *stp, struct nfslockfile *lfp);
130 static void nfsrv_updatelock(struct nfsstate *stp, struct nfslock **new_lopp,
131 struct nfslock **other_lopp, struct nfslockfile *lfp);
132 static int nfsrv_getipnumber(u_char *cp);
133 static int nfsrv_checkrestart(nfsquad_t clientid, u_int32_t flags,
134 nfsv4stateid_t *stateidp, int specialid);
135 static int nfsrv_checkgrace(struct nfsrv_descript *nd, struct nfsclient *clp,
137 static int nfsrv_docallback(struct nfsclient *clp, int procnum,
138 nfsv4stateid_t *stateidp, int trunc, fhandle_t *fhp,
139 struct nfsvattr *nap, nfsattrbit_t *attrbitp, NFSPROC_T *p);
140 static int nfsrv_cbcallargs(struct nfsrv_descript *nd, struct nfsclient *clp,
141 uint32_t callback, int op, const char *optag, struct nfsdsession **sepp);
142 static u_int32_t nfsrv_nextclientindex(void);
143 static u_int32_t nfsrv_nextstateindex(struct nfsclient *clp);
144 static void nfsrv_markstable(struct nfsclient *clp);
145 static void nfsrv_markreclaim(struct nfsclient *clp);
146 static int nfsrv_checkstable(struct nfsclient *clp);
147 static int nfsrv_clientconflict(struct nfsclient *clp, int *haslockp, struct
148 vnode *vp, NFSPROC_T *p);
149 static int nfsrv_delegconflict(struct nfsstate *stp, int *haslockp,
150 NFSPROC_T *p, vnode_t vp);
151 static int nfsrv_cleandeleg(vnode_t vp, struct nfslockfile *lfp,
152 struct nfsclient *clp, int *haslockp, NFSPROC_T *p);
153 static int nfsrv_notsamecredname(struct nfsrv_descript *nd,
154 struct nfsclient *clp);
155 static time_t nfsrv_leaseexpiry(void);
156 static void nfsrv_delaydelegtimeout(struct nfsstate *stp);
157 static int nfsrv_checkseqid(struct nfsrv_descript *nd, u_int32_t seqid,
158 struct nfsstate *stp, struct nfsrvcache *op);
159 static int nfsrv_nootherstate(struct nfsstate *stp);
160 static int nfsrv_locallock(vnode_t vp, struct nfslockfile *lfp, int flags,
161 uint64_t first, uint64_t end, struct nfslockconflict *cfp, NFSPROC_T *p);
162 static void nfsrv_localunlock(vnode_t vp, struct nfslockfile *lfp,
163 uint64_t init_first, uint64_t init_end, NFSPROC_T *p);
164 static int nfsrv_dolocal(vnode_t vp, struct nfslockfile *lfp, int flags,
165 int oldflags, uint64_t first, uint64_t end, struct nfslockconflict *cfp,
167 static void nfsrv_locallock_rollback(vnode_t vp, struct nfslockfile *lfp,
169 static void nfsrv_locallock_commit(struct nfslockfile *lfp, int flags,
170 uint64_t first, uint64_t end);
171 static void nfsrv_locklf(struct nfslockfile *lfp);
172 static void nfsrv_unlocklf(struct nfslockfile *lfp);
173 static struct nfsdsession *nfsrv_findsession(uint8_t *sessionid);
174 static int nfsrv_freesession(struct nfsdsession *sep, uint8_t *sessionid);
175 static int nfsv4_setcbsequence(struct nfsrv_descript *nd, struct nfsclient *clp,
176 int dont_replycache, struct nfsdsession **sepp);
177 static int nfsv4_getcbsession(struct nfsclient *clp, struct nfsdsession **sepp);
180 * Scan the client list for a match and either return the current one,
181 * create a new entry or return an error.
182 * If returning a non-error, the clp structure must either be linked into
183 * the client list or free'd.
186 nfsrv_setclient(struct nfsrv_descript *nd, struct nfsclient **new_clpp,
187 nfsquad_t *clientidp, nfsquad_t *confirmp, NFSPROC_T *p)
189 struct nfsclient *clp = NULL, *new_clp = *new_clpp;
190 int i, error = 0, ret;
191 struct nfsstate *stp, *tstp;
193 struct sockaddr_in *sin, *rin;
196 struct sockaddr_in6 *sin6, *rin6;
198 struct nfsdsession *sep, *nsep;
199 int zapit = 0, gotit, hasstate = 0, igotlock;
200 static u_int64_t confirm_index = 0;
203 * Check for state resource limit exceeded.
205 if (nfsrv_openpluslock > nfsrv_v4statelimit) {
206 error = NFSERR_RESOURCE;
210 if (nfsrv_issuedelegs == 0 ||
211 ((nd->nd_flag & ND_GSS) != 0 && nfsrv_nogsscallback != 0))
213 * Don't do callbacks when delegations are disabled or
214 * for AUTH_GSS unless enabled via nfsrv_nogsscallback.
215 * If establishing a callback connection is attempted
216 * when a firewall is blocking the callback path, the
217 * server may wait too long for the connect attempt to
218 * succeed during the Open. Some clients, such as Linux,
219 * may timeout and give up on the Open before the server
220 * replies. Also, since AUTH_GSS callbacks are not
221 * yet interoperability tested, they might cause the
222 * server to crap out, if they get past the Init call to
225 new_clp->lc_program = 0;
227 /* Lock out other nfsd threads */
228 NFSLOCKV4ROOTMUTEX();
229 nfsv4_relref(&nfsv4rootfs_lock);
231 igotlock = nfsv4_lock(&nfsv4rootfs_lock, 1, NULL,
232 NFSV4ROOTLOCKMUTEXPTR, NULL);
234 NFSUNLOCKV4ROOTMUTEX();
237 * Search for a match in the client list.
240 while (i < nfsrv_clienthashsize && !gotit) {
241 LIST_FOREACH(clp, &nfsclienthash[i], lc_hash) {
242 if (new_clp->lc_idlen == clp->lc_idlen &&
243 !NFSBCMP(new_clp->lc_id, clp->lc_id, clp->lc_idlen)) {
252 (clp->lc_flags & (LCL_NEEDSCONFIRM | LCL_ADMINREVOKED))) {
253 if ((nd->nd_flag & ND_NFSV41) != 0 && confirmp->lval[1] != 0) {
255 * For NFSv4.1, if confirmp->lval[1] is non-zero, the
256 * client is trying to update a confirmed clientid.
258 NFSLOCKV4ROOTMUTEX();
259 nfsv4_unlock(&nfsv4rootfs_lock, 1);
260 NFSUNLOCKV4ROOTMUTEX();
261 confirmp->lval[1] = 0;
262 error = NFSERR_NOENT;
266 * Get rid of the old one.
268 if (i != nfsrv_clienthashsize) {
269 LIST_REMOVE(clp, lc_hash);
270 nfsrv_cleanclient(clp, p);
271 nfsrv_freedeleglist(&clp->lc_deleg);
272 nfsrv_freedeleglist(&clp->lc_olddeleg);
276 * Add it after assigning a client id to it.
278 new_clp->lc_flags |= LCL_NEEDSCONFIRM;
279 if ((nd->nd_flag & ND_NFSV41) != 0)
280 new_clp->lc_confirm.lval[0] = confirmp->lval[0] =
283 confirmp->qval = new_clp->lc_confirm.qval =
285 clientidp->lval[0] = new_clp->lc_clientid.lval[0] =
286 (u_int32_t)nfsrvboottime;
287 clientidp->lval[1] = new_clp->lc_clientid.lval[1] =
288 nfsrv_nextclientindex();
289 new_clp->lc_stateindex = 0;
290 new_clp->lc_statemaxindex = 0;
291 new_clp->lc_cbref = 0;
292 new_clp->lc_expiry = nfsrv_leaseexpiry();
293 LIST_INIT(&new_clp->lc_open);
294 LIST_INIT(&new_clp->lc_deleg);
295 LIST_INIT(&new_clp->lc_olddeleg);
296 LIST_INIT(&new_clp->lc_session);
297 for (i = 0; i < nfsrv_statehashsize; i++)
298 LIST_INIT(&new_clp->lc_stateid[i]);
299 LIST_INSERT_HEAD(NFSCLIENTHASH(new_clp->lc_clientid), new_clp,
301 newnfsstats.srvclients++;
302 nfsrv_openpluslock++;
304 NFSLOCKV4ROOTMUTEX();
305 nfsv4_unlock(&nfsv4rootfs_lock, 1);
306 NFSUNLOCKV4ROOTMUTEX();
308 nfsrv_zapclient(clp, p);
314 * Now, handle the cases where the id is already issued.
316 if (nfsrv_notsamecredname(nd, clp)) {
318 * Check to see if there is expired state that should go away.
320 if (clp->lc_expiry < NFSD_MONOSEC &&
321 (!LIST_EMPTY(&clp->lc_open) || !LIST_EMPTY(&clp->lc_deleg))) {
322 nfsrv_cleanclient(clp, p);
323 nfsrv_freedeleglist(&clp->lc_deleg);
327 * If there is outstanding state, then reply NFSERR_CLIDINUSE per
328 * RFC3530 Sec. 8.1.2 last para.
330 if (!LIST_EMPTY(&clp->lc_deleg)) {
332 } else if (LIST_EMPTY(&clp->lc_open)) {
336 /* Look for an Open on the OpenOwner */
337 LIST_FOREACH(stp, &clp->lc_open, ls_list) {
338 if (!LIST_EMPTY(&stp->ls_open)) {
346 * If the uid doesn't match, return NFSERR_CLIDINUSE after
347 * filling out the correct ipaddr and portnum.
349 switch (clp->lc_req.nr_nam->sa_family) {
352 sin = (struct sockaddr_in *)new_clp->lc_req.nr_nam;
353 rin = (struct sockaddr_in *)clp->lc_req.nr_nam;
354 sin->sin_addr.s_addr = rin->sin_addr.s_addr;
355 sin->sin_port = rin->sin_port;
360 sin6 = (struct sockaddr_in6 *)new_clp->lc_req.nr_nam;
361 rin6 = (struct sockaddr_in6 *)clp->lc_req.nr_nam;
362 sin6->sin6_addr = rin6->sin6_addr;
363 sin6->sin6_port = rin6->sin6_port;
367 NFSLOCKV4ROOTMUTEX();
368 nfsv4_unlock(&nfsv4rootfs_lock, 1);
369 NFSUNLOCKV4ROOTMUTEX();
370 error = NFSERR_CLIDINUSE;
375 if (NFSBCMP(new_clp->lc_verf, clp->lc_verf, NFSX_VERF)) {
377 * If the verifier has changed, the client has rebooted
378 * and a new client id is issued. The old state info
379 * can be thrown away once the SETCLIENTID_CONFIRM occurs.
381 LIST_REMOVE(clp, lc_hash);
383 /* Get rid of all sessions on this clientid. */
384 LIST_FOREACH_SAFE(sep, &clp->lc_session, sess_list, nsep) {
385 ret = nfsrv_freesession(sep, NULL);
387 printf("nfsrv_setclient: verifier changed free"
388 " session failed=%d\n", ret);
391 new_clp->lc_flags |= LCL_NEEDSCONFIRM;
392 if ((nd->nd_flag & ND_NFSV41) != 0)
393 new_clp->lc_confirm.lval[0] = confirmp->lval[0] =
396 confirmp->qval = new_clp->lc_confirm.qval =
398 clientidp->lval[0] = new_clp->lc_clientid.lval[0] =
400 clientidp->lval[1] = new_clp->lc_clientid.lval[1] =
401 nfsrv_nextclientindex();
402 new_clp->lc_stateindex = 0;
403 new_clp->lc_statemaxindex = 0;
404 new_clp->lc_cbref = 0;
405 new_clp->lc_expiry = nfsrv_leaseexpiry();
408 * Save the state until confirmed.
410 LIST_NEWHEAD(&new_clp->lc_open, &clp->lc_open, ls_list);
411 LIST_FOREACH(tstp, &new_clp->lc_open, ls_list)
412 tstp->ls_clp = new_clp;
413 LIST_NEWHEAD(&new_clp->lc_deleg, &clp->lc_deleg, ls_list);
414 LIST_FOREACH(tstp, &new_clp->lc_deleg, ls_list)
415 tstp->ls_clp = new_clp;
416 LIST_NEWHEAD(&new_clp->lc_olddeleg, &clp->lc_olddeleg,
418 LIST_FOREACH(tstp, &new_clp->lc_olddeleg, ls_list)
419 tstp->ls_clp = new_clp;
420 for (i = 0; i < nfsrv_statehashsize; i++) {
421 LIST_NEWHEAD(&new_clp->lc_stateid[i],
422 &clp->lc_stateid[i], ls_hash);
423 LIST_FOREACH(tstp, &new_clp->lc_stateid[i], ls_hash)
424 tstp->ls_clp = new_clp;
426 LIST_INIT(&new_clp->lc_session);
427 LIST_INSERT_HEAD(NFSCLIENTHASH(new_clp->lc_clientid), new_clp,
429 newnfsstats.srvclients++;
430 nfsrv_openpluslock++;
432 NFSLOCKV4ROOTMUTEX();
433 nfsv4_unlock(&nfsv4rootfs_lock, 1);
434 NFSUNLOCKV4ROOTMUTEX();
437 * Must wait until any outstanding callback on the old clp
441 while (clp->lc_cbref) {
442 clp->lc_flags |= LCL_WAKEUPWANTED;
443 (void)mtx_sleep(clp, NFSSTATEMUTEXPTR, PZERO - 1,
444 "nfsd clp", 10 * hz);
447 nfsrv_zapclient(clp, p);
452 /* For NFSv4.1, mark that we found a confirmed clientid. */
453 if ((nd->nd_flag & ND_NFSV41) != 0) {
454 clientidp->lval[0] = clp->lc_clientid.lval[0];
455 clientidp->lval[1] = clp->lc_clientid.lval[1];
456 confirmp->lval[0] = 0; /* Ignored by client */
457 confirmp->lval[1] = 1;
460 * id and verifier match, so update the net address info
461 * and get rid of any existing callback authentication
462 * handle, so a new one will be acquired.
464 LIST_REMOVE(clp, lc_hash);
465 new_clp->lc_flags |= (LCL_NEEDSCONFIRM | LCL_DONTCLEAN);
466 new_clp->lc_expiry = nfsrv_leaseexpiry();
467 confirmp->qval = new_clp->lc_confirm.qval = ++confirm_index;
468 clientidp->lval[0] = new_clp->lc_clientid.lval[0] =
469 clp->lc_clientid.lval[0];
470 clientidp->lval[1] = new_clp->lc_clientid.lval[1] =
471 clp->lc_clientid.lval[1];
472 new_clp->lc_delegtime = clp->lc_delegtime;
473 new_clp->lc_stateindex = clp->lc_stateindex;
474 new_clp->lc_statemaxindex = clp->lc_statemaxindex;
475 new_clp->lc_cbref = 0;
476 LIST_NEWHEAD(&new_clp->lc_open, &clp->lc_open, ls_list);
477 LIST_FOREACH(tstp, &new_clp->lc_open, ls_list)
478 tstp->ls_clp = new_clp;
479 LIST_NEWHEAD(&new_clp->lc_deleg, &clp->lc_deleg, ls_list);
480 LIST_FOREACH(tstp, &new_clp->lc_deleg, ls_list)
481 tstp->ls_clp = new_clp;
482 LIST_NEWHEAD(&new_clp->lc_olddeleg, &clp->lc_olddeleg, ls_list);
483 LIST_FOREACH(tstp, &new_clp->lc_olddeleg, ls_list)
484 tstp->ls_clp = new_clp;
485 for (i = 0; i < nfsrv_statehashsize; i++) {
486 LIST_NEWHEAD(&new_clp->lc_stateid[i],
487 &clp->lc_stateid[i], ls_hash);
488 LIST_FOREACH(tstp, &new_clp->lc_stateid[i], ls_hash)
489 tstp->ls_clp = new_clp;
491 LIST_INIT(&new_clp->lc_session);
492 LIST_INSERT_HEAD(NFSCLIENTHASH(new_clp->lc_clientid), new_clp,
494 newnfsstats.srvclients++;
495 nfsrv_openpluslock++;
498 NFSLOCKV4ROOTMUTEX();
499 nfsv4_unlock(&nfsv4rootfs_lock, 1);
500 NFSUNLOCKV4ROOTMUTEX();
502 if ((nd->nd_flag & ND_NFSV41) == 0) {
504 * Must wait until any outstanding callback on the old clp
508 while (clp->lc_cbref) {
509 clp->lc_flags |= LCL_WAKEUPWANTED;
510 (void)mtx_sleep(clp, NFSSTATEMUTEXPTR, PZERO - 1,
514 nfsrv_zapclient(clp, p);
519 NFSEXITCODE2(error, nd);
524 * Check to see if the client id exists and optionally confirm it.
527 nfsrv_getclient(nfsquad_t clientid, int opflags, struct nfsclient **clpp,
528 struct nfsdsession *nsep, nfsquad_t confirm, uint32_t cbprogram,
529 struct nfsrv_descript *nd, NFSPROC_T *p)
531 struct nfsclient *clp;
532 struct nfsstate *stp;
534 struct nfsclienthashhead *hp;
535 int error = 0, igotlock, doneok;
536 struct nfssessionhash *shp;
537 struct nfsdsession *sep;
539 static uint64_t next_sess = 0;
543 if ((nd == NULL || (nd->nd_flag & ND_NFSV41) == 0 ||
544 opflags != CLOPS_RENEW) && nfsrvboottime != clientid.lval[0]) {
545 error = NFSERR_STALECLIENTID;
550 * If called with opflags == CLOPS_RENEW, the State Lock is
551 * already held. Otherwise, we need to get either that or,
552 * for the case of Confirm, lock out the nfsd threads.
554 if (opflags & CLOPS_CONFIRM) {
555 NFSLOCKV4ROOTMUTEX();
556 nfsv4_relref(&nfsv4rootfs_lock);
558 igotlock = nfsv4_lock(&nfsv4rootfs_lock, 1, NULL,
559 NFSV4ROOTLOCKMUTEXPTR, NULL);
562 * Create a new sessionid here, since we need to do it where
563 * there is a mutex held to serialize update of next_sess.
565 if ((nd->nd_flag & ND_NFSV41) != 0) {
566 sessid[0] = ++next_sess;
567 sessid[1] = clientid.qval;
569 NFSUNLOCKV4ROOTMUTEX();
570 } else if (opflags != CLOPS_RENEW) {
574 /* For NFSv4.1, the clp is acquired from the associated session. */
575 if (nd != NULL && (nd->nd_flag & ND_NFSV41) != 0 &&
576 opflags == CLOPS_RENEW) {
578 if ((nd->nd_flag & ND_HASSEQUENCE) != 0) {
579 shp = NFSSESSIONHASH(nd->nd_sessionid);
581 sep = nfsrv_findsession(nd->nd_sessionid);
584 NFSUNLOCKSESSION(shp);
587 hp = NFSCLIENTHASH(clientid);
588 LIST_FOREACH(clp, hp, lc_hash) {
589 if (clp->lc_clientid.lval[1] == clientid.lval[1])
594 if (opflags & CLOPS_CONFIRM)
595 error = NFSERR_STALECLIENTID;
597 error = NFSERR_EXPIRED;
598 } else if (clp->lc_flags & LCL_ADMINREVOKED) {
600 * If marked admin revoked, just return the error.
602 error = NFSERR_ADMINREVOKED;
605 if (opflags & CLOPS_CONFIRM) {
606 NFSLOCKV4ROOTMUTEX();
607 nfsv4_unlock(&nfsv4rootfs_lock, 1);
608 NFSUNLOCKV4ROOTMUTEX();
609 } else if (opflags != CLOPS_RENEW) {
616 * Perform any operations specified by the opflags.
618 if (opflags & CLOPS_CONFIRM) {
619 if (((nd->nd_flag & ND_NFSV41) != 0 &&
620 clp->lc_confirm.lval[0] != confirm.lval[0]) ||
621 ((nd->nd_flag & ND_NFSV41) == 0 &&
622 clp->lc_confirm.qval != confirm.qval))
623 error = NFSERR_STALECLIENTID;
624 else if (nfsrv_notsamecredname(nd, clp))
625 error = NFSERR_CLIDINUSE;
628 if ((clp->lc_flags & (LCL_NEEDSCONFIRM | LCL_DONTCLEAN)) ==
631 * Hang onto the delegations (as old delegations)
632 * for an Open with CLAIM_DELEGATE_PREV unless in
633 * grace, but get rid of the rest of the state.
635 nfsrv_cleanclient(clp, p);
636 nfsrv_freedeleglist(&clp->lc_olddeleg);
637 if (nfsrv_checkgrace(nd, clp, 0)) {
638 /* In grace, so just delete delegations */
639 nfsrv_freedeleglist(&clp->lc_deleg);
641 LIST_FOREACH(stp, &clp->lc_deleg, ls_list)
642 stp->ls_flags |= NFSLCK_OLDDELEG;
643 clp->lc_delegtime = NFSD_MONOSEC +
644 nfsrv_lease + NFSRV_LEASEDELTA;
645 LIST_NEWHEAD(&clp->lc_olddeleg, &clp->lc_deleg,
648 if ((nd->nd_flag & ND_NFSV41) != 0)
649 clp->lc_program = cbprogram;
651 clp->lc_flags &= ~(LCL_NEEDSCONFIRM | LCL_DONTCLEAN);
653 clp->lc_flags |= LCL_NEEDSCBNULL;
654 /* For NFSv4.1, link the session onto the client. */
656 /* Hold a reference on the xprt for a backchannel. */
657 if ((nsep->sess_crflags & NFSV4CRSESS_CONNBACKCHAN)
659 if (clp->lc_req.nr_client == NULL)
660 clp->lc_req.nr_client = (struct __rpc_client *)
661 clnt_bck_create(nd->nd_xprt->xp_socket,
662 cbprogram, NFSV4_CBVERS);
663 if (clp->lc_req.nr_client != NULL) {
664 SVC_ACQUIRE(nd->nd_xprt);
666 clp->lc_req.nr_client->cl_private;
667 /* Disable idle timeout. */
668 nd->nd_xprt->xp_idletimeout = 0;
669 nsep->sess_cbsess.nfsess_xprt = nd->nd_xprt;
671 nsep->sess_crflags &= ~NFSV4CRSESS_CONNBACKCHAN;
673 NFSBCOPY(sessid, nsep->sess_sessionid,
675 NFSBCOPY(sessid, nsep->sess_cbsess.nfsess_sessionid,
677 shp = NFSSESSIONHASH(nsep->sess_sessionid);
680 LIST_INSERT_HEAD(&shp->list, nsep, sess_hash);
681 LIST_INSERT_HEAD(&clp->lc_session, nsep, sess_list);
682 nsep->sess_clp = clp;
683 NFSUNLOCKSESSION(shp);
687 } else if (clp->lc_flags & LCL_NEEDSCONFIRM) {
688 error = NFSERR_EXPIRED;
692 * If called by the Renew Op, we must check the principal.
694 if (!error && (opflags & CLOPS_RENEWOP)) {
695 if (nfsrv_notsamecredname(nd, clp)) {
697 for (i = 0; i < nfsrv_statehashsize && doneok == 0; i++) {
698 LIST_FOREACH(stp, &clp->lc_stateid[i], ls_hash) {
699 if ((stp->ls_flags & NFSLCK_OPEN) &&
700 stp->ls_uid == nd->nd_cred->cr_uid) {
707 error = NFSERR_ACCES;
709 if (!error && (clp->lc_flags & LCL_CBDOWN))
710 error = NFSERR_CBPATHDOWN;
712 if ((!error || error == NFSERR_CBPATHDOWN) &&
713 (opflags & CLOPS_RENEW)) {
714 clp->lc_expiry = nfsrv_leaseexpiry();
716 if (opflags & CLOPS_CONFIRM) {
717 NFSLOCKV4ROOTMUTEX();
718 nfsv4_unlock(&nfsv4rootfs_lock, 1);
719 NFSUNLOCKV4ROOTMUTEX();
720 } else if (opflags != CLOPS_RENEW) {
727 NFSEXITCODE2(error, nd);
732 * Perform the NFSv4.1 destroy clientid.
735 nfsrv_destroyclient(nfsquad_t clientid, NFSPROC_T *p)
737 struct nfsclient *clp;
738 struct nfsclienthashhead *hp;
739 int error = 0, i, igotlock;
741 if (nfsrvboottime != clientid.lval[0]) {
742 error = NFSERR_STALECLIENTID;
746 /* Lock out other nfsd threads */
747 NFSLOCKV4ROOTMUTEX();
748 nfsv4_relref(&nfsv4rootfs_lock);
750 igotlock = nfsv4_lock(&nfsv4rootfs_lock, 1, NULL,
751 NFSV4ROOTLOCKMUTEXPTR, NULL);
752 } while (igotlock == 0);
753 NFSUNLOCKV4ROOTMUTEX();
755 hp = NFSCLIENTHASH(clientid);
756 LIST_FOREACH(clp, hp, lc_hash) {
757 if (clp->lc_clientid.lval[1] == clientid.lval[1])
761 NFSLOCKV4ROOTMUTEX();
762 nfsv4_unlock(&nfsv4rootfs_lock, 1);
763 NFSUNLOCKV4ROOTMUTEX();
764 /* Just return ok, since it is gone. */
768 /* Scan for state on the clientid. */
769 for (i = 0; i < nfsrv_statehashsize; i++)
770 if (!LIST_EMPTY(&clp->lc_stateid[i])) {
771 NFSLOCKV4ROOTMUTEX();
772 nfsv4_unlock(&nfsv4rootfs_lock, 1);
773 NFSUNLOCKV4ROOTMUTEX();
774 error = NFSERR_CLIENTIDBUSY;
777 if (!LIST_EMPTY(&clp->lc_session) || !LIST_EMPTY(&clp->lc_deleg)) {
778 NFSLOCKV4ROOTMUTEX();
779 nfsv4_unlock(&nfsv4rootfs_lock, 1);
780 NFSUNLOCKV4ROOTMUTEX();
781 error = NFSERR_CLIENTIDBUSY;
785 /* Destroy the clientid and return ok. */
786 nfsrv_cleanclient(clp, p);
787 nfsrv_freedeleglist(&clp->lc_deleg);
788 nfsrv_freedeleglist(&clp->lc_olddeleg);
789 LIST_REMOVE(clp, lc_hash);
790 NFSLOCKV4ROOTMUTEX();
791 nfsv4_unlock(&nfsv4rootfs_lock, 1);
792 NFSUNLOCKV4ROOTMUTEX();
793 nfsrv_zapclient(clp, p);
795 NFSEXITCODE2(error, nd);
800 * Called from the new nfssvc syscall to admin revoke a clientid.
801 * Returns 0 for success, error otherwise.
804 nfsrv_adminrevoke(struct nfsd_clid *revokep, NFSPROC_T *p)
806 struct nfsclient *clp = NULL;
811 * First, lock out the nfsd so that state won't change while the
812 * revocation record is being written to the stable storage restart
815 NFSLOCKV4ROOTMUTEX();
817 igotlock = nfsv4_lock(&nfsv4rootfs_lock, 1, NULL,
818 NFSV4ROOTLOCKMUTEXPTR, NULL);
820 NFSUNLOCKV4ROOTMUTEX();
823 * Search for a match in the client list.
826 while (i < nfsrv_clienthashsize && !gotit) {
827 LIST_FOREACH(clp, &nfsclienthash[i], lc_hash) {
828 if (revokep->nclid_idlen == clp->lc_idlen &&
829 !NFSBCMP(revokep->nclid_id, clp->lc_id, clp->lc_idlen)) {
837 NFSLOCKV4ROOTMUTEX();
838 nfsv4_unlock(&nfsv4rootfs_lock, 0);
839 NFSUNLOCKV4ROOTMUTEX();
845 * Now, write out the revocation record
847 nfsrv_writestable(clp->lc_id, clp->lc_idlen, NFSNST_REVOKE, p);
848 nfsrv_backupstable();
851 * and clear out the state, marking the clientid revoked.
853 clp->lc_flags &= ~LCL_CALLBACKSON;
854 clp->lc_flags |= LCL_ADMINREVOKED;
855 nfsrv_cleanclient(clp, p);
856 nfsrv_freedeleglist(&clp->lc_deleg);
857 nfsrv_freedeleglist(&clp->lc_olddeleg);
858 NFSLOCKV4ROOTMUTEX();
859 nfsv4_unlock(&nfsv4rootfs_lock, 0);
860 NFSUNLOCKV4ROOTMUTEX();
868 * Dump out stats for all clients. Called from nfssvc(2), that is used
872 nfsrv_dumpclients(struct nfsd_dumpclients *dumpp, int maxcnt)
874 struct nfsclient *clp;
878 * First, get a reference on the nfsv4rootfs_lock so that an
879 * exclusive lock cannot be acquired while dumping the clients.
881 NFSLOCKV4ROOTMUTEX();
882 nfsv4_getref(&nfsv4rootfs_lock, NULL, NFSV4ROOTLOCKMUTEXPTR, NULL);
883 NFSUNLOCKV4ROOTMUTEX();
886 * Rattle through the client lists until done.
888 while (i < nfsrv_clienthashsize && cnt < maxcnt) {
889 clp = LIST_FIRST(&nfsclienthash[i]);
890 while (clp != LIST_END(&nfsclienthash[i]) && cnt < maxcnt) {
891 nfsrv_dumpaclient(clp, &dumpp[cnt]);
893 clp = LIST_NEXT(clp, lc_hash);
898 dumpp[cnt].ndcl_clid.nclid_idlen = 0;
900 NFSLOCKV4ROOTMUTEX();
901 nfsv4_relref(&nfsv4rootfs_lock);
902 NFSUNLOCKV4ROOTMUTEX();
906 * Dump stats for a client. Must be called with the NFSSTATELOCK and spl'd.
909 nfsrv_dumpaclient(struct nfsclient *clp, struct nfsd_dumpclients *dumpp)
911 struct nfsstate *stp, *openstp, *lckownstp;
915 struct sockaddr_in *rin;
918 struct sockaddr_in6 *rin6;
921 dumpp->ndcl_nopenowners = dumpp->ndcl_nlockowners = 0;
922 dumpp->ndcl_nopens = dumpp->ndcl_nlocks = 0;
923 dumpp->ndcl_ndelegs = dumpp->ndcl_nolddelegs = 0;
924 dumpp->ndcl_flags = clp->lc_flags;
925 dumpp->ndcl_clid.nclid_idlen = clp->lc_idlen;
926 NFSBCOPY(clp->lc_id, dumpp->ndcl_clid.nclid_id, clp->lc_idlen);
927 af = clp->lc_req.nr_nam->sa_family;
928 dumpp->ndcl_addrfam = af;
932 rin = (struct sockaddr_in *)clp->lc_req.nr_nam;
933 dumpp->ndcl_cbaddr.sin_addr = rin->sin_addr;
938 rin6 = (struct sockaddr_in6 *)clp->lc_req.nr_nam;
939 dumpp->ndcl_cbaddr.sin6_addr = rin6->sin6_addr;
945 * Now, scan the state lists and total up the opens and locks.
947 LIST_FOREACH(stp, &clp->lc_open, ls_list) {
948 dumpp->ndcl_nopenowners++;
949 LIST_FOREACH(openstp, &stp->ls_open, ls_list) {
950 dumpp->ndcl_nopens++;
951 LIST_FOREACH(lckownstp, &openstp->ls_open, ls_list) {
952 dumpp->ndcl_nlockowners++;
953 LIST_FOREACH(lop, &lckownstp->ls_lock, lo_lckowner) {
954 dumpp->ndcl_nlocks++;
961 * and the delegation lists.
963 LIST_FOREACH(stp, &clp->lc_deleg, ls_list) {
964 dumpp->ndcl_ndelegs++;
966 LIST_FOREACH(stp, &clp->lc_olddeleg, ls_list) {
967 dumpp->ndcl_nolddelegs++;
972 * Dump out lock stats for a file.
975 nfsrv_dumplocks(vnode_t vp, struct nfsd_dumplocks *ldumpp, int maxcnt,
978 struct nfsstate *stp;
981 struct nfslockfile *lfp;
984 struct sockaddr_in *rin;
987 struct sockaddr_in6 *rin6;
992 ret = nfsrv_getlockfh(vp, 0, NULL, &nfh, p);
994 * First, get a reference on the nfsv4rootfs_lock so that an
995 * exclusive lock on it cannot be acquired while dumping the locks.
997 NFSLOCKV4ROOTMUTEX();
998 nfsv4_getref(&nfsv4rootfs_lock, NULL, NFSV4ROOTLOCKMUTEXPTR, NULL);
999 NFSUNLOCKV4ROOTMUTEX();
1002 ret = nfsrv_getlockfile(0, NULL, &lfp, &nfh, 0);
1004 ldumpp[0].ndlck_clid.nclid_idlen = 0;
1006 NFSLOCKV4ROOTMUTEX();
1007 nfsv4_relref(&nfsv4rootfs_lock);
1008 NFSUNLOCKV4ROOTMUTEX();
1013 * For each open share on file, dump it out.
1015 stp = LIST_FIRST(&lfp->lf_open);
1016 while (stp != LIST_END(&lfp->lf_open) && cnt < maxcnt) {
1017 ldumpp[cnt].ndlck_flags = stp->ls_flags;
1018 ldumpp[cnt].ndlck_stateid.seqid = stp->ls_stateid.seqid;
1019 ldumpp[cnt].ndlck_stateid.other[0] = stp->ls_stateid.other[0];
1020 ldumpp[cnt].ndlck_stateid.other[1] = stp->ls_stateid.other[1];
1021 ldumpp[cnt].ndlck_stateid.other[2] = stp->ls_stateid.other[2];
1022 ldumpp[cnt].ndlck_owner.nclid_idlen =
1023 stp->ls_openowner->ls_ownerlen;
1024 NFSBCOPY(stp->ls_openowner->ls_owner,
1025 ldumpp[cnt].ndlck_owner.nclid_id,
1026 stp->ls_openowner->ls_ownerlen);
1027 ldumpp[cnt].ndlck_clid.nclid_idlen = stp->ls_clp->lc_idlen;
1028 NFSBCOPY(stp->ls_clp->lc_id, ldumpp[cnt].ndlck_clid.nclid_id,
1029 stp->ls_clp->lc_idlen);
1030 af = stp->ls_clp->lc_req.nr_nam->sa_family;
1031 ldumpp[cnt].ndlck_addrfam = af;
1035 rin = (struct sockaddr_in *)stp->ls_clp->lc_req.nr_nam;
1036 ldumpp[cnt].ndlck_cbaddr.sin_addr = rin->sin_addr;
1041 rin6 = (struct sockaddr_in6 *)
1042 stp->ls_clp->lc_req.nr_nam;
1043 ldumpp[cnt].ndlck_cbaddr.sin6_addr = rin6->sin6_addr;
1047 stp = LIST_NEXT(stp, ls_file);
1054 lop = LIST_FIRST(&lfp->lf_lock);
1055 while (lop != LIST_END(&lfp->lf_lock) && cnt < maxcnt) {
1057 ldumpp[cnt].ndlck_flags = lop->lo_flags;
1058 ldumpp[cnt].ndlck_first = lop->lo_first;
1059 ldumpp[cnt].ndlck_end = lop->lo_end;
1060 ldumpp[cnt].ndlck_stateid.seqid = stp->ls_stateid.seqid;
1061 ldumpp[cnt].ndlck_stateid.other[0] = stp->ls_stateid.other[0];
1062 ldumpp[cnt].ndlck_stateid.other[1] = stp->ls_stateid.other[1];
1063 ldumpp[cnt].ndlck_stateid.other[2] = stp->ls_stateid.other[2];
1064 ldumpp[cnt].ndlck_owner.nclid_idlen = stp->ls_ownerlen;
1065 NFSBCOPY(stp->ls_owner, ldumpp[cnt].ndlck_owner.nclid_id,
1067 ldumpp[cnt].ndlck_clid.nclid_idlen = stp->ls_clp->lc_idlen;
1068 NFSBCOPY(stp->ls_clp->lc_id, ldumpp[cnt].ndlck_clid.nclid_id,
1069 stp->ls_clp->lc_idlen);
1070 af = stp->ls_clp->lc_req.nr_nam->sa_family;
1071 ldumpp[cnt].ndlck_addrfam = af;
1075 rin = (struct sockaddr_in *)stp->ls_clp->lc_req.nr_nam;
1076 ldumpp[cnt].ndlck_cbaddr.sin_addr = rin->sin_addr;
1081 rin6 = (struct sockaddr_in6 *)
1082 stp->ls_clp->lc_req.nr_nam;
1083 ldumpp[cnt].ndlck_cbaddr.sin6_addr = rin6->sin6_addr;
1087 lop = LIST_NEXT(lop, lo_lckfile);
1092 * and the delegations.
1094 stp = LIST_FIRST(&lfp->lf_deleg);
1095 while (stp != LIST_END(&lfp->lf_deleg) && cnt < maxcnt) {
1096 ldumpp[cnt].ndlck_flags = stp->ls_flags;
1097 ldumpp[cnt].ndlck_stateid.seqid = stp->ls_stateid.seqid;
1098 ldumpp[cnt].ndlck_stateid.other[0] = stp->ls_stateid.other[0];
1099 ldumpp[cnt].ndlck_stateid.other[1] = stp->ls_stateid.other[1];
1100 ldumpp[cnt].ndlck_stateid.other[2] = stp->ls_stateid.other[2];
1101 ldumpp[cnt].ndlck_owner.nclid_idlen = 0;
1102 ldumpp[cnt].ndlck_clid.nclid_idlen = stp->ls_clp->lc_idlen;
1103 NFSBCOPY(stp->ls_clp->lc_id, ldumpp[cnt].ndlck_clid.nclid_id,
1104 stp->ls_clp->lc_idlen);
1105 af = stp->ls_clp->lc_req.nr_nam->sa_family;
1106 ldumpp[cnt].ndlck_addrfam = af;
1110 rin = (struct sockaddr_in *)stp->ls_clp->lc_req.nr_nam;
1111 ldumpp[cnt].ndlck_cbaddr.sin_addr = rin->sin_addr;
1116 rin6 = (struct sockaddr_in6 *)
1117 stp->ls_clp->lc_req.nr_nam;
1118 ldumpp[cnt].ndlck_cbaddr.sin6_addr = rin6->sin6_addr;
1122 stp = LIST_NEXT(stp, ls_file);
1127 * If list isn't full, mark end of list by setting the client name
1131 ldumpp[cnt].ndlck_clid.nclid_idlen = 0;
1133 NFSLOCKV4ROOTMUTEX();
1134 nfsv4_relref(&nfsv4rootfs_lock);
1135 NFSUNLOCKV4ROOTMUTEX();
1139 * Server timer routine. It can scan any linked list, so long
1140 * as it holds the spin/mutex lock and there is no exclusive lock on
1142 * (For OpenBSD, a kthread is ok. For FreeBSD, I think it is ok
1143 * to do this from a callout, since the spin locks work. For
1144 * Darwin, I'm not sure what will work correctly yet.)
1145 * Should be called once per second.
1148 nfsrv_servertimer(void)
1150 struct nfsclient *clp, *nclp;
1151 struct nfsstate *stp, *nstp;
1155 * Make sure nfsboottime is set. This is used by V3 as well
1156 * as V4. Note that nfsboottime is not nfsrvboottime, which is
1157 * only used by the V4 server for leases.
1159 if (nfsboottime.tv_sec == 0)
1160 NFSSETBOOTTIME(nfsboottime);
1163 * If server hasn't started yet, just return.
1166 if (nfsrv_stablefirst.nsf_eograce == 0) {
1170 if (!(nfsrv_stablefirst.nsf_flags & NFSNSF_UPDATEDONE)) {
1171 if (!(nfsrv_stablefirst.nsf_flags & NFSNSF_GRACEOVER) &&
1172 NFSD_MONOSEC > nfsrv_stablefirst.nsf_eograce)
1173 nfsrv_stablefirst.nsf_flags |=
1174 (NFSNSF_GRACEOVER | NFSNSF_NEEDLOCK);
1180 * Try and get a reference count on the nfsv4rootfs_lock so that
1181 * no nfsd thread can acquire an exclusive lock on it before this
1182 * call is done. If it is already exclusively locked, just return.
1184 NFSLOCKV4ROOTMUTEX();
1185 got_ref = nfsv4_getref_nonblock(&nfsv4rootfs_lock);
1186 NFSUNLOCKV4ROOTMUTEX();
1193 * For each client...
1195 for (i = 0; i < nfsrv_clienthashsize; i++) {
1196 clp = LIST_FIRST(&nfsclienthash[i]);
1197 while (clp != LIST_END(&nfsclienthash[i])) {
1198 nclp = LIST_NEXT(clp, lc_hash);
1199 if (!(clp->lc_flags & LCL_EXPIREIT)) {
1200 if (((clp->lc_expiry + NFSRV_STALELEASE) < NFSD_MONOSEC
1201 && ((LIST_EMPTY(&clp->lc_deleg)
1202 && LIST_EMPTY(&clp->lc_open)) ||
1203 nfsrv_clients > nfsrv_clienthighwater)) ||
1204 (clp->lc_expiry + NFSRV_MOULDYLEASE) < NFSD_MONOSEC ||
1205 (clp->lc_expiry < NFSD_MONOSEC &&
1206 (nfsrv_openpluslock * 10 / 9) > nfsrv_v4statelimit)) {
1208 * Lease has expired several nfsrv_lease times ago:
1210 * - no state is associated with it
1212 * - above high water mark for number of clients
1213 * (nfsrv_clienthighwater should be large enough
1214 * that this only occurs when clients fail to
1215 * use the same nfs_client_id4.id. Maybe somewhat
1216 * higher that the maximum number of clients that
1217 * will mount this server?)
1219 * Lease has expired a very long time ago
1221 * Lease has expired PLUS the number of opens + locks
1222 * has exceeded 90% of capacity
1224 * --> Mark for expiry. The actual expiry will be done
1225 * by an nfsd sometime soon.
1227 clp->lc_flags |= LCL_EXPIREIT;
1228 nfsrv_stablefirst.nsf_flags |=
1229 (NFSNSF_NEEDLOCK | NFSNSF_EXPIREDCLIENT);
1232 * If there are no opens, increment no open tick cnt
1233 * If time exceeds NFSNOOPEN, mark it to be thrown away
1234 * otherwise, if there is an open, reset no open time
1235 * Hopefully, this will avoid excessive re-creation
1236 * of open owners and subsequent open confirms.
1238 stp = LIST_FIRST(&clp->lc_open);
1239 while (stp != LIST_END(&clp->lc_open)) {
1240 nstp = LIST_NEXT(stp, ls_list);
1241 if (LIST_EMPTY(&stp->ls_open)) {
1243 if (stp->ls_noopens > NFSNOOPEN ||
1244 (nfsrv_openpluslock * 2) >
1246 nfsrv_stablefirst.nsf_flags |=
1249 stp->ls_noopens = 0;
1259 NFSLOCKV4ROOTMUTEX();
1260 nfsv4_relref(&nfsv4rootfs_lock);
1261 NFSUNLOCKV4ROOTMUTEX();
1265 * The following set of functions free up the various data structures.
1268 * Clear out all open/lock state related to this nfsclient.
1269 * Caller must hold an exclusive lock on nfsv4rootfs_lock, so that
1270 * there are no other active nfsd threads.
1273 nfsrv_cleanclient(struct nfsclient *clp, NFSPROC_T *p)
1275 struct nfsstate *stp, *nstp;
1276 struct nfsdsession *sep, *nsep;
1278 LIST_FOREACH_SAFE(stp, &clp->lc_open, ls_list, nstp)
1279 nfsrv_freeopenowner(stp, 1, p);
1280 if ((clp->lc_flags & LCL_ADMINREVOKED) == 0)
1281 LIST_FOREACH_SAFE(sep, &clp->lc_session, sess_list, nsep)
1282 (void)nfsrv_freesession(sep, NULL);
1286 * Free a client that has been cleaned. It should also already have been
1287 * removed from the lists.
1288 * (Just to be safe w.r.t. newnfs_disconnect(), call this function when
1289 * softclock interrupts are enabled.)
1292 nfsrv_zapclient(struct nfsclient *clp, NFSPROC_T *p)
1296 if ((clp->lc_flags & (LCL_GSS | LCL_CALLBACKSON)) ==
1297 (LCL_GSS | LCL_CALLBACKSON) &&
1298 (clp->lc_hand.nfsh_flag & NFSG_COMPLETE) &&
1299 clp->lc_handlelen > 0) {
1300 clp->lc_hand.nfsh_flag &= ~NFSG_COMPLETE;
1301 clp->lc_hand.nfsh_flag |= NFSG_DESTROYED;
1302 (void) nfsrv_docallback(clp, NFSV4PROC_CBNULL,
1303 NULL, 0, NULL, NULL, NULL, p);
1306 newnfs_disconnect(&clp->lc_req);
1307 NFSSOCKADDRFREE(clp->lc_req.nr_nam);
1308 NFSFREEMUTEX(&clp->lc_req.nr_mtx);
1309 free(clp->lc_stateid, M_NFSDCLIENT);
1310 free(clp, M_NFSDCLIENT);
1312 newnfsstats.srvclients--;
1313 nfsrv_openpluslock--;
1319 * Free a list of delegation state structures.
1320 * (This function will also free all nfslockfile structures that no
1321 * longer have associated state.)
1324 nfsrv_freedeleglist(struct nfsstatehead *sthp)
1326 struct nfsstate *stp, *nstp;
1328 LIST_FOREACH_SAFE(stp, sthp, ls_list, nstp) {
1329 nfsrv_freedeleg(stp);
1335 * Free up a delegation.
1338 nfsrv_freedeleg(struct nfsstate *stp)
1340 struct nfslockfile *lfp;
1342 LIST_REMOVE(stp, ls_hash);
1343 LIST_REMOVE(stp, ls_list);
1344 LIST_REMOVE(stp, ls_file);
1345 if ((stp->ls_flags & NFSLCK_DELEGWRITE) != 0)
1346 nfsrv_writedelegcnt--;
1348 if (LIST_EMPTY(&lfp->lf_open) &&
1349 LIST_EMPTY(&lfp->lf_lock) && LIST_EMPTY(&lfp->lf_deleg) &&
1350 LIST_EMPTY(&lfp->lf_locallock) && LIST_EMPTY(&lfp->lf_rollback) &&
1351 lfp->lf_usecount == 0 &&
1352 nfsv4_testlock(&lfp->lf_locallock_lck) == 0)
1353 nfsrv_freenfslockfile(lfp);
1354 FREE((caddr_t)stp, M_NFSDSTATE);
1355 newnfsstats.srvdelegates--;
1356 nfsrv_openpluslock--;
1357 nfsrv_delegatecnt--;
1361 * This function frees an open owner and all associated opens.
1364 nfsrv_freeopenowner(struct nfsstate *stp, int cansleep, NFSPROC_T *p)
1366 struct nfsstate *nstp, *tstp;
1368 LIST_REMOVE(stp, ls_list);
1370 * Now, free all associated opens.
1372 nstp = LIST_FIRST(&stp->ls_open);
1373 while (nstp != LIST_END(&stp->ls_open)) {
1375 nstp = LIST_NEXT(nstp, ls_list);
1376 (void) nfsrv_freeopen(tstp, NULL, cansleep, p);
1379 nfsrvd_derefcache(stp->ls_op);
1380 FREE((caddr_t)stp, M_NFSDSTATE);
1381 newnfsstats.srvopenowners--;
1382 nfsrv_openpluslock--;
1386 * This function frees an open (nfsstate open structure) with all associated
1387 * lock_owners and locks. It also frees the nfslockfile structure iff there
1388 * are no other opens on the file.
1389 * Returns 1 if it free'd the nfslockfile, 0 otherwise.
1392 nfsrv_freeopen(struct nfsstate *stp, vnode_t vp, int cansleep, NFSPROC_T *p)
1394 struct nfsstate *nstp, *tstp;
1395 struct nfslockfile *lfp;
1398 LIST_REMOVE(stp, ls_hash);
1399 LIST_REMOVE(stp, ls_list);
1400 LIST_REMOVE(stp, ls_file);
1404 * Now, free all lockowners associated with this open.
1406 LIST_FOREACH_SAFE(tstp, &stp->ls_open, ls_list, nstp)
1407 nfsrv_freelockowner(tstp, vp, cansleep, p);
1410 * The nfslockfile is freed here if there are no locks
1411 * associated with the open.
1412 * If there are locks associated with the open, the
1413 * nfslockfile structure can be freed via nfsrv_freelockowner().
1414 * Acquire the state mutex to avoid races with calls to
1415 * nfsrv_getlockfile().
1419 if (lfp != NULL && LIST_EMPTY(&lfp->lf_open) &&
1420 LIST_EMPTY(&lfp->lf_deleg) && LIST_EMPTY(&lfp->lf_lock) &&
1421 LIST_EMPTY(&lfp->lf_locallock) && LIST_EMPTY(&lfp->lf_rollback) &&
1422 lfp->lf_usecount == 0 &&
1423 (cansleep != 0 || nfsv4_testlock(&lfp->lf_locallock_lck) == 0)) {
1424 nfsrv_freenfslockfile(lfp);
1430 FREE((caddr_t)stp, M_NFSDSTATE);
1431 newnfsstats.srvopens--;
1432 nfsrv_openpluslock--;
1437 * Frees a lockowner and all associated locks.
1440 nfsrv_freelockowner(struct nfsstate *stp, vnode_t vp, int cansleep,
1444 LIST_REMOVE(stp, ls_hash);
1445 LIST_REMOVE(stp, ls_list);
1446 nfsrv_freeallnfslocks(stp, vp, cansleep, p);
1448 nfsrvd_derefcache(stp->ls_op);
1449 FREE((caddr_t)stp, M_NFSDSTATE);
1450 newnfsstats.srvlockowners--;
1451 nfsrv_openpluslock--;
1455 * Free all the nfs locks on a lockowner.
1458 nfsrv_freeallnfslocks(struct nfsstate *stp, vnode_t vp, int cansleep,
1461 struct nfslock *lop, *nlop;
1462 struct nfsrollback *rlp, *nrlp;
1463 struct nfslockfile *lfp = NULL;
1466 uint64_t first, end;
1469 ASSERT_VOP_UNLOCKED(vp, "nfsrv_freeallnfslocks: vnode locked");
1470 lop = LIST_FIRST(&stp->ls_lock);
1471 while (lop != LIST_END(&stp->ls_lock)) {
1472 nlop = LIST_NEXT(lop, lo_lckowner);
1474 * Since all locks should be for the same file, lfp should
1479 else if (lfp != lop->lo_lfp)
1480 panic("allnfslocks");
1482 * If vp is NULL and cansleep != 0, a vnode must be acquired
1483 * from the file handle. This only occurs when called from
1484 * nfsrv_cleanclient().
1487 if (nfsrv_dolocallocks == 0)
1489 else if (vp == NULL && cansleep != 0) {
1490 tvp = nfsvno_getvp(&lfp->lf_fh);
1491 NFSVOPUNLOCK(tvp, 0);
1500 first = lop->lo_first;
1502 nfsrv_freenfslock(lop);
1503 nfsrv_localunlock(tvp, lfp, first, end, p);
1504 LIST_FOREACH_SAFE(rlp, &lfp->lf_rollback, rlck_list,
1506 free(rlp, M_NFSDROLLBACK);
1507 LIST_INIT(&lfp->lf_rollback);
1509 nfsrv_freenfslock(lop);
1512 if (vp == NULL && tvp != NULL)
1517 * Free an nfslock structure.
1520 nfsrv_freenfslock(struct nfslock *lop)
1523 if (lop->lo_lckfile.le_prev != NULL) {
1524 LIST_REMOVE(lop, lo_lckfile);
1525 newnfsstats.srvlocks--;
1526 nfsrv_openpluslock--;
1528 LIST_REMOVE(lop, lo_lckowner);
1529 FREE((caddr_t)lop, M_NFSDLOCK);
1533 * This function frees an nfslockfile structure.
1536 nfsrv_freenfslockfile(struct nfslockfile *lfp)
1539 LIST_REMOVE(lfp, lf_hash);
1540 FREE((caddr_t)lfp, M_NFSDLOCKFILE);
1544 * This function looks up an nfsstate structure via stateid.
1547 nfsrv_getstate(struct nfsclient *clp, nfsv4stateid_t *stateidp, __unused u_int32_t flags,
1548 struct nfsstate **stpp)
1550 struct nfsstate *stp;
1551 struct nfsstatehead *hp;
1555 hp = NFSSTATEHASH(clp, *stateidp);
1556 LIST_FOREACH(stp, hp, ls_hash) {
1557 if (!NFSBCMP(stp->ls_stateid.other, stateidp->other,
1563 * If no state id in list, return NFSERR_BADSTATEID.
1565 if (stp == LIST_END(hp)) {
1566 error = NFSERR_BADSTATEID;
1577 * This function gets an nfsstate structure via owner string.
1580 nfsrv_getowner(struct nfsstatehead *hp, struct nfsstate *new_stp,
1581 struct nfsstate **stpp)
1583 struct nfsstate *stp;
1586 LIST_FOREACH(stp, hp, ls_list) {
1587 if (new_stp->ls_ownerlen == stp->ls_ownerlen &&
1588 !NFSBCMP(new_stp->ls_owner,stp->ls_owner,stp->ls_ownerlen)) {
1596 * Lock control function called to update lock status.
1597 * Returns 0 upon success, -1 if there is no lock and the flags indicate
1598 * that one isn't to be created and an NFSERR_xxx for other errors.
1599 * The structures new_stp and new_lop are passed in as pointers that should
1600 * be set to NULL if the structure is used and shouldn't be free'd.
1601 * For the NFSLCK_TEST and NFSLCK_CHECK cases, the structures are
1602 * never used and can safely be allocated on the stack. For all other
1603 * cases, *new_stpp and *new_lopp should be malloc'd before the call,
1604 * in case they are used.
1607 nfsrv_lockctrl(vnode_t vp, struct nfsstate **new_stpp,
1608 struct nfslock **new_lopp, struct nfslockconflict *cfp,
1609 nfsquad_t clientid, nfsv4stateid_t *stateidp,
1610 __unused struct nfsexstuff *exp,
1611 struct nfsrv_descript *nd, NFSPROC_T *p)
1613 struct nfslock *lop;
1614 struct nfsstate *new_stp = *new_stpp;
1615 struct nfslock *new_lop = *new_lopp;
1616 struct nfsstate *tstp, *mystp, *nstp;
1618 struct nfslockfile *lfp;
1619 struct nfslock *other_lop = NULL;
1620 struct nfsstate *stp, *lckstp = NULL;
1621 struct nfsclient *clp = NULL;
1623 int error = 0, haslock = 0, ret, reterr;
1624 int getlckret, delegation = 0, filestruct_locked, vnode_unlocked = 0;
1626 uint64_t first, end;
1627 uint32_t lock_flags;
1629 if (new_stp->ls_flags & (NFSLCK_CHECK | NFSLCK_SETATTR)) {
1631 * Note the special cases of "all 1s" or "all 0s" stateids and
1632 * let reads with all 1s go ahead.
1634 if (new_stp->ls_stateid.seqid == 0x0 &&
1635 new_stp->ls_stateid.other[0] == 0x0 &&
1636 new_stp->ls_stateid.other[1] == 0x0 &&
1637 new_stp->ls_stateid.other[2] == 0x0)
1639 else if (new_stp->ls_stateid.seqid == 0xffffffff &&
1640 new_stp->ls_stateid.other[0] == 0xffffffff &&
1641 new_stp->ls_stateid.other[1] == 0xffffffff &&
1642 new_stp->ls_stateid.other[2] == 0xffffffff)
1647 * Check for restart conditions (client and server).
1649 error = nfsrv_checkrestart(clientid, new_stp->ls_flags,
1650 &new_stp->ls_stateid, specialid);
1655 * Check for state resource limit exceeded.
1657 if ((new_stp->ls_flags & NFSLCK_LOCK) &&
1658 nfsrv_openpluslock > nfsrv_v4statelimit) {
1659 error = NFSERR_RESOURCE;
1664 * For the lock case, get another nfslock structure,
1665 * just in case we need it.
1666 * Malloc now, before we start sifting through the linked lists,
1667 * in case we have to wait for memory.
1670 if (new_stp->ls_flags & NFSLCK_LOCK)
1671 MALLOC(other_lop, struct nfslock *, sizeof (struct nfslock),
1672 M_NFSDLOCK, M_WAITOK);
1673 filestruct_locked = 0;
1678 * Get the lockfile structure for CFH now, so we can do a sanity
1679 * check against the stateid, before incrementing the seqid#, since
1680 * we want to return NFSERR_BADSTATEID on failure and the seqid#
1681 * shouldn't be incremented for this case.
1682 * If nfsrv_getlockfile() returns -1, it means "not found", which
1683 * will be handled later.
1684 * If we are doing Lock/LockU and local locking is enabled, sleep
1685 * lock the nfslockfile structure.
1687 getlckret = nfsrv_getlockfh(vp, new_stp->ls_flags, NULL, &nfh, p);
1689 if (getlckret == 0) {
1690 if ((new_stp->ls_flags & (NFSLCK_LOCK | NFSLCK_UNLOCK)) != 0 &&
1691 nfsrv_dolocallocks != 0 && nd->nd_repstat == 0) {
1692 getlckret = nfsrv_getlockfile(new_stp->ls_flags, NULL,
1695 filestruct_locked = 1;
1697 getlckret = nfsrv_getlockfile(new_stp->ls_flags, NULL,
1700 if (getlckret != 0 && getlckret != -1)
1703 if (filestruct_locked != 0) {
1704 LIST_INIT(&lfp->lf_rollback);
1705 if ((new_stp->ls_flags & NFSLCK_LOCK)) {
1707 * For local locking, do the advisory locking now, so
1708 * that any conflict can be detected. A failure later
1709 * can be rolled back locally. If an error is returned,
1710 * struct nfslockfile has been unlocked and any local
1711 * locking rolled back.
1714 if (vnode_unlocked == 0) {
1715 ASSERT_VOP_ELOCKED(vp, "nfsrv_lockctrl1");
1717 NFSVOPUNLOCK(vp, 0);
1719 reterr = nfsrv_locallock(vp, lfp,
1720 (new_lop->lo_flags & (NFSLCK_READ | NFSLCK_WRITE)),
1721 new_lop->lo_first, new_lop->lo_end, cfp, p);
1726 if (specialid == 0) {
1727 if (new_stp->ls_flags & NFSLCK_TEST) {
1729 * RFC 3530 does not list LockT as an op that renews a
1730 * lease, but the concensus seems to be that it is ok
1731 * for a server to do so.
1733 error = nfsrv_getclient(clientid, CLOPS_RENEW, &clp, NULL,
1734 (nfsquad_t)((u_quad_t)0), 0, nd, p);
1737 * Since NFSERR_EXPIRED, NFSERR_ADMINREVOKED are not valid
1738 * error returns for LockT, just go ahead and test for a lock,
1739 * since there are no locks for this client, but other locks
1740 * can conflict. (ie. same client will always be false)
1742 if (error == NFSERR_EXPIRED || error == NFSERR_ADMINREVOKED)
1746 error = nfsrv_getclient(clientid, CLOPS_RENEW, &clp, NULL,
1747 (nfsquad_t)((u_quad_t)0), 0, nd, p);
1750 * Look up the stateid
1752 error = nfsrv_getstate(clp, &new_stp->ls_stateid,
1753 new_stp->ls_flags, &stp);
1755 * do some sanity checks for an unconfirmed open or a
1756 * stateid that refers to the wrong file, for an open stateid
1758 if (error == 0 && (stp->ls_flags & NFSLCK_OPEN) &&
1759 ((stp->ls_openowner->ls_flags & NFSLCK_NEEDSCONFIRM) ||
1760 (getlckret == 0 && stp->ls_lfp != lfp)))
1761 error = NFSERR_BADSTATEID;
1763 (stp->ls_flags & (NFSLCK_DELEGREAD | NFSLCK_DELEGWRITE)) &&
1764 getlckret == 0 && stp->ls_lfp != lfp)
1765 error = NFSERR_BADSTATEID;
1768 * If the lockowner stateid doesn't refer to the same file,
1769 * I believe that is considered ok, since some clients will
1770 * only create a single lockowner and use that for all locks
1772 * For now, log it as a diagnostic, instead of considering it
1775 if (error == 0 && (stp->ls_flags &
1776 (NFSLCK_OPEN | NFSLCK_DELEGREAD | NFSLCK_DELEGWRITE)) == 0 &&
1777 getlckret == 0 && stp->ls_lfp != lfp) {
1779 printf("Got a lock statid for different file open\n");
1782 error = NFSERR_BADSTATEID;
1787 if (new_stp->ls_flags & NFSLCK_OPENTOLOCK) {
1789 * If haslock set, we've already checked the seqid.
1792 if (stp->ls_flags & NFSLCK_OPEN)
1793 error = nfsrv_checkseqid(nd, new_stp->ls_seq,
1794 stp->ls_openowner, new_stp->ls_op);
1796 error = NFSERR_BADSTATEID;
1799 nfsrv_getowner(&stp->ls_open, new_stp, &lckstp);
1802 * I believe this should be an error, but it
1803 * isn't obvious what NFSERR_xxx would be
1804 * appropriate, so I'll use NFSERR_INVAL for now.
1806 error = NFSERR_INVAL;
1809 } else if (new_stp->ls_flags&(NFSLCK_LOCK|NFSLCK_UNLOCK)) {
1811 * If haslock set, ditto above.
1814 if (stp->ls_flags & NFSLCK_OPEN)
1815 error = NFSERR_BADSTATEID;
1817 error = nfsrv_checkseqid(nd, new_stp->ls_seq,
1818 stp, new_stp->ls_op);
1826 * If the seqid part of the stateid isn't the same, return
1827 * NFSERR_OLDSTATEID for cases other than I/O Ops.
1828 * For I/O Ops, only return NFSERR_OLDSTATEID if
1829 * nfsrv_returnoldstateid is set. (The concensus on the email
1830 * list was that most clients would prefer to not receive
1831 * NFSERR_OLDSTATEID for I/O Ops, but the RFC suggests that that
1832 * is what will happen, so I use the nfsrv_returnoldstateid to
1833 * allow for either server configuration.)
1835 if (!error && stp->ls_stateid.seqid!=new_stp->ls_stateid.seqid &&
1836 (((nd->nd_flag & ND_NFSV41) == 0 &&
1837 (!(new_stp->ls_flags & NFSLCK_CHECK) ||
1838 nfsrv_returnoldstateid)) ||
1839 ((nd->nd_flag & ND_NFSV41) != 0 &&
1840 new_stp->ls_stateid.seqid != 0)))
1841 error = NFSERR_OLDSTATEID;
1846 * Now we can check for grace.
1849 error = nfsrv_checkgrace(nd, clp, new_stp->ls_flags);
1850 if ((new_stp->ls_flags & NFSLCK_RECLAIM) && !error &&
1851 nfsrv_checkstable(clp))
1852 error = NFSERR_NOGRACE;
1854 * If we successfully Reclaimed state, note that.
1856 if ((new_stp->ls_flags & NFSLCK_RECLAIM) && !error)
1857 nfsrv_markstable(clp);
1860 * At this point, either error == NFSERR_BADSTATEID or the
1861 * seqid# has been updated, so we can return any error.
1862 * If error == 0, there may be an error in:
1863 * nd_repstat - Set by the calling function.
1864 * reterr - Set above, if getting the nfslockfile structure
1865 * or acquiring the local lock failed.
1866 * (If both of these are set, nd_repstat should probably be
1867 * returned, since that error was detected before this
1870 if (error != 0 || nd->nd_repstat != 0 || reterr != 0) {
1872 if (nd->nd_repstat != 0)
1873 error = nd->nd_repstat;
1877 if (filestruct_locked != 0) {
1878 /* Roll back local locks. */
1880 if (vnode_unlocked == 0) {
1881 ASSERT_VOP_ELOCKED(vp, "nfsrv_lockctrl2");
1883 NFSVOPUNLOCK(vp, 0);
1885 nfsrv_locallock_rollback(vp, lfp, p);
1887 nfsrv_unlocklf(lfp);
1894 * Check the nfsrv_getlockfile return.
1895 * Returned -1 if no structure found.
1897 if (getlckret == -1) {
1898 error = NFSERR_EXPIRED;
1900 * Called from lockt, so no lock is OK.
1902 if (new_stp->ls_flags & NFSLCK_TEST) {
1904 } else if (new_stp->ls_flags &
1905 (NFSLCK_CHECK | NFSLCK_SETATTR)) {
1907 * Called to check for a lock, OK if the stateid is all
1908 * 1s or all 0s, but there should be an nfsstate
1910 * (ie. If there is no open, I'll assume no share
1916 error = NFSERR_BADSTATEID;
1923 * For NFSLCK_CHECK and NFSLCK_LOCK, test for a share conflict.
1924 * For NFSLCK_CHECK, allow a read if write access is granted,
1925 * but check for a deny. For NFSLCK_LOCK, require correct access,
1926 * which implies a conflicting deny can't exist.
1928 if (new_stp->ls_flags & (NFSLCK_CHECK | NFSLCK_LOCK)) {
1930 * Four kinds of state id:
1931 * - specialid (all 0s or all 1s), only for NFSLCK_CHECK
1932 * - stateid for an open
1933 * - stateid for a delegation
1934 * - stateid for a lock owner
1937 if (stp->ls_flags & (NFSLCK_DELEGREAD | NFSLCK_DELEGWRITE)) {
1940 nfsrv_delaydelegtimeout(stp);
1941 } else if (stp->ls_flags & NFSLCK_OPEN) {
1944 mystp = stp->ls_openstp;
1947 * If locking or checking, require correct access
1950 if (((new_stp->ls_flags & NFSLCK_LOCK) &&
1951 !((new_lop->lo_flags >> NFSLCK_LOCKSHIFT) &
1952 mystp->ls_flags & NFSLCK_ACCESSBITS)) ||
1953 ((new_stp->ls_flags & (NFSLCK_CHECK|NFSLCK_READACCESS)) ==
1954 (NFSLCK_CHECK | NFSLCK_READACCESS) &&
1955 !(mystp->ls_flags & NFSLCK_READACCESS) &&
1956 nfsrv_allowreadforwriteopen == 0) ||
1957 ((new_stp->ls_flags & (NFSLCK_CHECK|NFSLCK_WRITEACCESS)) ==
1958 (NFSLCK_CHECK | NFSLCK_WRITEACCESS) &&
1959 !(mystp->ls_flags & NFSLCK_WRITEACCESS))) {
1960 if (filestruct_locked != 0) {
1961 /* Roll back local locks. */
1963 if (vnode_unlocked == 0) {
1964 ASSERT_VOP_ELOCKED(vp,
1967 NFSVOPUNLOCK(vp, 0);
1969 nfsrv_locallock_rollback(vp, lfp, p);
1971 nfsrv_unlocklf(lfp);
1974 error = NFSERR_OPENMODE;
1979 if ((new_stp->ls_flags & NFSLCK_CHECK) && !delegation) {
1981 * Check for a conflicting deny bit.
1983 LIST_FOREACH(tstp, &lfp->lf_open, ls_file) {
1984 if (tstp != mystp) {
1985 bits = tstp->ls_flags;
1986 bits >>= NFSLCK_SHIFT;
1987 if (new_stp->ls_flags & bits & NFSLCK_ACCESSBITS) {
1988 KASSERT(vnode_unlocked == 0,
1989 ("nfsrv_lockctrl: vnode unlocked1"));
1990 ret = nfsrv_clientconflict(tstp->ls_clp, &haslock,
1994 * nfsrv_clientconflict unlocks state
1995 * when it returns non-zero.
2003 error = NFSERR_PERM;
2005 error = NFSERR_OPENMODE;
2011 /* We're outta here */
2018 * For setattr, just get rid of all the Delegations for other clients.
2020 if (new_stp->ls_flags & NFSLCK_SETATTR) {
2021 KASSERT(vnode_unlocked == 0,
2022 ("nfsrv_lockctrl: vnode unlocked2"));
2023 ret = nfsrv_cleandeleg(vp, lfp, clp, &haslock, p);
2026 * nfsrv_cleandeleg() unlocks state when it
2036 if (!(new_stp->ls_flags & NFSLCK_CHECK) ||
2037 (LIST_EMPTY(&lfp->lf_open) && LIST_EMPTY(&lfp->lf_lock) &&
2038 LIST_EMPTY(&lfp->lf_deleg))) {
2045 * Check for a conflicting delegation. If one is found, call
2046 * nfsrv_delegconflict() to handle it. If the v4root lock hasn't
2047 * been set yet, it will get the lock. Otherwise, it will recall
2048 * the delegation. Then, we try try again...
2049 * I currently believe the conflict algorithm to be:
2050 * For Lock Ops (Lock/LockT/LockU)
2051 * - there is a conflict iff a different client has a write delegation
2052 * For Reading (Read Op)
2053 * - there is a conflict iff a different client has a write delegation
2054 * (the specialids are always a different client)
2055 * For Writing (Write/Setattr of size)
2056 * - there is a conflict if a different client has any delegation
2057 * - there is a conflict if the same client has a read delegation
2058 * (I don't understand why this isn't allowed, but that seems to be
2059 * the current concensus?)
2061 tstp = LIST_FIRST(&lfp->lf_deleg);
2062 while (tstp != LIST_END(&lfp->lf_deleg)) {
2063 nstp = LIST_NEXT(tstp, ls_file);
2064 if ((((new_stp->ls_flags&(NFSLCK_LOCK|NFSLCK_UNLOCK|NFSLCK_TEST))||
2065 ((new_stp->ls_flags & NFSLCK_CHECK) &&
2066 (new_lop->lo_flags & NFSLCK_READ))) &&
2067 clp != tstp->ls_clp &&
2068 (tstp->ls_flags & NFSLCK_DELEGWRITE)) ||
2069 ((new_stp->ls_flags & NFSLCK_CHECK) &&
2070 (new_lop->lo_flags & NFSLCK_WRITE) &&
2071 (clp != tstp->ls_clp ||
2072 (tstp->ls_flags & NFSLCK_DELEGREAD)))) {
2074 if (filestruct_locked != 0) {
2075 /* Roll back local locks. */
2077 if (vnode_unlocked == 0) {
2078 ASSERT_VOP_ELOCKED(vp, "nfsrv_lockctrl4");
2079 NFSVOPUNLOCK(vp, 0);
2081 nfsrv_locallock_rollback(vp, lfp, p);
2083 nfsrv_unlocklf(lfp);
2085 NFSVOPLOCK(vp, LK_EXCLUSIVE | LK_RETRY);
2087 if ((vp->v_iflag & VI_DOOMED) != 0)
2088 ret = NFSERR_SERVERFAULT;
2092 ret = nfsrv_delegconflict(tstp, &haslock, p, vp);
2095 * nfsrv_delegconflict unlocks state when it
2096 * returns non-zero, which it always does.
2099 FREE((caddr_t)other_lop, M_NFSDLOCK);
2109 /* Never gets here. */
2115 * Handle the unlock case by calling nfsrv_updatelock().
2116 * (Should I have done some access checking above for unlock? For now,
2117 * just let it happen.)
2119 if (new_stp->ls_flags & NFSLCK_UNLOCK) {
2120 first = new_lop->lo_first;
2121 end = new_lop->lo_end;
2122 nfsrv_updatelock(stp, new_lopp, &other_lop, lfp);
2123 stateidp->seqid = ++(stp->ls_stateid.seqid);
2124 if ((nd->nd_flag & ND_NFSV41) != 0 && stateidp->seqid == 0)
2125 stateidp->seqid = stp->ls_stateid.seqid = 1;
2126 stateidp->other[0] = stp->ls_stateid.other[0];
2127 stateidp->other[1] = stp->ls_stateid.other[1];
2128 stateidp->other[2] = stp->ls_stateid.other[2];
2129 if (filestruct_locked != 0) {
2131 if (vnode_unlocked == 0) {
2132 ASSERT_VOP_ELOCKED(vp, "nfsrv_lockctrl5");
2134 NFSVOPUNLOCK(vp, 0);
2136 /* Update the local locks. */
2137 nfsrv_localunlock(vp, lfp, first, end, p);
2139 nfsrv_unlocklf(lfp);
2146 * Search for a conflicting lock. A lock conflicts if:
2147 * - the lock range overlaps and
2148 * - at least one lock is a write lock and
2149 * - it is not owned by the same lock owner
2152 LIST_FOREACH(lop, &lfp->lf_lock, lo_lckfile) {
2153 if (new_lop->lo_end > lop->lo_first &&
2154 new_lop->lo_first < lop->lo_end &&
2155 (new_lop->lo_flags == NFSLCK_WRITE ||
2156 lop->lo_flags == NFSLCK_WRITE) &&
2157 lckstp != lop->lo_stp &&
2158 (clp != lop->lo_stp->ls_clp ||
2159 lckstp->ls_ownerlen != lop->lo_stp->ls_ownerlen ||
2160 NFSBCMP(lckstp->ls_owner, lop->lo_stp->ls_owner,
2161 lckstp->ls_ownerlen))) {
2163 FREE((caddr_t)other_lop, M_NFSDLOCK);
2166 if (vnode_unlocked != 0)
2167 ret = nfsrv_clientconflict(lop->lo_stp->ls_clp, &haslock,
2170 ret = nfsrv_clientconflict(lop->lo_stp->ls_clp, &haslock,
2173 if (filestruct_locked != 0) {
2174 if (vnode_unlocked == 0) {
2175 ASSERT_VOP_ELOCKED(vp, "nfsrv_lockctrl6");
2176 NFSVOPUNLOCK(vp, 0);
2178 /* Roll back local locks. */
2179 nfsrv_locallock_rollback(vp, lfp, p);
2181 nfsrv_unlocklf(lfp);
2183 NFSVOPLOCK(vp, LK_EXCLUSIVE | LK_RETRY);
2185 if ((vp->v_iflag & VI_DOOMED) != 0) {
2186 error = NFSERR_SERVERFAULT;
2191 * nfsrv_clientconflict() unlocks state when it
2198 * Found a conflicting lock, so record the conflict and
2201 if (cfp != NULL && ret == 0) {
2202 cfp->cl_clientid.lval[0]=lop->lo_stp->ls_stateid.other[0];
2203 cfp->cl_clientid.lval[1]=lop->lo_stp->ls_stateid.other[1];
2204 cfp->cl_first = lop->lo_first;
2205 cfp->cl_end = lop->lo_end;
2206 cfp->cl_flags = lop->lo_flags;
2207 cfp->cl_ownerlen = lop->lo_stp->ls_ownerlen;
2208 NFSBCOPY(lop->lo_stp->ls_owner, cfp->cl_owner,
2212 error = NFSERR_PERM;
2213 else if (new_stp->ls_flags & NFSLCK_RECLAIM)
2214 error = NFSERR_RECLAIMCONFLICT;
2215 else if (new_stp->ls_flags & NFSLCK_CHECK)
2216 error = NFSERR_LOCKED;
2218 error = NFSERR_DENIED;
2219 if (filestruct_locked != 0 && ret == 0) {
2220 /* Roll back local locks. */
2222 if (vnode_unlocked == 0) {
2223 ASSERT_VOP_ELOCKED(vp, "nfsrv_lockctrl7");
2225 NFSVOPUNLOCK(vp, 0);
2227 nfsrv_locallock_rollback(vp, lfp, p);
2229 nfsrv_unlocklf(lfp);
2239 * We only get here if there was no lock that conflicted.
2241 if (new_stp->ls_flags & (NFSLCK_TEST | NFSLCK_CHECK)) {
2247 * We only get here when we are creating or modifying a lock.
2248 * There are two variants:
2249 * - exist_lock_owner where lock_owner exists
2250 * - open_to_lock_owner with new lock_owner
2252 first = new_lop->lo_first;
2253 end = new_lop->lo_end;
2254 lock_flags = new_lop->lo_flags;
2255 if (!(new_stp->ls_flags & NFSLCK_OPENTOLOCK)) {
2256 nfsrv_updatelock(lckstp, new_lopp, &other_lop, lfp);
2257 stateidp->seqid = ++(lckstp->ls_stateid.seqid);
2258 if ((nd->nd_flag & ND_NFSV41) != 0 && stateidp->seqid == 0)
2259 stateidp->seqid = lckstp->ls_stateid.seqid = 1;
2260 stateidp->other[0] = lckstp->ls_stateid.other[0];
2261 stateidp->other[1] = lckstp->ls_stateid.other[1];
2262 stateidp->other[2] = lckstp->ls_stateid.other[2];
2265 * The new open_to_lock_owner case.
2266 * Link the new nfsstate into the lists.
2268 new_stp->ls_seq = new_stp->ls_opentolockseq;
2269 nfsrvd_refcache(new_stp->ls_op);
2270 stateidp->seqid = new_stp->ls_stateid.seqid = 1;
2271 stateidp->other[0] = new_stp->ls_stateid.other[0] =
2272 clp->lc_clientid.lval[0];
2273 stateidp->other[1] = new_stp->ls_stateid.other[1] =
2274 clp->lc_clientid.lval[1];
2275 stateidp->other[2] = new_stp->ls_stateid.other[2] =
2276 nfsrv_nextstateindex(clp);
2277 new_stp->ls_clp = clp;
2278 LIST_INIT(&new_stp->ls_lock);
2279 new_stp->ls_openstp = stp;
2280 new_stp->ls_lfp = lfp;
2281 nfsrv_insertlock(new_lop, (struct nfslock *)new_stp, new_stp,
2283 LIST_INSERT_HEAD(NFSSTATEHASH(clp, new_stp->ls_stateid),
2285 LIST_INSERT_HEAD(&stp->ls_open, new_stp, ls_list);
2288 newnfsstats.srvlockowners++;
2289 nfsrv_openpluslock++;
2291 if (filestruct_locked != 0) {
2293 nfsrv_locallock_commit(lfp, lock_flags, first, end);
2295 nfsrv_unlocklf(lfp);
2301 NFSLOCKV4ROOTMUTEX();
2302 nfsv4_unlock(&nfsv4rootfs_lock, 1);
2303 NFSUNLOCKV4ROOTMUTEX();
2305 if (vnode_unlocked != 0) {
2306 NFSVOPLOCK(vp, LK_EXCLUSIVE | LK_RETRY);
2307 if (error == 0 && (vp->v_iflag & VI_DOOMED) != 0)
2308 error = NFSERR_SERVERFAULT;
2311 FREE((caddr_t)other_lop, M_NFSDLOCK);
2312 NFSEXITCODE2(error, nd);
2317 * Check for state errors for Open.
2318 * repstat is passed back out as an error if more critical errors
2322 nfsrv_opencheck(nfsquad_t clientid, nfsv4stateid_t *stateidp,
2323 struct nfsstate *new_stp, vnode_t vp, struct nfsrv_descript *nd,
2324 NFSPROC_T *p, int repstat)
2326 struct nfsstate *stp, *nstp;
2327 struct nfsclient *clp;
2328 struct nfsstate *ownerstp;
2329 struct nfslockfile *lfp, *new_lfp;
2330 int error = 0, haslock = 0, ret, readonly = 0, getfhret = 0;
2332 if ((new_stp->ls_flags & NFSLCK_SHAREBITS) == NFSLCK_READACCESS)
2335 * Check for restart conditions (client and server).
2337 error = nfsrv_checkrestart(clientid, new_stp->ls_flags,
2338 &new_stp->ls_stateid, 0);
2343 * Check for state resource limit exceeded.
2344 * Technically this should be SMP protected, but the worst
2345 * case error is "out by one or two" on the count when it
2346 * returns NFSERR_RESOURCE and the limit is just a rather
2347 * arbitrary high water mark, so no harm is done.
2349 if (nfsrv_openpluslock > nfsrv_v4statelimit) {
2350 error = NFSERR_RESOURCE;
2355 MALLOC(new_lfp, struct nfslockfile *, sizeof (struct nfslockfile),
2356 M_NFSDLOCKFILE, M_WAITOK);
2358 getfhret = nfsrv_getlockfh(vp, new_stp->ls_flags, new_lfp,
2362 * Get the nfsclient structure.
2364 error = nfsrv_getclient(clientid, CLOPS_RENEW, &clp, NULL,
2365 (nfsquad_t)((u_quad_t)0), 0, nd, p);
2368 * Look up the open owner. See if it needs confirmation and
2369 * check the seq#, as required.
2372 nfsrv_getowner(&clp->lc_open, new_stp, &ownerstp);
2374 if (!error && ownerstp) {
2375 error = nfsrv_checkseqid(nd, new_stp->ls_seq, ownerstp,
2378 * If the OpenOwner hasn't been confirmed, assume the
2379 * old one was a replay and this one is ok.
2380 * See: RFC3530 Sec. 14.2.18.
2382 if (error == NFSERR_BADSEQID &&
2383 (ownerstp->ls_flags & NFSLCK_NEEDSCONFIRM))
2391 error = nfsrv_checkgrace(nd, clp, new_stp->ls_flags);
2392 if ((new_stp->ls_flags & NFSLCK_RECLAIM) && !error &&
2393 nfsrv_checkstable(clp))
2394 error = NFSERR_NOGRACE;
2397 * If none of the above errors occurred, let repstat be
2400 if (repstat && !error)
2405 NFSLOCKV4ROOTMUTEX();
2406 nfsv4_unlock(&nfsv4rootfs_lock, 1);
2407 NFSUNLOCKV4ROOTMUTEX();
2409 free((caddr_t)new_lfp, M_NFSDLOCKFILE);
2414 * If vp == NULL, the file doesn't exist yet, so return ok.
2415 * (This always happens on the first pass, so haslock must be 0.)
2419 FREE((caddr_t)new_lfp, M_NFSDLOCKFILE);
2424 * Get the structure for the underlying file.
2429 error = nfsrv_getlockfile(new_stp->ls_flags, &new_lfp, &lfp,
2432 FREE((caddr_t)new_lfp, M_NFSDLOCKFILE);
2436 NFSLOCKV4ROOTMUTEX();
2437 nfsv4_unlock(&nfsv4rootfs_lock, 1);
2438 NFSUNLOCKV4ROOTMUTEX();
2444 * Search for a conflicting open/share.
2446 if (new_stp->ls_flags & NFSLCK_DELEGCUR) {
2448 * For Delegate_Cur, search for the matching Delegation,
2449 * which indicates no conflict.
2450 * An old delegation should have been recovered by the
2451 * client doing a Claim_DELEGATE_Prev, so I won't let
2452 * it match and return NFSERR_EXPIRED. Should I let it
2455 LIST_FOREACH(stp, &lfp->lf_deleg, ls_file) {
2456 if (!(stp->ls_flags & NFSLCK_OLDDELEG) &&
2457 (((nd->nd_flag & ND_NFSV41) != 0 &&
2458 stateidp->seqid == 0) ||
2459 stateidp->seqid == stp->ls_stateid.seqid) &&
2460 !NFSBCMP(stateidp->other, stp->ls_stateid.other,
2464 if (stp == LIST_END(&lfp->lf_deleg) ||
2465 ((new_stp->ls_flags & NFSLCK_WRITEACCESS) &&
2466 (stp->ls_flags & NFSLCK_DELEGREAD))) {
2469 NFSLOCKV4ROOTMUTEX();
2470 nfsv4_unlock(&nfsv4rootfs_lock, 1);
2471 NFSUNLOCKV4ROOTMUTEX();
2473 error = NFSERR_EXPIRED;
2479 * Check for access/deny bit conflicts. I check for the same
2480 * owner as well, in case the client didn't bother.
2482 LIST_FOREACH(stp, &lfp->lf_open, ls_file) {
2483 if (!(new_stp->ls_flags & NFSLCK_DELEGCUR) &&
2484 (((new_stp->ls_flags & NFSLCK_ACCESSBITS) &
2485 ((stp->ls_flags>>NFSLCK_SHIFT) & NFSLCK_ACCESSBITS))||
2486 ((stp->ls_flags & NFSLCK_ACCESSBITS) &
2487 ((new_stp->ls_flags>>NFSLCK_SHIFT)&NFSLCK_ACCESSBITS)))){
2488 ret = nfsrv_clientconflict(stp->ls_clp,&haslock,vp,p);
2491 * nfsrv_clientconflict() unlocks
2492 * state when it returns non-zero.
2497 error = NFSERR_PERM;
2498 else if (new_stp->ls_flags & NFSLCK_RECLAIM)
2499 error = NFSERR_RECLAIMCONFLICT;
2501 error = NFSERR_SHAREDENIED;
2505 NFSLOCKV4ROOTMUTEX();
2506 nfsv4_unlock(&nfsv4rootfs_lock, 1);
2507 NFSUNLOCKV4ROOTMUTEX();
2514 * Check for a conflicting delegation. If one is found, call
2515 * nfsrv_delegconflict() to handle it. If the v4root lock hasn't
2516 * been set yet, it will get the lock. Otherwise, it will recall
2517 * the delegation. Then, we try try again...
2518 * (If NFSLCK_DELEGCUR is set, it has a delegation, so there
2519 * isn't a conflict.)
2520 * I currently believe the conflict algorithm to be:
2521 * For Open with Read Access and Deny None
2522 * - there is a conflict iff a different client has a write delegation
2523 * For Open with other Write Access or any Deny except None
2524 * - there is a conflict if a different client has any delegation
2525 * - there is a conflict if the same client has a read delegation
2526 * (The current concensus is that this last case should be
2527 * considered a conflict since the client with a read delegation
2528 * could have done an Open with ReadAccess and WriteDeny
2529 * locally and then not have checked for the WriteDeny.)
2530 * Don't check for a Reclaim, since that will be dealt with
2531 * by nfsrv_openctrl().
2533 if (!(new_stp->ls_flags &
2534 (NFSLCK_DELEGPREV | NFSLCK_DELEGCUR | NFSLCK_RECLAIM))) {
2535 stp = LIST_FIRST(&lfp->lf_deleg);
2536 while (stp != LIST_END(&lfp->lf_deleg)) {
2537 nstp = LIST_NEXT(stp, ls_file);
2538 if ((readonly && stp->ls_clp != clp &&
2539 (stp->ls_flags & NFSLCK_DELEGWRITE)) ||
2540 (!readonly && (stp->ls_clp != clp ||
2541 (stp->ls_flags & NFSLCK_DELEGREAD)))) {
2542 ret = nfsrv_delegconflict(stp, &haslock, p, vp);
2545 * nfsrv_delegconflict() unlocks state
2546 * when it returns non-zero.
2559 NFSLOCKV4ROOTMUTEX();
2560 nfsv4_unlock(&nfsv4rootfs_lock, 1);
2561 NFSUNLOCKV4ROOTMUTEX();
2565 NFSEXITCODE2(error, nd);
2570 * Open control function to create/update open state for an open.
2573 nfsrv_openctrl(struct nfsrv_descript *nd, vnode_t vp,
2574 struct nfsstate **new_stpp, nfsquad_t clientid, nfsv4stateid_t *stateidp,
2575 nfsv4stateid_t *delegstateidp, u_int32_t *rflagsp, struct nfsexstuff *exp,
2576 NFSPROC_T *p, u_quad_t filerev)
2578 struct nfsstate *new_stp = *new_stpp;
2579 struct nfsstate *stp, *nstp;
2580 struct nfsstate *openstp = NULL, *new_open, *ownerstp, *new_deleg;
2581 struct nfslockfile *lfp, *new_lfp;
2582 struct nfsclient *clp;
2583 int error = 0, haslock = 0, ret, delegate = 1, writedeleg = 1;
2584 int readonly = 0, cbret = 1, getfhret = 0;
2585 int gotstate = 0, len = 0;
2586 u_char *clidp = NULL;
2588 if ((new_stp->ls_flags & NFSLCK_SHAREBITS) == NFSLCK_READACCESS)
2591 * Check for restart conditions (client and server).
2592 * (Paranoia, should have been detected by nfsrv_opencheck().)
2593 * If an error does show up, return NFSERR_EXPIRED, since the
2594 * the seqid# has already been incremented.
2596 error = nfsrv_checkrestart(clientid, new_stp->ls_flags,
2597 &new_stp->ls_stateid, 0);
2599 printf("Nfsd: openctrl unexpected restart err=%d\n",
2601 error = NFSERR_EXPIRED;
2605 clidp = malloc(NFSV4_OPAQUELIMIT, M_TEMP, M_WAITOK);
2607 MALLOC(new_lfp, struct nfslockfile *, sizeof (struct nfslockfile),
2608 M_NFSDLOCKFILE, M_WAITOK);
2609 MALLOC(new_open, struct nfsstate *, sizeof (struct nfsstate),
2610 M_NFSDSTATE, M_WAITOK);
2611 MALLOC(new_deleg, struct nfsstate *, sizeof (struct nfsstate),
2612 M_NFSDSTATE, M_WAITOK);
2613 getfhret = nfsrv_getlockfh(vp, new_stp->ls_flags, new_lfp,
2617 * Get the client structure. Since the linked lists could be changed
2618 * by other nfsd processes if this process does a tsleep(), one of
2619 * two things must be done.
2620 * 1 - don't tsleep()
2622 * 2 - get the nfsv4_lock() { indicated by haslock == 1 }
2623 * before using the lists, since this lock stops the other
2624 * nfsd. This should only be used for rare cases, since it
2625 * essentially single threads the nfsd.
2626 * At this time, it is only done for cases where the stable
2627 * storage file must be written prior to completion of state
2630 error = nfsrv_getclient(clientid, CLOPS_RENEW, &clp, NULL,
2631 (nfsquad_t)((u_quad_t)0), 0, nd, p);
2632 if (!error && (clp->lc_flags & LCL_NEEDSCBNULL) &&
2635 * This happens on the first open for a client
2636 * that supports callbacks.
2640 * Although nfsrv_docallback() will sleep, clp won't
2641 * go away, since they are only removed when the
2642 * nfsv4_lock() has blocked the nfsd threads. The
2643 * fields in clp can change, but having multiple
2644 * threads do this Null callback RPC should be
2647 cbret = nfsrv_docallback(clp, NFSV4PROC_CBNULL,
2648 NULL, 0, NULL, NULL, NULL, p);
2650 clp->lc_flags &= ~LCL_NEEDSCBNULL;
2652 clp->lc_flags |= LCL_CALLBACKSON;
2656 * Look up the open owner. See if it needs confirmation and
2657 * check the seq#, as required.
2660 nfsrv_getowner(&clp->lc_open, new_stp, &ownerstp);
2664 printf("Nfsd: openctrl unexpected state err=%d\n",
2666 free((caddr_t)new_lfp, M_NFSDLOCKFILE);
2667 free((caddr_t)new_open, M_NFSDSTATE);
2668 free((caddr_t)new_deleg, M_NFSDSTATE);
2670 NFSLOCKV4ROOTMUTEX();
2671 nfsv4_unlock(&nfsv4rootfs_lock, 1);
2672 NFSUNLOCKV4ROOTMUTEX();
2674 error = NFSERR_EXPIRED;
2678 if (new_stp->ls_flags & NFSLCK_RECLAIM)
2679 nfsrv_markstable(clp);
2682 * Get the structure for the underlying file.
2687 error = nfsrv_getlockfile(new_stp->ls_flags, &new_lfp, &lfp,
2690 FREE((caddr_t)new_lfp, M_NFSDLOCKFILE);
2693 printf("Nfsd openctrl unexpected getlockfile err=%d\n",
2695 free((caddr_t)new_open, M_NFSDSTATE);
2696 free((caddr_t)new_deleg, M_NFSDSTATE);
2698 NFSLOCKV4ROOTMUTEX();
2699 nfsv4_unlock(&nfsv4rootfs_lock, 1);
2700 NFSUNLOCKV4ROOTMUTEX();
2706 * Search for a conflicting open/share.
2708 if (new_stp->ls_flags & NFSLCK_DELEGCUR) {
2710 * For Delegate_Cur, search for the matching Delegation,
2711 * which indicates no conflict.
2712 * An old delegation should have been recovered by the
2713 * client doing a Claim_DELEGATE_Prev, so I won't let
2714 * it match and return NFSERR_EXPIRED. Should I let it
2717 LIST_FOREACH(stp, &lfp->lf_deleg, ls_file) {
2718 if (!(stp->ls_flags & NFSLCK_OLDDELEG) &&
2719 (((nd->nd_flag & ND_NFSV41) != 0 &&
2720 stateidp->seqid == 0) ||
2721 stateidp->seqid == stp->ls_stateid.seqid) &&
2722 !NFSBCMP(stateidp->other, stp->ls_stateid.other,
2726 if (stp == LIST_END(&lfp->lf_deleg) ||
2727 ((new_stp->ls_flags & NFSLCK_WRITEACCESS) &&
2728 (stp->ls_flags & NFSLCK_DELEGREAD))) {
2730 printf("Nfsd openctrl unexpected expiry\n");
2731 free((caddr_t)new_open, M_NFSDSTATE);
2732 free((caddr_t)new_deleg, M_NFSDSTATE);
2734 NFSLOCKV4ROOTMUTEX();
2735 nfsv4_unlock(&nfsv4rootfs_lock, 1);
2736 NFSUNLOCKV4ROOTMUTEX();
2738 error = NFSERR_EXPIRED;
2743 * Don't issue a Delegation, since one already exists and
2744 * delay delegation timeout, as required.
2747 nfsrv_delaydelegtimeout(stp);
2751 * Check for access/deny bit conflicts. I also check for the
2752 * same owner, since the client might not have bothered to check.
2753 * Also, note an open for the same file and owner, if found,
2754 * which is all we do here for Delegate_Cur, since conflict
2755 * checking is already done.
2757 LIST_FOREACH(stp, &lfp->lf_open, ls_file) {
2758 if (ownerstp && stp->ls_openowner == ownerstp)
2760 if (!(new_stp->ls_flags & NFSLCK_DELEGCUR)) {
2762 * If another client has the file open, the only
2763 * delegation that can be issued is a Read delegation
2764 * and only if it is a Read open with Deny none.
2766 if (clp != stp->ls_clp) {
2767 if ((stp->ls_flags & NFSLCK_SHAREBITS) ==
2773 if(((new_stp->ls_flags & NFSLCK_ACCESSBITS) &
2774 ((stp->ls_flags>>NFSLCK_SHIFT) & NFSLCK_ACCESSBITS))||
2775 ((stp->ls_flags & NFSLCK_ACCESSBITS) &
2776 ((new_stp->ls_flags>>NFSLCK_SHIFT)&NFSLCK_ACCESSBITS))){
2777 ret = nfsrv_clientconflict(stp->ls_clp,&haslock,vp,p);
2780 * nfsrv_clientconflict() unlocks state
2781 * when it returns non-zero.
2783 free((caddr_t)new_open, M_NFSDSTATE);
2784 free((caddr_t)new_deleg, M_NFSDSTATE);
2789 error = NFSERR_PERM;
2790 else if (new_stp->ls_flags & NFSLCK_RECLAIM)
2791 error = NFSERR_RECLAIMCONFLICT;
2793 error = NFSERR_SHAREDENIED;
2797 NFSLOCKV4ROOTMUTEX();
2798 nfsv4_unlock(&nfsv4rootfs_lock, 1);
2799 NFSUNLOCKV4ROOTMUTEX();
2801 free((caddr_t)new_open, M_NFSDSTATE);
2802 free((caddr_t)new_deleg, M_NFSDSTATE);
2803 printf("nfsd openctrl unexpected client cnfl\n");
2810 * Check for a conflicting delegation. If one is found, call
2811 * nfsrv_delegconflict() to handle it. If the v4root lock hasn't
2812 * been set yet, it will get the lock. Otherwise, it will recall
2813 * the delegation. Then, we try try again...
2814 * (If NFSLCK_DELEGCUR is set, it has a delegation, so there
2815 * isn't a conflict.)
2816 * I currently believe the conflict algorithm to be:
2817 * For Open with Read Access and Deny None
2818 * - there is a conflict iff a different client has a write delegation
2819 * For Open with other Write Access or any Deny except None
2820 * - there is a conflict if a different client has any delegation
2821 * - there is a conflict if the same client has a read delegation
2822 * (The current concensus is that this last case should be
2823 * considered a conflict since the client with a read delegation
2824 * could have done an Open with ReadAccess and WriteDeny
2825 * locally and then not have checked for the WriteDeny.)
2827 if (!(new_stp->ls_flags & (NFSLCK_DELEGPREV | NFSLCK_DELEGCUR))) {
2828 stp = LIST_FIRST(&lfp->lf_deleg);
2829 while (stp != LIST_END(&lfp->lf_deleg)) {
2830 nstp = LIST_NEXT(stp, ls_file);
2831 if (stp->ls_clp != clp && (stp->ls_flags & NFSLCK_DELEGREAD))
2835 if ((readonly && stp->ls_clp != clp &&
2836 (stp->ls_flags & NFSLCK_DELEGWRITE)) ||
2837 (!readonly && (stp->ls_clp != clp ||
2838 (stp->ls_flags & NFSLCK_DELEGREAD)))) {
2839 if (new_stp->ls_flags & NFSLCK_RECLAIM) {
2842 ret = nfsrv_delegconflict(stp, &haslock, p, vp);
2845 * nfsrv_delegconflict() unlocks state
2846 * when it returns non-zero.
2848 printf("Nfsd openctrl unexpected deleg cnfl\n");
2849 free((caddr_t)new_open, M_NFSDSTATE);
2850 free((caddr_t)new_deleg, M_NFSDSTATE);
2865 * We only get here if there was no open that conflicted.
2866 * If an open for the owner exists, or in the access/deny bits.
2867 * Otherwise it is a new open. If the open_owner hasn't been
2868 * confirmed, replace the open with the new one needing confirmation,
2869 * otherwise add the open.
2871 if (new_stp->ls_flags & NFSLCK_DELEGPREV) {
2873 * Handle NFSLCK_DELEGPREV by searching the old delegations for
2874 * a match. If found, just move the old delegation to the current
2875 * delegation list and issue open. If not found, return
2878 LIST_FOREACH(stp, &clp->lc_olddeleg, ls_list) {
2879 if (stp->ls_lfp == lfp) {
2881 if (stp->ls_clp != clp)
2882 panic("olddeleg clp");
2883 LIST_REMOVE(stp, ls_list);
2884 LIST_REMOVE(stp, ls_hash);
2885 stp->ls_flags &= ~NFSLCK_OLDDELEG;
2886 stp->ls_stateid.seqid = delegstateidp->seqid = 1;
2887 stp->ls_stateid.other[0] = delegstateidp->other[0] =
2888 clp->lc_clientid.lval[0];
2889 stp->ls_stateid.other[1] = delegstateidp->other[1] =
2890 clp->lc_clientid.lval[1];
2891 stp->ls_stateid.other[2] = delegstateidp->other[2] =
2892 nfsrv_nextstateindex(clp);
2893 stp->ls_compref = nd->nd_compref;
2894 LIST_INSERT_HEAD(&clp->lc_deleg, stp, ls_list);
2895 LIST_INSERT_HEAD(NFSSTATEHASH(clp,
2896 stp->ls_stateid), stp, ls_hash);
2897 if (stp->ls_flags & NFSLCK_DELEGWRITE)
2898 *rflagsp |= NFSV4OPEN_WRITEDELEGATE;
2900 *rflagsp |= NFSV4OPEN_READDELEGATE;
2901 clp->lc_delegtime = NFSD_MONOSEC +
2902 nfsrv_lease + NFSRV_LEASEDELTA;
2905 * Now, do the associated open.
2907 new_open->ls_stateid.seqid = 1;
2908 new_open->ls_stateid.other[0] = clp->lc_clientid.lval[0];
2909 new_open->ls_stateid.other[1] = clp->lc_clientid.lval[1];
2910 new_open->ls_stateid.other[2] = nfsrv_nextstateindex(clp);
2911 new_open->ls_flags = (new_stp->ls_flags&NFSLCK_DENYBITS)|
2913 if (stp->ls_flags & NFSLCK_DELEGWRITE)
2914 new_open->ls_flags |= (NFSLCK_READACCESS |
2915 NFSLCK_WRITEACCESS);
2917 new_open->ls_flags |= NFSLCK_READACCESS;
2918 new_open->ls_uid = new_stp->ls_uid;
2919 new_open->ls_lfp = lfp;
2920 new_open->ls_clp = clp;
2921 LIST_INIT(&new_open->ls_open);
2922 LIST_INSERT_HEAD(&lfp->lf_open, new_open, ls_file);
2923 LIST_INSERT_HEAD(NFSSTATEHASH(clp, new_open->ls_stateid),
2926 * and handle the open owner
2929 new_open->ls_openowner = ownerstp;
2930 LIST_INSERT_HEAD(&ownerstp->ls_open,new_open,ls_list);
2932 new_open->ls_openowner = new_stp;
2933 new_stp->ls_flags = 0;
2934 nfsrvd_refcache(new_stp->ls_op);
2935 new_stp->ls_noopens = 0;
2936 LIST_INIT(&new_stp->ls_open);
2937 LIST_INSERT_HEAD(&new_stp->ls_open, new_open, ls_list);
2938 LIST_INSERT_HEAD(&clp->lc_open, new_stp, ls_list);
2940 newnfsstats.srvopenowners++;
2941 nfsrv_openpluslock++;
2945 newnfsstats.srvopens++;
2946 nfsrv_openpluslock++;
2950 if (stp == LIST_END(&clp->lc_olddeleg))
2951 error = NFSERR_EXPIRED;
2952 } else if (new_stp->ls_flags & (NFSLCK_DELEGREAD | NFSLCK_DELEGWRITE)) {
2954 * Scan to see that no delegation for this client and file
2955 * doesn't already exist.
2956 * There also shouldn't yet be an Open for this file and
2959 LIST_FOREACH(stp, &lfp->lf_deleg, ls_file) {
2960 if (stp->ls_clp == clp)
2963 if (stp == LIST_END(&lfp->lf_deleg) && openstp == NULL) {
2965 * This is the Claim_Previous case with a delegation
2966 * type != Delegate_None.
2969 * First, add the delegation. (Although we must issue the
2970 * delegation, we can also ask for an immediate return.)
2972 new_deleg->ls_stateid.seqid = delegstateidp->seqid = 1;
2973 new_deleg->ls_stateid.other[0] = delegstateidp->other[0] =
2974 clp->lc_clientid.lval[0];
2975 new_deleg->ls_stateid.other[1] = delegstateidp->other[1] =
2976 clp->lc_clientid.lval[1];
2977 new_deleg->ls_stateid.other[2] = delegstateidp->other[2] =
2978 nfsrv_nextstateindex(clp);
2979 if (new_stp->ls_flags & NFSLCK_DELEGWRITE) {
2980 new_deleg->ls_flags = (NFSLCK_DELEGWRITE |
2981 NFSLCK_READACCESS | NFSLCK_WRITEACCESS);
2982 *rflagsp |= NFSV4OPEN_WRITEDELEGATE;
2983 nfsrv_writedelegcnt++;
2985 new_deleg->ls_flags = (NFSLCK_DELEGREAD |
2987 *rflagsp |= NFSV4OPEN_READDELEGATE;
2989 new_deleg->ls_uid = new_stp->ls_uid;
2990 new_deleg->ls_lfp = lfp;
2991 new_deleg->ls_clp = clp;
2992 new_deleg->ls_filerev = filerev;
2993 new_deleg->ls_compref = nd->nd_compref;
2994 LIST_INSERT_HEAD(&lfp->lf_deleg, new_deleg, ls_file);
2995 LIST_INSERT_HEAD(NFSSTATEHASH(clp,
2996 new_deleg->ls_stateid), new_deleg, ls_hash);
2997 LIST_INSERT_HEAD(&clp->lc_deleg, new_deleg, ls_list);
2999 if (delegate == 2 || nfsrv_issuedelegs == 0 ||
3000 (clp->lc_flags & (LCL_CALLBACKSON | LCL_CBDOWN)) !=
3002 NFSRV_V4DELEGLIMIT(nfsrv_delegatecnt) ||
3003 !NFSVNO_DELEGOK(vp))
3004 *rflagsp |= NFSV4OPEN_RECALL;
3005 newnfsstats.srvdelegates++;
3006 nfsrv_openpluslock++;
3007 nfsrv_delegatecnt++;
3010 * Now, do the associated open.
3012 new_open->ls_stateid.seqid = 1;
3013 new_open->ls_stateid.other[0] = clp->lc_clientid.lval[0];
3014 new_open->ls_stateid.other[1] = clp->lc_clientid.lval[1];
3015 new_open->ls_stateid.other[2] = nfsrv_nextstateindex(clp);
3016 new_open->ls_flags = (new_stp->ls_flags & NFSLCK_DENYBITS) |
3018 if (new_stp->ls_flags & NFSLCK_DELEGWRITE)
3019 new_open->ls_flags |= (NFSLCK_READACCESS |
3020 NFSLCK_WRITEACCESS);
3022 new_open->ls_flags |= NFSLCK_READACCESS;
3023 new_open->ls_uid = new_stp->ls_uid;
3024 new_open->ls_lfp = lfp;
3025 new_open->ls_clp = clp;
3026 LIST_INIT(&new_open->ls_open);
3027 LIST_INSERT_HEAD(&lfp->lf_open, new_open, ls_file);
3028 LIST_INSERT_HEAD(NFSSTATEHASH(clp, new_open->ls_stateid),
3031 * and handle the open owner
3034 new_open->ls_openowner = ownerstp;
3035 LIST_INSERT_HEAD(&ownerstp->ls_open, new_open, ls_list);
3037 new_open->ls_openowner = new_stp;
3038 new_stp->ls_flags = 0;
3039 nfsrvd_refcache(new_stp->ls_op);
3040 new_stp->ls_noopens = 0;
3041 LIST_INIT(&new_stp->ls_open);
3042 LIST_INSERT_HEAD(&new_stp->ls_open, new_open, ls_list);
3043 LIST_INSERT_HEAD(&clp->lc_open, new_stp, ls_list);
3045 newnfsstats.srvopenowners++;
3046 nfsrv_openpluslock++;
3050 newnfsstats.srvopens++;
3051 nfsrv_openpluslock++;
3053 error = NFSERR_RECLAIMCONFLICT;
3055 } else if (ownerstp) {
3056 if (ownerstp->ls_flags & NFSLCK_NEEDSCONFIRM) {
3057 /* Replace the open */
3058 if (ownerstp->ls_op)
3059 nfsrvd_derefcache(ownerstp->ls_op);
3060 ownerstp->ls_op = new_stp->ls_op;
3061 nfsrvd_refcache(ownerstp->ls_op);
3062 ownerstp->ls_seq = new_stp->ls_seq;
3063 *rflagsp |= NFSV4OPEN_RESULTCONFIRM;
3064 stp = LIST_FIRST(&ownerstp->ls_open);
3065 stp->ls_flags = (new_stp->ls_flags & NFSLCK_SHAREBITS) |
3067 stp->ls_stateid.seqid = 1;
3068 stp->ls_uid = new_stp->ls_uid;
3069 if (lfp != stp->ls_lfp) {
3070 LIST_REMOVE(stp, ls_file);
3071 LIST_INSERT_HEAD(&lfp->lf_open, stp, ls_file);
3075 } else if (openstp) {
3076 openstp->ls_flags |= (new_stp->ls_flags & NFSLCK_SHAREBITS);
3077 openstp->ls_stateid.seqid++;
3078 if ((nd->nd_flag & ND_NFSV41) != 0 &&
3079 openstp->ls_stateid.seqid == 0)
3080 openstp->ls_stateid.seqid = 1;
3083 * This is where we can choose to issue a delegation.
3085 if ((new_stp->ls_flags & NFSLCK_WANTNODELEG) != 0)
3086 *rflagsp |= NFSV4OPEN_WDNOTWANTED;
3087 else if (nfsrv_issuedelegs == 0)
3088 *rflagsp |= NFSV4OPEN_WDSUPPFTYPE;
3089 else if (NFSRV_V4DELEGLIMIT(nfsrv_delegatecnt))
3090 *rflagsp |= NFSV4OPEN_WDRESOURCE;
3091 else if (delegate == 0 || writedeleg == 0 ||
3092 NFSVNO_EXRDONLY(exp) || (readonly != 0 &&
3093 nfsrv_writedelegifpos == 0) ||
3094 !NFSVNO_DELEGOK(vp) ||
3095 (new_stp->ls_flags & NFSLCK_WANTRDELEG) != 0 ||
3096 (clp->lc_flags & (LCL_CALLBACKSON | LCL_CBDOWN)) !=
3098 *rflagsp |= NFSV4OPEN_WDCONTENTION;
3100 new_deleg->ls_stateid.seqid = delegstateidp->seqid = 1;
3101 new_deleg->ls_stateid.other[0] = delegstateidp->other[0]
3102 = clp->lc_clientid.lval[0];
3103 new_deleg->ls_stateid.other[1] = delegstateidp->other[1]
3104 = clp->lc_clientid.lval[1];
3105 new_deleg->ls_stateid.other[2] = delegstateidp->other[2]
3106 = nfsrv_nextstateindex(clp);
3107 new_deleg->ls_flags = (NFSLCK_DELEGWRITE |
3108 NFSLCK_READACCESS | NFSLCK_WRITEACCESS);
3109 *rflagsp |= NFSV4OPEN_WRITEDELEGATE;
3110 new_deleg->ls_uid = new_stp->ls_uid;
3111 new_deleg->ls_lfp = lfp;
3112 new_deleg->ls_clp = clp;
3113 new_deleg->ls_filerev = filerev;
3114 new_deleg->ls_compref = nd->nd_compref;
3115 nfsrv_writedelegcnt++;
3116 LIST_INSERT_HEAD(&lfp->lf_deleg, new_deleg, ls_file);
3117 LIST_INSERT_HEAD(NFSSTATEHASH(clp,
3118 new_deleg->ls_stateid), new_deleg, ls_hash);
3119 LIST_INSERT_HEAD(&clp->lc_deleg, new_deleg, ls_list);
3121 newnfsstats.srvdelegates++;
3122 nfsrv_openpluslock++;
3123 nfsrv_delegatecnt++;
3126 new_open->ls_stateid.seqid = 1;
3127 new_open->ls_stateid.other[0] = clp->lc_clientid.lval[0];
3128 new_open->ls_stateid.other[1] = clp->lc_clientid.lval[1];
3129 new_open->ls_stateid.other[2] = nfsrv_nextstateindex(clp);
3130 new_open->ls_flags = (new_stp->ls_flags & NFSLCK_SHAREBITS)|
3132 new_open->ls_uid = new_stp->ls_uid;
3133 new_open->ls_openowner = ownerstp;
3134 new_open->ls_lfp = lfp;
3135 new_open->ls_clp = clp;
3136 LIST_INIT(&new_open->ls_open);
3137 LIST_INSERT_HEAD(&lfp->lf_open, new_open, ls_file);
3138 LIST_INSERT_HEAD(&ownerstp->ls_open, new_open, ls_list);
3139 LIST_INSERT_HEAD(NFSSTATEHASH(clp, new_open->ls_stateid),
3143 newnfsstats.srvopens++;
3144 nfsrv_openpluslock++;
3147 * This is where we can choose to issue a delegation.
3149 if ((new_stp->ls_flags & NFSLCK_WANTNODELEG) != 0)
3150 *rflagsp |= NFSV4OPEN_WDNOTWANTED;
3151 else if (nfsrv_issuedelegs == 0)
3152 *rflagsp |= NFSV4OPEN_WDSUPPFTYPE;
3153 else if (NFSRV_V4DELEGLIMIT(nfsrv_delegatecnt))
3154 *rflagsp |= NFSV4OPEN_WDRESOURCE;
3155 else if (delegate == 0 || (writedeleg == 0 &&
3156 readonly == 0) || !NFSVNO_DELEGOK(vp) ||
3157 (clp->lc_flags & (LCL_CALLBACKSON | LCL_CBDOWN)) !=
3159 *rflagsp |= NFSV4OPEN_WDCONTENTION;
3161 new_deleg->ls_stateid.seqid = delegstateidp->seqid = 1;
3162 new_deleg->ls_stateid.other[0] = delegstateidp->other[0]
3163 = clp->lc_clientid.lval[0];
3164 new_deleg->ls_stateid.other[1] = delegstateidp->other[1]
3165 = clp->lc_clientid.lval[1];
3166 new_deleg->ls_stateid.other[2] = delegstateidp->other[2]
3167 = nfsrv_nextstateindex(clp);
3168 if (writedeleg && !NFSVNO_EXRDONLY(exp) &&
3169 (nfsrv_writedelegifpos || !readonly) &&
3170 (new_stp->ls_flags & NFSLCK_WANTRDELEG) == 0) {
3171 new_deleg->ls_flags = (NFSLCK_DELEGWRITE |
3172 NFSLCK_READACCESS | NFSLCK_WRITEACCESS);
3173 *rflagsp |= NFSV4OPEN_WRITEDELEGATE;
3174 nfsrv_writedelegcnt++;
3176 new_deleg->ls_flags = (NFSLCK_DELEGREAD |
3178 *rflagsp |= NFSV4OPEN_READDELEGATE;
3180 new_deleg->ls_uid = new_stp->ls_uid;
3181 new_deleg->ls_lfp = lfp;
3182 new_deleg->ls_clp = clp;
3183 new_deleg->ls_filerev = filerev;
3184 new_deleg->ls_compref = nd->nd_compref;
3185 LIST_INSERT_HEAD(&lfp->lf_deleg, new_deleg, ls_file);
3186 LIST_INSERT_HEAD(NFSSTATEHASH(clp,
3187 new_deleg->ls_stateid), new_deleg, ls_hash);
3188 LIST_INSERT_HEAD(&clp->lc_deleg, new_deleg, ls_list);
3190 newnfsstats.srvdelegates++;
3191 nfsrv_openpluslock++;
3192 nfsrv_delegatecnt++;
3197 * New owner case. Start the open_owner sequence with a
3198 * Needs confirmation (unless a reclaim) and hang the
3201 new_open->ls_stateid.seqid = 1;
3202 new_open->ls_stateid.other[0] = clp->lc_clientid.lval[0];
3203 new_open->ls_stateid.other[1] = clp->lc_clientid.lval[1];
3204 new_open->ls_stateid.other[2] = nfsrv_nextstateindex(clp);
3205 new_open->ls_flags = (new_stp->ls_flags & NFSLCK_SHAREBITS) |
3207 new_open->ls_uid = new_stp->ls_uid;
3208 LIST_INIT(&new_open->ls_open);
3209 new_open->ls_openowner = new_stp;
3210 new_open->ls_lfp = lfp;
3211 new_open->ls_clp = clp;
3212 LIST_INSERT_HEAD(&lfp->lf_open, new_open, ls_file);
3213 if (new_stp->ls_flags & NFSLCK_RECLAIM) {
3214 new_stp->ls_flags = 0;
3215 } else if ((nd->nd_flag & ND_NFSV41) != 0) {
3216 /* NFSv4.1 never needs confirmation. */
3217 new_stp->ls_flags = 0;
3220 * This is where we can choose to issue a delegation.
3222 if (delegate && nfsrv_issuedelegs &&
3223 (writedeleg || readonly) &&
3224 (clp->lc_flags & (LCL_CALLBACKSON | LCL_CBDOWN)) ==
3226 !NFSRV_V4DELEGLIMIT(nfsrv_delegatecnt) &&
3227 NFSVNO_DELEGOK(vp) &&
3228 ((nd->nd_flag & ND_NFSV41) == 0 ||
3229 (new_stp->ls_flags & NFSLCK_WANTNODELEG) == 0)) {
3230 new_deleg->ls_stateid.seqid =
3231 delegstateidp->seqid = 1;
3232 new_deleg->ls_stateid.other[0] =
3233 delegstateidp->other[0]
3234 = clp->lc_clientid.lval[0];
3235 new_deleg->ls_stateid.other[1] =
3236 delegstateidp->other[1]
3237 = clp->lc_clientid.lval[1];
3238 new_deleg->ls_stateid.other[2] =
3239 delegstateidp->other[2]
3240 = nfsrv_nextstateindex(clp);
3241 if (writedeleg && !NFSVNO_EXRDONLY(exp) &&
3242 (nfsrv_writedelegifpos || !readonly) &&
3243 ((nd->nd_flag & ND_NFSV41) == 0 ||
3244 (new_stp->ls_flags & NFSLCK_WANTRDELEG) ==
3246 new_deleg->ls_flags =
3247 (NFSLCK_DELEGWRITE |
3249 NFSLCK_WRITEACCESS);
3250 *rflagsp |= NFSV4OPEN_WRITEDELEGATE;
3251 nfsrv_writedelegcnt++;
3253 new_deleg->ls_flags =
3256 *rflagsp |= NFSV4OPEN_READDELEGATE;
3258 new_deleg->ls_uid = new_stp->ls_uid;
3259 new_deleg->ls_lfp = lfp;
3260 new_deleg->ls_clp = clp;
3261 new_deleg->ls_filerev = filerev;
3262 new_deleg->ls_compref = nd->nd_compref;
3263 LIST_INSERT_HEAD(&lfp->lf_deleg, new_deleg,
3265 LIST_INSERT_HEAD(NFSSTATEHASH(clp,
3266 new_deleg->ls_stateid), new_deleg, ls_hash);
3267 LIST_INSERT_HEAD(&clp->lc_deleg, new_deleg,
3270 newnfsstats.srvdelegates++;
3271 nfsrv_openpluslock++;
3272 nfsrv_delegatecnt++;
3275 * Since NFSv4.1 never does an OpenConfirm, the first
3276 * open state will be acquired here.
3278 if (!(clp->lc_flags & LCL_STAMPEDSTABLE)) {
3279 clp->lc_flags |= LCL_STAMPEDSTABLE;
3280 len = clp->lc_idlen;
3281 NFSBCOPY(clp->lc_id, clidp, len);
3285 *rflagsp |= NFSV4OPEN_RESULTCONFIRM;
3286 new_stp->ls_flags = NFSLCK_NEEDSCONFIRM;
3288 nfsrvd_refcache(new_stp->ls_op);
3289 new_stp->ls_noopens = 0;
3290 LIST_INIT(&new_stp->ls_open);
3291 LIST_INSERT_HEAD(&new_stp->ls_open, new_open, ls_list);
3292 LIST_INSERT_HEAD(&clp->lc_open, new_stp, ls_list);
3293 LIST_INSERT_HEAD(NFSSTATEHASH(clp, new_open->ls_stateid),
3298 newnfsstats.srvopens++;
3299 nfsrv_openpluslock++;
3300 newnfsstats.srvopenowners++;
3301 nfsrv_openpluslock++;
3304 stateidp->seqid = openstp->ls_stateid.seqid;
3305 stateidp->other[0] = openstp->ls_stateid.other[0];
3306 stateidp->other[1] = openstp->ls_stateid.other[1];
3307 stateidp->other[2] = openstp->ls_stateid.other[2];
3311 NFSLOCKV4ROOTMUTEX();
3312 nfsv4_unlock(&nfsv4rootfs_lock, 1);
3313 NFSUNLOCKV4ROOTMUTEX();
3316 FREE((caddr_t)new_open, M_NFSDSTATE);
3318 FREE((caddr_t)new_deleg, M_NFSDSTATE);
3321 * If the NFSv4.1 client just acquired its first open, write a timestamp
3322 * to the stable storage file.
3324 if (gotstate != 0) {
3325 nfsrv_writestable(clidp, len, NFSNST_NEWSTATE, p);
3326 nfsrv_backupstable();
3330 free(clidp, M_TEMP);
3331 NFSEXITCODE2(error, nd);
3336 * Open update. Does the confirm, downgrade and close.
3339 nfsrv_openupdate(vnode_t vp, struct nfsstate *new_stp, nfsquad_t clientid,
3340 nfsv4stateid_t *stateidp, struct nfsrv_descript *nd, NFSPROC_T *p)
3342 struct nfsstate *stp, *ownerstp;
3343 struct nfsclient *clp;
3344 struct nfslockfile *lfp;
3346 int error = 0, gotstate = 0, len = 0;
3347 u_char *clidp = NULL;
3350 * Check for restart conditions (client and server).
3352 error = nfsrv_checkrestart(clientid, new_stp->ls_flags,
3353 &new_stp->ls_stateid, 0);
3357 clidp = malloc(NFSV4_OPAQUELIMIT, M_TEMP, M_WAITOK);
3360 * Get the open structure via clientid and stateid.
3362 error = nfsrv_getclient(clientid, CLOPS_RENEW, &clp, NULL,
3363 (nfsquad_t)((u_quad_t)0), 0, nd, p);
3365 error = nfsrv_getstate(clp, &new_stp->ls_stateid,
3366 new_stp->ls_flags, &stp);
3369 * Sanity check the open.
3371 if (!error && (!(stp->ls_flags & NFSLCK_OPEN) ||
3372 (!(new_stp->ls_flags & NFSLCK_CONFIRM) &&
3373 (stp->ls_openowner->ls_flags & NFSLCK_NEEDSCONFIRM)) ||
3374 ((new_stp->ls_flags & NFSLCK_CONFIRM) &&
3375 (!(stp->ls_openowner->ls_flags & NFSLCK_NEEDSCONFIRM)))))
3376 error = NFSERR_BADSTATEID;
3379 error = nfsrv_checkseqid(nd, new_stp->ls_seq,
3380 stp->ls_openowner, new_stp->ls_op);
3381 if (!error && stp->ls_stateid.seqid != new_stp->ls_stateid.seqid &&
3382 (((nd->nd_flag & ND_NFSV41) == 0 &&
3383 !(new_stp->ls_flags & NFSLCK_CONFIRM)) ||
3384 ((nd->nd_flag & ND_NFSV41) != 0 &&
3385 new_stp->ls_stateid.seqid != 0)))
3386 error = NFSERR_OLDSTATEID;
3387 if (!error && vnode_vtype(vp) != VREG) {
3388 if (vnode_vtype(vp) == VDIR)
3389 error = NFSERR_ISDIR;
3391 error = NFSERR_INVAL;
3396 * If a client tries to confirm an Open with a bad
3397 * seqid# and there are no byte range locks or other Opens
3398 * on the openowner, just throw it away, so the next use of the
3399 * openowner will start a fresh seq#.
3401 if (error == NFSERR_BADSEQID &&
3402 (new_stp->ls_flags & NFSLCK_CONFIRM) &&
3403 nfsrv_nootherstate(stp))
3404 nfsrv_freeopenowner(stp->ls_openowner, 0, p);
3410 * Set the return stateid.
3412 stateidp->seqid = stp->ls_stateid.seqid + 1;
3413 if ((nd->nd_flag & ND_NFSV41) != 0 && stateidp->seqid == 0)
3414 stateidp->seqid = 1;
3415 stateidp->other[0] = stp->ls_stateid.other[0];
3416 stateidp->other[1] = stp->ls_stateid.other[1];
3417 stateidp->other[2] = stp->ls_stateid.other[2];
3419 * Now, handle the three cases.
3421 if (new_stp->ls_flags & NFSLCK_CONFIRM) {
3423 * If the open doesn't need confirmation, it seems to me that
3424 * there is a client error, but I'll just log it and keep going?
3426 if (!(stp->ls_openowner->ls_flags & NFSLCK_NEEDSCONFIRM))
3427 printf("Nfsv4d: stray open confirm\n");
3428 stp->ls_openowner->ls_flags = 0;
3429 stp->ls_stateid.seqid++;
3430 if ((nd->nd_flag & ND_NFSV41) != 0 &&
3431 stp->ls_stateid.seqid == 0)
3432 stp->ls_stateid.seqid = 1;
3433 if (!(clp->lc_flags & LCL_STAMPEDSTABLE)) {
3434 clp->lc_flags |= LCL_STAMPEDSTABLE;
3435 len = clp->lc_idlen;
3436 NFSBCOPY(clp->lc_id, clidp, len);
3440 } else if (new_stp->ls_flags & NFSLCK_CLOSE) {
3441 ownerstp = stp->ls_openowner;
3443 if (nfsrv_dolocallocks != 0 && !LIST_EMPTY(&stp->ls_open)) {
3444 /* Get the lf lock */
3447 ASSERT_VOP_ELOCKED(vp, "nfsrv_openupdate");
3448 NFSVOPUNLOCK(vp, 0);
3449 if (nfsrv_freeopen(stp, vp, 1, p) == 0) {
3451 nfsrv_unlocklf(lfp);
3454 NFSVOPLOCK(vp, LK_EXCLUSIVE | LK_RETRY);
3456 (void) nfsrv_freeopen(stp, NULL, 0, p);
3461 * Update the share bits, making sure that the new set are a
3462 * subset of the old ones.
3464 bits = (new_stp->ls_flags & NFSLCK_SHAREBITS);
3465 if (~(stp->ls_flags) & bits) {
3467 error = NFSERR_INVAL;
3470 stp->ls_flags = (bits | NFSLCK_OPEN);
3471 stp->ls_stateid.seqid++;
3472 if ((nd->nd_flag & ND_NFSV41) != 0 &&
3473 stp->ls_stateid.seqid == 0)
3474 stp->ls_stateid.seqid = 1;
3479 * If the client just confirmed its first open, write a timestamp
3480 * to the stable storage file.
3482 if (gotstate != 0) {
3483 nfsrv_writestable(clidp, len, NFSNST_NEWSTATE, p);
3484 nfsrv_backupstable();
3488 free(clidp, M_TEMP);
3489 NFSEXITCODE2(error, nd);
3494 * Delegation update. Does the purge and return.
3497 nfsrv_delegupdate(struct nfsrv_descript *nd, nfsquad_t clientid,
3498 nfsv4stateid_t *stateidp, vnode_t vp, int op, struct ucred *cred,
3501 struct nfsstate *stp;
3502 struct nfsclient *clp;
3507 * Do a sanity check against the file handle for DelegReturn.
3510 error = nfsvno_getfh(vp, &fh, p);
3515 * Check for restart conditions (client and server).
3517 if (op == NFSV4OP_DELEGRETURN)
3518 error = nfsrv_checkrestart(clientid, NFSLCK_DELEGRETURN,
3521 error = nfsrv_checkrestart(clientid, NFSLCK_DELEGPURGE,
3526 * Get the open structure via clientid and stateid.
3529 error = nfsrv_getclient(clientid, CLOPS_RENEW, &clp, NULL,
3530 (nfsquad_t)((u_quad_t)0), 0, nd, p);
3532 if (error == NFSERR_CBPATHDOWN)
3534 if (error == NFSERR_STALECLIENTID && op == NFSV4OP_DELEGRETURN)
3535 error = NFSERR_STALESTATEID;
3537 if (!error && op == NFSV4OP_DELEGRETURN) {
3538 error = nfsrv_getstate(clp, stateidp, NFSLCK_DELEGRETURN, &stp);
3539 if (!error && stp->ls_stateid.seqid != stateidp->seqid &&
3540 ((nd->nd_flag & ND_NFSV41) == 0 || stateidp->seqid != 0))
3541 error = NFSERR_OLDSTATEID;
3544 * NFSERR_EXPIRED means that the state has gone away,
3545 * so Delegations have been purged. Just return ok.
3547 if (error == NFSERR_EXPIRED && op == NFSV4OP_DELEGPURGE) {
3557 if (op == NFSV4OP_DELEGRETURN) {
3558 if (NFSBCMP((caddr_t)&fh, (caddr_t)&stp->ls_lfp->lf_fh,
3559 sizeof (fhandle_t))) {
3561 error = NFSERR_BADSTATEID;
3564 nfsrv_freedeleg(stp);
3566 nfsrv_freedeleglist(&clp->lc_olddeleg);
3577 * Release lock owner.
3580 nfsrv_releaselckown(struct nfsstate *new_stp, nfsquad_t clientid,
3583 struct nfsstate *stp, *nstp, *openstp, *ownstp;
3584 struct nfsclient *clp;
3588 * Check for restart conditions (client and server).
3590 error = nfsrv_checkrestart(clientid, new_stp->ls_flags,
3591 &new_stp->ls_stateid, 0);
3597 * Get the lock owner by name.
3599 error = nfsrv_getclient(clientid, CLOPS_RENEW, &clp, NULL,
3600 (nfsquad_t)((u_quad_t)0), 0, NULL, p);
3605 LIST_FOREACH(ownstp, &clp->lc_open, ls_list) {
3606 LIST_FOREACH(openstp, &ownstp->ls_open, ls_list) {
3607 stp = LIST_FIRST(&openstp->ls_open);
3608 while (stp != LIST_END(&openstp->ls_open)) {
3609 nstp = LIST_NEXT(stp, ls_list);
3611 * If the owner matches, check for locks and
3612 * then free or return an error.
3614 if (stp->ls_ownerlen == new_stp->ls_ownerlen &&
3615 !NFSBCMP(stp->ls_owner, new_stp->ls_owner,
3617 if (LIST_EMPTY(&stp->ls_lock)) {
3618 nfsrv_freelockowner(stp, NULL, 0, p);
3621 error = NFSERR_LOCKSHELD;
3637 * Get the file handle for a lock structure.
3640 nfsrv_getlockfh(vnode_t vp, u_short flags, struct nfslockfile *new_lfp,
3641 fhandle_t *nfhp, NFSPROC_T *p)
3643 fhandle_t *fhp = NULL;
3647 * For lock, use the new nfslock structure, otherwise just
3648 * a fhandle_t on the stack.
3650 if (flags & NFSLCK_OPEN) {
3651 KASSERT(new_lfp != NULL, ("nfsrv_getlockfh: new_lfp NULL"));
3652 fhp = &new_lfp->lf_fh;
3656 panic("nfsrv_getlockfh");
3658 error = nfsvno_getfh(vp, fhp, p);
3664 * Get an nfs lock structure. Allocate one, as required, and return a
3666 * Returns an NFSERR_xxx upon failure or -1 to indicate no current lock.
3669 nfsrv_getlockfile(u_short flags, struct nfslockfile **new_lfpp,
3670 struct nfslockfile **lfpp, fhandle_t *nfhp, int lockit)
3672 struct nfslockfile *lfp;
3673 fhandle_t *fhp = NULL, *tfhp;
3674 struct nfslockhashhead *hp;
3675 struct nfslockfile *new_lfp = NULL;
3678 * For lock, use the new nfslock structure, otherwise just
3679 * a fhandle_t on the stack.
3681 if (flags & NFSLCK_OPEN) {
3682 new_lfp = *new_lfpp;
3683 fhp = &new_lfp->lf_fh;
3687 panic("nfsrv_getlockfile");
3690 hp = NFSLOCKHASH(fhp);
3691 LIST_FOREACH(lfp, hp, lf_hash) {
3693 if (NFSVNO_CMPFH(fhp, tfhp)) {
3700 if (!(flags & NFSLCK_OPEN))
3704 * No match, so chain the new one into the list.
3706 LIST_INIT(&new_lfp->lf_open);
3707 LIST_INIT(&new_lfp->lf_lock);
3708 LIST_INIT(&new_lfp->lf_deleg);
3709 LIST_INIT(&new_lfp->lf_locallock);
3710 LIST_INIT(&new_lfp->lf_rollback);
3711 new_lfp->lf_locallock_lck.nfslock_usecnt = 0;
3712 new_lfp->lf_locallock_lck.nfslock_lock = 0;
3713 new_lfp->lf_usecount = 0;
3714 LIST_INSERT_HEAD(hp, new_lfp, lf_hash);
3721 * This function adds a nfslock lock structure to the list for the associated
3722 * nfsstate and nfslockfile structures. It will be inserted after the
3723 * entry pointed at by insert_lop.
3726 nfsrv_insertlock(struct nfslock *new_lop, struct nfslock *insert_lop,
3727 struct nfsstate *stp, struct nfslockfile *lfp)
3729 struct nfslock *lop, *nlop;
3731 new_lop->lo_stp = stp;
3732 new_lop->lo_lfp = lfp;
3735 /* Insert in increasing lo_first order */
3736 lop = LIST_FIRST(&lfp->lf_lock);
3737 if (lop == LIST_END(&lfp->lf_lock) ||
3738 new_lop->lo_first <= lop->lo_first) {
3739 LIST_INSERT_HEAD(&lfp->lf_lock, new_lop, lo_lckfile);
3741 nlop = LIST_NEXT(lop, lo_lckfile);
3742 while (nlop != LIST_END(&lfp->lf_lock) &&
3743 nlop->lo_first < new_lop->lo_first) {
3745 nlop = LIST_NEXT(lop, lo_lckfile);
3747 LIST_INSERT_AFTER(lop, new_lop, lo_lckfile);
3750 new_lop->lo_lckfile.le_prev = NULL; /* list not used */
3754 * Insert after insert_lop, which is overloaded as stp or lfp for
3757 if (stp == NULL && (struct nfslockfile *)insert_lop == lfp)
3758 LIST_INSERT_HEAD(&lfp->lf_locallock, new_lop, lo_lckowner);
3759 else if ((struct nfsstate *)insert_lop == stp)
3760 LIST_INSERT_HEAD(&stp->ls_lock, new_lop, lo_lckowner);
3762 LIST_INSERT_AFTER(insert_lop, new_lop, lo_lckowner);
3764 newnfsstats.srvlocks++;
3765 nfsrv_openpluslock++;
3770 * This function updates the locking for a lock owner and given file. It
3771 * maintains a list of lock ranges ordered on increasing file offset that
3772 * are NFSLCK_READ or NFSLCK_WRITE and non-overlapping (aka POSIX style).
3773 * It always adds new_lop to the list and sometimes uses the one pointed
3777 nfsrv_updatelock(struct nfsstate *stp, struct nfslock **new_lopp,
3778 struct nfslock **other_lopp, struct nfslockfile *lfp)
3780 struct nfslock *new_lop = *new_lopp;
3781 struct nfslock *lop, *tlop, *ilop;
3782 struct nfslock *other_lop = *other_lopp;
3783 int unlock = 0, myfile = 0;
3787 * Work down the list until the lock is merged.
3789 if (new_lop->lo_flags & NFSLCK_UNLOCK)
3792 ilop = (struct nfslock *)stp;
3793 lop = LIST_FIRST(&stp->ls_lock);
3795 ilop = (struct nfslock *)lfp;
3796 lop = LIST_FIRST(&lfp->lf_locallock);
3798 while (lop != NULL) {
3800 * Only check locks for this file that aren't before the start of
3803 if (lop->lo_lfp == lfp) {
3805 if (lop->lo_end >= new_lop->lo_first) {
3806 if (new_lop->lo_end < lop->lo_first) {
3808 * If the new lock ends before the start of the
3809 * current lock's range, no merge, just insert
3814 if (new_lop->lo_flags == lop->lo_flags ||
3815 (new_lop->lo_first <= lop->lo_first &&
3816 new_lop->lo_end >= lop->lo_end)) {
3818 * This lock can be absorbed by the new lock/unlock.
3819 * This happens when it covers the entire range
3820 * of the old lock or is contiguous
3821 * with the old lock and is of the same type or an
3824 if (lop->lo_first < new_lop->lo_first)
3825 new_lop->lo_first = lop->lo_first;
3826 if (lop->lo_end > new_lop->lo_end)
3827 new_lop->lo_end = lop->lo_end;
3829 lop = LIST_NEXT(lop, lo_lckowner);
3830 nfsrv_freenfslock(tlop);
3835 * All these cases are for contiguous locks that are not the
3836 * same type, so they can't be merged.
3838 if (new_lop->lo_first <= lop->lo_first) {
3840 * This case is where the new lock overlaps with the
3841 * first part of the old lock. Move the start of the
3842 * old lock to just past the end of the new lock. The
3843 * new lock will be inserted in front of the old, since
3844 * ilop hasn't been updated. (We are done now.)
3846 lop->lo_first = new_lop->lo_end;
3849 if (new_lop->lo_end >= lop->lo_end) {
3851 * This case is where the new lock overlaps with the
3852 * end of the old lock's range. Move the old lock's
3853 * end to just before the new lock's first and insert
3854 * the new lock after the old lock.
3855 * Might not be done yet, since the new lock could
3856 * overlap further locks with higher ranges.
3858 lop->lo_end = new_lop->lo_first;
3860 lop = LIST_NEXT(lop, lo_lckowner);
3864 * The final case is where the new lock's range is in the
3865 * middle of the current lock's and splits the current lock
3866 * up. Use *other_lopp to handle the second part of the
3867 * split old lock range. (We are done now.)
3868 * For unlock, we use new_lop as other_lop and tmp, since
3869 * other_lop and new_lop are the same for this case.
3870 * We noted the unlock case above, so we don't need
3871 * new_lop->lo_flags any longer.
3873 tmp = new_lop->lo_first;
3874 if (other_lop == NULL) {
3876 panic("nfsd srv update unlock");
3877 other_lop = new_lop;
3880 other_lop->lo_first = new_lop->lo_end;
3881 other_lop->lo_end = lop->lo_end;
3882 other_lop->lo_flags = lop->lo_flags;
3883 other_lop->lo_stp = stp;
3884 other_lop->lo_lfp = lfp;
3886 nfsrv_insertlock(other_lop, lop, stp, lfp);
3893 lop = LIST_NEXT(lop, lo_lckowner);
3894 if (myfile && (lop == NULL || lop->lo_lfp != lfp))
3899 * Insert the new lock in the list at the appropriate place.
3902 nfsrv_insertlock(new_lop, ilop, stp, lfp);
3908 * This function handles sequencing of locks, etc.
3909 * It returns an error that indicates what the caller should do.
3912 nfsrv_checkseqid(struct nfsrv_descript *nd, u_int32_t seqid,
3913 struct nfsstate *stp, struct nfsrvcache *op)
3917 if ((nd->nd_flag & ND_NFSV41) != 0)
3918 /* NFSv4.1 ignores the open_seqid and lock_seqid. */
3920 if (op != nd->nd_rp)
3921 panic("nfsrvstate checkseqid");
3922 if (!(op->rc_flag & RC_INPROG))
3923 panic("nfsrvstate not inprog");
3924 if (stp->ls_op && stp->ls_op->rc_refcnt <= 0) {
3925 printf("refcnt=%d\n", stp->ls_op->rc_refcnt);
3926 panic("nfsrvstate op refcnt");
3928 if ((stp->ls_seq + 1) == seqid) {
3930 nfsrvd_derefcache(stp->ls_op);
3932 nfsrvd_refcache(op);
3933 stp->ls_seq = seqid;
3935 } else if (stp->ls_seq == seqid && stp->ls_op &&
3936 op->rc_xid == stp->ls_op->rc_xid &&
3937 op->rc_refcnt == 0 &&
3938 op->rc_reqlen == stp->ls_op->rc_reqlen &&
3939 op->rc_cksum == stp->ls_op->rc_cksum) {
3940 if (stp->ls_op->rc_flag & RC_INPROG) {
3941 error = NFSERR_DONTREPLY;
3944 nd->nd_rp = stp->ls_op;
3945 nd->nd_rp->rc_flag |= RC_INPROG;
3946 nfsrvd_delcache(op);
3947 error = NFSERR_REPLYFROMCACHE;
3950 error = NFSERR_BADSEQID;
3953 NFSEXITCODE2(error, nd);
3958 * Get the client ip address for callbacks. If the strings can't be parsed,
3959 * just set lc_program to 0 to indicate no callbacks are possible.
3960 * (For cases where the address can't be parsed or is 0.0.0.0.0.0, set
3961 * the address to the client's transport address. This won't be used
3962 * for callbacks, but can be printed out by newnfsstats for info.)
3963 * Return error if the xdr can't be parsed, 0 otherwise.
3966 nfsrv_getclientipaddr(struct nfsrv_descript *nd, struct nfsclient *clp)
3970 int i, j, maxalen = 0, minalen = 0;
3973 struct sockaddr_in *rin, *sin;
3976 struct sockaddr_in6 *rin6, *sin6;
3979 int error = 0, cantparse = 0;
3989 /* 8 is the maximum length of the port# string. */
3990 addr = malloc(INET6_ADDRSTRLEN + 8, M_TEMP, M_WAITOK);
3991 clp->lc_req.nr_client = NULL;
3992 clp->lc_req.nr_lock = 0;
3994 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
3995 i = fxdr_unsigned(int, *tl);
3996 if (i >= 3 && i <= 4) {
3997 error = nfsrv_mtostr(nd, addr, i);
4001 if (!strcmp(addr, "tcp")) {
4002 clp->lc_flags |= LCL_TCPCALLBACK;
4003 clp->lc_req.nr_sotype = SOCK_STREAM;
4004 clp->lc_req.nr_soproto = IPPROTO_TCP;
4006 } else if (!strcmp(addr, "udp")) {
4007 clp->lc_req.nr_sotype = SOCK_DGRAM;
4008 clp->lc_req.nr_soproto = IPPROTO_UDP;
4013 if (af == AF_UNSPEC) {
4014 if (!strcmp(addr, "tcp6")) {
4015 clp->lc_flags |= LCL_TCPCALLBACK;
4016 clp->lc_req.nr_sotype = SOCK_STREAM;
4017 clp->lc_req.nr_soproto = IPPROTO_TCP;
4019 } else if (!strcmp(addr, "udp6")) {
4020 clp->lc_req.nr_sotype = SOCK_DGRAM;
4021 clp->lc_req.nr_soproto = IPPROTO_UDP;
4026 if (af == AF_UNSPEC) {
4032 error = nfsm_advance(nd, NFSM_RNDUP(i), -1);
4038 * The caller has allocated clp->lc_req.nr_nam to be large enough
4039 * for either AF_INET or AF_INET6 and zeroed out the contents.
4040 * maxalen is set to the maximum length of the host IP address string
4041 * plus 8 for the maximum length of the port#.
4042 * minalen is set to the minimum length of the host IP address string
4043 * plus 4 for the minimum length of the port#.
4044 * These lengths do not include NULL termination,
4045 * so INET[6]_ADDRSTRLEN - 1 is used in the calculations.
4050 rin = (struct sockaddr_in *)clp->lc_req.nr_nam;
4051 rin->sin_family = AF_INET;
4052 rin->sin_len = sizeof(struct sockaddr_in);
4053 maxalen = INET_ADDRSTRLEN - 1 + 8;
4059 rin6 = (struct sockaddr_in6 *)clp->lc_req.nr_nam;
4060 rin6->sin6_family = AF_INET6;
4061 rin6->sin6_len = sizeof(struct sockaddr_in6);
4062 maxalen = INET6_ADDRSTRLEN - 1 + 8;
4067 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
4068 i = fxdr_unsigned(int, *tl);
4070 error = NFSERR_BADXDR;
4072 } else if (i == 0) {
4074 } else if (!cantparse && i <= maxalen && i >= minalen) {
4075 error = nfsrv_mtostr(nd, addr, i);
4080 * Parse out the address fields. We expect 6 decimal numbers
4081 * separated by '.'s for AF_INET and two decimal numbers
4082 * preceeded by '.'s for AF_INET6.
4088 * For AF_INET6, first parse the host address.
4091 cp = strchr(addr, '.');
4094 if (inet_pton(af, addr, &rin6->sin6_addr) == 1)
4110 while (cp != NULL && *cp && i < 6) {
4112 while (*cp2 && *cp2 != '.')
4120 j = nfsrv_getipnumber(cp);
4125 port.cval[5 - i] = j;
4135 * The host address INADDR_ANY is (mis)used to indicate
4136 * "there is no valid callback address".
4141 if (!IN6_ARE_ADDR_EQUAL(&rin6->sin6_addr,
4143 rin6->sin6_port = htons(port.sval);
4150 if (ip.ival != INADDR_ANY) {
4151 rin->sin_addr.s_addr = htonl(ip.ival);
4152 rin->sin_port = htons(port.sval);
4163 error = nfsm_advance(nd, NFSM_RNDUP(i), -1);
4169 switch (nd->nd_nam->sa_family) {
4172 sin = (struct sockaddr_in *)nd->nd_nam;
4173 rin = (struct sockaddr_in *)clp->lc_req.nr_nam;
4174 rin->sin_family = AF_INET;
4175 rin->sin_len = sizeof(struct sockaddr_in);
4176 rin->sin_addr.s_addr = sin->sin_addr.s_addr;
4177 rin->sin_port = 0x0;
4182 sin6 = (struct sockaddr_in6 *)nd->nd_nam;
4183 rin6 = (struct sockaddr_in6 *)clp->lc_req.nr_nam;
4184 rin6->sin6_family = AF_INET6;
4185 rin6->sin6_len = sizeof(struct sockaddr_in6);
4186 rin6->sin6_addr = sin6->sin6_addr;
4187 rin6->sin6_port = 0x0;
4191 clp->lc_program = 0;
4195 NFSEXITCODE2(error, nd);
4200 * Turn a string of up to three decimal digits into a number. Return -1 upon
4204 nfsrv_getipnumber(u_char *cp)
4209 if (j > 2 || *cp < '0' || *cp > '9')
4222 * This function checks for restart conditions.
4225 nfsrv_checkrestart(nfsquad_t clientid, u_int32_t flags,
4226 nfsv4stateid_t *stateidp, int specialid)
4231 * First check for a server restart. Open, LockT, ReleaseLockOwner
4232 * and DelegPurge have a clientid, the rest a stateid.
4235 (NFSLCK_OPEN | NFSLCK_TEST | NFSLCK_RELEASE | NFSLCK_DELEGPURGE)) {
4236 if (clientid.lval[0] != nfsrvboottime) {
4237 ret = NFSERR_STALECLIENTID;
4240 } else if (stateidp->other[0] != nfsrvboottime &&
4242 ret = NFSERR_STALESTATEID;
4247 * Read, Write, Setattr and LockT can return NFSERR_GRACE and do
4248 * not use a lock/open owner seqid#, so the check can be done now.
4249 * (The others will be checked, as required, later.)
4251 if (!(flags & (NFSLCK_CHECK | NFSLCK_TEST)))
4255 ret = nfsrv_checkgrace(NULL, NULL, flags);
4267 nfsrv_checkgrace(struct nfsrv_descript *nd, struct nfsclient *clp,
4270 int error = 0, notreclaimed;
4271 struct nfsrv_stable *sp;
4273 if ((nfsrv_stablefirst.nsf_flags & (NFSNSF_UPDATEDONE |
4274 NFSNSF_GRACEOVER)) == 0) {
4276 * First, check to see if all of the clients have done a
4277 * ReclaimComplete. If so, grace can end now.
4280 LIST_FOREACH(sp, &nfsrv_stablefirst.nsf_head, nst_list) {
4281 if ((sp->nst_flag & NFSNST_RECLAIMED) == 0) {
4286 if (notreclaimed == 0)
4287 nfsrv_stablefirst.nsf_flags |= (NFSNSF_GRACEOVER |
4291 if ((nfsrv_stablefirst.nsf_flags & NFSNSF_GRACEOVER) != 0) {
4292 if (flags & NFSLCK_RECLAIM) {
4293 error = NFSERR_NOGRACE;
4297 if (!(flags & NFSLCK_RECLAIM)) {
4298 error = NFSERR_GRACE;
4301 if (nd != NULL && clp != NULL &&
4302 (nd->nd_flag & ND_NFSV41) != 0 &&
4303 (clp->lc_flags & LCL_RECLAIMCOMPLETE) != 0) {
4304 error = NFSERR_NOGRACE;
4309 * If grace is almost over and we are still getting Reclaims,
4310 * extend grace a bit.
4312 if ((NFSD_MONOSEC + NFSRV_LEASEDELTA) >
4313 nfsrv_stablefirst.nsf_eograce)
4314 nfsrv_stablefirst.nsf_eograce = NFSD_MONOSEC +
4324 * Do a server callback.
4327 nfsrv_docallback(struct nfsclient *clp, int procnum,
4328 nfsv4stateid_t *stateidp, int trunc, fhandle_t *fhp,
4329 struct nfsvattr *nap, nfsattrbit_t *attrbitp, NFSPROC_T *p)
4333 struct nfsrv_descript nfsd, *nd = &nfsd;
4337 struct nfsdsession *sep = NULL;
4339 cred = newnfs_getcred();
4340 NFSLOCKSTATE(); /* mostly for lc_cbref++ */
4341 if (clp->lc_flags & LCL_NEEDSCONFIRM) {
4348 * Fill the callback program# and version into the request
4349 * structure for newnfs_connect() to use.
4351 clp->lc_req.nr_prog = clp->lc_program;
4353 if ((clp->lc_flags & LCL_NFSV41) != 0)
4354 clp->lc_req.nr_vers = NFSV41_CBVERS;
4357 clp->lc_req.nr_vers = NFSV4_CBVERS;
4360 * First, fill in some of the fields of nd and cr.
4362 nd->nd_flag = ND_NFSV4;
4363 if (clp->lc_flags & LCL_GSS)
4364 nd->nd_flag |= ND_KERBV;
4365 if ((clp->lc_flags & LCL_NFSV41) != 0)
4366 nd->nd_flag |= ND_NFSV41;
4368 cred->cr_uid = clp->lc_uid;
4369 cred->cr_gid = clp->lc_gid;
4370 callback = clp->lc_callback;
4372 cred->cr_ngroups = 1;
4375 * Get the first mbuf for the request.
4377 MGET(m, M_WAITOK, MT_DATA);
4379 nd->nd_mreq = nd->nd_mb = m;
4380 nd->nd_bpos = NFSMTOD(m, caddr_t);
4383 * and build the callback request.
4385 if (procnum == NFSV4OP_CBGETATTR) {
4386 nd->nd_procnum = NFSV4PROC_CBCOMPOUND;
4387 error = nfsrv_cbcallargs(nd, clp, callback, NFSV4OP_CBGETATTR,
4388 "CB Getattr", &sep);
4390 mbuf_freem(nd->nd_mreq);
4393 (void)nfsm_fhtom(nd, (u_int8_t *)fhp, NFSX_MYFH, 0);
4394 (void)nfsrv_putattrbit(nd, attrbitp);
4395 } else if (procnum == NFSV4OP_CBRECALL) {
4396 nd->nd_procnum = NFSV4PROC_CBCOMPOUND;
4397 error = nfsrv_cbcallargs(nd, clp, callback, NFSV4OP_CBRECALL,
4400 mbuf_freem(nd->nd_mreq);
4403 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED + NFSX_STATEID);
4404 *tl++ = txdr_unsigned(stateidp->seqid);
4405 NFSBCOPY((caddr_t)stateidp->other, (caddr_t)tl,
4407 tl += (NFSX_STATEIDOTHER / NFSX_UNSIGNED);
4412 (void)nfsm_fhtom(nd, (u_int8_t *)fhp, NFSX_MYFH, 0);
4413 } else if (procnum == NFSV4PROC_CBNULL) {
4414 nd->nd_procnum = NFSV4PROC_CBNULL;
4415 if ((clp->lc_flags & LCL_NFSV41) != 0) {
4416 error = nfsv4_getcbsession(clp, &sep);
4418 mbuf_freem(nd->nd_mreq);
4423 error = NFSERR_SERVERFAULT;
4424 mbuf_freem(nd->nd_mreq);
4429 * Call newnfs_connect(), as required, and then newnfs_request().
4431 (void) newnfs_sndlock(&clp->lc_req.nr_lock);
4432 if (clp->lc_req.nr_client == NULL) {
4433 if ((clp->lc_flags & LCL_NFSV41) != 0) {
4434 error = ECONNREFUSED;
4435 nfsrv_freesession(sep, NULL);
4436 } else if (nd->nd_procnum == NFSV4PROC_CBNULL)
4437 error = newnfs_connect(NULL, &clp->lc_req, cred,
4440 error = newnfs_connect(NULL, &clp->lc_req, cred,
4443 newnfs_sndunlock(&clp->lc_req.nr_lock);
4445 if ((nd->nd_flag & ND_NFSV41) != 0) {
4446 KASSERT(sep != NULL, ("sep NULL"));
4447 if (sep->sess_cbsess.nfsess_xprt != NULL)
4448 error = newnfs_request(nd, NULL, clp,
4449 &clp->lc_req, NULL, NULL, cred,
4450 clp->lc_program, clp->lc_req.nr_vers, NULL,
4451 1, NULL, &sep->sess_cbsess);
4454 * This should probably never occur, but if a
4455 * client somehow does an RPC without a
4456 * SequenceID Op that causes a callback just
4457 * after the nfsd threads have been terminated
4458 * and restared we could conceivably get here
4459 * without a backchannel xprt.
4461 printf("nfsrv_docallback: no xprt\n");
4462 error = ECONNREFUSED;
4464 nfsrv_freesession(sep, NULL);
4466 error = newnfs_request(nd, NULL, clp, &clp->lc_req,
4467 NULL, NULL, cred, clp->lc_program,
4468 clp->lc_req.nr_vers, NULL, 1, NULL, NULL);
4474 * If error is set here, the Callback path isn't working
4475 * properly, so twiddle the appropriate LCL_ flags.
4476 * (nd_repstat != 0 indicates the Callback path is working,
4477 * but the callback failed on the client.)
4481 * Mark the callback pathway down, which disabled issuing
4482 * of delegations and gets Renew to return NFSERR_CBPATHDOWN.
4485 clp->lc_flags |= LCL_CBDOWN;
4489 * Callback worked. If the callback path was down, disable
4490 * callbacks, so no more delegations will be issued. (This
4491 * is done on the assumption that the callback pathway is
4495 if (clp->lc_flags & LCL_CBDOWN)
4496 clp->lc_flags &= ~(LCL_CBDOWN | LCL_CALLBACKSON);
4499 error = nd->nd_repstat;
4500 else if (error == 0 && procnum == NFSV4OP_CBGETATTR)
4501 error = nfsv4_loadattr(nd, NULL, nap, NULL, NULL, 0,
4502 NULL, NULL, NULL, NULL, NULL, 0, NULL, NULL, NULL,
4504 mbuf_freem(nd->nd_mrep);
4508 if ((clp->lc_flags & LCL_WAKEUPWANTED) && clp->lc_cbref == 0) {
4509 clp->lc_flags &= ~LCL_WAKEUPWANTED;
4519 * Set up the compound RPC for the callback.
4522 nfsrv_cbcallargs(struct nfsrv_descript *nd, struct nfsclient *clp,
4523 uint32_t callback, int op, const char *optag, struct nfsdsession **sepp)
4528 len = strlen(optag);
4529 (void)nfsm_strtom(nd, optag, len);
4530 NFSM_BUILD(tl, uint32_t *, 4 * NFSX_UNSIGNED);
4531 if ((nd->nd_flag & ND_NFSV41) != 0) {
4532 *tl++ = txdr_unsigned(NFSV41_MINORVERSION);
4533 *tl++ = txdr_unsigned(callback);
4534 *tl++ = txdr_unsigned(2);
4535 *tl = txdr_unsigned(NFSV4OP_CBSEQUENCE);
4536 error = nfsv4_setcbsequence(nd, clp, 1, sepp);
4539 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
4540 *tl = txdr_unsigned(op);
4542 *tl++ = txdr_unsigned(NFSV4_MINORVERSION);
4543 *tl++ = txdr_unsigned(callback);
4544 *tl++ = txdr_unsigned(1);
4545 *tl = txdr_unsigned(op);
4551 * Return the next index# for a clientid. Mostly just increment and return
4552 * the next one, but... if the 32bit unsigned does actually wrap around,
4553 * it should be rebooted.
4554 * At an average rate of one new client per second, it will wrap around in
4555 * approximately 136 years. (I think the server will have been shut
4556 * down or rebooted before then.)
4559 nfsrv_nextclientindex(void)
4561 static u_int32_t client_index = 0;
4564 if (client_index != 0)
4565 return (client_index);
4567 printf("%s: out of clientids\n", __func__);
4568 return (client_index);
4572 * Return the next index# for a stateid. Mostly just increment and return
4573 * the next one, but... if the 32bit unsigned does actually wrap around
4574 * (will a BSD server stay up that long?), find
4575 * new start and end values.
4578 nfsrv_nextstateindex(struct nfsclient *clp)
4580 struct nfsstate *stp;
4582 u_int32_t canuse, min_index, max_index;
4584 if (!(clp->lc_flags & LCL_INDEXNOTOK)) {
4585 clp->lc_stateindex++;
4586 if (clp->lc_stateindex != clp->lc_statemaxindex)
4587 return (clp->lc_stateindex);
4591 * Yuck, we've hit the end.
4592 * Look for a new min and max.
4595 max_index = 0xffffffff;
4596 for (i = 0; i < nfsrv_statehashsize; i++) {
4597 LIST_FOREACH(stp, &clp->lc_stateid[i], ls_hash) {
4598 if (stp->ls_stateid.other[2] > 0x80000000) {
4599 if (stp->ls_stateid.other[2] < max_index)
4600 max_index = stp->ls_stateid.other[2];
4602 if (stp->ls_stateid.other[2] > min_index)
4603 min_index = stp->ls_stateid.other[2];
4609 * Yikes, highly unlikely, but I'll handle it anyhow.
4611 if (min_index == 0x80000000 && max_index == 0x80000001) {
4614 * Loop around until we find an unused entry. Return that
4615 * and set LCL_INDEXNOTOK, so the search will continue next time.
4616 * (This is one of those rare cases where a goto is the
4617 * cleanest way to code the loop.)
4620 for (i = 0; i < nfsrv_statehashsize; i++) {
4621 LIST_FOREACH(stp, &clp->lc_stateid[i], ls_hash) {
4622 if (stp->ls_stateid.other[2] == canuse) {
4628 clp->lc_flags |= LCL_INDEXNOTOK;
4633 * Ok to start again from min + 1.
4635 clp->lc_stateindex = min_index + 1;
4636 clp->lc_statemaxindex = max_index;
4637 clp->lc_flags &= ~LCL_INDEXNOTOK;
4638 return (clp->lc_stateindex);
4642 * The following functions handle the stable storage file that deals with
4643 * the edge conditions described in RFC3530 Sec. 8.6.3.
4644 * The file is as follows:
4645 * - a single record at the beginning that has the lease time of the
4646 * previous server instance (before the last reboot) and the nfsrvboottime
4647 * values for the previous server boots.
4648 * These previous boot times are used to ensure that the current
4649 * nfsrvboottime does not, somehow, get set to a previous one.
4650 * (This is important so that Stale ClientIDs and StateIDs can
4652 * The number of previous nfsvrboottime values preceeds the list.
4653 * - followed by some number of appended records with:
4654 * - client id string
4655 * - flag that indicates it is a record revoking state via lease
4656 * expiration or similar
4657 * OR has successfully acquired state.
4658 * These structures vary in length, with the client string at the end, up
4659 * to NFSV4_OPAQUELIMIT in size.
4661 * At the end of the grace period, the file is truncated, the first
4662 * record is rewritten with updated information and any acquired state
4663 * records for successful reclaims of state are written.
4665 * Subsequent records are appended when the first state is issued to
4666 * a client and when state is revoked for a client.
4668 * When reading the file in, state issued records that come later in
4669 * the file override older ones, since the append log is in cronological order.
4670 * If, for some reason, the file can't be read, the grace period is
4671 * immediately terminated and all reclaims get NFSERR_NOGRACE.
4675 * Read in the stable storage file. Called by nfssvc() before the nfsd
4676 * processes start servicing requests.
4679 nfsrv_setupstable(NFSPROC_T *p)
4681 struct nfsrv_stablefirst *sf = &nfsrv_stablefirst;
4682 struct nfsrv_stable *sp, *nsp;
4683 struct nfst_rec *tsp;
4684 int error, i, tryagain;
4686 ssize_t aresid, len;
4689 * If NFSNSF_UPDATEDONE is set, this is a restart of the nfsds without
4690 * a reboot, so state has not been lost.
4692 if (sf->nsf_flags & NFSNSF_UPDATEDONE)
4695 * Set Grace over just until the file reads successfully.
4697 nfsrvboottime = time_second;
4698 LIST_INIT(&sf->nsf_head);
4699 sf->nsf_flags = (NFSNSF_GRACEOVER | NFSNSF_NEEDLOCK);
4700 sf->nsf_eograce = NFSD_MONOSEC + NFSRV_LEASEDELTA;
4701 if (sf->nsf_fp == NULL)
4703 error = NFSD_RDWR(UIO_READ, NFSFPVNODE(sf->nsf_fp),
4704 (caddr_t)&sf->nsf_rec, sizeof (struct nfsf_rec), off, UIO_SYSSPACE,
4705 0, NFSFPCRED(sf->nsf_fp), &aresid, p);
4706 if (error || aresid || sf->nsf_numboots == 0 ||
4707 sf->nsf_numboots > NFSNSF_MAXNUMBOOTS)
4711 * Now, read in the boottimes.
4713 sf->nsf_bootvals = (time_t *)malloc((sf->nsf_numboots + 1) *
4714 sizeof (time_t), M_TEMP, M_WAITOK);
4715 off = sizeof (struct nfsf_rec);
4716 error = NFSD_RDWR(UIO_READ, NFSFPVNODE(sf->nsf_fp),
4717 (caddr_t)sf->nsf_bootvals, sf->nsf_numboots * sizeof (time_t), off,
4718 UIO_SYSSPACE, 0, NFSFPCRED(sf->nsf_fp), &aresid, p);
4719 if (error || aresid) {
4720 free((caddr_t)sf->nsf_bootvals, M_TEMP);
4721 sf->nsf_bootvals = NULL;
4726 * Make sure this nfsrvboottime is different from all recorded
4731 for (i = 0; i < sf->nsf_numboots; i++) {
4732 if (nfsrvboottime == sf->nsf_bootvals[i]) {
4740 sf->nsf_flags |= NFSNSF_OK;
4741 off += (sf->nsf_numboots * sizeof (time_t));
4744 * Read through the file, building a list of records for grace
4746 * Each record is between sizeof (struct nfst_rec) and
4747 * sizeof (struct nfst_rec) + NFSV4_OPAQUELIMIT - 1
4748 * and is actually sizeof (struct nfst_rec) + nst_len - 1.
4750 tsp = (struct nfst_rec *)malloc(sizeof (struct nfst_rec) +
4751 NFSV4_OPAQUELIMIT - 1, M_TEMP, M_WAITOK);
4753 error = NFSD_RDWR(UIO_READ, NFSFPVNODE(sf->nsf_fp),
4754 (caddr_t)tsp, sizeof (struct nfst_rec) + NFSV4_OPAQUELIMIT - 1,
4755 off, UIO_SYSSPACE, 0, NFSFPCRED(sf->nsf_fp), &aresid, p);
4756 len = (sizeof (struct nfst_rec) + NFSV4_OPAQUELIMIT - 1) - aresid;
4757 if (error || (len > 0 && (len < sizeof (struct nfst_rec) ||
4758 len < (sizeof (struct nfst_rec) + tsp->len - 1)))) {
4760 * Yuck, the file has been corrupted, so just return
4761 * after clearing out any restart state, so the grace period
4764 LIST_FOREACH_SAFE(sp, &sf->nsf_head, nst_list, nsp) {
4765 LIST_REMOVE(sp, nst_list);
4766 free((caddr_t)sp, M_TEMP);
4768 free((caddr_t)tsp, M_TEMP);
4769 sf->nsf_flags &= ~NFSNSF_OK;
4770 free((caddr_t)sf->nsf_bootvals, M_TEMP);
4771 sf->nsf_bootvals = NULL;
4775 off += sizeof (struct nfst_rec) + tsp->len - 1;
4777 * Search the list for a matching client.
4779 LIST_FOREACH(sp, &sf->nsf_head, nst_list) {
4780 if (tsp->len == sp->nst_len &&
4781 !NFSBCMP(tsp->client, sp->nst_client, tsp->len))
4784 if (sp == LIST_END(&sf->nsf_head)) {
4785 sp = (struct nfsrv_stable *)malloc(tsp->len +
4786 sizeof (struct nfsrv_stable) - 1, M_TEMP,
4788 NFSBCOPY((caddr_t)tsp, (caddr_t)&sp->nst_rec,
4789 sizeof (struct nfst_rec) + tsp->len - 1);
4790 LIST_INSERT_HEAD(&sf->nsf_head, sp, nst_list);
4792 if (tsp->flag == NFSNST_REVOKE)
4793 sp->nst_flag |= NFSNST_REVOKE;
4796 * A subsequent timestamp indicates the client
4797 * did a setclientid/confirm and any previous
4798 * revoke is no longer relevant.
4800 sp->nst_flag &= ~NFSNST_REVOKE;
4804 free((caddr_t)tsp, M_TEMP);
4805 sf->nsf_flags = NFSNSF_OK;
4806 sf->nsf_eograce = NFSD_MONOSEC + sf->nsf_lease +
4811 * Update the stable storage file, now that the grace period is over.
4814 nfsrv_updatestable(NFSPROC_T *p)
4816 struct nfsrv_stablefirst *sf = &nfsrv_stablefirst;
4817 struct nfsrv_stable *sp, *nsp;
4819 struct nfsvattr nva;
4821 #if defined(__FreeBSD_version) && (__FreeBSD_version >= 500000)
4826 if (sf->nsf_fp == NULL || (sf->nsf_flags & NFSNSF_UPDATEDONE))
4828 sf->nsf_flags |= NFSNSF_UPDATEDONE;
4830 * Ok, we need to rewrite the stable storage file.
4831 * - truncate to 0 length
4832 * - write the new first structure
4833 * - loop through the data structures, writing out any that
4834 * have timestamps older than the old boot
4836 if (sf->nsf_bootvals) {
4838 for (i = sf->nsf_numboots - 2; i >= 0; i--)
4839 sf->nsf_bootvals[i + 1] = sf->nsf_bootvals[i];
4841 sf->nsf_numboots = 1;
4842 sf->nsf_bootvals = (time_t *)malloc(sizeof (time_t),
4845 sf->nsf_bootvals[0] = nfsrvboottime;
4846 sf->nsf_lease = nfsrv_lease;
4847 NFSVNO_ATTRINIT(&nva);
4848 NFSVNO_SETATTRVAL(&nva, size, 0);
4849 vp = NFSFPVNODE(sf->nsf_fp);
4850 vn_start_write(vp, &mp, V_WAIT);
4851 if (NFSVOPLOCK(vp, LK_EXCLUSIVE) == 0) {
4852 error = nfsvno_setattr(vp, &nva, NFSFPCRED(sf->nsf_fp), p,
4854 NFSVOPUNLOCK(vp, 0);
4857 vn_finished_write(mp);
4859 error = NFSD_RDWR(UIO_WRITE, vp,
4860 (caddr_t)&sf->nsf_rec, sizeof (struct nfsf_rec), (off_t)0,
4861 UIO_SYSSPACE, IO_SYNC, NFSFPCRED(sf->nsf_fp), NULL, p);
4863 error = NFSD_RDWR(UIO_WRITE, vp,
4864 (caddr_t)sf->nsf_bootvals,
4865 sf->nsf_numboots * sizeof (time_t),
4866 (off_t)(sizeof (struct nfsf_rec)),
4867 UIO_SYSSPACE, IO_SYNC, NFSFPCRED(sf->nsf_fp), NULL, p);
4868 free((caddr_t)sf->nsf_bootvals, M_TEMP);
4869 sf->nsf_bootvals = NULL;
4871 sf->nsf_flags &= ~NFSNSF_OK;
4872 printf("EEK! Can't write NfsV4 stable storage file\n");
4875 sf->nsf_flags |= NFSNSF_OK;
4878 * Loop through the list and write out timestamp records for
4879 * any clients that successfully reclaimed state.
4881 LIST_FOREACH_SAFE(sp, &sf->nsf_head, nst_list, nsp) {
4882 if (sp->nst_flag & NFSNST_GOTSTATE) {
4883 nfsrv_writestable(sp->nst_client, sp->nst_len,
4884 NFSNST_NEWSTATE, p);
4885 sp->nst_clp->lc_flags |= LCL_STAMPEDSTABLE;
4887 LIST_REMOVE(sp, nst_list);
4888 free((caddr_t)sp, M_TEMP);
4890 nfsrv_backupstable();
4894 * Append a record to the stable storage file.
4897 nfsrv_writestable(u_char *client, int len, int flag, NFSPROC_T *p)
4899 struct nfsrv_stablefirst *sf = &nfsrv_stablefirst;
4900 struct nfst_rec *sp;
4903 if (!(sf->nsf_flags & NFSNSF_OK) || sf->nsf_fp == NULL)
4905 sp = (struct nfst_rec *)malloc(sizeof (struct nfst_rec) +
4906 len - 1, M_TEMP, M_WAITOK);
4908 NFSBCOPY(client, sp->client, len);
4910 error = NFSD_RDWR(UIO_WRITE, NFSFPVNODE(sf->nsf_fp),
4911 (caddr_t)sp, sizeof (struct nfst_rec) + len - 1, (off_t)0,
4912 UIO_SYSSPACE, (IO_SYNC | IO_APPEND), NFSFPCRED(sf->nsf_fp), NULL, p);
4913 free((caddr_t)sp, M_TEMP);
4915 sf->nsf_flags &= ~NFSNSF_OK;
4916 printf("EEK! Can't write NfsV4 stable storage file\n");
4921 * This function is called during the grace period to mark a client
4922 * that successfully reclaimed state.
4925 nfsrv_markstable(struct nfsclient *clp)
4927 struct nfsrv_stable *sp;
4930 * First find the client structure.
4932 LIST_FOREACH(sp, &nfsrv_stablefirst.nsf_head, nst_list) {
4933 if (sp->nst_len == clp->lc_idlen &&
4934 !NFSBCMP(sp->nst_client, clp->lc_id, sp->nst_len))
4937 if (sp == LIST_END(&nfsrv_stablefirst.nsf_head))
4941 * Now, just mark it and set the nfsclient back pointer.
4943 sp->nst_flag |= NFSNST_GOTSTATE;
4948 * This function is called when a NFSv4.1 client does a ReclaimComplete.
4949 * Very similar to nfsrv_markstable(), except for the flag being set.
4952 nfsrv_markreclaim(struct nfsclient *clp)
4954 struct nfsrv_stable *sp;
4957 * First find the client structure.
4959 LIST_FOREACH(sp, &nfsrv_stablefirst.nsf_head, nst_list) {
4960 if (sp->nst_len == clp->lc_idlen &&
4961 !NFSBCMP(sp->nst_client, clp->lc_id, sp->nst_len))
4964 if (sp == LIST_END(&nfsrv_stablefirst.nsf_head))
4968 * Now, just set the flag.
4970 sp->nst_flag |= NFSNST_RECLAIMED;
4974 * This function is called for a reclaim, to see if it gets grace.
4975 * It returns 0 if a reclaim is allowed, 1 otherwise.
4978 nfsrv_checkstable(struct nfsclient *clp)
4980 struct nfsrv_stable *sp;
4983 * First, find the entry for the client.
4985 LIST_FOREACH(sp, &nfsrv_stablefirst.nsf_head, nst_list) {
4986 if (sp->nst_len == clp->lc_idlen &&
4987 !NFSBCMP(sp->nst_client, clp->lc_id, sp->nst_len))
4992 * If not in the list, state was revoked or no state was issued
4993 * since the previous reboot, a reclaim is denied.
4995 if (sp == LIST_END(&nfsrv_stablefirst.nsf_head) ||
4996 (sp->nst_flag & NFSNST_REVOKE) ||
4997 !(nfsrv_stablefirst.nsf_flags & NFSNSF_OK))
5003 * Test for and try to clear out a conflicting client. This is called by
5004 * nfsrv_lockctrl() and nfsrv_openctrl() when conflicts with other clients
5006 * The trick here is that it can't revoke a conflicting client with an
5007 * expired lease unless it holds the v4root lock, so...
5008 * If no v4root lock, get the lock and return 1 to indicate "try again".
5009 * Return 0 to indicate the conflict can't be revoked and 1 to indicate
5010 * the revocation worked and the conflicting client is "bye, bye", so it
5011 * can be tried again.
5012 * Return 2 to indicate that the vnode is VI_DOOMED after NFSVOPLOCK().
5013 * Unlocks State before a non-zero value is returned.
5016 nfsrv_clientconflict(struct nfsclient *clp, int *haslockp, vnode_t vp,
5019 int gotlock, lktype = 0;
5022 * If lease hasn't expired, we can't fix it.
5024 if (clp->lc_expiry >= NFSD_MONOSEC ||
5025 !(nfsrv_stablefirst.nsf_flags & NFSNSF_UPDATEDONE))
5027 if (*haslockp == 0) {
5030 lktype = NFSVOPISLOCKED(vp);
5031 NFSVOPUNLOCK(vp, 0);
5033 NFSLOCKV4ROOTMUTEX();
5034 nfsv4_relref(&nfsv4rootfs_lock);
5036 gotlock = nfsv4_lock(&nfsv4rootfs_lock, 1, NULL,
5037 NFSV4ROOTLOCKMUTEXPTR, NULL);
5039 NFSUNLOCKV4ROOTMUTEX();
5042 NFSVOPLOCK(vp, lktype | LK_RETRY);
5043 if ((vp->v_iflag & VI_DOOMED) != 0)
5051 * Ok, we can expire the conflicting client.
5053 nfsrv_writestable(clp->lc_id, clp->lc_idlen, NFSNST_REVOKE, p);
5054 nfsrv_backupstable();
5055 nfsrv_cleanclient(clp, p);
5056 nfsrv_freedeleglist(&clp->lc_deleg);
5057 nfsrv_freedeleglist(&clp->lc_olddeleg);
5058 LIST_REMOVE(clp, lc_hash);
5059 nfsrv_zapclient(clp, p);
5064 * Resolve a delegation conflict.
5065 * Returns 0 to indicate the conflict was resolved without sleeping.
5066 * Return -1 to indicate that the caller should check for conflicts again.
5067 * Return > 0 for an error that should be returned, normally NFSERR_DELAY.
5069 * Also, manipulate the nfsv4root_lock, as required. It isn't changed
5070 * for a return of 0, since there was no sleep and it could be required
5071 * later. It is released for a return of NFSERR_DELAY, since the caller
5072 * will return that error. It is released when a sleep was done waiting
5073 * for the delegation to be returned or expire (so that other nfsds can
5074 * handle ops). Then, it must be acquired for the write to stable storage.
5075 * (This function is somewhat similar to nfsrv_clientconflict(), but
5076 * the semantics differ in a couple of subtle ways. The return of 0
5077 * indicates the conflict was resolved without sleeping here, not
5078 * that the conflict can't be resolved and the handling of nfsv4root_lock
5079 * differs, as noted above.)
5080 * Unlocks State before returning a non-zero value.
5083 nfsrv_delegconflict(struct nfsstate *stp, int *haslockp, NFSPROC_T *p,
5086 struct nfsclient *clp = stp->ls_clp;
5087 int gotlock, error, lktype = 0, retrycnt, zapped_clp;
5088 nfsv4stateid_t tstateid;
5092 * If the conflict is with an old delegation...
5094 if (stp->ls_flags & NFSLCK_OLDDELEG) {
5096 * You can delete it, if it has expired.
5098 if (clp->lc_delegtime < NFSD_MONOSEC) {
5099 nfsrv_freedeleg(stp);
5106 * During this delay, the old delegation could expire or it
5107 * could be recovered by the client via an Open with
5108 * CLAIM_DELEGATE_PREV.
5109 * Release the nfsv4root_lock, if held.
5113 NFSLOCKV4ROOTMUTEX();
5114 nfsv4_unlock(&nfsv4rootfs_lock, 1);
5115 NFSUNLOCKV4ROOTMUTEX();
5117 error = NFSERR_DELAY;
5122 * It's a current delegation, so:
5123 * - check to see if the delegation has expired
5124 * - if so, get the v4root lock and then expire it
5126 if (!(stp->ls_flags & NFSLCK_DELEGRECALL)) {
5128 * - do a recall callback, since not yet done
5129 * For now, never allow truncate to be set. To use
5130 * truncate safely, it must be guaranteed that the
5131 * Remove, Rename or Setattr with size of 0 will
5132 * succeed and that would require major changes to
5133 * the VFS/Vnode OPs.
5134 * Set the expiry time large enough so that it won't expire
5135 * until after the callback, then set it correctly, once
5136 * the callback is done. (The delegation will now time
5137 * out whether or not the Recall worked ok. The timeout
5138 * will be extended when ops are done on the delegation
5139 * stateid, up to the timelimit.)
5141 stp->ls_delegtime = NFSD_MONOSEC + (2 * nfsrv_lease) +
5143 stp->ls_delegtimelimit = NFSD_MONOSEC + (6 * nfsrv_lease) +
5145 stp->ls_flags |= NFSLCK_DELEGRECALL;
5148 * Loop NFSRV_CBRETRYCNT times while the CBRecall replies
5149 * NFSERR_BADSTATEID or NFSERR_BADHANDLE. This is done
5150 * in order to try and avoid a race that could happen
5151 * when a CBRecall request passed the Open reply with
5152 * the delegation in it when transitting the network.
5153 * Since nfsrv_docallback will sleep, don't use stp after
5156 NFSBCOPY((caddr_t)&stp->ls_stateid, (caddr_t)&tstateid,
5158 NFSBCOPY((caddr_t)&stp->ls_lfp->lf_fh, (caddr_t)&tfh,
5163 NFSLOCKV4ROOTMUTEX();
5164 nfsv4_unlock(&nfsv4rootfs_lock, 1);
5165 NFSUNLOCKV4ROOTMUTEX();
5169 error = nfsrv_docallback(clp, NFSV4OP_CBRECALL,
5170 &tstateid, 0, &tfh, NULL, NULL, p);
5172 } while ((error == NFSERR_BADSTATEID ||
5173 error == NFSERR_BADHANDLE) && retrycnt < NFSV4_CBRETRYCNT);
5174 error = NFSERR_DELAY;
5178 if (clp->lc_expiry >= NFSD_MONOSEC &&
5179 stp->ls_delegtime >= NFSD_MONOSEC) {
5182 * A recall has been done, but it has not yet expired.
5187 NFSLOCKV4ROOTMUTEX();
5188 nfsv4_unlock(&nfsv4rootfs_lock, 1);
5189 NFSUNLOCKV4ROOTMUTEX();
5191 error = NFSERR_DELAY;
5196 * If we don't yet have the lock, just get it and then return,
5197 * since we need that before deleting expired state, such as
5199 * When getting the lock, unlock the vnode, so other nfsds that
5200 * are in progress, won't get stuck waiting for the vnode lock.
5202 if (*haslockp == 0) {
5205 lktype = NFSVOPISLOCKED(vp);
5206 NFSVOPUNLOCK(vp, 0);
5208 NFSLOCKV4ROOTMUTEX();
5209 nfsv4_relref(&nfsv4rootfs_lock);
5211 gotlock = nfsv4_lock(&nfsv4rootfs_lock, 1, NULL,
5212 NFSV4ROOTLOCKMUTEXPTR, NULL);
5214 NFSUNLOCKV4ROOTMUTEX();
5217 NFSVOPLOCK(vp, lktype | LK_RETRY);
5218 if ((vp->v_iflag & VI_DOOMED) != 0) {
5220 NFSLOCKV4ROOTMUTEX();
5221 nfsv4_unlock(&nfsv4rootfs_lock, 1);
5222 NFSUNLOCKV4ROOTMUTEX();
5223 error = NFSERR_PERM;
5233 * Ok, we can delete the expired delegation.
5234 * First, write the Revoke record to stable storage and then
5235 * clear out the conflict.
5236 * Since all other nfsd threads are now blocked, we can safely
5237 * sleep without the state changing.
5239 nfsrv_writestable(clp->lc_id, clp->lc_idlen, NFSNST_REVOKE, p);
5240 nfsrv_backupstable();
5241 if (clp->lc_expiry < NFSD_MONOSEC) {
5242 nfsrv_cleanclient(clp, p);
5243 nfsrv_freedeleglist(&clp->lc_deleg);
5244 nfsrv_freedeleglist(&clp->lc_olddeleg);
5245 LIST_REMOVE(clp, lc_hash);
5248 nfsrv_freedeleg(stp);
5252 nfsrv_zapclient(clp, p);
5261 * Check for a remove allowed, if remove is set to 1 and get rid of
5265 nfsrv_checkremove(vnode_t vp, int remove, NFSPROC_T *p)
5267 struct nfsstate *stp;
5268 struct nfslockfile *lfp;
5269 int error, haslock = 0;
5273 * First, get the lock file structure.
5274 * (A return of -1 means no associated state, so remove ok.)
5276 error = nfsrv_getlockfh(vp, NFSLCK_CHECK, NULL, &nfh, p);
5280 error = nfsrv_getlockfile(NFSLCK_CHECK, NULL, &lfp, &nfh, 0);
5284 NFSLOCKV4ROOTMUTEX();
5285 nfsv4_unlock(&nfsv4rootfs_lock, 1);
5286 NFSUNLOCKV4ROOTMUTEX();
5294 * Now, we must Recall any delegations.
5296 error = nfsrv_cleandeleg(vp, lfp, NULL, &haslock, p);
5299 * nfsrv_cleandeleg() unlocks state for non-zero
5305 NFSLOCKV4ROOTMUTEX();
5306 nfsv4_unlock(&nfsv4rootfs_lock, 1);
5307 NFSUNLOCKV4ROOTMUTEX();
5313 * Now, look for a conflicting open share.
5316 LIST_FOREACH(stp, &lfp->lf_open, ls_file) {
5317 if (stp->ls_flags & NFSLCK_WRITEDENY) {
5318 error = NFSERR_FILEOPEN;
5326 NFSLOCKV4ROOTMUTEX();
5327 nfsv4_unlock(&nfsv4rootfs_lock, 1);
5328 NFSUNLOCKV4ROOTMUTEX();
5337 * Clear out all delegations for the file referred to by lfp.
5338 * May return NFSERR_DELAY, if there will be a delay waiting for
5339 * delegations to expire.
5340 * Returns -1 to indicate it slept while recalling a delegation.
5341 * This function has the side effect of deleting the nfslockfile structure,
5342 * if it no longer has associated state and didn't have to sleep.
5343 * Unlocks State before a non-zero value is returned.
5346 nfsrv_cleandeleg(vnode_t vp, struct nfslockfile *lfp,
5347 struct nfsclient *clp, int *haslockp, NFSPROC_T *p)
5349 struct nfsstate *stp, *nstp;
5352 stp = LIST_FIRST(&lfp->lf_deleg);
5353 while (stp != LIST_END(&lfp->lf_deleg)) {
5354 nstp = LIST_NEXT(stp, ls_file);
5355 if (stp->ls_clp != clp) {
5356 ret = nfsrv_delegconflict(stp, haslockp, p, vp);
5359 * nfsrv_delegconflict() unlocks state
5360 * when it returns non-zero.
5373 * There are certain operations that, when being done outside of NFSv4,
5374 * require that any NFSv4 delegation for the file be recalled.
5375 * This function is to be called for those cases:
5376 * VOP_RENAME() - When a delegation is being recalled for any reason,
5377 * the client may have to do Opens against the server, using the file's
5378 * final component name. If the file has been renamed on the server,
5379 * that component name will be incorrect and the Open will fail.
5380 * VOP_REMOVE() - Theoretically, a client could Open a file after it has
5381 * been removed on the server, if there is a delegation issued to
5382 * that client for the file. I say "theoretically" since clients
5383 * normally do an Access Op before the Open and that Access Op will
5384 * fail with ESTALE. Note that NFSv2 and 3 don't even do Opens, so
5385 * they will detect the file's removal in the same manner. (There is
5386 * one case where RFC3530 allows a client to do an Open without first
5387 * doing an Access Op, which is passage of a check against the ACE
5388 * returned with a Write delegation, but current practice is to ignore
5389 * the ACE and always do an Access Op.)
5390 * Since the functions can only be called with an unlocked vnode, this
5391 * can't be done at this time.
5392 * VOP_ADVLOCK() - When a client holds a delegation, it can issue byte range
5393 * locks locally in the client, which are not visible to the server. To
5394 * deal with this, issuing of delegations for a vnode must be disabled
5395 * and all delegations for the vnode recalled. This is done via the
5396 * second function, using the VV_DISABLEDELEG vflag on the vnode.
5399 nfsd_recalldelegation(vnode_t vp, NFSPROC_T *p)
5405 * First, check to see if the server is currently running and it has
5406 * been called for a regular file when issuing delegations.
5408 if (newnfs_numnfsd == 0 || vp->v_type != VREG ||
5409 nfsrv_issuedelegs == 0)
5412 KASSERT((NFSVOPISLOCKED(vp) != LK_EXCLUSIVE), ("vp %p is locked", vp));
5414 * First, get a reference on the nfsv4rootfs_lock so that an
5415 * exclusive lock cannot be acquired by another thread.
5417 NFSLOCKV4ROOTMUTEX();
5418 nfsv4_getref(&nfsv4rootfs_lock, NULL, NFSV4ROOTLOCKMUTEXPTR, NULL);
5419 NFSUNLOCKV4ROOTMUTEX();
5422 * Now, call nfsrv_checkremove() in a loop while it returns
5423 * NFSERR_DELAY. Return upon any other error or when timed out.
5425 starttime = NFSD_MONOSEC;
5427 if (NFSVOPLOCK(vp, LK_EXCLUSIVE) == 0) {
5428 error = nfsrv_checkremove(vp, 0, p);
5429 NFSVOPUNLOCK(vp, 0);
5432 if (error == NFSERR_DELAY) {
5433 if (NFSD_MONOSEC - starttime > NFS_REMOVETIMEO)
5435 /* Sleep for a short period of time */
5436 (void) nfs_catnap(PZERO, 0, "nfsremove");
5438 } while (error == NFSERR_DELAY);
5439 NFSLOCKV4ROOTMUTEX();
5440 nfsv4_relref(&nfsv4rootfs_lock);
5441 NFSUNLOCKV4ROOTMUTEX();
5445 nfsd_disabledelegation(vnode_t vp, NFSPROC_T *p)
5448 #ifdef VV_DISABLEDELEG
5450 * First, flag issuance of delegations disabled.
5452 atomic_set_long(&vp->v_vflag, VV_DISABLEDELEG);
5456 * Then call nfsd_recalldelegation() to get rid of all extant
5459 nfsd_recalldelegation(vp, p);
5463 * Check for conflicting locks, etc. and then get rid of delegations.
5464 * (At one point I thought that I should get rid of delegations for any
5465 * Setattr, since it could potentially disallow the I/O op (read or write)
5466 * allowed by the delegation. However, Setattr Ops that aren't changing
5467 * the size get a stateid of all 0s, so you can't tell if it is a delegation
5468 * for the same client or a different one, so I decided to only get rid
5469 * of delegations for other clients when the size is being changed.)
5470 * In general, a Setattr can disable NFS I/O Ops that are outstanding, such
5471 * as Write backs, even if there is no delegation, so it really isn't any
5475 nfsrv_checksetattr(vnode_t vp, struct nfsrv_descript *nd,
5476 nfsv4stateid_t *stateidp, struct nfsvattr *nvap, nfsattrbit_t *attrbitp,
5477 struct nfsexstuff *exp, NFSPROC_T *p)
5479 struct nfsstate st, *stp = &st;
5480 struct nfslock lo, *lop = &lo;
5484 if (NFSISSET_ATTRBIT(attrbitp, NFSATTRBIT_SIZE)) {
5485 stp->ls_flags = (NFSLCK_CHECK | NFSLCK_WRITEACCESS);
5486 lop->lo_first = nvap->na_size;
5491 if (NFSISSET_ATTRBIT(attrbitp, NFSATTRBIT_OWNER) ||
5492 NFSISSET_ATTRBIT(attrbitp, NFSATTRBIT_OWNERGROUP) ||
5493 NFSISSET_ATTRBIT(attrbitp, NFSATTRBIT_MODE) ||
5494 NFSISSET_ATTRBIT(attrbitp, NFSATTRBIT_ACL))
5495 stp->ls_flags |= NFSLCK_SETATTR;
5496 if (stp->ls_flags == 0)
5498 lop->lo_end = NFS64BITSSET;
5499 lop->lo_flags = NFSLCK_WRITE;
5500 stp->ls_ownerlen = 0;
5502 stp->ls_uid = nd->nd_cred->cr_uid;
5503 stp->ls_stateid.seqid = stateidp->seqid;
5504 clientid.lval[0] = stp->ls_stateid.other[0] = stateidp->other[0];
5505 clientid.lval[1] = stp->ls_stateid.other[1] = stateidp->other[1];
5506 stp->ls_stateid.other[2] = stateidp->other[2];
5507 error = nfsrv_lockctrl(vp, &stp, &lop, NULL, clientid,
5508 stateidp, exp, nd, p);
5511 NFSEXITCODE2(error, nd);
5516 * Check for a write delegation and do a CBGETATTR if there is one, updating
5517 * the attributes, as required.
5518 * Should I return an error if I can't get the attributes? (For now, I'll
5522 nfsrv_checkgetattr(struct nfsrv_descript *nd, vnode_t vp,
5523 struct nfsvattr *nvap, nfsattrbit_t *attrbitp, struct ucred *cred,
5526 struct nfsstate *stp;
5527 struct nfslockfile *lfp;
5528 struct nfsclient *clp;
5529 struct nfsvattr nva;
5532 nfsattrbit_t cbbits;
5533 u_quad_t delegfilerev;
5535 NFSCBGETATTR_ATTRBIT(attrbitp, &cbbits);
5536 if (!NFSNONZERO_ATTRBIT(&cbbits))
5538 if (nfsrv_writedelegcnt == 0)
5542 * Get the lock file structure.
5543 * (A return of -1 means no associated state, so return ok.)
5545 error = nfsrv_getlockfh(vp, NFSLCK_CHECK, NULL, &nfh, p);
5548 error = nfsrv_getlockfile(NFSLCK_CHECK, NULL, &lfp, &nfh, 0);
5557 * Now, look for a write delegation.
5559 LIST_FOREACH(stp, &lfp->lf_deleg, ls_file) {
5560 if (stp->ls_flags & NFSLCK_DELEGWRITE)
5563 if (stp == LIST_END(&lfp->lf_deleg)) {
5568 delegfilerev = stp->ls_filerev;
5571 * If the Write delegation was issued as a part of this Compound RPC
5572 * or if we have an Implied Clientid (used in a previous Op in this
5573 * compound) and it is the client the delegation was issued to,
5575 * I also assume that it is from the same client iff the network
5576 * host IP address is the same as the callback address. (Not
5577 * exactly correct by the RFC, but avoids a lot of Getattr
5580 if (nd->nd_compref == stp->ls_compref ||
5581 ((nd->nd_flag & ND_IMPLIEDCLID) &&
5582 clp->lc_clientid.qval == nd->nd_clientid.qval) ||
5583 nfsaddr2_match(clp->lc_req.nr_nam, nd->nd_nam)) {
5589 * We are now done with the delegation state structure,
5590 * so the statelock can be released and we can now tsleep().
5594 * Now, we must do the CB Getattr callback, to see if Change or Size
5597 if (clp->lc_expiry >= NFSD_MONOSEC) {
5599 NFSVNO_ATTRINIT(&nva);
5600 nva.na_filerev = NFS64BITSSET;
5601 error = nfsrv_docallback(clp, NFSV4OP_CBGETATTR, NULL,
5602 0, &nfh, &nva, &cbbits, p);
5604 if ((nva.na_filerev != NFS64BITSSET &&
5605 nva.na_filerev > delegfilerev) ||
5606 (NFSVNO_ISSETSIZE(&nva) &&
5607 nva.na_size != nvap->na_size)) {
5608 error = nfsvno_updfilerev(vp, nvap, cred, p);
5609 if (NFSVNO_ISSETSIZE(&nva))
5610 nvap->na_size = nva.na_size;
5613 error = 0; /* Ignore callback errors for now. */
5619 NFSEXITCODE2(error, nd);
5624 * This function looks for openowners that haven't had any opens for
5625 * a while and throws them away. Called by an nfsd when NFSNSF_NOOPENS
5629 nfsrv_throwawayopens(NFSPROC_T *p)
5631 struct nfsclient *clp, *nclp;
5632 struct nfsstate *stp, *nstp;
5636 nfsrv_stablefirst.nsf_flags &= ~NFSNSF_NOOPENS;
5638 * For each client...
5640 for (i = 0; i < nfsrv_clienthashsize; i++) {
5641 LIST_FOREACH_SAFE(clp, &nfsclienthash[i], lc_hash, nclp) {
5642 LIST_FOREACH_SAFE(stp, &clp->lc_open, ls_list, nstp) {
5643 if (LIST_EMPTY(&stp->ls_open) &&
5644 (stp->ls_noopens > NFSNOOPEN ||
5645 (nfsrv_openpluslock * 2) >
5646 nfsrv_v4statelimit))
5647 nfsrv_freeopenowner(stp, 0, p);
5655 * This function checks to see if the credentials are the same.
5656 * Returns 1 for not same, 0 otherwise.
5659 nfsrv_notsamecredname(struct nfsrv_descript *nd, struct nfsclient *clp)
5662 if (nd->nd_flag & ND_GSS) {
5663 if (!(clp->lc_flags & LCL_GSS))
5665 if (clp->lc_flags & LCL_NAME) {
5666 if (nd->nd_princlen != clp->lc_namelen ||
5667 NFSBCMP(nd->nd_principal, clp->lc_name,
5673 if (nd->nd_cred->cr_uid == clp->lc_uid)
5677 } else if (clp->lc_flags & LCL_GSS)
5680 * For AUTH_SYS, allow the same uid or root. (This is underspecified
5681 * in RFC3530, which talks about principals, but doesn't say anything
5682 * about uids for AUTH_SYS.)
5684 if (nd->nd_cred->cr_uid == clp->lc_uid || nd->nd_cred->cr_uid == 0)
5691 * Calculate the lease expiry time.
5694 nfsrv_leaseexpiry(void)
5697 if (nfsrv_stablefirst.nsf_eograce > NFSD_MONOSEC)
5698 return (NFSD_MONOSEC + 2 * (nfsrv_lease + NFSRV_LEASEDELTA));
5699 return (NFSD_MONOSEC + nfsrv_lease + NFSRV_LEASEDELTA);
5703 * Delay the delegation timeout as far as ls_delegtimelimit, as required.
5706 nfsrv_delaydelegtimeout(struct nfsstate *stp)
5709 if ((stp->ls_flags & NFSLCK_DELEGRECALL) == 0)
5712 if ((stp->ls_delegtime + 15) > NFSD_MONOSEC &&
5713 stp->ls_delegtime < stp->ls_delegtimelimit) {
5714 stp->ls_delegtime += nfsrv_lease;
5715 if (stp->ls_delegtime > stp->ls_delegtimelimit)
5716 stp->ls_delegtime = stp->ls_delegtimelimit;
5721 * This function checks to see if there is any other state associated
5722 * with the openowner for this Open.
5723 * It returns 1 if there is no other state, 0 otherwise.
5726 nfsrv_nootherstate(struct nfsstate *stp)
5728 struct nfsstate *tstp;
5730 LIST_FOREACH(tstp, &stp->ls_openowner->ls_open, ls_list) {
5731 if (tstp != stp || !LIST_EMPTY(&tstp->ls_lock))
5738 * Create a list of lock deltas (changes to local byte range locking
5739 * that can be rolled back using the list) and apply the changes via
5740 * nfsvno_advlock(). Optionally, lock the list. It is expected that either
5741 * the rollback or update function will be called after this.
5742 * It returns an error (and rolls back, as required), if any nfsvno_advlock()
5743 * call fails. If it returns an error, it will unlock the list.
5746 nfsrv_locallock(vnode_t vp, struct nfslockfile *lfp, int flags,
5747 uint64_t first, uint64_t end, struct nfslockconflict *cfp, NFSPROC_T *p)
5749 struct nfslock *lop, *nlop;
5752 /* Loop through the list of locks. */
5753 lop = LIST_FIRST(&lfp->lf_locallock);
5754 while (first < end && lop != NULL) {
5755 nlop = LIST_NEXT(lop, lo_lckowner);
5756 if (first >= lop->lo_end) {
5759 } else if (first < lop->lo_first) {
5760 /* new one starts before entry in list */
5761 if (end <= lop->lo_first) {
5762 /* no overlap between old and new */
5763 error = nfsrv_dolocal(vp, lfp, flags,
5764 NFSLCK_UNLOCK, first, end, cfp, p);
5769 /* handle fragment overlapped with new one */
5770 error = nfsrv_dolocal(vp, lfp, flags,
5771 NFSLCK_UNLOCK, first, lop->lo_first, cfp,
5775 first = lop->lo_first;
5778 /* new one overlaps this entry in list */
5779 if (end <= lop->lo_end) {
5780 /* overlaps all of new one */
5781 error = nfsrv_dolocal(vp, lfp, flags,
5782 lop->lo_flags, first, end, cfp, p);
5787 /* handle fragment overlapped with new one */
5788 error = nfsrv_dolocal(vp, lfp, flags,
5789 lop->lo_flags, first, lop->lo_end, cfp, p);
5792 first = lop->lo_end;
5797 if (first < end && error == 0)
5798 /* handle fragment past end of list */
5799 error = nfsrv_dolocal(vp, lfp, flags, NFSLCK_UNLOCK, first,
5807 * Local lock unlock. Unlock all byte ranges that are no longer locked
5808 * by NFSv4. To do this, unlock any subranges of first-->end that
5809 * do not overlap with the byte ranges of any lock in the lfp->lf_lock
5810 * list. This list has all locks for the file held by other
5811 * <clientid, lockowner> tuples. The list is ordered by increasing
5812 * lo_first value, but may have entries that overlap each other, for
5813 * the case of read locks.
5816 nfsrv_localunlock(vnode_t vp, struct nfslockfile *lfp, uint64_t init_first,
5817 uint64_t init_end, NFSPROC_T *p)
5819 struct nfslock *lop;
5820 uint64_t first, end, prevfirst;
5824 while (first < init_end) {
5825 /* Loop through all nfs locks, adjusting first and end */
5827 LIST_FOREACH(lop, &lfp->lf_lock, lo_lckfile) {
5828 KASSERT(prevfirst <= lop->lo_first,
5829 ("nfsv4 locks out of order"));
5830 KASSERT(lop->lo_first < lop->lo_end,
5831 ("nfsv4 bogus lock"));
5832 prevfirst = lop->lo_first;
5833 if (first >= lop->lo_first &&
5834 first < lop->lo_end)
5836 * Overlaps with initial part, so trim
5837 * off that initial part by moving first past
5840 first = lop->lo_end;
5841 else if (end > lop->lo_first &&
5842 lop->lo_first > first) {
5844 * This lock defines the end of the
5845 * segment to unlock, so set end to the
5846 * start of it and break out of the loop.
5848 end = lop->lo_first;
5853 * There is no segment left to do, so
5854 * break out of this loop and then exit
5855 * the outer while() since first will be set
5856 * to end, which must equal init_end here.
5861 /* Unlock this segment */
5862 (void) nfsrv_dolocal(vp, lfp, NFSLCK_UNLOCK,
5863 NFSLCK_READ, first, end, NULL, p);
5864 nfsrv_locallock_commit(lfp, NFSLCK_UNLOCK,
5868 * Now move past this segment and look for any further
5869 * segment in the range, if there is one.
5877 * Do the local lock operation and update the rollback list, as required.
5878 * Perform the rollback and return the error if nfsvno_advlock() fails.
5881 nfsrv_dolocal(vnode_t vp, struct nfslockfile *lfp, int flags, int oldflags,
5882 uint64_t first, uint64_t end, struct nfslockconflict *cfp, NFSPROC_T *p)
5884 struct nfsrollback *rlp;
5885 int error = 0, ltype, oldltype;
5887 if (flags & NFSLCK_WRITE)
5889 else if (flags & NFSLCK_READ)
5893 if (oldflags & NFSLCK_WRITE)
5895 else if (oldflags & NFSLCK_READ)
5899 if (ltype == oldltype || (oldltype == F_WRLCK && ltype == F_RDLCK))
5902 error = nfsvno_advlock(vp, ltype, first, end, p);
5905 cfp->cl_clientid.lval[0] = 0;
5906 cfp->cl_clientid.lval[1] = 0;
5908 cfp->cl_end = NFS64BITSSET;
5909 cfp->cl_flags = NFSLCK_WRITE;
5910 cfp->cl_ownerlen = 5;
5911 NFSBCOPY("LOCAL", cfp->cl_owner, 5);
5913 nfsrv_locallock_rollback(vp, lfp, p);
5914 } else if (ltype != F_UNLCK) {
5915 rlp = malloc(sizeof (struct nfsrollback), M_NFSDROLLBACK,
5917 rlp->rlck_first = first;
5918 rlp->rlck_end = end;
5919 rlp->rlck_type = oldltype;
5920 LIST_INSERT_HEAD(&lfp->lf_rollback, rlp, rlck_list);
5929 * Roll back local lock changes and free up the rollback list.
5932 nfsrv_locallock_rollback(vnode_t vp, struct nfslockfile *lfp, NFSPROC_T *p)
5934 struct nfsrollback *rlp, *nrlp;
5936 LIST_FOREACH_SAFE(rlp, &lfp->lf_rollback, rlck_list, nrlp) {
5937 (void) nfsvno_advlock(vp, rlp->rlck_type, rlp->rlck_first,
5939 free(rlp, M_NFSDROLLBACK);
5941 LIST_INIT(&lfp->lf_rollback);
5945 * Update local lock list and delete rollback list (ie now committed to the
5946 * local locks). Most of the work is done by the internal function.
5949 nfsrv_locallock_commit(struct nfslockfile *lfp, int flags, uint64_t first,
5952 struct nfsrollback *rlp, *nrlp;
5953 struct nfslock *new_lop, *other_lop;
5955 new_lop = malloc(sizeof (struct nfslock), M_NFSDLOCK, M_WAITOK);
5956 if (flags & (NFSLCK_READ | NFSLCK_WRITE))
5957 other_lop = malloc(sizeof (struct nfslock), M_NFSDLOCK,
5961 new_lop->lo_flags = flags;
5962 new_lop->lo_first = first;
5963 new_lop->lo_end = end;
5964 nfsrv_updatelock(NULL, &new_lop, &other_lop, lfp);
5965 if (new_lop != NULL)
5966 free(new_lop, M_NFSDLOCK);
5967 if (other_lop != NULL)
5968 free(other_lop, M_NFSDLOCK);
5970 /* and get rid of the rollback list */
5971 LIST_FOREACH_SAFE(rlp, &lfp->lf_rollback, rlck_list, nrlp)
5972 free(rlp, M_NFSDROLLBACK);
5973 LIST_INIT(&lfp->lf_rollback);
5977 * Lock the struct nfslockfile for local lock updating.
5980 nfsrv_locklf(struct nfslockfile *lfp)
5984 /* lf_usecount ensures *lfp won't be free'd */
5987 gotlock = nfsv4_lock(&lfp->lf_locallock_lck, 1, NULL,
5988 NFSSTATEMUTEXPTR, NULL);
5989 } while (gotlock == 0);
5994 * Unlock the struct nfslockfile after local lock updating.
5997 nfsrv_unlocklf(struct nfslockfile *lfp)
6000 nfsv4_unlock(&lfp->lf_locallock_lck, 0);
6004 * Clear out all state for the NFSv4 server.
6005 * Must be called by a thread that can sleep when no nfsds are running.
6008 nfsrv_throwawayallstate(NFSPROC_T *p)
6010 struct nfsclient *clp, *nclp;
6011 struct nfslockfile *lfp, *nlfp;
6015 * For each client, clean out the state and then free the structure.
6017 for (i = 0; i < nfsrv_clienthashsize; i++) {
6018 LIST_FOREACH_SAFE(clp, &nfsclienthash[i], lc_hash, nclp) {
6019 nfsrv_cleanclient(clp, p);
6020 nfsrv_freedeleglist(&clp->lc_deleg);
6021 nfsrv_freedeleglist(&clp->lc_olddeleg);
6022 free(clp->lc_stateid, M_NFSDCLIENT);
6023 free(clp, M_NFSDCLIENT);
6028 * Also, free up any remaining lock file structures.
6030 for (i = 0; i < nfsrv_lockhashsize; i++) {
6031 LIST_FOREACH_SAFE(lfp, &nfslockhash[i], lf_hash, nlfp) {
6032 printf("nfsd unload: fnd a lock file struct\n");
6033 nfsrv_freenfslockfile(lfp);
6039 * Check the sequence# for the session and slot provided as an argument.
6040 * Also, renew the lease if the session will return NFS_OK.
6043 nfsrv_checksequence(struct nfsrv_descript *nd, uint32_t sequenceid,
6044 uint32_t *highest_slotidp, uint32_t *target_highest_slotidp, int cache_this,
6045 uint32_t *sflagsp, NFSPROC_T *p)
6047 struct nfsdsession *sep;
6048 struct nfssessionhash *shp;
6052 shp = NFSSESSIONHASH(nd->nd_sessionid);
6053 NFSLOCKSESSION(shp);
6054 sep = nfsrv_findsession(nd->nd_sessionid);
6056 NFSUNLOCKSESSION(shp);
6057 return (NFSERR_BADSESSION);
6059 error = nfsv4_seqsession(sequenceid, nd->nd_slotid, *highest_slotidp,
6060 sep->sess_slots, NULL, NFSV4_SLOTS - 1);
6062 NFSUNLOCKSESSION(shp);
6065 if (cache_this != 0)
6066 nd->nd_flag |= ND_SAVEREPLY;
6067 /* Renew the lease. */
6068 sep->sess_clp->lc_expiry = nfsrv_leaseexpiry();
6069 nd->nd_clientid.qval = sep->sess_clp->lc_clientid.qval;
6070 nd->nd_flag |= ND_IMPLIEDCLID;
6073 * If this session handles the backchannel, save the nd_xprt for this
6074 * RPC, since this is the one being used.
6075 * RFC-5661 specifies that the fore channel will be implicitly
6076 * bound by a Sequence operation. However, since some NFSv4.1 clients
6077 * erroneously assumed that the back channel would be implicitly
6078 * bound as well, do the implicit binding unless a
6079 * BindConnectiontoSession has already been done on the session.
6081 if (sep->sess_clp->lc_req.nr_client != NULL &&
6082 sep->sess_cbsess.nfsess_xprt != nd->nd_xprt &&
6083 (sep->sess_crflags & NFSV4CRSESS_CONNBACKCHAN) != 0 &&
6084 (sep->sess_clp->lc_flags & LCL_DONEBINDCONN) == 0) {
6086 "nfsrv_checksequence: implicit back channel bind\n");
6087 savxprt = sep->sess_cbsess.nfsess_xprt;
6088 SVC_ACQUIRE(nd->nd_xprt);
6089 nd->nd_xprt->xp_p2 =
6090 sep->sess_clp->lc_req.nr_client->cl_private;
6091 nd->nd_xprt->xp_idletimeout = 0; /* Disable timeout. */
6092 sep->sess_cbsess.nfsess_xprt = nd->nd_xprt;
6093 if (savxprt != NULL)
6094 SVC_RELEASE(savxprt);
6098 if (sep->sess_clp->lc_req.nr_client == NULL)
6099 *sflagsp |= NFSV4SEQ_CBPATHDOWN;
6100 NFSUNLOCKSESSION(shp);
6101 if (error == NFSERR_EXPIRED) {
6102 *sflagsp |= NFSV4SEQ_EXPIREDALLSTATEREVOKED;
6104 } else if (error == NFSERR_ADMINREVOKED) {
6105 *sflagsp |= NFSV4SEQ_ADMINSTATEREVOKED;
6108 *highest_slotidp = *target_highest_slotidp = NFSV4_SLOTS - 1;
6113 * Check/set reclaim complete for this session/clientid.
6116 nfsrv_checkreclaimcomplete(struct nfsrv_descript *nd, int onefs)
6118 struct nfsdsession *sep;
6119 struct nfssessionhash *shp;
6122 shp = NFSSESSIONHASH(nd->nd_sessionid);
6124 NFSLOCKSESSION(shp);
6125 sep = nfsrv_findsession(nd->nd_sessionid);
6127 NFSUNLOCKSESSION(shp);
6129 return (NFSERR_BADSESSION);
6133 sep->sess_clp->lc_flags |= LCL_RECLAIMONEFS;
6134 /* Check to see if reclaim complete has already happened. */
6135 else if ((sep->sess_clp->lc_flags & LCL_RECLAIMCOMPLETE) != 0)
6136 error = NFSERR_COMPLETEALREADY;
6138 sep->sess_clp->lc_flags |= LCL_RECLAIMCOMPLETE;
6139 nfsrv_markreclaim(sep->sess_clp);
6141 NFSUNLOCKSESSION(shp);
6147 * Cache the reply in a session slot.
6150 nfsrv_cache_session(uint8_t *sessionid, uint32_t slotid, int repstat,
6153 struct nfsdsession *sep;
6154 struct nfssessionhash *shp;
6156 shp = NFSSESSIONHASH(sessionid);
6157 NFSLOCKSESSION(shp);
6158 sep = nfsrv_findsession(sessionid);
6160 NFSUNLOCKSESSION(shp);
6161 printf("nfsrv_cache_session: no session\n");
6165 nfsv4_seqsess_cacherep(slotid, sep->sess_slots, repstat, m);
6166 NFSUNLOCKSESSION(shp);
6170 * Search for a session that matches the sessionid.
6172 static struct nfsdsession *
6173 nfsrv_findsession(uint8_t *sessionid)
6175 struct nfsdsession *sep;
6176 struct nfssessionhash *shp;
6178 shp = NFSSESSIONHASH(sessionid);
6179 LIST_FOREACH(sep, &shp->list, sess_hash) {
6180 if (!NFSBCMP(sessionid, sep->sess_sessionid, NFSX_V4SESSIONID))
6187 * Destroy a session.
6190 nfsrv_destroysession(struct nfsrv_descript *nd, uint8_t *sessionid)
6192 int error, igotlock, samesess;
6195 if (!NFSBCMP(sessionid, nd->nd_sessionid, NFSX_V4SESSIONID) &&
6196 (nd->nd_flag & ND_HASSEQUENCE) != 0) {
6198 if ((nd->nd_flag & ND_LASTOP) == 0)
6199 return (NFSERR_BADSESSION);
6202 /* Lock out other nfsd threads */
6203 NFSLOCKV4ROOTMUTEX();
6204 nfsv4_relref(&nfsv4rootfs_lock);
6206 igotlock = nfsv4_lock(&nfsv4rootfs_lock, 1, NULL,
6207 NFSV4ROOTLOCKMUTEXPTR, NULL);
6208 } while (igotlock == 0);
6209 NFSUNLOCKV4ROOTMUTEX();
6211 error = nfsrv_freesession(NULL, sessionid);
6212 if (error == 0 && samesess != 0)
6213 nd->nd_flag &= ~ND_HASSEQUENCE;
6215 NFSLOCKV4ROOTMUTEX();
6216 nfsv4_unlock(&nfsv4rootfs_lock, 1);
6217 NFSUNLOCKV4ROOTMUTEX();
6222 * Bind a connection to a session.
6223 * For now, only certain variants are supported, since the current session
6224 * structure can only handle a single backchannel entry, which will be
6225 * applied to all connections if it is set.
6228 nfsrv_bindconnsess(struct nfsrv_descript *nd, uint8_t *sessionid, int *foreaftp)
6230 struct nfssessionhash *shp;
6231 struct nfsdsession *sep;
6232 struct nfsclient *clp;
6237 shp = NFSSESSIONHASH(sessionid);
6239 NFSLOCKSESSION(shp);
6240 sep = nfsrv_findsession(sessionid);
6242 clp = sep->sess_clp;
6243 if (*foreaftp == NFSCDFC4_BACK ||
6244 *foreaftp == NFSCDFC4_BACK_OR_BOTH ||
6245 *foreaftp == NFSCDFC4_FORE_OR_BOTH) {
6246 /* Try to set up a backchannel. */
6247 if (clp->lc_req.nr_client == NULL) {
6248 NFSD_DEBUG(2, "nfsrv_bindconnsess: acquire "
6250 clp->lc_req.nr_client = (struct __rpc_client *)
6251 clnt_bck_create(nd->nd_xprt->xp_socket,
6252 sep->sess_cbprogram, NFSV4_CBVERS);
6254 if (clp->lc_req.nr_client != NULL) {
6255 NFSD_DEBUG(2, "nfsrv_bindconnsess: set up "
6257 savxprt = sep->sess_cbsess.nfsess_xprt;
6258 SVC_ACQUIRE(nd->nd_xprt);
6259 nd->nd_xprt->xp_p2 =
6260 clp->lc_req.nr_client->cl_private;
6261 /* Disable idle timeout. */
6262 nd->nd_xprt->xp_idletimeout = 0;
6263 sep->sess_cbsess.nfsess_xprt = nd->nd_xprt;
6264 if (savxprt != NULL)
6265 SVC_RELEASE(savxprt);
6266 sep->sess_crflags |= NFSV4CRSESS_CONNBACKCHAN;
6267 clp->lc_flags |= LCL_DONEBINDCONN;
6268 if (*foreaftp == NFSCDFS4_BACK)
6269 *foreaftp = NFSCDFS4_BACK;
6271 *foreaftp = NFSCDFS4_BOTH;
6272 } else if (*foreaftp != NFSCDFC4_BACK) {
6273 NFSD_DEBUG(2, "nfsrv_bindconnsess: can't set "
6274 "up backchannel\n");
6275 sep->sess_crflags &= ~NFSV4CRSESS_CONNBACKCHAN;
6276 clp->lc_flags |= LCL_DONEBINDCONN;
6277 *foreaftp = NFSCDFS4_FORE;
6279 error = NFSERR_NOTSUPP;
6280 printf("nfsrv_bindconnsess: Can't add "
6284 NFSD_DEBUG(2, "nfsrv_bindconnsess: Set forechannel\n");
6285 clp->lc_flags |= LCL_DONEBINDCONN;
6286 *foreaftp = NFSCDFS4_FORE;
6289 error = NFSERR_BADSESSION;
6290 NFSUNLOCKSESSION(shp);
6296 * Free up a session structure.
6299 nfsrv_freesession(struct nfsdsession *sep, uint8_t *sessionid)
6301 struct nfssessionhash *shp;
6306 shp = NFSSESSIONHASH(sessionid);
6307 NFSLOCKSESSION(shp);
6308 sep = nfsrv_findsession(sessionid);
6310 shp = NFSSESSIONHASH(sep->sess_sessionid);
6311 NFSLOCKSESSION(shp);
6315 if (sep->sess_refcnt > 0) {
6316 NFSUNLOCKSESSION(shp);
6318 return (NFSERR_BACKCHANBUSY);
6320 LIST_REMOVE(sep, sess_hash);
6321 LIST_REMOVE(sep, sess_list);
6323 NFSUNLOCKSESSION(shp);
6326 return (NFSERR_BADSESSION);
6327 for (i = 0; i < NFSV4_SLOTS; i++)
6328 if (sep->sess_slots[i].nfssl_reply != NULL)
6329 m_freem(sep->sess_slots[i].nfssl_reply);
6330 if (sep->sess_cbsess.nfsess_xprt != NULL)
6331 SVC_RELEASE(sep->sess_cbsess.nfsess_xprt);
6332 free(sep, M_NFSDSESSION);
6338 * RFC5661 says that it should fail when there are associated opens, locks
6339 * or delegations. Since stateids represent opens, I don't see how you can
6340 * free an open stateid (it will be free'd when closed), so this function
6341 * only works for lock stateids (freeing the lock_owner) or delegations.
6344 nfsrv_freestateid(struct nfsrv_descript *nd, nfsv4stateid_t *stateidp,
6347 struct nfsclient *clp;
6348 struct nfsstate *stp;
6353 * Look up the stateid
6355 error = nfsrv_getclient((nfsquad_t)((u_quad_t)0), CLOPS_RENEW, &clp,
6356 NULL, (nfsquad_t)((u_quad_t)0), 0, nd, p);
6358 /* First, check for a delegation. */
6359 LIST_FOREACH(stp, &clp->lc_deleg, ls_list) {
6360 if (!NFSBCMP(stp->ls_stateid.other, stateidp->other,
6365 nfsrv_freedeleg(stp);
6370 /* Not a delegation, try for a lock_owner. */
6372 error = nfsrv_getstate(clp, stateidp, 0, &stp);
6373 if (error == 0 && ((stp->ls_flags & (NFSLCK_OPEN | NFSLCK_DELEGREAD |
6374 NFSLCK_DELEGWRITE)) != 0 || (stp->ls_flags & NFSLCK_LOCK) == 0))
6375 /* Not a lock_owner stateid. */
6376 error = NFSERR_LOCKSHELD;
6377 if (error == 0 && !LIST_EMPTY(&stp->ls_lock))
6378 error = NFSERR_LOCKSHELD;
6380 nfsrv_freelockowner(stp, NULL, 0, p);
6389 nfsrv_teststateid(struct nfsrv_descript *nd, nfsv4stateid_t *stateidp,
6392 struct nfsclient *clp;
6393 struct nfsstate *stp;
6398 * Look up the stateid
6400 error = nfsrv_getclient((nfsquad_t)((u_quad_t)0), CLOPS_RENEW, &clp,
6401 NULL, (nfsquad_t)((u_quad_t)0), 0, nd, p);
6403 error = nfsrv_getstate(clp, stateidp, 0, &stp);
6404 if (error == 0 && stateidp->seqid != 0 &&
6405 SEQ_LT(stateidp->seqid, stp->ls_stateid.seqid))
6406 error = NFSERR_OLDSTATEID;
6412 * Generate the xdr for an NFSv4.1 CBSequence Operation.
6415 nfsv4_setcbsequence(struct nfsrv_descript *nd, struct nfsclient *clp,
6416 int dont_replycache, struct nfsdsession **sepp)
6418 struct nfsdsession *sep;
6419 uint32_t *tl, slotseq = 0;
6420 int maxslot, slotpos;
6421 uint8_t sessionid[NFSX_V4SESSIONID];
6424 error = nfsv4_getcbsession(clp, sepp);
6428 (void)nfsv4_sequencelookup(NULL, &sep->sess_cbsess, &slotpos, &maxslot,
6429 &slotseq, sessionid);
6430 KASSERT(maxslot >= 0, ("nfsv4_setcbsequence neg maxslot"));
6432 /* Build the Sequence arguments. */
6433 NFSM_BUILD(tl, uint32_t *, NFSX_V4SESSIONID + 5 * NFSX_UNSIGNED);
6434 bcopy(sessionid, tl, NFSX_V4SESSIONID);
6435 tl += NFSX_V4SESSIONID / NFSX_UNSIGNED;
6436 nd->nd_slotseq = tl;
6437 *tl++ = txdr_unsigned(slotseq);
6438 *tl++ = txdr_unsigned(slotpos);
6439 *tl++ = txdr_unsigned(maxslot);
6440 if (dont_replycache == 0)
6441 *tl++ = newnfs_true;
6443 *tl++ = newnfs_false;
6444 *tl = 0; /* No referring call list, for now. */
6445 nd->nd_flag |= ND_HASSEQUENCE;
6450 * Get a session for the callback.
6453 nfsv4_getcbsession(struct nfsclient *clp, struct nfsdsession **sepp)
6455 struct nfsdsession *sep;
6458 LIST_FOREACH(sep, &clp->lc_session, sess_list) {
6459 if ((sep->sess_crflags & NFSV4CRSESS_CONNBACKCHAN) != 0)
6464 return (NFSERR_BADSESSION);
6473 * Free up all backchannel xprts. This needs to be done when the nfsd threads
6474 * exit, since those transports will all be going away.
6475 * This is only called after all the nfsd threads are done performing RPCs,
6476 * so locking shouldn't be an issue.
6479 nfsrv_freeallbackchannel_xprts(void)
6481 struct nfsdsession *sep;
6482 struct nfsclient *clp;
6486 for (i = 0; i < nfsrv_clienthashsize; i++) {
6487 LIST_FOREACH(clp, &nfsclienthash[i], lc_hash) {
6488 LIST_FOREACH(sep, &clp->lc_session, sess_list) {
6489 xprt = sep->sess_cbsess.nfsess_xprt;
6490 sep->sess_cbsess.nfsess_xprt = NULL;