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