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;
44 extern int nfsd_debuglevel;
48 SYSCTL_DECL(_vfs_nfsd);
49 int nfsrv_statehashsize = NFSSTATEHASHSIZE;
50 TUNABLE_INT("vfs.nfsd.statehashsize", &nfsrv_statehashsize);
51 SYSCTL_INT(_vfs_nfsd, OID_AUTO, statehashsize, CTLFLAG_RDTUN,
52 &nfsrv_statehashsize, 0,
53 "Size of state hash table set via loader.conf");
55 int nfsrv_clienthashsize = NFSCLIENTHASHSIZE;
56 TUNABLE_INT("vfs.nfsd.clienthashsize", &nfsrv_clienthashsize);
57 SYSCTL_INT(_vfs_nfsd, OID_AUTO, clienthashsize, CTLFLAG_RDTUN,
58 &nfsrv_clienthashsize, 0,
59 "Size of client hash table set via loader.conf");
61 int nfsrv_lockhashsize = NFSLOCKHASHSIZE;
62 TUNABLE_INT("vfs.nfsd.fhhashsize", &nfsrv_lockhashsize);
63 SYSCTL_INT(_vfs_nfsd, OID_AUTO, fhhashsize, CTLFLAG_RDTUN,
64 &nfsrv_lockhashsize, 0,
65 "Size of file handle hash table set via loader.conf");
67 int nfsrv_sessionhashsize = NFSSESSIONHASHSIZE;
68 TUNABLE_INT("vfs.nfsd.sessionhashsize", &nfsrv_sessionhashsize);
69 SYSCTL_INT(_vfs_nfsd, OID_AUTO, sessionhashsize, CTLFLAG_RDTUN,
70 &nfsrv_sessionhashsize, 0,
71 "Size of session hash table set via loader.conf");
73 static int nfsrv_v4statelimit = NFSRV_V4STATELIMIT;
74 TUNABLE_INT("vfs.nfsd.v4statelimit", &nfsrv_v4statelimit);
75 SYSCTL_INT(_vfs_nfsd, OID_AUTO, v4statelimit, CTLFLAG_RWTUN,
76 &nfsrv_v4statelimit, 0,
77 "High water limit for NFSv4 opens+locks+delegations");
79 static int nfsrv_writedelegifpos = 0;
80 SYSCTL_INT(_vfs_nfsd, OID_AUTO, writedelegifpos, CTLFLAG_RW,
81 &nfsrv_writedelegifpos, 0,
82 "Issue a write delegation for read opens if possible");
84 static int nfsrv_allowreadforwriteopen = 1;
85 SYSCTL_INT(_vfs_nfsd, OID_AUTO, allowreadforwriteopen, CTLFLAG_RW,
86 &nfsrv_allowreadforwriteopen, 0,
87 "Allow Reads to be done with Write Access StateIDs");
90 * Hash lists for nfs V4.
92 struct nfsclienthashhead *nfsclienthash;
93 struct nfslockhashhead *nfslockhash;
94 struct nfssessionhash *nfssessionhash;
95 #endif /* !APPLEKEXT */
97 static u_int32_t nfsrv_openpluslock = 0, nfsrv_delegatecnt = 0;
98 static time_t nfsrvboottime;
99 static int nfsrv_returnoldstateid = 0, nfsrv_clients = 0;
100 static int nfsrv_clienthighwater = NFSRV_CLIENTHIGHWATER;
101 static int nfsrv_nogsscallback = 0;
102 static volatile int nfsrv_writedelegcnt = 0;
104 /* local functions */
105 static void nfsrv_dumpaclient(struct nfsclient *clp,
106 struct nfsd_dumpclients *dumpp);
107 static void nfsrv_freeopenowner(struct nfsstate *stp, int cansleep,
109 static int nfsrv_freeopen(struct nfsstate *stp, vnode_t vp, int cansleep,
111 static void nfsrv_freelockowner(struct nfsstate *stp, vnode_t vp, int cansleep,
113 static void nfsrv_freeallnfslocks(struct nfsstate *stp, vnode_t vp,
114 int cansleep, NFSPROC_T *p);
115 static void nfsrv_freenfslock(struct nfslock *lop);
116 static void nfsrv_freenfslockfile(struct nfslockfile *lfp);
117 static void nfsrv_freedeleg(struct nfsstate *);
118 static int nfsrv_getstate(struct nfsclient *clp, nfsv4stateid_t *stateidp,
119 u_int32_t flags, struct nfsstate **stpp);
120 static void nfsrv_getowner(struct nfsstatehead *hp, struct nfsstate *new_stp,
121 struct nfsstate **stpp);
122 static int nfsrv_getlockfh(vnode_t vp, u_short flags,
123 struct nfslockfile *new_lfp, fhandle_t *nfhp, NFSPROC_T *p);
124 static int nfsrv_getlockfile(u_short flags, struct nfslockfile **new_lfpp,
125 struct nfslockfile **lfpp, fhandle_t *nfhp, int lockit);
126 static void nfsrv_insertlock(struct nfslock *new_lop,
127 struct nfslock *insert_lop, struct nfsstate *stp, struct nfslockfile *lfp);
128 static void nfsrv_updatelock(struct nfsstate *stp, struct nfslock **new_lopp,
129 struct nfslock **other_lopp, struct nfslockfile *lfp);
130 static int nfsrv_getipnumber(u_char *cp);
131 static int nfsrv_checkrestart(nfsquad_t clientid, u_int32_t flags,
132 nfsv4stateid_t *stateidp, int specialid);
133 static int nfsrv_checkgrace(struct nfsrv_descript *nd, struct nfsclient *clp,
135 static int nfsrv_docallback(struct nfsclient *clp, int procnum,
136 nfsv4stateid_t *stateidp, int trunc, fhandle_t *fhp,
137 struct nfsvattr *nap, nfsattrbit_t *attrbitp, NFSPROC_T *p);
138 static int nfsrv_cbcallargs(struct nfsrv_descript *nd, struct nfsclient *clp,
139 uint32_t callback, int op, const char *optag, struct nfsdsession **sepp);
140 static u_int32_t nfsrv_nextclientindex(void);
141 static u_int32_t nfsrv_nextstateindex(struct nfsclient *clp);
142 static void nfsrv_markstable(struct nfsclient *clp);
143 static void nfsrv_markreclaim(struct nfsclient *clp);
144 static int nfsrv_checkstable(struct nfsclient *clp);
145 static int nfsrv_clientconflict(struct nfsclient *clp, int *haslockp, struct
146 vnode *vp, NFSPROC_T *p);
147 static int nfsrv_delegconflict(struct nfsstate *stp, int *haslockp,
148 NFSPROC_T *p, vnode_t vp);
149 static int nfsrv_cleandeleg(vnode_t vp, struct nfslockfile *lfp,
150 struct nfsclient *clp, int *haslockp, NFSPROC_T *p);
151 static int nfsrv_notsamecredname(struct nfsrv_descript *nd,
152 struct nfsclient *clp);
153 static time_t nfsrv_leaseexpiry(void);
154 static void nfsrv_delaydelegtimeout(struct nfsstate *stp);
155 static int nfsrv_checkseqid(struct nfsrv_descript *nd, u_int32_t seqid,
156 struct nfsstate *stp, struct nfsrvcache *op);
157 static int nfsrv_nootherstate(struct nfsstate *stp);
158 static int nfsrv_locallock(vnode_t vp, struct nfslockfile *lfp, int flags,
159 uint64_t first, uint64_t end, struct nfslockconflict *cfp, NFSPROC_T *p);
160 static void nfsrv_localunlock(vnode_t vp, struct nfslockfile *lfp,
161 uint64_t init_first, uint64_t init_end, NFSPROC_T *p);
162 static int nfsrv_dolocal(vnode_t vp, struct nfslockfile *lfp, int flags,
163 int oldflags, uint64_t first, uint64_t end, struct nfslockconflict *cfp,
165 static void nfsrv_locallock_rollback(vnode_t vp, struct nfslockfile *lfp,
167 static void nfsrv_locallock_commit(struct nfslockfile *lfp, int flags,
168 uint64_t first, uint64_t end);
169 static void nfsrv_locklf(struct nfslockfile *lfp);
170 static void nfsrv_unlocklf(struct nfslockfile *lfp);
171 static struct nfsdsession *nfsrv_findsession(uint8_t *sessionid);
172 static int nfsrv_freesession(struct nfsdsession *sep, uint8_t *sessionid);
173 static int nfsv4_setcbsequence(struct nfsrv_descript *nd, struct nfsclient *clp,
174 int dont_replycache, struct nfsdsession **sepp);
175 static int nfsv4_getcbsession(struct nfsclient *clp, struct nfsdsession **sepp);
178 * Scan the client list for a match and either return the current one,
179 * create a new entry or return an error.
180 * If returning a non-error, the clp structure must either be linked into
181 * the client list or free'd.
184 nfsrv_setclient(struct nfsrv_descript *nd, struct nfsclient **new_clpp,
185 nfsquad_t *clientidp, nfsquad_t *confirmp, NFSPROC_T *p)
187 struct nfsclient *clp = NULL, *new_clp = *new_clpp;
188 int i, error = 0, ret;
189 struct nfsstate *stp, *tstp;
190 struct sockaddr_in *sad, *rad;
191 struct nfsdsession *sep, *nsep;
192 int zapit = 0, gotit, hasstate = 0, igotlock;
193 static u_int64_t confirm_index = 0;
196 * Check for state resource limit exceeded.
198 if (nfsrv_openpluslock > nfsrv_v4statelimit) {
199 error = NFSERR_RESOURCE;
203 if (nfsrv_issuedelegs == 0 ||
204 ((nd->nd_flag & ND_GSS) != 0 && nfsrv_nogsscallback != 0))
206 * Don't do callbacks when delegations are disabled or
207 * for AUTH_GSS unless enabled via nfsrv_nogsscallback.
208 * If establishing a callback connection is attempted
209 * when a firewall is blocking the callback path, the
210 * server may wait too long for the connect attempt to
211 * succeed during the Open. Some clients, such as Linux,
212 * may timeout and give up on the Open before the server
213 * replies. Also, since AUTH_GSS callbacks are not
214 * yet interoperability tested, they might cause the
215 * server to crap out, if they get past the Init call to
218 new_clp->lc_program = 0;
220 /* Lock out other nfsd threads */
221 NFSLOCKV4ROOTMUTEX();
222 nfsv4_relref(&nfsv4rootfs_lock);
224 igotlock = nfsv4_lock(&nfsv4rootfs_lock, 1, NULL,
225 NFSV4ROOTLOCKMUTEXPTR, NULL);
227 NFSUNLOCKV4ROOTMUTEX();
230 * Search for a match in the client list.
233 while (i < nfsrv_clienthashsize && !gotit) {
234 LIST_FOREACH(clp, &nfsclienthash[i], lc_hash) {
235 if (new_clp->lc_idlen == clp->lc_idlen &&
236 !NFSBCMP(new_clp->lc_id, clp->lc_id, clp->lc_idlen)) {
245 (clp->lc_flags & (LCL_NEEDSCONFIRM | LCL_ADMINREVOKED))) {
246 if ((nd->nd_flag & ND_NFSV41) != 0 && confirmp->lval[1] != 0) {
248 * For NFSv4.1, if confirmp->lval[1] is non-zero, the
249 * client is trying to update a confirmed clientid.
251 NFSLOCKV4ROOTMUTEX();
252 nfsv4_unlock(&nfsv4rootfs_lock, 1);
253 NFSUNLOCKV4ROOTMUTEX();
254 confirmp->lval[1] = 0;
255 error = NFSERR_NOENT;
259 * Get rid of the old one.
261 if (i != nfsrv_clienthashsize) {
262 LIST_REMOVE(clp, lc_hash);
263 nfsrv_cleanclient(clp, p);
264 nfsrv_freedeleglist(&clp->lc_deleg);
265 nfsrv_freedeleglist(&clp->lc_olddeleg);
269 * Add it after assigning a client id to it.
271 new_clp->lc_flags |= LCL_NEEDSCONFIRM;
272 if ((nd->nd_flag & ND_NFSV41) != 0)
273 new_clp->lc_confirm.lval[0] = confirmp->lval[0] =
276 confirmp->qval = new_clp->lc_confirm.qval =
278 clientidp->lval[0] = new_clp->lc_clientid.lval[0] =
279 (u_int32_t)nfsrvboottime;
280 clientidp->lval[1] = new_clp->lc_clientid.lval[1] =
281 nfsrv_nextclientindex();
282 new_clp->lc_stateindex = 0;
283 new_clp->lc_statemaxindex = 0;
284 new_clp->lc_cbref = 0;
285 new_clp->lc_expiry = nfsrv_leaseexpiry();
286 LIST_INIT(&new_clp->lc_open);
287 LIST_INIT(&new_clp->lc_deleg);
288 LIST_INIT(&new_clp->lc_olddeleg);
289 LIST_INIT(&new_clp->lc_session);
290 for (i = 0; i < nfsrv_statehashsize; i++)
291 LIST_INIT(&new_clp->lc_stateid[i]);
292 LIST_INSERT_HEAD(NFSCLIENTHASH(new_clp->lc_clientid), new_clp,
294 newnfsstats.srvclients++;
295 nfsrv_openpluslock++;
297 NFSLOCKV4ROOTMUTEX();
298 nfsv4_unlock(&nfsv4rootfs_lock, 1);
299 NFSUNLOCKV4ROOTMUTEX();
301 nfsrv_zapclient(clp, p);
307 * Now, handle the cases where the id is already issued.
309 if (nfsrv_notsamecredname(nd, clp)) {
311 * Check to see if there is expired state that should go away.
313 if (clp->lc_expiry < NFSD_MONOSEC &&
314 (!LIST_EMPTY(&clp->lc_open) || !LIST_EMPTY(&clp->lc_deleg))) {
315 nfsrv_cleanclient(clp, p);
316 nfsrv_freedeleglist(&clp->lc_deleg);
320 * If there is outstanding state, then reply NFSERR_CLIDINUSE per
321 * RFC3530 Sec. 8.1.2 last para.
323 if (!LIST_EMPTY(&clp->lc_deleg)) {
325 } else if (LIST_EMPTY(&clp->lc_open)) {
329 /* Look for an Open on the OpenOwner */
330 LIST_FOREACH(stp, &clp->lc_open, ls_list) {
331 if (!LIST_EMPTY(&stp->ls_open)) {
339 * If the uid doesn't match, return NFSERR_CLIDINUSE after
340 * filling out the correct ipaddr and portnum.
342 sad = NFSSOCKADDR(new_clp->lc_req.nr_nam, struct sockaddr_in *);
343 rad = NFSSOCKADDR(clp->lc_req.nr_nam, struct sockaddr_in *);
344 sad->sin_addr.s_addr = rad->sin_addr.s_addr;
345 sad->sin_port = rad->sin_port;
346 NFSLOCKV4ROOTMUTEX();
347 nfsv4_unlock(&nfsv4rootfs_lock, 1);
348 NFSUNLOCKV4ROOTMUTEX();
349 error = NFSERR_CLIDINUSE;
354 if (NFSBCMP(new_clp->lc_verf, clp->lc_verf, NFSX_VERF)) {
356 * If the verifier has changed, the client has rebooted
357 * and a new client id is issued. The old state info
358 * can be thrown away once the SETCLIENTID_CONFIRM occurs.
360 LIST_REMOVE(clp, lc_hash);
362 /* Get rid of all sessions on this clientid. */
363 LIST_FOREACH_SAFE(sep, &clp->lc_session, sess_list, nsep) {
364 ret = nfsrv_freesession(sep, NULL);
366 printf("nfsrv_setclient: verifier changed free"
367 " session failed=%d\n", ret);
370 new_clp->lc_flags |= LCL_NEEDSCONFIRM;
371 if ((nd->nd_flag & ND_NFSV41) != 0)
372 new_clp->lc_confirm.lval[0] = confirmp->lval[0] =
375 confirmp->qval = new_clp->lc_confirm.qval =
377 clientidp->lval[0] = new_clp->lc_clientid.lval[0] =
379 clientidp->lval[1] = new_clp->lc_clientid.lval[1] =
380 nfsrv_nextclientindex();
381 new_clp->lc_stateindex = 0;
382 new_clp->lc_statemaxindex = 0;
383 new_clp->lc_cbref = 0;
384 new_clp->lc_expiry = nfsrv_leaseexpiry();
387 * Save the state until confirmed.
389 LIST_NEWHEAD(&new_clp->lc_open, &clp->lc_open, ls_list);
390 LIST_FOREACH(tstp, &new_clp->lc_open, ls_list)
391 tstp->ls_clp = new_clp;
392 LIST_NEWHEAD(&new_clp->lc_deleg, &clp->lc_deleg, ls_list);
393 LIST_FOREACH(tstp, &new_clp->lc_deleg, ls_list)
394 tstp->ls_clp = new_clp;
395 LIST_NEWHEAD(&new_clp->lc_olddeleg, &clp->lc_olddeleg,
397 LIST_FOREACH(tstp, &new_clp->lc_olddeleg, ls_list)
398 tstp->ls_clp = new_clp;
399 for (i = 0; i < nfsrv_statehashsize; i++) {
400 LIST_NEWHEAD(&new_clp->lc_stateid[i],
401 &clp->lc_stateid[i], ls_hash);
402 LIST_FOREACH(tstp, &new_clp->lc_stateid[i], ls_hash)
403 tstp->ls_clp = new_clp;
405 LIST_INIT(&new_clp->lc_session);
406 LIST_INSERT_HEAD(NFSCLIENTHASH(new_clp->lc_clientid), new_clp,
408 newnfsstats.srvclients++;
409 nfsrv_openpluslock++;
411 NFSLOCKV4ROOTMUTEX();
412 nfsv4_unlock(&nfsv4rootfs_lock, 1);
413 NFSUNLOCKV4ROOTMUTEX();
416 * Must wait until any outstanding callback on the old clp
420 while (clp->lc_cbref) {
421 clp->lc_flags |= LCL_WAKEUPWANTED;
422 (void)mtx_sleep(clp, NFSSTATEMUTEXPTR, PZERO - 1,
423 "nfsd clp", 10 * hz);
426 nfsrv_zapclient(clp, p);
431 /* For NFSv4.1, mark that we found a confirmed clientid. */
432 if ((nd->nd_flag & ND_NFSV41) != 0) {
433 clientidp->lval[0] = clp->lc_clientid.lval[0];
434 clientidp->lval[1] = clp->lc_clientid.lval[1];
435 confirmp->lval[0] = 0; /* Ignored by client */
436 confirmp->lval[1] = 1;
439 * id and verifier match, so update the net address info
440 * and get rid of any existing callback authentication
441 * handle, so a new one will be acquired.
443 LIST_REMOVE(clp, lc_hash);
444 new_clp->lc_flags |= (LCL_NEEDSCONFIRM | LCL_DONTCLEAN);
445 new_clp->lc_expiry = nfsrv_leaseexpiry();
446 confirmp->qval = new_clp->lc_confirm.qval = ++confirm_index;
447 clientidp->lval[0] = new_clp->lc_clientid.lval[0] =
448 clp->lc_clientid.lval[0];
449 clientidp->lval[1] = new_clp->lc_clientid.lval[1] =
450 clp->lc_clientid.lval[1];
451 new_clp->lc_delegtime = clp->lc_delegtime;
452 new_clp->lc_stateindex = clp->lc_stateindex;
453 new_clp->lc_statemaxindex = clp->lc_statemaxindex;
454 new_clp->lc_cbref = 0;
455 LIST_NEWHEAD(&new_clp->lc_open, &clp->lc_open, ls_list);
456 LIST_FOREACH(tstp, &new_clp->lc_open, ls_list)
457 tstp->ls_clp = new_clp;
458 LIST_NEWHEAD(&new_clp->lc_deleg, &clp->lc_deleg, ls_list);
459 LIST_FOREACH(tstp, &new_clp->lc_deleg, ls_list)
460 tstp->ls_clp = new_clp;
461 LIST_NEWHEAD(&new_clp->lc_olddeleg, &clp->lc_olddeleg, ls_list);
462 LIST_FOREACH(tstp, &new_clp->lc_olddeleg, ls_list)
463 tstp->ls_clp = new_clp;
464 for (i = 0; i < nfsrv_statehashsize; i++) {
465 LIST_NEWHEAD(&new_clp->lc_stateid[i],
466 &clp->lc_stateid[i], ls_hash);
467 LIST_FOREACH(tstp, &new_clp->lc_stateid[i], ls_hash)
468 tstp->ls_clp = new_clp;
470 LIST_INIT(&new_clp->lc_session);
471 LIST_INSERT_HEAD(NFSCLIENTHASH(new_clp->lc_clientid), new_clp,
473 newnfsstats.srvclients++;
474 nfsrv_openpluslock++;
477 NFSLOCKV4ROOTMUTEX();
478 nfsv4_unlock(&nfsv4rootfs_lock, 1);
479 NFSUNLOCKV4ROOTMUTEX();
481 if ((nd->nd_flag & ND_NFSV41) == 0) {
483 * Must wait until any outstanding callback on the old clp
487 while (clp->lc_cbref) {
488 clp->lc_flags |= LCL_WAKEUPWANTED;
489 (void)mtx_sleep(clp, NFSSTATEMUTEXPTR, PZERO - 1,
493 nfsrv_zapclient(clp, p);
498 NFSEXITCODE2(error, nd);
503 * Check to see if the client id exists and optionally confirm it.
506 nfsrv_getclient(nfsquad_t clientid, int opflags, struct nfsclient **clpp,
507 struct nfsdsession *nsep, nfsquad_t confirm, uint32_t cbprogram,
508 struct nfsrv_descript *nd, NFSPROC_T *p)
510 struct nfsclient *clp;
511 struct nfsstate *stp;
513 struct nfsclienthashhead *hp;
514 int error = 0, igotlock, doneok;
515 struct nfssessionhash *shp;
516 struct nfsdsession *sep;
518 static uint64_t next_sess = 0;
522 if ((nd == NULL || (nd->nd_flag & ND_NFSV41) == 0 ||
523 opflags != CLOPS_RENEW) && nfsrvboottime != clientid.lval[0]) {
524 error = NFSERR_STALECLIENTID;
529 * If called with opflags == CLOPS_RENEW, the State Lock is
530 * already held. Otherwise, we need to get either that or,
531 * for the case of Confirm, lock out the nfsd threads.
533 if (opflags & CLOPS_CONFIRM) {
534 NFSLOCKV4ROOTMUTEX();
535 nfsv4_relref(&nfsv4rootfs_lock);
537 igotlock = nfsv4_lock(&nfsv4rootfs_lock, 1, NULL,
538 NFSV4ROOTLOCKMUTEXPTR, NULL);
541 * Create a new sessionid here, since we need to do it where
542 * there is a mutex held to serialize update of next_sess.
544 if ((nd->nd_flag & ND_NFSV41) != 0) {
545 sessid[0] = ++next_sess;
546 sessid[1] = clientid.qval;
548 NFSUNLOCKV4ROOTMUTEX();
549 } else if (opflags != CLOPS_RENEW) {
553 /* For NFSv4.1, the clp is acquired from the associated session. */
554 if (nd != NULL && (nd->nd_flag & ND_NFSV41) != 0 &&
555 opflags == CLOPS_RENEW) {
557 if ((nd->nd_flag & ND_HASSEQUENCE) != 0) {
558 shp = NFSSESSIONHASH(nd->nd_sessionid);
560 sep = nfsrv_findsession(nd->nd_sessionid);
563 NFSUNLOCKSESSION(shp);
566 hp = NFSCLIENTHASH(clientid);
567 LIST_FOREACH(clp, hp, lc_hash) {
568 if (clp->lc_clientid.lval[1] == clientid.lval[1])
573 if (opflags & CLOPS_CONFIRM)
574 error = NFSERR_STALECLIENTID;
576 error = NFSERR_EXPIRED;
577 } else if (clp->lc_flags & LCL_ADMINREVOKED) {
579 * If marked admin revoked, just return the error.
581 error = NFSERR_ADMINREVOKED;
584 if (opflags & CLOPS_CONFIRM) {
585 NFSLOCKV4ROOTMUTEX();
586 nfsv4_unlock(&nfsv4rootfs_lock, 1);
587 NFSUNLOCKV4ROOTMUTEX();
588 } else if (opflags != CLOPS_RENEW) {
595 * Perform any operations specified by the opflags.
597 if (opflags & CLOPS_CONFIRM) {
598 if (((nd->nd_flag & ND_NFSV41) != 0 &&
599 clp->lc_confirm.lval[0] != confirm.lval[0]) ||
600 ((nd->nd_flag & ND_NFSV41) == 0 &&
601 clp->lc_confirm.qval != confirm.qval))
602 error = NFSERR_STALECLIENTID;
603 else if (nfsrv_notsamecredname(nd, clp))
604 error = NFSERR_CLIDINUSE;
607 if ((clp->lc_flags & (LCL_NEEDSCONFIRM | LCL_DONTCLEAN)) ==
610 * Hang onto the delegations (as old delegations)
611 * for an Open with CLAIM_DELEGATE_PREV unless in
612 * grace, but get rid of the rest of the state.
614 nfsrv_cleanclient(clp, p);
615 nfsrv_freedeleglist(&clp->lc_olddeleg);
616 if (nfsrv_checkgrace(nd, clp, 0)) {
617 /* In grace, so just delete delegations */
618 nfsrv_freedeleglist(&clp->lc_deleg);
620 LIST_FOREACH(stp, &clp->lc_deleg, ls_list)
621 stp->ls_flags |= NFSLCK_OLDDELEG;
622 clp->lc_delegtime = NFSD_MONOSEC +
623 nfsrv_lease + NFSRV_LEASEDELTA;
624 LIST_NEWHEAD(&clp->lc_olddeleg, &clp->lc_deleg,
627 if ((nd->nd_flag & ND_NFSV41) != 0)
628 clp->lc_program = cbprogram;
630 clp->lc_flags &= ~(LCL_NEEDSCONFIRM | LCL_DONTCLEAN);
632 clp->lc_flags |= LCL_NEEDSCBNULL;
633 /* For NFSv4.1, link the session onto the client. */
635 /* Hold a reference on the xprt for a backchannel. */
636 if ((nsep->sess_crflags & NFSV4CRSESS_CONNBACKCHAN)
638 if (clp->lc_req.nr_client == NULL)
639 clp->lc_req.nr_client = (struct __rpc_client *)
640 clnt_bck_create(nd->nd_xprt->xp_socket,
641 cbprogram, NFSV4_CBVERS);
642 if (clp->lc_req.nr_client != NULL) {
643 SVC_ACQUIRE(nd->nd_xprt);
645 clp->lc_req.nr_client->cl_private;
646 /* Disable idle timeout. */
647 nd->nd_xprt->xp_idletimeout = 0;
648 nsep->sess_cbsess.nfsess_xprt = nd->nd_xprt;
650 nsep->sess_crflags &= ~NFSV4CRSESS_CONNBACKCHAN;
652 NFSBCOPY(sessid, nsep->sess_sessionid,
654 NFSBCOPY(sessid, nsep->sess_cbsess.nfsess_sessionid,
656 shp = NFSSESSIONHASH(nsep->sess_sessionid);
659 LIST_INSERT_HEAD(&shp->list, nsep, sess_hash);
660 LIST_INSERT_HEAD(&clp->lc_session, nsep, sess_list);
661 nsep->sess_clp = clp;
662 NFSUNLOCKSESSION(shp);
666 } else if (clp->lc_flags & LCL_NEEDSCONFIRM) {
667 error = NFSERR_EXPIRED;
671 * If called by the Renew Op, we must check the principal.
673 if (!error && (opflags & CLOPS_RENEWOP)) {
674 if (nfsrv_notsamecredname(nd, clp)) {
676 for (i = 0; i < nfsrv_statehashsize && doneok == 0; i++) {
677 LIST_FOREACH(stp, &clp->lc_stateid[i], ls_hash) {
678 if ((stp->ls_flags & NFSLCK_OPEN) &&
679 stp->ls_uid == nd->nd_cred->cr_uid) {
686 error = NFSERR_ACCES;
688 if (!error && (clp->lc_flags & LCL_CBDOWN))
689 error = NFSERR_CBPATHDOWN;
691 if ((!error || error == NFSERR_CBPATHDOWN) &&
692 (opflags & CLOPS_RENEW)) {
693 clp->lc_expiry = nfsrv_leaseexpiry();
695 if (opflags & CLOPS_CONFIRM) {
696 NFSLOCKV4ROOTMUTEX();
697 nfsv4_unlock(&nfsv4rootfs_lock, 1);
698 NFSUNLOCKV4ROOTMUTEX();
699 } else if (opflags != CLOPS_RENEW) {
706 NFSEXITCODE2(error, nd);
711 * Perform the NFSv4.1 destroy clientid.
714 nfsrv_destroyclient(nfsquad_t clientid, NFSPROC_T *p)
716 struct nfsclient *clp;
717 struct nfsclienthashhead *hp;
718 int error = 0, i, igotlock;
720 if (nfsrvboottime != clientid.lval[0]) {
721 error = NFSERR_STALECLIENTID;
725 /* Lock out other nfsd threads */
726 NFSLOCKV4ROOTMUTEX();
727 nfsv4_relref(&nfsv4rootfs_lock);
729 igotlock = nfsv4_lock(&nfsv4rootfs_lock, 1, NULL,
730 NFSV4ROOTLOCKMUTEXPTR, NULL);
731 } while (igotlock == 0);
732 NFSUNLOCKV4ROOTMUTEX();
734 hp = NFSCLIENTHASH(clientid);
735 LIST_FOREACH(clp, hp, lc_hash) {
736 if (clp->lc_clientid.lval[1] == clientid.lval[1])
740 NFSLOCKV4ROOTMUTEX();
741 nfsv4_unlock(&nfsv4rootfs_lock, 1);
742 NFSUNLOCKV4ROOTMUTEX();
743 /* Just return ok, since it is gone. */
747 /* Scan for state on the clientid. */
748 for (i = 0; i < nfsrv_statehashsize; i++)
749 if (!LIST_EMPTY(&clp->lc_stateid[i])) {
750 NFSLOCKV4ROOTMUTEX();
751 nfsv4_unlock(&nfsv4rootfs_lock, 1);
752 NFSUNLOCKV4ROOTMUTEX();
753 error = NFSERR_CLIENTIDBUSY;
756 if (!LIST_EMPTY(&clp->lc_session) || !LIST_EMPTY(&clp->lc_deleg)) {
757 NFSLOCKV4ROOTMUTEX();
758 nfsv4_unlock(&nfsv4rootfs_lock, 1);
759 NFSUNLOCKV4ROOTMUTEX();
760 error = NFSERR_CLIENTIDBUSY;
764 /* Destroy the clientid and return ok. */
765 nfsrv_cleanclient(clp, p);
766 nfsrv_freedeleglist(&clp->lc_deleg);
767 nfsrv_freedeleglist(&clp->lc_olddeleg);
768 LIST_REMOVE(clp, lc_hash);
769 NFSLOCKV4ROOTMUTEX();
770 nfsv4_unlock(&nfsv4rootfs_lock, 1);
771 NFSUNLOCKV4ROOTMUTEX();
772 nfsrv_zapclient(clp, p);
774 NFSEXITCODE2(error, nd);
779 * Called from the new nfssvc syscall to admin revoke a clientid.
780 * Returns 0 for success, error otherwise.
783 nfsrv_adminrevoke(struct nfsd_clid *revokep, NFSPROC_T *p)
785 struct nfsclient *clp = NULL;
790 * First, lock out the nfsd so that state won't change while the
791 * revocation record is being written to the stable storage restart
794 NFSLOCKV4ROOTMUTEX();
796 igotlock = nfsv4_lock(&nfsv4rootfs_lock, 1, NULL,
797 NFSV4ROOTLOCKMUTEXPTR, NULL);
799 NFSUNLOCKV4ROOTMUTEX();
802 * Search for a match in the client list.
805 while (i < nfsrv_clienthashsize && !gotit) {
806 LIST_FOREACH(clp, &nfsclienthash[i], lc_hash) {
807 if (revokep->nclid_idlen == clp->lc_idlen &&
808 !NFSBCMP(revokep->nclid_id, clp->lc_id, clp->lc_idlen)) {
816 NFSLOCKV4ROOTMUTEX();
817 nfsv4_unlock(&nfsv4rootfs_lock, 0);
818 NFSUNLOCKV4ROOTMUTEX();
824 * Now, write out the revocation record
826 nfsrv_writestable(clp->lc_id, clp->lc_idlen, NFSNST_REVOKE, p);
827 nfsrv_backupstable();
830 * and clear out the state, marking the clientid revoked.
832 clp->lc_flags &= ~LCL_CALLBACKSON;
833 clp->lc_flags |= LCL_ADMINREVOKED;
834 nfsrv_cleanclient(clp, p);
835 nfsrv_freedeleglist(&clp->lc_deleg);
836 nfsrv_freedeleglist(&clp->lc_olddeleg);
837 NFSLOCKV4ROOTMUTEX();
838 nfsv4_unlock(&nfsv4rootfs_lock, 0);
839 NFSUNLOCKV4ROOTMUTEX();
847 * Dump out stats for all clients. Called from nfssvc(2), that is used
851 nfsrv_dumpclients(struct nfsd_dumpclients *dumpp, int maxcnt)
853 struct nfsclient *clp;
857 * First, get a reference on the nfsv4rootfs_lock so that an
858 * exclusive lock cannot be acquired while dumping the clients.
860 NFSLOCKV4ROOTMUTEX();
861 nfsv4_getref(&nfsv4rootfs_lock, NULL, NFSV4ROOTLOCKMUTEXPTR, NULL);
862 NFSUNLOCKV4ROOTMUTEX();
865 * Rattle through the client lists until done.
867 while (i < nfsrv_clienthashsize && cnt < maxcnt) {
868 clp = LIST_FIRST(&nfsclienthash[i]);
869 while (clp != LIST_END(&nfsclienthash[i]) && cnt < maxcnt) {
870 nfsrv_dumpaclient(clp, &dumpp[cnt]);
872 clp = LIST_NEXT(clp, lc_hash);
877 dumpp[cnt].ndcl_clid.nclid_idlen = 0;
879 NFSLOCKV4ROOTMUTEX();
880 nfsv4_relref(&nfsv4rootfs_lock);
881 NFSUNLOCKV4ROOTMUTEX();
885 * Dump stats for a client. Must be called with the NFSSTATELOCK and spl'd.
888 nfsrv_dumpaclient(struct nfsclient *clp, struct nfsd_dumpclients *dumpp)
890 struct nfsstate *stp, *openstp, *lckownstp;
892 struct sockaddr *sad;
893 struct sockaddr_in *rad;
894 struct sockaddr_in6 *rad6;
896 dumpp->ndcl_nopenowners = dumpp->ndcl_nlockowners = 0;
897 dumpp->ndcl_nopens = dumpp->ndcl_nlocks = 0;
898 dumpp->ndcl_ndelegs = dumpp->ndcl_nolddelegs = 0;
899 dumpp->ndcl_flags = clp->lc_flags;
900 dumpp->ndcl_clid.nclid_idlen = clp->lc_idlen;
901 NFSBCOPY(clp->lc_id, dumpp->ndcl_clid.nclid_id, clp->lc_idlen);
902 sad = NFSSOCKADDR(clp->lc_req.nr_nam, struct sockaddr *);
903 dumpp->ndcl_addrfam = sad->sa_family;
904 if (sad->sa_family == AF_INET) {
905 rad = (struct sockaddr_in *)sad;
906 dumpp->ndcl_cbaddr.sin_addr = rad->sin_addr;
908 rad6 = (struct sockaddr_in6 *)sad;
909 dumpp->ndcl_cbaddr.sin6_addr = rad6->sin6_addr;
913 * Now, scan the state lists and total up the opens and locks.
915 LIST_FOREACH(stp, &clp->lc_open, ls_list) {
916 dumpp->ndcl_nopenowners++;
917 LIST_FOREACH(openstp, &stp->ls_open, ls_list) {
918 dumpp->ndcl_nopens++;
919 LIST_FOREACH(lckownstp, &openstp->ls_open, ls_list) {
920 dumpp->ndcl_nlockowners++;
921 LIST_FOREACH(lop, &lckownstp->ls_lock, lo_lckowner) {
922 dumpp->ndcl_nlocks++;
929 * and the delegation lists.
931 LIST_FOREACH(stp, &clp->lc_deleg, ls_list) {
932 dumpp->ndcl_ndelegs++;
934 LIST_FOREACH(stp, &clp->lc_olddeleg, ls_list) {
935 dumpp->ndcl_nolddelegs++;
940 * Dump out lock stats for a file.
943 nfsrv_dumplocks(vnode_t vp, struct nfsd_dumplocks *ldumpp, int maxcnt,
946 struct nfsstate *stp;
949 struct nfslockfile *lfp;
950 struct sockaddr *sad;
951 struct sockaddr_in *rad;
952 struct sockaddr_in6 *rad6;
956 ret = nfsrv_getlockfh(vp, 0, NULL, &nfh, p);
958 * First, get a reference on the nfsv4rootfs_lock so that an
959 * exclusive lock on it cannot be acquired while dumping the locks.
961 NFSLOCKV4ROOTMUTEX();
962 nfsv4_getref(&nfsv4rootfs_lock, NULL, NFSV4ROOTLOCKMUTEXPTR, NULL);
963 NFSUNLOCKV4ROOTMUTEX();
966 ret = nfsrv_getlockfile(0, NULL, &lfp, &nfh, 0);
968 ldumpp[0].ndlck_clid.nclid_idlen = 0;
970 NFSLOCKV4ROOTMUTEX();
971 nfsv4_relref(&nfsv4rootfs_lock);
972 NFSUNLOCKV4ROOTMUTEX();
977 * For each open share on file, dump it out.
979 stp = LIST_FIRST(&lfp->lf_open);
980 while (stp != LIST_END(&lfp->lf_open) && cnt < maxcnt) {
981 ldumpp[cnt].ndlck_flags = stp->ls_flags;
982 ldumpp[cnt].ndlck_stateid.seqid = stp->ls_stateid.seqid;
983 ldumpp[cnt].ndlck_stateid.other[0] = stp->ls_stateid.other[0];
984 ldumpp[cnt].ndlck_stateid.other[1] = stp->ls_stateid.other[1];
985 ldumpp[cnt].ndlck_stateid.other[2] = stp->ls_stateid.other[2];
986 ldumpp[cnt].ndlck_owner.nclid_idlen =
987 stp->ls_openowner->ls_ownerlen;
988 NFSBCOPY(stp->ls_openowner->ls_owner,
989 ldumpp[cnt].ndlck_owner.nclid_id,
990 stp->ls_openowner->ls_ownerlen);
991 ldumpp[cnt].ndlck_clid.nclid_idlen = stp->ls_clp->lc_idlen;
992 NFSBCOPY(stp->ls_clp->lc_id, ldumpp[cnt].ndlck_clid.nclid_id,
993 stp->ls_clp->lc_idlen);
994 sad=NFSSOCKADDR(stp->ls_clp->lc_req.nr_nam, struct sockaddr *);
995 ldumpp[cnt].ndlck_addrfam = sad->sa_family;
996 if (sad->sa_family == AF_INET) {
997 rad = (struct sockaddr_in *)sad;
998 ldumpp[cnt].ndlck_cbaddr.sin_addr = rad->sin_addr;
1000 rad6 = (struct sockaddr_in6 *)sad;
1001 ldumpp[cnt].ndlck_cbaddr.sin6_addr = rad6->sin6_addr;
1003 stp = LIST_NEXT(stp, ls_file);
1010 lop = LIST_FIRST(&lfp->lf_lock);
1011 while (lop != LIST_END(&lfp->lf_lock) && cnt < maxcnt) {
1013 ldumpp[cnt].ndlck_flags = lop->lo_flags;
1014 ldumpp[cnt].ndlck_first = lop->lo_first;
1015 ldumpp[cnt].ndlck_end = lop->lo_end;
1016 ldumpp[cnt].ndlck_stateid.seqid = stp->ls_stateid.seqid;
1017 ldumpp[cnt].ndlck_stateid.other[0] = stp->ls_stateid.other[0];
1018 ldumpp[cnt].ndlck_stateid.other[1] = stp->ls_stateid.other[1];
1019 ldumpp[cnt].ndlck_stateid.other[2] = stp->ls_stateid.other[2];
1020 ldumpp[cnt].ndlck_owner.nclid_idlen = stp->ls_ownerlen;
1021 NFSBCOPY(stp->ls_owner, ldumpp[cnt].ndlck_owner.nclid_id,
1023 ldumpp[cnt].ndlck_clid.nclid_idlen = stp->ls_clp->lc_idlen;
1024 NFSBCOPY(stp->ls_clp->lc_id, ldumpp[cnt].ndlck_clid.nclid_id,
1025 stp->ls_clp->lc_idlen);
1026 sad=NFSSOCKADDR(stp->ls_clp->lc_req.nr_nam, struct sockaddr *);
1027 ldumpp[cnt].ndlck_addrfam = sad->sa_family;
1028 if (sad->sa_family == AF_INET) {
1029 rad = (struct sockaddr_in *)sad;
1030 ldumpp[cnt].ndlck_cbaddr.sin_addr = rad->sin_addr;
1032 rad6 = (struct sockaddr_in6 *)sad;
1033 ldumpp[cnt].ndlck_cbaddr.sin6_addr = rad6->sin6_addr;
1035 lop = LIST_NEXT(lop, lo_lckfile);
1040 * and the delegations.
1042 stp = LIST_FIRST(&lfp->lf_deleg);
1043 while (stp != LIST_END(&lfp->lf_deleg) && cnt < maxcnt) {
1044 ldumpp[cnt].ndlck_flags = stp->ls_flags;
1045 ldumpp[cnt].ndlck_stateid.seqid = stp->ls_stateid.seqid;
1046 ldumpp[cnt].ndlck_stateid.other[0] = stp->ls_stateid.other[0];
1047 ldumpp[cnt].ndlck_stateid.other[1] = stp->ls_stateid.other[1];
1048 ldumpp[cnt].ndlck_stateid.other[2] = stp->ls_stateid.other[2];
1049 ldumpp[cnt].ndlck_owner.nclid_idlen = 0;
1050 ldumpp[cnt].ndlck_clid.nclid_idlen = stp->ls_clp->lc_idlen;
1051 NFSBCOPY(stp->ls_clp->lc_id, ldumpp[cnt].ndlck_clid.nclid_id,
1052 stp->ls_clp->lc_idlen);
1053 sad=NFSSOCKADDR(stp->ls_clp->lc_req.nr_nam, struct sockaddr *);
1054 ldumpp[cnt].ndlck_addrfam = sad->sa_family;
1055 if (sad->sa_family == AF_INET) {
1056 rad = (struct sockaddr_in *)sad;
1057 ldumpp[cnt].ndlck_cbaddr.sin_addr = rad->sin_addr;
1059 rad6 = (struct sockaddr_in6 *)sad;
1060 ldumpp[cnt].ndlck_cbaddr.sin6_addr = rad6->sin6_addr;
1062 stp = LIST_NEXT(stp, ls_file);
1067 * If list isn't full, mark end of list by setting the client name
1071 ldumpp[cnt].ndlck_clid.nclid_idlen = 0;
1073 NFSLOCKV4ROOTMUTEX();
1074 nfsv4_relref(&nfsv4rootfs_lock);
1075 NFSUNLOCKV4ROOTMUTEX();
1079 * Server timer routine. It can scan any linked list, so long
1080 * as it holds the spin/mutex lock and there is no exclusive lock on
1082 * (For OpenBSD, a kthread is ok. For FreeBSD, I think it is ok
1083 * to do this from a callout, since the spin locks work. For
1084 * Darwin, I'm not sure what will work correctly yet.)
1085 * Should be called once per second.
1088 nfsrv_servertimer(void)
1090 struct nfsclient *clp, *nclp;
1091 struct nfsstate *stp, *nstp;
1095 * Make sure nfsboottime is set. This is used by V3 as well
1096 * as V4. Note that nfsboottime is not nfsrvboottime, which is
1097 * only used by the V4 server for leases.
1099 if (nfsboottime.tv_sec == 0)
1100 NFSSETBOOTTIME(nfsboottime);
1103 * If server hasn't started yet, just return.
1106 if (nfsrv_stablefirst.nsf_eograce == 0) {
1110 if (!(nfsrv_stablefirst.nsf_flags & NFSNSF_UPDATEDONE)) {
1111 if (!(nfsrv_stablefirst.nsf_flags & NFSNSF_GRACEOVER) &&
1112 NFSD_MONOSEC > nfsrv_stablefirst.nsf_eograce)
1113 nfsrv_stablefirst.nsf_flags |=
1114 (NFSNSF_GRACEOVER | NFSNSF_NEEDLOCK);
1120 * Try and get a reference count on the nfsv4rootfs_lock so that
1121 * no nfsd thread can acquire an exclusive lock on it before this
1122 * call is done. If it is already exclusively locked, just return.
1124 NFSLOCKV4ROOTMUTEX();
1125 got_ref = nfsv4_getref_nonblock(&nfsv4rootfs_lock);
1126 NFSUNLOCKV4ROOTMUTEX();
1133 * For each client...
1135 for (i = 0; i < nfsrv_clienthashsize; i++) {
1136 clp = LIST_FIRST(&nfsclienthash[i]);
1137 while (clp != LIST_END(&nfsclienthash[i])) {
1138 nclp = LIST_NEXT(clp, lc_hash);
1139 if (!(clp->lc_flags & LCL_EXPIREIT)) {
1140 if (((clp->lc_expiry + NFSRV_STALELEASE) < NFSD_MONOSEC
1141 && ((LIST_EMPTY(&clp->lc_deleg)
1142 && LIST_EMPTY(&clp->lc_open)) ||
1143 nfsrv_clients > nfsrv_clienthighwater)) ||
1144 (clp->lc_expiry + NFSRV_MOULDYLEASE) < NFSD_MONOSEC ||
1145 (clp->lc_expiry < NFSD_MONOSEC &&
1146 (nfsrv_openpluslock * 10 / 9) > nfsrv_v4statelimit)) {
1148 * Lease has expired several nfsrv_lease times ago:
1150 * - no state is associated with it
1152 * - above high water mark for number of clients
1153 * (nfsrv_clienthighwater should be large enough
1154 * that this only occurs when clients fail to
1155 * use the same nfs_client_id4.id. Maybe somewhat
1156 * higher that the maximum number of clients that
1157 * will mount this server?)
1159 * Lease has expired a very long time ago
1161 * Lease has expired PLUS the number of opens + locks
1162 * has exceeded 90% of capacity
1164 * --> Mark for expiry. The actual expiry will be done
1165 * by an nfsd sometime soon.
1167 clp->lc_flags |= LCL_EXPIREIT;
1168 nfsrv_stablefirst.nsf_flags |=
1169 (NFSNSF_NEEDLOCK | NFSNSF_EXPIREDCLIENT);
1172 * If there are no opens, increment no open tick cnt
1173 * If time exceeds NFSNOOPEN, mark it to be thrown away
1174 * otherwise, if there is an open, reset no open time
1175 * Hopefully, this will avoid excessive re-creation
1176 * of open owners and subsequent open confirms.
1178 stp = LIST_FIRST(&clp->lc_open);
1179 while (stp != LIST_END(&clp->lc_open)) {
1180 nstp = LIST_NEXT(stp, ls_list);
1181 if (LIST_EMPTY(&stp->ls_open)) {
1183 if (stp->ls_noopens > NFSNOOPEN ||
1184 (nfsrv_openpluslock * 2) >
1186 nfsrv_stablefirst.nsf_flags |=
1189 stp->ls_noopens = 0;
1199 NFSLOCKV4ROOTMUTEX();
1200 nfsv4_relref(&nfsv4rootfs_lock);
1201 NFSUNLOCKV4ROOTMUTEX();
1205 * The following set of functions free up the various data structures.
1208 * Clear out all open/lock state related to this nfsclient.
1209 * Caller must hold an exclusive lock on nfsv4rootfs_lock, so that
1210 * there are no other active nfsd threads.
1213 nfsrv_cleanclient(struct nfsclient *clp, NFSPROC_T *p)
1215 struct nfsstate *stp, *nstp;
1216 struct nfsdsession *sep, *nsep;
1218 LIST_FOREACH_SAFE(stp, &clp->lc_open, ls_list, nstp)
1219 nfsrv_freeopenowner(stp, 1, p);
1220 if ((clp->lc_flags & LCL_ADMINREVOKED) == 0)
1221 LIST_FOREACH_SAFE(sep, &clp->lc_session, sess_list, nsep)
1222 (void)nfsrv_freesession(sep, NULL);
1226 * Free a client that has been cleaned. It should also already have been
1227 * removed from the lists.
1228 * (Just to be safe w.r.t. newnfs_disconnect(), call this function when
1229 * softclock interrupts are enabled.)
1232 nfsrv_zapclient(struct nfsclient *clp, NFSPROC_T *p)
1236 if ((clp->lc_flags & (LCL_GSS | LCL_CALLBACKSON)) ==
1237 (LCL_GSS | LCL_CALLBACKSON) &&
1238 (clp->lc_hand.nfsh_flag & NFSG_COMPLETE) &&
1239 clp->lc_handlelen > 0) {
1240 clp->lc_hand.nfsh_flag &= ~NFSG_COMPLETE;
1241 clp->lc_hand.nfsh_flag |= NFSG_DESTROYED;
1242 (void) nfsrv_docallback(clp, NFSV4PROC_CBNULL,
1243 NULL, 0, NULL, NULL, NULL, p);
1246 newnfs_disconnect(&clp->lc_req);
1247 NFSSOCKADDRFREE(clp->lc_req.nr_nam);
1248 NFSFREEMUTEX(&clp->lc_req.nr_mtx);
1249 free(clp->lc_stateid, M_NFSDCLIENT);
1250 free(clp, M_NFSDCLIENT);
1252 newnfsstats.srvclients--;
1253 nfsrv_openpluslock--;
1259 * Free a list of delegation state structures.
1260 * (This function will also free all nfslockfile structures that no
1261 * longer have associated state.)
1264 nfsrv_freedeleglist(struct nfsstatehead *sthp)
1266 struct nfsstate *stp, *nstp;
1268 LIST_FOREACH_SAFE(stp, sthp, ls_list, nstp) {
1269 nfsrv_freedeleg(stp);
1275 * Free up a delegation.
1278 nfsrv_freedeleg(struct nfsstate *stp)
1280 struct nfslockfile *lfp;
1282 LIST_REMOVE(stp, ls_hash);
1283 LIST_REMOVE(stp, ls_list);
1284 LIST_REMOVE(stp, ls_file);
1285 if ((stp->ls_flags & NFSLCK_DELEGWRITE) != 0)
1286 nfsrv_writedelegcnt--;
1288 if (LIST_EMPTY(&lfp->lf_open) &&
1289 LIST_EMPTY(&lfp->lf_lock) && LIST_EMPTY(&lfp->lf_deleg) &&
1290 LIST_EMPTY(&lfp->lf_locallock) && LIST_EMPTY(&lfp->lf_rollback) &&
1291 lfp->lf_usecount == 0 &&
1292 nfsv4_testlock(&lfp->lf_locallock_lck) == 0)
1293 nfsrv_freenfslockfile(lfp);
1294 FREE((caddr_t)stp, M_NFSDSTATE);
1295 newnfsstats.srvdelegates--;
1296 nfsrv_openpluslock--;
1297 nfsrv_delegatecnt--;
1301 * This function frees an open owner and all associated opens.
1304 nfsrv_freeopenowner(struct nfsstate *stp, int cansleep, NFSPROC_T *p)
1306 struct nfsstate *nstp, *tstp;
1308 LIST_REMOVE(stp, ls_list);
1310 * Now, free all associated opens.
1312 nstp = LIST_FIRST(&stp->ls_open);
1313 while (nstp != LIST_END(&stp->ls_open)) {
1315 nstp = LIST_NEXT(nstp, ls_list);
1316 (void) nfsrv_freeopen(tstp, NULL, cansleep, p);
1319 nfsrvd_derefcache(stp->ls_op);
1320 FREE((caddr_t)stp, M_NFSDSTATE);
1321 newnfsstats.srvopenowners--;
1322 nfsrv_openpluslock--;
1326 * This function frees an open (nfsstate open structure) with all associated
1327 * lock_owners and locks. It also frees the nfslockfile structure iff there
1328 * are no other opens on the file.
1329 * Returns 1 if it free'd the nfslockfile, 0 otherwise.
1332 nfsrv_freeopen(struct nfsstate *stp, vnode_t vp, int cansleep, NFSPROC_T *p)
1334 struct nfsstate *nstp, *tstp;
1335 struct nfslockfile *lfp;
1338 LIST_REMOVE(stp, ls_hash);
1339 LIST_REMOVE(stp, ls_list);
1340 LIST_REMOVE(stp, ls_file);
1344 * Now, free all lockowners associated with this open.
1346 LIST_FOREACH_SAFE(tstp, &stp->ls_open, ls_list, nstp)
1347 nfsrv_freelockowner(tstp, vp, cansleep, p);
1350 * The nfslockfile is freed here if there are no locks
1351 * associated with the open.
1352 * If there are locks associated with the open, the
1353 * nfslockfile structure can be freed via nfsrv_freelockowner().
1354 * Acquire the state mutex to avoid races with calls to
1355 * nfsrv_getlockfile().
1359 if (lfp != NULL && LIST_EMPTY(&lfp->lf_open) &&
1360 LIST_EMPTY(&lfp->lf_deleg) && LIST_EMPTY(&lfp->lf_lock) &&
1361 LIST_EMPTY(&lfp->lf_locallock) && LIST_EMPTY(&lfp->lf_rollback) &&
1362 lfp->lf_usecount == 0 &&
1363 (cansleep != 0 || nfsv4_testlock(&lfp->lf_locallock_lck) == 0)) {
1364 nfsrv_freenfslockfile(lfp);
1370 FREE((caddr_t)stp, M_NFSDSTATE);
1371 newnfsstats.srvopens--;
1372 nfsrv_openpluslock--;
1377 * Frees a lockowner and all associated locks.
1380 nfsrv_freelockowner(struct nfsstate *stp, vnode_t vp, int cansleep,
1384 LIST_REMOVE(stp, ls_hash);
1385 LIST_REMOVE(stp, ls_list);
1386 nfsrv_freeallnfslocks(stp, vp, cansleep, p);
1388 nfsrvd_derefcache(stp->ls_op);
1389 FREE((caddr_t)stp, M_NFSDSTATE);
1390 newnfsstats.srvlockowners--;
1391 nfsrv_openpluslock--;
1395 * Free all the nfs locks on a lockowner.
1398 nfsrv_freeallnfslocks(struct nfsstate *stp, vnode_t vp, int cansleep,
1401 struct nfslock *lop, *nlop;
1402 struct nfsrollback *rlp, *nrlp;
1403 struct nfslockfile *lfp = NULL;
1406 uint64_t first, end;
1409 ASSERT_VOP_UNLOCKED(vp, "nfsrv_freeallnfslocks: vnode locked");
1410 lop = LIST_FIRST(&stp->ls_lock);
1411 while (lop != LIST_END(&stp->ls_lock)) {
1412 nlop = LIST_NEXT(lop, lo_lckowner);
1414 * Since all locks should be for the same file, lfp should
1419 else if (lfp != lop->lo_lfp)
1420 panic("allnfslocks");
1422 * If vp is NULL and cansleep != 0, a vnode must be acquired
1423 * from the file handle. This only occurs when called from
1424 * nfsrv_cleanclient().
1427 if (nfsrv_dolocallocks == 0)
1429 else if (vp == NULL && cansleep != 0) {
1430 tvp = nfsvno_getvp(&lfp->lf_fh);
1431 NFSVOPUNLOCK(tvp, 0);
1440 first = lop->lo_first;
1442 nfsrv_freenfslock(lop);
1443 nfsrv_localunlock(tvp, lfp, first, end, p);
1444 LIST_FOREACH_SAFE(rlp, &lfp->lf_rollback, rlck_list,
1446 free(rlp, M_NFSDROLLBACK);
1447 LIST_INIT(&lfp->lf_rollback);
1449 nfsrv_freenfslock(lop);
1452 if (vp == NULL && tvp != NULL)
1457 * Free an nfslock structure.
1460 nfsrv_freenfslock(struct nfslock *lop)
1463 if (lop->lo_lckfile.le_prev != NULL) {
1464 LIST_REMOVE(lop, lo_lckfile);
1465 newnfsstats.srvlocks--;
1466 nfsrv_openpluslock--;
1468 LIST_REMOVE(lop, lo_lckowner);
1469 FREE((caddr_t)lop, M_NFSDLOCK);
1473 * This function frees an nfslockfile structure.
1476 nfsrv_freenfslockfile(struct nfslockfile *lfp)
1479 LIST_REMOVE(lfp, lf_hash);
1480 FREE((caddr_t)lfp, M_NFSDLOCKFILE);
1484 * This function looks up an nfsstate structure via stateid.
1487 nfsrv_getstate(struct nfsclient *clp, nfsv4stateid_t *stateidp, __unused u_int32_t flags,
1488 struct nfsstate **stpp)
1490 struct nfsstate *stp;
1491 struct nfsstatehead *hp;
1495 hp = NFSSTATEHASH(clp, *stateidp);
1496 LIST_FOREACH(stp, hp, ls_hash) {
1497 if (!NFSBCMP(stp->ls_stateid.other, stateidp->other,
1503 * If no state id in list, return NFSERR_BADSTATEID.
1505 if (stp == LIST_END(hp)) {
1506 error = NFSERR_BADSTATEID;
1517 * This function gets an nfsstate structure via owner string.
1520 nfsrv_getowner(struct nfsstatehead *hp, struct nfsstate *new_stp,
1521 struct nfsstate **stpp)
1523 struct nfsstate *stp;
1526 LIST_FOREACH(stp, hp, ls_list) {
1527 if (new_stp->ls_ownerlen == stp->ls_ownerlen &&
1528 !NFSBCMP(new_stp->ls_owner,stp->ls_owner,stp->ls_ownerlen)) {
1536 * Lock control function called to update lock status.
1537 * Returns 0 upon success, -1 if there is no lock and the flags indicate
1538 * that one isn't to be created and an NFSERR_xxx for other errors.
1539 * The structures new_stp and new_lop are passed in as pointers that should
1540 * be set to NULL if the structure is used and shouldn't be free'd.
1541 * For the NFSLCK_TEST and NFSLCK_CHECK cases, the structures are
1542 * never used and can safely be allocated on the stack. For all other
1543 * cases, *new_stpp and *new_lopp should be malloc'd before the call,
1544 * in case they are used.
1547 nfsrv_lockctrl(vnode_t vp, struct nfsstate **new_stpp,
1548 struct nfslock **new_lopp, struct nfslockconflict *cfp,
1549 nfsquad_t clientid, nfsv4stateid_t *stateidp,
1550 __unused struct nfsexstuff *exp,
1551 struct nfsrv_descript *nd, NFSPROC_T *p)
1553 struct nfslock *lop;
1554 struct nfsstate *new_stp = *new_stpp;
1555 struct nfslock *new_lop = *new_lopp;
1556 struct nfsstate *tstp, *mystp, *nstp;
1558 struct nfslockfile *lfp;
1559 struct nfslock *other_lop = NULL;
1560 struct nfsstate *stp, *lckstp = NULL;
1561 struct nfsclient *clp = NULL;
1563 int error = 0, haslock = 0, ret, reterr;
1564 int getlckret, delegation = 0, filestruct_locked, vnode_unlocked = 0;
1566 uint64_t first, end;
1567 uint32_t lock_flags;
1569 if (new_stp->ls_flags & (NFSLCK_CHECK | NFSLCK_SETATTR)) {
1571 * Note the special cases of "all 1s" or "all 0s" stateids and
1572 * let reads with all 1s go ahead.
1574 if (new_stp->ls_stateid.seqid == 0x0 &&
1575 new_stp->ls_stateid.other[0] == 0x0 &&
1576 new_stp->ls_stateid.other[1] == 0x0 &&
1577 new_stp->ls_stateid.other[2] == 0x0)
1579 else if (new_stp->ls_stateid.seqid == 0xffffffff &&
1580 new_stp->ls_stateid.other[0] == 0xffffffff &&
1581 new_stp->ls_stateid.other[1] == 0xffffffff &&
1582 new_stp->ls_stateid.other[2] == 0xffffffff)
1587 * Check for restart conditions (client and server).
1589 error = nfsrv_checkrestart(clientid, new_stp->ls_flags,
1590 &new_stp->ls_stateid, specialid);
1595 * Check for state resource limit exceeded.
1597 if ((new_stp->ls_flags & NFSLCK_LOCK) &&
1598 nfsrv_openpluslock > nfsrv_v4statelimit) {
1599 error = NFSERR_RESOURCE;
1604 * For the lock case, get another nfslock structure,
1605 * just in case we need it.
1606 * Malloc now, before we start sifting through the linked lists,
1607 * in case we have to wait for memory.
1610 if (new_stp->ls_flags & NFSLCK_LOCK)
1611 MALLOC(other_lop, struct nfslock *, sizeof (struct nfslock),
1612 M_NFSDLOCK, M_WAITOK);
1613 filestruct_locked = 0;
1618 * Get the lockfile structure for CFH now, so we can do a sanity
1619 * check against the stateid, before incrementing the seqid#, since
1620 * we want to return NFSERR_BADSTATEID on failure and the seqid#
1621 * shouldn't be incremented for this case.
1622 * If nfsrv_getlockfile() returns -1, it means "not found", which
1623 * will be handled later.
1624 * If we are doing Lock/LockU and local locking is enabled, sleep
1625 * lock the nfslockfile structure.
1627 getlckret = nfsrv_getlockfh(vp, new_stp->ls_flags, NULL, &nfh, p);
1629 if (getlckret == 0) {
1630 if ((new_stp->ls_flags & (NFSLCK_LOCK | NFSLCK_UNLOCK)) != 0 &&
1631 nfsrv_dolocallocks != 0 && nd->nd_repstat == 0) {
1632 getlckret = nfsrv_getlockfile(new_stp->ls_flags, NULL,
1635 filestruct_locked = 1;
1637 getlckret = nfsrv_getlockfile(new_stp->ls_flags, NULL,
1640 if (getlckret != 0 && getlckret != -1)
1643 if (filestruct_locked != 0) {
1644 LIST_INIT(&lfp->lf_rollback);
1645 if ((new_stp->ls_flags & NFSLCK_LOCK)) {
1647 * For local locking, do the advisory locking now, so
1648 * that any conflict can be detected. A failure later
1649 * can be rolled back locally. If an error is returned,
1650 * struct nfslockfile has been unlocked and any local
1651 * locking rolled back.
1654 if (vnode_unlocked == 0) {
1655 ASSERT_VOP_ELOCKED(vp, "nfsrv_lockctrl1");
1657 NFSVOPUNLOCK(vp, 0);
1659 reterr = nfsrv_locallock(vp, lfp,
1660 (new_lop->lo_flags & (NFSLCK_READ | NFSLCK_WRITE)),
1661 new_lop->lo_first, new_lop->lo_end, cfp, p);
1666 if (specialid == 0) {
1667 if (new_stp->ls_flags & NFSLCK_TEST) {
1669 * RFC 3530 does not list LockT as an op that renews a
1670 * lease, but the concensus seems to be that it is ok
1671 * for a server to do so.
1673 error = nfsrv_getclient(clientid, CLOPS_RENEW, &clp, NULL,
1674 (nfsquad_t)((u_quad_t)0), 0, nd, p);
1677 * Since NFSERR_EXPIRED, NFSERR_ADMINREVOKED are not valid
1678 * error returns for LockT, just go ahead and test for a lock,
1679 * since there are no locks for this client, but other locks
1680 * can conflict. (ie. same client will always be false)
1682 if (error == NFSERR_EXPIRED || error == NFSERR_ADMINREVOKED)
1686 error = nfsrv_getclient(clientid, CLOPS_RENEW, &clp, NULL,
1687 (nfsquad_t)((u_quad_t)0), 0, nd, p);
1690 * Look up the stateid
1692 error = nfsrv_getstate(clp, &new_stp->ls_stateid,
1693 new_stp->ls_flags, &stp);
1695 * do some sanity checks for an unconfirmed open or a
1696 * stateid that refers to the wrong file, for an open stateid
1698 if (error == 0 && (stp->ls_flags & NFSLCK_OPEN) &&
1699 ((stp->ls_openowner->ls_flags & NFSLCK_NEEDSCONFIRM) ||
1700 (getlckret == 0 && stp->ls_lfp != lfp)))
1701 error = NFSERR_BADSTATEID;
1703 (stp->ls_flags & (NFSLCK_DELEGREAD | NFSLCK_DELEGWRITE)) &&
1704 getlckret == 0 && stp->ls_lfp != lfp)
1705 error = NFSERR_BADSTATEID;
1708 * If the lockowner stateid doesn't refer to the same file,
1709 * I believe that is considered ok, since some clients will
1710 * only create a single lockowner and use that for all locks
1712 * For now, log it as a diagnostic, instead of considering it
1715 if (error == 0 && (stp->ls_flags &
1716 (NFSLCK_OPEN | NFSLCK_DELEGREAD | NFSLCK_DELEGWRITE)) == 0 &&
1717 getlckret == 0 && stp->ls_lfp != lfp) {
1719 printf("Got a lock statid for different file open\n");
1722 error = NFSERR_BADSTATEID;
1727 if (new_stp->ls_flags & NFSLCK_OPENTOLOCK) {
1729 * If haslock set, we've already checked the seqid.
1732 if (stp->ls_flags & NFSLCK_OPEN)
1733 error = nfsrv_checkseqid(nd, new_stp->ls_seq,
1734 stp->ls_openowner, new_stp->ls_op);
1736 error = NFSERR_BADSTATEID;
1739 nfsrv_getowner(&stp->ls_open, new_stp, &lckstp);
1742 * I believe this should be an error, but it
1743 * isn't obvious what NFSERR_xxx would be
1744 * appropriate, so I'll use NFSERR_INVAL for now.
1746 error = NFSERR_INVAL;
1749 } else if (new_stp->ls_flags&(NFSLCK_LOCK|NFSLCK_UNLOCK)) {
1751 * If haslock set, ditto above.
1754 if (stp->ls_flags & NFSLCK_OPEN)
1755 error = NFSERR_BADSTATEID;
1757 error = nfsrv_checkseqid(nd, new_stp->ls_seq,
1758 stp, new_stp->ls_op);
1766 * If the seqid part of the stateid isn't the same, return
1767 * NFSERR_OLDSTATEID for cases other than I/O Ops.
1768 * For I/O Ops, only return NFSERR_OLDSTATEID if
1769 * nfsrv_returnoldstateid is set. (The concensus on the email
1770 * list was that most clients would prefer to not receive
1771 * NFSERR_OLDSTATEID for I/O Ops, but the RFC suggests that that
1772 * is what will happen, so I use the nfsrv_returnoldstateid to
1773 * allow for either server configuration.)
1775 if (!error && stp->ls_stateid.seqid!=new_stp->ls_stateid.seqid &&
1776 (((nd->nd_flag & ND_NFSV41) == 0 &&
1777 (!(new_stp->ls_flags & NFSLCK_CHECK) ||
1778 nfsrv_returnoldstateid)) ||
1779 ((nd->nd_flag & ND_NFSV41) != 0 &&
1780 new_stp->ls_stateid.seqid != 0)))
1781 error = NFSERR_OLDSTATEID;
1786 * Now we can check for grace.
1789 error = nfsrv_checkgrace(nd, clp, new_stp->ls_flags);
1790 if ((new_stp->ls_flags & NFSLCK_RECLAIM) && !error &&
1791 nfsrv_checkstable(clp))
1792 error = NFSERR_NOGRACE;
1794 * If we successfully Reclaimed state, note that.
1796 if ((new_stp->ls_flags & NFSLCK_RECLAIM) && !error)
1797 nfsrv_markstable(clp);
1800 * At this point, either error == NFSERR_BADSTATEID or the
1801 * seqid# has been updated, so we can return any error.
1802 * If error == 0, there may be an error in:
1803 * nd_repstat - Set by the calling function.
1804 * reterr - Set above, if getting the nfslockfile structure
1805 * or acquiring the local lock failed.
1806 * (If both of these are set, nd_repstat should probably be
1807 * returned, since that error was detected before this
1810 if (error != 0 || nd->nd_repstat != 0 || reterr != 0) {
1812 if (nd->nd_repstat != 0)
1813 error = nd->nd_repstat;
1817 if (filestruct_locked != 0) {
1818 /* Roll back local locks. */
1820 if (vnode_unlocked == 0) {
1821 ASSERT_VOP_ELOCKED(vp, "nfsrv_lockctrl2");
1823 NFSVOPUNLOCK(vp, 0);
1825 nfsrv_locallock_rollback(vp, lfp, p);
1827 nfsrv_unlocklf(lfp);
1834 * Check the nfsrv_getlockfile return.
1835 * Returned -1 if no structure found.
1837 if (getlckret == -1) {
1838 error = NFSERR_EXPIRED;
1840 * Called from lockt, so no lock is OK.
1842 if (new_stp->ls_flags & NFSLCK_TEST) {
1844 } else if (new_stp->ls_flags &
1845 (NFSLCK_CHECK | NFSLCK_SETATTR)) {
1847 * Called to check for a lock, OK if the stateid is all
1848 * 1s or all 0s, but there should be an nfsstate
1850 * (ie. If there is no open, I'll assume no share
1856 error = NFSERR_BADSTATEID;
1863 * For NFSLCK_CHECK and NFSLCK_LOCK, test for a share conflict.
1864 * For NFSLCK_CHECK, allow a read if write access is granted,
1865 * but check for a deny. For NFSLCK_LOCK, require correct access,
1866 * which implies a conflicting deny can't exist.
1868 if (new_stp->ls_flags & (NFSLCK_CHECK | NFSLCK_LOCK)) {
1870 * Four kinds of state id:
1871 * - specialid (all 0s or all 1s), only for NFSLCK_CHECK
1872 * - stateid for an open
1873 * - stateid for a delegation
1874 * - stateid for a lock owner
1877 if (stp->ls_flags & (NFSLCK_DELEGREAD | NFSLCK_DELEGWRITE)) {
1880 nfsrv_delaydelegtimeout(stp);
1881 } else if (stp->ls_flags & NFSLCK_OPEN) {
1884 mystp = stp->ls_openstp;
1887 * If locking or checking, require correct access
1890 if (((new_stp->ls_flags & NFSLCK_LOCK) &&
1891 !((new_lop->lo_flags >> NFSLCK_LOCKSHIFT) &
1892 mystp->ls_flags & NFSLCK_ACCESSBITS)) ||
1893 ((new_stp->ls_flags & (NFSLCK_CHECK|NFSLCK_READACCESS)) ==
1894 (NFSLCK_CHECK | NFSLCK_READACCESS) &&
1895 !(mystp->ls_flags & NFSLCK_READACCESS) &&
1896 nfsrv_allowreadforwriteopen == 0) ||
1897 ((new_stp->ls_flags & (NFSLCK_CHECK|NFSLCK_WRITEACCESS)) ==
1898 (NFSLCK_CHECK | NFSLCK_WRITEACCESS) &&
1899 !(mystp->ls_flags & NFSLCK_WRITEACCESS))) {
1900 if (filestruct_locked != 0) {
1901 /* Roll back local locks. */
1903 if (vnode_unlocked == 0) {
1904 ASSERT_VOP_ELOCKED(vp,
1907 NFSVOPUNLOCK(vp, 0);
1909 nfsrv_locallock_rollback(vp, lfp, p);
1911 nfsrv_unlocklf(lfp);
1914 error = NFSERR_OPENMODE;
1919 if ((new_stp->ls_flags & NFSLCK_CHECK) && !delegation) {
1921 * Check for a conflicting deny bit.
1923 LIST_FOREACH(tstp, &lfp->lf_open, ls_file) {
1924 if (tstp != mystp) {
1925 bits = tstp->ls_flags;
1926 bits >>= NFSLCK_SHIFT;
1927 if (new_stp->ls_flags & bits & NFSLCK_ACCESSBITS) {
1928 KASSERT(vnode_unlocked == 0,
1929 ("nfsrv_lockctrl: vnode unlocked1"));
1930 ret = nfsrv_clientconflict(tstp->ls_clp, &haslock,
1934 * nfsrv_clientconflict unlocks state
1935 * when it returns non-zero.
1943 error = NFSERR_PERM;
1945 error = NFSERR_OPENMODE;
1951 /* We're outta here */
1958 * For setattr, just get rid of all the Delegations for other clients.
1960 if (new_stp->ls_flags & NFSLCK_SETATTR) {
1961 KASSERT(vnode_unlocked == 0,
1962 ("nfsrv_lockctrl: vnode unlocked2"));
1963 ret = nfsrv_cleandeleg(vp, lfp, clp, &haslock, p);
1966 * nfsrv_cleandeleg() unlocks state when it
1976 if (!(new_stp->ls_flags & NFSLCK_CHECK) ||
1977 (LIST_EMPTY(&lfp->lf_open) && LIST_EMPTY(&lfp->lf_lock) &&
1978 LIST_EMPTY(&lfp->lf_deleg))) {
1985 * Check for a conflicting delegation. If one is found, call
1986 * nfsrv_delegconflict() to handle it. If the v4root lock hasn't
1987 * been set yet, it will get the lock. Otherwise, it will recall
1988 * the delegation. Then, we try try again...
1989 * I currently believe the conflict algorithm to be:
1990 * For Lock Ops (Lock/LockT/LockU)
1991 * - there is a conflict iff a different client has a write delegation
1992 * For Reading (Read Op)
1993 * - there is a conflict iff a different client has a write delegation
1994 * (the specialids are always a different client)
1995 * For Writing (Write/Setattr of size)
1996 * - there is a conflict if a different client has any delegation
1997 * - there is a conflict if the same client has a read delegation
1998 * (I don't understand why this isn't allowed, but that seems to be
1999 * the current concensus?)
2001 tstp = LIST_FIRST(&lfp->lf_deleg);
2002 while (tstp != LIST_END(&lfp->lf_deleg)) {
2003 nstp = LIST_NEXT(tstp, ls_file);
2004 if ((((new_stp->ls_flags&(NFSLCK_LOCK|NFSLCK_UNLOCK|NFSLCK_TEST))||
2005 ((new_stp->ls_flags & NFSLCK_CHECK) &&
2006 (new_lop->lo_flags & NFSLCK_READ))) &&
2007 clp != tstp->ls_clp &&
2008 (tstp->ls_flags & NFSLCK_DELEGWRITE)) ||
2009 ((new_stp->ls_flags & NFSLCK_CHECK) &&
2010 (new_lop->lo_flags & NFSLCK_WRITE) &&
2011 (clp != tstp->ls_clp ||
2012 (tstp->ls_flags & NFSLCK_DELEGREAD)))) {
2014 if (filestruct_locked != 0) {
2015 /* Roll back local locks. */
2017 if (vnode_unlocked == 0) {
2018 ASSERT_VOP_ELOCKED(vp, "nfsrv_lockctrl4");
2019 NFSVOPUNLOCK(vp, 0);
2021 nfsrv_locallock_rollback(vp, lfp, p);
2023 nfsrv_unlocklf(lfp);
2025 NFSVOPLOCK(vp, LK_EXCLUSIVE | LK_RETRY);
2027 if ((vp->v_iflag & VI_DOOMED) != 0)
2028 ret = NFSERR_SERVERFAULT;
2032 ret = nfsrv_delegconflict(tstp, &haslock, p, vp);
2035 * nfsrv_delegconflict unlocks state when it
2036 * returns non-zero, which it always does.
2039 FREE((caddr_t)other_lop, M_NFSDLOCK);
2049 /* Never gets here. */
2055 * Handle the unlock case by calling nfsrv_updatelock().
2056 * (Should I have done some access checking above for unlock? For now,
2057 * just let it happen.)
2059 if (new_stp->ls_flags & NFSLCK_UNLOCK) {
2060 first = new_lop->lo_first;
2061 end = new_lop->lo_end;
2062 nfsrv_updatelock(stp, new_lopp, &other_lop, lfp);
2063 stateidp->seqid = ++(stp->ls_stateid.seqid);
2064 if ((nd->nd_flag & ND_NFSV41) != 0 && stateidp->seqid == 0)
2065 stateidp->seqid = stp->ls_stateid.seqid = 1;
2066 stateidp->other[0] = stp->ls_stateid.other[0];
2067 stateidp->other[1] = stp->ls_stateid.other[1];
2068 stateidp->other[2] = stp->ls_stateid.other[2];
2069 if (filestruct_locked != 0) {
2071 if (vnode_unlocked == 0) {
2072 ASSERT_VOP_ELOCKED(vp, "nfsrv_lockctrl5");
2074 NFSVOPUNLOCK(vp, 0);
2076 /* Update the local locks. */
2077 nfsrv_localunlock(vp, lfp, first, end, p);
2079 nfsrv_unlocklf(lfp);
2086 * Search for a conflicting lock. A lock conflicts if:
2087 * - the lock range overlaps and
2088 * - at least one lock is a write lock and
2089 * - it is not owned by the same lock owner
2092 LIST_FOREACH(lop, &lfp->lf_lock, lo_lckfile) {
2093 if (new_lop->lo_end > lop->lo_first &&
2094 new_lop->lo_first < lop->lo_end &&
2095 (new_lop->lo_flags == NFSLCK_WRITE ||
2096 lop->lo_flags == NFSLCK_WRITE) &&
2097 lckstp != lop->lo_stp &&
2098 (clp != lop->lo_stp->ls_clp ||
2099 lckstp->ls_ownerlen != lop->lo_stp->ls_ownerlen ||
2100 NFSBCMP(lckstp->ls_owner, lop->lo_stp->ls_owner,
2101 lckstp->ls_ownerlen))) {
2103 FREE((caddr_t)other_lop, M_NFSDLOCK);
2106 if (vnode_unlocked != 0)
2107 ret = nfsrv_clientconflict(lop->lo_stp->ls_clp, &haslock,
2110 ret = nfsrv_clientconflict(lop->lo_stp->ls_clp, &haslock,
2113 if (filestruct_locked != 0) {
2114 if (vnode_unlocked == 0) {
2115 ASSERT_VOP_ELOCKED(vp, "nfsrv_lockctrl6");
2116 NFSVOPUNLOCK(vp, 0);
2118 /* Roll back local locks. */
2119 nfsrv_locallock_rollback(vp, lfp, p);
2121 nfsrv_unlocklf(lfp);
2123 NFSVOPLOCK(vp, LK_EXCLUSIVE | LK_RETRY);
2125 if ((vp->v_iflag & VI_DOOMED) != 0) {
2126 error = NFSERR_SERVERFAULT;
2131 * nfsrv_clientconflict() unlocks state when it
2138 * Found a conflicting lock, so record the conflict and
2141 if (cfp != NULL && ret == 0) {
2142 cfp->cl_clientid.lval[0]=lop->lo_stp->ls_stateid.other[0];
2143 cfp->cl_clientid.lval[1]=lop->lo_stp->ls_stateid.other[1];
2144 cfp->cl_first = lop->lo_first;
2145 cfp->cl_end = lop->lo_end;
2146 cfp->cl_flags = lop->lo_flags;
2147 cfp->cl_ownerlen = lop->lo_stp->ls_ownerlen;
2148 NFSBCOPY(lop->lo_stp->ls_owner, cfp->cl_owner,
2152 error = NFSERR_PERM;
2153 else if (new_stp->ls_flags & NFSLCK_RECLAIM)
2154 error = NFSERR_RECLAIMCONFLICT;
2155 else if (new_stp->ls_flags & NFSLCK_CHECK)
2156 error = NFSERR_LOCKED;
2158 error = NFSERR_DENIED;
2159 if (filestruct_locked != 0 && ret == 0) {
2160 /* Roll back local locks. */
2162 if (vnode_unlocked == 0) {
2163 ASSERT_VOP_ELOCKED(vp, "nfsrv_lockctrl7");
2165 NFSVOPUNLOCK(vp, 0);
2167 nfsrv_locallock_rollback(vp, lfp, p);
2169 nfsrv_unlocklf(lfp);
2179 * We only get here if there was no lock that conflicted.
2181 if (new_stp->ls_flags & (NFSLCK_TEST | NFSLCK_CHECK)) {
2187 * We only get here when we are creating or modifying a lock.
2188 * There are two variants:
2189 * - exist_lock_owner where lock_owner exists
2190 * - open_to_lock_owner with new lock_owner
2192 first = new_lop->lo_first;
2193 end = new_lop->lo_end;
2194 lock_flags = new_lop->lo_flags;
2195 if (!(new_stp->ls_flags & NFSLCK_OPENTOLOCK)) {
2196 nfsrv_updatelock(lckstp, new_lopp, &other_lop, lfp);
2197 stateidp->seqid = ++(lckstp->ls_stateid.seqid);
2198 if ((nd->nd_flag & ND_NFSV41) != 0 && stateidp->seqid == 0)
2199 stateidp->seqid = lckstp->ls_stateid.seqid = 1;
2200 stateidp->other[0] = lckstp->ls_stateid.other[0];
2201 stateidp->other[1] = lckstp->ls_stateid.other[1];
2202 stateidp->other[2] = lckstp->ls_stateid.other[2];
2205 * The new open_to_lock_owner case.
2206 * Link the new nfsstate into the lists.
2208 new_stp->ls_seq = new_stp->ls_opentolockseq;
2209 nfsrvd_refcache(new_stp->ls_op);
2210 stateidp->seqid = new_stp->ls_stateid.seqid = 1;
2211 stateidp->other[0] = new_stp->ls_stateid.other[0] =
2212 clp->lc_clientid.lval[0];
2213 stateidp->other[1] = new_stp->ls_stateid.other[1] =
2214 clp->lc_clientid.lval[1];
2215 stateidp->other[2] = new_stp->ls_stateid.other[2] =
2216 nfsrv_nextstateindex(clp);
2217 new_stp->ls_clp = clp;
2218 LIST_INIT(&new_stp->ls_lock);
2219 new_stp->ls_openstp = stp;
2220 new_stp->ls_lfp = lfp;
2221 nfsrv_insertlock(new_lop, (struct nfslock *)new_stp, new_stp,
2223 LIST_INSERT_HEAD(NFSSTATEHASH(clp, new_stp->ls_stateid),
2225 LIST_INSERT_HEAD(&stp->ls_open, new_stp, ls_list);
2228 newnfsstats.srvlockowners++;
2229 nfsrv_openpluslock++;
2231 if (filestruct_locked != 0) {
2233 nfsrv_locallock_commit(lfp, lock_flags, first, end);
2235 nfsrv_unlocklf(lfp);
2241 NFSLOCKV4ROOTMUTEX();
2242 nfsv4_unlock(&nfsv4rootfs_lock, 1);
2243 NFSUNLOCKV4ROOTMUTEX();
2245 if (vnode_unlocked != 0) {
2246 NFSVOPLOCK(vp, LK_EXCLUSIVE | LK_RETRY);
2247 if (error == 0 && (vp->v_iflag & VI_DOOMED) != 0)
2248 error = NFSERR_SERVERFAULT;
2251 FREE((caddr_t)other_lop, M_NFSDLOCK);
2252 NFSEXITCODE2(error, nd);
2257 * Check for state errors for Open.
2258 * repstat is passed back out as an error if more critical errors
2262 nfsrv_opencheck(nfsquad_t clientid, nfsv4stateid_t *stateidp,
2263 struct nfsstate *new_stp, vnode_t vp, struct nfsrv_descript *nd,
2264 NFSPROC_T *p, int repstat)
2266 struct nfsstate *stp, *nstp;
2267 struct nfsclient *clp;
2268 struct nfsstate *ownerstp;
2269 struct nfslockfile *lfp, *new_lfp;
2270 int error = 0, haslock = 0, ret, readonly = 0, getfhret = 0;
2272 if ((new_stp->ls_flags & NFSLCK_SHAREBITS) == NFSLCK_READACCESS)
2275 * Check for restart conditions (client and server).
2277 error = nfsrv_checkrestart(clientid, new_stp->ls_flags,
2278 &new_stp->ls_stateid, 0);
2283 * Check for state resource limit exceeded.
2284 * Technically this should be SMP protected, but the worst
2285 * case error is "out by one or two" on the count when it
2286 * returns NFSERR_RESOURCE and the limit is just a rather
2287 * arbitrary high water mark, so no harm is done.
2289 if (nfsrv_openpluslock > nfsrv_v4statelimit) {
2290 error = NFSERR_RESOURCE;
2295 MALLOC(new_lfp, struct nfslockfile *, sizeof (struct nfslockfile),
2296 M_NFSDLOCKFILE, M_WAITOK);
2298 getfhret = nfsrv_getlockfh(vp, new_stp->ls_flags, new_lfp,
2302 * Get the nfsclient structure.
2304 error = nfsrv_getclient(clientid, CLOPS_RENEW, &clp, NULL,
2305 (nfsquad_t)((u_quad_t)0), 0, nd, p);
2308 * Look up the open owner. See if it needs confirmation and
2309 * check the seq#, as required.
2312 nfsrv_getowner(&clp->lc_open, new_stp, &ownerstp);
2314 if (!error && ownerstp) {
2315 error = nfsrv_checkseqid(nd, new_stp->ls_seq, ownerstp,
2318 * If the OpenOwner hasn't been confirmed, assume the
2319 * old one was a replay and this one is ok.
2320 * See: RFC3530 Sec. 14.2.18.
2322 if (error == NFSERR_BADSEQID &&
2323 (ownerstp->ls_flags & NFSLCK_NEEDSCONFIRM))
2331 error = nfsrv_checkgrace(nd, clp, new_stp->ls_flags);
2332 if ((new_stp->ls_flags & NFSLCK_RECLAIM) && !error &&
2333 nfsrv_checkstable(clp))
2334 error = NFSERR_NOGRACE;
2337 * If none of the above errors occurred, let repstat be
2340 if (repstat && !error)
2345 NFSLOCKV4ROOTMUTEX();
2346 nfsv4_unlock(&nfsv4rootfs_lock, 1);
2347 NFSUNLOCKV4ROOTMUTEX();
2349 free((caddr_t)new_lfp, M_NFSDLOCKFILE);
2354 * If vp == NULL, the file doesn't exist yet, so return ok.
2355 * (This always happens on the first pass, so haslock must be 0.)
2359 FREE((caddr_t)new_lfp, M_NFSDLOCKFILE);
2364 * Get the structure for the underlying file.
2369 error = nfsrv_getlockfile(new_stp->ls_flags, &new_lfp, &lfp,
2372 FREE((caddr_t)new_lfp, M_NFSDLOCKFILE);
2376 NFSLOCKV4ROOTMUTEX();
2377 nfsv4_unlock(&nfsv4rootfs_lock, 1);
2378 NFSUNLOCKV4ROOTMUTEX();
2384 * Search for a conflicting open/share.
2386 if (new_stp->ls_flags & NFSLCK_DELEGCUR) {
2388 * For Delegate_Cur, search for the matching Delegation,
2389 * which indicates no conflict.
2390 * An old delegation should have been recovered by the
2391 * client doing a Claim_DELEGATE_Prev, so I won't let
2392 * it match and return NFSERR_EXPIRED. Should I let it
2395 LIST_FOREACH(stp, &lfp->lf_deleg, ls_file) {
2396 if (!(stp->ls_flags & NFSLCK_OLDDELEG) &&
2397 (((nd->nd_flag & ND_NFSV41) != 0 &&
2398 stateidp->seqid == 0) ||
2399 stateidp->seqid == stp->ls_stateid.seqid) &&
2400 !NFSBCMP(stateidp->other, stp->ls_stateid.other,
2404 if (stp == LIST_END(&lfp->lf_deleg) ||
2405 ((new_stp->ls_flags & NFSLCK_WRITEACCESS) &&
2406 (stp->ls_flags & NFSLCK_DELEGREAD))) {
2409 NFSLOCKV4ROOTMUTEX();
2410 nfsv4_unlock(&nfsv4rootfs_lock, 1);
2411 NFSUNLOCKV4ROOTMUTEX();
2413 error = NFSERR_EXPIRED;
2419 * Check for access/deny bit conflicts. I check for the same
2420 * owner as well, in case the client didn't bother.
2422 LIST_FOREACH(stp, &lfp->lf_open, ls_file) {
2423 if (!(new_stp->ls_flags & NFSLCK_DELEGCUR) &&
2424 (((new_stp->ls_flags & NFSLCK_ACCESSBITS) &
2425 ((stp->ls_flags>>NFSLCK_SHIFT) & NFSLCK_ACCESSBITS))||
2426 ((stp->ls_flags & NFSLCK_ACCESSBITS) &
2427 ((new_stp->ls_flags>>NFSLCK_SHIFT)&NFSLCK_ACCESSBITS)))){
2428 ret = nfsrv_clientconflict(stp->ls_clp,&haslock,vp,p);
2431 * nfsrv_clientconflict() unlocks
2432 * state when it returns non-zero.
2437 error = NFSERR_PERM;
2438 else if (new_stp->ls_flags & NFSLCK_RECLAIM)
2439 error = NFSERR_RECLAIMCONFLICT;
2441 error = NFSERR_SHAREDENIED;
2445 NFSLOCKV4ROOTMUTEX();
2446 nfsv4_unlock(&nfsv4rootfs_lock, 1);
2447 NFSUNLOCKV4ROOTMUTEX();
2454 * Check for a conflicting delegation. If one is found, call
2455 * nfsrv_delegconflict() to handle it. If the v4root lock hasn't
2456 * been set yet, it will get the lock. Otherwise, it will recall
2457 * the delegation. Then, we try try again...
2458 * (If NFSLCK_DELEGCUR is set, it has a delegation, so there
2459 * isn't a conflict.)
2460 * I currently believe the conflict algorithm to be:
2461 * For Open with Read Access and Deny None
2462 * - there is a conflict iff a different client has a write delegation
2463 * For Open with other Write Access or any Deny except None
2464 * - there is a conflict if a different client has any delegation
2465 * - there is a conflict if the same client has a read delegation
2466 * (The current concensus is that this last case should be
2467 * considered a conflict since the client with a read delegation
2468 * could have done an Open with ReadAccess and WriteDeny
2469 * locally and then not have checked for the WriteDeny.)
2470 * Don't check for a Reclaim, since that will be dealt with
2471 * by nfsrv_openctrl().
2473 if (!(new_stp->ls_flags &
2474 (NFSLCK_DELEGPREV | NFSLCK_DELEGCUR | NFSLCK_RECLAIM))) {
2475 stp = LIST_FIRST(&lfp->lf_deleg);
2476 while (stp != LIST_END(&lfp->lf_deleg)) {
2477 nstp = LIST_NEXT(stp, ls_file);
2478 if ((readonly && stp->ls_clp != clp &&
2479 (stp->ls_flags & NFSLCK_DELEGWRITE)) ||
2480 (!readonly && (stp->ls_clp != clp ||
2481 (stp->ls_flags & NFSLCK_DELEGREAD)))) {
2482 ret = nfsrv_delegconflict(stp, &haslock, p, vp);
2485 * nfsrv_delegconflict() unlocks state
2486 * when it returns non-zero.
2499 NFSLOCKV4ROOTMUTEX();
2500 nfsv4_unlock(&nfsv4rootfs_lock, 1);
2501 NFSUNLOCKV4ROOTMUTEX();
2505 NFSEXITCODE2(error, nd);
2510 * Open control function to create/update open state for an open.
2513 nfsrv_openctrl(struct nfsrv_descript *nd, vnode_t vp,
2514 struct nfsstate **new_stpp, nfsquad_t clientid, nfsv4stateid_t *stateidp,
2515 nfsv4stateid_t *delegstateidp, u_int32_t *rflagsp, struct nfsexstuff *exp,
2516 NFSPROC_T *p, u_quad_t filerev)
2518 struct nfsstate *new_stp = *new_stpp;
2519 struct nfsstate *stp, *nstp;
2520 struct nfsstate *openstp = NULL, *new_open, *ownerstp, *new_deleg;
2521 struct nfslockfile *lfp, *new_lfp;
2522 struct nfsclient *clp;
2523 int error = 0, haslock = 0, ret, delegate = 1, writedeleg = 1;
2524 int readonly = 0, cbret = 1, getfhret = 0;
2525 int gotstate = 0, len = 0;
2526 u_char *clidp = NULL;
2528 if ((new_stp->ls_flags & NFSLCK_SHAREBITS) == NFSLCK_READACCESS)
2531 * Check for restart conditions (client and server).
2532 * (Paranoia, should have been detected by nfsrv_opencheck().)
2533 * If an error does show up, return NFSERR_EXPIRED, since the
2534 * the seqid# has already been incremented.
2536 error = nfsrv_checkrestart(clientid, new_stp->ls_flags,
2537 &new_stp->ls_stateid, 0);
2539 printf("Nfsd: openctrl unexpected restart err=%d\n",
2541 error = NFSERR_EXPIRED;
2545 clidp = malloc(NFSV4_OPAQUELIMIT, M_TEMP, M_WAITOK);
2547 MALLOC(new_lfp, struct nfslockfile *, sizeof (struct nfslockfile),
2548 M_NFSDLOCKFILE, M_WAITOK);
2549 MALLOC(new_open, struct nfsstate *, sizeof (struct nfsstate),
2550 M_NFSDSTATE, M_WAITOK);
2551 MALLOC(new_deleg, struct nfsstate *, sizeof (struct nfsstate),
2552 M_NFSDSTATE, M_WAITOK);
2553 getfhret = nfsrv_getlockfh(vp, new_stp->ls_flags, new_lfp,
2557 * Get the client structure. Since the linked lists could be changed
2558 * by other nfsd processes if this process does a tsleep(), one of
2559 * two things must be done.
2560 * 1 - don't tsleep()
2562 * 2 - get the nfsv4_lock() { indicated by haslock == 1 }
2563 * before using the lists, since this lock stops the other
2564 * nfsd. This should only be used for rare cases, since it
2565 * essentially single threads the nfsd.
2566 * At this time, it is only done for cases where the stable
2567 * storage file must be written prior to completion of state
2570 error = nfsrv_getclient(clientid, CLOPS_RENEW, &clp, NULL,
2571 (nfsquad_t)((u_quad_t)0), 0, nd, p);
2572 if (!error && (clp->lc_flags & LCL_NEEDSCBNULL) &&
2575 * This happens on the first open for a client
2576 * that supports callbacks.
2580 * Although nfsrv_docallback() will sleep, clp won't
2581 * go away, since they are only removed when the
2582 * nfsv4_lock() has blocked the nfsd threads. The
2583 * fields in clp can change, but having multiple
2584 * threads do this Null callback RPC should be
2587 cbret = nfsrv_docallback(clp, NFSV4PROC_CBNULL,
2588 NULL, 0, NULL, NULL, NULL, p);
2590 clp->lc_flags &= ~LCL_NEEDSCBNULL;
2592 clp->lc_flags |= LCL_CALLBACKSON;
2596 * Look up the open owner. See if it needs confirmation and
2597 * check the seq#, as required.
2600 nfsrv_getowner(&clp->lc_open, new_stp, &ownerstp);
2604 printf("Nfsd: openctrl unexpected state err=%d\n",
2606 free((caddr_t)new_lfp, M_NFSDLOCKFILE);
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();
2614 error = NFSERR_EXPIRED;
2618 if (new_stp->ls_flags & NFSLCK_RECLAIM)
2619 nfsrv_markstable(clp);
2622 * Get the structure for the underlying file.
2627 error = nfsrv_getlockfile(new_stp->ls_flags, &new_lfp, &lfp,
2630 FREE((caddr_t)new_lfp, M_NFSDLOCKFILE);
2633 printf("Nfsd openctrl unexpected getlockfile err=%d\n",
2635 free((caddr_t)new_open, M_NFSDSTATE);
2636 free((caddr_t)new_deleg, M_NFSDSTATE);
2638 NFSLOCKV4ROOTMUTEX();
2639 nfsv4_unlock(&nfsv4rootfs_lock, 1);
2640 NFSUNLOCKV4ROOTMUTEX();
2646 * Search for a conflicting open/share.
2648 if (new_stp->ls_flags & NFSLCK_DELEGCUR) {
2650 * For Delegate_Cur, search for the matching Delegation,
2651 * which indicates no conflict.
2652 * An old delegation should have been recovered by the
2653 * client doing a Claim_DELEGATE_Prev, so I won't let
2654 * it match and return NFSERR_EXPIRED. Should I let it
2657 LIST_FOREACH(stp, &lfp->lf_deleg, ls_file) {
2658 if (!(stp->ls_flags & NFSLCK_OLDDELEG) &&
2659 (((nd->nd_flag & ND_NFSV41) != 0 &&
2660 stateidp->seqid == 0) ||
2661 stateidp->seqid == stp->ls_stateid.seqid) &&
2662 !NFSBCMP(stateidp->other, stp->ls_stateid.other,
2666 if (stp == LIST_END(&lfp->lf_deleg) ||
2667 ((new_stp->ls_flags & NFSLCK_WRITEACCESS) &&
2668 (stp->ls_flags & NFSLCK_DELEGREAD))) {
2670 printf("Nfsd openctrl unexpected expiry\n");
2671 free((caddr_t)new_open, M_NFSDSTATE);
2672 free((caddr_t)new_deleg, M_NFSDSTATE);
2674 NFSLOCKV4ROOTMUTEX();
2675 nfsv4_unlock(&nfsv4rootfs_lock, 1);
2676 NFSUNLOCKV4ROOTMUTEX();
2678 error = NFSERR_EXPIRED;
2683 * Don't issue a Delegation, since one already exists and
2684 * delay delegation timeout, as required.
2687 nfsrv_delaydelegtimeout(stp);
2691 * Check for access/deny bit conflicts. I also check for the
2692 * same owner, since the client might not have bothered to check.
2693 * Also, note an open for the same file and owner, if found,
2694 * which is all we do here for Delegate_Cur, since conflict
2695 * checking is already done.
2697 LIST_FOREACH(stp, &lfp->lf_open, ls_file) {
2698 if (ownerstp && stp->ls_openowner == ownerstp)
2700 if (!(new_stp->ls_flags & NFSLCK_DELEGCUR)) {
2702 * If another client has the file open, the only
2703 * delegation that can be issued is a Read delegation
2704 * and only if it is a Read open with Deny none.
2706 if (clp != stp->ls_clp) {
2707 if ((stp->ls_flags & NFSLCK_SHAREBITS) ==
2713 if(((new_stp->ls_flags & NFSLCK_ACCESSBITS) &
2714 ((stp->ls_flags>>NFSLCK_SHIFT) & NFSLCK_ACCESSBITS))||
2715 ((stp->ls_flags & NFSLCK_ACCESSBITS) &
2716 ((new_stp->ls_flags>>NFSLCK_SHIFT)&NFSLCK_ACCESSBITS))){
2717 ret = nfsrv_clientconflict(stp->ls_clp,&haslock,vp,p);
2720 * nfsrv_clientconflict() unlocks state
2721 * when it returns non-zero.
2723 free((caddr_t)new_open, M_NFSDSTATE);
2724 free((caddr_t)new_deleg, M_NFSDSTATE);
2729 error = NFSERR_PERM;
2730 else if (new_stp->ls_flags & NFSLCK_RECLAIM)
2731 error = NFSERR_RECLAIMCONFLICT;
2733 error = NFSERR_SHAREDENIED;
2737 NFSLOCKV4ROOTMUTEX();
2738 nfsv4_unlock(&nfsv4rootfs_lock, 1);
2739 NFSUNLOCKV4ROOTMUTEX();
2741 free((caddr_t)new_open, M_NFSDSTATE);
2742 free((caddr_t)new_deleg, M_NFSDSTATE);
2743 printf("nfsd openctrl unexpected client cnfl\n");
2750 * Check for a conflicting delegation. If one is found, call
2751 * nfsrv_delegconflict() to handle it. If the v4root lock hasn't
2752 * been set yet, it will get the lock. Otherwise, it will recall
2753 * the delegation. Then, we try try again...
2754 * (If NFSLCK_DELEGCUR is set, it has a delegation, so there
2755 * isn't a conflict.)
2756 * I currently believe the conflict algorithm to be:
2757 * For Open with Read Access and Deny None
2758 * - there is a conflict iff a different client has a write delegation
2759 * For Open with other Write Access or any Deny except None
2760 * - there is a conflict if a different client has any delegation
2761 * - there is a conflict if the same client has a read delegation
2762 * (The current concensus is that this last case should be
2763 * considered a conflict since the client with a read delegation
2764 * could have done an Open with ReadAccess and WriteDeny
2765 * locally and then not have checked for the WriteDeny.)
2767 if (!(new_stp->ls_flags & (NFSLCK_DELEGPREV | NFSLCK_DELEGCUR))) {
2768 stp = LIST_FIRST(&lfp->lf_deleg);
2769 while (stp != LIST_END(&lfp->lf_deleg)) {
2770 nstp = LIST_NEXT(stp, ls_file);
2771 if (stp->ls_clp != clp && (stp->ls_flags & NFSLCK_DELEGREAD))
2775 if ((readonly && stp->ls_clp != clp &&
2776 (stp->ls_flags & NFSLCK_DELEGWRITE)) ||
2777 (!readonly && (stp->ls_clp != clp ||
2778 (stp->ls_flags & NFSLCK_DELEGREAD)))) {
2779 if (new_stp->ls_flags & NFSLCK_RECLAIM) {
2782 ret = nfsrv_delegconflict(stp, &haslock, p, vp);
2785 * nfsrv_delegconflict() unlocks state
2786 * when it returns non-zero.
2788 printf("Nfsd openctrl unexpected deleg cnfl\n");
2789 free((caddr_t)new_open, M_NFSDSTATE);
2790 free((caddr_t)new_deleg, M_NFSDSTATE);
2805 * We only get here if there was no open that conflicted.
2806 * If an open for the owner exists, or in the access/deny bits.
2807 * Otherwise it is a new open. If the open_owner hasn't been
2808 * confirmed, replace the open with the new one needing confirmation,
2809 * otherwise add the open.
2811 if (new_stp->ls_flags & NFSLCK_DELEGPREV) {
2813 * Handle NFSLCK_DELEGPREV by searching the old delegations for
2814 * a match. If found, just move the old delegation to the current
2815 * delegation list and issue open. If not found, return
2818 LIST_FOREACH(stp, &clp->lc_olddeleg, ls_list) {
2819 if (stp->ls_lfp == lfp) {
2821 if (stp->ls_clp != clp)
2822 panic("olddeleg clp");
2823 LIST_REMOVE(stp, ls_list);
2824 LIST_REMOVE(stp, ls_hash);
2825 stp->ls_flags &= ~NFSLCK_OLDDELEG;
2826 stp->ls_stateid.seqid = delegstateidp->seqid = 1;
2827 stp->ls_stateid.other[0] = delegstateidp->other[0] =
2828 clp->lc_clientid.lval[0];
2829 stp->ls_stateid.other[1] = delegstateidp->other[1] =
2830 clp->lc_clientid.lval[1];
2831 stp->ls_stateid.other[2] = delegstateidp->other[2] =
2832 nfsrv_nextstateindex(clp);
2833 stp->ls_compref = nd->nd_compref;
2834 LIST_INSERT_HEAD(&clp->lc_deleg, stp, ls_list);
2835 LIST_INSERT_HEAD(NFSSTATEHASH(clp,
2836 stp->ls_stateid), stp, ls_hash);
2837 if (stp->ls_flags & NFSLCK_DELEGWRITE)
2838 *rflagsp |= NFSV4OPEN_WRITEDELEGATE;
2840 *rflagsp |= NFSV4OPEN_READDELEGATE;
2841 clp->lc_delegtime = NFSD_MONOSEC +
2842 nfsrv_lease + NFSRV_LEASEDELTA;
2845 * Now, do the associated open.
2847 new_open->ls_stateid.seqid = 1;
2848 new_open->ls_stateid.other[0] = clp->lc_clientid.lval[0];
2849 new_open->ls_stateid.other[1] = clp->lc_clientid.lval[1];
2850 new_open->ls_stateid.other[2] = nfsrv_nextstateindex(clp);
2851 new_open->ls_flags = (new_stp->ls_flags&NFSLCK_DENYBITS)|
2853 if (stp->ls_flags & NFSLCK_DELEGWRITE)
2854 new_open->ls_flags |= (NFSLCK_READACCESS |
2855 NFSLCK_WRITEACCESS);
2857 new_open->ls_flags |= NFSLCK_READACCESS;
2858 new_open->ls_uid = new_stp->ls_uid;
2859 new_open->ls_lfp = lfp;
2860 new_open->ls_clp = clp;
2861 LIST_INIT(&new_open->ls_open);
2862 LIST_INSERT_HEAD(&lfp->lf_open, new_open, ls_file);
2863 LIST_INSERT_HEAD(NFSSTATEHASH(clp, new_open->ls_stateid),
2866 * and handle the open owner
2869 new_open->ls_openowner = ownerstp;
2870 LIST_INSERT_HEAD(&ownerstp->ls_open,new_open,ls_list);
2872 new_open->ls_openowner = new_stp;
2873 new_stp->ls_flags = 0;
2874 nfsrvd_refcache(new_stp->ls_op);
2875 new_stp->ls_noopens = 0;
2876 LIST_INIT(&new_stp->ls_open);
2877 LIST_INSERT_HEAD(&new_stp->ls_open, new_open, ls_list);
2878 LIST_INSERT_HEAD(&clp->lc_open, new_stp, ls_list);
2880 newnfsstats.srvopenowners++;
2881 nfsrv_openpluslock++;
2885 newnfsstats.srvopens++;
2886 nfsrv_openpluslock++;
2890 if (stp == LIST_END(&clp->lc_olddeleg))
2891 error = NFSERR_EXPIRED;
2892 } else if (new_stp->ls_flags & (NFSLCK_DELEGREAD | NFSLCK_DELEGWRITE)) {
2894 * Scan to see that no delegation for this client and file
2895 * doesn't already exist.
2896 * There also shouldn't yet be an Open for this file and
2899 LIST_FOREACH(stp, &lfp->lf_deleg, ls_file) {
2900 if (stp->ls_clp == clp)
2903 if (stp == LIST_END(&lfp->lf_deleg) && openstp == NULL) {
2905 * This is the Claim_Previous case with a delegation
2906 * type != Delegate_None.
2909 * First, add the delegation. (Although we must issue the
2910 * delegation, we can also ask for an immediate return.)
2912 new_deleg->ls_stateid.seqid = delegstateidp->seqid = 1;
2913 new_deleg->ls_stateid.other[0] = delegstateidp->other[0] =
2914 clp->lc_clientid.lval[0];
2915 new_deleg->ls_stateid.other[1] = delegstateidp->other[1] =
2916 clp->lc_clientid.lval[1];
2917 new_deleg->ls_stateid.other[2] = delegstateidp->other[2] =
2918 nfsrv_nextstateindex(clp);
2919 if (new_stp->ls_flags & NFSLCK_DELEGWRITE) {
2920 new_deleg->ls_flags = (NFSLCK_DELEGWRITE |
2921 NFSLCK_READACCESS | NFSLCK_WRITEACCESS);
2922 *rflagsp |= NFSV4OPEN_WRITEDELEGATE;
2923 nfsrv_writedelegcnt++;
2925 new_deleg->ls_flags = (NFSLCK_DELEGREAD |
2927 *rflagsp |= NFSV4OPEN_READDELEGATE;
2929 new_deleg->ls_uid = new_stp->ls_uid;
2930 new_deleg->ls_lfp = lfp;
2931 new_deleg->ls_clp = clp;
2932 new_deleg->ls_filerev = filerev;
2933 new_deleg->ls_compref = nd->nd_compref;
2934 LIST_INSERT_HEAD(&lfp->lf_deleg, new_deleg, ls_file);
2935 LIST_INSERT_HEAD(NFSSTATEHASH(clp,
2936 new_deleg->ls_stateid), new_deleg, ls_hash);
2937 LIST_INSERT_HEAD(&clp->lc_deleg, new_deleg, ls_list);
2939 if (delegate == 2 || nfsrv_issuedelegs == 0 ||
2940 (clp->lc_flags & (LCL_CALLBACKSON | LCL_CBDOWN)) !=
2942 NFSRV_V4DELEGLIMIT(nfsrv_delegatecnt) ||
2943 !NFSVNO_DELEGOK(vp))
2944 *rflagsp |= NFSV4OPEN_RECALL;
2945 newnfsstats.srvdelegates++;
2946 nfsrv_openpluslock++;
2947 nfsrv_delegatecnt++;
2950 * Now, do the associated open.
2952 new_open->ls_stateid.seqid = 1;
2953 new_open->ls_stateid.other[0] = clp->lc_clientid.lval[0];
2954 new_open->ls_stateid.other[1] = clp->lc_clientid.lval[1];
2955 new_open->ls_stateid.other[2] = nfsrv_nextstateindex(clp);
2956 new_open->ls_flags = (new_stp->ls_flags & NFSLCK_DENYBITS) |
2958 if (new_stp->ls_flags & NFSLCK_DELEGWRITE)
2959 new_open->ls_flags |= (NFSLCK_READACCESS |
2960 NFSLCK_WRITEACCESS);
2962 new_open->ls_flags |= NFSLCK_READACCESS;
2963 new_open->ls_uid = new_stp->ls_uid;
2964 new_open->ls_lfp = lfp;
2965 new_open->ls_clp = clp;
2966 LIST_INIT(&new_open->ls_open);
2967 LIST_INSERT_HEAD(&lfp->lf_open, new_open, ls_file);
2968 LIST_INSERT_HEAD(NFSSTATEHASH(clp, new_open->ls_stateid),
2971 * and handle the open owner
2974 new_open->ls_openowner = ownerstp;
2975 LIST_INSERT_HEAD(&ownerstp->ls_open, new_open, ls_list);
2977 new_open->ls_openowner = new_stp;
2978 new_stp->ls_flags = 0;
2979 nfsrvd_refcache(new_stp->ls_op);
2980 new_stp->ls_noopens = 0;
2981 LIST_INIT(&new_stp->ls_open);
2982 LIST_INSERT_HEAD(&new_stp->ls_open, new_open, ls_list);
2983 LIST_INSERT_HEAD(&clp->lc_open, new_stp, ls_list);
2985 newnfsstats.srvopenowners++;
2986 nfsrv_openpluslock++;
2990 newnfsstats.srvopens++;
2991 nfsrv_openpluslock++;
2993 error = NFSERR_RECLAIMCONFLICT;
2995 } else if (ownerstp) {
2996 if (ownerstp->ls_flags & NFSLCK_NEEDSCONFIRM) {
2997 /* Replace the open */
2998 if (ownerstp->ls_op)
2999 nfsrvd_derefcache(ownerstp->ls_op);
3000 ownerstp->ls_op = new_stp->ls_op;
3001 nfsrvd_refcache(ownerstp->ls_op);
3002 ownerstp->ls_seq = new_stp->ls_seq;
3003 *rflagsp |= NFSV4OPEN_RESULTCONFIRM;
3004 stp = LIST_FIRST(&ownerstp->ls_open);
3005 stp->ls_flags = (new_stp->ls_flags & NFSLCK_SHAREBITS) |
3007 stp->ls_stateid.seqid = 1;
3008 stp->ls_uid = new_stp->ls_uid;
3009 if (lfp != stp->ls_lfp) {
3010 LIST_REMOVE(stp, ls_file);
3011 LIST_INSERT_HEAD(&lfp->lf_open, stp, ls_file);
3015 } else if (openstp) {
3016 openstp->ls_flags |= (new_stp->ls_flags & NFSLCK_SHAREBITS);
3017 openstp->ls_stateid.seqid++;
3018 if ((nd->nd_flag & ND_NFSV41) != 0 &&
3019 openstp->ls_stateid.seqid == 0)
3020 openstp->ls_stateid.seqid = 1;
3023 * This is where we can choose to issue a delegation.
3025 if ((new_stp->ls_flags & NFSLCK_WANTNODELEG) != 0)
3026 *rflagsp |= NFSV4OPEN_WDNOTWANTED;
3027 else if (nfsrv_issuedelegs == 0)
3028 *rflagsp |= NFSV4OPEN_WDSUPPFTYPE;
3029 else if (NFSRV_V4DELEGLIMIT(nfsrv_delegatecnt))
3030 *rflagsp |= NFSV4OPEN_WDRESOURCE;
3031 else if (delegate == 0 || writedeleg == 0 ||
3032 NFSVNO_EXRDONLY(exp) || (readonly != 0 &&
3033 nfsrv_writedelegifpos == 0) ||
3034 !NFSVNO_DELEGOK(vp) ||
3035 (new_stp->ls_flags & NFSLCK_WANTRDELEG) != 0 ||
3036 (clp->lc_flags & (LCL_CALLBACKSON | LCL_CBDOWN)) !=
3038 *rflagsp |= NFSV4OPEN_WDCONTENTION;
3040 new_deleg->ls_stateid.seqid = delegstateidp->seqid = 1;
3041 new_deleg->ls_stateid.other[0] = delegstateidp->other[0]
3042 = clp->lc_clientid.lval[0];
3043 new_deleg->ls_stateid.other[1] = delegstateidp->other[1]
3044 = clp->lc_clientid.lval[1];
3045 new_deleg->ls_stateid.other[2] = delegstateidp->other[2]
3046 = nfsrv_nextstateindex(clp);
3047 new_deleg->ls_flags = (NFSLCK_DELEGWRITE |
3048 NFSLCK_READACCESS | NFSLCK_WRITEACCESS);
3049 *rflagsp |= NFSV4OPEN_WRITEDELEGATE;
3050 new_deleg->ls_uid = new_stp->ls_uid;
3051 new_deleg->ls_lfp = lfp;
3052 new_deleg->ls_clp = clp;
3053 new_deleg->ls_filerev = filerev;
3054 new_deleg->ls_compref = nd->nd_compref;
3055 nfsrv_writedelegcnt++;
3056 LIST_INSERT_HEAD(&lfp->lf_deleg, new_deleg, ls_file);
3057 LIST_INSERT_HEAD(NFSSTATEHASH(clp,
3058 new_deleg->ls_stateid), new_deleg, ls_hash);
3059 LIST_INSERT_HEAD(&clp->lc_deleg, new_deleg, ls_list);
3061 newnfsstats.srvdelegates++;
3062 nfsrv_openpluslock++;
3063 nfsrv_delegatecnt++;
3066 new_open->ls_stateid.seqid = 1;
3067 new_open->ls_stateid.other[0] = clp->lc_clientid.lval[0];
3068 new_open->ls_stateid.other[1] = clp->lc_clientid.lval[1];
3069 new_open->ls_stateid.other[2] = nfsrv_nextstateindex(clp);
3070 new_open->ls_flags = (new_stp->ls_flags & NFSLCK_SHAREBITS)|
3072 new_open->ls_uid = new_stp->ls_uid;
3073 new_open->ls_openowner = ownerstp;
3074 new_open->ls_lfp = lfp;
3075 new_open->ls_clp = clp;
3076 LIST_INIT(&new_open->ls_open);
3077 LIST_INSERT_HEAD(&lfp->lf_open, new_open, ls_file);
3078 LIST_INSERT_HEAD(&ownerstp->ls_open, new_open, ls_list);
3079 LIST_INSERT_HEAD(NFSSTATEHASH(clp, new_open->ls_stateid),
3083 newnfsstats.srvopens++;
3084 nfsrv_openpluslock++;
3087 * This is where we can choose to issue a delegation.
3089 if ((new_stp->ls_flags & NFSLCK_WANTNODELEG) != 0)
3090 *rflagsp |= NFSV4OPEN_WDNOTWANTED;
3091 else if (nfsrv_issuedelegs == 0)
3092 *rflagsp |= NFSV4OPEN_WDSUPPFTYPE;
3093 else if (NFSRV_V4DELEGLIMIT(nfsrv_delegatecnt))
3094 *rflagsp |= NFSV4OPEN_WDRESOURCE;
3095 else if (delegate == 0 || (writedeleg == 0 &&
3096 readonly == 0) || !NFSVNO_DELEGOK(vp) ||
3097 (clp->lc_flags & (LCL_CALLBACKSON | LCL_CBDOWN)) !=
3099 *rflagsp |= NFSV4OPEN_WDCONTENTION;
3101 new_deleg->ls_stateid.seqid = delegstateidp->seqid = 1;
3102 new_deleg->ls_stateid.other[0] = delegstateidp->other[0]
3103 = clp->lc_clientid.lval[0];
3104 new_deleg->ls_stateid.other[1] = delegstateidp->other[1]
3105 = clp->lc_clientid.lval[1];
3106 new_deleg->ls_stateid.other[2] = delegstateidp->other[2]
3107 = nfsrv_nextstateindex(clp);
3108 if (writedeleg && !NFSVNO_EXRDONLY(exp) &&
3109 (nfsrv_writedelegifpos || !readonly) &&
3110 (new_stp->ls_flags & NFSLCK_WANTRDELEG) == 0) {
3111 new_deleg->ls_flags = (NFSLCK_DELEGWRITE |
3112 NFSLCK_READACCESS | NFSLCK_WRITEACCESS);
3113 *rflagsp |= NFSV4OPEN_WRITEDELEGATE;
3114 nfsrv_writedelegcnt++;
3116 new_deleg->ls_flags = (NFSLCK_DELEGREAD |
3118 *rflagsp |= NFSV4OPEN_READDELEGATE;
3120 new_deleg->ls_uid = new_stp->ls_uid;
3121 new_deleg->ls_lfp = lfp;
3122 new_deleg->ls_clp = clp;
3123 new_deleg->ls_filerev = filerev;
3124 new_deleg->ls_compref = nd->nd_compref;
3125 LIST_INSERT_HEAD(&lfp->lf_deleg, new_deleg, ls_file);
3126 LIST_INSERT_HEAD(NFSSTATEHASH(clp,
3127 new_deleg->ls_stateid), new_deleg, ls_hash);
3128 LIST_INSERT_HEAD(&clp->lc_deleg, new_deleg, ls_list);
3130 newnfsstats.srvdelegates++;
3131 nfsrv_openpluslock++;
3132 nfsrv_delegatecnt++;
3137 * New owner case. Start the open_owner sequence with a
3138 * Needs confirmation (unless a reclaim) and hang the
3141 new_open->ls_stateid.seqid = 1;
3142 new_open->ls_stateid.other[0] = clp->lc_clientid.lval[0];
3143 new_open->ls_stateid.other[1] = clp->lc_clientid.lval[1];
3144 new_open->ls_stateid.other[2] = nfsrv_nextstateindex(clp);
3145 new_open->ls_flags = (new_stp->ls_flags & NFSLCK_SHAREBITS) |
3147 new_open->ls_uid = new_stp->ls_uid;
3148 LIST_INIT(&new_open->ls_open);
3149 new_open->ls_openowner = new_stp;
3150 new_open->ls_lfp = lfp;
3151 new_open->ls_clp = clp;
3152 LIST_INSERT_HEAD(&lfp->lf_open, new_open, ls_file);
3153 if (new_stp->ls_flags & NFSLCK_RECLAIM) {
3154 new_stp->ls_flags = 0;
3155 } else if ((nd->nd_flag & ND_NFSV41) != 0) {
3156 /* NFSv4.1 never needs confirmation. */
3157 new_stp->ls_flags = 0;
3160 * This is where we can choose to issue a delegation.
3162 if (delegate && nfsrv_issuedelegs &&
3163 (writedeleg || readonly) &&
3164 (clp->lc_flags & (LCL_CALLBACKSON | LCL_CBDOWN)) ==
3166 !NFSRV_V4DELEGLIMIT(nfsrv_delegatecnt) &&
3167 NFSVNO_DELEGOK(vp) &&
3168 ((nd->nd_flag & ND_NFSV41) == 0 ||
3169 (new_stp->ls_flags & NFSLCK_WANTNODELEG) == 0)) {
3170 new_deleg->ls_stateid.seqid =
3171 delegstateidp->seqid = 1;
3172 new_deleg->ls_stateid.other[0] =
3173 delegstateidp->other[0]
3174 = clp->lc_clientid.lval[0];
3175 new_deleg->ls_stateid.other[1] =
3176 delegstateidp->other[1]
3177 = clp->lc_clientid.lval[1];
3178 new_deleg->ls_stateid.other[2] =
3179 delegstateidp->other[2]
3180 = nfsrv_nextstateindex(clp);
3181 if (writedeleg && !NFSVNO_EXRDONLY(exp) &&
3182 (nfsrv_writedelegifpos || !readonly) &&
3183 ((nd->nd_flag & ND_NFSV41) == 0 ||
3184 (new_stp->ls_flags & NFSLCK_WANTRDELEG) ==
3186 new_deleg->ls_flags =
3187 (NFSLCK_DELEGWRITE |
3189 NFSLCK_WRITEACCESS);
3190 *rflagsp |= NFSV4OPEN_WRITEDELEGATE;
3191 nfsrv_writedelegcnt++;
3193 new_deleg->ls_flags =
3196 *rflagsp |= NFSV4OPEN_READDELEGATE;
3198 new_deleg->ls_uid = new_stp->ls_uid;
3199 new_deleg->ls_lfp = lfp;
3200 new_deleg->ls_clp = clp;
3201 new_deleg->ls_filerev = filerev;
3202 new_deleg->ls_compref = nd->nd_compref;
3203 LIST_INSERT_HEAD(&lfp->lf_deleg, new_deleg,
3205 LIST_INSERT_HEAD(NFSSTATEHASH(clp,
3206 new_deleg->ls_stateid), new_deleg, ls_hash);
3207 LIST_INSERT_HEAD(&clp->lc_deleg, new_deleg,
3210 newnfsstats.srvdelegates++;
3211 nfsrv_openpluslock++;
3212 nfsrv_delegatecnt++;
3215 * Since NFSv4.1 never does an OpenConfirm, the first
3216 * open state will be acquired here.
3218 if (!(clp->lc_flags & LCL_STAMPEDSTABLE)) {
3219 clp->lc_flags |= LCL_STAMPEDSTABLE;
3220 len = clp->lc_idlen;
3221 NFSBCOPY(clp->lc_id, clidp, len);
3225 *rflagsp |= NFSV4OPEN_RESULTCONFIRM;
3226 new_stp->ls_flags = NFSLCK_NEEDSCONFIRM;
3228 nfsrvd_refcache(new_stp->ls_op);
3229 new_stp->ls_noopens = 0;
3230 LIST_INIT(&new_stp->ls_open);
3231 LIST_INSERT_HEAD(&new_stp->ls_open, new_open, ls_list);
3232 LIST_INSERT_HEAD(&clp->lc_open, new_stp, ls_list);
3233 LIST_INSERT_HEAD(NFSSTATEHASH(clp, new_open->ls_stateid),
3238 newnfsstats.srvopens++;
3239 nfsrv_openpluslock++;
3240 newnfsstats.srvopenowners++;
3241 nfsrv_openpluslock++;
3244 stateidp->seqid = openstp->ls_stateid.seqid;
3245 stateidp->other[0] = openstp->ls_stateid.other[0];
3246 stateidp->other[1] = openstp->ls_stateid.other[1];
3247 stateidp->other[2] = openstp->ls_stateid.other[2];
3251 NFSLOCKV4ROOTMUTEX();
3252 nfsv4_unlock(&nfsv4rootfs_lock, 1);
3253 NFSUNLOCKV4ROOTMUTEX();
3256 FREE((caddr_t)new_open, M_NFSDSTATE);
3258 FREE((caddr_t)new_deleg, M_NFSDSTATE);
3261 * If the NFSv4.1 client just acquired its first open, write a timestamp
3262 * to the stable storage file.
3264 if (gotstate != 0) {
3265 nfsrv_writestable(clidp, len, NFSNST_NEWSTATE, p);
3266 nfsrv_backupstable();
3270 free(clidp, M_TEMP);
3271 NFSEXITCODE2(error, nd);
3276 * Open update. Does the confirm, downgrade and close.
3279 nfsrv_openupdate(vnode_t vp, struct nfsstate *new_stp, nfsquad_t clientid,
3280 nfsv4stateid_t *stateidp, struct nfsrv_descript *nd, NFSPROC_T *p)
3282 struct nfsstate *stp, *ownerstp;
3283 struct nfsclient *clp;
3284 struct nfslockfile *lfp;
3286 int error = 0, gotstate = 0, len = 0;
3287 u_char *clidp = NULL;
3290 * Check for restart conditions (client and server).
3292 error = nfsrv_checkrestart(clientid, new_stp->ls_flags,
3293 &new_stp->ls_stateid, 0);
3297 clidp = malloc(NFSV4_OPAQUELIMIT, M_TEMP, M_WAITOK);
3300 * Get the open structure via clientid and stateid.
3302 error = nfsrv_getclient(clientid, CLOPS_RENEW, &clp, NULL,
3303 (nfsquad_t)((u_quad_t)0), 0, nd, p);
3305 error = nfsrv_getstate(clp, &new_stp->ls_stateid,
3306 new_stp->ls_flags, &stp);
3309 * Sanity check the open.
3311 if (!error && (!(stp->ls_flags & NFSLCK_OPEN) ||
3312 (!(new_stp->ls_flags & NFSLCK_CONFIRM) &&
3313 (stp->ls_openowner->ls_flags & NFSLCK_NEEDSCONFIRM)) ||
3314 ((new_stp->ls_flags & NFSLCK_CONFIRM) &&
3315 (!(stp->ls_openowner->ls_flags & NFSLCK_NEEDSCONFIRM)))))
3316 error = NFSERR_BADSTATEID;
3319 error = nfsrv_checkseqid(nd, new_stp->ls_seq,
3320 stp->ls_openowner, new_stp->ls_op);
3321 if (!error && stp->ls_stateid.seqid != new_stp->ls_stateid.seqid &&
3322 (((nd->nd_flag & ND_NFSV41) == 0 &&
3323 !(new_stp->ls_flags & NFSLCK_CONFIRM)) ||
3324 ((nd->nd_flag & ND_NFSV41) != 0 &&
3325 new_stp->ls_stateid.seqid != 0)))
3326 error = NFSERR_OLDSTATEID;
3327 if (!error && vnode_vtype(vp) != VREG) {
3328 if (vnode_vtype(vp) == VDIR)
3329 error = NFSERR_ISDIR;
3331 error = NFSERR_INVAL;
3336 * If a client tries to confirm an Open with a bad
3337 * seqid# and there are no byte range locks or other Opens
3338 * on the openowner, just throw it away, so the next use of the
3339 * openowner will start a fresh seq#.
3341 if (error == NFSERR_BADSEQID &&
3342 (new_stp->ls_flags & NFSLCK_CONFIRM) &&
3343 nfsrv_nootherstate(stp))
3344 nfsrv_freeopenowner(stp->ls_openowner, 0, p);
3350 * Set the return stateid.
3352 stateidp->seqid = stp->ls_stateid.seqid + 1;
3353 if ((nd->nd_flag & ND_NFSV41) != 0 && stateidp->seqid == 0)
3354 stateidp->seqid = 1;
3355 stateidp->other[0] = stp->ls_stateid.other[0];
3356 stateidp->other[1] = stp->ls_stateid.other[1];
3357 stateidp->other[2] = stp->ls_stateid.other[2];
3359 * Now, handle the three cases.
3361 if (new_stp->ls_flags & NFSLCK_CONFIRM) {
3363 * If the open doesn't need confirmation, it seems to me that
3364 * there is a client error, but I'll just log it and keep going?
3366 if (!(stp->ls_openowner->ls_flags & NFSLCK_NEEDSCONFIRM))
3367 printf("Nfsv4d: stray open confirm\n");
3368 stp->ls_openowner->ls_flags = 0;
3369 stp->ls_stateid.seqid++;
3370 if ((nd->nd_flag & ND_NFSV41) != 0 &&
3371 stp->ls_stateid.seqid == 0)
3372 stp->ls_stateid.seqid = 1;
3373 if (!(clp->lc_flags & LCL_STAMPEDSTABLE)) {
3374 clp->lc_flags |= LCL_STAMPEDSTABLE;
3375 len = clp->lc_idlen;
3376 NFSBCOPY(clp->lc_id, clidp, len);
3380 } else if (new_stp->ls_flags & NFSLCK_CLOSE) {
3381 ownerstp = stp->ls_openowner;
3383 if (nfsrv_dolocallocks != 0 && !LIST_EMPTY(&stp->ls_open)) {
3384 /* Get the lf lock */
3387 ASSERT_VOP_ELOCKED(vp, "nfsrv_openupdate");
3388 NFSVOPUNLOCK(vp, 0);
3389 if (nfsrv_freeopen(stp, vp, 1, p) == 0) {
3391 nfsrv_unlocklf(lfp);
3394 NFSVOPLOCK(vp, LK_EXCLUSIVE | LK_RETRY);
3396 (void) nfsrv_freeopen(stp, NULL, 0, p);
3401 * Update the share bits, making sure that the new set are a
3402 * subset of the old ones.
3404 bits = (new_stp->ls_flags & NFSLCK_SHAREBITS);
3405 if (~(stp->ls_flags) & bits) {
3407 error = NFSERR_INVAL;
3410 stp->ls_flags = (bits | NFSLCK_OPEN);
3411 stp->ls_stateid.seqid++;
3412 if ((nd->nd_flag & ND_NFSV41) != 0 &&
3413 stp->ls_stateid.seqid == 0)
3414 stp->ls_stateid.seqid = 1;
3419 * If the client just confirmed its first open, write a timestamp
3420 * to the stable storage file.
3422 if (gotstate != 0) {
3423 nfsrv_writestable(clidp, len, NFSNST_NEWSTATE, p);
3424 nfsrv_backupstable();
3428 free(clidp, M_TEMP);
3429 NFSEXITCODE2(error, nd);
3434 * Delegation update. Does the purge and return.
3437 nfsrv_delegupdate(struct nfsrv_descript *nd, nfsquad_t clientid,
3438 nfsv4stateid_t *stateidp, vnode_t vp, int op, struct ucred *cred,
3441 struct nfsstate *stp;
3442 struct nfsclient *clp;
3447 * Do a sanity check against the file handle for DelegReturn.
3450 error = nfsvno_getfh(vp, &fh, p);
3455 * Check for restart conditions (client and server).
3457 if (op == NFSV4OP_DELEGRETURN)
3458 error = nfsrv_checkrestart(clientid, NFSLCK_DELEGRETURN,
3461 error = nfsrv_checkrestart(clientid, NFSLCK_DELEGPURGE,
3466 * Get the open structure via clientid and stateid.
3469 error = nfsrv_getclient(clientid, CLOPS_RENEW, &clp, NULL,
3470 (nfsquad_t)((u_quad_t)0), 0, nd, p);
3472 if (error == NFSERR_CBPATHDOWN)
3474 if (error == NFSERR_STALECLIENTID && op == NFSV4OP_DELEGRETURN)
3475 error = NFSERR_STALESTATEID;
3477 if (!error && op == NFSV4OP_DELEGRETURN) {
3478 error = nfsrv_getstate(clp, stateidp, NFSLCK_DELEGRETURN, &stp);
3479 if (!error && stp->ls_stateid.seqid != stateidp->seqid &&
3480 ((nd->nd_flag & ND_NFSV41) == 0 || stateidp->seqid != 0))
3481 error = NFSERR_OLDSTATEID;
3484 * NFSERR_EXPIRED means that the state has gone away,
3485 * so Delegations have been purged. Just return ok.
3487 if (error == NFSERR_EXPIRED && op == NFSV4OP_DELEGPURGE) {
3497 if (op == NFSV4OP_DELEGRETURN) {
3498 if (NFSBCMP((caddr_t)&fh, (caddr_t)&stp->ls_lfp->lf_fh,
3499 sizeof (fhandle_t))) {
3501 error = NFSERR_BADSTATEID;
3504 nfsrv_freedeleg(stp);
3506 nfsrv_freedeleglist(&clp->lc_olddeleg);
3517 * Release lock owner.
3520 nfsrv_releaselckown(struct nfsstate *new_stp, nfsquad_t clientid,
3523 struct nfsstate *stp, *nstp, *openstp, *ownstp;
3524 struct nfsclient *clp;
3528 * Check for restart conditions (client and server).
3530 error = nfsrv_checkrestart(clientid, new_stp->ls_flags,
3531 &new_stp->ls_stateid, 0);
3537 * Get the lock owner by name.
3539 error = nfsrv_getclient(clientid, CLOPS_RENEW, &clp, NULL,
3540 (nfsquad_t)((u_quad_t)0), 0, NULL, p);
3545 LIST_FOREACH(ownstp, &clp->lc_open, ls_list) {
3546 LIST_FOREACH(openstp, &ownstp->ls_open, ls_list) {
3547 stp = LIST_FIRST(&openstp->ls_open);
3548 while (stp != LIST_END(&openstp->ls_open)) {
3549 nstp = LIST_NEXT(stp, ls_list);
3551 * If the owner matches, check for locks and
3552 * then free or return an error.
3554 if (stp->ls_ownerlen == new_stp->ls_ownerlen &&
3555 !NFSBCMP(stp->ls_owner, new_stp->ls_owner,
3557 if (LIST_EMPTY(&stp->ls_lock)) {
3558 nfsrv_freelockowner(stp, NULL, 0, p);
3561 error = NFSERR_LOCKSHELD;
3577 * Get the file handle for a lock structure.
3580 nfsrv_getlockfh(vnode_t vp, u_short flags, struct nfslockfile *new_lfp,
3581 fhandle_t *nfhp, NFSPROC_T *p)
3583 fhandle_t *fhp = NULL;
3587 * For lock, use the new nfslock structure, otherwise just
3588 * a fhandle_t on the stack.
3590 if (flags & NFSLCK_OPEN) {
3591 KASSERT(new_lfp != NULL, ("nfsrv_getlockfh: new_lfp NULL"));
3592 fhp = &new_lfp->lf_fh;
3596 panic("nfsrv_getlockfh");
3598 error = nfsvno_getfh(vp, fhp, p);
3604 * Get an nfs lock structure. Allocate one, as required, and return a
3606 * Returns an NFSERR_xxx upon failure or -1 to indicate no current lock.
3609 nfsrv_getlockfile(u_short flags, struct nfslockfile **new_lfpp,
3610 struct nfslockfile **lfpp, fhandle_t *nfhp, int lockit)
3612 struct nfslockfile *lfp;
3613 fhandle_t *fhp = NULL, *tfhp;
3614 struct nfslockhashhead *hp;
3615 struct nfslockfile *new_lfp = NULL;
3618 * For lock, use the new nfslock structure, otherwise just
3619 * a fhandle_t on the stack.
3621 if (flags & NFSLCK_OPEN) {
3622 new_lfp = *new_lfpp;
3623 fhp = &new_lfp->lf_fh;
3627 panic("nfsrv_getlockfile");
3630 hp = NFSLOCKHASH(fhp);
3631 LIST_FOREACH(lfp, hp, lf_hash) {
3633 if (NFSVNO_CMPFH(fhp, tfhp)) {
3640 if (!(flags & NFSLCK_OPEN))
3644 * No match, so chain the new one into the list.
3646 LIST_INIT(&new_lfp->lf_open);
3647 LIST_INIT(&new_lfp->lf_lock);
3648 LIST_INIT(&new_lfp->lf_deleg);
3649 LIST_INIT(&new_lfp->lf_locallock);
3650 LIST_INIT(&new_lfp->lf_rollback);
3651 new_lfp->lf_locallock_lck.nfslock_usecnt = 0;
3652 new_lfp->lf_locallock_lck.nfslock_lock = 0;
3653 new_lfp->lf_usecount = 0;
3654 LIST_INSERT_HEAD(hp, new_lfp, lf_hash);
3661 * This function adds a nfslock lock structure to the list for the associated
3662 * nfsstate and nfslockfile structures. It will be inserted after the
3663 * entry pointed at by insert_lop.
3666 nfsrv_insertlock(struct nfslock *new_lop, struct nfslock *insert_lop,
3667 struct nfsstate *stp, struct nfslockfile *lfp)
3669 struct nfslock *lop, *nlop;
3671 new_lop->lo_stp = stp;
3672 new_lop->lo_lfp = lfp;
3675 /* Insert in increasing lo_first order */
3676 lop = LIST_FIRST(&lfp->lf_lock);
3677 if (lop == LIST_END(&lfp->lf_lock) ||
3678 new_lop->lo_first <= lop->lo_first) {
3679 LIST_INSERT_HEAD(&lfp->lf_lock, new_lop, lo_lckfile);
3681 nlop = LIST_NEXT(lop, lo_lckfile);
3682 while (nlop != LIST_END(&lfp->lf_lock) &&
3683 nlop->lo_first < new_lop->lo_first) {
3685 nlop = LIST_NEXT(lop, lo_lckfile);
3687 LIST_INSERT_AFTER(lop, new_lop, lo_lckfile);
3690 new_lop->lo_lckfile.le_prev = NULL; /* list not used */
3694 * Insert after insert_lop, which is overloaded as stp or lfp for
3697 if (stp == NULL && (struct nfslockfile *)insert_lop == lfp)
3698 LIST_INSERT_HEAD(&lfp->lf_locallock, new_lop, lo_lckowner);
3699 else if ((struct nfsstate *)insert_lop == stp)
3700 LIST_INSERT_HEAD(&stp->ls_lock, new_lop, lo_lckowner);
3702 LIST_INSERT_AFTER(insert_lop, new_lop, lo_lckowner);
3704 newnfsstats.srvlocks++;
3705 nfsrv_openpluslock++;
3710 * This function updates the locking for a lock owner and given file. It
3711 * maintains a list of lock ranges ordered on increasing file offset that
3712 * are NFSLCK_READ or NFSLCK_WRITE and non-overlapping (aka POSIX style).
3713 * It always adds new_lop to the list and sometimes uses the one pointed
3717 nfsrv_updatelock(struct nfsstate *stp, struct nfslock **new_lopp,
3718 struct nfslock **other_lopp, struct nfslockfile *lfp)
3720 struct nfslock *new_lop = *new_lopp;
3721 struct nfslock *lop, *tlop, *ilop;
3722 struct nfslock *other_lop = *other_lopp;
3723 int unlock = 0, myfile = 0;
3727 * Work down the list until the lock is merged.
3729 if (new_lop->lo_flags & NFSLCK_UNLOCK)
3732 ilop = (struct nfslock *)stp;
3733 lop = LIST_FIRST(&stp->ls_lock);
3735 ilop = (struct nfslock *)lfp;
3736 lop = LIST_FIRST(&lfp->lf_locallock);
3738 while (lop != NULL) {
3740 * Only check locks for this file that aren't before the start of
3743 if (lop->lo_lfp == lfp) {
3745 if (lop->lo_end >= new_lop->lo_first) {
3746 if (new_lop->lo_end < lop->lo_first) {
3748 * If the new lock ends before the start of the
3749 * current lock's range, no merge, just insert
3754 if (new_lop->lo_flags == lop->lo_flags ||
3755 (new_lop->lo_first <= lop->lo_first &&
3756 new_lop->lo_end >= lop->lo_end)) {
3758 * This lock can be absorbed by the new lock/unlock.
3759 * This happens when it covers the entire range
3760 * of the old lock or is contiguous
3761 * with the old lock and is of the same type or an
3764 if (lop->lo_first < new_lop->lo_first)
3765 new_lop->lo_first = lop->lo_first;
3766 if (lop->lo_end > new_lop->lo_end)
3767 new_lop->lo_end = lop->lo_end;
3769 lop = LIST_NEXT(lop, lo_lckowner);
3770 nfsrv_freenfslock(tlop);
3775 * All these cases are for contiguous locks that are not the
3776 * same type, so they can't be merged.
3778 if (new_lop->lo_first <= lop->lo_first) {
3780 * This case is where the new lock overlaps with the
3781 * first part of the old lock. Move the start of the
3782 * old lock to just past the end of the new lock. The
3783 * new lock will be inserted in front of the old, since
3784 * ilop hasn't been updated. (We are done now.)
3786 lop->lo_first = new_lop->lo_end;
3789 if (new_lop->lo_end >= lop->lo_end) {
3791 * This case is where the new lock overlaps with the
3792 * end of the old lock's range. Move the old lock's
3793 * end to just before the new lock's first and insert
3794 * the new lock after the old lock.
3795 * Might not be done yet, since the new lock could
3796 * overlap further locks with higher ranges.
3798 lop->lo_end = new_lop->lo_first;
3800 lop = LIST_NEXT(lop, lo_lckowner);
3804 * The final case is where the new lock's range is in the
3805 * middle of the current lock's and splits the current lock
3806 * up. Use *other_lopp to handle the second part of the
3807 * split old lock range. (We are done now.)
3808 * For unlock, we use new_lop as other_lop and tmp, since
3809 * other_lop and new_lop are the same for this case.
3810 * We noted the unlock case above, so we don't need
3811 * new_lop->lo_flags any longer.
3813 tmp = new_lop->lo_first;
3814 if (other_lop == NULL) {
3816 panic("nfsd srv update unlock");
3817 other_lop = new_lop;
3820 other_lop->lo_first = new_lop->lo_end;
3821 other_lop->lo_end = lop->lo_end;
3822 other_lop->lo_flags = lop->lo_flags;
3823 other_lop->lo_stp = stp;
3824 other_lop->lo_lfp = lfp;
3826 nfsrv_insertlock(other_lop, lop, stp, lfp);
3833 lop = LIST_NEXT(lop, lo_lckowner);
3834 if (myfile && (lop == NULL || lop->lo_lfp != lfp))
3839 * Insert the new lock in the list at the appropriate place.
3842 nfsrv_insertlock(new_lop, ilop, stp, lfp);
3848 * This function handles sequencing of locks, etc.
3849 * It returns an error that indicates what the caller should do.
3852 nfsrv_checkseqid(struct nfsrv_descript *nd, u_int32_t seqid,
3853 struct nfsstate *stp, struct nfsrvcache *op)
3857 if ((nd->nd_flag & ND_NFSV41) != 0)
3858 /* NFSv4.1 ignores the open_seqid and lock_seqid. */
3860 if (op != nd->nd_rp)
3861 panic("nfsrvstate checkseqid");
3862 if (!(op->rc_flag & RC_INPROG))
3863 panic("nfsrvstate not inprog");
3864 if (stp->ls_op && stp->ls_op->rc_refcnt <= 0) {
3865 printf("refcnt=%d\n", stp->ls_op->rc_refcnt);
3866 panic("nfsrvstate op refcnt");
3868 if ((stp->ls_seq + 1) == seqid) {
3870 nfsrvd_derefcache(stp->ls_op);
3872 nfsrvd_refcache(op);
3873 stp->ls_seq = seqid;
3875 } else if (stp->ls_seq == seqid && stp->ls_op &&
3876 op->rc_xid == stp->ls_op->rc_xid &&
3877 op->rc_refcnt == 0 &&
3878 op->rc_reqlen == stp->ls_op->rc_reqlen &&
3879 op->rc_cksum == stp->ls_op->rc_cksum) {
3880 if (stp->ls_op->rc_flag & RC_INPROG) {
3881 error = NFSERR_DONTREPLY;
3884 nd->nd_rp = stp->ls_op;
3885 nd->nd_rp->rc_flag |= RC_INPROG;
3886 nfsrvd_delcache(op);
3887 error = NFSERR_REPLYFROMCACHE;
3890 error = NFSERR_BADSEQID;
3893 NFSEXITCODE2(error, nd);
3898 * Get the client ip address for callbacks. If the strings can't be parsed,
3899 * just set lc_program to 0 to indicate no callbacks are possible.
3900 * (For cases where the address can't be parsed or is 0.0.0.0.0.0, set
3901 * the address to the client's transport address. This won't be used
3902 * for callbacks, but can be printed out by newnfsstats for info.)
3903 * Return error if the xdr can't be parsed, 0 otherwise.
3906 nfsrv_getclientipaddr(struct nfsrv_descript *nd, struct nfsclient *clp)
3911 struct sockaddr_in *rad, *sad;
3912 u_char protocol[5], addr[24];
3913 int error = 0, cantparse = 0;
3923 rad = NFSSOCKADDR(clp->lc_req.nr_nam, struct sockaddr_in *);
3924 rad->sin_family = AF_INET;
3925 rad->sin_len = sizeof (struct sockaddr_in);
3926 rad->sin_addr.s_addr = 0;
3928 clp->lc_req.nr_client = NULL;
3929 clp->lc_req.nr_lock = 0;
3930 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
3931 i = fxdr_unsigned(int, *tl);
3932 if (i >= 3 && i <= 4) {
3933 error = nfsrv_mtostr(nd, protocol, i);
3936 if (!strcmp(protocol, "tcp")) {
3937 clp->lc_flags |= LCL_TCPCALLBACK;
3938 clp->lc_req.nr_sotype = SOCK_STREAM;
3939 clp->lc_req.nr_soproto = IPPROTO_TCP;
3940 } else if (!strcmp(protocol, "udp")) {
3941 clp->lc_req.nr_sotype = SOCK_DGRAM;
3942 clp->lc_req.nr_soproto = IPPROTO_UDP;
3949 error = nfsm_advance(nd, NFSM_RNDUP(i), -1);
3954 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
3955 i = fxdr_unsigned(int, *tl);
3957 error = NFSERR_BADXDR;
3959 } else if (i == 0) {
3961 } else if (!cantparse && i <= 23 && i >= 11) {
3962 error = nfsrv_mtostr(nd, addr, i);
3967 * Parse out the address fields. We expect 6 decimal numbers
3968 * separated by '.'s.
3972 while (*cp && i < 6) {
3974 while (*cp2 && *cp2 != '.')
3982 j = nfsrv_getipnumber(cp);
3987 port.cval[5 - i] = j;
3996 if (ip.ival != 0x0) {
3997 rad->sin_addr.s_addr = htonl(ip.ival);
3998 rad->sin_port = htons(port.sval);
4006 error = nfsm_advance(nd, NFSM_RNDUP(i), -1);
4012 sad = NFSSOCKADDR(nd->nd_nam, struct sockaddr_in *);
4013 if (sad->sin_family == AF_INET) {
4014 rad->sin_addr.s_addr = sad->sin_addr.s_addr;
4015 rad->sin_port = 0x0;
4017 clp->lc_program = 0;
4020 NFSEXITCODE2(error, nd);
4025 * Turn a string of up to three decimal digits into a number. Return -1 upon
4029 nfsrv_getipnumber(u_char *cp)
4034 if (j > 2 || *cp < '0' || *cp > '9')
4047 * This function checks for restart conditions.
4050 nfsrv_checkrestart(nfsquad_t clientid, u_int32_t flags,
4051 nfsv4stateid_t *stateidp, int specialid)
4056 * First check for a server restart. Open, LockT, ReleaseLockOwner
4057 * and DelegPurge have a clientid, the rest a stateid.
4060 (NFSLCK_OPEN | NFSLCK_TEST | NFSLCK_RELEASE | NFSLCK_DELEGPURGE)) {
4061 if (clientid.lval[0] != nfsrvboottime) {
4062 ret = NFSERR_STALECLIENTID;
4065 } else if (stateidp->other[0] != nfsrvboottime &&
4067 ret = NFSERR_STALESTATEID;
4072 * Read, Write, Setattr and LockT can return NFSERR_GRACE and do
4073 * not use a lock/open owner seqid#, so the check can be done now.
4074 * (The others will be checked, as required, later.)
4076 if (!(flags & (NFSLCK_CHECK | NFSLCK_TEST)))
4080 ret = nfsrv_checkgrace(NULL, NULL, flags);
4092 nfsrv_checkgrace(struct nfsrv_descript *nd, struct nfsclient *clp,
4095 int error = 0, notreclaimed;
4096 struct nfsrv_stable *sp;
4098 if ((nfsrv_stablefirst.nsf_flags & (NFSNSF_UPDATEDONE |
4099 NFSNSF_GRACEOVER)) == 0) {
4101 * First, check to see if all of the clients have done a
4102 * ReclaimComplete. If so, grace can end now.
4105 LIST_FOREACH(sp, &nfsrv_stablefirst.nsf_head, nst_list) {
4106 if ((sp->nst_flag & NFSNST_RECLAIMED) == 0) {
4111 if (notreclaimed == 0)
4112 nfsrv_stablefirst.nsf_flags |= (NFSNSF_GRACEOVER |
4116 if ((nfsrv_stablefirst.nsf_flags & NFSNSF_GRACEOVER) != 0) {
4117 if (flags & NFSLCK_RECLAIM) {
4118 error = NFSERR_NOGRACE;
4122 if (!(flags & NFSLCK_RECLAIM)) {
4123 error = NFSERR_GRACE;
4126 if (nd != NULL && clp != NULL &&
4127 (nd->nd_flag & ND_NFSV41) != 0 &&
4128 (clp->lc_flags & LCL_RECLAIMCOMPLETE) != 0) {
4129 error = NFSERR_NOGRACE;
4134 * If grace is almost over and we are still getting Reclaims,
4135 * extend grace a bit.
4137 if ((NFSD_MONOSEC + NFSRV_LEASEDELTA) >
4138 nfsrv_stablefirst.nsf_eograce)
4139 nfsrv_stablefirst.nsf_eograce = NFSD_MONOSEC +
4149 * Do a server callback.
4152 nfsrv_docallback(struct nfsclient *clp, int procnum,
4153 nfsv4stateid_t *stateidp, int trunc, fhandle_t *fhp,
4154 struct nfsvattr *nap, nfsattrbit_t *attrbitp, NFSPROC_T *p)
4158 struct nfsrv_descript nfsd, *nd = &nfsd;
4162 struct nfsdsession *sep = NULL;
4164 cred = newnfs_getcred();
4165 NFSLOCKSTATE(); /* mostly for lc_cbref++ */
4166 if (clp->lc_flags & LCL_NEEDSCONFIRM) {
4173 * Fill the callback program# and version into the request
4174 * structure for newnfs_connect() to use.
4176 clp->lc_req.nr_prog = clp->lc_program;
4178 if ((clp->lc_flags & LCL_NFSV41) != 0)
4179 clp->lc_req.nr_vers = NFSV41_CBVERS;
4182 clp->lc_req.nr_vers = NFSV4_CBVERS;
4185 * First, fill in some of the fields of nd and cr.
4187 nd->nd_flag = ND_NFSV4;
4188 if (clp->lc_flags & LCL_GSS)
4189 nd->nd_flag |= ND_KERBV;
4190 if ((clp->lc_flags & LCL_NFSV41) != 0)
4191 nd->nd_flag |= ND_NFSV41;
4193 cred->cr_uid = clp->lc_uid;
4194 cred->cr_gid = clp->lc_gid;
4195 callback = clp->lc_callback;
4197 cred->cr_ngroups = 1;
4200 * Get the first mbuf for the request.
4202 MGET(m, M_WAITOK, MT_DATA);
4204 nd->nd_mreq = nd->nd_mb = m;
4205 nd->nd_bpos = NFSMTOD(m, caddr_t);
4208 * and build the callback request.
4210 if (procnum == NFSV4OP_CBGETATTR) {
4211 nd->nd_procnum = NFSV4PROC_CBCOMPOUND;
4212 error = nfsrv_cbcallargs(nd, clp, callback, NFSV4OP_CBGETATTR,
4213 "CB Getattr", &sep);
4215 mbuf_freem(nd->nd_mreq);
4218 (void)nfsm_fhtom(nd, (u_int8_t *)fhp, NFSX_MYFH, 0);
4219 (void)nfsrv_putattrbit(nd, attrbitp);
4220 } else if (procnum == NFSV4OP_CBRECALL) {
4221 nd->nd_procnum = NFSV4PROC_CBCOMPOUND;
4222 error = nfsrv_cbcallargs(nd, clp, callback, NFSV4OP_CBRECALL,
4225 mbuf_freem(nd->nd_mreq);
4228 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED + NFSX_STATEID);
4229 *tl++ = txdr_unsigned(stateidp->seqid);
4230 NFSBCOPY((caddr_t)stateidp->other, (caddr_t)tl,
4232 tl += (NFSX_STATEIDOTHER / NFSX_UNSIGNED);
4237 (void)nfsm_fhtom(nd, (u_int8_t *)fhp, NFSX_MYFH, 0);
4238 } else if (procnum == NFSV4PROC_CBNULL) {
4239 nd->nd_procnum = NFSV4PROC_CBNULL;
4240 if ((clp->lc_flags & LCL_NFSV41) != 0) {
4241 error = nfsv4_getcbsession(clp, &sep);
4243 mbuf_freem(nd->nd_mreq);
4248 error = NFSERR_SERVERFAULT;
4249 mbuf_freem(nd->nd_mreq);
4254 * Call newnfs_connect(), as required, and then newnfs_request().
4256 (void) newnfs_sndlock(&clp->lc_req.nr_lock);
4257 if (clp->lc_req.nr_client == NULL) {
4258 if ((clp->lc_flags & LCL_NFSV41) != 0) {
4259 error = ECONNREFUSED;
4260 nfsrv_freesession(sep, NULL);
4261 } else if (nd->nd_procnum == NFSV4PROC_CBNULL)
4262 error = newnfs_connect(NULL, &clp->lc_req, cred,
4265 error = newnfs_connect(NULL, &clp->lc_req, cred,
4268 newnfs_sndunlock(&clp->lc_req.nr_lock);
4270 if ((nd->nd_flag & ND_NFSV41) != 0) {
4271 KASSERT(sep != NULL, ("sep NULL"));
4272 if (sep->sess_cbsess.nfsess_xprt != NULL)
4273 error = newnfs_request(nd, NULL, clp,
4274 &clp->lc_req, NULL, NULL, cred,
4275 clp->lc_program, clp->lc_req.nr_vers, NULL,
4276 1, NULL, &sep->sess_cbsess);
4279 * This should probably never occur, but if a
4280 * client somehow does an RPC without a
4281 * SequenceID Op that causes a callback just
4282 * after the nfsd threads have been terminated
4283 * and restared we could conceivably get here
4284 * without a backchannel xprt.
4286 printf("nfsrv_docallback: no xprt\n");
4287 error = ECONNREFUSED;
4289 nfsrv_freesession(sep, NULL);
4291 error = newnfs_request(nd, NULL, clp, &clp->lc_req,
4292 NULL, NULL, cred, clp->lc_program,
4293 clp->lc_req.nr_vers, NULL, 1, NULL, NULL);
4299 * If error is set here, the Callback path isn't working
4300 * properly, so twiddle the appropriate LCL_ flags.
4301 * (nd_repstat != 0 indicates the Callback path is working,
4302 * but the callback failed on the client.)
4306 * Mark the callback pathway down, which disabled issuing
4307 * of delegations and gets Renew to return NFSERR_CBPATHDOWN.
4310 clp->lc_flags |= LCL_CBDOWN;
4314 * Callback worked. If the callback path was down, disable
4315 * callbacks, so no more delegations will be issued. (This
4316 * is done on the assumption that the callback pathway is
4320 if (clp->lc_flags & LCL_CBDOWN)
4321 clp->lc_flags &= ~(LCL_CBDOWN | LCL_CALLBACKSON);
4324 error = nd->nd_repstat;
4325 else if (error == 0 && procnum == NFSV4OP_CBGETATTR)
4326 error = nfsv4_loadattr(nd, NULL, nap, NULL, NULL, 0,
4327 NULL, NULL, NULL, NULL, NULL, 0, NULL, NULL, NULL,
4329 mbuf_freem(nd->nd_mrep);
4333 if ((clp->lc_flags & LCL_WAKEUPWANTED) && clp->lc_cbref == 0) {
4334 clp->lc_flags &= ~LCL_WAKEUPWANTED;
4344 * Set up the compound RPC for the callback.
4347 nfsrv_cbcallargs(struct nfsrv_descript *nd, struct nfsclient *clp,
4348 uint32_t callback, int op, const char *optag, struct nfsdsession **sepp)
4353 len = strlen(optag);
4354 (void)nfsm_strtom(nd, optag, len);
4355 NFSM_BUILD(tl, uint32_t *, 4 * NFSX_UNSIGNED);
4356 if ((nd->nd_flag & ND_NFSV41) != 0) {
4357 *tl++ = txdr_unsigned(NFSV41_MINORVERSION);
4358 *tl++ = txdr_unsigned(callback);
4359 *tl++ = txdr_unsigned(2);
4360 *tl = txdr_unsigned(NFSV4OP_CBSEQUENCE);
4361 error = nfsv4_setcbsequence(nd, clp, 1, sepp);
4364 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
4365 *tl = txdr_unsigned(op);
4367 *tl++ = txdr_unsigned(NFSV4_MINORVERSION);
4368 *tl++ = txdr_unsigned(callback);
4369 *tl++ = txdr_unsigned(1);
4370 *tl = txdr_unsigned(op);
4376 * Return the next index# for a clientid. Mostly just increment and return
4377 * the next one, but... if the 32bit unsigned does actually wrap around,
4378 * it should be rebooted.
4379 * At an average rate of one new client per second, it will wrap around in
4380 * approximately 136 years. (I think the server will have been shut
4381 * down or rebooted before then.)
4384 nfsrv_nextclientindex(void)
4386 static u_int32_t client_index = 0;
4389 if (client_index != 0)
4390 return (client_index);
4392 printf("%s: out of clientids\n", __func__);
4393 return (client_index);
4397 * Return the next index# for a stateid. Mostly just increment and return
4398 * the next one, but... if the 32bit unsigned does actually wrap around
4399 * (will a BSD server stay up that long?), find
4400 * new start and end values.
4403 nfsrv_nextstateindex(struct nfsclient *clp)
4405 struct nfsstate *stp;
4407 u_int32_t canuse, min_index, max_index;
4409 if (!(clp->lc_flags & LCL_INDEXNOTOK)) {
4410 clp->lc_stateindex++;
4411 if (clp->lc_stateindex != clp->lc_statemaxindex)
4412 return (clp->lc_stateindex);
4416 * Yuck, we've hit the end.
4417 * Look for a new min and max.
4420 max_index = 0xffffffff;
4421 for (i = 0; i < nfsrv_statehashsize; i++) {
4422 LIST_FOREACH(stp, &clp->lc_stateid[i], ls_hash) {
4423 if (stp->ls_stateid.other[2] > 0x80000000) {
4424 if (stp->ls_stateid.other[2] < max_index)
4425 max_index = stp->ls_stateid.other[2];
4427 if (stp->ls_stateid.other[2] > min_index)
4428 min_index = stp->ls_stateid.other[2];
4434 * Yikes, highly unlikely, but I'll handle it anyhow.
4436 if (min_index == 0x80000000 && max_index == 0x80000001) {
4439 * Loop around until we find an unused entry. Return that
4440 * and set LCL_INDEXNOTOK, so the search will continue next time.
4441 * (This is one of those rare cases where a goto is the
4442 * cleanest way to code the loop.)
4445 for (i = 0; i < nfsrv_statehashsize; i++) {
4446 LIST_FOREACH(stp, &clp->lc_stateid[i], ls_hash) {
4447 if (stp->ls_stateid.other[2] == canuse) {
4453 clp->lc_flags |= LCL_INDEXNOTOK;
4458 * Ok to start again from min + 1.
4460 clp->lc_stateindex = min_index + 1;
4461 clp->lc_statemaxindex = max_index;
4462 clp->lc_flags &= ~LCL_INDEXNOTOK;
4463 return (clp->lc_stateindex);
4467 * The following functions handle the stable storage file that deals with
4468 * the edge conditions described in RFC3530 Sec. 8.6.3.
4469 * The file is as follows:
4470 * - a single record at the beginning that has the lease time of the
4471 * previous server instance (before the last reboot) and the nfsrvboottime
4472 * values for the previous server boots.
4473 * These previous boot times are used to ensure that the current
4474 * nfsrvboottime does not, somehow, get set to a previous one.
4475 * (This is important so that Stale ClientIDs and StateIDs can
4477 * The number of previous nfsvrboottime values preceeds the list.
4478 * - followed by some number of appended records with:
4479 * - client id string
4480 * - flag that indicates it is a record revoking state via lease
4481 * expiration or similar
4482 * OR has successfully acquired state.
4483 * These structures vary in length, with the client string at the end, up
4484 * to NFSV4_OPAQUELIMIT in size.
4486 * At the end of the grace period, the file is truncated, the first
4487 * record is rewritten with updated information and any acquired state
4488 * records for successful reclaims of state are written.
4490 * Subsequent records are appended when the first state is issued to
4491 * a client and when state is revoked for a client.
4493 * When reading the file in, state issued records that come later in
4494 * the file override older ones, since the append log is in cronological order.
4495 * If, for some reason, the file can't be read, the grace period is
4496 * immediately terminated and all reclaims get NFSERR_NOGRACE.
4500 * Read in the stable storage file. Called by nfssvc() before the nfsd
4501 * processes start servicing requests.
4504 nfsrv_setupstable(NFSPROC_T *p)
4506 struct nfsrv_stablefirst *sf = &nfsrv_stablefirst;
4507 struct nfsrv_stable *sp, *nsp;
4508 struct nfst_rec *tsp;
4509 int error, i, tryagain;
4511 ssize_t aresid, len;
4514 * If NFSNSF_UPDATEDONE is set, this is a restart of the nfsds without
4515 * a reboot, so state has not been lost.
4517 if (sf->nsf_flags & NFSNSF_UPDATEDONE)
4520 * Set Grace over just until the file reads successfully.
4522 nfsrvboottime = time_second;
4523 LIST_INIT(&sf->nsf_head);
4524 sf->nsf_flags = (NFSNSF_GRACEOVER | NFSNSF_NEEDLOCK);
4525 sf->nsf_eograce = NFSD_MONOSEC + NFSRV_LEASEDELTA;
4526 if (sf->nsf_fp == NULL)
4528 error = NFSD_RDWR(UIO_READ, NFSFPVNODE(sf->nsf_fp),
4529 (caddr_t)&sf->nsf_rec, sizeof (struct nfsf_rec), off, UIO_SYSSPACE,
4530 0, NFSFPCRED(sf->nsf_fp), &aresid, p);
4531 if (error || aresid || sf->nsf_numboots == 0 ||
4532 sf->nsf_numboots > NFSNSF_MAXNUMBOOTS)
4536 * Now, read in the boottimes.
4538 sf->nsf_bootvals = (time_t *)malloc((sf->nsf_numboots + 1) *
4539 sizeof (time_t), M_TEMP, M_WAITOK);
4540 off = sizeof (struct nfsf_rec);
4541 error = NFSD_RDWR(UIO_READ, NFSFPVNODE(sf->nsf_fp),
4542 (caddr_t)sf->nsf_bootvals, sf->nsf_numboots * sizeof (time_t), off,
4543 UIO_SYSSPACE, 0, NFSFPCRED(sf->nsf_fp), &aresid, p);
4544 if (error || aresid) {
4545 free((caddr_t)sf->nsf_bootvals, M_TEMP);
4546 sf->nsf_bootvals = NULL;
4551 * Make sure this nfsrvboottime is different from all recorded
4556 for (i = 0; i < sf->nsf_numboots; i++) {
4557 if (nfsrvboottime == sf->nsf_bootvals[i]) {
4565 sf->nsf_flags |= NFSNSF_OK;
4566 off += (sf->nsf_numboots * sizeof (time_t));
4569 * Read through the file, building a list of records for grace
4571 * Each record is between sizeof (struct nfst_rec) and
4572 * sizeof (struct nfst_rec) + NFSV4_OPAQUELIMIT - 1
4573 * and is actually sizeof (struct nfst_rec) + nst_len - 1.
4575 tsp = (struct nfst_rec *)malloc(sizeof (struct nfst_rec) +
4576 NFSV4_OPAQUELIMIT - 1, M_TEMP, M_WAITOK);
4578 error = NFSD_RDWR(UIO_READ, NFSFPVNODE(sf->nsf_fp),
4579 (caddr_t)tsp, sizeof (struct nfst_rec) + NFSV4_OPAQUELIMIT - 1,
4580 off, UIO_SYSSPACE, 0, NFSFPCRED(sf->nsf_fp), &aresid, p);
4581 len = (sizeof (struct nfst_rec) + NFSV4_OPAQUELIMIT - 1) - aresid;
4582 if (error || (len > 0 && (len < sizeof (struct nfst_rec) ||
4583 len < (sizeof (struct nfst_rec) + tsp->len - 1)))) {
4585 * Yuck, the file has been corrupted, so just return
4586 * after clearing out any restart state, so the grace period
4589 LIST_FOREACH_SAFE(sp, &sf->nsf_head, nst_list, nsp) {
4590 LIST_REMOVE(sp, nst_list);
4591 free((caddr_t)sp, M_TEMP);
4593 free((caddr_t)tsp, M_TEMP);
4594 sf->nsf_flags &= ~NFSNSF_OK;
4595 free((caddr_t)sf->nsf_bootvals, M_TEMP);
4596 sf->nsf_bootvals = NULL;
4600 off += sizeof (struct nfst_rec) + tsp->len - 1;
4602 * Search the list for a matching client.
4604 LIST_FOREACH(sp, &sf->nsf_head, nst_list) {
4605 if (tsp->len == sp->nst_len &&
4606 !NFSBCMP(tsp->client, sp->nst_client, tsp->len))
4609 if (sp == LIST_END(&sf->nsf_head)) {
4610 sp = (struct nfsrv_stable *)malloc(tsp->len +
4611 sizeof (struct nfsrv_stable) - 1, M_TEMP,
4613 NFSBCOPY((caddr_t)tsp, (caddr_t)&sp->nst_rec,
4614 sizeof (struct nfst_rec) + tsp->len - 1);
4615 LIST_INSERT_HEAD(&sf->nsf_head, sp, nst_list);
4617 if (tsp->flag == NFSNST_REVOKE)
4618 sp->nst_flag |= NFSNST_REVOKE;
4621 * A subsequent timestamp indicates the client
4622 * did a setclientid/confirm and any previous
4623 * revoke is no longer relevant.
4625 sp->nst_flag &= ~NFSNST_REVOKE;
4629 free((caddr_t)tsp, M_TEMP);
4630 sf->nsf_flags = NFSNSF_OK;
4631 sf->nsf_eograce = NFSD_MONOSEC + sf->nsf_lease +
4636 * Update the stable storage file, now that the grace period is over.
4639 nfsrv_updatestable(NFSPROC_T *p)
4641 struct nfsrv_stablefirst *sf = &nfsrv_stablefirst;
4642 struct nfsrv_stable *sp, *nsp;
4644 struct nfsvattr nva;
4646 #if defined(__FreeBSD_version) && (__FreeBSD_version >= 500000)
4651 if (sf->nsf_fp == NULL || (sf->nsf_flags & NFSNSF_UPDATEDONE))
4653 sf->nsf_flags |= NFSNSF_UPDATEDONE;
4655 * Ok, we need to rewrite the stable storage file.
4656 * - truncate to 0 length
4657 * - write the new first structure
4658 * - loop through the data structures, writing out any that
4659 * have timestamps older than the old boot
4661 if (sf->nsf_bootvals) {
4663 for (i = sf->nsf_numboots - 2; i >= 0; i--)
4664 sf->nsf_bootvals[i + 1] = sf->nsf_bootvals[i];
4666 sf->nsf_numboots = 1;
4667 sf->nsf_bootvals = (time_t *)malloc(sizeof (time_t),
4670 sf->nsf_bootvals[0] = nfsrvboottime;
4671 sf->nsf_lease = nfsrv_lease;
4672 NFSVNO_ATTRINIT(&nva);
4673 NFSVNO_SETATTRVAL(&nva, size, 0);
4674 vp = NFSFPVNODE(sf->nsf_fp);
4675 vn_start_write(vp, &mp, V_WAIT);
4676 if (NFSVOPLOCK(vp, LK_EXCLUSIVE) == 0) {
4677 error = nfsvno_setattr(vp, &nva, NFSFPCRED(sf->nsf_fp), p,
4679 NFSVOPUNLOCK(vp, 0);
4682 vn_finished_write(mp);
4684 error = NFSD_RDWR(UIO_WRITE, vp,
4685 (caddr_t)&sf->nsf_rec, sizeof (struct nfsf_rec), (off_t)0,
4686 UIO_SYSSPACE, IO_SYNC, NFSFPCRED(sf->nsf_fp), NULL, p);
4688 error = NFSD_RDWR(UIO_WRITE, vp,
4689 (caddr_t)sf->nsf_bootvals,
4690 sf->nsf_numboots * sizeof (time_t),
4691 (off_t)(sizeof (struct nfsf_rec)),
4692 UIO_SYSSPACE, IO_SYNC, NFSFPCRED(sf->nsf_fp), NULL, p);
4693 free((caddr_t)sf->nsf_bootvals, M_TEMP);
4694 sf->nsf_bootvals = NULL;
4696 sf->nsf_flags &= ~NFSNSF_OK;
4697 printf("EEK! Can't write NfsV4 stable storage file\n");
4700 sf->nsf_flags |= NFSNSF_OK;
4703 * Loop through the list and write out timestamp records for
4704 * any clients that successfully reclaimed state.
4706 LIST_FOREACH_SAFE(sp, &sf->nsf_head, nst_list, nsp) {
4707 if (sp->nst_flag & NFSNST_GOTSTATE) {
4708 nfsrv_writestable(sp->nst_client, sp->nst_len,
4709 NFSNST_NEWSTATE, p);
4710 sp->nst_clp->lc_flags |= LCL_STAMPEDSTABLE;
4712 LIST_REMOVE(sp, nst_list);
4713 free((caddr_t)sp, M_TEMP);
4715 nfsrv_backupstable();
4719 * Append a record to the stable storage file.
4722 nfsrv_writestable(u_char *client, int len, int flag, NFSPROC_T *p)
4724 struct nfsrv_stablefirst *sf = &nfsrv_stablefirst;
4725 struct nfst_rec *sp;
4728 if (!(sf->nsf_flags & NFSNSF_OK) || sf->nsf_fp == NULL)
4730 sp = (struct nfst_rec *)malloc(sizeof (struct nfst_rec) +
4731 len - 1, M_TEMP, M_WAITOK);
4733 NFSBCOPY(client, sp->client, len);
4735 error = NFSD_RDWR(UIO_WRITE, NFSFPVNODE(sf->nsf_fp),
4736 (caddr_t)sp, sizeof (struct nfst_rec) + len - 1, (off_t)0,
4737 UIO_SYSSPACE, (IO_SYNC | IO_APPEND), NFSFPCRED(sf->nsf_fp), NULL, p);
4738 free((caddr_t)sp, M_TEMP);
4740 sf->nsf_flags &= ~NFSNSF_OK;
4741 printf("EEK! Can't write NfsV4 stable storage file\n");
4746 * This function is called during the grace period to mark a client
4747 * that successfully reclaimed state.
4750 nfsrv_markstable(struct nfsclient *clp)
4752 struct nfsrv_stable *sp;
4755 * First find the client structure.
4757 LIST_FOREACH(sp, &nfsrv_stablefirst.nsf_head, nst_list) {
4758 if (sp->nst_len == clp->lc_idlen &&
4759 !NFSBCMP(sp->nst_client, clp->lc_id, sp->nst_len))
4762 if (sp == LIST_END(&nfsrv_stablefirst.nsf_head))
4766 * Now, just mark it and set the nfsclient back pointer.
4768 sp->nst_flag |= NFSNST_GOTSTATE;
4773 * This function is called when a NFSv4.1 client does a ReclaimComplete.
4774 * Very similar to nfsrv_markstable(), except for the flag being set.
4777 nfsrv_markreclaim(struct nfsclient *clp)
4779 struct nfsrv_stable *sp;
4782 * First find the client structure.
4784 LIST_FOREACH(sp, &nfsrv_stablefirst.nsf_head, nst_list) {
4785 if (sp->nst_len == clp->lc_idlen &&
4786 !NFSBCMP(sp->nst_client, clp->lc_id, sp->nst_len))
4789 if (sp == LIST_END(&nfsrv_stablefirst.nsf_head))
4793 * Now, just set the flag.
4795 sp->nst_flag |= NFSNST_RECLAIMED;
4799 * This function is called for a reclaim, to see if it gets grace.
4800 * It returns 0 if a reclaim is allowed, 1 otherwise.
4803 nfsrv_checkstable(struct nfsclient *clp)
4805 struct nfsrv_stable *sp;
4808 * First, find the entry for the client.
4810 LIST_FOREACH(sp, &nfsrv_stablefirst.nsf_head, nst_list) {
4811 if (sp->nst_len == clp->lc_idlen &&
4812 !NFSBCMP(sp->nst_client, clp->lc_id, sp->nst_len))
4817 * If not in the list, state was revoked or no state was issued
4818 * since the previous reboot, a reclaim is denied.
4820 if (sp == LIST_END(&nfsrv_stablefirst.nsf_head) ||
4821 (sp->nst_flag & NFSNST_REVOKE) ||
4822 !(nfsrv_stablefirst.nsf_flags & NFSNSF_OK))
4828 * Test for and try to clear out a conflicting client. This is called by
4829 * nfsrv_lockctrl() and nfsrv_openctrl() when conflicts with other clients
4831 * The trick here is that it can't revoke a conflicting client with an
4832 * expired lease unless it holds the v4root lock, so...
4833 * If no v4root lock, get the lock and return 1 to indicate "try again".
4834 * Return 0 to indicate the conflict can't be revoked and 1 to indicate
4835 * the revocation worked and the conflicting client is "bye, bye", so it
4836 * can be tried again.
4837 * Return 2 to indicate that the vnode is VI_DOOMED after NFSVOPLOCK().
4838 * Unlocks State before a non-zero value is returned.
4841 nfsrv_clientconflict(struct nfsclient *clp, int *haslockp, vnode_t vp,
4844 int gotlock, lktype = 0;
4847 * If lease hasn't expired, we can't fix it.
4849 if (clp->lc_expiry >= NFSD_MONOSEC ||
4850 !(nfsrv_stablefirst.nsf_flags & NFSNSF_UPDATEDONE))
4852 if (*haslockp == 0) {
4855 lktype = NFSVOPISLOCKED(vp);
4856 NFSVOPUNLOCK(vp, 0);
4858 NFSLOCKV4ROOTMUTEX();
4859 nfsv4_relref(&nfsv4rootfs_lock);
4861 gotlock = nfsv4_lock(&nfsv4rootfs_lock, 1, NULL,
4862 NFSV4ROOTLOCKMUTEXPTR, NULL);
4864 NFSUNLOCKV4ROOTMUTEX();
4867 NFSVOPLOCK(vp, lktype | LK_RETRY);
4868 if ((vp->v_iflag & VI_DOOMED) != 0)
4876 * Ok, we can expire the conflicting client.
4878 nfsrv_writestable(clp->lc_id, clp->lc_idlen, NFSNST_REVOKE, p);
4879 nfsrv_backupstable();
4880 nfsrv_cleanclient(clp, p);
4881 nfsrv_freedeleglist(&clp->lc_deleg);
4882 nfsrv_freedeleglist(&clp->lc_olddeleg);
4883 LIST_REMOVE(clp, lc_hash);
4884 nfsrv_zapclient(clp, p);
4889 * Resolve a delegation conflict.
4890 * Returns 0 to indicate the conflict was resolved without sleeping.
4891 * Return -1 to indicate that the caller should check for conflicts again.
4892 * Return > 0 for an error that should be returned, normally NFSERR_DELAY.
4894 * Also, manipulate the nfsv4root_lock, as required. It isn't changed
4895 * for a return of 0, since there was no sleep and it could be required
4896 * later. It is released for a return of NFSERR_DELAY, since the caller
4897 * will return that error. It is released when a sleep was done waiting
4898 * for the delegation to be returned or expire (so that other nfsds can
4899 * handle ops). Then, it must be acquired for the write to stable storage.
4900 * (This function is somewhat similar to nfsrv_clientconflict(), but
4901 * the semantics differ in a couple of subtle ways. The return of 0
4902 * indicates the conflict was resolved without sleeping here, not
4903 * that the conflict can't be resolved and the handling of nfsv4root_lock
4904 * differs, as noted above.)
4905 * Unlocks State before returning a non-zero value.
4908 nfsrv_delegconflict(struct nfsstate *stp, int *haslockp, NFSPROC_T *p,
4911 struct nfsclient *clp = stp->ls_clp;
4912 int gotlock, error, lktype = 0, retrycnt, zapped_clp;
4913 nfsv4stateid_t tstateid;
4917 * If the conflict is with an old delegation...
4919 if (stp->ls_flags & NFSLCK_OLDDELEG) {
4921 * You can delete it, if it has expired.
4923 if (clp->lc_delegtime < NFSD_MONOSEC) {
4924 nfsrv_freedeleg(stp);
4931 * During this delay, the old delegation could expire or it
4932 * could be recovered by the client via an Open with
4933 * CLAIM_DELEGATE_PREV.
4934 * Release the nfsv4root_lock, if held.
4938 NFSLOCKV4ROOTMUTEX();
4939 nfsv4_unlock(&nfsv4rootfs_lock, 1);
4940 NFSUNLOCKV4ROOTMUTEX();
4942 error = NFSERR_DELAY;
4947 * It's a current delegation, so:
4948 * - check to see if the delegation has expired
4949 * - if so, get the v4root lock and then expire it
4951 if (!(stp->ls_flags & NFSLCK_DELEGRECALL)) {
4953 * - do a recall callback, since not yet done
4954 * For now, never allow truncate to be set. To use
4955 * truncate safely, it must be guaranteed that the
4956 * Remove, Rename or Setattr with size of 0 will
4957 * succeed and that would require major changes to
4958 * the VFS/Vnode OPs.
4959 * Set the expiry time large enough so that it won't expire
4960 * until after the callback, then set it correctly, once
4961 * the callback is done. (The delegation will now time
4962 * out whether or not the Recall worked ok. The timeout
4963 * will be extended when ops are done on the delegation
4964 * stateid, up to the timelimit.)
4966 stp->ls_delegtime = NFSD_MONOSEC + (2 * nfsrv_lease) +
4968 stp->ls_delegtimelimit = NFSD_MONOSEC + (6 * nfsrv_lease) +
4970 stp->ls_flags |= NFSLCK_DELEGRECALL;
4973 * Loop NFSRV_CBRETRYCNT times while the CBRecall replies
4974 * NFSERR_BADSTATEID or NFSERR_BADHANDLE. This is done
4975 * in order to try and avoid a race that could happen
4976 * when a CBRecall request passed the Open reply with
4977 * the delegation in it when transitting the network.
4978 * Since nfsrv_docallback will sleep, don't use stp after
4981 NFSBCOPY((caddr_t)&stp->ls_stateid, (caddr_t)&tstateid,
4983 NFSBCOPY((caddr_t)&stp->ls_lfp->lf_fh, (caddr_t)&tfh,
4988 NFSLOCKV4ROOTMUTEX();
4989 nfsv4_unlock(&nfsv4rootfs_lock, 1);
4990 NFSUNLOCKV4ROOTMUTEX();
4994 error = nfsrv_docallback(clp, NFSV4OP_CBRECALL,
4995 &tstateid, 0, &tfh, NULL, NULL, p);
4997 } while ((error == NFSERR_BADSTATEID ||
4998 error == NFSERR_BADHANDLE) && retrycnt < NFSV4_CBRETRYCNT);
4999 error = NFSERR_DELAY;
5003 if (clp->lc_expiry >= NFSD_MONOSEC &&
5004 stp->ls_delegtime >= NFSD_MONOSEC) {
5007 * A recall has been done, but it has not yet expired.
5012 NFSLOCKV4ROOTMUTEX();
5013 nfsv4_unlock(&nfsv4rootfs_lock, 1);
5014 NFSUNLOCKV4ROOTMUTEX();
5016 error = NFSERR_DELAY;
5021 * If we don't yet have the lock, just get it and then return,
5022 * since we need that before deleting expired state, such as
5024 * When getting the lock, unlock the vnode, so other nfsds that
5025 * are in progress, won't get stuck waiting for the vnode lock.
5027 if (*haslockp == 0) {
5030 lktype = NFSVOPISLOCKED(vp);
5031 NFSVOPUNLOCK(vp, 0);
5033 NFSLOCKV4ROOTMUTEX();
5034 nfsv4_relref(&nfsv4rootfs_lock);
5036 gotlock = nfsv4_lock(&nfsv4rootfs_lock, 1, NULL,
5037 NFSV4ROOTLOCKMUTEXPTR, NULL);
5039 NFSUNLOCKV4ROOTMUTEX();
5042 NFSVOPLOCK(vp, lktype | LK_RETRY);
5043 if ((vp->v_iflag & VI_DOOMED) != 0) {
5045 NFSLOCKV4ROOTMUTEX();
5046 nfsv4_unlock(&nfsv4rootfs_lock, 1);
5047 NFSUNLOCKV4ROOTMUTEX();
5048 error = NFSERR_PERM;
5058 * Ok, we can delete the expired delegation.
5059 * First, write the Revoke record to stable storage and then
5060 * clear out the conflict.
5061 * Since all other nfsd threads are now blocked, we can safely
5062 * sleep without the state changing.
5064 nfsrv_writestable(clp->lc_id, clp->lc_idlen, NFSNST_REVOKE, p);
5065 nfsrv_backupstable();
5066 if (clp->lc_expiry < NFSD_MONOSEC) {
5067 nfsrv_cleanclient(clp, p);
5068 nfsrv_freedeleglist(&clp->lc_deleg);
5069 nfsrv_freedeleglist(&clp->lc_olddeleg);
5070 LIST_REMOVE(clp, lc_hash);
5073 nfsrv_freedeleg(stp);
5077 nfsrv_zapclient(clp, p);
5086 * Check for a remove allowed, if remove is set to 1 and get rid of
5090 nfsrv_checkremove(vnode_t vp, int remove, NFSPROC_T *p)
5092 struct nfsstate *stp;
5093 struct nfslockfile *lfp;
5094 int error, haslock = 0;
5098 * First, get the lock file structure.
5099 * (A return of -1 means no associated state, so remove ok.)
5101 error = nfsrv_getlockfh(vp, NFSLCK_CHECK, NULL, &nfh, p);
5105 error = nfsrv_getlockfile(NFSLCK_CHECK, NULL, &lfp, &nfh, 0);
5109 NFSLOCKV4ROOTMUTEX();
5110 nfsv4_unlock(&nfsv4rootfs_lock, 1);
5111 NFSUNLOCKV4ROOTMUTEX();
5119 * Now, we must Recall any delegations.
5121 error = nfsrv_cleandeleg(vp, lfp, NULL, &haslock, p);
5124 * nfsrv_cleandeleg() unlocks state for non-zero
5130 NFSLOCKV4ROOTMUTEX();
5131 nfsv4_unlock(&nfsv4rootfs_lock, 1);
5132 NFSUNLOCKV4ROOTMUTEX();
5138 * Now, look for a conflicting open share.
5141 LIST_FOREACH(stp, &lfp->lf_open, ls_file) {
5142 if (stp->ls_flags & NFSLCK_WRITEDENY) {
5143 error = NFSERR_FILEOPEN;
5151 NFSLOCKV4ROOTMUTEX();
5152 nfsv4_unlock(&nfsv4rootfs_lock, 1);
5153 NFSUNLOCKV4ROOTMUTEX();
5162 * Clear out all delegations for the file referred to by lfp.
5163 * May return NFSERR_DELAY, if there will be a delay waiting for
5164 * delegations to expire.
5165 * Returns -1 to indicate it slept while recalling a delegation.
5166 * This function has the side effect of deleting the nfslockfile structure,
5167 * if it no longer has associated state and didn't have to sleep.
5168 * Unlocks State before a non-zero value is returned.
5171 nfsrv_cleandeleg(vnode_t vp, struct nfslockfile *lfp,
5172 struct nfsclient *clp, int *haslockp, NFSPROC_T *p)
5174 struct nfsstate *stp, *nstp;
5177 stp = LIST_FIRST(&lfp->lf_deleg);
5178 while (stp != LIST_END(&lfp->lf_deleg)) {
5179 nstp = LIST_NEXT(stp, ls_file);
5180 if (stp->ls_clp != clp) {
5181 ret = nfsrv_delegconflict(stp, haslockp, p, vp);
5184 * nfsrv_delegconflict() unlocks state
5185 * when it returns non-zero.
5198 * There are certain operations that, when being done outside of NFSv4,
5199 * require that any NFSv4 delegation for the file be recalled.
5200 * This function is to be called for those cases:
5201 * VOP_RENAME() - When a delegation is being recalled for any reason,
5202 * the client may have to do Opens against the server, using the file's
5203 * final component name. If the file has been renamed on the server,
5204 * that component name will be incorrect and the Open will fail.
5205 * VOP_REMOVE() - Theoretically, a client could Open a file after it has
5206 * been removed on the server, if there is a delegation issued to
5207 * that client for the file. I say "theoretically" since clients
5208 * normally do an Access Op before the Open and that Access Op will
5209 * fail with ESTALE. Note that NFSv2 and 3 don't even do Opens, so
5210 * they will detect the file's removal in the same manner. (There is
5211 * one case where RFC3530 allows a client to do an Open without first
5212 * doing an Access Op, which is passage of a check against the ACE
5213 * returned with a Write delegation, but current practice is to ignore
5214 * the ACE and always do an Access Op.)
5215 * Since the functions can only be called with an unlocked vnode, this
5216 * can't be done at this time.
5217 * VOP_ADVLOCK() - When a client holds a delegation, it can issue byte range
5218 * locks locally in the client, which are not visible to the server. To
5219 * deal with this, issuing of delegations for a vnode must be disabled
5220 * and all delegations for the vnode recalled. This is done via the
5221 * second function, using the VV_DISABLEDELEG vflag on the vnode.
5224 nfsd_recalldelegation(vnode_t vp, NFSPROC_T *p)
5230 * First, check to see if the server is currently running and it has
5231 * been called for a regular file when issuing delegations.
5233 if (newnfs_numnfsd == 0 || vp->v_type != VREG ||
5234 nfsrv_issuedelegs == 0)
5237 KASSERT((NFSVOPISLOCKED(vp) != LK_EXCLUSIVE), ("vp %p is locked", vp));
5239 * First, get a reference on the nfsv4rootfs_lock so that an
5240 * exclusive lock cannot be acquired by another thread.
5242 NFSLOCKV4ROOTMUTEX();
5243 nfsv4_getref(&nfsv4rootfs_lock, NULL, NFSV4ROOTLOCKMUTEXPTR, NULL);
5244 NFSUNLOCKV4ROOTMUTEX();
5247 * Now, call nfsrv_checkremove() in a loop while it returns
5248 * NFSERR_DELAY. Return upon any other error or when timed out.
5250 starttime = NFSD_MONOSEC;
5252 if (NFSVOPLOCK(vp, LK_EXCLUSIVE) == 0) {
5253 error = nfsrv_checkremove(vp, 0, p);
5254 NFSVOPUNLOCK(vp, 0);
5257 if (error == NFSERR_DELAY) {
5258 if (NFSD_MONOSEC - starttime > NFS_REMOVETIMEO)
5260 /* Sleep for a short period of time */
5261 (void) nfs_catnap(PZERO, 0, "nfsremove");
5263 } while (error == NFSERR_DELAY);
5264 NFSLOCKV4ROOTMUTEX();
5265 nfsv4_relref(&nfsv4rootfs_lock);
5266 NFSUNLOCKV4ROOTMUTEX();
5270 nfsd_disabledelegation(vnode_t vp, NFSPROC_T *p)
5273 #ifdef VV_DISABLEDELEG
5275 * First, flag issuance of delegations disabled.
5277 atomic_set_long(&vp->v_vflag, VV_DISABLEDELEG);
5281 * Then call nfsd_recalldelegation() to get rid of all extant
5284 nfsd_recalldelegation(vp, p);
5288 * Check for conflicting locks, etc. and then get rid of delegations.
5289 * (At one point I thought that I should get rid of delegations for any
5290 * Setattr, since it could potentially disallow the I/O op (read or write)
5291 * allowed by the delegation. However, Setattr Ops that aren't changing
5292 * the size get a stateid of all 0s, so you can't tell if it is a delegation
5293 * for the same client or a different one, so I decided to only get rid
5294 * of delegations for other clients when the size is being changed.)
5295 * In general, a Setattr can disable NFS I/O Ops that are outstanding, such
5296 * as Write backs, even if there is no delegation, so it really isn't any
5300 nfsrv_checksetattr(vnode_t vp, struct nfsrv_descript *nd,
5301 nfsv4stateid_t *stateidp, struct nfsvattr *nvap, nfsattrbit_t *attrbitp,
5302 struct nfsexstuff *exp, NFSPROC_T *p)
5304 struct nfsstate st, *stp = &st;
5305 struct nfslock lo, *lop = &lo;
5309 if (NFSISSET_ATTRBIT(attrbitp, NFSATTRBIT_SIZE)) {
5310 stp->ls_flags = (NFSLCK_CHECK | NFSLCK_WRITEACCESS);
5311 lop->lo_first = nvap->na_size;
5316 if (NFSISSET_ATTRBIT(attrbitp, NFSATTRBIT_OWNER) ||
5317 NFSISSET_ATTRBIT(attrbitp, NFSATTRBIT_OWNERGROUP) ||
5318 NFSISSET_ATTRBIT(attrbitp, NFSATTRBIT_MODE) ||
5319 NFSISSET_ATTRBIT(attrbitp, NFSATTRBIT_ACL))
5320 stp->ls_flags |= NFSLCK_SETATTR;
5321 if (stp->ls_flags == 0)
5323 lop->lo_end = NFS64BITSSET;
5324 lop->lo_flags = NFSLCK_WRITE;
5325 stp->ls_ownerlen = 0;
5327 stp->ls_uid = nd->nd_cred->cr_uid;
5328 stp->ls_stateid.seqid = stateidp->seqid;
5329 clientid.lval[0] = stp->ls_stateid.other[0] = stateidp->other[0];
5330 clientid.lval[1] = stp->ls_stateid.other[1] = stateidp->other[1];
5331 stp->ls_stateid.other[2] = stateidp->other[2];
5332 error = nfsrv_lockctrl(vp, &stp, &lop, NULL, clientid,
5333 stateidp, exp, nd, p);
5336 NFSEXITCODE2(error, nd);
5341 * Check for a write delegation and do a CBGETATTR if there is one, updating
5342 * the attributes, as required.
5343 * Should I return an error if I can't get the attributes? (For now, I'll
5347 nfsrv_checkgetattr(struct nfsrv_descript *nd, vnode_t vp,
5348 struct nfsvattr *nvap, nfsattrbit_t *attrbitp, struct ucred *cred,
5351 struct nfsstate *stp;
5352 struct nfslockfile *lfp;
5353 struct nfsclient *clp;
5354 struct nfsvattr nva;
5357 nfsattrbit_t cbbits;
5358 u_quad_t delegfilerev;
5360 NFSCBGETATTR_ATTRBIT(attrbitp, &cbbits);
5361 if (!NFSNONZERO_ATTRBIT(&cbbits))
5363 if (nfsrv_writedelegcnt == 0)
5367 * Get the lock file structure.
5368 * (A return of -1 means no associated state, so return ok.)
5370 error = nfsrv_getlockfh(vp, NFSLCK_CHECK, NULL, &nfh, p);
5373 error = nfsrv_getlockfile(NFSLCK_CHECK, NULL, &lfp, &nfh, 0);
5382 * Now, look for a write delegation.
5384 LIST_FOREACH(stp, &lfp->lf_deleg, ls_file) {
5385 if (stp->ls_flags & NFSLCK_DELEGWRITE)
5388 if (stp == LIST_END(&lfp->lf_deleg)) {
5393 delegfilerev = stp->ls_filerev;
5396 * If the Write delegation was issued as a part of this Compound RPC
5397 * or if we have an Implied Clientid (used in a previous Op in this
5398 * compound) and it is the client the delegation was issued to,
5400 * I also assume that it is from the same client iff the network
5401 * host IP address is the same as the callback address. (Not
5402 * exactly correct by the RFC, but avoids a lot of Getattr
5405 if (nd->nd_compref == stp->ls_compref ||
5406 ((nd->nd_flag & ND_IMPLIEDCLID) &&
5407 clp->lc_clientid.qval == nd->nd_clientid.qval) ||
5408 nfsaddr2_match(clp->lc_req.nr_nam, nd->nd_nam)) {
5414 * We are now done with the delegation state structure,
5415 * so the statelock can be released and we can now tsleep().
5419 * Now, we must do the CB Getattr callback, to see if Change or Size
5422 if (clp->lc_expiry >= NFSD_MONOSEC) {
5424 NFSVNO_ATTRINIT(&nva);
5425 nva.na_filerev = NFS64BITSSET;
5426 error = nfsrv_docallback(clp, NFSV4OP_CBGETATTR, NULL,
5427 0, &nfh, &nva, &cbbits, p);
5429 if ((nva.na_filerev != NFS64BITSSET &&
5430 nva.na_filerev > delegfilerev) ||
5431 (NFSVNO_ISSETSIZE(&nva) &&
5432 nva.na_size != nvap->na_size)) {
5433 error = nfsvno_updfilerev(vp, nvap, cred, p);
5434 if (NFSVNO_ISSETSIZE(&nva))
5435 nvap->na_size = nva.na_size;
5438 error = 0; /* Ignore callback errors for now. */
5444 NFSEXITCODE2(error, nd);
5449 * This function looks for openowners that haven't had any opens for
5450 * a while and throws them away. Called by an nfsd when NFSNSF_NOOPENS
5454 nfsrv_throwawayopens(NFSPROC_T *p)
5456 struct nfsclient *clp, *nclp;
5457 struct nfsstate *stp, *nstp;
5461 nfsrv_stablefirst.nsf_flags &= ~NFSNSF_NOOPENS;
5463 * For each client...
5465 for (i = 0; i < nfsrv_clienthashsize; i++) {
5466 LIST_FOREACH_SAFE(clp, &nfsclienthash[i], lc_hash, nclp) {
5467 LIST_FOREACH_SAFE(stp, &clp->lc_open, ls_list, nstp) {
5468 if (LIST_EMPTY(&stp->ls_open) &&
5469 (stp->ls_noopens > NFSNOOPEN ||
5470 (nfsrv_openpluslock * 2) >
5471 nfsrv_v4statelimit))
5472 nfsrv_freeopenowner(stp, 0, p);
5480 * This function checks to see if the credentials are the same.
5481 * Returns 1 for not same, 0 otherwise.
5484 nfsrv_notsamecredname(struct nfsrv_descript *nd, struct nfsclient *clp)
5487 if (nd->nd_flag & ND_GSS) {
5488 if (!(clp->lc_flags & LCL_GSS))
5490 if (clp->lc_flags & LCL_NAME) {
5491 if (nd->nd_princlen != clp->lc_namelen ||
5492 NFSBCMP(nd->nd_principal, clp->lc_name,
5498 if (nd->nd_cred->cr_uid == clp->lc_uid)
5502 } else if (clp->lc_flags & LCL_GSS)
5505 * For AUTH_SYS, allow the same uid or root. (This is underspecified
5506 * in RFC3530, which talks about principals, but doesn't say anything
5507 * about uids for AUTH_SYS.)
5509 if (nd->nd_cred->cr_uid == clp->lc_uid || nd->nd_cred->cr_uid == 0)
5516 * Calculate the lease expiry time.
5519 nfsrv_leaseexpiry(void)
5522 if (nfsrv_stablefirst.nsf_eograce > NFSD_MONOSEC)
5523 return (NFSD_MONOSEC + 2 * (nfsrv_lease + NFSRV_LEASEDELTA));
5524 return (NFSD_MONOSEC + nfsrv_lease + NFSRV_LEASEDELTA);
5528 * Delay the delegation timeout as far as ls_delegtimelimit, as required.
5531 nfsrv_delaydelegtimeout(struct nfsstate *stp)
5534 if ((stp->ls_flags & NFSLCK_DELEGRECALL) == 0)
5537 if ((stp->ls_delegtime + 15) > NFSD_MONOSEC &&
5538 stp->ls_delegtime < stp->ls_delegtimelimit) {
5539 stp->ls_delegtime += nfsrv_lease;
5540 if (stp->ls_delegtime > stp->ls_delegtimelimit)
5541 stp->ls_delegtime = stp->ls_delegtimelimit;
5546 * This function checks to see if there is any other state associated
5547 * with the openowner for this Open.
5548 * It returns 1 if there is no other state, 0 otherwise.
5551 nfsrv_nootherstate(struct nfsstate *stp)
5553 struct nfsstate *tstp;
5555 LIST_FOREACH(tstp, &stp->ls_openowner->ls_open, ls_list) {
5556 if (tstp != stp || !LIST_EMPTY(&tstp->ls_lock))
5563 * Create a list of lock deltas (changes to local byte range locking
5564 * that can be rolled back using the list) and apply the changes via
5565 * nfsvno_advlock(). Optionally, lock the list. It is expected that either
5566 * the rollback or update function will be called after this.
5567 * It returns an error (and rolls back, as required), if any nfsvno_advlock()
5568 * call fails. If it returns an error, it will unlock the list.
5571 nfsrv_locallock(vnode_t vp, struct nfslockfile *lfp, int flags,
5572 uint64_t first, uint64_t end, struct nfslockconflict *cfp, NFSPROC_T *p)
5574 struct nfslock *lop, *nlop;
5577 /* Loop through the list of locks. */
5578 lop = LIST_FIRST(&lfp->lf_locallock);
5579 while (first < end && lop != NULL) {
5580 nlop = LIST_NEXT(lop, lo_lckowner);
5581 if (first >= lop->lo_end) {
5584 } else if (first < lop->lo_first) {
5585 /* new one starts before entry in list */
5586 if (end <= lop->lo_first) {
5587 /* no overlap between old and new */
5588 error = nfsrv_dolocal(vp, lfp, flags,
5589 NFSLCK_UNLOCK, first, end, cfp, p);
5594 /* handle fragment overlapped with new one */
5595 error = nfsrv_dolocal(vp, lfp, flags,
5596 NFSLCK_UNLOCK, first, lop->lo_first, cfp,
5600 first = lop->lo_first;
5603 /* new one overlaps this entry in list */
5604 if (end <= lop->lo_end) {
5605 /* overlaps all of new one */
5606 error = nfsrv_dolocal(vp, lfp, flags,
5607 lop->lo_flags, first, end, cfp, p);
5612 /* handle fragment overlapped with new one */
5613 error = nfsrv_dolocal(vp, lfp, flags,
5614 lop->lo_flags, first, lop->lo_end, cfp, p);
5617 first = lop->lo_end;
5622 if (first < end && error == 0)
5623 /* handle fragment past end of list */
5624 error = nfsrv_dolocal(vp, lfp, flags, NFSLCK_UNLOCK, first,
5632 * Local lock unlock. Unlock all byte ranges that are no longer locked
5633 * by NFSv4. To do this, unlock any subranges of first-->end that
5634 * do not overlap with the byte ranges of any lock in the lfp->lf_lock
5635 * list. This list has all locks for the file held by other
5636 * <clientid, lockowner> tuples. The list is ordered by increasing
5637 * lo_first value, but may have entries that overlap each other, for
5638 * the case of read locks.
5641 nfsrv_localunlock(vnode_t vp, struct nfslockfile *lfp, uint64_t init_first,
5642 uint64_t init_end, NFSPROC_T *p)
5644 struct nfslock *lop;
5645 uint64_t first, end, prevfirst;
5649 while (first < init_end) {
5650 /* Loop through all nfs locks, adjusting first and end */
5652 LIST_FOREACH(lop, &lfp->lf_lock, lo_lckfile) {
5653 KASSERT(prevfirst <= lop->lo_first,
5654 ("nfsv4 locks out of order"));
5655 KASSERT(lop->lo_first < lop->lo_end,
5656 ("nfsv4 bogus lock"));
5657 prevfirst = lop->lo_first;
5658 if (first >= lop->lo_first &&
5659 first < lop->lo_end)
5661 * Overlaps with initial part, so trim
5662 * off that initial part by moving first past
5665 first = lop->lo_end;
5666 else if (end > lop->lo_first &&
5667 lop->lo_first > first) {
5669 * This lock defines the end of the
5670 * segment to unlock, so set end to the
5671 * start of it and break out of the loop.
5673 end = lop->lo_first;
5678 * There is no segment left to do, so
5679 * break out of this loop and then exit
5680 * the outer while() since first will be set
5681 * to end, which must equal init_end here.
5686 /* Unlock this segment */
5687 (void) nfsrv_dolocal(vp, lfp, NFSLCK_UNLOCK,
5688 NFSLCK_READ, first, end, NULL, p);
5689 nfsrv_locallock_commit(lfp, NFSLCK_UNLOCK,
5693 * Now move past this segment and look for any further
5694 * segment in the range, if there is one.
5702 * Do the local lock operation and update the rollback list, as required.
5703 * Perform the rollback and return the error if nfsvno_advlock() fails.
5706 nfsrv_dolocal(vnode_t vp, struct nfslockfile *lfp, int flags, int oldflags,
5707 uint64_t first, uint64_t end, struct nfslockconflict *cfp, NFSPROC_T *p)
5709 struct nfsrollback *rlp;
5710 int error = 0, ltype, oldltype;
5712 if (flags & NFSLCK_WRITE)
5714 else if (flags & NFSLCK_READ)
5718 if (oldflags & NFSLCK_WRITE)
5720 else if (oldflags & NFSLCK_READ)
5724 if (ltype == oldltype || (oldltype == F_WRLCK && ltype == F_RDLCK))
5727 error = nfsvno_advlock(vp, ltype, first, end, p);
5730 cfp->cl_clientid.lval[0] = 0;
5731 cfp->cl_clientid.lval[1] = 0;
5733 cfp->cl_end = NFS64BITSSET;
5734 cfp->cl_flags = NFSLCK_WRITE;
5735 cfp->cl_ownerlen = 5;
5736 NFSBCOPY("LOCAL", cfp->cl_owner, 5);
5738 nfsrv_locallock_rollback(vp, lfp, p);
5739 } else if (ltype != F_UNLCK) {
5740 rlp = malloc(sizeof (struct nfsrollback), M_NFSDROLLBACK,
5742 rlp->rlck_first = first;
5743 rlp->rlck_end = end;
5744 rlp->rlck_type = oldltype;
5745 LIST_INSERT_HEAD(&lfp->lf_rollback, rlp, rlck_list);
5754 * Roll back local lock changes and free up the rollback list.
5757 nfsrv_locallock_rollback(vnode_t vp, struct nfslockfile *lfp, NFSPROC_T *p)
5759 struct nfsrollback *rlp, *nrlp;
5761 LIST_FOREACH_SAFE(rlp, &lfp->lf_rollback, rlck_list, nrlp) {
5762 (void) nfsvno_advlock(vp, rlp->rlck_type, rlp->rlck_first,
5764 free(rlp, M_NFSDROLLBACK);
5766 LIST_INIT(&lfp->lf_rollback);
5770 * Update local lock list and delete rollback list (ie now committed to the
5771 * local locks). Most of the work is done by the internal function.
5774 nfsrv_locallock_commit(struct nfslockfile *lfp, int flags, uint64_t first,
5777 struct nfsrollback *rlp, *nrlp;
5778 struct nfslock *new_lop, *other_lop;
5780 new_lop = malloc(sizeof (struct nfslock), M_NFSDLOCK, M_WAITOK);
5781 if (flags & (NFSLCK_READ | NFSLCK_WRITE))
5782 other_lop = malloc(sizeof (struct nfslock), M_NFSDLOCK,
5786 new_lop->lo_flags = flags;
5787 new_lop->lo_first = first;
5788 new_lop->lo_end = end;
5789 nfsrv_updatelock(NULL, &new_lop, &other_lop, lfp);
5790 if (new_lop != NULL)
5791 free(new_lop, M_NFSDLOCK);
5792 if (other_lop != NULL)
5793 free(other_lop, M_NFSDLOCK);
5795 /* and get rid of the rollback list */
5796 LIST_FOREACH_SAFE(rlp, &lfp->lf_rollback, rlck_list, nrlp)
5797 free(rlp, M_NFSDROLLBACK);
5798 LIST_INIT(&lfp->lf_rollback);
5802 * Lock the struct nfslockfile for local lock updating.
5805 nfsrv_locklf(struct nfslockfile *lfp)
5809 /* lf_usecount ensures *lfp won't be free'd */
5812 gotlock = nfsv4_lock(&lfp->lf_locallock_lck, 1, NULL,
5813 NFSSTATEMUTEXPTR, NULL);
5814 } while (gotlock == 0);
5819 * Unlock the struct nfslockfile after local lock updating.
5822 nfsrv_unlocklf(struct nfslockfile *lfp)
5825 nfsv4_unlock(&lfp->lf_locallock_lck, 0);
5829 * Clear out all state for the NFSv4 server.
5830 * Must be called by a thread that can sleep when no nfsds are running.
5833 nfsrv_throwawayallstate(NFSPROC_T *p)
5835 struct nfsclient *clp, *nclp;
5836 struct nfslockfile *lfp, *nlfp;
5840 * For each client, clean out the state and then free the structure.
5842 for (i = 0; i < nfsrv_clienthashsize; i++) {
5843 LIST_FOREACH_SAFE(clp, &nfsclienthash[i], lc_hash, nclp) {
5844 nfsrv_cleanclient(clp, p);
5845 nfsrv_freedeleglist(&clp->lc_deleg);
5846 nfsrv_freedeleglist(&clp->lc_olddeleg);
5847 free(clp->lc_stateid, M_NFSDCLIENT);
5848 free(clp, M_NFSDCLIENT);
5853 * Also, free up any remaining lock file structures.
5855 for (i = 0; i < nfsrv_lockhashsize; i++) {
5856 LIST_FOREACH_SAFE(lfp, &nfslockhash[i], lf_hash, nlfp) {
5857 printf("nfsd unload: fnd a lock file struct\n");
5858 nfsrv_freenfslockfile(lfp);
5864 * Check the sequence# for the session and slot provided as an argument.
5865 * Also, renew the lease if the session will return NFS_OK.
5868 nfsrv_checksequence(struct nfsrv_descript *nd, uint32_t sequenceid,
5869 uint32_t *highest_slotidp, uint32_t *target_highest_slotidp, int cache_this,
5870 uint32_t *sflagsp, NFSPROC_T *p)
5872 struct nfsdsession *sep;
5873 struct nfssessionhash *shp;
5877 shp = NFSSESSIONHASH(nd->nd_sessionid);
5878 NFSLOCKSESSION(shp);
5879 sep = nfsrv_findsession(nd->nd_sessionid);
5881 NFSUNLOCKSESSION(shp);
5882 return (NFSERR_BADSESSION);
5884 error = nfsv4_seqsession(sequenceid, nd->nd_slotid, *highest_slotidp,
5885 sep->sess_slots, NULL, NFSV4_SLOTS - 1);
5887 NFSUNLOCKSESSION(shp);
5890 if (cache_this != 0)
5891 nd->nd_flag |= ND_SAVEREPLY;
5892 /* Renew the lease. */
5893 sep->sess_clp->lc_expiry = nfsrv_leaseexpiry();
5894 nd->nd_clientid.qval = sep->sess_clp->lc_clientid.qval;
5895 nd->nd_flag |= ND_IMPLIEDCLID;
5898 * If this session handles the backchannel, save the nd_xprt for this
5899 * RPC, since this is the one being used.
5900 * RFC-5661 specifies that the fore channel will be implicitly
5901 * bound by a Sequence operation. However, since some NFSv4.1 clients
5902 * erroneously assumed that the back channel would be implicitly
5903 * bound as well, do the implicit binding unless a
5904 * BindConnectiontoSession has already been done on the session.
5906 if (sep->sess_clp->lc_req.nr_client != NULL &&
5907 sep->sess_cbsess.nfsess_xprt != nd->nd_xprt &&
5908 (sep->sess_crflags & NFSV4CRSESS_CONNBACKCHAN) != 0 &&
5909 (sep->sess_clp->lc_flags & LCL_DONEBINDCONN) == 0) {
5911 "nfsrv_checksequence: implicit back channel bind\n");
5912 savxprt = sep->sess_cbsess.nfsess_xprt;
5913 SVC_ACQUIRE(nd->nd_xprt);
5914 nd->nd_xprt->xp_p2 =
5915 sep->sess_clp->lc_req.nr_client->cl_private;
5916 nd->nd_xprt->xp_idletimeout = 0; /* Disable timeout. */
5917 sep->sess_cbsess.nfsess_xprt = nd->nd_xprt;
5918 if (savxprt != NULL)
5919 SVC_RELEASE(savxprt);
5923 if (sep->sess_clp->lc_req.nr_client == NULL)
5924 *sflagsp |= NFSV4SEQ_CBPATHDOWN;
5925 NFSUNLOCKSESSION(shp);
5926 if (error == NFSERR_EXPIRED) {
5927 *sflagsp |= NFSV4SEQ_EXPIREDALLSTATEREVOKED;
5929 } else if (error == NFSERR_ADMINREVOKED) {
5930 *sflagsp |= NFSV4SEQ_ADMINSTATEREVOKED;
5933 *highest_slotidp = *target_highest_slotidp = NFSV4_SLOTS - 1;
5938 * Check/set reclaim complete for this session/clientid.
5941 nfsrv_checkreclaimcomplete(struct nfsrv_descript *nd)
5943 struct nfsdsession *sep;
5944 struct nfssessionhash *shp;
5947 shp = NFSSESSIONHASH(nd->nd_sessionid);
5949 NFSLOCKSESSION(shp);
5950 sep = nfsrv_findsession(nd->nd_sessionid);
5952 NFSUNLOCKSESSION(shp);
5954 return (NFSERR_BADSESSION);
5957 /* Check to see if reclaim complete has already happened. */
5958 if ((sep->sess_clp->lc_flags & LCL_RECLAIMCOMPLETE) != 0)
5959 error = NFSERR_COMPLETEALREADY;
5961 sep->sess_clp->lc_flags |= LCL_RECLAIMCOMPLETE;
5962 nfsrv_markreclaim(sep->sess_clp);
5964 NFSUNLOCKSESSION(shp);
5970 * Cache the reply in a session slot.
5973 nfsrv_cache_session(uint8_t *sessionid, uint32_t slotid, int repstat,
5976 struct nfsdsession *sep;
5977 struct nfssessionhash *shp;
5979 shp = NFSSESSIONHASH(sessionid);
5980 NFSLOCKSESSION(shp);
5981 sep = nfsrv_findsession(sessionid);
5983 NFSUNLOCKSESSION(shp);
5984 printf("nfsrv_cache_session: no session\n");
5988 nfsv4_seqsess_cacherep(slotid, sep->sess_slots, repstat, m);
5989 NFSUNLOCKSESSION(shp);
5993 * Search for a session that matches the sessionid.
5995 static struct nfsdsession *
5996 nfsrv_findsession(uint8_t *sessionid)
5998 struct nfsdsession *sep;
5999 struct nfssessionhash *shp;
6001 shp = NFSSESSIONHASH(sessionid);
6002 LIST_FOREACH(sep, &shp->list, sess_hash) {
6003 if (!NFSBCMP(sessionid, sep->sess_sessionid, NFSX_V4SESSIONID))
6010 * Destroy a session.
6013 nfsrv_destroysession(struct nfsrv_descript *nd, uint8_t *sessionid)
6015 int error, igotlock, samesess;
6018 if (!NFSBCMP(sessionid, nd->nd_sessionid, NFSX_V4SESSIONID) &&
6019 (nd->nd_flag & ND_HASSEQUENCE) != 0) {
6021 if ((nd->nd_flag & ND_LASTOP) == 0)
6022 return (NFSERR_BADSESSION);
6025 /* Lock out other nfsd threads */
6026 NFSLOCKV4ROOTMUTEX();
6027 nfsv4_relref(&nfsv4rootfs_lock);
6029 igotlock = nfsv4_lock(&nfsv4rootfs_lock, 1, NULL,
6030 NFSV4ROOTLOCKMUTEXPTR, NULL);
6031 } while (igotlock == 0);
6032 NFSUNLOCKV4ROOTMUTEX();
6034 error = nfsrv_freesession(NULL, sessionid);
6035 if (error == 0 && samesess != 0)
6036 nd->nd_flag &= ~ND_HASSEQUENCE;
6038 NFSLOCKV4ROOTMUTEX();
6039 nfsv4_unlock(&nfsv4rootfs_lock, 1);
6040 NFSUNLOCKV4ROOTMUTEX();
6045 * Bind a connection to a session.
6046 * For now, only certain variants are supported, since the current session
6047 * structure can only handle a single backchannel entry, which will be
6048 * applied to all connections if it is set.
6051 nfsrv_bindconnsess(struct nfsrv_descript *nd, uint8_t *sessionid, int *foreaftp)
6053 struct nfssessionhash *shp;
6054 struct nfsdsession *sep;
6055 struct nfsclient *clp;
6060 shp = NFSSESSIONHASH(sessionid);
6062 NFSLOCKSESSION(shp);
6063 sep = nfsrv_findsession(sessionid);
6065 clp = sep->sess_clp;
6066 if (*foreaftp == NFSCDFC4_BACK ||
6067 *foreaftp == NFSCDFC4_BACK_OR_BOTH ||
6068 *foreaftp == NFSCDFC4_FORE_OR_BOTH) {
6069 /* Try to set up a backchannel. */
6070 if (clp->lc_req.nr_client == NULL) {
6071 NFSD_DEBUG(2, "nfsrv_bindconnsess: acquire "
6073 clp->lc_req.nr_client = (struct __rpc_client *)
6074 clnt_bck_create(nd->nd_xprt->xp_socket,
6075 sep->sess_cbprogram, NFSV4_CBVERS);
6077 if (clp->lc_req.nr_client != NULL) {
6078 NFSD_DEBUG(2, "nfsrv_bindconnsess: set up "
6080 savxprt = sep->sess_cbsess.nfsess_xprt;
6081 SVC_ACQUIRE(nd->nd_xprt);
6082 nd->nd_xprt->xp_p2 =
6083 clp->lc_req.nr_client->cl_private;
6084 /* Disable idle timeout. */
6085 nd->nd_xprt->xp_idletimeout = 0;
6086 sep->sess_cbsess.nfsess_xprt = nd->nd_xprt;
6087 if (savxprt != NULL)
6088 SVC_RELEASE(savxprt);
6089 sep->sess_crflags |= NFSV4CRSESS_CONNBACKCHAN;
6090 clp->lc_flags |= LCL_DONEBINDCONN;
6091 if (*foreaftp == NFSCDFS4_BACK)
6092 *foreaftp = NFSCDFS4_BACK;
6094 *foreaftp = NFSCDFS4_BOTH;
6095 } else if (*foreaftp != NFSCDFC4_BACK) {
6096 NFSD_DEBUG(2, "nfsrv_bindconnsess: can't set "
6097 "up backchannel\n");
6098 sep->sess_crflags &= ~NFSV4CRSESS_CONNBACKCHAN;
6099 clp->lc_flags |= LCL_DONEBINDCONN;
6100 *foreaftp = NFSCDFS4_FORE;
6102 error = NFSERR_NOTSUPP;
6103 printf("nfsrv_bindconnsess: Can't add "
6107 NFSD_DEBUG(2, "nfsrv_bindconnsess: Set forechannel\n");
6108 clp->lc_flags |= LCL_DONEBINDCONN;
6109 *foreaftp = NFSCDFS4_FORE;
6112 error = NFSERR_BADSESSION;
6113 NFSUNLOCKSESSION(shp);
6119 * Free up a session structure.
6122 nfsrv_freesession(struct nfsdsession *sep, uint8_t *sessionid)
6124 struct nfssessionhash *shp;
6129 shp = NFSSESSIONHASH(sessionid);
6130 NFSLOCKSESSION(shp);
6131 sep = nfsrv_findsession(sessionid);
6133 shp = NFSSESSIONHASH(sep->sess_sessionid);
6134 NFSLOCKSESSION(shp);
6138 if (sep->sess_refcnt > 0) {
6139 NFSUNLOCKSESSION(shp);
6141 return (NFSERR_BACKCHANBUSY);
6143 LIST_REMOVE(sep, sess_hash);
6144 LIST_REMOVE(sep, sess_list);
6146 NFSUNLOCKSESSION(shp);
6149 return (NFSERR_BADSESSION);
6150 for (i = 0; i < NFSV4_SLOTS; i++)
6151 if (sep->sess_slots[i].nfssl_reply != NULL)
6152 m_freem(sep->sess_slots[i].nfssl_reply);
6153 if (sep->sess_cbsess.nfsess_xprt != NULL)
6154 SVC_RELEASE(sep->sess_cbsess.nfsess_xprt);
6155 free(sep, M_NFSDSESSION);
6161 * RFC5661 says that it should fail when there are associated opens, locks
6162 * or delegations. Since stateids represent opens, I don't see how you can
6163 * free an open stateid (it will be free'd when closed), so this function
6164 * only works for lock stateids (freeing the lock_owner) or delegations.
6167 nfsrv_freestateid(struct nfsrv_descript *nd, nfsv4stateid_t *stateidp,
6170 struct nfsclient *clp;
6171 struct nfsstate *stp;
6176 * Look up the stateid
6178 error = nfsrv_getclient((nfsquad_t)((u_quad_t)0), CLOPS_RENEW, &clp,
6179 NULL, (nfsquad_t)((u_quad_t)0), 0, nd, p);
6181 /* First, check for a delegation. */
6182 LIST_FOREACH(stp, &clp->lc_deleg, ls_list) {
6183 if (!NFSBCMP(stp->ls_stateid.other, stateidp->other,
6188 nfsrv_freedeleg(stp);
6193 /* Not a delegation, try for a lock_owner. */
6195 error = nfsrv_getstate(clp, stateidp, 0, &stp);
6196 if (error == 0 && ((stp->ls_flags & (NFSLCK_OPEN | NFSLCK_DELEGREAD |
6197 NFSLCK_DELEGWRITE)) != 0 || (stp->ls_flags & NFSLCK_LOCK) == 0))
6198 /* Not a lock_owner stateid. */
6199 error = NFSERR_LOCKSHELD;
6200 if (error == 0 && !LIST_EMPTY(&stp->ls_lock))
6201 error = NFSERR_LOCKSHELD;
6203 nfsrv_freelockowner(stp, NULL, 0, p);
6212 nfsrv_teststateid(struct nfsrv_descript *nd, nfsv4stateid_t *stateidp,
6215 struct nfsclient *clp;
6216 struct nfsstate *stp;
6221 * Look up the stateid
6223 error = nfsrv_getclient((nfsquad_t)((u_quad_t)0), CLOPS_RENEW, &clp,
6224 NULL, (nfsquad_t)((u_quad_t)0), 0, nd, p);
6226 error = nfsrv_getstate(clp, stateidp, 0, &stp);
6227 if (error == 0 && stateidp->seqid != 0 &&
6228 SEQ_LT(stateidp->seqid, stp->ls_stateid.seqid))
6229 error = NFSERR_OLDSTATEID;
6235 * Generate the xdr for an NFSv4.1 CBSequence Operation.
6238 nfsv4_setcbsequence(struct nfsrv_descript *nd, struct nfsclient *clp,
6239 int dont_replycache, struct nfsdsession **sepp)
6241 struct nfsdsession *sep;
6242 uint32_t *tl, slotseq = 0;
6243 int maxslot, slotpos;
6244 uint8_t sessionid[NFSX_V4SESSIONID];
6247 error = nfsv4_getcbsession(clp, sepp);
6251 (void)nfsv4_sequencelookup(NULL, &sep->sess_cbsess, &slotpos, &maxslot,
6252 &slotseq, sessionid);
6253 KASSERT(maxslot >= 0, ("nfsv4_setcbsequence neg maxslot"));
6255 /* Build the Sequence arguments. */
6256 NFSM_BUILD(tl, uint32_t *, NFSX_V4SESSIONID + 5 * NFSX_UNSIGNED);
6257 bcopy(sessionid, tl, NFSX_V4SESSIONID);
6258 tl += NFSX_V4SESSIONID / NFSX_UNSIGNED;
6259 nd->nd_slotseq = tl;
6260 *tl++ = txdr_unsigned(slotseq);
6261 *tl++ = txdr_unsigned(slotpos);
6262 *tl++ = txdr_unsigned(maxslot);
6263 if (dont_replycache == 0)
6264 *tl++ = newnfs_true;
6266 *tl++ = newnfs_false;
6267 *tl = 0; /* No referring call list, for now. */
6268 nd->nd_flag |= ND_HASSEQUENCE;
6273 * Get a session for the callback.
6276 nfsv4_getcbsession(struct nfsclient *clp, struct nfsdsession **sepp)
6278 struct nfsdsession *sep;
6281 LIST_FOREACH(sep, &clp->lc_session, sess_list) {
6282 if ((sep->sess_crflags & NFSV4CRSESS_CONNBACKCHAN) != 0)
6287 return (NFSERR_BADSESSION);
6296 * Free up all backchannel xprts. This needs to be done when the nfsd threads
6297 * exit, since those transports will all be going away.
6298 * This is only called after all the nfsd threads are done performing RPCs,
6299 * so locking shouldn't be an issue.
6302 nfsrv_freeallbackchannel_xprts(void)
6304 struct nfsdsession *sep;
6305 struct nfsclient *clp;
6309 for (i = 0; i < nfsrv_clienthashsize; i++) {
6310 LIST_FOREACH(clp, &nfsclienthash[i], lc_hash) {
6311 LIST_FOREACH(sep, &clp->lc_session, sess_list) {
6312 xprt = sep->sess_cbsess.nfsess_xprt;
6313 sep->sess_cbsess.nfsess_xprt = NULL;