2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2009 Rick Macklem, University of Guelph
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
34 #include <fs/nfs/nfsport.h>
36 struct nfsrv_stablefirst nfsrv_stablefirst;
37 int nfsrv_issuedelegs = 0;
38 int nfsrv_dolocallocks = 0;
39 struct nfsv4lock nfsv4rootfs_lock;
41 extern int newnfs_numnfsd;
42 extern struct nfsstatsv1 nfsstatsv1;
43 extern int nfsrv_lease;
44 extern struct timeval nfsboottime;
45 extern u_int32_t newnfs_true, newnfs_false;
49 SYSCTL_DECL(_vfs_nfsd);
50 int nfsrv_statehashsize = NFSSTATEHASHSIZE;
51 SYSCTL_INT(_vfs_nfsd, OID_AUTO, statehashsize, CTLFLAG_RDTUN,
52 &nfsrv_statehashsize, 0,
53 "Size of state hash table set via loader.conf");
55 int nfsrv_clienthashsize = NFSCLIENTHASHSIZE;
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 SYSCTL_INT(_vfs_nfsd, OID_AUTO, fhhashsize, CTLFLAG_RDTUN,
62 &nfsrv_lockhashsize, 0,
63 "Size of file handle hash table set via loader.conf");
65 int nfsrv_sessionhashsize = NFSSESSIONHASHSIZE;
66 SYSCTL_INT(_vfs_nfsd, OID_AUTO, sessionhashsize, CTLFLAG_RDTUN,
67 &nfsrv_sessionhashsize, 0,
68 "Size of session hash table set via loader.conf");
70 static int nfsrv_v4statelimit = NFSRV_V4STATELIMIT;
71 SYSCTL_INT(_vfs_nfsd, OID_AUTO, v4statelimit, CTLFLAG_RWTUN,
72 &nfsrv_v4statelimit, 0,
73 "High water limit for NFSv4 opens+locks+delegations");
75 static int nfsrv_writedelegifpos = 0;
76 SYSCTL_INT(_vfs_nfsd, OID_AUTO, writedelegifpos, CTLFLAG_RW,
77 &nfsrv_writedelegifpos, 0,
78 "Issue a write delegation for read opens if possible");
80 static int nfsrv_allowreadforwriteopen = 1;
81 SYSCTL_INT(_vfs_nfsd, OID_AUTO, allowreadforwriteopen, CTLFLAG_RW,
82 &nfsrv_allowreadforwriteopen, 0,
83 "Allow Reads to be done with Write Access StateIDs");
86 * Hash lists for nfs V4.
88 struct nfsclienthashhead *nfsclienthash;
89 struct nfslockhashhead *nfslockhash;
90 struct nfssessionhash *nfssessionhash;
91 #endif /* !APPLEKEXT */
93 static u_int32_t nfsrv_openpluslock = 0, nfsrv_delegatecnt = 0;
94 static time_t nfsrvboottime;
95 static int nfsrv_returnoldstateid = 0, nfsrv_clients = 0;
96 static int nfsrv_clienthighwater = NFSRV_CLIENTHIGHWATER;
97 static int nfsrv_nogsscallback = 0;
98 static volatile int nfsrv_writedelegcnt = 0;
100 /* local functions */
101 static void nfsrv_dumpaclient(struct nfsclient *clp,
102 struct nfsd_dumpclients *dumpp);
103 static void nfsrv_freeopenowner(struct nfsstate *stp, int cansleep,
105 static int nfsrv_freeopen(struct nfsstate *stp, vnode_t vp, int cansleep,
107 static void nfsrv_freelockowner(struct nfsstate *stp, vnode_t vp, int cansleep,
109 static void nfsrv_freeallnfslocks(struct nfsstate *stp, vnode_t vp,
110 int cansleep, NFSPROC_T *p);
111 static void nfsrv_freenfslock(struct nfslock *lop);
112 static void nfsrv_freenfslockfile(struct nfslockfile *lfp);
113 static void nfsrv_freedeleg(struct nfsstate *);
114 static int nfsrv_getstate(struct nfsclient *clp, nfsv4stateid_t *stateidp,
115 u_int32_t flags, struct nfsstate **stpp);
116 static void nfsrv_getowner(struct nfsstatehead *hp, struct nfsstate *new_stp,
117 struct nfsstate **stpp);
118 static int nfsrv_getlockfh(vnode_t vp, u_short flags,
119 struct nfslockfile *new_lfp, fhandle_t *nfhp, NFSPROC_T *p);
120 static int nfsrv_getlockfile(u_short flags, struct nfslockfile **new_lfpp,
121 struct nfslockfile **lfpp, fhandle_t *nfhp, int lockit);
122 static void nfsrv_insertlock(struct nfslock *new_lop,
123 struct nfslock *insert_lop, struct nfsstate *stp, struct nfslockfile *lfp);
124 static void nfsrv_updatelock(struct nfsstate *stp, struct nfslock **new_lopp,
125 struct nfslock **other_lopp, struct nfslockfile *lfp);
126 static int nfsrv_getipnumber(u_char *cp);
127 static int nfsrv_checkrestart(nfsquad_t clientid, u_int32_t flags,
128 nfsv4stateid_t *stateidp, int specialid);
129 static int nfsrv_checkgrace(struct nfsrv_descript *nd, struct nfsclient *clp,
131 static int nfsrv_docallback(struct nfsclient *clp, int procnum,
132 nfsv4stateid_t *stateidp, int trunc, fhandle_t *fhp,
133 struct nfsvattr *nap, nfsattrbit_t *attrbitp, NFSPROC_T *p);
134 static int nfsrv_cbcallargs(struct nfsrv_descript *nd, struct nfsclient *clp,
135 uint32_t callback, int op, const char *optag, struct nfsdsession **sepp);
136 static u_int32_t nfsrv_nextclientindex(void);
137 static u_int32_t nfsrv_nextstateindex(struct nfsclient *clp);
138 static void nfsrv_markstable(struct nfsclient *clp);
139 static int nfsrv_checkstable(struct nfsclient *clp);
140 static int nfsrv_clientconflict(struct nfsclient *clp, int *haslockp, struct
141 vnode *vp, NFSPROC_T *p);
142 static int nfsrv_delegconflict(struct nfsstate *stp, int *haslockp,
143 NFSPROC_T *p, vnode_t vp);
144 static int nfsrv_cleandeleg(vnode_t vp, struct nfslockfile *lfp,
145 struct nfsclient *clp, int *haslockp, NFSPROC_T *p);
146 static int nfsrv_notsamecredname(struct nfsrv_descript *nd,
147 struct nfsclient *clp);
148 static time_t nfsrv_leaseexpiry(void);
149 static void nfsrv_delaydelegtimeout(struct nfsstate *stp);
150 static int nfsrv_checkseqid(struct nfsrv_descript *nd, u_int32_t seqid,
151 struct nfsstate *stp, struct nfsrvcache *op);
152 static int nfsrv_nootherstate(struct nfsstate *stp);
153 static int nfsrv_locallock(vnode_t vp, struct nfslockfile *lfp, int flags,
154 uint64_t first, uint64_t end, struct nfslockconflict *cfp, NFSPROC_T *p);
155 static void nfsrv_localunlock(vnode_t vp, struct nfslockfile *lfp,
156 uint64_t init_first, uint64_t init_end, NFSPROC_T *p);
157 static int nfsrv_dolocal(vnode_t vp, struct nfslockfile *lfp, int flags,
158 int oldflags, uint64_t first, uint64_t end, struct nfslockconflict *cfp,
160 static void nfsrv_locallock_rollback(vnode_t vp, struct nfslockfile *lfp,
162 static void nfsrv_locallock_commit(struct nfslockfile *lfp, int flags,
163 uint64_t first, uint64_t end);
164 static void nfsrv_locklf(struct nfslockfile *lfp);
165 static void nfsrv_unlocklf(struct nfslockfile *lfp);
166 static struct nfsdsession *nfsrv_findsession(uint8_t *sessionid);
167 static int nfsrv_freesession(struct nfsdsession *sep, uint8_t *sessionid);
168 static int nfsv4_setcbsequence(struct nfsrv_descript *nd, struct nfsclient *clp,
169 int dont_replycache, struct nfsdsession **sepp);
170 static int nfsv4_getcbsession(struct nfsclient *clp, struct nfsdsession **sepp);
173 * Scan the client list for a match and either return the current one,
174 * create a new entry or return an error.
175 * If returning a non-error, the clp structure must either be linked into
176 * the client list or free'd.
179 nfsrv_setclient(struct nfsrv_descript *nd, struct nfsclient **new_clpp,
180 nfsquad_t *clientidp, nfsquad_t *confirmp, NFSPROC_T *p)
182 struct nfsclient *clp = NULL, *new_clp = *new_clpp;
184 struct nfsstate *stp, *tstp;
185 struct sockaddr_in *sad, *rad;
186 int zapit = 0, gotit, hasstate = 0, igotlock;
187 static u_int64_t confirm_index = 0;
190 * Check for state resource limit exceeded.
192 if (nfsrv_openpluslock > nfsrv_v4statelimit) {
193 error = NFSERR_RESOURCE;
197 if (nfsrv_issuedelegs == 0 ||
198 ((nd->nd_flag & ND_GSS) != 0 && nfsrv_nogsscallback != 0))
200 * Don't do callbacks when delegations are disabled or
201 * for AUTH_GSS unless enabled via nfsrv_nogsscallback.
202 * If establishing a callback connection is attempted
203 * when a firewall is blocking the callback path, the
204 * server may wait too long for the connect attempt to
205 * succeed during the Open. Some clients, such as Linux,
206 * may timeout and give up on the Open before the server
207 * replies. Also, since AUTH_GSS callbacks are not
208 * yet interoperability tested, they might cause the
209 * server to crap out, if they get past the Init call to
212 new_clp->lc_program = 0;
214 /* Lock out other nfsd threads */
215 NFSLOCKV4ROOTMUTEX();
216 nfsv4_relref(&nfsv4rootfs_lock);
218 igotlock = nfsv4_lock(&nfsv4rootfs_lock, 1, NULL,
219 NFSV4ROOTLOCKMUTEXPTR, NULL);
221 NFSUNLOCKV4ROOTMUTEX();
224 * Search for a match in the client list.
227 while (i < nfsrv_clienthashsize && !gotit) {
228 LIST_FOREACH(clp, &nfsclienthash[i], lc_hash) {
229 if (new_clp->lc_idlen == clp->lc_idlen &&
230 !NFSBCMP(new_clp->lc_id, clp->lc_id, clp->lc_idlen)) {
239 (clp->lc_flags & (LCL_NEEDSCONFIRM | LCL_ADMINREVOKED))) {
240 if ((nd->nd_flag & ND_NFSV41) != 0 && confirmp->lval[1] != 0) {
242 * For NFSv4.1, if confirmp->lval[1] is non-zero, the
243 * client is trying to update a confirmed clientid.
245 NFSLOCKV4ROOTMUTEX();
246 nfsv4_unlock(&nfsv4rootfs_lock, 1);
247 NFSUNLOCKV4ROOTMUTEX();
248 confirmp->lval[1] = 0;
249 error = NFSERR_NOENT;
253 * Get rid of the old one.
255 if (i != nfsrv_clienthashsize) {
256 LIST_REMOVE(clp, lc_hash);
257 nfsrv_cleanclient(clp, p);
258 nfsrv_freedeleglist(&clp->lc_deleg);
259 nfsrv_freedeleglist(&clp->lc_olddeleg);
263 * Add it after assigning a client id to it.
265 new_clp->lc_flags |= LCL_NEEDSCONFIRM;
266 if ((nd->nd_flag & ND_NFSV41) != 0)
267 new_clp->lc_confirm.lval[0] = confirmp->lval[0] =
270 confirmp->qval = new_clp->lc_confirm.qval =
272 clientidp->lval[0] = new_clp->lc_clientid.lval[0] =
273 (u_int32_t)nfsrvboottime;
274 clientidp->lval[1] = new_clp->lc_clientid.lval[1] =
275 nfsrv_nextclientindex();
276 new_clp->lc_stateindex = 0;
277 new_clp->lc_statemaxindex = 0;
278 new_clp->lc_cbref = 0;
279 new_clp->lc_expiry = nfsrv_leaseexpiry();
280 LIST_INIT(&new_clp->lc_open);
281 LIST_INIT(&new_clp->lc_deleg);
282 LIST_INIT(&new_clp->lc_olddeleg);
283 LIST_INIT(&new_clp->lc_session);
284 for (i = 0; i < nfsrv_statehashsize; i++)
285 LIST_INIT(&new_clp->lc_stateid[i]);
286 LIST_INSERT_HEAD(NFSCLIENTHASH(new_clp->lc_clientid), new_clp,
288 nfsstatsv1.srvclients++;
289 nfsrv_openpluslock++;
291 NFSLOCKV4ROOTMUTEX();
292 nfsv4_unlock(&nfsv4rootfs_lock, 1);
293 NFSUNLOCKV4ROOTMUTEX();
295 nfsrv_zapclient(clp, p);
301 * Now, handle the cases where the id is already issued.
303 if (nfsrv_notsamecredname(nd, clp)) {
305 * Check to see if there is expired state that should go away.
307 if (clp->lc_expiry < NFSD_MONOSEC &&
308 (!LIST_EMPTY(&clp->lc_open) || !LIST_EMPTY(&clp->lc_deleg))) {
309 nfsrv_cleanclient(clp, p);
310 nfsrv_freedeleglist(&clp->lc_deleg);
314 * If there is outstanding state, then reply NFSERR_CLIDINUSE per
315 * RFC3530 Sec. 8.1.2 last para.
317 if (!LIST_EMPTY(&clp->lc_deleg)) {
319 } else if (LIST_EMPTY(&clp->lc_open)) {
323 /* Look for an Open on the OpenOwner */
324 LIST_FOREACH(stp, &clp->lc_open, ls_list) {
325 if (!LIST_EMPTY(&stp->ls_open)) {
333 * If the uid doesn't match, return NFSERR_CLIDINUSE after
334 * filling out the correct ipaddr and portnum.
336 sad = NFSSOCKADDR(new_clp->lc_req.nr_nam, struct sockaddr_in *);
337 rad = NFSSOCKADDR(clp->lc_req.nr_nam, struct sockaddr_in *);
338 sad->sin_addr.s_addr = rad->sin_addr.s_addr;
339 sad->sin_port = rad->sin_port;
340 NFSLOCKV4ROOTMUTEX();
341 nfsv4_unlock(&nfsv4rootfs_lock, 1);
342 NFSUNLOCKV4ROOTMUTEX();
343 error = NFSERR_CLIDINUSE;
348 if (NFSBCMP(new_clp->lc_verf, clp->lc_verf, NFSX_VERF)) {
350 * If the verifier has changed, the client has rebooted
351 * and a new client id is issued. The old state info
352 * can be thrown away once the SETCLIENTID_CONFIRM occurs.
354 LIST_REMOVE(clp, lc_hash);
355 new_clp->lc_flags |= LCL_NEEDSCONFIRM;
356 if ((nd->nd_flag & ND_NFSV41) != 0)
357 new_clp->lc_confirm.lval[0] = confirmp->lval[0] =
360 confirmp->qval = new_clp->lc_confirm.qval =
362 clientidp->lval[0] = new_clp->lc_clientid.lval[0] =
364 clientidp->lval[1] = new_clp->lc_clientid.lval[1] =
365 nfsrv_nextclientindex();
366 new_clp->lc_stateindex = 0;
367 new_clp->lc_statemaxindex = 0;
368 new_clp->lc_cbref = 0;
369 new_clp->lc_expiry = nfsrv_leaseexpiry();
372 * Save the state until confirmed.
374 LIST_NEWHEAD(&new_clp->lc_open, &clp->lc_open, ls_list);
375 LIST_FOREACH(tstp, &new_clp->lc_open, ls_list)
376 tstp->ls_clp = new_clp;
377 LIST_NEWHEAD(&new_clp->lc_deleg, &clp->lc_deleg, ls_list);
378 LIST_FOREACH(tstp, &new_clp->lc_deleg, ls_list)
379 tstp->ls_clp = new_clp;
380 LIST_NEWHEAD(&new_clp->lc_olddeleg, &clp->lc_olddeleg,
382 LIST_FOREACH(tstp, &new_clp->lc_olddeleg, ls_list)
383 tstp->ls_clp = new_clp;
384 for (i = 0; i < nfsrv_statehashsize; i++) {
385 LIST_NEWHEAD(&new_clp->lc_stateid[i],
386 &clp->lc_stateid[i], ls_hash);
387 LIST_FOREACH(tstp, &new_clp->lc_stateid[i], ls_hash)
388 tstp->ls_clp = new_clp;
390 LIST_INSERT_HEAD(NFSCLIENTHASH(new_clp->lc_clientid), new_clp,
392 nfsstatsv1.srvclients++;
393 nfsrv_openpluslock++;
395 NFSLOCKV4ROOTMUTEX();
396 nfsv4_unlock(&nfsv4rootfs_lock, 1);
397 NFSUNLOCKV4ROOTMUTEX();
400 * Must wait until any outstanding callback on the old clp
404 while (clp->lc_cbref) {
405 clp->lc_flags |= LCL_WAKEUPWANTED;
406 (void)mtx_sleep(clp, NFSSTATEMUTEXPTR, PZERO - 1,
407 "nfsd clp", 10 * hz);
410 nfsrv_zapclient(clp, p);
415 /* For NFSv4.1, mark that we found a confirmed clientid. */
416 if ((nd->nd_flag & ND_NFSV41) != 0) {
417 clientidp->lval[0] = clp->lc_clientid.lval[0];
418 clientidp->lval[1] = clp->lc_clientid.lval[1];
419 confirmp->lval[0] = 0; /* Ignored by client */
420 confirmp->lval[1] = 1;
423 * id and verifier match, so update the net address info
424 * and get rid of any existing callback authentication
425 * handle, so a new one will be acquired.
427 LIST_REMOVE(clp, lc_hash);
428 new_clp->lc_flags |= (LCL_NEEDSCONFIRM | LCL_DONTCLEAN);
429 new_clp->lc_expiry = nfsrv_leaseexpiry();
430 confirmp->qval = new_clp->lc_confirm.qval = ++confirm_index;
431 clientidp->lval[0] = new_clp->lc_clientid.lval[0] =
432 clp->lc_clientid.lval[0];
433 clientidp->lval[1] = new_clp->lc_clientid.lval[1] =
434 clp->lc_clientid.lval[1];
435 new_clp->lc_delegtime = clp->lc_delegtime;
436 new_clp->lc_stateindex = clp->lc_stateindex;
437 new_clp->lc_statemaxindex = clp->lc_statemaxindex;
438 new_clp->lc_cbref = 0;
439 LIST_NEWHEAD(&new_clp->lc_open, &clp->lc_open, ls_list);
440 LIST_FOREACH(tstp, &new_clp->lc_open, ls_list)
441 tstp->ls_clp = new_clp;
442 LIST_NEWHEAD(&new_clp->lc_deleg, &clp->lc_deleg, ls_list);
443 LIST_FOREACH(tstp, &new_clp->lc_deleg, ls_list)
444 tstp->ls_clp = new_clp;
445 LIST_NEWHEAD(&new_clp->lc_olddeleg, &clp->lc_olddeleg, ls_list);
446 LIST_FOREACH(tstp, &new_clp->lc_olddeleg, ls_list)
447 tstp->ls_clp = new_clp;
448 for (i = 0; i < nfsrv_statehashsize; i++) {
449 LIST_NEWHEAD(&new_clp->lc_stateid[i],
450 &clp->lc_stateid[i], ls_hash);
451 LIST_FOREACH(tstp, &new_clp->lc_stateid[i], ls_hash)
452 tstp->ls_clp = new_clp;
454 LIST_INSERT_HEAD(NFSCLIENTHASH(new_clp->lc_clientid), new_clp,
456 nfsstatsv1.srvclients++;
457 nfsrv_openpluslock++;
460 NFSLOCKV4ROOTMUTEX();
461 nfsv4_unlock(&nfsv4rootfs_lock, 1);
462 NFSUNLOCKV4ROOTMUTEX();
464 if ((nd->nd_flag & ND_NFSV41) == 0) {
466 * Must wait until any outstanding callback on the old clp
470 while (clp->lc_cbref) {
471 clp->lc_flags |= LCL_WAKEUPWANTED;
472 (void)mtx_sleep(clp, NFSSTATEMUTEXPTR, PZERO - 1,
476 nfsrv_zapclient(clp, p);
481 NFSEXITCODE2(error, nd);
486 * Check to see if the client id exists and optionally confirm it.
489 nfsrv_getclient(nfsquad_t clientid, int opflags, struct nfsclient **clpp,
490 struct nfsdsession *nsep, nfsquad_t confirm, uint32_t cbprogram,
491 struct nfsrv_descript *nd, NFSPROC_T *p)
493 struct nfsclient *clp;
494 struct nfsstate *stp;
496 struct nfsclienthashhead *hp;
497 int error = 0, igotlock, doneok;
498 struct nfssessionhash *shp;
499 struct nfsdsession *sep;
501 static uint64_t next_sess = 0;
505 if ((nd == NULL || (nd->nd_flag & ND_NFSV41) == 0 ||
506 opflags != CLOPS_RENEW) && nfsrvboottime != clientid.lval[0]) {
507 error = NFSERR_STALECLIENTID;
512 * If called with opflags == CLOPS_RENEW, the State Lock is
513 * already held. Otherwise, we need to get either that or,
514 * for the case of Confirm, lock out the nfsd threads.
516 if (opflags & CLOPS_CONFIRM) {
517 NFSLOCKV4ROOTMUTEX();
518 nfsv4_relref(&nfsv4rootfs_lock);
520 igotlock = nfsv4_lock(&nfsv4rootfs_lock, 1, NULL,
521 NFSV4ROOTLOCKMUTEXPTR, NULL);
524 * Create a new sessionid here, since we need to do it where
525 * there is a mutex held to serialize update of next_sess.
527 if ((nd->nd_flag & ND_NFSV41) != 0) {
528 sessid[0] = ++next_sess;
529 sessid[1] = clientid.qval;
531 NFSUNLOCKV4ROOTMUTEX();
532 } else if (opflags != CLOPS_RENEW) {
536 /* For NFSv4.1, the clp is acquired from the associated session. */
537 if (nd != NULL && (nd->nd_flag & ND_NFSV41) != 0 &&
538 opflags == CLOPS_RENEW) {
540 if ((nd->nd_flag & ND_HASSEQUENCE) != 0) {
541 shp = NFSSESSIONHASH(nd->nd_sessionid);
543 sep = nfsrv_findsession(nd->nd_sessionid);
546 NFSUNLOCKSESSION(shp);
549 hp = NFSCLIENTHASH(clientid);
550 LIST_FOREACH(clp, hp, lc_hash) {
551 if (clp->lc_clientid.lval[1] == clientid.lval[1])
556 if (opflags & CLOPS_CONFIRM)
557 error = NFSERR_STALECLIENTID;
559 error = NFSERR_EXPIRED;
560 } else if (clp->lc_flags & LCL_ADMINREVOKED) {
562 * If marked admin revoked, just return the error.
564 error = NFSERR_ADMINREVOKED;
567 if (opflags & CLOPS_CONFIRM) {
568 NFSLOCKV4ROOTMUTEX();
569 nfsv4_unlock(&nfsv4rootfs_lock, 1);
570 NFSUNLOCKV4ROOTMUTEX();
571 } else if (opflags != CLOPS_RENEW) {
578 * Perform any operations specified by the opflags.
580 if (opflags & CLOPS_CONFIRM) {
581 if (((nd->nd_flag & ND_NFSV41) != 0 &&
582 clp->lc_confirm.lval[0] != confirm.lval[0]) ||
583 ((nd->nd_flag & ND_NFSV41) == 0 &&
584 clp->lc_confirm.qval != confirm.qval))
585 error = NFSERR_STALECLIENTID;
586 else if (nfsrv_notsamecredname(nd, clp))
587 error = NFSERR_CLIDINUSE;
590 if ((clp->lc_flags & (LCL_NEEDSCONFIRM | LCL_DONTCLEAN)) ==
593 * Hang onto the delegations (as old delegations)
594 * for an Open with CLAIM_DELEGATE_PREV unless in
595 * grace, but get rid of the rest of the state.
597 nfsrv_cleanclient(clp, p);
598 nfsrv_freedeleglist(&clp->lc_olddeleg);
599 if (nfsrv_checkgrace(nd, clp, 0)) {
600 /* In grace, so just delete delegations */
601 nfsrv_freedeleglist(&clp->lc_deleg);
603 LIST_FOREACH(stp, &clp->lc_deleg, ls_list)
604 stp->ls_flags |= NFSLCK_OLDDELEG;
605 clp->lc_delegtime = NFSD_MONOSEC +
606 nfsrv_lease + NFSRV_LEASEDELTA;
607 LIST_NEWHEAD(&clp->lc_olddeleg, &clp->lc_deleg,
610 if ((nd->nd_flag & ND_NFSV41) != 0)
611 clp->lc_program = cbprogram;
613 clp->lc_flags &= ~(LCL_NEEDSCONFIRM | LCL_DONTCLEAN);
615 clp->lc_flags |= LCL_NEEDSCBNULL;
616 /* For NFSv4.1, link the session onto the client. */
618 /* Hold a reference on the xprt for a backchannel. */
619 if ((nsep->sess_crflags & NFSV4CRSESS_CONNBACKCHAN)
620 != 0 && clp->lc_req.nr_client == NULL) {
621 clp->lc_req.nr_client = (struct __rpc_client *)
622 clnt_bck_create(nd->nd_xprt->xp_socket,
623 cbprogram, NFSV4_CBVERS);
624 if (clp->lc_req.nr_client != NULL) {
625 SVC_ACQUIRE(nd->nd_xprt);
627 clp->lc_req.nr_client->cl_private;
628 /* Disable idle timeout. */
629 nd->nd_xprt->xp_idletimeout = 0;
630 nsep->sess_cbsess.nfsess_xprt = nd->nd_xprt;
632 nsep->sess_crflags &= ~NFSV4CRSESS_CONNBACKCHAN;
634 NFSBCOPY(sessid, nsep->sess_sessionid,
636 NFSBCOPY(sessid, nsep->sess_cbsess.nfsess_sessionid,
638 shp = NFSSESSIONHASH(nsep->sess_sessionid);
641 LIST_INSERT_HEAD(&shp->list, nsep, sess_hash);
642 LIST_INSERT_HEAD(&clp->lc_session, nsep, sess_list);
643 nsep->sess_clp = clp;
644 NFSUNLOCKSESSION(shp);
648 } else if (clp->lc_flags & LCL_NEEDSCONFIRM) {
649 error = NFSERR_EXPIRED;
653 * If called by the Renew Op, we must check the principal.
655 if (!error && (opflags & CLOPS_RENEWOP)) {
656 if (nfsrv_notsamecredname(nd, clp)) {
658 for (i = 0; i < nfsrv_statehashsize && doneok == 0; i++) {
659 LIST_FOREACH(stp, &clp->lc_stateid[i], ls_hash) {
660 if ((stp->ls_flags & NFSLCK_OPEN) &&
661 stp->ls_uid == nd->nd_cred->cr_uid) {
668 error = NFSERR_ACCES;
670 if (!error && (clp->lc_flags & LCL_CBDOWN))
671 error = NFSERR_CBPATHDOWN;
673 if ((!error || error == NFSERR_CBPATHDOWN) &&
674 (opflags & CLOPS_RENEW)) {
675 clp->lc_expiry = nfsrv_leaseexpiry();
677 if (opflags & CLOPS_CONFIRM) {
678 NFSLOCKV4ROOTMUTEX();
679 nfsv4_unlock(&nfsv4rootfs_lock, 1);
680 NFSUNLOCKV4ROOTMUTEX();
681 } else if (opflags != CLOPS_RENEW) {
688 NFSEXITCODE2(error, nd);
693 * Perform the NFSv4.1 destroy clientid.
696 nfsrv_destroyclient(nfsquad_t clientid, NFSPROC_T *p)
698 struct nfsclient *clp;
699 struct nfsclienthashhead *hp;
700 int error = 0, i, igotlock;
702 if (nfsrvboottime != clientid.lval[0]) {
703 error = NFSERR_STALECLIENTID;
707 /* Lock out other nfsd threads */
708 NFSLOCKV4ROOTMUTEX();
709 nfsv4_relref(&nfsv4rootfs_lock);
711 igotlock = nfsv4_lock(&nfsv4rootfs_lock, 1, NULL,
712 NFSV4ROOTLOCKMUTEXPTR, NULL);
713 } while (igotlock == 0);
714 NFSUNLOCKV4ROOTMUTEX();
716 hp = NFSCLIENTHASH(clientid);
717 LIST_FOREACH(clp, hp, lc_hash) {
718 if (clp->lc_clientid.lval[1] == clientid.lval[1])
722 NFSLOCKV4ROOTMUTEX();
723 nfsv4_unlock(&nfsv4rootfs_lock, 1);
724 NFSUNLOCKV4ROOTMUTEX();
725 /* Just return ok, since it is gone. */
729 /* Scan for state on the clientid. */
730 for (i = 0; i < nfsrv_statehashsize; i++)
731 if (!LIST_EMPTY(&clp->lc_stateid[i])) {
732 NFSLOCKV4ROOTMUTEX();
733 nfsv4_unlock(&nfsv4rootfs_lock, 1);
734 NFSUNLOCKV4ROOTMUTEX();
735 error = NFSERR_CLIENTIDBUSY;
738 if (!LIST_EMPTY(&clp->lc_session) || !LIST_EMPTY(&clp->lc_deleg)) {
739 NFSLOCKV4ROOTMUTEX();
740 nfsv4_unlock(&nfsv4rootfs_lock, 1);
741 NFSUNLOCKV4ROOTMUTEX();
742 error = NFSERR_CLIENTIDBUSY;
746 /* Destroy the clientid and return ok. */
747 nfsrv_cleanclient(clp, p);
748 nfsrv_freedeleglist(&clp->lc_deleg);
749 nfsrv_freedeleglist(&clp->lc_olddeleg);
750 LIST_REMOVE(clp, lc_hash);
751 NFSLOCKV4ROOTMUTEX();
752 nfsv4_unlock(&nfsv4rootfs_lock, 1);
753 NFSUNLOCKV4ROOTMUTEX();
754 nfsrv_zapclient(clp, p);
756 NFSEXITCODE2(error, nd);
761 * Called from the new nfssvc syscall to admin revoke a clientid.
762 * Returns 0 for success, error otherwise.
765 nfsrv_adminrevoke(struct nfsd_clid *revokep, NFSPROC_T *p)
767 struct nfsclient *clp = NULL;
772 * First, lock out the nfsd so that state won't change while the
773 * revocation record is being written to the stable storage restart
776 NFSLOCKV4ROOTMUTEX();
778 igotlock = nfsv4_lock(&nfsv4rootfs_lock, 1, NULL,
779 NFSV4ROOTLOCKMUTEXPTR, NULL);
781 NFSUNLOCKV4ROOTMUTEX();
784 * Search for a match in the client list.
787 while (i < nfsrv_clienthashsize && !gotit) {
788 LIST_FOREACH(clp, &nfsclienthash[i], lc_hash) {
789 if (revokep->nclid_idlen == clp->lc_idlen &&
790 !NFSBCMP(revokep->nclid_id, clp->lc_id, clp->lc_idlen)) {
798 NFSLOCKV4ROOTMUTEX();
799 nfsv4_unlock(&nfsv4rootfs_lock, 0);
800 NFSUNLOCKV4ROOTMUTEX();
806 * Now, write out the revocation record
808 nfsrv_writestable(clp->lc_id, clp->lc_idlen, NFSNST_REVOKE, p);
809 nfsrv_backupstable();
812 * and clear out the state, marking the clientid revoked.
814 clp->lc_flags &= ~LCL_CALLBACKSON;
815 clp->lc_flags |= LCL_ADMINREVOKED;
816 nfsrv_cleanclient(clp, p);
817 nfsrv_freedeleglist(&clp->lc_deleg);
818 nfsrv_freedeleglist(&clp->lc_olddeleg);
819 NFSLOCKV4ROOTMUTEX();
820 nfsv4_unlock(&nfsv4rootfs_lock, 0);
821 NFSUNLOCKV4ROOTMUTEX();
829 * Dump out stats for all clients. Called from nfssvc(2), that is used
833 nfsrv_dumpclients(struct nfsd_dumpclients *dumpp, int maxcnt)
835 struct nfsclient *clp;
839 * First, get a reference on the nfsv4rootfs_lock so that an
840 * exclusive lock cannot be acquired while dumping the clients.
842 NFSLOCKV4ROOTMUTEX();
843 nfsv4_getref(&nfsv4rootfs_lock, NULL, NFSV4ROOTLOCKMUTEXPTR, NULL);
844 NFSUNLOCKV4ROOTMUTEX();
847 * Rattle through the client lists until done.
849 while (i < nfsrv_clienthashsize && cnt < maxcnt) {
850 clp = LIST_FIRST(&nfsclienthash[i]);
851 while (clp != LIST_END(&nfsclienthash[i]) && cnt < maxcnt) {
852 nfsrv_dumpaclient(clp, &dumpp[cnt]);
854 clp = LIST_NEXT(clp, lc_hash);
859 dumpp[cnt].ndcl_clid.nclid_idlen = 0;
861 NFSLOCKV4ROOTMUTEX();
862 nfsv4_relref(&nfsv4rootfs_lock);
863 NFSUNLOCKV4ROOTMUTEX();
867 * Dump stats for a client. Must be called with the NFSSTATELOCK and spl'd.
870 nfsrv_dumpaclient(struct nfsclient *clp, struct nfsd_dumpclients *dumpp)
872 struct nfsstate *stp, *openstp, *lckownstp;
874 struct sockaddr *sad;
875 struct sockaddr_in *rad;
876 struct sockaddr_in6 *rad6;
878 dumpp->ndcl_nopenowners = dumpp->ndcl_nlockowners = 0;
879 dumpp->ndcl_nopens = dumpp->ndcl_nlocks = 0;
880 dumpp->ndcl_ndelegs = dumpp->ndcl_nolddelegs = 0;
881 dumpp->ndcl_flags = clp->lc_flags;
882 dumpp->ndcl_clid.nclid_idlen = clp->lc_idlen;
883 NFSBCOPY(clp->lc_id, dumpp->ndcl_clid.nclid_id, clp->lc_idlen);
884 sad = NFSSOCKADDR(clp->lc_req.nr_nam, struct sockaddr *);
885 dumpp->ndcl_addrfam = sad->sa_family;
886 if (sad->sa_family == AF_INET) {
887 rad = (struct sockaddr_in *)sad;
888 dumpp->ndcl_cbaddr.sin_addr = rad->sin_addr;
890 rad6 = (struct sockaddr_in6 *)sad;
891 dumpp->ndcl_cbaddr.sin6_addr = rad6->sin6_addr;
895 * Now, scan the state lists and total up the opens and locks.
897 LIST_FOREACH(stp, &clp->lc_open, ls_list) {
898 dumpp->ndcl_nopenowners++;
899 LIST_FOREACH(openstp, &stp->ls_open, ls_list) {
900 dumpp->ndcl_nopens++;
901 LIST_FOREACH(lckownstp, &openstp->ls_open, ls_list) {
902 dumpp->ndcl_nlockowners++;
903 LIST_FOREACH(lop, &lckownstp->ls_lock, lo_lckowner) {
904 dumpp->ndcl_nlocks++;
911 * and the delegation lists.
913 LIST_FOREACH(stp, &clp->lc_deleg, ls_list) {
914 dumpp->ndcl_ndelegs++;
916 LIST_FOREACH(stp, &clp->lc_olddeleg, ls_list) {
917 dumpp->ndcl_nolddelegs++;
922 * Dump out lock stats for a file.
925 nfsrv_dumplocks(vnode_t vp, struct nfsd_dumplocks *ldumpp, int maxcnt,
928 struct nfsstate *stp;
931 struct nfslockfile *lfp;
932 struct sockaddr *sad;
933 struct sockaddr_in *rad;
934 struct sockaddr_in6 *rad6;
938 ret = nfsrv_getlockfh(vp, 0, NULL, &nfh, p);
940 * First, get a reference on the nfsv4rootfs_lock so that an
941 * exclusive lock on it cannot be acquired while dumping the locks.
943 NFSLOCKV4ROOTMUTEX();
944 nfsv4_getref(&nfsv4rootfs_lock, NULL, NFSV4ROOTLOCKMUTEXPTR, NULL);
945 NFSUNLOCKV4ROOTMUTEX();
948 ret = nfsrv_getlockfile(0, NULL, &lfp, &nfh, 0);
950 ldumpp[0].ndlck_clid.nclid_idlen = 0;
952 NFSLOCKV4ROOTMUTEX();
953 nfsv4_relref(&nfsv4rootfs_lock);
954 NFSUNLOCKV4ROOTMUTEX();
959 * For each open share on file, dump it out.
961 stp = LIST_FIRST(&lfp->lf_open);
962 while (stp != LIST_END(&lfp->lf_open) && cnt < maxcnt) {
963 ldumpp[cnt].ndlck_flags = stp->ls_flags;
964 ldumpp[cnt].ndlck_stateid.seqid = stp->ls_stateid.seqid;
965 ldumpp[cnt].ndlck_stateid.other[0] = stp->ls_stateid.other[0];
966 ldumpp[cnt].ndlck_stateid.other[1] = stp->ls_stateid.other[1];
967 ldumpp[cnt].ndlck_stateid.other[2] = stp->ls_stateid.other[2];
968 ldumpp[cnt].ndlck_owner.nclid_idlen =
969 stp->ls_openowner->ls_ownerlen;
970 NFSBCOPY(stp->ls_openowner->ls_owner,
971 ldumpp[cnt].ndlck_owner.nclid_id,
972 stp->ls_openowner->ls_ownerlen);
973 ldumpp[cnt].ndlck_clid.nclid_idlen = stp->ls_clp->lc_idlen;
974 NFSBCOPY(stp->ls_clp->lc_id, ldumpp[cnt].ndlck_clid.nclid_id,
975 stp->ls_clp->lc_idlen);
976 sad=NFSSOCKADDR(stp->ls_clp->lc_req.nr_nam, struct sockaddr *);
977 ldumpp[cnt].ndlck_addrfam = sad->sa_family;
978 if (sad->sa_family == AF_INET) {
979 rad = (struct sockaddr_in *)sad;
980 ldumpp[cnt].ndlck_cbaddr.sin_addr = rad->sin_addr;
982 rad6 = (struct sockaddr_in6 *)sad;
983 ldumpp[cnt].ndlck_cbaddr.sin6_addr = rad6->sin6_addr;
985 stp = LIST_NEXT(stp, ls_file);
992 lop = LIST_FIRST(&lfp->lf_lock);
993 while (lop != LIST_END(&lfp->lf_lock) && cnt < maxcnt) {
995 ldumpp[cnt].ndlck_flags = lop->lo_flags;
996 ldumpp[cnt].ndlck_first = lop->lo_first;
997 ldumpp[cnt].ndlck_end = lop->lo_end;
998 ldumpp[cnt].ndlck_stateid.seqid = stp->ls_stateid.seqid;
999 ldumpp[cnt].ndlck_stateid.other[0] = stp->ls_stateid.other[0];
1000 ldumpp[cnt].ndlck_stateid.other[1] = stp->ls_stateid.other[1];
1001 ldumpp[cnt].ndlck_stateid.other[2] = stp->ls_stateid.other[2];
1002 ldumpp[cnt].ndlck_owner.nclid_idlen = stp->ls_ownerlen;
1003 NFSBCOPY(stp->ls_owner, ldumpp[cnt].ndlck_owner.nclid_id,
1005 ldumpp[cnt].ndlck_clid.nclid_idlen = stp->ls_clp->lc_idlen;
1006 NFSBCOPY(stp->ls_clp->lc_id, ldumpp[cnt].ndlck_clid.nclid_id,
1007 stp->ls_clp->lc_idlen);
1008 sad=NFSSOCKADDR(stp->ls_clp->lc_req.nr_nam, struct sockaddr *);
1009 ldumpp[cnt].ndlck_addrfam = sad->sa_family;
1010 if (sad->sa_family == AF_INET) {
1011 rad = (struct sockaddr_in *)sad;
1012 ldumpp[cnt].ndlck_cbaddr.sin_addr = rad->sin_addr;
1014 rad6 = (struct sockaddr_in6 *)sad;
1015 ldumpp[cnt].ndlck_cbaddr.sin6_addr = rad6->sin6_addr;
1017 lop = LIST_NEXT(lop, lo_lckfile);
1022 * and the delegations.
1024 stp = LIST_FIRST(&lfp->lf_deleg);
1025 while (stp != LIST_END(&lfp->lf_deleg) && cnt < maxcnt) {
1026 ldumpp[cnt].ndlck_flags = stp->ls_flags;
1027 ldumpp[cnt].ndlck_stateid.seqid = stp->ls_stateid.seqid;
1028 ldumpp[cnt].ndlck_stateid.other[0] = stp->ls_stateid.other[0];
1029 ldumpp[cnt].ndlck_stateid.other[1] = stp->ls_stateid.other[1];
1030 ldumpp[cnt].ndlck_stateid.other[2] = stp->ls_stateid.other[2];
1031 ldumpp[cnt].ndlck_owner.nclid_idlen = 0;
1032 ldumpp[cnt].ndlck_clid.nclid_idlen = stp->ls_clp->lc_idlen;
1033 NFSBCOPY(stp->ls_clp->lc_id, ldumpp[cnt].ndlck_clid.nclid_id,
1034 stp->ls_clp->lc_idlen);
1035 sad=NFSSOCKADDR(stp->ls_clp->lc_req.nr_nam, struct sockaddr *);
1036 ldumpp[cnt].ndlck_addrfam = sad->sa_family;
1037 if (sad->sa_family == AF_INET) {
1038 rad = (struct sockaddr_in *)sad;
1039 ldumpp[cnt].ndlck_cbaddr.sin_addr = rad->sin_addr;
1041 rad6 = (struct sockaddr_in6 *)sad;
1042 ldumpp[cnt].ndlck_cbaddr.sin6_addr = rad6->sin6_addr;
1044 stp = LIST_NEXT(stp, ls_file);
1049 * If list isn't full, mark end of list by setting the client name
1053 ldumpp[cnt].ndlck_clid.nclid_idlen = 0;
1055 NFSLOCKV4ROOTMUTEX();
1056 nfsv4_relref(&nfsv4rootfs_lock);
1057 NFSUNLOCKV4ROOTMUTEX();
1061 * Server timer routine. It can scan any linked list, so long
1062 * as it holds the spin/mutex lock and there is no exclusive lock on
1064 * (For OpenBSD, a kthread is ok. For FreeBSD, I think it is ok
1065 * to do this from a callout, since the spin locks work. For
1066 * Darwin, I'm not sure what will work correctly yet.)
1067 * Should be called once per second.
1070 nfsrv_servertimer(void)
1072 struct nfsclient *clp, *nclp;
1073 struct nfsstate *stp, *nstp;
1077 * Make sure nfsboottime is set. This is used by V3 as well
1078 * as V4. Note that nfsboottime is not nfsrvboottime, which is
1079 * only used by the V4 server for leases.
1081 if (nfsboottime.tv_sec == 0)
1082 NFSSETBOOTTIME(nfsboottime);
1085 * If server hasn't started yet, just return.
1088 if (nfsrv_stablefirst.nsf_eograce == 0) {
1092 if (!(nfsrv_stablefirst.nsf_flags & NFSNSF_UPDATEDONE)) {
1093 if (!(nfsrv_stablefirst.nsf_flags & NFSNSF_GRACEOVER) &&
1094 NFSD_MONOSEC > nfsrv_stablefirst.nsf_eograce)
1095 nfsrv_stablefirst.nsf_flags |=
1096 (NFSNSF_GRACEOVER | NFSNSF_NEEDLOCK);
1102 * Try and get a reference count on the nfsv4rootfs_lock so that
1103 * no nfsd thread can acquire an exclusive lock on it before this
1104 * call is done. If it is already exclusively locked, just return.
1106 NFSLOCKV4ROOTMUTEX();
1107 got_ref = nfsv4_getref_nonblock(&nfsv4rootfs_lock);
1108 NFSUNLOCKV4ROOTMUTEX();
1115 * For each client...
1117 for (i = 0; i < nfsrv_clienthashsize; i++) {
1118 clp = LIST_FIRST(&nfsclienthash[i]);
1119 while (clp != LIST_END(&nfsclienthash[i])) {
1120 nclp = LIST_NEXT(clp, lc_hash);
1121 if (!(clp->lc_flags & LCL_EXPIREIT)) {
1122 if (((clp->lc_expiry + NFSRV_STALELEASE) < NFSD_MONOSEC
1123 && ((LIST_EMPTY(&clp->lc_deleg)
1124 && LIST_EMPTY(&clp->lc_open)) ||
1125 nfsrv_clients > nfsrv_clienthighwater)) ||
1126 (clp->lc_expiry + NFSRV_MOULDYLEASE) < NFSD_MONOSEC ||
1127 (clp->lc_expiry < NFSD_MONOSEC &&
1128 (nfsrv_openpluslock * 10 / 9) > nfsrv_v4statelimit)) {
1130 * Lease has expired several nfsrv_lease times ago:
1132 * - no state is associated with it
1134 * - above high water mark for number of clients
1135 * (nfsrv_clienthighwater should be large enough
1136 * that this only occurs when clients fail to
1137 * use the same nfs_client_id4.id. Maybe somewhat
1138 * higher that the maximum number of clients that
1139 * will mount this server?)
1141 * Lease has expired a very long time ago
1143 * Lease has expired PLUS the number of opens + locks
1144 * has exceeded 90% of capacity
1146 * --> Mark for expiry. The actual expiry will be done
1147 * by an nfsd sometime soon.
1149 clp->lc_flags |= LCL_EXPIREIT;
1150 nfsrv_stablefirst.nsf_flags |=
1151 (NFSNSF_NEEDLOCK | NFSNSF_EXPIREDCLIENT);
1154 * If there are no opens, increment no open tick cnt
1155 * If time exceeds NFSNOOPEN, mark it to be thrown away
1156 * otherwise, if there is an open, reset no open time
1157 * Hopefully, this will avoid excessive re-creation
1158 * of open owners and subsequent open confirms.
1160 stp = LIST_FIRST(&clp->lc_open);
1161 while (stp != LIST_END(&clp->lc_open)) {
1162 nstp = LIST_NEXT(stp, ls_list);
1163 if (LIST_EMPTY(&stp->ls_open)) {
1165 if (stp->ls_noopens > NFSNOOPEN ||
1166 (nfsrv_openpluslock * 2) >
1168 nfsrv_stablefirst.nsf_flags |=
1171 stp->ls_noopens = 0;
1181 NFSLOCKV4ROOTMUTEX();
1182 nfsv4_relref(&nfsv4rootfs_lock);
1183 NFSUNLOCKV4ROOTMUTEX();
1187 * The following set of functions free up the various data structures.
1190 * Clear out all open/lock state related to this nfsclient.
1191 * Caller must hold an exclusive lock on nfsv4rootfs_lock, so that
1192 * there are no other active nfsd threads.
1195 nfsrv_cleanclient(struct nfsclient *clp, NFSPROC_T *p)
1197 struct nfsstate *stp, *nstp;
1198 struct nfsdsession *sep, *nsep;
1200 LIST_FOREACH_SAFE(stp, &clp->lc_open, ls_list, nstp)
1201 nfsrv_freeopenowner(stp, 1, p);
1202 if ((clp->lc_flags & LCL_ADMINREVOKED) == 0)
1203 LIST_FOREACH_SAFE(sep, &clp->lc_session, sess_list, nsep)
1204 (void)nfsrv_freesession(sep, NULL);
1208 * Free a client that has been cleaned. It should also already have been
1209 * removed from the lists.
1210 * (Just to be safe w.r.t. newnfs_disconnect(), call this function when
1211 * softclock interrupts are enabled.)
1214 nfsrv_zapclient(struct nfsclient *clp, NFSPROC_T *p)
1218 if ((clp->lc_flags & (LCL_GSS | LCL_CALLBACKSON)) ==
1219 (LCL_GSS | LCL_CALLBACKSON) &&
1220 (clp->lc_hand.nfsh_flag & NFSG_COMPLETE) &&
1221 clp->lc_handlelen > 0) {
1222 clp->lc_hand.nfsh_flag &= ~NFSG_COMPLETE;
1223 clp->lc_hand.nfsh_flag |= NFSG_DESTROYED;
1224 (void) nfsrv_docallback(clp, NFSV4PROC_CBNULL,
1225 NULL, 0, NULL, NULL, NULL, p);
1228 newnfs_disconnect(&clp->lc_req);
1229 NFSSOCKADDRFREE(clp->lc_req.nr_nam);
1230 NFSFREEMUTEX(&clp->lc_req.nr_mtx);
1231 free(clp->lc_stateid, M_NFSDCLIENT);
1232 free(clp, M_NFSDCLIENT);
1234 nfsstatsv1.srvclients--;
1235 nfsrv_openpluslock--;
1241 * Free a list of delegation state structures.
1242 * (This function will also free all nfslockfile structures that no
1243 * longer have associated state.)
1246 nfsrv_freedeleglist(struct nfsstatehead *sthp)
1248 struct nfsstate *stp, *nstp;
1250 LIST_FOREACH_SAFE(stp, sthp, ls_list, nstp) {
1251 nfsrv_freedeleg(stp);
1257 * Free up a delegation.
1260 nfsrv_freedeleg(struct nfsstate *stp)
1262 struct nfslockfile *lfp;
1264 LIST_REMOVE(stp, ls_hash);
1265 LIST_REMOVE(stp, ls_list);
1266 LIST_REMOVE(stp, ls_file);
1267 if ((stp->ls_flags & NFSLCK_DELEGWRITE) != 0)
1268 nfsrv_writedelegcnt--;
1270 if (LIST_EMPTY(&lfp->lf_open) &&
1271 LIST_EMPTY(&lfp->lf_lock) && LIST_EMPTY(&lfp->lf_deleg) &&
1272 LIST_EMPTY(&lfp->lf_locallock) && LIST_EMPTY(&lfp->lf_rollback) &&
1273 lfp->lf_usecount == 0 &&
1274 nfsv4_testlock(&lfp->lf_locallock_lck) == 0)
1275 nfsrv_freenfslockfile(lfp);
1276 FREE((caddr_t)stp, M_NFSDSTATE);
1277 nfsstatsv1.srvdelegates--;
1278 nfsrv_openpluslock--;
1279 nfsrv_delegatecnt--;
1283 * This function frees an open owner and all associated opens.
1286 nfsrv_freeopenowner(struct nfsstate *stp, int cansleep, NFSPROC_T *p)
1288 struct nfsstate *nstp, *tstp;
1290 LIST_REMOVE(stp, ls_list);
1292 * Now, free all associated opens.
1294 nstp = LIST_FIRST(&stp->ls_open);
1295 while (nstp != LIST_END(&stp->ls_open)) {
1297 nstp = LIST_NEXT(nstp, ls_list);
1298 (void) nfsrv_freeopen(tstp, NULL, cansleep, p);
1301 nfsrvd_derefcache(stp->ls_op);
1302 FREE((caddr_t)stp, M_NFSDSTATE);
1303 nfsstatsv1.srvopenowners--;
1304 nfsrv_openpluslock--;
1308 * This function frees an open (nfsstate open structure) with all associated
1309 * lock_owners and locks. It also frees the nfslockfile structure iff there
1310 * are no other opens on the file.
1311 * Returns 1 if it free'd the nfslockfile, 0 otherwise.
1314 nfsrv_freeopen(struct nfsstate *stp, vnode_t vp, int cansleep, NFSPROC_T *p)
1316 struct nfsstate *nstp, *tstp;
1317 struct nfslockfile *lfp;
1320 LIST_REMOVE(stp, ls_hash);
1321 LIST_REMOVE(stp, ls_list);
1322 LIST_REMOVE(stp, ls_file);
1326 * Now, free all lockowners associated with this open.
1328 LIST_FOREACH_SAFE(tstp, &stp->ls_open, ls_list, nstp)
1329 nfsrv_freelockowner(tstp, vp, cansleep, p);
1332 * The nfslockfile is freed here if there are no locks
1333 * associated with the open.
1334 * If there are locks associated with the open, the
1335 * nfslockfile structure can be freed via nfsrv_freelockowner().
1336 * Acquire the state mutex to avoid races with calls to
1337 * nfsrv_getlockfile().
1341 if (lfp != NULL && LIST_EMPTY(&lfp->lf_open) &&
1342 LIST_EMPTY(&lfp->lf_deleg) && LIST_EMPTY(&lfp->lf_lock) &&
1343 LIST_EMPTY(&lfp->lf_locallock) && LIST_EMPTY(&lfp->lf_rollback) &&
1344 lfp->lf_usecount == 0 &&
1345 (cansleep != 0 || nfsv4_testlock(&lfp->lf_locallock_lck) == 0)) {
1346 nfsrv_freenfslockfile(lfp);
1352 FREE((caddr_t)stp, M_NFSDSTATE);
1353 nfsstatsv1.srvopens--;
1354 nfsrv_openpluslock--;
1359 * Frees a lockowner and all associated locks.
1362 nfsrv_freelockowner(struct nfsstate *stp, vnode_t vp, int cansleep,
1366 LIST_REMOVE(stp, ls_hash);
1367 LIST_REMOVE(stp, ls_list);
1368 nfsrv_freeallnfslocks(stp, vp, cansleep, p);
1370 nfsrvd_derefcache(stp->ls_op);
1371 FREE((caddr_t)stp, M_NFSDSTATE);
1372 nfsstatsv1.srvlockowners--;
1373 nfsrv_openpluslock--;
1377 * Free all the nfs locks on a lockowner.
1380 nfsrv_freeallnfslocks(struct nfsstate *stp, vnode_t vp, int cansleep,
1383 struct nfslock *lop, *nlop;
1384 struct nfsrollback *rlp, *nrlp;
1385 struct nfslockfile *lfp = NULL;
1388 uint64_t first, end;
1391 ASSERT_VOP_UNLOCKED(vp, "nfsrv_freeallnfslocks: vnode locked");
1392 lop = LIST_FIRST(&stp->ls_lock);
1393 while (lop != LIST_END(&stp->ls_lock)) {
1394 nlop = LIST_NEXT(lop, lo_lckowner);
1396 * Since all locks should be for the same file, lfp should
1401 else if (lfp != lop->lo_lfp)
1402 panic("allnfslocks");
1404 * If vp is NULL and cansleep != 0, a vnode must be acquired
1405 * from the file handle. This only occurs when called from
1406 * nfsrv_cleanclient().
1409 if (nfsrv_dolocallocks == 0)
1411 else if (vp == NULL && cansleep != 0) {
1412 tvp = nfsvno_getvp(&lfp->lf_fh);
1413 NFSVOPUNLOCK(tvp, 0);
1422 first = lop->lo_first;
1424 nfsrv_freenfslock(lop);
1425 nfsrv_localunlock(tvp, lfp, first, end, p);
1426 LIST_FOREACH_SAFE(rlp, &lfp->lf_rollback, rlck_list,
1428 free(rlp, M_NFSDROLLBACK);
1429 LIST_INIT(&lfp->lf_rollback);
1431 nfsrv_freenfslock(lop);
1434 if (vp == NULL && tvp != NULL)
1439 * Free an nfslock structure.
1442 nfsrv_freenfslock(struct nfslock *lop)
1445 if (lop->lo_lckfile.le_prev != NULL) {
1446 LIST_REMOVE(lop, lo_lckfile);
1447 nfsstatsv1.srvlocks--;
1448 nfsrv_openpluslock--;
1450 LIST_REMOVE(lop, lo_lckowner);
1451 FREE((caddr_t)lop, M_NFSDLOCK);
1455 * This function frees an nfslockfile structure.
1458 nfsrv_freenfslockfile(struct nfslockfile *lfp)
1461 LIST_REMOVE(lfp, lf_hash);
1462 FREE((caddr_t)lfp, M_NFSDLOCKFILE);
1466 * This function looks up an nfsstate structure via stateid.
1469 nfsrv_getstate(struct nfsclient *clp, nfsv4stateid_t *stateidp, __unused u_int32_t flags,
1470 struct nfsstate **stpp)
1472 struct nfsstate *stp;
1473 struct nfsstatehead *hp;
1477 hp = NFSSTATEHASH(clp, *stateidp);
1478 LIST_FOREACH(stp, hp, ls_hash) {
1479 if (!NFSBCMP(stp->ls_stateid.other, stateidp->other,
1485 * If no state id in list, return NFSERR_BADSTATEID.
1487 if (stp == LIST_END(hp)) {
1488 error = NFSERR_BADSTATEID;
1499 * This function gets an nfsstate structure via owner string.
1502 nfsrv_getowner(struct nfsstatehead *hp, struct nfsstate *new_stp,
1503 struct nfsstate **stpp)
1505 struct nfsstate *stp;
1508 LIST_FOREACH(stp, hp, ls_list) {
1509 if (new_stp->ls_ownerlen == stp->ls_ownerlen &&
1510 !NFSBCMP(new_stp->ls_owner,stp->ls_owner,stp->ls_ownerlen)) {
1518 * Lock control function called to update lock status.
1519 * Returns 0 upon success, -1 if there is no lock and the flags indicate
1520 * that one isn't to be created and an NFSERR_xxx for other errors.
1521 * The structures new_stp and new_lop are passed in as pointers that should
1522 * be set to NULL if the structure is used and shouldn't be free'd.
1523 * For the NFSLCK_TEST and NFSLCK_CHECK cases, the structures are
1524 * never used and can safely be allocated on the stack. For all other
1525 * cases, *new_stpp and *new_lopp should be malloc'd before the call,
1526 * in case they are used.
1529 nfsrv_lockctrl(vnode_t vp, struct nfsstate **new_stpp,
1530 struct nfslock **new_lopp, struct nfslockconflict *cfp,
1531 nfsquad_t clientid, nfsv4stateid_t *stateidp,
1532 __unused struct nfsexstuff *exp,
1533 struct nfsrv_descript *nd, NFSPROC_T *p)
1535 struct nfslock *lop;
1536 struct nfsstate *new_stp = *new_stpp;
1537 struct nfslock *new_lop = *new_lopp;
1538 struct nfsstate *tstp, *mystp, *nstp;
1540 struct nfslockfile *lfp;
1541 struct nfslock *other_lop = NULL;
1542 struct nfsstate *stp, *lckstp = NULL;
1543 struct nfsclient *clp = NULL;
1545 int error = 0, haslock = 0, ret, reterr;
1546 int getlckret, delegation = 0, filestruct_locked, vnode_unlocked = 0;
1548 uint64_t first, end;
1549 uint32_t lock_flags;
1551 if (new_stp->ls_flags & (NFSLCK_CHECK | NFSLCK_SETATTR)) {
1553 * Note the special cases of "all 1s" or "all 0s" stateids and
1554 * let reads with all 1s go ahead.
1556 if (new_stp->ls_stateid.seqid == 0x0 &&
1557 new_stp->ls_stateid.other[0] == 0x0 &&
1558 new_stp->ls_stateid.other[1] == 0x0 &&
1559 new_stp->ls_stateid.other[2] == 0x0)
1561 else if (new_stp->ls_stateid.seqid == 0xffffffff &&
1562 new_stp->ls_stateid.other[0] == 0xffffffff &&
1563 new_stp->ls_stateid.other[1] == 0xffffffff &&
1564 new_stp->ls_stateid.other[2] == 0xffffffff)
1569 * Check for restart conditions (client and server).
1571 error = nfsrv_checkrestart(clientid, new_stp->ls_flags,
1572 &new_stp->ls_stateid, specialid);
1577 * Check for state resource limit exceeded.
1579 if ((new_stp->ls_flags & NFSLCK_LOCK) &&
1580 nfsrv_openpluslock > nfsrv_v4statelimit) {
1581 error = NFSERR_RESOURCE;
1586 * For the lock case, get another nfslock structure,
1587 * just in case we need it.
1588 * Malloc now, before we start sifting through the linked lists,
1589 * in case we have to wait for memory.
1592 if (new_stp->ls_flags & NFSLCK_LOCK)
1593 MALLOC(other_lop, struct nfslock *, sizeof (struct nfslock),
1594 M_NFSDLOCK, M_WAITOK);
1595 filestruct_locked = 0;
1600 * Get the lockfile structure for CFH now, so we can do a sanity
1601 * check against the stateid, before incrementing the seqid#, since
1602 * we want to return NFSERR_BADSTATEID on failure and the seqid#
1603 * shouldn't be incremented for this case.
1604 * If nfsrv_getlockfile() returns -1, it means "not found", which
1605 * will be handled later.
1606 * If we are doing Lock/LockU and local locking is enabled, sleep
1607 * lock the nfslockfile structure.
1609 getlckret = nfsrv_getlockfh(vp, new_stp->ls_flags, NULL, &nfh, p);
1611 if (getlckret == 0) {
1612 if ((new_stp->ls_flags & (NFSLCK_LOCK | NFSLCK_UNLOCK)) != 0 &&
1613 nfsrv_dolocallocks != 0 && nd->nd_repstat == 0) {
1614 getlckret = nfsrv_getlockfile(new_stp->ls_flags, NULL,
1617 filestruct_locked = 1;
1619 getlckret = nfsrv_getlockfile(new_stp->ls_flags, NULL,
1622 if (getlckret != 0 && getlckret != -1)
1625 if (filestruct_locked != 0) {
1626 LIST_INIT(&lfp->lf_rollback);
1627 if ((new_stp->ls_flags & NFSLCK_LOCK)) {
1629 * For local locking, do the advisory locking now, so
1630 * that any conflict can be detected. A failure later
1631 * can be rolled back locally. If an error is returned,
1632 * struct nfslockfile has been unlocked and any local
1633 * locking rolled back.
1636 if (vnode_unlocked == 0) {
1637 ASSERT_VOP_ELOCKED(vp, "nfsrv_lockctrl1");
1639 NFSVOPUNLOCK(vp, 0);
1641 reterr = nfsrv_locallock(vp, lfp,
1642 (new_lop->lo_flags & (NFSLCK_READ | NFSLCK_WRITE)),
1643 new_lop->lo_first, new_lop->lo_end, cfp, p);
1648 if (specialid == 0) {
1649 if (new_stp->ls_flags & NFSLCK_TEST) {
1651 * RFC 3530 does not list LockT as an op that renews a
1652 * lease, but the consensus seems to be that it is ok
1653 * for a server to do so.
1655 error = nfsrv_getclient(clientid, CLOPS_RENEW, &clp, NULL,
1656 (nfsquad_t)((u_quad_t)0), 0, nd, p);
1659 * Since NFSERR_EXPIRED, NFSERR_ADMINREVOKED are not valid
1660 * error returns for LockT, just go ahead and test for a lock,
1661 * since there are no locks for this client, but other locks
1662 * can conflict. (ie. same client will always be false)
1664 if (error == NFSERR_EXPIRED || error == NFSERR_ADMINREVOKED)
1668 error = nfsrv_getclient(clientid, CLOPS_RENEW, &clp, NULL,
1669 (nfsquad_t)((u_quad_t)0), 0, nd, p);
1672 * Look up the stateid
1674 error = nfsrv_getstate(clp, &new_stp->ls_stateid,
1675 new_stp->ls_flags, &stp);
1677 * do some sanity checks for an unconfirmed open or a
1678 * stateid that refers to the wrong file, for an open stateid
1680 if (error == 0 && (stp->ls_flags & NFSLCK_OPEN) &&
1681 ((stp->ls_openowner->ls_flags & NFSLCK_NEEDSCONFIRM) ||
1682 (getlckret == 0 && stp->ls_lfp != lfp))){
1684 * NFSLCK_SETATTR should return OK rather than NFSERR_BADSTATEID
1685 * The only exception is using SETATTR with SIZE.
1687 if ((new_stp->ls_flags &
1688 (NFSLCK_SETATTR | NFSLCK_CHECK)) != NFSLCK_SETATTR)
1689 error = NFSERR_BADSTATEID;
1693 (stp->ls_flags & (NFSLCK_DELEGREAD | NFSLCK_DELEGWRITE)) &&
1694 getlckret == 0 && stp->ls_lfp != lfp)
1695 error = NFSERR_BADSTATEID;
1698 * If the lockowner stateid doesn't refer to the same file,
1699 * I believe that is considered ok, since some clients will
1700 * only create a single lockowner and use that for all locks
1702 * For now, log it as a diagnostic, instead of considering it
1705 if (error == 0 && (stp->ls_flags &
1706 (NFSLCK_OPEN | NFSLCK_DELEGREAD | NFSLCK_DELEGWRITE)) == 0 &&
1707 getlckret == 0 && stp->ls_lfp != lfp) {
1709 printf("Got a lock statid for different file open\n");
1712 error = NFSERR_BADSTATEID;
1717 if (new_stp->ls_flags & NFSLCK_OPENTOLOCK) {
1719 * If haslock set, we've already checked the seqid.
1722 if (stp->ls_flags & NFSLCK_OPEN)
1723 error = nfsrv_checkseqid(nd, new_stp->ls_seq,
1724 stp->ls_openowner, new_stp->ls_op);
1726 error = NFSERR_BADSTATEID;
1729 nfsrv_getowner(&stp->ls_open, new_stp, &lckstp);
1732 * I believe this should be an error, but it
1733 * isn't obvious what NFSERR_xxx would be
1734 * appropriate, so I'll use NFSERR_INVAL for now.
1736 error = NFSERR_INVAL;
1739 } else if (new_stp->ls_flags&(NFSLCK_LOCK|NFSLCK_UNLOCK)) {
1741 * If haslock set, ditto above.
1744 if (stp->ls_flags & NFSLCK_OPEN)
1745 error = NFSERR_BADSTATEID;
1747 error = nfsrv_checkseqid(nd, new_stp->ls_seq,
1748 stp, new_stp->ls_op);
1756 * If the seqid part of the stateid isn't the same, return
1757 * NFSERR_OLDSTATEID for cases other than I/O Ops.
1758 * For I/O Ops, only return NFSERR_OLDSTATEID if
1759 * nfsrv_returnoldstateid is set. (The consensus on the email
1760 * list was that most clients would prefer to not receive
1761 * NFSERR_OLDSTATEID for I/O Ops, but the RFC suggests that that
1762 * is what will happen, so I use the nfsrv_returnoldstateid to
1763 * allow for either server configuration.)
1765 if (!error && stp->ls_stateid.seqid!=new_stp->ls_stateid.seqid &&
1766 (((nd->nd_flag & ND_NFSV41) == 0 &&
1767 (!(new_stp->ls_flags & NFSLCK_CHECK) ||
1768 nfsrv_returnoldstateid)) ||
1769 ((nd->nd_flag & ND_NFSV41) != 0 &&
1770 new_stp->ls_stateid.seqid != 0)))
1771 error = NFSERR_OLDSTATEID;
1776 * Now we can check for grace.
1779 error = nfsrv_checkgrace(nd, clp, new_stp->ls_flags);
1780 if ((new_stp->ls_flags & NFSLCK_RECLAIM) && !error &&
1781 nfsrv_checkstable(clp))
1782 error = NFSERR_NOGRACE;
1784 * If we successfully Reclaimed state, note that.
1786 if ((new_stp->ls_flags & NFSLCK_RECLAIM) && !error)
1787 nfsrv_markstable(clp);
1790 * At this point, either error == NFSERR_BADSTATEID or the
1791 * seqid# has been updated, so we can return any error.
1792 * If error == 0, there may be an error in:
1793 * nd_repstat - Set by the calling function.
1794 * reterr - Set above, if getting the nfslockfile structure
1795 * or acquiring the local lock failed.
1796 * (If both of these are set, nd_repstat should probably be
1797 * returned, since that error was detected before this
1800 if (error != 0 || nd->nd_repstat != 0 || reterr != 0) {
1802 if (nd->nd_repstat != 0)
1803 error = nd->nd_repstat;
1807 if (filestruct_locked != 0) {
1808 /* Roll back local locks. */
1810 if (vnode_unlocked == 0) {
1811 ASSERT_VOP_ELOCKED(vp, "nfsrv_lockctrl2");
1813 NFSVOPUNLOCK(vp, 0);
1815 nfsrv_locallock_rollback(vp, lfp, p);
1817 nfsrv_unlocklf(lfp);
1824 * Check the nfsrv_getlockfile return.
1825 * Returned -1 if no structure found.
1827 if (getlckret == -1) {
1828 error = NFSERR_EXPIRED;
1830 * Called from lockt, so no lock is OK.
1832 if (new_stp->ls_flags & NFSLCK_TEST) {
1834 } else if (new_stp->ls_flags &
1835 (NFSLCK_CHECK | NFSLCK_SETATTR)) {
1837 * Called to check for a lock, OK if the stateid is all
1838 * 1s or all 0s, but there should be an nfsstate
1840 * (ie. If there is no open, I'll assume no share
1846 error = NFSERR_BADSTATEID;
1853 * For NFSLCK_CHECK and NFSLCK_LOCK, test for a share conflict.
1854 * For NFSLCK_CHECK, allow a read if write access is granted,
1855 * but check for a deny. For NFSLCK_LOCK, require correct access,
1856 * which implies a conflicting deny can't exist.
1858 if (new_stp->ls_flags & (NFSLCK_CHECK | NFSLCK_LOCK)) {
1860 * Four kinds of state id:
1861 * - specialid (all 0s or all 1s), only for NFSLCK_CHECK
1862 * - stateid for an open
1863 * - stateid for a delegation
1864 * - stateid for a lock owner
1867 if (stp->ls_flags & (NFSLCK_DELEGREAD | NFSLCK_DELEGWRITE)) {
1870 nfsrv_delaydelegtimeout(stp);
1871 } else if (stp->ls_flags & NFSLCK_OPEN) {
1874 mystp = stp->ls_openstp;
1877 * If locking or checking, require correct access
1880 if (((new_stp->ls_flags & NFSLCK_LOCK) &&
1881 !((new_lop->lo_flags >> NFSLCK_LOCKSHIFT) &
1882 mystp->ls_flags & NFSLCK_ACCESSBITS)) ||
1883 ((new_stp->ls_flags & (NFSLCK_CHECK|NFSLCK_READACCESS)) ==
1884 (NFSLCK_CHECK | NFSLCK_READACCESS) &&
1885 !(mystp->ls_flags & NFSLCK_READACCESS) &&
1886 nfsrv_allowreadforwriteopen == 0) ||
1887 ((new_stp->ls_flags & (NFSLCK_CHECK|NFSLCK_WRITEACCESS)) ==
1888 (NFSLCK_CHECK | NFSLCK_WRITEACCESS) &&
1889 !(mystp->ls_flags & NFSLCK_WRITEACCESS))) {
1890 if (filestruct_locked != 0) {
1891 /* Roll back local locks. */
1893 if (vnode_unlocked == 0) {
1894 ASSERT_VOP_ELOCKED(vp,
1897 NFSVOPUNLOCK(vp, 0);
1899 nfsrv_locallock_rollback(vp, lfp, p);
1901 nfsrv_unlocklf(lfp);
1904 error = NFSERR_OPENMODE;
1909 if ((new_stp->ls_flags & NFSLCK_CHECK) && !delegation) {
1911 * Check for a conflicting deny bit.
1913 LIST_FOREACH(tstp, &lfp->lf_open, ls_file) {
1914 if (tstp != mystp) {
1915 bits = tstp->ls_flags;
1916 bits >>= NFSLCK_SHIFT;
1917 if (new_stp->ls_flags & bits & NFSLCK_ACCESSBITS) {
1918 KASSERT(vnode_unlocked == 0,
1919 ("nfsrv_lockctrl: vnode unlocked1"));
1920 ret = nfsrv_clientconflict(tstp->ls_clp, &haslock,
1924 * nfsrv_clientconflict unlocks state
1925 * when it returns non-zero.
1933 error = NFSERR_PERM;
1935 error = NFSERR_OPENMODE;
1941 /* We're outta here */
1948 * For setattr, just get rid of all the Delegations for other clients.
1950 if (new_stp->ls_flags & NFSLCK_SETATTR) {
1951 KASSERT(vnode_unlocked == 0,
1952 ("nfsrv_lockctrl: vnode unlocked2"));
1953 ret = nfsrv_cleandeleg(vp, lfp, clp, &haslock, p);
1956 * nfsrv_cleandeleg() unlocks state when it
1966 if (!(new_stp->ls_flags & NFSLCK_CHECK) ||
1967 (LIST_EMPTY(&lfp->lf_open) && LIST_EMPTY(&lfp->lf_lock) &&
1968 LIST_EMPTY(&lfp->lf_deleg))) {
1975 * Check for a conflicting delegation. If one is found, call
1976 * nfsrv_delegconflict() to handle it. If the v4root lock hasn't
1977 * been set yet, it will get the lock. Otherwise, it will recall
1978 * the delegation. Then, we try try again...
1979 * I currently believe the conflict algorithm to be:
1980 * For Lock Ops (Lock/LockT/LockU)
1981 * - there is a conflict iff a different client has a write delegation
1982 * For Reading (Read Op)
1983 * - there is a conflict iff a different client has a write delegation
1984 * (the specialids are always a different client)
1985 * For Writing (Write/Setattr of size)
1986 * - there is a conflict if a different client has any delegation
1987 * - there is a conflict if the same client has a read delegation
1988 * (I don't understand why this isn't allowed, but that seems to be
1989 * the current consensus?)
1991 tstp = LIST_FIRST(&lfp->lf_deleg);
1992 while (tstp != LIST_END(&lfp->lf_deleg)) {
1993 nstp = LIST_NEXT(tstp, ls_file);
1994 if ((((new_stp->ls_flags&(NFSLCK_LOCK|NFSLCK_UNLOCK|NFSLCK_TEST))||
1995 ((new_stp->ls_flags & NFSLCK_CHECK) &&
1996 (new_lop->lo_flags & NFSLCK_READ))) &&
1997 clp != tstp->ls_clp &&
1998 (tstp->ls_flags & NFSLCK_DELEGWRITE)) ||
1999 ((new_stp->ls_flags & NFSLCK_CHECK) &&
2000 (new_lop->lo_flags & NFSLCK_WRITE) &&
2001 (clp != tstp->ls_clp ||
2002 (tstp->ls_flags & NFSLCK_DELEGREAD)))) {
2004 if (filestruct_locked != 0) {
2005 /* Roll back local locks. */
2007 if (vnode_unlocked == 0) {
2008 ASSERT_VOP_ELOCKED(vp, "nfsrv_lockctrl4");
2009 NFSVOPUNLOCK(vp, 0);
2011 nfsrv_locallock_rollback(vp, lfp, p);
2013 nfsrv_unlocklf(lfp);
2015 NFSVOPLOCK(vp, LK_EXCLUSIVE | LK_RETRY);
2017 if ((vp->v_iflag & VI_DOOMED) != 0)
2018 ret = NFSERR_SERVERFAULT;
2022 ret = nfsrv_delegconflict(tstp, &haslock, p, vp);
2025 * nfsrv_delegconflict unlocks state when it
2026 * returns non-zero, which it always does.
2029 FREE((caddr_t)other_lop, M_NFSDLOCK);
2039 /* Never gets here. */
2045 * Handle the unlock case by calling nfsrv_updatelock().
2046 * (Should I have done some access checking above for unlock? For now,
2047 * just let it happen.)
2049 if (new_stp->ls_flags & NFSLCK_UNLOCK) {
2050 first = new_lop->lo_first;
2051 end = new_lop->lo_end;
2052 nfsrv_updatelock(stp, new_lopp, &other_lop, lfp);
2053 stateidp->seqid = ++(stp->ls_stateid.seqid);
2054 if ((nd->nd_flag & ND_NFSV41) != 0 && stateidp->seqid == 0)
2055 stateidp->seqid = stp->ls_stateid.seqid = 1;
2056 stateidp->other[0] = stp->ls_stateid.other[0];
2057 stateidp->other[1] = stp->ls_stateid.other[1];
2058 stateidp->other[2] = stp->ls_stateid.other[2];
2059 if (filestruct_locked != 0) {
2061 if (vnode_unlocked == 0) {
2062 ASSERT_VOP_ELOCKED(vp, "nfsrv_lockctrl5");
2064 NFSVOPUNLOCK(vp, 0);
2066 /* Update the local locks. */
2067 nfsrv_localunlock(vp, lfp, first, end, p);
2069 nfsrv_unlocklf(lfp);
2076 * Search for a conflicting lock. A lock conflicts if:
2077 * - the lock range overlaps and
2078 * - at least one lock is a write lock and
2079 * - it is not owned by the same lock owner
2082 LIST_FOREACH(lop, &lfp->lf_lock, lo_lckfile) {
2083 if (new_lop->lo_end > lop->lo_first &&
2084 new_lop->lo_first < lop->lo_end &&
2085 (new_lop->lo_flags == NFSLCK_WRITE ||
2086 lop->lo_flags == NFSLCK_WRITE) &&
2087 lckstp != lop->lo_stp &&
2088 (clp != lop->lo_stp->ls_clp ||
2089 lckstp->ls_ownerlen != lop->lo_stp->ls_ownerlen ||
2090 NFSBCMP(lckstp->ls_owner, lop->lo_stp->ls_owner,
2091 lckstp->ls_ownerlen))) {
2093 FREE((caddr_t)other_lop, M_NFSDLOCK);
2096 if (vnode_unlocked != 0)
2097 ret = nfsrv_clientconflict(lop->lo_stp->ls_clp, &haslock,
2100 ret = nfsrv_clientconflict(lop->lo_stp->ls_clp, &haslock,
2103 if (filestruct_locked != 0) {
2104 if (vnode_unlocked == 0) {
2105 ASSERT_VOP_ELOCKED(vp, "nfsrv_lockctrl6");
2106 NFSVOPUNLOCK(vp, 0);
2108 /* Roll back local locks. */
2109 nfsrv_locallock_rollback(vp, lfp, p);
2111 nfsrv_unlocklf(lfp);
2113 NFSVOPLOCK(vp, LK_EXCLUSIVE | LK_RETRY);
2115 if ((vp->v_iflag & VI_DOOMED) != 0) {
2116 error = NFSERR_SERVERFAULT;
2121 * nfsrv_clientconflict() unlocks state when it
2128 * Found a conflicting lock, so record the conflict and
2131 if (cfp != NULL && ret == 0) {
2132 cfp->cl_clientid.lval[0]=lop->lo_stp->ls_stateid.other[0];
2133 cfp->cl_clientid.lval[1]=lop->lo_stp->ls_stateid.other[1];
2134 cfp->cl_first = lop->lo_first;
2135 cfp->cl_end = lop->lo_end;
2136 cfp->cl_flags = lop->lo_flags;
2137 cfp->cl_ownerlen = lop->lo_stp->ls_ownerlen;
2138 NFSBCOPY(lop->lo_stp->ls_owner, cfp->cl_owner,
2142 error = NFSERR_PERM;
2143 else if (new_stp->ls_flags & NFSLCK_RECLAIM)
2144 error = NFSERR_RECLAIMCONFLICT;
2145 else if (new_stp->ls_flags & NFSLCK_CHECK)
2146 error = NFSERR_LOCKED;
2148 error = NFSERR_DENIED;
2149 if (filestruct_locked != 0 && ret == 0) {
2150 /* Roll back local locks. */
2152 if (vnode_unlocked == 0) {
2153 ASSERT_VOP_ELOCKED(vp, "nfsrv_lockctrl7");
2155 NFSVOPUNLOCK(vp, 0);
2157 nfsrv_locallock_rollback(vp, lfp, p);
2159 nfsrv_unlocklf(lfp);
2169 * We only get here if there was no lock that conflicted.
2171 if (new_stp->ls_flags & (NFSLCK_TEST | NFSLCK_CHECK)) {
2177 * We only get here when we are creating or modifying a lock.
2178 * There are two variants:
2179 * - exist_lock_owner where lock_owner exists
2180 * - open_to_lock_owner with new lock_owner
2182 first = new_lop->lo_first;
2183 end = new_lop->lo_end;
2184 lock_flags = new_lop->lo_flags;
2185 if (!(new_stp->ls_flags & NFSLCK_OPENTOLOCK)) {
2186 nfsrv_updatelock(lckstp, new_lopp, &other_lop, lfp);
2187 stateidp->seqid = ++(lckstp->ls_stateid.seqid);
2188 if ((nd->nd_flag & ND_NFSV41) != 0 && stateidp->seqid == 0)
2189 stateidp->seqid = lckstp->ls_stateid.seqid = 1;
2190 stateidp->other[0] = lckstp->ls_stateid.other[0];
2191 stateidp->other[1] = lckstp->ls_stateid.other[1];
2192 stateidp->other[2] = lckstp->ls_stateid.other[2];
2195 * The new open_to_lock_owner case.
2196 * Link the new nfsstate into the lists.
2198 new_stp->ls_seq = new_stp->ls_opentolockseq;
2199 nfsrvd_refcache(new_stp->ls_op);
2200 stateidp->seqid = new_stp->ls_stateid.seqid = 1;
2201 stateidp->other[0] = new_stp->ls_stateid.other[0] =
2202 clp->lc_clientid.lval[0];
2203 stateidp->other[1] = new_stp->ls_stateid.other[1] =
2204 clp->lc_clientid.lval[1];
2205 stateidp->other[2] = new_stp->ls_stateid.other[2] =
2206 nfsrv_nextstateindex(clp);
2207 new_stp->ls_clp = clp;
2208 LIST_INIT(&new_stp->ls_lock);
2209 new_stp->ls_openstp = stp;
2210 new_stp->ls_lfp = lfp;
2211 nfsrv_insertlock(new_lop, (struct nfslock *)new_stp, new_stp,
2213 LIST_INSERT_HEAD(NFSSTATEHASH(clp, new_stp->ls_stateid),
2215 LIST_INSERT_HEAD(&stp->ls_open, new_stp, ls_list);
2218 nfsstatsv1.srvlockowners++;
2219 nfsrv_openpluslock++;
2221 if (filestruct_locked != 0) {
2223 nfsrv_locallock_commit(lfp, lock_flags, first, end);
2225 nfsrv_unlocklf(lfp);
2231 NFSLOCKV4ROOTMUTEX();
2232 nfsv4_unlock(&nfsv4rootfs_lock, 1);
2233 NFSUNLOCKV4ROOTMUTEX();
2235 if (vnode_unlocked != 0) {
2236 NFSVOPLOCK(vp, LK_EXCLUSIVE | LK_RETRY);
2237 if (error == 0 && (vp->v_iflag & VI_DOOMED) != 0)
2238 error = NFSERR_SERVERFAULT;
2241 FREE((caddr_t)other_lop, M_NFSDLOCK);
2242 NFSEXITCODE2(error, nd);
2247 * Check for state errors for Open.
2248 * repstat is passed back out as an error if more critical errors
2252 nfsrv_opencheck(nfsquad_t clientid, nfsv4stateid_t *stateidp,
2253 struct nfsstate *new_stp, vnode_t vp, struct nfsrv_descript *nd,
2254 NFSPROC_T *p, int repstat)
2256 struct nfsstate *stp, *nstp;
2257 struct nfsclient *clp;
2258 struct nfsstate *ownerstp;
2259 struct nfslockfile *lfp, *new_lfp;
2260 int error = 0, haslock = 0, ret, readonly = 0, getfhret = 0;
2262 if ((new_stp->ls_flags & NFSLCK_SHAREBITS) == NFSLCK_READACCESS)
2265 * Check for restart conditions (client and server).
2267 error = nfsrv_checkrestart(clientid, new_stp->ls_flags,
2268 &new_stp->ls_stateid, 0);
2273 * Check for state resource limit exceeded.
2274 * Technically this should be SMP protected, but the worst
2275 * case error is "out by one or two" on the count when it
2276 * returns NFSERR_RESOURCE and the limit is just a rather
2277 * arbitrary high water mark, so no harm is done.
2279 if (nfsrv_openpluslock > nfsrv_v4statelimit) {
2280 error = NFSERR_RESOURCE;
2285 MALLOC(new_lfp, struct nfslockfile *, sizeof (struct nfslockfile),
2286 M_NFSDLOCKFILE, M_WAITOK);
2288 getfhret = nfsrv_getlockfh(vp, new_stp->ls_flags, new_lfp,
2292 * Get the nfsclient structure.
2294 error = nfsrv_getclient(clientid, CLOPS_RENEW, &clp, NULL,
2295 (nfsquad_t)((u_quad_t)0), 0, nd, p);
2298 * Look up the open owner. See if it needs confirmation and
2299 * check the seq#, as required.
2302 nfsrv_getowner(&clp->lc_open, new_stp, &ownerstp);
2304 if (!error && ownerstp) {
2305 error = nfsrv_checkseqid(nd, new_stp->ls_seq, ownerstp,
2308 * If the OpenOwner hasn't been confirmed, assume the
2309 * old one was a replay and this one is ok.
2310 * See: RFC3530 Sec. 14.2.18.
2312 if (error == NFSERR_BADSEQID &&
2313 (ownerstp->ls_flags & NFSLCK_NEEDSCONFIRM))
2321 error = nfsrv_checkgrace(nd, clp, new_stp->ls_flags);
2322 if ((new_stp->ls_flags & NFSLCK_RECLAIM) && !error &&
2323 nfsrv_checkstable(clp))
2324 error = NFSERR_NOGRACE;
2327 * If none of the above errors occurred, let repstat be
2330 if (repstat && !error)
2335 NFSLOCKV4ROOTMUTEX();
2336 nfsv4_unlock(&nfsv4rootfs_lock, 1);
2337 NFSUNLOCKV4ROOTMUTEX();
2339 free((caddr_t)new_lfp, M_NFSDLOCKFILE);
2344 * If vp == NULL, the file doesn't exist yet, so return ok.
2345 * (This always happens on the first pass, so haslock must be 0.)
2349 FREE((caddr_t)new_lfp, M_NFSDLOCKFILE);
2354 * Get the structure for the underlying file.
2359 error = nfsrv_getlockfile(new_stp->ls_flags, &new_lfp, &lfp,
2362 FREE((caddr_t)new_lfp, M_NFSDLOCKFILE);
2366 NFSLOCKV4ROOTMUTEX();
2367 nfsv4_unlock(&nfsv4rootfs_lock, 1);
2368 NFSUNLOCKV4ROOTMUTEX();
2374 * Search for a conflicting open/share.
2376 if (new_stp->ls_flags & NFSLCK_DELEGCUR) {
2378 * For Delegate_Cur, search for the matching Delegation,
2379 * which indicates no conflict.
2380 * An old delegation should have been recovered by the
2381 * client doing a Claim_DELEGATE_Prev, so I won't let
2382 * it match and return NFSERR_EXPIRED. Should I let it
2385 LIST_FOREACH(stp, &lfp->lf_deleg, ls_file) {
2386 if (!(stp->ls_flags & NFSLCK_OLDDELEG) &&
2387 (((nd->nd_flag & ND_NFSV41) != 0 &&
2388 stateidp->seqid == 0) ||
2389 stateidp->seqid == stp->ls_stateid.seqid) &&
2390 !NFSBCMP(stateidp->other, stp->ls_stateid.other,
2394 if (stp == LIST_END(&lfp->lf_deleg) ||
2395 ((new_stp->ls_flags & NFSLCK_WRITEACCESS) &&
2396 (stp->ls_flags & NFSLCK_DELEGREAD))) {
2399 NFSLOCKV4ROOTMUTEX();
2400 nfsv4_unlock(&nfsv4rootfs_lock, 1);
2401 NFSUNLOCKV4ROOTMUTEX();
2403 error = NFSERR_EXPIRED;
2409 * Check for access/deny bit conflicts. I check for the same
2410 * owner as well, in case the client didn't bother.
2412 LIST_FOREACH(stp, &lfp->lf_open, ls_file) {
2413 if (!(new_stp->ls_flags & NFSLCK_DELEGCUR) &&
2414 (((new_stp->ls_flags & NFSLCK_ACCESSBITS) &
2415 ((stp->ls_flags>>NFSLCK_SHIFT) & NFSLCK_ACCESSBITS))||
2416 ((stp->ls_flags & NFSLCK_ACCESSBITS) &
2417 ((new_stp->ls_flags>>NFSLCK_SHIFT)&NFSLCK_ACCESSBITS)))){
2418 ret = nfsrv_clientconflict(stp->ls_clp,&haslock,vp,p);
2421 * nfsrv_clientconflict() unlocks
2422 * state when it returns non-zero.
2427 error = NFSERR_PERM;
2428 else if (new_stp->ls_flags & NFSLCK_RECLAIM)
2429 error = NFSERR_RECLAIMCONFLICT;
2431 error = NFSERR_SHAREDENIED;
2435 NFSLOCKV4ROOTMUTEX();
2436 nfsv4_unlock(&nfsv4rootfs_lock, 1);
2437 NFSUNLOCKV4ROOTMUTEX();
2444 * Check for a conflicting delegation. If one is found, call
2445 * nfsrv_delegconflict() to handle it. If the v4root lock hasn't
2446 * been set yet, it will get the lock. Otherwise, it will recall
2447 * the delegation. Then, we try try again...
2448 * (If NFSLCK_DELEGCUR is set, it has a delegation, so there
2449 * isn't a conflict.)
2450 * I currently believe the conflict algorithm to be:
2451 * For Open with Read Access and Deny None
2452 * - there is a conflict iff a different client has a write delegation
2453 * For Open with other Write Access or any Deny except None
2454 * - there is a conflict if a different client has any delegation
2455 * - there is a conflict if the same client has a read delegation
2456 * (The current consensus is that this last case should be
2457 * considered a conflict since the client with a read delegation
2458 * could have done an Open with ReadAccess and WriteDeny
2459 * locally and then not have checked for the WriteDeny.)
2460 * Don't check for a Reclaim, since that will be dealt with
2461 * by nfsrv_openctrl().
2463 if (!(new_stp->ls_flags &
2464 (NFSLCK_DELEGPREV | NFSLCK_DELEGCUR | NFSLCK_RECLAIM))) {
2465 stp = LIST_FIRST(&lfp->lf_deleg);
2466 while (stp != LIST_END(&lfp->lf_deleg)) {
2467 nstp = LIST_NEXT(stp, ls_file);
2468 if ((readonly && stp->ls_clp != clp &&
2469 (stp->ls_flags & NFSLCK_DELEGWRITE)) ||
2470 (!readonly && (stp->ls_clp != clp ||
2471 (stp->ls_flags & NFSLCK_DELEGREAD)))) {
2472 ret = nfsrv_delegconflict(stp, &haslock, p, vp);
2475 * nfsrv_delegconflict() unlocks state
2476 * when it returns non-zero.
2489 NFSLOCKV4ROOTMUTEX();
2490 nfsv4_unlock(&nfsv4rootfs_lock, 1);
2491 NFSUNLOCKV4ROOTMUTEX();
2495 NFSEXITCODE2(error, nd);
2500 * Open control function to create/update open state for an open.
2503 nfsrv_openctrl(struct nfsrv_descript *nd, vnode_t vp,
2504 struct nfsstate **new_stpp, nfsquad_t clientid, nfsv4stateid_t *stateidp,
2505 nfsv4stateid_t *delegstateidp, u_int32_t *rflagsp, struct nfsexstuff *exp,
2506 NFSPROC_T *p, u_quad_t filerev)
2508 struct nfsstate *new_stp = *new_stpp;
2509 struct nfsstate *stp, *nstp;
2510 struct nfsstate *openstp = NULL, *new_open, *ownerstp, *new_deleg;
2511 struct nfslockfile *lfp, *new_lfp;
2512 struct nfsclient *clp;
2513 int error = 0, haslock = 0, ret, delegate = 1, writedeleg = 1;
2514 int readonly = 0, cbret = 1, getfhret = 0;
2515 int gotstate = 0, len = 0;
2516 u_char *clidp = NULL;
2518 if ((new_stp->ls_flags & NFSLCK_SHAREBITS) == NFSLCK_READACCESS)
2521 * Check for restart conditions (client and server).
2522 * (Paranoia, should have been detected by nfsrv_opencheck().)
2523 * If an error does show up, return NFSERR_EXPIRED, since the
2524 * the seqid# has already been incremented.
2526 error = nfsrv_checkrestart(clientid, new_stp->ls_flags,
2527 &new_stp->ls_stateid, 0);
2529 printf("Nfsd: openctrl unexpected restart err=%d\n",
2531 error = NFSERR_EXPIRED;
2535 clidp = malloc(NFSV4_OPAQUELIMIT, M_TEMP, M_WAITOK);
2537 MALLOC(new_lfp, struct nfslockfile *, sizeof (struct nfslockfile),
2538 M_NFSDLOCKFILE, M_WAITOK);
2539 MALLOC(new_open, struct nfsstate *, sizeof (struct nfsstate),
2540 M_NFSDSTATE, M_WAITOK);
2541 MALLOC(new_deleg, struct nfsstate *, sizeof (struct nfsstate),
2542 M_NFSDSTATE, M_WAITOK);
2543 getfhret = nfsrv_getlockfh(vp, new_stp->ls_flags, new_lfp,
2547 * Get the client structure. Since the linked lists could be changed
2548 * by other nfsd processes if this process does a tsleep(), one of
2549 * two things must be done.
2550 * 1 - don't tsleep()
2552 * 2 - get the nfsv4_lock() { indicated by haslock == 1 }
2553 * before using the lists, since this lock stops the other
2554 * nfsd. This should only be used for rare cases, since it
2555 * essentially single threads the nfsd.
2556 * At this time, it is only done for cases where the stable
2557 * storage file must be written prior to completion of state
2560 error = nfsrv_getclient(clientid, CLOPS_RENEW, &clp, NULL,
2561 (nfsquad_t)((u_quad_t)0), 0, nd, p);
2562 if (!error && (clp->lc_flags & LCL_NEEDSCBNULL) &&
2565 * This happens on the first open for a client
2566 * that supports callbacks.
2570 * Although nfsrv_docallback() will sleep, clp won't
2571 * go away, since they are only removed when the
2572 * nfsv4_lock() has blocked the nfsd threads. The
2573 * fields in clp can change, but having multiple
2574 * threads do this Null callback RPC should be
2577 cbret = nfsrv_docallback(clp, NFSV4PROC_CBNULL,
2578 NULL, 0, NULL, NULL, NULL, p);
2580 clp->lc_flags &= ~LCL_NEEDSCBNULL;
2582 clp->lc_flags |= LCL_CALLBACKSON;
2586 * Look up the open owner. See if it needs confirmation and
2587 * check the seq#, as required.
2590 nfsrv_getowner(&clp->lc_open, new_stp, &ownerstp);
2594 printf("Nfsd: openctrl unexpected state err=%d\n",
2596 free((caddr_t)new_lfp, M_NFSDLOCKFILE);
2597 free((caddr_t)new_open, M_NFSDSTATE);
2598 free((caddr_t)new_deleg, M_NFSDSTATE);
2600 NFSLOCKV4ROOTMUTEX();
2601 nfsv4_unlock(&nfsv4rootfs_lock, 1);
2602 NFSUNLOCKV4ROOTMUTEX();
2604 error = NFSERR_EXPIRED;
2608 if (new_stp->ls_flags & NFSLCK_RECLAIM)
2609 nfsrv_markstable(clp);
2612 * Get the structure for the underlying file.
2617 error = nfsrv_getlockfile(new_stp->ls_flags, &new_lfp, &lfp,
2620 FREE((caddr_t)new_lfp, M_NFSDLOCKFILE);
2623 printf("Nfsd openctrl unexpected getlockfile err=%d\n",
2625 free((caddr_t)new_open, M_NFSDSTATE);
2626 free((caddr_t)new_deleg, M_NFSDSTATE);
2628 NFSLOCKV4ROOTMUTEX();
2629 nfsv4_unlock(&nfsv4rootfs_lock, 1);
2630 NFSUNLOCKV4ROOTMUTEX();
2636 * Search for a conflicting open/share.
2638 if (new_stp->ls_flags & NFSLCK_DELEGCUR) {
2640 * For Delegate_Cur, search for the matching Delegation,
2641 * which indicates no conflict.
2642 * An old delegation should have been recovered by the
2643 * client doing a Claim_DELEGATE_Prev, so I won't let
2644 * it match and return NFSERR_EXPIRED. Should I let it
2647 LIST_FOREACH(stp, &lfp->lf_deleg, ls_file) {
2648 if (!(stp->ls_flags & NFSLCK_OLDDELEG) &&
2649 (((nd->nd_flag & ND_NFSV41) != 0 &&
2650 stateidp->seqid == 0) ||
2651 stateidp->seqid == stp->ls_stateid.seqid) &&
2652 !NFSBCMP(stateidp->other, stp->ls_stateid.other,
2656 if (stp == LIST_END(&lfp->lf_deleg) ||
2657 ((new_stp->ls_flags & NFSLCK_WRITEACCESS) &&
2658 (stp->ls_flags & NFSLCK_DELEGREAD))) {
2660 printf("Nfsd openctrl unexpected expiry\n");
2661 free((caddr_t)new_open, M_NFSDSTATE);
2662 free((caddr_t)new_deleg, M_NFSDSTATE);
2664 NFSLOCKV4ROOTMUTEX();
2665 nfsv4_unlock(&nfsv4rootfs_lock, 1);
2666 NFSUNLOCKV4ROOTMUTEX();
2668 error = NFSERR_EXPIRED;
2673 * Don't issue a Delegation, since one already exists and
2674 * delay delegation timeout, as required.
2677 nfsrv_delaydelegtimeout(stp);
2681 * Check for access/deny bit conflicts. I also check for the
2682 * same owner, since the client might not have bothered to check.
2683 * Also, note an open for the same file and owner, if found,
2684 * which is all we do here for Delegate_Cur, since conflict
2685 * checking is already done.
2687 LIST_FOREACH(stp, &lfp->lf_open, ls_file) {
2688 if (ownerstp && stp->ls_openowner == ownerstp)
2690 if (!(new_stp->ls_flags & NFSLCK_DELEGCUR)) {
2692 * If another client has the file open, the only
2693 * delegation that can be issued is a Read delegation
2694 * and only if it is a Read open with Deny none.
2696 if (clp != stp->ls_clp) {
2697 if ((stp->ls_flags & NFSLCK_SHAREBITS) ==
2703 if(((new_stp->ls_flags & NFSLCK_ACCESSBITS) &
2704 ((stp->ls_flags>>NFSLCK_SHIFT) & NFSLCK_ACCESSBITS))||
2705 ((stp->ls_flags & NFSLCK_ACCESSBITS) &
2706 ((new_stp->ls_flags>>NFSLCK_SHIFT)&NFSLCK_ACCESSBITS))){
2707 ret = nfsrv_clientconflict(stp->ls_clp,&haslock,vp,p);
2710 * nfsrv_clientconflict() unlocks state
2711 * when it returns non-zero.
2713 free((caddr_t)new_open, M_NFSDSTATE);
2714 free((caddr_t)new_deleg, M_NFSDSTATE);
2719 error = NFSERR_PERM;
2720 else if (new_stp->ls_flags & NFSLCK_RECLAIM)
2721 error = NFSERR_RECLAIMCONFLICT;
2723 error = NFSERR_SHAREDENIED;
2727 NFSLOCKV4ROOTMUTEX();
2728 nfsv4_unlock(&nfsv4rootfs_lock, 1);
2729 NFSUNLOCKV4ROOTMUTEX();
2731 free((caddr_t)new_open, M_NFSDSTATE);
2732 free((caddr_t)new_deleg, M_NFSDSTATE);
2733 printf("nfsd openctrl unexpected client cnfl\n");
2740 * Check for a conflicting delegation. If one is found, call
2741 * nfsrv_delegconflict() to handle it. If the v4root lock hasn't
2742 * been set yet, it will get the lock. Otherwise, it will recall
2743 * the delegation. Then, we try try again...
2744 * (If NFSLCK_DELEGCUR is set, it has a delegation, so there
2745 * isn't a conflict.)
2746 * I currently believe the conflict algorithm to be:
2747 * For Open with Read Access and Deny None
2748 * - there is a conflict iff a different client has a write delegation
2749 * For Open with other Write Access or any Deny except None
2750 * - there is a conflict if a different client has any delegation
2751 * - there is a conflict if the same client has a read delegation
2752 * (The current consensus is that this last case should be
2753 * considered a conflict since the client with a read delegation
2754 * could have done an Open with ReadAccess and WriteDeny
2755 * locally and then not have checked for the WriteDeny.)
2757 if (!(new_stp->ls_flags & (NFSLCK_DELEGPREV | NFSLCK_DELEGCUR))) {
2758 stp = LIST_FIRST(&lfp->lf_deleg);
2759 while (stp != LIST_END(&lfp->lf_deleg)) {
2760 nstp = LIST_NEXT(stp, ls_file);
2761 if (stp->ls_clp != clp && (stp->ls_flags & NFSLCK_DELEGREAD))
2765 if ((readonly && stp->ls_clp != clp &&
2766 (stp->ls_flags & NFSLCK_DELEGWRITE)) ||
2767 (!readonly && (stp->ls_clp != clp ||
2768 (stp->ls_flags & NFSLCK_DELEGREAD)))) {
2769 if (new_stp->ls_flags & NFSLCK_RECLAIM) {
2772 ret = nfsrv_delegconflict(stp, &haslock, p, vp);
2775 * nfsrv_delegconflict() unlocks state
2776 * when it returns non-zero.
2778 printf("Nfsd openctrl unexpected deleg cnfl\n");
2779 free((caddr_t)new_open, M_NFSDSTATE);
2780 free((caddr_t)new_deleg, M_NFSDSTATE);
2795 * We only get here if there was no open that conflicted.
2796 * If an open for the owner exists, or in the access/deny bits.
2797 * Otherwise it is a new open. If the open_owner hasn't been
2798 * confirmed, replace the open with the new one needing confirmation,
2799 * otherwise add the open.
2801 if (new_stp->ls_flags & NFSLCK_DELEGPREV) {
2803 * Handle NFSLCK_DELEGPREV by searching the old delegations for
2804 * a match. If found, just move the old delegation to the current
2805 * delegation list and issue open. If not found, return
2808 LIST_FOREACH(stp, &clp->lc_olddeleg, ls_list) {
2809 if (stp->ls_lfp == lfp) {
2811 if (stp->ls_clp != clp)
2812 panic("olddeleg clp");
2813 LIST_REMOVE(stp, ls_list);
2814 LIST_REMOVE(stp, ls_hash);
2815 stp->ls_flags &= ~NFSLCK_OLDDELEG;
2816 stp->ls_stateid.seqid = delegstateidp->seqid = 1;
2817 stp->ls_stateid.other[0] = delegstateidp->other[0] =
2818 clp->lc_clientid.lval[0];
2819 stp->ls_stateid.other[1] = delegstateidp->other[1] =
2820 clp->lc_clientid.lval[1];
2821 stp->ls_stateid.other[2] = delegstateidp->other[2] =
2822 nfsrv_nextstateindex(clp);
2823 stp->ls_compref = nd->nd_compref;
2824 LIST_INSERT_HEAD(&clp->lc_deleg, stp, ls_list);
2825 LIST_INSERT_HEAD(NFSSTATEHASH(clp,
2826 stp->ls_stateid), stp, ls_hash);
2827 if (stp->ls_flags & NFSLCK_DELEGWRITE)
2828 *rflagsp |= NFSV4OPEN_WRITEDELEGATE;
2830 *rflagsp |= NFSV4OPEN_READDELEGATE;
2831 clp->lc_delegtime = NFSD_MONOSEC +
2832 nfsrv_lease + NFSRV_LEASEDELTA;
2835 * Now, do the associated open.
2837 new_open->ls_stateid.seqid = 1;
2838 new_open->ls_stateid.other[0] = clp->lc_clientid.lval[0];
2839 new_open->ls_stateid.other[1] = clp->lc_clientid.lval[1];
2840 new_open->ls_stateid.other[2] = nfsrv_nextstateindex(clp);
2841 new_open->ls_flags = (new_stp->ls_flags&NFSLCK_DENYBITS)|
2843 if (stp->ls_flags & NFSLCK_DELEGWRITE)
2844 new_open->ls_flags |= (NFSLCK_READACCESS |
2845 NFSLCK_WRITEACCESS);
2847 new_open->ls_flags |= NFSLCK_READACCESS;
2848 new_open->ls_uid = new_stp->ls_uid;
2849 new_open->ls_lfp = lfp;
2850 new_open->ls_clp = clp;
2851 LIST_INIT(&new_open->ls_open);
2852 LIST_INSERT_HEAD(&lfp->lf_open, new_open, ls_file);
2853 LIST_INSERT_HEAD(NFSSTATEHASH(clp, new_open->ls_stateid),
2856 * and handle the open owner
2859 new_open->ls_openowner = ownerstp;
2860 LIST_INSERT_HEAD(&ownerstp->ls_open,new_open,ls_list);
2862 new_open->ls_openowner = new_stp;
2863 new_stp->ls_flags = 0;
2864 nfsrvd_refcache(new_stp->ls_op);
2865 new_stp->ls_noopens = 0;
2866 LIST_INIT(&new_stp->ls_open);
2867 LIST_INSERT_HEAD(&new_stp->ls_open, new_open, ls_list);
2868 LIST_INSERT_HEAD(&clp->lc_open, new_stp, ls_list);
2870 nfsstatsv1.srvopenowners++;
2871 nfsrv_openpluslock++;
2875 nfsstatsv1.srvopens++;
2876 nfsrv_openpluslock++;
2880 if (stp == LIST_END(&clp->lc_olddeleg))
2881 error = NFSERR_EXPIRED;
2882 } else if (new_stp->ls_flags & (NFSLCK_DELEGREAD | NFSLCK_DELEGWRITE)) {
2884 * Scan to see that no delegation for this client and file
2885 * doesn't already exist.
2886 * There also shouldn't yet be an Open for this file and
2889 LIST_FOREACH(stp, &lfp->lf_deleg, ls_file) {
2890 if (stp->ls_clp == clp)
2893 if (stp == LIST_END(&lfp->lf_deleg) && openstp == NULL) {
2895 * This is the Claim_Previous case with a delegation
2896 * type != Delegate_None.
2899 * First, add the delegation. (Although we must issue the
2900 * delegation, we can also ask for an immediate return.)
2902 new_deleg->ls_stateid.seqid = delegstateidp->seqid = 1;
2903 new_deleg->ls_stateid.other[0] = delegstateidp->other[0] =
2904 clp->lc_clientid.lval[0];
2905 new_deleg->ls_stateid.other[1] = delegstateidp->other[1] =
2906 clp->lc_clientid.lval[1];
2907 new_deleg->ls_stateid.other[2] = delegstateidp->other[2] =
2908 nfsrv_nextstateindex(clp);
2909 if (new_stp->ls_flags & NFSLCK_DELEGWRITE) {
2910 new_deleg->ls_flags = (NFSLCK_DELEGWRITE |
2911 NFSLCK_READACCESS | NFSLCK_WRITEACCESS);
2912 *rflagsp |= NFSV4OPEN_WRITEDELEGATE;
2913 nfsrv_writedelegcnt++;
2915 new_deleg->ls_flags = (NFSLCK_DELEGREAD |
2917 *rflagsp |= NFSV4OPEN_READDELEGATE;
2919 new_deleg->ls_uid = new_stp->ls_uid;
2920 new_deleg->ls_lfp = lfp;
2921 new_deleg->ls_clp = clp;
2922 new_deleg->ls_filerev = filerev;
2923 new_deleg->ls_compref = nd->nd_compref;
2924 LIST_INSERT_HEAD(&lfp->lf_deleg, new_deleg, ls_file);
2925 LIST_INSERT_HEAD(NFSSTATEHASH(clp,
2926 new_deleg->ls_stateid), new_deleg, ls_hash);
2927 LIST_INSERT_HEAD(&clp->lc_deleg, new_deleg, ls_list);
2929 if (delegate == 2 || nfsrv_issuedelegs == 0 ||
2930 (clp->lc_flags & (LCL_CALLBACKSON | LCL_CBDOWN)) !=
2932 NFSRV_V4DELEGLIMIT(nfsrv_delegatecnt) ||
2933 !NFSVNO_DELEGOK(vp))
2934 *rflagsp |= NFSV4OPEN_RECALL;
2935 nfsstatsv1.srvdelegates++;
2936 nfsrv_openpluslock++;
2937 nfsrv_delegatecnt++;
2940 * Now, do the associated open.
2942 new_open->ls_stateid.seqid = 1;
2943 new_open->ls_stateid.other[0] = clp->lc_clientid.lval[0];
2944 new_open->ls_stateid.other[1] = clp->lc_clientid.lval[1];
2945 new_open->ls_stateid.other[2] = nfsrv_nextstateindex(clp);
2946 new_open->ls_flags = (new_stp->ls_flags & NFSLCK_DENYBITS) |
2948 if (new_stp->ls_flags & NFSLCK_DELEGWRITE)
2949 new_open->ls_flags |= (NFSLCK_READACCESS |
2950 NFSLCK_WRITEACCESS);
2952 new_open->ls_flags |= NFSLCK_READACCESS;
2953 new_open->ls_uid = new_stp->ls_uid;
2954 new_open->ls_lfp = lfp;
2955 new_open->ls_clp = clp;
2956 LIST_INIT(&new_open->ls_open);
2957 LIST_INSERT_HEAD(&lfp->lf_open, new_open, ls_file);
2958 LIST_INSERT_HEAD(NFSSTATEHASH(clp, new_open->ls_stateid),
2961 * and handle the open owner
2964 new_open->ls_openowner = ownerstp;
2965 LIST_INSERT_HEAD(&ownerstp->ls_open, new_open, ls_list);
2967 new_open->ls_openowner = new_stp;
2968 new_stp->ls_flags = 0;
2969 nfsrvd_refcache(new_stp->ls_op);
2970 new_stp->ls_noopens = 0;
2971 LIST_INIT(&new_stp->ls_open);
2972 LIST_INSERT_HEAD(&new_stp->ls_open, new_open, ls_list);
2973 LIST_INSERT_HEAD(&clp->lc_open, new_stp, ls_list);
2975 nfsstatsv1.srvopenowners++;
2976 nfsrv_openpluslock++;
2980 nfsstatsv1.srvopens++;
2981 nfsrv_openpluslock++;
2983 error = NFSERR_RECLAIMCONFLICT;
2985 } else if (ownerstp) {
2986 if (ownerstp->ls_flags & NFSLCK_NEEDSCONFIRM) {
2987 /* Replace the open */
2988 if (ownerstp->ls_op)
2989 nfsrvd_derefcache(ownerstp->ls_op);
2990 ownerstp->ls_op = new_stp->ls_op;
2991 nfsrvd_refcache(ownerstp->ls_op);
2992 ownerstp->ls_seq = new_stp->ls_seq;
2993 *rflagsp |= NFSV4OPEN_RESULTCONFIRM;
2994 stp = LIST_FIRST(&ownerstp->ls_open);
2995 stp->ls_flags = (new_stp->ls_flags & NFSLCK_SHAREBITS) |
2997 stp->ls_stateid.seqid = 1;
2998 stp->ls_uid = new_stp->ls_uid;
2999 if (lfp != stp->ls_lfp) {
3000 LIST_REMOVE(stp, ls_file);
3001 LIST_INSERT_HEAD(&lfp->lf_open, stp, ls_file);
3005 } else if (openstp) {
3006 openstp->ls_flags |= (new_stp->ls_flags & NFSLCK_SHAREBITS);
3007 openstp->ls_stateid.seqid++;
3008 if ((nd->nd_flag & ND_NFSV41) != 0 &&
3009 openstp->ls_stateid.seqid == 0)
3010 openstp->ls_stateid.seqid = 1;
3013 * This is where we can choose to issue a delegation.
3015 if (delegate == 0 || writedeleg == 0 ||
3016 NFSVNO_EXRDONLY(exp) || (readonly != 0 &&
3017 nfsrv_writedelegifpos == 0) ||
3018 !NFSVNO_DELEGOK(vp) ||
3019 (new_stp->ls_flags & NFSLCK_WANTRDELEG) != 0 ||
3020 (clp->lc_flags & (LCL_CALLBACKSON | LCL_CBDOWN)) !=
3022 *rflagsp |= NFSV4OPEN_WDCONTENTION;
3023 else if (nfsrv_issuedelegs == 0 ||
3024 NFSRV_V4DELEGLIMIT(nfsrv_delegatecnt))
3025 *rflagsp |= NFSV4OPEN_WDRESOURCE;
3026 else if ((new_stp->ls_flags & NFSLCK_WANTNODELEG) != 0)
3027 *rflagsp |= NFSV4OPEN_WDNOTWANTED;
3029 new_deleg->ls_stateid.seqid = delegstateidp->seqid = 1;
3030 new_deleg->ls_stateid.other[0] = delegstateidp->other[0]
3031 = clp->lc_clientid.lval[0];
3032 new_deleg->ls_stateid.other[1] = delegstateidp->other[1]
3033 = clp->lc_clientid.lval[1];
3034 new_deleg->ls_stateid.other[2] = delegstateidp->other[2]
3035 = nfsrv_nextstateindex(clp);
3036 new_deleg->ls_flags = (NFSLCK_DELEGWRITE |
3037 NFSLCK_READACCESS | NFSLCK_WRITEACCESS);
3038 *rflagsp |= NFSV4OPEN_WRITEDELEGATE;
3039 new_deleg->ls_uid = new_stp->ls_uid;
3040 new_deleg->ls_lfp = lfp;
3041 new_deleg->ls_clp = clp;
3042 new_deleg->ls_filerev = filerev;
3043 new_deleg->ls_compref = nd->nd_compref;
3044 nfsrv_writedelegcnt++;
3045 LIST_INSERT_HEAD(&lfp->lf_deleg, new_deleg, ls_file);
3046 LIST_INSERT_HEAD(NFSSTATEHASH(clp,
3047 new_deleg->ls_stateid), new_deleg, ls_hash);
3048 LIST_INSERT_HEAD(&clp->lc_deleg, new_deleg, ls_list);
3050 nfsstatsv1.srvdelegates++;
3051 nfsrv_openpluslock++;
3052 nfsrv_delegatecnt++;
3055 new_open->ls_stateid.seqid = 1;
3056 new_open->ls_stateid.other[0] = clp->lc_clientid.lval[0];
3057 new_open->ls_stateid.other[1] = clp->lc_clientid.lval[1];
3058 new_open->ls_stateid.other[2] = nfsrv_nextstateindex(clp);
3059 new_open->ls_flags = (new_stp->ls_flags & NFSLCK_SHAREBITS)|
3061 new_open->ls_uid = new_stp->ls_uid;
3062 new_open->ls_openowner = ownerstp;
3063 new_open->ls_lfp = lfp;
3064 new_open->ls_clp = clp;
3065 LIST_INIT(&new_open->ls_open);
3066 LIST_INSERT_HEAD(&lfp->lf_open, new_open, ls_file);
3067 LIST_INSERT_HEAD(&ownerstp->ls_open, new_open, ls_list);
3068 LIST_INSERT_HEAD(NFSSTATEHASH(clp, new_open->ls_stateid),
3072 nfsstatsv1.srvopens++;
3073 nfsrv_openpluslock++;
3076 * This is where we can choose to issue a delegation.
3078 if (delegate == 0 || (writedeleg == 0 && readonly == 0) ||
3079 !NFSVNO_DELEGOK(vp) ||
3080 (clp->lc_flags & (LCL_CALLBACKSON | LCL_CBDOWN)) !=
3082 *rflagsp |= NFSV4OPEN_WDCONTENTION;
3083 else if (nfsrv_issuedelegs == 0 ||
3084 NFSRV_V4DELEGLIMIT(nfsrv_delegatecnt))
3085 *rflagsp |= NFSV4OPEN_WDRESOURCE;
3086 else if ((new_stp->ls_flags & NFSLCK_WANTNODELEG) != 0)
3087 *rflagsp |= NFSV4OPEN_WDNOTWANTED;
3089 new_deleg->ls_stateid.seqid = delegstateidp->seqid = 1;
3090 new_deleg->ls_stateid.other[0] = delegstateidp->other[0]
3091 = clp->lc_clientid.lval[0];
3092 new_deleg->ls_stateid.other[1] = delegstateidp->other[1]
3093 = clp->lc_clientid.lval[1];
3094 new_deleg->ls_stateid.other[2] = delegstateidp->other[2]
3095 = nfsrv_nextstateindex(clp);
3096 if (writedeleg && !NFSVNO_EXRDONLY(exp) &&
3097 (nfsrv_writedelegifpos || !readonly) &&
3098 (new_stp->ls_flags & NFSLCK_WANTRDELEG) == 0) {
3099 new_deleg->ls_flags = (NFSLCK_DELEGWRITE |
3100 NFSLCK_READACCESS | NFSLCK_WRITEACCESS);
3101 *rflagsp |= NFSV4OPEN_WRITEDELEGATE;
3102 nfsrv_writedelegcnt++;
3104 new_deleg->ls_flags = (NFSLCK_DELEGREAD |
3106 *rflagsp |= NFSV4OPEN_READDELEGATE;
3108 new_deleg->ls_uid = new_stp->ls_uid;
3109 new_deleg->ls_lfp = lfp;
3110 new_deleg->ls_clp = clp;
3111 new_deleg->ls_filerev = filerev;
3112 new_deleg->ls_compref = nd->nd_compref;
3113 LIST_INSERT_HEAD(&lfp->lf_deleg, new_deleg, ls_file);
3114 LIST_INSERT_HEAD(NFSSTATEHASH(clp,
3115 new_deleg->ls_stateid), new_deleg, ls_hash);
3116 LIST_INSERT_HEAD(&clp->lc_deleg, new_deleg, ls_list);
3118 nfsstatsv1.srvdelegates++;
3119 nfsrv_openpluslock++;
3120 nfsrv_delegatecnt++;
3125 * New owner case. Start the open_owner sequence with a
3126 * Needs confirmation (unless a reclaim) and hang the
3129 new_open->ls_stateid.seqid = 1;
3130 new_open->ls_stateid.other[0] = clp->lc_clientid.lval[0];
3131 new_open->ls_stateid.other[1] = clp->lc_clientid.lval[1];
3132 new_open->ls_stateid.other[2] = nfsrv_nextstateindex(clp);
3133 new_open->ls_flags = (new_stp->ls_flags & NFSLCK_SHAREBITS) |
3135 new_open->ls_uid = new_stp->ls_uid;
3136 LIST_INIT(&new_open->ls_open);
3137 new_open->ls_openowner = new_stp;
3138 new_open->ls_lfp = lfp;
3139 new_open->ls_clp = clp;
3140 LIST_INSERT_HEAD(&lfp->lf_open, new_open, ls_file);
3141 if (new_stp->ls_flags & NFSLCK_RECLAIM) {
3142 new_stp->ls_flags = 0;
3143 } else if ((nd->nd_flag & ND_NFSV41) != 0) {
3144 /* NFSv4.1 never needs confirmation. */
3145 new_stp->ls_flags = 0;
3148 * This is where we can choose to issue a delegation.
3150 if (delegate && nfsrv_issuedelegs &&
3151 (writedeleg || readonly) &&
3152 (clp->lc_flags & (LCL_CALLBACKSON | LCL_CBDOWN)) ==
3154 !NFSRV_V4DELEGLIMIT(nfsrv_delegatecnt) &&
3155 NFSVNO_DELEGOK(vp) &&
3156 ((nd->nd_flag & ND_NFSV41) == 0 ||
3157 (new_stp->ls_flags & NFSLCK_WANTNODELEG) == 0)) {
3158 new_deleg->ls_stateid.seqid =
3159 delegstateidp->seqid = 1;
3160 new_deleg->ls_stateid.other[0] =
3161 delegstateidp->other[0]
3162 = clp->lc_clientid.lval[0];
3163 new_deleg->ls_stateid.other[1] =
3164 delegstateidp->other[1]
3165 = clp->lc_clientid.lval[1];
3166 new_deleg->ls_stateid.other[2] =
3167 delegstateidp->other[2]
3168 = nfsrv_nextstateindex(clp);
3169 if (writedeleg && !NFSVNO_EXRDONLY(exp) &&
3170 (nfsrv_writedelegifpos || !readonly) &&
3171 ((nd->nd_flag & ND_NFSV41) == 0 ||
3172 (new_stp->ls_flags & NFSLCK_WANTRDELEG) ==
3174 new_deleg->ls_flags =
3175 (NFSLCK_DELEGWRITE |
3177 NFSLCK_WRITEACCESS);
3178 *rflagsp |= NFSV4OPEN_WRITEDELEGATE;
3179 nfsrv_writedelegcnt++;
3181 new_deleg->ls_flags =
3184 *rflagsp |= NFSV4OPEN_READDELEGATE;
3186 new_deleg->ls_uid = new_stp->ls_uid;
3187 new_deleg->ls_lfp = lfp;
3188 new_deleg->ls_clp = clp;
3189 new_deleg->ls_filerev = filerev;
3190 new_deleg->ls_compref = nd->nd_compref;
3191 LIST_INSERT_HEAD(&lfp->lf_deleg, new_deleg,
3193 LIST_INSERT_HEAD(NFSSTATEHASH(clp,
3194 new_deleg->ls_stateid), new_deleg, ls_hash);
3195 LIST_INSERT_HEAD(&clp->lc_deleg, new_deleg,
3198 nfsstatsv1.srvdelegates++;
3199 nfsrv_openpluslock++;
3200 nfsrv_delegatecnt++;
3203 * Since NFSv4.1 never does an OpenConfirm, the first
3204 * open state will be acquired here.
3206 if (!(clp->lc_flags & LCL_STAMPEDSTABLE)) {
3207 clp->lc_flags |= LCL_STAMPEDSTABLE;
3208 len = clp->lc_idlen;
3209 NFSBCOPY(clp->lc_id, clidp, len);
3213 *rflagsp |= NFSV4OPEN_RESULTCONFIRM;
3214 new_stp->ls_flags = NFSLCK_NEEDSCONFIRM;
3216 nfsrvd_refcache(new_stp->ls_op);
3217 new_stp->ls_noopens = 0;
3218 LIST_INIT(&new_stp->ls_open);
3219 LIST_INSERT_HEAD(&new_stp->ls_open, new_open, ls_list);
3220 LIST_INSERT_HEAD(&clp->lc_open, new_stp, ls_list);
3221 LIST_INSERT_HEAD(NFSSTATEHASH(clp, new_open->ls_stateid),
3226 nfsstatsv1.srvopens++;
3227 nfsrv_openpluslock++;
3228 nfsstatsv1.srvopenowners++;
3229 nfsrv_openpluslock++;
3232 stateidp->seqid = openstp->ls_stateid.seqid;
3233 stateidp->other[0] = openstp->ls_stateid.other[0];
3234 stateidp->other[1] = openstp->ls_stateid.other[1];
3235 stateidp->other[2] = openstp->ls_stateid.other[2];
3239 NFSLOCKV4ROOTMUTEX();
3240 nfsv4_unlock(&nfsv4rootfs_lock, 1);
3241 NFSUNLOCKV4ROOTMUTEX();
3244 FREE((caddr_t)new_open, M_NFSDSTATE);
3246 FREE((caddr_t)new_deleg, M_NFSDSTATE);
3249 * If the NFSv4.1 client just acquired its first open, write a timestamp
3250 * to the stable storage file.
3252 if (gotstate != 0) {
3253 nfsrv_writestable(clidp, len, NFSNST_NEWSTATE, p);
3254 nfsrv_backupstable();
3258 free(clidp, M_TEMP);
3259 NFSEXITCODE2(error, nd);
3264 * Open update. Does the confirm, downgrade and close.
3267 nfsrv_openupdate(vnode_t vp, struct nfsstate *new_stp, nfsquad_t clientid,
3268 nfsv4stateid_t *stateidp, struct nfsrv_descript *nd, NFSPROC_T *p)
3270 struct nfsstate *stp;
3271 struct nfsclient *clp;
3272 struct nfslockfile *lfp;
3274 int error = 0, gotstate = 0, len = 0;
3275 u_char *clidp = NULL;
3278 * Check for restart conditions (client and server).
3280 error = nfsrv_checkrestart(clientid, new_stp->ls_flags,
3281 &new_stp->ls_stateid, 0);
3285 clidp = malloc(NFSV4_OPAQUELIMIT, M_TEMP, M_WAITOK);
3288 * Get the open structure via clientid and stateid.
3290 error = nfsrv_getclient(clientid, CLOPS_RENEW, &clp, NULL,
3291 (nfsquad_t)((u_quad_t)0), 0, nd, p);
3293 error = nfsrv_getstate(clp, &new_stp->ls_stateid,
3294 new_stp->ls_flags, &stp);
3297 * Sanity check the open.
3299 if (!error && (!(stp->ls_flags & NFSLCK_OPEN) ||
3300 (!(new_stp->ls_flags & NFSLCK_CONFIRM) &&
3301 (stp->ls_openowner->ls_flags & NFSLCK_NEEDSCONFIRM)) ||
3302 ((new_stp->ls_flags & NFSLCK_CONFIRM) &&
3303 (!(stp->ls_openowner->ls_flags & NFSLCK_NEEDSCONFIRM)))))
3304 error = NFSERR_BADSTATEID;
3307 error = nfsrv_checkseqid(nd, new_stp->ls_seq,
3308 stp->ls_openowner, new_stp->ls_op);
3309 if (!error && stp->ls_stateid.seqid != new_stp->ls_stateid.seqid &&
3310 (((nd->nd_flag & ND_NFSV41) == 0 &&
3311 !(new_stp->ls_flags & NFSLCK_CONFIRM)) ||
3312 ((nd->nd_flag & ND_NFSV41) != 0 &&
3313 new_stp->ls_stateid.seqid != 0)))
3314 error = NFSERR_OLDSTATEID;
3315 if (!error && vnode_vtype(vp) != VREG) {
3316 if (vnode_vtype(vp) == VDIR)
3317 error = NFSERR_ISDIR;
3319 error = NFSERR_INVAL;
3324 * If a client tries to confirm an Open with a bad
3325 * seqid# and there are no byte range locks or other Opens
3326 * on the openowner, just throw it away, so the next use of the
3327 * openowner will start a fresh seq#.
3329 if (error == NFSERR_BADSEQID &&
3330 (new_stp->ls_flags & NFSLCK_CONFIRM) &&
3331 nfsrv_nootherstate(stp))
3332 nfsrv_freeopenowner(stp->ls_openowner, 0, p);
3338 * Set the return stateid.
3340 stateidp->seqid = stp->ls_stateid.seqid + 1;
3341 if ((nd->nd_flag & ND_NFSV41) != 0 && stateidp->seqid == 0)
3342 stateidp->seqid = 1;
3343 stateidp->other[0] = stp->ls_stateid.other[0];
3344 stateidp->other[1] = stp->ls_stateid.other[1];
3345 stateidp->other[2] = stp->ls_stateid.other[2];
3347 * Now, handle the three cases.
3349 if (new_stp->ls_flags & NFSLCK_CONFIRM) {
3351 * If the open doesn't need confirmation, it seems to me that
3352 * there is a client error, but I'll just log it and keep going?
3354 if (!(stp->ls_openowner->ls_flags & NFSLCK_NEEDSCONFIRM))
3355 printf("Nfsv4d: stray open confirm\n");
3356 stp->ls_openowner->ls_flags = 0;
3357 stp->ls_stateid.seqid++;
3358 if ((nd->nd_flag & ND_NFSV41) != 0 &&
3359 stp->ls_stateid.seqid == 0)
3360 stp->ls_stateid.seqid = 1;
3361 if (!(clp->lc_flags & LCL_STAMPEDSTABLE)) {
3362 clp->lc_flags |= LCL_STAMPEDSTABLE;
3363 len = clp->lc_idlen;
3364 NFSBCOPY(clp->lc_id, clidp, len);
3368 } else if (new_stp->ls_flags & NFSLCK_CLOSE) {
3370 if (nfsrv_dolocallocks != 0 && !LIST_EMPTY(&stp->ls_open)) {
3371 /* Get the lf lock */
3374 ASSERT_VOP_ELOCKED(vp, "nfsrv_openupdate");
3375 NFSVOPUNLOCK(vp, 0);
3376 if (nfsrv_freeopen(stp, vp, 1, p) == 0) {
3378 nfsrv_unlocklf(lfp);
3381 NFSVOPLOCK(vp, LK_EXCLUSIVE | LK_RETRY);
3383 (void) nfsrv_freeopen(stp, NULL, 0, p);
3388 * Update the share bits, making sure that the new set are a
3389 * subset of the old ones.
3391 bits = (new_stp->ls_flags & NFSLCK_SHAREBITS);
3392 if (~(stp->ls_flags) & bits) {
3394 error = NFSERR_INVAL;
3397 stp->ls_flags = (bits | NFSLCK_OPEN);
3398 stp->ls_stateid.seqid++;
3399 if ((nd->nd_flag & ND_NFSV41) != 0 &&
3400 stp->ls_stateid.seqid == 0)
3401 stp->ls_stateid.seqid = 1;
3406 * If the client just confirmed its first open, write a timestamp
3407 * to the stable storage file.
3409 if (gotstate != 0) {
3410 nfsrv_writestable(clidp, len, NFSNST_NEWSTATE, p);
3411 nfsrv_backupstable();
3415 free(clidp, M_TEMP);
3416 NFSEXITCODE2(error, nd);
3421 * Delegation update. Does the purge and return.
3424 nfsrv_delegupdate(struct nfsrv_descript *nd, nfsquad_t clientid,
3425 nfsv4stateid_t *stateidp, vnode_t vp, int op, struct ucred *cred,
3428 struct nfsstate *stp;
3429 struct nfsclient *clp;
3434 * Do a sanity check against the file handle for DelegReturn.
3437 error = nfsvno_getfh(vp, &fh, p);
3442 * Check for restart conditions (client and server).
3444 if (op == NFSV4OP_DELEGRETURN)
3445 error = nfsrv_checkrestart(clientid, NFSLCK_DELEGRETURN,
3448 error = nfsrv_checkrestart(clientid, NFSLCK_DELEGPURGE,
3453 * Get the open structure via clientid and stateid.
3456 error = nfsrv_getclient(clientid, CLOPS_RENEW, &clp, NULL,
3457 (nfsquad_t)((u_quad_t)0), 0, nd, p);
3459 if (error == NFSERR_CBPATHDOWN)
3461 if (error == NFSERR_STALECLIENTID && op == NFSV4OP_DELEGRETURN)
3462 error = NFSERR_STALESTATEID;
3464 if (!error && op == NFSV4OP_DELEGRETURN) {
3465 error = nfsrv_getstate(clp, stateidp, NFSLCK_DELEGRETURN, &stp);
3466 if (!error && stp->ls_stateid.seqid != stateidp->seqid &&
3467 ((nd->nd_flag & ND_NFSV41) == 0 || stateidp->seqid != 0))
3468 error = NFSERR_OLDSTATEID;
3471 * NFSERR_EXPIRED means that the state has gone away,
3472 * so Delegations have been purged. Just return ok.
3474 if (error == NFSERR_EXPIRED && op == NFSV4OP_DELEGPURGE) {
3484 if (op == NFSV4OP_DELEGRETURN) {
3485 if (NFSBCMP((caddr_t)&fh, (caddr_t)&stp->ls_lfp->lf_fh,
3486 sizeof (fhandle_t))) {
3488 error = NFSERR_BADSTATEID;
3491 nfsrv_freedeleg(stp);
3493 nfsrv_freedeleglist(&clp->lc_olddeleg);
3504 * Release lock owner.
3507 nfsrv_releaselckown(struct nfsstate *new_stp, nfsquad_t clientid,
3510 struct nfsstate *stp, *nstp, *openstp, *ownstp;
3511 struct nfsclient *clp;
3515 * Check for restart conditions (client and server).
3517 error = nfsrv_checkrestart(clientid, new_stp->ls_flags,
3518 &new_stp->ls_stateid, 0);
3524 * Get the lock owner by name.
3526 error = nfsrv_getclient(clientid, CLOPS_RENEW, &clp, NULL,
3527 (nfsquad_t)((u_quad_t)0), 0, NULL, p);
3532 LIST_FOREACH(ownstp, &clp->lc_open, ls_list) {
3533 LIST_FOREACH(openstp, &ownstp->ls_open, ls_list) {
3534 stp = LIST_FIRST(&openstp->ls_open);
3535 while (stp != LIST_END(&openstp->ls_open)) {
3536 nstp = LIST_NEXT(stp, ls_list);
3538 * If the owner matches, check for locks and
3539 * then free or return an error.
3541 if (stp->ls_ownerlen == new_stp->ls_ownerlen &&
3542 !NFSBCMP(stp->ls_owner, new_stp->ls_owner,
3544 if (LIST_EMPTY(&stp->ls_lock)) {
3545 nfsrv_freelockowner(stp, NULL, 0, p);
3548 error = NFSERR_LOCKSHELD;
3564 * Get the file handle for a lock structure.
3567 nfsrv_getlockfh(vnode_t vp, u_short flags, struct nfslockfile *new_lfp,
3568 fhandle_t *nfhp, NFSPROC_T *p)
3570 fhandle_t *fhp = NULL;
3574 * For lock, use the new nfslock structure, otherwise just
3575 * a fhandle_t on the stack.
3577 if (flags & NFSLCK_OPEN) {
3578 KASSERT(new_lfp != NULL, ("nfsrv_getlockfh: new_lfp NULL"));
3579 fhp = &new_lfp->lf_fh;
3583 panic("nfsrv_getlockfh");
3585 error = nfsvno_getfh(vp, fhp, p);
3591 * Get an nfs lock structure. Allocate one, as required, and return a
3593 * Returns an NFSERR_xxx upon failure or -1 to indicate no current lock.
3596 nfsrv_getlockfile(u_short flags, struct nfslockfile **new_lfpp,
3597 struct nfslockfile **lfpp, fhandle_t *nfhp, int lockit)
3599 struct nfslockfile *lfp;
3600 fhandle_t *fhp = NULL, *tfhp;
3601 struct nfslockhashhead *hp;
3602 struct nfslockfile *new_lfp = NULL;
3605 * For lock, use the new nfslock structure, otherwise just
3606 * a fhandle_t on the stack.
3608 if (flags & NFSLCK_OPEN) {
3609 new_lfp = *new_lfpp;
3610 fhp = &new_lfp->lf_fh;
3614 panic("nfsrv_getlockfile");
3617 hp = NFSLOCKHASH(fhp);
3618 LIST_FOREACH(lfp, hp, lf_hash) {
3620 if (NFSVNO_CMPFH(fhp, tfhp)) {
3627 if (!(flags & NFSLCK_OPEN))
3631 * No match, so chain the new one into the list.
3633 LIST_INIT(&new_lfp->lf_open);
3634 LIST_INIT(&new_lfp->lf_lock);
3635 LIST_INIT(&new_lfp->lf_deleg);
3636 LIST_INIT(&new_lfp->lf_locallock);
3637 LIST_INIT(&new_lfp->lf_rollback);
3638 new_lfp->lf_locallock_lck.nfslock_usecnt = 0;
3639 new_lfp->lf_locallock_lck.nfslock_lock = 0;
3640 new_lfp->lf_usecount = 0;
3641 LIST_INSERT_HEAD(hp, new_lfp, lf_hash);
3648 * This function adds a nfslock lock structure to the list for the associated
3649 * nfsstate and nfslockfile structures. It will be inserted after the
3650 * entry pointed at by insert_lop.
3653 nfsrv_insertlock(struct nfslock *new_lop, struct nfslock *insert_lop,
3654 struct nfsstate *stp, struct nfslockfile *lfp)
3656 struct nfslock *lop, *nlop;
3658 new_lop->lo_stp = stp;
3659 new_lop->lo_lfp = lfp;
3662 /* Insert in increasing lo_first order */
3663 lop = LIST_FIRST(&lfp->lf_lock);
3664 if (lop == LIST_END(&lfp->lf_lock) ||
3665 new_lop->lo_first <= lop->lo_first) {
3666 LIST_INSERT_HEAD(&lfp->lf_lock, new_lop, lo_lckfile);
3668 nlop = LIST_NEXT(lop, lo_lckfile);
3669 while (nlop != LIST_END(&lfp->lf_lock) &&
3670 nlop->lo_first < new_lop->lo_first) {
3672 nlop = LIST_NEXT(lop, lo_lckfile);
3674 LIST_INSERT_AFTER(lop, new_lop, lo_lckfile);
3677 new_lop->lo_lckfile.le_prev = NULL; /* list not used */
3681 * Insert after insert_lop, which is overloaded as stp or lfp for
3684 if (stp == NULL && (struct nfslockfile *)insert_lop == lfp)
3685 LIST_INSERT_HEAD(&lfp->lf_locallock, new_lop, lo_lckowner);
3686 else if ((struct nfsstate *)insert_lop == stp)
3687 LIST_INSERT_HEAD(&stp->ls_lock, new_lop, lo_lckowner);
3689 LIST_INSERT_AFTER(insert_lop, new_lop, lo_lckowner);
3691 nfsstatsv1.srvlocks++;
3692 nfsrv_openpluslock++;
3697 * This function updates the locking for a lock owner and given file. It
3698 * maintains a list of lock ranges ordered on increasing file offset that
3699 * are NFSLCK_READ or NFSLCK_WRITE and non-overlapping (aka POSIX style).
3700 * It always adds new_lop to the list and sometimes uses the one pointed
3704 nfsrv_updatelock(struct nfsstate *stp, struct nfslock **new_lopp,
3705 struct nfslock **other_lopp, struct nfslockfile *lfp)
3707 struct nfslock *new_lop = *new_lopp;
3708 struct nfslock *lop, *tlop, *ilop;
3709 struct nfslock *other_lop = *other_lopp;
3710 int unlock = 0, myfile = 0;
3714 * Work down the list until the lock is merged.
3716 if (new_lop->lo_flags & NFSLCK_UNLOCK)
3719 ilop = (struct nfslock *)stp;
3720 lop = LIST_FIRST(&stp->ls_lock);
3722 ilop = (struct nfslock *)lfp;
3723 lop = LIST_FIRST(&lfp->lf_locallock);
3725 while (lop != NULL) {
3727 * Only check locks for this file that aren't before the start of
3730 if (lop->lo_lfp == lfp) {
3732 if (lop->lo_end >= new_lop->lo_first) {
3733 if (new_lop->lo_end < lop->lo_first) {
3735 * If the new lock ends before the start of the
3736 * current lock's range, no merge, just insert
3741 if (new_lop->lo_flags == lop->lo_flags ||
3742 (new_lop->lo_first <= lop->lo_first &&
3743 new_lop->lo_end >= lop->lo_end)) {
3745 * This lock can be absorbed by the new lock/unlock.
3746 * This happens when it covers the entire range
3747 * of the old lock or is contiguous
3748 * with the old lock and is of the same type or an
3751 if (lop->lo_first < new_lop->lo_first)
3752 new_lop->lo_first = lop->lo_first;
3753 if (lop->lo_end > new_lop->lo_end)
3754 new_lop->lo_end = lop->lo_end;
3756 lop = LIST_NEXT(lop, lo_lckowner);
3757 nfsrv_freenfslock(tlop);
3762 * All these cases are for contiguous locks that are not the
3763 * same type, so they can't be merged.
3765 if (new_lop->lo_first <= lop->lo_first) {
3767 * This case is where the new lock overlaps with the
3768 * first part of the old lock. Move the start of the
3769 * old lock to just past the end of the new lock. The
3770 * new lock will be inserted in front of the old, since
3771 * ilop hasn't been updated. (We are done now.)
3773 lop->lo_first = new_lop->lo_end;
3776 if (new_lop->lo_end >= lop->lo_end) {
3778 * This case is where the new lock overlaps with the
3779 * end of the old lock's range. Move the old lock's
3780 * end to just before the new lock's first and insert
3781 * the new lock after the old lock.
3782 * Might not be done yet, since the new lock could
3783 * overlap further locks with higher ranges.
3785 lop->lo_end = new_lop->lo_first;
3787 lop = LIST_NEXT(lop, lo_lckowner);
3791 * The final case is where the new lock's range is in the
3792 * middle of the current lock's and splits the current lock
3793 * up. Use *other_lopp to handle the second part of the
3794 * split old lock range. (We are done now.)
3795 * For unlock, we use new_lop as other_lop and tmp, since
3796 * other_lop and new_lop are the same for this case.
3797 * We noted the unlock case above, so we don't need
3798 * new_lop->lo_flags any longer.
3800 tmp = new_lop->lo_first;
3801 if (other_lop == NULL) {
3803 panic("nfsd srv update unlock");
3804 other_lop = new_lop;
3807 other_lop->lo_first = new_lop->lo_end;
3808 other_lop->lo_end = lop->lo_end;
3809 other_lop->lo_flags = lop->lo_flags;
3810 other_lop->lo_stp = stp;
3811 other_lop->lo_lfp = lfp;
3813 nfsrv_insertlock(other_lop, lop, stp, lfp);
3820 lop = LIST_NEXT(lop, lo_lckowner);
3821 if (myfile && (lop == NULL || lop->lo_lfp != lfp))
3826 * Insert the new lock in the list at the appropriate place.
3829 nfsrv_insertlock(new_lop, ilop, stp, lfp);
3835 * This function handles sequencing of locks, etc.
3836 * It returns an error that indicates what the caller should do.
3839 nfsrv_checkseqid(struct nfsrv_descript *nd, u_int32_t seqid,
3840 struct nfsstate *stp, struct nfsrvcache *op)
3844 if ((nd->nd_flag & ND_NFSV41) != 0)
3845 /* NFSv4.1 ignores the open_seqid and lock_seqid. */
3847 if (op != nd->nd_rp)
3848 panic("nfsrvstate checkseqid");
3849 if (!(op->rc_flag & RC_INPROG))
3850 panic("nfsrvstate not inprog");
3851 if (stp->ls_op && stp->ls_op->rc_refcnt <= 0) {
3852 printf("refcnt=%d\n", stp->ls_op->rc_refcnt);
3853 panic("nfsrvstate op refcnt");
3855 if ((stp->ls_seq + 1) == seqid) {
3857 nfsrvd_derefcache(stp->ls_op);
3859 nfsrvd_refcache(op);
3860 stp->ls_seq = seqid;
3862 } else if (stp->ls_seq == seqid && stp->ls_op &&
3863 op->rc_xid == stp->ls_op->rc_xid &&
3864 op->rc_refcnt == 0 &&
3865 op->rc_reqlen == stp->ls_op->rc_reqlen &&
3866 op->rc_cksum == stp->ls_op->rc_cksum) {
3867 if (stp->ls_op->rc_flag & RC_INPROG) {
3868 error = NFSERR_DONTREPLY;
3871 nd->nd_rp = stp->ls_op;
3872 nd->nd_rp->rc_flag |= RC_INPROG;
3873 nfsrvd_delcache(op);
3874 error = NFSERR_REPLYFROMCACHE;
3877 error = NFSERR_BADSEQID;
3880 NFSEXITCODE2(error, nd);
3885 * Get the client ip address for callbacks. If the strings can't be parsed,
3886 * just set lc_program to 0 to indicate no callbacks are possible.
3887 * (For cases where the address can't be parsed or is 0.0.0.0.0.0, set
3888 * the address to the client's transport address. This won't be used
3889 * for callbacks, but can be printed out by nfsstats for info.)
3890 * Return error if the xdr can't be parsed, 0 otherwise.
3893 nfsrv_getclientipaddr(struct nfsrv_descript *nd, struct nfsclient *clp)
3898 struct sockaddr_in *rad, *sad;
3899 u_char protocol[5], addr[24];
3900 int error = 0, cantparse = 0;
3910 rad = NFSSOCKADDR(clp->lc_req.nr_nam, struct sockaddr_in *);
3911 rad->sin_family = AF_INET;
3912 rad->sin_len = sizeof (struct sockaddr_in);
3913 rad->sin_addr.s_addr = 0;
3915 clp->lc_req.nr_client = NULL;
3916 clp->lc_req.nr_lock = 0;
3917 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
3918 i = fxdr_unsigned(int, *tl);
3919 if (i >= 3 && i <= 4) {
3920 error = nfsrv_mtostr(nd, protocol, i);
3923 if (!strcmp(protocol, "tcp")) {
3924 clp->lc_flags |= LCL_TCPCALLBACK;
3925 clp->lc_req.nr_sotype = SOCK_STREAM;
3926 clp->lc_req.nr_soproto = IPPROTO_TCP;
3927 } else if (!strcmp(protocol, "udp")) {
3928 clp->lc_req.nr_sotype = SOCK_DGRAM;
3929 clp->lc_req.nr_soproto = IPPROTO_UDP;
3936 error = nfsm_advance(nd, NFSM_RNDUP(i), -1);
3941 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
3942 i = fxdr_unsigned(int, *tl);
3944 error = NFSERR_BADXDR;
3946 } else if (i == 0) {
3948 } else if (!cantparse && i <= 23 && i >= 11) {
3949 error = nfsrv_mtostr(nd, addr, i);
3954 * Parse out the address fields. We expect 6 decimal numbers
3955 * separated by '.'s.
3959 while (*cp && i < 6) {
3961 while (*cp2 && *cp2 != '.')
3969 j = nfsrv_getipnumber(cp);
3974 port.cval[5 - i] = j;
3983 if (ip.ival != 0x0) {
3984 rad->sin_addr.s_addr = htonl(ip.ival);
3985 rad->sin_port = htons(port.sval);
3993 error = nfsm_advance(nd, NFSM_RNDUP(i), -1);
3999 sad = NFSSOCKADDR(nd->nd_nam, struct sockaddr_in *);
4000 if (sad->sin_family == AF_INET) {
4001 rad->sin_addr.s_addr = sad->sin_addr.s_addr;
4002 rad->sin_port = 0x0;
4004 clp->lc_program = 0;
4007 NFSEXITCODE2(error, nd);
4012 * Turn a string of up to three decimal digits into a number. Return -1 upon
4016 nfsrv_getipnumber(u_char *cp)
4021 if (j > 2 || *cp < '0' || *cp > '9')
4034 * This function checks for restart conditions.
4037 nfsrv_checkrestart(nfsquad_t clientid, u_int32_t flags,
4038 nfsv4stateid_t *stateidp, int specialid)
4043 * First check for a server restart. Open, LockT, ReleaseLockOwner
4044 * and DelegPurge have a clientid, the rest a stateid.
4047 (NFSLCK_OPEN | NFSLCK_TEST | NFSLCK_RELEASE | NFSLCK_DELEGPURGE)) {
4048 if (clientid.lval[0] != nfsrvboottime) {
4049 ret = NFSERR_STALECLIENTID;
4052 } else if (stateidp->other[0] != nfsrvboottime &&
4054 ret = NFSERR_STALESTATEID;
4059 * Read, Write, Setattr and LockT can return NFSERR_GRACE and do
4060 * not use a lock/open owner seqid#, so the check can be done now.
4061 * (The others will be checked, as required, later.)
4063 if (!(flags & (NFSLCK_CHECK | NFSLCK_TEST)))
4067 ret = nfsrv_checkgrace(NULL, NULL, flags);
4079 nfsrv_checkgrace(struct nfsrv_descript *nd, struct nfsclient *clp,
4084 if ((nfsrv_stablefirst.nsf_flags & NFSNSF_GRACEOVER) != 0) {
4085 if (flags & NFSLCK_RECLAIM) {
4086 error = NFSERR_NOGRACE;
4090 if (!(flags & NFSLCK_RECLAIM)) {
4091 error = NFSERR_GRACE;
4094 if (nd != NULL && clp != NULL &&
4095 (nd->nd_flag & ND_NFSV41) != 0 &&
4096 (clp->lc_flags & LCL_RECLAIMCOMPLETE) != 0) {
4097 error = NFSERR_NOGRACE;
4102 * If grace is almost over and we are still getting Reclaims,
4103 * extend grace a bit.
4105 if ((NFSD_MONOSEC + NFSRV_LEASEDELTA) >
4106 nfsrv_stablefirst.nsf_eograce)
4107 nfsrv_stablefirst.nsf_eograce = NFSD_MONOSEC +
4117 * Do a server callback.
4120 nfsrv_docallback(struct nfsclient *clp, int procnum,
4121 nfsv4stateid_t *stateidp, int trunc, fhandle_t *fhp,
4122 struct nfsvattr *nap, nfsattrbit_t *attrbitp, NFSPROC_T *p)
4126 struct nfsrv_descript nfsd, *nd = &nfsd;
4130 struct nfsdsession *sep = NULL;
4132 cred = newnfs_getcred();
4133 NFSLOCKSTATE(); /* mostly for lc_cbref++ */
4134 if (clp->lc_flags & LCL_NEEDSCONFIRM) {
4141 * Fill the callback program# and version into the request
4142 * structure for newnfs_connect() to use.
4144 clp->lc_req.nr_prog = clp->lc_program;
4146 if ((clp->lc_flags & LCL_NFSV41) != 0)
4147 clp->lc_req.nr_vers = NFSV41_CBVERS;
4150 clp->lc_req.nr_vers = NFSV4_CBVERS;
4153 * First, fill in some of the fields of nd and cr.
4155 nd->nd_flag = ND_NFSV4;
4156 if (clp->lc_flags & LCL_GSS)
4157 nd->nd_flag |= ND_KERBV;
4158 if ((clp->lc_flags & LCL_NFSV41) != 0)
4159 nd->nd_flag |= ND_NFSV41;
4161 cred->cr_uid = clp->lc_uid;
4162 cred->cr_gid = clp->lc_gid;
4163 callback = clp->lc_callback;
4165 cred->cr_ngroups = 1;
4168 * Get the first mbuf for the request.
4170 MGET(m, M_WAITOK, MT_DATA);
4172 nd->nd_mreq = nd->nd_mb = m;
4173 nd->nd_bpos = NFSMTOD(m, caddr_t);
4176 * and build the callback request.
4178 if (procnum == NFSV4OP_CBGETATTR) {
4179 nd->nd_procnum = NFSV4PROC_CBCOMPOUND;
4180 error = nfsrv_cbcallargs(nd, clp, callback, NFSV4OP_CBGETATTR,
4181 "CB Getattr", &sep);
4183 mbuf_freem(nd->nd_mreq);
4186 (void)nfsm_fhtom(nd, (u_int8_t *)fhp, NFSX_MYFH, 0);
4187 (void)nfsrv_putattrbit(nd, attrbitp);
4188 } else if (procnum == NFSV4OP_CBRECALL) {
4189 nd->nd_procnum = NFSV4PROC_CBCOMPOUND;
4190 error = nfsrv_cbcallargs(nd, clp, callback, NFSV4OP_CBRECALL,
4193 mbuf_freem(nd->nd_mreq);
4196 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED + NFSX_STATEID);
4197 *tl++ = txdr_unsigned(stateidp->seqid);
4198 NFSBCOPY((caddr_t)stateidp->other, (caddr_t)tl,
4200 tl += (NFSX_STATEIDOTHER / NFSX_UNSIGNED);
4205 (void)nfsm_fhtom(nd, (u_int8_t *)fhp, NFSX_MYFH, 0);
4206 } else if (procnum == NFSV4PROC_CBNULL) {
4207 nd->nd_procnum = NFSV4PROC_CBNULL;
4208 if ((clp->lc_flags & LCL_NFSV41) != 0) {
4209 error = nfsv4_getcbsession(clp, &sep);
4211 mbuf_freem(nd->nd_mreq);
4216 error = NFSERR_SERVERFAULT;
4217 mbuf_freem(nd->nd_mreq);
4222 * Call newnfs_connect(), as required, and then newnfs_request().
4224 (void) newnfs_sndlock(&clp->lc_req.nr_lock);
4225 if (clp->lc_req.nr_client == NULL) {
4226 if ((clp->lc_flags & LCL_NFSV41) != 0)
4227 error = ECONNREFUSED;
4228 else if (nd->nd_procnum == NFSV4PROC_CBNULL)
4229 error = newnfs_connect(NULL, &clp->lc_req, cred,
4232 error = newnfs_connect(NULL, &clp->lc_req, cred,
4235 newnfs_sndunlock(&clp->lc_req.nr_lock);
4237 if ((nd->nd_flag & ND_NFSV41) != 0) {
4238 KASSERT(sep != NULL, ("sep NULL"));
4239 if (sep->sess_cbsess.nfsess_xprt != NULL)
4240 error = newnfs_request(nd, NULL, clp,
4241 &clp->lc_req, NULL, NULL, cred,
4242 clp->lc_program, clp->lc_req.nr_vers, NULL,
4243 1, NULL, &sep->sess_cbsess);
4246 * This should probably never occur, but if a
4247 * client somehow does an RPC without a
4248 * SequenceID Op that causes a callback just
4249 * after the nfsd threads have been terminated
4250 * and restared we could conceivably get here
4251 * without a backchannel xprt.
4253 printf("nfsrv_docallback: no xprt\n");
4254 error = ECONNREFUSED;
4256 nfsrv_freesession(sep, NULL);
4258 error = newnfs_request(nd, NULL, clp, &clp->lc_req,
4259 NULL, NULL, cred, clp->lc_program,
4260 clp->lc_req.nr_vers, NULL, 1, NULL, NULL);
4266 * If error is set here, the Callback path isn't working
4267 * properly, so twiddle the appropriate LCL_ flags.
4268 * (nd_repstat != 0 indicates the Callback path is working,
4269 * but the callback failed on the client.)
4273 * Mark the callback pathway down, which disabled issuing
4274 * of delegations and gets Renew to return NFSERR_CBPATHDOWN.
4277 clp->lc_flags |= LCL_CBDOWN;
4281 * Callback worked. If the callback path was down, disable
4282 * callbacks, so no more delegations will be issued. (This
4283 * is done on the assumption that the callback pathway is
4287 if (clp->lc_flags & LCL_CBDOWN)
4288 clp->lc_flags &= ~(LCL_CBDOWN | LCL_CALLBACKSON);
4291 error = nd->nd_repstat;
4292 else if (error == 0 && procnum == NFSV4OP_CBGETATTR)
4293 error = nfsv4_loadattr(nd, NULL, nap, NULL, NULL, 0,
4294 NULL, NULL, NULL, NULL, NULL, 0, NULL, NULL, NULL,
4296 mbuf_freem(nd->nd_mrep);
4300 if ((clp->lc_flags & LCL_WAKEUPWANTED) && clp->lc_cbref == 0) {
4301 clp->lc_flags &= ~LCL_WAKEUPWANTED;
4311 * Set up the compound RPC for the callback.
4314 nfsrv_cbcallargs(struct nfsrv_descript *nd, struct nfsclient *clp,
4315 uint32_t callback, int op, const char *optag, struct nfsdsession **sepp)
4320 len = strlen(optag);
4321 (void)nfsm_strtom(nd, optag, len);
4322 NFSM_BUILD(tl, uint32_t *, 4 * NFSX_UNSIGNED);
4323 if ((nd->nd_flag & ND_NFSV41) != 0) {
4324 *tl++ = txdr_unsigned(NFSV41_MINORVERSION);
4325 *tl++ = txdr_unsigned(callback);
4326 *tl++ = txdr_unsigned(2);
4327 *tl = txdr_unsigned(NFSV4OP_CBSEQUENCE);
4328 error = nfsv4_setcbsequence(nd, clp, 1, sepp);
4331 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
4332 *tl = txdr_unsigned(op);
4334 *tl++ = txdr_unsigned(NFSV4_MINORVERSION);
4335 *tl++ = txdr_unsigned(callback);
4336 *tl++ = txdr_unsigned(1);
4337 *tl = txdr_unsigned(op);
4343 * Return the next index# for a clientid. Mostly just increment and return
4344 * the next one, but... if the 32bit unsigned does actually wrap around,
4345 * it should be rebooted.
4346 * At an average rate of one new client per second, it will wrap around in
4347 * approximately 136 years. (I think the server will have been shut
4348 * down or rebooted before then.)
4351 nfsrv_nextclientindex(void)
4353 static u_int32_t client_index = 0;
4356 if (client_index != 0)
4357 return (client_index);
4359 printf("%s: out of clientids\n", __func__);
4360 return (client_index);
4364 * Return the next index# for a stateid. Mostly just increment and return
4365 * the next one, but... if the 32bit unsigned does actually wrap around
4366 * (will a BSD server stay up that long?), find
4367 * new start and end values.
4370 nfsrv_nextstateindex(struct nfsclient *clp)
4372 struct nfsstate *stp;
4374 u_int32_t canuse, min_index, max_index;
4376 if (!(clp->lc_flags & LCL_INDEXNOTOK)) {
4377 clp->lc_stateindex++;
4378 if (clp->lc_stateindex != clp->lc_statemaxindex)
4379 return (clp->lc_stateindex);
4383 * Yuck, we've hit the end.
4384 * Look for a new min and max.
4387 max_index = 0xffffffff;
4388 for (i = 0; i < nfsrv_statehashsize; i++) {
4389 LIST_FOREACH(stp, &clp->lc_stateid[i], ls_hash) {
4390 if (stp->ls_stateid.other[2] > 0x80000000) {
4391 if (stp->ls_stateid.other[2] < max_index)
4392 max_index = stp->ls_stateid.other[2];
4394 if (stp->ls_stateid.other[2] > min_index)
4395 min_index = stp->ls_stateid.other[2];
4401 * Yikes, highly unlikely, but I'll handle it anyhow.
4403 if (min_index == 0x80000000 && max_index == 0x80000001) {
4406 * Loop around until we find an unused entry. Return that
4407 * and set LCL_INDEXNOTOK, so the search will continue next time.
4408 * (This is one of those rare cases where a goto is the
4409 * cleanest way to code the loop.)
4412 for (i = 0; i < nfsrv_statehashsize; i++) {
4413 LIST_FOREACH(stp, &clp->lc_stateid[i], ls_hash) {
4414 if (stp->ls_stateid.other[2] == canuse) {
4420 clp->lc_flags |= LCL_INDEXNOTOK;
4425 * Ok to start again from min + 1.
4427 clp->lc_stateindex = min_index + 1;
4428 clp->lc_statemaxindex = max_index;
4429 clp->lc_flags &= ~LCL_INDEXNOTOK;
4430 return (clp->lc_stateindex);
4434 * The following functions handle the stable storage file that deals with
4435 * the edge conditions described in RFC3530 Sec. 8.6.3.
4436 * The file is as follows:
4437 * - a single record at the beginning that has the lease time of the
4438 * previous server instance (before the last reboot) and the nfsrvboottime
4439 * values for the previous server boots.
4440 * These previous boot times are used to ensure that the current
4441 * nfsrvboottime does not, somehow, get set to a previous one.
4442 * (This is important so that Stale ClientIDs and StateIDs can
4444 * The number of previous nfsvrboottime values precedes the list.
4445 * - followed by some number of appended records with:
4446 * - client id string
4447 * - flag that indicates it is a record revoking state via lease
4448 * expiration or similar
4449 * OR has successfully acquired state.
4450 * These structures vary in length, with the client string at the end, up
4451 * to NFSV4_OPAQUELIMIT in size.
4453 * At the end of the grace period, the file is truncated, the first
4454 * record is rewritten with updated information and any acquired state
4455 * records for successful reclaims of state are written.
4457 * Subsequent records are appended when the first state is issued to
4458 * a client and when state is revoked for a client.
4460 * When reading the file in, state issued records that come later in
4461 * the file override older ones, since the append log is in cronological order.
4462 * If, for some reason, the file can't be read, the grace period is
4463 * immediately terminated and all reclaims get NFSERR_NOGRACE.
4467 * Read in the stable storage file. Called by nfssvc() before the nfsd
4468 * processes start servicing requests.
4471 nfsrv_setupstable(NFSPROC_T *p)
4473 struct nfsrv_stablefirst *sf = &nfsrv_stablefirst;
4474 struct nfsrv_stable *sp, *nsp;
4475 struct nfst_rec *tsp;
4476 int error, i, tryagain;
4478 ssize_t aresid, len;
4481 * If NFSNSF_UPDATEDONE is set, this is a restart of the nfsds without
4482 * a reboot, so state has not been lost.
4484 if (sf->nsf_flags & NFSNSF_UPDATEDONE)
4487 * Set Grace over just until the file reads successfully.
4489 nfsrvboottime = time_second;
4490 LIST_INIT(&sf->nsf_head);
4491 sf->nsf_flags = (NFSNSF_GRACEOVER | NFSNSF_NEEDLOCK);
4492 sf->nsf_eograce = NFSD_MONOSEC + NFSRV_LEASEDELTA;
4493 if (sf->nsf_fp == NULL)
4495 error = NFSD_RDWR(UIO_READ, NFSFPVNODE(sf->nsf_fp),
4496 (caddr_t)&sf->nsf_rec, sizeof (struct nfsf_rec), off, UIO_SYSSPACE,
4497 0, NFSFPCRED(sf->nsf_fp), &aresid, p);
4498 if (error || aresid || sf->nsf_numboots == 0 ||
4499 sf->nsf_numboots > NFSNSF_MAXNUMBOOTS)
4503 * Now, read in the boottimes.
4505 sf->nsf_bootvals = (time_t *)malloc((sf->nsf_numboots + 1) *
4506 sizeof (time_t), M_TEMP, M_WAITOK);
4507 off = sizeof (struct nfsf_rec);
4508 error = NFSD_RDWR(UIO_READ, NFSFPVNODE(sf->nsf_fp),
4509 (caddr_t)sf->nsf_bootvals, sf->nsf_numboots * sizeof (time_t), off,
4510 UIO_SYSSPACE, 0, NFSFPCRED(sf->nsf_fp), &aresid, p);
4511 if (error || aresid) {
4512 free((caddr_t)sf->nsf_bootvals, M_TEMP);
4513 sf->nsf_bootvals = NULL;
4518 * Make sure this nfsrvboottime is different from all recorded
4523 for (i = 0; i < sf->nsf_numboots; i++) {
4524 if (nfsrvboottime == sf->nsf_bootvals[i]) {
4532 sf->nsf_flags |= NFSNSF_OK;
4533 off += (sf->nsf_numboots * sizeof (time_t));
4536 * Read through the file, building a list of records for grace
4538 * Each record is between sizeof (struct nfst_rec) and
4539 * sizeof (struct nfst_rec) + NFSV4_OPAQUELIMIT - 1
4540 * and is actually sizeof (struct nfst_rec) + nst_len - 1.
4542 tsp = (struct nfst_rec *)malloc(sizeof (struct nfst_rec) +
4543 NFSV4_OPAQUELIMIT - 1, M_TEMP, M_WAITOK);
4545 error = NFSD_RDWR(UIO_READ, NFSFPVNODE(sf->nsf_fp),
4546 (caddr_t)tsp, sizeof (struct nfst_rec) + NFSV4_OPAQUELIMIT - 1,
4547 off, UIO_SYSSPACE, 0, NFSFPCRED(sf->nsf_fp), &aresid, p);
4548 len = (sizeof (struct nfst_rec) + NFSV4_OPAQUELIMIT - 1) - aresid;
4549 if (error || (len > 0 && (len < sizeof (struct nfst_rec) ||
4550 len < (sizeof (struct nfst_rec) + tsp->len - 1)))) {
4552 * Yuck, the file has been corrupted, so just return
4553 * after clearing out any restart state, so the grace period
4556 LIST_FOREACH_SAFE(sp, &sf->nsf_head, nst_list, nsp) {
4557 LIST_REMOVE(sp, nst_list);
4558 free((caddr_t)sp, M_TEMP);
4560 free((caddr_t)tsp, M_TEMP);
4561 sf->nsf_flags &= ~NFSNSF_OK;
4562 free((caddr_t)sf->nsf_bootvals, M_TEMP);
4563 sf->nsf_bootvals = NULL;
4567 off += sizeof (struct nfst_rec) + tsp->len - 1;
4569 * Search the list for a matching client.
4571 LIST_FOREACH(sp, &sf->nsf_head, nst_list) {
4572 if (tsp->len == sp->nst_len &&
4573 !NFSBCMP(tsp->client, sp->nst_client, tsp->len))
4576 if (sp == LIST_END(&sf->nsf_head)) {
4577 sp = (struct nfsrv_stable *)malloc(tsp->len +
4578 sizeof (struct nfsrv_stable) - 1, M_TEMP,
4580 NFSBCOPY((caddr_t)tsp, (caddr_t)&sp->nst_rec,
4581 sizeof (struct nfst_rec) + tsp->len - 1);
4582 LIST_INSERT_HEAD(&sf->nsf_head, sp, nst_list);
4584 if (tsp->flag == NFSNST_REVOKE)
4585 sp->nst_flag |= NFSNST_REVOKE;
4588 * A subsequent timestamp indicates the client
4589 * did a setclientid/confirm and any previous
4590 * revoke is no longer relevant.
4592 sp->nst_flag &= ~NFSNST_REVOKE;
4596 free((caddr_t)tsp, M_TEMP);
4597 sf->nsf_flags = NFSNSF_OK;
4598 sf->nsf_eograce = NFSD_MONOSEC + sf->nsf_lease +
4603 * Update the stable storage file, now that the grace period is over.
4606 nfsrv_updatestable(NFSPROC_T *p)
4608 struct nfsrv_stablefirst *sf = &nfsrv_stablefirst;
4609 struct nfsrv_stable *sp, *nsp;
4611 struct nfsvattr nva;
4613 #if defined(__FreeBSD_version) && (__FreeBSD_version >= 500000)
4618 if (sf->nsf_fp == NULL || (sf->nsf_flags & NFSNSF_UPDATEDONE))
4620 sf->nsf_flags |= NFSNSF_UPDATEDONE;
4622 * Ok, we need to rewrite the stable storage file.
4623 * - truncate to 0 length
4624 * - write the new first structure
4625 * - loop through the data structures, writing out any that
4626 * have timestamps older than the old boot
4628 if (sf->nsf_bootvals) {
4630 for (i = sf->nsf_numboots - 2; i >= 0; i--)
4631 sf->nsf_bootvals[i + 1] = sf->nsf_bootvals[i];
4633 sf->nsf_numboots = 1;
4634 sf->nsf_bootvals = (time_t *)malloc(sizeof (time_t),
4637 sf->nsf_bootvals[0] = nfsrvboottime;
4638 sf->nsf_lease = nfsrv_lease;
4639 NFSVNO_ATTRINIT(&nva);
4640 NFSVNO_SETATTRVAL(&nva, size, 0);
4641 vp = NFSFPVNODE(sf->nsf_fp);
4642 vn_start_write(vp, &mp, V_WAIT);
4643 if (NFSVOPLOCK(vp, LK_EXCLUSIVE) == 0) {
4644 error = nfsvno_setattr(vp, &nva, NFSFPCRED(sf->nsf_fp), p,
4646 NFSVOPUNLOCK(vp, 0);
4649 vn_finished_write(mp);
4651 error = NFSD_RDWR(UIO_WRITE, vp,
4652 (caddr_t)&sf->nsf_rec, sizeof (struct nfsf_rec), (off_t)0,
4653 UIO_SYSSPACE, IO_SYNC, NFSFPCRED(sf->nsf_fp), NULL, p);
4655 error = NFSD_RDWR(UIO_WRITE, vp,
4656 (caddr_t)sf->nsf_bootvals,
4657 sf->nsf_numboots * sizeof (time_t),
4658 (off_t)(sizeof (struct nfsf_rec)),
4659 UIO_SYSSPACE, IO_SYNC, NFSFPCRED(sf->nsf_fp), NULL, p);
4660 free((caddr_t)sf->nsf_bootvals, M_TEMP);
4661 sf->nsf_bootvals = NULL;
4663 sf->nsf_flags &= ~NFSNSF_OK;
4664 printf("EEK! Can't write NfsV4 stable storage file\n");
4667 sf->nsf_flags |= NFSNSF_OK;
4670 * Loop through the list and write out timestamp records for
4671 * any clients that successfully reclaimed state.
4673 LIST_FOREACH_SAFE(sp, &sf->nsf_head, nst_list, nsp) {
4674 if (sp->nst_flag & NFSNST_GOTSTATE) {
4675 nfsrv_writestable(sp->nst_client, sp->nst_len,
4676 NFSNST_NEWSTATE, p);
4677 sp->nst_clp->lc_flags |= LCL_STAMPEDSTABLE;
4679 LIST_REMOVE(sp, nst_list);
4680 free((caddr_t)sp, M_TEMP);
4682 nfsrv_backupstable();
4686 * Append a record to the stable storage file.
4689 nfsrv_writestable(u_char *client, int len, int flag, NFSPROC_T *p)
4691 struct nfsrv_stablefirst *sf = &nfsrv_stablefirst;
4692 struct nfst_rec *sp;
4695 if (!(sf->nsf_flags & NFSNSF_OK) || sf->nsf_fp == NULL)
4697 sp = (struct nfst_rec *)malloc(sizeof (struct nfst_rec) +
4698 len - 1, M_TEMP, M_WAITOK);
4700 NFSBCOPY(client, sp->client, len);
4702 error = NFSD_RDWR(UIO_WRITE, NFSFPVNODE(sf->nsf_fp),
4703 (caddr_t)sp, sizeof (struct nfst_rec) + len - 1, (off_t)0,
4704 UIO_SYSSPACE, (IO_SYNC | IO_APPEND), NFSFPCRED(sf->nsf_fp), NULL, p);
4705 free((caddr_t)sp, M_TEMP);
4707 sf->nsf_flags &= ~NFSNSF_OK;
4708 printf("EEK! Can't write NfsV4 stable storage file\n");
4713 * This function is called during the grace period to mark a client
4714 * that successfully reclaimed state.
4717 nfsrv_markstable(struct nfsclient *clp)
4719 struct nfsrv_stable *sp;
4722 * First find the client structure.
4724 LIST_FOREACH(sp, &nfsrv_stablefirst.nsf_head, nst_list) {
4725 if (sp->nst_len == clp->lc_idlen &&
4726 !NFSBCMP(sp->nst_client, clp->lc_id, sp->nst_len))
4729 if (sp == LIST_END(&nfsrv_stablefirst.nsf_head))
4733 * Now, just mark it and set the nfsclient back pointer.
4735 sp->nst_flag |= NFSNST_GOTSTATE;
4740 * This function is called for a reclaim, to see if it gets grace.
4741 * It returns 0 if a reclaim is allowed, 1 otherwise.
4744 nfsrv_checkstable(struct nfsclient *clp)
4746 struct nfsrv_stable *sp;
4749 * First, find the entry for the client.
4751 LIST_FOREACH(sp, &nfsrv_stablefirst.nsf_head, nst_list) {
4752 if (sp->nst_len == clp->lc_idlen &&
4753 !NFSBCMP(sp->nst_client, clp->lc_id, sp->nst_len))
4758 * If not in the list, state was revoked or no state was issued
4759 * since the previous reboot, a reclaim is denied.
4761 if (sp == LIST_END(&nfsrv_stablefirst.nsf_head) ||
4762 (sp->nst_flag & NFSNST_REVOKE) ||
4763 !(nfsrv_stablefirst.nsf_flags & NFSNSF_OK))
4769 * Test for and try to clear out a conflicting client. This is called by
4770 * nfsrv_lockctrl() and nfsrv_openctrl() when conflicts with other clients
4772 * The trick here is that it can't revoke a conflicting client with an
4773 * expired lease unless it holds the v4root lock, so...
4774 * If no v4root lock, get the lock and return 1 to indicate "try again".
4775 * Return 0 to indicate the conflict can't be revoked and 1 to indicate
4776 * the revocation worked and the conflicting client is "bye, bye", so it
4777 * can be tried again.
4778 * Return 2 to indicate that the vnode is VI_DOOMED after NFSVOPLOCK().
4779 * Unlocks State before a non-zero value is returned.
4782 nfsrv_clientconflict(struct nfsclient *clp, int *haslockp, vnode_t vp,
4785 int gotlock, lktype = 0;
4788 * If lease hasn't expired, we can't fix it.
4790 if (clp->lc_expiry >= NFSD_MONOSEC ||
4791 !(nfsrv_stablefirst.nsf_flags & NFSNSF_UPDATEDONE))
4793 if (*haslockp == 0) {
4796 lktype = NFSVOPISLOCKED(vp);
4797 NFSVOPUNLOCK(vp, 0);
4799 NFSLOCKV4ROOTMUTEX();
4800 nfsv4_relref(&nfsv4rootfs_lock);
4802 gotlock = nfsv4_lock(&nfsv4rootfs_lock, 1, NULL,
4803 NFSV4ROOTLOCKMUTEXPTR, NULL);
4805 NFSUNLOCKV4ROOTMUTEX();
4808 NFSVOPLOCK(vp, lktype | LK_RETRY);
4809 if ((vp->v_iflag & VI_DOOMED) != 0)
4817 * Ok, we can expire the conflicting client.
4819 nfsrv_writestable(clp->lc_id, clp->lc_idlen, NFSNST_REVOKE, p);
4820 nfsrv_backupstable();
4821 nfsrv_cleanclient(clp, p);
4822 nfsrv_freedeleglist(&clp->lc_deleg);
4823 nfsrv_freedeleglist(&clp->lc_olddeleg);
4824 LIST_REMOVE(clp, lc_hash);
4825 nfsrv_zapclient(clp, p);
4830 * Resolve a delegation conflict.
4831 * Returns 0 to indicate the conflict was resolved without sleeping.
4832 * Return -1 to indicate that the caller should check for conflicts again.
4833 * Return > 0 for an error that should be returned, normally NFSERR_DELAY.
4835 * Also, manipulate the nfsv4root_lock, as required. It isn't changed
4836 * for a return of 0, since there was no sleep and it could be required
4837 * later. It is released for a return of NFSERR_DELAY, since the caller
4838 * will return that error. It is released when a sleep was done waiting
4839 * for the delegation to be returned or expire (so that other nfsds can
4840 * handle ops). Then, it must be acquired for the write to stable storage.
4841 * (This function is somewhat similar to nfsrv_clientconflict(), but
4842 * the semantics differ in a couple of subtle ways. The return of 0
4843 * indicates the conflict was resolved without sleeping here, not
4844 * that the conflict can't be resolved and the handling of nfsv4root_lock
4845 * differs, as noted above.)
4846 * Unlocks State before returning a non-zero value.
4849 nfsrv_delegconflict(struct nfsstate *stp, int *haslockp, NFSPROC_T *p,
4852 struct nfsclient *clp = stp->ls_clp;
4853 int gotlock, error, lktype = 0, retrycnt, zapped_clp;
4854 nfsv4stateid_t tstateid;
4858 * If the conflict is with an old delegation...
4860 if (stp->ls_flags & NFSLCK_OLDDELEG) {
4862 * You can delete it, if it has expired.
4864 if (clp->lc_delegtime < NFSD_MONOSEC) {
4865 nfsrv_freedeleg(stp);
4872 * During this delay, the old delegation could expire or it
4873 * could be recovered by the client via an Open with
4874 * CLAIM_DELEGATE_PREV.
4875 * Release the nfsv4root_lock, if held.
4879 NFSLOCKV4ROOTMUTEX();
4880 nfsv4_unlock(&nfsv4rootfs_lock, 1);
4881 NFSUNLOCKV4ROOTMUTEX();
4883 error = NFSERR_DELAY;
4888 * It's a current delegation, so:
4889 * - check to see if the delegation has expired
4890 * - if so, get the v4root lock and then expire it
4892 if (!(stp->ls_flags & NFSLCK_DELEGRECALL)) {
4894 * - do a recall callback, since not yet done
4895 * For now, never allow truncate to be set. To use
4896 * truncate safely, it must be guaranteed that the
4897 * Remove, Rename or Setattr with size of 0 will
4898 * succeed and that would require major changes to
4899 * the VFS/Vnode OPs.
4900 * Set the expiry time large enough so that it won't expire
4901 * until after the callback, then set it correctly, once
4902 * the callback is done. (The delegation will now time
4903 * out whether or not the Recall worked ok. The timeout
4904 * will be extended when ops are done on the delegation
4905 * stateid, up to the timelimit.)
4907 stp->ls_delegtime = NFSD_MONOSEC + (2 * nfsrv_lease) +
4909 stp->ls_delegtimelimit = NFSD_MONOSEC + (6 * nfsrv_lease) +
4911 stp->ls_flags |= NFSLCK_DELEGRECALL;
4914 * Loop NFSRV_CBRETRYCNT times while the CBRecall replies
4915 * NFSERR_BADSTATEID or NFSERR_BADHANDLE. This is done
4916 * in order to try and avoid a race that could happen
4917 * when a CBRecall request passed the Open reply with
4918 * the delegation in it when transitting the network.
4919 * Since nfsrv_docallback will sleep, don't use stp after
4922 NFSBCOPY((caddr_t)&stp->ls_stateid, (caddr_t)&tstateid,
4924 NFSBCOPY((caddr_t)&stp->ls_lfp->lf_fh, (caddr_t)&tfh,
4929 NFSLOCKV4ROOTMUTEX();
4930 nfsv4_unlock(&nfsv4rootfs_lock, 1);
4931 NFSUNLOCKV4ROOTMUTEX();
4935 error = nfsrv_docallback(clp, NFSV4OP_CBRECALL,
4936 &tstateid, 0, &tfh, NULL, NULL, p);
4938 } while ((error == NFSERR_BADSTATEID ||
4939 error == NFSERR_BADHANDLE) && retrycnt < NFSV4_CBRETRYCNT);
4940 error = NFSERR_DELAY;
4944 if (clp->lc_expiry >= NFSD_MONOSEC &&
4945 stp->ls_delegtime >= NFSD_MONOSEC) {
4948 * A recall has been done, but it has not yet expired.
4953 NFSLOCKV4ROOTMUTEX();
4954 nfsv4_unlock(&nfsv4rootfs_lock, 1);
4955 NFSUNLOCKV4ROOTMUTEX();
4957 error = NFSERR_DELAY;
4962 * If we don't yet have the lock, just get it and then return,
4963 * since we need that before deleting expired state, such as
4965 * When getting the lock, unlock the vnode, so other nfsds that
4966 * are in progress, won't get stuck waiting for the vnode lock.
4968 if (*haslockp == 0) {
4971 lktype = NFSVOPISLOCKED(vp);
4972 NFSVOPUNLOCK(vp, 0);
4974 NFSLOCKV4ROOTMUTEX();
4975 nfsv4_relref(&nfsv4rootfs_lock);
4977 gotlock = nfsv4_lock(&nfsv4rootfs_lock, 1, NULL,
4978 NFSV4ROOTLOCKMUTEXPTR, NULL);
4980 NFSUNLOCKV4ROOTMUTEX();
4983 NFSVOPLOCK(vp, lktype | LK_RETRY);
4984 if ((vp->v_iflag & VI_DOOMED) != 0) {
4986 NFSLOCKV4ROOTMUTEX();
4987 nfsv4_unlock(&nfsv4rootfs_lock, 1);
4988 NFSUNLOCKV4ROOTMUTEX();
4989 error = NFSERR_PERM;
4999 * Ok, we can delete the expired delegation.
5000 * First, write the Revoke record to stable storage and then
5001 * clear out the conflict.
5002 * Since all other nfsd threads are now blocked, we can safely
5003 * sleep without the state changing.
5005 nfsrv_writestable(clp->lc_id, clp->lc_idlen, NFSNST_REVOKE, p);
5006 nfsrv_backupstable();
5007 if (clp->lc_expiry < NFSD_MONOSEC) {
5008 nfsrv_cleanclient(clp, p);
5009 nfsrv_freedeleglist(&clp->lc_deleg);
5010 nfsrv_freedeleglist(&clp->lc_olddeleg);
5011 LIST_REMOVE(clp, lc_hash);
5014 nfsrv_freedeleg(stp);
5018 nfsrv_zapclient(clp, p);
5027 * Check for a remove allowed, if remove is set to 1 and get rid of
5031 nfsrv_checkremove(vnode_t vp, int remove, NFSPROC_T *p)
5033 struct nfsstate *stp;
5034 struct nfslockfile *lfp;
5035 int error, haslock = 0;
5039 * First, get the lock file structure.
5040 * (A return of -1 means no associated state, so remove ok.)
5042 error = nfsrv_getlockfh(vp, NFSLCK_CHECK, NULL, &nfh, p);
5046 error = nfsrv_getlockfile(NFSLCK_CHECK, NULL, &lfp, &nfh, 0);
5050 NFSLOCKV4ROOTMUTEX();
5051 nfsv4_unlock(&nfsv4rootfs_lock, 1);
5052 NFSUNLOCKV4ROOTMUTEX();
5060 * Now, we must Recall any delegations.
5062 error = nfsrv_cleandeleg(vp, lfp, NULL, &haslock, p);
5065 * nfsrv_cleandeleg() unlocks state for non-zero
5071 NFSLOCKV4ROOTMUTEX();
5072 nfsv4_unlock(&nfsv4rootfs_lock, 1);
5073 NFSUNLOCKV4ROOTMUTEX();
5079 * Now, look for a conflicting open share.
5083 * If the entry in the directory was the last reference to the
5084 * corresponding filesystem object, the object can be destroyed
5086 if(lfp->lf_usecount>1)
5087 LIST_FOREACH(stp, &lfp->lf_open, ls_file) {
5088 if (stp->ls_flags & NFSLCK_WRITEDENY) {
5089 error = NFSERR_FILEOPEN;
5097 NFSLOCKV4ROOTMUTEX();
5098 nfsv4_unlock(&nfsv4rootfs_lock, 1);
5099 NFSUNLOCKV4ROOTMUTEX();
5108 * Clear out all delegations for the file referred to by lfp.
5109 * May return NFSERR_DELAY, if there will be a delay waiting for
5110 * delegations to expire.
5111 * Returns -1 to indicate it slept while recalling a delegation.
5112 * This function has the side effect of deleting the nfslockfile structure,
5113 * if it no longer has associated state and didn't have to sleep.
5114 * Unlocks State before a non-zero value is returned.
5117 nfsrv_cleandeleg(vnode_t vp, struct nfslockfile *lfp,
5118 struct nfsclient *clp, int *haslockp, NFSPROC_T *p)
5120 struct nfsstate *stp, *nstp;
5123 stp = LIST_FIRST(&lfp->lf_deleg);
5124 while (stp != LIST_END(&lfp->lf_deleg)) {
5125 nstp = LIST_NEXT(stp, ls_file);
5126 if (stp->ls_clp != clp) {
5127 ret = nfsrv_delegconflict(stp, haslockp, p, vp);
5130 * nfsrv_delegconflict() unlocks state
5131 * when it returns non-zero.
5144 * There are certain operations that, when being done outside of NFSv4,
5145 * require that any NFSv4 delegation for the file be recalled.
5146 * This function is to be called for those cases:
5147 * VOP_RENAME() - When a delegation is being recalled for any reason,
5148 * the client may have to do Opens against the server, using the file's
5149 * final component name. If the file has been renamed on the server,
5150 * that component name will be incorrect and the Open will fail.
5151 * VOP_REMOVE() - Theoretically, a client could Open a file after it has
5152 * been removed on the server, if there is a delegation issued to
5153 * that client for the file. I say "theoretically" since clients
5154 * normally do an Access Op before the Open and that Access Op will
5155 * fail with ESTALE. Note that NFSv2 and 3 don't even do Opens, so
5156 * they will detect the file's removal in the same manner. (There is
5157 * one case where RFC3530 allows a client to do an Open without first
5158 * doing an Access Op, which is passage of a check against the ACE
5159 * returned with a Write delegation, but current practice is to ignore
5160 * the ACE and always do an Access Op.)
5161 * Since the functions can only be called with an unlocked vnode, this
5162 * can't be done at this time.
5163 * VOP_ADVLOCK() - When a client holds a delegation, it can issue byte range
5164 * locks locally in the client, which are not visible to the server. To
5165 * deal with this, issuing of delegations for a vnode must be disabled
5166 * and all delegations for the vnode recalled. This is done via the
5167 * second function, using the VV_DISABLEDELEG vflag on the vnode.
5170 nfsd_recalldelegation(vnode_t vp, NFSPROC_T *p)
5176 * First, check to see if the server is currently running and it has
5177 * been called for a regular file when issuing delegations.
5179 if (newnfs_numnfsd == 0 || vp->v_type != VREG ||
5180 nfsrv_issuedelegs == 0)
5183 KASSERT((NFSVOPISLOCKED(vp) != LK_EXCLUSIVE), ("vp %p is locked", vp));
5185 * First, get a reference on the nfsv4rootfs_lock so that an
5186 * exclusive lock cannot be acquired by another thread.
5188 NFSLOCKV4ROOTMUTEX();
5189 nfsv4_getref(&nfsv4rootfs_lock, NULL, NFSV4ROOTLOCKMUTEXPTR, NULL);
5190 NFSUNLOCKV4ROOTMUTEX();
5193 * Now, call nfsrv_checkremove() in a loop while it returns
5194 * NFSERR_DELAY. Return upon any other error or when timed out.
5196 starttime = NFSD_MONOSEC;
5198 if (NFSVOPLOCK(vp, LK_EXCLUSIVE) == 0) {
5199 error = nfsrv_checkremove(vp, 0, p);
5200 NFSVOPUNLOCK(vp, 0);
5203 if (error == NFSERR_DELAY) {
5204 if (NFSD_MONOSEC - starttime > NFS_REMOVETIMEO)
5206 /* Sleep for a short period of time */
5207 (void) nfs_catnap(PZERO, 0, "nfsremove");
5209 } while (error == NFSERR_DELAY);
5210 NFSLOCKV4ROOTMUTEX();
5211 nfsv4_relref(&nfsv4rootfs_lock);
5212 NFSUNLOCKV4ROOTMUTEX();
5216 nfsd_disabledelegation(vnode_t vp, NFSPROC_T *p)
5219 #ifdef VV_DISABLEDELEG
5221 * First, flag issuance of delegations disabled.
5223 atomic_set_long(&vp->v_vflag, VV_DISABLEDELEG);
5227 * Then call nfsd_recalldelegation() to get rid of all extant
5230 nfsd_recalldelegation(vp, p);
5234 * Check for conflicting locks, etc. and then get rid of delegations.
5235 * (At one point I thought that I should get rid of delegations for any
5236 * Setattr, since it could potentially disallow the I/O op (read or write)
5237 * allowed by the delegation. However, Setattr Ops that aren't changing
5238 * the size get a stateid of all 0s, so you can't tell if it is a delegation
5239 * for the same client or a different one, so I decided to only get rid
5240 * of delegations for other clients when the size is being changed.)
5241 * In general, a Setattr can disable NFS I/O Ops that are outstanding, such
5242 * as Write backs, even if there is no delegation, so it really isn't any
5246 nfsrv_checksetattr(vnode_t vp, struct nfsrv_descript *nd,
5247 nfsv4stateid_t *stateidp, struct nfsvattr *nvap, nfsattrbit_t *attrbitp,
5248 struct nfsexstuff *exp, NFSPROC_T *p)
5250 struct nfsstate st, *stp = &st;
5251 struct nfslock lo, *lop = &lo;
5255 if (NFSISSET_ATTRBIT(attrbitp, NFSATTRBIT_SIZE)) {
5256 stp->ls_flags = (NFSLCK_CHECK | NFSLCK_WRITEACCESS);
5257 lop->lo_first = nvap->na_size;
5262 if (NFSISSET_ATTRBIT(attrbitp, NFSATTRBIT_OWNER) ||
5263 NFSISSET_ATTRBIT(attrbitp, NFSATTRBIT_OWNERGROUP) ||
5264 NFSISSET_ATTRBIT(attrbitp, NFSATTRBIT_MODE) ||
5265 NFSISSET_ATTRBIT(attrbitp, NFSATTRBIT_ACL))
5266 stp->ls_flags |= NFSLCK_SETATTR;
5267 if (stp->ls_flags == 0)
5269 lop->lo_end = NFS64BITSSET;
5270 lop->lo_flags = NFSLCK_WRITE;
5271 stp->ls_ownerlen = 0;
5273 stp->ls_uid = nd->nd_cred->cr_uid;
5274 stp->ls_stateid.seqid = stateidp->seqid;
5275 clientid.lval[0] = stp->ls_stateid.other[0] = stateidp->other[0];
5276 clientid.lval[1] = stp->ls_stateid.other[1] = stateidp->other[1];
5277 stp->ls_stateid.other[2] = stateidp->other[2];
5278 error = nfsrv_lockctrl(vp, &stp, &lop, NULL, clientid,
5279 stateidp, exp, nd, p);
5282 NFSEXITCODE2(error, nd);
5287 * Check for a write delegation and do a CBGETATTR if there is one, updating
5288 * the attributes, as required.
5289 * Should I return an error if I can't get the attributes? (For now, I'll
5293 nfsrv_checkgetattr(struct nfsrv_descript *nd, vnode_t vp,
5294 struct nfsvattr *nvap, nfsattrbit_t *attrbitp, struct ucred *cred,
5297 struct nfsstate *stp;
5298 struct nfslockfile *lfp;
5299 struct nfsclient *clp;
5300 struct nfsvattr nva;
5303 nfsattrbit_t cbbits;
5304 u_quad_t delegfilerev;
5306 NFSCBGETATTR_ATTRBIT(attrbitp, &cbbits);
5307 if (!NFSNONZERO_ATTRBIT(&cbbits))
5309 if (nfsrv_writedelegcnt == 0)
5313 * Get the lock file structure.
5314 * (A return of -1 means no associated state, so return ok.)
5316 error = nfsrv_getlockfh(vp, NFSLCK_CHECK, NULL, &nfh, p);
5319 error = nfsrv_getlockfile(NFSLCK_CHECK, NULL, &lfp, &nfh, 0);
5328 * Now, look for a write delegation.
5330 LIST_FOREACH(stp, &lfp->lf_deleg, ls_file) {
5331 if (stp->ls_flags & NFSLCK_DELEGWRITE)
5334 if (stp == LIST_END(&lfp->lf_deleg)) {
5339 delegfilerev = stp->ls_filerev;
5342 * If the Write delegation was issued as a part of this Compound RPC
5343 * or if we have an Implied Clientid (used in a previous Op in this
5344 * compound) and it is the client the delegation was issued to,
5346 * I also assume that it is from the same client iff the network
5347 * host IP address is the same as the callback address. (Not
5348 * exactly correct by the RFC, but avoids a lot of Getattr
5351 if (nd->nd_compref == stp->ls_compref ||
5352 ((nd->nd_flag & ND_IMPLIEDCLID) &&
5353 clp->lc_clientid.qval == nd->nd_clientid.qval) ||
5354 nfsaddr2_match(clp->lc_req.nr_nam, nd->nd_nam)) {
5360 * We are now done with the delegation state structure,
5361 * so the statelock can be released and we can now tsleep().
5365 * Now, we must do the CB Getattr callback, to see if Change or Size
5368 if (clp->lc_expiry >= NFSD_MONOSEC) {
5370 NFSVNO_ATTRINIT(&nva);
5371 nva.na_filerev = NFS64BITSSET;
5372 error = nfsrv_docallback(clp, NFSV4OP_CBGETATTR, NULL,
5373 0, &nfh, &nva, &cbbits, p);
5375 if ((nva.na_filerev != NFS64BITSSET &&
5376 nva.na_filerev > delegfilerev) ||
5377 (NFSVNO_ISSETSIZE(&nva) &&
5378 nva.na_size != nvap->na_size)) {
5379 error = nfsvno_updfilerev(vp, nvap, cred, p);
5380 if (NFSVNO_ISSETSIZE(&nva))
5381 nvap->na_size = nva.na_size;
5384 error = 0; /* Ignore callback errors for now. */
5390 NFSEXITCODE2(error, nd);
5395 * This function looks for openowners that haven't had any opens for
5396 * a while and throws them away. Called by an nfsd when NFSNSF_NOOPENS
5400 nfsrv_throwawayopens(NFSPROC_T *p)
5402 struct nfsclient *clp, *nclp;
5403 struct nfsstate *stp, *nstp;
5407 nfsrv_stablefirst.nsf_flags &= ~NFSNSF_NOOPENS;
5409 * For each client...
5411 for (i = 0; i < nfsrv_clienthashsize; i++) {
5412 LIST_FOREACH_SAFE(clp, &nfsclienthash[i], lc_hash, nclp) {
5413 LIST_FOREACH_SAFE(stp, &clp->lc_open, ls_list, nstp) {
5414 if (LIST_EMPTY(&stp->ls_open) &&
5415 (stp->ls_noopens > NFSNOOPEN ||
5416 (nfsrv_openpluslock * 2) >
5417 nfsrv_v4statelimit))
5418 nfsrv_freeopenowner(stp, 0, p);
5426 * This function checks to see if the credentials are the same.
5427 * Returns 1 for not same, 0 otherwise.
5430 nfsrv_notsamecredname(struct nfsrv_descript *nd, struct nfsclient *clp)
5433 if (nd->nd_flag & ND_GSS) {
5434 if (!(clp->lc_flags & LCL_GSS))
5436 if (clp->lc_flags & LCL_NAME) {
5437 if (nd->nd_princlen != clp->lc_namelen ||
5438 NFSBCMP(nd->nd_principal, clp->lc_name,
5444 if (nd->nd_cred->cr_uid == clp->lc_uid)
5448 } else if (clp->lc_flags & LCL_GSS)
5451 * For AUTH_SYS, allow the same uid or root. (This is underspecified
5452 * in RFC3530, which talks about principals, but doesn't say anything
5453 * about uids for AUTH_SYS.)
5455 if (nd->nd_cred->cr_uid == clp->lc_uid || nd->nd_cred->cr_uid == 0)
5462 * Calculate the lease expiry time.
5465 nfsrv_leaseexpiry(void)
5468 if (nfsrv_stablefirst.nsf_eograce > NFSD_MONOSEC)
5469 return (NFSD_MONOSEC + 2 * (nfsrv_lease + NFSRV_LEASEDELTA));
5470 return (NFSD_MONOSEC + nfsrv_lease + NFSRV_LEASEDELTA);
5474 * Delay the delegation timeout as far as ls_delegtimelimit, as required.
5477 nfsrv_delaydelegtimeout(struct nfsstate *stp)
5480 if ((stp->ls_flags & NFSLCK_DELEGRECALL) == 0)
5483 if ((stp->ls_delegtime + 15) > NFSD_MONOSEC &&
5484 stp->ls_delegtime < stp->ls_delegtimelimit) {
5485 stp->ls_delegtime += nfsrv_lease;
5486 if (stp->ls_delegtime > stp->ls_delegtimelimit)
5487 stp->ls_delegtime = stp->ls_delegtimelimit;
5492 * This function checks to see if there is any other state associated
5493 * with the openowner for this Open.
5494 * It returns 1 if there is no other state, 0 otherwise.
5497 nfsrv_nootherstate(struct nfsstate *stp)
5499 struct nfsstate *tstp;
5501 LIST_FOREACH(tstp, &stp->ls_openowner->ls_open, ls_list) {
5502 if (tstp != stp || !LIST_EMPTY(&tstp->ls_lock))
5509 * Create a list of lock deltas (changes to local byte range locking
5510 * that can be rolled back using the list) and apply the changes via
5511 * nfsvno_advlock(). Optionally, lock the list. It is expected that either
5512 * the rollback or update function will be called after this.
5513 * It returns an error (and rolls back, as required), if any nfsvno_advlock()
5514 * call fails. If it returns an error, it will unlock the list.
5517 nfsrv_locallock(vnode_t vp, struct nfslockfile *lfp, int flags,
5518 uint64_t first, uint64_t end, struct nfslockconflict *cfp, NFSPROC_T *p)
5520 struct nfslock *lop, *nlop;
5523 /* Loop through the list of locks. */
5524 lop = LIST_FIRST(&lfp->lf_locallock);
5525 while (first < end && lop != NULL) {
5526 nlop = LIST_NEXT(lop, lo_lckowner);
5527 if (first >= lop->lo_end) {
5530 } else if (first < lop->lo_first) {
5531 /* new one starts before entry in list */
5532 if (end <= lop->lo_first) {
5533 /* no overlap between old and new */
5534 error = nfsrv_dolocal(vp, lfp, flags,
5535 NFSLCK_UNLOCK, first, end, cfp, p);
5540 /* handle fragment overlapped with new one */
5541 error = nfsrv_dolocal(vp, lfp, flags,
5542 NFSLCK_UNLOCK, first, lop->lo_first, cfp,
5546 first = lop->lo_first;
5549 /* new one overlaps this entry in list */
5550 if (end <= lop->lo_end) {
5551 /* overlaps all of new one */
5552 error = nfsrv_dolocal(vp, lfp, flags,
5553 lop->lo_flags, first, end, cfp, p);
5558 /* handle fragment overlapped with new one */
5559 error = nfsrv_dolocal(vp, lfp, flags,
5560 lop->lo_flags, first, lop->lo_end, cfp, p);
5563 first = lop->lo_end;
5568 if (first < end && error == 0)
5569 /* handle fragment past end of list */
5570 error = nfsrv_dolocal(vp, lfp, flags, NFSLCK_UNLOCK, first,
5578 * Local lock unlock. Unlock all byte ranges that are no longer locked
5579 * by NFSv4. To do this, unlock any subranges of first-->end that
5580 * do not overlap with the byte ranges of any lock in the lfp->lf_lock
5581 * list. This list has all locks for the file held by other
5582 * <clientid, lockowner> tuples. The list is ordered by increasing
5583 * lo_first value, but may have entries that overlap each other, for
5584 * the case of read locks.
5587 nfsrv_localunlock(vnode_t vp, struct nfslockfile *lfp, uint64_t init_first,
5588 uint64_t init_end, NFSPROC_T *p)
5590 struct nfslock *lop;
5591 uint64_t first, end, prevfirst;
5595 while (first < init_end) {
5596 /* Loop through all nfs locks, adjusting first and end */
5598 LIST_FOREACH(lop, &lfp->lf_lock, lo_lckfile) {
5599 KASSERT(prevfirst <= lop->lo_first,
5600 ("nfsv4 locks out of order"));
5601 KASSERT(lop->lo_first < lop->lo_end,
5602 ("nfsv4 bogus lock"));
5603 prevfirst = lop->lo_first;
5604 if (first >= lop->lo_first &&
5605 first < lop->lo_end)
5607 * Overlaps with initial part, so trim
5608 * off that initial part by moving first past
5611 first = lop->lo_end;
5612 else if (end > lop->lo_first &&
5613 lop->lo_first > first) {
5615 * This lock defines the end of the
5616 * segment to unlock, so set end to the
5617 * start of it and break out of the loop.
5619 end = lop->lo_first;
5624 * There is no segment left to do, so
5625 * break out of this loop and then exit
5626 * the outer while() since first will be set
5627 * to end, which must equal init_end here.
5632 /* Unlock this segment */
5633 (void) nfsrv_dolocal(vp, lfp, NFSLCK_UNLOCK,
5634 NFSLCK_READ, first, end, NULL, p);
5635 nfsrv_locallock_commit(lfp, NFSLCK_UNLOCK,
5639 * Now move past this segment and look for any further
5640 * segment in the range, if there is one.
5648 * Do the local lock operation and update the rollback list, as required.
5649 * Perform the rollback and return the error if nfsvno_advlock() fails.
5652 nfsrv_dolocal(vnode_t vp, struct nfslockfile *lfp, int flags, int oldflags,
5653 uint64_t first, uint64_t end, struct nfslockconflict *cfp, NFSPROC_T *p)
5655 struct nfsrollback *rlp;
5656 int error = 0, ltype, oldltype;
5658 if (flags & NFSLCK_WRITE)
5660 else if (flags & NFSLCK_READ)
5664 if (oldflags & NFSLCK_WRITE)
5666 else if (oldflags & NFSLCK_READ)
5670 if (ltype == oldltype || (oldltype == F_WRLCK && ltype == F_RDLCK))
5673 error = nfsvno_advlock(vp, ltype, first, end, p);
5676 cfp->cl_clientid.lval[0] = 0;
5677 cfp->cl_clientid.lval[1] = 0;
5679 cfp->cl_end = NFS64BITSSET;
5680 cfp->cl_flags = NFSLCK_WRITE;
5681 cfp->cl_ownerlen = 5;
5682 NFSBCOPY("LOCAL", cfp->cl_owner, 5);
5684 nfsrv_locallock_rollback(vp, lfp, p);
5685 } else if (ltype != F_UNLCK) {
5686 rlp = malloc(sizeof (struct nfsrollback), M_NFSDROLLBACK,
5688 rlp->rlck_first = first;
5689 rlp->rlck_end = end;
5690 rlp->rlck_type = oldltype;
5691 LIST_INSERT_HEAD(&lfp->lf_rollback, rlp, rlck_list);
5700 * Roll back local lock changes and free up the rollback list.
5703 nfsrv_locallock_rollback(vnode_t vp, struct nfslockfile *lfp, NFSPROC_T *p)
5705 struct nfsrollback *rlp, *nrlp;
5707 LIST_FOREACH_SAFE(rlp, &lfp->lf_rollback, rlck_list, nrlp) {
5708 (void) nfsvno_advlock(vp, rlp->rlck_type, rlp->rlck_first,
5710 free(rlp, M_NFSDROLLBACK);
5712 LIST_INIT(&lfp->lf_rollback);
5716 * Update local lock list and delete rollback list (ie now committed to the
5717 * local locks). Most of the work is done by the internal function.
5720 nfsrv_locallock_commit(struct nfslockfile *lfp, int flags, uint64_t first,
5723 struct nfsrollback *rlp, *nrlp;
5724 struct nfslock *new_lop, *other_lop;
5726 new_lop = malloc(sizeof (struct nfslock), M_NFSDLOCK, M_WAITOK);
5727 if (flags & (NFSLCK_READ | NFSLCK_WRITE))
5728 other_lop = malloc(sizeof (struct nfslock), M_NFSDLOCK,
5732 new_lop->lo_flags = flags;
5733 new_lop->lo_first = first;
5734 new_lop->lo_end = end;
5735 nfsrv_updatelock(NULL, &new_lop, &other_lop, lfp);
5736 if (new_lop != NULL)
5737 free(new_lop, M_NFSDLOCK);
5738 if (other_lop != NULL)
5739 free(other_lop, M_NFSDLOCK);
5741 /* and get rid of the rollback list */
5742 LIST_FOREACH_SAFE(rlp, &lfp->lf_rollback, rlck_list, nrlp)
5743 free(rlp, M_NFSDROLLBACK);
5744 LIST_INIT(&lfp->lf_rollback);
5748 * Lock the struct nfslockfile for local lock updating.
5751 nfsrv_locklf(struct nfslockfile *lfp)
5755 /* lf_usecount ensures *lfp won't be free'd */
5758 gotlock = nfsv4_lock(&lfp->lf_locallock_lck, 1, NULL,
5759 NFSSTATEMUTEXPTR, NULL);
5760 } while (gotlock == 0);
5765 * Unlock the struct nfslockfile after local lock updating.
5768 nfsrv_unlocklf(struct nfslockfile *lfp)
5771 nfsv4_unlock(&lfp->lf_locallock_lck, 0);
5775 * Clear out all state for the NFSv4 server.
5776 * Must be called by a thread that can sleep when no nfsds are running.
5779 nfsrv_throwawayallstate(NFSPROC_T *p)
5781 struct nfsclient *clp, *nclp;
5782 struct nfslockfile *lfp, *nlfp;
5786 * For each client, clean out the state and then free the structure.
5788 for (i = 0; i < nfsrv_clienthashsize; i++) {
5789 LIST_FOREACH_SAFE(clp, &nfsclienthash[i], lc_hash, nclp) {
5790 nfsrv_cleanclient(clp, p);
5791 nfsrv_freedeleglist(&clp->lc_deleg);
5792 nfsrv_freedeleglist(&clp->lc_olddeleg);
5793 free(clp->lc_stateid, M_NFSDCLIENT);
5794 free(clp, M_NFSDCLIENT);
5799 * Also, free up any remaining lock file structures.
5801 for (i = 0; i < nfsrv_lockhashsize; i++) {
5802 LIST_FOREACH_SAFE(lfp, &nfslockhash[i], lf_hash, nlfp)
5803 nfsrv_freenfslockfile(lfp);
5808 * Check the sequence# for the session and slot provided as an argument.
5809 * Also, renew the lease if the session will return NFS_OK.
5812 nfsrv_checksequence(struct nfsrv_descript *nd, uint32_t sequenceid,
5813 uint32_t *highest_slotidp, uint32_t *target_highest_slotidp, int cache_this,
5814 uint32_t *sflagsp, NFSPROC_T *p)
5816 struct nfsdsession *sep;
5817 struct nfssessionhash *shp;
5821 shp = NFSSESSIONHASH(nd->nd_sessionid);
5822 NFSLOCKSESSION(shp);
5823 sep = nfsrv_findsession(nd->nd_sessionid);
5825 NFSUNLOCKSESSION(shp);
5826 return (NFSERR_BADSESSION);
5828 error = nfsv4_seqsession(sequenceid, nd->nd_slotid, *highest_slotidp,
5829 sep->sess_slots, NULL, NFSV4_SLOTS - 1);
5831 NFSUNLOCKSESSION(shp);
5834 if (cache_this != 0)
5835 nd->nd_flag |= ND_SAVEREPLY;
5836 /* Renew the lease. */
5837 sep->sess_clp->lc_expiry = nfsrv_leaseexpiry();
5838 nd->nd_clientid.qval = sep->sess_clp->lc_clientid.qval;
5839 nd->nd_flag |= ND_IMPLIEDCLID;
5842 * If this session handles the backchannel, save the nd_xprt for this
5843 * RPC, since this is the one being used.
5845 if (sep->sess_clp->lc_req.nr_client != NULL &&
5846 (sep->sess_crflags & NFSV4CRSESS_CONNBACKCHAN) != 0) {
5847 savxprt = sep->sess_cbsess.nfsess_xprt;
5848 SVC_ACQUIRE(nd->nd_xprt);
5849 nd->nd_xprt->xp_p2 =
5850 sep->sess_clp->lc_req.nr_client->cl_private;
5851 nd->nd_xprt->xp_idletimeout = 0; /* Disable timeout. */
5852 sep->sess_cbsess.nfsess_xprt = nd->nd_xprt;
5853 if (savxprt != NULL)
5854 SVC_RELEASE(savxprt);
5858 if (sep->sess_clp->lc_req.nr_client == NULL)
5859 *sflagsp |= NFSV4SEQ_CBPATHDOWN;
5860 NFSUNLOCKSESSION(shp);
5861 if (error == NFSERR_EXPIRED) {
5862 *sflagsp |= NFSV4SEQ_EXPIREDALLSTATEREVOKED;
5864 } else if (error == NFSERR_ADMINREVOKED) {
5865 *sflagsp |= NFSV4SEQ_ADMINSTATEREVOKED;
5868 *highest_slotidp = *target_highest_slotidp = NFSV4_SLOTS - 1;
5873 * Check/set reclaim complete for this session/clientid.
5876 nfsrv_checkreclaimcomplete(struct nfsrv_descript *nd)
5878 struct nfsdsession *sep;
5879 struct nfssessionhash *shp;
5882 shp = NFSSESSIONHASH(nd->nd_sessionid);
5884 NFSLOCKSESSION(shp);
5885 sep = nfsrv_findsession(nd->nd_sessionid);
5887 NFSUNLOCKSESSION(shp);
5889 return (NFSERR_BADSESSION);
5892 /* Check to see if reclaim complete has already happened. */
5893 if ((sep->sess_clp->lc_flags & LCL_RECLAIMCOMPLETE) != 0)
5894 error = NFSERR_COMPLETEALREADY;
5896 sep->sess_clp->lc_flags |= LCL_RECLAIMCOMPLETE;
5897 NFSUNLOCKSESSION(shp);
5903 * Cache the reply in a session slot.
5906 nfsrv_cache_session(uint8_t *sessionid, uint32_t slotid, int repstat,
5909 struct nfsdsession *sep;
5910 struct nfssessionhash *shp;
5912 shp = NFSSESSIONHASH(sessionid);
5913 NFSLOCKSESSION(shp);
5914 sep = nfsrv_findsession(sessionid);
5916 NFSUNLOCKSESSION(shp);
5917 printf("nfsrv_cache_session: no session\n");
5921 nfsv4_seqsess_cacherep(slotid, sep->sess_slots, repstat, m);
5922 NFSUNLOCKSESSION(shp);
5926 * Search for a session that matches the sessionid.
5928 static struct nfsdsession *
5929 nfsrv_findsession(uint8_t *sessionid)
5931 struct nfsdsession *sep;
5932 struct nfssessionhash *shp;
5934 shp = NFSSESSIONHASH(sessionid);
5935 LIST_FOREACH(sep, &shp->list, sess_hash) {
5936 if (!NFSBCMP(sessionid, sep->sess_sessionid, NFSX_V4SESSIONID))
5943 * Destroy a session.
5946 nfsrv_destroysession(struct nfsrv_descript *nd, uint8_t *sessionid)
5948 int error, samesess;
5951 if (!NFSBCMP(sessionid, nd->nd_sessionid, NFSX_V4SESSIONID)) {
5953 if ((nd->nd_flag & ND_LASTOP) == 0)
5954 return (NFSERR_BADSESSION);
5956 error = nfsrv_freesession(NULL, sessionid);
5957 if (error == 0 && samesess != 0)
5958 nd->nd_flag &= ~ND_HASSEQUENCE;
5963 * Free up a session structure.
5966 nfsrv_freesession(struct nfsdsession *sep, uint8_t *sessionid)
5968 struct nfssessionhash *shp;
5973 shp = NFSSESSIONHASH(sessionid);
5974 NFSLOCKSESSION(shp);
5975 sep = nfsrv_findsession(sessionid);
5977 shp = NFSSESSIONHASH(sep->sess_sessionid);
5978 NFSLOCKSESSION(shp);
5982 if (sep->sess_refcnt > 0) {
5983 NFSUNLOCKSESSION(shp);
5987 LIST_REMOVE(sep, sess_hash);
5988 LIST_REMOVE(sep, sess_list);
5990 NFSUNLOCKSESSION(shp);
5993 return (NFSERR_BADSESSION);
5994 for (i = 0; i < NFSV4_SLOTS; i++)
5995 if (sep->sess_slots[i].nfssl_reply != NULL)
5996 m_freem(sep->sess_slots[i].nfssl_reply);
5997 if (sep->sess_cbsess.nfsess_xprt != NULL)
5998 SVC_RELEASE(sep->sess_cbsess.nfsess_xprt);
5999 free(sep, M_NFSDSESSION);
6005 * RFC5661 says that it should fail when there are associated opens, locks
6006 * or delegations. Since stateids represent opens, I don't see how you can
6007 * free an open stateid (it will be free'd when closed), so this function
6008 * only works for lock stateids (freeing the lock_owner) or delegations.
6011 nfsrv_freestateid(struct nfsrv_descript *nd, nfsv4stateid_t *stateidp,
6014 struct nfsclient *clp;
6015 struct nfsstate *stp;
6020 * Look up the stateid
6022 error = nfsrv_getclient((nfsquad_t)((u_quad_t)0), CLOPS_RENEW, &clp,
6023 NULL, (nfsquad_t)((u_quad_t)0), 0, nd, p);
6025 /* First, check for a delegation. */
6026 LIST_FOREACH(stp, &clp->lc_deleg, ls_list) {
6027 if (!NFSBCMP(stp->ls_stateid.other, stateidp->other,
6032 nfsrv_freedeleg(stp);
6037 /* Not a delegation, try for a lock_owner. */
6039 error = nfsrv_getstate(clp, stateidp, 0, &stp);
6040 if (error == 0 && ((stp->ls_flags & (NFSLCK_OPEN | NFSLCK_DELEGREAD |
6041 NFSLCK_DELEGWRITE)) != 0 || (stp->ls_flags & NFSLCK_LOCK) == 0))
6042 /* Not a lock_owner stateid. */
6043 error = NFSERR_LOCKSHELD;
6044 if (error == 0 && !LIST_EMPTY(&stp->ls_lock))
6045 error = NFSERR_LOCKSHELD;
6047 nfsrv_freelockowner(stp, NULL, 0, p);
6053 * Generate the xdr for an NFSv4.1 CBSequence Operation.
6056 nfsv4_setcbsequence(struct nfsrv_descript *nd, struct nfsclient *clp,
6057 int dont_replycache, struct nfsdsession **sepp)
6059 struct nfsdsession *sep;
6060 uint32_t *tl, slotseq = 0;
6061 int maxslot, slotpos;
6062 uint8_t sessionid[NFSX_V4SESSIONID];
6065 error = nfsv4_getcbsession(clp, sepp);
6069 (void)nfsv4_sequencelookup(NULL, &sep->sess_cbsess, &slotpos, &maxslot,
6070 &slotseq, sessionid);
6071 KASSERT(maxslot >= 0, ("nfsv4_setcbsequence neg maxslot"));
6073 /* Build the Sequence arguments. */
6074 NFSM_BUILD(tl, uint32_t *, NFSX_V4SESSIONID + 5 * NFSX_UNSIGNED);
6075 bcopy(sessionid, tl, NFSX_V4SESSIONID);
6076 tl += NFSX_V4SESSIONID / NFSX_UNSIGNED;
6077 nd->nd_slotseq = tl;
6078 *tl++ = txdr_unsigned(slotseq);
6079 *tl++ = txdr_unsigned(slotpos);
6080 *tl++ = txdr_unsigned(maxslot);
6081 if (dont_replycache == 0)
6082 *tl++ = newnfs_true;
6084 *tl++ = newnfs_false;
6085 *tl = 0; /* No referring call list, for now. */
6086 nd->nd_flag |= ND_HASSEQUENCE;
6091 * Get a session for the callback.
6094 nfsv4_getcbsession(struct nfsclient *clp, struct nfsdsession **sepp)
6096 struct nfsdsession *sep;
6099 LIST_FOREACH(sep, &clp->lc_session, sess_list) {
6100 if ((sep->sess_crflags & NFSV4CRSESS_CONNBACKCHAN) != 0)
6105 return (NFSERR_BADSESSION);
6114 * Free up all backchannel xprts. This needs to be done when the nfsd threads
6115 * exit, since those transports will all be going away.
6116 * This is only called after all the nfsd threads are done performing RPCs,
6117 * so locking shouldn't be an issue.
6120 nfsrv_freeallbackchannel_xprts(void)
6122 struct nfsdsession *sep;
6123 struct nfsclient *clp;
6127 for (i = 0; i < nfsrv_clienthashsize; i++) {
6128 LIST_FOREACH(clp, &nfsclienthash[i], lc_hash) {
6129 LIST_FOREACH(sep, &clp->lc_session, sess_list) {
6130 xprt = sep->sess_cbsess.nfsess_xprt;
6131 sep->sess_cbsess.nfsess_xprt = NULL;