2 * Copyright (c) 2009 Rick Macklem, University of Guelph
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
32 #include <fs/nfs/nfsport.h>
34 struct nfsrv_stablefirst nfsrv_stablefirst;
35 int nfsrv_issuedelegs = 0;
36 int nfsrv_dolocallocks = 0;
37 struct nfsv4lock nfsv4rootfs_lock;
39 extern int newnfs_numnfsd;
40 extern struct nfsstats newnfsstats;
41 extern int nfsrv_lease;
42 extern struct timeval nfsboottime;
43 extern u_int32_t newnfs_true, newnfs_false;
47 SYSCTL_DECL(_vfs_nfsd);
48 int nfsrv_statehashsize = NFSSTATEHASHSIZE;
49 TUNABLE_INT("vfs.nfsd.statehashsize", &nfsrv_statehashsize);
50 SYSCTL_INT(_vfs_nfsd, OID_AUTO, statehashsize, CTLFLAG_RDTUN,
51 &nfsrv_statehashsize, 0,
52 "Size of state hash table set via loader.conf");
54 int nfsrv_clienthashsize = NFSCLIENTHASHSIZE;
55 TUNABLE_INT("vfs.nfsd.clienthashsize", &nfsrv_clienthashsize);
56 SYSCTL_INT(_vfs_nfsd, OID_AUTO, clienthashsize, CTLFLAG_RDTUN,
57 &nfsrv_clienthashsize, 0,
58 "Size of client hash table set via loader.conf");
60 int nfsrv_lockhashsize = NFSLOCKHASHSIZE;
61 TUNABLE_INT("vfs.nfsd.fhhashsize", &nfsrv_lockhashsize);
62 SYSCTL_INT(_vfs_nfsd, OID_AUTO, fhhashsize, CTLFLAG_RDTUN,
63 &nfsrv_lockhashsize, 0,
64 "Size of file handle hash table set via loader.conf");
66 int nfsrv_sessionhashsize = NFSSESSIONHASHSIZE;
67 TUNABLE_INT("vfs.nfsd.sessionhashsize", &nfsrv_sessionhashsize);
68 SYSCTL_INT(_vfs_nfsd, OID_AUTO, sessionhashsize, CTLFLAG_RDTUN,
69 &nfsrv_sessionhashsize, 0,
70 "Size of session hash table set via loader.conf");
72 static int nfsrv_v4statelimit = NFSRV_V4STATELIMIT;
73 TUNABLE_INT("vfs.nfsd.v4statelimit", &nfsrv_v4statelimit);
74 SYSCTL_INT(_vfs_nfsd, OID_AUTO, v4statelimit, CTLFLAG_RWTUN,
75 &nfsrv_v4statelimit, 0,
76 "High water limit for NFSv4 opens+locks+delegations");
78 static int nfsrv_writedelegifpos = 0;
79 SYSCTL_INT(_vfs_nfsd, OID_AUTO, writedelegifpos, CTLFLAG_RW,
80 &nfsrv_writedelegifpos, 0,
81 "Issue a write delegation for read opens if possible");
83 static int nfsrv_allowreadforwriteopen = 1;
84 SYSCTL_INT(_vfs_nfsd, OID_AUTO, allowreadforwriteopen, CTLFLAG_RW,
85 &nfsrv_allowreadforwriteopen, 0,
86 "Allow Reads to be done with Write Access StateIDs");
89 * Hash lists for nfs V4.
91 struct nfsclienthashhead *nfsclienthash;
92 struct nfslockhashhead *nfslockhash;
93 struct nfssessionhash *nfssessionhash;
94 #endif /* !APPLEKEXT */
96 static u_int32_t nfsrv_openpluslock = 0, nfsrv_delegatecnt = 0;
97 static time_t nfsrvboottime;
98 static int nfsrv_returnoldstateid = 0, nfsrv_clients = 0;
99 static int nfsrv_clienthighwater = NFSRV_CLIENTHIGHWATER;
100 static int nfsrv_nogsscallback = 0;
101 static volatile int nfsrv_writedelegcnt = 0;
103 /* local functions */
104 static void nfsrv_dumpaclient(struct nfsclient *clp,
105 struct nfsd_dumpclients *dumpp);
106 static void nfsrv_freeopenowner(struct nfsstate *stp, int cansleep,
108 static int nfsrv_freeopen(struct nfsstate *stp, vnode_t vp, int cansleep,
110 static void nfsrv_freelockowner(struct nfsstate *stp, vnode_t vp, int cansleep,
112 static void nfsrv_freeallnfslocks(struct nfsstate *stp, vnode_t vp,
113 int cansleep, NFSPROC_T *p);
114 static void nfsrv_freenfslock(struct nfslock *lop);
115 static void nfsrv_freenfslockfile(struct nfslockfile *lfp);
116 static void nfsrv_freedeleg(struct nfsstate *);
117 static int nfsrv_getstate(struct nfsclient *clp, nfsv4stateid_t *stateidp,
118 u_int32_t flags, struct nfsstate **stpp);
119 static void nfsrv_getowner(struct nfsstatehead *hp, struct nfsstate *new_stp,
120 struct nfsstate **stpp);
121 static int nfsrv_getlockfh(vnode_t vp, u_short flags,
122 struct nfslockfile *new_lfp, fhandle_t *nfhp, NFSPROC_T *p);
123 static int nfsrv_getlockfile(u_short flags, struct nfslockfile **new_lfpp,
124 struct nfslockfile **lfpp, fhandle_t *nfhp, int lockit);
125 static void nfsrv_insertlock(struct nfslock *new_lop,
126 struct nfslock *insert_lop, struct nfsstate *stp, struct nfslockfile *lfp);
127 static void nfsrv_updatelock(struct nfsstate *stp, struct nfslock **new_lopp,
128 struct nfslock **other_lopp, struct nfslockfile *lfp);
129 static int nfsrv_getipnumber(u_char *cp);
130 static int nfsrv_checkrestart(nfsquad_t clientid, u_int32_t flags,
131 nfsv4stateid_t *stateidp, int specialid);
132 static int nfsrv_checkgrace(struct nfsrv_descript *nd, struct nfsclient *clp,
134 static int nfsrv_docallback(struct nfsclient *clp, int procnum,
135 nfsv4stateid_t *stateidp, int trunc, fhandle_t *fhp,
136 struct nfsvattr *nap, nfsattrbit_t *attrbitp, NFSPROC_T *p);
137 static int nfsrv_cbcallargs(struct nfsrv_descript *nd, struct nfsclient *clp,
138 uint32_t callback, int op, const char *optag, struct nfsdsession **sepp);
139 static u_int32_t nfsrv_nextclientindex(void);
140 static u_int32_t nfsrv_nextstateindex(struct nfsclient *clp);
141 static void nfsrv_markstable(struct nfsclient *clp);
142 static int nfsrv_checkstable(struct nfsclient *clp);
143 static int nfsrv_clientconflict(struct nfsclient *clp, int *haslockp, struct
144 vnode *vp, NFSPROC_T *p);
145 static int nfsrv_delegconflict(struct nfsstate *stp, int *haslockp,
146 NFSPROC_T *p, vnode_t vp);
147 static int nfsrv_cleandeleg(vnode_t vp, struct nfslockfile *lfp,
148 struct nfsclient *clp, int *haslockp, NFSPROC_T *p);
149 static int nfsrv_notsamecredname(struct nfsrv_descript *nd,
150 struct nfsclient *clp);
151 static time_t nfsrv_leaseexpiry(void);
152 static void nfsrv_delaydelegtimeout(struct nfsstate *stp);
153 static int nfsrv_checkseqid(struct nfsrv_descript *nd, u_int32_t seqid,
154 struct nfsstate *stp, struct nfsrvcache *op);
155 static int nfsrv_nootherstate(struct nfsstate *stp);
156 static int nfsrv_locallock(vnode_t vp, struct nfslockfile *lfp, int flags,
157 uint64_t first, uint64_t end, struct nfslockconflict *cfp, NFSPROC_T *p);
158 static void nfsrv_localunlock(vnode_t vp, struct nfslockfile *lfp,
159 uint64_t init_first, uint64_t init_end, NFSPROC_T *p);
160 static int nfsrv_dolocal(vnode_t vp, struct nfslockfile *lfp, int flags,
161 int oldflags, uint64_t first, uint64_t end, struct nfslockconflict *cfp,
163 static void nfsrv_locallock_rollback(vnode_t vp, struct nfslockfile *lfp,
165 static void nfsrv_locallock_commit(struct nfslockfile *lfp, int flags,
166 uint64_t first, uint64_t end);
167 static void nfsrv_locklf(struct nfslockfile *lfp);
168 static void nfsrv_unlocklf(struct nfslockfile *lfp);
169 static struct nfsdsession *nfsrv_findsession(uint8_t *sessionid);
170 static int nfsrv_freesession(struct nfsdsession *sep, uint8_t *sessionid);
171 static int nfsv4_setcbsequence(struct nfsrv_descript *nd, struct nfsclient *clp,
172 int dont_replycache, struct nfsdsession **sepp);
173 static int nfsv4_getcbsession(struct nfsclient *clp, struct nfsdsession **sepp);
176 * Scan the client list for a match and either return the current one,
177 * create a new entry or return an error.
178 * If returning a non-error, the clp structure must either be linked into
179 * the client list or free'd.
182 nfsrv_setclient(struct nfsrv_descript *nd, struct nfsclient **new_clpp,
183 nfsquad_t *clientidp, nfsquad_t *confirmp, NFSPROC_T *p)
185 struct nfsclient *clp = NULL, *new_clp = *new_clpp;
187 struct nfsstate *stp, *tstp;
188 struct sockaddr_in *sad, *rad;
189 int zapit = 0, gotit, hasstate = 0, igotlock;
190 static u_int64_t confirm_index = 0;
193 * Check for state resource limit exceeded.
195 if (nfsrv_openpluslock > nfsrv_v4statelimit) {
196 error = NFSERR_RESOURCE;
200 if (nfsrv_issuedelegs == 0 ||
201 ((nd->nd_flag & ND_GSS) != 0 && nfsrv_nogsscallback != 0))
203 * Don't do callbacks when delegations are disabled or
204 * for AUTH_GSS unless enabled via nfsrv_nogsscallback.
205 * If establishing a callback connection is attempted
206 * when a firewall is blocking the callback path, the
207 * server may wait too long for the connect attempt to
208 * succeed during the Open. Some clients, such as Linux,
209 * may timeout and give up on the Open before the server
210 * replies. Also, since AUTH_GSS callbacks are not
211 * yet interoperability tested, they might cause the
212 * server to crap out, if they get past the Init call to
215 new_clp->lc_program = 0;
217 /* Lock out other nfsd threads */
218 NFSLOCKV4ROOTMUTEX();
219 nfsv4_relref(&nfsv4rootfs_lock);
221 igotlock = nfsv4_lock(&nfsv4rootfs_lock, 1, NULL,
222 NFSV4ROOTLOCKMUTEXPTR, NULL);
224 NFSUNLOCKV4ROOTMUTEX();
227 * Search for a match in the client list.
230 while (i < nfsrv_clienthashsize && !gotit) {
231 LIST_FOREACH(clp, &nfsclienthash[i], lc_hash) {
232 if (new_clp->lc_idlen == clp->lc_idlen &&
233 !NFSBCMP(new_clp->lc_id, clp->lc_id, clp->lc_idlen)) {
242 (clp->lc_flags & (LCL_NEEDSCONFIRM | LCL_ADMINREVOKED))) {
243 if ((nd->nd_flag & ND_NFSV41) != 0 && confirmp->lval[1] != 0) {
245 * For NFSv4.1, if confirmp->lval[1] is non-zero, the
246 * client is trying to update a confirmed clientid.
248 NFSLOCKV4ROOTMUTEX();
249 nfsv4_unlock(&nfsv4rootfs_lock, 1);
250 NFSUNLOCKV4ROOTMUTEX();
251 confirmp->lval[1] = 0;
252 error = NFSERR_NOENT;
256 * Get rid of the old one.
258 if (i != nfsrv_clienthashsize) {
259 LIST_REMOVE(clp, lc_hash);
260 nfsrv_cleanclient(clp, p);
261 nfsrv_freedeleglist(&clp->lc_deleg);
262 nfsrv_freedeleglist(&clp->lc_olddeleg);
266 * Add it after assigning a client id to it.
268 new_clp->lc_flags |= LCL_NEEDSCONFIRM;
269 if ((nd->nd_flag & ND_NFSV41) != 0)
270 new_clp->lc_confirm.lval[0] = confirmp->lval[0] =
273 confirmp->qval = new_clp->lc_confirm.qval =
275 clientidp->lval[0] = new_clp->lc_clientid.lval[0] =
276 (u_int32_t)nfsrvboottime;
277 clientidp->lval[1] = new_clp->lc_clientid.lval[1] =
278 nfsrv_nextclientindex();
279 new_clp->lc_stateindex = 0;
280 new_clp->lc_statemaxindex = 0;
281 new_clp->lc_cbref = 0;
282 new_clp->lc_expiry = nfsrv_leaseexpiry();
283 LIST_INIT(&new_clp->lc_open);
284 LIST_INIT(&new_clp->lc_deleg);
285 LIST_INIT(&new_clp->lc_olddeleg);
286 LIST_INIT(&new_clp->lc_session);
287 for (i = 0; i < nfsrv_statehashsize; i++)
288 LIST_INIT(&new_clp->lc_stateid[i]);
289 LIST_INSERT_HEAD(NFSCLIENTHASH(new_clp->lc_clientid), new_clp,
291 newnfsstats.srvclients++;
292 nfsrv_openpluslock++;
294 NFSLOCKV4ROOTMUTEX();
295 nfsv4_unlock(&nfsv4rootfs_lock, 1);
296 NFSUNLOCKV4ROOTMUTEX();
298 nfsrv_zapclient(clp, p);
304 * Now, handle the cases where the id is already issued.
306 if (nfsrv_notsamecredname(nd, clp)) {
308 * Check to see if there is expired state that should go away.
310 if (clp->lc_expiry < NFSD_MONOSEC &&
311 (!LIST_EMPTY(&clp->lc_open) || !LIST_EMPTY(&clp->lc_deleg))) {
312 nfsrv_cleanclient(clp, p);
313 nfsrv_freedeleglist(&clp->lc_deleg);
317 * If there is outstanding state, then reply NFSERR_CLIDINUSE per
318 * RFC3530 Sec. 8.1.2 last para.
320 if (!LIST_EMPTY(&clp->lc_deleg)) {
322 } else if (LIST_EMPTY(&clp->lc_open)) {
326 /* Look for an Open on the OpenOwner */
327 LIST_FOREACH(stp, &clp->lc_open, ls_list) {
328 if (!LIST_EMPTY(&stp->ls_open)) {
336 * If the uid doesn't match, return NFSERR_CLIDINUSE after
337 * filling out the correct ipaddr and portnum.
339 sad = NFSSOCKADDR(new_clp->lc_req.nr_nam, struct sockaddr_in *);
340 rad = NFSSOCKADDR(clp->lc_req.nr_nam, struct sockaddr_in *);
341 sad->sin_addr.s_addr = rad->sin_addr.s_addr;
342 sad->sin_port = rad->sin_port;
343 NFSLOCKV4ROOTMUTEX();
344 nfsv4_unlock(&nfsv4rootfs_lock, 1);
345 NFSUNLOCKV4ROOTMUTEX();
346 error = NFSERR_CLIDINUSE;
351 if (NFSBCMP(new_clp->lc_verf, clp->lc_verf, NFSX_VERF)) {
353 * If the verifier has changed, the client has rebooted
354 * and a new client id is issued. The old state info
355 * can be thrown away once the SETCLIENTID_CONFIRM occurs.
357 LIST_REMOVE(clp, lc_hash);
358 new_clp->lc_flags |= LCL_NEEDSCONFIRM;
359 if ((nd->nd_flag & ND_NFSV41) != 0)
360 new_clp->lc_confirm.lval[0] = confirmp->lval[0] =
363 confirmp->qval = new_clp->lc_confirm.qval =
365 clientidp->lval[0] = new_clp->lc_clientid.lval[0] =
367 clientidp->lval[1] = new_clp->lc_clientid.lval[1] =
368 nfsrv_nextclientindex();
369 new_clp->lc_stateindex = 0;
370 new_clp->lc_statemaxindex = 0;
371 new_clp->lc_cbref = 0;
372 new_clp->lc_expiry = nfsrv_leaseexpiry();
375 * Save the state until confirmed.
377 LIST_NEWHEAD(&new_clp->lc_open, &clp->lc_open, ls_list);
378 LIST_FOREACH(tstp, &new_clp->lc_open, ls_list)
379 tstp->ls_clp = new_clp;
380 LIST_NEWHEAD(&new_clp->lc_deleg, &clp->lc_deleg, ls_list);
381 LIST_FOREACH(tstp, &new_clp->lc_deleg, ls_list)
382 tstp->ls_clp = new_clp;
383 LIST_NEWHEAD(&new_clp->lc_olddeleg, &clp->lc_olddeleg,
385 LIST_FOREACH(tstp, &new_clp->lc_olddeleg, ls_list)
386 tstp->ls_clp = new_clp;
387 for (i = 0; i < nfsrv_statehashsize; i++) {
388 LIST_NEWHEAD(&new_clp->lc_stateid[i],
389 &clp->lc_stateid[i], ls_hash);
390 LIST_FOREACH(tstp, &new_clp->lc_stateid[i], ls_hash)
391 tstp->ls_clp = new_clp;
393 LIST_INSERT_HEAD(NFSCLIENTHASH(new_clp->lc_clientid), new_clp,
395 newnfsstats.srvclients++;
396 nfsrv_openpluslock++;
398 NFSLOCKV4ROOTMUTEX();
399 nfsv4_unlock(&nfsv4rootfs_lock, 1);
400 NFSUNLOCKV4ROOTMUTEX();
403 * Must wait until any outstanding callback on the old clp
407 while (clp->lc_cbref) {
408 clp->lc_flags |= LCL_WAKEUPWANTED;
409 (void)mtx_sleep(clp, NFSSTATEMUTEXPTR, PZERO - 1,
410 "nfsd clp", 10 * hz);
413 nfsrv_zapclient(clp, p);
418 /* For NFSv4.1, mark that we found a confirmed clientid. */
419 if ((nd->nd_flag & ND_NFSV41) != 0) {
420 clientidp->lval[0] = clp->lc_clientid.lval[0];
421 clientidp->lval[1] = clp->lc_clientid.lval[1];
422 confirmp->lval[0] = 0; /* Ignored by client */
423 confirmp->lval[1] = 1;
426 * id and verifier match, so update the net address info
427 * and get rid of any existing callback authentication
428 * handle, so a new one will be acquired.
430 LIST_REMOVE(clp, lc_hash);
431 new_clp->lc_flags |= (LCL_NEEDSCONFIRM | LCL_DONTCLEAN);
432 new_clp->lc_expiry = nfsrv_leaseexpiry();
433 confirmp->qval = new_clp->lc_confirm.qval = ++confirm_index;
434 clientidp->lval[0] = new_clp->lc_clientid.lval[0] =
435 clp->lc_clientid.lval[0];
436 clientidp->lval[1] = new_clp->lc_clientid.lval[1] =
437 clp->lc_clientid.lval[1];
438 new_clp->lc_delegtime = clp->lc_delegtime;
439 new_clp->lc_stateindex = clp->lc_stateindex;
440 new_clp->lc_statemaxindex = clp->lc_statemaxindex;
441 new_clp->lc_cbref = 0;
442 LIST_NEWHEAD(&new_clp->lc_open, &clp->lc_open, ls_list);
443 LIST_FOREACH(tstp, &new_clp->lc_open, ls_list)
444 tstp->ls_clp = new_clp;
445 LIST_NEWHEAD(&new_clp->lc_deleg, &clp->lc_deleg, ls_list);
446 LIST_FOREACH(tstp, &new_clp->lc_deleg, ls_list)
447 tstp->ls_clp = new_clp;
448 LIST_NEWHEAD(&new_clp->lc_olddeleg, &clp->lc_olddeleg, ls_list);
449 LIST_FOREACH(tstp, &new_clp->lc_olddeleg, ls_list)
450 tstp->ls_clp = new_clp;
451 for (i = 0; i < nfsrv_statehashsize; i++) {
452 LIST_NEWHEAD(&new_clp->lc_stateid[i],
453 &clp->lc_stateid[i], ls_hash);
454 LIST_FOREACH(tstp, &new_clp->lc_stateid[i], ls_hash)
455 tstp->ls_clp = new_clp;
457 LIST_INSERT_HEAD(NFSCLIENTHASH(new_clp->lc_clientid), new_clp,
459 newnfsstats.srvclients++;
460 nfsrv_openpluslock++;
463 NFSLOCKV4ROOTMUTEX();
464 nfsv4_unlock(&nfsv4rootfs_lock, 1);
465 NFSUNLOCKV4ROOTMUTEX();
467 if ((nd->nd_flag & ND_NFSV41) == 0) {
469 * Must wait until any outstanding callback on the old clp
473 while (clp->lc_cbref) {
474 clp->lc_flags |= LCL_WAKEUPWANTED;
475 (void)mtx_sleep(clp, NFSSTATEMUTEXPTR, PZERO - 1,
479 nfsrv_zapclient(clp, p);
484 NFSEXITCODE2(error, nd);
489 * Check to see if the client id exists and optionally confirm it.
492 nfsrv_getclient(nfsquad_t clientid, int opflags, struct nfsclient **clpp,
493 struct nfsdsession *nsep, nfsquad_t confirm, uint32_t cbprogram,
494 struct nfsrv_descript *nd, NFSPROC_T *p)
496 struct nfsclient *clp;
497 struct nfsstate *stp;
499 struct nfsclienthashhead *hp;
500 int error = 0, igotlock, doneok;
501 struct nfssessionhash *shp;
502 struct nfsdsession *sep;
504 static uint64_t next_sess = 0;
508 if ((nd == NULL || (nd->nd_flag & ND_NFSV41) == 0 ||
509 opflags != CLOPS_RENEW) && nfsrvboottime != clientid.lval[0]) {
510 error = NFSERR_STALECLIENTID;
515 * If called with opflags == CLOPS_RENEW, the State Lock is
516 * already held. Otherwise, we need to get either that or,
517 * for the case of Confirm, lock out the nfsd threads.
519 if (opflags & CLOPS_CONFIRM) {
520 NFSLOCKV4ROOTMUTEX();
521 nfsv4_relref(&nfsv4rootfs_lock);
523 igotlock = nfsv4_lock(&nfsv4rootfs_lock, 1, NULL,
524 NFSV4ROOTLOCKMUTEXPTR, NULL);
527 * Create a new sessionid here, since we need to do it where
528 * there is a mutex held to serialize update of next_sess.
530 if ((nd->nd_flag & ND_NFSV41) != 0) {
531 sessid[0] = ++next_sess;
532 sessid[1] = clientid.qval;
534 NFSUNLOCKV4ROOTMUTEX();
535 } else if (opflags != CLOPS_RENEW) {
539 /* For NFSv4.1, the clp is acquired from the associated session. */
540 if (nd != NULL && (nd->nd_flag & ND_NFSV41) != 0 &&
541 opflags == CLOPS_RENEW) {
543 if ((nd->nd_flag & ND_HASSEQUENCE) != 0) {
544 shp = NFSSESSIONHASH(nd->nd_sessionid);
546 sep = nfsrv_findsession(nd->nd_sessionid);
549 NFSUNLOCKSESSION(shp);
552 hp = NFSCLIENTHASH(clientid);
553 LIST_FOREACH(clp, hp, lc_hash) {
554 if (clp->lc_clientid.lval[1] == clientid.lval[1])
559 if (opflags & CLOPS_CONFIRM)
560 error = NFSERR_STALECLIENTID;
562 error = NFSERR_EXPIRED;
563 } else if (clp->lc_flags & LCL_ADMINREVOKED) {
565 * If marked admin revoked, just return the error.
567 error = NFSERR_ADMINREVOKED;
570 if (opflags & CLOPS_CONFIRM) {
571 NFSLOCKV4ROOTMUTEX();
572 nfsv4_unlock(&nfsv4rootfs_lock, 1);
573 NFSUNLOCKV4ROOTMUTEX();
574 } else if (opflags != CLOPS_RENEW) {
581 * Perform any operations specified by the opflags.
583 if (opflags & CLOPS_CONFIRM) {
584 if (((nd->nd_flag & ND_NFSV41) != 0 &&
585 clp->lc_confirm.lval[0] != confirm.lval[0]) ||
586 ((nd->nd_flag & ND_NFSV41) == 0 &&
587 clp->lc_confirm.qval != confirm.qval))
588 error = NFSERR_STALECLIENTID;
589 else if (nfsrv_notsamecredname(nd, clp))
590 error = NFSERR_CLIDINUSE;
593 if ((clp->lc_flags & (LCL_NEEDSCONFIRM | LCL_DONTCLEAN)) ==
596 * Hang onto the delegations (as old delegations)
597 * for an Open with CLAIM_DELEGATE_PREV unless in
598 * grace, but get rid of the rest of the state.
600 nfsrv_cleanclient(clp, p);
601 nfsrv_freedeleglist(&clp->lc_olddeleg);
602 if (nfsrv_checkgrace(nd, clp, 0)) {
603 /* In grace, so just delete delegations */
604 nfsrv_freedeleglist(&clp->lc_deleg);
606 LIST_FOREACH(stp, &clp->lc_deleg, ls_list)
607 stp->ls_flags |= NFSLCK_OLDDELEG;
608 clp->lc_delegtime = NFSD_MONOSEC +
609 nfsrv_lease + NFSRV_LEASEDELTA;
610 LIST_NEWHEAD(&clp->lc_olddeleg, &clp->lc_deleg,
613 if ((nd->nd_flag & ND_NFSV41) != 0)
614 clp->lc_program = cbprogram;
616 clp->lc_flags &= ~(LCL_NEEDSCONFIRM | LCL_DONTCLEAN);
618 clp->lc_flags |= LCL_NEEDSCBNULL;
619 /* For NFSv4.1, link the session onto the client. */
621 /* Hold a reference on the xprt for a backchannel. */
622 if ((nsep->sess_crflags & NFSV4CRSESS_CONNBACKCHAN)
623 != 0 && clp->lc_req.nr_client == NULL) {
624 clp->lc_req.nr_client = (struct __rpc_client *)
625 clnt_bck_create(nd->nd_xprt->xp_socket,
626 cbprogram, NFSV4_CBVERS);
627 if (clp->lc_req.nr_client != NULL) {
628 SVC_ACQUIRE(nd->nd_xprt);
630 clp->lc_req.nr_client->cl_private;
631 /* Disable idle timeout. */
632 nd->nd_xprt->xp_idletimeout = 0;
633 nsep->sess_cbsess.nfsess_xprt = nd->nd_xprt;
635 nsep->sess_crflags &= ~NFSV4CRSESS_CONNBACKCHAN;
637 NFSBCOPY(sessid, nsep->sess_sessionid,
639 NFSBCOPY(sessid, nsep->sess_cbsess.nfsess_sessionid,
641 shp = NFSSESSIONHASH(nsep->sess_sessionid);
644 LIST_INSERT_HEAD(&shp->list, nsep, sess_hash);
645 LIST_INSERT_HEAD(&clp->lc_session, nsep, sess_list);
646 nsep->sess_clp = clp;
647 NFSUNLOCKSESSION(shp);
651 } else if (clp->lc_flags & LCL_NEEDSCONFIRM) {
652 error = NFSERR_EXPIRED;
656 * If called by the Renew Op, we must check the principal.
658 if (!error && (opflags & CLOPS_RENEWOP)) {
659 if (nfsrv_notsamecredname(nd, clp)) {
661 for (i = 0; i < nfsrv_statehashsize && doneok == 0; i++) {
662 LIST_FOREACH(stp, &clp->lc_stateid[i], ls_hash) {
663 if ((stp->ls_flags & NFSLCK_OPEN) &&
664 stp->ls_uid == nd->nd_cred->cr_uid) {
671 error = NFSERR_ACCES;
673 if (!error && (clp->lc_flags & LCL_CBDOWN))
674 error = NFSERR_CBPATHDOWN;
676 if ((!error || error == NFSERR_CBPATHDOWN) &&
677 (opflags & CLOPS_RENEW)) {
678 clp->lc_expiry = nfsrv_leaseexpiry();
680 if (opflags & CLOPS_CONFIRM) {
681 NFSLOCKV4ROOTMUTEX();
682 nfsv4_unlock(&nfsv4rootfs_lock, 1);
683 NFSUNLOCKV4ROOTMUTEX();
684 } else if (opflags != CLOPS_RENEW) {
691 NFSEXITCODE2(error, nd);
696 * Perform the NFSv4.1 destroy clientid.
699 nfsrv_destroyclient(nfsquad_t clientid, NFSPROC_T *p)
701 struct nfsclient *clp;
702 struct nfsclienthashhead *hp;
703 int error = 0, i, igotlock;
705 if (nfsrvboottime != clientid.lval[0]) {
706 error = NFSERR_STALECLIENTID;
710 /* Lock out other nfsd threads */
711 NFSLOCKV4ROOTMUTEX();
712 nfsv4_relref(&nfsv4rootfs_lock);
714 igotlock = nfsv4_lock(&nfsv4rootfs_lock, 1, NULL,
715 NFSV4ROOTLOCKMUTEXPTR, NULL);
716 } while (igotlock == 0);
717 NFSUNLOCKV4ROOTMUTEX();
719 hp = NFSCLIENTHASH(clientid);
720 LIST_FOREACH(clp, hp, lc_hash) {
721 if (clp->lc_clientid.lval[1] == clientid.lval[1])
725 NFSLOCKV4ROOTMUTEX();
726 nfsv4_unlock(&nfsv4rootfs_lock, 1);
727 NFSUNLOCKV4ROOTMUTEX();
728 /* Just return ok, since it is gone. */
732 /* Scan for state on the clientid. */
733 for (i = 0; i < nfsrv_statehashsize; i++)
734 if (!LIST_EMPTY(&clp->lc_stateid[i])) {
735 NFSLOCKV4ROOTMUTEX();
736 nfsv4_unlock(&nfsv4rootfs_lock, 1);
737 NFSUNLOCKV4ROOTMUTEX();
738 error = NFSERR_CLIENTIDBUSY;
741 if (!LIST_EMPTY(&clp->lc_session) || !LIST_EMPTY(&clp->lc_deleg)) {
742 NFSLOCKV4ROOTMUTEX();
743 nfsv4_unlock(&nfsv4rootfs_lock, 1);
744 NFSUNLOCKV4ROOTMUTEX();
745 error = NFSERR_CLIENTIDBUSY;
749 /* Destroy the clientid and return ok. */
750 nfsrv_cleanclient(clp, p);
751 nfsrv_freedeleglist(&clp->lc_deleg);
752 nfsrv_freedeleglist(&clp->lc_olddeleg);
753 LIST_REMOVE(clp, lc_hash);
754 NFSLOCKV4ROOTMUTEX();
755 nfsv4_unlock(&nfsv4rootfs_lock, 1);
756 NFSUNLOCKV4ROOTMUTEX();
757 nfsrv_zapclient(clp, p);
759 NFSEXITCODE2(error, nd);
764 * Called from the new nfssvc syscall to admin revoke a clientid.
765 * Returns 0 for success, error otherwise.
768 nfsrv_adminrevoke(struct nfsd_clid *revokep, NFSPROC_T *p)
770 struct nfsclient *clp = NULL;
775 * First, lock out the nfsd so that state won't change while the
776 * revocation record is being written to the stable storage restart
779 NFSLOCKV4ROOTMUTEX();
781 igotlock = nfsv4_lock(&nfsv4rootfs_lock, 1, NULL,
782 NFSV4ROOTLOCKMUTEXPTR, NULL);
784 NFSUNLOCKV4ROOTMUTEX();
787 * Search for a match in the client list.
790 while (i < nfsrv_clienthashsize && !gotit) {
791 LIST_FOREACH(clp, &nfsclienthash[i], lc_hash) {
792 if (revokep->nclid_idlen == clp->lc_idlen &&
793 !NFSBCMP(revokep->nclid_id, clp->lc_id, clp->lc_idlen)) {
801 NFSLOCKV4ROOTMUTEX();
802 nfsv4_unlock(&nfsv4rootfs_lock, 0);
803 NFSUNLOCKV4ROOTMUTEX();
809 * Now, write out the revocation record
811 nfsrv_writestable(clp->lc_id, clp->lc_idlen, NFSNST_REVOKE, p);
812 nfsrv_backupstable();
815 * and clear out the state, marking the clientid revoked.
817 clp->lc_flags &= ~LCL_CALLBACKSON;
818 clp->lc_flags |= LCL_ADMINREVOKED;
819 nfsrv_cleanclient(clp, p);
820 nfsrv_freedeleglist(&clp->lc_deleg);
821 nfsrv_freedeleglist(&clp->lc_olddeleg);
822 NFSLOCKV4ROOTMUTEX();
823 nfsv4_unlock(&nfsv4rootfs_lock, 0);
824 NFSUNLOCKV4ROOTMUTEX();
832 * Dump out stats for all clients. Called from nfssvc(2), that is used
836 nfsrv_dumpclients(struct nfsd_dumpclients *dumpp, int maxcnt)
838 struct nfsclient *clp;
842 * First, get a reference on the nfsv4rootfs_lock so that an
843 * exclusive lock cannot be acquired while dumping the clients.
845 NFSLOCKV4ROOTMUTEX();
846 nfsv4_getref(&nfsv4rootfs_lock, NULL, NFSV4ROOTLOCKMUTEXPTR, NULL);
847 NFSUNLOCKV4ROOTMUTEX();
850 * Rattle through the client lists until done.
852 while (i < nfsrv_clienthashsize && cnt < maxcnt) {
853 clp = LIST_FIRST(&nfsclienthash[i]);
854 while (clp != LIST_END(&nfsclienthash[i]) && cnt < maxcnt) {
855 nfsrv_dumpaclient(clp, &dumpp[cnt]);
857 clp = LIST_NEXT(clp, lc_hash);
862 dumpp[cnt].ndcl_clid.nclid_idlen = 0;
864 NFSLOCKV4ROOTMUTEX();
865 nfsv4_relref(&nfsv4rootfs_lock);
866 NFSUNLOCKV4ROOTMUTEX();
870 * Dump stats for a client. Must be called with the NFSSTATELOCK and spl'd.
873 nfsrv_dumpaclient(struct nfsclient *clp, struct nfsd_dumpclients *dumpp)
875 struct nfsstate *stp, *openstp, *lckownstp;
877 struct sockaddr *sad;
878 struct sockaddr_in *rad;
879 struct sockaddr_in6 *rad6;
881 dumpp->ndcl_nopenowners = dumpp->ndcl_nlockowners = 0;
882 dumpp->ndcl_nopens = dumpp->ndcl_nlocks = 0;
883 dumpp->ndcl_ndelegs = dumpp->ndcl_nolddelegs = 0;
884 dumpp->ndcl_flags = clp->lc_flags;
885 dumpp->ndcl_clid.nclid_idlen = clp->lc_idlen;
886 NFSBCOPY(clp->lc_id, dumpp->ndcl_clid.nclid_id, clp->lc_idlen);
887 sad = NFSSOCKADDR(clp->lc_req.nr_nam, struct sockaddr *);
888 dumpp->ndcl_addrfam = sad->sa_family;
889 if (sad->sa_family == AF_INET) {
890 rad = (struct sockaddr_in *)sad;
891 dumpp->ndcl_cbaddr.sin_addr = rad->sin_addr;
893 rad6 = (struct sockaddr_in6 *)sad;
894 dumpp->ndcl_cbaddr.sin6_addr = rad6->sin6_addr;
898 * Now, scan the state lists and total up the opens and locks.
900 LIST_FOREACH(stp, &clp->lc_open, ls_list) {
901 dumpp->ndcl_nopenowners++;
902 LIST_FOREACH(openstp, &stp->ls_open, ls_list) {
903 dumpp->ndcl_nopens++;
904 LIST_FOREACH(lckownstp, &openstp->ls_open, ls_list) {
905 dumpp->ndcl_nlockowners++;
906 LIST_FOREACH(lop, &lckownstp->ls_lock, lo_lckowner) {
907 dumpp->ndcl_nlocks++;
914 * and the delegation lists.
916 LIST_FOREACH(stp, &clp->lc_deleg, ls_list) {
917 dumpp->ndcl_ndelegs++;
919 LIST_FOREACH(stp, &clp->lc_olddeleg, ls_list) {
920 dumpp->ndcl_nolddelegs++;
925 * Dump out lock stats for a file.
928 nfsrv_dumplocks(vnode_t vp, struct nfsd_dumplocks *ldumpp, int maxcnt,
931 struct nfsstate *stp;
934 struct nfslockfile *lfp;
935 struct sockaddr *sad;
936 struct sockaddr_in *rad;
937 struct sockaddr_in6 *rad6;
941 ret = nfsrv_getlockfh(vp, 0, NULL, &nfh, p);
943 * First, get a reference on the nfsv4rootfs_lock so that an
944 * exclusive lock on it cannot be acquired while dumping the locks.
946 NFSLOCKV4ROOTMUTEX();
947 nfsv4_getref(&nfsv4rootfs_lock, NULL, NFSV4ROOTLOCKMUTEXPTR, NULL);
948 NFSUNLOCKV4ROOTMUTEX();
951 ret = nfsrv_getlockfile(0, NULL, &lfp, &nfh, 0);
953 ldumpp[0].ndlck_clid.nclid_idlen = 0;
955 NFSLOCKV4ROOTMUTEX();
956 nfsv4_relref(&nfsv4rootfs_lock);
957 NFSUNLOCKV4ROOTMUTEX();
962 * For each open share on file, dump it out.
964 stp = LIST_FIRST(&lfp->lf_open);
965 while (stp != LIST_END(&lfp->lf_open) && cnt < maxcnt) {
966 ldumpp[cnt].ndlck_flags = stp->ls_flags;
967 ldumpp[cnt].ndlck_stateid.seqid = stp->ls_stateid.seqid;
968 ldumpp[cnt].ndlck_stateid.other[0] = stp->ls_stateid.other[0];
969 ldumpp[cnt].ndlck_stateid.other[1] = stp->ls_stateid.other[1];
970 ldumpp[cnt].ndlck_stateid.other[2] = stp->ls_stateid.other[2];
971 ldumpp[cnt].ndlck_owner.nclid_idlen =
972 stp->ls_openowner->ls_ownerlen;
973 NFSBCOPY(stp->ls_openowner->ls_owner,
974 ldumpp[cnt].ndlck_owner.nclid_id,
975 stp->ls_openowner->ls_ownerlen);
976 ldumpp[cnt].ndlck_clid.nclid_idlen = stp->ls_clp->lc_idlen;
977 NFSBCOPY(stp->ls_clp->lc_id, ldumpp[cnt].ndlck_clid.nclid_id,
978 stp->ls_clp->lc_idlen);
979 sad=NFSSOCKADDR(stp->ls_clp->lc_req.nr_nam, struct sockaddr *);
980 ldumpp[cnt].ndlck_addrfam = sad->sa_family;
981 if (sad->sa_family == AF_INET) {
982 rad = (struct sockaddr_in *)sad;
983 ldumpp[cnt].ndlck_cbaddr.sin_addr = rad->sin_addr;
985 rad6 = (struct sockaddr_in6 *)sad;
986 ldumpp[cnt].ndlck_cbaddr.sin6_addr = rad6->sin6_addr;
988 stp = LIST_NEXT(stp, ls_file);
995 lop = LIST_FIRST(&lfp->lf_lock);
996 while (lop != LIST_END(&lfp->lf_lock) && cnt < maxcnt) {
998 ldumpp[cnt].ndlck_flags = lop->lo_flags;
999 ldumpp[cnt].ndlck_first = lop->lo_first;
1000 ldumpp[cnt].ndlck_end = lop->lo_end;
1001 ldumpp[cnt].ndlck_stateid.seqid = stp->ls_stateid.seqid;
1002 ldumpp[cnt].ndlck_stateid.other[0] = stp->ls_stateid.other[0];
1003 ldumpp[cnt].ndlck_stateid.other[1] = stp->ls_stateid.other[1];
1004 ldumpp[cnt].ndlck_stateid.other[2] = stp->ls_stateid.other[2];
1005 ldumpp[cnt].ndlck_owner.nclid_idlen = stp->ls_ownerlen;
1006 NFSBCOPY(stp->ls_owner, ldumpp[cnt].ndlck_owner.nclid_id,
1008 ldumpp[cnt].ndlck_clid.nclid_idlen = stp->ls_clp->lc_idlen;
1009 NFSBCOPY(stp->ls_clp->lc_id, ldumpp[cnt].ndlck_clid.nclid_id,
1010 stp->ls_clp->lc_idlen);
1011 sad=NFSSOCKADDR(stp->ls_clp->lc_req.nr_nam, struct sockaddr *);
1012 ldumpp[cnt].ndlck_addrfam = sad->sa_family;
1013 if (sad->sa_family == AF_INET) {
1014 rad = (struct sockaddr_in *)sad;
1015 ldumpp[cnt].ndlck_cbaddr.sin_addr = rad->sin_addr;
1017 rad6 = (struct sockaddr_in6 *)sad;
1018 ldumpp[cnt].ndlck_cbaddr.sin6_addr = rad6->sin6_addr;
1020 lop = LIST_NEXT(lop, lo_lckfile);
1025 * and the delegations.
1027 stp = LIST_FIRST(&lfp->lf_deleg);
1028 while (stp != LIST_END(&lfp->lf_deleg) && cnt < maxcnt) {
1029 ldumpp[cnt].ndlck_flags = stp->ls_flags;
1030 ldumpp[cnt].ndlck_stateid.seqid = stp->ls_stateid.seqid;
1031 ldumpp[cnt].ndlck_stateid.other[0] = stp->ls_stateid.other[0];
1032 ldumpp[cnt].ndlck_stateid.other[1] = stp->ls_stateid.other[1];
1033 ldumpp[cnt].ndlck_stateid.other[2] = stp->ls_stateid.other[2];
1034 ldumpp[cnt].ndlck_owner.nclid_idlen = 0;
1035 ldumpp[cnt].ndlck_clid.nclid_idlen = stp->ls_clp->lc_idlen;
1036 NFSBCOPY(stp->ls_clp->lc_id, ldumpp[cnt].ndlck_clid.nclid_id,
1037 stp->ls_clp->lc_idlen);
1038 sad=NFSSOCKADDR(stp->ls_clp->lc_req.nr_nam, struct sockaddr *);
1039 ldumpp[cnt].ndlck_addrfam = sad->sa_family;
1040 if (sad->sa_family == AF_INET) {
1041 rad = (struct sockaddr_in *)sad;
1042 ldumpp[cnt].ndlck_cbaddr.sin_addr = rad->sin_addr;
1044 rad6 = (struct sockaddr_in6 *)sad;
1045 ldumpp[cnt].ndlck_cbaddr.sin6_addr = rad6->sin6_addr;
1047 stp = LIST_NEXT(stp, ls_file);
1052 * If list isn't full, mark end of list by setting the client name
1056 ldumpp[cnt].ndlck_clid.nclid_idlen = 0;
1058 NFSLOCKV4ROOTMUTEX();
1059 nfsv4_relref(&nfsv4rootfs_lock);
1060 NFSUNLOCKV4ROOTMUTEX();
1064 * Server timer routine. It can scan any linked list, so long
1065 * as it holds the spin/mutex lock and there is no exclusive lock on
1067 * (For OpenBSD, a kthread is ok. For FreeBSD, I think it is ok
1068 * to do this from a callout, since the spin locks work. For
1069 * Darwin, I'm not sure what will work correctly yet.)
1070 * Should be called once per second.
1073 nfsrv_servertimer(void)
1075 struct nfsclient *clp, *nclp;
1076 struct nfsstate *stp, *nstp;
1080 * Make sure nfsboottime is set. This is used by V3 as well
1081 * as V4. Note that nfsboottime is not nfsrvboottime, which is
1082 * only used by the V4 server for leases.
1084 if (nfsboottime.tv_sec == 0)
1085 NFSSETBOOTTIME(nfsboottime);
1088 * If server hasn't started yet, just return.
1091 if (nfsrv_stablefirst.nsf_eograce == 0) {
1095 if (!(nfsrv_stablefirst.nsf_flags & NFSNSF_UPDATEDONE)) {
1096 if (!(nfsrv_stablefirst.nsf_flags & NFSNSF_GRACEOVER) &&
1097 NFSD_MONOSEC > nfsrv_stablefirst.nsf_eograce)
1098 nfsrv_stablefirst.nsf_flags |=
1099 (NFSNSF_GRACEOVER | NFSNSF_NEEDLOCK);
1105 * Try and get a reference count on the nfsv4rootfs_lock so that
1106 * no nfsd thread can acquire an exclusive lock on it before this
1107 * call is done. If it is already exclusively locked, just return.
1109 NFSLOCKV4ROOTMUTEX();
1110 got_ref = nfsv4_getref_nonblock(&nfsv4rootfs_lock);
1111 NFSUNLOCKV4ROOTMUTEX();
1118 * For each client...
1120 for (i = 0; i < nfsrv_clienthashsize; i++) {
1121 clp = LIST_FIRST(&nfsclienthash[i]);
1122 while (clp != LIST_END(&nfsclienthash[i])) {
1123 nclp = LIST_NEXT(clp, lc_hash);
1124 if (!(clp->lc_flags & LCL_EXPIREIT)) {
1125 if (((clp->lc_expiry + NFSRV_STALELEASE) < NFSD_MONOSEC
1126 && ((LIST_EMPTY(&clp->lc_deleg)
1127 && LIST_EMPTY(&clp->lc_open)) ||
1128 nfsrv_clients > nfsrv_clienthighwater)) ||
1129 (clp->lc_expiry + NFSRV_MOULDYLEASE) < NFSD_MONOSEC ||
1130 (clp->lc_expiry < NFSD_MONOSEC &&
1131 (nfsrv_openpluslock * 10 / 9) > nfsrv_v4statelimit)) {
1133 * Lease has expired several nfsrv_lease times ago:
1135 * - no state is associated with it
1137 * - above high water mark for number of clients
1138 * (nfsrv_clienthighwater should be large enough
1139 * that this only occurs when clients fail to
1140 * use the same nfs_client_id4.id. Maybe somewhat
1141 * higher that the maximum number of clients that
1142 * will mount this server?)
1144 * Lease has expired a very long time ago
1146 * Lease has expired PLUS the number of opens + locks
1147 * has exceeded 90% of capacity
1149 * --> Mark for expiry. The actual expiry will be done
1150 * by an nfsd sometime soon.
1152 clp->lc_flags |= LCL_EXPIREIT;
1153 nfsrv_stablefirst.nsf_flags |=
1154 (NFSNSF_NEEDLOCK | NFSNSF_EXPIREDCLIENT);
1157 * If there are no opens, increment no open tick cnt
1158 * If time exceeds NFSNOOPEN, mark it to be thrown away
1159 * otherwise, if there is an open, reset no open time
1160 * Hopefully, this will avoid excessive re-creation
1161 * of open owners and subsequent open confirms.
1163 stp = LIST_FIRST(&clp->lc_open);
1164 while (stp != LIST_END(&clp->lc_open)) {
1165 nstp = LIST_NEXT(stp, ls_list);
1166 if (LIST_EMPTY(&stp->ls_open)) {
1168 if (stp->ls_noopens > NFSNOOPEN ||
1169 (nfsrv_openpluslock * 2) >
1171 nfsrv_stablefirst.nsf_flags |=
1174 stp->ls_noopens = 0;
1184 NFSLOCKV4ROOTMUTEX();
1185 nfsv4_relref(&nfsv4rootfs_lock);
1186 NFSUNLOCKV4ROOTMUTEX();
1190 * The following set of functions free up the various data structures.
1193 * Clear out all open/lock state related to this nfsclient.
1194 * Caller must hold an exclusive lock on nfsv4rootfs_lock, so that
1195 * there are no other active nfsd threads.
1198 nfsrv_cleanclient(struct nfsclient *clp, NFSPROC_T *p)
1200 struct nfsstate *stp, *nstp;
1201 struct nfsdsession *sep, *nsep;
1203 LIST_FOREACH_SAFE(stp, &clp->lc_open, ls_list, nstp)
1204 nfsrv_freeopenowner(stp, 1, p);
1205 if ((clp->lc_flags & LCL_ADMINREVOKED) == 0)
1206 LIST_FOREACH_SAFE(sep, &clp->lc_session, sess_list, nsep)
1207 (void)nfsrv_freesession(sep, NULL);
1211 * Free a client that has been cleaned. It should also already have been
1212 * removed from the lists.
1213 * (Just to be safe w.r.t. newnfs_disconnect(), call this function when
1214 * softclock interrupts are enabled.)
1217 nfsrv_zapclient(struct nfsclient *clp, NFSPROC_T *p)
1221 if ((clp->lc_flags & (LCL_GSS | LCL_CALLBACKSON)) ==
1222 (LCL_GSS | LCL_CALLBACKSON) &&
1223 (clp->lc_hand.nfsh_flag & NFSG_COMPLETE) &&
1224 clp->lc_handlelen > 0) {
1225 clp->lc_hand.nfsh_flag &= ~NFSG_COMPLETE;
1226 clp->lc_hand.nfsh_flag |= NFSG_DESTROYED;
1227 (void) nfsrv_docallback(clp, NFSV4PROC_CBNULL,
1228 NULL, 0, NULL, NULL, NULL, p);
1231 newnfs_disconnect(&clp->lc_req);
1232 NFSSOCKADDRFREE(clp->lc_req.nr_nam);
1233 NFSFREEMUTEX(&clp->lc_req.nr_mtx);
1234 free(clp->lc_stateid, M_NFSDCLIENT);
1235 free(clp, M_NFSDCLIENT);
1237 newnfsstats.srvclients--;
1238 nfsrv_openpluslock--;
1244 * Free a list of delegation state structures.
1245 * (This function will also free all nfslockfile structures that no
1246 * longer have associated state.)
1249 nfsrv_freedeleglist(struct nfsstatehead *sthp)
1251 struct nfsstate *stp, *nstp;
1253 LIST_FOREACH_SAFE(stp, sthp, ls_list, nstp) {
1254 nfsrv_freedeleg(stp);
1260 * Free up a delegation.
1263 nfsrv_freedeleg(struct nfsstate *stp)
1265 struct nfslockfile *lfp;
1267 LIST_REMOVE(stp, ls_hash);
1268 LIST_REMOVE(stp, ls_list);
1269 LIST_REMOVE(stp, ls_file);
1270 if ((stp->ls_flags & NFSLCK_DELEGWRITE) != 0)
1271 nfsrv_writedelegcnt--;
1273 if (LIST_EMPTY(&lfp->lf_open) &&
1274 LIST_EMPTY(&lfp->lf_lock) && LIST_EMPTY(&lfp->lf_deleg) &&
1275 LIST_EMPTY(&lfp->lf_locallock) && LIST_EMPTY(&lfp->lf_rollback) &&
1276 lfp->lf_usecount == 0 &&
1277 nfsv4_testlock(&lfp->lf_locallock_lck) == 0)
1278 nfsrv_freenfslockfile(lfp);
1279 FREE((caddr_t)stp, M_NFSDSTATE);
1280 newnfsstats.srvdelegates--;
1281 nfsrv_openpluslock--;
1282 nfsrv_delegatecnt--;
1286 * This function frees an open owner and all associated opens.
1289 nfsrv_freeopenowner(struct nfsstate *stp, int cansleep, NFSPROC_T *p)
1291 struct nfsstate *nstp, *tstp;
1293 LIST_REMOVE(stp, ls_list);
1295 * Now, free all associated opens.
1297 nstp = LIST_FIRST(&stp->ls_open);
1298 while (nstp != LIST_END(&stp->ls_open)) {
1300 nstp = LIST_NEXT(nstp, ls_list);
1301 (void) nfsrv_freeopen(tstp, NULL, cansleep, p);
1304 nfsrvd_derefcache(stp->ls_op);
1305 FREE((caddr_t)stp, M_NFSDSTATE);
1306 newnfsstats.srvopenowners--;
1307 nfsrv_openpluslock--;
1311 * This function frees an open (nfsstate open structure) with all associated
1312 * lock_owners and locks. It also frees the nfslockfile structure iff there
1313 * are no other opens on the file.
1314 * Returns 1 if it free'd the nfslockfile, 0 otherwise.
1317 nfsrv_freeopen(struct nfsstate *stp, vnode_t vp, int cansleep, NFSPROC_T *p)
1319 struct nfsstate *nstp, *tstp;
1320 struct nfslockfile *lfp;
1323 LIST_REMOVE(stp, ls_hash);
1324 LIST_REMOVE(stp, ls_list);
1325 LIST_REMOVE(stp, ls_file);
1329 * Now, free all lockowners associated with this open.
1331 LIST_FOREACH_SAFE(tstp, &stp->ls_open, ls_list, nstp)
1332 nfsrv_freelockowner(tstp, vp, cansleep, p);
1335 * The nfslockfile is freed here if there are no locks
1336 * associated with the open.
1337 * If there are locks associated with the open, the
1338 * nfslockfile structure can be freed via nfsrv_freelockowner().
1339 * Acquire the state mutex to avoid races with calls to
1340 * nfsrv_getlockfile().
1344 if (lfp != NULL && LIST_EMPTY(&lfp->lf_open) &&
1345 LIST_EMPTY(&lfp->lf_deleg) && LIST_EMPTY(&lfp->lf_lock) &&
1346 LIST_EMPTY(&lfp->lf_locallock) && LIST_EMPTY(&lfp->lf_rollback) &&
1347 lfp->lf_usecount == 0 &&
1348 (cansleep != 0 || nfsv4_testlock(&lfp->lf_locallock_lck) == 0)) {
1349 nfsrv_freenfslockfile(lfp);
1355 FREE((caddr_t)stp, M_NFSDSTATE);
1356 newnfsstats.srvopens--;
1357 nfsrv_openpluslock--;
1362 * Frees a lockowner and all associated locks.
1365 nfsrv_freelockowner(struct nfsstate *stp, vnode_t vp, int cansleep,
1369 LIST_REMOVE(stp, ls_hash);
1370 LIST_REMOVE(stp, ls_list);
1371 nfsrv_freeallnfslocks(stp, vp, cansleep, p);
1373 nfsrvd_derefcache(stp->ls_op);
1374 FREE((caddr_t)stp, M_NFSDSTATE);
1375 newnfsstats.srvlockowners--;
1376 nfsrv_openpluslock--;
1380 * Free all the nfs locks on a lockowner.
1383 nfsrv_freeallnfslocks(struct nfsstate *stp, vnode_t vp, int cansleep,
1386 struct nfslock *lop, *nlop;
1387 struct nfsrollback *rlp, *nrlp;
1388 struct nfslockfile *lfp = NULL;
1391 uint64_t first, end;
1394 ASSERT_VOP_UNLOCKED(vp, "nfsrv_freeallnfslocks: vnode locked");
1395 lop = LIST_FIRST(&stp->ls_lock);
1396 while (lop != LIST_END(&stp->ls_lock)) {
1397 nlop = LIST_NEXT(lop, lo_lckowner);
1399 * Since all locks should be for the same file, lfp should
1404 else if (lfp != lop->lo_lfp)
1405 panic("allnfslocks");
1407 * If vp is NULL and cansleep != 0, a vnode must be acquired
1408 * from the file handle. This only occurs when called from
1409 * nfsrv_cleanclient().
1412 if (nfsrv_dolocallocks == 0)
1414 else if (vp == NULL && cansleep != 0) {
1415 tvp = nfsvno_getvp(&lfp->lf_fh);
1416 NFSVOPUNLOCK(tvp, 0);
1425 first = lop->lo_first;
1427 nfsrv_freenfslock(lop);
1428 nfsrv_localunlock(tvp, lfp, first, end, p);
1429 LIST_FOREACH_SAFE(rlp, &lfp->lf_rollback, rlck_list,
1431 free(rlp, M_NFSDROLLBACK);
1432 LIST_INIT(&lfp->lf_rollback);
1434 nfsrv_freenfslock(lop);
1437 if (vp == NULL && tvp != NULL)
1442 * Free an nfslock structure.
1445 nfsrv_freenfslock(struct nfslock *lop)
1448 if (lop->lo_lckfile.le_prev != NULL) {
1449 LIST_REMOVE(lop, lo_lckfile);
1450 newnfsstats.srvlocks--;
1451 nfsrv_openpluslock--;
1453 LIST_REMOVE(lop, lo_lckowner);
1454 FREE((caddr_t)lop, M_NFSDLOCK);
1458 * This function frees an nfslockfile structure.
1461 nfsrv_freenfslockfile(struct nfslockfile *lfp)
1464 LIST_REMOVE(lfp, lf_hash);
1465 FREE((caddr_t)lfp, M_NFSDLOCKFILE);
1469 * This function looks up an nfsstate structure via stateid.
1472 nfsrv_getstate(struct nfsclient *clp, nfsv4stateid_t *stateidp, __unused u_int32_t flags,
1473 struct nfsstate **stpp)
1475 struct nfsstate *stp;
1476 struct nfsstatehead *hp;
1480 hp = NFSSTATEHASH(clp, *stateidp);
1481 LIST_FOREACH(stp, hp, ls_hash) {
1482 if (!NFSBCMP(stp->ls_stateid.other, stateidp->other,
1488 * If no state id in list, return NFSERR_BADSTATEID.
1490 if (stp == LIST_END(hp)) {
1491 error = NFSERR_BADSTATEID;
1502 * This function gets an nfsstate structure via owner string.
1505 nfsrv_getowner(struct nfsstatehead *hp, struct nfsstate *new_stp,
1506 struct nfsstate **stpp)
1508 struct nfsstate *stp;
1511 LIST_FOREACH(stp, hp, ls_list) {
1512 if (new_stp->ls_ownerlen == stp->ls_ownerlen &&
1513 !NFSBCMP(new_stp->ls_owner,stp->ls_owner,stp->ls_ownerlen)) {
1521 * Lock control function called to update lock status.
1522 * Returns 0 upon success, -1 if there is no lock and the flags indicate
1523 * that one isn't to be created and an NFSERR_xxx for other errors.
1524 * The structures new_stp and new_lop are passed in as pointers that should
1525 * be set to NULL if the structure is used and shouldn't be free'd.
1526 * For the NFSLCK_TEST and NFSLCK_CHECK cases, the structures are
1527 * never used and can safely be allocated on the stack. For all other
1528 * cases, *new_stpp and *new_lopp should be malloc'd before the call,
1529 * in case they are used.
1532 nfsrv_lockctrl(vnode_t vp, struct nfsstate **new_stpp,
1533 struct nfslock **new_lopp, struct nfslockconflict *cfp,
1534 nfsquad_t clientid, nfsv4stateid_t *stateidp,
1535 __unused struct nfsexstuff *exp,
1536 struct nfsrv_descript *nd, NFSPROC_T *p)
1538 struct nfslock *lop;
1539 struct nfsstate *new_stp = *new_stpp;
1540 struct nfslock *new_lop = *new_lopp;
1541 struct nfsstate *tstp, *mystp, *nstp;
1543 struct nfslockfile *lfp;
1544 struct nfslock *other_lop = NULL;
1545 struct nfsstate *stp, *lckstp = NULL;
1546 struct nfsclient *clp = NULL;
1548 int error = 0, haslock = 0, ret, reterr;
1549 int getlckret, delegation = 0, filestruct_locked, vnode_unlocked = 0;
1551 uint64_t first, end;
1552 uint32_t lock_flags;
1554 if (new_stp->ls_flags & (NFSLCK_CHECK | NFSLCK_SETATTR)) {
1556 * Note the special cases of "all 1s" or "all 0s" stateids and
1557 * let reads with all 1s go ahead.
1559 if (new_stp->ls_stateid.seqid == 0x0 &&
1560 new_stp->ls_stateid.other[0] == 0x0 &&
1561 new_stp->ls_stateid.other[1] == 0x0 &&
1562 new_stp->ls_stateid.other[2] == 0x0)
1564 else if (new_stp->ls_stateid.seqid == 0xffffffff &&
1565 new_stp->ls_stateid.other[0] == 0xffffffff &&
1566 new_stp->ls_stateid.other[1] == 0xffffffff &&
1567 new_stp->ls_stateid.other[2] == 0xffffffff)
1572 * Check for restart conditions (client and server).
1574 error = nfsrv_checkrestart(clientid, new_stp->ls_flags,
1575 &new_stp->ls_stateid, specialid);
1580 * Check for state resource limit exceeded.
1582 if ((new_stp->ls_flags & NFSLCK_LOCK) &&
1583 nfsrv_openpluslock > nfsrv_v4statelimit) {
1584 error = NFSERR_RESOURCE;
1589 * For the lock case, get another nfslock structure,
1590 * just in case we need it.
1591 * Malloc now, before we start sifting through the linked lists,
1592 * in case we have to wait for memory.
1595 if (new_stp->ls_flags & NFSLCK_LOCK)
1596 MALLOC(other_lop, struct nfslock *, sizeof (struct nfslock),
1597 M_NFSDLOCK, M_WAITOK);
1598 filestruct_locked = 0;
1603 * Get the lockfile structure for CFH now, so we can do a sanity
1604 * check against the stateid, before incrementing the seqid#, since
1605 * we want to return NFSERR_BADSTATEID on failure and the seqid#
1606 * shouldn't be incremented for this case.
1607 * If nfsrv_getlockfile() returns -1, it means "not found", which
1608 * will be handled later.
1609 * If we are doing Lock/LockU and local locking is enabled, sleep
1610 * lock the nfslockfile structure.
1612 getlckret = nfsrv_getlockfh(vp, new_stp->ls_flags, NULL, &nfh, p);
1614 if (getlckret == 0) {
1615 if ((new_stp->ls_flags & (NFSLCK_LOCK | NFSLCK_UNLOCK)) != 0 &&
1616 nfsrv_dolocallocks != 0 && nd->nd_repstat == 0) {
1617 getlckret = nfsrv_getlockfile(new_stp->ls_flags, NULL,
1620 filestruct_locked = 1;
1622 getlckret = nfsrv_getlockfile(new_stp->ls_flags, NULL,
1625 if (getlckret != 0 && getlckret != -1)
1628 if (filestruct_locked != 0) {
1629 LIST_INIT(&lfp->lf_rollback);
1630 if ((new_stp->ls_flags & NFSLCK_LOCK)) {
1632 * For local locking, do the advisory locking now, so
1633 * that any conflict can be detected. A failure later
1634 * can be rolled back locally. If an error is returned,
1635 * struct nfslockfile has been unlocked and any local
1636 * locking rolled back.
1639 if (vnode_unlocked == 0) {
1640 ASSERT_VOP_ELOCKED(vp, "nfsrv_lockctrl1");
1642 NFSVOPUNLOCK(vp, 0);
1644 reterr = nfsrv_locallock(vp, lfp,
1645 (new_lop->lo_flags & (NFSLCK_READ | NFSLCK_WRITE)),
1646 new_lop->lo_first, new_lop->lo_end, cfp, p);
1651 if (specialid == 0) {
1652 if (new_stp->ls_flags & NFSLCK_TEST) {
1654 * RFC 3530 does not list LockT as an op that renews a
1655 * lease, but the concensus seems to be that it is ok
1656 * for a server to do so.
1658 error = nfsrv_getclient(clientid, CLOPS_RENEW, &clp, NULL,
1659 (nfsquad_t)((u_quad_t)0), 0, nd, p);
1662 * Since NFSERR_EXPIRED, NFSERR_ADMINREVOKED are not valid
1663 * error returns for LockT, just go ahead and test for a lock,
1664 * since there are no locks for this client, but other locks
1665 * can conflict. (ie. same client will always be false)
1667 if (error == NFSERR_EXPIRED || error == NFSERR_ADMINREVOKED)
1671 error = nfsrv_getclient(clientid, CLOPS_RENEW, &clp, NULL,
1672 (nfsquad_t)((u_quad_t)0), 0, nd, p);
1675 * Look up the stateid
1677 error = nfsrv_getstate(clp, &new_stp->ls_stateid,
1678 new_stp->ls_flags, &stp);
1680 * do some sanity checks for an unconfirmed open or a
1681 * stateid that refers to the wrong file, for an open stateid
1683 if (error == 0 && (stp->ls_flags & NFSLCK_OPEN) &&
1684 ((stp->ls_openowner->ls_flags & NFSLCK_NEEDSCONFIRM) ||
1685 (getlckret == 0 && stp->ls_lfp != lfp)))
1686 error = NFSERR_BADSTATEID;
1688 (stp->ls_flags & (NFSLCK_DELEGREAD | NFSLCK_DELEGWRITE)) &&
1689 getlckret == 0 && stp->ls_lfp != lfp)
1690 error = NFSERR_BADSTATEID;
1693 * If the lockowner stateid doesn't refer to the same file,
1694 * I believe that is considered ok, since some clients will
1695 * only create a single lockowner and use that for all locks
1697 * For now, log it as a diagnostic, instead of considering it
1700 if (error == 0 && (stp->ls_flags &
1701 (NFSLCK_OPEN | NFSLCK_DELEGREAD | NFSLCK_DELEGWRITE)) == 0 &&
1702 getlckret == 0 && stp->ls_lfp != lfp) {
1704 printf("Got a lock statid for different file open\n");
1707 error = NFSERR_BADSTATEID;
1712 if (new_stp->ls_flags & NFSLCK_OPENTOLOCK) {
1714 * If haslock set, we've already checked the seqid.
1717 if (stp->ls_flags & NFSLCK_OPEN)
1718 error = nfsrv_checkseqid(nd, new_stp->ls_seq,
1719 stp->ls_openowner, new_stp->ls_op);
1721 error = NFSERR_BADSTATEID;
1724 nfsrv_getowner(&stp->ls_open, new_stp, &lckstp);
1727 * I believe this should be an error, but it
1728 * isn't obvious what NFSERR_xxx would be
1729 * appropriate, so I'll use NFSERR_INVAL for now.
1731 error = NFSERR_INVAL;
1734 } else if (new_stp->ls_flags&(NFSLCK_LOCK|NFSLCK_UNLOCK)) {
1736 * If haslock set, ditto above.
1739 if (stp->ls_flags & NFSLCK_OPEN)
1740 error = NFSERR_BADSTATEID;
1742 error = nfsrv_checkseqid(nd, new_stp->ls_seq,
1743 stp, new_stp->ls_op);
1751 * If the seqid part of the stateid isn't the same, return
1752 * NFSERR_OLDSTATEID for cases other than I/O Ops.
1753 * For I/O Ops, only return NFSERR_OLDSTATEID if
1754 * nfsrv_returnoldstateid is set. (The concensus on the email
1755 * list was that most clients would prefer to not receive
1756 * NFSERR_OLDSTATEID for I/O Ops, but the RFC suggests that that
1757 * is what will happen, so I use the nfsrv_returnoldstateid to
1758 * allow for either server configuration.)
1760 if (!error && stp->ls_stateid.seqid!=new_stp->ls_stateid.seqid &&
1761 (((nd->nd_flag & ND_NFSV41) == 0 &&
1762 (!(new_stp->ls_flags & NFSLCK_CHECK) ||
1763 nfsrv_returnoldstateid)) ||
1764 ((nd->nd_flag & ND_NFSV41) != 0 &&
1765 new_stp->ls_stateid.seqid != 0)))
1766 error = NFSERR_OLDSTATEID;
1771 * Now we can check for grace.
1774 error = nfsrv_checkgrace(nd, clp, new_stp->ls_flags);
1775 if ((new_stp->ls_flags & NFSLCK_RECLAIM) && !error &&
1776 nfsrv_checkstable(clp))
1777 error = NFSERR_NOGRACE;
1779 * If we successfully Reclaimed state, note that.
1781 if ((new_stp->ls_flags & NFSLCK_RECLAIM) && !error)
1782 nfsrv_markstable(clp);
1785 * At this point, either error == NFSERR_BADSTATEID or the
1786 * seqid# has been updated, so we can return any error.
1787 * If error == 0, there may be an error in:
1788 * nd_repstat - Set by the calling function.
1789 * reterr - Set above, if getting the nfslockfile structure
1790 * or acquiring the local lock failed.
1791 * (If both of these are set, nd_repstat should probably be
1792 * returned, since that error was detected before this
1795 if (error != 0 || nd->nd_repstat != 0 || reterr != 0) {
1797 if (nd->nd_repstat != 0)
1798 error = nd->nd_repstat;
1802 if (filestruct_locked != 0) {
1803 /* Roll back local locks. */
1805 if (vnode_unlocked == 0) {
1806 ASSERT_VOP_ELOCKED(vp, "nfsrv_lockctrl2");
1808 NFSVOPUNLOCK(vp, 0);
1810 nfsrv_locallock_rollback(vp, lfp, p);
1812 nfsrv_unlocklf(lfp);
1819 * Check the nfsrv_getlockfile return.
1820 * Returned -1 if no structure found.
1822 if (getlckret == -1) {
1823 error = NFSERR_EXPIRED;
1825 * Called from lockt, so no lock is OK.
1827 if (new_stp->ls_flags & NFSLCK_TEST) {
1829 } else if (new_stp->ls_flags &
1830 (NFSLCK_CHECK | NFSLCK_SETATTR)) {
1832 * Called to check for a lock, OK if the stateid is all
1833 * 1s or all 0s, but there should be an nfsstate
1835 * (ie. If there is no open, I'll assume no share
1841 error = NFSERR_BADSTATEID;
1848 * For NFSLCK_CHECK and NFSLCK_LOCK, test for a share conflict.
1849 * For NFSLCK_CHECK, allow a read if write access is granted,
1850 * but check for a deny. For NFSLCK_LOCK, require correct access,
1851 * which implies a conflicting deny can't exist.
1853 if (new_stp->ls_flags & (NFSLCK_CHECK | NFSLCK_LOCK)) {
1855 * Four kinds of state id:
1856 * - specialid (all 0s or all 1s), only for NFSLCK_CHECK
1857 * - stateid for an open
1858 * - stateid for a delegation
1859 * - stateid for a lock owner
1862 if (stp->ls_flags & (NFSLCK_DELEGREAD | NFSLCK_DELEGWRITE)) {
1865 nfsrv_delaydelegtimeout(stp);
1866 } else if (stp->ls_flags & NFSLCK_OPEN) {
1869 mystp = stp->ls_openstp;
1872 * If locking or checking, require correct access
1875 if (((new_stp->ls_flags & NFSLCK_LOCK) &&
1876 !((new_lop->lo_flags >> NFSLCK_LOCKSHIFT) &
1877 mystp->ls_flags & NFSLCK_ACCESSBITS)) ||
1878 ((new_stp->ls_flags & (NFSLCK_CHECK|NFSLCK_READACCESS)) ==
1879 (NFSLCK_CHECK | NFSLCK_READACCESS) &&
1880 !(mystp->ls_flags & NFSLCK_READACCESS) &&
1881 nfsrv_allowreadforwriteopen == 0) ||
1882 ((new_stp->ls_flags & (NFSLCK_CHECK|NFSLCK_WRITEACCESS)) ==
1883 (NFSLCK_CHECK | NFSLCK_WRITEACCESS) &&
1884 !(mystp->ls_flags & NFSLCK_WRITEACCESS))) {
1885 if (filestruct_locked != 0) {
1886 /* Roll back local locks. */
1888 if (vnode_unlocked == 0) {
1889 ASSERT_VOP_ELOCKED(vp,
1892 NFSVOPUNLOCK(vp, 0);
1894 nfsrv_locallock_rollback(vp, lfp, p);
1896 nfsrv_unlocklf(lfp);
1899 error = NFSERR_OPENMODE;
1904 if ((new_stp->ls_flags & NFSLCK_CHECK) && !delegation) {
1906 * Check for a conflicting deny bit.
1908 LIST_FOREACH(tstp, &lfp->lf_open, ls_file) {
1909 if (tstp != mystp) {
1910 bits = tstp->ls_flags;
1911 bits >>= NFSLCK_SHIFT;
1912 if (new_stp->ls_flags & bits & NFSLCK_ACCESSBITS) {
1913 KASSERT(vnode_unlocked == 0,
1914 ("nfsrv_lockctrl: vnode unlocked1"));
1915 ret = nfsrv_clientconflict(tstp->ls_clp, &haslock,
1919 * nfsrv_clientconflict unlocks state
1920 * when it returns non-zero.
1928 error = NFSERR_PERM;
1930 error = NFSERR_OPENMODE;
1936 /* We're outta here */
1943 * For setattr, just get rid of all the Delegations for other clients.
1945 if (new_stp->ls_flags & NFSLCK_SETATTR) {
1946 KASSERT(vnode_unlocked == 0,
1947 ("nfsrv_lockctrl: vnode unlocked2"));
1948 ret = nfsrv_cleandeleg(vp, lfp, clp, &haslock, p);
1951 * nfsrv_cleandeleg() unlocks state when it
1961 if (!(new_stp->ls_flags & NFSLCK_CHECK) ||
1962 (LIST_EMPTY(&lfp->lf_open) && LIST_EMPTY(&lfp->lf_lock) &&
1963 LIST_EMPTY(&lfp->lf_deleg))) {
1970 * Check for a conflicting delegation. If one is found, call
1971 * nfsrv_delegconflict() to handle it. If the v4root lock hasn't
1972 * been set yet, it will get the lock. Otherwise, it will recall
1973 * the delegation. Then, we try try again...
1974 * I currently believe the conflict algorithm to be:
1975 * For Lock Ops (Lock/LockT/LockU)
1976 * - there is a conflict iff a different client has a write delegation
1977 * For Reading (Read Op)
1978 * - there is a conflict iff a different client has a write delegation
1979 * (the specialids are always a different client)
1980 * For Writing (Write/Setattr of size)
1981 * - there is a conflict if a different client has any delegation
1982 * - there is a conflict if the same client has a read delegation
1983 * (I don't understand why this isn't allowed, but that seems to be
1984 * the current concensus?)
1986 tstp = LIST_FIRST(&lfp->lf_deleg);
1987 while (tstp != LIST_END(&lfp->lf_deleg)) {
1988 nstp = LIST_NEXT(tstp, ls_file);
1989 if ((((new_stp->ls_flags&(NFSLCK_LOCK|NFSLCK_UNLOCK|NFSLCK_TEST))||
1990 ((new_stp->ls_flags & NFSLCK_CHECK) &&
1991 (new_lop->lo_flags & NFSLCK_READ))) &&
1992 clp != tstp->ls_clp &&
1993 (tstp->ls_flags & NFSLCK_DELEGWRITE)) ||
1994 ((new_stp->ls_flags & NFSLCK_CHECK) &&
1995 (new_lop->lo_flags & NFSLCK_WRITE) &&
1996 (clp != tstp->ls_clp ||
1997 (tstp->ls_flags & NFSLCK_DELEGREAD)))) {
1999 if (filestruct_locked != 0) {
2000 /* Roll back local locks. */
2002 if (vnode_unlocked == 0) {
2003 ASSERT_VOP_ELOCKED(vp, "nfsrv_lockctrl4");
2004 NFSVOPUNLOCK(vp, 0);
2006 nfsrv_locallock_rollback(vp, lfp, p);
2008 nfsrv_unlocklf(lfp);
2010 NFSVOPLOCK(vp, LK_EXCLUSIVE | LK_RETRY);
2012 if ((vp->v_iflag & VI_DOOMED) != 0)
2013 ret = NFSERR_SERVERFAULT;
2017 ret = nfsrv_delegconflict(tstp, &haslock, p, vp);
2020 * nfsrv_delegconflict unlocks state when it
2021 * returns non-zero, which it always does.
2024 FREE((caddr_t)other_lop, M_NFSDLOCK);
2034 /* Never gets here. */
2040 * Handle the unlock case by calling nfsrv_updatelock().
2041 * (Should I have done some access checking above for unlock? For now,
2042 * just let it happen.)
2044 if (new_stp->ls_flags & NFSLCK_UNLOCK) {
2045 first = new_lop->lo_first;
2046 end = new_lop->lo_end;
2047 nfsrv_updatelock(stp, new_lopp, &other_lop, lfp);
2048 stateidp->seqid = ++(stp->ls_stateid.seqid);
2049 if ((nd->nd_flag & ND_NFSV41) != 0 && stateidp->seqid == 0)
2050 stateidp->seqid = stp->ls_stateid.seqid = 1;
2051 stateidp->other[0] = stp->ls_stateid.other[0];
2052 stateidp->other[1] = stp->ls_stateid.other[1];
2053 stateidp->other[2] = stp->ls_stateid.other[2];
2054 if (filestruct_locked != 0) {
2056 if (vnode_unlocked == 0) {
2057 ASSERT_VOP_ELOCKED(vp, "nfsrv_lockctrl5");
2059 NFSVOPUNLOCK(vp, 0);
2061 /* Update the local locks. */
2062 nfsrv_localunlock(vp, lfp, first, end, p);
2064 nfsrv_unlocklf(lfp);
2071 * Search for a conflicting lock. A lock conflicts if:
2072 * - the lock range overlaps and
2073 * - at least one lock is a write lock and
2074 * - it is not owned by the same lock owner
2077 LIST_FOREACH(lop, &lfp->lf_lock, lo_lckfile) {
2078 if (new_lop->lo_end > lop->lo_first &&
2079 new_lop->lo_first < lop->lo_end &&
2080 (new_lop->lo_flags == NFSLCK_WRITE ||
2081 lop->lo_flags == NFSLCK_WRITE) &&
2082 lckstp != lop->lo_stp &&
2083 (clp != lop->lo_stp->ls_clp ||
2084 lckstp->ls_ownerlen != lop->lo_stp->ls_ownerlen ||
2085 NFSBCMP(lckstp->ls_owner, lop->lo_stp->ls_owner,
2086 lckstp->ls_ownerlen))) {
2088 FREE((caddr_t)other_lop, M_NFSDLOCK);
2091 if (vnode_unlocked != 0)
2092 ret = nfsrv_clientconflict(lop->lo_stp->ls_clp, &haslock,
2095 ret = nfsrv_clientconflict(lop->lo_stp->ls_clp, &haslock,
2098 if (filestruct_locked != 0) {
2099 if (vnode_unlocked == 0) {
2100 ASSERT_VOP_ELOCKED(vp, "nfsrv_lockctrl6");
2101 NFSVOPUNLOCK(vp, 0);
2103 /* Roll back local locks. */
2104 nfsrv_locallock_rollback(vp, lfp, p);
2106 nfsrv_unlocklf(lfp);
2108 NFSVOPLOCK(vp, LK_EXCLUSIVE | LK_RETRY);
2110 if ((vp->v_iflag & VI_DOOMED) != 0) {
2111 error = NFSERR_SERVERFAULT;
2116 * nfsrv_clientconflict() unlocks state when it
2123 * Found a conflicting lock, so record the conflict and
2126 if (cfp != NULL && ret == 0) {
2127 cfp->cl_clientid.lval[0]=lop->lo_stp->ls_stateid.other[0];
2128 cfp->cl_clientid.lval[1]=lop->lo_stp->ls_stateid.other[1];
2129 cfp->cl_first = lop->lo_first;
2130 cfp->cl_end = lop->lo_end;
2131 cfp->cl_flags = lop->lo_flags;
2132 cfp->cl_ownerlen = lop->lo_stp->ls_ownerlen;
2133 NFSBCOPY(lop->lo_stp->ls_owner, cfp->cl_owner,
2137 error = NFSERR_PERM;
2138 else if (new_stp->ls_flags & NFSLCK_RECLAIM)
2139 error = NFSERR_RECLAIMCONFLICT;
2140 else if (new_stp->ls_flags & NFSLCK_CHECK)
2141 error = NFSERR_LOCKED;
2143 error = NFSERR_DENIED;
2144 if (filestruct_locked != 0 && ret == 0) {
2145 /* Roll back local locks. */
2147 if (vnode_unlocked == 0) {
2148 ASSERT_VOP_ELOCKED(vp, "nfsrv_lockctrl7");
2150 NFSVOPUNLOCK(vp, 0);
2152 nfsrv_locallock_rollback(vp, lfp, p);
2154 nfsrv_unlocklf(lfp);
2164 * We only get here if there was no lock that conflicted.
2166 if (new_stp->ls_flags & (NFSLCK_TEST | NFSLCK_CHECK)) {
2172 * We only get here when we are creating or modifying a lock.
2173 * There are two variants:
2174 * - exist_lock_owner where lock_owner exists
2175 * - open_to_lock_owner with new lock_owner
2177 first = new_lop->lo_first;
2178 end = new_lop->lo_end;
2179 lock_flags = new_lop->lo_flags;
2180 if (!(new_stp->ls_flags & NFSLCK_OPENTOLOCK)) {
2181 nfsrv_updatelock(lckstp, new_lopp, &other_lop, lfp);
2182 stateidp->seqid = ++(lckstp->ls_stateid.seqid);
2183 if ((nd->nd_flag & ND_NFSV41) != 0 && stateidp->seqid == 0)
2184 stateidp->seqid = lckstp->ls_stateid.seqid = 1;
2185 stateidp->other[0] = lckstp->ls_stateid.other[0];
2186 stateidp->other[1] = lckstp->ls_stateid.other[1];
2187 stateidp->other[2] = lckstp->ls_stateid.other[2];
2190 * The new open_to_lock_owner case.
2191 * Link the new nfsstate into the lists.
2193 new_stp->ls_seq = new_stp->ls_opentolockseq;
2194 nfsrvd_refcache(new_stp->ls_op);
2195 stateidp->seqid = new_stp->ls_stateid.seqid = 1;
2196 stateidp->other[0] = new_stp->ls_stateid.other[0] =
2197 clp->lc_clientid.lval[0];
2198 stateidp->other[1] = new_stp->ls_stateid.other[1] =
2199 clp->lc_clientid.lval[1];
2200 stateidp->other[2] = new_stp->ls_stateid.other[2] =
2201 nfsrv_nextstateindex(clp);
2202 new_stp->ls_clp = clp;
2203 LIST_INIT(&new_stp->ls_lock);
2204 new_stp->ls_openstp = stp;
2205 new_stp->ls_lfp = lfp;
2206 nfsrv_insertlock(new_lop, (struct nfslock *)new_stp, new_stp,
2208 LIST_INSERT_HEAD(NFSSTATEHASH(clp, new_stp->ls_stateid),
2210 LIST_INSERT_HEAD(&stp->ls_open, new_stp, ls_list);
2213 newnfsstats.srvlockowners++;
2214 nfsrv_openpluslock++;
2216 if (filestruct_locked != 0) {
2218 nfsrv_locallock_commit(lfp, lock_flags, first, end);
2220 nfsrv_unlocklf(lfp);
2226 NFSLOCKV4ROOTMUTEX();
2227 nfsv4_unlock(&nfsv4rootfs_lock, 1);
2228 NFSUNLOCKV4ROOTMUTEX();
2230 if (vnode_unlocked != 0) {
2231 NFSVOPLOCK(vp, LK_EXCLUSIVE | LK_RETRY);
2232 if (error == 0 && (vp->v_iflag & VI_DOOMED) != 0)
2233 error = NFSERR_SERVERFAULT;
2236 FREE((caddr_t)other_lop, M_NFSDLOCK);
2237 NFSEXITCODE2(error, nd);
2242 * Check for state errors for Open.
2243 * repstat is passed back out as an error if more critical errors
2247 nfsrv_opencheck(nfsquad_t clientid, nfsv4stateid_t *stateidp,
2248 struct nfsstate *new_stp, vnode_t vp, struct nfsrv_descript *nd,
2249 NFSPROC_T *p, int repstat)
2251 struct nfsstate *stp, *nstp;
2252 struct nfsclient *clp;
2253 struct nfsstate *ownerstp;
2254 struct nfslockfile *lfp, *new_lfp;
2255 int error = 0, haslock = 0, ret, readonly = 0, getfhret = 0;
2257 if ((new_stp->ls_flags & NFSLCK_SHAREBITS) == NFSLCK_READACCESS)
2260 * Check for restart conditions (client and server).
2262 error = nfsrv_checkrestart(clientid, new_stp->ls_flags,
2263 &new_stp->ls_stateid, 0);
2268 * Check for state resource limit exceeded.
2269 * Technically this should be SMP protected, but the worst
2270 * case error is "out by one or two" on the count when it
2271 * returns NFSERR_RESOURCE and the limit is just a rather
2272 * arbitrary high water mark, so no harm is done.
2274 if (nfsrv_openpluslock > nfsrv_v4statelimit) {
2275 error = NFSERR_RESOURCE;
2280 MALLOC(new_lfp, struct nfslockfile *, sizeof (struct nfslockfile),
2281 M_NFSDLOCKFILE, M_WAITOK);
2283 getfhret = nfsrv_getlockfh(vp, new_stp->ls_flags, new_lfp,
2287 * Get the nfsclient structure.
2289 error = nfsrv_getclient(clientid, CLOPS_RENEW, &clp, NULL,
2290 (nfsquad_t)((u_quad_t)0), 0, nd, p);
2293 * Look up the open owner. See if it needs confirmation and
2294 * check the seq#, as required.
2297 nfsrv_getowner(&clp->lc_open, new_stp, &ownerstp);
2299 if (!error && ownerstp) {
2300 error = nfsrv_checkseqid(nd, new_stp->ls_seq, ownerstp,
2303 * If the OpenOwner hasn't been confirmed, assume the
2304 * old one was a replay and this one is ok.
2305 * See: RFC3530 Sec. 14.2.18.
2307 if (error == NFSERR_BADSEQID &&
2308 (ownerstp->ls_flags & NFSLCK_NEEDSCONFIRM))
2316 error = nfsrv_checkgrace(nd, clp, new_stp->ls_flags);
2317 if ((new_stp->ls_flags & NFSLCK_RECLAIM) && !error &&
2318 nfsrv_checkstable(clp))
2319 error = NFSERR_NOGRACE;
2322 * If none of the above errors occurred, let repstat be
2325 if (repstat && !error)
2330 NFSLOCKV4ROOTMUTEX();
2331 nfsv4_unlock(&nfsv4rootfs_lock, 1);
2332 NFSUNLOCKV4ROOTMUTEX();
2334 free((caddr_t)new_lfp, M_NFSDLOCKFILE);
2339 * If vp == NULL, the file doesn't exist yet, so return ok.
2340 * (This always happens on the first pass, so haslock must be 0.)
2344 FREE((caddr_t)new_lfp, M_NFSDLOCKFILE);
2349 * Get the structure for the underlying file.
2354 error = nfsrv_getlockfile(new_stp->ls_flags, &new_lfp, &lfp,
2357 FREE((caddr_t)new_lfp, M_NFSDLOCKFILE);
2361 NFSLOCKV4ROOTMUTEX();
2362 nfsv4_unlock(&nfsv4rootfs_lock, 1);
2363 NFSUNLOCKV4ROOTMUTEX();
2369 * Search for a conflicting open/share.
2371 if (new_stp->ls_flags & NFSLCK_DELEGCUR) {
2373 * For Delegate_Cur, search for the matching Delegation,
2374 * which indicates no conflict.
2375 * An old delegation should have been recovered by the
2376 * client doing a Claim_DELEGATE_Prev, so I won't let
2377 * it match and return NFSERR_EXPIRED. Should I let it
2380 LIST_FOREACH(stp, &lfp->lf_deleg, ls_file) {
2381 if (!(stp->ls_flags & NFSLCK_OLDDELEG) &&
2382 (((nd->nd_flag & ND_NFSV41) != 0 &&
2383 stateidp->seqid == 0) ||
2384 stateidp->seqid == stp->ls_stateid.seqid) &&
2385 !NFSBCMP(stateidp->other, stp->ls_stateid.other,
2389 if (stp == LIST_END(&lfp->lf_deleg) ||
2390 ((new_stp->ls_flags & NFSLCK_WRITEACCESS) &&
2391 (stp->ls_flags & NFSLCK_DELEGREAD))) {
2394 NFSLOCKV4ROOTMUTEX();
2395 nfsv4_unlock(&nfsv4rootfs_lock, 1);
2396 NFSUNLOCKV4ROOTMUTEX();
2398 error = NFSERR_EXPIRED;
2404 * Check for access/deny bit conflicts. I check for the same
2405 * owner as well, in case the client didn't bother.
2407 LIST_FOREACH(stp, &lfp->lf_open, ls_file) {
2408 if (!(new_stp->ls_flags & NFSLCK_DELEGCUR) &&
2409 (((new_stp->ls_flags & NFSLCK_ACCESSBITS) &
2410 ((stp->ls_flags>>NFSLCK_SHIFT) & NFSLCK_ACCESSBITS))||
2411 ((stp->ls_flags & NFSLCK_ACCESSBITS) &
2412 ((new_stp->ls_flags>>NFSLCK_SHIFT)&NFSLCK_ACCESSBITS)))){
2413 ret = nfsrv_clientconflict(stp->ls_clp,&haslock,vp,p);
2416 * nfsrv_clientconflict() unlocks
2417 * state when it returns non-zero.
2422 error = NFSERR_PERM;
2423 else if (new_stp->ls_flags & NFSLCK_RECLAIM)
2424 error = NFSERR_RECLAIMCONFLICT;
2426 error = NFSERR_SHAREDENIED;
2430 NFSLOCKV4ROOTMUTEX();
2431 nfsv4_unlock(&nfsv4rootfs_lock, 1);
2432 NFSUNLOCKV4ROOTMUTEX();
2439 * Check for a conflicting delegation. If one is found, call
2440 * nfsrv_delegconflict() to handle it. If the v4root lock hasn't
2441 * been set yet, it will get the lock. Otherwise, it will recall
2442 * the delegation. Then, we try try again...
2443 * (If NFSLCK_DELEGCUR is set, it has a delegation, so there
2444 * isn't a conflict.)
2445 * I currently believe the conflict algorithm to be:
2446 * For Open with Read Access and Deny None
2447 * - there is a conflict iff a different client has a write delegation
2448 * For Open with other Write Access or any Deny except None
2449 * - there is a conflict if a different client has any delegation
2450 * - there is a conflict if the same client has a read delegation
2451 * (The current concensus is that this last case should be
2452 * considered a conflict since the client with a read delegation
2453 * could have done an Open with ReadAccess and WriteDeny
2454 * locally and then not have checked for the WriteDeny.)
2455 * Don't check for a Reclaim, since that will be dealt with
2456 * by nfsrv_openctrl().
2458 if (!(new_stp->ls_flags &
2459 (NFSLCK_DELEGPREV | NFSLCK_DELEGCUR | NFSLCK_RECLAIM))) {
2460 stp = LIST_FIRST(&lfp->lf_deleg);
2461 while (stp != LIST_END(&lfp->lf_deleg)) {
2462 nstp = LIST_NEXT(stp, ls_file);
2463 if ((readonly && stp->ls_clp != clp &&
2464 (stp->ls_flags & NFSLCK_DELEGWRITE)) ||
2465 (!readonly && (stp->ls_clp != clp ||
2466 (stp->ls_flags & NFSLCK_DELEGREAD)))) {
2467 ret = nfsrv_delegconflict(stp, &haslock, p, vp);
2470 * nfsrv_delegconflict() unlocks state
2471 * when it returns non-zero.
2484 NFSLOCKV4ROOTMUTEX();
2485 nfsv4_unlock(&nfsv4rootfs_lock, 1);
2486 NFSUNLOCKV4ROOTMUTEX();
2490 NFSEXITCODE2(error, nd);
2495 * Open control function to create/update open state for an open.
2498 nfsrv_openctrl(struct nfsrv_descript *nd, vnode_t vp,
2499 struct nfsstate **new_stpp, nfsquad_t clientid, nfsv4stateid_t *stateidp,
2500 nfsv4stateid_t *delegstateidp, u_int32_t *rflagsp, struct nfsexstuff *exp,
2501 NFSPROC_T *p, u_quad_t filerev)
2503 struct nfsstate *new_stp = *new_stpp;
2504 struct nfsstate *stp, *nstp;
2505 struct nfsstate *openstp = NULL, *new_open, *ownerstp, *new_deleg;
2506 struct nfslockfile *lfp, *new_lfp;
2507 struct nfsclient *clp;
2508 int error = 0, haslock = 0, ret, delegate = 1, writedeleg = 1;
2509 int readonly = 0, cbret = 1, getfhret = 0;
2510 int gotstate = 0, len = 0;
2511 u_char *clidp = NULL;
2513 if ((new_stp->ls_flags & NFSLCK_SHAREBITS) == NFSLCK_READACCESS)
2516 * Check for restart conditions (client and server).
2517 * (Paranoia, should have been detected by nfsrv_opencheck().)
2518 * If an error does show up, return NFSERR_EXPIRED, since the
2519 * the seqid# has already been incremented.
2521 error = nfsrv_checkrestart(clientid, new_stp->ls_flags,
2522 &new_stp->ls_stateid, 0);
2524 printf("Nfsd: openctrl unexpected restart err=%d\n",
2526 error = NFSERR_EXPIRED;
2530 clidp = malloc(NFSV4_OPAQUELIMIT, M_TEMP, M_WAITOK);
2532 MALLOC(new_lfp, struct nfslockfile *, sizeof (struct nfslockfile),
2533 M_NFSDLOCKFILE, M_WAITOK);
2534 MALLOC(new_open, struct nfsstate *, sizeof (struct nfsstate),
2535 M_NFSDSTATE, M_WAITOK);
2536 MALLOC(new_deleg, struct nfsstate *, sizeof (struct nfsstate),
2537 M_NFSDSTATE, M_WAITOK);
2538 getfhret = nfsrv_getlockfh(vp, new_stp->ls_flags, new_lfp,
2542 * Get the client structure. Since the linked lists could be changed
2543 * by other nfsd processes if this process does a tsleep(), one of
2544 * two things must be done.
2545 * 1 - don't tsleep()
2547 * 2 - get the nfsv4_lock() { indicated by haslock == 1 }
2548 * before using the lists, since this lock stops the other
2549 * nfsd. This should only be used for rare cases, since it
2550 * essentially single threads the nfsd.
2551 * At this time, it is only done for cases where the stable
2552 * storage file must be written prior to completion of state
2555 error = nfsrv_getclient(clientid, CLOPS_RENEW, &clp, NULL,
2556 (nfsquad_t)((u_quad_t)0), 0, nd, p);
2557 if (!error && (clp->lc_flags & LCL_NEEDSCBNULL) &&
2560 * This happens on the first open for a client
2561 * that supports callbacks.
2565 * Although nfsrv_docallback() will sleep, clp won't
2566 * go away, since they are only removed when the
2567 * nfsv4_lock() has blocked the nfsd threads. The
2568 * fields in clp can change, but having multiple
2569 * threads do this Null callback RPC should be
2572 cbret = nfsrv_docallback(clp, NFSV4PROC_CBNULL,
2573 NULL, 0, NULL, NULL, NULL, p);
2575 clp->lc_flags &= ~LCL_NEEDSCBNULL;
2577 clp->lc_flags |= LCL_CALLBACKSON;
2581 * Look up the open owner. See if it needs confirmation and
2582 * check the seq#, as required.
2585 nfsrv_getowner(&clp->lc_open, new_stp, &ownerstp);
2589 printf("Nfsd: openctrl unexpected state err=%d\n",
2591 free((caddr_t)new_lfp, M_NFSDLOCKFILE);
2592 free((caddr_t)new_open, M_NFSDSTATE);
2593 free((caddr_t)new_deleg, M_NFSDSTATE);
2595 NFSLOCKV4ROOTMUTEX();
2596 nfsv4_unlock(&nfsv4rootfs_lock, 1);
2597 NFSUNLOCKV4ROOTMUTEX();
2599 error = NFSERR_EXPIRED;
2603 if (new_stp->ls_flags & NFSLCK_RECLAIM)
2604 nfsrv_markstable(clp);
2607 * Get the structure for the underlying file.
2612 error = nfsrv_getlockfile(new_stp->ls_flags, &new_lfp, &lfp,
2615 FREE((caddr_t)new_lfp, M_NFSDLOCKFILE);
2618 printf("Nfsd openctrl unexpected getlockfile err=%d\n",
2620 free((caddr_t)new_open, M_NFSDSTATE);
2621 free((caddr_t)new_deleg, M_NFSDSTATE);
2623 NFSLOCKV4ROOTMUTEX();
2624 nfsv4_unlock(&nfsv4rootfs_lock, 1);
2625 NFSUNLOCKV4ROOTMUTEX();
2631 * Search for a conflicting open/share.
2633 if (new_stp->ls_flags & NFSLCK_DELEGCUR) {
2635 * For Delegate_Cur, search for the matching Delegation,
2636 * which indicates no conflict.
2637 * An old delegation should have been recovered by the
2638 * client doing a Claim_DELEGATE_Prev, so I won't let
2639 * it match and return NFSERR_EXPIRED. Should I let it
2642 LIST_FOREACH(stp, &lfp->lf_deleg, ls_file) {
2643 if (!(stp->ls_flags & NFSLCK_OLDDELEG) &&
2644 (((nd->nd_flag & ND_NFSV41) != 0 &&
2645 stateidp->seqid == 0) ||
2646 stateidp->seqid == stp->ls_stateid.seqid) &&
2647 !NFSBCMP(stateidp->other, stp->ls_stateid.other,
2651 if (stp == LIST_END(&lfp->lf_deleg) ||
2652 ((new_stp->ls_flags & NFSLCK_WRITEACCESS) &&
2653 (stp->ls_flags & NFSLCK_DELEGREAD))) {
2655 printf("Nfsd openctrl unexpected expiry\n");
2656 free((caddr_t)new_open, M_NFSDSTATE);
2657 free((caddr_t)new_deleg, M_NFSDSTATE);
2659 NFSLOCKV4ROOTMUTEX();
2660 nfsv4_unlock(&nfsv4rootfs_lock, 1);
2661 NFSUNLOCKV4ROOTMUTEX();
2663 error = NFSERR_EXPIRED;
2668 * Don't issue a Delegation, since one already exists and
2669 * delay delegation timeout, as required.
2672 nfsrv_delaydelegtimeout(stp);
2676 * Check for access/deny bit conflicts. I also check for the
2677 * same owner, since the client might not have bothered to check.
2678 * Also, note an open for the same file and owner, if found,
2679 * which is all we do here for Delegate_Cur, since conflict
2680 * checking is already done.
2682 LIST_FOREACH(stp, &lfp->lf_open, ls_file) {
2683 if (ownerstp && stp->ls_openowner == ownerstp)
2685 if (!(new_stp->ls_flags & NFSLCK_DELEGCUR)) {
2687 * If another client has the file open, the only
2688 * delegation that can be issued is a Read delegation
2689 * and only if it is a Read open with Deny none.
2691 if (clp != stp->ls_clp) {
2692 if ((stp->ls_flags & NFSLCK_SHAREBITS) ==
2698 if(((new_stp->ls_flags & NFSLCK_ACCESSBITS) &
2699 ((stp->ls_flags>>NFSLCK_SHIFT) & NFSLCK_ACCESSBITS))||
2700 ((stp->ls_flags & NFSLCK_ACCESSBITS) &
2701 ((new_stp->ls_flags>>NFSLCK_SHIFT)&NFSLCK_ACCESSBITS))){
2702 ret = nfsrv_clientconflict(stp->ls_clp,&haslock,vp,p);
2705 * nfsrv_clientconflict() unlocks state
2706 * when it returns non-zero.
2708 free((caddr_t)new_open, M_NFSDSTATE);
2709 free((caddr_t)new_deleg, M_NFSDSTATE);
2714 error = NFSERR_PERM;
2715 else if (new_stp->ls_flags & NFSLCK_RECLAIM)
2716 error = NFSERR_RECLAIMCONFLICT;
2718 error = NFSERR_SHAREDENIED;
2722 NFSLOCKV4ROOTMUTEX();
2723 nfsv4_unlock(&nfsv4rootfs_lock, 1);
2724 NFSUNLOCKV4ROOTMUTEX();
2726 free((caddr_t)new_open, M_NFSDSTATE);
2727 free((caddr_t)new_deleg, M_NFSDSTATE);
2728 printf("nfsd openctrl unexpected client cnfl\n");
2735 * Check for a conflicting delegation. If one is found, call
2736 * nfsrv_delegconflict() to handle it. If the v4root lock hasn't
2737 * been set yet, it will get the lock. Otherwise, it will recall
2738 * the delegation. Then, we try try again...
2739 * (If NFSLCK_DELEGCUR is set, it has a delegation, so there
2740 * isn't a conflict.)
2741 * I currently believe the conflict algorithm to be:
2742 * For Open with Read Access and Deny None
2743 * - there is a conflict iff a different client has a write delegation
2744 * For Open with other Write Access or any Deny except None
2745 * - there is a conflict if a different client has any delegation
2746 * - there is a conflict if the same client has a read delegation
2747 * (The current concensus is that this last case should be
2748 * considered a conflict since the client with a read delegation
2749 * could have done an Open with ReadAccess and WriteDeny
2750 * locally and then not have checked for the WriteDeny.)
2752 if (!(new_stp->ls_flags & (NFSLCK_DELEGPREV | NFSLCK_DELEGCUR))) {
2753 stp = LIST_FIRST(&lfp->lf_deleg);
2754 while (stp != LIST_END(&lfp->lf_deleg)) {
2755 nstp = LIST_NEXT(stp, ls_file);
2756 if (stp->ls_clp != clp && (stp->ls_flags & NFSLCK_DELEGREAD))
2760 if ((readonly && stp->ls_clp != clp &&
2761 (stp->ls_flags & NFSLCK_DELEGWRITE)) ||
2762 (!readonly && (stp->ls_clp != clp ||
2763 (stp->ls_flags & NFSLCK_DELEGREAD)))) {
2764 if (new_stp->ls_flags & NFSLCK_RECLAIM) {
2767 ret = nfsrv_delegconflict(stp, &haslock, p, vp);
2770 * nfsrv_delegconflict() unlocks state
2771 * when it returns non-zero.
2773 printf("Nfsd openctrl unexpected deleg cnfl\n");
2774 free((caddr_t)new_open, M_NFSDSTATE);
2775 free((caddr_t)new_deleg, M_NFSDSTATE);
2790 * We only get here if there was no open that conflicted.
2791 * If an open for the owner exists, or in the access/deny bits.
2792 * Otherwise it is a new open. If the open_owner hasn't been
2793 * confirmed, replace the open with the new one needing confirmation,
2794 * otherwise add the open.
2796 if (new_stp->ls_flags & NFSLCK_DELEGPREV) {
2798 * Handle NFSLCK_DELEGPREV by searching the old delegations for
2799 * a match. If found, just move the old delegation to the current
2800 * delegation list and issue open. If not found, return
2803 LIST_FOREACH(stp, &clp->lc_olddeleg, ls_list) {
2804 if (stp->ls_lfp == lfp) {
2806 if (stp->ls_clp != clp)
2807 panic("olddeleg clp");
2808 LIST_REMOVE(stp, ls_list);
2809 LIST_REMOVE(stp, ls_hash);
2810 stp->ls_flags &= ~NFSLCK_OLDDELEG;
2811 stp->ls_stateid.seqid = delegstateidp->seqid = 1;
2812 stp->ls_stateid.other[0] = delegstateidp->other[0] =
2813 clp->lc_clientid.lval[0];
2814 stp->ls_stateid.other[1] = delegstateidp->other[1] =
2815 clp->lc_clientid.lval[1];
2816 stp->ls_stateid.other[2] = delegstateidp->other[2] =
2817 nfsrv_nextstateindex(clp);
2818 stp->ls_compref = nd->nd_compref;
2819 LIST_INSERT_HEAD(&clp->lc_deleg, stp, ls_list);
2820 LIST_INSERT_HEAD(NFSSTATEHASH(clp,
2821 stp->ls_stateid), stp, ls_hash);
2822 if (stp->ls_flags & NFSLCK_DELEGWRITE)
2823 *rflagsp |= NFSV4OPEN_WRITEDELEGATE;
2825 *rflagsp |= NFSV4OPEN_READDELEGATE;
2826 clp->lc_delegtime = NFSD_MONOSEC +
2827 nfsrv_lease + NFSRV_LEASEDELTA;
2830 * Now, do the associated open.
2832 new_open->ls_stateid.seqid = 1;
2833 new_open->ls_stateid.other[0] = clp->lc_clientid.lval[0];
2834 new_open->ls_stateid.other[1] = clp->lc_clientid.lval[1];
2835 new_open->ls_stateid.other[2] = nfsrv_nextstateindex(clp);
2836 new_open->ls_flags = (new_stp->ls_flags&NFSLCK_DENYBITS)|
2838 if (stp->ls_flags & NFSLCK_DELEGWRITE)
2839 new_open->ls_flags |= (NFSLCK_READACCESS |
2840 NFSLCK_WRITEACCESS);
2842 new_open->ls_flags |= NFSLCK_READACCESS;
2843 new_open->ls_uid = new_stp->ls_uid;
2844 new_open->ls_lfp = lfp;
2845 new_open->ls_clp = clp;
2846 LIST_INIT(&new_open->ls_open);
2847 LIST_INSERT_HEAD(&lfp->lf_open, new_open, ls_file);
2848 LIST_INSERT_HEAD(NFSSTATEHASH(clp, new_open->ls_stateid),
2851 * and handle the open owner
2854 new_open->ls_openowner = ownerstp;
2855 LIST_INSERT_HEAD(&ownerstp->ls_open,new_open,ls_list);
2857 new_open->ls_openowner = new_stp;
2858 new_stp->ls_flags = 0;
2859 nfsrvd_refcache(new_stp->ls_op);
2860 new_stp->ls_noopens = 0;
2861 LIST_INIT(&new_stp->ls_open);
2862 LIST_INSERT_HEAD(&new_stp->ls_open, new_open, ls_list);
2863 LIST_INSERT_HEAD(&clp->lc_open, new_stp, ls_list);
2865 newnfsstats.srvopenowners++;
2866 nfsrv_openpluslock++;
2870 newnfsstats.srvopens++;
2871 nfsrv_openpluslock++;
2875 if (stp == LIST_END(&clp->lc_olddeleg))
2876 error = NFSERR_EXPIRED;
2877 } else if (new_stp->ls_flags & (NFSLCK_DELEGREAD | NFSLCK_DELEGWRITE)) {
2879 * Scan to see that no delegation for this client and file
2880 * doesn't already exist.
2881 * There also shouldn't yet be an Open for this file and
2884 LIST_FOREACH(stp, &lfp->lf_deleg, ls_file) {
2885 if (stp->ls_clp == clp)
2888 if (stp == LIST_END(&lfp->lf_deleg) && openstp == NULL) {
2890 * This is the Claim_Previous case with a delegation
2891 * type != Delegate_None.
2894 * First, add the delegation. (Although we must issue the
2895 * delegation, we can also ask for an immediate return.)
2897 new_deleg->ls_stateid.seqid = delegstateidp->seqid = 1;
2898 new_deleg->ls_stateid.other[0] = delegstateidp->other[0] =
2899 clp->lc_clientid.lval[0];
2900 new_deleg->ls_stateid.other[1] = delegstateidp->other[1] =
2901 clp->lc_clientid.lval[1];
2902 new_deleg->ls_stateid.other[2] = delegstateidp->other[2] =
2903 nfsrv_nextstateindex(clp);
2904 if (new_stp->ls_flags & NFSLCK_DELEGWRITE) {
2905 new_deleg->ls_flags = (NFSLCK_DELEGWRITE |
2906 NFSLCK_READACCESS | NFSLCK_WRITEACCESS);
2907 *rflagsp |= NFSV4OPEN_WRITEDELEGATE;
2908 nfsrv_writedelegcnt++;
2910 new_deleg->ls_flags = (NFSLCK_DELEGREAD |
2912 *rflagsp |= NFSV4OPEN_READDELEGATE;
2914 new_deleg->ls_uid = new_stp->ls_uid;
2915 new_deleg->ls_lfp = lfp;
2916 new_deleg->ls_clp = clp;
2917 new_deleg->ls_filerev = filerev;
2918 new_deleg->ls_compref = nd->nd_compref;
2919 LIST_INSERT_HEAD(&lfp->lf_deleg, new_deleg, ls_file);
2920 LIST_INSERT_HEAD(NFSSTATEHASH(clp,
2921 new_deleg->ls_stateid), new_deleg, ls_hash);
2922 LIST_INSERT_HEAD(&clp->lc_deleg, new_deleg, ls_list);
2924 if (delegate == 2 || nfsrv_issuedelegs == 0 ||
2925 (clp->lc_flags & (LCL_CALLBACKSON | LCL_CBDOWN)) !=
2927 NFSRV_V4DELEGLIMIT(nfsrv_delegatecnt) ||
2928 !NFSVNO_DELEGOK(vp))
2929 *rflagsp |= NFSV4OPEN_RECALL;
2930 newnfsstats.srvdelegates++;
2931 nfsrv_openpluslock++;
2932 nfsrv_delegatecnt++;
2935 * Now, do the associated open.
2937 new_open->ls_stateid.seqid = 1;
2938 new_open->ls_stateid.other[0] = clp->lc_clientid.lval[0];
2939 new_open->ls_stateid.other[1] = clp->lc_clientid.lval[1];
2940 new_open->ls_stateid.other[2] = nfsrv_nextstateindex(clp);
2941 new_open->ls_flags = (new_stp->ls_flags & NFSLCK_DENYBITS) |
2943 if (new_stp->ls_flags & NFSLCK_DELEGWRITE)
2944 new_open->ls_flags |= (NFSLCK_READACCESS |
2945 NFSLCK_WRITEACCESS);
2947 new_open->ls_flags |= NFSLCK_READACCESS;
2948 new_open->ls_uid = new_stp->ls_uid;
2949 new_open->ls_lfp = lfp;
2950 new_open->ls_clp = clp;
2951 LIST_INIT(&new_open->ls_open);
2952 LIST_INSERT_HEAD(&lfp->lf_open, new_open, ls_file);
2953 LIST_INSERT_HEAD(NFSSTATEHASH(clp, new_open->ls_stateid),
2956 * and handle the open owner
2959 new_open->ls_openowner = ownerstp;
2960 LIST_INSERT_HEAD(&ownerstp->ls_open, new_open, ls_list);
2962 new_open->ls_openowner = new_stp;
2963 new_stp->ls_flags = 0;
2964 nfsrvd_refcache(new_stp->ls_op);
2965 new_stp->ls_noopens = 0;
2966 LIST_INIT(&new_stp->ls_open);
2967 LIST_INSERT_HEAD(&new_stp->ls_open, new_open, ls_list);
2968 LIST_INSERT_HEAD(&clp->lc_open, new_stp, ls_list);
2970 newnfsstats.srvopenowners++;
2971 nfsrv_openpluslock++;
2975 newnfsstats.srvopens++;
2976 nfsrv_openpluslock++;
2978 error = NFSERR_RECLAIMCONFLICT;
2980 } else if (ownerstp) {
2981 if (ownerstp->ls_flags & NFSLCK_NEEDSCONFIRM) {
2982 /* Replace the open */
2983 if (ownerstp->ls_op)
2984 nfsrvd_derefcache(ownerstp->ls_op);
2985 ownerstp->ls_op = new_stp->ls_op;
2986 nfsrvd_refcache(ownerstp->ls_op);
2987 ownerstp->ls_seq = new_stp->ls_seq;
2988 *rflagsp |= NFSV4OPEN_RESULTCONFIRM;
2989 stp = LIST_FIRST(&ownerstp->ls_open);
2990 stp->ls_flags = (new_stp->ls_flags & NFSLCK_SHAREBITS) |
2992 stp->ls_stateid.seqid = 1;
2993 stp->ls_uid = new_stp->ls_uid;
2994 if (lfp != stp->ls_lfp) {
2995 LIST_REMOVE(stp, ls_file);
2996 LIST_INSERT_HEAD(&lfp->lf_open, stp, ls_file);
3000 } else if (openstp) {
3001 openstp->ls_flags |= (new_stp->ls_flags & NFSLCK_SHAREBITS);
3002 openstp->ls_stateid.seqid++;
3003 if ((nd->nd_flag & ND_NFSV41) != 0 &&
3004 openstp->ls_stateid.seqid == 0)
3005 openstp->ls_stateid.seqid = 1;
3008 * This is where we can choose to issue a delegation.
3010 if (delegate == 0 || writedeleg == 0 ||
3011 NFSVNO_EXRDONLY(exp) || (readonly != 0 &&
3012 nfsrv_writedelegifpos == 0) ||
3013 !NFSVNO_DELEGOK(vp) ||
3014 (new_stp->ls_flags & NFSLCK_WANTRDELEG) != 0 ||
3015 (clp->lc_flags & (LCL_CALLBACKSON | LCL_CBDOWN)) !=
3017 *rflagsp |= NFSV4OPEN_WDCONTENTION;
3018 else if (nfsrv_issuedelegs == 0 ||
3019 NFSRV_V4DELEGLIMIT(nfsrv_delegatecnt))
3020 *rflagsp |= NFSV4OPEN_WDRESOURCE;
3021 else if ((new_stp->ls_flags & NFSLCK_WANTNODELEG) != 0)
3022 *rflagsp |= NFSV4OPEN_WDNOTWANTED;
3024 new_deleg->ls_stateid.seqid = delegstateidp->seqid = 1;
3025 new_deleg->ls_stateid.other[0] = delegstateidp->other[0]
3026 = clp->lc_clientid.lval[0];
3027 new_deleg->ls_stateid.other[1] = delegstateidp->other[1]
3028 = clp->lc_clientid.lval[1];
3029 new_deleg->ls_stateid.other[2] = delegstateidp->other[2]
3030 = nfsrv_nextstateindex(clp);
3031 new_deleg->ls_flags = (NFSLCK_DELEGWRITE |
3032 NFSLCK_READACCESS | NFSLCK_WRITEACCESS);
3033 *rflagsp |= NFSV4OPEN_WRITEDELEGATE;
3034 new_deleg->ls_uid = new_stp->ls_uid;
3035 new_deleg->ls_lfp = lfp;
3036 new_deleg->ls_clp = clp;
3037 new_deleg->ls_filerev = filerev;
3038 new_deleg->ls_compref = nd->nd_compref;
3039 nfsrv_writedelegcnt++;
3040 LIST_INSERT_HEAD(&lfp->lf_deleg, new_deleg, ls_file);
3041 LIST_INSERT_HEAD(NFSSTATEHASH(clp,
3042 new_deleg->ls_stateid), new_deleg, ls_hash);
3043 LIST_INSERT_HEAD(&clp->lc_deleg, new_deleg, ls_list);
3045 newnfsstats.srvdelegates++;
3046 nfsrv_openpluslock++;
3047 nfsrv_delegatecnt++;
3050 new_open->ls_stateid.seqid = 1;
3051 new_open->ls_stateid.other[0] = clp->lc_clientid.lval[0];
3052 new_open->ls_stateid.other[1] = clp->lc_clientid.lval[1];
3053 new_open->ls_stateid.other[2] = nfsrv_nextstateindex(clp);
3054 new_open->ls_flags = (new_stp->ls_flags & NFSLCK_SHAREBITS)|
3056 new_open->ls_uid = new_stp->ls_uid;
3057 new_open->ls_openowner = ownerstp;
3058 new_open->ls_lfp = lfp;
3059 new_open->ls_clp = clp;
3060 LIST_INIT(&new_open->ls_open);
3061 LIST_INSERT_HEAD(&lfp->lf_open, new_open, ls_file);
3062 LIST_INSERT_HEAD(&ownerstp->ls_open, new_open, ls_list);
3063 LIST_INSERT_HEAD(NFSSTATEHASH(clp, new_open->ls_stateid),
3067 newnfsstats.srvopens++;
3068 nfsrv_openpluslock++;
3071 * This is where we can choose to issue a delegation.
3073 if (delegate == 0 || (writedeleg == 0 && readonly == 0) ||
3074 !NFSVNO_DELEGOK(vp) ||
3075 (clp->lc_flags & (LCL_CALLBACKSON | LCL_CBDOWN)) !=
3077 *rflagsp |= NFSV4OPEN_WDCONTENTION;
3078 else if (nfsrv_issuedelegs == 0 ||
3079 NFSRV_V4DELEGLIMIT(nfsrv_delegatecnt))
3080 *rflagsp |= NFSV4OPEN_WDRESOURCE;
3081 else if ((new_stp->ls_flags & NFSLCK_WANTNODELEG) != 0)
3082 *rflagsp |= NFSV4OPEN_WDNOTWANTED;
3084 new_deleg->ls_stateid.seqid = delegstateidp->seqid = 1;
3085 new_deleg->ls_stateid.other[0] = delegstateidp->other[0]
3086 = clp->lc_clientid.lval[0];
3087 new_deleg->ls_stateid.other[1] = delegstateidp->other[1]
3088 = clp->lc_clientid.lval[1];
3089 new_deleg->ls_stateid.other[2] = delegstateidp->other[2]
3090 = nfsrv_nextstateindex(clp);
3091 if (writedeleg && !NFSVNO_EXRDONLY(exp) &&
3092 (nfsrv_writedelegifpos || !readonly) &&
3093 (new_stp->ls_flags & NFSLCK_WANTRDELEG) == 0) {
3094 new_deleg->ls_flags = (NFSLCK_DELEGWRITE |
3095 NFSLCK_READACCESS | NFSLCK_WRITEACCESS);
3096 *rflagsp |= NFSV4OPEN_WRITEDELEGATE;
3097 nfsrv_writedelegcnt++;
3099 new_deleg->ls_flags = (NFSLCK_DELEGREAD |
3101 *rflagsp |= NFSV4OPEN_READDELEGATE;
3103 new_deleg->ls_uid = new_stp->ls_uid;
3104 new_deleg->ls_lfp = lfp;
3105 new_deleg->ls_clp = clp;
3106 new_deleg->ls_filerev = filerev;
3107 new_deleg->ls_compref = nd->nd_compref;
3108 LIST_INSERT_HEAD(&lfp->lf_deleg, new_deleg, ls_file);
3109 LIST_INSERT_HEAD(NFSSTATEHASH(clp,
3110 new_deleg->ls_stateid), new_deleg, ls_hash);
3111 LIST_INSERT_HEAD(&clp->lc_deleg, new_deleg, ls_list);
3113 newnfsstats.srvdelegates++;
3114 nfsrv_openpluslock++;
3115 nfsrv_delegatecnt++;
3120 * New owner case. Start the open_owner sequence with a
3121 * Needs confirmation (unless a reclaim) and hang the
3124 new_open->ls_stateid.seqid = 1;
3125 new_open->ls_stateid.other[0] = clp->lc_clientid.lval[0];
3126 new_open->ls_stateid.other[1] = clp->lc_clientid.lval[1];
3127 new_open->ls_stateid.other[2] = nfsrv_nextstateindex(clp);
3128 new_open->ls_flags = (new_stp->ls_flags & NFSLCK_SHAREBITS) |
3130 new_open->ls_uid = new_stp->ls_uid;
3131 LIST_INIT(&new_open->ls_open);
3132 new_open->ls_openowner = new_stp;
3133 new_open->ls_lfp = lfp;
3134 new_open->ls_clp = clp;
3135 LIST_INSERT_HEAD(&lfp->lf_open, new_open, ls_file);
3136 if (new_stp->ls_flags & NFSLCK_RECLAIM) {
3137 new_stp->ls_flags = 0;
3138 } else if ((nd->nd_flag & ND_NFSV41) != 0) {
3139 /* NFSv4.1 never needs confirmation. */
3140 new_stp->ls_flags = 0;
3143 * This is where we can choose to issue a delegation.
3145 if (delegate && nfsrv_issuedelegs &&
3146 (writedeleg || readonly) &&
3147 (clp->lc_flags & (LCL_CALLBACKSON | LCL_CBDOWN)) ==
3149 !NFSRV_V4DELEGLIMIT(nfsrv_delegatecnt) &&
3150 NFSVNO_DELEGOK(vp) &&
3151 ((nd->nd_flag & ND_NFSV41) == 0 ||
3152 (new_stp->ls_flags & NFSLCK_WANTNODELEG) == 0)) {
3153 new_deleg->ls_stateid.seqid =
3154 delegstateidp->seqid = 1;
3155 new_deleg->ls_stateid.other[0] =
3156 delegstateidp->other[0]
3157 = clp->lc_clientid.lval[0];
3158 new_deleg->ls_stateid.other[1] =
3159 delegstateidp->other[1]
3160 = clp->lc_clientid.lval[1];
3161 new_deleg->ls_stateid.other[2] =
3162 delegstateidp->other[2]
3163 = nfsrv_nextstateindex(clp);
3164 if (writedeleg && !NFSVNO_EXRDONLY(exp) &&
3165 (nfsrv_writedelegifpos || !readonly) &&
3166 ((nd->nd_flag & ND_NFSV41) == 0 ||
3167 (new_stp->ls_flags & NFSLCK_WANTRDELEG) ==
3169 new_deleg->ls_flags =
3170 (NFSLCK_DELEGWRITE |
3172 NFSLCK_WRITEACCESS);
3173 *rflagsp |= NFSV4OPEN_WRITEDELEGATE;
3174 nfsrv_writedelegcnt++;
3176 new_deleg->ls_flags =
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,
3188 LIST_INSERT_HEAD(NFSSTATEHASH(clp,
3189 new_deleg->ls_stateid), new_deleg, ls_hash);
3190 LIST_INSERT_HEAD(&clp->lc_deleg, new_deleg,
3193 newnfsstats.srvdelegates++;
3194 nfsrv_openpluslock++;
3195 nfsrv_delegatecnt++;
3198 * Since NFSv4.1 never does an OpenConfirm, the first
3199 * open state will be acquired here.
3201 if (!(clp->lc_flags & LCL_STAMPEDSTABLE)) {
3202 clp->lc_flags |= LCL_STAMPEDSTABLE;
3203 len = clp->lc_idlen;
3204 NFSBCOPY(clp->lc_id, clidp, len);
3208 *rflagsp |= NFSV4OPEN_RESULTCONFIRM;
3209 new_stp->ls_flags = NFSLCK_NEEDSCONFIRM;
3211 nfsrvd_refcache(new_stp->ls_op);
3212 new_stp->ls_noopens = 0;
3213 LIST_INIT(&new_stp->ls_open);
3214 LIST_INSERT_HEAD(&new_stp->ls_open, new_open, ls_list);
3215 LIST_INSERT_HEAD(&clp->lc_open, new_stp, ls_list);
3216 LIST_INSERT_HEAD(NFSSTATEHASH(clp, new_open->ls_stateid),
3221 newnfsstats.srvopens++;
3222 nfsrv_openpluslock++;
3223 newnfsstats.srvopenowners++;
3224 nfsrv_openpluslock++;
3227 stateidp->seqid = openstp->ls_stateid.seqid;
3228 stateidp->other[0] = openstp->ls_stateid.other[0];
3229 stateidp->other[1] = openstp->ls_stateid.other[1];
3230 stateidp->other[2] = openstp->ls_stateid.other[2];
3234 NFSLOCKV4ROOTMUTEX();
3235 nfsv4_unlock(&nfsv4rootfs_lock, 1);
3236 NFSUNLOCKV4ROOTMUTEX();
3239 FREE((caddr_t)new_open, M_NFSDSTATE);
3241 FREE((caddr_t)new_deleg, M_NFSDSTATE);
3244 * If the NFSv4.1 client just acquired its first open, write a timestamp
3245 * to the stable storage file.
3247 if (gotstate != 0) {
3248 nfsrv_writestable(clidp, len, NFSNST_NEWSTATE, p);
3249 nfsrv_backupstable();
3253 free(clidp, M_TEMP);
3254 NFSEXITCODE2(error, nd);
3259 * Open update. Does the confirm, downgrade and close.
3262 nfsrv_openupdate(vnode_t vp, struct nfsstate *new_stp, nfsquad_t clientid,
3263 nfsv4stateid_t *stateidp, struct nfsrv_descript *nd, NFSPROC_T *p)
3265 struct nfsstate *stp, *ownerstp;
3266 struct nfsclient *clp;
3267 struct nfslockfile *lfp;
3269 int error = 0, gotstate = 0, len = 0;
3270 u_char *clidp = NULL;
3273 * Check for restart conditions (client and server).
3275 error = nfsrv_checkrestart(clientid, new_stp->ls_flags,
3276 &new_stp->ls_stateid, 0);
3280 clidp = malloc(NFSV4_OPAQUELIMIT, M_TEMP, M_WAITOK);
3283 * Get the open structure via clientid and stateid.
3285 error = nfsrv_getclient(clientid, CLOPS_RENEW, &clp, NULL,
3286 (nfsquad_t)((u_quad_t)0), 0, nd, p);
3288 error = nfsrv_getstate(clp, &new_stp->ls_stateid,
3289 new_stp->ls_flags, &stp);
3292 * Sanity check the open.
3294 if (!error && (!(stp->ls_flags & NFSLCK_OPEN) ||
3295 (!(new_stp->ls_flags & NFSLCK_CONFIRM) &&
3296 (stp->ls_openowner->ls_flags & NFSLCK_NEEDSCONFIRM)) ||
3297 ((new_stp->ls_flags & NFSLCK_CONFIRM) &&
3298 (!(stp->ls_openowner->ls_flags & NFSLCK_NEEDSCONFIRM)))))
3299 error = NFSERR_BADSTATEID;
3302 error = nfsrv_checkseqid(nd, new_stp->ls_seq,
3303 stp->ls_openowner, new_stp->ls_op);
3304 if (!error && stp->ls_stateid.seqid != new_stp->ls_stateid.seqid &&
3305 (((nd->nd_flag & ND_NFSV41) == 0 &&
3306 !(new_stp->ls_flags & NFSLCK_CONFIRM)) ||
3307 ((nd->nd_flag & ND_NFSV41) != 0 &&
3308 new_stp->ls_stateid.seqid != 0)))
3309 error = NFSERR_OLDSTATEID;
3310 if (!error && vnode_vtype(vp) != VREG) {
3311 if (vnode_vtype(vp) == VDIR)
3312 error = NFSERR_ISDIR;
3314 error = NFSERR_INVAL;
3319 * If a client tries to confirm an Open with a bad
3320 * seqid# and there are no byte range locks or other Opens
3321 * on the openowner, just throw it away, so the next use of the
3322 * openowner will start a fresh seq#.
3324 if (error == NFSERR_BADSEQID &&
3325 (new_stp->ls_flags & NFSLCK_CONFIRM) &&
3326 nfsrv_nootherstate(stp))
3327 nfsrv_freeopenowner(stp->ls_openowner, 0, p);
3333 * Set the return stateid.
3335 stateidp->seqid = stp->ls_stateid.seqid + 1;
3336 if ((nd->nd_flag & ND_NFSV41) != 0 && stateidp->seqid == 0)
3337 stateidp->seqid = 1;
3338 stateidp->other[0] = stp->ls_stateid.other[0];
3339 stateidp->other[1] = stp->ls_stateid.other[1];
3340 stateidp->other[2] = stp->ls_stateid.other[2];
3342 * Now, handle the three cases.
3344 if (new_stp->ls_flags & NFSLCK_CONFIRM) {
3346 * If the open doesn't need confirmation, it seems to me that
3347 * there is a client error, but I'll just log it and keep going?
3349 if (!(stp->ls_openowner->ls_flags & NFSLCK_NEEDSCONFIRM))
3350 printf("Nfsv4d: stray open confirm\n");
3351 stp->ls_openowner->ls_flags = 0;
3352 stp->ls_stateid.seqid++;
3353 if ((nd->nd_flag & ND_NFSV41) != 0 &&
3354 stp->ls_stateid.seqid == 0)
3355 stp->ls_stateid.seqid = 1;
3356 if (!(clp->lc_flags & LCL_STAMPEDSTABLE)) {
3357 clp->lc_flags |= LCL_STAMPEDSTABLE;
3358 len = clp->lc_idlen;
3359 NFSBCOPY(clp->lc_id, clidp, len);
3363 } else if (new_stp->ls_flags & NFSLCK_CLOSE) {
3364 ownerstp = stp->ls_openowner;
3366 if (nfsrv_dolocallocks != 0 && !LIST_EMPTY(&stp->ls_open)) {
3367 /* Get the lf lock */
3370 ASSERT_VOP_ELOCKED(vp, "nfsrv_openupdate");
3371 NFSVOPUNLOCK(vp, 0);
3372 if (nfsrv_freeopen(stp, vp, 1, p) == 0) {
3374 nfsrv_unlocklf(lfp);
3377 NFSVOPLOCK(vp, LK_EXCLUSIVE | LK_RETRY);
3379 (void) nfsrv_freeopen(stp, NULL, 0, p);
3384 * Update the share bits, making sure that the new set are a
3385 * subset of the old ones.
3387 bits = (new_stp->ls_flags & NFSLCK_SHAREBITS);
3388 if (~(stp->ls_flags) & bits) {
3390 error = NFSERR_INVAL;
3393 stp->ls_flags = (bits | NFSLCK_OPEN);
3394 stp->ls_stateid.seqid++;
3395 if ((nd->nd_flag & ND_NFSV41) != 0 &&
3396 stp->ls_stateid.seqid == 0)
3397 stp->ls_stateid.seqid = 1;
3402 * If the client just confirmed its first open, write a timestamp
3403 * to the stable storage file.
3405 if (gotstate != 0) {
3406 nfsrv_writestable(clidp, len, NFSNST_NEWSTATE, p);
3407 nfsrv_backupstable();
3411 free(clidp, M_TEMP);
3412 NFSEXITCODE2(error, nd);
3417 * Delegation update. Does the purge and return.
3420 nfsrv_delegupdate(struct nfsrv_descript *nd, nfsquad_t clientid,
3421 nfsv4stateid_t *stateidp, vnode_t vp, int op, struct ucred *cred,
3424 struct nfsstate *stp;
3425 struct nfsclient *clp;
3430 * Do a sanity check against the file handle for DelegReturn.
3433 error = nfsvno_getfh(vp, &fh, p);
3438 * Check for restart conditions (client and server).
3440 if (op == NFSV4OP_DELEGRETURN)
3441 error = nfsrv_checkrestart(clientid, NFSLCK_DELEGRETURN,
3444 error = nfsrv_checkrestart(clientid, NFSLCK_DELEGPURGE,
3449 * Get the open structure via clientid and stateid.
3452 error = nfsrv_getclient(clientid, CLOPS_RENEW, &clp, NULL,
3453 (nfsquad_t)((u_quad_t)0), 0, nd, p);
3455 if (error == NFSERR_CBPATHDOWN)
3457 if (error == NFSERR_STALECLIENTID && op == NFSV4OP_DELEGRETURN)
3458 error = NFSERR_STALESTATEID;
3460 if (!error && op == NFSV4OP_DELEGRETURN) {
3461 error = nfsrv_getstate(clp, stateidp, NFSLCK_DELEGRETURN, &stp);
3462 if (!error && stp->ls_stateid.seqid != stateidp->seqid &&
3463 ((nd->nd_flag & ND_NFSV41) == 0 || stateidp->seqid != 0))
3464 error = NFSERR_OLDSTATEID;
3467 * NFSERR_EXPIRED means that the state has gone away,
3468 * so Delegations have been purged. Just return ok.
3470 if (error == NFSERR_EXPIRED && op == NFSV4OP_DELEGPURGE) {
3480 if (op == NFSV4OP_DELEGRETURN) {
3481 if (NFSBCMP((caddr_t)&fh, (caddr_t)&stp->ls_lfp->lf_fh,
3482 sizeof (fhandle_t))) {
3484 error = NFSERR_BADSTATEID;
3487 nfsrv_freedeleg(stp);
3489 nfsrv_freedeleglist(&clp->lc_olddeleg);
3500 * Release lock owner.
3503 nfsrv_releaselckown(struct nfsstate *new_stp, nfsquad_t clientid,
3506 struct nfsstate *stp, *nstp, *openstp, *ownstp;
3507 struct nfsclient *clp;
3511 * Check for restart conditions (client and server).
3513 error = nfsrv_checkrestart(clientid, new_stp->ls_flags,
3514 &new_stp->ls_stateid, 0);
3520 * Get the lock owner by name.
3522 error = nfsrv_getclient(clientid, CLOPS_RENEW, &clp, NULL,
3523 (nfsquad_t)((u_quad_t)0), 0, NULL, p);
3528 LIST_FOREACH(ownstp, &clp->lc_open, ls_list) {
3529 LIST_FOREACH(openstp, &ownstp->ls_open, ls_list) {
3530 stp = LIST_FIRST(&openstp->ls_open);
3531 while (stp != LIST_END(&openstp->ls_open)) {
3532 nstp = LIST_NEXT(stp, ls_list);
3534 * If the owner matches, check for locks and
3535 * then free or return an error.
3537 if (stp->ls_ownerlen == new_stp->ls_ownerlen &&
3538 !NFSBCMP(stp->ls_owner, new_stp->ls_owner,
3540 if (LIST_EMPTY(&stp->ls_lock)) {
3541 nfsrv_freelockowner(stp, NULL, 0, p);
3544 error = NFSERR_LOCKSHELD;
3560 * Get the file handle for a lock structure.
3563 nfsrv_getlockfh(vnode_t vp, u_short flags, struct nfslockfile *new_lfp,
3564 fhandle_t *nfhp, NFSPROC_T *p)
3566 fhandle_t *fhp = NULL;
3570 * For lock, use the new nfslock structure, otherwise just
3571 * a fhandle_t on the stack.
3573 if (flags & NFSLCK_OPEN) {
3574 KASSERT(new_lfp != NULL, ("nfsrv_getlockfh: new_lfp NULL"));
3575 fhp = &new_lfp->lf_fh;
3579 panic("nfsrv_getlockfh");
3581 error = nfsvno_getfh(vp, fhp, p);
3587 * Get an nfs lock structure. Allocate one, as required, and return a
3589 * Returns an NFSERR_xxx upon failure or -1 to indicate no current lock.
3592 nfsrv_getlockfile(u_short flags, struct nfslockfile **new_lfpp,
3593 struct nfslockfile **lfpp, fhandle_t *nfhp, int lockit)
3595 struct nfslockfile *lfp;
3596 fhandle_t *fhp = NULL, *tfhp;
3597 struct nfslockhashhead *hp;
3598 struct nfslockfile *new_lfp = NULL;
3601 * For lock, use the new nfslock structure, otherwise just
3602 * a fhandle_t on the stack.
3604 if (flags & NFSLCK_OPEN) {
3605 new_lfp = *new_lfpp;
3606 fhp = &new_lfp->lf_fh;
3610 panic("nfsrv_getlockfile");
3613 hp = NFSLOCKHASH(fhp);
3614 LIST_FOREACH(lfp, hp, lf_hash) {
3616 if (NFSVNO_CMPFH(fhp, tfhp)) {
3623 if (!(flags & NFSLCK_OPEN))
3627 * No match, so chain the new one into the list.
3629 LIST_INIT(&new_lfp->lf_open);
3630 LIST_INIT(&new_lfp->lf_lock);
3631 LIST_INIT(&new_lfp->lf_deleg);
3632 LIST_INIT(&new_lfp->lf_locallock);
3633 LIST_INIT(&new_lfp->lf_rollback);
3634 new_lfp->lf_locallock_lck.nfslock_usecnt = 0;
3635 new_lfp->lf_locallock_lck.nfslock_lock = 0;
3636 new_lfp->lf_usecount = 0;
3637 LIST_INSERT_HEAD(hp, new_lfp, lf_hash);
3644 * This function adds a nfslock lock structure to the list for the associated
3645 * nfsstate and nfslockfile structures. It will be inserted after the
3646 * entry pointed at by insert_lop.
3649 nfsrv_insertlock(struct nfslock *new_lop, struct nfslock *insert_lop,
3650 struct nfsstate *stp, struct nfslockfile *lfp)
3652 struct nfslock *lop, *nlop;
3654 new_lop->lo_stp = stp;
3655 new_lop->lo_lfp = lfp;
3658 /* Insert in increasing lo_first order */
3659 lop = LIST_FIRST(&lfp->lf_lock);
3660 if (lop == LIST_END(&lfp->lf_lock) ||
3661 new_lop->lo_first <= lop->lo_first) {
3662 LIST_INSERT_HEAD(&lfp->lf_lock, new_lop, lo_lckfile);
3664 nlop = LIST_NEXT(lop, lo_lckfile);
3665 while (nlop != LIST_END(&lfp->lf_lock) &&
3666 nlop->lo_first < new_lop->lo_first) {
3668 nlop = LIST_NEXT(lop, lo_lckfile);
3670 LIST_INSERT_AFTER(lop, new_lop, lo_lckfile);
3673 new_lop->lo_lckfile.le_prev = NULL; /* list not used */
3677 * Insert after insert_lop, which is overloaded as stp or lfp for
3680 if (stp == NULL && (struct nfslockfile *)insert_lop == lfp)
3681 LIST_INSERT_HEAD(&lfp->lf_locallock, new_lop, lo_lckowner);
3682 else if ((struct nfsstate *)insert_lop == stp)
3683 LIST_INSERT_HEAD(&stp->ls_lock, new_lop, lo_lckowner);
3685 LIST_INSERT_AFTER(insert_lop, new_lop, lo_lckowner);
3687 newnfsstats.srvlocks++;
3688 nfsrv_openpluslock++;
3693 * This function updates the locking for a lock owner and given file. It
3694 * maintains a list of lock ranges ordered on increasing file offset that
3695 * are NFSLCK_READ or NFSLCK_WRITE and non-overlapping (aka POSIX style).
3696 * It always adds new_lop to the list and sometimes uses the one pointed
3700 nfsrv_updatelock(struct nfsstate *stp, struct nfslock **new_lopp,
3701 struct nfslock **other_lopp, struct nfslockfile *lfp)
3703 struct nfslock *new_lop = *new_lopp;
3704 struct nfslock *lop, *tlop, *ilop;
3705 struct nfslock *other_lop = *other_lopp;
3706 int unlock = 0, myfile = 0;
3710 * Work down the list until the lock is merged.
3712 if (new_lop->lo_flags & NFSLCK_UNLOCK)
3715 ilop = (struct nfslock *)stp;
3716 lop = LIST_FIRST(&stp->ls_lock);
3718 ilop = (struct nfslock *)lfp;
3719 lop = LIST_FIRST(&lfp->lf_locallock);
3721 while (lop != NULL) {
3723 * Only check locks for this file that aren't before the start of
3726 if (lop->lo_lfp == lfp) {
3728 if (lop->lo_end >= new_lop->lo_first) {
3729 if (new_lop->lo_end < lop->lo_first) {
3731 * If the new lock ends before the start of the
3732 * current lock's range, no merge, just insert
3737 if (new_lop->lo_flags == lop->lo_flags ||
3738 (new_lop->lo_first <= lop->lo_first &&
3739 new_lop->lo_end >= lop->lo_end)) {
3741 * This lock can be absorbed by the new lock/unlock.
3742 * This happens when it covers the entire range
3743 * of the old lock or is contiguous
3744 * with the old lock and is of the same type or an
3747 if (lop->lo_first < new_lop->lo_first)
3748 new_lop->lo_first = lop->lo_first;
3749 if (lop->lo_end > new_lop->lo_end)
3750 new_lop->lo_end = lop->lo_end;
3752 lop = LIST_NEXT(lop, lo_lckowner);
3753 nfsrv_freenfslock(tlop);
3758 * All these cases are for contiguous locks that are not the
3759 * same type, so they can't be merged.
3761 if (new_lop->lo_first <= lop->lo_first) {
3763 * This case is where the new lock overlaps with the
3764 * first part of the old lock. Move the start of the
3765 * old lock to just past the end of the new lock. The
3766 * new lock will be inserted in front of the old, since
3767 * ilop hasn't been updated. (We are done now.)
3769 lop->lo_first = new_lop->lo_end;
3772 if (new_lop->lo_end >= lop->lo_end) {
3774 * This case is where the new lock overlaps with the
3775 * end of the old lock's range. Move the old lock's
3776 * end to just before the new lock's first and insert
3777 * the new lock after the old lock.
3778 * Might not be done yet, since the new lock could
3779 * overlap further locks with higher ranges.
3781 lop->lo_end = new_lop->lo_first;
3783 lop = LIST_NEXT(lop, lo_lckowner);
3787 * The final case is where the new lock's range is in the
3788 * middle of the current lock's and splits the current lock
3789 * up. Use *other_lopp to handle the second part of the
3790 * split old lock range. (We are done now.)
3791 * For unlock, we use new_lop as other_lop and tmp, since
3792 * other_lop and new_lop are the same for this case.
3793 * We noted the unlock case above, so we don't need
3794 * new_lop->lo_flags any longer.
3796 tmp = new_lop->lo_first;
3797 if (other_lop == NULL) {
3799 panic("nfsd srv update unlock");
3800 other_lop = new_lop;
3803 other_lop->lo_first = new_lop->lo_end;
3804 other_lop->lo_end = lop->lo_end;
3805 other_lop->lo_flags = lop->lo_flags;
3806 other_lop->lo_stp = stp;
3807 other_lop->lo_lfp = lfp;
3809 nfsrv_insertlock(other_lop, lop, stp, lfp);
3816 lop = LIST_NEXT(lop, lo_lckowner);
3817 if (myfile && (lop == NULL || lop->lo_lfp != lfp))
3822 * Insert the new lock in the list at the appropriate place.
3825 nfsrv_insertlock(new_lop, ilop, stp, lfp);
3831 * This function handles sequencing of locks, etc.
3832 * It returns an error that indicates what the caller should do.
3835 nfsrv_checkseqid(struct nfsrv_descript *nd, u_int32_t seqid,
3836 struct nfsstate *stp, struct nfsrvcache *op)
3840 if ((nd->nd_flag & ND_NFSV41) != 0)
3841 /* NFSv4.1 ignores the open_seqid and lock_seqid. */
3843 if (op != nd->nd_rp)
3844 panic("nfsrvstate checkseqid");
3845 if (!(op->rc_flag & RC_INPROG))
3846 panic("nfsrvstate not inprog");
3847 if (stp->ls_op && stp->ls_op->rc_refcnt <= 0) {
3848 printf("refcnt=%d\n", stp->ls_op->rc_refcnt);
3849 panic("nfsrvstate op refcnt");
3851 if ((stp->ls_seq + 1) == seqid) {
3853 nfsrvd_derefcache(stp->ls_op);
3855 nfsrvd_refcache(op);
3856 stp->ls_seq = seqid;
3858 } else if (stp->ls_seq == seqid && stp->ls_op &&
3859 op->rc_xid == stp->ls_op->rc_xid &&
3860 op->rc_refcnt == 0 &&
3861 op->rc_reqlen == stp->ls_op->rc_reqlen &&
3862 op->rc_cksum == stp->ls_op->rc_cksum) {
3863 if (stp->ls_op->rc_flag & RC_INPROG) {
3864 error = NFSERR_DONTREPLY;
3867 nd->nd_rp = stp->ls_op;
3868 nd->nd_rp->rc_flag |= RC_INPROG;
3869 nfsrvd_delcache(op);
3870 error = NFSERR_REPLYFROMCACHE;
3873 error = NFSERR_BADSEQID;
3876 NFSEXITCODE2(error, nd);
3881 * Get the client ip address for callbacks. If the strings can't be parsed,
3882 * just set lc_program to 0 to indicate no callbacks are possible.
3883 * (For cases where the address can't be parsed or is 0.0.0.0.0.0, set
3884 * the address to the client's transport address. This won't be used
3885 * for callbacks, but can be printed out by newnfsstats for info.)
3886 * Return error if the xdr can't be parsed, 0 otherwise.
3889 nfsrv_getclientipaddr(struct nfsrv_descript *nd, struct nfsclient *clp)
3894 struct sockaddr_in *rad, *sad;
3895 u_char protocol[5], addr[24];
3896 int error = 0, cantparse = 0;
3906 rad = NFSSOCKADDR(clp->lc_req.nr_nam, struct sockaddr_in *);
3907 rad->sin_family = AF_INET;
3908 rad->sin_len = sizeof (struct sockaddr_in);
3909 rad->sin_addr.s_addr = 0;
3911 clp->lc_req.nr_client = NULL;
3912 clp->lc_req.nr_lock = 0;
3913 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
3914 i = fxdr_unsigned(int, *tl);
3915 if (i >= 3 && i <= 4) {
3916 error = nfsrv_mtostr(nd, protocol, i);
3919 if (!strcmp(protocol, "tcp")) {
3920 clp->lc_flags |= LCL_TCPCALLBACK;
3921 clp->lc_req.nr_sotype = SOCK_STREAM;
3922 clp->lc_req.nr_soproto = IPPROTO_TCP;
3923 } else if (!strcmp(protocol, "udp")) {
3924 clp->lc_req.nr_sotype = SOCK_DGRAM;
3925 clp->lc_req.nr_soproto = IPPROTO_UDP;
3932 error = nfsm_advance(nd, NFSM_RNDUP(i), -1);
3937 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
3938 i = fxdr_unsigned(int, *tl);
3940 error = NFSERR_BADXDR;
3942 } else if (i == 0) {
3944 } else if (!cantparse && i <= 23 && i >= 11) {
3945 error = nfsrv_mtostr(nd, addr, i);
3950 * Parse out the address fields. We expect 6 decimal numbers
3951 * separated by '.'s.
3955 while (*cp && i < 6) {
3957 while (*cp2 && *cp2 != '.')
3965 j = nfsrv_getipnumber(cp);
3970 port.cval[5 - i] = j;
3979 if (ip.ival != 0x0) {
3980 rad->sin_addr.s_addr = htonl(ip.ival);
3981 rad->sin_port = htons(port.sval);
3989 error = nfsm_advance(nd, NFSM_RNDUP(i), -1);
3995 sad = NFSSOCKADDR(nd->nd_nam, struct sockaddr_in *);
3996 if (sad->sin_family == AF_INET) {
3997 rad->sin_addr.s_addr = sad->sin_addr.s_addr;
3998 rad->sin_port = 0x0;
4000 clp->lc_program = 0;
4003 NFSEXITCODE2(error, nd);
4008 * Turn a string of up to three decimal digits into a number. Return -1 upon
4012 nfsrv_getipnumber(u_char *cp)
4017 if (j > 2 || *cp < '0' || *cp > '9')
4030 * This function checks for restart conditions.
4033 nfsrv_checkrestart(nfsquad_t clientid, u_int32_t flags,
4034 nfsv4stateid_t *stateidp, int specialid)
4039 * First check for a server restart. Open, LockT, ReleaseLockOwner
4040 * and DelegPurge have a clientid, the rest a stateid.
4043 (NFSLCK_OPEN | NFSLCK_TEST | NFSLCK_RELEASE | NFSLCK_DELEGPURGE)) {
4044 if (clientid.lval[0] != nfsrvboottime) {
4045 ret = NFSERR_STALECLIENTID;
4048 } else if (stateidp->other[0] != nfsrvboottime &&
4050 ret = NFSERR_STALESTATEID;
4055 * Read, Write, Setattr and LockT can return NFSERR_GRACE and do
4056 * not use a lock/open owner seqid#, so the check can be done now.
4057 * (The others will be checked, as required, later.)
4059 if (!(flags & (NFSLCK_CHECK | NFSLCK_TEST)))
4063 ret = nfsrv_checkgrace(NULL, NULL, flags);
4075 nfsrv_checkgrace(struct nfsrv_descript *nd, struct nfsclient *clp,
4080 if ((nfsrv_stablefirst.nsf_flags & NFSNSF_GRACEOVER) != 0) {
4081 if (flags & NFSLCK_RECLAIM) {
4082 error = NFSERR_NOGRACE;
4086 if (!(flags & NFSLCK_RECLAIM)) {
4087 error = NFSERR_GRACE;
4090 if (nd != NULL && clp != NULL &&
4091 (nd->nd_flag & ND_NFSV41) != 0 &&
4092 (clp->lc_flags & LCL_RECLAIMCOMPLETE) != 0) {
4093 error = NFSERR_NOGRACE;
4098 * If grace is almost over and we are still getting Reclaims,
4099 * extend grace a bit.
4101 if ((NFSD_MONOSEC + NFSRV_LEASEDELTA) >
4102 nfsrv_stablefirst.nsf_eograce)
4103 nfsrv_stablefirst.nsf_eograce = NFSD_MONOSEC +
4113 * Do a server callback.
4116 nfsrv_docallback(struct nfsclient *clp, int procnum,
4117 nfsv4stateid_t *stateidp, int trunc, fhandle_t *fhp,
4118 struct nfsvattr *nap, nfsattrbit_t *attrbitp, NFSPROC_T *p)
4122 struct nfsrv_descript nfsd, *nd = &nfsd;
4126 struct nfsdsession *sep = NULL;
4128 cred = newnfs_getcred();
4129 NFSLOCKSTATE(); /* mostly for lc_cbref++ */
4130 if (clp->lc_flags & LCL_NEEDSCONFIRM) {
4137 * Fill the callback program# and version into the request
4138 * structure for newnfs_connect() to use.
4140 clp->lc_req.nr_prog = clp->lc_program;
4142 if ((clp->lc_flags & LCL_NFSV41) != 0)
4143 clp->lc_req.nr_vers = NFSV41_CBVERS;
4146 clp->lc_req.nr_vers = NFSV4_CBVERS;
4149 * First, fill in some of the fields of nd and cr.
4151 nd->nd_flag = ND_NFSV4;
4152 if (clp->lc_flags & LCL_GSS)
4153 nd->nd_flag |= ND_KERBV;
4154 if ((clp->lc_flags & LCL_NFSV41) != 0)
4155 nd->nd_flag |= ND_NFSV41;
4157 cred->cr_uid = clp->lc_uid;
4158 cred->cr_gid = clp->lc_gid;
4159 callback = clp->lc_callback;
4161 cred->cr_ngroups = 1;
4164 * Get the first mbuf for the request.
4166 MGET(m, M_WAITOK, MT_DATA);
4168 nd->nd_mreq = nd->nd_mb = m;
4169 nd->nd_bpos = NFSMTOD(m, caddr_t);
4172 * and build the callback request.
4174 if (procnum == NFSV4OP_CBGETATTR) {
4175 nd->nd_procnum = NFSV4PROC_CBCOMPOUND;
4176 error = nfsrv_cbcallargs(nd, clp, callback, NFSV4OP_CBGETATTR,
4177 "CB Getattr", &sep);
4179 mbuf_freem(nd->nd_mreq);
4182 (void)nfsm_fhtom(nd, (u_int8_t *)fhp, NFSX_MYFH, 0);
4183 (void)nfsrv_putattrbit(nd, attrbitp);
4184 } else if (procnum == NFSV4OP_CBRECALL) {
4185 nd->nd_procnum = NFSV4PROC_CBCOMPOUND;
4186 error = nfsrv_cbcallargs(nd, clp, callback, NFSV4OP_CBRECALL,
4189 mbuf_freem(nd->nd_mreq);
4192 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED + NFSX_STATEID);
4193 *tl++ = txdr_unsigned(stateidp->seqid);
4194 NFSBCOPY((caddr_t)stateidp->other, (caddr_t)tl,
4196 tl += (NFSX_STATEIDOTHER / NFSX_UNSIGNED);
4201 (void)nfsm_fhtom(nd, (u_int8_t *)fhp, NFSX_MYFH, 0);
4202 } else if (procnum == NFSV4PROC_CBNULL) {
4203 nd->nd_procnum = NFSV4PROC_CBNULL;
4204 if ((clp->lc_flags & LCL_NFSV41) != 0) {
4205 error = nfsv4_getcbsession(clp, &sep);
4207 mbuf_freem(nd->nd_mreq);
4212 error = NFSERR_SERVERFAULT;
4213 mbuf_freem(nd->nd_mreq);
4218 * Call newnfs_connect(), as required, and then newnfs_request().
4220 (void) newnfs_sndlock(&clp->lc_req.nr_lock);
4221 if (clp->lc_req.nr_client == NULL) {
4222 if ((clp->lc_flags & LCL_NFSV41) != 0)
4223 error = ECONNREFUSED;
4224 else if (nd->nd_procnum == NFSV4PROC_CBNULL)
4225 error = newnfs_connect(NULL, &clp->lc_req, cred,
4228 error = newnfs_connect(NULL, &clp->lc_req, cred,
4231 newnfs_sndunlock(&clp->lc_req.nr_lock);
4233 if ((nd->nd_flag & ND_NFSV41) != 0) {
4234 KASSERT(sep != NULL, ("sep NULL"));
4235 if (sep->sess_cbsess.nfsess_xprt != NULL)
4236 error = newnfs_request(nd, NULL, clp,
4237 &clp->lc_req, NULL, NULL, cred,
4238 clp->lc_program, clp->lc_req.nr_vers, NULL,
4239 1, NULL, &sep->sess_cbsess);
4242 * This should probably never occur, but if a
4243 * client somehow does an RPC without a
4244 * SequenceID Op that causes a callback just
4245 * after the nfsd threads have been terminated
4246 * and restared we could conceivably get here
4247 * without a backchannel xprt.
4249 printf("nfsrv_docallback: no xprt\n");
4250 error = ECONNREFUSED;
4252 nfsrv_freesession(sep, NULL);
4254 error = newnfs_request(nd, NULL, clp, &clp->lc_req,
4255 NULL, NULL, cred, clp->lc_program,
4256 clp->lc_req.nr_vers, NULL, 1, NULL, NULL);
4262 * If error is set here, the Callback path isn't working
4263 * properly, so twiddle the appropriate LCL_ flags.
4264 * (nd_repstat != 0 indicates the Callback path is working,
4265 * but the callback failed on the client.)
4269 * Mark the callback pathway down, which disabled issuing
4270 * of delegations and gets Renew to return NFSERR_CBPATHDOWN.
4273 clp->lc_flags |= LCL_CBDOWN;
4277 * Callback worked. If the callback path was down, disable
4278 * callbacks, so no more delegations will be issued. (This
4279 * is done on the assumption that the callback pathway is
4283 if (clp->lc_flags & LCL_CBDOWN)
4284 clp->lc_flags &= ~(LCL_CBDOWN | LCL_CALLBACKSON);
4287 error = nd->nd_repstat;
4288 else if (error == 0 && procnum == NFSV4OP_CBGETATTR)
4289 error = nfsv4_loadattr(nd, NULL, nap, NULL, NULL, 0,
4290 NULL, NULL, NULL, NULL, NULL, 0, NULL, NULL, NULL,
4292 mbuf_freem(nd->nd_mrep);
4296 if ((clp->lc_flags & LCL_WAKEUPWANTED) && clp->lc_cbref == 0) {
4297 clp->lc_flags &= ~LCL_WAKEUPWANTED;
4307 * Set up the compound RPC for the callback.
4310 nfsrv_cbcallargs(struct nfsrv_descript *nd, struct nfsclient *clp,
4311 uint32_t callback, int op, const char *optag, struct nfsdsession **sepp)
4316 len = strlen(optag);
4317 (void)nfsm_strtom(nd, optag, len);
4318 NFSM_BUILD(tl, uint32_t *, 4 * NFSX_UNSIGNED);
4319 if ((nd->nd_flag & ND_NFSV41) != 0) {
4320 *tl++ = txdr_unsigned(NFSV41_MINORVERSION);
4321 *tl++ = txdr_unsigned(callback);
4322 *tl++ = txdr_unsigned(2);
4323 *tl = txdr_unsigned(NFSV4OP_CBSEQUENCE);
4324 error = nfsv4_setcbsequence(nd, clp, 1, sepp);
4327 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
4328 *tl = txdr_unsigned(op);
4330 *tl++ = txdr_unsigned(NFSV4_MINORVERSION);
4331 *tl++ = txdr_unsigned(callback);
4332 *tl++ = txdr_unsigned(1);
4333 *tl = txdr_unsigned(op);
4339 * Return the next index# for a clientid. Mostly just increment and return
4340 * the next one, but... if the 32bit unsigned does actually wrap around,
4341 * it should be rebooted.
4342 * At an average rate of one new client per second, it will wrap around in
4343 * approximately 136 years. (I think the server will have been shut
4344 * down or rebooted before then.)
4347 nfsrv_nextclientindex(void)
4349 static u_int32_t client_index = 0;
4352 if (client_index != 0)
4353 return (client_index);
4355 printf("%s: out of clientids\n", __func__);
4356 return (client_index);
4360 * Return the next index# for a stateid. Mostly just increment and return
4361 * the next one, but... if the 32bit unsigned does actually wrap around
4362 * (will a BSD server stay up that long?), find
4363 * new start and end values.
4366 nfsrv_nextstateindex(struct nfsclient *clp)
4368 struct nfsstate *stp;
4370 u_int32_t canuse, min_index, max_index;
4372 if (!(clp->lc_flags & LCL_INDEXNOTOK)) {
4373 clp->lc_stateindex++;
4374 if (clp->lc_stateindex != clp->lc_statemaxindex)
4375 return (clp->lc_stateindex);
4379 * Yuck, we've hit the end.
4380 * Look for a new min and max.
4383 max_index = 0xffffffff;
4384 for (i = 0; i < nfsrv_statehashsize; i++) {
4385 LIST_FOREACH(stp, &clp->lc_stateid[i], ls_hash) {
4386 if (stp->ls_stateid.other[2] > 0x80000000) {
4387 if (stp->ls_stateid.other[2] < max_index)
4388 max_index = stp->ls_stateid.other[2];
4390 if (stp->ls_stateid.other[2] > min_index)
4391 min_index = stp->ls_stateid.other[2];
4397 * Yikes, highly unlikely, but I'll handle it anyhow.
4399 if (min_index == 0x80000000 && max_index == 0x80000001) {
4402 * Loop around until we find an unused entry. Return that
4403 * and set LCL_INDEXNOTOK, so the search will continue next time.
4404 * (This is one of those rare cases where a goto is the
4405 * cleanest way to code the loop.)
4408 for (i = 0; i < nfsrv_statehashsize; i++) {
4409 LIST_FOREACH(stp, &clp->lc_stateid[i], ls_hash) {
4410 if (stp->ls_stateid.other[2] == canuse) {
4416 clp->lc_flags |= LCL_INDEXNOTOK;
4421 * Ok to start again from min + 1.
4423 clp->lc_stateindex = min_index + 1;
4424 clp->lc_statemaxindex = max_index;
4425 clp->lc_flags &= ~LCL_INDEXNOTOK;
4426 return (clp->lc_stateindex);
4430 * The following functions handle the stable storage file that deals with
4431 * the edge conditions described in RFC3530 Sec. 8.6.3.
4432 * The file is as follows:
4433 * - a single record at the beginning that has the lease time of the
4434 * previous server instance (before the last reboot) and the nfsrvboottime
4435 * values for the previous server boots.
4436 * These previous boot times are used to ensure that the current
4437 * nfsrvboottime does not, somehow, get set to a previous one.
4438 * (This is important so that Stale ClientIDs and StateIDs can
4440 * The number of previous nfsvrboottime values preceeds the list.
4441 * - followed by some number of appended records with:
4442 * - client id string
4443 * - flag that indicates it is a record revoking state via lease
4444 * expiration or similar
4445 * OR has successfully acquired state.
4446 * These structures vary in length, with the client string at the end, up
4447 * to NFSV4_OPAQUELIMIT in size.
4449 * At the end of the grace period, the file is truncated, the first
4450 * record is rewritten with updated information and any acquired state
4451 * records for successful reclaims of state are written.
4453 * Subsequent records are appended when the first state is issued to
4454 * a client and when state is revoked for a client.
4456 * When reading the file in, state issued records that come later in
4457 * the file override older ones, since the append log is in cronological order.
4458 * If, for some reason, the file can't be read, the grace period is
4459 * immediately terminated and all reclaims get NFSERR_NOGRACE.
4463 * Read in the stable storage file. Called by nfssvc() before the nfsd
4464 * processes start servicing requests.
4467 nfsrv_setupstable(NFSPROC_T *p)
4469 struct nfsrv_stablefirst *sf = &nfsrv_stablefirst;
4470 struct nfsrv_stable *sp, *nsp;
4471 struct nfst_rec *tsp;
4472 int error, i, tryagain;
4474 ssize_t aresid, len;
4477 * If NFSNSF_UPDATEDONE is set, this is a restart of the nfsds without
4478 * a reboot, so state has not been lost.
4480 if (sf->nsf_flags & NFSNSF_UPDATEDONE)
4483 * Set Grace over just until the file reads successfully.
4485 nfsrvboottime = time_second;
4486 LIST_INIT(&sf->nsf_head);
4487 sf->nsf_flags = (NFSNSF_GRACEOVER | NFSNSF_NEEDLOCK);
4488 sf->nsf_eograce = NFSD_MONOSEC + NFSRV_LEASEDELTA;
4489 if (sf->nsf_fp == NULL)
4491 error = NFSD_RDWR(UIO_READ, NFSFPVNODE(sf->nsf_fp),
4492 (caddr_t)&sf->nsf_rec, sizeof (struct nfsf_rec), off, UIO_SYSSPACE,
4493 0, NFSFPCRED(sf->nsf_fp), &aresid, p);
4494 if (error || aresid || sf->nsf_numboots == 0 ||
4495 sf->nsf_numboots > NFSNSF_MAXNUMBOOTS)
4499 * Now, read in the boottimes.
4501 sf->nsf_bootvals = (time_t *)malloc((sf->nsf_numboots + 1) *
4502 sizeof (time_t), M_TEMP, M_WAITOK);
4503 off = sizeof (struct nfsf_rec);
4504 error = NFSD_RDWR(UIO_READ, NFSFPVNODE(sf->nsf_fp),
4505 (caddr_t)sf->nsf_bootvals, sf->nsf_numboots * sizeof (time_t), off,
4506 UIO_SYSSPACE, 0, NFSFPCRED(sf->nsf_fp), &aresid, p);
4507 if (error || aresid) {
4508 free((caddr_t)sf->nsf_bootvals, M_TEMP);
4509 sf->nsf_bootvals = NULL;
4514 * Make sure this nfsrvboottime is different from all recorded
4519 for (i = 0; i < sf->nsf_numboots; i++) {
4520 if (nfsrvboottime == sf->nsf_bootvals[i]) {
4528 sf->nsf_flags |= NFSNSF_OK;
4529 off += (sf->nsf_numboots * sizeof (time_t));
4532 * Read through the file, building a list of records for grace
4534 * Each record is between sizeof (struct nfst_rec) and
4535 * sizeof (struct nfst_rec) + NFSV4_OPAQUELIMIT - 1
4536 * and is actually sizeof (struct nfst_rec) + nst_len - 1.
4538 tsp = (struct nfst_rec *)malloc(sizeof (struct nfst_rec) +
4539 NFSV4_OPAQUELIMIT - 1, M_TEMP, M_WAITOK);
4541 error = NFSD_RDWR(UIO_READ, NFSFPVNODE(sf->nsf_fp),
4542 (caddr_t)tsp, sizeof (struct nfst_rec) + NFSV4_OPAQUELIMIT - 1,
4543 off, UIO_SYSSPACE, 0, NFSFPCRED(sf->nsf_fp), &aresid, p);
4544 len = (sizeof (struct nfst_rec) + NFSV4_OPAQUELIMIT - 1) - aresid;
4545 if (error || (len > 0 && (len < sizeof (struct nfst_rec) ||
4546 len < (sizeof (struct nfst_rec) + tsp->len - 1)))) {
4548 * Yuck, the file has been corrupted, so just return
4549 * after clearing out any restart state, so the grace period
4552 LIST_FOREACH_SAFE(sp, &sf->nsf_head, nst_list, nsp) {
4553 LIST_REMOVE(sp, nst_list);
4554 free((caddr_t)sp, M_TEMP);
4556 free((caddr_t)tsp, M_TEMP);
4557 sf->nsf_flags &= ~NFSNSF_OK;
4558 free((caddr_t)sf->nsf_bootvals, M_TEMP);
4559 sf->nsf_bootvals = NULL;
4563 off += sizeof (struct nfst_rec) + tsp->len - 1;
4565 * Search the list for a matching client.
4567 LIST_FOREACH(sp, &sf->nsf_head, nst_list) {
4568 if (tsp->len == sp->nst_len &&
4569 !NFSBCMP(tsp->client, sp->nst_client, tsp->len))
4572 if (sp == LIST_END(&sf->nsf_head)) {
4573 sp = (struct nfsrv_stable *)malloc(tsp->len +
4574 sizeof (struct nfsrv_stable) - 1, M_TEMP,
4576 NFSBCOPY((caddr_t)tsp, (caddr_t)&sp->nst_rec,
4577 sizeof (struct nfst_rec) + tsp->len - 1);
4578 LIST_INSERT_HEAD(&sf->nsf_head, sp, nst_list);
4580 if (tsp->flag == NFSNST_REVOKE)
4581 sp->nst_flag |= NFSNST_REVOKE;
4584 * A subsequent timestamp indicates the client
4585 * did a setclientid/confirm and any previous
4586 * revoke is no longer relevant.
4588 sp->nst_flag &= ~NFSNST_REVOKE;
4592 free((caddr_t)tsp, M_TEMP);
4593 sf->nsf_flags = NFSNSF_OK;
4594 sf->nsf_eograce = NFSD_MONOSEC + sf->nsf_lease +
4599 * Update the stable storage file, now that the grace period is over.
4602 nfsrv_updatestable(NFSPROC_T *p)
4604 struct nfsrv_stablefirst *sf = &nfsrv_stablefirst;
4605 struct nfsrv_stable *sp, *nsp;
4607 struct nfsvattr nva;
4609 #if defined(__FreeBSD_version) && (__FreeBSD_version >= 500000)
4614 if (sf->nsf_fp == NULL || (sf->nsf_flags & NFSNSF_UPDATEDONE))
4616 sf->nsf_flags |= NFSNSF_UPDATEDONE;
4618 * Ok, we need to rewrite the stable storage file.
4619 * - truncate to 0 length
4620 * - write the new first structure
4621 * - loop through the data structures, writing out any that
4622 * have timestamps older than the old boot
4624 if (sf->nsf_bootvals) {
4626 for (i = sf->nsf_numboots - 2; i >= 0; i--)
4627 sf->nsf_bootvals[i + 1] = sf->nsf_bootvals[i];
4629 sf->nsf_numboots = 1;
4630 sf->nsf_bootvals = (time_t *)malloc(sizeof (time_t),
4633 sf->nsf_bootvals[0] = nfsrvboottime;
4634 sf->nsf_lease = nfsrv_lease;
4635 NFSVNO_ATTRINIT(&nva);
4636 NFSVNO_SETATTRVAL(&nva, size, 0);
4637 vp = NFSFPVNODE(sf->nsf_fp);
4638 vn_start_write(vp, &mp, V_WAIT);
4639 if (NFSVOPLOCK(vp, LK_EXCLUSIVE) == 0) {
4640 error = nfsvno_setattr(vp, &nva, NFSFPCRED(sf->nsf_fp), p,
4642 NFSVOPUNLOCK(vp, 0);
4645 vn_finished_write(mp);
4647 error = NFSD_RDWR(UIO_WRITE, vp,
4648 (caddr_t)&sf->nsf_rec, sizeof (struct nfsf_rec), (off_t)0,
4649 UIO_SYSSPACE, IO_SYNC, NFSFPCRED(sf->nsf_fp), NULL, p);
4651 error = NFSD_RDWR(UIO_WRITE, vp,
4652 (caddr_t)sf->nsf_bootvals,
4653 sf->nsf_numboots * sizeof (time_t),
4654 (off_t)(sizeof (struct nfsf_rec)),
4655 UIO_SYSSPACE, IO_SYNC, NFSFPCRED(sf->nsf_fp), NULL, p);
4656 free((caddr_t)sf->nsf_bootvals, M_TEMP);
4657 sf->nsf_bootvals = NULL;
4659 sf->nsf_flags &= ~NFSNSF_OK;
4660 printf("EEK! Can't write NfsV4 stable storage file\n");
4663 sf->nsf_flags |= NFSNSF_OK;
4666 * Loop through the list and write out timestamp records for
4667 * any clients that successfully reclaimed state.
4669 LIST_FOREACH_SAFE(sp, &sf->nsf_head, nst_list, nsp) {
4670 if (sp->nst_flag & NFSNST_GOTSTATE) {
4671 nfsrv_writestable(sp->nst_client, sp->nst_len,
4672 NFSNST_NEWSTATE, p);
4673 sp->nst_clp->lc_flags |= LCL_STAMPEDSTABLE;
4675 LIST_REMOVE(sp, nst_list);
4676 free((caddr_t)sp, M_TEMP);
4678 nfsrv_backupstable();
4682 * Append a record to the stable storage file.
4685 nfsrv_writestable(u_char *client, int len, int flag, NFSPROC_T *p)
4687 struct nfsrv_stablefirst *sf = &nfsrv_stablefirst;
4688 struct nfst_rec *sp;
4691 if (!(sf->nsf_flags & NFSNSF_OK) || sf->nsf_fp == NULL)
4693 sp = (struct nfst_rec *)malloc(sizeof (struct nfst_rec) +
4694 len - 1, M_TEMP, M_WAITOK);
4696 NFSBCOPY(client, sp->client, len);
4698 error = NFSD_RDWR(UIO_WRITE, NFSFPVNODE(sf->nsf_fp),
4699 (caddr_t)sp, sizeof (struct nfst_rec) + len - 1, (off_t)0,
4700 UIO_SYSSPACE, (IO_SYNC | IO_APPEND), NFSFPCRED(sf->nsf_fp), NULL, p);
4701 free((caddr_t)sp, M_TEMP);
4703 sf->nsf_flags &= ~NFSNSF_OK;
4704 printf("EEK! Can't write NfsV4 stable storage file\n");
4709 * This function is called during the grace period to mark a client
4710 * that successfully reclaimed state.
4713 nfsrv_markstable(struct nfsclient *clp)
4715 struct nfsrv_stable *sp;
4718 * First find the client structure.
4720 LIST_FOREACH(sp, &nfsrv_stablefirst.nsf_head, nst_list) {
4721 if (sp->nst_len == clp->lc_idlen &&
4722 !NFSBCMP(sp->nst_client, clp->lc_id, sp->nst_len))
4725 if (sp == LIST_END(&nfsrv_stablefirst.nsf_head))
4729 * Now, just mark it and set the nfsclient back pointer.
4731 sp->nst_flag |= NFSNST_GOTSTATE;
4736 * This function is called for a reclaim, to see if it gets grace.
4737 * It returns 0 if a reclaim is allowed, 1 otherwise.
4740 nfsrv_checkstable(struct nfsclient *clp)
4742 struct nfsrv_stable *sp;
4745 * First, find the entry for the client.
4747 LIST_FOREACH(sp, &nfsrv_stablefirst.nsf_head, nst_list) {
4748 if (sp->nst_len == clp->lc_idlen &&
4749 !NFSBCMP(sp->nst_client, clp->lc_id, sp->nst_len))
4754 * If not in the list, state was revoked or no state was issued
4755 * since the previous reboot, a reclaim is denied.
4757 if (sp == LIST_END(&nfsrv_stablefirst.nsf_head) ||
4758 (sp->nst_flag & NFSNST_REVOKE) ||
4759 !(nfsrv_stablefirst.nsf_flags & NFSNSF_OK))
4765 * Test for and try to clear out a conflicting client. This is called by
4766 * nfsrv_lockctrl() and nfsrv_openctrl() when conflicts with other clients
4768 * The trick here is that it can't revoke a conflicting client with an
4769 * expired lease unless it holds the v4root lock, so...
4770 * If no v4root lock, get the lock and return 1 to indicate "try again".
4771 * Return 0 to indicate the conflict can't be revoked and 1 to indicate
4772 * the revocation worked and the conflicting client is "bye, bye", so it
4773 * can be tried again.
4774 * Return 2 to indicate that the vnode is VI_DOOMED after NFSVOPLOCK().
4775 * Unlocks State before a non-zero value is returned.
4778 nfsrv_clientconflict(struct nfsclient *clp, int *haslockp, vnode_t vp,
4781 int gotlock, lktype = 0;
4784 * If lease hasn't expired, we can't fix it.
4786 if (clp->lc_expiry >= NFSD_MONOSEC ||
4787 !(nfsrv_stablefirst.nsf_flags & NFSNSF_UPDATEDONE))
4789 if (*haslockp == 0) {
4792 lktype = NFSVOPISLOCKED(vp);
4793 NFSVOPUNLOCK(vp, 0);
4795 NFSLOCKV4ROOTMUTEX();
4796 nfsv4_relref(&nfsv4rootfs_lock);
4798 gotlock = nfsv4_lock(&nfsv4rootfs_lock, 1, NULL,
4799 NFSV4ROOTLOCKMUTEXPTR, NULL);
4801 NFSUNLOCKV4ROOTMUTEX();
4804 NFSVOPLOCK(vp, lktype | LK_RETRY);
4805 if ((vp->v_iflag & VI_DOOMED) != 0)
4813 * Ok, we can expire the conflicting client.
4815 nfsrv_writestable(clp->lc_id, clp->lc_idlen, NFSNST_REVOKE, p);
4816 nfsrv_backupstable();
4817 nfsrv_cleanclient(clp, p);
4818 nfsrv_freedeleglist(&clp->lc_deleg);
4819 nfsrv_freedeleglist(&clp->lc_olddeleg);
4820 LIST_REMOVE(clp, lc_hash);
4821 nfsrv_zapclient(clp, p);
4826 * Resolve a delegation conflict.
4827 * Returns 0 to indicate the conflict was resolved without sleeping.
4828 * Return -1 to indicate that the caller should check for conflicts again.
4829 * Return > 0 for an error that should be returned, normally NFSERR_DELAY.
4831 * Also, manipulate the nfsv4root_lock, as required. It isn't changed
4832 * for a return of 0, since there was no sleep and it could be required
4833 * later. It is released for a return of NFSERR_DELAY, since the caller
4834 * will return that error. It is released when a sleep was done waiting
4835 * for the delegation to be returned or expire (so that other nfsds can
4836 * handle ops). Then, it must be acquired for the write to stable storage.
4837 * (This function is somewhat similar to nfsrv_clientconflict(), but
4838 * the semantics differ in a couple of subtle ways. The return of 0
4839 * indicates the conflict was resolved without sleeping here, not
4840 * that the conflict can't be resolved and the handling of nfsv4root_lock
4841 * differs, as noted above.)
4842 * Unlocks State before returning a non-zero value.
4845 nfsrv_delegconflict(struct nfsstate *stp, int *haslockp, NFSPROC_T *p,
4848 struct nfsclient *clp = stp->ls_clp;
4849 int gotlock, error, lktype = 0, retrycnt, zapped_clp;
4850 nfsv4stateid_t tstateid;
4854 * If the conflict is with an old delegation...
4856 if (stp->ls_flags & NFSLCK_OLDDELEG) {
4858 * You can delete it, if it has expired.
4860 if (clp->lc_delegtime < NFSD_MONOSEC) {
4861 nfsrv_freedeleg(stp);
4868 * During this delay, the old delegation could expire or it
4869 * could be recovered by the client via an Open with
4870 * CLAIM_DELEGATE_PREV.
4871 * Release the nfsv4root_lock, if held.
4875 NFSLOCKV4ROOTMUTEX();
4876 nfsv4_unlock(&nfsv4rootfs_lock, 1);
4877 NFSUNLOCKV4ROOTMUTEX();
4879 error = NFSERR_DELAY;
4884 * It's a current delegation, so:
4885 * - check to see if the delegation has expired
4886 * - if so, get the v4root lock and then expire it
4888 if (!(stp->ls_flags & NFSLCK_DELEGRECALL)) {
4890 * - do a recall callback, since not yet done
4891 * For now, never allow truncate to be set. To use
4892 * truncate safely, it must be guaranteed that the
4893 * Remove, Rename or Setattr with size of 0 will
4894 * succeed and that would require major changes to
4895 * the VFS/Vnode OPs.
4896 * Set the expiry time large enough so that it won't expire
4897 * until after the callback, then set it correctly, once
4898 * the callback is done. (The delegation will now time
4899 * out whether or not the Recall worked ok. The timeout
4900 * will be extended when ops are done on the delegation
4901 * stateid, up to the timelimit.)
4903 stp->ls_delegtime = NFSD_MONOSEC + (2 * nfsrv_lease) +
4905 stp->ls_delegtimelimit = NFSD_MONOSEC + (6 * nfsrv_lease) +
4907 stp->ls_flags |= NFSLCK_DELEGRECALL;
4910 * Loop NFSRV_CBRETRYCNT times while the CBRecall replies
4911 * NFSERR_BADSTATEID or NFSERR_BADHANDLE. This is done
4912 * in order to try and avoid a race that could happen
4913 * when a CBRecall request passed the Open reply with
4914 * the delegation in it when transitting the network.
4915 * Since nfsrv_docallback will sleep, don't use stp after
4918 NFSBCOPY((caddr_t)&stp->ls_stateid, (caddr_t)&tstateid,
4920 NFSBCOPY((caddr_t)&stp->ls_lfp->lf_fh, (caddr_t)&tfh,
4925 NFSLOCKV4ROOTMUTEX();
4926 nfsv4_unlock(&nfsv4rootfs_lock, 1);
4927 NFSUNLOCKV4ROOTMUTEX();
4931 error = nfsrv_docallback(clp, NFSV4OP_CBRECALL,
4932 &tstateid, 0, &tfh, NULL, NULL, p);
4934 } while ((error == NFSERR_BADSTATEID ||
4935 error == NFSERR_BADHANDLE) && retrycnt < NFSV4_CBRETRYCNT);
4936 error = NFSERR_DELAY;
4940 if (clp->lc_expiry >= NFSD_MONOSEC &&
4941 stp->ls_delegtime >= NFSD_MONOSEC) {
4944 * A recall has been done, but it has not yet expired.
4949 NFSLOCKV4ROOTMUTEX();
4950 nfsv4_unlock(&nfsv4rootfs_lock, 1);
4951 NFSUNLOCKV4ROOTMUTEX();
4953 error = NFSERR_DELAY;
4958 * If we don't yet have the lock, just get it and then return,
4959 * since we need that before deleting expired state, such as
4961 * When getting the lock, unlock the vnode, so other nfsds that
4962 * are in progress, won't get stuck waiting for the vnode lock.
4964 if (*haslockp == 0) {
4967 lktype = NFSVOPISLOCKED(vp);
4968 NFSVOPUNLOCK(vp, 0);
4970 NFSLOCKV4ROOTMUTEX();
4971 nfsv4_relref(&nfsv4rootfs_lock);
4973 gotlock = nfsv4_lock(&nfsv4rootfs_lock, 1, NULL,
4974 NFSV4ROOTLOCKMUTEXPTR, NULL);
4976 NFSUNLOCKV4ROOTMUTEX();
4979 NFSVOPLOCK(vp, lktype | LK_RETRY);
4980 if ((vp->v_iflag & VI_DOOMED) != 0) {
4982 NFSLOCKV4ROOTMUTEX();
4983 nfsv4_unlock(&nfsv4rootfs_lock, 1);
4984 NFSUNLOCKV4ROOTMUTEX();
4985 error = NFSERR_PERM;
4995 * Ok, we can delete the expired delegation.
4996 * First, write the Revoke record to stable storage and then
4997 * clear out the conflict.
4998 * Since all other nfsd threads are now blocked, we can safely
4999 * sleep without the state changing.
5001 nfsrv_writestable(clp->lc_id, clp->lc_idlen, NFSNST_REVOKE, p);
5002 nfsrv_backupstable();
5003 if (clp->lc_expiry < NFSD_MONOSEC) {
5004 nfsrv_cleanclient(clp, p);
5005 nfsrv_freedeleglist(&clp->lc_deleg);
5006 nfsrv_freedeleglist(&clp->lc_olddeleg);
5007 LIST_REMOVE(clp, lc_hash);
5010 nfsrv_freedeleg(stp);
5014 nfsrv_zapclient(clp, p);
5023 * Check for a remove allowed, if remove is set to 1 and get rid of
5027 nfsrv_checkremove(vnode_t vp, int remove, NFSPROC_T *p)
5029 struct nfsstate *stp;
5030 struct nfslockfile *lfp;
5031 int error, haslock = 0;
5035 * First, get the lock file structure.
5036 * (A return of -1 means no associated state, so remove ok.)
5038 error = nfsrv_getlockfh(vp, NFSLCK_CHECK, NULL, &nfh, p);
5042 error = nfsrv_getlockfile(NFSLCK_CHECK, NULL, &lfp, &nfh, 0);
5046 NFSLOCKV4ROOTMUTEX();
5047 nfsv4_unlock(&nfsv4rootfs_lock, 1);
5048 NFSUNLOCKV4ROOTMUTEX();
5056 * Now, we must Recall any delegations.
5058 error = nfsrv_cleandeleg(vp, lfp, NULL, &haslock, p);
5061 * nfsrv_cleandeleg() unlocks state for non-zero
5067 NFSLOCKV4ROOTMUTEX();
5068 nfsv4_unlock(&nfsv4rootfs_lock, 1);
5069 NFSUNLOCKV4ROOTMUTEX();
5075 * Now, look for a conflicting open share.
5078 LIST_FOREACH(stp, &lfp->lf_open, ls_file) {
5079 if (stp->ls_flags & NFSLCK_WRITEDENY) {
5080 error = NFSERR_FILEOPEN;
5088 NFSLOCKV4ROOTMUTEX();
5089 nfsv4_unlock(&nfsv4rootfs_lock, 1);
5090 NFSUNLOCKV4ROOTMUTEX();
5099 * Clear out all delegations for the file referred to by lfp.
5100 * May return NFSERR_DELAY, if there will be a delay waiting for
5101 * delegations to expire.
5102 * Returns -1 to indicate it slept while recalling a delegation.
5103 * This function has the side effect of deleting the nfslockfile structure,
5104 * if it no longer has associated state and didn't have to sleep.
5105 * Unlocks State before a non-zero value is returned.
5108 nfsrv_cleandeleg(vnode_t vp, struct nfslockfile *lfp,
5109 struct nfsclient *clp, int *haslockp, NFSPROC_T *p)
5111 struct nfsstate *stp, *nstp;
5114 stp = LIST_FIRST(&lfp->lf_deleg);
5115 while (stp != LIST_END(&lfp->lf_deleg)) {
5116 nstp = LIST_NEXT(stp, ls_file);
5117 if (stp->ls_clp != clp) {
5118 ret = nfsrv_delegconflict(stp, haslockp, p, vp);
5121 * nfsrv_delegconflict() unlocks state
5122 * when it returns non-zero.
5135 * There are certain operations that, when being done outside of NFSv4,
5136 * require that any NFSv4 delegation for the file be recalled.
5137 * This function is to be called for those cases:
5138 * VOP_RENAME() - When a delegation is being recalled for any reason,
5139 * the client may have to do Opens against the server, using the file's
5140 * final component name. If the file has been renamed on the server,
5141 * that component name will be incorrect and the Open will fail.
5142 * VOP_REMOVE() - Theoretically, a client could Open a file after it has
5143 * been removed on the server, if there is a delegation issued to
5144 * that client for the file. I say "theoretically" since clients
5145 * normally do an Access Op before the Open and that Access Op will
5146 * fail with ESTALE. Note that NFSv2 and 3 don't even do Opens, so
5147 * they will detect the file's removal in the same manner. (There is
5148 * one case where RFC3530 allows a client to do an Open without first
5149 * doing an Access Op, which is passage of a check against the ACE
5150 * returned with a Write delegation, but current practice is to ignore
5151 * the ACE and always do an Access Op.)
5152 * Since the functions can only be called with an unlocked vnode, this
5153 * can't be done at this time.
5154 * VOP_ADVLOCK() - When a client holds a delegation, it can issue byte range
5155 * locks locally in the client, which are not visible to the server. To
5156 * deal with this, issuing of delegations for a vnode must be disabled
5157 * and all delegations for the vnode recalled. This is done via the
5158 * second function, using the VV_DISABLEDELEG vflag on the vnode.
5161 nfsd_recalldelegation(vnode_t vp, NFSPROC_T *p)
5167 * First, check to see if the server is currently running and it has
5168 * been called for a regular file when issuing delegations.
5170 if (newnfs_numnfsd == 0 || vp->v_type != VREG ||
5171 nfsrv_issuedelegs == 0)
5174 KASSERT((NFSVOPISLOCKED(vp) != LK_EXCLUSIVE), ("vp %p is locked", vp));
5176 * First, get a reference on the nfsv4rootfs_lock so that an
5177 * exclusive lock cannot be acquired by another thread.
5179 NFSLOCKV4ROOTMUTEX();
5180 nfsv4_getref(&nfsv4rootfs_lock, NULL, NFSV4ROOTLOCKMUTEXPTR, NULL);
5181 NFSUNLOCKV4ROOTMUTEX();
5184 * Now, call nfsrv_checkremove() in a loop while it returns
5185 * NFSERR_DELAY. Return upon any other error or when timed out.
5187 starttime = NFSD_MONOSEC;
5189 if (NFSVOPLOCK(vp, LK_EXCLUSIVE) == 0) {
5190 error = nfsrv_checkremove(vp, 0, p);
5191 NFSVOPUNLOCK(vp, 0);
5194 if (error == NFSERR_DELAY) {
5195 if (NFSD_MONOSEC - starttime > NFS_REMOVETIMEO)
5197 /* Sleep for a short period of time */
5198 (void) nfs_catnap(PZERO, 0, "nfsremove");
5200 } while (error == NFSERR_DELAY);
5201 NFSLOCKV4ROOTMUTEX();
5202 nfsv4_relref(&nfsv4rootfs_lock);
5203 NFSUNLOCKV4ROOTMUTEX();
5207 nfsd_disabledelegation(vnode_t vp, NFSPROC_T *p)
5210 #ifdef VV_DISABLEDELEG
5212 * First, flag issuance of delegations disabled.
5214 atomic_set_long(&vp->v_vflag, VV_DISABLEDELEG);
5218 * Then call nfsd_recalldelegation() to get rid of all extant
5221 nfsd_recalldelegation(vp, p);
5225 * Check for conflicting locks, etc. and then get rid of delegations.
5226 * (At one point I thought that I should get rid of delegations for any
5227 * Setattr, since it could potentially disallow the I/O op (read or write)
5228 * allowed by the delegation. However, Setattr Ops that aren't changing
5229 * the size get a stateid of all 0s, so you can't tell if it is a delegation
5230 * for the same client or a different one, so I decided to only get rid
5231 * of delegations for other clients when the size is being changed.)
5232 * In general, a Setattr can disable NFS I/O Ops that are outstanding, such
5233 * as Write backs, even if there is no delegation, so it really isn't any
5237 nfsrv_checksetattr(vnode_t vp, struct nfsrv_descript *nd,
5238 nfsv4stateid_t *stateidp, struct nfsvattr *nvap, nfsattrbit_t *attrbitp,
5239 struct nfsexstuff *exp, NFSPROC_T *p)
5241 struct nfsstate st, *stp = &st;
5242 struct nfslock lo, *lop = &lo;
5246 if (NFSISSET_ATTRBIT(attrbitp, NFSATTRBIT_SIZE)) {
5247 stp->ls_flags = (NFSLCK_CHECK | NFSLCK_WRITEACCESS);
5248 lop->lo_first = nvap->na_size;
5253 if (NFSISSET_ATTRBIT(attrbitp, NFSATTRBIT_OWNER) ||
5254 NFSISSET_ATTRBIT(attrbitp, NFSATTRBIT_OWNERGROUP) ||
5255 NFSISSET_ATTRBIT(attrbitp, NFSATTRBIT_MODE) ||
5256 NFSISSET_ATTRBIT(attrbitp, NFSATTRBIT_ACL))
5257 stp->ls_flags |= NFSLCK_SETATTR;
5258 if (stp->ls_flags == 0)
5260 lop->lo_end = NFS64BITSSET;
5261 lop->lo_flags = NFSLCK_WRITE;
5262 stp->ls_ownerlen = 0;
5264 stp->ls_uid = nd->nd_cred->cr_uid;
5265 stp->ls_stateid.seqid = stateidp->seqid;
5266 clientid.lval[0] = stp->ls_stateid.other[0] = stateidp->other[0];
5267 clientid.lval[1] = stp->ls_stateid.other[1] = stateidp->other[1];
5268 stp->ls_stateid.other[2] = stateidp->other[2];
5269 error = nfsrv_lockctrl(vp, &stp, &lop, NULL, clientid,
5270 stateidp, exp, nd, p);
5273 NFSEXITCODE2(error, nd);
5278 * Check for a write delegation and do a CBGETATTR if there is one, updating
5279 * the attributes, as required.
5280 * Should I return an error if I can't get the attributes? (For now, I'll
5284 nfsrv_checkgetattr(struct nfsrv_descript *nd, vnode_t vp,
5285 struct nfsvattr *nvap, nfsattrbit_t *attrbitp, struct ucred *cred,
5288 struct nfsstate *stp;
5289 struct nfslockfile *lfp;
5290 struct nfsclient *clp;
5291 struct nfsvattr nva;
5294 nfsattrbit_t cbbits;
5295 u_quad_t delegfilerev;
5297 NFSCBGETATTR_ATTRBIT(attrbitp, &cbbits);
5298 if (!NFSNONZERO_ATTRBIT(&cbbits))
5300 if (nfsrv_writedelegcnt == 0)
5304 * Get the lock file structure.
5305 * (A return of -1 means no associated state, so return ok.)
5307 error = nfsrv_getlockfh(vp, NFSLCK_CHECK, NULL, &nfh, p);
5310 error = nfsrv_getlockfile(NFSLCK_CHECK, NULL, &lfp, &nfh, 0);
5319 * Now, look for a write delegation.
5321 LIST_FOREACH(stp, &lfp->lf_deleg, ls_file) {
5322 if (stp->ls_flags & NFSLCK_DELEGWRITE)
5325 if (stp == LIST_END(&lfp->lf_deleg)) {
5330 delegfilerev = stp->ls_filerev;
5333 * If the Write delegation was issued as a part of this Compound RPC
5334 * or if we have an Implied Clientid (used in a previous Op in this
5335 * compound) and it is the client the delegation was issued to,
5337 * I also assume that it is from the same client iff the network
5338 * host IP address is the same as the callback address. (Not
5339 * exactly correct by the RFC, but avoids a lot of Getattr
5342 if (nd->nd_compref == stp->ls_compref ||
5343 ((nd->nd_flag & ND_IMPLIEDCLID) &&
5344 clp->lc_clientid.qval == nd->nd_clientid.qval) ||
5345 nfsaddr2_match(clp->lc_req.nr_nam, nd->nd_nam)) {
5351 * We are now done with the delegation state structure,
5352 * so the statelock can be released and we can now tsleep().
5356 * Now, we must do the CB Getattr callback, to see if Change or Size
5359 if (clp->lc_expiry >= NFSD_MONOSEC) {
5361 NFSVNO_ATTRINIT(&nva);
5362 nva.na_filerev = NFS64BITSSET;
5363 error = nfsrv_docallback(clp, NFSV4OP_CBGETATTR, NULL,
5364 0, &nfh, &nva, &cbbits, p);
5366 if ((nva.na_filerev != NFS64BITSSET &&
5367 nva.na_filerev > delegfilerev) ||
5368 (NFSVNO_ISSETSIZE(&nva) &&
5369 nva.na_size != nvap->na_size)) {
5370 error = nfsvno_updfilerev(vp, nvap, cred, p);
5371 if (NFSVNO_ISSETSIZE(&nva))
5372 nvap->na_size = nva.na_size;
5375 error = 0; /* Ignore callback errors for now. */
5381 NFSEXITCODE2(error, nd);
5386 * This function looks for openowners that haven't had any opens for
5387 * a while and throws them away. Called by an nfsd when NFSNSF_NOOPENS
5391 nfsrv_throwawayopens(NFSPROC_T *p)
5393 struct nfsclient *clp, *nclp;
5394 struct nfsstate *stp, *nstp;
5398 nfsrv_stablefirst.nsf_flags &= ~NFSNSF_NOOPENS;
5400 * For each client...
5402 for (i = 0; i < nfsrv_clienthashsize; i++) {
5403 LIST_FOREACH_SAFE(clp, &nfsclienthash[i], lc_hash, nclp) {
5404 LIST_FOREACH_SAFE(stp, &clp->lc_open, ls_list, nstp) {
5405 if (LIST_EMPTY(&stp->ls_open) &&
5406 (stp->ls_noopens > NFSNOOPEN ||
5407 (nfsrv_openpluslock * 2) >
5408 nfsrv_v4statelimit))
5409 nfsrv_freeopenowner(stp, 0, p);
5417 * This function checks to see if the credentials are the same.
5418 * Returns 1 for not same, 0 otherwise.
5421 nfsrv_notsamecredname(struct nfsrv_descript *nd, struct nfsclient *clp)
5424 if (nd->nd_flag & ND_GSS) {
5425 if (!(clp->lc_flags & LCL_GSS))
5427 if (clp->lc_flags & LCL_NAME) {
5428 if (nd->nd_princlen != clp->lc_namelen ||
5429 NFSBCMP(nd->nd_principal, clp->lc_name,
5435 if (nd->nd_cred->cr_uid == clp->lc_uid)
5439 } else if (clp->lc_flags & LCL_GSS)
5442 * For AUTH_SYS, allow the same uid or root. (This is underspecified
5443 * in RFC3530, which talks about principals, but doesn't say anything
5444 * about uids for AUTH_SYS.)
5446 if (nd->nd_cred->cr_uid == clp->lc_uid || nd->nd_cred->cr_uid == 0)
5453 * Calculate the lease expiry time.
5456 nfsrv_leaseexpiry(void)
5459 if (nfsrv_stablefirst.nsf_eograce > NFSD_MONOSEC)
5460 return (NFSD_MONOSEC + 2 * (nfsrv_lease + NFSRV_LEASEDELTA));
5461 return (NFSD_MONOSEC + nfsrv_lease + NFSRV_LEASEDELTA);
5465 * Delay the delegation timeout as far as ls_delegtimelimit, as required.
5468 nfsrv_delaydelegtimeout(struct nfsstate *stp)
5471 if ((stp->ls_flags & NFSLCK_DELEGRECALL) == 0)
5474 if ((stp->ls_delegtime + 15) > NFSD_MONOSEC &&
5475 stp->ls_delegtime < stp->ls_delegtimelimit) {
5476 stp->ls_delegtime += nfsrv_lease;
5477 if (stp->ls_delegtime > stp->ls_delegtimelimit)
5478 stp->ls_delegtime = stp->ls_delegtimelimit;
5483 * This function checks to see if there is any other state associated
5484 * with the openowner for this Open.
5485 * It returns 1 if there is no other state, 0 otherwise.
5488 nfsrv_nootherstate(struct nfsstate *stp)
5490 struct nfsstate *tstp;
5492 LIST_FOREACH(tstp, &stp->ls_openowner->ls_open, ls_list) {
5493 if (tstp != stp || !LIST_EMPTY(&tstp->ls_lock))
5500 * Create a list of lock deltas (changes to local byte range locking
5501 * that can be rolled back using the list) and apply the changes via
5502 * nfsvno_advlock(). Optionally, lock the list. It is expected that either
5503 * the rollback or update function will be called after this.
5504 * It returns an error (and rolls back, as required), if any nfsvno_advlock()
5505 * call fails. If it returns an error, it will unlock the list.
5508 nfsrv_locallock(vnode_t vp, struct nfslockfile *lfp, int flags,
5509 uint64_t first, uint64_t end, struct nfslockconflict *cfp, NFSPROC_T *p)
5511 struct nfslock *lop, *nlop;
5514 /* Loop through the list of locks. */
5515 lop = LIST_FIRST(&lfp->lf_locallock);
5516 while (first < end && lop != NULL) {
5517 nlop = LIST_NEXT(lop, lo_lckowner);
5518 if (first >= lop->lo_end) {
5521 } else if (first < lop->lo_first) {
5522 /* new one starts before entry in list */
5523 if (end <= lop->lo_first) {
5524 /* no overlap between old and new */
5525 error = nfsrv_dolocal(vp, lfp, flags,
5526 NFSLCK_UNLOCK, first, end, cfp, p);
5531 /* handle fragment overlapped with new one */
5532 error = nfsrv_dolocal(vp, lfp, flags,
5533 NFSLCK_UNLOCK, first, lop->lo_first, cfp,
5537 first = lop->lo_first;
5540 /* new one overlaps this entry in list */
5541 if (end <= lop->lo_end) {
5542 /* overlaps all of new one */
5543 error = nfsrv_dolocal(vp, lfp, flags,
5544 lop->lo_flags, first, end, cfp, p);
5549 /* handle fragment overlapped with new one */
5550 error = nfsrv_dolocal(vp, lfp, flags,
5551 lop->lo_flags, first, lop->lo_end, cfp, p);
5554 first = lop->lo_end;
5559 if (first < end && error == 0)
5560 /* handle fragment past end of list */
5561 error = nfsrv_dolocal(vp, lfp, flags, NFSLCK_UNLOCK, first,
5569 * Local lock unlock. Unlock all byte ranges that are no longer locked
5570 * by NFSv4. To do this, unlock any subranges of first-->end that
5571 * do not overlap with the byte ranges of any lock in the lfp->lf_lock
5572 * list. This list has all locks for the file held by other
5573 * <clientid, lockowner> tuples. The list is ordered by increasing
5574 * lo_first value, but may have entries that overlap each other, for
5575 * the case of read locks.
5578 nfsrv_localunlock(vnode_t vp, struct nfslockfile *lfp, uint64_t init_first,
5579 uint64_t init_end, NFSPROC_T *p)
5581 struct nfslock *lop;
5582 uint64_t first, end, prevfirst;
5586 while (first < init_end) {
5587 /* Loop through all nfs locks, adjusting first and end */
5589 LIST_FOREACH(lop, &lfp->lf_lock, lo_lckfile) {
5590 KASSERT(prevfirst <= lop->lo_first,
5591 ("nfsv4 locks out of order"));
5592 KASSERT(lop->lo_first < lop->lo_end,
5593 ("nfsv4 bogus lock"));
5594 prevfirst = lop->lo_first;
5595 if (first >= lop->lo_first &&
5596 first < lop->lo_end)
5598 * Overlaps with initial part, so trim
5599 * off that initial part by moving first past
5602 first = lop->lo_end;
5603 else if (end > lop->lo_first &&
5604 lop->lo_first > first) {
5606 * This lock defines the end of the
5607 * segment to unlock, so set end to the
5608 * start of it and break out of the loop.
5610 end = lop->lo_first;
5615 * There is no segment left to do, so
5616 * break out of this loop and then exit
5617 * the outer while() since first will be set
5618 * to end, which must equal init_end here.
5623 /* Unlock this segment */
5624 (void) nfsrv_dolocal(vp, lfp, NFSLCK_UNLOCK,
5625 NFSLCK_READ, first, end, NULL, p);
5626 nfsrv_locallock_commit(lfp, NFSLCK_UNLOCK,
5630 * Now move past this segment and look for any further
5631 * segment in the range, if there is one.
5639 * Do the local lock operation and update the rollback list, as required.
5640 * Perform the rollback and return the error if nfsvno_advlock() fails.
5643 nfsrv_dolocal(vnode_t vp, struct nfslockfile *lfp, int flags, int oldflags,
5644 uint64_t first, uint64_t end, struct nfslockconflict *cfp, NFSPROC_T *p)
5646 struct nfsrollback *rlp;
5647 int error = 0, ltype, oldltype;
5649 if (flags & NFSLCK_WRITE)
5651 else if (flags & NFSLCK_READ)
5655 if (oldflags & NFSLCK_WRITE)
5657 else if (oldflags & NFSLCK_READ)
5661 if (ltype == oldltype || (oldltype == F_WRLCK && ltype == F_RDLCK))
5664 error = nfsvno_advlock(vp, ltype, first, end, p);
5667 cfp->cl_clientid.lval[0] = 0;
5668 cfp->cl_clientid.lval[1] = 0;
5670 cfp->cl_end = NFS64BITSSET;
5671 cfp->cl_flags = NFSLCK_WRITE;
5672 cfp->cl_ownerlen = 5;
5673 NFSBCOPY("LOCAL", cfp->cl_owner, 5);
5675 nfsrv_locallock_rollback(vp, lfp, p);
5676 } else if (ltype != F_UNLCK) {
5677 rlp = malloc(sizeof (struct nfsrollback), M_NFSDROLLBACK,
5679 rlp->rlck_first = first;
5680 rlp->rlck_end = end;
5681 rlp->rlck_type = oldltype;
5682 LIST_INSERT_HEAD(&lfp->lf_rollback, rlp, rlck_list);
5691 * Roll back local lock changes and free up the rollback list.
5694 nfsrv_locallock_rollback(vnode_t vp, struct nfslockfile *lfp, NFSPROC_T *p)
5696 struct nfsrollback *rlp, *nrlp;
5698 LIST_FOREACH_SAFE(rlp, &lfp->lf_rollback, rlck_list, nrlp) {
5699 (void) nfsvno_advlock(vp, rlp->rlck_type, rlp->rlck_first,
5701 free(rlp, M_NFSDROLLBACK);
5703 LIST_INIT(&lfp->lf_rollback);
5707 * Update local lock list and delete rollback list (ie now committed to the
5708 * local locks). Most of the work is done by the internal function.
5711 nfsrv_locallock_commit(struct nfslockfile *lfp, int flags, uint64_t first,
5714 struct nfsrollback *rlp, *nrlp;
5715 struct nfslock *new_lop, *other_lop;
5717 new_lop = malloc(sizeof (struct nfslock), M_NFSDLOCK, M_WAITOK);
5718 if (flags & (NFSLCK_READ | NFSLCK_WRITE))
5719 other_lop = malloc(sizeof (struct nfslock), M_NFSDLOCK,
5723 new_lop->lo_flags = flags;
5724 new_lop->lo_first = first;
5725 new_lop->lo_end = end;
5726 nfsrv_updatelock(NULL, &new_lop, &other_lop, lfp);
5727 if (new_lop != NULL)
5728 free(new_lop, M_NFSDLOCK);
5729 if (other_lop != NULL)
5730 free(other_lop, M_NFSDLOCK);
5732 /* and get rid of the rollback list */
5733 LIST_FOREACH_SAFE(rlp, &lfp->lf_rollback, rlck_list, nrlp)
5734 free(rlp, M_NFSDROLLBACK);
5735 LIST_INIT(&lfp->lf_rollback);
5739 * Lock the struct nfslockfile for local lock updating.
5742 nfsrv_locklf(struct nfslockfile *lfp)
5746 /* lf_usecount ensures *lfp won't be free'd */
5749 gotlock = nfsv4_lock(&lfp->lf_locallock_lck, 1, NULL,
5750 NFSSTATEMUTEXPTR, NULL);
5751 } while (gotlock == 0);
5756 * Unlock the struct nfslockfile after local lock updating.
5759 nfsrv_unlocklf(struct nfslockfile *lfp)
5762 nfsv4_unlock(&lfp->lf_locallock_lck, 0);
5766 * Clear out all state for the NFSv4 server.
5767 * Must be called by a thread that can sleep when no nfsds are running.
5770 nfsrv_throwawayallstate(NFSPROC_T *p)
5772 struct nfsclient *clp, *nclp;
5773 struct nfslockfile *lfp, *nlfp;
5777 * For each client, clean out the state and then free the structure.
5779 for (i = 0; i < nfsrv_clienthashsize; i++) {
5780 LIST_FOREACH_SAFE(clp, &nfsclienthash[i], lc_hash, nclp) {
5781 nfsrv_cleanclient(clp, p);
5782 nfsrv_freedeleglist(&clp->lc_deleg);
5783 nfsrv_freedeleglist(&clp->lc_olddeleg);
5784 free(clp->lc_stateid, M_NFSDCLIENT);
5785 free(clp, M_NFSDCLIENT);
5790 * Also, free up any remaining lock file structures.
5792 for (i = 0; i < nfsrv_lockhashsize; i++) {
5793 LIST_FOREACH_SAFE(lfp, &nfslockhash[i], lf_hash, nlfp) {
5794 printf("nfsd unload: fnd a lock file struct\n");
5795 nfsrv_freenfslockfile(lfp);
5801 * Check the sequence# for the session and slot provided as an argument.
5802 * Also, renew the lease if the session will return NFS_OK.
5805 nfsrv_checksequence(struct nfsrv_descript *nd, uint32_t sequenceid,
5806 uint32_t *highest_slotidp, uint32_t *target_highest_slotidp, int cache_this,
5807 uint32_t *sflagsp, NFSPROC_T *p)
5809 struct nfsdsession *sep;
5810 struct nfssessionhash *shp;
5814 shp = NFSSESSIONHASH(nd->nd_sessionid);
5815 NFSLOCKSESSION(shp);
5816 sep = nfsrv_findsession(nd->nd_sessionid);
5818 NFSUNLOCKSESSION(shp);
5819 return (NFSERR_BADSESSION);
5821 error = nfsv4_seqsession(sequenceid, nd->nd_slotid, *highest_slotidp,
5822 sep->sess_slots, NULL, NFSV4_SLOTS - 1);
5824 NFSUNLOCKSESSION(shp);
5827 if (cache_this != 0)
5828 nd->nd_flag |= ND_SAVEREPLY;
5829 /* Renew the lease. */
5830 sep->sess_clp->lc_expiry = nfsrv_leaseexpiry();
5831 nd->nd_clientid.qval = sep->sess_clp->lc_clientid.qval;
5832 nd->nd_flag |= ND_IMPLIEDCLID;
5835 * If this session handles the backchannel, save the nd_xprt for this
5836 * RPC, since this is the one being used.
5838 if (sep->sess_clp->lc_req.nr_client != NULL &&
5839 (sep->sess_crflags & NFSV4CRSESS_CONNBACKCHAN) != 0) {
5840 savxprt = sep->sess_cbsess.nfsess_xprt;
5841 SVC_ACQUIRE(nd->nd_xprt);
5842 nd->nd_xprt->xp_p2 =
5843 sep->sess_clp->lc_req.nr_client->cl_private;
5844 nd->nd_xprt->xp_idletimeout = 0; /* Disable timeout. */
5845 sep->sess_cbsess.nfsess_xprt = nd->nd_xprt;
5846 if (savxprt != NULL)
5847 SVC_RELEASE(savxprt);
5851 if (sep->sess_clp->lc_req.nr_client == NULL)
5852 *sflagsp |= NFSV4SEQ_CBPATHDOWN;
5853 NFSUNLOCKSESSION(shp);
5854 if (error == NFSERR_EXPIRED) {
5855 *sflagsp |= NFSV4SEQ_EXPIREDALLSTATEREVOKED;
5857 } else if (error == NFSERR_ADMINREVOKED) {
5858 *sflagsp |= NFSV4SEQ_ADMINSTATEREVOKED;
5861 *highest_slotidp = *target_highest_slotidp = NFSV4_SLOTS - 1;
5866 * Check/set reclaim complete for this session/clientid.
5869 nfsrv_checkreclaimcomplete(struct nfsrv_descript *nd)
5871 struct nfsdsession *sep;
5872 struct nfssessionhash *shp;
5875 shp = NFSSESSIONHASH(nd->nd_sessionid);
5877 NFSLOCKSESSION(shp);
5878 sep = nfsrv_findsession(nd->nd_sessionid);
5880 NFSUNLOCKSESSION(shp);
5882 return (NFSERR_BADSESSION);
5885 /* Check to see if reclaim complete has already happened. */
5886 if ((sep->sess_clp->lc_flags & LCL_RECLAIMCOMPLETE) != 0)
5887 error = NFSERR_COMPLETEALREADY;
5889 sep->sess_clp->lc_flags |= LCL_RECLAIMCOMPLETE;
5890 NFSUNLOCKSESSION(shp);
5896 * Cache the reply in a session slot.
5899 nfsrv_cache_session(uint8_t *sessionid, uint32_t slotid, int repstat,
5902 struct nfsdsession *sep;
5903 struct nfssessionhash *shp;
5905 shp = NFSSESSIONHASH(sessionid);
5906 NFSLOCKSESSION(shp);
5907 sep = nfsrv_findsession(sessionid);
5909 NFSUNLOCKSESSION(shp);
5910 printf("nfsrv_cache_session: no session\n");
5914 nfsv4_seqsess_cacherep(slotid, sep->sess_slots, repstat, m);
5915 NFSUNLOCKSESSION(shp);
5919 * Search for a session that matches the sessionid.
5921 static struct nfsdsession *
5922 nfsrv_findsession(uint8_t *sessionid)
5924 struct nfsdsession *sep;
5925 struct nfssessionhash *shp;
5927 shp = NFSSESSIONHASH(sessionid);
5928 LIST_FOREACH(sep, &shp->list, sess_hash) {
5929 if (!NFSBCMP(sessionid, sep->sess_sessionid, NFSX_V4SESSIONID))
5936 * Destroy a session.
5939 nfsrv_destroysession(struct nfsrv_descript *nd, uint8_t *sessionid)
5941 int error, samesess;
5944 if (!NFSBCMP(sessionid, nd->nd_sessionid, NFSX_V4SESSIONID)) {
5946 if ((nd->nd_flag & ND_LASTOP) == 0)
5947 return (NFSERR_BADSESSION);
5949 error = nfsrv_freesession(NULL, sessionid);
5950 if (error == 0 && samesess != 0)
5951 nd->nd_flag &= ~ND_HASSEQUENCE;
5956 * Free up a session structure.
5959 nfsrv_freesession(struct nfsdsession *sep, uint8_t *sessionid)
5961 struct nfssessionhash *shp;
5966 shp = NFSSESSIONHASH(sessionid);
5967 NFSLOCKSESSION(shp);
5968 sep = nfsrv_findsession(sessionid);
5970 shp = NFSSESSIONHASH(sep->sess_sessionid);
5971 NFSLOCKSESSION(shp);
5975 if (sep->sess_refcnt > 0) {
5976 NFSUNLOCKSESSION(shp);
5980 LIST_REMOVE(sep, sess_hash);
5981 LIST_REMOVE(sep, sess_list);
5983 NFSUNLOCKSESSION(shp);
5986 return (NFSERR_BADSESSION);
5987 for (i = 0; i < NFSV4_SLOTS; i++)
5988 if (sep->sess_slots[i].nfssl_reply != NULL)
5989 m_freem(sep->sess_slots[i].nfssl_reply);
5990 if (sep->sess_cbsess.nfsess_xprt != NULL)
5991 SVC_RELEASE(sep->sess_cbsess.nfsess_xprt);
5992 free(sep, M_NFSDSESSION);
5998 * RFC5661 says that it should fail when there are associated opens, locks
5999 * or delegations. Since stateids represent opens, I don't see how you can
6000 * free an open stateid (it will be free'd when closed), so this function
6001 * only works for lock stateids (freeing the lock_owner) or delegations.
6004 nfsrv_freestateid(struct nfsrv_descript *nd, nfsv4stateid_t *stateidp,
6007 struct nfsclient *clp;
6008 struct nfsstate *stp;
6013 * Look up the stateid
6015 error = nfsrv_getclient((nfsquad_t)((u_quad_t)0), CLOPS_RENEW, &clp,
6016 NULL, (nfsquad_t)((u_quad_t)0), 0, nd, p);
6018 /* First, check for a delegation. */
6019 LIST_FOREACH(stp, &clp->lc_deleg, ls_list) {
6020 if (!NFSBCMP(stp->ls_stateid.other, stateidp->other,
6025 nfsrv_freedeleg(stp);
6030 /* Not a delegation, try for a lock_owner. */
6032 error = nfsrv_getstate(clp, stateidp, 0, &stp);
6033 if (error == 0 && ((stp->ls_flags & (NFSLCK_OPEN | NFSLCK_DELEGREAD |
6034 NFSLCK_DELEGWRITE)) != 0 || (stp->ls_flags & NFSLCK_LOCK) == 0))
6035 /* Not a lock_owner stateid. */
6036 error = NFSERR_LOCKSHELD;
6037 if (error == 0 && !LIST_EMPTY(&stp->ls_lock))
6038 error = NFSERR_LOCKSHELD;
6040 nfsrv_freelockowner(stp, NULL, 0, p);
6046 * Generate the xdr for an NFSv4.1 CBSequence Operation.
6049 nfsv4_setcbsequence(struct nfsrv_descript *nd, struct nfsclient *clp,
6050 int dont_replycache, struct nfsdsession **sepp)
6052 struct nfsdsession *sep;
6053 uint32_t *tl, slotseq = 0;
6054 int maxslot, slotpos;
6055 uint8_t sessionid[NFSX_V4SESSIONID];
6058 error = nfsv4_getcbsession(clp, sepp);
6062 (void)nfsv4_sequencelookup(NULL, &sep->sess_cbsess, &slotpos, &maxslot,
6063 &slotseq, sessionid);
6064 KASSERT(maxslot >= 0, ("nfsv4_setcbsequence neg maxslot"));
6066 /* Build the Sequence arguments. */
6067 NFSM_BUILD(tl, uint32_t *, NFSX_V4SESSIONID + 5 * NFSX_UNSIGNED);
6068 bcopy(sessionid, tl, NFSX_V4SESSIONID);
6069 tl += NFSX_V4SESSIONID / NFSX_UNSIGNED;
6070 nd->nd_slotseq = tl;
6071 *tl++ = txdr_unsigned(slotseq);
6072 *tl++ = txdr_unsigned(slotpos);
6073 *tl++ = txdr_unsigned(maxslot);
6074 if (dont_replycache == 0)
6075 *tl++ = newnfs_true;
6077 *tl++ = newnfs_false;
6078 *tl = 0; /* No referring call list, for now. */
6079 nd->nd_flag |= ND_HASSEQUENCE;
6084 * Get a session for the callback.
6087 nfsv4_getcbsession(struct nfsclient *clp, struct nfsdsession **sepp)
6089 struct nfsdsession *sep;
6092 LIST_FOREACH(sep, &clp->lc_session, sess_list) {
6093 if ((sep->sess_crflags & NFSV4CRSESS_CONNBACKCHAN) != 0)
6098 return (NFSERR_BADSESSION);
6107 * Free up all backchannel xprts. This needs to be done when the nfsd threads
6108 * exit, since those transports will all be going away.
6109 * This is only called after all the nfsd threads are done performing RPCs,
6110 * so locking shouldn't be an issue.
6113 nfsrv_freeallbackchannel_xprts(void)
6115 struct nfsdsession *sep;
6116 struct nfsclient *clp;
6120 for (i = 0; i < nfsrv_clienthashsize; i++) {
6121 LIST_FOREACH(clp, &nfsclienthash[i], lc_hash) {
6122 LIST_FOREACH(sep, &clp->lc_session, sess_list) {
6123 xprt = sep->sess_cbsess.nfsess_xprt;
6124 sep->sess_cbsess.nfsess_xprt = NULL;