2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2009 Rick Macklem, University of Guelph
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
34 #include "opt_inet6.h"
35 #include <sys/extattr.h>
36 #include <fs/nfs/nfsport.h>
38 struct nfsrv_stablefirst nfsrv_stablefirst;
39 int nfsrv_issuedelegs = 0;
40 int nfsrv_dolocallocks = 0;
41 struct nfsv4lock nfsv4rootfs_lock;
42 time_t nfsdev_time = 0;
43 int nfsrv_layouthashsize;
44 volatile int nfsrv_layoutcnt = 0;
45 extern uint32_t nfs_srvmaxio;
47 extern int newnfs_numnfsd;
48 extern struct nfsstatsv1 nfsstatsv1;
49 extern int nfsrv_lease;
50 extern struct timeval nfsboottime;
51 extern u_int32_t newnfs_true, newnfs_false;
52 extern struct mtx nfsrv_dslock_mtx;
53 extern struct mtx nfsrv_recalllock_mtx;
54 extern struct mtx nfsrv_dontlistlock_mtx;
55 extern int nfsd_debuglevel;
56 extern u_int nfsrv_dsdirsize;
57 extern struct nfsdevicehead nfsrv_devidhead;
58 extern int nfsrv_doflexfile;
59 extern int nfsrv_maxpnfsmirror;
62 extern struct nfsdontlisthead nfsrv_dontlisthead;
63 extern volatile int nfsrv_devidcnt;
64 extern struct nfslayouthead nfsrv_recalllisthead;
65 extern char *nfsrv_zeropnfsdat;
67 SYSCTL_DECL(_vfs_nfsd);
68 int nfsrv_statehashsize = NFSSTATEHASHSIZE;
69 SYSCTL_INT(_vfs_nfsd, OID_AUTO, statehashsize, CTLFLAG_RDTUN,
70 &nfsrv_statehashsize, 0,
71 "Size of state hash table set via loader.conf");
73 int nfsrv_clienthashsize = NFSCLIENTHASHSIZE;
74 SYSCTL_INT(_vfs_nfsd, OID_AUTO, clienthashsize, CTLFLAG_RDTUN,
75 &nfsrv_clienthashsize, 0,
76 "Size of client hash table set via loader.conf");
78 int nfsrv_lockhashsize = NFSLOCKHASHSIZE;
79 SYSCTL_INT(_vfs_nfsd, OID_AUTO, fhhashsize, CTLFLAG_RDTUN,
80 &nfsrv_lockhashsize, 0,
81 "Size of file handle hash table set via loader.conf");
83 int nfsrv_sessionhashsize = NFSSESSIONHASHSIZE;
84 SYSCTL_INT(_vfs_nfsd, OID_AUTO, sessionhashsize, CTLFLAG_RDTUN,
85 &nfsrv_sessionhashsize, 0,
86 "Size of session hash table set via loader.conf");
88 int nfsrv_layouthighwater = NFSLAYOUTHIGHWATER;
89 SYSCTL_INT(_vfs_nfsd, OID_AUTO, layouthighwater, CTLFLAG_RDTUN,
90 &nfsrv_layouthighwater, 0,
91 "High water mark for number of layouts set via loader.conf");
93 static int nfsrv_v4statelimit = NFSRV_V4STATELIMIT;
94 SYSCTL_INT(_vfs_nfsd, OID_AUTO, v4statelimit, CTLFLAG_RWTUN,
95 &nfsrv_v4statelimit, 0,
96 "High water limit for NFSv4 opens+locks+delegations");
98 static int nfsrv_writedelegifpos = 0;
99 SYSCTL_INT(_vfs_nfsd, OID_AUTO, writedelegifpos, CTLFLAG_RW,
100 &nfsrv_writedelegifpos, 0,
101 "Issue a write delegation for read opens if possible");
103 static int nfsrv_allowreadforwriteopen = 1;
104 SYSCTL_INT(_vfs_nfsd, OID_AUTO, allowreadforwriteopen, CTLFLAG_RW,
105 &nfsrv_allowreadforwriteopen, 0,
106 "Allow Reads to be done with Write Access StateIDs");
108 int nfsrv_pnfsatime = 0;
109 SYSCTL_INT(_vfs_nfsd, OID_AUTO, pnfsstrictatime, CTLFLAG_RW,
111 "For pNFS service, do Getattr ops to keep atime up-to-date");
113 int nfsrv_flexlinuxhack = 0;
114 SYSCTL_INT(_vfs_nfsd, OID_AUTO, flexlinuxhack, CTLFLAG_RW,
115 &nfsrv_flexlinuxhack, 0,
116 "For Linux clients, hack around Flex File Layout bug");
119 * Hash lists for nfs V4.
121 struct nfsclienthashhead *nfsclienthash;
122 struct nfslockhashhead *nfslockhash;
123 struct nfssessionhash *nfssessionhash;
124 struct nfslayouthash *nfslayouthash;
125 volatile int nfsrv_dontlistlen = 0;
127 static u_int32_t nfsrv_openpluslock = 0, nfsrv_delegatecnt = 0;
128 static time_t nfsrvboottime;
129 static int nfsrv_returnoldstateid = 0, nfsrv_clients = 0;
130 static int nfsrv_clienthighwater = NFSRV_CLIENTHIGHWATER;
131 static int nfsrv_nogsscallback = 0;
132 static volatile int nfsrv_writedelegcnt = 0;
133 static int nfsrv_faildscnt;
135 /* local functions */
136 static void nfsrv_dumpaclient(struct nfsclient *clp,
137 struct nfsd_dumpclients *dumpp);
138 static void nfsrv_freeopenowner(struct nfsstate *stp, int cansleep,
140 static int nfsrv_freeopen(struct nfsstate *stp, vnode_t vp, int cansleep,
142 static void nfsrv_freelockowner(struct nfsstate *stp, vnode_t vp, int cansleep,
144 static void nfsrv_freeallnfslocks(struct nfsstate *stp, vnode_t vp,
145 int cansleep, NFSPROC_T *p);
146 static void nfsrv_freenfslock(struct nfslock *lop);
147 static void nfsrv_freenfslockfile(struct nfslockfile *lfp);
148 static void nfsrv_freedeleg(struct nfsstate *);
149 static int nfsrv_getstate(struct nfsclient *clp, nfsv4stateid_t *stateidp,
150 u_int32_t flags, struct nfsstate **stpp);
151 static void nfsrv_getowner(struct nfsstatehead *hp, struct nfsstate *new_stp,
152 struct nfsstate **stpp);
153 static int nfsrv_getlockfh(vnode_t vp, u_short flags,
154 struct nfslockfile *new_lfp, fhandle_t *nfhp, NFSPROC_T *p);
155 static int nfsrv_getlockfile(u_short flags, struct nfslockfile **new_lfpp,
156 struct nfslockfile **lfpp, fhandle_t *nfhp, int lockit);
157 static void nfsrv_insertlock(struct nfslock *new_lop,
158 struct nfslock *insert_lop, struct nfsstate *stp, struct nfslockfile *lfp);
159 static void nfsrv_updatelock(struct nfsstate *stp, struct nfslock **new_lopp,
160 struct nfslock **other_lopp, struct nfslockfile *lfp);
161 static int nfsrv_getipnumber(u_char *cp);
162 static int nfsrv_checkrestart(nfsquad_t clientid, u_int32_t flags,
163 nfsv4stateid_t *stateidp, int specialid);
164 static int nfsrv_checkgrace(struct nfsrv_descript *nd, struct nfsclient *clp,
166 static int nfsrv_docallback(struct nfsclient *clp, int procnum,
167 nfsv4stateid_t *stateidp, int trunc, fhandle_t *fhp,
168 struct nfsvattr *nap, nfsattrbit_t *attrbitp, int laytype, NFSPROC_T *p);
169 static int nfsrv_cbcallargs(struct nfsrv_descript *nd, struct nfsclient *clp,
170 uint32_t callback, int op, const char *optag, struct nfsdsession **sepp,
172 static u_int32_t nfsrv_nextclientindex(void);
173 static u_int32_t nfsrv_nextstateindex(struct nfsclient *clp);
174 static void nfsrv_markstable(struct nfsclient *clp);
175 static void nfsrv_markreclaim(struct nfsclient *clp);
176 static int nfsrv_checkstable(struct nfsclient *clp);
177 static int nfsrv_clientconflict(struct nfsclient *clp, int *haslockp, struct
178 vnode *vp, NFSPROC_T *p);
179 static int nfsrv_delegconflict(struct nfsstate *stp, int *haslockp,
180 NFSPROC_T *p, vnode_t vp);
181 static int nfsrv_cleandeleg(vnode_t vp, struct nfslockfile *lfp,
182 struct nfsclient *clp, int *haslockp, NFSPROC_T *p);
183 static int nfsrv_notsamecredname(struct nfsrv_descript *nd,
184 struct nfsclient *clp);
185 static time_t nfsrv_leaseexpiry(void);
186 static void nfsrv_delaydelegtimeout(struct nfsstate *stp);
187 static int nfsrv_checkseqid(struct nfsrv_descript *nd, u_int32_t seqid,
188 struct nfsstate *stp, struct nfsrvcache *op);
189 static int nfsrv_nootherstate(struct nfsstate *stp);
190 static int nfsrv_locallock(vnode_t vp, struct nfslockfile *lfp, int flags,
191 uint64_t first, uint64_t end, struct nfslockconflict *cfp, NFSPROC_T *p);
192 static void nfsrv_localunlock(vnode_t vp, struct nfslockfile *lfp,
193 uint64_t init_first, uint64_t init_end, NFSPROC_T *p);
194 static int nfsrv_dolocal(vnode_t vp, struct nfslockfile *lfp, int flags,
195 int oldflags, uint64_t first, uint64_t end, struct nfslockconflict *cfp,
197 static void nfsrv_locallock_rollback(vnode_t vp, struct nfslockfile *lfp,
199 static void nfsrv_locallock_commit(struct nfslockfile *lfp, int flags,
200 uint64_t first, uint64_t end);
201 static void nfsrv_locklf(struct nfslockfile *lfp);
202 static void nfsrv_unlocklf(struct nfslockfile *lfp);
203 static struct nfsdsession *nfsrv_findsession(uint8_t *sessionid);
204 static int nfsrv_freesession(struct nfsdsession *sep, uint8_t *sessionid);
205 static int nfsv4_setcbsequence(struct nfsrv_descript *nd, struct nfsclient *clp,
206 int dont_replycache, struct nfsdsession **sepp, int *slotposp);
207 static int nfsv4_getcbsession(struct nfsclient *clp, struct nfsdsession **sepp);
208 static int nfsrv_addlayout(struct nfsrv_descript *nd, struct nfslayout **lypp,
209 nfsv4stateid_t *stateidp, char *layp, int *layoutlenp, NFSPROC_T *p);
210 static void nfsrv_freelayout(struct nfslayouthead *lhp, struct nfslayout *lyp);
211 static void nfsrv_freelayoutlist(nfsquad_t clientid);
212 static void nfsrv_freelayouts(nfsquad_t *clid, fsid_t *fs, int laytype,
214 static void nfsrv_freealllayouts(void);
215 static void nfsrv_freedevid(struct nfsdevice *ds);
216 static int nfsrv_setdsserver(char *dspathp, char *mdspathp, NFSPROC_T *p,
217 struct nfsdevice **dsp);
218 static void nfsrv_deleteds(struct nfsdevice *fndds);
219 static void nfsrv_allocdevid(struct nfsdevice *ds, char *addr, char *dnshost);
220 static void nfsrv_freealldevids(void);
221 static void nfsrv_flexlayouterr(struct nfsrv_descript *nd, uint32_t *layp,
222 int maxcnt, NFSPROC_T *p);
223 static int nfsrv_recalllayout(nfsquad_t clid, nfsv4stateid_t *stateidp,
224 fhandle_t *fhp, struct nfslayout *lyp, int changed, int laytype,
226 static int nfsrv_findlayout(nfsquad_t *clientidp, fhandle_t *fhp, int laytype,
227 NFSPROC_T *, struct nfslayout **lypp);
228 static int nfsrv_fndclid(nfsquad_t *clidvec, nfsquad_t clid, int clidcnt);
229 static struct nfslayout *nfsrv_filelayout(struct nfsrv_descript *nd, int iomode,
230 fhandle_t *fhp, fhandle_t *dsfhp, char *devid, fsid_t fs);
231 static struct nfslayout *nfsrv_flexlayout(struct nfsrv_descript *nd, int iomode,
232 int mirrorcnt, fhandle_t *fhp, fhandle_t *dsfhp, char *devid, fsid_t fs);
233 static int nfsrv_dontlayout(fhandle_t *fhp);
234 static int nfsrv_createdsfile(vnode_t vp, fhandle_t *fhp, struct pnfsdsfile *pf,
235 vnode_t dvp, struct nfsdevice *ds, struct ucred *cred, NFSPROC_T *p,
237 static struct nfsdevice *nfsrv_findmirroredds(struct nfsmount *nmp);
240 * Scan the client list for a match and either return the current one,
241 * create a new entry or return an error.
242 * If returning a non-error, the clp structure must either be linked into
243 * the client list or free'd.
246 nfsrv_setclient(struct nfsrv_descript *nd, struct nfsclient **new_clpp,
247 nfsquad_t *clientidp, nfsquad_t *confirmp, NFSPROC_T *p)
249 struct nfsclient *clp = NULL, *new_clp = *new_clpp;
250 int i, error = 0, ret;
251 struct nfsstate *stp, *tstp;
253 struct sockaddr_in *sin, *rin;
256 struct sockaddr_in6 *sin6, *rin6;
258 struct nfsdsession *sep, *nsep;
259 int zapit = 0, gotit, hasstate = 0, igotlock;
260 static u_int64_t confirm_index = 0;
263 * Check for state resource limit exceeded.
265 if (nfsrv_openpluslock > nfsrv_v4statelimit) {
266 error = NFSERR_RESOURCE;
270 if (nfsrv_issuedelegs == 0 ||
271 ((nd->nd_flag & ND_GSS) != 0 && nfsrv_nogsscallback != 0))
273 * Don't do callbacks when delegations are disabled or
274 * for AUTH_GSS unless enabled via nfsrv_nogsscallback.
275 * If establishing a callback connection is attempted
276 * when a firewall is blocking the callback path, the
277 * server may wait too long for the connect attempt to
278 * succeed during the Open. Some clients, such as Linux,
279 * may timeout and give up on the Open before the server
280 * replies. Also, since AUTH_GSS callbacks are not
281 * yet interoperability tested, they might cause the
282 * server to crap out, if they get past the Init call to
285 new_clp->lc_program = 0;
287 /* Lock out other nfsd threads */
288 NFSLOCKV4ROOTMUTEX();
289 nfsv4_relref(&nfsv4rootfs_lock);
291 igotlock = nfsv4_lock(&nfsv4rootfs_lock, 1, NULL,
292 NFSV4ROOTLOCKMUTEXPTR, NULL);
294 NFSUNLOCKV4ROOTMUTEX();
297 * Search for a match in the client list.
300 while (i < nfsrv_clienthashsize && !gotit) {
301 LIST_FOREACH(clp, &nfsclienthash[i], lc_hash) {
302 if (new_clp->lc_idlen == clp->lc_idlen &&
303 !NFSBCMP(new_clp->lc_id, clp->lc_id, clp->lc_idlen)) {
312 (clp->lc_flags & (LCL_NEEDSCONFIRM | LCL_ADMINREVOKED))) {
313 if ((nd->nd_flag & ND_NFSV41) != 0 && confirmp->lval[1] != 0) {
315 * For NFSv4.1, if confirmp->lval[1] is non-zero, the
316 * client is trying to update a confirmed clientid.
318 NFSLOCKV4ROOTMUTEX();
319 nfsv4_unlock(&nfsv4rootfs_lock, 1);
320 NFSUNLOCKV4ROOTMUTEX();
321 confirmp->lval[1] = 0;
322 error = NFSERR_NOENT;
326 * Get rid of the old one.
328 if (i != nfsrv_clienthashsize) {
329 LIST_REMOVE(clp, lc_hash);
330 nfsrv_cleanclient(clp, p);
331 nfsrv_freedeleglist(&clp->lc_deleg);
332 nfsrv_freedeleglist(&clp->lc_olddeleg);
336 * Add it after assigning a client id to it.
338 new_clp->lc_flags |= LCL_NEEDSCONFIRM;
339 if ((nd->nd_flag & ND_NFSV41) != 0)
340 new_clp->lc_confirm.lval[0] = confirmp->lval[0] =
343 confirmp->qval = new_clp->lc_confirm.qval =
345 clientidp->lval[0] = new_clp->lc_clientid.lval[0] =
346 (u_int32_t)nfsrvboottime;
347 clientidp->lval[1] = new_clp->lc_clientid.lval[1] =
348 nfsrv_nextclientindex();
349 new_clp->lc_stateindex = 0;
350 new_clp->lc_statemaxindex = 0;
351 new_clp->lc_cbref = 0;
352 new_clp->lc_expiry = nfsrv_leaseexpiry();
353 LIST_INIT(&new_clp->lc_open);
354 LIST_INIT(&new_clp->lc_deleg);
355 LIST_INIT(&new_clp->lc_olddeleg);
356 LIST_INIT(&new_clp->lc_session);
357 for (i = 0; i < nfsrv_statehashsize; i++)
358 LIST_INIT(&new_clp->lc_stateid[i]);
359 LIST_INSERT_HEAD(NFSCLIENTHASH(new_clp->lc_clientid), new_clp,
361 nfsstatsv1.srvclients++;
362 nfsrv_openpluslock++;
364 NFSLOCKV4ROOTMUTEX();
365 nfsv4_unlock(&nfsv4rootfs_lock, 1);
366 NFSUNLOCKV4ROOTMUTEX();
368 nfsrv_zapclient(clp, p);
374 * Now, handle the cases where the id is already issued.
376 if (nfsrv_notsamecredname(nd, clp)) {
378 * Check to see if there is expired state that should go away.
380 if (clp->lc_expiry < NFSD_MONOSEC &&
381 (!LIST_EMPTY(&clp->lc_open) || !LIST_EMPTY(&clp->lc_deleg))) {
382 nfsrv_cleanclient(clp, p);
383 nfsrv_freedeleglist(&clp->lc_deleg);
387 * If there is outstanding state, then reply NFSERR_CLIDINUSE per
388 * RFC3530 Sec. 8.1.2 last para.
390 if (!LIST_EMPTY(&clp->lc_deleg)) {
392 } else if (LIST_EMPTY(&clp->lc_open)) {
396 /* Look for an Open on the OpenOwner */
397 LIST_FOREACH(stp, &clp->lc_open, ls_list) {
398 if (!LIST_EMPTY(&stp->ls_open)) {
406 * If the uid doesn't match, return NFSERR_CLIDINUSE after
407 * filling out the correct ipaddr and portnum.
409 switch (clp->lc_req.nr_nam->sa_family) {
412 sin = (struct sockaddr_in *)new_clp->lc_req.nr_nam;
413 rin = (struct sockaddr_in *)clp->lc_req.nr_nam;
414 sin->sin_addr.s_addr = rin->sin_addr.s_addr;
415 sin->sin_port = rin->sin_port;
420 sin6 = (struct sockaddr_in6 *)new_clp->lc_req.nr_nam;
421 rin6 = (struct sockaddr_in6 *)clp->lc_req.nr_nam;
422 sin6->sin6_addr = rin6->sin6_addr;
423 sin6->sin6_port = rin6->sin6_port;
427 NFSLOCKV4ROOTMUTEX();
428 nfsv4_unlock(&nfsv4rootfs_lock, 1);
429 NFSUNLOCKV4ROOTMUTEX();
430 error = NFSERR_CLIDINUSE;
435 if (NFSBCMP(new_clp->lc_verf, clp->lc_verf, NFSX_VERF)) {
437 * If the verifier has changed, the client has rebooted
438 * and a new client id is issued. The old state info
439 * can be thrown away once the SETCLIENTID_CONFIRM occurs.
441 LIST_REMOVE(clp, lc_hash);
443 /* Get rid of all sessions on this clientid. */
444 LIST_FOREACH_SAFE(sep, &clp->lc_session, sess_list, nsep) {
445 ret = nfsrv_freesession(sep, NULL);
447 printf("nfsrv_setclient: verifier changed free"
448 " session failed=%d\n", ret);
451 new_clp->lc_flags |= LCL_NEEDSCONFIRM;
452 if ((nd->nd_flag & ND_NFSV41) != 0)
453 new_clp->lc_confirm.lval[0] = confirmp->lval[0] =
456 confirmp->qval = new_clp->lc_confirm.qval =
458 clientidp->lval[0] = new_clp->lc_clientid.lval[0] =
460 clientidp->lval[1] = new_clp->lc_clientid.lval[1] =
461 nfsrv_nextclientindex();
462 new_clp->lc_stateindex = 0;
463 new_clp->lc_statemaxindex = 0;
464 new_clp->lc_cbref = 0;
465 new_clp->lc_expiry = nfsrv_leaseexpiry();
468 * Save the state until confirmed.
470 LIST_NEWHEAD(&new_clp->lc_open, &clp->lc_open, ls_list);
471 LIST_FOREACH(tstp, &new_clp->lc_open, ls_list)
472 tstp->ls_clp = new_clp;
473 LIST_NEWHEAD(&new_clp->lc_deleg, &clp->lc_deleg, ls_list);
474 LIST_FOREACH(tstp, &new_clp->lc_deleg, ls_list)
475 tstp->ls_clp = new_clp;
476 LIST_NEWHEAD(&new_clp->lc_olddeleg, &clp->lc_olddeleg,
478 LIST_FOREACH(tstp, &new_clp->lc_olddeleg, ls_list)
479 tstp->ls_clp = new_clp;
480 for (i = 0; i < nfsrv_statehashsize; i++) {
481 LIST_NEWHEAD(&new_clp->lc_stateid[i],
482 &clp->lc_stateid[i], ls_hash);
483 LIST_FOREACH(tstp, &new_clp->lc_stateid[i], ls_hash)
484 tstp->ls_clp = new_clp;
486 LIST_INIT(&new_clp->lc_session);
487 LIST_INSERT_HEAD(NFSCLIENTHASH(new_clp->lc_clientid), new_clp,
489 nfsstatsv1.srvclients++;
490 nfsrv_openpluslock++;
492 NFSLOCKV4ROOTMUTEX();
493 nfsv4_unlock(&nfsv4rootfs_lock, 1);
494 NFSUNLOCKV4ROOTMUTEX();
497 * Must wait until any outstanding callback on the old clp
501 while (clp->lc_cbref) {
502 clp->lc_flags |= LCL_WAKEUPWANTED;
503 (void)mtx_sleep(clp, NFSSTATEMUTEXPTR, PZERO - 1,
504 "nfsd clp", 10 * hz);
507 nfsrv_zapclient(clp, p);
512 /* For NFSv4.1, mark that we found a confirmed clientid. */
513 if ((nd->nd_flag & ND_NFSV41) != 0) {
514 clientidp->lval[0] = clp->lc_clientid.lval[0];
515 clientidp->lval[1] = clp->lc_clientid.lval[1];
516 confirmp->lval[0] = 0; /* Ignored by client */
517 confirmp->lval[1] = 1;
520 * id and verifier match, so update the net address info
521 * and get rid of any existing callback authentication
522 * handle, so a new one will be acquired.
524 LIST_REMOVE(clp, lc_hash);
525 new_clp->lc_flags |= (LCL_NEEDSCONFIRM | LCL_DONTCLEAN);
526 new_clp->lc_expiry = nfsrv_leaseexpiry();
527 confirmp->qval = new_clp->lc_confirm.qval = ++confirm_index;
528 clientidp->lval[0] = new_clp->lc_clientid.lval[0] =
529 clp->lc_clientid.lval[0];
530 clientidp->lval[1] = new_clp->lc_clientid.lval[1] =
531 clp->lc_clientid.lval[1];
532 new_clp->lc_delegtime = clp->lc_delegtime;
533 new_clp->lc_stateindex = clp->lc_stateindex;
534 new_clp->lc_statemaxindex = clp->lc_statemaxindex;
535 new_clp->lc_cbref = 0;
536 LIST_NEWHEAD(&new_clp->lc_open, &clp->lc_open, ls_list);
537 LIST_FOREACH(tstp, &new_clp->lc_open, ls_list)
538 tstp->ls_clp = new_clp;
539 LIST_NEWHEAD(&new_clp->lc_deleg, &clp->lc_deleg, ls_list);
540 LIST_FOREACH(tstp, &new_clp->lc_deleg, ls_list)
541 tstp->ls_clp = new_clp;
542 LIST_NEWHEAD(&new_clp->lc_olddeleg, &clp->lc_olddeleg, ls_list);
543 LIST_FOREACH(tstp, &new_clp->lc_olddeleg, ls_list)
544 tstp->ls_clp = new_clp;
545 for (i = 0; i < nfsrv_statehashsize; i++) {
546 LIST_NEWHEAD(&new_clp->lc_stateid[i],
547 &clp->lc_stateid[i], ls_hash);
548 LIST_FOREACH(tstp, &new_clp->lc_stateid[i], ls_hash)
549 tstp->ls_clp = new_clp;
551 LIST_INIT(&new_clp->lc_session);
552 LIST_INSERT_HEAD(NFSCLIENTHASH(new_clp->lc_clientid), new_clp,
554 nfsstatsv1.srvclients++;
555 nfsrv_openpluslock++;
558 NFSLOCKV4ROOTMUTEX();
559 nfsv4_unlock(&nfsv4rootfs_lock, 1);
560 NFSUNLOCKV4ROOTMUTEX();
562 if ((nd->nd_flag & ND_NFSV41) == 0) {
564 * Must wait until any outstanding callback on the old clp
568 while (clp->lc_cbref) {
569 clp->lc_flags |= LCL_WAKEUPWANTED;
570 (void)mtx_sleep(clp, NFSSTATEMUTEXPTR, PZERO - 1,
574 nfsrv_zapclient(clp, p);
579 NFSEXITCODE2(error, nd);
584 * Check to see if the client id exists and optionally confirm it.
587 nfsrv_getclient(nfsquad_t clientid, int opflags, struct nfsclient **clpp,
588 struct nfsdsession *nsep, nfsquad_t confirm, uint32_t cbprogram,
589 struct nfsrv_descript *nd, NFSPROC_T *p)
591 struct nfsclient *clp;
592 struct nfsstate *stp;
594 struct nfsclienthashhead *hp;
595 int error = 0, igotlock, doneok;
596 struct nfssessionhash *shp;
597 struct nfsdsession *sep;
599 static uint64_t next_sess = 0;
603 if ((nd == NULL || (nd->nd_flag & ND_NFSV41) == 0 ||
604 opflags != CLOPS_RENEW) && nfsrvboottime != clientid.lval[0]) {
605 error = NFSERR_STALECLIENTID;
610 * If called with opflags == CLOPS_RENEW, the State Lock is
611 * already held. Otherwise, we need to get either that or,
612 * for the case of Confirm, lock out the nfsd threads.
614 if (opflags & CLOPS_CONFIRM) {
615 NFSLOCKV4ROOTMUTEX();
616 nfsv4_relref(&nfsv4rootfs_lock);
618 igotlock = nfsv4_lock(&nfsv4rootfs_lock, 1, NULL,
619 NFSV4ROOTLOCKMUTEXPTR, NULL);
622 * Create a new sessionid here, since we need to do it where
623 * there is a mutex held to serialize update of next_sess.
625 if ((nd->nd_flag & ND_NFSV41) != 0) {
626 sessid[0] = ++next_sess;
627 sessid[1] = clientid.qval;
629 NFSUNLOCKV4ROOTMUTEX();
630 } else if (opflags != CLOPS_RENEW) {
634 /* For NFSv4.1, the clp is acquired from the associated session. */
635 if (nd != NULL && (nd->nd_flag & ND_NFSV41) != 0 &&
636 opflags == CLOPS_RENEW) {
638 if ((nd->nd_flag & ND_HASSEQUENCE) != 0) {
639 shp = NFSSESSIONHASH(nd->nd_sessionid);
641 sep = nfsrv_findsession(nd->nd_sessionid);
644 NFSUNLOCKSESSION(shp);
647 hp = NFSCLIENTHASH(clientid);
648 LIST_FOREACH(clp, hp, lc_hash) {
649 if (clp->lc_clientid.lval[1] == clientid.lval[1])
654 if (opflags & CLOPS_CONFIRM)
655 error = NFSERR_STALECLIENTID;
657 error = NFSERR_EXPIRED;
658 } else if (clp->lc_flags & LCL_ADMINREVOKED) {
660 * If marked admin revoked, just return the error.
662 error = NFSERR_ADMINREVOKED;
665 if (opflags & CLOPS_CONFIRM) {
666 NFSLOCKV4ROOTMUTEX();
667 nfsv4_unlock(&nfsv4rootfs_lock, 1);
668 NFSUNLOCKV4ROOTMUTEX();
669 } else if (opflags != CLOPS_RENEW) {
676 * Perform any operations specified by the opflags.
678 if (opflags & CLOPS_CONFIRM) {
679 if (((nd->nd_flag & ND_NFSV41) != 0 &&
680 clp->lc_confirm.lval[0] != confirm.lval[0]) ||
681 ((nd->nd_flag & ND_NFSV41) == 0 &&
682 clp->lc_confirm.qval != confirm.qval))
683 error = NFSERR_STALECLIENTID;
684 else if (nfsrv_notsamecredname(nd, clp))
685 error = NFSERR_CLIDINUSE;
688 if ((clp->lc_flags & (LCL_NEEDSCONFIRM | LCL_DONTCLEAN)) ==
691 * Hang onto the delegations (as old delegations)
692 * for an Open with CLAIM_DELEGATE_PREV unless in
693 * grace, but get rid of the rest of the state.
695 nfsrv_cleanclient(clp, p);
696 nfsrv_freedeleglist(&clp->lc_olddeleg);
697 if (nfsrv_checkgrace(nd, clp, 0)) {
698 /* In grace, so just delete delegations */
699 nfsrv_freedeleglist(&clp->lc_deleg);
701 LIST_FOREACH(stp, &clp->lc_deleg, ls_list)
702 stp->ls_flags |= NFSLCK_OLDDELEG;
703 clp->lc_delegtime = NFSD_MONOSEC +
704 nfsrv_lease + NFSRV_LEASEDELTA;
705 LIST_NEWHEAD(&clp->lc_olddeleg, &clp->lc_deleg,
708 if ((nd->nd_flag & ND_NFSV41) != 0)
709 clp->lc_program = cbprogram;
711 clp->lc_flags &= ~(LCL_NEEDSCONFIRM | LCL_DONTCLEAN);
713 clp->lc_flags |= LCL_NEEDSCBNULL;
714 /* For NFSv4.1, link the session onto the client. */
716 /* Hold a reference on the xprt for a backchannel. */
717 if ((nsep->sess_crflags & NFSV4CRSESS_CONNBACKCHAN)
719 if (clp->lc_req.nr_client == NULL)
720 clp->lc_req.nr_client = (struct __rpc_client *)
721 clnt_bck_create(nd->nd_xprt->xp_socket,
722 cbprogram, NFSV4_CBVERS);
723 if (clp->lc_req.nr_client != NULL) {
724 SVC_ACQUIRE(nd->nd_xprt);
725 CLNT_ACQUIRE(clp->lc_req.nr_client);
726 nd->nd_xprt->xp_p2 = clp->lc_req.nr_client;
727 /* Disable idle timeout. */
728 nd->nd_xprt->xp_idletimeout = 0;
729 nsep->sess_cbsess.nfsess_xprt = nd->nd_xprt;
731 nsep->sess_crflags &= ~NFSV4CRSESS_CONNBACKCHAN;
733 NFSBCOPY(sessid, nsep->sess_sessionid,
735 NFSBCOPY(sessid, nsep->sess_cbsess.nfsess_sessionid,
737 shp = NFSSESSIONHASH(nsep->sess_sessionid);
740 LIST_INSERT_HEAD(&shp->list, nsep, sess_hash);
741 LIST_INSERT_HEAD(&clp->lc_session, nsep, sess_list);
742 nsep->sess_clp = clp;
743 NFSUNLOCKSESSION(shp);
747 } else if (clp->lc_flags & LCL_NEEDSCONFIRM) {
748 error = NFSERR_EXPIRED;
752 * If called by the Renew Op, we must check the principal.
754 if (!error && (opflags & CLOPS_RENEWOP)) {
755 if (nfsrv_notsamecredname(nd, clp)) {
757 for (i = 0; i < nfsrv_statehashsize && doneok == 0; i++) {
758 LIST_FOREACH(stp, &clp->lc_stateid[i], ls_hash) {
759 if ((stp->ls_flags & NFSLCK_OPEN) &&
760 stp->ls_uid == nd->nd_cred->cr_uid) {
767 error = NFSERR_ACCES;
769 if (!error && (clp->lc_flags & LCL_CBDOWN))
770 error = NFSERR_CBPATHDOWN;
772 if ((!error || error == NFSERR_CBPATHDOWN) &&
773 (opflags & CLOPS_RENEW)) {
774 clp->lc_expiry = nfsrv_leaseexpiry();
776 if (opflags & CLOPS_CONFIRM) {
777 NFSLOCKV4ROOTMUTEX();
778 nfsv4_unlock(&nfsv4rootfs_lock, 1);
779 NFSUNLOCKV4ROOTMUTEX();
780 } else if (opflags != CLOPS_RENEW) {
787 NFSEXITCODE2(error, nd);
792 * Perform the NFSv4.1 destroy clientid.
795 nfsrv_destroyclient(nfsquad_t clientid, NFSPROC_T *p)
797 struct nfsclient *clp;
798 struct nfsclienthashhead *hp;
799 int error = 0, i, igotlock;
801 if (nfsrvboottime != clientid.lval[0]) {
802 error = NFSERR_STALECLIENTID;
806 /* Lock out other nfsd threads */
807 NFSLOCKV4ROOTMUTEX();
808 nfsv4_relref(&nfsv4rootfs_lock);
810 igotlock = nfsv4_lock(&nfsv4rootfs_lock, 1, NULL,
811 NFSV4ROOTLOCKMUTEXPTR, NULL);
812 } while (igotlock == 0);
813 NFSUNLOCKV4ROOTMUTEX();
815 hp = NFSCLIENTHASH(clientid);
816 LIST_FOREACH(clp, hp, lc_hash) {
817 if (clp->lc_clientid.lval[1] == clientid.lval[1])
821 NFSLOCKV4ROOTMUTEX();
822 nfsv4_unlock(&nfsv4rootfs_lock, 1);
823 NFSUNLOCKV4ROOTMUTEX();
824 /* Just return ok, since it is gone. */
829 * Free up all layouts on the clientid. Should the client return the
832 nfsrv_freelayoutlist(clientid);
834 /* Scan for state on the clientid. */
835 for (i = 0; i < nfsrv_statehashsize; i++)
836 if (!LIST_EMPTY(&clp->lc_stateid[i])) {
837 NFSLOCKV4ROOTMUTEX();
838 nfsv4_unlock(&nfsv4rootfs_lock, 1);
839 NFSUNLOCKV4ROOTMUTEX();
840 error = NFSERR_CLIENTIDBUSY;
843 if (!LIST_EMPTY(&clp->lc_session) || !LIST_EMPTY(&clp->lc_deleg)) {
844 NFSLOCKV4ROOTMUTEX();
845 nfsv4_unlock(&nfsv4rootfs_lock, 1);
846 NFSUNLOCKV4ROOTMUTEX();
847 error = NFSERR_CLIENTIDBUSY;
851 /* Destroy the clientid and return ok. */
852 nfsrv_cleanclient(clp, p);
853 nfsrv_freedeleglist(&clp->lc_deleg);
854 nfsrv_freedeleglist(&clp->lc_olddeleg);
855 LIST_REMOVE(clp, lc_hash);
856 NFSLOCKV4ROOTMUTEX();
857 nfsv4_unlock(&nfsv4rootfs_lock, 1);
858 NFSUNLOCKV4ROOTMUTEX();
859 nfsrv_zapclient(clp, p);
861 NFSEXITCODE2(error, nd);
866 * Called from the new nfssvc syscall to admin revoke a clientid.
867 * Returns 0 for success, error otherwise.
870 nfsrv_adminrevoke(struct nfsd_clid *revokep, NFSPROC_T *p)
872 struct nfsclient *clp = NULL;
877 * First, lock out the nfsd so that state won't change while the
878 * revocation record is being written to the stable storage restart
881 NFSLOCKV4ROOTMUTEX();
883 igotlock = nfsv4_lock(&nfsv4rootfs_lock, 1, NULL,
884 NFSV4ROOTLOCKMUTEXPTR, NULL);
886 NFSUNLOCKV4ROOTMUTEX();
889 * Search for a match in the client list.
892 while (i < nfsrv_clienthashsize && !gotit) {
893 LIST_FOREACH(clp, &nfsclienthash[i], lc_hash) {
894 if (revokep->nclid_idlen == clp->lc_idlen &&
895 !NFSBCMP(revokep->nclid_id, clp->lc_id, clp->lc_idlen)) {
903 NFSLOCKV4ROOTMUTEX();
904 nfsv4_unlock(&nfsv4rootfs_lock, 0);
905 NFSUNLOCKV4ROOTMUTEX();
911 * Now, write out the revocation record
913 nfsrv_writestable(clp->lc_id, clp->lc_idlen, NFSNST_REVOKE, p);
914 nfsrv_backupstable();
917 * and clear out the state, marking the clientid revoked.
919 clp->lc_flags &= ~LCL_CALLBACKSON;
920 clp->lc_flags |= LCL_ADMINREVOKED;
921 nfsrv_cleanclient(clp, p);
922 nfsrv_freedeleglist(&clp->lc_deleg);
923 nfsrv_freedeleglist(&clp->lc_olddeleg);
924 NFSLOCKV4ROOTMUTEX();
925 nfsv4_unlock(&nfsv4rootfs_lock, 0);
926 NFSUNLOCKV4ROOTMUTEX();
934 * Dump out stats for all clients. Called from nfssvc(2), that is used
938 nfsrv_dumpclients(struct nfsd_dumpclients *dumpp, int maxcnt)
940 struct nfsclient *clp;
944 * First, get a reference on the nfsv4rootfs_lock so that an
945 * exclusive lock cannot be acquired while dumping the clients.
947 NFSLOCKV4ROOTMUTEX();
948 nfsv4_getref(&nfsv4rootfs_lock, NULL, NFSV4ROOTLOCKMUTEXPTR, NULL);
949 NFSUNLOCKV4ROOTMUTEX();
952 * Rattle through the client lists until done.
954 while (i < nfsrv_clienthashsize && cnt < maxcnt) {
955 clp = LIST_FIRST(&nfsclienthash[i]);
956 while (clp != LIST_END(&nfsclienthash[i]) && cnt < maxcnt) {
957 nfsrv_dumpaclient(clp, &dumpp[cnt]);
959 clp = LIST_NEXT(clp, lc_hash);
964 dumpp[cnt].ndcl_clid.nclid_idlen = 0;
966 NFSLOCKV4ROOTMUTEX();
967 nfsv4_relref(&nfsv4rootfs_lock);
968 NFSUNLOCKV4ROOTMUTEX();
972 * Dump stats for a client. Must be called with the NFSSTATELOCK and spl'd.
975 nfsrv_dumpaclient(struct nfsclient *clp, struct nfsd_dumpclients *dumpp)
977 struct nfsstate *stp, *openstp, *lckownstp;
981 struct sockaddr_in *rin;
984 struct sockaddr_in6 *rin6;
987 dumpp->ndcl_nopenowners = dumpp->ndcl_nlockowners = 0;
988 dumpp->ndcl_nopens = dumpp->ndcl_nlocks = 0;
989 dumpp->ndcl_ndelegs = dumpp->ndcl_nolddelegs = 0;
990 dumpp->ndcl_flags = clp->lc_flags;
991 dumpp->ndcl_clid.nclid_idlen = clp->lc_idlen;
992 NFSBCOPY(clp->lc_id, dumpp->ndcl_clid.nclid_id, clp->lc_idlen);
993 af = clp->lc_req.nr_nam->sa_family;
994 dumpp->ndcl_addrfam = af;
998 rin = (struct sockaddr_in *)clp->lc_req.nr_nam;
999 dumpp->ndcl_cbaddr.sin_addr = rin->sin_addr;
1004 rin6 = (struct sockaddr_in6 *)clp->lc_req.nr_nam;
1005 dumpp->ndcl_cbaddr.sin6_addr = rin6->sin6_addr;
1011 * Now, scan the state lists and total up the opens and locks.
1013 LIST_FOREACH(stp, &clp->lc_open, ls_list) {
1014 dumpp->ndcl_nopenowners++;
1015 LIST_FOREACH(openstp, &stp->ls_open, ls_list) {
1016 dumpp->ndcl_nopens++;
1017 LIST_FOREACH(lckownstp, &openstp->ls_open, ls_list) {
1018 dumpp->ndcl_nlockowners++;
1019 LIST_FOREACH(lop, &lckownstp->ls_lock, lo_lckowner) {
1020 dumpp->ndcl_nlocks++;
1027 * and the delegation lists.
1029 LIST_FOREACH(stp, &clp->lc_deleg, ls_list) {
1030 dumpp->ndcl_ndelegs++;
1032 LIST_FOREACH(stp, &clp->lc_olddeleg, ls_list) {
1033 dumpp->ndcl_nolddelegs++;
1038 * Dump out lock stats for a file.
1041 nfsrv_dumplocks(vnode_t vp, struct nfsd_dumplocks *ldumpp, int maxcnt,
1044 struct nfsstate *stp;
1045 struct nfslock *lop;
1047 struct nfslockfile *lfp;
1050 struct sockaddr_in *rin;
1053 struct sockaddr_in6 *rin6;
1058 ret = nfsrv_getlockfh(vp, 0, NULL, &nfh, p);
1060 * First, get a reference on the nfsv4rootfs_lock so that an
1061 * exclusive lock on it cannot be acquired while dumping the locks.
1063 NFSLOCKV4ROOTMUTEX();
1064 nfsv4_getref(&nfsv4rootfs_lock, NULL, NFSV4ROOTLOCKMUTEXPTR, NULL);
1065 NFSUNLOCKV4ROOTMUTEX();
1068 ret = nfsrv_getlockfile(0, NULL, &lfp, &nfh, 0);
1070 ldumpp[0].ndlck_clid.nclid_idlen = 0;
1072 NFSLOCKV4ROOTMUTEX();
1073 nfsv4_relref(&nfsv4rootfs_lock);
1074 NFSUNLOCKV4ROOTMUTEX();
1079 * For each open share on file, dump it out.
1081 stp = LIST_FIRST(&lfp->lf_open);
1082 while (stp != LIST_END(&lfp->lf_open) && cnt < maxcnt) {
1083 ldumpp[cnt].ndlck_flags = stp->ls_flags;
1084 ldumpp[cnt].ndlck_stateid.seqid = stp->ls_stateid.seqid;
1085 ldumpp[cnt].ndlck_stateid.other[0] = stp->ls_stateid.other[0];
1086 ldumpp[cnt].ndlck_stateid.other[1] = stp->ls_stateid.other[1];
1087 ldumpp[cnt].ndlck_stateid.other[2] = stp->ls_stateid.other[2];
1088 ldumpp[cnt].ndlck_owner.nclid_idlen =
1089 stp->ls_openowner->ls_ownerlen;
1090 NFSBCOPY(stp->ls_openowner->ls_owner,
1091 ldumpp[cnt].ndlck_owner.nclid_id,
1092 stp->ls_openowner->ls_ownerlen);
1093 ldumpp[cnt].ndlck_clid.nclid_idlen = stp->ls_clp->lc_idlen;
1094 NFSBCOPY(stp->ls_clp->lc_id, ldumpp[cnt].ndlck_clid.nclid_id,
1095 stp->ls_clp->lc_idlen);
1096 af = stp->ls_clp->lc_req.nr_nam->sa_family;
1097 ldumpp[cnt].ndlck_addrfam = af;
1101 rin = (struct sockaddr_in *)stp->ls_clp->lc_req.nr_nam;
1102 ldumpp[cnt].ndlck_cbaddr.sin_addr = rin->sin_addr;
1107 rin6 = (struct sockaddr_in6 *)
1108 stp->ls_clp->lc_req.nr_nam;
1109 ldumpp[cnt].ndlck_cbaddr.sin6_addr = rin6->sin6_addr;
1113 stp = LIST_NEXT(stp, ls_file);
1120 lop = LIST_FIRST(&lfp->lf_lock);
1121 while (lop != LIST_END(&lfp->lf_lock) && cnt < maxcnt) {
1123 ldumpp[cnt].ndlck_flags = lop->lo_flags;
1124 ldumpp[cnt].ndlck_first = lop->lo_first;
1125 ldumpp[cnt].ndlck_end = lop->lo_end;
1126 ldumpp[cnt].ndlck_stateid.seqid = stp->ls_stateid.seqid;
1127 ldumpp[cnt].ndlck_stateid.other[0] = stp->ls_stateid.other[0];
1128 ldumpp[cnt].ndlck_stateid.other[1] = stp->ls_stateid.other[1];
1129 ldumpp[cnt].ndlck_stateid.other[2] = stp->ls_stateid.other[2];
1130 ldumpp[cnt].ndlck_owner.nclid_idlen = stp->ls_ownerlen;
1131 NFSBCOPY(stp->ls_owner, ldumpp[cnt].ndlck_owner.nclid_id,
1133 ldumpp[cnt].ndlck_clid.nclid_idlen = stp->ls_clp->lc_idlen;
1134 NFSBCOPY(stp->ls_clp->lc_id, ldumpp[cnt].ndlck_clid.nclid_id,
1135 stp->ls_clp->lc_idlen);
1136 af = stp->ls_clp->lc_req.nr_nam->sa_family;
1137 ldumpp[cnt].ndlck_addrfam = af;
1141 rin = (struct sockaddr_in *)stp->ls_clp->lc_req.nr_nam;
1142 ldumpp[cnt].ndlck_cbaddr.sin_addr = rin->sin_addr;
1147 rin6 = (struct sockaddr_in6 *)
1148 stp->ls_clp->lc_req.nr_nam;
1149 ldumpp[cnt].ndlck_cbaddr.sin6_addr = rin6->sin6_addr;
1153 lop = LIST_NEXT(lop, lo_lckfile);
1158 * and the delegations.
1160 stp = LIST_FIRST(&lfp->lf_deleg);
1161 while (stp != LIST_END(&lfp->lf_deleg) && cnt < maxcnt) {
1162 ldumpp[cnt].ndlck_flags = stp->ls_flags;
1163 ldumpp[cnt].ndlck_stateid.seqid = stp->ls_stateid.seqid;
1164 ldumpp[cnt].ndlck_stateid.other[0] = stp->ls_stateid.other[0];
1165 ldumpp[cnt].ndlck_stateid.other[1] = stp->ls_stateid.other[1];
1166 ldumpp[cnt].ndlck_stateid.other[2] = stp->ls_stateid.other[2];
1167 ldumpp[cnt].ndlck_owner.nclid_idlen = 0;
1168 ldumpp[cnt].ndlck_clid.nclid_idlen = stp->ls_clp->lc_idlen;
1169 NFSBCOPY(stp->ls_clp->lc_id, ldumpp[cnt].ndlck_clid.nclid_id,
1170 stp->ls_clp->lc_idlen);
1171 af = stp->ls_clp->lc_req.nr_nam->sa_family;
1172 ldumpp[cnt].ndlck_addrfam = af;
1176 rin = (struct sockaddr_in *)stp->ls_clp->lc_req.nr_nam;
1177 ldumpp[cnt].ndlck_cbaddr.sin_addr = rin->sin_addr;
1182 rin6 = (struct sockaddr_in6 *)
1183 stp->ls_clp->lc_req.nr_nam;
1184 ldumpp[cnt].ndlck_cbaddr.sin6_addr = rin6->sin6_addr;
1188 stp = LIST_NEXT(stp, ls_file);
1193 * If list isn't full, mark end of list by setting the client name
1197 ldumpp[cnt].ndlck_clid.nclid_idlen = 0;
1199 NFSLOCKV4ROOTMUTEX();
1200 nfsv4_relref(&nfsv4rootfs_lock);
1201 NFSUNLOCKV4ROOTMUTEX();
1205 * Server timer routine. It can scan any linked list, so long
1206 * as it holds the spin/mutex lock and there is no exclusive lock on
1208 * (For OpenBSD, a kthread is ok. For FreeBSD, I think it is ok
1209 * to do this from a callout, since the spin locks work. For
1210 * Darwin, I'm not sure what will work correctly yet.)
1211 * Should be called once per second.
1214 nfsrv_servertimer(void)
1216 struct nfsclient *clp, *nclp;
1217 struct nfsstate *stp, *nstp;
1221 * Make sure nfsboottime is set. This is used by V3 as well
1222 * as V4. Note that nfsboottime is not nfsrvboottime, which is
1223 * only used by the V4 server for leases.
1225 if (nfsboottime.tv_sec == 0)
1226 NFSSETBOOTTIME(nfsboottime);
1229 * If server hasn't started yet, just return.
1232 if (nfsrv_stablefirst.nsf_eograce == 0) {
1236 if (!(nfsrv_stablefirst.nsf_flags & NFSNSF_UPDATEDONE)) {
1237 if (!(nfsrv_stablefirst.nsf_flags & NFSNSF_GRACEOVER) &&
1238 NFSD_MONOSEC > nfsrv_stablefirst.nsf_eograce)
1239 nfsrv_stablefirst.nsf_flags |=
1240 (NFSNSF_GRACEOVER | NFSNSF_NEEDLOCK);
1246 * Try and get a reference count on the nfsv4rootfs_lock so that
1247 * no nfsd thread can acquire an exclusive lock on it before this
1248 * call is done. If it is already exclusively locked, just return.
1250 NFSLOCKV4ROOTMUTEX();
1251 got_ref = nfsv4_getref_nonblock(&nfsv4rootfs_lock);
1252 NFSUNLOCKV4ROOTMUTEX();
1259 * For each client...
1261 for (i = 0; i < nfsrv_clienthashsize; i++) {
1262 clp = LIST_FIRST(&nfsclienthash[i]);
1263 while (clp != LIST_END(&nfsclienthash[i])) {
1264 nclp = LIST_NEXT(clp, lc_hash);
1265 if (!(clp->lc_flags & LCL_EXPIREIT)) {
1266 if (((clp->lc_expiry + NFSRV_STALELEASE) < NFSD_MONOSEC
1267 && ((LIST_EMPTY(&clp->lc_deleg)
1268 && LIST_EMPTY(&clp->lc_open)) ||
1269 nfsrv_clients > nfsrv_clienthighwater)) ||
1270 (clp->lc_expiry + NFSRV_MOULDYLEASE) < NFSD_MONOSEC ||
1271 (clp->lc_expiry < NFSD_MONOSEC &&
1272 (nfsrv_openpluslock * 10 / 9) > nfsrv_v4statelimit)) {
1274 * Lease has expired several nfsrv_lease times ago:
1276 * - no state is associated with it
1278 * - above high water mark for number of clients
1279 * (nfsrv_clienthighwater should be large enough
1280 * that this only occurs when clients fail to
1281 * use the same nfs_client_id4.id. Maybe somewhat
1282 * higher that the maximum number of clients that
1283 * will mount this server?)
1285 * Lease has expired a very long time ago
1287 * Lease has expired PLUS the number of opens + locks
1288 * has exceeded 90% of capacity
1290 * --> Mark for expiry. The actual expiry will be done
1291 * by an nfsd sometime soon.
1293 clp->lc_flags |= LCL_EXPIREIT;
1294 nfsrv_stablefirst.nsf_flags |=
1295 (NFSNSF_NEEDLOCK | NFSNSF_EXPIREDCLIENT);
1298 * If there are no opens, increment no open tick cnt
1299 * If time exceeds NFSNOOPEN, mark it to be thrown away
1300 * otherwise, if there is an open, reset no open time
1301 * Hopefully, this will avoid excessive re-creation
1302 * of open owners and subsequent open confirms.
1304 stp = LIST_FIRST(&clp->lc_open);
1305 while (stp != LIST_END(&clp->lc_open)) {
1306 nstp = LIST_NEXT(stp, ls_list);
1307 if (LIST_EMPTY(&stp->ls_open)) {
1309 if (stp->ls_noopens > NFSNOOPEN ||
1310 (nfsrv_openpluslock * 2) >
1312 nfsrv_stablefirst.nsf_flags |=
1315 stp->ls_noopens = 0;
1325 NFSLOCKV4ROOTMUTEX();
1326 nfsv4_relref(&nfsv4rootfs_lock);
1327 NFSUNLOCKV4ROOTMUTEX();
1331 * The following set of functions free up the various data structures.
1334 * Clear out all open/lock state related to this nfsclient.
1335 * Caller must hold an exclusive lock on nfsv4rootfs_lock, so that
1336 * there are no other active nfsd threads.
1339 nfsrv_cleanclient(struct nfsclient *clp, NFSPROC_T *p)
1341 struct nfsstate *stp, *nstp;
1342 struct nfsdsession *sep, *nsep;
1344 LIST_FOREACH_SAFE(stp, &clp->lc_open, ls_list, nstp)
1345 nfsrv_freeopenowner(stp, 1, p);
1346 if ((clp->lc_flags & LCL_ADMINREVOKED) == 0)
1347 LIST_FOREACH_SAFE(sep, &clp->lc_session, sess_list, nsep)
1348 (void)nfsrv_freesession(sep, NULL);
1352 * Free a client that has been cleaned. It should also already have been
1353 * removed from the lists.
1354 * (Just to be safe w.r.t. newnfs_disconnect(), call this function when
1355 * softclock interrupts are enabled.)
1358 nfsrv_zapclient(struct nfsclient *clp, NFSPROC_T *p)
1362 if ((clp->lc_flags & (LCL_GSS | LCL_CALLBACKSON)) ==
1363 (LCL_GSS | LCL_CALLBACKSON) &&
1364 (clp->lc_hand.nfsh_flag & NFSG_COMPLETE) &&
1365 clp->lc_handlelen > 0) {
1366 clp->lc_hand.nfsh_flag &= ~NFSG_COMPLETE;
1367 clp->lc_hand.nfsh_flag |= NFSG_DESTROYED;
1368 (void) nfsrv_docallback(clp, NFSV4PROC_CBNULL,
1369 NULL, 0, NULL, NULL, NULL, 0, p);
1372 newnfs_disconnect(NULL, &clp->lc_req);
1373 free(clp->lc_req.nr_nam, M_SONAME);
1374 NFSFREEMUTEX(&clp->lc_req.nr_mtx);
1375 free(clp->lc_stateid, M_NFSDCLIENT);
1376 free(clp, M_NFSDCLIENT);
1378 nfsstatsv1.srvclients--;
1379 nfsrv_openpluslock--;
1385 * Free a list of delegation state structures.
1386 * (This function will also free all nfslockfile structures that no
1387 * longer have associated state.)
1390 nfsrv_freedeleglist(struct nfsstatehead *sthp)
1392 struct nfsstate *stp, *nstp;
1394 LIST_FOREACH_SAFE(stp, sthp, ls_list, nstp) {
1395 nfsrv_freedeleg(stp);
1401 * Free up a delegation.
1404 nfsrv_freedeleg(struct nfsstate *stp)
1406 struct nfslockfile *lfp;
1408 LIST_REMOVE(stp, ls_hash);
1409 LIST_REMOVE(stp, ls_list);
1410 LIST_REMOVE(stp, ls_file);
1411 if ((stp->ls_flags & NFSLCK_DELEGWRITE) != 0)
1412 nfsrv_writedelegcnt--;
1414 if (LIST_EMPTY(&lfp->lf_open) &&
1415 LIST_EMPTY(&lfp->lf_lock) && LIST_EMPTY(&lfp->lf_deleg) &&
1416 LIST_EMPTY(&lfp->lf_locallock) && LIST_EMPTY(&lfp->lf_rollback) &&
1417 lfp->lf_usecount == 0 &&
1418 nfsv4_testlock(&lfp->lf_locallock_lck) == 0)
1419 nfsrv_freenfslockfile(lfp);
1420 free(stp, M_NFSDSTATE);
1421 nfsstatsv1.srvdelegates--;
1422 nfsrv_openpluslock--;
1423 nfsrv_delegatecnt--;
1427 * This function frees an open owner and all associated opens.
1430 nfsrv_freeopenowner(struct nfsstate *stp, int cansleep, NFSPROC_T *p)
1432 struct nfsstate *nstp, *tstp;
1434 LIST_REMOVE(stp, ls_list);
1436 * Now, free all associated opens.
1438 nstp = LIST_FIRST(&stp->ls_open);
1439 while (nstp != LIST_END(&stp->ls_open)) {
1441 nstp = LIST_NEXT(nstp, ls_list);
1442 (void) nfsrv_freeopen(tstp, NULL, cansleep, p);
1445 nfsrvd_derefcache(stp->ls_op);
1446 free(stp, M_NFSDSTATE);
1447 nfsstatsv1.srvopenowners--;
1448 nfsrv_openpluslock--;
1452 * This function frees an open (nfsstate open structure) with all associated
1453 * lock_owners and locks. It also frees the nfslockfile structure iff there
1454 * are no other opens on the file.
1455 * Returns 1 if it free'd the nfslockfile, 0 otherwise.
1458 nfsrv_freeopen(struct nfsstate *stp, vnode_t vp, int cansleep, NFSPROC_T *p)
1460 struct nfsstate *nstp, *tstp;
1461 struct nfslockfile *lfp;
1464 LIST_REMOVE(stp, ls_hash);
1465 LIST_REMOVE(stp, ls_list);
1466 LIST_REMOVE(stp, ls_file);
1470 * Now, free all lockowners associated with this open.
1472 LIST_FOREACH_SAFE(tstp, &stp->ls_open, ls_list, nstp)
1473 nfsrv_freelockowner(tstp, vp, cansleep, p);
1476 * The nfslockfile is freed here if there are no locks
1477 * associated with the open.
1478 * If there are locks associated with the open, the
1479 * nfslockfile structure can be freed via nfsrv_freelockowner().
1480 * Acquire the state mutex to avoid races with calls to
1481 * nfsrv_getlockfile().
1485 if (lfp != NULL && LIST_EMPTY(&lfp->lf_open) &&
1486 LIST_EMPTY(&lfp->lf_deleg) && LIST_EMPTY(&lfp->lf_lock) &&
1487 LIST_EMPTY(&lfp->lf_locallock) && LIST_EMPTY(&lfp->lf_rollback) &&
1488 lfp->lf_usecount == 0 &&
1489 (cansleep != 0 || nfsv4_testlock(&lfp->lf_locallock_lck) == 0)) {
1490 nfsrv_freenfslockfile(lfp);
1496 free(stp, M_NFSDSTATE);
1497 nfsstatsv1.srvopens--;
1498 nfsrv_openpluslock--;
1503 * Frees a lockowner and all associated locks.
1506 nfsrv_freelockowner(struct nfsstate *stp, vnode_t vp, int cansleep,
1510 LIST_REMOVE(stp, ls_hash);
1511 LIST_REMOVE(stp, ls_list);
1512 nfsrv_freeallnfslocks(stp, vp, cansleep, p);
1514 nfsrvd_derefcache(stp->ls_op);
1515 free(stp, M_NFSDSTATE);
1516 nfsstatsv1.srvlockowners--;
1517 nfsrv_openpluslock--;
1521 * Free all the nfs locks on a lockowner.
1524 nfsrv_freeallnfslocks(struct nfsstate *stp, vnode_t vp, int cansleep,
1527 struct nfslock *lop, *nlop;
1528 struct nfsrollback *rlp, *nrlp;
1529 struct nfslockfile *lfp = NULL;
1532 uint64_t first, end;
1535 ASSERT_VOP_UNLOCKED(vp, "nfsrv_freeallnfslocks: vnode locked");
1536 lop = LIST_FIRST(&stp->ls_lock);
1537 while (lop != LIST_END(&stp->ls_lock)) {
1538 nlop = LIST_NEXT(lop, lo_lckowner);
1540 * Since all locks should be for the same file, lfp should
1545 else if (lfp != lop->lo_lfp)
1546 panic("allnfslocks");
1548 * If vp is NULL and cansleep != 0, a vnode must be acquired
1549 * from the file handle. This only occurs when called from
1550 * nfsrv_cleanclient().
1553 if (nfsrv_dolocallocks == 0)
1555 else if (vp == NULL && cansleep != 0) {
1556 tvp = nfsvno_getvp(&lfp->lf_fh);
1567 first = lop->lo_first;
1569 nfsrv_freenfslock(lop);
1570 nfsrv_localunlock(tvp, lfp, first, end, p);
1571 LIST_FOREACH_SAFE(rlp, &lfp->lf_rollback, rlck_list,
1573 free(rlp, M_NFSDROLLBACK);
1574 LIST_INIT(&lfp->lf_rollback);
1576 nfsrv_freenfslock(lop);
1579 if (vp == NULL && tvp != NULL)
1584 * Free an nfslock structure.
1587 nfsrv_freenfslock(struct nfslock *lop)
1590 if (lop->lo_lckfile.le_prev != NULL) {
1591 LIST_REMOVE(lop, lo_lckfile);
1592 nfsstatsv1.srvlocks--;
1593 nfsrv_openpluslock--;
1595 LIST_REMOVE(lop, lo_lckowner);
1596 free(lop, M_NFSDLOCK);
1600 * This function frees an nfslockfile structure.
1603 nfsrv_freenfslockfile(struct nfslockfile *lfp)
1606 LIST_REMOVE(lfp, lf_hash);
1607 free(lfp, M_NFSDLOCKFILE);
1611 * This function looks up an nfsstate structure via stateid.
1614 nfsrv_getstate(struct nfsclient *clp, nfsv4stateid_t *stateidp, __unused u_int32_t flags,
1615 struct nfsstate **stpp)
1617 struct nfsstate *stp;
1618 struct nfsstatehead *hp;
1622 hp = NFSSTATEHASH(clp, *stateidp);
1623 LIST_FOREACH(stp, hp, ls_hash) {
1624 if (!NFSBCMP(stp->ls_stateid.other, stateidp->other,
1630 * If no state id in list, return NFSERR_BADSTATEID.
1632 if (stp == LIST_END(hp)) {
1633 error = NFSERR_BADSTATEID;
1644 * This function gets an nfsstate structure via owner string.
1647 nfsrv_getowner(struct nfsstatehead *hp, struct nfsstate *new_stp,
1648 struct nfsstate **stpp)
1650 struct nfsstate *stp;
1653 LIST_FOREACH(stp, hp, ls_list) {
1654 if (new_stp->ls_ownerlen == stp->ls_ownerlen &&
1655 !NFSBCMP(new_stp->ls_owner,stp->ls_owner,stp->ls_ownerlen)) {
1663 * Lock control function called to update lock status.
1664 * Returns 0 upon success, -1 if there is no lock and the flags indicate
1665 * that one isn't to be created and an NFSERR_xxx for other errors.
1666 * The structures new_stp and new_lop are passed in as pointers that should
1667 * be set to NULL if the structure is used and shouldn't be free'd.
1668 * For the NFSLCK_TEST and NFSLCK_CHECK cases, the structures are
1669 * never used and can safely be allocated on the stack. For all other
1670 * cases, *new_stpp and *new_lopp should be malloc'd before the call,
1671 * in case they are used.
1674 nfsrv_lockctrl(vnode_t vp, struct nfsstate **new_stpp,
1675 struct nfslock **new_lopp, struct nfslockconflict *cfp,
1676 nfsquad_t clientid, nfsv4stateid_t *stateidp,
1677 __unused struct nfsexstuff *exp,
1678 struct nfsrv_descript *nd, NFSPROC_T *p)
1680 struct nfslock *lop;
1681 struct nfsstate *new_stp = *new_stpp;
1682 struct nfslock *new_lop = *new_lopp;
1683 struct nfsstate *tstp, *mystp, *nstp;
1685 struct nfslockfile *lfp;
1686 struct nfslock *other_lop = NULL;
1687 struct nfsstate *stp, *lckstp = NULL;
1688 struct nfsclient *clp = NULL;
1690 int error = 0, haslock = 0, ret, reterr;
1691 int getlckret, delegation = 0, filestruct_locked, vnode_unlocked = 0;
1693 uint64_t first, end;
1694 uint32_t lock_flags;
1696 if (new_stp->ls_flags & (NFSLCK_CHECK | NFSLCK_SETATTR)) {
1698 * Note the special cases of "all 1s" or "all 0s" stateids and
1699 * let reads with all 1s go ahead.
1701 if (new_stp->ls_stateid.seqid == 0x0 &&
1702 new_stp->ls_stateid.other[0] == 0x0 &&
1703 new_stp->ls_stateid.other[1] == 0x0 &&
1704 new_stp->ls_stateid.other[2] == 0x0)
1706 else if (new_stp->ls_stateid.seqid == 0xffffffff &&
1707 new_stp->ls_stateid.other[0] == 0xffffffff &&
1708 new_stp->ls_stateid.other[1] == 0xffffffff &&
1709 new_stp->ls_stateid.other[2] == 0xffffffff)
1714 * Check for restart conditions (client and server).
1716 error = nfsrv_checkrestart(clientid, new_stp->ls_flags,
1717 &new_stp->ls_stateid, specialid);
1722 * Check for state resource limit exceeded.
1724 if ((new_stp->ls_flags & NFSLCK_LOCK) &&
1725 nfsrv_openpluslock > nfsrv_v4statelimit) {
1726 error = NFSERR_RESOURCE;
1731 * For the lock case, get another nfslock structure,
1732 * just in case we need it.
1733 * Malloc now, before we start sifting through the linked lists,
1734 * in case we have to wait for memory.
1737 if (new_stp->ls_flags & NFSLCK_LOCK)
1738 other_lop = malloc(sizeof (struct nfslock),
1739 M_NFSDLOCK, M_WAITOK);
1740 filestruct_locked = 0;
1745 * Get the lockfile structure for CFH now, so we can do a sanity
1746 * check against the stateid, before incrementing the seqid#, since
1747 * we want to return NFSERR_BADSTATEID on failure and the seqid#
1748 * shouldn't be incremented for this case.
1749 * If nfsrv_getlockfile() returns -1, it means "not found", which
1750 * will be handled later.
1751 * If we are doing Lock/LockU and local locking is enabled, sleep
1752 * lock the nfslockfile structure.
1754 getlckret = nfsrv_getlockfh(vp, new_stp->ls_flags, NULL, &nfh, p);
1756 if (getlckret == 0) {
1757 if ((new_stp->ls_flags & (NFSLCK_LOCK | NFSLCK_UNLOCK)) != 0 &&
1758 nfsrv_dolocallocks != 0 && nd->nd_repstat == 0) {
1759 getlckret = nfsrv_getlockfile(new_stp->ls_flags, NULL,
1762 filestruct_locked = 1;
1764 getlckret = nfsrv_getlockfile(new_stp->ls_flags, NULL,
1767 if (getlckret != 0 && getlckret != -1)
1770 if (filestruct_locked != 0) {
1771 LIST_INIT(&lfp->lf_rollback);
1772 if ((new_stp->ls_flags & NFSLCK_LOCK)) {
1774 * For local locking, do the advisory locking now, so
1775 * that any conflict can be detected. A failure later
1776 * can be rolled back locally. If an error is returned,
1777 * struct nfslockfile has been unlocked and any local
1778 * locking rolled back.
1781 if (vnode_unlocked == 0) {
1782 ASSERT_VOP_ELOCKED(vp, "nfsrv_lockctrl1");
1786 reterr = nfsrv_locallock(vp, lfp,
1787 (new_lop->lo_flags & (NFSLCK_READ | NFSLCK_WRITE)),
1788 new_lop->lo_first, new_lop->lo_end, cfp, p);
1793 if (specialid == 0) {
1794 if (new_stp->ls_flags & NFSLCK_TEST) {
1796 * RFC 3530 does not list LockT as an op that renews a
1797 * lease, but the consensus seems to be that it is ok
1798 * for a server to do so.
1800 error = nfsrv_getclient(clientid, CLOPS_RENEW, &clp, NULL,
1801 (nfsquad_t)((u_quad_t)0), 0, nd, p);
1804 * Since NFSERR_EXPIRED, NFSERR_ADMINREVOKED are not valid
1805 * error returns for LockT, just go ahead and test for a lock,
1806 * since there are no locks for this client, but other locks
1807 * can conflict. (ie. same client will always be false)
1809 if (error == NFSERR_EXPIRED || error == NFSERR_ADMINREVOKED)
1813 error = nfsrv_getclient(clientid, CLOPS_RENEW, &clp, NULL,
1814 (nfsquad_t)((u_quad_t)0), 0, nd, p);
1817 * Look up the stateid
1819 error = nfsrv_getstate(clp, &new_stp->ls_stateid,
1820 new_stp->ls_flags, &stp);
1822 * do some sanity checks for an unconfirmed open or a
1823 * stateid that refers to the wrong file, for an open stateid
1825 if (error == 0 && (stp->ls_flags & NFSLCK_OPEN) &&
1826 ((stp->ls_openowner->ls_flags & NFSLCK_NEEDSCONFIRM) ||
1827 (getlckret == 0 && stp->ls_lfp != lfp))){
1829 * NFSLCK_SETATTR should return OK rather than NFSERR_BADSTATEID
1830 * The only exception is using SETATTR with SIZE.
1832 if ((new_stp->ls_flags &
1833 (NFSLCK_SETATTR | NFSLCK_CHECK)) != NFSLCK_SETATTR)
1834 error = NFSERR_BADSTATEID;
1838 (stp->ls_flags & (NFSLCK_DELEGREAD | NFSLCK_DELEGWRITE)) &&
1839 getlckret == 0 && stp->ls_lfp != lfp)
1840 error = NFSERR_BADSTATEID;
1843 * If the lockowner stateid doesn't refer to the same file,
1844 * I believe that is considered ok, since some clients will
1845 * only create a single lockowner and use that for all locks
1847 * For now, log it as a diagnostic, instead of considering it
1850 if (error == 0 && (stp->ls_flags &
1851 (NFSLCK_OPEN | NFSLCK_DELEGREAD | NFSLCK_DELEGWRITE)) == 0 &&
1852 getlckret == 0 && stp->ls_lfp != lfp) {
1854 printf("Got a lock statid for different file open\n");
1857 error = NFSERR_BADSTATEID;
1862 if (new_stp->ls_flags & NFSLCK_OPENTOLOCK) {
1864 * If haslock set, we've already checked the seqid.
1867 if (stp->ls_flags & NFSLCK_OPEN)
1868 error = nfsrv_checkseqid(nd, new_stp->ls_seq,
1869 stp->ls_openowner, new_stp->ls_op);
1871 error = NFSERR_BADSTATEID;
1874 nfsrv_getowner(&stp->ls_open, new_stp, &lckstp);
1877 * For NFSv4.1 and NFSv4.2 allow an
1878 * open_to_lock_owner when the lock_owner already
1879 * exists. Just clear NFSLCK_OPENTOLOCK so that
1880 * a new lock_owner will not be created.
1881 * RFC7530 states that the error for NFSv4.0
1882 * is NFS4ERR_BAD_SEQID.
1884 if ((nd->nd_flag & ND_NFSV41) != 0)
1885 new_stp->ls_flags &= ~NFSLCK_OPENTOLOCK;
1887 error = NFSERR_BADSEQID;
1890 } else if (new_stp->ls_flags&(NFSLCK_LOCK|NFSLCK_UNLOCK)) {
1892 * If haslock set, ditto above.
1895 if (stp->ls_flags & NFSLCK_OPEN)
1896 error = NFSERR_BADSTATEID;
1898 error = nfsrv_checkseqid(nd, new_stp->ls_seq,
1899 stp, new_stp->ls_op);
1907 * If the seqid part of the stateid isn't the same, return
1908 * NFSERR_OLDSTATEID for cases other than I/O Ops.
1909 * For I/O Ops, only return NFSERR_OLDSTATEID if
1910 * nfsrv_returnoldstateid is set. (The consensus on the email
1911 * list was that most clients would prefer to not receive
1912 * NFSERR_OLDSTATEID for I/O Ops, but the RFC suggests that that
1913 * is what will happen, so I use the nfsrv_returnoldstateid to
1914 * allow for either server configuration.)
1916 if (!error && stp->ls_stateid.seqid!=new_stp->ls_stateid.seqid &&
1917 (((nd->nd_flag & ND_NFSV41) == 0 &&
1918 (!(new_stp->ls_flags & NFSLCK_CHECK) ||
1919 nfsrv_returnoldstateid)) ||
1920 ((nd->nd_flag & ND_NFSV41) != 0 &&
1921 new_stp->ls_stateid.seqid != 0)))
1922 error = NFSERR_OLDSTATEID;
1927 * Now we can check for grace.
1930 error = nfsrv_checkgrace(nd, clp, new_stp->ls_flags);
1931 if ((new_stp->ls_flags & NFSLCK_RECLAIM) && !error &&
1932 nfsrv_checkstable(clp))
1933 error = NFSERR_NOGRACE;
1935 * If we successfully Reclaimed state, note that.
1937 if ((new_stp->ls_flags & NFSLCK_RECLAIM) && !error)
1938 nfsrv_markstable(clp);
1941 * At this point, either error == NFSERR_BADSTATEID or the
1942 * seqid# has been updated, so we can return any error.
1943 * If error == 0, there may be an error in:
1944 * nd_repstat - Set by the calling function.
1945 * reterr - Set above, if getting the nfslockfile structure
1946 * or acquiring the local lock failed.
1947 * (If both of these are set, nd_repstat should probably be
1948 * returned, since that error was detected before this
1951 if (error != 0 || nd->nd_repstat != 0 || reterr != 0) {
1953 if (nd->nd_repstat != 0)
1954 error = nd->nd_repstat;
1958 if (filestruct_locked != 0) {
1959 /* Roll back local locks. */
1961 if (vnode_unlocked == 0) {
1962 ASSERT_VOP_ELOCKED(vp, "nfsrv_lockctrl2");
1966 nfsrv_locallock_rollback(vp, lfp, p);
1968 nfsrv_unlocklf(lfp);
1975 * Check the nfsrv_getlockfile return.
1976 * Returned -1 if no structure found.
1978 if (getlckret == -1) {
1979 error = NFSERR_EXPIRED;
1981 * Called from lockt, so no lock is OK.
1983 if (new_stp->ls_flags & NFSLCK_TEST) {
1985 } else if (new_stp->ls_flags &
1986 (NFSLCK_CHECK | NFSLCK_SETATTR)) {
1988 * Called to check for a lock, OK if the stateid is all
1989 * 1s or all 0s, but there should be an nfsstate
1991 * (ie. If there is no open, I'll assume no share
1997 error = NFSERR_BADSTATEID;
2004 * For NFSLCK_CHECK and NFSLCK_LOCK, test for a share conflict.
2005 * For NFSLCK_CHECK, allow a read if write access is granted,
2006 * but check for a deny. For NFSLCK_LOCK, require correct access,
2007 * which implies a conflicting deny can't exist.
2009 if (new_stp->ls_flags & (NFSLCK_CHECK | NFSLCK_LOCK)) {
2011 * Four kinds of state id:
2012 * - specialid (all 0s or all 1s), only for NFSLCK_CHECK
2013 * - stateid for an open
2014 * - stateid for a delegation
2015 * - stateid for a lock owner
2018 if (stp->ls_flags & (NFSLCK_DELEGREAD | NFSLCK_DELEGWRITE)) {
2021 nfsrv_delaydelegtimeout(stp);
2022 } else if (stp->ls_flags & NFSLCK_OPEN) {
2025 mystp = stp->ls_openstp;
2028 * If locking or checking, require correct access
2031 if (((new_stp->ls_flags & NFSLCK_LOCK) &&
2032 !((new_lop->lo_flags >> NFSLCK_LOCKSHIFT) &
2033 mystp->ls_flags & NFSLCK_ACCESSBITS)) ||
2034 ((new_stp->ls_flags & (NFSLCK_CHECK|NFSLCK_READACCESS)) ==
2035 (NFSLCK_CHECK | NFSLCK_READACCESS) &&
2036 !(mystp->ls_flags & NFSLCK_READACCESS) &&
2037 nfsrv_allowreadforwriteopen == 0) ||
2038 ((new_stp->ls_flags & (NFSLCK_CHECK|NFSLCK_WRITEACCESS)) ==
2039 (NFSLCK_CHECK | NFSLCK_WRITEACCESS) &&
2040 !(mystp->ls_flags & NFSLCK_WRITEACCESS))) {
2041 if (filestruct_locked != 0) {
2042 /* Roll back local locks. */
2044 if (vnode_unlocked == 0) {
2045 ASSERT_VOP_ELOCKED(vp,
2050 nfsrv_locallock_rollback(vp, lfp, p);
2052 nfsrv_unlocklf(lfp);
2055 error = NFSERR_OPENMODE;
2060 if ((new_stp->ls_flags & NFSLCK_CHECK) && !delegation) {
2062 * Check for a conflicting deny bit.
2064 LIST_FOREACH(tstp, &lfp->lf_open, ls_file) {
2065 if (tstp != mystp) {
2066 bits = tstp->ls_flags;
2067 bits >>= NFSLCK_SHIFT;
2068 if (new_stp->ls_flags & bits & NFSLCK_ACCESSBITS) {
2069 KASSERT(vnode_unlocked == 0,
2070 ("nfsrv_lockctrl: vnode unlocked1"));
2071 ret = nfsrv_clientconflict(tstp->ls_clp, &haslock,
2075 * nfsrv_clientconflict unlocks state
2076 * when it returns non-zero.
2084 error = NFSERR_PERM;
2086 error = NFSERR_OPENMODE;
2092 /* We're outta here */
2099 * For setattr, just get rid of all the Delegations for other clients.
2101 if (new_stp->ls_flags & NFSLCK_SETATTR) {
2102 KASSERT(vnode_unlocked == 0,
2103 ("nfsrv_lockctrl: vnode unlocked2"));
2104 ret = nfsrv_cleandeleg(vp, lfp, clp, &haslock, p);
2107 * nfsrv_cleandeleg() unlocks state when it
2117 if (!(new_stp->ls_flags & NFSLCK_CHECK) ||
2118 (LIST_EMPTY(&lfp->lf_open) && LIST_EMPTY(&lfp->lf_lock) &&
2119 LIST_EMPTY(&lfp->lf_deleg))) {
2126 * Check for a conflicting delegation. If one is found, call
2127 * nfsrv_delegconflict() to handle it. If the v4root lock hasn't
2128 * been set yet, it will get the lock. Otherwise, it will recall
2129 * the delegation. Then, we try try again...
2130 * I currently believe the conflict algorithm to be:
2131 * For Lock Ops (Lock/LockT/LockU)
2132 * - there is a conflict iff a different client has a write delegation
2133 * For Reading (Read Op)
2134 * - there is a conflict iff a different client has a write delegation
2135 * (the specialids are always a different client)
2136 * For Writing (Write/Setattr of size)
2137 * - there is a conflict if a different client has any delegation
2138 * - there is a conflict if the same client has a read delegation
2139 * (I don't understand why this isn't allowed, but that seems to be
2140 * the current consensus?)
2142 tstp = LIST_FIRST(&lfp->lf_deleg);
2143 while (tstp != LIST_END(&lfp->lf_deleg)) {
2144 nstp = LIST_NEXT(tstp, ls_file);
2145 if ((((new_stp->ls_flags&(NFSLCK_LOCK|NFSLCK_UNLOCK|NFSLCK_TEST))||
2146 ((new_stp->ls_flags & NFSLCK_CHECK) &&
2147 (new_lop->lo_flags & NFSLCK_READ))) &&
2148 clp != tstp->ls_clp &&
2149 (tstp->ls_flags & NFSLCK_DELEGWRITE)) ||
2150 ((new_stp->ls_flags & NFSLCK_CHECK) &&
2151 (new_lop->lo_flags & NFSLCK_WRITE) &&
2152 (clp != tstp->ls_clp ||
2153 (tstp->ls_flags & NFSLCK_DELEGREAD)))) {
2155 if (filestruct_locked != 0) {
2156 /* Roll back local locks. */
2158 if (vnode_unlocked == 0) {
2159 ASSERT_VOP_ELOCKED(vp, "nfsrv_lockctrl4");
2162 nfsrv_locallock_rollback(vp, lfp, p);
2164 nfsrv_unlocklf(lfp);
2166 NFSVOPLOCK(vp, LK_EXCLUSIVE | LK_RETRY);
2168 if (VN_IS_DOOMED(vp))
2169 ret = NFSERR_SERVERFAULT;
2173 ret = nfsrv_delegconflict(tstp, &haslock, p, vp);
2176 * nfsrv_delegconflict unlocks state when it
2177 * returns non-zero, which it always does.
2180 free(other_lop, M_NFSDLOCK);
2190 /* Never gets here. */
2196 * Handle the unlock case by calling nfsrv_updatelock().
2197 * (Should I have done some access checking above for unlock? For now,
2198 * just let it happen.)
2200 if (new_stp->ls_flags & NFSLCK_UNLOCK) {
2201 first = new_lop->lo_first;
2202 end = new_lop->lo_end;
2203 nfsrv_updatelock(stp, new_lopp, &other_lop, lfp);
2204 stateidp->seqid = ++(stp->ls_stateid.seqid);
2205 if ((nd->nd_flag & ND_NFSV41) != 0 && stateidp->seqid == 0)
2206 stateidp->seqid = stp->ls_stateid.seqid = 1;
2207 stateidp->other[0] = stp->ls_stateid.other[0];
2208 stateidp->other[1] = stp->ls_stateid.other[1];
2209 stateidp->other[2] = stp->ls_stateid.other[2];
2210 if (filestruct_locked != 0) {
2212 if (vnode_unlocked == 0) {
2213 ASSERT_VOP_ELOCKED(vp, "nfsrv_lockctrl5");
2217 /* Update the local locks. */
2218 nfsrv_localunlock(vp, lfp, first, end, p);
2220 nfsrv_unlocklf(lfp);
2227 * Search for a conflicting lock. A lock conflicts if:
2228 * - the lock range overlaps and
2229 * - at least one lock is a write lock and
2230 * - it is not owned by the same lock owner
2233 LIST_FOREACH(lop, &lfp->lf_lock, lo_lckfile) {
2234 if (new_lop->lo_end > lop->lo_first &&
2235 new_lop->lo_first < lop->lo_end &&
2236 (new_lop->lo_flags == NFSLCK_WRITE ||
2237 lop->lo_flags == NFSLCK_WRITE) &&
2238 lckstp != lop->lo_stp &&
2239 (clp != lop->lo_stp->ls_clp ||
2240 lckstp->ls_ownerlen != lop->lo_stp->ls_ownerlen ||
2241 NFSBCMP(lckstp->ls_owner, lop->lo_stp->ls_owner,
2242 lckstp->ls_ownerlen))) {
2244 free(other_lop, M_NFSDLOCK);
2247 if (vnode_unlocked != 0)
2248 ret = nfsrv_clientconflict(lop->lo_stp->ls_clp, &haslock,
2251 ret = nfsrv_clientconflict(lop->lo_stp->ls_clp, &haslock,
2254 if (filestruct_locked != 0) {
2255 if (vnode_unlocked == 0) {
2256 ASSERT_VOP_ELOCKED(vp, "nfsrv_lockctrl6");
2259 /* Roll back local locks. */
2260 nfsrv_locallock_rollback(vp, lfp, p);
2262 nfsrv_unlocklf(lfp);
2264 NFSVOPLOCK(vp, LK_EXCLUSIVE | LK_RETRY);
2266 if (VN_IS_DOOMED(vp)) {
2267 error = NFSERR_SERVERFAULT;
2272 * nfsrv_clientconflict() unlocks state when it
2279 * Found a conflicting lock, so record the conflict and
2282 if (cfp != NULL && ret == 0) {
2283 cfp->cl_clientid.lval[0]=lop->lo_stp->ls_stateid.other[0];
2284 cfp->cl_clientid.lval[1]=lop->lo_stp->ls_stateid.other[1];
2285 cfp->cl_first = lop->lo_first;
2286 cfp->cl_end = lop->lo_end;
2287 cfp->cl_flags = lop->lo_flags;
2288 cfp->cl_ownerlen = lop->lo_stp->ls_ownerlen;
2289 NFSBCOPY(lop->lo_stp->ls_owner, cfp->cl_owner,
2293 error = NFSERR_PERM;
2294 else if (new_stp->ls_flags & NFSLCK_RECLAIM)
2295 error = NFSERR_RECLAIMCONFLICT;
2296 else if (new_stp->ls_flags & NFSLCK_CHECK)
2297 error = NFSERR_LOCKED;
2299 error = NFSERR_DENIED;
2300 if (filestruct_locked != 0 && ret == 0) {
2301 /* Roll back local locks. */
2303 if (vnode_unlocked == 0) {
2304 ASSERT_VOP_ELOCKED(vp, "nfsrv_lockctrl7");
2308 nfsrv_locallock_rollback(vp, lfp, p);
2310 nfsrv_unlocklf(lfp);
2320 * We only get here if there was no lock that conflicted.
2322 if (new_stp->ls_flags & (NFSLCK_TEST | NFSLCK_CHECK)) {
2328 * We only get here when we are creating or modifying a lock.
2329 * There are two variants:
2330 * - exist_lock_owner where lock_owner exists
2331 * - open_to_lock_owner with new lock_owner
2333 first = new_lop->lo_first;
2334 end = new_lop->lo_end;
2335 lock_flags = new_lop->lo_flags;
2336 if (!(new_stp->ls_flags & NFSLCK_OPENTOLOCK)) {
2337 nfsrv_updatelock(lckstp, new_lopp, &other_lop, lfp);
2338 stateidp->seqid = ++(lckstp->ls_stateid.seqid);
2339 if ((nd->nd_flag & ND_NFSV41) != 0 && stateidp->seqid == 0)
2340 stateidp->seqid = lckstp->ls_stateid.seqid = 1;
2341 stateidp->other[0] = lckstp->ls_stateid.other[0];
2342 stateidp->other[1] = lckstp->ls_stateid.other[1];
2343 stateidp->other[2] = lckstp->ls_stateid.other[2];
2346 * The new open_to_lock_owner case.
2347 * Link the new nfsstate into the lists.
2349 new_stp->ls_seq = new_stp->ls_opentolockseq;
2350 nfsrvd_refcache(new_stp->ls_op);
2351 stateidp->seqid = new_stp->ls_stateid.seqid = 1;
2352 stateidp->other[0] = new_stp->ls_stateid.other[0] =
2353 clp->lc_clientid.lval[0];
2354 stateidp->other[1] = new_stp->ls_stateid.other[1] =
2355 clp->lc_clientid.lval[1];
2356 stateidp->other[2] = new_stp->ls_stateid.other[2] =
2357 nfsrv_nextstateindex(clp);
2358 new_stp->ls_clp = clp;
2359 LIST_INIT(&new_stp->ls_lock);
2360 new_stp->ls_openstp = stp;
2361 new_stp->ls_lfp = lfp;
2362 nfsrv_insertlock(new_lop, (struct nfslock *)new_stp, new_stp,
2364 LIST_INSERT_HEAD(NFSSTATEHASH(clp, new_stp->ls_stateid),
2366 LIST_INSERT_HEAD(&stp->ls_open, new_stp, ls_list);
2369 nfsstatsv1.srvlockowners++;
2370 nfsrv_openpluslock++;
2372 if (filestruct_locked != 0) {
2374 nfsrv_locallock_commit(lfp, lock_flags, first, end);
2376 nfsrv_unlocklf(lfp);
2382 NFSLOCKV4ROOTMUTEX();
2383 nfsv4_unlock(&nfsv4rootfs_lock, 1);
2384 NFSUNLOCKV4ROOTMUTEX();
2386 if (vnode_unlocked != 0) {
2387 NFSVOPLOCK(vp, LK_EXCLUSIVE | LK_RETRY);
2388 if (error == 0 && VN_IS_DOOMED(vp))
2389 error = NFSERR_SERVERFAULT;
2392 free(other_lop, M_NFSDLOCK);
2393 NFSEXITCODE2(error, nd);
2398 * Check for state errors for Open.
2399 * repstat is passed back out as an error if more critical errors
2403 nfsrv_opencheck(nfsquad_t clientid, nfsv4stateid_t *stateidp,
2404 struct nfsstate *new_stp, vnode_t vp, struct nfsrv_descript *nd,
2405 NFSPROC_T *p, int repstat)
2407 struct nfsstate *stp, *nstp;
2408 struct nfsclient *clp;
2409 struct nfsstate *ownerstp;
2410 struct nfslockfile *lfp, *new_lfp;
2411 int error = 0, haslock = 0, ret, readonly = 0, getfhret = 0;
2413 if ((new_stp->ls_flags & NFSLCK_SHAREBITS) == NFSLCK_READACCESS)
2416 * Check for restart conditions (client and server).
2418 error = nfsrv_checkrestart(clientid, new_stp->ls_flags,
2419 &new_stp->ls_stateid, 0);
2424 * Check for state resource limit exceeded.
2425 * Technically this should be SMP protected, but the worst
2426 * case error is "out by one or two" on the count when it
2427 * returns NFSERR_RESOURCE and the limit is just a rather
2428 * arbitrary high water mark, so no harm is done.
2430 if (nfsrv_openpluslock > nfsrv_v4statelimit) {
2431 error = NFSERR_RESOURCE;
2436 new_lfp = malloc(sizeof (struct nfslockfile),
2437 M_NFSDLOCKFILE, M_WAITOK);
2439 getfhret = nfsrv_getlockfh(vp, new_stp->ls_flags, new_lfp,
2443 * Get the nfsclient structure.
2445 error = nfsrv_getclient(clientid, CLOPS_RENEW, &clp, NULL,
2446 (nfsquad_t)((u_quad_t)0), 0, nd, p);
2449 * Look up the open owner. See if it needs confirmation and
2450 * check the seq#, as required.
2453 nfsrv_getowner(&clp->lc_open, new_stp, &ownerstp);
2455 if (!error && ownerstp) {
2456 error = nfsrv_checkseqid(nd, new_stp->ls_seq, ownerstp,
2459 * If the OpenOwner hasn't been confirmed, assume the
2460 * old one was a replay and this one is ok.
2461 * See: RFC3530 Sec. 14.2.18.
2463 if (error == NFSERR_BADSEQID &&
2464 (ownerstp->ls_flags & NFSLCK_NEEDSCONFIRM))
2472 error = nfsrv_checkgrace(nd, clp, new_stp->ls_flags);
2473 if ((new_stp->ls_flags & NFSLCK_RECLAIM) && !error &&
2474 nfsrv_checkstable(clp))
2475 error = NFSERR_NOGRACE;
2478 * If none of the above errors occurred, let repstat be
2481 if (repstat && !error)
2486 NFSLOCKV4ROOTMUTEX();
2487 nfsv4_unlock(&nfsv4rootfs_lock, 1);
2488 NFSUNLOCKV4ROOTMUTEX();
2490 free(new_lfp, M_NFSDLOCKFILE);
2495 * If vp == NULL, the file doesn't exist yet, so return ok.
2496 * (This always happens on the first pass, so haslock must be 0.)
2500 free(new_lfp, M_NFSDLOCKFILE);
2505 * Get the structure for the underlying file.
2510 error = nfsrv_getlockfile(new_stp->ls_flags, &new_lfp, &lfp,
2513 free(new_lfp, M_NFSDLOCKFILE);
2517 NFSLOCKV4ROOTMUTEX();
2518 nfsv4_unlock(&nfsv4rootfs_lock, 1);
2519 NFSUNLOCKV4ROOTMUTEX();
2525 * Search for a conflicting open/share.
2527 if (new_stp->ls_flags & NFSLCK_DELEGCUR) {
2529 * For Delegate_Cur, search for the matching Delegation,
2530 * which indicates no conflict.
2531 * An old delegation should have been recovered by the
2532 * client doing a Claim_DELEGATE_Prev, so I won't let
2533 * it match and return NFSERR_EXPIRED. Should I let it
2536 LIST_FOREACH(stp, &lfp->lf_deleg, ls_file) {
2537 if (!(stp->ls_flags & NFSLCK_OLDDELEG) &&
2538 (((nd->nd_flag & ND_NFSV41) != 0 &&
2539 stateidp->seqid == 0) ||
2540 stateidp->seqid == stp->ls_stateid.seqid) &&
2541 !NFSBCMP(stateidp->other, stp->ls_stateid.other,
2545 if (stp == LIST_END(&lfp->lf_deleg) ||
2546 ((new_stp->ls_flags & NFSLCK_WRITEACCESS) &&
2547 (stp->ls_flags & NFSLCK_DELEGREAD))) {
2550 NFSLOCKV4ROOTMUTEX();
2551 nfsv4_unlock(&nfsv4rootfs_lock, 1);
2552 NFSUNLOCKV4ROOTMUTEX();
2554 error = NFSERR_EXPIRED;
2560 * Check for access/deny bit conflicts. I check for the same
2561 * owner as well, in case the client didn't bother.
2563 LIST_FOREACH(stp, &lfp->lf_open, ls_file) {
2564 if (!(new_stp->ls_flags & NFSLCK_DELEGCUR) &&
2565 (((new_stp->ls_flags & NFSLCK_ACCESSBITS) &
2566 ((stp->ls_flags>>NFSLCK_SHIFT) & NFSLCK_ACCESSBITS))||
2567 ((stp->ls_flags & NFSLCK_ACCESSBITS) &
2568 ((new_stp->ls_flags>>NFSLCK_SHIFT)&NFSLCK_ACCESSBITS)))){
2569 ret = nfsrv_clientconflict(stp->ls_clp,&haslock,vp,p);
2572 * nfsrv_clientconflict() unlocks
2573 * state when it returns non-zero.
2578 error = NFSERR_PERM;
2579 else if (new_stp->ls_flags & NFSLCK_RECLAIM)
2580 error = NFSERR_RECLAIMCONFLICT;
2582 error = NFSERR_SHAREDENIED;
2586 NFSLOCKV4ROOTMUTEX();
2587 nfsv4_unlock(&nfsv4rootfs_lock, 1);
2588 NFSUNLOCKV4ROOTMUTEX();
2595 * Check for a conflicting delegation. If one is found, call
2596 * nfsrv_delegconflict() to handle it. If the v4root lock hasn't
2597 * been set yet, it will get the lock. Otherwise, it will recall
2598 * the delegation. Then, we try try again...
2599 * (If NFSLCK_DELEGCUR is set, it has a delegation, so there
2600 * isn't a conflict.)
2601 * I currently believe the conflict algorithm to be:
2602 * For Open with Read Access and Deny None
2603 * - there is a conflict iff a different client has a write delegation
2604 * For Open with other Write Access or any Deny except None
2605 * - there is a conflict if a different client has any delegation
2606 * - there is a conflict if the same client has a read delegation
2607 * (The current consensus is that this last case should be
2608 * considered a conflict since the client with a read delegation
2609 * could have done an Open with ReadAccess and WriteDeny
2610 * locally and then not have checked for the WriteDeny.)
2611 * Don't check for a Reclaim, since that will be dealt with
2612 * by nfsrv_openctrl().
2614 if (!(new_stp->ls_flags &
2615 (NFSLCK_DELEGPREV | NFSLCK_DELEGCUR | NFSLCK_RECLAIM))) {
2616 stp = LIST_FIRST(&lfp->lf_deleg);
2617 while (stp != LIST_END(&lfp->lf_deleg)) {
2618 nstp = LIST_NEXT(stp, ls_file);
2619 if ((readonly && stp->ls_clp != clp &&
2620 (stp->ls_flags & NFSLCK_DELEGWRITE)) ||
2621 (!readonly && (stp->ls_clp != clp ||
2622 (stp->ls_flags & NFSLCK_DELEGREAD)))) {
2623 ret = nfsrv_delegconflict(stp, &haslock, p, vp);
2626 * nfsrv_delegconflict() unlocks state
2627 * when it returns non-zero.
2640 NFSLOCKV4ROOTMUTEX();
2641 nfsv4_unlock(&nfsv4rootfs_lock, 1);
2642 NFSUNLOCKV4ROOTMUTEX();
2646 NFSEXITCODE2(error, nd);
2651 * Open control function to create/update open state for an open.
2654 nfsrv_openctrl(struct nfsrv_descript *nd, vnode_t vp,
2655 struct nfsstate **new_stpp, nfsquad_t clientid, nfsv4stateid_t *stateidp,
2656 nfsv4stateid_t *delegstateidp, u_int32_t *rflagsp, struct nfsexstuff *exp,
2657 NFSPROC_T *p, u_quad_t filerev)
2659 struct nfsstate *new_stp = *new_stpp;
2660 struct nfsstate *stp, *nstp;
2661 struct nfsstate *openstp = NULL, *new_open, *ownerstp, *new_deleg;
2662 struct nfslockfile *lfp, *new_lfp;
2663 struct nfsclient *clp;
2664 int error = 0, haslock = 0, ret, delegate = 1, writedeleg = 1;
2665 int readonly = 0, cbret = 1, getfhret = 0;
2666 int gotstate = 0, len = 0;
2667 u_char *clidp = NULL;
2669 if ((new_stp->ls_flags & NFSLCK_SHAREBITS) == NFSLCK_READACCESS)
2672 * Check for restart conditions (client and server).
2673 * (Paranoia, should have been detected by nfsrv_opencheck().)
2674 * If an error does show up, return NFSERR_EXPIRED, since the
2675 * the seqid# has already been incremented.
2677 error = nfsrv_checkrestart(clientid, new_stp->ls_flags,
2678 &new_stp->ls_stateid, 0);
2680 printf("Nfsd: openctrl unexpected restart err=%d\n",
2682 error = NFSERR_EXPIRED;
2686 clidp = malloc(NFSV4_OPAQUELIMIT, M_TEMP, M_WAITOK);
2688 new_lfp = malloc(sizeof (struct nfslockfile),
2689 M_NFSDLOCKFILE, M_WAITOK);
2690 new_open = malloc(sizeof (struct nfsstate),
2691 M_NFSDSTATE, M_WAITOK);
2692 new_deleg = malloc(sizeof (struct nfsstate),
2693 M_NFSDSTATE, M_WAITOK);
2694 getfhret = nfsrv_getlockfh(vp, new_stp->ls_flags, new_lfp,
2698 * Get the client structure. Since the linked lists could be changed
2699 * by other nfsd processes if this process does a tsleep(), one of
2700 * two things must be done.
2701 * 1 - don't tsleep()
2703 * 2 - get the nfsv4_lock() { indicated by haslock == 1 }
2704 * before using the lists, since this lock stops the other
2705 * nfsd. This should only be used for rare cases, since it
2706 * essentially single threads the nfsd.
2707 * At this time, it is only done for cases where the stable
2708 * storage file must be written prior to completion of state
2711 error = nfsrv_getclient(clientid, CLOPS_RENEW, &clp, NULL,
2712 (nfsquad_t)((u_quad_t)0), 0, nd, p);
2713 if (!error && (clp->lc_flags & LCL_NEEDSCBNULL) &&
2716 * This happens on the first open for a client
2717 * that supports callbacks.
2721 * Although nfsrv_docallback() will sleep, clp won't
2722 * go away, since they are only removed when the
2723 * nfsv4_lock() has blocked the nfsd threads. The
2724 * fields in clp can change, but having multiple
2725 * threads do this Null callback RPC should be
2728 cbret = nfsrv_docallback(clp, NFSV4PROC_CBNULL,
2729 NULL, 0, NULL, NULL, NULL, 0, p);
2731 clp->lc_flags &= ~LCL_NEEDSCBNULL;
2733 clp->lc_flags |= LCL_CALLBACKSON;
2737 * Look up the open owner. See if it needs confirmation and
2738 * check the seq#, as required.
2741 nfsrv_getowner(&clp->lc_open, new_stp, &ownerstp);
2745 printf("Nfsd: openctrl unexpected state err=%d\n",
2747 free(new_lfp, M_NFSDLOCKFILE);
2748 free(new_open, M_NFSDSTATE);
2749 free(new_deleg, M_NFSDSTATE);
2751 NFSLOCKV4ROOTMUTEX();
2752 nfsv4_unlock(&nfsv4rootfs_lock, 1);
2753 NFSUNLOCKV4ROOTMUTEX();
2755 error = NFSERR_EXPIRED;
2759 if (new_stp->ls_flags & NFSLCK_RECLAIM)
2760 nfsrv_markstable(clp);
2763 * Get the structure for the underlying file.
2768 error = nfsrv_getlockfile(new_stp->ls_flags, &new_lfp, &lfp,
2771 free(new_lfp, M_NFSDLOCKFILE);
2774 printf("Nfsd openctrl unexpected getlockfile err=%d\n",
2776 free(new_open, M_NFSDSTATE);
2777 free(new_deleg, M_NFSDSTATE);
2779 NFSLOCKV4ROOTMUTEX();
2780 nfsv4_unlock(&nfsv4rootfs_lock, 1);
2781 NFSUNLOCKV4ROOTMUTEX();
2787 * Search for a conflicting open/share.
2789 if (new_stp->ls_flags & NFSLCK_DELEGCUR) {
2791 * For Delegate_Cur, search for the matching Delegation,
2792 * which indicates no conflict.
2793 * An old delegation should have been recovered by the
2794 * client doing a Claim_DELEGATE_Prev, so I won't let
2795 * it match and return NFSERR_EXPIRED. Should I let it
2798 LIST_FOREACH(stp, &lfp->lf_deleg, ls_file) {
2799 if (!(stp->ls_flags & NFSLCK_OLDDELEG) &&
2800 (((nd->nd_flag & ND_NFSV41) != 0 &&
2801 stateidp->seqid == 0) ||
2802 stateidp->seqid == stp->ls_stateid.seqid) &&
2803 !NFSBCMP(stateidp->other, stp->ls_stateid.other,
2807 if (stp == LIST_END(&lfp->lf_deleg) ||
2808 ((new_stp->ls_flags & NFSLCK_WRITEACCESS) &&
2809 (stp->ls_flags & NFSLCK_DELEGREAD))) {
2811 printf("Nfsd openctrl unexpected expiry\n");
2812 free(new_open, M_NFSDSTATE);
2813 free(new_deleg, M_NFSDSTATE);
2815 NFSLOCKV4ROOTMUTEX();
2816 nfsv4_unlock(&nfsv4rootfs_lock, 1);
2817 NFSUNLOCKV4ROOTMUTEX();
2819 error = NFSERR_EXPIRED;
2824 * Don't issue a Delegation, since one already exists and
2825 * delay delegation timeout, as required.
2828 nfsrv_delaydelegtimeout(stp);
2832 * Check for access/deny bit conflicts. I also check for the
2833 * same owner, since the client might not have bothered to check.
2834 * Also, note an open for the same file and owner, if found,
2835 * which is all we do here for Delegate_Cur, since conflict
2836 * checking is already done.
2838 LIST_FOREACH(stp, &lfp->lf_open, ls_file) {
2839 if (ownerstp && stp->ls_openowner == ownerstp)
2841 if (!(new_stp->ls_flags & NFSLCK_DELEGCUR)) {
2843 * If another client has the file open, the only
2844 * delegation that can be issued is a Read delegation
2845 * and only if it is a Read open with Deny none.
2847 if (clp != stp->ls_clp) {
2848 if ((stp->ls_flags & NFSLCK_SHAREBITS) ==
2854 if(((new_stp->ls_flags & NFSLCK_ACCESSBITS) &
2855 ((stp->ls_flags>>NFSLCK_SHIFT) & NFSLCK_ACCESSBITS))||
2856 ((stp->ls_flags & NFSLCK_ACCESSBITS) &
2857 ((new_stp->ls_flags>>NFSLCK_SHIFT)&NFSLCK_ACCESSBITS))){
2858 ret = nfsrv_clientconflict(stp->ls_clp,&haslock,vp,p);
2861 * nfsrv_clientconflict() unlocks state
2862 * when it returns non-zero.
2864 free(new_open, M_NFSDSTATE);
2865 free(new_deleg, M_NFSDSTATE);
2870 error = NFSERR_PERM;
2871 else if (new_stp->ls_flags & NFSLCK_RECLAIM)
2872 error = NFSERR_RECLAIMCONFLICT;
2874 error = NFSERR_SHAREDENIED;
2878 NFSLOCKV4ROOTMUTEX();
2879 nfsv4_unlock(&nfsv4rootfs_lock, 1);
2880 NFSUNLOCKV4ROOTMUTEX();
2882 free(new_open, M_NFSDSTATE);
2883 free(new_deleg, M_NFSDSTATE);
2884 printf("nfsd openctrl unexpected client cnfl\n");
2891 * Check for a conflicting delegation. If one is found, call
2892 * nfsrv_delegconflict() to handle it. If the v4root lock hasn't
2893 * been set yet, it will get the lock. Otherwise, it will recall
2894 * the delegation. Then, we try try again...
2895 * (If NFSLCK_DELEGCUR is set, it has a delegation, so there
2896 * isn't a conflict.)
2897 * I currently believe the conflict algorithm to be:
2898 * For Open with Read Access and Deny None
2899 * - there is a conflict iff a different client has a write delegation
2900 * For Open with other Write Access or any Deny except None
2901 * - there is a conflict if a different client has any delegation
2902 * - there is a conflict if the same client has a read delegation
2903 * (The current consensus is that this last case should be
2904 * considered a conflict since the client with a read delegation
2905 * could have done an Open with ReadAccess and WriteDeny
2906 * locally and then not have checked for the WriteDeny.)
2908 if (!(new_stp->ls_flags & (NFSLCK_DELEGPREV | NFSLCK_DELEGCUR))) {
2909 stp = LIST_FIRST(&lfp->lf_deleg);
2910 while (stp != LIST_END(&lfp->lf_deleg)) {
2911 nstp = LIST_NEXT(stp, ls_file);
2912 if (stp->ls_clp != clp && (stp->ls_flags & NFSLCK_DELEGREAD))
2916 if ((readonly && stp->ls_clp != clp &&
2917 (stp->ls_flags & NFSLCK_DELEGWRITE)) ||
2918 (!readonly && (stp->ls_clp != clp ||
2919 (stp->ls_flags & NFSLCK_DELEGREAD)))) {
2920 if (new_stp->ls_flags & NFSLCK_RECLAIM) {
2923 ret = nfsrv_delegconflict(stp, &haslock, p, vp);
2926 * nfsrv_delegconflict() unlocks state
2927 * when it returns non-zero.
2929 printf("Nfsd openctrl unexpected deleg cnfl\n");
2930 free(new_open, M_NFSDSTATE);
2931 free(new_deleg, M_NFSDSTATE);
2946 * We only get here if there was no open that conflicted.
2947 * If an open for the owner exists, or in the access/deny bits.
2948 * Otherwise it is a new open. If the open_owner hasn't been
2949 * confirmed, replace the open with the new one needing confirmation,
2950 * otherwise add the open.
2952 if (new_stp->ls_flags & NFSLCK_DELEGPREV) {
2954 * Handle NFSLCK_DELEGPREV by searching the old delegations for
2955 * a match. If found, just move the old delegation to the current
2956 * delegation list and issue open. If not found, return
2959 LIST_FOREACH(stp, &clp->lc_olddeleg, ls_list) {
2960 if (stp->ls_lfp == lfp) {
2962 if (stp->ls_clp != clp)
2963 panic("olddeleg clp");
2964 LIST_REMOVE(stp, ls_list);
2965 LIST_REMOVE(stp, ls_hash);
2966 stp->ls_flags &= ~NFSLCK_OLDDELEG;
2967 stp->ls_stateid.seqid = delegstateidp->seqid = 1;
2968 stp->ls_stateid.other[0] = delegstateidp->other[0] =
2969 clp->lc_clientid.lval[0];
2970 stp->ls_stateid.other[1] = delegstateidp->other[1] =
2971 clp->lc_clientid.lval[1];
2972 stp->ls_stateid.other[2] = delegstateidp->other[2] =
2973 nfsrv_nextstateindex(clp);
2974 stp->ls_compref = nd->nd_compref;
2975 LIST_INSERT_HEAD(&clp->lc_deleg, stp, ls_list);
2976 LIST_INSERT_HEAD(NFSSTATEHASH(clp,
2977 stp->ls_stateid), stp, ls_hash);
2978 if (stp->ls_flags & NFSLCK_DELEGWRITE)
2979 *rflagsp |= NFSV4OPEN_WRITEDELEGATE;
2981 *rflagsp |= NFSV4OPEN_READDELEGATE;
2982 clp->lc_delegtime = NFSD_MONOSEC +
2983 nfsrv_lease + NFSRV_LEASEDELTA;
2986 * Now, do the associated open.
2988 new_open->ls_stateid.seqid = 1;
2989 new_open->ls_stateid.other[0] = clp->lc_clientid.lval[0];
2990 new_open->ls_stateid.other[1] = clp->lc_clientid.lval[1];
2991 new_open->ls_stateid.other[2] = nfsrv_nextstateindex(clp);
2992 new_open->ls_flags = (new_stp->ls_flags&NFSLCK_DENYBITS)|
2994 if (stp->ls_flags & NFSLCK_DELEGWRITE)
2995 new_open->ls_flags |= (NFSLCK_READACCESS |
2996 NFSLCK_WRITEACCESS);
2998 new_open->ls_flags |= NFSLCK_READACCESS;
2999 new_open->ls_uid = new_stp->ls_uid;
3000 new_open->ls_lfp = lfp;
3001 new_open->ls_clp = clp;
3002 LIST_INIT(&new_open->ls_open);
3003 LIST_INSERT_HEAD(&lfp->lf_open, new_open, ls_file);
3004 LIST_INSERT_HEAD(NFSSTATEHASH(clp, new_open->ls_stateid),
3007 * and handle the open owner
3010 new_open->ls_openowner = ownerstp;
3011 LIST_INSERT_HEAD(&ownerstp->ls_open,new_open,ls_list);
3013 new_open->ls_openowner = new_stp;
3014 new_stp->ls_flags = 0;
3015 nfsrvd_refcache(new_stp->ls_op);
3016 new_stp->ls_noopens = 0;
3017 LIST_INIT(&new_stp->ls_open);
3018 LIST_INSERT_HEAD(&new_stp->ls_open, new_open, ls_list);
3019 LIST_INSERT_HEAD(&clp->lc_open, new_stp, ls_list);
3021 nfsstatsv1.srvopenowners++;
3022 nfsrv_openpluslock++;
3026 nfsstatsv1.srvopens++;
3027 nfsrv_openpluslock++;
3031 if (stp == LIST_END(&clp->lc_olddeleg))
3032 error = NFSERR_EXPIRED;
3033 } else if (new_stp->ls_flags & (NFSLCK_DELEGREAD | NFSLCK_DELEGWRITE)) {
3035 * Scan to see that no delegation for this client and file
3036 * doesn't already exist.
3037 * There also shouldn't yet be an Open for this file and
3040 LIST_FOREACH(stp, &lfp->lf_deleg, ls_file) {
3041 if (stp->ls_clp == clp)
3044 if (stp == LIST_END(&lfp->lf_deleg) && openstp == NULL) {
3046 * This is the Claim_Previous case with a delegation
3047 * type != Delegate_None.
3050 * First, add the delegation. (Although we must issue the
3051 * delegation, we can also ask for an immediate return.)
3053 new_deleg->ls_stateid.seqid = delegstateidp->seqid = 1;
3054 new_deleg->ls_stateid.other[0] = delegstateidp->other[0] =
3055 clp->lc_clientid.lval[0];
3056 new_deleg->ls_stateid.other[1] = delegstateidp->other[1] =
3057 clp->lc_clientid.lval[1];
3058 new_deleg->ls_stateid.other[2] = delegstateidp->other[2] =
3059 nfsrv_nextstateindex(clp);
3060 if (new_stp->ls_flags & NFSLCK_DELEGWRITE) {
3061 new_deleg->ls_flags = (NFSLCK_DELEGWRITE |
3062 NFSLCK_READACCESS | NFSLCK_WRITEACCESS);
3063 *rflagsp |= NFSV4OPEN_WRITEDELEGATE;
3064 nfsrv_writedelegcnt++;
3066 new_deleg->ls_flags = (NFSLCK_DELEGREAD |
3068 *rflagsp |= NFSV4OPEN_READDELEGATE;
3070 new_deleg->ls_uid = new_stp->ls_uid;
3071 new_deleg->ls_lfp = lfp;
3072 new_deleg->ls_clp = clp;
3073 new_deleg->ls_filerev = filerev;
3074 new_deleg->ls_compref = nd->nd_compref;
3075 new_deleg->ls_lastrecall = 0;
3076 LIST_INSERT_HEAD(&lfp->lf_deleg, new_deleg, ls_file);
3077 LIST_INSERT_HEAD(NFSSTATEHASH(clp,
3078 new_deleg->ls_stateid), new_deleg, ls_hash);
3079 LIST_INSERT_HEAD(&clp->lc_deleg, new_deleg, ls_list);
3081 if (delegate == 2 || nfsrv_issuedelegs == 0 ||
3082 (clp->lc_flags & (LCL_CALLBACKSON | LCL_CBDOWN)) !=
3084 NFSRV_V4DELEGLIMIT(nfsrv_delegatecnt) ||
3085 !NFSVNO_DELEGOK(vp))
3086 *rflagsp |= NFSV4OPEN_RECALL;
3087 nfsstatsv1.srvdelegates++;
3088 nfsrv_openpluslock++;
3089 nfsrv_delegatecnt++;
3092 * Now, do the associated open.
3094 new_open->ls_stateid.seqid = 1;
3095 new_open->ls_stateid.other[0] = clp->lc_clientid.lval[0];
3096 new_open->ls_stateid.other[1] = clp->lc_clientid.lval[1];
3097 new_open->ls_stateid.other[2] = nfsrv_nextstateindex(clp);
3098 new_open->ls_flags = (new_stp->ls_flags & NFSLCK_DENYBITS) |
3100 if (new_stp->ls_flags & NFSLCK_DELEGWRITE)
3101 new_open->ls_flags |= (NFSLCK_READACCESS |
3102 NFSLCK_WRITEACCESS);
3104 new_open->ls_flags |= NFSLCK_READACCESS;
3105 new_open->ls_uid = new_stp->ls_uid;
3106 new_open->ls_lfp = lfp;
3107 new_open->ls_clp = clp;
3108 LIST_INIT(&new_open->ls_open);
3109 LIST_INSERT_HEAD(&lfp->lf_open, new_open, ls_file);
3110 LIST_INSERT_HEAD(NFSSTATEHASH(clp, new_open->ls_stateid),
3113 * and handle the open owner
3116 new_open->ls_openowner = ownerstp;
3117 LIST_INSERT_HEAD(&ownerstp->ls_open, new_open, ls_list);
3119 new_open->ls_openowner = new_stp;
3120 new_stp->ls_flags = 0;
3121 nfsrvd_refcache(new_stp->ls_op);
3122 new_stp->ls_noopens = 0;
3123 LIST_INIT(&new_stp->ls_open);
3124 LIST_INSERT_HEAD(&new_stp->ls_open, new_open, ls_list);
3125 LIST_INSERT_HEAD(&clp->lc_open, new_stp, ls_list);
3127 nfsstatsv1.srvopenowners++;
3128 nfsrv_openpluslock++;
3132 nfsstatsv1.srvopens++;
3133 nfsrv_openpluslock++;
3135 error = NFSERR_RECLAIMCONFLICT;
3137 } else if (ownerstp) {
3138 if (ownerstp->ls_flags & NFSLCK_NEEDSCONFIRM) {
3139 /* Replace the open */
3140 if (ownerstp->ls_op)
3141 nfsrvd_derefcache(ownerstp->ls_op);
3142 ownerstp->ls_op = new_stp->ls_op;
3143 nfsrvd_refcache(ownerstp->ls_op);
3144 ownerstp->ls_seq = new_stp->ls_seq;
3145 *rflagsp |= NFSV4OPEN_RESULTCONFIRM;
3146 stp = LIST_FIRST(&ownerstp->ls_open);
3147 stp->ls_flags = (new_stp->ls_flags & NFSLCK_SHAREBITS) |
3149 stp->ls_stateid.seqid = 1;
3150 stp->ls_uid = new_stp->ls_uid;
3151 if (lfp != stp->ls_lfp) {
3152 LIST_REMOVE(stp, ls_file);
3153 LIST_INSERT_HEAD(&lfp->lf_open, stp, ls_file);
3157 } else if (openstp) {
3158 openstp->ls_flags |= (new_stp->ls_flags & NFSLCK_SHAREBITS);
3159 openstp->ls_stateid.seqid++;
3160 if ((nd->nd_flag & ND_NFSV41) != 0 &&
3161 openstp->ls_stateid.seqid == 0)
3162 openstp->ls_stateid.seqid = 1;
3165 * This is where we can choose to issue a delegation.
3167 if ((new_stp->ls_flags & NFSLCK_WANTNODELEG) != 0)
3168 *rflagsp |= NFSV4OPEN_WDNOTWANTED;
3169 else if (nfsrv_issuedelegs == 0)
3170 *rflagsp |= NFSV4OPEN_WDSUPPFTYPE;
3171 else if (NFSRV_V4DELEGLIMIT(nfsrv_delegatecnt))
3172 *rflagsp |= NFSV4OPEN_WDRESOURCE;
3173 else if (delegate == 0 || writedeleg == 0 ||
3174 NFSVNO_EXRDONLY(exp) || (readonly != 0 &&
3175 nfsrv_writedelegifpos == 0) ||
3176 !NFSVNO_DELEGOK(vp) ||
3177 (new_stp->ls_flags & NFSLCK_WANTRDELEG) != 0 ||
3178 (clp->lc_flags & (LCL_CALLBACKSON | LCL_CBDOWN)) !=
3180 *rflagsp |= NFSV4OPEN_WDCONTENTION;
3182 new_deleg->ls_stateid.seqid = delegstateidp->seqid = 1;
3183 new_deleg->ls_stateid.other[0] = delegstateidp->other[0]
3184 = clp->lc_clientid.lval[0];
3185 new_deleg->ls_stateid.other[1] = delegstateidp->other[1]
3186 = clp->lc_clientid.lval[1];
3187 new_deleg->ls_stateid.other[2] = delegstateidp->other[2]
3188 = nfsrv_nextstateindex(clp);
3189 new_deleg->ls_flags = (NFSLCK_DELEGWRITE |
3190 NFSLCK_READACCESS | NFSLCK_WRITEACCESS);
3191 *rflagsp |= NFSV4OPEN_WRITEDELEGATE;
3192 new_deleg->ls_uid = new_stp->ls_uid;
3193 new_deleg->ls_lfp = lfp;
3194 new_deleg->ls_clp = clp;
3195 new_deleg->ls_filerev = filerev;
3196 new_deleg->ls_compref = nd->nd_compref;
3197 new_deleg->ls_lastrecall = 0;
3198 nfsrv_writedelegcnt++;
3199 LIST_INSERT_HEAD(&lfp->lf_deleg, new_deleg, ls_file);
3200 LIST_INSERT_HEAD(NFSSTATEHASH(clp,
3201 new_deleg->ls_stateid), new_deleg, ls_hash);
3202 LIST_INSERT_HEAD(&clp->lc_deleg, new_deleg, ls_list);
3204 nfsstatsv1.srvdelegates++;
3205 nfsrv_openpluslock++;
3206 nfsrv_delegatecnt++;
3209 new_open->ls_stateid.seqid = 1;
3210 new_open->ls_stateid.other[0] = clp->lc_clientid.lval[0];
3211 new_open->ls_stateid.other[1] = clp->lc_clientid.lval[1];
3212 new_open->ls_stateid.other[2] = nfsrv_nextstateindex(clp);
3213 new_open->ls_flags = (new_stp->ls_flags & NFSLCK_SHAREBITS)|
3215 new_open->ls_uid = new_stp->ls_uid;
3216 new_open->ls_openowner = ownerstp;
3217 new_open->ls_lfp = lfp;
3218 new_open->ls_clp = clp;
3219 LIST_INIT(&new_open->ls_open);
3220 LIST_INSERT_HEAD(&lfp->lf_open, new_open, ls_file);
3221 LIST_INSERT_HEAD(&ownerstp->ls_open, new_open, ls_list);
3222 LIST_INSERT_HEAD(NFSSTATEHASH(clp, new_open->ls_stateid),
3226 nfsstatsv1.srvopens++;
3227 nfsrv_openpluslock++;
3230 * This is where we can choose to issue a delegation.
3232 if ((new_stp->ls_flags & NFSLCK_WANTNODELEG) != 0)
3233 *rflagsp |= NFSV4OPEN_WDNOTWANTED;
3234 else if (nfsrv_issuedelegs == 0)
3235 *rflagsp |= NFSV4OPEN_WDSUPPFTYPE;
3236 else if (NFSRV_V4DELEGLIMIT(nfsrv_delegatecnt))
3237 *rflagsp |= NFSV4OPEN_WDRESOURCE;
3238 else if (delegate == 0 || (writedeleg == 0 &&
3239 readonly == 0) || !NFSVNO_DELEGOK(vp) ||
3240 (clp->lc_flags & (LCL_CALLBACKSON | LCL_CBDOWN)) !=
3242 *rflagsp |= NFSV4OPEN_WDCONTENTION;
3244 new_deleg->ls_stateid.seqid = delegstateidp->seqid = 1;
3245 new_deleg->ls_stateid.other[0] = delegstateidp->other[0]
3246 = clp->lc_clientid.lval[0];
3247 new_deleg->ls_stateid.other[1] = delegstateidp->other[1]
3248 = clp->lc_clientid.lval[1];
3249 new_deleg->ls_stateid.other[2] = delegstateidp->other[2]
3250 = nfsrv_nextstateindex(clp);
3251 if (writedeleg && !NFSVNO_EXRDONLY(exp) &&
3252 (nfsrv_writedelegifpos || !readonly) &&
3253 (new_stp->ls_flags & NFSLCK_WANTRDELEG) == 0) {
3254 new_deleg->ls_flags = (NFSLCK_DELEGWRITE |
3255 NFSLCK_READACCESS | NFSLCK_WRITEACCESS);
3256 *rflagsp |= NFSV4OPEN_WRITEDELEGATE;
3257 nfsrv_writedelegcnt++;
3259 new_deleg->ls_flags = (NFSLCK_DELEGREAD |
3261 *rflagsp |= NFSV4OPEN_READDELEGATE;
3263 new_deleg->ls_uid = new_stp->ls_uid;
3264 new_deleg->ls_lfp = lfp;
3265 new_deleg->ls_clp = clp;
3266 new_deleg->ls_filerev = filerev;
3267 new_deleg->ls_compref = nd->nd_compref;
3268 new_deleg->ls_lastrecall = 0;
3269 LIST_INSERT_HEAD(&lfp->lf_deleg, new_deleg, ls_file);
3270 LIST_INSERT_HEAD(NFSSTATEHASH(clp,
3271 new_deleg->ls_stateid), new_deleg, ls_hash);
3272 LIST_INSERT_HEAD(&clp->lc_deleg, new_deleg, ls_list);
3274 nfsstatsv1.srvdelegates++;
3275 nfsrv_openpluslock++;
3276 nfsrv_delegatecnt++;
3281 * New owner case. Start the open_owner sequence with a
3282 * Needs confirmation (unless a reclaim) and hang the
3285 new_open->ls_stateid.seqid = 1;
3286 new_open->ls_stateid.other[0] = clp->lc_clientid.lval[0];
3287 new_open->ls_stateid.other[1] = clp->lc_clientid.lval[1];
3288 new_open->ls_stateid.other[2] = nfsrv_nextstateindex(clp);
3289 new_open->ls_flags = (new_stp->ls_flags & NFSLCK_SHAREBITS) |
3291 new_open->ls_uid = new_stp->ls_uid;
3292 LIST_INIT(&new_open->ls_open);
3293 new_open->ls_openowner = new_stp;
3294 new_open->ls_lfp = lfp;
3295 new_open->ls_clp = clp;
3296 LIST_INSERT_HEAD(&lfp->lf_open, new_open, ls_file);
3297 if (new_stp->ls_flags & NFSLCK_RECLAIM) {
3298 new_stp->ls_flags = 0;
3299 } else if ((nd->nd_flag & ND_NFSV41) != 0) {
3300 /* NFSv4.1 never needs confirmation. */
3301 new_stp->ls_flags = 0;
3304 * This is where we can choose to issue a delegation.
3306 if (delegate && nfsrv_issuedelegs &&
3307 (writedeleg || readonly) &&
3308 (clp->lc_flags & (LCL_CALLBACKSON | LCL_CBDOWN)) ==
3310 !NFSRV_V4DELEGLIMIT(nfsrv_delegatecnt) &&
3311 NFSVNO_DELEGOK(vp) &&
3312 ((nd->nd_flag & ND_NFSV41) == 0 ||
3313 (new_stp->ls_flags & NFSLCK_WANTNODELEG) == 0)) {
3314 new_deleg->ls_stateid.seqid =
3315 delegstateidp->seqid = 1;
3316 new_deleg->ls_stateid.other[0] =
3317 delegstateidp->other[0]
3318 = clp->lc_clientid.lval[0];
3319 new_deleg->ls_stateid.other[1] =
3320 delegstateidp->other[1]
3321 = clp->lc_clientid.lval[1];
3322 new_deleg->ls_stateid.other[2] =
3323 delegstateidp->other[2]
3324 = nfsrv_nextstateindex(clp);
3325 if (writedeleg && !NFSVNO_EXRDONLY(exp) &&
3326 (nfsrv_writedelegifpos || !readonly) &&
3327 ((nd->nd_flag & ND_NFSV41) == 0 ||
3328 (new_stp->ls_flags & NFSLCK_WANTRDELEG) ==
3330 new_deleg->ls_flags =
3331 (NFSLCK_DELEGWRITE |
3333 NFSLCK_WRITEACCESS);
3334 *rflagsp |= NFSV4OPEN_WRITEDELEGATE;
3335 nfsrv_writedelegcnt++;
3337 new_deleg->ls_flags =
3340 *rflagsp |= NFSV4OPEN_READDELEGATE;
3342 new_deleg->ls_uid = new_stp->ls_uid;
3343 new_deleg->ls_lfp = lfp;
3344 new_deleg->ls_clp = clp;
3345 new_deleg->ls_filerev = filerev;
3346 new_deleg->ls_compref = nd->nd_compref;
3347 new_deleg->ls_lastrecall = 0;
3348 LIST_INSERT_HEAD(&lfp->lf_deleg, new_deleg,
3350 LIST_INSERT_HEAD(NFSSTATEHASH(clp,
3351 new_deleg->ls_stateid), new_deleg, ls_hash);
3352 LIST_INSERT_HEAD(&clp->lc_deleg, new_deleg,
3355 nfsstatsv1.srvdelegates++;
3356 nfsrv_openpluslock++;
3357 nfsrv_delegatecnt++;
3360 * Since NFSv4.1 never does an OpenConfirm, the first
3361 * open state will be acquired here.
3363 if (!(clp->lc_flags & LCL_STAMPEDSTABLE)) {
3364 clp->lc_flags |= LCL_STAMPEDSTABLE;
3365 len = clp->lc_idlen;
3366 NFSBCOPY(clp->lc_id, clidp, len);
3370 *rflagsp |= NFSV4OPEN_RESULTCONFIRM;
3371 new_stp->ls_flags = NFSLCK_NEEDSCONFIRM;
3373 nfsrvd_refcache(new_stp->ls_op);
3374 new_stp->ls_noopens = 0;
3375 LIST_INIT(&new_stp->ls_open);
3376 LIST_INSERT_HEAD(&new_stp->ls_open, new_open, ls_list);
3377 LIST_INSERT_HEAD(&clp->lc_open, new_stp, ls_list);
3378 LIST_INSERT_HEAD(NFSSTATEHASH(clp, new_open->ls_stateid),
3383 nfsstatsv1.srvopens++;
3384 nfsrv_openpluslock++;
3385 nfsstatsv1.srvopenowners++;
3386 nfsrv_openpluslock++;
3389 stateidp->seqid = openstp->ls_stateid.seqid;
3390 stateidp->other[0] = openstp->ls_stateid.other[0];
3391 stateidp->other[1] = openstp->ls_stateid.other[1];
3392 stateidp->other[2] = openstp->ls_stateid.other[2];
3396 NFSLOCKV4ROOTMUTEX();
3397 nfsv4_unlock(&nfsv4rootfs_lock, 1);
3398 NFSUNLOCKV4ROOTMUTEX();
3401 free(new_open, M_NFSDSTATE);
3403 free(new_deleg, M_NFSDSTATE);
3406 * If the NFSv4.1 client just acquired its first open, write a timestamp
3407 * to the stable storage file.
3409 if (gotstate != 0) {
3410 nfsrv_writestable(clidp, len, NFSNST_NEWSTATE, p);
3411 nfsrv_backupstable();
3415 free(clidp, M_TEMP);
3416 NFSEXITCODE2(error, nd);
3421 * Open update. Does the confirm, downgrade and close.
3424 nfsrv_openupdate(vnode_t vp, struct nfsstate *new_stp, nfsquad_t clientid,
3425 nfsv4stateid_t *stateidp, struct nfsrv_descript *nd, NFSPROC_T *p,
3426 int *retwriteaccessp)
3428 struct nfsstate *stp;
3429 struct nfsclient *clp;
3430 struct nfslockfile *lfp;
3432 int error = 0, gotstate = 0, len = 0;
3433 u_char *clidp = NULL;
3436 * Check for restart conditions (client and server).
3438 error = nfsrv_checkrestart(clientid, new_stp->ls_flags,
3439 &new_stp->ls_stateid, 0);
3443 clidp = malloc(NFSV4_OPAQUELIMIT, M_TEMP, M_WAITOK);
3446 * Get the open structure via clientid and stateid.
3448 error = nfsrv_getclient(clientid, CLOPS_RENEW, &clp, NULL,
3449 (nfsquad_t)((u_quad_t)0), 0, nd, p);
3451 error = nfsrv_getstate(clp, &new_stp->ls_stateid,
3452 new_stp->ls_flags, &stp);
3455 * Sanity check the open.
3457 if (!error && (!(stp->ls_flags & NFSLCK_OPEN) ||
3458 (!(new_stp->ls_flags & NFSLCK_CONFIRM) &&
3459 (stp->ls_openowner->ls_flags & NFSLCK_NEEDSCONFIRM)) ||
3460 ((new_stp->ls_flags & NFSLCK_CONFIRM) &&
3461 (!(stp->ls_openowner->ls_flags & NFSLCK_NEEDSCONFIRM)))))
3462 error = NFSERR_BADSTATEID;
3465 error = nfsrv_checkseqid(nd, new_stp->ls_seq,
3466 stp->ls_openowner, new_stp->ls_op);
3467 if (!error && stp->ls_stateid.seqid != new_stp->ls_stateid.seqid &&
3468 (((nd->nd_flag & ND_NFSV41) == 0 &&
3469 !(new_stp->ls_flags & NFSLCK_CONFIRM)) ||
3470 ((nd->nd_flag & ND_NFSV41) != 0 &&
3471 new_stp->ls_stateid.seqid != 0)))
3472 error = NFSERR_OLDSTATEID;
3473 if (!error && vnode_vtype(vp) != VREG) {
3474 if (vnode_vtype(vp) == VDIR)
3475 error = NFSERR_ISDIR;
3477 error = NFSERR_INVAL;
3482 * If a client tries to confirm an Open with a bad
3483 * seqid# and there are no byte range locks or other Opens
3484 * on the openowner, just throw it away, so the next use of the
3485 * openowner will start a fresh seq#.
3487 if (error == NFSERR_BADSEQID &&
3488 (new_stp->ls_flags & NFSLCK_CONFIRM) &&
3489 nfsrv_nootherstate(stp))
3490 nfsrv_freeopenowner(stp->ls_openowner, 0, p);
3496 * Set the return stateid.
3498 stateidp->seqid = stp->ls_stateid.seqid + 1;
3499 if ((nd->nd_flag & ND_NFSV41) != 0 && stateidp->seqid == 0)
3500 stateidp->seqid = 1;
3501 stateidp->other[0] = stp->ls_stateid.other[0];
3502 stateidp->other[1] = stp->ls_stateid.other[1];
3503 stateidp->other[2] = stp->ls_stateid.other[2];
3505 * Now, handle the three cases.
3507 if (new_stp->ls_flags & NFSLCK_CONFIRM) {
3509 * If the open doesn't need confirmation, it seems to me that
3510 * there is a client error, but I'll just log it and keep going?
3512 if (!(stp->ls_openowner->ls_flags & NFSLCK_NEEDSCONFIRM))
3513 printf("Nfsv4d: stray open confirm\n");
3514 stp->ls_openowner->ls_flags = 0;
3515 stp->ls_stateid.seqid++;
3516 if ((nd->nd_flag & ND_NFSV41) != 0 &&
3517 stp->ls_stateid.seqid == 0)
3518 stp->ls_stateid.seqid = 1;
3519 if (!(clp->lc_flags & LCL_STAMPEDSTABLE)) {
3520 clp->lc_flags |= LCL_STAMPEDSTABLE;
3521 len = clp->lc_idlen;
3522 NFSBCOPY(clp->lc_id, clidp, len);
3526 } else if (new_stp->ls_flags & NFSLCK_CLOSE) {
3528 if (retwriteaccessp != NULL) {
3529 if ((stp->ls_flags & NFSLCK_WRITEACCESS) != 0)
3530 *retwriteaccessp = 1;
3532 *retwriteaccessp = 0;
3534 if (nfsrv_dolocallocks != 0 && !LIST_EMPTY(&stp->ls_open)) {
3535 /* Get the lf lock */
3538 ASSERT_VOP_ELOCKED(vp, "nfsrv_openupdate");
3540 if (nfsrv_freeopen(stp, vp, 1, p) == 0) {
3542 nfsrv_unlocklf(lfp);
3545 NFSVOPLOCK(vp, LK_EXCLUSIVE | LK_RETRY);
3547 (void) nfsrv_freeopen(stp, NULL, 0, p);
3552 * Update the share bits, making sure that the new set are a
3553 * subset of the old ones.
3555 bits = (new_stp->ls_flags & NFSLCK_SHAREBITS);
3556 if (~(stp->ls_flags) & bits) {
3558 error = NFSERR_INVAL;
3561 stp->ls_flags = (bits | NFSLCK_OPEN);
3562 stp->ls_stateid.seqid++;
3563 if ((nd->nd_flag & ND_NFSV41) != 0 &&
3564 stp->ls_stateid.seqid == 0)
3565 stp->ls_stateid.seqid = 1;
3570 * If the client just confirmed its first open, write a timestamp
3571 * to the stable storage file.
3573 if (gotstate != 0) {
3574 nfsrv_writestable(clidp, len, NFSNST_NEWSTATE, p);
3575 nfsrv_backupstable();
3579 free(clidp, M_TEMP);
3580 NFSEXITCODE2(error, nd);
3585 * Delegation update. Does the purge and return.
3588 nfsrv_delegupdate(struct nfsrv_descript *nd, nfsquad_t clientid,
3589 nfsv4stateid_t *stateidp, vnode_t vp, int op, struct ucred *cred,
3590 NFSPROC_T *p, int *retwriteaccessp)
3592 struct nfsstate *stp;
3593 struct nfsclient *clp;
3598 * Do a sanity check against the file handle for DelegReturn.
3601 error = nfsvno_getfh(vp, &fh, p);
3606 * Check for restart conditions (client and server).
3608 if (op == NFSV4OP_DELEGRETURN)
3609 error = nfsrv_checkrestart(clientid, NFSLCK_DELEGRETURN,
3612 error = nfsrv_checkrestart(clientid, NFSLCK_DELEGPURGE,
3617 * Get the open structure via clientid and stateid.
3620 error = nfsrv_getclient(clientid, CLOPS_RENEW, &clp, NULL,
3621 (nfsquad_t)((u_quad_t)0), 0, nd, p);
3623 if (error == NFSERR_CBPATHDOWN)
3625 if (error == NFSERR_STALECLIENTID && op == NFSV4OP_DELEGRETURN)
3626 error = NFSERR_STALESTATEID;
3628 if (!error && op == NFSV4OP_DELEGRETURN) {
3629 error = nfsrv_getstate(clp, stateidp, NFSLCK_DELEGRETURN, &stp);
3630 if (!error && stp->ls_stateid.seqid != stateidp->seqid &&
3631 ((nd->nd_flag & ND_NFSV41) == 0 || stateidp->seqid != 0))
3632 error = NFSERR_OLDSTATEID;
3635 * NFSERR_EXPIRED means that the state has gone away,
3636 * so Delegations have been purged. Just return ok.
3638 if (error == NFSERR_EXPIRED && op == NFSV4OP_DELEGPURGE) {
3648 if (op == NFSV4OP_DELEGRETURN) {
3649 if (NFSBCMP((caddr_t)&fh, (caddr_t)&stp->ls_lfp->lf_fh,
3650 sizeof (fhandle_t))) {
3652 error = NFSERR_BADSTATEID;
3655 if (retwriteaccessp != NULL) {
3656 if ((stp->ls_flags & NFSLCK_DELEGWRITE) != 0)
3657 *retwriteaccessp = 1;
3659 *retwriteaccessp = 0;
3661 nfsrv_freedeleg(stp);
3663 nfsrv_freedeleglist(&clp->lc_olddeleg);
3674 * Release lock owner.
3677 nfsrv_releaselckown(struct nfsstate *new_stp, nfsquad_t clientid,
3680 struct nfsstate *stp, *nstp, *openstp, *ownstp;
3681 struct nfsclient *clp;
3685 * Check for restart conditions (client and server).
3687 error = nfsrv_checkrestart(clientid, new_stp->ls_flags,
3688 &new_stp->ls_stateid, 0);
3694 * Get the lock owner by name.
3696 error = nfsrv_getclient(clientid, CLOPS_RENEW, &clp, NULL,
3697 (nfsquad_t)((u_quad_t)0), 0, NULL, p);
3702 LIST_FOREACH(ownstp, &clp->lc_open, ls_list) {
3703 LIST_FOREACH(openstp, &ownstp->ls_open, ls_list) {
3704 stp = LIST_FIRST(&openstp->ls_open);
3705 while (stp != LIST_END(&openstp->ls_open)) {
3706 nstp = LIST_NEXT(stp, ls_list);
3708 * If the owner matches, check for locks and
3709 * then free or return an error.
3711 if (stp->ls_ownerlen == new_stp->ls_ownerlen &&
3712 !NFSBCMP(stp->ls_owner, new_stp->ls_owner,
3714 if (LIST_EMPTY(&stp->ls_lock)) {
3715 nfsrv_freelockowner(stp, NULL, 0, p);
3718 error = NFSERR_LOCKSHELD;
3734 * Get the file handle for a lock structure.
3737 nfsrv_getlockfh(vnode_t vp, u_short flags, struct nfslockfile *new_lfp,
3738 fhandle_t *nfhp, NFSPROC_T *p)
3740 fhandle_t *fhp = NULL;
3744 * For lock, use the new nfslock structure, otherwise just
3745 * a fhandle_t on the stack.
3747 if (flags & NFSLCK_OPEN) {
3748 KASSERT(new_lfp != NULL, ("nfsrv_getlockfh: new_lfp NULL"));
3749 fhp = &new_lfp->lf_fh;
3753 panic("nfsrv_getlockfh");
3755 error = nfsvno_getfh(vp, fhp, p);
3761 * Get an nfs lock structure. Allocate one, as required, and return a
3763 * Returns an NFSERR_xxx upon failure or -1 to indicate no current lock.
3766 nfsrv_getlockfile(u_short flags, struct nfslockfile **new_lfpp,
3767 struct nfslockfile **lfpp, fhandle_t *nfhp, int lockit)
3769 struct nfslockfile *lfp;
3770 fhandle_t *fhp = NULL, *tfhp;
3771 struct nfslockhashhead *hp;
3772 struct nfslockfile *new_lfp = NULL;
3775 * For lock, use the new nfslock structure, otherwise just
3776 * a fhandle_t on the stack.
3778 if (flags & NFSLCK_OPEN) {
3779 new_lfp = *new_lfpp;
3780 fhp = &new_lfp->lf_fh;
3784 panic("nfsrv_getlockfile");
3787 hp = NFSLOCKHASH(fhp);
3788 LIST_FOREACH(lfp, hp, lf_hash) {
3790 if (NFSVNO_CMPFH(fhp, tfhp)) {
3797 if (!(flags & NFSLCK_OPEN))
3801 * No match, so chain the new one into the list.
3803 LIST_INIT(&new_lfp->lf_open);
3804 LIST_INIT(&new_lfp->lf_lock);
3805 LIST_INIT(&new_lfp->lf_deleg);
3806 LIST_INIT(&new_lfp->lf_locallock);
3807 LIST_INIT(&new_lfp->lf_rollback);
3808 new_lfp->lf_locallock_lck.nfslock_usecnt = 0;
3809 new_lfp->lf_locallock_lck.nfslock_lock = 0;
3810 new_lfp->lf_usecount = 0;
3811 LIST_INSERT_HEAD(hp, new_lfp, lf_hash);
3818 * This function adds a nfslock lock structure to the list for the associated
3819 * nfsstate and nfslockfile structures. It will be inserted after the
3820 * entry pointed at by insert_lop.
3823 nfsrv_insertlock(struct nfslock *new_lop, struct nfslock *insert_lop,
3824 struct nfsstate *stp, struct nfslockfile *lfp)
3826 struct nfslock *lop, *nlop;
3828 new_lop->lo_stp = stp;
3829 new_lop->lo_lfp = lfp;
3832 /* Insert in increasing lo_first order */
3833 lop = LIST_FIRST(&lfp->lf_lock);
3834 if (lop == LIST_END(&lfp->lf_lock) ||
3835 new_lop->lo_first <= lop->lo_first) {
3836 LIST_INSERT_HEAD(&lfp->lf_lock, new_lop, lo_lckfile);
3838 nlop = LIST_NEXT(lop, lo_lckfile);
3839 while (nlop != LIST_END(&lfp->lf_lock) &&
3840 nlop->lo_first < new_lop->lo_first) {
3842 nlop = LIST_NEXT(lop, lo_lckfile);
3844 LIST_INSERT_AFTER(lop, new_lop, lo_lckfile);
3847 new_lop->lo_lckfile.le_prev = NULL; /* list not used */
3851 * Insert after insert_lop, which is overloaded as stp or lfp for
3854 if (stp == NULL && (struct nfslockfile *)insert_lop == lfp)
3855 LIST_INSERT_HEAD(&lfp->lf_locallock, new_lop, lo_lckowner);
3856 else if ((struct nfsstate *)insert_lop == stp)
3857 LIST_INSERT_HEAD(&stp->ls_lock, new_lop, lo_lckowner);
3859 LIST_INSERT_AFTER(insert_lop, new_lop, lo_lckowner);
3861 nfsstatsv1.srvlocks++;
3862 nfsrv_openpluslock++;
3867 * This function updates the locking for a lock owner and given file. It
3868 * maintains a list of lock ranges ordered on increasing file offset that
3869 * are NFSLCK_READ or NFSLCK_WRITE and non-overlapping (aka POSIX style).
3870 * It always adds new_lop to the list and sometimes uses the one pointed
3874 nfsrv_updatelock(struct nfsstate *stp, struct nfslock **new_lopp,
3875 struct nfslock **other_lopp, struct nfslockfile *lfp)
3877 struct nfslock *new_lop = *new_lopp;
3878 struct nfslock *lop, *tlop, *ilop;
3879 struct nfslock *other_lop = *other_lopp;
3880 int unlock = 0, myfile = 0;
3884 * Work down the list until the lock is merged.
3886 if (new_lop->lo_flags & NFSLCK_UNLOCK)
3889 ilop = (struct nfslock *)stp;
3890 lop = LIST_FIRST(&stp->ls_lock);
3892 ilop = (struct nfslock *)lfp;
3893 lop = LIST_FIRST(&lfp->lf_locallock);
3895 while (lop != NULL) {
3897 * Only check locks for this file that aren't before the start of
3900 if (lop->lo_lfp == lfp) {
3902 if (lop->lo_end >= new_lop->lo_first) {
3903 if (new_lop->lo_end < lop->lo_first) {
3905 * If the new lock ends before the start of the
3906 * current lock's range, no merge, just insert
3911 if (new_lop->lo_flags == lop->lo_flags ||
3912 (new_lop->lo_first <= lop->lo_first &&
3913 new_lop->lo_end >= lop->lo_end)) {
3915 * This lock can be absorbed by the new lock/unlock.
3916 * This happens when it covers the entire range
3917 * of the old lock or is contiguous
3918 * with the old lock and is of the same type or an
3921 if (lop->lo_first < new_lop->lo_first)
3922 new_lop->lo_first = lop->lo_first;
3923 if (lop->lo_end > new_lop->lo_end)
3924 new_lop->lo_end = lop->lo_end;
3926 lop = LIST_NEXT(lop, lo_lckowner);
3927 nfsrv_freenfslock(tlop);
3932 * All these cases are for contiguous locks that are not the
3933 * same type, so they can't be merged.
3935 if (new_lop->lo_first <= lop->lo_first) {
3937 * This case is where the new lock overlaps with the
3938 * first part of the old lock. Move the start of the
3939 * old lock to just past the end of the new lock. The
3940 * new lock will be inserted in front of the old, since
3941 * ilop hasn't been updated. (We are done now.)
3943 lop->lo_first = new_lop->lo_end;
3946 if (new_lop->lo_end >= lop->lo_end) {
3948 * This case is where the new lock overlaps with the
3949 * end of the old lock's range. Move the old lock's
3950 * end to just before the new lock's first and insert
3951 * the new lock after the old lock.
3952 * Might not be done yet, since the new lock could
3953 * overlap further locks with higher ranges.
3955 lop->lo_end = new_lop->lo_first;
3957 lop = LIST_NEXT(lop, lo_lckowner);
3961 * The final case is where the new lock's range is in the
3962 * middle of the current lock's and splits the current lock
3963 * up. Use *other_lopp to handle the second part of the
3964 * split old lock range. (We are done now.)
3965 * For unlock, we use new_lop as other_lop and tmp, since
3966 * other_lop and new_lop are the same for this case.
3967 * We noted the unlock case above, so we don't need
3968 * new_lop->lo_flags any longer.
3970 tmp = new_lop->lo_first;
3971 if (other_lop == NULL) {
3973 panic("nfsd srv update unlock");
3974 other_lop = new_lop;
3977 other_lop->lo_first = new_lop->lo_end;
3978 other_lop->lo_end = lop->lo_end;
3979 other_lop->lo_flags = lop->lo_flags;
3980 other_lop->lo_stp = stp;
3981 other_lop->lo_lfp = lfp;
3983 nfsrv_insertlock(other_lop, lop, stp, lfp);
3990 lop = LIST_NEXT(lop, lo_lckowner);
3991 if (myfile && (lop == NULL || lop->lo_lfp != lfp))
3996 * Insert the new lock in the list at the appropriate place.
3999 nfsrv_insertlock(new_lop, ilop, stp, lfp);
4005 * This function handles sequencing of locks, etc.
4006 * It returns an error that indicates what the caller should do.
4009 nfsrv_checkseqid(struct nfsrv_descript *nd, u_int32_t seqid,
4010 struct nfsstate *stp, struct nfsrvcache *op)
4014 if ((nd->nd_flag & ND_NFSV41) != 0)
4015 /* NFSv4.1 ignores the open_seqid and lock_seqid. */
4017 if (op != nd->nd_rp)
4018 panic("nfsrvstate checkseqid");
4019 if (!(op->rc_flag & RC_INPROG))
4020 panic("nfsrvstate not inprog");
4021 if (stp->ls_op && stp->ls_op->rc_refcnt <= 0) {
4022 printf("refcnt=%d\n", stp->ls_op->rc_refcnt);
4023 panic("nfsrvstate op refcnt");
4026 /* If ND_ERELOOKUP is set, the seqid has already been handled. */
4027 if ((nd->nd_flag & ND_ERELOOKUP) != 0)
4030 if ((stp->ls_seq + 1) == seqid) {
4032 nfsrvd_derefcache(stp->ls_op);
4034 nfsrvd_refcache(op);
4035 stp->ls_seq = seqid;
4037 } else if (stp->ls_seq == seqid && stp->ls_op &&
4038 op->rc_xid == stp->ls_op->rc_xid &&
4039 op->rc_refcnt == 0 &&
4040 op->rc_reqlen == stp->ls_op->rc_reqlen &&
4041 op->rc_cksum == stp->ls_op->rc_cksum) {
4042 if (stp->ls_op->rc_flag & RC_INPROG) {
4043 error = NFSERR_DONTREPLY;
4046 nd->nd_rp = stp->ls_op;
4047 nd->nd_rp->rc_flag |= RC_INPROG;
4048 nfsrvd_delcache(op);
4049 error = NFSERR_REPLYFROMCACHE;
4052 error = NFSERR_BADSEQID;
4055 NFSEXITCODE2(error, nd);
4060 * Get the client ip address for callbacks. If the strings can't be parsed,
4061 * just set lc_program to 0 to indicate no callbacks are possible.
4062 * (For cases where the address can't be parsed or is 0.0.0.0.0.0, set
4063 * the address to the client's transport address. This won't be used
4064 * for callbacks, but can be printed out by nfsstats for info.)
4065 * Return error if the xdr can't be parsed, 0 otherwise.
4068 nfsrv_getclientipaddr(struct nfsrv_descript *nd, struct nfsclient *clp)
4072 int i, j, maxalen = 0, minalen = 0;
4075 struct sockaddr_in *rin = NULL, *sin;
4078 struct sockaddr_in6 *rin6 = NULL, *sin6;
4081 int error = 0, cantparse = 0;
4091 /* 8 is the maximum length of the port# string. */
4092 addr = malloc(INET6_ADDRSTRLEN + 8, M_TEMP, M_WAITOK);
4093 clp->lc_req.nr_client = NULL;
4094 clp->lc_req.nr_lock = 0;
4096 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
4097 i = fxdr_unsigned(int, *tl);
4098 if (i >= 3 && i <= 4) {
4099 error = nfsrv_mtostr(nd, addr, i);
4103 if (!strcmp(addr, "tcp")) {
4104 clp->lc_flags |= LCL_TCPCALLBACK;
4105 clp->lc_req.nr_sotype = SOCK_STREAM;
4106 clp->lc_req.nr_soproto = IPPROTO_TCP;
4108 } else if (!strcmp(addr, "udp")) {
4109 clp->lc_req.nr_sotype = SOCK_DGRAM;
4110 clp->lc_req.nr_soproto = IPPROTO_UDP;
4115 if (af == AF_UNSPEC) {
4116 if (!strcmp(addr, "tcp6")) {
4117 clp->lc_flags |= LCL_TCPCALLBACK;
4118 clp->lc_req.nr_sotype = SOCK_STREAM;
4119 clp->lc_req.nr_soproto = IPPROTO_TCP;
4121 } else if (!strcmp(addr, "udp6")) {
4122 clp->lc_req.nr_sotype = SOCK_DGRAM;
4123 clp->lc_req.nr_soproto = IPPROTO_UDP;
4128 if (af == AF_UNSPEC) {
4134 error = nfsm_advance(nd, NFSM_RNDUP(i), -1);
4140 * The caller has allocated clp->lc_req.nr_nam to be large enough
4141 * for either AF_INET or AF_INET6 and zeroed out the contents.
4142 * maxalen is set to the maximum length of the host IP address string
4143 * plus 8 for the maximum length of the port#.
4144 * minalen is set to the minimum length of the host IP address string
4145 * plus 4 for the minimum length of the port#.
4146 * These lengths do not include NULL termination,
4147 * so INET[6]_ADDRSTRLEN - 1 is used in the calculations.
4152 rin = (struct sockaddr_in *)clp->lc_req.nr_nam;
4153 rin->sin_family = AF_INET;
4154 rin->sin_len = sizeof(struct sockaddr_in);
4155 maxalen = INET_ADDRSTRLEN - 1 + 8;
4161 rin6 = (struct sockaddr_in6 *)clp->lc_req.nr_nam;
4162 rin6->sin6_family = AF_INET6;
4163 rin6->sin6_len = sizeof(struct sockaddr_in6);
4164 maxalen = INET6_ADDRSTRLEN - 1 + 8;
4169 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
4170 i = fxdr_unsigned(int, *tl);
4172 error = NFSERR_BADXDR;
4174 } else if (i == 0) {
4176 } else if (!cantparse && i <= maxalen && i >= minalen) {
4177 error = nfsrv_mtostr(nd, addr, i);
4182 * Parse out the address fields. We expect 6 decimal numbers
4183 * separated by '.'s for AF_INET and two decimal numbers
4184 * preceeded by '.'s for AF_INET6.
4190 * For AF_INET6, first parse the host address.
4193 cp = strchr(addr, '.');
4196 if (inet_pton(af, addr, &rin6->sin6_addr) == 1)
4212 while (cp != NULL && *cp && i < 6) {
4214 while (*cp2 && *cp2 != '.')
4222 j = nfsrv_getipnumber(cp);
4227 port.cval[5 - i] = j;
4237 * The host address INADDR_ANY is (mis)used to indicate
4238 * "there is no valid callback address".
4243 if (!IN6_ARE_ADDR_EQUAL(&rin6->sin6_addr,
4245 rin6->sin6_port = htons(port.sval);
4252 if (ip.ival != INADDR_ANY) {
4253 rin->sin_addr.s_addr = htonl(ip.ival);
4254 rin->sin_port = htons(port.sval);
4265 error = nfsm_advance(nd, NFSM_RNDUP(i), -1);
4271 switch (nd->nd_nam->sa_family) {
4274 sin = (struct sockaddr_in *)nd->nd_nam;
4275 rin = (struct sockaddr_in *)clp->lc_req.nr_nam;
4276 rin->sin_family = AF_INET;
4277 rin->sin_len = sizeof(struct sockaddr_in);
4278 rin->sin_addr.s_addr = sin->sin_addr.s_addr;
4279 rin->sin_port = 0x0;
4284 sin6 = (struct sockaddr_in6 *)nd->nd_nam;
4285 rin6 = (struct sockaddr_in6 *)clp->lc_req.nr_nam;
4286 rin6->sin6_family = AF_INET6;
4287 rin6->sin6_len = sizeof(struct sockaddr_in6);
4288 rin6->sin6_addr = sin6->sin6_addr;
4289 rin6->sin6_port = 0x0;
4293 clp->lc_program = 0;
4297 NFSEXITCODE2(error, nd);
4302 * Turn a string of up to three decimal digits into a number. Return -1 upon
4306 nfsrv_getipnumber(u_char *cp)
4311 if (j > 2 || *cp < '0' || *cp > '9')
4324 * This function checks for restart conditions.
4327 nfsrv_checkrestart(nfsquad_t clientid, u_int32_t flags,
4328 nfsv4stateid_t *stateidp, int specialid)
4333 * First check for a server restart. Open, LockT, ReleaseLockOwner
4334 * and DelegPurge have a clientid, the rest a stateid.
4337 (NFSLCK_OPEN | NFSLCK_TEST | NFSLCK_RELEASE | NFSLCK_DELEGPURGE)) {
4338 if (clientid.lval[0] != nfsrvboottime) {
4339 ret = NFSERR_STALECLIENTID;
4342 } else if (stateidp->other[0] != nfsrvboottime &&
4344 ret = NFSERR_STALESTATEID;
4349 * Read, Write, Setattr and LockT can return NFSERR_GRACE and do
4350 * not use a lock/open owner seqid#, so the check can be done now.
4351 * (The others will be checked, as required, later.)
4353 if (!(flags & (NFSLCK_CHECK | NFSLCK_TEST)))
4357 ret = nfsrv_checkgrace(NULL, NULL, flags);
4369 nfsrv_checkgrace(struct nfsrv_descript *nd, struct nfsclient *clp,
4372 int error = 0, notreclaimed;
4373 struct nfsrv_stable *sp;
4375 if ((nfsrv_stablefirst.nsf_flags & (NFSNSF_UPDATEDONE |
4376 NFSNSF_GRACEOVER)) == 0) {
4378 * First, check to see if all of the clients have done a
4379 * ReclaimComplete. If so, grace can end now.
4382 LIST_FOREACH(sp, &nfsrv_stablefirst.nsf_head, nst_list) {
4383 if ((sp->nst_flag & NFSNST_RECLAIMED) == 0) {
4388 if (notreclaimed == 0)
4389 nfsrv_stablefirst.nsf_flags |= (NFSNSF_GRACEOVER |
4393 if ((nfsrv_stablefirst.nsf_flags & NFSNSF_GRACEOVER) != 0) {
4394 if (flags & NFSLCK_RECLAIM) {
4395 error = NFSERR_NOGRACE;
4399 if (!(flags & NFSLCK_RECLAIM)) {
4400 error = NFSERR_GRACE;
4403 if (nd != NULL && clp != NULL &&
4404 (nd->nd_flag & ND_NFSV41) != 0 &&
4405 (clp->lc_flags & LCL_RECLAIMCOMPLETE) != 0) {
4406 error = NFSERR_NOGRACE;
4411 * If grace is almost over and we are still getting Reclaims,
4412 * extend grace a bit.
4414 if ((NFSD_MONOSEC + NFSRV_LEASEDELTA) >
4415 nfsrv_stablefirst.nsf_eograce)
4416 nfsrv_stablefirst.nsf_eograce = NFSD_MONOSEC +
4426 * Do a server callback.
4427 * The "trunc" argument is slightly overloaded and refers to different
4428 * boolean arguments for CBRECALL and CBLAYOUTRECALL.
4431 nfsrv_docallback(struct nfsclient *clp, int procnum, nfsv4stateid_t *stateidp,
4432 int trunc, fhandle_t *fhp, struct nfsvattr *nap, nfsattrbit_t *attrbitp,
4433 int laytype, NFSPROC_T *p)
4437 struct nfsrv_descript *nd;
4439 int error = 0, slotpos;
4441 struct nfsdsession *sep = NULL;
4445 nd = malloc(sizeof(*nd), M_TEMP, M_WAITOK | M_ZERO);
4446 cred = newnfs_getcred();
4447 NFSLOCKSTATE(); /* mostly for lc_cbref++ */
4448 if (clp->lc_flags & LCL_NEEDSCONFIRM) {
4455 * Fill the callback program# and version into the request
4456 * structure for newnfs_connect() to use.
4458 clp->lc_req.nr_prog = clp->lc_program;
4460 if ((clp->lc_flags & LCL_NFSV41) != 0)
4461 clp->lc_req.nr_vers = NFSV41_CBVERS;
4464 clp->lc_req.nr_vers = NFSV4_CBVERS;
4467 * First, fill in some of the fields of nd and cr.
4469 nd->nd_flag = ND_NFSV4;
4470 if (clp->lc_flags & LCL_GSS)
4471 nd->nd_flag |= ND_KERBV;
4472 if ((clp->lc_flags & LCL_NFSV41) != 0)
4473 nd->nd_flag |= ND_NFSV41;
4474 if ((clp->lc_flags & LCL_NFSV42) != 0)
4475 nd->nd_flag |= ND_NFSV42;
4477 cred->cr_uid = clp->lc_uid;
4478 cred->cr_gid = clp->lc_gid;
4479 callback = clp->lc_callback;
4481 cred->cr_ngroups = 1;
4484 * Get the first mbuf for the request.
4486 MGET(m, M_WAITOK, MT_DATA);
4488 nd->nd_mreq = nd->nd_mb = m;
4489 nd->nd_bpos = mtod(m, caddr_t);
4492 * and build the callback request.
4494 if (procnum == NFSV4OP_CBGETATTR) {
4495 nd->nd_procnum = NFSV4PROC_CBCOMPOUND;
4496 error = nfsrv_cbcallargs(nd, clp, callback, NFSV4OP_CBGETATTR,
4497 "CB Getattr", &sep, &slotpos);
4499 m_freem(nd->nd_mreq);
4502 (void)nfsm_fhtom(nd, (u_int8_t *)fhp, NFSX_MYFH, 0);
4503 (void)nfsrv_putattrbit(nd, attrbitp);
4504 } else if (procnum == NFSV4OP_CBRECALL) {
4505 nd->nd_procnum = NFSV4PROC_CBCOMPOUND;
4506 error = nfsrv_cbcallargs(nd, clp, callback, NFSV4OP_CBRECALL,
4507 "CB Recall", &sep, &slotpos);
4509 m_freem(nd->nd_mreq);
4512 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED + NFSX_STATEID);
4513 *tl++ = txdr_unsigned(stateidp->seqid);
4514 NFSBCOPY((caddr_t)stateidp->other, (caddr_t)tl,
4516 tl += (NFSX_STATEIDOTHER / NFSX_UNSIGNED);
4521 (void)nfsm_fhtom(nd, (u_int8_t *)fhp, NFSX_MYFH, 0);
4522 } else if (procnum == NFSV4OP_CBLAYOUTRECALL) {
4523 NFSD_DEBUG(4, "docallback layout recall\n");
4524 nd->nd_procnum = NFSV4PROC_CBCOMPOUND;
4525 error = nfsrv_cbcallargs(nd, clp, callback,
4526 NFSV4OP_CBLAYOUTRECALL, "CB Reclayout", &sep, &slotpos);
4527 NFSD_DEBUG(4, "aft cbcallargs=%d\n", error);
4529 m_freem(nd->nd_mreq);
4532 NFSM_BUILD(tl, u_int32_t *, 4 * NFSX_UNSIGNED);
4533 *tl++ = txdr_unsigned(laytype);
4534 *tl++ = txdr_unsigned(NFSLAYOUTIOMODE_ANY);
4536 *tl++ = newnfs_true;
4538 *tl++ = newnfs_false;
4539 *tl = txdr_unsigned(NFSV4LAYOUTRET_FILE);
4540 nfsm_fhtom(nd, (uint8_t *)fhp, NFSX_MYFH, 0);
4541 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_HYPER + NFSX_STATEID);
4543 txdr_hyper(tval, tl); tl += 2;
4545 txdr_hyper(tval, tl); tl += 2;
4546 *tl++ = txdr_unsigned(stateidp->seqid);
4547 NFSBCOPY(stateidp->other, tl, NFSX_STATEIDOTHER);
4548 tl += (NFSX_STATEIDOTHER / NFSX_UNSIGNED);
4549 NFSD_DEBUG(4, "aft args\n");
4550 } else if (procnum == NFSV4PROC_CBNULL) {
4551 nd->nd_procnum = NFSV4PROC_CBNULL;
4552 if ((clp->lc_flags & LCL_NFSV41) != 0) {
4553 error = nfsv4_getcbsession(clp, &sep);
4555 m_freem(nd->nd_mreq);
4560 error = NFSERR_SERVERFAULT;
4561 m_freem(nd->nd_mreq);
4566 * Call newnfs_connect(), as required, and then newnfs_request().
4569 if ((clp->lc_flags & LCL_TLSCB) != 0)
4571 (void) newnfs_sndlock(&clp->lc_req.nr_lock);
4572 if (clp->lc_req.nr_client == NULL) {
4573 if ((clp->lc_flags & LCL_NFSV41) != 0) {
4574 error = ECONNREFUSED;
4575 if (procnum != NFSV4PROC_CBNULL)
4576 nfsv4_freeslot(&sep->sess_cbsess, slotpos,
4578 nfsrv_freesession(sep, NULL);
4579 } else if (nd->nd_procnum == NFSV4PROC_CBNULL)
4580 error = newnfs_connect(NULL, &clp->lc_req, cred,
4581 NULL, 1, dotls, &clp->lc_req.nr_client);
4583 error = newnfs_connect(NULL, &clp->lc_req, cred,
4584 NULL, 3, dotls, &clp->lc_req.nr_client);
4586 newnfs_sndunlock(&clp->lc_req.nr_lock);
4587 NFSD_DEBUG(4, "aft sndunlock=%d\n", error);
4589 if ((nd->nd_flag & ND_NFSV41) != 0) {
4590 KASSERT(sep != NULL, ("sep NULL"));
4591 if (sep->sess_cbsess.nfsess_xprt != NULL)
4592 error = newnfs_request(nd, NULL, clp,
4593 &clp->lc_req, NULL, NULL, cred,
4594 clp->lc_program, clp->lc_req.nr_vers, NULL,
4595 1, NULL, &sep->sess_cbsess);
4598 * This should probably never occur, but if a
4599 * client somehow does an RPC without a
4600 * SequenceID Op that causes a callback just
4601 * after the nfsd threads have been terminated
4602 * and restared we could conceivably get here
4603 * without a backchannel xprt.
4605 printf("nfsrv_docallback: no xprt\n");
4606 error = ECONNREFUSED;
4608 NFSD_DEBUG(4, "aft newnfs_request=%d\n", error);
4609 if (error != 0 && procnum != NFSV4PROC_CBNULL) {
4611 * It is likely that the callback was never
4612 * processed by the client and, as such,
4613 * the sequence# for the session slot needs
4614 * to be backed up by one to avoid a
4615 * NFSERR_SEQMISORDERED error reply.
4616 * For the unlikely case where the callback
4617 * was processed by the client, this will
4618 * make the next callback on the slot
4619 * appear to be a retry.
4620 * Since callbacks never specify that the
4621 * reply be cached, this "apparent retry"
4622 * should not be a problem.
4624 nfsv4_freeslot(&sep->sess_cbsess, slotpos,
4627 nfsrv_freesession(sep, NULL);
4629 error = newnfs_request(nd, NULL, clp, &clp->lc_req,
4630 NULL, NULL, cred, clp->lc_program,
4631 clp->lc_req.nr_vers, NULL, 1, NULL, NULL);
4637 * If error is set here, the Callback path isn't working
4638 * properly, so twiddle the appropriate LCL_ flags.
4639 * (nd_repstat != 0 indicates the Callback path is working,
4640 * but the callback failed on the client.)
4644 * Mark the callback pathway down, which disabled issuing
4645 * of delegations and gets Renew to return NFSERR_CBPATHDOWN.
4648 clp->lc_flags |= LCL_CBDOWN;
4652 * Callback worked. If the callback path was down, disable
4653 * callbacks, so no more delegations will be issued. (This
4654 * is done on the assumption that the callback pathway is
4658 if (clp->lc_flags & LCL_CBDOWN)
4659 clp->lc_flags &= ~(LCL_CBDOWN | LCL_CALLBACKSON);
4661 if (nd->nd_repstat) {
4662 error = nd->nd_repstat;
4663 NFSD_DEBUG(1, "nfsrv_docallback op=%d err=%d\n",
4665 } else if (error == 0 && procnum == NFSV4OP_CBGETATTR)
4666 error = nfsv4_loadattr(nd, NULL, nap, NULL, NULL, 0,
4667 NULL, NULL, NULL, NULL, NULL, 0, NULL, NULL, NULL,
4669 m_freem(nd->nd_mrep);
4673 if ((clp->lc_flags & LCL_WAKEUPWANTED) && clp->lc_cbref == 0) {
4674 clp->lc_flags &= ~LCL_WAKEUPWANTED;
4685 * Set up the compound RPC for the callback.
4688 nfsrv_cbcallargs(struct nfsrv_descript *nd, struct nfsclient *clp,
4689 uint32_t callback, int op, const char *optag, struct nfsdsession **sepp,
4695 len = strlen(optag);
4696 (void)nfsm_strtom(nd, optag, len);
4697 NFSM_BUILD(tl, uint32_t *, 4 * NFSX_UNSIGNED);
4698 if ((nd->nd_flag & ND_NFSV41) != 0) {
4699 if ((nd->nd_flag & ND_NFSV42) != 0)
4700 *tl++ = txdr_unsigned(NFSV42_MINORVERSION);
4702 *tl++ = txdr_unsigned(NFSV41_MINORVERSION);
4703 *tl++ = txdr_unsigned(callback);
4704 *tl++ = txdr_unsigned(2);
4705 *tl = txdr_unsigned(NFSV4OP_CBSEQUENCE);
4706 error = nfsv4_setcbsequence(nd, clp, 1, sepp, slotposp);
4709 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
4710 *tl = txdr_unsigned(op);
4712 *tl++ = txdr_unsigned(NFSV4_MINORVERSION);
4713 *tl++ = txdr_unsigned(callback);
4714 *tl++ = txdr_unsigned(1);
4715 *tl = txdr_unsigned(op);
4721 * Return the next index# for a clientid. Mostly just increment and return
4722 * the next one, but... if the 32bit unsigned does actually wrap around,
4723 * it should be rebooted.
4724 * At an average rate of one new client per second, it will wrap around in
4725 * approximately 136 years. (I think the server will have been shut
4726 * down or rebooted before then.)
4729 nfsrv_nextclientindex(void)
4731 static u_int32_t client_index = 0;
4734 if (client_index != 0)
4735 return (client_index);
4737 printf("%s: out of clientids\n", __func__);
4738 return (client_index);
4742 * Return the next index# for a stateid. Mostly just increment and return
4743 * the next one, but... if the 32bit unsigned does actually wrap around
4744 * (will a BSD server stay up that long?), find
4745 * new start and end values.
4748 nfsrv_nextstateindex(struct nfsclient *clp)
4750 struct nfsstate *stp;
4752 u_int32_t canuse, min_index, max_index;
4754 if (!(clp->lc_flags & LCL_INDEXNOTOK)) {
4755 clp->lc_stateindex++;
4756 if (clp->lc_stateindex != clp->lc_statemaxindex)
4757 return (clp->lc_stateindex);
4761 * Yuck, we've hit the end.
4762 * Look for a new min and max.
4765 max_index = 0xffffffff;
4766 for (i = 0; i < nfsrv_statehashsize; i++) {
4767 LIST_FOREACH(stp, &clp->lc_stateid[i], ls_hash) {
4768 if (stp->ls_stateid.other[2] > 0x80000000) {
4769 if (stp->ls_stateid.other[2] < max_index)
4770 max_index = stp->ls_stateid.other[2];
4772 if (stp->ls_stateid.other[2] > min_index)
4773 min_index = stp->ls_stateid.other[2];
4779 * Yikes, highly unlikely, but I'll handle it anyhow.
4781 if (min_index == 0x80000000 && max_index == 0x80000001) {
4784 * Loop around until we find an unused entry. Return that
4785 * and set LCL_INDEXNOTOK, so the search will continue next time.
4786 * (This is one of those rare cases where a goto is the
4787 * cleanest way to code the loop.)
4790 for (i = 0; i < nfsrv_statehashsize; i++) {
4791 LIST_FOREACH(stp, &clp->lc_stateid[i], ls_hash) {
4792 if (stp->ls_stateid.other[2] == canuse) {
4798 clp->lc_flags |= LCL_INDEXNOTOK;
4803 * Ok to start again from min + 1.
4805 clp->lc_stateindex = min_index + 1;
4806 clp->lc_statemaxindex = max_index;
4807 clp->lc_flags &= ~LCL_INDEXNOTOK;
4808 return (clp->lc_stateindex);
4812 * The following functions handle the stable storage file that deals with
4813 * the edge conditions described in RFC3530 Sec. 8.6.3.
4814 * The file is as follows:
4815 * - a single record at the beginning that has the lease time of the
4816 * previous server instance (before the last reboot) and the nfsrvboottime
4817 * values for the previous server boots.
4818 * These previous boot times are used to ensure that the current
4819 * nfsrvboottime does not, somehow, get set to a previous one.
4820 * (This is important so that Stale ClientIDs and StateIDs can
4822 * The number of previous nfsvrboottime values precedes the list.
4823 * - followed by some number of appended records with:
4824 * - client id string
4825 * - flag that indicates it is a record revoking state via lease
4826 * expiration or similar
4827 * OR has successfully acquired state.
4828 * These structures vary in length, with the client string at the end, up
4829 * to NFSV4_OPAQUELIMIT in size.
4831 * At the end of the grace period, the file is truncated, the first
4832 * record is rewritten with updated information and any acquired state
4833 * records for successful reclaims of state are written.
4835 * Subsequent records are appended when the first state is issued to
4836 * a client and when state is revoked for a client.
4838 * When reading the file in, state issued records that come later in
4839 * the file override older ones, since the append log is in cronological order.
4840 * If, for some reason, the file can't be read, the grace period is
4841 * immediately terminated and all reclaims get NFSERR_NOGRACE.
4845 * Read in the stable storage file. Called by nfssvc() before the nfsd
4846 * processes start servicing requests.
4849 nfsrv_setupstable(NFSPROC_T *p)
4851 struct nfsrv_stablefirst *sf = &nfsrv_stablefirst;
4852 struct nfsrv_stable *sp, *nsp;
4853 struct nfst_rec *tsp;
4854 int error, i, tryagain;
4856 ssize_t aresid, len;
4859 * If NFSNSF_UPDATEDONE is set, this is a restart of the nfsds without
4860 * a reboot, so state has not been lost.
4862 if (sf->nsf_flags & NFSNSF_UPDATEDONE)
4865 * Set Grace over just until the file reads successfully.
4867 nfsrvboottime = time_second;
4868 LIST_INIT(&sf->nsf_head);
4869 sf->nsf_flags = (NFSNSF_GRACEOVER | NFSNSF_NEEDLOCK);
4870 sf->nsf_eograce = NFSD_MONOSEC + NFSRV_LEASEDELTA;
4871 if (sf->nsf_fp == NULL)
4873 error = NFSD_RDWR(UIO_READ, NFSFPVNODE(sf->nsf_fp),
4874 (caddr_t)&sf->nsf_rec, sizeof (struct nfsf_rec), off, UIO_SYSSPACE,
4875 0, NFSFPCRED(sf->nsf_fp), &aresid, p);
4876 if (error || aresid || sf->nsf_numboots == 0 ||
4877 sf->nsf_numboots > NFSNSF_MAXNUMBOOTS)
4881 * Now, read in the boottimes.
4883 sf->nsf_bootvals = (time_t *)malloc((sf->nsf_numboots + 1) *
4884 sizeof (time_t), M_TEMP, M_WAITOK);
4885 off = sizeof (struct nfsf_rec);
4886 error = NFSD_RDWR(UIO_READ, NFSFPVNODE(sf->nsf_fp),
4887 (caddr_t)sf->nsf_bootvals, sf->nsf_numboots * sizeof (time_t), off,
4888 UIO_SYSSPACE, 0, NFSFPCRED(sf->nsf_fp), &aresid, p);
4889 if (error || aresid) {
4890 free(sf->nsf_bootvals, M_TEMP);
4891 sf->nsf_bootvals = NULL;
4896 * Make sure this nfsrvboottime is different from all recorded
4901 for (i = 0; i < sf->nsf_numboots; i++) {
4902 if (nfsrvboottime == sf->nsf_bootvals[i]) {
4910 sf->nsf_flags |= NFSNSF_OK;
4911 off += (sf->nsf_numboots * sizeof (time_t));
4914 * Read through the file, building a list of records for grace
4916 * Each record is between sizeof (struct nfst_rec) and
4917 * sizeof (struct nfst_rec) + NFSV4_OPAQUELIMIT - 1
4918 * and is actually sizeof (struct nfst_rec) + nst_len - 1.
4920 tsp = (struct nfst_rec *)malloc(sizeof (struct nfst_rec) +
4921 NFSV4_OPAQUELIMIT - 1, M_TEMP, M_WAITOK);
4923 error = NFSD_RDWR(UIO_READ, NFSFPVNODE(sf->nsf_fp),
4924 (caddr_t)tsp, sizeof (struct nfst_rec) + NFSV4_OPAQUELIMIT - 1,
4925 off, UIO_SYSSPACE, 0, NFSFPCRED(sf->nsf_fp), &aresid, p);
4926 len = (sizeof (struct nfst_rec) + NFSV4_OPAQUELIMIT - 1) - aresid;
4927 if (error || (len > 0 && (len < sizeof (struct nfst_rec) ||
4928 len < (sizeof (struct nfst_rec) + tsp->len - 1)))) {
4930 * Yuck, the file has been corrupted, so just return
4931 * after clearing out any restart state, so the grace period
4934 LIST_FOREACH_SAFE(sp, &sf->nsf_head, nst_list, nsp) {
4935 LIST_REMOVE(sp, nst_list);
4939 sf->nsf_flags &= ~NFSNSF_OK;
4940 free(sf->nsf_bootvals, M_TEMP);
4941 sf->nsf_bootvals = NULL;
4945 off += sizeof (struct nfst_rec) + tsp->len - 1;
4947 * Search the list for a matching client.
4949 LIST_FOREACH(sp, &sf->nsf_head, nst_list) {
4950 if (tsp->len == sp->nst_len &&
4951 !NFSBCMP(tsp->client, sp->nst_client, tsp->len))
4954 if (sp == LIST_END(&sf->nsf_head)) {
4955 sp = (struct nfsrv_stable *)malloc(tsp->len +
4956 sizeof (struct nfsrv_stable) - 1, M_TEMP,
4958 NFSBCOPY((caddr_t)tsp, (caddr_t)&sp->nst_rec,
4959 sizeof (struct nfst_rec) + tsp->len - 1);
4960 LIST_INSERT_HEAD(&sf->nsf_head, sp, nst_list);
4962 if (tsp->flag == NFSNST_REVOKE)
4963 sp->nst_flag |= NFSNST_REVOKE;
4966 * A subsequent timestamp indicates the client
4967 * did a setclientid/confirm and any previous
4968 * revoke is no longer relevant.
4970 sp->nst_flag &= ~NFSNST_REVOKE;
4975 sf->nsf_flags = NFSNSF_OK;
4976 sf->nsf_eograce = NFSD_MONOSEC + sf->nsf_lease +
4981 * Update the stable storage file, now that the grace period is over.
4984 nfsrv_updatestable(NFSPROC_T *p)
4986 struct nfsrv_stablefirst *sf = &nfsrv_stablefirst;
4987 struct nfsrv_stable *sp, *nsp;
4989 struct nfsvattr nva;
4991 #if defined(__FreeBSD_version) && (__FreeBSD_version >= 500000)
4996 if (sf->nsf_fp == NULL || (sf->nsf_flags & NFSNSF_UPDATEDONE))
4998 sf->nsf_flags |= NFSNSF_UPDATEDONE;
5000 * Ok, we need to rewrite the stable storage file.
5001 * - truncate to 0 length
5002 * - write the new first structure
5003 * - loop through the data structures, writing out any that
5004 * have timestamps older than the old boot
5006 if (sf->nsf_bootvals) {
5008 for (i = sf->nsf_numboots - 2; i >= 0; i--)
5009 sf->nsf_bootvals[i + 1] = sf->nsf_bootvals[i];
5011 sf->nsf_numboots = 1;
5012 sf->nsf_bootvals = (time_t *)malloc(sizeof (time_t),
5015 sf->nsf_bootvals[0] = nfsrvboottime;
5016 sf->nsf_lease = nfsrv_lease;
5017 NFSVNO_ATTRINIT(&nva);
5018 NFSVNO_SETATTRVAL(&nva, size, 0);
5019 vp = NFSFPVNODE(sf->nsf_fp);
5020 vn_start_write(vp, &mp, V_WAIT);
5021 if (NFSVOPLOCK(vp, LK_EXCLUSIVE) == 0) {
5022 error = nfsvno_setattr(vp, &nva, NFSFPCRED(sf->nsf_fp), p,
5027 vn_finished_write(mp);
5029 error = NFSD_RDWR(UIO_WRITE, vp,
5030 (caddr_t)&sf->nsf_rec, sizeof (struct nfsf_rec), (off_t)0,
5031 UIO_SYSSPACE, IO_SYNC, NFSFPCRED(sf->nsf_fp), NULL, p);
5033 error = NFSD_RDWR(UIO_WRITE, vp,
5034 (caddr_t)sf->nsf_bootvals,
5035 sf->nsf_numboots * sizeof (time_t),
5036 (off_t)(sizeof (struct nfsf_rec)),
5037 UIO_SYSSPACE, IO_SYNC, NFSFPCRED(sf->nsf_fp), NULL, p);
5038 free(sf->nsf_bootvals, M_TEMP);
5039 sf->nsf_bootvals = NULL;
5041 sf->nsf_flags &= ~NFSNSF_OK;
5042 printf("EEK! Can't write NfsV4 stable storage file\n");
5045 sf->nsf_flags |= NFSNSF_OK;
5048 * Loop through the list and write out timestamp records for
5049 * any clients that successfully reclaimed state.
5051 LIST_FOREACH_SAFE(sp, &sf->nsf_head, nst_list, nsp) {
5052 if (sp->nst_flag & NFSNST_GOTSTATE) {
5053 nfsrv_writestable(sp->nst_client, sp->nst_len,
5054 NFSNST_NEWSTATE, p);
5055 sp->nst_clp->lc_flags |= LCL_STAMPEDSTABLE;
5057 LIST_REMOVE(sp, nst_list);
5060 nfsrv_backupstable();
5064 * Append a record to the stable storage file.
5067 nfsrv_writestable(u_char *client, int len, int flag, NFSPROC_T *p)
5069 struct nfsrv_stablefirst *sf = &nfsrv_stablefirst;
5070 struct nfst_rec *sp;
5073 if (!(sf->nsf_flags & NFSNSF_OK) || sf->nsf_fp == NULL)
5075 sp = (struct nfst_rec *)malloc(sizeof (struct nfst_rec) +
5076 len - 1, M_TEMP, M_WAITOK);
5078 NFSBCOPY(client, sp->client, len);
5080 error = NFSD_RDWR(UIO_WRITE, NFSFPVNODE(sf->nsf_fp),
5081 (caddr_t)sp, sizeof (struct nfst_rec) + len - 1, (off_t)0,
5082 UIO_SYSSPACE, (IO_SYNC | IO_APPEND), NFSFPCRED(sf->nsf_fp), NULL, p);
5085 sf->nsf_flags &= ~NFSNSF_OK;
5086 printf("EEK! Can't write NfsV4 stable storage file\n");
5091 * This function is called during the grace period to mark a client
5092 * that successfully reclaimed state.
5095 nfsrv_markstable(struct nfsclient *clp)
5097 struct nfsrv_stable *sp;
5100 * First find the client structure.
5102 LIST_FOREACH(sp, &nfsrv_stablefirst.nsf_head, nst_list) {
5103 if (sp->nst_len == clp->lc_idlen &&
5104 !NFSBCMP(sp->nst_client, clp->lc_id, sp->nst_len))
5107 if (sp == LIST_END(&nfsrv_stablefirst.nsf_head))
5111 * Now, just mark it and set the nfsclient back pointer.
5113 sp->nst_flag |= NFSNST_GOTSTATE;
5118 * This function is called when a NFSv4.1 client does a ReclaimComplete.
5119 * Very similar to nfsrv_markstable(), except for the flag being set.
5122 nfsrv_markreclaim(struct nfsclient *clp)
5124 struct nfsrv_stable *sp;
5127 * First find the client structure.
5129 LIST_FOREACH(sp, &nfsrv_stablefirst.nsf_head, nst_list) {
5130 if (sp->nst_len == clp->lc_idlen &&
5131 !NFSBCMP(sp->nst_client, clp->lc_id, sp->nst_len))
5134 if (sp == LIST_END(&nfsrv_stablefirst.nsf_head))
5138 * Now, just set the flag.
5140 sp->nst_flag |= NFSNST_RECLAIMED;
5144 * This function is called for a reclaim, to see if it gets grace.
5145 * It returns 0 if a reclaim is allowed, 1 otherwise.
5148 nfsrv_checkstable(struct nfsclient *clp)
5150 struct nfsrv_stable *sp;
5153 * First, find the entry for the client.
5155 LIST_FOREACH(sp, &nfsrv_stablefirst.nsf_head, nst_list) {
5156 if (sp->nst_len == clp->lc_idlen &&
5157 !NFSBCMP(sp->nst_client, clp->lc_id, sp->nst_len))
5162 * If not in the list, state was revoked or no state was issued
5163 * since the previous reboot, a reclaim is denied.
5165 if (sp == LIST_END(&nfsrv_stablefirst.nsf_head) ||
5166 (sp->nst_flag & NFSNST_REVOKE) ||
5167 !(nfsrv_stablefirst.nsf_flags & NFSNSF_OK))
5173 * Test for and try to clear out a conflicting client. This is called by
5174 * nfsrv_lockctrl() and nfsrv_openctrl() when conflicts with other clients
5176 * The trick here is that it can't revoke a conflicting client with an
5177 * expired lease unless it holds the v4root lock, so...
5178 * If no v4root lock, get the lock and return 1 to indicate "try again".
5179 * Return 0 to indicate the conflict can't be revoked and 1 to indicate
5180 * the revocation worked and the conflicting client is "bye, bye", so it
5181 * can be tried again.
5182 * Return 2 to indicate that the vnode is VIRF_DOOMED after NFSVOPLOCK().
5183 * Unlocks State before a non-zero value is returned.
5186 nfsrv_clientconflict(struct nfsclient *clp, int *haslockp, vnode_t vp,
5189 int gotlock, lktype = 0;
5192 * If lease hasn't expired, we can't fix it.
5194 if (clp->lc_expiry >= NFSD_MONOSEC ||
5195 !(nfsrv_stablefirst.nsf_flags & NFSNSF_UPDATEDONE))
5197 if (*haslockp == 0) {
5200 lktype = NFSVOPISLOCKED(vp);
5203 NFSLOCKV4ROOTMUTEX();
5204 nfsv4_relref(&nfsv4rootfs_lock);
5206 gotlock = nfsv4_lock(&nfsv4rootfs_lock, 1, NULL,
5207 NFSV4ROOTLOCKMUTEXPTR, NULL);
5209 NFSUNLOCKV4ROOTMUTEX();
5212 NFSVOPLOCK(vp, lktype | LK_RETRY);
5213 if (VN_IS_DOOMED(vp))
5221 * Ok, we can expire the conflicting client.
5223 nfsrv_writestable(clp->lc_id, clp->lc_idlen, NFSNST_REVOKE, p);
5224 nfsrv_backupstable();
5225 nfsrv_cleanclient(clp, p);
5226 nfsrv_freedeleglist(&clp->lc_deleg);
5227 nfsrv_freedeleglist(&clp->lc_olddeleg);
5228 LIST_REMOVE(clp, lc_hash);
5229 nfsrv_zapclient(clp, p);
5234 * Resolve a delegation conflict.
5235 * Returns 0 to indicate the conflict was resolved without sleeping.
5236 * Return -1 to indicate that the caller should check for conflicts again.
5237 * Return > 0 for an error that should be returned, normally NFSERR_DELAY.
5239 * Also, manipulate the nfsv4root_lock, as required. It isn't changed
5240 * for a return of 0, since there was no sleep and it could be required
5241 * later. It is released for a return of NFSERR_DELAY, since the caller
5242 * will return that error. It is released when a sleep was done waiting
5243 * for the delegation to be returned or expire (so that other nfsds can
5244 * handle ops). Then, it must be acquired for the write to stable storage.
5245 * (This function is somewhat similar to nfsrv_clientconflict(), but
5246 * the semantics differ in a couple of subtle ways. The return of 0
5247 * indicates the conflict was resolved without sleeping here, not
5248 * that the conflict can't be resolved and the handling of nfsv4root_lock
5249 * differs, as noted above.)
5250 * Unlocks State before returning a non-zero value.
5253 nfsrv_delegconflict(struct nfsstate *stp, int *haslockp, NFSPROC_T *p,
5256 struct nfsclient *clp = stp->ls_clp;
5257 int gotlock, error, lktype = 0, retrycnt, zapped_clp;
5258 nfsv4stateid_t tstateid;
5262 * If the conflict is with an old delegation...
5264 if (stp->ls_flags & NFSLCK_OLDDELEG) {
5266 * You can delete it, if it has expired.
5268 if (clp->lc_delegtime < NFSD_MONOSEC) {
5269 nfsrv_freedeleg(stp);
5276 * During this delay, the old delegation could expire or it
5277 * could be recovered by the client via an Open with
5278 * CLAIM_DELEGATE_PREV.
5279 * Release the nfsv4root_lock, if held.
5283 NFSLOCKV4ROOTMUTEX();
5284 nfsv4_unlock(&nfsv4rootfs_lock, 1);
5285 NFSUNLOCKV4ROOTMUTEX();
5287 error = NFSERR_DELAY;
5292 * It's a current delegation, so:
5293 * - check to see if the delegation has expired
5294 * - if so, get the v4root lock and then expire it
5296 if ((stp->ls_flags & NFSLCK_DELEGRECALL) == 0 || (stp->ls_lastrecall <
5297 NFSD_MONOSEC && clp->lc_expiry >= NFSD_MONOSEC &&
5298 stp->ls_delegtime >= NFSD_MONOSEC)) {
5300 * - do a recall callback, since not yet done
5301 * For now, never allow truncate to be set. To use
5302 * truncate safely, it must be guaranteed that the
5303 * Remove, Rename or Setattr with size of 0 will
5304 * succeed and that would require major changes to
5305 * the VFS/Vnode OPs.
5306 * Set the expiry time large enough so that it won't expire
5307 * until after the callback, then set it correctly, once
5308 * the callback is done. (The delegation will now time
5309 * out whether or not the Recall worked ok. The timeout
5310 * will be extended when ops are done on the delegation
5311 * stateid, up to the timelimit.)
5313 if ((stp->ls_flags & NFSLCK_DELEGRECALL) == 0) {
5314 stp->ls_delegtime = NFSD_MONOSEC + (2 * nfsrv_lease) +
5316 stp->ls_delegtimelimit = NFSD_MONOSEC + (6 *
5317 nfsrv_lease) + NFSRV_LEASEDELTA;
5318 stp->ls_flags |= NFSLCK_DELEGRECALL;
5320 stp->ls_lastrecall = time_uptime + 1;
5323 * Loop NFSRV_CBRETRYCNT times while the CBRecall replies
5324 * NFSERR_BADSTATEID or NFSERR_BADHANDLE. This is done
5325 * in order to try and avoid a race that could happen
5326 * when a CBRecall request passed the Open reply with
5327 * the delegation in it when transitting the network.
5328 * Since nfsrv_docallback will sleep, don't use stp after
5331 NFSBCOPY((caddr_t)&stp->ls_stateid, (caddr_t)&tstateid,
5333 NFSBCOPY((caddr_t)&stp->ls_lfp->lf_fh, (caddr_t)&tfh,
5338 NFSLOCKV4ROOTMUTEX();
5339 nfsv4_unlock(&nfsv4rootfs_lock, 1);
5340 NFSUNLOCKV4ROOTMUTEX();
5344 error = nfsrv_docallback(clp, NFSV4OP_CBRECALL,
5345 &tstateid, 0, &tfh, NULL, NULL, 0, p);
5347 } while ((error == NFSERR_BADSTATEID ||
5348 error == NFSERR_BADHANDLE) && retrycnt < NFSV4_CBRETRYCNT);
5349 error = NFSERR_DELAY;
5353 if (clp->lc_expiry >= NFSD_MONOSEC &&
5354 stp->ls_delegtime >= NFSD_MONOSEC) {
5357 * A recall has been done, but it has not yet expired.
5362 NFSLOCKV4ROOTMUTEX();
5363 nfsv4_unlock(&nfsv4rootfs_lock, 1);
5364 NFSUNLOCKV4ROOTMUTEX();
5366 error = NFSERR_DELAY;
5371 * If we don't yet have the lock, just get it and then return,
5372 * since we need that before deleting expired state, such as
5374 * When getting the lock, unlock the vnode, so other nfsds that
5375 * are in progress, won't get stuck waiting for the vnode lock.
5377 if (*haslockp == 0) {
5380 lktype = NFSVOPISLOCKED(vp);
5383 NFSLOCKV4ROOTMUTEX();
5384 nfsv4_relref(&nfsv4rootfs_lock);
5386 gotlock = nfsv4_lock(&nfsv4rootfs_lock, 1, NULL,
5387 NFSV4ROOTLOCKMUTEXPTR, NULL);
5389 NFSUNLOCKV4ROOTMUTEX();
5392 NFSVOPLOCK(vp, lktype | LK_RETRY);
5393 if (VN_IS_DOOMED(vp)) {
5395 NFSLOCKV4ROOTMUTEX();
5396 nfsv4_unlock(&nfsv4rootfs_lock, 1);
5397 NFSUNLOCKV4ROOTMUTEX();
5398 error = NFSERR_PERM;
5408 * Ok, we can delete the expired delegation.
5409 * First, write the Revoke record to stable storage and then
5410 * clear out the conflict.
5411 * Since all other nfsd threads are now blocked, we can safely
5412 * sleep without the state changing.
5414 nfsrv_writestable(clp->lc_id, clp->lc_idlen, NFSNST_REVOKE, p);
5415 nfsrv_backupstable();
5416 if (clp->lc_expiry < NFSD_MONOSEC) {
5417 nfsrv_cleanclient(clp, p);
5418 nfsrv_freedeleglist(&clp->lc_deleg);
5419 nfsrv_freedeleglist(&clp->lc_olddeleg);
5420 LIST_REMOVE(clp, lc_hash);
5423 nfsrv_freedeleg(stp);
5427 nfsrv_zapclient(clp, p);
5436 * Check for a remove allowed, if remove is set to 1 and get rid of
5440 nfsrv_checkremove(vnode_t vp, int remove, struct nfsrv_descript *nd,
5441 nfsquad_t clientid, NFSPROC_T *p)
5443 struct nfsclient *clp;
5444 struct nfsstate *stp;
5445 struct nfslockfile *lfp;
5446 int error, haslock = 0;
5451 * First, get the lock file structure.
5452 * (A return of -1 means no associated state, so remove ok.)
5454 error = nfsrv_getlockfh(vp, NFSLCK_CHECK, NULL, &nfh, p);
5457 if (error == 0 && clientid.qval != 0)
5458 error = nfsrv_getclient(clientid, CLOPS_RENEW, &clp, NULL,
5459 (nfsquad_t)((u_quad_t)0), 0, nd, p);
5461 error = nfsrv_getlockfile(NFSLCK_CHECK, NULL, &lfp, &nfh, 0);
5465 NFSLOCKV4ROOTMUTEX();
5466 nfsv4_unlock(&nfsv4rootfs_lock, 1);
5467 NFSUNLOCKV4ROOTMUTEX();
5475 * Now, we must Recall any delegations.
5477 error = nfsrv_cleandeleg(vp, lfp, clp, &haslock, p);
5480 * nfsrv_cleandeleg() unlocks state for non-zero
5486 NFSLOCKV4ROOTMUTEX();
5487 nfsv4_unlock(&nfsv4rootfs_lock, 1);
5488 NFSUNLOCKV4ROOTMUTEX();
5494 * Now, look for a conflicting open share.
5498 * If the entry in the directory was the last reference to the
5499 * corresponding filesystem object, the object can be destroyed
5501 if(lfp->lf_usecount>1)
5502 LIST_FOREACH(stp, &lfp->lf_open, ls_file) {
5503 if (stp->ls_flags & NFSLCK_WRITEDENY) {
5504 error = NFSERR_FILEOPEN;
5512 NFSLOCKV4ROOTMUTEX();
5513 nfsv4_unlock(&nfsv4rootfs_lock, 1);
5514 NFSUNLOCKV4ROOTMUTEX();
5523 * Clear out all delegations for the file referred to by lfp.
5524 * May return NFSERR_DELAY, if there will be a delay waiting for
5525 * delegations to expire.
5526 * Returns -1 to indicate it slept while recalling a delegation.
5527 * This function has the side effect of deleting the nfslockfile structure,
5528 * if it no longer has associated state and didn't have to sleep.
5529 * Unlocks State before a non-zero value is returned.
5532 nfsrv_cleandeleg(vnode_t vp, struct nfslockfile *lfp,
5533 struct nfsclient *clp, int *haslockp, NFSPROC_T *p)
5535 struct nfsstate *stp, *nstp;
5538 stp = LIST_FIRST(&lfp->lf_deleg);
5539 while (stp != LIST_END(&lfp->lf_deleg)) {
5540 nstp = LIST_NEXT(stp, ls_file);
5541 if (stp->ls_clp != clp) {
5542 ret = nfsrv_delegconflict(stp, haslockp, p, vp);
5545 * nfsrv_delegconflict() unlocks state
5546 * when it returns non-zero.
5559 * There are certain operations that, when being done outside of NFSv4,
5560 * require that any NFSv4 delegation for the file be recalled.
5561 * This function is to be called for those cases:
5562 * VOP_RENAME() - When a delegation is being recalled for any reason,
5563 * the client may have to do Opens against the server, using the file's
5564 * final component name. If the file has been renamed on the server,
5565 * that component name will be incorrect and the Open will fail.
5566 * VOP_REMOVE() - Theoretically, a client could Open a file after it has
5567 * been removed on the server, if there is a delegation issued to
5568 * that client for the file. I say "theoretically" since clients
5569 * normally do an Access Op before the Open and that Access Op will
5570 * fail with ESTALE. Note that NFSv2 and 3 don't even do Opens, so
5571 * they will detect the file's removal in the same manner. (There is
5572 * one case where RFC3530 allows a client to do an Open without first
5573 * doing an Access Op, which is passage of a check against the ACE
5574 * returned with a Write delegation, but current practice is to ignore
5575 * the ACE and always do an Access Op.)
5576 * Since the functions can only be called with an unlocked vnode, this
5577 * can't be done at this time.
5578 * VOP_ADVLOCK() - When a client holds a delegation, it can issue byte range
5579 * locks locally in the client, which are not visible to the server. To
5580 * deal with this, issuing of delegations for a vnode must be disabled
5581 * and all delegations for the vnode recalled. This is done via the
5582 * second function, using the VV_DISABLEDELEG vflag on the vnode.
5585 nfsd_recalldelegation(vnode_t vp, NFSPROC_T *p)
5591 * First, check to see if the server is currently running and it has
5592 * been called for a regular file when issuing delegations.
5594 if (newnfs_numnfsd == 0 || vp->v_type != VREG ||
5595 nfsrv_issuedelegs == 0)
5598 KASSERT((NFSVOPISLOCKED(vp) != LK_EXCLUSIVE), ("vp %p is locked", vp));
5600 * First, get a reference on the nfsv4rootfs_lock so that an
5601 * exclusive lock cannot be acquired by another thread.
5603 NFSLOCKV4ROOTMUTEX();
5604 nfsv4_getref(&nfsv4rootfs_lock, NULL, NFSV4ROOTLOCKMUTEXPTR, NULL);
5605 NFSUNLOCKV4ROOTMUTEX();
5608 * Now, call nfsrv_checkremove() in a loop while it returns
5609 * NFSERR_DELAY. Return upon any other error or when timed out.
5611 starttime = NFSD_MONOSEC;
5613 if (NFSVOPLOCK(vp, LK_EXCLUSIVE) == 0) {
5614 error = nfsrv_checkremove(vp, 0, NULL,
5615 (nfsquad_t)((u_quad_t)0), p);
5619 if (error == NFSERR_DELAY) {
5620 if (NFSD_MONOSEC - starttime > NFS_REMOVETIMEO)
5622 /* Sleep for a short period of time */
5623 (void) nfs_catnap(PZERO, 0, "nfsremove");
5625 } while (error == NFSERR_DELAY);
5626 NFSLOCKV4ROOTMUTEX();
5627 nfsv4_relref(&nfsv4rootfs_lock);
5628 NFSUNLOCKV4ROOTMUTEX();
5632 nfsd_disabledelegation(vnode_t vp, NFSPROC_T *p)
5635 #ifdef VV_DISABLEDELEG
5637 * First, flag issuance of delegations disabled.
5639 atomic_set_long(&vp->v_vflag, VV_DISABLEDELEG);
5643 * Then call nfsd_recalldelegation() to get rid of all extant
5646 nfsd_recalldelegation(vp, p);
5650 * Check for conflicting locks, etc. and then get rid of delegations.
5651 * (At one point I thought that I should get rid of delegations for any
5652 * Setattr, since it could potentially disallow the I/O op (read or write)
5653 * allowed by the delegation. However, Setattr Ops that aren't changing
5654 * the size get a stateid of all 0s, so you can't tell if it is a delegation
5655 * for the same client or a different one, so I decided to only get rid
5656 * of delegations for other clients when the size is being changed.)
5657 * In general, a Setattr can disable NFS I/O Ops that are outstanding, such
5658 * as Write backs, even if there is no delegation, so it really isn't any
5662 nfsrv_checksetattr(vnode_t vp, struct nfsrv_descript *nd,
5663 nfsv4stateid_t *stateidp, struct nfsvattr *nvap, nfsattrbit_t *attrbitp,
5664 struct nfsexstuff *exp, NFSPROC_T *p)
5666 struct nfsstate st, *stp = &st;
5667 struct nfslock lo, *lop = &lo;
5671 if (NFSISSET_ATTRBIT(attrbitp, NFSATTRBIT_SIZE)) {
5672 stp->ls_flags = (NFSLCK_CHECK | NFSLCK_WRITEACCESS);
5673 lop->lo_first = nvap->na_size;
5678 if (NFSISSET_ATTRBIT(attrbitp, NFSATTRBIT_OWNER) ||
5679 NFSISSET_ATTRBIT(attrbitp, NFSATTRBIT_OWNERGROUP) ||
5680 NFSISSET_ATTRBIT(attrbitp, NFSATTRBIT_MODE) ||
5681 NFSISSET_ATTRBIT(attrbitp, NFSATTRBIT_ACL))
5682 stp->ls_flags |= NFSLCK_SETATTR;
5683 if (stp->ls_flags == 0)
5685 lop->lo_end = NFS64BITSSET;
5686 lop->lo_flags = NFSLCK_WRITE;
5687 stp->ls_ownerlen = 0;
5689 stp->ls_uid = nd->nd_cred->cr_uid;
5690 stp->ls_stateid.seqid = stateidp->seqid;
5691 clientid.lval[0] = stp->ls_stateid.other[0] = stateidp->other[0];
5692 clientid.lval[1] = stp->ls_stateid.other[1] = stateidp->other[1];
5693 stp->ls_stateid.other[2] = stateidp->other[2];
5694 error = nfsrv_lockctrl(vp, &stp, &lop, NULL, clientid,
5695 stateidp, exp, nd, p);
5698 NFSEXITCODE2(error, nd);
5703 * Check for a write delegation and do a CBGETATTR if there is one, updating
5704 * the attributes, as required.
5705 * Should I return an error if I can't get the attributes? (For now, I'll
5709 nfsrv_checkgetattr(struct nfsrv_descript *nd, vnode_t vp,
5710 struct nfsvattr *nvap, nfsattrbit_t *attrbitp, NFSPROC_T *p)
5712 struct nfsstate *stp;
5713 struct nfslockfile *lfp;
5714 struct nfsclient *clp;
5715 struct nfsvattr nva;
5718 nfsattrbit_t cbbits;
5719 u_quad_t delegfilerev;
5721 NFSCBGETATTR_ATTRBIT(attrbitp, &cbbits);
5722 if (!NFSNONZERO_ATTRBIT(&cbbits))
5724 if (nfsrv_writedelegcnt == 0)
5728 * Get the lock file structure.
5729 * (A return of -1 means no associated state, so return ok.)
5731 error = nfsrv_getlockfh(vp, NFSLCK_CHECK, NULL, &nfh, p);
5734 error = nfsrv_getlockfile(NFSLCK_CHECK, NULL, &lfp, &nfh, 0);
5743 * Now, look for a write delegation.
5745 LIST_FOREACH(stp, &lfp->lf_deleg, ls_file) {
5746 if (stp->ls_flags & NFSLCK_DELEGWRITE)
5749 if (stp == LIST_END(&lfp->lf_deleg)) {
5755 /* If the clientid is not confirmed, ignore the delegation. */
5756 if (clp->lc_flags & LCL_NEEDSCONFIRM) {
5761 delegfilerev = stp->ls_filerev;
5763 * If the Write delegation was issued as a part of this Compound RPC
5764 * or if we have an Implied Clientid (used in a previous Op in this
5765 * compound) and it is the client the delegation was issued to,
5767 * I also assume that it is from the same client iff the network
5768 * host IP address is the same as the callback address. (Not
5769 * exactly correct by the RFC, but avoids a lot of Getattr
5772 if (nd->nd_compref == stp->ls_compref ||
5773 ((nd->nd_flag & ND_IMPLIEDCLID) &&
5774 clp->lc_clientid.qval == nd->nd_clientid.qval) ||
5775 nfsaddr2_match(clp->lc_req.nr_nam, nd->nd_nam)) {
5781 * We are now done with the delegation state structure,
5782 * so the statelock can be released and we can now tsleep().
5786 * Now, we must do the CB Getattr callback, to see if Change or Size
5789 if (clp->lc_expiry >= NFSD_MONOSEC) {
5791 NFSVNO_ATTRINIT(&nva);
5792 nva.na_filerev = NFS64BITSSET;
5793 error = nfsrv_docallback(clp, NFSV4OP_CBGETATTR, NULL,
5794 0, &nfh, &nva, &cbbits, 0, p);
5796 if ((nva.na_filerev != NFS64BITSSET &&
5797 nva.na_filerev > delegfilerev) ||
5798 (NFSVNO_ISSETSIZE(&nva) &&
5799 nva.na_size != nvap->na_size)) {
5800 error = nfsvno_updfilerev(vp, nvap, nd, p);
5801 if (NFSVNO_ISSETSIZE(&nva))
5802 nvap->na_size = nva.na_size;
5805 error = 0; /* Ignore callback errors for now. */
5811 NFSEXITCODE2(error, nd);
5816 * This function looks for openowners that haven't had any opens for
5817 * a while and throws them away. Called by an nfsd when NFSNSF_NOOPENS
5821 nfsrv_throwawayopens(NFSPROC_T *p)
5823 struct nfsclient *clp, *nclp;
5824 struct nfsstate *stp, *nstp;
5828 nfsrv_stablefirst.nsf_flags &= ~NFSNSF_NOOPENS;
5830 * For each client...
5832 for (i = 0; i < nfsrv_clienthashsize; i++) {
5833 LIST_FOREACH_SAFE(clp, &nfsclienthash[i], lc_hash, nclp) {
5834 LIST_FOREACH_SAFE(stp, &clp->lc_open, ls_list, nstp) {
5835 if (LIST_EMPTY(&stp->ls_open) &&
5836 (stp->ls_noopens > NFSNOOPEN ||
5837 (nfsrv_openpluslock * 2) >
5838 nfsrv_v4statelimit))
5839 nfsrv_freeopenowner(stp, 0, p);
5847 * This function checks to see if the credentials are the same.
5848 * Returns 1 for not same, 0 otherwise.
5851 nfsrv_notsamecredname(struct nfsrv_descript *nd, struct nfsclient *clp)
5854 if (nd->nd_flag & ND_GSS) {
5855 if (!(clp->lc_flags & LCL_GSS))
5857 if (clp->lc_flags & LCL_NAME) {
5858 if (nd->nd_princlen != clp->lc_namelen ||
5859 NFSBCMP(nd->nd_principal, clp->lc_name,
5865 if (nd->nd_cred->cr_uid == clp->lc_uid)
5869 } else if (clp->lc_flags & LCL_GSS)
5872 * For AUTH_SYS, allow the same uid or root. (This is underspecified
5873 * in RFC3530, which talks about principals, but doesn't say anything
5874 * about uids for AUTH_SYS.)
5876 if (nd->nd_cred->cr_uid == clp->lc_uid || nd->nd_cred->cr_uid == 0)
5883 * Calculate the lease expiry time.
5886 nfsrv_leaseexpiry(void)
5889 if (nfsrv_stablefirst.nsf_eograce > NFSD_MONOSEC)
5890 return (NFSD_MONOSEC + 2 * (nfsrv_lease + NFSRV_LEASEDELTA));
5891 return (NFSD_MONOSEC + nfsrv_lease + NFSRV_LEASEDELTA);
5895 * Delay the delegation timeout as far as ls_delegtimelimit, as required.
5898 nfsrv_delaydelegtimeout(struct nfsstate *stp)
5901 if ((stp->ls_flags & NFSLCK_DELEGRECALL) == 0)
5904 if ((stp->ls_delegtime + 15) > NFSD_MONOSEC &&
5905 stp->ls_delegtime < stp->ls_delegtimelimit) {
5906 stp->ls_delegtime += nfsrv_lease;
5907 if (stp->ls_delegtime > stp->ls_delegtimelimit)
5908 stp->ls_delegtime = stp->ls_delegtimelimit;
5913 * This function checks to see if there is any other state associated
5914 * with the openowner for this Open.
5915 * It returns 1 if there is no other state, 0 otherwise.
5918 nfsrv_nootherstate(struct nfsstate *stp)
5920 struct nfsstate *tstp;
5922 LIST_FOREACH(tstp, &stp->ls_openowner->ls_open, ls_list) {
5923 if (tstp != stp || !LIST_EMPTY(&tstp->ls_lock))
5930 * Create a list of lock deltas (changes to local byte range locking
5931 * that can be rolled back using the list) and apply the changes via
5932 * nfsvno_advlock(). Optionally, lock the list. It is expected that either
5933 * the rollback or update function will be called after this.
5934 * It returns an error (and rolls back, as required), if any nfsvno_advlock()
5935 * call fails. If it returns an error, it will unlock the list.
5938 nfsrv_locallock(vnode_t vp, struct nfslockfile *lfp, int flags,
5939 uint64_t first, uint64_t end, struct nfslockconflict *cfp, NFSPROC_T *p)
5941 struct nfslock *lop, *nlop;
5944 /* Loop through the list of locks. */
5945 lop = LIST_FIRST(&lfp->lf_locallock);
5946 while (first < end && lop != NULL) {
5947 nlop = LIST_NEXT(lop, lo_lckowner);
5948 if (first >= lop->lo_end) {
5951 } else if (first < lop->lo_first) {
5952 /* new one starts before entry in list */
5953 if (end <= lop->lo_first) {
5954 /* no overlap between old and new */
5955 error = nfsrv_dolocal(vp, lfp, flags,
5956 NFSLCK_UNLOCK, first, end, cfp, p);
5961 /* handle fragment overlapped with new one */
5962 error = nfsrv_dolocal(vp, lfp, flags,
5963 NFSLCK_UNLOCK, first, lop->lo_first, cfp,
5967 first = lop->lo_first;
5970 /* new one overlaps this entry in list */
5971 if (end <= lop->lo_end) {
5972 /* overlaps all of new one */
5973 error = nfsrv_dolocal(vp, lfp, flags,
5974 lop->lo_flags, first, end, cfp, p);
5979 /* handle fragment overlapped with new one */
5980 error = nfsrv_dolocal(vp, lfp, flags,
5981 lop->lo_flags, first, lop->lo_end, cfp, p);
5984 first = lop->lo_end;
5989 if (first < end && error == 0)
5990 /* handle fragment past end of list */
5991 error = nfsrv_dolocal(vp, lfp, flags, NFSLCK_UNLOCK, first,
5999 * Local lock unlock. Unlock all byte ranges that are no longer locked
6000 * by NFSv4. To do this, unlock any subranges of first-->end that
6001 * do not overlap with the byte ranges of any lock in the lfp->lf_lock
6002 * list. This list has all locks for the file held by other
6003 * <clientid, lockowner> tuples. The list is ordered by increasing
6004 * lo_first value, but may have entries that overlap each other, for
6005 * the case of read locks.
6008 nfsrv_localunlock(vnode_t vp, struct nfslockfile *lfp, uint64_t init_first,
6009 uint64_t init_end, NFSPROC_T *p)
6011 struct nfslock *lop;
6012 uint64_t first, end, prevfirst __unused;
6016 while (first < init_end) {
6017 /* Loop through all nfs locks, adjusting first and end */
6019 LIST_FOREACH(lop, &lfp->lf_lock, lo_lckfile) {
6020 KASSERT(prevfirst <= lop->lo_first,
6021 ("nfsv4 locks out of order"));
6022 KASSERT(lop->lo_first < lop->lo_end,
6023 ("nfsv4 bogus lock"));
6024 prevfirst = lop->lo_first;
6025 if (first >= lop->lo_first &&
6026 first < lop->lo_end)
6028 * Overlaps with initial part, so trim
6029 * off that initial part by moving first past
6032 first = lop->lo_end;
6033 else if (end > lop->lo_first &&
6034 lop->lo_first > first) {
6036 * This lock defines the end of the
6037 * segment to unlock, so set end to the
6038 * start of it and break out of the loop.
6040 end = lop->lo_first;
6045 * There is no segment left to do, so
6046 * break out of this loop and then exit
6047 * the outer while() since first will be set
6048 * to end, which must equal init_end here.
6053 /* Unlock this segment */
6054 (void) nfsrv_dolocal(vp, lfp, NFSLCK_UNLOCK,
6055 NFSLCK_READ, first, end, NULL, p);
6056 nfsrv_locallock_commit(lfp, NFSLCK_UNLOCK,
6060 * Now move past this segment and look for any further
6061 * segment in the range, if there is one.
6069 * Do the local lock operation and update the rollback list, as required.
6070 * Perform the rollback and return the error if nfsvno_advlock() fails.
6073 nfsrv_dolocal(vnode_t vp, struct nfslockfile *lfp, int flags, int oldflags,
6074 uint64_t first, uint64_t end, struct nfslockconflict *cfp, NFSPROC_T *p)
6076 struct nfsrollback *rlp;
6077 int error = 0, ltype, oldltype;
6079 if (flags & NFSLCK_WRITE)
6081 else if (flags & NFSLCK_READ)
6085 if (oldflags & NFSLCK_WRITE)
6087 else if (oldflags & NFSLCK_READ)
6091 if (ltype == oldltype || (oldltype == F_WRLCK && ltype == F_RDLCK))
6094 error = nfsvno_advlock(vp, ltype, first, end, p);
6097 cfp->cl_clientid.lval[0] = 0;
6098 cfp->cl_clientid.lval[1] = 0;
6100 cfp->cl_end = NFS64BITSSET;
6101 cfp->cl_flags = NFSLCK_WRITE;
6102 cfp->cl_ownerlen = 5;
6103 NFSBCOPY("LOCAL", cfp->cl_owner, 5);
6105 nfsrv_locallock_rollback(vp, lfp, p);
6106 } else if (ltype != F_UNLCK) {
6107 rlp = malloc(sizeof (struct nfsrollback), M_NFSDROLLBACK,
6109 rlp->rlck_first = first;
6110 rlp->rlck_end = end;
6111 rlp->rlck_type = oldltype;
6112 LIST_INSERT_HEAD(&lfp->lf_rollback, rlp, rlck_list);
6121 * Roll back local lock changes and free up the rollback list.
6124 nfsrv_locallock_rollback(vnode_t vp, struct nfslockfile *lfp, NFSPROC_T *p)
6126 struct nfsrollback *rlp, *nrlp;
6128 LIST_FOREACH_SAFE(rlp, &lfp->lf_rollback, rlck_list, nrlp) {
6129 (void) nfsvno_advlock(vp, rlp->rlck_type, rlp->rlck_first,
6131 free(rlp, M_NFSDROLLBACK);
6133 LIST_INIT(&lfp->lf_rollback);
6137 * Update local lock list and delete rollback list (ie now committed to the
6138 * local locks). Most of the work is done by the internal function.
6141 nfsrv_locallock_commit(struct nfslockfile *lfp, int flags, uint64_t first,
6144 struct nfsrollback *rlp, *nrlp;
6145 struct nfslock *new_lop, *other_lop;
6147 new_lop = malloc(sizeof (struct nfslock), M_NFSDLOCK, M_WAITOK);
6148 if (flags & (NFSLCK_READ | NFSLCK_WRITE))
6149 other_lop = malloc(sizeof (struct nfslock), M_NFSDLOCK,
6153 new_lop->lo_flags = flags;
6154 new_lop->lo_first = first;
6155 new_lop->lo_end = end;
6156 nfsrv_updatelock(NULL, &new_lop, &other_lop, lfp);
6157 if (new_lop != NULL)
6158 free(new_lop, M_NFSDLOCK);
6159 if (other_lop != NULL)
6160 free(other_lop, M_NFSDLOCK);
6162 /* and get rid of the rollback list */
6163 LIST_FOREACH_SAFE(rlp, &lfp->lf_rollback, rlck_list, nrlp)
6164 free(rlp, M_NFSDROLLBACK);
6165 LIST_INIT(&lfp->lf_rollback);
6169 * Lock the struct nfslockfile for local lock updating.
6172 nfsrv_locklf(struct nfslockfile *lfp)
6176 /* lf_usecount ensures *lfp won't be free'd */
6179 gotlock = nfsv4_lock(&lfp->lf_locallock_lck, 1, NULL,
6180 NFSSTATEMUTEXPTR, NULL);
6181 } while (gotlock == 0);
6186 * Unlock the struct nfslockfile after local lock updating.
6189 nfsrv_unlocklf(struct nfslockfile *lfp)
6192 nfsv4_unlock(&lfp->lf_locallock_lck, 0);
6196 * Clear out all state for the NFSv4 server.
6197 * Must be called by a thread that can sleep when no nfsds are running.
6200 nfsrv_throwawayallstate(NFSPROC_T *p)
6202 struct nfsclient *clp, *nclp;
6203 struct nfslockfile *lfp, *nlfp;
6207 * For each client, clean out the state and then free the structure.
6209 for (i = 0; i < nfsrv_clienthashsize; i++) {
6210 LIST_FOREACH_SAFE(clp, &nfsclienthash[i], lc_hash, nclp) {
6211 nfsrv_cleanclient(clp, p);
6212 nfsrv_freedeleglist(&clp->lc_deleg);
6213 nfsrv_freedeleglist(&clp->lc_olddeleg);
6214 free(clp->lc_stateid, M_NFSDCLIENT);
6215 free(clp, M_NFSDCLIENT);
6220 * Also, free up any remaining lock file structures.
6222 for (i = 0; i < nfsrv_lockhashsize; i++) {
6223 LIST_FOREACH_SAFE(lfp, &nfslockhash[i], lf_hash, nlfp) {
6224 printf("nfsd unload: fnd a lock file struct\n");
6225 nfsrv_freenfslockfile(lfp);
6229 /* And get rid of the deviceid structures and layouts. */
6230 nfsrv_freealllayoutsanddevids();
6234 * Check the sequence# for the session and slot provided as an argument.
6235 * Also, renew the lease if the session will return NFS_OK.
6238 nfsrv_checksequence(struct nfsrv_descript *nd, uint32_t sequenceid,
6239 uint32_t *highest_slotidp, uint32_t *target_highest_slotidp, int cache_this,
6240 uint32_t *sflagsp, NFSPROC_T *p)
6242 struct nfsdsession *sep;
6243 struct nfssessionhash *shp;
6246 shp = NFSSESSIONHASH(nd->nd_sessionid);
6247 NFSLOCKSESSION(shp);
6248 sep = nfsrv_findsession(nd->nd_sessionid);
6250 NFSUNLOCKSESSION(shp);
6251 return (NFSERR_BADSESSION);
6253 error = nfsv4_seqsession(sequenceid, nd->nd_slotid, *highest_slotidp,
6254 sep->sess_slots, NULL, NFSV4_SLOTS - 1);
6256 NFSUNLOCKSESSION(shp);
6259 if (cache_this != 0)
6260 nd->nd_flag |= ND_SAVEREPLY;
6261 /* Renew the lease. */
6262 sep->sess_clp->lc_expiry = nfsrv_leaseexpiry();
6263 nd->nd_clientid.qval = sep->sess_clp->lc_clientid.qval;
6264 nd->nd_flag |= ND_IMPLIEDCLID;
6266 /* Save maximum request and reply sizes. */
6267 nd->nd_maxreq = sep->sess_maxreq;
6268 nd->nd_maxresp = sep->sess_maxresp;
6271 if (sep->sess_clp->lc_req.nr_client == NULL ||
6272 (sep->sess_clp->lc_flags & LCL_CBDOWN) != 0)
6273 *sflagsp |= NFSV4SEQ_CBPATHDOWN;
6274 NFSUNLOCKSESSION(shp);
6275 if (error == NFSERR_EXPIRED) {
6276 *sflagsp |= NFSV4SEQ_EXPIREDALLSTATEREVOKED;
6278 } else if (error == NFSERR_ADMINREVOKED) {
6279 *sflagsp |= NFSV4SEQ_ADMINSTATEREVOKED;
6282 *highest_slotidp = *target_highest_slotidp = NFSV4_SLOTS - 1;
6287 * Check/set reclaim complete for this session/clientid.
6290 nfsrv_checkreclaimcomplete(struct nfsrv_descript *nd, int onefs)
6292 struct nfsdsession *sep;
6293 struct nfssessionhash *shp;
6296 shp = NFSSESSIONHASH(nd->nd_sessionid);
6298 NFSLOCKSESSION(shp);
6299 sep = nfsrv_findsession(nd->nd_sessionid);
6301 NFSUNLOCKSESSION(shp);
6303 return (NFSERR_BADSESSION);
6307 sep->sess_clp->lc_flags |= LCL_RECLAIMONEFS;
6308 /* Check to see if reclaim complete has already happened. */
6309 else if ((sep->sess_clp->lc_flags & LCL_RECLAIMCOMPLETE) != 0)
6310 error = NFSERR_COMPLETEALREADY;
6312 sep->sess_clp->lc_flags |= LCL_RECLAIMCOMPLETE;
6313 nfsrv_markreclaim(sep->sess_clp);
6315 NFSUNLOCKSESSION(shp);
6321 * Cache the reply in a session slot.
6324 nfsrv_cache_session(struct nfsrv_descript *nd, struct mbuf **m)
6326 struct nfsdsession *sep;
6327 struct nfssessionhash *shp;
6330 struct sockaddr_in *sin;
6333 struct sockaddr_in6 *sin6;
6336 shp = NFSSESSIONHASH(nd->nd_sessionid);
6337 NFSLOCKSESSION(shp);
6338 sep = nfsrv_findsession(nd->nd_sessionid);
6340 NFSUNLOCKSESSION(shp);
6341 if ((nfsrv_stablefirst.nsf_flags & NFSNSF_GRACEOVER) != 0) {
6342 buf = malloc(INET6_ADDRSTRLEN, M_TEMP, M_WAITOK);
6343 switch (nd->nd_nam->sa_family) {
6346 sin = (struct sockaddr_in *)nd->nd_nam;
6347 cp = inet_ntop(sin->sin_family,
6348 &sin->sin_addr.s_addr, buf,
6354 sin6 = (struct sockaddr_in6 *)nd->nd_nam;
6355 cp = inet_ntop(sin6->sin6_family,
6356 &sin6->sin6_addr, buf, INET6_ADDRSTRLEN);
6363 printf("nfsrv_cache_session: no session "
6366 printf("nfsrv_cache_session: no session\n");
6372 nfsv4_seqsess_cacherep(nd->nd_slotid, sep->sess_slots, nd->nd_repstat,
6374 NFSUNLOCKSESSION(shp);
6378 * Search for a session that matches the sessionid.
6380 static struct nfsdsession *
6381 nfsrv_findsession(uint8_t *sessionid)
6383 struct nfsdsession *sep;
6384 struct nfssessionhash *shp;
6386 shp = NFSSESSIONHASH(sessionid);
6387 LIST_FOREACH(sep, &shp->list, sess_hash) {
6388 if (!NFSBCMP(sessionid, sep->sess_sessionid, NFSX_V4SESSIONID))
6395 * Destroy a session.
6398 nfsrv_destroysession(struct nfsrv_descript *nd, uint8_t *sessionid)
6400 int error, igotlock, samesess;
6403 if (!NFSBCMP(sessionid, nd->nd_sessionid, NFSX_V4SESSIONID) &&
6404 (nd->nd_flag & ND_HASSEQUENCE) != 0) {
6406 if ((nd->nd_flag & ND_LASTOP) == 0)
6407 return (NFSERR_BADSESSION);
6410 /* Lock out other nfsd threads */
6411 NFSLOCKV4ROOTMUTEX();
6412 nfsv4_relref(&nfsv4rootfs_lock);
6414 igotlock = nfsv4_lock(&nfsv4rootfs_lock, 1, NULL,
6415 NFSV4ROOTLOCKMUTEXPTR, NULL);
6416 } while (igotlock == 0);
6417 NFSUNLOCKV4ROOTMUTEX();
6419 error = nfsrv_freesession(NULL, sessionid);
6420 if (error == 0 && samesess != 0)
6421 nd->nd_flag &= ~ND_HASSEQUENCE;
6423 NFSLOCKV4ROOTMUTEX();
6424 nfsv4_unlock(&nfsv4rootfs_lock, 1);
6425 NFSUNLOCKV4ROOTMUTEX();
6430 * Bind a connection to a session.
6431 * For now, only certain variants are supported, since the current session
6432 * structure can only handle a single backchannel entry, which will be
6433 * applied to all connections if it is set.
6436 nfsrv_bindconnsess(struct nfsrv_descript *nd, uint8_t *sessionid, int *foreaftp)
6438 struct nfssessionhash *shp;
6439 struct nfsdsession *sep;
6440 struct nfsclient *clp;
6446 shp = NFSSESSIONHASH(sessionid);
6448 NFSLOCKSESSION(shp);
6449 sep = nfsrv_findsession(sessionid);
6451 clp = sep->sess_clp;
6452 if (*foreaftp == NFSCDFC4_BACK ||
6453 *foreaftp == NFSCDFC4_BACK_OR_BOTH ||
6454 *foreaftp == NFSCDFC4_FORE_OR_BOTH) {
6455 /* Try to set up a backchannel. */
6456 if (clp->lc_req.nr_client == NULL) {
6457 NFSD_DEBUG(2, "nfsrv_bindconnsess: acquire "
6459 clp->lc_req.nr_client = (struct __rpc_client *)
6460 clnt_bck_create(nd->nd_xprt->xp_socket,
6461 sep->sess_cbprogram, NFSV4_CBVERS);
6463 if (clp->lc_req.nr_client != NULL) {
6464 NFSD_DEBUG(2, "nfsrv_bindconnsess: set up "
6466 savxprt = sep->sess_cbsess.nfsess_xprt;
6467 SVC_ACQUIRE(nd->nd_xprt);
6468 CLNT_ACQUIRE(clp->lc_req.nr_client);
6469 nd->nd_xprt->xp_p2 = clp->lc_req.nr_client;
6470 /* Disable idle timeout. */
6471 nd->nd_xprt->xp_idletimeout = 0;
6472 sep->sess_cbsess.nfsess_xprt = nd->nd_xprt;
6473 sep->sess_crflags |= NFSV4CRSESS_CONNBACKCHAN;
6474 clp->lc_flags |= LCL_DONEBINDCONN |
6476 clp->lc_flags &= ~LCL_CBDOWN;
6477 if (*foreaftp == NFSCDFS4_BACK)
6478 *foreaftp = NFSCDFS4_BACK;
6480 *foreaftp = NFSCDFS4_BOTH;
6481 } else if (*foreaftp != NFSCDFC4_BACK) {
6482 NFSD_DEBUG(2, "nfsrv_bindconnsess: can't set "
6483 "up backchannel\n");
6484 sep->sess_crflags &= ~NFSV4CRSESS_CONNBACKCHAN;
6485 clp->lc_flags |= LCL_DONEBINDCONN;
6486 *foreaftp = NFSCDFS4_FORE;
6488 error = NFSERR_NOTSUPP;
6489 printf("nfsrv_bindconnsess: Can't add "
6493 NFSD_DEBUG(2, "nfsrv_bindconnsess: Set forechannel\n");
6494 clp->lc_flags |= LCL_DONEBINDCONN;
6495 *foreaftp = NFSCDFS4_FORE;
6498 error = NFSERR_BADSESSION;
6499 NFSUNLOCKSESSION(shp);
6501 if (savxprt != NULL)
6502 SVC_RELEASE(savxprt);
6507 * Free up a session structure.
6510 nfsrv_freesession(struct nfsdsession *sep, uint8_t *sessionid)
6512 struct nfssessionhash *shp;
6517 shp = NFSSESSIONHASH(sessionid);
6518 NFSLOCKSESSION(shp);
6519 sep = nfsrv_findsession(sessionid);
6521 shp = NFSSESSIONHASH(sep->sess_sessionid);
6522 NFSLOCKSESSION(shp);
6526 if (sep->sess_refcnt > 0) {
6527 NFSUNLOCKSESSION(shp);
6529 return (NFSERR_BACKCHANBUSY);
6531 LIST_REMOVE(sep, sess_hash);
6532 LIST_REMOVE(sep, sess_list);
6534 NFSUNLOCKSESSION(shp);
6537 return (NFSERR_BADSESSION);
6538 for (i = 0; i < NFSV4_SLOTS; i++)
6539 if (sep->sess_slots[i].nfssl_reply != NULL)
6540 m_freem(sep->sess_slots[i].nfssl_reply);
6541 if (sep->sess_cbsess.nfsess_xprt != NULL)
6542 SVC_RELEASE(sep->sess_cbsess.nfsess_xprt);
6543 free(sep, M_NFSDSESSION);
6549 * RFC5661 says that it should fail when there are associated opens, locks
6550 * or delegations. Since stateids represent opens, I don't see how you can
6551 * free an open stateid (it will be free'd when closed), so this function
6552 * only works for lock stateids (freeing the lock_owner) or delegations.
6555 nfsrv_freestateid(struct nfsrv_descript *nd, nfsv4stateid_t *stateidp,
6558 struct nfsclient *clp;
6559 struct nfsstate *stp;
6564 * Look up the stateid
6566 error = nfsrv_getclient((nfsquad_t)((u_quad_t)0), CLOPS_RENEW, &clp,
6567 NULL, (nfsquad_t)((u_quad_t)0), 0, nd, p);
6569 /* First, check for a delegation. */
6570 LIST_FOREACH(stp, &clp->lc_deleg, ls_list) {
6571 if (!NFSBCMP(stp->ls_stateid.other, stateidp->other,
6576 nfsrv_freedeleg(stp);
6581 /* Not a delegation, try for a lock_owner. */
6583 error = nfsrv_getstate(clp, stateidp, 0, &stp);
6584 if (error == 0 && ((stp->ls_flags & (NFSLCK_OPEN | NFSLCK_DELEGREAD |
6585 NFSLCK_DELEGWRITE)) != 0 || (stp->ls_flags & NFSLCK_LOCK) == 0))
6586 /* Not a lock_owner stateid. */
6587 error = NFSERR_LOCKSHELD;
6588 if (error == 0 && !LIST_EMPTY(&stp->ls_lock))
6589 error = NFSERR_LOCKSHELD;
6591 nfsrv_freelockowner(stp, NULL, 0, p);
6600 nfsrv_teststateid(struct nfsrv_descript *nd, nfsv4stateid_t *stateidp,
6603 struct nfsclient *clp;
6604 struct nfsstate *stp;
6609 * Look up the stateid
6611 error = nfsrv_getclient((nfsquad_t)((u_quad_t)0), CLOPS_RENEW, &clp,
6612 NULL, (nfsquad_t)((u_quad_t)0), 0, nd, p);
6614 error = nfsrv_getstate(clp, stateidp, 0, &stp);
6615 if (error == 0 && stateidp->seqid != 0 &&
6616 SEQ_LT(stateidp->seqid, stp->ls_stateid.seqid))
6617 error = NFSERR_OLDSTATEID;
6623 * Generate the xdr for an NFSv4.1 CBSequence Operation.
6626 nfsv4_setcbsequence(struct nfsrv_descript *nd, struct nfsclient *clp,
6627 int dont_replycache, struct nfsdsession **sepp, int *slotposp)
6629 struct nfsdsession *sep;
6630 uint32_t *tl, slotseq = 0;
6632 uint8_t sessionid[NFSX_V4SESSIONID];
6635 error = nfsv4_getcbsession(clp, sepp);
6639 (void)nfsv4_sequencelookup(NULL, &sep->sess_cbsess, slotposp, &maxslot,
6640 &slotseq, sessionid);
6641 KASSERT(maxslot >= 0, ("nfsv4_setcbsequence neg maxslot"));
6643 /* Build the Sequence arguments. */
6644 NFSM_BUILD(tl, uint32_t *, NFSX_V4SESSIONID + 5 * NFSX_UNSIGNED);
6645 bcopy(sessionid, tl, NFSX_V4SESSIONID);
6646 tl += NFSX_V4SESSIONID / NFSX_UNSIGNED;
6647 nd->nd_slotseq = tl;
6648 *tl++ = txdr_unsigned(slotseq);
6649 *tl++ = txdr_unsigned(*slotposp);
6650 *tl++ = txdr_unsigned(maxslot);
6651 if (dont_replycache == 0)
6652 *tl++ = newnfs_true;
6654 *tl++ = newnfs_false;
6655 *tl = 0; /* No referring call list, for now. */
6656 nd->nd_flag |= ND_HASSEQUENCE;
6661 * Get a session for the callback.
6664 nfsv4_getcbsession(struct nfsclient *clp, struct nfsdsession **sepp)
6666 struct nfsdsession *sep;
6669 LIST_FOREACH(sep, &clp->lc_session, sess_list) {
6670 if ((sep->sess_crflags & NFSV4CRSESS_CONNBACKCHAN) != 0)
6675 return (NFSERR_BADSESSION);
6684 * Free up all backchannel xprts. This needs to be done when the nfsd threads
6685 * exit, since those transports will all be going away.
6686 * This is only called after all the nfsd threads are done performing RPCs,
6687 * so locking shouldn't be an issue.
6690 nfsrv_freeallbackchannel_xprts(void)
6692 struct nfsdsession *sep;
6693 struct nfsclient *clp;
6697 for (i = 0; i < nfsrv_clienthashsize; i++) {
6698 LIST_FOREACH(clp, &nfsclienthash[i], lc_hash) {
6699 LIST_FOREACH(sep, &clp->lc_session, sess_list) {
6700 xprt = sep->sess_cbsess.nfsess_xprt;
6701 sep->sess_cbsess.nfsess_xprt = NULL;
6710 * Do a layout commit. Actually just call nfsrv_updatemdsattr().
6711 * I have no idea if the rest of these arguments will ever be useful?
6714 nfsrv_layoutcommit(struct nfsrv_descript *nd, vnode_t vp, int layouttype,
6715 int hasnewoff, uint64_t newoff, uint64_t offset, uint64_t len,
6716 int hasnewmtime, struct timespec *newmtimep, int reclaim,
6717 nfsv4stateid_t *stateidp, int maxcnt, char *layp, int *hasnewsizep,
6718 uint64_t *newsizep, struct ucred *cred, NFSPROC_T *p)
6723 error = nfsrv_updatemdsattr(vp, &na, p);
6726 *newsizep = na.na_size;
6732 * Try and get a layout.
6735 nfsrv_layoutget(struct nfsrv_descript *nd, vnode_t vp, struct nfsexstuff *exp,
6736 int layouttype, int *iomode, uint64_t *offset, uint64_t *len,
6737 uint64_t minlen, nfsv4stateid_t *stateidp, int maxcnt, int *retonclose,
6738 int *layoutlenp, char *layp, struct ucred *cred, NFSPROC_T *p)
6740 struct nfslayouthash *lhyp;
6741 struct nfslayout *lyp;
6743 fhandle_t fh, *dsfhp;
6744 int error, mirrorcnt;
6746 if (nfsrv_devidcnt == 0)
6747 return (NFSERR_UNKNLAYOUTTYPE);
6750 printf("nfsrv_layoutget: off=%ju len=%ju\n", (uintmax_t)*offset,
6752 error = nfsvno_getfh(vp, &fh, p);
6753 NFSD_DEBUG(4, "layoutget getfh=%d\n", error);
6758 * For now, all layouts are for entire files.
6759 * Only issue Read/Write layouts if requested for a non-readonly fs.
6761 if (NFSVNO_EXRDONLY(exp)) {
6762 if (*iomode == NFSLAYOUTIOMODE_RW)
6763 return (NFSERR_LAYOUTTRYLATER);
6764 *iomode = NFSLAYOUTIOMODE_READ;
6766 if (*iomode != NFSLAYOUTIOMODE_RW)
6767 *iomode = NFSLAYOUTIOMODE_READ;
6770 * Check to see if a write layout can be issued for this file.
6771 * This is used during mirror recovery to avoid RW layouts being
6772 * issued for a file while it is being copied to the recovered
6775 if (*iomode == NFSLAYOUTIOMODE_RW && nfsrv_dontlayout(&fh) != 0)
6776 return (NFSERR_LAYOUTTRYLATER);
6782 /* First, see if a layout already exists and return if found. */
6783 lhyp = NFSLAYOUTHASH(&fh);
6784 NFSLOCKLAYOUT(lhyp);
6785 error = nfsrv_findlayout(&nd->nd_clientid, &fh, layouttype, p, &lyp);
6786 NFSD_DEBUG(4, "layoutget findlay=%d\n", error);
6788 * Not sure if the seqid must be the same, so I won't check it.
6790 if (error == 0 && (stateidp->other[0] != lyp->lay_stateid.other[0] ||
6791 stateidp->other[1] != lyp->lay_stateid.other[1] ||
6792 stateidp->other[2] != lyp->lay_stateid.other[2])) {
6793 if ((lyp->lay_flags & NFSLAY_CALLB) == 0) {
6794 NFSUNLOCKLAYOUT(lhyp);
6795 NFSD_DEBUG(1, "ret bad stateid\n");
6796 return (NFSERR_BADSTATEID);
6799 * I believe we get here because there is a race between
6800 * the client processing the CBLAYOUTRECALL and the layout
6801 * being deleted here on the server.
6802 * The client has now done a LayoutGet with a non-layout
6803 * stateid, as it would when there is no layout.
6804 * As such, free this layout and set error == NFSERR_BADSTATEID
6805 * so the code below will create a new layout structure as
6806 * would happen if no layout was found.
6807 * "lyp" will be set before being used below, but set it NULL
6810 nfsrv_freelayout(&lhyp->list, lyp);
6812 error = NFSERR_BADSTATEID;
6815 if (lyp->lay_layoutlen > maxcnt) {
6816 NFSUNLOCKLAYOUT(lhyp);
6817 NFSD_DEBUG(1, "ret layout too small\n");
6818 return (NFSERR_TOOSMALL);
6820 if (*iomode == NFSLAYOUTIOMODE_RW)
6821 lyp->lay_flags |= NFSLAY_RW;
6823 lyp->lay_flags |= NFSLAY_READ;
6824 NFSBCOPY(lyp->lay_xdr, layp, lyp->lay_layoutlen);
6825 *layoutlenp = lyp->lay_layoutlen;
6826 if (++lyp->lay_stateid.seqid == 0)
6827 lyp->lay_stateid.seqid = 1;
6828 stateidp->seqid = lyp->lay_stateid.seqid;
6829 NFSUNLOCKLAYOUT(lhyp);
6830 NFSD_DEBUG(4, "ret fnd layout\n");
6833 NFSUNLOCKLAYOUT(lhyp);
6835 /* Find the device id and file handle. */
6836 dsfhp = malloc(sizeof(fhandle_t) * NFSDEV_MAXMIRRORS, M_TEMP, M_WAITOK);
6837 devid = malloc(NFSX_V4DEVICEID * NFSDEV_MAXMIRRORS, M_TEMP, M_WAITOK);
6838 error = nfsrv_dsgetdevandfh(vp, p, &mirrorcnt, dsfhp, devid);
6839 NFSD_DEBUG(4, "layoutget devandfh=%d\n", error);
6841 if (layouttype == NFSLAYOUT_NFSV4_1_FILES) {
6842 if (NFSX_V4FILELAYOUT > maxcnt)
6843 error = NFSERR_TOOSMALL;
6845 lyp = nfsrv_filelayout(nd, *iomode, &fh, dsfhp,
6846 devid, vp->v_mount->mnt_stat.f_fsid);
6848 if (NFSX_V4FLEXLAYOUT(mirrorcnt) > maxcnt)
6849 error = NFSERR_TOOSMALL;
6851 lyp = nfsrv_flexlayout(nd, *iomode, mirrorcnt,
6853 vp->v_mount->mnt_stat.f_fsid);
6856 free(dsfhp, M_TEMP);
6857 free(devid, M_TEMP);
6862 * Now, add this layout to the list.
6864 error = nfsrv_addlayout(nd, &lyp, stateidp, layp, layoutlenp, p);
6865 NFSD_DEBUG(4, "layoutget addl=%d\n", error);
6867 * The lyp will be set to NULL by nfsrv_addlayout() if it
6868 * linked the new structure into the lists.
6870 free(lyp, M_NFSDSTATE);
6875 * Generate a File Layout.
6877 static struct nfslayout *
6878 nfsrv_filelayout(struct nfsrv_descript *nd, int iomode, fhandle_t *fhp,
6879 fhandle_t *dsfhp, char *devid, fsid_t fs)
6882 struct nfslayout *lyp;
6883 uint64_t pattern_offset;
6885 lyp = malloc(sizeof(struct nfslayout) + NFSX_V4FILELAYOUT, M_NFSDSTATE,
6887 lyp->lay_type = NFSLAYOUT_NFSV4_1_FILES;
6888 if (iomode == NFSLAYOUTIOMODE_RW)
6889 lyp->lay_flags = NFSLAY_RW;
6891 lyp->lay_flags = NFSLAY_READ;
6892 NFSBCOPY(fhp, &lyp->lay_fh, sizeof(*fhp));
6893 lyp->lay_clientid.qval = nd->nd_clientid.qval;
6896 /* Fill in the xdr for the files layout. */
6897 tl = (uint32_t *)lyp->lay_xdr;
6898 NFSBCOPY(devid, tl, NFSX_V4DEVICEID); /* Device ID. */
6899 tl += (NFSX_V4DEVICEID / NFSX_UNSIGNED);
6901 /* Set the stripe size to the maximum I/O size. */
6902 *tl++ = txdr_unsigned(nfs_srvmaxio & NFSFLAYUTIL_STRIPE_MASK);
6903 *tl++ = 0; /* 1st stripe index. */
6905 txdr_hyper(pattern_offset, tl); tl += 2; /* Pattern offset. */
6906 *tl++ = txdr_unsigned(1); /* 1 file handle. */
6907 *tl++ = txdr_unsigned(NFSX_V4PNFSFH);
6908 NFSBCOPY(dsfhp, tl, sizeof(*dsfhp));
6909 lyp->lay_layoutlen = NFSX_V4FILELAYOUT;
6913 #define FLEX_OWNERID "999"
6914 #define FLEX_UID0 "0"
6916 * Generate a Flex File Layout.
6917 * The FLEX_OWNERID can be any string of 3 decimal digits. Although this
6918 * string goes on the wire, it isn't supposed to be used by the client,
6919 * since this server uses tight coupling.
6920 * Although not recommended by the spec., if vfs.nfsd.flexlinuxhack=1 use
6921 * a string of "0". This works around the Linux Flex File Layout driver bug
6922 * which uses the synthetic uid/gid strings for the "tightly coupled" case.
6924 static struct nfslayout *
6925 nfsrv_flexlayout(struct nfsrv_descript *nd, int iomode, int mirrorcnt,
6926 fhandle_t *fhp, fhandle_t *dsfhp, char *devid, fsid_t fs)
6929 struct nfslayout *lyp;
6933 lyp = malloc(sizeof(struct nfslayout) + NFSX_V4FLEXLAYOUT(mirrorcnt),
6934 M_NFSDSTATE, M_WAITOK | M_ZERO);
6935 lyp->lay_type = NFSLAYOUT_FLEXFILE;
6936 if (iomode == NFSLAYOUTIOMODE_RW)
6937 lyp->lay_flags = NFSLAY_RW;
6939 lyp->lay_flags = NFSLAY_READ;
6940 NFSBCOPY(fhp, &lyp->lay_fh, sizeof(*fhp));
6941 lyp->lay_clientid.qval = nd->nd_clientid.qval;
6943 lyp->lay_mirrorcnt = mirrorcnt;
6945 /* Fill in the xdr for the files layout. */
6946 tl = (uint32_t *)lyp->lay_xdr;
6948 txdr_hyper(lenval, tl); tl += 2; /* Stripe unit. */
6949 *tl++ = txdr_unsigned(mirrorcnt); /* # of mirrors. */
6950 for (i = 0; i < mirrorcnt; i++) {
6951 *tl++ = txdr_unsigned(1); /* One stripe. */
6952 NFSBCOPY(devid, tl, NFSX_V4DEVICEID); /* Device ID. */
6953 tl += (NFSX_V4DEVICEID / NFSX_UNSIGNED);
6954 devid += NFSX_V4DEVICEID;
6955 *tl++ = txdr_unsigned(1); /* Efficiency. */
6956 *tl++ = 0; /* Proxy Stateid. */
6960 *tl++ = txdr_unsigned(1); /* 1 file handle. */
6961 *tl++ = txdr_unsigned(NFSX_V4PNFSFH);
6962 NFSBCOPY(dsfhp, tl, sizeof(*dsfhp));
6963 tl += (NFSM_RNDUP(NFSX_V4PNFSFH) / NFSX_UNSIGNED);
6965 if (nfsrv_flexlinuxhack != 0) {
6966 *tl++ = txdr_unsigned(strlen(FLEX_UID0));
6967 *tl = 0; /* 0 pad string. */
6968 NFSBCOPY(FLEX_UID0, tl++, strlen(FLEX_UID0));
6969 *tl++ = txdr_unsigned(strlen(FLEX_UID0));
6970 *tl = 0; /* 0 pad string. */
6971 NFSBCOPY(FLEX_UID0, tl++, strlen(FLEX_UID0));
6973 *tl++ = txdr_unsigned(strlen(FLEX_OWNERID));
6974 NFSBCOPY(FLEX_OWNERID, tl++, NFSX_UNSIGNED);
6975 *tl++ = txdr_unsigned(strlen(FLEX_OWNERID));
6976 NFSBCOPY(FLEX_OWNERID, tl++, NFSX_UNSIGNED);
6979 *tl++ = txdr_unsigned(0); /* ff_flags. */
6980 *tl = txdr_unsigned(60); /* Status interval hint. */
6981 lyp->lay_layoutlen = NFSX_V4FLEXLAYOUT(mirrorcnt);
6986 * Parse and process Flex File errors returned via LayoutReturn.
6989 nfsrv_flexlayouterr(struct nfsrv_descript *nd, uint32_t *layp, int maxcnt,
6993 int cnt, errcnt, i, j, opnum, stat;
6994 char devid[NFSX_V4DEVICEID];
6997 cnt = fxdr_unsigned(int, *tl++);
6998 NFSD_DEBUG(4, "flexlayouterr cnt=%d\n", cnt);
6999 for (i = 0; i < cnt; i++) {
7000 /* Skip offset, length and stateid for now. */
7001 tl += (4 + NFSX_STATEID / NFSX_UNSIGNED);
7002 errcnt = fxdr_unsigned(int, *tl++);
7003 NFSD_DEBUG(4, "flexlayouterr errcnt=%d\n", errcnt);
7004 for (j = 0; j < errcnt; j++) {
7005 NFSBCOPY(tl, devid, NFSX_V4DEVICEID);
7006 tl += (NFSX_V4DEVICEID / NFSX_UNSIGNED);
7007 stat = fxdr_unsigned(int, *tl++);
7008 opnum = fxdr_unsigned(int, *tl++);
7009 NFSD_DEBUG(4, "flexlayouterr op=%d stat=%d\n", opnum,
7012 * Except for NFSERR_ACCES and NFSERR_STALE errors,
7013 * disable the mirror.
7015 if (stat != NFSERR_ACCES && stat != NFSERR_STALE)
7016 nfsrv_delds(devid, p);
7022 * This function removes all flex file layouts which has a mirror with
7023 * a device id that matches the argument.
7024 * Called when the DS represented by the device id has failed.
7027 nfsrv_flexmirrordel(char *devid, NFSPROC_T *p)
7030 struct nfslayout *lyp, *nlyp;
7031 struct nfslayouthash *lhyp;
7032 struct nfslayouthead loclyp;
7035 NFSD_DEBUG(4, "flexmirrordel\n");
7036 /* Move all layouts found onto a local list. */
7037 TAILQ_INIT(&loclyp);
7038 for (i = 0; i < nfsrv_layouthashsize; i++) {
7039 lhyp = &nfslayouthash[i];
7040 NFSLOCKLAYOUT(lhyp);
7041 TAILQ_FOREACH_SAFE(lyp, &lhyp->list, lay_list, nlyp) {
7042 if (lyp->lay_type == NFSLAYOUT_FLEXFILE &&
7043 lyp->lay_mirrorcnt > 1) {
7044 NFSD_DEBUG(4, "possible match\n");
7047 for (j = 0; j < lyp->lay_mirrorcnt; j++) {
7049 if (NFSBCMP(devid, tl, NFSX_V4DEVICEID)
7052 NFSD_DEBUG(4, "fnd one\n");
7053 TAILQ_REMOVE(&lhyp->list, lyp,
7055 TAILQ_INSERT_HEAD(&loclyp, lyp,
7059 tl += (NFSX_V4DEVICEID / NFSX_UNSIGNED +
7060 NFSM_RNDUP(NFSX_V4PNFSFH) /
7061 NFSX_UNSIGNED + 11 * NFSX_UNSIGNED);
7065 NFSUNLOCKLAYOUT(lhyp);
7068 /* Now, try to do a Layout recall for each one found. */
7069 TAILQ_FOREACH_SAFE(lyp, &loclyp, lay_list, nlyp) {
7070 NFSD_DEBUG(4, "do layout recall\n");
7072 * The layout stateid.seqid needs to be incremented
7073 * before doing a LAYOUT_RECALL callback.
7075 if (++lyp->lay_stateid.seqid == 0)
7076 lyp->lay_stateid.seqid = 1;
7077 nfsrv_recalllayout(lyp->lay_clientid, &lyp->lay_stateid,
7078 &lyp->lay_fh, lyp, 1, lyp->lay_type, p);
7079 nfsrv_freelayout(&loclyp, lyp);
7084 * Do a recall callback to the client for this layout.
7087 nfsrv_recalllayout(nfsquad_t clid, nfsv4stateid_t *stateidp, fhandle_t *fhp,
7088 struct nfslayout *lyp, int changed, int laytype, NFSPROC_T *p)
7090 struct nfsclient *clp;
7093 NFSD_DEBUG(4, "nfsrv_recalllayout\n");
7094 error = nfsrv_getclient(clid, 0, &clp, NULL, (nfsquad_t)((u_quad_t)0),
7096 NFSD_DEBUG(4, "aft nfsrv_getclient=%d\n", error);
7098 printf("nfsrv_recalllayout: getclient err=%d\n", error);
7101 if ((clp->lc_flags & LCL_NFSV41) != 0) {
7102 error = nfsrv_docallback(clp, NFSV4OP_CBLAYOUTRECALL,
7103 stateidp, changed, fhp, NULL, NULL, laytype, p);
7104 /* If lyp != NULL, handle an error return here. */
7105 if (error != 0 && lyp != NULL) {
7108 * Mark it returned, since no layout recall
7110 * All errors seem to be non-recoverable, although
7111 * NFSERR_NOMATCHLAYOUT is a normal event.
7113 if ((lyp->lay_flags & NFSLAY_RECALL) != 0) {
7114 lyp->lay_flags |= NFSLAY_RETURNED;
7118 if (error != NFSERR_NOMATCHLAYOUT)
7119 printf("nfsrv_recalllayout: err=%d\n", error);
7122 printf("nfsrv_recalllayout: clp not NFSv4.1\n");
7127 * Find a layout to recall when we exceed our high water mark.
7130 nfsrv_recalloldlayout(NFSPROC_T *p)
7132 struct nfslayouthash *lhyp;
7133 struct nfslayout *lyp;
7135 nfsv4stateid_t stateid;
7137 int error, laytype = 0, ret;
7139 lhyp = &nfslayouthash[arc4random() % nfsrv_layouthashsize];
7140 NFSLOCKLAYOUT(lhyp);
7141 TAILQ_FOREACH_REVERSE(lyp, &lhyp->list, nfslayouthead, lay_list) {
7142 if ((lyp->lay_flags & NFSLAY_CALLB) == 0) {
7143 lyp->lay_flags |= NFSLAY_CALLB;
7145 * The layout stateid.seqid needs to be incremented
7146 * before doing a LAYOUT_RECALL callback.
7148 if (++lyp->lay_stateid.seqid == 0)
7149 lyp->lay_stateid.seqid = 1;
7150 clientid = lyp->lay_clientid;
7151 stateid = lyp->lay_stateid;
7152 NFSBCOPY(&lyp->lay_fh, &fh, sizeof(fh));
7153 laytype = lyp->lay_type;
7157 NFSUNLOCKLAYOUT(lhyp);
7159 error = nfsrv_recalllayout(clientid, &stateid, &fh, NULL, 0,
7161 if (error != 0 && error != NFSERR_NOMATCHLAYOUT)
7162 NFSD_DEBUG(4, "recallold=%d\n", error);
7164 NFSLOCKLAYOUT(lhyp);
7166 * Since the hash list was unlocked, we need to
7169 ret = nfsrv_findlayout(&clientid, &fh, laytype, p,
7172 (lyp->lay_flags & NFSLAY_CALLB) != 0 &&
7173 lyp->lay_stateid.other[0] == stateid.other[0] &&
7174 lyp->lay_stateid.other[1] == stateid.other[1] &&
7175 lyp->lay_stateid.other[2] == stateid.other[2]) {
7177 * The client no longer knows this layout, so
7178 * it can be free'd now.
7180 if (error == NFSERR_NOMATCHLAYOUT)
7181 nfsrv_freelayout(&lhyp->list, lyp);
7184 * Leave it to be tried later by
7185 * clearing NFSLAY_CALLB and moving
7186 * it to the head of the list, so it
7187 * won't be tried again for a while.
7189 lyp->lay_flags &= ~NFSLAY_CALLB;
7190 TAILQ_REMOVE(&lhyp->list, lyp,
7192 TAILQ_INSERT_HEAD(&lhyp->list, lyp,
7196 NFSUNLOCKLAYOUT(lhyp);
7202 * Try and return layout(s).
7205 nfsrv_layoutreturn(struct nfsrv_descript *nd, vnode_t vp,
7206 int layouttype, int iomode, uint64_t offset, uint64_t len, int reclaim,
7207 int kind, nfsv4stateid_t *stateidp, int maxcnt, uint32_t *layp, int *fndp,
7208 struct ucred *cred, NFSPROC_T *p)
7211 struct nfslayouthash *lhyp;
7212 struct nfslayout *lyp;
7217 if (kind == NFSV4LAYOUTRET_FILE) {
7218 error = nfsvno_getfh(vp, &fh, p);
7220 error = nfsrv_updatemdsattr(vp, &na, p);
7222 printf("nfsrv_layoutreturn: updatemdsattr"
7223 " failed=%d\n", error);
7226 if (reclaim == newnfs_true) {
7227 error = nfsrv_checkgrace(NULL, NULL,
7229 if (error != NFSERR_NOGRACE)
7233 lhyp = NFSLAYOUTHASH(&fh);
7235 NFSLOCKLAYOUT(lhyp);
7236 error = nfsrv_findlayout(&nd->nd_clientid, &fh,
7237 layouttype, p, &lyp);
7238 NFSD_DEBUG(4, "layoutret findlay=%d\n", error);
7240 stateidp->other[0] == lyp->lay_stateid.other[0] &&
7241 stateidp->other[1] == lyp->lay_stateid.other[1] &&
7242 stateidp->other[2] == lyp->lay_stateid.other[2]) {
7243 NFSD_DEBUG(4, "nfsrv_layoutreturn: stateid %d"
7244 " %x %x %x laystateid %d %x %x %x"
7245 " off=%ju len=%ju flgs=0x%x\n",
7246 stateidp->seqid, stateidp->other[0],
7247 stateidp->other[1], stateidp->other[2],
7248 lyp->lay_stateid.seqid,
7249 lyp->lay_stateid.other[0],
7250 lyp->lay_stateid.other[1],
7251 lyp->lay_stateid.other[2],
7252 (uintmax_t)offset, (uintmax_t)len,
7254 if (++lyp->lay_stateid.seqid == 0)
7255 lyp->lay_stateid.seqid = 1;
7256 stateidp->seqid = lyp->lay_stateid.seqid;
7257 if (offset == 0 && len == UINT64_MAX) {
7258 if ((iomode & NFSLAYOUTIOMODE_READ) !=
7260 lyp->lay_flags &= ~NFSLAY_READ;
7261 if ((iomode & NFSLAYOUTIOMODE_RW) != 0)
7262 lyp->lay_flags &= ~NFSLAY_RW;
7263 if ((lyp->lay_flags & (NFSLAY_READ |
7265 nfsrv_freelayout(&lhyp->list,
7272 NFSUNLOCKLAYOUT(lhyp);
7273 /* Search the nfsrv_recalllist for a match. */
7274 TAILQ_FOREACH(lyp, &nfsrv_recalllisthead, lay_list) {
7275 if (NFSBCMP(&lyp->lay_fh, &fh,
7277 lyp->lay_clientid.qval ==
7278 nd->nd_clientid.qval &&
7279 stateidp->other[0] ==
7280 lyp->lay_stateid.other[0] &&
7281 stateidp->other[1] ==
7282 lyp->lay_stateid.other[1] &&
7283 stateidp->other[2] ==
7284 lyp->lay_stateid.other[2]) {
7285 lyp->lay_flags |= NFSLAY_RETURNED;
7292 if (layouttype == NFSLAYOUT_FLEXFILE)
7293 nfsrv_flexlayouterr(nd, layp, maxcnt, p);
7294 } else if (kind == NFSV4LAYOUTRET_FSID)
7295 nfsrv_freelayouts(&nd->nd_clientid,
7296 &vp->v_mount->mnt_stat.f_fsid, layouttype, iomode);
7297 else if (kind == NFSV4LAYOUTRET_ALL)
7298 nfsrv_freelayouts(&nd->nd_clientid, NULL, layouttype, iomode);
7300 error = NFSERR_INVAL;
7307 * Look for an existing layout.
7310 nfsrv_findlayout(nfsquad_t *clientidp, fhandle_t *fhp, int laytype,
7311 NFSPROC_T *p, struct nfslayout **lypp)
7313 struct nfslayouthash *lhyp;
7314 struct nfslayout *lyp;
7319 lhyp = NFSLAYOUTHASH(fhp);
7320 TAILQ_FOREACH(lyp, &lhyp->list, lay_list) {
7321 if (NFSBCMP(&lyp->lay_fh, fhp, sizeof(*fhp)) == 0 &&
7322 lyp->lay_clientid.qval == clientidp->qval &&
7323 lyp->lay_type == laytype)
7334 * Add the new layout, as required.
7337 nfsrv_addlayout(struct nfsrv_descript *nd, struct nfslayout **lypp,
7338 nfsv4stateid_t *stateidp, char *layp, int *layoutlenp, NFSPROC_T *p)
7340 struct nfsclient *clp;
7341 struct nfslayouthash *lhyp;
7342 struct nfslayout *lyp, *nlyp;
7346 KASSERT((nd->nd_flag & ND_IMPLIEDCLID) != 0,
7347 ("nfsrv_layoutget: no nd_clientid\n"));
7351 error = nfsrv_getclient((nfsquad_t)((u_quad_t)0), CLOPS_RENEW, &clp,
7352 NULL, (nfsquad_t)((u_quad_t)0), 0, nd, p);
7357 lyp->lay_stateid.seqid = stateidp->seqid = 1;
7358 lyp->lay_stateid.other[0] = stateidp->other[0] =
7359 clp->lc_clientid.lval[0];
7360 lyp->lay_stateid.other[1] = stateidp->other[1] =
7361 clp->lc_clientid.lval[1];
7362 lyp->lay_stateid.other[2] = stateidp->other[2] =
7363 nfsrv_nextstateindex(clp);
7366 lhyp = NFSLAYOUTHASH(fhp);
7367 NFSLOCKLAYOUT(lhyp);
7368 TAILQ_FOREACH(nlyp, &lhyp->list, lay_list) {
7369 if (NFSBCMP(&nlyp->lay_fh, fhp, sizeof(*fhp)) == 0 &&
7370 nlyp->lay_clientid.qval == nd->nd_clientid.qval)
7374 /* A layout already exists, so use it. */
7375 nlyp->lay_flags |= (lyp->lay_flags & (NFSLAY_READ | NFSLAY_RW));
7376 NFSBCOPY(nlyp->lay_xdr, layp, nlyp->lay_layoutlen);
7377 *layoutlenp = nlyp->lay_layoutlen;
7378 if (++nlyp->lay_stateid.seqid == 0)
7379 nlyp->lay_stateid.seqid = 1;
7380 stateidp->seqid = nlyp->lay_stateid.seqid;
7381 stateidp->other[0] = nlyp->lay_stateid.other[0];
7382 stateidp->other[1] = nlyp->lay_stateid.other[1];
7383 stateidp->other[2] = nlyp->lay_stateid.other[2];
7384 NFSUNLOCKLAYOUT(lhyp);
7388 /* Insert the new layout in the lists. */
7390 atomic_add_int(&nfsrv_layoutcnt, 1);
7391 NFSBCOPY(lyp->lay_xdr, layp, lyp->lay_layoutlen);
7392 *layoutlenp = lyp->lay_layoutlen;
7393 TAILQ_INSERT_HEAD(&lhyp->list, lyp, lay_list);
7394 NFSUNLOCKLAYOUT(lhyp);
7399 * Get the devinfo for a deviceid.
7402 nfsrv_getdevinfo(char *devid, int layouttype, uint32_t *maxcnt,
7403 uint32_t *notify, int *devaddrlen, char **devaddr)
7405 struct nfsdevice *ds;
7407 if ((layouttype != NFSLAYOUT_NFSV4_1_FILES && layouttype !=
7408 NFSLAYOUT_FLEXFILE) ||
7409 (nfsrv_maxpnfsmirror > 1 && layouttype == NFSLAYOUT_NFSV4_1_FILES))
7410 return (NFSERR_UNKNLAYOUTTYPE);
7413 * Now, search for the device id. Note that the structures won't go
7414 * away, but the order changes in the list. As such, the lock only
7415 * needs to be held during the search through the list.
7418 TAILQ_FOREACH(ds, &nfsrv_devidhead, nfsdev_list) {
7419 if (NFSBCMP(devid, ds->nfsdev_deviceid, NFSX_V4DEVICEID) == 0 &&
7420 ds->nfsdev_nmp != NULL)
7425 return (NFSERR_NOENT);
7427 /* If the correct nfsdev_XXXXaddrlen is > 0, we have the device info. */
7429 if (layouttype == NFSLAYOUT_NFSV4_1_FILES) {
7430 *devaddrlen = ds->nfsdev_fileaddrlen;
7431 *devaddr = ds->nfsdev_fileaddr;
7432 } else if (layouttype == NFSLAYOUT_FLEXFILE) {
7433 *devaddrlen = ds->nfsdev_flexaddrlen;
7434 *devaddr = ds->nfsdev_flexaddr;
7436 if (*devaddrlen == 0)
7437 return (NFSERR_UNKNLAYOUTTYPE);
7440 * The XDR overhead is 3 unsigned values: layout_type,
7441 * length_of_address and notify bitmap.
7442 * If the notify array is changed to not all zeros, the
7443 * count of unsigned values must be increased.
7445 if (*maxcnt > 0 && *maxcnt < NFSM_RNDUP(*devaddrlen) +
7446 3 * NFSX_UNSIGNED) {
7447 *maxcnt = NFSM_RNDUP(*devaddrlen) + 3 * NFSX_UNSIGNED;
7448 return (NFSERR_TOOSMALL);
7454 * Free a list of layout state structures.
7457 nfsrv_freelayoutlist(nfsquad_t clientid)
7459 struct nfslayouthash *lhyp;
7460 struct nfslayout *lyp, *nlyp;
7463 for (i = 0; i < nfsrv_layouthashsize; i++) {
7464 lhyp = &nfslayouthash[i];
7465 NFSLOCKLAYOUT(lhyp);
7466 TAILQ_FOREACH_SAFE(lyp, &lhyp->list, lay_list, nlyp) {
7467 if (lyp->lay_clientid.qval == clientid.qval)
7468 nfsrv_freelayout(&lhyp->list, lyp);
7470 NFSUNLOCKLAYOUT(lhyp);
7478 nfsrv_freelayout(struct nfslayouthead *lhp, struct nfslayout *lyp)
7481 NFSD_DEBUG(4, "Freelayout=%p\n", lyp);
7482 atomic_add_int(&nfsrv_layoutcnt, -1);
7483 TAILQ_REMOVE(lhp, lyp, lay_list);
7484 free(lyp, M_NFSDSTATE);
7488 * Free up a device id.
7491 nfsrv_freeonedevid(struct nfsdevice *ds)
7495 atomic_add_int(&nfsrv_devidcnt, -1);
7496 vrele(ds->nfsdev_dvp);
7497 for (i = 0; i < nfsrv_dsdirsize; i++)
7498 if (ds->nfsdev_dsdir[i] != NULL)
7499 vrele(ds->nfsdev_dsdir[i]);
7500 free(ds->nfsdev_fileaddr, M_NFSDSTATE);
7501 free(ds->nfsdev_flexaddr, M_NFSDSTATE);
7502 free(ds->nfsdev_host, M_NFSDSTATE);
7503 free(ds, M_NFSDSTATE);
7507 * Free up a device id and its mirrors.
7510 nfsrv_freedevid(struct nfsdevice *ds)
7513 TAILQ_REMOVE(&nfsrv_devidhead, ds, nfsdev_list);
7514 nfsrv_freeonedevid(ds);
7518 * Free all layouts and device ids.
7519 * Done when the nfsd threads are shut down since there may be a new
7520 * modified device id list created when the nfsd is restarted.
7523 nfsrv_freealllayoutsanddevids(void)
7525 struct nfsdontlist *mrp, *nmrp;
7526 struct nfslayout *lyp, *nlyp;
7528 /* Get rid of the deviceid structures. */
7529 nfsrv_freealldevids();
7530 TAILQ_INIT(&nfsrv_devidhead);
7533 /* Get rid of all layouts. */
7534 nfsrv_freealllayouts();
7536 /* Get rid of any nfsdontlist entries. */
7537 LIST_FOREACH_SAFE(mrp, &nfsrv_dontlisthead, nfsmr_list, nmrp)
7538 free(mrp, M_NFSDSTATE);
7539 LIST_INIT(&nfsrv_dontlisthead);
7540 nfsrv_dontlistlen = 0;
7542 /* Free layouts in the recall list. */
7543 TAILQ_FOREACH_SAFE(lyp, &nfsrv_recalllisthead, lay_list, nlyp)
7544 nfsrv_freelayout(&nfsrv_recalllisthead, lyp);
7545 TAILQ_INIT(&nfsrv_recalllisthead);
7549 * Free layouts that match the arguments.
7552 nfsrv_freelayouts(nfsquad_t *clid, fsid_t *fs, int laytype, int iomode)
7554 struct nfslayouthash *lhyp;
7555 struct nfslayout *lyp, *nlyp;
7558 for (i = 0; i < nfsrv_layouthashsize; i++) {
7559 lhyp = &nfslayouthash[i];
7560 NFSLOCKLAYOUT(lhyp);
7561 TAILQ_FOREACH_SAFE(lyp, &lhyp->list, lay_list, nlyp) {
7562 if (clid->qval != lyp->lay_clientid.qval)
7564 if (fs != NULL && fsidcmp(fs, &lyp->lay_fsid) != 0)
7566 if (laytype != lyp->lay_type)
7568 if ((iomode & NFSLAYOUTIOMODE_READ) != 0)
7569 lyp->lay_flags &= ~NFSLAY_READ;
7570 if ((iomode & NFSLAYOUTIOMODE_RW) != 0)
7571 lyp->lay_flags &= ~NFSLAY_RW;
7572 if ((lyp->lay_flags & (NFSLAY_READ | NFSLAY_RW)) == 0)
7573 nfsrv_freelayout(&lhyp->list, lyp);
7575 NFSUNLOCKLAYOUT(lhyp);
7580 * Free all layouts for the argument file.
7583 nfsrv_freefilelayouts(fhandle_t *fhp)
7585 struct nfslayouthash *lhyp;
7586 struct nfslayout *lyp, *nlyp;
7588 lhyp = NFSLAYOUTHASH(fhp);
7589 NFSLOCKLAYOUT(lhyp);
7590 TAILQ_FOREACH_SAFE(lyp, &lhyp->list, lay_list, nlyp) {
7591 if (NFSBCMP(&lyp->lay_fh, fhp, sizeof(*fhp)) == 0)
7592 nfsrv_freelayout(&lhyp->list, lyp);
7594 NFSUNLOCKLAYOUT(lhyp);
7601 nfsrv_freealllayouts(void)
7603 struct nfslayouthash *lhyp;
7604 struct nfslayout *lyp, *nlyp;
7607 for (i = 0; i < nfsrv_layouthashsize; i++) {
7608 lhyp = &nfslayouthash[i];
7609 NFSLOCKLAYOUT(lhyp);
7610 TAILQ_FOREACH_SAFE(lyp, &lhyp->list, lay_list, nlyp)
7611 nfsrv_freelayout(&lhyp->list, lyp);
7612 NFSUNLOCKLAYOUT(lhyp);
7617 * Look up the mount path for the DS server.
7620 nfsrv_setdsserver(char *dspathp, char *mdspathp, NFSPROC_T *p,
7621 struct nfsdevice **dsp)
7623 struct nameidata nd;
7624 struct nfsdevice *ds;
7630 NFSD_DEBUG(4, "setdssrv path=%s\n", dspathp);
7632 NDINIT(&nd, LOOKUP, FOLLOW | LOCKSHARED | LOCKLEAF, UIO_SYSSPACE,
7635 NFSD_DEBUG(4, "lookup=%d\n", error);
7638 if (nd.ni_vp->v_type != VDIR) {
7640 NFSD_DEBUG(4, "dspath not dir\n");
7643 if (strcmp(nd.ni_vp->v_mount->mnt_vfc->vfc_name, "nfs") != 0) {
7645 NFSD_DEBUG(4, "dspath not an NFS mount\n");
7650 * Allocate a DS server structure with the NFS mounted directory
7651 * vnode reference counted, so that a non-forced dismount will
7653 * This structure is always linked into the list, even if an error
7654 * is being returned. The caller will free the entire list upon
7657 *dsp = ds = malloc(sizeof(*ds) + nfsrv_dsdirsize * sizeof(vnode_t),
7658 M_NFSDSTATE, M_WAITOK | M_ZERO);
7659 ds->nfsdev_dvp = nd.ni_vp;
7660 ds->nfsdev_nmp = VFSTONFS(nd.ni_vp->v_mount);
7661 NFSVOPUNLOCK(nd.ni_vp);
7663 dsdirsize = strlen(dspathp) + 16;
7664 dsdirpath = malloc(dsdirsize, M_TEMP, M_WAITOK);
7665 /* Now, create the DS directory structures. */
7666 for (i = 0; i < nfsrv_dsdirsize; i++) {
7667 snprintf(dsdirpath, dsdirsize, "%s/ds%d", dspathp, i);
7668 NDINIT(&nd, LOOKUP, FOLLOW | LOCKSHARED | LOCKLEAF,
7669 UIO_SYSSPACE, dsdirpath, p);
7671 NFSD_DEBUG(4, "dsdirpath=%s lookup=%d\n", dsdirpath, error);
7674 if (nd.ni_vp->v_type != VDIR) {
7677 NFSD_DEBUG(4, "dsdirpath not a VDIR\n");
7680 if (strcmp(nd.ni_vp->v_mount->mnt_vfc->vfc_name, "nfs") != 0) {
7683 NFSD_DEBUG(4, "dsdirpath not an NFS mount\n");
7686 ds->nfsdev_dsdir[i] = nd.ni_vp;
7687 NFSVOPUNLOCK(nd.ni_vp);
7689 free(dsdirpath, M_TEMP);
7691 if (strlen(mdspathp) > 0) {
7693 * This DS stores file for a specific MDS exported file
7696 NDINIT(&nd, LOOKUP, FOLLOW | LOCKSHARED | LOCKLEAF,
7697 UIO_SYSSPACE, mdspathp, p);
7699 NFSD_DEBUG(4, "mds lookup=%d\n", error);
7702 if (nd.ni_vp->v_type != VDIR) {
7705 NFSD_DEBUG(4, "mdspath not dir\n");
7708 mp = nd.ni_vp->v_mount;
7709 if ((mp->mnt_flag & MNT_EXPORTED) == 0) {
7712 NFSD_DEBUG(4, "mdspath not an exported fs\n");
7715 ds->nfsdev_mdsfsid = mp->mnt_stat.f_fsid;
7716 ds->nfsdev_mdsisset = 1;
7721 TAILQ_INSERT_TAIL(&nfsrv_devidhead, ds, nfsdev_list);
7722 atomic_add_int(&nfsrv_devidcnt, 1);
7727 * Look up the mount path for the DS server and delete it.
7730 nfsrv_deldsserver(int op, char *dspathp, NFSPROC_T *p)
7733 struct nfsmount *nmp;
7734 struct nfsdevice *ds;
7737 NFSD_DEBUG(4, "deldssrv path=%s\n", dspathp);
7739 * Search for the path in the mount list. Avoid looking the path
7740 * up, since this mount point may be hung, with associated locked
7742 * Set NFSMNTP_CANCELRPCS so that any forced dismount will be blocked
7743 * until this completes.
7744 * As noted in the man page, this should be done before any forced
7745 * dismount on the mount point, but at least the handshake on
7746 * NFSMNTP_CANCELRPCS should make it safe.
7751 mtx_lock(&mountlist_mtx);
7752 TAILQ_FOREACH(mp, &mountlist, mnt_list) {
7753 if (strcmp(mp->mnt_stat.f_mntonname, dspathp) == 0 &&
7754 strcmp(mp->mnt_stat.f_fstypename, "nfs") == 0 &&
7755 mp->mnt_data != NULL) {
7758 if ((nmp->nm_privflag & (NFSMNTP_FORCEDISM |
7759 NFSMNTP_CANCELRPCS)) == 0) {
7760 nmp->nm_privflag |= NFSMNTP_CANCELRPCS;
7769 mtx_unlock(&mountlist_mtx);
7772 ds = nfsrv_deldsnmp(op, nmp, p);
7773 NFSD_DEBUG(4, "deldsnmp=%p\n", ds);
7775 nfsrv_killrpcs(nmp);
7776 NFSD_DEBUG(4, "aft killrpcs\n");
7780 nmp->nm_privflag &= ~NFSMNTP_CANCELRPCS;
7789 * Search for and remove a DS entry which matches the "nmp" argument.
7790 * The nfsdevice structure pointer is returned so that the caller can
7791 * free it via nfsrv_freeonedevid().
7792 * For the forced case, do not try to do LayoutRecalls, since the server
7793 * must be shut down now anyhow.
7796 nfsrv_deldsnmp(int op, struct nfsmount *nmp, NFSPROC_T *p)
7798 struct nfsdevice *fndds;
7800 NFSD_DEBUG(4, "deldsdvp\n");
7802 if (op == PNFSDOP_FORCEDELDS)
7803 fndds = nfsv4_findmirror(nmp);
7805 fndds = nfsrv_findmirroredds(nmp);
7807 nfsrv_deleteds(fndds);
7809 if (fndds != NULL) {
7810 if (op != PNFSDOP_FORCEDELDS)
7811 nfsrv_flexmirrordel(fndds->nfsdev_deviceid, p);
7812 printf("pNFS server: mirror %s failed\n", fndds->nfsdev_host);
7818 * Similar to nfsrv_deldsnmp(), except that the DS is indicated by deviceid.
7819 * This function also calls nfsrv_killrpcs() to unblock RPCs on the mount
7821 * Also, returns an error instead of the nfsdevice found.
7824 nfsrv_delds(char *devid, NFSPROC_T *p)
7826 struct nfsdevice *ds, *fndds;
7827 struct nfsmount *nmp;
7830 NFSD_DEBUG(4, "delds\n");
7832 * Search the DS server list for a match with devid.
7833 * Remove the DS entry if found and there is a mirror.
7839 TAILQ_FOREACH(ds, &nfsrv_devidhead, nfsdev_list) {
7840 if (NFSBCMP(ds->nfsdev_deviceid, devid, NFSX_V4DEVICEID) == 0 &&
7841 ds->nfsdev_nmp != NULL) {
7842 NFSD_DEBUG(4, "fnd main ds\n");
7847 if (fndds == NULL) {
7851 if (fndds->nfsdev_mdsisset == 0 && nfsrv_faildscnt > 0)
7853 else if (fndds->nfsdev_mdsisset != 0) {
7854 /* For the fsid is set case, search for a mirror. */
7855 TAILQ_FOREACH(ds, &nfsrv_devidhead, nfsdev_list) {
7856 if (ds != fndds && ds->nfsdev_nmp != NULL &&
7857 ds->nfsdev_mdsisset != 0 &&
7858 fsidcmp(&ds->nfsdev_mdsfsid,
7859 &fndds->nfsdev_mdsfsid) == 0) {
7865 if (fndmirror != 0) {
7866 nmp = fndds->nfsdev_nmp;
7868 if ((nmp->nm_privflag & (NFSMNTP_FORCEDISM |
7869 NFSMNTP_CANCELRPCS)) == 0) {
7870 nmp->nm_privflag |= NFSMNTP_CANCELRPCS;
7872 nfsrv_deleteds(fndds);
7880 nfsrv_flexmirrordel(fndds->nfsdev_deviceid, p);
7881 printf("pNFS server: mirror %s failed\n", fndds->nfsdev_host);
7882 nfsrv_killrpcs(nmp);
7884 nmp->nm_privflag &= ~NFSMNTP_CANCELRPCS;
7893 * Mark a DS as disabled by setting nfsdev_nmp = NULL.
7896 nfsrv_deleteds(struct nfsdevice *fndds)
7899 NFSD_DEBUG(4, "deleteds: deleting a mirror\n");
7900 fndds->nfsdev_nmp = NULL;
7901 if (fndds->nfsdev_mdsisset == 0)
7906 * Fill in the addr structures for the File and Flex File layouts.
7909 nfsrv_allocdevid(struct nfsdevice *ds, char *addr, char *dnshost)
7914 static uint64_t new_devid = 0;
7916 if (strchr(addr, ':') != NULL)
7921 /* Fill in the device id. */
7922 NFSBCOPY(&nfsdev_time, ds->nfsdev_deviceid, sizeof(nfsdev_time));
7924 NFSBCOPY(&new_devid, &ds->nfsdev_deviceid[sizeof(nfsdev_time)],
7928 * Fill in the file addr (actually the nfsv4_file_layout_ds_addr4
7929 * as defined in RFC5661) in XDR.
7931 addrlen = NFSM_RNDUP(strlen(addr)) + NFSM_RNDUP(strlen(netprot)) +
7933 NFSD_DEBUG(4, "hn=%s addr=%s netprot=%s\n", dnshost, addr, netprot);
7934 ds->nfsdev_fileaddrlen = addrlen;
7935 tl = malloc(addrlen, M_NFSDSTATE, M_WAITOK | M_ZERO);
7936 ds->nfsdev_fileaddr = (char *)tl;
7937 *tl++ = txdr_unsigned(1); /* One stripe with index 0. */
7939 *tl++ = txdr_unsigned(1); /* One multipath list */
7940 *tl++ = txdr_unsigned(1); /* with one entry in it. */
7941 /* The netaddr for this one entry. */
7942 *tl++ = txdr_unsigned(strlen(netprot));
7943 NFSBCOPY(netprot, tl, strlen(netprot));
7944 tl += (NFSM_RNDUP(strlen(netprot)) / NFSX_UNSIGNED);
7945 *tl++ = txdr_unsigned(strlen(addr));
7946 NFSBCOPY(addr, tl, strlen(addr));
7949 * Fill in the flex file addr (actually the ff_device_addr4
7950 * as defined for Flexible File Layout) in XDR.
7952 addrlen = NFSM_RNDUP(strlen(addr)) + NFSM_RNDUP(strlen(netprot)) +
7954 ds->nfsdev_flexaddrlen = addrlen;
7955 tl = malloc(addrlen, M_NFSDSTATE, M_WAITOK | M_ZERO);
7956 ds->nfsdev_flexaddr = (char *)tl;
7957 *tl++ = txdr_unsigned(1); /* One multipath entry. */
7958 /* The netaddr for this one entry. */
7959 *tl++ = txdr_unsigned(strlen(netprot));
7960 NFSBCOPY(netprot, tl, strlen(netprot));
7961 tl += (NFSM_RNDUP(strlen(netprot)) / NFSX_UNSIGNED);
7962 *tl++ = txdr_unsigned(strlen(addr));
7963 NFSBCOPY(addr, tl, strlen(addr));
7964 tl += (NFSM_RNDUP(strlen(addr)) / NFSX_UNSIGNED);
7965 *tl++ = txdr_unsigned(2); /* Two NFS Versions. */
7966 *tl++ = txdr_unsigned(NFS_VER4); /* NFSv4. */
7967 *tl++ = txdr_unsigned(NFSV42_MINORVERSION); /* Minor version 2. */
7968 *tl++ = txdr_unsigned(nfs_srvmaxio); /* DS max rsize. */
7969 *tl++ = txdr_unsigned(nfs_srvmaxio); /* DS max wsize. */
7970 *tl++ = newnfs_true; /* Tightly coupled. */
7971 *tl++ = txdr_unsigned(NFS_VER4); /* NFSv4. */
7972 *tl++ = txdr_unsigned(NFSV41_MINORVERSION); /* Minor version 1. */
7973 *tl++ = txdr_unsigned(nfs_srvmaxio); /* DS max rsize. */
7974 *tl++ = txdr_unsigned(nfs_srvmaxio); /* DS max wsize. */
7975 *tl = newnfs_true; /* Tightly coupled. */
7977 ds->nfsdev_hostnamelen = strlen(dnshost);
7978 ds->nfsdev_host = malloc(ds->nfsdev_hostnamelen + 1, M_NFSDSTATE,
7980 NFSBCOPY(dnshost, ds->nfsdev_host, ds->nfsdev_hostnamelen + 1);
7984 * Create the device id list.
7985 * Return 0 if the nfsd threads are to run and ENXIO if the "-p" argument
7989 nfsrv_createdevids(struct nfsd_nfsd_args *args, NFSPROC_T *p)
7991 struct nfsdevice *ds;
7992 char *addrp, *dnshostp, *dspathp, *mdspathp;
7996 dnshostp = args->dnshost;
7997 dspathp = args->dspath;
7998 mdspathp = args->mdspath;
7999 nfsrv_maxpnfsmirror = args->mirrorcnt;
8000 if (addrp == NULL || dnshostp == NULL || dspathp == NULL ||
8005 * Loop around for each nul-terminated string in args->addr,
8006 * args->dnshost, args->dnspath and args->mdspath.
8008 while (addrp < (args->addr + args->addrlen) &&
8009 dnshostp < (args->dnshost + args->dnshostlen) &&
8010 dspathp < (args->dspath + args->dspathlen) &&
8011 mdspathp < (args->mdspath + args->mdspathlen)) {
8012 error = nfsrv_setdsserver(dspathp, mdspathp, p, &ds);
8014 /* Free all DS servers. */
8015 nfsrv_freealldevids();
8019 nfsrv_allocdevid(ds, addrp, dnshostp);
8020 addrp += (strlen(addrp) + 1);
8021 dnshostp += (strlen(dnshostp) + 1);
8022 dspathp += (strlen(dspathp) + 1);
8023 mdspathp += (strlen(mdspathp) + 1);
8025 if (nfsrv_devidcnt < nfsrv_maxpnfsmirror) {
8026 /* Free all DS servers. */
8027 nfsrv_freealldevids();
8029 nfsrv_maxpnfsmirror = 1;
8032 /* We can fail at most one less DS than the mirror level. */
8033 nfsrv_faildscnt = nfsrv_maxpnfsmirror - 1;
8036 * Allocate the nfslayout hash table now, since this is a pNFS server.
8037 * Make it 1% of the high water mark and at least 100.
8039 if (nfslayouthash == NULL) {
8040 nfsrv_layouthashsize = nfsrv_layouthighwater / 100;
8041 if (nfsrv_layouthashsize < 100)
8042 nfsrv_layouthashsize = 100;
8043 nfslayouthash = mallocarray(nfsrv_layouthashsize,
8044 sizeof(struct nfslayouthash), M_NFSDSESSION, M_WAITOK |
8046 for (i = 0; i < nfsrv_layouthashsize; i++) {
8047 mtx_init(&nfslayouthash[i].mtx, "nfslm", NULL, MTX_DEF);
8048 TAILQ_INIT(&nfslayouthash[i].list);
8055 * Free all device ids.
8058 nfsrv_freealldevids(void)
8060 struct nfsdevice *ds, *nds;
8062 TAILQ_FOREACH_SAFE(ds, &nfsrv_devidhead, nfsdev_list, nds)
8063 nfsrv_freedevid(ds);
8067 * Check to see if there is a Read/Write Layout plus either:
8068 * - A Write Delegation
8070 * - An Open with Write_access.
8071 * Return 1 if this is the case and 0 otherwise.
8072 * This function is used by nfsrv_proxyds() to decide if doing a Proxy
8073 * Getattr RPC to the Data Server (DS) is necessary.
8075 #define NFSCLIDVECSIZE 6
8077 nfsrv_checkdsattr(vnode_t vp, NFSPROC_T *p)
8079 fhandle_t fh, *tfhp;
8080 struct nfsstate *stp;
8081 struct nfslayout *lyp;
8082 struct nfslayouthash *lhyp;
8083 struct nfslockhashhead *hp;
8084 struct nfslockfile *lfp;
8085 nfsquad_t clid[NFSCLIDVECSIZE];
8088 ret = nfsvno_getfh(vp, &fh, p);
8092 /* First check for a Read/Write Layout. */
8094 lhyp = NFSLAYOUTHASH(&fh);
8095 NFSLOCKLAYOUT(lhyp);
8096 TAILQ_FOREACH(lyp, &lhyp->list, lay_list) {
8097 if (NFSBCMP(&lyp->lay_fh, &fh, sizeof(fh)) == 0 &&
8098 ((lyp->lay_flags & NFSLAY_RW) != 0 ||
8099 ((lyp->lay_flags & NFSLAY_READ) != 0 &&
8100 nfsrv_pnfsatime != 0))) {
8101 if (clidcnt < NFSCLIDVECSIZE)
8102 clid[clidcnt].qval = lyp->lay_clientid.qval;
8106 NFSUNLOCKLAYOUT(lhyp);
8108 /* None found, so return 0. */
8112 /* Get the nfslockfile for this fh. */
8114 hp = NFSLOCKHASH(&fh);
8115 LIST_FOREACH(lfp, hp, lf_hash) {
8117 if (NFSVNO_CMPFH(&fh, tfhp))
8121 /* None found, so return 0. */
8126 /* Now, look for a Write delegation for this clientid. */
8127 LIST_FOREACH(stp, &lfp->lf_deleg, ls_file) {
8128 if ((stp->ls_flags & NFSLCK_DELEGWRITE) != 0 &&
8129 nfsrv_fndclid(clid, stp->ls_clp->lc_clientid, clidcnt) != 0)
8133 /* Found one, so return 1. */
8138 /* No Write delegation, so look for an Open with Write_access. */
8139 LIST_FOREACH(stp, &lfp->lf_open, ls_file) {
8140 KASSERT((stp->ls_flags & NFSLCK_OPEN) != 0,
8141 ("nfsrv_checkdsattr: Non-open in Open list\n"));
8142 if ((stp->ls_flags & NFSLCK_WRITEACCESS) != 0 &&
8143 nfsrv_fndclid(clid, stp->ls_clp->lc_clientid, clidcnt) != 0)
8153 * Look for a matching clientid in the vector. Return 1 if one might match.
8156 nfsrv_fndclid(nfsquad_t *clidvec, nfsquad_t clid, int clidcnt)
8160 /* If too many for the vector, return 1 since there might be a match. */
8161 if (clidcnt > NFSCLIDVECSIZE)
8164 for (i = 0; i < clidcnt; i++)
8165 if (clidvec[i].qval == clid.qval)
8171 * Check the don't list for "vp" and see if issuing an rw layout is allowed.
8172 * Return 1 if issuing an rw layout isn't allowed, 0 otherwise.
8175 nfsrv_dontlayout(fhandle_t *fhp)
8177 struct nfsdontlist *mrp;
8180 if (nfsrv_dontlistlen == 0)
8184 LIST_FOREACH(mrp, &nfsrv_dontlisthead, nfsmr_list) {
8185 if (NFSBCMP(fhp, &mrp->nfsmr_fh, sizeof(*fhp)) == 0 &&
8186 (mrp->nfsmr_flags & NFSMR_DONTLAYOUT) != 0) {
8191 NFSDDONTLISTUNLOCK();
8195 #define PNFSDS_COPYSIZ 65536
8197 * Create a new file on a DS and copy the contents of an extant DS file to it.
8198 * This can be used for recovery of a DS file onto a recovered DS.
8200 * - When called, the MDS file's vnode is locked, blocking LayoutGet operations.
8201 * - Disable issuing of read/write layouts for the file via the nfsdontlist,
8202 * so that they will be disabled after the MDS file's vnode is unlocked.
8203 * - Set up the nfsrv_recalllist so that recall of read/write layouts can
8205 * - Unlock the MDS file's vnode, so that the client(s) can perform proxied
8206 * writes, LayoutCommits and LayoutReturns for the file when completing the
8207 * LayoutReturn requested by the LayoutRecall callback.
8208 * - Issue a LayoutRecall callback for all read/write layouts and wait for
8209 * them to be returned. (If the LayoutRecall callback replies
8210 * NFSERR_NOMATCHLAYOUT, they are gone and no LayoutReturn is needed.)
8211 * - Exclusively lock the MDS file's vnode. This ensures that no proxied
8212 * writes are in progress or can occur during the DS file copy.
8213 * It also blocks Setattr operations.
8214 * - Create the file on the recovered mirror.
8215 * - Copy the file from the operational DS.
8216 * - Copy any ACL from the MDS file to the new DS file.
8217 * - Set the modify time of the new DS file to that of the MDS file.
8218 * - Update the extended attribute for the MDS file.
8219 * - Enable issuing of rw layouts by deleting the nfsdontlist entry.
8220 * - The caller will unlock the MDS file's vnode allowing operations
8221 * to continue normally, since it is now on the mirror again.
8224 nfsrv_copymr(vnode_t vp, vnode_t fvp, vnode_t dvp, struct nfsdevice *ds,
8225 struct pnfsdsfile *pf, struct pnfsdsfile *wpf, int mirrorcnt,
8226 struct ucred *cred, NFSPROC_T *p)
8228 struct nfsdontlist *mrp, *nmrp;
8229 struct nfslayouthash *lhyp;
8230 struct nfslayout *lyp, *nlyp;
8231 struct nfslayouthead thl;
8232 struct mount *mp, *tvmp;
8235 struct timespec mtime;
8241 int didprintf, ret, retacl, xfer;
8243 ASSERT_VOP_LOCKED(fvp, "nfsrv_copymr fvp");
8244 ASSERT_VOP_LOCKED(vp, "nfsrv_copymr vp");
8246 * Allocate a nfsdontlist entry and set the NFSMR_DONTLAYOUT flag
8247 * so that no more RW layouts will get issued.
8249 ret = nfsvno_getfh(vp, &fh, p);
8251 NFSD_DEBUG(4, "nfsrv_copymr: getfh=%d\n", ret);
8254 nmrp = malloc(sizeof(*nmrp), M_NFSDSTATE, M_WAITOK);
8255 nmrp->nfsmr_flags = NFSMR_DONTLAYOUT;
8256 NFSBCOPY(&fh, &nmrp->nfsmr_fh, sizeof(fh));
8258 LIST_FOREACH(mrp, &nfsrv_dontlisthead, nfsmr_list) {
8259 if (NFSBCMP(&fh, &mrp->nfsmr_fh, sizeof(fh)) == 0)
8263 LIST_INSERT_HEAD(&nfsrv_dontlisthead, nmrp, nfsmr_list);
8266 nfsrv_dontlistlen++;
8267 NFSD_DEBUG(4, "nfsrv_copymr: in dontlist\n");
8269 NFSDDONTLISTUNLOCK();
8270 free(nmrp, M_NFSDSTATE);
8271 NFSD_DEBUG(4, "nfsrv_copymr: dup dontlist\n");
8274 NFSDDONTLISTUNLOCK();
8277 * Search for all RW layouts for this file. Move them to the
8278 * recall list, so they can be recalled and their return noted.
8280 lhyp = NFSLAYOUTHASH(&fh);
8282 NFSLOCKLAYOUT(lhyp);
8283 TAILQ_FOREACH_SAFE(lyp, &lhyp->list, lay_list, nlyp) {
8284 if (NFSBCMP(&lyp->lay_fh, &fh, sizeof(fh)) == 0 &&
8285 (lyp->lay_flags & NFSLAY_RW) != 0) {
8286 TAILQ_REMOVE(&lhyp->list, lyp, lay_list);
8287 TAILQ_INSERT_HEAD(&nfsrv_recalllisthead, lyp, lay_list);
8288 lyp->lay_trycnt = 0;
8291 NFSUNLOCKLAYOUT(lhyp);
8298 /* Unlock the MDS vp, so that a LayoutReturn can be done on it. */
8300 /* Now, do a recall for all layouts not yet recalled. */
8303 TAILQ_FOREACH(lyp, &nfsrv_recalllisthead, lay_list) {
8304 if (NFSBCMP(&lyp->lay_fh, &fh, sizeof(fh)) == 0 &&
8305 (lyp->lay_flags & NFSLAY_RECALL) == 0) {
8306 lyp->lay_flags |= NFSLAY_RECALL;
8308 * The layout stateid.seqid needs to be incremented
8309 * before doing a LAYOUT_RECALL callback.
8311 if (++lyp->lay_stateid.seqid == 0)
8312 lyp->lay_stateid.seqid = 1;
8314 nfsrv_recalllayout(lyp->lay_clientid, &lyp->lay_stateid,
8315 &lyp->lay_fh, lyp, 0, lyp->lay_type, p);
8316 NFSD_DEBUG(4, "nfsrv_copymr: recalled layout\n");
8321 /* Now wait for them to be returned. */
8323 TAILQ_FOREACH(lyp, &nfsrv_recalllisthead, lay_list) {
8324 if (NFSBCMP(&lyp->lay_fh, &fh, sizeof(fh)) == 0) {
8325 if ((lyp->lay_flags & NFSLAY_RETURNED) != 0) {
8326 TAILQ_REMOVE(&nfsrv_recalllisthead, lyp,
8328 TAILQ_INSERT_HEAD(&thl, lyp, lay_list);
8330 "nfsrv_copymr: layout returned\n");
8333 ret = mtx_sleep(lyp, NFSDRECALLMUTEXPTR,
8334 PVFS | PCATCH, "nfsmrl", hz);
8335 NFSD_DEBUG(4, "nfsrv_copymr: aft sleep=%d\n",
8337 if (ret == EINTR || ret == ERESTART)
8339 if ((lyp->lay_flags & NFSLAY_RETURNED) == 0) {
8341 * Give up after 60sec and return
8342 * ENXIO, failing the copymr.
8343 * This layout will remain on the
8344 * recalllist. It can only be cleared
8345 * by restarting the nfsd.
8346 * This seems the safe way to handle
8347 * it, since it cannot be safely copied
8348 * with an outstanding RW layout.
8350 if (lyp->lay_trycnt >= 60) {
8354 if (didprintf == 0) {
8355 printf("nfsrv_copymr: layout "
8365 /* We can now get rid of the layouts that have been returned. */
8366 TAILQ_FOREACH_SAFE(lyp, &thl, lay_list, nlyp)
8367 nfsrv_freelayout(&thl, lyp);
8370 * Do the vn_start_write() calls here, before the MDS vnode is
8371 * locked and the tvp is created (locked) in the NFS file system
8373 * For tvmp, this probably isn't necessary, since it will be an
8374 * NFS mount and they are not suspendable at this time.
8377 ret = vn_start_write(vp, &mp, V_WAIT | PCATCH);
8379 tvmp = dvp->v_mount;
8380 ret = vn_start_write(NULL, &tvmp, V_WAIT | PCATCH);
8384 * LK_EXCLUSIVE lock the MDS vnode, so that any
8385 * proxied writes through the MDS will be blocked until we have
8386 * completed the copy and update of the extended attributes.
8387 * This will also ensure that any attributes and ACL will not be
8388 * changed until the copy is complete.
8390 NFSVOPLOCK(vp, LK_EXCLUSIVE | LK_RETRY);
8391 if (ret == 0 && VN_IS_DOOMED(vp)) {
8392 NFSD_DEBUG(4, "nfsrv_copymr: lk_exclusive doomed\n");
8396 /* Create the data file on the recovered DS. */
8398 ret = nfsrv_createdsfile(vp, &fh, pf, dvp, ds, cred, p, &tvp);
8400 /* Copy the DS file, if created successfully. */
8403 * Get any NFSv4 ACL on the MDS file, so that it can be set
8404 * on the new DS file.
8406 aclp = acl_alloc(M_WAITOK | M_ZERO);
8407 retacl = VOP_GETACL(vp, ACL_TYPE_NFS4, aclp, cred, p);
8408 if (retacl != 0 && retacl != ENOATTR)
8409 NFSD_DEBUG(1, "nfsrv_copymr: vop_getacl=%d\n", retacl);
8410 dat = malloc(PNFSDS_COPYSIZ, M_TEMP, M_WAITOK);
8411 /* Malloc a block of 0s used to check for holes. */
8412 if (nfsrv_zeropnfsdat == NULL)
8413 nfsrv_zeropnfsdat = malloc(PNFSDS_COPYSIZ, M_TEMP,
8416 ret = VOP_GETATTR(fvp, &va, cred);
8418 while (ret == 0 && aresid == 0) {
8419 ret = vn_rdwr(UIO_READ, fvp, dat, PNFSDS_COPYSIZ,
8420 rdpos, UIO_SYSSPACE, IO_NODELOCKED, cred, NULL,
8422 xfer = PNFSDS_COPYSIZ - aresid;
8423 if (ret == 0 && xfer > 0) {
8426 * Skip the write for holes, except for the
8429 if (xfer < PNFSDS_COPYSIZ || rdpos ==
8430 va.va_size || NFSBCMP(dat,
8431 nfsrv_zeropnfsdat, PNFSDS_COPYSIZ) != 0)
8432 ret = vn_rdwr(UIO_WRITE, tvp, dat, xfer,
8433 wrpos, UIO_SYSSPACE, IO_NODELOCKED,
8434 cred, NULL, NULL, p);
8440 /* If there is an ACL and the copy succeeded, set the ACL. */
8441 if (ret == 0 && retacl == 0) {
8442 ret = VOP_SETACL(tvp, ACL_TYPE_NFS4, aclp, cred, p);
8444 * Don't consider these as errors, since VOP_GETACL()
8445 * can return an ACL when they are not actually
8446 * supported. For example, for UFS, VOP_GETACL()
8447 * will return a trivial ACL based on the uid/gid/mode
8448 * when there is no ACL on the file.
8449 * This case should be recognized as a trivial ACL
8450 * by UFS's VOP_SETACL() and succeed, but...
8452 if (ret == ENOATTR || ret == EOPNOTSUPP || ret == EPERM)
8457 ret = VOP_FSYNC(tvp, MNT_WAIT, p);
8459 /* Set the DS data file's modify time that of the MDS file. */
8461 ret = VOP_GETATTR(vp, &va, cred);
8463 mtime = va.va_mtime;
8465 va.va_mtime = mtime;
8466 ret = VOP_SETATTR(tvp, &va, cred);
8474 vn_finished_write(tvmp);
8476 /* Update the extended attributes for the newly created DS file. */
8478 ret = vn_extattr_set(vp, IO_NODELOCKED,
8479 EXTATTR_NAMESPACE_SYSTEM, "pnfsd.dsfile",
8480 sizeof(*wpf) * mirrorcnt, (char *)wpf, p);
8482 vn_finished_write(mp);
8484 /* Get rid of the dontlist entry, so that Layouts can be issued. */
8486 LIST_REMOVE(mrp, nfsmr_list);
8487 NFSDDONTLISTUNLOCK();
8488 free(mrp, M_NFSDSTATE);
8493 * Create a data storage file on the recovered DS.
8496 nfsrv_createdsfile(vnode_t vp, fhandle_t *fhp, struct pnfsdsfile *pf,
8497 vnode_t dvp, struct nfsdevice *ds, struct ucred *cred, NFSPROC_T *p,
8500 struct vattr va, nva;
8503 /* Make data file name based on FH. */
8504 error = VOP_GETATTR(vp, &va, cred);
8506 /* Set the attributes for "vp" to Setattr the DS vp. */
8508 nva.va_uid = va.va_uid;
8509 nva.va_gid = va.va_gid;
8510 nva.va_mode = va.va_mode;
8514 va.va_mode = nva.va_mode;
8515 NFSD_DEBUG(4, "nfsrv_dscreatefile: dvp=%p pf=%p\n", dvp, pf);
8516 error = nfsrv_dscreate(dvp, &va, &nva, fhp, pf, NULL,
8517 pf->dsf_filename, cred, p, tvpp);
8523 * Look up the MDS file shared locked, and then get the extended attribute
8524 * to find the extant DS file to be copied to the new mirror.
8525 * If successful, *vpp is set to the MDS file's vp and *nvpp is
8526 * set to a DS data file for the MDS file, both exclusively locked.
8527 * The "buf" argument has the pnfsdsfile structure from the MDS file
8528 * in it and buflen is set to its length.
8531 nfsrv_mdscopymr(char *mdspathp, char *dspathp, char *curdspathp, char *buf,
8532 int *buflenp, char *fname, NFSPROC_T *p, struct vnode **vpp,
8533 struct vnode **nvpp, struct pnfsdsfile **pfp, struct nfsdevice **dsp,
8534 struct nfsdevice **fdsp)
8536 struct nameidata nd;
8537 struct vnode *vp, *curvp;
8538 struct pnfsdsfile *pf;
8539 struct nfsmount *nmp, *curnmp;
8540 int dsdir, error, mirrorcnt, ippos;
8547 if (dspathp == NULL && curdspathp != NULL)
8551 * Look up the MDS file shared locked. The lock will be upgraded
8552 * to an exclusive lock after any rw layouts have been returned.
8554 NFSD_DEBUG(4, "mdsopen path=%s\n", mdspathp);
8555 NDINIT(&nd, LOOKUP, FOLLOW | LOCKSHARED | LOCKLEAF, UIO_SYSSPACE,
8558 NFSD_DEBUG(4, "lookup=%d\n", error);
8561 if (nd.ni_vp->v_type != VREG) {
8563 NFSD_DEBUG(4, "mdspath not reg\n");
8568 if (curdspathp != NULL) {
8570 * Look up the current DS path and find the nfsdev structure for
8573 NFSD_DEBUG(4, "curmdsdev path=%s\n", curdspathp);
8574 NDINIT(&nd, LOOKUP, FOLLOW | LOCKSHARED | LOCKLEAF,
8575 UIO_SYSSPACE, curdspathp, p);
8577 NFSD_DEBUG(4, "ds lookup=%d\n", error);
8582 if (nd.ni_vp->v_type != VDIR) {
8585 NFSD_DEBUG(4, "curdspath not dir\n");
8588 if (strcmp(nd.ni_vp->v_mount->mnt_vfc->vfc_name, "nfs") != 0) {
8591 NFSD_DEBUG(4, "curdspath not an NFS mount\n");
8594 curnmp = VFSTONFS(nd.ni_vp->v_mount);
8596 /* Search the nfsdev list for a match. */
8598 *fdsp = nfsv4_findmirror(curnmp);
8602 if (curnmp == NULL) {
8605 NFSD_DEBUG(4, "mdscopymr: no current ds\n");
8611 if (dspathp != NULL) {
8612 /* Look up the nfsdev path and find the nfsdev structure. */
8613 NFSD_DEBUG(4, "mdsdev path=%s\n", dspathp);
8614 NDINIT(&nd, LOOKUP, FOLLOW | LOCKSHARED | LOCKLEAF,
8615 UIO_SYSSPACE, dspathp, p);
8617 NFSD_DEBUG(4, "ds lookup=%d\n", error);
8624 if (nd.ni_vp->v_type != VDIR || nd.ni_vp == curvp) {
8629 NFSD_DEBUG(4, "dspath not dir\n");
8630 if (nd.ni_vp == curvp)
8634 if (strcmp(nd.ni_vp->v_mount->mnt_vfc->vfc_name, "nfs") != 0) {
8639 NFSD_DEBUG(4, "dspath not an NFS mount\n");
8642 nmp = VFSTONFS(nd.ni_vp->v_mount);
8645 * Search the nfsdevice list for a match. If curnmp == NULL,
8646 * this is a recovery and there must be a mirror.
8650 *dsp = nfsrv_findmirroredds(nmp);
8652 *dsp = nfsv4_findmirror(nmp);
8659 NFSD_DEBUG(4, "mdscopymr: no ds\n");
8668 * Get a vp for an available DS data file using the extended
8669 * attribute on the MDS file.
8670 * If there is a valid entry for the new DS in the extended attribute
8671 * on the MDS file (as checked via the nmp argument),
8672 * nfsrv_dsgetsockmnt() returns EEXIST, so no copying will occur.
8674 error = nfsrv_dsgetsockmnt(vp, 0, buf, buflenp, &mirrorcnt, p,
8675 NULL, NULL, NULL, fname, nvpp, &nmp, curnmp, &ippos, &dsdir);
8678 if (nd.ni_vp == NULL) {
8679 if (error == 0 && nmp != NULL) {
8680 /* Search the nfsdev list for a match. */
8682 *dsp = nfsrv_findmirroredds(nmp);
8685 if (error == 0 && (nmp == NULL || *dsp == NULL)) {
8686 if (nvpp != NULL && *nvpp != NULL) {
8696 * When dspathp != NULL and curdspathp == NULL, this is a recovery
8697 * and is only allowed if there is a 0.0.0.0 IP address entry.
8698 * When curdspathp != NULL, the ippos will be set to that entry.
8700 if (error == 0 && dspathp != NULL && ippos == -1) {
8701 if (nvpp != NULL && *nvpp != NULL) {
8710 pf = (struct pnfsdsfile *)buf;
8712 /* If no zeroip pnfsdsfile, add one. */
8713 ippos = *buflenp / sizeof(*pf);
8714 *buflenp += sizeof(*pf);
8716 pf->dsf_dir = dsdir;
8717 strlcpy(pf->dsf_filename, fname,
8718 sizeof(pf->dsf_filename));
8728 * Search for a matching pnfsd mirror device structure, base on the nmp arg.
8729 * Return one if found, NULL otherwise.
8731 static struct nfsdevice *
8732 nfsrv_findmirroredds(struct nfsmount *nmp)
8734 struct nfsdevice *ds, *fndds;
8737 mtx_assert(NFSDDSMUTEXPTR, MA_OWNED);
8739 * Search the DS server list for a match with nmp.
8740 * Remove the DS entry if found and there is a mirror.
8744 if (nfsrv_devidcnt == 0)
8746 TAILQ_FOREACH(ds, &nfsrv_devidhead, nfsdev_list) {
8747 if (ds->nfsdev_nmp == nmp) {
8748 NFSD_DEBUG(4, "nfsrv_findmirroredds: fnd main ds\n");
8755 if (fndds->nfsdev_mdsisset == 0 && nfsrv_faildscnt > 0)
8757 else if (fndds->nfsdev_mdsisset != 0) {
8758 /* For the fsid is set case, search for a mirror. */
8759 TAILQ_FOREACH(ds, &nfsrv_devidhead, nfsdev_list) {
8760 if (ds != fndds && ds->nfsdev_nmp != NULL &&
8761 ds->nfsdev_mdsisset != 0 &&
8762 fsidcmp(&ds->nfsdev_mdsfsid,
8763 &fndds->nfsdev_mdsfsid) == 0) {
8769 if (fndmirror == 0) {
8770 NFSD_DEBUG(4, "nfsrv_findmirroredds: no mirror for DS\n");