xref: /freebsd/sys/fs/nfsserver/nfs_nfsdstate.c (revision 457c621add0a531273ee27798c924c6aaeacc4c1)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2009 Rick Macklem, University of Guelph
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
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.
15  *
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
26  * SUCH DAMAGE.
27  *
28  */
29 
30 #include <sys/cdefs.h>
31 #include "opt_inet.h"
32 #include "opt_inet6.h"
33 #include <sys/extattr.h>
34 #include <fs/nfs/nfsport.h>
35 
36 int nfsrv_issuedelegs = 0;
37 int nfsrv_dolocallocks = 0;
38 struct nfsv4lock nfsv4rootfs_lock;
39 time_t nfsdev_time = 0;
40 int nfsrv_layouthashsize;
41 volatile int nfsrv_layoutcnt = 0;
42 
43 VNET_DEFINE(struct nfsrv_stablefirst, nfsrv_stablefirst);
44 
45 VNET_DECLARE(int, nfsrv_numnfsd);
46 VNET_DECLARE(struct nfsstatsv1 *, nfsstatsv1_p);
47 
48 extern uint32_t nfs_srvmaxio;
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;
60 NFSV4ROOTLOCKMUTEX;
61 NFSSTATESPINLOCK;
62 extern struct nfsdontlisthead nfsrv_dontlisthead;
63 extern volatile int nfsrv_devidcnt;
64 extern struct nfslayouthead nfsrv_recalllisthead;
65 extern char *nfsrv_zeropnfsdat;
66 
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");
72 
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");
77 
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");
82 
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");
87 
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");
92 
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");
97 
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");
102 
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");
107 
108 int	nfsrv_pnfsatime = 0;
109 SYSCTL_INT(_vfs_nfsd, OID_AUTO, pnfsstrictatime, CTLFLAG_RW,
110     &nfsrv_pnfsatime, 0,
111     "For pNFS service, do Getattr ops to keep atime up-to-date");
112 
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");
117 
118 VNET_DEFINE_STATIC(bool, nfsd_disable_grace) = false;
119 SYSCTL_BOOL(_vfs_nfsd, OID_AUTO, testing_disable_grace,
120     CTLFLAG_VNET | CTLFLAG_RW, &VNET_NAME(nfsd_disable_grace),
121     0, "Disable grace for testing");
122 
123 /*
124  * Hash lists for nfs V4.
125  */
126 VNET_DEFINE(struct nfsclienthashhead *, nfsclienthash);
127 VNET_DEFINE(struct nfslockhashhead *, nfslockhash);
128 VNET_DEFINE(struct nfssessionhash *, nfssessionhash);
129 
130 struct nfslayouthash		*nfslayouthash;
131 volatile int nfsrv_dontlistlen = 0;
132 
133 static u_int32_t nfsrv_openpluslock = 0, nfsrv_delegatecnt = 0;
134 static int nfsrv_returnoldstateid = 0, nfsrv_clients = 0;
135 static int nfsrv_clienthighwater = NFSRV_CLIENTHIGHWATER;
136 static int nfsrv_nogsscallback = 0;
137 static volatile int nfsrv_writedelegcnt = 0;
138 static int nfsrv_faildscnt;
139 
140 VNET_DEFINE_STATIC(time_t, nfsrvboottime);
141 
142 /* local functions */
143 static void nfsrv_dumpaclient(struct nfsclient *clp,
144     struct nfsd_dumpclients *dumpp);
145 static void nfsrv_freeopenowner(struct nfsstate *stp, int cansleep,
146     NFSPROC_T *p);
147 static void nfsrv_freeopen(struct nfsstate *stp, vnode_t vp, int cansleep,
148     NFSPROC_T *p);
149 static void nfsrv_freelockowner(struct nfsstate *stp, vnode_t vp, int cansleep,
150     NFSPROC_T *p);
151 static void nfsrv_freeallnfslocks(struct nfsstate *stp, vnode_t vp,
152     int cansleep, NFSPROC_T *p);
153 static void nfsrv_freenfslock(struct nfslock *lop);
154 static void nfsrv_freenfslockfile(struct nfslockfile *lfp);
155 static void nfsrv_freedeleg(struct nfsstate *);
156 static int nfsrv_getstate(struct nfsclient *clp, nfsv4stateid_t *stateidp,
157     u_int32_t flags, struct nfsstate **stpp);
158 static void nfsrv_getowner(struct nfsstatehead *hp, struct nfsstate *new_stp,
159     struct nfsstate **stpp);
160 static int nfsrv_getlockfh(vnode_t vp, u_short flags,
161     struct nfslockfile *new_lfp, fhandle_t *nfhp, NFSPROC_T *p);
162 static int nfsrv_getlockfile(u_short flags, struct nfslockfile **new_lfpp,
163     struct nfslockfile **lfpp, fhandle_t *nfhp, int lockit);
164 static void nfsrv_insertlock(struct nfslock *new_lop,
165     struct nfslock *insert_lop, struct nfsstate *stp, struct nfslockfile *lfp);
166 static void nfsrv_updatelock(struct nfsstate *stp, struct nfslock **new_lopp,
167     struct nfslock **other_lopp, struct nfslockfile *lfp);
168 static int nfsrv_checkrestart(nfsquad_t clientid, u_int32_t flags,
169     nfsv4stateid_t *stateidp, int specialid);
170 static int nfsrv_checkgrace(struct nfsrv_descript *nd, struct nfsclient *clp,
171     u_int32_t flags);
172 static int nfsrv_docallback(struct nfsclient *clp, int procnum,
173     nfsv4stateid_t *stateidp, int trunc, fhandle_t *fhp,
174     struct nfsvattr *nap, nfsattrbit_t *attrbitp, int laytype, NFSPROC_T *p);
175 static int nfsrv_cbcallargs(struct nfsrv_descript *nd, struct nfsclient *clp,
176     uint32_t callback, int op, const char *optag, struct nfsdsession **sepp,
177     int *slotposp);
178 static u_int32_t nfsrv_nextclientindex(void);
179 static u_int32_t nfsrv_nextstateindex(struct nfsclient *clp);
180 static void nfsrv_markstable(struct nfsclient *clp);
181 static void nfsrv_markreclaim(struct nfsclient *clp);
182 static int nfsrv_checkstable(struct nfsclient *clp);
183 static int nfsrv_clientconflict(struct nfsclient *clp, int *haslockp, struct
184     vnode *vp, NFSPROC_T *p);
185 static int nfsrv_delegconflict(struct nfsstate *stp, int *haslockp,
186     NFSPROC_T *p, vnode_t vp);
187 static int nfsrv_cleandeleg(vnode_t vp, struct nfslockfile *lfp,
188     struct nfsclient *clp, int *haslockp, NFSPROC_T *p);
189 static int nfsrv_notsamecredname(int op, struct nfsrv_descript *nd,
190     struct nfsclient *clp);
191 static time_t nfsrv_leaseexpiry(void);
192 static void nfsrv_delaydelegtimeout(struct nfsstate *stp);
193 static int nfsrv_checkseqid(struct nfsrv_descript *nd, u_int32_t seqid,
194     struct nfsstate *stp, struct nfsrvcache *op);
195 static int nfsrv_nootherstate(struct nfsstate *stp);
196 static int nfsrv_locallock(vnode_t vp, struct nfslockfile *lfp, int flags,
197     uint64_t first, uint64_t end, struct nfslockconflict *cfp, NFSPROC_T *p);
198 static void nfsrv_localunlock(vnode_t vp, struct nfslockfile *lfp,
199     uint64_t init_first, uint64_t init_end, NFSPROC_T *p);
200 static int nfsrv_dolocal(vnode_t vp, struct nfslockfile *lfp, int flags,
201     int oldflags, uint64_t first, uint64_t end, struct nfslockconflict *cfp,
202     NFSPROC_T *p);
203 static void nfsrv_locallock_rollback(vnode_t vp, struct nfslockfile *lfp,
204     NFSPROC_T *p);
205 static void nfsrv_locallock_commit(struct nfslockfile *lfp, int flags,
206     uint64_t first, uint64_t end);
207 static void nfsrv_locklf(struct nfslockfile *lfp);
208 static void nfsrv_unlocklf(struct nfslockfile *lfp);
209 static struct nfsdsession *nfsrv_findsession(uint8_t *sessionid);
210 static int nfsrv_freesession(struct nfsrv_descript *nd, struct nfsdsession *sep,
211     uint8_t *sessionid, bool locked, SVCXPRT **old_xprtp);
212 static int nfsv4_setcbsequence(struct nfsrv_descript *nd, struct nfsclient *clp,
213     int dont_replycache, struct nfsdsession **sepp, int *slotposp);
214 static int nfsv4_getcbsession(struct nfsclient *clp, struct nfsdsession **sepp);
215 static int nfsrv_addlayout(struct nfsrv_descript *nd, struct nfslayout **lypp,
216     nfsv4stateid_t *stateidp, char *layp, int *layoutlenp, NFSPROC_T *p);
217 static void nfsrv_freelayout(struct nfslayouthead *lhp, struct nfslayout *lyp);
218 static void nfsrv_freelayoutlist(nfsquad_t clientid);
219 static void nfsrv_freelayouts(nfsquad_t *clid, fsid_t *fs, int laytype,
220     int iomode);
221 static void nfsrv_freealllayouts(void);
222 static void nfsrv_freedevid(struct nfsdevice *ds);
223 static int nfsrv_setdsserver(char *dspathp, char *mdspathp, NFSPROC_T *p,
224     struct nfsdevice **dsp);
225 static void nfsrv_deleteds(struct nfsdevice *fndds);
226 static void nfsrv_allocdevid(struct nfsdevice *ds, char *addr, char *dnshost);
227 static void nfsrv_freealldevids(void);
228 static void nfsrv_flexlayouterr(struct nfsrv_descript *nd, uint32_t *layp,
229     int maxcnt, NFSPROC_T *p);
230 static int nfsrv_recalllayout(nfsquad_t clid, nfsv4stateid_t *stateidp,
231     fhandle_t *fhp, struct nfslayout *lyp, int changed, int laytype,
232     NFSPROC_T *p);
233 static int nfsrv_findlayout(nfsquad_t *clientidp, fhandle_t *fhp, int laytype,
234     NFSPROC_T *, struct nfslayout **lypp);
235 static int nfsrv_fndclid(nfsquad_t *clidvec, nfsquad_t clid, int clidcnt);
236 static struct nfslayout *nfsrv_filelayout(struct nfsrv_descript *nd, int iomode,
237     fhandle_t *fhp, fhandle_t *dsfhp, char *devid, fsid_t fs);
238 static struct nfslayout *nfsrv_flexlayout(struct nfsrv_descript *nd, int iomode,
239     int mirrorcnt, fhandle_t *fhp, fhandle_t *dsfhp, char *devid, fsid_t fs);
240 static int nfsrv_dontlayout(fhandle_t *fhp);
241 static int nfsrv_createdsfile(vnode_t vp, fhandle_t *fhp, struct pnfsdsfile *pf,
242     vnode_t dvp, struct nfsdevice *ds, struct ucred *cred, NFSPROC_T *p,
243     vnode_t *tvpp);
244 static struct nfsdevice *nfsrv_findmirroredds(struct nfsmount *nmp);
245 static int nfsrv_checkmachcred(int op, struct nfsrv_descript *nd,
246     struct nfsclient *clp);
247 static void nfsrv_issuedelegation(struct vnode *vp, struct nfsclient *clp,
248     struct nfsrv_descript *nd, int delegate, int writedeleg, int readonly,
249     u_quad_t filerev, uint64_t rdonly, struct nfsstate **new_delegp,
250     struct nfsstate *new_stp, struct nfslockfile *lfp, uint32_t *rflagsp,
251     nfsv4stateid_t *delegstateidp);
252 static void nfsrv_clientlock(bool mlocked);
253 static void nfsrv_clientunlock(bool mlocked);
254 
255 /*
256  * Lock the client structure, either with the mutex or the exclusive nfsd lock.
257  */
258 static void
nfsrv_clientlock(bool mlocked)259 nfsrv_clientlock(bool mlocked)
260 {
261 	int igotlock;
262 
263 	if (mlocked) {
264 		NFSLOCKSTATE();
265 	} else {
266 		NFSLOCKV4ROOTMUTEX();
267 		nfsv4_relref(&nfsv4rootfs_lock);
268 		do {
269 			igotlock = nfsv4_lock(&nfsv4rootfs_lock, 1, NULL,
270 			    NFSV4ROOTLOCKMUTEXPTR, NULL);
271 		} while (!igotlock);
272 		NFSUNLOCKV4ROOTMUTEX();
273 	}
274 }
275 
276 /*
277  * Unlock the client structure.
278  */
279 static void
nfsrv_clientunlock(bool mlocked)280 nfsrv_clientunlock(bool mlocked)
281 {
282 
283 	if (mlocked) {
284 		NFSUNLOCKSTATE();
285 	} else {
286 		NFSLOCKV4ROOTMUTEX();
287 		nfsv4_unlock(&nfsv4rootfs_lock, 1);
288 		NFSUNLOCKV4ROOTMUTEX();
289 	}
290 }
291 
292 /*
293  * Scan the client list for a match and either return the current one,
294  * create a new entry or return an error.
295  * If returning a non-error, the clp structure must either be linked into
296  * the client list or free'd.
297  */
298 int
nfsrv_setclient(struct nfsrv_descript * nd,struct nfsclient ** new_clpp,nfsquad_t * clientidp,nfsquad_t * confirmp,NFSPROC_T * p)299 nfsrv_setclient(struct nfsrv_descript *nd, struct nfsclient **new_clpp,
300     nfsquad_t *clientidp, nfsquad_t *confirmp, NFSPROC_T *p)
301 {
302 	struct nfsclient *clp = NULL, *new_clp = *new_clpp;
303 	int i, error = 0, ret;
304 	struct nfsstate *stp, *tstp;
305 #ifdef INET
306 	struct sockaddr_in *sin, *rin;
307 #endif
308 #ifdef INET6
309 	struct sockaddr_in6 *sin6, *rin6;
310 #endif
311 	struct nfsdsession *sep, *nsep;
312 	SVCXPRT *old_xprt;
313 	struct nfssessionhead old_sess;
314 	int zapit = 0, gotit, hasstate = 0;
315 	bool mlocked;
316 	static u_int64_t confirm_index = 0;
317 
318 	/*
319 	 * Check for state resource limit exceeded.
320 	 */
321 	if (nfsrv_openpluslock > nfsrv_v4statelimit) {
322 		error = NFSERR_RESOURCE;
323 		goto out;
324 	}
325 
326 	if (nfsrv_issuedelegs == 0 ||
327 	    ((nd->nd_flag & ND_GSS) != 0 && nfsrv_nogsscallback != 0))
328 		/*
329 		 * Don't do callbacks when delegations are disabled or
330 		 * for AUTH_GSS unless enabled via nfsrv_nogsscallback.
331 		 * If establishing a callback connection is attempted
332 		 * when a firewall is blocking the callback path, the
333 		 * server may wait too long for the connect attempt to
334 		 * succeed during the Open. Some clients, such as Linux,
335 		 * may timeout and give up on the Open before the server
336 		 * replies. Also, since AUTH_GSS callbacks are not
337 		 * yet interoperability tested, they might cause the
338 		 * server to crap out, if they get past the Init call to
339 		 * the client.
340 		 */
341 		new_clp->lc_program = 0;
342 
343 	mlocked = true;
344 	if (nfsrv_dolocallocks != 0)
345 		mlocked = false;
346 	/* Lock out other nfsd threads */
347 	nfsrv_clientlock(mlocked);
348 
349 	/*
350 	 * Search for a match in the client list.
351 	 */
352 	gotit = i = 0;
353 	while (i < nfsrv_clienthashsize && !gotit) {
354 	    LIST_FOREACH(clp, &VNET(nfsclienthash)[i], lc_hash) {
355 		if (new_clp->lc_idlen == clp->lc_idlen &&
356 		    !NFSBCMP(new_clp->lc_id, clp->lc_id, clp->lc_idlen)) {
357 			gotit = 1;
358 			break;
359 		}
360 	    }
361 	    if (gotit == 0)
362 		i++;
363 	}
364 	old_xprt = NULL;
365 	if (!gotit ||
366 	    (clp->lc_flags & (LCL_NEEDSCONFIRM | LCL_ADMINREVOKED))) {
367 		if ((nd->nd_flag & ND_NFSV41) != 0 && confirmp->lval[1] != 0) {
368 			/*
369 			 * For NFSv4.1, if confirmp->lval[1] is non-zero, the
370 			 * client is trying to update a confirmed clientid.
371 			 */
372 			nfsrv_clientunlock(mlocked);
373 			confirmp->lval[1] = 0;
374 			error = NFSERR_NOENT;
375 			goto out;
376 		}
377 		/*
378 		 * Get rid of the old one.
379 		 */
380 		if (i != nfsrv_clienthashsize) {
381 			LIST_REMOVE(clp, lc_hash);
382 			if (mlocked)
383 				nfsrv_cleanclient(clp, p, true, &old_xprt);
384 			else
385 				nfsrv_cleanclient(clp, p, false, NULL);
386 			nfsrv_freedeleglist(&clp->lc_deleg);
387 			nfsrv_freedeleglist(&clp->lc_olddeleg);
388 			zapit = 1;
389 		}
390 		/*
391 		 * Add it after assigning a client id to it.
392 		 */
393 		new_clp->lc_flags |= LCL_NEEDSCONFIRM;
394 		if ((nd->nd_flag & ND_NFSV41) != 0) {
395 			confirmp->lval[0] = ++confirm_index;
396 			new_clp->lc_confirm.lval[0] = confirmp->lval[0] - 1;
397 		} else
398 			confirmp->qval = new_clp->lc_confirm.qval =
399 			    ++confirm_index;
400 		clientidp->lval[0] = new_clp->lc_clientid.lval[0] =
401 		    VNET(nfsrvboottime);
402 		clientidp->lval[1] = new_clp->lc_clientid.lval[1] =
403 		    nfsrv_nextclientindex();
404 		new_clp->lc_stateindex = 0;
405 		new_clp->lc_statemaxindex = 0;
406 		new_clp->lc_prevsess = 0;
407 		new_clp->lc_cbref = 0;
408 		new_clp->lc_expiry = nfsrv_leaseexpiry();
409 		LIST_INIT(&new_clp->lc_open);
410 		LIST_INIT(&new_clp->lc_deleg);
411 		LIST_INIT(&new_clp->lc_olddeleg);
412 		LIST_INIT(&new_clp->lc_session);
413 		for (i = 0; i < nfsrv_statehashsize; i++)
414 			LIST_INIT(&new_clp->lc_stateid[i]);
415 		LIST_INSERT_HEAD(NFSCLIENTHASH(new_clp->lc_clientid), new_clp,
416 		    lc_hash);
417 		VNET(nfsstatsv1_p)->srvclients++;
418 		nfsrv_openpluslock++;
419 		nfsrv_clients++;
420 		nfsrv_clientunlock(mlocked);
421 		if (zapit != 0) {
422 			if (old_xprt != NULL)
423 				SVC_RELEASE(old_xprt);
424 			nfsrv_zapclient(clp, p);
425 		}
426 		*new_clpp = NULL;
427 		goto out;
428 	}
429 
430 	/*
431 	 * Now, handle the cases where the id is already issued.
432 	 */
433 	if (nfsrv_notsamecredname(NFSV4OP_EXCHANGEID, nd, clp)) {
434 	    /*
435 	     * Check to see if there is expired state that should go away.
436 	     */
437 	    if (clp->lc_expiry < NFSD_MONOSEC &&
438 	        (!LIST_EMPTY(&clp->lc_open) || !LIST_EMPTY(&clp->lc_deleg))) {
439 		if (mlocked)
440 		    nfsrv_cleanclient(clp, p, true, &old_xprt);
441 		else
442 		    nfsrv_cleanclient(clp, p, false, NULL);
443 		nfsrv_freedeleglist(&clp->lc_deleg);
444 	    }
445 
446 	    /*
447 	     * If there is outstanding state, then reply NFSERR_CLIDINUSE per
448 	     * RFC3530 Sec. 8.1.2 last para.
449 	     */
450 	    if (!LIST_EMPTY(&clp->lc_deleg)) {
451 		hasstate = 1;
452 	    } else if (LIST_EMPTY(&clp->lc_open)) {
453 		hasstate = 0;
454 	    } else {
455 		hasstate = 0;
456 		/* Look for an Open on the OpenOwner */
457 		LIST_FOREACH(stp, &clp->lc_open, ls_list) {
458 		    if (!LIST_EMPTY(&stp->ls_open)) {
459 			hasstate = 1;
460 			break;
461 		    }
462 		}
463 	    }
464 	    if (hasstate) {
465 		/*
466 		 * If the uid doesn't match, return NFSERR_CLIDINUSE after
467 		 * filling out the correct ipaddr and portnum.
468 		 */
469 		switch (clp->lc_req.nr_nam->sa_family) {
470 #ifdef INET
471 		case AF_INET:
472 			sin = (struct sockaddr_in *)new_clp->lc_req.nr_nam;
473 			rin = (struct sockaddr_in *)clp->lc_req.nr_nam;
474 			sin->sin_addr.s_addr = rin->sin_addr.s_addr;
475 			sin->sin_port = rin->sin_port;
476 			break;
477 #endif
478 #ifdef INET6
479 		case AF_INET6:
480 			sin6 = (struct sockaddr_in6 *)new_clp->lc_req.nr_nam;
481 			rin6 = (struct sockaddr_in6 *)clp->lc_req.nr_nam;
482 			sin6->sin6_addr = rin6->sin6_addr;
483 			sin6->sin6_port = rin6->sin6_port;
484 			break;
485 #endif
486 		}
487 		nfsrv_clientunlock(mlocked);
488 		if (old_xprt != NULL)
489 			SVC_RELEASE(old_xprt);
490 		error = NFSERR_CLIDINUSE;
491 		goto out;
492 	    }
493 	}
494 
495 	if (NFSBCMP(new_clp->lc_verf, clp->lc_verf, NFSX_VERF)) {
496 		/*
497 		 * If the verifier has changed, the client has rebooted
498 		 * and a new client id is issued. The old state info
499 		 * can be thrown away once the SetClientID_Confirm or
500 		 * Create_Session that confirms the clientid occurs.
501 		 */
502 		LIST_REMOVE(clp, lc_hash);
503 
504 		LIST_NEWHEAD(&old_sess, &clp->lc_session, sess_list);
505 
506 		new_clp->lc_flags |= LCL_NEEDSCONFIRM;
507 		if ((nd->nd_flag & ND_NFSV41) != 0) {
508 			confirmp->lval[0] = ++confirm_index;
509 			new_clp->lc_confirm.lval[0] = confirmp->lval[0] - 1;
510 		} else
511 			confirmp->qval = new_clp->lc_confirm.qval =
512 			    ++confirm_index;
513 		clientidp->lval[0] = new_clp->lc_clientid.lval[0] =
514 		    VNET(nfsrvboottime);
515 		clientidp->lval[1] = new_clp->lc_clientid.lval[1] =
516 		    nfsrv_nextclientindex();
517 		new_clp->lc_stateindex = 0;
518 		new_clp->lc_statemaxindex = 0;
519 		new_clp->lc_prevsess = 0;
520 		new_clp->lc_cbref = 0;
521 		new_clp->lc_expiry = nfsrv_leaseexpiry();
522 
523 		/*
524 		 * Save the state until confirmed.
525 		 */
526 		LIST_NEWHEAD(&new_clp->lc_open, &clp->lc_open, ls_list);
527 		LIST_FOREACH(tstp, &new_clp->lc_open, ls_list)
528 			tstp->ls_clp = new_clp;
529 		LIST_NEWHEAD(&new_clp->lc_deleg, &clp->lc_deleg, ls_list);
530 		LIST_FOREACH(tstp, &new_clp->lc_deleg, ls_list)
531 			tstp->ls_clp = new_clp;
532 		LIST_NEWHEAD(&new_clp->lc_olddeleg, &clp->lc_olddeleg,
533 		    ls_list);
534 		LIST_FOREACH(tstp, &new_clp->lc_olddeleg, ls_list)
535 			tstp->ls_clp = new_clp;
536 		for (i = 0; i < nfsrv_statehashsize; i++) {
537 			LIST_NEWHEAD(&new_clp->lc_stateid[i],
538 			    &clp->lc_stateid[i], ls_hash);
539 			LIST_FOREACH(tstp, &new_clp->lc_stateid[i], ls_hash)
540 				tstp->ls_clp = new_clp;
541 		}
542 		LIST_INIT(&new_clp->lc_session);
543 		LIST_INSERT_HEAD(NFSCLIENTHASH(new_clp->lc_clientid), new_clp,
544 		    lc_hash);
545 		VNET(nfsstatsv1_p)->srvclients++;
546 		nfsrv_openpluslock++;
547 		nfsrv_clients++;
548 		if (!mlocked) {
549 			nfsrv_clientunlock(mlocked);
550 			NFSLOCKSTATE();
551 		}
552 
553 		/*
554 		 * Must wait until any outstanding callback on the old clp
555 		 * completes.
556 		 */
557 		while (clp->lc_cbref) {
558 			clp->lc_flags |= LCL_WAKEUPWANTED;
559 			(void)mtx_sleep(clp, NFSSTATEMUTEXPTR, PVFS,
560 			    "nfsd clp", 10 * hz);
561 		}
562 		NFSUNLOCKSTATE();
563 		if (old_xprt != NULL)
564 			SVC_RELEASE(old_xprt);
565 		/* Get rid of all sessions on this clientid. */
566 		LIST_FOREACH_SAFE(sep, &old_sess, sess_list, nsep) {
567 			ret = nfsrv_freesession(NULL, sep, NULL, false, NULL);
568 			if (ret != 0)
569 				printf("nfsrv_setclient: verifier changed free"
570 				    " session failed=%d\n", ret);
571 		}
572 
573 		nfsrv_zapclient(clp, p);
574 		*new_clpp = NULL;
575 		goto out;
576 	}
577 
578 	/* For NFSv4.1, mark that we found a confirmed clientid. */
579 	if ((nd->nd_flag & ND_NFSV41) != 0) {
580 		clientidp->lval[0] = clp->lc_clientid.lval[0];
581 		clientidp->lval[1] = clp->lc_clientid.lval[1];
582 		confirmp->lval[0] = 0;	/* Ignored by client */
583 		confirmp->lval[1] = 1;
584 	} else {
585 		/*
586 		 * id and verifier match, so update the net address info
587 		 * and get rid of any existing callback authentication
588 		 * handle, so a new one will be acquired.
589 		 */
590 		LIST_REMOVE(clp, lc_hash);
591 		new_clp->lc_flags |= (LCL_NEEDSCONFIRM | LCL_DONTCLEAN);
592 		new_clp->lc_expiry = nfsrv_leaseexpiry();
593 		confirmp->qval = new_clp->lc_confirm.qval = ++confirm_index;
594 		clientidp->lval[0] = new_clp->lc_clientid.lval[0] =
595 		    clp->lc_clientid.lval[0];
596 		clientidp->lval[1] = new_clp->lc_clientid.lval[1] =
597 		    clp->lc_clientid.lval[1];
598 		new_clp->lc_delegtime = clp->lc_delegtime;
599 		new_clp->lc_stateindex = clp->lc_stateindex;
600 		new_clp->lc_statemaxindex = clp->lc_statemaxindex;
601 		new_clp->lc_cbref = 0;
602 		LIST_NEWHEAD(&new_clp->lc_open, &clp->lc_open, ls_list);
603 		LIST_FOREACH(tstp, &new_clp->lc_open, ls_list)
604 			tstp->ls_clp = new_clp;
605 		LIST_NEWHEAD(&new_clp->lc_deleg, &clp->lc_deleg, ls_list);
606 		LIST_FOREACH(tstp, &new_clp->lc_deleg, ls_list)
607 			tstp->ls_clp = new_clp;
608 		LIST_NEWHEAD(&new_clp->lc_olddeleg, &clp->lc_olddeleg, ls_list);
609 		LIST_FOREACH(tstp, &new_clp->lc_olddeleg, ls_list)
610 			tstp->ls_clp = new_clp;
611 		for (i = 0; i < nfsrv_statehashsize; i++) {
612 			LIST_NEWHEAD(&new_clp->lc_stateid[i],
613 			    &clp->lc_stateid[i], ls_hash);
614 			LIST_FOREACH(tstp, &new_clp->lc_stateid[i], ls_hash)
615 				tstp->ls_clp = new_clp;
616 		}
617 		LIST_INIT(&new_clp->lc_session);
618 		LIST_INSERT_HEAD(NFSCLIENTHASH(new_clp->lc_clientid), new_clp,
619 		    lc_hash);
620 		VNET(nfsstatsv1_p)->srvclients++;
621 		nfsrv_openpluslock++;
622 		nfsrv_clients++;
623 	}
624 	if (!mlocked)
625 		nfsrv_clientunlock(mlocked);
626 
627 	if ((nd->nd_flag & ND_NFSV41) == 0) {
628 		/*
629 		 * Must wait until any outstanding callback on the old clp
630 		 * completes.
631 		 */
632 		if (!mlocked)
633 			NFSLOCKSTATE();
634 		while (clp->lc_cbref) {
635 			clp->lc_flags |= LCL_WAKEUPWANTED;
636 			(void)mtx_sleep(clp, NFSSTATEMUTEXPTR, PVFS,
637 			    "nfsdclp", 10 * hz);
638 		}
639 		NFSUNLOCKSTATE();
640 		if (old_xprt != NULL)
641 			SVC_RELEASE(old_xprt);
642 		nfsrv_zapclient(clp, p);
643 		*new_clpp = NULL;
644 	} else {
645 		if (mlocked)
646 			NFSUNLOCKSTATE();
647 		if (old_xprt != NULL)
648 			SVC_RELEASE(old_xprt);
649 	}
650 
651 out:
652 	NFSEXITCODE2(error, nd);
653 	return (error);
654 }
655 
656 /*
657  * Check to see if the client id exists and optionally confirm it.
658  */
659 int
nfsrv_getclient(nfsquad_t clientid,int opflags,struct nfsclient ** clpp,struct nfsdsession * nsep,nfsquad_t confirm,uint32_t cbprogram,struct nfsrv_descript * nd,NFSPROC_T * p)660 nfsrv_getclient(nfsquad_t clientid, int opflags, struct nfsclient **clpp,
661     struct nfsdsession *nsep, nfsquad_t confirm, uint32_t cbprogram,
662     struct nfsrv_descript *nd, NFSPROC_T *p)
663 {
664 	struct nfsclient *clp;
665 	struct nfsstate *stp;
666 	int i;
667 	struct nfsclienthashhead *hp;
668 	int error = 0, doneok, igotlock;
669 	struct nfssessionhash *shp;
670 	struct nfsdsession *sep;
671 	uint64_t sessid[2];
672 	CLIENT *client;
673 	SVCXPRT *old_xprt;
674 	bool mlocked, sess_replay;
675 	static uint64_t next_sess = 0;
676 
677 	if (clpp)
678 		*clpp = NULL;
679 	if ((nd == NULL || (nd->nd_flag & ND_NFSV41) == 0 ||
680 	    opflags != CLOPS_RENEW) && VNET(nfsrvboottime) !=
681 	    clientid.lval[0]) {
682 		error = NFSERR_STALECLIENTID;
683 		goto out;
684 	}
685 
686 	/*
687 	 * If called with opflags == CLOPS_RENEW, the State Lock is
688 	 * already held. Otherwise, we need to get either that or,
689 	 * for the case of Confirm, lock out the nfsd threads.
690 	 */
691 	client = NULL;
692 	old_xprt = NULL;
693 	mlocked = true;
694 	if (nfsrv_dolocallocks != 0)
695 		mlocked = false;
696 	if (opflags & CLOPS_CONFIRM) {
697 		if (nsep != NULL &&
698 		    (nsep->sess_crflags & NFSV4CRSESS_CONNBACKCHAN) != 0)
699 			client = (struct __rpc_client *)
700 			    clnt_bck_create(nd->nd_xprt->xp_socket,
701 			    cbprogram, NFSV4_CBVERS);
702 		if (mlocked) {
703 			nfsrv_clientlock(mlocked);
704 		} else {
705 			NFSLOCKV4ROOTMUTEX();
706 			nfsv4_relref(&nfsv4rootfs_lock);
707 			do {
708 				igotlock = nfsv4_lock(&nfsv4rootfs_lock, 1,
709 				    NULL, NFSV4ROOTLOCKMUTEXPTR, NULL);
710 			} while (!igotlock);
711 		}
712 		/*
713 		 * Create a new sessionid here, since we need to do it where
714 		 * there is a mutex held to serialize update of next_sess.
715 		 */
716 		if ((nd->nd_flag & ND_NFSV41) != 0) {
717 			sessid[0] = ++next_sess;
718 			sessid[1] = clientid.qval;
719 		}
720 		if (!mlocked)
721 			NFSUNLOCKV4ROOTMUTEX();
722 	} else if (opflags != CLOPS_RENEW) {
723 		NFSLOCKSTATE();
724 	}
725 
726 	/* For NFSv4.1, the clp is acquired from the associated session. */
727 	if (nd != NULL && (nd->nd_flag & ND_NFSV41) != 0 &&
728 	    opflags == CLOPS_RENEW) {
729 		clp = NULL;
730 		if ((nd->nd_flag & ND_HASSEQUENCE) != 0) {
731 			shp = NFSSESSIONHASH(nd->nd_sessionid);
732 			NFSLOCKSESSION(shp);
733 			sep = nfsrv_findsession(nd->nd_sessionid);
734 			if (sep != NULL)
735 				clp = sep->sess_clp;
736 			NFSUNLOCKSESSION(shp);
737 		}
738 	} else {
739 		hp = NFSCLIENTHASH(clientid);
740 		LIST_FOREACH(clp, hp, lc_hash) {
741 			if (clp->lc_clientid.lval[1] == clientid.lval[1])
742 				break;
743 		}
744 	}
745 	if (clp == NULL) {
746 		if (opflags & CLOPS_CONFIRM)
747 			error = NFSERR_STALECLIENTID;
748 		else
749 			error = NFSERR_EXPIRED;
750 	} else if (clp->lc_flags & LCL_ADMINREVOKED) {
751 		/*
752 		 * If marked admin revoked, just return the error.
753 		 */
754 		error = NFSERR_ADMINREVOKED;
755 	}
756 	if (error) {
757 		if (opflags & CLOPS_CONFIRM) {
758 			nfsrv_clientunlock(mlocked);
759 			if (client != NULL)
760 				CLNT_RELEASE(client);
761 		} else if (opflags != CLOPS_RENEW) {
762 			NFSUNLOCKSTATE();
763 		}
764 		goto out;
765 	}
766 
767 	/*
768 	 * Perform any operations specified by the opflags.
769 	 */
770 	if (opflags & CLOPS_CONFIRM) {
771 		sess_replay = false;
772 		if ((nd->nd_flag & ND_NFSV41) != 0) {
773 		    /*
774 		     * For the case where lc_confirm.lval[0] == confirm.lval[0],
775 		     * use the new session, but with the previous sessionid.
776 		     * This is not exactly what the RFC describes, but should
777 		     * result in the same reply as the previous CreateSession.
778 		     */
779 		    if (clp->lc_confirm.lval[0] + 1 == confirm.lval[0]) {
780 			clp->lc_confirm.lval[0] = confirm.lval[0];
781 			clp->lc_prevsess = sessid[0];
782 		    } else if (clp->lc_confirm.lval[0] == confirm.lval[0]) {
783 			if (clp->lc_prevsess == 0)
784 			    error = NFSERR_SEQMISORDERED;
785 			else
786 			    sessid[0] = clp->lc_prevsess;
787 			sess_replay = true;
788 		    } else
789 			error = NFSERR_SEQMISORDERED;
790 		} else if ((nd->nd_flag & ND_NFSV41) == 0 &&
791 		     clp->lc_confirm.qval != confirm.qval)
792 			error = NFSERR_STALECLIENTID;
793 		if (error == 0 && nfsrv_notsamecredname(NFSV4OP_CREATESESSION,
794 		    nd, clp))
795 			error = NFSERR_CLIDINUSE;
796 
797 		if (!error) {
798 		    if ((clp->lc_flags & (LCL_NEEDSCONFIRM | LCL_DONTCLEAN)) ==
799 			LCL_NEEDSCONFIRM) {
800 			/*
801 			 * Hang onto the delegations (as old delegations)
802 			 * for an Open with CLAIM_DELEGATE_PREV unless in
803 			 * grace, but get rid of the rest of the state.
804 			 */
805 			if (mlocked)
806 				nfsrv_cleanclient(clp, p, true, &old_xprt);
807 			else
808 				nfsrv_cleanclient(clp, p, false, NULL);
809 			nfsrv_freedeleglist(&clp->lc_olddeleg);
810 			if (nfsrv_checkgrace(nd, clp, 0)) {
811 			    /* In grace, so just delete delegations */
812 			    nfsrv_freedeleglist(&clp->lc_deleg);
813 			} else {
814 			    LIST_FOREACH(stp, &clp->lc_deleg, ls_list)
815 				stp->ls_flags |= NFSLCK_OLDDELEG;
816 			    clp->lc_delegtime = NFSD_MONOSEC +
817 				nfsrv_lease + NFSRV_LEASEDELTA;
818 			    LIST_NEWHEAD(&clp->lc_olddeleg, &clp->lc_deleg,
819 				ls_list);
820 			}
821 			if ((nd->nd_flag & ND_NFSV41) != 0)
822 			    clp->lc_program = cbprogram;
823 		    }
824 		    clp->lc_flags &= ~(LCL_NEEDSCONFIRM | LCL_DONTCLEAN);
825 		    if (clp->lc_program)
826 			clp->lc_flags |= LCL_NEEDSCBNULL;
827 		    /* For NFSv4.1, link the session onto the client. */
828 		    if (nsep != NULL) {
829 			/* Hold a reference on the xprt for a backchannel. */
830 			if ((nsep->sess_crflags & NFSV4CRSESS_CONNBACKCHAN)
831 			    != 0 && !sess_replay) {
832 			    if (clp->lc_req.nr_client == NULL) {
833 				clp->lc_req.nr_client = client;
834 				client = NULL;
835 			    }
836 			    if (clp->lc_req.nr_client != NULL) {
837 				SVC_ACQUIRE(nd->nd_xprt);
838 				CLNT_ACQUIRE(clp->lc_req.nr_client);
839 				nd->nd_xprt->xp_p2 = clp->lc_req.nr_client;
840 				/* Disable idle timeout. */
841 				nd->nd_xprt->xp_idletimeout = 0;
842 				nsep->sess_cbsess.nfsess_xprt = nd->nd_xprt;
843 			    } else
844 				nsep->sess_crflags &= ~NFSV4CRSESS_CONNBACKCHAN;
845 			}
846 			NFSBCOPY(sessid, nsep->sess_sessionid,
847 			    NFSX_V4SESSIONID);
848 			NFSBCOPY(sessid, nsep->sess_cbsess.nfsess_sessionid,
849 			    NFSX_V4SESSIONID);
850 			if (!sess_replay) {
851 			    shp = NFSSESSIONHASH(nsep->sess_sessionid);
852 			    if (!mlocked)
853 				NFSLOCKSTATE();
854 			    NFSLOCKSESSION(shp);
855 			    LIST_INSERT_HEAD(&shp->list, nsep, sess_hash);
856 			    LIST_INSERT_HEAD(&clp->lc_session, nsep, sess_list);
857 			    nsep->sess_clp = clp;
858 			    NFSUNLOCKSESSION(shp);
859 			    if (!mlocked)
860 				NFSUNLOCKSTATE();
861 			}
862 		    }
863 		}
864 	} else if (clp->lc_flags & LCL_NEEDSCONFIRM) {
865 		error = NFSERR_EXPIRED;
866 	}
867 
868 	/*
869 	 * If called by the Renew Op, we must check the principal.
870 	 */
871 	if (!error && (opflags & CLOPS_RENEWOP)) {
872 	    if (nfsrv_notsamecredname(0, nd, clp)) {
873 		doneok = 0;
874 		for (i = 0; i < nfsrv_statehashsize && doneok == 0; i++) {
875 		    LIST_FOREACH(stp, &clp->lc_stateid[i], ls_hash) {
876 			if ((stp->ls_flags & NFSLCK_OPEN) &&
877 			    stp->ls_uid == nd->nd_cred->cr_uid) {
878 				doneok = 1;
879 				break;
880 			}
881 		    }
882 		}
883 		if (!doneok)
884 			error = NFSERR_ACCES;
885 	    }
886 	    if (!error && (clp->lc_flags & LCL_CBDOWN))
887 		error = NFSERR_CBPATHDOWN;
888 	}
889 	if ((!error || error == NFSERR_CBPATHDOWN) &&
890 	     (opflags & CLOPS_RENEW)) {
891 		clp->lc_expiry = nfsrv_leaseexpiry();
892 	}
893 	if (opflags & CLOPS_CONFIRM) {
894 		nfsrv_clientunlock(mlocked);
895 		if (client != NULL)
896 			CLNT_RELEASE(client);
897 		if (old_xprt != NULL)
898 			SVC_RELEASE(old_xprt);
899 	} else if (opflags != CLOPS_RENEW) {
900 		NFSUNLOCKSTATE();
901 	}
902 	if (clpp)
903 		*clpp = clp;
904 
905 out:
906 	NFSEXITCODE2(error, nd);
907 	return (error);
908 }
909 
910 /*
911  * Perform the NFSv4.1 destroy clientid.
912  */
913 int
nfsrv_destroyclient(struct nfsrv_descript * nd,nfsquad_t clientid,NFSPROC_T * p)914 nfsrv_destroyclient(struct nfsrv_descript *nd, nfsquad_t clientid, NFSPROC_T *p)
915 {
916 	struct nfsclient *clp;
917 	struct nfsclienthashhead *hp;
918 	SVCXPRT *old_xprt;
919 	int error = 0, i;
920 	bool mlocked;
921 
922 	if (VNET(nfsrvboottime) != clientid.lval[0]) {
923 		error = NFSERR_STALECLIENTID;
924 		goto out;
925 	}
926 
927 	mlocked = true;
928 	if (nfsrv_dolocallocks != 0)
929 		mlocked = false;
930 	/* Lock out other nfsd threads */
931 	nfsrv_clientlock(mlocked);
932 
933 	hp = NFSCLIENTHASH(clientid);
934 	LIST_FOREACH(clp, hp, lc_hash) {
935 		if (clp->lc_clientid.lval[1] == clientid.lval[1])
936 			break;
937 	}
938 	if (clp == NULL) {
939 		nfsrv_clientunlock(mlocked);
940 		/* Just return ok, since it is gone. */
941 		goto out;
942 	}
943 
944 	/* Check for the SP4_MACH_CRED case. */
945 	error = nfsrv_checkmachcred(NFSV4OP_DESTROYCLIENTID, nd, clp);
946 	if (error != 0) {
947 		nfsrv_clientunlock(mlocked);
948 		goto out;
949 	}
950 
951 	/*
952 	 * Free up all layouts on the clientid.  Should the client return the
953 	 * layouts?
954 	 */
955 	nfsrv_freelayoutlist(clientid);
956 
957 	/* Scan for state on the clientid. */
958 	for (i = 0; i < nfsrv_statehashsize; i++)
959 		if (!LIST_EMPTY(&clp->lc_stateid[i])) {
960 			nfsrv_clientunlock(mlocked);
961 			error = NFSERR_CLIENTIDBUSY;
962 			goto out;
963 		}
964 	if (!LIST_EMPTY(&clp->lc_session) || !LIST_EMPTY(&clp->lc_deleg)) {
965 		nfsrv_clientunlock(mlocked);
966 		error = NFSERR_CLIENTIDBUSY;
967 		goto out;
968 	}
969 
970 	/* Destroy the clientid and return ok. */
971 	old_xprt = NULL;
972 	if (mlocked)
973 		nfsrv_cleanclient(clp, p, true, &old_xprt);
974 	else
975 		nfsrv_cleanclient(clp, p, false, NULL);
976 	nfsrv_freedeleglist(&clp->lc_deleg);
977 	nfsrv_freedeleglist(&clp->lc_olddeleg);
978 	LIST_REMOVE(clp, lc_hash);
979 	nfsrv_clientunlock(mlocked);
980 	if (old_xprt != NULL)
981 		SVC_RELEASE(old_xprt);
982 	nfsrv_zapclient(clp, p);
983 out:
984 	NFSEXITCODE2(error, nd);
985 	return (error);
986 }
987 
988 /*
989  * Called from the new nfssvc syscall to admin revoke a clientid.
990  * Returns 0 for success, error otherwise.
991  */
992 int
nfsrv_adminrevoke(struct nfsd_clid * revokep,NFSPROC_T * p)993 nfsrv_adminrevoke(struct nfsd_clid *revokep, NFSPROC_T *p)
994 {
995 	struct nfsclient *clp = NULL;
996 	int i, error = 0;
997 	int gotit, igotlock;
998 
999 	/*
1000 	 * First, lock out the nfsd so that state won't change while the
1001 	 * revocation record is being written to the stable storage restart
1002 	 * file.
1003 	 */
1004 	NFSLOCKV4ROOTMUTEX();
1005 	do {
1006 		igotlock = nfsv4_lock(&nfsv4rootfs_lock, 1, NULL,
1007 		    NFSV4ROOTLOCKMUTEXPTR, NULL);
1008 	} while (!igotlock);
1009 	NFSUNLOCKV4ROOTMUTEX();
1010 
1011 	/*
1012 	 * Search for a match in the client list.
1013 	 */
1014 	gotit = i = 0;
1015 	while (i < nfsrv_clienthashsize && !gotit) {
1016 	    LIST_FOREACH(clp, &VNET(nfsclienthash)[i], lc_hash) {
1017 		if (revokep->nclid_idlen == clp->lc_idlen &&
1018 		    !NFSBCMP(revokep->nclid_id, clp->lc_id, clp->lc_idlen)) {
1019 			gotit = 1;
1020 			break;
1021 		}
1022 	    }
1023 	    i++;
1024 	}
1025 	if (!gotit) {
1026 		NFSLOCKV4ROOTMUTEX();
1027 		nfsv4_unlock(&nfsv4rootfs_lock, 0);
1028 		NFSUNLOCKV4ROOTMUTEX();
1029 		error = EPERM;
1030 		goto out;
1031 	}
1032 
1033 	/*
1034 	 * Now, write out the revocation record
1035 	 */
1036 	nfsrv_writestable(clp->lc_id, clp->lc_idlen, NFSNST_REVOKE, p);
1037 	nfsrv_backupstable();
1038 
1039 	/*
1040 	 * and clear out the state, marking the clientid revoked.
1041 	 */
1042 	clp->lc_flags &= ~LCL_CALLBACKSON;
1043 	clp->lc_flags |= LCL_ADMINREVOKED;
1044 	nfsrv_cleanclient(clp, p, false, NULL);
1045 	nfsrv_freedeleglist(&clp->lc_deleg);
1046 	nfsrv_freedeleglist(&clp->lc_olddeleg);
1047 	NFSLOCKV4ROOTMUTEX();
1048 	nfsv4_unlock(&nfsv4rootfs_lock, 0);
1049 	NFSUNLOCKV4ROOTMUTEX();
1050 
1051 out:
1052 	NFSEXITCODE(error);
1053 	return (error);
1054 }
1055 
1056 /*
1057  * Dump out stats for all clients. Called from nfssvc(2), that is used
1058  * nfsstatsv1.
1059  */
1060 void
nfsrv_dumpclients(struct nfsd_dumpclients * dumpp,int maxcnt)1061 nfsrv_dumpclients(struct nfsd_dumpclients *dumpp, int maxcnt)
1062 {
1063 	struct nfsclient *clp;
1064 	int i = 0, cnt = 0;
1065 
1066 	/*
1067 	 * First, get a reference on the nfsv4rootfs_lock so that an
1068 	 * exclusive lock cannot be acquired while dumping the clients.
1069 	 */
1070 	NFSLOCKV4ROOTMUTEX();
1071 	nfsv4_getref(&nfsv4rootfs_lock, NULL, NFSV4ROOTLOCKMUTEXPTR, NULL);
1072 	NFSUNLOCKV4ROOTMUTEX();
1073 	NFSLOCKSTATE();
1074 	/*
1075 	 * Rattle through the client lists until done.
1076 	 */
1077 	while (i < nfsrv_clienthashsize && cnt < maxcnt) {
1078 	    clp = LIST_FIRST(&VNET(nfsclienthash)[i]);
1079 	    while (clp != LIST_END(&VNET(nfsclienthash)[i]) && cnt <
1080 		maxcnt) {
1081 		nfsrv_dumpaclient(clp, &dumpp[cnt]);
1082 		cnt++;
1083 		clp = LIST_NEXT(clp, lc_hash);
1084 	    }
1085 	    i++;
1086 	}
1087 	if (cnt < maxcnt)
1088 	    dumpp[cnt].ndcl_clid.nclid_idlen = 0;
1089 	NFSUNLOCKSTATE();
1090 	NFSLOCKV4ROOTMUTEX();
1091 	nfsv4_relref(&nfsv4rootfs_lock);
1092 	NFSUNLOCKV4ROOTMUTEX();
1093 }
1094 
1095 /*
1096  * Dump stats for a client. Must be called with the NFSSTATELOCK and spl'd.
1097  */
1098 static void
nfsrv_dumpaclient(struct nfsclient * clp,struct nfsd_dumpclients * dumpp)1099 nfsrv_dumpaclient(struct nfsclient *clp, struct nfsd_dumpclients *dumpp)
1100 {
1101 	struct nfsstate *stp, *openstp, *lckownstp;
1102 	struct nfslock *lop;
1103 	sa_family_t af;
1104 #ifdef INET
1105 	struct sockaddr_in *rin;
1106 #endif
1107 #ifdef INET6
1108 	struct sockaddr_in6 *rin6;
1109 #endif
1110 
1111 	dumpp->ndcl_nopenowners = dumpp->ndcl_nlockowners = 0;
1112 	dumpp->ndcl_nopens = dumpp->ndcl_nlocks = 0;
1113 	dumpp->ndcl_ndelegs = dumpp->ndcl_nolddelegs = 0;
1114 	dumpp->ndcl_flags = clp->lc_flags;
1115 	dumpp->ndcl_clid.nclid_idlen = clp->lc_idlen;
1116 	NFSBCOPY(clp->lc_id, dumpp->ndcl_clid.nclid_id, clp->lc_idlen);
1117 	af = clp->lc_req.nr_nam->sa_family;
1118 	dumpp->ndcl_addrfam = af;
1119 	switch (af) {
1120 #ifdef INET
1121 	case AF_INET:
1122 		rin = (struct sockaddr_in *)clp->lc_req.nr_nam;
1123 		dumpp->ndcl_cbaddr.sin_addr = rin->sin_addr;
1124 		break;
1125 #endif
1126 #ifdef INET6
1127 	case AF_INET6:
1128 		rin6 = (struct sockaddr_in6 *)clp->lc_req.nr_nam;
1129 		dumpp->ndcl_cbaddr.sin6_addr = rin6->sin6_addr;
1130 		break;
1131 #endif
1132 	}
1133 
1134 	/*
1135 	 * Now, scan the state lists and total up the opens and locks.
1136 	 */
1137 	LIST_FOREACH(stp, &clp->lc_open, ls_list) {
1138 	    dumpp->ndcl_nopenowners++;
1139 	    LIST_FOREACH(openstp, &stp->ls_open, ls_list) {
1140 		dumpp->ndcl_nopens++;
1141 		LIST_FOREACH(lckownstp, &openstp->ls_open, ls_list) {
1142 		    dumpp->ndcl_nlockowners++;
1143 		    LIST_FOREACH(lop, &lckownstp->ls_lock, lo_lckowner) {
1144 			dumpp->ndcl_nlocks++;
1145 		    }
1146 		}
1147 	    }
1148 	}
1149 
1150 	/*
1151 	 * and the delegation lists.
1152 	 */
1153 	LIST_FOREACH(stp, &clp->lc_deleg, ls_list) {
1154 	    dumpp->ndcl_ndelegs++;
1155 	}
1156 	LIST_FOREACH(stp, &clp->lc_olddeleg, ls_list) {
1157 	    dumpp->ndcl_nolddelegs++;
1158 	}
1159 }
1160 
1161 /*
1162  * Dump out lock stats for a file.
1163  */
1164 void
nfsrv_dumplocks(vnode_t vp,struct nfsd_dumplocks * ldumpp,int maxcnt,NFSPROC_T * p)1165 nfsrv_dumplocks(vnode_t vp, struct nfsd_dumplocks *ldumpp, int maxcnt,
1166     NFSPROC_T *p)
1167 {
1168 	struct nfsstate *stp;
1169 	struct nfslock *lop;
1170 	int cnt = 0;
1171 	struct nfslockfile *lfp;
1172 	sa_family_t af;
1173 #ifdef INET
1174 	struct sockaddr_in *rin;
1175 #endif
1176 #ifdef INET6
1177 	struct sockaddr_in6 *rin6;
1178 #endif
1179 	int ret;
1180 	fhandle_t nfh;
1181 
1182 	ret = nfsrv_getlockfh(vp, 0, NULL, &nfh, p);
1183 	/*
1184 	 * First, get a reference on the nfsv4rootfs_lock so that an
1185 	 * exclusive lock on it cannot be acquired while dumping the locks.
1186 	 */
1187 	NFSLOCKV4ROOTMUTEX();
1188 	nfsv4_getref(&nfsv4rootfs_lock, NULL, NFSV4ROOTLOCKMUTEXPTR, NULL);
1189 	NFSUNLOCKV4ROOTMUTEX();
1190 	NFSLOCKSTATE();
1191 	if (!ret)
1192 		ret = nfsrv_getlockfile(0, NULL, &lfp, &nfh, 0);
1193 	if (ret) {
1194 		ldumpp[0].ndlck_clid.nclid_idlen = 0;
1195 		NFSUNLOCKSTATE();
1196 		NFSLOCKV4ROOTMUTEX();
1197 		nfsv4_relref(&nfsv4rootfs_lock);
1198 		NFSUNLOCKV4ROOTMUTEX();
1199 		return;
1200 	}
1201 
1202 	/*
1203 	 * For each open share on file, dump it out.
1204 	 */
1205 	stp = LIST_FIRST(&lfp->lf_open);
1206 	while (stp != LIST_END(&lfp->lf_open) && cnt < maxcnt) {
1207 		ldumpp[cnt].ndlck_flags = stp->ls_flags;
1208 		ldumpp[cnt].ndlck_stateid.seqid = stp->ls_stateid.seqid;
1209 		ldumpp[cnt].ndlck_stateid.other[0] = stp->ls_stateid.other[0];
1210 		ldumpp[cnt].ndlck_stateid.other[1] = stp->ls_stateid.other[1];
1211 		ldumpp[cnt].ndlck_stateid.other[2] = stp->ls_stateid.other[2];
1212 		ldumpp[cnt].ndlck_owner.nclid_idlen =
1213 		    stp->ls_openowner->ls_ownerlen;
1214 		NFSBCOPY(stp->ls_openowner->ls_owner,
1215 		    ldumpp[cnt].ndlck_owner.nclid_id,
1216 		    stp->ls_openowner->ls_ownerlen);
1217 		ldumpp[cnt].ndlck_clid.nclid_idlen = stp->ls_clp->lc_idlen;
1218 		NFSBCOPY(stp->ls_clp->lc_id, ldumpp[cnt].ndlck_clid.nclid_id,
1219 		    stp->ls_clp->lc_idlen);
1220 		af = stp->ls_clp->lc_req.nr_nam->sa_family;
1221 		ldumpp[cnt].ndlck_addrfam = af;
1222 		switch (af) {
1223 #ifdef INET
1224 		case AF_INET:
1225 			rin = (struct sockaddr_in *)stp->ls_clp->lc_req.nr_nam;
1226 			ldumpp[cnt].ndlck_cbaddr.sin_addr = rin->sin_addr;
1227 			break;
1228 #endif
1229 #ifdef INET6
1230 		case AF_INET6:
1231 			rin6 = (struct sockaddr_in6 *)
1232 			    stp->ls_clp->lc_req.nr_nam;
1233 			ldumpp[cnt].ndlck_cbaddr.sin6_addr = rin6->sin6_addr;
1234 			break;
1235 #endif
1236 		}
1237 		stp = LIST_NEXT(stp, ls_file);
1238 		cnt++;
1239 	}
1240 
1241 	/*
1242 	 * and all locks.
1243 	 */
1244 	lop = LIST_FIRST(&lfp->lf_lock);
1245 	while (lop != LIST_END(&lfp->lf_lock) && cnt < maxcnt) {
1246 		stp = lop->lo_stp;
1247 		ldumpp[cnt].ndlck_flags = lop->lo_flags;
1248 		ldumpp[cnt].ndlck_first = lop->lo_first;
1249 		ldumpp[cnt].ndlck_end = lop->lo_end;
1250 		ldumpp[cnt].ndlck_stateid.seqid = stp->ls_stateid.seqid;
1251 		ldumpp[cnt].ndlck_stateid.other[0] = stp->ls_stateid.other[0];
1252 		ldumpp[cnt].ndlck_stateid.other[1] = stp->ls_stateid.other[1];
1253 		ldumpp[cnt].ndlck_stateid.other[2] = stp->ls_stateid.other[2];
1254 		ldumpp[cnt].ndlck_owner.nclid_idlen = stp->ls_ownerlen;
1255 		NFSBCOPY(stp->ls_owner, ldumpp[cnt].ndlck_owner.nclid_id,
1256 		    stp->ls_ownerlen);
1257 		ldumpp[cnt].ndlck_clid.nclid_idlen = stp->ls_clp->lc_idlen;
1258 		NFSBCOPY(stp->ls_clp->lc_id, ldumpp[cnt].ndlck_clid.nclid_id,
1259 		    stp->ls_clp->lc_idlen);
1260 		af = stp->ls_clp->lc_req.nr_nam->sa_family;
1261 		ldumpp[cnt].ndlck_addrfam = af;
1262 		switch (af) {
1263 #ifdef INET
1264 		case AF_INET:
1265 			rin = (struct sockaddr_in *)stp->ls_clp->lc_req.nr_nam;
1266 			ldumpp[cnt].ndlck_cbaddr.sin_addr = rin->sin_addr;
1267 			break;
1268 #endif
1269 #ifdef INET6
1270 		case AF_INET6:
1271 			rin6 = (struct sockaddr_in6 *)
1272 			    stp->ls_clp->lc_req.nr_nam;
1273 			ldumpp[cnt].ndlck_cbaddr.sin6_addr = rin6->sin6_addr;
1274 			break;
1275 #endif
1276 		}
1277 		lop = LIST_NEXT(lop, lo_lckfile);
1278 		cnt++;
1279 	}
1280 
1281 	/*
1282 	 * and the delegations.
1283 	 */
1284 	stp = LIST_FIRST(&lfp->lf_deleg);
1285 	while (stp != LIST_END(&lfp->lf_deleg) && cnt < maxcnt) {
1286 		ldumpp[cnt].ndlck_flags = stp->ls_flags;
1287 		ldumpp[cnt].ndlck_stateid.seqid = stp->ls_stateid.seqid;
1288 		ldumpp[cnt].ndlck_stateid.other[0] = stp->ls_stateid.other[0];
1289 		ldumpp[cnt].ndlck_stateid.other[1] = stp->ls_stateid.other[1];
1290 		ldumpp[cnt].ndlck_stateid.other[2] = stp->ls_stateid.other[2];
1291 		ldumpp[cnt].ndlck_owner.nclid_idlen = 0;
1292 		ldumpp[cnt].ndlck_clid.nclid_idlen = stp->ls_clp->lc_idlen;
1293 		NFSBCOPY(stp->ls_clp->lc_id, ldumpp[cnt].ndlck_clid.nclid_id,
1294 		    stp->ls_clp->lc_idlen);
1295 		af = stp->ls_clp->lc_req.nr_nam->sa_family;
1296 		ldumpp[cnt].ndlck_addrfam = af;
1297 		switch (af) {
1298 #ifdef INET
1299 		case AF_INET:
1300 			rin = (struct sockaddr_in *)stp->ls_clp->lc_req.nr_nam;
1301 			ldumpp[cnt].ndlck_cbaddr.sin_addr = rin->sin_addr;
1302 			break;
1303 #endif
1304 #ifdef INET6
1305 		case AF_INET6:
1306 			rin6 = (struct sockaddr_in6 *)
1307 			    stp->ls_clp->lc_req.nr_nam;
1308 			ldumpp[cnt].ndlck_cbaddr.sin6_addr = rin6->sin6_addr;
1309 			break;
1310 #endif
1311 		}
1312 		stp = LIST_NEXT(stp, ls_file);
1313 		cnt++;
1314 	}
1315 
1316 	/*
1317 	 * If list isn't full, mark end of list by setting the client name
1318 	 * to zero length.
1319 	 */
1320 	if (cnt < maxcnt)
1321 		ldumpp[cnt].ndlck_clid.nclid_idlen = 0;
1322 	NFSUNLOCKSTATE();
1323 	NFSLOCKV4ROOTMUTEX();
1324 	nfsv4_relref(&nfsv4rootfs_lock);
1325 	NFSUNLOCKV4ROOTMUTEX();
1326 }
1327 
1328 /*
1329  * Server timer routine. It can scan any linked list, so long
1330  * as it holds the spin/mutex lock and there is no exclusive lock on
1331  * nfsv4rootfs_lock.
1332  * (For OpenBSD, a kthread is ok. For FreeBSD, I think it is ok
1333  *  to do this from a callout, since the spin locks work. For
1334  *  Darwin, I'm not sure what will work correctly yet.)
1335  * Should be called once per second.
1336  */
1337 void
nfsrv_servertimer(void * arg __unused)1338 nfsrv_servertimer(void *arg __unused)
1339 {
1340 	struct nfsclient *clp, *nclp;
1341 	struct nfsstate *stp, *nstp;
1342 	int got_ref, i;
1343 
1344 	/*
1345 	 * Make sure nfsboottime is set. This is used by V3 as well
1346 	 * as V4. Note that nfsboottime is not nfsrvboottime, which is
1347 	 * only used by the V4 server for leases.
1348 	 */
1349 	if (nfsboottime.tv_sec == 0)
1350 		NFSSETBOOTTIME(nfsboottime);
1351 
1352 	/*
1353 	 * If server hasn't started yet, just return.
1354 	 */
1355 	NFSLOCKSTATE();
1356 	if (VNET(nfsrv_stablefirst).nsf_eograce == 0) {
1357 		NFSUNLOCKSTATE();
1358 		return;
1359 	}
1360 	if (!(VNET(nfsrv_stablefirst).nsf_flags & NFSNSF_UPDATEDONE)) {
1361 		if (!(VNET(nfsrv_stablefirst).nsf_flags &
1362 		      NFSNSF_GRACEOVER) &&
1363 		    NFSD_MONOSEC > VNET(nfsrv_stablefirst).nsf_eograce)
1364 			VNET(nfsrv_stablefirst).nsf_flags |=
1365 			    (NFSNSF_GRACEOVER | NFSNSF_NEEDLOCK);
1366 		NFSUNLOCKSTATE();
1367 		return;
1368 	}
1369 
1370 	/*
1371 	 * Try and get a reference count on the nfsv4rootfs_lock so that
1372 	 * no nfsd thread can acquire an exclusive lock on it before this
1373 	 * call is done. If it is already exclusively locked, just return.
1374 	 */
1375 	NFSLOCKV4ROOTMUTEX();
1376 	got_ref = nfsv4_getref_nonblock(&nfsv4rootfs_lock);
1377 	NFSUNLOCKV4ROOTMUTEX();
1378 	if (got_ref == 0) {
1379 		NFSUNLOCKSTATE();
1380 		return;
1381 	}
1382 
1383 	/*
1384 	 * For each client...
1385 	 */
1386 	for (i = 0; i < nfsrv_clienthashsize; i++) {
1387 	    clp = LIST_FIRST(&VNET(nfsclienthash)[i]);
1388 	    while (clp != LIST_END(&VNET(nfsclienthash)[i])) {
1389 		nclp = LIST_NEXT(clp, lc_hash);
1390 		if (!(clp->lc_flags & LCL_EXPIREIT)) {
1391 		    if (((clp->lc_expiry + NFSRV_STALELEASE) < NFSD_MONOSEC
1392 			 && ((LIST_EMPTY(&clp->lc_deleg)
1393 			      && LIST_EMPTY(&clp->lc_open)) ||
1394 			     nfsrv_clients > nfsrv_clienthighwater)) ||
1395 			(clp->lc_expiry + NFSRV_MOULDYLEASE) < NFSD_MONOSEC ||
1396 			(clp->lc_expiry < NFSD_MONOSEC &&
1397 			 (nfsrv_openpluslock * 10 / 9) > nfsrv_v4statelimit)) {
1398 			/*
1399 			 * Lease has expired several nfsrv_lease times ago:
1400 			 * PLUS
1401 			 *    - no state is associated with it
1402 			 *    OR
1403 			 *    - above high water mark for number of clients
1404 			 *      (nfsrv_clienthighwater should be large enough
1405 			 *       that this only occurs when clients fail to
1406 			 *       use the same nfs_client_id4.id. Maybe somewhat
1407 			 *       higher that the maximum number of clients that
1408 			 *       will mount this server?)
1409 			 * OR
1410 			 * Lease has expired a very long time ago
1411 			 * OR
1412 			 * Lease has expired PLUS the number of opens + locks
1413 			 * has exceeded 90% of capacity
1414 			 *
1415 			 * --> Mark for expiry. The actual expiry will be done
1416 			 *     by an nfsd sometime soon.
1417 			 */
1418 			clp->lc_flags |= LCL_EXPIREIT;
1419 			VNET(nfsrv_stablefirst).nsf_flags |=
1420 			    (NFSNSF_NEEDLOCK | NFSNSF_EXPIREDCLIENT);
1421 		    } else {
1422 			/*
1423 			 * If there are no opens, increment no open tick cnt
1424 			 * If time exceeds NFSNOOPEN, mark it to be thrown away
1425 			 * otherwise, if there is an open, reset no open time
1426 			 * Hopefully, this will avoid excessive re-creation
1427 			 * of open owners and subsequent open confirms.
1428 			 */
1429 			stp = LIST_FIRST(&clp->lc_open);
1430 			while (stp != LIST_END(&clp->lc_open)) {
1431 				nstp = LIST_NEXT(stp, ls_list);
1432 				if (LIST_EMPTY(&stp->ls_open)) {
1433 					stp->ls_noopens++;
1434 					if (stp->ls_noopens > NFSNOOPEN ||
1435 					    (nfsrv_openpluslock * 2) >
1436 					    nfsrv_v4statelimit)
1437 						VNET(nfsrv_stablefirst).nsf_flags |=
1438 							NFSNSF_NOOPENS;
1439 				} else {
1440 					stp->ls_noopens = 0;
1441 				}
1442 				stp = nstp;
1443 			}
1444 		    }
1445 		}
1446 		clp = nclp;
1447 	    }
1448 	}
1449 	NFSUNLOCKSTATE();
1450 	NFSLOCKV4ROOTMUTEX();
1451 	nfsv4_relref(&nfsv4rootfs_lock);
1452 	NFSUNLOCKV4ROOTMUTEX();
1453 }
1454 
1455 /*
1456  * The following set of functions free up the various data structures.
1457  */
1458 /*
1459  * Clear out all open/lock state related to this nfsclient.
1460  * Caller must hold an exclusive lock on nfsv4rootfs_lock, so that
1461  * there are no other active nfsd threads.
1462  */
1463 void
nfsrv_cleanclient(struct nfsclient * clp,NFSPROC_T * p,bool locked,SVCXPRT ** old_xprtp)1464 nfsrv_cleanclient(struct nfsclient *clp, NFSPROC_T *p, bool locked,
1465     SVCXPRT **old_xprtp)
1466 {
1467 	struct nfsstate *stp, *nstp;
1468 	struct nfsdsession *sep, *nsep;
1469 
1470 	LIST_FOREACH_SAFE(stp, &clp->lc_open, ls_list, nstp) {
1471 		if (locked)
1472 			nfsrv_freeopenowner(stp, 0, p);
1473 		else
1474 			nfsrv_freeopenowner(stp, 1, p);
1475 	}
1476 	if ((clp->lc_flags & LCL_ADMINREVOKED) == 0)
1477 		LIST_FOREACH_SAFE(sep, &clp->lc_session, sess_list, nsep)
1478 			(void)nfsrv_freesession(NULL, sep, NULL, locked,
1479 			    old_xprtp);
1480 }
1481 
1482 /*
1483  * Free a client that has been cleaned. It should also already have been
1484  * removed from the lists.
1485  * (Just to be safe w.r.t. newnfs_disconnect(), call this function when
1486  *  softclock interrupts are enabled.)
1487  */
1488 void
nfsrv_zapclient(struct nfsclient * clp,NFSPROC_T * p)1489 nfsrv_zapclient(struct nfsclient *clp, NFSPROC_T *p)
1490 {
1491 
1492 #ifdef notyet
1493 	if ((clp->lc_flags & (LCL_GSS | LCL_CALLBACKSON)) ==
1494 	     (LCL_GSS | LCL_CALLBACKSON) &&
1495 	    (clp->lc_hand.nfsh_flag & NFSG_COMPLETE) &&
1496 	    clp->lc_handlelen > 0) {
1497 		clp->lc_hand.nfsh_flag &= ~NFSG_COMPLETE;
1498 		clp->lc_hand.nfsh_flag |= NFSG_DESTROYED;
1499 		(void) nfsrv_docallback(clp, NFSV4PROC_CBNULL,
1500 			NULL, 0, NULL, NULL, NULL, 0, p);
1501 	}
1502 #endif
1503 	newnfs_disconnect(NULL, &clp->lc_req);
1504 	free(clp->lc_req.nr_nam, M_SONAME);
1505 	NFSFREEMUTEX(&clp->lc_req.nr_mtx);
1506 	crfree(clp->lc_req.nr_cred);
1507 	free(clp->lc_stateid, M_NFSDCLIENT);
1508 	free(clp, M_NFSDCLIENT);
1509 	NFSLOCKSTATE();
1510 	VNET(nfsstatsv1_p)->srvclients--;
1511 	nfsrv_openpluslock--;
1512 	nfsrv_clients--;
1513 	NFSUNLOCKSTATE();
1514 }
1515 
1516 /*
1517  * Free a list of delegation state structures.
1518  * (This function will also free all nfslockfile structures that no
1519  *  longer have associated state.)
1520  */
1521 void
nfsrv_freedeleglist(struct nfsstatehead * sthp)1522 nfsrv_freedeleglist(struct nfsstatehead *sthp)
1523 {
1524 	struct nfsstate *stp, *nstp;
1525 
1526 	LIST_FOREACH_SAFE(stp, sthp, ls_list, nstp) {
1527 		nfsrv_freedeleg(stp);
1528 	}
1529 	LIST_INIT(sthp);
1530 }
1531 
1532 /*
1533  * Free up a delegation.
1534  */
1535 static void
nfsrv_freedeleg(struct nfsstate * stp)1536 nfsrv_freedeleg(struct nfsstate *stp)
1537 {
1538 	struct nfslockfile *lfp;
1539 
1540 	LIST_REMOVE(stp, ls_hash);
1541 	LIST_REMOVE(stp, ls_list);
1542 	LIST_REMOVE(stp, ls_file);
1543 	if ((stp->ls_flags & NFSLCK_DELEGWRITE) != 0)
1544 		nfsrv_writedelegcnt--;
1545 	lfp = stp->ls_lfp;
1546 	if (LIST_EMPTY(&lfp->lf_open) &&
1547 	    LIST_EMPTY(&lfp->lf_lock) && LIST_EMPTY(&lfp->lf_deleg) &&
1548 	    LIST_EMPTY(&lfp->lf_locallock) && LIST_EMPTY(&lfp->lf_rollback) &&
1549 	    lfp->lf_usecount == 0 &&
1550 	    nfsv4_testlock(&lfp->lf_locallock_lck) == 0)
1551 		nfsrv_freenfslockfile(lfp);
1552 	free(stp, M_NFSDSTATE);
1553 	VNET(nfsstatsv1_p)->srvdelegates--;
1554 	nfsrv_openpluslock--;
1555 	nfsrv_delegatecnt--;
1556 }
1557 
1558 /*
1559  * This function frees an open owner and all associated opens.
1560  */
1561 static void
nfsrv_freeopenowner(struct nfsstate * stp,int cansleep,NFSPROC_T * p)1562 nfsrv_freeopenowner(struct nfsstate *stp, int cansleep, NFSPROC_T *p)
1563 {
1564 	struct nfsstate *nstp, *tstp;
1565 
1566 	LIST_REMOVE(stp, ls_list);
1567 	/*
1568 	 * Now, free all associated opens.
1569 	 */
1570 	nstp = LIST_FIRST(&stp->ls_open);
1571 	while (nstp != LIST_END(&stp->ls_open)) {
1572 		tstp = nstp;
1573 		nstp = LIST_NEXT(nstp, ls_list);
1574 		nfsrv_freeopen(tstp, NULL, cansleep, p);
1575 	}
1576 	if (stp->ls_op)
1577 		nfsrvd_derefcache(stp->ls_op);
1578 	free(stp, M_NFSDSTATE);
1579 	VNET(nfsstatsv1_p)->srvopenowners--;
1580 	nfsrv_openpluslock--;
1581 }
1582 
1583 /*
1584  * This function frees an open (nfsstate open structure) with all associated
1585  * lock_owners and locks. It also frees the nfslockfile structure iff there
1586  * are no other opens on the file.
1587  * Returns 1 if it free'd the nfslockfile, 0 otherwise.
1588  */
1589 static void
nfsrv_freeopen(struct nfsstate * stp,vnode_t vp,int cansleep,NFSPROC_T * p)1590 nfsrv_freeopen(struct nfsstate *stp, vnode_t vp, int cansleep, NFSPROC_T *p)
1591 {
1592 	struct nfsstate *nstp, *tstp;
1593 	struct nfslockfile *lfp;
1594 
1595 	LIST_REMOVE(stp, ls_hash);
1596 	LIST_REMOVE(stp, ls_list);
1597 	LIST_REMOVE(stp, ls_file);
1598 
1599 	lfp = stp->ls_lfp;
1600 	/*
1601 	 * Now, free all lockowners associated with this open.
1602 	 * Note that, if vp != NULL, nfsrv_freelockowner() will
1603 	 * not call nfsrv_freeallnfslocks(), so it needs to be called, below.
1604 	 */
1605 	LIST_FOREACH_SAFE(tstp, &stp->ls_open, ls_list, nstp)
1606 		nfsrv_freelockowner(tstp, vp, cansleep, p);
1607 
1608 	if (vp != NULL) {
1609 		KASSERT(cansleep != 0, ("nfsrv_freeopen: cansleep == 0"));
1610 		mtx_assert(NFSSTATEMUTEXPTR, MA_OWNED);
1611 		/*
1612 		 * Only called with vp != NULL for Close when
1613 		 * vfs.nfsd.enable_locallocks != 0.
1614 		 * Lock the lfp so that it will not go away and do the
1615 		 * nfsrv_freeallnfslocks() call that was not done by
1616 		 * nfsrv_freelockowner().
1617 		 */
1618 		nfsrv_locklf(lfp);
1619 		NFSUNLOCKSTATE();
1620 		NFSVOPUNLOCK(vp);
1621 		nfsrv_freeallnfslocks(stp, vp, cansleep, p);
1622 		NFSVOPLOCK(vp, LK_EXCLUSIVE | LK_RETRY);
1623 		NFSLOCKSTATE();
1624 		nfsrv_unlocklf(lfp);
1625 	}
1626 
1627 	/*
1628 	 * The nfslockfile is freed here if there are no locks
1629 	 * associated with the open.
1630 	 * If there are locks associated with the open, the
1631 	 * nfslockfile structure can be freed via nfsrv_freelockowner().
1632 	 */
1633 	if (lfp != NULL && LIST_EMPTY(&lfp->lf_open) &&
1634 	    LIST_EMPTY(&lfp->lf_deleg) && LIST_EMPTY(&lfp->lf_lock) &&
1635 	    LIST_EMPTY(&lfp->lf_locallock) && LIST_EMPTY(&lfp->lf_rollback) &&
1636 	    lfp->lf_usecount == 0 &&
1637 	    nfsv4_testlock(&lfp->lf_locallock_lck) == 0)
1638 		nfsrv_freenfslockfile(lfp);
1639 	free(stp, M_NFSDSTATE);
1640 	VNET(nfsstatsv1_p)->srvopens--;
1641 	nfsrv_openpluslock--;
1642 }
1643 
1644 /*
1645  * Frees a lockowner and all associated locks.
1646  */
1647 static void
nfsrv_freelockowner(struct nfsstate * stp,vnode_t vp,int cansleep,NFSPROC_T * p)1648 nfsrv_freelockowner(struct nfsstate *stp, vnode_t vp, int cansleep,
1649     NFSPROC_T *p)
1650 {
1651 
1652 	LIST_REMOVE(stp, ls_hash);
1653 	LIST_REMOVE(stp, ls_list);
1654 	if (vp == NULL)
1655 		nfsrv_freeallnfslocks(stp, vp, cansleep, p);
1656 	if (stp->ls_op)
1657 		nfsrvd_derefcache(stp->ls_op);
1658 	free(stp, M_NFSDSTATE);
1659 	VNET(nfsstatsv1_p)->srvlockowners--;
1660 	nfsrv_openpluslock--;
1661 }
1662 
1663 /*
1664  * Free all the nfs locks on a lockowner.
1665  */
1666 static void
nfsrv_freeallnfslocks(struct nfsstate * stp,vnode_t vp,int cansleep,NFSPROC_T * p)1667 nfsrv_freeallnfslocks(struct nfsstate *stp, vnode_t vp, int cansleep,
1668     NFSPROC_T *p)
1669 {
1670 	struct nfslock *lop, *nlop;
1671 	struct nfsrollback *rlp, *nrlp;
1672 	struct nfslockfile *lfp = NULL;
1673 	int gottvp = 0;
1674 	vnode_t tvp = NULL;
1675 	uint64_t first, end;
1676 
1677 	if (vp != NULL)
1678 		ASSERT_VOP_UNLOCKED(vp, "nfsrv_freeallnfslocks: vnode locked");
1679 	lop = LIST_FIRST(&stp->ls_lock);
1680 	while (lop != LIST_END(&stp->ls_lock)) {
1681 		nlop = LIST_NEXT(lop, lo_lckowner);
1682 		/*
1683 		 * Since all locks should be for the same file, lfp should
1684 		 * not change.
1685 		 */
1686 		if (lfp == NULL)
1687 			lfp = lop->lo_lfp;
1688 		else if (lfp != lop->lo_lfp)
1689 			panic("allnfslocks");
1690 		/*
1691 		 * If vp is NULL and cansleep != 0, a vnode must be acquired
1692 		 * from the file handle. This only occurs when called from
1693 		 * nfsrv_cleanclient().
1694 		 */
1695 		if (gottvp == 0) {
1696 			if (nfsrv_dolocallocks == 0)
1697 				tvp = NULL;
1698 			else if (vp == NULL && cansleep != 0) {
1699 				tvp = nfsvno_getvp(&lfp->lf_fh);
1700 				if (tvp != NULL)
1701 					NFSVOPUNLOCK(tvp);
1702 			} else
1703 				tvp = vp;
1704 			gottvp = 1;
1705 		}
1706 
1707 		if (tvp != NULL) {
1708 			if (cansleep == 0)
1709 				panic("allnfs2");
1710 			first = lop->lo_first;
1711 			end = lop->lo_end;
1712 			nfsrv_freenfslock(lop);
1713 			nfsrv_localunlock(tvp, lfp, first, end, p);
1714 			LIST_FOREACH_SAFE(rlp, &lfp->lf_rollback, rlck_list,
1715 			    nrlp)
1716 				free(rlp, M_NFSDROLLBACK);
1717 			LIST_INIT(&lfp->lf_rollback);
1718 		} else
1719 			nfsrv_freenfslock(lop);
1720 		lop = nlop;
1721 	}
1722 	if (vp == NULL && tvp != NULL)
1723 		vrele(tvp);
1724 }
1725 
1726 /*
1727  * Free an nfslock structure.
1728  */
1729 static void
nfsrv_freenfslock(struct nfslock * lop)1730 nfsrv_freenfslock(struct nfslock *lop)
1731 {
1732 
1733 	if (lop->lo_lckfile.le_prev != NULL) {
1734 		LIST_REMOVE(lop, lo_lckfile);
1735 		VNET(nfsstatsv1_p)->srvlocks--;
1736 		nfsrv_openpluslock--;
1737 	}
1738 	LIST_REMOVE(lop, lo_lckowner);
1739 	free(lop, M_NFSDLOCK);
1740 }
1741 
1742 /*
1743  * This function frees an nfslockfile structure.
1744  */
1745 static void
nfsrv_freenfslockfile(struct nfslockfile * lfp)1746 nfsrv_freenfslockfile(struct nfslockfile *lfp)
1747 {
1748 
1749 	LIST_REMOVE(lfp, lf_hash);
1750 	free(lfp, M_NFSDLOCKFILE);
1751 }
1752 
1753 /*
1754  * This function looks up an nfsstate structure via stateid.
1755  */
1756 static int
nfsrv_getstate(struct nfsclient * clp,nfsv4stateid_t * stateidp,__unused u_int32_t flags,struct nfsstate ** stpp)1757 nfsrv_getstate(struct nfsclient *clp, nfsv4stateid_t *stateidp, __unused u_int32_t flags,
1758     struct nfsstate **stpp)
1759 {
1760 	struct nfsstate *stp;
1761 	struct nfsstatehead *hp;
1762 	int error = 0;
1763 
1764 	*stpp = NULL;
1765 	hp = NFSSTATEHASH(clp, *stateidp);
1766 	LIST_FOREACH(stp, hp, ls_hash) {
1767 		if (!NFSBCMP(stp->ls_stateid.other, stateidp->other,
1768 			NFSX_STATEIDOTHER))
1769 			break;
1770 	}
1771 
1772 	/*
1773 	 * If no state id in list, return NFSERR_BADSTATEID.
1774 	 */
1775 	if (stp == LIST_END(hp)) {
1776 		error = NFSERR_BADSTATEID;
1777 		goto out;
1778 	}
1779 	*stpp = stp;
1780 
1781 out:
1782 	NFSEXITCODE(error);
1783 	return (error);
1784 }
1785 
1786 /*
1787  * This function gets an nfsstate structure via owner string.
1788  */
1789 static void
nfsrv_getowner(struct nfsstatehead * hp,struct nfsstate * new_stp,struct nfsstate ** stpp)1790 nfsrv_getowner(struct nfsstatehead *hp, struct nfsstate *new_stp,
1791     struct nfsstate **stpp)
1792 {
1793 	struct nfsstate *stp;
1794 
1795 	*stpp = NULL;
1796 	LIST_FOREACH(stp, hp, ls_list) {
1797 		if (new_stp->ls_ownerlen == stp->ls_ownerlen &&
1798 		  !NFSBCMP(new_stp->ls_owner,stp->ls_owner,stp->ls_ownerlen)) {
1799 			*stpp = stp;
1800 			return;
1801 		}
1802 	}
1803 }
1804 
1805 /*
1806  * Lock control function called to update lock status.
1807  * Returns 0 upon success, -1 if there is no lock and the flags indicate
1808  * that one isn't to be created and an NFSERR_xxx for other errors.
1809  * The structures new_stp and new_lop are passed in as pointers that should
1810  * be set to NULL if the structure is used and shouldn't be free'd.
1811  * For the NFSLCK_TEST and NFSLCK_CHECK cases, the structures are
1812  * never used and can safely be allocated on the stack. For all other
1813  * cases, *new_stpp and *new_lopp should be malloc'd before the call,
1814  * in case they are used.
1815  */
1816 int
nfsrv_lockctrl(vnode_t vp,struct nfsstate ** new_stpp,struct nfslock ** new_lopp,struct nfslockconflict * cfp,nfsquad_t clientid,nfsv4stateid_t * stateidp,__unused struct nfsexstuff * exp,struct nfsrv_descript * nd,NFSPROC_T * p)1817 nfsrv_lockctrl(vnode_t vp, struct nfsstate **new_stpp,
1818     struct nfslock **new_lopp, struct nfslockconflict *cfp,
1819     nfsquad_t clientid, nfsv4stateid_t *stateidp,
1820     __unused struct nfsexstuff *exp,
1821     struct nfsrv_descript *nd, NFSPROC_T *p)
1822 {
1823 	struct nfslock *lop;
1824 	struct nfsstate *new_stp = *new_stpp;
1825 	struct nfslock *new_lop = *new_lopp;
1826 	struct nfsstate *tstp, *mystp, *nstp;
1827 	int specialid = 0;
1828 	struct nfslockfile *lfp;
1829 	struct nfslock *other_lop = NULL;
1830 	struct nfsstate *stp, *lckstp = NULL;
1831 	struct nfsclient *clp = NULL;
1832 	u_int32_t bits;
1833 	int error = 0, haslock = 0, ret, reterr;
1834 	int getlckret, delegation = 0, filestruct_locked, vnode_unlocked = 0;
1835 	fhandle_t nfh;
1836 	uint64_t first, end;
1837 	uint32_t lock_flags;
1838 
1839 	if (new_stp->ls_flags & (NFSLCK_CHECK | NFSLCK_SETATTR)) {
1840 		/*
1841 		 * Note the special cases of "all 1s" or "all 0s" stateids and
1842 		 * let reads with all 1s go ahead.
1843 		 */
1844 		if (new_stp->ls_stateid.seqid == 0x0 &&
1845 		    new_stp->ls_stateid.other[0] == 0x0 &&
1846 		    new_stp->ls_stateid.other[1] == 0x0 &&
1847 		    new_stp->ls_stateid.other[2] == 0x0)
1848 			specialid = 1;
1849 		else if (new_stp->ls_stateid.seqid == 0xffffffff &&
1850 		    new_stp->ls_stateid.other[0] == 0xffffffff &&
1851 		    new_stp->ls_stateid.other[1] == 0xffffffff &&
1852 		    new_stp->ls_stateid.other[2] == 0xffffffff)
1853 			specialid = 2;
1854 	}
1855 
1856 	/*
1857 	 * Check for restart conditions (client and server).
1858 	 */
1859 	error = nfsrv_checkrestart(clientid, new_stp->ls_flags,
1860 	    &new_stp->ls_stateid, specialid);
1861 	if (error)
1862 		goto out;
1863 
1864 	/*
1865 	 * Check for state resource limit exceeded.
1866 	 */
1867 	if ((new_stp->ls_flags & NFSLCK_LOCK) &&
1868 	    nfsrv_openpluslock > nfsrv_v4statelimit) {
1869 		error = NFSERR_RESOURCE;
1870 		goto out;
1871 	}
1872 
1873 	/*
1874 	 * For the lock case, get another nfslock structure,
1875 	 * just in case we need it.
1876 	 * Malloc now, before we start sifting through the linked lists,
1877 	 * in case we have to wait for memory.
1878 	 */
1879 tryagain:
1880 	if (new_stp->ls_flags & NFSLCK_LOCK)
1881 		other_lop = malloc(sizeof (struct nfslock),
1882 		    M_NFSDLOCK, M_WAITOK);
1883 	filestruct_locked = 0;
1884 	reterr = 0;
1885 	lfp = NULL;
1886 
1887 	/*
1888 	 * Get the lockfile structure for CFH now, so we can do a sanity
1889 	 * check against the stateid, before incrementing the seqid#, since
1890 	 * we want to return NFSERR_BADSTATEID on failure and the seqid#
1891 	 * shouldn't be incremented for this case.
1892 	 * If nfsrv_getlockfile() returns -1, it means "not found", which
1893 	 * will be handled later.
1894 	 * If we are doing Lock/LockU and local locking is enabled, sleep
1895 	 * lock the nfslockfile structure.
1896 	 */
1897 	getlckret = nfsrv_getlockfh(vp, new_stp->ls_flags, NULL, &nfh, p);
1898 	NFSLOCKSTATE();
1899 	if (getlckret == 0) {
1900 		if ((new_stp->ls_flags & (NFSLCK_LOCK | NFSLCK_UNLOCK)) != 0 &&
1901 		    nfsrv_dolocallocks != 0 && nd->nd_repstat == 0) {
1902 			getlckret = nfsrv_getlockfile(new_stp->ls_flags, NULL,
1903 			    &lfp, &nfh, 1);
1904 			if (getlckret == 0)
1905 				filestruct_locked = 1;
1906 		} else
1907 			getlckret = nfsrv_getlockfile(new_stp->ls_flags, NULL,
1908 			    &lfp, &nfh, 0);
1909 	}
1910 	if (getlckret != 0 && getlckret != -1)
1911 		reterr = getlckret;
1912 
1913 	if (filestruct_locked != 0) {
1914 		LIST_INIT(&lfp->lf_rollback);
1915 		if ((new_stp->ls_flags & NFSLCK_LOCK)) {
1916 			/*
1917 			 * For local locking, do the advisory locking now, so
1918 			 * that any conflict can be detected. A failure later
1919 			 * can be rolled back locally. If an error is returned,
1920 			 * struct nfslockfile has been unlocked and any local
1921 			 * locking rolled back.
1922 			 */
1923 			NFSUNLOCKSTATE();
1924 			if (vnode_unlocked == 0) {
1925 				ASSERT_VOP_ELOCKED(vp, "nfsrv_lockctrl1");
1926 				vnode_unlocked = 1;
1927 				NFSVOPUNLOCK(vp);
1928 			}
1929 			reterr = nfsrv_locallock(vp, lfp,
1930 			    (new_lop->lo_flags & (NFSLCK_READ | NFSLCK_WRITE)),
1931 			    new_lop->lo_first, new_lop->lo_end, cfp, p);
1932 			NFSLOCKSTATE();
1933 		}
1934 	}
1935 
1936 	if (specialid == 0) {
1937 	    if (new_stp->ls_flags & NFSLCK_TEST) {
1938 		/*
1939 		 * RFC 3530 does not list LockT as an op that renews a
1940 		 * lease, but the consensus seems to be that it is ok
1941 		 * for a server to do so.
1942 		 */
1943 		error = nfsrv_getclient(clientid, CLOPS_RENEW, &clp, NULL,
1944 		    (nfsquad_t)((u_quad_t)0), 0, nd, p);
1945 
1946 		/*
1947 		 * Since NFSERR_EXPIRED, NFSERR_ADMINREVOKED are not valid
1948 		 * error returns for LockT, just go ahead and test for a lock,
1949 		 * since there are no locks for this client, but other locks
1950 		 * can conflict. (ie. same client will always be false)
1951 		 */
1952 		if (error == NFSERR_EXPIRED || error == NFSERR_ADMINREVOKED)
1953 		    error = 0;
1954 		lckstp = new_stp;
1955 	    } else {
1956 	      error = nfsrv_getclient(clientid, CLOPS_RENEW, &clp, NULL,
1957 		(nfsquad_t)((u_quad_t)0), 0, nd, p);
1958 	      if (error == 0)
1959 		/*
1960 		 * Look up the stateid
1961 		 */
1962 		error = nfsrv_getstate(clp, &new_stp->ls_stateid,
1963 		  new_stp->ls_flags, &stp);
1964 	      /*
1965 	       * do some sanity checks for an unconfirmed open or a
1966 	       * stateid that refers to the wrong file, for an open stateid
1967 	       */
1968 	      if (error == 0 && (stp->ls_flags & NFSLCK_OPEN) &&
1969 		  ((stp->ls_openowner->ls_flags & NFSLCK_NEEDSCONFIRM) ||
1970 		   (getlckret == 0 && stp->ls_lfp != lfp))){
1971 		      /*
1972 		       * NFSLCK_SETATTR should return OK rather than NFSERR_BADSTATEID
1973 		       * The only exception is using SETATTR with SIZE.
1974 		       * */
1975                     if ((new_stp->ls_flags &
1976                          (NFSLCK_SETATTR | NFSLCK_CHECK)) != NFSLCK_SETATTR)
1977 			     error = NFSERR_BADSTATEID;
1978 	      }
1979 
1980 	      /*
1981 	       * Sanity check the stateid for the Lock/LockU cases.
1982 	       */
1983 	      if (error == 0 && (new_stp->ls_flags & NFSLCK_LOCK) != 0 &&
1984 		  (((new_stp->ls_flags & NFSLCK_OPENTOLOCK) != 0 &&
1985 		    (stp->ls_flags & NFSLCK_OPEN) == 0) ||
1986 		   ((new_stp->ls_flags & NFSLCK_OPENTOLOCK) == 0 &&
1987 		    (stp->ls_flags & NFSLCK_LOCK) == 0)))
1988 			error = NFSERR_BADSTATEID;
1989 	      if (error == 0 && (new_stp->ls_flags & NFSLCK_UNLOCK) != 0 &&
1990 		  (stp->ls_flags & NFSLCK_LOCK) == 0)
1991 			error = NFSERR_BADSTATEID;
1992 
1993 		/* Sanity check the delegation stateid. */
1994 		if (error == 0 &&
1995 		  (stp->ls_flags & (NFSLCK_DELEGREAD | NFSLCK_DELEGWRITE)) &&
1996 		  getlckret == 0 && stp->ls_lfp != lfp)
1997 			error = NFSERR_BADSTATEID;
1998 
1999 	      /*
2000 	       * If the lockowner stateid doesn't refer to the same file,
2001 	       * I believe that is considered ok, since some clients will
2002 	       * only create a single lockowner and use that for all locks
2003 	       * on all files.
2004 	       * For now, log it as a diagnostic, instead of considering it
2005 	       * a BadStateid.
2006 	       */
2007 	      if (error == 0 && (stp->ls_flags &
2008 		  (NFSLCK_OPEN | NFSLCK_DELEGREAD | NFSLCK_DELEGWRITE)) == 0 &&
2009 		  getlckret == 0 && stp->ls_lfp != lfp) {
2010 #ifdef DIAGNOSTIC
2011 		  printf("Got a lock statid for different file open\n");
2012 #endif
2013 		  /*
2014 		  error = NFSERR_BADSTATEID;
2015 		  */
2016 	      }
2017 
2018 	      if (error == 0) {
2019 		    if (new_stp->ls_flags & NFSLCK_OPENTOLOCK) {
2020 			/*
2021 			 * If haslock set, we've already checked the seqid.
2022 			 */
2023 			if (!haslock) {
2024 			    if (stp->ls_flags & NFSLCK_OPEN)
2025 				error = nfsrv_checkseqid(nd, new_stp->ls_seq,
2026 				    stp->ls_openowner, new_stp->ls_op);
2027 			    else
2028 				error = NFSERR_BADSTATEID;
2029 			}
2030 			if (!error)
2031 			    nfsrv_getowner(&stp->ls_open, new_stp, &lckstp);
2032 			if (lckstp) {
2033 			    /*
2034 			     * For NFSv4.1 and NFSv4.2 allow an
2035 			     * open_to_lock_owner when the lock_owner already
2036 			     * exists.  Just clear NFSLCK_OPENTOLOCK so that
2037 			     * a new lock_owner will not be created.
2038 			     * RFC7530 states that the error for NFSv4.0
2039 			     * is NFS4ERR_BAD_SEQID.
2040 			     */
2041 			    if ((nd->nd_flag & ND_NFSV41) != 0)
2042 				new_stp->ls_flags &= ~NFSLCK_OPENTOLOCK;
2043 			    else
2044 				error = NFSERR_BADSEQID;
2045 			} else
2046 			    lckstp = new_stp;
2047 		    } else if (new_stp->ls_flags&(NFSLCK_LOCK|NFSLCK_UNLOCK)) {
2048 			/*
2049 			 * If haslock set, ditto above.
2050 			 */
2051 			if (!haslock) {
2052 			    if (stp->ls_flags & NFSLCK_OPEN)
2053 				error = NFSERR_BADSTATEID;
2054 			    else
2055 				error = nfsrv_checkseqid(nd, new_stp->ls_seq,
2056 				    stp, new_stp->ls_op);
2057 			}
2058 			lckstp = stp;
2059 		    } else {
2060 			lckstp = stp;
2061 		    }
2062 	      }
2063 	      /*
2064 	       * If the seqid part of the stateid isn't the same, return
2065 	       * NFSERR_OLDSTATEID for cases other than I/O Ops.
2066 	       * For I/O Ops, only return NFSERR_OLDSTATEID if
2067 	       * nfsrv_returnoldstateid is set. (The consensus on the email
2068 	       * list was that most clients would prefer to not receive
2069 	       * NFSERR_OLDSTATEID for I/O Ops, but the RFC suggests that that
2070 	       * is what will happen, so I use the nfsrv_returnoldstateid to
2071 	       * allow for either server configuration.)
2072 	       */
2073 	      if (!error && stp->ls_stateid.seqid!=new_stp->ls_stateid.seqid &&
2074 		  (((nd->nd_flag & ND_NFSV41) == 0 &&
2075 		   (!(new_stp->ls_flags & NFSLCK_CHECK) ||
2076 		    nfsrv_returnoldstateid)) ||
2077 		   ((nd->nd_flag & ND_NFSV41) != 0 &&
2078 		    new_stp->ls_stateid.seqid != 0)))
2079 		    error = NFSERR_OLDSTATEID;
2080 	    }
2081 	}
2082 
2083 	/*
2084 	 * Now we can check for grace.
2085 	 */
2086 	if (!error)
2087 		error = nfsrv_checkgrace(nd, clp, new_stp->ls_flags);
2088 	if ((new_stp->ls_flags & NFSLCK_RECLAIM) && !error &&
2089 		nfsrv_checkstable(clp))
2090 		error = NFSERR_NOGRACE;
2091 	/*
2092 	 * If we successfully Reclaimed state, note that.
2093 	 */
2094 	if ((new_stp->ls_flags & NFSLCK_RECLAIM) && !error)
2095 		nfsrv_markstable(clp);
2096 
2097 	/*
2098 	 * At this point, either error == NFSERR_BADSTATEID or the
2099 	 * seqid# has been updated, so we can return any error.
2100 	 * If error == 0, there may be an error in:
2101 	 *    nd_repstat - Set by the calling function.
2102 	 *    reterr - Set above, if getting the nfslockfile structure
2103 	 *       or acquiring the local lock failed.
2104 	 *    (If both of these are set, nd_repstat should probably be
2105 	 *     returned, since that error was detected before this
2106 	 *     function call.)
2107 	 */
2108 	if (error != 0 || nd->nd_repstat != 0 || reterr != 0) {
2109 		if (error == 0) {
2110 			if (nd->nd_repstat != 0)
2111 				error = nd->nd_repstat;
2112 			else
2113 				error = reterr;
2114 		}
2115 		if (filestruct_locked != 0) {
2116 			/* Roll back local locks. */
2117 			NFSUNLOCKSTATE();
2118 			if (vnode_unlocked == 0) {
2119 				ASSERT_VOP_ELOCKED(vp, "nfsrv_lockctrl2");
2120 				vnode_unlocked = 1;
2121 				NFSVOPUNLOCK(vp);
2122 			}
2123 			nfsrv_locallock_rollback(vp, lfp, p);
2124 			NFSLOCKSTATE();
2125 			nfsrv_unlocklf(lfp);
2126 		}
2127 		NFSUNLOCKSTATE();
2128 		goto out;
2129 	}
2130 
2131 	/*
2132 	 * Check the nfsrv_getlockfile return.
2133 	 * Returned -1 if no structure found.
2134 	 */
2135 	if (getlckret == -1) {
2136 		error = NFSERR_EXPIRED;
2137 		/*
2138 		 * Called from lockt, so no lock is OK.
2139 		 */
2140 		if (new_stp->ls_flags & NFSLCK_TEST) {
2141 			error = 0;
2142 		} else if (new_stp->ls_flags &
2143 		    (NFSLCK_CHECK | NFSLCK_SETATTR)) {
2144 			/*
2145 			 * Called to check for a lock, OK if the stateid is all
2146 			 * 1s or all 0s, but there should be an nfsstate
2147 			 * otherwise.
2148 			 * (ie. If there is no open, I'll assume no share
2149 			 *  deny bits.)
2150 			 */
2151 			if (specialid)
2152 				error = 0;
2153 			else
2154 				error = NFSERR_BADSTATEID;
2155 		}
2156 		NFSUNLOCKSTATE();
2157 		goto out;
2158 	}
2159 
2160 	/*
2161 	 * For NFSLCK_CHECK and NFSLCK_LOCK, test for a share conflict.
2162 	 * For NFSLCK_CHECK, allow a read if write access is granted,
2163 	 * but check for a deny. For NFSLCK_LOCK, require correct access,
2164 	 * which implies a conflicting deny can't exist.
2165 	 */
2166 	if (new_stp->ls_flags & (NFSLCK_CHECK | NFSLCK_LOCK)) {
2167 	    /*
2168 	     * Four kinds of state id:
2169 	     * - specialid (all 0s or all 1s), only for NFSLCK_CHECK
2170 	     * - stateid for an open
2171 	     * - stateid for a delegation
2172 	     * - stateid for a lock owner
2173 	     */
2174 	    if (!specialid) {
2175 		if (stp->ls_flags & (NFSLCK_DELEGREAD | NFSLCK_DELEGWRITE)) {
2176 		    delegation = 1;
2177 		    mystp = stp;
2178 		    nfsrv_delaydelegtimeout(stp);
2179 	        } else if (stp->ls_flags & NFSLCK_OPEN) {
2180 		    mystp = stp;
2181 		} else {
2182 		    mystp = stp->ls_openstp;
2183 		}
2184 		/*
2185 		 * If locking or checking, require correct access
2186 		 * bit set.
2187 		 */
2188 		if (((new_stp->ls_flags & NFSLCK_LOCK) &&
2189 		     !((new_lop->lo_flags >> NFSLCK_LOCKSHIFT) &
2190 		       mystp->ls_flags & NFSLCK_ACCESSBITS)) ||
2191 		    ((new_stp->ls_flags & (NFSLCK_CHECK|NFSLCK_READACCESS)) ==
2192 		      (NFSLCK_CHECK | NFSLCK_READACCESS) &&
2193 		     !(mystp->ls_flags & NFSLCK_READACCESS) &&
2194 		     nfsrv_allowreadforwriteopen == 0) ||
2195 		    ((new_stp->ls_flags & (NFSLCK_CHECK|NFSLCK_WRITEACCESS)) ==
2196 		      (NFSLCK_CHECK | NFSLCK_WRITEACCESS) &&
2197 		     !(mystp->ls_flags & NFSLCK_WRITEACCESS))) {
2198 			if (filestruct_locked != 0) {
2199 				/* Roll back local locks. */
2200 				NFSUNLOCKSTATE();
2201 				if (vnode_unlocked == 0) {
2202 					ASSERT_VOP_ELOCKED(vp,
2203 					    "nfsrv_lockctrl3");
2204 					vnode_unlocked = 1;
2205 					NFSVOPUNLOCK(vp);
2206 				}
2207 				nfsrv_locallock_rollback(vp, lfp, p);
2208 				NFSLOCKSTATE();
2209 				nfsrv_unlocklf(lfp);
2210 			}
2211 			NFSUNLOCKSTATE();
2212 			error = NFSERR_OPENMODE;
2213 			goto out;
2214 		}
2215 	    } else
2216 		mystp = NULL;
2217 	    if ((new_stp->ls_flags & NFSLCK_CHECK) && !delegation) {
2218 		/*
2219 		 * Check for a conflicting deny bit.
2220 		 */
2221 		LIST_FOREACH(tstp, &lfp->lf_open, ls_file) {
2222 		    if (tstp != mystp) {
2223 			bits = tstp->ls_flags;
2224 			bits >>= NFSLCK_SHIFT;
2225 			if (new_stp->ls_flags & bits & NFSLCK_ACCESSBITS) {
2226 			    KASSERT(vnode_unlocked == 0,
2227 				("nfsrv_lockctrl: vnode unlocked1"));
2228 			    ret = nfsrv_clientconflict(tstp->ls_clp, &haslock,
2229 				vp, p);
2230 			    if (ret == 1) {
2231 				/*
2232 				* nfsrv_clientconflict unlocks state
2233 				 * when it returns non-zero.
2234 				 */
2235 				lckstp = NULL;
2236 				goto tryagain;
2237 			    }
2238 			    if (ret == 0)
2239 				NFSUNLOCKSTATE();
2240 			    if (ret == 2)
2241 				error = NFSERR_PERM;
2242 			    else
2243 				error = NFSERR_OPENMODE;
2244 			    goto out;
2245 			}
2246 		    }
2247 		}
2248 
2249 		/* We're outta here */
2250 		NFSUNLOCKSTATE();
2251 		goto out;
2252 	    }
2253 	}
2254 
2255 	/*
2256 	 * For setattr, just get rid of all the Delegations for other clients.
2257 	 */
2258 	if (new_stp->ls_flags & NFSLCK_SETATTR) {
2259 		KASSERT(vnode_unlocked == 0,
2260 		    ("nfsrv_lockctrl: vnode unlocked2"));
2261 		ret = nfsrv_cleandeleg(vp, lfp, clp, &haslock, p);
2262 		if (ret) {
2263 			/*
2264 			 * nfsrv_cleandeleg() unlocks state when it
2265 			 * returns non-zero.
2266 			 */
2267 			if (ret == -1) {
2268 				lckstp = NULL;
2269 				goto tryagain;
2270 			}
2271 			error = ret;
2272 			goto out;
2273 		}
2274 		if (!(new_stp->ls_flags & NFSLCK_CHECK) ||
2275 		    (LIST_EMPTY(&lfp->lf_open) && LIST_EMPTY(&lfp->lf_lock) &&
2276 		     LIST_EMPTY(&lfp->lf_deleg))) {
2277 			NFSUNLOCKSTATE();
2278 			goto out;
2279 		}
2280 	}
2281 
2282 	/*
2283 	 * Check for a conflicting delegation. If one is found, call
2284 	 * nfsrv_delegconflict() to handle it. If the v4root lock hasn't
2285 	 * been set yet, it will get the lock. Otherwise, it will recall
2286 	 * the delegation. Then, we try try again...
2287 	 * I currently believe the conflict algorithm to be:
2288 	 * For Lock Ops (Lock/LockT/LockU)
2289 	 * - there is a conflict iff a different client has a write delegation
2290 	 * For Reading (Read Op)
2291 	 * - there is a conflict iff a different client has a write delegation
2292 	 *   (the specialids are always a different client)
2293 	 * For Writing (Write/Setattr of size)
2294 	 * - there is a conflict if a different client has any delegation
2295 	 * - there is a conflict if the same client has a read delegation
2296 	 *   (I don't understand why this isn't allowed, but that seems to be
2297 	 *    the current consensus?)
2298 	 */
2299 	tstp = LIST_FIRST(&lfp->lf_deleg);
2300 	while (tstp != LIST_END(&lfp->lf_deleg)) {
2301 	    nstp = LIST_NEXT(tstp, ls_file);
2302 	    if ((((new_stp->ls_flags&(NFSLCK_LOCK|NFSLCK_UNLOCK|NFSLCK_TEST))||
2303 		 ((new_stp->ls_flags & NFSLCK_CHECK) &&
2304 		  (new_lop->lo_flags & NFSLCK_READ))) &&
2305 		  clp != tstp->ls_clp &&
2306 		 (tstp->ls_flags & NFSLCK_DELEGWRITE)) ||
2307 		 ((new_stp->ls_flags & NFSLCK_CHECK) &&
2308 		   (new_lop->lo_flags & NFSLCK_WRITE) &&
2309 		  (clp != tstp->ls_clp ||
2310 		   (tstp->ls_flags & NFSLCK_DELEGREAD)))) {
2311 		ret = 0;
2312 		if (filestruct_locked != 0) {
2313 			/* Roll back local locks. */
2314 			NFSUNLOCKSTATE();
2315 			if (vnode_unlocked == 0) {
2316 				ASSERT_VOP_ELOCKED(vp, "nfsrv_lockctrl4");
2317 				NFSVOPUNLOCK(vp);
2318 			}
2319 			nfsrv_locallock_rollback(vp, lfp, p);
2320 			NFSLOCKSTATE();
2321 			nfsrv_unlocklf(lfp);
2322 			NFSUNLOCKSTATE();
2323 			NFSVOPLOCK(vp, LK_EXCLUSIVE | LK_RETRY);
2324 			vnode_unlocked = 0;
2325 			if (VN_IS_DOOMED(vp))
2326 				ret = NFSERR_SERVERFAULT;
2327 			NFSLOCKSTATE();
2328 		}
2329 		if (ret == 0)
2330 			ret = nfsrv_delegconflict(tstp, &haslock, p, vp);
2331 		if (ret) {
2332 		    /*
2333 		     * nfsrv_delegconflict unlocks state when it
2334 		     * returns non-zero, which it always does.
2335 		     */
2336 		    if (other_lop) {
2337 			free(other_lop, M_NFSDLOCK);
2338 			other_lop = NULL;
2339 		    }
2340 		    if (ret == -1) {
2341 			lckstp = NULL;
2342 			goto tryagain;
2343 		    }
2344 		    error = ret;
2345 		    goto out;
2346 		}
2347 		/* Never gets here. */
2348 	    }
2349 	    tstp = nstp;
2350 	}
2351 
2352 	/*
2353 	 * Handle the unlock case by calling nfsrv_updatelock().
2354 	 * (Should I have done some access checking above for unlock? For now,
2355 	 *  just let it happen.)
2356 	 */
2357 	if (new_stp->ls_flags & NFSLCK_UNLOCK) {
2358 		first = new_lop->lo_first;
2359 		end = new_lop->lo_end;
2360 		nfsrv_updatelock(stp, new_lopp, &other_lop, lfp);
2361 		stateidp->seqid = ++(stp->ls_stateid.seqid);
2362 		if ((nd->nd_flag & ND_NFSV41) != 0 && stateidp->seqid == 0)
2363 			stateidp->seqid = stp->ls_stateid.seqid = 1;
2364 		stateidp->other[0] = stp->ls_stateid.other[0];
2365 		stateidp->other[1] = stp->ls_stateid.other[1];
2366 		stateidp->other[2] = stp->ls_stateid.other[2];
2367 		if (filestruct_locked != 0) {
2368 			NFSUNLOCKSTATE();
2369 			if (vnode_unlocked == 0) {
2370 				ASSERT_VOP_ELOCKED(vp, "nfsrv_lockctrl5");
2371 				vnode_unlocked = 1;
2372 				NFSVOPUNLOCK(vp);
2373 			}
2374 			/* Update the local locks. */
2375 			nfsrv_localunlock(vp, lfp, first, end, p);
2376 			NFSLOCKSTATE();
2377 			nfsrv_unlocklf(lfp);
2378 		}
2379 		NFSUNLOCKSTATE();
2380 		goto out;
2381 	}
2382 
2383 	/*
2384 	 * Search for a conflicting lock. A lock conflicts if:
2385 	 * - the lock range overlaps and
2386 	 * - at least one lock is a write lock and
2387 	 * - it is not owned by the same lock owner
2388 	 */
2389 	if (!delegation) {
2390 	  LIST_FOREACH(lop, &lfp->lf_lock, lo_lckfile) {
2391 	    if (new_lop->lo_end > lop->lo_first &&
2392 		new_lop->lo_first < lop->lo_end &&
2393 		(new_lop->lo_flags == NFSLCK_WRITE ||
2394 		 lop->lo_flags == NFSLCK_WRITE) &&
2395 		lckstp != lop->lo_stp &&
2396 		(clp != lop->lo_stp->ls_clp ||
2397 		 lckstp->ls_ownerlen != lop->lo_stp->ls_ownerlen ||
2398 		 NFSBCMP(lckstp->ls_owner, lop->lo_stp->ls_owner,
2399 		    lckstp->ls_ownerlen))) {
2400 		if (other_lop) {
2401 		    free(other_lop, M_NFSDLOCK);
2402 		    other_lop = NULL;
2403 		}
2404 		if (vnode_unlocked != 0)
2405 		    ret = nfsrv_clientconflict(lop->lo_stp->ls_clp, &haslock,
2406 			NULL, p);
2407 		else
2408 		    ret = nfsrv_clientconflict(lop->lo_stp->ls_clp, &haslock,
2409 			vp, p);
2410 		if (ret == 1) {
2411 		    if (filestruct_locked != 0) {
2412 			if (vnode_unlocked == 0) {
2413 				ASSERT_VOP_ELOCKED(vp, "nfsrv_lockctrl6");
2414 				NFSVOPUNLOCK(vp);
2415 			}
2416 			/* Roll back local locks. */
2417 			nfsrv_locallock_rollback(vp, lfp, p);
2418 			NFSLOCKSTATE();
2419 			nfsrv_unlocklf(lfp);
2420 			NFSUNLOCKSTATE();
2421 			NFSVOPLOCK(vp, LK_EXCLUSIVE | LK_RETRY);
2422 			vnode_unlocked = 0;
2423 			if (VN_IS_DOOMED(vp)) {
2424 				error = NFSERR_SERVERFAULT;
2425 				goto out;
2426 			}
2427 		    }
2428 		    /*
2429 		     * nfsrv_clientconflict() unlocks state when it
2430 		     * returns non-zero.
2431 		     */
2432 		    lckstp = NULL;
2433 		    goto tryagain;
2434 		}
2435 		/*
2436 		 * Found a conflicting lock, so record the conflict and
2437 		 * return the error.
2438 		 */
2439 		if (cfp != NULL && ret == 0) {
2440 		    cfp->cl_clientid.lval[0]=lop->lo_stp->ls_stateid.other[0];
2441 		    cfp->cl_clientid.lval[1]=lop->lo_stp->ls_stateid.other[1];
2442 		    cfp->cl_first = lop->lo_first;
2443 		    cfp->cl_end = lop->lo_end;
2444 		    cfp->cl_flags = lop->lo_flags;
2445 		    cfp->cl_ownerlen = lop->lo_stp->ls_ownerlen;
2446 		    NFSBCOPY(lop->lo_stp->ls_owner, cfp->cl_owner,
2447 			cfp->cl_ownerlen);
2448 		}
2449 		if (ret == 2)
2450 		    error = NFSERR_PERM;
2451 		else if (new_stp->ls_flags & NFSLCK_RECLAIM)
2452 		    error = NFSERR_RECLAIMCONFLICT;
2453 		else if (new_stp->ls_flags & NFSLCK_CHECK)
2454 		    error = NFSERR_LOCKED;
2455 		else
2456 		    error = NFSERR_DENIED;
2457 		if (filestruct_locked != 0 && ret == 0) {
2458 			/* Roll back local locks. */
2459 			NFSUNLOCKSTATE();
2460 			if (vnode_unlocked == 0) {
2461 				ASSERT_VOP_ELOCKED(vp, "nfsrv_lockctrl7");
2462 				vnode_unlocked = 1;
2463 				NFSVOPUNLOCK(vp);
2464 			}
2465 			nfsrv_locallock_rollback(vp, lfp, p);
2466 			NFSLOCKSTATE();
2467 			nfsrv_unlocklf(lfp);
2468 		}
2469 		if (ret == 0)
2470 			NFSUNLOCKSTATE();
2471 		goto out;
2472 	    }
2473 	  }
2474 	}
2475 
2476 	/*
2477 	 * We only get here if there was no lock that conflicted.
2478 	 */
2479 	if (new_stp->ls_flags & (NFSLCK_TEST | NFSLCK_CHECK)) {
2480 		NFSUNLOCKSTATE();
2481 		goto out;
2482 	}
2483 
2484 	/*
2485 	 * We only get here when we are creating or modifying a lock.
2486 	 * There are two variants:
2487 	 * - exist_lock_owner where lock_owner exists
2488 	 * - open_to_lock_owner with new lock_owner
2489 	 */
2490 	first = new_lop->lo_first;
2491 	end = new_lop->lo_end;
2492 	lock_flags = new_lop->lo_flags;
2493 	if (!(new_stp->ls_flags & NFSLCK_OPENTOLOCK)) {
2494 		nfsrv_updatelock(lckstp, new_lopp, &other_lop, lfp);
2495 		stateidp->seqid = ++(lckstp->ls_stateid.seqid);
2496 		if ((nd->nd_flag & ND_NFSV41) != 0 && stateidp->seqid == 0)
2497 			stateidp->seqid = lckstp->ls_stateid.seqid = 1;
2498 		stateidp->other[0] = lckstp->ls_stateid.other[0];
2499 		stateidp->other[1] = lckstp->ls_stateid.other[1];
2500 		stateidp->other[2] = lckstp->ls_stateid.other[2];
2501 	} else {
2502 		/*
2503 		 * The new open_to_lock_owner case.
2504 		 * Link the new nfsstate into the lists.
2505 		 */
2506 		new_stp->ls_seq = new_stp->ls_opentolockseq;
2507 		nfsrvd_refcache(new_stp->ls_op);
2508 		stateidp->seqid = new_stp->ls_stateid.seqid = 1;
2509 		stateidp->other[0] = new_stp->ls_stateid.other[0] =
2510 		    clp->lc_clientid.lval[0];
2511 		stateidp->other[1] = new_stp->ls_stateid.other[1] =
2512 		    clp->lc_clientid.lval[1];
2513 		stateidp->other[2] = new_stp->ls_stateid.other[2] =
2514 		    nfsrv_nextstateindex(clp);
2515 		new_stp->ls_clp = clp;
2516 		LIST_INIT(&new_stp->ls_lock);
2517 		new_stp->ls_openstp = stp;
2518 		new_stp->ls_lfp = lfp;
2519 		nfsrv_insertlock(new_lop, (struct nfslock *)new_stp, new_stp,
2520 		    lfp);
2521 		LIST_INSERT_HEAD(NFSSTATEHASH(clp, new_stp->ls_stateid),
2522 		    new_stp, ls_hash);
2523 		LIST_INSERT_HEAD(&stp->ls_open, new_stp, ls_list);
2524 		*new_lopp = NULL;
2525 		*new_stpp = NULL;
2526 		VNET(nfsstatsv1_p)->srvlockowners++;
2527 		nfsrv_openpluslock++;
2528 	}
2529 	if (filestruct_locked != 0) {
2530 		NFSUNLOCKSTATE();
2531 		nfsrv_locallock_commit(lfp, lock_flags, first, end);
2532 		NFSLOCKSTATE();
2533 		nfsrv_unlocklf(lfp);
2534 	}
2535 	NFSUNLOCKSTATE();
2536 
2537 out:
2538 	if (haslock) {
2539 		NFSLOCKV4ROOTMUTEX();
2540 		nfsv4_unlock(&nfsv4rootfs_lock, 1);
2541 		NFSUNLOCKV4ROOTMUTEX();
2542 	}
2543 	if (vnode_unlocked != 0) {
2544 		NFSVOPLOCK(vp, LK_EXCLUSIVE | LK_RETRY);
2545 		if (error == 0 && VN_IS_DOOMED(vp))
2546 			error = NFSERR_SERVERFAULT;
2547 	}
2548 	if (other_lop)
2549 		free(other_lop, M_NFSDLOCK);
2550 	NFSEXITCODE2(error, nd);
2551 	return (error);
2552 }
2553 
2554 /*
2555  * Check for state errors for Open.
2556  * repstat is passed back out as an error if more critical errors
2557  * are not detected.
2558  */
2559 int
nfsrv_opencheck(nfsquad_t clientid,nfsv4stateid_t * stateidp,struct nfsstate * new_stp,vnode_t vp,struct nfsrv_descript * nd,NFSPROC_T * p,int repstat)2560 nfsrv_opencheck(nfsquad_t clientid, nfsv4stateid_t *stateidp,
2561     struct nfsstate *new_stp, vnode_t vp, struct nfsrv_descript *nd,
2562     NFSPROC_T *p, int repstat)
2563 {
2564 	struct nfsstate *stp, *nstp;
2565 	struct nfsclient *clp;
2566 	struct nfsstate *ownerstp;
2567 	struct nfslockfile *lfp, *new_lfp;
2568 	int error = 0, haslock = 0, ret, readonly = 0, getfhret = 0;
2569 
2570 	if ((new_stp->ls_flags & NFSLCK_SHAREBITS) == NFSLCK_READACCESS)
2571 		readonly = 1;
2572 	/*
2573 	 * Check for restart conditions (client and server).
2574 	 */
2575 	error = nfsrv_checkrestart(clientid, new_stp->ls_flags,
2576 		&new_stp->ls_stateid, 0);
2577 	if (error)
2578 		goto out;
2579 
2580 	/*
2581 	 * Check for state resource limit exceeded.
2582 	 * Technically this should be SMP protected, but the worst
2583 	 * case error is "out by one or two" on the count when it
2584 	 * returns NFSERR_RESOURCE and the limit is just a rather
2585 	 * arbitrary high water mark, so no harm is done.
2586 	 */
2587 	if (nfsrv_openpluslock > nfsrv_v4statelimit) {
2588 		error = NFSERR_RESOURCE;
2589 		goto out;
2590 	}
2591 
2592 tryagain:
2593 	new_lfp = malloc(sizeof (struct nfslockfile),
2594 	    M_NFSDLOCKFILE, M_WAITOK);
2595 	if (vp)
2596 		getfhret = nfsrv_getlockfh(vp, new_stp->ls_flags, new_lfp,
2597 		    NULL, p);
2598 	NFSLOCKSTATE();
2599 	/*
2600 	 * Get the nfsclient structure.
2601 	 */
2602 	error = nfsrv_getclient(clientid, CLOPS_RENEW, &clp, NULL,
2603 	    (nfsquad_t)((u_quad_t)0), 0, nd, p);
2604 
2605 	/*
2606 	 * Look up the open owner. See if it needs confirmation and
2607 	 * check the seq#, as required.
2608 	 */
2609 	if (!error)
2610 		nfsrv_getowner(&clp->lc_open, new_stp, &ownerstp);
2611 
2612 	if (!error && ownerstp) {
2613 		error = nfsrv_checkseqid(nd, new_stp->ls_seq, ownerstp,
2614 		    new_stp->ls_op);
2615 		/*
2616 		 * If the OpenOwner hasn't been confirmed, assume the
2617 		 * old one was a replay and this one is ok.
2618 		 * See: RFC3530 Sec. 14.2.18.
2619 		 */
2620 		if (error == NFSERR_BADSEQID &&
2621 		    (ownerstp->ls_flags & NFSLCK_NEEDSCONFIRM))
2622 			error = 0;
2623 	}
2624 
2625 	/*
2626 	 * Check for grace.
2627 	 */
2628 	if (!error)
2629 		error = nfsrv_checkgrace(nd, clp, new_stp->ls_flags);
2630 	if ((new_stp->ls_flags & NFSLCK_RECLAIM) && !error &&
2631 		nfsrv_checkstable(clp))
2632 		error = NFSERR_NOGRACE;
2633 
2634 	/*
2635 	 * If none of the above errors occurred, let repstat be
2636 	 * returned.
2637 	 */
2638 	if (repstat && !error)
2639 		error = repstat;
2640 	if (error) {
2641 		NFSUNLOCKSTATE();
2642 		if (haslock) {
2643 			NFSLOCKV4ROOTMUTEX();
2644 			nfsv4_unlock(&nfsv4rootfs_lock, 1);
2645 			NFSUNLOCKV4ROOTMUTEX();
2646 		}
2647 		free(new_lfp, M_NFSDLOCKFILE);
2648 		goto out;
2649 	}
2650 
2651 	/*
2652 	 * If vp == NULL, the file doesn't exist yet, so return ok.
2653 	 * (This always happens on the first pass, so haslock must be 0.)
2654 	 */
2655 	if (vp == NULL) {
2656 		NFSUNLOCKSTATE();
2657 		free(new_lfp, M_NFSDLOCKFILE);
2658 		goto out;
2659 	}
2660 
2661 	/*
2662 	 * Get the structure for the underlying file.
2663 	 */
2664 	if (getfhret)
2665 		error = getfhret;
2666 	else
2667 		error = nfsrv_getlockfile(new_stp->ls_flags, &new_lfp, &lfp,
2668 		    NULL, 0);
2669 	if (new_lfp)
2670 		free(new_lfp, M_NFSDLOCKFILE);
2671 	if (error) {
2672 		NFSUNLOCKSTATE();
2673 		if (haslock) {
2674 			NFSLOCKV4ROOTMUTEX();
2675 			nfsv4_unlock(&nfsv4rootfs_lock, 1);
2676 			NFSUNLOCKV4ROOTMUTEX();
2677 		}
2678 		goto out;
2679 	}
2680 
2681 	/*
2682 	 * Search for a conflicting open/share.
2683 	 */
2684 	if (new_stp->ls_flags & NFSLCK_DELEGCUR) {
2685 	    /*
2686 	     * For Delegate_Cur, search for the matching Delegation,
2687 	     * which indicates no conflict.
2688 	     * An old delegation should have been recovered by the
2689 	     * client doing a Claim_DELEGATE_Prev, so I won't let
2690 	     * it match and return NFSERR_EXPIRED. Should I let it
2691 	     * match?
2692 	     */
2693 	    LIST_FOREACH(stp, &lfp->lf_deleg, ls_file) {
2694 		if (!(stp->ls_flags & NFSLCK_OLDDELEG) &&
2695 		    (((nd->nd_flag & ND_NFSV41) != 0 &&
2696 		    stateidp->seqid == 0) ||
2697 		    stateidp->seqid == stp->ls_stateid.seqid) &&
2698 		    !NFSBCMP(stateidp->other, stp->ls_stateid.other,
2699 			  NFSX_STATEIDOTHER))
2700 			break;
2701 	    }
2702 	    if (stp == LIST_END(&lfp->lf_deleg) ||
2703 		((new_stp->ls_flags & NFSLCK_WRITEACCESS) &&
2704 		 (stp->ls_flags & NFSLCK_DELEGREAD))) {
2705 		NFSUNLOCKSTATE();
2706 		if (haslock) {
2707 			NFSLOCKV4ROOTMUTEX();
2708 			nfsv4_unlock(&nfsv4rootfs_lock, 1);
2709 			NFSUNLOCKV4ROOTMUTEX();
2710 		}
2711 		error = NFSERR_EXPIRED;
2712 		goto out;
2713 	    }
2714 	}
2715 
2716 	/*
2717 	 * Check for access/deny bit conflicts. I check for the same
2718 	 * owner as well, in case the client didn't bother.
2719 	 */
2720 	LIST_FOREACH(stp, &lfp->lf_open, ls_file) {
2721 		if (!(new_stp->ls_flags & NFSLCK_DELEGCUR) &&
2722 		    (((new_stp->ls_flags & NFSLCK_ACCESSBITS) &
2723 		      ((stp->ls_flags>>NFSLCK_SHIFT) & NFSLCK_ACCESSBITS))||
2724 		     ((stp->ls_flags & NFSLCK_ACCESSBITS) &
2725 		      ((new_stp->ls_flags>>NFSLCK_SHIFT)&NFSLCK_ACCESSBITS)))){
2726 			ret = nfsrv_clientconflict(stp->ls_clp,&haslock,vp,p);
2727 			if (ret == 1) {
2728 				/*
2729 				 * nfsrv_clientconflict() unlocks
2730 				 * state when it returns non-zero.
2731 				 */
2732 				goto tryagain;
2733 			}
2734 			if (ret == 2)
2735 				error = NFSERR_PERM;
2736 			else if (new_stp->ls_flags & NFSLCK_RECLAIM)
2737 				error = NFSERR_RECLAIMCONFLICT;
2738 			else
2739 				error = NFSERR_SHAREDENIED;
2740 			if (ret == 0)
2741 				NFSUNLOCKSTATE();
2742 			if (haslock) {
2743 				NFSLOCKV4ROOTMUTEX();
2744 				nfsv4_unlock(&nfsv4rootfs_lock, 1);
2745 				NFSUNLOCKV4ROOTMUTEX();
2746 			}
2747 			goto out;
2748 		}
2749 	}
2750 
2751 	/*
2752 	 * Check for a conflicting delegation. If one is found, call
2753 	 * nfsrv_delegconflict() to handle it. If the v4root lock hasn't
2754 	 * been set yet, it will get the lock. Otherwise, it will recall
2755 	 * the delegation. Then, we try try again...
2756 	 * (If NFSLCK_DELEGCUR is set, it has a delegation, so there
2757 	 *  isn't a conflict.)
2758 	 * I currently believe the conflict algorithm to be:
2759 	 * For Open with Read Access and Deny None
2760 	 * - there is a conflict iff a different client has a write delegation
2761 	 * For Open with other Write Access or any Deny except None
2762 	 * - there is a conflict if a different client has any delegation
2763 	 * - there is a conflict if the same client has a read delegation
2764 	 *   (The current consensus is that this last case should be
2765 	 *    considered a conflict since the client with a read delegation
2766 	 *    could have done an Open with ReadAccess and WriteDeny
2767 	 *    locally and then not have checked for the WriteDeny.)
2768 	 *    The exception is a NFSv4.1/4.2 client that has requested
2769 	 *    an atomic upgrade to a write delegation.
2770 	 * Don't check for a Reclaim, since that will be dealt with
2771 	 * by nfsrv_openctrl().
2772 	 */
2773 	if (!(new_stp->ls_flags &
2774 		(NFSLCK_DELEGPREV | NFSLCK_DELEGCUR | NFSLCK_RECLAIM))) {
2775 	    stp = LIST_FIRST(&lfp->lf_deleg);
2776 	    while (stp != LIST_END(&lfp->lf_deleg)) {
2777 		nstp = LIST_NEXT(stp, ls_file);
2778 		if ((readonly && stp->ls_clp != clp &&
2779 		     (stp->ls_flags & NFSLCK_DELEGWRITE) != 0) ||
2780 		    (!readonly && (stp->ls_clp != clp ||
2781 		     ((stp->ls_flags & NFSLCK_DELEGREAD) != 0 &&
2782 		      (new_stp->ls_flags & NFSLCK_WANTWDELEG) == 0)))) {
2783 			ret = nfsrv_delegconflict(stp, &haslock, p, vp);
2784 			if (ret) {
2785 			    /*
2786 			     * nfsrv_delegconflict() unlocks state
2787 			     * when it returns non-zero.
2788 			     */
2789 			    if (ret == -1)
2790 				goto tryagain;
2791 			    error = ret;
2792 			    goto out;
2793 			}
2794 		}
2795 		stp = nstp;
2796 	    }
2797 	}
2798 	NFSUNLOCKSTATE();
2799 	if (haslock) {
2800 		NFSLOCKV4ROOTMUTEX();
2801 		nfsv4_unlock(&nfsv4rootfs_lock, 1);
2802 		NFSUNLOCKV4ROOTMUTEX();
2803 	}
2804 
2805 out:
2806 	NFSEXITCODE2(error, nd);
2807 	return (error);
2808 }
2809 
2810 /*
2811  * Open control function to create/update open state for an open.
2812  */
2813 int
nfsrv_openctrl(struct nfsrv_descript * nd,vnode_t vp,struct nfsstate ** new_stpp,nfsquad_t clientid,nfsv4stateid_t * stateidp,nfsv4stateid_t * delegstateidp,u_int32_t * rflagsp,struct nfsexstuff * exp,NFSPROC_T * p,u_quad_t filerev)2814 nfsrv_openctrl(struct nfsrv_descript *nd, vnode_t vp,
2815     struct nfsstate **new_stpp, nfsquad_t clientid, nfsv4stateid_t *stateidp,
2816     nfsv4stateid_t *delegstateidp, u_int32_t *rflagsp, struct nfsexstuff *exp,
2817     NFSPROC_T *p, u_quad_t filerev)
2818 {
2819 	struct nfsstate *new_stp = *new_stpp;
2820 	struct nfsstate *stp, *nstp;
2821 	struct nfsstate *openstp = NULL, *new_open, *ownerstp, *new_deleg;
2822 	struct nfslockfile *lfp, *new_lfp;
2823 	struct nfsclient *clp;
2824 	int error = 0, haslock = 0, ret, delegate = 1, writedeleg = 1;
2825 	int readonly = 0, cbret = 1, getfhret = 0;
2826 	int gotstate = 0, len = 0;
2827 	u_char *clidp = NULL;
2828 
2829 	if ((new_stp->ls_flags & NFSLCK_SHAREBITS) == NFSLCK_READACCESS)
2830 		readonly = 1;
2831 	/*
2832 	 * Check for restart conditions (client and server).
2833 	 * (Paranoia, should have been detected by nfsrv_opencheck().)
2834 	 * If an error does show up, return NFSERR_EXPIRED, since the
2835 	 * the seqid# has already been incremented.
2836 	 */
2837 	error = nfsrv_checkrestart(clientid, new_stp->ls_flags,
2838 	    &new_stp->ls_stateid, 0);
2839 	if (error) {
2840 		printf("Nfsd: openctrl unexpected restart err=%d\n",
2841 		    error);
2842 		error = NFSERR_EXPIRED;
2843 		goto out;
2844 	}
2845 
2846 	clidp = malloc(NFSV4_OPAQUELIMIT, M_TEMP, M_WAITOK);
2847 tryagain:
2848 	new_lfp = malloc(sizeof (struct nfslockfile),
2849 	    M_NFSDLOCKFILE, M_WAITOK);
2850 	new_open = malloc(sizeof (struct nfsstate),
2851 	    M_NFSDSTATE, M_WAITOK);
2852 	new_deleg = malloc(sizeof (struct nfsstate),
2853 	    M_NFSDSTATE, M_WAITOK);
2854 	getfhret = nfsrv_getlockfh(vp, new_stp->ls_flags, new_lfp,
2855 	    NULL, p);
2856 	NFSLOCKSTATE();
2857 	/*
2858 	 * Get the client structure. Since the linked lists could be changed
2859 	 * by other nfsd processes if this process does a tsleep(), one of
2860 	 * two things must be done.
2861 	 * 1 - don't tsleep()
2862 	 * or
2863 	 * 2 - get the nfsv4_lock() { indicated by haslock == 1 }
2864 	 *     before using the lists, since this lock stops the other
2865 	 *     nfsd. This should only be used for rare cases, since it
2866 	 *     essentially single threads the nfsd.
2867 	 *     At this time, it is only done for cases where the stable
2868 	 *     storage file must be written prior to completion of state
2869 	 *     expiration.
2870 	 */
2871 	error = nfsrv_getclient(clientid, CLOPS_RENEW, &clp, NULL,
2872 	    (nfsquad_t)((u_quad_t)0), 0, nd, p);
2873 	if (!error && (clp->lc_flags & LCL_NEEDSCBNULL) &&
2874 	    clp->lc_program) {
2875 		/*
2876 		 * This happens on the first open for a client
2877 		 * that supports callbacks.
2878 		 */
2879 		NFSUNLOCKSTATE();
2880 		/*
2881 		 * Although nfsrv_docallback() will sleep, clp won't
2882 		 * go away, since they are only removed when the
2883 		 * nfsv4_lock() has blocked the nfsd threads. The
2884 		 * fields in clp can change, but having multiple
2885 		 * threads do this Null callback RPC should be
2886 		 * harmless.
2887 		 */
2888 		cbret = nfsrv_docallback(clp, NFSV4PROC_CBNULL,
2889 		    NULL, 0, NULL, NULL, NULL, 0, p);
2890 		NFSLOCKSTATE();
2891 		clp->lc_flags &= ~LCL_NEEDSCBNULL;
2892 		if (!cbret)
2893 			clp->lc_flags |= LCL_CALLBACKSON;
2894 	}
2895 
2896 	/*
2897 	 * Look up the open owner. See if it needs confirmation and
2898 	 * check the seq#, as required.
2899 	 */
2900 	if (!error)
2901 		nfsrv_getowner(&clp->lc_open, new_stp, &ownerstp);
2902 
2903 	if (error) {
2904 		NFSUNLOCKSTATE();
2905 		printf("Nfsd: openctrl unexpected state err=%d\n",
2906 			error);
2907 		free(new_lfp, M_NFSDLOCKFILE);
2908 		free(new_open, M_NFSDSTATE);
2909 		free(new_deleg, M_NFSDSTATE);
2910 		if (haslock) {
2911 			NFSLOCKV4ROOTMUTEX();
2912 			nfsv4_unlock(&nfsv4rootfs_lock, 1);
2913 			NFSUNLOCKV4ROOTMUTEX();
2914 		}
2915 		error = NFSERR_EXPIRED;
2916 		goto out;
2917 	}
2918 
2919 	if (new_stp->ls_flags & NFSLCK_RECLAIM)
2920 		nfsrv_markstable(clp);
2921 
2922 	/*
2923 	 * Get the structure for the underlying file.
2924 	 */
2925 	if (getfhret)
2926 		error = getfhret;
2927 	else
2928 		error = nfsrv_getlockfile(new_stp->ls_flags, &new_lfp, &lfp,
2929 		    NULL, 0);
2930 	if (new_lfp)
2931 		free(new_lfp, M_NFSDLOCKFILE);
2932 	if (error) {
2933 		NFSUNLOCKSTATE();
2934 		printf("Nfsd openctrl unexpected getlockfile err=%d\n",
2935 		    error);
2936 		free(new_open, M_NFSDSTATE);
2937 		free(new_deleg, M_NFSDSTATE);
2938 		if (haslock) {
2939 			NFSLOCKV4ROOTMUTEX();
2940 			nfsv4_unlock(&nfsv4rootfs_lock, 1);
2941 			NFSUNLOCKV4ROOTMUTEX();
2942 		}
2943 		goto out;
2944 	}
2945 
2946 	/*
2947 	 * Search for a conflicting open/share.
2948 	 */
2949 	if (new_stp->ls_flags & NFSLCK_DELEGCUR) {
2950 	    /*
2951 	     * For Delegate_Cur, search for the matching Delegation,
2952 	     * which indicates no conflict.
2953 	     * An old delegation should have been recovered by the
2954 	     * client doing a Claim_DELEGATE_Prev, so I won't let
2955 	     * it match and return NFSERR_EXPIRED. Should I let it
2956 	     * match?
2957 	     */
2958 	    LIST_FOREACH(stp, &lfp->lf_deleg, ls_file) {
2959 		if (!(stp->ls_flags & NFSLCK_OLDDELEG) &&
2960 		    (((nd->nd_flag & ND_NFSV41) != 0 &&
2961 		    stateidp->seqid == 0) ||
2962 		    stateidp->seqid == stp->ls_stateid.seqid) &&
2963 		    !NFSBCMP(stateidp->other, stp->ls_stateid.other,
2964 			NFSX_STATEIDOTHER))
2965 			break;
2966 	    }
2967 	    if (stp == LIST_END(&lfp->lf_deleg) ||
2968 		((new_stp->ls_flags & NFSLCK_WRITEACCESS) &&
2969 		 (stp->ls_flags & NFSLCK_DELEGREAD))) {
2970 		NFSUNLOCKSTATE();
2971 		printf("Nfsd openctrl unexpected expiry\n");
2972 		free(new_open, M_NFSDSTATE);
2973 		free(new_deleg, M_NFSDSTATE);
2974 		if (haslock) {
2975 			NFSLOCKV4ROOTMUTEX();
2976 			nfsv4_unlock(&nfsv4rootfs_lock, 1);
2977 			NFSUNLOCKV4ROOTMUTEX();
2978 		}
2979 		error = NFSERR_EXPIRED;
2980 		goto out;
2981 	    }
2982 
2983 	    /*
2984 	     * Don't issue a Delegation, since one already exists and
2985 	     * delay delegation timeout, as required.
2986 	     */
2987 	    delegate = 0;
2988 	    nfsrv_delaydelegtimeout(stp);
2989 	}
2990 
2991 	/*
2992 	 * Check for access/deny bit conflicts. I also check for the
2993 	 * same owner, since the client might not have bothered to check.
2994 	 * Also, note an open for the same file and owner, if found,
2995 	 * which is all we do here for Delegate_Cur, since conflict
2996 	 * checking is already done.
2997 	 */
2998 	LIST_FOREACH(stp, &lfp->lf_open, ls_file) {
2999 		if (ownerstp && stp->ls_openowner == ownerstp)
3000 			openstp = stp;
3001 		if (!(new_stp->ls_flags & NFSLCK_DELEGCUR)) {
3002 		    /*
3003 		     * If another client has the file open, the only
3004 		     * delegation that can be issued is a Read delegation
3005 		     * and only if it is a Read open with Deny none.
3006 		     */
3007 		    if (clp != stp->ls_clp) {
3008 			if ((stp->ls_flags & NFSLCK_SHAREBITS) ==
3009 			    NFSLCK_READACCESS)
3010 			    writedeleg = 0;
3011 			else
3012 			    delegate = 0;
3013 		    }
3014 		    if(((new_stp->ls_flags & NFSLCK_ACCESSBITS) &
3015 		        ((stp->ls_flags>>NFSLCK_SHIFT) & NFSLCK_ACCESSBITS))||
3016 		       ((stp->ls_flags & NFSLCK_ACCESSBITS) &
3017 		        ((new_stp->ls_flags>>NFSLCK_SHIFT)&NFSLCK_ACCESSBITS))){
3018 			ret = nfsrv_clientconflict(stp->ls_clp,&haslock,vp,p);
3019 			if (ret == 1) {
3020 				/*
3021 				 * nfsrv_clientconflict() unlocks state
3022 				 * when it returns non-zero.
3023 				 */
3024 				free(new_open, M_NFSDSTATE);
3025 				free(new_deleg, M_NFSDSTATE);
3026 				openstp = NULL;
3027 				goto tryagain;
3028 			}
3029 			if (ret == 2)
3030 				error = NFSERR_PERM;
3031 			else if (new_stp->ls_flags & NFSLCK_RECLAIM)
3032 				error = NFSERR_RECLAIMCONFLICT;
3033 			else
3034 				error = NFSERR_SHAREDENIED;
3035 			if (ret == 0)
3036 				NFSUNLOCKSTATE();
3037 			if (haslock) {
3038 				NFSLOCKV4ROOTMUTEX();
3039 				nfsv4_unlock(&nfsv4rootfs_lock, 1);
3040 				NFSUNLOCKV4ROOTMUTEX();
3041 			}
3042 			free(new_open, M_NFSDSTATE);
3043 			free(new_deleg, M_NFSDSTATE);
3044 			printf("nfsd openctrl unexpected client cnfl\n");
3045 			goto out;
3046 		    }
3047 		}
3048 	}
3049 
3050 	/*
3051 	 * Check for a conflicting delegation. If one is found, call
3052 	 * nfsrv_delegconflict() to handle it. If the v4root lock hasn't
3053 	 * been set yet, it will get the lock. Otherwise, it will recall
3054 	 * the delegation. Then, we try try again...
3055 	 * (If NFSLCK_DELEGCUR is set, it has a delegation, so there
3056 	 *  isn't a conflict.)
3057 	 * I currently believe the conflict algorithm to be:
3058 	 * For Open with Read Access and Deny None
3059 	 * - there is a conflict iff a different client has a write delegation
3060 	 * For Open with other Write Access or any Deny except None
3061 	 * - there is a conflict if a different client has any delegation
3062 	 * - there is a conflict if the same client has a read delegation
3063 	 *   (The current consensus is that this last case should be
3064 	 *    considered a conflict since the client with a read delegation
3065 	 *    could have done an Open with ReadAccess and WriteDeny
3066 	 *    locally and then not have checked for the WriteDeny.)
3067 	 *    The exception is a NFSv4.1/4.2 client that has requested
3068 	 *    an atomic upgrade to a write delegation.
3069 	 */
3070 	if (!(new_stp->ls_flags & (NFSLCK_DELEGPREV | NFSLCK_DELEGCUR))) {
3071 	    stp = LIST_FIRST(&lfp->lf_deleg);
3072 	    while (stp != LIST_END(&lfp->lf_deleg)) {
3073 		nstp = LIST_NEXT(stp, ls_file);
3074 		if (stp->ls_clp != clp && (stp->ls_flags & NFSLCK_DELEGREAD))
3075 			writedeleg = 0;
3076 		else if (stp->ls_clp != clp ||
3077 		    (stp->ls_flags & NFSLCK_DELEGWRITE) != 0 ||
3078 		    (new_stp->ls_flags & NFSLCK_WANTWDELEG) == 0)
3079 			delegate = 0;
3080 		if ((readonly && stp->ls_clp != clp &&
3081 		     (stp->ls_flags & NFSLCK_DELEGWRITE) != 0) ||
3082 		    (!readonly && (stp->ls_clp != clp ||
3083 		     ((stp->ls_flags & NFSLCK_DELEGREAD) != 0 &&
3084 		      (new_stp->ls_flags & NFSLCK_WANTWDELEG) == 0)))) {
3085 		    if (new_stp->ls_flags & NFSLCK_RECLAIM) {
3086 			delegate = 2;
3087 		    } else {
3088 			ret = nfsrv_delegconflict(stp, &haslock, p, vp);
3089 			if (ret) {
3090 			    /*
3091 			     * nfsrv_delegconflict() unlocks state
3092 			     * when it returns non-zero.
3093 			     */
3094 			    printf("Nfsd openctrl unexpected deleg cnfl\n");
3095 			    free(new_open, M_NFSDSTATE);
3096 			    free(new_deleg, M_NFSDSTATE);
3097 			    if (ret == -1) {
3098 				openstp = NULL;
3099 				goto tryagain;
3100 			    }
3101 			    error = ret;
3102 			    goto out;
3103 			}
3104 		    }
3105 		}
3106 		stp = nstp;
3107 	    }
3108 	}
3109 
3110 	/*
3111 	 * We only get here if there was no open that conflicted.
3112 	 * If an open for the owner exists, or in the access/deny bits.
3113 	 * Otherwise it is a new open. If the open_owner hasn't been
3114 	 * confirmed, replace the open with the new one needing confirmation,
3115 	 * otherwise add the open.
3116 	 */
3117 	if (new_stp->ls_flags & NFSLCK_DELEGPREV) {
3118 	    /*
3119 	     * Handle NFSLCK_DELEGPREV by searching the old delegations for
3120 	     * a match. If found, just move the old delegation to the current
3121 	     * delegation list and issue open. If not found, return
3122 	     * NFSERR_EXPIRED.
3123 	     */
3124 	    LIST_FOREACH(stp, &clp->lc_olddeleg, ls_list) {
3125 		if (stp->ls_lfp == lfp) {
3126 		    /* Found it */
3127 		    if (stp->ls_clp != clp)
3128 			panic("olddeleg clp");
3129 		    LIST_REMOVE(stp, ls_list);
3130 		    LIST_REMOVE(stp, ls_hash);
3131 		    stp->ls_flags &= ~NFSLCK_OLDDELEG;
3132 		    stp->ls_stateid.seqid = delegstateidp->seqid = 1;
3133 		    stp->ls_stateid.other[0] = delegstateidp->other[0] =
3134 			clp->lc_clientid.lval[0];
3135 		    stp->ls_stateid.other[1] = delegstateidp->other[1] =
3136 			clp->lc_clientid.lval[1];
3137 		    stp->ls_stateid.other[2] = delegstateidp->other[2] =
3138 			nfsrv_nextstateindex(clp);
3139 		    stp->ls_compref = nd->nd_compref;
3140 		    LIST_INSERT_HEAD(&clp->lc_deleg, stp, ls_list);
3141 		    LIST_INSERT_HEAD(NFSSTATEHASH(clp,
3142 			stp->ls_stateid), stp, ls_hash);
3143 		    if (stp->ls_flags & NFSLCK_DELEGWRITE)
3144 			*rflagsp |= NFSV4OPEN_WRITEDELEGATE;
3145 		    else
3146 			*rflagsp |= NFSV4OPEN_READDELEGATE;
3147 		    clp->lc_delegtime = NFSD_MONOSEC +
3148 			nfsrv_lease + NFSRV_LEASEDELTA;
3149 
3150 		    /*
3151 		     * Now, do the associated open.
3152 		     */
3153 		    new_open->ls_stateid.seqid = 1;
3154 		    new_open->ls_stateid.other[0] = clp->lc_clientid.lval[0];
3155 		    new_open->ls_stateid.other[1] = clp->lc_clientid.lval[1];
3156 		    new_open->ls_stateid.other[2] = nfsrv_nextstateindex(clp);
3157 		    new_open->ls_flags = (new_stp->ls_flags&NFSLCK_DENYBITS)|
3158 			NFSLCK_OPEN;
3159 		    if (stp->ls_flags & NFSLCK_DELEGWRITE)
3160 			new_open->ls_flags |= (NFSLCK_READACCESS |
3161 			    NFSLCK_WRITEACCESS);
3162 		    else
3163 			new_open->ls_flags |= NFSLCK_READACCESS;
3164 		    new_open->ls_uid = new_stp->ls_uid;
3165 		    new_open->ls_lfp = lfp;
3166 		    new_open->ls_clp = clp;
3167 		    LIST_INIT(&new_open->ls_open);
3168 		    LIST_INSERT_HEAD(&lfp->lf_open, new_open, ls_file);
3169 		    LIST_INSERT_HEAD(NFSSTATEHASH(clp, new_open->ls_stateid),
3170 			new_open, ls_hash);
3171 		    /*
3172 		     * and handle the open owner
3173 		     */
3174 		    if (ownerstp) {
3175 			new_open->ls_openowner = ownerstp;
3176 			LIST_INSERT_HEAD(&ownerstp->ls_open,new_open,ls_list);
3177 		    } else {
3178 			new_open->ls_openowner = new_stp;
3179 			new_stp->ls_flags = 0;
3180 			nfsrvd_refcache(new_stp->ls_op);
3181 			new_stp->ls_noopens = 0;
3182 			LIST_INIT(&new_stp->ls_open);
3183 			LIST_INSERT_HEAD(&new_stp->ls_open, new_open, ls_list);
3184 			LIST_INSERT_HEAD(&clp->lc_open, new_stp, ls_list);
3185 			*new_stpp = NULL;
3186 			VNET(nfsstatsv1_p)->srvopenowners++;
3187 			nfsrv_openpluslock++;
3188 		    }
3189 		    openstp = new_open;
3190 		    new_open = NULL;
3191 		    VNET(nfsstatsv1_p)->srvopens++;
3192 		    nfsrv_openpluslock++;
3193 		    break;
3194 		}
3195 	    }
3196 	    if (stp == LIST_END(&clp->lc_olddeleg))
3197 		error = NFSERR_EXPIRED;
3198 	} else if (new_stp->ls_flags & (NFSLCK_DELEGREAD | NFSLCK_DELEGWRITE)) {
3199 	    /*
3200 	     * Scan to see that no delegation for this client and file
3201 	     * doesn't already exist.
3202 	     * There also shouldn't yet be an Open for this file and
3203 	     * openowner.
3204 	     */
3205 	    LIST_FOREACH(stp, &lfp->lf_deleg, ls_file) {
3206 		if (stp->ls_clp == clp)
3207 		    break;
3208 	    }
3209 	    if (stp == LIST_END(&lfp->lf_deleg) && openstp == NULL) {
3210 		/*
3211 		 * This is the Claim_Previous case with a delegation
3212 		 * type != Delegate_None.
3213 		 */
3214 		/*
3215 		 * First, add the delegation. (Although we must issue the
3216 		 * delegation, we can also ask for an immediate return.)
3217 		 */
3218 		new_deleg->ls_stateid.seqid = delegstateidp->seqid = 1;
3219 		new_deleg->ls_stateid.other[0] = delegstateidp->other[0] =
3220 		    clp->lc_clientid.lval[0];
3221 		new_deleg->ls_stateid.other[1] = delegstateidp->other[1] =
3222 		    clp->lc_clientid.lval[1];
3223 		new_deleg->ls_stateid.other[2] = delegstateidp->other[2] =
3224 		    nfsrv_nextstateindex(clp);
3225 		if (new_stp->ls_flags & NFSLCK_DELEGWRITE) {
3226 		    new_deleg->ls_flags = (NFSLCK_DELEGWRITE |
3227 			NFSLCK_READACCESS | NFSLCK_WRITEACCESS);
3228 		    *rflagsp |= NFSV4OPEN_WRITEDELEGATE;
3229 		    nfsrv_writedelegcnt++;
3230 		} else {
3231 		    new_deleg->ls_flags = (NFSLCK_DELEGREAD |
3232 			NFSLCK_READACCESS);
3233 		    *rflagsp |= NFSV4OPEN_READDELEGATE;
3234 		}
3235 		new_deleg->ls_uid = new_stp->ls_uid;
3236 		new_deleg->ls_lfp = lfp;
3237 		new_deleg->ls_clp = clp;
3238 		new_deleg->ls_filerev = filerev;
3239 		new_deleg->ls_compref = nd->nd_compref;
3240 		new_deleg->ls_lastrecall = 0;
3241 		LIST_INSERT_HEAD(&lfp->lf_deleg, new_deleg, ls_file);
3242 		LIST_INSERT_HEAD(NFSSTATEHASH(clp,
3243 		    new_deleg->ls_stateid), new_deleg, ls_hash);
3244 		LIST_INSERT_HEAD(&clp->lc_deleg, new_deleg, ls_list);
3245 		new_deleg = NULL;
3246 		if (delegate == 2 || nfsrv_issuedelegs == 0 ||
3247 		    (clp->lc_flags & (LCL_CALLBACKSON | LCL_CBDOWN)) !=
3248 		     LCL_CALLBACKSON ||
3249 		    NFSRV_V4DELEGLIMIT(nfsrv_delegatecnt) ||
3250 		    !NFSVNO_DELEGOK(vp))
3251 		    *rflagsp |= NFSV4OPEN_RECALL;
3252 		VNET(nfsstatsv1_p)->srvdelegates++;
3253 		nfsrv_openpluslock++;
3254 		nfsrv_delegatecnt++;
3255 
3256 		/*
3257 		 * Now, do the associated open.
3258 		 */
3259 		new_open->ls_stateid.seqid = 1;
3260 		new_open->ls_stateid.other[0] = clp->lc_clientid.lval[0];
3261 		new_open->ls_stateid.other[1] = clp->lc_clientid.lval[1];
3262 		new_open->ls_stateid.other[2] = nfsrv_nextstateindex(clp);
3263 		new_open->ls_flags = (new_stp->ls_flags & NFSLCK_DENYBITS) |
3264 		    NFSLCK_OPEN;
3265 		if (new_stp->ls_flags & NFSLCK_DELEGWRITE)
3266 			new_open->ls_flags |= (NFSLCK_READACCESS |
3267 			    NFSLCK_WRITEACCESS);
3268 		else
3269 			new_open->ls_flags |= NFSLCK_READACCESS;
3270 		new_open->ls_uid = new_stp->ls_uid;
3271 		new_open->ls_lfp = lfp;
3272 		new_open->ls_clp = clp;
3273 		LIST_INIT(&new_open->ls_open);
3274 		LIST_INSERT_HEAD(&lfp->lf_open, new_open, ls_file);
3275 		LIST_INSERT_HEAD(NFSSTATEHASH(clp, new_open->ls_stateid),
3276 		   new_open, ls_hash);
3277 		/*
3278 		 * and handle the open owner
3279 		 */
3280 		if (ownerstp) {
3281 		    new_open->ls_openowner = ownerstp;
3282 		    LIST_INSERT_HEAD(&ownerstp->ls_open, new_open, ls_list);
3283 		} else {
3284 		    new_open->ls_openowner = new_stp;
3285 		    new_stp->ls_flags = 0;
3286 		    nfsrvd_refcache(new_stp->ls_op);
3287 		    new_stp->ls_noopens = 0;
3288 		    LIST_INIT(&new_stp->ls_open);
3289 		    LIST_INSERT_HEAD(&new_stp->ls_open, new_open, ls_list);
3290 		    LIST_INSERT_HEAD(&clp->lc_open, new_stp, ls_list);
3291 		    *new_stpp = NULL;
3292 		    VNET(nfsstatsv1_p)->srvopenowners++;
3293 		    nfsrv_openpluslock++;
3294 		}
3295 		openstp = new_open;
3296 		new_open = NULL;
3297 		VNET(nfsstatsv1_p)->srvopens++;
3298 		nfsrv_openpluslock++;
3299 	    } else {
3300 		error = NFSERR_RECLAIMCONFLICT;
3301 	    }
3302 	} else if (ownerstp) {
3303 		if (ownerstp->ls_flags & NFSLCK_NEEDSCONFIRM) {
3304 		    /* Replace the open */
3305 		    if (ownerstp->ls_op)
3306 			nfsrvd_derefcache(ownerstp->ls_op);
3307 		    ownerstp->ls_op = new_stp->ls_op;
3308 		    nfsrvd_refcache(ownerstp->ls_op);
3309 		    ownerstp->ls_seq = new_stp->ls_seq;
3310 		    *rflagsp |= NFSV4OPEN_RESULTCONFIRM;
3311 		    stp = LIST_FIRST(&ownerstp->ls_open);
3312 		    stp->ls_flags = (new_stp->ls_flags & NFSLCK_SHAREBITS) |
3313 			NFSLCK_OPEN;
3314 		    stp->ls_stateid.seqid = 1;
3315 		    stp->ls_uid = new_stp->ls_uid;
3316 		    if (lfp != stp->ls_lfp) {
3317 			LIST_REMOVE(stp, ls_file);
3318 			LIST_INSERT_HEAD(&lfp->lf_open, stp, ls_file);
3319 			stp->ls_lfp = lfp;
3320 		    }
3321 		    openstp = stp;
3322 		} else if (openstp) {
3323 		    openstp->ls_flags |= (new_stp->ls_flags & NFSLCK_SHAREBITS);
3324 		    openstp->ls_stateid.seqid++;
3325 		    if ((nd->nd_flag & ND_NFSV41) != 0 &&
3326 			openstp->ls_stateid.seqid == 0)
3327 			openstp->ls_stateid.seqid = 1;
3328 
3329 		    /*
3330 		     * This is where we can choose to issue a delegation.
3331 		     */
3332 		    nfsrv_issuedelegation(vp, clp, nd, delegate, writedeleg,
3333 			readonly, filerev, NFSVNO_EXRDONLY(exp), &new_deleg,
3334 			new_stp, lfp, rflagsp, delegstateidp);
3335 		} else {
3336 		    new_open->ls_stateid.seqid = 1;
3337 		    new_open->ls_stateid.other[0] = clp->lc_clientid.lval[0];
3338 		    new_open->ls_stateid.other[1] = clp->lc_clientid.lval[1];
3339 		    new_open->ls_stateid.other[2] = nfsrv_nextstateindex(clp);
3340 		    new_open->ls_flags = (new_stp->ls_flags & NFSLCK_SHAREBITS)|
3341 			NFSLCK_OPEN;
3342 		    new_open->ls_uid = new_stp->ls_uid;
3343 		    new_open->ls_openowner = ownerstp;
3344 		    new_open->ls_lfp = lfp;
3345 		    new_open->ls_clp = clp;
3346 		    LIST_INIT(&new_open->ls_open);
3347 		    LIST_INSERT_HEAD(&lfp->lf_open, new_open, ls_file);
3348 		    LIST_INSERT_HEAD(&ownerstp->ls_open, new_open, ls_list);
3349 		    LIST_INSERT_HEAD(NFSSTATEHASH(clp, new_open->ls_stateid),
3350 			new_open, ls_hash);
3351 		    openstp = new_open;
3352 		    new_open = NULL;
3353 		    VNET(nfsstatsv1_p)->srvopens++;
3354 		    nfsrv_openpluslock++;
3355 
3356 		    /*
3357 		     * This is where we can choose to issue a delegation.
3358 		     */
3359 		    nfsrv_issuedelegation(vp, clp, nd, delegate, writedeleg,
3360 			readonly, filerev, NFSVNO_EXRDONLY(exp), &new_deleg,
3361 			new_stp, lfp, rflagsp, delegstateidp);
3362 		}
3363 	} else {
3364 		/*
3365 		 * New owner case. Start the open_owner sequence with a
3366 		 * Needs confirmation (unless a reclaim) and hang the
3367 		 * new open off it.
3368 		 */
3369 		new_open->ls_stateid.seqid = 1;
3370 		new_open->ls_stateid.other[0] = clp->lc_clientid.lval[0];
3371 		new_open->ls_stateid.other[1] = clp->lc_clientid.lval[1];
3372 		new_open->ls_stateid.other[2] = nfsrv_nextstateindex(clp);
3373 		new_open->ls_flags = (new_stp->ls_flags & NFSLCK_SHAREBITS) |
3374 		    NFSLCK_OPEN;
3375 		new_open->ls_uid = new_stp->ls_uid;
3376 		LIST_INIT(&new_open->ls_open);
3377 		new_open->ls_openowner = new_stp;
3378 		new_open->ls_lfp = lfp;
3379 		new_open->ls_clp = clp;
3380 		LIST_INSERT_HEAD(&lfp->lf_open, new_open, ls_file);
3381 		if (new_stp->ls_flags & NFSLCK_RECLAIM) {
3382 			new_stp->ls_flags = 0;
3383 		} else if ((nd->nd_flag & ND_NFSV41) != 0) {
3384 		    /*
3385 		     * This is where we can choose to issue a delegation.
3386 		     */
3387 		    nfsrv_issuedelegation(vp, clp, nd, delegate, writedeleg,
3388 			readonly, filerev, NFSVNO_EXRDONLY(exp), &new_deleg,
3389 			new_stp, lfp, rflagsp, delegstateidp);
3390 		    /* NFSv4.1 never needs confirmation. */
3391 		    new_stp->ls_flags = 0;
3392 
3393 		    /*
3394 		     * Since NFSv4.1 never does an OpenConfirm, the first
3395 		     * open state will be acquired here.
3396 		     */
3397 		    if (!(clp->lc_flags & LCL_STAMPEDSTABLE)) {
3398 			clp->lc_flags |= LCL_STAMPEDSTABLE;
3399 			len = clp->lc_idlen;
3400 			NFSBCOPY(clp->lc_id, clidp, len);
3401 			gotstate = 1;
3402 		    }
3403 		} else {
3404 		    *rflagsp |= NFSV4OPEN_RESULTCONFIRM;
3405 		    new_stp->ls_flags = NFSLCK_NEEDSCONFIRM;
3406 		}
3407 		nfsrvd_refcache(new_stp->ls_op);
3408 		new_stp->ls_noopens = 0;
3409 		LIST_INIT(&new_stp->ls_open);
3410 		LIST_INSERT_HEAD(&new_stp->ls_open, new_open, ls_list);
3411 		LIST_INSERT_HEAD(&clp->lc_open, new_stp, ls_list);
3412 		LIST_INSERT_HEAD(NFSSTATEHASH(clp, new_open->ls_stateid),
3413 		    new_open, ls_hash);
3414 		openstp = new_open;
3415 		new_open = NULL;
3416 		*new_stpp = NULL;
3417 		VNET(nfsstatsv1_p)->srvopens++;
3418 		nfsrv_openpluslock++;
3419 		VNET(nfsstatsv1_p)->srvopenowners++;
3420 		nfsrv_openpluslock++;
3421 	}
3422 	if (!error) {
3423 		stateidp->seqid = openstp->ls_stateid.seqid;
3424 		stateidp->other[0] = openstp->ls_stateid.other[0];
3425 		stateidp->other[1] = openstp->ls_stateid.other[1];
3426 		stateidp->other[2] = openstp->ls_stateid.other[2];
3427 	}
3428 	NFSUNLOCKSTATE();
3429 	if (haslock) {
3430 		NFSLOCKV4ROOTMUTEX();
3431 		nfsv4_unlock(&nfsv4rootfs_lock, 1);
3432 		NFSUNLOCKV4ROOTMUTEX();
3433 	}
3434 	if (new_open)
3435 		free(new_open, M_NFSDSTATE);
3436 	if (new_deleg)
3437 		free(new_deleg, M_NFSDSTATE);
3438 
3439 	/*
3440 	 * If the NFSv4.1 client just acquired its first open, write a timestamp
3441 	 * to the stable storage file.
3442 	 */
3443 	if (gotstate != 0) {
3444 		nfsrv_writestable(clidp, len, NFSNST_NEWSTATE, p);
3445 		nfsrv_backupstable();
3446 	}
3447 
3448 out:
3449 	free(clidp, M_TEMP);
3450 	NFSEXITCODE2(error, nd);
3451 	return (error);
3452 }
3453 
3454 /*
3455  * Open update. Does the confirm, downgrade and close.
3456  */
3457 int
nfsrv_openupdate(vnode_t vp,struct nfsstate * new_stp,nfsquad_t clientid,nfsv4stateid_t * stateidp,struct nfsrv_descript * nd,NFSPROC_T * p,int * retwriteaccessp)3458 nfsrv_openupdate(vnode_t vp, struct nfsstate *new_stp, nfsquad_t clientid,
3459     nfsv4stateid_t *stateidp, struct nfsrv_descript *nd, NFSPROC_T *p,
3460     int *retwriteaccessp)
3461 {
3462 	struct nfsstate *stp;
3463 	struct nfsclient *clp;
3464 	u_int32_t bits;
3465 	int error = 0, gotstate = 0, len = 0;
3466 	u_char *clidp = NULL;
3467 
3468 	/*
3469 	 * Check for restart conditions (client and server).
3470 	 */
3471 	error = nfsrv_checkrestart(clientid, new_stp->ls_flags,
3472 	    &new_stp->ls_stateid, 0);
3473 	if (error)
3474 		goto out;
3475 
3476 	clidp = malloc(NFSV4_OPAQUELIMIT, M_TEMP, M_WAITOK);
3477 	NFSLOCKSTATE();
3478 	/*
3479 	 * Get the open structure via clientid and stateid.
3480 	 */
3481 	error = nfsrv_getclient(clientid, CLOPS_RENEW, &clp, NULL,
3482 	    (nfsquad_t)((u_quad_t)0), 0, nd, p);
3483 	if (!error)
3484 		error = nfsrv_getstate(clp, &new_stp->ls_stateid,
3485 		    new_stp->ls_flags, &stp);
3486 
3487 	/*
3488 	 * Sanity check the open.
3489 	 */
3490 	if (!error && (!(stp->ls_flags & NFSLCK_OPEN) ||
3491 		(!(new_stp->ls_flags & NFSLCK_CONFIRM) &&
3492 		 (stp->ls_openowner->ls_flags & NFSLCK_NEEDSCONFIRM)) ||
3493 		((new_stp->ls_flags & NFSLCK_CONFIRM) &&
3494 		 (!(stp->ls_openowner->ls_flags & NFSLCK_NEEDSCONFIRM)))))
3495 		error = NFSERR_BADSTATEID;
3496 
3497 	if (!error)
3498 		error = nfsrv_checkseqid(nd, new_stp->ls_seq,
3499 		    stp->ls_openowner, new_stp->ls_op);
3500 	if (!error && stp->ls_stateid.seqid != new_stp->ls_stateid.seqid &&
3501 	    (((nd->nd_flag & ND_NFSV41) == 0 &&
3502 	      !(new_stp->ls_flags & NFSLCK_CONFIRM)) ||
3503 	     ((nd->nd_flag & ND_NFSV41) != 0 &&
3504 	      new_stp->ls_stateid.seqid != 0)))
3505 		error = NFSERR_OLDSTATEID;
3506 	if (!error && vp->v_type != VREG) {
3507 		if (vp->v_type == VDIR)
3508 			error = NFSERR_ISDIR;
3509 		else
3510 			error = NFSERR_INVAL;
3511 	}
3512 
3513 	if (error) {
3514 		/*
3515 		 * If a client tries to confirm an Open with a bad
3516 		 * seqid# and there are no byte range locks or other Opens
3517 		 * on the openowner, just throw it away, so the next use of the
3518 		 * openowner will start a fresh seq#.
3519 		 */
3520 		if (error == NFSERR_BADSEQID &&
3521 		    (new_stp->ls_flags & NFSLCK_CONFIRM) &&
3522 		    nfsrv_nootherstate(stp))
3523 			nfsrv_freeopenowner(stp->ls_openowner, 0, p);
3524 		NFSUNLOCKSTATE();
3525 		goto out;
3526 	}
3527 
3528 	/*
3529 	 * Set the return stateid.
3530 	 */
3531 	stateidp->seqid = stp->ls_stateid.seqid + 1;
3532 	if ((nd->nd_flag & ND_NFSV41) != 0 && stateidp->seqid == 0)
3533 		stateidp->seqid = 1;
3534 	stateidp->other[0] = stp->ls_stateid.other[0];
3535 	stateidp->other[1] = stp->ls_stateid.other[1];
3536 	stateidp->other[2] = stp->ls_stateid.other[2];
3537 	/*
3538 	 * Now, handle the three cases.
3539 	 */
3540 	if (new_stp->ls_flags & NFSLCK_CONFIRM) {
3541 		/*
3542 		 * If the open doesn't need confirmation, it seems to me that
3543 		 * there is a client error, but I'll just log it and keep going?
3544 		 */
3545 		if (!(stp->ls_openowner->ls_flags & NFSLCK_NEEDSCONFIRM))
3546 			printf("Nfsv4d: stray open confirm\n");
3547 		stp->ls_openowner->ls_flags = 0;
3548 		stp->ls_stateid.seqid++;
3549 		if ((nd->nd_flag & ND_NFSV41) != 0 &&
3550 		    stp->ls_stateid.seqid == 0)
3551 			stp->ls_stateid.seqid = 1;
3552 		if (!(clp->lc_flags & LCL_STAMPEDSTABLE)) {
3553 			clp->lc_flags |= LCL_STAMPEDSTABLE;
3554 			len = clp->lc_idlen;
3555 			NFSBCOPY(clp->lc_id, clidp, len);
3556 			gotstate = 1;
3557 		}
3558 	} else if (new_stp->ls_flags & NFSLCK_CLOSE) {
3559 		if (retwriteaccessp != NULL) {
3560 			if ((stp->ls_flags & NFSLCK_WRITEACCESS) != 0)
3561 				*retwriteaccessp = 1;
3562 			else
3563 				*retwriteaccessp = 0;
3564 		}
3565 		if (nfsrv_dolocallocks != 0 && !LIST_EMPTY(&stp->ls_open)) {
3566 			ASSERT_VOP_ELOCKED(vp, "nfsrv_openupdate");
3567 			nfsrv_freeopen(stp, vp, 1, p);
3568 		} else {
3569 			nfsrv_freeopen(stp, NULL, 0, p);
3570 		}
3571 	} else {
3572 		/*
3573 		 * Update the share bits, making sure that the new set are a
3574 		 * subset of the old ones.
3575 		 */
3576 		bits = (new_stp->ls_flags & NFSLCK_SHAREBITS);
3577 		if (~(stp->ls_flags) & bits) {
3578 			NFSUNLOCKSTATE();
3579 			error = NFSERR_INVAL;
3580 			goto out;
3581 		}
3582 		stp->ls_flags = (bits | NFSLCK_OPEN);
3583 		stp->ls_stateid.seqid++;
3584 		if ((nd->nd_flag & ND_NFSV41) != 0 &&
3585 		    stp->ls_stateid.seqid == 0)
3586 			stp->ls_stateid.seqid = 1;
3587 	}
3588 	NFSUNLOCKSTATE();
3589 
3590 	/*
3591 	 * If the client just confirmed its first open, write a timestamp
3592 	 * to the stable storage file.
3593 	 */
3594 	if (gotstate != 0) {
3595 		nfsrv_writestable(clidp, len, NFSNST_NEWSTATE, p);
3596 		nfsrv_backupstable();
3597 	}
3598 
3599 out:
3600 	free(clidp, M_TEMP);
3601 	NFSEXITCODE2(error, nd);
3602 	return (error);
3603 }
3604 
3605 /*
3606  * Delegation update. Does the purge and return.
3607  */
3608 int
nfsrv_delegupdate(struct nfsrv_descript * nd,nfsquad_t clientid,nfsv4stateid_t * stateidp,vnode_t vp,int op,struct ucred * cred,NFSPROC_T * p,int * retwriteaccessp)3609 nfsrv_delegupdate(struct nfsrv_descript *nd, nfsquad_t clientid,
3610     nfsv4stateid_t *stateidp, vnode_t vp, int op, struct ucred *cred,
3611     NFSPROC_T *p, int *retwriteaccessp)
3612 {
3613 	struct nfsstate *stp;
3614 	struct nfsclient *clp;
3615 	int error = 0;
3616 	fhandle_t fh;
3617 
3618 	/*
3619 	 * Do a sanity check against the file handle for DelegReturn.
3620 	 */
3621 	if (vp) {
3622 		error = nfsvno_getfh(vp, &fh, p);
3623 		if (error)
3624 			goto out;
3625 	}
3626 	/*
3627 	 * Check for restart conditions (client and server).
3628 	 */
3629 	if (op == NFSV4OP_DELEGRETURN)
3630 		error = nfsrv_checkrestart(clientid, NFSLCK_DELEGRETURN,
3631 			stateidp, 0);
3632 	else
3633 		error = nfsrv_checkrestart(clientid, NFSLCK_DELEGPURGE,
3634 			stateidp, 0);
3635 
3636 	NFSLOCKSTATE();
3637 	/*
3638 	 * Get the open structure via clientid and stateid.
3639 	 */
3640 	if (!error)
3641 	    error = nfsrv_getclient(clientid, CLOPS_RENEW, &clp, NULL,
3642 		(nfsquad_t)((u_quad_t)0), 0, nd, p);
3643 	if (error) {
3644 		if (error == NFSERR_CBPATHDOWN)
3645 			error = 0;
3646 		if (error == NFSERR_STALECLIENTID && op == NFSV4OP_DELEGRETURN)
3647 			error = NFSERR_STALESTATEID;
3648 	}
3649 	if (!error && op == NFSV4OP_DELEGRETURN) {
3650 	    error = nfsrv_getstate(clp, stateidp, NFSLCK_DELEGRETURN, &stp);
3651 	    if (!error && stp->ls_stateid.seqid != stateidp->seqid &&
3652 		((nd->nd_flag & ND_NFSV41) == 0 || stateidp->seqid != 0))
3653 		error = NFSERR_OLDSTATEID;
3654 	}
3655 	/*
3656 	 * NFSERR_EXPIRED means that the state has gone away,
3657 	 * so Delegations have been purged. Just return ok.
3658 	 */
3659 	if (error == NFSERR_EXPIRED && op == NFSV4OP_DELEGPURGE) {
3660 		NFSUNLOCKSTATE();
3661 		error = 0;
3662 		goto out;
3663 	}
3664 	if (error) {
3665 		NFSUNLOCKSTATE();
3666 		goto out;
3667 	}
3668 
3669 	if (op == NFSV4OP_DELEGRETURN) {
3670 		if (NFSBCMP((caddr_t)&fh, (caddr_t)&stp->ls_lfp->lf_fh,
3671 		    sizeof (fhandle_t))) {
3672 			NFSUNLOCKSTATE();
3673 			error = NFSERR_BADSTATEID;
3674 			goto out;
3675 		}
3676 		if (retwriteaccessp != NULL) {
3677 			if ((stp->ls_flags & NFSLCK_DELEGWRITE) != 0)
3678 				*retwriteaccessp = 1;
3679 			else
3680 				*retwriteaccessp = 0;
3681 		}
3682 		nfsrv_freedeleg(stp);
3683 	} else {
3684 		nfsrv_freedeleglist(&clp->lc_olddeleg);
3685 	}
3686 	NFSUNLOCKSTATE();
3687 	error = 0;
3688 
3689 out:
3690 	NFSEXITCODE(error);
3691 	return (error);
3692 }
3693 
3694 /*
3695  * Release lock owner.
3696  */
3697 int
nfsrv_releaselckown(struct nfsstate * new_stp,nfsquad_t clientid,NFSPROC_T * p)3698 nfsrv_releaselckown(struct nfsstate *new_stp, nfsquad_t clientid,
3699     NFSPROC_T *p)
3700 {
3701 	struct nfsstate *stp, *nstp, *openstp, *ownstp;
3702 	struct nfsclient *clp;
3703 	int error = 0;
3704 
3705 	/*
3706 	 * Check for restart conditions (client and server).
3707 	 */
3708 	error = nfsrv_checkrestart(clientid, new_stp->ls_flags,
3709 	    &new_stp->ls_stateid, 0);
3710 	if (error)
3711 		goto out;
3712 
3713 	NFSLOCKSTATE();
3714 	/*
3715 	 * Get the lock owner by name.
3716 	 */
3717 	error = nfsrv_getclient(clientid, CLOPS_RENEW, &clp, NULL,
3718 	    (nfsquad_t)((u_quad_t)0), 0, NULL, p);
3719 	if (error) {
3720 		NFSUNLOCKSTATE();
3721 		goto out;
3722 	}
3723 	LIST_FOREACH(ownstp, &clp->lc_open, ls_list) {
3724 	    LIST_FOREACH(openstp, &ownstp->ls_open, ls_list) {
3725 		stp = LIST_FIRST(&openstp->ls_open);
3726 		while (stp != LIST_END(&openstp->ls_open)) {
3727 		    nstp = LIST_NEXT(stp, ls_list);
3728 		    /*
3729 		     * If the owner matches, check for locks and
3730 		     * then free or return an error.
3731 		     */
3732 		    if (stp->ls_ownerlen == new_stp->ls_ownerlen &&
3733 			!NFSBCMP(stp->ls_owner, new_stp->ls_owner,
3734 			 stp->ls_ownerlen)){
3735 			if (LIST_EMPTY(&stp->ls_lock)) {
3736 			    nfsrv_freelockowner(stp, NULL, 0, p);
3737 			} else {
3738 			    NFSUNLOCKSTATE();
3739 			    error = NFSERR_LOCKSHELD;
3740 			    goto out;
3741 			}
3742 		    }
3743 		    stp = nstp;
3744 		}
3745 	    }
3746 	}
3747 	NFSUNLOCKSTATE();
3748 
3749 out:
3750 	NFSEXITCODE(error);
3751 	return (error);
3752 }
3753 
3754 /*
3755  * Get the file handle for a lock structure.
3756  */
3757 static int
nfsrv_getlockfh(vnode_t vp,u_short flags,struct nfslockfile * new_lfp,fhandle_t * nfhp,NFSPROC_T * p)3758 nfsrv_getlockfh(vnode_t vp, u_short flags, struct nfslockfile *new_lfp,
3759     fhandle_t *nfhp, NFSPROC_T *p)
3760 {
3761 	fhandle_t *fhp = NULL;
3762 	int error;
3763 
3764 	/*
3765 	 * For lock, use the new nfslock structure, otherwise just
3766 	 * a fhandle_t on the stack.
3767 	 */
3768 	if (flags & NFSLCK_OPEN) {
3769 		KASSERT(new_lfp != NULL, ("nfsrv_getlockfh: new_lfp NULL"));
3770 		fhp = &new_lfp->lf_fh;
3771 	} else if (nfhp) {
3772 		fhp = nfhp;
3773 	} else {
3774 		panic("nfsrv_getlockfh");
3775 	}
3776 	error = nfsvno_getfh(vp, fhp, p);
3777 	NFSEXITCODE(error);
3778 	return (error);
3779 }
3780 
3781 /*
3782  * Get an nfs lock structure. Allocate one, as required, and return a
3783  * pointer to it.
3784  * Returns an NFSERR_xxx upon failure or -1 to indicate no current lock.
3785  */
3786 static int
nfsrv_getlockfile(u_short flags,struct nfslockfile ** new_lfpp,struct nfslockfile ** lfpp,fhandle_t * nfhp,int lockit)3787 nfsrv_getlockfile(u_short flags, struct nfslockfile **new_lfpp,
3788     struct nfslockfile **lfpp, fhandle_t *nfhp, int lockit)
3789 {
3790 	struct nfslockfile *lfp;
3791 	fhandle_t *fhp = NULL, *tfhp;
3792 	struct nfslockhashhead *hp;
3793 	struct nfslockfile *new_lfp = NULL;
3794 
3795 	/*
3796 	 * For lock, use the new nfslock structure, otherwise just
3797 	 * a fhandle_t on the stack.
3798 	 */
3799 	if (flags & NFSLCK_OPEN) {
3800 		new_lfp = *new_lfpp;
3801 		fhp = &new_lfp->lf_fh;
3802 	} else if (nfhp) {
3803 		fhp = nfhp;
3804 	} else {
3805 		panic("nfsrv_getlockfile");
3806 	}
3807 
3808 	hp = NFSLOCKHASH(fhp);
3809 	LIST_FOREACH(lfp, hp, lf_hash) {
3810 		tfhp = &lfp->lf_fh;
3811 		if (NFSVNO_CMPFH(fhp, tfhp)) {
3812 			if (lockit)
3813 				nfsrv_locklf(lfp);
3814 			*lfpp = lfp;
3815 			return (0);
3816 		}
3817 	}
3818 	if (!(flags & NFSLCK_OPEN))
3819 		return (-1);
3820 
3821 	/*
3822 	 * No match, so chain the new one into the list.
3823 	 */
3824 	LIST_INIT(&new_lfp->lf_open);
3825 	LIST_INIT(&new_lfp->lf_lock);
3826 	LIST_INIT(&new_lfp->lf_deleg);
3827 	LIST_INIT(&new_lfp->lf_locallock);
3828 	LIST_INIT(&new_lfp->lf_rollback);
3829 	new_lfp->lf_locallock_lck.nfslock_usecnt = 0;
3830 	new_lfp->lf_locallock_lck.nfslock_lock = 0;
3831 	new_lfp->lf_usecount = 0;
3832 	LIST_INSERT_HEAD(hp, new_lfp, lf_hash);
3833 	*lfpp = new_lfp;
3834 	*new_lfpp = NULL;
3835 	return (0);
3836 }
3837 
3838 /*
3839  * This function adds a nfslock lock structure to the list for the associated
3840  * nfsstate and nfslockfile structures. It will be inserted after the
3841  * entry pointed at by insert_lop.
3842  */
3843 static void
nfsrv_insertlock(struct nfslock * new_lop,struct nfslock * insert_lop,struct nfsstate * stp,struct nfslockfile * lfp)3844 nfsrv_insertlock(struct nfslock *new_lop, struct nfslock *insert_lop,
3845     struct nfsstate *stp, struct nfslockfile *lfp)
3846 {
3847 	struct nfslock *lop, *nlop;
3848 
3849 	new_lop->lo_stp = stp;
3850 	new_lop->lo_lfp = lfp;
3851 
3852 	if (stp != NULL) {
3853 		/* Insert in increasing lo_first order */
3854 		lop = LIST_FIRST(&lfp->lf_lock);
3855 		if (lop == LIST_END(&lfp->lf_lock) ||
3856 		    new_lop->lo_first <= lop->lo_first) {
3857 			LIST_INSERT_HEAD(&lfp->lf_lock, new_lop, lo_lckfile);
3858 		} else {
3859 			nlop = LIST_NEXT(lop, lo_lckfile);
3860 			while (nlop != LIST_END(&lfp->lf_lock) &&
3861 			       nlop->lo_first < new_lop->lo_first) {
3862 				lop = nlop;
3863 				nlop = LIST_NEXT(lop, lo_lckfile);
3864 			}
3865 			LIST_INSERT_AFTER(lop, new_lop, lo_lckfile);
3866 		}
3867 	} else {
3868 		new_lop->lo_lckfile.le_prev = NULL;	/* list not used */
3869 	}
3870 
3871 	/*
3872 	 * Insert after insert_lop, which is overloaded as stp or lfp for
3873 	 * an empty list.
3874 	 */
3875 	if (stp == NULL && (struct nfslockfile *)insert_lop == lfp)
3876 		LIST_INSERT_HEAD(&lfp->lf_locallock, new_lop, lo_lckowner);
3877 	else if ((struct nfsstate *)insert_lop == stp)
3878 		LIST_INSERT_HEAD(&stp->ls_lock, new_lop, lo_lckowner);
3879 	else
3880 		LIST_INSERT_AFTER(insert_lop, new_lop, lo_lckowner);
3881 	if (stp != NULL) {
3882 		VNET(nfsstatsv1_p)->srvlocks++;
3883 		nfsrv_openpluslock++;
3884 	}
3885 }
3886 
3887 /*
3888  * This function updates the locking for a lock owner and given file. It
3889  * maintains a list of lock ranges ordered on increasing file offset that
3890  * are NFSLCK_READ or NFSLCK_WRITE and non-overlapping (aka POSIX style).
3891  * It always adds new_lop to the list and sometimes uses the one pointed
3892  * at by other_lopp.
3893  */
3894 static void
nfsrv_updatelock(struct nfsstate * stp,struct nfslock ** new_lopp,struct nfslock ** other_lopp,struct nfslockfile * lfp)3895 nfsrv_updatelock(struct nfsstate *stp, struct nfslock **new_lopp,
3896     struct nfslock **other_lopp, struct nfslockfile *lfp)
3897 {
3898 	struct nfslock *new_lop = *new_lopp;
3899 	struct nfslock *lop, *tlop, *ilop;
3900 	struct nfslock *other_lop = *other_lopp;
3901 	int unlock = 0, myfile = 0;
3902 	u_int64_t tmp;
3903 
3904 	/*
3905 	 * Work down the list until the lock is merged.
3906 	 */
3907 	if (new_lop->lo_flags & NFSLCK_UNLOCK)
3908 		unlock = 1;
3909 	if (stp != NULL) {
3910 		ilop = (struct nfslock *)stp;
3911 		lop = LIST_FIRST(&stp->ls_lock);
3912 	} else {
3913 		ilop = (struct nfslock *)lfp;
3914 		lop = LIST_FIRST(&lfp->lf_locallock);
3915 	}
3916 	while (lop != NULL) {
3917 	    /*
3918 	     * Only check locks for this file that aren't before the start of
3919 	     * new lock's range.
3920 	     */
3921 	    if (lop->lo_lfp == lfp) {
3922 	      myfile = 1;
3923 	      if (lop->lo_end >= new_lop->lo_first) {
3924 		if (new_lop->lo_end < lop->lo_first) {
3925 			/*
3926 			 * If the new lock ends before the start of the
3927 			 * current lock's range, no merge, just insert
3928 			 * the new lock.
3929 			 */
3930 			break;
3931 		}
3932 		if (new_lop->lo_flags == lop->lo_flags ||
3933 		    (new_lop->lo_first <= lop->lo_first &&
3934 		     new_lop->lo_end >= lop->lo_end)) {
3935 			/*
3936 			 * This lock can be absorbed by the new lock/unlock.
3937 			 * This happens when it covers the entire range
3938 			 * of the old lock or is contiguous
3939 			 * with the old lock and is of the same type or an
3940 			 * unlock.
3941 			 */
3942 			if (lop->lo_first < new_lop->lo_first)
3943 				new_lop->lo_first = lop->lo_first;
3944 			if (lop->lo_end > new_lop->lo_end)
3945 				new_lop->lo_end = lop->lo_end;
3946 			tlop = lop;
3947 			lop = LIST_NEXT(lop, lo_lckowner);
3948 			nfsrv_freenfslock(tlop);
3949 			continue;
3950 		}
3951 
3952 		/*
3953 		 * All these cases are for contiguous locks that are not the
3954 		 * same type, so they can't be merged.
3955 		 */
3956 		if (new_lop->lo_first <= lop->lo_first) {
3957 			/*
3958 			 * This case is where the new lock overlaps with the
3959 			 * first part of the old lock. Move the start of the
3960 			 * old lock to just past the end of the new lock. The
3961 			 * new lock will be inserted in front of the old, since
3962 			 * ilop hasn't been updated. (We are done now.)
3963 			 */
3964 			lop->lo_first = new_lop->lo_end;
3965 			break;
3966 		}
3967 		if (new_lop->lo_end >= lop->lo_end) {
3968 			/*
3969 			 * This case is where the new lock overlaps with the
3970 			 * end of the old lock's range. Move the old lock's
3971 			 * end to just before the new lock's first and insert
3972 			 * the new lock after the old lock.
3973 			 * Might not be done yet, since the new lock could
3974 			 * overlap further locks with higher ranges.
3975 			 */
3976 			lop->lo_end = new_lop->lo_first;
3977 			ilop = lop;
3978 			lop = LIST_NEXT(lop, lo_lckowner);
3979 			continue;
3980 		}
3981 		/*
3982 		 * The final case is where the new lock's range is in the
3983 		 * middle of the current lock's and splits the current lock
3984 		 * up. Use *other_lopp to handle the second part of the
3985 		 * split old lock range. (We are done now.)
3986 		 * For unlock, we use new_lop as other_lop and tmp, since
3987 		 * other_lop and new_lop are the same for this case.
3988 		 * We noted the unlock case above, so we don't need
3989 		 * new_lop->lo_flags any longer.
3990 		 */
3991 		tmp = new_lop->lo_first;
3992 		if (other_lop == NULL) {
3993 			if (!unlock)
3994 				panic("nfsd srv update unlock");
3995 			other_lop = new_lop;
3996 			*new_lopp = NULL;
3997 		}
3998 		other_lop->lo_first = new_lop->lo_end;
3999 		other_lop->lo_end = lop->lo_end;
4000 		other_lop->lo_flags = lop->lo_flags;
4001 		other_lop->lo_stp = stp;
4002 		other_lop->lo_lfp = lfp;
4003 		lop->lo_end = tmp;
4004 		nfsrv_insertlock(other_lop, lop, stp, lfp);
4005 		*other_lopp = NULL;
4006 		ilop = lop;
4007 		break;
4008 	      }
4009 	    }
4010 	    ilop = lop;
4011 	    lop = LIST_NEXT(lop, lo_lckowner);
4012 	    if (myfile && (lop == NULL || lop->lo_lfp != lfp))
4013 		break;
4014 	}
4015 
4016 	/*
4017 	 * Insert the new lock in the list at the appropriate place.
4018 	 */
4019 	if (!unlock) {
4020 		nfsrv_insertlock(new_lop, ilop, stp, lfp);
4021 		*new_lopp = NULL;
4022 	}
4023 }
4024 
4025 /*
4026  * This function handles sequencing of locks, etc.
4027  * It returns an error that indicates what the caller should do.
4028  */
4029 static int
nfsrv_checkseqid(struct nfsrv_descript * nd,u_int32_t seqid,struct nfsstate * stp,struct nfsrvcache * op)4030 nfsrv_checkseqid(struct nfsrv_descript *nd, u_int32_t seqid,
4031     struct nfsstate *stp, struct nfsrvcache *op)
4032 {
4033 	int error = 0;
4034 
4035 	if ((nd->nd_flag & ND_NFSV41) != 0)
4036 		/* NFSv4.1 ignores the open_seqid and lock_seqid. */
4037 		goto out;
4038 	if (op != nd->nd_rp)
4039 		panic("nfsrvstate checkseqid");
4040 	if (!(op->rc_flag & RC_INPROG))
4041 		panic("nfsrvstate not inprog");
4042 	if (stp->ls_op && stp->ls_op->rc_refcnt <= 0) {
4043 		printf("refcnt=%d\n", stp->ls_op->rc_refcnt);
4044 		panic("nfsrvstate op refcnt");
4045 	}
4046 
4047 	/* If ND_ERELOOKUP is set, the seqid has already been handled. */
4048 	if ((nd->nd_flag & ND_ERELOOKUP) != 0)
4049 		goto out;
4050 
4051 	if ((stp->ls_seq + 1) == seqid) {
4052 		if (stp->ls_op)
4053 			nfsrvd_derefcache(stp->ls_op);
4054 		stp->ls_op = op;
4055 		nfsrvd_refcache(op);
4056 		stp->ls_seq = seqid;
4057 		goto out;
4058 	} else if (stp->ls_seq == seqid && stp->ls_op &&
4059 		op->rc_xid == stp->ls_op->rc_xid &&
4060 		op->rc_refcnt == 0 &&
4061 		op->rc_reqlen == stp->ls_op->rc_reqlen &&
4062 		op->rc_cksum == stp->ls_op->rc_cksum) {
4063 		if (stp->ls_op->rc_flag & RC_INPROG) {
4064 			error = NFSERR_DONTREPLY;
4065 			goto out;
4066 		}
4067 		nd->nd_rp = stp->ls_op;
4068 		nd->nd_rp->rc_flag |= RC_INPROG;
4069 		nfsrvd_delcache(op);
4070 		error = NFSERR_REPLYFROMCACHE;
4071 		goto out;
4072 	}
4073 	error = NFSERR_BADSEQID;
4074 
4075 out:
4076 	NFSEXITCODE2(error, nd);
4077 	return (error);
4078 }
4079 
4080 /*
4081  * Just set lc_program to 0 to indicate no callbacks are possible.
4082  * Set the address to the client's transport address. This won't be used
4083  * for callbacks, but can be printed out by nfsstats for info.
4084  * Return error if the xdr can't be parsed, 0 otherwise.
4085  */
4086 int
nfsrv_getclientipaddr(struct nfsrv_descript * nd,struct nfsclient * clp)4087 nfsrv_getclientipaddr(struct nfsrv_descript *nd, struct nfsclient *clp)
4088 {
4089 	uint32_t *tl;
4090 	int error = 0, i;
4091 #ifdef INET
4092 	struct sockaddr_in *rin, *sin;
4093 #endif
4094 #ifdef INET6
4095 	struct sockaddr_in6 *rin6, *sin6;
4096 #endif
4097 
4098 	clp->lc_req.nr_client = NULL;
4099 	clp->lc_req.nr_lock = 0;
4100 	NFSM_DISSECT(tl, uint32_t *, NFSX_UNSIGNED);
4101 	i = fxdr_unsigned(int, *tl);
4102 	if (i < 0) {
4103 		error = NFSERR_BADXDR;
4104 		goto nfsmout;
4105 	} else if (i > 0) {
4106 		error = nfsm_advance(nd, NFSM_RNDUP(i), -1);
4107 		if (error)
4108 			goto nfsmout;
4109 	}
4110 	NFSM_DISSECT(tl, uint32_t *, NFSX_UNSIGNED);
4111 	i = fxdr_unsigned(int, *tl);
4112 	if (i < 0) {
4113 		error = NFSERR_BADXDR;
4114 		goto nfsmout;
4115 	} else if (i > 0) {
4116 		error = nfsm_advance(nd, NFSM_RNDUP(i), -1);
4117 		if (error)
4118 			goto nfsmout;
4119 	}
4120 	switch (nd->nd_nam->sa_family) {
4121 #ifdef INET
4122 	case AF_INET:
4123 		sin = (struct sockaddr_in *)nd->nd_nam;
4124 		rin = (struct sockaddr_in *)clp->lc_req.nr_nam;
4125 		rin->sin_family = AF_INET;
4126 		rin->sin_len = sizeof(struct sockaddr_in);
4127 		rin->sin_addr.s_addr = sin->sin_addr.s_addr;
4128 		rin->sin_port = 0x0;
4129 		break;
4130 #endif
4131 #ifdef INET6
4132 	case AF_INET6:
4133 		sin6 = (struct sockaddr_in6 *)nd->nd_nam;
4134 		rin6 = (struct sockaddr_in6 *)clp->lc_req.nr_nam;
4135 		rin6->sin6_family = AF_INET6;
4136 		rin6->sin6_len = sizeof(struct sockaddr_in6);
4137 		rin6->sin6_addr = sin6->sin6_addr;
4138 		rin6->sin6_port = 0x0;
4139 		break;
4140 #endif
4141 	}
4142 	clp->lc_program = 0;
4143 nfsmout:
4144 	NFSEXITCODE2(error, nd);
4145 	return (error);
4146 }
4147 
4148 /*
4149  * This function checks for restart conditions.
4150  */
4151 static int
nfsrv_checkrestart(nfsquad_t clientid,u_int32_t flags,nfsv4stateid_t * stateidp,int specialid)4152 nfsrv_checkrestart(nfsquad_t clientid, u_int32_t flags,
4153     nfsv4stateid_t *stateidp, int specialid)
4154 {
4155 	int ret = 0;
4156 
4157 	/*
4158 	 * First check for a server restart. Open, LockT, ReleaseLockOwner
4159 	 * and DelegPurge have a clientid, the rest a stateid.
4160 	 */
4161 	if (flags &
4162 	    (NFSLCK_OPEN | NFSLCK_TEST | NFSLCK_RELEASE | NFSLCK_DELEGPURGE)) {
4163 		if (clientid.lval[0] != VNET(nfsrvboottime)) {
4164 			ret = NFSERR_STALECLIENTID;
4165 			goto out;
4166 		}
4167 	} else if (stateidp->other[0] != VNET(nfsrvboottime) &&
4168 		specialid == 0) {
4169 		ret = NFSERR_STALESTATEID;
4170 		goto out;
4171 	}
4172 
4173 	/*
4174 	 * Read, Write, Setattr and LockT can return NFSERR_GRACE and do
4175 	 * not use a lock/open owner seqid#, so the check can be done now.
4176 	 * (The others will be checked, as required, later.)
4177 	 */
4178 	if (!(flags & (NFSLCK_CHECK | NFSLCK_TEST)))
4179 		goto out;
4180 
4181 	NFSLOCKSTATE();
4182 	ret = nfsrv_checkgrace(NULL, NULL, flags);
4183 	NFSUNLOCKSTATE();
4184 
4185 out:
4186 	NFSEXITCODE(ret);
4187 	return (ret);
4188 }
4189 
4190 /*
4191  * Check for grace.
4192  */
4193 static int
nfsrv_checkgrace(struct nfsrv_descript * nd,struct nfsclient * clp,u_int32_t flags)4194 nfsrv_checkgrace(struct nfsrv_descript *nd, struct nfsclient *clp,
4195     u_int32_t flags)
4196 {
4197 	int error = 0, notreclaimed;
4198 	struct nfsrv_stable *sp;
4199 
4200 	if ((VNET(nfsrv_stablefirst).nsf_flags & (NFSNSF_UPDATEDONE |
4201 	     NFSNSF_GRACEOVER)) == 0) {
4202 		/*
4203 		 * First, check to see if all of the clients have done a
4204 		 * ReclaimComplete.  If so, grace can end now.
4205 		 */
4206 		notreclaimed = 0;
4207 		if (!VNET(nfsd_disable_grace)) {
4208 			LIST_FOREACH(sp, &VNET(nfsrv_stablefirst).nsf_head,
4209 			    nst_list) {
4210 				if ((sp->nst_flag & NFSNST_RECLAIMED) == 0) {
4211 					notreclaimed = 1;
4212 					break;
4213 				}
4214 			}
4215 		}
4216 		if (notreclaimed == 0)
4217 			VNET(nfsrv_stablefirst).nsf_flags |=
4218 			    (NFSNSF_GRACEOVER | NFSNSF_NEEDLOCK);
4219 	}
4220 
4221 	if ((VNET(nfsrv_stablefirst).nsf_flags & NFSNSF_GRACEOVER) != 0) {
4222 		if (flags & NFSLCK_RECLAIM) {
4223 			error = NFSERR_NOGRACE;
4224 			goto out;
4225 		}
4226 	} else {
4227 		if (!(flags & NFSLCK_RECLAIM)) {
4228 			error = NFSERR_GRACE;
4229 			goto out;
4230 		}
4231 		if (nd != NULL && clp != NULL &&
4232 		    (nd->nd_flag & ND_NFSV41) != 0 &&
4233 		    (clp->lc_flags & LCL_RECLAIMCOMPLETE) != 0) {
4234 			error = NFSERR_NOGRACE;
4235 			goto out;
4236 		}
4237 
4238 		/*
4239 		 * If grace is almost over and we are still getting Reclaims,
4240 		 * extend grace a bit.
4241 		 */
4242 		if ((NFSD_MONOSEC + NFSRV_LEASEDELTA) >
4243 		    VNET(nfsrv_stablefirst).nsf_eograce)
4244 			VNET(nfsrv_stablefirst).nsf_eograce =
4245 				NFSD_MONOSEC + NFSRV_LEASEDELTA;
4246 	}
4247 
4248 out:
4249 	NFSEXITCODE(error);
4250 	return (error);
4251 }
4252 
4253 /*
4254  * Do a server callback.
4255  * The "trunc" argument is slightly overloaded and refers to different
4256  * boolean arguments for CBRECALL and CBLAYOUTRECALL.
4257  */
4258 static int
nfsrv_docallback(struct nfsclient * clp,int procnum,nfsv4stateid_t * stateidp,int trunc,fhandle_t * fhp,struct nfsvattr * nap,nfsattrbit_t * attrbitp,int laytype,NFSPROC_T * p)4259 nfsrv_docallback(struct nfsclient *clp, int procnum, nfsv4stateid_t *stateidp,
4260     int trunc, fhandle_t *fhp, struct nfsvattr *nap, nfsattrbit_t *attrbitp,
4261     int laytype, NFSPROC_T *p)
4262 {
4263 	struct mbuf *m;
4264 	u_int32_t *tl;
4265 	struct nfsrv_descript *nd;
4266 	struct ucred *cred;
4267 	int error = 0, slotpos;
4268 	u_int32_t callback;
4269 	struct nfsdsession *sep = NULL;
4270 	uint64_t tval;
4271 	bool dotls;
4272 
4273 	nd = malloc(sizeof(*nd), M_TEMP, M_WAITOK | M_ZERO);
4274 	cred = newnfs_getcred();
4275 	NFSLOCKSTATE();	/* mostly for lc_cbref++ */
4276 	if (clp->lc_flags & LCL_NEEDSCONFIRM) {
4277 		NFSUNLOCKSTATE();
4278 		panic("docallb");
4279 	}
4280 	clp->lc_cbref++;
4281 
4282 	/*
4283 	 * Fill the callback program# and version into the request
4284 	 * structure for newnfs_connect() to use.
4285 	 */
4286 	clp->lc_req.nr_prog = clp->lc_program;
4287 #ifdef notnow
4288 	if ((clp->lc_flags & LCL_NFSV41) != 0)
4289 		clp->lc_req.nr_vers = NFSV41_CBVERS;
4290 	else
4291 #endif
4292 		clp->lc_req.nr_vers = NFSV4_CBVERS;
4293 
4294 	/*
4295 	 * First, fill in some of the fields of nd and cr.
4296 	 */
4297 	nd->nd_flag = ND_NFSV4;
4298 	if (clp->lc_flags & LCL_GSS)
4299 		nd->nd_flag |= ND_KERBV;
4300 	if ((clp->lc_flags & LCL_NFSV41) != 0)
4301 		nd->nd_flag |= ND_NFSV41;
4302 	if ((clp->lc_flags & LCL_NFSV42) != 0)
4303 		nd->nd_flag |= ND_NFSV42;
4304 	nd->nd_repstat = 0;
4305 	cred->cr_uid = clp->lc_uid;
4306 	cred->cr_gid = clp->lc_gid;
4307 	callback = clp->lc_callback;
4308 	NFSUNLOCKSTATE();
4309 	cred->cr_ngroups = 1;
4310 
4311 	/*
4312 	 * Get the first mbuf for the request.
4313 	 */
4314 	MGET(m, M_WAITOK, MT_DATA);
4315 	m->m_len = 0;
4316 	nd->nd_mreq = nd->nd_mb = m;
4317 	nd->nd_bpos = mtod(m, caddr_t);
4318 
4319 	/*
4320 	 * and build the callback request.
4321 	 */
4322 	if (procnum == NFSV4OP_CBGETATTR) {
4323 		nd->nd_procnum = NFSV4PROC_CBCOMPOUND;
4324 		error = nfsrv_cbcallargs(nd, clp, callback, NFSV4OP_CBGETATTR,
4325 		    "CB Getattr", &sep, &slotpos);
4326 		if (error != 0) {
4327 			m_freem(nd->nd_mreq);
4328 			goto errout;
4329 		}
4330 		(void)nfsm_fhtom(NULL, nd, (u_int8_t *)fhp, NFSX_MYFH, 0);
4331 		(void)nfsrv_putattrbit(nd, attrbitp);
4332 	} else if (procnum == NFSV4OP_CBRECALL) {
4333 		nd->nd_procnum = NFSV4PROC_CBCOMPOUND;
4334 		error = nfsrv_cbcallargs(nd, clp, callback, NFSV4OP_CBRECALL,
4335 		    "CB Recall", &sep, &slotpos);
4336 		if (error != 0) {
4337 			m_freem(nd->nd_mreq);
4338 			goto errout;
4339 		}
4340 		NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED + NFSX_STATEID);
4341 		*tl++ = txdr_unsigned(stateidp->seqid);
4342 		NFSBCOPY((caddr_t)stateidp->other, (caddr_t)tl,
4343 		    NFSX_STATEIDOTHER);
4344 		tl += (NFSX_STATEIDOTHER / NFSX_UNSIGNED);
4345 		if (trunc)
4346 			*tl = newnfs_true;
4347 		else
4348 			*tl = newnfs_false;
4349 		(void)nfsm_fhtom(NULL, nd, (u_int8_t *)fhp, NFSX_MYFH, 0);
4350 	} else if (procnum == NFSV4OP_CBLAYOUTRECALL) {
4351 		NFSD_DEBUG(4, "docallback layout recall\n");
4352 		nd->nd_procnum = NFSV4PROC_CBCOMPOUND;
4353 		error = nfsrv_cbcallargs(nd, clp, callback,
4354 		    NFSV4OP_CBLAYOUTRECALL, "CB Reclayout", &sep, &slotpos);
4355 		NFSD_DEBUG(4, "aft cbcallargs=%d\n", error);
4356 		if (error != 0) {
4357 			m_freem(nd->nd_mreq);
4358 			goto errout;
4359 		}
4360 		NFSM_BUILD(tl, u_int32_t *, 4 * NFSX_UNSIGNED);
4361 		*tl++ = txdr_unsigned(laytype);
4362 		*tl++ = txdr_unsigned(NFSLAYOUTIOMODE_ANY);
4363 		if (trunc)
4364 			*tl++ = newnfs_true;
4365 		else
4366 			*tl++ = newnfs_false;
4367 		*tl = txdr_unsigned(NFSV4LAYOUTRET_FILE);
4368 		(void)nfsm_fhtom(NULL, nd, (uint8_t *)fhp, NFSX_MYFH, 0);
4369 		NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_HYPER + NFSX_STATEID);
4370 		tval = 0;
4371 		txdr_hyper(tval, tl); tl += 2;
4372 		tval = UINT64_MAX;
4373 		txdr_hyper(tval, tl); tl += 2;
4374 		*tl++ = txdr_unsigned(stateidp->seqid);
4375 		NFSBCOPY(stateidp->other, tl, NFSX_STATEIDOTHER);
4376 		tl += (NFSX_STATEIDOTHER / NFSX_UNSIGNED);
4377 		NFSD_DEBUG(4, "aft args\n");
4378 	} else if (procnum == NFSV4PROC_CBNULL) {
4379 		nd->nd_procnum = NFSV4PROC_CBNULL;
4380 		if ((clp->lc_flags & LCL_NFSV41) != 0) {
4381 			error = nfsv4_getcbsession(clp, &sep);
4382 			if (error != 0) {
4383 				m_freem(nd->nd_mreq);
4384 				goto errout;
4385 			}
4386 		}
4387 	} else {
4388 		error = NFSERR_SERVERFAULT;
4389 		m_freem(nd->nd_mreq);
4390 		goto errout;
4391 	}
4392 
4393 	/*
4394 	 * Call newnfs_connect(), as required, and then newnfs_request().
4395 	 */
4396 	dotls = false;
4397 	if ((clp->lc_flags & LCL_TLSCB) != 0)
4398 		dotls = true;
4399 	(void) newnfs_sndlock(&clp->lc_req.nr_lock);
4400 	if (clp->lc_req.nr_client == NULL) {
4401 		if ((clp->lc_flags & LCL_NFSV41) != 0) {
4402 			error = ECONNREFUSED;
4403 			if (procnum != NFSV4PROC_CBNULL)
4404 				nfsv4_freeslot(&sep->sess_cbsess, slotpos,
4405 				    true);
4406 			nfsrv_freesession(NULL, sep, NULL, false, NULL);
4407 		} else if (nd->nd_procnum == NFSV4PROC_CBNULL)
4408 			error = newnfs_connect(NULL, &clp->lc_req, cred,
4409 			    NULL, 1, dotls, &clp->lc_req.nr_client);
4410 		else
4411 			error = newnfs_connect(NULL, &clp->lc_req, cred,
4412 			    NULL, 3, dotls, &clp->lc_req.nr_client);
4413 	}
4414 	newnfs_sndunlock(&clp->lc_req.nr_lock);
4415 	NFSD_DEBUG(4, "aft sndunlock=%d\n", error);
4416 	if (!error) {
4417 		if ((nd->nd_flag & ND_NFSV41) != 0) {
4418 			KASSERT(sep != NULL, ("sep NULL"));
4419 			if (sep->sess_cbsess.nfsess_xprt != NULL)
4420 				error = newnfs_request(nd, NULL, clp,
4421 				    &clp->lc_req, NULL, NULL, cred,
4422 				    clp->lc_program, clp->lc_req.nr_vers, NULL,
4423 				    1, NULL, &sep->sess_cbsess);
4424 			else {
4425 				/*
4426 				 * This should probably never occur, but if a
4427 				 * client somehow does an RPC without a
4428 				 * SequenceID Op that causes a callback just
4429 				 * after the nfsd threads have been terminated
4430 				 * and restarted we could conceivably get here
4431 				 * without a backchannel xprt.
4432 				 */
4433 				printf("nfsrv_docallback: no xprt\n");
4434 				error = ECONNREFUSED;
4435 			}
4436 			NFSD_DEBUG(4, "aft newnfs_request=%d\n", error);
4437 			if (error != 0 && procnum != NFSV4PROC_CBNULL) {
4438 				/*
4439 				 * It is likely that the callback was never
4440 				 * processed by the client and, as such,
4441 				 * the sequence# for the session slot needs
4442 				 * to be backed up by one to avoid a
4443 				 * NFSERR_SEQMISORDERED error reply.
4444 				 * For the unlikely case where the callback
4445 				 * was processed by the client, this will
4446 				 * make the next callback on the slot
4447 				 * appear to be a retry.
4448 				 * Since callbacks never specify that the
4449 				 * reply be cached, this "apparent retry"
4450 				 * should not be a problem.
4451 				 */
4452 				nfsv4_freeslot(&sep->sess_cbsess, slotpos,
4453 				    true);
4454 			}
4455 			nfsrv_freesession(NULL, sep, NULL, false, NULL);
4456 		} else
4457 			error = newnfs_request(nd, NULL, clp, &clp->lc_req,
4458 			    NULL, NULL, cred, clp->lc_program,
4459 			    clp->lc_req.nr_vers, NULL, 1, NULL, NULL);
4460 	}
4461 errout:
4462 	NFSFREECRED(cred);
4463 
4464 	/*
4465 	 * If error is set here, the Callback path isn't working
4466 	 * properly, so twiddle the appropriate LCL_ flags.
4467 	 * (nd_repstat != 0 indicates the Callback path is working,
4468 	 *  but the callback failed on the client.)
4469 	 */
4470 	if (error) {
4471 		/*
4472 		 * Mark the callback pathway down, which disabled issuing
4473 		 * of delegations and gets Renew to return NFSERR_CBPATHDOWN.
4474 		 */
4475 		NFSLOCKSTATE();
4476 		clp->lc_flags |= LCL_CBDOWN;
4477 		NFSUNLOCKSTATE();
4478 	} else {
4479 		/*
4480 		 * Callback worked. If the callback path was down, disable
4481 		 * callbacks, so no more delegations will be issued. (This
4482 		 * is done on the assumption that the callback pathway is
4483 		 * flakey.)
4484 		 */
4485 		NFSLOCKSTATE();
4486 		if (clp->lc_flags & LCL_CBDOWN)
4487 			clp->lc_flags &= ~(LCL_CBDOWN | LCL_CALLBACKSON);
4488 		NFSUNLOCKSTATE();
4489 		if (nd->nd_repstat) {
4490 			error = nd->nd_repstat;
4491 			NFSD_DEBUG(1, "nfsrv_docallback op=%d err=%d\n",
4492 			    procnum, error);
4493 		} else if (error == 0 && procnum == NFSV4OP_CBGETATTR)
4494 			error = nfsv4_loadattr(nd, NULL, nap, NULL, NULL, 0,
4495 			    NULL, NULL, NULL, NULL, NULL, 0, NULL, NULL, NULL,
4496 			    NULL, NULL, NULL, p, NULL);
4497 		m_freem(nd->nd_mrep);
4498 	}
4499 	NFSLOCKSTATE();
4500 	clp->lc_cbref--;
4501 	if ((clp->lc_flags & LCL_WAKEUPWANTED) && clp->lc_cbref == 0) {
4502 		clp->lc_flags &= ~LCL_WAKEUPWANTED;
4503 		wakeup(clp);
4504 	}
4505 	NFSUNLOCKSTATE();
4506 
4507 	free(nd, M_TEMP);
4508 	NFSEXITCODE(error);
4509 	return (error);
4510 }
4511 
4512 /*
4513  * Set up the compound RPC for the callback.
4514  */
4515 static int
nfsrv_cbcallargs(struct nfsrv_descript * nd,struct nfsclient * clp,uint32_t callback,int op,const char * optag,struct nfsdsession ** sepp,int * slotposp)4516 nfsrv_cbcallargs(struct nfsrv_descript *nd, struct nfsclient *clp,
4517     uint32_t callback, int op, const char *optag, struct nfsdsession **sepp,
4518     int *slotposp)
4519 {
4520 	uint32_t *tl;
4521 	int error, len;
4522 
4523 	len = strlen(optag);
4524 	(void)nfsm_strtom(nd, optag, len);
4525 	NFSM_BUILD(tl, uint32_t *, 4 * NFSX_UNSIGNED);
4526 	if ((nd->nd_flag & ND_NFSV41) != 0) {
4527 		if ((nd->nd_flag & ND_NFSV42) != 0)
4528 			*tl++ = txdr_unsigned(NFSV42_MINORVERSION);
4529 		else
4530 			*tl++ = txdr_unsigned(NFSV41_MINORVERSION);
4531 		*tl++ = txdr_unsigned(callback);
4532 		*tl++ = txdr_unsigned(2);
4533 		*tl = txdr_unsigned(NFSV4OP_CBSEQUENCE);
4534 		error = nfsv4_setcbsequence(nd, clp, 1, sepp, slotposp);
4535 		if (error != 0)
4536 			return (error);
4537 		NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
4538 		*tl = txdr_unsigned(op);
4539 	} else {
4540 		*tl++ = txdr_unsigned(NFSV4_MINORVERSION);
4541 		*tl++ = txdr_unsigned(callback);
4542 		*tl++ = txdr_unsigned(1);
4543 		*tl = txdr_unsigned(op);
4544 	}
4545 	return (0);
4546 }
4547 
4548 /*
4549  * Return the next index# for a clientid. Mostly just increment and return
4550  * the next one, but... if the 32bit unsigned does actually wrap around,
4551  * it should be rebooted.
4552  * At an average rate of one new client per second, it will wrap around in
4553  * approximately 136 years. (I think the server will have been shut
4554  * down or rebooted before then.)
4555  */
4556 static u_int32_t
nfsrv_nextclientindex(void)4557 nfsrv_nextclientindex(void)
4558 {
4559 	static u_int32_t client_index = 0;
4560 
4561 	client_index++;
4562 	if (client_index != 0)
4563 		return (client_index);
4564 
4565 	printf("%s: out of clientids\n", __func__);
4566 	return (client_index);
4567 }
4568 
4569 /*
4570  * Return the next index# for a stateid. Mostly just increment and return
4571  * the next one, but... if the 32bit unsigned does actually wrap around
4572  * (will a BSD server stay up that long?), find
4573  * new start and end values.
4574  */
4575 static u_int32_t
nfsrv_nextstateindex(struct nfsclient * clp)4576 nfsrv_nextstateindex(struct nfsclient *clp)
4577 {
4578 	struct nfsstate *stp;
4579 	int i;
4580 	u_int32_t canuse, min_index, max_index;
4581 
4582 	if (!(clp->lc_flags & LCL_INDEXNOTOK)) {
4583 		clp->lc_stateindex++;
4584 		if (clp->lc_stateindex != clp->lc_statemaxindex)
4585 			return (clp->lc_stateindex);
4586 	}
4587 
4588 	/*
4589 	 * Yuck, we've hit the end.
4590 	 * Look for a new min and max.
4591 	 */
4592 	min_index = 0;
4593 	max_index = 0xffffffff;
4594 	for (i = 0; i < nfsrv_statehashsize; i++) {
4595 	    LIST_FOREACH(stp, &clp->lc_stateid[i], ls_hash) {
4596 		if (stp->ls_stateid.other[2] > 0x80000000) {
4597 		    if (stp->ls_stateid.other[2] < max_index)
4598 			max_index = stp->ls_stateid.other[2];
4599 		} else {
4600 		    if (stp->ls_stateid.other[2] > min_index)
4601 			min_index = stp->ls_stateid.other[2];
4602 		}
4603 	    }
4604 	}
4605 
4606 	/*
4607 	 * Yikes, highly unlikely, but I'll handle it anyhow.
4608 	 */
4609 	if (min_index == 0x80000000 && max_index == 0x80000001) {
4610 	    canuse = 0;
4611 	    /*
4612 	     * Loop around until we find an unused entry. Return that
4613 	     * and set LCL_INDEXNOTOK, so the search will continue next time.
4614 	     * (This is one of those rare cases where a goto is the
4615 	     *  cleanest way to code the loop.)
4616 	     */
4617 tryagain:
4618 	    for (i = 0; i < nfsrv_statehashsize; i++) {
4619 		LIST_FOREACH(stp, &clp->lc_stateid[i], ls_hash) {
4620 		    if (stp->ls_stateid.other[2] == canuse) {
4621 			canuse++;
4622 			goto tryagain;
4623 		    }
4624 		}
4625 	    }
4626 	    clp->lc_flags |= LCL_INDEXNOTOK;
4627 	    return (canuse);
4628 	}
4629 
4630 	/*
4631 	 * Ok to start again from min + 1.
4632 	 */
4633 	clp->lc_stateindex = min_index + 1;
4634 	clp->lc_statemaxindex = max_index;
4635 	clp->lc_flags &= ~LCL_INDEXNOTOK;
4636 	return (clp->lc_stateindex);
4637 }
4638 
4639 /*
4640  * The following functions handle the stable storage file that deals with
4641  * the edge conditions described in RFC3530 Sec. 8.6.3.
4642  * The file is as follows:
4643  * - a single record at the beginning that has the lease time of the
4644  *   previous server instance (before the last reboot) and the nfsrvboottime
4645  *   values for the previous server boots.
4646  *   These previous boot times are used to ensure that the current
4647  *   nfsrvboottime does not, somehow, get set to a previous one.
4648  *   (This is important so that Stale ClientIDs and StateIDs can
4649  *    be recognized.)
4650  *   The number of previous nfsvrboottime values precedes the list.
4651  * - followed by some number of appended records with:
4652  *   - client id string
4653  *   - flag that indicates it is a record revoking state via lease
4654  *     expiration or similar
4655  *     OR has successfully acquired state.
4656  * These structures vary in length, with the client string at the end, up
4657  * to NFSV4_OPAQUELIMIT in size.
4658  *
4659  * At the end of the grace period, the file is truncated, the first
4660  * record is rewritten with updated information and any acquired state
4661  * records for successful reclaims of state are written.
4662  *
4663  * Subsequent records are appended when the first state is issued to
4664  * a client and when state is revoked for a client.
4665  *
4666  * When reading the file in, state issued records that come later in
4667  * the file override older ones, since the append log is in cronological order.
4668  * If, for some reason, the file can't be read, the grace period is
4669  * immediately terminated and all reclaims get NFSERR_NOGRACE.
4670  */
4671 
4672 /*
4673  * Read in the stable storage file. Called by nfssvc() before the nfsd
4674  * processes start servicing requests.
4675  */
4676 void
nfsrv_setupstable(NFSPROC_T * p)4677 nfsrv_setupstable(NFSPROC_T *p)
4678 {
4679 	struct nfsrv_stablefirst *sf = &VNET(nfsrv_stablefirst);
4680 	struct nfsrv_stable *sp, *nsp;
4681 	struct nfst_rec *tsp;
4682 	int error, i, tryagain;
4683 	off_t off = 0;
4684 	ssize_t aresid, len;
4685 
4686 	/*
4687 	 * If NFSNSF_UPDATEDONE is set, this is a restart of the nfsds without
4688 	 * a reboot, so state has not been lost.
4689 	 */
4690 	if (sf->nsf_flags & NFSNSF_UPDATEDONE)
4691 		return;
4692 	/*
4693 	 * Set Grace over just until the file reads successfully.
4694 	 */
4695 	VNET(nfsrvboottime) = time_second;
4696 	LIST_INIT(&sf->nsf_head);
4697 	sf->nsf_flags = (NFSNSF_GRACEOVER | NFSNSF_NEEDLOCK);
4698 	sf->nsf_eograce = NFSD_MONOSEC + NFSRV_LEASEDELTA;
4699 	if (sf->nsf_fp == NULL)
4700 		return;
4701 	error = NFSD_RDWR(UIO_READ, NFSFPVNODE(sf->nsf_fp),
4702 	    (caddr_t)&sf->nsf_rec, sizeof (struct nfsf_rec), off, UIO_SYSSPACE,
4703 	    0, NFSFPCRED(sf->nsf_fp), &aresid, p);
4704 	if (error || aresid || sf->nsf_numboots == 0 ||
4705 		sf->nsf_numboots > NFSNSF_MAXNUMBOOTS)
4706 		return;
4707 
4708 	/*
4709 	 * Now, read in the boottimes.
4710 	 */
4711 	sf->nsf_bootvals = (time_t *)malloc((sf->nsf_numboots + 1) *
4712 		sizeof(time_t), M_TEMP, M_WAITOK);
4713 	off = sizeof (struct nfsf_rec);
4714 	error = NFSD_RDWR(UIO_READ, NFSFPVNODE(sf->nsf_fp),
4715 	    (caddr_t)sf->nsf_bootvals, sf->nsf_numboots * sizeof (time_t), off,
4716 	    UIO_SYSSPACE, 0, NFSFPCRED(sf->nsf_fp), &aresid, p);
4717 	if (error || aresid) {
4718 		free(sf->nsf_bootvals, M_TEMP);
4719 		sf->nsf_bootvals = NULL;
4720 		return;
4721 	}
4722 
4723 	/*
4724 	 * Make sure this nfsrvboottime is different from all recorded
4725 	 * previous ones.
4726 	 */
4727 	do {
4728 		tryagain = 0;
4729 		for (i = 0; i < sf->nsf_numboots; i++) {
4730 			if (VNET(nfsrvboottime) == sf->nsf_bootvals[i]) {
4731 				VNET(nfsrvboottime)++;
4732 				tryagain = 1;
4733 				break;
4734 			}
4735 		}
4736 	} while (tryagain);
4737 
4738 	sf->nsf_flags |= NFSNSF_OK;
4739 	off += (sf->nsf_numboots * sizeof (time_t));
4740 
4741 	/*
4742 	 * Read through the file, building a list of records for grace
4743 	 * checking.
4744 	 * Each record is between sizeof (struct nfst_rec) and
4745 	 * sizeof (struct nfst_rec) + NFSV4_OPAQUELIMIT - 1
4746 	 * and is actually sizeof (struct nfst_rec) + nst_len - 1.
4747 	 */
4748 	tsp = (struct nfst_rec *)malloc(sizeof (struct nfst_rec) +
4749 		NFSV4_OPAQUELIMIT - 1, M_TEMP, M_WAITOK);
4750 	do {
4751 	    error = NFSD_RDWR(UIO_READ, NFSFPVNODE(sf->nsf_fp),
4752 	        (caddr_t)tsp, sizeof (struct nfst_rec) + NFSV4_OPAQUELIMIT - 1,
4753 	        off, UIO_SYSSPACE, 0, NFSFPCRED(sf->nsf_fp), &aresid, p);
4754 	    len = (sizeof (struct nfst_rec) + NFSV4_OPAQUELIMIT - 1) - aresid;
4755 	    if (error || (len > 0 && (len < sizeof (struct nfst_rec) ||
4756 		len < (sizeof (struct nfst_rec) + tsp->len - 1)))) {
4757 		/*
4758 		 * Yuck, the file has been corrupted, so just return
4759 		 * after clearing out any restart state, so the grace period
4760 		 * is over.
4761 		 */
4762 		LIST_FOREACH_SAFE(sp, &sf->nsf_head, nst_list, nsp) {
4763 			LIST_REMOVE(sp, nst_list);
4764 			free(sp, M_TEMP);
4765 		}
4766 		free(tsp, M_TEMP);
4767 		sf->nsf_flags &= ~NFSNSF_OK;
4768 		free(sf->nsf_bootvals, M_TEMP);
4769 		sf->nsf_bootvals = NULL;
4770 		return;
4771 	    }
4772 	    if (len > 0) {
4773 		off += sizeof (struct nfst_rec) + tsp->len - 1;
4774 		/*
4775 		 * Search the list for a matching client.
4776 		 */
4777 		LIST_FOREACH(sp, &sf->nsf_head, nst_list) {
4778 			if (tsp->len == sp->nst_len &&
4779 			    !NFSBCMP(tsp->client, sp->nst_client, tsp->len))
4780 				break;
4781 		}
4782 		if (sp == LIST_END(&sf->nsf_head)) {
4783 			sp = (struct nfsrv_stable *)malloc(tsp->len +
4784 				sizeof (struct nfsrv_stable) - 1, M_TEMP,
4785 				M_WAITOK);
4786 			NFSBCOPY((caddr_t)tsp, (caddr_t)&sp->nst_rec,
4787 				sizeof (struct nfst_rec) + tsp->len - 1);
4788 			LIST_INSERT_HEAD(&sf->nsf_head, sp, nst_list);
4789 		} else {
4790 			if (tsp->flag == NFSNST_REVOKE)
4791 				sp->nst_flag |= NFSNST_REVOKE;
4792 			else
4793 				/*
4794 				 * A subsequent timestamp indicates the client
4795 				 * did a setclientid/confirm and any previous
4796 				 * revoke is no longer relevant.
4797 				 */
4798 				sp->nst_flag &= ~NFSNST_REVOKE;
4799 		}
4800 	    }
4801 	} while (len > 0);
4802 	free(tsp, M_TEMP);
4803 	sf->nsf_flags = NFSNSF_OK;
4804 	sf->nsf_eograce = NFSD_MONOSEC + sf->nsf_lease +
4805 		NFSRV_LEASEDELTA;
4806 }
4807 
4808 /*
4809  * Update the stable storage file, now that the grace period is over.
4810  */
4811 void
nfsrv_updatestable(NFSPROC_T * p)4812 nfsrv_updatestable(NFSPROC_T *p)
4813 {
4814 	struct nfsrv_stablefirst *sf = &VNET(nfsrv_stablefirst);
4815 	struct nfsrv_stable *sp, *nsp;
4816 	int i;
4817 	struct nfsvattr nva;
4818 	vnode_t vp;
4819 #if defined(__FreeBSD_version) && (__FreeBSD_version >= 500000)
4820 	mount_t mp = NULL;
4821 #endif
4822 	int error;
4823 
4824 	if (sf->nsf_fp == NULL || (sf->nsf_flags & NFSNSF_UPDATEDONE))
4825 		return;
4826 	sf->nsf_flags |= NFSNSF_UPDATEDONE;
4827 	/*
4828 	 * Ok, we need to rewrite the stable storage file.
4829 	 * - truncate to 0 length
4830 	 * - write the new first structure
4831 	 * - loop through the data structures, writing out any that
4832 	 *   have timestamps older than the old boot
4833 	 */
4834 	if (sf->nsf_bootvals) {
4835 		sf->nsf_numboots++;
4836 		for (i = sf->nsf_numboots - 2; i >= 0; i--)
4837 			sf->nsf_bootvals[i + 1] = sf->nsf_bootvals[i];
4838 	} else {
4839 		sf->nsf_numboots = 1;
4840 		sf->nsf_bootvals = (time_t *)malloc(sizeof(time_t),
4841 			M_TEMP, M_WAITOK);
4842 	}
4843 	sf->nsf_bootvals[0] = VNET(nfsrvboottime);
4844 	sf->nsf_lease = nfsrv_lease;
4845 	NFSVNO_ATTRINIT(&nva);
4846 	NFSVNO_SETATTRVAL(&nva, size, 0);
4847 	vp = NFSFPVNODE(sf->nsf_fp);
4848 	vn_start_write(vp, &mp, V_WAIT);
4849 	if (NFSVOPLOCK(vp, LK_EXCLUSIVE) == 0) {
4850 		error = nfsvno_setattr(vp, &nva, NFSFPCRED(sf->nsf_fp), p,
4851 		    NULL);
4852 		NFSVOPUNLOCK(vp);
4853 	} else
4854 		error = EPERM;
4855 	vn_finished_write(mp);
4856 	if (!error)
4857 	    error = NFSD_RDWR(UIO_WRITE, vp,
4858 		(caddr_t)&sf->nsf_rec, sizeof (struct nfsf_rec), (off_t)0,
4859 		UIO_SYSSPACE, IO_SYNC, NFSFPCRED(sf->nsf_fp), NULL, p);
4860 	if (!error)
4861 	    error = NFSD_RDWR(UIO_WRITE, vp,
4862 		(caddr_t)sf->nsf_bootvals,
4863 		sf->nsf_numboots * sizeof (time_t),
4864 		(off_t)(sizeof (struct nfsf_rec)),
4865 		UIO_SYSSPACE, IO_SYNC, NFSFPCRED(sf->nsf_fp), NULL, p);
4866 	free(sf->nsf_bootvals, M_TEMP);
4867 	sf->nsf_bootvals = NULL;
4868 	if (error) {
4869 		sf->nsf_flags &= ~NFSNSF_OK;
4870 		printf("EEK! Can't write NfsV4 stable storage file\n");
4871 		return;
4872 	}
4873 	sf->nsf_flags |= NFSNSF_OK;
4874 
4875 	/*
4876 	 * Loop through the list and write out timestamp records for
4877 	 * any clients that successfully reclaimed state.
4878 	 */
4879 	LIST_FOREACH_SAFE(sp, &sf->nsf_head, nst_list, nsp) {
4880 		if (sp->nst_flag & NFSNST_GOTSTATE) {
4881 			nfsrv_writestable(sp->nst_client, sp->nst_len,
4882 				NFSNST_NEWSTATE, p);
4883 			sp->nst_clp->lc_flags |= LCL_STAMPEDSTABLE;
4884 		}
4885 		LIST_REMOVE(sp, nst_list);
4886 		free(sp, M_TEMP);
4887 	}
4888 	nfsrv_backupstable();
4889 }
4890 
4891 /*
4892  * Append a record to the stable storage file.
4893  */
4894 void
nfsrv_writestable(u_char * client,int len,int flag,NFSPROC_T * p)4895 nfsrv_writestable(u_char *client, int len, int flag, NFSPROC_T *p)
4896 {
4897 	struct nfsrv_stablefirst *sf = &VNET(nfsrv_stablefirst);
4898 	struct nfst_rec *sp;
4899 	int error;
4900 
4901 	if (!(sf->nsf_flags & NFSNSF_OK) || sf->nsf_fp == NULL)
4902 		return;
4903 	sp = (struct nfst_rec *)malloc(sizeof (struct nfst_rec) +
4904 		len - 1, M_TEMP, M_WAITOK);
4905 	sp->len = len;
4906 	NFSBCOPY(client, sp->client, len);
4907 	sp->flag = flag;
4908 	error = NFSD_RDWR(UIO_WRITE, NFSFPVNODE(sf->nsf_fp),
4909 	    (caddr_t)sp, sizeof (struct nfst_rec) + len - 1, (off_t)0,
4910 	    UIO_SYSSPACE, (IO_SYNC | IO_APPEND), NFSFPCRED(sf->nsf_fp), NULL, p);
4911 	free(sp, M_TEMP);
4912 	if (error) {
4913 		sf->nsf_flags &= ~NFSNSF_OK;
4914 		printf("EEK! Can't write NfsV4 stable storage file\n");
4915 	}
4916 }
4917 
4918 /*
4919  * This function is called during the grace period to mark a client
4920  * that successfully reclaimed state.
4921  */
4922 static void
nfsrv_markstable(struct nfsclient * clp)4923 nfsrv_markstable(struct nfsclient *clp)
4924 {
4925 	struct nfsrv_stable *sp;
4926 
4927 	/*
4928 	 * First find the client structure.
4929 	 */
4930 	LIST_FOREACH(sp, &VNET(nfsrv_stablefirst).nsf_head, nst_list) {
4931 		if (sp->nst_len == clp->lc_idlen &&
4932 		    !NFSBCMP(sp->nst_client, clp->lc_id, sp->nst_len))
4933 			break;
4934 	}
4935 	if (sp == LIST_END(&VNET(nfsrv_stablefirst).nsf_head))
4936 		return;
4937 
4938 	/*
4939 	 * Now, just mark it and set the nfsclient back pointer.
4940 	 */
4941 	sp->nst_flag |= NFSNST_GOTSTATE;
4942 	sp->nst_clp = clp;
4943 }
4944 
4945 /*
4946  * This function is called when a NFSv4.1 client does a ReclaimComplete.
4947  * Very similar to nfsrv_markstable(), except for the flag being set.
4948  */
4949 static void
nfsrv_markreclaim(struct nfsclient * clp)4950 nfsrv_markreclaim(struct nfsclient *clp)
4951 {
4952 	struct nfsrv_stable *sp;
4953 
4954 	/*
4955 	 * First find the client structure.
4956 	 */
4957 	LIST_FOREACH(sp, &VNET(nfsrv_stablefirst).nsf_head, nst_list) {
4958 		if (sp->nst_len == clp->lc_idlen &&
4959 		    !NFSBCMP(sp->nst_client, clp->lc_id, sp->nst_len))
4960 			break;
4961 	}
4962 	if (sp == LIST_END(&VNET(nfsrv_stablefirst).nsf_head))
4963 		return;
4964 
4965 	/*
4966 	 * Now, just set the flag.
4967 	 */
4968 	sp->nst_flag |= NFSNST_RECLAIMED;
4969 
4970 	/*
4971 	 * Free up any old delegations.
4972 	 */
4973 	nfsrv_freedeleglist(&clp->lc_olddeleg);
4974 }
4975 
4976 /*
4977  * This function is called for a reclaim, to see if it gets grace.
4978  * It returns 0 if a reclaim is allowed, 1 otherwise.
4979  */
4980 static int
nfsrv_checkstable(struct nfsclient * clp)4981 nfsrv_checkstable(struct nfsclient *clp)
4982 {
4983 	struct nfsrv_stable *sp;
4984 
4985 	/*
4986 	 * First, find the entry for the client.
4987 	 */
4988 	LIST_FOREACH(sp, &VNET(nfsrv_stablefirst).nsf_head, nst_list) {
4989 		if (sp->nst_len == clp->lc_idlen &&
4990 		    !NFSBCMP(sp->nst_client, clp->lc_id, sp->nst_len))
4991 			break;
4992 	}
4993 
4994 	/*
4995 	 * If not in the list, state was revoked or no state was issued
4996 	 * since the previous reboot, a reclaim is denied.
4997 	 */
4998 	if (sp == LIST_END(&VNET(nfsrv_stablefirst).nsf_head) ||
4999 	    (sp->nst_flag & NFSNST_REVOKE) ||
5000 	    !(VNET(nfsrv_stablefirst).nsf_flags & NFSNSF_OK))
5001 		return (1);
5002 	return (0);
5003 }
5004 
5005 /*
5006  * Test for and try to clear out a conflicting client. This is called by
5007  * nfsrv_lockctrl() and nfsrv_openctrl() when conflicts with other clients
5008  * a found.
5009  * The trick here is that it can't revoke a conflicting client with an
5010  * expired lease unless it holds the v4root lock, so...
5011  * If no v4root lock, get the lock and return 1 to indicate "try again".
5012  * Return 0 to indicate the conflict can't be revoked and 1 to indicate
5013  * the revocation worked and the conflicting client is "bye, bye", so it
5014  * can be tried again.
5015  * Return 2 to indicate that the vnode is VIRF_DOOMED after NFSVOPLOCK().
5016  * Unlocks State before a non-zero value is returned.
5017  */
5018 static int
nfsrv_clientconflict(struct nfsclient * clp,int * haslockp,vnode_t vp,NFSPROC_T * p)5019 nfsrv_clientconflict(struct nfsclient *clp, int *haslockp, vnode_t vp,
5020     NFSPROC_T *p)
5021 {
5022 	int gotlock, lktype = 0;
5023 
5024 	/*
5025 	 * If lease hasn't expired, we can't fix it.
5026 	 */
5027 	if (clp->lc_expiry >= NFSD_MONOSEC ||
5028 	    !(VNET(nfsrv_stablefirst).nsf_flags & NFSNSF_UPDATEDONE))
5029 		return (0);
5030 	if (*haslockp == 0) {
5031 		NFSUNLOCKSTATE();
5032 		if (vp != NULL) {
5033 			lktype = NFSVOPISLOCKED(vp);
5034 			NFSVOPUNLOCK(vp);
5035 		}
5036 		NFSLOCKV4ROOTMUTEX();
5037 		nfsv4_relref(&nfsv4rootfs_lock);
5038 		do {
5039 			gotlock = nfsv4_lock(&nfsv4rootfs_lock, 1, NULL,
5040 			    NFSV4ROOTLOCKMUTEXPTR, NULL);
5041 		} while (!gotlock);
5042 		NFSUNLOCKV4ROOTMUTEX();
5043 		*haslockp = 1;
5044 		if (vp != NULL) {
5045 			NFSVOPLOCK(vp, lktype | LK_RETRY);
5046 			if (VN_IS_DOOMED(vp))
5047 				return (2);
5048 		}
5049 		return (1);
5050 	}
5051 	NFSUNLOCKSTATE();
5052 
5053 	/*
5054 	 * Ok, we can expire the conflicting client.
5055 	 */
5056 	nfsrv_writestable(clp->lc_id, clp->lc_idlen, NFSNST_REVOKE, p);
5057 	nfsrv_backupstable();
5058 	nfsrv_cleanclient(clp, p, false, NULL);
5059 	nfsrv_freedeleglist(&clp->lc_deleg);
5060 	nfsrv_freedeleglist(&clp->lc_olddeleg);
5061 	LIST_REMOVE(clp, lc_hash);
5062 	nfsrv_zapclient(clp, p);
5063 	return (1);
5064 }
5065 
5066 /*
5067  * Resolve a delegation conflict.
5068  * Returns 0 to indicate the conflict was resolved without sleeping.
5069  * Return -1 to indicate that the caller should check for conflicts again.
5070  * Return > 0 for an error that should be returned, normally NFSERR_DELAY.
5071  *
5072  * Also, manipulate the nfsv4root_lock, as required. It isn't changed
5073  * for a return of 0, since there was no sleep and it could be required
5074  * later. It is released for a return of NFSERR_DELAY, since the caller
5075  * will return that error. It is released when a sleep was done waiting
5076  * for the delegation to be returned or expire (so that other nfsds can
5077  * handle ops). Then, it must be acquired for the write to stable storage.
5078  * (This function is somewhat similar to nfsrv_clientconflict(), but
5079  *  the semantics differ in a couple of subtle ways. The return of 0
5080  *  indicates the conflict was resolved without sleeping here, not
5081  *  that the conflict can't be resolved and the handling of nfsv4root_lock
5082  *  differs, as noted above.)
5083  * Unlocks State before returning a non-zero value.
5084  */
5085 static int
nfsrv_delegconflict(struct nfsstate * stp,int * haslockp,NFSPROC_T * p,vnode_t vp)5086 nfsrv_delegconflict(struct nfsstate *stp, int *haslockp, NFSPROC_T *p,
5087     vnode_t vp)
5088 {
5089 	struct nfsclient *clp = stp->ls_clp;
5090 	int gotlock, error, lktype = 0, retrycnt, zapped_clp;
5091 	nfsv4stateid_t tstateid;
5092 	fhandle_t tfh;
5093 
5094 	/*
5095 	 * If the conflict is with an old delegation...
5096 	 */
5097 	if (stp->ls_flags & NFSLCK_OLDDELEG) {
5098 		/*
5099 		 * You can delete it, if it has expired.
5100 		 */
5101 		if (clp->lc_delegtime < NFSD_MONOSEC) {
5102 			nfsrv_freedeleg(stp);
5103 			NFSUNLOCKSTATE();
5104 			error = -1;
5105 			goto out;
5106 		}
5107 		NFSUNLOCKSTATE();
5108 		/*
5109 		 * During this delay, the old delegation could expire or it
5110 		 * could be recovered by the client via an Open with
5111 		 * CLAIM_DELEGATE_PREV.
5112 		 * Release the nfsv4root_lock, if held.
5113 		 */
5114 		if (*haslockp) {
5115 			*haslockp = 0;
5116 			NFSLOCKV4ROOTMUTEX();
5117 			nfsv4_unlock(&nfsv4rootfs_lock, 1);
5118 			NFSUNLOCKV4ROOTMUTEX();
5119 		}
5120 		error = NFSERR_DELAY;
5121 		goto out;
5122 	}
5123 
5124 	/*
5125 	 * It's a current delegation, so:
5126 	 * - check to see if the delegation has expired
5127 	 *   - if so, get the v4root lock and then expire it
5128 	 */
5129 	if ((stp->ls_flags & NFSLCK_DELEGRECALL) == 0 || (stp->ls_lastrecall <
5130 	    NFSD_MONOSEC && clp->lc_expiry >= NFSD_MONOSEC &&
5131 	    stp->ls_delegtime >= NFSD_MONOSEC)) {
5132 		/*
5133 		 * - do a recall callback, since not yet done
5134 		 * For now, never allow truncate to be set. To use
5135 		 * truncate safely, it must be guaranteed that the
5136 		 * Remove, Rename or Setattr with size of 0 will
5137 		 * succeed and that would require major changes to
5138 		 * the VFS/Vnode OPs.
5139 		 * Set the expiry time large enough so that it won't expire
5140 		 * until after the callback, then set it correctly, once
5141 		 * the callback is done. (The delegation will now time
5142 		 * out whether or not the Recall worked ok. The timeout
5143 		 * will be extended when ops are done on the delegation
5144 		 * stateid, up to the timelimit.)
5145 		 */
5146 		if ((stp->ls_flags & NFSLCK_DELEGRECALL) == 0) {
5147 			stp->ls_delegtime = NFSD_MONOSEC + (2 * nfsrv_lease) +
5148 			    NFSRV_LEASEDELTA;
5149 			stp->ls_delegtimelimit = NFSD_MONOSEC + (6 *
5150 			    nfsrv_lease) + NFSRV_LEASEDELTA;
5151 			stp->ls_flags |= NFSLCK_DELEGRECALL;
5152 		}
5153 		stp->ls_lastrecall = time_uptime + 1;
5154 
5155 		/*
5156 		 * Loop NFSRV_CBRETRYCNT times while the CBRecall replies
5157 		 * NFSERR_BADSTATEID or NFSERR_BADHANDLE. This is done
5158 		 * in order to try and avoid a race that could happen
5159 		 * when a CBRecall request passed the Open reply with
5160 		 * the delegation in it when transitting the network.
5161 		 * Since nfsrv_docallback will sleep, don't use stp after
5162 		 * the call.
5163 		 */
5164 		NFSBCOPY((caddr_t)&stp->ls_stateid, (caddr_t)&tstateid,
5165 		    sizeof (tstateid));
5166 		NFSBCOPY((caddr_t)&stp->ls_lfp->lf_fh, (caddr_t)&tfh,
5167 		    sizeof (tfh));
5168 		NFSUNLOCKSTATE();
5169 		if (*haslockp) {
5170 			*haslockp = 0;
5171 			NFSLOCKV4ROOTMUTEX();
5172 			nfsv4_unlock(&nfsv4rootfs_lock, 1);
5173 			NFSUNLOCKV4ROOTMUTEX();
5174 		}
5175 		retrycnt = 0;
5176 		do {
5177 		    error = nfsrv_docallback(clp, NFSV4OP_CBRECALL,
5178 			&tstateid, 0, &tfh, NULL, NULL, 0, p);
5179 		    retrycnt++;
5180 		} while ((error == NFSERR_BADSTATEID ||
5181 		    error == NFSERR_BADHANDLE) && retrycnt < NFSV4_CBRETRYCNT);
5182 		error = NFSERR_DELAY;
5183 		goto out;
5184 	}
5185 
5186 	if (clp->lc_expiry >= NFSD_MONOSEC &&
5187 	    stp->ls_delegtime >= NFSD_MONOSEC) {
5188 		NFSUNLOCKSTATE();
5189 		/*
5190 		 * A recall has been done, but it has not yet expired.
5191 		 * So, RETURN_DELAY.
5192 		 */
5193 		if (*haslockp) {
5194 			*haslockp = 0;
5195 			NFSLOCKV4ROOTMUTEX();
5196 			nfsv4_unlock(&nfsv4rootfs_lock, 1);
5197 			NFSUNLOCKV4ROOTMUTEX();
5198 		}
5199 		error = NFSERR_DELAY;
5200 		goto out;
5201 	}
5202 
5203 	/*
5204 	 * If we don't yet have the lock, just get it and then return,
5205 	 * since we need that before deleting expired state, such as
5206 	 * this delegation.
5207 	 * When getting the lock, unlock the vnode, so other nfsds that
5208 	 * are in progress, won't get stuck waiting for the vnode lock.
5209 	 */
5210 	if (*haslockp == 0) {
5211 		NFSUNLOCKSTATE();
5212 		if (vp != NULL) {
5213 			lktype = NFSVOPISLOCKED(vp);
5214 			NFSVOPUNLOCK(vp);
5215 		}
5216 		NFSLOCKV4ROOTMUTEX();
5217 		nfsv4_relref(&nfsv4rootfs_lock);
5218 		do {
5219 			gotlock = nfsv4_lock(&nfsv4rootfs_lock, 1, NULL,
5220 			    NFSV4ROOTLOCKMUTEXPTR, NULL);
5221 		} while (!gotlock);
5222 		NFSUNLOCKV4ROOTMUTEX();
5223 		*haslockp = 1;
5224 		if (vp != NULL) {
5225 			NFSVOPLOCK(vp, lktype | LK_RETRY);
5226 			if (VN_IS_DOOMED(vp)) {
5227 				*haslockp = 0;
5228 				NFSLOCKV4ROOTMUTEX();
5229 				nfsv4_unlock(&nfsv4rootfs_lock, 1);
5230 				NFSUNLOCKV4ROOTMUTEX();
5231 				error = NFSERR_PERM;
5232 				goto out;
5233 			}
5234 		}
5235 		error = -1;
5236 		goto out;
5237 	}
5238 
5239 	NFSUNLOCKSTATE();
5240 	/*
5241 	 * Ok, we can delete the expired delegation.
5242 	 * First, write the Revoke record to stable storage and then
5243 	 * clear out the conflict.
5244 	 * Since all other nfsd threads are now blocked, we can safely
5245 	 * sleep without the state changing.
5246 	 */
5247 	nfsrv_writestable(clp->lc_id, clp->lc_idlen, NFSNST_REVOKE, p);
5248 	nfsrv_backupstable();
5249 	if (clp->lc_expiry < NFSD_MONOSEC) {
5250 		nfsrv_cleanclient(clp, p, false, NULL);
5251 		nfsrv_freedeleglist(&clp->lc_deleg);
5252 		nfsrv_freedeleglist(&clp->lc_olddeleg);
5253 		LIST_REMOVE(clp, lc_hash);
5254 		zapped_clp = 1;
5255 	} else {
5256 		nfsrv_freedeleg(stp);
5257 		zapped_clp = 0;
5258 	}
5259 	if (zapped_clp)
5260 		nfsrv_zapclient(clp, p);
5261 	error = -1;
5262 
5263 out:
5264 	NFSEXITCODE(error);
5265 	return (error);
5266 }
5267 
5268 /*
5269  * Check for a remove allowed, if remove is set to 1 and get rid of
5270  * delegations.
5271  */
5272 int
nfsrv_checkremove(vnode_t vp,int remove,struct nfsrv_descript * nd,nfsquad_t clientid,NFSPROC_T * p)5273 nfsrv_checkremove(vnode_t vp, int remove, struct nfsrv_descript *nd,
5274     nfsquad_t clientid, NFSPROC_T *p)
5275 {
5276 	struct nfsclient *clp;
5277 	struct nfsstate *stp;
5278 	struct nfslockfile *lfp;
5279 	int error, haslock = 0;
5280 	fhandle_t nfh;
5281 
5282 	clp = NULL;
5283 	/*
5284 	 * First, get the lock file structure.
5285 	 * (A return of -1 means no associated state, so remove ok.)
5286 	 */
5287 	error = nfsrv_getlockfh(vp, NFSLCK_CHECK, NULL, &nfh, p);
5288 tryagain:
5289 	NFSLOCKSTATE();
5290 	if (error == 0 && clientid.qval != 0)
5291 		error = nfsrv_getclient(clientid, CLOPS_RENEW, &clp, NULL,
5292 		    (nfsquad_t)((u_quad_t)0), 0, nd, p);
5293 	if (!error)
5294 		error = nfsrv_getlockfile(NFSLCK_CHECK, NULL, &lfp, &nfh, 0);
5295 	if (error) {
5296 		NFSUNLOCKSTATE();
5297 		if (haslock) {
5298 			NFSLOCKV4ROOTMUTEX();
5299 			nfsv4_unlock(&nfsv4rootfs_lock, 1);
5300 			NFSUNLOCKV4ROOTMUTEX();
5301 		}
5302 		if (error == -1)
5303 			error = 0;
5304 		goto out;
5305 	}
5306 
5307 	/*
5308 	 * Now, we must Recall any delegations.
5309 	 */
5310 	error = nfsrv_cleandeleg(vp, lfp, clp, &haslock, p);
5311 	if (error) {
5312 		/*
5313 		 * nfsrv_cleandeleg() unlocks state for non-zero
5314 		 * return.
5315 		 */
5316 		if (error == -1)
5317 			goto tryagain;
5318 		if (haslock) {
5319 			NFSLOCKV4ROOTMUTEX();
5320 			nfsv4_unlock(&nfsv4rootfs_lock, 1);
5321 			NFSUNLOCKV4ROOTMUTEX();
5322 		}
5323 		goto out;
5324 	}
5325 
5326 	/*
5327 	 * Now, look for a conflicting open share.
5328 	 */
5329 	if (remove) {
5330 		/*
5331 		 * If the entry in the directory was the last reference to the
5332 		 * corresponding filesystem object, the object can be destroyed
5333 		 * */
5334 		if(lfp->lf_usecount>1)
5335 			LIST_FOREACH(stp, &lfp->lf_open, ls_file) {
5336 				if (stp->ls_flags & NFSLCK_WRITEDENY) {
5337 					error = NFSERR_FILEOPEN;
5338 					break;
5339 				}
5340 			}
5341 	}
5342 
5343 	NFSUNLOCKSTATE();
5344 	if (haslock) {
5345 		NFSLOCKV4ROOTMUTEX();
5346 		nfsv4_unlock(&nfsv4rootfs_lock, 1);
5347 		NFSUNLOCKV4ROOTMUTEX();
5348 	}
5349 
5350 out:
5351 	NFSEXITCODE(error);
5352 	return (error);
5353 }
5354 
5355 /*
5356  * Clear out all delegations for the file referred to by lfp.
5357  * May return NFSERR_DELAY, if there will be a delay waiting for
5358  * delegations to expire.
5359  * Returns -1 to indicate it slept while recalling a delegation.
5360  * This function has the side effect of deleting the nfslockfile structure,
5361  * if it no longer has associated state and didn't have to sleep.
5362  * Unlocks State before a non-zero value is returned.
5363  */
5364 static int
nfsrv_cleandeleg(vnode_t vp,struct nfslockfile * lfp,struct nfsclient * clp,int * haslockp,NFSPROC_T * p)5365 nfsrv_cleandeleg(vnode_t vp, struct nfslockfile *lfp,
5366     struct nfsclient *clp, int *haslockp, NFSPROC_T *p)
5367 {
5368 	struct nfsstate *stp, *nstp;
5369 	int ret = 0;
5370 
5371 	stp = LIST_FIRST(&lfp->lf_deleg);
5372 	while (stp != LIST_END(&lfp->lf_deleg)) {
5373 		nstp = LIST_NEXT(stp, ls_file);
5374 		if (stp->ls_clp != clp) {
5375 			ret = nfsrv_delegconflict(stp, haslockp, p, vp);
5376 			if (ret) {
5377 				/*
5378 				 * nfsrv_delegconflict() unlocks state
5379 				 * when it returns non-zero.
5380 				 */
5381 				goto out;
5382 			}
5383 		}
5384 		stp = nstp;
5385 	}
5386 out:
5387 	NFSEXITCODE(ret);
5388 	return (ret);
5389 }
5390 
5391 /*
5392  * There are certain operations that, when being done outside of NFSv4,
5393  * require that any NFSv4 delegation for the file be recalled.
5394  * This function is to be called for those cases:
5395  * VOP_RENAME() - When a delegation is being recalled for any reason,
5396  *	the client may have to do Opens against the server, using the file's
5397  *	final component name. If the file has been renamed on the server,
5398  *	that component name will be incorrect and the Open will fail.
5399  * VOP_REMOVE() - Theoretically, a client could Open a file after it has
5400  *	been removed on the server, if there is a delegation issued to
5401  *	that client for the file. I say "theoretically" since clients
5402  *	normally do an Access Op before the Open and that Access Op will
5403  *	fail with ESTALE. Note that NFSv2 and 3 don't even do Opens, so
5404  *	they will detect the file's removal in the same manner. (There is
5405  *	one case where RFC3530 allows a client to do an Open without first
5406  *	doing an Access Op, which is passage of a check against the ACE
5407  *	returned with a Write delegation, but current practice is to ignore
5408  *	the ACE and always do an Access Op.)
5409  *	Since the functions can only be called with an unlocked vnode, this
5410  *	can't be done at this time.
5411  * VOP_ADVLOCK() - When a client holds a delegation, it can issue byte range
5412  *	locks locally in the client, which are not visible to the server. To
5413  *	deal with this, issuing of delegations for a vnode must be disabled
5414  *	and all delegations for the vnode recalled. This is done via the
5415  *	second function, using the VV_DISABLEDELEG vflag on the vnode.
5416  */
5417 void
nfsd_recalldelegation(vnode_t vp,NFSPROC_T * p)5418 nfsd_recalldelegation(vnode_t vp, NFSPROC_T *p)
5419 {
5420 	time_t starttime;
5421 	int error;
5422 
5423 	/*
5424 	 * First, check to see if the server is currently running and it has
5425 	 * been called for a regular file when issuing delegations.
5426 	 */
5427 	if (VNET(nfsrv_numnfsd) == 0 || vp->v_type != VREG ||
5428 	    nfsrv_issuedelegs == 0)
5429 		return;
5430 
5431 	KASSERT((NFSVOPISLOCKED(vp) != LK_EXCLUSIVE), ("vp %p is locked", vp));
5432 	/*
5433 	 * First, get a reference on the nfsv4rootfs_lock so that an
5434 	 * exclusive lock cannot be acquired by another thread.
5435 	 */
5436 	NFSLOCKV4ROOTMUTEX();
5437 	nfsv4_getref(&nfsv4rootfs_lock, NULL, NFSV4ROOTLOCKMUTEXPTR, NULL);
5438 	NFSUNLOCKV4ROOTMUTEX();
5439 
5440 	/*
5441 	 * Now, call nfsrv_checkremove() in a loop while it returns
5442 	 * NFSERR_DELAY. Return upon any other error or when timed out.
5443 	 */
5444 	starttime = NFSD_MONOSEC;
5445 	do {
5446 		if (NFSVOPLOCK(vp, LK_EXCLUSIVE) == 0) {
5447 			error = nfsrv_checkremove(vp, 0, NULL,
5448 			    (nfsquad_t)((u_quad_t)0), p);
5449 			NFSVOPUNLOCK(vp);
5450 		} else
5451 			error = EPERM;
5452 		if (error == NFSERR_DELAY) {
5453 			if (NFSD_MONOSEC - starttime > NFS_REMOVETIMEO)
5454 				break;
5455 			/* Sleep for a short period of time */
5456 			(void) nfs_catnap(PZERO, 0, "nfsremove");
5457 		}
5458 	} while (error == NFSERR_DELAY);
5459 	NFSLOCKV4ROOTMUTEX();
5460 	nfsv4_relref(&nfsv4rootfs_lock);
5461 	NFSUNLOCKV4ROOTMUTEX();
5462 }
5463 
5464 void
nfsd_disabledelegation(vnode_t vp,NFSPROC_T * p)5465 nfsd_disabledelegation(vnode_t vp, NFSPROC_T *p)
5466 {
5467 
5468 #ifdef VV_DISABLEDELEG
5469 	/*
5470 	 * First, flag issuance of delegations disabled.
5471 	 */
5472 	atomic_set_long(&vp->v_vflag, VV_DISABLEDELEG);
5473 #endif
5474 
5475 	/*
5476 	 * Then call nfsd_recalldelegation() to get rid of all extant
5477 	 * delegations.
5478 	 */
5479 	nfsd_recalldelegation(vp, p);
5480 }
5481 
5482 /*
5483  * Check for conflicting locks, etc. and then get rid of delegations.
5484  * (At one point I thought that I should get rid of delegations for any
5485  *  Setattr, since it could potentially disallow the I/O op (read or write)
5486  *  allowed by the delegation. However, Setattr Ops that aren't changing
5487  *  the size get a stateid of all 0s, so you can't tell if it is a delegation
5488  *  for the same client or a different one, so I decided to only get rid
5489  *  of delegations for other clients when the size is being changed.)
5490  * In general, a Setattr can disable NFS I/O Ops that are outstanding, such
5491  * as Write backs, even if there is no delegation, so it really isn't any
5492  * different?)
5493  */
5494 int
nfsrv_checksetattr(vnode_t vp,struct nfsrv_descript * nd,nfsv4stateid_t * stateidp,struct nfsvattr * nvap,nfsattrbit_t * attrbitp,struct nfsexstuff * exp,NFSPROC_T * p)5495 nfsrv_checksetattr(vnode_t vp, struct nfsrv_descript *nd,
5496     nfsv4stateid_t *stateidp, struct nfsvattr *nvap, nfsattrbit_t *attrbitp,
5497     struct nfsexstuff *exp, NFSPROC_T *p)
5498 {
5499 	struct nfsstate st, *stp = &st;
5500 	struct nfslock lo, *lop = &lo;
5501 	int error = 0;
5502 	nfsquad_t clientid;
5503 
5504 	if (NFSISSET_ATTRBIT(attrbitp, NFSATTRBIT_SIZE)) {
5505 		stp->ls_flags = (NFSLCK_CHECK | NFSLCK_WRITEACCESS);
5506 		lop->lo_first = nvap->na_size;
5507 	} else {
5508 		stp->ls_flags = 0;
5509 		lop->lo_first = 0;
5510 	}
5511 	if (NFSISSET_ATTRBIT(attrbitp, NFSATTRBIT_OWNER) ||
5512 	    NFSISSET_ATTRBIT(attrbitp, NFSATTRBIT_OWNERGROUP) ||
5513 	    NFSISSET_ATTRBIT(attrbitp, NFSATTRBIT_MODE) ||
5514 	    NFSISSET_ATTRBIT(attrbitp, NFSATTRBIT_ACL))
5515 		stp->ls_flags |= NFSLCK_SETATTR;
5516 	if (stp->ls_flags == 0)
5517 		goto out;
5518 	lop->lo_end = NFS64BITSSET;
5519 	lop->lo_flags = NFSLCK_WRITE;
5520 	stp->ls_ownerlen = 0;
5521 	stp->ls_op = NULL;
5522 	stp->ls_uid = nd->nd_cred->cr_uid;
5523 	stp->ls_stateid.seqid = stateidp->seqid;
5524 	clientid.lval[0] = stp->ls_stateid.other[0] = stateidp->other[0];
5525 	clientid.lval[1] = stp->ls_stateid.other[1] = stateidp->other[1];
5526 	stp->ls_stateid.other[2] = stateidp->other[2];
5527 	error = nfsrv_lockctrl(vp, &stp, &lop, NULL, clientid,
5528 	    stateidp, exp, nd, p);
5529 
5530 out:
5531 	NFSEXITCODE2(error, nd);
5532 	return (error);
5533 }
5534 
5535 /*
5536  * Check for a write delegation and do a CBGETATTR if there is one, updating
5537  * the attributes, as required.
5538  * Should I return an error if I can't get the attributes? (For now, I'll
5539  * just return ok.
5540  */
5541 int
nfsrv_checkgetattr(struct nfsrv_descript * nd,vnode_t vp,struct nfsvattr * nvap,nfsattrbit_t * attrbitp,NFSPROC_T * p)5542 nfsrv_checkgetattr(struct nfsrv_descript *nd, vnode_t vp,
5543     struct nfsvattr *nvap, nfsattrbit_t *attrbitp, NFSPROC_T *p)
5544 {
5545 	struct nfsstate *stp;
5546 	struct nfslockfile *lfp;
5547 	struct nfsclient *clp;
5548 	struct nfsvattr nva;
5549 	fhandle_t nfh;
5550 	int error = 0;
5551 	nfsattrbit_t cbbits;
5552 	u_quad_t delegfilerev;
5553 
5554 	NFSCBGETATTR_ATTRBIT(attrbitp, &cbbits);
5555 	if (!NFSNONZERO_ATTRBIT(&cbbits))
5556 		goto out;
5557 	if (nfsrv_writedelegcnt == 0)
5558 		goto out;
5559 
5560 	/*
5561 	 * Get the lock file structure.
5562 	 * (A return of -1 means no associated state, so return ok.)
5563 	 */
5564 	error = nfsrv_getlockfh(vp, NFSLCK_CHECK, NULL, &nfh, p);
5565 	NFSLOCKSTATE();
5566 	if (!error)
5567 		error = nfsrv_getlockfile(NFSLCK_CHECK, NULL, &lfp, &nfh, 0);
5568 	if (error) {
5569 		NFSUNLOCKSTATE();
5570 		if (error == -1)
5571 			error = 0;
5572 		goto out;
5573 	}
5574 
5575 	/*
5576 	 * Now, look for a write delegation.
5577 	 */
5578 	LIST_FOREACH(stp, &lfp->lf_deleg, ls_file) {
5579 		if (stp->ls_flags & NFSLCK_DELEGWRITE)
5580 			break;
5581 	}
5582 	if (stp == LIST_END(&lfp->lf_deleg)) {
5583 		NFSUNLOCKSTATE();
5584 		goto out;
5585 	}
5586 	clp = stp->ls_clp;
5587 
5588 	/* If the clientid is not confirmed, ignore the delegation. */
5589 	if (clp->lc_flags & LCL_NEEDSCONFIRM) {
5590 		NFSUNLOCKSTATE();
5591 		goto out;
5592 	}
5593 
5594 	delegfilerev = stp->ls_filerev;
5595 	/*
5596 	 * If the Write delegation was issued as a part of this Compound RPC
5597 	 * or if we have an Implied Clientid (used in a previous Op in this
5598 	 * compound) and it is the client the delegation was issued to,
5599 	 * just return ok.
5600 	 * I also assume that it is from the same client iff the network
5601 	 * host IP address is the same as the callback address. (Not
5602 	 * exactly correct by the RFC, but avoids a lot of Getattr
5603 	 * callbacks.)
5604 	 */
5605 	if (nd->nd_compref == stp->ls_compref ||
5606 	    ((nd->nd_flag & ND_IMPLIEDCLID) &&
5607 	     clp->lc_clientid.qval == nd->nd_clientid.qval) ||
5608 	     nfsaddr2_match(clp->lc_req.nr_nam, nd->nd_nam)) {
5609 		NFSUNLOCKSTATE();
5610 		goto out;
5611 	}
5612 
5613 	/*
5614 	 * We are now done with the delegation state structure,
5615 	 * so the statelock can be released and we can now tsleep().
5616 	 */
5617 
5618 	/*
5619 	 * Now, we must do the CB Getattr callback, to see if Change or Size
5620 	 * has changed.
5621 	 */
5622 	if (clp->lc_expiry >= NFSD_MONOSEC) {
5623 		NFSUNLOCKSTATE();
5624 		NFSVNO_ATTRINIT(&nva);
5625 		nva.na_filerev = NFS64BITSSET;
5626 		error = nfsrv_docallback(clp, NFSV4OP_CBGETATTR, NULL,
5627 		    0, &nfh, &nva, &cbbits, 0, p);
5628 		if (!error) {
5629 			if ((nva.na_filerev != NFS64BITSSET &&
5630 			    nva.na_filerev > delegfilerev) ||
5631 			    (NFSVNO_ISSETSIZE(&nva) &&
5632 			     nva.na_size != nvap->na_size)) {
5633 				error = nfsvno_updfilerev(vp, nvap, nd, p);
5634 				if (NFSVNO_ISSETSIZE(&nva))
5635 					nvap->na_size = nva.na_size;
5636 			}
5637 		} else
5638 			error = 0;	/* Ignore callback errors for now. */
5639 	} else {
5640 		NFSUNLOCKSTATE();
5641 	}
5642 
5643 out:
5644 	NFSEXITCODE2(error, nd);
5645 	return (error);
5646 }
5647 
5648 /*
5649  * This function looks for openowners that haven't had any opens for
5650  * a while and throws them away. Called by an nfsd when NFSNSF_NOOPENS
5651  * is set.
5652  */
5653 void
nfsrv_throwawayopens(NFSPROC_T * p)5654 nfsrv_throwawayopens(NFSPROC_T *p)
5655 {
5656 	struct nfsclient *clp, *nclp;
5657 	struct nfsstate *stp, *nstp;
5658 	int i;
5659 
5660 	NFSLOCKSTATE();
5661 	VNET(nfsrv_stablefirst).nsf_flags &= ~NFSNSF_NOOPENS;
5662 	/*
5663 	 * For each client...
5664 	 */
5665 	for (i = 0; i < nfsrv_clienthashsize; i++) {
5666 	    LIST_FOREACH_SAFE(clp, &VNET(nfsclienthash)[i], lc_hash,
5667 		nclp) {
5668 		LIST_FOREACH_SAFE(stp, &clp->lc_open, ls_list, nstp) {
5669 			if (LIST_EMPTY(&stp->ls_open) &&
5670 			    (stp->ls_noopens > NFSNOOPEN ||
5671 			     (nfsrv_openpluslock * 2) >
5672 			     nfsrv_v4statelimit))
5673 				nfsrv_freeopenowner(stp, 0, p);
5674 		}
5675 	    }
5676 	}
5677 	NFSUNLOCKSTATE();
5678 }
5679 
5680 /*
5681  * This function checks to see if the credentials are the same.
5682  * The check for same credentials is needed for state management operations
5683  * for NFSv4.0 or NFSv4.1/4.2 when SP4_MACH_CRED is configured via
5684  * ExchangeID.
5685  * Returns 1 for not same, 0 otherwise.
5686  */
5687 static int
nfsrv_notsamecredname(int op,struct nfsrv_descript * nd,struct nfsclient * clp)5688 nfsrv_notsamecredname(int op, struct nfsrv_descript *nd, struct nfsclient *clp)
5689 {
5690 
5691 	/* Check for the SP4_MACH_CRED case. */
5692 	if (op != 0 && nfsrv_checkmachcred(op, nd, clp) != 0)
5693 		return (1);
5694 
5695 	/* For NFSv4.1/4.2, SP4_NONE always allows this. */
5696 	if ((nd->nd_flag & ND_NFSV41) != 0)
5697 		return (0);
5698 
5699 	if (nd->nd_flag & ND_GSS) {
5700 		if (!(clp->lc_flags & LCL_GSS))
5701 			return (1);
5702 		if (clp->lc_flags & LCL_NAME) {
5703 			if (nd->nd_princlen != clp->lc_namelen ||
5704 			    NFSBCMP(nd->nd_principal, clp->lc_name,
5705 				clp->lc_namelen))
5706 				return (1);
5707 			else
5708 				return (0);
5709 		}
5710 		if (nd->nd_cred->cr_uid == clp->lc_uid)
5711 			return (0);
5712 		else
5713 			return (1);
5714 	} else if (clp->lc_flags & LCL_GSS)
5715 		return (1);
5716 	/*
5717 	 * For AUTH_SYS, allow the same uid or root. (This is underspecified
5718 	 * in RFC3530, which talks about principals, but doesn't say anything
5719 	 * about uids for AUTH_SYS.)
5720 	 */
5721 	if (nd->nd_cred->cr_uid == clp->lc_uid || nd->nd_cred->cr_uid == 0)
5722 		return (0);
5723 	else
5724 		return (1);
5725 }
5726 
5727 /*
5728  * Calculate the lease expiry time.
5729  */
5730 static time_t
nfsrv_leaseexpiry(void)5731 nfsrv_leaseexpiry(void)
5732 {
5733 
5734 	if (VNET(nfsrv_stablefirst).nsf_eograce > NFSD_MONOSEC)
5735 		return (NFSD_MONOSEC + 2 * (nfsrv_lease + NFSRV_LEASEDELTA));
5736 	return (NFSD_MONOSEC + nfsrv_lease + NFSRV_LEASEDELTA);
5737 }
5738 
5739 /*
5740  * Delay the delegation timeout as far as ls_delegtimelimit, as required.
5741  */
5742 static void
nfsrv_delaydelegtimeout(struct nfsstate * stp)5743 nfsrv_delaydelegtimeout(struct nfsstate *stp)
5744 {
5745 
5746 	if ((stp->ls_flags & NFSLCK_DELEGRECALL) == 0)
5747 		return;
5748 
5749 	if ((stp->ls_delegtime + 15) > NFSD_MONOSEC &&
5750 	    stp->ls_delegtime < stp->ls_delegtimelimit) {
5751 		stp->ls_delegtime += nfsrv_lease;
5752 		if (stp->ls_delegtime > stp->ls_delegtimelimit)
5753 			stp->ls_delegtime = stp->ls_delegtimelimit;
5754 	}
5755 }
5756 
5757 /*
5758  * This function checks to see if there is any other state associated
5759  * with the openowner for this Open.
5760  * It returns 1 if there is no other state, 0 otherwise.
5761  */
5762 static int
nfsrv_nootherstate(struct nfsstate * stp)5763 nfsrv_nootherstate(struct nfsstate *stp)
5764 {
5765 	struct nfsstate *tstp;
5766 
5767 	LIST_FOREACH(tstp, &stp->ls_openowner->ls_open, ls_list) {
5768 		if (tstp != stp || !LIST_EMPTY(&tstp->ls_lock))
5769 			return (0);
5770 	}
5771 	return (1);
5772 }
5773 
5774 /*
5775  * Create a list of lock deltas (changes to local byte range locking
5776  * that can be rolled back using the list) and apply the changes via
5777  * nfsvno_advlock(). Optionally, lock the list. It is expected that either
5778  * the rollback or update function will be called after this.
5779  * It returns an error (and rolls back, as required), if any nfsvno_advlock()
5780  * call fails. If it returns an error, it will unlock the list.
5781  */
5782 static int
nfsrv_locallock(vnode_t vp,struct nfslockfile * lfp,int flags,uint64_t first,uint64_t end,struct nfslockconflict * cfp,NFSPROC_T * p)5783 nfsrv_locallock(vnode_t vp, struct nfslockfile *lfp, int flags,
5784     uint64_t first, uint64_t end, struct nfslockconflict *cfp, NFSPROC_T *p)
5785 {
5786 	struct nfslock *lop, *nlop;
5787 	int error = 0;
5788 
5789 	/* Loop through the list of locks. */
5790 	lop = LIST_FIRST(&lfp->lf_locallock);
5791 	while (first < end && lop != NULL) {
5792 		nlop = LIST_NEXT(lop, lo_lckowner);
5793 		if (first >= lop->lo_end) {
5794 			/* not there yet */
5795 			lop = nlop;
5796 		} else if (first < lop->lo_first) {
5797 			/* new one starts before entry in list */
5798 			if (end <= lop->lo_first) {
5799 				/* no overlap between old and new */
5800 				error = nfsrv_dolocal(vp, lfp, flags,
5801 				    NFSLCK_UNLOCK, first, end, cfp, p);
5802 				if (error != 0)
5803 					break;
5804 				first = end;
5805 			} else {
5806 				/* handle fragment overlapped with new one */
5807 				error = nfsrv_dolocal(vp, lfp, flags,
5808 				    NFSLCK_UNLOCK, first, lop->lo_first, cfp,
5809 				    p);
5810 				if (error != 0)
5811 					break;
5812 				first = lop->lo_first;
5813 			}
5814 		} else {
5815 			/* new one overlaps this entry in list */
5816 			if (end <= lop->lo_end) {
5817 				/* overlaps all of new one */
5818 				error = nfsrv_dolocal(vp, lfp, flags,
5819 				    lop->lo_flags, first, end, cfp, p);
5820 				if (error != 0)
5821 					break;
5822 				first = end;
5823 			} else {
5824 				/* handle fragment overlapped with new one */
5825 				error = nfsrv_dolocal(vp, lfp, flags,
5826 				    lop->lo_flags, first, lop->lo_end, cfp, p);
5827 				if (error != 0)
5828 					break;
5829 				first = lop->lo_end;
5830 				lop = nlop;
5831 			}
5832 		}
5833 	}
5834 	if (first < end && error == 0)
5835 		/* handle fragment past end of list */
5836 		error = nfsrv_dolocal(vp, lfp, flags, NFSLCK_UNLOCK, first,
5837 		    end, cfp, p);
5838 
5839 	NFSEXITCODE(error);
5840 	return (error);
5841 }
5842 
5843 /*
5844  * Local lock unlock. Unlock all byte ranges that are no longer locked
5845  * by NFSv4. To do this, unlock any subranges of first-->end that
5846  * do not overlap with the byte ranges of any lock in the lfp->lf_lock
5847  * list. This list has all locks for the file held by other
5848  * <clientid, lockowner> tuples. The list is ordered by increasing
5849  * lo_first value, but may have entries that overlap each other, for
5850  * the case of read locks.
5851  */
5852 static void
nfsrv_localunlock(vnode_t vp,struct nfslockfile * lfp,uint64_t init_first,uint64_t init_end,NFSPROC_T * p)5853 nfsrv_localunlock(vnode_t vp, struct nfslockfile *lfp, uint64_t init_first,
5854     uint64_t init_end, NFSPROC_T *p)
5855 {
5856 	struct nfslock *lop;
5857 	uint64_t first, end, prevfirst __unused;
5858 
5859 	first = init_first;
5860 	end = init_end;
5861 	while (first < init_end) {
5862 		/* Loop through all nfs locks, adjusting first and end */
5863 		prevfirst = 0;
5864 		LIST_FOREACH(lop, &lfp->lf_lock, lo_lckfile) {
5865 			KASSERT(prevfirst <= lop->lo_first,
5866 			    ("nfsv4 locks out of order"));
5867 			KASSERT(lop->lo_first < lop->lo_end,
5868 			    ("nfsv4 bogus lock"));
5869 			prevfirst = lop->lo_first;
5870 			if (first >= lop->lo_first &&
5871 			    first < lop->lo_end)
5872 				/*
5873 				 * Overlaps with initial part, so trim
5874 				 * off that initial part by moving first past
5875 				 * it.
5876 				 */
5877 				first = lop->lo_end;
5878 			else if (end > lop->lo_first &&
5879 			    lop->lo_first > first) {
5880 				/*
5881 				 * This lock defines the end of the
5882 				 * segment to unlock, so set end to the
5883 				 * start of it and break out of the loop.
5884 				 */
5885 				end = lop->lo_first;
5886 				break;
5887 			}
5888 			if (first >= end)
5889 				/*
5890 				 * There is no segment left to do, so
5891 				 * break out of this loop and then exit
5892 				 * the outer while() since first will be set
5893 				 * to end, which must equal init_end here.
5894 				 */
5895 				break;
5896 		}
5897 		if (first < end) {
5898 			/* Unlock this segment */
5899 			(void) nfsrv_dolocal(vp, lfp, NFSLCK_UNLOCK,
5900 			    NFSLCK_READ, first, end, NULL, p);
5901 			nfsrv_locallock_commit(lfp, NFSLCK_UNLOCK,
5902 			    first, end);
5903 		}
5904 		/*
5905 		 * Now move past this segment and look for any further
5906 		 * segment in the range, if there is one.
5907 		 */
5908 		first = end;
5909 		end = init_end;
5910 	}
5911 }
5912 
5913 /*
5914  * Do the local lock operation and update the rollback list, as required.
5915  * Perform the rollback and return the error if nfsvno_advlock() fails.
5916  */
5917 static int
nfsrv_dolocal(vnode_t vp,struct nfslockfile * lfp,int flags,int oldflags,uint64_t first,uint64_t end,struct nfslockconflict * cfp,NFSPROC_T * p)5918 nfsrv_dolocal(vnode_t vp, struct nfslockfile *lfp, int flags, int oldflags,
5919     uint64_t first, uint64_t end, struct nfslockconflict *cfp, NFSPROC_T *p)
5920 {
5921 	struct nfsrollback *rlp;
5922 	int error = 0, ltype, oldltype;
5923 
5924 	if (flags & NFSLCK_WRITE)
5925 		ltype = F_WRLCK;
5926 	else if (flags & NFSLCK_READ)
5927 		ltype = F_RDLCK;
5928 	else
5929 		ltype = F_UNLCK;
5930 	if (oldflags & NFSLCK_WRITE)
5931 		oldltype = F_WRLCK;
5932 	else if (oldflags & NFSLCK_READ)
5933 		oldltype = F_RDLCK;
5934 	else
5935 		oldltype = F_UNLCK;
5936 	if (ltype == oldltype || (oldltype == F_WRLCK && ltype == F_RDLCK))
5937 		/* nothing to do */
5938 		goto out;
5939 	error = nfsvno_advlock(vp, ltype, first, end, p);
5940 	if (error != 0) {
5941 		if (cfp != NULL) {
5942 			cfp->cl_clientid.lval[0] = 0;
5943 			cfp->cl_clientid.lval[1] = 0;
5944 			cfp->cl_first = 0;
5945 			cfp->cl_end = NFS64BITSSET;
5946 			cfp->cl_flags = NFSLCK_WRITE;
5947 			cfp->cl_ownerlen = 5;
5948 			NFSBCOPY("LOCAL", cfp->cl_owner, 5);
5949 		}
5950 		nfsrv_locallock_rollback(vp, lfp, p);
5951 	} else if (ltype != F_UNLCK) {
5952 		rlp = malloc(sizeof (struct nfsrollback), M_NFSDROLLBACK,
5953 		    M_WAITOK);
5954 		rlp->rlck_first = first;
5955 		rlp->rlck_end = end;
5956 		rlp->rlck_type = oldltype;
5957 		LIST_INSERT_HEAD(&lfp->lf_rollback, rlp, rlck_list);
5958 	}
5959 
5960 out:
5961 	NFSEXITCODE(error);
5962 	return (error);
5963 }
5964 
5965 /*
5966  * Roll back local lock changes and free up the rollback list.
5967  */
5968 static void
nfsrv_locallock_rollback(vnode_t vp,struct nfslockfile * lfp,NFSPROC_T * p)5969 nfsrv_locallock_rollback(vnode_t vp, struct nfslockfile *lfp, NFSPROC_T *p)
5970 {
5971 	struct nfsrollback *rlp, *nrlp;
5972 
5973 	LIST_FOREACH_SAFE(rlp, &lfp->lf_rollback, rlck_list, nrlp) {
5974 		(void) nfsvno_advlock(vp, rlp->rlck_type, rlp->rlck_first,
5975 		    rlp->rlck_end, p);
5976 		free(rlp, M_NFSDROLLBACK);
5977 	}
5978 	LIST_INIT(&lfp->lf_rollback);
5979 }
5980 
5981 /*
5982  * Update local lock list and delete rollback list (ie now committed to the
5983  * local locks). Most of the work is done by the internal function.
5984  */
5985 static void
nfsrv_locallock_commit(struct nfslockfile * lfp,int flags,uint64_t first,uint64_t end)5986 nfsrv_locallock_commit(struct nfslockfile *lfp, int flags, uint64_t first,
5987     uint64_t end)
5988 {
5989 	struct nfsrollback *rlp, *nrlp;
5990 	struct nfslock *new_lop, *other_lop;
5991 
5992 	new_lop = malloc(sizeof (struct nfslock), M_NFSDLOCK, M_WAITOK);
5993 	if (flags & (NFSLCK_READ | NFSLCK_WRITE))
5994 		other_lop = malloc(sizeof (struct nfslock), M_NFSDLOCK,
5995 		    M_WAITOK);
5996 	else
5997 		other_lop = NULL;
5998 	new_lop->lo_flags = flags;
5999 	new_lop->lo_first = first;
6000 	new_lop->lo_end = end;
6001 	nfsrv_updatelock(NULL, &new_lop, &other_lop, lfp);
6002 	if (new_lop != NULL)
6003 		free(new_lop, M_NFSDLOCK);
6004 	if (other_lop != NULL)
6005 		free(other_lop, M_NFSDLOCK);
6006 
6007 	/* and get rid of the rollback list */
6008 	LIST_FOREACH_SAFE(rlp, &lfp->lf_rollback, rlck_list, nrlp)
6009 		free(rlp, M_NFSDROLLBACK);
6010 	LIST_INIT(&lfp->lf_rollback);
6011 }
6012 
6013 /*
6014  * Lock the struct nfslockfile for local lock updating.
6015  */
6016 static void
nfsrv_locklf(struct nfslockfile * lfp)6017 nfsrv_locklf(struct nfslockfile *lfp)
6018 {
6019 	int gotlock;
6020 
6021 	/* lf_usecount ensures *lfp won't be free'd */
6022 	lfp->lf_usecount++;
6023 	do {
6024 		gotlock = nfsv4_lock(&lfp->lf_locallock_lck, 1, NULL,
6025 		    NFSSTATEMUTEXPTR, NULL);
6026 	} while (gotlock == 0);
6027 	lfp->lf_usecount--;
6028 }
6029 
6030 /*
6031  * Unlock the struct nfslockfile after local lock updating.
6032  */
6033 static void
nfsrv_unlocklf(struct nfslockfile * lfp)6034 nfsrv_unlocklf(struct nfslockfile *lfp)
6035 {
6036 
6037 	nfsv4_unlock(&lfp->lf_locallock_lck, 0);
6038 }
6039 
6040 /*
6041  * Clear out all state for the NFSv4 server.
6042  * Must be called by a thread that can sleep when no nfsds are running.
6043  */
6044 void
nfsrv_throwawayallstate(NFSPROC_T * p)6045 nfsrv_throwawayallstate(NFSPROC_T *p)
6046 {
6047 	struct nfsclient *clp, *nclp;
6048 	struct nfslockfile *lfp, *nlfp;
6049 	int i;
6050 
6051 	/*
6052 	 * For each client, clean out the state and then free the structure.
6053 	 */
6054 	for (i = 0; i < nfsrv_clienthashsize; i++) {
6055 		LIST_FOREACH_SAFE(clp, &VNET(nfsclienthash)[i], lc_hash,
6056 		    nclp) {
6057 			nfsrv_cleanclient(clp, p, false, NULL);
6058 			nfsrv_freedeleglist(&clp->lc_deleg);
6059 			nfsrv_freedeleglist(&clp->lc_olddeleg);
6060 			free(clp->lc_stateid, M_NFSDCLIENT);
6061 			free(clp, M_NFSDCLIENT);
6062 		}
6063 	}
6064 
6065 	/*
6066 	 * Also, free up any remaining lock file structures.
6067 	 */
6068 	for (i = 0; i < nfsrv_lockhashsize; i++) {
6069 		LIST_FOREACH_SAFE(lfp, &VNET(nfslockhash)[i], lf_hash,
6070 		    nlfp) {
6071 			printf("nfsd unload: fnd a lock file struct\n");
6072 			nfsrv_freenfslockfile(lfp);
6073 		}
6074 	}
6075 
6076 	/* And get rid of the deviceid structures and layouts. */
6077 	nfsrv_freealllayoutsanddevids();
6078 }
6079 
6080 /*
6081  * Check the sequence# for the session and slot provided as an argument.
6082  * Also, renew the lease if the session will return NFS_OK.
6083  */
6084 int
nfsrv_checksequence(struct nfsrv_descript * nd,uint32_t sequenceid,uint32_t * highest_slotidp,uint32_t * target_highest_slotidp,int cache_this,uint32_t * sflagsp,NFSPROC_T * p)6085 nfsrv_checksequence(struct nfsrv_descript *nd, uint32_t sequenceid,
6086     uint32_t *highest_slotidp, uint32_t *target_highest_slotidp, int cache_this,
6087     uint32_t *sflagsp, NFSPROC_T *p)
6088 {
6089 	struct nfsdsession *sep;
6090 	struct nfssessionhash *shp;
6091 	int error;
6092 
6093 	shp = NFSSESSIONHASH(nd->nd_sessionid);
6094 	NFSLOCKSESSION(shp);
6095 	sep = nfsrv_findsession(nd->nd_sessionid);
6096 	if (sep == NULL) {
6097 		NFSUNLOCKSESSION(shp);
6098 		return (NFSERR_BADSESSION);
6099 	}
6100 	error = nfsv4_seqsession(sequenceid, nd->nd_slotid, *highest_slotidp,
6101 	    sep->sess_slots, NULL, NFSV4_SLOTS - 1);
6102 	if (error != 0) {
6103 		NFSUNLOCKSESSION(shp);
6104 		return (error);
6105 	}
6106 	if (cache_this != 0)
6107 		nd->nd_flag |= ND_SAVEREPLY;
6108 	/* Renew the lease. */
6109 	sep->sess_clp->lc_expiry = nfsrv_leaseexpiry();
6110 	nd->nd_clientid.qval = sep->sess_clp->lc_clientid.qval;
6111 	nd->nd_flag |= ND_IMPLIEDCLID;
6112 
6113 	/* Handle the SP4_MECH_CRED case for NFSv4.1/4.2. */
6114 	if ((sep->sess_clp->lc_flags & LCL_MACHCRED) != 0 &&
6115 	    (nd->nd_flag & (ND_GSSINTEGRITY | ND_GSSPRIVACY)) != 0 &&
6116 	    nd->nd_princlen == sep->sess_clp->lc_namelen &&
6117 	    !NFSBCMP(sep->sess_clp->lc_name, nd->nd_principal,
6118 	    nd->nd_princlen)) {
6119 		nd->nd_flag |= ND_MACHCRED;
6120 		NFSSET_OPBIT(&nd->nd_allowops, &sep->sess_clp->lc_allowops);
6121 	}
6122 
6123 	/* Save maximum request and reply sizes. */
6124 	nd->nd_maxreq = sep->sess_maxreq;
6125 	nd->nd_maxresp = sep->sess_maxresp;
6126 
6127 	*sflagsp = 0;
6128 	if (sep->sess_clp->lc_req.nr_client == NULL ||
6129 	    (sep->sess_clp->lc_flags & LCL_CBDOWN) != 0)
6130 		*sflagsp |= NFSV4SEQ_CBPATHDOWN;
6131 	NFSUNLOCKSESSION(shp);
6132 	if (error == NFSERR_EXPIRED) {
6133 		*sflagsp |= NFSV4SEQ_EXPIREDALLSTATEREVOKED;
6134 		error = 0;
6135 	} else if (error == NFSERR_ADMINREVOKED) {
6136 		*sflagsp |= NFSV4SEQ_ADMINSTATEREVOKED;
6137 		error = 0;
6138 	}
6139 	*highest_slotidp = *target_highest_slotidp = NFSV4_SLOTS - 1;
6140 	return (0);
6141 }
6142 
6143 /*
6144  * Check/set reclaim complete for this session/clientid.
6145  */
6146 int
nfsrv_checkreclaimcomplete(struct nfsrv_descript * nd,int onefs)6147 nfsrv_checkreclaimcomplete(struct nfsrv_descript *nd, int onefs)
6148 {
6149 	struct nfsdsession *sep;
6150 	struct nfssessionhash *shp;
6151 	int error = 0;
6152 
6153 	shp = NFSSESSIONHASH(nd->nd_sessionid);
6154 	NFSLOCKSTATE();
6155 	NFSLOCKSESSION(shp);
6156 	sep = nfsrv_findsession(nd->nd_sessionid);
6157 	if (sep == NULL) {
6158 		NFSUNLOCKSESSION(shp);
6159 		NFSUNLOCKSTATE();
6160 		return (NFSERR_BADSESSION);
6161 	}
6162 
6163 	if (onefs != 0)
6164 		sep->sess_clp->lc_flags |= LCL_RECLAIMONEFS;
6165 		/* Check to see if reclaim complete has already happened. */
6166 	else if ((sep->sess_clp->lc_flags & LCL_RECLAIMCOMPLETE) != 0)
6167 		error = NFSERR_COMPLETEALREADY;
6168 	else {
6169 		sep->sess_clp->lc_flags |= LCL_RECLAIMCOMPLETE;
6170 		nfsrv_markreclaim(sep->sess_clp);
6171 	}
6172 	NFSUNLOCKSESSION(shp);
6173 	NFSUNLOCKSTATE();
6174 	return (error);
6175 }
6176 
6177 /*
6178  * Cache the reply in a session slot.
6179  */
6180 void
nfsrv_cache_session(struct nfsrv_descript * nd,struct mbuf ** m)6181 nfsrv_cache_session(struct nfsrv_descript *nd, struct mbuf **m)
6182 {
6183 	struct nfsdsession *sep;
6184 	struct nfssessionhash *shp;
6185 	char *buf, *cp;
6186 #ifdef INET
6187 	struct sockaddr_in *sin;
6188 #endif
6189 #ifdef INET6
6190 	struct sockaddr_in6 *sin6;
6191 #endif
6192 
6193 	shp = NFSSESSIONHASH(nd->nd_sessionid);
6194 	NFSLOCKSESSION(shp);
6195 	sep = nfsrv_findsession(nd->nd_sessionid);
6196 	if (sep == NULL) {
6197 		NFSUNLOCKSESSION(shp);
6198 		if ((VNET(nfsrv_stablefirst).nsf_flags &
6199 		     NFSNSF_GRACEOVER) != 0) {
6200 			buf = malloc(INET6_ADDRSTRLEN, M_TEMP, M_WAITOK);
6201 			switch (nd->nd_nam->sa_family) {
6202 #ifdef INET
6203 			case AF_INET:
6204 				sin = (struct sockaddr_in *)nd->nd_nam;
6205 				cp = inet_ntop(sin->sin_family,
6206 				    &sin->sin_addr.s_addr, buf,
6207 				    INET6_ADDRSTRLEN);
6208 				break;
6209 #endif
6210 #ifdef INET6
6211 			case AF_INET6:
6212 				sin6 = (struct sockaddr_in6 *)nd->nd_nam;
6213 				cp = inet_ntop(sin6->sin6_family,
6214 				    &sin6->sin6_addr, buf, INET6_ADDRSTRLEN);
6215 				break;
6216 #endif
6217 			default:
6218 				cp = NULL;
6219 			}
6220 			if (cp != NULL)
6221 				printf("nfsrv_cache_session: no session "
6222 				    "IPaddr=%s, check NFS clients for unique "
6223 				    "/etc/hostid's\n", cp);
6224 			else
6225 				printf("nfsrv_cache_session: no session, "
6226 				    "check NFS clients for unique "
6227 				    "/etc/hostid's\n");
6228 			free(buf, M_TEMP);
6229 		}
6230 		m_freem(*m);
6231 		return;
6232 	}
6233 	nfsv4_seqsess_cacherep(nd->nd_slotid, sep->sess_slots, nd->nd_repstat,
6234 	    m);
6235 	NFSUNLOCKSESSION(shp);
6236 }
6237 
6238 /*
6239  * Search for a session that matches the sessionid.
6240  */
6241 static struct nfsdsession *
nfsrv_findsession(uint8_t * sessionid)6242 nfsrv_findsession(uint8_t *sessionid)
6243 {
6244 	struct nfsdsession *sep;
6245 	struct nfssessionhash *shp;
6246 
6247 	shp = NFSSESSIONHASH(sessionid);
6248 	LIST_FOREACH(sep, &shp->list, sess_hash) {
6249 		if (!NFSBCMP(sessionid, sep->sess_sessionid, NFSX_V4SESSIONID))
6250 			break;
6251 	}
6252 	return (sep);
6253 }
6254 
6255 /*
6256  * Destroy a session.
6257  */
6258 int
nfsrv_destroysession(struct nfsrv_descript * nd,uint8_t * sessionid)6259 nfsrv_destroysession(struct nfsrv_descript *nd, uint8_t *sessionid)
6260 {
6261 	int error, igotlock, samesess;
6262 
6263 	samesess = 0;
6264 	if (!NFSBCMP(sessionid, nd->nd_sessionid, NFSX_V4SESSIONID) &&
6265 	    (nd->nd_flag & ND_HASSEQUENCE) != 0) {
6266 		samesess = 1;
6267 		if ((nd->nd_flag & ND_LASTOP) == 0)
6268 			return (NFSERR_BADSESSION);
6269 	}
6270 
6271 	/* Lock out other nfsd threads */
6272 	NFSLOCKV4ROOTMUTEX();
6273 	nfsv4_relref(&nfsv4rootfs_lock);
6274 	do {
6275 		igotlock = nfsv4_lock(&nfsv4rootfs_lock, 1, NULL,
6276 		    NFSV4ROOTLOCKMUTEXPTR, NULL);
6277 	} while (igotlock == 0);
6278 	NFSUNLOCKV4ROOTMUTEX();
6279 
6280 	error = nfsrv_freesession(nd, NULL, sessionid, false, NULL);
6281 	if (error == 0 && samesess != 0)
6282 		nd->nd_flag &= ~ND_HASSEQUENCE;
6283 
6284 	NFSLOCKV4ROOTMUTEX();
6285 	nfsv4_unlock(&nfsv4rootfs_lock, 1);
6286 	NFSUNLOCKV4ROOTMUTEX();
6287 	return (error);
6288 }
6289 
6290 /*
6291  * Bind a connection to a session.
6292  * For now, only certain variants are supported, since the current session
6293  * structure can only handle a single backchannel entry, which will be
6294  * applied to all connections if it is set.
6295  */
6296 int
nfsrv_bindconnsess(struct nfsrv_descript * nd,uint8_t * sessionid,int * foreaftp)6297 nfsrv_bindconnsess(struct nfsrv_descript *nd, uint8_t *sessionid, int *foreaftp)
6298 {
6299 	struct nfssessionhash *shp;
6300 	struct nfsdsession *sep;
6301 	struct nfsclient *clp;
6302 	SVCXPRT *savxprt;
6303 	int error;
6304 
6305 	error = 0;
6306 	savxprt = NULL;
6307 	shp = NFSSESSIONHASH(sessionid);
6308 	NFSLOCKSTATE();
6309 	NFSLOCKSESSION(shp);
6310 	sep = nfsrv_findsession(sessionid);
6311 	if (sep != NULL) {
6312 		clp = sep->sess_clp;
6313 		error = nfsrv_checkmachcred(NFSV4OP_BINDCONNTOSESS, nd, clp);
6314 		if (error != 0)
6315 			goto out;
6316 		if (*foreaftp == NFSCDFC4_BACK ||
6317 		    *foreaftp == NFSCDFC4_BACK_OR_BOTH ||
6318 		    *foreaftp == NFSCDFC4_FORE_OR_BOTH) {
6319 			/* Try to set up a backchannel. */
6320 			if (clp->lc_req.nr_client == NULL) {
6321 				NFSD_DEBUG(2, "nfsrv_bindconnsess: acquire "
6322 				    "backchannel\n");
6323 				clp->lc_req.nr_client = (struct __rpc_client *)
6324 				    clnt_bck_create(nd->nd_xprt->xp_socket,
6325 				    sep->sess_cbprogram, NFSV4_CBVERS);
6326 			}
6327 			if (clp->lc_req.nr_client != NULL) {
6328 				NFSD_DEBUG(2, "nfsrv_bindconnsess: set up "
6329 				    "backchannel\n");
6330 				savxprt = sep->sess_cbsess.nfsess_xprt;
6331 				SVC_ACQUIRE(nd->nd_xprt);
6332 				CLNT_ACQUIRE(clp->lc_req.nr_client);
6333 				nd->nd_xprt->xp_p2 = clp->lc_req.nr_client;
6334 				/* Disable idle timeout. */
6335 				nd->nd_xprt->xp_idletimeout = 0;
6336 				sep->sess_cbsess.nfsess_xprt = nd->nd_xprt;
6337 				sep->sess_crflags |= NFSV4CRSESS_CONNBACKCHAN;
6338 				clp->lc_flags |= LCL_DONEBINDCONN |
6339 				    LCL_NEEDSCBNULL;
6340 				clp->lc_flags &= ~LCL_CBDOWN;
6341 				if (*foreaftp == NFSCDFS4_BACK)
6342 					*foreaftp = NFSCDFS4_BACK;
6343 				else
6344 					*foreaftp = NFSCDFS4_BOTH;
6345 			} else if (*foreaftp != NFSCDFC4_BACK) {
6346 				NFSD_DEBUG(2, "nfsrv_bindconnsess: can't set "
6347 				    "up backchannel\n");
6348 				sep->sess_crflags &= ~NFSV4CRSESS_CONNBACKCHAN;
6349 				clp->lc_flags |= LCL_DONEBINDCONN;
6350 				*foreaftp = NFSCDFS4_FORE;
6351 			} else {
6352 				error = NFSERR_NOTSUPP;
6353 				printf("nfsrv_bindconnsess: Can't add "
6354 				    "backchannel\n");
6355 			}
6356 		} else {
6357 			NFSD_DEBUG(2, "nfsrv_bindconnsess: Set forechannel\n");
6358 			clp->lc_flags |= LCL_DONEBINDCONN;
6359 			*foreaftp = NFSCDFS4_FORE;
6360 		}
6361 	} else
6362 		error = NFSERR_BADSESSION;
6363 out:
6364 	NFSUNLOCKSESSION(shp);
6365 	NFSUNLOCKSTATE();
6366 	if (savxprt != NULL)
6367 		SVC_RELEASE(savxprt);
6368 	return (error);
6369 }
6370 
6371 /*
6372  * Free up a session structure.
6373  */
6374 static int
nfsrv_freesession(struct nfsrv_descript * nd,struct nfsdsession * sep,uint8_t * sessionid,bool locked,SVCXPRT ** old_xprtp)6375 nfsrv_freesession(struct nfsrv_descript *nd, struct nfsdsession *sep,
6376     uint8_t *sessionid, bool locked, SVCXPRT **old_xprtp)
6377 {
6378 	struct nfssessionhash *shp;
6379 	int i;
6380 
6381 	if (!locked)
6382 		NFSLOCKSTATE();
6383 	if (sep == NULL) {
6384 		shp = NFSSESSIONHASH(sessionid);
6385 		NFSLOCKSESSION(shp);
6386 		sep = nfsrv_findsession(sessionid);
6387 	} else {
6388 		shp = NFSSESSIONHASH(sep->sess_sessionid);
6389 		NFSLOCKSESSION(shp);
6390 	}
6391 	if (sep != NULL) {
6392 		/* Check for the SP4_MACH_CRED case. */
6393 		if (nd != NULL && nfsrv_checkmachcred(NFSV4OP_DESTROYSESSION,
6394 		    nd, sep->sess_clp) != 0) {
6395 			NFSUNLOCKSESSION(shp);
6396 			if (!locked)
6397 				NFSUNLOCKSTATE();
6398 			return (NFSERR_AUTHERR | AUTH_TOOWEAK);
6399 		}
6400 
6401 		sep->sess_refcnt--;
6402 		if (sep->sess_refcnt > 0) {
6403 			NFSUNLOCKSESSION(shp);
6404 			if (!locked)
6405 				NFSUNLOCKSTATE();
6406 			return (NFSERR_BACKCHANBUSY);
6407 		}
6408 		LIST_REMOVE(sep, sess_hash);
6409 		LIST_REMOVE(sep, sess_list);
6410 	}
6411 	NFSUNLOCKSESSION(shp);
6412 	if (!locked)
6413 		NFSUNLOCKSTATE();
6414 	if (sep == NULL)
6415 		return (NFSERR_BADSESSION);
6416 	for (i = 0; i < NFSV4_SLOTS; i++)
6417 		if (sep->sess_slots[i].nfssl_reply != NULL)
6418 			m_freem(sep->sess_slots[i].nfssl_reply);
6419 	if (!locked) {
6420 		if (sep->sess_cbsess.nfsess_xprt != NULL)
6421 			SVC_RELEASE(sep->sess_cbsess.nfsess_xprt);
6422 		if (old_xprtp != NULL)
6423 			*old_xprtp = NULL;
6424 	} else if (old_xprtp != NULL)
6425 		*old_xprtp = sep->sess_cbsess.nfsess_xprt;
6426 	free(sep, M_NFSDSESSION);
6427 	return (0);
6428 }
6429 
6430 /*
6431  * Free a stateid.
6432  * RFC5661 says that it should fail when there are associated opens, locks
6433  * or delegations. Since stateids represent opens, I don't see how you can
6434  * free an open stateid (it will be free'd when closed), so this function
6435  * only works for lock stateids (freeing the lock_owner) or delegations.
6436  */
6437 int
nfsrv_freestateid(struct nfsrv_descript * nd,nfsv4stateid_t * stateidp,NFSPROC_T * p)6438 nfsrv_freestateid(struct nfsrv_descript *nd, nfsv4stateid_t *stateidp,
6439     NFSPROC_T *p)
6440 {
6441 	struct nfsclient *clp;
6442 	struct nfsstate *stp;
6443 	int error;
6444 
6445 	NFSLOCKSTATE();
6446 	/*
6447 	 * Look up the stateid
6448 	 */
6449 	error = nfsrv_getclient((nfsquad_t)((u_quad_t)0), CLOPS_RENEW, &clp,
6450 	    NULL, (nfsquad_t)((u_quad_t)0), 0, nd, p);
6451 	if (error == 0) {
6452 		/* First, check for a delegation. */
6453 		LIST_FOREACH(stp, &clp->lc_deleg, ls_list) {
6454 			if (!NFSBCMP(stp->ls_stateid.other, stateidp->other,
6455 			    NFSX_STATEIDOTHER))
6456 				break;
6457 		}
6458 		if (stp != NULL) {
6459 			nfsrv_freedeleg(stp);
6460 			NFSUNLOCKSTATE();
6461 			return (error);
6462 		}
6463 	}
6464 	/* Not a delegation, try for a lock_owner. */
6465 	if (error == 0)
6466 		error = nfsrv_getstate(clp, stateidp, 0, &stp);
6467 	if (error == 0 && ((stp->ls_flags & (NFSLCK_OPEN | NFSLCK_DELEGREAD |
6468 	    NFSLCK_DELEGWRITE)) != 0 || (stp->ls_flags & NFSLCK_LOCK) == 0))
6469 		/* Not a lock_owner stateid. */
6470 		error = NFSERR_LOCKSHELD;
6471 	if (error == 0 && !LIST_EMPTY(&stp->ls_lock))
6472 		error = NFSERR_LOCKSHELD;
6473 	if (error == 0)
6474 		nfsrv_freelockowner(stp, NULL, 0, p);
6475 	NFSUNLOCKSTATE();
6476 	return (error);
6477 }
6478 
6479 /*
6480  * Test a stateid.
6481  */
6482 int
nfsrv_teststateid(struct nfsrv_descript * nd,nfsv4stateid_t * stateidp,NFSPROC_T * p)6483 nfsrv_teststateid(struct nfsrv_descript *nd, nfsv4stateid_t *stateidp,
6484     NFSPROC_T *p)
6485 {
6486 	struct nfsclient *clp;
6487 	struct nfsstate *stp;
6488 	int error;
6489 
6490 	NFSLOCKSTATE();
6491 	/*
6492 	 * Look up the stateid
6493 	 */
6494 	error = nfsrv_getclient((nfsquad_t)((u_quad_t)0), CLOPS_RENEW, &clp,
6495 	    NULL, (nfsquad_t)((u_quad_t)0), 0, nd, p);
6496 	if (error == 0)
6497 		error = nfsrv_getstate(clp, stateidp, 0, &stp);
6498 	if (error == 0 && stateidp->seqid != 0 &&
6499 	    SEQ_LT(stateidp->seqid, stp->ls_stateid.seqid))
6500 		error = NFSERR_OLDSTATEID;
6501 	NFSUNLOCKSTATE();
6502 	return (error);
6503 }
6504 
6505 /*
6506  * Generate the xdr for an NFSv4.1 CBSequence Operation.
6507  */
6508 static int
nfsv4_setcbsequence(struct nfsrv_descript * nd,struct nfsclient * clp,int dont_replycache,struct nfsdsession ** sepp,int * slotposp)6509 nfsv4_setcbsequence(struct nfsrv_descript *nd, struct nfsclient *clp,
6510     int dont_replycache, struct nfsdsession **sepp, int *slotposp)
6511 {
6512 	struct nfsdsession *sep;
6513 	uint32_t *tl, slotseq = 0;
6514 	int maxslot;
6515 	uint8_t sessionid[NFSX_V4SESSIONID];
6516 	int error;
6517 
6518 	error = nfsv4_getcbsession(clp, sepp);
6519 	if (error != 0)
6520 		return (error);
6521 	sep = *sepp;
6522 	nfsv4_sequencelookup(NULL, &sep->sess_cbsess, slotposp, &maxslot,
6523 	    &slotseq, sessionid, true);
6524 	KASSERT(maxslot >= 0, ("nfsv4_setcbsequence neg maxslot"));
6525 
6526 	/* Build the Sequence arguments. */
6527 	NFSM_BUILD(tl, uint32_t *, NFSX_V4SESSIONID + 5 * NFSX_UNSIGNED);
6528 	bcopy(sessionid, tl, NFSX_V4SESSIONID);
6529 	tl += NFSX_V4SESSIONID / NFSX_UNSIGNED;
6530 	nd->nd_slotseq = tl;
6531 	nd->nd_slotid = *slotposp;
6532 	nd->nd_flag |= ND_HASSLOTID;
6533 	*tl++ = txdr_unsigned(slotseq);
6534 	*tl++ = txdr_unsigned(*slotposp);
6535 	*tl++ = txdr_unsigned(maxslot);
6536 	if (dont_replycache == 0)
6537 		*tl++ = newnfs_true;
6538 	else
6539 		*tl++ = newnfs_false;
6540 	*tl = 0;			/* No referring call list, for now. */
6541 	nd->nd_flag |= ND_HASSEQUENCE;
6542 	return (0);
6543 }
6544 
6545 /*
6546  * Get a session for the callback.
6547  */
6548 static int
nfsv4_getcbsession(struct nfsclient * clp,struct nfsdsession ** sepp)6549 nfsv4_getcbsession(struct nfsclient *clp, struct nfsdsession **sepp)
6550 {
6551 	struct nfsdsession *sep;
6552 
6553 	NFSLOCKSTATE();
6554 	LIST_FOREACH(sep, &clp->lc_session, sess_list) {
6555 		if ((sep->sess_crflags & NFSV4CRSESS_CONNBACKCHAN) != 0)
6556 			break;
6557 	}
6558 	if (sep == NULL) {
6559 		NFSUNLOCKSTATE();
6560 		return (NFSERR_BADSESSION);
6561 	}
6562 	sep->sess_refcnt++;
6563 	*sepp = sep;
6564 	NFSUNLOCKSTATE();
6565 	return (0);
6566 }
6567 
6568 /*
6569  * Free up all backchannel xprts.  This needs to be done when the nfsd threads
6570  * exit, since those transports will all be going away.
6571  * This is only called after all the nfsd threads are done performing RPCs,
6572  * so locking shouldn't be an issue.
6573  */
6574 void
nfsrv_freeallbackchannel_xprts(void)6575 nfsrv_freeallbackchannel_xprts(void)
6576 {
6577 	struct nfsdsession *sep;
6578 	struct nfsclient *clp;
6579 	SVCXPRT *xprt;
6580 	int i;
6581 
6582 	for (i = 0; i < nfsrv_clienthashsize; i++) {
6583 		LIST_FOREACH(clp, &VNET(nfsclienthash)[i], lc_hash) {
6584 			LIST_FOREACH(sep, &clp->lc_session, sess_list) {
6585 				xprt = sep->sess_cbsess.nfsess_xprt;
6586 				sep->sess_cbsess.nfsess_xprt = NULL;
6587 				if (xprt != NULL)
6588 					SVC_RELEASE(xprt);
6589 			}
6590 		}
6591 	}
6592 }
6593 
6594 /*
6595  * Do a layout commit.  Actually just call nfsrv_updatemdsattr().
6596  * I have no idea if the rest of these arguments will ever be useful?
6597  */
6598 int
nfsrv_layoutcommit(struct nfsrv_descript * nd,vnode_t vp,int layouttype,int hasnewoff,uint64_t newoff,uint64_t offset,uint64_t len,int hasnewmtime,struct timespec * newmtimep,int reclaim,nfsv4stateid_t * stateidp,int maxcnt,char * layp,int * hasnewsizep,uint64_t * newsizep,struct ucred * cred,NFSPROC_T * p)6599 nfsrv_layoutcommit(struct nfsrv_descript *nd, vnode_t vp, int layouttype,
6600     int hasnewoff, uint64_t newoff, uint64_t offset, uint64_t len,
6601     int hasnewmtime, struct timespec *newmtimep, int reclaim,
6602     nfsv4stateid_t *stateidp, int maxcnt, char *layp, int *hasnewsizep,
6603     uint64_t *newsizep, struct ucred *cred, NFSPROC_T *p)
6604 {
6605 	struct nfsvattr na;
6606 	int error;
6607 
6608 	error = nfsrv_updatemdsattr(vp, &na, p);
6609 	if (error == 0) {
6610 		*hasnewsizep = 1;
6611 		*newsizep = na.na_size;
6612 	}
6613 	return (error);
6614 }
6615 
6616 /*
6617  * Try and get a layout.
6618  */
6619 int
nfsrv_layoutget(struct nfsrv_descript * nd,vnode_t vp,struct nfsexstuff * exp,int layouttype,int * iomode,uint64_t * offset,uint64_t * len,uint64_t minlen,nfsv4stateid_t * stateidp,int maxcnt,int * retonclose,int * layoutlenp,char * layp,struct ucred * cred,NFSPROC_T * p)6620 nfsrv_layoutget(struct nfsrv_descript *nd, vnode_t vp, struct nfsexstuff *exp,
6621     int layouttype, int *iomode, uint64_t *offset, uint64_t *len,
6622     uint64_t minlen, nfsv4stateid_t *stateidp, int maxcnt, int *retonclose,
6623     int *layoutlenp, char *layp, struct ucred *cred, NFSPROC_T *p)
6624 {
6625 	struct nfslayouthash *lhyp;
6626 	struct nfslayout *lyp;
6627 	char *devid;
6628 	fhandle_t fh, *dsfhp;
6629 	int error, mirrorcnt;
6630 
6631 	if (nfsrv_devidcnt == 0)
6632 		return (NFSERR_UNKNLAYOUTTYPE);
6633 
6634 	if (*offset != 0)
6635 		printf("nfsrv_layoutget: off=%ju len=%ju\n", (uintmax_t)*offset,
6636 		    (uintmax_t)*len);
6637 	error = nfsvno_getfh(vp, &fh, p);
6638 	NFSD_DEBUG(4, "layoutget getfh=%d\n", error);
6639 	if (error != 0)
6640 		return (error);
6641 
6642 	/*
6643 	 * For now, all layouts are for entire files.
6644 	 * Only issue Read/Write layouts if requested for a non-readonly fs.
6645 	 */
6646 	if (NFSVNO_EXRDONLY(exp)) {
6647 		if (*iomode == NFSLAYOUTIOMODE_RW)
6648 			return (NFSERR_LAYOUTTRYLATER);
6649 		*iomode = NFSLAYOUTIOMODE_READ;
6650 	}
6651 	if (*iomode != NFSLAYOUTIOMODE_RW)
6652 		*iomode = NFSLAYOUTIOMODE_READ;
6653 
6654 	/*
6655 	 * Check to see if a write layout can be issued for this file.
6656 	 * This is used during mirror recovery to avoid RW layouts being
6657 	 * issued for a file while it is being copied to the recovered
6658 	 * mirror.
6659 	 */
6660 	if (*iomode == NFSLAYOUTIOMODE_RW && nfsrv_dontlayout(&fh) != 0)
6661 		return (NFSERR_LAYOUTTRYLATER);
6662 
6663 	*retonclose = 0;
6664 	*offset = 0;
6665 	*len = UINT64_MAX;
6666 
6667 	/* First, see if a layout already exists and return if found. */
6668 	lhyp = NFSLAYOUTHASH(&fh);
6669 	NFSLOCKLAYOUT(lhyp);
6670 	error = nfsrv_findlayout(&nd->nd_clientid, &fh, layouttype, p, &lyp);
6671 	NFSD_DEBUG(4, "layoutget findlay=%d\n", error);
6672 	/*
6673 	 * Not sure if the seqid must be the same, so I won't check it.
6674 	 */
6675 	if (error == 0 && (stateidp->other[0] != lyp->lay_stateid.other[0] ||
6676 	    stateidp->other[1] != lyp->lay_stateid.other[1] ||
6677 	    stateidp->other[2] != lyp->lay_stateid.other[2])) {
6678 		if ((lyp->lay_flags & NFSLAY_CALLB) == 0) {
6679 			NFSUNLOCKLAYOUT(lhyp);
6680 			NFSD_DEBUG(1, "ret bad stateid\n");
6681 			return (NFSERR_BADSTATEID);
6682 		}
6683 		/*
6684 		 * I believe we get here because there is a race between
6685 		 * the client processing the CBLAYOUTRECALL and the layout
6686 		 * being deleted here on the server.
6687 		 * The client has now done a LayoutGet with a non-layout
6688 		 * stateid, as it would when there is no layout.
6689 		 * As such, free this layout and set error == NFSERR_BADSTATEID
6690 		 * so the code below will create a new layout structure as
6691 		 * would happen if no layout was found.
6692 		 * "lyp" will be set before being used below, but set it NULL
6693 		 * as a safety belt.
6694 		 */
6695 		nfsrv_freelayout(&lhyp->list, lyp);
6696 		lyp = NULL;
6697 		error = NFSERR_BADSTATEID;
6698 	}
6699 	if (error == 0) {
6700 		if (lyp->lay_layoutlen > maxcnt) {
6701 			NFSUNLOCKLAYOUT(lhyp);
6702 			NFSD_DEBUG(1, "ret layout too small\n");
6703 			return (NFSERR_TOOSMALL);
6704 		}
6705 		if (*iomode == NFSLAYOUTIOMODE_RW) {
6706 			if ((lyp->lay_flags & NFSLAY_NOSPC) != 0) {
6707 				NFSUNLOCKLAYOUT(lhyp);
6708 				NFSD_DEBUG(1, "ret layout nospace\n");
6709 				return (NFSERR_NOSPC);
6710 			}
6711 			lyp->lay_flags |= NFSLAY_RW;
6712 		} else
6713 			lyp->lay_flags |= NFSLAY_READ;
6714 		NFSBCOPY(lyp->lay_xdr, layp, lyp->lay_layoutlen);
6715 		*layoutlenp = lyp->lay_layoutlen;
6716 		if (++lyp->lay_stateid.seqid == 0)
6717 			lyp->lay_stateid.seqid = 1;
6718 		stateidp->seqid = lyp->lay_stateid.seqid;
6719 		NFSUNLOCKLAYOUT(lhyp);
6720 		NFSD_DEBUG(4, "ret fnd layout\n");
6721 		return (0);
6722 	}
6723 	NFSUNLOCKLAYOUT(lhyp);
6724 
6725 	/* Find the device id and file handle. */
6726 	dsfhp = malloc(sizeof(fhandle_t) * NFSDEV_MAXMIRRORS, M_TEMP, M_WAITOK);
6727 	devid = malloc(NFSX_V4DEVICEID * NFSDEV_MAXMIRRORS, M_TEMP, M_WAITOK);
6728 	error = nfsrv_dsgetdevandfh(vp, p, &mirrorcnt, dsfhp, devid);
6729 	NFSD_DEBUG(4, "layoutget devandfh=%d\n", error);
6730 	if (error == 0) {
6731 		if (layouttype == NFSLAYOUT_NFSV4_1_FILES) {
6732 			if (NFSX_V4FILELAYOUT > maxcnt)
6733 				error = NFSERR_TOOSMALL;
6734 			else
6735 				lyp = nfsrv_filelayout(nd, *iomode, &fh, dsfhp,
6736 				    devid, vp->v_mount->mnt_stat.f_fsid);
6737 		} else {
6738 			if (NFSX_V4FLEXLAYOUT(mirrorcnt) > maxcnt)
6739 				error = NFSERR_TOOSMALL;
6740 			else
6741 				lyp = nfsrv_flexlayout(nd, *iomode, mirrorcnt,
6742 				    &fh, dsfhp, devid,
6743 				    vp->v_mount->mnt_stat.f_fsid);
6744 		}
6745 	}
6746 	free(dsfhp, M_TEMP);
6747 	free(devid, M_TEMP);
6748 	if (error != 0)
6749 		return (error);
6750 
6751 	/*
6752 	 * Now, add this layout to the list.
6753 	 */
6754 	error = nfsrv_addlayout(nd, &lyp, stateidp, layp, layoutlenp, p);
6755 	NFSD_DEBUG(4, "layoutget addl=%d\n", error);
6756 	/*
6757 	 * The lyp will be set to NULL by nfsrv_addlayout() if it
6758 	 * linked the new structure into the lists.
6759 	 */
6760 	free(lyp, M_NFSDSTATE);
6761 	return (error);
6762 }
6763 
6764 /*
6765  * Generate a File Layout.
6766  */
6767 static struct nfslayout *
nfsrv_filelayout(struct nfsrv_descript * nd,int iomode,fhandle_t * fhp,fhandle_t * dsfhp,char * devid,fsid_t fs)6768 nfsrv_filelayout(struct nfsrv_descript *nd, int iomode, fhandle_t *fhp,
6769     fhandle_t *dsfhp, char *devid, fsid_t fs)
6770 {
6771 	uint32_t *tl;
6772 	struct nfslayout *lyp;
6773 	uint64_t pattern_offset;
6774 
6775 	lyp = malloc(sizeof(struct nfslayout) + NFSX_V4FILELAYOUT, M_NFSDSTATE,
6776 	    M_WAITOK | M_ZERO);
6777 	lyp->lay_type = NFSLAYOUT_NFSV4_1_FILES;
6778 	if (iomode == NFSLAYOUTIOMODE_RW)
6779 		lyp->lay_flags = NFSLAY_RW;
6780 	else
6781 		lyp->lay_flags = NFSLAY_READ;
6782 	NFSBCOPY(fhp, &lyp->lay_fh, sizeof(*fhp));
6783 	lyp->lay_clientid.qval = nd->nd_clientid.qval;
6784 	lyp->lay_fsid = fs;
6785 	NFSBCOPY(devid, lyp->lay_deviceid, NFSX_V4DEVICEID);
6786 
6787 	/* Fill in the xdr for the files layout. */
6788 	tl = (uint32_t *)lyp->lay_xdr;
6789 	NFSBCOPY(devid, tl, NFSX_V4DEVICEID);		/* Device ID. */
6790 	tl += (NFSX_V4DEVICEID / NFSX_UNSIGNED);
6791 
6792 	/* Set the stripe size to the maximum I/O size. */
6793 	*tl++ = txdr_unsigned(nfs_srvmaxio & NFSFLAYUTIL_STRIPE_MASK);
6794 	*tl++ = 0;					/* 1st stripe index. */
6795 	pattern_offset = 0;
6796 	txdr_hyper(pattern_offset, tl); tl += 2;	/* Pattern offset. */
6797 	*tl++ = txdr_unsigned(1);			/* 1 file handle. */
6798 	*tl++ = txdr_unsigned(NFSX_V4PNFSFH);
6799 	NFSBCOPY(dsfhp, tl, sizeof(*dsfhp));
6800 	lyp->lay_layoutlen = NFSX_V4FILELAYOUT;
6801 	return (lyp);
6802 }
6803 
6804 #define	FLEX_OWNERID	"999"
6805 #define	FLEX_UID0	"0"
6806 /*
6807  * Generate a Flex File Layout.
6808  * The FLEX_OWNERID can be any string of 3 decimal digits. Although this
6809  * string goes on the wire, it isn't supposed to be used by the client,
6810  * since this server uses tight coupling.
6811  * Although not recommended by the spec., if vfs.nfsd.flexlinuxhack=1 use
6812  * a string of "0". This works around the Linux Flex File Layout driver bug
6813  * which uses the synthetic uid/gid strings for the "tightly coupled" case.
6814  */
6815 static struct nfslayout *
nfsrv_flexlayout(struct nfsrv_descript * nd,int iomode,int mirrorcnt,fhandle_t * fhp,fhandle_t * dsfhp,char * devid,fsid_t fs)6816 nfsrv_flexlayout(struct nfsrv_descript *nd, int iomode, int mirrorcnt,
6817     fhandle_t *fhp, fhandle_t *dsfhp, char *devid, fsid_t fs)
6818 {
6819 	uint32_t *tl;
6820 	struct nfslayout *lyp;
6821 	uint64_t lenval;
6822 	int i;
6823 
6824 	lyp = malloc(sizeof(struct nfslayout) + NFSX_V4FLEXLAYOUT(mirrorcnt),
6825 	    M_NFSDSTATE, M_WAITOK | M_ZERO);
6826 	lyp->lay_type = NFSLAYOUT_FLEXFILE;
6827 	if (iomode == NFSLAYOUTIOMODE_RW)
6828 		lyp->lay_flags = NFSLAY_RW;
6829 	else
6830 		lyp->lay_flags = NFSLAY_READ;
6831 	NFSBCOPY(fhp, &lyp->lay_fh, sizeof(*fhp));
6832 	lyp->lay_clientid.qval = nd->nd_clientid.qval;
6833 	lyp->lay_fsid = fs;
6834 	lyp->lay_mirrorcnt = mirrorcnt;
6835 	NFSBCOPY(devid, lyp->lay_deviceid, NFSX_V4DEVICEID);
6836 
6837 	/* Fill in the xdr for the files layout. */
6838 	tl = (uint32_t *)lyp->lay_xdr;
6839 	lenval = 0;
6840 	txdr_hyper(lenval, tl); tl += 2;		/* Stripe unit. */
6841 	*tl++ = txdr_unsigned(mirrorcnt);		/* # of mirrors. */
6842 	for (i = 0; i < mirrorcnt; i++) {
6843 		*tl++ = txdr_unsigned(1);		/* One stripe. */
6844 		NFSBCOPY(devid, tl, NFSX_V4DEVICEID);	/* Device ID. */
6845 		tl += (NFSX_V4DEVICEID / NFSX_UNSIGNED);
6846 		devid += NFSX_V4DEVICEID;
6847 		*tl++ = txdr_unsigned(1);		/* Efficiency. */
6848 		*tl++ = 0;				/* Proxy Stateid. */
6849 		*tl++ = 0x55555555;
6850 		*tl++ = 0x55555555;
6851 		*tl++ = 0x55555555;
6852 		*tl++ = txdr_unsigned(1);		/* 1 file handle. */
6853 		*tl++ = txdr_unsigned(NFSX_V4PNFSFH);
6854 		NFSBCOPY(dsfhp, tl, sizeof(*dsfhp));
6855 		tl += (NFSM_RNDUP(NFSX_V4PNFSFH) / NFSX_UNSIGNED);
6856 		dsfhp++;
6857 		if (nfsrv_flexlinuxhack != 0) {
6858 			*tl++ = txdr_unsigned(strlen(FLEX_UID0));
6859 			*tl = 0;		/* 0 pad string. */
6860 			NFSBCOPY(FLEX_UID0, tl++, strlen(FLEX_UID0));
6861 			*tl++ = txdr_unsigned(strlen(FLEX_UID0));
6862 			*tl = 0;		/* 0 pad string. */
6863 			NFSBCOPY(FLEX_UID0, tl++, strlen(FLEX_UID0));
6864 		} else {
6865 			*tl++ = txdr_unsigned(strlen(FLEX_OWNERID));
6866 			NFSBCOPY(FLEX_OWNERID, tl++, NFSX_UNSIGNED);
6867 			*tl++ = txdr_unsigned(strlen(FLEX_OWNERID));
6868 			NFSBCOPY(FLEX_OWNERID, tl++, NFSX_UNSIGNED);
6869 		}
6870 	}
6871 	*tl++ = txdr_unsigned(0);		/* ff_flags. */
6872 	*tl = txdr_unsigned(60);		/* Status interval hint. */
6873 	lyp->lay_layoutlen = NFSX_V4FLEXLAYOUT(mirrorcnt);
6874 	return (lyp);
6875 }
6876 
6877 /*
6878  * Parse and process Flex File errors returned via LayoutReturn.
6879  */
6880 static void
nfsrv_flexlayouterr(struct nfsrv_descript * nd,uint32_t * layp,int maxcnt,NFSPROC_T * p)6881 nfsrv_flexlayouterr(struct nfsrv_descript *nd, uint32_t *layp, int maxcnt,
6882     NFSPROC_T *p)
6883 {
6884 	uint32_t *tl;
6885 	int cnt, errcnt, i, j, opnum, stat;
6886 	char devid[NFSX_V4DEVICEID];
6887 
6888 	tl = layp;
6889 	maxcnt -= NFSX_UNSIGNED;
6890 	if (maxcnt > 0)
6891 		cnt = fxdr_unsigned(int, *tl++);
6892 	else
6893 		cnt = 0;
6894 	NFSD_DEBUG(4, "flexlayouterr cnt=%d\n", cnt);
6895 	for (i = 0; i < cnt; i++) {
6896 		maxcnt -= NFSX_STATEID + 2 * NFSX_HYPER +
6897 		    NFSX_UNSIGNED;
6898 		if (maxcnt <= 0)
6899 			break;
6900 		/* Skip offset, length and stateid for now. */
6901 		tl += (4 + NFSX_STATEID / NFSX_UNSIGNED);
6902 		errcnt = fxdr_unsigned(int, *tl++);
6903 		NFSD_DEBUG(4, "flexlayouterr errcnt=%d\n", errcnt);
6904 		for (j = 0; j < errcnt; j++) {
6905 			maxcnt -= NFSX_V4DEVICEID + 2 * NFSX_UNSIGNED;
6906 			if (maxcnt < 0)
6907 				break;
6908 			NFSBCOPY(tl, devid, NFSX_V4DEVICEID);
6909 			tl += (NFSX_V4DEVICEID / NFSX_UNSIGNED);
6910 			stat = fxdr_unsigned(int, *tl++);
6911 			opnum = fxdr_unsigned(int, *tl++);
6912 			NFSD_DEBUG(4, "flexlayouterr op=%d stat=%d\n", opnum,
6913 			    stat);
6914 			/*
6915 			 * Except for NFSERR_ACCES, NFSERR_STALE and
6916 			 * NFSERR_NOSPC errors, disable the mirror.
6917 			 */
6918 			if (stat != NFSERR_ACCES && stat != NFSERR_STALE &&
6919 			    stat != NFSERR_NOSPC)
6920 				nfsrv_delds(devid, p);
6921 
6922 			/* For NFSERR_NOSPC, mark all devids and layouts. */
6923 			if (stat == NFSERR_NOSPC)
6924 				nfsrv_marknospc(devid, true);
6925 		}
6926 	}
6927 }
6928 
6929 /*
6930  * This function removes all flex file layouts which has a mirror with
6931  * a device id that matches the argument.
6932  * Called when the DS represented by the device id has failed.
6933  */
6934 void
nfsrv_flexmirrordel(char * devid,NFSPROC_T * p)6935 nfsrv_flexmirrordel(char *devid, NFSPROC_T *p)
6936 {
6937 	uint32_t *tl;
6938 	struct nfslayout *lyp, *nlyp;
6939 	struct nfslayouthash *lhyp;
6940 	struct nfslayouthead loclyp;
6941 	int i, j;
6942 
6943 	NFSD_DEBUG(4, "flexmirrordel\n");
6944 	/* Move all layouts found onto a local list. */
6945 	TAILQ_INIT(&loclyp);
6946 	for (i = 0; i < nfsrv_layouthashsize; i++) {
6947 		lhyp = &nfslayouthash[i];
6948 		NFSLOCKLAYOUT(lhyp);
6949 		TAILQ_FOREACH_SAFE(lyp, &lhyp->list, lay_list, nlyp) {
6950 			if (lyp->lay_type == NFSLAYOUT_FLEXFILE &&
6951 			    lyp->lay_mirrorcnt > 1) {
6952 				NFSD_DEBUG(4, "possible match\n");
6953 				tl = lyp->lay_xdr;
6954 				tl += 3;
6955 				for (j = 0; j < lyp->lay_mirrorcnt; j++) {
6956 					tl++;
6957 					if (NFSBCMP(devid, tl, NFSX_V4DEVICEID)
6958 					    == 0) {
6959 						/* Found one. */
6960 						NFSD_DEBUG(4, "fnd one\n");
6961 						TAILQ_REMOVE(&lhyp->list, lyp,
6962 						    lay_list);
6963 						TAILQ_INSERT_HEAD(&loclyp, lyp,
6964 						    lay_list);
6965 						break;
6966 					}
6967 					tl += (NFSX_V4DEVICEID / NFSX_UNSIGNED +
6968 					    NFSM_RNDUP(NFSX_V4PNFSFH) /
6969 					    NFSX_UNSIGNED + 11 * NFSX_UNSIGNED);
6970 				}
6971 			}
6972 		}
6973 		NFSUNLOCKLAYOUT(lhyp);
6974 	}
6975 
6976 	/* Now, try to do a Layout recall for each one found. */
6977 	TAILQ_FOREACH_SAFE(lyp, &loclyp, lay_list, nlyp) {
6978 		NFSD_DEBUG(4, "do layout recall\n");
6979 		/*
6980 		 * The layout stateid.seqid needs to be incremented
6981 		 * before doing a LAYOUT_RECALL callback.
6982 		 */
6983 		if (++lyp->lay_stateid.seqid == 0)
6984 			lyp->lay_stateid.seqid = 1;
6985 		nfsrv_recalllayout(lyp->lay_clientid, &lyp->lay_stateid,
6986 		    &lyp->lay_fh, lyp, 1, lyp->lay_type, p);
6987 		nfsrv_freelayout(&loclyp, lyp);
6988 	}
6989 }
6990 
6991 /*
6992  * Do a recall callback to the client for this layout.
6993  */
6994 static int
nfsrv_recalllayout(nfsquad_t clid,nfsv4stateid_t * stateidp,fhandle_t * fhp,struct nfslayout * lyp,int changed,int laytype,NFSPROC_T * p)6995 nfsrv_recalllayout(nfsquad_t clid, nfsv4stateid_t *stateidp, fhandle_t *fhp,
6996     struct nfslayout *lyp, int changed, int laytype, NFSPROC_T *p)
6997 {
6998 	struct nfsclient *clp;
6999 	int error;
7000 
7001 	NFSD_DEBUG(4, "nfsrv_recalllayout\n");
7002 	error = nfsrv_getclient(clid, 0, &clp, NULL, (nfsquad_t)((u_quad_t)0),
7003 	    0, NULL, p);
7004 	NFSD_DEBUG(4, "aft nfsrv_getclient=%d\n", error);
7005 	if (error != 0) {
7006 		printf("nfsrv_recalllayout: getclient err=%d\n", error);
7007 		return (error);
7008 	}
7009 	if ((clp->lc_flags & LCL_NFSV41) != 0) {
7010 		error = nfsrv_docallback(clp, NFSV4OP_CBLAYOUTRECALL,
7011 		    stateidp, changed, fhp, NULL, NULL, laytype, p);
7012 		/* If lyp != NULL, handle an error return here. */
7013 		if (error != 0 && lyp != NULL) {
7014 			NFSDRECALLLOCK();
7015 			/*
7016 			 * Mark it returned, since no layout recall
7017 			 * has been done.
7018 			 * All errors seem to be non-recoverable, although
7019 			 * NFSERR_NOMATCHLAYOUT is a normal event.
7020 			 */
7021 			if ((lyp->lay_flags & NFSLAY_RECALL) != 0) {
7022 				lyp->lay_flags |= NFSLAY_RETURNED;
7023 				wakeup(lyp);
7024 			}
7025 			NFSDRECALLUNLOCK();
7026 			if (error != NFSERR_NOMATCHLAYOUT)
7027 				printf("nfsrv_recalllayout: err=%d\n", error);
7028 		}
7029 	} else
7030 		printf("nfsrv_recalllayout: clp not NFSv4.1\n");
7031 	return (error);
7032 }
7033 
7034 /*
7035  * Find a layout to recall when we exceed our high water mark.
7036  */
7037 void
nfsrv_recalloldlayout(NFSPROC_T * p)7038 nfsrv_recalloldlayout(NFSPROC_T *p)
7039 {
7040 	struct nfslayouthash *lhyp;
7041 	struct nfslayout *lyp;
7042 	nfsquad_t clientid;
7043 	nfsv4stateid_t stateid;
7044 	fhandle_t fh;
7045 	int error, laytype = 0, ret;
7046 
7047 	lhyp = &nfslayouthash[arc4random() % nfsrv_layouthashsize];
7048 	NFSLOCKLAYOUT(lhyp);
7049 	TAILQ_FOREACH_REVERSE(lyp, &lhyp->list, nfslayouthead, lay_list) {
7050 		if ((lyp->lay_flags & NFSLAY_CALLB) == 0) {
7051 			lyp->lay_flags |= NFSLAY_CALLB;
7052 			/*
7053 			 * The layout stateid.seqid needs to be incremented
7054 			 * before doing a LAYOUT_RECALL callback.
7055 			 */
7056 			if (++lyp->lay_stateid.seqid == 0)
7057 				lyp->lay_stateid.seqid = 1;
7058 			clientid = lyp->lay_clientid;
7059 			stateid = lyp->lay_stateid;
7060 			NFSBCOPY(&lyp->lay_fh, &fh, sizeof(fh));
7061 			laytype = lyp->lay_type;
7062 			break;
7063 		}
7064 	}
7065 	NFSUNLOCKLAYOUT(lhyp);
7066 	if (lyp != NULL) {
7067 		error = nfsrv_recalllayout(clientid, &stateid, &fh, NULL, 0,
7068 		    laytype, p);
7069 		if (error != 0 && error != NFSERR_NOMATCHLAYOUT)
7070 			NFSD_DEBUG(4, "recallold=%d\n", error);
7071 		if (error != 0) {
7072 			NFSLOCKLAYOUT(lhyp);
7073 			/*
7074 			 * Since the hash list was unlocked, we need to
7075 			 * find it again.
7076 			 */
7077 			ret = nfsrv_findlayout(&clientid, &fh, laytype, p,
7078 			    &lyp);
7079 			if (ret == 0 &&
7080 			    (lyp->lay_flags & NFSLAY_CALLB) != 0 &&
7081 			    lyp->lay_stateid.other[0] == stateid.other[0] &&
7082 			    lyp->lay_stateid.other[1] == stateid.other[1] &&
7083 			    lyp->lay_stateid.other[2] == stateid.other[2]) {
7084 				/*
7085 				 * The client no longer knows this layout, so
7086 				 * it can be free'd now.
7087 				 */
7088 				if (error == NFSERR_NOMATCHLAYOUT)
7089 					nfsrv_freelayout(&lhyp->list, lyp);
7090 				else {
7091 					/*
7092 					 * Leave it to be tried later by
7093 					 * clearing NFSLAY_CALLB and moving
7094 					 * it to the head of the list, so it
7095 					 * won't be tried again for a while.
7096 					 */
7097 					lyp->lay_flags &= ~NFSLAY_CALLB;
7098 					TAILQ_REMOVE(&lhyp->list, lyp,
7099 					    lay_list);
7100 					TAILQ_INSERT_HEAD(&lhyp->list, lyp,
7101 					    lay_list);
7102 				}
7103 			}
7104 			NFSUNLOCKLAYOUT(lhyp);
7105 		}
7106 	}
7107 }
7108 
7109 /*
7110  * Try and return layout(s).
7111  */
7112 int
nfsrv_layoutreturn(struct nfsrv_descript * nd,vnode_t vp,int layouttype,int iomode,uint64_t offset,uint64_t len,int reclaim,int kind,nfsv4stateid_t * stateidp,int maxcnt,uint32_t * layp,int * fndp,struct ucred * cred,NFSPROC_T * p)7113 nfsrv_layoutreturn(struct nfsrv_descript *nd, vnode_t vp,
7114     int layouttype, int iomode, uint64_t offset, uint64_t len, int reclaim,
7115     int kind, nfsv4stateid_t *stateidp, int maxcnt, uint32_t *layp, int *fndp,
7116     struct ucred *cred, NFSPROC_T *p)
7117 {
7118 	struct nfsvattr na;
7119 	struct nfslayouthash *lhyp;
7120 	struct nfslayout *lyp;
7121 	fhandle_t fh;
7122 	int error = 0;
7123 
7124 	*fndp = 0;
7125 	if (kind == NFSV4LAYOUTRET_FILE) {
7126 		error = nfsvno_getfh(vp, &fh, p);
7127 		if (error == 0) {
7128 			error = nfsrv_updatemdsattr(vp, &na, p);
7129 			if (error != 0)
7130 				printf("nfsrv_layoutreturn: updatemdsattr"
7131 				    " failed=%d\n", error);
7132 		}
7133 		if (error == 0) {
7134 			if (reclaim == newnfs_true) {
7135 				error = nfsrv_checkgrace(NULL, NULL,
7136 				    NFSLCK_RECLAIM);
7137 				if (error != NFSERR_NOGRACE)
7138 					error = 0;
7139 				return (error);
7140 			}
7141 			lhyp = NFSLAYOUTHASH(&fh);
7142 			NFSDRECALLLOCK();
7143 			NFSLOCKLAYOUT(lhyp);
7144 			error = nfsrv_findlayout(&nd->nd_clientid, &fh,
7145 			    layouttype, p, &lyp);
7146 			NFSD_DEBUG(4, "layoutret findlay=%d\n", error);
7147 			if (error == 0 &&
7148 			    stateidp->other[0] == lyp->lay_stateid.other[0] &&
7149 			    stateidp->other[1] == lyp->lay_stateid.other[1] &&
7150 			    stateidp->other[2] == lyp->lay_stateid.other[2]) {
7151 				NFSD_DEBUG(4, "nfsrv_layoutreturn: stateid %d"
7152 				    " %x %x %x laystateid %d %x %x %x"
7153 				    " off=%ju len=%ju flgs=0x%x\n",
7154 				    stateidp->seqid, stateidp->other[0],
7155 				    stateidp->other[1], stateidp->other[2],
7156 				    lyp->lay_stateid.seqid,
7157 				    lyp->lay_stateid.other[0],
7158 				    lyp->lay_stateid.other[1],
7159 				    lyp->lay_stateid.other[2],
7160 				    (uintmax_t)offset, (uintmax_t)len,
7161 				    lyp->lay_flags);
7162 				if (++lyp->lay_stateid.seqid == 0)
7163 					lyp->lay_stateid.seqid = 1;
7164 				stateidp->seqid = lyp->lay_stateid.seqid;
7165 				if (offset == 0 && len == UINT64_MAX) {
7166 					if ((iomode & NFSLAYOUTIOMODE_READ) !=
7167 					    0)
7168 						lyp->lay_flags &= ~NFSLAY_READ;
7169 					if ((iomode & NFSLAYOUTIOMODE_RW) != 0)
7170 						lyp->lay_flags &= ~NFSLAY_RW;
7171 					if ((lyp->lay_flags & (NFSLAY_READ |
7172 					    NFSLAY_RW)) == 0)
7173 						nfsrv_freelayout(&lhyp->list,
7174 						    lyp);
7175 					else
7176 						*fndp = 1;
7177 				} else
7178 					*fndp = 1;
7179 			}
7180 			NFSUNLOCKLAYOUT(lhyp);
7181 			/* Search the nfsrv_recalllist for a match. */
7182 			TAILQ_FOREACH(lyp, &nfsrv_recalllisthead, lay_list) {
7183 				if (NFSBCMP(&lyp->lay_fh, &fh,
7184 				    sizeof(fh)) == 0 &&
7185 				    lyp->lay_clientid.qval ==
7186 				    nd->nd_clientid.qval &&
7187 				    stateidp->other[0] ==
7188 				    lyp->lay_stateid.other[0] &&
7189 				    stateidp->other[1] ==
7190 				    lyp->lay_stateid.other[1] &&
7191 				    stateidp->other[2] ==
7192 				    lyp->lay_stateid.other[2]) {
7193 					lyp->lay_flags |= NFSLAY_RETURNED;
7194 					wakeup(lyp);
7195 					error = 0;
7196 				}
7197 			}
7198 			NFSDRECALLUNLOCK();
7199 		}
7200 		if (layouttype == NFSLAYOUT_FLEXFILE && layp != NULL)
7201 			nfsrv_flexlayouterr(nd, layp, maxcnt, p);
7202 	} else if (kind == NFSV4LAYOUTRET_FSID)
7203 		nfsrv_freelayouts(&nd->nd_clientid,
7204 		    &vp->v_mount->mnt_stat.f_fsid, layouttype, iomode);
7205 	else if (kind == NFSV4LAYOUTRET_ALL)
7206 		nfsrv_freelayouts(&nd->nd_clientid, NULL, layouttype, iomode);
7207 	else
7208 		error = NFSERR_INVAL;
7209 	if (error == -1)
7210 		error = 0;
7211 	return (error);
7212 }
7213 
7214 /*
7215  * Look for an existing layout.
7216  */
7217 static int
nfsrv_findlayout(nfsquad_t * clientidp,fhandle_t * fhp,int laytype,NFSPROC_T * p,struct nfslayout ** lypp)7218 nfsrv_findlayout(nfsquad_t *clientidp, fhandle_t *fhp, int laytype,
7219     NFSPROC_T *p, struct nfslayout **lypp)
7220 {
7221 	struct nfslayouthash *lhyp;
7222 	struct nfslayout *lyp;
7223 	int ret;
7224 
7225 	*lypp = NULL;
7226 	ret = 0;
7227 	lhyp = NFSLAYOUTHASH(fhp);
7228 	TAILQ_FOREACH(lyp, &lhyp->list, lay_list) {
7229 		if (NFSBCMP(&lyp->lay_fh, fhp, sizeof(*fhp)) == 0 &&
7230 		    lyp->lay_clientid.qval == clientidp->qval &&
7231 		    lyp->lay_type == laytype)
7232 			break;
7233 	}
7234 	if (lyp != NULL)
7235 		*lypp = lyp;
7236 	else
7237 		ret = -1;
7238 	return (ret);
7239 }
7240 
7241 /*
7242  * Add the new layout, as required.
7243  */
7244 static int
nfsrv_addlayout(struct nfsrv_descript * nd,struct nfslayout ** lypp,nfsv4stateid_t * stateidp,char * layp,int * layoutlenp,NFSPROC_T * p)7245 nfsrv_addlayout(struct nfsrv_descript *nd, struct nfslayout **lypp,
7246     nfsv4stateid_t *stateidp, char *layp, int *layoutlenp, NFSPROC_T *p)
7247 {
7248 	struct nfsclient *clp;
7249 	struct nfslayouthash *lhyp;
7250 	struct nfslayout *lyp, *nlyp;
7251 	fhandle_t *fhp;
7252 	int error;
7253 
7254 	KASSERT((nd->nd_flag & ND_IMPLIEDCLID) != 0,
7255 	    ("nfsrv_layoutget: no nd_clientid\n"));
7256 	lyp = *lypp;
7257 	fhp = &lyp->lay_fh;
7258 	NFSLOCKSTATE();
7259 	error = nfsrv_getclient((nfsquad_t)((u_quad_t)0), CLOPS_RENEW, &clp,
7260 	    NULL, (nfsquad_t)((u_quad_t)0), 0, nd, p);
7261 	if (error != 0) {
7262 		NFSUNLOCKSTATE();
7263 		return (error);
7264 	}
7265 	lyp->lay_stateid.seqid = stateidp->seqid = 1;
7266 	lyp->lay_stateid.other[0] = stateidp->other[0] =
7267 	    clp->lc_clientid.lval[0];
7268 	lyp->lay_stateid.other[1] = stateidp->other[1] =
7269 	    clp->lc_clientid.lval[1];
7270 	lyp->lay_stateid.other[2] = stateidp->other[2] =
7271 	    nfsrv_nextstateindex(clp);
7272 	NFSUNLOCKSTATE();
7273 
7274 	lhyp = NFSLAYOUTHASH(fhp);
7275 	NFSLOCKLAYOUT(lhyp);
7276 	TAILQ_FOREACH(nlyp, &lhyp->list, lay_list) {
7277 		if (NFSBCMP(&nlyp->lay_fh, fhp, sizeof(*fhp)) == 0 &&
7278 		    nlyp->lay_clientid.qval == nd->nd_clientid.qval)
7279 			break;
7280 	}
7281 	if (nlyp != NULL) {
7282 		/* A layout already exists, so use it. */
7283 		nlyp->lay_flags |= (lyp->lay_flags & (NFSLAY_READ | NFSLAY_RW));
7284 		NFSBCOPY(nlyp->lay_xdr, layp, nlyp->lay_layoutlen);
7285 		*layoutlenp = nlyp->lay_layoutlen;
7286 		if (++nlyp->lay_stateid.seqid == 0)
7287 			nlyp->lay_stateid.seqid = 1;
7288 		stateidp->seqid = nlyp->lay_stateid.seqid;
7289 		stateidp->other[0] = nlyp->lay_stateid.other[0];
7290 		stateidp->other[1] = nlyp->lay_stateid.other[1];
7291 		stateidp->other[2] = nlyp->lay_stateid.other[2];
7292 		NFSUNLOCKLAYOUT(lhyp);
7293 		return (0);
7294 	}
7295 
7296 	/* Insert the new layout in the lists. */
7297 	*lypp = NULL;
7298 	atomic_add_int(&nfsrv_layoutcnt, 1);
7299 	VNET(nfsstatsv1_p)->srvlayouts++;
7300 	NFSBCOPY(lyp->lay_xdr, layp, lyp->lay_layoutlen);
7301 	*layoutlenp = lyp->lay_layoutlen;
7302 	TAILQ_INSERT_HEAD(&lhyp->list, lyp, lay_list);
7303 	NFSUNLOCKLAYOUT(lhyp);
7304 	return (0);
7305 }
7306 
7307 /*
7308  * Get the devinfo for a deviceid.
7309  */
7310 int
nfsrv_getdevinfo(char * devid,int layouttype,uint32_t * maxcnt,uint32_t * notify,int * devaddrlen,char ** devaddr)7311 nfsrv_getdevinfo(char *devid, int layouttype, uint32_t *maxcnt,
7312     uint32_t *notify, int *devaddrlen, char **devaddr)
7313 {
7314 	struct nfsdevice *ds;
7315 
7316 	if ((layouttype != NFSLAYOUT_NFSV4_1_FILES && layouttype !=
7317 	     NFSLAYOUT_FLEXFILE) ||
7318 	    (nfsrv_maxpnfsmirror > 1 && layouttype == NFSLAYOUT_NFSV4_1_FILES))
7319 		return (NFSERR_UNKNLAYOUTTYPE);
7320 
7321 	/*
7322 	 * Now, search for the device id.  Note that the structures won't go
7323 	 * away, but the order changes in the list.  As such, the lock only
7324 	 * needs to be held during the search through the list.
7325 	 */
7326 	NFSDDSLOCK();
7327 	TAILQ_FOREACH(ds, &nfsrv_devidhead, nfsdev_list) {
7328 		if (NFSBCMP(devid, ds->nfsdev_deviceid, NFSX_V4DEVICEID) == 0 &&
7329 		    ds->nfsdev_nmp != NULL)
7330 			break;
7331 	}
7332 	NFSDDSUNLOCK();
7333 	if (ds == NULL)
7334 		return (NFSERR_NOENT);
7335 
7336 	/* If the correct nfsdev_XXXXaddrlen is > 0, we have the device info. */
7337 	*devaddrlen = 0;
7338 	if (layouttype == NFSLAYOUT_NFSV4_1_FILES) {
7339 		*devaddrlen = ds->nfsdev_fileaddrlen;
7340 		*devaddr = ds->nfsdev_fileaddr;
7341 	} else if (layouttype == NFSLAYOUT_FLEXFILE) {
7342 		*devaddrlen = ds->nfsdev_flexaddrlen;
7343 		*devaddr = ds->nfsdev_flexaddr;
7344 	}
7345 	if (*devaddrlen == 0)
7346 		return (NFSERR_UNKNLAYOUTTYPE);
7347 
7348 	/*
7349 	 * The XDR overhead is 3 unsigned values: layout_type,
7350 	 * length_of_address and notify bitmap.
7351 	 * If the notify array is changed to not all zeros, the
7352 	 * count of unsigned values must be increased.
7353 	 */
7354 	if (*maxcnt > 0 && *maxcnt < NFSM_RNDUP(*devaddrlen) +
7355 	    3 * NFSX_UNSIGNED) {
7356 		*maxcnt = NFSM_RNDUP(*devaddrlen) + 3 * NFSX_UNSIGNED;
7357 		return (NFSERR_TOOSMALL);
7358 	}
7359 	return (0);
7360 }
7361 
7362 /*
7363  * Free a list of layout state structures.
7364  */
7365 static void
nfsrv_freelayoutlist(nfsquad_t clientid)7366 nfsrv_freelayoutlist(nfsquad_t clientid)
7367 {
7368 	struct nfslayouthash *lhyp;
7369 	struct nfslayout *lyp, *nlyp;
7370 	int i;
7371 
7372 	for (i = 0; i < nfsrv_layouthashsize; i++) {
7373 		lhyp = &nfslayouthash[i];
7374 		NFSLOCKLAYOUT(lhyp);
7375 		TAILQ_FOREACH_SAFE(lyp, &lhyp->list, lay_list, nlyp) {
7376 			if (lyp->lay_clientid.qval == clientid.qval)
7377 				nfsrv_freelayout(&lhyp->list, lyp);
7378 		}
7379 		NFSUNLOCKLAYOUT(lhyp);
7380 	}
7381 }
7382 
7383 /*
7384  * Free up a layout.
7385  */
7386 static void
nfsrv_freelayout(struct nfslayouthead * lhp,struct nfslayout * lyp)7387 nfsrv_freelayout(struct nfslayouthead *lhp, struct nfslayout *lyp)
7388 {
7389 
7390 	NFSD_DEBUG(4, "Freelayout=%p\n", lyp);
7391 	atomic_add_int(&nfsrv_layoutcnt, -1);
7392 	VNET(nfsstatsv1_p)->srvlayouts--;
7393 	TAILQ_REMOVE(lhp, lyp, lay_list);
7394 	free(lyp, M_NFSDSTATE);
7395 }
7396 
7397 /*
7398  * Free up a device id.
7399  */
7400 void
nfsrv_freeonedevid(struct nfsdevice * ds)7401 nfsrv_freeonedevid(struct nfsdevice *ds)
7402 {
7403 	int i;
7404 
7405 	atomic_add_int(&nfsrv_devidcnt, -1);
7406 	vrele(ds->nfsdev_dvp);
7407 	for (i = 0; i < nfsrv_dsdirsize; i++)
7408 		if (ds->nfsdev_dsdir[i] != NULL)
7409 			vrele(ds->nfsdev_dsdir[i]);
7410 	free(ds->nfsdev_fileaddr, M_NFSDSTATE);
7411 	free(ds->nfsdev_flexaddr, M_NFSDSTATE);
7412 	free(ds->nfsdev_host, M_NFSDSTATE);
7413 	free(ds, M_NFSDSTATE);
7414 }
7415 
7416 /*
7417  * Free up a device id and its mirrors.
7418  */
7419 static void
nfsrv_freedevid(struct nfsdevice * ds)7420 nfsrv_freedevid(struct nfsdevice *ds)
7421 {
7422 
7423 	TAILQ_REMOVE(&nfsrv_devidhead, ds, nfsdev_list);
7424 	nfsrv_freeonedevid(ds);
7425 }
7426 
7427 /*
7428  * Free all layouts and device ids.
7429  * Done when the nfsd threads are shut down since there may be a new
7430  * modified device id list created when the nfsd is restarted.
7431  */
7432 void
nfsrv_freealllayoutsanddevids(void)7433 nfsrv_freealllayoutsanddevids(void)
7434 {
7435 	struct nfsdontlist *mrp, *nmrp;
7436 	struct nfslayout *lyp, *nlyp;
7437 
7438 	/* Get rid of the deviceid structures. */
7439 	nfsrv_freealldevids();
7440 	TAILQ_INIT(&nfsrv_devidhead);
7441 	nfsrv_devidcnt = 0;
7442 
7443 	/* Get rid of all layouts. */
7444 	nfsrv_freealllayouts();
7445 
7446 	/* Get rid of any nfsdontlist entries. */
7447 	LIST_FOREACH_SAFE(mrp, &nfsrv_dontlisthead, nfsmr_list, nmrp)
7448 		free(mrp, M_NFSDSTATE);
7449 	LIST_INIT(&nfsrv_dontlisthead);
7450 	nfsrv_dontlistlen = 0;
7451 
7452 	/* Free layouts in the recall list. */
7453 	TAILQ_FOREACH_SAFE(lyp, &nfsrv_recalllisthead, lay_list, nlyp)
7454 		nfsrv_freelayout(&nfsrv_recalllisthead, lyp);
7455 	TAILQ_INIT(&nfsrv_recalllisthead);
7456 }
7457 
7458 /*
7459  * Free layouts that match the arguments.
7460  */
7461 static void
nfsrv_freelayouts(nfsquad_t * clid,fsid_t * fs,int laytype,int iomode)7462 nfsrv_freelayouts(nfsquad_t *clid, fsid_t *fs, int laytype, int iomode)
7463 {
7464 	struct nfslayouthash *lhyp;
7465 	struct nfslayout *lyp, *nlyp;
7466 	int i;
7467 
7468 	for (i = 0; i < nfsrv_layouthashsize; i++) {
7469 		lhyp = &nfslayouthash[i];
7470 		NFSLOCKLAYOUT(lhyp);
7471 		TAILQ_FOREACH_SAFE(lyp, &lhyp->list, lay_list, nlyp) {
7472 			if (clid->qval != lyp->lay_clientid.qval)
7473 				continue;
7474 			if (fs != NULL && fsidcmp(fs, &lyp->lay_fsid) != 0)
7475 				continue;
7476 			if (laytype != lyp->lay_type)
7477 				continue;
7478 			if ((iomode & NFSLAYOUTIOMODE_READ) != 0)
7479 				lyp->lay_flags &= ~NFSLAY_READ;
7480 			if ((iomode & NFSLAYOUTIOMODE_RW) != 0)
7481 				lyp->lay_flags &= ~NFSLAY_RW;
7482 			if ((lyp->lay_flags & (NFSLAY_READ | NFSLAY_RW)) == 0)
7483 				nfsrv_freelayout(&lhyp->list, lyp);
7484 		}
7485 		NFSUNLOCKLAYOUT(lhyp);
7486 	}
7487 }
7488 
7489 /*
7490  * Free all layouts for the argument file.
7491  */
7492 void
nfsrv_freefilelayouts(fhandle_t * fhp)7493 nfsrv_freefilelayouts(fhandle_t *fhp)
7494 {
7495 	struct nfslayouthash *lhyp;
7496 	struct nfslayout *lyp, *nlyp;
7497 
7498 	lhyp = NFSLAYOUTHASH(fhp);
7499 	NFSLOCKLAYOUT(lhyp);
7500 	TAILQ_FOREACH_SAFE(lyp, &lhyp->list, lay_list, nlyp) {
7501 		if (NFSBCMP(&lyp->lay_fh, fhp, sizeof(*fhp)) == 0)
7502 			nfsrv_freelayout(&lhyp->list, lyp);
7503 	}
7504 	NFSUNLOCKLAYOUT(lhyp);
7505 }
7506 
7507 /*
7508  * Free all layouts.
7509  */
7510 static void
nfsrv_freealllayouts(void)7511 nfsrv_freealllayouts(void)
7512 {
7513 	struct nfslayouthash *lhyp;
7514 	struct nfslayout *lyp, *nlyp;
7515 	int i;
7516 
7517 	for (i = 0; i < nfsrv_layouthashsize; i++) {
7518 		lhyp = &nfslayouthash[i];
7519 		NFSLOCKLAYOUT(lhyp);
7520 		TAILQ_FOREACH_SAFE(lyp, &lhyp->list, lay_list, nlyp)
7521 			nfsrv_freelayout(&lhyp->list, lyp);
7522 		NFSUNLOCKLAYOUT(lhyp);
7523 	}
7524 }
7525 
7526 /*
7527  * Look up the mount path for the DS server.
7528  */
7529 static int
nfsrv_setdsserver(char * dspathp,char * mdspathp,NFSPROC_T * p,struct nfsdevice ** dsp)7530 nfsrv_setdsserver(char *dspathp, char *mdspathp, NFSPROC_T *p,
7531     struct nfsdevice **dsp)
7532 {
7533 	struct nameidata nd;
7534 	struct nfsdevice *ds;
7535 	struct mount *mp;
7536 	int error, i;
7537 	char *dsdirpath;
7538 	size_t dsdirsize;
7539 
7540 	NFSD_DEBUG(4, "setdssrv path=%s\n", dspathp);
7541 	*dsp = NULL;
7542 	if (jailed(p->td_ucred)) {
7543 		printf("A pNFS nfsd cannot run in a jail\n");
7544 		return (EPERM);
7545 	}
7546 	NDINIT(&nd, LOOKUP, FOLLOW | LOCKSHARED | LOCKLEAF, UIO_SYSSPACE,
7547 	    dspathp);
7548 	error = namei(&nd);
7549 	NFSD_DEBUG(4, "lookup=%d\n", error);
7550 	if (error != 0)
7551 		return (error);
7552 	NDFREE_PNBUF(&nd);
7553 	if (nd.ni_vp->v_type != VDIR) {
7554 		vput(nd.ni_vp);
7555 		NFSD_DEBUG(4, "dspath not dir\n");
7556 		return (ENOTDIR);
7557 	}
7558 	if (strcmp(nd.ni_vp->v_mount->mnt_vfc->vfc_name, "nfs") != 0) {
7559 		vput(nd.ni_vp);
7560 		NFSD_DEBUG(4, "dspath not an NFS mount\n");
7561 		return (ENXIO);
7562 	}
7563 
7564 	/*
7565 	 * Allocate a DS server structure with the NFS mounted directory
7566 	 * vnode reference counted, so that a non-forced dismount will
7567 	 * fail with EBUSY.
7568 	 * This structure is always linked into the list, even if an error
7569 	 * is being returned.  The caller will free the entire list upon
7570 	 * an error return.
7571 	 */
7572 	*dsp = ds = malloc(sizeof(*ds) + nfsrv_dsdirsize * sizeof(vnode_t),
7573 	    M_NFSDSTATE, M_WAITOK | M_ZERO);
7574 	ds->nfsdev_dvp = nd.ni_vp;
7575 	ds->nfsdev_nmp = VFSTONFS(nd.ni_vp->v_mount);
7576 	NFSVOPUNLOCK(nd.ni_vp);
7577 
7578 	dsdirsize = strlen(dspathp) + 16;
7579 	dsdirpath = malloc(dsdirsize, M_TEMP, M_WAITOK);
7580 	/* Now, create the DS directory structures. */
7581 	for (i = 0; i < nfsrv_dsdirsize; i++) {
7582 		snprintf(dsdirpath, dsdirsize, "%s/ds%d", dspathp, i);
7583 		NDINIT(&nd, LOOKUP, FOLLOW | LOCKSHARED | LOCKLEAF,
7584 		    UIO_SYSSPACE, dsdirpath);
7585 		error = namei(&nd);
7586 		NFSD_DEBUG(4, "dsdirpath=%s lookup=%d\n", dsdirpath, error);
7587 		if (error != 0)
7588 			break;
7589 		NDFREE_PNBUF(&nd);
7590 		if (nd.ni_vp->v_type != VDIR) {
7591 			vput(nd.ni_vp);
7592 			error = ENOTDIR;
7593 			NFSD_DEBUG(4, "dsdirpath not a VDIR\n");
7594 			break;
7595 		}
7596 		if (strcmp(nd.ni_vp->v_mount->mnt_vfc->vfc_name, "nfs") != 0) {
7597 			vput(nd.ni_vp);
7598 			error = ENXIO;
7599 			NFSD_DEBUG(4, "dsdirpath not an NFS mount\n");
7600 			break;
7601 		}
7602 		ds->nfsdev_dsdir[i] = nd.ni_vp;
7603 		NFSVOPUNLOCK(nd.ni_vp);
7604 	}
7605 	free(dsdirpath, M_TEMP);
7606 
7607 	if (strlen(mdspathp) > 0) {
7608 		/*
7609 		 * This DS stores file for a specific MDS exported file
7610 		 * system.
7611 		 */
7612 		NDINIT(&nd, LOOKUP, FOLLOW | LOCKSHARED | LOCKLEAF,
7613 		    UIO_SYSSPACE, mdspathp);
7614 		error = namei(&nd);
7615 		NFSD_DEBUG(4, "mds lookup=%d\n", error);
7616 		if (error != 0)
7617 			goto out;
7618 		NDFREE_PNBUF(&nd);
7619 		if (nd.ni_vp->v_type != VDIR) {
7620 			vput(nd.ni_vp);
7621 			error = ENOTDIR;
7622 			NFSD_DEBUG(4, "mdspath not dir\n");
7623 			goto out;
7624 		}
7625 		mp = nd.ni_vp->v_mount;
7626 		if ((mp->mnt_flag & MNT_EXPORTED) == 0) {
7627 			vput(nd.ni_vp);
7628 			error = ENXIO;
7629 			NFSD_DEBUG(4, "mdspath not an exported fs\n");
7630 			goto out;
7631 		}
7632 		ds->nfsdev_mdsfsid = mp->mnt_stat.f_fsid;
7633 		ds->nfsdev_mdsisset = 1;
7634 		vput(nd.ni_vp);
7635 	}
7636 
7637 out:
7638 	TAILQ_INSERT_TAIL(&nfsrv_devidhead, ds, nfsdev_list);
7639 	atomic_add_int(&nfsrv_devidcnt, 1);
7640 	return (error);
7641 }
7642 
7643 /*
7644  * Look up the mount path for the DS server and delete it.
7645  */
7646 int
nfsrv_deldsserver(int op,char * dspathp,NFSPROC_T * p)7647 nfsrv_deldsserver(int op, char *dspathp, NFSPROC_T *p)
7648 {
7649 	struct mount *mp;
7650 	struct nfsmount *nmp;
7651 	struct nfsdevice *ds;
7652 	int error;
7653 
7654 	NFSD_DEBUG(4, "deldssrv path=%s\n", dspathp);
7655 	/*
7656 	 * Search for the path in the mount list.  Avoid looking the path
7657 	 * up, since this mount point may be hung, with associated locked
7658 	 * vnodes, etc.
7659 	 * Set NFSMNTP_CANCELRPCS so that any forced dismount will be blocked
7660 	 * until this completes.
7661 	 * As noted in the man page, this should be done before any forced
7662 	 * dismount on the mount point, but at least the handshake on
7663 	 * NFSMNTP_CANCELRPCS should make it safe.
7664 	 */
7665 	error = 0;
7666 	ds = NULL;
7667 	nmp = NULL;
7668 	mtx_lock(&mountlist_mtx);
7669 	TAILQ_FOREACH(mp, &mountlist, mnt_list) {
7670 		if (strcmp(mp->mnt_stat.f_mntonname, dspathp) == 0 &&
7671 		    strcmp(mp->mnt_stat.f_fstypename, "nfs") == 0 &&
7672 		    mp->mnt_data != NULL) {
7673 			nmp = VFSTONFS(mp);
7674 			NFSLOCKMNT(nmp);
7675 			if ((nmp->nm_privflag & (NFSMNTP_FORCEDISM |
7676 			     NFSMNTP_CANCELRPCS)) == 0) {
7677 				nmp->nm_privflag |= NFSMNTP_CANCELRPCS;
7678 				NFSUNLOCKMNT(nmp);
7679 			} else {
7680 				NFSUNLOCKMNT(nmp);
7681 				nmp = NULL;
7682 			}
7683 			break;
7684 		}
7685 	}
7686 	mtx_unlock(&mountlist_mtx);
7687 
7688 	if (nmp != NULL) {
7689 		ds = nfsrv_deldsnmp(op, nmp, p);
7690 		NFSD_DEBUG(4, "deldsnmp=%p\n", ds);
7691 		if (ds != NULL) {
7692 			nfsrv_killrpcs(nmp);
7693 			NFSD_DEBUG(4, "aft killrpcs\n");
7694 		} else
7695 			error = ENXIO;
7696 		NFSLOCKMNT(nmp);
7697 		nmp->nm_privflag &= ~NFSMNTP_CANCELRPCS;
7698 		wakeup(nmp);
7699 		NFSUNLOCKMNT(nmp);
7700 	} else
7701 		error = EINVAL;
7702 	return (error);
7703 }
7704 
7705 /*
7706  * Search for and remove a DS entry which matches the "nmp" argument.
7707  * The nfsdevice structure pointer is returned so that the caller can
7708  * free it via nfsrv_freeonedevid().
7709  * For the forced case, do not try to do LayoutRecalls, since the server
7710  * must be shut down now anyhow.
7711  */
7712 struct nfsdevice *
nfsrv_deldsnmp(int op,struct nfsmount * nmp,NFSPROC_T * p)7713 nfsrv_deldsnmp(int op, struct nfsmount *nmp, NFSPROC_T *p)
7714 {
7715 	struct nfsdevice *fndds;
7716 
7717 	NFSD_DEBUG(4, "deldsdvp\n");
7718 	NFSDDSLOCK();
7719 	if (op == PNFSDOP_FORCEDELDS)
7720 		fndds = nfsv4_findmirror(nmp);
7721 	else
7722 		fndds = nfsrv_findmirroredds(nmp);
7723 	if (fndds != NULL)
7724 		nfsrv_deleteds(fndds);
7725 	NFSDDSUNLOCK();
7726 	if (fndds != NULL) {
7727 		if (op != PNFSDOP_FORCEDELDS)
7728 			nfsrv_flexmirrordel(fndds->nfsdev_deviceid, p);
7729 		printf("pNFS server: mirror %s failed\n", fndds->nfsdev_host);
7730 	}
7731 	return (fndds);
7732 }
7733 
7734 /*
7735  * Similar to nfsrv_deldsnmp(), except that the DS is indicated by deviceid.
7736  * This function also calls nfsrv_killrpcs() to unblock RPCs on the mount
7737  * point.
7738  * Also, returns an error instead of the nfsdevice found.
7739  */
7740 int
nfsrv_delds(char * devid,NFSPROC_T * p)7741 nfsrv_delds(char *devid, NFSPROC_T *p)
7742 {
7743 	struct nfsdevice *ds, *fndds;
7744 	struct nfsmount *nmp;
7745 	int fndmirror;
7746 
7747 	NFSD_DEBUG(4, "delds\n");
7748 	/*
7749 	 * Search the DS server list for a match with devid.
7750 	 * Remove the DS entry if found and there is a mirror.
7751 	 */
7752 	fndds = NULL;
7753 	nmp = NULL;
7754 	fndmirror = 0;
7755 	NFSDDSLOCK();
7756 	TAILQ_FOREACH(ds, &nfsrv_devidhead, nfsdev_list) {
7757 		if (NFSBCMP(ds->nfsdev_deviceid, devid, NFSX_V4DEVICEID) == 0 &&
7758 		    ds->nfsdev_nmp != NULL) {
7759 			NFSD_DEBUG(4, "fnd main ds\n");
7760 			fndds = ds;
7761 			break;
7762 		}
7763 	}
7764 	if (fndds == NULL) {
7765 		NFSDDSUNLOCK();
7766 		return (ENXIO);
7767 	}
7768 	if (fndds->nfsdev_mdsisset == 0 && nfsrv_faildscnt > 0)
7769 		fndmirror = 1;
7770 	else if (fndds->nfsdev_mdsisset != 0) {
7771 		/* For the fsid is set case, search for a mirror. */
7772 		TAILQ_FOREACH(ds, &nfsrv_devidhead, nfsdev_list) {
7773 			if (ds != fndds && ds->nfsdev_nmp != NULL &&
7774 			    ds->nfsdev_mdsisset != 0 &&
7775 			    fsidcmp(&ds->nfsdev_mdsfsid,
7776 			    &fndds->nfsdev_mdsfsid) == 0) {
7777 				fndmirror = 1;
7778 				break;
7779 			}
7780 		}
7781 	}
7782 	if (fndmirror != 0) {
7783 		nmp = fndds->nfsdev_nmp;
7784 		NFSLOCKMNT(nmp);
7785 		if ((nmp->nm_privflag & (NFSMNTP_FORCEDISM |
7786 		     NFSMNTP_CANCELRPCS)) == 0) {
7787 			nmp->nm_privflag |= NFSMNTP_CANCELRPCS;
7788 			NFSUNLOCKMNT(nmp);
7789 			nfsrv_deleteds(fndds);
7790 		} else {
7791 			NFSUNLOCKMNT(nmp);
7792 			nmp = NULL;
7793 		}
7794 	}
7795 	NFSDDSUNLOCK();
7796 	if (nmp != NULL) {
7797 		nfsrv_flexmirrordel(fndds->nfsdev_deviceid, p);
7798 		printf("pNFS server: mirror %s failed\n", fndds->nfsdev_host);
7799 		nfsrv_killrpcs(nmp);
7800 		NFSLOCKMNT(nmp);
7801 		nmp->nm_privflag &= ~NFSMNTP_CANCELRPCS;
7802 		wakeup(nmp);
7803 		NFSUNLOCKMNT(nmp);
7804 		return (0);
7805 	}
7806 	return (ENXIO);
7807 }
7808 
7809 /*
7810  * Mark a DS as disabled by setting nfsdev_nmp = NULL.
7811  */
7812 static void
nfsrv_deleteds(struct nfsdevice * fndds)7813 nfsrv_deleteds(struct nfsdevice *fndds)
7814 {
7815 
7816 	NFSD_DEBUG(4, "deleteds: deleting a mirror\n");
7817 	fndds->nfsdev_nmp = NULL;
7818 	if (fndds->nfsdev_mdsisset == 0)
7819 		nfsrv_faildscnt--;
7820 }
7821 
7822 /*
7823  * Fill in the addr structures for the File and Flex File layouts.
7824  */
7825 static void
nfsrv_allocdevid(struct nfsdevice * ds,char * addr,char * dnshost)7826 nfsrv_allocdevid(struct nfsdevice *ds, char *addr, char *dnshost)
7827 {
7828 	uint32_t *tl;
7829 	char *netprot;
7830 	int addrlen;
7831 	static uint64_t new_devid = 0;
7832 
7833 	if (strchr(addr, ':') != NULL)
7834 		netprot = "tcp6";
7835 	else
7836 		netprot = "tcp";
7837 
7838 	/* Fill in the device id. */
7839 	NFSBCOPY(&nfsdev_time, ds->nfsdev_deviceid, sizeof(nfsdev_time));
7840 	new_devid++;
7841 	NFSBCOPY(&new_devid, &ds->nfsdev_deviceid[sizeof(nfsdev_time)],
7842 	    sizeof(new_devid));
7843 
7844 	/*
7845 	 * Fill in the file addr (actually the nfsv4_file_layout_ds_addr4
7846 	 * as defined in RFC5661) in XDR.
7847 	 */
7848 	addrlen = NFSM_RNDUP(strlen(addr)) + NFSM_RNDUP(strlen(netprot)) +
7849 	    6 * NFSX_UNSIGNED;
7850 	NFSD_DEBUG(4, "hn=%s addr=%s netprot=%s\n", dnshost, addr, netprot);
7851 	ds->nfsdev_fileaddrlen = addrlen;
7852 	tl = malloc(addrlen, M_NFSDSTATE, M_WAITOK | M_ZERO);
7853 	ds->nfsdev_fileaddr = (char *)tl;
7854 	*tl++ = txdr_unsigned(1);		/* One stripe with index 0. */
7855 	*tl++ = 0;
7856 	*tl++ = txdr_unsigned(1);		/* One multipath list */
7857 	*tl++ = txdr_unsigned(1);		/* with one entry in it. */
7858 	/* The netaddr for this one entry. */
7859 	*tl++ = txdr_unsigned(strlen(netprot));
7860 	NFSBCOPY(netprot, tl, strlen(netprot));
7861 	tl += (NFSM_RNDUP(strlen(netprot)) / NFSX_UNSIGNED);
7862 	*tl++ = txdr_unsigned(strlen(addr));
7863 	NFSBCOPY(addr, tl, strlen(addr));
7864 
7865 	/*
7866 	 * Fill in the flex file addr (actually the ff_device_addr4
7867 	 * as defined for Flexible File Layout) in XDR.
7868 	 */
7869 	addrlen = NFSM_RNDUP(strlen(addr)) + NFSM_RNDUP(strlen(netprot)) +
7870 	    14 * NFSX_UNSIGNED;
7871 	ds->nfsdev_flexaddrlen = addrlen;
7872 	tl = malloc(addrlen, M_NFSDSTATE, M_WAITOK | M_ZERO);
7873 	ds->nfsdev_flexaddr = (char *)tl;
7874 	*tl++ = txdr_unsigned(1);		/* One multipath entry. */
7875 	/* The netaddr for this one entry. */
7876 	*tl++ = txdr_unsigned(strlen(netprot));
7877 	NFSBCOPY(netprot, tl, strlen(netprot));
7878 	tl += (NFSM_RNDUP(strlen(netprot)) / NFSX_UNSIGNED);
7879 	*tl++ = txdr_unsigned(strlen(addr));
7880 	NFSBCOPY(addr, tl, strlen(addr));
7881 	tl += (NFSM_RNDUP(strlen(addr)) / NFSX_UNSIGNED);
7882 	*tl++ = txdr_unsigned(2);		/* Two NFS Versions. */
7883 	*tl++ = txdr_unsigned(NFS_VER4);	/* NFSv4. */
7884 	*tl++ = txdr_unsigned(NFSV42_MINORVERSION); /* Minor version 2. */
7885 	*tl++ = txdr_unsigned(nfs_srvmaxio);	/* DS max rsize. */
7886 	*tl++ = txdr_unsigned(nfs_srvmaxio);	/* DS max wsize. */
7887 	*tl++ = newnfs_true;			/* Tightly coupled. */
7888 	*tl++ = txdr_unsigned(NFS_VER4);	/* NFSv4. */
7889 	*tl++ = txdr_unsigned(NFSV41_MINORVERSION); /* Minor version 1. */
7890 	*tl++ = txdr_unsigned(nfs_srvmaxio);	/* DS max rsize. */
7891 	*tl++ = txdr_unsigned(nfs_srvmaxio);	/* DS max wsize. */
7892 	*tl = newnfs_true;			/* Tightly coupled. */
7893 
7894 	ds->nfsdev_hostnamelen = strlen(dnshost);
7895 	ds->nfsdev_host = malloc(ds->nfsdev_hostnamelen + 1, M_NFSDSTATE,
7896 	    M_WAITOK);
7897 	NFSBCOPY(dnshost, ds->nfsdev_host, ds->nfsdev_hostnamelen + 1);
7898 }
7899 
7900 /*
7901  * Create the device id list.
7902  * Return 0 if the nfsd threads are to run and ENXIO if the "-p" argument
7903  * is misconfigured.
7904  */
7905 int
nfsrv_createdevids(struct nfsd_nfsd_args * args,NFSPROC_T * p)7906 nfsrv_createdevids(struct nfsd_nfsd_args *args, NFSPROC_T *p)
7907 {
7908 	struct nfsdevice *ds;
7909 	char *addrp, *dnshostp, *dspathp, *mdspathp;
7910 	int error, i;
7911 
7912 	addrp = args->addr;
7913 	dnshostp = args->dnshost;
7914 	dspathp = args->dspath;
7915 	mdspathp = args->mdspath;
7916 	nfsrv_maxpnfsmirror = args->mirrorcnt;
7917 	if (addrp == NULL || dnshostp == NULL || dspathp == NULL ||
7918 	    mdspathp == NULL)
7919 		return (0);
7920 
7921 	/*
7922 	 * Loop around for each nul-terminated string in args->addr,
7923 	 * args->dnshost, args->dnspath and args->mdspath.
7924 	 */
7925 	while (addrp < (args->addr + args->addrlen) &&
7926 	    dnshostp < (args->dnshost + args->dnshostlen) &&
7927 	    dspathp < (args->dspath + args->dspathlen) &&
7928 	    mdspathp < (args->mdspath + args->mdspathlen)) {
7929 		error = nfsrv_setdsserver(dspathp, mdspathp, p, &ds);
7930 		if (error != 0) {
7931 			/* Free all DS servers. */
7932 			nfsrv_freealldevids();
7933 			nfsrv_devidcnt = 0;
7934 			return (ENXIO);
7935 		}
7936 		nfsrv_allocdevid(ds, addrp, dnshostp);
7937 		addrp += (strlen(addrp) + 1);
7938 		dnshostp += (strlen(dnshostp) + 1);
7939 		dspathp += (strlen(dspathp) + 1);
7940 		mdspathp += (strlen(mdspathp) + 1);
7941 	}
7942 	if (nfsrv_devidcnt < nfsrv_maxpnfsmirror) {
7943 		/* Free all DS servers. */
7944 		nfsrv_freealldevids();
7945 		nfsrv_devidcnt = 0;
7946 		nfsrv_maxpnfsmirror = 1;
7947 		return (ENXIO);
7948 	}
7949 	/* We can fail at most one less DS than the mirror level. */
7950 	nfsrv_faildscnt = nfsrv_maxpnfsmirror - 1;
7951 
7952 	/*
7953 	 * Allocate the nfslayout hash table now, since this is a pNFS server.
7954 	 * Make it 1% of the high water mark and at least 100.
7955 	 */
7956 	if (nfslayouthash == NULL) {
7957 		nfsrv_layouthashsize = nfsrv_layouthighwater / 100;
7958 		if (nfsrv_layouthashsize < 100)
7959 			nfsrv_layouthashsize = 100;
7960 		nfslayouthash = mallocarray(nfsrv_layouthashsize,
7961 		    sizeof(struct nfslayouthash), M_NFSDSESSION, M_WAITOK |
7962 		    M_ZERO);
7963 		for (i = 0; i < nfsrv_layouthashsize; i++) {
7964 			mtx_init(&nfslayouthash[i].mtx, "nfslm", NULL, MTX_DEF);
7965 			TAILQ_INIT(&nfslayouthash[i].list);
7966 		}
7967 	}
7968 	return (0);
7969 }
7970 
7971 /*
7972  * Free all device ids.
7973  */
7974 static void
nfsrv_freealldevids(void)7975 nfsrv_freealldevids(void)
7976 {
7977 	struct nfsdevice *ds, *nds;
7978 
7979 	TAILQ_FOREACH_SAFE(ds, &nfsrv_devidhead, nfsdev_list, nds)
7980 		nfsrv_freedevid(ds);
7981 }
7982 
7983 /*
7984  * Check to see if there is a Read/Write Layout plus either:
7985  * - A Write Delegation
7986  * or
7987  * - An Open with Write_access.
7988  * Return 1 if this is the case and 0 otherwise.
7989  * This function is used by nfsrv_proxyds() to decide if doing a Proxy
7990  * Getattr RPC to the Data Server (DS) is necessary.
7991  */
7992 #define	NFSCLIDVECSIZE	6
7993 int
nfsrv_checkdsattr(vnode_t vp,NFSPROC_T * p)7994 nfsrv_checkdsattr(vnode_t vp, NFSPROC_T *p)
7995 {
7996 	fhandle_t fh, *tfhp;
7997 	struct nfsstate *stp;
7998 	struct nfslayout *lyp;
7999 	struct nfslayouthash *lhyp;
8000 	struct nfslockhashhead *hp;
8001 	struct nfslockfile *lfp;
8002 	nfsquad_t clid[NFSCLIDVECSIZE];
8003 	int clidcnt, ret;
8004 
8005 	ret = nfsvno_getfh(vp, &fh, p);
8006 	if (ret != 0)
8007 		return (0);
8008 
8009 	/* First check for a Read/Write Layout. */
8010 	clidcnt = 0;
8011 	lhyp = NFSLAYOUTHASH(&fh);
8012 	NFSLOCKLAYOUT(lhyp);
8013 	TAILQ_FOREACH(lyp, &lhyp->list, lay_list) {
8014 		if (NFSBCMP(&lyp->lay_fh, &fh, sizeof(fh)) == 0 &&
8015 		    ((lyp->lay_flags & NFSLAY_RW) != 0 ||
8016 		     ((lyp->lay_flags & NFSLAY_READ) != 0 &&
8017 		      nfsrv_pnfsatime != 0))) {
8018 			if (clidcnt < NFSCLIDVECSIZE)
8019 				clid[clidcnt].qval = lyp->lay_clientid.qval;
8020 			clidcnt++;
8021 		}
8022 	}
8023 	NFSUNLOCKLAYOUT(lhyp);
8024 	if (clidcnt == 0) {
8025 		/* None found, so return 0. */
8026 		return (0);
8027 	}
8028 
8029 	/* Get the nfslockfile for this fh. */
8030 	NFSLOCKSTATE();
8031 	hp = NFSLOCKHASH(&fh);
8032 	LIST_FOREACH(lfp, hp, lf_hash) {
8033 		tfhp = &lfp->lf_fh;
8034 		if (NFSVNO_CMPFH(&fh, tfhp))
8035 			break;
8036 	}
8037 	if (lfp == NULL) {
8038 		/* None found, so return 0. */
8039 		NFSUNLOCKSTATE();
8040 		return (0);
8041 	}
8042 
8043 	/* Now, look for a Write delegation for this clientid. */
8044 	LIST_FOREACH(stp, &lfp->lf_deleg, ls_file) {
8045 		if ((stp->ls_flags & NFSLCK_DELEGWRITE) != 0 &&
8046 		    nfsrv_fndclid(clid, stp->ls_clp->lc_clientid, clidcnt) != 0)
8047 			break;
8048 	}
8049 	if (stp != NULL) {
8050 		/* Found one, so return 1. */
8051 		NFSUNLOCKSTATE();
8052 		return (1);
8053 	}
8054 
8055 	/* No Write delegation, so look for an Open with Write_access. */
8056 	LIST_FOREACH(stp, &lfp->lf_open, ls_file) {
8057 		KASSERT((stp->ls_flags & NFSLCK_OPEN) != 0,
8058 		    ("nfsrv_checkdsattr: Non-open in Open list\n"));
8059 		if ((stp->ls_flags & NFSLCK_WRITEACCESS) != 0 &&
8060 		    nfsrv_fndclid(clid, stp->ls_clp->lc_clientid, clidcnt) != 0)
8061 			break;
8062 	}
8063 	NFSUNLOCKSTATE();
8064 	if (stp != NULL)
8065 		return (1);
8066 	return (0);
8067 }
8068 
8069 /*
8070  * Look for a matching clientid in the vector. Return 1 if one might match.
8071  */
8072 static int
nfsrv_fndclid(nfsquad_t * clidvec,nfsquad_t clid,int clidcnt)8073 nfsrv_fndclid(nfsquad_t *clidvec, nfsquad_t clid, int clidcnt)
8074 {
8075 	int i;
8076 
8077 	/* If too many for the vector, return 1 since there might be a match. */
8078 	if (clidcnt > NFSCLIDVECSIZE)
8079 		return (1);
8080 
8081 	for (i = 0; i < clidcnt; i++)
8082 		if (clidvec[i].qval == clid.qval)
8083 			return (1);
8084 	return (0);
8085 }
8086 
8087 /*
8088  * Check the don't list for "vp" and see if issuing an rw layout is allowed.
8089  * Return 1 if issuing an rw layout isn't allowed, 0 otherwise.
8090  */
8091 static int
nfsrv_dontlayout(fhandle_t * fhp)8092 nfsrv_dontlayout(fhandle_t *fhp)
8093 {
8094 	struct nfsdontlist *mrp;
8095 	int ret;
8096 
8097 	if (nfsrv_dontlistlen == 0)
8098 		return (0);
8099 	ret = 0;
8100 	NFSDDONTLISTLOCK();
8101 	LIST_FOREACH(mrp, &nfsrv_dontlisthead, nfsmr_list) {
8102 		if (NFSBCMP(fhp, &mrp->nfsmr_fh, sizeof(*fhp)) == 0 &&
8103 		    (mrp->nfsmr_flags & NFSMR_DONTLAYOUT) != 0) {
8104 			ret = 1;
8105 			break;
8106 		}
8107 	}
8108 	NFSDDONTLISTUNLOCK();
8109 	return (ret);
8110 }
8111 
8112 #define	PNFSDS_COPYSIZ	65536
8113 /*
8114  * Create a new file on a DS and copy the contents of an extant DS file to it.
8115  * This can be used for recovery of a DS file onto a recovered DS.
8116  * The steps are:
8117  * - When called, the MDS file's vnode is locked, blocking LayoutGet operations.
8118  * - Disable issuing of read/write layouts for the file via the nfsdontlist,
8119  *   so that they will be disabled after the MDS file's vnode is unlocked.
8120  * - Set up the nfsrv_recalllist so that recall of read/write layouts can
8121  *   be done.
8122  * - Unlock the MDS file's vnode, so that the client(s) can perform proxied
8123  *   writes, LayoutCommits and LayoutReturns for the file when completing the
8124  *   LayoutReturn requested by the LayoutRecall callback.
8125  * - Issue a LayoutRecall callback for all read/write layouts and wait for
8126  *   them to be returned. (If the LayoutRecall callback replies
8127  *   NFSERR_NOMATCHLAYOUT, they are gone and no LayoutReturn is needed.)
8128  * - Exclusively lock the MDS file's vnode.  This ensures that no proxied
8129  *   writes are in progress or can occur during the DS file copy.
8130  *   It also blocks Setattr operations.
8131  * - Create the file on the recovered mirror.
8132  * - Copy the file from the operational DS.
8133  * - Copy any ACL from the MDS file to the new DS file.
8134  * - Set the modify time of the new DS file to that of the MDS file.
8135  * - Update the extended attribute for the MDS file.
8136  * - Enable issuing of rw layouts by deleting the nfsdontlist entry.
8137  * - The caller will unlock the MDS file's vnode allowing operations
8138  *   to continue normally, since it is now on the mirror again.
8139  */
8140 int
nfsrv_copymr(vnode_t vp,vnode_t fvp,vnode_t dvp,struct nfsdevice * ds,struct pnfsdsfile * pf,struct pnfsdsfile * wpf,int mirrorcnt,struct ucred * cred,NFSPROC_T * p)8141 nfsrv_copymr(vnode_t vp, vnode_t fvp, vnode_t dvp, struct nfsdevice *ds,
8142     struct pnfsdsfile *pf, struct pnfsdsfile *wpf, int mirrorcnt,
8143     struct ucred *cred, NFSPROC_T *p)
8144 {
8145 	struct nfsdontlist *mrp, *nmrp;
8146 	struct nfslayouthash *lhyp;
8147 	struct nfslayout *lyp, *nlyp;
8148 	struct nfslayouthead thl;
8149 	struct mount *mp, *tvmp;
8150 	struct acl *aclp;
8151 	struct vattr va;
8152 	struct timespec mtime;
8153 	fhandle_t fh;
8154 	vnode_t tvp;
8155 	off_t rdpos, wrpos;
8156 	ssize_t aresid;
8157 	char *dat;
8158 	int didprintf, ret, retacl, xfer;
8159 
8160 	ASSERT_VOP_LOCKED(fvp, "nfsrv_copymr fvp");
8161 	ASSERT_VOP_LOCKED(vp, "nfsrv_copymr vp");
8162 	/*
8163 	 * Allocate a nfsdontlist entry and set the NFSMR_DONTLAYOUT flag
8164 	 * so that no more RW layouts will get issued.
8165 	 */
8166 	ret = nfsvno_getfh(vp, &fh, p);
8167 	if (ret != 0) {
8168 		NFSD_DEBUG(4, "nfsrv_copymr: getfh=%d\n", ret);
8169 		return (ret);
8170 	}
8171 	nmrp = malloc(sizeof(*nmrp), M_NFSDSTATE, M_WAITOK);
8172 	nmrp->nfsmr_flags = NFSMR_DONTLAYOUT;
8173 	NFSBCOPY(&fh, &nmrp->nfsmr_fh, sizeof(fh));
8174 	NFSDDONTLISTLOCK();
8175 	LIST_FOREACH(mrp, &nfsrv_dontlisthead, nfsmr_list) {
8176 		if (NFSBCMP(&fh, &mrp->nfsmr_fh, sizeof(fh)) == 0)
8177 			break;
8178 	}
8179 	if (mrp == NULL) {
8180 		LIST_INSERT_HEAD(&nfsrv_dontlisthead, nmrp, nfsmr_list);
8181 		mrp = nmrp;
8182 		nmrp = NULL;
8183 		nfsrv_dontlistlen++;
8184 		NFSD_DEBUG(4, "nfsrv_copymr: in dontlist\n");
8185 	} else {
8186 		NFSDDONTLISTUNLOCK();
8187 		free(nmrp, M_NFSDSTATE);
8188 		NFSD_DEBUG(4, "nfsrv_copymr: dup dontlist\n");
8189 		return (ENXIO);
8190 	}
8191 	NFSDDONTLISTUNLOCK();
8192 
8193 	/*
8194 	 * Search for all RW layouts for this file.  Move them to the
8195 	 * recall list, so they can be recalled and their return noted.
8196 	 */
8197 	lhyp = NFSLAYOUTHASH(&fh);
8198 	NFSDRECALLLOCK();
8199 	NFSLOCKLAYOUT(lhyp);
8200 	TAILQ_FOREACH_SAFE(lyp, &lhyp->list, lay_list, nlyp) {
8201 		if (NFSBCMP(&lyp->lay_fh, &fh, sizeof(fh)) == 0 &&
8202 		    (lyp->lay_flags & NFSLAY_RW) != 0) {
8203 			TAILQ_REMOVE(&lhyp->list, lyp, lay_list);
8204 			TAILQ_INSERT_HEAD(&nfsrv_recalllisthead, lyp, lay_list);
8205 			lyp->lay_trycnt = 0;
8206 		}
8207 	}
8208 	NFSUNLOCKLAYOUT(lhyp);
8209 	NFSDRECALLUNLOCK();
8210 
8211 	ret = 0;
8212 	mp = tvmp = NULL;
8213 	didprintf = 0;
8214 	TAILQ_INIT(&thl);
8215 	/* Unlock the MDS vp, so that a LayoutReturn can be done on it. */
8216 	NFSVOPUNLOCK(vp);
8217 	/* Now, do a recall for all layouts not yet recalled. */
8218 tryagain:
8219 	NFSDRECALLLOCK();
8220 	TAILQ_FOREACH(lyp, &nfsrv_recalllisthead, lay_list) {
8221 		if (NFSBCMP(&lyp->lay_fh, &fh, sizeof(fh)) == 0 &&
8222 		    (lyp->lay_flags & NFSLAY_RECALL) == 0) {
8223 			lyp->lay_flags |= NFSLAY_RECALL;
8224 			/*
8225 			 * The layout stateid.seqid needs to be incremented
8226 			 * before doing a LAYOUT_RECALL callback.
8227 			 */
8228 			if (++lyp->lay_stateid.seqid == 0)
8229 				lyp->lay_stateid.seqid = 1;
8230 			NFSDRECALLUNLOCK();
8231 			nfsrv_recalllayout(lyp->lay_clientid, &lyp->lay_stateid,
8232 			    &lyp->lay_fh, lyp, 0, lyp->lay_type, p);
8233 			NFSD_DEBUG(4, "nfsrv_copymr: recalled layout\n");
8234 			goto tryagain;
8235 		}
8236 	}
8237 
8238 	/* Now wait for them to be returned. */
8239 tryagain2:
8240 	TAILQ_FOREACH(lyp, &nfsrv_recalllisthead, lay_list) {
8241 		if (NFSBCMP(&lyp->lay_fh, &fh, sizeof(fh)) == 0) {
8242 			if ((lyp->lay_flags & NFSLAY_RETURNED) != 0) {
8243 				TAILQ_REMOVE(&nfsrv_recalllisthead, lyp,
8244 				    lay_list);
8245 				TAILQ_INSERT_HEAD(&thl, lyp, lay_list);
8246 				NFSD_DEBUG(4,
8247 				    "nfsrv_copymr: layout returned\n");
8248 			} else {
8249 				lyp->lay_trycnt++;
8250 				ret = mtx_sleep(lyp, NFSDRECALLMUTEXPTR,
8251 				    PVFS | PCATCH, "nfsmrl", hz);
8252 				NFSD_DEBUG(4, "nfsrv_copymr: aft sleep=%d\n",
8253 				    ret);
8254 				if (ret == EINTR || ret == ERESTART)
8255 					break;
8256 				if ((lyp->lay_flags & NFSLAY_RETURNED) == 0) {
8257 					/*
8258 					 * Give up after 60sec and return
8259 					 * ENXIO, failing the copymr.
8260 					 * This layout will remain on the
8261 					 * recalllist.  It can only be cleared
8262 					 * by restarting the nfsd.
8263 					 * This seems the safe way to handle
8264 					 * it, since it cannot be safely copied
8265 					 * with an outstanding RW layout.
8266 					 */
8267 					if (lyp->lay_trycnt >= 60) {
8268 						ret = ENXIO;
8269 						break;
8270 					}
8271 					if (didprintf == 0) {
8272 						printf("nfsrv_copymr: layout "
8273 						    "not returned\n");
8274 						didprintf = 1;
8275 					}
8276 				}
8277 			}
8278 			goto tryagain2;
8279 		}
8280 	}
8281 	NFSDRECALLUNLOCK();
8282 	/* We can now get rid of the layouts that have been returned. */
8283 	TAILQ_FOREACH_SAFE(lyp, &thl, lay_list, nlyp)
8284 		nfsrv_freelayout(&thl, lyp);
8285 
8286 	/*
8287 	 * Do the vn_start_write() calls here, before the MDS vnode is
8288 	 * locked and the tvp is created (locked) in the NFS file system
8289 	 * that dvp is in.
8290 	 * For tvmp, this probably isn't necessary, since it will be an
8291 	 * NFS mount and they are not suspendable at this time.
8292 	 */
8293 	if (ret == 0)
8294 		ret = vn_start_write(vp, &mp, V_WAIT | V_PCATCH);
8295 	if (ret == 0) {
8296 		tvmp = dvp->v_mount;
8297 		ret = vn_start_write(NULL, &tvmp, V_WAIT | V_PCATCH);
8298 	}
8299 
8300 	/*
8301 	 * LK_EXCLUSIVE lock the MDS vnode, so that any
8302 	 * proxied writes through the MDS will be blocked until we have
8303 	 * completed the copy and update of the extended attributes.
8304 	 * This will also ensure that any attributes and ACL will not be
8305 	 * changed until the copy is complete.
8306 	 */
8307 	NFSVOPLOCK(vp, LK_EXCLUSIVE | LK_RETRY);
8308 	if (ret == 0 && VN_IS_DOOMED(vp)) {
8309 		NFSD_DEBUG(4, "nfsrv_copymr: lk_exclusive doomed\n");
8310 		ret = ESTALE;
8311 	}
8312 
8313 	/* Create the data file on the recovered DS. */
8314 	if (ret == 0)
8315 		ret = nfsrv_createdsfile(vp, &fh, pf, dvp, ds, cred, p, &tvp);
8316 
8317 	/* Copy the DS file, if created successfully. */
8318 	if (ret == 0) {
8319 		/*
8320 		 * Get any NFSv4 ACL on the MDS file, so that it can be set
8321 		 * on the new DS file.
8322 		 */
8323 		aclp = acl_alloc(M_WAITOK | M_ZERO);
8324 		retacl = VOP_GETACL(vp, ACL_TYPE_NFS4, aclp, cred, p);
8325 		if (retacl != 0 && retacl != ENOATTR)
8326 			NFSD_DEBUG(1, "nfsrv_copymr: vop_getacl=%d\n", retacl);
8327 		dat = malloc(PNFSDS_COPYSIZ, M_TEMP, M_WAITOK);
8328 		/* Malloc a block of 0s used to check for holes. */
8329 		if (nfsrv_zeropnfsdat == NULL)
8330 			nfsrv_zeropnfsdat = malloc(PNFSDS_COPYSIZ, M_TEMP,
8331 			    M_WAITOK | M_ZERO);
8332 		rdpos = wrpos = 0;
8333 		ret = VOP_GETATTR(fvp, &va, cred);
8334 		aresid = 0;
8335 		while (ret == 0 && aresid == 0) {
8336 			ret = vn_rdwr(UIO_READ, fvp, dat, PNFSDS_COPYSIZ,
8337 			    rdpos, UIO_SYSSPACE, IO_NODELOCKED, cred, NULL,
8338 			    &aresid, p);
8339 			xfer = PNFSDS_COPYSIZ - aresid;
8340 			if (ret == 0 && xfer > 0) {
8341 				rdpos += xfer;
8342 				/*
8343 				 * Skip the write for holes, except for the
8344 				 * last block.
8345 				 */
8346 				if (xfer < PNFSDS_COPYSIZ || rdpos ==
8347 				    va.va_size || NFSBCMP(dat,
8348 				    nfsrv_zeropnfsdat, PNFSDS_COPYSIZ) != 0)
8349 					ret = vn_rdwr(UIO_WRITE, tvp, dat, xfer,
8350 					    wrpos, UIO_SYSSPACE, IO_NODELOCKED,
8351 					    cred, NULL, NULL, p);
8352 				if (ret == 0)
8353 					wrpos += xfer;
8354 			}
8355 		}
8356 
8357 		/* If there is an ACL and the copy succeeded, set the ACL. */
8358 		if (ret == 0 && retacl == 0) {
8359 			ret = VOP_SETACL(tvp, ACL_TYPE_NFS4, aclp, cred, p);
8360 			/*
8361 			 * Don't consider these as errors, since VOP_GETACL()
8362 			 * can return an ACL when they are not actually
8363 			 * supported.  For example, for UFS, VOP_GETACL()
8364 			 * will return a trivial ACL based on the uid/gid/mode
8365 			 * when there is no ACL on the file.
8366 			 * This case should be recognized as a trivial ACL
8367 			 * by UFS's VOP_SETACL() and succeed, but...
8368 			 */
8369 			if (ret == ENOATTR || ret == EOPNOTSUPP || ret == EPERM)
8370 				ret = 0;
8371 		}
8372 
8373 		if (ret == 0)
8374 			ret = VOP_FSYNC(tvp, MNT_WAIT, p);
8375 
8376 		/* Set the DS data file's modify time that of the MDS file. */
8377 		if (ret == 0)
8378 			ret = VOP_GETATTR(vp, &va, cred);
8379 		if (ret == 0) {
8380 			mtime = va.va_mtime;
8381 			VATTR_NULL(&va);
8382 			va.va_mtime = mtime;
8383 			ret = VOP_SETATTR(tvp, &va, cred);
8384 		}
8385 
8386 		vput(tvp);
8387 		acl_free(aclp);
8388 		free(dat, M_TEMP);
8389 	}
8390 	if (tvmp != NULL)
8391 		vn_finished_write(tvmp);
8392 
8393 	/* Update the extended attributes for the newly created DS file. */
8394 	if (ret == 0)
8395 		ret = vn_extattr_set(vp, IO_NODELOCKED,
8396 		    EXTATTR_NAMESPACE_SYSTEM, "pnfsd.dsfile",
8397 		    sizeof(*wpf) * mirrorcnt, (char *)wpf, p);
8398 	if (mp != NULL)
8399 		vn_finished_write(mp);
8400 
8401 	/* Get rid of the dontlist entry, so that Layouts can be issued. */
8402 	NFSDDONTLISTLOCK();
8403 	LIST_REMOVE(mrp, nfsmr_list);
8404 	NFSDDONTLISTUNLOCK();
8405 	free(mrp, M_NFSDSTATE);
8406 	return (ret);
8407 }
8408 
8409 /*
8410  * Create a data storage file on the recovered DS.
8411  */
8412 static int
nfsrv_createdsfile(vnode_t vp,fhandle_t * fhp,struct pnfsdsfile * pf,vnode_t dvp,struct nfsdevice * ds,struct ucred * cred,NFSPROC_T * p,vnode_t * tvpp)8413 nfsrv_createdsfile(vnode_t vp, fhandle_t *fhp, struct pnfsdsfile *pf,
8414     vnode_t dvp, struct nfsdevice *ds, struct ucred *cred, NFSPROC_T *p,
8415     vnode_t *tvpp)
8416 {
8417 	struct vattr va, nva;
8418 	int error;
8419 
8420 	/* Make data file name based on FH. */
8421 	error = VOP_GETATTR(vp, &va, cred);
8422 	if (error == 0) {
8423 		/* Set the attributes for "vp" to Setattr the DS vp. */
8424 		VATTR_NULL(&nva);
8425 		nva.va_uid = va.va_uid;
8426 		nva.va_gid = va.va_gid;
8427 		nva.va_mode = va.va_mode;
8428 		nva.va_size = 0;
8429 		VATTR_NULL(&va);
8430 		va.va_type = VREG;
8431 		va.va_mode = nva.va_mode;
8432 		NFSD_DEBUG(4, "nfsrv_dscreatefile: dvp=%p pf=%p\n", dvp, pf);
8433 		error = nfsrv_dscreate(dvp, &va, &nva, fhp, pf, NULL,
8434 		    pf->dsf_filename, cred, p, tvpp);
8435 	}
8436 	return (error);
8437 }
8438 
8439 /*
8440  * Look up the MDS file shared locked, and then get the extended attribute
8441  * to find the extant DS file to be copied to the new mirror.
8442  * If successful, *vpp is set to the MDS file's vp and *nvpp is
8443  * set to a DS data file for the MDS file, both exclusively locked.
8444  * The "buf" argument has the pnfsdsfile structure from the MDS file
8445  * in it and buflen is set to its length.
8446  */
8447 int
nfsrv_mdscopymr(char * mdspathp,char * dspathp,char * curdspathp,char * buf,int * buflenp,char * fname,NFSPROC_T * p,struct vnode ** vpp,struct vnode ** nvpp,struct pnfsdsfile ** pfp,struct nfsdevice ** dsp,struct nfsdevice ** fdsp)8448 nfsrv_mdscopymr(char *mdspathp, char *dspathp, char *curdspathp, char *buf,
8449     int *buflenp, char *fname, NFSPROC_T *p, struct vnode **vpp,
8450     struct vnode **nvpp, struct pnfsdsfile **pfp, struct nfsdevice **dsp,
8451     struct nfsdevice **fdsp)
8452 {
8453 	struct nameidata nd;
8454 	struct vnode *vp, *curvp;
8455 	struct pnfsdsfile *pf;
8456 	struct nfsmount *nmp, *curnmp;
8457 	int dsdir, error, mirrorcnt, ippos;
8458 
8459 	vp = NULL;
8460 	curvp = NULL;
8461 	curnmp = NULL;
8462 	*dsp = NULL;
8463 	*fdsp = NULL;
8464 	if (dspathp == NULL && curdspathp != NULL)
8465 		return (EPERM);
8466 
8467 	/*
8468 	 * Look up the MDS file shared locked.  The lock will be upgraded
8469 	 * to an exclusive lock after any rw layouts have been returned.
8470 	 */
8471 	NFSD_DEBUG(4, "mdsopen path=%s\n", mdspathp);
8472 	NDINIT(&nd, LOOKUP, FOLLOW | LOCKSHARED | LOCKLEAF, UIO_SYSSPACE,
8473 	    mdspathp);
8474 	error = namei(&nd);
8475 	NFSD_DEBUG(4, "lookup=%d\n", error);
8476 	if (error != 0)
8477 		return (error);
8478 	NDFREE_PNBUF(&nd);
8479 	if (nd.ni_vp->v_type != VREG) {
8480 		vput(nd.ni_vp);
8481 		NFSD_DEBUG(4, "mdspath not reg\n");
8482 		return (EISDIR);
8483 	}
8484 	vp = nd.ni_vp;
8485 
8486 	if (curdspathp != NULL) {
8487 		/*
8488 		 * Look up the current DS path and find the nfsdev structure for
8489 		 * it.
8490 		 */
8491 		NFSD_DEBUG(4, "curmdsdev path=%s\n", curdspathp);
8492 		NDINIT(&nd, LOOKUP, FOLLOW | LOCKSHARED | LOCKLEAF,
8493 		    UIO_SYSSPACE, curdspathp);
8494 		error = namei(&nd);
8495 		NFSD_DEBUG(4, "ds lookup=%d\n", error);
8496 		if (error != 0) {
8497 			vput(vp);
8498 			return (error);
8499 		}
8500 		NDFREE_PNBUF(&nd);
8501 		if (nd.ni_vp->v_type != VDIR) {
8502 			vput(nd.ni_vp);
8503 			vput(vp);
8504 			NFSD_DEBUG(4, "curdspath not dir\n");
8505 			return (ENOTDIR);
8506 		}
8507 		if (strcmp(nd.ni_vp->v_mount->mnt_vfc->vfc_name, "nfs") != 0) {
8508 			vput(nd.ni_vp);
8509 			vput(vp);
8510 			NFSD_DEBUG(4, "curdspath not an NFS mount\n");
8511 			return (ENXIO);
8512 		}
8513 		curnmp = VFSTONFS(nd.ni_vp->v_mount);
8514 
8515 		/* Search the nfsdev list for a match. */
8516 		NFSDDSLOCK();
8517 		*fdsp = nfsv4_findmirror(curnmp);
8518 		NFSDDSUNLOCK();
8519 		if (*fdsp == NULL)
8520 			curnmp = NULL;
8521 		if (curnmp == NULL) {
8522 			vput(nd.ni_vp);
8523 			vput(vp);
8524 			NFSD_DEBUG(4, "mdscopymr: no current ds\n");
8525 			return (ENXIO);
8526 		}
8527 		curvp = nd.ni_vp;
8528 	}
8529 
8530 	if (dspathp != NULL) {
8531 		/* Look up the nfsdev path and find the nfsdev structure. */
8532 		NFSD_DEBUG(4, "mdsdev path=%s\n", dspathp);
8533 		NDINIT(&nd, LOOKUP, FOLLOW | LOCKSHARED | LOCKLEAF,
8534 		    UIO_SYSSPACE, dspathp);
8535 		error = namei(&nd);
8536 		NFSD_DEBUG(4, "ds lookup=%d\n", error);
8537 		if (error != 0) {
8538 			vput(vp);
8539 			if (curvp != NULL)
8540 				vput(curvp);
8541 			return (error);
8542 		}
8543 		NDFREE_PNBUF(&nd);
8544 		if (nd.ni_vp->v_type != VDIR || nd.ni_vp == curvp) {
8545 			vput(nd.ni_vp);
8546 			vput(vp);
8547 			if (curvp != NULL)
8548 				vput(curvp);
8549 			NFSD_DEBUG(4, "dspath not dir\n");
8550 			if (nd.ni_vp == curvp)
8551 				return (EPERM);
8552 			return (ENOTDIR);
8553 		}
8554 		if (strcmp(nd.ni_vp->v_mount->mnt_vfc->vfc_name, "nfs") != 0) {
8555 			vput(nd.ni_vp);
8556 			vput(vp);
8557 			if (curvp != NULL)
8558 				vput(curvp);
8559 			NFSD_DEBUG(4, "dspath not an NFS mount\n");
8560 			return (ENXIO);
8561 		}
8562 		nmp = VFSTONFS(nd.ni_vp->v_mount);
8563 
8564 		/*
8565 		 * Search the nfsdevice list for a match.  If curnmp == NULL,
8566 		 * this is a recovery and there must be a mirror.
8567 		 */
8568 		NFSDDSLOCK();
8569 		if (curnmp == NULL)
8570 			*dsp = nfsrv_findmirroredds(nmp);
8571 		else
8572 			*dsp = nfsv4_findmirror(nmp);
8573 		NFSDDSUNLOCK();
8574 		if (*dsp == NULL) {
8575 			vput(nd.ni_vp);
8576 			vput(vp);
8577 			if (curvp != NULL)
8578 				vput(curvp);
8579 			NFSD_DEBUG(4, "mdscopymr: no ds\n");
8580 			return (ENXIO);
8581 		}
8582 	} else {
8583 		nd.ni_vp = NULL;
8584 		nmp = NULL;
8585 	}
8586 
8587 	/*
8588 	 * Get a vp for an available DS data file using the extended
8589 	 * attribute on the MDS file.
8590 	 * If there is a valid entry for the new DS in the extended attribute
8591 	 * on the MDS file (as checked via the nmp argument),
8592 	 * nfsrv_dsgetsockmnt() returns EEXIST, so no copying will occur.
8593 	 */
8594 	error = nfsrv_dsgetsockmnt(vp, 0, buf, buflenp, &mirrorcnt, p,
8595 	    NULL, NULL, NULL, fname, nvpp, &nmp, curnmp, &ippos, &dsdir);
8596 	if (curvp != NULL)
8597 		vput(curvp);
8598 	if (nd.ni_vp == NULL) {
8599 		if (error == 0 && nmp != NULL) {
8600 			/* Search the nfsdev list for a match. */
8601 			NFSDDSLOCK();
8602 			*dsp = nfsrv_findmirroredds(nmp);
8603 			NFSDDSUNLOCK();
8604 		}
8605 		if (error == 0 && (nmp == NULL || *dsp == NULL)) {
8606 			if (nvpp != NULL && *nvpp != NULL) {
8607 				vput(*nvpp);
8608 				*nvpp = NULL;
8609 			}
8610 			error = ENXIO;
8611 		}
8612 	} else
8613 		vput(nd.ni_vp);
8614 
8615 	/*
8616 	 * When dspathp != NULL and curdspathp == NULL, this is a recovery
8617 	 * and is only allowed if there is a 0.0.0.0 IP address entry.
8618 	 * When curdspathp != NULL, the ippos will be set to that entry.
8619 	 */
8620 	if (error == 0 && dspathp != NULL && ippos == -1) {
8621 		if (nvpp != NULL && *nvpp != NULL) {
8622 			vput(*nvpp);
8623 			*nvpp = NULL;
8624 		}
8625 		error = ENXIO;
8626 	}
8627 	if (error == 0) {
8628 		*vpp = vp;
8629 
8630 		pf = (struct pnfsdsfile *)buf;
8631 		if (ippos == -1) {
8632 			/* If no zeroip pnfsdsfile, add one. */
8633 			ippos = *buflenp / sizeof(*pf);
8634 			*buflenp += sizeof(*pf);
8635 			pf += ippos;
8636 			pf->dsf_dir = dsdir;
8637 			strlcpy(pf->dsf_filename, fname,
8638 			    sizeof(pf->dsf_filename));
8639 		} else
8640 			pf += ippos;
8641 		*pfp = pf;
8642 	} else
8643 		vput(vp);
8644 	return (error);
8645 }
8646 
8647 /*
8648  * Search for a matching pnfsd mirror device structure, base on the nmp arg.
8649  * Return one if found, NULL otherwise.
8650  */
8651 static struct nfsdevice *
nfsrv_findmirroredds(struct nfsmount * nmp)8652 nfsrv_findmirroredds(struct nfsmount *nmp)
8653 {
8654 	struct nfsdevice *ds, *fndds;
8655 	int fndmirror;
8656 
8657 	mtx_assert(NFSDDSMUTEXPTR, MA_OWNED);
8658 	/*
8659 	 * Search the DS server list for a match with nmp.
8660 	 * Remove the DS entry if found and there is a mirror.
8661 	 */
8662 	fndds = NULL;
8663 	fndmirror = 0;
8664 	if (nfsrv_devidcnt == 0)
8665 		return (fndds);
8666 	TAILQ_FOREACH(ds, &nfsrv_devidhead, nfsdev_list) {
8667 		if (ds->nfsdev_nmp == nmp) {
8668 			NFSD_DEBUG(4, "nfsrv_findmirroredds: fnd main ds\n");
8669 			fndds = ds;
8670 			break;
8671 		}
8672 	}
8673 	if (fndds == NULL)
8674 		return (fndds);
8675 	if (fndds->nfsdev_mdsisset == 0 && nfsrv_faildscnt > 0)
8676 		fndmirror = 1;
8677 	else if (fndds->nfsdev_mdsisset != 0) {
8678 		/* For the fsid is set case, search for a mirror. */
8679 		TAILQ_FOREACH(ds, &nfsrv_devidhead, nfsdev_list) {
8680 			if (ds != fndds && ds->nfsdev_nmp != NULL &&
8681 			    ds->nfsdev_mdsisset != 0 &&
8682 			    fsidcmp(&ds->nfsdev_mdsfsid,
8683 			    &fndds->nfsdev_mdsfsid) == 0) {
8684 				fndmirror = 1;
8685 				break;
8686 			}
8687 		}
8688 	}
8689 	if (fndmirror == 0) {
8690 		NFSD_DEBUG(4, "nfsrv_findmirroredds: no mirror for DS\n");
8691 		return (NULL);
8692 	}
8693 	return (fndds);
8694 }
8695 
8696 /*
8697  * Mark the appropriate devid and all associated layout as "out of space".
8698  */
8699 void
nfsrv_marknospc(char * devid,bool setit)8700 nfsrv_marknospc(char *devid, bool setit)
8701 {
8702 	struct nfsdevice *ds;
8703 	struct nfslayout *lyp;
8704 	struct nfslayouthash *lhyp;
8705 	int i;
8706 
8707 	NFSDDSLOCK();
8708 	TAILQ_FOREACH(ds, &nfsrv_devidhead, nfsdev_list) {
8709 		if (NFSBCMP(ds->nfsdev_deviceid, devid, NFSX_V4DEVICEID) == 0) {
8710 			NFSD_DEBUG(1, "nfsrv_marknospc: devid %d\n", setit);
8711 			ds->nfsdev_nospc = setit;
8712 		}
8713 	}
8714 	NFSDDSUNLOCK();
8715 
8716 	for (i = 0; i < nfsrv_layouthashsize; i++) {
8717 		lhyp = &nfslayouthash[i];
8718 		NFSLOCKLAYOUT(lhyp);
8719 		TAILQ_FOREACH(lyp, &lhyp->list, lay_list) {
8720 			if (NFSBCMP(lyp->lay_deviceid, devid,
8721 			    NFSX_V4DEVICEID) == 0) {
8722 				NFSD_DEBUG(1, "nfsrv_marknospc: layout %d\n",
8723 				    setit);
8724 				if (setit)
8725 					lyp->lay_flags |= NFSLAY_NOSPC;
8726 				else
8727 					lyp->lay_flags &= ~NFSLAY_NOSPC;
8728 			}
8729 		}
8730 		NFSUNLOCKLAYOUT(lhyp);
8731 	}
8732 }
8733 
8734 /*
8735  * Check to see if SP4_MACH_CRED is in use and, if it is, check that the
8736  * correct machine credential is being used.
8737  */
8738 static int
nfsrv_checkmachcred(int op,struct nfsrv_descript * nd,struct nfsclient * clp)8739 nfsrv_checkmachcred(int op, struct nfsrv_descript *nd, struct nfsclient *clp)
8740 {
8741 
8742 	if ((clp->lc_flags & LCL_MACHCRED) == 0 ||
8743 	    !NFSISSET_OPBIT(&clp->lc_mustops, op))
8744 		return (0);
8745 	KASSERT((nd->nd_flag & ND_NFSV41) != 0,
8746 	    ("nfsrv_checkmachcred: MachCred for NFSv4.0"));
8747 	if ((nd->nd_flag & (ND_GSSINTEGRITY | ND_GSSPRIVACY)) != 0 &&
8748 	    nd->nd_princlen == clp->lc_namelen &&
8749 	    !NFSBCMP(nd->nd_principal, clp->lc_name, nd->nd_princlen))
8750 		return (0);
8751 	return (NFSERR_AUTHERR | AUTH_TOOWEAK);
8752 }
8753 
8754 /*
8755  * Issue a delegation and, optionally set rflagsp for why not.
8756  */
8757 static void
nfsrv_issuedelegation(struct vnode * vp,struct nfsclient * clp,struct nfsrv_descript * nd,int delegate,int writedeleg,int readonly,u_quad_t filerev,uint64_t rdonly,struct nfsstate ** new_delegp,struct nfsstate * new_stp,struct nfslockfile * lfp,uint32_t * rflagsp,nfsv4stateid_t * delegstateidp)8758 nfsrv_issuedelegation(struct vnode *vp, struct nfsclient *clp,
8759     struct nfsrv_descript *nd, int delegate, int writedeleg, int readonly,
8760     u_quad_t filerev, uint64_t rdonly, struct nfsstate **new_delegp,
8761     struct nfsstate *new_stp, struct nfslockfile *lfp, uint32_t *rflagsp,
8762     nfsv4stateid_t *delegstateidp)
8763 {
8764 	struct nfsstate *up_deleg, *new_deleg;
8765 
8766 	new_deleg = *new_delegp;
8767 	up_deleg = LIST_FIRST(&lfp->lf_deleg);
8768 	if ((new_stp->ls_flags & NFSLCK_WANTNODELEG) != 0)
8769 		*rflagsp |= NFSV4OPEN_WDNOTWANTED;
8770 	else if (nfsrv_issuedelegs == 0)
8771 		*rflagsp |= NFSV4OPEN_WDSUPPFTYPE;
8772 	else if (NFSRV_V4DELEGLIMIT(nfsrv_delegatecnt))
8773 		*rflagsp |= NFSV4OPEN_WDRESOURCE;
8774 	else if (delegate == 0 || !NFSVNO_DELEGOK(vp) ||
8775 	    (writedeleg == 0 && (readonly == 0 ||
8776 	    (new_stp->ls_flags & NFSLCK_WANTWDELEG) != 0)) ||
8777 	    (clp->lc_flags & (LCL_CALLBACKSON | LCL_CBDOWN)) !=
8778 	     LCL_CALLBACKSON) {
8779 		/* Is this a downgrade attempt? */
8780 		if (up_deleg != NULL && up_deleg->ls_clp == clp &&
8781 		    (up_deleg->ls_flags & NFSLCK_DELEGWRITE) != 0 &&
8782 		    (new_stp->ls_flags & NFSLCK_WANTRDELEG) != 0)
8783 			*rflagsp |= NFSV4OPEN_WDNOTSUPPDOWNGRADE;
8784 		else
8785 			*rflagsp |= NFSV4OPEN_WDCONTENTION;
8786 	} else if (up_deleg != NULL &&
8787 	    (up_deleg->ls_flags & NFSLCK_DELEGREAD) != 0 &&
8788 	    (new_stp->ls_flags & NFSLCK_WANTWDELEG) != 0) {
8789 		/* This is an atomic upgrade. */
8790 		up_deleg->ls_stateid.seqid++;
8791 		delegstateidp->seqid = up_deleg->ls_stateid.seqid;
8792 		delegstateidp->other[0] = up_deleg->ls_stateid.other[0];
8793 		delegstateidp->other[1] = up_deleg->ls_stateid.other[1];
8794 		delegstateidp->other[2] = up_deleg->ls_stateid.other[2];
8795 		up_deleg->ls_flags = (NFSLCK_DELEGWRITE |
8796 		    NFSLCK_READACCESS | NFSLCK_WRITEACCESS);
8797 		*rflagsp |= NFSV4OPEN_WRITEDELEGATE;
8798 		nfsrv_writedelegcnt++;
8799 	} else {
8800 		new_deleg->ls_stateid.seqid = delegstateidp->seqid = 1;
8801 		new_deleg->ls_stateid.other[0] = delegstateidp->other[0]
8802 		    = clp->lc_clientid.lval[0];
8803 		new_deleg->ls_stateid.other[1] = delegstateidp->other[1]
8804 		    = clp->lc_clientid.lval[1];
8805 		new_deleg->ls_stateid.other[2] = delegstateidp->other[2]
8806 		    = nfsrv_nextstateindex(clp);
8807 		if (writedeleg && !rdonly &&
8808 		    (nfsrv_writedelegifpos || !readonly) &&
8809 		    (new_stp->ls_flags & (NFSLCK_WANTRDELEG |
8810 		     NFSLCK_WANTWDELEG)) != NFSLCK_WANTRDELEG) {
8811 			new_deleg->ls_flags = (NFSLCK_DELEGWRITE |
8812 			    NFSLCK_READACCESS | NFSLCK_WRITEACCESS);
8813 			*rflagsp |= NFSV4OPEN_WRITEDELEGATE;
8814 			nfsrv_writedelegcnt++;
8815 		} else {
8816 			new_deleg->ls_flags = (NFSLCK_DELEGREAD |
8817 			    NFSLCK_READACCESS);
8818 			*rflagsp |= NFSV4OPEN_READDELEGATE;
8819 		}
8820 		new_deleg->ls_uid = new_stp->ls_uid;
8821 		new_deleg->ls_lfp = lfp;
8822 		new_deleg->ls_clp = clp;
8823 		new_deleg->ls_filerev = filerev;
8824 		new_deleg->ls_compref = nd->nd_compref;
8825 		new_deleg->ls_lastrecall = 0;
8826 		LIST_INSERT_HEAD(&lfp->lf_deleg, new_deleg, ls_file);
8827 		LIST_INSERT_HEAD(NFSSTATEHASH(clp, new_deleg->ls_stateid),
8828 		    new_deleg, ls_hash);
8829 		LIST_INSERT_HEAD(&clp->lc_deleg, new_deleg, ls_list);
8830 		*new_delegp = NULL;
8831 		VNET(nfsstatsv1_p)->srvdelegates++;
8832 		nfsrv_openpluslock++;
8833 		nfsrv_delegatecnt++;
8834 	}
8835 }
8836 
8837 /*
8838  * Find and remove any delegations for the fh.
8839  */
8840 void
nfsrv_removedeleg(fhandle_t * fhp,struct nfsrv_descript * nd,NFSPROC_T * p)8841 nfsrv_removedeleg(fhandle_t *fhp, struct nfsrv_descript *nd, NFSPROC_T *p)
8842 {
8843 	struct nfsclient *clp;
8844 	struct nfsstate *stp, *nstp;
8845 	struct nfslockfile *lfp;
8846 	int error;
8847 
8848 	NFSLOCKSTATE();
8849 	error = nfsrv_getclient(nd->nd_clientid, CLOPS_RENEW, &clp, NULL,
8850 	    (nfsquad_t)((u_quad_t)0), 0, nd, p);
8851 	if (error == 0)
8852 		error = nfsrv_getlockfile(NFSLCK_CHECK, NULL, &lfp, fhp, 0);
8853 	/*
8854 	 * Now we must free any delegations.
8855 	 */
8856 	if (error == 0) {
8857 		LIST_FOREACH_SAFE(stp, &lfp->lf_deleg, ls_file, nstp)
8858 			nfsrv_freedeleg(stp);
8859 	}
8860 	NFSUNLOCKSTATE();
8861 }
8862