2 * Copyright (c) 2009 Rick Macklem, University of Guelph
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
32 #include <fs/nfs/nfsport.h>
34 struct nfsrv_stablefirst nfsrv_stablefirst;
35 int nfsrv_issuedelegs = 0;
36 int nfsrv_dolocallocks = 0;
37 struct nfsv4lock nfsv4rootfs_lock;
39 extern int newnfs_numnfsd;
40 extern struct nfsstats newnfsstats;
41 extern int nfsrv_lease;
42 extern struct timeval nfsboottime;
43 extern u_int32_t newnfs_true, newnfs_false;
47 SYSCTL_DECL(_vfs_nfsd);
48 int nfsrv_statehashsize = NFSSTATEHASHSIZE;
49 SYSCTL_INT(_vfs_nfsd, OID_AUTO, statehashsize, CTLFLAG_RDTUN,
50 &nfsrv_statehashsize, 0,
51 "Size of state hash table set via loader.conf");
53 int nfsrv_clienthashsize = NFSCLIENTHASHSIZE;
54 SYSCTL_INT(_vfs_nfsd, OID_AUTO, clienthashsize, CTLFLAG_RDTUN,
55 &nfsrv_clienthashsize, 0,
56 "Size of client hash table set via loader.conf");
58 int nfsrv_lockhashsize = NFSLOCKHASHSIZE;
59 SYSCTL_INT(_vfs_nfsd, OID_AUTO, fhhashsize, CTLFLAG_RDTUN,
60 &nfsrv_lockhashsize, 0,
61 "Size of file handle hash table set via loader.conf");
63 int nfsrv_sessionhashsize = NFSSESSIONHASHSIZE;
64 SYSCTL_INT(_vfs_nfsd, OID_AUTO, sessionhashsize, CTLFLAG_RDTUN,
65 &nfsrv_sessionhashsize, 0,
66 "Size of session hash table set via loader.conf");
68 static int nfsrv_v4statelimit = NFSRV_V4STATELIMIT;
69 SYSCTL_INT(_vfs_nfsd, OID_AUTO, v4statelimit, CTLFLAG_RWTUN,
70 &nfsrv_v4statelimit, 0,
71 "High water limit for NFSv4 opens+locks+delegations");
74 * Hash lists for nfs V4.
76 struct nfsclienthashhead *nfsclienthash;
77 struct nfslockhashhead *nfslockhash;
78 struct nfssessionhash *nfssessionhash;
79 #endif /* !APPLEKEXT */
81 static u_int32_t nfsrv_openpluslock = 0, nfsrv_delegatecnt = 0;
82 static time_t nfsrvboottime;
83 static int nfsrv_writedelegifpos = 1;
84 static int nfsrv_returnoldstateid = 0, nfsrv_clients = 0;
85 static int nfsrv_clienthighwater = NFSRV_CLIENTHIGHWATER;
86 static int nfsrv_nogsscallback = 0;
89 static void nfsrv_dumpaclient(struct nfsclient *clp,
90 struct nfsd_dumpclients *dumpp);
91 static void nfsrv_freeopenowner(struct nfsstate *stp, int cansleep,
93 static int nfsrv_freeopen(struct nfsstate *stp, vnode_t vp, int cansleep,
95 static void nfsrv_freelockowner(struct nfsstate *stp, vnode_t vp, int cansleep,
97 static void nfsrv_freeallnfslocks(struct nfsstate *stp, vnode_t vp,
98 int cansleep, NFSPROC_T *p);
99 static void nfsrv_freenfslock(struct nfslock *lop);
100 static void nfsrv_freenfslockfile(struct nfslockfile *lfp);
101 static void nfsrv_freedeleg(struct nfsstate *);
102 static int nfsrv_getstate(struct nfsclient *clp, nfsv4stateid_t *stateidp,
103 u_int32_t flags, struct nfsstate **stpp);
104 static void nfsrv_getowner(struct nfsstatehead *hp, struct nfsstate *new_stp,
105 struct nfsstate **stpp);
106 static int nfsrv_getlockfh(vnode_t vp, u_short flags,
107 struct nfslockfile *new_lfp, fhandle_t *nfhp, NFSPROC_T *p);
108 static int nfsrv_getlockfile(u_short flags, struct nfslockfile **new_lfpp,
109 struct nfslockfile **lfpp, fhandle_t *nfhp, int lockit);
110 static void nfsrv_insertlock(struct nfslock *new_lop,
111 struct nfslock *insert_lop, struct nfsstate *stp, struct nfslockfile *lfp);
112 static void nfsrv_updatelock(struct nfsstate *stp, struct nfslock **new_lopp,
113 struct nfslock **other_lopp, struct nfslockfile *lfp);
114 static int nfsrv_getipnumber(u_char *cp);
115 static int nfsrv_checkrestart(nfsquad_t clientid, u_int32_t flags,
116 nfsv4stateid_t *stateidp, int specialid);
117 static int nfsrv_checkgrace(struct nfsrv_descript *nd, struct nfsclient *clp,
119 static int nfsrv_docallback(struct nfsclient *clp, int procnum,
120 nfsv4stateid_t *stateidp, int trunc, fhandle_t *fhp,
121 struct nfsvattr *nap, nfsattrbit_t *attrbitp, NFSPROC_T *p);
122 static int nfsrv_cbcallargs(struct nfsrv_descript *nd, struct nfsclient *clp,
123 uint32_t callback, int op, const char *optag, struct nfsdsession **sepp);
124 static u_int32_t nfsrv_nextclientindex(void);
125 static u_int32_t nfsrv_nextstateindex(struct nfsclient *clp);
126 static void nfsrv_markstable(struct nfsclient *clp);
127 static int nfsrv_checkstable(struct nfsclient *clp);
128 static int nfsrv_clientconflict(struct nfsclient *clp, int *haslockp, struct
129 vnode *vp, NFSPROC_T *p);
130 static int nfsrv_delegconflict(struct nfsstate *stp, int *haslockp,
131 NFSPROC_T *p, vnode_t vp);
132 static int nfsrv_cleandeleg(vnode_t vp, struct nfslockfile *lfp,
133 struct nfsclient *clp, int *haslockp, NFSPROC_T *p);
134 static int nfsrv_notsamecredname(struct nfsrv_descript *nd,
135 struct nfsclient *clp);
136 static time_t nfsrv_leaseexpiry(void);
137 static void nfsrv_delaydelegtimeout(struct nfsstate *stp);
138 static int nfsrv_checkseqid(struct nfsrv_descript *nd, u_int32_t seqid,
139 struct nfsstate *stp, struct nfsrvcache *op);
140 static int nfsrv_nootherstate(struct nfsstate *stp);
141 static int nfsrv_locallock(vnode_t vp, struct nfslockfile *lfp, int flags,
142 uint64_t first, uint64_t end, struct nfslockconflict *cfp, NFSPROC_T *p);
143 static void nfsrv_localunlock(vnode_t vp, struct nfslockfile *lfp,
144 uint64_t init_first, uint64_t init_end, NFSPROC_T *p);
145 static int nfsrv_dolocal(vnode_t vp, struct nfslockfile *lfp, int flags,
146 int oldflags, uint64_t first, uint64_t end, struct nfslockconflict *cfp,
148 static void nfsrv_locallock_rollback(vnode_t vp, struct nfslockfile *lfp,
150 static void nfsrv_locallock_commit(struct nfslockfile *lfp, int flags,
151 uint64_t first, uint64_t end);
152 static void nfsrv_locklf(struct nfslockfile *lfp);
153 static void nfsrv_unlocklf(struct nfslockfile *lfp);
154 static struct nfsdsession *nfsrv_findsession(uint8_t *sessionid);
155 static int nfsrv_freesession(struct nfsdsession *sep, uint8_t *sessionid);
156 static int nfsv4_setcbsequence(struct nfsrv_descript *nd, struct nfsclient *clp,
157 int dont_replycache, struct nfsdsession **sepp);
158 static int nfsv4_getcbsession(struct nfsclient *clp, struct nfsdsession **sepp);
161 * Scan the client list for a match and either return the current one,
162 * create a new entry or return an error.
163 * If returning a non-error, the clp structure must either be linked into
164 * the client list or free'd.
167 nfsrv_setclient(struct nfsrv_descript *nd, struct nfsclient **new_clpp,
168 nfsquad_t *clientidp, nfsquad_t *confirmp, NFSPROC_T *p)
170 struct nfsclient *clp = NULL, *new_clp = *new_clpp;
172 struct nfsstate *stp, *tstp;
173 struct sockaddr_in *sad, *rad;
174 int zapit = 0, gotit, hasstate = 0, igotlock;
175 static u_int64_t confirm_index = 0;
178 * Check for state resource limit exceeded.
180 if (nfsrv_openpluslock > nfsrv_v4statelimit) {
181 error = NFSERR_RESOURCE;
185 if (nfsrv_issuedelegs == 0 ||
186 ((nd->nd_flag & ND_GSS) != 0 && nfsrv_nogsscallback != 0))
188 * Don't do callbacks when delegations are disabled or
189 * for AUTH_GSS unless enabled via nfsrv_nogsscallback.
190 * If establishing a callback connection is attempted
191 * when a firewall is blocking the callback path, the
192 * server may wait too long for the connect attempt to
193 * succeed during the Open. Some clients, such as Linux,
194 * may timeout and give up on the Open before the server
195 * replies. Also, since AUTH_GSS callbacks are not
196 * yet interoperability tested, they might cause the
197 * server to crap out, if they get past the Init call to
200 new_clp->lc_program = 0;
202 /* Lock out other nfsd threads */
203 NFSLOCKV4ROOTMUTEX();
204 nfsv4_relref(&nfsv4rootfs_lock);
206 igotlock = nfsv4_lock(&nfsv4rootfs_lock, 1, NULL,
207 NFSV4ROOTLOCKMUTEXPTR, NULL);
209 NFSUNLOCKV4ROOTMUTEX();
212 * Search for a match in the client list.
215 while (i < nfsrv_clienthashsize && !gotit) {
216 LIST_FOREACH(clp, &nfsclienthash[i], lc_hash) {
217 if (new_clp->lc_idlen == clp->lc_idlen &&
218 !NFSBCMP(new_clp->lc_id, clp->lc_id, clp->lc_idlen)) {
227 (clp->lc_flags & (LCL_NEEDSCONFIRM | LCL_ADMINREVOKED))) {
228 if ((nd->nd_flag & ND_NFSV41) != 0 && confirmp->lval[1] != 0) {
230 * For NFSv4.1, if confirmp->lval[1] is non-zero, the
231 * client is trying to update a confirmed clientid.
233 NFSLOCKV4ROOTMUTEX();
234 nfsv4_unlock(&nfsv4rootfs_lock, 1);
235 NFSUNLOCKV4ROOTMUTEX();
236 confirmp->lval[1] = 0;
237 error = NFSERR_NOENT;
241 * Get rid of the old one.
243 if (i != nfsrv_clienthashsize) {
244 LIST_REMOVE(clp, lc_hash);
245 nfsrv_cleanclient(clp, p);
246 nfsrv_freedeleglist(&clp->lc_deleg);
247 nfsrv_freedeleglist(&clp->lc_olddeleg);
251 * Add it after assigning a client id to it.
253 new_clp->lc_flags |= LCL_NEEDSCONFIRM;
254 if ((nd->nd_flag & ND_NFSV41) != 0)
255 new_clp->lc_confirm.lval[0] = confirmp->lval[0] =
258 confirmp->qval = new_clp->lc_confirm.qval =
260 clientidp->lval[0] = new_clp->lc_clientid.lval[0] =
261 (u_int32_t)nfsrvboottime;
262 clientidp->lval[1] = new_clp->lc_clientid.lval[1] =
263 nfsrv_nextclientindex();
264 new_clp->lc_stateindex = 0;
265 new_clp->lc_statemaxindex = 0;
266 new_clp->lc_cbref = 0;
267 new_clp->lc_expiry = nfsrv_leaseexpiry();
268 LIST_INIT(&new_clp->lc_open);
269 LIST_INIT(&new_clp->lc_deleg);
270 LIST_INIT(&new_clp->lc_olddeleg);
271 LIST_INIT(&new_clp->lc_session);
272 for (i = 0; i < nfsrv_statehashsize; i++)
273 LIST_INIT(&new_clp->lc_stateid[i]);
274 LIST_INSERT_HEAD(NFSCLIENTHASH(new_clp->lc_clientid), new_clp,
276 newnfsstats.srvclients++;
277 nfsrv_openpluslock++;
279 NFSLOCKV4ROOTMUTEX();
280 nfsv4_unlock(&nfsv4rootfs_lock, 1);
281 NFSUNLOCKV4ROOTMUTEX();
283 nfsrv_zapclient(clp, p);
289 * Now, handle the cases where the id is already issued.
291 if (nfsrv_notsamecredname(nd, clp)) {
293 * Check to see if there is expired state that should go away.
295 if (clp->lc_expiry < NFSD_MONOSEC &&
296 (!LIST_EMPTY(&clp->lc_open) || !LIST_EMPTY(&clp->lc_deleg))) {
297 nfsrv_cleanclient(clp, p);
298 nfsrv_freedeleglist(&clp->lc_deleg);
302 * If there is outstanding state, then reply NFSERR_CLIDINUSE per
303 * RFC3530 Sec. 8.1.2 last para.
305 if (!LIST_EMPTY(&clp->lc_deleg)) {
307 } else if (LIST_EMPTY(&clp->lc_open)) {
311 /* Look for an Open on the OpenOwner */
312 LIST_FOREACH(stp, &clp->lc_open, ls_list) {
313 if (!LIST_EMPTY(&stp->ls_open)) {
321 * If the uid doesn't match, return NFSERR_CLIDINUSE after
322 * filling out the correct ipaddr and portnum.
324 sad = NFSSOCKADDR(new_clp->lc_req.nr_nam, struct sockaddr_in *);
325 rad = NFSSOCKADDR(clp->lc_req.nr_nam, struct sockaddr_in *);
326 sad->sin_addr.s_addr = rad->sin_addr.s_addr;
327 sad->sin_port = rad->sin_port;
328 NFSLOCKV4ROOTMUTEX();
329 nfsv4_unlock(&nfsv4rootfs_lock, 1);
330 NFSUNLOCKV4ROOTMUTEX();
331 error = NFSERR_CLIDINUSE;
336 if (NFSBCMP(new_clp->lc_verf, clp->lc_verf, NFSX_VERF)) {
338 * If the verifier has changed, the client has rebooted
339 * and a new client id is issued. The old state info
340 * can be thrown away once the SETCLIENTID_CONFIRM occurs.
342 LIST_REMOVE(clp, lc_hash);
343 new_clp->lc_flags |= LCL_NEEDSCONFIRM;
344 if ((nd->nd_flag & ND_NFSV41) != 0)
345 new_clp->lc_confirm.lval[0] = confirmp->lval[0] =
348 confirmp->qval = new_clp->lc_confirm.qval =
350 clientidp->lval[0] = new_clp->lc_clientid.lval[0] =
352 clientidp->lval[1] = new_clp->lc_clientid.lval[1] =
353 nfsrv_nextclientindex();
354 new_clp->lc_stateindex = 0;
355 new_clp->lc_statemaxindex = 0;
356 new_clp->lc_cbref = 0;
357 new_clp->lc_expiry = nfsrv_leaseexpiry();
360 * Save the state until confirmed.
362 LIST_NEWHEAD(&new_clp->lc_open, &clp->lc_open, ls_list);
363 LIST_FOREACH(tstp, &new_clp->lc_open, ls_list)
364 tstp->ls_clp = new_clp;
365 LIST_NEWHEAD(&new_clp->lc_deleg, &clp->lc_deleg, ls_list);
366 LIST_FOREACH(tstp, &new_clp->lc_deleg, ls_list)
367 tstp->ls_clp = new_clp;
368 LIST_NEWHEAD(&new_clp->lc_olddeleg, &clp->lc_olddeleg,
370 LIST_FOREACH(tstp, &new_clp->lc_olddeleg, ls_list)
371 tstp->ls_clp = new_clp;
372 for (i = 0; i < nfsrv_statehashsize; i++) {
373 LIST_NEWHEAD(&new_clp->lc_stateid[i],
374 &clp->lc_stateid[i], ls_hash);
375 LIST_FOREACH(tstp, &new_clp->lc_stateid[i], ls_hash)
376 tstp->ls_clp = new_clp;
378 LIST_INSERT_HEAD(NFSCLIENTHASH(new_clp->lc_clientid), new_clp,
380 newnfsstats.srvclients++;
381 nfsrv_openpluslock++;
383 NFSLOCKV4ROOTMUTEX();
384 nfsv4_unlock(&nfsv4rootfs_lock, 1);
385 NFSUNLOCKV4ROOTMUTEX();
388 * Must wait until any outstanding callback on the old clp
392 while (clp->lc_cbref) {
393 clp->lc_flags |= LCL_WAKEUPWANTED;
394 (void)mtx_sleep(clp, NFSSTATEMUTEXPTR, PZERO - 1,
395 "nfsd clp", 10 * hz);
398 nfsrv_zapclient(clp, p);
403 /* For NFSv4.1, mark that we found a confirmed clientid. */
404 if ((nd->nd_flag & ND_NFSV41) != 0) {
405 clientidp->lval[0] = clp->lc_clientid.lval[0];
406 clientidp->lval[1] = clp->lc_clientid.lval[1];
407 confirmp->lval[0] = 0; /* Ignored by client */
408 confirmp->lval[1] = 1;
411 * id and verifier match, so update the net address info
412 * and get rid of any existing callback authentication
413 * handle, so a new one will be acquired.
415 LIST_REMOVE(clp, lc_hash);
416 new_clp->lc_flags |= (LCL_NEEDSCONFIRM | LCL_DONTCLEAN);
417 new_clp->lc_expiry = nfsrv_leaseexpiry();
418 confirmp->qval = new_clp->lc_confirm.qval = ++confirm_index;
419 clientidp->lval[0] = new_clp->lc_clientid.lval[0] =
420 clp->lc_clientid.lval[0];
421 clientidp->lval[1] = new_clp->lc_clientid.lval[1] =
422 clp->lc_clientid.lval[1];
423 new_clp->lc_delegtime = clp->lc_delegtime;
424 new_clp->lc_stateindex = clp->lc_stateindex;
425 new_clp->lc_statemaxindex = clp->lc_statemaxindex;
426 new_clp->lc_cbref = 0;
427 LIST_NEWHEAD(&new_clp->lc_open, &clp->lc_open, ls_list);
428 LIST_FOREACH(tstp, &new_clp->lc_open, ls_list)
429 tstp->ls_clp = new_clp;
430 LIST_NEWHEAD(&new_clp->lc_deleg, &clp->lc_deleg, ls_list);
431 LIST_FOREACH(tstp, &new_clp->lc_deleg, ls_list)
432 tstp->ls_clp = new_clp;
433 LIST_NEWHEAD(&new_clp->lc_olddeleg, &clp->lc_olddeleg, ls_list);
434 LIST_FOREACH(tstp, &new_clp->lc_olddeleg, ls_list)
435 tstp->ls_clp = new_clp;
436 for (i = 0; i < nfsrv_statehashsize; i++) {
437 LIST_NEWHEAD(&new_clp->lc_stateid[i],
438 &clp->lc_stateid[i], ls_hash);
439 LIST_FOREACH(tstp, &new_clp->lc_stateid[i], ls_hash)
440 tstp->ls_clp = new_clp;
442 LIST_INSERT_HEAD(NFSCLIENTHASH(new_clp->lc_clientid), new_clp,
444 newnfsstats.srvclients++;
445 nfsrv_openpluslock++;
448 NFSLOCKV4ROOTMUTEX();
449 nfsv4_unlock(&nfsv4rootfs_lock, 1);
450 NFSUNLOCKV4ROOTMUTEX();
452 if ((nd->nd_flag & ND_NFSV41) == 0) {
454 * Must wait until any outstanding callback on the old clp
458 while (clp->lc_cbref) {
459 clp->lc_flags |= LCL_WAKEUPWANTED;
460 (void)mtx_sleep(clp, NFSSTATEMUTEXPTR, PZERO - 1,
464 nfsrv_zapclient(clp, p);
469 NFSEXITCODE2(error, nd);
474 * Check to see if the client id exists and optionally confirm it.
477 nfsrv_getclient(nfsquad_t clientid, int opflags, struct nfsclient **clpp,
478 struct nfsdsession *nsep, nfsquad_t confirm, uint32_t cbprogram,
479 struct nfsrv_descript *nd, NFSPROC_T *p)
481 struct nfsclient *clp;
482 struct nfsstate *stp;
484 struct nfsclienthashhead *hp;
485 int error = 0, igotlock, doneok;
486 struct nfssessionhash *shp;
487 struct nfsdsession *sep;
489 static uint64_t next_sess = 0;
493 if ((nd == NULL || (nd->nd_flag & ND_NFSV41) == 0 ||
494 opflags != CLOPS_RENEW) && nfsrvboottime != clientid.lval[0]) {
495 error = NFSERR_STALECLIENTID;
500 * If called with opflags == CLOPS_RENEW, the State Lock is
501 * already held. Otherwise, we need to get either that or,
502 * for the case of Confirm, lock out the nfsd threads.
504 if (opflags & CLOPS_CONFIRM) {
505 NFSLOCKV4ROOTMUTEX();
506 nfsv4_relref(&nfsv4rootfs_lock);
508 igotlock = nfsv4_lock(&nfsv4rootfs_lock, 1, NULL,
509 NFSV4ROOTLOCKMUTEXPTR, NULL);
512 * Create a new sessionid here, since we need to do it where
513 * there is a mutex held to serialize update of next_sess.
515 if ((nd->nd_flag & ND_NFSV41) != 0) {
516 sessid[0] = ++next_sess;
517 sessid[1] = clientid.qval;
519 NFSUNLOCKV4ROOTMUTEX();
520 } else if (opflags != CLOPS_RENEW) {
524 /* For NFSv4.1, the clp is acquired from the associated session. */
525 if (nd != NULL && (nd->nd_flag & ND_NFSV41) != 0 &&
526 opflags == CLOPS_RENEW) {
528 if ((nd->nd_flag & ND_HASSEQUENCE) != 0) {
529 shp = NFSSESSIONHASH(nd->nd_sessionid);
531 sep = nfsrv_findsession(nd->nd_sessionid);
534 NFSUNLOCKSESSION(shp);
537 hp = NFSCLIENTHASH(clientid);
538 LIST_FOREACH(clp, hp, lc_hash) {
539 if (clp->lc_clientid.lval[1] == clientid.lval[1])
544 if (opflags & CLOPS_CONFIRM)
545 error = NFSERR_STALECLIENTID;
547 error = NFSERR_EXPIRED;
548 } else if (clp->lc_flags & LCL_ADMINREVOKED) {
550 * If marked admin revoked, just return the error.
552 error = NFSERR_ADMINREVOKED;
555 if (opflags & CLOPS_CONFIRM) {
556 NFSLOCKV4ROOTMUTEX();
557 nfsv4_unlock(&nfsv4rootfs_lock, 1);
558 NFSUNLOCKV4ROOTMUTEX();
559 } else if (opflags != CLOPS_RENEW) {
566 * Perform any operations specified by the opflags.
568 if (opflags & CLOPS_CONFIRM) {
569 if (((nd->nd_flag & ND_NFSV41) != 0 &&
570 clp->lc_confirm.lval[0] != confirm.lval[0]) ||
571 ((nd->nd_flag & ND_NFSV41) == 0 &&
572 clp->lc_confirm.qval != confirm.qval))
573 error = NFSERR_STALECLIENTID;
574 else if (nfsrv_notsamecredname(nd, clp))
575 error = NFSERR_CLIDINUSE;
578 if ((clp->lc_flags & (LCL_NEEDSCONFIRM | LCL_DONTCLEAN)) ==
581 * Hang onto the delegations (as old delegations)
582 * for an Open with CLAIM_DELEGATE_PREV unless in
583 * grace, but get rid of the rest of the state.
585 nfsrv_cleanclient(clp, p);
586 nfsrv_freedeleglist(&clp->lc_olddeleg);
587 if (nfsrv_checkgrace(nd, clp, 0)) {
588 /* In grace, so just delete delegations */
589 nfsrv_freedeleglist(&clp->lc_deleg);
591 LIST_FOREACH(stp, &clp->lc_deleg, ls_list)
592 stp->ls_flags |= NFSLCK_OLDDELEG;
593 clp->lc_delegtime = NFSD_MONOSEC +
594 nfsrv_lease + NFSRV_LEASEDELTA;
595 LIST_NEWHEAD(&clp->lc_olddeleg, &clp->lc_deleg,
598 if ((nd->nd_flag & ND_NFSV41) != 0)
599 clp->lc_program = cbprogram;
601 clp->lc_flags &= ~(LCL_NEEDSCONFIRM | LCL_DONTCLEAN);
603 clp->lc_flags |= LCL_NEEDSCBNULL;
604 /* For NFSv4.1, link the session onto the client. */
606 /* Hold a reference on the xprt for a backchannel. */
607 if ((nsep->sess_crflags & NFSV4CRSESS_CONNBACKCHAN)
608 != 0 && clp->lc_req.nr_client == NULL) {
609 clp->lc_req.nr_client = (struct __rpc_client *)
610 clnt_bck_create(nd->nd_xprt->xp_socket,
611 cbprogram, NFSV4_CBVERS);
612 if (clp->lc_req.nr_client != NULL) {
613 SVC_ACQUIRE(nd->nd_xprt);
615 clp->lc_req.nr_client->cl_private;
616 /* Disable idle timeout. */
617 nd->nd_xprt->xp_idletimeout = 0;
618 nsep->sess_cbsess.nfsess_xprt = nd->nd_xprt;
620 nsep->sess_crflags &= ~NFSV4CRSESS_CONNBACKCHAN;
622 NFSBCOPY(sessid, nsep->sess_sessionid,
624 NFSBCOPY(sessid, nsep->sess_cbsess.nfsess_sessionid,
626 shp = NFSSESSIONHASH(nsep->sess_sessionid);
628 LIST_INSERT_HEAD(&shp->list, nsep, sess_hash);
630 LIST_INSERT_HEAD(&clp->lc_session, nsep, sess_list);
631 nsep->sess_clp = clp;
633 NFSUNLOCKSESSION(shp);
636 } else if (clp->lc_flags & LCL_NEEDSCONFIRM) {
637 error = NFSERR_EXPIRED;
641 * If called by the Renew Op, we must check the principal.
643 if (!error && (opflags & CLOPS_RENEWOP)) {
644 if (nfsrv_notsamecredname(nd, clp)) {
646 for (i = 0; i < nfsrv_statehashsize && doneok == 0; i++) {
647 LIST_FOREACH(stp, &clp->lc_stateid[i], ls_hash) {
648 if ((stp->ls_flags & NFSLCK_OPEN) &&
649 stp->ls_uid == nd->nd_cred->cr_uid) {
656 error = NFSERR_ACCES;
658 if (!error && (clp->lc_flags & LCL_CBDOWN))
659 error = NFSERR_CBPATHDOWN;
661 if ((!error || error == NFSERR_CBPATHDOWN) &&
662 (opflags & CLOPS_RENEW)) {
663 clp->lc_expiry = nfsrv_leaseexpiry();
665 if (opflags & CLOPS_CONFIRM) {
666 NFSLOCKV4ROOTMUTEX();
667 nfsv4_unlock(&nfsv4rootfs_lock, 1);
668 NFSUNLOCKV4ROOTMUTEX();
669 } else if (opflags != CLOPS_RENEW) {
676 NFSEXITCODE2(error, nd);
681 * Perform the NFSv4.1 destroy clientid.
684 nfsrv_destroyclient(nfsquad_t clientid, NFSPROC_T *p)
686 struct nfsclient *clp;
687 struct nfsclienthashhead *hp;
688 int error = 0, i, igotlock;
690 if (nfsrvboottime != clientid.lval[0]) {
691 error = NFSERR_STALECLIENTID;
695 /* Lock out other nfsd threads */
696 NFSLOCKV4ROOTMUTEX();
697 nfsv4_relref(&nfsv4rootfs_lock);
699 igotlock = nfsv4_lock(&nfsv4rootfs_lock, 1, NULL,
700 NFSV4ROOTLOCKMUTEXPTR, NULL);
701 } while (igotlock == 0);
702 NFSUNLOCKV4ROOTMUTEX();
704 hp = NFSCLIENTHASH(clientid);
705 LIST_FOREACH(clp, hp, lc_hash) {
706 if (clp->lc_clientid.lval[1] == clientid.lval[1])
710 NFSLOCKV4ROOTMUTEX();
711 nfsv4_unlock(&nfsv4rootfs_lock, 1);
712 NFSUNLOCKV4ROOTMUTEX();
713 /* Just return ok, since it is gone. */
717 /* Scan for state on the clientid. */
718 for (i = 0; i < nfsrv_statehashsize; i++)
719 if (!LIST_EMPTY(&clp->lc_stateid[i])) {
720 NFSLOCKV4ROOTMUTEX();
721 nfsv4_unlock(&nfsv4rootfs_lock, 1);
722 NFSUNLOCKV4ROOTMUTEX();
723 error = NFSERR_CLIENTIDBUSY;
726 if (!LIST_EMPTY(&clp->lc_session) || !LIST_EMPTY(&clp->lc_deleg)) {
727 NFSLOCKV4ROOTMUTEX();
728 nfsv4_unlock(&nfsv4rootfs_lock, 1);
729 NFSUNLOCKV4ROOTMUTEX();
730 error = NFSERR_CLIENTIDBUSY;
734 /* Destroy the clientid and return ok. */
735 nfsrv_cleanclient(clp, p);
736 nfsrv_freedeleglist(&clp->lc_deleg);
737 nfsrv_freedeleglist(&clp->lc_olddeleg);
738 LIST_REMOVE(clp, lc_hash);
739 NFSLOCKV4ROOTMUTEX();
740 nfsv4_unlock(&nfsv4rootfs_lock, 1);
741 NFSUNLOCKV4ROOTMUTEX();
742 nfsrv_zapclient(clp, p);
744 NFSEXITCODE2(error, nd);
749 * Called from the new nfssvc syscall to admin revoke a clientid.
750 * Returns 0 for success, error otherwise.
753 nfsrv_adminrevoke(struct nfsd_clid *revokep, NFSPROC_T *p)
755 struct nfsclient *clp = NULL;
760 * First, lock out the nfsd so that state won't change while the
761 * revocation record is being written to the stable storage restart
764 NFSLOCKV4ROOTMUTEX();
766 igotlock = nfsv4_lock(&nfsv4rootfs_lock, 1, NULL,
767 NFSV4ROOTLOCKMUTEXPTR, NULL);
769 NFSUNLOCKV4ROOTMUTEX();
772 * Search for a match in the client list.
775 while (i < nfsrv_clienthashsize && !gotit) {
776 LIST_FOREACH(clp, &nfsclienthash[i], lc_hash) {
777 if (revokep->nclid_idlen == clp->lc_idlen &&
778 !NFSBCMP(revokep->nclid_id, clp->lc_id, clp->lc_idlen)) {
786 NFSLOCKV4ROOTMUTEX();
787 nfsv4_unlock(&nfsv4rootfs_lock, 0);
788 NFSUNLOCKV4ROOTMUTEX();
794 * Now, write out the revocation record
796 nfsrv_writestable(clp->lc_id, clp->lc_idlen, NFSNST_REVOKE, p);
797 nfsrv_backupstable();
800 * and clear out the state, marking the clientid revoked.
802 clp->lc_flags &= ~LCL_CALLBACKSON;
803 clp->lc_flags |= LCL_ADMINREVOKED;
804 nfsrv_cleanclient(clp, p);
805 nfsrv_freedeleglist(&clp->lc_deleg);
806 nfsrv_freedeleglist(&clp->lc_olddeleg);
807 NFSLOCKV4ROOTMUTEX();
808 nfsv4_unlock(&nfsv4rootfs_lock, 0);
809 NFSUNLOCKV4ROOTMUTEX();
817 * Dump out stats for all clients. Called from nfssvc(2), that is used
821 nfsrv_dumpclients(struct nfsd_dumpclients *dumpp, int maxcnt)
823 struct nfsclient *clp;
827 * First, get a reference on the nfsv4rootfs_lock so that an
828 * exclusive lock cannot be acquired while dumping the clients.
830 NFSLOCKV4ROOTMUTEX();
831 nfsv4_getref(&nfsv4rootfs_lock, NULL, NFSV4ROOTLOCKMUTEXPTR, NULL);
832 NFSUNLOCKV4ROOTMUTEX();
835 * Rattle through the client lists until done.
837 while (i < nfsrv_clienthashsize && cnt < maxcnt) {
838 clp = LIST_FIRST(&nfsclienthash[i]);
839 while (clp != LIST_END(&nfsclienthash[i]) && cnt < maxcnt) {
840 nfsrv_dumpaclient(clp, &dumpp[cnt]);
842 clp = LIST_NEXT(clp, lc_hash);
847 dumpp[cnt].ndcl_clid.nclid_idlen = 0;
849 NFSLOCKV4ROOTMUTEX();
850 nfsv4_relref(&nfsv4rootfs_lock);
851 NFSUNLOCKV4ROOTMUTEX();
855 * Dump stats for a client. Must be called with the NFSSTATELOCK and spl'd.
858 nfsrv_dumpaclient(struct nfsclient *clp, struct nfsd_dumpclients *dumpp)
860 struct nfsstate *stp, *openstp, *lckownstp;
862 struct sockaddr *sad;
863 struct sockaddr_in *rad;
864 struct sockaddr_in6 *rad6;
866 dumpp->ndcl_nopenowners = dumpp->ndcl_nlockowners = 0;
867 dumpp->ndcl_nopens = dumpp->ndcl_nlocks = 0;
868 dumpp->ndcl_ndelegs = dumpp->ndcl_nolddelegs = 0;
869 dumpp->ndcl_flags = clp->lc_flags;
870 dumpp->ndcl_clid.nclid_idlen = clp->lc_idlen;
871 NFSBCOPY(clp->lc_id, dumpp->ndcl_clid.nclid_id, clp->lc_idlen);
872 sad = NFSSOCKADDR(clp->lc_req.nr_nam, struct sockaddr *);
873 dumpp->ndcl_addrfam = sad->sa_family;
874 if (sad->sa_family == AF_INET) {
875 rad = (struct sockaddr_in *)sad;
876 dumpp->ndcl_cbaddr.sin_addr = rad->sin_addr;
878 rad6 = (struct sockaddr_in6 *)sad;
879 dumpp->ndcl_cbaddr.sin6_addr = rad6->sin6_addr;
883 * Now, scan the state lists and total up the opens and locks.
885 LIST_FOREACH(stp, &clp->lc_open, ls_list) {
886 dumpp->ndcl_nopenowners++;
887 LIST_FOREACH(openstp, &stp->ls_open, ls_list) {
888 dumpp->ndcl_nopens++;
889 LIST_FOREACH(lckownstp, &openstp->ls_open, ls_list) {
890 dumpp->ndcl_nlockowners++;
891 LIST_FOREACH(lop, &lckownstp->ls_lock, lo_lckowner) {
892 dumpp->ndcl_nlocks++;
899 * and the delegation lists.
901 LIST_FOREACH(stp, &clp->lc_deleg, ls_list) {
902 dumpp->ndcl_ndelegs++;
904 LIST_FOREACH(stp, &clp->lc_olddeleg, ls_list) {
905 dumpp->ndcl_nolddelegs++;
910 * Dump out lock stats for a file.
913 nfsrv_dumplocks(vnode_t vp, struct nfsd_dumplocks *ldumpp, int maxcnt,
916 struct nfsstate *stp;
919 struct nfslockfile *lfp;
920 struct sockaddr *sad;
921 struct sockaddr_in *rad;
922 struct sockaddr_in6 *rad6;
926 ret = nfsrv_getlockfh(vp, 0, NULL, &nfh, p);
928 * First, get a reference on the nfsv4rootfs_lock so that an
929 * exclusive lock on it cannot be acquired while dumping the locks.
931 NFSLOCKV4ROOTMUTEX();
932 nfsv4_getref(&nfsv4rootfs_lock, NULL, NFSV4ROOTLOCKMUTEXPTR, NULL);
933 NFSUNLOCKV4ROOTMUTEX();
936 ret = nfsrv_getlockfile(0, NULL, &lfp, &nfh, 0);
938 ldumpp[0].ndlck_clid.nclid_idlen = 0;
940 NFSLOCKV4ROOTMUTEX();
941 nfsv4_relref(&nfsv4rootfs_lock);
942 NFSUNLOCKV4ROOTMUTEX();
947 * For each open share on file, dump it out.
949 stp = LIST_FIRST(&lfp->lf_open);
950 while (stp != LIST_END(&lfp->lf_open) && cnt < maxcnt) {
951 ldumpp[cnt].ndlck_flags = stp->ls_flags;
952 ldumpp[cnt].ndlck_stateid.seqid = stp->ls_stateid.seqid;
953 ldumpp[cnt].ndlck_stateid.other[0] = stp->ls_stateid.other[0];
954 ldumpp[cnt].ndlck_stateid.other[1] = stp->ls_stateid.other[1];
955 ldumpp[cnt].ndlck_stateid.other[2] = stp->ls_stateid.other[2];
956 ldumpp[cnt].ndlck_owner.nclid_idlen =
957 stp->ls_openowner->ls_ownerlen;
958 NFSBCOPY(stp->ls_openowner->ls_owner,
959 ldumpp[cnt].ndlck_owner.nclid_id,
960 stp->ls_openowner->ls_ownerlen);
961 ldumpp[cnt].ndlck_clid.nclid_idlen = stp->ls_clp->lc_idlen;
962 NFSBCOPY(stp->ls_clp->lc_id, ldumpp[cnt].ndlck_clid.nclid_id,
963 stp->ls_clp->lc_idlen);
964 sad=NFSSOCKADDR(stp->ls_clp->lc_req.nr_nam, struct sockaddr *);
965 ldumpp[cnt].ndlck_addrfam = sad->sa_family;
966 if (sad->sa_family == AF_INET) {
967 rad = (struct sockaddr_in *)sad;
968 ldumpp[cnt].ndlck_cbaddr.sin_addr = rad->sin_addr;
970 rad6 = (struct sockaddr_in6 *)sad;
971 ldumpp[cnt].ndlck_cbaddr.sin6_addr = rad6->sin6_addr;
973 stp = LIST_NEXT(stp, ls_file);
980 lop = LIST_FIRST(&lfp->lf_lock);
981 while (lop != LIST_END(&lfp->lf_lock) && cnt < maxcnt) {
983 ldumpp[cnt].ndlck_flags = lop->lo_flags;
984 ldumpp[cnt].ndlck_first = lop->lo_first;
985 ldumpp[cnt].ndlck_end = lop->lo_end;
986 ldumpp[cnt].ndlck_stateid.seqid = stp->ls_stateid.seqid;
987 ldumpp[cnt].ndlck_stateid.other[0] = stp->ls_stateid.other[0];
988 ldumpp[cnt].ndlck_stateid.other[1] = stp->ls_stateid.other[1];
989 ldumpp[cnt].ndlck_stateid.other[2] = stp->ls_stateid.other[2];
990 ldumpp[cnt].ndlck_owner.nclid_idlen = stp->ls_ownerlen;
991 NFSBCOPY(stp->ls_owner, ldumpp[cnt].ndlck_owner.nclid_id,
993 ldumpp[cnt].ndlck_clid.nclid_idlen = stp->ls_clp->lc_idlen;
994 NFSBCOPY(stp->ls_clp->lc_id, ldumpp[cnt].ndlck_clid.nclid_id,
995 stp->ls_clp->lc_idlen);
996 sad=NFSSOCKADDR(stp->ls_clp->lc_req.nr_nam, struct sockaddr *);
997 ldumpp[cnt].ndlck_addrfam = sad->sa_family;
998 if (sad->sa_family == AF_INET) {
999 rad = (struct sockaddr_in *)sad;
1000 ldumpp[cnt].ndlck_cbaddr.sin_addr = rad->sin_addr;
1002 rad6 = (struct sockaddr_in6 *)sad;
1003 ldumpp[cnt].ndlck_cbaddr.sin6_addr = rad6->sin6_addr;
1005 lop = LIST_NEXT(lop, lo_lckfile);
1010 * and the delegations.
1012 stp = LIST_FIRST(&lfp->lf_deleg);
1013 while (stp != LIST_END(&lfp->lf_deleg) && cnt < maxcnt) {
1014 ldumpp[cnt].ndlck_flags = stp->ls_flags;
1015 ldumpp[cnt].ndlck_stateid.seqid = stp->ls_stateid.seqid;
1016 ldumpp[cnt].ndlck_stateid.other[0] = stp->ls_stateid.other[0];
1017 ldumpp[cnt].ndlck_stateid.other[1] = stp->ls_stateid.other[1];
1018 ldumpp[cnt].ndlck_stateid.other[2] = stp->ls_stateid.other[2];
1019 ldumpp[cnt].ndlck_owner.nclid_idlen = 0;
1020 ldumpp[cnt].ndlck_clid.nclid_idlen = stp->ls_clp->lc_idlen;
1021 NFSBCOPY(stp->ls_clp->lc_id, ldumpp[cnt].ndlck_clid.nclid_id,
1022 stp->ls_clp->lc_idlen);
1023 sad=NFSSOCKADDR(stp->ls_clp->lc_req.nr_nam, struct sockaddr *);
1024 ldumpp[cnt].ndlck_addrfam = sad->sa_family;
1025 if (sad->sa_family == AF_INET) {
1026 rad = (struct sockaddr_in *)sad;
1027 ldumpp[cnt].ndlck_cbaddr.sin_addr = rad->sin_addr;
1029 rad6 = (struct sockaddr_in6 *)sad;
1030 ldumpp[cnt].ndlck_cbaddr.sin6_addr = rad6->sin6_addr;
1032 stp = LIST_NEXT(stp, ls_file);
1037 * If list isn't full, mark end of list by setting the client name
1041 ldumpp[cnt].ndlck_clid.nclid_idlen = 0;
1043 NFSLOCKV4ROOTMUTEX();
1044 nfsv4_relref(&nfsv4rootfs_lock);
1045 NFSUNLOCKV4ROOTMUTEX();
1049 * Server timer routine. It can scan any linked list, so long
1050 * as it holds the spin/mutex lock and there is no exclusive lock on
1052 * (For OpenBSD, a kthread is ok. For FreeBSD, I think it is ok
1053 * to do this from a callout, since the spin locks work. For
1054 * Darwin, I'm not sure what will work correctly yet.)
1055 * Should be called once per second.
1058 nfsrv_servertimer(void)
1060 struct nfsclient *clp, *nclp;
1061 struct nfsstate *stp, *nstp;
1065 * Make sure nfsboottime is set. This is used by V3 as well
1066 * as V4. Note that nfsboottime is not nfsrvboottime, which is
1067 * only used by the V4 server for leases.
1069 if (nfsboottime.tv_sec == 0)
1070 NFSSETBOOTTIME(nfsboottime);
1073 * If server hasn't started yet, just return.
1076 if (nfsrv_stablefirst.nsf_eograce == 0) {
1080 if (!(nfsrv_stablefirst.nsf_flags & NFSNSF_UPDATEDONE)) {
1081 if (!(nfsrv_stablefirst.nsf_flags & NFSNSF_GRACEOVER) &&
1082 NFSD_MONOSEC > nfsrv_stablefirst.nsf_eograce)
1083 nfsrv_stablefirst.nsf_flags |=
1084 (NFSNSF_GRACEOVER | NFSNSF_NEEDLOCK);
1090 * Try and get a reference count on the nfsv4rootfs_lock so that
1091 * no nfsd thread can acquire an exclusive lock on it before this
1092 * call is done. If it is already exclusively locked, just return.
1094 NFSLOCKV4ROOTMUTEX();
1095 got_ref = nfsv4_getref_nonblock(&nfsv4rootfs_lock);
1096 NFSUNLOCKV4ROOTMUTEX();
1103 * For each client...
1105 for (i = 0; i < nfsrv_clienthashsize; i++) {
1106 clp = LIST_FIRST(&nfsclienthash[i]);
1107 while (clp != LIST_END(&nfsclienthash[i])) {
1108 nclp = LIST_NEXT(clp, lc_hash);
1109 if (!(clp->lc_flags & LCL_EXPIREIT)) {
1110 if (((clp->lc_expiry + NFSRV_STALELEASE) < NFSD_MONOSEC
1111 && ((LIST_EMPTY(&clp->lc_deleg)
1112 && LIST_EMPTY(&clp->lc_open)) ||
1113 nfsrv_clients > nfsrv_clienthighwater)) ||
1114 (clp->lc_expiry + NFSRV_MOULDYLEASE) < NFSD_MONOSEC ||
1115 (clp->lc_expiry < NFSD_MONOSEC &&
1116 (nfsrv_openpluslock * 10 / 9) > nfsrv_v4statelimit)) {
1118 * Lease has expired several nfsrv_lease times ago:
1120 * - no state is associated with it
1122 * - above high water mark for number of clients
1123 * (nfsrv_clienthighwater should be large enough
1124 * that this only occurs when clients fail to
1125 * use the same nfs_client_id4.id. Maybe somewhat
1126 * higher that the maximum number of clients that
1127 * will mount this server?)
1129 * Lease has expired a very long time ago
1131 * Lease has expired PLUS the number of opens + locks
1132 * has exceeded 90% of capacity
1134 * --> Mark for expiry. The actual expiry will be done
1135 * by an nfsd sometime soon.
1137 clp->lc_flags |= LCL_EXPIREIT;
1138 nfsrv_stablefirst.nsf_flags |=
1139 (NFSNSF_NEEDLOCK | NFSNSF_EXPIREDCLIENT);
1142 * If there are no opens, increment no open tick cnt
1143 * If time exceeds NFSNOOPEN, mark it to be thrown away
1144 * otherwise, if there is an open, reset no open time
1145 * Hopefully, this will avoid excessive re-creation
1146 * of open owners and subsequent open confirms.
1148 stp = LIST_FIRST(&clp->lc_open);
1149 while (stp != LIST_END(&clp->lc_open)) {
1150 nstp = LIST_NEXT(stp, ls_list);
1151 if (LIST_EMPTY(&stp->ls_open)) {
1153 if (stp->ls_noopens > NFSNOOPEN ||
1154 (nfsrv_openpluslock * 2) >
1156 nfsrv_stablefirst.nsf_flags |=
1159 stp->ls_noopens = 0;
1169 NFSLOCKV4ROOTMUTEX();
1170 nfsv4_relref(&nfsv4rootfs_lock);
1171 NFSUNLOCKV4ROOTMUTEX();
1175 * The following set of functions free up the various data structures.
1178 * Clear out all open/lock state related to this nfsclient.
1179 * Caller must hold an exclusive lock on nfsv4rootfs_lock, so that
1180 * there are no other active nfsd threads.
1183 nfsrv_cleanclient(struct nfsclient *clp, NFSPROC_T *p)
1185 struct nfsstate *stp, *nstp;
1186 struct nfsdsession *sep, *nsep;
1188 LIST_FOREACH_SAFE(stp, &clp->lc_open, ls_list, nstp)
1189 nfsrv_freeopenowner(stp, 1, p);
1190 if ((clp->lc_flags & LCL_ADMINREVOKED) == 0)
1191 LIST_FOREACH_SAFE(sep, &clp->lc_session, sess_list, nsep)
1192 (void)nfsrv_freesession(sep, NULL);
1196 * Free a client that has been cleaned. It should also already have been
1197 * removed from the lists.
1198 * (Just to be safe w.r.t. newnfs_disconnect(), call this function when
1199 * softclock interrupts are enabled.)
1202 nfsrv_zapclient(struct nfsclient *clp, NFSPROC_T *p)
1206 if ((clp->lc_flags & (LCL_GSS | LCL_CALLBACKSON)) ==
1207 (LCL_GSS | LCL_CALLBACKSON) &&
1208 (clp->lc_hand.nfsh_flag & NFSG_COMPLETE) &&
1209 clp->lc_handlelen > 0) {
1210 clp->lc_hand.nfsh_flag &= ~NFSG_COMPLETE;
1211 clp->lc_hand.nfsh_flag |= NFSG_DESTROYED;
1212 (void) nfsrv_docallback(clp, NFSV4PROC_CBNULL,
1213 NULL, 0, NULL, NULL, NULL, p);
1216 newnfs_disconnect(&clp->lc_req);
1217 NFSSOCKADDRFREE(clp->lc_req.nr_nam);
1218 NFSFREEMUTEX(&clp->lc_req.nr_mtx);
1219 free(clp->lc_stateid, M_NFSDCLIENT);
1220 free(clp, M_NFSDCLIENT);
1222 newnfsstats.srvclients--;
1223 nfsrv_openpluslock--;
1229 * Free a list of delegation state structures.
1230 * (This function will also free all nfslockfile structures that no
1231 * longer have associated state.)
1234 nfsrv_freedeleglist(struct nfsstatehead *sthp)
1236 struct nfsstate *stp, *nstp;
1238 LIST_FOREACH_SAFE(stp, sthp, ls_list, nstp) {
1239 nfsrv_freedeleg(stp);
1245 * Free up a delegation.
1248 nfsrv_freedeleg(struct nfsstate *stp)
1250 struct nfslockfile *lfp;
1252 LIST_REMOVE(stp, ls_hash);
1253 LIST_REMOVE(stp, ls_list);
1254 LIST_REMOVE(stp, ls_file);
1256 if (LIST_EMPTY(&lfp->lf_open) &&
1257 LIST_EMPTY(&lfp->lf_lock) && LIST_EMPTY(&lfp->lf_deleg) &&
1258 LIST_EMPTY(&lfp->lf_locallock) && LIST_EMPTY(&lfp->lf_rollback) &&
1259 lfp->lf_usecount == 0 &&
1260 nfsv4_testlock(&lfp->lf_locallock_lck) == 0)
1261 nfsrv_freenfslockfile(lfp);
1262 FREE((caddr_t)stp, M_NFSDSTATE);
1263 newnfsstats.srvdelegates--;
1264 nfsrv_openpluslock--;
1265 nfsrv_delegatecnt--;
1269 * This function frees an open owner and all associated opens.
1272 nfsrv_freeopenowner(struct nfsstate *stp, int cansleep, NFSPROC_T *p)
1274 struct nfsstate *nstp, *tstp;
1276 LIST_REMOVE(stp, ls_list);
1278 * Now, free all associated opens.
1280 nstp = LIST_FIRST(&stp->ls_open);
1281 while (nstp != LIST_END(&stp->ls_open)) {
1283 nstp = LIST_NEXT(nstp, ls_list);
1284 (void) nfsrv_freeopen(tstp, NULL, cansleep, p);
1287 nfsrvd_derefcache(stp->ls_op);
1288 FREE((caddr_t)stp, M_NFSDSTATE);
1289 newnfsstats.srvopenowners--;
1290 nfsrv_openpluslock--;
1294 * This function frees an open (nfsstate open structure) with all associated
1295 * lock_owners and locks. It also frees the nfslockfile structure iff there
1296 * are no other opens on the file.
1297 * Returns 1 if it free'd the nfslockfile, 0 otherwise.
1300 nfsrv_freeopen(struct nfsstate *stp, vnode_t vp, int cansleep, NFSPROC_T *p)
1302 struct nfsstate *nstp, *tstp;
1303 struct nfslockfile *lfp;
1306 LIST_REMOVE(stp, ls_hash);
1307 LIST_REMOVE(stp, ls_list);
1308 LIST_REMOVE(stp, ls_file);
1312 * Now, free all lockowners associated with this open.
1314 LIST_FOREACH_SAFE(tstp, &stp->ls_open, ls_list, nstp)
1315 nfsrv_freelockowner(tstp, vp, cansleep, p);
1318 * The nfslockfile is freed here if there are no locks
1319 * associated with the open.
1320 * If there are locks associated with the open, the
1321 * nfslockfile structure can be freed via nfsrv_freelockowner().
1322 * Acquire the state mutex to avoid races with calls to
1323 * nfsrv_getlockfile().
1327 if (lfp != NULL && LIST_EMPTY(&lfp->lf_open) &&
1328 LIST_EMPTY(&lfp->lf_deleg) && LIST_EMPTY(&lfp->lf_lock) &&
1329 LIST_EMPTY(&lfp->lf_locallock) && LIST_EMPTY(&lfp->lf_rollback) &&
1330 lfp->lf_usecount == 0 &&
1331 (cansleep != 0 || nfsv4_testlock(&lfp->lf_locallock_lck) == 0)) {
1332 nfsrv_freenfslockfile(lfp);
1338 FREE((caddr_t)stp, M_NFSDSTATE);
1339 newnfsstats.srvopens--;
1340 nfsrv_openpluslock--;
1345 * Frees a lockowner and all associated locks.
1348 nfsrv_freelockowner(struct nfsstate *stp, vnode_t vp, int cansleep,
1352 LIST_REMOVE(stp, ls_hash);
1353 LIST_REMOVE(stp, ls_list);
1354 nfsrv_freeallnfslocks(stp, vp, cansleep, p);
1356 nfsrvd_derefcache(stp->ls_op);
1357 FREE((caddr_t)stp, M_NFSDSTATE);
1358 newnfsstats.srvlockowners--;
1359 nfsrv_openpluslock--;
1363 * Free all the nfs locks on a lockowner.
1366 nfsrv_freeallnfslocks(struct nfsstate *stp, vnode_t vp, int cansleep,
1369 struct nfslock *lop, *nlop;
1370 struct nfsrollback *rlp, *nrlp;
1371 struct nfslockfile *lfp = NULL;
1374 uint64_t first, end;
1377 ASSERT_VOP_UNLOCKED(vp, "nfsrv_freeallnfslocks: vnode locked");
1378 lop = LIST_FIRST(&stp->ls_lock);
1379 while (lop != LIST_END(&stp->ls_lock)) {
1380 nlop = LIST_NEXT(lop, lo_lckowner);
1382 * Since all locks should be for the same file, lfp should
1387 else if (lfp != lop->lo_lfp)
1388 panic("allnfslocks");
1390 * If vp is NULL and cansleep != 0, a vnode must be acquired
1391 * from the file handle. This only occurs when called from
1392 * nfsrv_cleanclient().
1395 if (nfsrv_dolocallocks == 0)
1397 else if (vp == NULL && cansleep != 0) {
1398 tvp = nfsvno_getvp(&lfp->lf_fh);
1399 NFSVOPUNLOCK(tvp, 0);
1408 first = lop->lo_first;
1410 nfsrv_freenfslock(lop);
1411 nfsrv_localunlock(tvp, lfp, first, end, p);
1412 LIST_FOREACH_SAFE(rlp, &lfp->lf_rollback, rlck_list,
1414 free(rlp, M_NFSDROLLBACK);
1415 LIST_INIT(&lfp->lf_rollback);
1417 nfsrv_freenfslock(lop);
1420 if (vp == NULL && tvp != NULL)
1425 * Free an nfslock structure.
1428 nfsrv_freenfslock(struct nfslock *lop)
1431 if (lop->lo_lckfile.le_prev != NULL) {
1432 LIST_REMOVE(lop, lo_lckfile);
1433 newnfsstats.srvlocks--;
1434 nfsrv_openpluslock--;
1436 LIST_REMOVE(lop, lo_lckowner);
1437 FREE((caddr_t)lop, M_NFSDLOCK);
1441 * This function frees an nfslockfile structure.
1444 nfsrv_freenfslockfile(struct nfslockfile *lfp)
1447 LIST_REMOVE(lfp, lf_hash);
1448 FREE((caddr_t)lfp, M_NFSDLOCKFILE);
1452 * This function looks up an nfsstate structure via stateid.
1455 nfsrv_getstate(struct nfsclient *clp, nfsv4stateid_t *stateidp, __unused u_int32_t flags,
1456 struct nfsstate **stpp)
1458 struct nfsstate *stp;
1459 struct nfsstatehead *hp;
1463 hp = NFSSTATEHASH(clp, *stateidp);
1464 LIST_FOREACH(stp, hp, ls_hash) {
1465 if (!NFSBCMP(stp->ls_stateid.other, stateidp->other,
1471 * If no state id in list, return NFSERR_BADSTATEID.
1473 if (stp == LIST_END(hp)) {
1474 error = NFSERR_BADSTATEID;
1485 * This function gets an nfsstate structure via owner string.
1488 nfsrv_getowner(struct nfsstatehead *hp, struct nfsstate *new_stp,
1489 struct nfsstate **stpp)
1491 struct nfsstate *stp;
1494 LIST_FOREACH(stp, hp, ls_list) {
1495 if (new_stp->ls_ownerlen == stp->ls_ownerlen &&
1496 !NFSBCMP(new_stp->ls_owner,stp->ls_owner,stp->ls_ownerlen)) {
1504 * Lock control function called to update lock status.
1505 * Returns 0 upon success, -1 if there is no lock and the flags indicate
1506 * that one isn't to be created and an NFSERR_xxx for other errors.
1507 * The structures new_stp and new_lop are passed in as pointers that should
1508 * be set to NULL if the structure is used and shouldn't be free'd.
1509 * For the NFSLCK_TEST and NFSLCK_CHECK cases, the structures are
1510 * never used and can safely be allocated on the stack. For all other
1511 * cases, *new_stpp and *new_lopp should be malloc'd before the call,
1512 * in case they are used.
1515 nfsrv_lockctrl(vnode_t vp, struct nfsstate **new_stpp,
1516 struct nfslock **new_lopp, struct nfslockconflict *cfp,
1517 nfsquad_t clientid, nfsv4stateid_t *stateidp,
1518 __unused struct nfsexstuff *exp,
1519 struct nfsrv_descript *nd, NFSPROC_T *p)
1521 struct nfslock *lop;
1522 struct nfsstate *new_stp = *new_stpp;
1523 struct nfslock *new_lop = *new_lopp;
1524 struct nfsstate *tstp, *mystp, *nstp;
1526 struct nfslockfile *lfp;
1527 struct nfslock *other_lop = NULL;
1528 struct nfsstate *stp, *lckstp = NULL;
1529 struct nfsclient *clp = NULL;
1531 int error = 0, haslock = 0, ret, reterr;
1532 int getlckret, delegation = 0, filestruct_locked, vnode_unlocked = 0;
1534 uint64_t first, end;
1535 uint32_t lock_flags;
1537 if (new_stp->ls_flags & (NFSLCK_CHECK | NFSLCK_SETATTR)) {
1539 * Note the special cases of "all 1s" or "all 0s" stateids and
1540 * let reads with all 1s go ahead.
1542 if (new_stp->ls_stateid.seqid == 0x0 &&
1543 new_stp->ls_stateid.other[0] == 0x0 &&
1544 new_stp->ls_stateid.other[1] == 0x0 &&
1545 new_stp->ls_stateid.other[2] == 0x0)
1547 else if (new_stp->ls_stateid.seqid == 0xffffffff &&
1548 new_stp->ls_stateid.other[0] == 0xffffffff &&
1549 new_stp->ls_stateid.other[1] == 0xffffffff &&
1550 new_stp->ls_stateid.other[2] == 0xffffffff)
1555 * Check for restart conditions (client and server).
1557 error = nfsrv_checkrestart(clientid, new_stp->ls_flags,
1558 &new_stp->ls_stateid, specialid);
1563 * Check for state resource limit exceeded.
1565 if ((new_stp->ls_flags & NFSLCK_LOCK) &&
1566 nfsrv_openpluslock > nfsrv_v4statelimit) {
1567 error = NFSERR_RESOURCE;
1572 * For the lock case, get another nfslock structure,
1573 * just in case we need it.
1574 * Malloc now, before we start sifting through the linked lists,
1575 * in case we have to wait for memory.
1578 if (new_stp->ls_flags & NFSLCK_LOCK)
1579 MALLOC(other_lop, struct nfslock *, sizeof (struct nfslock),
1580 M_NFSDLOCK, M_WAITOK);
1581 filestruct_locked = 0;
1586 * Get the lockfile structure for CFH now, so we can do a sanity
1587 * check against the stateid, before incrementing the seqid#, since
1588 * we want to return NFSERR_BADSTATEID on failure and the seqid#
1589 * shouldn't be incremented for this case.
1590 * If nfsrv_getlockfile() returns -1, it means "not found", which
1591 * will be handled later.
1592 * If we are doing Lock/LockU and local locking is enabled, sleep
1593 * lock the nfslockfile structure.
1595 getlckret = nfsrv_getlockfh(vp, new_stp->ls_flags, NULL, &nfh, p);
1597 if (getlckret == 0) {
1598 if ((new_stp->ls_flags & (NFSLCK_LOCK | NFSLCK_UNLOCK)) != 0 &&
1599 nfsrv_dolocallocks != 0 && nd->nd_repstat == 0) {
1600 getlckret = nfsrv_getlockfile(new_stp->ls_flags, NULL,
1603 filestruct_locked = 1;
1605 getlckret = nfsrv_getlockfile(new_stp->ls_flags, NULL,
1608 if (getlckret != 0 && getlckret != -1)
1611 if (filestruct_locked != 0) {
1612 LIST_INIT(&lfp->lf_rollback);
1613 if ((new_stp->ls_flags & NFSLCK_LOCK)) {
1615 * For local locking, do the advisory locking now, so
1616 * that any conflict can be detected. A failure later
1617 * can be rolled back locally. If an error is returned,
1618 * struct nfslockfile has been unlocked and any local
1619 * locking rolled back.
1622 if (vnode_unlocked == 0) {
1623 ASSERT_VOP_ELOCKED(vp, "nfsrv_lockctrl1");
1625 NFSVOPUNLOCK(vp, 0);
1627 reterr = nfsrv_locallock(vp, lfp,
1628 (new_lop->lo_flags & (NFSLCK_READ | NFSLCK_WRITE)),
1629 new_lop->lo_first, new_lop->lo_end, cfp, p);
1634 if (specialid == 0) {
1635 if (new_stp->ls_flags & NFSLCK_TEST) {
1637 * RFC 3530 does not list LockT as an op that renews a
1638 * lease, but the concensus seems to be that it is ok
1639 * for a server to do so.
1641 error = nfsrv_getclient(clientid, CLOPS_RENEW, &clp, NULL,
1642 (nfsquad_t)((u_quad_t)0), 0, nd, p);
1645 * Since NFSERR_EXPIRED, NFSERR_ADMINREVOKED are not valid
1646 * error returns for LockT, just go ahead and test for a lock,
1647 * since there are no locks for this client, but other locks
1648 * can conflict. (ie. same client will always be false)
1650 if (error == NFSERR_EXPIRED || error == NFSERR_ADMINREVOKED)
1654 error = nfsrv_getclient(clientid, CLOPS_RENEW, &clp, NULL,
1655 (nfsquad_t)((u_quad_t)0), 0, nd, p);
1658 * Look up the stateid
1660 error = nfsrv_getstate(clp, &new_stp->ls_stateid,
1661 new_stp->ls_flags, &stp);
1663 * do some sanity checks for an unconfirmed open or a
1664 * stateid that refers to the wrong file, for an open stateid
1666 if (error == 0 && (stp->ls_flags & NFSLCK_OPEN) &&
1667 ((stp->ls_openowner->ls_flags & NFSLCK_NEEDSCONFIRM) ||
1668 (getlckret == 0 && stp->ls_lfp != lfp))){
1670 * NFSLCK_SETATTR should return OK rather than NFSERR_BADSTATEID
1671 * The only exception is using SETATTR with SIZE.
1673 if ((new_stp->ls_flags &
1674 (NFSLCK_SETATTR | NFSLCK_CHECK)) != NFSLCK_SETATTR)
1675 error = NFSERR_BADSTATEID;
1679 (stp->ls_flags & (NFSLCK_DELEGREAD | NFSLCK_DELEGWRITE)) &&
1680 getlckret == 0 && stp->ls_lfp != lfp)
1681 error = NFSERR_BADSTATEID;
1684 * If the lockowner stateid doesn't refer to the same file,
1685 * I believe that is considered ok, since some clients will
1686 * only create a single lockowner and use that for all locks
1688 * For now, log it as a diagnostic, instead of considering it
1691 if (error == 0 && (stp->ls_flags &
1692 (NFSLCK_OPEN | NFSLCK_DELEGREAD | NFSLCK_DELEGWRITE)) == 0 &&
1693 getlckret == 0 && stp->ls_lfp != lfp) {
1695 printf("Got a lock statid for different file open\n");
1698 error = NFSERR_BADSTATEID;
1703 if (new_stp->ls_flags & NFSLCK_OPENTOLOCK) {
1705 * If haslock set, we've already checked the seqid.
1708 if (stp->ls_flags & NFSLCK_OPEN)
1709 error = nfsrv_checkseqid(nd, new_stp->ls_seq,
1710 stp->ls_openowner, new_stp->ls_op);
1712 error = NFSERR_BADSTATEID;
1715 nfsrv_getowner(&stp->ls_open, new_stp, &lckstp);
1718 * I believe this should be an error, but it
1719 * isn't obvious what NFSERR_xxx would be
1720 * appropriate, so I'll use NFSERR_INVAL for now.
1722 error = NFSERR_INVAL;
1725 } else if (new_stp->ls_flags&(NFSLCK_LOCK|NFSLCK_UNLOCK)) {
1727 * If haslock set, ditto above.
1730 if (stp->ls_flags & NFSLCK_OPEN)
1731 error = NFSERR_BADSTATEID;
1733 error = nfsrv_checkseqid(nd, new_stp->ls_seq,
1734 stp, new_stp->ls_op);
1742 * If the seqid part of the stateid isn't the same, return
1743 * NFSERR_OLDSTATEID for cases other than I/O Ops.
1744 * For I/O Ops, only return NFSERR_OLDSTATEID if
1745 * nfsrv_returnoldstateid is set. (The concensus on the email
1746 * list was that most clients would prefer to not receive
1747 * NFSERR_OLDSTATEID for I/O Ops, but the RFC suggests that that
1748 * is what will happen, so I use the nfsrv_returnoldstateid to
1749 * allow for either server configuration.)
1751 if (!error && stp->ls_stateid.seqid!=new_stp->ls_stateid.seqid &&
1752 (((nd->nd_flag & ND_NFSV41) == 0 &&
1753 (!(new_stp->ls_flags & NFSLCK_CHECK) ||
1754 nfsrv_returnoldstateid)) ||
1755 ((nd->nd_flag & ND_NFSV41) != 0 &&
1756 new_stp->ls_stateid.seqid != 0)))
1757 error = NFSERR_OLDSTATEID;
1762 * Now we can check for grace.
1765 error = nfsrv_checkgrace(nd, clp, new_stp->ls_flags);
1766 if ((new_stp->ls_flags & NFSLCK_RECLAIM) && !error &&
1767 nfsrv_checkstable(clp))
1768 error = NFSERR_NOGRACE;
1770 * If we successfully Reclaimed state, note that.
1772 if ((new_stp->ls_flags & NFSLCK_RECLAIM) && !error)
1773 nfsrv_markstable(clp);
1776 * At this point, either error == NFSERR_BADSTATEID or the
1777 * seqid# has been updated, so we can return any error.
1778 * If error == 0, there may be an error in:
1779 * nd_repstat - Set by the calling function.
1780 * reterr - Set above, if getting the nfslockfile structure
1781 * or acquiring the local lock failed.
1782 * (If both of these are set, nd_repstat should probably be
1783 * returned, since that error was detected before this
1786 if (error != 0 || nd->nd_repstat != 0 || reterr != 0) {
1788 if (nd->nd_repstat != 0)
1789 error = nd->nd_repstat;
1793 if (filestruct_locked != 0) {
1794 /* Roll back local locks. */
1796 if (vnode_unlocked == 0) {
1797 ASSERT_VOP_ELOCKED(vp, "nfsrv_lockctrl2");
1799 NFSVOPUNLOCK(vp, 0);
1801 nfsrv_locallock_rollback(vp, lfp, p);
1803 nfsrv_unlocklf(lfp);
1810 * Check the nfsrv_getlockfile return.
1811 * Returned -1 if no structure found.
1813 if (getlckret == -1) {
1814 error = NFSERR_EXPIRED;
1816 * Called from lockt, so no lock is OK.
1818 if (new_stp->ls_flags & NFSLCK_TEST) {
1820 } else if (new_stp->ls_flags &
1821 (NFSLCK_CHECK | NFSLCK_SETATTR)) {
1823 * Called to check for a lock, OK if the stateid is all
1824 * 1s or all 0s, but there should be an nfsstate
1826 * (ie. If there is no open, I'll assume no share
1832 error = NFSERR_BADSTATEID;
1839 * For NFSLCK_CHECK and NFSLCK_LOCK, test for a share conflict.
1840 * For NFSLCK_CHECK, allow a read if write access is granted,
1841 * but check for a deny. For NFSLCK_LOCK, require correct access,
1842 * which implies a conflicting deny can't exist.
1844 if (new_stp->ls_flags & (NFSLCK_CHECK | NFSLCK_LOCK)) {
1846 * Four kinds of state id:
1847 * - specialid (all 0s or all 1s), only for NFSLCK_CHECK
1848 * - stateid for an open
1849 * - stateid for a delegation
1850 * - stateid for a lock owner
1853 if (stp->ls_flags & (NFSLCK_DELEGREAD | NFSLCK_DELEGWRITE)) {
1856 nfsrv_delaydelegtimeout(stp);
1857 } else if (stp->ls_flags & NFSLCK_OPEN) {
1860 mystp = stp->ls_openstp;
1863 * If locking or checking, require correct access
1866 if (((new_stp->ls_flags & NFSLCK_LOCK) &&
1867 !((new_lop->lo_flags >> NFSLCK_LOCKSHIFT) &
1868 mystp->ls_flags & NFSLCK_ACCESSBITS)) ||
1869 ((new_stp->ls_flags & (NFSLCK_CHECK|NFSLCK_READACCESS)) ==
1870 (NFSLCK_CHECK | NFSLCK_READACCESS) &&
1871 !(mystp->ls_flags & NFSLCK_READACCESS)) ||
1872 ((new_stp->ls_flags & (NFSLCK_CHECK|NFSLCK_WRITEACCESS)) ==
1873 (NFSLCK_CHECK | NFSLCK_WRITEACCESS) &&
1874 !(mystp->ls_flags & NFSLCK_WRITEACCESS))) {
1875 if (filestruct_locked != 0) {
1876 /* Roll back local locks. */
1878 if (vnode_unlocked == 0) {
1879 ASSERT_VOP_ELOCKED(vp,
1882 NFSVOPUNLOCK(vp, 0);
1884 nfsrv_locallock_rollback(vp, lfp, p);
1886 nfsrv_unlocklf(lfp);
1889 error = NFSERR_OPENMODE;
1894 if ((new_stp->ls_flags & NFSLCK_CHECK) && !delegation) {
1896 * Check for a conflicting deny bit.
1898 LIST_FOREACH(tstp, &lfp->lf_open, ls_file) {
1899 if (tstp != mystp) {
1900 bits = tstp->ls_flags;
1901 bits >>= NFSLCK_SHIFT;
1902 if (new_stp->ls_flags & bits & NFSLCK_ACCESSBITS) {
1903 KASSERT(vnode_unlocked == 0,
1904 ("nfsrv_lockctrl: vnode unlocked1"));
1905 ret = nfsrv_clientconflict(tstp->ls_clp, &haslock,
1909 * nfsrv_clientconflict unlocks state
1910 * when it returns non-zero.
1918 error = NFSERR_PERM;
1920 error = NFSERR_OPENMODE;
1926 /* We're outta here */
1933 * For setattr, just get rid of all the Delegations for other clients.
1935 if (new_stp->ls_flags & NFSLCK_SETATTR) {
1936 KASSERT(vnode_unlocked == 0,
1937 ("nfsrv_lockctrl: vnode unlocked2"));
1938 ret = nfsrv_cleandeleg(vp, lfp, clp, &haslock, p);
1941 * nfsrv_cleandeleg() unlocks state when it
1951 if (!(new_stp->ls_flags & NFSLCK_CHECK) ||
1952 (LIST_EMPTY(&lfp->lf_open) && LIST_EMPTY(&lfp->lf_lock) &&
1953 LIST_EMPTY(&lfp->lf_deleg))) {
1960 * Check for a conflicting delegation. If one is found, call
1961 * nfsrv_delegconflict() to handle it. If the v4root lock hasn't
1962 * been set yet, it will get the lock. Otherwise, it will recall
1963 * the delegation. Then, we try try again...
1964 * I currently believe the conflict algorithm to be:
1965 * For Lock Ops (Lock/LockT/LockU)
1966 * - there is a conflict iff a different client has a write delegation
1967 * For Reading (Read Op)
1968 * - there is a conflict iff a different client has a write delegation
1969 * (the specialids are always a different client)
1970 * For Writing (Write/Setattr of size)
1971 * - there is a conflict if a different client has any delegation
1972 * - there is a conflict if the same client has a read delegation
1973 * (I don't understand why this isn't allowed, but that seems to be
1974 * the current concensus?)
1976 tstp = LIST_FIRST(&lfp->lf_deleg);
1977 while (tstp != LIST_END(&lfp->lf_deleg)) {
1978 nstp = LIST_NEXT(tstp, ls_file);
1979 if ((((new_stp->ls_flags&(NFSLCK_LOCK|NFSLCK_UNLOCK|NFSLCK_TEST))||
1980 ((new_stp->ls_flags & NFSLCK_CHECK) &&
1981 (new_lop->lo_flags & NFSLCK_READ))) &&
1982 clp != tstp->ls_clp &&
1983 (tstp->ls_flags & NFSLCK_DELEGWRITE)) ||
1984 ((new_stp->ls_flags & NFSLCK_CHECK) &&
1985 (new_lop->lo_flags & NFSLCK_WRITE) &&
1986 (clp != tstp->ls_clp ||
1987 (tstp->ls_flags & NFSLCK_DELEGREAD)))) {
1989 if (filestruct_locked != 0) {
1990 /* Roll back local locks. */
1992 if (vnode_unlocked == 0) {
1993 ASSERT_VOP_ELOCKED(vp, "nfsrv_lockctrl4");
1994 NFSVOPUNLOCK(vp, 0);
1996 nfsrv_locallock_rollback(vp, lfp, p);
1998 nfsrv_unlocklf(lfp);
2000 NFSVOPLOCK(vp, LK_EXCLUSIVE | LK_RETRY);
2002 if ((vp->v_iflag & VI_DOOMED) != 0)
2003 ret = NFSERR_SERVERFAULT;
2007 ret = nfsrv_delegconflict(tstp, &haslock, p, vp);
2010 * nfsrv_delegconflict unlocks state when it
2011 * returns non-zero, which it always does.
2014 FREE((caddr_t)other_lop, M_NFSDLOCK);
2024 /* Never gets here. */
2030 * Handle the unlock case by calling nfsrv_updatelock().
2031 * (Should I have done some access checking above for unlock? For now,
2032 * just let it happen.)
2034 if (new_stp->ls_flags & NFSLCK_UNLOCK) {
2035 first = new_lop->lo_first;
2036 end = new_lop->lo_end;
2037 nfsrv_updatelock(stp, new_lopp, &other_lop, lfp);
2038 stateidp->seqid = ++(stp->ls_stateid.seqid);
2039 if ((nd->nd_flag & ND_NFSV41) != 0 && stateidp->seqid == 0)
2040 stateidp->seqid = stp->ls_stateid.seqid = 1;
2041 stateidp->other[0] = stp->ls_stateid.other[0];
2042 stateidp->other[1] = stp->ls_stateid.other[1];
2043 stateidp->other[2] = stp->ls_stateid.other[2];
2044 if (filestruct_locked != 0) {
2046 if (vnode_unlocked == 0) {
2047 ASSERT_VOP_ELOCKED(vp, "nfsrv_lockctrl5");
2049 NFSVOPUNLOCK(vp, 0);
2051 /* Update the local locks. */
2052 nfsrv_localunlock(vp, lfp, first, end, p);
2054 nfsrv_unlocklf(lfp);
2061 * Search for a conflicting lock. A lock conflicts if:
2062 * - the lock range overlaps and
2063 * - at least one lock is a write lock and
2064 * - it is not owned by the same lock owner
2067 LIST_FOREACH(lop, &lfp->lf_lock, lo_lckfile) {
2068 if (new_lop->lo_end > lop->lo_first &&
2069 new_lop->lo_first < lop->lo_end &&
2070 (new_lop->lo_flags == NFSLCK_WRITE ||
2071 lop->lo_flags == NFSLCK_WRITE) &&
2072 lckstp != lop->lo_stp &&
2073 (clp != lop->lo_stp->ls_clp ||
2074 lckstp->ls_ownerlen != lop->lo_stp->ls_ownerlen ||
2075 NFSBCMP(lckstp->ls_owner, lop->lo_stp->ls_owner,
2076 lckstp->ls_ownerlen))) {
2078 FREE((caddr_t)other_lop, M_NFSDLOCK);
2081 if (vnode_unlocked != 0)
2082 ret = nfsrv_clientconflict(lop->lo_stp->ls_clp, &haslock,
2085 ret = nfsrv_clientconflict(lop->lo_stp->ls_clp, &haslock,
2088 if (filestruct_locked != 0) {
2089 if (vnode_unlocked == 0) {
2090 ASSERT_VOP_ELOCKED(vp, "nfsrv_lockctrl6");
2091 NFSVOPUNLOCK(vp, 0);
2093 /* Roll back local locks. */
2094 nfsrv_locallock_rollback(vp, lfp, p);
2096 nfsrv_unlocklf(lfp);
2098 NFSVOPLOCK(vp, LK_EXCLUSIVE | LK_RETRY);
2100 if ((vp->v_iflag & VI_DOOMED) != 0) {
2101 error = NFSERR_SERVERFAULT;
2106 * nfsrv_clientconflict() unlocks state when it
2113 * Found a conflicting lock, so record the conflict and
2116 if (cfp != NULL && ret == 0) {
2117 cfp->cl_clientid.lval[0]=lop->lo_stp->ls_stateid.other[0];
2118 cfp->cl_clientid.lval[1]=lop->lo_stp->ls_stateid.other[1];
2119 cfp->cl_first = lop->lo_first;
2120 cfp->cl_end = lop->lo_end;
2121 cfp->cl_flags = lop->lo_flags;
2122 cfp->cl_ownerlen = lop->lo_stp->ls_ownerlen;
2123 NFSBCOPY(lop->lo_stp->ls_owner, cfp->cl_owner,
2127 error = NFSERR_PERM;
2128 else if (new_stp->ls_flags & NFSLCK_RECLAIM)
2129 error = NFSERR_RECLAIMCONFLICT;
2130 else if (new_stp->ls_flags & NFSLCK_CHECK)
2131 error = NFSERR_LOCKED;
2133 error = NFSERR_DENIED;
2134 if (filestruct_locked != 0 && ret == 0) {
2135 /* Roll back local locks. */
2137 if (vnode_unlocked == 0) {
2138 ASSERT_VOP_ELOCKED(vp, "nfsrv_lockctrl7");
2140 NFSVOPUNLOCK(vp, 0);
2142 nfsrv_locallock_rollback(vp, lfp, p);
2144 nfsrv_unlocklf(lfp);
2154 * We only get here if there was no lock that conflicted.
2156 if (new_stp->ls_flags & (NFSLCK_TEST | NFSLCK_CHECK)) {
2162 * We only get here when we are creating or modifying a lock.
2163 * There are two variants:
2164 * - exist_lock_owner where lock_owner exists
2165 * - open_to_lock_owner with new lock_owner
2167 first = new_lop->lo_first;
2168 end = new_lop->lo_end;
2169 lock_flags = new_lop->lo_flags;
2170 if (!(new_stp->ls_flags & NFSLCK_OPENTOLOCK)) {
2171 nfsrv_updatelock(lckstp, new_lopp, &other_lop, lfp);
2172 stateidp->seqid = ++(lckstp->ls_stateid.seqid);
2173 if ((nd->nd_flag & ND_NFSV41) != 0 && stateidp->seqid == 0)
2174 stateidp->seqid = lckstp->ls_stateid.seqid = 1;
2175 stateidp->other[0] = lckstp->ls_stateid.other[0];
2176 stateidp->other[1] = lckstp->ls_stateid.other[1];
2177 stateidp->other[2] = lckstp->ls_stateid.other[2];
2180 * The new open_to_lock_owner case.
2181 * Link the new nfsstate into the lists.
2183 new_stp->ls_seq = new_stp->ls_opentolockseq;
2184 nfsrvd_refcache(new_stp->ls_op);
2185 stateidp->seqid = new_stp->ls_stateid.seqid = 1;
2186 stateidp->other[0] = new_stp->ls_stateid.other[0] =
2187 clp->lc_clientid.lval[0];
2188 stateidp->other[1] = new_stp->ls_stateid.other[1] =
2189 clp->lc_clientid.lval[1];
2190 stateidp->other[2] = new_stp->ls_stateid.other[2] =
2191 nfsrv_nextstateindex(clp);
2192 new_stp->ls_clp = clp;
2193 LIST_INIT(&new_stp->ls_lock);
2194 new_stp->ls_openstp = stp;
2195 new_stp->ls_lfp = lfp;
2196 nfsrv_insertlock(new_lop, (struct nfslock *)new_stp, new_stp,
2198 LIST_INSERT_HEAD(NFSSTATEHASH(clp, new_stp->ls_stateid),
2200 LIST_INSERT_HEAD(&stp->ls_open, new_stp, ls_list);
2203 newnfsstats.srvlockowners++;
2204 nfsrv_openpluslock++;
2206 if (filestruct_locked != 0) {
2208 nfsrv_locallock_commit(lfp, lock_flags, first, end);
2210 nfsrv_unlocklf(lfp);
2216 NFSLOCKV4ROOTMUTEX();
2217 nfsv4_unlock(&nfsv4rootfs_lock, 1);
2218 NFSUNLOCKV4ROOTMUTEX();
2220 if (vnode_unlocked != 0) {
2221 NFSVOPLOCK(vp, LK_EXCLUSIVE | LK_RETRY);
2222 if (error == 0 && (vp->v_iflag & VI_DOOMED) != 0)
2223 error = NFSERR_SERVERFAULT;
2226 FREE((caddr_t)other_lop, M_NFSDLOCK);
2227 NFSEXITCODE2(error, nd);
2232 * Check for state errors for Open.
2233 * repstat is passed back out as an error if more critical errors
2237 nfsrv_opencheck(nfsquad_t clientid, nfsv4stateid_t *stateidp,
2238 struct nfsstate *new_stp, vnode_t vp, struct nfsrv_descript *nd,
2239 NFSPROC_T *p, int repstat)
2241 struct nfsstate *stp, *nstp;
2242 struct nfsclient *clp;
2243 struct nfsstate *ownerstp;
2244 struct nfslockfile *lfp, *new_lfp;
2245 int error = 0, haslock = 0, ret, readonly = 0, getfhret = 0;
2247 if ((new_stp->ls_flags & NFSLCK_SHAREBITS) == NFSLCK_READACCESS)
2250 * Check for restart conditions (client and server).
2252 error = nfsrv_checkrestart(clientid, new_stp->ls_flags,
2253 &new_stp->ls_stateid, 0);
2258 * Check for state resource limit exceeded.
2259 * Technically this should be SMP protected, but the worst
2260 * case error is "out by one or two" on the count when it
2261 * returns NFSERR_RESOURCE and the limit is just a rather
2262 * arbitrary high water mark, so no harm is done.
2264 if (nfsrv_openpluslock > nfsrv_v4statelimit) {
2265 error = NFSERR_RESOURCE;
2270 MALLOC(new_lfp, struct nfslockfile *, sizeof (struct nfslockfile),
2271 M_NFSDLOCKFILE, M_WAITOK);
2273 getfhret = nfsrv_getlockfh(vp, new_stp->ls_flags, new_lfp,
2277 * Get the nfsclient structure.
2279 error = nfsrv_getclient(clientid, CLOPS_RENEW, &clp, NULL,
2280 (nfsquad_t)((u_quad_t)0), 0, nd, p);
2283 * Look up the open owner. See if it needs confirmation and
2284 * check the seq#, as required.
2287 nfsrv_getowner(&clp->lc_open, new_stp, &ownerstp);
2289 if (!error && ownerstp) {
2290 error = nfsrv_checkseqid(nd, new_stp->ls_seq, ownerstp,
2293 * If the OpenOwner hasn't been confirmed, assume the
2294 * old one was a replay and this one is ok.
2295 * See: RFC3530 Sec. 14.2.18.
2297 if (error == NFSERR_BADSEQID &&
2298 (ownerstp->ls_flags & NFSLCK_NEEDSCONFIRM))
2306 error = nfsrv_checkgrace(nd, clp, new_stp->ls_flags);
2307 if ((new_stp->ls_flags & NFSLCK_RECLAIM) && !error &&
2308 nfsrv_checkstable(clp))
2309 error = NFSERR_NOGRACE;
2312 * If none of the above errors occurred, let repstat be
2315 if (repstat && !error)
2320 NFSLOCKV4ROOTMUTEX();
2321 nfsv4_unlock(&nfsv4rootfs_lock, 1);
2322 NFSUNLOCKV4ROOTMUTEX();
2324 free((caddr_t)new_lfp, M_NFSDLOCKFILE);
2329 * If vp == NULL, the file doesn't exist yet, so return ok.
2330 * (This always happens on the first pass, so haslock must be 0.)
2334 FREE((caddr_t)new_lfp, M_NFSDLOCKFILE);
2339 * Get the structure for the underlying file.
2344 error = nfsrv_getlockfile(new_stp->ls_flags, &new_lfp, &lfp,
2347 FREE((caddr_t)new_lfp, M_NFSDLOCKFILE);
2351 NFSLOCKV4ROOTMUTEX();
2352 nfsv4_unlock(&nfsv4rootfs_lock, 1);
2353 NFSUNLOCKV4ROOTMUTEX();
2359 * Search for a conflicting open/share.
2361 if (new_stp->ls_flags & NFSLCK_DELEGCUR) {
2363 * For Delegate_Cur, search for the matching Delegation,
2364 * which indicates no conflict.
2365 * An old delegation should have been recovered by the
2366 * client doing a Claim_DELEGATE_Prev, so I won't let
2367 * it match and return NFSERR_EXPIRED. Should I let it
2370 LIST_FOREACH(stp, &lfp->lf_deleg, ls_file) {
2371 if (!(stp->ls_flags & NFSLCK_OLDDELEG) &&
2372 (((nd->nd_flag & ND_NFSV41) != 0 &&
2373 stateidp->seqid == 0) ||
2374 stateidp->seqid == stp->ls_stateid.seqid) &&
2375 !NFSBCMP(stateidp->other, stp->ls_stateid.other,
2379 if (stp == LIST_END(&lfp->lf_deleg) ||
2380 ((new_stp->ls_flags & NFSLCK_WRITEACCESS) &&
2381 (stp->ls_flags & NFSLCK_DELEGREAD))) {
2384 NFSLOCKV4ROOTMUTEX();
2385 nfsv4_unlock(&nfsv4rootfs_lock, 1);
2386 NFSUNLOCKV4ROOTMUTEX();
2388 error = NFSERR_EXPIRED;
2394 * Check for access/deny bit conflicts. I check for the same
2395 * owner as well, in case the client didn't bother.
2397 LIST_FOREACH(stp, &lfp->lf_open, ls_file) {
2398 if (!(new_stp->ls_flags & NFSLCK_DELEGCUR) &&
2399 (((new_stp->ls_flags & NFSLCK_ACCESSBITS) &
2400 ((stp->ls_flags>>NFSLCK_SHIFT) & NFSLCK_ACCESSBITS))||
2401 ((stp->ls_flags & NFSLCK_ACCESSBITS) &
2402 ((new_stp->ls_flags>>NFSLCK_SHIFT)&NFSLCK_ACCESSBITS)))){
2403 ret = nfsrv_clientconflict(stp->ls_clp,&haslock,vp,p);
2406 * nfsrv_clientconflict() unlocks
2407 * state when it returns non-zero.
2412 error = NFSERR_PERM;
2413 else if (new_stp->ls_flags & NFSLCK_RECLAIM)
2414 error = NFSERR_RECLAIMCONFLICT;
2416 error = NFSERR_SHAREDENIED;
2420 NFSLOCKV4ROOTMUTEX();
2421 nfsv4_unlock(&nfsv4rootfs_lock, 1);
2422 NFSUNLOCKV4ROOTMUTEX();
2429 * Check for a conflicting delegation. If one is found, call
2430 * nfsrv_delegconflict() to handle it. If the v4root lock hasn't
2431 * been set yet, it will get the lock. Otherwise, it will recall
2432 * the delegation. Then, we try try again...
2433 * (If NFSLCK_DELEGCUR is set, it has a delegation, so there
2434 * isn't a conflict.)
2435 * I currently believe the conflict algorithm to be:
2436 * For Open with Read Access and Deny None
2437 * - there is a conflict iff a different client has a write delegation
2438 * For Open with other Write Access or any Deny except None
2439 * - there is a conflict if a different client has any delegation
2440 * - there is a conflict if the same client has a read delegation
2441 * (The current concensus is that this last case should be
2442 * considered a conflict since the client with a read delegation
2443 * could have done an Open with ReadAccess and WriteDeny
2444 * locally and then not have checked for the WriteDeny.)
2445 * Don't check for a Reclaim, since that will be dealt with
2446 * by nfsrv_openctrl().
2448 if (!(new_stp->ls_flags &
2449 (NFSLCK_DELEGPREV | NFSLCK_DELEGCUR | NFSLCK_RECLAIM))) {
2450 stp = LIST_FIRST(&lfp->lf_deleg);
2451 while (stp != LIST_END(&lfp->lf_deleg)) {
2452 nstp = LIST_NEXT(stp, ls_file);
2453 if ((readonly && stp->ls_clp != clp &&
2454 (stp->ls_flags & NFSLCK_DELEGWRITE)) ||
2455 (!readonly && (stp->ls_clp != clp ||
2456 (stp->ls_flags & NFSLCK_DELEGREAD)))) {
2457 ret = nfsrv_delegconflict(stp, &haslock, p, vp);
2460 * nfsrv_delegconflict() unlocks state
2461 * when it returns non-zero.
2474 NFSLOCKV4ROOTMUTEX();
2475 nfsv4_unlock(&nfsv4rootfs_lock, 1);
2476 NFSUNLOCKV4ROOTMUTEX();
2480 NFSEXITCODE2(error, nd);
2485 * Open control function to create/update open state for an open.
2488 nfsrv_openctrl(struct nfsrv_descript *nd, vnode_t vp,
2489 struct nfsstate **new_stpp, nfsquad_t clientid, nfsv4stateid_t *stateidp,
2490 nfsv4stateid_t *delegstateidp, u_int32_t *rflagsp, struct nfsexstuff *exp,
2491 NFSPROC_T *p, u_quad_t filerev)
2493 struct nfsstate *new_stp = *new_stpp;
2494 struct nfsstate *stp, *nstp;
2495 struct nfsstate *openstp = NULL, *new_open, *ownerstp, *new_deleg;
2496 struct nfslockfile *lfp, *new_lfp;
2497 struct nfsclient *clp;
2498 int error = 0, haslock = 0, ret, delegate = 1, writedeleg = 1;
2499 int readonly = 0, cbret = 1, getfhret = 0;
2501 if ((new_stp->ls_flags & NFSLCK_SHAREBITS) == NFSLCK_READACCESS)
2504 * Check for restart conditions (client and server).
2505 * (Paranoia, should have been detected by nfsrv_opencheck().)
2506 * If an error does show up, return NFSERR_EXPIRED, since the
2507 * the seqid# has already been incremented.
2509 error = nfsrv_checkrestart(clientid, new_stp->ls_flags,
2510 &new_stp->ls_stateid, 0);
2512 printf("Nfsd: openctrl unexpected restart err=%d\n",
2514 error = NFSERR_EXPIRED;
2519 MALLOC(new_lfp, struct nfslockfile *, sizeof (struct nfslockfile),
2520 M_NFSDLOCKFILE, M_WAITOK);
2521 MALLOC(new_open, struct nfsstate *, sizeof (struct nfsstate),
2522 M_NFSDSTATE, M_WAITOK);
2523 MALLOC(new_deleg, struct nfsstate *, sizeof (struct nfsstate),
2524 M_NFSDSTATE, M_WAITOK);
2525 getfhret = nfsrv_getlockfh(vp, new_stp->ls_flags, new_lfp,
2529 * Get the client structure. Since the linked lists could be changed
2530 * by other nfsd processes if this process does a tsleep(), one of
2531 * two things must be done.
2532 * 1 - don't tsleep()
2534 * 2 - get the nfsv4_lock() { indicated by haslock == 1 }
2535 * before using the lists, since this lock stops the other
2536 * nfsd. This should only be used for rare cases, since it
2537 * essentially single threads the nfsd.
2538 * At this time, it is only done for cases where the stable
2539 * storage file must be written prior to completion of state
2542 error = nfsrv_getclient(clientid, CLOPS_RENEW, &clp, NULL,
2543 (nfsquad_t)((u_quad_t)0), 0, nd, p);
2544 if (!error && (clp->lc_flags & LCL_NEEDSCBNULL) &&
2547 * This happens on the first open for a client
2548 * that supports callbacks.
2552 * Although nfsrv_docallback() will sleep, clp won't
2553 * go away, since they are only removed when the
2554 * nfsv4_lock() has blocked the nfsd threads. The
2555 * fields in clp can change, but having multiple
2556 * threads do this Null callback RPC should be
2559 cbret = nfsrv_docallback(clp, NFSV4PROC_CBNULL,
2560 NULL, 0, NULL, NULL, NULL, p);
2562 clp->lc_flags &= ~LCL_NEEDSCBNULL;
2564 clp->lc_flags |= LCL_CALLBACKSON;
2568 * Look up the open owner. See if it needs confirmation and
2569 * check the seq#, as required.
2572 nfsrv_getowner(&clp->lc_open, new_stp, &ownerstp);
2576 printf("Nfsd: openctrl unexpected state err=%d\n",
2578 free((caddr_t)new_lfp, M_NFSDLOCKFILE);
2579 free((caddr_t)new_open, M_NFSDSTATE);
2580 free((caddr_t)new_deleg, M_NFSDSTATE);
2582 NFSLOCKV4ROOTMUTEX();
2583 nfsv4_unlock(&nfsv4rootfs_lock, 1);
2584 NFSUNLOCKV4ROOTMUTEX();
2586 error = NFSERR_EXPIRED;
2590 if (new_stp->ls_flags & NFSLCK_RECLAIM)
2591 nfsrv_markstable(clp);
2594 * Get the structure for the underlying file.
2599 error = nfsrv_getlockfile(new_stp->ls_flags, &new_lfp, &lfp,
2602 FREE((caddr_t)new_lfp, M_NFSDLOCKFILE);
2605 printf("Nfsd openctrl unexpected getlockfile err=%d\n",
2607 free((caddr_t)new_open, M_NFSDSTATE);
2608 free((caddr_t)new_deleg, M_NFSDSTATE);
2610 NFSLOCKV4ROOTMUTEX();
2611 nfsv4_unlock(&nfsv4rootfs_lock, 1);
2612 NFSUNLOCKV4ROOTMUTEX();
2618 * Search for a conflicting open/share.
2620 if (new_stp->ls_flags & NFSLCK_DELEGCUR) {
2622 * For Delegate_Cur, search for the matching Delegation,
2623 * which indicates no conflict.
2624 * An old delegation should have been recovered by the
2625 * client doing a Claim_DELEGATE_Prev, so I won't let
2626 * it match and return NFSERR_EXPIRED. Should I let it
2629 LIST_FOREACH(stp, &lfp->lf_deleg, ls_file) {
2630 if (!(stp->ls_flags & NFSLCK_OLDDELEG) &&
2631 (((nd->nd_flag & ND_NFSV41) != 0 &&
2632 stateidp->seqid == 0) ||
2633 stateidp->seqid == stp->ls_stateid.seqid) &&
2634 !NFSBCMP(stateidp->other, stp->ls_stateid.other,
2638 if (stp == LIST_END(&lfp->lf_deleg) ||
2639 ((new_stp->ls_flags & NFSLCK_WRITEACCESS) &&
2640 (stp->ls_flags & NFSLCK_DELEGREAD))) {
2642 printf("Nfsd openctrl unexpected expiry\n");
2643 free((caddr_t)new_open, M_NFSDSTATE);
2644 free((caddr_t)new_deleg, M_NFSDSTATE);
2646 NFSLOCKV4ROOTMUTEX();
2647 nfsv4_unlock(&nfsv4rootfs_lock, 1);
2648 NFSUNLOCKV4ROOTMUTEX();
2650 error = NFSERR_EXPIRED;
2655 * Don't issue a Delegation, since one already exists and
2656 * delay delegation timeout, as required.
2659 nfsrv_delaydelegtimeout(stp);
2663 * Check for access/deny bit conflicts. I also check for the
2664 * same owner, since the client might not have bothered to check.
2665 * Also, note an open for the same file and owner, if found,
2666 * which is all we do here for Delegate_Cur, since conflict
2667 * checking is already done.
2669 LIST_FOREACH(stp, &lfp->lf_open, ls_file) {
2670 if (ownerstp && stp->ls_openowner == ownerstp)
2672 if (!(new_stp->ls_flags & NFSLCK_DELEGCUR)) {
2674 * If another client has the file open, the only
2675 * delegation that can be issued is a Read delegation
2676 * and only if it is a Read open with Deny none.
2678 if (clp != stp->ls_clp) {
2679 if ((stp->ls_flags & NFSLCK_SHAREBITS) ==
2685 if(((new_stp->ls_flags & NFSLCK_ACCESSBITS) &
2686 ((stp->ls_flags>>NFSLCK_SHIFT) & NFSLCK_ACCESSBITS))||
2687 ((stp->ls_flags & NFSLCK_ACCESSBITS) &
2688 ((new_stp->ls_flags>>NFSLCK_SHIFT)&NFSLCK_ACCESSBITS))){
2689 ret = nfsrv_clientconflict(stp->ls_clp,&haslock,vp,p);
2692 * nfsrv_clientconflict() unlocks state
2693 * when it returns non-zero.
2695 free((caddr_t)new_open, M_NFSDSTATE);
2696 free((caddr_t)new_deleg, M_NFSDSTATE);
2701 error = NFSERR_PERM;
2702 else if (new_stp->ls_flags & NFSLCK_RECLAIM)
2703 error = NFSERR_RECLAIMCONFLICT;
2705 error = NFSERR_SHAREDENIED;
2709 NFSLOCKV4ROOTMUTEX();
2710 nfsv4_unlock(&nfsv4rootfs_lock, 1);
2711 NFSUNLOCKV4ROOTMUTEX();
2713 free((caddr_t)new_open, M_NFSDSTATE);
2714 free((caddr_t)new_deleg, M_NFSDSTATE);
2715 printf("nfsd openctrl unexpected client cnfl\n");
2722 * Check for a conflicting delegation. If one is found, call
2723 * nfsrv_delegconflict() to handle it. If the v4root lock hasn't
2724 * been set yet, it will get the lock. Otherwise, it will recall
2725 * the delegation. Then, we try try again...
2726 * (If NFSLCK_DELEGCUR is set, it has a delegation, so there
2727 * isn't a conflict.)
2728 * I currently believe the conflict algorithm to be:
2729 * For Open with Read Access and Deny None
2730 * - there is a conflict iff a different client has a write delegation
2731 * For Open with other Write Access or any Deny except None
2732 * - there is a conflict if a different client has any delegation
2733 * - there is a conflict if the same client has a read delegation
2734 * (The current concensus is that this last case should be
2735 * considered a conflict since the client with a read delegation
2736 * could have done an Open with ReadAccess and WriteDeny
2737 * locally and then not have checked for the WriteDeny.)
2739 if (!(new_stp->ls_flags & (NFSLCK_DELEGPREV | NFSLCK_DELEGCUR))) {
2740 stp = LIST_FIRST(&lfp->lf_deleg);
2741 while (stp != LIST_END(&lfp->lf_deleg)) {
2742 nstp = LIST_NEXT(stp, ls_file);
2743 if (stp->ls_clp != clp && (stp->ls_flags & NFSLCK_DELEGREAD))
2747 if ((readonly && stp->ls_clp != clp &&
2748 (stp->ls_flags & NFSLCK_DELEGWRITE)) ||
2749 (!readonly && (stp->ls_clp != clp ||
2750 (stp->ls_flags & NFSLCK_DELEGREAD)))) {
2751 if (new_stp->ls_flags & NFSLCK_RECLAIM) {
2754 ret = nfsrv_delegconflict(stp, &haslock, p, vp);
2757 * nfsrv_delegconflict() unlocks state
2758 * when it returns non-zero.
2760 printf("Nfsd openctrl unexpected deleg cnfl\n");
2761 free((caddr_t)new_open, M_NFSDSTATE);
2762 free((caddr_t)new_deleg, M_NFSDSTATE);
2777 * We only get here if there was no open that conflicted.
2778 * If an open for the owner exists, or in the access/deny bits.
2779 * Otherwise it is a new open. If the open_owner hasn't been
2780 * confirmed, replace the open with the new one needing confirmation,
2781 * otherwise add the open.
2783 if (new_stp->ls_flags & NFSLCK_DELEGPREV) {
2785 * Handle NFSLCK_DELEGPREV by searching the old delegations for
2786 * a match. If found, just move the old delegation to the current
2787 * delegation list and issue open. If not found, return
2790 LIST_FOREACH(stp, &clp->lc_olddeleg, ls_list) {
2791 if (stp->ls_lfp == lfp) {
2793 if (stp->ls_clp != clp)
2794 panic("olddeleg clp");
2795 LIST_REMOVE(stp, ls_list);
2796 LIST_REMOVE(stp, ls_hash);
2797 stp->ls_flags &= ~NFSLCK_OLDDELEG;
2798 stp->ls_stateid.seqid = delegstateidp->seqid = 1;
2799 stp->ls_stateid.other[0] = delegstateidp->other[0] =
2800 clp->lc_clientid.lval[0];
2801 stp->ls_stateid.other[1] = delegstateidp->other[1] =
2802 clp->lc_clientid.lval[1];
2803 stp->ls_stateid.other[2] = delegstateidp->other[2] =
2804 nfsrv_nextstateindex(clp);
2805 stp->ls_compref = nd->nd_compref;
2806 LIST_INSERT_HEAD(&clp->lc_deleg, stp, ls_list);
2807 LIST_INSERT_HEAD(NFSSTATEHASH(clp,
2808 stp->ls_stateid), stp, ls_hash);
2809 if (stp->ls_flags & NFSLCK_DELEGWRITE)
2810 *rflagsp |= NFSV4OPEN_WRITEDELEGATE;
2812 *rflagsp |= NFSV4OPEN_READDELEGATE;
2813 clp->lc_delegtime = NFSD_MONOSEC +
2814 nfsrv_lease + NFSRV_LEASEDELTA;
2817 * Now, do the associated open.
2819 new_open->ls_stateid.seqid = 1;
2820 new_open->ls_stateid.other[0] = clp->lc_clientid.lval[0];
2821 new_open->ls_stateid.other[1] = clp->lc_clientid.lval[1];
2822 new_open->ls_stateid.other[2] = nfsrv_nextstateindex(clp);
2823 new_open->ls_flags = (new_stp->ls_flags&NFSLCK_DENYBITS)|
2825 if (stp->ls_flags & NFSLCK_DELEGWRITE)
2826 new_open->ls_flags |= (NFSLCK_READACCESS |
2827 NFSLCK_WRITEACCESS);
2829 new_open->ls_flags |= NFSLCK_READACCESS;
2830 new_open->ls_uid = new_stp->ls_uid;
2831 new_open->ls_lfp = lfp;
2832 new_open->ls_clp = clp;
2833 LIST_INIT(&new_open->ls_open);
2834 LIST_INSERT_HEAD(&lfp->lf_open, new_open, ls_file);
2835 LIST_INSERT_HEAD(NFSSTATEHASH(clp, new_open->ls_stateid),
2838 * and handle the open owner
2841 new_open->ls_openowner = ownerstp;
2842 LIST_INSERT_HEAD(&ownerstp->ls_open,new_open,ls_list);
2844 new_open->ls_openowner = new_stp;
2845 new_stp->ls_flags = 0;
2846 nfsrvd_refcache(new_stp->ls_op);
2847 new_stp->ls_noopens = 0;
2848 LIST_INIT(&new_stp->ls_open);
2849 LIST_INSERT_HEAD(&new_stp->ls_open, new_open, ls_list);
2850 LIST_INSERT_HEAD(&clp->lc_open, new_stp, ls_list);
2852 newnfsstats.srvopenowners++;
2853 nfsrv_openpluslock++;
2857 newnfsstats.srvopens++;
2858 nfsrv_openpluslock++;
2862 if (stp == LIST_END(&clp->lc_olddeleg))
2863 error = NFSERR_EXPIRED;
2864 } else if (new_stp->ls_flags & (NFSLCK_DELEGREAD | NFSLCK_DELEGWRITE)) {
2866 * Scan to see that no delegation for this client and file
2867 * doesn't already exist.
2868 * There also shouldn't yet be an Open for this file and
2871 LIST_FOREACH(stp, &lfp->lf_deleg, ls_file) {
2872 if (stp->ls_clp == clp)
2875 if (stp == LIST_END(&lfp->lf_deleg) && openstp == NULL) {
2877 * This is the Claim_Previous case with a delegation
2878 * type != Delegate_None.
2881 * First, add the delegation. (Although we must issue the
2882 * delegation, we can also ask for an immediate return.)
2884 new_deleg->ls_stateid.seqid = delegstateidp->seqid = 1;
2885 new_deleg->ls_stateid.other[0] = delegstateidp->other[0] =
2886 clp->lc_clientid.lval[0];
2887 new_deleg->ls_stateid.other[1] = delegstateidp->other[1] =
2888 clp->lc_clientid.lval[1];
2889 new_deleg->ls_stateid.other[2] = delegstateidp->other[2] =
2890 nfsrv_nextstateindex(clp);
2891 if (new_stp->ls_flags & NFSLCK_DELEGWRITE) {
2892 new_deleg->ls_flags = (NFSLCK_DELEGWRITE |
2893 NFSLCK_READACCESS | NFSLCK_WRITEACCESS);
2894 *rflagsp |= NFSV4OPEN_WRITEDELEGATE;
2896 new_deleg->ls_flags = (NFSLCK_DELEGREAD |
2898 *rflagsp |= NFSV4OPEN_READDELEGATE;
2900 new_deleg->ls_uid = new_stp->ls_uid;
2901 new_deleg->ls_lfp = lfp;
2902 new_deleg->ls_clp = clp;
2903 new_deleg->ls_filerev = filerev;
2904 new_deleg->ls_compref = nd->nd_compref;
2905 LIST_INSERT_HEAD(&lfp->lf_deleg, new_deleg, ls_file);
2906 LIST_INSERT_HEAD(NFSSTATEHASH(clp,
2907 new_deleg->ls_stateid), new_deleg, ls_hash);
2908 LIST_INSERT_HEAD(&clp->lc_deleg, new_deleg, ls_list);
2910 if (delegate == 2 || nfsrv_issuedelegs == 0 ||
2911 (clp->lc_flags & (LCL_CALLBACKSON | LCL_CBDOWN)) !=
2913 NFSRV_V4DELEGLIMIT(nfsrv_delegatecnt) ||
2914 !NFSVNO_DELEGOK(vp))
2915 *rflagsp |= NFSV4OPEN_RECALL;
2916 newnfsstats.srvdelegates++;
2917 nfsrv_openpluslock++;
2918 nfsrv_delegatecnt++;
2921 * Now, do the associated open.
2923 new_open->ls_stateid.seqid = 1;
2924 new_open->ls_stateid.other[0] = clp->lc_clientid.lval[0];
2925 new_open->ls_stateid.other[1] = clp->lc_clientid.lval[1];
2926 new_open->ls_stateid.other[2] = nfsrv_nextstateindex(clp);
2927 new_open->ls_flags = (new_stp->ls_flags & NFSLCK_DENYBITS) |
2929 if (new_stp->ls_flags & NFSLCK_DELEGWRITE)
2930 new_open->ls_flags |= (NFSLCK_READACCESS |
2931 NFSLCK_WRITEACCESS);
2933 new_open->ls_flags |= NFSLCK_READACCESS;
2934 new_open->ls_uid = new_stp->ls_uid;
2935 new_open->ls_lfp = lfp;
2936 new_open->ls_clp = clp;
2937 LIST_INIT(&new_open->ls_open);
2938 LIST_INSERT_HEAD(&lfp->lf_open, new_open, ls_file);
2939 LIST_INSERT_HEAD(NFSSTATEHASH(clp, new_open->ls_stateid),
2942 * and handle the open owner
2945 new_open->ls_openowner = ownerstp;
2946 LIST_INSERT_HEAD(&ownerstp->ls_open, new_open, ls_list);
2948 new_open->ls_openowner = new_stp;
2949 new_stp->ls_flags = 0;
2950 nfsrvd_refcache(new_stp->ls_op);
2951 new_stp->ls_noopens = 0;
2952 LIST_INIT(&new_stp->ls_open);
2953 LIST_INSERT_HEAD(&new_stp->ls_open, new_open, ls_list);
2954 LIST_INSERT_HEAD(&clp->lc_open, new_stp, ls_list);
2956 newnfsstats.srvopenowners++;
2957 nfsrv_openpluslock++;
2961 newnfsstats.srvopens++;
2962 nfsrv_openpluslock++;
2964 error = NFSERR_RECLAIMCONFLICT;
2966 } else if (ownerstp) {
2967 if (ownerstp->ls_flags & NFSLCK_NEEDSCONFIRM) {
2968 /* Replace the open */
2969 if (ownerstp->ls_op)
2970 nfsrvd_derefcache(ownerstp->ls_op);
2971 ownerstp->ls_op = new_stp->ls_op;
2972 nfsrvd_refcache(ownerstp->ls_op);
2973 ownerstp->ls_seq = new_stp->ls_seq;
2974 *rflagsp |= NFSV4OPEN_RESULTCONFIRM;
2975 stp = LIST_FIRST(&ownerstp->ls_open);
2976 stp->ls_flags = (new_stp->ls_flags & NFSLCK_SHAREBITS) |
2978 stp->ls_stateid.seqid = 1;
2979 stp->ls_uid = new_stp->ls_uid;
2980 if (lfp != stp->ls_lfp) {
2981 LIST_REMOVE(stp, ls_file);
2982 LIST_INSERT_HEAD(&lfp->lf_open, stp, ls_file);
2986 } else if (openstp) {
2987 openstp->ls_flags |= (new_stp->ls_flags & NFSLCK_SHAREBITS);
2988 openstp->ls_stateid.seqid++;
2989 if ((nd->nd_flag & ND_NFSV41) != 0 &&
2990 openstp->ls_stateid.seqid == 0)
2991 openstp->ls_stateid.seqid = 1;
2994 * This is where we can choose to issue a delegation.
2996 if (delegate == 0 || writedeleg == 0 ||
2997 NFSVNO_EXRDONLY(exp) || (readonly != 0 &&
2998 nfsrv_writedelegifpos == 0) ||
2999 !NFSVNO_DELEGOK(vp) ||
3000 (new_stp->ls_flags & NFSLCK_WANTRDELEG) != 0 ||
3001 (clp->lc_flags & (LCL_CALLBACKSON | LCL_CBDOWN)) !=
3003 *rflagsp |= NFSV4OPEN_WDCONTENTION;
3004 else if (nfsrv_issuedelegs == 0 ||
3005 NFSRV_V4DELEGLIMIT(nfsrv_delegatecnt))
3006 *rflagsp |= NFSV4OPEN_WDRESOURCE;
3007 else if ((new_stp->ls_flags & NFSLCK_WANTNODELEG) != 0)
3008 *rflagsp |= NFSV4OPEN_WDNOTWANTED;
3010 new_deleg->ls_stateid.seqid = delegstateidp->seqid = 1;
3011 new_deleg->ls_stateid.other[0] = delegstateidp->other[0]
3012 = clp->lc_clientid.lval[0];
3013 new_deleg->ls_stateid.other[1] = delegstateidp->other[1]
3014 = clp->lc_clientid.lval[1];
3015 new_deleg->ls_stateid.other[2] = delegstateidp->other[2]
3016 = nfsrv_nextstateindex(clp);
3017 new_deleg->ls_flags = (NFSLCK_DELEGWRITE |
3018 NFSLCK_READACCESS | NFSLCK_WRITEACCESS);
3019 *rflagsp |= NFSV4OPEN_WRITEDELEGATE;
3020 new_deleg->ls_uid = new_stp->ls_uid;
3021 new_deleg->ls_lfp = lfp;
3022 new_deleg->ls_clp = clp;
3023 new_deleg->ls_filerev = filerev;
3024 new_deleg->ls_compref = nd->nd_compref;
3025 LIST_INSERT_HEAD(&lfp->lf_deleg, new_deleg, ls_file);
3026 LIST_INSERT_HEAD(NFSSTATEHASH(clp,
3027 new_deleg->ls_stateid), new_deleg, ls_hash);
3028 LIST_INSERT_HEAD(&clp->lc_deleg, new_deleg, ls_list);
3030 newnfsstats.srvdelegates++;
3031 nfsrv_openpluslock++;
3032 nfsrv_delegatecnt++;
3035 new_open->ls_stateid.seqid = 1;
3036 new_open->ls_stateid.other[0] = clp->lc_clientid.lval[0];
3037 new_open->ls_stateid.other[1] = clp->lc_clientid.lval[1];
3038 new_open->ls_stateid.other[2] = nfsrv_nextstateindex(clp);
3039 new_open->ls_flags = (new_stp->ls_flags & NFSLCK_SHAREBITS)|
3041 new_open->ls_uid = new_stp->ls_uid;
3042 new_open->ls_openowner = ownerstp;
3043 new_open->ls_lfp = lfp;
3044 new_open->ls_clp = clp;
3045 LIST_INIT(&new_open->ls_open);
3046 LIST_INSERT_HEAD(&lfp->lf_open, new_open, ls_file);
3047 LIST_INSERT_HEAD(&ownerstp->ls_open, new_open, ls_list);
3048 LIST_INSERT_HEAD(NFSSTATEHASH(clp, new_open->ls_stateid),
3052 newnfsstats.srvopens++;
3053 nfsrv_openpluslock++;
3056 * This is where we can choose to issue a delegation.
3058 if (delegate == 0 || (writedeleg == 0 && readonly == 0) ||
3059 !NFSVNO_DELEGOK(vp) ||
3060 (clp->lc_flags & (LCL_CALLBACKSON | LCL_CBDOWN)) !=
3062 *rflagsp |= NFSV4OPEN_WDCONTENTION;
3063 else if (nfsrv_issuedelegs == 0 ||
3064 NFSRV_V4DELEGLIMIT(nfsrv_delegatecnt))
3065 *rflagsp |= NFSV4OPEN_WDRESOURCE;
3066 else if ((new_stp->ls_flags & NFSLCK_WANTNODELEG) != 0)
3067 *rflagsp |= NFSV4OPEN_WDNOTWANTED;
3069 new_deleg->ls_stateid.seqid = delegstateidp->seqid = 1;
3070 new_deleg->ls_stateid.other[0] = delegstateidp->other[0]
3071 = clp->lc_clientid.lval[0];
3072 new_deleg->ls_stateid.other[1] = delegstateidp->other[1]
3073 = clp->lc_clientid.lval[1];
3074 new_deleg->ls_stateid.other[2] = delegstateidp->other[2]
3075 = nfsrv_nextstateindex(clp);
3076 if (writedeleg && !NFSVNO_EXRDONLY(exp) &&
3077 (nfsrv_writedelegifpos || !readonly) &&
3078 (new_stp->ls_flags & NFSLCK_WANTRDELEG) == 0) {
3079 new_deleg->ls_flags = (NFSLCK_DELEGWRITE |
3080 NFSLCK_READACCESS | NFSLCK_WRITEACCESS);
3081 *rflagsp |= NFSV4OPEN_WRITEDELEGATE;
3083 new_deleg->ls_flags = (NFSLCK_DELEGREAD |
3085 *rflagsp |= NFSV4OPEN_READDELEGATE;
3087 new_deleg->ls_uid = new_stp->ls_uid;
3088 new_deleg->ls_lfp = lfp;
3089 new_deleg->ls_clp = clp;
3090 new_deleg->ls_filerev = filerev;
3091 new_deleg->ls_compref = nd->nd_compref;
3092 LIST_INSERT_HEAD(&lfp->lf_deleg, new_deleg, ls_file);
3093 LIST_INSERT_HEAD(NFSSTATEHASH(clp,
3094 new_deleg->ls_stateid), new_deleg, ls_hash);
3095 LIST_INSERT_HEAD(&clp->lc_deleg, new_deleg, ls_list);
3097 newnfsstats.srvdelegates++;
3098 nfsrv_openpluslock++;
3099 nfsrv_delegatecnt++;
3104 * New owner case. Start the open_owner sequence with a
3105 * Needs confirmation (unless a reclaim) and hang the
3108 new_open->ls_stateid.seqid = 1;
3109 new_open->ls_stateid.other[0] = clp->lc_clientid.lval[0];
3110 new_open->ls_stateid.other[1] = clp->lc_clientid.lval[1];
3111 new_open->ls_stateid.other[2] = nfsrv_nextstateindex(clp);
3112 new_open->ls_flags = (new_stp->ls_flags & NFSLCK_SHAREBITS) |
3114 new_open->ls_uid = new_stp->ls_uid;
3115 LIST_INIT(&new_open->ls_open);
3116 new_open->ls_openowner = new_stp;
3117 new_open->ls_lfp = lfp;
3118 new_open->ls_clp = clp;
3119 LIST_INSERT_HEAD(&lfp->lf_open, new_open, ls_file);
3120 if (new_stp->ls_flags & NFSLCK_RECLAIM) {
3121 new_stp->ls_flags = 0;
3122 } else if ((nd->nd_flag & ND_NFSV41) != 0) {
3123 /* NFSv4.1 never needs confirmation. */
3124 new_stp->ls_flags = 0;
3127 * This is where we can choose to issue a delegation.
3129 if (delegate && nfsrv_issuedelegs &&
3130 (writedeleg || readonly) &&
3131 (clp->lc_flags & (LCL_CALLBACKSON | LCL_CBDOWN)) ==
3133 !NFSRV_V4DELEGLIMIT(nfsrv_delegatecnt) &&
3134 NFSVNO_DELEGOK(vp) &&
3135 ((nd->nd_flag & ND_NFSV41) == 0 ||
3136 (new_stp->ls_flags & NFSLCK_WANTNODELEG) == 0)) {
3137 new_deleg->ls_stateid.seqid =
3138 delegstateidp->seqid = 1;
3139 new_deleg->ls_stateid.other[0] =
3140 delegstateidp->other[0]
3141 = clp->lc_clientid.lval[0];
3142 new_deleg->ls_stateid.other[1] =
3143 delegstateidp->other[1]
3144 = clp->lc_clientid.lval[1];
3145 new_deleg->ls_stateid.other[2] =
3146 delegstateidp->other[2]
3147 = nfsrv_nextstateindex(clp);
3148 if (writedeleg && !NFSVNO_EXRDONLY(exp) &&
3149 (nfsrv_writedelegifpos || !readonly) &&
3150 ((nd->nd_flag & ND_NFSV41) == 0 ||
3151 (new_stp->ls_flags & NFSLCK_WANTRDELEG) ==
3153 new_deleg->ls_flags =
3154 (NFSLCK_DELEGWRITE |
3156 NFSLCK_WRITEACCESS);
3157 *rflagsp |= NFSV4OPEN_WRITEDELEGATE;
3159 new_deleg->ls_flags =
3162 *rflagsp |= NFSV4OPEN_READDELEGATE;
3164 new_deleg->ls_uid = new_stp->ls_uid;
3165 new_deleg->ls_lfp = lfp;
3166 new_deleg->ls_clp = clp;
3167 new_deleg->ls_filerev = filerev;
3168 new_deleg->ls_compref = nd->nd_compref;
3169 LIST_INSERT_HEAD(&lfp->lf_deleg, new_deleg,
3171 LIST_INSERT_HEAD(NFSSTATEHASH(clp,
3172 new_deleg->ls_stateid), new_deleg, ls_hash);
3173 LIST_INSERT_HEAD(&clp->lc_deleg, new_deleg,
3176 newnfsstats.srvdelegates++;
3177 nfsrv_openpluslock++;
3178 nfsrv_delegatecnt++;
3181 *rflagsp |= NFSV4OPEN_RESULTCONFIRM;
3182 new_stp->ls_flags = NFSLCK_NEEDSCONFIRM;
3184 nfsrvd_refcache(new_stp->ls_op);
3185 new_stp->ls_noopens = 0;
3186 LIST_INIT(&new_stp->ls_open);
3187 LIST_INSERT_HEAD(&new_stp->ls_open, new_open, ls_list);
3188 LIST_INSERT_HEAD(&clp->lc_open, new_stp, ls_list);
3189 LIST_INSERT_HEAD(NFSSTATEHASH(clp, new_open->ls_stateid),
3194 newnfsstats.srvopens++;
3195 nfsrv_openpluslock++;
3196 newnfsstats.srvopenowners++;
3197 nfsrv_openpluslock++;
3200 stateidp->seqid = openstp->ls_stateid.seqid;
3201 stateidp->other[0] = openstp->ls_stateid.other[0];
3202 stateidp->other[1] = openstp->ls_stateid.other[1];
3203 stateidp->other[2] = openstp->ls_stateid.other[2];
3207 NFSLOCKV4ROOTMUTEX();
3208 nfsv4_unlock(&nfsv4rootfs_lock, 1);
3209 NFSUNLOCKV4ROOTMUTEX();
3212 FREE((caddr_t)new_open, M_NFSDSTATE);
3214 FREE((caddr_t)new_deleg, M_NFSDSTATE);
3217 NFSEXITCODE2(error, nd);
3222 * Open update. Does the confirm, downgrade and close.
3225 nfsrv_openupdate(vnode_t vp, struct nfsstate *new_stp, nfsquad_t clientid,
3226 nfsv4stateid_t *stateidp, struct nfsrv_descript *nd, NFSPROC_T *p)
3228 struct nfsstate *stp, *ownerstp;
3229 struct nfsclient *clp;
3230 struct nfslockfile *lfp;
3232 int error = 0, gotstate = 0, len = 0;
3233 u_char client[NFSV4_OPAQUELIMIT];
3236 * Check for restart conditions (client and server).
3238 error = nfsrv_checkrestart(clientid, new_stp->ls_flags,
3239 &new_stp->ls_stateid, 0);
3245 * Get the open structure via clientid and stateid.
3247 error = nfsrv_getclient(clientid, CLOPS_RENEW, &clp, NULL,
3248 (nfsquad_t)((u_quad_t)0), 0, nd, p);
3250 error = nfsrv_getstate(clp, &new_stp->ls_stateid,
3251 new_stp->ls_flags, &stp);
3254 * Sanity check the open.
3256 if (!error && (!(stp->ls_flags & NFSLCK_OPEN) ||
3257 (!(new_stp->ls_flags & NFSLCK_CONFIRM) &&
3258 (stp->ls_openowner->ls_flags & NFSLCK_NEEDSCONFIRM)) ||
3259 ((new_stp->ls_flags & NFSLCK_CONFIRM) &&
3260 (!(stp->ls_openowner->ls_flags & NFSLCK_NEEDSCONFIRM)))))
3261 error = NFSERR_BADSTATEID;
3264 error = nfsrv_checkseqid(nd, new_stp->ls_seq,
3265 stp->ls_openowner, new_stp->ls_op);
3266 if (!error && stp->ls_stateid.seqid != new_stp->ls_stateid.seqid &&
3267 (((nd->nd_flag & ND_NFSV41) == 0 &&
3268 !(new_stp->ls_flags & NFSLCK_CONFIRM)) ||
3269 ((nd->nd_flag & ND_NFSV41) != 0 &&
3270 new_stp->ls_stateid.seqid != 0)))
3271 error = NFSERR_OLDSTATEID;
3272 if (!error && vnode_vtype(vp) != VREG) {
3273 if (vnode_vtype(vp) == VDIR)
3274 error = NFSERR_ISDIR;
3276 error = NFSERR_INVAL;
3281 * If a client tries to confirm an Open with a bad
3282 * seqid# and there are no byte range locks or other Opens
3283 * on the openowner, just throw it away, so the next use of the
3284 * openowner will start a fresh seq#.
3286 if (error == NFSERR_BADSEQID &&
3287 (new_stp->ls_flags & NFSLCK_CONFIRM) &&
3288 nfsrv_nootherstate(stp))
3289 nfsrv_freeopenowner(stp->ls_openowner, 0, p);
3295 * Set the return stateid.
3297 stateidp->seqid = stp->ls_stateid.seqid + 1;
3298 if ((nd->nd_flag & ND_NFSV41) != 0 && stateidp->seqid == 0)
3299 stateidp->seqid = 1;
3300 stateidp->other[0] = stp->ls_stateid.other[0];
3301 stateidp->other[1] = stp->ls_stateid.other[1];
3302 stateidp->other[2] = stp->ls_stateid.other[2];
3304 * Now, handle the three cases.
3306 if (new_stp->ls_flags & NFSLCK_CONFIRM) {
3308 * If the open doesn't need confirmation, it seems to me that
3309 * there is a client error, but I'll just log it and keep going?
3311 if (!(stp->ls_openowner->ls_flags & NFSLCK_NEEDSCONFIRM))
3312 printf("Nfsv4d: stray open confirm\n");
3313 stp->ls_openowner->ls_flags = 0;
3314 stp->ls_stateid.seqid++;
3315 if ((nd->nd_flag & ND_NFSV41) != 0 &&
3316 stp->ls_stateid.seqid == 0)
3317 stp->ls_stateid.seqid = 1;
3318 if (!(clp->lc_flags & LCL_STAMPEDSTABLE)) {
3319 clp->lc_flags |= LCL_STAMPEDSTABLE;
3320 len = clp->lc_idlen;
3321 NFSBCOPY(clp->lc_id, client, len);
3325 } else if (new_stp->ls_flags & NFSLCK_CLOSE) {
3326 ownerstp = stp->ls_openowner;
3328 if (nfsrv_dolocallocks != 0 && !LIST_EMPTY(&stp->ls_open)) {
3329 /* Get the lf lock */
3332 ASSERT_VOP_ELOCKED(vp, "nfsrv_openupdate");
3333 NFSVOPUNLOCK(vp, 0);
3334 if (nfsrv_freeopen(stp, vp, 1, p) == 0) {
3336 nfsrv_unlocklf(lfp);
3339 NFSVOPLOCK(vp, LK_EXCLUSIVE | LK_RETRY);
3341 (void) nfsrv_freeopen(stp, NULL, 0, p);
3346 * Update the share bits, making sure that the new set are a
3347 * subset of the old ones.
3349 bits = (new_stp->ls_flags & NFSLCK_SHAREBITS);
3350 if (~(stp->ls_flags) & bits) {
3352 error = NFSERR_INVAL;
3355 stp->ls_flags = (bits | NFSLCK_OPEN);
3356 stp->ls_stateid.seqid++;
3357 if ((nd->nd_flag & ND_NFSV41) != 0 &&
3358 stp->ls_stateid.seqid == 0)
3359 stp->ls_stateid.seqid = 1;
3364 * If the client just confirmed its first open, write a timestamp
3365 * to the stable storage file.
3367 if (gotstate != 0) {
3368 nfsrv_writestable(client, len, NFSNST_NEWSTATE, p);
3369 nfsrv_backupstable();
3373 NFSEXITCODE2(error, nd);
3378 * Delegation update. Does the purge and return.
3381 nfsrv_delegupdate(struct nfsrv_descript *nd, nfsquad_t clientid,
3382 nfsv4stateid_t *stateidp, vnode_t vp, int op, struct ucred *cred,
3385 struct nfsstate *stp;
3386 struct nfsclient *clp;
3391 * Do a sanity check against the file handle for DelegReturn.
3394 error = nfsvno_getfh(vp, &fh, p);
3399 * Check for restart conditions (client and server).
3401 if (op == NFSV4OP_DELEGRETURN)
3402 error = nfsrv_checkrestart(clientid, NFSLCK_DELEGRETURN,
3405 error = nfsrv_checkrestart(clientid, NFSLCK_DELEGPURGE,
3410 * Get the open structure via clientid and stateid.
3413 error = nfsrv_getclient(clientid, CLOPS_RENEW, &clp, NULL,
3414 (nfsquad_t)((u_quad_t)0), 0, nd, p);
3416 if (error == NFSERR_CBPATHDOWN)
3418 if (error == NFSERR_STALECLIENTID && op == NFSV4OP_DELEGRETURN)
3419 error = NFSERR_STALESTATEID;
3421 if (!error && op == NFSV4OP_DELEGRETURN) {
3422 error = nfsrv_getstate(clp, stateidp, NFSLCK_DELEGRETURN, &stp);
3423 if (!error && stp->ls_stateid.seqid != stateidp->seqid &&
3424 ((nd->nd_flag & ND_NFSV41) == 0 || stateidp->seqid != 0))
3425 error = NFSERR_OLDSTATEID;
3428 * NFSERR_EXPIRED means that the state has gone away,
3429 * so Delegations have been purged. Just return ok.
3431 if (error == NFSERR_EXPIRED && op == NFSV4OP_DELEGPURGE) {
3441 if (op == NFSV4OP_DELEGRETURN) {
3442 if (NFSBCMP((caddr_t)&fh, (caddr_t)&stp->ls_lfp->lf_fh,
3443 sizeof (fhandle_t))) {
3445 error = NFSERR_BADSTATEID;
3448 nfsrv_freedeleg(stp);
3450 nfsrv_freedeleglist(&clp->lc_olddeleg);
3461 * Release lock owner.
3464 nfsrv_releaselckown(struct nfsstate *new_stp, nfsquad_t clientid,
3467 struct nfsstate *stp, *nstp, *openstp, *ownstp;
3468 struct nfsclient *clp;
3472 * Check for restart conditions (client and server).
3474 error = nfsrv_checkrestart(clientid, new_stp->ls_flags,
3475 &new_stp->ls_stateid, 0);
3481 * Get the lock owner by name.
3483 error = nfsrv_getclient(clientid, CLOPS_RENEW, &clp, NULL,
3484 (nfsquad_t)((u_quad_t)0), 0, NULL, p);
3489 LIST_FOREACH(ownstp, &clp->lc_open, ls_list) {
3490 LIST_FOREACH(openstp, &ownstp->ls_open, ls_list) {
3491 stp = LIST_FIRST(&openstp->ls_open);
3492 while (stp != LIST_END(&openstp->ls_open)) {
3493 nstp = LIST_NEXT(stp, ls_list);
3495 * If the owner matches, check for locks and
3496 * then free or return an error.
3498 if (stp->ls_ownerlen == new_stp->ls_ownerlen &&
3499 !NFSBCMP(stp->ls_owner, new_stp->ls_owner,
3501 if (LIST_EMPTY(&stp->ls_lock)) {
3502 nfsrv_freelockowner(stp, NULL, 0, p);
3505 error = NFSERR_LOCKSHELD;
3521 * Get the file handle for a lock structure.
3524 nfsrv_getlockfh(vnode_t vp, u_short flags, struct nfslockfile *new_lfp,
3525 fhandle_t *nfhp, NFSPROC_T *p)
3527 fhandle_t *fhp = NULL;
3531 * For lock, use the new nfslock structure, otherwise just
3532 * a fhandle_t on the stack.
3534 if (flags & NFSLCK_OPEN) {
3535 KASSERT(new_lfp != NULL, ("nfsrv_getlockfh: new_lfp NULL"));
3536 fhp = &new_lfp->lf_fh;
3540 panic("nfsrv_getlockfh");
3542 error = nfsvno_getfh(vp, fhp, p);
3548 * Get an nfs lock structure. Allocate one, as required, and return a
3550 * Returns an NFSERR_xxx upon failure or -1 to indicate no current lock.
3553 nfsrv_getlockfile(u_short flags, struct nfslockfile **new_lfpp,
3554 struct nfslockfile **lfpp, fhandle_t *nfhp, int lockit)
3556 struct nfslockfile *lfp;
3557 fhandle_t *fhp = NULL, *tfhp;
3558 struct nfslockhashhead *hp;
3559 struct nfslockfile *new_lfp = NULL;
3562 * For lock, use the new nfslock structure, otherwise just
3563 * a fhandle_t on the stack.
3565 if (flags & NFSLCK_OPEN) {
3566 new_lfp = *new_lfpp;
3567 fhp = &new_lfp->lf_fh;
3571 panic("nfsrv_getlockfile");
3574 hp = NFSLOCKHASH(fhp);
3575 LIST_FOREACH(lfp, hp, lf_hash) {
3577 if (NFSVNO_CMPFH(fhp, tfhp)) {
3584 if (!(flags & NFSLCK_OPEN))
3588 * No match, so chain the new one into the list.
3590 LIST_INIT(&new_lfp->lf_open);
3591 LIST_INIT(&new_lfp->lf_lock);
3592 LIST_INIT(&new_lfp->lf_deleg);
3593 LIST_INIT(&new_lfp->lf_locallock);
3594 LIST_INIT(&new_lfp->lf_rollback);
3595 new_lfp->lf_locallock_lck.nfslock_usecnt = 0;
3596 new_lfp->lf_locallock_lck.nfslock_lock = 0;
3597 new_lfp->lf_usecount = 0;
3598 LIST_INSERT_HEAD(hp, new_lfp, lf_hash);
3605 * This function adds a nfslock lock structure to the list for the associated
3606 * nfsstate and nfslockfile structures. It will be inserted after the
3607 * entry pointed at by insert_lop.
3610 nfsrv_insertlock(struct nfslock *new_lop, struct nfslock *insert_lop,
3611 struct nfsstate *stp, struct nfslockfile *lfp)
3613 struct nfslock *lop, *nlop;
3615 new_lop->lo_stp = stp;
3616 new_lop->lo_lfp = lfp;
3619 /* Insert in increasing lo_first order */
3620 lop = LIST_FIRST(&lfp->lf_lock);
3621 if (lop == LIST_END(&lfp->lf_lock) ||
3622 new_lop->lo_first <= lop->lo_first) {
3623 LIST_INSERT_HEAD(&lfp->lf_lock, new_lop, lo_lckfile);
3625 nlop = LIST_NEXT(lop, lo_lckfile);
3626 while (nlop != LIST_END(&lfp->lf_lock) &&
3627 nlop->lo_first < new_lop->lo_first) {
3629 nlop = LIST_NEXT(lop, lo_lckfile);
3631 LIST_INSERT_AFTER(lop, new_lop, lo_lckfile);
3634 new_lop->lo_lckfile.le_prev = NULL; /* list not used */
3638 * Insert after insert_lop, which is overloaded as stp or lfp for
3641 if (stp == NULL && (struct nfslockfile *)insert_lop == lfp)
3642 LIST_INSERT_HEAD(&lfp->lf_locallock, new_lop, lo_lckowner);
3643 else if ((struct nfsstate *)insert_lop == stp)
3644 LIST_INSERT_HEAD(&stp->ls_lock, new_lop, lo_lckowner);
3646 LIST_INSERT_AFTER(insert_lop, new_lop, lo_lckowner);
3648 newnfsstats.srvlocks++;
3649 nfsrv_openpluslock++;
3654 * This function updates the locking for a lock owner and given file. It
3655 * maintains a list of lock ranges ordered on increasing file offset that
3656 * are NFSLCK_READ or NFSLCK_WRITE and non-overlapping (aka POSIX style).
3657 * It always adds new_lop to the list and sometimes uses the one pointed
3661 nfsrv_updatelock(struct nfsstate *stp, struct nfslock **new_lopp,
3662 struct nfslock **other_lopp, struct nfslockfile *lfp)
3664 struct nfslock *new_lop = *new_lopp;
3665 struct nfslock *lop, *tlop, *ilop;
3666 struct nfslock *other_lop = *other_lopp;
3667 int unlock = 0, myfile = 0;
3671 * Work down the list until the lock is merged.
3673 if (new_lop->lo_flags & NFSLCK_UNLOCK)
3676 ilop = (struct nfslock *)stp;
3677 lop = LIST_FIRST(&stp->ls_lock);
3679 ilop = (struct nfslock *)lfp;
3680 lop = LIST_FIRST(&lfp->lf_locallock);
3682 while (lop != NULL) {
3684 * Only check locks for this file that aren't before the start of
3687 if (lop->lo_lfp == lfp) {
3689 if (lop->lo_end >= new_lop->lo_first) {
3690 if (new_lop->lo_end < lop->lo_first) {
3692 * If the new lock ends before the start of the
3693 * current lock's range, no merge, just insert
3698 if (new_lop->lo_flags == lop->lo_flags ||
3699 (new_lop->lo_first <= lop->lo_first &&
3700 new_lop->lo_end >= lop->lo_end)) {
3702 * This lock can be absorbed by the new lock/unlock.
3703 * This happens when it covers the entire range
3704 * of the old lock or is contiguous
3705 * with the old lock and is of the same type or an
3708 if (lop->lo_first < new_lop->lo_first)
3709 new_lop->lo_first = lop->lo_first;
3710 if (lop->lo_end > new_lop->lo_end)
3711 new_lop->lo_end = lop->lo_end;
3713 lop = LIST_NEXT(lop, lo_lckowner);
3714 nfsrv_freenfslock(tlop);
3719 * All these cases are for contiguous locks that are not the
3720 * same type, so they can't be merged.
3722 if (new_lop->lo_first <= lop->lo_first) {
3724 * This case is where the new lock overlaps with the
3725 * first part of the old lock. Move the start of the
3726 * old lock to just past the end of the new lock. The
3727 * new lock will be inserted in front of the old, since
3728 * ilop hasn't been updated. (We are done now.)
3730 lop->lo_first = new_lop->lo_end;
3733 if (new_lop->lo_end >= lop->lo_end) {
3735 * This case is where the new lock overlaps with the
3736 * end of the old lock's range. Move the old lock's
3737 * end to just before the new lock's first and insert
3738 * the new lock after the old lock.
3739 * Might not be done yet, since the new lock could
3740 * overlap further locks with higher ranges.
3742 lop->lo_end = new_lop->lo_first;
3744 lop = LIST_NEXT(lop, lo_lckowner);
3748 * The final case is where the new lock's range is in the
3749 * middle of the current lock's and splits the current lock
3750 * up. Use *other_lopp to handle the second part of the
3751 * split old lock range. (We are done now.)
3752 * For unlock, we use new_lop as other_lop and tmp, since
3753 * other_lop and new_lop are the same for this case.
3754 * We noted the unlock case above, so we don't need
3755 * new_lop->lo_flags any longer.
3757 tmp = new_lop->lo_first;
3758 if (other_lop == NULL) {
3760 panic("nfsd srv update unlock");
3761 other_lop = new_lop;
3764 other_lop->lo_first = new_lop->lo_end;
3765 other_lop->lo_end = lop->lo_end;
3766 other_lop->lo_flags = lop->lo_flags;
3767 other_lop->lo_stp = stp;
3768 other_lop->lo_lfp = lfp;
3770 nfsrv_insertlock(other_lop, lop, stp, lfp);
3777 lop = LIST_NEXT(lop, lo_lckowner);
3778 if (myfile && (lop == NULL || lop->lo_lfp != lfp))
3783 * Insert the new lock in the list at the appropriate place.
3786 nfsrv_insertlock(new_lop, ilop, stp, lfp);
3792 * This function handles sequencing of locks, etc.
3793 * It returns an error that indicates what the caller should do.
3796 nfsrv_checkseqid(struct nfsrv_descript *nd, u_int32_t seqid,
3797 struct nfsstate *stp, struct nfsrvcache *op)
3801 if ((nd->nd_flag & ND_NFSV41) != 0)
3802 /* NFSv4.1 ignores the open_seqid and lock_seqid. */
3804 if (op != nd->nd_rp)
3805 panic("nfsrvstate checkseqid");
3806 if (!(op->rc_flag & RC_INPROG))
3807 panic("nfsrvstate not inprog");
3808 if (stp->ls_op && stp->ls_op->rc_refcnt <= 0) {
3809 printf("refcnt=%d\n", stp->ls_op->rc_refcnt);
3810 panic("nfsrvstate op refcnt");
3812 if ((stp->ls_seq + 1) == seqid) {
3814 nfsrvd_derefcache(stp->ls_op);
3816 nfsrvd_refcache(op);
3817 stp->ls_seq = seqid;
3819 } else if (stp->ls_seq == seqid && stp->ls_op &&
3820 op->rc_xid == stp->ls_op->rc_xid &&
3821 op->rc_refcnt == 0 &&
3822 op->rc_reqlen == stp->ls_op->rc_reqlen &&
3823 op->rc_cksum == stp->ls_op->rc_cksum) {
3824 if (stp->ls_op->rc_flag & RC_INPROG) {
3825 error = NFSERR_DONTREPLY;
3828 nd->nd_rp = stp->ls_op;
3829 nd->nd_rp->rc_flag |= RC_INPROG;
3830 nfsrvd_delcache(op);
3831 error = NFSERR_REPLYFROMCACHE;
3834 error = NFSERR_BADSEQID;
3837 NFSEXITCODE2(error, nd);
3842 * Get the client ip address for callbacks. If the strings can't be parsed,
3843 * just set lc_program to 0 to indicate no callbacks are possible.
3844 * (For cases where the address can't be parsed or is 0.0.0.0.0.0, set
3845 * the address to the client's transport address. This won't be used
3846 * for callbacks, but can be printed out by newnfsstats for info.)
3847 * Return error if the xdr can't be parsed, 0 otherwise.
3850 nfsrv_getclientipaddr(struct nfsrv_descript *nd, struct nfsclient *clp)
3855 struct sockaddr_in *rad, *sad;
3856 u_char protocol[5], addr[24];
3857 int error = 0, cantparse = 0;
3867 rad = NFSSOCKADDR(clp->lc_req.nr_nam, struct sockaddr_in *);
3868 rad->sin_family = AF_INET;
3869 rad->sin_len = sizeof (struct sockaddr_in);
3870 rad->sin_addr.s_addr = 0;
3872 clp->lc_req.nr_client = NULL;
3873 clp->lc_req.nr_lock = 0;
3874 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
3875 i = fxdr_unsigned(int, *tl);
3876 if (i >= 3 && i <= 4) {
3877 error = nfsrv_mtostr(nd, protocol, i);
3880 if (!strcmp(protocol, "tcp")) {
3881 clp->lc_flags |= LCL_TCPCALLBACK;
3882 clp->lc_req.nr_sotype = SOCK_STREAM;
3883 clp->lc_req.nr_soproto = IPPROTO_TCP;
3884 } else if (!strcmp(protocol, "udp")) {
3885 clp->lc_req.nr_sotype = SOCK_DGRAM;
3886 clp->lc_req.nr_soproto = IPPROTO_UDP;
3893 error = nfsm_advance(nd, NFSM_RNDUP(i), -1);
3898 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
3899 i = fxdr_unsigned(int, *tl);
3901 error = NFSERR_BADXDR;
3903 } else if (i == 0) {
3905 } else if (!cantparse && i <= 23 && i >= 11) {
3906 error = nfsrv_mtostr(nd, addr, i);
3911 * Parse out the address fields. We expect 6 decimal numbers
3912 * separated by '.'s.
3916 while (*cp && i < 6) {
3918 while (*cp2 && *cp2 != '.')
3926 j = nfsrv_getipnumber(cp);
3931 port.cval[5 - i] = j;
3940 if (ip.ival != 0x0) {
3941 rad->sin_addr.s_addr = htonl(ip.ival);
3942 rad->sin_port = htons(port.sval);
3950 error = nfsm_advance(nd, NFSM_RNDUP(i), -1);
3956 sad = NFSSOCKADDR(nd->nd_nam, struct sockaddr_in *);
3957 rad->sin_addr.s_addr = sad->sin_addr.s_addr;
3958 rad->sin_port = 0x0;
3959 clp->lc_program = 0;
3962 NFSEXITCODE2(error, nd);
3967 * Turn a string of up to three decimal digits into a number. Return -1 upon
3971 nfsrv_getipnumber(u_char *cp)
3976 if (j > 2 || *cp < '0' || *cp > '9')
3989 * This function checks for restart conditions.
3992 nfsrv_checkrestart(nfsquad_t clientid, u_int32_t flags,
3993 nfsv4stateid_t *stateidp, int specialid)
3998 * First check for a server restart. Open, LockT, ReleaseLockOwner
3999 * and DelegPurge have a clientid, the rest a stateid.
4002 (NFSLCK_OPEN | NFSLCK_TEST | NFSLCK_RELEASE | NFSLCK_DELEGPURGE)) {
4003 if (clientid.lval[0] != nfsrvboottime) {
4004 ret = NFSERR_STALECLIENTID;
4007 } else if (stateidp->other[0] != nfsrvboottime &&
4009 ret = NFSERR_STALESTATEID;
4014 * Read, Write, Setattr and LockT can return NFSERR_GRACE and do
4015 * not use a lock/open owner seqid#, so the check can be done now.
4016 * (The others will be checked, as required, later.)
4018 if (!(flags & (NFSLCK_CHECK | NFSLCK_TEST)))
4022 ret = nfsrv_checkgrace(NULL, NULL, flags);
4034 nfsrv_checkgrace(struct nfsrv_descript *nd, struct nfsclient *clp,
4039 if ((nfsrv_stablefirst.nsf_flags & NFSNSF_GRACEOVER) != 0) {
4040 if (flags & NFSLCK_RECLAIM) {
4041 error = NFSERR_NOGRACE;
4045 if (!(flags & NFSLCK_RECLAIM)) {
4046 error = NFSERR_GRACE;
4049 if (nd != NULL && clp != NULL &&
4050 (nd->nd_flag & ND_NFSV41) != 0 &&
4051 (clp->lc_flags & LCL_RECLAIMCOMPLETE) != 0) {
4052 error = NFSERR_NOGRACE;
4057 * If grace is almost over and we are still getting Reclaims,
4058 * extend grace a bit.
4060 if ((NFSD_MONOSEC + NFSRV_LEASEDELTA) >
4061 nfsrv_stablefirst.nsf_eograce)
4062 nfsrv_stablefirst.nsf_eograce = NFSD_MONOSEC +
4072 * Do a server callback.
4075 nfsrv_docallback(struct nfsclient *clp, int procnum,
4076 nfsv4stateid_t *stateidp, int trunc, fhandle_t *fhp,
4077 struct nfsvattr *nap, nfsattrbit_t *attrbitp, NFSPROC_T *p)
4081 struct nfsrv_descript nfsd, *nd = &nfsd;
4085 struct nfsdsession *sep = NULL;
4087 cred = newnfs_getcred();
4088 NFSLOCKSTATE(); /* mostly for lc_cbref++ */
4089 if (clp->lc_flags & LCL_NEEDSCONFIRM) {
4096 * Fill the callback program# and version into the request
4097 * structure for newnfs_connect() to use.
4099 clp->lc_req.nr_prog = clp->lc_program;
4101 if ((clp->lc_flags & LCL_NFSV41) != 0)
4102 clp->lc_req.nr_vers = NFSV41_CBVERS;
4105 clp->lc_req.nr_vers = NFSV4_CBVERS;
4108 * First, fill in some of the fields of nd and cr.
4110 nd->nd_flag = ND_NFSV4;
4111 if (clp->lc_flags & LCL_GSS)
4112 nd->nd_flag |= ND_KERBV;
4113 if ((clp->lc_flags & LCL_NFSV41) != 0)
4114 nd->nd_flag |= ND_NFSV41;
4116 cred->cr_uid = clp->lc_uid;
4117 cred->cr_gid = clp->lc_gid;
4118 callback = clp->lc_callback;
4120 cred->cr_ngroups = 1;
4123 * Get the first mbuf for the request.
4125 MGET(m, M_WAITOK, MT_DATA);
4127 nd->nd_mreq = nd->nd_mb = m;
4128 nd->nd_bpos = NFSMTOD(m, caddr_t);
4131 * and build the callback request.
4133 if (procnum == NFSV4OP_CBGETATTR) {
4134 nd->nd_procnum = NFSV4PROC_CBCOMPOUND;
4135 error = nfsrv_cbcallargs(nd, clp, callback, NFSV4OP_CBGETATTR,
4136 "CB Getattr", &sep);
4138 mbuf_freem(nd->nd_mreq);
4141 (void)nfsm_fhtom(nd, (u_int8_t *)fhp, NFSX_MYFH, 0);
4142 (void)nfsrv_putattrbit(nd, attrbitp);
4143 } else if (procnum == NFSV4OP_CBRECALL) {
4144 nd->nd_procnum = NFSV4PROC_CBCOMPOUND;
4145 error = nfsrv_cbcallargs(nd, clp, callback, NFSV4OP_CBRECALL,
4148 mbuf_freem(nd->nd_mreq);
4151 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED + NFSX_STATEID);
4152 *tl++ = txdr_unsigned(stateidp->seqid);
4153 NFSBCOPY((caddr_t)stateidp->other, (caddr_t)tl,
4155 tl += (NFSX_STATEIDOTHER / NFSX_UNSIGNED);
4160 (void)nfsm_fhtom(nd, (u_int8_t *)fhp, NFSX_MYFH, 0);
4161 } else if (procnum == NFSV4PROC_CBNULL) {
4162 nd->nd_procnum = NFSV4PROC_CBNULL;
4163 if ((clp->lc_flags & LCL_NFSV41) != 0) {
4164 error = nfsv4_getcbsession(clp, &sep);
4166 mbuf_freem(nd->nd_mreq);
4171 error = NFSERR_SERVERFAULT;
4172 mbuf_freem(nd->nd_mreq);
4177 * Call newnfs_connect(), as required, and then newnfs_request().
4179 (void) newnfs_sndlock(&clp->lc_req.nr_lock);
4180 if (clp->lc_req.nr_client == NULL) {
4181 if ((clp->lc_flags & LCL_NFSV41) != 0)
4182 error = ECONNREFUSED;
4183 else if (nd->nd_procnum == NFSV4PROC_CBNULL)
4184 error = newnfs_connect(NULL, &clp->lc_req, cred,
4187 error = newnfs_connect(NULL, &clp->lc_req, cred,
4190 newnfs_sndunlock(&clp->lc_req.nr_lock);
4192 if ((nd->nd_flag & ND_NFSV41) != 0) {
4193 KASSERT(sep != NULL, ("sep NULL"));
4194 error = newnfs_request(nd, NULL, clp, &clp->lc_req,
4195 NULL, NULL, cred, clp->lc_program,
4196 clp->lc_req.nr_vers, NULL, 1, NULL,
4198 nfsrv_freesession(sep, NULL);
4200 error = newnfs_request(nd, NULL, clp, &clp->lc_req,
4201 NULL, NULL, cred, clp->lc_program,
4202 clp->lc_req.nr_vers, NULL, 1, NULL, NULL);
4208 * If error is set here, the Callback path isn't working
4209 * properly, so twiddle the appropriate LCL_ flags.
4210 * (nd_repstat != 0 indicates the Callback path is working,
4211 * but the callback failed on the client.)
4215 * Mark the callback pathway down, which disabled issuing
4216 * of delegations and gets Renew to return NFSERR_CBPATHDOWN.
4219 clp->lc_flags |= LCL_CBDOWN;
4223 * Callback worked. If the callback path was down, disable
4224 * callbacks, so no more delegations will be issued. (This
4225 * is done on the assumption that the callback pathway is
4229 if (clp->lc_flags & LCL_CBDOWN)
4230 clp->lc_flags &= ~(LCL_CBDOWN | LCL_CALLBACKSON);
4233 error = nd->nd_repstat;
4234 else if (error == 0 && procnum == NFSV4OP_CBGETATTR)
4235 error = nfsv4_loadattr(nd, NULL, nap, NULL, NULL, 0,
4236 NULL, NULL, NULL, NULL, NULL, 0, NULL, NULL, NULL,
4238 mbuf_freem(nd->nd_mrep);
4242 if ((clp->lc_flags & LCL_WAKEUPWANTED) && clp->lc_cbref == 0) {
4243 clp->lc_flags &= ~LCL_WAKEUPWANTED;
4253 * Set up the compound RPC for the callback.
4256 nfsrv_cbcallargs(struct nfsrv_descript *nd, struct nfsclient *clp,
4257 uint32_t callback, int op, const char *optag, struct nfsdsession **sepp)
4262 len = strlen(optag);
4263 (void)nfsm_strtom(nd, optag, len);
4264 NFSM_BUILD(tl, uint32_t *, 4 * NFSX_UNSIGNED);
4265 if ((nd->nd_flag & ND_NFSV41) != 0) {
4266 *tl++ = txdr_unsigned(NFSV41_MINORVERSION);
4267 *tl++ = txdr_unsigned(callback);
4268 *tl++ = txdr_unsigned(2);
4269 *tl = txdr_unsigned(NFSV4OP_CBSEQUENCE);
4270 error = nfsv4_setcbsequence(nd, clp, 1, sepp);
4273 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
4274 *tl = txdr_unsigned(op);
4276 *tl++ = txdr_unsigned(NFSV4_MINORVERSION);
4277 *tl++ = txdr_unsigned(callback);
4278 *tl++ = txdr_unsigned(1);
4279 *tl = txdr_unsigned(op);
4285 * Return the next index# for a clientid. Mostly just increment and return
4286 * the next one, but... if the 32bit unsigned does actually wrap around,
4287 * it should be rebooted.
4288 * At an average rate of one new client per second, it will wrap around in
4289 * approximately 136 years. (I think the server will have been shut
4290 * down or rebooted before then.)
4293 nfsrv_nextclientindex(void)
4295 static u_int32_t client_index = 0;
4298 if (client_index != 0)
4299 return (client_index);
4301 printf("%s: out of clientids\n", __func__);
4302 return (client_index);
4306 * Return the next index# for a stateid. Mostly just increment and return
4307 * the next one, but... if the 32bit unsigned does actually wrap around
4308 * (will a BSD server stay up that long?), find
4309 * new start and end values.
4312 nfsrv_nextstateindex(struct nfsclient *clp)
4314 struct nfsstate *stp;
4316 u_int32_t canuse, min_index, max_index;
4318 if (!(clp->lc_flags & LCL_INDEXNOTOK)) {
4319 clp->lc_stateindex++;
4320 if (clp->lc_stateindex != clp->lc_statemaxindex)
4321 return (clp->lc_stateindex);
4325 * Yuck, we've hit the end.
4326 * Look for a new min and max.
4329 max_index = 0xffffffff;
4330 for (i = 0; i < nfsrv_statehashsize; i++) {
4331 LIST_FOREACH(stp, &clp->lc_stateid[i], ls_hash) {
4332 if (stp->ls_stateid.other[2] > 0x80000000) {
4333 if (stp->ls_stateid.other[2] < max_index)
4334 max_index = stp->ls_stateid.other[2];
4336 if (stp->ls_stateid.other[2] > min_index)
4337 min_index = stp->ls_stateid.other[2];
4343 * Yikes, highly unlikely, but I'll handle it anyhow.
4345 if (min_index == 0x80000000 && max_index == 0x80000001) {
4348 * Loop around until we find an unused entry. Return that
4349 * and set LCL_INDEXNOTOK, so the search will continue next time.
4350 * (This is one of those rare cases where a goto is the
4351 * cleanest way to code the loop.)
4354 for (i = 0; i < nfsrv_statehashsize; i++) {
4355 LIST_FOREACH(stp, &clp->lc_stateid[i], ls_hash) {
4356 if (stp->ls_stateid.other[2] == canuse) {
4362 clp->lc_flags |= LCL_INDEXNOTOK;
4367 * Ok to start again from min + 1.
4369 clp->lc_stateindex = min_index + 1;
4370 clp->lc_statemaxindex = max_index;
4371 clp->lc_flags &= ~LCL_INDEXNOTOK;
4372 return (clp->lc_stateindex);
4376 * The following functions handle the stable storage file that deals with
4377 * the edge conditions described in RFC3530 Sec. 8.6.3.
4378 * The file is as follows:
4379 * - a single record at the beginning that has the lease time of the
4380 * previous server instance (before the last reboot) and the nfsrvboottime
4381 * values for the previous server boots.
4382 * These previous boot times are used to ensure that the current
4383 * nfsrvboottime does not, somehow, get set to a previous one.
4384 * (This is important so that Stale ClientIDs and StateIDs can
4386 * The number of previous nfsvrboottime values preceeds the list.
4387 * - followed by some number of appended records with:
4388 * - client id string
4389 * - flag that indicates it is a record revoking state via lease
4390 * expiration or similar
4391 * OR has successfully acquired state.
4392 * These structures vary in length, with the client string at the end, up
4393 * to NFSV4_OPAQUELIMIT in size.
4395 * At the end of the grace period, the file is truncated, the first
4396 * record is rewritten with updated information and any acquired state
4397 * records for successful reclaims of state are written.
4399 * Subsequent records are appended when the first state is issued to
4400 * a client and when state is revoked for a client.
4402 * When reading the file in, state issued records that come later in
4403 * the file override older ones, since the append log is in cronological order.
4404 * If, for some reason, the file can't be read, the grace period is
4405 * immediately terminated and all reclaims get NFSERR_NOGRACE.
4409 * Read in the stable storage file. Called by nfssvc() before the nfsd
4410 * processes start servicing requests.
4413 nfsrv_setupstable(NFSPROC_T *p)
4415 struct nfsrv_stablefirst *sf = &nfsrv_stablefirst;
4416 struct nfsrv_stable *sp, *nsp;
4417 struct nfst_rec *tsp;
4418 int error, i, tryagain;
4420 ssize_t aresid, len;
4423 * If NFSNSF_UPDATEDONE is set, this is a restart of the nfsds without
4424 * a reboot, so state has not been lost.
4426 if (sf->nsf_flags & NFSNSF_UPDATEDONE)
4429 * Set Grace over just until the file reads successfully.
4431 nfsrvboottime = time_second;
4432 LIST_INIT(&sf->nsf_head);
4433 sf->nsf_flags = (NFSNSF_GRACEOVER | NFSNSF_NEEDLOCK);
4434 sf->nsf_eograce = NFSD_MONOSEC + NFSRV_LEASEDELTA;
4435 if (sf->nsf_fp == NULL)
4437 error = NFSD_RDWR(UIO_READ, NFSFPVNODE(sf->nsf_fp),
4438 (caddr_t)&sf->nsf_rec, sizeof (struct nfsf_rec), off, UIO_SYSSPACE,
4439 0, NFSFPCRED(sf->nsf_fp), &aresid, p);
4440 if (error || aresid || sf->nsf_numboots == 0 ||
4441 sf->nsf_numboots > NFSNSF_MAXNUMBOOTS)
4445 * Now, read in the boottimes.
4447 sf->nsf_bootvals = (time_t *)malloc((sf->nsf_numboots + 1) *
4448 sizeof (time_t), M_TEMP, M_WAITOK);
4449 off = sizeof (struct nfsf_rec);
4450 error = NFSD_RDWR(UIO_READ, NFSFPVNODE(sf->nsf_fp),
4451 (caddr_t)sf->nsf_bootvals, sf->nsf_numboots * sizeof (time_t), off,
4452 UIO_SYSSPACE, 0, NFSFPCRED(sf->nsf_fp), &aresid, p);
4453 if (error || aresid) {
4454 free((caddr_t)sf->nsf_bootvals, M_TEMP);
4455 sf->nsf_bootvals = NULL;
4460 * Make sure this nfsrvboottime is different from all recorded
4465 for (i = 0; i < sf->nsf_numboots; i++) {
4466 if (nfsrvboottime == sf->nsf_bootvals[i]) {
4474 sf->nsf_flags |= NFSNSF_OK;
4475 off += (sf->nsf_numboots * sizeof (time_t));
4478 * Read through the file, building a list of records for grace
4480 * Each record is between sizeof (struct nfst_rec) and
4481 * sizeof (struct nfst_rec) + NFSV4_OPAQUELIMIT - 1
4482 * and is actually sizeof (struct nfst_rec) + nst_len - 1.
4484 tsp = (struct nfst_rec *)malloc(sizeof (struct nfst_rec) +
4485 NFSV4_OPAQUELIMIT - 1, M_TEMP, M_WAITOK);
4487 error = NFSD_RDWR(UIO_READ, NFSFPVNODE(sf->nsf_fp),
4488 (caddr_t)tsp, sizeof (struct nfst_rec) + NFSV4_OPAQUELIMIT - 1,
4489 off, UIO_SYSSPACE, 0, NFSFPCRED(sf->nsf_fp), &aresid, p);
4490 len = (sizeof (struct nfst_rec) + NFSV4_OPAQUELIMIT - 1) - aresid;
4491 if (error || (len > 0 && (len < sizeof (struct nfst_rec) ||
4492 len < (sizeof (struct nfst_rec) + tsp->len - 1)))) {
4494 * Yuck, the file has been corrupted, so just return
4495 * after clearing out any restart state, so the grace period
4498 LIST_FOREACH_SAFE(sp, &sf->nsf_head, nst_list, nsp) {
4499 LIST_REMOVE(sp, nst_list);
4500 free((caddr_t)sp, M_TEMP);
4502 free((caddr_t)tsp, M_TEMP);
4503 sf->nsf_flags &= ~NFSNSF_OK;
4504 free((caddr_t)sf->nsf_bootvals, M_TEMP);
4505 sf->nsf_bootvals = NULL;
4509 off += sizeof (struct nfst_rec) + tsp->len - 1;
4511 * Search the list for a matching client.
4513 LIST_FOREACH(sp, &sf->nsf_head, nst_list) {
4514 if (tsp->len == sp->nst_len &&
4515 !NFSBCMP(tsp->client, sp->nst_client, tsp->len))
4518 if (sp == LIST_END(&sf->nsf_head)) {
4519 sp = (struct nfsrv_stable *)malloc(tsp->len +
4520 sizeof (struct nfsrv_stable) - 1, M_TEMP,
4522 NFSBCOPY((caddr_t)tsp, (caddr_t)&sp->nst_rec,
4523 sizeof (struct nfst_rec) + tsp->len - 1);
4524 LIST_INSERT_HEAD(&sf->nsf_head, sp, nst_list);
4526 if (tsp->flag == NFSNST_REVOKE)
4527 sp->nst_flag |= NFSNST_REVOKE;
4530 * A subsequent timestamp indicates the client
4531 * did a setclientid/confirm and any previous
4532 * revoke is no longer relevant.
4534 sp->nst_flag &= ~NFSNST_REVOKE;
4538 free((caddr_t)tsp, M_TEMP);
4539 sf->nsf_flags = NFSNSF_OK;
4540 sf->nsf_eograce = NFSD_MONOSEC + sf->nsf_lease +
4545 * Update the stable storage file, now that the grace period is over.
4548 nfsrv_updatestable(NFSPROC_T *p)
4550 struct nfsrv_stablefirst *sf = &nfsrv_stablefirst;
4551 struct nfsrv_stable *sp, *nsp;
4553 struct nfsvattr nva;
4555 #if defined(__FreeBSD_version) && (__FreeBSD_version >= 500000)
4560 if (sf->nsf_fp == NULL || (sf->nsf_flags & NFSNSF_UPDATEDONE))
4562 sf->nsf_flags |= NFSNSF_UPDATEDONE;
4564 * Ok, we need to rewrite the stable storage file.
4565 * - truncate to 0 length
4566 * - write the new first structure
4567 * - loop through the data structures, writing out any that
4568 * have timestamps older than the old boot
4570 if (sf->nsf_bootvals) {
4572 for (i = sf->nsf_numboots - 2; i >= 0; i--)
4573 sf->nsf_bootvals[i + 1] = sf->nsf_bootvals[i];
4575 sf->nsf_numboots = 1;
4576 sf->nsf_bootvals = (time_t *)malloc(sizeof (time_t),
4579 sf->nsf_bootvals[0] = nfsrvboottime;
4580 sf->nsf_lease = nfsrv_lease;
4581 NFSVNO_ATTRINIT(&nva);
4582 NFSVNO_SETATTRVAL(&nva, size, 0);
4583 vp = NFSFPVNODE(sf->nsf_fp);
4584 vn_start_write(vp, &mp, V_WAIT);
4585 if (NFSVOPLOCK(vp, LK_EXCLUSIVE) == 0) {
4586 error = nfsvno_setattr(vp, &nva, NFSFPCRED(sf->nsf_fp), p,
4588 NFSVOPUNLOCK(vp, 0);
4591 vn_finished_write(mp);
4593 error = NFSD_RDWR(UIO_WRITE, vp,
4594 (caddr_t)&sf->nsf_rec, sizeof (struct nfsf_rec), (off_t)0,
4595 UIO_SYSSPACE, IO_SYNC, NFSFPCRED(sf->nsf_fp), NULL, p);
4597 error = NFSD_RDWR(UIO_WRITE, vp,
4598 (caddr_t)sf->nsf_bootvals,
4599 sf->nsf_numboots * sizeof (time_t),
4600 (off_t)(sizeof (struct nfsf_rec)),
4601 UIO_SYSSPACE, IO_SYNC, NFSFPCRED(sf->nsf_fp), NULL, p);
4602 free((caddr_t)sf->nsf_bootvals, M_TEMP);
4603 sf->nsf_bootvals = NULL;
4605 sf->nsf_flags &= ~NFSNSF_OK;
4606 printf("EEK! Can't write NfsV4 stable storage file\n");
4609 sf->nsf_flags |= NFSNSF_OK;
4612 * Loop through the list and write out timestamp records for
4613 * any clients that successfully reclaimed state.
4615 LIST_FOREACH_SAFE(sp, &sf->nsf_head, nst_list, nsp) {
4616 if (sp->nst_flag & NFSNST_GOTSTATE) {
4617 nfsrv_writestable(sp->nst_client, sp->nst_len,
4618 NFSNST_NEWSTATE, p);
4619 sp->nst_clp->lc_flags |= LCL_STAMPEDSTABLE;
4621 LIST_REMOVE(sp, nst_list);
4622 free((caddr_t)sp, M_TEMP);
4624 nfsrv_backupstable();
4628 * Append a record to the stable storage file.
4631 nfsrv_writestable(u_char *client, int len, int flag, NFSPROC_T *p)
4633 struct nfsrv_stablefirst *sf = &nfsrv_stablefirst;
4634 struct nfst_rec *sp;
4637 if (!(sf->nsf_flags & NFSNSF_OK) || sf->nsf_fp == NULL)
4639 sp = (struct nfst_rec *)malloc(sizeof (struct nfst_rec) +
4640 len - 1, M_TEMP, M_WAITOK);
4642 NFSBCOPY(client, sp->client, len);
4644 error = NFSD_RDWR(UIO_WRITE, NFSFPVNODE(sf->nsf_fp),
4645 (caddr_t)sp, sizeof (struct nfst_rec) + len - 1, (off_t)0,
4646 UIO_SYSSPACE, (IO_SYNC | IO_APPEND), NFSFPCRED(sf->nsf_fp), NULL, p);
4647 free((caddr_t)sp, M_TEMP);
4649 sf->nsf_flags &= ~NFSNSF_OK;
4650 printf("EEK! Can't write NfsV4 stable storage file\n");
4655 * This function is called during the grace period to mark a client
4656 * that successfully reclaimed state.
4659 nfsrv_markstable(struct nfsclient *clp)
4661 struct nfsrv_stable *sp;
4664 * First find the client structure.
4666 LIST_FOREACH(sp, &nfsrv_stablefirst.nsf_head, nst_list) {
4667 if (sp->nst_len == clp->lc_idlen &&
4668 !NFSBCMP(sp->nst_client, clp->lc_id, sp->nst_len))
4671 if (sp == LIST_END(&nfsrv_stablefirst.nsf_head))
4675 * Now, just mark it and set the nfsclient back pointer.
4677 sp->nst_flag |= NFSNST_GOTSTATE;
4682 * This function is called for a reclaim, to see if it gets grace.
4683 * It returns 0 if a reclaim is allowed, 1 otherwise.
4686 nfsrv_checkstable(struct nfsclient *clp)
4688 struct nfsrv_stable *sp;
4691 * First, find the entry for the client.
4693 LIST_FOREACH(sp, &nfsrv_stablefirst.nsf_head, nst_list) {
4694 if (sp->nst_len == clp->lc_idlen &&
4695 !NFSBCMP(sp->nst_client, clp->lc_id, sp->nst_len))
4700 * If not in the list, state was revoked or no state was issued
4701 * since the previous reboot, a reclaim is denied.
4703 if (sp == LIST_END(&nfsrv_stablefirst.nsf_head) ||
4704 (sp->nst_flag & NFSNST_REVOKE) ||
4705 !(nfsrv_stablefirst.nsf_flags & NFSNSF_OK))
4711 * Test for and try to clear out a conflicting client. This is called by
4712 * nfsrv_lockctrl() and nfsrv_openctrl() when conflicts with other clients
4714 * The trick here is that it can't revoke a conflicting client with an
4715 * expired lease unless it holds the v4root lock, so...
4716 * If no v4root lock, get the lock and return 1 to indicate "try again".
4717 * Return 0 to indicate the conflict can't be revoked and 1 to indicate
4718 * the revocation worked and the conflicting client is "bye, bye", so it
4719 * can be tried again.
4720 * Return 2 to indicate that the vnode is VI_DOOMED after NFSVOPLOCK().
4721 * Unlocks State before a non-zero value is returned.
4724 nfsrv_clientconflict(struct nfsclient *clp, int *haslockp, vnode_t vp,
4727 int gotlock, lktype = 0;
4730 * If lease hasn't expired, we can't fix it.
4732 if (clp->lc_expiry >= NFSD_MONOSEC ||
4733 !(nfsrv_stablefirst.nsf_flags & NFSNSF_UPDATEDONE))
4735 if (*haslockp == 0) {
4738 lktype = NFSVOPISLOCKED(vp);
4739 NFSVOPUNLOCK(vp, 0);
4741 NFSLOCKV4ROOTMUTEX();
4742 nfsv4_relref(&nfsv4rootfs_lock);
4744 gotlock = nfsv4_lock(&nfsv4rootfs_lock, 1, NULL,
4745 NFSV4ROOTLOCKMUTEXPTR, NULL);
4747 NFSUNLOCKV4ROOTMUTEX();
4750 NFSVOPLOCK(vp, lktype | LK_RETRY);
4751 if ((vp->v_iflag & VI_DOOMED) != 0)
4759 * Ok, we can expire the conflicting client.
4761 nfsrv_writestable(clp->lc_id, clp->lc_idlen, NFSNST_REVOKE, p);
4762 nfsrv_backupstable();
4763 nfsrv_cleanclient(clp, p);
4764 nfsrv_freedeleglist(&clp->lc_deleg);
4765 nfsrv_freedeleglist(&clp->lc_olddeleg);
4766 LIST_REMOVE(clp, lc_hash);
4767 nfsrv_zapclient(clp, p);
4772 * Resolve a delegation conflict.
4773 * Returns 0 to indicate the conflict was resolved without sleeping.
4774 * Return -1 to indicate that the caller should check for conflicts again.
4775 * Return > 0 for an error that should be returned, normally NFSERR_DELAY.
4777 * Also, manipulate the nfsv4root_lock, as required. It isn't changed
4778 * for a return of 0, since there was no sleep and it could be required
4779 * later. It is released for a return of NFSERR_DELAY, since the caller
4780 * will return that error. It is released when a sleep was done waiting
4781 * for the delegation to be returned or expire (so that other nfsds can
4782 * handle ops). Then, it must be acquired for the write to stable storage.
4783 * (This function is somewhat similar to nfsrv_clientconflict(), but
4784 * the semantics differ in a couple of subtle ways. The return of 0
4785 * indicates the conflict was resolved without sleeping here, not
4786 * that the conflict can't be resolved and the handling of nfsv4root_lock
4787 * differs, as noted above.)
4788 * Unlocks State before returning a non-zero value.
4791 nfsrv_delegconflict(struct nfsstate *stp, int *haslockp, NFSPROC_T *p,
4794 struct nfsclient *clp = stp->ls_clp;
4795 int gotlock, error, lktype = 0, retrycnt, zapped_clp;
4796 nfsv4stateid_t tstateid;
4800 * If the conflict is with an old delegation...
4802 if (stp->ls_flags & NFSLCK_OLDDELEG) {
4804 * You can delete it, if it has expired.
4806 if (clp->lc_delegtime < NFSD_MONOSEC) {
4807 nfsrv_freedeleg(stp);
4814 * During this delay, the old delegation could expire or it
4815 * could be recovered by the client via an Open with
4816 * CLAIM_DELEGATE_PREV.
4817 * Release the nfsv4root_lock, if held.
4821 NFSLOCKV4ROOTMUTEX();
4822 nfsv4_unlock(&nfsv4rootfs_lock, 1);
4823 NFSUNLOCKV4ROOTMUTEX();
4825 error = NFSERR_DELAY;
4830 * It's a current delegation, so:
4831 * - check to see if the delegation has expired
4832 * - if so, get the v4root lock and then expire it
4834 if (!(stp->ls_flags & NFSLCK_DELEGRECALL)) {
4836 * - do a recall callback, since not yet done
4837 * For now, never allow truncate to be set. To use
4838 * truncate safely, it must be guaranteed that the
4839 * Remove, Rename or Setattr with size of 0 will
4840 * succeed and that would require major changes to
4841 * the VFS/Vnode OPs.
4842 * Set the expiry time large enough so that it won't expire
4843 * until after the callback, then set it correctly, once
4844 * the callback is done. (The delegation will now time
4845 * out whether or not the Recall worked ok. The timeout
4846 * will be extended when ops are done on the delegation
4847 * stateid, up to the timelimit.)
4849 stp->ls_delegtime = NFSD_MONOSEC + (2 * nfsrv_lease) +
4851 stp->ls_delegtimelimit = NFSD_MONOSEC + (6 * nfsrv_lease) +
4853 stp->ls_flags |= NFSLCK_DELEGRECALL;
4856 * Loop NFSRV_CBRETRYCNT times while the CBRecall replies
4857 * NFSERR_BADSTATEID or NFSERR_BADHANDLE. This is done
4858 * in order to try and avoid a race that could happen
4859 * when a CBRecall request passed the Open reply with
4860 * the delegation in it when transitting the network.
4861 * Since nfsrv_docallback will sleep, don't use stp after
4864 NFSBCOPY((caddr_t)&stp->ls_stateid, (caddr_t)&tstateid,
4866 NFSBCOPY((caddr_t)&stp->ls_lfp->lf_fh, (caddr_t)&tfh,
4871 NFSLOCKV4ROOTMUTEX();
4872 nfsv4_unlock(&nfsv4rootfs_lock, 1);
4873 NFSUNLOCKV4ROOTMUTEX();
4877 error = nfsrv_docallback(clp, NFSV4OP_CBRECALL,
4878 &tstateid, 0, &tfh, NULL, NULL, p);
4880 } while ((error == NFSERR_BADSTATEID ||
4881 error == NFSERR_BADHANDLE) && retrycnt < NFSV4_CBRETRYCNT);
4882 error = NFSERR_DELAY;
4886 if (clp->lc_expiry >= NFSD_MONOSEC &&
4887 stp->ls_delegtime >= NFSD_MONOSEC) {
4890 * A recall has been done, but it has not yet expired.
4895 NFSLOCKV4ROOTMUTEX();
4896 nfsv4_unlock(&nfsv4rootfs_lock, 1);
4897 NFSUNLOCKV4ROOTMUTEX();
4899 error = NFSERR_DELAY;
4904 * If we don't yet have the lock, just get it and then return,
4905 * since we need that before deleting expired state, such as
4907 * When getting the lock, unlock the vnode, so other nfsds that
4908 * are in progress, won't get stuck waiting for the vnode lock.
4910 if (*haslockp == 0) {
4913 lktype = NFSVOPISLOCKED(vp);
4914 NFSVOPUNLOCK(vp, 0);
4916 NFSLOCKV4ROOTMUTEX();
4917 nfsv4_relref(&nfsv4rootfs_lock);
4919 gotlock = nfsv4_lock(&nfsv4rootfs_lock, 1, NULL,
4920 NFSV4ROOTLOCKMUTEXPTR, NULL);
4922 NFSUNLOCKV4ROOTMUTEX();
4925 NFSVOPLOCK(vp, lktype | LK_RETRY);
4926 if ((vp->v_iflag & VI_DOOMED) != 0) {
4928 NFSLOCKV4ROOTMUTEX();
4929 nfsv4_unlock(&nfsv4rootfs_lock, 1);
4930 NFSUNLOCKV4ROOTMUTEX();
4931 error = NFSERR_PERM;
4941 * Ok, we can delete the expired delegation.
4942 * First, write the Revoke record to stable storage and then
4943 * clear out the conflict.
4944 * Since all other nfsd threads are now blocked, we can safely
4945 * sleep without the state changing.
4947 nfsrv_writestable(clp->lc_id, clp->lc_idlen, NFSNST_REVOKE, p);
4948 nfsrv_backupstable();
4949 if (clp->lc_expiry < NFSD_MONOSEC) {
4950 nfsrv_cleanclient(clp, p);
4951 nfsrv_freedeleglist(&clp->lc_deleg);
4952 nfsrv_freedeleglist(&clp->lc_olddeleg);
4953 LIST_REMOVE(clp, lc_hash);
4956 nfsrv_freedeleg(stp);
4960 nfsrv_zapclient(clp, p);
4969 * Check for a remove allowed, if remove is set to 1 and get rid of
4973 nfsrv_checkremove(vnode_t vp, int remove, NFSPROC_T *p)
4975 struct nfsstate *stp;
4976 struct nfslockfile *lfp;
4977 int error, haslock = 0;
4981 * First, get the lock file structure.
4982 * (A return of -1 means no associated state, so remove ok.)
4984 error = nfsrv_getlockfh(vp, NFSLCK_CHECK, NULL, &nfh, p);
4988 error = nfsrv_getlockfile(NFSLCK_CHECK, NULL, &lfp, &nfh, 0);
4992 NFSLOCKV4ROOTMUTEX();
4993 nfsv4_unlock(&nfsv4rootfs_lock, 1);
4994 NFSUNLOCKV4ROOTMUTEX();
5002 * Now, we must Recall any delegations.
5004 error = nfsrv_cleandeleg(vp, lfp, NULL, &haslock, p);
5007 * nfsrv_cleandeleg() unlocks state for non-zero
5013 NFSLOCKV4ROOTMUTEX();
5014 nfsv4_unlock(&nfsv4rootfs_lock, 1);
5015 NFSUNLOCKV4ROOTMUTEX();
5021 * Now, look for a conflicting open share.
5025 * If the entry in the directory was the last reference to the
5026 * corresponding filesystem object, the object can be destroyed
5028 if(lfp->lf_usecount>1)
5029 LIST_FOREACH(stp, &lfp->lf_open, ls_file) {
5030 if (stp->ls_flags & NFSLCK_WRITEDENY) {
5031 error = NFSERR_FILEOPEN;
5039 NFSLOCKV4ROOTMUTEX();
5040 nfsv4_unlock(&nfsv4rootfs_lock, 1);
5041 NFSUNLOCKV4ROOTMUTEX();
5050 * Clear out all delegations for the file referred to by lfp.
5051 * May return NFSERR_DELAY, if there will be a delay waiting for
5052 * delegations to expire.
5053 * Returns -1 to indicate it slept while recalling a delegation.
5054 * This function has the side effect of deleting the nfslockfile structure,
5055 * if it no longer has associated state and didn't have to sleep.
5056 * Unlocks State before a non-zero value is returned.
5059 nfsrv_cleandeleg(vnode_t vp, struct nfslockfile *lfp,
5060 struct nfsclient *clp, int *haslockp, NFSPROC_T *p)
5062 struct nfsstate *stp, *nstp;
5065 stp = LIST_FIRST(&lfp->lf_deleg);
5066 while (stp != LIST_END(&lfp->lf_deleg)) {
5067 nstp = LIST_NEXT(stp, ls_file);
5068 if (stp->ls_clp != clp) {
5069 ret = nfsrv_delegconflict(stp, haslockp, p, vp);
5072 * nfsrv_delegconflict() unlocks state
5073 * when it returns non-zero.
5086 * There are certain operations that, when being done outside of NFSv4,
5087 * require that any NFSv4 delegation for the file be recalled.
5088 * This function is to be called for those cases:
5089 * VOP_RENAME() - When a delegation is being recalled for any reason,
5090 * the client may have to do Opens against the server, using the file's
5091 * final component name. If the file has been renamed on the server,
5092 * that component name will be incorrect and the Open will fail.
5093 * VOP_REMOVE() - Theoretically, a client could Open a file after it has
5094 * been removed on the server, if there is a delegation issued to
5095 * that client for the file. I say "theoretically" since clients
5096 * normally do an Access Op before the Open and that Access Op will
5097 * fail with ESTALE. Note that NFSv2 and 3 don't even do Opens, so
5098 * they will detect the file's removal in the same manner. (There is
5099 * one case where RFC3530 allows a client to do an Open without first
5100 * doing an Access Op, which is passage of a check against the ACE
5101 * returned with a Write delegation, but current practice is to ignore
5102 * the ACE and always do an Access Op.)
5103 * Since the functions can only be called with an unlocked vnode, this
5104 * can't be done at this time.
5105 * VOP_ADVLOCK() - When a client holds a delegation, it can issue byte range
5106 * locks locally in the client, which are not visible to the server. To
5107 * deal with this, issuing of delegations for a vnode must be disabled
5108 * and all delegations for the vnode recalled. This is done via the
5109 * second function, using the VV_DISABLEDELEG vflag on the vnode.
5112 nfsd_recalldelegation(vnode_t vp, NFSPROC_T *p)
5118 * First, check to see if the server is currently running and it has
5119 * been called for a regular file when issuing delegations.
5121 if (newnfs_numnfsd == 0 || vp->v_type != VREG ||
5122 nfsrv_issuedelegs == 0)
5125 KASSERT((NFSVOPISLOCKED(vp) != LK_EXCLUSIVE), ("vp %p is locked", vp));
5127 * First, get a reference on the nfsv4rootfs_lock so that an
5128 * exclusive lock cannot be acquired by another thread.
5130 NFSLOCKV4ROOTMUTEX();
5131 nfsv4_getref(&nfsv4rootfs_lock, NULL, NFSV4ROOTLOCKMUTEXPTR, NULL);
5132 NFSUNLOCKV4ROOTMUTEX();
5135 * Now, call nfsrv_checkremove() in a loop while it returns
5136 * NFSERR_DELAY. Return upon any other error or when timed out.
5138 starttime = NFSD_MONOSEC;
5140 if (NFSVOPLOCK(vp, LK_EXCLUSIVE) == 0) {
5141 error = nfsrv_checkremove(vp, 0, p);
5142 NFSVOPUNLOCK(vp, 0);
5145 if (error == NFSERR_DELAY) {
5146 if (NFSD_MONOSEC - starttime > NFS_REMOVETIMEO)
5148 /* Sleep for a short period of time */
5149 (void) nfs_catnap(PZERO, 0, "nfsremove");
5151 } while (error == NFSERR_DELAY);
5152 NFSLOCKV4ROOTMUTEX();
5153 nfsv4_relref(&nfsv4rootfs_lock);
5154 NFSUNLOCKV4ROOTMUTEX();
5158 nfsd_disabledelegation(vnode_t vp, NFSPROC_T *p)
5161 #ifdef VV_DISABLEDELEG
5163 * First, flag issuance of delegations disabled.
5165 atomic_set_long(&vp->v_vflag, VV_DISABLEDELEG);
5169 * Then call nfsd_recalldelegation() to get rid of all extant
5172 nfsd_recalldelegation(vp, p);
5176 * Check for conflicting locks, etc. and then get rid of delegations.
5177 * (At one point I thought that I should get rid of delegations for any
5178 * Setattr, since it could potentially disallow the I/O op (read or write)
5179 * allowed by the delegation. However, Setattr Ops that aren't changing
5180 * the size get a stateid of all 0s, so you can't tell if it is a delegation
5181 * for the same client or a different one, so I decided to only get rid
5182 * of delegations for other clients when the size is being changed.)
5183 * In general, a Setattr can disable NFS I/O Ops that are outstanding, such
5184 * as Write backs, even if there is no delegation, so it really isn't any
5188 nfsrv_checksetattr(vnode_t vp, struct nfsrv_descript *nd,
5189 nfsv4stateid_t *stateidp, struct nfsvattr *nvap, nfsattrbit_t *attrbitp,
5190 struct nfsexstuff *exp, NFSPROC_T *p)
5192 struct nfsstate st, *stp = &st;
5193 struct nfslock lo, *lop = &lo;
5197 if (NFSISSET_ATTRBIT(attrbitp, NFSATTRBIT_SIZE)) {
5198 stp->ls_flags = (NFSLCK_CHECK | NFSLCK_WRITEACCESS);
5199 lop->lo_first = nvap->na_size;
5204 if (NFSISSET_ATTRBIT(attrbitp, NFSATTRBIT_OWNER) ||
5205 NFSISSET_ATTRBIT(attrbitp, NFSATTRBIT_OWNERGROUP) ||
5206 NFSISSET_ATTRBIT(attrbitp, NFSATTRBIT_MODE) ||
5207 NFSISSET_ATTRBIT(attrbitp, NFSATTRBIT_ACL))
5208 stp->ls_flags |= NFSLCK_SETATTR;
5209 if (stp->ls_flags == 0)
5211 lop->lo_end = NFS64BITSSET;
5212 lop->lo_flags = NFSLCK_WRITE;
5213 stp->ls_ownerlen = 0;
5215 stp->ls_uid = nd->nd_cred->cr_uid;
5216 stp->ls_stateid.seqid = stateidp->seqid;
5217 clientid.lval[0] = stp->ls_stateid.other[0] = stateidp->other[0];
5218 clientid.lval[1] = stp->ls_stateid.other[1] = stateidp->other[1];
5219 stp->ls_stateid.other[2] = stateidp->other[2];
5220 error = nfsrv_lockctrl(vp, &stp, &lop, NULL, clientid,
5221 stateidp, exp, nd, p);
5224 NFSEXITCODE2(error, nd);
5229 * Check for a write delegation and do a CBGETATTR if there is one, updating
5230 * the attributes, as required.
5231 * Should I return an error if I can't get the attributes? (For now, I'll
5235 nfsrv_checkgetattr(struct nfsrv_descript *nd, vnode_t vp,
5236 struct nfsvattr *nvap, nfsattrbit_t *attrbitp, struct ucred *cred,
5239 struct nfsstate *stp;
5240 struct nfslockfile *lfp;
5241 struct nfsclient *clp;
5242 struct nfsvattr nva;
5245 nfsattrbit_t cbbits;
5246 u_quad_t delegfilerev;
5248 NFSCBGETATTR_ATTRBIT(attrbitp, &cbbits);
5249 if (!NFSNONZERO_ATTRBIT(&cbbits))
5253 * Get the lock file structure.
5254 * (A return of -1 means no associated state, so return ok.)
5256 error = nfsrv_getlockfh(vp, NFSLCK_CHECK, NULL, &nfh, p);
5259 error = nfsrv_getlockfile(NFSLCK_CHECK, NULL, &lfp, &nfh, 0);
5268 * Now, look for a write delegation.
5270 LIST_FOREACH(stp, &lfp->lf_deleg, ls_file) {
5271 if (stp->ls_flags & NFSLCK_DELEGWRITE)
5274 if (stp == LIST_END(&lfp->lf_deleg)) {
5279 delegfilerev = stp->ls_filerev;
5282 * If the Write delegation was issued as a part of this Compound RPC
5283 * or if we have an Implied Clientid (used in a previous Op in this
5284 * compound) and it is the client the delegation was issued to,
5286 * I also assume that it is from the same client iff the network
5287 * host IP address is the same as the callback address. (Not
5288 * exactly correct by the RFC, but avoids a lot of Getattr
5291 if (nd->nd_compref == stp->ls_compref ||
5292 ((nd->nd_flag & ND_IMPLIEDCLID) &&
5293 clp->lc_clientid.qval == nd->nd_clientid.qval) ||
5294 nfsaddr2_match(clp->lc_req.nr_nam, nd->nd_nam)) {
5300 * We are now done with the delegation state structure,
5301 * so the statelock can be released and we can now tsleep().
5305 * Now, we must do the CB Getattr callback, to see if Change or Size
5308 if (clp->lc_expiry >= NFSD_MONOSEC) {
5310 NFSVNO_ATTRINIT(&nva);
5311 nva.na_filerev = NFS64BITSSET;
5312 error = nfsrv_docallback(clp, NFSV4OP_CBGETATTR, NULL,
5313 0, &nfh, &nva, &cbbits, p);
5315 if ((nva.na_filerev != NFS64BITSSET &&
5316 nva.na_filerev > delegfilerev) ||
5317 (NFSVNO_ISSETSIZE(&nva) &&
5318 nva.na_size != nvap->na_size)) {
5319 error = nfsvno_updfilerev(vp, nvap, cred, p);
5320 if (NFSVNO_ISSETSIZE(&nva))
5321 nvap->na_size = nva.na_size;
5324 error = 0; /* Ignore callback errors for now. */
5330 NFSEXITCODE2(error, nd);
5335 * This function looks for openowners that haven't had any opens for
5336 * a while and throws them away. Called by an nfsd when NFSNSF_NOOPENS
5340 nfsrv_throwawayopens(NFSPROC_T *p)
5342 struct nfsclient *clp, *nclp;
5343 struct nfsstate *stp, *nstp;
5347 nfsrv_stablefirst.nsf_flags &= ~NFSNSF_NOOPENS;
5349 * For each client...
5351 for (i = 0; i < nfsrv_clienthashsize; i++) {
5352 LIST_FOREACH_SAFE(clp, &nfsclienthash[i], lc_hash, nclp) {
5353 LIST_FOREACH_SAFE(stp, &clp->lc_open, ls_list, nstp) {
5354 if (LIST_EMPTY(&stp->ls_open) &&
5355 (stp->ls_noopens > NFSNOOPEN ||
5356 (nfsrv_openpluslock * 2) >
5357 nfsrv_v4statelimit))
5358 nfsrv_freeopenowner(stp, 0, p);
5366 * This function checks to see if the credentials are the same.
5367 * Returns 1 for not same, 0 otherwise.
5370 nfsrv_notsamecredname(struct nfsrv_descript *nd, struct nfsclient *clp)
5373 if (nd->nd_flag & ND_GSS) {
5374 if (!(clp->lc_flags & LCL_GSS))
5376 if (clp->lc_flags & LCL_NAME) {
5377 if (nd->nd_princlen != clp->lc_namelen ||
5378 NFSBCMP(nd->nd_principal, clp->lc_name,
5384 if (nd->nd_cred->cr_uid == clp->lc_uid)
5388 } else if (clp->lc_flags & LCL_GSS)
5391 * For AUTH_SYS, allow the same uid or root. (This is underspecified
5392 * in RFC3530, which talks about principals, but doesn't say anything
5393 * about uids for AUTH_SYS.)
5395 if (nd->nd_cred->cr_uid == clp->lc_uid || nd->nd_cred->cr_uid == 0)
5402 * Calculate the lease expiry time.
5405 nfsrv_leaseexpiry(void)
5408 if (nfsrv_stablefirst.nsf_eograce > NFSD_MONOSEC)
5409 return (NFSD_MONOSEC + 2 * (nfsrv_lease + NFSRV_LEASEDELTA));
5410 return (NFSD_MONOSEC + nfsrv_lease + NFSRV_LEASEDELTA);
5414 * Delay the delegation timeout as far as ls_delegtimelimit, as required.
5417 nfsrv_delaydelegtimeout(struct nfsstate *stp)
5420 if ((stp->ls_flags & NFSLCK_DELEGRECALL) == 0)
5423 if ((stp->ls_delegtime + 15) > NFSD_MONOSEC &&
5424 stp->ls_delegtime < stp->ls_delegtimelimit) {
5425 stp->ls_delegtime += nfsrv_lease;
5426 if (stp->ls_delegtime > stp->ls_delegtimelimit)
5427 stp->ls_delegtime = stp->ls_delegtimelimit;
5432 * This function checks to see if there is any other state associated
5433 * with the openowner for this Open.
5434 * It returns 1 if there is no other state, 0 otherwise.
5437 nfsrv_nootherstate(struct nfsstate *stp)
5439 struct nfsstate *tstp;
5441 LIST_FOREACH(tstp, &stp->ls_openowner->ls_open, ls_list) {
5442 if (tstp != stp || !LIST_EMPTY(&tstp->ls_lock))
5449 * Create a list of lock deltas (changes to local byte range locking
5450 * that can be rolled back using the list) and apply the changes via
5451 * nfsvno_advlock(). Optionally, lock the list. It is expected that either
5452 * the rollback or update function will be called after this.
5453 * It returns an error (and rolls back, as required), if any nfsvno_advlock()
5454 * call fails. If it returns an error, it will unlock the list.
5457 nfsrv_locallock(vnode_t vp, struct nfslockfile *lfp, int flags,
5458 uint64_t first, uint64_t end, struct nfslockconflict *cfp, NFSPROC_T *p)
5460 struct nfslock *lop, *nlop;
5463 /* Loop through the list of locks. */
5464 lop = LIST_FIRST(&lfp->lf_locallock);
5465 while (first < end && lop != NULL) {
5466 nlop = LIST_NEXT(lop, lo_lckowner);
5467 if (first >= lop->lo_end) {
5470 } else if (first < lop->lo_first) {
5471 /* new one starts before entry in list */
5472 if (end <= lop->lo_first) {
5473 /* no overlap between old and new */
5474 error = nfsrv_dolocal(vp, lfp, flags,
5475 NFSLCK_UNLOCK, first, end, cfp, p);
5480 /* handle fragment overlapped with new one */
5481 error = nfsrv_dolocal(vp, lfp, flags,
5482 NFSLCK_UNLOCK, first, lop->lo_first, cfp,
5486 first = lop->lo_first;
5489 /* new one overlaps this entry in list */
5490 if (end <= lop->lo_end) {
5491 /* overlaps all of new one */
5492 error = nfsrv_dolocal(vp, lfp, flags,
5493 lop->lo_flags, first, end, cfp, p);
5498 /* handle fragment overlapped with new one */
5499 error = nfsrv_dolocal(vp, lfp, flags,
5500 lop->lo_flags, first, lop->lo_end, cfp, p);
5503 first = lop->lo_end;
5508 if (first < end && error == 0)
5509 /* handle fragment past end of list */
5510 error = nfsrv_dolocal(vp, lfp, flags, NFSLCK_UNLOCK, first,
5518 * Local lock unlock. Unlock all byte ranges that are no longer locked
5519 * by NFSv4. To do this, unlock any subranges of first-->end that
5520 * do not overlap with the byte ranges of any lock in the lfp->lf_lock
5521 * list. This list has all locks for the file held by other
5522 * <clientid, lockowner> tuples. The list is ordered by increasing
5523 * lo_first value, but may have entries that overlap each other, for
5524 * the case of read locks.
5527 nfsrv_localunlock(vnode_t vp, struct nfslockfile *lfp, uint64_t init_first,
5528 uint64_t init_end, NFSPROC_T *p)
5530 struct nfslock *lop;
5531 uint64_t first, end, prevfirst;
5535 while (first < init_end) {
5536 /* Loop through all nfs locks, adjusting first and end */
5538 LIST_FOREACH(lop, &lfp->lf_lock, lo_lckfile) {
5539 KASSERT(prevfirst <= lop->lo_first,
5540 ("nfsv4 locks out of order"));
5541 KASSERT(lop->lo_first < lop->lo_end,
5542 ("nfsv4 bogus lock"));
5543 prevfirst = lop->lo_first;
5544 if (first >= lop->lo_first &&
5545 first < lop->lo_end)
5547 * Overlaps with initial part, so trim
5548 * off that initial part by moving first past
5551 first = lop->lo_end;
5552 else if (end > lop->lo_first &&
5553 lop->lo_first > first) {
5555 * This lock defines the end of the
5556 * segment to unlock, so set end to the
5557 * start of it and break out of the loop.
5559 end = lop->lo_first;
5564 * There is no segment left to do, so
5565 * break out of this loop and then exit
5566 * the outer while() since first will be set
5567 * to end, which must equal init_end here.
5572 /* Unlock this segment */
5573 (void) nfsrv_dolocal(vp, lfp, NFSLCK_UNLOCK,
5574 NFSLCK_READ, first, end, NULL, p);
5575 nfsrv_locallock_commit(lfp, NFSLCK_UNLOCK,
5579 * Now move past this segment and look for any further
5580 * segment in the range, if there is one.
5588 * Do the local lock operation and update the rollback list, as required.
5589 * Perform the rollback and return the error if nfsvno_advlock() fails.
5592 nfsrv_dolocal(vnode_t vp, struct nfslockfile *lfp, int flags, int oldflags,
5593 uint64_t first, uint64_t end, struct nfslockconflict *cfp, NFSPROC_T *p)
5595 struct nfsrollback *rlp;
5596 int error = 0, ltype, oldltype;
5598 if (flags & NFSLCK_WRITE)
5600 else if (flags & NFSLCK_READ)
5604 if (oldflags & NFSLCK_WRITE)
5606 else if (oldflags & NFSLCK_READ)
5610 if (ltype == oldltype || (oldltype == F_WRLCK && ltype == F_RDLCK))
5613 error = nfsvno_advlock(vp, ltype, first, end, p);
5616 cfp->cl_clientid.lval[0] = 0;
5617 cfp->cl_clientid.lval[1] = 0;
5619 cfp->cl_end = NFS64BITSSET;
5620 cfp->cl_flags = NFSLCK_WRITE;
5621 cfp->cl_ownerlen = 5;
5622 NFSBCOPY("LOCAL", cfp->cl_owner, 5);
5624 nfsrv_locallock_rollback(vp, lfp, p);
5625 } else if (ltype != F_UNLCK) {
5626 rlp = malloc(sizeof (struct nfsrollback), M_NFSDROLLBACK,
5628 rlp->rlck_first = first;
5629 rlp->rlck_end = end;
5630 rlp->rlck_type = oldltype;
5631 LIST_INSERT_HEAD(&lfp->lf_rollback, rlp, rlck_list);
5640 * Roll back local lock changes and free up the rollback list.
5643 nfsrv_locallock_rollback(vnode_t vp, struct nfslockfile *lfp, NFSPROC_T *p)
5645 struct nfsrollback *rlp, *nrlp;
5647 LIST_FOREACH_SAFE(rlp, &lfp->lf_rollback, rlck_list, nrlp) {
5648 (void) nfsvno_advlock(vp, rlp->rlck_type, rlp->rlck_first,
5650 free(rlp, M_NFSDROLLBACK);
5652 LIST_INIT(&lfp->lf_rollback);
5656 * Update local lock list and delete rollback list (ie now committed to the
5657 * local locks). Most of the work is done by the internal function.
5660 nfsrv_locallock_commit(struct nfslockfile *lfp, int flags, uint64_t first,
5663 struct nfsrollback *rlp, *nrlp;
5664 struct nfslock *new_lop, *other_lop;
5666 new_lop = malloc(sizeof (struct nfslock), M_NFSDLOCK, M_WAITOK);
5667 if (flags & (NFSLCK_READ | NFSLCK_WRITE))
5668 other_lop = malloc(sizeof (struct nfslock), M_NFSDLOCK,
5672 new_lop->lo_flags = flags;
5673 new_lop->lo_first = first;
5674 new_lop->lo_end = end;
5675 nfsrv_updatelock(NULL, &new_lop, &other_lop, lfp);
5676 if (new_lop != NULL)
5677 free(new_lop, M_NFSDLOCK);
5678 if (other_lop != NULL)
5679 free(other_lop, M_NFSDLOCK);
5681 /* and get rid of the rollback list */
5682 LIST_FOREACH_SAFE(rlp, &lfp->lf_rollback, rlck_list, nrlp)
5683 free(rlp, M_NFSDROLLBACK);
5684 LIST_INIT(&lfp->lf_rollback);
5688 * Lock the struct nfslockfile for local lock updating.
5691 nfsrv_locklf(struct nfslockfile *lfp)
5695 /* lf_usecount ensures *lfp won't be free'd */
5698 gotlock = nfsv4_lock(&lfp->lf_locallock_lck, 1, NULL,
5699 NFSSTATEMUTEXPTR, NULL);
5700 } while (gotlock == 0);
5705 * Unlock the struct nfslockfile after local lock updating.
5708 nfsrv_unlocklf(struct nfslockfile *lfp)
5711 nfsv4_unlock(&lfp->lf_locallock_lck, 0);
5715 * Clear out all state for the NFSv4 server.
5716 * Must be called by a thread that can sleep when no nfsds are running.
5719 nfsrv_throwawayallstate(NFSPROC_T *p)
5721 struct nfsclient *clp, *nclp;
5722 struct nfslockfile *lfp, *nlfp;
5726 * For each client, clean out the state and then free the structure.
5728 for (i = 0; i < nfsrv_clienthashsize; i++) {
5729 LIST_FOREACH_SAFE(clp, &nfsclienthash[i], lc_hash, nclp) {
5730 nfsrv_cleanclient(clp, p);
5731 nfsrv_freedeleglist(&clp->lc_deleg);
5732 nfsrv_freedeleglist(&clp->lc_olddeleg);
5733 free(clp->lc_stateid, M_NFSDCLIENT);
5734 free(clp, M_NFSDCLIENT);
5739 * Also, free up any remaining lock file structures.
5741 for (i = 0; i < nfsrv_lockhashsize; i++) {
5742 LIST_FOREACH_SAFE(lfp, &nfslockhash[i], lf_hash, nlfp) {
5743 printf("nfsd unload: fnd a lock file struct\n");
5744 nfsrv_freenfslockfile(lfp);
5750 * Check the sequence# for the session and slot provided as an argument.
5751 * Also, renew the lease if the session will return NFS_OK.
5754 nfsrv_checksequence(struct nfsrv_descript *nd, uint32_t sequenceid,
5755 uint32_t *highest_slotidp, uint32_t *target_highest_slotidp, int cache_this,
5756 uint32_t *sflagsp, NFSPROC_T *p)
5758 struct nfsdsession *sep;
5759 struct nfssessionhash *shp;
5763 shp = NFSSESSIONHASH(nd->nd_sessionid);
5764 NFSLOCKSESSION(shp);
5765 sep = nfsrv_findsession(nd->nd_sessionid);
5767 NFSUNLOCKSESSION(shp);
5768 return (NFSERR_BADSESSION);
5770 error = nfsv4_seqsession(sequenceid, nd->nd_slotid, *highest_slotidp,
5771 sep->sess_slots, NULL, NFSV4_SLOTS - 1);
5773 NFSUNLOCKSESSION(shp);
5776 if (cache_this != 0)
5777 nd->nd_flag |= ND_SAVEREPLY;
5778 /* Renew the lease. */
5779 sep->sess_clp->lc_expiry = nfsrv_leaseexpiry();
5780 nd->nd_clientid.qval = sep->sess_clp->lc_clientid.qval;
5781 nd->nd_flag |= ND_IMPLIEDCLID;
5784 * If this session handles the backchannel, save the nd_xprt for this
5785 * RPC, since this is the one being used.
5787 if (sep->sess_cbsess.nfsess_xprt != NULL &&
5788 (sep->sess_crflags & NFSV4CRSESS_CONNBACKCHAN) != 0) {
5789 savxprt = sep->sess_cbsess.nfsess_xprt;
5790 SVC_ACQUIRE(nd->nd_xprt);
5791 nd->nd_xprt->xp_p2 = savxprt->xp_p2;
5792 nd->nd_xprt->xp_idletimeout = 0; /* Disable timeout. */
5793 sep->sess_cbsess.nfsess_xprt = nd->nd_xprt;
5794 SVC_RELEASE(savxprt);
5798 if (sep->sess_clp->lc_req.nr_client == NULL)
5799 *sflagsp |= NFSV4SEQ_CBPATHDOWN;
5800 NFSUNLOCKSESSION(shp);
5801 if (error == NFSERR_EXPIRED) {
5802 *sflagsp |= NFSV4SEQ_EXPIREDALLSTATEREVOKED;
5804 } else if (error == NFSERR_ADMINREVOKED) {
5805 *sflagsp |= NFSV4SEQ_ADMINSTATEREVOKED;
5808 *highest_slotidp = *target_highest_slotidp = NFSV4_SLOTS - 1;
5813 * Check/set reclaim complete for this session/clientid.
5816 nfsrv_checkreclaimcomplete(struct nfsrv_descript *nd)
5818 struct nfsdsession *sep;
5819 struct nfssessionhash *shp;
5822 shp = NFSSESSIONHASH(nd->nd_sessionid);
5824 NFSLOCKSESSION(shp);
5825 sep = nfsrv_findsession(nd->nd_sessionid);
5827 NFSUNLOCKSESSION(shp);
5829 return (NFSERR_BADSESSION);
5832 /* Check to see if reclaim complete has already happened. */
5833 if ((sep->sess_clp->lc_flags & LCL_RECLAIMCOMPLETE) != 0)
5834 error = NFSERR_COMPLETEALREADY;
5836 sep->sess_clp->lc_flags |= LCL_RECLAIMCOMPLETE;
5837 NFSUNLOCKSESSION(shp);
5843 * Cache the reply in a session slot.
5846 nfsrv_cache_session(uint8_t *sessionid, uint32_t slotid, int repstat,
5849 struct nfsdsession *sep;
5850 struct nfssessionhash *shp;
5852 shp = NFSSESSIONHASH(sessionid);
5853 NFSLOCKSESSION(shp);
5854 sep = nfsrv_findsession(sessionid);
5856 NFSUNLOCKSESSION(shp);
5857 printf("nfsrv_cache_session: no session\n");
5861 nfsv4_seqsess_cacherep(slotid, sep->sess_slots, repstat, m);
5862 NFSUNLOCKSESSION(shp);
5866 * Search for a session that matches the sessionid.
5868 static struct nfsdsession *
5869 nfsrv_findsession(uint8_t *sessionid)
5871 struct nfsdsession *sep;
5872 struct nfssessionhash *shp;
5874 shp = NFSSESSIONHASH(sessionid);
5875 LIST_FOREACH(sep, &shp->list, sess_hash) {
5876 if (!NFSBCMP(sessionid, sep->sess_sessionid, NFSX_V4SESSIONID))
5883 * Destroy a session.
5886 nfsrv_destroysession(struct nfsrv_descript *nd, uint8_t *sessionid)
5888 int error, samesess;
5891 if (!NFSBCMP(sessionid, nd->nd_sessionid, NFSX_V4SESSIONID)) {
5893 if ((nd->nd_flag & ND_LASTOP) == 0)
5894 return (NFSERR_BADSESSION);
5896 error = nfsrv_freesession(NULL, sessionid);
5897 if (error == 0 && samesess != 0)
5898 nd->nd_flag &= ~ND_HASSEQUENCE;
5903 * Free up a session structure.
5906 nfsrv_freesession(struct nfsdsession *sep, uint8_t *sessionid)
5908 struct nfssessionhash *shp;
5912 shp = NFSSESSIONHASH(sessionid);
5913 NFSLOCKSESSION(shp);
5914 sep = nfsrv_findsession(sessionid);
5916 shp = NFSSESSIONHASH(sep->sess_sessionid);
5917 NFSLOCKSESSION(shp);
5922 if (sep->sess_refcnt > 0) {
5924 NFSUNLOCKSESSION(shp);
5927 LIST_REMOVE(sep, sess_hash);
5928 LIST_REMOVE(sep, sess_list);
5931 NFSUNLOCKSESSION(shp);
5933 return (NFSERR_BADSESSION);
5934 for (i = 0; i < NFSV4_SLOTS; i++)
5935 if (sep->sess_slots[i].nfssl_reply != NULL)
5936 m_freem(sep->sess_slots[i].nfssl_reply);
5937 if (sep->sess_cbsess.nfsess_xprt != NULL)
5938 SVC_RELEASE(sep->sess_cbsess.nfsess_xprt);
5939 free(sep, M_NFSDSESSION);
5945 * RFC5661 says that it should fail when there are associated opens, locks
5946 * or delegations. Since stateids represent opens, I don't see how you can
5947 * free an open stateid (it will be free'd when closed), so this function
5948 * only works for lock stateids (freeing the lock_owner) or delegations.
5951 nfsrv_freestateid(struct nfsrv_descript *nd, nfsv4stateid_t *stateidp,
5954 struct nfsclient *clp;
5955 struct nfsstate *stp;
5960 * Look up the stateid
5962 error = nfsrv_getclient((nfsquad_t)((u_quad_t)0), CLOPS_RENEW, &clp,
5963 NULL, (nfsquad_t)((u_quad_t)0), 0, nd, p);
5965 /* First, check for a delegation. */
5966 LIST_FOREACH(stp, &clp->lc_deleg, ls_list) {
5967 if (!NFSBCMP(stp->ls_stateid.other, stateidp->other,
5972 nfsrv_freedeleg(stp);
5977 /* Not a delegation, try for a lock_owner. */
5979 error = nfsrv_getstate(clp, stateidp, 0, &stp);
5980 if (error == 0 && ((stp->ls_flags & (NFSLCK_OPEN | NFSLCK_DELEGREAD |
5981 NFSLCK_DELEGWRITE)) != 0 || (stp->ls_flags & NFSLCK_LOCK) == 0))
5982 /* Not a lock_owner stateid. */
5983 error = NFSERR_LOCKSHELD;
5984 if (error == 0 && !LIST_EMPTY(&stp->ls_lock))
5985 error = NFSERR_LOCKSHELD;
5987 nfsrv_freelockowner(stp, NULL, 0, p);
5993 * Generate the xdr for an NFSv4.1 CBSequence Operation.
5996 nfsv4_setcbsequence(struct nfsrv_descript *nd, struct nfsclient *clp,
5997 int dont_replycache, struct nfsdsession **sepp)
5999 struct nfsdsession *sep;
6000 uint32_t *tl, slotseq = 0;
6001 int maxslot, slotpos;
6002 uint8_t sessionid[NFSX_V4SESSIONID];
6005 error = nfsv4_getcbsession(clp, sepp);
6009 (void)nfsv4_sequencelookup(NULL, &sep->sess_cbsess, &slotpos, &maxslot,
6010 &slotseq, sessionid);
6011 KASSERT(maxslot >= 0, ("nfsv4_setcbsequence neg maxslot"));
6013 /* Build the Sequence arguments. */
6014 NFSM_BUILD(tl, uint32_t *, NFSX_V4SESSIONID + 5 * NFSX_UNSIGNED);
6015 bcopy(sessionid, tl, NFSX_V4SESSIONID);
6016 tl += NFSX_V4SESSIONID / NFSX_UNSIGNED;
6017 nd->nd_slotseq = tl;
6018 *tl++ = txdr_unsigned(slotseq);
6019 *tl++ = txdr_unsigned(slotpos);
6020 *tl++ = txdr_unsigned(maxslot);
6021 if (dont_replycache == 0)
6022 *tl++ = newnfs_true;
6024 *tl++ = newnfs_false;
6025 *tl = 0; /* No referring call list, for now. */
6026 nd->nd_flag |= ND_HASSEQUENCE;
6031 * Get a session for the callback.
6034 nfsv4_getcbsession(struct nfsclient *clp, struct nfsdsession **sepp)
6036 struct nfsdsession *sep;
6039 LIST_FOREACH(sep, &clp->lc_session, sess_list) {
6040 if ((sep->sess_crflags & NFSV4CRSESS_CONNBACKCHAN) != 0)
6045 return (NFSERR_BADSESSION);