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 int nfsrv_issuedelegs = 0;
39 int nfsrv_dolocallocks = 0;
40 struct nfsv4lock nfsv4rootfs_lock;
41 time_t nfsdev_time = 0;
42 int nfsrv_layouthashsize;
43 volatile int nfsrv_layoutcnt = 0;
45 NFSD_VNET_DEFINE(struct nfsrv_stablefirst, nfsrv_stablefirst);
47 NFSD_VNET_DECLARE(int, nfsrv_numnfsd);
48 NFSD_VNET_DECLARE(struct nfsstatsv1 *, nfsstatsv1_p);
50 extern uint32_t nfs_srvmaxio;
51 extern int nfsrv_lease;
52 extern struct timeval nfsboottime;
53 extern u_int32_t newnfs_true, newnfs_false;
54 extern struct mtx nfsrv_dslock_mtx;
55 extern struct mtx nfsrv_recalllock_mtx;
56 extern struct mtx nfsrv_dontlistlock_mtx;
57 extern int nfsd_debuglevel;
58 extern u_int nfsrv_dsdirsize;
59 extern struct nfsdevicehead nfsrv_devidhead;
60 extern int nfsrv_doflexfile;
61 extern int nfsrv_maxpnfsmirror;
64 extern struct nfsdontlisthead nfsrv_dontlisthead;
65 extern volatile int nfsrv_devidcnt;
66 extern struct nfslayouthead nfsrv_recalllisthead;
67 extern char *nfsrv_zeropnfsdat;
69 SYSCTL_DECL(_vfs_nfsd);
70 int nfsrv_statehashsize = NFSSTATEHASHSIZE;
71 SYSCTL_INT(_vfs_nfsd, OID_AUTO, statehashsize, CTLFLAG_RDTUN,
72 &nfsrv_statehashsize, 0,
73 "Size of state hash table set via loader.conf");
75 int nfsrv_clienthashsize = NFSCLIENTHASHSIZE;
76 SYSCTL_INT(_vfs_nfsd, OID_AUTO, clienthashsize, CTLFLAG_RDTUN,
77 &nfsrv_clienthashsize, 0,
78 "Size of client hash table set via loader.conf");
80 int nfsrv_lockhashsize = NFSLOCKHASHSIZE;
81 SYSCTL_INT(_vfs_nfsd, OID_AUTO, fhhashsize, CTLFLAG_RDTUN,
82 &nfsrv_lockhashsize, 0,
83 "Size of file handle hash table set via loader.conf");
85 int nfsrv_sessionhashsize = NFSSESSIONHASHSIZE;
86 SYSCTL_INT(_vfs_nfsd, OID_AUTO, sessionhashsize, CTLFLAG_RDTUN,
87 &nfsrv_sessionhashsize, 0,
88 "Size of session hash table set via loader.conf");
90 int nfsrv_layouthighwater = NFSLAYOUTHIGHWATER;
91 SYSCTL_INT(_vfs_nfsd, OID_AUTO, layouthighwater, CTLFLAG_RDTUN,
92 &nfsrv_layouthighwater, 0,
93 "High water mark for number of layouts set via loader.conf");
95 static int nfsrv_v4statelimit = NFSRV_V4STATELIMIT;
96 SYSCTL_INT(_vfs_nfsd, OID_AUTO, v4statelimit, CTLFLAG_RWTUN,
97 &nfsrv_v4statelimit, 0,
98 "High water limit for NFSv4 opens+locks+delegations");
100 static int nfsrv_writedelegifpos = 0;
101 SYSCTL_INT(_vfs_nfsd, OID_AUTO, writedelegifpos, CTLFLAG_RW,
102 &nfsrv_writedelegifpos, 0,
103 "Issue a write delegation for read opens if possible");
105 static int nfsrv_allowreadforwriteopen = 1;
106 SYSCTL_INT(_vfs_nfsd, OID_AUTO, allowreadforwriteopen, CTLFLAG_RW,
107 &nfsrv_allowreadforwriteopen, 0,
108 "Allow Reads to be done with Write Access StateIDs");
110 int nfsrv_pnfsatime = 0;
111 SYSCTL_INT(_vfs_nfsd, OID_AUTO, pnfsstrictatime, CTLFLAG_RW,
113 "For pNFS service, do Getattr ops to keep atime up-to-date");
115 int nfsrv_flexlinuxhack = 0;
116 SYSCTL_INT(_vfs_nfsd, OID_AUTO, flexlinuxhack, CTLFLAG_RW,
117 &nfsrv_flexlinuxhack, 0,
118 "For Linux clients, hack around Flex File Layout bug");
121 * Hash lists for nfs V4.
123 NFSD_VNET_DEFINE(struct nfsclienthashhead *, nfsclienthash);
124 NFSD_VNET_DEFINE(struct nfslockhashhead *, nfslockhash);
125 NFSD_VNET_DEFINE(struct nfssessionhash *, nfssessionhash);
127 struct nfslayouthash *nfslayouthash;
128 volatile int nfsrv_dontlistlen = 0;
130 static u_int32_t nfsrv_openpluslock = 0, nfsrv_delegatecnt = 0;
131 static int nfsrv_returnoldstateid = 0, nfsrv_clients = 0;
132 static int nfsrv_clienthighwater = NFSRV_CLIENTHIGHWATER;
133 static int nfsrv_nogsscallback = 0;
134 static volatile int nfsrv_writedelegcnt = 0;
135 static int nfsrv_faildscnt;
137 NFSD_VNET_DEFINE_STATIC(time_t, nfsrvboottime);
139 /* local functions */
140 static void nfsrv_dumpaclient(struct nfsclient *clp,
141 struct nfsd_dumpclients *dumpp);
142 static void nfsrv_freeopenowner(struct nfsstate *stp, int cansleep,
144 static int nfsrv_freeopen(struct nfsstate *stp, vnode_t vp, int cansleep,
146 static void nfsrv_freelockowner(struct nfsstate *stp, vnode_t vp, int cansleep,
148 static void nfsrv_freeallnfslocks(struct nfsstate *stp, vnode_t vp,
149 int cansleep, NFSPROC_T *p);
150 static void nfsrv_freenfslock(struct nfslock *lop);
151 static void nfsrv_freenfslockfile(struct nfslockfile *lfp);
152 static void nfsrv_freedeleg(struct nfsstate *);
153 static int nfsrv_getstate(struct nfsclient *clp, nfsv4stateid_t *stateidp,
154 u_int32_t flags, struct nfsstate **stpp);
155 static void nfsrv_getowner(struct nfsstatehead *hp, struct nfsstate *new_stp,
156 struct nfsstate **stpp);
157 static int nfsrv_getlockfh(vnode_t vp, u_short flags,
158 struct nfslockfile *new_lfp, fhandle_t *nfhp, NFSPROC_T *p);
159 static int nfsrv_getlockfile(u_short flags, struct nfslockfile **new_lfpp,
160 struct nfslockfile **lfpp, fhandle_t *nfhp, int lockit);
161 static void nfsrv_insertlock(struct nfslock *new_lop,
162 struct nfslock *insert_lop, struct nfsstate *stp, struct nfslockfile *lfp);
163 static void nfsrv_updatelock(struct nfsstate *stp, struct nfslock **new_lopp,
164 struct nfslock **other_lopp, struct nfslockfile *lfp);
165 static int nfsrv_getipnumber(u_char *cp);
166 static int nfsrv_checkrestart(nfsquad_t clientid, u_int32_t flags,
167 nfsv4stateid_t *stateidp, int specialid);
168 static int nfsrv_checkgrace(struct nfsrv_descript *nd, struct nfsclient *clp,
170 static int nfsrv_docallback(struct nfsclient *clp, int procnum,
171 nfsv4stateid_t *stateidp, int trunc, fhandle_t *fhp,
172 struct nfsvattr *nap, nfsattrbit_t *attrbitp, int laytype, NFSPROC_T *p);
173 static int nfsrv_cbcallargs(struct nfsrv_descript *nd, struct nfsclient *clp,
174 uint32_t callback, int op, const char *optag, struct nfsdsession **sepp,
176 static u_int32_t nfsrv_nextclientindex(void);
177 static u_int32_t nfsrv_nextstateindex(struct nfsclient *clp);
178 static void nfsrv_markstable(struct nfsclient *clp);
179 static void nfsrv_markreclaim(struct nfsclient *clp);
180 static int nfsrv_checkstable(struct nfsclient *clp);
181 static int nfsrv_clientconflict(struct nfsclient *clp, int *haslockp, struct
182 vnode *vp, NFSPROC_T *p);
183 static int nfsrv_delegconflict(struct nfsstate *stp, int *haslockp,
184 NFSPROC_T *p, vnode_t vp);
185 static int nfsrv_cleandeleg(vnode_t vp, struct nfslockfile *lfp,
186 struct nfsclient *clp, int *haslockp, NFSPROC_T *p);
187 static int nfsrv_notsamecredname(struct nfsrv_descript *nd,
188 struct nfsclient *clp);
189 static time_t nfsrv_leaseexpiry(void);
190 static void nfsrv_delaydelegtimeout(struct nfsstate *stp);
191 static int nfsrv_checkseqid(struct nfsrv_descript *nd, u_int32_t seqid,
192 struct nfsstate *stp, struct nfsrvcache *op);
193 static int nfsrv_nootherstate(struct nfsstate *stp);
194 static int nfsrv_locallock(vnode_t vp, struct nfslockfile *lfp, int flags,
195 uint64_t first, uint64_t end, struct nfslockconflict *cfp, NFSPROC_T *p);
196 static void nfsrv_localunlock(vnode_t vp, struct nfslockfile *lfp,
197 uint64_t init_first, uint64_t init_end, NFSPROC_T *p);
198 static int nfsrv_dolocal(vnode_t vp, struct nfslockfile *lfp, int flags,
199 int oldflags, uint64_t first, uint64_t end, struct nfslockconflict *cfp,
201 static void nfsrv_locallock_rollback(vnode_t vp, struct nfslockfile *lfp,
203 static void nfsrv_locallock_commit(struct nfslockfile *lfp, int flags,
204 uint64_t first, uint64_t end);
205 static void nfsrv_locklf(struct nfslockfile *lfp);
206 static void nfsrv_unlocklf(struct nfslockfile *lfp);
207 static struct nfsdsession *nfsrv_findsession(uint8_t *sessionid);
208 static int nfsrv_freesession(struct nfsdsession *sep, uint8_t *sessionid);
209 static int nfsv4_setcbsequence(struct nfsrv_descript *nd, struct nfsclient *clp,
210 int dont_replycache, struct nfsdsession **sepp, int *slotposp);
211 static int nfsv4_getcbsession(struct nfsclient *clp, struct nfsdsession **sepp);
212 static int nfsrv_addlayout(struct nfsrv_descript *nd, struct nfslayout **lypp,
213 nfsv4stateid_t *stateidp, char *layp, int *layoutlenp, NFSPROC_T *p);
214 static void nfsrv_freelayout(struct nfslayouthead *lhp, struct nfslayout *lyp);
215 static void nfsrv_freelayoutlist(nfsquad_t clientid);
216 static void nfsrv_freelayouts(nfsquad_t *clid, fsid_t *fs, int laytype,
218 static void nfsrv_freealllayouts(void);
219 static void nfsrv_freedevid(struct nfsdevice *ds);
220 static int nfsrv_setdsserver(char *dspathp, char *mdspathp, NFSPROC_T *p,
221 struct nfsdevice **dsp);
222 static void nfsrv_deleteds(struct nfsdevice *fndds);
223 static void nfsrv_allocdevid(struct nfsdevice *ds, char *addr, char *dnshost);
224 static void nfsrv_freealldevids(void);
225 static void nfsrv_flexlayouterr(struct nfsrv_descript *nd, uint32_t *layp,
226 int maxcnt, NFSPROC_T *p);
227 static int nfsrv_recalllayout(nfsquad_t clid, nfsv4stateid_t *stateidp,
228 fhandle_t *fhp, struct nfslayout *lyp, int changed, int laytype,
230 static int nfsrv_findlayout(nfsquad_t *clientidp, fhandle_t *fhp, int laytype,
231 NFSPROC_T *, struct nfslayout **lypp);
232 static int nfsrv_fndclid(nfsquad_t *clidvec, nfsquad_t clid, int clidcnt);
233 static struct nfslayout *nfsrv_filelayout(struct nfsrv_descript *nd, int iomode,
234 fhandle_t *fhp, fhandle_t *dsfhp, char *devid, fsid_t fs);
235 static struct nfslayout *nfsrv_flexlayout(struct nfsrv_descript *nd, int iomode,
236 int mirrorcnt, fhandle_t *fhp, fhandle_t *dsfhp, char *devid, fsid_t fs);
237 static int nfsrv_dontlayout(fhandle_t *fhp);
238 static int nfsrv_createdsfile(vnode_t vp, fhandle_t *fhp, struct pnfsdsfile *pf,
239 vnode_t dvp, struct nfsdevice *ds, struct ucred *cred, NFSPROC_T *p,
241 static struct nfsdevice *nfsrv_findmirroredds(struct nfsmount *nmp);
244 * Scan the client list for a match and either return the current one,
245 * create a new entry or return an error.
246 * If returning a non-error, the clp structure must either be linked into
247 * the client list or free'd.
250 nfsrv_setclient(struct nfsrv_descript *nd, struct nfsclient **new_clpp,
251 nfsquad_t *clientidp, nfsquad_t *confirmp, NFSPROC_T *p)
253 struct nfsclient *clp = NULL, *new_clp = *new_clpp;
254 int i, error = 0, ret;
255 struct nfsstate *stp, *tstp;
257 struct sockaddr_in *sin, *rin;
260 struct sockaddr_in6 *sin6, *rin6;
262 struct nfsdsession *sep, *nsep;
263 int zapit = 0, gotit, hasstate = 0, igotlock;
264 static u_int64_t confirm_index = 0;
267 * Check for state resource limit exceeded.
269 if (nfsrv_openpluslock > nfsrv_v4statelimit) {
270 error = NFSERR_RESOURCE;
274 if (nfsrv_issuedelegs == 0 ||
275 ((nd->nd_flag & ND_GSS) != 0 && nfsrv_nogsscallback != 0))
277 * Don't do callbacks when delegations are disabled or
278 * for AUTH_GSS unless enabled via nfsrv_nogsscallback.
279 * If establishing a callback connection is attempted
280 * when a firewall is blocking the callback path, the
281 * server may wait too long for the connect attempt to
282 * succeed during the Open. Some clients, such as Linux,
283 * may timeout and give up on the Open before the server
284 * replies. Also, since AUTH_GSS callbacks are not
285 * yet interoperability tested, they might cause the
286 * server to crap out, if they get past the Init call to
289 new_clp->lc_program = 0;
291 /* Lock out other nfsd threads */
292 NFSLOCKV4ROOTMUTEX();
293 nfsv4_relref(&nfsv4rootfs_lock);
295 igotlock = nfsv4_lock(&nfsv4rootfs_lock, 1, NULL,
296 NFSV4ROOTLOCKMUTEXPTR, NULL);
298 NFSUNLOCKV4ROOTMUTEX();
301 * Search for a match in the client list.
304 while (i < nfsrv_clienthashsize && !gotit) {
305 LIST_FOREACH(clp, &NFSD_VNET(nfsclienthash)[i], lc_hash) {
306 if (new_clp->lc_idlen == clp->lc_idlen &&
307 !NFSBCMP(new_clp->lc_id, clp->lc_id, clp->lc_idlen)) {
316 (clp->lc_flags & (LCL_NEEDSCONFIRM | LCL_ADMINREVOKED))) {
317 if ((nd->nd_flag & ND_NFSV41) != 0 && confirmp->lval[1] != 0) {
319 * For NFSv4.1, if confirmp->lval[1] is non-zero, the
320 * client is trying to update a confirmed clientid.
322 NFSLOCKV4ROOTMUTEX();
323 nfsv4_unlock(&nfsv4rootfs_lock, 1);
324 NFSUNLOCKV4ROOTMUTEX();
325 confirmp->lval[1] = 0;
326 error = NFSERR_NOENT;
330 * Get rid of the old one.
332 if (i != nfsrv_clienthashsize) {
333 LIST_REMOVE(clp, lc_hash);
334 nfsrv_cleanclient(clp, p);
335 nfsrv_freedeleglist(&clp->lc_deleg);
336 nfsrv_freedeleglist(&clp->lc_olddeleg);
340 * Add it after assigning a client id to it.
342 new_clp->lc_flags |= LCL_NEEDSCONFIRM;
343 if ((nd->nd_flag & ND_NFSV41) != 0) {
344 confirmp->lval[0] = ++confirm_index;
345 new_clp->lc_confirm.lval[0] = confirmp->lval[0] - 1;
347 confirmp->qval = new_clp->lc_confirm.qval =
349 clientidp->lval[0] = new_clp->lc_clientid.lval[0] =
350 NFSD_VNET(nfsrvboottime);
351 clientidp->lval[1] = new_clp->lc_clientid.lval[1] =
352 nfsrv_nextclientindex();
353 new_clp->lc_stateindex = 0;
354 new_clp->lc_statemaxindex = 0;
355 new_clp->lc_prevsess = 0;
356 new_clp->lc_cbref = 0;
357 new_clp->lc_expiry = nfsrv_leaseexpiry();
358 LIST_INIT(&new_clp->lc_open);
359 LIST_INIT(&new_clp->lc_deleg);
360 LIST_INIT(&new_clp->lc_olddeleg);
361 LIST_INIT(&new_clp->lc_session);
362 for (i = 0; i < nfsrv_statehashsize; i++)
363 LIST_INIT(&new_clp->lc_stateid[i]);
364 LIST_INSERT_HEAD(NFSCLIENTHASH(new_clp->lc_clientid), new_clp,
366 NFSD_VNET(nfsstatsv1_p)->srvclients++;
367 nfsrv_openpluslock++;
369 NFSLOCKV4ROOTMUTEX();
370 nfsv4_unlock(&nfsv4rootfs_lock, 1);
371 NFSUNLOCKV4ROOTMUTEX();
373 nfsrv_zapclient(clp, p);
379 * Now, handle the cases where the id is already issued.
381 if (nfsrv_notsamecredname(nd, clp)) {
383 * Check to see if there is expired state that should go away.
385 if (clp->lc_expiry < NFSD_MONOSEC &&
386 (!LIST_EMPTY(&clp->lc_open) || !LIST_EMPTY(&clp->lc_deleg))) {
387 nfsrv_cleanclient(clp, p);
388 nfsrv_freedeleglist(&clp->lc_deleg);
392 * If there is outstanding state, then reply NFSERR_CLIDINUSE per
393 * RFC3530 Sec. 8.1.2 last para.
395 if (!LIST_EMPTY(&clp->lc_deleg)) {
397 } else if (LIST_EMPTY(&clp->lc_open)) {
401 /* Look for an Open on the OpenOwner */
402 LIST_FOREACH(stp, &clp->lc_open, ls_list) {
403 if (!LIST_EMPTY(&stp->ls_open)) {
411 * If the uid doesn't match, return NFSERR_CLIDINUSE after
412 * filling out the correct ipaddr and portnum.
414 switch (clp->lc_req.nr_nam->sa_family) {
417 sin = (struct sockaddr_in *)new_clp->lc_req.nr_nam;
418 rin = (struct sockaddr_in *)clp->lc_req.nr_nam;
419 sin->sin_addr.s_addr = rin->sin_addr.s_addr;
420 sin->sin_port = rin->sin_port;
425 sin6 = (struct sockaddr_in6 *)new_clp->lc_req.nr_nam;
426 rin6 = (struct sockaddr_in6 *)clp->lc_req.nr_nam;
427 sin6->sin6_addr = rin6->sin6_addr;
428 sin6->sin6_port = rin6->sin6_port;
432 NFSLOCKV4ROOTMUTEX();
433 nfsv4_unlock(&nfsv4rootfs_lock, 1);
434 NFSUNLOCKV4ROOTMUTEX();
435 error = NFSERR_CLIDINUSE;
440 if (NFSBCMP(new_clp->lc_verf, clp->lc_verf, NFSX_VERF)) {
442 * If the verifier has changed, the client has rebooted
443 * and a new client id is issued. The old state info
444 * can be thrown away once the SETCLIENTID_CONFIRM occurs.
446 LIST_REMOVE(clp, lc_hash);
448 /* Get rid of all sessions on this clientid. */
449 LIST_FOREACH_SAFE(sep, &clp->lc_session, sess_list, nsep) {
450 ret = nfsrv_freesession(sep, NULL);
452 printf("nfsrv_setclient: verifier changed free"
453 " session failed=%d\n", ret);
456 new_clp->lc_flags |= LCL_NEEDSCONFIRM;
457 if ((nd->nd_flag & ND_NFSV41) != 0) {
458 confirmp->lval[0] = ++confirm_index;
459 new_clp->lc_confirm.lval[0] = confirmp->lval[0] - 1;
461 confirmp->qval = new_clp->lc_confirm.qval =
463 clientidp->lval[0] = new_clp->lc_clientid.lval[0] =
464 NFSD_VNET(nfsrvboottime);
465 clientidp->lval[1] = new_clp->lc_clientid.lval[1] =
466 nfsrv_nextclientindex();
467 new_clp->lc_stateindex = 0;
468 new_clp->lc_statemaxindex = 0;
469 new_clp->lc_prevsess = 0;
470 new_clp->lc_cbref = 0;
471 new_clp->lc_expiry = nfsrv_leaseexpiry();
474 * Save the state until confirmed.
476 LIST_NEWHEAD(&new_clp->lc_open, &clp->lc_open, ls_list);
477 LIST_FOREACH(tstp, &new_clp->lc_open, ls_list)
478 tstp->ls_clp = new_clp;
479 LIST_NEWHEAD(&new_clp->lc_deleg, &clp->lc_deleg, ls_list);
480 LIST_FOREACH(tstp, &new_clp->lc_deleg, ls_list)
481 tstp->ls_clp = new_clp;
482 LIST_NEWHEAD(&new_clp->lc_olddeleg, &clp->lc_olddeleg,
484 LIST_FOREACH(tstp, &new_clp->lc_olddeleg, ls_list)
485 tstp->ls_clp = new_clp;
486 for (i = 0; i < nfsrv_statehashsize; i++) {
487 LIST_NEWHEAD(&new_clp->lc_stateid[i],
488 &clp->lc_stateid[i], ls_hash);
489 LIST_FOREACH(tstp, &new_clp->lc_stateid[i], ls_hash)
490 tstp->ls_clp = new_clp;
492 LIST_INIT(&new_clp->lc_session);
493 LIST_INSERT_HEAD(NFSCLIENTHASH(new_clp->lc_clientid), new_clp,
495 NFSD_VNET(nfsstatsv1_p)->srvclients++;
496 nfsrv_openpluslock++;
498 NFSLOCKV4ROOTMUTEX();
499 nfsv4_unlock(&nfsv4rootfs_lock, 1);
500 NFSUNLOCKV4ROOTMUTEX();
503 * Must wait until any outstanding callback on the old clp
507 while (clp->lc_cbref) {
508 clp->lc_flags |= LCL_WAKEUPWANTED;
509 (void)mtx_sleep(clp, NFSSTATEMUTEXPTR, PZERO - 1,
510 "nfsd clp", 10 * hz);
513 nfsrv_zapclient(clp, p);
518 /* For NFSv4.1, mark that we found a confirmed clientid. */
519 if ((nd->nd_flag & ND_NFSV41) != 0) {
520 clientidp->lval[0] = clp->lc_clientid.lval[0];
521 clientidp->lval[1] = clp->lc_clientid.lval[1];
522 confirmp->lval[0] = 0; /* Ignored by client */
523 confirmp->lval[1] = 1;
526 * id and verifier match, so update the net address info
527 * and get rid of any existing callback authentication
528 * handle, so a new one will be acquired.
530 LIST_REMOVE(clp, lc_hash);
531 new_clp->lc_flags |= (LCL_NEEDSCONFIRM | LCL_DONTCLEAN);
532 new_clp->lc_expiry = nfsrv_leaseexpiry();
533 confirmp->qval = new_clp->lc_confirm.qval = ++confirm_index;
534 clientidp->lval[0] = new_clp->lc_clientid.lval[0] =
535 clp->lc_clientid.lval[0];
536 clientidp->lval[1] = new_clp->lc_clientid.lval[1] =
537 clp->lc_clientid.lval[1];
538 new_clp->lc_delegtime = clp->lc_delegtime;
539 new_clp->lc_stateindex = clp->lc_stateindex;
540 new_clp->lc_statemaxindex = clp->lc_statemaxindex;
541 new_clp->lc_cbref = 0;
542 LIST_NEWHEAD(&new_clp->lc_open, &clp->lc_open, ls_list);
543 LIST_FOREACH(tstp, &new_clp->lc_open, ls_list)
544 tstp->ls_clp = new_clp;
545 LIST_NEWHEAD(&new_clp->lc_deleg, &clp->lc_deleg, ls_list);
546 LIST_FOREACH(tstp, &new_clp->lc_deleg, ls_list)
547 tstp->ls_clp = new_clp;
548 LIST_NEWHEAD(&new_clp->lc_olddeleg, &clp->lc_olddeleg, ls_list);
549 LIST_FOREACH(tstp, &new_clp->lc_olddeleg, ls_list)
550 tstp->ls_clp = new_clp;
551 for (i = 0; i < nfsrv_statehashsize; i++) {
552 LIST_NEWHEAD(&new_clp->lc_stateid[i],
553 &clp->lc_stateid[i], ls_hash);
554 LIST_FOREACH(tstp, &new_clp->lc_stateid[i], ls_hash)
555 tstp->ls_clp = new_clp;
557 LIST_INIT(&new_clp->lc_session);
558 LIST_INSERT_HEAD(NFSCLIENTHASH(new_clp->lc_clientid), new_clp,
560 NFSD_VNET(nfsstatsv1_p)->srvclients++;
561 nfsrv_openpluslock++;
564 NFSLOCKV4ROOTMUTEX();
565 nfsv4_unlock(&nfsv4rootfs_lock, 1);
566 NFSUNLOCKV4ROOTMUTEX();
568 if ((nd->nd_flag & ND_NFSV41) == 0) {
570 * Must wait until any outstanding callback on the old clp
574 while (clp->lc_cbref) {
575 clp->lc_flags |= LCL_WAKEUPWANTED;
576 (void)mtx_sleep(clp, NFSSTATEMUTEXPTR, PZERO - 1,
580 nfsrv_zapclient(clp, p);
585 NFSEXITCODE2(error, nd);
590 * Check to see if the client id exists and optionally confirm it.
593 nfsrv_getclient(nfsquad_t clientid, int opflags, struct nfsclient **clpp,
594 struct nfsdsession *nsep, nfsquad_t confirm, uint32_t cbprogram,
595 struct nfsrv_descript *nd, NFSPROC_T *p)
597 struct nfsclient *clp;
598 struct nfsstate *stp;
600 struct nfsclienthashhead *hp;
601 int error = 0, igotlock, doneok;
602 struct nfssessionhash *shp;
603 struct nfsdsession *sep;
606 static uint64_t next_sess = 0;
610 if ((nd == NULL || (nd->nd_flag & ND_NFSV41) == 0 ||
611 opflags != CLOPS_RENEW) && NFSD_VNET(nfsrvboottime) !=
613 error = NFSERR_STALECLIENTID;
618 * If called with opflags == CLOPS_RENEW, the State Lock is
619 * already held. Otherwise, we need to get either that or,
620 * for the case of Confirm, lock out the nfsd threads.
622 if (opflags & CLOPS_CONFIRM) {
623 NFSLOCKV4ROOTMUTEX();
624 nfsv4_relref(&nfsv4rootfs_lock);
626 igotlock = nfsv4_lock(&nfsv4rootfs_lock, 1, NULL,
627 NFSV4ROOTLOCKMUTEXPTR, NULL);
630 * Create a new sessionid here, since we need to do it where
631 * there is a mutex held to serialize update of next_sess.
633 if ((nd->nd_flag & ND_NFSV41) != 0) {
634 sessid[0] = ++next_sess;
635 sessid[1] = clientid.qval;
637 NFSUNLOCKV4ROOTMUTEX();
638 } else if (opflags != CLOPS_RENEW) {
642 /* For NFSv4.1, the clp is acquired from the associated session. */
643 if (nd != NULL && (nd->nd_flag & ND_NFSV41) != 0 &&
644 opflags == CLOPS_RENEW) {
646 if ((nd->nd_flag & ND_HASSEQUENCE) != 0) {
647 shp = NFSSESSIONHASH(nd->nd_sessionid);
649 sep = nfsrv_findsession(nd->nd_sessionid);
652 NFSUNLOCKSESSION(shp);
655 hp = NFSCLIENTHASH(clientid);
656 LIST_FOREACH(clp, hp, lc_hash) {
657 if (clp->lc_clientid.lval[1] == clientid.lval[1])
662 if (opflags & CLOPS_CONFIRM)
663 error = NFSERR_STALECLIENTID;
665 error = NFSERR_EXPIRED;
666 } else if (clp->lc_flags & LCL_ADMINREVOKED) {
668 * If marked admin revoked, just return the error.
670 error = NFSERR_ADMINREVOKED;
673 if (opflags & CLOPS_CONFIRM) {
674 NFSLOCKV4ROOTMUTEX();
675 nfsv4_unlock(&nfsv4rootfs_lock, 1);
676 NFSUNLOCKV4ROOTMUTEX();
677 } else if (opflags != CLOPS_RENEW) {
684 * Perform any operations specified by the opflags.
686 if (opflags & CLOPS_CONFIRM) {
688 if ((nd->nd_flag & ND_NFSV41) != 0) {
690 * For the case where lc_confirm.lval[0] == confirm.lval[0],
691 * use the new session, but with the previous sessionid.
692 * This is not exactly what the RFC describes, but should
693 * result in the same reply as the previous CreateSession.
695 if (clp->lc_confirm.lval[0] + 1 == confirm.lval[0]) {
696 clp->lc_confirm.lval[0] = confirm.lval[0];
697 clp->lc_prevsess = sessid[0];
698 } else if (clp->lc_confirm.lval[0] == confirm.lval[0]) {
699 if (clp->lc_prevsess == 0)
700 error = NFSERR_SEQMISORDERED;
702 sessid[0] = clp->lc_prevsess;
705 error = NFSERR_SEQMISORDERED;
706 } else if ((nd->nd_flag & ND_NFSV41) == 0 &&
707 clp->lc_confirm.qval != confirm.qval)
708 error = NFSERR_STALECLIENTID;
709 if (error == 0 && nfsrv_notsamecredname(nd, clp))
710 error = NFSERR_CLIDINUSE;
713 if ((clp->lc_flags & (LCL_NEEDSCONFIRM | LCL_DONTCLEAN)) ==
716 * Hang onto the delegations (as old delegations)
717 * for an Open with CLAIM_DELEGATE_PREV unless in
718 * grace, but get rid of the rest of the state.
720 nfsrv_cleanclient(clp, p);
721 nfsrv_freedeleglist(&clp->lc_olddeleg);
722 if (nfsrv_checkgrace(nd, clp, 0)) {
723 /* In grace, so just delete delegations */
724 nfsrv_freedeleglist(&clp->lc_deleg);
726 LIST_FOREACH(stp, &clp->lc_deleg, ls_list)
727 stp->ls_flags |= NFSLCK_OLDDELEG;
728 clp->lc_delegtime = NFSD_MONOSEC +
729 nfsrv_lease + NFSRV_LEASEDELTA;
730 LIST_NEWHEAD(&clp->lc_olddeleg, &clp->lc_deleg,
733 if ((nd->nd_flag & ND_NFSV41) != 0)
734 clp->lc_program = cbprogram;
736 clp->lc_flags &= ~(LCL_NEEDSCONFIRM | LCL_DONTCLEAN);
738 clp->lc_flags |= LCL_NEEDSCBNULL;
739 /* For NFSv4.1, link the session onto the client. */
741 /* Hold a reference on the xprt for a backchannel. */
742 if ((nsep->sess_crflags & NFSV4CRSESS_CONNBACKCHAN)
743 != 0 && !sess_replay) {
744 if (clp->lc_req.nr_client == NULL)
745 clp->lc_req.nr_client = (struct __rpc_client *)
746 clnt_bck_create(nd->nd_xprt->xp_socket,
747 cbprogram, NFSV4_CBVERS);
748 if (clp->lc_req.nr_client != NULL) {
749 SVC_ACQUIRE(nd->nd_xprt);
750 CLNT_ACQUIRE(clp->lc_req.nr_client);
751 nd->nd_xprt->xp_p2 = clp->lc_req.nr_client;
752 /* Disable idle timeout. */
753 nd->nd_xprt->xp_idletimeout = 0;
754 nsep->sess_cbsess.nfsess_xprt = nd->nd_xprt;
756 nsep->sess_crflags &= ~NFSV4CRSESS_CONNBACKCHAN;
758 NFSBCOPY(sessid, nsep->sess_sessionid,
760 NFSBCOPY(sessid, nsep->sess_cbsess.nfsess_sessionid,
763 shp = NFSSESSIONHASH(nsep->sess_sessionid);
766 LIST_INSERT_HEAD(&shp->list, nsep, sess_hash);
767 LIST_INSERT_HEAD(&clp->lc_session, nsep, sess_list);
768 nsep->sess_clp = clp;
769 NFSUNLOCKSESSION(shp);
774 } else if (clp->lc_flags & LCL_NEEDSCONFIRM) {
775 error = NFSERR_EXPIRED;
779 * If called by the Renew Op, we must check the principal.
781 if (!error && (opflags & CLOPS_RENEWOP)) {
782 if (nfsrv_notsamecredname(nd, clp)) {
784 for (i = 0; i < nfsrv_statehashsize && doneok == 0; i++) {
785 LIST_FOREACH(stp, &clp->lc_stateid[i], ls_hash) {
786 if ((stp->ls_flags & NFSLCK_OPEN) &&
787 stp->ls_uid == nd->nd_cred->cr_uid) {
794 error = NFSERR_ACCES;
796 if (!error && (clp->lc_flags & LCL_CBDOWN))
797 error = NFSERR_CBPATHDOWN;
799 if ((!error || error == NFSERR_CBPATHDOWN) &&
800 (opflags & CLOPS_RENEW)) {
801 clp->lc_expiry = nfsrv_leaseexpiry();
803 if (opflags & CLOPS_CONFIRM) {
804 NFSLOCKV4ROOTMUTEX();
805 nfsv4_unlock(&nfsv4rootfs_lock, 1);
806 NFSUNLOCKV4ROOTMUTEX();
807 } else if (opflags != CLOPS_RENEW) {
814 NFSEXITCODE2(error, nd);
819 * Perform the NFSv4.1 destroy clientid.
822 nfsrv_destroyclient(nfsquad_t clientid, NFSPROC_T *p)
824 struct nfsclient *clp;
825 struct nfsclienthashhead *hp;
826 int error = 0, i, igotlock;
828 if (NFSD_VNET(nfsrvboottime) != clientid.lval[0]) {
829 error = NFSERR_STALECLIENTID;
833 /* Lock out other nfsd threads */
834 NFSLOCKV4ROOTMUTEX();
835 nfsv4_relref(&nfsv4rootfs_lock);
837 igotlock = nfsv4_lock(&nfsv4rootfs_lock, 1, NULL,
838 NFSV4ROOTLOCKMUTEXPTR, NULL);
839 } while (igotlock == 0);
840 NFSUNLOCKV4ROOTMUTEX();
842 hp = NFSCLIENTHASH(clientid);
843 LIST_FOREACH(clp, hp, lc_hash) {
844 if (clp->lc_clientid.lval[1] == clientid.lval[1])
848 NFSLOCKV4ROOTMUTEX();
849 nfsv4_unlock(&nfsv4rootfs_lock, 1);
850 NFSUNLOCKV4ROOTMUTEX();
851 /* Just return ok, since it is gone. */
856 * Free up all layouts on the clientid. Should the client return the
859 nfsrv_freelayoutlist(clientid);
861 /* Scan for state on the clientid. */
862 for (i = 0; i < nfsrv_statehashsize; i++)
863 if (!LIST_EMPTY(&clp->lc_stateid[i])) {
864 NFSLOCKV4ROOTMUTEX();
865 nfsv4_unlock(&nfsv4rootfs_lock, 1);
866 NFSUNLOCKV4ROOTMUTEX();
867 error = NFSERR_CLIENTIDBUSY;
870 if (!LIST_EMPTY(&clp->lc_session) || !LIST_EMPTY(&clp->lc_deleg)) {
871 NFSLOCKV4ROOTMUTEX();
872 nfsv4_unlock(&nfsv4rootfs_lock, 1);
873 NFSUNLOCKV4ROOTMUTEX();
874 error = NFSERR_CLIENTIDBUSY;
878 /* Destroy the clientid and return ok. */
879 nfsrv_cleanclient(clp, p);
880 nfsrv_freedeleglist(&clp->lc_deleg);
881 nfsrv_freedeleglist(&clp->lc_olddeleg);
882 LIST_REMOVE(clp, lc_hash);
883 NFSLOCKV4ROOTMUTEX();
884 nfsv4_unlock(&nfsv4rootfs_lock, 1);
885 NFSUNLOCKV4ROOTMUTEX();
886 nfsrv_zapclient(clp, p);
888 NFSEXITCODE2(error, nd);
893 * Called from the new nfssvc syscall to admin revoke a clientid.
894 * Returns 0 for success, error otherwise.
897 nfsrv_adminrevoke(struct nfsd_clid *revokep, NFSPROC_T *p)
899 struct nfsclient *clp = NULL;
904 * First, lock out the nfsd so that state won't change while the
905 * revocation record is being written to the stable storage restart
908 NFSLOCKV4ROOTMUTEX();
910 igotlock = nfsv4_lock(&nfsv4rootfs_lock, 1, NULL,
911 NFSV4ROOTLOCKMUTEXPTR, NULL);
913 NFSUNLOCKV4ROOTMUTEX();
916 * Search for a match in the client list.
919 while (i < nfsrv_clienthashsize && !gotit) {
920 LIST_FOREACH(clp, &NFSD_VNET(nfsclienthash)[i], lc_hash) {
921 if (revokep->nclid_idlen == clp->lc_idlen &&
922 !NFSBCMP(revokep->nclid_id, clp->lc_id, clp->lc_idlen)) {
930 NFSLOCKV4ROOTMUTEX();
931 nfsv4_unlock(&nfsv4rootfs_lock, 0);
932 NFSUNLOCKV4ROOTMUTEX();
938 * Now, write out the revocation record
940 nfsrv_writestable(clp->lc_id, clp->lc_idlen, NFSNST_REVOKE, p);
941 nfsrv_backupstable();
944 * and clear out the state, marking the clientid revoked.
946 clp->lc_flags &= ~LCL_CALLBACKSON;
947 clp->lc_flags |= LCL_ADMINREVOKED;
948 nfsrv_cleanclient(clp, p);
949 nfsrv_freedeleglist(&clp->lc_deleg);
950 nfsrv_freedeleglist(&clp->lc_olddeleg);
951 NFSLOCKV4ROOTMUTEX();
952 nfsv4_unlock(&nfsv4rootfs_lock, 0);
953 NFSUNLOCKV4ROOTMUTEX();
961 * Dump out stats for all clients. Called from nfssvc(2), that is used
965 nfsrv_dumpclients(struct nfsd_dumpclients *dumpp, int maxcnt)
967 struct nfsclient *clp;
971 * First, get a reference on the nfsv4rootfs_lock so that an
972 * exclusive lock cannot be acquired while dumping the clients.
974 NFSLOCKV4ROOTMUTEX();
975 nfsv4_getref(&nfsv4rootfs_lock, NULL, NFSV4ROOTLOCKMUTEXPTR, NULL);
976 NFSUNLOCKV4ROOTMUTEX();
979 * Rattle through the client lists until done.
981 while (i < nfsrv_clienthashsize && cnt < maxcnt) {
982 clp = LIST_FIRST(&NFSD_VNET(nfsclienthash)[i]);
983 while (clp != LIST_END(&NFSD_VNET(nfsclienthash)[i]) && cnt <
985 nfsrv_dumpaclient(clp, &dumpp[cnt]);
987 clp = LIST_NEXT(clp, lc_hash);
992 dumpp[cnt].ndcl_clid.nclid_idlen = 0;
994 NFSLOCKV4ROOTMUTEX();
995 nfsv4_relref(&nfsv4rootfs_lock);
996 NFSUNLOCKV4ROOTMUTEX();
1000 * Dump stats for a client. Must be called with the NFSSTATELOCK and spl'd.
1003 nfsrv_dumpaclient(struct nfsclient *clp, struct nfsd_dumpclients *dumpp)
1005 struct nfsstate *stp, *openstp, *lckownstp;
1006 struct nfslock *lop;
1009 struct sockaddr_in *rin;
1012 struct sockaddr_in6 *rin6;
1015 dumpp->ndcl_nopenowners = dumpp->ndcl_nlockowners = 0;
1016 dumpp->ndcl_nopens = dumpp->ndcl_nlocks = 0;
1017 dumpp->ndcl_ndelegs = dumpp->ndcl_nolddelegs = 0;
1018 dumpp->ndcl_flags = clp->lc_flags;
1019 dumpp->ndcl_clid.nclid_idlen = clp->lc_idlen;
1020 NFSBCOPY(clp->lc_id, dumpp->ndcl_clid.nclid_id, clp->lc_idlen);
1021 af = clp->lc_req.nr_nam->sa_family;
1022 dumpp->ndcl_addrfam = af;
1026 rin = (struct sockaddr_in *)clp->lc_req.nr_nam;
1027 dumpp->ndcl_cbaddr.sin_addr = rin->sin_addr;
1032 rin6 = (struct sockaddr_in6 *)clp->lc_req.nr_nam;
1033 dumpp->ndcl_cbaddr.sin6_addr = rin6->sin6_addr;
1039 * Now, scan the state lists and total up the opens and locks.
1041 LIST_FOREACH(stp, &clp->lc_open, ls_list) {
1042 dumpp->ndcl_nopenowners++;
1043 LIST_FOREACH(openstp, &stp->ls_open, ls_list) {
1044 dumpp->ndcl_nopens++;
1045 LIST_FOREACH(lckownstp, &openstp->ls_open, ls_list) {
1046 dumpp->ndcl_nlockowners++;
1047 LIST_FOREACH(lop, &lckownstp->ls_lock, lo_lckowner) {
1048 dumpp->ndcl_nlocks++;
1055 * and the delegation lists.
1057 LIST_FOREACH(stp, &clp->lc_deleg, ls_list) {
1058 dumpp->ndcl_ndelegs++;
1060 LIST_FOREACH(stp, &clp->lc_olddeleg, ls_list) {
1061 dumpp->ndcl_nolddelegs++;
1066 * Dump out lock stats for a file.
1069 nfsrv_dumplocks(vnode_t vp, struct nfsd_dumplocks *ldumpp, int maxcnt,
1072 struct nfsstate *stp;
1073 struct nfslock *lop;
1075 struct nfslockfile *lfp;
1078 struct sockaddr_in *rin;
1081 struct sockaddr_in6 *rin6;
1086 ret = nfsrv_getlockfh(vp, 0, NULL, &nfh, p);
1088 * First, get a reference on the nfsv4rootfs_lock so that an
1089 * exclusive lock on it cannot be acquired while dumping the locks.
1091 NFSLOCKV4ROOTMUTEX();
1092 nfsv4_getref(&nfsv4rootfs_lock, NULL, NFSV4ROOTLOCKMUTEXPTR, NULL);
1093 NFSUNLOCKV4ROOTMUTEX();
1096 ret = nfsrv_getlockfile(0, NULL, &lfp, &nfh, 0);
1098 ldumpp[0].ndlck_clid.nclid_idlen = 0;
1100 NFSLOCKV4ROOTMUTEX();
1101 nfsv4_relref(&nfsv4rootfs_lock);
1102 NFSUNLOCKV4ROOTMUTEX();
1107 * For each open share on file, dump it out.
1109 stp = LIST_FIRST(&lfp->lf_open);
1110 while (stp != LIST_END(&lfp->lf_open) && cnt < maxcnt) {
1111 ldumpp[cnt].ndlck_flags = stp->ls_flags;
1112 ldumpp[cnt].ndlck_stateid.seqid = stp->ls_stateid.seqid;
1113 ldumpp[cnt].ndlck_stateid.other[0] = stp->ls_stateid.other[0];
1114 ldumpp[cnt].ndlck_stateid.other[1] = stp->ls_stateid.other[1];
1115 ldumpp[cnt].ndlck_stateid.other[2] = stp->ls_stateid.other[2];
1116 ldumpp[cnt].ndlck_owner.nclid_idlen =
1117 stp->ls_openowner->ls_ownerlen;
1118 NFSBCOPY(stp->ls_openowner->ls_owner,
1119 ldumpp[cnt].ndlck_owner.nclid_id,
1120 stp->ls_openowner->ls_ownerlen);
1121 ldumpp[cnt].ndlck_clid.nclid_idlen = stp->ls_clp->lc_idlen;
1122 NFSBCOPY(stp->ls_clp->lc_id, ldumpp[cnt].ndlck_clid.nclid_id,
1123 stp->ls_clp->lc_idlen);
1124 af = stp->ls_clp->lc_req.nr_nam->sa_family;
1125 ldumpp[cnt].ndlck_addrfam = af;
1129 rin = (struct sockaddr_in *)stp->ls_clp->lc_req.nr_nam;
1130 ldumpp[cnt].ndlck_cbaddr.sin_addr = rin->sin_addr;
1135 rin6 = (struct sockaddr_in6 *)
1136 stp->ls_clp->lc_req.nr_nam;
1137 ldumpp[cnt].ndlck_cbaddr.sin6_addr = rin6->sin6_addr;
1141 stp = LIST_NEXT(stp, ls_file);
1148 lop = LIST_FIRST(&lfp->lf_lock);
1149 while (lop != LIST_END(&lfp->lf_lock) && cnt < maxcnt) {
1151 ldumpp[cnt].ndlck_flags = lop->lo_flags;
1152 ldumpp[cnt].ndlck_first = lop->lo_first;
1153 ldumpp[cnt].ndlck_end = lop->lo_end;
1154 ldumpp[cnt].ndlck_stateid.seqid = stp->ls_stateid.seqid;
1155 ldumpp[cnt].ndlck_stateid.other[0] = stp->ls_stateid.other[0];
1156 ldumpp[cnt].ndlck_stateid.other[1] = stp->ls_stateid.other[1];
1157 ldumpp[cnt].ndlck_stateid.other[2] = stp->ls_stateid.other[2];
1158 ldumpp[cnt].ndlck_owner.nclid_idlen = stp->ls_ownerlen;
1159 NFSBCOPY(stp->ls_owner, ldumpp[cnt].ndlck_owner.nclid_id,
1161 ldumpp[cnt].ndlck_clid.nclid_idlen = stp->ls_clp->lc_idlen;
1162 NFSBCOPY(stp->ls_clp->lc_id, ldumpp[cnt].ndlck_clid.nclid_id,
1163 stp->ls_clp->lc_idlen);
1164 af = stp->ls_clp->lc_req.nr_nam->sa_family;
1165 ldumpp[cnt].ndlck_addrfam = af;
1169 rin = (struct sockaddr_in *)stp->ls_clp->lc_req.nr_nam;
1170 ldumpp[cnt].ndlck_cbaddr.sin_addr = rin->sin_addr;
1175 rin6 = (struct sockaddr_in6 *)
1176 stp->ls_clp->lc_req.nr_nam;
1177 ldumpp[cnt].ndlck_cbaddr.sin6_addr = rin6->sin6_addr;
1181 lop = LIST_NEXT(lop, lo_lckfile);
1186 * and the delegations.
1188 stp = LIST_FIRST(&lfp->lf_deleg);
1189 while (stp != LIST_END(&lfp->lf_deleg) && cnt < maxcnt) {
1190 ldumpp[cnt].ndlck_flags = stp->ls_flags;
1191 ldumpp[cnt].ndlck_stateid.seqid = stp->ls_stateid.seqid;
1192 ldumpp[cnt].ndlck_stateid.other[0] = stp->ls_stateid.other[0];
1193 ldumpp[cnt].ndlck_stateid.other[1] = stp->ls_stateid.other[1];
1194 ldumpp[cnt].ndlck_stateid.other[2] = stp->ls_stateid.other[2];
1195 ldumpp[cnt].ndlck_owner.nclid_idlen = 0;
1196 ldumpp[cnt].ndlck_clid.nclid_idlen = stp->ls_clp->lc_idlen;
1197 NFSBCOPY(stp->ls_clp->lc_id, ldumpp[cnt].ndlck_clid.nclid_id,
1198 stp->ls_clp->lc_idlen);
1199 af = stp->ls_clp->lc_req.nr_nam->sa_family;
1200 ldumpp[cnt].ndlck_addrfam = af;
1204 rin = (struct sockaddr_in *)stp->ls_clp->lc_req.nr_nam;
1205 ldumpp[cnt].ndlck_cbaddr.sin_addr = rin->sin_addr;
1210 rin6 = (struct sockaddr_in6 *)
1211 stp->ls_clp->lc_req.nr_nam;
1212 ldumpp[cnt].ndlck_cbaddr.sin6_addr = rin6->sin6_addr;
1216 stp = LIST_NEXT(stp, ls_file);
1221 * If list isn't full, mark end of list by setting the client name
1225 ldumpp[cnt].ndlck_clid.nclid_idlen = 0;
1227 NFSLOCKV4ROOTMUTEX();
1228 nfsv4_relref(&nfsv4rootfs_lock);
1229 NFSUNLOCKV4ROOTMUTEX();
1233 * Server timer routine. It can scan any linked list, so long
1234 * as it holds the spin/mutex lock and there is no exclusive lock on
1236 * (For OpenBSD, a kthread is ok. For FreeBSD, I think it is ok
1237 * to do this from a callout, since the spin locks work. For
1238 * Darwin, I'm not sure what will work correctly yet.)
1239 * Should be called once per second.
1242 nfsrv_servertimer(void *arg __unused)
1244 struct nfsclient *clp, *nclp;
1245 struct nfsstate *stp, *nstp;
1249 * Make sure nfsboottime is set. This is used by V3 as well
1250 * as V4. Note that nfsboottime is not nfsrvboottime, which is
1251 * only used by the V4 server for leases.
1253 if (nfsboottime.tv_sec == 0)
1254 NFSSETBOOTTIME(nfsboottime);
1257 * If server hasn't started yet, just return.
1260 if (NFSD_VNET(nfsrv_stablefirst).nsf_eograce == 0) {
1264 if (!(NFSD_VNET(nfsrv_stablefirst).nsf_flags & NFSNSF_UPDATEDONE)) {
1265 if (!(NFSD_VNET(nfsrv_stablefirst).nsf_flags &
1266 NFSNSF_GRACEOVER) &&
1267 NFSD_MONOSEC > NFSD_VNET(nfsrv_stablefirst).nsf_eograce)
1268 NFSD_VNET(nfsrv_stablefirst).nsf_flags |=
1269 (NFSNSF_GRACEOVER | NFSNSF_NEEDLOCK);
1275 * Try and get a reference count on the nfsv4rootfs_lock so that
1276 * no nfsd thread can acquire an exclusive lock on it before this
1277 * call is done. If it is already exclusively locked, just return.
1279 NFSLOCKV4ROOTMUTEX();
1280 got_ref = nfsv4_getref_nonblock(&nfsv4rootfs_lock);
1281 NFSUNLOCKV4ROOTMUTEX();
1288 * For each client...
1290 for (i = 0; i < nfsrv_clienthashsize; i++) {
1291 clp = LIST_FIRST(&NFSD_VNET(nfsclienthash)[i]);
1292 while (clp != LIST_END(&NFSD_VNET(nfsclienthash)[i])) {
1293 nclp = LIST_NEXT(clp, lc_hash);
1294 if (!(clp->lc_flags & LCL_EXPIREIT)) {
1295 if (((clp->lc_expiry + NFSRV_STALELEASE) < NFSD_MONOSEC
1296 && ((LIST_EMPTY(&clp->lc_deleg)
1297 && LIST_EMPTY(&clp->lc_open)) ||
1298 nfsrv_clients > nfsrv_clienthighwater)) ||
1299 (clp->lc_expiry + NFSRV_MOULDYLEASE) < NFSD_MONOSEC ||
1300 (clp->lc_expiry < NFSD_MONOSEC &&
1301 (nfsrv_openpluslock * 10 / 9) > nfsrv_v4statelimit)) {
1303 * Lease has expired several nfsrv_lease times ago:
1305 * - no state is associated with it
1307 * - above high water mark for number of clients
1308 * (nfsrv_clienthighwater should be large enough
1309 * that this only occurs when clients fail to
1310 * use the same nfs_client_id4.id. Maybe somewhat
1311 * higher that the maximum number of clients that
1312 * will mount this server?)
1314 * Lease has expired a very long time ago
1316 * Lease has expired PLUS the number of opens + locks
1317 * has exceeded 90% of capacity
1319 * --> Mark for expiry. The actual expiry will be done
1320 * by an nfsd sometime soon.
1322 clp->lc_flags |= LCL_EXPIREIT;
1323 NFSD_VNET(nfsrv_stablefirst).nsf_flags |=
1324 (NFSNSF_NEEDLOCK | NFSNSF_EXPIREDCLIENT);
1327 * If there are no opens, increment no open tick cnt
1328 * If time exceeds NFSNOOPEN, mark it to be thrown away
1329 * otherwise, if there is an open, reset no open time
1330 * Hopefully, this will avoid excessive re-creation
1331 * of open owners and subsequent open confirms.
1333 stp = LIST_FIRST(&clp->lc_open);
1334 while (stp != LIST_END(&clp->lc_open)) {
1335 nstp = LIST_NEXT(stp, ls_list);
1336 if (LIST_EMPTY(&stp->ls_open)) {
1338 if (stp->ls_noopens > NFSNOOPEN ||
1339 (nfsrv_openpluslock * 2) >
1341 NFSD_VNET(nfsrv_stablefirst).nsf_flags |=
1344 stp->ls_noopens = 0;
1354 NFSLOCKV4ROOTMUTEX();
1355 nfsv4_relref(&nfsv4rootfs_lock);
1356 NFSUNLOCKV4ROOTMUTEX();
1360 * The following set of functions free up the various data structures.
1363 * Clear out all open/lock state related to this nfsclient.
1364 * Caller must hold an exclusive lock on nfsv4rootfs_lock, so that
1365 * there are no other active nfsd threads.
1368 nfsrv_cleanclient(struct nfsclient *clp, NFSPROC_T *p)
1370 struct nfsstate *stp, *nstp;
1371 struct nfsdsession *sep, *nsep;
1373 LIST_FOREACH_SAFE(stp, &clp->lc_open, ls_list, nstp)
1374 nfsrv_freeopenowner(stp, 1, p);
1375 if ((clp->lc_flags & LCL_ADMINREVOKED) == 0)
1376 LIST_FOREACH_SAFE(sep, &clp->lc_session, sess_list, nsep)
1377 (void)nfsrv_freesession(sep, NULL);
1381 * Free a client that has been cleaned. It should also already have been
1382 * removed from the lists.
1383 * (Just to be safe w.r.t. newnfs_disconnect(), call this function when
1384 * softclock interrupts are enabled.)
1387 nfsrv_zapclient(struct nfsclient *clp, NFSPROC_T *p)
1391 if ((clp->lc_flags & (LCL_GSS | LCL_CALLBACKSON)) ==
1392 (LCL_GSS | LCL_CALLBACKSON) &&
1393 (clp->lc_hand.nfsh_flag & NFSG_COMPLETE) &&
1394 clp->lc_handlelen > 0) {
1395 clp->lc_hand.nfsh_flag &= ~NFSG_COMPLETE;
1396 clp->lc_hand.nfsh_flag |= NFSG_DESTROYED;
1397 (void) nfsrv_docallback(clp, NFSV4PROC_CBNULL,
1398 NULL, 0, NULL, NULL, NULL, 0, p);
1401 newnfs_disconnect(NULL, &clp->lc_req);
1402 free(clp->lc_req.nr_nam, M_SONAME);
1403 NFSFREEMUTEX(&clp->lc_req.nr_mtx);
1404 free(clp->lc_stateid, M_NFSDCLIENT);
1405 free(clp, M_NFSDCLIENT);
1407 NFSD_VNET(nfsstatsv1_p)->srvclients--;
1408 nfsrv_openpluslock--;
1414 * Free a list of delegation state structures.
1415 * (This function will also free all nfslockfile structures that no
1416 * longer have associated state.)
1419 nfsrv_freedeleglist(struct nfsstatehead *sthp)
1421 struct nfsstate *stp, *nstp;
1423 LIST_FOREACH_SAFE(stp, sthp, ls_list, nstp) {
1424 nfsrv_freedeleg(stp);
1430 * Free up a delegation.
1433 nfsrv_freedeleg(struct nfsstate *stp)
1435 struct nfslockfile *lfp;
1437 LIST_REMOVE(stp, ls_hash);
1438 LIST_REMOVE(stp, ls_list);
1439 LIST_REMOVE(stp, ls_file);
1440 if ((stp->ls_flags & NFSLCK_DELEGWRITE) != 0)
1441 nfsrv_writedelegcnt--;
1443 if (LIST_EMPTY(&lfp->lf_open) &&
1444 LIST_EMPTY(&lfp->lf_lock) && LIST_EMPTY(&lfp->lf_deleg) &&
1445 LIST_EMPTY(&lfp->lf_locallock) && LIST_EMPTY(&lfp->lf_rollback) &&
1446 lfp->lf_usecount == 0 &&
1447 nfsv4_testlock(&lfp->lf_locallock_lck) == 0)
1448 nfsrv_freenfslockfile(lfp);
1449 free(stp, M_NFSDSTATE);
1450 NFSD_VNET(nfsstatsv1_p)->srvdelegates--;
1451 nfsrv_openpluslock--;
1452 nfsrv_delegatecnt--;
1456 * This function frees an open owner and all associated opens.
1459 nfsrv_freeopenowner(struct nfsstate *stp, int cansleep, NFSPROC_T *p)
1461 struct nfsstate *nstp, *tstp;
1463 LIST_REMOVE(stp, ls_list);
1465 * Now, free all associated opens.
1467 nstp = LIST_FIRST(&stp->ls_open);
1468 while (nstp != LIST_END(&stp->ls_open)) {
1470 nstp = LIST_NEXT(nstp, ls_list);
1471 (void) nfsrv_freeopen(tstp, NULL, cansleep, p);
1474 nfsrvd_derefcache(stp->ls_op);
1475 free(stp, M_NFSDSTATE);
1476 NFSD_VNET(nfsstatsv1_p)->srvopenowners--;
1477 nfsrv_openpluslock--;
1481 * This function frees an open (nfsstate open structure) with all associated
1482 * lock_owners and locks. It also frees the nfslockfile structure iff there
1483 * are no other opens on the file.
1484 * Returns 1 if it free'd the nfslockfile, 0 otherwise.
1487 nfsrv_freeopen(struct nfsstate *stp, vnode_t vp, int cansleep, NFSPROC_T *p)
1489 struct nfsstate *nstp, *tstp;
1490 struct nfslockfile *lfp;
1493 LIST_REMOVE(stp, ls_hash);
1494 LIST_REMOVE(stp, ls_list);
1495 LIST_REMOVE(stp, ls_file);
1499 * Now, free all lockowners associated with this open.
1501 LIST_FOREACH_SAFE(tstp, &stp->ls_open, ls_list, nstp)
1502 nfsrv_freelockowner(tstp, vp, cansleep, p);
1505 * The nfslockfile is freed here if there are no locks
1506 * associated with the open.
1507 * If there are locks associated with the open, the
1508 * nfslockfile structure can be freed via nfsrv_freelockowner().
1509 * Acquire the state mutex to avoid races with calls to
1510 * nfsrv_getlockfile().
1514 if (lfp != NULL && LIST_EMPTY(&lfp->lf_open) &&
1515 LIST_EMPTY(&lfp->lf_deleg) && LIST_EMPTY(&lfp->lf_lock) &&
1516 LIST_EMPTY(&lfp->lf_locallock) && LIST_EMPTY(&lfp->lf_rollback) &&
1517 lfp->lf_usecount == 0 &&
1518 (cansleep != 0 || nfsv4_testlock(&lfp->lf_locallock_lck) == 0)) {
1519 nfsrv_freenfslockfile(lfp);
1525 free(stp, M_NFSDSTATE);
1526 NFSD_VNET(nfsstatsv1_p)->srvopens--;
1527 nfsrv_openpluslock--;
1532 * Frees a lockowner and all associated locks.
1535 nfsrv_freelockowner(struct nfsstate *stp, vnode_t vp, int cansleep,
1539 LIST_REMOVE(stp, ls_hash);
1540 LIST_REMOVE(stp, ls_list);
1541 nfsrv_freeallnfslocks(stp, vp, cansleep, p);
1543 nfsrvd_derefcache(stp->ls_op);
1544 free(stp, M_NFSDSTATE);
1545 NFSD_VNET(nfsstatsv1_p)->srvlockowners--;
1546 nfsrv_openpluslock--;
1550 * Free all the nfs locks on a lockowner.
1553 nfsrv_freeallnfslocks(struct nfsstate *stp, vnode_t vp, int cansleep,
1556 struct nfslock *lop, *nlop;
1557 struct nfsrollback *rlp, *nrlp;
1558 struct nfslockfile *lfp = NULL;
1561 uint64_t first, end;
1564 ASSERT_VOP_UNLOCKED(vp, "nfsrv_freeallnfslocks: vnode locked");
1565 lop = LIST_FIRST(&stp->ls_lock);
1566 while (lop != LIST_END(&stp->ls_lock)) {
1567 nlop = LIST_NEXT(lop, lo_lckowner);
1569 * Since all locks should be for the same file, lfp should
1574 else if (lfp != lop->lo_lfp)
1575 panic("allnfslocks");
1577 * If vp is NULL and cansleep != 0, a vnode must be acquired
1578 * from the file handle. This only occurs when called from
1579 * nfsrv_cleanclient().
1582 if (nfsrv_dolocallocks == 0)
1584 else if (vp == NULL && cansleep != 0) {
1585 tvp = nfsvno_getvp(&lfp->lf_fh);
1596 first = lop->lo_first;
1598 nfsrv_freenfslock(lop);
1599 nfsrv_localunlock(tvp, lfp, first, end, p);
1600 LIST_FOREACH_SAFE(rlp, &lfp->lf_rollback, rlck_list,
1602 free(rlp, M_NFSDROLLBACK);
1603 LIST_INIT(&lfp->lf_rollback);
1605 nfsrv_freenfslock(lop);
1608 if (vp == NULL && tvp != NULL)
1613 * Free an nfslock structure.
1616 nfsrv_freenfslock(struct nfslock *lop)
1619 if (lop->lo_lckfile.le_prev != NULL) {
1620 LIST_REMOVE(lop, lo_lckfile);
1621 NFSD_VNET(nfsstatsv1_p)->srvlocks--;
1622 nfsrv_openpluslock--;
1624 LIST_REMOVE(lop, lo_lckowner);
1625 free(lop, M_NFSDLOCK);
1629 * This function frees an nfslockfile structure.
1632 nfsrv_freenfslockfile(struct nfslockfile *lfp)
1635 LIST_REMOVE(lfp, lf_hash);
1636 free(lfp, M_NFSDLOCKFILE);
1640 * This function looks up an nfsstate structure via stateid.
1643 nfsrv_getstate(struct nfsclient *clp, nfsv4stateid_t *stateidp, __unused u_int32_t flags,
1644 struct nfsstate **stpp)
1646 struct nfsstate *stp;
1647 struct nfsstatehead *hp;
1651 hp = NFSSTATEHASH(clp, *stateidp);
1652 LIST_FOREACH(stp, hp, ls_hash) {
1653 if (!NFSBCMP(stp->ls_stateid.other, stateidp->other,
1659 * If no state id in list, return NFSERR_BADSTATEID.
1661 if (stp == LIST_END(hp)) {
1662 error = NFSERR_BADSTATEID;
1673 * This function gets an nfsstate structure via owner string.
1676 nfsrv_getowner(struct nfsstatehead *hp, struct nfsstate *new_stp,
1677 struct nfsstate **stpp)
1679 struct nfsstate *stp;
1682 LIST_FOREACH(stp, hp, ls_list) {
1683 if (new_stp->ls_ownerlen == stp->ls_ownerlen &&
1684 !NFSBCMP(new_stp->ls_owner,stp->ls_owner,stp->ls_ownerlen)) {
1692 * Lock control function called to update lock status.
1693 * Returns 0 upon success, -1 if there is no lock and the flags indicate
1694 * that one isn't to be created and an NFSERR_xxx for other errors.
1695 * The structures new_stp and new_lop are passed in as pointers that should
1696 * be set to NULL if the structure is used and shouldn't be free'd.
1697 * For the NFSLCK_TEST and NFSLCK_CHECK cases, the structures are
1698 * never used and can safely be allocated on the stack. For all other
1699 * cases, *new_stpp and *new_lopp should be malloc'd before the call,
1700 * in case they are used.
1703 nfsrv_lockctrl(vnode_t vp, struct nfsstate **new_stpp,
1704 struct nfslock **new_lopp, struct nfslockconflict *cfp,
1705 nfsquad_t clientid, nfsv4stateid_t *stateidp,
1706 __unused struct nfsexstuff *exp,
1707 struct nfsrv_descript *nd, NFSPROC_T *p)
1709 struct nfslock *lop;
1710 struct nfsstate *new_stp = *new_stpp;
1711 struct nfslock *new_lop = *new_lopp;
1712 struct nfsstate *tstp, *mystp, *nstp;
1714 struct nfslockfile *lfp;
1715 struct nfslock *other_lop = NULL;
1716 struct nfsstate *stp, *lckstp = NULL;
1717 struct nfsclient *clp = NULL;
1719 int error = 0, haslock = 0, ret, reterr;
1720 int getlckret, delegation = 0, filestruct_locked, vnode_unlocked = 0;
1722 uint64_t first, end;
1723 uint32_t lock_flags;
1725 if (new_stp->ls_flags & (NFSLCK_CHECK | NFSLCK_SETATTR)) {
1727 * Note the special cases of "all 1s" or "all 0s" stateids and
1728 * let reads with all 1s go ahead.
1730 if (new_stp->ls_stateid.seqid == 0x0 &&
1731 new_stp->ls_stateid.other[0] == 0x0 &&
1732 new_stp->ls_stateid.other[1] == 0x0 &&
1733 new_stp->ls_stateid.other[2] == 0x0)
1735 else if (new_stp->ls_stateid.seqid == 0xffffffff &&
1736 new_stp->ls_stateid.other[0] == 0xffffffff &&
1737 new_stp->ls_stateid.other[1] == 0xffffffff &&
1738 new_stp->ls_stateid.other[2] == 0xffffffff)
1743 * Check for restart conditions (client and server).
1745 error = nfsrv_checkrestart(clientid, new_stp->ls_flags,
1746 &new_stp->ls_stateid, specialid);
1751 * Check for state resource limit exceeded.
1753 if ((new_stp->ls_flags & NFSLCK_LOCK) &&
1754 nfsrv_openpluslock > nfsrv_v4statelimit) {
1755 error = NFSERR_RESOURCE;
1760 * For the lock case, get another nfslock structure,
1761 * just in case we need it.
1762 * Malloc now, before we start sifting through the linked lists,
1763 * in case we have to wait for memory.
1766 if (new_stp->ls_flags & NFSLCK_LOCK)
1767 other_lop = malloc(sizeof (struct nfslock),
1768 M_NFSDLOCK, M_WAITOK);
1769 filestruct_locked = 0;
1774 * Get the lockfile structure for CFH now, so we can do a sanity
1775 * check against the stateid, before incrementing the seqid#, since
1776 * we want to return NFSERR_BADSTATEID on failure and the seqid#
1777 * shouldn't be incremented for this case.
1778 * If nfsrv_getlockfile() returns -1, it means "not found", which
1779 * will be handled later.
1780 * If we are doing Lock/LockU and local locking is enabled, sleep
1781 * lock the nfslockfile structure.
1783 getlckret = nfsrv_getlockfh(vp, new_stp->ls_flags, NULL, &nfh, p);
1785 if (getlckret == 0) {
1786 if ((new_stp->ls_flags & (NFSLCK_LOCK | NFSLCK_UNLOCK)) != 0 &&
1787 nfsrv_dolocallocks != 0 && nd->nd_repstat == 0) {
1788 getlckret = nfsrv_getlockfile(new_stp->ls_flags, NULL,
1791 filestruct_locked = 1;
1793 getlckret = nfsrv_getlockfile(new_stp->ls_flags, NULL,
1796 if (getlckret != 0 && getlckret != -1)
1799 if (filestruct_locked != 0) {
1800 LIST_INIT(&lfp->lf_rollback);
1801 if ((new_stp->ls_flags & NFSLCK_LOCK)) {
1803 * For local locking, do the advisory locking now, so
1804 * that any conflict can be detected. A failure later
1805 * can be rolled back locally. If an error is returned,
1806 * struct nfslockfile has been unlocked and any local
1807 * locking rolled back.
1810 if (vnode_unlocked == 0) {
1811 ASSERT_VOP_ELOCKED(vp, "nfsrv_lockctrl1");
1815 reterr = nfsrv_locallock(vp, lfp,
1816 (new_lop->lo_flags & (NFSLCK_READ | NFSLCK_WRITE)),
1817 new_lop->lo_first, new_lop->lo_end, cfp, p);
1822 if (specialid == 0) {
1823 if (new_stp->ls_flags & NFSLCK_TEST) {
1825 * RFC 3530 does not list LockT as an op that renews a
1826 * lease, but the consensus seems to be that it is ok
1827 * for a server to do so.
1829 error = nfsrv_getclient(clientid, CLOPS_RENEW, &clp, NULL,
1830 (nfsquad_t)((u_quad_t)0), 0, nd, p);
1833 * Since NFSERR_EXPIRED, NFSERR_ADMINREVOKED are not valid
1834 * error returns for LockT, just go ahead and test for a lock,
1835 * since there are no locks for this client, but other locks
1836 * can conflict. (ie. same client will always be false)
1838 if (error == NFSERR_EXPIRED || error == NFSERR_ADMINREVOKED)
1842 error = nfsrv_getclient(clientid, CLOPS_RENEW, &clp, NULL,
1843 (nfsquad_t)((u_quad_t)0), 0, nd, p);
1846 * Look up the stateid
1848 error = nfsrv_getstate(clp, &new_stp->ls_stateid,
1849 new_stp->ls_flags, &stp);
1851 * do some sanity checks for an unconfirmed open or a
1852 * stateid that refers to the wrong file, for an open stateid
1854 if (error == 0 && (stp->ls_flags & NFSLCK_OPEN) &&
1855 ((stp->ls_openowner->ls_flags & NFSLCK_NEEDSCONFIRM) ||
1856 (getlckret == 0 && stp->ls_lfp != lfp))){
1858 * NFSLCK_SETATTR should return OK rather than NFSERR_BADSTATEID
1859 * The only exception is using SETATTR with SIZE.
1861 if ((new_stp->ls_flags &
1862 (NFSLCK_SETATTR | NFSLCK_CHECK)) != NFSLCK_SETATTR)
1863 error = NFSERR_BADSTATEID;
1867 (stp->ls_flags & (NFSLCK_DELEGREAD | NFSLCK_DELEGWRITE)) &&
1868 getlckret == 0 && stp->ls_lfp != lfp)
1869 error = NFSERR_BADSTATEID;
1872 * If the lockowner stateid doesn't refer to the same file,
1873 * I believe that is considered ok, since some clients will
1874 * only create a single lockowner and use that for all locks
1876 * For now, log it as a diagnostic, instead of considering it
1879 if (error == 0 && (stp->ls_flags &
1880 (NFSLCK_OPEN | NFSLCK_DELEGREAD | NFSLCK_DELEGWRITE)) == 0 &&
1881 getlckret == 0 && stp->ls_lfp != lfp) {
1883 printf("Got a lock statid for different file open\n");
1886 error = NFSERR_BADSTATEID;
1891 if (new_stp->ls_flags & NFSLCK_OPENTOLOCK) {
1893 * If haslock set, we've already checked the seqid.
1896 if (stp->ls_flags & NFSLCK_OPEN)
1897 error = nfsrv_checkseqid(nd, new_stp->ls_seq,
1898 stp->ls_openowner, new_stp->ls_op);
1900 error = NFSERR_BADSTATEID;
1903 nfsrv_getowner(&stp->ls_open, new_stp, &lckstp);
1906 * For NFSv4.1 and NFSv4.2 allow an
1907 * open_to_lock_owner when the lock_owner already
1908 * exists. Just clear NFSLCK_OPENTOLOCK so that
1909 * a new lock_owner will not be created.
1910 * RFC7530 states that the error for NFSv4.0
1911 * is NFS4ERR_BAD_SEQID.
1913 if ((nd->nd_flag & ND_NFSV41) != 0)
1914 new_stp->ls_flags &= ~NFSLCK_OPENTOLOCK;
1916 error = NFSERR_BADSEQID;
1919 } else if (new_stp->ls_flags&(NFSLCK_LOCK|NFSLCK_UNLOCK)) {
1921 * If haslock set, ditto above.
1924 if (stp->ls_flags & NFSLCK_OPEN)
1925 error = NFSERR_BADSTATEID;
1927 error = nfsrv_checkseqid(nd, new_stp->ls_seq,
1928 stp, new_stp->ls_op);
1936 * If the seqid part of the stateid isn't the same, return
1937 * NFSERR_OLDSTATEID for cases other than I/O Ops.
1938 * For I/O Ops, only return NFSERR_OLDSTATEID if
1939 * nfsrv_returnoldstateid is set. (The consensus on the email
1940 * list was that most clients would prefer to not receive
1941 * NFSERR_OLDSTATEID for I/O Ops, but the RFC suggests that that
1942 * is what will happen, so I use the nfsrv_returnoldstateid to
1943 * allow for either server configuration.)
1945 if (!error && stp->ls_stateid.seqid!=new_stp->ls_stateid.seqid &&
1946 (((nd->nd_flag & ND_NFSV41) == 0 &&
1947 (!(new_stp->ls_flags & NFSLCK_CHECK) ||
1948 nfsrv_returnoldstateid)) ||
1949 ((nd->nd_flag & ND_NFSV41) != 0 &&
1950 new_stp->ls_stateid.seqid != 0)))
1951 error = NFSERR_OLDSTATEID;
1956 * Now we can check for grace.
1959 error = nfsrv_checkgrace(nd, clp, new_stp->ls_flags);
1960 if ((new_stp->ls_flags & NFSLCK_RECLAIM) && !error &&
1961 nfsrv_checkstable(clp))
1962 error = NFSERR_NOGRACE;
1964 * If we successfully Reclaimed state, note that.
1966 if ((new_stp->ls_flags & NFSLCK_RECLAIM) && !error)
1967 nfsrv_markstable(clp);
1970 * At this point, either error == NFSERR_BADSTATEID or the
1971 * seqid# has been updated, so we can return any error.
1972 * If error == 0, there may be an error in:
1973 * nd_repstat - Set by the calling function.
1974 * reterr - Set above, if getting the nfslockfile structure
1975 * or acquiring the local lock failed.
1976 * (If both of these are set, nd_repstat should probably be
1977 * returned, since that error was detected before this
1980 if (error != 0 || nd->nd_repstat != 0 || reterr != 0) {
1982 if (nd->nd_repstat != 0)
1983 error = nd->nd_repstat;
1987 if (filestruct_locked != 0) {
1988 /* Roll back local locks. */
1990 if (vnode_unlocked == 0) {
1991 ASSERT_VOP_ELOCKED(vp, "nfsrv_lockctrl2");
1995 nfsrv_locallock_rollback(vp, lfp, p);
1997 nfsrv_unlocklf(lfp);
2004 * Check the nfsrv_getlockfile return.
2005 * Returned -1 if no structure found.
2007 if (getlckret == -1) {
2008 error = NFSERR_EXPIRED;
2010 * Called from lockt, so no lock is OK.
2012 if (new_stp->ls_flags & NFSLCK_TEST) {
2014 } else if (new_stp->ls_flags &
2015 (NFSLCK_CHECK | NFSLCK_SETATTR)) {
2017 * Called to check for a lock, OK if the stateid is all
2018 * 1s or all 0s, but there should be an nfsstate
2020 * (ie. If there is no open, I'll assume no share
2026 error = NFSERR_BADSTATEID;
2033 * For NFSLCK_CHECK and NFSLCK_LOCK, test for a share conflict.
2034 * For NFSLCK_CHECK, allow a read if write access is granted,
2035 * but check for a deny. For NFSLCK_LOCK, require correct access,
2036 * which implies a conflicting deny can't exist.
2038 if (new_stp->ls_flags & (NFSLCK_CHECK | NFSLCK_LOCK)) {
2040 * Four kinds of state id:
2041 * - specialid (all 0s or all 1s), only for NFSLCK_CHECK
2042 * - stateid for an open
2043 * - stateid for a delegation
2044 * - stateid for a lock owner
2047 if (stp->ls_flags & (NFSLCK_DELEGREAD | NFSLCK_DELEGWRITE)) {
2050 nfsrv_delaydelegtimeout(stp);
2051 } else if (stp->ls_flags & NFSLCK_OPEN) {
2054 mystp = stp->ls_openstp;
2057 * If locking or checking, require correct access
2060 if (((new_stp->ls_flags & NFSLCK_LOCK) &&
2061 !((new_lop->lo_flags >> NFSLCK_LOCKSHIFT) &
2062 mystp->ls_flags & NFSLCK_ACCESSBITS)) ||
2063 ((new_stp->ls_flags & (NFSLCK_CHECK|NFSLCK_READACCESS)) ==
2064 (NFSLCK_CHECK | NFSLCK_READACCESS) &&
2065 !(mystp->ls_flags & NFSLCK_READACCESS) &&
2066 nfsrv_allowreadforwriteopen == 0) ||
2067 ((new_stp->ls_flags & (NFSLCK_CHECK|NFSLCK_WRITEACCESS)) ==
2068 (NFSLCK_CHECK | NFSLCK_WRITEACCESS) &&
2069 !(mystp->ls_flags & NFSLCK_WRITEACCESS))) {
2070 if (filestruct_locked != 0) {
2071 /* Roll back local locks. */
2073 if (vnode_unlocked == 0) {
2074 ASSERT_VOP_ELOCKED(vp,
2079 nfsrv_locallock_rollback(vp, lfp, p);
2081 nfsrv_unlocklf(lfp);
2084 error = NFSERR_OPENMODE;
2089 if ((new_stp->ls_flags & NFSLCK_CHECK) && !delegation) {
2091 * Check for a conflicting deny bit.
2093 LIST_FOREACH(tstp, &lfp->lf_open, ls_file) {
2094 if (tstp != mystp) {
2095 bits = tstp->ls_flags;
2096 bits >>= NFSLCK_SHIFT;
2097 if (new_stp->ls_flags & bits & NFSLCK_ACCESSBITS) {
2098 KASSERT(vnode_unlocked == 0,
2099 ("nfsrv_lockctrl: vnode unlocked1"));
2100 ret = nfsrv_clientconflict(tstp->ls_clp, &haslock,
2104 * nfsrv_clientconflict unlocks state
2105 * when it returns non-zero.
2113 error = NFSERR_PERM;
2115 error = NFSERR_OPENMODE;
2121 /* We're outta here */
2128 * For setattr, just get rid of all the Delegations for other clients.
2130 if (new_stp->ls_flags & NFSLCK_SETATTR) {
2131 KASSERT(vnode_unlocked == 0,
2132 ("nfsrv_lockctrl: vnode unlocked2"));
2133 ret = nfsrv_cleandeleg(vp, lfp, clp, &haslock, p);
2136 * nfsrv_cleandeleg() unlocks state when it
2146 if (!(new_stp->ls_flags & NFSLCK_CHECK) ||
2147 (LIST_EMPTY(&lfp->lf_open) && LIST_EMPTY(&lfp->lf_lock) &&
2148 LIST_EMPTY(&lfp->lf_deleg))) {
2155 * Check for a conflicting delegation. If one is found, call
2156 * nfsrv_delegconflict() to handle it. If the v4root lock hasn't
2157 * been set yet, it will get the lock. Otherwise, it will recall
2158 * the delegation. Then, we try try again...
2159 * I currently believe the conflict algorithm to be:
2160 * For Lock Ops (Lock/LockT/LockU)
2161 * - there is a conflict iff a different client has a write delegation
2162 * For Reading (Read Op)
2163 * - there is a conflict iff a different client has a write delegation
2164 * (the specialids are always a different client)
2165 * For Writing (Write/Setattr of size)
2166 * - there is a conflict if a different client has any delegation
2167 * - there is a conflict if the same client has a read delegation
2168 * (I don't understand why this isn't allowed, but that seems to be
2169 * the current consensus?)
2171 tstp = LIST_FIRST(&lfp->lf_deleg);
2172 while (tstp != LIST_END(&lfp->lf_deleg)) {
2173 nstp = LIST_NEXT(tstp, ls_file);
2174 if ((((new_stp->ls_flags&(NFSLCK_LOCK|NFSLCK_UNLOCK|NFSLCK_TEST))||
2175 ((new_stp->ls_flags & NFSLCK_CHECK) &&
2176 (new_lop->lo_flags & NFSLCK_READ))) &&
2177 clp != tstp->ls_clp &&
2178 (tstp->ls_flags & NFSLCK_DELEGWRITE)) ||
2179 ((new_stp->ls_flags & NFSLCK_CHECK) &&
2180 (new_lop->lo_flags & NFSLCK_WRITE) &&
2181 (clp != tstp->ls_clp ||
2182 (tstp->ls_flags & NFSLCK_DELEGREAD)))) {
2184 if (filestruct_locked != 0) {
2185 /* Roll back local locks. */
2187 if (vnode_unlocked == 0) {
2188 ASSERT_VOP_ELOCKED(vp, "nfsrv_lockctrl4");
2191 nfsrv_locallock_rollback(vp, lfp, p);
2193 nfsrv_unlocklf(lfp);
2195 NFSVOPLOCK(vp, LK_EXCLUSIVE | LK_RETRY);
2197 if (VN_IS_DOOMED(vp))
2198 ret = NFSERR_SERVERFAULT;
2202 ret = nfsrv_delegconflict(tstp, &haslock, p, vp);
2205 * nfsrv_delegconflict unlocks state when it
2206 * returns non-zero, which it always does.
2209 free(other_lop, M_NFSDLOCK);
2219 /* Never gets here. */
2225 * Handle the unlock case by calling nfsrv_updatelock().
2226 * (Should I have done some access checking above for unlock? For now,
2227 * just let it happen.)
2229 if (new_stp->ls_flags & NFSLCK_UNLOCK) {
2230 first = new_lop->lo_first;
2231 end = new_lop->lo_end;
2232 nfsrv_updatelock(stp, new_lopp, &other_lop, lfp);
2233 stateidp->seqid = ++(stp->ls_stateid.seqid);
2234 if ((nd->nd_flag & ND_NFSV41) != 0 && stateidp->seqid == 0)
2235 stateidp->seqid = stp->ls_stateid.seqid = 1;
2236 stateidp->other[0] = stp->ls_stateid.other[0];
2237 stateidp->other[1] = stp->ls_stateid.other[1];
2238 stateidp->other[2] = stp->ls_stateid.other[2];
2239 if (filestruct_locked != 0) {
2241 if (vnode_unlocked == 0) {
2242 ASSERT_VOP_ELOCKED(vp, "nfsrv_lockctrl5");
2246 /* Update the local locks. */
2247 nfsrv_localunlock(vp, lfp, first, end, p);
2249 nfsrv_unlocklf(lfp);
2256 * Search for a conflicting lock. A lock conflicts if:
2257 * - the lock range overlaps and
2258 * - at least one lock is a write lock and
2259 * - it is not owned by the same lock owner
2262 LIST_FOREACH(lop, &lfp->lf_lock, lo_lckfile) {
2263 if (new_lop->lo_end > lop->lo_first &&
2264 new_lop->lo_first < lop->lo_end &&
2265 (new_lop->lo_flags == NFSLCK_WRITE ||
2266 lop->lo_flags == NFSLCK_WRITE) &&
2267 lckstp != lop->lo_stp &&
2268 (clp != lop->lo_stp->ls_clp ||
2269 lckstp->ls_ownerlen != lop->lo_stp->ls_ownerlen ||
2270 NFSBCMP(lckstp->ls_owner, lop->lo_stp->ls_owner,
2271 lckstp->ls_ownerlen))) {
2273 free(other_lop, M_NFSDLOCK);
2276 if (vnode_unlocked != 0)
2277 ret = nfsrv_clientconflict(lop->lo_stp->ls_clp, &haslock,
2280 ret = nfsrv_clientconflict(lop->lo_stp->ls_clp, &haslock,
2283 if (filestruct_locked != 0) {
2284 if (vnode_unlocked == 0) {
2285 ASSERT_VOP_ELOCKED(vp, "nfsrv_lockctrl6");
2288 /* Roll back local locks. */
2289 nfsrv_locallock_rollback(vp, lfp, p);
2291 nfsrv_unlocklf(lfp);
2293 NFSVOPLOCK(vp, LK_EXCLUSIVE | LK_RETRY);
2295 if (VN_IS_DOOMED(vp)) {
2296 error = NFSERR_SERVERFAULT;
2301 * nfsrv_clientconflict() unlocks state when it
2308 * Found a conflicting lock, so record the conflict and
2311 if (cfp != NULL && ret == 0) {
2312 cfp->cl_clientid.lval[0]=lop->lo_stp->ls_stateid.other[0];
2313 cfp->cl_clientid.lval[1]=lop->lo_stp->ls_stateid.other[1];
2314 cfp->cl_first = lop->lo_first;
2315 cfp->cl_end = lop->lo_end;
2316 cfp->cl_flags = lop->lo_flags;
2317 cfp->cl_ownerlen = lop->lo_stp->ls_ownerlen;
2318 NFSBCOPY(lop->lo_stp->ls_owner, cfp->cl_owner,
2322 error = NFSERR_PERM;
2323 else if (new_stp->ls_flags & NFSLCK_RECLAIM)
2324 error = NFSERR_RECLAIMCONFLICT;
2325 else if (new_stp->ls_flags & NFSLCK_CHECK)
2326 error = NFSERR_LOCKED;
2328 error = NFSERR_DENIED;
2329 if (filestruct_locked != 0 && ret == 0) {
2330 /* Roll back local locks. */
2332 if (vnode_unlocked == 0) {
2333 ASSERT_VOP_ELOCKED(vp, "nfsrv_lockctrl7");
2337 nfsrv_locallock_rollback(vp, lfp, p);
2339 nfsrv_unlocklf(lfp);
2349 * We only get here if there was no lock that conflicted.
2351 if (new_stp->ls_flags & (NFSLCK_TEST | NFSLCK_CHECK)) {
2357 * We only get here when we are creating or modifying a lock.
2358 * There are two variants:
2359 * - exist_lock_owner where lock_owner exists
2360 * - open_to_lock_owner with new lock_owner
2362 first = new_lop->lo_first;
2363 end = new_lop->lo_end;
2364 lock_flags = new_lop->lo_flags;
2365 if (!(new_stp->ls_flags & NFSLCK_OPENTOLOCK)) {
2366 nfsrv_updatelock(lckstp, new_lopp, &other_lop, lfp);
2367 stateidp->seqid = ++(lckstp->ls_stateid.seqid);
2368 if ((nd->nd_flag & ND_NFSV41) != 0 && stateidp->seqid == 0)
2369 stateidp->seqid = lckstp->ls_stateid.seqid = 1;
2370 stateidp->other[0] = lckstp->ls_stateid.other[0];
2371 stateidp->other[1] = lckstp->ls_stateid.other[1];
2372 stateidp->other[2] = lckstp->ls_stateid.other[2];
2375 * The new open_to_lock_owner case.
2376 * Link the new nfsstate into the lists.
2378 new_stp->ls_seq = new_stp->ls_opentolockseq;
2379 nfsrvd_refcache(new_stp->ls_op);
2380 stateidp->seqid = new_stp->ls_stateid.seqid = 1;
2381 stateidp->other[0] = new_stp->ls_stateid.other[0] =
2382 clp->lc_clientid.lval[0];
2383 stateidp->other[1] = new_stp->ls_stateid.other[1] =
2384 clp->lc_clientid.lval[1];
2385 stateidp->other[2] = new_stp->ls_stateid.other[2] =
2386 nfsrv_nextstateindex(clp);
2387 new_stp->ls_clp = clp;
2388 LIST_INIT(&new_stp->ls_lock);
2389 new_stp->ls_openstp = stp;
2390 new_stp->ls_lfp = lfp;
2391 nfsrv_insertlock(new_lop, (struct nfslock *)new_stp, new_stp,
2393 LIST_INSERT_HEAD(NFSSTATEHASH(clp, new_stp->ls_stateid),
2395 LIST_INSERT_HEAD(&stp->ls_open, new_stp, ls_list);
2398 NFSD_VNET(nfsstatsv1_p)->srvlockowners++;
2399 nfsrv_openpluslock++;
2401 if (filestruct_locked != 0) {
2403 nfsrv_locallock_commit(lfp, lock_flags, first, end);
2405 nfsrv_unlocklf(lfp);
2411 NFSLOCKV4ROOTMUTEX();
2412 nfsv4_unlock(&nfsv4rootfs_lock, 1);
2413 NFSUNLOCKV4ROOTMUTEX();
2415 if (vnode_unlocked != 0) {
2416 NFSVOPLOCK(vp, LK_EXCLUSIVE | LK_RETRY);
2417 if (error == 0 && VN_IS_DOOMED(vp))
2418 error = NFSERR_SERVERFAULT;
2421 free(other_lop, M_NFSDLOCK);
2422 NFSEXITCODE2(error, nd);
2427 * Check for state errors for Open.
2428 * repstat is passed back out as an error if more critical errors
2432 nfsrv_opencheck(nfsquad_t clientid, nfsv4stateid_t *stateidp,
2433 struct nfsstate *new_stp, vnode_t vp, struct nfsrv_descript *nd,
2434 NFSPROC_T *p, int repstat)
2436 struct nfsstate *stp, *nstp;
2437 struct nfsclient *clp;
2438 struct nfsstate *ownerstp;
2439 struct nfslockfile *lfp, *new_lfp;
2440 int error = 0, haslock = 0, ret, readonly = 0, getfhret = 0;
2442 if ((new_stp->ls_flags & NFSLCK_SHAREBITS) == NFSLCK_READACCESS)
2445 * Check for restart conditions (client and server).
2447 error = nfsrv_checkrestart(clientid, new_stp->ls_flags,
2448 &new_stp->ls_stateid, 0);
2453 * Check for state resource limit exceeded.
2454 * Technically this should be SMP protected, but the worst
2455 * case error is "out by one or two" on the count when it
2456 * returns NFSERR_RESOURCE and the limit is just a rather
2457 * arbitrary high water mark, so no harm is done.
2459 if (nfsrv_openpluslock > nfsrv_v4statelimit) {
2460 error = NFSERR_RESOURCE;
2465 new_lfp = malloc(sizeof (struct nfslockfile),
2466 M_NFSDLOCKFILE, M_WAITOK);
2468 getfhret = nfsrv_getlockfh(vp, new_stp->ls_flags, new_lfp,
2472 * Get the nfsclient structure.
2474 error = nfsrv_getclient(clientid, CLOPS_RENEW, &clp, NULL,
2475 (nfsquad_t)((u_quad_t)0), 0, nd, p);
2478 * Look up the open owner. See if it needs confirmation and
2479 * check the seq#, as required.
2482 nfsrv_getowner(&clp->lc_open, new_stp, &ownerstp);
2484 if (!error && ownerstp) {
2485 error = nfsrv_checkseqid(nd, new_stp->ls_seq, ownerstp,
2488 * If the OpenOwner hasn't been confirmed, assume the
2489 * old one was a replay and this one is ok.
2490 * See: RFC3530 Sec. 14.2.18.
2492 if (error == NFSERR_BADSEQID &&
2493 (ownerstp->ls_flags & NFSLCK_NEEDSCONFIRM))
2501 error = nfsrv_checkgrace(nd, clp, new_stp->ls_flags);
2502 if ((new_stp->ls_flags & NFSLCK_RECLAIM) && !error &&
2503 nfsrv_checkstable(clp))
2504 error = NFSERR_NOGRACE;
2507 * If none of the above errors occurred, let repstat be
2510 if (repstat && !error)
2515 NFSLOCKV4ROOTMUTEX();
2516 nfsv4_unlock(&nfsv4rootfs_lock, 1);
2517 NFSUNLOCKV4ROOTMUTEX();
2519 free(new_lfp, M_NFSDLOCKFILE);
2524 * If vp == NULL, the file doesn't exist yet, so return ok.
2525 * (This always happens on the first pass, so haslock must be 0.)
2529 free(new_lfp, M_NFSDLOCKFILE);
2534 * Get the structure for the underlying file.
2539 error = nfsrv_getlockfile(new_stp->ls_flags, &new_lfp, &lfp,
2542 free(new_lfp, M_NFSDLOCKFILE);
2546 NFSLOCKV4ROOTMUTEX();
2547 nfsv4_unlock(&nfsv4rootfs_lock, 1);
2548 NFSUNLOCKV4ROOTMUTEX();
2554 * Search for a conflicting open/share.
2556 if (new_stp->ls_flags & NFSLCK_DELEGCUR) {
2558 * For Delegate_Cur, search for the matching Delegation,
2559 * which indicates no conflict.
2560 * An old delegation should have been recovered by the
2561 * client doing a Claim_DELEGATE_Prev, so I won't let
2562 * it match and return NFSERR_EXPIRED. Should I let it
2565 LIST_FOREACH(stp, &lfp->lf_deleg, ls_file) {
2566 if (!(stp->ls_flags & NFSLCK_OLDDELEG) &&
2567 (((nd->nd_flag & ND_NFSV41) != 0 &&
2568 stateidp->seqid == 0) ||
2569 stateidp->seqid == stp->ls_stateid.seqid) &&
2570 !NFSBCMP(stateidp->other, stp->ls_stateid.other,
2574 if (stp == LIST_END(&lfp->lf_deleg) ||
2575 ((new_stp->ls_flags & NFSLCK_WRITEACCESS) &&
2576 (stp->ls_flags & NFSLCK_DELEGREAD))) {
2579 NFSLOCKV4ROOTMUTEX();
2580 nfsv4_unlock(&nfsv4rootfs_lock, 1);
2581 NFSUNLOCKV4ROOTMUTEX();
2583 error = NFSERR_EXPIRED;
2589 * Check for access/deny bit conflicts. I check for the same
2590 * owner as well, in case the client didn't bother.
2592 LIST_FOREACH(stp, &lfp->lf_open, ls_file) {
2593 if (!(new_stp->ls_flags & NFSLCK_DELEGCUR) &&
2594 (((new_stp->ls_flags & NFSLCK_ACCESSBITS) &
2595 ((stp->ls_flags>>NFSLCK_SHIFT) & NFSLCK_ACCESSBITS))||
2596 ((stp->ls_flags & NFSLCK_ACCESSBITS) &
2597 ((new_stp->ls_flags>>NFSLCK_SHIFT)&NFSLCK_ACCESSBITS)))){
2598 ret = nfsrv_clientconflict(stp->ls_clp,&haslock,vp,p);
2601 * nfsrv_clientconflict() unlocks
2602 * state when it returns non-zero.
2607 error = NFSERR_PERM;
2608 else if (new_stp->ls_flags & NFSLCK_RECLAIM)
2609 error = NFSERR_RECLAIMCONFLICT;
2611 error = NFSERR_SHAREDENIED;
2615 NFSLOCKV4ROOTMUTEX();
2616 nfsv4_unlock(&nfsv4rootfs_lock, 1);
2617 NFSUNLOCKV4ROOTMUTEX();
2624 * Check for a conflicting delegation. If one is found, call
2625 * nfsrv_delegconflict() to handle it. If the v4root lock hasn't
2626 * been set yet, it will get the lock. Otherwise, it will recall
2627 * the delegation. Then, we try try again...
2628 * (If NFSLCK_DELEGCUR is set, it has a delegation, so there
2629 * isn't a conflict.)
2630 * I currently believe the conflict algorithm to be:
2631 * For Open with Read Access and Deny None
2632 * - there is a conflict iff a different client has a write delegation
2633 * For Open with other Write Access or any Deny except None
2634 * - there is a conflict if a different client has any delegation
2635 * - there is a conflict if the same client has a read delegation
2636 * (The current consensus is that this last case should be
2637 * considered a conflict since the client with a read delegation
2638 * could have done an Open with ReadAccess and WriteDeny
2639 * locally and then not have checked for the WriteDeny.)
2640 * Don't check for a Reclaim, since that will be dealt with
2641 * by nfsrv_openctrl().
2643 if (!(new_stp->ls_flags &
2644 (NFSLCK_DELEGPREV | NFSLCK_DELEGCUR | NFSLCK_RECLAIM))) {
2645 stp = LIST_FIRST(&lfp->lf_deleg);
2646 while (stp != LIST_END(&lfp->lf_deleg)) {
2647 nstp = LIST_NEXT(stp, ls_file);
2648 if ((readonly && stp->ls_clp != clp &&
2649 (stp->ls_flags & NFSLCK_DELEGWRITE)) ||
2650 (!readonly && (stp->ls_clp != clp ||
2651 (stp->ls_flags & NFSLCK_DELEGREAD)))) {
2652 ret = nfsrv_delegconflict(stp, &haslock, p, vp);
2655 * nfsrv_delegconflict() unlocks state
2656 * when it returns non-zero.
2669 NFSLOCKV4ROOTMUTEX();
2670 nfsv4_unlock(&nfsv4rootfs_lock, 1);
2671 NFSUNLOCKV4ROOTMUTEX();
2675 NFSEXITCODE2(error, nd);
2680 * Open control function to create/update open state for an open.
2683 nfsrv_openctrl(struct nfsrv_descript *nd, vnode_t vp,
2684 struct nfsstate **new_stpp, nfsquad_t clientid, nfsv4stateid_t *stateidp,
2685 nfsv4stateid_t *delegstateidp, u_int32_t *rflagsp, struct nfsexstuff *exp,
2686 NFSPROC_T *p, u_quad_t filerev)
2688 struct nfsstate *new_stp = *new_stpp;
2689 struct nfsstate *stp, *nstp;
2690 struct nfsstate *openstp = NULL, *new_open, *ownerstp, *new_deleg;
2691 struct nfslockfile *lfp, *new_lfp;
2692 struct nfsclient *clp;
2693 int error = 0, haslock = 0, ret, delegate = 1, writedeleg = 1;
2694 int readonly = 0, cbret = 1, getfhret = 0;
2695 int gotstate = 0, len = 0;
2696 u_char *clidp = NULL;
2698 if ((new_stp->ls_flags & NFSLCK_SHAREBITS) == NFSLCK_READACCESS)
2701 * Check for restart conditions (client and server).
2702 * (Paranoia, should have been detected by nfsrv_opencheck().)
2703 * If an error does show up, return NFSERR_EXPIRED, since the
2704 * the seqid# has already been incremented.
2706 error = nfsrv_checkrestart(clientid, new_stp->ls_flags,
2707 &new_stp->ls_stateid, 0);
2709 printf("Nfsd: openctrl unexpected restart err=%d\n",
2711 error = NFSERR_EXPIRED;
2715 clidp = malloc(NFSV4_OPAQUELIMIT, M_TEMP, M_WAITOK);
2717 new_lfp = malloc(sizeof (struct nfslockfile),
2718 M_NFSDLOCKFILE, M_WAITOK);
2719 new_open = malloc(sizeof (struct nfsstate),
2720 M_NFSDSTATE, M_WAITOK);
2721 new_deleg = malloc(sizeof (struct nfsstate),
2722 M_NFSDSTATE, M_WAITOK);
2723 getfhret = nfsrv_getlockfh(vp, new_stp->ls_flags, new_lfp,
2727 * Get the client structure. Since the linked lists could be changed
2728 * by other nfsd processes if this process does a tsleep(), one of
2729 * two things must be done.
2730 * 1 - don't tsleep()
2732 * 2 - get the nfsv4_lock() { indicated by haslock == 1 }
2733 * before using the lists, since this lock stops the other
2734 * nfsd. This should only be used for rare cases, since it
2735 * essentially single threads the nfsd.
2736 * At this time, it is only done for cases where the stable
2737 * storage file must be written prior to completion of state
2740 error = nfsrv_getclient(clientid, CLOPS_RENEW, &clp, NULL,
2741 (nfsquad_t)((u_quad_t)0), 0, nd, p);
2742 if (!error && (clp->lc_flags & LCL_NEEDSCBNULL) &&
2745 * This happens on the first open for a client
2746 * that supports callbacks.
2750 * Although nfsrv_docallback() will sleep, clp won't
2751 * go away, since they are only removed when the
2752 * nfsv4_lock() has blocked the nfsd threads. The
2753 * fields in clp can change, but having multiple
2754 * threads do this Null callback RPC should be
2757 cbret = nfsrv_docallback(clp, NFSV4PROC_CBNULL,
2758 NULL, 0, NULL, NULL, NULL, 0, p);
2760 clp->lc_flags &= ~LCL_NEEDSCBNULL;
2762 clp->lc_flags |= LCL_CALLBACKSON;
2766 * Look up the open owner. See if it needs confirmation and
2767 * check the seq#, as required.
2770 nfsrv_getowner(&clp->lc_open, new_stp, &ownerstp);
2774 printf("Nfsd: openctrl unexpected state err=%d\n",
2776 free(new_lfp, M_NFSDLOCKFILE);
2777 free(new_open, M_NFSDSTATE);
2778 free(new_deleg, M_NFSDSTATE);
2780 NFSLOCKV4ROOTMUTEX();
2781 nfsv4_unlock(&nfsv4rootfs_lock, 1);
2782 NFSUNLOCKV4ROOTMUTEX();
2784 error = NFSERR_EXPIRED;
2788 if (new_stp->ls_flags & NFSLCK_RECLAIM)
2789 nfsrv_markstable(clp);
2792 * Get the structure for the underlying file.
2797 error = nfsrv_getlockfile(new_stp->ls_flags, &new_lfp, &lfp,
2800 free(new_lfp, M_NFSDLOCKFILE);
2803 printf("Nfsd openctrl unexpected getlockfile err=%d\n",
2805 free(new_open, M_NFSDSTATE);
2806 free(new_deleg, M_NFSDSTATE);
2808 NFSLOCKV4ROOTMUTEX();
2809 nfsv4_unlock(&nfsv4rootfs_lock, 1);
2810 NFSUNLOCKV4ROOTMUTEX();
2816 * Search for a conflicting open/share.
2818 if (new_stp->ls_flags & NFSLCK_DELEGCUR) {
2820 * For Delegate_Cur, search for the matching Delegation,
2821 * which indicates no conflict.
2822 * An old delegation should have been recovered by the
2823 * client doing a Claim_DELEGATE_Prev, so I won't let
2824 * it match and return NFSERR_EXPIRED. Should I let it
2827 LIST_FOREACH(stp, &lfp->lf_deleg, ls_file) {
2828 if (!(stp->ls_flags & NFSLCK_OLDDELEG) &&
2829 (((nd->nd_flag & ND_NFSV41) != 0 &&
2830 stateidp->seqid == 0) ||
2831 stateidp->seqid == stp->ls_stateid.seqid) &&
2832 !NFSBCMP(stateidp->other, stp->ls_stateid.other,
2836 if (stp == LIST_END(&lfp->lf_deleg) ||
2837 ((new_stp->ls_flags & NFSLCK_WRITEACCESS) &&
2838 (stp->ls_flags & NFSLCK_DELEGREAD))) {
2840 printf("Nfsd openctrl unexpected expiry\n");
2841 free(new_open, M_NFSDSTATE);
2842 free(new_deleg, M_NFSDSTATE);
2844 NFSLOCKV4ROOTMUTEX();
2845 nfsv4_unlock(&nfsv4rootfs_lock, 1);
2846 NFSUNLOCKV4ROOTMUTEX();
2848 error = NFSERR_EXPIRED;
2853 * Don't issue a Delegation, since one already exists and
2854 * delay delegation timeout, as required.
2857 nfsrv_delaydelegtimeout(stp);
2861 * Check for access/deny bit conflicts. I also check for the
2862 * same owner, since the client might not have bothered to check.
2863 * Also, note an open for the same file and owner, if found,
2864 * which is all we do here for Delegate_Cur, since conflict
2865 * checking is already done.
2867 LIST_FOREACH(stp, &lfp->lf_open, ls_file) {
2868 if (ownerstp && stp->ls_openowner == ownerstp)
2870 if (!(new_stp->ls_flags & NFSLCK_DELEGCUR)) {
2872 * If another client has the file open, the only
2873 * delegation that can be issued is a Read delegation
2874 * and only if it is a Read open with Deny none.
2876 if (clp != stp->ls_clp) {
2877 if ((stp->ls_flags & NFSLCK_SHAREBITS) ==
2883 if(((new_stp->ls_flags & NFSLCK_ACCESSBITS) &
2884 ((stp->ls_flags>>NFSLCK_SHIFT) & NFSLCK_ACCESSBITS))||
2885 ((stp->ls_flags & NFSLCK_ACCESSBITS) &
2886 ((new_stp->ls_flags>>NFSLCK_SHIFT)&NFSLCK_ACCESSBITS))){
2887 ret = nfsrv_clientconflict(stp->ls_clp,&haslock,vp,p);
2890 * nfsrv_clientconflict() unlocks state
2891 * when it returns non-zero.
2893 free(new_open, M_NFSDSTATE);
2894 free(new_deleg, M_NFSDSTATE);
2899 error = NFSERR_PERM;
2900 else if (new_stp->ls_flags & NFSLCK_RECLAIM)
2901 error = NFSERR_RECLAIMCONFLICT;
2903 error = NFSERR_SHAREDENIED;
2907 NFSLOCKV4ROOTMUTEX();
2908 nfsv4_unlock(&nfsv4rootfs_lock, 1);
2909 NFSUNLOCKV4ROOTMUTEX();
2911 free(new_open, M_NFSDSTATE);
2912 free(new_deleg, M_NFSDSTATE);
2913 printf("nfsd openctrl unexpected client cnfl\n");
2920 * Check for a conflicting delegation. If one is found, call
2921 * nfsrv_delegconflict() to handle it. If the v4root lock hasn't
2922 * been set yet, it will get the lock. Otherwise, it will recall
2923 * the delegation. Then, we try try again...
2924 * (If NFSLCK_DELEGCUR is set, it has a delegation, so there
2925 * isn't a conflict.)
2926 * I currently believe the conflict algorithm to be:
2927 * For Open with Read Access and Deny None
2928 * - there is a conflict iff a different client has a write delegation
2929 * For Open with other Write Access or any Deny except None
2930 * - there is a conflict if a different client has any delegation
2931 * - there is a conflict if the same client has a read delegation
2932 * (The current consensus is that this last case should be
2933 * considered a conflict since the client with a read delegation
2934 * could have done an Open with ReadAccess and WriteDeny
2935 * locally and then not have checked for the WriteDeny.)
2937 if (!(new_stp->ls_flags & (NFSLCK_DELEGPREV | NFSLCK_DELEGCUR))) {
2938 stp = LIST_FIRST(&lfp->lf_deleg);
2939 while (stp != LIST_END(&lfp->lf_deleg)) {
2940 nstp = LIST_NEXT(stp, ls_file);
2941 if (stp->ls_clp != clp && (stp->ls_flags & NFSLCK_DELEGREAD))
2945 if ((readonly && stp->ls_clp != clp &&
2946 (stp->ls_flags & NFSLCK_DELEGWRITE)) ||
2947 (!readonly && (stp->ls_clp != clp ||
2948 (stp->ls_flags & NFSLCK_DELEGREAD)))) {
2949 if (new_stp->ls_flags & NFSLCK_RECLAIM) {
2952 ret = nfsrv_delegconflict(stp, &haslock, p, vp);
2955 * nfsrv_delegconflict() unlocks state
2956 * when it returns non-zero.
2958 printf("Nfsd openctrl unexpected deleg cnfl\n");
2959 free(new_open, M_NFSDSTATE);
2960 free(new_deleg, M_NFSDSTATE);
2975 * We only get here if there was no open that conflicted.
2976 * If an open for the owner exists, or in the access/deny bits.
2977 * Otherwise it is a new open. If the open_owner hasn't been
2978 * confirmed, replace the open with the new one needing confirmation,
2979 * otherwise add the open.
2981 if (new_stp->ls_flags & NFSLCK_DELEGPREV) {
2983 * Handle NFSLCK_DELEGPREV by searching the old delegations for
2984 * a match. If found, just move the old delegation to the current
2985 * delegation list and issue open. If not found, return
2988 LIST_FOREACH(stp, &clp->lc_olddeleg, ls_list) {
2989 if (stp->ls_lfp == lfp) {
2991 if (stp->ls_clp != clp)
2992 panic("olddeleg clp");
2993 LIST_REMOVE(stp, ls_list);
2994 LIST_REMOVE(stp, ls_hash);
2995 stp->ls_flags &= ~NFSLCK_OLDDELEG;
2996 stp->ls_stateid.seqid = delegstateidp->seqid = 1;
2997 stp->ls_stateid.other[0] = delegstateidp->other[0] =
2998 clp->lc_clientid.lval[0];
2999 stp->ls_stateid.other[1] = delegstateidp->other[1] =
3000 clp->lc_clientid.lval[1];
3001 stp->ls_stateid.other[2] = delegstateidp->other[2] =
3002 nfsrv_nextstateindex(clp);
3003 stp->ls_compref = nd->nd_compref;
3004 LIST_INSERT_HEAD(&clp->lc_deleg, stp, ls_list);
3005 LIST_INSERT_HEAD(NFSSTATEHASH(clp,
3006 stp->ls_stateid), stp, ls_hash);
3007 if (stp->ls_flags & NFSLCK_DELEGWRITE)
3008 *rflagsp |= NFSV4OPEN_WRITEDELEGATE;
3010 *rflagsp |= NFSV4OPEN_READDELEGATE;
3011 clp->lc_delegtime = NFSD_MONOSEC +
3012 nfsrv_lease + NFSRV_LEASEDELTA;
3015 * Now, do the associated open.
3017 new_open->ls_stateid.seqid = 1;
3018 new_open->ls_stateid.other[0] = clp->lc_clientid.lval[0];
3019 new_open->ls_stateid.other[1] = clp->lc_clientid.lval[1];
3020 new_open->ls_stateid.other[2] = nfsrv_nextstateindex(clp);
3021 new_open->ls_flags = (new_stp->ls_flags&NFSLCK_DENYBITS)|
3023 if (stp->ls_flags & NFSLCK_DELEGWRITE)
3024 new_open->ls_flags |= (NFSLCK_READACCESS |
3025 NFSLCK_WRITEACCESS);
3027 new_open->ls_flags |= NFSLCK_READACCESS;
3028 new_open->ls_uid = new_stp->ls_uid;
3029 new_open->ls_lfp = lfp;
3030 new_open->ls_clp = clp;
3031 LIST_INIT(&new_open->ls_open);
3032 LIST_INSERT_HEAD(&lfp->lf_open, new_open, ls_file);
3033 LIST_INSERT_HEAD(NFSSTATEHASH(clp, new_open->ls_stateid),
3036 * and handle the open owner
3039 new_open->ls_openowner = ownerstp;
3040 LIST_INSERT_HEAD(&ownerstp->ls_open,new_open,ls_list);
3042 new_open->ls_openowner = new_stp;
3043 new_stp->ls_flags = 0;
3044 nfsrvd_refcache(new_stp->ls_op);
3045 new_stp->ls_noopens = 0;
3046 LIST_INIT(&new_stp->ls_open);
3047 LIST_INSERT_HEAD(&new_stp->ls_open, new_open, ls_list);
3048 LIST_INSERT_HEAD(&clp->lc_open, new_stp, ls_list);
3050 NFSD_VNET(nfsstatsv1_p)->srvopenowners++;
3051 nfsrv_openpluslock++;
3055 NFSD_VNET(nfsstatsv1_p)->srvopens++;
3056 nfsrv_openpluslock++;
3060 if (stp == LIST_END(&clp->lc_olddeleg))
3061 error = NFSERR_EXPIRED;
3062 } else if (new_stp->ls_flags & (NFSLCK_DELEGREAD | NFSLCK_DELEGWRITE)) {
3064 * Scan to see that no delegation for this client and file
3065 * doesn't already exist.
3066 * There also shouldn't yet be an Open for this file and
3069 LIST_FOREACH(stp, &lfp->lf_deleg, ls_file) {
3070 if (stp->ls_clp == clp)
3073 if (stp == LIST_END(&lfp->lf_deleg) && openstp == NULL) {
3075 * This is the Claim_Previous case with a delegation
3076 * type != Delegate_None.
3079 * First, add the delegation. (Although we must issue the
3080 * delegation, we can also ask for an immediate return.)
3082 new_deleg->ls_stateid.seqid = delegstateidp->seqid = 1;
3083 new_deleg->ls_stateid.other[0] = delegstateidp->other[0] =
3084 clp->lc_clientid.lval[0];
3085 new_deleg->ls_stateid.other[1] = delegstateidp->other[1] =
3086 clp->lc_clientid.lval[1];
3087 new_deleg->ls_stateid.other[2] = delegstateidp->other[2] =
3088 nfsrv_nextstateindex(clp);
3089 if (new_stp->ls_flags & NFSLCK_DELEGWRITE) {
3090 new_deleg->ls_flags = (NFSLCK_DELEGWRITE |
3091 NFSLCK_READACCESS | NFSLCK_WRITEACCESS);
3092 *rflagsp |= NFSV4OPEN_WRITEDELEGATE;
3093 nfsrv_writedelegcnt++;
3095 new_deleg->ls_flags = (NFSLCK_DELEGREAD |
3097 *rflagsp |= NFSV4OPEN_READDELEGATE;
3099 new_deleg->ls_uid = new_stp->ls_uid;
3100 new_deleg->ls_lfp = lfp;
3101 new_deleg->ls_clp = clp;
3102 new_deleg->ls_filerev = filerev;
3103 new_deleg->ls_compref = nd->nd_compref;
3104 new_deleg->ls_lastrecall = 0;
3105 LIST_INSERT_HEAD(&lfp->lf_deleg, new_deleg, ls_file);
3106 LIST_INSERT_HEAD(NFSSTATEHASH(clp,
3107 new_deleg->ls_stateid), new_deleg, ls_hash);
3108 LIST_INSERT_HEAD(&clp->lc_deleg, new_deleg, ls_list);
3110 if (delegate == 2 || nfsrv_issuedelegs == 0 ||
3111 (clp->lc_flags & (LCL_CALLBACKSON | LCL_CBDOWN)) !=
3113 NFSRV_V4DELEGLIMIT(nfsrv_delegatecnt) ||
3114 !NFSVNO_DELEGOK(vp))
3115 *rflagsp |= NFSV4OPEN_RECALL;
3116 NFSD_VNET(nfsstatsv1_p)->srvdelegates++;
3117 nfsrv_openpluslock++;
3118 nfsrv_delegatecnt++;
3121 * Now, do the associated open.
3123 new_open->ls_stateid.seqid = 1;
3124 new_open->ls_stateid.other[0] = clp->lc_clientid.lval[0];
3125 new_open->ls_stateid.other[1] = clp->lc_clientid.lval[1];
3126 new_open->ls_stateid.other[2] = nfsrv_nextstateindex(clp);
3127 new_open->ls_flags = (new_stp->ls_flags & NFSLCK_DENYBITS) |
3129 if (new_stp->ls_flags & NFSLCK_DELEGWRITE)
3130 new_open->ls_flags |= (NFSLCK_READACCESS |
3131 NFSLCK_WRITEACCESS);
3133 new_open->ls_flags |= NFSLCK_READACCESS;
3134 new_open->ls_uid = new_stp->ls_uid;
3135 new_open->ls_lfp = lfp;
3136 new_open->ls_clp = clp;
3137 LIST_INIT(&new_open->ls_open);
3138 LIST_INSERT_HEAD(&lfp->lf_open, new_open, ls_file);
3139 LIST_INSERT_HEAD(NFSSTATEHASH(clp, new_open->ls_stateid),
3142 * and handle the open owner
3145 new_open->ls_openowner = ownerstp;
3146 LIST_INSERT_HEAD(&ownerstp->ls_open, new_open, ls_list);
3148 new_open->ls_openowner = new_stp;
3149 new_stp->ls_flags = 0;
3150 nfsrvd_refcache(new_stp->ls_op);
3151 new_stp->ls_noopens = 0;
3152 LIST_INIT(&new_stp->ls_open);
3153 LIST_INSERT_HEAD(&new_stp->ls_open, new_open, ls_list);
3154 LIST_INSERT_HEAD(&clp->lc_open, new_stp, ls_list);
3156 NFSD_VNET(nfsstatsv1_p)->srvopenowners++;
3157 nfsrv_openpluslock++;
3161 NFSD_VNET(nfsstatsv1_p)->srvopens++;
3162 nfsrv_openpluslock++;
3164 error = NFSERR_RECLAIMCONFLICT;
3166 } else if (ownerstp) {
3167 if (ownerstp->ls_flags & NFSLCK_NEEDSCONFIRM) {
3168 /* Replace the open */
3169 if (ownerstp->ls_op)
3170 nfsrvd_derefcache(ownerstp->ls_op);
3171 ownerstp->ls_op = new_stp->ls_op;
3172 nfsrvd_refcache(ownerstp->ls_op);
3173 ownerstp->ls_seq = new_stp->ls_seq;
3174 *rflagsp |= NFSV4OPEN_RESULTCONFIRM;
3175 stp = LIST_FIRST(&ownerstp->ls_open);
3176 stp->ls_flags = (new_stp->ls_flags & NFSLCK_SHAREBITS) |
3178 stp->ls_stateid.seqid = 1;
3179 stp->ls_uid = new_stp->ls_uid;
3180 if (lfp != stp->ls_lfp) {
3181 LIST_REMOVE(stp, ls_file);
3182 LIST_INSERT_HEAD(&lfp->lf_open, stp, ls_file);
3186 } else if (openstp) {
3187 openstp->ls_flags |= (new_stp->ls_flags & NFSLCK_SHAREBITS);
3188 openstp->ls_stateid.seqid++;
3189 if ((nd->nd_flag & ND_NFSV41) != 0 &&
3190 openstp->ls_stateid.seqid == 0)
3191 openstp->ls_stateid.seqid = 1;
3194 * This is where we can choose to issue a delegation.
3196 if ((new_stp->ls_flags & NFSLCK_WANTNODELEG) != 0)
3197 *rflagsp |= NFSV4OPEN_WDNOTWANTED;
3198 else if (nfsrv_issuedelegs == 0)
3199 *rflagsp |= NFSV4OPEN_WDSUPPFTYPE;
3200 else if (NFSRV_V4DELEGLIMIT(nfsrv_delegatecnt))
3201 *rflagsp |= NFSV4OPEN_WDRESOURCE;
3202 else if (delegate == 0 || writedeleg == 0 ||
3203 NFSVNO_EXRDONLY(exp) || (readonly != 0 &&
3204 nfsrv_writedelegifpos == 0) ||
3205 !NFSVNO_DELEGOK(vp) ||
3206 (new_stp->ls_flags & NFSLCK_WANTRDELEG) != 0 ||
3207 (clp->lc_flags & (LCL_CALLBACKSON | LCL_CBDOWN)) !=
3209 *rflagsp |= NFSV4OPEN_WDCONTENTION;
3211 new_deleg->ls_stateid.seqid = delegstateidp->seqid = 1;
3212 new_deleg->ls_stateid.other[0] = delegstateidp->other[0]
3213 = clp->lc_clientid.lval[0];
3214 new_deleg->ls_stateid.other[1] = delegstateidp->other[1]
3215 = clp->lc_clientid.lval[1];
3216 new_deleg->ls_stateid.other[2] = delegstateidp->other[2]
3217 = nfsrv_nextstateindex(clp);
3218 new_deleg->ls_flags = (NFSLCK_DELEGWRITE |
3219 NFSLCK_READACCESS | NFSLCK_WRITEACCESS);
3220 *rflagsp |= NFSV4OPEN_WRITEDELEGATE;
3221 new_deleg->ls_uid = new_stp->ls_uid;
3222 new_deleg->ls_lfp = lfp;
3223 new_deleg->ls_clp = clp;
3224 new_deleg->ls_filerev = filerev;
3225 new_deleg->ls_compref = nd->nd_compref;
3226 new_deleg->ls_lastrecall = 0;
3227 nfsrv_writedelegcnt++;
3228 LIST_INSERT_HEAD(&lfp->lf_deleg, new_deleg, ls_file);
3229 LIST_INSERT_HEAD(NFSSTATEHASH(clp,
3230 new_deleg->ls_stateid), new_deleg, ls_hash);
3231 LIST_INSERT_HEAD(&clp->lc_deleg, new_deleg, ls_list);
3233 NFSD_VNET(nfsstatsv1_p)->srvdelegates++;
3234 nfsrv_openpluslock++;
3235 nfsrv_delegatecnt++;
3238 new_open->ls_stateid.seqid = 1;
3239 new_open->ls_stateid.other[0] = clp->lc_clientid.lval[0];
3240 new_open->ls_stateid.other[1] = clp->lc_clientid.lval[1];
3241 new_open->ls_stateid.other[2] = nfsrv_nextstateindex(clp);
3242 new_open->ls_flags = (new_stp->ls_flags & NFSLCK_SHAREBITS)|
3244 new_open->ls_uid = new_stp->ls_uid;
3245 new_open->ls_openowner = ownerstp;
3246 new_open->ls_lfp = lfp;
3247 new_open->ls_clp = clp;
3248 LIST_INIT(&new_open->ls_open);
3249 LIST_INSERT_HEAD(&lfp->lf_open, new_open, ls_file);
3250 LIST_INSERT_HEAD(&ownerstp->ls_open, new_open, ls_list);
3251 LIST_INSERT_HEAD(NFSSTATEHASH(clp, new_open->ls_stateid),
3255 NFSD_VNET(nfsstatsv1_p)->srvopens++;
3256 nfsrv_openpluslock++;
3259 * This is where we can choose to issue a delegation.
3261 if ((new_stp->ls_flags & NFSLCK_WANTNODELEG) != 0)
3262 *rflagsp |= NFSV4OPEN_WDNOTWANTED;
3263 else if (nfsrv_issuedelegs == 0)
3264 *rflagsp |= NFSV4OPEN_WDSUPPFTYPE;
3265 else if (NFSRV_V4DELEGLIMIT(nfsrv_delegatecnt))
3266 *rflagsp |= NFSV4OPEN_WDRESOURCE;
3267 else if (delegate == 0 || (writedeleg == 0 &&
3268 readonly == 0) || !NFSVNO_DELEGOK(vp) ||
3269 (clp->lc_flags & (LCL_CALLBACKSON | LCL_CBDOWN)) !=
3271 *rflagsp |= NFSV4OPEN_WDCONTENTION;
3273 new_deleg->ls_stateid.seqid = delegstateidp->seqid = 1;
3274 new_deleg->ls_stateid.other[0] = delegstateidp->other[0]
3275 = clp->lc_clientid.lval[0];
3276 new_deleg->ls_stateid.other[1] = delegstateidp->other[1]
3277 = clp->lc_clientid.lval[1];
3278 new_deleg->ls_stateid.other[2] = delegstateidp->other[2]
3279 = nfsrv_nextstateindex(clp);
3280 if (writedeleg && !NFSVNO_EXRDONLY(exp) &&
3281 (nfsrv_writedelegifpos || !readonly) &&
3282 (new_stp->ls_flags & NFSLCK_WANTRDELEG) == 0) {
3283 new_deleg->ls_flags = (NFSLCK_DELEGWRITE |
3284 NFSLCK_READACCESS | NFSLCK_WRITEACCESS);
3285 *rflagsp |= NFSV4OPEN_WRITEDELEGATE;
3286 nfsrv_writedelegcnt++;
3288 new_deleg->ls_flags = (NFSLCK_DELEGREAD |
3290 *rflagsp |= NFSV4OPEN_READDELEGATE;
3292 new_deleg->ls_uid = new_stp->ls_uid;
3293 new_deleg->ls_lfp = lfp;
3294 new_deleg->ls_clp = clp;
3295 new_deleg->ls_filerev = filerev;
3296 new_deleg->ls_compref = nd->nd_compref;
3297 new_deleg->ls_lastrecall = 0;
3298 LIST_INSERT_HEAD(&lfp->lf_deleg, new_deleg, ls_file);
3299 LIST_INSERT_HEAD(NFSSTATEHASH(clp,
3300 new_deleg->ls_stateid), new_deleg, ls_hash);
3301 LIST_INSERT_HEAD(&clp->lc_deleg, new_deleg, ls_list);
3303 NFSD_VNET(nfsstatsv1_p)->srvdelegates++;
3304 nfsrv_openpluslock++;
3305 nfsrv_delegatecnt++;
3310 * New owner case. Start the open_owner sequence with a
3311 * Needs confirmation (unless a reclaim) and hang the
3314 new_open->ls_stateid.seqid = 1;
3315 new_open->ls_stateid.other[0] = clp->lc_clientid.lval[0];
3316 new_open->ls_stateid.other[1] = clp->lc_clientid.lval[1];
3317 new_open->ls_stateid.other[2] = nfsrv_nextstateindex(clp);
3318 new_open->ls_flags = (new_stp->ls_flags & NFSLCK_SHAREBITS) |
3320 new_open->ls_uid = new_stp->ls_uid;
3321 LIST_INIT(&new_open->ls_open);
3322 new_open->ls_openowner = new_stp;
3323 new_open->ls_lfp = lfp;
3324 new_open->ls_clp = clp;
3325 LIST_INSERT_HEAD(&lfp->lf_open, new_open, ls_file);
3326 if (new_stp->ls_flags & NFSLCK_RECLAIM) {
3327 new_stp->ls_flags = 0;
3328 } else if ((nd->nd_flag & ND_NFSV41) != 0) {
3329 /* NFSv4.1 never needs confirmation. */
3330 new_stp->ls_flags = 0;
3333 * This is where we can choose to issue a delegation.
3335 if (delegate && nfsrv_issuedelegs &&
3336 (writedeleg || readonly) &&
3337 (clp->lc_flags & (LCL_CALLBACKSON | LCL_CBDOWN)) ==
3339 !NFSRV_V4DELEGLIMIT(nfsrv_delegatecnt) &&
3340 NFSVNO_DELEGOK(vp) &&
3341 ((nd->nd_flag & ND_NFSV41) == 0 ||
3342 (new_stp->ls_flags & NFSLCK_WANTNODELEG) == 0)) {
3343 new_deleg->ls_stateid.seqid =
3344 delegstateidp->seqid = 1;
3345 new_deleg->ls_stateid.other[0] =
3346 delegstateidp->other[0]
3347 = clp->lc_clientid.lval[0];
3348 new_deleg->ls_stateid.other[1] =
3349 delegstateidp->other[1]
3350 = clp->lc_clientid.lval[1];
3351 new_deleg->ls_stateid.other[2] =
3352 delegstateidp->other[2]
3353 = nfsrv_nextstateindex(clp);
3354 if (writedeleg && !NFSVNO_EXRDONLY(exp) &&
3355 (nfsrv_writedelegifpos || !readonly) &&
3356 ((nd->nd_flag & ND_NFSV41) == 0 ||
3357 (new_stp->ls_flags & NFSLCK_WANTRDELEG) ==
3359 new_deleg->ls_flags =
3360 (NFSLCK_DELEGWRITE |
3362 NFSLCK_WRITEACCESS);
3363 *rflagsp |= NFSV4OPEN_WRITEDELEGATE;
3364 nfsrv_writedelegcnt++;
3366 new_deleg->ls_flags =
3369 *rflagsp |= NFSV4OPEN_READDELEGATE;
3371 new_deleg->ls_uid = new_stp->ls_uid;
3372 new_deleg->ls_lfp = lfp;
3373 new_deleg->ls_clp = clp;
3374 new_deleg->ls_filerev = filerev;
3375 new_deleg->ls_compref = nd->nd_compref;
3376 new_deleg->ls_lastrecall = 0;
3377 LIST_INSERT_HEAD(&lfp->lf_deleg, new_deleg,
3379 LIST_INSERT_HEAD(NFSSTATEHASH(clp,
3380 new_deleg->ls_stateid), new_deleg, ls_hash);
3381 LIST_INSERT_HEAD(&clp->lc_deleg, new_deleg,
3384 NFSD_VNET(nfsstatsv1_p)->srvdelegates++;
3385 nfsrv_openpluslock++;
3386 nfsrv_delegatecnt++;
3389 * Since NFSv4.1 never does an OpenConfirm, the first
3390 * open state will be acquired here.
3392 if (!(clp->lc_flags & LCL_STAMPEDSTABLE)) {
3393 clp->lc_flags |= LCL_STAMPEDSTABLE;
3394 len = clp->lc_idlen;
3395 NFSBCOPY(clp->lc_id, clidp, len);
3399 *rflagsp |= NFSV4OPEN_RESULTCONFIRM;
3400 new_stp->ls_flags = NFSLCK_NEEDSCONFIRM;
3402 nfsrvd_refcache(new_stp->ls_op);
3403 new_stp->ls_noopens = 0;
3404 LIST_INIT(&new_stp->ls_open);
3405 LIST_INSERT_HEAD(&new_stp->ls_open, new_open, ls_list);
3406 LIST_INSERT_HEAD(&clp->lc_open, new_stp, ls_list);
3407 LIST_INSERT_HEAD(NFSSTATEHASH(clp, new_open->ls_stateid),
3412 NFSD_VNET(nfsstatsv1_p)->srvopens++;
3413 nfsrv_openpluslock++;
3414 NFSD_VNET(nfsstatsv1_p)->srvopenowners++;
3415 nfsrv_openpluslock++;
3418 stateidp->seqid = openstp->ls_stateid.seqid;
3419 stateidp->other[0] = openstp->ls_stateid.other[0];
3420 stateidp->other[1] = openstp->ls_stateid.other[1];
3421 stateidp->other[2] = openstp->ls_stateid.other[2];
3425 NFSLOCKV4ROOTMUTEX();
3426 nfsv4_unlock(&nfsv4rootfs_lock, 1);
3427 NFSUNLOCKV4ROOTMUTEX();
3430 free(new_open, M_NFSDSTATE);
3432 free(new_deleg, M_NFSDSTATE);
3435 * If the NFSv4.1 client just acquired its first open, write a timestamp
3436 * to the stable storage file.
3438 if (gotstate != 0) {
3439 nfsrv_writestable(clidp, len, NFSNST_NEWSTATE, p);
3440 nfsrv_backupstable();
3444 free(clidp, M_TEMP);
3445 NFSEXITCODE2(error, nd);
3450 * Open update. Does the confirm, downgrade and close.
3453 nfsrv_openupdate(vnode_t vp, struct nfsstate *new_stp, nfsquad_t clientid,
3454 nfsv4stateid_t *stateidp, struct nfsrv_descript *nd, NFSPROC_T *p,
3455 int *retwriteaccessp)
3457 struct nfsstate *stp;
3458 struct nfsclient *clp;
3459 struct nfslockfile *lfp;
3461 int error = 0, gotstate = 0, len = 0;
3462 u_char *clidp = NULL;
3465 * Check for restart conditions (client and server).
3467 error = nfsrv_checkrestart(clientid, new_stp->ls_flags,
3468 &new_stp->ls_stateid, 0);
3472 clidp = malloc(NFSV4_OPAQUELIMIT, M_TEMP, M_WAITOK);
3475 * Get the open structure via clientid and stateid.
3477 error = nfsrv_getclient(clientid, CLOPS_RENEW, &clp, NULL,
3478 (nfsquad_t)((u_quad_t)0), 0, nd, p);
3480 error = nfsrv_getstate(clp, &new_stp->ls_stateid,
3481 new_stp->ls_flags, &stp);
3484 * Sanity check the open.
3486 if (!error && (!(stp->ls_flags & NFSLCK_OPEN) ||
3487 (!(new_stp->ls_flags & NFSLCK_CONFIRM) &&
3488 (stp->ls_openowner->ls_flags & NFSLCK_NEEDSCONFIRM)) ||
3489 ((new_stp->ls_flags & NFSLCK_CONFIRM) &&
3490 (!(stp->ls_openowner->ls_flags & NFSLCK_NEEDSCONFIRM)))))
3491 error = NFSERR_BADSTATEID;
3494 error = nfsrv_checkseqid(nd, new_stp->ls_seq,
3495 stp->ls_openowner, new_stp->ls_op);
3496 if (!error && stp->ls_stateid.seqid != new_stp->ls_stateid.seqid &&
3497 (((nd->nd_flag & ND_NFSV41) == 0 &&
3498 !(new_stp->ls_flags & NFSLCK_CONFIRM)) ||
3499 ((nd->nd_flag & ND_NFSV41) != 0 &&
3500 new_stp->ls_stateid.seqid != 0)))
3501 error = NFSERR_OLDSTATEID;
3502 if (!error && vp->v_type != VREG) {
3503 if (vp->v_type == VDIR)
3504 error = NFSERR_ISDIR;
3506 error = NFSERR_INVAL;
3511 * If a client tries to confirm an Open with a bad
3512 * seqid# and there are no byte range locks or other Opens
3513 * on the openowner, just throw it away, so the next use of the
3514 * openowner will start a fresh seq#.
3516 if (error == NFSERR_BADSEQID &&
3517 (new_stp->ls_flags & NFSLCK_CONFIRM) &&
3518 nfsrv_nootherstate(stp))
3519 nfsrv_freeopenowner(stp->ls_openowner, 0, p);
3525 * Set the return stateid.
3527 stateidp->seqid = stp->ls_stateid.seqid + 1;
3528 if ((nd->nd_flag & ND_NFSV41) != 0 && stateidp->seqid == 0)
3529 stateidp->seqid = 1;
3530 stateidp->other[0] = stp->ls_stateid.other[0];
3531 stateidp->other[1] = stp->ls_stateid.other[1];
3532 stateidp->other[2] = stp->ls_stateid.other[2];
3534 * Now, handle the three cases.
3536 if (new_stp->ls_flags & NFSLCK_CONFIRM) {
3538 * If the open doesn't need confirmation, it seems to me that
3539 * there is a client error, but I'll just log it and keep going?
3541 if (!(stp->ls_openowner->ls_flags & NFSLCK_NEEDSCONFIRM))
3542 printf("Nfsv4d: stray open confirm\n");
3543 stp->ls_openowner->ls_flags = 0;
3544 stp->ls_stateid.seqid++;
3545 if ((nd->nd_flag & ND_NFSV41) != 0 &&
3546 stp->ls_stateid.seqid == 0)
3547 stp->ls_stateid.seqid = 1;
3548 if (!(clp->lc_flags & LCL_STAMPEDSTABLE)) {
3549 clp->lc_flags |= LCL_STAMPEDSTABLE;
3550 len = clp->lc_idlen;
3551 NFSBCOPY(clp->lc_id, clidp, len);
3555 } else if (new_stp->ls_flags & NFSLCK_CLOSE) {
3557 if (retwriteaccessp != NULL) {
3558 if ((stp->ls_flags & NFSLCK_WRITEACCESS) != 0)
3559 *retwriteaccessp = 1;
3561 *retwriteaccessp = 0;
3563 if (nfsrv_dolocallocks != 0 && !LIST_EMPTY(&stp->ls_open)) {
3564 /* Get the lf lock */
3567 ASSERT_VOP_ELOCKED(vp, "nfsrv_openupdate");
3569 if (nfsrv_freeopen(stp, vp, 1, p) == 0) {
3571 nfsrv_unlocklf(lfp);
3574 NFSVOPLOCK(vp, LK_EXCLUSIVE | LK_RETRY);
3576 (void) nfsrv_freeopen(stp, NULL, 0, p);
3581 * Update the share bits, making sure that the new set are a
3582 * subset of the old ones.
3584 bits = (new_stp->ls_flags & NFSLCK_SHAREBITS);
3585 if (~(stp->ls_flags) & bits) {
3587 error = NFSERR_INVAL;
3590 stp->ls_flags = (bits | NFSLCK_OPEN);
3591 stp->ls_stateid.seqid++;
3592 if ((nd->nd_flag & ND_NFSV41) != 0 &&
3593 stp->ls_stateid.seqid == 0)
3594 stp->ls_stateid.seqid = 1;
3599 * If the client just confirmed its first open, write a timestamp
3600 * to the stable storage file.
3602 if (gotstate != 0) {
3603 nfsrv_writestable(clidp, len, NFSNST_NEWSTATE, p);
3604 nfsrv_backupstable();
3608 free(clidp, M_TEMP);
3609 NFSEXITCODE2(error, nd);
3614 * Delegation update. Does the purge and return.
3617 nfsrv_delegupdate(struct nfsrv_descript *nd, nfsquad_t clientid,
3618 nfsv4stateid_t *stateidp, vnode_t vp, int op, struct ucred *cred,
3619 NFSPROC_T *p, int *retwriteaccessp)
3621 struct nfsstate *stp;
3622 struct nfsclient *clp;
3627 * Do a sanity check against the file handle for DelegReturn.
3630 error = nfsvno_getfh(vp, &fh, p);
3635 * Check for restart conditions (client and server).
3637 if (op == NFSV4OP_DELEGRETURN)
3638 error = nfsrv_checkrestart(clientid, NFSLCK_DELEGRETURN,
3641 error = nfsrv_checkrestart(clientid, NFSLCK_DELEGPURGE,
3646 * Get the open structure via clientid and stateid.
3649 error = nfsrv_getclient(clientid, CLOPS_RENEW, &clp, NULL,
3650 (nfsquad_t)((u_quad_t)0), 0, nd, p);
3652 if (error == NFSERR_CBPATHDOWN)
3654 if (error == NFSERR_STALECLIENTID && op == NFSV4OP_DELEGRETURN)
3655 error = NFSERR_STALESTATEID;
3657 if (!error && op == NFSV4OP_DELEGRETURN) {
3658 error = nfsrv_getstate(clp, stateidp, NFSLCK_DELEGRETURN, &stp);
3659 if (!error && stp->ls_stateid.seqid != stateidp->seqid &&
3660 ((nd->nd_flag & ND_NFSV41) == 0 || stateidp->seqid != 0))
3661 error = NFSERR_OLDSTATEID;
3664 * NFSERR_EXPIRED means that the state has gone away,
3665 * so Delegations have been purged. Just return ok.
3667 if (error == NFSERR_EXPIRED && op == NFSV4OP_DELEGPURGE) {
3677 if (op == NFSV4OP_DELEGRETURN) {
3678 if (NFSBCMP((caddr_t)&fh, (caddr_t)&stp->ls_lfp->lf_fh,
3679 sizeof (fhandle_t))) {
3681 error = NFSERR_BADSTATEID;
3684 if (retwriteaccessp != NULL) {
3685 if ((stp->ls_flags & NFSLCK_DELEGWRITE) != 0)
3686 *retwriteaccessp = 1;
3688 *retwriteaccessp = 0;
3690 nfsrv_freedeleg(stp);
3692 nfsrv_freedeleglist(&clp->lc_olddeleg);
3703 * Release lock owner.
3706 nfsrv_releaselckown(struct nfsstate *new_stp, nfsquad_t clientid,
3709 struct nfsstate *stp, *nstp, *openstp, *ownstp;
3710 struct nfsclient *clp;
3714 * Check for restart conditions (client and server).
3716 error = nfsrv_checkrestart(clientid, new_stp->ls_flags,
3717 &new_stp->ls_stateid, 0);
3723 * Get the lock owner by name.
3725 error = nfsrv_getclient(clientid, CLOPS_RENEW, &clp, NULL,
3726 (nfsquad_t)((u_quad_t)0), 0, NULL, p);
3731 LIST_FOREACH(ownstp, &clp->lc_open, ls_list) {
3732 LIST_FOREACH(openstp, &ownstp->ls_open, ls_list) {
3733 stp = LIST_FIRST(&openstp->ls_open);
3734 while (stp != LIST_END(&openstp->ls_open)) {
3735 nstp = LIST_NEXT(stp, ls_list);
3737 * If the owner matches, check for locks and
3738 * then free or return an error.
3740 if (stp->ls_ownerlen == new_stp->ls_ownerlen &&
3741 !NFSBCMP(stp->ls_owner, new_stp->ls_owner,
3743 if (LIST_EMPTY(&stp->ls_lock)) {
3744 nfsrv_freelockowner(stp, NULL, 0, p);
3747 error = NFSERR_LOCKSHELD;
3763 * Get the file handle for a lock structure.
3766 nfsrv_getlockfh(vnode_t vp, u_short flags, struct nfslockfile *new_lfp,
3767 fhandle_t *nfhp, NFSPROC_T *p)
3769 fhandle_t *fhp = NULL;
3773 * For lock, use the new nfslock structure, otherwise just
3774 * a fhandle_t on the stack.
3776 if (flags & NFSLCK_OPEN) {
3777 KASSERT(new_lfp != NULL, ("nfsrv_getlockfh: new_lfp NULL"));
3778 fhp = &new_lfp->lf_fh;
3782 panic("nfsrv_getlockfh");
3784 error = nfsvno_getfh(vp, fhp, p);
3790 * Get an nfs lock structure. Allocate one, as required, and return a
3792 * Returns an NFSERR_xxx upon failure or -1 to indicate no current lock.
3795 nfsrv_getlockfile(u_short flags, struct nfslockfile **new_lfpp,
3796 struct nfslockfile **lfpp, fhandle_t *nfhp, int lockit)
3798 struct nfslockfile *lfp;
3799 fhandle_t *fhp = NULL, *tfhp;
3800 struct nfslockhashhead *hp;
3801 struct nfslockfile *new_lfp = NULL;
3804 * For lock, use the new nfslock structure, otherwise just
3805 * a fhandle_t on the stack.
3807 if (flags & NFSLCK_OPEN) {
3808 new_lfp = *new_lfpp;
3809 fhp = &new_lfp->lf_fh;
3813 panic("nfsrv_getlockfile");
3816 hp = NFSLOCKHASH(fhp);
3817 LIST_FOREACH(lfp, hp, lf_hash) {
3819 if (NFSVNO_CMPFH(fhp, tfhp)) {
3826 if (!(flags & NFSLCK_OPEN))
3830 * No match, so chain the new one into the list.
3832 LIST_INIT(&new_lfp->lf_open);
3833 LIST_INIT(&new_lfp->lf_lock);
3834 LIST_INIT(&new_lfp->lf_deleg);
3835 LIST_INIT(&new_lfp->lf_locallock);
3836 LIST_INIT(&new_lfp->lf_rollback);
3837 new_lfp->lf_locallock_lck.nfslock_usecnt = 0;
3838 new_lfp->lf_locallock_lck.nfslock_lock = 0;
3839 new_lfp->lf_usecount = 0;
3840 LIST_INSERT_HEAD(hp, new_lfp, lf_hash);
3847 * This function adds a nfslock lock structure to the list for the associated
3848 * nfsstate and nfslockfile structures. It will be inserted after the
3849 * entry pointed at by insert_lop.
3852 nfsrv_insertlock(struct nfslock *new_lop, struct nfslock *insert_lop,
3853 struct nfsstate *stp, struct nfslockfile *lfp)
3855 struct nfslock *lop, *nlop;
3857 new_lop->lo_stp = stp;
3858 new_lop->lo_lfp = lfp;
3861 /* Insert in increasing lo_first order */
3862 lop = LIST_FIRST(&lfp->lf_lock);
3863 if (lop == LIST_END(&lfp->lf_lock) ||
3864 new_lop->lo_first <= lop->lo_first) {
3865 LIST_INSERT_HEAD(&lfp->lf_lock, new_lop, lo_lckfile);
3867 nlop = LIST_NEXT(lop, lo_lckfile);
3868 while (nlop != LIST_END(&lfp->lf_lock) &&
3869 nlop->lo_first < new_lop->lo_first) {
3871 nlop = LIST_NEXT(lop, lo_lckfile);
3873 LIST_INSERT_AFTER(lop, new_lop, lo_lckfile);
3876 new_lop->lo_lckfile.le_prev = NULL; /* list not used */
3880 * Insert after insert_lop, which is overloaded as stp or lfp for
3883 if (stp == NULL && (struct nfslockfile *)insert_lop == lfp)
3884 LIST_INSERT_HEAD(&lfp->lf_locallock, new_lop, lo_lckowner);
3885 else if ((struct nfsstate *)insert_lop == stp)
3886 LIST_INSERT_HEAD(&stp->ls_lock, new_lop, lo_lckowner);
3888 LIST_INSERT_AFTER(insert_lop, new_lop, lo_lckowner);
3890 NFSD_VNET(nfsstatsv1_p)->srvlocks++;
3891 nfsrv_openpluslock++;
3896 * This function updates the locking for a lock owner and given file. It
3897 * maintains a list of lock ranges ordered on increasing file offset that
3898 * are NFSLCK_READ or NFSLCK_WRITE and non-overlapping (aka POSIX style).
3899 * It always adds new_lop to the list and sometimes uses the one pointed
3903 nfsrv_updatelock(struct nfsstate *stp, struct nfslock **new_lopp,
3904 struct nfslock **other_lopp, struct nfslockfile *lfp)
3906 struct nfslock *new_lop = *new_lopp;
3907 struct nfslock *lop, *tlop, *ilop;
3908 struct nfslock *other_lop = *other_lopp;
3909 int unlock = 0, myfile = 0;
3913 * Work down the list until the lock is merged.
3915 if (new_lop->lo_flags & NFSLCK_UNLOCK)
3918 ilop = (struct nfslock *)stp;
3919 lop = LIST_FIRST(&stp->ls_lock);
3921 ilop = (struct nfslock *)lfp;
3922 lop = LIST_FIRST(&lfp->lf_locallock);
3924 while (lop != NULL) {
3926 * Only check locks for this file that aren't before the start of
3929 if (lop->lo_lfp == lfp) {
3931 if (lop->lo_end >= new_lop->lo_first) {
3932 if (new_lop->lo_end < lop->lo_first) {
3934 * If the new lock ends before the start of the
3935 * current lock's range, no merge, just insert
3940 if (new_lop->lo_flags == lop->lo_flags ||
3941 (new_lop->lo_first <= lop->lo_first &&
3942 new_lop->lo_end >= lop->lo_end)) {
3944 * This lock can be absorbed by the new lock/unlock.
3945 * This happens when it covers the entire range
3946 * of the old lock or is contiguous
3947 * with the old lock and is of the same type or an
3950 if (lop->lo_first < new_lop->lo_first)
3951 new_lop->lo_first = lop->lo_first;
3952 if (lop->lo_end > new_lop->lo_end)
3953 new_lop->lo_end = lop->lo_end;
3955 lop = LIST_NEXT(lop, lo_lckowner);
3956 nfsrv_freenfslock(tlop);
3961 * All these cases are for contiguous locks that are not the
3962 * same type, so they can't be merged.
3964 if (new_lop->lo_first <= lop->lo_first) {
3966 * This case is where the new lock overlaps with the
3967 * first part of the old lock. Move the start of the
3968 * old lock to just past the end of the new lock. The
3969 * new lock will be inserted in front of the old, since
3970 * ilop hasn't been updated. (We are done now.)
3972 lop->lo_first = new_lop->lo_end;
3975 if (new_lop->lo_end >= lop->lo_end) {
3977 * This case is where the new lock overlaps with the
3978 * end of the old lock's range. Move the old lock's
3979 * end to just before the new lock's first and insert
3980 * the new lock after the old lock.
3981 * Might not be done yet, since the new lock could
3982 * overlap further locks with higher ranges.
3984 lop->lo_end = new_lop->lo_first;
3986 lop = LIST_NEXT(lop, lo_lckowner);
3990 * The final case is where the new lock's range is in the
3991 * middle of the current lock's and splits the current lock
3992 * up. Use *other_lopp to handle the second part of the
3993 * split old lock range. (We are done now.)
3994 * For unlock, we use new_lop as other_lop and tmp, since
3995 * other_lop and new_lop are the same for this case.
3996 * We noted the unlock case above, so we don't need
3997 * new_lop->lo_flags any longer.
3999 tmp = new_lop->lo_first;
4000 if (other_lop == NULL) {
4002 panic("nfsd srv update unlock");
4003 other_lop = new_lop;
4006 other_lop->lo_first = new_lop->lo_end;
4007 other_lop->lo_end = lop->lo_end;
4008 other_lop->lo_flags = lop->lo_flags;
4009 other_lop->lo_stp = stp;
4010 other_lop->lo_lfp = lfp;
4012 nfsrv_insertlock(other_lop, lop, stp, lfp);
4019 lop = LIST_NEXT(lop, lo_lckowner);
4020 if (myfile && (lop == NULL || lop->lo_lfp != lfp))
4025 * Insert the new lock in the list at the appropriate place.
4028 nfsrv_insertlock(new_lop, ilop, stp, lfp);
4034 * This function handles sequencing of locks, etc.
4035 * It returns an error that indicates what the caller should do.
4038 nfsrv_checkseqid(struct nfsrv_descript *nd, u_int32_t seqid,
4039 struct nfsstate *stp, struct nfsrvcache *op)
4043 if ((nd->nd_flag & ND_NFSV41) != 0)
4044 /* NFSv4.1 ignores the open_seqid and lock_seqid. */
4046 if (op != nd->nd_rp)
4047 panic("nfsrvstate checkseqid");
4048 if (!(op->rc_flag & RC_INPROG))
4049 panic("nfsrvstate not inprog");
4050 if (stp->ls_op && stp->ls_op->rc_refcnt <= 0) {
4051 printf("refcnt=%d\n", stp->ls_op->rc_refcnt);
4052 panic("nfsrvstate op refcnt");
4055 /* If ND_ERELOOKUP is set, the seqid has already been handled. */
4056 if ((nd->nd_flag & ND_ERELOOKUP) != 0)
4059 if ((stp->ls_seq + 1) == seqid) {
4061 nfsrvd_derefcache(stp->ls_op);
4063 nfsrvd_refcache(op);
4064 stp->ls_seq = seqid;
4066 } else if (stp->ls_seq == seqid && stp->ls_op &&
4067 op->rc_xid == stp->ls_op->rc_xid &&
4068 op->rc_refcnt == 0 &&
4069 op->rc_reqlen == stp->ls_op->rc_reqlen &&
4070 op->rc_cksum == stp->ls_op->rc_cksum) {
4071 if (stp->ls_op->rc_flag & RC_INPROG) {
4072 error = NFSERR_DONTREPLY;
4075 nd->nd_rp = stp->ls_op;
4076 nd->nd_rp->rc_flag |= RC_INPROG;
4077 nfsrvd_delcache(op);
4078 error = NFSERR_REPLYFROMCACHE;
4081 error = NFSERR_BADSEQID;
4084 NFSEXITCODE2(error, nd);
4089 * Get the client ip address for callbacks. If the strings can't be parsed,
4090 * just set lc_program to 0 to indicate no callbacks are possible.
4091 * (For cases where the address can't be parsed or is 0.0.0.0.0.0, set
4092 * the address to the client's transport address. This won't be used
4093 * for callbacks, but can be printed out by nfsstats for info.)
4094 * Return error if the xdr can't be parsed, 0 otherwise.
4097 nfsrv_getclientipaddr(struct nfsrv_descript *nd, struct nfsclient *clp)
4101 int i, j, maxalen = 0, minalen = 0;
4104 struct sockaddr_in *rin = NULL, *sin;
4107 struct sockaddr_in6 *rin6 = NULL, *sin6;
4110 int error = 0, cantparse = 0;
4120 /* 8 is the maximum length of the port# string. */
4121 addr = malloc(INET6_ADDRSTRLEN + 8, M_TEMP, M_WAITOK);
4122 clp->lc_req.nr_client = NULL;
4123 clp->lc_req.nr_lock = 0;
4125 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
4126 i = fxdr_unsigned(int, *tl);
4127 if (i >= 3 && i <= 4) {
4128 error = nfsrv_mtostr(nd, addr, i);
4132 if (!strcmp(addr, "tcp")) {
4133 clp->lc_flags |= LCL_TCPCALLBACK;
4134 clp->lc_req.nr_sotype = SOCK_STREAM;
4135 clp->lc_req.nr_soproto = IPPROTO_TCP;
4137 } else if (!strcmp(addr, "udp")) {
4138 clp->lc_req.nr_sotype = SOCK_DGRAM;
4139 clp->lc_req.nr_soproto = IPPROTO_UDP;
4144 if (af == AF_UNSPEC) {
4145 if (!strcmp(addr, "tcp6")) {
4146 clp->lc_flags |= LCL_TCPCALLBACK;
4147 clp->lc_req.nr_sotype = SOCK_STREAM;
4148 clp->lc_req.nr_soproto = IPPROTO_TCP;
4150 } else if (!strcmp(addr, "udp6")) {
4151 clp->lc_req.nr_sotype = SOCK_DGRAM;
4152 clp->lc_req.nr_soproto = IPPROTO_UDP;
4157 if (af == AF_UNSPEC) {
4163 error = nfsm_advance(nd, NFSM_RNDUP(i), -1);
4169 * The caller has allocated clp->lc_req.nr_nam to be large enough
4170 * for either AF_INET or AF_INET6 and zeroed out the contents.
4171 * maxalen is set to the maximum length of the host IP address string
4172 * plus 8 for the maximum length of the port#.
4173 * minalen is set to the minimum length of the host IP address string
4174 * plus 4 for the minimum length of the port#.
4175 * These lengths do not include NULL termination,
4176 * so INET[6]_ADDRSTRLEN - 1 is used in the calculations.
4181 rin = (struct sockaddr_in *)clp->lc_req.nr_nam;
4182 rin->sin_family = AF_INET;
4183 rin->sin_len = sizeof(struct sockaddr_in);
4184 maxalen = INET_ADDRSTRLEN - 1 + 8;
4190 rin6 = (struct sockaddr_in6 *)clp->lc_req.nr_nam;
4191 rin6->sin6_family = AF_INET6;
4192 rin6->sin6_len = sizeof(struct sockaddr_in6);
4193 maxalen = INET6_ADDRSTRLEN - 1 + 8;
4198 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
4199 i = fxdr_unsigned(int, *tl);
4201 error = NFSERR_BADXDR;
4203 } else if (i == 0) {
4205 } else if (!cantparse && i <= maxalen && i >= minalen) {
4206 error = nfsrv_mtostr(nd, addr, i);
4211 * Parse out the address fields. We expect 6 decimal numbers
4212 * separated by '.'s for AF_INET and two decimal numbers
4213 * preceeded by '.'s for AF_INET6.
4219 * For AF_INET6, first parse the host address.
4222 cp = strchr(addr, '.');
4225 if (inet_pton(af, addr, &rin6->sin6_addr) == 1)
4241 while (cp != NULL && *cp && i < 6) {
4243 while (*cp2 && *cp2 != '.')
4251 j = nfsrv_getipnumber(cp);
4256 port.cval[5 - i] = j;
4266 * The host address INADDR_ANY is (mis)used to indicate
4267 * "there is no valid callback address".
4272 if (!IN6_ARE_ADDR_EQUAL(&rin6->sin6_addr,
4274 rin6->sin6_port = htons(port.sval);
4281 if (ip.ival != INADDR_ANY) {
4282 rin->sin_addr.s_addr = htonl(ip.ival);
4283 rin->sin_port = htons(port.sval);
4294 error = nfsm_advance(nd, NFSM_RNDUP(i), -1);
4300 switch (nd->nd_nam->sa_family) {
4303 sin = (struct sockaddr_in *)nd->nd_nam;
4304 rin = (struct sockaddr_in *)clp->lc_req.nr_nam;
4305 rin->sin_family = AF_INET;
4306 rin->sin_len = sizeof(struct sockaddr_in);
4307 rin->sin_addr.s_addr = sin->sin_addr.s_addr;
4308 rin->sin_port = 0x0;
4313 sin6 = (struct sockaddr_in6 *)nd->nd_nam;
4314 rin6 = (struct sockaddr_in6 *)clp->lc_req.nr_nam;
4315 rin6->sin6_family = AF_INET6;
4316 rin6->sin6_len = sizeof(struct sockaddr_in6);
4317 rin6->sin6_addr = sin6->sin6_addr;
4318 rin6->sin6_port = 0x0;
4322 clp->lc_program = 0;
4326 NFSEXITCODE2(error, nd);
4331 * Turn a string of up to three decimal digits into a number. Return -1 upon
4335 nfsrv_getipnumber(u_char *cp)
4340 if (j > 2 || *cp < '0' || *cp > '9')
4353 * This function checks for restart conditions.
4356 nfsrv_checkrestart(nfsquad_t clientid, u_int32_t flags,
4357 nfsv4stateid_t *stateidp, int specialid)
4362 * First check for a server restart. Open, LockT, ReleaseLockOwner
4363 * and DelegPurge have a clientid, the rest a stateid.
4366 (NFSLCK_OPEN | NFSLCK_TEST | NFSLCK_RELEASE | NFSLCK_DELEGPURGE)) {
4367 if (clientid.lval[0] != NFSD_VNET(nfsrvboottime)) {
4368 ret = NFSERR_STALECLIENTID;
4371 } else if (stateidp->other[0] != NFSD_VNET(nfsrvboottime) &&
4373 ret = NFSERR_STALESTATEID;
4378 * Read, Write, Setattr and LockT can return NFSERR_GRACE and do
4379 * not use a lock/open owner seqid#, so the check can be done now.
4380 * (The others will be checked, as required, later.)
4382 if (!(flags & (NFSLCK_CHECK | NFSLCK_TEST)))
4386 ret = nfsrv_checkgrace(NULL, NULL, flags);
4398 nfsrv_checkgrace(struct nfsrv_descript *nd, struct nfsclient *clp,
4401 int error = 0, notreclaimed;
4402 struct nfsrv_stable *sp;
4404 if ((NFSD_VNET(nfsrv_stablefirst).nsf_flags & (NFSNSF_UPDATEDONE |
4405 NFSNSF_GRACEOVER)) == 0) {
4407 * First, check to see if all of the clients have done a
4408 * ReclaimComplete. If so, grace can end now.
4411 LIST_FOREACH(sp, &NFSD_VNET(nfsrv_stablefirst).nsf_head,
4413 if ((sp->nst_flag & NFSNST_RECLAIMED) == 0) {
4418 if (notreclaimed == 0)
4419 NFSD_VNET(nfsrv_stablefirst).nsf_flags |=
4420 (NFSNSF_GRACEOVER | NFSNSF_NEEDLOCK);
4423 if ((NFSD_VNET(nfsrv_stablefirst).nsf_flags & NFSNSF_GRACEOVER) != 0) {
4424 if (flags & NFSLCK_RECLAIM) {
4425 error = NFSERR_NOGRACE;
4429 if (!(flags & NFSLCK_RECLAIM)) {
4430 error = NFSERR_GRACE;
4433 if (nd != NULL && clp != NULL &&
4434 (nd->nd_flag & ND_NFSV41) != 0 &&
4435 (clp->lc_flags & LCL_RECLAIMCOMPLETE) != 0) {
4436 error = NFSERR_NOGRACE;
4441 * If grace is almost over and we are still getting Reclaims,
4442 * extend grace a bit.
4444 if ((NFSD_MONOSEC + NFSRV_LEASEDELTA) >
4445 NFSD_VNET(nfsrv_stablefirst).nsf_eograce)
4446 NFSD_VNET(nfsrv_stablefirst).nsf_eograce =
4447 NFSD_MONOSEC + NFSRV_LEASEDELTA;
4456 * Do a server callback.
4457 * The "trunc" argument is slightly overloaded and refers to different
4458 * boolean arguments for CBRECALL and CBLAYOUTRECALL.
4461 nfsrv_docallback(struct nfsclient *clp, int procnum, nfsv4stateid_t *stateidp,
4462 int trunc, fhandle_t *fhp, struct nfsvattr *nap, nfsattrbit_t *attrbitp,
4463 int laytype, NFSPROC_T *p)
4467 struct nfsrv_descript *nd;
4469 int error = 0, slotpos;
4471 struct nfsdsession *sep = NULL;
4475 nd = malloc(sizeof(*nd), M_TEMP, M_WAITOK | M_ZERO);
4476 cred = newnfs_getcred();
4477 NFSLOCKSTATE(); /* mostly for lc_cbref++ */
4478 if (clp->lc_flags & LCL_NEEDSCONFIRM) {
4485 * Fill the callback program# and version into the request
4486 * structure for newnfs_connect() to use.
4488 clp->lc_req.nr_prog = clp->lc_program;
4490 if ((clp->lc_flags & LCL_NFSV41) != 0)
4491 clp->lc_req.nr_vers = NFSV41_CBVERS;
4494 clp->lc_req.nr_vers = NFSV4_CBVERS;
4497 * First, fill in some of the fields of nd and cr.
4499 nd->nd_flag = ND_NFSV4;
4500 if (clp->lc_flags & LCL_GSS)
4501 nd->nd_flag |= ND_KERBV;
4502 if ((clp->lc_flags & LCL_NFSV41) != 0)
4503 nd->nd_flag |= ND_NFSV41;
4504 if ((clp->lc_flags & LCL_NFSV42) != 0)
4505 nd->nd_flag |= ND_NFSV42;
4507 cred->cr_uid = clp->lc_uid;
4508 cred->cr_gid = clp->lc_gid;
4509 callback = clp->lc_callback;
4511 cred->cr_ngroups = 1;
4514 * Get the first mbuf for the request.
4516 MGET(m, M_WAITOK, MT_DATA);
4518 nd->nd_mreq = nd->nd_mb = m;
4519 nd->nd_bpos = mtod(m, caddr_t);
4522 * and build the callback request.
4524 if (procnum == NFSV4OP_CBGETATTR) {
4525 nd->nd_procnum = NFSV4PROC_CBCOMPOUND;
4526 error = nfsrv_cbcallargs(nd, clp, callback, NFSV4OP_CBGETATTR,
4527 "CB Getattr", &sep, &slotpos);
4529 m_freem(nd->nd_mreq);
4532 (void)nfsm_fhtom(NULL, nd, (u_int8_t *)fhp, NFSX_MYFH, 0);
4533 (void)nfsrv_putattrbit(nd, attrbitp);
4534 } else if (procnum == NFSV4OP_CBRECALL) {
4535 nd->nd_procnum = NFSV4PROC_CBCOMPOUND;
4536 error = nfsrv_cbcallargs(nd, clp, callback, NFSV4OP_CBRECALL,
4537 "CB Recall", &sep, &slotpos);
4539 m_freem(nd->nd_mreq);
4542 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED + NFSX_STATEID);
4543 *tl++ = txdr_unsigned(stateidp->seqid);
4544 NFSBCOPY((caddr_t)stateidp->other, (caddr_t)tl,
4546 tl += (NFSX_STATEIDOTHER / NFSX_UNSIGNED);
4551 (void)nfsm_fhtom(NULL, nd, (u_int8_t *)fhp, NFSX_MYFH, 0);
4552 } else if (procnum == NFSV4OP_CBLAYOUTRECALL) {
4553 NFSD_DEBUG(4, "docallback layout recall\n");
4554 nd->nd_procnum = NFSV4PROC_CBCOMPOUND;
4555 error = nfsrv_cbcallargs(nd, clp, callback,
4556 NFSV4OP_CBLAYOUTRECALL, "CB Reclayout", &sep, &slotpos);
4557 NFSD_DEBUG(4, "aft cbcallargs=%d\n", error);
4559 m_freem(nd->nd_mreq);
4562 NFSM_BUILD(tl, u_int32_t *, 4 * NFSX_UNSIGNED);
4563 *tl++ = txdr_unsigned(laytype);
4564 *tl++ = txdr_unsigned(NFSLAYOUTIOMODE_ANY);
4566 *tl++ = newnfs_true;
4568 *tl++ = newnfs_false;
4569 *tl = txdr_unsigned(NFSV4LAYOUTRET_FILE);
4570 (void)nfsm_fhtom(NULL, nd, (uint8_t *)fhp, NFSX_MYFH, 0);
4571 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_HYPER + NFSX_STATEID);
4573 txdr_hyper(tval, tl); tl += 2;
4575 txdr_hyper(tval, tl); tl += 2;
4576 *tl++ = txdr_unsigned(stateidp->seqid);
4577 NFSBCOPY(stateidp->other, tl, NFSX_STATEIDOTHER);
4578 tl += (NFSX_STATEIDOTHER / NFSX_UNSIGNED);
4579 NFSD_DEBUG(4, "aft args\n");
4580 } else if (procnum == NFSV4PROC_CBNULL) {
4581 nd->nd_procnum = NFSV4PROC_CBNULL;
4582 if ((clp->lc_flags & LCL_NFSV41) != 0) {
4583 error = nfsv4_getcbsession(clp, &sep);
4585 m_freem(nd->nd_mreq);
4590 error = NFSERR_SERVERFAULT;
4591 m_freem(nd->nd_mreq);
4596 * Call newnfs_connect(), as required, and then newnfs_request().
4599 if ((clp->lc_flags & LCL_TLSCB) != 0)
4601 (void) newnfs_sndlock(&clp->lc_req.nr_lock);
4602 if (clp->lc_req.nr_client == NULL) {
4603 if ((clp->lc_flags & LCL_NFSV41) != 0) {
4604 error = ECONNREFUSED;
4605 if (procnum != NFSV4PROC_CBNULL)
4606 nfsv4_freeslot(&sep->sess_cbsess, slotpos,
4608 nfsrv_freesession(sep, NULL);
4609 } else if (nd->nd_procnum == NFSV4PROC_CBNULL)
4610 error = newnfs_connect(NULL, &clp->lc_req, cred,
4611 NULL, 1, dotls, &clp->lc_req.nr_client);
4613 error = newnfs_connect(NULL, &clp->lc_req, cred,
4614 NULL, 3, dotls, &clp->lc_req.nr_client);
4616 newnfs_sndunlock(&clp->lc_req.nr_lock);
4617 NFSD_DEBUG(4, "aft sndunlock=%d\n", error);
4619 if ((nd->nd_flag & ND_NFSV41) != 0) {
4620 KASSERT(sep != NULL, ("sep NULL"));
4621 if (sep->sess_cbsess.nfsess_xprt != NULL)
4622 error = newnfs_request(nd, NULL, clp,
4623 &clp->lc_req, NULL, NULL, cred,
4624 clp->lc_program, clp->lc_req.nr_vers, NULL,
4625 1, NULL, &sep->sess_cbsess);
4628 * This should probably never occur, but if a
4629 * client somehow does an RPC without a
4630 * SequenceID Op that causes a callback just
4631 * after the nfsd threads have been terminated
4632 * and restared we could conceivably get here
4633 * without a backchannel xprt.
4635 printf("nfsrv_docallback: no xprt\n");
4636 error = ECONNREFUSED;
4638 NFSD_DEBUG(4, "aft newnfs_request=%d\n", error);
4639 if (error != 0 && procnum != NFSV4PROC_CBNULL) {
4641 * It is likely that the callback was never
4642 * processed by the client and, as such,
4643 * the sequence# for the session slot needs
4644 * to be backed up by one to avoid a
4645 * NFSERR_SEQMISORDERED error reply.
4646 * For the unlikely case where the callback
4647 * was processed by the client, this will
4648 * make the next callback on the slot
4649 * appear to be a retry.
4650 * Since callbacks never specify that the
4651 * reply be cached, this "apparent retry"
4652 * should not be a problem.
4654 nfsv4_freeslot(&sep->sess_cbsess, slotpos,
4657 nfsrv_freesession(sep, NULL);
4659 error = newnfs_request(nd, NULL, clp, &clp->lc_req,
4660 NULL, NULL, cred, clp->lc_program,
4661 clp->lc_req.nr_vers, NULL, 1, NULL, NULL);
4667 * If error is set here, the Callback path isn't working
4668 * properly, so twiddle the appropriate LCL_ flags.
4669 * (nd_repstat != 0 indicates the Callback path is working,
4670 * but the callback failed on the client.)
4674 * Mark the callback pathway down, which disabled issuing
4675 * of delegations and gets Renew to return NFSERR_CBPATHDOWN.
4678 clp->lc_flags |= LCL_CBDOWN;
4682 * Callback worked. If the callback path was down, disable
4683 * callbacks, so no more delegations will be issued. (This
4684 * is done on the assumption that the callback pathway is
4688 if (clp->lc_flags & LCL_CBDOWN)
4689 clp->lc_flags &= ~(LCL_CBDOWN | LCL_CALLBACKSON);
4691 if (nd->nd_repstat) {
4692 error = nd->nd_repstat;
4693 NFSD_DEBUG(1, "nfsrv_docallback op=%d err=%d\n",
4695 } else if (error == 0 && procnum == NFSV4OP_CBGETATTR)
4696 error = nfsv4_loadattr(nd, NULL, nap, NULL, NULL, 0,
4697 NULL, NULL, NULL, NULL, NULL, 0, NULL, NULL, NULL,
4699 m_freem(nd->nd_mrep);
4703 if ((clp->lc_flags & LCL_WAKEUPWANTED) && clp->lc_cbref == 0) {
4704 clp->lc_flags &= ~LCL_WAKEUPWANTED;
4715 * Set up the compound RPC for the callback.
4718 nfsrv_cbcallargs(struct nfsrv_descript *nd, struct nfsclient *clp,
4719 uint32_t callback, int op, const char *optag, struct nfsdsession **sepp,
4725 len = strlen(optag);
4726 (void)nfsm_strtom(nd, optag, len);
4727 NFSM_BUILD(tl, uint32_t *, 4 * NFSX_UNSIGNED);
4728 if ((nd->nd_flag & ND_NFSV41) != 0) {
4729 if ((nd->nd_flag & ND_NFSV42) != 0)
4730 *tl++ = txdr_unsigned(NFSV42_MINORVERSION);
4732 *tl++ = txdr_unsigned(NFSV41_MINORVERSION);
4733 *tl++ = txdr_unsigned(callback);
4734 *tl++ = txdr_unsigned(2);
4735 *tl = txdr_unsigned(NFSV4OP_CBSEQUENCE);
4736 error = nfsv4_setcbsequence(nd, clp, 1, sepp, slotposp);
4739 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
4740 *tl = txdr_unsigned(op);
4742 *tl++ = txdr_unsigned(NFSV4_MINORVERSION);
4743 *tl++ = txdr_unsigned(callback);
4744 *tl++ = txdr_unsigned(1);
4745 *tl = txdr_unsigned(op);
4751 * Return the next index# for a clientid. Mostly just increment and return
4752 * the next one, but... if the 32bit unsigned does actually wrap around,
4753 * it should be rebooted.
4754 * At an average rate of one new client per second, it will wrap around in
4755 * approximately 136 years. (I think the server will have been shut
4756 * down or rebooted before then.)
4759 nfsrv_nextclientindex(void)
4761 static u_int32_t client_index = 0;
4764 if (client_index != 0)
4765 return (client_index);
4767 printf("%s: out of clientids\n", __func__);
4768 return (client_index);
4772 * Return the next index# for a stateid. Mostly just increment and return
4773 * the next one, but... if the 32bit unsigned does actually wrap around
4774 * (will a BSD server stay up that long?), find
4775 * new start and end values.
4778 nfsrv_nextstateindex(struct nfsclient *clp)
4780 struct nfsstate *stp;
4782 u_int32_t canuse, min_index, max_index;
4784 if (!(clp->lc_flags & LCL_INDEXNOTOK)) {
4785 clp->lc_stateindex++;
4786 if (clp->lc_stateindex != clp->lc_statemaxindex)
4787 return (clp->lc_stateindex);
4791 * Yuck, we've hit the end.
4792 * Look for a new min and max.
4795 max_index = 0xffffffff;
4796 for (i = 0; i < nfsrv_statehashsize; i++) {
4797 LIST_FOREACH(stp, &clp->lc_stateid[i], ls_hash) {
4798 if (stp->ls_stateid.other[2] > 0x80000000) {
4799 if (stp->ls_stateid.other[2] < max_index)
4800 max_index = stp->ls_stateid.other[2];
4802 if (stp->ls_stateid.other[2] > min_index)
4803 min_index = stp->ls_stateid.other[2];
4809 * Yikes, highly unlikely, but I'll handle it anyhow.
4811 if (min_index == 0x80000000 && max_index == 0x80000001) {
4814 * Loop around until we find an unused entry. Return that
4815 * and set LCL_INDEXNOTOK, so the search will continue next time.
4816 * (This is one of those rare cases where a goto is the
4817 * cleanest way to code the loop.)
4820 for (i = 0; i < nfsrv_statehashsize; i++) {
4821 LIST_FOREACH(stp, &clp->lc_stateid[i], ls_hash) {
4822 if (stp->ls_stateid.other[2] == canuse) {
4828 clp->lc_flags |= LCL_INDEXNOTOK;
4833 * Ok to start again from min + 1.
4835 clp->lc_stateindex = min_index + 1;
4836 clp->lc_statemaxindex = max_index;
4837 clp->lc_flags &= ~LCL_INDEXNOTOK;
4838 return (clp->lc_stateindex);
4842 * The following functions handle the stable storage file that deals with
4843 * the edge conditions described in RFC3530 Sec. 8.6.3.
4844 * The file is as follows:
4845 * - a single record at the beginning that has the lease time of the
4846 * previous server instance (before the last reboot) and the nfsrvboottime
4847 * values for the previous server boots.
4848 * These previous boot times are used to ensure that the current
4849 * nfsrvboottime does not, somehow, get set to a previous one.
4850 * (This is important so that Stale ClientIDs and StateIDs can
4852 * The number of previous nfsvrboottime values precedes the list.
4853 * - followed by some number of appended records with:
4854 * - client id string
4855 * - flag that indicates it is a record revoking state via lease
4856 * expiration or similar
4857 * OR has successfully acquired state.
4858 * These structures vary in length, with the client string at the end, up
4859 * to NFSV4_OPAQUELIMIT in size.
4861 * At the end of the grace period, the file is truncated, the first
4862 * record is rewritten with updated information and any acquired state
4863 * records for successful reclaims of state are written.
4865 * Subsequent records are appended when the first state is issued to
4866 * a client and when state is revoked for a client.
4868 * When reading the file in, state issued records that come later in
4869 * the file override older ones, since the append log is in cronological order.
4870 * If, for some reason, the file can't be read, the grace period is
4871 * immediately terminated and all reclaims get NFSERR_NOGRACE.
4875 * Read in the stable storage file. Called by nfssvc() before the nfsd
4876 * processes start servicing requests.
4879 nfsrv_setupstable(NFSPROC_T *p)
4881 struct nfsrv_stablefirst *sf = &NFSD_VNET(nfsrv_stablefirst);
4882 struct nfsrv_stable *sp, *nsp;
4883 struct nfst_rec *tsp;
4884 int error, i, tryagain;
4886 ssize_t aresid, len;
4889 * If NFSNSF_UPDATEDONE is set, this is a restart of the nfsds without
4890 * a reboot, so state has not been lost.
4892 if (sf->nsf_flags & NFSNSF_UPDATEDONE)
4895 * Set Grace over just until the file reads successfully.
4897 NFSD_VNET(nfsrvboottime) = time_second;
4898 LIST_INIT(&sf->nsf_head);
4899 sf->nsf_flags = (NFSNSF_GRACEOVER | NFSNSF_NEEDLOCK);
4900 sf->nsf_eograce = NFSD_MONOSEC + NFSRV_LEASEDELTA;
4901 if (sf->nsf_fp == NULL)
4903 error = NFSD_RDWR(UIO_READ, NFSFPVNODE(sf->nsf_fp),
4904 (caddr_t)&sf->nsf_rec, sizeof (struct nfsf_rec), off, UIO_SYSSPACE,
4905 0, NFSFPCRED(sf->nsf_fp), &aresid, p);
4906 if (error || aresid || sf->nsf_numboots == 0 ||
4907 sf->nsf_numboots > NFSNSF_MAXNUMBOOTS)
4911 * Now, read in the boottimes.
4913 sf->nsf_bootvals = (time_t *)malloc((sf->nsf_numboots + 1) *
4914 sizeof(time_t), M_TEMP, M_WAITOK);
4915 off = sizeof (struct nfsf_rec);
4916 error = NFSD_RDWR(UIO_READ, NFSFPVNODE(sf->nsf_fp),
4917 (caddr_t)sf->nsf_bootvals, sf->nsf_numboots * sizeof (time_t), off,
4918 UIO_SYSSPACE, 0, NFSFPCRED(sf->nsf_fp), &aresid, p);
4919 if (error || aresid) {
4920 free(sf->nsf_bootvals, M_TEMP);
4921 sf->nsf_bootvals = NULL;
4926 * Make sure this nfsrvboottime is different from all recorded
4931 for (i = 0; i < sf->nsf_numboots; i++) {
4932 if (NFSD_VNET(nfsrvboottime) == sf->nsf_bootvals[i]) {
4933 NFSD_VNET(nfsrvboottime)++;
4940 sf->nsf_flags |= NFSNSF_OK;
4941 off += (sf->nsf_numboots * sizeof (time_t));
4944 * Read through the file, building a list of records for grace
4946 * Each record is between sizeof (struct nfst_rec) and
4947 * sizeof (struct nfst_rec) + NFSV4_OPAQUELIMIT - 1
4948 * and is actually sizeof (struct nfst_rec) + nst_len - 1.
4950 tsp = (struct nfst_rec *)malloc(sizeof (struct nfst_rec) +
4951 NFSV4_OPAQUELIMIT - 1, M_TEMP, M_WAITOK);
4953 error = NFSD_RDWR(UIO_READ, NFSFPVNODE(sf->nsf_fp),
4954 (caddr_t)tsp, sizeof (struct nfst_rec) + NFSV4_OPAQUELIMIT - 1,
4955 off, UIO_SYSSPACE, 0, NFSFPCRED(sf->nsf_fp), &aresid, p);
4956 len = (sizeof (struct nfst_rec) + NFSV4_OPAQUELIMIT - 1) - aresid;
4957 if (error || (len > 0 && (len < sizeof (struct nfst_rec) ||
4958 len < (sizeof (struct nfst_rec) + tsp->len - 1)))) {
4960 * Yuck, the file has been corrupted, so just return
4961 * after clearing out any restart state, so the grace period
4964 LIST_FOREACH_SAFE(sp, &sf->nsf_head, nst_list, nsp) {
4965 LIST_REMOVE(sp, nst_list);
4969 sf->nsf_flags &= ~NFSNSF_OK;
4970 free(sf->nsf_bootvals, M_TEMP);
4971 sf->nsf_bootvals = NULL;
4975 off += sizeof (struct nfst_rec) + tsp->len - 1;
4977 * Search the list for a matching client.
4979 LIST_FOREACH(sp, &sf->nsf_head, nst_list) {
4980 if (tsp->len == sp->nst_len &&
4981 !NFSBCMP(tsp->client, sp->nst_client, tsp->len))
4984 if (sp == LIST_END(&sf->nsf_head)) {
4985 sp = (struct nfsrv_stable *)malloc(tsp->len +
4986 sizeof (struct nfsrv_stable) - 1, M_TEMP,
4988 NFSBCOPY((caddr_t)tsp, (caddr_t)&sp->nst_rec,
4989 sizeof (struct nfst_rec) + tsp->len - 1);
4990 LIST_INSERT_HEAD(&sf->nsf_head, sp, nst_list);
4992 if (tsp->flag == NFSNST_REVOKE)
4993 sp->nst_flag |= NFSNST_REVOKE;
4996 * A subsequent timestamp indicates the client
4997 * did a setclientid/confirm and any previous
4998 * revoke is no longer relevant.
5000 sp->nst_flag &= ~NFSNST_REVOKE;
5005 sf->nsf_flags = NFSNSF_OK;
5006 sf->nsf_eograce = NFSD_MONOSEC + sf->nsf_lease +
5011 * Update the stable storage file, now that the grace period is over.
5014 nfsrv_updatestable(NFSPROC_T *p)
5016 struct nfsrv_stablefirst *sf = &NFSD_VNET(nfsrv_stablefirst);
5017 struct nfsrv_stable *sp, *nsp;
5019 struct nfsvattr nva;
5021 #if defined(__FreeBSD_version) && (__FreeBSD_version >= 500000)
5026 if (sf->nsf_fp == NULL || (sf->nsf_flags & NFSNSF_UPDATEDONE))
5028 sf->nsf_flags |= NFSNSF_UPDATEDONE;
5030 * Ok, we need to rewrite the stable storage file.
5031 * - truncate to 0 length
5032 * - write the new first structure
5033 * - loop through the data structures, writing out any that
5034 * have timestamps older than the old boot
5036 if (sf->nsf_bootvals) {
5038 for (i = sf->nsf_numboots - 2; i >= 0; i--)
5039 sf->nsf_bootvals[i + 1] = sf->nsf_bootvals[i];
5041 sf->nsf_numboots = 1;
5042 sf->nsf_bootvals = (time_t *)malloc(sizeof(time_t),
5045 sf->nsf_bootvals[0] = NFSD_VNET(nfsrvboottime);
5046 sf->nsf_lease = nfsrv_lease;
5047 NFSVNO_ATTRINIT(&nva);
5048 NFSVNO_SETATTRVAL(&nva, size, 0);
5049 vp = NFSFPVNODE(sf->nsf_fp);
5050 vn_start_write(vp, &mp, V_WAIT);
5051 if (NFSVOPLOCK(vp, LK_EXCLUSIVE) == 0) {
5052 error = nfsvno_setattr(vp, &nva, NFSFPCRED(sf->nsf_fp), p,
5057 vn_finished_write(mp);
5059 error = NFSD_RDWR(UIO_WRITE, vp,
5060 (caddr_t)&sf->nsf_rec, sizeof (struct nfsf_rec), (off_t)0,
5061 UIO_SYSSPACE, IO_SYNC, NFSFPCRED(sf->nsf_fp), NULL, p);
5063 error = NFSD_RDWR(UIO_WRITE, vp,
5064 (caddr_t)sf->nsf_bootvals,
5065 sf->nsf_numboots * sizeof (time_t),
5066 (off_t)(sizeof (struct nfsf_rec)),
5067 UIO_SYSSPACE, IO_SYNC, NFSFPCRED(sf->nsf_fp), NULL, p);
5068 free(sf->nsf_bootvals, M_TEMP);
5069 sf->nsf_bootvals = NULL;
5071 sf->nsf_flags &= ~NFSNSF_OK;
5072 printf("EEK! Can't write NfsV4 stable storage file\n");
5075 sf->nsf_flags |= NFSNSF_OK;
5078 * Loop through the list and write out timestamp records for
5079 * any clients that successfully reclaimed state.
5081 LIST_FOREACH_SAFE(sp, &sf->nsf_head, nst_list, nsp) {
5082 if (sp->nst_flag & NFSNST_GOTSTATE) {
5083 nfsrv_writestable(sp->nst_client, sp->nst_len,
5084 NFSNST_NEWSTATE, p);
5085 sp->nst_clp->lc_flags |= LCL_STAMPEDSTABLE;
5087 LIST_REMOVE(sp, nst_list);
5090 nfsrv_backupstable();
5094 * Append a record to the stable storage file.
5097 nfsrv_writestable(u_char *client, int len, int flag, NFSPROC_T *p)
5099 struct nfsrv_stablefirst *sf = &NFSD_VNET(nfsrv_stablefirst);
5100 struct nfst_rec *sp;
5103 if (!(sf->nsf_flags & NFSNSF_OK) || sf->nsf_fp == NULL)
5105 sp = (struct nfst_rec *)malloc(sizeof (struct nfst_rec) +
5106 len - 1, M_TEMP, M_WAITOK);
5108 NFSBCOPY(client, sp->client, len);
5110 error = NFSD_RDWR(UIO_WRITE, NFSFPVNODE(sf->nsf_fp),
5111 (caddr_t)sp, sizeof (struct nfst_rec) + len - 1, (off_t)0,
5112 UIO_SYSSPACE, (IO_SYNC | IO_APPEND), NFSFPCRED(sf->nsf_fp), NULL, p);
5115 sf->nsf_flags &= ~NFSNSF_OK;
5116 printf("EEK! Can't write NfsV4 stable storage file\n");
5121 * This function is called during the grace period to mark a client
5122 * that successfully reclaimed state.
5125 nfsrv_markstable(struct nfsclient *clp)
5127 struct nfsrv_stable *sp;
5130 * First find the client structure.
5132 LIST_FOREACH(sp, &NFSD_VNET(nfsrv_stablefirst).nsf_head, nst_list) {
5133 if (sp->nst_len == clp->lc_idlen &&
5134 !NFSBCMP(sp->nst_client, clp->lc_id, sp->nst_len))
5137 if (sp == LIST_END(&NFSD_VNET(nfsrv_stablefirst).nsf_head))
5141 * Now, just mark it and set the nfsclient back pointer.
5143 sp->nst_flag |= NFSNST_GOTSTATE;
5148 * This function is called when a NFSv4.1 client does a ReclaimComplete.
5149 * Very similar to nfsrv_markstable(), except for the flag being set.
5152 nfsrv_markreclaim(struct nfsclient *clp)
5154 struct nfsrv_stable *sp;
5157 * First find the client structure.
5159 LIST_FOREACH(sp, &NFSD_VNET(nfsrv_stablefirst).nsf_head, nst_list) {
5160 if (sp->nst_len == clp->lc_idlen &&
5161 !NFSBCMP(sp->nst_client, clp->lc_id, sp->nst_len))
5164 if (sp == LIST_END(&NFSD_VNET(nfsrv_stablefirst).nsf_head))
5168 * Now, just set the flag.
5170 sp->nst_flag |= NFSNST_RECLAIMED;
5174 * This function is called for a reclaim, to see if it gets grace.
5175 * It returns 0 if a reclaim is allowed, 1 otherwise.
5178 nfsrv_checkstable(struct nfsclient *clp)
5180 struct nfsrv_stable *sp;
5183 * First, find the entry for the client.
5185 LIST_FOREACH(sp, &NFSD_VNET(nfsrv_stablefirst).nsf_head, nst_list) {
5186 if (sp->nst_len == clp->lc_idlen &&
5187 !NFSBCMP(sp->nst_client, clp->lc_id, sp->nst_len))
5192 * If not in the list, state was revoked or no state was issued
5193 * since the previous reboot, a reclaim is denied.
5195 if (sp == LIST_END(&NFSD_VNET(nfsrv_stablefirst).nsf_head) ||
5196 (sp->nst_flag & NFSNST_REVOKE) ||
5197 !(NFSD_VNET(nfsrv_stablefirst).nsf_flags & NFSNSF_OK))
5203 * Test for and try to clear out a conflicting client. This is called by
5204 * nfsrv_lockctrl() and nfsrv_openctrl() when conflicts with other clients
5206 * The trick here is that it can't revoke a conflicting client with an
5207 * expired lease unless it holds the v4root lock, so...
5208 * If no v4root lock, get the lock and return 1 to indicate "try again".
5209 * Return 0 to indicate the conflict can't be revoked and 1 to indicate
5210 * the revocation worked and the conflicting client is "bye, bye", so it
5211 * can be tried again.
5212 * Return 2 to indicate that the vnode is VIRF_DOOMED after NFSVOPLOCK().
5213 * Unlocks State before a non-zero value is returned.
5216 nfsrv_clientconflict(struct nfsclient *clp, int *haslockp, vnode_t vp,
5219 int gotlock, lktype = 0;
5222 * If lease hasn't expired, we can't fix it.
5224 if (clp->lc_expiry >= NFSD_MONOSEC ||
5225 !(NFSD_VNET(nfsrv_stablefirst).nsf_flags & NFSNSF_UPDATEDONE))
5227 if (*haslockp == 0) {
5230 lktype = NFSVOPISLOCKED(vp);
5233 NFSLOCKV4ROOTMUTEX();
5234 nfsv4_relref(&nfsv4rootfs_lock);
5236 gotlock = nfsv4_lock(&nfsv4rootfs_lock, 1, NULL,
5237 NFSV4ROOTLOCKMUTEXPTR, NULL);
5239 NFSUNLOCKV4ROOTMUTEX();
5242 NFSVOPLOCK(vp, lktype | LK_RETRY);
5243 if (VN_IS_DOOMED(vp))
5251 * Ok, we can expire the conflicting client.
5253 nfsrv_writestable(clp->lc_id, clp->lc_idlen, NFSNST_REVOKE, p);
5254 nfsrv_backupstable();
5255 nfsrv_cleanclient(clp, p);
5256 nfsrv_freedeleglist(&clp->lc_deleg);
5257 nfsrv_freedeleglist(&clp->lc_olddeleg);
5258 LIST_REMOVE(clp, lc_hash);
5259 nfsrv_zapclient(clp, p);
5264 * Resolve a delegation conflict.
5265 * Returns 0 to indicate the conflict was resolved without sleeping.
5266 * Return -1 to indicate that the caller should check for conflicts again.
5267 * Return > 0 for an error that should be returned, normally NFSERR_DELAY.
5269 * Also, manipulate the nfsv4root_lock, as required. It isn't changed
5270 * for a return of 0, since there was no sleep and it could be required
5271 * later. It is released for a return of NFSERR_DELAY, since the caller
5272 * will return that error. It is released when a sleep was done waiting
5273 * for the delegation to be returned or expire (so that other nfsds can
5274 * handle ops). Then, it must be acquired for the write to stable storage.
5275 * (This function is somewhat similar to nfsrv_clientconflict(), but
5276 * the semantics differ in a couple of subtle ways. The return of 0
5277 * indicates the conflict was resolved without sleeping here, not
5278 * that the conflict can't be resolved and the handling of nfsv4root_lock
5279 * differs, as noted above.)
5280 * Unlocks State before returning a non-zero value.
5283 nfsrv_delegconflict(struct nfsstate *stp, int *haslockp, NFSPROC_T *p,
5286 struct nfsclient *clp = stp->ls_clp;
5287 int gotlock, error, lktype = 0, retrycnt, zapped_clp;
5288 nfsv4stateid_t tstateid;
5292 * If the conflict is with an old delegation...
5294 if (stp->ls_flags & NFSLCK_OLDDELEG) {
5296 * You can delete it, if it has expired.
5298 if (clp->lc_delegtime < NFSD_MONOSEC) {
5299 nfsrv_freedeleg(stp);
5306 * During this delay, the old delegation could expire or it
5307 * could be recovered by the client via an Open with
5308 * CLAIM_DELEGATE_PREV.
5309 * Release the nfsv4root_lock, if held.
5313 NFSLOCKV4ROOTMUTEX();
5314 nfsv4_unlock(&nfsv4rootfs_lock, 1);
5315 NFSUNLOCKV4ROOTMUTEX();
5317 error = NFSERR_DELAY;
5322 * It's a current delegation, so:
5323 * - check to see if the delegation has expired
5324 * - if so, get the v4root lock and then expire it
5326 if ((stp->ls_flags & NFSLCK_DELEGRECALL) == 0 || (stp->ls_lastrecall <
5327 NFSD_MONOSEC && clp->lc_expiry >= NFSD_MONOSEC &&
5328 stp->ls_delegtime >= NFSD_MONOSEC)) {
5330 * - do a recall callback, since not yet done
5331 * For now, never allow truncate to be set. To use
5332 * truncate safely, it must be guaranteed that the
5333 * Remove, Rename or Setattr with size of 0 will
5334 * succeed and that would require major changes to
5335 * the VFS/Vnode OPs.
5336 * Set the expiry time large enough so that it won't expire
5337 * until after the callback, then set it correctly, once
5338 * the callback is done. (The delegation will now time
5339 * out whether or not the Recall worked ok. The timeout
5340 * will be extended when ops are done on the delegation
5341 * stateid, up to the timelimit.)
5343 if ((stp->ls_flags & NFSLCK_DELEGRECALL) == 0) {
5344 stp->ls_delegtime = NFSD_MONOSEC + (2 * nfsrv_lease) +
5346 stp->ls_delegtimelimit = NFSD_MONOSEC + (6 *
5347 nfsrv_lease) + NFSRV_LEASEDELTA;
5348 stp->ls_flags |= NFSLCK_DELEGRECALL;
5350 stp->ls_lastrecall = time_uptime + 1;
5353 * Loop NFSRV_CBRETRYCNT times while the CBRecall replies
5354 * NFSERR_BADSTATEID or NFSERR_BADHANDLE. This is done
5355 * in order to try and avoid a race that could happen
5356 * when a CBRecall request passed the Open reply with
5357 * the delegation in it when transitting the network.
5358 * Since nfsrv_docallback will sleep, don't use stp after
5361 NFSBCOPY((caddr_t)&stp->ls_stateid, (caddr_t)&tstateid,
5363 NFSBCOPY((caddr_t)&stp->ls_lfp->lf_fh, (caddr_t)&tfh,
5368 NFSLOCKV4ROOTMUTEX();
5369 nfsv4_unlock(&nfsv4rootfs_lock, 1);
5370 NFSUNLOCKV4ROOTMUTEX();
5374 error = nfsrv_docallback(clp, NFSV4OP_CBRECALL,
5375 &tstateid, 0, &tfh, NULL, NULL, 0, p);
5377 } while ((error == NFSERR_BADSTATEID ||
5378 error == NFSERR_BADHANDLE) && retrycnt < NFSV4_CBRETRYCNT);
5379 error = NFSERR_DELAY;
5383 if (clp->lc_expiry >= NFSD_MONOSEC &&
5384 stp->ls_delegtime >= NFSD_MONOSEC) {
5387 * A recall has been done, but it has not yet expired.
5392 NFSLOCKV4ROOTMUTEX();
5393 nfsv4_unlock(&nfsv4rootfs_lock, 1);
5394 NFSUNLOCKV4ROOTMUTEX();
5396 error = NFSERR_DELAY;
5401 * If we don't yet have the lock, just get it and then return,
5402 * since we need that before deleting expired state, such as
5404 * When getting the lock, unlock the vnode, so other nfsds that
5405 * are in progress, won't get stuck waiting for the vnode lock.
5407 if (*haslockp == 0) {
5410 lktype = NFSVOPISLOCKED(vp);
5413 NFSLOCKV4ROOTMUTEX();
5414 nfsv4_relref(&nfsv4rootfs_lock);
5416 gotlock = nfsv4_lock(&nfsv4rootfs_lock, 1, NULL,
5417 NFSV4ROOTLOCKMUTEXPTR, NULL);
5419 NFSUNLOCKV4ROOTMUTEX();
5422 NFSVOPLOCK(vp, lktype | LK_RETRY);
5423 if (VN_IS_DOOMED(vp)) {
5425 NFSLOCKV4ROOTMUTEX();
5426 nfsv4_unlock(&nfsv4rootfs_lock, 1);
5427 NFSUNLOCKV4ROOTMUTEX();
5428 error = NFSERR_PERM;
5438 * Ok, we can delete the expired delegation.
5439 * First, write the Revoke record to stable storage and then
5440 * clear out the conflict.
5441 * Since all other nfsd threads are now blocked, we can safely
5442 * sleep without the state changing.
5444 nfsrv_writestable(clp->lc_id, clp->lc_idlen, NFSNST_REVOKE, p);
5445 nfsrv_backupstable();
5446 if (clp->lc_expiry < NFSD_MONOSEC) {
5447 nfsrv_cleanclient(clp, p);
5448 nfsrv_freedeleglist(&clp->lc_deleg);
5449 nfsrv_freedeleglist(&clp->lc_olddeleg);
5450 LIST_REMOVE(clp, lc_hash);
5453 nfsrv_freedeleg(stp);
5457 nfsrv_zapclient(clp, p);
5466 * Check for a remove allowed, if remove is set to 1 and get rid of
5470 nfsrv_checkremove(vnode_t vp, int remove, struct nfsrv_descript *nd,
5471 nfsquad_t clientid, NFSPROC_T *p)
5473 struct nfsclient *clp;
5474 struct nfsstate *stp;
5475 struct nfslockfile *lfp;
5476 int error, haslock = 0;
5481 * First, get the lock file structure.
5482 * (A return of -1 means no associated state, so remove ok.)
5484 error = nfsrv_getlockfh(vp, NFSLCK_CHECK, NULL, &nfh, p);
5487 if (error == 0 && clientid.qval != 0)
5488 error = nfsrv_getclient(clientid, CLOPS_RENEW, &clp, NULL,
5489 (nfsquad_t)((u_quad_t)0), 0, nd, p);
5491 error = nfsrv_getlockfile(NFSLCK_CHECK, NULL, &lfp, &nfh, 0);
5495 NFSLOCKV4ROOTMUTEX();
5496 nfsv4_unlock(&nfsv4rootfs_lock, 1);
5497 NFSUNLOCKV4ROOTMUTEX();
5505 * Now, we must Recall any delegations.
5507 error = nfsrv_cleandeleg(vp, lfp, clp, &haslock, p);
5510 * nfsrv_cleandeleg() unlocks state for non-zero
5516 NFSLOCKV4ROOTMUTEX();
5517 nfsv4_unlock(&nfsv4rootfs_lock, 1);
5518 NFSUNLOCKV4ROOTMUTEX();
5524 * Now, look for a conflicting open share.
5528 * If the entry in the directory was the last reference to the
5529 * corresponding filesystem object, the object can be destroyed
5531 if(lfp->lf_usecount>1)
5532 LIST_FOREACH(stp, &lfp->lf_open, ls_file) {
5533 if (stp->ls_flags & NFSLCK_WRITEDENY) {
5534 error = NFSERR_FILEOPEN;
5542 NFSLOCKV4ROOTMUTEX();
5543 nfsv4_unlock(&nfsv4rootfs_lock, 1);
5544 NFSUNLOCKV4ROOTMUTEX();
5553 * Clear out all delegations for the file referred to by lfp.
5554 * May return NFSERR_DELAY, if there will be a delay waiting for
5555 * delegations to expire.
5556 * Returns -1 to indicate it slept while recalling a delegation.
5557 * This function has the side effect of deleting the nfslockfile structure,
5558 * if it no longer has associated state and didn't have to sleep.
5559 * Unlocks State before a non-zero value is returned.
5562 nfsrv_cleandeleg(vnode_t vp, struct nfslockfile *lfp,
5563 struct nfsclient *clp, int *haslockp, NFSPROC_T *p)
5565 struct nfsstate *stp, *nstp;
5568 stp = LIST_FIRST(&lfp->lf_deleg);
5569 while (stp != LIST_END(&lfp->lf_deleg)) {
5570 nstp = LIST_NEXT(stp, ls_file);
5571 if (stp->ls_clp != clp) {
5572 ret = nfsrv_delegconflict(stp, haslockp, p, vp);
5575 * nfsrv_delegconflict() unlocks state
5576 * when it returns non-zero.
5589 * There are certain operations that, when being done outside of NFSv4,
5590 * require that any NFSv4 delegation for the file be recalled.
5591 * This function is to be called for those cases:
5592 * VOP_RENAME() - When a delegation is being recalled for any reason,
5593 * the client may have to do Opens against the server, using the file's
5594 * final component name. If the file has been renamed on the server,
5595 * that component name will be incorrect and the Open will fail.
5596 * VOP_REMOVE() - Theoretically, a client could Open a file after it has
5597 * been removed on the server, if there is a delegation issued to
5598 * that client for the file. I say "theoretically" since clients
5599 * normally do an Access Op before the Open and that Access Op will
5600 * fail with ESTALE. Note that NFSv2 and 3 don't even do Opens, so
5601 * they will detect the file's removal in the same manner. (There is
5602 * one case where RFC3530 allows a client to do an Open without first
5603 * doing an Access Op, which is passage of a check against the ACE
5604 * returned with a Write delegation, but current practice is to ignore
5605 * the ACE and always do an Access Op.)
5606 * Since the functions can only be called with an unlocked vnode, this
5607 * can't be done at this time.
5608 * VOP_ADVLOCK() - When a client holds a delegation, it can issue byte range
5609 * locks locally in the client, which are not visible to the server. To
5610 * deal with this, issuing of delegations for a vnode must be disabled
5611 * and all delegations for the vnode recalled. This is done via the
5612 * second function, using the VV_DISABLEDELEG vflag on the vnode.
5615 nfsd_recalldelegation(vnode_t vp, NFSPROC_T *p)
5621 * First, check to see if the server is currently running and it has
5622 * been called for a regular file when issuing delegations.
5624 if (NFSD_VNET(nfsrv_numnfsd) == 0 || vp->v_type != VREG ||
5625 nfsrv_issuedelegs == 0)
5628 KASSERT((NFSVOPISLOCKED(vp) != LK_EXCLUSIVE), ("vp %p is locked", vp));
5630 * First, get a reference on the nfsv4rootfs_lock so that an
5631 * exclusive lock cannot be acquired by another thread.
5633 NFSLOCKV4ROOTMUTEX();
5634 nfsv4_getref(&nfsv4rootfs_lock, NULL, NFSV4ROOTLOCKMUTEXPTR, NULL);
5635 NFSUNLOCKV4ROOTMUTEX();
5638 * Now, call nfsrv_checkremove() in a loop while it returns
5639 * NFSERR_DELAY. Return upon any other error or when timed out.
5641 starttime = NFSD_MONOSEC;
5643 if (NFSVOPLOCK(vp, LK_EXCLUSIVE) == 0) {
5644 error = nfsrv_checkremove(vp, 0, NULL,
5645 (nfsquad_t)((u_quad_t)0), p);
5649 if (error == NFSERR_DELAY) {
5650 if (NFSD_MONOSEC - starttime > NFS_REMOVETIMEO)
5652 /* Sleep for a short period of time */
5653 (void) nfs_catnap(PZERO, 0, "nfsremove");
5655 } while (error == NFSERR_DELAY);
5656 NFSLOCKV4ROOTMUTEX();
5657 nfsv4_relref(&nfsv4rootfs_lock);
5658 NFSUNLOCKV4ROOTMUTEX();
5662 nfsd_disabledelegation(vnode_t vp, NFSPROC_T *p)
5665 #ifdef VV_DISABLEDELEG
5667 * First, flag issuance of delegations disabled.
5669 atomic_set_long(&vp->v_vflag, VV_DISABLEDELEG);
5673 * Then call nfsd_recalldelegation() to get rid of all extant
5676 nfsd_recalldelegation(vp, p);
5680 * Check for conflicting locks, etc. and then get rid of delegations.
5681 * (At one point I thought that I should get rid of delegations for any
5682 * Setattr, since it could potentially disallow the I/O op (read or write)
5683 * allowed by the delegation. However, Setattr Ops that aren't changing
5684 * the size get a stateid of all 0s, so you can't tell if it is a delegation
5685 * for the same client or a different one, so I decided to only get rid
5686 * of delegations for other clients when the size is being changed.)
5687 * In general, a Setattr can disable NFS I/O Ops that are outstanding, such
5688 * as Write backs, even if there is no delegation, so it really isn't any
5692 nfsrv_checksetattr(vnode_t vp, struct nfsrv_descript *nd,
5693 nfsv4stateid_t *stateidp, struct nfsvattr *nvap, nfsattrbit_t *attrbitp,
5694 struct nfsexstuff *exp, NFSPROC_T *p)
5696 struct nfsstate st, *stp = &st;
5697 struct nfslock lo, *lop = &lo;
5701 if (NFSISSET_ATTRBIT(attrbitp, NFSATTRBIT_SIZE)) {
5702 stp->ls_flags = (NFSLCK_CHECK | NFSLCK_WRITEACCESS);
5703 lop->lo_first = nvap->na_size;
5708 if (NFSISSET_ATTRBIT(attrbitp, NFSATTRBIT_OWNER) ||
5709 NFSISSET_ATTRBIT(attrbitp, NFSATTRBIT_OWNERGROUP) ||
5710 NFSISSET_ATTRBIT(attrbitp, NFSATTRBIT_MODE) ||
5711 NFSISSET_ATTRBIT(attrbitp, NFSATTRBIT_ACL))
5712 stp->ls_flags |= NFSLCK_SETATTR;
5713 if (stp->ls_flags == 0)
5715 lop->lo_end = NFS64BITSSET;
5716 lop->lo_flags = NFSLCK_WRITE;
5717 stp->ls_ownerlen = 0;
5719 stp->ls_uid = nd->nd_cred->cr_uid;
5720 stp->ls_stateid.seqid = stateidp->seqid;
5721 clientid.lval[0] = stp->ls_stateid.other[0] = stateidp->other[0];
5722 clientid.lval[1] = stp->ls_stateid.other[1] = stateidp->other[1];
5723 stp->ls_stateid.other[2] = stateidp->other[2];
5724 error = nfsrv_lockctrl(vp, &stp, &lop, NULL, clientid,
5725 stateidp, exp, nd, p);
5728 NFSEXITCODE2(error, nd);
5733 * Check for a write delegation and do a CBGETATTR if there is one, updating
5734 * the attributes, as required.
5735 * Should I return an error if I can't get the attributes? (For now, I'll
5739 nfsrv_checkgetattr(struct nfsrv_descript *nd, vnode_t vp,
5740 struct nfsvattr *nvap, nfsattrbit_t *attrbitp, NFSPROC_T *p)
5742 struct nfsstate *stp;
5743 struct nfslockfile *lfp;
5744 struct nfsclient *clp;
5745 struct nfsvattr nva;
5748 nfsattrbit_t cbbits;
5749 u_quad_t delegfilerev;
5751 NFSCBGETATTR_ATTRBIT(attrbitp, &cbbits);
5752 if (!NFSNONZERO_ATTRBIT(&cbbits))
5754 if (nfsrv_writedelegcnt == 0)
5758 * Get the lock file structure.
5759 * (A return of -1 means no associated state, so return ok.)
5761 error = nfsrv_getlockfh(vp, NFSLCK_CHECK, NULL, &nfh, p);
5764 error = nfsrv_getlockfile(NFSLCK_CHECK, NULL, &lfp, &nfh, 0);
5773 * Now, look for a write delegation.
5775 LIST_FOREACH(stp, &lfp->lf_deleg, ls_file) {
5776 if (stp->ls_flags & NFSLCK_DELEGWRITE)
5779 if (stp == LIST_END(&lfp->lf_deleg)) {
5785 /* If the clientid is not confirmed, ignore the delegation. */
5786 if (clp->lc_flags & LCL_NEEDSCONFIRM) {
5791 delegfilerev = stp->ls_filerev;
5793 * If the Write delegation was issued as a part of this Compound RPC
5794 * or if we have an Implied Clientid (used in a previous Op in this
5795 * compound) and it is the client the delegation was issued to,
5797 * I also assume that it is from the same client iff the network
5798 * host IP address is the same as the callback address. (Not
5799 * exactly correct by the RFC, but avoids a lot of Getattr
5802 if (nd->nd_compref == stp->ls_compref ||
5803 ((nd->nd_flag & ND_IMPLIEDCLID) &&
5804 clp->lc_clientid.qval == nd->nd_clientid.qval) ||
5805 nfsaddr2_match(clp->lc_req.nr_nam, nd->nd_nam)) {
5811 * We are now done with the delegation state structure,
5812 * so the statelock can be released and we can now tsleep().
5816 * Now, we must do the CB Getattr callback, to see if Change or Size
5819 if (clp->lc_expiry >= NFSD_MONOSEC) {
5821 NFSVNO_ATTRINIT(&nva);
5822 nva.na_filerev = NFS64BITSSET;
5823 error = nfsrv_docallback(clp, NFSV4OP_CBGETATTR, NULL,
5824 0, &nfh, &nva, &cbbits, 0, p);
5826 if ((nva.na_filerev != NFS64BITSSET &&
5827 nva.na_filerev > delegfilerev) ||
5828 (NFSVNO_ISSETSIZE(&nva) &&
5829 nva.na_size != nvap->na_size)) {
5830 error = nfsvno_updfilerev(vp, nvap, nd, p);
5831 if (NFSVNO_ISSETSIZE(&nva))
5832 nvap->na_size = nva.na_size;
5835 error = 0; /* Ignore callback errors for now. */
5841 NFSEXITCODE2(error, nd);
5846 * This function looks for openowners that haven't had any opens for
5847 * a while and throws them away. Called by an nfsd when NFSNSF_NOOPENS
5851 nfsrv_throwawayopens(NFSPROC_T *p)
5853 struct nfsclient *clp, *nclp;
5854 struct nfsstate *stp, *nstp;
5858 NFSD_VNET(nfsrv_stablefirst).nsf_flags &= ~NFSNSF_NOOPENS;
5860 * For each client...
5862 for (i = 0; i < nfsrv_clienthashsize; i++) {
5863 LIST_FOREACH_SAFE(clp, &NFSD_VNET(nfsclienthash)[i], lc_hash,
5865 LIST_FOREACH_SAFE(stp, &clp->lc_open, ls_list, nstp) {
5866 if (LIST_EMPTY(&stp->ls_open) &&
5867 (stp->ls_noopens > NFSNOOPEN ||
5868 (nfsrv_openpluslock * 2) >
5869 nfsrv_v4statelimit))
5870 nfsrv_freeopenowner(stp, 0, p);
5878 * This function checks to see if the credentials are the same.
5879 * The check for same credentials is needed for state management operations
5880 * for NFSv4.0 where 1 is returned if not same, 0 is returned otherwise.
5883 nfsrv_notsamecredname(struct nfsrv_descript *nd, struct nfsclient *clp)
5886 /* For NFSv4.1/4.2, SP4_NONE always allows this. */
5887 if ((nd->nd_flag & ND_NFSV41) != 0)
5890 if (nd->nd_flag & ND_GSS) {
5891 if (!(clp->lc_flags & LCL_GSS))
5893 if (clp->lc_flags & LCL_NAME) {
5894 if (nd->nd_princlen != clp->lc_namelen ||
5895 NFSBCMP(nd->nd_principal, clp->lc_name,
5901 if (nd->nd_cred->cr_uid == clp->lc_uid)
5905 } else if (clp->lc_flags & LCL_GSS)
5908 * For AUTH_SYS, allow the same uid or root. (This is underspecified
5909 * in RFC3530, which talks about principals, but doesn't say anything
5910 * about uids for AUTH_SYS.)
5912 if (nd->nd_cred->cr_uid == clp->lc_uid || nd->nd_cred->cr_uid == 0)
5919 * Calculate the lease expiry time.
5922 nfsrv_leaseexpiry(void)
5925 if (NFSD_VNET(nfsrv_stablefirst).nsf_eograce > NFSD_MONOSEC)
5926 return (NFSD_MONOSEC + 2 * (nfsrv_lease + NFSRV_LEASEDELTA));
5927 return (NFSD_MONOSEC + nfsrv_lease + NFSRV_LEASEDELTA);
5931 * Delay the delegation timeout as far as ls_delegtimelimit, as required.
5934 nfsrv_delaydelegtimeout(struct nfsstate *stp)
5937 if ((stp->ls_flags & NFSLCK_DELEGRECALL) == 0)
5940 if ((stp->ls_delegtime + 15) > NFSD_MONOSEC &&
5941 stp->ls_delegtime < stp->ls_delegtimelimit) {
5942 stp->ls_delegtime += nfsrv_lease;
5943 if (stp->ls_delegtime > stp->ls_delegtimelimit)
5944 stp->ls_delegtime = stp->ls_delegtimelimit;
5949 * This function checks to see if there is any other state associated
5950 * with the openowner for this Open.
5951 * It returns 1 if there is no other state, 0 otherwise.
5954 nfsrv_nootherstate(struct nfsstate *stp)
5956 struct nfsstate *tstp;
5958 LIST_FOREACH(tstp, &stp->ls_openowner->ls_open, ls_list) {
5959 if (tstp != stp || !LIST_EMPTY(&tstp->ls_lock))
5966 * Create a list of lock deltas (changes to local byte range locking
5967 * that can be rolled back using the list) and apply the changes via
5968 * nfsvno_advlock(). Optionally, lock the list. It is expected that either
5969 * the rollback or update function will be called after this.
5970 * It returns an error (and rolls back, as required), if any nfsvno_advlock()
5971 * call fails. If it returns an error, it will unlock the list.
5974 nfsrv_locallock(vnode_t vp, struct nfslockfile *lfp, int flags,
5975 uint64_t first, uint64_t end, struct nfslockconflict *cfp, NFSPROC_T *p)
5977 struct nfslock *lop, *nlop;
5980 /* Loop through the list of locks. */
5981 lop = LIST_FIRST(&lfp->lf_locallock);
5982 while (first < end && lop != NULL) {
5983 nlop = LIST_NEXT(lop, lo_lckowner);
5984 if (first >= lop->lo_end) {
5987 } else if (first < lop->lo_first) {
5988 /* new one starts before entry in list */
5989 if (end <= lop->lo_first) {
5990 /* no overlap between old and new */
5991 error = nfsrv_dolocal(vp, lfp, flags,
5992 NFSLCK_UNLOCK, first, end, cfp, p);
5997 /* handle fragment overlapped with new one */
5998 error = nfsrv_dolocal(vp, lfp, flags,
5999 NFSLCK_UNLOCK, first, lop->lo_first, cfp,
6003 first = lop->lo_first;
6006 /* new one overlaps this entry in list */
6007 if (end <= lop->lo_end) {
6008 /* overlaps all of new one */
6009 error = nfsrv_dolocal(vp, lfp, flags,
6010 lop->lo_flags, first, end, cfp, p);
6015 /* handle fragment overlapped with new one */
6016 error = nfsrv_dolocal(vp, lfp, flags,
6017 lop->lo_flags, first, lop->lo_end, cfp, p);
6020 first = lop->lo_end;
6025 if (first < end && error == 0)
6026 /* handle fragment past end of list */
6027 error = nfsrv_dolocal(vp, lfp, flags, NFSLCK_UNLOCK, first,
6035 * Local lock unlock. Unlock all byte ranges that are no longer locked
6036 * by NFSv4. To do this, unlock any subranges of first-->end that
6037 * do not overlap with the byte ranges of any lock in the lfp->lf_lock
6038 * list. This list has all locks for the file held by other
6039 * <clientid, lockowner> tuples. The list is ordered by increasing
6040 * lo_first value, but may have entries that overlap each other, for
6041 * the case of read locks.
6044 nfsrv_localunlock(vnode_t vp, struct nfslockfile *lfp, uint64_t init_first,
6045 uint64_t init_end, NFSPROC_T *p)
6047 struct nfslock *lop;
6048 uint64_t first, end, prevfirst __unused;
6052 while (first < init_end) {
6053 /* Loop through all nfs locks, adjusting first and end */
6055 LIST_FOREACH(lop, &lfp->lf_lock, lo_lckfile) {
6056 KASSERT(prevfirst <= lop->lo_first,
6057 ("nfsv4 locks out of order"));
6058 KASSERT(lop->lo_first < lop->lo_end,
6059 ("nfsv4 bogus lock"));
6060 prevfirst = lop->lo_first;
6061 if (first >= lop->lo_first &&
6062 first < lop->lo_end)
6064 * Overlaps with initial part, so trim
6065 * off that initial part by moving first past
6068 first = lop->lo_end;
6069 else if (end > lop->lo_first &&
6070 lop->lo_first > first) {
6072 * This lock defines the end of the
6073 * segment to unlock, so set end to the
6074 * start of it and break out of the loop.
6076 end = lop->lo_first;
6081 * There is no segment left to do, so
6082 * break out of this loop and then exit
6083 * the outer while() since first will be set
6084 * to end, which must equal init_end here.
6089 /* Unlock this segment */
6090 (void) nfsrv_dolocal(vp, lfp, NFSLCK_UNLOCK,
6091 NFSLCK_READ, first, end, NULL, p);
6092 nfsrv_locallock_commit(lfp, NFSLCK_UNLOCK,
6096 * Now move past this segment and look for any further
6097 * segment in the range, if there is one.
6105 * Do the local lock operation and update the rollback list, as required.
6106 * Perform the rollback and return the error if nfsvno_advlock() fails.
6109 nfsrv_dolocal(vnode_t vp, struct nfslockfile *lfp, int flags, int oldflags,
6110 uint64_t first, uint64_t end, struct nfslockconflict *cfp, NFSPROC_T *p)
6112 struct nfsrollback *rlp;
6113 int error = 0, ltype, oldltype;
6115 if (flags & NFSLCK_WRITE)
6117 else if (flags & NFSLCK_READ)
6121 if (oldflags & NFSLCK_WRITE)
6123 else if (oldflags & NFSLCK_READ)
6127 if (ltype == oldltype || (oldltype == F_WRLCK && ltype == F_RDLCK))
6130 error = nfsvno_advlock(vp, ltype, first, end, p);
6133 cfp->cl_clientid.lval[0] = 0;
6134 cfp->cl_clientid.lval[1] = 0;
6136 cfp->cl_end = NFS64BITSSET;
6137 cfp->cl_flags = NFSLCK_WRITE;
6138 cfp->cl_ownerlen = 5;
6139 NFSBCOPY("LOCAL", cfp->cl_owner, 5);
6141 nfsrv_locallock_rollback(vp, lfp, p);
6142 } else if (ltype != F_UNLCK) {
6143 rlp = malloc(sizeof (struct nfsrollback), M_NFSDROLLBACK,
6145 rlp->rlck_first = first;
6146 rlp->rlck_end = end;
6147 rlp->rlck_type = oldltype;
6148 LIST_INSERT_HEAD(&lfp->lf_rollback, rlp, rlck_list);
6157 * Roll back local lock changes and free up the rollback list.
6160 nfsrv_locallock_rollback(vnode_t vp, struct nfslockfile *lfp, NFSPROC_T *p)
6162 struct nfsrollback *rlp, *nrlp;
6164 LIST_FOREACH_SAFE(rlp, &lfp->lf_rollback, rlck_list, nrlp) {
6165 (void) nfsvno_advlock(vp, rlp->rlck_type, rlp->rlck_first,
6167 free(rlp, M_NFSDROLLBACK);
6169 LIST_INIT(&lfp->lf_rollback);
6173 * Update local lock list and delete rollback list (ie now committed to the
6174 * local locks). Most of the work is done by the internal function.
6177 nfsrv_locallock_commit(struct nfslockfile *lfp, int flags, uint64_t first,
6180 struct nfsrollback *rlp, *nrlp;
6181 struct nfslock *new_lop, *other_lop;
6183 new_lop = malloc(sizeof (struct nfslock), M_NFSDLOCK, M_WAITOK);
6184 if (flags & (NFSLCK_READ | NFSLCK_WRITE))
6185 other_lop = malloc(sizeof (struct nfslock), M_NFSDLOCK,
6189 new_lop->lo_flags = flags;
6190 new_lop->lo_first = first;
6191 new_lop->lo_end = end;
6192 nfsrv_updatelock(NULL, &new_lop, &other_lop, lfp);
6193 if (new_lop != NULL)
6194 free(new_lop, M_NFSDLOCK);
6195 if (other_lop != NULL)
6196 free(other_lop, M_NFSDLOCK);
6198 /* and get rid of the rollback list */
6199 LIST_FOREACH_SAFE(rlp, &lfp->lf_rollback, rlck_list, nrlp)
6200 free(rlp, M_NFSDROLLBACK);
6201 LIST_INIT(&lfp->lf_rollback);
6205 * Lock the struct nfslockfile for local lock updating.
6208 nfsrv_locklf(struct nfslockfile *lfp)
6212 /* lf_usecount ensures *lfp won't be free'd */
6215 gotlock = nfsv4_lock(&lfp->lf_locallock_lck, 1, NULL,
6216 NFSSTATEMUTEXPTR, NULL);
6217 } while (gotlock == 0);
6222 * Unlock the struct nfslockfile after local lock updating.
6225 nfsrv_unlocklf(struct nfslockfile *lfp)
6228 nfsv4_unlock(&lfp->lf_locallock_lck, 0);
6232 * Clear out all state for the NFSv4 server.
6233 * Must be called by a thread that can sleep when no nfsds are running.
6236 nfsrv_throwawayallstate(NFSPROC_T *p)
6238 struct nfsclient *clp, *nclp;
6239 struct nfslockfile *lfp, *nlfp;
6243 * For each client, clean out the state and then free the structure.
6245 for (i = 0; i < nfsrv_clienthashsize; i++) {
6246 LIST_FOREACH_SAFE(clp, &NFSD_VNET(nfsclienthash)[i], lc_hash,
6248 nfsrv_cleanclient(clp, p);
6249 nfsrv_freedeleglist(&clp->lc_deleg);
6250 nfsrv_freedeleglist(&clp->lc_olddeleg);
6251 free(clp->lc_stateid, M_NFSDCLIENT);
6252 free(clp, M_NFSDCLIENT);
6257 * Also, free up any remaining lock file structures.
6259 for (i = 0; i < nfsrv_lockhashsize; i++) {
6260 LIST_FOREACH_SAFE(lfp, &NFSD_VNET(nfslockhash)[i], lf_hash,
6262 printf("nfsd unload: fnd a lock file struct\n");
6263 nfsrv_freenfslockfile(lfp);
6267 /* And get rid of the deviceid structures and layouts. */
6268 nfsrv_freealllayoutsanddevids();
6272 * Check the sequence# for the session and slot provided as an argument.
6273 * Also, renew the lease if the session will return NFS_OK.
6276 nfsrv_checksequence(struct nfsrv_descript *nd, uint32_t sequenceid,
6277 uint32_t *highest_slotidp, uint32_t *target_highest_slotidp, int cache_this,
6278 uint32_t *sflagsp, NFSPROC_T *p)
6280 struct nfsdsession *sep;
6281 struct nfssessionhash *shp;
6284 shp = NFSSESSIONHASH(nd->nd_sessionid);
6285 NFSLOCKSESSION(shp);
6286 sep = nfsrv_findsession(nd->nd_sessionid);
6288 NFSUNLOCKSESSION(shp);
6289 return (NFSERR_BADSESSION);
6291 error = nfsv4_seqsession(sequenceid, nd->nd_slotid, *highest_slotidp,
6292 sep->sess_slots, NULL, NFSV4_SLOTS - 1);
6294 NFSUNLOCKSESSION(shp);
6297 if (cache_this != 0)
6298 nd->nd_flag |= ND_SAVEREPLY;
6299 /* Renew the lease. */
6300 sep->sess_clp->lc_expiry = nfsrv_leaseexpiry();
6301 nd->nd_clientid.qval = sep->sess_clp->lc_clientid.qval;
6302 nd->nd_flag |= ND_IMPLIEDCLID;
6304 /* Save maximum request and reply sizes. */
6305 nd->nd_maxreq = sep->sess_maxreq;
6306 nd->nd_maxresp = sep->sess_maxresp;
6309 if (sep->sess_clp->lc_req.nr_client == NULL ||
6310 (sep->sess_clp->lc_flags & LCL_CBDOWN) != 0)
6311 *sflagsp |= NFSV4SEQ_CBPATHDOWN;
6312 NFSUNLOCKSESSION(shp);
6313 if (error == NFSERR_EXPIRED) {
6314 *sflagsp |= NFSV4SEQ_EXPIREDALLSTATEREVOKED;
6316 } else if (error == NFSERR_ADMINREVOKED) {
6317 *sflagsp |= NFSV4SEQ_ADMINSTATEREVOKED;
6320 *highest_slotidp = *target_highest_slotidp = NFSV4_SLOTS - 1;
6325 * Check/set reclaim complete for this session/clientid.
6328 nfsrv_checkreclaimcomplete(struct nfsrv_descript *nd, int onefs)
6330 struct nfsdsession *sep;
6331 struct nfssessionhash *shp;
6334 shp = NFSSESSIONHASH(nd->nd_sessionid);
6336 NFSLOCKSESSION(shp);
6337 sep = nfsrv_findsession(nd->nd_sessionid);
6339 NFSUNLOCKSESSION(shp);
6341 return (NFSERR_BADSESSION);
6345 sep->sess_clp->lc_flags |= LCL_RECLAIMONEFS;
6346 /* Check to see if reclaim complete has already happened. */
6347 else if ((sep->sess_clp->lc_flags & LCL_RECLAIMCOMPLETE) != 0)
6348 error = NFSERR_COMPLETEALREADY;
6350 sep->sess_clp->lc_flags |= LCL_RECLAIMCOMPLETE;
6351 nfsrv_markreclaim(sep->sess_clp);
6353 NFSUNLOCKSESSION(shp);
6359 * Cache the reply in a session slot.
6362 nfsrv_cache_session(struct nfsrv_descript *nd, struct mbuf **m)
6364 struct nfsdsession *sep;
6365 struct nfssessionhash *shp;
6368 struct sockaddr_in *sin;
6371 struct sockaddr_in6 *sin6;
6374 shp = NFSSESSIONHASH(nd->nd_sessionid);
6375 NFSLOCKSESSION(shp);
6376 sep = nfsrv_findsession(nd->nd_sessionid);
6378 NFSUNLOCKSESSION(shp);
6379 if ((NFSD_VNET(nfsrv_stablefirst).nsf_flags &
6380 NFSNSF_GRACEOVER) != 0) {
6381 buf = malloc(INET6_ADDRSTRLEN, M_TEMP, M_WAITOK);
6382 switch (nd->nd_nam->sa_family) {
6385 sin = (struct sockaddr_in *)nd->nd_nam;
6386 cp = inet_ntop(sin->sin_family,
6387 &sin->sin_addr.s_addr, buf,
6393 sin6 = (struct sockaddr_in6 *)nd->nd_nam;
6394 cp = inet_ntop(sin6->sin6_family,
6395 &sin6->sin6_addr, buf, INET6_ADDRSTRLEN);
6402 printf("nfsrv_cache_session: no session "
6403 "IPaddr=%s, check NFS clients for unique "
6404 "/etc/hostid's\n", cp);
6406 printf("nfsrv_cache_session: no session, "
6407 "check NFS clients for unique "
6414 nfsv4_seqsess_cacherep(nd->nd_slotid, sep->sess_slots, nd->nd_repstat,
6416 NFSUNLOCKSESSION(shp);
6420 * Search for a session that matches the sessionid.
6422 static struct nfsdsession *
6423 nfsrv_findsession(uint8_t *sessionid)
6425 struct nfsdsession *sep;
6426 struct nfssessionhash *shp;
6428 shp = NFSSESSIONHASH(sessionid);
6429 LIST_FOREACH(sep, &shp->list, sess_hash) {
6430 if (!NFSBCMP(sessionid, sep->sess_sessionid, NFSX_V4SESSIONID))
6437 * Destroy a session.
6440 nfsrv_destroysession(struct nfsrv_descript *nd, uint8_t *sessionid)
6442 int error, igotlock, samesess;
6445 if (!NFSBCMP(sessionid, nd->nd_sessionid, NFSX_V4SESSIONID) &&
6446 (nd->nd_flag & ND_HASSEQUENCE) != 0) {
6448 if ((nd->nd_flag & ND_LASTOP) == 0)
6449 return (NFSERR_BADSESSION);
6452 /* Lock out other nfsd threads */
6453 NFSLOCKV4ROOTMUTEX();
6454 nfsv4_relref(&nfsv4rootfs_lock);
6456 igotlock = nfsv4_lock(&nfsv4rootfs_lock, 1, NULL,
6457 NFSV4ROOTLOCKMUTEXPTR, NULL);
6458 } while (igotlock == 0);
6459 NFSUNLOCKV4ROOTMUTEX();
6461 error = nfsrv_freesession(NULL, sessionid);
6462 if (error == 0 && samesess != 0)
6463 nd->nd_flag &= ~ND_HASSEQUENCE;
6465 NFSLOCKV4ROOTMUTEX();
6466 nfsv4_unlock(&nfsv4rootfs_lock, 1);
6467 NFSUNLOCKV4ROOTMUTEX();
6472 * Bind a connection to a session.
6473 * For now, only certain variants are supported, since the current session
6474 * structure can only handle a single backchannel entry, which will be
6475 * applied to all connections if it is set.
6478 nfsrv_bindconnsess(struct nfsrv_descript *nd, uint8_t *sessionid, int *foreaftp)
6480 struct nfssessionhash *shp;
6481 struct nfsdsession *sep;
6482 struct nfsclient *clp;
6488 shp = NFSSESSIONHASH(sessionid);
6490 NFSLOCKSESSION(shp);
6491 sep = nfsrv_findsession(sessionid);
6493 clp = sep->sess_clp;
6494 if (*foreaftp == NFSCDFC4_BACK ||
6495 *foreaftp == NFSCDFC4_BACK_OR_BOTH ||
6496 *foreaftp == NFSCDFC4_FORE_OR_BOTH) {
6497 /* Try to set up a backchannel. */
6498 if (clp->lc_req.nr_client == NULL) {
6499 NFSD_DEBUG(2, "nfsrv_bindconnsess: acquire "
6501 clp->lc_req.nr_client = (struct __rpc_client *)
6502 clnt_bck_create(nd->nd_xprt->xp_socket,
6503 sep->sess_cbprogram, NFSV4_CBVERS);
6505 if (clp->lc_req.nr_client != NULL) {
6506 NFSD_DEBUG(2, "nfsrv_bindconnsess: set up "
6508 savxprt = sep->sess_cbsess.nfsess_xprt;
6509 SVC_ACQUIRE(nd->nd_xprt);
6510 CLNT_ACQUIRE(clp->lc_req.nr_client);
6511 nd->nd_xprt->xp_p2 = clp->lc_req.nr_client;
6512 /* Disable idle timeout. */
6513 nd->nd_xprt->xp_idletimeout = 0;
6514 sep->sess_cbsess.nfsess_xprt = nd->nd_xprt;
6515 sep->sess_crflags |= NFSV4CRSESS_CONNBACKCHAN;
6516 clp->lc_flags |= LCL_DONEBINDCONN |
6518 clp->lc_flags &= ~LCL_CBDOWN;
6519 if (*foreaftp == NFSCDFS4_BACK)
6520 *foreaftp = NFSCDFS4_BACK;
6522 *foreaftp = NFSCDFS4_BOTH;
6523 } else if (*foreaftp != NFSCDFC4_BACK) {
6524 NFSD_DEBUG(2, "nfsrv_bindconnsess: can't set "
6525 "up backchannel\n");
6526 sep->sess_crflags &= ~NFSV4CRSESS_CONNBACKCHAN;
6527 clp->lc_flags |= LCL_DONEBINDCONN;
6528 *foreaftp = NFSCDFS4_FORE;
6530 error = NFSERR_NOTSUPP;
6531 printf("nfsrv_bindconnsess: Can't add "
6535 NFSD_DEBUG(2, "nfsrv_bindconnsess: Set forechannel\n");
6536 clp->lc_flags |= LCL_DONEBINDCONN;
6537 *foreaftp = NFSCDFS4_FORE;
6540 error = NFSERR_BADSESSION;
6541 NFSUNLOCKSESSION(shp);
6543 if (savxprt != NULL)
6544 SVC_RELEASE(savxprt);
6549 * Free up a session structure.
6552 nfsrv_freesession(struct nfsdsession *sep, uint8_t *sessionid)
6554 struct nfssessionhash *shp;
6559 shp = NFSSESSIONHASH(sessionid);
6560 NFSLOCKSESSION(shp);
6561 sep = nfsrv_findsession(sessionid);
6563 shp = NFSSESSIONHASH(sep->sess_sessionid);
6564 NFSLOCKSESSION(shp);
6568 if (sep->sess_refcnt > 0) {
6569 NFSUNLOCKSESSION(shp);
6571 return (NFSERR_BACKCHANBUSY);
6573 LIST_REMOVE(sep, sess_hash);
6574 LIST_REMOVE(sep, sess_list);
6576 NFSUNLOCKSESSION(shp);
6579 return (NFSERR_BADSESSION);
6580 for (i = 0; i < NFSV4_SLOTS; i++)
6581 if (sep->sess_slots[i].nfssl_reply != NULL)
6582 m_freem(sep->sess_slots[i].nfssl_reply);
6583 if (sep->sess_cbsess.nfsess_xprt != NULL)
6584 SVC_RELEASE(sep->sess_cbsess.nfsess_xprt);
6585 free(sep, M_NFSDSESSION);
6591 * RFC5661 says that it should fail when there are associated opens, locks
6592 * or delegations. Since stateids represent opens, I don't see how you can
6593 * free an open stateid (it will be free'd when closed), so this function
6594 * only works for lock stateids (freeing the lock_owner) or delegations.
6597 nfsrv_freestateid(struct nfsrv_descript *nd, nfsv4stateid_t *stateidp,
6600 struct nfsclient *clp;
6601 struct nfsstate *stp;
6606 * Look up the stateid
6608 error = nfsrv_getclient((nfsquad_t)((u_quad_t)0), CLOPS_RENEW, &clp,
6609 NULL, (nfsquad_t)((u_quad_t)0), 0, nd, p);
6611 /* First, check for a delegation. */
6612 LIST_FOREACH(stp, &clp->lc_deleg, ls_list) {
6613 if (!NFSBCMP(stp->ls_stateid.other, stateidp->other,
6618 nfsrv_freedeleg(stp);
6623 /* Not a delegation, try for a lock_owner. */
6625 error = nfsrv_getstate(clp, stateidp, 0, &stp);
6626 if (error == 0 && ((stp->ls_flags & (NFSLCK_OPEN | NFSLCK_DELEGREAD |
6627 NFSLCK_DELEGWRITE)) != 0 || (stp->ls_flags & NFSLCK_LOCK) == 0))
6628 /* Not a lock_owner stateid. */
6629 error = NFSERR_LOCKSHELD;
6630 if (error == 0 && !LIST_EMPTY(&stp->ls_lock))
6631 error = NFSERR_LOCKSHELD;
6633 nfsrv_freelockowner(stp, NULL, 0, p);
6642 nfsrv_teststateid(struct nfsrv_descript *nd, nfsv4stateid_t *stateidp,
6645 struct nfsclient *clp;
6646 struct nfsstate *stp;
6651 * Look up the stateid
6653 error = nfsrv_getclient((nfsquad_t)((u_quad_t)0), CLOPS_RENEW, &clp,
6654 NULL, (nfsquad_t)((u_quad_t)0), 0, nd, p);
6656 error = nfsrv_getstate(clp, stateidp, 0, &stp);
6657 if (error == 0 && stateidp->seqid != 0 &&
6658 SEQ_LT(stateidp->seqid, stp->ls_stateid.seqid))
6659 error = NFSERR_OLDSTATEID;
6665 * Generate the xdr for an NFSv4.1 CBSequence Operation.
6668 nfsv4_setcbsequence(struct nfsrv_descript *nd, struct nfsclient *clp,
6669 int dont_replycache, struct nfsdsession **sepp, int *slotposp)
6671 struct nfsdsession *sep;
6672 uint32_t *tl, slotseq = 0;
6674 uint8_t sessionid[NFSX_V4SESSIONID];
6677 error = nfsv4_getcbsession(clp, sepp);
6681 nfsv4_sequencelookup(NULL, &sep->sess_cbsess, slotposp, &maxslot,
6682 &slotseq, sessionid, true);
6683 KASSERT(maxslot >= 0, ("nfsv4_setcbsequence neg maxslot"));
6685 /* Build the Sequence arguments. */
6686 NFSM_BUILD(tl, uint32_t *, NFSX_V4SESSIONID + 5 * NFSX_UNSIGNED);
6687 bcopy(sessionid, tl, NFSX_V4SESSIONID);
6688 tl += NFSX_V4SESSIONID / NFSX_UNSIGNED;
6689 nd->nd_slotseq = tl;
6690 nd->nd_slotid = *slotposp;
6691 nd->nd_flag |= ND_HASSLOTID;
6692 *tl++ = txdr_unsigned(slotseq);
6693 *tl++ = txdr_unsigned(*slotposp);
6694 *tl++ = txdr_unsigned(maxslot);
6695 if (dont_replycache == 0)
6696 *tl++ = newnfs_true;
6698 *tl++ = newnfs_false;
6699 *tl = 0; /* No referring call list, for now. */
6700 nd->nd_flag |= ND_HASSEQUENCE;
6705 * Get a session for the callback.
6708 nfsv4_getcbsession(struct nfsclient *clp, struct nfsdsession **sepp)
6710 struct nfsdsession *sep;
6713 LIST_FOREACH(sep, &clp->lc_session, sess_list) {
6714 if ((sep->sess_crflags & NFSV4CRSESS_CONNBACKCHAN) != 0)
6719 return (NFSERR_BADSESSION);
6728 * Free up all backchannel xprts. This needs to be done when the nfsd threads
6729 * exit, since those transports will all be going away.
6730 * This is only called after all the nfsd threads are done performing RPCs,
6731 * so locking shouldn't be an issue.
6734 nfsrv_freeallbackchannel_xprts(void)
6736 struct nfsdsession *sep;
6737 struct nfsclient *clp;
6741 for (i = 0; i < nfsrv_clienthashsize; i++) {
6742 LIST_FOREACH(clp, &NFSD_VNET(nfsclienthash)[i], lc_hash) {
6743 LIST_FOREACH(sep, &clp->lc_session, sess_list) {
6744 xprt = sep->sess_cbsess.nfsess_xprt;
6745 sep->sess_cbsess.nfsess_xprt = NULL;
6754 * Do a layout commit. Actually just call nfsrv_updatemdsattr().
6755 * I have no idea if the rest of these arguments will ever be useful?
6758 nfsrv_layoutcommit(struct nfsrv_descript *nd, vnode_t vp, int layouttype,
6759 int hasnewoff, uint64_t newoff, uint64_t offset, uint64_t len,
6760 int hasnewmtime, struct timespec *newmtimep, int reclaim,
6761 nfsv4stateid_t *stateidp, int maxcnt, char *layp, int *hasnewsizep,
6762 uint64_t *newsizep, struct ucred *cred, NFSPROC_T *p)
6767 error = nfsrv_updatemdsattr(vp, &na, p);
6770 *newsizep = na.na_size;
6776 * Try and get a layout.
6779 nfsrv_layoutget(struct nfsrv_descript *nd, vnode_t vp, struct nfsexstuff *exp,
6780 int layouttype, int *iomode, uint64_t *offset, uint64_t *len,
6781 uint64_t minlen, nfsv4stateid_t *stateidp, int maxcnt, int *retonclose,
6782 int *layoutlenp, char *layp, struct ucred *cred, NFSPROC_T *p)
6784 struct nfslayouthash *lhyp;
6785 struct nfslayout *lyp;
6787 fhandle_t fh, *dsfhp;
6788 int error, mirrorcnt;
6790 if (nfsrv_devidcnt == 0)
6791 return (NFSERR_UNKNLAYOUTTYPE);
6794 printf("nfsrv_layoutget: off=%ju len=%ju\n", (uintmax_t)*offset,
6796 error = nfsvno_getfh(vp, &fh, p);
6797 NFSD_DEBUG(4, "layoutget getfh=%d\n", error);
6802 * For now, all layouts are for entire files.
6803 * Only issue Read/Write layouts if requested for a non-readonly fs.
6805 if (NFSVNO_EXRDONLY(exp)) {
6806 if (*iomode == NFSLAYOUTIOMODE_RW)
6807 return (NFSERR_LAYOUTTRYLATER);
6808 *iomode = NFSLAYOUTIOMODE_READ;
6810 if (*iomode != NFSLAYOUTIOMODE_RW)
6811 *iomode = NFSLAYOUTIOMODE_READ;
6814 * Check to see if a write layout can be issued for this file.
6815 * This is used during mirror recovery to avoid RW layouts being
6816 * issued for a file while it is being copied to the recovered
6819 if (*iomode == NFSLAYOUTIOMODE_RW && nfsrv_dontlayout(&fh) != 0)
6820 return (NFSERR_LAYOUTTRYLATER);
6826 /* First, see if a layout already exists and return if found. */
6827 lhyp = NFSLAYOUTHASH(&fh);
6828 NFSLOCKLAYOUT(lhyp);
6829 error = nfsrv_findlayout(&nd->nd_clientid, &fh, layouttype, p, &lyp);
6830 NFSD_DEBUG(4, "layoutget findlay=%d\n", error);
6832 * Not sure if the seqid must be the same, so I won't check it.
6834 if (error == 0 && (stateidp->other[0] != lyp->lay_stateid.other[0] ||
6835 stateidp->other[1] != lyp->lay_stateid.other[1] ||
6836 stateidp->other[2] != lyp->lay_stateid.other[2])) {
6837 if ((lyp->lay_flags & NFSLAY_CALLB) == 0) {
6838 NFSUNLOCKLAYOUT(lhyp);
6839 NFSD_DEBUG(1, "ret bad stateid\n");
6840 return (NFSERR_BADSTATEID);
6843 * I believe we get here because there is a race between
6844 * the client processing the CBLAYOUTRECALL and the layout
6845 * being deleted here on the server.
6846 * The client has now done a LayoutGet with a non-layout
6847 * stateid, as it would when there is no layout.
6848 * As such, free this layout and set error == NFSERR_BADSTATEID
6849 * so the code below will create a new layout structure as
6850 * would happen if no layout was found.
6851 * "lyp" will be set before being used below, but set it NULL
6854 nfsrv_freelayout(&lhyp->list, lyp);
6856 error = NFSERR_BADSTATEID;
6859 if (lyp->lay_layoutlen > maxcnt) {
6860 NFSUNLOCKLAYOUT(lhyp);
6861 NFSD_DEBUG(1, "ret layout too small\n");
6862 return (NFSERR_TOOSMALL);
6864 if (*iomode == NFSLAYOUTIOMODE_RW) {
6865 if ((lyp->lay_flags & NFSLAY_NOSPC) != 0) {
6866 NFSUNLOCKLAYOUT(lhyp);
6867 NFSD_DEBUG(1, "ret layout nospace\n");
6868 return (NFSERR_NOSPC);
6870 lyp->lay_flags |= NFSLAY_RW;
6872 lyp->lay_flags |= NFSLAY_READ;
6873 NFSBCOPY(lyp->lay_xdr, layp, lyp->lay_layoutlen);
6874 *layoutlenp = lyp->lay_layoutlen;
6875 if (++lyp->lay_stateid.seqid == 0)
6876 lyp->lay_stateid.seqid = 1;
6877 stateidp->seqid = lyp->lay_stateid.seqid;
6878 NFSUNLOCKLAYOUT(lhyp);
6879 NFSD_DEBUG(4, "ret fnd layout\n");
6882 NFSUNLOCKLAYOUT(lhyp);
6884 /* Find the device id and file handle. */
6885 dsfhp = malloc(sizeof(fhandle_t) * NFSDEV_MAXMIRRORS, M_TEMP, M_WAITOK);
6886 devid = malloc(NFSX_V4DEVICEID * NFSDEV_MAXMIRRORS, M_TEMP, M_WAITOK);
6887 error = nfsrv_dsgetdevandfh(vp, p, &mirrorcnt, dsfhp, devid);
6888 NFSD_DEBUG(4, "layoutget devandfh=%d\n", error);
6890 if (layouttype == NFSLAYOUT_NFSV4_1_FILES) {
6891 if (NFSX_V4FILELAYOUT > maxcnt)
6892 error = NFSERR_TOOSMALL;
6894 lyp = nfsrv_filelayout(nd, *iomode, &fh, dsfhp,
6895 devid, vp->v_mount->mnt_stat.f_fsid);
6897 if (NFSX_V4FLEXLAYOUT(mirrorcnt) > maxcnt)
6898 error = NFSERR_TOOSMALL;
6900 lyp = nfsrv_flexlayout(nd, *iomode, mirrorcnt,
6902 vp->v_mount->mnt_stat.f_fsid);
6905 free(dsfhp, M_TEMP);
6906 free(devid, M_TEMP);
6911 * Now, add this layout to the list.
6913 error = nfsrv_addlayout(nd, &lyp, stateidp, layp, layoutlenp, p);
6914 NFSD_DEBUG(4, "layoutget addl=%d\n", error);
6916 * The lyp will be set to NULL by nfsrv_addlayout() if it
6917 * linked the new structure into the lists.
6919 free(lyp, M_NFSDSTATE);
6924 * Generate a File Layout.
6926 static struct nfslayout *
6927 nfsrv_filelayout(struct nfsrv_descript *nd, int iomode, fhandle_t *fhp,
6928 fhandle_t *dsfhp, char *devid, fsid_t fs)
6931 struct nfslayout *lyp;
6932 uint64_t pattern_offset;
6934 lyp = malloc(sizeof(struct nfslayout) + NFSX_V4FILELAYOUT, M_NFSDSTATE,
6936 lyp->lay_type = NFSLAYOUT_NFSV4_1_FILES;
6937 if (iomode == NFSLAYOUTIOMODE_RW)
6938 lyp->lay_flags = NFSLAY_RW;
6940 lyp->lay_flags = NFSLAY_READ;
6941 NFSBCOPY(fhp, &lyp->lay_fh, sizeof(*fhp));
6942 lyp->lay_clientid.qval = nd->nd_clientid.qval;
6944 NFSBCOPY(devid, lyp->lay_deviceid, NFSX_V4DEVICEID);
6946 /* Fill in the xdr for the files layout. */
6947 tl = (uint32_t *)lyp->lay_xdr;
6948 NFSBCOPY(devid, tl, NFSX_V4DEVICEID); /* Device ID. */
6949 tl += (NFSX_V4DEVICEID / NFSX_UNSIGNED);
6951 /* Set the stripe size to the maximum I/O size. */
6952 *tl++ = txdr_unsigned(nfs_srvmaxio & NFSFLAYUTIL_STRIPE_MASK);
6953 *tl++ = 0; /* 1st stripe index. */
6955 txdr_hyper(pattern_offset, tl); tl += 2; /* Pattern offset. */
6956 *tl++ = txdr_unsigned(1); /* 1 file handle. */
6957 *tl++ = txdr_unsigned(NFSX_V4PNFSFH);
6958 NFSBCOPY(dsfhp, tl, sizeof(*dsfhp));
6959 lyp->lay_layoutlen = NFSX_V4FILELAYOUT;
6963 #define FLEX_OWNERID "999"
6964 #define FLEX_UID0 "0"
6966 * Generate a Flex File Layout.
6967 * The FLEX_OWNERID can be any string of 3 decimal digits. Although this
6968 * string goes on the wire, it isn't supposed to be used by the client,
6969 * since this server uses tight coupling.
6970 * Although not recommended by the spec., if vfs.nfsd.flexlinuxhack=1 use
6971 * a string of "0". This works around the Linux Flex File Layout driver bug
6972 * which uses the synthetic uid/gid strings for the "tightly coupled" case.
6974 static struct nfslayout *
6975 nfsrv_flexlayout(struct nfsrv_descript *nd, int iomode, int mirrorcnt,
6976 fhandle_t *fhp, fhandle_t *dsfhp, char *devid, fsid_t fs)
6979 struct nfslayout *lyp;
6983 lyp = malloc(sizeof(struct nfslayout) + NFSX_V4FLEXLAYOUT(mirrorcnt),
6984 M_NFSDSTATE, M_WAITOK | M_ZERO);
6985 lyp->lay_type = NFSLAYOUT_FLEXFILE;
6986 if (iomode == NFSLAYOUTIOMODE_RW)
6987 lyp->lay_flags = NFSLAY_RW;
6989 lyp->lay_flags = NFSLAY_READ;
6990 NFSBCOPY(fhp, &lyp->lay_fh, sizeof(*fhp));
6991 lyp->lay_clientid.qval = nd->nd_clientid.qval;
6993 lyp->lay_mirrorcnt = mirrorcnt;
6994 NFSBCOPY(devid, lyp->lay_deviceid, NFSX_V4DEVICEID);
6996 /* Fill in the xdr for the files layout. */
6997 tl = (uint32_t *)lyp->lay_xdr;
6999 txdr_hyper(lenval, tl); tl += 2; /* Stripe unit. */
7000 *tl++ = txdr_unsigned(mirrorcnt); /* # of mirrors. */
7001 for (i = 0; i < mirrorcnt; i++) {
7002 *tl++ = txdr_unsigned(1); /* One stripe. */
7003 NFSBCOPY(devid, tl, NFSX_V4DEVICEID); /* Device ID. */
7004 tl += (NFSX_V4DEVICEID / NFSX_UNSIGNED);
7005 devid += NFSX_V4DEVICEID;
7006 *tl++ = txdr_unsigned(1); /* Efficiency. */
7007 *tl++ = 0; /* Proxy Stateid. */
7011 *tl++ = txdr_unsigned(1); /* 1 file handle. */
7012 *tl++ = txdr_unsigned(NFSX_V4PNFSFH);
7013 NFSBCOPY(dsfhp, tl, sizeof(*dsfhp));
7014 tl += (NFSM_RNDUP(NFSX_V4PNFSFH) / NFSX_UNSIGNED);
7016 if (nfsrv_flexlinuxhack != 0) {
7017 *tl++ = txdr_unsigned(strlen(FLEX_UID0));
7018 *tl = 0; /* 0 pad string. */
7019 NFSBCOPY(FLEX_UID0, tl++, strlen(FLEX_UID0));
7020 *tl++ = txdr_unsigned(strlen(FLEX_UID0));
7021 *tl = 0; /* 0 pad string. */
7022 NFSBCOPY(FLEX_UID0, tl++, strlen(FLEX_UID0));
7024 *tl++ = txdr_unsigned(strlen(FLEX_OWNERID));
7025 NFSBCOPY(FLEX_OWNERID, tl++, NFSX_UNSIGNED);
7026 *tl++ = txdr_unsigned(strlen(FLEX_OWNERID));
7027 NFSBCOPY(FLEX_OWNERID, tl++, NFSX_UNSIGNED);
7030 *tl++ = txdr_unsigned(0); /* ff_flags. */
7031 *tl = txdr_unsigned(60); /* Status interval hint. */
7032 lyp->lay_layoutlen = NFSX_V4FLEXLAYOUT(mirrorcnt);
7037 * Parse and process Flex File errors returned via LayoutReturn.
7040 nfsrv_flexlayouterr(struct nfsrv_descript *nd, uint32_t *layp, int maxcnt,
7044 int cnt, errcnt, i, j, opnum, stat;
7045 char devid[NFSX_V4DEVICEID];
7048 maxcnt -= NFSX_UNSIGNED;
7050 cnt = fxdr_unsigned(int, *tl++);
7053 NFSD_DEBUG(4, "flexlayouterr cnt=%d\n", cnt);
7054 for (i = 0; i < cnt; i++) {
7055 maxcnt -= NFSX_STATEID + 2 * NFSX_HYPER +
7059 /* Skip offset, length and stateid for now. */
7060 tl += (4 + NFSX_STATEID / NFSX_UNSIGNED);
7061 errcnt = fxdr_unsigned(int, *tl++);
7062 NFSD_DEBUG(4, "flexlayouterr errcnt=%d\n", errcnt);
7063 for (j = 0; j < errcnt; j++) {
7064 maxcnt -= NFSX_V4DEVICEID + 2 * NFSX_UNSIGNED;
7067 NFSBCOPY(tl, devid, NFSX_V4DEVICEID);
7068 tl += (NFSX_V4DEVICEID / NFSX_UNSIGNED);
7069 stat = fxdr_unsigned(int, *tl++);
7070 opnum = fxdr_unsigned(int, *tl++);
7071 NFSD_DEBUG(4, "flexlayouterr op=%d stat=%d\n", opnum,
7074 * Except for NFSERR_ACCES, NFSERR_STALE and
7075 * NFSERR_NOSPC errors, disable the mirror.
7077 if (stat != NFSERR_ACCES && stat != NFSERR_STALE &&
7078 stat != NFSERR_NOSPC)
7079 nfsrv_delds(devid, p);
7081 /* For NFSERR_NOSPC, mark all devids and layouts. */
7082 if (stat == NFSERR_NOSPC)
7083 nfsrv_marknospc(devid, true);
7089 * This function removes all flex file layouts which has a mirror with
7090 * a device id that matches the argument.
7091 * Called when the DS represented by the device id has failed.
7094 nfsrv_flexmirrordel(char *devid, NFSPROC_T *p)
7097 struct nfslayout *lyp, *nlyp;
7098 struct nfslayouthash *lhyp;
7099 struct nfslayouthead loclyp;
7102 NFSD_DEBUG(4, "flexmirrordel\n");
7103 /* Move all layouts found onto a local list. */
7104 TAILQ_INIT(&loclyp);
7105 for (i = 0; i < nfsrv_layouthashsize; i++) {
7106 lhyp = &nfslayouthash[i];
7107 NFSLOCKLAYOUT(lhyp);
7108 TAILQ_FOREACH_SAFE(lyp, &lhyp->list, lay_list, nlyp) {
7109 if (lyp->lay_type == NFSLAYOUT_FLEXFILE &&
7110 lyp->lay_mirrorcnt > 1) {
7111 NFSD_DEBUG(4, "possible match\n");
7114 for (j = 0; j < lyp->lay_mirrorcnt; j++) {
7116 if (NFSBCMP(devid, tl, NFSX_V4DEVICEID)
7119 NFSD_DEBUG(4, "fnd one\n");
7120 TAILQ_REMOVE(&lhyp->list, lyp,
7122 TAILQ_INSERT_HEAD(&loclyp, lyp,
7126 tl += (NFSX_V4DEVICEID / NFSX_UNSIGNED +
7127 NFSM_RNDUP(NFSX_V4PNFSFH) /
7128 NFSX_UNSIGNED + 11 * NFSX_UNSIGNED);
7132 NFSUNLOCKLAYOUT(lhyp);
7135 /* Now, try to do a Layout recall for each one found. */
7136 TAILQ_FOREACH_SAFE(lyp, &loclyp, lay_list, nlyp) {
7137 NFSD_DEBUG(4, "do layout recall\n");
7139 * The layout stateid.seqid needs to be incremented
7140 * before doing a LAYOUT_RECALL callback.
7142 if (++lyp->lay_stateid.seqid == 0)
7143 lyp->lay_stateid.seqid = 1;
7144 nfsrv_recalllayout(lyp->lay_clientid, &lyp->lay_stateid,
7145 &lyp->lay_fh, lyp, 1, lyp->lay_type, p);
7146 nfsrv_freelayout(&loclyp, lyp);
7151 * Do a recall callback to the client for this layout.
7154 nfsrv_recalllayout(nfsquad_t clid, nfsv4stateid_t *stateidp, fhandle_t *fhp,
7155 struct nfslayout *lyp, int changed, int laytype, NFSPROC_T *p)
7157 struct nfsclient *clp;
7160 NFSD_DEBUG(4, "nfsrv_recalllayout\n");
7161 error = nfsrv_getclient(clid, 0, &clp, NULL, (nfsquad_t)((u_quad_t)0),
7163 NFSD_DEBUG(4, "aft nfsrv_getclient=%d\n", error);
7165 printf("nfsrv_recalllayout: getclient err=%d\n", error);
7168 if ((clp->lc_flags & LCL_NFSV41) != 0) {
7169 error = nfsrv_docallback(clp, NFSV4OP_CBLAYOUTRECALL,
7170 stateidp, changed, fhp, NULL, NULL, laytype, p);
7171 /* If lyp != NULL, handle an error return here. */
7172 if (error != 0 && lyp != NULL) {
7175 * Mark it returned, since no layout recall
7177 * All errors seem to be non-recoverable, although
7178 * NFSERR_NOMATCHLAYOUT is a normal event.
7180 if ((lyp->lay_flags & NFSLAY_RECALL) != 0) {
7181 lyp->lay_flags |= NFSLAY_RETURNED;
7185 if (error != NFSERR_NOMATCHLAYOUT)
7186 printf("nfsrv_recalllayout: err=%d\n", error);
7189 printf("nfsrv_recalllayout: clp not NFSv4.1\n");
7194 * Find a layout to recall when we exceed our high water mark.
7197 nfsrv_recalloldlayout(NFSPROC_T *p)
7199 struct nfslayouthash *lhyp;
7200 struct nfslayout *lyp;
7202 nfsv4stateid_t stateid;
7204 int error, laytype = 0, ret;
7206 lhyp = &nfslayouthash[arc4random() % nfsrv_layouthashsize];
7207 NFSLOCKLAYOUT(lhyp);
7208 TAILQ_FOREACH_REVERSE(lyp, &lhyp->list, nfslayouthead, lay_list) {
7209 if ((lyp->lay_flags & NFSLAY_CALLB) == 0) {
7210 lyp->lay_flags |= NFSLAY_CALLB;
7212 * The layout stateid.seqid needs to be incremented
7213 * before doing a LAYOUT_RECALL callback.
7215 if (++lyp->lay_stateid.seqid == 0)
7216 lyp->lay_stateid.seqid = 1;
7217 clientid = lyp->lay_clientid;
7218 stateid = lyp->lay_stateid;
7219 NFSBCOPY(&lyp->lay_fh, &fh, sizeof(fh));
7220 laytype = lyp->lay_type;
7224 NFSUNLOCKLAYOUT(lhyp);
7226 error = nfsrv_recalllayout(clientid, &stateid, &fh, NULL, 0,
7228 if (error != 0 && error != NFSERR_NOMATCHLAYOUT)
7229 NFSD_DEBUG(4, "recallold=%d\n", error);
7231 NFSLOCKLAYOUT(lhyp);
7233 * Since the hash list was unlocked, we need to
7236 ret = nfsrv_findlayout(&clientid, &fh, laytype, p,
7239 (lyp->lay_flags & NFSLAY_CALLB) != 0 &&
7240 lyp->lay_stateid.other[0] == stateid.other[0] &&
7241 lyp->lay_stateid.other[1] == stateid.other[1] &&
7242 lyp->lay_stateid.other[2] == stateid.other[2]) {
7244 * The client no longer knows this layout, so
7245 * it can be free'd now.
7247 if (error == NFSERR_NOMATCHLAYOUT)
7248 nfsrv_freelayout(&lhyp->list, lyp);
7251 * Leave it to be tried later by
7252 * clearing NFSLAY_CALLB and moving
7253 * it to the head of the list, so it
7254 * won't be tried again for a while.
7256 lyp->lay_flags &= ~NFSLAY_CALLB;
7257 TAILQ_REMOVE(&lhyp->list, lyp,
7259 TAILQ_INSERT_HEAD(&lhyp->list, lyp,
7263 NFSUNLOCKLAYOUT(lhyp);
7269 * Try and return layout(s).
7272 nfsrv_layoutreturn(struct nfsrv_descript *nd, vnode_t vp,
7273 int layouttype, int iomode, uint64_t offset, uint64_t len, int reclaim,
7274 int kind, nfsv4stateid_t *stateidp, int maxcnt, uint32_t *layp, int *fndp,
7275 struct ucred *cred, NFSPROC_T *p)
7278 struct nfslayouthash *lhyp;
7279 struct nfslayout *lyp;
7284 if (kind == NFSV4LAYOUTRET_FILE) {
7285 error = nfsvno_getfh(vp, &fh, p);
7287 error = nfsrv_updatemdsattr(vp, &na, p);
7289 printf("nfsrv_layoutreturn: updatemdsattr"
7290 " failed=%d\n", error);
7293 if (reclaim == newnfs_true) {
7294 error = nfsrv_checkgrace(NULL, NULL,
7296 if (error != NFSERR_NOGRACE)
7300 lhyp = NFSLAYOUTHASH(&fh);
7302 NFSLOCKLAYOUT(lhyp);
7303 error = nfsrv_findlayout(&nd->nd_clientid, &fh,
7304 layouttype, p, &lyp);
7305 NFSD_DEBUG(4, "layoutret findlay=%d\n", error);
7307 stateidp->other[0] == lyp->lay_stateid.other[0] &&
7308 stateidp->other[1] == lyp->lay_stateid.other[1] &&
7309 stateidp->other[2] == lyp->lay_stateid.other[2]) {
7310 NFSD_DEBUG(4, "nfsrv_layoutreturn: stateid %d"
7311 " %x %x %x laystateid %d %x %x %x"
7312 " off=%ju len=%ju flgs=0x%x\n",
7313 stateidp->seqid, stateidp->other[0],
7314 stateidp->other[1], stateidp->other[2],
7315 lyp->lay_stateid.seqid,
7316 lyp->lay_stateid.other[0],
7317 lyp->lay_stateid.other[1],
7318 lyp->lay_stateid.other[2],
7319 (uintmax_t)offset, (uintmax_t)len,
7321 if (++lyp->lay_stateid.seqid == 0)
7322 lyp->lay_stateid.seqid = 1;
7323 stateidp->seqid = lyp->lay_stateid.seqid;
7324 if (offset == 0 && len == UINT64_MAX) {
7325 if ((iomode & NFSLAYOUTIOMODE_READ) !=
7327 lyp->lay_flags &= ~NFSLAY_READ;
7328 if ((iomode & NFSLAYOUTIOMODE_RW) != 0)
7329 lyp->lay_flags &= ~NFSLAY_RW;
7330 if ((lyp->lay_flags & (NFSLAY_READ |
7332 nfsrv_freelayout(&lhyp->list,
7339 NFSUNLOCKLAYOUT(lhyp);
7340 /* Search the nfsrv_recalllist for a match. */
7341 TAILQ_FOREACH(lyp, &nfsrv_recalllisthead, lay_list) {
7342 if (NFSBCMP(&lyp->lay_fh, &fh,
7344 lyp->lay_clientid.qval ==
7345 nd->nd_clientid.qval &&
7346 stateidp->other[0] ==
7347 lyp->lay_stateid.other[0] &&
7348 stateidp->other[1] ==
7349 lyp->lay_stateid.other[1] &&
7350 stateidp->other[2] ==
7351 lyp->lay_stateid.other[2]) {
7352 lyp->lay_flags |= NFSLAY_RETURNED;
7359 if (layouttype == NFSLAYOUT_FLEXFILE && layp != NULL)
7360 nfsrv_flexlayouterr(nd, layp, maxcnt, p);
7361 } else if (kind == NFSV4LAYOUTRET_FSID)
7362 nfsrv_freelayouts(&nd->nd_clientid,
7363 &vp->v_mount->mnt_stat.f_fsid, layouttype, iomode);
7364 else if (kind == NFSV4LAYOUTRET_ALL)
7365 nfsrv_freelayouts(&nd->nd_clientid, NULL, layouttype, iomode);
7367 error = NFSERR_INVAL;
7374 * Look for an existing layout.
7377 nfsrv_findlayout(nfsquad_t *clientidp, fhandle_t *fhp, int laytype,
7378 NFSPROC_T *p, struct nfslayout **lypp)
7380 struct nfslayouthash *lhyp;
7381 struct nfslayout *lyp;
7386 lhyp = NFSLAYOUTHASH(fhp);
7387 TAILQ_FOREACH(lyp, &lhyp->list, lay_list) {
7388 if (NFSBCMP(&lyp->lay_fh, fhp, sizeof(*fhp)) == 0 &&
7389 lyp->lay_clientid.qval == clientidp->qval &&
7390 lyp->lay_type == laytype)
7401 * Add the new layout, as required.
7404 nfsrv_addlayout(struct nfsrv_descript *nd, struct nfslayout **lypp,
7405 nfsv4stateid_t *stateidp, char *layp, int *layoutlenp, NFSPROC_T *p)
7407 struct nfsclient *clp;
7408 struct nfslayouthash *lhyp;
7409 struct nfslayout *lyp, *nlyp;
7413 KASSERT((nd->nd_flag & ND_IMPLIEDCLID) != 0,
7414 ("nfsrv_layoutget: no nd_clientid\n"));
7418 error = nfsrv_getclient((nfsquad_t)((u_quad_t)0), CLOPS_RENEW, &clp,
7419 NULL, (nfsquad_t)((u_quad_t)0), 0, nd, p);
7424 lyp->lay_stateid.seqid = stateidp->seqid = 1;
7425 lyp->lay_stateid.other[0] = stateidp->other[0] =
7426 clp->lc_clientid.lval[0];
7427 lyp->lay_stateid.other[1] = stateidp->other[1] =
7428 clp->lc_clientid.lval[1];
7429 lyp->lay_stateid.other[2] = stateidp->other[2] =
7430 nfsrv_nextstateindex(clp);
7433 lhyp = NFSLAYOUTHASH(fhp);
7434 NFSLOCKLAYOUT(lhyp);
7435 TAILQ_FOREACH(nlyp, &lhyp->list, lay_list) {
7436 if (NFSBCMP(&nlyp->lay_fh, fhp, sizeof(*fhp)) == 0 &&
7437 nlyp->lay_clientid.qval == nd->nd_clientid.qval)
7441 /* A layout already exists, so use it. */
7442 nlyp->lay_flags |= (lyp->lay_flags & (NFSLAY_READ | NFSLAY_RW));
7443 NFSBCOPY(nlyp->lay_xdr, layp, nlyp->lay_layoutlen);
7444 *layoutlenp = nlyp->lay_layoutlen;
7445 if (++nlyp->lay_stateid.seqid == 0)
7446 nlyp->lay_stateid.seqid = 1;
7447 stateidp->seqid = nlyp->lay_stateid.seqid;
7448 stateidp->other[0] = nlyp->lay_stateid.other[0];
7449 stateidp->other[1] = nlyp->lay_stateid.other[1];
7450 stateidp->other[2] = nlyp->lay_stateid.other[2];
7451 NFSUNLOCKLAYOUT(lhyp);
7455 /* Insert the new layout in the lists. */
7457 atomic_add_int(&nfsrv_layoutcnt, 1);
7458 NFSD_VNET(nfsstatsv1_p)->srvlayouts++;
7459 NFSBCOPY(lyp->lay_xdr, layp, lyp->lay_layoutlen);
7460 *layoutlenp = lyp->lay_layoutlen;
7461 TAILQ_INSERT_HEAD(&lhyp->list, lyp, lay_list);
7462 NFSUNLOCKLAYOUT(lhyp);
7467 * Get the devinfo for a deviceid.
7470 nfsrv_getdevinfo(char *devid, int layouttype, uint32_t *maxcnt,
7471 uint32_t *notify, int *devaddrlen, char **devaddr)
7473 struct nfsdevice *ds;
7475 if ((layouttype != NFSLAYOUT_NFSV4_1_FILES && layouttype !=
7476 NFSLAYOUT_FLEXFILE) ||
7477 (nfsrv_maxpnfsmirror > 1 && layouttype == NFSLAYOUT_NFSV4_1_FILES))
7478 return (NFSERR_UNKNLAYOUTTYPE);
7481 * Now, search for the device id. Note that the structures won't go
7482 * away, but the order changes in the list. As such, the lock only
7483 * needs to be held during the search through the list.
7486 TAILQ_FOREACH(ds, &nfsrv_devidhead, nfsdev_list) {
7487 if (NFSBCMP(devid, ds->nfsdev_deviceid, NFSX_V4DEVICEID) == 0 &&
7488 ds->nfsdev_nmp != NULL)
7493 return (NFSERR_NOENT);
7495 /* If the correct nfsdev_XXXXaddrlen is > 0, we have the device info. */
7497 if (layouttype == NFSLAYOUT_NFSV4_1_FILES) {
7498 *devaddrlen = ds->nfsdev_fileaddrlen;
7499 *devaddr = ds->nfsdev_fileaddr;
7500 } else if (layouttype == NFSLAYOUT_FLEXFILE) {
7501 *devaddrlen = ds->nfsdev_flexaddrlen;
7502 *devaddr = ds->nfsdev_flexaddr;
7504 if (*devaddrlen == 0)
7505 return (NFSERR_UNKNLAYOUTTYPE);
7508 * The XDR overhead is 3 unsigned values: layout_type,
7509 * length_of_address and notify bitmap.
7510 * If the notify array is changed to not all zeros, the
7511 * count of unsigned values must be increased.
7513 if (*maxcnt > 0 && *maxcnt < NFSM_RNDUP(*devaddrlen) +
7514 3 * NFSX_UNSIGNED) {
7515 *maxcnt = NFSM_RNDUP(*devaddrlen) + 3 * NFSX_UNSIGNED;
7516 return (NFSERR_TOOSMALL);
7522 * Free a list of layout state structures.
7525 nfsrv_freelayoutlist(nfsquad_t clientid)
7527 struct nfslayouthash *lhyp;
7528 struct nfslayout *lyp, *nlyp;
7531 for (i = 0; i < nfsrv_layouthashsize; i++) {
7532 lhyp = &nfslayouthash[i];
7533 NFSLOCKLAYOUT(lhyp);
7534 TAILQ_FOREACH_SAFE(lyp, &lhyp->list, lay_list, nlyp) {
7535 if (lyp->lay_clientid.qval == clientid.qval)
7536 nfsrv_freelayout(&lhyp->list, lyp);
7538 NFSUNLOCKLAYOUT(lhyp);
7546 nfsrv_freelayout(struct nfslayouthead *lhp, struct nfslayout *lyp)
7549 NFSD_DEBUG(4, "Freelayout=%p\n", lyp);
7550 atomic_add_int(&nfsrv_layoutcnt, -1);
7551 NFSD_VNET(nfsstatsv1_p)->srvlayouts--;
7552 TAILQ_REMOVE(lhp, lyp, lay_list);
7553 free(lyp, M_NFSDSTATE);
7557 * Free up a device id.
7560 nfsrv_freeonedevid(struct nfsdevice *ds)
7564 atomic_add_int(&nfsrv_devidcnt, -1);
7565 vrele(ds->nfsdev_dvp);
7566 for (i = 0; i < nfsrv_dsdirsize; i++)
7567 if (ds->nfsdev_dsdir[i] != NULL)
7568 vrele(ds->nfsdev_dsdir[i]);
7569 free(ds->nfsdev_fileaddr, M_NFSDSTATE);
7570 free(ds->nfsdev_flexaddr, M_NFSDSTATE);
7571 free(ds->nfsdev_host, M_NFSDSTATE);
7572 free(ds, M_NFSDSTATE);
7576 * Free up a device id and its mirrors.
7579 nfsrv_freedevid(struct nfsdevice *ds)
7582 TAILQ_REMOVE(&nfsrv_devidhead, ds, nfsdev_list);
7583 nfsrv_freeonedevid(ds);
7587 * Free all layouts and device ids.
7588 * Done when the nfsd threads are shut down since there may be a new
7589 * modified device id list created when the nfsd is restarted.
7592 nfsrv_freealllayoutsanddevids(void)
7594 struct nfsdontlist *mrp, *nmrp;
7595 struct nfslayout *lyp, *nlyp;
7597 /* Get rid of the deviceid structures. */
7598 nfsrv_freealldevids();
7599 TAILQ_INIT(&nfsrv_devidhead);
7602 /* Get rid of all layouts. */
7603 nfsrv_freealllayouts();
7605 /* Get rid of any nfsdontlist entries. */
7606 LIST_FOREACH_SAFE(mrp, &nfsrv_dontlisthead, nfsmr_list, nmrp)
7607 free(mrp, M_NFSDSTATE);
7608 LIST_INIT(&nfsrv_dontlisthead);
7609 nfsrv_dontlistlen = 0;
7611 /* Free layouts in the recall list. */
7612 TAILQ_FOREACH_SAFE(lyp, &nfsrv_recalllisthead, lay_list, nlyp)
7613 nfsrv_freelayout(&nfsrv_recalllisthead, lyp);
7614 TAILQ_INIT(&nfsrv_recalllisthead);
7618 * Free layouts that match the arguments.
7621 nfsrv_freelayouts(nfsquad_t *clid, fsid_t *fs, int laytype, int iomode)
7623 struct nfslayouthash *lhyp;
7624 struct nfslayout *lyp, *nlyp;
7627 for (i = 0; i < nfsrv_layouthashsize; i++) {
7628 lhyp = &nfslayouthash[i];
7629 NFSLOCKLAYOUT(lhyp);
7630 TAILQ_FOREACH_SAFE(lyp, &lhyp->list, lay_list, nlyp) {
7631 if (clid->qval != lyp->lay_clientid.qval)
7633 if (fs != NULL && fsidcmp(fs, &lyp->lay_fsid) != 0)
7635 if (laytype != lyp->lay_type)
7637 if ((iomode & NFSLAYOUTIOMODE_READ) != 0)
7638 lyp->lay_flags &= ~NFSLAY_READ;
7639 if ((iomode & NFSLAYOUTIOMODE_RW) != 0)
7640 lyp->lay_flags &= ~NFSLAY_RW;
7641 if ((lyp->lay_flags & (NFSLAY_READ | NFSLAY_RW)) == 0)
7642 nfsrv_freelayout(&lhyp->list, lyp);
7644 NFSUNLOCKLAYOUT(lhyp);
7649 * Free all layouts for the argument file.
7652 nfsrv_freefilelayouts(fhandle_t *fhp)
7654 struct nfslayouthash *lhyp;
7655 struct nfslayout *lyp, *nlyp;
7657 lhyp = NFSLAYOUTHASH(fhp);
7658 NFSLOCKLAYOUT(lhyp);
7659 TAILQ_FOREACH_SAFE(lyp, &lhyp->list, lay_list, nlyp) {
7660 if (NFSBCMP(&lyp->lay_fh, fhp, sizeof(*fhp)) == 0)
7661 nfsrv_freelayout(&lhyp->list, lyp);
7663 NFSUNLOCKLAYOUT(lhyp);
7670 nfsrv_freealllayouts(void)
7672 struct nfslayouthash *lhyp;
7673 struct nfslayout *lyp, *nlyp;
7676 for (i = 0; i < nfsrv_layouthashsize; i++) {
7677 lhyp = &nfslayouthash[i];
7678 NFSLOCKLAYOUT(lhyp);
7679 TAILQ_FOREACH_SAFE(lyp, &lhyp->list, lay_list, nlyp)
7680 nfsrv_freelayout(&lhyp->list, lyp);
7681 NFSUNLOCKLAYOUT(lhyp);
7686 * Look up the mount path for the DS server.
7689 nfsrv_setdsserver(char *dspathp, char *mdspathp, NFSPROC_T *p,
7690 struct nfsdevice **dsp)
7692 struct nameidata nd;
7693 struct nfsdevice *ds;
7699 NFSD_DEBUG(4, "setdssrv path=%s\n", dspathp);
7701 if (jailed(p->td_ucred)) {
7702 printf("A pNFS nfsd cannot run in a jail\n");
7705 NDINIT(&nd, LOOKUP, FOLLOW | LOCKSHARED | LOCKLEAF, UIO_SYSSPACE,
7708 NFSD_DEBUG(4, "lookup=%d\n", error);
7711 if (nd.ni_vp->v_type != VDIR) {
7713 NFSD_DEBUG(4, "dspath not dir\n");
7716 if (strcmp(nd.ni_vp->v_mount->mnt_vfc->vfc_name, "nfs") != 0) {
7718 NFSD_DEBUG(4, "dspath not an NFS mount\n");
7723 * Allocate a DS server structure with the NFS mounted directory
7724 * vnode reference counted, so that a non-forced dismount will
7726 * This structure is always linked into the list, even if an error
7727 * is being returned. The caller will free the entire list upon
7730 *dsp = ds = malloc(sizeof(*ds) + nfsrv_dsdirsize * sizeof(vnode_t),
7731 M_NFSDSTATE, M_WAITOK | M_ZERO);
7732 ds->nfsdev_dvp = nd.ni_vp;
7733 ds->nfsdev_nmp = VFSTONFS(nd.ni_vp->v_mount);
7734 NFSVOPUNLOCK(nd.ni_vp);
7736 dsdirsize = strlen(dspathp) + 16;
7737 dsdirpath = malloc(dsdirsize, M_TEMP, M_WAITOK);
7738 /* Now, create the DS directory structures. */
7739 for (i = 0; i < nfsrv_dsdirsize; i++) {
7740 snprintf(dsdirpath, dsdirsize, "%s/ds%d", dspathp, i);
7741 NDINIT(&nd, LOOKUP, FOLLOW | LOCKSHARED | LOCKLEAF,
7742 UIO_SYSSPACE, dsdirpath);
7744 NFSD_DEBUG(4, "dsdirpath=%s lookup=%d\n", dsdirpath, error);
7747 if (nd.ni_vp->v_type != VDIR) {
7750 NFSD_DEBUG(4, "dsdirpath not a VDIR\n");
7753 if (strcmp(nd.ni_vp->v_mount->mnt_vfc->vfc_name, "nfs") != 0) {
7756 NFSD_DEBUG(4, "dsdirpath not an NFS mount\n");
7759 ds->nfsdev_dsdir[i] = nd.ni_vp;
7760 NFSVOPUNLOCK(nd.ni_vp);
7762 free(dsdirpath, M_TEMP);
7764 if (strlen(mdspathp) > 0) {
7766 * This DS stores file for a specific MDS exported file
7769 NDINIT(&nd, LOOKUP, FOLLOW | LOCKSHARED | LOCKLEAF,
7770 UIO_SYSSPACE, mdspathp);
7772 NFSD_DEBUG(4, "mds lookup=%d\n", error);
7775 if (nd.ni_vp->v_type != VDIR) {
7778 NFSD_DEBUG(4, "mdspath not dir\n");
7781 mp = nd.ni_vp->v_mount;
7782 if ((mp->mnt_flag & MNT_EXPORTED) == 0) {
7785 NFSD_DEBUG(4, "mdspath not an exported fs\n");
7788 ds->nfsdev_mdsfsid = mp->mnt_stat.f_fsid;
7789 ds->nfsdev_mdsisset = 1;
7794 TAILQ_INSERT_TAIL(&nfsrv_devidhead, ds, nfsdev_list);
7795 atomic_add_int(&nfsrv_devidcnt, 1);
7800 * Look up the mount path for the DS server and delete it.
7803 nfsrv_deldsserver(int op, char *dspathp, NFSPROC_T *p)
7806 struct nfsmount *nmp;
7807 struct nfsdevice *ds;
7810 NFSD_DEBUG(4, "deldssrv path=%s\n", dspathp);
7812 * Search for the path in the mount list. Avoid looking the path
7813 * up, since this mount point may be hung, with associated locked
7815 * Set NFSMNTP_CANCELRPCS so that any forced dismount will be blocked
7816 * until this completes.
7817 * As noted in the man page, this should be done before any forced
7818 * dismount on the mount point, but at least the handshake on
7819 * NFSMNTP_CANCELRPCS should make it safe.
7824 mtx_lock(&mountlist_mtx);
7825 TAILQ_FOREACH(mp, &mountlist, mnt_list) {
7826 if (strcmp(mp->mnt_stat.f_mntonname, dspathp) == 0 &&
7827 strcmp(mp->mnt_stat.f_fstypename, "nfs") == 0 &&
7828 mp->mnt_data != NULL) {
7831 if ((nmp->nm_privflag & (NFSMNTP_FORCEDISM |
7832 NFSMNTP_CANCELRPCS)) == 0) {
7833 nmp->nm_privflag |= NFSMNTP_CANCELRPCS;
7842 mtx_unlock(&mountlist_mtx);
7845 ds = nfsrv_deldsnmp(op, nmp, p);
7846 NFSD_DEBUG(4, "deldsnmp=%p\n", ds);
7848 nfsrv_killrpcs(nmp);
7849 NFSD_DEBUG(4, "aft killrpcs\n");
7853 nmp->nm_privflag &= ~NFSMNTP_CANCELRPCS;
7862 * Search for and remove a DS entry which matches the "nmp" argument.
7863 * The nfsdevice structure pointer is returned so that the caller can
7864 * free it via nfsrv_freeonedevid().
7865 * For the forced case, do not try to do LayoutRecalls, since the server
7866 * must be shut down now anyhow.
7869 nfsrv_deldsnmp(int op, struct nfsmount *nmp, NFSPROC_T *p)
7871 struct nfsdevice *fndds;
7873 NFSD_DEBUG(4, "deldsdvp\n");
7875 if (op == PNFSDOP_FORCEDELDS)
7876 fndds = nfsv4_findmirror(nmp);
7878 fndds = nfsrv_findmirroredds(nmp);
7880 nfsrv_deleteds(fndds);
7882 if (fndds != NULL) {
7883 if (op != PNFSDOP_FORCEDELDS)
7884 nfsrv_flexmirrordel(fndds->nfsdev_deviceid, p);
7885 printf("pNFS server: mirror %s failed\n", fndds->nfsdev_host);
7891 * Similar to nfsrv_deldsnmp(), except that the DS is indicated by deviceid.
7892 * This function also calls nfsrv_killrpcs() to unblock RPCs on the mount
7894 * Also, returns an error instead of the nfsdevice found.
7897 nfsrv_delds(char *devid, NFSPROC_T *p)
7899 struct nfsdevice *ds, *fndds;
7900 struct nfsmount *nmp;
7903 NFSD_DEBUG(4, "delds\n");
7905 * Search the DS server list for a match with devid.
7906 * Remove the DS entry if found and there is a mirror.
7912 TAILQ_FOREACH(ds, &nfsrv_devidhead, nfsdev_list) {
7913 if (NFSBCMP(ds->nfsdev_deviceid, devid, NFSX_V4DEVICEID) == 0 &&
7914 ds->nfsdev_nmp != NULL) {
7915 NFSD_DEBUG(4, "fnd main ds\n");
7920 if (fndds == NULL) {
7924 if (fndds->nfsdev_mdsisset == 0 && nfsrv_faildscnt > 0)
7926 else if (fndds->nfsdev_mdsisset != 0) {
7927 /* For the fsid is set case, search for a mirror. */
7928 TAILQ_FOREACH(ds, &nfsrv_devidhead, nfsdev_list) {
7929 if (ds != fndds && ds->nfsdev_nmp != NULL &&
7930 ds->nfsdev_mdsisset != 0 &&
7931 fsidcmp(&ds->nfsdev_mdsfsid,
7932 &fndds->nfsdev_mdsfsid) == 0) {
7938 if (fndmirror != 0) {
7939 nmp = fndds->nfsdev_nmp;
7941 if ((nmp->nm_privflag & (NFSMNTP_FORCEDISM |
7942 NFSMNTP_CANCELRPCS)) == 0) {
7943 nmp->nm_privflag |= NFSMNTP_CANCELRPCS;
7945 nfsrv_deleteds(fndds);
7953 nfsrv_flexmirrordel(fndds->nfsdev_deviceid, p);
7954 printf("pNFS server: mirror %s failed\n", fndds->nfsdev_host);
7955 nfsrv_killrpcs(nmp);
7957 nmp->nm_privflag &= ~NFSMNTP_CANCELRPCS;
7966 * Mark a DS as disabled by setting nfsdev_nmp = NULL.
7969 nfsrv_deleteds(struct nfsdevice *fndds)
7972 NFSD_DEBUG(4, "deleteds: deleting a mirror\n");
7973 fndds->nfsdev_nmp = NULL;
7974 if (fndds->nfsdev_mdsisset == 0)
7979 * Fill in the addr structures for the File and Flex File layouts.
7982 nfsrv_allocdevid(struct nfsdevice *ds, char *addr, char *dnshost)
7987 static uint64_t new_devid = 0;
7989 if (strchr(addr, ':') != NULL)
7994 /* Fill in the device id. */
7995 NFSBCOPY(&nfsdev_time, ds->nfsdev_deviceid, sizeof(nfsdev_time));
7997 NFSBCOPY(&new_devid, &ds->nfsdev_deviceid[sizeof(nfsdev_time)],
8001 * Fill in the file addr (actually the nfsv4_file_layout_ds_addr4
8002 * as defined in RFC5661) in XDR.
8004 addrlen = NFSM_RNDUP(strlen(addr)) + NFSM_RNDUP(strlen(netprot)) +
8006 NFSD_DEBUG(4, "hn=%s addr=%s netprot=%s\n", dnshost, addr, netprot);
8007 ds->nfsdev_fileaddrlen = addrlen;
8008 tl = malloc(addrlen, M_NFSDSTATE, M_WAITOK | M_ZERO);
8009 ds->nfsdev_fileaddr = (char *)tl;
8010 *tl++ = txdr_unsigned(1); /* One stripe with index 0. */
8012 *tl++ = txdr_unsigned(1); /* One multipath list */
8013 *tl++ = txdr_unsigned(1); /* with one entry in it. */
8014 /* The netaddr for this one entry. */
8015 *tl++ = txdr_unsigned(strlen(netprot));
8016 NFSBCOPY(netprot, tl, strlen(netprot));
8017 tl += (NFSM_RNDUP(strlen(netprot)) / NFSX_UNSIGNED);
8018 *tl++ = txdr_unsigned(strlen(addr));
8019 NFSBCOPY(addr, tl, strlen(addr));
8022 * Fill in the flex file addr (actually the ff_device_addr4
8023 * as defined for Flexible File Layout) in XDR.
8025 addrlen = NFSM_RNDUP(strlen(addr)) + NFSM_RNDUP(strlen(netprot)) +
8027 ds->nfsdev_flexaddrlen = addrlen;
8028 tl = malloc(addrlen, M_NFSDSTATE, M_WAITOK | M_ZERO);
8029 ds->nfsdev_flexaddr = (char *)tl;
8030 *tl++ = txdr_unsigned(1); /* One multipath entry. */
8031 /* The netaddr for this one entry. */
8032 *tl++ = txdr_unsigned(strlen(netprot));
8033 NFSBCOPY(netprot, tl, strlen(netprot));
8034 tl += (NFSM_RNDUP(strlen(netprot)) / NFSX_UNSIGNED);
8035 *tl++ = txdr_unsigned(strlen(addr));
8036 NFSBCOPY(addr, tl, strlen(addr));
8037 tl += (NFSM_RNDUP(strlen(addr)) / NFSX_UNSIGNED);
8038 *tl++ = txdr_unsigned(2); /* Two NFS Versions. */
8039 *tl++ = txdr_unsigned(NFS_VER4); /* NFSv4. */
8040 *tl++ = txdr_unsigned(NFSV42_MINORVERSION); /* Minor version 2. */
8041 *tl++ = txdr_unsigned(nfs_srvmaxio); /* DS max rsize. */
8042 *tl++ = txdr_unsigned(nfs_srvmaxio); /* DS max wsize. */
8043 *tl++ = newnfs_true; /* Tightly coupled. */
8044 *tl++ = txdr_unsigned(NFS_VER4); /* NFSv4. */
8045 *tl++ = txdr_unsigned(NFSV41_MINORVERSION); /* Minor version 1. */
8046 *tl++ = txdr_unsigned(nfs_srvmaxio); /* DS max rsize. */
8047 *tl++ = txdr_unsigned(nfs_srvmaxio); /* DS max wsize. */
8048 *tl = newnfs_true; /* Tightly coupled. */
8050 ds->nfsdev_hostnamelen = strlen(dnshost);
8051 ds->nfsdev_host = malloc(ds->nfsdev_hostnamelen + 1, M_NFSDSTATE,
8053 NFSBCOPY(dnshost, ds->nfsdev_host, ds->nfsdev_hostnamelen + 1);
8057 * Create the device id list.
8058 * Return 0 if the nfsd threads are to run and ENXIO if the "-p" argument
8062 nfsrv_createdevids(struct nfsd_nfsd_args *args, NFSPROC_T *p)
8064 struct nfsdevice *ds;
8065 char *addrp, *dnshostp, *dspathp, *mdspathp;
8069 dnshostp = args->dnshost;
8070 dspathp = args->dspath;
8071 mdspathp = args->mdspath;
8072 nfsrv_maxpnfsmirror = args->mirrorcnt;
8073 if (addrp == NULL || dnshostp == NULL || dspathp == NULL ||
8078 * Loop around for each nul-terminated string in args->addr,
8079 * args->dnshost, args->dnspath and args->mdspath.
8081 while (addrp < (args->addr + args->addrlen) &&
8082 dnshostp < (args->dnshost + args->dnshostlen) &&
8083 dspathp < (args->dspath + args->dspathlen) &&
8084 mdspathp < (args->mdspath + args->mdspathlen)) {
8085 error = nfsrv_setdsserver(dspathp, mdspathp, p, &ds);
8087 /* Free all DS servers. */
8088 nfsrv_freealldevids();
8092 nfsrv_allocdevid(ds, addrp, dnshostp);
8093 addrp += (strlen(addrp) + 1);
8094 dnshostp += (strlen(dnshostp) + 1);
8095 dspathp += (strlen(dspathp) + 1);
8096 mdspathp += (strlen(mdspathp) + 1);
8098 if (nfsrv_devidcnt < nfsrv_maxpnfsmirror) {
8099 /* Free all DS servers. */
8100 nfsrv_freealldevids();
8102 nfsrv_maxpnfsmirror = 1;
8105 /* We can fail at most one less DS than the mirror level. */
8106 nfsrv_faildscnt = nfsrv_maxpnfsmirror - 1;
8109 * Allocate the nfslayout hash table now, since this is a pNFS server.
8110 * Make it 1% of the high water mark and at least 100.
8112 if (nfslayouthash == NULL) {
8113 nfsrv_layouthashsize = nfsrv_layouthighwater / 100;
8114 if (nfsrv_layouthashsize < 100)
8115 nfsrv_layouthashsize = 100;
8116 nfslayouthash = mallocarray(nfsrv_layouthashsize,
8117 sizeof(struct nfslayouthash), M_NFSDSESSION, M_WAITOK |
8119 for (i = 0; i < nfsrv_layouthashsize; i++) {
8120 mtx_init(&nfslayouthash[i].mtx, "nfslm", NULL, MTX_DEF);
8121 TAILQ_INIT(&nfslayouthash[i].list);
8128 * Free all device ids.
8131 nfsrv_freealldevids(void)
8133 struct nfsdevice *ds, *nds;
8135 TAILQ_FOREACH_SAFE(ds, &nfsrv_devidhead, nfsdev_list, nds)
8136 nfsrv_freedevid(ds);
8140 * Check to see if there is a Read/Write Layout plus either:
8141 * - A Write Delegation
8143 * - An Open with Write_access.
8144 * Return 1 if this is the case and 0 otherwise.
8145 * This function is used by nfsrv_proxyds() to decide if doing a Proxy
8146 * Getattr RPC to the Data Server (DS) is necessary.
8148 #define NFSCLIDVECSIZE 6
8150 nfsrv_checkdsattr(vnode_t vp, NFSPROC_T *p)
8152 fhandle_t fh, *tfhp;
8153 struct nfsstate *stp;
8154 struct nfslayout *lyp;
8155 struct nfslayouthash *lhyp;
8156 struct nfslockhashhead *hp;
8157 struct nfslockfile *lfp;
8158 nfsquad_t clid[NFSCLIDVECSIZE];
8161 ret = nfsvno_getfh(vp, &fh, p);
8165 /* First check for a Read/Write Layout. */
8167 lhyp = NFSLAYOUTHASH(&fh);
8168 NFSLOCKLAYOUT(lhyp);
8169 TAILQ_FOREACH(lyp, &lhyp->list, lay_list) {
8170 if (NFSBCMP(&lyp->lay_fh, &fh, sizeof(fh)) == 0 &&
8171 ((lyp->lay_flags & NFSLAY_RW) != 0 ||
8172 ((lyp->lay_flags & NFSLAY_READ) != 0 &&
8173 nfsrv_pnfsatime != 0))) {
8174 if (clidcnt < NFSCLIDVECSIZE)
8175 clid[clidcnt].qval = lyp->lay_clientid.qval;
8179 NFSUNLOCKLAYOUT(lhyp);
8181 /* None found, so return 0. */
8185 /* Get the nfslockfile for this fh. */
8187 hp = NFSLOCKHASH(&fh);
8188 LIST_FOREACH(lfp, hp, lf_hash) {
8190 if (NFSVNO_CMPFH(&fh, tfhp))
8194 /* None found, so return 0. */
8199 /* Now, look for a Write delegation for this clientid. */
8200 LIST_FOREACH(stp, &lfp->lf_deleg, ls_file) {
8201 if ((stp->ls_flags & NFSLCK_DELEGWRITE) != 0 &&
8202 nfsrv_fndclid(clid, stp->ls_clp->lc_clientid, clidcnt) != 0)
8206 /* Found one, so return 1. */
8211 /* No Write delegation, so look for an Open with Write_access. */
8212 LIST_FOREACH(stp, &lfp->lf_open, ls_file) {
8213 KASSERT((stp->ls_flags & NFSLCK_OPEN) != 0,
8214 ("nfsrv_checkdsattr: Non-open in Open list\n"));
8215 if ((stp->ls_flags & NFSLCK_WRITEACCESS) != 0 &&
8216 nfsrv_fndclid(clid, stp->ls_clp->lc_clientid, clidcnt) != 0)
8226 * Look for a matching clientid in the vector. Return 1 if one might match.
8229 nfsrv_fndclid(nfsquad_t *clidvec, nfsquad_t clid, int clidcnt)
8233 /* If too many for the vector, return 1 since there might be a match. */
8234 if (clidcnt > NFSCLIDVECSIZE)
8237 for (i = 0; i < clidcnt; i++)
8238 if (clidvec[i].qval == clid.qval)
8244 * Check the don't list for "vp" and see if issuing an rw layout is allowed.
8245 * Return 1 if issuing an rw layout isn't allowed, 0 otherwise.
8248 nfsrv_dontlayout(fhandle_t *fhp)
8250 struct nfsdontlist *mrp;
8253 if (nfsrv_dontlistlen == 0)
8257 LIST_FOREACH(mrp, &nfsrv_dontlisthead, nfsmr_list) {
8258 if (NFSBCMP(fhp, &mrp->nfsmr_fh, sizeof(*fhp)) == 0 &&
8259 (mrp->nfsmr_flags & NFSMR_DONTLAYOUT) != 0) {
8264 NFSDDONTLISTUNLOCK();
8268 #define PNFSDS_COPYSIZ 65536
8270 * Create a new file on a DS and copy the contents of an extant DS file to it.
8271 * This can be used for recovery of a DS file onto a recovered DS.
8273 * - When called, the MDS file's vnode is locked, blocking LayoutGet operations.
8274 * - Disable issuing of read/write layouts for the file via the nfsdontlist,
8275 * so that they will be disabled after the MDS file's vnode is unlocked.
8276 * - Set up the nfsrv_recalllist so that recall of read/write layouts can
8278 * - Unlock the MDS file's vnode, so that the client(s) can perform proxied
8279 * writes, LayoutCommits and LayoutReturns for the file when completing the
8280 * LayoutReturn requested by the LayoutRecall callback.
8281 * - Issue a LayoutRecall callback for all read/write layouts and wait for
8282 * them to be returned. (If the LayoutRecall callback replies
8283 * NFSERR_NOMATCHLAYOUT, they are gone and no LayoutReturn is needed.)
8284 * - Exclusively lock the MDS file's vnode. This ensures that no proxied
8285 * writes are in progress or can occur during the DS file copy.
8286 * It also blocks Setattr operations.
8287 * - Create the file on the recovered mirror.
8288 * - Copy the file from the operational DS.
8289 * - Copy any ACL from the MDS file to the new DS file.
8290 * - Set the modify time of the new DS file to that of the MDS file.
8291 * - Update the extended attribute for the MDS file.
8292 * - Enable issuing of rw layouts by deleting the nfsdontlist entry.
8293 * - The caller will unlock the MDS file's vnode allowing operations
8294 * to continue normally, since it is now on the mirror again.
8297 nfsrv_copymr(vnode_t vp, vnode_t fvp, vnode_t dvp, struct nfsdevice *ds,
8298 struct pnfsdsfile *pf, struct pnfsdsfile *wpf, int mirrorcnt,
8299 struct ucred *cred, NFSPROC_T *p)
8301 struct nfsdontlist *mrp, *nmrp;
8302 struct nfslayouthash *lhyp;
8303 struct nfslayout *lyp, *nlyp;
8304 struct nfslayouthead thl;
8305 struct mount *mp, *tvmp;
8308 struct timespec mtime;
8314 int didprintf, ret, retacl, xfer;
8316 ASSERT_VOP_LOCKED(fvp, "nfsrv_copymr fvp");
8317 ASSERT_VOP_LOCKED(vp, "nfsrv_copymr vp");
8319 * Allocate a nfsdontlist entry and set the NFSMR_DONTLAYOUT flag
8320 * so that no more RW layouts will get issued.
8322 ret = nfsvno_getfh(vp, &fh, p);
8324 NFSD_DEBUG(4, "nfsrv_copymr: getfh=%d\n", ret);
8327 nmrp = malloc(sizeof(*nmrp), M_NFSDSTATE, M_WAITOK);
8328 nmrp->nfsmr_flags = NFSMR_DONTLAYOUT;
8329 NFSBCOPY(&fh, &nmrp->nfsmr_fh, sizeof(fh));
8331 LIST_FOREACH(mrp, &nfsrv_dontlisthead, nfsmr_list) {
8332 if (NFSBCMP(&fh, &mrp->nfsmr_fh, sizeof(fh)) == 0)
8336 LIST_INSERT_HEAD(&nfsrv_dontlisthead, nmrp, nfsmr_list);
8339 nfsrv_dontlistlen++;
8340 NFSD_DEBUG(4, "nfsrv_copymr: in dontlist\n");
8342 NFSDDONTLISTUNLOCK();
8343 free(nmrp, M_NFSDSTATE);
8344 NFSD_DEBUG(4, "nfsrv_copymr: dup dontlist\n");
8347 NFSDDONTLISTUNLOCK();
8350 * Search for all RW layouts for this file. Move them to the
8351 * recall list, so they can be recalled and their return noted.
8353 lhyp = NFSLAYOUTHASH(&fh);
8355 NFSLOCKLAYOUT(lhyp);
8356 TAILQ_FOREACH_SAFE(lyp, &lhyp->list, lay_list, nlyp) {
8357 if (NFSBCMP(&lyp->lay_fh, &fh, sizeof(fh)) == 0 &&
8358 (lyp->lay_flags & NFSLAY_RW) != 0) {
8359 TAILQ_REMOVE(&lhyp->list, lyp, lay_list);
8360 TAILQ_INSERT_HEAD(&nfsrv_recalllisthead, lyp, lay_list);
8361 lyp->lay_trycnt = 0;
8364 NFSUNLOCKLAYOUT(lhyp);
8371 /* Unlock the MDS vp, so that a LayoutReturn can be done on it. */
8373 /* Now, do a recall for all layouts not yet recalled. */
8376 TAILQ_FOREACH(lyp, &nfsrv_recalllisthead, lay_list) {
8377 if (NFSBCMP(&lyp->lay_fh, &fh, sizeof(fh)) == 0 &&
8378 (lyp->lay_flags & NFSLAY_RECALL) == 0) {
8379 lyp->lay_flags |= NFSLAY_RECALL;
8381 * The layout stateid.seqid needs to be incremented
8382 * before doing a LAYOUT_RECALL callback.
8384 if (++lyp->lay_stateid.seqid == 0)
8385 lyp->lay_stateid.seqid = 1;
8387 nfsrv_recalllayout(lyp->lay_clientid, &lyp->lay_stateid,
8388 &lyp->lay_fh, lyp, 0, lyp->lay_type, p);
8389 NFSD_DEBUG(4, "nfsrv_copymr: recalled layout\n");
8394 /* Now wait for them to be returned. */
8396 TAILQ_FOREACH(lyp, &nfsrv_recalllisthead, lay_list) {
8397 if (NFSBCMP(&lyp->lay_fh, &fh, sizeof(fh)) == 0) {
8398 if ((lyp->lay_flags & NFSLAY_RETURNED) != 0) {
8399 TAILQ_REMOVE(&nfsrv_recalllisthead, lyp,
8401 TAILQ_INSERT_HEAD(&thl, lyp, lay_list);
8403 "nfsrv_copymr: layout returned\n");
8406 ret = mtx_sleep(lyp, NFSDRECALLMUTEXPTR,
8407 PVFS | PCATCH, "nfsmrl", hz);
8408 NFSD_DEBUG(4, "nfsrv_copymr: aft sleep=%d\n",
8410 if (ret == EINTR || ret == ERESTART)
8412 if ((lyp->lay_flags & NFSLAY_RETURNED) == 0) {
8414 * Give up after 60sec and return
8415 * ENXIO, failing the copymr.
8416 * This layout will remain on the
8417 * recalllist. It can only be cleared
8418 * by restarting the nfsd.
8419 * This seems the safe way to handle
8420 * it, since it cannot be safely copied
8421 * with an outstanding RW layout.
8423 if (lyp->lay_trycnt >= 60) {
8427 if (didprintf == 0) {
8428 printf("nfsrv_copymr: layout "
8438 /* We can now get rid of the layouts that have been returned. */
8439 TAILQ_FOREACH_SAFE(lyp, &thl, lay_list, nlyp)
8440 nfsrv_freelayout(&thl, lyp);
8443 * Do the vn_start_write() calls here, before the MDS vnode is
8444 * locked and the tvp is created (locked) in the NFS file system
8446 * For tvmp, this probably isn't necessary, since it will be an
8447 * NFS mount and they are not suspendable at this time.
8450 ret = vn_start_write(vp, &mp, V_WAIT | V_PCATCH);
8452 tvmp = dvp->v_mount;
8453 ret = vn_start_write(NULL, &tvmp, V_WAIT | V_PCATCH);
8457 * LK_EXCLUSIVE lock the MDS vnode, so that any
8458 * proxied writes through the MDS will be blocked until we have
8459 * completed the copy and update of the extended attributes.
8460 * This will also ensure that any attributes and ACL will not be
8461 * changed until the copy is complete.
8463 NFSVOPLOCK(vp, LK_EXCLUSIVE | LK_RETRY);
8464 if (ret == 0 && VN_IS_DOOMED(vp)) {
8465 NFSD_DEBUG(4, "nfsrv_copymr: lk_exclusive doomed\n");
8469 /* Create the data file on the recovered DS. */
8471 ret = nfsrv_createdsfile(vp, &fh, pf, dvp, ds, cred, p, &tvp);
8473 /* Copy the DS file, if created successfully. */
8476 * Get any NFSv4 ACL on the MDS file, so that it can be set
8477 * on the new DS file.
8479 aclp = acl_alloc(M_WAITOK | M_ZERO);
8480 retacl = VOP_GETACL(vp, ACL_TYPE_NFS4, aclp, cred, p);
8481 if (retacl != 0 && retacl != ENOATTR)
8482 NFSD_DEBUG(1, "nfsrv_copymr: vop_getacl=%d\n", retacl);
8483 dat = malloc(PNFSDS_COPYSIZ, M_TEMP, M_WAITOK);
8484 /* Malloc a block of 0s used to check for holes. */
8485 if (nfsrv_zeropnfsdat == NULL)
8486 nfsrv_zeropnfsdat = malloc(PNFSDS_COPYSIZ, M_TEMP,
8489 ret = VOP_GETATTR(fvp, &va, cred);
8491 while (ret == 0 && aresid == 0) {
8492 ret = vn_rdwr(UIO_READ, fvp, dat, PNFSDS_COPYSIZ,
8493 rdpos, UIO_SYSSPACE, IO_NODELOCKED, cred, NULL,
8495 xfer = PNFSDS_COPYSIZ - aresid;
8496 if (ret == 0 && xfer > 0) {
8499 * Skip the write for holes, except for the
8502 if (xfer < PNFSDS_COPYSIZ || rdpos ==
8503 va.va_size || NFSBCMP(dat,
8504 nfsrv_zeropnfsdat, PNFSDS_COPYSIZ) != 0)
8505 ret = vn_rdwr(UIO_WRITE, tvp, dat, xfer,
8506 wrpos, UIO_SYSSPACE, IO_NODELOCKED,
8507 cred, NULL, NULL, p);
8513 /* If there is an ACL and the copy succeeded, set the ACL. */
8514 if (ret == 0 && retacl == 0) {
8515 ret = VOP_SETACL(tvp, ACL_TYPE_NFS4, aclp, cred, p);
8517 * Don't consider these as errors, since VOP_GETACL()
8518 * can return an ACL when they are not actually
8519 * supported. For example, for UFS, VOP_GETACL()
8520 * will return a trivial ACL based on the uid/gid/mode
8521 * when there is no ACL on the file.
8522 * This case should be recognized as a trivial ACL
8523 * by UFS's VOP_SETACL() and succeed, but...
8525 if (ret == ENOATTR || ret == EOPNOTSUPP || ret == EPERM)
8530 ret = VOP_FSYNC(tvp, MNT_WAIT, p);
8532 /* Set the DS data file's modify time that of the MDS file. */
8534 ret = VOP_GETATTR(vp, &va, cred);
8536 mtime = va.va_mtime;
8538 va.va_mtime = mtime;
8539 ret = VOP_SETATTR(tvp, &va, cred);
8547 vn_finished_write(tvmp);
8549 /* Update the extended attributes for the newly created DS file. */
8551 ret = vn_extattr_set(vp, IO_NODELOCKED,
8552 EXTATTR_NAMESPACE_SYSTEM, "pnfsd.dsfile",
8553 sizeof(*wpf) * mirrorcnt, (char *)wpf, p);
8555 vn_finished_write(mp);
8557 /* Get rid of the dontlist entry, so that Layouts can be issued. */
8559 LIST_REMOVE(mrp, nfsmr_list);
8560 NFSDDONTLISTUNLOCK();
8561 free(mrp, M_NFSDSTATE);
8566 * Create a data storage file on the recovered DS.
8569 nfsrv_createdsfile(vnode_t vp, fhandle_t *fhp, struct pnfsdsfile *pf,
8570 vnode_t dvp, struct nfsdevice *ds, struct ucred *cred, NFSPROC_T *p,
8573 struct vattr va, nva;
8576 /* Make data file name based on FH. */
8577 error = VOP_GETATTR(vp, &va, cred);
8579 /* Set the attributes for "vp" to Setattr the DS vp. */
8581 nva.va_uid = va.va_uid;
8582 nva.va_gid = va.va_gid;
8583 nva.va_mode = va.va_mode;
8587 va.va_mode = nva.va_mode;
8588 NFSD_DEBUG(4, "nfsrv_dscreatefile: dvp=%p pf=%p\n", dvp, pf);
8589 error = nfsrv_dscreate(dvp, &va, &nva, fhp, pf, NULL,
8590 pf->dsf_filename, cred, p, tvpp);
8596 * Look up the MDS file shared locked, and then get the extended attribute
8597 * to find the extant DS file to be copied to the new mirror.
8598 * If successful, *vpp is set to the MDS file's vp and *nvpp is
8599 * set to a DS data file for the MDS file, both exclusively locked.
8600 * The "buf" argument has the pnfsdsfile structure from the MDS file
8601 * in it and buflen is set to its length.
8604 nfsrv_mdscopymr(char *mdspathp, char *dspathp, char *curdspathp, char *buf,
8605 int *buflenp, char *fname, NFSPROC_T *p, struct vnode **vpp,
8606 struct vnode **nvpp, struct pnfsdsfile **pfp, struct nfsdevice **dsp,
8607 struct nfsdevice **fdsp)
8609 struct nameidata nd;
8610 struct vnode *vp, *curvp;
8611 struct pnfsdsfile *pf;
8612 struct nfsmount *nmp, *curnmp;
8613 int dsdir, error, mirrorcnt, ippos;
8620 if (dspathp == NULL && curdspathp != NULL)
8624 * Look up the MDS file shared locked. The lock will be upgraded
8625 * to an exclusive lock after any rw layouts have been returned.
8627 NFSD_DEBUG(4, "mdsopen path=%s\n", mdspathp);
8628 NDINIT(&nd, LOOKUP, FOLLOW | LOCKSHARED | LOCKLEAF, UIO_SYSSPACE,
8631 NFSD_DEBUG(4, "lookup=%d\n", error);
8634 if (nd.ni_vp->v_type != VREG) {
8636 NFSD_DEBUG(4, "mdspath not reg\n");
8641 if (curdspathp != NULL) {
8643 * Look up the current DS path and find the nfsdev structure for
8646 NFSD_DEBUG(4, "curmdsdev path=%s\n", curdspathp);
8647 NDINIT(&nd, LOOKUP, FOLLOW | LOCKSHARED | LOCKLEAF,
8648 UIO_SYSSPACE, curdspathp);
8650 NFSD_DEBUG(4, "ds lookup=%d\n", error);
8655 if (nd.ni_vp->v_type != VDIR) {
8658 NFSD_DEBUG(4, "curdspath not dir\n");
8661 if (strcmp(nd.ni_vp->v_mount->mnt_vfc->vfc_name, "nfs") != 0) {
8664 NFSD_DEBUG(4, "curdspath not an NFS mount\n");
8667 curnmp = VFSTONFS(nd.ni_vp->v_mount);
8669 /* Search the nfsdev list for a match. */
8671 *fdsp = nfsv4_findmirror(curnmp);
8675 if (curnmp == NULL) {
8678 NFSD_DEBUG(4, "mdscopymr: no current ds\n");
8684 if (dspathp != NULL) {
8685 /* Look up the nfsdev path and find the nfsdev structure. */
8686 NFSD_DEBUG(4, "mdsdev path=%s\n", dspathp);
8687 NDINIT(&nd, LOOKUP, FOLLOW | LOCKSHARED | LOCKLEAF,
8688 UIO_SYSSPACE, dspathp);
8690 NFSD_DEBUG(4, "ds lookup=%d\n", error);
8697 if (nd.ni_vp->v_type != VDIR || nd.ni_vp == curvp) {
8702 NFSD_DEBUG(4, "dspath not dir\n");
8703 if (nd.ni_vp == curvp)
8707 if (strcmp(nd.ni_vp->v_mount->mnt_vfc->vfc_name, "nfs") != 0) {
8712 NFSD_DEBUG(4, "dspath not an NFS mount\n");
8715 nmp = VFSTONFS(nd.ni_vp->v_mount);
8718 * Search the nfsdevice list for a match. If curnmp == NULL,
8719 * this is a recovery and there must be a mirror.
8723 *dsp = nfsrv_findmirroredds(nmp);
8725 *dsp = nfsv4_findmirror(nmp);
8732 NFSD_DEBUG(4, "mdscopymr: no ds\n");
8741 * Get a vp for an available DS data file using the extended
8742 * attribute on the MDS file.
8743 * If there is a valid entry for the new DS in the extended attribute
8744 * on the MDS file (as checked via the nmp argument),
8745 * nfsrv_dsgetsockmnt() returns EEXIST, so no copying will occur.
8747 error = nfsrv_dsgetsockmnt(vp, 0, buf, buflenp, &mirrorcnt, p,
8748 NULL, NULL, NULL, fname, nvpp, &nmp, curnmp, &ippos, &dsdir);
8751 if (nd.ni_vp == NULL) {
8752 if (error == 0 && nmp != NULL) {
8753 /* Search the nfsdev list for a match. */
8755 *dsp = nfsrv_findmirroredds(nmp);
8758 if (error == 0 && (nmp == NULL || *dsp == NULL)) {
8759 if (nvpp != NULL && *nvpp != NULL) {
8769 * When dspathp != NULL and curdspathp == NULL, this is a recovery
8770 * and is only allowed if there is a 0.0.0.0 IP address entry.
8771 * When curdspathp != NULL, the ippos will be set to that entry.
8773 if (error == 0 && dspathp != NULL && ippos == -1) {
8774 if (nvpp != NULL && *nvpp != NULL) {
8783 pf = (struct pnfsdsfile *)buf;
8785 /* If no zeroip pnfsdsfile, add one. */
8786 ippos = *buflenp / sizeof(*pf);
8787 *buflenp += sizeof(*pf);
8789 pf->dsf_dir = dsdir;
8790 strlcpy(pf->dsf_filename, fname,
8791 sizeof(pf->dsf_filename));
8801 * Search for a matching pnfsd mirror device structure, base on the nmp arg.
8802 * Return one if found, NULL otherwise.
8804 static struct nfsdevice *
8805 nfsrv_findmirroredds(struct nfsmount *nmp)
8807 struct nfsdevice *ds, *fndds;
8810 mtx_assert(NFSDDSMUTEXPTR, MA_OWNED);
8812 * Search the DS server list for a match with nmp.
8813 * Remove the DS entry if found and there is a mirror.
8817 if (nfsrv_devidcnt == 0)
8819 TAILQ_FOREACH(ds, &nfsrv_devidhead, nfsdev_list) {
8820 if (ds->nfsdev_nmp == nmp) {
8821 NFSD_DEBUG(4, "nfsrv_findmirroredds: fnd main ds\n");
8828 if (fndds->nfsdev_mdsisset == 0 && nfsrv_faildscnt > 0)
8830 else if (fndds->nfsdev_mdsisset != 0) {
8831 /* For the fsid is set case, search for a mirror. */
8832 TAILQ_FOREACH(ds, &nfsrv_devidhead, nfsdev_list) {
8833 if (ds != fndds && ds->nfsdev_nmp != NULL &&
8834 ds->nfsdev_mdsisset != 0 &&
8835 fsidcmp(&ds->nfsdev_mdsfsid,
8836 &fndds->nfsdev_mdsfsid) == 0) {
8842 if (fndmirror == 0) {
8843 NFSD_DEBUG(4, "nfsrv_findmirroredds: no mirror for DS\n");
8850 * Mark the appropriate devid and all associated layout as "out of space".
8853 nfsrv_marknospc(char *devid, bool setit)
8855 struct nfsdevice *ds;
8856 struct nfslayout *lyp;
8857 struct nfslayouthash *lhyp;
8861 TAILQ_FOREACH(ds, &nfsrv_devidhead, nfsdev_list) {
8862 if (NFSBCMP(ds->nfsdev_deviceid, devid, NFSX_V4DEVICEID) == 0) {
8863 NFSD_DEBUG(1, "nfsrv_marknospc: devid %d\n", setit);
8864 ds->nfsdev_nospc = setit;
8869 for (i = 0; i < nfsrv_layouthashsize; i++) {
8870 lhyp = &nfslayouthash[i];
8871 NFSLOCKLAYOUT(lhyp);
8872 TAILQ_FOREACH(lyp, &lhyp->list, lay_list) {
8873 if (NFSBCMP(lyp->lay_deviceid, devid,
8874 NFSX_V4DEVICEID) == 0) {
8875 NFSD_DEBUG(1, "nfsrv_marknospc: layout %d\n",
8878 lyp->lay_flags |= NFSLAY_NOSPC;
8880 lyp->lay_flags &= ~NFSLAY_NOSPC;
8883 NFSUNLOCKLAYOUT(lhyp);