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);
1492 NFSVOPUNLOCK(tvp, 0);
1501 first = lop->lo_first;
1503 nfsrv_freenfslock(lop);
1504 nfsrv_localunlock(tvp, lfp, first, end, p);
1505 LIST_FOREACH_SAFE(rlp, &lfp->lf_rollback, rlck_list,
1507 free(rlp, M_NFSDROLLBACK);
1508 LIST_INIT(&lfp->lf_rollback);
1510 nfsrv_freenfslock(lop);
1513 if (vp == NULL && tvp != NULL)
1518 * Free an nfslock structure.
1521 nfsrv_freenfslock(struct nfslock *lop)
1524 if (lop->lo_lckfile.le_prev != NULL) {
1525 LIST_REMOVE(lop, lo_lckfile);
1526 newnfsstats.srvlocks--;
1527 nfsrv_openpluslock--;
1529 LIST_REMOVE(lop, lo_lckowner);
1530 FREE((caddr_t)lop, M_NFSDLOCK);
1534 * This function frees an nfslockfile structure.
1537 nfsrv_freenfslockfile(struct nfslockfile *lfp)
1540 LIST_REMOVE(lfp, lf_hash);
1541 FREE((caddr_t)lfp, M_NFSDLOCKFILE);
1545 * This function looks up an nfsstate structure via stateid.
1548 nfsrv_getstate(struct nfsclient *clp, nfsv4stateid_t *stateidp, __unused u_int32_t flags,
1549 struct nfsstate **stpp)
1551 struct nfsstate *stp;
1552 struct nfsstatehead *hp;
1556 hp = NFSSTATEHASH(clp, *stateidp);
1557 LIST_FOREACH(stp, hp, ls_hash) {
1558 if (!NFSBCMP(stp->ls_stateid.other, stateidp->other,
1564 * If no state id in list, return NFSERR_BADSTATEID.
1566 if (stp == LIST_END(hp)) {
1567 error = NFSERR_BADSTATEID;
1578 * This function gets an nfsstate structure via owner string.
1581 nfsrv_getowner(struct nfsstatehead *hp, struct nfsstate *new_stp,
1582 struct nfsstate **stpp)
1584 struct nfsstate *stp;
1587 LIST_FOREACH(stp, hp, ls_list) {
1588 if (new_stp->ls_ownerlen == stp->ls_ownerlen &&
1589 !NFSBCMP(new_stp->ls_owner,stp->ls_owner,stp->ls_ownerlen)) {
1597 * Lock control function called to update lock status.
1598 * Returns 0 upon success, -1 if there is no lock and the flags indicate
1599 * that one isn't to be created and an NFSERR_xxx for other errors.
1600 * The structures new_stp and new_lop are passed in as pointers that should
1601 * be set to NULL if the structure is used and shouldn't be free'd.
1602 * For the NFSLCK_TEST and NFSLCK_CHECK cases, the structures are
1603 * never used and can safely be allocated on the stack. For all other
1604 * cases, *new_stpp and *new_lopp should be malloc'd before the call,
1605 * in case they are used.
1608 nfsrv_lockctrl(vnode_t vp, struct nfsstate **new_stpp,
1609 struct nfslock **new_lopp, struct nfslockconflict *cfp,
1610 nfsquad_t clientid, nfsv4stateid_t *stateidp,
1611 __unused struct nfsexstuff *exp,
1612 struct nfsrv_descript *nd, NFSPROC_T *p)
1614 struct nfslock *lop;
1615 struct nfsstate *new_stp = *new_stpp;
1616 struct nfslock *new_lop = *new_lopp;
1617 struct nfsstate *tstp, *mystp, *nstp;
1619 struct nfslockfile *lfp;
1620 struct nfslock *other_lop = NULL;
1621 struct nfsstate *stp, *lckstp = NULL;
1622 struct nfsclient *clp = NULL;
1624 int error = 0, haslock = 0, ret, reterr;
1625 int getlckret, delegation = 0, filestruct_locked, vnode_unlocked = 0;
1627 uint64_t first, end;
1628 uint32_t lock_flags;
1630 if (new_stp->ls_flags & (NFSLCK_CHECK | NFSLCK_SETATTR)) {
1632 * Note the special cases of "all 1s" or "all 0s" stateids and
1633 * let reads with all 1s go ahead.
1635 if (new_stp->ls_stateid.seqid == 0x0 &&
1636 new_stp->ls_stateid.other[0] == 0x0 &&
1637 new_stp->ls_stateid.other[1] == 0x0 &&
1638 new_stp->ls_stateid.other[2] == 0x0)
1640 else if (new_stp->ls_stateid.seqid == 0xffffffff &&
1641 new_stp->ls_stateid.other[0] == 0xffffffff &&
1642 new_stp->ls_stateid.other[1] == 0xffffffff &&
1643 new_stp->ls_stateid.other[2] == 0xffffffff)
1648 * Check for restart conditions (client and server).
1650 error = nfsrv_checkrestart(clientid, new_stp->ls_flags,
1651 &new_stp->ls_stateid, specialid);
1656 * Check for state resource limit exceeded.
1658 if ((new_stp->ls_flags & NFSLCK_LOCK) &&
1659 nfsrv_openpluslock > nfsrv_v4statelimit) {
1660 error = NFSERR_RESOURCE;
1665 * For the lock case, get another nfslock structure,
1666 * just in case we need it.
1667 * Malloc now, before we start sifting through the linked lists,
1668 * in case we have to wait for memory.
1671 if (new_stp->ls_flags & NFSLCK_LOCK)
1672 MALLOC(other_lop, struct nfslock *, sizeof (struct nfslock),
1673 M_NFSDLOCK, M_WAITOK);
1674 filestruct_locked = 0;
1679 * Get the lockfile structure for CFH now, so we can do a sanity
1680 * check against the stateid, before incrementing the seqid#, since
1681 * we want to return NFSERR_BADSTATEID on failure and the seqid#
1682 * shouldn't be incremented for this case.
1683 * If nfsrv_getlockfile() returns -1, it means "not found", which
1684 * will be handled later.
1685 * If we are doing Lock/LockU and local locking is enabled, sleep
1686 * lock the nfslockfile structure.
1688 getlckret = nfsrv_getlockfh(vp, new_stp->ls_flags, NULL, &nfh, p);
1690 if (getlckret == 0) {
1691 if ((new_stp->ls_flags & (NFSLCK_LOCK | NFSLCK_UNLOCK)) != 0 &&
1692 nfsrv_dolocallocks != 0 && nd->nd_repstat == 0) {
1693 getlckret = nfsrv_getlockfile(new_stp->ls_flags, NULL,
1696 filestruct_locked = 1;
1698 getlckret = nfsrv_getlockfile(new_stp->ls_flags, NULL,
1701 if (getlckret != 0 && getlckret != -1)
1704 if (filestruct_locked != 0) {
1705 LIST_INIT(&lfp->lf_rollback);
1706 if ((new_stp->ls_flags & NFSLCK_LOCK)) {
1708 * For local locking, do the advisory locking now, so
1709 * that any conflict can be detected. A failure later
1710 * can be rolled back locally. If an error is returned,
1711 * struct nfslockfile has been unlocked and any local
1712 * locking rolled back.
1715 if (vnode_unlocked == 0) {
1716 ASSERT_VOP_ELOCKED(vp, "nfsrv_lockctrl1");
1718 NFSVOPUNLOCK(vp, 0);
1720 reterr = nfsrv_locallock(vp, lfp,
1721 (new_lop->lo_flags & (NFSLCK_READ | NFSLCK_WRITE)),
1722 new_lop->lo_first, new_lop->lo_end, cfp, p);
1727 if (specialid == 0) {
1728 if (new_stp->ls_flags & NFSLCK_TEST) {
1730 * RFC 3530 does not list LockT as an op that renews a
1731 * lease, but the concensus seems to be that it is ok
1732 * for a server to do so.
1734 error = nfsrv_getclient(clientid, CLOPS_RENEW, &clp, NULL,
1735 (nfsquad_t)((u_quad_t)0), 0, nd, p);
1738 * Since NFSERR_EXPIRED, NFSERR_ADMINREVOKED are not valid
1739 * error returns for LockT, just go ahead and test for a lock,
1740 * since there are no locks for this client, but other locks
1741 * can conflict. (ie. same client will always be false)
1743 if (error == NFSERR_EXPIRED || error == NFSERR_ADMINREVOKED)
1747 error = nfsrv_getclient(clientid, CLOPS_RENEW, &clp, NULL,
1748 (nfsquad_t)((u_quad_t)0), 0, nd, p);
1751 * Look up the stateid
1753 error = nfsrv_getstate(clp, &new_stp->ls_stateid,
1754 new_stp->ls_flags, &stp);
1756 * do some sanity checks for an unconfirmed open or a
1757 * stateid that refers to the wrong file, for an open stateid
1759 if (error == 0 && (stp->ls_flags & NFSLCK_OPEN) &&
1760 ((stp->ls_openowner->ls_flags & NFSLCK_NEEDSCONFIRM) ||
1761 (getlckret == 0 && stp->ls_lfp != lfp)))
1762 error = NFSERR_BADSTATEID;
1764 (stp->ls_flags & (NFSLCK_DELEGREAD | NFSLCK_DELEGWRITE)) &&
1765 getlckret == 0 && stp->ls_lfp != lfp)
1766 error = NFSERR_BADSTATEID;
1769 * If the lockowner stateid doesn't refer to the same file,
1770 * I believe that is considered ok, since some clients will
1771 * only create a single lockowner and use that for all locks
1773 * For now, log it as a diagnostic, instead of considering it
1776 if (error == 0 && (stp->ls_flags &
1777 (NFSLCK_OPEN | NFSLCK_DELEGREAD | NFSLCK_DELEGWRITE)) == 0 &&
1778 getlckret == 0 && stp->ls_lfp != lfp) {
1780 printf("Got a lock statid for different file open\n");
1783 error = NFSERR_BADSTATEID;
1788 if (new_stp->ls_flags & NFSLCK_OPENTOLOCK) {
1790 * If haslock set, we've already checked the seqid.
1793 if (stp->ls_flags & NFSLCK_OPEN)
1794 error = nfsrv_checkseqid(nd, new_stp->ls_seq,
1795 stp->ls_openowner, new_stp->ls_op);
1797 error = NFSERR_BADSTATEID;
1800 nfsrv_getowner(&stp->ls_open, new_stp, &lckstp);
1803 * I believe this should be an error, but it
1804 * isn't obvious what NFSERR_xxx would be
1805 * appropriate, so I'll use NFSERR_INVAL for now.
1807 error = NFSERR_INVAL;
1810 } else if (new_stp->ls_flags&(NFSLCK_LOCK|NFSLCK_UNLOCK)) {
1812 * If haslock set, ditto above.
1815 if (stp->ls_flags & NFSLCK_OPEN)
1816 error = NFSERR_BADSTATEID;
1818 error = nfsrv_checkseqid(nd, new_stp->ls_seq,
1819 stp, new_stp->ls_op);
1827 * If the seqid part of the stateid isn't the same, return
1828 * NFSERR_OLDSTATEID for cases other than I/O Ops.
1829 * For I/O Ops, only return NFSERR_OLDSTATEID if
1830 * nfsrv_returnoldstateid is set. (The concensus on the email
1831 * list was that most clients would prefer to not receive
1832 * NFSERR_OLDSTATEID for I/O Ops, but the RFC suggests that that
1833 * is what will happen, so I use the nfsrv_returnoldstateid to
1834 * allow for either server configuration.)
1836 if (!error && stp->ls_stateid.seqid!=new_stp->ls_stateid.seqid &&
1837 (((nd->nd_flag & ND_NFSV41) == 0 &&
1838 (!(new_stp->ls_flags & NFSLCK_CHECK) ||
1839 nfsrv_returnoldstateid)) ||
1840 ((nd->nd_flag & ND_NFSV41) != 0 &&
1841 new_stp->ls_stateid.seqid != 0)))
1842 error = NFSERR_OLDSTATEID;
1847 * Now we can check for grace.
1850 error = nfsrv_checkgrace(nd, clp, new_stp->ls_flags);
1851 if ((new_stp->ls_flags & NFSLCK_RECLAIM) && !error &&
1852 nfsrv_checkstable(clp))
1853 error = NFSERR_NOGRACE;
1855 * If we successfully Reclaimed state, note that.
1857 if ((new_stp->ls_flags & NFSLCK_RECLAIM) && !error)
1858 nfsrv_markstable(clp);
1861 * At this point, either error == NFSERR_BADSTATEID or the
1862 * seqid# has been updated, so we can return any error.
1863 * If error == 0, there may be an error in:
1864 * nd_repstat - Set by the calling function.
1865 * reterr - Set above, if getting the nfslockfile structure
1866 * or acquiring the local lock failed.
1867 * (If both of these are set, nd_repstat should probably be
1868 * returned, since that error was detected before this
1871 if (error != 0 || nd->nd_repstat != 0 || reterr != 0) {
1873 if (nd->nd_repstat != 0)
1874 error = nd->nd_repstat;
1878 if (filestruct_locked != 0) {
1879 /* Roll back local locks. */
1881 if (vnode_unlocked == 0) {
1882 ASSERT_VOP_ELOCKED(vp, "nfsrv_lockctrl2");
1884 NFSVOPUNLOCK(vp, 0);
1886 nfsrv_locallock_rollback(vp, lfp, p);
1888 nfsrv_unlocklf(lfp);
1895 * Check the nfsrv_getlockfile return.
1896 * Returned -1 if no structure found.
1898 if (getlckret == -1) {
1899 error = NFSERR_EXPIRED;
1901 * Called from lockt, so no lock is OK.
1903 if (new_stp->ls_flags & NFSLCK_TEST) {
1905 } else if (new_stp->ls_flags &
1906 (NFSLCK_CHECK | NFSLCK_SETATTR)) {
1908 * Called to check for a lock, OK if the stateid is all
1909 * 1s or all 0s, but there should be an nfsstate
1911 * (ie. If there is no open, I'll assume no share
1917 error = NFSERR_BADSTATEID;
1924 * For NFSLCK_CHECK and NFSLCK_LOCK, test for a share conflict.
1925 * For NFSLCK_CHECK, allow a read if write access is granted,
1926 * but check for a deny. For NFSLCK_LOCK, require correct access,
1927 * which implies a conflicting deny can't exist.
1929 if (new_stp->ls_flags & (NFSLCK_CHECK | NFSLCK_LOCK)) {
1931 * Four kinds of state id:
1932 * - specialid (all 0s or all 1s), only for NFSLCK_CHECK
1933 * - stateid for an open
1934 * - stateid for a delegation
1935 * - stateid for a lock owner
1938 if (stp->ls_flags & (NFSLCK_DELEGREAD | NFSLCK_DELEGWRITE)) {
1941 nfsrv_delaydelegtimeout(stp);
1942 } else if (stp->ls_flags & NFSLCK_OPEN) {
1945 mystp = stp->ls_openstp;
1948 * If locking or checking, require correct access
1951 if (((new_stp->ls_flags & NFSLCK_LOCK) &&
1952 !((new_lop->lo_flags >> NFSLCK_LOCKSHIFT) &
1953 mystp->ls_flags & NFSLCK_ACCESSBITS)) ||
1954 ((new_stp->ls_flags & (NFSLCK_CHECK|NFSLCK_READACCESS)) ==
1955 (NFSLCK_CHECK | NFSLCK_READACCESS) &&
1956 !(mystp->ls_flags & NFSLCK_READACCESS) &&
1957 nfsrv_allowreadforwriteopen == 0) ||
1958 ((new_stp->ls_flags & (NFSLCK_CHECK|NFSLCK_WRITEACCESS)) ==
1959 (NFSLCK_CHECK | NFSLCK_WRITEACCESS) &&
1960 !(mystp->ls_flags & NFSLCK_WRITEACCESS))) {
1961 if (filestruct_locked != 0) {
1962 /* Roll back local locks. */
1964 if (vnode_unlocked == 0) {
1965 ASSERT_VOP_ELOCKED(vp,
1968 NFSVOPUNLOCK(vp, 0);
1970 nfsrv_locallock_rollback(vp, lfp, p);
1972 nfsrv_unlocklf(lfp);
1975 error = NFSERR_OPENMODE;
1980 if ((new_stp->ls_flags & NFSLCK_CHECK) && !delegation) {
1982 * Check for a conflicting deny bit.
1984 LIST_FOREACH(tstp, &lfp->lf_open, ls_file) {
1985 if (tstp != mystp) {
1986 bits = tstp->ls_flags;
1987 bits >>= NFSLCK_SHIFT;
1988 if (new_stp->ls_flags & bits & NFSLCK_ACCESSBITS) {
1989 KASSERT(vnode_unlocked == 0,
1990 ("nfsrv_lockctrl: vnode unlocked1"));
1991 ret = nfsrv_clientconflict(tstp->ls_clp, &haslock,
1995 * nfsrv_clientconflict unlocks state
1996 * when it returns non-zero.
2004 error = NFSERR_PERM;
2006 error = NFSERR_OPENMODE;
2012 /* We're outta here */
2019 * For setattr, just get rid of all the Delegations for other clients.
2021 if (new_stp->ls_flags & NFSLCK_SETATTR) {
2022 KASSERT(vnode_unlocked == 0,
2023 ("nfsrv_lockctrl: vnode unlocked2"));
2024 ret = nfsrv_cleandeleg(vp, lfp, clp, &haslock, p);
2027 * nfsrv_cleandeleg() unlocks state when it
2037 if (!(new_stp->ls_flags & NFSLCK_CHECK) ||
2038 (LIST_EMPTY(&lfp->lf_open) && LIST_EMPTY(&lfp->lf_lock) &&
2039 LIST_EMPTY(&lfp->lf_deleg))) {
2046 * Check for a conflicting delegation. If one is found, call
2047 * nfsrv_delegconflict() to handle it. If the v4root lock hasn't
2048 * been set yet, it will get the lock. Otherwise, it will recall
2049 * the delegation. Then, we try try again...
2050 * I currently believe the conflict algorithm to be:
2051 * For Lock Ops (Lock/LockT/LockU)
2052 * - there is a conflict iff a different client has a write delegation
2053 * For Reading (Read Op)
2054 * - there is a conflict iff a different client has a write delegation
2055 * (the specialids are always a different client)
2056 * For Writing (Write/Setattr of size)
2057 * - there is a conflict if a different client has any delegation
2058 * - there is a conflict if the same client has a read delegation
2059 * (I don't understand why this isn't allowed, but that seems to be
2060 * the current concensus?)
2062 tstp = LIST_FIRST(&lfp->lf_deleg);
2063 while (tstp != LIST_END(&lfp->lf_deleg)) {
2064 nstp = LIST_NEXT(tstp, ls_file);
2065 if ((((new_stp->ls_flags&(NFSLCK_LOCK|NFSLCK_UNLOCK|NFSLCK_TEST))||
2066 ((new_stp->ls_flags & NFSLCK_CHECK) &&
2067 (new_lop->lo_flags & NFSLCK_READ))) &&
2068 clp != tstp->ls_clp &&
2069 (tstp->ls_flags & NFSLCK_DELEGWRITE)) ||
2070 ((new_stp->ls_flags & NFSLCK_CHECK) &&
2071 (new_lop->lo_flags & NFSLCK_WRITE) &&
2072 (clp != tstp->ls_clp ||
2073 (tstp->ls_flags & NFSLCK_DELEGREAD)))) {
2075 if (filestruct_locked != 0) {
2076 /* Roll back local locks. */
2078 if (vnode_unlocked == 0) {
2079 ASSERT_VOP_ELOCKED(vp, "nfsrv_lockctrl4");
2080 NFSVOPUNLOCK(vp, 0);
2082 nfsrv_locallock_rollback(vp, lfp, p);
2084 nfsrv_unlocklf(lfp);
2086 NFSVOPLOCK(vp, LK_EXCLUSIVE | LK_RETRY);
2088 if ((vp->v_iflag & VI_DOOMED) != 0)
2089 ret = NFSERR_SERVERFAULT;
2093 ret = nfsrv_delegconflict(tstp, &haslock, p, vp);
2096 * nfsrv_delegconflict unlocks state when it
2097 * returns non-zero, which it always does.
2100 FREE((caddr_t)other_lop, M_NFSDLOCK);
2110 /* Never gets here. */
2116 * Handle the unlock case by calling nfsrv_updatelock().
2117 * (Should I have done some access checking above for unlock? For now,
2118 * just let it happen.)
2120 if (new_stp->ls_flags & NFSLCK_UNLOCK) {
2121 first = new_lop->lo_first;
2122 end = new_lop->lo_end;
2123 nfsrv_updatelock(stp, new_lopp, &other_lop, lfp);
2124 stateidp->seqid = ++(stp->ls_stateid.seqid);
2125 if ((nd->nd_flag & ND_NFSV41) != 0 && stateidp->seqid == 0)
2126 stateidp->seqid = stp->ls_stateid.seqid = 1;
2127 stateidp->other[0] = stp->ls_stateid.other[0];
2128 stateidp->other[1] = stp->ls_stateid.other[1];
2129 stateidp->other[2] = stp->ls_stateid.other[2];
2130 if (filestruct_locked != 0) {
2132 if (vnode_unlocked == 0) {
2133 ASSERT_VOP_ELOCKED(vp, "nfsrv_lockctrl5");
2135 NFSVOPUNLOCK(vp, 0);
2137 /* Update the local locks. */
2138 nfsrv_localunlock(vp, lfp, first, end, p);
2140 nfsrv_unlocklf(lfp);
2147 * Search for a conflicting lock. A lock conflicts if:
2148 * - the lock range overlaps and
2149 * - at least one lock is a write lock and
2150 * - it is not owned by the same lock owner
2153 LIST_FOREACH(lop, &lfp->lf_lock, lo_lckfile) {
2154 if (new_lop->lo_end > lop->lo_first &&
2155 new_lop->lo_first < lop->lo_end &&
2156 (new_lop->lo_flags == NFSLCK_WRITE ||
2157 lop->lo_flags == NFSLCK_WRITE) &&
2158 lckstp != lop->lo_stp &&
2159 (clp != lop->lo_stp->ls_clp ||
2160 lckstp->ls_ownerlen != lop->lo_stp->ls_ownerlen ||
2161 NFSBCMP(lckstp->ls_owner, lop->lo_stp->ls_owner,
2162 lckstp->ls_ownerlen))) {
2164 FREE((caddr_t)other_lop, M_NFSDLOCK);
2167 if (vnode_unlocked != 0)
2168 ret = nfsrv_clientconflict(lop->lo_stp->ls_clp, &haslock,
2171 ret = nfsrv_clientconflict(lop->lo_stp->ls_clp, &haslock,
2174 if (filestruct_locked != 0) {
2175 if (vnode_unlocked == 0) {
2176 ASSERT_VOP_ELOCKED(vp, "nfsrv_lockctrl6");
2177 NFSVOPUNLOCK(vp, 0);
2179 /* Roll back local locks. */
2180 nfsrv_locallock_rollback(vp, lfp, p);
2182 nfsrv_unlocklf(lfp);
2184 NFSVOPLOCK(vp, LK_EXCLUSIVE | LK_RETRY);
2186 if ((vp->v_iflag & VI_DOOMED) != 0) {
2187 error = NFSERR_SERVERFAULT;
2192 * nfsrv_clientconflict() unlocks state when it
2199 * Found a conflicting lock, so record the conflict and
2202 if (cfp != NULL && ret == 0) {
2203 cfp->cl_clientid.lval[0]=lop->lo_stp->ls_stateid.other[0];
2204 cfp->cl_clientid.lval[1]=lop->lo_stp->ls_stateid.other[1];
2205 cfp->cl_first = lop->lo_first;
2206 cfp->cl_end = lop->lo_end;
2207 cfp->cl_flags = lop->lo_flags;
2208 cfp->cl_ownerlen = lop->lo_stp->ls_ownerlen;
2209 NFSBCOPY(lop->lo_stp->ls_owner, cfp->cl_owner,
2213 error = NFSERR_PERM;
2214 else if (new_stp->ls_flags & NFSLCK_RECLAIM)
2215 error = NFSERR_RECLAIMCONFLICT;
2216 else if (new_stp->ls_flags & NFSLCK_CHECK)
2217 error = NFSERR_LOCKED;
2219 error = NFSERR_DENIED;
2220 if (filestruct_locked != 0 && ret == 0) {
2221 /* Roll back local locks. */
2223 if (vnode_unlocked == 0) {
2224 ASSERT_VOP_ELOCKED(vp, "nfsrv_lockctrl7");
2226 NFSVOPUNLOCK(vp, 0);
2228 nfsrv_locallock_rollback(vp, lfp, p);
2230 nfsrv_unlocklf(lfp);
2240 * We only get here if there was no lock that conflicted.
2242 if (new_stp->ls_flags & (NFSLCK_TEST | NFSLCK_CHECK)) {
2248 * We only get here when we are creating or modifying a lock.
2249 * There are two variants:
2250 * - exist_lock_owner where lock_owner exists
2251 * - open_to_lock_owner with new lock_owner
2253 first = new_lop->lo_first;
2254 end = new_lop->lo_end;
2255 lock_flags = new_lop->lo_flags;
2256 if (!(new_stp->ls_flags & NFSLCK_OPENTOLOCK)) {
2257 nfsrv_updatelock(lckstp, new_lopp, &other_lop, lfp);
2258 stateidp->seqid = ++(lckstp->ls_stateid.seqid);
2259 if ((nd->nd_flag & ND_NFSV41) != 0 && stateidp->seqid == 0)
2260 stateidp->seqid = lckstp->ls_stateid.seqid = 1;
2261 stateidp->other[0] = lckstp->ls_stateid.other[0];
2262 stateidp->other[1] = lckstp->ls_stateid.other[1];
2263 stateidp->other[2] = lckstp->ls_stateid.other[2];
2266 * The new open_to_lock_owner case.
2267 * Link the new nfsstate into the lists.
2269 new_stp->ls_seq = new_stp->ls_opentolockseq;
2270 nfsrvd_refcache(new_stp->ls_op);
2271 stateidp->seqid = new_stp->ls_stateid.seqid = 1;
2272 stateidp->other[0] = new_stp->ls_stateid.other[0] =
2273 clp->lc_clientid.lval[0];
2274 stateidp->other[1] = new_stp->ls_stateid.other[1] =
2275 clp->lc_clientid.lval[1];
2276 stateidp->other[2] = new_stp->ls_stateid.other[2] =
2277 nfsrv_nextstateindex(clp);
2278 new_stp->ls_clp = clp;
2279 LIST_INIT(&new_stp->ls_lock);
2280 new_stp->ls_openstp = stp;
2281 new_stp->ls_lfp = lfp;
2282 nfsrv_insertlock(new_lop, (struct nfslock *)new_stp, new_stp,
2284 LIST_INSERT_HEAD(NFSSTATEHASH(clp, new_stp->ls_stateid),
2286 LIST_INSERT_HEAD(&stp->ls_open, new_stp, ls_list);
2289 newnfsstats.srvlockowners++;
2290 nfsrv_openpluslock++;
2292 if (filestruct_locked != 0) {
2294 nfsrv_locallock_commit(lfp, lock_flags, first, end);
2296 nfsrv_unlocklf(lfp);
2302 NFSLOCKV4ROOTMUTEX();
2303 nfsv4_unlock(&nfsv4rootfs_lock, 1);
2304 NFSUNLOCKV4ROOTMUTEX();
2306 if (vnode_unlocked != 0) {
2307 NFSVOPLOCK(vp, LK_EXCLUSIVE | LK_RETRY);
2308 if (error == 0 && (vp->v_iflag & VI_DOOMED) != 0)
2309 error = NFSERR_SERVERFAULT;
2312 FREE((caddr_t)other_lop, M_NFSDLOCK);
2313 NFSEXITCODE2(error, nd);
2318 * Check for state errors for Open.
2319 * repstat is passed back out as an error if more critical errors
2323 nfsrv_opencheck(nfsquad_t clientid, nfsv4stateid_t *stateidp,
2324 struct nfsstate *new_stp, vnode_t vp, struct nfsrv_descript *nd,
2325 NFSPROC_T *p, int repstat)
2327 struct nfsstate *stp, *nstp;
2328 struct nfsclient *clp;
2329 struct nfsstate *ownerstp;
2330 struct nfslockfile *lfp, *new_lfp;
2331 int error = 0, haslock = 0, ret, readonly = 0, getfhret = 0;
2333 if ((new_stp->ls_flags & NFSLCK_SHAREBITS) == NFSLCK_READACCESS)
2336 * Check for restart conditions (client and server).
2338 error = nfsrv_checkrestart(clientid, new_stp->ls_flags,
2339 &new_stp->ls_stateid, 0);
2344 * Check for state resource limit exceeded.
2345 * Technically this should be SMP protected, but the worst
2346 * case error is "out by one or two" on the count when it
2347 * returns NFSERR_RESOURCE and the limit is just a rather
2348 * arbitrary high water mark, so no harm is done.
2350 if (nfsrv_openpluslock > nfsrv_v4statelimit) {
2351 error = NFSERR_RESOURCE;
2356 MALLOC(new_lfp, struct nfslockfile *, sizeof (struct nfslockfile),
2357 M_NFSDLOCKFILE, M_WAITOK);
2359 getfhret = nfsrv_getlockfh(vp, new_stp->ls_flags, new_lfp,
2363 * Get the nfsclient structure.
2365 error = nfsrv_getclient(clientid, CLOPS_RENEW, &clp, NULL,
2366 (nfsquad_t)((u_quad_t)0), 0, nd, p);
2369 * Look up the open owner. See if it needs confirmation and
2370 * check the seq#, as required.
2373 nfsrv_getowner(&clp->lc_open, new_stp, &ownerstp);
2375 if (!error && ownerstp) {
2376 error = nfsrv_checkseqid(nd, new_stp->ls_seq, ownerstp,
2379 * If the OpenOwner hasn't been confirmed, assume the
2380 * old one was a replay and this one is ok.
2381 * See: RFC3530 Sec. 14.2.18.
2383 if (error == NFSERR_BADSEQID &&
2384 (ownerstp->ls_flags & NFSLCK_NEEDSCONFIRM))
2392 error = nfsrv_checkgrace(nd, clp, new_stp->ls_flags);
2393 if ((new_stp->ls_flags & NFSLCK_RECLAIM) && !error &&
2394 nfsrv_checkstable(clp))
2395 error = NFSERR_NOGRACE;
2398 * If none of the above errors occurred, let repstat be
2401 if (repstat && !error)
2406 NFSLOCKV4ROOTMUTEX();
2407 nfsv4_unlock(&nfsv4rootfs_lock, 1);
2408 NFSUNLOCKV4ROOTMUTEX();
2410 free((caddr_t)new_lfp, M_NFSDLOCKFILE);
2415 * If vp == NULL, the file doesn't exist yet, so return ok.
2416 * (This always happens on the first pass, so haslock must be 0.)
2420 FREE((caddr_t)new_lfp, M_NFSDLOCKFILE);
2425 * Get the structure for the underlying file.
2430 error = nfsrv_getlockfile(new_stp->ls_flags, &new_lfp, &lfp,
2433 FREE((caddr_t)new_lfp, M_NFSDLOCKFILE);
2437 NFSLOCKV4ROOTMUTEX();
2438 nfsv4_unlock(&nfsv4rootfs_lock, 1);
2439 NFSUNLOCKV4ROOTMUTEX();
2445 * Search for a conflicting open/share.
2447 if (new_stp->ls_flags & NFSLCK_DELEGCUR) {
2449 * For Delegate_Cur, search for the matching Delegation,
2450 * which indicates no conflict.
2451 * An old delegation should have been recovered by the
2452 * client doing a Claim_DELEGATE_Prev, so I won't let
2453 * it match and return NFSERR_EXPIRED. Should I let it
2456 LIST_FOREACH(stp, &lfp->lf_deleg, ls_file) {
2457 if (!(stp->ls_flags & NFSLCK_OLDDELEG) &&
2458 (((nd->nd_flag & ND_NFSV41) != 0 &&
2459 stateidp->seqid == 0) ||
2460 stateidp->seqid == stp->ls_stateid.seqid) &&
2461 !NFSBCMP(stateidp->other, stp->ls_stateid.other,
2465 if (stp == LIST_END(&lfp->lf_deleg) ||
2466 ((new_stp->ls_flags & NFSLCK_WRITEACCESS) &&
2467 (stp->ls_flags & NFSLCK_DELEGREAD))) {
2470 NFSLOCKV4ROOTMUTEX();
2471 nfsv4_unlock(&nfsv4rootfs_lock, 1);
2472 NFSUNLOCKV4ROOTMUTEX();
2474 error = NFSERR_EXPIRED;
2480 * Check for access/deny bit conflicts. I check for the same
2481 * owner as well, in case the client didn't bother.
2483 LIST_FOREACH(stp, &lfp->lf_open, ls_file) {
2484 if (!(new_stp->ls_flags & NFSLCK_DELEGCUR) &&
2485 (((new_stp->ls_flags & NFSLCK_ACCESSBITS) &
2486 ((stp->ls_flags>>NFSLCK_SHIFT) & NFSLCK_ACCESSBITS))||
2487 ((stp->ls_flags & NFSLCK_ACCESSBITS) &
2488 ((new_stp->ls_flags>>NFSLCK_SHIFT)&NFSLCK_ACCESSBITS)))){
2489 ret = nfsrv_clientconflict(stp->ls_clp,&haslock,vp,p);
2492 * nfsrv_clientconflict() unlocks
2493 * state when it returns non-zero.
2498 error = NFSERR_PERM;
2499 else if (new_stp->ls_flags & NFSLCK_RECLAIM)
2500 error = NFSERR_RECLAIMCONFLICT;
2502 error = NFSERR_SHAREDENIED;
2506 NFSLOCKV4ROOTMUTEX();
2507 nfsv4_unlock(&nfsv4rootfs_lock, 1);
2508 NFSUNLOCKV4ROOTMUTEX();
2515 * Check for a conflicting delegation. If one is found, call
2516 * nfsrv_delegconflict() to handle it. If the v4root lock hasn't
2517 * been set yet, it will get the lock. Otherwise, it will recall
2518 * the delegation. Then, we try try again...
2519 * (If NFSLCK_DELEGCUR is set, it has a delegation, so there
2520 * isn't a conflict.)
2521 * I currently believe the conflict algorithm to be:
2522 * For Open with Read Access and Deny None
2523 * - there is a conflict iff a different client has a write delegation
2524 * For Open with other Write Access or any Deny except None
2525 * - there is a conflict if a different client has any delegation
2526 * - there is a conflict if the same client has a read delegation
2527 * (The current concensus is that this last case should be
2528 * considered a conflict since the client with a read delegation
2529 * could have done an Open with ReadAccess and WriteDeny
2530 * locally and then not have checked for the WriteDeny.)
2531 * Don't check for a Reclaim, since that will be dealt with
2532 * by nfsrv_openctrl().
2534 if (!(new_stp->ls_flags &
2535 (NFSLCK_DELEGPREV | NFSLCK_DELEGCUR | NFSLCK_RECLAIM))) {
2536 stp = LIST_FIRST(&lfp->lf_deleg);
2537 while (stp != LIST_END(&lfp->lf_deleg)) {
2538 nstp = LIST_NEXT(stp, ls_file);
2539 if ((readonly && stp->ls_clp != clp &&
2540 (stp->ls_flags & NFSLCK_DELEGWRITE)) ||
2541 (!readonly && (stp->ls_clp != clp ||
2542 (stp->ls_flags & NFSLCK_DELEGREAD)))) {
2543 ret = nfsrv_delegconflict(stp, &haslock, p, vp);
2546 * nfsrv_delegconflict() unlocks state
2547 * when it returns non-zero.
2560 NFSLOCKV4ROOTMUTEX();
2561 nfsv4_unlock(&nfsv4rootfs_lock, 1);
2562 NFSUNLOCKV4ROOTMUTEX();
2566 NFSEXITCODE2(error, nd);
2571 * Open control function to create/update open state for an open.
2574 nfsrv_openctrl(struct nfsrv_descript *nd, vnode_t vp,
2575 struct nfsstate **new_stpp, nfsquad_t clientid, nfsv4stateid_t *stateidp,
2576 nfsv4stateid_t *delegstateidp, u_int32_t *rflagsp, struct nfsexstuff *exp,
2577 NFSPROC_T *p, u_quad_t filerev)
2579 struct nfsstate *new_stp = *new_stpp;
2580 struct nfsstate *stp, *nstp;
2581 struct nfsstate *openstp = NULL, *new_open, *ownerstp, *new_deleg;
2582 struct nfslockfile *lfp, *new_lfp;
2583 struct nfsclient *clp;
2584 int error = 0, haslock = 0, ret, delegate = 1, writedeleg = 1;
2585 int readonly = 0, cbret = 1, getfhret = 0;
2586 int gotstate = 0, len = 0;
2587 u_char *clidp = NULL;
2589 if ((new_stp->ls_flags & NFSLCK_SHAREBITS) == NFSLCK_READACCESS)
2592 * Check for restart conditions (client and server).
2593 * (Paranoia, should have been detected by nfsrv_opencheck().)
2594 * If an error does show up, return NFSERR_EXPIRED, since the
2595 * the seqid# has already been incremented.
2597 error = nfsrv_checkrestart(clientid, new_stp->ls_flags,
2598 &new_stp->ls_stateid, 0);
2600 printf("Nfsd: openctrl unexpected restart err=%d\n",
2602 error = NFSERR_EXPIRED;
2606 clidp = malloc(NFSV4_OPAQUELIMIT, M_TEMP, M_WAITOK);
2608 MALLOC(new_lfp, struct nfslockfile *, sizeof (struct nfslockfile),
2609 M_NFSDLOCKFILE, M_WAITOK);
2610 MALLOC(new_open, struct nfsstate *, sizeof (struct nfsstate),
2611 M_NFSDSTATE, M_WAITOK);
2612 MALLOC(new_deleg, struct nfsstate *, sizeof (struct nfsstate),
2613 M_NFSDSTATE, M_WAITOK);
2614 getfhret = nfsrv_getlockfh(vp, new_stp->ls_flags, new_lfp,
2618 * Get the client structure. Since the linked lists could be changed
2619 * by other nfsd processes if this process does a tsleep(), one of
2620 * two things must be done.
2621 * 1 - don't tsleep()
2623 * 2 - get the nfsv4_lock() { indicated by haslock == 1 }
2624 * before using the lists, since this lock stops the other
2625 * nfsd. This should only be used for rare cases, since it
2626 * essentially single threads the nfsd.
2627 * At this time, it is only done for cases where the stable
2628 * storage file must be written prior to completion of state
2631 error = nfsrv_getclient(clientid, CLOPS_RENEW, &clp, NULL,
2632 (nfsquad_t)((u_quad_t)0), 0, nd, p);
2633 if (!error && (clp->lc_flags & LCL_NEEDSCBNULL) &&
2636 * This happens on the first open for a client
2637 * that supports callbacks.
2641 * Although nfsrv_docallback() will sleep, clp won't
2642 * go away, since they are only removed when the
2643 * nfsv4_lock() has blocked the nfsd threads. The
2644 * fields in clp can change, but having multiple
2645 * threads do this Null callback RPC should be
2648 cbret = nfsrv_docallback(clp, NFSV4PROC_CBNULL,
2649 NULL, 0, NULL, NULL, NULL, p);
2651 clp->lc_flags &= ~LCL_NEEDSCBNULL;
2653 clp->lc_flags |= LCL_CALLBACKSON;
2657 * Look up the open owner. See if it needs confirmation and
2658 * check the seq#, as required.
2661 nfsrv_getowner(&clp->lc_open, new_stp, &ownerstp);
2665 printf("Nfsd: openctrl unexpected state err=%d\n",
2667 free((caddr_t)new_lfp, M_NFSDLOCKFILE);
2668 free((caddr_t)new_open, M_NFSDSTATE);
2669 free((caddr_t)new_deleg, M_NFSDSTATE);
2671 NFSLOCKV4ROOTMUTEX();
2672 nfsv4_unlock(&nfsv4rootfs_lock, 1);
2673 NFSUNLOCKV4ROOTMUTEX();
2675 error = NFSERR_EXPIRED;
2679 if (new_stp->ls_flags & NFSLCK_RECLAIM)
2680 nfsrv_markstable(clp);
2683 * Get the structure for the underlying file.
2688 error = nfsrv_getlockfile(new_stp->ls_flags, &new_lfp, &lfp,
2691 FREE((caddr_t)new_lfp, M_NFSDLOCKFILE);
2694 printf("Nfsd openctrl unexpected getlockfile err=%d\n",
2696 free((caddr_t)new_open, M_NFSDSTATE);
2697 free((caddr_t)new_deleg, M_NFSDSTATE);
2699 NFSLOCKV4ROOTMUTEX();
2700 nfsv4_unlock(&nfsv4rootfs_lock, 1);
2701 NFSUNLOCKV4ROOTMUTEX();
2707 * Search for a conflicting open/share.
2709 if (new_stp->ls_flags & NFSLCK_DELEGCUR) {
2711 * For Delegate_Cur, search for the matching Delegation,
2712 * which indicates no conflict.
2713 * An old delegation should have been recovered by the
2714 * client doing a Claim_DELEGATE_Prev, so I won't let
2715 * it match and return NFSERR_EXPIRED. Should I let it
2718 LIST_FOREACH(stp, &lfp->lf_deleg, ls_file) {
2719 if (!(stp->ls_flags & NFSLCK_OLDDELEG) &&
2720 (((nd->nd_flag & ND_NFSV41) != 0 &&
2721 stateidp->seqid == 0) ||
2722 stateidp->seqid == stp->ls_stateid.seqid) &&
2723 !NFSBCMP(stateidp->other, stp->ls_stateid.other,
2727 if (stp == LIST_END(&lfp->lf_deleg) ||
2728 ((new_stp->ls_flags & NFSLCK_WRITEACCESS) &&
2729 (stp->ls_flags & NFSLCK_DELEGREAD))) {
2731 printf("Nfsd openctrl unexpected expiry\n");
2732 free((caddr_t)new_open, M_NFSDSTATE);
2733 free((caddr_t)new_deleg, M_NFSDSTATE);
2735 NFSLOCKV4ROOTMUTEX();
2736 nfsv4_unlock(&nfsv4rootfs_lock, 1);
2737 NFSUNLOCKV4ROOTMUTEX();
2739 error = NFSERR_EXPIRED;
2744 * Don't issue a Delegation, since one already exists and
2745 * delay delegation timeout, as required.
2748 nfsrv_delaydelegtimeout(stp);
2752 * Check for access/deny bit conflicts. I also check for the
2753 * same owner, since the client might not have bothered to check.
2754 * Also, note an open for the same file and owner, if found,
2755 * which is all we do here for Delegate_Cur, since conflict
2756 * checking is already done.
2758 LIST_FOREACH(stp, &lfp->lf_open, ls_file) {
2759 if (ownerstp && stp->ls_openowner == ownerstp)
2761 if (!(new_stp->ls_flags & NFSLCK_DELEGCUR)) {
2763 * If another client has the file open, the only
2764 * delegation that can be issued is a Read delegation
2765 * and only if it is a Read open with Deny none.
2767 if (clp != stp->ls_clp) {
2768 if ((stp->ls_flags & NFSLCK_SHAREBITS) ==
2774 if(((new_stp->ls_flags & NFSLCK_ACCESSBITS) &
2775 ((stp->ls_flags>>NFSLCK_SHIFT) & NFSLCK_ACCESSBITS))||
2776 ((stp->ls_flags & NFSLCK_ACCESSBITS) &
2777 ((new_stp->ls_flags>>NFSLCK_SHIFT)&NFSLCK_ACCESSBITS))){
2778 ret = nfsrv_clientconflict(stp->ls_clp,&haslock,vp,p);
2781 * nfsrv_clientconflict() unlocks state
2782 * when it returns non-zero.
2784 free((caddr_t)new_open, M_NFSDSTATE);
2785 free((caddr_t)new_deleg, M_NFSDSTATE);
2790 error = NFSERR_PERM;
2791 else if (new_stp->ls_flags & NFSLCK_RECLAIM)
2792 error = NFSERR_RECLAIMCONFLICT;
2794 error = NFSERR_SHAREDENIED;
2798 NFSLOCKV4ROOTMUTEX();
2799 nfsv4_unlock(&nfsv4rootfs_lock, 1);
2800 NFSUNLOCKV4ROOTMUTEX();
2802 free((caddr_t)new_open, M_NFSDSTATE);
2803 free((caddr_t)new_deleg, M_NFSDSTATE);
2804 printf("nfsd openctrl unexpected client cnfl\n");
2811 * Check for a conflicting delegation. If one is found, call
2812 * nfsrv_delegconflict() to handle it. If the v4root lock hasn't
2813 * been set yet, it will get the lock. Otherwise, it will recall
2814 * the delegation. Then, we try try again...
2815 * (If NFSLCK_DELEGCUR is set, it has a delegation, so there
2816 * isn't a conflict.)
2817 * I currently believe the conflict algorithm to be:
2818 * For Open with Read Access and Deny None
2819 * - there is a conflict iff a different client has a write delegation
2820 * For Open with other Write Access or any Deny except None
2821 * - there is a conflict if a different client has any delegation
2822 * - there is a conflict if the same client has a read delegation
2823 * (The current concensus is that this last case should be
2824 * considered a conflict since the client with a read delegation
2825 * could have done an Open with ReadAccess and WriteDeny
2826 * locally and then not have checked for the WriteDeny.)
2828 if (!(new_stp->ls_flags & (NFSLCK_DELEGPREV | NFSLCK_DELEGCUR))) {
2829 stp = LIST_FIRST(&lfp->lf_deleg);
2830 while (stp != LIST_END(&lfp->lf_deleg)) {
2831 nstp = LIST_NEXT(stp, ls_file);
2832 if (stp->ls_clp != clp && (stp->ls_flags & NFSLCK_DELEGREAD))
2836 if ((readonly && stp->ls_clp != clp &&
2837 (stp->ls_flags & NFSLCK_DELEGWRITE)) ||
2838 (!readonly && (stp->ls_clp != clp ||
2839 (stp->ls_flags & NFSLCK_DELEGREAD)))) {
2840 if (new_stp->ls_flags & NFSLCK_RECLAIM) {
2843 ret = nfsrv_delegconflict(stp, &haslock, p, vp);
2846 * nfsrv_delegconflict() unlocks state
2847 * when it returns non-zero.
2849 printf("Nfsd openctrl unexpected deleg cnfl\n");
2850 free((caddr_t)new_open, M_NFSDSTATE);
2851 free((caddr_t)new_deleg, M_NFSDSTATE);
2866 * We only get here if there was no open that conflicted.
2867 * If an open for the owner exists, or in the access/deny bits.
2868 * Otherwise it is a new open. If the open_owner hasn't been
2869 * confirmed, replace the open with the new one needing confirmation,
2870 * otherwise add the open.
2872 if (new_stp->ls_flags & NFSLCK_DELEGPREV) {
2874 * Handle NFSLCK_DELEGPREV by searching the old delegations for
2875 * a match. If found, just move the old delegation to the current
2876 * delegation list and issue open. If not found, return
2879 LIST_FOREACH(stp, &clp->lc_olddeleg, ls_list) {
2880 if (stp->ls_lfp == lfp) {
2882 if (stp->ls_clp != clp)
2883 panic("olddeleg clp");
2884 LIST_REMOVE(stp, ls_list);
2885 LIST_REMOVE(stp, ls_hash);
2886 stp->ls_flags &= ~NFSLCK_OLDDELEG;
2887 stp->ls_stateid.seqid = delegstateidp->seqid = 1;
2888 stp->ls_stateid.other[0] = delegstateidp->other[0] =
2889 clp->lc_clientid.lval[0];
2890 stp->ls_stateid.other[1] = delegstateidp->other[1] =
2891 clp->lc_clientid.lval[1];
2892 stp->ls_stateid.other[2] = delegstateidp->other[2] =
2893 nfsrv_nextstateindex(clp);
2894 stp->ls_compref = nd->nd_compref;
2895 LIST_INSERT_HEAD(&clp->lc_deleg, stp, ls_list);
2896 LIST_INSERT_HEAD(NFSSTATEHASH(clp,
2897 stp->ls_stateid), stp, ls_hash);
2898 if (stp->ls_flags & NFSLCK_DELEGWRITE)
2899 *rflagsp |= NFSV4OPEN_WRITEDELEGATE;
2901 *rflagsp |= NFSV4OPEN_READDELEGATE;
2902 clp->lc_delegtime = NFSD_MONOSEC +
2903 nfsrv_lease + NFSRV_LEASEDELTA;
2906 * Now, do the associated open.
2908 new_open->ls_stateid.seqid = 1;
2909 new_open->ls_stateid.other[0] = clp->lc_clientid.lval[0];
2910 new_open->ls_stateid.other[1] = clp->lc_clientid.lval[1];
2911 new_open->ls_stateid.other[2] = nfsrv_nextstateindex(clp);
2912 new_open->ls_flags = (new_stp->ls_flags&NFSLCK_DENYBITS)|
2914 if (stp->ls_flags & NFSLCK_DELEGWRITE)
2915 new_open->ls_flags |= (NFSLCK_READACCESS |
2916 NFSLCK_WRITEACCESS);
2918 new_open->ls_flags |= NFSLCK_READACCESS;
2919 new_open->ls_uid = new_stp->ls_uid;
2920 new_open->ls_lfp = lfp;
2921 new_open->ls_clp = clp;
2922 LIST_INIT(&new_open->ls_open);
2923 LIST_INSERT_HEAD(&lfp->lf_open, new_open, ls_file);
2924 LIST_INSERT_HEAD(NFSSTATEHASH(clp, new_open->ls_stateid),
2927 * and handle the open owner
2930 new_open->ls_openowner = ownerstp;
2931 LIST_INSERT_HEAD(&ownerstp->ls_open,new_open,ls_list);
2933 new_open->ls_openowner = new_stp;
2934 new_stp->ls_flags = 0;
2935 nfsrvd_refcache(new_stp->ls_op);
2936 new_stp->ls_noopens = 0;
2937 LIST_INIT(&new_stp->ls_open);
2938 LIST_INSERT_HEAD(&new_stp->ls_open, new_open, ls_list);
2939 LIST_INSERT_HEAD(&clp->lc_open, new_stp, ls_list);
2941 newnfsstats.srvopenowners++;
2942 nfsrv_openpluslock++;
2946 newnfsstats.srvopens++;
2947 nfsrv_openpluslock++;
2951 if (stp == LIST_END(&clp->lc_olddeleg))
2952 error = NFSERR_EXPIRED;
2953 } else if (new_stp->ls_flags & (NFSLCK_DELEGREAD | NFSLCK_DELEGWRITE)) {
2955 * Scan to see that no delegation for this client and file
2956 * doesn't already exist.
2957 * There also shouldn't yet be an Open for this file and
2960 LIST_FOREACH(stp, &lfp->lf_deleg, ls_file) {
2961 if (stp->ls_clp == clp)
2964 if (stp == LIST_END(&lfp->lf_deleg) && openstp == NULL) {
2966 * This is the Claim_Previous case with a delegation
2967 * type != Delegate_None.
2970 * First, add the delegation. (Although we must issue the
2971 * delegation, we can also ask for an immediate return.)
2973 new_deleg->ls_stateid.seqid = delegstateidp->seqid = 1;
2974 new_deleg->ls_stateid.other[0] = delegstateidp->other[0] =
2975 clp->lc_clientid.lval[0];
2976 new_deleg->ls_stateid.other[1] = delegstateidp->other[1] =
2977 clp->lc_clientid.lval[1];
2978 new_deleg->ls_stateid.other[2] = delegstateidp->other[2] =
2979 nfsrv_nextstateindex(clp);
2980 if (new_stp->ls_flags & NFSLCK_DELEGWRITE) {
2981 new_deleg->ls_flags = (NFSLCK_DELEGWRITE |
2982 NFSLCK_READACCESS | NFSLCK_WRITEACCESS);
2983 *rflagsp |= NFSV4OPEN_WRITEDELEGATE;
2984 nfsrv_writedelegcnt++;
2986 new_deleg->ls_flags = (NFSLCK_DELEGREAD |
2988 *rflagsp |= NFSV4OPEN_READDELEGATE;
2990 new_deleg->ls_uid = new_stp->ls_uid;
2991 new_deleg->ls_lfp = lfp;
2992 new_deleg->ls_clp = clp;
2993 new_deleg->ls_filerev = filerev;
2994 new_deleg->ls_compref = nd->nd_compref;
2995 LIST_INSERT_HEAD(&lfp->lf_deleg, new_deleg, ls_file);
2996 LIST_INSERT_HEAD(NFSSTATEHASH(clp,
2997 new_deleg->ls_stateid), new_deleg, ls_hash);
2998 LIST_INSERT_HEAD(&clp->lc_deleg, new_deleg, ls_list);
3000 if (delegate == 2 || nfsrv_issuedelegs == 0 ||
3001 (clp->lc_flags & (LCL_CALLBACKSON | LCL_CBDOWN)) !=
3003 NFSRV_V4DELEGLIMIT(nfsrv_delegatecnt) ||
3004 !NFSVNO_DELEGOK(vp))
3005 *rflagsp |= NFSV4OPEN_RECALL;
3006 newnfsstats.srvdelegates++;
3007 nfsrv_openpluslock++;
3008 nfsrv_delegatecnt++;
3011 * Now, do the associated open.
3013 new_open->ls_stateid.seqid = 1;
3014 new_open->ls_stateid.other[0] = clp->lc_clientid.lval[0];
3015 new_open->ls_stateid.other[1] = clp->lc_clientid.lval[1];
3016 new_open->ls_stateid.other[2] = nfsrv_nextstateindex(clp);
3017 new_open->ls_flags = (new_stp->ls_flags & NFSLCK_DENYBITS) |
3019 if (new_stp->ls_flags & NFSLCK_DELEGWRITE)
3020 new_open->ls_flags |= (NFSLCK_READACCESS |
3021 NFSLCK_WRITEACCESS);
3023 new_open->ls_flags |= NFSLCK_READACCESS;
3024 new_open->ls_uid = new_stp->ls_uid;
3025 new_open->ls_lfp = lfp;
3026 new_open->ls_clp = clp;
3027 LIST_INIT(&new_open->ls_open);
3028 LIST_INSERT_HEAD(&lfp->lf_open, new_open, ls_file);
3029 LIST_INSERT_HEAD(NFSSTATEHASH(clp, new_open->ls_stateid),
3032 * and handle the open owner
3035 new_open->ls_openowner = ownerstp;
3036 LIST_INSERT_HEAD(&ownerstp->ls_open, new_open, ls_list);
3038 new_open->ls_openowner = new_stp;
3039 new_stp->ls_flags = 0;
3040 nfsrvd_refcache(new_stp->ls_op);
3041 new_stp->ls_noopens = 0;
3042 LIST_INIT(&new_stp->ls_open);
3043 LIST_INSERT_HEAD(&new_stp->ls_open, new_open, ls_list);
3044 LIST_INSERT_HEAD(&clp->lc_open, new_stp, ls_list);
3046 newnfsstats.srvopenowners++;
3047 nfsrv_openpluslock++;
3051 newnfsstats.srvopens++;
3052 nfsrv_openpluslock++;
3054 error = NFSERR_RECLAIMCONFLICT;
3056 } else if (ownerstp) {
3057 if (ownerstp->ls_flags & NFSLCK_NEEDSCONFIRM) {
3058 /* Replace the open */
3059 if (ownerstp->ls_op)
3060 nfsrvd_derefcache(ownerstp->ls_op);
3061 ownerstp->ls_op = new_stp->ls_op;
3062 nfsrvd_refcache(ownerstp->ls_op);
3063 ownerstp->ls_seq = new_stp->ls_seq;
3064 *rflagsp |= NFSV4OPEN_RESULTCONFIRM;
3065 stp = LIST_FIRST(&ownerstp->ls_open);
3066 stp->ls_flags = (new_stp->ls_flags & NFSLCK_SHAREBITS) |
3068 stp->ls_stateid.seqid = 1;
3069 stp->ls_uid = new_stp->ls_uid;
3070 if (lfp != stp->ls_lfp) {
3071 LIST_REMOVE(stp, ls_file);
3072 LIST_INSERT_HEAD(&lfp->lf_open, stp, ls_file);
3076 } else if (openstp) {
3077 openstp->ls_flags |= (new_stp->ls_flags & NFSLCK_SHAREBITS);
3078 openstp->ls_stateid.seqid++;
3079 if ((nd->nd_flag & ND_NFSV41) != 0 &&
3080 openstp->ls_stateid.seqid == 0)
3081 openstp->ls_stateid.seqid = 1;
3084 * This is where we can choose to issue a delegation.
3086 if ((new_stp->ls_flags & NFSLCK_WANTNODELEG) != 0)
3087 *rflagsp |= NFSV4OPEN_WDNOTWANTED;
3088 else if (nfsrv_issuedelegs == 0)
3089 *rflagsp |= NFSV4OPEN_WDSUPPFTYPE;
3090 else if (NFSRV_V4DELEGLIMIT(nfsrv_delegatecnt))
3091 *rflagsp |= NFSV4OPEN_WDRESOURCE;
3092 else if (delegate == 0 || writedeleg == 0 ||
3093 NFSVNO_EXRDONLY(exp) || (readonly != 0 &&
3094 nfsrv_writedelegifpos == 0) ||
3095 !NFSVNO_DELEGOK(vp) ||
3096 (new_stp->ls_flags & NFSLCK_WANTRDELEG) != 0 ||
3097 (clp->lc_flags & (LCL_CALLBACKSON | LCL_CBDOWN)) !=
3099 *rflagsp |= NFSV4OPEN_WDCONTENTION;
3101 new_deleg->ls_stateid.seqid = delegstateidp->seqid = 1;
3102 new_deleg->ls_stateid.other[0] = delegstateidp->other[0]
3103 = clp->lc_clientid.lval[0];
3104 new_deleg->ls_stateid.other[1] = delegstateidp->other[1]
3105 = clp->lc_clientid.lval[1];
3106 new_deleg->ls_stateid.other[2] = delegstateidp->other[2]
3107 = nfsrv_nextstateindex(clp);
3108 new_deleg->ls_flags = (NFSLCK_DELEGWRITE |
3109 NFSLCK_READACCESS | NFSLCK_WRITEACCESS);
3110 *rflagsp |= NFSV4OPEN_WRITEDELEGATE;
3111 new_deleg->ls_uid = new_stp->ls_uid;
3112 new_deleg->ls_lfp = lfp;
3113 new_deleg->ls_clp = clp;
3114 new_deleg->ls_filerev = filerev;
3115 new_deleg->ls_compref = nd->nd_compref;
3116 nfsrv_writedelegcnt++;
3117 LIST_INSERT_HEAD(&lfp->lf_deleg, new_deleg, ls_file);
3118 LIST_INSERT_HEAD(NFSSTATEHASH(clp,
3119 new_deleg->ls_stateid), new_deleg, ls_hash);
3120 LIST_INSERT_HEAD(&clp->lc_deleg, new_deleg, ls_list);
3122 newnfsstats.srvdelegates++;
3123 nfsrv_openpluslock++;
3124 nfsrv_delegatecnt++;
3127 new_open->ls_stateid.seqid = 1;
3128 new_open->ls_stateid.other[0] = clp->lc_clientid.lval[0];
3129 new_open->ls_stateid.other[1] = clp->lc_clientid.lval[1];
3130 new_open->ls_stateid.other[2] = nfsrv_nextstateindex(clp);
3131 new_open->ls_flags = (new_stp->ls_flags & NFSLCK_SHAREBITS)|
3133 new_open->ls_uid = new_stp->ls_uid;
3134 new_open->ls_openowner = ownerstp;
3135 new_open->ls_lfp = lfp;
3136 new_open->ls_clp = clp;
3137 LIST_INIT(&new_open->ls_open);
3138 LIST_INSERT_HEAD(&lfp->lf_open, new_open, ls_file);
3139 LIST_INSERT_HEAD(&ownerstp->ls_open, new_open, ls_list);
3140 LIST_INSERT_HEAD(NFSSTATEHASH(clp, new_open->ls_stateid),
3144 newnfsstats.srvopens++;
3145 nfsrv_openpluslock++;
3148 * This is where we can choose to issue a delegation.
3150 if ((new_stp->ls_flags & NFSLCK_WANTNODELEG) != 0)
3151 *rflagsp |= NFSV4OPEN_WDNOTWANTED;
3152 else if (nfsrv_issuedelegs == 0)
3153 *rflagsp |= NFSV4OPEN_WDSUPPFTYPE;
3154 else if (NFSRV_V4DELEGLIMIT(nfsrv_delegatecnt))
3155 *rflagsp |= NFSV4OPEN_WDRESOURCE;
3156 else if (delegate == 0 || (writedeleg == 0 &&
3157 readonly == 0) || !NFSVNO_DELEGOK(vp) ||
3158 (clp->lc_flags & (LCL_CALLBACKSON | LCL_CBDOWN)) !=
3160 *rflagsp |= NFSV4OPEN_WDCONTENTION;
3162 new_deleg->ls_stateid.seqid = delegstateidp->seqid = 1;
3163 new_deleg->ls_stateid.other[0] = delegstateidp->other[0]
3164 = clp->lc_clientid.lval[0];
3165 new_deleg->ls_stateid.other[1] = delegstateidp->other[1]
3166 = clp->lc_clientid.lval[1];
3167 new_deleg->ls_stateid.other[2] = delegstateidp->other[2]
3168 = nfsrv_nextstateindex(clp);
3169 if (writedeleg && !NFSVNO_EXRDONLY(exp) &&
3170 (nfsrv_writedelegifpos || !readonly) &&
3171 (new_stp->ls_flags & NFSLCK_WANTRDELEG) == 0) {
3172 new_deleg->ls_flags = (NFSLCK_DELEGWRITE |
3173 NFSLCK_READACCESS | NFSLCK_WRITEACCESS);
3174 *rflagsp |= NFSV4OPEN_WRITEDELEGATE;
3175 nfsrv_writedelegcnt++;
3177 new_deleg->ls_flags = (NFSLCK_DELEGREAD |
3179 *rflagsp |= NFSV4OPEN_READDELEGATE;
3181 new_deleg->ls_uid = new_stp->ls_uid;
3182 new_deleg->ls_lfp = lfp;
3183 new_deleg->ls_clp = clp;
3184 new_deleg->ls_filerev = filerev;
3185 new_deleg->ls_compref = nd->nd_compref;
3186 LIST_INSERT_HEAD(&lfp->lf_deleg, new_deleg, ls_file);
3187 LIST_INSERT_HEAD(NFSSTATEHASH(clp,
3188 new_deleg->ls_stateid), new_deleg, ls_hash);
3189 LIST_INSERT_HEAD(&clp->lc_deleg, new_deleg, ls_list);
3191 newnfsstats.srvdelegates++;
3192 nfsrv_openpluslock++;
3193 nfsrv_delegatecnt++;
3198 * New owner case. Start the open_owner sequence with a
3199 * Needs confirmation (unless a reclaim) and hang the
3202 new_open->ls_stateid.seqid = 1;
3203 new_open->ls_stateid.other[0] = clp->lc_clientid.lval[0];
3204 new_open->ls_stateid.other[1] = clp->lc_clientid.lval[1];
3205 new_open->ls_stateid.other[2] = nfsrv_nextstateindex(clp);
3206 new_open->ls_flags = (new_stp->ls_flags & NFSLCK_SHAREBITS) |
3208 new_open->ls_uid = new_stp->ls_uid;
3209 LIST_INIT(&new_open->ls_open);
3210 new_open->ls_openowner = new_stp;
3211 new_open->ls_lfp = lfp;
3212 new_open->ls_clp = clp;
3213 LIST_INSERT_HEAD(&lfp->lf_open, new_open, ls_file);
3214 if (new_stp->ls_flags & NFSLCK_RECLAIM) {
3215 new_stp->ls_flags = 0;
3216 } else if ((nd->nd_flag & ND_NFSV41) != 0) {
3217 /* NFSv4.1 never needs confirmation. */
3218 new_stp->ls_flags = 0;
3221 * This is where we can choose to issue a delegation.
3223 if (delegate && nfsrv_issuedelegs &&
3224 (writedeleg || readonly) &&
3225 (clp->lc_flags & (LCL_CALLBACKSON | LCL_CBDOWN)) ==
3227 !NFSRV_V4DELEGLIMIT(nfsrv_delegatecnt) &&
3228 NFSVNO_DELEGOK(vp) &&
3229 ((nd->nd_flag & ND_NFSV41) == 0 ||
3230 (new_stp->ls_flags & NFSLCK_WANTNODELEG) == 0)) {
3231 new_deleg->ls_stateid.seqid =
3232 delegstateidp->seqid = 1;
3233 new_deleg->ls_stateid.other[0] =
3234 delegstateidp->other[0]
3235 = clp->lc_clientid.lval[0];
3236 new_deleg->ls_stateid.other[1] =
3237 delegstateidp->other[1]
3238 = clp->lc_clientid.lval[1];
3239 new_deleg->ls_stateid.other[2] =
3240 delegstateidp->other[2]
3241 = nfsrv_nextstateindex(clp);
3242 if (writedeleg && !NFSVNO_EXRDONLY(exp) &&
3243 (nfsrv_writedelegifpos || !readonly) &&
3244 ((nd->nd_flag & ND_NFSV41) == 0 ||
3245 (new_stp->ls_flags & NFSLCK_WANTRDELEG) ==
3247 new_deleg->ls_flags =
3248 (NFSLCK_DELEGWRITE |
3250 NFSLCK_WRITEACCESS);
3251 *rflagsp |= NFSV4OPEN_WRITEDELEGATE;
3252 nfsrv_writedelegcnt++;
3254 new_deleg->ls_flags =
3257 *rflagsp |= NFSV4OPEN_READDELEGATE;
3259 new_deleg->ls_uid = new_stp->ls_uid;
3260 new_deleg->ls_lfp = lfp;
3261 new_deleg->ls_clp = clp;
3262 new_deleg->ls_filerev = filerev;
3263 new_deleg->ls_compref = nd->nd_compref;
3264 LIST_INSERT_HEAD(&lfp->lf_deleg, new_deleg,
3266 LIST_INSERT_HEAD(NFSSTATEHASH(clp,
3267 new_deleg->ls_stateid), new_deleg, ls_hash);
3268 LIST_INSERT_HEAD(&clp->lc_deleg, new_deleg,
3271 newnfsstats.srvdelegates++;
3272 nfsrv_openpluslock++;
3273 nfsrv_delegatecnt++;
3276 * Since NFSv4.1 never does an OpenConfirm, the first
3277 * open state will be acquired here.
3279 if (!(clp->lc_flags & LCL_STAMPEDSTABLE)) {
3280 clp->lc_flags |= LCL_STAMPEDSTABLE;
3281 len = clp->lc_idlen;
3282 NFSBCOPY(clp->lc_id, clidp, len);
3286 *rflagsp |= NFSV4OPEN_RESULTCONFIRM;
3287 new_stp->ls_flags = NFSLCK_NEEDSCONFIRM;
3289 nfsrvd_refcache(new_stp->ls_op);
3290 new_stp->ls_noopens = 0;
3291 LIST_INIT(&new_stp->ls_open);
3292 LIST_INSERT_HEAD(&new_stp->ls_open, new_open, ls_list);
3293 LIST_INSERT_HEAD(&clp->lc_open, new_stp, ls_list);
3294 LIST_INSERT_HEAD(NFSSTATEHASH(clp, new_open->ls_stateid),
3299 newnfsstats.srvopens++;
3300 nfsrv_openpluslock++;
3301 newnfsstats.srvopenowners++;
3302 nfsrv_openpluslock++;
3305 stateidp->seqid = openstp->ls_stateid.seqid;
3306 stateidp->other[0] = openstp->ls_stateid.other[0];
3307 stateidp->other[1] = openstp->ls_stateid.other[1];
3308 stateidp->other[2] = openstp->ls_stateid.other[2];
3312 NFSLOCKV4ROOTMUTEX();
3313 nfsv4_unlock(&nfsv4rootfs_lock, 1);
3314 NFSUNLOCKV4ROOTMUTEX();
3317 FREE((caddr_t)new_open, M_NFSDSTATE);
3319 FREE((caddr_t)new_deleg, M_NFSDSTATE);
3322 * If the NFSv4.1 client just acquired its first open, write a timestamp
3323 * to the stable storage file.
3325 if (gotstate != 0) {
3326 nfsrv_writestable(clidp, len, NFSNST_NEWSTATE, p);
3327 nfsrv_backupstable();
3331 free(clidp, M_TEMP);
3332 NFSEXITCODE2(error, nd);
3337 * Open update. Does the confirm, downgrade and close.
3340 nfsrv_openupdate(vnode_t vp, struct nfsstate *new_stp, nfsquad_t clientid,
3341 nfsv4stateid_t *stateidp, struct nfsrv_descript *nd, NFSPROC_T *p)
3343 struct nfsstate *stp, *ownerstp;
3344 struct nfsclient *clp;
3345 struct nfslockfile *lfp;
3347 int error = 0, gotstate = 0, len = 0;
3348 u_char *clidp = NULL;
3351 * Check for restart conditions (client and server).
3353 error = nfsrv_checkrestart(clientid, new_stp->ls_flags,
3354 &new_stp->ls_stateid, 0);
3358 clidp = malloc(NFSV4_OPAQUELIMIT, M_TEMP, M_WAITOK);
3361 * Get the open structure via clientid and stateid.
3363 error = nfsrv_getclient(clientid, CLOPS_RENEW, &clp, NULL,
3364 (nfsquad_t)((u_quad_t)0), 0, nd, p);
3366 error = nfsrv_getstate(clp, &new_stp->ls_stateid,
3367 new_stp->ls_flags, &stp);
3370 * Sanity check the open.
3372 if (!error && (!(stp->ls_flags & NFSLCK_OPEN) ||
3373 (!(new_stp->ls_flags & NFSLCK_CONFIRM) &&
3374 (stp->ls_openowner->ls_flags & NFSLCK_NEEDSCONFIRM)) ||
3375 ((new_stp->ls_flags & NFSLCK_CONFIRM) &&
3376 (!(stp->ls_openowner->ls_flags & NFSLCK_NEEDSCONFIRM)))))
3377 error = NFSERR_BADSTATEID;
3380 error = nfsrv_checkseqid(nd, new_stp->ls_seq,
3381 stp->ls_openowner, new_stp->ls_op);
3382 if (!error && stp->ls_stateid.seqid != new_stp->ls_stateid.seqid &&
3383 (((nd->nd_flag & ND_NFSV41) == 0 &&
3384 !(new_stp->ls_flags & NFSLCK_CONFIRM)) ||
3385 ((nd->nd_flag & ND_NFSV41) != 0 &&
3386 new_stp->ls_stateid.seqid != 0)))
3387 error = NFSERR_OLDSTATEID;
3388 if (!error && vnode_vtype(vp) != VREG) {
3389 if (vnode_vtype(vp) == VDIR)
3390 error = NFSERR_ISDIR;
3392 error = NFSERR_INVAL;
3397 * If a client tries to confirm an Open with a bad
3398 * seqid# and there are no byte range locks or other Opens
3399 * on the openowner, just throw it away, so the next use of the
3400 * openowner will start a fresh seq#.
3402 if (error == NFSERR_BADSEQID &&
3403 (new_stp->ls_flags & NFSLCK_CONFIRM) &&
3404 nfsrv_nootherstate(stp))
3405 nfsrv_freeopenowner(stp->ls_openowner, 0, p);
3411 * Set the return stateid.
3413 stateidp->seqid = stp->ls_stateid.seqid + 1;
3414 if ((nd->nd_flag & ND_NFSV41) != 0 && stateidp->seqid == 0)
3415 stateidp->seqid = 1;
3416 stateidp->other[0] = stp->ls_stateid.other[0];
3417 stateidp->other[1] = stp->ls_stateid.other[1];
3418 stateidp->other[2] = stp->ls_stateid.other[2];
3420 * Now, handle the three cases.
3422 if (new_stp->ls_flags & NFSLCK_CONFIRM) {
3424 * If the open doesn't need confirmation, it seems to me that
3425 * there is a client error, but I'll just log it and keep going?
3427 if (!(stp->ls_openowner->ls_flags & NFSLCK_NEEDSCONFIRM))
3428 printf("Nfsv4d: stray open confirm\n");
3429 stp->ls_openowner->ls_flags = 0;
3430 stp->ls_stateid.seqid++;
3431 if ((nd->nd_flag & ND_NFSV41) != 0 &&
3432 stp->ls_stateid.seqid == 0)
3433 stp->ls_stateid.seqid = 1;
3434 if (!(clp->lc_flags & LCL_STAMPEDSTABLE)) {
3435 clp->lc_flags |= LCL_STAMPEDSTABLE;
3436 len = clp->lc_idlen;
3437 NFSBCOPY(clp->lc_id, clidp, len);
3441 } else if (new_stp->ls_flags & NFSLCK_CLOSE) {
3442 ownerstp = stp->ls_openowner;
3444 if (nfsrv_dolocallocks != 0 && !LIST_EMPTY(&stp->ls_open)) {
3445 /* Get the lf lock */
3448 ASSERT_VOP_ELOCKED(vp, "nfsrv_openupdate");
3449 NFSVOPUNLOCK(vp, 0);
3450 if (nfsrv_freeopen(stp, vp, 1, p) == 0) {
3452 nfsrv_unlocklf(lfp);
3455 NFSVOPLOCK(vp, LK_EXCLUSIVE | LK_RETRY);
3457 (void) nfsrv_freeopen(stp, NULL, 0, p);
3462 * Update the share bits, making sure that the new set are a
3463 * subset of the old ones.
3465 bits = (new_stp->ls_flags & NFSLCK_SHAREBITS);
3466 if (~(stp->ls_flags) & bits) {
3468 error = NFSERR_INVAL;
3471 stp->ls_flags = (bits | NFSLCK_OPEN);
3472 stp->ls_stateid.seqid++;
3473 if ((nd->nd_flag & ND_NFSV41) != 0 &&
3474 stp->ls_stateid.seqid == 0)
3475 stp->ls_stateid.seqid = 1;
3480 * If the client just confirmed its first open, write a timestamp
3481 * to the stable storage file.
3483 if (gotstate != 0) {
3484 nfsrv_writestable(clidp, len, NFSNST_NEWSTATE, p);
3485 nfsrv_backupstable();
3489 free(clidp, M_TEMP);
3490 NFSEXITCODE2(error, nd);
3495 * Delegation update. Does the purge and return.
3498 nfsrv_delegupdate(struct nfsrv_descript *nd, nfsquad_t clientid,
3499 nfsv4stateid_t *stateidp, vnode_t vp, int op, struct ucred *cred,
3502 struct nfsstate *stp;
3503 struct nfsclient *clp;
3508 * Do a sanity check against the file handle for DelegReturn.
3511 error = nfsvno_getfh(vp, &fh, p);
3516 * Check for restart conditions (client and server).
3518 if (op == NFSV4OP_DELEGRETURN)
3519 error = nfsrv_checkrestart(clientid, NFSLCK_DELEGRETURN,
3522 error = nfsrv_checkrestart(clientid, NFSLCK_DELEGPURGE,
3527 * Get the open structure via clientid and stateid.
3530 error = nfsrv_getclient(clientid, CLOPS_RENEW, &clp, NULL,
3531 (nfsquad_t)((u_quad_t)0), 0, nd, p);
3533 if (error == NFSERR_CBPATHDOWN)
3535 if (error == NFSERR_STALECLIENTID && op == NFSV4OP_DELEGRETURN)
3536 error = NFSERR_STALESTATEID;
3538 if (!error && op == NFSV4OP_DELEGRETURN) {
3539 error = nfsrv_getstate(clp, stateidp, NFSLCK_DELEGRETURN, &stp);
3540 if (!error && stp->ls_stateid.seqid != stateidp->seqid &&
3541 ((nd->nd_flag & ND_NFSV41) == 0 || stateidp->seqid != 0))
3542 error = NFSERR_OLDSTATEID;
3545 * NFSERR_EXPIRED means that the state has gone away,
3546 * so Delegations have been purged. Just return ok.
3548 if (error == NFSERR_EXPIRED && op == NFSV4OP_DELEGPURGE) {
3558 if (op == NFSV4OP_DELEGRETURN) {
3559 if (NFSBCMP((caddr_t)&fh, (caddr_t)&stp->ls_lfp->lf_fh,
3560 sizeof (fhandle_t))) {
3562 error = NFSERR_BADSTATEID;
3565 nfsrv_freedeleg(stp);
3567 nfsrv_freedeleglist(&clp->lc_olddeleg);
3578 * Release lock owner.
3581 nfsrv_releaselckown(struct nfsstate *new_stp, nfsquad_t clientid,
3584 struct nfsstate *stp, *nstp, *openstp, *ownstp;
3585 struct nfsclient *clp;
3589 * Check for restart conditions (client and server).
3591 error = nfsrv_checkrestart(clientid, new_stp->ls_flags,
3592 &new_stp->ls_stateid, 0);
3598 * Get the lock owner by name.
3600 error = nfsrv_getclient(clientid, CLOPS_RENEW, &clp, NULL,
3601 (nfsquad_t)((u_quad_t)0), 0, NULL, p);
3606 LIST_FOREACH(ownstp, &clp->lc_open, ls_list) {
3607 LIST_FOREACH(openstp, &ownstp->ls_open, ls_list) {
3608 stp = LIST_FIRST(&openstp->ls_open);
3609 while (stp != LIST_END(&openstp->ls_open)) {
3610 nstp = LIST_NEXT(stp, ls_list);
3612 * If the owner matches, check for locks and
3613 * then free or return an error.
3615 if (stp->ls_ownerlen == new_stp->ls_ownerlen &&
3616 !NFSBCMP(stp->ls_owner, new_stp->ls_owner,
3618 if (LIST_EMPTY(&stp->ls_lock)) {
3619 nfsrv_freelockowner(stp, NULL, 0, p);
3622 error = NFSERR_LOCKSHELD;
3638 * Get the file handle for a lock structure.
3641 nfsrv_getlockfh(vnode_t vp, u_short flags, struct nfslockfile *new_lfp,
3642 fhandle_t *nfhp, NFSPROC_T *p)
3644 fhandle_t *fhp = NULL;
3648 * For lock, use the new nfslock structure, otherwise just
3649 * a fhandle_t on the stack.
3651 if (flags & NFSLCK_OPEN) {
3652 KASSERT(new_lfp != NULL, ("nfsrv_getlockfh: new_lfp NULL"));
3653 fhp = &new_lfp->lf_fh;
3657 panic("nfsrv_getlockfh");
3659 error = nfsvno_getfh(vp, fhp, p);
3665 * Get an nfs lock structure. Allocate one, as required, and return a
3667 * Returns an NFSERR_xxx upon failure or -1 to indicate no current lock.
3670 nfsrv_getlockfile(u_short flags, struct nfslockfile **new_lfpp,
3671 struct nfslockfile **lfpp, fhandle_t *nfhp, int lockit)
3673 struct nfslockfile *lfp;
3674 fhandle_t *fhp = NULL, *tfhp;
3675 struct nfslockhashhead *hp;
3676 struct nfslockfile *new_lfp = NULL;
3679 * For lock, use the new nfslock structure, otherwise just
3680 * a fhandle_t on the stack.
3682 if (flags & NFSLCK_OPEN) {
3683 new_lfp = *new_lfpp;
3684 fhp = &new_lfp->lf_fh;
3688 panic("nfsrv_getlockfile");
3691 hp = NFSLOCKHASH(fhp);
3692 LIST_FOREACH(lfp, hp, lf_hash) {
3694 if (NFSVNO_CMPFH(fhp, tfhp)) {
3701 if (!(flags & NFSLCK_OPEN))
3705 * No match, so chain the new one into the list.
3707 LIST_INIT(&new_lfp->lf_open);
3708 LIST_INIT(&new_lfp->lf_lock);
3709 LIST_INIT(&new_lfp->lf_deleg);
3710 LIST_INIT(&new_lfp->lf_locallock);
3711 LIST_INIT(&new_lfp->lf_rollback);
3712 new_lfp->lf_locallock_lck.nfslock_usecnt = 0;
3713 new_lfp->lf_locallock_lck.nfslock_lock = 0;
3714 new_lfp->lf_usecount = 0;
3715 LIST_INSERT_HEAD(hp, new_lfp, lf_hash);
3722 * This function adds a nfslock lock structure to the list for the associated
3723 * nfsstate and nfslockfile structures. It will be inserted after the
3724 * entry pointed at by insert_lop.
3727 nfsrv_insertlock(struct nfslock *new_lop, struct nfslock *insert_lop,
3728 struct nfsstate *stp, struct nfslockfile *lfp)
3730 struct nfslock *lop, *nlop;
3732 new_lop->lo_stp = stp;
3733 new_lop->lo_lfp = lfp;
3736 /* Insert in increasing lo_first order */
3737 lop = LIST_FIRST(&lfp->lf_lock);
3738 if (lop == LIST_END(&lfp->lf_lock) ||
3739 new_lop->lo_first <= lop->lo_first) {
3740 LIST_INSERT_HEAD(&lfp->lf_lock, new_lop, lo_lckfile);
3742 nlop = LIST_NEXT(lop, lo_lckfile);
3743 while (nlop != LIST_END(&lfp->lf_lock) &&
3744 nlop->lo_first < new_lop->lo_first) {
3746 nlop = LIST_NEXT(lop, lo_lckfile);
3748 LIST_INSERT_AFTER(lop, new_lop, lo_lckfile);
3751 new_lop->lo_lckfile.le_prev = NULL; /* list not used */
3755 * Insert after insert_lop, which is overloaded as stp or lfp for
3758 if (stp == NULL && (struct nfslockfile *)insert_lop == lfp)
3759 LIST_INSERT_HEAD(&lfp->lf_locallock, new_lop, lo_lckowner);
3760 else if ((struct nfsstate *)insert_lop == stp)
3761 LIST_INSERT_HEAD(&stp->ls_lock, new_lop, lo_lckowner);
3763 LIST_INSERT_AFTER(insert_lop, new_lop, lo_lckowner);
3765 newnfsstats.srvlocks++;
3766 nfsrv_openpluslock++;
3771 * This function updates the locking for a lock owner and given file. It
3772 * maintains a list of lock ranges ordered on increasing file offset that
3773 * are NFSLCK_READ or NFSLCK_WRITE and non-overlapping (aka POSIX style).
3774 * It always adds new_lop to the list and sometimes uses the one pointed
3778 nfsrv_updatelock(struct nfsstate *stp, struct nfslock **new_lopp,
3779 struct nfslock **other_lopp, struct nfslockfile *lfp)
3781 struct nfslock *new_lop = *new_lopp;
3782 struct nfslock *lop, *tlop, *ilop;
3783 struct nfslock *other_lop = *other_lopp;
3784 int unlock = 0, myfile = 0;
3788 * Work down the list until the lock is merged.
3790 if (new_lop->lo_flags & NFSLCK_UNLOCK)
3793 ilop = (struct nfslock *)stp;
3794 lop = LIST_FIRST(&stp->ls_lock);
3796 ilop = (struct nfslock *)lfp;
3797 lop = LIST_FIRST(&lfp->lf_locallock);
3799 while (lop != NULL) {
3801 * Only check locks for this file that aren't before the start of
3804 if (lop->lo_lfp == lfp) {
3806 if (lop->lo_end >= new_lop->lo_first) {
3807 if (new_lop->lo_end < lop->lo_first) {
3809 * If the new lock ends before the start of the
3810 * current lock's range, no merge, just insert
3815 if (new_lop->lo_flags == lop->lo_flags ||
3816 (new_lop->lo_first <= lop->lo_first &&
3817 new_lop->lo_end >= lop->lo_end)) {
3819 * This lock can be absorbed by the new lock/unlock.
3820 * This happens when it covers the entire range
3821 * of the old lock or is contiguous
3822 * with the old lock and is of the same type or an
3825 if (lop->lo_first < new_lop->lo_first)
3826 new_lop->lo_first = lop->lo_first;
3827 if (lop->lo_end > new_lop->lo_end)
3828 new_lop->lo_end = lop->lo_end;
3830 lop = LIST_NEXT(lop, lo_lckowner);
3831 nfsrv_freenfslock(tlop);
3836 * All these cases are for contiguous locks that are not the
3837 * same type, so they can't be merged.
3839 if (new_lop->lo_first <= lop->lo_first) {
3841 * This case is where the new lock overlaps with the
3842 * first part of the old lock. Move the start of the
3843 * old lock to just past the end of the new lock. The
3844 * new lock will be inserted in front of the old, since
3845 * ilop hasn't been updated. (We are done now.)
3847 lop->lo_first = new_lop->lo_end;
3850 if (new_lop->lo_end >= lop->lo_end) {
3852 * This case is where the new lock overlaps with the
3853 * end of the old lock's range. Move the old lock's
3854 * end to just before the new lock's first and insert
3855 * the new lock after the old lock.
3856 * Might not be done yet, since the new lock could
3857 * overlap further locks with higher ranges.
3859 lop->lo_end = new_lop->lo_first;
3861 lop = LIST_NEXT(lop, lo_lckowner);
3865 * The final case is where the new lock's range is in the
3866 * middle of the current lock's and splits the current lock
3867 * up. Use *other_lopp to handle the second part of the
3868 * split old lock range. (We are done now.)
3869 * For unlock, we use new_lop as other_lop and tmp, since
3870 * other_lop and new_lop are the same for this case.
3871 * We noted the unlock case above, so we don't need
3872 * new_lop->lo_flags any longer.
3874 tmp = new_lop->lo_first;
3875 if (other_lop == NULL) {
3877 panic("nfsd srv update unlock");
3878 other_lop = new_lop;
3881 other_lop->lo_first = new_lop->lo_end;
3882 other_lop->lo_end = lop->lo_end;
3883 other_lop->lo_flags = lop->lo_flags;
3884 other_lop->lo_stp = stp;
3885 other_lop->lo_lfp = lfp;
3887 nfsrv_insertlock(other_lop, lop, stp, lfp);
3894 lop = LIST_NEXT(lop, lo_lckowner);
3895 if (myfile && (lop == NULL || lop->lo_lfp != lfp))
3900 * Insert the new lock in the list at the appropriate place.
3903 nfsrv_insertlock(new_lop, ilop, stp, lfp);
3909 * This function handles sequencing of locks, etc.
3910 * It returns an error that indicates what the caller should do.
3913 nfsrv_checkseqid(struct nfsrv_descript *nd, u_int32_t seqid,
3914 struct nfsstate *stp, struct nfsrvcache *op)
3918 if ((nd->nd_flag & ND_NFSV41) != 0)
3919 /* NFSv4.1 ignores the open_seqid and lock_seqid. */
3921 if (op != nd->nd_rp)
3922 panic("nfsrvstate checkseqid");
3923 if (!(op->rc_flag & RC_INPROG))
3924 panic("nfsrvstate not inprog");
3925 if (stp->ls_op && stp->ls_op->rc_refcnt <= 0) {
3926 printf("refcnt=%d\n", stp->ls_op->rc_refcnt);
3927 panic("nfsrvstate op refcnt");
3929 if ((stp->ls_seq + 1) == seqid) {
3931 nfsrvd_derefcache(stp->ls_op);
3933 nfsrvd_refcache(op);
3934 stp->ls_seq = seqid;
3936 } else if (stp->ls_seq == seqid && stp->ls_op &&
3937 op->rc_xid == stp->ls_op->rc_xid &&
3938 op->rc_refcnt == 0 &&
3939 op->rc_reqlen == stp->ls_op->rc_reqlen &&
3940 op->rc_cksum == stp->ls_op->rc_cksum) {
3941 if (stp->ls_op->rc_flag & RC_INPROG) {
3942 error = NFSERR_DONTREPLY;
3945 nd->nd_rp = stp->ls_op;
3946 nd->nd_rp->rc_flag |= RC_INPROG;
3947 nfsrvd_delcache(op);
3948 error = NFSERR_REPLYFROMCACHE;
3951 error = NFSERR_BADSEQID;
3954 NFSEXITCODE2(error, nd);
3959 * Get the client ip address for callbacks. If the strings can't be parsed,
3960 * just set lc_program to 0 to indicate no callbacks are possible.
3961 * (For cases where the address can't be parsed or is 0.0.0.0.0.0, set
3962 * the address to the client's transport address. This won't be used
3963 * for callbacks, but can be printed out by newnfsstats for info.)
3964 * Return error if the xdr can't be parsed, 0 otherwise.
3967 nfsrv_getclientipaddr(struct nfsrv_descript *nd, struct nfsclient *clp)
3971 int i, j, maxalen = 0, minalen = 0;
3974 struct sockaddr_in *rin, *sin;
3977 struct sockaddr_in6 *rin6, *sin6;
3980 int error = 0, cantparse = 0;
3990 /* 8 is the maximum length of the port# string. */
3991 addr = malloc(INET6_ADDRSTRLEN + 8, M_TEMP, M_WAITOK);
3992 clp->lc_req.nr_client = NULL;
3993 clp->lc_req.nr_lock = 0;
3995 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
3996 i = fxdr_unsigned(int, *tl);
3997 if (i >= 3 && i <= 4) {
3998 error = nfsrv_mtostr(nd, addr, i);
4002 if (!strcmp(addr, "tcp")) {
4003 clp->lc_flags |= LCL_TCPCALLBACK;
4004 clp->lc_req.nr_sotype = SOCK_STREAM;
4005 clp->lc_req.nr_soproto = IPPROTO_TCP;
4007 } else if (!strcmp(addr, "udp")) {
4008 clp->lc_req.nr_sotype = SOCK_DGRAM;
4009 clp->lc_req.nr_soproto = IPPROTO_UDP;
4014 if (af == AF_UNSPEC) {
4015 if (!strcmp(addr, "tcp6")) {
4016 clp->lc_flags |= LCL_TCPCALLBACK;
4017 clp->lc_req.nr_sotype = SOCK_STREAM;
4018 clp->lc_req.nr_soproto = IPPROTO_TCP;
4020 } else if (!strcmp(addr, "udp6")) {
4021 clp->lc_req.nr_sotype = SOCK_DGRAM;
4022 clp->lc_req.nr_soproto = IPPROTO_UDP;
4027 if (af == AF_UNSPEC) {
4033 error = nfsm_advance(nd, NFSM_RNDUP(i), -1);
4039 * The caller has allocated clp->lc_req.nr_nam to be large enough
4040 * for either AF_INET or AF_INET6 and zeroed out the contents.
4041 * maxalen is set to the maximum length of the host IP address string
4042 * plus 8 for the maximum length of the port#.
4043 * minalen is set to the minimum length of the host IP address string
4044 * plus 4 for the minimum length of the port#.
4045 * These lengths do not include NULL termination,
4046 * so INET[6]_ADDRSTRLEN - 1 is used in the calculations.
4051 rin = (struct sockaddr_in *)clp->lc_req.nr_nam;
4052 rin->sin_family = AF_INET;
4053 rin->sin_len = sizeof(struct sockaddr_in);
4054 maxalen = INET_ADDRSTRLEN - 1 + 8;
4060 rin6 = (struct sockaddr_in6 *)clp->lc_req.nr_nam;
4061 rin6->sin6_family = AF_INET6;
4062 rin6->sin6_len = sizeof(struct sockaddr_in6);
4063 maxalen = INET6_ADDRSTRLEN - 1 + 8;
4068 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
4069 i = fxdr_unsigned(int, *tl);
4071 error = NFSERR_BADXDR;
4073 } else if (i == 0) {
4075 } else if (!cantparse && i <= maxalen && i >= minalen) {
4076 error = nfsrv_mtostr(nd, addr, i);
4081 * Parse out the address fields. We expect 6 decimal numbers
4082 * separated by '.'s for AF_INET and two decimal numbers
4083 * preceeded by '.'s for AF_INET6.
4089 * For AF_INET6, first parse the host address.
4092 cp = strchr(addr, '.');
4095 if (inet_pton(af, addr, &rin6->sin6_addr) == 1)
4111 while (cp != NULL && *cp && i < 6) {
4113 while (*cp2 && *cp2 != '.')
4121 j = nfsrv_getipnumber(cp);
4126 port.cval[5 - i] = j;
4136 * The host address INADDR_ANY is (mis)used to indicate
4137 * "there is no valid callback address".
4142 if (!IN6_ARE_ADDR_EQUAL(&rin6->sin6_addr,
4144 rin6->sin6_port = htons(port.sval);
4151 if (ip.ival != INADDR_ANY) {
4152 rin->sin_addr.s_addr = htonl(ip.ival);
4153 rin->sin_port = htons(port.sval);
4164 error = nfsm_advance(nd, NFSM_RNDUP(i), -1);
4170 switch (nd->nd_nam->sa_family) {
4173 sin = (struct sockaddr_in *)nd->nd_nam;
4174 rin = (struct sockaddr_in *)clp->lc_req.nr_nam;
4175 rin->sin_family = AF_INET;
4176 rin->sin_len = sizeof(struct sockaddr_in);
4177 rin->sin_addr.s_addr = sin->sin_addr.s_addr;
4178 rin->sin_port = 0x0;
4183 sin6 = (struct sockaddr_in6 *)nd->nd_nam;
4184 rin6 = (struct sockaddr_in6 *)clp->lc_req.nr_nam;
4185 rin6->sin6_family = AF_INET6;
4186 rin6->sin6_len = sizeof(struct sockaddr_in6);
4187 rin6->sin6_addr = sin6->sin6_addr;
4188 rin6->sin6_port = 0x0;
4192 clp->lc_program = 0;
4196 NFSEXITCODE2(error, nd);
4201 * Turn a string of up to three decimal digits into a number. Return -1 upon
4205 nfsrv_getipnumber(u_char *cp)
4210 if (j > 2 || *cp < '0' || *cp > '9')
4223 * This function checks for restart conditions.
4226 nfsrv_checkrestart(nfsquad_t clientid, u_int32_t flags,
4227 nfsv4stateid_t *stateidp, int specialid)
4232 * First check for a server restart. Open, LockT, ReleaseLockOwner
4233 * and DelegPurge have a clientid, the rest a stateid.
4236 (NFSLCK_OPEN | NFSLCK_TEST | NFSLCK_RELEASE | NFSLCK_DELEGPURGE)) {
4237 if (clientid.lval[0] != nfsrvboottime) {
4238 ret = NFSERR_STALECLIENTID;
4241 } else if (stateidp->other[0] != nfsrvboottime &&
4243 ret = NFSERR_STALESTATEID;
4248 * Read, Write, Setattr and LockT can return NFSERR_GRACE and do
4249 * not use a lock/open owner seqid#, so the check can be done now.
4250 * (The others will be checked, as required, later.)
4252 if (!(flags & (NFSLCK_CHECK | NFSLCK_TEST)))
4256 ret = nfsrv_checkgrace(NULL, NULL, flags);
4268 nfsrv_checkgrace(struct nfsrv_descript *nd, struct nfsclient *clp,
4271 int error = 0, notreclaimed;
4272 struct nfsrv_stable *sp;
4274 if ((nfsrv_stablefirst.nsf_flags & (NFSNSF_UPDATEDONE |
4275 NFSNSF_GRACEOVER)) == 0) {
4277 * First, check to see if all of the clients have done a
4278 * ReclaimComplete. If so, grace can end now.
4281 LIST_FOREACH(sp, &nfsrv_stablefirst.nsf_head, nst_list) {
4282 if ((sp->nst_flag & NFSNST_RECLAIMED) == 0) {
4287 if (notreclaimed == 0)
4288 nfsrv_stablefirst.nsf_flags |= (NFSNSF_GRACEOVER |
4292 if ((nfsrv_stablefirst.nsf_flags & NFSNSF_GRACEOVER) != 0) {
4293 if (flags & NFSLCK_RECLAIM) {
4294 error = NFSERR_NOGRACE;
4298 if (!(flags & NFSLCK_RECLAIM)) {
4299 error = NFSERR_GRACE;
4302 if (nd != NULL && clp != NULL &&
4303 (nd->nd_flag & ND_NFSV41) != 0 &&
4304 (clp->lc_flags & LCL_RECLAIMCOMPLETE) != 0) {
4305 error = NFSERR_NOGRACE;
4310 * If grace is almost over and we are still getting Reclaims,
4311 * extend grace a bit.
4313 if ((NFSD_MONOSEC + NFSRV_LEASEDELTA) >
4314 nfsrv_stablefirst.nsf_eograce)
4315 nfsrv_stablefirst.nsf_eograce = NFSD_MONOSEC +
4325 * Do a server callback.
4328 nfsrv_docallback(struct nfsclient *clp, int procnum,
4329 nfsv4stateid_t *stateidp, int trunc, fhandle_t *fhp,
4330 struct nfsvattr *nap, nfsattrbit_t *attrbitp, NFSPROC_T *p)
4334 struct nfsrv_descript nfsd, *nd = &nfsd;
4338 struct nfsdsession *sep = NULL;
4340 cred = newnfs_getcred();
4341 NFSLOCKSTATE(); /* mostly for lc_cbref++ */
4342 if (clp->lc_flags & LCL_NEEDSCONFIRM) {
4349 * Fill the callback program# and version into the request
4350 * structure for newnfs_connect() to use.
4352 clp->lc_req.nr_prog = clp->lc_program;
4354 if ((clp->lc_flags & LCL_NFSV41) != 0)
4355 clp->lc_req.nr_vers = NFSV41_CBVERS;
4358 clp->lc_req.nr_vers = NFSV4_CBVERS;
4361 * First, fill in some of the fields of nd and cr.
4363 nd->nd_flag = ND_NFSV4;
4364 if (clp->lc_flags & LCL_GSS)
4365 nd->nd_flag |= ND_KERBV;
4366 if ((clp->lc_flags & LCL_NFSV41) != 0)
4367 nd->nd_flag |= ND_NFSV41;
4369 cred->cr_uid = clp->lc_uid;
4370 cred->cr_gid = clp->lc_gid;
4371 callback = clp->lc_callback;
4373 cred->cr_ngroups = 1;
4376 * Get the first mbuf for the request.
4378 MGET(m, M_WAITOK, MT_DATA);
4380 nd->nd_mreq = nd->nd_mb = m;
4381 nd->nd_bpos = NFSMTOD(m, caddr_t);
4384 * and build the callback request.
4386 if (procnum == NFSV4OP_CBGETATTR) {
4387 nd->nd_procnum = NFSV4PROC_CBCOMPOUND;
4388 error = nfsrv_cbcallargs(nd, clp, callback, NFSV4OP_CBGETATTR,
4389 "CB Getattr", &sep);
4391 mbuf_freem(nd->nd_mreq);
4394 (void)nfsm_fhtom(nd, (u_int8_t *)fhp, NFSX_MYFH, 0);
4395 (void)nfsrv_putattrbit(nd, attrbitp);
4396 } else if (procnum == NFSV4OP_CBRECALL) {
4397 nd->nd_procnum = NFSV4PROC_CBCOMPOUND;
4398 error = nfsrv_cbcallargs(nd, clp, callback, NFSV4OP_CBRECALL,
4401 mbuf_freem(nd->nd_mreq);
4404 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED + NFSX_STATEID);
4405 *tl++ = txdr_unsigned(stateidp->seqid);
4406 NFSBCOPY((caddr_t)stateidp->other, (caddr_t)tl,
4408 tl += (NFSX_STATEIDOTHER / NFSX_UNSIGNED);
4413 (void)nfsm_fhtom(nd, (u_int8_t *)fhp, NFSX_MYFH, 0);
4414 } else if (procnum == NFSV4PROC_CBNULL) {
4415 nd->nd_procnum = NFSV4PROC_CBNULL;
4416 if ((clp->lc_flags & LCL_NFSV41) != 0) {
4417 error = nfsv4_getcbsession(clp, &sep);
4419 mbuf_freem(nd->nd_mreq);
4424 error = NFSERR_SERVERFAULT;
4425 mbuf_freem(nd->nd_mreq);
4430 * Call newnfs_connect(), as required, and then newnfs_request().
4432 (void) newnfs_sndlock(&clp->lc_req.nr_lock);
4433 if (clp->lc_req.nr_client == NULL) {
4434 if ((clp->lc_flags & LCL_NFSV41) != 0) {
4435 error = ECONNREFUSED;
4436 nfsrv_freesession(sep, NULL);
4437 } else if (nd->nd_procnum == NFSV4PROC_CBNULL)
4438 error = newnfs_connect(NULL, &clp->lc_req, cred,
4441 error = newnfs_connect(NULL, &clp->lc_req, cred,
4444 newnfs_sndunlock(&clp->lc_req.nr_lock);
4446 if ((nd->nd_flag & ND_NFSV41) != 0) {
4447 KASSERT(sep != NULL, ("sep NULL"));
4448 if (sep->sess_cbsess.nfsess_xprt != NULL)
4449 error = newnfs_request(nd, NULL, clp,
4450 &clp->lc_req, NULL, NULL, cred,
4451 clp->lc_program, clp->lc_req.nr_vers, NULL,
4452 1, NULL, &sep->sess_cbsess);
4455 * This should probably never occur, but if a
4456 * client somehow does an RPC without a
4457 * SequenceID Op that causes a callback just
4458 * after the nfsd threads have been terminated
4459 * and restared we could conceivably get here
4460 * without a backchannel xprt.
4462 printf("nfsrv_docallback: no xprt\n");
4463 error = ECONNREFUSED;
4465 nfsrv_freesession(sep, NULL);
4467 error = newnfs_request(nd, NULL, clp, &clp->lc_req,
4468 NULL, NULL, cred, clp->lc_program,
4469 clp->lc_req.nr_vers, NULL, 1, NULL, NULL);
4475 * If error is set here, the Callback path isn't working
4476 * properly, so twiddle the appropriate LCL_ flags.
4477 * (nd_repstat != 0 indicates the Callback path is working,
4478 * but the callback failed on the client.)
4482 * Mark the callback pathway down, which disabled issuing
4483 * of delegations and gets Renew to return NFSERR_CBPATHDOWN.
4486 clp->lc_flags |= LCL_CBDOWN;
4490 * Callback worked. If the callback path was down, disable
4491 * callbacks, so no more delegations will be issued. (This
4492 * is done on the assumption that the callback pathway is
4496 if (clp->lc_flags & LCL_CBDOWN)
4497 clp->lc_flags &= ~(LCL_CBDOWN | LCL_CALLBACKSON);
4500 error = nd->nd_repstat;
4501 else if (error == 0 && procnum == NFSV4OP_CBGETATTR)
4502 error = nfsv4_loadattr(nd, NULL, nap, NULL, NULL, 0,
4503 NULL, NULL, NULL, NULL, NULL, 0, NULL, NULL, NULL,
4505 mbuf_freem(nd->nd_mrep);
4509 if ((clp->lc_flags & LCL_WAKEUPWANTED) && clp->lc_cbref == 0) {
4510 clp->lc_flags &= ~LCL_WAKEUPWANTED;
4520 * Set up the compound RPC for the callback.
4523 nfsrv_cbcallargs(struct nfsrv_descript *nd, struct nfsclient *clp,
4524 uint32_t callback, int op, const char *optag, struct nfsdsession **sepp)
4529 len = strlen(optag);
4530 (void)nfsm_strtom(nd, optag, len);
4531 NFSM_BUILD(tl, uint32_t *, 4 * NFSX_UNSIGNED);
4532 if ((nd->nd_flag & ND_NFSV41) != 0) {
4533 *tl++ = txdr_unsigned(NFSV41_MINORVERSION);
4534 *tl++ = txdr_unsigned(callback);
4535 *tl++ = txdr_unsigned(2);
4536 *tl = txdr_unsigned(NFSV4OP_CBSEQUENCE);
4537 error = nfsv4_setcbsequence(nd, clp, 1, sepp);
4540 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
4541 *tl = txdr_unsigned(op);
4543 *tl++ = txdr_unsigned(NFSV4_MINORVERSION);
4544 *tl++ = txdr_unsigned(callback);
4545 *tl++ = txdr_unsigned(1);
4546 *tl = txdr_unsigned(op);
4552 * Return the next index# for a clientid. Mostly just increment and return
4553 * the next one, but... if the 32bit unsigned does actually wrap around,
4554 * it should be rebooted.
4555 * At an average rate of one new client per second, it will wrap around in
4556 * approximately 136 years. (I think the server will have been shut
4557 * down or rebooted before then.)
4560 nfsrv_nextclientindex(void)
4562 static u_int32_t client_index = 0;
4565 if (client_index != 0)
4566 return (client_index);
4568 printf("%s: out of clientids\n", __func__);
4569 return (client_index);
4573 * Return the next index# for a stateid. Mostly just increment and return
4574 * the next one, but... if the 32bit unsigned does actually wrap around
4575 * (will a BSD server stay up that long?), find
4576 * new start and end values.
4579 nfsrv_nextstateindex(struct nfsclient *clp)
4581 struct nfsstate *stp;
4583 u_int32_t canuse, min_index, max_index;
4585 if (!(clp->lc_flags & LCL_INDEXNOTOK)) {
4586 clp->lc_stateindex++;
4587 if (clp->lc_stateindex != clp->lc_statemaxindex)
4588 return (clp->lc_stateindex);
4592 * Yuck, we've hit the end.
4593 * Look for a new min and max.
4596 max_index = 0xffffffff;
4597 for (i = 0; i < nfsrv_statehashsize; i++) {
4598 LIST_FOREACH(stp, &clp->lc_stateid[i], ls_hash) {
4599 if (stp->ls_stateid.other[2] > 0x80000000) {
4600 if (stp->ls_stateid.other[2] < max_index)
4601 max_index = stp->ls_stateid.other[2];
4603 if (stp->ls_stateid.other[2] > min_index)
4604 min_index = stp->ls_stateid.other[2];
4610 * Yikes, highly unlikely, but I'll handle it anyhow.
4612 if (min_index == 0x80000000 && max_index == 0x80000001) {
4615 * Loop around until we find an unused entry. Return that
4616 * and set LCL_INDEXNOTOK, so the search will continue next time.
4617 * (This is one of those rare cases where a goto is the
4618 * cleanest way to code the loop.)
4621 for (i = 0; i < nfsrv_statehashsize; i++) {
4622 LIST_FOREACH(stp, &clp->lc_stateid[i], ls_hash) {
4623 if (stp->ls_stateid.other[2] == canuse) {
4629 clp->lc_flags |= LCL_INDEXNOTOK;
4634 * Ok to start again from min + 1.
4636 clp->lc_stateindex = min_index + 1;
4637 clp->lc_statemaxindex = max_index;
4638 clp->lc_flags &= ~LCL_INDEXNOTOK;
4639 return (clp->lc_stateindex);
4643 * The following functions handle the stable storage file that deals with
4644 * the edge conditions described in RFC3530 Sec. 8.6.3.
4645 * The file is as follows:
4646 * - a single record at the beginning that has the lease time of the
4647 * previous server instance (before the last reboot) and the nfsrvboottime
4648 * values for the previous server boots.
4649 * These previous boot times are used to ensure that the current
4650 * nfsrvboottime does not, somehow, get set to a previous one.
4651 * (This is important so that Stale ClientIDs and StateIDs can
4653 * The number of previous nfsvrboottime values preceeds the list.
4654 * - followed by some number of appended records with:
4655 * - client id string
4656 * - flag that indicates it is a record revoking state via lease
4657 * expiration or similar
4658 * OR has successfully acquired state.
4659 * These structures vary in length, with the client string at the end, up
4660 * to NFSV4_OPAQUELIMIT in size.
4662 * At the end of the grace period, the file is truncated, the first
4663 * record is rewritten with updated information and any acquired state
4664 * records for successful reclaims of state are written.
4666 * Subsequent records are appended when the first state is issued to
4667 * a client and when state is revoked for a client.
4669 * When reading the file in, state issued records that come later in
4670 * the file override older ones, since the append log is in cronological order.
4671 * If, for some reason, the file can't be read, the grace period is
4672 * immediately terminated and all reclaims get NFSERR_NOGRACE.
4676 * Read in the stable storage file. Called by nfssvc() before the nfsd
4677 * processes start servicing requests.
4680 nfsrv_setupstable(NFSPROC_T *p)
4682 struct nfsrv_stablefirst *sf = &nfsrv_stablefirst;
4683 struct nfsrv_stable *sp, *nsp;
4684 struct nfst_rec *tsp;
4685 int error, i, tryagain;
4687 ssize_t aresid, len;
4690 * If NFSNSF_UPDATEDONE is set, this is a restart of the nfsds without
4691 * a reboot, so state has not been lost.
4693 if (sf->nsf_flags & NFSNSF_UPDATEDONE)
4696 * Set Grace over just until the file reads successfully.
4698 nfsrvboottime = time_second;
4699 LIST_INIT(&sf->nsf_head);
4700 sf->nsf_flags = (NFSNSF_GRACEOVER | NFSNSF_NEEDLOCK);
4701 sf->nsf_eograce = NFSD_MONOSEC + NFSRV_LEASEDELTA;
4702 if (sf->nsf_fp == NULL)
4704 error = NFSD_RDWR(UIO_READ, NFSFPVNODE(sf->nsf_fp),
4705 (caddr_t)&sf->nsf_rec, sizeof (struct nfsf_rec), off, UIO_SYSSPACE,
4706 0, NFSFPCRED(sf->nsf_fp), &aresid, p);
4707 if (error || aresid || sf->nsf_numboots == 0 ||
4708 sf->nsf_numboots > NFSNSF_MAXNUMBOOTS)
4712 * Now, read in the boottimes.
4714 sf->nsf_bootvals = (time_t *)malloc((sf->nsf_numboots + 1) *
4715 sizeof (time_t), M_TEMP, M_WAITOK);
4716 off = sizeof (struct nfsf_rec);
4717 error = NFSD_RDWR(UIO_READ, NFSFPVNODE(sf->nsf_fp),
4718 (caddr_t)sf->nsf_bootvals, sf->nsf_numboots * sizeof (time_t), off,
4719 UIO_SYSSPACE, 0, NFSFPCRED(sf->nsf_fp), &aresid, p);
4720 if (error || aresid) {
4721 free((caddr_t)sf->nsf_bootvals, M_TEMP);
4722 sf->nsf_bootvals = NULL;
4727 * Make sure this nfsrvboottime is different from all recorded
4732 for (i = 0; i < sf->nsf_numboots; i++) {
4733 if (nfsrvboottime == sf->nsf_bootvals[i]) {
4741 sf->nsf_flags |= NFSNSF_OK;
4742 off += (sf->nsf_numboots * sizeof (time_t));
4745 * Read through the file, building a list of records for grace
4747 * Each record is between sizeof (struct nfst_rec) and
4748 * sizeof (struct nfst_rec) + NFSV4_OPAQUELIMIT - 1
4749 * and is actually sizeof (struct nfst_rec) + nst_len - 1.
4751 tsp = (struct nfst_rec *)malloc(sizeof (struct nfst_rec) +
4752 NFSV4_OPAQUELIMIT - 1, M_TEMP, M_WAITOK);
4754 error = NFSD_RDWR(UIO_READ, NFSFPVNODE(sf->nsf_fp),
4755 (caddr_t)tsp, sizeof (struct nfst_rec) + NFSV4_OPAQUELIMIT - 1,
4756 off, UIO_SYSSPACE, 0, NFSFPCRED(sf->nsf_fp), &aresid, p);
4757 len = (sizeof (struct nfst_rec) + NFSV4_OPAQUELIMIT - 1) - aresid;
4758 if (error || (len > 0 && (len < sizeof (struct nfst_rec) ||
4759 len < (sizeof (struct nfst_rec) + tsp->len - 1)))) {
4761 * Yuck, the file has been corrupted, so just return
4762 * after clearing out any restart state, so the grace period
4765 LIST_FOREACH_SAFE(sp, &sf->nsf_head, nst_list, nsp) {
4766 LIST_REMOVE(sp, nst_list);
4767 free((caddr_t)sp, M_TEMP);
4769 free((caddr_t)tsp, M_TEMP);
4770 sf->nsf_flags &= ~NFSNSF_OK;
4771 free((caddr_t)sf->nsf_bootvals, M_TEMP);
4772 sf->nsf_bootvals = NULL;
4776 off += sizeof (struct nfst_rec) + tsp->len - 1;
4778 * Search the list for a matching client.
4780 LIST_FOREACH(sp, &sf->nsf_head, nst_list) {
4781 if (tsp->len == sp->nst_len &&
4782 !NFSBCMP(tsp->client, sp->nst_client, tsp->len))
4785 if (sp == LIST_END(&sf->nsf_head)) {
4786 sp = (struct nfsrv_stable *)malloc(tsp->len +
4787 sizeof (struct nfsrv_stable) - 1, M_TEMP,
4789 NFSBCOPY((caddr_t)tsp, (caddr_t)&sp->nst_rec,
4790 sizeof (struct nfst_rec) + tsp->len - 1);
4791 LIST_INSERT_HEAD(&sf->nsf_head, sp, nst_list);
4793 if (tsp->flag == NFSNST_REVOKE)
4794 sp->nst_flag |= NFSNST_REVOKE;
4797 * A subsequent timestamp indicates the client
4798 * did a setclientid/confirm and any previous
4799 * revoke is no longer relevant.
4801 sp->nst_flag &= ~NFSNST_REVOKE;
4805 free((caddr_t)tsp, M_TEMP);
4806 sf->nsf_flags = NFSNSF_OK;
4807 sf->nsf_eograce = NFSD_MONOSEC + sf->nsf_lease +
4812 * Update the stable storage file, now that the grace period is over.
4815 nfsrv_updatestable(NFSPROC_T *p)
4817 struct nfsrv_stablefirst *sf = &nfsrv_stablefirst;
4818 struct nfsrv_stable *sp, *nsp;
4820 struct nfsvattr nva;
4822 #if defined(__FreeBSD_version) && (__FreeBSD_version >= 500000)
4827 if (sf->nsf_fp == NULL || (sf->nsf_flags & NFSNSF_UPDATEDONE))
4829 sf->nsf_flags |= NFSNSF_UPDATEDONE;
4831 * Ok, we need to rewrite the stable storage file.
4832 * - truncate to 0 length
4833 * - write the new first structure
4834 * - loop through the data structures, writing out any that
4835 * have timestamps older than the old boot
4837 if (sf->nsf_bootvals) {
4839 for (i = sf->nsf_numboots - 2; i >= 0; i--)
4840 sf->nsf_bootvals[i + 1] = sf->nsf_bootvals[i];
4842 sf->nsf_numboots = 1;
4843 sf->nsf_bootvals = (time_t *)malloc(sizeof (time_t),
4846 sf->nsf_bootvals[0] = nfsrvboottime;
4847 sf->nsf_lease = nfsrv_lease;
4848 NFSVNO_ATTRINIT(&nva);
4849 NFSVNO_SETATTRVAL(&nva, size, 0);
4850 vp = NFSFPVNODE(sf->nsf_fp);
4851 vn_start_write(vp, &mp, V_WAIT);
4852 if (NFSVOPLOCK(vp, LK_EXCLUSIVE) == 0) {
4853 error = nfsvno_setattr(vp, &nva, NFSFPCRED(sf->nsf_fp), p,
4855 NFSVOPUNLOCK(vp, 0);
4858 vn_finished_write(mp);
4860 error = NFSD_RDWR(UIO_WRITE, vp,
4861 (caddr_t)&sf->nsf_rec, sizeof (struct nfsf_rec), (off_t)0,
4862 UIO_SYSSPACE, IO_SYNC, NFSFPCRED(sf->nsf_fp), NULL, p);
4864 error = NFSD_RDWR(UIO_WRITE, vp,
4865 (caddr_t)sf->nsf_bootvals,
4866 sf->nsf_numboots * sizeof (time_t),
4867 (off_t)(sizeof (struct nfsf_rec)),
4868 UIO_SYSSPACE, IO_SYNC, NFSFPCRED(sf->nsf_fp), NULL, p);
4869 free((caddr_t)sf->nsf_bootvals, M_TEMP);
4870 sf->nsf_bootvals = NULL;
4872 sf->nsf_flags &= ~NFSNSF_OK;
4873 printf("EEK! Can't write NfsV4 stable storage file\n");
4876 sf->nsf_flags |= NFSNSF_OK;
4879 * Loop through the list and write out timestamp records for
4880 * any clients that successfully reclaimed state.
4882 LIST_FOREACH_SAFE(sp, &sf->nsf_head, nst_list, nsp) {
4883 if (sp->nst_flag & NFSNST_GOTSTATE) {
4884 nfsrv_writestable(sp->nst_client, sp->nst_len,
4885 NFSNST_NEWSTATE, p);
4886 sp->nst_clp->lc_flags |= LCL_STAMPEDSTABLE;
4888 LIST_REMOVE(sp, nst_list);
4889 free((caddr_t)sp, M_TEMP);
4891 nfsrv_backupstable();
4895 * Append a record to the stable storage file.
4898 nfsrv_writestable(u_char *client, int len, int flag, NFSPROC_T *p)
4900 struct nfsrv_stablefirst *sf = &nfsrv_stablefirst;
4901 struct nfst_rec *sp;
4904 if (!(sf->nsf_flags & NFSNSF_OK) || sf->nsf_fp == NULL)
4906 sp = (struct nfst_rec *)malloc(sizeof (struct nfst_rec) +
4907 len - 1, M_TEMP, M_WAITOK);
4909 NFSBCOPY(client, sp->client, len);
4911 error = NFSD_RDWR(UIO_WRITE, NFSFPVNODE(sf->nsf_fp),
4912 (caddr_t)sp, sizeof (struct nfst_rec) + len - 1, (off_t)0,
4913 UIO_SYSSPACE, (IO_SYNC | IO_APPEND), NFSFPCRED(sf->nsf_fp), NULL, p);
4914 free((caddr_t)sp, M_TEMP);
4916 sf->nsf_flags &= ~NFSNSF_OK;
4917 printf("EEK! Can't write NfsV4 stable storage file\n");
4922 * This function is called during the grace period to mark a client
4923 * that successfully reclaimed state.
4926 nfsrv_markstable(struct nfsclient *clp)
4928 struct nfsrv_stable *sp;
4931 * First find the client structure.
4933 LIST_FOREACH(sp, &nfsrv_stablefirst.nsf_head, nst_list) {
4934 if (sp->nst_len == clp->lc_idlen &&
4935 !NFSBCMP(sp->nst_client, clp->lc_id, sp->nst_len))
4938 if (sp == LIST_END(&nfsrv_stablefirst.nsf_head))
4942 * Now, just mark it and set the nfsclient back pointer.
4944 sp->nst_flag |= NFSNST_GOTSTATE;
4949 * This function is called when a NFSv4.1 client does a ReclaimComplete.
4950 * Very similar to nfsrv_markstable(), except for the flag being set.
4953 nfsrv_markreclaim(struct nfsclient *clp)
4955 struct nfsrv_stable *sp;
4958 * First find the client structure.
4960 LIST_FOREACH(sp, &nfsrv_stablefirst.nsf_head, nst_list) {
4961 if (sp->nst_len == clp->lc_idlen &&
4962 !NFSBCMP(sp->nst_client, clp->lc_id, sp->nst_len))
4965 if (sp == LIST_END(&nfsrv_stablefirst.nsf_head))
4969 * Now, just set the flag.
4971 sp->nst_flag |= NFSNST_RECLAIMED;
4975 * This function is called for a reclaim, to see if it gets grace.
4976 * It returns 0 if a reclaim is allowed, 1 otherwise.
4979 nfsrv_checkstable(struct nfsclient *clp)
4981 struct nfsrv_stable *sp;
4984 * First, find the entry for the client.
4986 LIST_FOREACH(sp, &nfsrv_stablefirst.nsf_head, nst_list) {
4987 if (sp->nst_len == clp->lc_idlen &&
4988 !NFSBCMP(sp->nst_client, clp->lc_id, sp->nst_len))
4993 * If not in the list, state was revoked or no state was issued
4994 * since the previous reboot, a reclaim is denied.
4996 if (sp == LIST_END(&nfsrv_stablefirst.nsf_head) ||
4997 (sp->nst_flag & NFSNST_REVOKE) ||
4998 !(nfsrv_stablefirst.nsf_flags & NFSNSF_OK))
5004 * Test for and try to clear out a conflicting client. This is called by
5005 * nfsrv_lockctrl() and nfsrv_openctrl() when conflicts with other clients
5007 * The trick here is that it can't revoke a conflicting client with an
5008 * expired lease unless it holds the v4root lock, so...
5009 * If no v4root lock, get the lock and return 1 to indicate "try again".
5010 * Return 0 to indicate the conflict can't be revoked and 1 to indicate
5011 * the revocation worked and the conflicting client is "bye, bye", so it
5012 * can be tried again.
5013 * Return 2 to indicate that the vnode is VI_DOOMED after NFSVOPLOCK().
5014 * Unlocks State before a non-zero value is returned.
5017 nfsrv_clientconflict(struct nfsclient *clp, int *haslockp, vnode_t vp,
5020 int gotlock, lktype = 0;
5023 * If lease hasn't expired, we can't fix it.
5025 if (clp->lc_expiry >= NFSD_MONOSEC ||
5026 !(nfsrv_stablefirst.nsf_flags & NFSNSF_UPDATEDONE))
5028 if (*haslockp == 0) {
5031 lktype = NFSVOPISLOCKED(vp);
5032 NFSVOPUNLOCK(vp, 0);
5034 NFSLOCKV4ROOTMUTEX();
5035 nfsv4_relref(&nfsv4rootfs_lock);
5037 gotlock = nfsv4_lock(&nfsv4rootfs_lock, 1, NULL,
5038 NFSV4ROOTLOCKMUTEXPTR, NULL);
5040 NFSUNLOCKV4ROOTMUTEX();
5043 NFSVOPLOCK(vp, lktype | LK_RETRY);
5044 if ((vp->v_iflag & VI_DOOMED) != 0)
5052 * Ok, we can expire the conflicting client.
5054 nfsrv_writestable(clp->lc_id, clp->lc_idlen, NFSNST_REVOKE, p);
5055 nfsrv_backupstable();
5056 nfsrv_cleanclient(clp, p);
5057 nfsrv_freedeleglist(&clp->lc_deleg);
5058 nfsrv_freedeleglist(&clp->lc_olddeleg);
5059 LIST_REMOVE(clp, lc_hash);
5060 nfsrv_zapclient(clp, p);
5065 * Resolve a delegation conflict.
5066 * Returns 0 to indicate the conflict was resolved without sleeping.
5067 * Return -1 to indicate that the caller should check for conflicts again.
5068 * Return > 0 for an error that should be returned, normally NFSERR_DELAY.
5070 * Also, manipulate the nfsv4root_lock, as required. It isn't changed
5071 * for a return of 0, since there was no sleep and it could be required
5072 * later. It is released for a return of NFSERR_DELAY, since the caller
5073 * will return that error. It is released when a sleep was done waiting
5074 * for the delegation to be returned or expire (so that other nfsds can
5075 * handle ops). Then, it must be acquired for the write to stable storage.
5076 * (This function is somewhat similar to nfsrv_clientconflict(), but
5077 * the semantics differ in a couple of subtle ways. The return of 0
5078 * indicates the conflict was resolved without sleeping here, not
5079 * that the conflict can't be resolved and the handling of nfsv4root_lock
5080 * differs, as noted above.)
5081 * Unlocks State before returning a non-zero value.
5084 nfsrv_delegconflict(struct nfsstate *stp, int *haslockp, NFSPROC_T *p,
5087 struct nfsclient *clp = stp->ls_clp;
5088 int gotlock, error, lktype = 0, retrycnt, zapped_clp;
5089 nfsv4stateid_t tstateid;
5093 * If the conflict is with an old delegation...
5095 if (stp->ls_flags & NFSLCK_OLDDELEG) {
5097 * You can delete it, if it has expired.
5099 if (clp->lc_delegtime < NFSD_MONOSEC) {
5100 nfsrv_freedeleg(stp);
5107 * During this delay, the old delegation could expire or it
5108 * could be recovered by the client via an Open with
5109 * CLAIM_DELEGATE_PREV.
5110 * Release the nfsv4root_lock, if held.
5114 NFSLOCKV4ROOTMUTEX();
5115 nfsv4_unlock(&nfsv4rootfs_lock, 1);
5116 NFSUNLOCKV4ROOTMUTEX();
5118 error = NFSERR_DELAY;
5123 * It's a current delegation, so:
5124 * - check to see if the delegation has expired
5125 * - if so, get the v4root lock and then expire it
5127 if (!(stp->ls_flags & NFSLCK_DELEGRECALL)) {
5129 * - do a recall callback, since not yet done
5130 * For now, never allow truncate to be set. To use
5131 * truncate safely, it must be guaranteed that the
5132 * Remove, Rename or Setattr with size of 0 will
5133 * succeed and that would require major changes to
5134 * the VFS/Vnode OPs.
5135 * Set the expiry time large enough so that it won't expire
5136 * until after the callback, then set it correctly, once
5137 * the callback is done. (The delegation will now time
5138 * out whether or not the Recall worked ok. The timeout
5139 * will be extended when ops are done on the delegation
5140 * stateid, up to the timelimit.)
5142 stp->ls_delegtime = NFSD_MONOSEC + (2 * nfsrv_lease) +
5144 stp->ls_delegtimelimit = NFSD_MONOSEC + (6 * nfsrv_lease) +
5146 stp->ls_flags |= NFSLCK_DELEGRECALL;
5149 * Loop NFSRV_CBRETRYCNT times while the CBRecall replies
5150 * NFSERR_BADSTATEID or NFSERR_BADHANDLE. This is done
5151 * in order to try and avoid a race that could happen
5152 * when a CBRecall request passed the Open reply with
5153 * the delegation in it when transitting the network.
5154 * Since nfsrv_docallback will sleep, don't use stp after
5157 NFSBCOPY((caddr_t)&stp->ls_stateid, (caddr_t)&tstateid,
5159 NFSBCOPY((caddr_t)&stp->ls_lfp->lf_fh, (caddr_t)&tfh,
5164 NFSLOCKV4ROOTMUTEX();
5165 nfsv4_unlock(&nfsv4rootfs_lock, 1);
5166 NFSUNLOCKV4ROOTMUTEX();
5170 error = nfsrv_docallback(clp, NFSV4OP_CBRECALL,
5171 &tstateid, 0, &tfh, NULL, NULL, p);
5173 } while ((error == NFSERR_BADSTATEID ||
5174 error == NFSERR_BADHANDLE) && retrycnt < NFSV4_CBRETRYCNT);
5175 error = NFSERR_DELAY;
5179 if (clp->lc_expiry >= NFSD_MONOSEC &&
5180 stp->ls_delegtime >= NFSD_MONOSEC) {
5183 * A recall has been done, but it has not yet expired.
5188 NFSLOCKV4ROOTMUTEX();
5189 nfsv4_unlock(&nfsv4rootfs_lock, 1);
5190 NFSUNLOCKV4ROOTMUTEX();
5192 error = NFSERR_DELAY;
5197 * If we don't yet have the lock, just get it and then return,
5198 * since we need that before deleting expired state, such as
5200 * When getting the lock, unlock the vnode, so other nfsds that
5201 * are in progress, won't get stuck waiting for the vnode lock.
5203 if (*haslockp == 0) {
5206 lktype = NFSVOPISLOCKED(vp);
5207 NFSVOPUNLOCK(vp, 0);
5209 NFSLOCKV4ROOTMUTEX();
5210 nfsv4_relref(&nfsv4rootfs_lock);
5212 gotlock = nfsv4_lock(&nfsv4rootfs_lock, 1, NULL,
5213 NFSV4ROOTLOCKMUTEXPTR, NULL);
5215 NFSUNLOCKV4ROOTMUTEX();
5218 NFSVOPLOCK(vp, lktype | LK_RETRY);
5219 if ((vp->v_iflag & VI_DOOMED) != 0) {
5221 NFSLOCKV4ROOTMUTEX();
5222 nfsv4_unlock(&nfsv4rootfs_lock, 1);
5223 NFSUNLOCKV4ROOTMUTEX();
5224 error = NFSERR_PERM;
5234 * Ok, we can delete the expired delegation.
5235 * First, write the Revoke record to stable storage and then
5236 * clear out the conflict.
5237 * Since all other nfsd threads are now blocked, we can safely
5238 * sleep without the state changing.
5240 nfsrv_writestable(clp->lc_id, clp->lc_idlen, NFSNST_REVOKE, p);
5241 nfsrv_backupstable();
5242 if (clp->lc_expiry < NFSD_MONOSEC) {
5243 nfsrv_cleanclient(clp, p);
5244 nfsrv_freedeleglist(&clp->lc_deleg);
5245 nfsrv_freedeleglist(&clp->lc_olddeleg);
5246 LIST_REMOVE(clp, lc_hash);
5249 nfsrv_freedeleg(stp);
5253 nfsrv_zapclient(clp, p);
5262 * Check for a remove allowed, if remove is set to 1 and get rid of
5266 nfsrv_checkremove(vnode_t vp, int remove, NFSPROC_T *p)
5268 struct nfsstate *stp;
5269 struct nfslockfile *lfp;
5270 int error, haslock = 0;
5274 * First, get the lock file structure.
5275 * (A return of -1 means no associated state, so remove ok.)
5277 error = nfsrv_getlockfh(vp, NFSLCK_CHECK, NULL, &nfh, p);
5281 error = nfsrv_getlockfile(NFSLCK_CHECK, NULL, &lfp, &nfh, 0);
5285 NFSLOCKV4ROOTMUTEX();
5286 nfsv4_unlock(&nfsv4rootfs_lock, 1);
5287 NFSUNLOCKV4ROOTMUTEX();
5295 * Now, we must Recall any delegations.
5297 error = nfsrv_cleandeleg(vp, lfp, NULL, &haslock, p);
5300 * nfsrv_cleandeleg() unlocks state for non-zero
5306 NFSLOCKV4ROOTMUTEX();
5307 nfsv4_unlock(&nfsv4rootfs_lock, 1);
5308 NFSUNLOCKV4ROOTMUTEX();
5314 * Now, look for a conflicting open share.
5317 LIST_FOREACH(stp, &lfp->lf_open, ls_file) {
5318 if (stp->ls_flags & NFSLCK_WRITEDENY) {
5319 error = NFSERR_FILEOPEN;
5327 NFSLOCKV4ROOTMUTEX();
5328 nfsv4_unlock(&nfsv4rootfs_lock, 1);
5329 NFSUNLOCKV4ROOTMUTEX();
5338 * Clear out all delegations for the file referred to by lfp.
5339 * May return NFSERR_DELAY, if there will be a delay waiting for
5340 * delegations to expire.
5341 * Returns -1 to indicate it slept while recalling a delegation.
5342 * This function has the side effect of deleting the nfslockfile structure,
5343 * if it no longer has associated state and didn't have to sleep.
5344 * Unlocks State before a non-zero value is returned.
5347 nfsrv_cleandeleg(vnode_t vp, struct nfslockfile *lfp,
5348 struct nfsclient *clp, int *haslockp, NFSPROC_T *p)
5350 struct nfsstate *stp, *nstp;
5353 stp = LIST_FIRST(&lfp->lf_deleg);
5354 while (stp != LIST_END(&lfp->lf_deleg)) {
5355 nstp = LIST_NEXT(stp, ls_file);
5356 if (stp->ls_clp != clp) {
5357 ret = nfsrv_delegconflict(stp, haslockp, p, vp);
5360 * nfsrv_delegconflict() unlocks state
5361 * when it returns non-zero.
5374 * There are certain operations that, when being done outside of NFSv4,
5375 * require that any NFSv4 delegation for the file be recalled.
5376 * This function is to be called for those cases:
5377 * VOP_RENAME() - When a delegation is being recalled for any reason,
5378 * the client may have to do Opens against the server, using the file's
5379 * final component name. If the file has been renamed on the server,
5380 * that component name will be incorrect and the Open will fail.
5381 * VOP_REMOVE() - Theoretically, a client could Open a file after it has
5382 * been removed on the server, if there is a delegation issued to
5383 * that client for the file. I say "theoretically" since clients
5384 * normally do an Access Op before the Open and that Access Op will
5385 * fail with ESTALE. Note that NFSv2 and 3 don't even do Opens, so
5386 * they will detect the file's removal in the same manner. (There is
5387 * one case where RFC3530 allows a client to do an Open without first
5388 * doing an Access Op, which is passage of a check against the ACE
5389 * returned with a Write delegation, but current practice is to ignore
5390 * the ACE and always do an Access Op.)
5391 * Since the functions can only be called with an unlocked vnode, this
5392 * can't be done at this time.
5393 * VOP_ADVLOCK() - When a client holds a delegation, it can issue byte range
5394 * locks locally in the client, which are not visible to the server. To
5395 * deal with this, issuing of delegations for a vnode must be disabled
5396 * and all delegations for the vnode recalled. This is done via the
5397 * second function, using the VV_DISABLEDELEG vflag on the vnode.
5400 nfsd_recalldelegation(vnode_t vp, NFSPROC_T *p)
5406 * First, check to see if the server is currently running and it has
5407 * been called for a regular file when issuing delegations.
5409 if (newnfs_numnfsd == 0 || vp->v_type != VREG ||
5410 nfsrv_issuedelegs == 0)
5413 KASSERT((NFSVOPISLOCKED(vp) != LK_EXCLUSIVE), ("vp %p is locked", vp));
5415 * First, get a reference on the nfsv4rootfs_lock so that an
5416 * exclusive lock cannot be acquired by another thread.
5418 NFSLOCKV4ROOTMUTEX();
5419 nfsv4_getref(&nfsv4rootfs_lock, NULL, NFSV4ROOTLOCKMUTEXPTR, NULL);
5420 NFSUNLOCKV4ROOTMUTEX();
5423 * Now, call nfsrv_checkremove() in a loop while it returns
5424 * NFSERR_DELAY. Return upon any other error or when timed out.
5426 starttime = NFSD_MONOSEC;
5428 if (NFSVOPLOCK(vp, LK_EXCLUSIVE) == 0) {
5429 error = nfsrv_checkremove(vp, 0, p);
5430 NFSVOPUNLOCK(vp, 0);
5433 if (error == NFSERR_DELAY) {
5434 if (NFSD_MONOSEC - starttime > NFS_REMOVETIMEO)
5436 /* Sleep for a short period of time */
5437 (void) nfs_catnap(PZERO, 0, "nfsremove");
5439 } while (error == NFSERR_DELAY);
5440 NFSLOCKV4ROOTMUTEX();
5441 nfsv4_relref(&nfsv4rootfs_lock);
5442 NFSUNLOCKV4ROOTMUTEX();
5446 nfsd_disabledelegation(vnode_t vp, NFSPROC_T *p)
5449 #ifdef VV_DISABLEDELEG
5451 * First, flag issuance of delegations disabled.
5453 atomic_set_long(&vp->v_vflag, VV_DISABLEDELEG);
5457 * Then call nfsd_recalldelegation() to get rid of all extant
5460 nfsd_recalldelegation(vp, p);
5464 * Check for conflicting locks, etc. and then get rid of delegations.
5465 * (At one point I thought that I should get rid of delegations for any
5466 * Setattr, since it could potentially disallow the I/O op (read or write)
5467 * allowed by the delegation. However, Setattr Ops that aren't changing
5468 * the size get a stateid of all 0s, so you can't tell if it is a delegation
5469 * for the same client or a different one, so I decided to only get rid
5470 * of delegations for other clients when the size is being changed.)
5471 * In general, a Setattr can disable NFS I/O Ops that are outstanding, such
5472 * as Write backs, even if there is no delegation, so it really isn't any
5476 nfsrv_checksetattr(vnode_t vp, struct nfsrv_descript *nd,
5477 nfsv4stateid_t *stateidp, struct nfsvattr *nvap, nfsattrbit_t *attrbitp,
5478 struct nfsexstuff *exp, NFSPROC_T *p)
5480 struct nfsstate st, *stp = &st;
5481 struct nfslock lo, *lop = &lo;
5485 if (NFSISSET_ATTRBIT(attrbitp, NFSATTRBIT_SIZE)) {
5486 stp->ls_flags = (NFSLCK_CHECK | NFSLCK_WRITEACCESS);
5487 lop->lo_first = nvap->na_size;
5492 if (NFSISSET_ATTRBIT(attrbitp, NFSATTRBIT_OWNER) ||
5493 NFSISSET_ATTRBIT(attrbitp, NFSATTRBIT_OWNERGROUP) ||
5494 NFSISSET_ATTRBIT(attrbitp, NFSATTRBIT_MODE) ||
5495 NFSISSET_ATTRBIT(attrbitp, NFSATTRBIT_ACL))
5496 stp->ls_flags |= NFSLCK_SETATTR;
5497 if (stp->ls_flags == 0)
5499 lop->lo_end = NFS64BITSSET;
5500 lop->lo_flags = NFSLCK_WRITE;
5501 stp->ls_ownerlen = 0;
5503 stp->ls_uid = nd->nd_cred->cr_uid;
5504 stp->ls_stateid.seqid = stateidp->seqid;
5505 clientid.lval[0] = stp->ls_stateid.other[0] = stateidp->other[0];
5506 clientid.lval[1] = stp->ls_stateid.other[1] = stateidp->other[1];
5507 stp->ls_stateid.other[2] = stateidp->other[2];
5508 error = nfsrv_lockctrl(vp, &stp, &lop, NULL, clientid,
5509 stateidp, exp, nd, p);
5512 NFSEXITCODE2(error, nd);
5517 * Check for a write delegation and do a CBGETATTR if there is one, updating
5518 * the attributes, as required.
5519 * Should I return an error if I can't get the attributes? (For now, I'll
5523 nfsrv_checkgetattr(struct nfsrv_descript *nd, vnode_t vp,
5524 struct nfsvattr *nvap, nfsattrbit_t *attrbitp, struct ucred *cred,
5527 struct nfsstate *stp;
5528 struct nfslockfile *lfp;
5529 struct nfsclient *clp;
5530 struct nfsvattr nva;
5533 nfsattrbit_t cbbits;
5534 u_quad_t delegfilerev;
5536 NFSCBGETATTR_ATTRBIT(attrbitp, &cbbits);
5537 if (!NFSNONZERO_ATTRBIT(&cbbits))
5539 if (nfsrv_writedelegcnt == 0)
5543 * Get the lock file structure.
5544 * (A return of -1 means no associated state, so return ok.)
5546 error = nfsrv_getlockfh(vp, NFSLCK_CHECK, NULL, &nfh, p);
5549 error = nfsrv_getlockfile(NFSLCK_CHECK, NULL, &lfp, &nfh, 0);
5558 * Now, look for a write delegation.
5560 LIST_FOREACH(stp, &lfp->lf_deleg, ls_file) {
5561 if (stp->ls_flags & NFSLCK_DELEGWRITE)
5564 if (stp == LIST_END(&lfp->lf_deleg)) {
5569 delegfilerev = stp->ls_filerev;
5572 * If the Write delegation was issued as a part of this Compound RPC
5573 * or if we have an Implied Clientid (used in a previous Op in this
5574 * compound) and it is the client the delegation was issued to,
5576 * I also assume that it is from the same client iff the network
5577 * host IP address is the same as the callback address. (Not
5578 * exactly correct by the RFC, but avoids a lot of Getattr
5581 if (nd->nd_compref == stp->ls_compref ||
5582 ((nd->nd_flag & ND_IMPLIEDCLID) &&
5583 clp->lc_clientid.qval == nd->nd_clientid.qval) ||
5584 nfsaddr2_match(clp->lc_req.nr_nam, nd->nd_nam)) {
5590 * We are now done with the delegation state structure,
5591 * so the statelock can be released and we can now tsleep().
5595 * Now, we must do the CB Getattr callback, to see if Change or Size
5598 if (clp->lc_expiry >= NFSD_MONOSEC) {
5600 NFSVNO_ATTRINIT(&nva);
5601 nva.na_filerev = NFS64BITSSET;
5602 error = nfsrv_docallback(clp, NFSV4OP_CBGETATTR, NULL,
5603 0, &nfh, &nva, &cbbits, p);
5605 if ((nva.na_filerev != NFS64BITSSET &&
5606 nva.na_filerev > delegfilerev) ||
5607 (NFSVNO_ISSETSIZE(&nva) &&
5608 nva.na_size != nvap->na_size)) {
5609 error = nfsvno_updfilerev(vp, nvap, cred, p);
5610 if (NFSVNO_ISSETSIZE(&nva))
5611 nvap->na_size = nva.na_size;
5614 error = 0; /* Ignore callback errors for now. */
5620 NFSEXITCODE2(error, nd);
5625 * This function looks for openowners that haven't had any opens for
5626 * a while and throws them away. Called by an nfsd when NFSNSF_NOOPENS
5630 nfsrv_throwawayopens(NFSPROC_T *p)
5632 struct nfsclient *clp, *nclp;
5633 struct nfsstate *stp, *nstp;
5637 nfsrv_stablefirst.nsf_flags &= ~NFSNSF_NOOPENS;
5639 * For each client...
5641 for (i = 0; i < nfsrv_clienthashsize; i++) {
5642 LIST_FOREACH_SAFE(clp, &nfsclienthash[i], lc_hash, nclp) {
5643 LIST_FOREACH_SAFE(stp, &clp->lc_open, ls_list, nstp) {
5644 if (LIST_EMPTY(&stp->ls_open) &&
5645 (stp->ls_noopens > NFSNOOPEN ||
5646 (nfsrv_openpluslock * 2) >
5647 nfsrv_v4statelimit))
5648 nfsrv_freeopenowner(stp, 0, p);
5656 * This function checks to see if the credentials are the same.
5657 * Returns 1 for not same, 0 otherwise.
5660 nfsrv_notsamecredname(struct nfsrv_descript *nd, struct nfsclient *clp)
5663 if (nd->nd_flag & ND_GSS) {
5664 if (!(clp->lc_flags & LCL_GSS))
5666 if (clp->lc_flags & LCL_NAME) {
5667 if (nd->nd_princlen != clp->lc_namelen ||
5668 NFSBCMP(nd->nd_principal, clp->lc_name,
5674 if (nd->nd_cred->cr_uid == clp->lc_uid)
5678 } else if (clp->lc_flags & LCL_GSS)
5681 * For AUTH_SYS, allow the same uid or root. (This is underspecified
5682 * in RFC3530, which talks about principals, but doesn't say anything
5683 * about uids for AUTH_SYS.)
5685 if (nd->nd_cred->cr_uid == clp->lc_uid || nd->nd_cred->cr_uid == 0)
5692 * Calculate the lease expiry time.
5695 nfsrv_leaseexpiry(void)
5698 if (nfsrv_stablefirst.nsf_eograce > NFSD_MONOSEC)
5699 return (NFSD_MONOSEC + 2 * (nfsrv_lease + NFSRV_LEASEDELTA));
5700 return (NFSD_MONOSEC + nfsrv_lease + NFSRV_LEASEDELTA);
5704 * Delay the delegation timeout as far as ls_delegtimelimit, as required.
5707 nfsrv_delaydelegtimeout(struct nfsstate *stp)
5710 if ((stp->ls_flags & NFSLCK_DELEGRECALL) == 0)
5713 if ((stp->ls_delegtime + 15) > NFSD_MONOSEC &&
5714 stp->ls_delegtime < stp->ls_delegtimelimit) {
5715 stp->ls_delegtime += nfsrv_lease;
5716 if (stp->ls_delegtime > stp->ls_delegtimelimit)
5717 stp->ls_delegtime = stp->ls_delegtimelimit;
5722 * This function checks to see if there is any other state associated
5723 * with the openowner for this Open.
5724 * It returns 1 if there is no other state, 0 otherwise.
5727 nfsrv_nootherstate(struct nfsstate *stp)
5729 struct nfsstate *tstp;
5731 LIST_FOREACH(tstp, &stp->ls_openowner->ls_open, ls_list) {
5732 if (tstp != stp || !LIST_EMPTY(&tstp->ls_lock))
5739 * Create a list of lock deltas (changes to local byte range locking
5740 * that can be rolled back using the list) and apply the changes via
5741 * nfsvno_advlock(). Optionally, lock the list. It is expected that either
5742 * the rollback or update function will be called after this.
5743 * It returns an error (and rolls back, as required), if any nfsvno_advlock()
5744 * call fails. If it returns an error, it will unlock the list.
5747 nfsrv_locallock(vnode_t vp, struct nfslockfile *lfp, int flags,
5748 uint64_t first, uint64_t end, struct nfslockconflict *cfp, NFSPROC_T *p)
5750 struct nfslock *lop, *nlop;
5753 /* Loop through the list of locks. */
5754 lop = LIST_FIRST(&lfp->lf_locallock);
5755 while (first < end && lop != NULL) {
5756 nlop = LIST_NEXT(lop, lo_lckowner);
5757 if (first >= lop->lo_end) {
5760 } else if (first < lop->lo_first) {
5761 /* new one starts before entry in list */
5762 if (end <= lop->lo_first) {
5763 /* no overlap between old and new */
5764 error = nfsrv_dolocal(vp, lfp, flags,
5765 NFSLCK_UNLOCK, first, end, cfp, p);
5770 /* handle fragment overlapped with new one */
5771 error = nfsrv_dolocal(vp, lfp, flags,
5772 NFSLCK_UNLOCK, first, lop->lo_first, cfp,
5776 first = lop->lo_first;
5779 /* new one overlaps this entry in list */
5780 if (end <= lop->lo_end) {
5781 /* overlaps all of new one */
5782 error = nfsrv_dolocal(vp, lfp, flags,
5783 lop->lo_flags, first, end, cfp, p);
5788 /* handle fragment overlapped with new one */
5789 error = nfsrv_dolocal(vp, lfp, flags,
5790 lop->lo_flags, first, lop->lo_end, cfp, p);
5793 first = lop->lo_end;
5798 if (first < end && error == 0)
5799 /* handle fragment past end of list */
5800 error = nfsrv_dolocal(vp, lfp, flags, NFSLCK_UNLOCK, first,
5808 * Local lock unlock. Unlock all byte ranges that are no longer locked
5809 * by NFSv4. To do this, unlock any subranges of first-->end that
5810 * do not overlap with the byte ranges of any lock in the lfp->lf_lock
5811 * list. This list has all locks for the file held by other
5812 * <clientid, lockowner> tuples. The list is ordered by increasing
5813 * lo_first value, but may have entries that overlap each other, for
5814 * the case of read locks.
5817 nfsrv_localunlock(vnode_t vp, struct nfslockfile *lfp, uint64_t init_first,
5818 uint64_t init_end, NFSPROC_T *p)
5820 struct nfslock *lop;
5821 uint64_t first, end, prevfirst;
5825 while (first < init_end) {
5826 /* Loop through all nfs locks, adjusting first and end */
5828 LIST_FOREACH(lop, &lfp->lf_lock, lo_lckfile) {
5829 KASSERT(prevfirst <= lop->lo_first,
5830 ("nfsv4 locks out of order"));
5831 KASSERT(lop->lo_first < lop->lo_end,
5832 ("nfsv4 bogus lock"));
5833 prevfirst = lop->lo_first;
5834 if (first >= lop->lo_first &&
5835 first < lop->lo_end)
5837 * Overlaps with initial part, so trim
5838 * off that initial part by moving first past
5841 first = lop->lo_end;
5842 else if (end > lop->lo_first &&
5843 lop->lo_first > first) {
5845 * This lock defines the end of the
5846 * segment to unlock, so set end to the
5847 * start of it and break out of the loop.
5849 end = lop->lo_first;
5854 * There is no segment left to do, so
5855 * break out of this loop and then exit
5856 * the outer while() since first will be set
5857 * to end, which must equal init_end here.
5862 /* Unlock this segment */
5863 (void) nfsrv_dolocal(vp, lfp, NFSLCK_UNLOCK,
5864 NFSLCK_READ, first, end, NULL, p);
5865 nfsrv_locallock_commit(lfp, NFSLCK_UNLOCK,
5869 * Now move past this segment and look for any further
5870 * segment in the range, if there is one.
5878 * Do the local lock operation and update the rollback list, as required.
5879 * Perform the rollback and return the error if nfsvno_advlock() fails.
5882 nfsrv_dolocal(vnode_t vp, struct nfslockfile *lfp, int flags, int oldflags,
5883 uint64_t first, uint64_t end, struct nfslockconflict *cfp, NFSPROC_T *p)
5885 struct nfsrollback *rlp;
5886 int error = 0, ltype, oldltype;
5888 if (flags & NFSLCK_WRITE)
5890 else if (flags & NFSLCK_READ)
5894 if (oldflags & NFSLCK_WRITE)
5896 else if (oldflags & NFSLCK_READ)
5900 if (ltype == oldltype || (oldltype == F_WRLCK && ltype == F_RDLCK))
5903 error = nfsvno_advlock(vp, ltype, first, end, p);
5906 cfp->cl_clientid.lval[0] = 0;
5907 cfp->cl_clientid.lval[1] = 0;
5909 cfp->cl_end = NFS64BITSSET;
5910 cfp->cl_flags = NFSLCK_WRITE;
5911 cfp->cl_ownerlen = 5;
5912 NFSBCOPY("LOCAL", cfp->cl_owner, 5);
5914 nfsrv_locallock_rollback(vp, lfp, p);
5915 } else if (ltype != F_UNLCK) {
5916 rlp = malloc(sizeof (struct nfsrollback), M_NFSDROLLBACK,
5918 rlp->rlck_first = first;
5919 rlp->rlck_end = end;
5920 rlp->rlck_type = oldltype;
5921 LIST_INSERT_HEAD(&lfp->lf_rollback, rlp, rlck_list);
5930 * Roll back local lock changes and free up the rollback list.
5933 nfsrv_locallock_rollback(vnode_t vp, struct nfslockfile *lfp, NFSPROC_T *p)
5935 struct nfsrollback *rlp, *nrlp;
5937 LIST_FOREACH_SAFE(rlp, &lfp->lf_rollback, rlck_list, nrlp) {
5938 (void) nfsvno_advlock(vp, rlp->rlck_type, rlp->rlck_first,
5940 free(rlp, M_NFSDROLLBACK);
5942 LIST_INIT(&lfp->lf_rollback);
5946 * Update local lock list and delete rollback list (ie now committed to the
5947 * local locks). Most of the work is done by the internal function.
5950 nfsrv_locallock_commit(struct nfslockfile *lfp, int flags, uint64_t first,
5953 struct nfsrollback *rlp, *nrlp;
5954 struct nfslock *new_lop, *other_lop;
5956 new_lop = malloc(sizeof (struct nfslock), M_NFSDLOCK, M_WAITOK);
5957 if (flags & (NFSLCK_READ | NFSLCK_WRITE))
5958 other_lop = malloc(sizeof (struct nfslock), M_NFSDLOCK,
5962 new_lop->lo_flags = flags;
5963 new_lop->lo_first = first;
5964 new_lop->lo_end = end;
5965 nfsrv_updatelock(NULL, &new_lop, &other_lop, lfp);
5966 if (new_lop != NULL)
5967 free(new_lop, M_NFSDLOCK);
5968 if (other_lop != NULL)
5969 free(other_lop, M_NFSDLOCK);
5971 /* and get rid of the rollback list */
5972 LIST_FOREACH_SAFE(rlp, &lfp->lf_rollback, rlck_list, nrlp)
5973 free(rlp, M_NFSDROLLBACK);
5974 LIST_INIT(&lfp->lf_rollback);
5978 * Lock the struct nfslockfile for local lock updating.
5981 nfsrv_locklf(struct nfslockfile *lfp)
5985 /* lf_usecount ensures *lfp won't be free'd */
5988 gotlock = nfsv4_lock(&lfp->lf_locallock_lck, 1, NULL,
5989 NFSSTATEMUTEXPTR, NULL);
5990 } while (gotlock == 0);
5995 * Unlock the struct nfslockfile after local lock updating.
5998 nfsrv_unlocklf(struct nfslockfile *lfp)
6001 nfsv4_unlock(&lfp->lf_locallock_lck, 0);
6005 * Clear out all state for the NFSv4 server.
6006 * Must be called by a thread that can sleep when no nfsds are running.
6009 nfsrv_throwawayallstate(NFSPROC_T *p)
6011 struct nfsclient *clp, *nclp;
6012 struct nfslockfile *lfp, *nlfp;
6016 * For each client, clean out the state and then free the structure.
6018 for (i = 0; i < nfsrv_clienthashsize; i++) {
6019 LIST_FOREACH_SAFE(clp, &nfsclienthash[i], lc_hash, nclp) {
6020 nfsrv_cleanclient(clp, p);
6021 nfsrv_freedeleglist(&clp->lc_deleg);
6022 nfsrv_freedeleglist(&clp->lc_olddeleg);
6023 free(clp->lc_stateid, M_NFSDCLIENT);
6024 free(clp, M_NFSDCLIENT);
6029 * Also, free up any remaining lock file structures.
6031 for (i = 0; i < nfsrv_lockhashsize; i++) {
6032 LIST_FOREACH_SAFE(lfp, &nfslockhash[i], lf_hash, nlfp) {
6033 printf("nfsd unload: fnd a lock file struct\n");
6034 nfsrv_freenfslockfile(lfp);
6040 * Check the sequence# for the session and slot provided as an argument.
6041 * Also, renew the lease if the session will return NFS_OK.
6044 nfsrv_checksequence(struct nfsrv_descript *nd, uint32_t sequenceid,
6045 uint32_t *highest_slotidp, uint32_t *target_highest_slotidp, int cache_this,
6046 uint32_t *sflagsp, NFSPROC_T *p)
6048 struct nfsdsession *sep;
6049 struct nfssessionhash *shp;
6053 shp = NFSSESSIONHASH(nd->nd_sessionid);
6054 NFSLOCKSESSION(shp);
6055 sep = nfsrv_findsession(nd->nd_sessionid);
6057 NFSUNLOCKSESSION(shp);
6058 return (NFSERR_BADSESSION);
6060 error = nfsv4_seqsession(sequenceid, nd->nd_slotid, *highest_slotidp,
6061 sep->sess_slots, NULL, NFSV4_SLOTS - 1);
6063 NFSUNLOCKSESSION(shp);
6066 if (cache_this != 0)
6067 nd->nd_flag |= ND_SAVEREPLY;
6068 /* Renew the lease. */
6069 sep->sess_clp->lc_expiry = nfsrv_leaseexpiry();
6070 nd->nd_clientid.qval = sep->sess_clp->lc_clientid.qval;
6071 nd->nd_flag |= ND_IMPLIEDCLID;
6074 * If this session handles the backchannel, save the nd_xprt for this
6075 * RPC, since this is the one being used.
6076 * RFC-5661 specifies that the fore channel will be implicitly
6077 * bound by a Sequence operation. However, since some NFSv4.1 clients
6078 * erroneously assumed that the back channel would be implicitly
6079 * bound as well, do the implicit binding unless a
6080 * BindConnectiontoSession has already been done on the session.
6082 if (sep->sess_clp->lc_req.nr_client != NULL &&
6083 sep->sess_cbsess.nfsess_xprt != nd->nd_xprt &&
6084 (sep->sess_crflags & NFSV4CRSESS_CONNBACKCHAN) != 0 &&
6085 (sep->sess_clp->lc_flags & LCL_DONEBINDCONN) == 0) {
6087 "nfsrv_checksequence: implicit back channel bind\n");
6088 savxprt = sep->sess_cbsess.nfsess_xprt;
6089 SVC_ACQUIRE(nd->nd_xprt);
6090 nd->nd_xprt->xp_p2 =
6091 sep->sess_clp->lc_req.nr_client->cl_private;
6092 nd->nd_xprt->xp_idletimeout = 0; /* Disable timeout. */
6093 sep->sess_cbsess.nfsess_xprt = nd->nd_xprt;
6094 if (savxprt != NULL)
6095 SVC_RELEASE(savxprt);
6099 if (sep->sess_clp->lc_req.nr_client == NULL)
6100 *sflagsp |= NFSV4SEQ_CBPATHDOWN;
6101 NFSUNLOCKSESSION(shp);
6102 if (error == NFSERR_EXPIRED) {
6103 *sflagsp |= NFSV4SEQ_EXPIREDALLSTATEREVOKED;
6105 } else if (error == NFSERR_ADMINREVOKED) {
6106 *sflagsp |= NFSV4SEQ_ADMINSTATEREVOKED;
6109 *highest_slotidp = *target_highest_slotidp = NFSV4_SLOTS - 1;
6114 * Check/set reclaim complete for this session/clientid.
6117 nfsrv_checkreclaimcomplete(struct nfsrv_descript *nd, int onefs)
6119 struct nfsdsession *sep;
6120 struct nfssessionhash *shp;
6123 shp = NFSSESSIONHASH(nd->nd_sessionid);
6125 NFSLOCKSESSION(shp);
6126 sep = nfsrv_findsession(nd->nd_sessionid);
6128 NFSUNLOCKSESSION(shp);
6130 return (NFSERR_BADSESSION);
6134 sep->sess_clp->lc_flags |= LCL_RECLAIMONEFS;
6135 /* Check to see if reclaim complete has already happened. */
6136 else if ((sep->sess_clp->lc_flags & LCL_RECLAIMCOMPLETE) != 0)
6137 error = NFSERR_COMPLETEALREADY;
6139 sep->sess_clp->lc_flags |= LCL_RECLAIMCOMPLETE;
6140 nfsrv_markreclaim(sep->sess_clp);
6142 NFSUNLOCKSESSION(shp);
6148 * Cache the reply in a session slot.
6151 nfsrv_cache_session(uint8_t *sessionid, uint32_t slotid, int repstat,
6154 struct nfsdsession *sep;
6155 struct nfssessionhash *shp;
6157 shp = NFSSESSIONHASH(sessionid);
6158 NFSLOCKSESSION(shp);
6159 sep = nfsrv_findsession(sessionid);
6161 NFSUNLOCKSESSION(shp);
6162 printf("nfsrv_cache_session: no session\n");
6166 nfsv4_seqsess_cacherep(slotid, sep->sess_slots, repstat, m);
6167 NFSUNLOCKSESSION(shp);
6171 * Search for a session that matches the sessionid.
6173 static struct nfsdsession *
6174 nfsrv_findsession(uint8_t *sessionid)
6176 struct nfsdsession *sep;
6177 struct nfssessionhash *shp;
6179 shp = NFSSESSIONHASH(sessionid);
6180 LIST_FOREACH(sep, &shp->list, sess_hash) {
6181 if (!NFSBCMP(sessionid, sep->sess_sessionid, NFSX_V4SESSIONID))
6188 * Destroy a session.
6191 nfsrv_destroysession(struct nfsrv_descript *nd, uint8_t *sessionid)
6193 int error, igotlock, samesess;
6196 if (!NFSBCMP(sessionid, nd->nd_sessionid, NFSX_V4SESSIONID) &&
6197 (nd->nd_flag & ND_HASSEQUENCE) != 0) {
6199 if ((nd->nd_flag & ND_LASTOP) == 0)
6200 return (NFSERR_BADSESSION);
6203 /* Lock out other nfsd threads */
6204 NFSLOCKV4ROOTMUTEX();
6205 nfsv4_relref(&nfsv4rootfs_lock);
6207 igotlock = nfsv4_lock(&nfsv4rootfs_lock, 1, NULL,
6208 NFSV4ROOTLOCKMUTEXPTR, NULL);
6209 } while (igotlock == 0);
6210 NFSUNLOCKV4ROOTMUTEX();
6212 error = nfsrv_freesession(NULL, sessionid);
6213 if (error == 0 && samesess != 0)
6214 nd->nd_flag &= ~ND_HASSEQUENCE;
6216 NFSLOCKV4ROOTMUTEX();
6217 nfsv4_unlock(&nfsv4rootfs_lock, 1);
6218 NFSUNLOCKV4ROOTMUTEX();
6223 * Bind a connection to a session.
6224 * For now, only certain variants are supported, since the current session
6225 * structure can only handle a single backchannel entry, which will be
6226 * applied to all connections if it is set.
6229 nfsrv_bindconnsess(struct nfsrv_descript *nd, uint8_t *sessionid, int *foreaftp)
6231 struct nfssessionhash *shp;
6232 struct nfsdsession *sep;
6233 struct nfsclient *clp;
6238 shp = NFSSESSIONHASH(sessionid);
6240 NFSLOCKSESSION(shp);
6241 sep = nfsrv_findsession(sessionid);
6243 clp = sep->sess_clp;
6244 if (*foreaftp == NFSCDFC4_BACK ||
6245 *foreaftp == NFSCDFC4_BACK_OR_BOTH ||
6246 *foreaftp == NFSCDFC4_FORE_OR_BOTH) {
6247 /* Try to set up a backchannel. */
6248 if (clp->lc_req.nr_client == NULL) {
6249 NFSD_DEBUG(2, "nfsrv_bindconnsess: acquire "
6251 clp->lc_req.nr_client = (struct __rpc_client *)
6252 clnt_bck_create(nd->nd_xprt->xp_socket,
6253 sep->sess_cbprogram, NFSV4_CBVERS);
6255 if (clp->lc_req.nr_client != NULL) {
6256 NFSD_DEBUG(2, "nfsrv_bindconnsess: set up "
6258 savxprt = sep->sess_cbsess.nfsess_xprt;
6259 SVC_ACQUIRE(nd->nd_xprt);
6260 nd->nd_xprt->xp_p2 =
6261 clp->lc_req.nr_client->cl_private;
6262 /* Disable idle timeout. */
6263 nd->nd_xprt->xp_idletimeout = 0;
6264 sep->sess_cbsess.nfsess_xprt = nd->nd_xprt;
6265 if (savxprt != NULL)
6266 SVC_RELEASE(savxprt);
6267 sep->sess_crflags |= NFSV4CRSESS_CONNBACKCHAN;
6268 clp->lc_flags |= LCL_DONEBINDCONN;
6269 if (*foreaftp == NFSCDFS4_BACK)
6270 *foreaftp = NFSCDFS4_BACK;
6272 *foreaftp = NFSCDFS4_BOTH;
6273 } else if (*foreaftp != NFSCDFC4_BACK) {
6274 NFSD_DEBUG(2, "nfsrv_bindconnsess: can't set "
6275 "up backchannel\n");
6276 sep->sess_crflags &= ~NFSV4CRSESS_CONNBACKCHAN;
6277 clp->lc_flags |= LCL_DONEBINDCONN;
6278 *foreaftp = NFSCDFS4_FORE;
6280 error = NFSERR_NOTSUPP;
6281 printf("nfsrv_bindconnsess: Can't add "
6285 NFSD_DEBUG(2, "nfsrv_bindconnsess: Set forechannel\n");
6286 clp->lc_flags |= LCL_DONEBINDCONN;
6287 *foreaftp = NFSCDFS4_FORE;
6290 error = NFSERR_BADSESSION;
6291 NFSUNLOCKSESSION(shp);
6297 * Free up a session structure.
6300 nfsrv_freesession(struct nfsdsession *sep, uint8_t *sessionid)
6302 struct nfssessionhash *shp;
6307 shp = NFSSESSIONHASH(sessionid);
6308 NFSLOCKSESSION(shp);
6309 sep = nfsrv_findsession(sessionid);
6311 shp = NFSSESSIONHASH(sep->sess_sessionid);
6312 NFSLOCKSESSION(shp);
6316 if (sep->sess_refcnt > 0) {
6317 NFSUNLOCKSESSION(shp);
6319 return (NFSERR_BACKCHANBUSY);
6321 LIST_REMOVE(sep, sess_hash);
6322 LIST_REMOVE(sep, sess_list);
6324 NFSUNLOCKSESSION(shp);
6327 return (NFSERR_BADSESSION);
6328 for (i = 0; i < NFSV4_SLOTS; i++)
6329 if (sep->sess_slots[i].nfssl_reply != NULL)
6330 m_freem(sep->sess_slots[i].nfssl_reply);
6331 if (sep->sess_cbsess.nfsess_xprt != NULL)
6332 SVC_RELEASE(sep->sess_cbsess.nfsess_xprt);
6333 free(sep, M_NFSDSESSION);
6339 * RFC5661 says that it should fail when there are associated opens, locks
6340 * or delegations. Since stateids represent opens, I don't see how you can
6341 * free an open stateid (it will be free'd when closed), so this function
6342 * only works for lock stateids (freeing the lock_owner) or delegations.
6345 nfsrv_freestateid(struct nfsrv_descript *nd, nfsv4stateid_t *stateidp,
6348 struct nfsclient *clp;
6349 struct nfsstate *stp;
6354 * Look up the stateid
6356 error = nfsrv_getclient((nfsquad_t)((u_quad_t)0), CLOPS_RENEW, &clp,
6357 NULL, (nfsquad_t)((u_quad_t)0), 0, nd, p);
6359 /* First, check for a delegation. */
6360 LIST_FOREACH(stp, &clp->lc_deleg, ls_list) {
6361 if (!NFSBCMP(stp->ls_stateid.other, stateidp->other,
6366 nfsrv_freedeleg(stp);
6371 /* Not a delegation, try for a lock_owner. */
6373 error = nfsrv_getstate(clp, stateidp, 0, &stp);
6374 if (error == 0 && ((stp->ls_flags & (NFSLCK_OPEN | NFSLCK_DELEGREAD |
6375 NFSLCK_DELEGWRITE)) != 0 || (stp->ls_flags & NFSLCK_LOCK) == 0))
6376 /* Not a lock_owner stateid. */
6377 error = NFSERR_LOCKSHELD;
6378 if (error == 0 && !LIST_EMPTY(&stp->ls_lock))
6379 error = NFSERR_LOCKSHELD;
6381 nfsrv_freelockowner(stp, NULL, 0, p);
6390 nfsrv_teststateid(struct nfsrv_descript *nd, nfsv4stateid_t *stateidp,
6393 struct nfsclient *clp;
6394 struct nfsstate *stp;
6399 * Look up the stateid
6401 error = nfsrv_getclient((nfsquad_t)((u_quad_t)0), CLOPS_RENEW, &clp,
6402 NULL, (nfsquad_t)((u_quad_t)0), 0, nd, p);
6404 error = nfsrv_getstate(clp, stateidp, 0, &stp);
6405 if (error == 0 && stateidp->seqid != 0 &&
6406 SEQ_LT(stateidp->seqid, stp->ls_stateid.seqid))
6407 error = NFSERR_OLDSTATEID;
6413 * Generate the xdr for an NFSv4.1 CBSequence Operation.
6416 nfsv4_setcbsequence(struct nfsrv_descript *nd, struct nfsclient *clp,
6417 int dont_replycache, struct nfsdsession **sepp)
6419 struct nfsdsession *sep;
6420 uint32_t *tl, slotseq = 0;
6421 int maxslot, slotpos;
6422 uint8_t sessionid[NFSX_V4SESSIONID];
6425 error = nfsv4_getcbsession(clp, sepp);
6429 (void)nfsv4_sequencelookup(NULL, &sep->sess_cbsess, &slotpos, &maxslot,
6430 &slotseq, sessionid);
6431 KASSERT(maxslot >= 0, ("nfsv4_setcbsequence neg maxslot"));
6433 /* Build the Sequence arguments. */
6434 NFSM_BUILD(tl, uint32_t *, NFSX_V4SESSIONID + 5 * NFSX_UNSIGNED);
6435 bcopy(sessionid, tl, NFSX_V4SESSIONID);
6436 tl += NFSX_V4SESSIONID / NFSX_UNSIGNED;
6437 nd->nd_slotseq = tl;
6438 *tl++ = txdr_unsigned(slotseq);
6439 *tl++ = txdr_unsigned(slotpos);
6440 *tl++ = txdr_unsigned(maxslot);
6441 if (dont_replycache == 0)
6442 *tl++ = newnfs_true;
6444 *tl++ = newnfs_false;
6445 *tl = 0; /* No referring call list, for now. */
6446 nd->nd_flag |= ND_HASSEQUENCE;
6451 * Get a session for the callback.
6454 nfsv4_getcbsession(struct nfsclient *clp, struct nfsdsession **sepp)
6456 struct nfsdsession *sep;
6459 LIST_FOREACH(sep, &clp->lc_session, sess_list) {
6460 if ((sep->sess_crflags & NFSV4CRSESS_CONNBACKCHAN) != 0)
6465 return (NFSERR_BADSESSION);
6474 * Free up all backchannel xprts. This needs to be done when the nfsd threads
6475 * exit, since those transports will all be going away.
6476 * This is only called after all the nfsd threads are done performing RPCs,
6477 * so locking shouldn't be an issue.
6480 nfsrv_freeallbackchannel_xprts(void)
6482 struct nfsdsession *sep;
6483 struct nfsclient *clp;
6487 for (i = 0; i < nfsrv_clienthashsize; i++) {
6488 LIST_FOREACH(clp, &nfsclienthash[i], lc_hash) {
6489 LIST_FOREACH(sep, &clp->lc_session, sess_list) {
6490 xprt = sep->sess_cbsess.nfsess_xprt;
6491 sep->sess_cbsess.nfsess_xprt = NULL;