2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2009 Rick Macklem, University of Guelph
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
34 #include "opt_inet6.h"
35 #include <sys/extattr.h>
36 #include <fs/nfs/nfsport.h>
38 struct nfsrv_stablefirst nfsrv_stablefirst;
39 int nfsrv_issuedelegs = 0;
40 int nfsrv_dolocallocks = 0;
41 struct nfsv4lock nfsv4rootfs_lock;
42 time_t nfsdev_time = 0;
43 int nfsrv_layouthashsize;
44 volatile int nfsrv_layoutcnt = 0;
45 extern uint32_t nfs_srvmaxio;
47 extern int newnfs_numnfsd;
48 extern struct nfsstatsv1 nfsstatsv1;
49 extern int nfsrv_lease;
50 extern struct timeval nfsboottime;
51 extern u_int32_t newnfs_true, newnfs_false;
52 extern struct mtx nfsrv_dslock_mtx;
53 extern struct mtx nfsrv_recalllock_mtx;
54 extern struct mtx nfsrv_dontlistlock_mtx;
55 extern int nfsd_debuglevel;
56 extern u_int nfsrv_dsdirsize;
57 extern struct nfsdevicehead nfsrv_devidhead;
58 extern int nfsrv_doflexfile;
59 extern int nfsrv_maxpnfsmirror;
62 extern struct nfsdontlisthead nfsrv_dontlisthead;
63 extern volatile int nfsrv_devidcnt;
64 extern struct nfslayouthead nfsrv_recalllisthead;
65 extern char *nfsrv_zeropnfsdat;
67 SYSCTL_DECL(_vfs_nfsd);
68 int nfsrv_statehashsize = NFSSTATEHASHSIZE;
69 SYSCTL_INT(_vfs_nfsd, OID_AUTO, statehashsize, CTLFLAG_RDTUN,
70 &nfsrv_statehashsize, 0,
71 "Size of state hash table set via loader.conf");
73 int nfsrv_clienthashsize = NFSCLIENTHASHSIZE;
74 SYSCTL_INT(_vfs_nfsd, OID_AUTO, clienthashsize, CTLFLAG_RDTUN,
75 &nfsrv_clienthashsize, 0,
76 "Size of client hash table set via loader.conf");
78 int nfsrv_lockhashsize = NFSLOCKHASHSIZE;
79 SYSCTL_INT(_vfs_nfsd, OID_AUTO, fhhashsize, CTLFLAG_RDTUN,
80 &nfsrv_lockhashsize, 0,
81 "Size of file handle hash table set via loader.conf");
83 int nfsrv_sessionhashsize = NFSSESSIONHASHSIZE;
84 SYSCTL_INT(_vfs_nfsd, OID_AUTO, sessionhashsize, CTLFLAG_RDTUN,
85 &nfsrv_sessionhashsize, 0,
86 "Size of session hash table set via loader.conf");
88 int nfsrv_layouthighwater = NFSLAYOUTHIGHWATER;
89 SYSCTL_INT(_vfs_nfsd, OID_AUTO, layouthighwater, CTLFLAG_RDTUN,
90 &nfsrv_layouthighwater, 0,
91 "High water mark for number of layouts set via loader.conf");
93 static int nfsrv_v4statelimit = NFSRV_V4STATELIMIT;
94 SYSCTL_INT(_vfs_nfsd, OID_AUTO, v4statelimit, CTLFLAG_RWTUN,
95 &nfsrv_v4statelimit, 0,
96 "High water limit for NFSv4 opens+locks+delegations");
98 static int nfsrv_writedelegifpos = 0;
99 SYSCTL_INT(_vfs_nfsd, OID_AUTO, writedelegifpos, CTLFLAG_RW,
100 &nfsrv_writedelegifpos, 0,
101 "Issue a write delegation for read opens if possible");
103 static int nfsrv_allowreadforwriteopen = 1;
104 SYSCTL_INT(_vfs_nfsd, OID_AUTO, allowreadforwriteopen, CTLFLAG_RW,
105 &nfsrv_allowreadforwriteopen, 0,
106 "Allow Reads to be done with Write Access StateIDs");
108 int nfsrv_pnfsatime = 0;
109 SYSCTL_INT(_vfs_nfsd, OID_AUTO, pnfsstrictatime, CTLFLAG_RW,
111 "For pNFS service, do Getattr ops to keep atime up-to-date");
113 int nfsrv_flexlinuxhack = 0;
114 SYSCTL_INT(_vfs_nfsd, OID_AUTO, flexlinuxhack, CTLFLAG_RW,
115 &nfsrv_flexlinuxhack, 0,
116 "For Linux clients, hack around Flex File Layout bug");
119 * Hash lists for nfs V4.
121 struct nfsclienthashhead *nfsclienthash;
122 struct nfslockhashhead *nfslockhash;
123 struct nfssessionhash *nfssessionhash;
124 struct nfslayouthash *nfslayouthash;
125 volatile int nfsrv_dontlistlen = 0;
127 static u_int32_t nfsrv_openpluslock = 0, nfsrv_delegatecnt = 0;
128 static time_t nfsrvboottime;
129 static int nfsrv_returnoldstateid = 0, nfsrv_clients = 0;
130 static int nfsrv_clienthighwater = NFSRV_CLIENTHIGHWATER;
131 static int nfsrv_nogsscallback = 0;
132 static volatile int nfsrv_writedelegcnt = 0;
133 static int nfsrv_faildscnt;
135 /* local functions */
136 static void nfsrv_dumpaclient(struct nfsclient *clp,
137 struct nfsd_dumpclients *dumpp);
138 static void nfsrv_freeopenowner(struct nfsstate *stp, int cansleep,
140 static int nfsrv_freeopen(struct nfsstate *stp, vnode_t vp, int cansleep,
142 static void nfsrv_freelockowner(struct nfsstate *stp, vnode_t vp, int cansleep,
144 static void nfsrv_freeallnfslocks(struct nfsstate *stp, vnode_t vp,
145 int cansleep, NFSPROC_T *p);
146 static void nfsrv_freenfslock(struct nfslock *lop);
147 static void nfsrv_freenfslockfile(struct nfslockfile *lfp);
148 static void nfsrv_freedeleg(struct nfsstate *);
149 static int nfsrv_getstate(struct nfsclient *clp, nfsv4stateid_t *stateidp,
150 u_int32_t flags, struct nfsstate **stpp);
151 static void nfsrv_getowner(struct nfsstatehead *hp, struct nfsstate *new_stp,
152 struct nfsstate **stpp);
153 static int nfsrv_getlockfh(vnode_t vp, u_short flags,
154 struct nfslockfile *new_lfp, fhandle_t *nfhp, NFSPROC_T *p);
155 static int nfsrv_getlockfile(u_short flags, struct nfslockfile **new_lfpp,
156 struct nfslockfile **lfpp, fhandle_t *nfhp, int lockit);
157 static void nfsrv_insertlock(struct nfslock *new_lop,
158 struct nfslock *insert_lop, struct nfsstate *stp, struct nfslockfile *lfp);
159 static void nfsrv_updatelock(struct nfsstate *stp, struct nfslock **new_lopp,
160 struct nfslock **other_lopp, struct nfslockfile *lfp);
161 static int nfsrv_getipnumber(u_char *cp);
162 static int nfsrv_checkrestart(nfsquad_t clientid, u_int32_t flags,
163 nfsv4stateid_t *stateidp, int specialid);
164 static int nfsrv_checkgrace(struct nfsrv_descript *nd, struct nfsclient *clp,
166 static int nfsrv_docallback(struct nfsclient *clp, int procnum,
167 nfsv4stateid_t *stateidp, int trunc, fhandle_t *fhp,
168 struct nfsvattr *nap, nfsattrbit_t *attrbitp, int laytype, NFSPROC_T *p);
169 static int nfsrv_cbcallargs(struct nfsrv_descript *nd, struct nfsclient *clp,
170 uint32_t callback, int op, const char *optag, struct nfsdsession **sepp,
172 static u_int32_t nfsrv_nextclientindex(void);
173 static u_int32_t nfsrv_nextstateindex(struct nfsclient *clp);
174 static void nfsrv_markstable(struct nfsclient *clp);
175 static void nfsrv_markreclaim(struct nfsclient *clp);
176 static int nfsrv_checkstable(struct nfsclient *clp);
177 static int nfsrv_clientconflict(struct nfsclient *clp, int *haslockp, struct
178 vnode *vp, NFSPROC_T *p);
179 static int nfsrv_delegconflict(struct nfsstate *stp, int *haslockp,
180 NFSPROC_T *p, vnode_t vp);
181 static int nfsrv_cleandeleg(vnode_t vp, struct nfslockfile *lfp,
182 struct nfsclient *clp, int *haslockp, NFSPROC_T *p);
183 static int nfsrv_notsamecredname(struct nfsrv_descript *nd,
184 struct nfsclient *clp);
185 static time_t nfsrv_leaseexpiry(void);
186 static void nfsrv_delaydelegtimeout(struct nfsstate *stp);
187 static int nfsrv_checkseqid(struct nfsrv_descript *nd, u_int32_t seqid,
188 struct nfsstate *stp, struct nfsrvcache *op);
189 static int nfsrv_nootherstate(struct nfsstate *stp);
190 static int nfsrv_locallock(vnode_t vp, struct nfslockfile *lfp, int flags,
191 uint64_t first, uint64_t end, struct nfslockconflict *cfp, NFSPROC_T *p);
192 static void nfsrv_localunlock(vnode_t vp, struct nfslockfile *lfp,
193 uint64_t init_first, uint64_t init_end, NFSPROC_T *p);
194 static int nfsrv_dolocal(vnode_t vp, struct nfslockfile *lfp, int flags,
195 int oldflags, uint64_t first, uint64_t end, struct nfslockconflict *cfp,
197 static void nfsrv_locallock_rollback(vnode_t vp, struct nfslockfile *lfp,
199 static void nfsrv_locallock_commit(struct nfslockfile *lfp, int flags,
200 uint64_t first, uint64_t end);
201 static void nfsrv_locklf(struct nfslockfile *lfp);
202 static void nfsrv_unlocklf(struct nfslockfile *lfp);
203 static struct nfsdsession *nfsrv_findsession(uint8_t *sessionid);
204 static int nfsrv_freesession(struct nfsdsession *sep, uint8_t *sessionid);
205 static int nfsv4_setcbsequence(struct nfsrv_descript *nd, struct nfsclient *clp,
206 int dont_replycache, struct nfsdsession **sepp, int *slotposp);
207 static int nfsv4_getcbsession(struct nfsclient *clp, struct nfsdsession **sepp);
208 static int nfsrv_addlayout(struct nfsrv_descript *nd, struct nfslayout **lypp,
209 nfsv4stateid_t *stateidp, char *layp, int *layoutlenp, NFSPROC_T *p);
210 static void nfsrv_freelayout(struct nfslayouthead *lhp, struct nfslayout *lyp);
211 static void nfsrv_freelayoutlist(nfsquad_t clientid);
212 static void nfsrv_freelayouts(nfsquad_t *clid, fsid_t *fs, int laytype,
214 static void nfsrv_freealllayouts(void);
215 static void nfsrv_freedevid(struct nfsdevice *ds);
216 static int nfsrv_setdsserver(char *dspathp, char *mdspathp, NFSPROC_T *p,
217 struct nfsdevice **dsp);
218 static void nfsrv_deleteds(struct nfsdevice *fndds);
219 static void nfsrv_allocdevid(struct nfsdevice *ds, char *addr, char *dnshost);
220 static void nfsrv_freealldevids(void);
221 static void nfsrv_flexlayouterr(struct nfsrv_descript *nd, uint32_t *layp,
222 int maxcnt, NFSPROC_T *p);
223 static int nfsrv_recalllayout(nfsquad_t clid, nfsv4stateid_t *stateidp,
224 fhandle_t *fhp, struct nfslayout *lyp, int changed, int laytype,
226 static int nfsrv_findlayout(nfsquad_t *clientidp, fhandle_t *fhp, int laytype,
227 NFSPROC_T *, struct nfslayout **lypp);
228 static int nfsrv_fndclid(nfsquad_t *clidvec, nfsquad_t clid, int clidcnt);
229 static struct nfslayout *nfsrv_filelayout(struct nfsrv_descript *nd, int iomode,
230 fhandle_t *fhp, fhandle_t *dsfhp, char *devid, fsid_t fs);
231 static struct nfslayout *nfsrv_flexlayout(struct nfsrv_descript *nd, int iomode,
232 int mirrorcnt, fhandle_t *fhp, fhandle_t *dsfhp, char *devid, fsid_t fs);
233 static int nfsrv_dontlayout(fhandle_t *fhp);
234 static int nfsrv_createdsfile(vnode_t vp, fhandle_t *fhp, struct pnfsdsfile *pf,
235 vnode_t dvp, struct nfsdevice *ds, struct ucred *cred, NFSPROC_T *p,
237 static struct nfsdevice *nfsrv_findmirroredds(struct nfsmount *nmp);
240 * Scan the client list for a match and either return the current one,
241 * create a new entry or return an error.
242 * If returning a non-error, the clp structure must either be linked into
243 * the client list or free'd.
246 nfsrv_setclient(struct nfsrv_descript *nd, struct nfsclient **new_clpp,
247 nfsquad_t *clientidp, nfsquad_t *confirmp, NFSPROC_T *p)
249 struct nfsclient *clp = NULL, *new_clp = *new_clpp;
250 int i, error = 0, ret;
251 struct nfsstate *stp, *tstp;
253 struct sockaddr_in *sin, *rin;
256 struct sockaddr_in6 *sin6, *rin6;
258 struct nfsdsession *sep, *nsep;
259 int zapit = 0, gotit, hasstate = 0, igotlock;
260 static u_int64_t confirm_index = 0;
263 * Check for state resource limit exceeded.
265 if (nfsrv_openpluslock > nfsrv_v4statelimit) {
266 error = NFSERR_RESOURCE;
270 if (nfsrv_issuedelegs == 0 ||
271 ((nd->nd_flag & ND_GSS) != 0 && nfsrv_nogsscallback != 0))
273 * Don't do callbacks when delegations are disabled or
274 * for AUTH_GSS unless enabled via nfsrv_nogsscallback.
275 * If establishing a callback connection is attempted
276 * when a firewall is blocking the callback path, the
277 * server may wait too long for the connect attempt to
278 * succeed during the Open. Some clients, such as Linux,
279 * may timeout and give up on the Open before the server
280 * replies. Also, since AUTH_GSS callbacks are not
281 * yet interoperability tested, they might cause the
282 * server to crap out, if they get past the Init call to
285 new_clp->lc_program = 0;
287 /* Lock out other nfsd threads */
288 NFSLOCKV4ROOTMUTEX();
289 nfsv4_relref(&nfsv4rootfs_lock);
291 igotlock = nfsv4_lock(&nfsv4rootfs_lock, 1, NULL,
292 NFSV4ROOTLOCKMUTEXPTR, NULL);
294 NFSUNLOCKV4ROOTMUTEX();
297 * Search for a match in the client list.
300 while (i < nfsrv_clienthashsize && !gotit) {
301 LIST_FOREACH(clp, &nfsclienthash[i], lc_hash) {
302 if (new_clp->lc_idlen == clp->lc_idlen &&
303 !NFSBCMP(new_clp->lc_id, clp->lc_id, clp->lc_idlen)) {
312 (clp->lc_flags & (LCL_NEEDSCONFIRM | LCL_ADMINREVOKED))) {
313 if ((nd->nd_flag & ND_NFSV41) != 0 && confirmp->lval[1] != 0) {
315 * For NFSv4.1, if confirmp->lval[1] is non-zero, the
316 * client is trying to update a confirmed clientid.
318 NFSLOCKV4ROOTMUTEX();
319 nfsv4_unlock(&nfsv4rootfs_lock, 1);
320 NFSUNLOCKV4ROOTMUTEX();
321 confirmp->lval[1] = 0;
322 error = NFSERR_NOENT;
326 * Get rid of the old one.
328 if (i != nfsrv_clienthashsize) {
329 LIST_REMOVE(clp, lc_hash);
330 nfsrv_cleanclient(clp, p);
331 nfsrv_freedeleglist(&clp->lc_deleg);
332 nfsrv_freedeleglist(&clp->lc_olddeleg);
336 * Add it after assigning a client id to it.
338 new_clp->lc_flags |= LCL_NEEDSCONFIRM;
339 if ((nd->nd_flag & ND_NFSV41) != 0) {
340 confirmp->lval[0] = ++confirm_index;
341 new_clp->lc_confirm.lval[0] = confirmp->lval[0] - 1;
343 confirmp->qval = new_clp->lc_confirm.qval =
345 clientidp->lval[0] = new_clp->lc_clientid.lval[0] =
346 (u_int32_t)nfsrvboottime;
347 clientidp->lval[1] = new_clp->lc_clientid.lval[1] =
348 nfsrv_nextclientindex();
349 new_clp->lc_stateindex = 0;
350 new_clp->lc_statemaxindex = 0;
351 new_clp->lc_prevsess = 0;
352 new_clp->lc_cbref = 0;
353 new_clp->lc_expiry = nfsrv_leaseexpiry();
354 LIST_INIT(&new_clp->lc_open);
355 LIST_INIT(&new_clp->lc_deleg);
356 LIST_INIT(&new_clp->lc_olddeleg);
357 LIST_INIT(&new_clp->lc_session);
358 for (i = 0; i < nfsrv_statehashsize; i++)
359 LIST_INIT(&new_clp->lc_stateid[i]);
360 LIST_INSERT_HEAD(NFSCLIENTHASH(new_clp->lc_clientid), new_clp,
362 nfsstatsv1.srvclients++;
363 nfsrv_openpluslock++;
365 NFSLOCKV4ROOTMUTEX();
366 nfsv4_unlock(&nfsv4rootfs_lock, 1);
367 NFSUNLOCKV4ROOTMUTEX();
369 nfsrv_zapclient(clp, p);
375 * Now, handle the cases where the id is already issued.
377 if (nfsrv_notsamecredname(nd, clp)) {
379 * Check to see if there is expired state that should go away.
381 if (clp->lc_expiry < NFSD_MONOSEC &&
382 (!LIST_EMPTY(&clp->lc_open) || !LIST_EMPTY(&clp->lc_deleg))) {
383 nfsrv_cleanclient(clp, p);
384 nfsrv_freedeleglist(&clp->lc_deleg);
388 * If there is outstanding state, then reply NFSERR_CLIDINUSE per
389 * RFC3530 Sec. 8.1.2 last para.
391 if (!LIST_EMPTY(&clp->lc_deleg)) {
393 } else if (LIST_EMPTY(&clp->lc_open)) {
397 /* Look for an Open on the OpenOwner */
398 LIST_FOREACH(stp, &clp->lc_open, ls_list) {
399 if (!LIST_EMPTY(&stp->ls_open)) {
407 * If the uid doesn't match, return NFSERR_CLIDINUSE after
408 * filling out the correct ipaddr and portnum.
410 switch (clp->lc_req.nr_nam->sa_family) {
413 sin = (struct sockaddr_in *)new_clp->lc_req.nr_nam;
414 rin = (struct sockaddr_in *)clp->lc_req.nr_nam;
415 sin->sin_addr.s_addr = rin->sin_addr.s_addr;
416 sin->sin_port = rin->sin_port;
421 sin6 = (struct sockaddr_in6 *)new_clp->lc_req.nr_nam;
422 rin6 = (struct sockaddr_in6 *)clp->lc_req.nr_nam;
423 sin6->sin6_addr = rin6->sin6_addr;
424 sin6->sin6_port = rin6->sin6_port;
428 NFSLOCKV4ROOTMUTEX();
429 nfsv4_unlock(&nfsv4rootfs_lock, 1);
430 NFSUNLOCKV4ROOTMUTEX();
431 error = NFSERR_CLIDINUSE;
436 if (NFSBCMP(new_clp->lc_verf, clp->lc_verf, NFSX_VERF)) {
438 * If the verifier has changed, the client has rebooted
439 * and a new client id is issued. The old state info
440 * can be thrown away once the SETCLIENTID_CONFIRM occurs.
442 LIST_REMOVE(clp, lc_hash);
444 /* Get rid of all sessions on this clientid. */
445 LIST_FOREACH_SAFE(sep, &clp->lc_session, sess_list, nsep) {
446 ret = nfsrv_freesession(sep, NULL);
448 printf("nfsrv_setclient: verifier changed free"
449 " session failed=%d\n", ret);
452 new_clp->lc_flags |= LCL_NEEDSCONFIRM;
453 if ((nd->nd_flag & ND_NFSV41) != 0) {
454 confirmp->lval[0] = ++confirm_index;
455 new_clp->lc_confirm.lval[0] = confirmp->lval[0] - 1;
457 confirmp->qval = new_clp->lc_confirm.qval =
459 clientidp->lval[0] = new_clp->lc_clientid.lval[0] =
461 clientidp->lval[1] = new_clp->lc_clientid.lval[1] =
462 nfsrv_nextclientindex();
463 new_clp->lc_stateindex = 0;
464 new_clp->lc_statemaxindex = 0;
465 new_clp->lc_prevsess = 0;
466 new_clp->lc_cbref = 0;
467 new_clp->lc_expiry = nfsrv_leaseexpiry();
470 * Save the state until confirmed.
472 LIST_NEWHEAD(&new_clp->lc_open, &clp->lc_open, ls_list);
473 LIST_FOREACH(tstp, &new_clp->lc_open, ls_list)
474 tstp->ls_clp = new_clp;
475 LIST_NEWHEAD(&new_clp->lc_deleg, &clp->lc_deleg, ls_list);
476 LIST_FOREACH(tstp, &new_clp->lc_deleg, ls_list)
477 tstp->ls_clp = new_clp;
478 LIST_NEWHEAD(&new_clp->lc_olddeleg, &clp->lc_olddeleg,
480 LIST_FOREACH(tstp, &new_clp->lc_olddeleg, ls_list)
481 tstp->ls_clp = new_clp;
482 for (i = 0; i < nfsrv_statehashsize; i++) {
483 LIST_NEWHEAD(&new_clp->lc_stateid[i],
484 &clp->lc_stateid[i], ls_hash);
485 LIST_FOREACH(tstp, &new_clp->lc_stateid[i], ls_hash)
486 tstp->ls_clp = new_clp;
488 LIST_INIT(&new_clp->lc_session);
489 LIST_INSERT_HEAD(NFSCLIENTHASH(new_clp->lc_clientid), new_clp,
491 nfsstatsv1.srvclients++;
492 nfsrv_openpluslock++;
494 NFSLOCKV4ROOTMUTEX();
495 nfsv4_unlock(&nfsv4rootfs_lock, 1);
496 NFSUNLOCKV4ROOTMUTEX();
499 * Must wait until any outstanding callback on the old clp
503 while (clp->lc_cbref) {
504 clp->lc_flags |= LCL_WAKEUPWANTED;
505 (void)mtx_sleep(clp, NFSSTATEMUTEXPTR, PZERO - 1,
506 "nfsd clp", 10 * hz);
509 nfsrv_zapclient(clp, p);
514 /* For NFSv4.1, mark that we found a confirmed clientid. */
515 if ((nd->nd_flag & ND_NFSV41) != 0) {
516 clientidp->lval[0] = clp->lc_clientid.lval[0];
517 clientidp->lval[1] = clp->lc_clientid.lval[1];
518 confirmp->lval[0] = 0; /* Ignored by client */
519 confirmp->lval[1] = 1;
522 * id and verifier match, so update the net address info
523 * and get rid of any existing callback authentication
524 * handle, so a new one will be acquired.
526 LIST_REMOVE(clp, lc_hash);
527 new_clp->lc_flags |= (LCL_NEEDSCONFIRM | LCL_DONTCLEAN);
528 new_clp->lc_expiry = nfsrv_leaseexpiry();
529 confirmp->qval = new_clp->lc_confirm.qval = ++confirm_index;
530 clientidp->lval[0] = new_clp->lc_clientid.lval[0] =
531 clp->lc_clientid.lval[0];
532 clientidp->lval[1] = new_clp->lc_clientid.lval[1] =
533 clp->lc_clientid.lval[1];
534 new_clp->lc_delegtime = clp->lc_delegtime;
535 new_clp->lc_stateindex = clp->lc_stateindex;
536 new_clp->lc_statemaxindex = clp->lc_statemaxindex;
537 new_clp->lc_cbref = 0;
538 LIST_NEWHEAD(&new_clp->lc_open, &clp->lc_open, ls_list);
539 LIST_FOREACH(tstp, &new_clp->lc_open, ls_list)
540 tstp->ls_clp = new_clp;
541 LIST_NEWHEAD(&new_clp->lc_deleg, &clp->lc_deleg, ls_list);
542 LIST_FOREACH(tstp, &new_clp->lc_deleg, ls_list)
543 tstp->ls_clp = new_clp;
544 LIST_NEWHEAD(&new_clp->lc_olddeleg, &clp->lc_olddeleg, ls_list);
545 LIST_FOREACH(tstp, &new_clp->lc_olddeleg, ls_list)
546 tstp->ls_clp = new_clp;
547 for (i = 0; i < nfsrv_statehashsize; i++) {
548 LIST_NEWHEAD(&new_clp->lc_stateid[i],
549 &clp->lc_stateid[i], ls_hash);
550 LIST_FOREACH(tstp, &new_clp->lc_stateid[i], ls_hash)
551 tstp->ls_clp = new_clp;
553 LIST_INIT(&new_clp->lc_session);
554 LIST_INSERT_HEAD(NFSCLIENTHASH(new_clp->lc_clientid), new_clp,
556 nfsstatsv1.srvclients++;
557 nfsrv_openpluslock++;
560 NFSLOCKV4ROOTMUTEX();
561 nfsv4_unlock(&nfsv4rootfs_lock, 1);
562 NFSUNLOCKV4ROOTMUTEX();
564 if ((nd->nd_flag & ND_NFSV41) == 0) {
566 * Must wait until any outstanding callback on the old clp
570 while (clp->lc_cbref) {
571 clp->lc_flags |= LCL_WAKEUPWANTED;
572 (void)mtx_sleep(clp, NFSSTATEMUTEXPTR, PZERO - 1,
576 nfsrv_zapclient(clp, p);
581 NFSEXITCODE2(error, nd);
586 * Check to see if the client id exists and optionally confirm it.
589 nfsrv_getclient(nfsquad_t clientid, int opflags, struct nfsclient **clpp,
590 struct nfsdsession *nsep, nfsquad_t confirm, uint32_t cbprogram,
591 struct nfsrv_descript *nd, NFSPROC_T *p)
593 struct nfsclient *clp;
594 struct nfsstate *stp;
596 struct nfsclienthashhead *hp;
597 int error = 0, igotlock, doneok;
598 struct nfssessionhash *shp;
599 struct nfsdsession *sep;
602 static uint64_t next_sess = 0;
606 if ((nd == NULL || (nd->nd_flag & ND_NFSV41) == 0 ||
607 opflags != CLOPS_RENEW) && nfsrvboottime != clientid.lval[0]) {
608 error = NFSERR_STALECLIENTID;
613 * If called with opflags == CLOPS_RENEW, the State Lock is
614 * already held. Otherwise, we need to get either that or,
615 * for the case of Confirm, lock out the nfsd threads.
617 if (opflags & CLOPS_CONFIRM) {
618 NFSLOCKV4ROOTMUTEX();
619 nfsv4_relref(&nfsv4rootfs_lock);
621 igotlock = nfsv4_lock(&nfsv4rootfs_lock, 1, NULL,
622 NFSV4ROOTLOCKMUTEXPTR, NULL);
625 * Create a new sessionid here, since we need to do it where
626 * there is a mutex held to serialize update of next_sess.
628 if ((nd->nd_flag & ND_NFSV41) != 0) {
629 sessid[0] = ++next_sess;
630 sessid[1] = clientid.qval;
632 NFSUNLOCKV4ROOTMUTEX();
633 } else if (opflags != CLOPS_RENEW) {
637 /* For NFSv4.1, the clp is acquired from the associated session. */
638 if (nd != NULL && (nd->nd_flag & ND_NFSV41) != 0 &&
639 opflags == CLOPS_RENEW) {
641 if ((nd->nd_flag & ND_HASSEQUENCE) != 0) {
642 shp = NFSSESSIONHASH(nd->nd_sessionid);
644 sep = nfsrv_findsession(nd->nd_sessionid);
647 NFSUNLOCKSESSION(shp);
650 hp = NFSCLIENTHASH(clientid);
651 LIST_FOREACH(clp, hp, lc_hash) {
652 if (clp->lc_clientid.lval[1] == clientid.lval[1])
657 if (opflags & CLOPS_CONFIRM)
658 error = NFSERR_STALECLIENTID;
660 error = NFSERR_EXPIRED;
661 } else if (clp->lc_flags & LCL_ADMINREVOKED) {
663 * If marked admin revoked, just return the error.
665 error = NFSERR_ADMINREVOKED;
668 if (opflags & CLOPS_CONFIRM) {
669 NFSLOCKV4ROOTMUTEX();
670 nfsv4_unlock(&nfsv4rootfs_lock, 1);
671 NFSUNLOCKV4ROOTMUTEX();
672 } else if (opflags != CLOPS_RENEW) {
679 * Perform any operations specified by the opflags.
681 if (opflags & CLOPS_CONFIRM) {
683 if ((nd->nd_flag & ND_NFSV41) != 0) {
685 * For the case where lc_confirm.lval[0] == confirm.lval[0],
686 * use the new session, but with the previous sessionid.
687 * This is not exactly what the RFC describes, but should
688 * result in the same reply as the previous CreateSession.
690 if (clp->lc_confirm.lval[0] + 1 == confirm.lval[0]) {
691 clp->lc_confirm.lval[0] = confirm.lval[0];
692 clp->lc_prevsess = sessid[0];
693 } else if (clp->lc_confirm.lval[0] == confirm.lval[0]) {
694 if (clp->lc_prevsess == 0)
695 error = NFSERR_SEQMISORDERED;
697 sessid[0] = clp->lc_prevsess;
700 error = NFSERR_SEQMISORDERED;
701 } else if ((nd->nd_flag & ND_NFSV41) == 0 &&
702 clp->lc_confirm.qval != confirm.qval)
703 error = NFSERR_STALECLIENTID;
704 if (error == 0 && nfsrv_notsamecredname(nd, clp))
705 error = NFSERR_CLIDINUSE;
708 if ((clp->lc_flags & (LCL_NEEDSCONFIRM | LCL_DONTCLEAN)) ==
711 * Hang onto the delegations (as old delegations)
712 * for an Open with CLAIM_DELEGATE_PREV unless in
713 * grace, but get rid of the rest of the state.
715 nfsrv_cleanclient(clp, p);
716 nfsrv_freedeleglist(&clp->lc_olddeleg);
717 if (nfsrv_checkgrace(nd, clp, 0)) {
718 /* In grace, so just delete delegations */
719 nfsrv_freedeleglist(&clp->lc_deleg);
721 LIST_FOREACH(stp, &clp->lc_deleg, ls_list)
722 stp->ls_flags |= NFSLCK_OLDDELEG;
723 clp->lc_delegtime = NFSD_MONOSEC +
724 nfsrv_lease + NFSRV_LEASEDELTA;
725 LIST_NEWHEAD(&clp->lc_olddeleg, &clp->lc_deleg,
728 if ((nd->nd_flag & ND_NFSV41) != 0)
729 clp->lc_program = cbprogram;
731 clp->lc_flags &= ~(LCL_NEEDSCONFIRM | LCL_DONTCLEAN);
733 clp->lc_flags |= LCL_NEEDSCBNULL;
734 /* For NFSv4.1, link the session onto the client. */
736 /* Hold a reference on the xprt for a backchannel. */
737 if ((nsep->sess_crflags & NFSV4CRSESS_CONNBACKCHAN)
738 != 0 && !sess_replay) {
739 if (clp->lc_req.nr_client == NULL)
740 clp->lc_req.nr_client = (struct __rpc_client *)
741 clnt_bck_create(nd->nd_xprt->xp_socket,
742 cbprogram, NFSV4_CBVERS);
743 if (clp->lc_req.nr_client != NULL) {
744 SVC_ACQUIRE(nd->nd_xprt);
745 CLNT_ACQUIRE(clp->lc_req.nr_client);
746 nd->nd_xprt->xp_p2 = clp->lc_req.nr_client;
747 /* Disable idle timeout. */
748 nd->nd_xprt->xp_idletimeout = 0;
749 nsep->sess_cbsess.nfsess_xprt = nd->nd_xprt;
751 nsep->sess_crflags &= ~NFSV4CRSESS_CONNBACKCHAN;
753 NFSBCOPY(sessid, nsep->sess_sessionid,
755 NFSBCOPY(sessid, nsep->sess_cbsess.nfsess_sessionid,
758 shp = NFSSESSIONHASH(nsep->sess_sessionid);
761 LIST_INSERT_HEAD(&shp->list, nsep, sess_hash);
762 LIST_INSERT_HEAD(&clp->lc_session, nsep, sess_list);
763 nsep->sess_clp = clp;
764 NFSUNLOCKSESSION(shp);
769 } else if (clp->lc_flags & LCL_NEEDSCONFIRM) {
770 error = NFSERR_EXPIRED;
774 * If called by the Renew Op, we must check the principal.
776 if (!error && (opflags & CLOPS_RENEWOP)) {
777 if (nfsrv_notsamecredname(nd, clp)) {
779 for (i = 0; i < nfsrv_statehashsize && doneok == 0; i++) {
780 LIST_FOREACH(stp, &clp->lc_stateid[i], ls_hash) {
781 if ((stp->ls_flags & NFSLCK_OPEN) &&
782 stp->ls_uid == nd->nd_cred->cr_uid) {
789 error = NFSERR_ACCES;
791 if (!error && (clp->lc_flags & LCL_CBDOWN))
792 error = NFSERR_CBPATHDOWN;
794 if ((!error || error == NFSERR_CBPATHDOWN) &&
795 (opflags & CLOPS_RENEW)) {
796 clp->lc_expiry = nfsrv_leaseexpiry();
798 if (opflags & CLOPS_CONFIRM) {
799 NFSLOCKV4ROOTMUTEX();
800 nfsv4_unlock(&nfsv4rootfs_lock, 1);
801 NFSUNLOCKV4ROOTMUTEX();
802 } else if (opflags != CLOPS_RENEW) {
809 NFSEXITCODE2(error, nd);
814 * Perform the NFSv4.1 destroy clientid.
817 nfsrv_destroyclient(nfsquad_t clientid, NFSPROC_T *p)
819 struct nfsclient *clp;
820 struct nfsclienthashhead *hp;
821 int error = 0, i, igotlock;
823 if (nfsrvboottime != clientid.lval[0]) {
824 error = NFSERR_STALECLIENTID;
828 /* Lock out other nfsd threads */
829 NFSLOCKV4ROOTMUTEX();
830 nfsv4_relref(&nfsv4rootfs_lock);
832 igotlock = nfsv4_lock(&nfsv4rootfs_lock, 1, NULL,
833 NFSV4ROOTLOCKMUTEXPTR, NULL);
834 } while (igotlock == 0);
835 NFSUNLOCKV4ROOTMUTEX();
837 hp = NFSCLIENTHASH(clientid);
838 LIST_FOREACH(clp, hp, lc_hash) {
839 if (clp->lc_clientid.lval[1] == clientid.lval[1])
843 NFSLOCKV4ROOTMUTEX();
844 nfsv4_unlock(&nfsv4rootfs_lock, 1);
845 NFSUNLOCKV4ROOTMUTEX();
846 /* Just return ok, since it is gone. */
851 * Free up all layouts on the clientid. Should the client return the
854 nfsrv_freelayoutlist(clientid);
856 /* Scan for state on the clientid. */
857 for (i = 0; i < nfsrv_statehashsize; i++)
858 if (!LIST_EMPTY(&clp->lc_stateid[i])) {
859 NFSLOCKV4ROOTMUTEX();
860 nfsv4_unlock(&nfsv4rootfs_lock, 1);
861 NFSUNLOCKV4ROOTMUTEX();
862 error = NFSERR_CLIENTIDBUSY;
865 if (!LIST_EMPTY(&clp->lc_session) || !LIST_EMPTY(&clp->lc_deleg)) {
866 NFSLOCKV4ROOTMUTEX();
867 nfsv4_unlock(&nfsv4rootfs_lock, 1);
868 NFSUNLOCKV4ROOTMUTEX();
869 error = NFSERR_CLIENTIDBUSY;
873 /* Destroy the clientid and return ok. */
874 nfsrv_cleanclient(clp, p);
875 nfsrv_freedeleglist(&clp->lc_deleg);
876 nfsrv_freedeleglist(&clp->lc_olddeleg);
877 LIST_REMOVE(clp, lc_hash);
878 NFSLOCKV4ROOTMUTEX();
879 nfsv4_unlock(&nfsv4rootfs_lock, 1);
880 NFSUNLOCKV4ROOTMUTEX();
881 nfsrv_zapclient(clp, p);
883 NFSEXITCODE2(error, nd);
888 * Called from the new nfssvc syscall to admin revoke a clientid.
889 * Returns 0 for success, error otherwise.
892 nfsrv_adminrevoke(struct nfsd_clid *revokep, NFSPROC_T *p)
894 struct nfsclient *clp = NULL;
899 * First, lock out the nfsd so that state won't change while the
900 * revocation record is being written to the stable storage restart
903 NFSLOCKV4ROOTMUTEX();
905 igotlock = nfsv4_lock(&nfsv4rootfs_lock, 1, NULL,
906 NFSV4ROOTLOCKMUTEXPTR, NULL);
908 NFSUNLOCKV4ROOTMUTEX();
911 * Search for a match in the client list.
914 while (i < nfsrv_clienthashsize && !gotit) {
915 LIST_FOREACH(clp, &nfsclienthash[i], lc_hash) {
916 if (revokep->nclid_idlen == clp->lc_idlen &&
917 !NFSBCMP(revokep->nclid_id, clp->lc_id, clp->lc_idlen)) {
925 NFSLOCKV4ROOTMUTEX();
926 nfsv4_unlock(&nfsv4rootfs_lock, 0);
927 NFSUNLOCKV4ROOTMUTEX();
933 * Now, write out the revocation record
935 nfsrv_writestable(clp->lc_id, clp->lc_idlen, NFSNST_REVOKE, p);
936 nfsrv_backupstable();
939 * and clear out the state, marking the clientid revoked.
941 clp->lc_flags &= ~LCL_CALLBACKSON;
942 clp->lc_flags |= LCL_ADMINREVOKED;
943 nfsrv_cleanclient(clp, p);
944 nfsrv_freedeleglist(&clp->lc_deleg);
945 nfsrv_freedeleglist(&clp->lc_olddeleg);
946 NFSLOCKV4ROOTMUTEX();
947 nfsv4_unlock(&nfsv4rootfs_lock, 0);
948 NFSUNLOCKV4ROOTMUTEX();
956 * Dump out stats for all clients. Called from nfssvc(2), that is used
960 nfsrv_dumpclients(struct nfsd_dumpclients *dumpp, int maxcnt)
962 struct nfsclient *clp;
966 * First, get a reference on the nfsv4rootfs_lock so that an
967 * exclusive lock cannot be acquired while dumping the clients.
969 NFSLOCKV4ROOTMUTEX();
970 nfsv4_getref(&nfsv4rootfs_lock, NULL, NFSV4ROOTLOCKMUTEXPTR, NULL);
971 NFSUNLOCKV4ROOTMUTEX();
974 * Rattle through the client lists until done.
976 while (i < nfsrv_clienthashsize && cnt < maxcnt) {
977 clp = LIST_FIRST(&nfsclienthash[i]);
978 while (clp != LIST_END(&nfsclienthash[i]) && cnt < maxcnt) {
979 nfsrv_dumpaclient(clp, &dumpp[cnt]);
981 clp = LIST_NEXT(clp, lc_hash);
986 dumpp[cnt].ndcl_clid.nclid_idlen = 0;
988 NFSLOCKV4ROOTMUTEX();
989 nfsv4_relref(&nfsv4rootfs_lock);
990 NFSUNLOCKV4ROOTMUTEX();
994 * Dump stats for a client. Must be called with the NFSSTATELOCK and spl'd.
997 nfsrv_dumpaclient(struct nfsclient *clp, struct nfsd_dumpclients *dumpp)
999 struct nfsstate *stp, *openstp, *lckownstp;
1000 struct nfslock *lop;
1003 struct sockaddr_in *rin;
1006 struct sockaddr_in6 *rin6;
1009 dumpp->ndcl_nopenowners = dumpp->ndcl_nlockowners = 0;
1010 dumpp->ndcl_nopens = dumpp->ndcl_nlocks = 0;
1011 dumpp->ndcl_ndelegs = dumpp->ndcl_nolddelegs = 0;
1012 dumpp->ndcl_flags = clp->lc_flags;
1013 dumpp->ndcl_clid.nclid_idlen = clp->lc_idlen;
1014 NFSBCOPY(clp->lc_id, dumpp->ndcl_clid.nclid_id, clp->lc_idlen);
1015 af = clp->lc_req.nr_nam->sa_family;
1016 dumpp->ndcl_addrfam = af;
1020 rin = (struct sockaddr_in *)clp->lc_req.nr_nam;
1021 dumpp->ndcl_cbaddr.sin_addr = rin->sin_addr;
1026 rin6 = (struct sockaddr_in6 *)clp->lc_req.nr_nam;
1027 dumpp->ndcl_cbaddr.sin6_addr = rin6->sin6_addr;
1033 * Now, scan the state lists and total up the opens and locks.
1035 LIST_FOREACH(stp, &clp->lc_open, ls_list) {
1036 dumpp->ndcl_nopenowners++;
1037 LIST_FOREACH(openstp, &stp->ls_open, ls_list) {
1038 dumpp->ndcl_nopens++;
1039 LIST_FOREACH(lckownstp, &openstp->ls_open, ls_list) {
1040 dumpp->ndcl_nlockowners++;
1041 LIST_FOREACH(lop, &lckownstp->ls_lock, lo_lckowner) {
1042 dumpp->ndcl_nlocks++;
1049 * and the delegation lists.
1051 LIST_FOREACH(stp, &clp->lc_deleg, ls_list) {
1052 dumpp->ndcl_ndelegs++;
1054 LIST_FOREACH(stp, &clp->lc_olddeleg, ls_list) {
1055 dumpp->ndcl_nolddelegs++;
1060 * Dump out lock stats for a file.
1063 nfsrv_dumplocks(vnode_t vp, struct nfsd_dumplocks *ldumpp, int maxcnt,
1066 struct nfsstate *stp;
1067 struct nfslock *lop;
1069 struct nfslockfile *lfp;
1072 struct sockaddr_in *rin;
1075 struct sockaddr_in6 *rin6;
1080 ret = nfsrv_getlockfh(vp, 0, NULL, &nfh, p);
1082 * First, get a reference on the nfsv4rootfs_lock so that an
1083 * exclusive lock on it cannot be acquired while dumping the locks.
1085 NFSLOCKV4ROOTMUTEX();
1086 nfsv4_getref(&nfsv4rootfs_lock, NULL, NFSV4ROOTLOCKMUTEXPTR, NULL);
1087 NFSUNLOCKV4ROOTMUTEX();
1090 ret = nfsrv_getlockfile(0, NULL, &lfp, &nfh, 0);
1092 ldumpp[0].ndlck_clid.nclid_idlen = 0;
1094 NFSLOCKV4ROOTMUTEX();
1095 nfsv4_relref(&nfsv4rootfs_lock);
1096 NFSUNLOCKV4ROOTMUTEX();
1101 * For each open share on file, dump it out.
1103 stp = LIST_FIRST(&lfp->lf_open);
1104 while (stp != LIST_END(&lfp->lf_open) && cnt < maxcnt) {
1105 ldumpp[cnt].ndlck_flags = stp->ls_flags;
1106 ldumpp[cnt].ndlck_stateid.seqid = stp->ls_stateid.seqid;
1107 ldumpp[cnt].ndlck_stateid.other[0] = stp->ls_stateid.other[0];
1108 ldumpp[cnt].ndlck_stateid.other[1] = stp->ls_stateid.other[1];
1109 ldumpp[cnt].ndlck_stateid.other[2] = stp->ls_stateid.other[2];
1110 ldumpp[cnt].ndlck_owner.nclid_idlen =
1111 stp->ls_openowner->ls_ownerlen;
1112 NFSBCOPY(stp->ls_openowner->ls_owner,
1113 ldumpp[cnt].ndlck_owner.nclid_id,
1114 stp->ls_openowner->ls_ownerlen);
1115 ldumpp[cnt].ndlck_clid.nclid_idlen = stp->ls_clp->lc_idlen;
1116 NFSBCOPY(stp->ls_clp->lc_id, ldumpp[cnt].ndlck_clid.nclid_id,
1117 stp->ls_clp->lc_idlen);
1118 af = stp->ls_clp->lc_req.nr_nam->sa_family;
1119 ldumpp[cnt].ndlck_addrfam = af;
1123 rin = (struct sockaddr_in *)stp->ls_clp->lc_req.nr_nam;
1124 ldumpp[cnt].ndlck_cbaddr.sin_addr = rin->sin_addr;
1129 rin6 = (struct sockaddr_in6 *)
1130 stp->ls_clp->lc_req.nr_nam;
1131 ldumpp[cnt].ndlck_cbaddr.sin6_addr = rin6->sin6_addr;
1135 stp = LIST_NEXT(stp, ls_file);
1142 lop = LIST_FIRST(&lfp->lf_lock);
1143 while (lop != LIST_END(&lfp->lf_lock) && cnt < maxcnt) {
1145 ldumpp[cnt].ndlck_flags = lop->lo_flags;
1146 ldumpp[cnt].ndlck_first = lop->lo_first;
1147 ldumpp[cnt].ndlck_end = lop->lo_end;
1148 ldumpp[cnt].ndlck_stateid.seqid = stp->ls_stateid.seqid;
1149 ldumpp[cnt].ndlck_stateid.other[0] = stp->ls_stateid.other[0];
1150 ldumpp[cnt].ndlck_stateid.other[1] = stp->ls_stateid.other[1];
1151 ldumpp[cnt].ndlck_stateid.other[2] = stp->ls_stateid.other[2];
1152 ldumpp[cnt].ndlck_owner.nclid_idlen = stp->ls_ownerlen;
1153 NFSBCOPY(stp->ls_owner, ldumpp[cnt].ndlck_owner.nclid_id,
1155 ldumpp[cnt].ndlck_clid.nclid_idlen = stp->ls_clp->lc_idlen;
1156 NFSBCOPY(stp->ls_clp->lc_id, ldumpp[cnt].ndlck_clid.nclid_id,
1157 stp->ls_clp->lc_idlen);
1158 af = stp->ls_clp->lc_req.nr_nam->sa_family;
1159 ldumpp[cnt].ndlck_addrfam = af;
1163 rin = (struct sockaddr_in *)stp->ls_clp->lc_req.nr_nam;
1164 ldumpp[cnt].ndlck_cbaddr.sin_addr = rin->sin_addr;
1169 rin6 = (struct sockaddr_in6 *)
1170 stp->ls_clp->lc_req.nr_nam;
1171 ldumpp[cnt].ndlck_cbaddr.sin6_addr = rin6->sin6_addr;
1175 lop = LIST_NEXT(lop, lo_lckfile);
1180 * and the delegations.
1182 stp = LIST_FIRST(&lfp->lf_deleg);
1183 while (stp != LIST_END(&lfp->lf_deleg) && cnt < maxcnt) {
1184 ldumpp[cnt].ndlck_flags = stp->ls_flags;
1185 ldumpp[cnt].ndlck_stateid.seqid = stp->ls_stateid.seqid;
1186 ldumpp[cnt].ndlck_stateid.other[0] = stp->ls_stateid.other[0];
1187 ldumpp[cnt].ndlck_stateid.other[1] = stp->ls_stateid.other[1];
1188 ldumpp[cnt].ndlck_stateid.other[2] = stp->ls_stateid.other[2];
1189 ldumpp[cnt].ndlck_owner.nclid_idlen = 0;
1190 ldumpp[cnt].ndlck_clid.nclid_idlen = stp->ls_clp->lc_idlen;
1191 NFSBCOPY(stp->ls_clp->lc_id, ldumpp[cnt].ndlck_clid.nclid_id,
1192 stp->ls_clp->lc_idlen);
1193 af = stp->ls_clp->lc_req.nr_nam->sa_family;
1194 ldumpp[cnt].ndlck_addrfam = af;
1198 rin = (struct sockaddr_in *)stp->ls_clp->lc_req.nr_nam;
1199 ldumpp[cnt].ndlck_cbaddr.sin_addr = rin->sin_addr;
1204 rin6 = (struct sockaddr_in6 *)
1205 stp->ls_clp->lc_req.nr_nam;
1206 ldumpp[cnt].ndlck_cbaddr.sin6_addr = rin6->sin6_addr;
1210 stp = LIST_NEXT(stp, ls_file);
1215 * If list isn't full, mark end of list by setting the client name
1219 ldumpp[cnt].ndlck_clid.nclid_idlen = 0;
1221 NFSLOCKV4ROOTMUTEX();
1222 nfsv4_relref(&nfsv4rootfs_lock);
1223 NFSUNLOCKV4ROOTMUTEX();
1227 * Server timer routine. It can scan any linked list, so long
1228 * as it holds the spin/mutex lock and there is no exclusive lock on
1230 * (For OpenBSD, a kthread is ok. For FreeBSD, I think it is ok
1231 * to do this from a callout, since the spin locks work. For
1232 * Darwin, I'm not sure what will work correctly yet.)
1233 * Should be called once per second.
1236 nfsrv_servertimer(void)
1238 struct nfsclient *clp, *nclp;
1239 struct nfsstate *stp, *nstp;
1243 * Make sure nfsboottime is set. This is used by V3 as well
1244 * as V4. Note that nfsboottime is not nfsrvboottime, which is
1245 * only used by the V4 server for leases.
1247 if (nfsboottime.tv_sec == 0)
1248 NFSSETBOOTTIME(nfsboottime);
1251 * If server hasn't started yet, just return.
1254 if (nfsrv_stablefirst.nsf_eograce == 0) {
1258 if (!(nfsrv_stablefirst.nsf_flags & NFSNSF_UPDATEDONE)) {
1259 if (!(nfsrv_stablefirst.nsf_flags & NFSNSF_GRACEOVER) &&
1260 NFSD_MONOSEC > nfsrv_stablefirst.nsf_eograce)
1261 nfsrv_stablefirst.nsf_flags |=
1262 (NFSNSF_GRACEOVER | NFSNSF_NEEDLOCK);
1268 * Try and get a reference count on the nfsv4rootfs_lock so that
1269 * no nfsd thread can acquire an exclusive lock on it before this
1270 * call is done. If it is already exclusively locked, just return.
1272 NFSLOCKV4ROOTMUTEX();
1273 got_ref = nfsv4_getref_nonblock(&nfsv4rootfs_lock);
1274 NFSUNLOCKV4ROOTMUTEX();
1281 * For each client...
1283 for (i = 0; i < nfsrv_clienthashsize; i++) {
1284 clp = LIST_FIRST(&nfsclienthash[i]);
1285 while (clp != LIST_END(&nfsclienthash[i])) {
1286 nclp = LIST_NEXT(clp, lc_hash);
1287 if (!(clp->lc_flags & LCL_EXPIREIT)) {
1288 if (((clp->lc_expiry + NFSRV_STALELEASE) < NFSD_MONOSEC
1289 && ((LIST_EMPTY(&clp->lc_deleg)
1290 && LIST_EMPTY(&clp->lc_open)) ||
1291 nfsrv_clients > nfsrv_clienthighwater)) ||
1292 (clp->lc_expiry + NFSRV_MOULDYLEASE) < NFSD_MONOSEC ||
1293 (clp->lc_expiry < NFSD_MONOSEC &&
1294 (nfsrv_openpluslock * 10 / 9) > nfsrv_v4statelimit)) {
1296 * Lease has expired several nfsrv_lease times ago:
1298 * - no state is associated with it
1300 * - above high water mark for number of clients
1301 * (nfsrv_clienthighwater should be large enough
1302 * that this only occurs when clients fail to
1303 * use the same nfs_client_id4.id. Maybe somewhat
1304 * higher that the maximum number of clients that
1305 * will mount this server?)
1307 * Lease has expired a very long time ago
1309 * Lease has expired PLUS the number of opens + locks
1310 * has exceeded 90% of capacity
1312 * --> Mark for expiry. The actual expiry will be done
1313 * by an nfsd sometime soon.
1315 clp->lc_flags |= LCL_EXPIREIT;
1316 nfsrv_stablefirst.nsf_flags |=
1317 (NFSNSF_NEEDLOCK | NFSNSF_EXPIREDCLIENT);
1320 * If there are no opens, increment no open tick cnt
1321 * If time exceeds NFSNOOPEN, mark it to be thrown away
1322 * otherwise, if there is an open, reset no open time
1323 * Hopefully, this will avoid excessive re-creation
1324 * of open owners and subsequent open confirms.
1326 stp = LIST_FIRST(&clp->lc_open);
1327 while (stp != LIST_END(&clp->lc_open)) {
1328 nstp = LIST_NEXT(stp, ls_list);
1329 if (LIST_EMPTY(&stp->ls_open)) {
1331 if (stp->ls_noopens > NFSNOOPEN ||
1332 (nfsrv_openpluslock * 2) >
1334 nfsrv_stablefirst.nsf_flags |=
1337 stp->ls_noopens = 0;
1347 NFSLOCKV4ROOTMUTEX();
1348 nfsv4_relref(&nfsv4rootfs_lock);
1349 NFSUNLOCKV4ROOTMUTEX();
1353 * The following set of functions free up the various data structures.
1356 * Clear out all open/lock state related to this nfsclient.
1357 * Caller must hold an exclusive lock on nfsv4rootfs_lock, so that
1358 * there are no other active nfsd threads.
1361 nfsrv_cleanclient(struct nfsclient *clp, NFSPROC_T *p)
1363 struct nfsstate *stp, *nstp;
1364 struct nfsdsession *sep, *nsep;
1366 LIST_FOREACH_SAFE(stp, &clp->lc_open, ls_list, nstp)
1367 nfsrv_freeopenowner(stp, 1, p);
1368 if ((clp->lc_flags & LCL_ADMINREVOKED) == 0)
1369 LIST_FOREACH_SAFE(sep, &clp->lc_session, sess_list, nsep)
1370 (void)nfsrv_freesession(sep, NULL);
1374 * Free a client that has been cleaned. It should also already have been
1375 * removed from the lists.
1376 * (Just to be safe w.r.t. newnfs_disconnect(), call this function when
1377 * softclock interrupts are enabled.)
1380 nfsrv_zapclient(struct nfsclient *clp, NFSPROC_T *p)
1384 if ((clp->lc_flags & (LCL_GSS | LCL_CALLBACKSON)) ==
1385 (LCL_GSS | LCL_CALLBACKSON) &&
1386 (clp->lc_hand.nfsh_flag & NFSG_COMPLETE) &&
1387 clp->lc_handlelen > 0) {
1388 clp->lc_hand.nfsh_flag &= ~NFSG_COMPLETE;
1389 clp->lc_hand.nfsh_flag |= NFSG_DESTROYED;
1390 (void) nfsrv_docallback(clp, NFSV4PROC_CBNULL,
1391 NULL, 0, NULL, NULL, NULL, 0, p);
1394 newnfs_disconnect(NULL, &clp->lc_req);
1395 free(clp->lc_req.nr_nam, M_SONAME);
1396 NFSFREEMUTEX(&clp->lc_req.nr_mtx);
1397 free(clp->lc_stateid, M_NFSDCLIENT);
1398 free(clp, M_NFSDCLIENT);
1400 nfsstatsv1.srvclients--;
1401 nfsrv_openpluslock--;
1407 * Free a list of delegation state structures.
1408 * (This function will also free all nfslockfile structures that no
1409 * longer have associated state.)
1412 nfsrv_freedeleglist(struct nfsstatehead *sthp)
1414 struct nfsstate *stp, *nstp;
1416 LIST_FOREACH_SAFE(stp, sthp, ls_list, nstp) {
1417 nfsrv_freedeleg(stp);
1423 * Free up a delegation.
1426 nfsrv_freedeleg(struct nfsstate *stp)
1428 struct nfslockfile *lfp;
1430 LIST_REMOVE(stp, ls_hash);
1431 LIST_REMOVE(stp, ls_list);
1432 LIST_REMOVE(stp, ls_file);
1433 if ((stp->ls_flags & NFSLCK_DELEGWRITE) != 0)
1434 nfsrv_writedelegcnt--;
1436 if (LIST_EMPTY(&lfp->lf_open) &&
1437 LIST_EMPTY(&lfp->lf_lock) && LIST_EMPTY(&lfp->lf_deleg) &&
1438 LIST_EMPTY(&lfp->lf_locallock) && LIST_EMPTY(&lfp->lf_rollback) &&
1439 lfp->lf_usecount == 0 &&
1440 nfsv4_testlock(&lfp->lf_locallock_lck) == 0)
1441 nfsrv_freenfslockfile(lfp);
1442 free(stp, M_NFSDSTATE);
1443 nfsstatsv1.srvdelegates--;
1444 nfsrv_openpluslock--;
1445 nfsrv_delegatecnt--;
1449 * This function frees an open owner and all associated opens.
1452 nfsrv_freeopenowner(struct nfsstate *stp, int cansleep, NFSPROC_T *p)
1454 struct nfsstate *nstp, *tstp;
1456 LIST_REMOVE(stp, ls_list);
1458 * Now, free all associated opens.
1460 nstp = LIST_FIRST(&stp->ls_open);
1461 while (nstp != LIST_END(&stp->ls_open)) {
1463 nstp = LIST_NEXT(nstp, ls_list);
1464 (void) nfsrv_freeopen(tstp, NULL, cansleep, p);
1467 nfsrvd_derefcache(stp->ls_op);
1468 free(stp, M_NFSDSTATE);
1469 nfsstatsv1.srvopenowners--;
1470 nfsrv_openpluslock--;
1474 * This function frees an open (nfsstate open structure) with all associated
1475 * lock_owners and locks. It also frees the nfslockfile structure iff there
1476 * are no other opens on the file.
1477 * Returns 1 if it free'd the nfslockfile, 0 otherwise.
1480 nfsrv_freeopen(struct nfsstate *stp, vnode_t vp, int cansleep, NFSPROC_T *p)
1482 struct nfsstate *nstp, *tstp;
1483 struct nfslockfile *lfp;
1486 LIST_REMOVE(stp, ls_hash);
1487 LIST_REMOVE(stp, ls_list);
1488 LIST_REMOVE(stp, ls_file);
1492 * Now, free all lockowners associated with this open.
1494 LIST_FOREACH_SAFE(tstp, &stp->ls_open, ls_list, nstp)
1495 nfsrv_freelockowner(tstp, vp, cansleep, p);
1498 * The nfslockfile is freed here if there are no locks
1499 * associated with the open.
1500 * If there are locks associated with the open, the
1501 * nfslockfile structure can be freed via nfsrv_freelockowner().
1502 * Acquire the state mutex to avoid races with calls to
1503 * nfsrv_getlockfile().
1507 if (lfp != NULL && LIST_EMPTY(&lfp->lf_open) &&
1508 LIST_EMPTY(&lfp->lf_deleg) && LIST_EMPTY(&lfp->lf_lock) &&
1509 LIST_EMPTY(&lfp->lf_locallock) && LIST_EMPTY(&lfp->lf_rollback) &&
1510 lfp->lf_usecount == 0 &&
1511 (cansleep != 0 || nfsv4_testlock(&lfp->lf_locallock_lck) == 0)) {
1512 nfsrv_freenfslockfile(lfp);
1518 free(stp, M_NFSDSTATE);
1519 nfsstatsv1.srvopens--;
1520 nfsrv_openpluslock--;
1525 * Frees a lockowner and all associated locks.
1528 nfsrv_freelockowner(struct nfsstate *stp, vnode_t vp, int cansleep,
1532 LIST_REMOVE(stp, ls_hash);
1533 LIST_REMOVE(stp, ls_list);
1534 nfsrv_freeallnfslocks(stp, vp, cansleep, p);
1536 nfsrvd_derefcache(stp->ls_op);
1537 free(stp, M_NFSDSTATE);
1538 nfsstatsv1.srvlockowners--;
1539 nfsrv_openpluslock--;
1543 * Free all the nfs locks on a lockowner.
1546 nfsrv_freeallnfslocks(struct nfsstate *stp, vnode_t vp, int cansleep,
1549 struct nfslock *lop, *nlop;
1550 struct nfsrollback *rlp, *nrlp;
1551 struct nfslockfile *lfp = NULL;
1554 uint64_t first, end;
1557 ASSERT_VOP_UNLOCKED(vp, "nfsrv_freeallnfslocks: vnode locked");
1558 lop = LIST_FIRST(&stp->ls_lock);
1559 while (lop != LIST_END(&stp->ls_lock)) {
1560 nlop = LIST_NEXT(lop, lo_lckowner);
1562 * Since all locks should be for the same file, lfp should
1567 else if (lfp != lop->lo_lfp)
1568 panic("allnfslocks");
1570 * If vp is NULL and cansleep != 0, a vnode must be acquired
1571 * from the file handle. This only occurs when called from
1572 * nfsrv_cleanclient().
1575 if (nfsrv_dolocallocks == 0)
1577 else if (vp == NULL && cansleep != 0) {
1578 tvp = nfsvno_getvp(&lfp->lf_fh);
1589 first = lop->lo_first;
1591 nfsrv_freenfslock(lop);
1592 nfsrv_localunlock(tvp, lfp, first, end, p);
1593 LIST_FOREACH_SAFE(rlp, &lfp->lf_rollback, rlck_list,
1595 free(rlp, M_NFSDROLLBACK);
1596 LIST_INIT(&lfp->lf_rollback);
1598 nfsrv_freenfslock(lop);
1601 if (vp == NULL && tvp != NULL)
1606 * Free an nfslock structure.
1609 nfsrv_freenfslock(struct nfslock *lop)
1612 if (lop->lo_lckfile.le_prev != NULL) {
1613 LIST_REMOVE(lop, lo_lckfile);
1614 nfsstatsv1.srvlocks--;
1615 nfsrv_openpluslock--;
1617 LIST_REMOVE(lop, lo_lckowner);
1618 free(lop, M_NFSDLOCK);
1622 * This function frees an nfslockfile structure.
1625 nfsrv_freenfslockfile(struct nfslockfile *lfp)
1628 LIST_REMOVE(lfp, lf_hash);
1629 free(lfp, M_NFSDLOCKFILE);
1633 * This function looks up an nfsstate structure via stateid.
1636 nfsrv_getstate(struct nfsclient *clp, nfsv4stateid_t *stateidp, __unused u_int32_t flags,
1637 struct nfsstate **stpp)
1639 struct nfsstate *stp;
1640 struct nfsstatehead *hp;
1644 hp = NFSSTATEHASH(clp, *stateidp);
1645 LIST_FOREACH(stp, hp, ls_hash) {
1646 if (!NFSBCMP(stp->ls_stateid.other, stateidp->other,
1652 * If no state id in list, return NFSERR_BADSTATEID.
1654 if (stp == LIST_END(hp)) {
1655 error = NFSERR_BADSTATEID;
1666 * This function gets an nfsstate structure via owner string.
1669 nfsrv_getowner(struct nfsstatehead *hp, struct nfsstate *new_stp,
1670 struct nfsstate **stpp)
1672 struct nfsstate *stp;
1675 LIST_FOREACH(stp, hp, ls_list) {
1676 if (new_stp->ls_ownerlen == stp->ls_ownerlen &&
1677 !NFSBCMP(new_stp->ls_owner,stp->ls_owner,stp->ls_ownerlen)) {
1685 * Lock control function called to update lock status.
1686 * Returns 0 upon success, -1 if there is no lock and the flags indicate
1687 * that one isn't to be created and an NFSERR_xxx for other errors.
1688 * The structures new_stp and new_lop are passed in as pointers that should
1689 * be set to NULL if the structure is used and shouldn't be free'd.
1690 * For the NFSLCK_TEST and NFSLCK_CHECK cases, the structures are
1691 * never used and can safely be allocated on the stack. For all other
1692 * cases, *new_stpp and *new_lopp should be malloc'd before the call,
1693 * in case they are used.
1696 nfsrv_lockctrl(vnode_t vp, struct nfsstate **new_stpp,
1697 struct nfslock **new_lopp, struct nfslockconflict *cfp,
1698 nfsquad_t clientid, nfsv4stateid_t *stateidp,
1699 __unused struct nfsexstuff *exp,
1700 struct nfsrv_descript *nd, NFSPROC_T *p)
1702 struct nfslock *lop;
1703 struct nfsstate *new_stp = *new_stpp;
1704 struct nfslock *new_lop = *new_lopp;
1705 struct nfsstate *tstp, *mystp, *nstp;
1707 struct nfslockfile *lfp;
1708 struct nfslock *other_lop = NULL;
1709 struct nfsstate *stp, *lckstp = NULL;
1710 struct nfsclient *clp = NULL;
1712 int error = 0, haslock = 0, ret, reterr;
1713 int getlckret, delegation = 0, filestruct_locked, vnode_unlocked = 0;
1715 uint64_t first, end;
1716 uint32_t lock_flags;
1718 if (new_stp->ls_flags & (NFSLCK_CHECK | NFSLCK_SETATTR)) {
1720 * Note the special cases of "all 1s" or "all 0s" stateids and
1721 * let reads with all 1s go ahead.
1723 if (new_stp->ls_stateid.seqid == 0x0 &&
1724 new_stp->ls_stateid.other[0] == 0x0 &&
1725 new_stp->ls_stateid.other[1] == 0x0 &&
1726 new_stp->ls_stateid.other[2] == 0x0)
1728 else if (new_stp->ls_stateid.seqid == 0xffffffff &&
1729 new_stp->ls_stateid.other[0] == 0xffffffff &&
1730 new_stp->ls_stateid.other[1] == 0xffffffff &&
1731 new_stp->ls_stateid.other[2] == 0xffffffff)
1736 * Check for restart conditions (client and server).
1738 error = nfsrv_checkrestart(clientid, new_stp->ls_flags,
1739 &new_stp->ls_stateid, specialid);
1744 * Check for state resource limit exceeded.
1746 if ((new_stp->ls_flags & NFSLCK_LOCK) &&
1747 nfsrv_openpluslock > nfsrv_v4statelimit) {
1748 error = NFSERR_RESOURCE;
1753 * For the lock case, get another nfslock structure,
1754 * just in case we need it.
1755 * Malloc now, before we start sifting through the linked lists,
1756 * in case we have to wait for memory.
1759 if (new_stp->ls_flags & NFSLCK_LOCK)
1760 other_lop = malloc(sizeof (struct nfslock),
1761 M_NFSDLOCK, M_WAITOK);
1762 filestruct_locked = 0;
1767 * Get the lockfile structure for CFH now, so we can do a sanity
1768 * check against the stateid, before incrementing the seqid#, since
1769 * we want to return NFSERR_BADSTATEID on failure and the seqid#
1770 * shouldn't be incremented for this case.
1771 * If nfsrv_getlockfile() returns -1, it means "not found", which
1772 * will be handled later.
1773 * If we are doing Lock/LockU and local locking is enabled, sleep
1774 * lock the nfslockfile structure.
1776 getlckret = nfsrv_getlockfh(vp, new_stp->ls_flags, NULL, &nfh, p);
1778 if (getlckret == 0) {
1779 if ((new_stp->ls_flags & (NFSLCK_LOCK | NFSLCK_UNLOCK)) != 0 &&
1780 nfsrv_dolocallocks != 0 && nd->nd_repstat == 0) {
1781 getlckret = nfsrv_getlockfile(new_stp->ls_flags, NULL,
1784 filestruct_locked = 1;
1786 getlckret = nfsrv_getlockfile(new_stp->ls_flags, NULL,
1789 if (getlckret != 0 && getlckret != -1)
1792 if (filestruct_locked != 0) {
1793 LIST_INIT(&lfp->lf_rollback);
1794 if ((new_stp->ls_flags & NFSLCK_LOCK)) {
1796 * For local locking, do the advisory locking now, so
1797 * that any conflict can be detected. A failure later
1798 * can be rolled back locally. If an error is returned,
1799 * struct nfslockfile has been unlocked and any local
1800 * locking rolled back.
1803 if (vnode_unlocked == 0) {
1804 ASSERT_VOP_ELOCKED(vp, "nfsrv_lockctrl1");
1808 reterr = nfsrv_locallock(vp, lfp,
1809 (new_lop->lo_flags & (NFSLCK_READ | NFSLCK_WRITE)),
1810 new_lop->lo_first, new_lop->lo_end, cfp, p);
1815 if (specialid == 0) {
1816 if (new_stp->ls_flags & NFSLCK_TEST) {
1818 * RFC 3530 does not list LockT as an op that renews a
1819 * lease, but the consensus seems to be that it is ok
1820 * for a server to do so.
1822 error = nfsrv_getclient(clientid, CLOPS_RENEW, &clp, NULL,
1823 (nfsquad_t)((u_quad_t)0), 0, nd, p);
1826 * Since NFSERR_EXPIRED, NFSERR_ADMINREVOKED are not valid
1827 * error returns for LockT, just go ahead and test for a lock,
1828 * since there are no locks for this client, but other locks
1829 * can conflict. (ie. same client will always be false)
1831 if (error == NFSERR_EXPIRED || error == NFSERR_ADMINREVOKED)
1835 error = nfsrv_getclient(clientid, CLOPS_RENEW, &clp, NULL,
1836 (nfsquad_t)((u_quad_t)0), 0, nd, p);
1839 * Look up the stateid
1841 error = nfsrv_getstate(clp, &new_stp->ls_stateid,
1842 new_stp->ls_flags, &stp);
1844 * do some sanity checks for an unconfirmed open or a
1845 * stateid that refers to the wrong file, for an open stateid
1847 if (error == 0 && (stp->ls_flags & NFSLCK_OPEN) &&
1848 ((stp->ls_openowner->ls_flags & NFSLCK_NEEDSCONFIRM) ||
1849 (getlckret == 0 && stp->ls_lfp != lfp))){
1851 * NFSLCK_SETATTR should return OK rather than NFSERR_BADSTATEID
1852 * The only exception is using SETATTR with SIZE.
1854 if ((new_stp->ls_flags &
1855 (NFSLCK_SETATTR | NFSLCK_CHECK)) != NFSLCK_SETATTR)
1856 error = NFSERR_BADSTATEID;
1860 (stp->ls_flags & (NFSLCK_DELEGREAD | NFSLCK_DELEGWRITE)) &&
1861 getlckret == 0 && stp->ls_lfp != lfp)
1862 error = NFSERR_BADSTATEID;
1865 * If the lockowner stateid doesn't refer to the same file,
1866 * I believe that is considered ok, since some clients will
1867 * only create a single lockowner and use that for all locks
1869 * For now, log it as a diagnostic, instead of considering it
1872 if (error == 0 && (stp->ls_flags &
1873 (NFSLCK_OPEN | NFSLCK_DELEGREAD | NFSLCK_DELEGWRITE)) == 0 &&
1874 getlckret == 0 && stp->ls_lfp != lfp) {
1876 printf("Got a lock statid for different file open\n");
1879 error = NFSERR_BADSTATEID;
1884 if (new_stp->ls_flags & NFSLCK_OPENTOLOCK) {
1886 * If haslock set, we've already checked the seqid.
1889 if (stp->ls_flags & NFSLCK_OPEN)
1890 error = nfsrv_checkseqid(nd, new_stp->ls_seq,
1891 stp->ls_openowner, new_stp->ls_op);
1893 error = NFSERR_BADSTATEID;
1896 nfsrv_getowner(&stp->ls_open, new_stp, &lckstp);
1899 * For NFSv4.1 and NFSv4.2 allow an
1900 * open_to_lock_owner when the lock_owner already
1901 * exists. Just clear NFSLCK_OPENTOLOCK so that
1902 * a new lock_owner will not be created.
1903 * RFC7530 states that the error for NFSv4.0
1904 * is NFS4ERR_BAD_SEQID.
1906 if ((nd->nd_flag & ND_NFSV41) != 0)
1907 new_stp->ls_flags &= ~NFSLCK_OPENTOLOCK;
1909 error = NFSERR_BADSEQID;
1912 } else if (new_stp->ls_flags&(NFSLCK_LOCK|NFSLCK_UNLOCK)) {
1914 * If haslock set, ditto above.
1917 if (stp->ls_flags & NFSLCK_OPEN)
1918 error = NFSERR_BADSTATEID;
1920 error = nfsrv_checkseqid(nd, new_stp->ls_seq,
1921 stp, new_stp->ls_op);
1929 * If the seqid part of the stateid isn't the same, return
1930 * NFSERR_OLDSTATEID for cases other than I/O Ops.
1931 * For I/O Ops, only return NFSERR_OLDSTATEID if
1932 * nfsrv_returnoldstateid is set. (The consensus on the email
1933 * list was that most clients would prefer to not receive
1934 * NFSERR_OLDSTATEID for I/O Ops, but the RFC suggests that that
1935 * is what will happen, so I use the nfsrv_returnoldstateid to
1936 * allow for either server configuration.)
1938 if (!error && stp->ls_stateid.seqid!=new_stp->ls_stateid.seqid &&
1939 (((nd->nd_flag & ND_NFSV41) == 0 &&
1940 (!(new_stp->ls_flags & NFSLCK_CHECK) ||
1941 nfsrv_returnoldstateid)) ||
1942 ((nd->nd_flag & ND_NFSV41) != 0 &&
1943 new_stp->ls_stateid.seqid != 0)))
1944 error = NFSERR_OLDSTATEID;
1949 * Now we can check for grace.
1952 error = nfsrv_checkgrace(nd, clp, new_stp->ls_flags);
1953 if ((new_stp->ls_flags & NFSLCK_RECLAIM) && !error &&
1954 nfsrv_checkstable(clp))
1955 error = NFSERR_NOGRACE;
1957 * If we successfully Reclaimed state, note that.
1959 if ((new_stp->ls_flags & NFSLCK_RECLAIM) && !error)
1960 nfsrv_markstable(clp);
1963 * At this point, either error == NFSERR_BADSTATEID or the
1964 * seqid# has been updated, so we can return any error.
1965 * If error == 0, there may be an error in:
1966 * nd_repstat - Set by the calling function.
1967 * reterr - Set above, if getting the nfslockfile structure
1968 * or acquiring the local lock failed.
1969 * (If both of these are set, nd_repstat should probably be
1970 * returned, since that error was detected before this
1973 if (error != 0 || nd->nd_repstat != 0 || reterr != 0) {
1975 if (nd->nd_repstat != 0)
1976 error = nd->nd_repstat;
1980 if (filestruct_locked != 0) {
1981 /* Roll back local locks. */
1983 if (vnode_unlocked == 0) {
1984 ASSERT_VOP_ELOCKED(vp, "nfsrv_lockctrl2");
1988 nfsrv_locallock_rollback(vp, lfp, p);
1990 nfsrv_unlocklf(lfp);
1997 * Check the nfsrv_getlockfile return.
1998 * Returned -1 if no structure found.
2000 if (getlckret == -1) {
2001 error = NFSERR_EXPIRED;
2003 * Called from lockt, so no lock is OK.
2005 if (new_stp->ls_flags & NFSLCK_TEST) {
2007 } else if (new_stp->ls_flags &
2008 (NFSLCK_CHECK | NFSLCK_SETATTR)) {
2010 * Called to check for a lock, OK if the stateid is all
2011 * 1s or all 0s, but there should be an nfsstate
2013 * (ie. If there is no open, I'll assume no share
2019 error = NFSERR_BADSTATEID;
2026 * For NFSLCK_CHECK and NFSLCK_LOCK, test for a share conflict.
2027 * For NFSLCK_CHECK, allow a read if write access is granted,
2028 * but check for a deny. For NFSLCK_LOCK, require correct access,
2029 * which implies a conflicting deny can't exist.
2031 if (new_stp->ls_flags & (NFSLCK_CHECK | NFSLCK_LOCK)) {
2033 * Four kinds of state id:
2034 * - specialid (all 0s or all 1s), only for NFSLCK_CHECK
2035 * - stateid for an open
2036 * - stateid for a delegation
2037 * - stateid for a lock owner
2040 if (stp->ls_flags & (NFSLCK_DELEGREAD | NFSLCK_DELEGWRITE)) {
2043 nfsrv_delaydelegtimeout(stp);
2044 } else if (stp->ls_flags & NFSLCK_OPEN) {
2047 mystp = stp->ls_openstp;
2050 * If locking or checking, require correct access
2053 if (((new_stp->ls_flags & NFSLCK_LOCK) &&
2054 !((new_lop->lo_flags >> NFSLCK_LOCKSHIFT) &
2055 mystp->ls_flags & NFSLCK_ACCESSBITS)) ||
2056 ((new_stp->ls_flags & (NFSLCK_CHECK|NFSLCK_READACCESS)) ==
2057 (NFSLCK_CHECK | NFSLCK_READACCESS) &&
2058 !(mystp->ls_flags & NFSLCK_READACCESS) &&
2059 nfsrv_allowreadforwriteopen == 0) ||
2060 ((new_stp->ls_flags & (NFSLCK_CHECK|NFSLCK_WRITEACCESS)) ==
2061 (NFSLCK_CHECK | NFSLCK_WRITEACCESS) &&
2062 !(mystp->ls_flags & NFSLCK_WRITEACCESS))) {
2063 if (filestruct_locked != 0) {
2064 /* Roll back local locks. */
2066 if (vnode_unlocked == 0) {
2067 ASSERT_VOP_ELOCKED(vp,
2072 nfsrv_locallock_rollback(vp, lfp, p);
2074 nfsrv_unlocklf(lfp);
2077 error = NFSERR_OPENMODE;
2082 if ((new_stp->ls_flags & NFSLCK_CHECK) && !delegation) {
2084 * Check for a conflicting deny bit.
2086 LIST_FOREACH(tstp, &lfp->lf_open, ls_file) {
2087 if (tstp != mystp) {
2088 bits = tstp->ls_flags;
2089 bits >>= NFSLCK_SHIFT;
2090 if (new_stp->ls_flags & bits & NFSLCK_ACCESSBITS) {
2091 KASSERT(vnode_unlocked == 0,
2092 ("nfsrv_lockctrl: vnode unlocked1"));
2093 ret = nfsrv_clientconflict(tstp->ls_clp, &haslock,
2097 * nfsrv_clientconflict unlocks state
2098 * when it returns non-zero.
2106 error = NFSERR_PERM;
2108 error = NFSERR_OPENMODE;
2114 /* We're outta here */
2121 * For setattr, just get rid of all the Delegations for other clients.
2123 if (new_stp->ls_flags & NFSLCK_SETATTR) {
2124 KASSERT(vnode_unlocked == 0,
2125 ("nfsrv_lockctrl: vnode unlocked2"));
2126 ret = nfsrv_cleandeleg(vp, lfp, clp, &haslock, p);
2129 * nfsrv_cleandeleg() unlocks state when it
2139 if (!(new_stp->ls_flags & NFSLCK_CHECK) ||
2140 (LIST_EMPTY(&lfp->lf_open) && LIST_EMPTY(&lfp->lf_lock) &&
2141 LIST_EMPTY(&lfp->lf_deleg))) {
2148 * Check for a conflicting delegation. If one is found, call
2149 * nfsrv_delegconflict() to handle it. If the v4root lock hasn't
2150 * been set yet, it will get the lock. Otherwise, it will recall
2151 * the delegation. Then, we try try again...
2152 * I currently believe the conflict algorithm to be:
2153 * For Lock Ops (Lock/LockT/LockU)
2154 * - there is a conflict iff a different client has a write delegation
2155 * For Reading (Read Op)
2156 * - there is a conflict iff a different client has a write delegation
2157 * (the specialids are always a different client)
2158 * For Writing (Write/Setattr of size)
2159 * - there is a conflict if a different client has any delegation
2160 * - there is a conflict if the same client has a read delegation
2161 * (I don't understand why this isn't allowed, but that seems to be
2162 * the current consensus?)
2164 tstp = LIST_FIRST(&lfp->lf_deleg);
2165 while (tstp != LIST_END(&lfp->lf_deleg)) {
2166 nstp = LIST_NEXT(tstp, ls_file);
2167 if ((((new_stp->ls_flags&(NFSLCK_LOCK|NFSLCK_UNLOCK|NFSLCK_TEST))||
2168 ((new_stp->ls_flags & NFSLCK_CHECK) &&
2169 (new_lop->lo_flags & NFSLCK_READ))) &&
2170 clp != tstp->ls_clp &&
2171 (tstp->ls_flags & NFSLCK_DELEGWRITE)) ||
2172 ((new_stp->ls_flags & NFSLCK_CHECK) &&
2173 (new_lop->lo_flags & NFSLCK_WRITE) &&
2174 (clp != tstp->ls_clp ||
2175 (tstp->ls_flags & NFSLCK_DELEGREAD)))) {
2177 if (filestruct_locked != 0) {
2178 /* Roll back local locks. */
2180 if (vnode_unlocked == 0) {
2181 ASSERT_VOP_ELOCKED(vp, "nfsrv_lockctrl4");
2184 nfsrv_locallock_rollback(vp, lfp, p);
2186 nfsrv_unlocklf(lfp);
2188 NFSVOPLOCK(vp, LK_EXCLUSIVE | LK_RETRY);
2190 if (VN_IS_DOOMED(vp))
2191 ret = NFSERR_SERVERFAULT;
2195 ret = nfsrv_delegconflict(tstp, &haslock, p, vp);
2198 * nfsrv_delegconflict unlocks state when it
2199 * returns non-zero, which it always does.
2202 free(other_lop, M_NFSDLOCK);
2212 /* Never gets here. */
2218 * Handle the unlock case by calling nfsrv_updatelock().
2219 * (Should I have done some access checking above for unlock? For now,
2220 * just let it happen.)
2222 if (new_stp->ls_flags & NFSLCK_UNLOCK) {
2223 first = new_lop->lo_first;
2224 end = new_lop->lo_end;
2225 nfsrv_updatelock(stp, new_lopp, &other_lop, lfp);
2226 stateidp->seqid = ++(stp->ls_stateid.seqid);
2227 if ((nd->nd_flag & ND_NFSV41) != 0 && stateidp->seqid == 0)
2228 stateidp->seqid = stp->ls_stateid.seqid = 1;
2229 stateidp->other[0] = stp->ls_stateid.other[0];
2230 stateidp->other[1] = stp->ls_stateid.other[1];
2231 stateidp->other[2] = stp->ls_stateid.other[2];
2232 if (filestruct_locked != 0) {
2234 if (vnode_unlocked == 0) {
2235 ASSERT_VOP_ELOCKED(vp, "nfsrv_lockctrl5");
2239 /* Update the local locks. */
2240 nfsrv_localunlock(vp, lfp, first, end, p);
2242 nfsrv_unlocklf(lfp);
2249 * Search for a conflicting lock. A lock conflicts if:
2250 * - the lock range overlaps and
2251 * - at least one lock is a write lock and
2252 * - it is not owned by the same lock owner
2255 LIST_FOREACH(lop, &lfp->lf_lock, lo_lckfile) {
2256 if (new_lop->lo_end > lop->lo_first &&
2257 new_lop->lo_first < lop->lo_end &&
2258 (new_lop->lo_flags == NFSLCK_WRITE ||
2259 lop->lo_flags == NFSLCK_WRITE) &&
2260 lckstp != lop->lo_stp &&
2261 (clp != lop->lo_stp->ls_clp ||
2262 lckstp->ls_ownerlen != lop->lo_stp->ls_ownerlen ||
2263 NFSBCMP(lckstp->ls_owner, lop->lo_stp->ls_owner,
2264 lckstp->ls_ownerlen))) {
2266 free(other_lop, M_NFSDLOCK);
2269 if (vnode_unlocked != 0)
2270 ret = nfsrv_clientconflict(lop->lo_stp->ls_clp, &haslock,
2273 ret = nfsrv_clientconflict(lop->lo_stp->ls_clp, &haslock,
2276 if (filestruct_locked != 0) {
2277 if (vnode_unlocked == 0) {
2278 ASSERT_VOP_ELOCKED(vp, "nfsrv_lockctrl6");
2281 /* Roll back local locks. */
2282 nfsrv_locallock_rollback(vp, lfp, p);
2284 nfsrv_unlocklf(lfp);
2286 NFSVOPLOCK(vp, LK_EXCLUSIVE | LK_RETRY);
2288 if (VN_IS_DOOMED(vp)) {
2289 error = NFSERR_SERVERFAULT;
2294 * nfsrv_clientconflict() unlocks state when it
2301 * Found a conflicting lock, so record the conflict and
2304 if (cfp != NULL && ret == 0) {
2305 cfp->cl_clientid.lval[0]=lop->lo_stp->ls_stateid.other[0];
2306 cfp->cl_clientid.lval[1]=lop->lo_stp->ls_stateid.other[1];
2307 cfp->cl_first = lop->lo_first;
2308 cfp->cl_end = lop->lo_end;
2309 cfp->cl_flags = lop->lo_flags;
2310 cfp->cl_ownerlen = lop->lo_stp->ls_ownerlen;
2311 NFSBCOPY(lop->lo_stp->ls_owner, cfp->cl_owner,
2315 error = NFSERR_PERM;
2316 else if (new_stp->ls_flags & NFSLCK_RECLAIM)
2317 error = NFSERR_RECLAIMCONFLICT;
2318 else if (new_stp->ls_flags & NFSLCK_CHECK)
2319 error = NFSERR_LOCKED;
2321 error = NFSERR_DENIED;
2322 if (filestruct_locked != 0 && ret == 0) {
2323 /* Roll back local locks. */
2325 if (vnode_unlocked == 0) {
2326 ASSERT_VOP_ELOCKED(vp, "nfsrv_lockctrl7");
2330 nfsrv_locallock_rollback(vp, lfp, p);
2332 nfsrv_unlocklf(lfp);
2342 * We only get here if there was no lock that conflicted.
2344 if (new_stp->ls_flags & (NFSLCK_TEST | NFSLCK_CHECK)) {
2350 * We only get here when we are creating or modifying a lock.
2351 * There are two variants:
2352 * - exist_lock_owner where lock_owner exists
2353 * - open_to_lock_owner with new lock_owner
2355 first = new_lop->lo_first;
2356 end = new_lop->lo_end;
2357 lock_flags = new_lop->lo_flags;
2358 if (!(new_stp->ls_flags & NFSLCK_OPENTOLOCK)) {
2359 nfsrv_updatelock(lckstp, new_lopp, &other_lop, lfp);
2360 stateidp->seqid = ++(lckstp->ls_stateid.seqid);
2361 if ((nd->nd_flag & ND_NFSV41) != 0 && stateidp->seqid == 0)
2362 stateidp->seqid = lckstp->ls_stateid.seqid = 1;
2363 stateidp->other[0] = lckstp->ls_stateid.other[0];
2364 stateidp->other[1] = lckstp->ls_stateid.other[1];
2365 stateidp->other[2] = lckstp->ls_stateid.other[2];
2368 * The new open_to_lock_owner case.
2369 * Link the new nfsstate into the lists.
2371 new_stp->ls_seq = new_stp->ls_opentolockseq;
2372 nfsrvd_refcache(new_stp->ls_op);
2373 stateidp->seqid = new_stp->ls_stateid.seqid = 1;
2374 stateidp->other[0] = new_stp->ls_stateid.other[0] =
2375 clp->lc_clientid.lval[0];
2376 stateidp->other[1] = new_stp->ls_stateid.other[1] =
2377 clp->lc_clientid.lval[1];
2378 stateidp->other[2] = new_stp->ls_stateid.other[2] =
2379 nfsrv_nextstateindex(clp);
2380 new_stp->ls_clp = clp;
2381 LIST_INIT(&new_stp->ls_lock);
2382 new_stp->ls_openstp = stp;
2383 new_stp->ls_lfp = lfp;
2384 nfsrv_insertlock(new_lop, (struct nfslock *)new_stp, new_stp,
2386 LIST_INSERT_HEAD(NFSSTATEHASH(clp, new_stp->ls_stateid),
2388 LIST_INSERT_HEAD(&stp->ls_open, new_stp, ls_list);
2391 nfsstatsv1.srvlockowners++;
2392 nfsrv_openpluslock++;
2394 if (filestruct_locked != 0) {
2396 nfsrv_locallock_commit(lfp, lock_flags, first, end);
2398 nfsrv_unlocklf(lfp);
2404 NFSLOCKV4ROOTMUTEX();
2405 nfsv4_unlock(&nfsv4rootfs_lock, 1);
2406 NFSUNLOCKV4ROOTMUTEX();
2408 if (vnode_unlocked != 0) {
2409 NFSVOPLOCK(vp, LK_EXCLUSIVE | LK_RETRY);
2410 if (error == 0 && VN_IS_DOOMED(vp))
2411 error = NFSERR_SERVERFAULT;
2414 free(other_lop, M_NFSDLOCK);
2415 NFSEXITCODE2(error, nd);
2420 * Check for state errors for Open.
2421 * repstat is passed back out as an error if more critical errors
2425 nfsrv_opencheck(nfsquad_t clientid, nfsv4stateid_t *stateidp,
2426 struct nfsstate *new_stp, vnode_t vp, struct nfsrv_descript *nd,
2427 NFSPROC_T *p, int repstat)
2429 struct nfsstate *stp, *nstp;
2430 struct nfsclient *clp;
2431 struct nfsstate *ownerstp;
2432 struct nfslockfile *lfp, *new_lfp;
2433 int error = 0, haslock = 0, ret, readonly = 0, getfhret = 0;
2435 if ((new_stp->ls_flags & NFSLCK_SHAREBITS) == NFSLCK_READACCESS)
2438 * Check for restart conditions (client and server).
2440 error = nfsrv_checkrestart(clientid, new_stp->ls_flags,
2441 &new_stp->ls_stateid, 0);
2446 * Check for state resource limit exceeded.
2447 * Technically this should be SMP protected, but the worst
2448 * case error is "out by one or two" on the count when it
2449 * returns NFSERR_RESOURCE and the limit is just a rather
2450 * arbitrary high water mark, so no harm is done.
2452 if (nfsrv_openpluslock > nfsrv_v4statelimit) {
2453 error = NFSERR_RESOURCE;
2458 new_lfp = malloc(sizeof (struct nfslockfile),
2459 M_NFSDLOCKFILE, M_WAITOK);
2461 getfhret = nfsrv_getlockfh(vp, new_stp->ls_flags, new_lfp,
2465 * Get the nfsclient structure.
2467 error = nfsrv_getclient(clientid, CLOPS_RENEW, &clp, NULL,
2468 (nfsquad_t)((u_quad_t)0), 0, nd, p);
2471 * Look up the open owner. See if it needs confirmation and
2472 * check the seq#, as required.
2475 nfsrv_getowner(&clp->lc_open, new_stp, &ownerstp);
2477 if (!error && ownerstp) {
2478 error = nfsrv_checkseqid(nd, new_stp->ls_seq, ownerstp,
2481 * If the OpenOwner hasn't been confirmed, assume the
2482 * old one was a replay and this one is ok.
2483 * See: RFC3530 Sec. 14.2.18.
2485 if (error == NFSERR_BADSEQID &&
2486 (ownerstp->ls_flags & NFSLCK_NEEDSCONFIRM))
2494 error = nfsrv_checkgrace(nd, clp, new_stp->ls_flags);
2495 if ((new_stp->ls_flags & NFSLCK_RECLAIM) && !error &&
2496 nfsrv_checkstable(clp))
2497 error = NFSERR_NOGRACE;
2500 * If none of the above errors occurred, let repstat be
2503 if (repstat && !error)
2508 NFSLOCKV4ROOTMUTEX();
2509 nfsv4_unlock(&nfsv4rootfs_lock, 1);
2510 NFSUNLOCKV4ROOTMUTEX();
2512 free(new_lfp, M_NFSDLOCKFILE);
2517 * If vp == NULL, the file doesn't exist yet, so return ok.
2518 * (This always happens on the first pass, so haslock must be 0.)
2522 free(new_lfp, M_NFSDLOCKFILE);
2527 * Get the structure for the underlying file.
2532 error = nfsrv_getlockfile(new_stp->ls_flags, &new_lfp, &lfp,
2535 free(new_lfp, M_NFSDLOCKFILE);
2539 NFSLOCKV4ROOTMUTEX();
2540 nfsv4_unlock(&nfsv4rootfs_lock, 1);
2541 NFSUNLOCKV4ROOTMUTEX();
2547 * Search for a conflicting open/share.
2549 if (new_stp->ls_flags & NFSLCK_DELEGCUR) {
2551 * For Delegate_Cur, search for the matching Delegation,
2552 * which indicates no conflict.
2553 * An old delegation should have been recovered by the
2554 * client doing a Claim_DELEGATE_Prev, so I won't let
2555 * it match and return NFSERR_EXPIRED. Should I let it
2558 LIST_FOREACH(stp, &lfp->lf_deleg, ls_file) {
2559 if (!(stp->ls_flags & NFSLCK_OLDDELEG) &&
2560 (((nd->nd_flag & ND_NFSV41) != 0 &&
2561 stateidp->seqid == 0) ||
2562 stateidp->seqid == stp->ls_stateid.seqid) &&
2563 !NFSBCMP(stateidp->other, stp->ls_stateid.other,
2567 if (stp == LIST_END(&lfp->lf_deleg) ||
2568 ((new_stp->ls_flags & NFSLCK_WRITEACCESS) &&
2569 (stp->ls_flags & NFSLCK_DELEGREAD))) {
2572 NFSLOCKV4ROOTMUTEX();
2573 nfsv4_unlock(&nfsv4rootfs_lock, 1);
2574 NFSUNLOCKV4ROOTMUTEX();
2576 error = NFSERR_EXPIRED;
2582 * Check for access/deny bit conflicts. I check for the same
2583 * owner as well, in case the client didn't bother.
2585 LIST_FOREACH(stp, &lfp->lf_open, ls_file) {
2586 if (!(new_stp->ls_flags & NFSLCK_DELEGCUR) &&
2587 (((new_stp->ls_flags & NFSLCK_ACCESSBITS) &
2588 ((stp->ls_flags>>NFSLCK_SHIFT) & NFSLCK_ACCESSBITS))||
2589 ((stp->ls_flags & NFSLCK_ACCESSBITS) &
2590 ((new_stp->ls_flags>>NFSLCK_SHIFT)&NFSLCK_ACCESSBITS)))){
2591 ret = nfsrv_clientconflict(stp->ls_clp,&haslock,vp,p);
2594 * nfsrv_clientconflict() unlocks
2595 * state when it returns non-zero.
2600 error = NFSERR_PERM;
2601 else if (new_stp->ls_flags & NFSLCK_RECLAIM)
2602 error = NFSERR_RECLAIMCONFLICT;
2604 error = NFSERR_SHAREDENIED;
2608 NFSLOCKV4ROOTMUTEX();
2609 nfsv4_unlock(&nfsv4rootfs_lock, 1);
2610 NFSUNLOCKV4ROOTMUTEX();
2617 * Check for a conflicting delegation. If one is found, call
2618 * nfsrv_delegconflict() to handle it. If the v4root lock hasn't
2619 * been set yet, it will get the lock. Otherwise, it will recall
2620 * the delegation. Then, we try try again...
2621 * (If NFSLCK_DELEGCUR is set, it has a delegation, so there
2622 * isn't a conflict.)
2623 * I currently believe the conflict algorithm to be:
2624 * For Open with Read Access and Deny None
2625 * - there is a conflict iff a different client has a write delegation
2626 * For Open with other Write Access or any Deny except None
2627 * - there is a conflict if a different client has any delegation
2628 * - there is a conflict if the same client has a read delegation
2629 * (The current consensus is that this last case should be
2630 * considered a conflict since the client with a read delegation
2631 * could have done an Open with ReadAccess and WriteDeny
2632 * locally and then not have checked for the WriteDeny.)
2633 * Don't check for a Reclaim, since that will be dealt with
2634 * by nfsrv_openctrl().
2636 if (!(new_stp->ls_flags &
2637 (NFSLCK_DELEGPREV | NFSLCK_DELEGCUR | NFSLCK_RECLAIM))) {
2638 stp = LIST_FIRST(&lfp->lf_deleg);
2639 while (stp != LIST_END(&lfp->lf_deleg)) {
2640 nstp = LIST_NEXT(stp, ls_file);
2641 if ((readonly && stp->ls_clp != clp &&
2642 (stp->ls_flags & NFSLCK_DELEGWRITE)) ||
2643 (!readonly && (stp->ls_clp != clp ||
2644 (stp->ls_flags & NFSLCK_DELEGREAD)))) {
2645 ret = nfsrv_delegconflict(stp, &haslock, p, vp);
2648 * nfsrv_delegconflict() unlocks state
2649 * when it returns non-zero.
2662 NFSLOCKV4ROOTMUTEX();
2663 nfsv4_unlock(&nfsv4rootfs_lock, 1);
2664 NFSUNLOCKV4ROOTMUTEX();
2668 NFSEXITCODE2(error, nd);
2673 * Open control function to create/update open state for an open.
2676 nfsrv_openctrl(struct nfsrv_descript *nd, vnode_t vp,
2677 struct nfsstate **new_stpp, nfsquad_t clientid, nfsv4stateid_t *stateidp,
2678 nfsv4stateid_t *delegstateidp, u_int32_t *rflagsp, struct nfsexstuff *exp,
2679 NFSPROC_T *p, u_quad_t filerev)
2681 struct nfsstate *new_stp = *new_stpp;
2682 struct nfsstate *stp, *nstp;
2683 struct nfsstate *openstp = NULL, *new_open, *ownerstp, *new_deleg;
2684 struct nfslockfile *lfp, *new_lfp;
2685 struct nfsclient *clp;
2686 int error = 0, haslock = 0, ret, delegate = 1, writedeleg = 1;
2687 int readonly = 0, cbret = 1, getfhret = 0;
2688 int gotstate = 0, len = 0;
2689 u_char *clidp = NULL;
2691 if ((new_stp->ls_flags & NFSLCK_SHAREBITS) == NFSLCK_READACCESS)
2694 * Check for restart conditions (client and server).
2695 * (Paranoia, should have been detected by nfsrv_opencheck().)
2696 * If an error does show up, return NFSERR_EXPIRED, since the
2697 * the seqid# has already been incremented.
2699 error = nfsrv_checkrestart(clientid, new_stp->ls_flags,
2700 &new_stp->ls_stateid, 0);
2702 printf("Nfsd: openctrl unexpected restart err=%d\n",
2704 error = NFSERR_EXPIRED;
2708 clidp = malloc(NFSV4_OPAQUELIMIT, M_TEMP, M_WAITOK);
2710 new_lfp = malloc(sizeof (struct nfslockfile),
2711 M_NFSDLOCKFILE, M_WAITOK);
2712 new_open = malloc(sizeof (struct nfsstate),
2713 M_NFSDSTATE, M_WAITOK);
2714 new_deleg = malloc(sizeof (struct nfsstate),
2715 M_NFSDSTATE, M_WAITOK);
2716 getfhret = nfsrv_getlockfh(vp, new_stp->ls_flags, new_lfp,
2720 * Get the client structure. Since the linked lists could be changed
2721 * by other nfsd processes if this process does a tsleep(), one of
2722 * two things must be done.
2723 * 1 - don't tsleep()
2725 * 2 - get the nfsv4_lock() { indicated by haslock == 1 }
2726 * before using the lists, since this lock stops the other
2727 * nfsd. This should only be used for rare cases, since it
2728 * essentially single threads the nfsd.
2729 * At this time, it is only done for cases where the stable
2730 * storage file must be written prior to completion of state
2733 error = nfsrv_getclient(clientid, CLOPS_RENEW, &clp, NULL,
2734 (nfsquad_t)((u_quad_t)0), 0, nd, p);
2735 if (!error && (clp->lc_flags & LCL_NEEDSCBNULL) &&
2738 * This happens on the first open for a client
2739 * that supports callbacks.
2743 * Although nfsrv_docallback() will sleep, clp won't
2744 * go away, since they are only removed when the
2745 * nfsv4_lock() has blocked the nfsd threads. The
2746 * fields in clp can change, but having multiple
2747 * threads do this Null callback RPC should be
2750 cbret = nfsrv_docallback(clp, NFSV4PROC_CBNULL,
2751 NULL, 0, NULL, NULL, NULL, 0, p);
2753 clp->lc_flags &= ~LCL_NEEDSCBNULL;
2755 clp->lc_flags |= LCL_CALLBACKSON;
2759 * Look up the open owner. See if it needs confirmation and
2760 * check the seq#, as required.
2763 nfsrv_getowner(&clp->lc_open, new_stp, &ownerstp);
2767 printf("Nfsd: openctrl unexpected state err=%d\n",
2769 free(new_lfp, M_NFSDLOCKFILE);
2770 free(new_open, M_NFSDSTATE);
2771 free(new_deleg, M_NFSDSTATE);
2773 NFSLOCKV4ROOTMUTEX();
2774 nfsv4_unlock(&nfsv4rootfs_lock, 1);
2775 NFSUNLOCKV4ROOTMUTEX();
2777 error = NFSERR_EXPIRED;
2781 if (new_stp->ls_flags & NFSLCK_RECLAIM)
2782 nfsrv_markstable(clp);
2785 * Get the structure for the underlying file.
2790 error = nfsrv_getlockfile(new_stp->ls_flags, &new_lfp, &lfp,
2793 free(new_lfp, M_NFSDLOCKFILE);
2796 printf("Nfsd openctrl unexpected getlockfile err=%d\n",
2798 free(new_open, M_NFSDSTATE);
2799 free(new_deleg, M_NFSDSTATE);
2801 NFSLOCKV4ROOTMUTEX();
2802 nfsv4_unlock(&nfsv4rootfs_lock, 1);
2803 NFSUNLOCKV4ROOTMUTEX();
2809 * Search for a conflicting open/share.
2811 if (new_stp->ls_flags & NFSLCK_DELEGCUR) {
2813 * For Delegate_Cur, search for the matching Delegation,
2814 * which indicates no conflict.
2815 * An old delegation should have been recovered by the
2816 * client doing a Claim_DELEGATE_Prev, so I won't let
2817 * it match and return NFSERR_EXPIRED. Should I let it
2820 LIST_FOREACH(stp, &lfp->lf_deleg, ls_file) {
2821 if (!(stp->ls_flags & NFSLCK_OLDDELEG) &&
2822 (((nd->nd_flag & ND_NFSV41) != 0 &&
2823 stateidp->seqid == 0) ||
2824 stateidp->seqid == stp->ls_stateid.seqid) &&
2825 !NFSBCMP(stateidp->other, stp->ls_stateid.other,
2829 if (stp == LIST_END(&lfp->lf_deleg) ||
2830 ((new_stp->ls_flags & NFSLCK_WRITEACCESS) &&
2831 (stp->ls_flags & NFSLCK_DELEGREAD))) {
2833 printf("Nfsd openctrl unexpected expiry\n");
2834 free(new_open, M_NFSDSTATE);
2835 free(new_deleg, M_NFSDSTATE);
2837 NFSLOCKV4ROOTMUTEX();
2838 nfsv4_unlock(&nfsv4rootfs_lock, 1);
2839 NFSUNLOCKV4ROOTMUTEX();
2841 error = NFSERR_EXPIRED;
2846 * Don't issue a Delegation, since one already exists and
2847 * delay delegation timeout, as required.
2850 nfsrv_delaydelegtimeout(stp);
2854 * Check for access/deny bit conflicts. I also check for the
2855 * same owner, since the client might not have bothered to check.
2856 * Also, note an open for the same file and owner, if found,
2857 * which is all we do here for Delegate_Cur, since conflict
2858 * checking is already done.
2860 LIST_FOREACH(stp, &lfp->lf_open, ls_file) {
2861 if (ownerstp && stp->ls_openowner == ownerstp)
2863 if (!(new_stp->ls_flags & NFSLCK_DELEGCUR)) {
2865 * If another client has the file open, the only
2866 * delegation that can be issued is a Read delegation
2867 * and only if it is a Read open with Deny none.
2869 if (clp != stp->ls_clp) {
2870 if ((stp->ls_flags & NFSLCK_SHAREBITS) ==
2876 if(((new_stp->ls_flags & NFSLCK_ACCESSBITS) &
2877 ((stp->ls_flags>>NFSLCK_SHIFT) & NFSLCK_ACCESSBITS))||
2878 ((stp->ls_flags & NFSLCK_ACCESSBITS) &
2879 ((new_stp->ls_flags>>NFSLCK_SHIFT)&NFSLCK_ACCESSBITS))){
2880 ret = nfsrv_clientconflict(stp->ls_clp,&haslock,vp,p);
2883 * nfsrv_clientconflict() unlocks state
2884 * when it returns non-zero.
2886 free(new_open, M_NFSDSTATE);
2887 free(new_deleg, M_NFSDSTATE);
2892 error = NFSERR_PERM;
2893 else if (new_stp->ls_flags & NFSLCK_RECLAIM)
2894 error = NFSERR_RECLAIMCONFLICT;
2896 error = NFSERR_SHAREDENIED;
2900 NFSLOCKV4ROOTMUTEX();
2901 nfsv4_unlock(&nfsv4rootfs_lock, 1);
2902 NFSUNLOCKV4ROOTMUTEX();
2904 free(new_open, M_NFSDSTATE);
2905 free(new_deleg, M_NFSDSTATE);
2906 printf("nfsd openctrl unexpected client cnfl\n");
2913 * Check for a conflicting delegation. If one is found, call
2914 * nfsrv_delegconflict() to handle it. If the v4root lock hasn't
2915 * been set yet, it will get the lock. Otherwise, it will recall
2916 * the delegation. Then, we try try again...
2917 * (If NFSLCK_DELEGCUR is set, it has a delegation, so there
2918 * isn't a conflict.)
2919 * I currently believe the conflict algorithm to be:
2920 * For Open with Read Access and Deny None
2921 * - there is a conflict iff a different client has a write delegation
2922 * For Open with other Write Access or any Deny except None
2923 * - there is a conflict if a different client has any delegation
2924 * - there is a conflict if the same client has a read delegation
2925 * (The current consensus is that this last case should be
2926 * considered a conflict since the client with a read delegation
2927 * could have done an Open with ReadAccess and WriteDeny
2928 * locally and then not have checked for the WriteDeny.)
2930 if (!(new_stp->ls_flags & (NFSLCK_DELEGPREV | NFSLCK_DELEGCUR))) {
2931 stp = LIST_FIRST(&lfp->lf_deleg);
2932 while (stp != LIST_END(&lfp->lf_deleg)) {
2933 nstp = LIST_NEXT(stp, ls_file);
2934 if (stp->ls_clp != clp && (stp->ls_flags & NFSLCK_DELEGREAD))
2938 if ((readonly && stp->ls_clp != clp &&
2939 (stp->ls_flags & NFSLCK_DELEGWRITE)) ||
2940 (!readonly && (stp->ls_clp != clp ||
2941 (stp->ls_flags & NFSLCK_DELEGREAD)))) {
2942 if (new_stp->ls_flags & NFSLCK_RECLAIM) {
2945 ret = nfsrv_delegconflict(stp, &haslock, p, vp);
2948 * nfsrv_delegconflict() unlocks state
2949 * when it returns non-zero.
2951 printf("Nfsd openctrl unexpected deleg cnfl\n");
2952 free(new_open, M_NFSDSTATE);
2953 free(new_deleg, M_NFSDSTATE);
2968 * We only get here if there was no open that conflicted.
2969 * If an open for the owner exists, or in the access/deny bits.
2970 * Otherwise it is a new open. If the open_owner hasn't been
2971 * confirmed, replace the open with the new one needing confirmation,
2972 * otherwise add the open.
2974 if (new_stp->ls_flags & NFSLCK_DELEGPREV) {
2976 * Handle NFSLCK_DELEGPREV by searching the old delegations for
2977 * a match. If found, just move the old delegation to the current
2978 * delegation list and issue open. If not found, return
2981 LIST_FOREACH(stp, &clp->lc_olddeleg, ls_list) {
2982 if (stp->ls_lfp == lfp) {
2984 if (stp->ls_clp != clp)
2985 panic("olddeleg clp");
2986 LIST_REMOVE(stp, ls_list);
2987 LIST_REMOVE(stp, ls_hash);
2988 stp->ls_flags &= ~NFSLCK_OLDDELEG;
2989 stp->ls_stateid.seqid = delegstateidp->seqid = 1;
2990 stp->ls_stateid.other[0] = delegstateidp->other[0] =
2991 clp->lc_clientid.lval[0];
2992 stp->ls_stateid.other[1] = delegstateidp->other[1] =
2993 clp->lc_clientid.lval[1];
2994 stp->ls_stateid.other[2] = delegstateidp->other[2] =
2995 nfsrv_nextstateindex(clp);
2996 stp->ls_compref = nd->nd_compref;
2997 LIST_INSERT_HEAD(&clp->lc_deleg, stp, ls_list);
2998 LIST_INSERT_HEAD(NFSSTATEHASH(clp,
2999 stp->ls_stateid), stp, ls_hash);
3000 if (stp->ls_flags & NFSLCK_DELEGWRITE)
3001 *rflagsp |= NFSV4OPEN_WRITEDELEGATE;
3003 *rflagsp |= NFSV4OPEN_READDELEGATE;
3004 clp->lc_delegtime = NFSD_MONOSEC +
3005 nfsrv_lease + NFSRV_LEASEDELTA;
3008 * Now, do the associated open.
3010 new_open->ls_stateid.seqid = 1;
3011 new_open->ls_stateid.other[0] = clp->lc_clientid.lval[0];
3012 new_open->ls_stateid.other[1] = clp->lc_clientid.lval[1];
3013 new_open->ls_stateid.other[2] = nfsrv_nextstateindex(clp);
3014 new_open->ls_flags = (new_stp->ls_flags&NFSLCK_DENYBITS)|
3016 if (stp->ls_flags & NFSLCK_DELEGWRITE)
3017 new_open->ls_flags |= (NFSLCK_READACCESS |
3018 NFSLCK_WRITEACCESS);
3020 new_open->ls_flags |= NFSLCK_READACCESS;
3021 new_open->ls_uid = new_stp->ls_uid;
3022 new_open->ls_lfp = lfp;
3023 new_open->ls_clp = clp;
3024 LIST_INIT(&new_open->ls_open);
3025 LIST_INSERT_HEAD(&lfp->lf_open, new_open, ls_file);
3026 LIST_INSERT_HEAD(NFSSTATEHASH(clp, new_open->ls_stateid),
3029 * and handle the open owner
3032 new_open->ls_openowner = ownerstp;
3033 LIST_INSERT_HEAD(&ownerstp->ls_open,new_open,ls_list);
3035 new_open->ls_openowner = new_stp;
3036 new_stp->ls_flags = 0;
3037 nfsrvd_refcache(new_stp->ls_op);
3038 new_stp->ls_noopens = 0;
3039 LIST_INIT(&new_stp->ls_open);
3040 LIST_INSERT_HEAD(&new_stp->ls_open, new_open, ls_list);
3041 LIST_INSERT_HEAD(&clp->lc_open, new_stp, ls_list);
3043 nfsstatsv1.srvopenowners++;
3044 nfsrv_openpluslock++;
3048 nfsstatsv1.srvopens++;
3049 nfsrv_openpluslock++;
3053 if (stp == LIST_END(&clp->lc_olddeleg))
3054 error = NFSERR_EXPIRED;
3055 } else if (new_stp->ls_flags & (NFSLCK_DELEGREAD | NFSLCK_DELEGWRITE)) {
3057 * Scan to see that no delegation for this client and file
3058 * doesn't already exist.
3059 * There also shouldn't yet be an Open for this file and
3062 LIST_FOREACH(stp, &lfp->lf_deleg, ls_file) {
3063 if (stp->ls_clp == clp)
3066 if (stp == LIST_END(&lfp->lf_deleg) && openstp == NULL) {
3068 * This is the Claim_Previous case with a delegation
3069 * type != Delegate_None.
3072 * First, add the delegation. (Although we must issue the
3073 * delegation, we can also ask for an immediate return.)
3075 new_deleg->ls_stateid.seqid = delegstateidp->seqid = 1;
3076 new_deleg->ls_stateid.other[0] = delegstateidp->other[0] =
3077 clp->lc_clientid.lval[0];
3078 new_deleg->ls_stateid.other[1] = delegstateidp->other[1] =
3079 clp->lc_clientid.lval[1];
3080 new_deleg->ls_stateid.other[2] = delegstateidp->other[2] =
3081 nfsrv_nextstateindex(clp);
3082 if (new_stp->ls_flags & NFSLCK_DELEGWRITE) {
3083 new_deleg->ls_flags = (NFSLCK_DELEGWRITE |
3084 NFSLCK_READACCESS | NFSLCK_WRITEACCESS);
3085 *rflagsp |= NFSV4OPEN_WRITEDELEGATE;
3086 nfsrv_writedelegcnt++;
3088 new_deleg->ls_flags = (NFSLCK_DELEGREAD |
3090 *rflagsp |= NFSV4OPEN_READDELEGATE;
3092 new_deleg->ls_uid = new_stp->ls_uid;
3093 new_deleg->ls_lfp = lfp;
3094 new_deleg->ls_clp = clp;
3095 new_deleg->ls_filerev = filerev;
3096 new_deleg->ls_compref = nd->nd_compref;
3097 new_deleg->ls_lastrecall = 0;
3098 LIST_INSERT_HEAD(&lfp->lf_deleg, new_deleg, ls_file);
3099 LIST_INSERT_HEAD(NFSSTATEHASH(clp,
3100 new_deleg->ls_stateid), new_deleg, ls_hash);
3101 LIST_INSERT_HEAD(&clp->lc_deleg, new_deleg, ls_list);
3103 if (delegate == 2 || nfsrv_issuedelegs == 0 ||
3104 (clp->lc_flags & (LCL_CALLBACKSON | LCL_CBDOWN)) !=
3106 NFSRV_V4DELEGLIMIT(nfsrv_delegatecnt) ||
3107 !NFSVNO_DELEGOK(vp))
3108 *rflagsp |= NFSV4OPEN_RECALL;
3109 nfsstatsv1.srvdelegates++;
3110 nfsrv_openpluslock++;
3111 nfsrv_delegatecnt++;
3114 * Now, do the associated open.
3116 new_open->ls_stateid.seqid = 1;
3117 new_open->ls_stateid.other[0] = clp->lc_clientid.lval[0];
3118 new_open->ls_stateid.other[1] = clp->lc_clientid.lval[1];
3119 new_open->ls_stateid.other[2] = nfsrv_nextstateindex(clp);
3120 new_open->ls_flags = (new_stp->ls_flags & NFSLCK_DENYBITS) |
3122 if (new_stp->ls_flags & NFSLCK_DELEGWRITE)
3123 new_open->ls_flags |= (NFSLCK_READACCESS |
3124 NFSLCK_WRITEACCESS);
3126 new_open->ls_flags |= NFSLCK_READACCESS;
3127 new_open->ls_uid = new_stp->ls_uid;
3128 new_open->ls_lfp = lfp;
3129 new_open->ls_clp = clp;
3130 LIST_INIT(&new_open->ls_open);
3131 LIST_INSERT_HEAD(&lfp->lf_open, new_open, ls_file);
3132 LIST_INSERT_HEAD(NFSSTATEHASH(clp, new_open->ls_stateid),
3135 * and handle the open owner
3138 new_open->ls_openowner = ownerstp;
3139 LIST_INSERT_HEAD(&ownerstp->ls_open, new_open, ls_list);
3141 new_open->ls_openowner = new_stp;
3142 new_stp->ls_flags = 0;
3143 nfsrvd_refcache(new_stp->ls_op);
3144 new_stp->ls_noopens = 0;
3145 LIST_INIT(&new_stp->ls_open);
3146 LIST_INSERT_HEAD(&new_stp->ls_open, new_open, ls_list);
3147 LIST_INSERT_HEAD(&clp->lc_open, new_stp, ls_list);
3149 nfsstatsv1.srvopenowners++;
3150 nfsrv_openpluslock++;
3154 nfsstatsv1.srvopens++;
3155 nfsrv_openpluslock++;
3157 error = NFSERR_RECLAIMCONFLICT;
3159 } else if (ownerstp) {
3160 if (ownerstp->ls_flags & NFSLCK_NEEDSCONFIRM) {
3161 /* Replace the open */
3162 if (ownerstp->ls_op)
3163 nfsrvd_derefcache(ownerstp->ls_op);
3164 ownerstp->ls_op = new_stp->ls_op;
3165 nfsrvd_refcache(ownerstp->ls_op);
3166 ownerstp->ls_seq = new_stp->ls_seq;
3167 *rflagsp |= NFSV4OPEN_RESULTCONFIRM;
3168 stp = LIST_FIRST(&ownerstp->ls_open);
3169 stp->ls_flags = (new_stp->ls_flags & NFSLCK_SHAREBITS) |
3171 stp->ls_stateid.seqid = 1;
3172 stp->ls_uid = new_stp->ls_uid;
3173 if (lfp != stp->ls_lfp) {
3174 LIST_REMOVE(stp, ls_file);
3175 LIST_INSERT_HEAD(&lfp->lf_open, stp, ls_file);
3179 } else if (openstp) {
3180 openstp->ls_flags |= (new_stp->ls_flags & NFSLCK_SHAREBITS);
3181 openstp->ls_stateid.seqid++;
3182 if ((nd->nd_flag & ND_NFSV41) != 0 &&
3183 openstp->ls_stateid.seqid == 0)
3184 openstp->ls_stateid.seqid = 1;
3187 * This is where we can choose to issue a delegation.
3189 if ((new_stp->ls_flags & NFSLCK_WANTNODELEG) != 0)
3190 *rflagsp |= NFSV4OPEN_WDNOTWANTED;
3191 else if (nfsrv_issuedelegs == 0)
3192 *rflagsp |= NFSV4OPEN_WDSUPPFTYPE;
3193 else if (NFSRV_V4DELEGLIMIT(nfsrv_delegatecnt))
3194 *rflagsp |= NFSV4OPEN_WDRESOURCE;
3195 else if (delegate == 0 || writedeleg == 0 ||
3196 NFSVNO_EXRDONLY(exp) || (readonly != 0 &&
3197 nfsrv_writedelegifpos == 0) ||
3198 !NFSVNO_DELEGOK(vp) ||
3199 (new_stp->ls_flags & NFSLCK_WANTRDELEG) != 0 ||
3200 (clp->lc_flags & (LCL_CALLBACKSON | LCL_CBDOWN)) !=
3202 *rflagsp |= NFSV4OPEN_WDCONTENTION;
3204 new_deleg->ls_stateid.seqid = delegstateidp->seqid = 1;
3205 new_deleg->ls_stateid.other[0] = delegstateidp->other[0]
3206 = clp->lc_clientid.lval[0];
3207 new_deleg->ls_stateid.other[1] = delegstateidp->other[1]
3208 = clp->lc_clientid.lval[1];
3209 new_deleg->ls_stateid.other[2] = delegstateidp->other[2]
3210 = nfsrv_nextstateindex(clp);
3211 new_deleg->ls_flags = (NFSLCK_DELEGWRITE |
3212 NFSLCK_READACCESS | NFSLCK_WRITEACCESS);
3213 *rflagsp |= NFSV4OPEN_WRITEDELEGATE;
3214 new_deleg->ls_uid = new_stp->ls_uid;
3215 new_deleg->ls_lfp = lfp;
3216 new_deleg->ls_clp = clp;
3217 new_deleg->ls_filerev = filerev;
3218 new_deleg->ls_compref = nd->nd_compref;
3219 new_deleg->ls_lastrecall = 0;
3220 nfsrv_writedelegcnt++;
3221 LIST_INSERT_HEAD(&lfp->lf_deleg, new_deleg, ls_file);
3222 LIST_INSERT_HEAD(NFSSTATEHASH(clp,
3223 new_deleg->ls_stateid), new_deleg, ls_hash);
3224 LIST_INSERT_HEAD(&clp->lc_deleg, new_deleg, ls_list);
3226 nfsstatsv1.srvdelegates++;
3227 nfsrv_openpluslock++;
3228 nfsrv_delegatecnt++;
3231 new_open->ls_stateid.seqid = 1;
3232 new_open->ls_stateid.other[0] = clp->lc_clientid.lval[0];
3233 new_open->ls_stateid.other[1] = clp->lc_clientid.lval[1];
3234 new_open->ls_stateid.other[2] = nfsrv_nextstateindex(clp);
3235 new_open->ls_flags = (new_stp->ls_flags & NFSLCK_SHAREBITS)|
3237 new_open->ls_uid = new_stp->ls_uid;
3238 new_open->ls_openowner = ownerstp;
3239 new_open->ls_lfp = lfp;
3240 new_open->ls_clp = clp;
3241 LIST_INIT(&new_open->ls_open);
3242 LIST_INSERT_HEAD(&lfp->lf_open, new_open, ls_file);
3243 LIST_INSERT_HEAD(&ownerstp->ls_open, new_open, ls_list);
3244 LIST_INSERT_HEAD(NFSSTATEHASH(clp, new_open->ls_stateid),
3248 nfsstatsv1.srvopens++;
3249 nfsrv_openpluslock++;
3252 * This is where we can choose to issue a delegation.
3254 if ((new_stp->ls_flags & NFSLCK_WANTNODELEG) != 0)
3255 *rflagsp |= NFSV4OPEN_WDNOTWANTED;
3256 else if (nfsrv_issuedelegs == 0)
3257 *rflagsp |= NFSV4OPEN_WDSUPPFTYPE;
3258 else if (NFSRV_V4DELEGLIMIT(nfsrv_delegatecnt))
3259 *rflagsp |= NFSV4OPEN_WDRESOURCE;
3260 else if (delegate == 0 || (writedeleg == 0 &&
3261 readonly == 0) || !NFSVNO_DELEGOK(vp) ||
3262 (clp->lc_flags & (LCL_CALLBACKSON | LCL_CBDOWN)) !=
3264 *rflagsp |= NFSV4OPEN_WDCONTENTION;
3266 new_deleg->ls_stateid.seqid = delegstateidp->seqid = 1;
3267 new_deleg->ls_stateid.other[0] = delegstateidp->other[0]
3268 = clp->lc_clientid.lval[0];
3269 new_deleg->ls_stateid.other[1] = delegstateidp->other[1]
3270 = clp->lc_clientid.lval[1];
3271 new_deleg->ls_stateid.other[2] = delegstateidp->other[2]
3272 = nfsrv_nextstateindex(clp);
3273 if (writedeleg && !NFSVNO_EXRDONLY(exp) &&
3274 (nfsrv_writedelegifpos || !readonly) &&
3275 (new_stp->ls_flags & NFSLCK_WANTRDELEG) == 0) {
3276 new_deleg->ls_flags = (NFSLCK_DELEGWRITE |
3277 NFSLCK_READACCESS | NFSLCK_WRITEACCESS);
3278 *rflagsp |= NFSV4OPEN_WRITEDELEGATE;
3279 nfsrv_writedelegcnt++;
3281 new_deleg->ls_flags = (NFSLCK_DELEGREAD |
3283 *rflagsp |= NFSV4OPEN_READDELEGATE;
3285 new_deleg->ls_uid = new_stp->ls_uid;
3286 new_deleg->ls_lfp = lfp;
3287 new_deleg->ls_clp = clp;
3288 new_deleg->ls_filerev = filerev;
3289 new_deleg->ls_compref = nd->nd_compref;
3290 new_deleg->ls_lastrecall = 0;
3291 LIST_INSERT_HEAD(&lfp->lf_deleg, new_deleg, ls_file);
3292 LIST_INSERT_HEAD(NFSSTATEHASH(clp,
3293 new_deleg->ls_stateid), new_deleg, ls_hash);
3294 LIST_INSERT_HEAD(&clp->lc_deleg, new_deleg, ls_list);
3296 nfsstatsv1.srvdelegates++;
3297 nfsrv_openpluslock++;
3298 nfsrv_delegatecnt++;
3303 * New owner case. Start the open_owner sequence with a
3304 * Needs confirmation (unless a reclaim) and hang the
3307 new_open->ls_stateid.seqid = 1;
3308 new_open->ls_stateid.other[0] = clp->lc_clientid.lval[0];
3309 new_open->ls_stateid.other[1] = clp->lc_clientid.lval[1];
3310 new_open->ls_stateid.other[2] = nfsrv_nextstateindex(clp);
3311 new_open->ls_flags = (new_stp->ls_flags & NFSLCK_SHAREBITS) |
3313 new_open->ls_uid = new_stp->ls_uid;
3314 LIST_INIT(&new_open->ls_open);
3315 new_open->ls_openowner = new_stp;
3316 new_open->ls_lfp = lfp;
3317 new_open->ls_clp = clp;
3318 LIST_INSERT_HEAD(&lfp->lf_open, new_open, ls_file);
3319 if (new_stp->ls_flags & NFSLCK_RECLAIM) {
3320 new_stp->ls_flags = 0;
3321 } else if ((nd->nd_flag & ND_NFSV41) != 0) {
3322 /* NFSv4.1 never needs confirmation. */
3323 new_stp->ls_flags = 0;
3326 * This is where we can choose to issue a delegation.
3328 if (delegate && nfsrv_issuedelegs &&
3329 (writedeleg || readonly) &&
3330 (clp->lc_flags & (LCL_CALLBACKSON | LCL_CBDOWN)) ==
3332 !NFSRV_V4DELEGLIMIT(nfsrv_delegatecnt) &&
3333 NFSVNO_DELEGOK(vp) &&
3334 ((nd->nd_flag & ND_NFSV41) == 0 ||
3335 (new_stp->ls_flags & NFSLCK_WANTNODELEG) == 0)) {
3336 new_deleg->ls_stateid.seqid =
3337 delegstateidp->seqid = 1;
3338 new_deleg->ls_stateid.other[0] =
3339 delegstateidp->other[0]
3340 = clp->lc_clientid.lval[0];
3341 new_deleg->ls_stateid.other[1] =
3342 delegstateidp->other[1]
3343 = clp->lc_clientid.lval[1];
3344 new_deleg->ls_stateid.other[2] =
3345 delegstateidp->other[2]
3346 = nfsrv_nextstateindex(clp);
3347 if (writedeleg && !NFSVNO_EXRDONLY(exp) &&
3348 (nfsrv_writedelegifpos || !readonly) &&
3349 ((nd->nd_flag & ND_NFSV41) == 0 ||
3350 (new_stp->ls_flags & NFSLCK_WANTRDELEG) ==
3352 new_deleg->ls_flags =
3353 (NFSLCK_DELEGWRITE |
3355 NFSLCK_WRITEACCESS);
3356 *rflagsp |= NFSV4OPEN_WRITEDELEGATE;
3357 nfsrv_writedelegcnt++;
3359 new_deleg->ls_flags =
3362 *rflagsp |= NFSV4OPEN_READDELEGATE;
3364 new_deleg->ls_uid = new_stp->ls_uid;
3365 new_deleg->ls_lfp = lfp;
3366 new_deleg->ls_clp = clp;
3367 new_deleg->ls_filerev = filerev;
3368 new_deleg->ls_compref = nd->nd_compref;
3369 new_deleg->ls_lastrecall = 0;
3370 LIST_INSERT_HEAD(&lfp->lf_deleg, new_deleg,
3372 LIST_INSERT_HEAD(NFSSTATEHASH(clp,
3373 new_deleg->ls_stateid), new_deleg, ls_hash);
3374 LIST_INSERT_HEAD(&clp->lc_deleg, new_deleg,
3377 nfsstatsv1.srvdelegates++;
3378 nfsrv_openpluslock++;
3379 nfsrv_delegatecnt++;
3382 * Since NFSv4.1 never does an OpenConfirm, the first
3383 * open state will be acquired here.
3385 if (!(clp->lc_flags & LCL_STAMPEDSTABLE)) {
3386 clp->lc_flags |= LCL_STAMPEDSTABLE;
3387 len = clp->lc_idlen;
3388 NFSBCOPY(clp->lc_id, clidp, len);
3392 *rflagsp |= NFSV4OPEN_RESULTCONFIRM;
3393 new_stp->ls_flags = NFSLCK_NEEDSCONFIRM;
3395 nfsrvd_refcache(new_stp->ls_op);
3396 new_stp->ls_noopens = 0;
3397 LIST_INIT(&new_stp->ls_open);
3398 LIST_INSERT_HEAD(&new_stp->ls_open, new_open, ls_list);
3399 LIST_INSERT_HEAD(&clp->lc_open, new_stp, ls_list);
3400 LIST_INSERT_HEAD(NFSSTATEHASH(clp, new_open->ls_stateid),
3405 nfsstatsv1.srvopens++;
3406 nfsrv_openpluslock++;
3407 nfsstatsv1.srvopenowners++;
3408 nfsrv_openpluslock++;
3411 stateidp->seqid = openstp->ls_stateid.seqid;
3412 stateidp->other[0] = openstp->ls_stateid.other[0];
3413 stateidp->other[1] = openstp->ls_stateid.other[1];
3414 stateidp->other[2] = openstp->ls_stateid.other[2];
3418 NFSLOCKV4ROOTMUTEX();
3419 nfsv4_unlock(&nfsv4rootfs_lock, 1);
3420 NFSUNLOCKV4ROOTMUTEX();
3423 free(new_open, M_NFSDSTATE);
3425 free(new_deleg, M_NFSDSTATE);
3428 * If the NFSv4.1 client just acquired its first open, write a timestamp
3429 * to the stable storage file.
3431 if (gotstate != 0) {
3432 nfsrv_writestable(clidp, len, NFSNST_NEWSTATE, p);
3433 nfsrv_backupstable();
3437 free(clidp, M_TEMP);
3438 NFSEXITCODE2(error, nd);
3443 * Open update. Does the confirm, downgrade and close.
3446 nfsrv_openupdate(vnode_t vp, struct nfsstate *new_stp, nfsquad_t clientid,
3447 nfsv4stateid_t *stateidp, struct nfsrv_descript *nd, NFSPROC_T *p,
3448 int *retwriteaccessp)
3450 struct nfsstate *stp;
3451 struct nfsclient *clp;
3452 struct nfslockfile *lfp;
3454 int error = 0, gotstate = 0, len = 0;
3455 u_char *clidp = NULL;
3458 * Check for restart conditions (client and server).
3460 error = nfsrv_checkrestart(clientid, new_stp->ls_flags,
3461 &new_stp->ls_stateid, 0);
3465 clidp = malloc(NFSV4_OPAQUELIMIT, M_TEMP, M_WAITOK);
3468 * Get the open structure via clientid and stateid.
3470 error = nfsrv_getclient(clientid, CLOPS_RENEW, &clp, NULL,
3471 (nfsquad_t)((u_quad_t)0), 0, nd, p);
3473 error = nfsrv_getstate(clp, &new_stp->ls_stateid,
3474 new_stp->ls_flags, &stp);
3477 * Sanity check the open.
3479 if (!error && (!(stp->ls_flags & NFSLCK_OPEN) ||
3480 (!(new_stp->ls_flags & NFSLCK_CONFIRM) &&
3481 (stp->ls_openowner->ls_flags & NFSLCK_NEEDSCONFIRM)) ||
3482 ((new_stp->ls_flags & NFSLCK_CONFIRM) &&
3483 (!(stp->ls_openowner->ls_flags & NFSLCK_NEEDSCONFIRM)))))
3484 error = NFSERR_BADSTATEID;
3487 error = nfsrv_checkseqid(nd, new_stp->ls_seq,
3488 stp->ls_openowner, new_stp->ls_op);
3489 if (!error && stp->ls_stateid.seqid != new_stp->ls_stateid.seqid &&
3490 (((nd->nd_flag & ND_NFSV41) == 0 &&
3491 !(new_stp->ls_flags & NFSLCK_CONFIRM)) ||
3492 ((nd->nd_flag & ND_NFSV41) != 0 &&
3493 new_stp->ls_stateid.seqid != 0)))
3494 error = NFSERR_OLDSTATEID;
3495 if (!error && vp->v_type != VREG) {
3496 if (vp->v_type == VDIR)
3497 error = NFSERR_ISDIR;
3499 error = NFSERR_INVAL;
3504 * If a client tries to confirm an Open with a bad
3505 * seqid# and there are no byte range locks or other Opens
3506 * on the openowner, just throw it away, so the next use of the
3507 * openowner will start a fresh seq#.
3509 if (error == NFSERR_BADSEQID &&
3510 (new_stp->ls_flags & NFSLCK_CONFIRM) &&
3511 nfsrv_nootherstate(stp))
3512 nfsrv_freeopenowner(stp->ls_openowner, 0, p);
3518 * Set the return stateid.
3520 stateidp->seqid = stp->ls_stateid.seqid + 1;
3521 if ((nd->nd_flag & ND_NFSV41) != 0 && stateidp->seqid == 0)
3522 stateidp->seqid = 1;
3523 stateidp->other[0] = stp->ls_stateid.other[0];
3524 stateidp->other[1] = stp->ls_stateid.other[1];
3525 stateidp->other[2] = stp->ls_stateid.other[2];
3527 * Now, handle the three cases.
3529 if (new_stp->ls_flags & NFSLCK_CONFIRM) {
3531 * If the open doesn't need confirmation, it seems to me that
3532 * there is a client error, but I'll just log it and keep going?
3534 if (!(stp->ls_openowner->ls_flags & NFSLCK_NEEDSCONFIRM))
3535 printf("Nfsv4d: stray open confirm\n");
3536 stp->ls_openowner->ls_flags = 0;
3537 stp->ls_stateid.seqid++;
3538 if ((nd->nd_flag & ND_NFSV41) != 0 &&
3539 stp->ls_stateid.seqid == 0)
3540 stp->ls_stateid.seqid = 1;
3541 if (!(clp->lc_flags & LCL_STAMPEDSTABLE)) {
3542 clp->lc_flags |= LCL_STAMPEDSTABLE;
3543 len = clp->lc_idlen;
3544 NFSBCOPY(clp->lc_id, clidp, len);
3548 } else if (new_stp->ls_flags & NFSLCK_CLOSE) {
3550 if (retwriteaccessp != NULL) {
3551 if ((stp->ls_flags & NFSLCK_WRITEACCESS) != 0)
3552 *retwriteaccessp = 1;
3554 *retwriteaccessp = 0;
3556 if (nfsrv_dolocallocks != 0 && !LIST_EMPTY(&stp->ls_open)) {
3557 /* Get the lf lock */
3560 ASSERT_VOP_ELOCKED(vp, "nfsrv_openupdate");
3562 if (nfsrv_freeopen(stp, vp, 1, p) == 0) {
3564 nfsrv_unlocklf(lfp);
3567 NFSVOPLOCK(vp, LK_EXCLUSIVE | LK_RETRY);
3569 (void) nfsrv_freeopen(stp, NULL, 0, p);
3574 * Update the share bits, making sure that the new set are a
3575 * subset of the old ones.
3577 bits = (new_stp->ls_flags & NFSLCK_SHAREBITS);
3578 if (~(stp->ls_flags) & bits) {
3580 error = NFSERR_INVAL;
3583 stp->ls_flags = (bits | NFSLCK_OPEN);
3584 stp->ls_stateid.seqid++;
3585 if ((nd->nd_flag & ND_NFSV41) != 0 &&
3586 stp->ls_stateid.seqid == 0)
3587 stp->ls_stateid.seqid = 1;
3592 * If the client just confirmed its first open, write a timestamp
3593 * to the stable storage file.
3595 if (gotstate != 0) {
3596 nfsrv_writestable(clidp, len, NFSNST_NEWSTATE, p);
3597 nfsrv_backupstable();
3601 free(clidp, M_TEMP);
3602 NFSEXITCODE2(error, nd);
3607 * Delegation update. Does the purge and return.
3610 nfsrv_delegupdate(struct nfsrv_descript *nd, nfsquad_t clientid,
3611 nfsv4stateid_t *stateidp, vnode_t vp, int op, struct ucred *cred,
3612 NFSPROC_T *p, int *retwriteaccessp)
3614 struct nfsstate *stp;
3615 struct nfsclient *clp;
3620 * Do a sanity check against the file handle for DelegReturn.
3623 error = nfsvno_getfh(vp, &fh, p);
3628 * Check for restart conditions (client and server).
3630 if (op == NFSV4OP_DELEGRETURN)
3631 error = nfsrv_checkrestart(clientid, NFSLCK_DELEGRETURN,
3634 error = nfsrv_checkrestart(clientid, NFSLCK_DELEGPURGE,
3639 * Get the open structure via clientid and stateid.
3642 error = nfsrv_getclient(clientid, CLOPS_RENEW, &clp, NULL,
3643 (nfsquad_t)((u_quad_t)0), 0, nd, p);
3645 if (error == NFSERR_CBPATHDOWN)
3647 if (error == NFSERR_STALECLIENTID && op == NFSV4OP_DELEGRETURN)
3648 error = NFSERR_STALESTATEID;
3650 if (!error && op == NFSV4OP_DELEGRETURN) {
3651 error = nfsrv_getstate(clp, stateidp, NFSLCK_DELEGRETURN, &stp);
3652 if (!error && stp->ls_stateid.seqid != stateidp->seqid &&
3653 ((nd->nd_flag & ND_NFSV41) == 0 || stateidp->seqid != 0))
3654 error = NFSERR_OLDSTATEID;
3657 * NFSERR_EXPIRED means that the state has gone away,
3658 * so Delegations have been purged. Just return ok.
3660 if (error == NFSERR_EXPIRED && op == NFSV4OP_DELEGPURGE) {
3670 if (op == NFSV4OP_DELEGRETURN) {
3671 if (NFSBCMP((caddr_t)&fh, (caddr_t)&stp->ls_lfp->lf_fh,
3672 sizeof (fhandle_t))) {
3674 error = NFSERR_BADSTATEID;
3677 if (retwriteaccessp != NULL) {
3678 if ((stp->ls_flags & NFSLCK_DELEGWRITE) != 0)
3679 *retwriteaccessp = 1;
3681 *retwriteaccessp = 0;
3683 nfsrv_freedeleg(stp);
3685 nfsrv_freedeleglist(&clp->lc_olddeleg);
3696 * Release lock owner.
3699 nfsrv_releaselckown(struct nfsstate *new_stp, nfsquad_t clientid,
3702 struct nfsstate *stp, *nstp, *openstp, *ownstp;
3703 struct nfsclient *clp;
3707 * Check for restart conditions (client and server).
3709 error = nfsrv_checkrestart(clientid, new_stp->ls_flags,
3710 &new_stp->ls_stateid, 0);
3716 * Get the lock owner by name.
3718 error = nfsrv_getclient(clientid, CLOPS_RENEW, &clp, NULL,
3719 (nfsquad_t)((u_quad_t)0), 0, NULL, p);
3724 LIST_FOREACH(ownstp, &clp->lc_open, ls_list) {
3725 LIST_FOREACH(openstp, &ownstp->ls_open, ls_list) {
3726 stp = LIST_FIRST(&openstp->ls_open);
3727 while (stp != LIST_END(&openstp->ls_open)) {
3728 nstp = LIST_NEXT(stp, ls_list);
3730 * If the owner matches, check for locks and
3731 * then free or return an error.
3733 if (stp->ls_ownerlen == new_stp->ls_ownerlen &&
3734 !NFSBCMP(stp->ls_owner, new_stp->ls_owner,
3736 if (LIST_EMPTY(&stp->ls_lock)) {
3737 nfsrv_freelockowner(stp, NULL, 0, p);
3740 error = NFSERR_LOCKSHELD;
3756 * Get the file handle for a lock structure.
3759 nfsrv_getlockfh(vnode_t vp, u_short flags, struct nfslockfile *new_lfp,
3760 fhandle_t *nfhp, NFSPROC_T *p)
3762 fhandle_t *fhp = NULL;
3766 * For lock, use the new nfslock structure, otherwise just
3767 * a fhandle_t on the stack.
3769 if (flags & NFSLCK_OPEN) {
3770 KASSERT(new_lfp != NULL, ("nfsrv_getlockfh: new_lfp NULL"));
3771 fhp = &new_lfp->lf_fh;
3775 panic("nfsrv_getlockfh");
3777 error = nfsvno_getfh(vp, fhp, p);
3783 * Get an nfs lock structure. Allocate one, as required, and return a
3785 * Returns an NFSERR_xxx upon failure or -1 to indicate no current lock.
3788 nfsrv_getlockfile(u_short flags, struct nfslockfile **new_lfpp,
3789 struct nfslockfile **lfpp, fhandle_t *nfhp, int lockit)
3791 struct nfslockfile *lfp;
3792 fhandle_t *fhp = NULL, *tfhp;
3793 struct nfslockhashhead *hp;
3794 struct nfslockfile *new_lfp = NULL;
3797 * For lock, use the new nfslock structure, otherwise just
3798 * a fhandle_t on the stack.
3800 if (flags & NFSLCK_OPEN) {
3801 new_lfp = *new_lfpp;
3802 fhp = &new_lfp->lf_fh;
3806 panic("nfsrv_getlockfile");
3809 hp = NFSLOCKHASH(fhp);
3810 LIST_FOREACH(lfp, hp, lf_hash) {
3812 if (NFSVNO_CMPFH(fhp, tfhp)) {
3819 if (!(flags & NFSLCK_OPEN))
3823 * No match, so chain the new one into the list.
3825 LIST_INIT(&new_lfp->lf_open);
3826 LIST_INIT(&new_lfp->lf_lock);
3827 LIST_INIT(&new_lfp->lf_deleg);
3828 LIST_INIT(&new_lfp->lf_locallock);
3829 LIST_INIT(&new_lfp->lf_rollback);
3830 new_lfp->lf_locallock_lck.nfslock_usecnt = 0;
3831 new_lfp->lf_locallock_lck.nfslock_lock = 0;
3832 new_lfp->lf_usecount = 0;
3833 LIST_INSERT_HEAD(hp, new_lfp, lf_hash);
3840 * This function adds a nfslock lock structure to the list for the associated
3841 * nfsstate and nfslockfile structures. It will be inserted after the
3842 * entry pointed at by insert_lop.
3845 nfsrv_insertlock(struct nfslock *new_lop, struct nfslock *insert_lop,
3846 struct nfsstate *stp, struct nfslockfile *lfp)
3848 struct nfslock *lop, *nlop;
3850 new_lop->lo_stp = stp;
3851 new_lop->lo_lfp = lfp;
3854 /* Insert in increasing lo_first order */
3855 lop = LIST_FIRST(&lfp->lf_lock);
3856 if (lop == LIST_END(&lfp->lf_lock) ||
3857 new_lop->lo_first <= lop->lo_first) {
3858 LIST_INSERT_HEAD(&lfp->lf_lock, new_lop, lo_lckfile);
3860 nlop = LIST_NEXT(lop, lo_lckfile);
3861 while (nlop != LIST_END(&lfp->lf_lock) &&
3862 nlop->lo_first < new_lop->lo_first) {
3864 nlop = LIST_NEXT(lop, lo_lckfile);
3866 LIST_INSERT_AFTER(lop, new_lop, lo_lckfile);
3869 new_lop->lo_lckfile.le_prev = NULL; /* list not used */
3873 * Insert after insert_lop, which is overloaded as stp or lfp for
3876 if (stp == NULL && (struct nfslockfile *)insert_lop == lfp)
3877 LIST_INSERT_HEAD(&lfp->lf_locallock, new_lop, lo_lckowner);
3878 else if ((struct nfsstate *)insert_lop == stp)
3879 LIST_INSERT_HEAD(&stp->ls_lock, new_lop, lo_lckowner);
3881 LIST_INSERT_AFTER(insert_lop, new_lop, lo_lckowner);
3883 nfsstatsv1.srvlocks++;
3884 nfsrv_openpluslock++;
3889 * This function updates the locking for a lock owner and given file. It
3890 * maintains a list of lock ranges ordered on increasing file offset that
3891 * are NFSLCK_READ or NFSLCK_WRITE and non-overlapping (aka POSIX style).
3892 * It always adds new_lop to the list and sometimes uses the one pointed
3896 nfsrv_updatelock(struct nfsstate *stp, struct nfslock **new_lopp,
3897 struct nfslock **other_lopp, struct nfslockfile *lfp)
3899 struct nfslock *new_lop = *new_lopp;
3900 struct nfslock *lop, *tlop, *ilop;
3901 struct nfslock *other_lop = *other_lopp;
3902 int unlock = 0, myfile = 0;
3906 * Work down the list until the lock is merged.
3908 if (new_lop->lo_flags & NFSLCK_UNLOCK)
3911 ilop = (struct nfslock *)stp;
3912 lop = LIST_FIRST(&stp->ls_lock);
3914 ilop = (struct nfslock *)lfp;
3915 lop = LIST_FIRST(&lfp->lf_locallock);
3917 while (lop != NULL) {
3919 * Only check locks for this file that aren't before the start of
3922 if (lop->lo_lfp == lfp) {
3924 if (lop->lo_end >= new_lop->lo_first) {
3925 if (new_lop->lo_end < lop->lo_first) {
3927 * If the new lock ends before the start of the
3928 * current lock's range, no merge, just insert
3933 if (new_lop->lo_flags == lop->lo_flags ||
3934 (new_lop->lo_first <= lop->lo_first &&
3935 new_lop->lo_end >= lop->lo_end)) {
3937 * This lock can be absorbed by the new lock/unlock.
3938 * This happens when it covers the entire range
3939 * of the old lock or is contiguous
3940 * with the old lock and is of the same type or an
3943 if (lop->lo_first < new_lop->lo_first)
3944 new_lop->lo_first = lop->lo_first;
3945 if (lop->lo_end > new_lop->lo_end)
3946 new_lop->lo_end = lop->lo_end;
3948 lop = LIST_NEXT(lop, lo_lckowner);
3949 nfsrv_freenfslock(tlop);
3954 * All these cases are for contiguous locks that are not the
3955 * same type, so they can't be merged.
3957 if (new_lop->lo_first <= lop->lo_first) {
3959 * This case is where the new lock overlaps with the
3960 * first part of the old lock. Move the start of the
3961 * old lock to just past the end of the new lock. The
3962 * new lock will be inserted in front of the old, since
3963 * ilop hasn't been updated. (We are done now.)
3965 lop->lo_first = new_lop->lo_end;
3968 if (new_lop->lo_end >= lop->lo_end) {
3970 * This case is where the new lock overlaps with the
3971 * end of the old lock's range. Move the old lock's
3972 * end to just before the new lock's first and insert
3973 * the new lock after the old lock.
3974 * Might not be done yet, since the new lock could
3975 * overlap further locks with higher ranges.
3977 lop->lo_end = new_lop->lo_first;
3979 lop = LIST_NEXT(lop, lo_lckowner);
3983 * The final case is where the new lock's range is in the
3984 * middle of the current lock's and splits the current lock
3985 * up. Use *other_lopp to handle the second part of the
3986 * split old lock range. (We are done now.)
3987 * For unlock, we use new_lop as other_lop and tmp, since
3988 * other_lop and new_lop are the same for this case.
3989 * We noted the unlock case above, so we don't need
3990 * new_lop->lo_flags any longer.
3992 tmp = new_lop->lo_first;
3993 if (other_lop == NULL) {
3995 panic("nfsd srv update unlock");
3996 other_lop = new_lop;
3999 other_lop->lo_first = new_lop->lo_end;
4000 other_lop->lo_end = lop->lo_end;
4001 other_lop->lo_flags = lop->lo_flags;
4002 other_lop->lo_stp = stp;
4003 other_lop->lo_lfp = lfp;
4005 nfsrv_insertlock(other_lop, lop, stp, lfp);
4012 lop = LIST_NEXT(lop, lo_lckowner);
4013 if (myfile && (lop == NULL || lop->lo_lfp != lfp))
4018 * Insert the new lock in the list at the appropriate place.
4021 nfsrv_insertlock(new_lop, ilop, stp, lfp);
4027 * This function handles sequencing of locks, etc.
4028 * It returns an error that indicates what the caller should do.
4031 nfsrv_checkseqid(struct nfsrv_descript *nd, u_int32_t seqid,
4032 struct nfsstate *stp, struct nfsrvcache *op)
4036 if ((nd->nd_flag & ND_NFSV41) != 0)
4037 /* NFSv4.1 ignores the open_seqid and lock_seqid. */
4039 if (op != nd->nd_rp)
4040 panic("nfsrvstate checkseqid");
4041 if (!(op->rc_flag & RC_INPROG))
4042 panic("nfsrvstate not inprog");
4043 if (stp->ls_op && stp->ls_op->rc_refcnt <= 0) {
4044 printf("refcnt=%d\n", stp->ls_op->rc_refcnt);
4045 panic("nfsrvstate op refcnt");
4048 /* If ND_ERELOOKUP is set, the seqid has already been handled. */
4049 if ((nd->nd_flag & ND_ERELOOKUP) != 0)
4052 if ((stp->ls_seq + 1) == seqid) {
4054 nfsrvd_derefcache(stp->ls_op);
4056 nfsrvd_refcache(op);
4057 stp->ls_seq = seqid;
4059 } else if (stp->ls_seq == seqid && stp->ls_op &&
4060 op->rc_xid == stp->ls_op->rc_xid &&
4061 op->rc_refcnt == 0 &&
4062 op->rc_reqlen == stp->ls_op->rc_reqlen &&
4063 op->rc_cksum == stp->ls_op->rc_cksum) {
4064 if (stp->ls_op->rc_flag & RC_INPROG) {
4065 error = NFSERR_DONTREPLY;
4068 nd->nd_rp = stp->ls_op;
4069 nd->nd_rp->rc_flag |= RC_INPROG;
4070 nfsrvd_delcache(op);
4071 error = NFSERR_REPLYFROMCACHE;
4074 error = NFSERR_BADSEQID;
4077 NFSEXITCODE2(error, nd);
4082 * Get the client ip address for callbacks. If the strings can't be parsed,
4083 * just set lc_program to 0 to indicate no callbacks are possible.
4084 * (For cases where the address can't be parsed or is 0.0.0.0.0.0, set
4085 * the address to the client's transport address. This won't be used
4086 * for callbacks, but can be printed out by nfsstats for info.)
4087 * Return error if the xdr can't be parsed, 0 otherwise.
4090 nfsrv_getclientipaddr(struct nfsrv_descript *nd, struct nfsclient *clp)
4094 int i, j, maxalen = 0, minalen = 0;
4097 struct sockaddr_in *rin = NULL, *sin;
4100 struct sockaddr_in6 *rin6 = NULL, *sin6;
4103 int error = 0, cantparse = 0;
4113 /* 8 is the maximum length of the port# string. */
4114 addr = malloc(INET6_ADDRSTRLEN + 8, M_TEMP, M_WAITOK);
4115 clp->lc_req.nr_client = NULL;
4116 clp->lc_req.nr_lock = 0;
4118 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
4119 i = fxdr_unsigned(int, *tl);
4120 if (i >= 3 && i <= 4) {
4121 error = nfsrv_mtostr(nd, addr, i);
4125 if (!strcmp(addr, "tcp")) {
4126 clp->lc_flags |= LCL_TCPCALLBACK;
4127 clp->lc_req.nr_sotype = SOCK_STREAM;
4128 clp->lc_req.nr_soproto = IPPROTO_TCP;
4130 } else if (!strcmp(addr, "udp")) {
4131 clp->lc_req.nr_sotype = SOCK_DGRAM;
4132 clp->lc_req.nr_soproto = IPPROTO_UDP;
4137 if (af == AF_UNSPEC) {
4138 if (!strcmp(addr, "tcp6")) {
4139 clp->lc_flags |= LCL_TCPCALLBACK;
4140 clp->lc_req.nr_sotype = SOCK_STREAM;
4141 clp->lc_req.nr_soproto = IPPROTO_TCP;
4143 } else if (!strcmp(addr, "udp6")) {
4144 clp->lc_req.nr_sotype = SOCK_DGRAM;
4145 clp->lc_req.nr_soproto = IPPROTO_UDP;
4150 if (af == AF_UNSPEC) {
4156 error = nfsm_advance(nd, NFSM_RNDUP(i), -1);
4162 * The caller has allocated clp->lc_req.nr_nam to be large enough
4163 * for either AF_INET or AF_INET6 and zeroed out the contents.
4164 * maxalen is set to the maximum length of the host IP address string
4165 * plus 8 for the maximum length of the port#.
4166 * minalen is set to the minimum length of the host IP address string
4167 * plus 4 for the minimum length of the port#.
4168 * These lengths do not include NULL termination,
4169 * so INET[6]_ADDRSTRLEN - 1 is used in the calculations.
4174 rin = (struct sockaddr_in *)clp->lc_req.nr_nam;
4175 rin->sin_family = AF_INET;
4176 rin->sin_len = sizeof(struct sockaddr_in);
4177 maxalen = INET_ADDRSTRLEN - 1 + 8;
4183 rin6 = (struct sockaddr_in6 *)clp->lc_req.nr_nam;
4184 rin6->sin6_family = AF_INET6;
4185 rin6->sin6_len = sizeof(struct sockaddr_in6);
4186 maxalen = INET6_ADDRSTRLEN - 1 + 8;
4191 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
4192 i = fxdr_unsigned(int, *tl);
4194 error = NFSERR_BADXDR;
4196 } else if (i == 0) {
4198 } else if (!cantparse && i <= maxalen && i >= minalen) {
4199 error = nfsrv_mtostr(nd, addr, i);
4204 * Parse out the address fields. We expect 6 decimal numbers
4205 * separated by '.'s for AF_INET and two decimal numbers
4206 * preceeded by '.'s for AF_INET6.
4212 * For AF_INET6, first parse the host address.
4215 cp = strchr(addr, '.');
4218 if (inet_pton(af, addr, &rin6->sin6_addr) == 1)
4234 while (cp != NULL && *cp && i < 6) {
4236 while (*cp2 && *cp2 != '.')
4244 j = nfsrv_getipnumber(cp);
4249 port.cval[5 - i] = j;
4259 * The host address INADDR_ANY is (mis)used to indicate
4260 * "there is no valid callback address".
4265 if (!IN6_ARE_ADDR_EQUAL(&rin6->sin6_addr,
4267 rin6->sin6_port = htons(port.sval);
4274 if (ip.ival != INADDR_ANY) {
4275 rin->sin_addr.s_addr = htonl(ip.ival);
4276 rin->sin_port = htons(port.sval);
4287 error = nfsm_advance(nd, NFSM_RNDUP(i), -1);
4293 switch (nd->nd_nam->sa_family) {
4296 sin = (struct sockaddr_in *)nd->nd_nam;
4297 rin = (struct sockaddr_in *)clp->lc_req.nr_nam;
4298 rin->sin_family = AF_INET;
4299 rin->sin_len = sizeof(struct sockaddr_in);
4300 rin->sin_addr.s_addr = sin->sin_addr.s_addr;
4301 rin->sin_port = 0x0;
4306 sin6 = (struct sockaddr_in6 *)nd->nd_nam;
4307 rin6 = (struct sockaddr_in6 *)clp->lc_req.nr_nam;
4308 rin6->sin6_family = AF_INET6;
4309 rin6->sin6_len = sizeof(struct sockaddr_in6);
4310 rin6->sin6_addr = sin6->sin6_addr;
4311 rin6->sin6_port = 0x0;
4315 clp->lc_program = 0;
4319 NFSEXITCODE2(error, nd);
4324 * Turn a string of up to three decimal digits into a number. Return -1 upon
4328 nfsrv_getipnumber(u_char *cp)
4333 if (j > 2 || *cp < '0' || *cp > '9')
4346 * This function checks for restart conditions.
4349 nfsrv_checkrestart(nfsquad_t clientid, u_int32_t flags,
4350 nfsv4stateid_t *stateidp, int specialid)
4355 * First check for a server restart. Open, LockT, ReleaseLockOwner
4356 * and DelegPurge have a clientid, the rest a stateid.
4359 (NFSLCK_OPEN | NFSLCK_TEST | NFSLCK_RELEASE | NFSLCK_DELEGPURGE)) {
4360 if (clientid.lval[0] != nfsrvboottime) {
4361 ret = NFSERR_STALECLIENTID;
4364 } else if (stateidp->other[0] != nfsrvboottime &&
4366 ret = NFSERR_STALESTATEID;
4371 * Read, Write, Setattr and LockT can return NFSERR_GRACE and do
4372 * not use a lock/open owner seqid#, so the check can be done now.
4373 * (The others will be checked, as required, later.)
4375 if (!(flags & (NFSLCK_CHECK | NFSLCK_TEST)))
4379 ret = nfsrv_checkgrace(NULL, NULL, flags);
4391 nfsrv_checkgrace(struct nfsrv_descript *nd, struct nfsclient *clp,
4394 int error = 0, notreclaimed;
4395 struct nfsrv_stable *sp;
4397 if ((nfsrv_stablefirst.nsf_flags & (NFSNSF_UPDATEDONE |
4398 NFSNSF_GRACEOVER)) == 0) {
4400 * First, check to see if all of the clients have done a
4401 * ReclaimComplete. If so, grace can end now.
4404 LIST_FOREACH(sp, &nfsrv_stablefirst.nsf_head, nst_list) {
4405 if ((sp->nst_flag & NFSNST_RECLAIMED) == 0) {
4410 if (notreclaimed == 0)
4411 nfsrv_stablefirst.nsf_flags |= (NFSNSF_GRACEOVER |
4415 if ((nfsrv_stablefirst.nsf_flags & NFSNSF_GRACEOVER) != 0) {
4416 if (flags & NFSLCK_RECLAIM) {
4417 error = NFSERR_NOGRACE;
4421 if (!(flags & NFSLCK_RECLAIM)) {
4422 error = NFSERR_GRACE;
4425 if (nd != NULL && clp != NULL &&
4426 (nd->nd_flag & ND_NFSV41) != 0 &&
4427 (clp->lc_flags & LCL_RECLAIMCOMPLETE) != 0) {
4428 error = NFSERR_NOGRACE;
4433 * If grace is almost over and we are still getting Reclaims,
4434 * extend grace a bit.
4436 if ((NFSD_MONOSEC + NFSRV_LEASEDELTA) >
4437 nfsrv_stablefirst.nsf_eograce)
4438 nfsrv_stablefirst.nsf_eograce = NFSD_MONOSEC +
4448 * Do a server callback.
4449 * The "trunc" argument is slightly overloaded and refers to different
4450 * boolean arguments for CBRECALL and CBLAYOUTRECALL.
4453 nfsrv_docallback(struct nfsclient *clp, int procnum, nfsv4stateid_t *stateidp,
4454 int trunc, fhandle_t *fhp, struct nfsvattr *nap, nfsattrbit_t *attrbitp,
4455 int laytype, NFSPROC_T *p)
4459 struct nfsrv_descript *nd;
4461 int error = 0, slotpos;
4463 struct nfsdsession *sep = NULL;
4467 nd = malloc(sizeof(*nd), M_TEMP, M_WAITOK | M_ZERO);
4468 cred = newnfs_getcred();
4469 NFSLOCKSTATE(); /* mostly for lc_cbref++ */
4470 if (clp->lc_flags & LCL_NEEDSCONFIRM) {
4477 * Fill the callback program# and version into the request
4478 * structure for newnfs_connect() to use.
4480 clp->lc_req.nr_prog = clp->lc_program;
4482 if ((clp->lc_flags & LCL_NFSV41) != 0)
4483 clp->lc_req.nr_vers = NFSV41_CBVERS;
4486 clp->lc_req.nr_vers = NFSV4_CBVERS;
4489 * First, fill in some of the fields of nd and cr.
4491 nd->nd_flag = ND_NFSV4;
4492 if (clp->lc_flags & LCL_GSS)
4493 nd->nd_flag |= ND_KERBV;
4494 if ((clp->lc_flags & LCL_NFSV41) != 0)
4495 nd->nd_flag |= ND_NFSV41;
4496 if ((clp->lc_flags & LCL_NFSV42) != 0)
4497 nd->nd_flag |= ND_NFSV42;
4499 cred->cr_uid = clp->lc_uid;
4500 cred->cr_gid = clp->lc_gid;
4501 callback = clp->lc_callback;
4503 cred->cr_ngroups = 1;
4506 * Get the first mbuf for the request.
4508 MGET(m, M_WAITOK, MT_DATA);
4510 nd->nd_mreq = nd->nd_mb = m;
4511 nd->nd_bpos = mtod(m, caddr_t);
4514 * and build the callback request.
4516 if (procnum == NFSV4OP_CBGETATTR) {
4517 nd->nd_procnum = NFSV4PROC_CBCOMPOUND;
4518 error = nfsrv_cbcallargs(nd, clp, callback, NFSV4OP_CBGETATTR,
4519 "CB Getattr", &sep, &slotpos);
4521 m_freem(nd->nd_mreq);
4524 (void)nfsm_fhtom(nd, (u_int8_t *)fhp, NFSX_MYFH, 0);
4525 (void)nfsrv_putattrbit(nd, attrbitp);
4526 } else if (procnum == NFSV4OP_CBRECALL) {
4527 nd->nd_procnum = NFSV4PROC_CBCOMPOUND;
4528 error = nfsrv_cbcallargs(nd, clp, callback, NFSV4OP_CBRECALL,
4529 "CB Recall", &sep, &slotpos);
4531 m_freem(nd->nd_mreq);
4534 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED + NFSX_STATEID);
4535 *tl++ = txdr_unsigned(stateidp->seqid);
4536 NFSBCOPY((caddr_t)stateidp->other, (caddr_t)tl,
4538 tl += (NFSX_STATEIDOTHER / NFSX_UNSIGNED);
4543 (void)nfsm_fhtom(nd, (u_int8_t *)fhp, NFSX_MYFH, 0);
4544 } else if (procnum == NFSV4OP_CBLAYOUTRECALL) {
4545 NFSD_DEBUG(4, "docallback layout recall\n");
4546 nd->nd_procnum = NFSV4PROC_CBCOMPOUND;
4547 error = nfsrv_cbcallargs(nd, clp, callback,
4548 NFSV4OP_CBLAYOUTRECALL, "CB Reclayout", &sep, &slotpos);
4549 NFSD_DEBUG(4, "aft cbcallargs=%d\n", error);
4551 m_freem(nd->nd_mreq);
4554 NFSM_BUILD(tl, u_int32_t *, 4 * NFSX_UNSIGNED);
4555 *tl++ = txdr_unsigned(laytype);
4556 *tl++ = txdr_unsigned(NFSLAYOUTIOMODE_ANY);
4558 *tl++ = newnfs_true;
4560 *tl++ = newnfs_false;
4561 *tl = txdr_unsigned(NFSV4LAYOUTRET_FILE);
4562 nfsm_fhtom(nd, (uint8_t *)fhp, NFSX_MYFH, 0);
4563 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_HYPER + NFSX_STATEID);
4565 txdr_hyper(tval, tl); tl += 2;
4567 txdr_hyper(tval, tl); tl += 2;
4568 *tl++ = txdr_unsigned(stateidp->seqid);
4569 NFSBCOPY(stateidp->other, tl, NFSX_STATEIDOTHER);
4570 tl += (NFSX_STATEIDOTHER / NFSX_UNSIGNED);
4571 NFSD_DEBUG(4, "aft args\n");
4572 } else if (procnum == NFSV4PROC_CBNULL) {
4573 nd->nd_procnum = NFSV4PROC_CBNULL;
4574 if ((clp->lc_flags & LCL_NFSV41) != 0) {
4575 error = nfsv4_getcbsession(clp, &sep);
4577 m_freem(nd->nd_mreq);
4582 error = NFSERR_SERVERFAULT;
4583 m_freem(nd->nd_mreq);
4588 * Call newnfs_connect(), as required, and then newnfs_request().
4591 if ((clp->lc_flags & LCL_TLSCB) != 0)
4593 (void) newnfs_sndlock(&clp->lc_req.nr_lock);
4594 if (clp->lc_req.nr_client == NULL) {
4595 if ((clp->lc_flags & LCL_NFSV41) != 0) {
4596 error = ECONNREFUSED;
4597 if (procnum != NFSV4PROC_CBNULL)
4598 nfsv4_freeslot(&sep->sess_cbsess, slotpos,
4600 nfsrv_freesession(sep, NULL);
4601 } else if (nd->nd_procnum == NFSV4PROC_CBNULL)
4602 error = newnfs_connect(NULL, &clp->lc_req, cred,
4603 NULL, 1, dotls, &clp->lc_req.nr_client);
4605 error = newnfs_connect(NULL, &clp->lc_req, cred,
4606 NULL, 3, dotls, &clp->lc_req.nr_client);
4608 newnfs_sndunlock(&clp->lc_req.nr_lock);
4609 NFSD_DEBUG(4, "aft sndunlock=%d\n", error);
4611 if ((nd->nd_flag & ND_NFSV41) != 0) {
4612 KASSERT(sep != NULL, ("sep NULL"));
4613 if (sep->sess_cbsess.nfsess_xprt != NULL)
4614 error = newnfs_request(nd, NULL, clp,
4615 &clp->lc_req, NULL, NULL, cred,
4616 clp->lc_program, clp->lc_req.nr_vers, NULL,
4617 1, NULL, &sep->sess_cbsess);
4620 * This should probably never occur, but if a
4621 * client somehow does an RPC without a
4622 * SequenceID Op that causes a callback just
4623 * after the nfsd threads have been terminated
4624 * and restared we could conceivably get here
4625 * without a backchannel xprt.
4627 printf("nfsrv_docallback: no xprt\n");
4628 error = ECONNREFUSED;
4630 NFSD_DEBUG(4, "aft newnfs_request=%d\n", error);
4631 if (error != 0 && procnum != NFSV4PROC_CBNULL) {
4633 * It is likely that the callback was never
4634 * processed by the client and, as such,
4635 * the sequence# for the session slot needs
4636 * to be backed up by one to avoid a
4637 * NFSERR_SEQMISORDERED error reply.
4638 * For the unlikely case where the callback
4639 * was processed by the client, this will
4640 * make the next callback on the slot
4641 * appear to be a retry.
4642 * Since callbacks never specify that the
4643 * reply be cached, this "apparent retry"
4644 * should not be a problem.
4646 nfsv4_freeslot(&sep->sess_cbsess, slotpos,
4649 nfsrv_freesession(sep, NULL);
4651 error = newnfs_request(nd, NULL, clp, &clp->lc_req,
4652 NULL, NULL, cred, clp->lc_program,
4653 clp->lc_req.nr_vers, NULL, 1, NULL, NULL);
4659 * If error is set here, the Callback path isn't working
4660 * properly, so twiddle the appropriate LCL_ flags.
4661 * (nd_repstat != 0 indicates the Callback path is working,
4662 * but the callback failed on the client.)
4666 * Mark the callback pathway down, which disabled issuing
4667 * of delegations and gets Renew to return NFSERR_CBPATHDOWN.
4670 clp->lc_flags |= LCL_CBDOWN;
4674 * Callback worked. If the callback path was down, disable
4675 * callbacks, so no more delegations will be issued. (This
4676 * is done on the assumption that the callback pathway is
4680 if (clp->lc_flags & LCL_CBDOWN)
4681 clp->lc_flags &= ~(LCL_CBDOWN | LCL_CALLBACKSON);
4683 if (nd->nd_repstat) {
4684 error = nd->nd_repstat;
4685 NFSD_DEBUG(1, "nfsrv_docallback op=%d err=%d\n",
4687 } else if (error == 0 && procnum == NFSV4OP_CBGETATTR)
4688 error = nfsv4_loadattr(nd, NULL, nap, NULL, NULL, 0,
4689 NULL, NULL, NULL, NULL, NULL, 0, NULL, NULL, NULL,
4691 m_freem(nd->nd_mrep);
4695 if ((clp->lc_flags & LCL_WAKEUPWANTED) && clp->lc_cbref == 0) {
4696 clp->lc_flags &= ~LCL_WAKEUPWANTED;
4707 * Set up the compound RPC for the callback.
4710 nfsrv_cbcallargs(struct nfsrv_descript *nd, struct nfsclient *clp,
4711 uint32_t callback, int op, const char *optag, struct nfsdsession **sepp,
4717 len = strlen(optag);
4718 (void)nfsm_strtom(nd, optag, len);
4719 NFSM_BUILD(tl, uint32_t *, 4 * NFSX_UNSIGNED);
4720 if ((nd->nd_flag & ND_NFSV41) != 0) {
4721 if ((nd->nd_flag & ND_NFSV42) != 0)
4722 *tl++ = txdr_unsigned(NFSV42_MINORVERSION);
4724 *tl++ = txdr_unsigned(NFSV41_MINORVERSION);
4725 *tl++ = txdr_unsigned(callback);
4726 *tl++ = txdr_unsigned(2);
4727 *tl = txdr_unsigned(NFSV4OP_CBSEQUENCE);
4728 error = nfsv4_setcbsequence(nd, clp, 1, sepp, slotposp);
4731 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
4732 *tl = txdr_unsigned(op);
4734 *tl++ = txdr_unsigned(NFSV4_MINORVERSION);
4735 *tl++ = txdr_unsigned(callback);
4736 *tl++ = txdr_unsigned(1);
4737 *tl = txdr_unsigned(op);
4743 * Return the next index# for a clientid. Mostly just increment and return
4744 * the next one, but... if the 32bit unsigned does actually wrap around,
4745 * it should be rebooted.
4746 * At an average rate of one new client per second, it will wrap around in
4747 * approximately 136 years. (I think the server will have been shut
4748 * down or rebooted before then.)
4751 nfsrv_nextclientindex(void)
4753 static u_int32_t client_index = 0;
4756 if (client_index != 0)
4757 return (client_index);
4759 printf("%s: out of clientids\n", __func__);
4760 return (client_index);
4764 * Return the next index# for a stateid. Mostly just increment and return
4765 * the next one, but... if the 32bit unsigned does actually wrap around
4766 * (will a BSD server stay up that long?), find
4767 * new start and end values.
4770 nfsrv_nextstateindex(struct nfsclient *clp)
4772 struct nfsstate *stp;
4774 u_int32_t canuse, min_index, max_index;
4776 if (!(clp->lc_flags & LCL_INDEXNOTOK)) {
4777 clp->lc_stateindex++;
4778 if (clp->lc_stateindex != clp->lc_statemaxindex)
4779 return (clp->lc_stateindex);
4783 * Yuck, we've hit the end.
4784 * Look for a new min and max.
4787 max_index = 0xffffffff;
4788 for (i = 0; i < nfsrv_statehashsize; i++) {
4789 LIST_FOREACH(stp, &clp->lc_stateid[i], ls_hash) {
4790 if (stp->ls_stateid.other[2] > 0x80000000) {
4791 if (stp->ls_stateid.other[2] < max_index)
4792 max_index = stp->ls_stateid.other[2];
4794 if (stp->ls_stateid.other[2] > min_index)
4795 min_index = stp->ls_stateid.other[2];
4801 * Yikes, highly unlikely, but I'll handle it anyhow.
4803 if (min_index == 0x80000000 && max_index == 0x80000001) {
4806 * Loop around until we find an unused entry. Return that
4807 * and set LCL_INDEXNOTOK, so the search will continue next time.
4808 * (This is one of those rare cases where a goto is the
4809 * cleanest way to code the loop.)
4812 for (i = 0; i < nfsrv_statehashsize; i++) {
4813 LIST_FOREACH(stp, &clp->lc_stateid[i], ls_hash) {
4814 if (stp->ls_stateid.other[2] == canuse) {
4820 clp->lc_flags |= LCL_INDEXNOTOK;
4825 * Ok to start again from min + 1.
4827 clp->lc_stateindex = min_index + 1;
4828 clp->lc_statemaxindex = max_index;
4829 clp->lc_flags &= ~LCL_INDEXNOTOK;
4830 return (clp->lc_stateindex);
4834 * The following functions handle the stable storage file that deals with
4835 * the edge conditions described in RFC3530 Sec. 8.6.3.
4836 * The file is as follows:
4837 * - a single record at the beginning that has the lease time of the
4838 * previous server instance (before the last reboot) and the nfsrvboottime
4839 * values for the previous server boots.
4840 * These previous boot times are used to ensure that the current
4841 * nfsrvboottime does not, somehow, get set to a previous one.
4842 * (This is important so that Stale ClientIDs and StateIDs can
4844 * The number of previous nfsvrboottime values precedes the list.
4845 * - followed by some number of appended records with:
4846 * - client id string
4847 * - flag that indicates it is a record revoking state via lease
4848 * expiration or similar
4849 * OR has successfully acquired state.
4850 * These structures vary in length, with the client string at the end, up
4851 * to NFSV4_OPAQUELIMIT in size.
4853 * At the end of the grace period, the file is truncated, the first
4854 * record is rewritten with updated information and any acquired state
4855 * records for successful reclaims of state are written.
4857 * Subsequent records are appended when the first state is issued to
4858 * a client and when state is revoked for a client.
4860 * When reading the file in, state issued records that come later in
4861 * the file override older ones, since the append log is in cronological order.
4862 * If, for some reason, the file can't be read, the grace period is
4863 * immediately terminated and all reclaims get NFSERR_NOGRACE.
4867 * Read in the stable storage file. Called by nfssvc() before the nfsd
4868 * processes start servicing requests.
4871 nfsrv_setupstable(NFSPROC_T *p)
4873 struct nfsrv_stablefirst *sf = &nfsrv_stablefirst;
4874 struct nfsrv_stable *sp, *nsp;
4875 struct nfst_rec *tsp;
4876 int error, i, tryagain;
4878 ssize_t aresid, len;
4881 * If NFSNSF_UPDATEDONE is set, this is a restart of the nfsds without
4882 * a reboot, so state has not been lost.
4884 if (sf->nsf_flags & NFSNSF_UPDATEDONE)
4887 * Set Grace over just until the file reads successfully.
4889 nfsrvboottime = time_second;
4890 LIST_INIT(&sf->nsf_head);
4891 sf->nsf_flags = (NFSNSF_GRACEOVER | NFSNSF_NEEDLOCK);
4892 sf->nsf_eograce = NFSD_MONOSEC + NFSRV_LEASEDELTA;
4893 if (sf->nsf_fp == NULL)
4895 error = NFSD_RDWR(UIO_READ, NFSFPVNODE(sf->nsf_fp),
4896 (caddr_t)&sf->nsf_rec, sizeof (struct nfsf_rec), off, UIO_SYSSPACE,
4897 0, NFSFPCRED(sf->nsf_fp), &aresid, p);
4898 if (error || aresid || sf->nsf_numboots == 0 ||
4899 sf->nsf_numboots > NFSNSF_MAXNUMBOOTS)
4903 * Now, read in the boottimes.
4905 sf->nsf_bootvals = (time_t *)malloc((sf->nsf_numboots + 1) *
4906 sizeof (time_t), M_TEMP, M_WAITOK);
4907 off = sizeof (struct nfsf_rec);
4908 error = NFSD_RDWR(UIO_READ, NFSFPVNODE(sf->nsf_fp),
4909 (caddr_t)sf->nsf_bootvals, sf->nsf_numboots * sizeof (time_t), off,
4910 UIO_SYSSPACE, 0, NFSFPCRED(sf->nsf_fp), &aresid, p);
4911 if (error || aresid) {
4912 free(sf->nsf_bootvals, M_TEMP);
4913 sf->nsf_bootvals = NULL;
4918 * Make sure this nfsrvboottime is different from all recorded
4923 for (i = 0; i < sf->nsf_numboots; i++) {
4924 if (nfsrvboottime == sf->nsf_bootvals[i]) {
4932 sf->nsf_flags |= NFSNSF_OK;
4933 off += (sf->nsf_numboots * sizeof (time_t));
4936 * Read through the file, building a list of records for grace
4938 * Each record is between sizeof (struct nfst_rec) and
4939 * sizeof (struct nfst_rec) + NFSV4_OPAQUELIMIT - 1
4940 * and is actually sizeof (struct nfst_rec) + nst_len - 1.
4942 tsp = (struct nfst_rec *)malloc(sizeof (struct nfst_rec) +
4943 NFSV4_OPAQUELIMIT - 1, M_TEMP, M_WAITOK);
4945 error = NFSD_RDWR(UIO_READ, NFSFPVNODE(sf->nsf_fp),
4946 (caddr_t)tsp, sizeof (struct nfst_rec) + NFSV4_OPAQUELIMIT - 1,
4947 off, UIO_SYSSPACE, 0, NFSFPCRED(sf->nsf_fp), &aresid, p);
4948 len = (sizeof (struct nfst_rec) + NFSV4_OPAQUELIMIT - 1) - aresid;
4949 if (error || (len > 0 && (len < sizeof (struct nfst_rec) ||
4950 len < (sizeof (struct nfst_rec) + tsp->len - 1)))) {
4952 * Yuck, the file has been corrupted, so just return
4953 * after clearing out any restart state, so the grace period
4956 LIST_FOREACH_SAFE(sp, &sf->nsf_head, nst_list, nsp) {
4957 LIST_REMOVE(sp, nst_list);
4961 sf->nsf_flags &= ~NFSNSF_OK;
4962 free(sf->nsf_bootvals, M_TEMP);
4963 sf->nsf_bootvals = NULL;
4967 off += sizeof (struct nfst_rec) + tsp->len - 1;
4969 * Search the list for a matching client.
4971 LIST_FOREACH(sp, &sf->nsf_head, nst_list) {
4972 if (tsp->len == sp->nst_len &&
4973 !NFSBCMP(tsp->client, sp->nst_client, tsp->len))
4976 if (sp == LIST_END(&sf->nsf_head)) {
4977 sp = (struct nfsrv_stable *)malloc(tsp->len +
4978 sizeof (struct nfsrv_stable) - 1, M_TEMP,
4980 NFSBCOPY((caddr_t)tsp, (caddr_t)&sp->nst_rec,
4981 sizeof (struct nfst_rec) + tsp->len - 1);
4982 LIST_INSERT_HEAD(&sf->nsf_head, sp, nst_list);
4984 if (tsp->flag == NFSNST_REVOKE)
4985 sp->nst_flag |= NFSNST_REVOKE;
4988 * A subsequent timestamp indicates the client
4989 * did a setclientid/confirm and any previous
4990 * revoke is no longer relevant.
4992 sp->nst_flag &= ~NFSNST_REVOKE;
4997 sf->nsf_flags = NFSNSF_OK;
4998 sf->nsf_eograce = NFSD_MONOSEC + sf->nsf_lease +
5003 * Update the stable storage file, now that the grace period is over.
5006 nfsrv_updatestable(NFSPROC_T *p)
5008 struct nfsrv_stablefirst *sf = &nfsrv_stablefirst;
5009 struct nfsrv_stable *sp, *nsp;
5011 struct nfsvattr nva;
5016 if (sf->nsf_fp == NULL || (sf->nsf_flags & NFSNSF_UPDATEDONE))
5018 sf->nsf_flags |= NFSNSF_UPDATEDONE;
5020 * Ok, we need to rewrite the stable storage file.
5021 * - truncate to 0 length
5022 * - write the new first structure
5023 * - loop through the data structures, writing out any that
5024 * have timestamps older than the old boot
5026 if (sf->nsf_bootvals) {
5028 for (i = sf->nsf_numboots - 2; i >= 0; i--)
5029 sf->nsf_bootvals[i + 1] = sf->nsf_bootvals[i];
5031 sf->nsf_numboots = 1;
5032 sf->nsf_bootvals = (time_t *)malloc(sizeof (time_t),
5035 sf->nsf_bootvals[0] = nfsrvboottime;
5036 sf->nsf_lease = nfsrv_lease;
5037 NFSVNO_ATTRINIT(&nva);
5038 NFSVNO_SETATTRVAL(&nva, size, 0);
5039 vp = NFSFPVNODE(sf->nsf_fp);
5040 vn_start_write(vp, &mp, V_WAIT);
5041 if (NFSVOPLOCK(vp, LK_EXCLUSIVE) == 0) {
5042 error = nfsvno_setattr(vp, &nva, NFSFPCRED(sf->nsf_fp), p,
5047 vn_finished_write(mp);
5049 error = NFSD_RDWR(UIO_WRITE, vp,
5050 (caddr_t)&sf->nsf_rec, sizeof (struct nfsf_rec), (off_t)0,
5051 UIO_SYSSPACE, IO_SYNC, NFSFPCRED(sf->nsf_fp), NULL, p);
5053 error = NFSD_RDWR(UIO_WRITE, vp,
5054 (caddr_t)sf->nsf_bootvals,
5055 sf->nsf_numboots * sizeof (time_t),
5056 (off_t)(sizeof (struct nfsf_rec)),
5057 UIO_SYSSPACE, IO_SYNC, NFSFPCRED(sf->nsf_fp), NULL, p);
5058 free(sf->nsf_bootvals, M_TEMP);
5059 sf->nsf_bootvals = NULL;
5061 sf->nsf_flags &= ~NFSNSF_OK;
5062 printf("EEK! Can't write NfsV4 stable storage file\n");
5065 sf->nsf_flags |= NFSNSF_OK;
5068 * Loop through the list and write out timestamp records for
5069 * any clients that successfully reclaimed state.
5071 LIST_FOREACH_SAFE(sp, &sf->nsf_head, nst_list, nsp) {
5072 if (sp->nst_flag & NFSNST_GOTSTATE) {
5073 nfsrv_writestable(sp->nst_client, sp->nst_len,
5074 NFSNST_NEWSTATE, p);
5075 sp->nst_clp->lc_flags |= LCL_STAMPEDSTABLE;
5077 LIST_REMOVE(sp, nst_list);
5080 nfsrv_backupstable();
5084 * Append a record to the stable storage file.
5087 nfsrv_writestable(u_char *client, int len, int flag, NFSPROC_T *p)
5089 struct nfsrv_stablefirst *sf = &nfsrv_stablefirst;
5090 struct nfst_rec *sp;
5093 if (!(sf->nsf_flags & NFSNSF_OK) || sf->nsf_fp == NULL)
5095 sp = (struct nfst_rec *)malloc(sizeof (struct nfst_rec) +
5096 len - 1, M_TEMP, M_WAITOK);
5098 NFSBCOPY(client, sp->client, len);
5100 error = NFSD_RDWR(UIO_WRITE, NFSFPVNODE(sf->nsf_fp),
5101 (caddr_t)sp, sizeof (struct nfst_rec) + len - 1, (off_t)0,
5102 UIO_SYSSPACE, (IO_SYNC | IO_APPEND), NFSFPCRED(sf->nsf_fp), NULL, p);
5105 sf->nsf_flags &= ~NFSNSF_OK;
5106 printf("EEK! Can't write NfsV4 stable storage file\n");
5111 * This function is called during the grace period to mark a client
5112 * that successfully reclaimed state.
5115 nfsrv_markstable(struct nfsclient *clp)
5117 struct nfsrv_stable *sp;
5120 * First find the client structure.
5122 LIST_FOREACH(sp, &nfsrv_stablefirst.nsf_head, nst_list) {
5123 if (sp->nst_len == clp->lc_idlen &&
5124 !NFSBCMP(sp->nst_client, clp->lc_id, sp->nst_len))
5127 if (sp == LIST_END(&nfsrv_stablefirst.nsf_head))
5131 * Now, just mark it and set the nfsclient back pointer.
5133 sp->nst_flag |= NFSNST_GOTSTATE;
5138 * This function is called when a NFSv4.1 client does a ReclaimComplete.
5139 * Very similar to nfsrv_markstable(), except for the flag being set.
5142 nfsrv_markreclaim(struct nfsclient *clp)
5144 struct nfsrv_stable *sp;
5147 * First find the client structure.
5149 LIST_FOREACH(sp, &nfsrv_stablefirst.nsf_head, nst_list) {
5150 if (sp->nst_len == clp->lc_idlen &&
5151 !NFSBCMP(sp->nst_client, clp->lc_id, sp->nst_len))
5154 if (sp == LIST_END(&nfsrv_stablefirst.nsf_head))
5158 * Now, just set the flag.
5160 sp->nst_flag |= NFSNST_RECLAIMED;
5164 * This function is called for a reclaim, to see if it gets grace.
5165 * It returns 0 if a reclaim is allowed, 1 otherwise.
5168 nfsrv_checkstable(struct nfsclient *clp)
5170 struct nfsrv_stable *sp;
5173 * First, find the entry for the client.
5175 LIST_FOREACH(sp, &nfsrv_stablefirst.nsf_head, nst_list) {
5176 if (sp->nst_len == clp->lc_idlen &&
5177 !NFSBCMP(sp->nst_client, clp->lc_id, sp->nst_len))
5182 * If not in the list, state was revoked or no state was issued
5183 * since the previous reboot, a reclaim is denied.
5185 if (sp == LIST_END(&nfsrv_stablefirst.nsf_head) ||
5186 (sp->nst_flag & NFSNST_REVOKE) ||
5187 !(nfsrv_stablefirst.nsf_flags & NFSNSF_OK))
5193 * Test for and try to clear out a conflicting client. This is called by
5194 * nfsrv_lockctrl() and nfsrv_openctrl() when conflicts with other clients
5196 * The trick here is that it can't revoke a conflicting client with an
5197 * expired lease unless it holds the v4root lock, so...
5198 * If no v4root lock, get the lock and return 1 to indicate "try again".
5199 * Return 0 to indicate the conflict can't be revoked and 1 to indicate
5200 * the revocation worked and the conflicting client is "bye, bye", so it
5201 * can be tried again.
5202 * Return 2 to indicate that the vnode is VIRF_DOOMED after NFSVOPLOCK().
5203 * Unlocks State before a non-zero value is returned.
5206 nfsrv_clientconflict(struct nfsclient *clp, int *haslockp, vnode_t vp,
5209 int gotlock, lktype = 0;
5212 * If lease hasn't expired, we can't fix it.
5214 if (clp->lc_expiry >= NFSD_MONOSEC ||
5215 !(nfsrv_stablefirst.nsf_flags & NFSNSF_UPDATEDONE))
5217 if (*haslockp == 0) {
5220 lktype = NFSVOPISLOCKED(vp);
5223 NFSLOCKV4ROOTMUTEX();
5224 nfsv4_relref(&nfsv4rootfs_lock);
5226 gotlock = nfsv4_lock(&nfsv4rootfs_lock, 1, NULL,
5227 NFSV4ROOTLOCKMUTEXPTR, NULL);
5229 NFSUNLOCKV4ROOTMUTEX();
5232 NFSVOPLOCK(vp, lktype | LK_RETRY);
5233 if (VN_IS_DOOMED(vp))
5241 * Ok, we can expire the conflicting client.
5243 nfsrv_writestable(clp->lc_id, clp->lc_idlen, NFSNST_REVOKE, p);
5244 nfsrv_backupstable();
5245 nfsrv_cleanclient(clp, p);
5246 nfsrv_freedeleglist(&clp->lc_deleg);
5247 nfsrv_freedeleglist(&clp->lc_olddeleg);
5248 LIST_REMOVE(clp, lc_hash);
5249 nfsrv_zapclient(clp, p);
5254 * Resolve a delegation conflict.
5255 * Returns 0 to indicate the conflict was resolved without sleeping.
5256 * Return -1 to indicate that the caller should check for conflicts again.
5257 * Return > 0 for an error that should be returned, normally NFSERR_DELAY.
5259 * Also, manipulate the nfsv4root_lock, as required. It isn't changed
5260 * for a return of 0, since there was no sleep and it could be required
5261 * later. It is released for a return of NFSERR_DELAY, since the caller
5262 * will return that error. It is released when a sleep was done waiting
5263 * for the delegation to be returned or expire (so that other nfsds can
5264 * handle ops). Then, it must be acquired for the write to stable storage.
5265 * (This function is somewhat similar to nfsrv_clientconflict(), but
5266 * the semantics differ in a couple of subtle ways. The return of 0
5267 * indicates the conflict was resolved without sleeping here, not
5268 * that the conflict can't be resolved and the handling of nfsv4root_lock
5269 * differs, as noted above.)
5270 * Unlocks State before returning a non-zero value.
5273 nfsrv_delegconflict(struct nfsstate *stp, int *haslockp, NFSPROC_T *p,
5276 struct nfsclient *clp = stp->ls_clp;
5277 int gotlock, error, lktype = 0, retrycnt, zapped_clp;
5278 nfsv4stateid_t tstateid;
5282 * If the conflict is with an old delegation...
5284 if (stp->ls_flags & NFSLCK_OLDDELEG) {
5286 * You can delete it, if it has expired.
5288 if (clp->lc_delegtime < NFSD_MONOSEC) {
5289 nfsrv_freedeleg(stp);
5296 * During this delay, the old delegation could expire or it
5297 * could be recovered by the client via an Open with
5298 * CLAIM_DELEGATE_PREV.
5299 * Release the nfsv4root_lock, if held.
5303 NFSLOCKV4ROOTMUTEX();
5304 nfsv4_unlock(&nfsv4rootfs_lock, 1);
5305 NFSUNLOCKV4ROOTMUTEX();
5307 error = NFSERR_DELAY;
5312 * It's a current delegation, so:
5313 * - check to see if the delegation has expired
5314 * - if so, get the v4root lock and then expire it
5316 if ((stp->ls_flags & NFSLCK_DELEGRECALL) == 0 || (stp->ls_lastrecall <
5317 NFSD_MONOSEC && clp->lc_expiry >= NFSD_MONOSEC &&
5318 stp->ls_delegtime >= NFSD_MONOSEC)) {
5320 * - do a recall callback, since not yet done
5321 * For now, never allow truncate to be set. To use
5322 * truncate safely, it must be guaranteed that the
5323 * Remove, Rename or Setattr with size of 0 will
5324 * succeed and that would require major changes to
5325 * the VFS/Vnode OPs.
5326 * Set the expiry time large enough so that it won't expire
5327 * until after the callback, then set it correctly, once
5328 * the callback is done. (The delegation will now time
5329 * out whether or not the Recall worked ok. The timeout
5330 * will be extended when ops are done on the delegation
5331 * stateid, up to the timelimit.)
5333 if ((stp->ls_flags & NFSLCK_DELEGRECALL) == 0) {
5334 stp->ls_delegtime = NFSD_MONOSEC + (2 * nfsrv_lease) +
5336 stp->ls_delegtimelimit = NFSD_MONOSEC + (6 *
5337 nfsrv_lease) + NFSRV_LEASEDELTA;
5338 stp->ls_flags |= NFSLCK_DELEGRECALL;
5340 stp->ls_lastrecall = time_uptime + 1;
5343 * Loop NFSRV_CBRETRYCNT times while the CBRecall replies
5344 * NFSERR_BADSTATEID or NFSERR_BADHANDLE. This is done
5345 * in order to try and avoid a race that could happen
5346 * when a CBRecall request passed the Open reply with
5347 * the delegation in it when transitting the network.
5348 * Since nfsrv_docallback will sleep, don't use stp after
5351 NFSBCOPY((caddr_t)&stp->ls_stateid, (caddr_t)&tstateid,
5353 NFSBCOPY((caddr_t)&stp->ls_lfp->lf_fh, (caddr_t)&tfh,
5358 NFSLOCKV4ROOTMUTEX();
5359 nfsv4_unlock(&nfsv4rootfs_lock, 1);
5360 NFSUNLOCKV4ROOTMUTEX();
5364 error = nfsrv_docallback(clp, NFSV4OP_CBRECALL,
5365 &tstateid, 0, &tfh, NULL, NULL, 0, p);
5367 } while ((error == NFSERR_BADSTATEID ||
5368 error == NFSERR_BADHANDLE) && retrycnt < NFSV4_CBRETRYCNT);
5369 error = NFSERR_DELAY;
5373 if (clp->lc_expiry >= NFSD_MONOSEC &&
5374 stp->ls_delegtime >= NFSD_MONOSEC) {
5377 * A recall has been done, but it has not yet expired.
5382 NFSLOCKV4ROOTMUTEX();
5383 nfsv4_unlock(&nfsv4rootfs_lock, 1);
5384 NFSUNLOCKV4ROOTMUTEX();
5386 error = NFSERR_DELAY;
5391 * If we don't yet have the lock, just get it and then return,
5392 * since we need that before deleting expired state, such as
5394 * When getting the lock, unlock the vnode, so other nfsds that
5395 * are in progress, won't get stuck waiting for the vnode lock.
5397 if (*haslockp == 0) {
5400 lktype = NFSVOPISLOCKED(vp);
5403 NFSLOCKV4ROOTMUTEX();
5404 nfsv4_relref(&nfsv4rootfs_lock);
5406 gotlock = nfsv4_lock(&nfsv4rootfs_lock, 1, NULL,
5407 NFSV4ROOTLOCKMUTEXPTR, NULL);
5409 NFSUNLOCKV4ROOTMUTEX();
5412 NFSVOPLOCK(vp, lktype | LK_RETRY);
5413 if (VN_IS_DOOMED(vp)) {
5415 NFSLOCKV4ROOTMUTEX();
5416 nfsv4_unlock(&nfsv4rootfs_lock, 1);
5417 NFSUNLOCKV4ROOTMUTEX();
5418 error = NFSERR_PERM;
5428 * Ok, we can delete the expired delegation.
5429 * First, write the Revoke record to stable storage and then
5430 * clear out the conflict.
5431 * Since all other nfsd threads are now blocked, we can safely
5432 * sleep without the state changing.
5434 nfsrv_writestable(clp->lc_id, clp->lc_idlen, NFSNST_REVOKE, p);
5435 nfsrv_backupstable();
5436 if (clp->lc_expiry < NFSD_MONOSEC) {
5437 nfsrv_cleanclient(clp, p);
5438 nfsrv_freedeleglist(&clp->lc_deleg);
5439 nfsrv_freedeleglist(&clp->lc_olddeleg);
5440 LIST_REMOVE(clp, lc_hash);
5443 nfsrv_freedeleg(stp);
5447 nfsrv_zapclient(clp, p);
5456 * Check for a remove allowed, if remove is set to 1 and get rid of
5460 nfsrv_checkremove(vnode_t vp, int remove, struct nfsrv_descript *nd,
5461 nfsquad_t clientid, NFSPROC_T *p)
5463 struct nfsclient *clp;
5464 struct nfsstate *stp;
5465 struct nfslockfile *lfp;
5466 int error, haslock = 0;
5471 * First, get the lock file structure.
5472 * (A return of -1 means no associated state, so remove ok.)
5474 error = nfsrv_getlockfh(vp, NFSLCK_CHECK, NULL, &nfh, p);
5477 if (error == 0 && clientid.qval != 0)
5478 error = nfsrv_getclient(clientid, CLOPS_RENEW, &clp, NULL,
5479 (nfsquad_t)((u_quad_t)0), 0, nd, p);
5481 error = nfsrv_getlockfile(NFSLCK_CHECK, NULL, &lfp, &nfh, 0);
5485 NFSLOCKV4ROOTMUTEX();
5486 nfsv4_unlock(&nfsv4rootfs_lock, 1);
5487 NFSUNLOCKV4ROOTMUTEX();
5495 * Now, we must Recall any delegations.
5497 error = nfsrv_cleandeleg(vp, lfp, clp, &haslock, p);
5500 * nfsrv_cleandeleg() unlocks state for non-zero
5506 NFSLOCKV4ROOTMUTEX();
5507 nfsv4_unlock(&nfsv4rootfs_lock, 1);
5508 NFSUNLOCKV4ROOTMUTEX();
5514 * Now, look for a conflicting open share.
5518 * If the entry in the directory was the last reference to the
5519 * corresponding filesystem object, the object can be destroyed
5521 if(lfp->lf_usecount>1)
5522 LIST_FOREACH(stp, &lfp->lf_open, ls_file) {
5523 if (stp->ls_flags & NFSLCK_WRITEDENY) {
5524 error = NFSERR_FILEOPEN;
5532 NFSLOCKV4ROOTMUTEX();
5533 nfsv4_unlock(&nfsv4rootfs_lock, 1);
5534 NFSUNLOCKV4ROOTMUTEX();
5543 * Clear out all delegations for the file referred to by lfp.
5544 * May return NFSERR_DELAY, if there will be a delay waiting for
5545 * delegations to expire.
5546 * Returns -1 to indicate it slept while recalling a delegation.
5547 * This function has the side effect of deleting the nfslockfile structure,
5548 * if it no longer has associated state and didn't have to sleep.
5549 * Unlocks State before a non-zero value is returned.
5552 nfsrv_cleandeleg(vnode_t vp, struct nfslockfile *lfp,
5553 struct nfsclient *clp, int *haslockp, NFSPROC_T *p)
5555 struct nfsstate *stp, *nstp;
5558 stp = LIST_FIRST(&lfp->lf_deleg);
5559 while (stp != LIST_END(&lfp->lf_deleg)) {
5560 nstp = LIST_NEXT(stp, ls_file);
5561 if (stp->ls_clp != clp) {
5562 ret = nfsrv_delegconflict(stp, haslockp, p, vp);
5565 * nfsrv_delegconflict() unlocks state
5566 * when it returns non-zero.
5579 * There are certain operations that, when being done outside of NFSv4,
5580 * require that any NFSv4 delegation for the file be recalled.
5581 * This function is to be called for those cases:
5582 * VOP_RENAME() - When a delegation is being recalled for any reason,
5583 * the client may have to do Opens against the server, using the file's
5584 * final component name. If the file has been renamed on the server,
5585 * that component name will be incorrect and the Open will fail.
5586 * VOP_REMOVE() - Theoretically, a client could Open a file after it has
5587 * been removed on the server, if there is a delegation issued to
5588 * that client for the file. I say "theoretically" since clients
5589 * normally do an Access Op before the Open and that Access Op will
5590 * fail with ESTALE. Note that NFSv2 and 3 don't even do Opens, so
5591 * they will detect the file's removal in the same manner. (There is
5592 * one case where RFC3530 allows a client to do an Open without first
5593 * doing an Access Op, which is passage of a check against the ACE
5594 * returned with a Write delegation, but current practice is to ignore
5595 * the ACE and always do an Access Op.)
5596 * Since the functions can only be called with an unlocked vnode, this
5597 * can't be done at this time.
5598 * VOP_ADVLOCK() - When a client holds a delegation, it can issue byte range
5599 * locks locally in the client, which are not visible to the server. To
5600 * deal with this, issuing of delegations for a vnode must be disabled
5601 * and all delegations for the vnode recalled. This is done via the
5602 * second function, using the VV_DISABLEDELEG vflag on the vnode.
5605 nfsd_recalldelegation(vnode_t vp, NFSPROC_T *p)
5611 * First, check to see if the server is currently running and it has
5612 * been called for a regular file when issuing delegations.
5614 if (newnfs_numnfsd == 0 || vp->v_type != VREG ||
5615 nfsrv_issuedelegs == 0)
5618 KASSERT((NFSVOPISLOCKED(vp) != LK_EXCLUSIVE), ("vp %p is locked", vp));
5620 * First, get a reference on the nfsv4rootfs_lock so that an
5621 * exclusive lock cannot be acquired by another thread.
5623 NFSLOCKV4ROOTMUTEX();
5624 nfsv4_getref(&nfsv4rootfs_lock, NULL, NFSV4ROOTLOCKMUTEXPTR, NULL);
5625 NFSUNLOCKV4ROOTMUTEX();
5628 * Now, call nfsrv_checkremove() in a loop while it returns
5629 * NFSERR_DELAY. Return upon any other error or when timed out.
5631 starttime = NFSD_MONOSEC;
5633 if (NFSVOPLOCK(vp, LK_EXCLUSIVE) == 0) {
5634 error = nfsrv_checkremove(vp, 0, NULL,
5635 (nfsquad_t)((u_quad_t)0), p);
5639 if (error == NFSERR_DELAY) {
5640 if (NFSD_MONOSEC - starttime > NFS_REMOVETIMEO)
5642 /* Sleep for a short period of time */
5643 (void) nfs_catnap(PZERO, 0, "nfsremove");
5645 } while (error == NFSERR_DELAY);
5646 NFSLOCKV4ROOTMUTEX();
5647 nfsv4_relref(&nfsv4rootfs_lock);
5648 NFSUNLOCKV4ROOTMUTEX();
5652 nfsd_disabledelegation(vnode_t vp, NFSPROC_T *p)
5655 #ifdef VV_DISABLEDELEG
5657 * First, flag issuance of delegations disabled.
5659 atomic_set_long(&vp->v_vflag, VV_DISABLEDELEG);
5663 * Then call nfsd_recalldelegation() to get rid of all extant
5666 nfsd_recalldelegation(vp, p);
5670 * Check for conflicting locks, etc. and then get rid of delegations.
5671 * (At one point I thought that I should get rid of delegations for any
5672 * Setattr, since it could potentially disallow the I/O op (read or write)
5673 * allowed by the delegation. However, Setattr Ops that aren't changing
5674 * the size get a stateid of all 0s, so you can't tell if it is a delegation
5675 * for the same client or a different one, so I decided to only get rid
5676 * of delegations for other clients when the size is being changed.)
5677 * In general, a Setattr can disable NFS I/O Ops that are outstanding, such
5678 * as Write backs, even if there is no delegation, so it really isn't any
5682 nfsrv_checksetattr(vnode_t vp, struct nfsrv_descript *nd,
5683 nfsv4stateid_t *stateidp, struct nfsvattr *nvap, nfsattrbit_t *attrbitp,
5684 struct nfsexstuff *exp, NFSPROC_T *p)
5686 struct nfsstate st, *stp = &st;
5687 struct nfslock lo, *lop = &lo;
5691 if (NFSISSET_ATTRBIT(attrbitp, NFSATTRBIT_SIZE)) {
5692 stp->ls_flags = (NFSLCK_CHECK | NFSLCK_WRITEACCESS);
5693 lop->lo_first = nvap->na_size;
5698 if (NFSISSET_ATTRBIT(attrbitp, NFSATTRBIT_OWNER) ||
5699 NFSISSET_ATTRBIT(attrbitp, NFSATTRBIT_OWNERGROUP) ||
5700 NFSISSET_ATTRBIT(attrbitp, NFSATTRBIT_MODE) ||
5701 NFSISSET_ATTRBIT(attrbitp, NFSATTRBIT_ACL))
5702 stp->ls_flags |= NFSLCK_SETATTR;
5703 if (stp->ls_flags == 0)
5705 lop->lo_end = NFS64BITSSET;
5706 lop->lo_flags = NFSLCK_WRITE;
5707 stp->ls_ownerlen = 0;
5709 stp->ls_uid = nd->nd_cred->cr_uid;
5710 stp->ls_stateid.seqid = stateidp->seqid;
5711 clientid.lval[0] = stp->ls_stateid.other[0] = stateidp->other[0];
5712 clientid.lval[1] = stp->ls_stateid.other[1] = stateidp->other[1];
5713 stp->ls_stateid.other[2] = stateidp->other[2];
5714 error = nfsrv_lockctrl(vp, &stp, &lop, NULL, clientid,
5715 stateidp, exp, nd, p);
5718 NFSEXITCODE2(error, nd);
5723 * Check for a write delegation and do a CBGETATTR if there is one, updating
5724 * the attributes, as required.
5725 * Should I return an error if I can't get the attributes? (For now, I'll
5729 nfsrv_checkgetattr(struct nfsrv_descript *nd, vnode_t vp,
5730 struct nfsvattr *nvap, nfsattrbit_t *attrbitp, NFSPROC_T *p)
5732 struct nfsstate *stp;
5733 struct nfslockfile *lfp;
5734 struct nfsclient *clp;
5735 struct nfsvattr nva;
5738 nfsattrbit_t cbbits;
5739 u_quad_t delegfilerev;
5741 NFSCBGETATTR_ATTRBIT(attrbitp, &cbbits);
5742 if (!NFSNONZERO_ATTRBIT(&cbbits))
5744 if (nfsrv_writedelegcnt == 0)
5748 * Get the lock file structure.
5749 * (A return of -1 means no associated state, so return ok.)
5751 error = nfsrv_getlockfh(vp, NFSLCK_CHECK, NULL, &nfh, p);
5754 error = nfsrv_getlockfile(NFSLCK_CHECK, NULL, &lfp, &nfh, 0);
5763 * Now, look for a write delegation.
5765 LIST_FOREACH(stp, &lfp->lf_deleg, ls_file) {
5766 if (stp->ls_flags & NFSLCK_DELEGWRITE)
5769 if (stp == LIST_END(&lfp->lf_deleg)) {
5775 /* If the clientid is not confirmed, ignore the delegation. */
5776 if (clp->lc_flags & LCL_NEEDSCONFIRM) {
5781 delegfilerev = stp->ls_filerev;
5783 * If the Write delegation was issued as a part of this Compound RPC
5784 * or if we have an Implied Clientid (used in a previous Op in this
5785 * compound) and it is the client the delegation was issued to,
5787 * I also assume that it is from the same client iff the network
5788 * host IP address is the same as the callback address. (Not
5789 * exactly correct by the RFC, but avoids a lot of Getattr
5792 if (nd->nd_compref == stp->ls_compref ||
5793 ((nd->nd_flag & ND_IMPLIEDCLID) &&
5794 clp->lc_clientid.qval == nd->nd_clientid.qval) ||
5795 nfsaddr2_match(clp->lc_req.nr_nam, nd->nd_nam)) {
5801 * We are now done with the delegation state structure,
5802 * so the statelock can be released and we can now tsleep().
5806 * Now, we must do the CB Getattr callback, to see if Change or Size
5809 if (clp->lc_expiry >= NFSD_MONOSEC) {
5811 NFSVNO_ATTRINIT(&nva);
5812 nva.na_filerev = NFS64BITSSET;
5813 error = nfsrv_docallback(clp, NFSV4OP_CBGETATTR, NULL,
5814 0, &nfh, &nva, &cbbits, 0, p);
5816 if ((nva.na_filerev != NFS64BITSSET &&
5817 nva.na_filerev > delegfilerev) ||
5818 (NFSVNO_ISSETSIZE(&nva) &&
5819 nva.na_size != nvap->na_size)) {
5820 error = nfsvno_updfilerev(vp, nvap, nd, p);
5821 if (NFSVNO_ISSETSIZE(&nva))
5822 nvap->na_size = nva.na_size;
5825 error = 0; /* Ignore callback errors for now. */
5831 NFSEXITCODE2(error, nd);
5836 * This function looks for openowners that haven't had any opens for
5837 * a while and throws them away. Called by an nfsd when NFSNSF_NOOPENS
5841 nfsrv_throwawayopens(NFSPROC_T *p)
5843 struct nfsclient *clp, *nclp;
5844 struct nfsstate *stp, *nstp;
5848 nfsrv_stablefirst.nsf_flags &= ~NFSNSF_NOOPENS;
5850 * For each client...
5852 for (i = 0; i < nfsrv_clienthashsize; i++) {
5853 LIST_FOREACH_SAFE(clp, &nfsclienthash[i], lc_hash, nclp) {
5854 LIST_FOREACH_SAFE(stp, &clp->lc_open, ls_list, nstp) {
5855 if (LIST_EMPTY(&stp->ls_open) &&
5856 (stp->ls_noopens > NFSNOOPEN ||
5857 (nfsrv_openpluslock * 2) >
5858 nfsrv_v4statelimit))
5859 nfsrv_freeopenowner(stp, 0, p);
5867 * This function checks to see if the credentials are the same.
5868 * The check for same credentials is needed for state management operations
5869 * for NFSv4.0 where 1 is returned if not same, 0 is returned otherwise.
5872 nfsrv_notsamecredname(struct nfsrv_descript *nd, struct nfsclient *clp)
5875 /* For NFSv4.1/4.2, SP4_NONE always allows this. */
5876 if ((nd->nd_flag & ND_NFSV41) != 0)
5879 if (nd->nd_flag & ND_GSS) {
5880 if (!(clp->lc_flags & LCL_GSS))
5882 if (clp->lc_flags & LCL_NAME) {
5883 if (nd->nd_princlen != clp->lc_namelen ||
5884 NFSBCMP(nd->nd_principal, clp->lc_name,
5890 if (nd->nd_cred->cr_uid == clp->lc_uid)
5894 } else if (clp->lc_flags & LCL_GSS)
5897 * For AUTH_SYS, allow the same uid or root. (This is underspecified
5898 * in RFC3530, which talks about principals, but doesn't say anything
5899 * about uids for AUTH_SYS.)
5901 if (nd->nd_cred->cr_uid == clp->lc_uid || nd->nd_cred->cr_uid == 0)
5908 * Calculate the lease expiry time.
5911 nfsrv_leaseexpiry(void)
5914 if (nfsrv_stablefirst.nsf_eograce > NFSD_MONOSEC)
5915 return (NFSD_MONOSEC + 2 * (nfsrv_lease + NFSRV_LEASEDELTA));
5916 return (NFSD_MONOSEC + nfsrv_lease + NFSRV_LEASEDELTA);
5920 * Delay the delegation timeout as far as ls_delegtimelimit, as required.
5923 nfsrv_delaydelegtimeout(struct nfsstate *stp)
5926 if ((stp->ls_flags & NFSLCK_DELEGRECALL) == 0)
5929 if ((stp->ls_delegtime + 15) > NFSD_MONOSEC &&
5930 stp->ls_delegtime < stp->ls_delegtimelimit) {
5931 stp->ls_delegtime += nfsrv_lease;
5932 if (stp->ls_delegtime > stp->ls_delegtimelimit)
5933 stp->ls_delegtime = stp->ls_delegtimelimit;
5938 * This function checks to see if there is any other state associated
5939 * with the openowner for this Open.
5940 * It returns 1 if there is no other state, 0 otherwise.
5943 nfsrv_nootherstate(struct nfsstate *stp)
5945 struct nfsstate *tstp;
5947 LIST_FOREACH(tstp, &stp->ls_openowner->ls_open, ls_list) {
5948 if (tstp != stp || !LIST_EMPTY(&tstp->ls_lock))
5955 * Create a list of lock deltas (changes to local byte range locking
5956 * that can be rolled back using the list) and apply the changes via
5957 * nfsvno_advlock(). Optionally, lock the list. It is expected that either
5958 * the rollback or update function will be called after this.
5959 * It returns an error (and rolls back, as required), if any nfsvno_advlock()
5960 * call fails. If it returns an error, it will unlock the list.
5963 nfsrv_locallock(vnode_t vp, struct nfslockfile *lfp, int flags,
5964 uint64_t first, uint64_t end, struct nfslockconflict *cfp, NFSPROC_T *p)
5966 struct nfslock *lop, *nlop;
5969 /* Loop through the list of locks. */
5970 lop = LIST_FIRST(&lfp->lf_locallock);
5971 while (first < end && lop != NULL) {
5972 nlop = LIST_NEXT(lop, lo_lckowner);
5973 if (first >= lop->lo_end) {
5976 } else if (first < lop->lo_first) {
5977 /* new one starts before entry in list */
5978 if (end <= lop->lo_first) {
5979 /* no overlap between old and new */
5980 error = nfsrv_dolocal(vp, lfp, flags,
5981 NFSLCK_UNLOCK, first, end, cfp, p);
5986 /* handle fragment overlapped with new one */
5987 error = nfsrv_dolocal(vp, lfp, flags,
5988 NFSLCK_UNLOCK, first, lop->lo_first, cfp,
5992 first = lop->lo_first;
5995 /* new one overlaps this entry in list */
5996 if (end <= lop->lo_end) {
5997 /* overlaps all of new one */
5998 error = nfsrv_dolocal(vp, lfp, flags,
5999 lop->lo_flags, first, end, cfp, p);
6004 /* handle fragment overlapped with new one */
6005 error = nfsrv_dolocal(vp, lfp, flags,
6006 lop->lo_flags, first, lop->lo_end, cfp, p);
6009 first = lop->lo_end;
6014 if (first < end && error == 0)
6015 /* handle fragment past end of list */
6016 error = nfsrv_dolocal(vp, lfp, flags, NFSLCK_UNLOCK, first,
6024 * Local lock unlock. Unlock all byte ranges that are no longer locked
6025 * by NFSv4. To do this, unlock any subranges of first-->end that
6026 * do not overlap with the byte ranges of any lock in the lfp->lf_lock
6027 * list. This list has all locks for the file held by other
6028 * <clientid, lockowner> tuples. The list is ordered by increasing
6029 * lo_first value, but may have entries that overlap each other, for
6030 * the case of read locks.
6033 nfsrv_localunlock(vnode_t vp, struct nfslockfile *lfp, uint64_t init_first,
6034 uint64_t init_end, NFSPROC_T *p)
6036 struct nfslock *lop;
6037 uint64_t first, end, prevfirst __unused;
6041 while (first < init_end) {
6042 /* Loop through all nfs locks, adjusting first and end */
6044 LIST_FOREACH(lop, &lfp->lf_lock, lo_lckfile) {
6045 KASSERT(prevfirst <= lop->lo_first,
6046 ("nfsv4 locks out of order"));
6047 KASSERT(lop->lo_first < lop->lo_end,
6048 ("nfsv4 bogus lock"));
6049 prevfirst = lop->lo_first;
6050 if (first >= lop->lo_first &&
6051 first < lop->lo_end)
6053 * Overlaps with initial part, so trim
6054 * off that initial part by moving first past
6057 first = lop->lo_end;
6058 else if (end > lop->lo_first &&
6059 lop->lo_first > first) {
6061 * This lock defines the end of the
6062 * segment to unlock, so set end to the
6063 * start of it and break out of the loop.
6065 end = lop->lo_first;
6070 * There is no segment left to do, so
6071 * break out of this loop and then exit
6072 * the outer while() since first will be set
6073 * to end, which must equal init_end here.
6078 /* Unlock this segment */
6079 (void) nfsrv_dolocal(vp, lfp, NFSLCK_UNLOCK,
6080 NFSLCK_READ, first, end, NULL, p);
6081 nfsrv_locallock_commit(lfp, NFSLCK_UNLOCK,
6085 * Now move past this segment and look for any further
6086 * segment in the range, if there is one.
6094 * Do the local lock operation and update the rollback list, as required.
6095 * Perform the rollback and return the error if nfsvno_advlock() fails.
6098 nfsrv_dolocal(vnode_t vp, struct nfslockfile *lfp, int flags, int oldflags,
6099 uint64_t first, uint64_t end, struct nfslockconflict *cfp, NFSPROC_T *p)
6101 struct nfsrollback *rlp;
6102 int error = 0, ltype, oldltype;
6104 if (flags & NFSLCK_WRITE)
6106 else if (flags & NFSLCK_READ)
6110 if (oldflags & NFSLCK_WRITE)
6112 else if (oldflags & NFSLCK_READ)
6116 if (ltype == oldltype || (oldltype == F_WRLCK && ltype == F_RDLCK))
6119 error = nfsvno_advlock(vp, ltype, first, end, p);
6122 cfp->cl_clientid.lval[0] = 0;
6123 cfp->cl_clientid.lval[1] = 0;
6125 cfp->cl_end = NFS64BITSSET;
6126 cfp->cl_flags = NFSLCK_WRITE;
6127 cfp->cl_ownerlen = 5;
6128 NFSBCOPY("LOCAL", cfp->cl_owner, 5);
6130 nfsrv_locallock_rollback(vp, lfp, p);
6131 } else if (ltype != F_UNLCK) {
6132 rlp = malloc(sizeof (struct nfsrollback), M_NFSDROLLBACK,
6134 rlp->rlck_first = first;
6135 rlp->rlck_end = end;
6136 rlp->rlck_type = oldltype;
6137 LIST_INSERT_HEAD(&lfp->lf_rollback, rlp, rlck_list);
6146 * Roll back local lock changes and free up the rollback list.
6149 nfsrv_locallock_rollback(vnode_t vp, struct nfslockfile *lfp, NFSPROC_T *p)
6151 struct nfsrollback *rlp, *nrlp;
6153 LIST_FOREACH_SAFE(rlp, &lfp->lf_rollback, rlck_list, nrlp) {
6154 (void) nfsvno_advlock(vp, rlp->rlck_type, rlp->rlck_first,
6156 free(rlp, M_NFSDROLLBACK);
6158 LIST_INIT(&lfp->lf_rollback);
6162 * Update local lock list and delete rollback list (ie now committed to the
6163 * local locks). Most of the work is done by the internal function.
6166 nfsrv_locallock_commit(struct nfslockfile *lfp, int flags, uint64_t first,
6169 struct nfsrollback *rlp, *nrlp;
6170 struct nfslock *new_lop, *other_lop;
6172 new_lop = malloc(sizeof (struct nfslock), M_NFSDLOCK, M_WAITOK);
6173 if (flags & (NFSLCK_READ | NFSLCK_WRITE))
6174 other_lop = malloc(sizeof (struct nfslock), M_NFSDLOCK,
6178 new_lop->lo_flags = flags;
6179 new_lop->lo_first = first;
6180 new_lop->lo_end = end;
6181 nfsrv_updatelock(NULL, &new_lop, &other_lop, lfp);
6182 if (new_lop != NULL)
6183 free(new_lop, M_NFSDLOCK);
6184 if (other_lop != NULL)
6185 free(other_lop, M_NFSDLOCK);
6187 /* and get rid of the rollback list */
6188 LIST_FOREACH_SAFE(rlp, &lfp->lf_rollback, rlck_list, nrlp)
6189 free(rlp, M_NFSDROLLBACK);
6190 LIST_INIT(&lfp->lf_rollback);
6194 * Lock the struct nfslockfile for local lock updating.
6197 nfsrv_locklf(struct nfslockfile *lfp)
6201 /* lf_usecount ensures *lfp won't be free'd */
6204 gotlock = nfsv4_lock(&lfp->lf_locallock_lck, 1, NULL,
6205 NFSSTATEMUTEXPTR, NULL);
6206 } while (gotlock == 0);
6211 * Unlock the struct nfslockfile after local lock updating.
6214 nfsrv_unlocklf(struct nfslockfile *lfp)
6217 nfsv4_unlock(&lfp->lf_locallock_lck, 0);
6221 * Clear out all state for the NFSv4 server.
6222 * Must be called by a thread that can sleep when no nfsds are running.
6225 nfsrv_throwawayallstate(NFSPROC_T *p)
6227 struct nfsclient *clp, *nclp;
6228 struct nfslockfile *lfp, *nlfp;
6232 * For each client, clean out the state and then free the structure.
6234 for (i = 0; i < nfsrv_clienthashsize; i++) {
6235 LIST_FOREACH_SAFE(clp, &nfsclienthash[i], lc_hash, nclp) {
6236 nfsrv_cleanclient(clp, p);
6237 nfsrv_freedeleglist(&clp->lc_deleg);
6238 nfsrv_freedeleglist(&clp->lc_olddeleg);
6239 free(clp->lc_stateid, M_NFSDCLIENT);
6240 free(clp, M_NFSDCLIENT);
6245 * Also, free up any remaining lock file structures.
6247 for (i = 0; i < nfsrv_lockhashsize; i++) {
6248 LIST_FOREACH_SAFE(lfp, &nfslockhash[i], lf_hash, nlfp) {
6249 printf("nfsd unload: fnd a lock file struct\n");
6250 nfsrv_freenfslockfile(lfp);
6254 /* And get rid of the deviceid structures and layouts. */
6255 nfsrv_freealllayoutsanddevids();
6259 * Check the sequence# for the session and slot provided as an argument.
6260 * Also, renew the lease if the session will return NFS_OK.
6263 nfsrv_checksequence(struct nfsrv_descript *nd, uint32_t sequenceid,
6264 uint32_t *highest_slotidp, uint32_t *target_highest_slotidp, int cache_this,
6265 uint32_t *sflagsp, NFSPROC_T *p)
6267 struct nfsdsession *sep;
6268 struct nfssessionhash *shp;
6271 shp = NFSSESSIONHASH(nd->nd_sessionid);
6272 NFSLOCKSESSION(shp);
6273 sep = nfsrv_findsession(nd->nd_sessionid);
6275 NFSUNLOCKSESSION(shp);
6276 return (NFSERR_BADSESSION);
6278 error = nfsv4_seqsession(sequenceid, nd->nd_slotid, *highest_slotidp,
6279 sep->sess_slots, NULL, NFSV4_SLOTS - 1);
6281 NFSUNLOCKSESSION(shp);
6284 if (cache_this != 0)
6285 nd->nd_flag |= ND_SAVEREPLY;
6286 /* Renew the lease. */
6287 sep->sess_clp->lc_expiry = nfsrv_leaseexpiry();
6288 nd->nd_clientid.qval = sep->sess_clp->lc_clientid.qval;
6289 nd->nd_flag |= ND_IMPLIEDCLID;
6291 /* Save maximum request and reply sizes. */
6292 nd->nd_maxreq = sep->sess_maxreq;
6293 nd->nd_maxresp = sep->sess_maxresp;
6296 if (sep->sess_clp->lc_req.nr_client == NULL ||
6297 (sep->sess_clp->lc_flags & LCL_CBDOWN) != 0)
6298 *sflagsp |= NFSV4SEQ_CBPATHDOWN;
6299 NFSUNLOCKSESSION(shp);
6300 if (error == NFSERR_EXPIRED) {
6301 *sflagsp |= NFSV4SEQ_EXPIREDALLSTATEREVOKED;
6303 } else if (error == NFSERR_ADMINREVOKED) {
6304 *sflagsp |= NFSV4SEQ_ADMINSTATEREVOKED;
6307 *highest_slotidp = *target_highest_slotidp = NFSV4_SLOTS - 1;
6312 * Check/set reclaim complete for this session/clientid.
6315 nfsrv_checkreclaimcomplete(struct nfsrv_descript *nd, int onefs)
6317 struct nfsdsession *sep;
6318 struct nfssessionhash *shp;
6321 shp = NFSSESSIONHASH(nd->nd_sessionid);
6323 NFSLOCKSESSION(shp);
6324 sep = nfsrv_findsession(nd->nd_sessionid);
6326 NFSUNLOCKSESSION(shp);
6328 return (NFSERR_BADSESSION);
6332 sep->sess_clp->lc_flags |= LCL_RECLAIMONEFS;
6333 /* Check to see if reclaim complete has already happened. */
6334 else if ((sep->sess_clp->lc_flags & LCL_RECLAIMCOMPLETE) != 0)
6335 error = NFSERR_COMPLETEALREADY;
6337 sep->sess_clp->lc_flags |= LCL_RECLAIMCOMPLETE;
6338 nfsrv_markreclaim(sep->sess_clp);
6340 NFSUNLOCKSESSION(shp);
6346 * Cache the reply in a session slot.
6349 nfsrv_cache_session(struct nfsrv_descript *nd, struct mbuf **m)
6351 struct nfsdsession *sep;
6352 struct nfssessionhash *shp;
6355 struct sockaddr_in *sin;
6358 struct sockaddr_in6 *sin6;
6361 shp = NFSSESSIONHASH(nd->nd_sessionid);
6362 NFSLOCKSESSION(shp);
6363 sep = nfsrv_findsession(nd->nd_sessionid);
6365 NFSUNLOCKSESSION(shp);
6366 if ((nfsrv_stablefirst.nsf_flags & NFSNSF_GRACEOVER) != 0) {
6367 buf = malloc(INET6_ADDRSTRLEN, M_TEMP, M_WAITOK);
6368 switch (nd->nd_nam->sa_family) {
6371 sin = (struct sockaddr_in *)nd->nd_nam;
6372 cp = inet_ntop(sin->sin_family,
6373 &sin->sin_addr.s_addr, buf,
6379 sin6 = (struct sockaddr_in6 *)nd->nd_nam;
6380 cp = inet_ntop(sin6->sin6_family,
6381 &sin6->sin6_addr, buf, INET6_ADDRSTRLEN);
6388 printf("nfsrv_cache_session: no session "
6389 "IPaddr=%s, check NFS clients for unique "
6390 "/etc/hostid's\n", cp);
6392 printf("nfsrv_cache_session: no session, "
6393 "check NFS clients for unique "
6400 nfsv4_seqsess_cacherep(nd->nd_slotid, sep->sess_slots, nd->nd_repstat,
6402 NFSUNLOCKSESSION(shp);
6406 * Search for a session that matches the sessionid.
6408 static struct nfsdsession *
6409 nfsrv_findsession(uint8_t *sessionid)
6411 struct nfsdsession *sep;
6412 struct nfssessionhash *shp;
6414 shp = NFSSESSIONHASH(sessionid);
6415 LIST_FOREACH(sep, &shp->list, sess_hash) {
6416 if (!NFSBCMP(sessionid, sep->sess_sessionid, NFSX_V4SESSIONID))
6423 * Destroy a session.
6426 nfsrv_destroysession(struct nfsrv_descript *nd, uint8_t *sessionid)
6428 int error, igotlock, samesess;
6431 if (!NFSBCMP(sessionid, nd->nd_sessionid, NFSX_V4SESSIONID) &&
6432 (nd->nd_flag & ND_HASSEQUENCE) != 0) {
6434 if ((nd->nd_flag & ND_LASTOP) == 0)
6435 return (NFSERR_BADSESSION);
6438 /* Lock out other nfsd threads */
6439 NFSLOCKV4ROOTMUTEX();
6440 nfsv4_relref(&nfsv4rootfs_lock);
6442 igotlock = nfsv4_lock(&nfsv4rootfs_lock, 1, NULL,
6443 NFSV4ROOTLOCKMUTEXPTR, NULL);
6444 } while (igotlock == 0);
6445 NFSUNLOCKV4ROOTMUTEX();
6447 error = nfsrv_freesession(NULL, sessionid);
6448 if (error == 0 && samesess != 0)
6449 nd->nd_flag &= ~ND_HASSEQUENCE;
6451 NFSLOCKV4ROOTMUTEX();
6452 nfsv4_unlock(&nfsv4rootfs_lock, 1);
6453 NFSUNLOCKV4ROOTMUTEX();
6458 * Bind a connection to a session.
6459 * For now, only certain variants are supported, since the current session
6460 * structure can only handle a single backchannel entry, which will be
6461 * applied to all connections if it is set.
6464 nfsrv_bindconnsess(struct nfsrv_descript *nd, uint8_t *sessionid, int *foreaftp)
6466 struct nfssessionhash *shp;
6467 struct nfsdsession *sep;
6468 struct nfsclient *clp;
6474 shp = NFSSESSIONHASH(sessionid);
6476 NFSLOCKSESSION(shp);
6477 sep = nfsrv_findsession(sessionid);
6479 clp = sep->sess_clp;
6480 if (*foreaftp == NFSCDFC4_BACK ||
6481 *foreaftp == NFSCDFC4_BACK_OR_BOTH ||
6482 *foreaftp == NFSCDFC4_FORE_OR_BOTH) {
6483 /* Try to set up a backchannel. */
6484 if (clp->lc_req.nr_client == NULL) {
6485 NFSD_DEBUG(2, "nfsrv_bindconnsess: acquire "
6487 clp->lc_req.nr_client = (struct __rpc_client *)
6488 clnt_bck_create(nd->nd_xprt->xp_socket,
6489 sep->sess_cbprogram, NFSV4_CBVERS);
6491 if (clp->lc_req.nr_client != NULL) {
6492 NFSD_DEBUG(2, "nfsrv_bindconnsess: set up "
6494 savxprt = sep->sess_cbsess.nfsess_xprt;
6495 SVC_ACQUIRE(nd->nd_xprt);
6496 CLNT_ACQUIRE(clp->lc_req.nr_client);
6497 nd->nd_xprt->xp_p2 = clp->lc_req.nr_client;
6498 /* Disable idle timeout. */
6499 nd->nd_xprt->xp_idletimeout = 0;
6500 sep->sess_cbsess.nfsess_xprt = nd->nd_xprt;
6501 sep->sess_crflags |= NFSV4CRSESS_CONNBACKCHAN;
6502 clp->lc_flags |= LCL_DONEBINDCONN |
6504 clp->lc_flags &= ~LCL_CBDOWN;
6505 if (*foreaftp == NFSCDFS4_BACK)
6506 *foreaftp = NFSCDFS4_BACK;
6508 *foreaftp = NFSCDFS4_BOTH;
6509 } else if (*foreaftp != NFSCDFC4_BACK) {
6510 NFSD_DEBUG(2, "nfsrv_bindconnsess: can't set "
6511 "up backchannel\n");
6512 sep->sess_crflags &= ~NFSV4CRSESS_CONNBACKCHAN;
6513 clp->lc_flags |= LCL_DONEBINDCONN;
6514 *foreaftp = NFSCDFS4_FORE;
6516 error = NFSERR_NOTSUPP;
6517 printf("nfsrv_bindconnsess: Can't add "
6521 NFSD_DEBUG(2, "nfsrv_bindconnsess: Set forechannel\n");
6522 clp->lc_flags |= LCL_DONEBINDCONN;
6523 *foreaftp = NFSCDFS4_FORE;
6526 error = NFSERR_BADSESSION;
6527 NFSUNLOCKSESSION(shp);
6529 if (savxprt != NULL)
6530 SVC_RELEASE(savxprt);
6535 * Free up a session structure.
6538 nfsrv_freesession(struct nfsdsession *sep, uint8_t *sessionid)
6540 struct nfssessionhash *shp;
6545 shp = NFSSESSIONHASH(sessionid);
6546 NFSLOCKSESSION(shp);
6547 sep = nfsrv_findsession(sessionid);
6549 shp = NFSSESSIONHASH(sep->sess_sessionid);
6550 NFSLOCKSESSION(shp);
6554 if (sep->sess_refcnt > 0) {
6555 NFSUNLOCKSESSION(shp);
6557 return (NFSERR_BACKCHANBUSY);
6559 LIST_REMOVE(sep, sess_hash);
6560 LIST_REMOVE(sep, sess_list);
6562 NFSUNLOCKSESSION(shp);
6565 return (NFSERR_BADSESSION);
6566 for (i = 0; i < NFSV4_SLOTS; i++)
6567 if (sep->sess_slots[i].nfssl_reply != NULL)
6568 m_freem(sep->sess_slots[i].nfssl_reply);
6569 if (sep->sess_cbsess.nfsess_xprt != NULL)
6570 SVC_RELEASE(sep->sess_cbsess.nfsess_xprt);
6571 free(sep, M_NFSDSESSION);
6577 * RFC5661 says that it should fail when there are associated opens, locks
6578 * or delegations. Since stateids represent opens, I don't see how you can
6579 * free an open stateid (it will be free'd when closed), so this function
6580 * only works for lock stateids (freeing the lock_owner) or delegations.
6583 nfsrv_freestateid(struct nfsrv_descript *nd, nfsv4stateid_t *stateidp,
6586 struct nfsclient *clp;
6587 struct nfsstate *stp;
6592 * Look up the stateid
6594 error = nfsrv_getclient((nfsquad_t)((u_quad_t)0), CLOPS_RENEW, &clp,
6595 NULL, (nfsquad_t)((u_quad_t)0), 0, nd, p);
6597 /* First, check for a delegation. */
6598 LIST_FOREACH(stp, &clp->lc_deleg, ls_list) {
6599 if (!NFSBCMP(stp->ls_stateid.other, stateidp->other,
6604 nfsrv_freedeleg(stp);
6609 /* Not a delegation, try for a lock_owner. */
6611 error = nfsrv_getstate(clp, stateidp, 0, &stp);
6612 if (error == 0 && ((stp->ls_flags & (NFSLCK_OPEN | NFSLCK_DELEGREAD |
6613 NFSLCK_DELEGWRITE)) != 0 || (stp->ls_flags & NFSLCK_LOCK) == 0))
6614 /* Not a lock_owner stateid. */
6615 error = NFSERR_LOCKSHELD;
6616 if (error == 0 && !LIST_EMPTY(&stp->ls_lock))
6617 error = NFSERR_LOCKSHELD;
6619 nfsrv_freelockowner(stp, NULL, 0, p);
6628 nfsrv_teststateid(struct nfsrv_descript *nd, nfsv4stateid_t *stateidp,
6631 struct nfsclient *clp;
6632 struct nfsstate *stp;
6637 * Look up the stateid
6639 error = nfsrv_getclient((nfsquad_t)((u_quad_t)0), CLOPS_RENEW, &clp,
6640 NULL, (nfsquad_t)((u_quad_t)0), 0, nd, p);
6642 error = nfsrv_getstate(clp, stateidp, 0, &stp);
6643 if (error == 0 && stateidp->seqid != 0 &&
6644 SEQ_LT(stateidp->seqid, stp->ls_stateid.seqid))
6645 error = NFSERR_OLDSTATEID;
6651 * Generate the xdr for an NFSv4.1 CBSequence Operation.
6654 nfsv4_setcbsequence(struct nfsrv_descript *nd, struct nfsclient *clp,
6655 int dont_replycache, struct nfsdsession **sepp, int *slotposp)
6657 struct nfsdsession *sep;
6658 uint32_t *tl, slotseq = 0;
6660 uint8_t sessionid[NFSX_V4SESSIONID];
6663 error = nfsv4_getcbsession(clp, sepp);
6667 nfsv4_sequencelookup(NULL, &sep->sess_cbsess, slotposp, &maxslot,
6668 &slotseq, sessionid, true);
6669 KASSERT(maxslot >= 0, ("nfsv4_setcbsequence neg maxslot"));
6671 /* Build the Sequence arguments. */
6672 NFSM_BUILD(tl, uint32_t *, NFSX_V4SESSIONID + 5 * NFSX_UNSIGNED);
6673 bcopy(sessionid, tl, NFSX_V4SESSIONID);
6674 tl += NFSX_V4SESSIONID / NFSX_UNSIGNED;
6675 nd->nd_slotseq = tl;
6676 nd->nd_slotid = *slotposp;
6677 nd->nd_flag |= ND_HASSLOTID;
6678 *tl++ = txdr_unsigned(slotseq);
6679 *tl++ = txdr_unsigned(*slotposp);
6680 *tl++ = txdr_unsigned(maxslot);
6681 if (dont_replycache == 0)
6682 *tl++ = newnfs_true;
6684 *tl++ = newnfs_false;
6685 *tl = 0; /* No referring call list, for now. */
6686 nd->nd_flag |= ND_HASSEQUENCE;
6691 * Get a session for the callback.
6694 nfsv4_getcbsession(struct nfsclient *clp, struct nfsdsession **sepp)
6696 struct nfsdsession *sep;
6699 LIST_FOREACH(sep, &clp->lc_session, sess_list) {
6700 if ((sep->sess_crflags & NFSV4CRSESS_CONNBACKCHAN) != 0)
6705 return (NFSERR_BADSESSION);
6714 * Free up all backchannel xprts. This needs to be done when the nfsd threads
6715 * exit, since those transports will all be going away.
6716 * This is only called after all the nfsd threads are done performing RPCs,
6717 * so locking shouldn't be an issue.
6720 nfsrv_freeallbackchannel_xprts(void)
6722 struct nfsdsession *sep;
6723 struct nfsclient *clp;
6727 for (i = 0; i < nfsrv_clienthashsize; i++) {
6728 LIST_FOREACH(clp, &nfsclienthash[i], lc_hash) {
6729 LIST_FOREACH(sep, &clp->lc_session, sess_list) {
6730 xprt = sep->sess_cbsess.nfsess_xprt;
6731 sep->sess_cbsess.nfsess_xprt = NULL;
6740 * Do a layout commit. Actually just call nfsrv_updatemdsattr().
6741 * I have no idea if the rest of these arguments will ever be useful?
6744 nfsrv_layoutcommit(struct nfsrv_descript *nd, vnode_t vp, int layouttype,
6745 int hasnewoff, uint64_t newoff, uint64_t offset, uint64_t len,
6746 int hasnewmtime, struct timespec *newmtimep, int reclaim,
6747 nfsv4stateid_t *stateidp, int maxcnt, char *layp, int *hasnewsizep,
6748 uint64_t *newsizep, struct ucred *cred, NFSPROC_T *p)
6753 error = nfsrv_updatemdsattr(vp, &na, p);
6756 *newsizep = na.na_size;
6762 * Try and get a layout.
6765 nfsrv_layoutget(struct nfsrv_descript *nd, vnode_t vp, struct nfsexstuff *exp,
6766 int layouttype, int *iomode, uint64_t *offset, uint64_t *len,
6767 uint64_t minlen, nfsv4stateid_t *stateidp, int maxcnt, int *retonclose,
6768 int *layoutlenp, char *layp, struct ucred *cred, NFSPROC_T *p)
6770 struct nfslayouthash *lhyp;
6771 struct nfslayout *lyp;
6773 fhandle_t fh, *dsfhp;
6774 int error, mirrorcnt;
6776 if (nfsrv_devidcnt == 0)
6777 return (NFSERR_UNKNLAYOUTTYPE);
6780 printf("nfsrv_layoutget: off=%ju len=%ju\n", (uintmax_t)*offset,
6782 error = nfsvno_getfh(vp, &fh, p);
6783 NFSD_DEBUG(4, "layoutget getfh=%d\n", error);
6788 * For now, all layouts are for entire files.
6789 * Only issue Read/Write layouts if requested for a non-readonly fs.
6791 if (NFSVNO_EXRDONLY(exp)) {
6792 if (*iomode == NFSLAYOUTIOMODE_RW)
6793 return (NFSERR_LAYOUTTRYLATER);
6794 *iomode = NFSLAYOUTIOMODE_READ;
6796 if (*iomode != NFSLAYOUTIOMODE_RW)
6797 *iomode = NFSLAYOUTIOMODE_READ;
6800 * Check to see if a write layout can be issued for this file.
6801 * This is used during mirror recovery to avoid RW layouts being
6802 * issued for a file while it is being copied to the recovered
6805 if (*iomode == NFSLAYOUTIOMODE_RW && nfsrv_dontlayout(&fh) != 0)
6806 return (NFSERR_LAYOUTTRYLATER);
6812 /* First, see if a layout already exists and return if found. */
6813 lhyp = NFSLAYOUTHASH(&fh);
6814 NFSLOCKLAYOUT(lhyp);
6815 error = nfsrv_findlayout(&nd->nd_clientid, &fh, layouttype, p, &lyp);
6816 NFSD_DEBUG(4, "layoutget findlay=%d\n", error);
6818 * Not sure if the seqid must be the same, so I won't check it.
6820 if (error == 0 && (stateidp->other[0] != lyp->lay_stateid.other[0] ||
6821 stateidp->other[1] != lyp->lay_stateid.other[1] ||
6822 stateidp->other[2] != lyp->lay_stateid.other[2])) {
6823 if ((lyp->lay_flags & NFSLAY_CALLB) == 0) {
6824 NFSUNLOCKLAYOUT(lhyp);
6825 NFSD_DEBUG(1, "ret bad stateid\n");
6826 return (NFSERR_BADSTATEID);
6829 * I believe we get here because there is a race between
6830 * the client processing the CBLAYOUTRECALL and the layout
6831 * being deleted here on the server.
6832 * The client has now done a LayoutGet with a non-layout
6833 * stateid, as it would when there is no layout.
6834 * As such, free this layout and set error == NFSERR_BADSTATEID
6835 * so the code below will create a new layout structure as
6836 * would happen if no layout was found.
6837 * "lyp" will be set before being used below, but set it NULL
6840 nfsrv_freelayout(&lhyp->list, lyp);
6842 error = NFSERR_BADSTATEID;
6845 if (lyp->lay_layoutlen > maxcnt) {
6846 NFSUNLOCKLAYOUT(lhyp);
6847 NFSD_DEBUG(1, "ret layout too small\n");
6848 return (NFSERR_TOOSMALL);
6850 if (*iomode == NFSLAYOUTIOMODE_RW) {
6851 if ((lyp->lay_flags & NFSLAY_NOSPC) != 0) {
6852 NFSUNLOCKLAYOUT(lhyp);
6853 NFSD_DEBUG(1, "ret layout nospace\n");
6854 return (NFSERR_NOSPC);
6856 lyp->lay_flags |= NFSLAY_RW;
6858 lyp->lay_flags |= NFSLAY_READ;
6859 NFSBCOPY(lyp->lay_xdr, layp, lyp->lay_layoutlen);
6860 *layoutlenp = lyp->lay_layoutlen;
6861 if (++lyp->lay_stateid.seqid == 0)
6862 lyp->lay_stateid.seqid = 1;
6863 stateidp->seqid = lyp->lay_stateid.seqid;
6864 NFSUNLOCKLAYOUT(lhyp);
6865 NFSD_DEBUG(4, "ret fnd layout\n");
6868 NFSUNLOCKLAYOUT(lhyp);
6870 /* Find the device id and file handle. */
6871 dsfhp = malloc(sizeof(fhandle_t) * NFSDEV_MAXMIRRORS, M_TEMP, M_WAITOK);
6872 devid = malloc(NFSX_V4DEVICEID * NFSDEV_MAXMIRRORS, M_TEMP, M_WAITOK);
6873 error = nfsrv_dsgetdevandfh(vp, p, &mirrorcnt, dsfhp, devid);
6874 NFSD_DEBUG(4, "layoutget devandfh=%d\n", error);
6876 if (layouttype == NFSLAYOUT_NFSV4_1_FILES) {
6877 if (NFSX_V4FILELAYOUT > maxcnt)
6878 error = NFSERR_TOOSMALL;
6880 lyp = nfsrv_filelayout(nd, *iomode, &fh, dsfhp,
6881 devid, vp->v_mount->mnt_stat.f_fsid);
6883 if (NFSX_V4FLEXLAYOUT(mirrorcnt) > maxcnt)
6884 error = NFSERR_TOOSMALL;
6886 lyp = nfsrv_flexlayout(nd, *iomode, mirrorcnt,
6888 vp->v_mount->mnt_stat.f_fsid);
6891 free(dsfhp, M_TEMP);
6892 free(devid, M_TEMP);
6897 * Now, add this layout to the list.
6899 error = nfsrv_addlayout(nd, &lyp, stateidp, layp, layoutlenp, p);
6900 NFSD_DEBUG(4, "layoutget addl=%d\n", error);
6902 * The lyp will be set to NULL by nfsrv_addlayout() if it
6903 * linked the new structure into the lists.
6905 free(lyp, M_NFSDSTATE);
6910 * Generate a File Layout.
6912 static struct nfslayout *
6913 nfsrv_filelayout(struct nfsrv_descript *nd, int iomode, fhandle_t *fhp,
6914 fhandle_t *dsfhp, char *devid, fsid_t fs)
6917 struct nfslayout *lyp;
6918 uint64_t pattern_offset;
6920 lyp = malloc(sizeof(struct nfslayout) + NFSX_V4FILELAYOUT, M_NFSDSTATE,
6922 lyp->lay_type = NFSLAYOUT_NFSV4_1_FILES;
6923 if (iomode == NFSLAYOUTIOMODE_RW)
6924 lyp->lay_flags = NFSLAY_RW;
6926 lyp->lay_flags = NFSLAY_READ;
6927 NFSBCOPY(fhp, &lyp->lay_fh, sizeof(*fhp));
6928 lyp->lay_clientid.qval = nd->nd_clientid.qval;
6930 NFSBCOPY(devid, lyp->lay_deviceid, NFSX_V4DEVICEID);
6932 /* Fill in the xdr for the files layout. */
6933 tl = (uint32_t *)lyp->lay_xdr;
6934 NFSBCOPY(devid, tl, NFSX_V4DEVICEID); /* Device ID. */
6935 tl += (NFSX_V4DEVICEID / NFSX_UNSIGNED);
6937 /* Set the stripe size to the maximum I/O size. */
6938 *tl++ = txdr_unsigned(nfs_srvmaxio & NFSFLAYUTIL_STRIPE_MASK);
6939 *tl++ = 0; /* 1st stripe index. */
6941 txdr_hyper(pattern_offset, tl); tl += 2; /* Pattern offset. */
6942 *tl++ = txdr_unsigned(1); /* 1 file handle. */
6943 *tl++ = txdr_unsigned(NFSX_V4PNFSFH);
6944 NFSBCOPY(dsfhp, tl, sizeof(*dsfhp));
6945 lyp->lay_layoutlen = NFSX_V4FILELAYOUT;
6949 #define FLEX_OWNERID "999"
6950 #define FLEX_UID0 "0"
6952 * Generate a Flex File Layout.
6953 * The FLEX_OWNERID can be any string of 3 decimal digits. Although this
6954 * string goes on the wire, it isn't supposed to be used by the client,
6955 * since this server uses tight coupling.
6956 * Although not recommended by the spec., if vfs.nfsd.flexlinuxhack=1 use
6957 * a string of "0". This works around the Linux Flex File Layout driver bug
6958 * which uses the synthetic uid/gid strings for the "tightly coupled" case.
6960 static struct nfslayout *
6961 nfsrv_flexlayout(struct nfsrv_descript *nd, int iomode, int mirrorcnt,
6962 fhandle_t *fhp, fhandle_t *dsfhp, char *devid, fsid_t fs)
6965 struct nfslayout *lyp;
6969 lyp = malloc(sizeof(struct nfslayout) + NFSX_V4FLEXLAYOUT(mirrorcnt),
6970 M_NFSDSTATE, M_WAITOK | M_ZERO);
6971 lyp->lay_type = NFSLAYOUT_FLEXFILE;
6972 if (iomode == NFSLAYOUTIOMODE_RW)
6973 lyp->lay_flags = NFSLAY_RW;
6975 lyp->lay_flags = NFSLAY_READ;
6976 NFSBCOPY(fhp, &lyp->lay_fh, sizeof(*fhp));
6977 lyp->lay_clientid.qval = nd->nd_clientid.qval;
6979 lyp->lay_mirrorcnt = mirrorcnt;
6980 NFSBCOPY(devid, lyp->lay_deviceid, NFSX_V4DEVICEID);
6982 /* Fill in the xdr for the files layout. */
6983 tl = (uint32_t *)lyp->lay_xdr;
6985 txdr_hyper(lenval, tl); tl += 2; /* Stripe unit. */
6986 *tl++ = txdr_unsigned(mirrorcnt); /* # of mirrors. */
6987 for (i = 0; i < mirrorcnt; i++) {
6988 *tl++ = txdr_unsigned(1); /* One stripe. */
6989 NFSBCOPY(devid, tl, NFSX_V4DEVICEID); /* Device ID. */
6990 tl += (NFSX_V4DEVICEID / NFSX_UNSIGNED);
6991 devid += NFSX_V4DEVICEID;
6992 *tl++ = txdr_unsigned(1); /* Efficiency. */
6993 *tl++ = 0; /* Proxy Stateid. */
6997 *tl++ = txdr_unsigned(1); /* 1 file handle. */
6998 *tl++ = txdr_unsigned(NFSX_V4PNFSFH);
6999 NFSBCOPY(dsfhp, tl, sizeof(*dsfhp));
7000 tl += (NFSM_RNDUP(NFSX_V4PNFSFH) / NFSX_UNSIGNED);
7002 if (nfsrv_flexlinuxhack != 0) {
7003 *tl++ = txdr_unsigned(strlen(FLEX_UID0));
7004 *tl = 0; /* 0 pad string. */
7005 NFSBCOPY(FLEX_UID0, tl++, strlen(FLEX_UID0));
7006 *tl++ = txdr_unsigned(strlen(FLEX_UID0));
7007 *tl = 0; /* 0 pad string. */
7008 NFSBCOPY(FLEX_UID0, tl++, strlen(FLEX_UID0));
7010 *tl++ = txdr_unsigned(strlen(FLEX_OWNERID));
7011 NFSBCOPY(FLEX_OWNERID, tl++, NFSX_UNSIGNED);
7012 *tl++ = txdr_unsigned(strlen(FLEX_OWNERID));
7013 NFSBCOPY(FLEX_OWNERID, tl++, NFSX_UNSIGNED);
7016 *tl++ = txdr_unsigned(0); /* ff_flags. */
7017 *tl = txdr_unsigned(60); /* Status interval hint. */
7018 lyp->lay_layoutlen = NFSX_V4FLEXLAYOUT(mirrorcnt);
7023 * Parse and process Flex File errors returned via LayoutReturn.
7026 nfsrv_flexlayouterr(struct nfsrv_descript *nd, uint32_t *layp, int maxcnt,
7030 int cnt, errcnt, i, j, opnum, stat;
7031 char devid[NFSX_V4DEVICEID];
7034 maxcnt -= NFSX_UNSIGNED;
7036 cnt = fxdr_unsigned(int, *tl++);
7039 NFSD_DEBUG(4, "flexlayouterr cnt=%d\n", cnt);
7040 for (i = 0; i < cnt; i++) {
7041 maxcnt -= NFSX_STATEID + 2 * NFSX_HYPER +
7045 /* Skip offset, length and stateid for now. */
7046 tl += (4 + NFSX_STATEID / NFSX_UNSIGNED);
7047 errcnt = fxdr_unsigned(int, *tl++);
7048 NFSD_DEBUG(4, "flexlayouterr errcnt=%d\n", errcnt);
7049 for (j = 0; j < errcnt; j++) {
7050 maxcnt -= NFSX_V4DEVICEID + 2 * NFSX_UNSIGNED;
7053 NFSBCOPY(tl, devid, NFSX_V4DEVICEID);
7054 tl += (NFSX_V4DEVICEID / NFSX_UNSIGNED);
7055 stat = fxdr_unsigned(int, *tl++);
7056 opnum = fxdr_unsigned(int, *tl++);
7057 NFSD_DEBUG(4, "flexlayouterr op=%d stat=%d\n", opnum,
7060 * Except for NFSERR_ACCES, NFSERR_STALE and
7061 * NFSERR_NOSPC errors, disable the mirror.
7063 if (stat != NFSERR_ACCES && stat != NFSERR_STALE &&
7064 stat != NFSERR_NOSPC)
7065 nfsrv_delds(devid, p);
7067 /* For NFSERR_NOSPC, mark all devids and layouts. */
7068 if (stat == NFSERR_NOSPC)
7069 nfsrv_marknospc(devid, true);
7075 * This function removes all flex file layouts which has a mirror with
7076 * a device id that matches the argument.
7077 * Called when the DS represented by the device id has failed.
7080 nfsrv_flexmirrordel(char *devid, NFSPROC_T *p)
7083 struct nfslayout *lyp, *nlyp;
7084 struct nfslayouthash *lhyp;
7085 struct nfslayouthead loclyp;
7088 NFSD_DEBUG(4, "flexmirrordel\n");
7089 /* Move all layouts found onto a local list. */
7090 TAILQ_INIT(&loclyp);
7091 for (i = 0; i < nfsrv_layouthashsize; i++) {
7092 lhyp = &nfslayouthash[i];
7093 NFSLOCKLAYOUT(lhyp);
7094 TAILQ_FOREACH_SAFE(lyp, &lhyp->list, lay_list, nlyp) {
7095 if (lyp->lay_type == NFSLAYOUT_FLEXFILE &&
7096 lyp->lay_mirrorcnt > 1) {
7097 NFSD_DEBUG(4, "possible match\n");
7100 for (j = 0; j < lyp->lay_mirrorcnt; j++) {
7102 if (NFSBCMP(devid, tl, NFSX_V4DEVICEID)
7105 NFSD_DEBUG(4, "fnd one\n");
7106 TAILQ_REMOVE(&lhyp->list, lyp,
7108 TAILQ_INSERT_HEAD(&loclyp, lyp,
7112 tl += (NFSX_V4DEVICEID / NFSX_UNSIGNED +
7113 NFSM_RNDUP(NFSX_V4PNFSFH) /
7114 NFSX_UNSIGNED + 11 * NFSX_UNSIGNED);
7118 NFSUNLOCKLAYOUT(lhyp);
7121 /* Now, try to do a Layout recall for each one found. */
7122 TAILQ_FOREACH_SAFE(lyp, &loclyp, lay_list, nlyp) {
7123 NFSD_DEBUG(4, "do layout recall\n");
7125 * The layout stateid.seqid needs to be incremented
7126 * before doing a LAYOUT_RECALL callback.
7128 if (++lyp->lay_stateid.seqid == 0)
7129 lyp->lay_stateid.seqid = 1;
7130 nfsrv_recalllayout(lyp->lay_clientid, &lyp->lay_stateid,
7131 &lyp->lay_fh, lyp, 1, lyp->lay_type, p);
7132 nfsrv_freelayout(&loclyp, lyp);
7137 * Do a recall callback to the client for this layout.
7140 nfsrv_recalllayout(nfsquad_t clid, nfsv4stateid_t *stateidp, fhandle_t *fhp,
7141 struct nfslayout *lyp, int changed, int laytype, NFSPROC_T *p)
7143 struct nfsclient *clp;
7146 NFSD_DEBUG(4, "nfsrv_recalllayout\n");
7147 error = nfsrv_getclient(clid, 0, &clp, NULL, (nfsquad_t)((u_quad_t)0),
7149 NFSD_DEBUG(4, "aft nfsrv_getclient=%d\n", error);
7151 printf("nfsrv_recalllayout: getclient err=%d\n", error);
7154 if ((clp->lc_flags & LCL_NFSV41) != 0) {
7155 error = nfsrv_docallback(clp, NFSV4OP_CBLAYOUTRECALL,
7156 stateidp, changed, fhp, NULL, NULL, laytype, p);
7157 /* If lyp != NULL, handle an error return here. */
7158 if (error != 0 && lyp != NULL) {
7161 * Mark it returned, since no layout recall
7163 * All errors seem to be non-recoverable, although
7164 * NFSERR_NOMATCHLAYOUT is a normal event.
7166 if ((lyp->lay_flags & NFSLAY_RECALL) != 0) {
7167 lyp->lay_flags |= NFSLAY_RETURNED;
7171 if (error != NFSERR_NOMATCHLAYOUT)
7172 printf("nfsrv_recalllayout: err=%d\n", error);
7175 printf("nfsrv_recalllayout: clp not NFSv4.1\n");
7180 * Find a layout to recall when we exceed our high water mark.
7183 nfsrv_recalloldlayout(NFSPROC_T *p)
7185 struct nfslayouthash *lhyp;
7186 struct nfslayout *lyp;
7188 nfsv4stateid_t stateid;
7190 int error, laytype = 0, ret;
7192 lhyp = &nfslayouthash[arc4random() % nfsrv_layouthashsize];
7193 NFSLOCKLAYOUT(lhyp);
7194 TAILQ_FOREACH_REVERSE(lyp, &lhyp->list, nfslayouthead, lay_list) {
7195 if ((lyp->lay_flags & NFSLAY_CALLB) == 0) {
7196 lyp->lay_flags |= NFSLAY_CALLB;
7198 * The layout stateid.seqid needs to be incremented
7199 * before doing a LAYOUT_RECALL callback.
7201 if (++lyp->lay_stateid.seqid == 0)
7202 lyp->lay_stateid.seqid = 1;
7203 clientid = lyp->lay_clientid;
7204 stateid = lyp->lay_stateid;
7205 NFSBCOPY(&lyp->lay_fh, &fh, sizeof(fh));
7206 laytype = lyp->lay_type;
7210 NFSUNLOCKLAYOUT(lhyp);
7212 error = nfsrv_recalllayout(clientid, &stateid, &fh, NULL, 0,
7214 if (error != 0 && error != NFSERR_NOMATCHLAYOUT)
7215 NFSD_DEBUG(4, "recallold=%d\n", error);
7217 NFSLOCKLAYOUT(lhyp);
7219 * Since the hash list was unlocked, we need to
7222 ret = nfsrv_findlayout(&clientid, &fh, laytype, p,
7225 (lyp->lay_flags & NFSLAY_CALLB) != 0 &&
7226 lyp->lay_stateid.other[0] == stateid.other[0] &&
7227 lyp->lay_stateid.other[1] == stateid.other[1] &&
7228 lyp->lay_stateid.other[2] == stateid.other[2]) {
7230 * The client no longer knows this layout, so
7231 * it can be free'd now.
7233 if (error == NFSERR_NOMATCHLAYOUT)
7234 nfsrv_freelayout(&lhyp->list, lyp);
7237 * Leave it to be tried later by
7238 * clearing NFSLAY_CALLB and moving
7239 * it to the head of the list, so it
7240 * won't be tried again for a while.
7242 lyp->lay_flags &= ~NFSLAY_CALLB;
7243 TAILQ_REMOVE(&lhyp->list, lyp,
7245 TAILQ_INSERT_HEAD(&lhyp->list, lyp,
7249 NFSUNLOCKLAYOUT(lhyp);
7255 * Try and return layout(s).
7258 nfsrv_layoutreturn(struct nfsrv_descript *nd, vnode_t vp,
7259 int layouttype, int iomode, uint64_t offset, uint64_t len, int reclaim,
7260 int kind, nfsv4stateid_t *stateidp, int maxcnt, uint32_t *layp, int *fndp,
7261 struct ucred *cred, NFSPROC_T *p)
7264 struct nfslayouthash *lhyp;
7265 struct nfslayout *lyp;
7270 if (kind == NFSV4LAYOUTRET_FILE) {
7271 error = nfsvno_getfh(vp, &fh, p);
7273 error = nfsrv_updatemdsattr(vp, &na, p);
7275 printf("nfsrv_layoutreturn: updatemdsattr"
7276 " failed=%d\n", error);
7279 if (reclaim == newnfs_true) {
7280 error = nfsrv_checkgrace(NULL, NULL,
7282 if (error != NFSERR_NOGRACE)
7286 lhyp = NFSLAYOUTHASH(&fh);
7288 NFSLOCKLAYOUT(lhyp);
7289 error = nfsrv_findlayout(&nd->nd_clientid, &fh,
7290 layouttype, p, &lyp);
7291 NFSD_DEBUG(4, "layoutret findlay=%d\n", error);
7293 stateidp->other[0] == lyp->lay_stateid.other[0] &&
7294 stateidp->other[1] == lyp->lay_stateid.other[1] &&
7295 stateidp->other[2] == lyp->lay_stateid.other[2]) {
7296 NFSD_DEBUG(4, "nfsrv_layoutreturn: stateid %d"
7297 " %x %x %x laystateid %d %x %x %x"
7298 " off=%ju len=%ju flgs=0x%x\n",
7299 stateidp->seqid, stateidp->other[0],
7300 stateidp->other[1], stateidp->other[2],
7301 lyp->lay_stateid.seqid,
7302 lyp->lay_stateid.other[0],
7303 lyp->lay_stateid.other[1],
7304 lyp->lay_stateid.other[2],
7305 (uintmax_t)offset, (uintmax_t)len,
7307 if (++lyp->lay_stateid.seqid == 0)
7308 lyp->lay_stateid.seqid = 1;
7309 stateidp->seqid = lyp->lay_stateid.seqid;
7310 if (offset == 0 && len == UINT64_MAX) {
7311 if ((iomode & NFSLAYOUTIOMODE_READ) !=
7313 lyp->lay_flags &= ~NFSLAY_READ;
7314 if ((iomode & NFSLAYOUTIOMODE_RW) != 0)
7315 lyp->lay_flags &= ~NFSLAY_RW;
7316 if ((lyp->lay_flags & (NFSLAY_READ |
7318 nfsrv_freelayout(&lhyp->list,
7325 NFSUNLOCKLAYOUT(lhyp);
7326 /* Search the nfsrv_recalllist for a match. */
7327 TAILQ_FOREACH(lyp, &nfsrv_recalllisthead, lay_list) {
7328 if (NFSBCMP(&lyp->lay_fh, &fh,
7330 lyp->lay_clientid.qval ==
7331 nd->nd_clientid.qval &&
7332 stateidp->other[0] ==
7333 lyp->lay_stateid.other[0] &&
7334 stateidp->other[1] ==
7335 lyp->lay_stateid.other[1] &&
7336 stateidp->other[2] ==
7337 lyp->lay_stateid.other[2]) {
7338 lyp->lay_flags |= NFSLAY_RETURNED;
7345 if (layouttype == NFSLAYOUT_FLEXFILE && layp != NULL)
7346 nfsrv_flexlayouterr(nd, layp, maxcnt, p);
7347 } else if (kind == NFSV4LAYOUTRET_FSID)
7348 nfsrv_freelayouts(&nd->nd_clientid,
7349 &vp->v_mount->mnt_stat.f_fsid, layouttype, iomode);
7350 else if (kind == NFSV4LAYOUTRET_ALL)
7351 nfsrv_freelayouts(&nd->nd_clientid, NULL, layouttype, iomode);
7353 error = NFSERR_INVAL;
7360 * Look for an existing layout.
7363 nfsrv_findlayout(nfsquad_t *clientidp, fhandle_t *fhp, int laytype,
7364 NFSPROC_T *p, struct nfslayout **lypp)
7366 struct nfslayouthash *lhyp;
7367 struct nfslayout *lyp;
7372 lhyp = NFSLAYOUTHASH(fhp);
7373 TAILQ_FOREACH(lyp, &lhyp->list, lay_list) {
7374 if (NFSBCMP(&lyp->lay_fh, fhp, sizeof(*fhp)) == 0 &&
7375 lyp->lay_clientid.qval == clientidp->qval &&
7376 lyp->lay_type == laytype)
7387 * Add the new layout, as required.
7390 nfsrv_addlayout(struct nfsrv_descript *nd, struct nfslayout **lypp,
7391 nfsv4stateid_t *stateidp, char *layp, int *layoutlenp, NFSPROC_T *p)
7393 struct nfsclient *clp;
7394 struct nfslayouthash *lhyp;
7395 struct nfslayout *lyp, *nlyp;
7399 KASSERT((nd->nd_flag & ND_IMPLIEDCLID) != 0,
7400 ("nfsrv_layoutget: no nd_clientid\n"));
7404 error = nfsrv_getclient((nfsquad_t)((u_quad_t)0), CLOPS_RENEW, &clp,
7405 NULL, (nfsquad_t)((u_quad_t)0), 0, nd, p);
7410 lyp->lay_stateid.seqid = stateidp->seqid = 1;
7411 lyp->lay_stateid.other[0] = stateidp->other[0] =
7412 clp->lc_clientid.lval[0];
7413 lyp->lay_stateid.other[1] = stateidp->other[1] =
7414 clp->lc_clientid.lval[1];
7415 lyp->lay_stateid.other[2] = stateidp->other[2] =
7416 nfsrv_nextstateindex(clp);
7419 lhyp = NFSLAYOUTHASH(fhp);
7420 NFSLOCKLAYOUT(lhyp);
7421 TAILQ_FOREACH(nlyp, &lhyp->list, lay_list) {
7422 if (NFSBCMP(&nlyp->lay_fh, fhp, sizeof(*fhp)) == 0 &&
7423 nlyp->lay_clientid.qval == nd->nd_clientid.qval)
7427 /* A layout already exists, so use it. */
7428 nlyp->lay_flags |= (lyp->lay_flags & (NFSLAY_READ | NFSLAY_RW));
7429 NFSBCOPY(nlyp->lay_xdr, layp, nlyp->lay_layoutlen);
7430 *layoutlenp = nlyp->lay_layoutlen;
7431 if (++nlyp->lay_stateid.seqid == 0)
7432 nlyp->lay_stateid.seqid = 1;
7433 stateidp->seqid = nlyp->lay_stateid.seqid;
7434 stateidp->other[0] = nlyp->lay_stateid.other[0];
7435 stateidp->other[1] = nlyp->lay_stateid.other[1];
7436 stateidp->other[2] = nlyp->lay_stateid.other[2];
7437 NFSUNLOCKLAYOUT(lhyp);
7441 /* Insert the new layout in the lists. */
7443 atomic_add_int(&nfsrv_layoutcnt, 1);
7444 nfsstatsv1.srvlayouts++;
7445 NFSBCOPY(lyp->lay_xdr, layp, lyp->lay_layoutlen);
7446 *layoutlenp = lyp->lay_layoutlen;
7447 TAILQ_INSERT_HEAD(&lhyp->list, lyp, lay_list);
7448 NFSUNLOCKLAYOUT(lhyp);
7453 * Get the devinfo for a deviceid.
7456 nfsrv_getdevinfo(char *devid, int layouttype, uint32_t *maxcnt,
7457 uint32_t *notify, int *devaddrlen, char **devaddr)
7459 struct nfsdevice *ds;
7461 if ((layouttype != NFSLAYOUT_NFSV4_1_FILES && layouttype !=
7462 NFSLAYOUT_FLEXFILE) ||
7463 (nfsrv_maxpnfsmirror > 1 && layouttype == NFSLAYOUT_NFSV4_1_FILES))
7464 return (NFSERR_UNKNLAYOUTTYPE);
7467 * Now, search for the device id. Note that the structures won't go
7468 * away, but the order changes in the list. As such, the lock only
7469 * needs to be held during the search through the list.
7472 TAILQ_FOREACH(ds, &nfsrv_devidhead, nfsdev_list) {
7473 if (NFSBCMP(devid, ds->nfsdev_deviceid, NFSX_V4DEVICEID) == 0 &&
7474 ds->nfsdev_nmp != NULL)
7479 return (NFSERR_NOENT);
7481 /* If the correct nfsdev_XXXXaddrlen is > 0, we have the device info. */
7483 if (layouttype == NFSLAYOUT_NFSV4_1_FILES) {
7484 *devaddrlen = ds->nfsdev_fileaddrlen;
7485 *devaddr = ds->nfsdev_fileaddr;
7486 } else if (layouttype == NFSLAYOUT_FLEXFILE) {
7487 *devaddrlen = ds->nfsdev_flexaddrlen;
7488 *devaddr = ds->nfsdev_flexaddr;
7490 if (*devaddrlen == 0)
7491 return (NFSERR_UNKNLAYOUTTYPE);
7494 * The XDR overhead is 3 unsigned values: layout_type,
7495 * length_of_address and notify bitmap.
7496 * If the notify array is changed to not all zeros, the
7497 * count of unsigned values must be increased.
7499 if (*maxcnt > 0 && *maxcnt < NFSM_RNDUP(*devaddrlen) +
7500 3 * NFSX_UNSIGNED) {
7501 *maxcnt = NFSM_RNDUP(*devaddrlen) + 3 * NFSX_UNSIGNED;
7502 return (NFSERR_TOOSMALL);
7508 * Free a list of layout state structures.
7511 nfsrv_freelayoutlist(nfsquad_t clientid)
7513 struct nfslayouthash *lhyp;
7514 struct nfslayout *lyp, *nlyp;
7517 for (i = 0; i < nfsrv_layouthashsize; i++) {
7518 lhyp = &nfslayouthash[i];
7519 NFSLOCKLAYOUT(lhyp);
7520 TAILQ_FOREACH_SAFE(lyp, &lhyp->list, lay_list, nlyp) {
7521 if (lyp->lay_clientid.qval == clientid.qval)
7522 nfsrv_freelayout(&lhyp->list, lyp);
7524 NFSUNLOCKLAYOUT(lhyp);
7532 nfsrv_freelayout(struct nfslayouthead *lhp, struct nfslayout *lyp)
7535 NFSD_DEBUG(4, "Freelayout=%p\n", lyp);
7536 atomic_add_int(&nfsrv_layoutcnt, -1);
7537 nfsstatsv1.srvlayouts--;
7538 TAILQ_REMOVE(lhp, lyp, lay_list);
7539 free(lyp, M_NFSDSTATE);
7543 * Free up a device id.
7546 nfsrv_freeonedevid(struct nfsdevice *ds)
7550 atomic_add_int(&nfsrv_devidcnt, -1);
7551 vrele(ds->nfsdev_dvp);
7552 for (i = 0; i < nfsrv_dsdirsize; i++)
7553 if (ds->nfsdev_dsdir[i] != NULL)
7554 vrele(ds->nfsdev_dsdir[i]);
7555 free(ds->nfsdev_fileaddr, M_NFSDSTATE);
7556 free(ds->nfsdev_flexaddr, M_NFSDSTATE);
7557 free(ds->nfsdev_host, M_NFSDSTATE);
7558 free(ds, M_NFSDSTATE);
7562 * Free up a device id and its mirrors.
7565 nfsrv_freedevid(struct nfsdevice *ds)
7568 TAILQ_REMOVE(&nfsrv_devidhead, ds, nfsdev_list);
7569 nfsrv_freeonedevid(ds);
7573 * Free all layouts and device ids.
7574 * Done when the nfsd threads are shut down since there may be a new
7575 * modified device id list created when the nfsd is restarted.
7578 nfsrv_freealllayoutsanddevids(void)
7580 struct nfsdontlist *mrp, *nmrp;
7581 struct nfslayout *lyp, *nlyp;
7583 /* Get rid of the deviceid structures. */
7584 nfsrv_freealldevids();
7585 TAILQ_INIT(&nfsrv_devidhead);
7588 /* Get rid of all layouts. */
7589 nfsrv_freealllayouts();
7591 /* Get rid of any nfsdontlist entries. */
7592 LIST_FOREACH_SAFE(mrp, &nfsrv_dontlisthead, nfsmr_list, nmrp)
7593 free(mrp, M_NFSDSTATE);
7594 LIST_INIT(&nfsrv_dontlisthead);
7595 nfsrv_dontlistlen = 0;
7597 /* Free layouts in the recall list. */
7598 TAILQ_FOREACH_SAFE(lyp, &nfsrv_recalllisthead, lay_list, nlyp)
7599 nfsrv_freelayout(&nfsrv_recalllisthead, lyp);
7600 TAILQ_INIT(&nfsrv_recalllisthead);
7604 * Free layouts that match the arguments.
7607 nfsrv_freelayouts(nfsquad_t *clid, fsid_t *fs, int laytype, int iomode)
7609 struct nfslayouthash *lhyp;
7610 struct nfslayout *lyp, *nlyp;
7613 for (i = 0; i < nfsrv_layouthashsize; i++) {
7614 lhyp = &nfslayouthash[i];
7615 NFSLOCKLAYOUT(lhyp);
7616 TAILQ_FOREACH_SAFE(lyp, &lhyp->list, lay_list, nlyp) {
7617 if (clid->qval != lyp->lay_clientid.qval)
7619 if (fs != NULL && fsidcmp(fs, &lyp->lay_fsid) != 0)
7621 if (laytype != lyp->lay_type)
7623 if ((iomode & NFSLAYOUTIOMODE_READ) != 0)
7624 lyp->lay_flags &= ~NFSLAY_READ;
7625 if ((iomode & NFSLAYOUTIOMODE_RW) != 0)
7626 lyp->lay_flags &= ~NFSLAY_RW;
7627 if ((lyp->lay_flags & (NFSLAY_READ | NFSLAY_RW)) == 0)
7628 nfsrv_freelayout(&lhyp->list, lyp);
7630 NFSUNLOCKLAYOUT(lhyp);
7635 * Free all layouts for the argument file.
7638 nfsrv_freefilelayouts(fhandle_t *fhp)
7640 struct nfslayouthash *lhyp;
7641 struct nfslayout *lyp, *nlyp;
7643 lhyp = NFSLAYOUTHASH(fhp);
7644 NFSLOCKLAYOUT(lhyp);
7645 TAILQ_FOREACH_SAFE(lyp, &lhyp->list, lay_list, nlyp) {
7646 if (NFSBCMP(&lyp->lay_fh, fhp, sizeof(*fhp)) == 0)
7647 nfsrv_freelayout(&lhyp->list, lyp);
7649 NFSUNLOCKLAYOUT(lhyp);
7656 nfsrv_freealllayouts(void)
7658 struct nfslayouthash *lhyp;
7659 struct nfslayout *lyp, *nlyp;
7662 for (i = 0; i < nfsrv_layouthashsize; i++) {
7663 lhyp = &nfslayouthash[i];
7664 NFSLOCKLAYOUT(lhyp);
7665 TAILQ_FOREACH_SAFE(lyp, &lhyp->list, lay_list, nlyp)
7666 nfsrv_freelayout(&lhyp->list, lyp);
7667 NFSUNLOCKLAYOUT(lhyp);
7672 * Look up the mount path for the DS server.
7675 nfsrv_setdsserver(char *dspathp, char *mdspathp, NFSPROC_T *p,
7676 struct nfsdevice **dsp)
7678 struct nameidata nd;
7679 struct nfsdevice *ds;
7685 NFSD_DEBUG(4, "setdssrv path=%s\n", dspathp);
7687 NDINIT(&nd, LOOKUP, FOLLOW | LOCKSHARED | LOCKLEAF, UIO_SYSSPACE,
7690 NFSD_DEBUG(4, "lookup=%d\n", error);
7693 if (nd.ni_vp->v_type != VDIR) {
7695 NFSD_DEBUG(4, "dspath not dir\n");
7698 if (strcmp(nd.ni_vp->v_mount->mnt_vfc->vfc_name, "nfs") != 0) {
7700 NFSD_DEBUG(4, "dspath not an NFS mount\n");
7705 * Allocate a DS server structure with the NFS mounted directory
7706 * vnode reference counted, so that a non-forced dismount will
7708 * This structure is always linked into the list, even if an error
7709 * is being returned. The caller will free the entire list upon
7712 *dsp = ds = malloc(sizeof(*ds) + nfsrv_dsdirsize * sizeof(vnode_t),
7713 M_NFSDSTATE, M_WAITOK | M_ZERO);
7714 ds->nfsdev_dvp = nd.ni_vp;
7715 ds->nfsdev_nmp = VFSTONFS(nd.ni_vp->v_mount);
7716 NFSVOPUNLOCK(nd.ni_vp);
7718 dsdirsize = strlen(dspathp) + 16;
7719 dsdirpath = malloc(dsdirsize, M_TEMP, M_WAITOK);
7720 /* Now, create the DS directory structures. */
7721 for (i = 0; i < nfsrv_dsdirsize; i++) {
7722 snprintf(dsdirpath, dsdirsize, "%s/ds%d", dspathp, i);
7723 NDINIT(&nd, LOOKUP, FOLLOW | LOCKSHARED | LOCKLEAF,
7724 UIO_SYSSPACE, dsdirpath);
7726 NFSD_DEBUG(4, "dsdirpath=%s lookup=%d\n", dsdirpath, error);
7729 if (nd.ni_vp->v_type != VDIR) {
7732 NFSD_DEBUG(4, "dsdirpath not a VDIR\n");
7735 if (strcmp(nd.ni_vp->v_mount->mnt_vfc->vfc_name, "nfs") != 0) {
7738 NFSD_DEBUG(4, "dsdirpath not an NFS mount\n");
7741 ds->nfsdev_dsdir[i] = nd.ni_vp;
7742 NFSVOPUNLOCK(nd.ni_vp);
7744 free(dsdirpath, M_TEMP);
7746 if (strlen(mdspathp) > 0) {
7748 * This DS stores file for a specific MDS exported file
7751 NDINIT(&nd, LOOKUP, FOLLOW | LOCKSHARED | LOCKLEAF,
7752 UIO_SYSSPACE, mdspathp);
7754 NFSD_DEBUG(4, "mds lookup=%d\n", error);
7757 if (nd.ni_vp->v_type != VDIR) {
7760 NFSD_DEBUG(4, "mdspath not dir\n");
7763 mp = nd.ni_vp->v_mount;
7764 if ((mp->mnt_flag & MNT_EXPORTED) == 0) {
7767 NFSD_DEBUG(4, "mdspath not an exported fs\n");
7770 ds->nfsdev_mdsfsid = mp->mnt_stat.f_fsid;
7771 ds->nfsdev_mdsisset = 1;
7776 TAILQ_INSERT_TAIL(&nfsrv_devidhead, ds, nfsdev_list);
7777 atomic_add_int(&nfsrv_devidcnt, 1);
7782 * Look up the mount path for the DS server and delete it.
7785 nfsrv_deldsserver(int op, char *dspathp, NFSPROC_T *p)
7788 struct nfsmount *nmp;
7789 struct nfsdevice *ds;
7792 NFSD_DEBUG(4, "deldssrv path=%s\n", dspathp);
7794 * Search for the path in the mount list. Avoid looking the path
7795 * up, since this mount point may be hung, with associated locked
7797 * Set NFSMNTP_CANCELRPCS so that any forced dismount will be blocked
7798 * until this completes.
7799 * As noted in the man page, this should be done before any forced
7800 * dismount on the mount point, but at least the handshake on
7801 * NFSMNTP_CANCELRPCS should make it safe.
7806 mtx_lock(&mountlist_mtx);
7807 TAILQ_FOREACH(mp, &mountlist, mnt_list) {
7808 if (strcmp(mp->mnt_stat.f_mntonname, dspathp) == 0 &&
7809 strcmp(mp->mnt_stat.f_fstypename, "nfs") == 0 &&
7810 mp->mnt_data != NULL) {
7813 if ((nmp->nm_privflag & (NFSMNTP_FORCEDISM |
7814 NFSMNTP_CANCELRPCS)) == 0) {
7815 nmp->nm_privflag |= NFSMNTP_CANCELRPCS;
7824 mtx_unlock(&mountlist_mtx);
7827 ds = nfsrv_deldsnmp(op, nmp, p);
7828 NFSD_DEBUG(4, "deldsnmp=%p\n", ds);
7830 nfsrv_killrpcs(nmp);
7831 NFSD_DEBUG(4, "aft killrpcs\n");
7835 nmp->nm_privflag &= ~NFSMNTP_CANCELRPCS;
7844 * Search for and remove a DS entry which matches the "nmp" argument.
7845 * The nfsdevice structure pointer is returned so that the caller can
7846 * free it via nfsrv_freeonedevid().
7847 * For the forced case, do not try to do LayoutRecalls, since the server
7848 * must be shut down now anyhow.
7851 nfsrv_deldsnmp(int op, struct nfsmount *nmp, NFSPROC_T *p)
7853 struct nfsdevice *fndds;
7855 NFSD_DEBUG(4, "deldsdvp\n");
7857 if (op == PNFSDOP_FORCEDELDS)
7858 fndds = nfsv4_findmirror(nmp);
7860 fndds = nfsrv_findmirroredds(nmp);
7862 nfsrv_deleteds(fndds);
7864 if (fndds != NULL) {
7865 if (op != PNFSDOP_FORCEDELDS)
7866 nfsrv_flexmirrordel(fndds->nfsdev_deviceid, p);
7867 printf("pNFS server: mirror %s failed\n", fndds->nfsdev_host);
7873 * Similar to nfsrv_deldsnmp(), except that the DS is indicated by deviceid.
7874 * This function also calls nfsrv_killrpcs() to unblock RPCs on the mount
7876 * Also, returns an error instead of the nfsdevice found.
7879 nfsrv_delds(char *devid, NFSPROC_T *p)
7881 struct nfsdevice *ds, *fndds;
7882 struct nfsmount *nmp;
7885 NFSD_DEBUG(4, "delds\n");
7887 * Search the DS server list for a match with devid.
7888 * Remove the DS entry if found and there is a mirror.
7894 TAILQ_FOREACH(ds, &nfsrv_devidhead, nfsdev_list) {
7895 if (NFSBCMP(ds->nfsdev_deviceid, devid, NFSX_V4DEVICEID) == 0 &&
7896 ds->nfsdev_nmp != NULL) {
7897 NFSD_DEBUG(4, "fnd main ds\n");
7902 if (fndds == NULL) {
7906 if (fndds->nfsdev_mdsisset == 0 && nfsrv_faildscnt > 0)
7908 else if (fndds->nfsdev_mdsisset != 0) {
7909 /* For the fsid is set case, search for a mirror. */
7910 TAILQ_FOREACH(ds, &nfsrv_devidhead, nfsdev_list) {
7911 if (ds != fndds && ds->nfsdev_nmp != NULL &&
7912 ds->nfsdev_mdsisset != 0 &&
7913 fsidcmp(&ds->nfsdev_mdsfsid,
7914 &fndds->nfsdev_mdsfsid) == 0) {
7920 if (fndmirror != 0) {
7921 nmp = fndds->nfsdev_nmp;
7923 if ((nmp->nm_privflag & (NFSMNTP_FORCEDISM |
7924 NFSMNTP_CANCELRPCS)) == 0) {
7925 nmp->nm_privflag |= NFSMNTP_CANCELRPCS;
7927 nfsrv_deleteds(fndds);
7935 nfsrv_flexmirrordel(fndds->nfsdev_deviceid, p);
7936 printf("pNFS server: mirror %s failed\n", fndds->nfsdev_host);
7937 nfsrv_killrpcs(nmp);
7939 nmp->nm_privflag &= ~NFSMNTP_CANCELRPCS;
7948 * Mark a DS as disabled by setting nfsdev_nmp = NULL.
7951 nfsrv_deleteds(struct nfsdevice *fndds)
7954 NFSD_DEBUG(4, "deleteds: deleting a mirror\n");
7955 fndds->nfsdev_nmp = NULL;
7956 if (fndds->nfsdev_mdsisset == 0)
7961 * Fill in the addr structures for the File and Flex File layouts.
7964 nfsrv_allocdevid(struct nfsdevice *ds, char *addr, char *dnshost)
7969 static uint64_t new_devid = 0;
7971 if (strchr(addr, ':') != NULL)
7976 /* Fill in the device id. */
7977 NFSBCOPY(&nfsdev_time, ds->nfsdev_deviceid, sizeof(nfsdev_time));
7979 NFSBCOPY(&new_devid, &ds->nfsdev_deviceid[sizeof(nfsdev_time)],
7983 * Fill in the file addr (actually the nfsv4_file_layout_ds_addr4
7984 * as defined in RFC5661) in XDR.
7986 addrlen = NFSM_RNDUP(strlen(addr)) + NFSM_RNDUP(strlen(netprot)) +
7988 NFSD_DEBUG(4, "hn=%s addr=%s netprot=%s\n", dnshost, addr, netprot);
7989 ds->nfsdev_fileaddrlen = addrlen;
7990 tl = malloc(addrlen, M_NFSDSTATE, M_WAITOK | M_ZERO);
7991 ds->nfsdev_fileaddr = (char *)tl;
7992 *tl++ = txdr_unsigned(1); /* One stripe with index 0. */
7994 *tl++ = txdr_unsigned(1); /* One multipath list */
7995 *tl++ = txdr_unsigned(1); /* with one entry in it. */
7996 /* The netaddr for this one entry. */
7997 *tl++ = txdr_unsigned(strlen(netprot));
7998 NFSBCOPY(netprot, tl, strlen(netprot));
7999 tl += (NFSM_RNDUP(strlen(netprot)) / NFSX_UNSIGNED);
8000 *tl++ = txdr_unsigned(strlen(addr));
8001 NFSBCOPY(addr, tl, strlen(addr));
8004 * Fill in the flex file addr (actually the ff_device_addr4
8005 * as defined for Flexible File Layout) in XDR.
8007 addrlen = NFSM_RNDUP(strlen(addr)) + NFSM_RNDUP(strlen(netprot)) +
8009 ds->nfsdev_flexaddrlen = addrlen;
8010 tl = malloc(addrlen, M_NFSDSTATE, M_WAITOK | M_ZERO);
8011 ds->nfsdev_flexaddr = (char *)tl;
8012 *tl++ = txdr_unsigned(1); /* One multipath entry. */
8013 /* The netaddr for this one entry. */
8014 *tl++ = txdr_unsigned(strlen(netprot));
8015 NFSBCOPY(netprot, tl, strlen(netprot));
8016 tl += (NFSM_RNDUP(strlen(netprot)) / NFSX_UNSIGNED);
8017 *tl++ = txdr_unsigned(strlen(addr));
8018 NFSBCOPY(addr, tl, strlen(addr));
8019 tl += (NFSM_RNDUP(strlen(addr)) / NFSX_UNSIGNED);
8020 *tl++ = txdr_unsigned(2); /* Two NFS Versions. */
8021 *tl++ = txdr_unsigned(NFS_VER4); /* NFSv4. */
8022 *tl++ = txdr_unsigned(NFSV42_MINORVERSION); /* Minor version 2. */
8023 *tl++ = txdr_unsigned(nfs_srvmaxio); /* DS max rsize. */
8024 *tl++ = txdr_unsigned(nfs_srvmaxio); /* DS max wsize. */
8025 *tl++ = newnfs_true; /* Tightly coupled. */
8026 *tl++ = txdr_unsigned(NFS_VER4); /* NFSv4. */
8027 *tl++ = txdr_unsigned(NFSV41_MINORVERSION); /* Minor version 1. */
8028 *tl++ = txdr_unsigned(nfs_srvmaxio); /* DS max rsize. */
8029 *tl++ = txdr_unsigned(nfs_srvmaxio); /* DS max wsize. */
8030 *tl = newnfs_true; /* Tightly coupled. */
8032 ds->nfsdev_hostnamelen = strlen(dnshost);
8033 ds->nfsdev_host = malloc(ds->nfsdev_hostnamelen + 1, M_NFSDSTATE,
8035 NFSBCOPY(dnshost, ds->nfsdev_host, ds->nfsdev_hostnamelen + 1);
8039 * Create the device id list.
8040 * Return 0 if the nfsd threads are to run and ENXIO if the "-p" argument
8044 nfsrv_createdevids(struct nfsd_nfsd_args *args, NFSPROC_T *p)
8046 struct nfsdevice *ds;
8047 char *addrp, *dnshostp, *dspathp, *mdspathp;
8051 dnshostp = args->dnshost;
8052 dspathp = args->dspath;
8053 mdspathp = args->mdspath;
8054 nfsrv_maxpnfsmirror = args->mirrorcnt;
8055 if (addrp == NULL || dnshostp == NULL || dspathp == NULL ||
8060 * Loop around for each nul-terminated string in args->addr,
8061 * args->dnshost, args->dnspath and args->mdspath.
8063 while (addrp < (args->addr + args->addrlen) &&
8064 dnshostp < (args->dnshost + args->dnshostlen) &&
8065 dspathp < (args->dspath + args->dspathlen) &&
8066 mdspathp < (args->mdspath + args->mdspathlen)) {
8067 error = nfsrv_setdsserver(dspathp, mdspathp, p, &ds);
8069 /* Free all DS servers. */
8070 nfsrv_freealldevids();
8074 nfsrv_allocdevid(ds, addrp, dnshostp);
8075 addrp += (strlen(addrp) + 1);
8076 dnshostp += (strlen(dnshostp) + 1);
8077 dspathp += (strlen(dspathp) + 1);
8078 mdspathp += (strlen(mdspathp) + 1);
8080 if (nfsrv_devidcnt < nfsrv_maxpnfsmirror) {
8081 /* Free all DS servers. */
8082 nfsrv_freealldevids();
8084 nfsrv_maxpnfsmirror = 1;
8087 /* We can fail at most one less DS than the mirror level. */
8088 nfsrv_faildscnt = nfsrv_maxpnfsmirror - 1;
8091 * Allocate the nfslayout hash table now, since this is a pNFS server.
8092 * Make it 1% of the high water mark and at least 100.
8094 if (nfslayouthash == NULL) {
8095 nfsrv_layouthashsize = nfsrv_layouthighwater / 100;
8096 if (nfsrv_layouthashsize < 100)
8097 nfsrv_layouthashsize = 100;
8098 nfslayouthash = mallocarray(nfsrv_layouthashsize,
8099 sizeof(struct nfslayouthash), M_NFSDSESSION, M_WAITOK |
8101 for (i = 0; i < nfsrv_layouthashsize; i++) {
8102 mtx_init(&nfslayouthash[i].mtx, "nfslm", NULL, MTX_DEF);
8103 TAILQ_INIT(&nfslayouthash[i].list);
8110 * Free all device ids.
8113 nfsrv_freealldevids(void)
8115 struct nfsdevice *ds, *nds;
8117 TAILQ_FOREACH_SAFE(ds, &nfsrv_devidhead, nfsdev_list, nds)
8118 nfsrv_freedevid(ds);
8122 * Check to see if there is a Read/Write Layout plus either:
8123 * - A Write Delegation
8125 * - An Open with Write_access.
8126 * Return 1 if this is the case and 0 otherwise.
8127 * This function is used by nfsrv_proxyds() to decide if doing a Proxy
8128 * Getattr RPC to the Data Server (DS) is necessary.
8130 #define NFSCLIDVECSIZE 6
8132 nfsrv_checkdsattr(vnode_t vp, NFSPROC_T *p)
8134 fhandle_t fh, *tfhp;
8135 struct nfsstate *stp;
8136 struct nfslayout *lyp;
8137 struct nfslayouthash *lhyp;
8138 struct nfslockhashhead *hp;
8139 struct nfslockfile *lfp;
8140 nfsquad_t clid[NFSCLIDVECSIZE];
8143 ret = nfsvno_getfh(vp, &fh, p);
8147 /* First check for a Read/Write Layout. */
8149 lhyp = NFSLAYOUTHASH(&fh);
8150 NFSLOCKLAYOUT(lhyp);
8151 TAILQ_FOREACH(lyp, &lhyp->list, lay_list) {
8152 if (NFSBCMP(&lyp->lay_fh, &fh, sizeof(fh)) == 0 &&
8153 ((lyp->lay_flags & NFSLAY_RW) != 0 ||
8154 ((lyp->lay_flags & NFSLAY_READ) != 0 &&
8155 nfsrv_pnfsatime != 0))) {
8156 if (clidcnt < NFSCLIDVECSIZE)
8157 clid[clidcnt].qval = lyp->lay_clientid.qval;
8161 NFSUNLOCKLAYOUT(lhyp);
8163 /* None found, so return 0. */
8167 /* Get the nfslockfile for this fh. */
8169 hp = NFSLOCKHASH(&fh);
8170 LIST_FOREACH(lfp, hp, lf_hash) {
8172 if (NFSVNO_CMPFH(&fh, tfhp))
8176 /* None found, so return 0. */
8181 /* Now, look for a Write delegation for this clientid. */
8182 LIST_FOREACH(stp, &lfp->lf_deleg, ls_file) {
8183 if ((stp->ls_flags & NFSLCK_DELEGWRITE) != 0 &&
8184 nfsrv_fndclid(clid, stp->ls_clp->lc_clientid, clidcnt) != 0)
8188 /* Found one, so return 1. */
8193 /* No Write delegation, so look for an Open with Write_access. */
8194 LIST_FOREACH(stp, &lfp->lf_open, ls_file) {
8195 KASSERT((stp->ls_flags & NFSLCK_OPEN) != 0,
8196 ("nfsrv_checkdsattr: Non-open in Open list\n"));
8197 if ((stp->ls_flags & NFSLCK_WRITEACCESS) != 0 &&
8198 nfsrv_fndclid(clid, stp->ls_clp->lc_clientid, clidcnt) != 0)
8208 * Look for a matching clientid in the vector. Return 1 if one might match.
8211 nfsrv_fndclid(nfsquad_t *clidvec, nfsquad_t clid, int clidcnt)
8215 /* If too many for the vector, return 1 since there might be a match. */
8216 if (clidcnt > NFSCLIDVECSIZE)
8219 for (i = 0; i < clidcnt; i++)
8220 if (clidvec[i].qval == clid.qval)
8226 * Check the don't list for "vp" and see if issuing an rw layout is allowed.
8227 * Return 1 if issuing an rw layout isn't allowed, 0 otherwise.
8230 nfsrv_dontlayout(fhandle_t *fhp)
8232 struct nfsdontlist *mrp;
8235 if (nfsrv_dontlistlen == 0)
8239 LIST_FOREACH(mrp, &nfsrv_dontlisthead, nfsmr_list) {
8240 if (NFSBCMP(fhp, &mrp->nfsmr_fh, sizeof(*fhp)) == 0 &&
8241 (mrp->nfsmr_flags & NFSMR_DONTLAYOUT) != 0) {
8246 NFSDDONTLISTUNLOCK();
8250 #define PNFSDS_COPYSIZ 65536
8252 * Create a new file on a DS and copy the contents of an extant DS file to it.
8253 * This can be used for recovery of a DS file onto a recovered DS.
8255 * - When called, the MDS file's vnode is locked, blocking LayoutGet operations.
8256 * - Disable issuing of read/write layouts for the file via the nfsdontlist,
8257 * so that they will be disabled after the MDS file's vnode is unlocked.
8258 * - Set up the nfsrv_recalllist so that recall of read/write layouts can
8260 * - Unlock the MDS file's vnode, so that the client(s) can perform proxied
8261 * writes, LayoutCommits and LayoutReturns for the file when completing the
8262 * LayoutReturn requested by the LayoutRecall callback.
8263 * - Issue a LayoutRecall callback for all read/write layouts and wait for
8264 * them to be returned. (If the LayoutRecall callback replies
8265 * NFSERR_NOMATCHLAYOUT, they are gone and no LayoutReturn is needed.)
8266 * - Exclusively lock the MDS file's vnode. This ensures that no proxied
8267 * writes are in progress or can occur during the DS file copy.
8268 * It also blocks Setattr operations.
8269 * - Create the file on the recovered mirror.
8270 * - Copy the file from the operational DS.
8271 * - Copy any ACL from the MDS file to the new DS file.
8272 * - Set the modify time of the new DS file to that of the MDS file.
8273 * - Update the extended attribute for the MDS file.
8274 * - Enable issuing of rw layouts by deleting the nfsdontlist entry.
8275 * - The caller will unlock the MDS file's vnode allowing operations
8276 * to continue normally, since it is now on the mirror again.
8279 nfsrv_copymr(vnode_t vp, vnode_t fvp, vnode_t dvp, struct nfsdevice *ds,
8280 struct pnfsdsfile *pf, struct pnfsdsfile *wpf, int mirrorcnt,
8281 struct ucred *cred, NFSPROC_T *p)
8283 struct nfsdontlist *mrp, *nmrp;
8284 struct nfslayouthash *lhyp;
8285 struct nfslayout *lyp, *nlyp;
8286 struct nfslayouthead thl;
8287 struct mount *mp, *tvmp;
8290 struct timespec mtime;
8296 int didprintf, ret, retacl, xfer;
8298 ASSERT_VOP_LOCKED(fvp, "nfsrv_copymr fvp");
8299 ASSERT_VOP_LOCKED(vp, "nfsrv_copymr vp");
8301 * Allocate a nfsdontlist entry and set the NFSMR_DONTLAYOUT flag
8302 * so that no more RW layouts will get issued.
8304 ret = nfsvno_getfh(vp, &fh, p);
8306 NFSD_DEBUG(4, "nfsrv_copymr: getfh=%d\n", ret);
8309 nmrp = malloc(sizeof(*nmrp), M_NFSDSTATE, M_WAITOK);
8310 nmrp->nfsmr_flags = NFSMR_DONTLAYOUT;
8311 NFSBCOPY(&fh, &nmrp->nfsmr_fh, sizeof(fh));
8313 LIST_FOREACH(mrp, &nfsrv_dontlisthead, nfsmr_list) {
8314 if (NFSBCMP(&fh, &mrp->nfsmr_fh, sizeof(fh)) == 0)
8318 LIST_INSERT_HEAD(&nfsrv_dontlisthead, nmrp, nfsmr_list);
8321 nfsrv_dontlistlen++;
8322 NFSD_DEBUG(4, "nfsrv_copymr: in dontlist\n");
8324 NFSDDONTLISTUNLOCK();
8325 free(nmrp, M_NFSDSTATE);
8326 NFSD_DEBUG(4, "nfsrv_copymr: dup dontlist\n");
8329 NFSDDONTLISTUNLOCK();
8332 * Search for all RW layouts for this file. Move them to the
8333 * recall list, so they can be recalled and their return noted.
8335 lhyp = NFSLAYOUTHASH(&fh);
8337 NFSLOCKLAYOUT(lhyp);
8338 TAILQ_FOREACH_SAFE(lyp, &lhyp->list, lay_list, nlyp) {
8339 if (NFSBCMP(&lyp->lay_fh, &fh, sizeof(fh)) == 0 &&
8340 (lyp->lay_flags & NFSLAY_RW) != 0) {
8341 TAILQ_REMOVE(&lhyp->list, lyp, lay_list);
8342 TAILQ_INSERT_HEAD(&nfsrv_recalllisthead, lyp, lay_list);
8343 lyp->lay_trycnt = 0;
8346 NFSUNLOCKLAYOUT(lhyp);
8353 /* Unlock the MDS vp, so that a LayoutReturn can be done on it. */
8355 /* Now, do a recall for all layouts not yet recalled. */
8358 TAILQ_FOREACH(lyp, &nfsrv_recalllisthead, lay_list) {
8359 if (NFSBCMP(&lyp->lay_fh, &fh, sizeof(fh)) == 0 &&
8360 (lyp->lay_flags & NFSLAY_RECALL) == 0) {
8361 lyp->lay_flags |= NFSLAY_RECALL;
8363 * The layout stateid.seqid needs to be incremented
8364 * before doing a LAYOUT_RECALL callback.
8366 if (++lyp->lay_stateid.seqid == 0)
8367 lyp->lay_stateid.seqid = 1;
8369 nfsrv_recalllayout(lyp->lay_clientid, &lyp->lay_stateid,
8370 &lyp->lay_fh, lyp, 0, lyp->lay_type, p);
8371 NFSD_DEBUG(4, "nfsrv_copymr: recalled layout\n");
8376 /* Now wait for them to be returned. */
8378 TAILQ_FOREACH(lyp, &nfsrv_recalllisthead, lay_list) {
8379 if (NFSBCMP(&lyp->lay_fh, &fh, sizeof(fh)) == 0) {
8380 if ((lyp->lay_flags & NFSLAY_RETURNED) != 0) {
8381 TAILQ_REMOVE(&nfsrv_recalllisthead, lyp,
8383 TAILQ_INSERT_HEAD(&thl, lyp, lay_list);
8385 "nfsrv_copymr: layout returned\n");
8388 ret = mtx_sleep(lyp, NFSDRECALLMUTEXPTR,
8389 PVFS | PCATCH, "nfsmrl", hz);
8390 NFSD_DEBUG(4, "nfsrv_copymr: aft sleep=%d\n",
8392 if (ret == EINTR || ret == ERESTART)
8394 if ((lyp->lay_flags & NFSLAY_RETURNED) == 0) {
8396 * Give up after 60sec and return
8397 * ENXIO, failing the copymr.
8398 * This layout will remain on the
8399 * recalllist. It can only be cleared
8400 * by restarting the nfsd.
8401 * This seems the safe way to handle
8402 * it, since it cannot be safely copied
8403 * with an outstanding RW layout.
8405 if (lyp->lay_trycnt >= 60) {
8409 if (didprintf == 0) {
8410 printf("nfsrv_copymr: layout "
8420 /* We can now get rid of the layouts that have been returned. */
8421 TAILQ_FOREACH_SAFE(lyp, &thl, lay_list, nlyp)
8422 nfsrv_freelayout(&thl, lyp);
8425 * Do the vn_start_write() calls here, before the MDS vnode is
8426 * locked and the tvp is created (locked) in the NFS file system
8428 * For tvmp, this probably isn't necessary, since it will be an
8429 * NFS mount and they are not suspendable at this time.
8432 ret = vn_start_write(vp, &mp, V_WAIT | V_PCATCH);
8434 tvmp = dvp->v_mount;
8435 ret = vn_start_write(NULL, &tvmp, V_WAIT | V_PCATCH);
8439 * LK_EXCLUSIVE lock the MDS vnode, so that any
8440 * proxied writes through the MDS will be blocked until we have
8441 * completed the copy and update of the extended attributes.
8442 * This will also ensure that any attributes and ACL will not be
8443 * changed until the copy is complete.
8445 NFSVOPLOCK(vp, LK_EXCLUSIVE | LK_RETRY);
8446 if (ret == 0 && VN_IS_DOOMED(vp)) {
8447 NFSD_DEBUG(4, "nfsrv_copymr: lk_exclusive doomed\n");
8451 /* Create the data file on the recovered DS. */
8453 ret = nfsrv_createdsfile(vp, &fh, pf, dvp, ds, cred, p, &tvp);
8455 /* Copy the DS file, if created successfully. */
8458 * Get any NFSv4 ACL on the MDS file, so that it can be set
8459 * on the new DS file.
8461 aclp = acl_alloc(M_WAITOK | M_ZERO);
8462 retacl = VOP_GETACL(vp, ACL_TYPE_NFS4, aclp, cred, p);
8463 if (retacl != 0 && retacl != ENOATTR)
8464 NFSD_DEBUG(1, "nfsrv_copymr: vop_getacl=%d\n", retacl);
8465 dat = malloc(PNFSDS_COPYSIZ, M_TEMP, M_WAITOK);
8466 /* Malloc a block of 0s used to check for holes. */
8467 if (nfsrv_zeropnfsdat == NULL)
8468 nfsrv_zeropnfsdat = malloc(PNFSDS_COPYSIZ, M_TEMP,
8471 ret = VOP_GETATTR(fvp, &va, cred);
8473 while (ret == 0 && aresid == 0) {
8474 ret = vn_rdwr(UIO_READ, fvp, dat, PNFSDS_COPYSIZ,
8475 rdpos, UIO_SYSSPACE, IO_NODELOCKED, cred, NULL,
8477 xfer = PNFSDS_COPYSIZ - aresid;
8478 if (ret == 0 && xfer > 0) {
8481 * Skip the write for holes, except for the
8484 if (xfer < PNFSDS_COPYSIZ || rdpos ==
8485 va.va_size || NFSBCMP(dat,
8486 nfsrv_zeropnfsdat, PNFSDS_COPYSIZ) != 0)
8487 ret = vn_rdwr(UIO_WRITE, tvp, dat, xfer,
8488 wrpos, UIO_SYSSPACE, IO_NODELOCKED,
8489 cred, NULL, NULL, p);
8495 /* If there is an ACL and the copy succeeded, set the ACL. */
8496 if (ret == 0 && retacl == 0) {
8497 ret = VOP_SETACL(tvp, ACL_TYPE_NFS4, aclp, cred, p);
8499 * Don't consider these as errors, since VOP_GETACL()
8500 * can return an ACL when they are not actually
8501 * supported. For example, for UFS, VOP_GETACL()
8502 * will return a trivial ACL based on the uid/gid/mode
8503 * when there is no ACL on the file.
8504 * This case should be recognized as a trivial ACL
8505 * by UFS's VOP_SETACL() and succeed, but...
8507 if (ret == ENOATTR || ret == EOPNOTSUPP || ret == EPERM)
8512 ret = VOP_FSYNC(tvp, MNT_WAIT, p);
8514 /* Set the DS data file's modify time that of the MDS file. */
8516 ret = VOP_GETATTR(vp, &va, cred);
8518 mtime = va.va_mtime;
8520 va.va_mtime = mtime;
8521 ret = VOP_SETATTR(tvp, &va, cred);
8529 vn_finished_write(tvmp);
8531 /* Update the extended attributes for the newly created DS file. */
8533 ret = vn_extattr_set(vp, IO_NODELOCKED,
8534 EXTATTR_NAMESPACE_SYSTEM, "pnfsd.dsfile",
8535 sizeof(*wpf) * mirrorcnt, (char *)wpf, p);
8537 vn_finished_write(mp);
8539 /* Get rid of the dontlist entry, so that Layouts can be issued. */
8541 LIST_REMOVE(mrp, nfsmr_list);
8542 NFSDDONTLISTUNLOCK();
8543 free(mrp, M_NFSDSTATE);
8548 * Create a data storage file on the recovered DS.
8551 nfsrv_createdsfile(vnode_t vp, fhandle_t *fhp, struct pnfsdsfile *pf,
8552 vnode_t dvp, struct nfsdevice *ds, struct ucred *cred, NFSPROC_T *p,
8555 struct vattr va, nva;
8558 /* Make data file name based on FH. */
8559 error = VOP_GETATTR(vp, &va, cred);
8561 /* Set the attributes for "vp" to Setattr the DS vp. */
8563 nva.va_uid = va.va_uid;
8564 nva.va_gid = va.va_gid;
8565 nva.va_mode = va.va_mode;
8569 va.va_mode = nva.va_mode;
8570 NFSD_DEBUG(4, "nfsrv_dscreatefile: dvp=%p pf=%p\n", dvp, pf);
8571 error = nfsrv_dscreate(dvp, &va, &nva, fhp, pf, NULL,
8572 pf->dsf_filename, cred, p, tvpp);
8578 * Look up the MDS file shared locked, and then get the extended attribute
8579 * to find the extant DS file to be copied to the new mirror.
8580 * If successful, *vpp is set to the MDS file's vp and *nvpp is
8581 * set to a DS data file for the MDS file, both exclusively locked.
8582 * The "buf" argument has the pnfsdsfile structure from the MDS file
8583 * in it and buflen is set to its length.
8586 nfsrv_mdscopymr(char *mdspathp, char *dspathp, char *curdspathp, char *buf,
8587 int *buflenp, char *fname, NFSPROC_T *p, struct vnode **vpp,
8588 struct vnode **nvpp, struct pnfsdsfile **pfp, struct nfsdevice **dsp,
8589 struct nfsdevice **fdsp)
8591 struct nameidata nd;
8592 struct vnode *vp, *curvp;
8593 struct pnfsdsfile *pf;
8594 struct nfsmount *nmp, *curnmp;
8595 int dsdir, error, mirrorcnt, ippos;
8602 if (dspathp == NULL && curdspathp != NULL)
8606 * Look up the MDS file shared locked. The lock will be upgraded
8607 * to an exclusive lock after any rw layouts have been returned.
8609 NFSD_DEBUG(4, "mdsopen path=%s\n", mdspathp);
8610 NDINIT(&nd, LOOKUP, FOLLOW | LOCKSHARED | LOCKLEAF, UIO_SYSSPACE,
8613 NFSD_DEBUG(4, "lookup=%d\n", error);
8616 if (nd.ni_vp->v_type != VREG) {
8618 NFSD_DEBUG(4, "mdspath not reg\n");
8623 if (curdspathp != NULL) {
8625 * Look up the current DS path and find the nfsdev structure for
8628 NFSD_DEBUG(4, "curmdsdev path=%s\n", curdspathp);
8629 NDINIT(&nd, LOOKUP, FOLLOW | LOCKSHARED | LOCKLEAF,
8630 UIO_SYSSPACE, curdspathp);
8632 NFSD_DEBUG(4, "ds lookup=%d\n", error);
8637 if (nd.ni_vp->v_type != VDIR) {
8640 NFSD_DEBUG(4, "curdspath not dir\n");
8643 if (strcmp(nd.ni_vp->v_mount->mnt_vfc->vfc_name, "nfs") != 0) {
8646 NFSD_DEBUG(4, "curdspath not an NFS mount\n");
8649 curnmp = VFSTONFS(nd.ni_vp->v_mount);
8651 /* Search the nfsdev list for a match. */
8653 *fdsp = nfsv4_findmirror(curnmp);
8657 if (curnmp == NULL) {
8660 NFSD_DEBUG(4, "mdscopymr: no current ds\n");
8666 if (dspathp != NULL) {
8667 /* Look up the nfsdev path and find the nfsdev structure. */
8668 NFSD_DEBUG(4, "mdsdev path=%s\n", dspathp);
8669 NDINIT(&nd, LOOKUP, FOLLOW | LOCKSHARED | LOCKLEAF,
8670 UIO_SYSSPACE, dspathp);
8672 NFSD_DEBUG(4, "ds lookup=%d\n", error);
8679 if (nd.ni_vp->v_type != VDIR || nd.ni_vp == curvp) {
8684 NFSD_DEBUG(4, "dspath not dir\n");
8685 if (nd.ni_vp == curvp)
8689 if (strcmp(nd.ni_vp->v_mount->mnt_vfc->vfc_name, "nfs") != 0) {
8694 NFSD_DEBUG(4, "dspath not an NFS mount\n");
8697 nmp = VFSTONFS(nd.ni_vp->v_mount);
8700 * Search the nfsdevice list for a match. If curnmp == NULL,
8701 * this is a recovery and there must be a mirror.
8705 *dsp = nfsrv_findmirroredds(nmp);
8707 *dsp = nfsv4_findmirror(nmp);
8714 NFSD_DEBUG(4, "mdscopymr: no ds\n");
8723 * Get a vp for an available DS data file using the extended
8724 * attribute on the MDS file.
8725 * If there is a valid entry for the new DS in the extended attribute
8726 * on the MDS file (as checked via the nmp argument),
8727 * nfsrv_dsgetsockmnt() returns EEXIST, so no copying will occur.
8729 error = nfsrv_dsgetsockmnt(vp, 0, buf, buflenp, &mirrorcnt, p,
8730 NULL, NULL, NULL, fname, nvpp, &nmp, curnmp, &ippos, &dsdir);
8733 if (nd.ni_vp == NULL) {
8734 if (error == 0 && nmp != NULL) {
8735 /* Search the nfsdev list for a match. */
8737 *dsp = nfsrv_findmirroredds(nmp);
8740 if (error == 0 && (nmp == NULL || *dsp == NULL)) {
8741 if (nvpp != NULL && *nvpp != NULL) {
8751 * When dspathp != NULL and curdspathp == NULL, this is a recovery
8752 * and is only allowed if there is a 0.0.0.0 IP address entry.
8753 * When curdspathp != NULL, the ippos will be set to that entry.
8755 if (error == 0 && dspathp != NULL && ippos == -1) {
8756 if (nvpp != NULL && *nvpp != NULL) {
8765 pf = (struct pnfsdsfile *)buf;
8767 /* If no zeroip pnfsdsfile, add one. */
8768 ippos = *buflenp / sizeof(*pf);
8769 *buflenp += sizeof(*pf);
8771 pf->dsf_dir = dsdir;
8772 strlcpy(pf->dsf_filename, fname,
8773 sizeof(pf->dsf_filename));
8783 * Search for a matching pnfsd mirror device structure, base on the nmp arg.
8784 * Return one if found, NULL otherwise.
8786 static struct nfsdevice *
8787 nfsrv_findmirroredds(struct nfsmount *nmp)
8789 struct nfsdevice *ds, *fndds;
8792 mtx_assert(NFSDDSMUTEXPTR, MA_OWNED);
8794 * Search the DS server list for a match with nmp.
8795 * Remove the DS entry if found and there is a mirror.
8799 if (nfsrv_devidcnt == 0)
8801 TAILQ_FOREACH(ds, &nfsrv_devidhead, nfsdev_list) {
8802 if (ds->nfsdev_nmp == nmp) {
8803 NFSD_DEBUG(4, "nfsrv_findmirroredds: fnd main ds\n");
8810 if (fndds->nfsdev_mdsisset == 0 && nfsrv_faildscnt > 0)
8812 else if (fndds->nfsdev_mdsisset != 0) {
8813 /* For the fsid is set case, search for a mirror. */
8814 TAILQ_FOREACH(ds, &nfsrv_devidhead, nfsdev_list) {
8815 if (ds != fndds && ds->nfsdev_nmp != NULL &&
8816 ds->nfsdev_mdsisset != 0 &&
8817 fsidcmp(&ds->nfsdev_mdsfsid,
8818 &fndds->nfsdev_mdsfsid) == 0) {
8824 if (fndmirror == 0) {
8825 NFSD_DEBUG(4, "nfsrv_findmirroredds: no mirror for DS\n");
8832 * Mark the appropriate devid and all associated layout as "out of space".
8835 nfsrv_marknospc(char *devid, bool setit)
8837 struct nfsdevice *ds;
8838 struct nfslayout *lyp;
8839 struct nfslayouthash *lhyp;
8843 TAILQ_FOREACH(ds, &nfsrv_devidhead, nfsdev_list) {
8844 if (NFSBCMP(ds->nfsdev_deviceid, devid, NFSX_V4DEVICEID) == 0) {
8845 NFSD_DEBUG(1, "nfsrv_marknospc: devid %d\n", setit);
8846 ds->nfsdev_nospc = setit;
8851 for (i = 0; i < nfsrv_layouthashsize; i++) {
8852 lhyp = &nfslayouthash[i];
8853 NFSLOCKLAYOUT(lhyp);
8854 TAILQ_FOREACH(lyp, &lhyp->list, lay_list) {
8855 if (NFSBCMP(lyp->lay_deviceid, devid,
8856 NFSX_V4DEVICEID) == 0) {
8857 NFSD_DEBUG(1, "nfsrv_marknospc: layout %d\n",
8860 lyp->lay_flags |= NFSLAY_NOSPC;
8862 lyp->lay_flags &= ~NFSLAY_NOSPC;
8865 NFSUNLOCKLAYOUT(lhyp);