2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2009 Rick Macklem, University of Guelph
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
34 #include <sys/extattr.h>
35 #include <fs/nfs/nfsport.h>
37 struct nfsrv_stablefirst nfsrv_stablefirst;
38 int nfsrv_issuedelegs = 0;
39 int nfsrv_dolocallocks = 0;
40 struct nfsv4lock nfsv4rootfs_lock;
41 time_t nfsdev_time = 0;
42 int nfsrv_layouthashsize;
43 volatile int nfsrv_layoutcnt = 0;
45 extern int newnfs_numnfsd;
46 extern struct nfsstatsv1 nfsstatsv1;
47 extern int nfsrv_lease;
48 extern struct timeval nfsboottime;
49 extern u_int32_t newnfs_true, newnfs_false;
50 extern struct mtx nfsrv_dslock_mtx;
51 extern struct mtx nfsrv_recalllock_mtx;
52 extern struct mtx nfsrv_dontlistlock_mtx;
53 extern int nfsd_debuglevel;
54 extern u_int nfsrv_dsdirsize;
55 extern struct nfsdevicehead nfsrv_devidhead;
56 extern int nfsrv_doflexfile;
57 extern int nfsrv_maxpnfsmirror;
60 extern struct nfsdontlisthead nfsrv_dontlisthead;
61 extern volatile int nfsrv_devidcnt;
62 extern struct nfslayouthead nfsrv_recalllisthead;
63 extern char *nfsrv_zeropnfsdat;
65 SYSCTL_DECL(_vfs_nfsd);
66 int nfsrv_statehashsize = NFSSTATEHASHSIZE;
67 SYSCTL_INT(_vfs_nfsd, OID_AUTO, statehashsize, CTLFLAG_RDTUN,
68 &nfsrv_statehashsize, 0,
69 "Size of state hash table set via loader.conf");
71 int nfsrv_clienthashsize = NFSCLIENTHASHSIZE;
72 SYSCTL_INT(_vfs_nfsd, OID_AUTO, clienthashsize, CTLFLAG_RDTUN,
73 &nfsrv_clienthashsize, 0,
74 "Size of client hash table set via loader.conf");
76 int nfsrv_lockhashsize = NFSLOCKHASHSIZE;
77 SYSCTL_INT(_vfs_nfsd, OID_AUTO, fhhashsize, CTLFLAG_RDTUN,
78 &nfsrv_lockhashsize, 0,
79 "Size of file handle hash table set via loader.conf");
81 int nfsrv_sessionhashsize = NFSSESSIONHASHSIZE;
82 SYSCTL_INT(_vfs_nfsd, OID_AUTO, sessionhashsize, CTLFLAG_RDTUN,
83 &nfsrv_sessionhashsize, 0,
84 "Size of session hash table set via loader.conf");
86 int nfsrv_layouthighwater = NFSLAYOUTHIGHWATER;
87 SYSCTL_INT(_vfs_nfsd, OID_AUTO, layouthighwater, CTLFLAG_RDTUN,
88 &nfsrv_layouthighwater, 0,
89 "High water mark for number of layouts set via loader.conf");
91 static int nfsrv_v4statelimit = NFSRV_V4STATELIMIT;
92 SYSCTL_INT(_vfs_nfsd, OID_AUTO, v4statelimit, CTLFLAG_RWTUN,
93 &nfsrv_v4statelimit, 0,
94 "High water limit for NFSv4 opens+locks+delegations");
96 static int nfsrv_writedelegifpos = 0;
97 SYSCTL_INT(_vfs_nfsd, OID_AUTO, writedelegifpos, CTLFLAG_RW,
98 &nfsrv_writedelegifpos, 0,
99 "Issue a write delegation for read opens if possible");
101 static int nfsrv_allowreadforwriteopen = 1;
102 SYSCTL_INT(_vfs_nfsd, OID_AUTO, allowreadforwriteopen, CTLFLAG_RW,
103 &nfsrv_allowreadforwriteopen, 0,
104 "Allow Reads to be done with Write Access StateIDs");
106 int nfsrv_pnfsatime = 0;
107 SYSCTL_INT(_vfs_nfsd, OID_AUTO, pnfsstrictatime, CTLFLAG_RW,
109 "For pNFS service, do Getattr ops to keep atime up-to-date");
111 int nfsrv_flexlinuxhack = 0;
112 SYSCTL_INT(_vfs_nfsd, OID_AUTO, flexlinuxhack, CTLFLAG_RW,
113 &nfsrv_flexlinuxhack, 0,
114 "For Linux clients, hack around Flex File Layout bug");
117 * Hash lists for nfs V4.
119 struct nfsclienthashhead *nfsclienthash;
120 struct nfslockhashhead *nfslockhash;
121 struct nfssessionhash *nfssessionhash;
122 struct nfslayouthash *nfslayouthash;
123 volatile int nfsrv_dontlistlen = 0;
124 #endif /* !APPLEKEXT */
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;
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,
139 static int nfsrv_freeopen(struct nfsstate *stp, vnode_t vp, int cansleep,
141 static void nfsrv_freelockowner(struct nfsstate *stp, vnode_t vp, int cansleep,
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,
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 static u_int32_t nfsrv_nextclientindex(void);
171 static u_int32_t nfsrv_nextstateindex(struct nfsclient *clp);
172 static void nfsrv_markstable(struct nfsclient *clp);
173 static void nfsrv_markreclaim(struct nfsclient *clp);
174 static int nfsrv_checkstable(struct nfsclient *clp);
175 static int nfsrv_clientconflict(struct nfsclient *clp, int *haslockp, struct
176 vnode *vp, NFSPROC_T *p);
177 static int nfsrv_delegconflict(struct nfsstate *stp, int *haslockp,
178 NFSPROC_T *p, vnode_t vp);
179 static int nfsrv_cleandeleg(vnode_t vp, struct nfslockfile *lfp,
180 struct nfsclient *clp, int *haslockp, NFSPROC_T *p);
181 static int nfsrv_notsamecredname(struct nfsrv_descript *nd,
182 struct nfsclient *clp);
183 static time_t nfsrv_leaseexpiry(void);
184 static void nfsrv_delaydelegtimeout(struct nfsstate *stp);
185 static int nfsrv_checkseqid(struct nfsrv_descript *nd, u_int32_t seqid,
186 struct nfsstate *stp, struct nfsrvcache *op);
187 static int nfsrv_nootherstate(struct nfsstate *stp);
188 static int nfsrv_locallock(vnode_t vp, struct nfslockfile *lfp, int flags,
189 uint64_t first, uint64_t end, struct nfslockconflict *cfp, NFSPROC_T *p);
190 static void nfsrv_localunlock(vnode_t vp, struct nfslockfile *lfp,
191 uint64_t init_first, uint64_t init_end, NFSPROC_T *p);
192 static int nfsrv_dolocal(vnode_t vp, struct nfslockfile *lfp, int flags,
193 int oldflags, uint64_t first, uint64_t end, struct nfslockconflict *cfp,
195 static void nfsrv_locallock_rollback(vnode_t vp, struct nfslockfile *lfp,
197 static void nfsrv_locallock_commit(struct nfslockfile *lfp, int flags,
198 uint64_t first, uint64_t end);
199 static void nfsrv_locklf(struct nfslockfile *lfp);
200 static void nfsrv_unlocklf(struct nfslockfile *lfp);
201 static struct nfsdsession *nfsrv_findsession(uint8_t *sessionid);
202 static int nfsrv_freesession(struct nfsdsession *sep, uint8_t *sessionid);
203 static int nfsv4_setcbsequence(struct nfsrv_descript *nd, struct nfsclient *clp,
204 int dont_replycache, struct nfsdsession **sepp);
205 static int nfsv4_getcbsession(struct nfsclient *clp, struct nfsdsession **sepp);
206 static int nfsrv_addlayout(struct nfsrv_descript *nd, struct nfslayout **lypp,
207 nfsv4stateid_t *stateidp, char *layp, int *layoutlenp, NFSPROC_T *p);
208 static void nfsrv_freelayout(struct nfslayouthead *lhp, struct nfslayout *lyp);
209 static void nfsrv_freelayoutlist(nfsquad_t clientid);
210 static void nfsrv_freelayouts(nfsquad_t *clid, fsid_t *fs, int laytype,
212 static void nfsrv_freealllayouts(void);
213 static void nfsrv_freedevid(struct nfsdevice *ds);
214 static int nfsrv_setdsserver(char *dspathp, char *mdspathp, NFSPROC_T *p,
215 struct nfsdevice **dsp);
216 static int nfsrv_delds(char *devid, NFSPROC_T *p);
217 static void nfsrv_deleteds(struct nfsdevice *fndds);
218 static void nfsrv_allocdevid(struct nfsdevice *ds, char *addr, char *dnshost);
219 static void nfsrv_freealldevids(void);
220 static void nfsrv_flexlayouterr(struct nfsrv_descript *nd, uint32_t *layp,
221 int maxcnt, NFSPROC_T *p);
222 static int nfsrv_recalllayout(nfsquad_t clid, nfsv4stateid_t *stateidp,
223 fhandle_t *fhp, struct nfslayout *lyp, int changed, int laytype,
225 static int nfsrv_findlayout(nfsquad_t *clientidp, fhandle_t *fhp, int laytype,
226 NFSPROC_T *, struct nfslayout **lypp);
227 static int nfsrv_fndclid(nfsquad_t *clidvec, nfsquad_t clid, int clidcnt);
228 static struct nfslayout *nfsrv_filelayout(struct nfsrv_descript *nd, int iomode,
229 fhandle_t *fhp, fhandle_t *dsfhp, char *devid, fsid_t fs);
230 static struct nfslayout *nfsrv_flexlayout(struct nfsrv_descript *nd, int iomode,
231 int mirrorcnt, fhandle_t *fhp, fhandle_t *dsfhp, char *devid, fsid_t fs);
232 static int nfsrv_dontlayout(fhandle_t *fhp);
233 static int nfsrv_createdsfile(vnode_t vp, fhandle_t *fhp, struct pnfsdsfile *pf,
234 vnode_t dvp, struct nfsdevice *ds, struct ucred *cred, NFSPROC_T *p,
236 static struct nfsdevice *nfsrv_findmirroredds(struct nfsmount *nmp);
239 * Scan the client list for a match and either return the current one,
240 * create a new entry or return an error.
241 * If returning a non-error, the clp structure must either be linked into
242 * the client list or free'd.
245 nfsrv_setclient(struct nfsrv_descript *nd, struct nfsclient **new_clpp,
246 nfsquad_t *clientidp, nfsquad_t *confirmp, NFSPROC_T *p)
248 struct nfsclient *clp = NULL, *new_clp = *new_clpp;
249 int i, error = 0, ret;
250 struct nfsstate *stp, *tstp;
251 struct sockaddr_in *sad, *rad;
252 struct nfsdsession *sep, *nsep;
253 int zapit = 0, gotit, hasstate = 0, igotlock;
254 static u_int64_t confirm_index = 0;
257 * Check for state resource limit exceeded.
259 if (nfsrv_openpluslock > nfsrv_v4statelimit) {
260 error = NFSERR_RESOURCE;
264 if (nfsrv_issuedelegs == 0 ||
265 ((nd->nd_flag & ND_GSS) != 0 && nfsrv_nogsscallback != 0))
267 * Don't do callbacks when delegations are disabled or
268 * for AUTH_GSS unless enabled via nfsrv_nogsscallback.
269 * If establishing a callback connection is attempted
270 * when a firewall is blocking the callback path, the
271 * server may wait too long for the connect attempt to
272 * succeed during the Open. Some clients, such as Linux,
273 * may timeout and give up on the Open before the server
274 * replies. Also, since AUTH_GSS callbacks are not
275 * yet interoperability tested, they might cause the
276 * server to crap out, if they get past the Init call to
279 new_clp->lc_program = 0;
281 /* Lock out other nfsd threads */
282 NFSLOCKV4ROOTMUTEX();
283 nfsv4_relref(&nfsv4rootfs_lock);
285 igotlock = nfsv4_lock(&nfsv4rootfs_lock, 1, NULL,
286 NFSV4ROOTLOCKMUTEXPTR, NULL);
288 NFSUNLOCKV4ROOTMUTEX();
291 * Search for a match in the client list.
294 while (i < nfsrv_clienthashsize && !gotit) {
295 LIST_FOREACH(clp, &nfsclienthash[i], lc_hash) {
296 if (new_clp->lc_idlen == clp->lc_idlen &&
297 !NFSBCMP(new_clp->lc_id, clp->lc_id, clp->lc_idlen)) {
306 (clp->lc_flags & (LCL_NEEDSCONFIRM | LCL_ADMINREVOKED))) {
307 if ((nd->nd_flag & ND_NFSV41) != 0 && confirmp->lval[1] != 0) {
309 * For NFSv4.1, if confirmp->lval[1] is non-zero, the
310 * client is trying to update a confirmed clientid.
312 NFSLOCKV4ROOTMUTEX();
313 nfsv4_unlock(&nfsv4rootfs_lock, 1);
314 NFSUNLOCKV4ROOTMUTEX();
315 confirmp->lval[1] = 0;
316 error = NFSERR_NOENT;
320 * Get rid of the old one.
322 if (i != nfsrv_clienthashsize) {
323 LIST_REMOVE(clp, lc_hash);
324 nfsrv_cleanclient(clp, p);
325 nfsrv_freedeleglist(&clp->lc_deleg);
326 nfsrv_freedeleglist(&clp->lc_olddeleg);
330 * Add it after assigning a client id to it.
332 new_clp->lc_flags |= LCL_NEEDSCONFIRM;
333 if ((nd->nd_flag & ND_NFSV41) != 0)
334 new_clp->lc_confirm.lval[0] = confirmp->lval[0] =
337 confirmp->qval = new_clp->lc_confirm.qval =
339 clientidp->lval[0] = new_clp->lc_clientid.lval[0] =
340 (u_int32_t)nfsrvboottime;
341 clientidp->lval[1] = new_clp->lc_clientid.lval[1] =
342 nfsrv_nextclientindex();
343 new_clp->lc_stateindex = 0;
344 new_clp->lc_statemaxindex = 0;
345 new_clp->lc_cbref = 0;
346 new_clp->lc_expiry = nfsrv_leaseexpiry();
347 LIST_INIT(&new_clp->lc_open);
348 LIST_INIT(&new_clp->lc_deleg);
349 LIST_INIT(&new_clp->lc_olddeleg);
350 LIST_INIT(&new_clp->lc_session);
351 for (i = 0; i < nfsrv_statehashsize; i++)
352 LIST_INIT(&new_clp->lc_stateid[i]);
353 LIST_INSERT_HEAD(NFSCLIENTHASH(new_clp->lc_clientid), new_clp,
355 nfsstatsv1.srvclients++;
356 nfsrv_openpluslock++;
358 NFSLOCKV4ROOTMUTEX();
359 nfsv4_unlock(&nfsv4rootfs_lock, 1);
360 NFSUNLOCKV4ROOTMUTEX();
362 nfsrv_zapclient(clp, p);
368 * Now, handle the cases where the id is already issued.
370 if (nfsrv_notsamecredname(nd, clp)) {
372 * Check to see if there is expired state that should go away.
374 if (clp->lc_expiry < NFSD_MONOSEC &&
375 (!LIST_EMPTY(&clp->lc_open) || !LIST_EMPTY(&clp->lc_deleg))) {
376 nfsrv_cleanclient(clp, p);
377 nfsrv_freedeleglist(&clp->lc_deleg);
381 * If there is outstanding state, then reply NFSERR_CLIDINUSE per
382 * RFC3530 Sec. 8.1.2 last para.
384 if (!LIST_EMPTY(&clp->lc_deleg)) {
386 } else if (LIST_EMPTY(&clp->lc_open)) {
390 /* Look for an Open on the OpenOwner */
391 LIST_FOREACH(stp, &clp->lc_open, ls_list) {
392 if (!LIST_EMPTY(&stp->ls_open)) {
400 * If the uid doesn't match, return NFSERR_CLIDINUSE after
401 * filling out the correct ipaddr and portnum.
403 sad = NFSSOCKADDR(new_clp->lc_req.nr_nam, struct sockaddr_in *);
404 rad = NFSSOCKADDR(clp->lc_req.nr_nam, struct sockaddr_in *);
405 sad->sin_addr.s_addr = rad->sin_addr.s_addr;
406 sad->sin_port = rad->sin_port;
407 NFSLOCKV4ROOTMUTEX();
408 nfsv4_unlock(&nfsv4rootfs_lock, 1);
409 NFSUNLOCKV4ROOTMUTEX();
410 error = NFSERR_CLIDINUSE;
415 if (NFSBCMP(new_clp->lc_verf, clp->lc_verf, NFSX_VERF)) {
417 * If the verifier has changed, the client has rebooted
418 * and a new client id is issued. The old state info
419 * can be thrown away once the SETCLIENTID_CONFIRM occurs.
421 LIST_REMOVE(clp, lc_hash);
423 /* Get rid of all sessions on this clientid. */
424 LIST_FOREACH_SAFE(sep, &clp->lc_session, sess_list, nsep) {
425 ret = nfsrv_freesession(sep, NULL);
427 printf("nfsrv_setclient: verifier changed free"
428 " session failed=%d\n", ret);
431 new_clp->lc_flags |= LCL_NEEDSCONFIRM;
432 if ((nd->nd_flag & ND_NFSV41) != 0)
433 new_clp->lc_confirm.lval[0] = confirmp->lval[0] =
436 confirmp->qval = new_clp->lc_confirm.qval =
438 clientidp->lval[0] = new_clp->lc_clientid.lval[0] =
440 clientidp->lval[1] = new_clp->lc_clientid.lval[1] =
441 nfsrv_nextclientindex();
442 new_clp->lc_stateindex = 0;
443 new_clp->lc_statemaxindex = 0;
444 new_clp->lc_cbref = 0;
445 new_clp->lc_expiry = nfsrv_leaseexpiry();
448 * Save the state until confirmed.
450 LIST_NEWHEAD(&new_clp->lc_open, &clp->lc_open, ls_list);
451 LIST_FOREACH(tstp, &new_clp->lc_open, ls_list)
452 tstp->ls_clp = new_clp;
453 LIST_NEWHEAD(&new_clp->lc_deleg, &clp->lc_deleg, ls_list);
454 LIST_FOREACH(tstp, &new_clp->lc_deleg, ls_list)
455 tstp->ls_clp = new_clp;
456 LIST_NEWHEAD(&new_clp->lc_olddeleg, &clp->lc_olddeleg,
458 LIST_FOREACH(tstp, &new_clp->lc_olddeleg, ls_list)
459 tstp->ls_clp = new_clp;
460 for (i = 0; i < nfsrv_statehashsize; i++) {
461 LIST_NEWHEAD(&new_clp->lc_stateid[i],
462 &clp->lc_stateid[i], ls_hash);
463 LIST_FOREACH(tstp, &new_clp->lc_stateid[i], ls_hash)
464 tstp->ls_clp = new_clp;
466 LIST_INIT(&new_clp->lc_session);
467 LIST_INSERT_HEAD(NFSCLIENTHASH(new_clp->lc_clientid), new_clp,
469 nfsstatsv1.srvclients++;
470 nfsrv_openpluslock++;
472 NFSLOCKV4ROOTMUTEX();
473 nfsv4_unlock(&nfsv4rootfs_lock, 1);
474 NFSUNLOCKV4ROOTMUTEX();
477 * Must wait until any outstanding callback on the old clp
481 while (clp->lc_cbref) {
482 clp->lc_flags |= LCL_WAKEUPWANTED;
483 (void)mtx_sleep(clp, NFSSTATEMUTEXPTR, PZERO - 1,
484 "nfsd clp", 10 * hz);
487 nfsrv_zapclient(clp, p);
492 /* For NFSv4.1, mark that we found a confirmed clientid. */
493 if ((nd->nd_flag & ND_NFSV41) != 0) {
494 clientidp->lval[0] = clp->lc_clientid.lval[0];
495 clientidp->lval[1] = clp->lc_clientid.lval[1];
496 confirmp->lval[0] = 0; /* Ignored by client */
497 confirmp->lval[1] = 1;
500 * id and verifier match, so update the net address info
501 * and get rid of any existing callback authentication
502 * handle, so a new one will be acquired.
504 LIST_REMOVE(clp, lc_hash);
505 new_clp->lc_flags |= (LCL_NEEDSCONFIRM | LCL_DONTCLEAN);
506 new_clp->lc_expiry = nfsrv_leaseexpiry();
507 confirmp->qval = new_clp->lc_confirm.qval = ++confirm_index;
508 clientidp->lval[0] = new_clp->lc_clientid.lval[0] =
509 clp->lc_clientid.lval[0];
510 clientidp->lval[1] = new_clp->lc_clientid.lval[1] =
511 clp->lc_clientid.lval[1];
512 new_clp->lc_delegtime = clp->lc_delegtime;
513 new_clp->lc_stateindex = clp->lc_stateindex;
514 new_clp->lc_statemaxindex = clp->lc_statemaxindex;
515 new_clp->lc_cbref = 0;
516 LIST_NEWHEAD(&new_clp->lc_open, &clp->lc_open, ls_list);
517 LIST_FOREACH(tstp, &new_clp->lc_open, ls_list)
518 tstp->ls_clp = new_clp;
519 LIST_NEWHEAD(&new_clp->lc_deleg, &clp->lc_deleg, ls_list);
520 LIST_FOREACH(tstp, &new_clp->lc_deleg, ls_list)
521 tstp->ls_clp = new_clp;
522 LIST_NEWHEAD(&new_clp->lc_olddeleg, &clp->lc_olddeleg, ls_list);
523 LIST_FOREACH(tstp, &new_clp->lc_olddeleg, ls_list)
524 tstp->ls_clp = new_clp;
525 for (i = 0; i < nfsrv_statehashsize; i++) {
526 LIST_NEWHEAD(&new_clp->lc_stateid[i],
527 &clp->lc_stateid[i], ls_hash);
528 LIST_FOREACH(tstp, &new_clp->lc_stateid[i], ls_hash)
529 tstp->ls_clp = new_clp;
531 LIST_INIT(&new_clp->lc_session);
532 LIST_INSERT_HEAD(NFSCLIENTHASH(new_clp->lc_clientid), new_clp,
534 nfsstatsv1.srvclients++;
535 nfsrv_openpluslock++;
538 NFSLOCKV4ROOTMUTEX();
539 nfsv4_unlock(&nfsv4rootfs_lock, 1);
540 NFSUNLOCKV4ROOTMUTEX();
542 if ((nd->nd_flag & ND_NFSV41) == 0) {
544 * Must wait until any outstanding callback on the old clp
548 while (clp->lc_cbref) {
549 clp->lc_flags |= LCL_WAKEUPWANTED;
550 (void)mtx_sleep(clp, NFSSTATEMUTEXPTR, PZERO - 1,
554 nfsrv_zapclient(clp, p);
559 NFSEXITCODE2(error, nd);
564 * Check to see if the client id exists and optionally confirm it.
567 nfsrv_getclient(nfsquad_t clientid, int opflags, struct nfsclient **clpp,
568 struct nfsdsession *nsep, nfsquad_t confirm, uint32_t cbprogram,
569 struct nfsrv_descript *nd, NFSPROC_T *p)
571 struct nfsclient *clp;
572 struct nfsstate *stp;
574 struct nfsclienthashhead *hp;
575 int error = 0, igotlock, doneok;
576 struct nfssessionhash *shp;
577 struct nfsdsession *sep;
579 static uint64_t next_sess = 0;
583 if ((nd == NULL || (nd->nd_flag & ND_NFSV41) == 0 ||
584 opflags != CLOPS_RENEW) && nfsrvboottime != clientid.lval[0]) {
585 error = NFSERR_STALECLIENTID;
590 * If called with opflags == CLOPS_RENEW, the State Lock is
591 * already held. Otherwise, we need to get either that or,
592 * for the case of Confirm, lock out the nfsd threads.
594 if (opflags & CLOPS_CONFIRM) {
595 NFSLOCKV4ROOTMUTEX();
596 nfsv4_relref(&nfsv4rootfs_lock);
598 igotlock = nfsv4_lock(&nfsv4rootfs_lock, 1, NULL,
599 NFSV4ROOTLOCKMUTEXPTR, NULL);
602 * Create a new sessionid here, since we need to do it where
603 * there is a mutex held to serialize update of next_sess.
605 if ((nd->nd_flag & ND_NFSV41) != 0) {
606 sessid[0] = ++next_sess;
607 sessid[1] = clientid.qval;
609 NFSUNLOCKV4ROOTMUTEX();
610 } else if (opflags != CLOPS_RENEW) {
614 /* For NFSv4.1, the clp is acquired from the associated session. */
615 if (nd != NULL && (nd->nd_flag & ND_NFSV41) != 0 &&
616 opflags == CLOPS_RENEW) {
618 if ((nd->nd_flag & ND_HASSEQUENCE) != 0) {
619 shp = NFSSESSIONHASH(nd->nd_sessionid);
621 sep = nfsrv_findsession(nd->nd_sessionid);
624 NFSUNLOCKSESSION(shp);
627 hp = NFSCLIENTHASH(clientid);
628 LIST_FOREACH(clp, hp, lc_hash) {
629 if (clp->lc_clientid.lval[1] == clientid.lval[1])
634 if (opflags & CLOPS_CONFIRM)
635 error = NFSERR_STALECLIENTID;
637 error = NFSERR_EXPIRED;
638 } else if (clp->lc_flags & LCL_ADMINREVOKED) {
640 * If marked admin revoked, just return the error.
642 error = NFSERR_ADMINREVOKED;
645 if (opflags & CLOPS_CONFIRM) {
646 NFSLOCKV4ROOTMUTEX();
647 nfsv4_unlock(&nfsv4rootfs_lock, 1);
648 NFSUNLOCKV4ROOTMUTEX();
649 } else if (opflags != CLOPS_RENEW) {
656 * Perform any operations specified by the opflags.
658 if (opflags & CLOPS_CONFIRM) {
659 if (((nd->nd_flag & ND_NFSV41) != 0 &&
660 clp->lc_confirm.lval[0] != confirm.lval[0]) ||
661 ((nd->nd_flag & ND_NFSV41) == 0 &&
662 clp->lc_confirm.qval != confirm.qval))
663 error = NFSERR_STALECLIENTID;
664 else if (nfsrv_notsamecredname(nd, clp))
665 error = NFSERR_CLIDINUSE;
668 if ((clp->lc_flags & (LCL_NEEDSCONFIRM | LCL_DONTCLEAN)) ==
671 * Hang onto the delegations (as old delegations)
672 * for an Open with CLAIM_DELEGATE_PREV unless in
673 * grace, but get rid of the rest of the state.
675 nfsrv_cleanclient(clp, p);
676 nfsrv_freedeleglist(&clp->lc_olddeleg);
677 if (nfsrv_checkgrace(nd, clp, 0)) {
678 /* In grace, so just delete delegations */
679 nfsrv_freedeleglist(&clp->lc_deleg);
681 LIST_FOREACH(stp, &clp->lc_deleg, ls_list)
682 stp->ls_flags |= NFSLCK_OLDDELEG;
683 clp->lc_delegtime = NFSD_MONOSEC +
684 nfsrv_lease + NFSRV_LEASEDELTA;
685 LIST_NEWHEAD(&clp->lc_olddeleg, &clp->lc_deleg,
688 if ((nd->nd_flag & ND_NFSV41) != 0)
689 clp->lc_program = cbprogram;
691 clp->lc_flags &= ~(LCL_NEEDSCONFIRM | LCL_DONTCLEAN);
693 clp->lc_flags |= LCL_NEEDSCBNULL;
694 /* For NFSv4.1, link the session onto the client. */
696 /* Hold a reference on the xprt for a backchannel. */
697 if ((nsep->sess_crflags & NFSV4CRSESS_CONNBACKCHAN)
699 if (clp->lc_req.nr_client == NULL)
700 clp->lc_req.nr_client = (struct __rpc_client *)
701 clnt_bck_create(nd->nd_xprt->xp_socket,
702 cbprogram, NFSV4_CBVERS);
703 if (clp->lc_req.nr_client != NULL) {
704 SVC_ACQUIRE(nd->nd_xprt);
706 clp->lc_req.nr_client->cl_private;
707 /* Disable idle timeout. */
708 nd->nd_xprt->xp_idletimeout = 0;
709 nsep->sess_cbsess.nfsess_xprt = nd->nd_xprt;
711 nsep->sess_crflags &= ~NFSV4CRSESS_CONNBACKCHAN;
713 NFSBCOPY(sessid, nsep->sess_sessionid,
715 NFSBCOPY(sessid, nsep->sess_cbsess.nfsess_sessionid,
717 shp = NFSSESSIONHASH(nsep->sess_sessionid);
720 LIST_INSERT_HEAD(&shp->list, nsep, sess_hash);
721 LIST_INSERT_HEAD(&clp->lc_session, nsep, sess_list);
722 nsep->sess_clp = clp;
723 NFSUNLOCKSESSION(shp);
727 } else if (clp->lc_flags & LCL_NEEDSCONFIRM) {
728 error = NFSERR_EXPIRED;
732 * If called by the Renew Op, we must check the principal.
734 if (!error && (opflags & CLOPS_RENEWOP)) {
735 if (nfsrv_notsamecredname(nd, clp)) {
737 for (i = 0; i < nfsrv_statehashsize && doneok == 0; i++) {
738 LIST_FOREACH(stp, &clp->lc_stateid[i], ls_hash) {
739 if ((stp->ls_flags & NFSLCK_OPEN) &&
740 stp->ls_uid == nd->nd_cred->cr_uid) {
747 error = NFSERR_ACCES;
749 if (!error && (clp->lc_flags & LCL_CBDOWN))
750 error = NFSERR_CBPATHDOWN;
752 if ((!error || error == NFSERR_CBPATHDOWN) &&
753 (opflags & CLOPS_RENEW)) {
754 clp->lc_expiry = nfsrv_leaseexpiry();
756 if (opflags & CLOPS_CONFIRM) {
757 NFSLOCKV4ROOTMUTEX();
758 nfsv4_unlock(&nfsv4rootfs_lock, 1);
759 NFSUNLOCKV4ROOTMUTEX();
760 } else if (opflags != CLOPS_RENEW) {
767 NFSEXITCODE2(error, nd);
772 * Perform the NFSv4.1 destroy clientid.
775 nfsrv_destroyclient(nfsquad_t clientid, NFSPROC_T *p)
777 struct nfsclient *clp;
778 struct nfsclienthashhead *hp;
779 int error = 0, i, igotlock;
781 if (nfsrvboottime != clientid.lval[0]) {
782 error = NFSERR_STALECLIENTID;
786 /* Lock out other nfsd threads */
787 NFSLOCKV4ROOTMUTEX();
788 nfsv4_relref(&nfsv4rootfs_lock);
790 igotlock = nfsv4_lock(&nfsv4rootfs_lock, 1, NULL,
791 NFSV4ROOTLOCKMUTEXPTR, NULL);
792 } while (igotlock == 0);
793 NFSUNLOCKV4ROOTMUTEX();
795 hp = NFSCLIENTHASH(clientid);
796 LIST_FOREACH(clp, hp, lc_hash) {
797 if (clp->lc_clientid.lval[1] == clientid.lval[1])
801 NFSLOCKV4ROOTMUTEX();
802 nfsv4_unlock(&nfsv4rootfs_lock, 1);
803 NFSUNLOCKV4ROOTMUTEX();
804 /* Just return ok, since it is gone. */
809 * Free up all layouts on the clientid. Should the client return the
812 nfsrv_freelayoutlist(clientid);
814 /* Scan for state on the clientid. */
815 for (i = 0; i < nfsrv_statehashsize; i++)
816 if (!LIST_EMPTY(&clp->lc_stateid[i])) {
817 NFSLOCKV4ROOTMUTEX();
818 nfsv4_unlock(&nfsv4rootfs_lock, 1);
819 NFSUNLOCKV4ROOTMUTEX();
820 error = NFSERR_CLIENTIDBUSY;
823 if (!LIST_EMPTY(&clp->lc_session) || !LIST_EMPTY(&clp->lc_deleg)) {
824 NFSLOCKV4ROOTMUTEX();
825 nfsv4_unlock(&nfsv4rootfs_lock, 1);
826 NFSUNLOCKV4ROOTMUTEX();
827 error = NFSERR_CLIENTIDBUSY;
831 /* Destroy the clientid and return ok. */
832 nfsrv_cleanclient(clp, p);
833 nfsrv_freedeleglist(&clp->lc_deleg);
834 nfsrv_freedeleglist(&clp->lc_olddeleg);
835 LIST_REMOVE(clp, lc_hash);
836 NFSLOCKV4ROOTMUTEX();
837 nfsv4_unlock(&nfsv4rootfs_lock, 1);
838 NFSUNLOCKV4ROOTMUTEX();
839 nfsrv_zapclient(clp, p);
841 NFSEXITCODE2(error, nd);
846 * Called from the new nfssvc syscall to admin revoke a clientid.
847 * Returns 0 for success, error otherwise.
850 nfsrv_adminrevoke(struct nfsd_clid *revokep, NFSPROC_T *p)
852 struct nfsclient *clp = NULL;
857 * First, lock out the nfsd so that state won't change while the
858 * revocation record is being written to the stable storage restart
861 NFSLOCKV4ROOTMUTEX();
863 igotlock = nfsv4_lock(&nfsv4rootfs_lock, 1, NULL,
864 NFSV4ROOTLOCKMUTEXPTR, NULL);
866 NFSUNLOCKV4ROOTMUTEX();
869 * Search for a match in the client list.
872 while (i < nfsrv_clienthashsize && !gotit) {
873 LIST_FOREACH(clp, &nfsclienthash[i], lc_hash) {
874 if (revokep->nclid_idlen == clp->lc_idlen &&
875 !NFSBCMP(revokep->nclid_id, clp->lc_id, clp->lc_idlen)) {
883 NFSLOCKV4ROOTMUTEX();
884 nfsv4_unlock(&nfsv4rootfs_lock, 0);
885 NFSUNLOCKV4ROOTMUTEX();
891 * Now, write out the revocation record
893 nfsrv_writestable(clp->lc_id, clp->lc_idlen, NFSNST_REVOKE, p);
894 nfsrv_backupstable();
897 * and clear out the state, marking the clientid revoked.
899 clp->lc_flags &= ~LCL_CALLBACKSON;
900 clp->lc_flags |= LCL_ADMINREVOKED;
901 nfsrv_cleanclient(clp, p);
902 nfsrv_freedeleglist(&clp->lc_deleg);
903 nfsrv_freedeleglist(&clp->lc_olddeleg);
904 NFSLOCKV4ROOTMUTEX();
905 nfsv4_unlock(&nfsv4rootfs_lock, 0);
906 NFSUNLOCKV4ROOTMUTEX();
914 * Dump out stats for all clients. Called from nfssvc(2), that is used
918 nfsrv_dumpclients(struct nfsd_dumpclients *dumpp, int maxcnt)
920 struct nfsclient *clp;
924 * First, get a reference on the nfsv4rootfs_lock so that an
925 * exclusive lock cannot be acquired while dumping the clients.
927 NFSLOCKV4ROOTMUTEX();
928 nfsv4_getref(&nfsv4rootfs_lock, NULL, NFSV4ROOTLOCKMUTEXPTR, NULL);
929 NFSUNLOCKV4ROOTMUTEX();
932 * Rattle through the client lists until done.
934 while (i < nfsrv_clienthashsize && cnt < maxcnt) {
935 clp = LIST_FIRST(&nfsclienthash[i]);
936 while (clp != LIST_END(&nfsclienthash[i]) && cnt < maxcnt) {
937 nfsrv_dumpaclient(clp, &dumpp[cnt]);
939 clp = LIST_NEXT(clp, lc_hash);
944 dumpp[cnt].ndcl_clid.nclid_idlen = 0;
946 NFSLOCKV4ROOTMUTEX();
947 nfsv4_relref(&nfsv4rootfs_lock);
948 NFSUNLOCKV4ROOTMUTEX();
952 * Dump stats for a client. Must be called with the NFSSTATELOCK and spl'd.
955 nfsrv_dumpaclient(struct nfsclient *clp, struct nfsd_dumpclients *dumpp)
957 struct nfsstate *stp, *openstp, *lckownstp;
959 struct sockaddr *sad;
960 struct sockaddr_in *rad;
961 struct sockaddr_in6 *rad6;
963 dumpp->ndcl_nopenowners = dumpp->ndcl_nlockowners = 0;
964 dumpp->ndcl_nopens = dumpp->ndcl_nlocks = 0;
965 dumpp->ndcl_ndelegs = dumpp->ndcl_nolddelegs = 0;
966 dumpp->ndcl_flags = clp->lc_flags;
967 dumpp->ndcl_clid.nclid_idlen = clp->lc_idlen;
968 NFSBCOPY(clp->lc_id, dumpp->ndcl_clid.nclid_id, clp->lc_idlen);
969 sad = NFSSOCKADDR(clp->lc_req.nr_nam, struct sockaddr *);
970 dumpp->ndcl_addrfam = sad->sa_family;
971 if (sad->sa_family == AF_INET) {
972 rad = (struct sockaddr_in *)sad;
973 dumpp->ndcl_cbaddr.sin_addr = rad->sin_addr;
975 rad6 = (struct sockaddr_in6 *)sad;
976 dumpp->ndcl_cbaddr.sin6_addr = rad6->sin6_addr;
980 * Now, scan the state lists and total up the opens and locks.
982 LIST_FOREACH(stp, &clp->lc_open, ls_list) {
983 dumpp->ndcl_nopenowners++;
984 LIST_FOREACH(openstp, &stp->ls_open, ls_list) {
985 dumpp->ndcl_nopens++;
986 LIST_FOREACH(lckownstp, &openstp->ls_open, ls_list) {
987 dumpp->ndcl_nlockowners++;
988 LIST_FOREACH(lop, &lckownstp->ls_lock, lo_lckowner) {
989 dumpp->ndcl_nlocks++;
996 * and the delegation lists.
998 LIST_FOREACH(stp, &clp->lc_deleg, ls_list) {
999 dumpp->ndcl_ndelegs++;
1001 LIST_FOREACH(stp, &clp->lc_olddeleg, ls_list) {
1002 dumpp->ndcl_nolddelegs++;
1007 * Dump out lock stats for a file.
1010 nfsrv_dumplocks(vnode_t vp, struct nfsd_dumplocks *ldumpp, int maxcnt,
1013 struct nfsstate *stp;
1014 struct nfslock *lop;
1016 struct nfslockfile *lfp;
1017 struct sockaddr *sad;
1018 struct sockaddr_in *rad;
1019 struct sockaddr_in6 *rad6;
1023 ret = nfsrv_getlockfh(vp, 0, NULL, &nfh, p);
1025 * First, get a reference on the nfsv4rootfs_lock so that an
1026 * exclusive lock on it cannot be acquired while dumping the locks.
1028 NFSLOCKV4ROOTMUTEX();
1029 nfsv4_getref(&nfsv4rootfs_lock, NULL, NFSV4ROOTLOCKMUTEXPTR, NULL);
1030 NFSUNLOCKV4ROOTMUTEX();
1033 ret = nfsrv_getlockfile(0, NULL, &lfp, &nfh, 0);
1035 ldumpp[0].ndlck_clid.nclid_idlen = 0;
1037 NFSLOCKV4ROOTMUTEX();
1038 nfsv4_relref(&nfsv4rootfs_lock);
1039 NFSUNLOCKV4ROOTMUTEX();
1044 * For each open share on file, dump it out.
1046 stp = LIST_FIRST(&lfp->lf_open);
1047 while (stp != LIST_END(&lfp->lf_open) && cnt < maxcnt) {
1048 ldumpp[cnt].ndlck_flags = stp->ls_flags;
1049 ldumpp[cnt].ndlck_stateid.seqid = stp->ls_stateid.seqid;
1050 ldumpp[cnt].ndlck_stateid.other[0] = stp->ls_stateid.other[0];
1051 ldumpp[cnt].ndlck_stateid.other[1] = stp->ls_stateid.other[1];
1052 ldumpp[cnt].ndlck_stateid.other[2] = stp->ls_stateid.other[2];
1053 ldumpp[cnt].ndlck_owner.nclid_idlen =
1054 stp->ls_openowner->ls_ownerlen;
1055 NFSBCOPY(stp->ls_openowner->ls_owner,
1056 ldumpp[cnt].ndlck_owner.nclid_id,
1057 stp->ls_openowner->ls_ownerlen);
1058 ldumpp[cnt].ndlck_clid.nclid_idlen = stp->ls_clp->lc_idlen;
1059 NFSBCOPY(stp->ls_clp->lc_id, ldumpp[cnt].ndlck_clid.nclid_id,
1060 stp->ls_clp->lc_idlen);
1061 sad=NFSSOCKADDR(stp->ls_clp->lc_req.nr_nam, struct sockaddr *);
1062 ldumpp[cnt].ndlck_addrfam = sad->sa_family;
1063 if (sad->sa_family == AF_INET) {
1064 rad = (struct sockaddr_in *)sad;
1065 ldumpp[cnt].ndlck_cbaddr.sin_addr = rad->sin_addr;
1067 rad6 = (struct sockaddr_in6 *)sad;
1068 ldumpp[cnt].ndlck_cbaddr.sin6_addr = rad6->sin6_addr;
1070 stp = LIST_NEXT(stp, ls_file);
1077 lop = LIST_FIRST(&lfp->lf_lock);
1078 while (lop != LIST_END(&lfp->lf_lock) && cnt < maxcnt) {
1080 ldumpp[cnt].ndlck_flags = lop->lo_flags;
1081 ldumpp[cnt].ndlck_first = lop->lo_first;
1082 ldumpp[cnt].ndlck_end = lop->lo_end;
1083 ldumpp[cnt].ndlck_stateid.seqid = stp->ls_stateid.seqid;
1084 ldumpp[cnt].ndlck_stateid.other[0] = stp->ls_stateid.other[0];
1085 ldumpp[cnt].ndlck_stateid.other[1] = stp->ls_stateid.other[1];
1086 ldumpp[cnt].ndlck_stateid.other[2] = stp->ls_stateid.other[2];
1087 ldumpp[cnt].ndlck_owner.nclid_idlen = stp->ls_ownerlen;
1088 NFSBCOPY(stp->ls_owner, ldumpp[cnt].ndlck_owner.nclid_id,
1090 ldumpp[cnt].ndlck_clid.nclid_idlen = stp->ls_clp->lc_idlen;
1091 NFSBCOPY(stp->ls_clp->lc_id, ldumpp[cnt].ndlck_clid.nclid_id,
1092 stp->ls_clp->lc_idlen);
1093 sad=NFSSOCKADDR(stp->ls_clp->lc_req.nr_nam, struct sockaddr *);
1094 ldumpp[cnt].ndlck_addrfam = sad->sa_family;
1095 if (sad->sa_family == AF_INET) {
1096 rad = (struct sockaddr_in *)sad;
1097 ldumpp[cnt].ndlck_cbaddr.sin_addr = rad->sin_addr;
1099 rad6 = (struct sockaddr_in6 *)sad;
1100 ldumpp[cnt].ndlck_cbaddr.sin6_addr = rad6->sin6_addr;
1102 lop = LIST_NEXT(lop, lo_lckfile);
1107 * and the delegations.
1109 stp = LIST_FIRST(&lfp->lf_deleg);
1110 while (stp != LIST_END(&lfp->lf_deleg) && cnt < maxcnt) {
1111 ldumpp[cnt].ndlck_flags = stp->ls_flags;
1112 ldumpp[cnt].ndlck_stateid.seqid = stp->ls_stateid.seqid;
1113 ldumpp[cnt].ndlck_stateid.other[0] = stp->ls_stateid.other[0];
1114 ldumpp[cnt].ndlck_stateid.other[1] = stp->ls_stateid.other[1];
1115 ldumpp[cnt].ndlck_stateid.other[2] = stp->ls_stateid.other[2];
1116 ldumpp[cnt].ndlck_owner.nclid_idlen = 0;
1117 ldumpp[cnt].ndlck_clid.nclid_idlen = stp->ls_clp->lc_idlen;
1118 NFSBCOPY(stp->ls_clp->lc_id, ldumpp[cnt].ndlck_clid.nclid_id,
1119 stp->ls_clp->lc_idlen);
1120 sad=NFSSOCKADDR(stp->ls_clp->lc_req.nr_nam, struct sockaddr *);
1121 ldumpp[cnt].ndlck_addrfam = sad->sa_family;
1122 if (sad->sa_family == AF_INET) {
1123 rad = (struct sockaddr_in *)sad;
1124 ldumpp[cnt].ndlck_cbaddr.sin_addr = rad->sin_addr;
1126 rad6 = (struct sockaddr_in6 *)sad;
1127 ldumpp[cnt].ndlck_cbaddr.sin6_addr = rad6->sin6_addr;
1129 stp = LIST_NEXT(stp, ls_file);
1134 * If list isn't full, mark end of list by setting the client name
1138 ldumpp[cnt].ndlck_clid.nclid_idlen = 0;
1140 NFSLOCKV4ROOTMUTEX();
1141 nfsv4_relref(&nfsv4rootfs_lock);
1142 NFSUNLOCKV4ROOTMUTEX();
1146 * Server timer routine. It can scan any linked list, so long
1147 * as it holds the spin/mutex lock and there is no exclusive lock on
1149 * (For OpenBSD, a kthread is ok. For FreeBSD, I think it is ok
1150 * to do this from a callout, since the spin locks work. For
1151 * Darwin, I'm not sure what will work correctly yet.)
1152 * Should be called once per second.
1155 nfsrv_servertimer(void)
1157 struct nfsclient *clp, *nclp;
1158 struct nfsstate *stp, *nstp;
1162 * Make sure nfsboottime is set. This is used by V3 as well
1163 * as V4. Note that nfsboottime is not nfsrvboottime, which is
1164 * only used by the V4 server for leases.
1166 if (nfsboottime.tv_sec == 0)
1167 NFSSETBOOTTIME(nfsboottime);
1170 * If server hasn't started yet, just return.
1173 if (nfsrv_stablefirst.nsf_eograce == 0) {
1177 if (!(nfsrv_stablefirst.nsf_flags & NFSNSF_UPDATEDONE)) {
1178 if (!(nfsrv_stablefirst.nsf_flags & NFSNSF_GRACEOVER) &&
1179 NFSD_MONOSEC > nfsrv_stablefirst.nsf_eograce)
1180 nfsrv_stablefirst.nsf_flags |=
1181 (NFSNSF_GRACEOVER | NFSNSF_NEEDLOCK);
1187 * Try and get a reference count on the nfsv4rootfs_lock so that
1188 * no nfsd thread can acquire an exclusive lock on it before this
1189 * call is done. If it is already exclusively locked, just return.
1191 NFSLOCKV4ROOTMUTEX();
1192 got_ref = nfsv4_getref_nonblock(&nfsv4rootfs_lock);
1193 NFSUNLOCKV4ROOTMUTEX();
1200 * For each client...
1202 for (i = 0; i < nfsrv_clienthashsize; i++) {
1203 clp = LIST_FIRST(&nfsclienthash[i]);
1204 while (clp != LIST_END(&nfsclienthash[i])) {
1205 nclp = LIST_NEXT(clp, lc_hash);
1206 if (!(clp->lc_flags & LCL_EXPIREIT)) {
1207 if (((clp->lc_expiry + NFSRV_STALELEASE) < NFSD_MONOSEC
1208 && ((LIST_EMPTY(&clp->lc_deleg)
1209 && LIST_EMPTY(&clp->lc_open)) ||
1210 nfsrv_clients > nfsrv_clienthighwater)) ||
1211 (clp->lc_expiry + NFSRV_MOULDYLEASE) < NFSD_MONOSEC ||
1212 (clp->lc_expiry < NFSD_MONOSEC &&
1213 (nfsrv_openpluslock * 10 / 9) > nfsrv_v4statelimit)) {
1215 * Lease has expired several nfsrv_lease times ago:
1217 * - no state is associated with it
1219 * - above high water mark for number of clients
1220 * (nfsrv_clienthighwater should be large enough
1221 * that this only occurs when clients fail to
1222 * use the same nfs_client_id4.id. Maybe somewhat
1223 * higher that the maximum number of clients that
1224 * will mount this server?)
1226 * Lease has expired a very long time ago
1228 * Lease has expired PLUS the number of opens + locks
1229 * has exceeded 90% of capacity
1231 * --> Mark for expiry. The actual expiry will be done
1232 * by an nfsd sometime soon.
1234 clp->lc_flags |= LCL_EXPIREIT;
1235 nfsrv_stablefirst.nsf_flags |=
1236 (NFSNSF_NEEDLOCK | NFSNSF_EXPIREDCLIENT);
1239 * If there are no opens, increment no open tick cnt
1240 * If time exceeds NFSNOOPEN, mark it to be thrown away
1241 * otherwise, if there is an open, reset no open time
1242 * Hopefully, this will avoid excessive re-creation
1243 * of open owners and subsequent open confirms.
1245 stp = LIST_FIRST(&clp->lc_open);
1246 while (stp != LIST_END(&clp->lc_open)) {
1247 nstp = LIST_NEXT(stp, ls_list);
1248 if (LIST_EMPTY(&stp->ls_open)) {
1250 if (stp->ls_noopens > NFSNOOPEN ||
1251 (nfsrv_openpluslock * 2) >
1253 nfsrv_stablefirst.nsf_flags |=
1256 stp->ls_noopens = 0;
1266 NFSLOCKV4ROOTMUTEX();
1267 nfsv4_relref(&nfsv4rootfs_lock);
1268 NFSUNLOCKV4ROOTMUTEX();
1272 * The following set of functions free up the various data structures.
1275 * Clear out all open/lock state related to this nfsclient.
1276 * Caller must hold an exclusive lock on nfsv4rootfs_lock, so that
1277 * there are no other active nfsd threads.
1280 nfsrv_cleanclient(struct nfsclient *clp, NFSPROC_T *p)
1282 struct nfsstate *stp, *nstp;
1283 struct nfsdsession *sep, *nsep;
1285 LIST_FOREACH_SAFE(stp, &clp->lc_open, ls_list, nstp)
1286 nfsrv_freeopenowner(stp, 1, p);
1287 if ((clp->lc_flags & LCL_ADMINREVOKED) == 0)
1288 LIST_FOREACH_SAFE(sep, &clp->lc_session, sess_list, nsep)
1289 (void)nfsrv_freesession(sep, NULL);
1293 * Free a client that has been cleaned. It should also already have been
1294 * removed from the lists.
1295 * (Just to be safe w.r.t. newnfs_disconnect(), call this function when
1296 * softclock interrupts are enabled.)
1299 nfsrv_zapclient(struct nfsclient *clp, NFSPROC_T *p)
1303 if ((clp->lc_flags & (LCL_GSS | LCL_CALLBACKSON)) ==
1304 (LCL_GSS | LCL_CALLBACKSON) &&
1305 (clp->lc_hand.nfsh_flag & NFSG_COMPLETE) &&
1306 clp->lc_handlelen > 0) {
1307 clp->lc_hand.nfsh_flag &= ~NFSG_COMPLETE;
1308 clp->lc_hand.nfsh_flag |= NFSG_DESTROYED;
1309 (void) nfsrv_docallback(clp, NFSV4PROC_CBNULL,
1310 NULL, 0, NULL, NULL, NULL, 0, p);
1313 newnfs_disconnect(&clp->lc_req);
1314 free(clp->lc_req.nr_nam, M_SONAME);
1315 NFSFREEMUTEX(&clp->lc_req.nr_mtx);
1316 free(clp->lc_stateid, M_NFSDCLIENT);
1317 free(clp, M_NFSDCLIENT);
1319 nfsstatsv1.srvclients--;
1320 nfsrv_openpluslock--;
1326 * Free a list of delegation state structures.
1327 * (This function will also free all nfslockfile structures that no
1328 * longer have associated state.)
1331 nfsrv_freedeleglist(struct nfsstatehead *sthp)
1333 struct nfsstate *stp, *nstp;
1335 LIST_FOREACH_SAFE(stp, sthp, ls_list, nstp) {
1336 nfsrv_freedeleg(stp);
1342 * Free up a delegation.
1345 nfsrv_freedeleg(struct nfsstate *stp)
1347 struct nfslockfile *lfp;
1349 LIST_REMOVE(stp, ls_hash);
1350 LIST_REMOVE(stp, ls_list);
1351 LIST_REMOVE(stp, ls_file);
1352 if ((stp->ls_flags & NFSLCK_DELEGWRITE) != 0)
1353 nfsrv_writedelegcnt--;
1355 if (LIST_EMPTY(&lfp->lf_open) &&
1356 LIST_EMPTY(&lfp->lf_lock) && LIST_EMPTY(&lfp->lf_deleg) &&
1357 LIST_EMPTY(&lfp->lf_locallock) && LIST_EMPTY(&lfp->lf_rollback) &&
1358 lfp->lf_usecount == 0 &&
1359 nfsv4_testlock(&lfp->lf_locallock_lck) == 0)
1360 nfsrv_freenfslockfile(lfp);
1361 free(stp, M_NFSDSTATE);
1362 nfsstatsv1.srvdelegates--;
1363 nfsrv_openpluslock--;
1364 nfsrv_delegatecnt--;
1368 * This function frees an open owner and all associated opens.
1371 nfsrv_freeopenowner(struct nfsstate *stp, int cansleep, NFSPROC_T *p)
1373 struct nfsstate *nstp, *tstp;
1375 LIST_REMOVE(stp, ls_list);
1377 * Now, free all associated opens.
1379 nstp = LIST_FIRST(&stp->ls_open);
1380 while (nstp != LIST_END(&stp->ls_open)) {
1382 nstp = LIST_NEXT(nstp, ls_list);
1383 (void) nfsrv_freeopen(tstp, NULL, cansleep, p);
1386 nfsrvd_derefcache(stp->ls_op);
1387 free(stp, M_NFSDSTATE);
1388 nfsstatsv1.srvopenowners--;
1389 nfsrv_openpluslock--;
1393 * This function frees an open (nfsstate open structure) with all associated
1394 * lock_owners and locks. It also frees the nfslockfile structure iff there
1395 * are no other opens on the file.
1396 * Returns 1 if it free'd the nfslockfile, 0 otherwise.
1399 nfsrv_freeopen(struct nfsstate *stp, vnode_t vp, int cansleep, NFSPROC_T *p)
1401 struct nfsstate *nstp, *tstp;
1402 struct nfslockfile *lfp;
1405 LIST_REMOVE(stp, ls_hash);
1406 LIST_REMOVE(stp, ls_list);
1407 LIST_REMOVE(stp, ls_file);
1411 * Now, free all lockowners associated with this open.
1413 LIST_FOREACH_SAFE(tstp, &stp->ls_open, ls_list, nstp)
1414 nfsrv_freelockowner(tstp, vp, cansleep, p);
1417 * The nfslockfile is freed here if there are no locks
1418 * associated with the open.
1419 * If there are locks associated with the open, the
1420 * nfslockfile structure can be freed via nfsrv_freelockowner().
1421 * Acquire the state mutex to avoid races with calls to
1422 * nfsrv_getlockfile().
1426 if (lfp != NULL && LIST_EMPTY(&lfp->lf_open) &&
1427 LIST_EMPTY(&lfp->lf_deleg) && LIST_EMPTY(&lfp->lf_lock) &&
1428 LIST_EMPTY(&lfp->lf_locallock) && LIST_EMPTY(&lfp->lf_rollback) &&
1429 lfp->lf_usecount == 0 &&
1430 (cansleep != 0 || nfsv4_testlock(&lfp->lf_locallock_lck) == 0)) {
1431 nfsrv_freenfslockfile(lfp);
1437 free(stp, M_NFSDSTATE);
1438 nfsstatsv1.srvopens--;
1439 nfsrv_openpluslock--;
1444 * Frees a lockowner and all associated locks.
1447 nfsrv_freelockowner(struct nfsstate *stp, vnode_t vp, int cansleep,
1451 LIST_REMOVE(stp, ls_hash);
1452 LIST_REMOVE(stp, ls_list);
1453 nfsrv_freeallnfslocks(stp, vp, cansleep, p);
1455 nfsrvd_derefcache(stp->ls_op);
1456 free(stp, M_NFSDSTATE);
1457 nfsstatsv1.srvlockowners--;
1458 nfsrv_openpluslock--;
1462 * Free all the nfs locks on a lockowner.
1465 nfsrv_freeallnfslocks(struct nfsstate *stp, vnode_t vp, int cansleep,
1468 struct nfslock *lop, *nlop;
1469 struct nfsrollback *rlp, *nrlp;
1470 struct nfslockfile *lfp = NULL;
1473 uint64_t first, end;
1476 ASSERT_VOP_UNLOCKED(vp, "nfsrv_freeallnfslocks: vnode locked");
1477 lop = LIST_FIRST(&stp->ls_lock);
1478 while (lop != LIST_END(&stp->ls_lock)) {
1479 nlop = LIST_NEXT(lop, lo_lckowner);
1481 * Since all locks should be for the same file, lfp should
1486 else if (lfp != lop->lo_lfp)
1487 panic("allnfslocks");
1489 * If vp is NULL and cansleep != 0, a vnode must be acquired
1490 * from the file handle. This only occurs when called from
1491 * nfsrv_cleanclient().
1494 if (nfsrv_dolocallocks == 0)
1496 else if (vp == NULL && cansleep != 0) {
1497 tvp = nfsvno_getvp(&lfp->lf_fh);
1498 NFSVOPUNLOCK(tvp, 0);
1507 first = lop->lo_first;
1509 nfsrv_freenfslock(lop);
1510 nfsrv_localunlock(tvp, lfp, first, end, p);
1511 LIST_FOREACH_SAFE(rlp, &lfp->lf_rollback, rlck_list,
1513 free(rlp, M_NFSDROLLBACK);
1514 LIST_INIT(&lfp->lf_rollback);
1516 nfsrv_freenfslock(lop);
1519 if (vp == NULL && tvp != NULL)
1524 * Free an nfslock structure.
1527 nfsrv_freenfslock(struct nfslock *lop)
1530 if (lop->lo_lckfile.le_prev != NULL) {
1531 LIST_REMOVE(lop, lo_lckfile);
1532 nfsstatsv1.srvlocks--;
1533 nfsrv_openpluslock--;
1535 LIST_REMOVE(lop, lo_lckowner);
1536 free(lop, M_NFSDLOCK);
1540 * This function frees an nfslockfile structure.
1543 nfsrv_freenfslockfile(struct nfslockfile *lfp)
1546 LIST_REMOVE(lfp, lf_hash);
1547 free(lfp, M_NFSDLOCKFILE);
1551 * This function looks up an nfsstate structure via stateid.
1554 nfsrv_getstate(struct nfsclient *clp, nfsv4stateid_t *stateidp, __unused u_int32_t flags,
1555 struct nfsstate **stpp)
1557 struct nfsstate *stp;
1558 struct nfsstatehead *hp;
1562 hp = NFSSTATEHASH(clp, *stateidp);
1563 LIST_FOREACH(stp, hp, ls_hash) {
1564 if (!NFSBCMP(stp->ls_stateid.other, stateidp->other,
1570 * If no state id in list, return NFSERR_BADSTATEID.
1572 if (stp == LIST_END(hp)) {
1573 error = NFSERR_BADSTATEID;
1584 * This function gets an nfsstate structure via owner string.
1587 nfsrv_getowner(struct nfsstatehead *hp, struct nfsstate *new_stp,
1588 struct nfsstate **stpp)
1590 struct nfsstate *stp;
1593 LIST_FOREACH(stp, hp, ls_list) {
1594 if (new_stp->ls_ownerlen == stp->ls_ownerlen &&
1595 !NFSBCMP(new_stp->ls_owner,stp->ls_owner,stp->ls_ownerlen)) {
1603 * Lock control function called to update lock status.
1604 * Returns 0 upon success, -1 if there is no lock and the flags indicate
1605 * that one isn't to be created and an NFSERR_xxx for other errors.
1606 * The structures new_stp and new_lop are passed in as pointers that should
1607 * be set to NULL if the structure is used and shouldn't be free'd.
1608 * For the NFSLCK_TEST and NFSLCK_CHECK cases, the structures are
1609 * never used and can safely be allocated on the stack. For all other
1610 * cases, *new_stpp and *new_lopp should be malloc'd before the call,
1611 * in case they are used.
1614 nfsrv_lockctrl(vnode_t vp, struct nfsstate **new_stpp,
1615 struct nfslock **new_lopp, struct nfslockconflict *cfp,
1616 nfsquad_t clientid, nfsv4stateid_t *stateidp,
1617 __unused struct nfsexstuff *exp,
1618 struct nfsrv_descript *nd, NFSPROC_T *p)
1620 struct nfslock *lop;
1621 struct nfsstate *new_stp = *new_stpp;
1622 struct nfslock *new_lop = *new_lopp;
1623 struct nfsstate *tstp, *mystp, *nstp;
1625 struct nfslockfile *lfp;
1626 struct nfslock *other_lop = NULL;
1627 struct nfsstate *stp, *lckstp = NULL;
1628 struct nfsclient *clp = NULL;
1630 int error = 0, haslock = 0, ret, reterr;
1631 int getlckret, delegation = 0, filestruct_locked, vnode_unlocked = 0;
1633 uint64_t first, end;
1634 uint32_t lock_flags;
1636 if (new_stp->ls_flags & (NFSLCK_CHECK | NFSLCK_SETATTR)) {
1638 * Note the special cases of "all 1s" or "all 0s" stateids and
1639 * let reads with all 1s go ahead.
1641 if (new_stp->ls_stateid.seqid == 0x0 &&
1642 new_stp->ls_stateid.other[0] == 0x0 &&
1643 new_stp->ls_stateid.other[1] == 0x0 &&
1644 new_stp->ls_stateid.other[2] == 0x0)
1646 else if (new_stp->ls_stateid.seqid == 0xffffffff &&
1647 new_stp->ls_stateid.other[0] == 0xffffffff &&
1648 new_stp->ls_stateid.other[1] == 0xffffffff &&
1649 new_stp->ls_stateid.other[2] == 0xffffffff)
1654 * Check for restart conditions (client and server).
1656 error = nfsrv_checkrestart(clientid, new_stp->ls_flags,
1657 &new_stp->ls_stateid, specialid);
1662 * Check for state resource limit exceeded.
1664 if ((new_stp->ls_flags & NFSLCK_LOCK) &&
1665 nfsrv_openpluslock > nfsrv_v4statelimit) {
1666 error = NFSERR_RESOURCE;
1671 * For the lock case, get another nfslock structure,
1672 * just in case we need it.
1673 * Malloc now, before we start sifting through the linked lists,
1674 * in case we have to wait for memory.
1677 if (new_stp->ls_flags & NFSLCK_LOCK)
1678 other_lop = malloc(sizeof (struct nfslock),
1679 M_NFSDLOCK, M_WAITOK);
1680 filestruct_locked = 0;
1685 * Get the lockfile structure for CFH now, so we can do a sanity
1686 * check against the stateid, before incrementing the seqid#, since
1687 * we want to return NFSERR_BADSTATEID on failure and the seqid#
1688 * shouldn't be incremented for this case.
1689 * If nfsrv_getlockfile() returns -1, it means "not found", which
1690 * will be handled later.
1691 * If we are doing Lock/LockU and local locking is enabled, sleep
1692 * lock the nfslockfile structure.
1694 getlckret = nfsrv_getlockfh(vp, new_stp->ls_flags, NULL, &nfh, p);
1696 if (getlckret == 0) {
1697 if ((new_stp->ls_flags & (NFSLCK_LOCK | NFSLCK_UNLOCK)) != 0 &&
1698 nfsrv_dolocallocks != 0 && nd->nd_repstat == 0) {
1699 getlckret = nfsrv_getlockfile(new_stp->ls_flags, NULL,
1702 filestruct_locked = 1;
1704 getlckret = nfsrv_getlockfile(new_stp->ls_flags, NULL,
1707 if (getlckret != 0 && getlckret != -1)
1710 if (filestruct_locked != 0) {
1711 LIST_INIT(&lfp->lf_rollback);
1712 if ((new_stp->ls_flags & NFSLCK_LOCK)) {
1714 * For local locking, do the advisory locking now, so
1715 * that any conflict can be detected. A failure later
1716 * can be rolled back locally. If an error is returned,
1717 * struct nfslockfile has been unlocked and any local
1718 * locking rolled back.
1721 if (vnode_unlocked == 0) {
1722 ASSERT_VOP_ELOCKED(vp, "nfsrv_lockctrl1");
1724 NFSVOPUNLOCK(vp, 0);
1726 reterr = nfsrv_locallock(vp, lfp,
1727 (new_lop->lo_flags & (NFSLCK_READ | NFSLCK_WRITE)),
1728 new_lop->lo_first, new_lop->lo_end, cfp, p);
1733 if (specialid == 0) {
1734 if (new_stp->ls_flags & NFSLCK_TEST) {
1736 * RFC 3530 does not list LockT as an op that renews a
1737 * lease, but the consensus seems to be that it is ok
1738 * for a server to do so.
1740 error = nfsrv_getclient(clientid, CLOPS_RENEW, &clp, NULL,
1741 (nfsquad_t)((u_quad_t)0), 0, nd, p);
1744 * Since NFSERR_EXPIRED, NFSERR_ADMINREVOKED are not valid
1745 * error returns for LockT, just go ahead and test for a lock,
1746 * since there are no locks for this client, but other locks
1747 * can conflict. (ie. same client will always be false)
1749 if (error == NFSERR_EXPIRED || error == NFSERR_ADMINREVOKED)
1753 error = nfsrv_getclient(clientid, CLOPS_RENEW, &clp, NULL,
1754 (nfsquad_t)((u_quad_t)0), 0, nd, p);
1757 * Look up the stateid
1759 error = nfsrv_getstate(clp, &new_stp->ls_stateid,
1760 new_stp->ls_flags, &stp);
1762 * do some sanity checks for an unconfirmed open or a
1763 * stateid that refers to the wrong file, for an open stateid
1765 if (error == 0 && (stp->ls_flags & NFSLCK_OPEN) &&
1766 ((stp->ls_openowner->ls_flags & NFSLCK_NEEDSCONFIRM) ||
1767 (getlckret == 0 && stp->ls_lfp != lfp))){
1769 * NFSLCK_SETATTR should return OK rather than NFSERR_BADSTATEID
1770 * The only exception is using SETATTR with SIZE.
1772 if ((new_stp->ls_flags &
1773 (NFSLCK_SETATTR | NFSLCK_CHECK)) != NFSLCK_SETATTR)
1774 error = NFSERR_BADSTATEID;
1778 (stp->ls_flags & (NFSLCK_DELEGREAD | NFSLCK_DELEGWRITE)) &&
1779 getlckret == 0 && stp->ls_lfp != lfp)
1780 error = NFSERR_BADSTATEID;
1783 * If the lockowner stateid doesn't refer to the same file,
1784 * I believe that is considered ok, since some clients will
1785 * only create a single lockowner and use that for all locks
1787 * For now, log it as a diagnostic, instead of considering it
1790 if (error == 0 && (stp->ls_flags &
1791 (NFSLCK_OPEN | NFSLCK_DELEGREAD | NFSLCK_DELEGWRITE)) == 0 &&
1792 getlckret == 0 && stp->ls_lfp != lfp) {
1794 printf("Got a lock statid for different file open\n");
1797 error = NFSERR_BADSTATEID;
1802 if (new_stp->ls_flags & NFSLCK_OPENTOLOCK) {
1804 * If haslock set, we've already checked the seqid.
1807 if (stp->ls_flags & NFSLCK_OPEN)
1808 error = nfsrv_checkseqid(nd, new_stp->ls_seq,
1809 stp->ls_openowner, new_stp->ls_op);
1811 error = NFSERR_BADSTATEID;
1814 nfsrv_getowner(&stp->ls_open, new_stp, &lckstp);
1817 * I believe this should be an error, but it
1818 * isn't obvious what NFSERR_xxx would be
1819 * appropriate, so I'll use NFSERR_INVAL for now.
1821 error = NFSERR_INVAL;
1824 } else if (new_stp->ls_flags&(NFSLCK_LOCK|NFSLCK_UNLOCK)) {
1826 * If haslock set, ditto above.
1829 if (stp->ls_flags & NFSLCK_OPEN)
1830 error = NFSERR_BADSTATEID;
1832 error = nfsrv_checkseqid(nd, new_stp->ls_seq,
1833 stp, new_stp->ls_op);
1841 * If the seqid part of the stateid isn't the same, return
1842 * NFSERR_OLDSTATEID for cases other than I/O Ops.
1843 * For I/O Ops, only return NFSERR_OLDSTATEID if
1844 * nfsrv_returnoldstateid is set. (The consensus on the email
1845 * list was that most clients would prefer to not receive
1846 * NFSERR_OLDSTATEID for I/O Ops, but the RFC suggests that that
1847 * is what will happen, so I use the nfsrv_returnoldstateid to
1848 * allow for either server configuration.)
1850 if (!error && stp->ls_stateid.seqid!=new_stp->ls_stateid.seqid &&
1851 (((nd->nd_flag & ND_NFSV41) == 0 &&
1852 (!(new_stp->ls_flags & NFSLCK_CHECK) ||
1853 nfsrv_returnoldstateid)) ||
1854 ((nd->nd_flag & ND_NFSV41) != 0 &&
1855 new_stp->ls_stateid.seqid != 0)))
1856 error = NFSERR_OLDSTATEID;
1861 * Now we can check for grace.
1864 error = nfsrv_checkgrace(nd, clp, new_stp->ls_flags);
1865 if ((new_stp->ls_flags & NFSLCK_RECLAIM) && !error &&
1866 nfsrv_checkstable(clp))
1867 error = NFSERR_NOGRACE;
1869 * If we successfully Reclaimed state, note that.
1871 if ((new_stp->ls_flags & NFSLCK_RECLAIM) && !error)
1872 nfsrv_markstable(clp);
1875 * At this point, either error == NFSERR_BADSTATEID or the
1876 * seqid# has been updated, so we can return any error.
1877 * If error == 0, there may be an error in:
1878 * nd_repstat - Set by the calling function.
1879 * reterr - Set above, if getting the nfslockfile structure
1880 * or acquiring the local lock failed.
1881 * (If both of these are set, nd_repstat should probably be
1882 * returned, since that error was detected before this
1885 if (error != 0 || nd->nd_repstat != 0 || reterr != 0) {
1887 if (nd->nd_repstat != 0)
1888 error = nd->nd_repstat;
1892 if (filestruct_locked != 0) {
1893 /* Roll back local locks. */
1895 if (vnode_unlocked == 0) {
1896 ASSERT_VOP_ELOCKED(vp, "nfsrv_lockctrl2");
1898 NFSVOPUNLOCK(vp, 0);
1900 nfsrv_locallock_rollback(vp, lfp, p);
1902 nfsrv_unlocklf(lfp);
1909 * Check the nfsrv_getlockfile return.
1910 * Returned -1 if no structure found.
1912 if (getlckret == -1) {
1913 error = NFSERR_EXPIRED;
1915 * Called from lockt, so no lock is OK.
1917 if (new_stp->ls_flags & NFSLCK_TEST) {
1919 } else if (new_stp->ls_flags &
1920 (NFSLCK_CHECK | NFSLCK_SETATTR)) {
1922 * Called to check for a lock, OK if the stateid is all
1923 * 1s or all 0s, but there should be an nfsstate
1925 * (ie. If there is no open, I'll assume no share
1931 error = NFSERR_BADSTATEID;
1938 * For NFSLCK_CHECK and NFSLCK_LOCK, test for a share conflict.
1939 * For NFSLCK_CHECK, allow a read if write access is granted,
1940 * but check for a deny. For NFSLCK_LOCK, require correct access,
1941 * which implies a conflicting deny can't exist.
1943 if (new_stp->ls_flags & (NFSLCK_CHECK | NFSLCK_LOCK)) {
1945 * Four kinds of state id:
1946 * - specialid (all 0s or all 1s), only for NFSLCK_CHECK
1947 * - stateid for an open
1948 * - stateid for a delegation
1949 * - stateid for a lock owner
1952 if (stp->ls_flags & (NFSLCK_DELEGREAD | NFSLCK_DELEGWRITE)) {
1955 nfsrv_delaydelegtimeout(stp);
1956 } else if (stp->ls_flags & NFSLCK_OPEN) {
1959 mystp = stp->ls_openstp;
1962 * If locking or checking, require correct access
1965 if (((new_stp->ls_flags & NFSLCK_LOCK) &&
1966 !((new_lop->lo_flags >> NFSLCK_LOCKSHIFT) &
1967 mystp->ls_flags & NFSLCK_ACCESSBITS)) ||
1968 ((new_stp->ls_flags & (NFSLCK_CHECK|NFSLCK_READACCESS)) ==
1969 (NFSLCK_CHECK | NFSLCK_READACCESS) &&
1970 !(mystp->ls_flags & NFSLCK_READACCESS) &&
1971 nfsrv_allowreadforwriteopen == 0) ||
1972 ((new_stp->ls_flags & (NFSLCK_CHECK|NFSLCK_WRITEACCESS)) ==
1973 (NFSLCK_CHECK | NFSLCK_WRITEACCESS) &&
1974 !(mystp->ls_flags & NFSLCK_WRITEACCESS))) {
1975 if (filestruct_locked != 0) {
1976 /* Roll back local locks. */
1978 if (vnode_unlocked == 0) {
1979 ASSERT_VOP_ELOCKED(vp,
1982 NFSVOPUNLOCK(vp, 0);
1984 nfsrv_locallock_rollback(vp, lfp, p);
1986 nfsrv_unlocklf(lfp);
1989 error = NFSERR_OPENMODE;
1994 if ((new_stp->ls_flags & NFSLCK_CHECK) && !delegation) {
1996 * Check for a conflicting deny bit.
1998 LIST_FOREACH(tstp, &lfp->lf_open, ls_file) {
1999 if (tstp != mystp) {
2000 bits = tstp->ls_flags;
2001 bits >>= NFSLCK_SHIFT;
2002 if (new_stp->ls_flags & bits & NFSLCK_ACCESSBITS) {
2003 KASSERT(vnode_unlocked == 0,
2004 ("nfsrv_lockctrl: vnode unlocked1"));
2005 ret = nfsrv_clientconflict(tstp->ls_clp, &haslock,
2009 * nfsrv_clientconflict unlocks state
2010 * when it returns non-zero.
2018 error = NFSERR_PERM;
2020 error = NFSERR_OPENMODE;
2026 /* We're outta here */
2033 * For setattr, just get rid of all the Delegations for other clients.
2035 if (new_stp->ls_flags & NFSLCK_SETATTR) {
2036 KASSERT(vnode_unlocked == 0,
2037 ("nfsrv_lockctrl: vnode unlocked2"));
2038 ret = nfsrv_cleandeleg(vp, lfp, clp, &haslock, p);
2041 * nfsrv_cleandeleg() unlocks state when it
2051 if (!(new_stp->ls_flags & NFSLCK_CHECK) ||
2052 (LIST_EMPTY(&lfp->lf_open) && LIST_EMPTY(&lfp->lf_lock) &&
2053 LIST_EMPTY(&lfp->lf_deleg))) {
2060 * Check for a conflicting delegation. If one is found, call
2061 * nfsrv_delegconflict() to handle it. If the v4root lock hasn't
2062 * been set yet, it will get the lock. Otherwise, it will recall
2063 * the delegation. Then, we try try again...
2064 * I currently believe the conflict algorithm to be:
2065 * For Lock Ops (Lock/LockT/LockU)
2066 * - there is a conflict iff a different client has a write delegation
2067 * For Reading (Read Op)
2068 * - there is a conflict iff a different client has a write delegation
2069 * (the specialids are always a different client)
2070 * For Writing (Write/Setattr of size)
2071 * - there is a conflict if a different client has any delegation
2072 * - there is a conflict if the same client has a read delegation
2073 * (I don't understand why this isn't allowed, but that seems to be
2074 * the current consensus?)
2076 tstp = LIST_FIRST(&lfp->lf_deleg);
2077 while (tstp != LIST_END(&lfp->lf_deleg)) {
2078 nstp = LIST_NEXT(tstp, ls_file);
2079 if ((((new_stp->ls_flags&(NFSLCK_LOCK|NFSLCK_UNLOCK|NFSLCK_TEST))||
2080 ((new_stp->ls_flags & NFSLCK_CHECK) &&
2081 (new_lop->lo_flags & NFSLCK_READ))) &&
2082 clp != tstp->ls_clp &&
2083 (tstp->ls_flags & NFSLCK_DELEGWRITE)) ||
2084 ((new_stp->ls_flags & NFSLCK_CHECK) &&
2085 (new_lop->lo_flags & NFSLCK_WRITE) &&
2086 (clp != tstp->ls_clp ||
2087 (tstp->ls_flags & NFSLCK_DELEGREAD)))) {
2089 if (filestruct_locked != 0) {
2090 /* Roll back local locks. */
2092 if (vnode_unlocked == 0) {
2093 ASSERT_VOP_ELOCKED(vp, "nfsrv_lockctrl4");
2094 NFSVOPUNLOCK(vp, 0);
2096 nfsrv_locallock_rollback(vp, lfp, p);
2098 nfsrv_unlocklf(lfp);
2100 NFSVOPLOCK(vp, LK_EXCLUSIVE | LK_RETRY);
2102 if ((vp->v_iflag & VI_DOOMED) != 0)
2103 ret = NFSERR_SERVERFAULT;
2107 ret = nfsrv_delegconflict(tstp, &haslock, p, vp);
2110 * nfsrv_delegconflict unlocks state when it
2111 * returns non-zero, which it always does.
2114 free(other_lop, M_NFSDLOCK);
2124 /* Never gets here. */
2130 * Handle the unlock case by calling nfsrv_updatelock().
2131 * (Should I have done some access checking above for unlock? For now,
2132 * just let it happen.)
2134 if (new_stp->ls_flags & NFSLCK_UNLOCK) {
2135 first = new_lop->lo_first;
2136 end = new_lop->lo_end;
2137 nfsrv_updatelock(stp, new_lopp, &other_lop, lfp);
2138 stateidp->seqid = ++(stp->ls_stateid.seqid);
2139 if ((nd->nd_flag & ND_NFSV41) != 0 && stateidp->seqid == 0)
2140 stateidp->seqid = stp->ls_stateid.seqid = 1;
2141 stateidp->other[0] = stp->ls_stateid.other[0];
2142 stateidp->other[1] = stp->ls_stateid.other[1];
2143 stateidp->other[2] = stp->ls_stateid.other[2];
2144 if (filestruct_locked != 0) {
2146 if (vnode_unlocked == 0) {
2147 ASSERT_VOP_ELOCKED(vp, "nfsrv_lockctrl5");
2149 NFSVOPUNLOCK(vp, 0);
2151 /* Update the local locks. */
2152 nfsrv_localunlock(vp, lfp, first, end, p);
2154 nfsrv_unlocklf(lfp);
2161 * Search for a conflicting lock. A lock conflicts if:
2162 * - the lock range overlaps and
2163 * - at least one lock is a write lock and
2164 * - it is not owned by the same lock owner
2167 LIST_FOREACH(lop, &lfp->lf_lock, lo_lckfile) {
2168 if (new_lop->lo_end > lop->lo_first &&
2169 new_lop->lo_first < lop->lo_end &&
2170 (new_lop->lo_flags == NFSLCK_WRITE ||
2171 lop->lo_flags == NFSLCK_WRITE) &&
2172 lckstp != lop->lo_stp &&
2173 (clp != lop->lo_stp->ls_clp ||
2174 lckstp->ls_ownerlen != lop->lo_stp->ls_ownerlen ||
2175 NFSBCMP(lckstp->ls_owner, lop->lo_stp->ls_owner,
2176 lckstp->ls_ownerlen))) {
2178 free(other_lop, M_NFSDLOCK);
2181 if (vnode_unlocked != 0)
2182 ret = nfsrv_clientconflict(lop->lo_stp->ls_clp, &haslock,
2185 ret = nfsrv_clientconflict(lop->lo_stp->ls_clp, &haslock,
2188 if (filestruct_locked != 0) {
2189 if (vnode_unlocked == 0) {
2190 ASSERT_VOP_ELOCKED(vp, "nfsrv_lockctrl6");
2191 NFSVOPUNLOCK(vp, 0);
2193 /* Roll back local locks. */
2194 nfsrv_locallock_rollback(vp, lfp, p);
2196 nfsrv_unlocklf(lfp);
2198 NFSVOPLOCK(vp, LK_EXCLUSIVE | LK_RETRY);
2200 if ((vp->v_iflag & VI_DOOMED) != 0) {
2201 error = NFSERR_SERVERFAULT;
2206 * nfsrv_clientconflict() unlocks state when it
2213 * Found a conflicting lock, so record the conflict and
2216 if (cfp != NULL && ret == 0) {
2217 cfp->cl_clientid.lval[0]=lop->lo_stp->ls_stateid.other[0];
2218 cfp->cl_clientid.lval[1]=lop->lo_stp->ls_stateid.other[1];
2219 cfp->cl_first = lop->lo_first;
2220 cfp->cl_end = lop->lo_end;
2221 cfp->cl_flags = lop->lo_flags;
2222 cfp->cl_ownerlen = lop->lo_stp->ls_ownerlen;
2223 NFSBCOPY(lop->lo_stp->ls_owner, cfp->cl_owner,
2227 error = NFSERR_PERM;
2228 else if (new_stp->ls_flags & NFSLCK_RECLAIM)
2229 error = NFSERR_RECLAIMCONFLICT;
2230 else if (new_stp->ls_flags & NFSLCK_CHECK)
2231 error = NFSERR_LOCKED;
2233 error = NFSERR_DENIED;
2234 if (filestruct_locked != 0 && ret == 0) {
2235 /* Roll back local locks. */
2237 if (vnode_unlocked == 0) {
2238 ASSERT_VOP_ELOCKED(vp, "nfsrv_lockctrl7");
2240 NFSVOPUNLOCK(vp, 0);
2242 nfsrv_locallock_rollback(vp, lfp, p);
2244 nfsrv_unlocklf(lfp);
2254 * We only get here if there was no lock that conflicted.
2256 if (new_stp->ls_flags & (NFSLCK_TEST | NFSLCK_CHECK)) {
2262 * We only get here when we are creating or modifying a lock.
2263 * There are two variants:
2264 * - exist_lock_owner where lock_owner exists
2265 * - open_to_lock_owner with new lock_owner
2267 first = new_lop->lo_first;
2268 end = new_lop->lo_end;
2269 lock_flags = new_lop->lo_flags;
2270 if (!(new_stp->ls_flags & NFSLCK_OPENTOLOCK)) {
2271 nfsrv_updatelock(lckstp, new_lopp, &other_lop, lfp);
2272 stateidp->seqid = ++(lckstp->ls_stateid.seqid);
2273 if ((nd->nd_flag & ND_NFSV41) != 0 && stateidp->seqid == 0)
2274 stateidp->seqid = lckstp->ls_stateid.seqid = 1;
2275 stateidp->other[0] = lckstp->ls_stateid.other[0];
2276 stateidp->other[1] = lckstp->ls_stateid.other[1];
2277 stateidp->other[2] = lckstp->ls_stateid.other[2];
2280 * The new open_to_lock_owner case.
2281 * Link the new nfsstate into the lists.
2283 new_stp->ls_seq = new_stp->ls_opentolockseq;
2284 nfsrvd_refcache(new_stp->ls_op);
2285 stateidp->seqid = new_stp->ls_stateid.seqid = 1;
2286 stateidp->other[0] = new_stp->ls_stateid.other[0] =
2287 clp->lc_clientid.lval[0];
2288 stateidp->other[1] = new_stp->ls_stateid.other[1] =
2289 clp->lc_clientid.lval[1];
2290 stateidp->other[2] = new_stp->ls_stateid.other[2] =
2291 nfsrv_nextstateindex(clp);
2292 new_stp->ls_clp = clp;
2293 LIST_INIT(&new_stp->ls_lock);
2294 new_stp->ls_openstp = stp;
2295 new_stp->ls_lfp = lfp;
2296 nfsrv_insertlock(new_lop, (struct nfslock *)new_stp, new_stp,
2298 LIST_INSERT_HEAD(NFSSTATEHASH(clp, new_stp->ls_stateid),
2300 LIST_INSERT_HEAD(&stp->ls_open, new_stp, ls_list);
2303 nfsstatsv1.srvlockowners++;
2304 nfsrv_openpluslock++;
2306 if (filestruct_locked != 0) {
2308 nfsrv_locallock_commit(lfp, lock_flags, first, end);
2310 nfsrv_unlocklf(lfp);
2316 NFSLOCKV4ROOTMUTEX();
2317 nfsv4_unlock(&nfsv4rootfs_lock, 1);
2318 NFSUNLOCKV4ROOTMUTEX();
2320 if (vnode_unlocked != 0) {
2321 NFSVOPLOCK(vp, LK_EXCLUSIVE | LK_RETRY);
2322 if (error == 0 && (vp->v_iflag & VI_DOOMED) != 0)
2323 error = NFSERR_SERVERFAULT;
2326 free(other_lop, M_NFSDLOCK);
2327 NFSEXITCODE2(error, nd);
2332 * Check for state errors for Open.
2333 * repstat is passed back out as an error if more critical errors
2337 nfsrv_opencheck(nfsquad_t clientid, nfsv4stateid_t *stateidp,
2338 struct nfsstate *new_stp, vnode_t vp, struct nfsrv_descript *nd,
2339 NFSPROC_T *p, int repstat)
2341 struct nfsstate *stp, *nstp;
2342 struct nfsclient *clp;
2343 struct nfsstate *ownerstp;
2344 struct nfslockfile *lfp, *new_lfp;
2345 int error = 0, haslock = 0, ret, readonly = 0, getfhret = 0;
2347 if ((new_stp->ls_flags & NFSLCK_SHAREBITS) == NFSLCK_READACCESS)
2350 * Check for restart conditions (client and server).
2352 error = nfsrv_checkrestart(clientid, new_stp->ls_flags,
2353 &new_stp->ls_stateid, 0);
2358 * Check for state resource limit exceeded.
2359 * Technically this should be SMP protected, but the worst
2360 * case error is "out by one or two" on the count when it
2361 * returns NFSERR_RESOURCE and the limit is just a rather
2362 * arbitrary high water mark, so no harm is done.
2364 if (nfsrv_openpluslock > nfsrv_v4statelimit) {
2365 error = NFSERR_RESOURCE;
2370 new_lfp = malloc(sizeof (struct nfslockfile),
2371 M_NFSDLOCKFILE, M_WAITOK);
2373 getfhret = nfsrv_getlockfh(vp, new_stp->ls_flags, new_lfp,
2377 * Get the nfsclient structure.
2379 error = nfsrv_getclient(clientid, CLOPS_RENEW, &clp, NULL,
2380 (nfsquad_t)((u_quad_t)0), 0, nd, p);
2383 * Look up the open owner. See if it needs confirmation and
2384 * check the seq#, as required.
2387 nfsrv_getowner(&clp->lc_open, new_stp, &ownerstp);
2389 if (!error && ownerstp) {
2390 error = nfsrv_checkseqid(nd, new_stp->ls_seq, ownerstp,
2393 * If the OpenOwner hasn't been confirmed, assume the
2394 * old one was a replay and this one is ok.
2395 * See: RFC3530 Sec. 14.2.18.
2397 if (error == NFSERR_BADSEQID &&
2398 (ownerstp->ls_flags & NFSLCK_NEEDSCONFIRM))
2406 error = nfsrv_checkgrace(nd, clp, new_stp->ls_flags);
2407 if ((new_stp->ls_flags & NFSLCK_RECLAIM) && !error &&
2408 nfsrv_checkstable(clp))
2409 error = NFSERR_NOGRACE;
2412 * If none of the above errors occurred, let repstat be
2415 if (repstat && !error)
2420 NFSLOCKV4ROOTMUTEX();
2421 nfsv4_unlock(&nfsv4rootfs_lock, 1);
2422 NFSUNLOCKV4ROOTMUTEX();
2424 free(new_lfp, M_NFSDLOCKFILE);
2429 * If vp == NULL, the file doesn't exist yet, so return ok.
2430 * (This always happens on the first pass, so haslock must be 0.)
2434 free(new_lfp, M_NFSDLOCKFILE);
2439 * Get the structure for the underlying file.
2444 error = nfsrv_getlockfile(new_stp->ls_flags, &new_lfp, &lfp,
2447 free(new_lfp, M_NFSDLOCKFILE);
2451 NFSLOCKV4ROOTMUTEX();
2452 nfsv4_unlock(&nfsv4rootfs_lock, 1);
2453 NFSUNLOCKV4ROOTMUTEX();
2459 * Search for a conflicting open/share.
2461 if (new_stp->ls_flags & NFSLCK_DELEGCUR) {
2463 * For Delegate_Cur, search for the matching Delegation,
2464 * which indicates no conflict.
2465 * An old delegation should have been recovered by the
2466 * client doing a Claim_DELEGATE_Prev, so I won't let
2467 * it match and return NFSERR_EXPIRED. Should I let it
2470 LIST_FOREACH(stp, &lfp->lf_deleg, ls_file) {
2471 if (!(stp->ls_flags & NFSLCK_OLDDELEG) &&
2472 (((nd->nd_flag & ND_NFSV41) != 0 &&
2473 stateidp->seqid == 0) ||
2474 stateidp->seqid == stp->ls_stateid.seqid) &&
2475 !NFSBCMP(stateidp->other, stp->ls_stateid.other,
2479 if (stp == LIST_END(&lfp->lf_deleg) ||
2480 ((new_stp->ls_flags & NFSLCK_WRITEACCESS) &&
2481 (stp->ls_flags & NFSLCK_DELEGREAD))) {
2484 NFSLOCKV4ROOTMUTEX();
2485 nfsv4_unlock(&nfsv4rootfs_lock, 1);
2486 NFSUNLOCKV4ROOTMUTEX();
2488 error = NFSERR_EXPIRED;
2494 * Check for access/deny bit conflicts. I check for the same
2495 * owner as well, in case the client didn't bother.
2497 LIST_FOREACH(stp, &lfp->lf_open, ls_file) {
2498 if (!(new_stp->ls_flags & NFSLCK_DELEGCUR) &&
2499 (((new_stp->ls_flags & NFSLCK_ACCESSBITS) &
2500 ((stp->ls_flags>>NFSLCK_SHIFT) & NFSLCK_ACCESSBITS))||
2501 ((stp->ls_flags & NFSLCK_ACCESSBITS) &
2502 ((new_stp->ls_flags>>NFSLCK_SHIFT)&NFSLCK_ACCESSBITS)))){
2503 ret = nfsrv_clientconflict(stp->ls_clp,&haslock,vp,p);
2506 * nfsrv_clientconflict() unlocks
2507 * state when it returns non-zero.
2512 error = NFSERR_PERM;
2513 else if (new_stp->ls_flags & NFSLCK_RECLAIM)
2514 error = NFSERR_RECLAIMCONFLICT;
2516 error = NFSERR_SHAREDENIED;
2520 NFSLOCKV4ROOTMUTEX();
2521 nfsv4_unlock(&nfsv4rootfs_lock, 1);
2522 NFSUNLOCKV4ROOTMUTEX();
2529 * Check for a conflicting delegation. If one is found, call
2530 * nfsrv_delegconflict() to handle it. If the v4root lock hasn't
2531 * been set yet, it will get the lock. Otherwise, it will recall
2532 * the delegation. Then, we try try again...
2533 * (If NFSLCK_DELEGCUR is set, it has a delegation, so there
2534 * isn't a conflict.)
2535 * I currently believe the conflict algorithm to be:
2536 * For Open with Read Access and Deny None
2537 * - there is a conflict iff a different client has a write delegation
2538 * For Open with other Write Access or any Deny except None
2539 * - there is a conflict if a different client has any delegation
2540 * - there is a conflict if the same client has a read delegation
2541 * (The current consensus is that this last case should be
2542 * considered a conflict since the client with a read delegation
2543 * could have done an Open with ReadAccess and WriteDeny
2544 * locally and then not have checked for the WriteDeny.)
2545 * Don't check for a Reclaim, since that will be dealt with
2546 * by nfsrv_openctrl().
2548 if (!(new_stp->ls_flags &
2549 (NFSLCK_DELEGPREV | NFSLCK_DELEGCUR | NFSLCK_RECLAIM))) {
2550 stp = LIST_FIRST(&lfp->lf_deleg);
2551 while (stp != LIST_END(&lfp->lf_deleg)) {
2552 nstp = LIST_NEXT(stp, ls_file);
2553 if ((readonly && stp->ls_clp != clp &&
2554 (stp->ls_flags & NFSLCK_DELEGWRITE)) ||
2555 (!readonly && (stp->ls_clp != clp ||
2556 (stp->ls_flags & NFSLCK_DELEGREAD)))) {
2557 ret = nfsrv_delegconflict(stp, &haslock, p, vp);
2560 * nfsrv_delegconflict() unlocks state
2561 * when it returns non-zero.
2574 NFSLOCKV4ROOTMUTEX();
2575 nfsv4_unlock(&nfsv4rootfs_lock, 1);
2576 NFSUNLOCKV4ROOTMUTEX();
2580 NFSEXITCODE2(error, nd);
2585 * Open control function to create/update open state for an open.
2588 nfsrv_openctrl(struct nfsrv_descript *nd, vnode_t vp,
2589 struct nfsstate **new_stpp, nfsquad_t clientid, nfsv4stateid_t *stateidp,
2590 nfsv4stateid_t *delegstateidp, u_int32_t *rflagsp, struct nfsexstuff *exp,
2591 NFSPROC_T *p, u_quad_t filerev)
2593 struct nfsstate *new_stp = *new_stpp;
2594 struct nfsstate *stp, *nstp;
2595 struct nfsstate *openstp = NULL, *new_open, *ownerstp, *new_deleg;
2596 struct nfslockfile *lfp, *new_lfp;
2597 struct nfsclient *clp;
2598 int error = 0, haslock = 0, ret, delegate = 1, writedeleg = 1;
2599 int readonly = 0, cbret = 1, getfhret = 0;
2600 int gotstate = 0, len = 0;
2601 u_char *clidp = NULL;
2603 if ((new_stp->ls_flags & NFSLCK_SHAREBITS) == NFSLCK_READACCESS)
2606 * Check for restart conditions (client and server).
2607 * (Paranoia, should have been detected by nfsrv_opencheck().)
2608 * If an error does show up, return NFSERR_EXPIRED, since the
2609 * the seqid# has already been incremented.
2611 error = nfsrv_checkrestart(clientid, new_stp->ls_flags,
2612 &new_stp->ls_stateid, 0);
2614 printf("Nfsd: openctrl unexpected restart err=%d\n",
2616 error = NFSERR_EXPIRED;
2620 clidp = malloc(NFSV4_OPAQUELIMIT, M_TEMP, M_WAITOK);
2622 new_lfp = malloc(sizeof (struct nfslockfile),
2623 M_NFSDLOCKFILE, M_WAITOK);
2624 new_open = malloc(sizeof (struct nfsstate),
2625 M_NFSDSTATE, M_WAITOK);
2626 new_deleg = malloc(sizeof (struct nfsstate),
2627 M_NFSDSTATE, M_WAITOK);
2628 getfhret = nfsrv_getlockfh(vp, new_stp->ls_flags, new_lfp,
2632 * Get the client structure. Since the linked lists could be changed
2633 * by other nfsd processes if this process does a tsleep(), one of
2634 * two things must be done.
2635 * 1 - don't tsleep()
2637 * 2 - get the nfsv4_lock() { indicated by haslock == 1 }
2638 * before using the lists, since this lock stops the other
2639 * nfsd. This should only be used for rare cases, since it
2640 * essentially single threads the nfsd.
2641 * At this time, it is only done for cases where the stable
2642 * storage file must be written prior to completion of state
2645 error = nfsrv_getclient(clientid, CLOPS_RENEW, &clp, NULL,
2646 (nfsquad_t)((u_quad_t)0), 0, nd, p);
2647 if (!error && (clp->lc_flags & LCL_NEEDSCBNULL) &&
2650 * This happens on the first open for a client
2651 * that supports callbacks.
2655 * Although nfsrv_docallback() will sleep, clp won't
2656 * go away, since they are only removed when the
2657 * nfsv4_lock() has blocked the nfsd threads. The
2658 * fields in clp can change, but having multiple
2659 * threads do this Null callback RPC should be
2662 cbret = nfsrv_docallback(clp, NFSV4PROC_CBNULL,
2663 NULL, 0, NULL, NULL, NULL, 0, p);
2665 clp->lc_flags &= ~LCL_NEEDSCBNULL;
2667 clp->lc_flags |= LCL_CALLBACKSON;
2671 * Look up the open owner. See if it needs confirmation and
2672 * check the seq#, as required.
2675 nfsrv_getowner(&clp->lc_open, new_stp, &ownerstp);
2679 printf("Nfsd: openctrl unexpected state err=%d\n",
2681 free(new_lfp, M_NFSDLOCKFILE);
2682 free(new_open, M_NFSDSTATE);
2683 free(new_deleg, M_NFSDSTATE);
2685 NFSLOCKV4ROOTMUTEX();
2686 nfsv4_unlock(&nfsv4rootfs_lock, 1);
2687 NFSUNLOCKV4ROOTMUTEX();
2689 error = NFSERR_EXPIRED;
2693 if (new_stp->ls_flags & NFSLCK_RECLAIM)
2694 nfsrv_markstable(clp);
2697 * Get the structure for the underlying file.
2702 error = nfsrv_getlockfile(new_stp->ls_flags, &new_lfp, &lfp,
2705 free(new_lfp, M_NFSDLOCKFILE);
2708 printf("Nfsd openctrl unexpected getlockfile err=%d\n",
2710 free(new_open, M_NFSDSTATE);
2711 free(new_deleg, M_NFSDSTATE);
2713 NFSLOCKV4ROOTMUTEX();
2714 nfsv4_unlock(&nfsv4rootfs_lock, 1);
2715 NFSUNLOCKV4ROOTMUTEX();
2721 * Search for a conflicting open/share.
2723 if (new_stp->ls_flags & NFSLCK_DELEGCUR) {
2725 * For Delegate_Cur, search for the matching Delegation,
2726 * which indicates no conflict.
2727 * An old delegation should have been recovered by the
2728 * client doing a Claim_DELEGATE_Prev, so I won't let
2729 * it match and return NFSERR_EXPIRED. Should I let it
2732 LIST_FOREACH(stp, &lfp->lf_deleg, ls_file) {
2733 if (!(stp->ls_flags & NFSLCK_OLDDELEG) &&
2734 (((nd->nd_flag & ND_NFSV41) != 0 &&
2735 stateidp->seqid == 0) ||
2736 stateidp->seqid == stp->ls_stateid.seqid) &&
2737 !NFSBCMP(stateidp->other, stp->ls_stateid.other,
2741 if (stp == LIST_END(&lfp->lf_deleg) ||
2742 ((new_stp->ls_flags & NFSLCK_WRITEACCESS) &&
2743 (stp->ls_flags & NFSLCK_DELEGREAD))) {
2745 printf("Nfsd openctrl unexpected expiry\n");
2746 free(new_open, M_NFSDSTATE);
2747 free(new_deleg, M_NFSDSTATE);
2749 NFSLOCKV4ROOTMUTEX();
2750 nfsv4_unlock(&nfsv4rootfs_lock, 1);
2751 NFSUNLOCKV4ROOTMUTEX();
2753 error = NFSERR_EXPIRED;
2758 * Don't issue a Delegation, since one already exists and
2759 * delay delegation timeout, as required.
2762 nfsrv_delaydelegtimeout(stp);
2766 * Check for access/deny bit conflicts. I also check for the
2767 * same owner, since the client might not have bothered to check.
2768 * Also, note an open for the same file and owner, if found,
2769 * which is all we do here for Delegate_Cur, since conflict
2770 * checking is already done.
2772 LIST_FOREACH(stp, &lfp->lf_open, ls_file) {
2773 if (ownerstp && stp->ls_openowner == ownerstp)
2775 if (!(new_stp->ls_flags & NFSLCK_DELEGCUR)) {
2777 * If another client has the file open, the only
2778 * delegation that can be issued is a Read delegation
2779 * and only if it is a Read open with Deny none.
2781 if (clp != stp->ls_clp) {
2782 if ((stp->ls_flags & NFSLCK_SHAREBITS) ==
2788 if(((new_stp->ls_flags & NFSLCK_ACCESSBITS) &
2789 ((stp->ls_flags>>NFSLCK_SHIFT) & NFSLCK_ACCESSBITS))||
2790 ((stp->ls_flags & NFSLCK_ACCESSBITS) &
2791 ((new_stp->ls_flags>>NFSLCK_SHIFT)&NFSLCK_ACCESSBITS))){
2792 ret = nfsrv_clientconflict(stp->ls_clp,&haslock,vp,p);
2795 * nfsrv_clientconflict() unlocks state
2796 * when it returns non-zero.
2798 free(new_open, M_NFSDSTATE);
2799 free(new_deleg, M_NFSDSTATE);
2804 error = NFSERR_PERM;
2805 else if (new_stp->ls_flags & NFSLCK_RECLAIM)
2806 error = NFSERR_RECLAIMCONFLICT;
2808 error = NFSERR_SHAREDENIED;
2812 NFSLOCKV4ROOTMUTEX();
2813 nfsv4_unlock(&nfsv4rootfs_lock, 1);
2814 NFSUNLOCKV4ROOTMUTEX();
2816 free(new_open, M_NFSDSTATE);
2817 free(new_deleg, M_NFSDSTATE);
2818 printf("nfsd openctrl unexpected client cnfl\n");
2825 * Check for a conflicting delegation. If one is found, call
2826 * nfsrv_delegconflict() to handle it. If the v4root lock hasn't
2827 * been set yet, it will get the lock. Otherwise, it will recall
2828 * the delegation. Then, we try try again...
2829 * (If NFSLCK_DELEGCUR is set, it has a delegation, so there
2830 * isn't a conflict.)
2831 * I currently believe the conflict algorithm to be:
2832 * For Open with Read Access and Deny None
2833 * - there is a conflict iff a different client has a write delegation
2834 * For Open with other Write Access or any Deny except None
2835 * - there is a conflict if a different client has any delegation
2836 * - there is a conflict if the same client has a read delegation
2837 * (The current consensus is that this last case should be
2838 * considered a conflict since the client with a read delegation
2839 * could have done an Open with ReadAccess and WriteDeny
2840 * locally and then not have checked for the WriteDeny.)
2842 if (!(new_stp->ls_flags & (NFSLCK_DELEGPREV | NFSLCK_DELEGCUR))) {
2843 stp = LIST_FIRST(&lfp->lf_deleg);
2844 while (stp != LIST_END(&lfp->lf_deleg)) {
2845 nstp = LIST_NEXT(stp, ls_file);
2846 if (stp->ls_clp != clp && (stp->ls_flags & NFSLCK_DELEGREAD))
2850 if ((readonly && stp->ls_clp != clp &&
2851 (stp->ls_flags & NFSLCK_DELEGWRITE)) ||
2852 (!readonly && (stp->ls_clp != clp ||
2853 (stp->ls_flags & NFSLCK_DELEGREAD)))) {
2854 if (new_stp->ls_flags & NFSLCK_RECLAIM) {
2857 ret = nfsrv_delegconflict(stp, &haslock, p, vp);
2860 * nfsrv_delegconflict() unlocks state
2861 * when it returns non-zero.
2863 printf("Nfsd openctrl unexpected deleg cnfl\n");
2864 free(new_open, M_NFSDSTATE);
2865 free(new_deleg, M_NFSDSTATE);
2880 * We only get here if there was no open that conflicted.
2881 * If an open for the owner exists, or in the access/deny bits.
2882 * Otherwise it is a new open. If the open_owner hasn't been
2883 * confirmed, replace the open with the new one needing confirmation,
2884 * otherwise add the open.
2886 if (new_stp->ls_flags & NFSLCK_DELEGPREV) {
2888 * Handle NFSLCK_DELEGPREV by searching the old delegations for
2889 * a match. If found, just move the old delegation to the current
2890 * delegation list and issue open. If not found, return
2893 LIST_FOREACH(stp, &clp->lc_olddeleg, ls_list) {
2894 if (stp->ls_lfp == lfp) {
2896 if (stp->ls_clp != clp)
2897 panic("olddeleg clp");
2898 LIST_REMOVE(stp, ls_list);
2899 LIST_REMOVE(stp, ls_hash);
2900 stp->ls_flags &= ~NFSLCK_OLDDELEG;
2901 stp->ls_stateid.seqid = delegstateidp->seqid = 1;
2902 stp->ls_stateid.other[0] = delegstateidp->other[0] =
2903 clp->lc_clientid.lval[0];
2904 stp->ls_stateid.other[1] = delegstateidp->other[1] =
2905 clp->lc_clientid.lval[1];
2906 stp->ls_stateid.other[2] = delegstateidp->other[2] =
2907 nfsrv_nextstateindex(clp);
2908 stp->ls_compref = nd->nd_compref;
2909 LIST_INSERT_HEAD(&clp->lc_deleg, stp, ls_list);
2910 LIST_INSERT_HEAD(NFSSTATEHASH(clp,
2911 stp->ls_stateid), stp, ls_hash);
2912 if (stp->ls_flags & NFSLCK_DELEGWRITE)
2913 *rflagsp |= NFSV4OPEN_WRITEDELEGATE;
2915 *rflagsp |= NFSV4OPEN_READDELEGATE;
2916 clp->lc_delegtime = NFSD_MONOSEC +
2917 nfsrv_lease + NFSRV_LEASEDELTA;
2920 * Now, do the associated open.
2922 new_open->ls_stateid.seqid = 1;
2923 new_open->ls_stateid.other[0] = clp->lc_clientid.lval[0];
2924 new_open->ls_stateid.other[1] = clp->lc_clientid.lval[1];
2925 new_open->ls_stateid.other[2] = nfsrv_nextstateindex(clp);
2926 new_open->ls_flags = (new_stp->ls_flags&NFSLCK_DENYBITS)|
2928 if (stp->ls_flags & NFSLCK_DELEGWRITE)
2929 new_open->ls_flags |= (NFSLCK_READACCESS |
2930 NFSLCK_WRITEACCESS);
2932 new_open->ls_flags |= NFSLCK_READACCESS;
2933 new_open->ls_uid = new_stp->ls_uid;
2934 new_open->ls_lfp = lfp;
2935 new_open->ls_clp = clp;
2936 LIST_INIT(&new_open->ls_open);
2937 LIST_INSERT_HEAD(&lfp->lf_open, new_open, ls_file);
2938 LIST_INSERT_HEAD(NFSSTATEHASH(clp, new_open->ls_stateid),
2941 * and handle the open owner
2944 new_open->ls_openowner = ownerstp;
2945 LIST_INSERT_HEAD(&ownerstp->ls_open,new_open,ls_list);
2947 new_open->ls_openowner = new_stp;
2948 new_stp->ls_flags = 0;
2949 nfsrvd_refcache(new_stp->ls_op);
2950 new_stp->ls_noopens = 0;
2951 LIST_INIT(&new_stp->ls_open);
2952 LIST_INSERT_HEAD(&new_stp->ls_open, new_open, ls_list);
2953 LIST_INSERT_HEAD(&clp->lc_open, new_stp, ls_list);
2955 nfsstatsv1.srvopenowners++;
2956 nfsrv_openpluslock++;
2960 nfsstatsv1.srvopens++;
2961 nfsrv_openpluslock++;
2965 if (stp == LIST_END(&clp->lc_olddeleg))
2966 error = NFSERR_EXPIRED;
2967 } else if (new_stp->ls_flags & (NFSLCK_DELEGREAD | NFSLCK_DELEGWRITE)) {
2969 * Scan to see that no delegation for this client and file
2970 * doesn't already exist.
2971 * There also shouldn't yet be an Open for this file and
2974 LIST_FOREACH(stp, &lfp->lf_deleg, ls_file) {
2975 if (stp->ls_clp == clp)
2978 if (stp == LIST_END(&lfp->lf_deleg) && openstp == NULL) {
2980 * This is the Claim_Previous case with a delegation
2981 * type != Delegate_None.
2984 * First, add the delegation. (Although we must issue the
2985 * delegation, we can also ask for an immediate return.)
2987 new_deleg->ls_stateid.seqid = delegstateidp->seqid = 1;
2988 new_deleg->ls_stateid.other[0] = delegstateidp->other[0] =
2989 clp->lc_clientid.lval[0];
2990 new_deleg->ls_stateid.other[1] = delegstateidp->other[1] =
2991 clp->lc_clientid.lval[1];
2992 new_deleg->ls_stateid.other[2] = delegstateidp->other[2] =
2993 nfsrv_nextstateindex(clp);
2994 if (new_stp->ls_flags & NFSLCK_DELEGWRITE) {
2995 new_deleg->ls_flags = (NFSLCK_DELEGWRITE |
2996 NFSLCK_READACCESS | NFSLCK_WRITEACCESS);
2997 *rflagsp |= NFSV4OPEN_WRITEDELEGATE;
2998 nfsrv_writedelegcnt++;
3000 new_deleg->ls_flags = (NFSLCK_DELEGREAD |
3002 *rflagsp |= NFSV4OPEN_READDELEGATE;
3004 new_deleg->ls_uid = new_stp->ls_uid;
3005 new_deleg->ls_lfp = lfp;
3006 new_deleg->ls_clp = clp;
3007 new_deleg->ls_filerev = filerev;
3008 new_deleg->ls_compref = nd->nd_compref;
3009 LIST_INSERT_HEAD(&lfp->lf_deleg, new_deleg, ls_file);
3010 LIST_INSERT_HEAD(NFSSTATEHASH(clp,
3011 new_deleg->ls_stateid), new_deleg, ls_hash);
3012 LIST_INSERT_HEAD(&clp->lc_deleg, new_deleg, ls_list);
3014 if (delegate == 2 || nfsrv_issuedelegs == 0 ||
3015 (clp->lc_flags & (LCL_CALLBACKSON | LCL_CBDOWN)) !=
3017 NFSRV_V4DELEGLIMIT(nfsrv_delegatecnt) ||
3018 !NFSVNO_DELEGOK(vp))
3019 *rflagsp |= NFSV4OPEN_RECALL;
3020 nfsstatsv1.srvdelegates++;
3021 nfsrv_openpluslock++;
3022 nfsrv_delegatecnt++;
3025 * Now, do the associated open.
3027 new_open->ls_stateid.seqid = 1;
3028 new_open->ls_stateid.other[0] = clp->lc_clientid.lval[0];
3029 new_open->ls_stateid.other[1] = clp->lc_clientid.lval[1];
3030 new_open->ls_stateid.other[2] = nfsrv_nextstateindex(clp);
3031 new_open->ls_flags = (new_stp->ls_flags & NFSLCK_DENYBITS) |
3033 if (new_stp->ls_flags & NFSLCK_DELEGWRITE)
3034 new_open->ls_flags |= (NFSLCK_READACCESS |
3035 NFSLCK_WRITEACCESS);
3037 new_open->ls_flags |= NFSLCK_READACCESS;
3038 new_open->ls_uid = new_stp->ls_uid;
3039 new_open->ls_lfp = lfp;
3040 new_open->ls_clp = clp;
3041 LIST_INIT(&new_open->ls_open);
3042 LIST_INSERT_HEAD(&lfp->lf_open, new_open, ls_file);
3043 LIST_INSERT_HEAD(NFSSTATEHASH(clp, new_open->ls_stateid),
3046 * and handle the open owner
3049 new_open->ls_openowner = ownerstp;
3050 LIST_INSERT_HEAD(&ownerstp->ls_open, new_open, ls_list);
3052 new_open->ls_openowner = new_stp;
3053 new_stp->ls_flags = 0;
3054 nfsrvd_refcache(new_stp->ls_op);
3055 new_stp->ls_noopens = 0;
3056 LIST_INIT(&new_stp->ls_open);
3057 LIST_INSERT_HEAD(&new_stp->ls_open, new_open, ls_list);
3058 LIST_INSERT_HEAD(&clp->lc_open, new_stp, ls_list);
3060 nfsstatsv1.srvopenowners++;
3061 nfsrv_openpluslock++;
3065 nfsstatsv1.srvopens++;
3066 nfsrv_openpluslock++;
3068 error = NFSERR_RECLAIMCONFLICT;
3070 } else if (ownerstp) {
3071 if (ownerstp->ls_flags & NFSLCK_NEEDSCONFIRM) {
3072 /* Replace the open */
3073 if (ownerstp->ls_op)
3074 nfsrvd_derefcache(ownerstp->ls_op);
3075 ownerstp->ls_op = new_stp->ls_op;
3076 nfsrvd_refcache(ownerstp->ls_op);
3077 ownerstp->ls_seq = new_stp->ls_seq;
3078 *rflagsp |= NFSV4OPEN_RESULTCONFIRM;
3079 stp = LIST_FIRST(&ownerstp->ls_open);
3080 stp->ls_flags = (new_stp->ls_flags & NFSLCK_SHAREBITS) |
3082 stp->ls_stateid.seqid = 1;
3083 stp->ls_uid = new_stp->ls_uid;
3084 if (lfp != stp->ls_lfp) {
3085 LIST_REMOVE(stp, ls_file);
3086 LIST_INSERT_HEAD(&lfp->lf_open, stp, ls_file);
3090 } else if (openstp) {
3091 openstp->ls_flags |= (new_stp->ls_flags & NFSLCK_SHAREBITS);
3092 openstp->ls_stateid.seqid++;
3093 if ((nd->nd_flag & ND_NFSV41) != 0 &&
3094 openstp->ls_stateid.seqid == 0)
3095 openstp->ls_stateid.seqid = 1;
3098 * This is where we can choose to issue a delegation.
3100 if ((new_stp->ls_flags & NFSLCK_WANTNODELEG) != 0)
3101 *rflagsp |= NFSV4OPEN_WDNOTWANTED;
3102 else if (nfsrv_issuedelegs == 0)
3103 *rflagsp |= NFSV4OPEN_WDSUPPFTYPE;
3104 else if (NFSRV_V4DELEGLIMIT(nfsrv_delegatecnt))
3105 *rflagsp |= NFSV4OPEN_WDRESOURCE;
3106 else if (delegate == 0 || writedeleg == 0 ||
3107 NFSVNO_EXRDONLY(exp) || (readonly != 0 &&
3108 nfsrv_writedelegifpos == 0) ||
3109 !NFSVNO_DELEGOK(vp) ||
3110 (new_stp->ls_flags & NFSLCK_WANTRDELEG) != 0 ||
3111 (clp->lc_flags & (LCL_CALLBACKSON | LCL_CBDOWN)) !=
3113 *rflagsp |= NFSV4OPEN_WDCONTENTION;
3115 new_deleg->ls_stateid.seqid = delegstateidp->seqid = 1;
3116 new_deleg->ls_stateid.other[0] = delegstateidp->other[0]
3117 = clp->lc_clientid.lval[0];
3118 new_deleg->ls_stateid.other[1] = delegstateidp->other[1]
3119 = clp->lc_clientid.lval[1];
3120 new_deleg->ls_stateid.other[2] = delegstateidp->other[2]
3121 = nfsrv_nextstateindex(clp);
3122 new_deleg->ls_flags = (NFSLCK_DELEGWRITE |
3123 NFSLCK_READACCESS | NFSLCK_WRITEACCESS);
3124 *rflagsp |= NFSV4OPEN_WRITEDELEGATE;
3125 new_deleg->ls_uid = new_stp->ls_uid;
3126 new_deleg->ls_lfp = lfp;
3127 new_deleg->ls_clp = clp;
3128 new_deleg->ls_filerev = filerev;
3129 new_deleg->ls_compref = nd->nd_compref;
3130 nfsrv_writedelegcnt++;
3131 LIST_INSERT_HEAD(&lfp->lf_deleg, new_deleg, ls_file);
3132 LIST_INSERT_HEAD(NFSSTATEHASH(clp,
3133 new_deleg->ls_stateid), new_deleg, ls_hash);
3134 LIST_INSERT_HEAD(&clp->lc_deleg, new_deleg, ls_list);
3136 nfsstatsv1.srvdelegates++;
3137 nfsrv_openpluslock++;
3138 nfsrv_delegatecnt++;
3141 new_open->ls_stateid.seqid = 1;
3142 new_open->ls_stateid.other[0] = clp->lc_clientid.lval[0];
3143 new_open->ls_stateid.other[1] = clp->lc_clientid.lval[1];
3144 new_open->ls_stateid.other[2] = nfsrv_nextstateindex(clp);
3145 new_open->ls_flags = (new_stp->ls_flags & NFSLCK_SHAREBITS)|
3147 new_open->ls_uid = new_stp->ls_uid;
3148 new_open->ls_openowner = ownerstp;
3149 new_open->ls_lfp = lfp;
3150 new_open->ls_clp = clp;
3151 LIST_INIT(&new_open->ls_open);
3152 LIST_INSERT_HEAD(&lfp->lf_open, new_open, ls_file);
3153 LIST_INSERT_HEAD(&ownerstp->ls_open, new_open, ls_list);
3154 LIST_INSERT_HEAD(NFSSTATEHASH(clp, new_open->ls_stateid),
3158 nfsstatsv1.srvopens++;
3159 nfsrv_openpluslock++;
3162 * This is where we can choose to issue a delegation.
3164 if ((new_stp->ls_flags & NFSLCK_WANTNODELEG) != 0)
3165 *rflagsp |= NFSV4OPEN_WDNOTWANTED;
3166 else if (nfsrv_issuedelegs == 0)
3167 *rflagsp |= NFSV4OPEN_WDSUPPFTYPE;
3168 else if (NFSRV_V4DELEGLIMIT(nfsrv_delegatecnt))
3169 *rflagsp |= NFSV4OPEN_WDRESOURCE;
3170 else if (delegate == 0 || (writedeleg == 0 &&
3171 readonly == 0) || !NFSVNO_DELEGOK(vp) ||
3172 (clp->lc_flags & (LCL_CALLBACKSON | LCL_CBDOWN)) !=
3174 *rflagsp |= NFSV4OPEN_WDCONTENTION;
3176 new_deleg->ls_stateid.seqid = delegstateidp->seqid = 1;
3177 new_deleg->ls_stateid.other[0] = delegstateidp->other[0]
3178 = clp->lc_clientid.lval[0];
3179 new_deleg->ls_stateid.other[1] = delegstateidp->other[1]
3180 = clp->lc_clientid.lval[1];
3181 new_deleg->ls_stateid.other[2] = delegstateidp->other[2]
3182 = nfsrv_nextstateindex(clp);
3183 if (writedeleg && !NFSVNO_EXRDONLY(exp) &&
3184 (nfsrv_writedelegifpos || !readonly) &&
3185 (new_stp->ls_flags & NFSLCK_WANTRDELEG) == 0) {
3186 new_deleg->ls_flags = (NFSLCK_DELEGWRITE |
3187 NFSLCK_READACCESS | NFSLCK_WRITEACCESS);
3188 *rflagsp |= NFSV4OPEN_WRITEDELEGATE;
3189 nfsrv_writedelegcnt++;
3191 new_deleg->ls_flags = (NFSLCK_DELEGREAD |
3193 *rflagsp |= NFSV4OPEN_READDELEGATE;
3195 new_deleg->ls_uid = new_stp->ls_uid;
3196 new_deleg->ls_lfp = lfp;
3197 new_deleg->ls_clp = clp;
3198 new_deleg->ls_filerev = filerev;
3199 new_deleg->ls_compref = nd->nd_compref;
3200 LIST_INSERT_HEAD(&lfp->lf_deleg, new_deleg, ls_file);
3201 LIST_INSERT_HEAD(NFSSTATEHASH(clp,
3202 new_deleg->ls_stateid), new_deleg, ls_hash);
3203 LIST_INSERT_HEAD(&clp->lc_deleg, new_deleg, ls_list);
3205 nfsstatsv1.srvdelegates++;
3206 nfsrv_openpluslock++;
3207 nfsrv_delegatecnt++;
3212 * New owner case. Start the open_owner sequence with a
3213 * Needs confirmation (unless a reclaim) and hang the
3216 new_open->ls_stateid.seqid = 1;
3217 new_open->ls_stateid.other[0] = clp->lc_clientid.lval[0];
3218 new_open->ls_stateid.other[1] = clp->lc_clientid.lval[1];
3219 new_open->ls_stateid.other[2] = nfsrv_nextstateindex(clp);
3220 new_open->ls_flags = (new_stp->ls_flags & NFSLCK_SHAREBITS) |
3222 new_open->ls_uid = new_stp->ls_uid;
3223 LIST_INIT(&new_open->ls_open);
3224 new_open->ls_openowner = new_stp;
3225 new_open->ls_lfp = lfp;
3226 new_open->ls_clp = clp;
3227 LIST_INSERT_HEAD(&lfp->lf_open, new_open, ls_file);
3228 if (new_stp->ls_flags & NFSLCK_RECLAIM) {
3229 new_stp->ls_flags = 0;
3230 } else if ((nd->nd_flag & ND_NFSV41) != 0) {
3231 /* NFSv4.1 never needs confirmation. */
3232 new_stp->ls_flags = 0;
3235 * This is where we can choose to issue a delegation.
3237 if (delegate && nfsrv_issuedelegs &&
3238 (writedeleg || readonly) &&
3239 (clp->lc_flags & (LCL_CALLBACKSON | LCL_CBDOWN)) ==
3241 !NFSRV_V4DELEGLIMIT(nfsrv_delegatecnt) &&
3242 NFSVNO_DELEGOK(vp) &&
3243 ((nd->nd_flag & ND_NFSV41) == 0 ||
3244 (new_stp->ls_flags & NFSLCK_WANTNODELEG) == 0)) {
3245 new_deleg->ls_stateid.seqid =
3246 delegstateidp->seqid = 1;
3247 new_deleg->ls_stateid.other[0] =
3248 delegstateidp->other[0]
3249 = clp->lc_clientid.lval[0];
3250 new_deleg->ls_stateid.other[1] =
3251 delegstateidp->other[1]
3252 = clp->lc_clientid.lval[1];
3253 new_deleg->ls_stateid.other[2] =
3254 delegstateidp->other[2]
3255 = nfsrv_nextstateindex(clp);
3256 if (writedeleg && !NFSVNO_EXRDONLY(exp) &&
3257 (nfsrv_writedelegifpos || !readonly) &&
3258 ((nd->nd_flag & ND_NFSV41) == 0 ||
3259 (new_stp->ls_flags & NFSLCK_WANTRDELEG) ==
3261 new_deleg->ls_flags =
3262 (NFSLCK_DELEGWRITE |
3264 NFSLCK_WRITEACCESS);
3265 *rflagsp |= NFSV4OPEN_WRITEDELEGATE;
3266 nfsrv_writedelegcnt++;
3268 new_deleg->ls_flags =
3271 *rflagsp |= NFSV4OPEN_READDELEGATE;
3273 new_deleg->ls_uid = new_stp->ls_uid;
3274 new_deleg->ls_lfp = lfp;
3275 new_deleg->ls_clp = clp;
3276 new_deleg->ls_filerev = filerev;
3277 new_deleg->ls_compref = nd->nd_compref;
3278 LIST_INSERT_HEAD(&lfp->lf_deleg, new_deleg,
3280 LIST_INSERT_HEAD(NFSSTATEHASH(clp,
3281 new_deleg->ls_stateid), new_deleg, ls_hash);
3282 LIST_INSERT_HEAD(&clp->lc_deleg, new_deleg,
3285 nfsstatsv1.srvdelegates++;
3286 nfsrv_openpluslock++;
3287 nfsrv_delegatecnt++;
3290 * Since NFSv4.1 never does an OpenConfirm, the first
3291 * open state will be acquired here.
3293 if (!(clp->lc_flags & LCL_STAMPEDSTABLE)) {
3294 clp->lc_flags |= LCL_STAMPEDSTABLE;
3295 len = clp->lc_idlen;
3296 NFSBCOPY(clp->lc_id, clidp, len);
3300 *rflagsp |= NFSV4OPEN_RESULTCONFIRM;
3301 new_stp->ls_flags = NFSLCK_NEEDSCONFIRM;
3303 nfsrvd_refcache(new_stp->ls_op);
3304 new_stp->ls_noopens = 0;
3305 LIST_INIT(&new_stp->ls_open);
3306 LIST_INSERT_HEAD(&new_stp->ls_open, new_open, ls_list);
3307 LIST_INSERT_HEAD(&clp->lc_open, new_stp, ls_list);
3308 LIST_INSERT_HEAD(NFSSTATEHASH(clp, new_open->ls_stateid),
3313 nfsstatsv1.srvopens++;
3314 nfsrv_openpluslock++;
3315 nfsstatsv1.srvopenowners++;
3316 nfsrv_openpluslock++;
3319 stateidp->seqid = openstp->ls_stateid.seqid;
3320 stateidp->other[0] = openstp->ls_stateid.other[0];
3321 stateidp->other[1] = openstp->ls_stateid.other[1];
3322 stateidp->other[2] = openstp->ls_stateid.other[2];
3326 NFSLOCKV4ROOTMUTEX();
3327 nfsv4_unlock(&nfsv4rootfs_lock, 1);
3328 NFSUNLOCKV4ROOTMUTEX();
3331 free(new_open, M_NFSDSTATE);
3333 free(new_deleg, M_NFSDSTATE);
3336 * If the NFSv4.1 client just acquired its first open, write a timestamp
3337 * to the stable storage file.
3339 if (gotstate != 0) {
3340 nfsrv_writestable(clidp, len, NFSNST_NEWSTATE, p);
3341 nfsrv_backupstable();
3345 free(clidp, M_TEMP);
3346 NFSEXITCODE2(error, nd);
3351 * Open update. Does the confirm, downgrade and close.
3354 nfsrv_openupdate(vnode_t vp, struct nfsstate *new_stp, nfsquad_t clientid,
3355 nfsv4stateid_t *stateidp, struct nfsrv_descript *nd, NFSPROC_T *p,
3356 int *retwriteaccessp)
3358 struct nfsstate *stp;
3359 struct nfsclient *clp;
3360 struct nfslockfile *lfp;
3362 int error = 0, gotstate = 0, len = 0;
3363 u_char *clidp = NULL;
3366 * Check for restart conditions (client and server).
3368 error = nfsrv_checkrestart(clientid, new_stp->ls_flags,
3369 &new_stp->ls_stateid, 0);
3373 clidp = malloc(NFSV4_OPAQUELIMIT, M_TEMP, M_WAITOK);
3376 * Get the open structure via clientid and stateid.
3378 error = nfsrv_getclient(clientid, CLOPS_RENEW, &clp, NULL,
3379 (nfsquad_t)((u_quad_t)0), 0, nd, p);
3381 error = nfsrv_getstate(clp, &new_stp->ls_stateid,
3382 new_stp->ls_flags, &stp);
3385 * Sanity check the open.
3387 if (!error && (!(stp->ls_flags & NFSLCK_OPEN) ||
3388 (!(new_stp->ls_flags & NFSLCK_CONFIRM) &&
3389 (stp->ls_openowner->ls_flags & NFSLCK_NEEDSCONFIRM)) ||
3390 ((new_stp->ls_flags & NFSLCK_CONFIRM) &&
3391 (!(stp->ls_openowner->ls_flags & NFSLCK_NEEDSCONFIRM)))))
3392 error = NFSERR_BADSTATEID;
3395 error = nfsrv_checkseqid(nd, new_stp->ls_seq,
3396 stp->ls_openowner, new_stp->ls_op);
3397 if (!error && stp->ls_stateid.seqid != new_stp->ls_stateid.seqid &&
3398 (((nd->nd_flag & ND_NFSV41) == 0 &&
3399 !(new_stp->ls_flags & NFSLCK_CONFIRM)) ||
3400 ((nd->nd_flag & ND_NFSV41) != 0 &&
3401 new_stp->ls_stateid.seqid != 0)))
3402 error = NFSERR_OLDSTATEID;
3403 if (!error && vnode_vtype(vp) != VREG) {
3404 if (vnode_vtype(vp) == VDIR)
3405 error = NFSERR_ISDIR;
3407 error = NFSERR_INVAL;
3412 * If a client tries to confirm an Open with a bad
3413 * seqid# and there are no byte range locks or other Opens
3414 * on the openowner, just throw it away, so the next use of the
3415 * openowner will start a fresh seq#.
3417 if (error == NFSERR_BADSEQID &&
3418 (new_stp->ls_flags & NFSLCK_CONFIRM) &&
3419 nfsrv_nootherstate(stp))
3420 nfsrv_freeopenowner(stp->ls_openowner, 0, p);
3426 * Set the return stateid.
3428 stateidp->seqid = stp->ls_stateid.seqid + 1;
3429 if ((nd->nd_flag & ND_NFSV41) != 0 && stateidp->seqid == 0)
3430 stateidp->seqid = 1;
3431 stateidp->other[0] = stp->ls_stateid.other[0];
3432 stateidp->other[1] = stp->ls_stateid.other[1];
3433 stateidp->other[2] = stp->ls_stateid.other[2];
3435 * Now, handle the three cases.
3437 if (new_stp->ls_flags & NFSLCK_CONFIRM) {
3439 * If the open doesn't need confirmation, it seems to me that
3440 * there is a client error, but I'll just log it and keep going?
3442 if (!(stp->ls_openowner->ls_flags & NFSLCK_NEEDSCONFIRM))
3443 printf("Nfsv4d: stray open confirm\n");
3444 stp->ls_openowner->ls_flags = 0;
3445 stp->ls_stateid.seqid++;
3446 if ((nd->nd_flag & ND_NFSV41) != 0 &&
3447 stp->ls_stateid.seqid == 0)
3448 stp->ls_stateid.seqid = 1;
3449 if (!(clp->lc_flags & LCL_STAMPEDSTABLE)) {
3450 clp->lc_flags |= LCL_STAMPEDSTABLE;
3451 len = clp->lc_idlen;
3452 NFSBCOPY(clp->lc_id, clidp, len);
3456 } else if (new_stp->ls_flags & NFSLCK_CLOSE) {
3458 if (retwriteaccessp != NULL) {
3459 if ((stp->ls_flags & NFSLCK_WRITEACCESS) != 0)
3460 *retwriteaccessp = 1;
3462 *retwriteaccessp = 0;
3464 if (nfsrv_dolocallocks != 0 && !LIST_EMPTY(&stp->ls_open)) {
3465 /* Get the lf lock */
3468 ASSERT_VOP_ELOCKED(vp, "nfsrv_openupdate");
3469 NFSVOPUNLOCK(vp, 0);
3470 if (nfsrv_freeopen(stp, vp, 1, p) == 0) {
3472 nfsrv_unlocklf(lfp);
3475 NFSVOPLOCK(vp, LK_EXCLUSIVE | LK_RETRY);
3477 (void) nfsrv_freeopen(stp, NULL, 0, p);
3482 * Update the share bits, making sure that the new set are a
3483 * subset of the old ones.
3485 bits = (new_stp->ls_flags & NFSLCK_SHAREBITS);
3486 if (~(stp->ls_flags) & bits) {
3488 error = NFSERR_INVAL;
3491 stp->ls_flags = (bits | NFSLCK_OPEN);
3492 stp->ls_stateid.seqid++;
3493 if ((nd->nd_flag & ND_NFSV41) != 0 &&
3494 stp->ls_stateid.seqid == 0)
3495 stp->ls_stateid.seqid = 1;
3500 * If the client just confirmed its first open, write a timestamp
3501 * to the stable storage file.
3503 if (gotstate != 0) {
3504 nfsrv_writestable(clidp, len, NFSNST_NEWSTATE, p);
3505 nfsrv_backupstable();
3509 free(clidp, M_TEMP);
3510 NFSEXITCODE2(error, nd);
3515 * Delegation update. Does the purge and return.
3518 nfsrv_delegupdate(struct nfsrv_descript *nd, nfsquad_t clientid,
3519 nfsv4stateid_t *stateidp, vnode_t vp, int op, struct ucred *cred,
3520 NFSPROC_T *p, int *retwriteaccessp)
3522 struct nfsstate *stp;
3523 struct nfsclient *clp;
3528 * Do a sanity check against the file handle for DelegReturn.
3531 error = nfsvno_getfh(vp, &fh, p);
3536 * Check for restart conditions (client and server).
3538 if (op == NFSV4OP_DELEGRETURN)
3539 error = nfsrv_checkrestart(clientid, NFSLCK_DELEGRETURN,
3542 error = nfsrv_checkrestart(clientid, NFSLCK_DELEGPURGE,
3547 * Get the open structure via clientid and stateid.
3550 error = nfsrv_getclient(clientid, CLOPS_RENEW, &clp, NULL,
3551 (nfsquad_t)((u_quad_t)0), 0, nd, p);
3553 if (error == NFSERR_CBPATHDOWN)
3555 if (error == NFSERR_STALECLIENTID && op == NFSV4OP_DELEGRETURN)
3556 error = NFSERR_STALESTATEID;
3558 if (!error && op == NFSV4OP_DELEGRETURN) {
3559 error = nfsrv_getstate(clp, stateidp, NFSLCK_DELEGRETURN, &stp);
3560 if (!error && stp->ls_stateid.seqid != stateidp->seqid &&
3561 ((nd->nd_flag & ND_NFSV41) == 0 || stateidp->seqid != 0))
3562 error = NFSERR_OLDSTATEID;
3565 * NFSERR_EXPIRED means that the state has gone away,
3566 * so Delegations have been purged. Just return ok.
3568 if (error == NFSERR_EXPIRED && op == NFSV4OP_DELEGPURGE) {
3578 if (op == NFSV4OP_DELEGRETURN) {
3579 if (NFSBCMP((caddr_t)&fh, (caddr_t)&stp->ls_lfp->lf_fh,
3580 sizeof (fhandle_t))) {
3582 error = NFSERR_BADSTATEID;
3585 if (retwriteaccessp != NULL) {
3586 if ((stp->ls_flags & NFSLCK_DELEGWRITE) != 0)
3587 *retwriteaccessp = 1;
3589 *retwriteaccessp = 0;
3591 nfsrv_freedeleg(stp);
3593 nfsrv_freedeleglist(&clp->lc_olddeleg);
3604 * Release lock owner.
3607 nfsrv_releaselckown(struct nfsstate *new_stp, nfsquad_t clientid,
3610 struct nfsstate *stp, *nstp, *openstp, *ownstp;
3611 struct nfsclient *clp;
3615 * Check for restart conditions (client and server).
3617 error = nfsrv_checkrestart(clientid, new_stp->ls_flags,
3618 &new_stp->ls_stateid, 0);
3624 * Get the lock owner by name.
3626 error = nfsrv_getclient(clientid, CLOPS_RENEW, &clp, NULL,
3627 (nfsquad_t)((u_quad_t)0), 0, NULL, p);
3632 LIST_FOREACH(ownstp, &clp->lc_open, ls_list) {
3633 LIST_FOREACH(openstp, &ownstp->ls_open, ls_list) {
3634 stp = LIST_FIRST(&openstp->ls_open);
3635 while (stp != LIST_END(&openstp->ls_open)) {
3636 nstp = LIST_NEXT(stp, ls_list);
3638 * If the owner matches, check for locks and
3639 * then free or return an error.
3641 if (stp->ls_ownerlen == new_stp->ls_ownerlen &&
3642 !NFSBCMP(stp->ls_owner, new_stp->ls_owner,
3644 if (LIST_EMPTY(&stp->ls_lock)) {
3645 nfsrv_freelockowner(stp, NULL, 0, p);
3648 error = NFSERR_LOCKSHELD;
3664 * Get the file handle for a lock structure.
3667 nfsrv_getlockfh(vnode_t vp, u_short flags, struct nfslockfile *new_lfp,
3668 fhandle_t *nfhp, NFSPROC_T *p)
3670 fhandle_t *fhp = NULL;
3674 * For lock, use the new nfslock structure, otherwise just
3675 * a fhandle_t on the stack.
3677 if (flags & NFSLCK_OPEN) {
3678 KASSERT(new_lfp != NULL, ("nfsrv_getlockfh: new_lfp NULL"));
3679 fhp = &new_lfp->lf_fh;
3683 panic("nfsrv_getlockfh");
3685 error = nfsvno_getfh(vp, fhp, p);
3691 * Get an nfs lock structure. Allocate one, as required, and return a
3693 * Returns an NFSERR_xxx upon failure or -1 to indicate no current lock.
3696 nfsrv_getlockfile(u_short flags, struct nfslockfile **new_lfpp,
3697 struct nfslockfile **lfpp, fhandle_t *nfhp, int lockit)
3699 struct nfslockfile *lfp;
3700 fhandle_t *fhp = NULL, *tfhp;
3701 struct nfslockhashhead *hp;
3702 struct nfslockfile *new_lfp = NULL;
3705 * For lock, use the new nfslock structure, otherwise just
3706 * a fhandle_t on the stack.
3708 if (flags & NFSLCK_OPEN) {
3709 new_lfp = *new_lfpp;
3710 fhp = &new_lfp->lf_fh;
3714 panic("nfsrv_getlockfile");
3717 hp = NFSLOCKHASH(fhp);
3718 LIST_FOREACH(lfp, hp, lf_hash) {
3720 if (NFSVNO_CMPFH(fhp, tfhp)) {
3727 if (!(flags & NFSLCK_OPEN))
3731 * No match, so chain the new one into the list.
3733 LIST_INIT(&new_lfp->lf_open);
3734 LIST_INIT(&new_lfp->lf_lock);
3735 LIST_INIT(&new_lfp->lf_deleg);
3736 LIST_INIT(&new_lfp->lf_locallock);
3737 LIST_INIT(&new_lfp->lf_rollback);
3738 new_lfp->lf_locallock_lck.nfslock_usecnt = 0;
3739 new_lfp->lf_locallock_lck.nfslock_lock = 0;
3740 new_lfp->lf_usecount = 0;
3741 LIST_INSERT_HEAD(hp, new_lfp, lf_hash);
3748 * This function adds a nfslock lock structure to the list for the associated
3749 * nfsstate and nfslockfile structures. It will be inserted after the
3750 * entry pointed at by insert_lop.
3753 nfsrv_insertlock(struct nfslock *new_lop, struct nfslock *insert_lop,
3754 struct nfsstate *stp, struct nfslockfile *lfp)
3756 struct nfslock *lop, *nlop;
3758 new_lop->lo_stp = stp;
3759 new_lop->lo_lfp = lfp;
3762 /* Insert in increasing lo_first order */
3763 lop = LIST_FIRST(&lfp->lf_lock);
3764 if (lop == LIST_END(&lfp->lf_lock) ||
3765 new_lop->lo_first <= lop->lo_first) {
3766 LIST_INSERT_HEAD(&lfp->lf_lock, new_lop, lo_lckfile);
3768 nlop = LIST_NEXT(lop, lo_lckfile);
3769 while (nlop != LIST_END(&lfp->lf_lock) &&
3770 nlop->lo_first < new_lop->lo_first) {
3772 nlop = LIST_NEXT(lop, lo_lckfile);
3774 LIST_INSERT_AFTER(lop, new_lop, lo_lckfile);
3777 new_lop->lo_lckfile.le_prev = NULL; /* list not used */
3781 * Insert after insert_lop, which is overloaded as stp or lfp for
3784 if (stp == NULL && (struct nfslockfile *)insert_lop == lfp)
3785 LIST_INSERT_HEAD(&lfp->lf_locallock, new_lop, lo_lckowner);
3786 else if ((struct nfsstate *)insert_lop == stp)
3787 LIST_INSERT_HEAD(&stp->ls_lock, new_lop, lo_lckowner);
3789 LIST_INSERT_AFTER(insert_lop, new_lop, lo_lckowner);
3791 nfsstatsv1.srvlocks++;
3792 nfsrv_openpluslock++;
3797 * This function updates the locking for a lock owner and given file. It
3798 * maintains a list of lock ranges ordered on increasing file offset that
3799 * are NFSLCK_READ or NFSLCK_WRITE and non-overlapping (aka POSIX style).
3800 * It always adds new_lop to the list and sometimes uses the one pointed
3804 nfsrv_updatelock(struct nfsstate *stp, struct nfslock **new_lopp,
3805 struct nfslock **other_lopp, struct nfslockfile *lfp)
3807 struct nfslock *new_lop = *new_lopp;
3808 struct nfslock *lop, *tlop, *ilop;
3809 struct nfslock *other_lop = *other_lopp;
3810 int unlock = 0, myfile = 0;
3814 * Work down the list until the lock is merged.
3816 if (new_lop->lo_flags & NFSLCK_UNLOCK)
3819 ilop = (struct nfslock *)stp;
3820 lop = LIST_FIRST(&stp->ls_lock);
3822 ilop = (struct nfslock *)lfp;
3823 lop = LIST_FIRST(&lfp->lf_locallock);
3825 while (lop != NULL) {
3827 * Only check locks for this file that aren't before the start of
3830 if (lop->lo_lfp == lfp) {
3832 if (lop->lo_end >= new_lop->lo_first) {
3833 if (new_lop->lo_end < lop->lo_first) {
3835 * If the new lock ends before the start of the
3836 * current lock's range, no merge, just insert
3841 if (new_lop->lo_flags == lop->lo_flags ||
3842 (new_lop->lo_first <= lop->lo_first &&
3843 new_lop->lo_end >= lop->lo_end)) {
3845 * This lock can be absorbed by the new lock/unlock.
3846 * This happens when it covers the entire range
3847 * of the old lock or is contiguous
3848 * with the old lock and is of the same type or an
3851 if (lop->lo_first < new_lop->lo_first)
3852 new_lop->lo_first = lop->lo_first;
3853 if (lop->lo_end > new_lop->lo_end)
3854 new_lop->lo_end = lop->lo_end;
3856 lop = LIST_NEXT(lop, lo_lckowner);
3857 nfsrv_freenfslock(tlop);
3862 * All these cases are for contiguous locks that are not the
3863 * same type, so they can't be merged.
3865 if (new_lop->lo_first <= lop->lo_first) {
3867 * This case is where the new lock overlaps with the
3868 * first part of the old lock. Move the start of the
3869 * old lock to just past the end of the new lock. The
3870 * new lock will be inserted in front of the old, since
3871 * ilop hasn't been updated. (We are done now.)
3873 lop->lo_first = new_lop->lo_end;
3876 if (new_lop->lo_end >= lop->lo_end) {
3878 * This case is where the new lock overlaps with the
3879 * end of the old lock's range. Move the old lock's
3880 * end to just before the new lock's first and insert
3881 * the new lock after the old lock.
3882 * Might not be done yet, since the new lock could
3883 * overlap further locks with higher ranges.
3885 lop->lo_end = new_lop->lo_first;
3887 lop = LIST_NEXT(lop, lo_lckowner);
3891 * The final case is where the new lock's range is in the
3892 * middle of the current lock's and splits the current lock
3893 * up. Use *other_lopp to handle the second part of the
3894 * split old lock range. (We are done now.)
3895 * For unlock, we use new_lop as other_lop and tmp, since
3896 * other_lop and new_lop are the same for this case.
3897 * We noted the unlock case above, so we don't need
3898 * new_lop->lo_flags any longer.
3900 tmp = new_lop->lo_first;
3901 if (other_lop == NULL) {
3903 panic("nfsd srv update unlock");
3904 other_lop = new_lop;
3907 other_lop->lo_first = new_lop->lo_end;
3908 other_lop->lo_end = lop->lo_end;
3909 other_lop->lo_flags = lop->lo_flags;
3910 other_lop->lo_stp = stp;
3911 other_lop->lo_lfp = lfp;
3913 nfsrv_insertlock(other_lop, lop, stp, lfp);
3920 lop = LIST_NEXT(lop, lo_lckowner);
3921 if (myfile && (lop == NULL || lop->lo_lfp != lfp))
3926 * Insert the new lock in the list at the appropriate place.
3929 nfsrv_insertlock(new_lop, ilop, stp, lfp);
3935 * This function handles sequencing of locks, etc.
3936 * It returns an error that indicates what the caller should do.
3939 nfsrv_checkseqid(struct nfsrv_descript *nd, u_int32_t seqid,
3940 struct nfsstate *stp, struct nfsrvcache *op)
3944 if ((nd->nd_flag & ND_NFSV41) != 0)
3945 /* NFSv4.1 ignores the open_seqid and lock_seqid. */
3947 if (op != nd->nd_rp)
3948 panic("nfsrvstate checkseqid");
3949 if (!(op->rc_flag & RC_INPROG))
3950 panic("nfsrvstate not inprog");
3951 if (stp->ls_op && stp->ls_op->rc_refcnt <= 0) {
3952 printf("refcnt=%d\n", stp->ls_op->rc_refcnt);
3953 panic("nfsrvstate op refcnt");
3955 if ((stp->ls_seq + 1) == seqid) {
3957 nfsrvd_derefcache(stp->ls_op);
3959 nfsrvd_refcache(op);
3960 stp->ls_seq = seqid;
3962 } else if (stp->ls_seq == seqid && stp->ls_op &&
3963 op->rc_xid == stp->ls_op->rc_xid &&
3964 op->rc_refcnt == 0 &&
3965 op->rc_reqlen == stp->ls_op->rc_reqlen &&
3966 op->rc_cksum == stp->ls_op->rc_cksum) {
3967 if (stp->ls_op->rc_flag & RC_INPROG) {
3968 error = NFSERR_DONTREPLY;
3971 nd->nd_rp = stp->ls_op;
3972 nd->nd_rp->rc_flag |= RC_INPROG;
3973 nfsrvd_delcache(op);
3974 error = NFSERR_REPLYFROMCACHE;
3977 error = NFSERR_BADSEQID;
3980 NFSEXITCODE2(error, nd);
3985 * Get the client ip address for callbacks. If the strings can't be parsed,
3986 * just set lc_program to 0 to indicate no callbacks are possible.
3987 * (For cases where the address can't be parsed or is 0.0.0.0.0.0, set
3988 * the address to the client's transport address. This won't be used
3989 * for callbacks, but can be printed out by nfsstats for info.)
3990 * Return error if the xdr can't be parsed, 0 otherwise.
3993 nfsrv_getclientipaddr(struct nfsrv_descript *nd, struct nfsclient *clp)
3998 struct sockaddr_in *rad, *sad;
3999 u_char protocol[5], addr[24];
4000 int error = 0, cantparse = 0;
4010 rad = NFSSOCKADDR(clp->lc_req.nr_nam, struct sockaddr_in *);
4011 rad->sin_family = AF_INET;
4012 rad->sin_len = sizeof (struct sockaddr_in);
4013 rad->sin_addr.s_addr = 0;
4015 clp->lc_req.nr_client = NULL;
4016 clp->lc_req.nr_lock = 0;
4017 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
4018 i = fxdr_unsigned(int, *tl);
4019 if (i >= 3 && i <= 4) {
4020 error = nfsrv_mtostr(nd, protocol, i);
4023 if (!strcmp(protocol, "tcp")) {
4024 clp->lc_flags |= LCL_TCPCALLBACK;
4025 clp->lc_req.nr_sotype = SOCK_STREAM;
4026 clp->lc_req.nr_soproto = IPPROTO_TCP;
4027 } else if (!strcmp(protocol, "udp")) {
4028 clp->lc_req.nr_sotype = SOCK_DGRAM;
4029 clp->lc_req.nr_soproto = IPPROTO_UDP;
4036 error = nfsm_advance(nd, NFSM_RNDUP(i), -1);
4041 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
4042 i = fxdr_unsigned(int, *tl);
4044 error = NFSERR_BADXDR;
4046 } else if (i == 0) {
4048 } else if (!cantparse && i <= 23 && i >= 11) {
4049 error = nfsrv_mtostr(nd, addr, i);
4054 * Parse out the address fields. We expect 6 decimal numbers
4055 * separated by '.'s.
4059 while (*cp && i < 6) {
4061 while (*cp2 && *cp2 != '.')
4069 j = nfsrv_getipnumber(cp);
4074 port.cval[5 - i] = j;
4083 if (ip.ival != 0x0) {
4084 rad->sin_addr.s_addr = htonl(ip.ival);
4085 rad->sin_port = htons(port.sval);
4093 error = nfsm_advance(nd, NFSM_RNDUP(i), -1);
4099 sad = NFSSOCKADDR(nd->nd_nam, struct sockaddr_in *);
4100 if (sad->sin_family == AF_INET) {
4101 rad->sin_addr.s_addr = sad->sin_addr.s_addr;
4102 rad->sin_port = 0x0;
4104 clp->lc_program = 0;
4107 NFSEXITCODE2(error, nd);
4112 * Turn a string of up to three decimal digits into a number. Return -1 upon
4116 nfsrv_getipnumber(u_char *cp)
4121 if (j > 2 || *cp < '0' || *cp > '9')
4134 * This function checks for restart conditions.
4137 nfsrv_checkrestart(nfsquad_t clientid, u_int32_t flags,
4138 nfsv4stateid_t *stateidp, int specialid)
4143 * First check for a server restart. Open, LockT, ReleaseLockOwner
4144 * and DelegPurge have a clientid, the rest a stateid.
4147 (NFSLCK_OPEN | NFSLCK_TEST | NFSLCK_RELEASE | NFSLCK_DELEGPURGE)) {
4148 if (clientid.lval[0] != nfsrvboottime) {
4149 ret = NFSERR_STALECLIENTID;
4152 } else if (stateidp->other[0] != nfsrvboottime &&
4154 ret = NFSERR_STALESTATEID;
4159 * Read, Write, Setattr and LockT can return NFSERR_GRACE and do
4160 * not use a lock/open owner seqid#, so the check can be done now.
4161 * (The others will be checked, as required, later.)
4163 if (!(flags & (NFSLCK_CHECK | NFSLCK_TEST)))
4167 ret = nfsrv_checkgrace(NULL, NULL, flags);
4179 nfsrv_checkgrace(struct nfsrv_descript *nd, struct nfsclient *clp,
4182 int error = 0, notreclaimed;
4183 struct nfsrv_stable *sp;
4185 if ((nfsrv_stablefirst.nsf_flags & (NFSNSF_UPDATEDONE |
4186 NFSNSF_GRACEOVER)) == 0) {
4188 * First, check to see if all of the clients have done a
4189 * ReclaimComplete. If so, grace can end now.
4192 LIST_FOREACH(sp, &nfsrv_stablefirst.nsf_head, nst_list) {
4193 if ((sp->nst_flag & NFSNST_RECLAIMED) == 0) {
4198 if (notreclaimed == 0)
4199 nfsrv_stablefirst.nsf_flags |= (NFSNSF_GRACEOVER |
4203 if ((nfsrv_stablefirst.nsf_flags & NFSNSF_GRACEOVER) != 0) {
4204 if (flags & NFSLCK_RECLAIM) {
4205 error = NFSERR_NOGRACE;
4209 if (!(flags & NFSLCK_RECLAIM)) {
4210 error = NFSERR_GRACE;
4213 if (nd != NULL && clp != NULL &&
4214 (nd->nd_flag & ND_NFSV41) != 0 &&
4215 (clp->lc_flags & LCL_RECLAIMCOMPLETE) != 0) {
4216 error = NFSERR_NOGRACE;
4221 * If grace is almost over and we are still getting Reclaims,
4222 * extend grace a bit.
4224 if ((NFSD_MONOSEC + NFSRV_LEASEDELTA) >
4225 nfsrv_stablefirst.nsf_eograce)
4226 nfsrv_stablefirst.nsf_eograce = NFSD_MONOSEC +
4236 * Do a server callback.
4237 * The "trunc" argument is slightly overloaded and refers to different
4238 * boolean arguments for CBRECALL and CBLAYOUTRECALL.
4241 nfsrv_docallback(struct nfsclient *clp, int procnum, nfsv4stateid_t *stateidp,
4242 int trunc, fhandle_t *fhp, struct nfsvattr *nap, nfsattrbit_t *attrbitp,
4243 int laytype, NFSPROC_T *p)
4247 struct nfsrv_descript *nd;
4251 struct nfsdsession *sep = NULL;
4254 nd = malloc(sizeof(*nd), M_TEMP, M_WAITOK | M_ZERO);
4255 cred = newnfs_getcred();
4256 NFSLOCKSTATE(); /* mostly for lc_cbref++ */
4257 if (clp->lc_flags & LCL_NEEDSCONFIRM) {
4264 * Fill the callback program# and version into the request
4265 * structure for newnfs_connect() to use.
4267 clp->lc_req.nr_prog = clp->lc_program;
4269 if ((clp->lc_flags & LCL_NFSV41) != 0)
4270 clp->lc_req.nr_vers = NFSV41_CBVERS;
4273 clp->lc_req.nr_vers = NFSV4_CBVERS;
4276 * First, fill in some of the fields of nd and cr.
4278 nd->nd_flag = ND_NFSV4;
4279 if (clp->lc_flags & LCL_GSS)
4280 nd->nd_flag |= ND_KERBV;
4281 if ((clp->lc_flags & LCL_NFSV41) != 0)
4282 nd->nd_flag |= ND_NFSV41;
4284 cred->cr_uid = clp->lc_uid;
4285 cred->cr_gid = clp->lc_gid;
4286 callback = clp->lc_callback;
4288 cred->cr_ngroups = 1;
4291 * Get the first mbuf for the request.
4293 MGET(m, M_WAITOK, MT_DATA);
4295 nd->nd_mreq = nd->nd_mb = m;
4296 nd->nd_bpos = NFSMTOD(m, caddr_t);
4299 * and build the callback request.
4301 if (procnum == NFSV4OP_CBGETATTR) {
4302 nd->nd_procnum = NFSV4PROC_CBCOMPOUND;
4303 error = nfsrv_cbcallargs(nd, clp, callback, NFSV4OP_CBGETATTR,
4304 "CB Getattr", &sep);
4306 mbuf_freem(nd->nd_mreq);
4309 (void)nfsm_fhtom(nd, (u_int8_t *)fhp, NFSX_MYFH, 0);
4310 (void)nfsrv_putattrbit(nd, attrbitp);
4311 } else if (procnum == NFSV4OP_CBRECALL) {
4312 nd->nd_procnum = NFSV4PROC_CBCOMPOUND;
4313 error = nfsrv_cbcallargs(nd, clp, callback, NFSV4OP_CBRECALL,
4316 mbuf_freem(nd->nd_mreq);
4319 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED + NFSX_STATEID);
4320 *tl++ = txdr_unsigned(stateidp->seqid);
4321 NFSBCOPY((caddr_t)stateidp->other, (caddr_t)tl,
4323 tl += (NFSX_STATEIDOTHER / NFSX_UNSIGNED);
4328 (void)nfsm_fhtom(nd, (u_int8_t *)fhp, NFSX_MYFH, 0);
4329 } else if (procnum == NFSV4OP_CBLAYOUTRECALL) {
4330 NFSD_DEBUG(4, "docallback layout recall\n");
4331 nd->nd_procnum = NFSV4PROC_CBCOMPOUND;
4332 error = nfsrv_cbcallargs(nd, clp, callback,
4333 NFSV4OP_CBLAYOUTRECALL, "CB Reclayout", &sep);
4334 NFSD_DEBUG(4, "aft cbcallargs=%d\n", error);
4336 mbuf_freem(nd->nd_mreq);
4339 NFSM_BUILD(tl, u_int32_t *, 4 * NFSX_UNSIGNED);
4340 *tl++ = txdr_unsigned(laytype);
4341 *tl++ = txdr_unsigned(NFSLAYOUTIOMODE_ANY);
4343 *tl++ = newnfs_true;
4345 *tl++ = newnfs_false;
4346 *tl = txdr_unsigned(NFSV4LAYOUTRET_FILE);
4347 nfsm_fhtom(nd, (uint8_t *)fhp, NFSX_MYFH, 0);
4348 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_HYPER + NFSX_STATEID);
4350 txdr_hyper(tval, tl); tl += 2;
4352 txdr_hyper(tval, tl); tl += 2;
4353 *tl++ = txdr_unsigned(stateidp->seqid);
4354 NFSBCOPY(stateidp->other, tl, NFSX_STATEIDOTHER);
4355 tl += (NFSX_STATEIDOTHER / NFSX_UNSIGNED);
4356 NFSD_DEBUG(4, "aft args\n");
4357 } else if (procnum == NFSV4PROC_CBNULL) {
4358 nd->nd_procnum = NFSV4PROC_CBNULL;
4359 if ((clp->lc_flags & LCL_NFSV41) != 0) {
4360 error = nfsv4_getcbsession(clp, &sep);
4362 mbuf_freem(nd->nd_mreq);
4367 error = NFSERR_SERVERFAULT;
4368 mbuf_freem(nd->nd_mreq);
4373 * Call newnfs_connect(), as required, and then newnfs_request().
4375 (void) newnfs_sndlock(&clp->lc_req.nr_lock);
4376 if (clp->lc_req.nr_client == NULL) {
4377 if ((clp->lc_flags & LCL_NFSV41) != 0) {
4378 error = ECONNREFUSED;
4379 nfsrv_freesession(sep, NULL);
4380 } else if (nd->nd_procnum == NFSV4PROC_CBNULL)
4381 error = newnfs_connect(NULL, &clp->lc_req, cred,
4384 error = newnfs_connect(NULL, &clp->lc_req, cred,
4387 newnfs_sndunlock(&clp->lc_req.nr_lock);
4388 NFSD_DEBUG(4, "aft sndunlock=%d\n", error);
4390 if ((nd->nd_flag & ND_NFSV41) != 0) {
4391 KASSERT(sep != NULL, ("sep NULL"));
4392 if (sep->sess_cbsess.nfsess_xprt != NULL)
4393 error = newnfs_request(nd, NULL, clp,
4394 &clp->lc_req, NULL, NULL, cred,
4395 clp->lc_program, clp->lc_req.nr_vers, NULL,
4396 1, NULL, &sep->sess_cbsess);
4399 * This should probably never occur, but if a
4400 * client somehow does an RPC without a
4401 * SequenceID Op that causes a callback just
4402 * after the nfsd threads have been terminated
4403 * and restared we could conceivably get here
4404 * without a backchannel xprt.
4406 printf("nfsrv_docallback: no xprt\n");
4407 error = ECONNREFUSED;
4409 NFSD_DEBUG(4, "aft newnfs_request=%d\n", error);
4410 nfsrv_freesession(sep, NULL);
4412 error = newnfs_request(nd, NULL, clp, &clp->lc_req,
4413 NULL, NULL, cred, clp->lc_program,
4414 clp->lc_req.nr_vers, NULL, 1, NULL, NULL);
4420 * If error is set here, the Callback path isn't working
4421 * properly, so twiddle the appropriate LCL_ flags.
4422 * (nd_repstat != 0 indicates the Callback path is working,
4423 * but the callback failed on the client.)
4427 * Mark the callback pathway down, which disabled issuing
4428 * of delegations and gets Renew to return NFSERR_CBPATHDOWN.
4431 clp->lc_flags |= LCL_CBDOWN;
4435 * Callback worked. If the callback path was down, disable
4436 * callbacks, so no more delegations will be issued. (This
4437 * is done on the assumption that the callback pathway is
4441 if (clp->lc_flags & LCL_CBDOWN)
4442 clp->lc_flags &= ~(LCL_CBDOWN | LCL_CALLBACKSON);
4444 if (nd->nd_repstat) {
4445 error = nd->nd_repstat;
4446 NFSD_DEBUG(1, "nfsrv_docallback op=%d err=%d\n",
4448 } else if (error == 0 && procnum == NFSV4OP_CBGETATTR)
4449 error = nfsv4_loadattr(nd, NULL, nap, NULL, NULL, 0,
4450 NULL, NULL, NULL, NULL, NULL, 0, NULL, NULL, NULL,
4452 mbuf_freem(nd->nd_mrep);
4456 if ((clp->lc_flags & LCL_WAKEUPWANTED) && clp->lc_cbref == 0) {
4457 clp->lc_flags &= ~LCL_WAKEUPWANTED;
4468 * Set up the compound RPC for the callback.
4471 nfsrv_cbcallargs(struct nfsrv_descript *nd, struct nfsclient *clp,
4472 uint32_t callback, int op, const char *optag, struct nfsdsession **sepp)
4477 len = strlen(optag);
4478 (void)nfsm_strtom(nd, optag, len);
4479 NFSM_BUILD(tl, uint32_t *, 4 * NFSX_UNSIGNED);
4480 if ((nd->nd_flag & ND_NFSV41) != 0) {
4481 *tl++ = txdr_unsigned(NFSV41_MINORVERSION);
4482 *tl++ = txdr_unsigned(callback);
4483 *tl++ = txdr_unsigned(2);
4484 *tl = txdr_unsigned(NFSV4OP_CBSEQUENCE);
4485 error = nfsv4_setcbsequence(nd, clp, 1, sepp);
4488 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
4489 *tl = txdr_unsigned(op);
4491 *tl++ = txdr_unsigned(NFSV4_MINORVERSION);
4492 *tl++ = txdr_unsigned(callback);
4493 *tl++ = txdr_unsigned(1);
4494 *tl = txdr_unsigned(op);
4500 * Return the next index# for a clientid. Mostly just increment and return
4501 * the next one, but... if the 32bit unsigned does actually wrap around,
4502 * it should be rebooted.
4503 * At an average rate of one new client per second, it will wrap around in
4504 * approximately 136 years. (I think the server will have been shut
4505 * down or rebooted before then.)
4508 nfsrv_nextclientindex(void)
4510 static u_int32_t client_index = 0;
4513 if (client_index != 0)
4514 return (client_index);
4516 printf("%s: out of clientids\n", __func__);
4517 return (client_index);
4521 * Return the next index# for a stateid. Mostly just increment and return
4522 * the next one, but... if the 32bit unsigned does actually wrap around
4523 * (will a BSD server stay up that long?), find
4524 * new start and end values.
4527 nfsrv_nextstateindex(struct nfsclient *clp)
4529 struct nfsstate *stp;
4531 u_int32_t canuse, min_index, max_index;
4533 if (!(clp->lc_flags & LCL_INDEXNOTOK)) {
4534 clp->lc_stateindex++;
4535 if (clp->lc_stateindex != clp->lc_statemaxindex)
4536 return (clp->lc_stateindex);
4540 * Yuck, we've hit the end.
4541 * Look for a new min and max.
4544 max_index = 0xffffffff;
4545 for (i = 0; i < nfsrv_statehashsize; i++) {
4546 LIST_FOREACH(stp, &clp->lc_stateid[i], ls_hash) {
4547 if (stp->ls_stateid.other[2] > 0x80000000) {
4548 if (stp->ls_stateid.other[2] < max_index)
4549 max_index = stp->ls_stateid.other[2];
4551 if (stp->ls_stateid.other[2] > min_index)
4552 min_index = stp->ls_stateid.other[2];
4558 * Yikes, highly unlikely, but I'll handle it anyhow.
4560 if (min_index == 0x80000000 && max_index == 0x80000001) {
4563 * Loop around until we find an unused entry. Return that
4564 * and set LCL_INDEXNOTOK, so the search will continue next time.
4565 * (This is one of those rare cases where a goto is the
4566 * cleanest way to code the loop.)
4569 for (i = 0; i < nfsrv_statehashsize; i++) {
4570 LIST_FOREACH(stp, &clp->lc_stateid[i], ls_hash) {
4571 if (stp->ls_stateid.other[2] == canuse) {
4577 clp->lc_flags |= LCL_INDEXNOTOK;
4582 * Ok to start again from min + 1.
4584 clp->lc_stateindex = min_index + 1;
4585 clp->lc_statemaxindex = max_index;
4586 clp->lc_flags &= ~LCL_INDEXNOTOK;
4587 return (clp->lc_stateindex);
4591 * The following functions handle the stable storage file that deals with
4592 * the edge conditions described in RFC3530 Sec. 8.6.3.
4593 * The file is as follows:
4594 * - a single record at the beginning that has the lease time of the
4595 * previous server instance (before the last reboot) and the nfsrvboottime
4596 * values for the previous server boots.
4597 * These previous boot times are used to ensure that the current
4598 * nfsrvboottime does not, somehow, get set to a previous one.
4599 * (This is important so that Stale ClientIDs and StateIDs can
4601 * The number of previous nfsvrboottime values precedes the list.
4602 * - followed by some number of appended records with:
4603 * - client id string
4604 * - flag that indicates it is a record revoking state via lease
4605 * expiration or similar
4606 * OR has successfully acquired state.
4607 * These structures vary in length, with the client string at the end, up
4608 * to NFSV4_OPAQUELIMIT in size.
4610 * At the end of the grace period, the file is truncated, the first
4611 * record is rewritten with updated information and any acquired state
4612 * records for successful reclaims of state are written.
4614 * Subsequent records are appended when the first state is issued to
4615 * a client and when state is revoked for a client.
4617 * When reading the file in, state issued records that come later in
4618 * the file override older ones, since the append log is in cronological order.
4619 * If, for some reason, the file can't be read, the grace period is
4620 * immediately terminated and all reclaims get NFSERR_NOGRACE.
4624 * Read in the stable storage file. Called by nfssvc() before the nfsd
4625 * processes start servicing requests.
4628 nfsrv_setupstable(NFSPROC_T *p)
4630 struct nfsrv_stablefirst *sf = &nfsrv_stablefirst;
4631 struct nfsrv_stable *sp, *nsp;
4632 struct nfst_rec *tsp;
4633 int error, i, tryagain;
4635 ssize_t aresid, len;
4638 * If NFSNSF_UPDATEDONE is set, this is a restart of the nfsds without
4639 * a reboot, so state has not been lost.
4641 if (sf->nsf_flags & NFSNSF_UPDATEDONE)
4644 * Set Grace over just until the file reads successfully.
4646 nfsrvboottime = time_second;
4647 LIST_INIT(&sf->nsf_head);
4648 sf->nsf_flags = (NFSNSF_GRACEOVER | NFSNSF_NEEDLOCK);
4649 sf->nsf_eograce = NFSD_MONOSEC + NFSRV_LEASEDELTA;
4650 if (sf->nsf_fp == NULL)
4652 error = NFSD_RDWR(UIO_READ, NFSFPVNODE(sf->nsf_fp),
4653 (caddr_t)&sf->nsf_rec, sizeof (struct nfsf_rec), off, UIO_SYSSPACE,
4654 0, NFSFPCRED(sf->nsf_fp), &aresid, p);
4655 if (error || aresid || sf->nsf_numboots == 0 ||
4656 sf->nsf_numboots > NFSNSF_MAXNUMBOOTS)
4660 * Now, read in the boottimes.
4662 sf->nsf_bootvals = (time_t *)malloc((sf->nsf_numboots + 1) *
4663 sizeof (time_t), M_TEMP, M_WAITOK);
4664 off = sizeof (struct nfsf_rec);
4665 error = NFSD_RDWR(UIO_READ, NFSFPVNODE(sf->nsf_fp),
4666 (caddr_t)sf->nsf_bootvals, sf->nsf_numboots * sizeof (time_t), off,
4667 UIO_SYSSPACE, 0, NFSFPCRED(sf->nsf_fp), &aresid, p);
4668 if (error || aresid) {
4669 free(sf->nsf_bootvals, M_TEMP);
4670 sf->nsf_bootvals = NULL;
4675 * Make sure this nfsrvboottime is different from all recorded
4680 for (i = 0; i < sf->nsf_numboots; i++) {
4681 if (nfsrvboottime == sf->nsf_bootvals[i]) {
4689 sf->nsf_flags |= NFSNSF_OK;
4690 off += (sf->nsf_numboots * sizeof (time_t));
4693 * Read through the file, building a list of records for grace
4695 * Each record is between sizeof (struct nfst_rec) and
4696 * sizeof (struct nfst_rec) + NFSV4_OPAQUELIMIT - 1
4697 * and is actually sizeof (struct nfst_rec) + nst_len - 1.
4699 tsp = (struct nfst_rec *)malloc(sizeof (struct nfst_rec) +
4700 NFSV4_OPAQUELIMIT - 1, M_TEMP, M_WAITOK);
4702 error = NFSD_RDWR(UIO_READ, NFSFPVNODE(sf->nsf_fp),
4703 (caddr_t)tsp, sizeof (struct nfst_rec) + NFSV4_OPAQUELIMIT - 1,
4704 off, UIO_SYSSPACE, 0, NFSFPCRED(sf->nsf_fp), &aresid, p);
4705 len = (sizeof (struct nfst_rec) + NFSV4_OPAQUELIMIT - 1) - aresid;
4706 if (error || (len > 0 && (len < sizeof (struct nfst_rec) ||
4707 len < (sizeof (struct nfst_rec) + tsp->len - 1)))) {
4709 * Yuck, the file has been corrupted, so just return
4710 * after clearing out any restart state, so the grace period
4713 LIST_FOREACH_SAFE(sp, &sf->nsf_head, nst_list, nsp) {
4714 LIST_REMOVE(sp, nst_list);
4718 sf->nsf_flags &= ~NFSNSF_OK;
4719 free(sf->nsf_bootvals, M_TEMP);
4720 sf->nsf_bootvals = NULL;
4724 off += sizeof (struct nfst_rec) + tsp->len - 1;
4726 * Search the list for a matching client.
4728 LIST_FOREACH(sp, &sf->nsf_head, nst_list) {
4729 if (tsp->len == sp->nst_len &&
4730 !NFSBCMP(tsp->client, sp->nst_client, tsp->len))
4733 if (sp == LIST_END(&sf->nsf_head)) {
4734 sp = (struct nfsrv_stable *)malloc(tsp->len +
4735 sizeof (struct nfsrv_stable) - 1, M_TEMP,
4737 NFSBCOPY((caddr_t)tsp, (caddr_t)&sp->nst_rec,
4738 sizeof (struct nfst_rec) + tsp->len - 1);
4739 LIST_INSERT_HEAD(&sf->nsf_head, sp, nst_list);
4741 if (tsp->flag == NFSNST_REVOKE)
4742 sp->nst_flag |= NFSNST_REVOKE;
4745 * A subsequent timestamp indicates the client
4746 * did a setclientid/confirm and any previous
4747 * revoke is no longer relevant.
4749 sp->nst_flag &= ~NFSNST_REVOKE;
4754 sf->nsf_flags = NFSNSF_OK;
4755 sf->nsf_eograce = NFSD_MONOSEC + sf->nsf_lease +
4760 * Update the stable storage file, now that the grace period is over.
4763 nfsrv_updatestable(NFSPROC_T *p)
4765 struct nfsrv_stablefirst *sf = &nfsrv_stablefirst;
4766 struct nfsrv_stable *sp, *nsp;
4768 struct nfsvattr nva;
4770 #if defined(__FreeBSD_version) && (__FreeBSD_version >= 500000)
4775 if (sf->nsf_fp == NULL || (sf->nsf_flags & NFSNSF_UPDATEDONE))
4777 sf->nsf_flags |= NFSNSF_UPDATEDONE;
4779 * Ok, we need to rewrite the stable storage file.
4780 * - truncate to 0 length
4781 * - write the new first structure
4782 * - loop through the data structures, writing out any that
4783 * have timestamps older than the old boot
4785 if (sf->nsf_bootvals) {
4787 for (i = sf->nsf_numboots - 2; i >= 0; i--)
4788 sf->nsf_bootvals[i + 1] = sf->nsf_bootvals[i];
4790 sf->nsf_numboots = 1;
4791 sf->nsf_bootvals = (time_t *)malloc(sizeof (time_t),
4794 sf->nsf_bootvals[0] = nfsrvboottime;
4795 sf->nsf_lease = nfsrv_lease;
4796 NFSVNO_ATTRINIT(&nva);
4797 NFSVNO_SETATTRVAL(&nva, size, 0);
4798 vp = NFSFPVNODE(sf->nsf_fp);
4799 vn_start_write(vp, &mp, V_WAIT);
4800 if (NFSVOPLOCK(vp, LK_EXCLUSIVE) == 0) {
4801 error = nfsvno_setattr(vp, &nva, NFSFPCRED(sf->nsf_fp), p,
4803 NFSVOPUNLOCK(vp, 0);
4806 vn_finished_write(mp);
4808 error = NFSD_RDWR(UIO_WRITE, vp,
4809 (caddr_t)&sf->nsf_rec, sizeof (struct nfsf_rec), (off_t)0,
4810 UIO_SYSSPACE, IO_SYNC, NFSFPCRED(sf->nsf_fp), NULL, p);
4812 error = NFSD_RDWR(UIO_WRITE, vp,
4813 (caddr_t)sf->nsf_bootvals,
4814 sf->nsf_numboots * sizeof (time_t),
4815 (off_t)(sizeof (struct nfsf_rec)),
4816 UIO_SYSSPACE, IO_SYNC, NFSFPCRED(sf->nsf_fp), NULL, p);
4817 free(sf->nsf_bootvals, M_TEMP);
4818 sf->nsf_bootvals = NULL;
4820 sf->nsf_flags &= ~NFSNSF_OK;
4821 printf("EEK! Can't write NfsV4 stable storage file\n");
4824 sf->nsf_flags |= NFSNSF_OK;
4827 * Loop through the list and write out timestamp records for
4828 * any clients that successfully reclaimed state.
4830 LIST_FOREACH_SAFE(sp, &sf->nsf_head, nst_list, nsp) {
4831 if (sp->nst_flag & NFSNST_GOTSTATE) {
4832 nfsrv_writestable(sp->nst_client, sp->nst_len,
4833 NFSNST_NEWSTATE, p);
4834 sp->nst_clp->lc_flags |= LCL_STAMPEDSTABLE;
4836 LIST_REMOVE(sp, nst_list);
4839 nfsrv_backupstable();
4843 * Append a record to the stable storage file.
4846 nfsrv_writestable(u_char *client, int len, int flag, NFSPROC_T *p)
4848 struct nfsrv_stablefirst *sf = &nfsrv_stablefirst;
4849 struct nfst_rec *sp;
4852 if (!(sf->nsf_flags & NFSNSF_OK) || sf->nsf_fp == NULL)
4854 sp = (struct nfst_rec *)malloc(sizeof (struct nfst_rec) +
4855 len - 1, M_TEMP, M_WAITOK);
4857 NFSBCOPY(client, sp->client, len);
4859 error = NFSD_RDWR(UIO_WRITE, NFSFPVNODE(sf->nsf_fp),
4860 (caddr_t)sp, sizeof (struct nfst_rec) + len - 1, (off_t)0,
4861 UIO_SYSSPACE, (IO_SYNC | IO_APPEND), NFSFPCRED(sf->nsf_fp), NULL, p);
4864 sf->nsf_flags &= ~NFSNSF_OK;
4865 printf("EEK! Can't write NfsV4 stable storage file\n");
4870 * This function is called during the grace period to mark a client
4871 * that successfully reclaimed state.
4874 nfsrv_markstable(struct nfsclient *clp)
4876 struct nfsrv_stable *sp;
4879 * First find the client structure.
4881 LIST_FOREACH(sp, &nfsrv_stablefirst.nsf_head, nst_list) {
4882 if (sp->nst_len == clp->lc_idlen &&
4883 !NFSBCMP(sp->nst_client, clp->lc_id, sp->nst_len))
4886 if (sp == LIST_END(&nfsrv_stablefirst.nsf_head))
4890 * Now, just mark it and set the nfsclient back pointer.
4892 sp->nst_flag |= NFSNST_GOTSTATE;
4897 * This function is called when a NFSv4.1 client does a ReclaimComplete.
4898 * Very similar to nfsrv_markstable(), except for the flag being set.
4901 nfsrv_markreclaim(struct nfsclient *clp)
4903 struct nfsrv_stable *sp;
4906 * First find the client structure.
4908 LIST_FOREACH(sp, &nfsrv_stablefirst.nsf_head, nst_list) {
4909 if (sp->nst_len == clp->lc_idlen &&
4910 !NFSBCMP(sp->nst_client, clp->lc_id, sp->nst_len))
4913 if (sp == LIST_END(&nfsrv_stablefirst.nsf_head))
4917 * Now, just set the flag.
4919 sp->nst_flag |= NFSNST_RECLAIMED;
4923 * This function is called for a reclaim, to see if it gets grace.
4924 * It returns 0 if a reclaim is allowed, 1 otherwise.
4927 nfsrv_checkstable(struct nfsclient *clp)
4929 struct nfsrv_stable *sp;
4932 * First, find the entry for the client.
4934 LIST_FOREACH(sp, &nfsrv_stablefirst.nsf_head, nst_list) {
4935 if (sp->nst_len == clp->lc_idlen &&
4936 !NFSBCMP(sp->nst_client, clp->lc_id, sp->nst_len))
4941 * If not in the list, state was revoked or no state was issued
4942 * since the previous reboot, a reclaim is denied.
4944 if (sp == LIST_END(&nfsrv_stablefirst.nsf_head) ||
4945 (sp->nst_flag & NFSNST_REVOKE) ||
4946 !(nfsrv_stablefirst.nsf_flags & NFSNSF_OK))
4952 * Test for and try to clear out a conflicting client. This is called by
4953 * nfsrv_lockctrl() and nfsrv_openctrl() when conflicts with other clients
4955 * The trick here is that it can't revoke a conflicting client with an
4956 * expired lease unless it holds the v4root lock, so...
4957 * If no v4root lock, get the lock and return 1 to indicate "try again".
4958 * Return 0 to indicate the conflict can't be revoked and 1 to indicate
4959 * the revocation worked and the conflicting client is "bye, bye", so it
4960 * can be tried again.
4961 * Return 2 to indicate that the vnode is VI_DOOMED after NFSVOPLOCK().
4962 * Unlocks State before a non-zero value is returned.
4965 nfsrv_clientconflict(struct nfsclient *clp, int *haslockp, vnode_t vp,
4968 int gotlock, lktype = 0;
4971 * If lease hasn't expired, we can't fix it.
4973 if (clp->lc_expiry >= NFSD_MONOSEC ||
4974 !(nfsrv_stablefirst.nsf_flags & NFSNSF_UPDATEDONE))
4976 if (*haslockp == 0) {
4979 lktype = NFSVOPISLOCKED(vp);
4980 NFSVOPUNLOCK(vp, 0);
4982 NFSLOCKV4ROOTMUTEX();
4983 nfsv4_relref(&nfsv4rootfs_lock);
4985 gotlock = nfsv4_lock(&nfsv4rootfs_lock, 1, NULL,
4986 NFSV4ROOTLOCKMUTEXPTR, NULL);
4988 NFSUNLOCKV4ROOTMUTEX();
4991 NFSVOPLOCK(vp, lktype | LK_RETRY);
4992 if ((vp->v_iflag & VI_DOOMED) != 0)
5000 * Ok, we can expire the conflicting client.
5002 nfsrv_writestable(clp->lc_id, clp->lc_idlen, NFSNST_REVOKE, p);
5003 nfsrv_backupstable();
5004 nfsrv_cleanclient(clp, p);
5005 nfsrv_freedeleglist(&clp->lc_deleg);
5006 nfsrv_freedeleglist(&clp->lc_olddeleg);
5007 LIST_REMOVE(clp, lc_hash);
5008 nfsrv_zapclient(clp, p);
5013 * Resolve a delegation conflict.
5014 * Returns 0 to indicate the conflict was resolved without sleeping.
5015 * Return -1 to indicate that the caller should check for conflicts again.
5016 * Return > 0 for an error that should be returned, normally NFSERR_DELAY.
5018 * Also, manipulate the nfsv4root_lock, as required. It isn't changed
5019 * for a return of 0, since there was no sleep and it could be required
5020 * later. It is released for a return of NFSERR_DELAY, since the caller
5021 * will return that error. It is released when a sleep was done waiting
5022 * for the delegation to be returned or expire (so that other nfsds can
5023 * handle ops). Then, it must be acquired for the write to stable storage.
5024 * (This function is somewhat similar to nfsrv_clientconflict(), but
5025 * the semantics differ in a couple of subtle ways. The return of 0
5026 * indicates the conflict was resolved without sleeping here, not
5027 * that the conflict can't be resolved and the handling of nfsv4root_lock
5028 * differs, as noted above.)
5029 * Unlocks State before returning a non-zero value.
5032 nfsrv_delegconflict(struct nfsstate *stp, int *haslockp, NFSPROC_T *p,
5035 struct nfsclient *clp = stp->ls_clp;
5036 int gotlock, error, lktype = 0, retrycnt, zapped_clp;
5037 nfsv4stateid_t tstateid;
5041 * If the conflict is with an old delegation...
5043 if (stp->ls_flags & NFSLCK_OLDDELEG) {
5045 * You can delete it, if it has expired.
5047 if (clp->lc_delegtime < NFSD_MONOSEC) {
5048 nfsrv_freedeleg(stp);
5055 * During this delay, the old delegation could expire or it
5056 * could be recovered by the client via an Open with
5057 * CLAIM_DELEGATE_PREV.
5058 * Release the nfsv4root_lock, if held.
5062 NFSLOCKV4ROOTMUTEX();
5063 nfsv4_unlock(&nfsv4rootfs_lock, 1);
5064 NFSUNLOCKV4ROOTMUTEX();
5066 error = NFSERR_DELAY;
5071 * It's a current delegation, so:
5072 * - check to see if the delegation has expired
5073 * - if so, get the v4root lock and then expire it
5075 if (!(stp->ls_flags & NFSLCK_DELEGRECALL)) {
5077 * - do a recall callback, since not yet done
5078 * For now, never allow truncate to be set. To use
5079 * truncate safely, it must be guaranteed that the
5080 * Remove, Rename or Setattr with size of 0 will
5081 * succeed and that would require major changes to
5082 * the VFS/Vnode OPs.
5083 * Set the expiry time large enough so that it won't expire
5084 * until after the callback, then set it correctly, once
5085 * the callback is done. (The delegation will now time
5086 * out whether or not the Recall worked ok. The timeout
5087 * will be extended when ops are done on the delegation
5088 * stateid, up to the timelimit.)
5090 stp->ls_delegtime = NFSD_MONOSEC + (2 * nfsrv_lease) +
5092 stp->ls_delegtimelimit = NFSD_MONOSEC + (6 * nfsrv_lease) +
5094 stp->ls_flags |= NFSLCK_DELEGRECALL;
5097 * Loop NFSRV_CBRETRYCNT times while the CBRecall replies
5098 * NFSERR_BADSTATEID or NFSERR_BADHANDLE. This is done
5099 * in order to try and avoid a race that could happen
5100 * when a CBRecall request passed the Open reply with
5101 * the delegation in it when transitting the network.
5102 * Since nfsrv_docallback will sleep, don't use stp after
5105 NFSBCOPY((caddr_t)&stp->ls_stateid, (caddr_t)&tstateid,
5107 NFSBCOPY((caddr_t)&stp->ls_lfp->lf_fh, (caddr_t)&tfh,
5112 NFSLOCKV4ROOTMUTEX();
5113 nfsv4_unlock(&nfsv4rootfs_lock, 1);
5114 NFSUNLOCKV4ROOTMUTEX();
5118 error = nfsrv_docallback(clp, NFSV4OP_CBRECALL,
5119 &tstateid, 0, &tfh, NULL, NULL, 0, p);
5121 } while ((error == NFSERR_BADSTATEID ||
5122 error == NFSERR_BADHANDLE) && retrycnt < NFSV4_CBRETRYCNT);
5123 error = NFSERR_DELAY;
5127 if (clp->lc_expiry >= NFSD_MONOSEC &&
5128 stp->ls_delegtime >= NFSD_MONOSEC) {
5131 * A recall has been done, but it has not yet expired.
5136 NFSLOCKV4ROOTMUTEX();
5137 nfsv4_unlock(&nfsv4rootfs_lock, 1);
5138 NFSUNLOCKV4ROOTMUTEX();
5140 error = NFSERR_DELAY;
5145 * If we don't yet have the lock, just get it and then return,
5146 * since we need that before deleting expired state, such as
5148 * When getting the lock, unlock the vnode, so other nfsds that
5149 * are in progress, won't get stuck waiting for the vnode lock.
5151 if (*haslockp == 0) {
5154 lktype = NFSVOPISLOCKED(vp);
5155 NFSVOPUNLOCK(vp, 0);
5157 NFSLOCKV4ROOTMUTEX();
5158 nfsv4_relref(&nfsv4rootfs_lock);
5160 gotlock = nfsv4_lock(&nfsv4rootfs_lock, 1, NULL,
5161 NFSV4ROOTLOCKMUTEXPTR, NULL);
5163 NFSUNLOCKV4ROOTMUTEX();
5166 NFSVOPLOCK(vp, lktype | LK_RETRY);
5167 if ((vp->v_iflag & VI_DOOMED) != 0) {
5169 NFSLOCKV4ROOTMUTEX();
5170 nfsv4_unlock(&nfsv4rootfs_lock, 1);
5171 NFSUNLOCKV4ROOTMUTEX();
5172 error = NFSERR_PERM;
5182 * Ok, we can delete the expired delegation.
5183 * First, write the Revoke record to stable storage and then
5184 * clear out the conflict.
5185 * Since all other nfsd threads are now blocked, we can safely
5186 * sleep without the state changing.
5188 nfsrv_writestable(clp->lc_id, clp->lc_idlen, NFSNST_REVOKE, p);
5189 nfsrv_backupstable();
5190 if (clp->lc_expiry < NFSD_MONOSEC) {
5191 nfsrv_cleanclient(clp, p);
5192 nfsrv_freedeleglist(&clp->lc_deleg);
5193 nfsrv_freedeleglist(&clp->lc_olddeleg);
5194 LIST_REMOVE(clp, lc_hash);
5197 nfsrv_freedeleg(stp);
5201 nfsrv_zapclient(clp, p);
5210 * Check for a remove allowed, if remove is set to 1 and get rid of
5214 nfsrv_checkremove(vnode_t vp, int remove, NFSPROC_T *p)
5216 struct nfsstate *stp;
5217 struct nfslockfile *lfp;
5218 int error, haslock = 0;
5222 * First, get the lock file structure.
5223 * (A return of -1 means no associated state, so remove ok.)
5225 error = nfsrv_getlockfh(vp, NFSLCK_CHECK, NULL, &nfh, p);
5229 error = nfsrv_getlockfile(NFSLCK_CHECK, NULL, &lfp, &nfh, 0);
5233 NFSLOCKV4ROOTMUTEX();
5234 nfsv4_unlock(&nfsv4rootfs_lock, 1);
5235 NFSUNLOCKV4ROOTMUTEX();
5243 * Now, we must Recall any delegations.
5245 error = nfsrv_cleandeleg(vp, lfp, NULL, &haslock, p);
5248 * nfsrv_cleandeleg() unlocks state for non-zero
5254 NFSLOCKV4ROOTMUTEX();
5255 nfsv4_unlock(&nfsv4rootfs_lock, 1);
5256 NFSUNLOCKV4ROOTMUTEX();
5262 * Now, look for a conflicting open share.
5266 * If the entry in the directory was the last reference to the
5267 * corresponding filesystem object, the object can be destroyed
5269 if(lfp->lf_usecount>1)
5270 LIST_FOREACH(stp, &lfp->lf_open, ls_file) {
5271 if (stp->ls_flags & NFSLCK_WRITEDENY) {
5272 error = NFSERR_FILEOPEN;
5280 NFSLOCKV4ROOTMUTEX();
5281 nfsv4_unlock(&nfsv4rootfs_lock, 1);
5282 NFSUNLOCKV4ROOTMUTEX();
5291 * Clear out all delegations for the file referred to by lfp.
5292 * May return NFSERR_DELAY, if there will be a delay waiting for
5293 * delegations to expire.
5294 * Returns -1 to indicate it slept while recalling a delegation.
5295 * This function has the side effect of deleting the nfslockfile structure,
5296 * if it no longer has associated state and didn't have to sleep.
5297 * Unlocks State before a non-zero value is returned.
5300 nfsrv_cleandeleg(vnode_t vp, struct nfslockfile *lfp,
5301 struct nfsclient *clp, int *haslockp, NFSPROC_T *p)
5303 struct nfsstate *stp, *nstp;
5306 stp = LIST_FIRST(&lfp->lf_deleg);
5307 while (stp != LIST_END(&lfp->lf_deleg)) {
5308 nstp = LIST_NEXT(stp, ls_file);
5309 if (stp->ls_clp != clp) {
5310 ret = nfsrv_delegconflict(stp, haslockp, p, vp);
5313 * nfsrv_delegconflict() unlocks state
5314 * when it returns non-zero.
5327 * There are certain operations that, when being done outside of NFSv4,
5328 * require that any NFSv4 delegation for the file be recalled.
5329 * This function is to be called for those cases:
5330 * VOP_RENAME() - When a delegation is being recalled for any reason,
5331 * the client may have to do Opens against the server, using the file's
5332 * final component name. If the file has been renamed on the server,
5333 * that component name will be incorrect and the Open will fail.
5334 * VOP_REMOVE() - Theoretically, a client could Open a file after it has
5335 * been removed on the server, if there is a delegation issued to
5336 * that client for the file. I say "theoretically" since clients
5337 * normally do an Access Op before the Open and that Access Op will
5338 * fail with ESTALE. Note that NFSv2 and 3 don't even do Opens, so
5339 * they will detect the file's removal in the same manner. (There is
5340 * one case where RFC3530 allows a client to do an Open without first
5341 * doing an Access Op, which is passage of a check against the ACE
5342 * returned with a Write delegation, but current practice is to ignore
5343 * the ACE and always do an Access Op.)
5344 * Since the functions can only be called with an unlocked vnode, this
5345 * can't be done at this time.
5346 * VOP_ADVLOCK() - When a client holds a delegation, it can issue byte range
5347 * locks locally in the client, which are not visible to the server. To
5348 * deal with this, issuing of delegations for a vnode must be disabled
5349 * and all delegations for the vnode recalled. This is done via the
5350 * second function, using the VV_DISABLEDELEG vflag on the vnode.
5353 nfsd_recalldelegation(vnode_t vp, NFSPROC_T *p)
5359 * First, check to see if the server is currently running and it has
5360 * been called for a regular file when issuing delegations.
5362 if (newnfs_numnfsd == 0 || vp->v_type != VREG ||
5363 nfsrv_issuedelegs == 0)
5366 KASSERT((NFSVOPISLOCKED(vp) != LK_EXCLUSIVE), ("vp %p is locked", vp));
5368 * First, get a reference on the nfsv4rootfs_lock so that an
5369 * exclusive lock cannot be acquired by another thread.
5371 NFSLOCKV4ROOTMUTEX();
5372 nfsv4_getref(&nfsv4rootfs_lock, NULL, NFSV4ROOTLOCKMUTEXPTR, NULL);
5373 NFSUNLOCKV4ROOTMUTEX();
5376 * Now, call nfsrv_checkremove() in a loop while it returns
5377 * NFSERR_DELAY. Return upon any other error or when timed out.
5379 starttime = NFSD_MONOSEC;
5381 if (NFSVOPLOCK(vp, LK_EXCLUSIVE) == 0) {
5382 error = nfsrv_checkremove(vp, 0, p);
5383 NFSVOPUNLOCK(vp, 0);
5386 if (error == NFSERR_DELAY) {
5387 if (NFSD_MONOSEC - starttime > NFS_REMOVETIMEO)
5389 /* Sleep for a short period of time */
5390 (void) nfs_catnap(PZERO, 0, "nfsremove");
5392 } while (error == NFSERR_DELAY);
5393 NFSLOCKV4ROOTMUTEX();
5394 nfsv4_relref(&nfsv4rootfs_lock);
5395 NFSUNLOCKV4ROOTMUTEX();
5399 nfsd_disabledelegation(vnode_t vp, NFSPROC_T *p)
5402 #ifdef VV_DISABLEDELEG
5404 * First, flag issuance of delegations disabled.
5406 atomic_set_long(&vp->v_vflag, VV_DISABLEDELEG);
5410 * Then call nfsd_recalldelegation() to get rid of all extant
5413 nfsd_recalldelegation(vp, p);
5417 * Check for conflicting locks, etc. and then get rid of delegations.
5418 * (At one point I thought that I should get rid of delegations for any
5419 * Setattr, since it could potentially disallow the I/O op (read or write)
5420 * allowed by the delegation. However, Setattr Ops that aren't changing
5421 * the size get a stateid of all 0s, so you can't tell if it is a delegation
5422 * for the same client or a different one, so I decided to only get rid
5423 * of delegations for other clients when the size is being changed.)
5424 * In general, a Setattr can disable NFS I/O Ops that are outstanding, such
5425 * as Write backs, even if there is no delegation, so it really isn't any
5429 nfsrv_checksetattr(vnode_t vp, struct nfsrv_descript *nd,
5430 nfsv4stateid_t *stateidp, struct nfsvattr *nvap, nfsattrbit_t *attrbitp,
5431 struct nfsexstuff *exp, NFSPROC_T *p)
5433 struct nfsstate st, *stp = &st;
5434 struct nfslock lo, *lop = &lo;
5438 if (NFSISSET_ATTRBIT(attrbitp, NFSATTRBIT_SIZE)) {
5439 stp->ls_flags = (NFSLCK_CHECK | NFSLCK_WRITEACCESS);
5440 lop->lo_first = nvap->na_size;
5445 if (NFSISSET_ATTRBIT(attrbitp, NFSATTRBIT_OWNER) ||
5446 NFSISSET_ATTRBIT(attrbitp, NFSATTRBIT_OWNERGROUP) ||
5447 NFSISSET_ATTRBIT(attrbitp, NFSATTRBIT_MODE) ||
5448 NFSISSET_ATTRBIT(attrbitp, NFSATTRBIT_ACL))
5449 stp->ls_flags |= NFSLCK_SETATTR;
5450 if (stp->ls_flags == 0)
5452 lop->lo_end = NFS64BITSSET;
5453 lop->lo_flags = NFSLCK_WRITE;
5454 stp->ls_ownerlen = 0;
5456 stp->ls_uid = nd->nd_cred->cr_uid;
5457 stp->ls_stateid.seqid = stateidp->seqid;
5458 clientid.lval[0] = stp->ls_stateid.other[0] = stateidp->other[0];
5459 clientid.lval[1] = stp->ls_stateid.other[1] = stateidp->other[1];
5460 stp->ls_stateid.other[2] = stateidp->other[2];
5461 error = nfsrv_lockctrl(vp, &stp, &lop, NULL, clientid,
5462 stateidp, exp, nd, p);
5465 NFSEXITCODE2(error, nd);
5470 * Check for a write delegation and do a CBGETATTR if there is one, updating
5471 * the attributes, as required.
5472 * Should I return an error if I can't get the attributes? (For now, I'll
5476 nfsrv_checkgetattr(struct nfsrv_descript *nd, vnode_t vp,
5477 struct nfsvattr *nvap, nfsattrbit_t *attrbitp, NFSPROC_T *p)
5479 struct nfsstate *stp;
5480 struct nfslockfile *lfp;
5481 struct nfsclient *clp;
5482 struct nfsvattr nva;
5485 nfsattrbit_t cbbits;
5486 u_quad_t delegfilerev;
5488 NFSCBGETATTR_ATTRBIT(attrbitp, &cbbits);
5489 if (!NFSNONZERO_ATTRBIT(&cbbits))
5491 if (nfsrv_writedelegcnt == 0)
5495 * Get the lock file structure.
5496 * (A return of -1 means no associated state, so return ok.)
5498 error = nfsrv_getlockfh(vp, NFSLCK_CHECK, NULL, &nfh, p);
5501 error = nfsrv_getlockfile(NFSLCK_CHECK, NULL, &lfp, &nfh, 0);
5510 * Now, look for a write delegation.
5512 LIST_FOREACH(stp, &lfp->lf_deleg, ls_file) {
5513 if (stp->ls_flags & NFSLCK_DELEGWRITE)
5516 if (stp == LIST_END(&lfp->lf_deleg)) {
5521 delegfilerev = stp->ls_filerev;
5524 * If the Write delegation was issued as a part of this Compound RPC
5525 * or if we have an Implied Clientid (used in a previous Op in this
5526 * compound) and it is the client the delegation was issued to,
5528 * I also assume that it is from the same client iff the network
5529 * host IP address is the same as the callback address. (Not
5530 * exactly correct by the RFC, but avoids a lot of Getattr
5533 if (nd->nd_compref == stp->ls_compref ||
5534 ((nd->nd_flag & ND_IMPLIEDCLID) &&
5535 clp->lc_clientid.qval == nd->nd_clientid.qval) ||
5536 nfsaddr2_match(clp->lc_req.nr_nam, nd->nd_nam)) {
5542 * We are now done with the delegation state structure,
5543 * so the statelock can be released and we can now tsleep().
5547 * Now, we must do the CB Getattr callback, to see if Change or Size
5550 if (clp->lc_expiry >= NFSD_MONOSEC) {
5552 NFSVNO_ATTRINIT(&nva);
5553 nva.na_filerev = NFS64BITSSET;
5554 error = nfsrv_docallback(clp, NFSV4OP_CBGETATTR, NULL,
5555 0, &nfh, &nva, &cbbits, 0, p);
5557 if ((nva.na_filerev != NFS64BITSSET &&
5558 nva.na_filerev > delegfilerev) ||
5559 (NFSVNO_ISSETSIZE(&nva) &&
5560 nva.na_size != nvap->na_size)) {
5561 error = nfsvno_updfilerev(vp, nvap, nd, p);
5562 if (NFSVNO_ISSETSIZE(&nva))
5563 nvap->na_size = nva.na_size;
5566 error = 0; /* Ignore callback errors for now. */
5572 NFSEXITCODE2(error, nd);
5577 * This function looks for openowners that haven't had any opens for
5578 * a while and throws them away. Called by an nfsd when NFSNSF_NOOPENS
5582 nfsrv_throwawayopens(NFSPROC_T *p)
5584 struct nfsclient *clp, *nclp;
5585 struct nfsstate *stp, *nstp;
5589 nfsrv_stablefirst.nsf_flags &= ~NFSNSF_NOOPENS;
5591 * For each client...
5593 for (i = 0; i < nfsrv_clienthashsize; i++) {
5594 LIST_FOREACH_SAFE(clp, &nfsclienthash[i], lc_hash, nclp) {
5595 LIST_FOREACH_SAFE(stp, &clp->lc_open, ls_list, nstp) {
5596 if (LIST_EMPTY(&stp->ls_open) &&
5597 (stp->ls_noopens > NFSNOOPEN ||
5598 (nfsrv_openpluslock * 2) >
5599 nfsrv_v4statelimit))
5600 nfsrv_freeopenowner(stp, 0, p);
5608 * This function checks to see if the credentials are the same.
5609 * Returns 1 for not same, 0 otherwise.
5612 nfsrv_notsamecredname(struct nfsrv_descript *nd, struct nfsclient *clp)
5615 if (nd->nd_flag & ND_GSS) {
5616 if (!(clp->lc_flags & LCL_GSS))
5618 if (clp->lc_flags & LCL_NAME) {
5619 if (nd->nd_princlen != clp->lc_namelen ||
5620 NFSBCMP(nd->nd_principal, clp->lc_name,
5626 if (nd->nd_cred->cr_uid == clp->lc_uid)
5630 } else if (clp->lc_flags & LCL_GSS)
5633 * For AUTH_SYS, allow the same uid or root. (This is underspecified
5634 * in RFC3530, which talks about principals, but doesn't say anything
5635 * about uids for AUTH_SYS.)
5637 if (nd->nd_cred->cr_uid == clp->lc_uid || nd->nd_cred->cr_uid == 0)
5644 * Calculate the lease expiry time.
5647 nfsrv_leaseexpiry(void)
5650 if (nfsrv_stablefirst.nsf_eograce > NFSD_MONOSEC)
5651 return (NFSD_MONOSEC + 2 * (nfsrv_lease + NFSRV_LEASEDELTA));
5652 return (NFSD_MONOSEC + nfsrv_lease + NFSRV_LEASEDELTA);
5656 * Delay the delegation timeout as far as ls_delegtimelimit, as required.
5659 nfsrv_delaydelegtimeout(struct nfsstate *stp)
5662 if ((stp->ls_flags & NFSLCK_DELEGRECALL) == 0)
5665 if ((stp->ls_delegtime + 15) > NFSD_MONOSEC &&
5666 stp->ls_delegtime < stp->ls_delegtimelimit) {
5667 stp->ls_delegtime += nfsrv_lease;
5668 if (stp->ls_delegtime > stp->ls_delegtimelimit)
5669 stp->ls_delegtime = stp->ls_delegtimelimit;
5674 * This function checks to see if there is any other state associated
5675 * with the openowner for this Open.
5676 * It returns 1 if there is no other state, 0 otherwise.
5679 nfsrv_nootherstate(struct nfsstate *stp)
5681 struct nfsstate *tstp;
5683 LIST_FOREACH(tstp, &stp->ls_openowner->ls_open, ls_list) {
5684 if (tstp != stp || !LIST_EMPTY(&tstp->ls_lock))
5691 * Create a list of lock deltas (changes to local byte range locking
5692 * that can be rolled back using the list) and apply the changes via
5693 * nfsvno_advlock(). Optionally, lock the list. It is expected that either
5694 * the rollback or update function will be called after this.
5695 * It returns an error (and rolls back, as required), if any nfsvno_advlock()
5696 * call fails. If it returns an error, it will unlock the list.
5699 nfsrv_locallock(vnode_t vp, struct nfslockfile *lfp, int flags,
5700 uint64_t first, uint64_t end, struct nfslockconflict *cfp, NFSPROC_T *p)
5702 struct nfslock *lop, *nlop;
5705 /* Loop through the list of locks. */
5706 lop = LIST_FIRST(&lfp->lf_locallock);
5707 while (first < end && lop != NULL) {
5708 nlop = LIST_NEXT(lop, lo_lckowner);
5709 if (first >= lop->lo_end) {
5712 } else if (first < lop->lo_first) {
5713 /* new one starts before entry in list */
5714 if (end <= lop->lo_first) {
5715 /* no overlap between old and new */
5716 error = nfsrv_dolocal(vp, lfp, flags,
5717 NFSLCK_UNLOCK, first, end, cfp, p);
5722 /* handle fragment overlapped with new one */
5723 error = nfsrv_dolocal(vp, lfp, flags,
5724 NFSLCK_UNLOCK, first, lop->lo_first, cfp,
5728 first = lop->lo_first;
5731 /* new one overlaps this entry in list */
5732 if (end <= lop->lo_end) {
5733 /* overlaps all of new one */
5734 error = nfsrv_dolocal(vp, lfp, flags,
5735 lop->lo_flags, first, end, cfp, p);
5740 /* handle fragment overlapped with new one */
5741 error = nfsrv_dolocal(vp, lfp, flags,
5742 lop->lo_flags, first, lop->lo_end, cfp, p);
5745 first = lop->lo_end;
5750 if (first < end && error == 0)
5751 /* handle fragment past end of list */
5752 error = nfsrv_dolocal(vp, lfp, flags, NFSLCK_UNLOCK, first,
5760 * Local lock unlock. Unlock all byte ranges that are no longer locked
5761 * by NFSv4. To do this, unlock any subranges of first-->end that
5762 * do not overlap with the byte ranges of any lock in the lfp->lf_lock
5763 * list. This list has all locks for the file held by other
5764 * <clientid, lockowner> tuples. The list is ordered by increasing
5765 * lo_first value, but may have entries that overlap each other, for
5766 * the case of read locks.
5769 nfsrv_localunlock(vnode_t vp, struct nfslockfile *lfp, uint64_t init_first,
5770 uint64_t init_end, NFSPROC_T *p)
5772 struct nfslock *lop;
5773 uint64_t first, end, prevfirst __unused;
5777 while (first < init_end) {
5778 /* Loop through all nfs locks, adjusting first and end */
5780 LIST_FOREACH(lop, &lfp->lf_lock, lo_lckfile) {
5781 KASSERT(prevfirst <= lop->lo_first,
5782 ("nfsv4 locks out of order"));
5783 KASSERT(lop->lo_first < lop->lo_end,
5784 ("nfsv4 bogus lock"));
5785 prevfirst = lop->lo_first;
5786 if (first >= lop->lo_first &&
5787 first < lop->lo_end)
5789 * Overlaps with initial part, so trim
5790 * off that initial part by moving first past
5793 first = lop->lo_end;
5794 else if (end > lop->lo_first &&
5795 lop->lo_first > first) {
5797 * This lock defines the end of the
5798 * segment to unlock, so set end to the
5799 * start of it and break out of the loop.
5801 end = lop->lo_first;
5806 * There is no segment left to do, so
5807 * break out of this loop and then exit
5808 * the outer while() since first will be set
5809 * to end, which must equal init_end here.
5814 /* Unlock this segment */
5815 (void) nfsrv_dolocal(vp, lfp, NFSLCK_UNLOCK,
5816 NFSLCK_READ, first, end, NULL, p);
5817 nfsrv_locallock_commit(lfp, NFSLCK_UNLOCK,
5821 * Now move past this segment and look for any further
5822 * segment in the range, if there is one.
5830 * Do the local lock operation and update the rollback list, as required.
5831 * Perform the rollback and return the error if nfsvno_advlock() fails.
5834 nfsrv_dolocal(vnode_t vp, struct nfslockfile *lfp, int flags, int oldflags,
5835 uint64_t first, uint64_t end, struct nfslockconflict *cfp, NFSPROC_T *p)
5837 struct nfsrollback *rlp;
5838 int error = 0, ltype, oldltype;
5840 if (flags & NFSLCK_WRITE)
5842 else if (flags & NFSLCK_READ)
5846 if (oldflags & NFSLCK_WRITE)
5848 else if (oldflags & NFSLCK_READ)
5852 if (ltype == oldltype || (oldltype == F_WRLCK && ltype == F_RDLCK))
5855 error = nfsvno_advlock(vp, ltype, first, end, p);
5858 cfp->cl_clientid.lval[0] = 0;
5859 cfp->cl_clientid.lval[1] = 0;
5861 cfp->cl_end = NFS64BITSSET;
5862 cfp->cl_flags = NFSLCK_WRITE;
5863 cfp->cl_ownerlen = 5;
5864 NFSBCOPY("LOCAL", cfp->cl_owner, 5);
5866 nfsrv_locallock_rollback(vp, lfp, p);
5867 } else if (ltype != F_UNLCK) {
5868 rlp = malloc(sizeof (struct nfsrollback), M_NFSDROLLBACK,
5870 rlp->rlck_first = first;
5871 rlp->rlck_end = end;
5872 rlp->rlck_type = oldltype;
5873 LIST_INSERT_HEAD(&lfp->lf_rollback, rlp, rlck_list);
5882 * Roll back local lock changes and free up the rollback list.
5885 nfsrv_locallock_rollback(vnode_t vp, struct nfslockfile *lfp, NFSPROC_T *p)
5887 struct nfsrollback *rlp, *nrlp;
5889 LIST_FOREACH_SAFE(rlp, &lfp->lf_rollback, rlck_list, nrlp) {
5890 (void) nfsvno_advlock(vp, rlp->rlck_type, rlp->rlck_first,
5892 free(rlp, M_NFSDROLLBACK);
5894 LIST_INIT(&lfp->lf_rollback);
5898 * Update local lock list and delete rollback list (ie now committed to the
5899 * local locks). Most of the work is done by the internal function.
5902 nfsrv_locallock_commit(struct nfslockfile *lfp, int flags, uint64_t first,
5905 struct nfsrollback *rlp, *nrlp;
5906 struct nfslock *new_lop, *other_lop;
5908 new_lop = malloc(sizeof (struct nfslock), M_NFSDLOCK, M_WAITOK);
5909 if (flags & (NFSLCK_READ | NFSLCK_WRITE))
5910 other_lop = malloc(sizeof (struct nfslock), M_NFSDLOCK,
5914 new_lop->lo_flags = flags;
5915 new_lop->lo_first = first;
5916 new_lop->lo_end = end;
5917 nfsrv_updatelock(NULL, &new_lop, &other_lop, lfp);
5918 if (new_lop != NULL)
5919 free(new_lop, M_NFSDLOCK);
5920 if (other_lop != NULL)
5921 free(other_lop, M_NFSDLOCK);
5923 /* and get rid of the rollback list */
5924 LIST_FOREACH_SAFE(rlp, &lfp->lf_rollback, rlck_list, nrlp)
5925 free(rlp, M_NFSDROLLBACK);
5926 LIST_INIT(&lfp->lf_rollback);
5930 * Lock the struct nfslockfile for local lock updating.
5933 nfsrv_locklf(struct nfslockfile *lfp)
5937 /* lf_usecount ensures *lfp won't be free'd */
5940 gotlock = nfsv4_lock(&lfp->lf_locallock_lck, 1, NULL,
5941 NFSSTATEMUTEXPTR, NULL);
5942 } while (gotlock == 0);
5947 * Unlock the struct nfslockfile after local lock updating.
5950 nfsrv_unlocklf(struct nfslockfile *lfp)
5953 nfsv4_unlock(&lfp->lf_locallock_lck, 0);
5957 * Clear out all state for the NFSv4 server.
5958 * Must be called by a thread that can sleep when no nfsds are running.
5961 nfsrv_throwawayallstate(NFSPROC_T *p)
5963 struct nfsclient *clp, *nclp;
5964 struct nfslockfile *lfp, *nlfp;
5968 * For each client, clean out the state and then free the structure.
5970 for (i = 0; i < nfsrv_clienthashsize; i++) {
5971 LIST_FOREACH_SAFE(clp, &nfsclienthash[i], lc_hash, nclp) {
5972 nfsrv_cleanclient(clp, p);
5973 nfsrv_freedeleglist(&clp->lc_deleg);
5974 nfsrv_freedeleglist(&clp->lc_olddeleg);
5975 free(clp->lc_stateid, M_NFSDCLIENT);
5976 free(clp, M_NFSDCLIENT);
5981 * Also, free up any remaining lock file structures.
5983 for (i = 0; i < nfsrv_lockhashsize; i++) {
5984 LIST_FOREACH_SAFE(lfp, &nfslockhash[i], lf_hash, nlfp) {
5985 printf("nfsd unload: fnd a lock file struct\n");
5986 nfsrv_freenfslockfile(lfp);
5990 /* And get rid of the deviceid structures and layouts. */
5991 nfsrv_freealllayoutsanddevids();
5995 * Check the sequence# for the session and slot provided as an argument.
5996 * Also, renew the lease if the session will return NFS_OK.
5999 nfsrv_checksequence(struct nfsrv_descript *nd, uint32_t sequenceid,
6000 uint32_t *highest_slotidp, uint32_t *target_highest_slotidp, int cache_this,
6001 uint32_t *sflagsp, NFSPROC_T *p)
6003 struct nfsdsession *sep;
6004 struct nfssessionhash *shp;
6008 shp = NFSSESSIONHASH(nd->nd_sessionid);
6009 NFSLOCKSESSION(shp);
6010 sep = nfsrv_findsession(nd->nd_sessionid);
6012 NFSUNLOCKSESSION(shp);
6013 return (NFSERR_BADSESSION);
6015 error = nfsv4_seqsession(sequenceid, nd->nd_slotid, *highest_slotidp,
6016 sep->sess_slots, NULL, NFSV4_SLOTS - 1);
6018 NFSUNLOCKSESSION(shp);
6021 if (cache_this != 0)
6022 nd->nd_flag |= ND_SAVEREPLY;
6023 /* Renew the lease. */
6024 sep->sess_clp->lc_expiry = nfsrv_leaseexpiry();
6025 nd->nd_clientid.qval = sep->sess_clp->lc_clientid.qval;
6026 nd->nd_flag |= ND_IMPLIEDCLID;
6029 * If this session handles the backchannel, save the nd_xprt for this
6030 * RPC, since this is the one being used.
6031 * RFC-5661 specifies that the fore channel will be implicitly
6032 * bound by a Sequence operation. However, since some NFSv4.1 clients
6033 * erroneously assumed that the back channel would be implicitly
6034 * bound as well, do the implicit binding unless a
6035 * BindConnectiontoSession has already been done on the session.
6037 if (sep->sess_clp->lc_req.nr_client != NULL &&
6038 sep->sess_cbsess.nfsess_xprt != nd->nd_xprt &&
6039 (sep->sess_crflags & NFSV4CRSESS_CONNBACKCHAN) != 0 &&
6040 (sep->sess_clp->lc_flags & LCL_DONEBINDCONN) == 0) {
6042 "nfsrv_checksequence: implicit back channel bind\n");
6043 savxprt = sep->sess_cbsess.nfsess_xprt;
6044 SVC_ACQUIRE(nd->nd_xprt);
6045 nd->nd_xprt->xp_p2 =
6046 sep->sess_clp->lc_req.nr_client->cl_private;
6047 nd->nd_xprt->xp_idletimeout = 0; /* Disable timeout. */
6048 sep->sess_cbsess.nfsess_xprt = nd->nd_xprt;
6049 if (savxprt != NULL)
6050 SVC_RELEASE(savxprt);
6054 if (sep->sess_clp->lc_req.nr_client == NULL)
6055 *sflagsp |= NFSV4SEQ_CBPATHDOWN;
6056 NFSUNLOCKSESSION(shp);
6057 if (error == NFSERR_EXPIRED) {
6058 *sflagsp |= NFSV4SEQ_EXPIREDALLSTATEREVOKED;
6060 } else if (error == NFSERR_ADMINREVOKED) {
6061 *sflagsp |= NFSV4SEQ_ADMINSTATEREVOKED;
6064 *highest_slotidp = *target_highest_slotidp = NFSV4_SLOTS - 1;
6069 * Check/set reclaim complete for this session/clientid.
6072 nfsrv_checkreclaimcomplete(struct nfsrv_descript *nd, int onefs)
6074 struct nfsdsession *sep;
6075 struct nfssessionhash *shp;
6078 shp = NFSSESSIONHASH(nd->nd_sessionid);
6080 NFSLOCKSESSION(shp);
6081 sep = nfsrv_findsession(nd->nd_sessionid);
6083 NFSUNLOCKSESSION(shp);
6085 return (NFSERR_BADSESSION);
6089 sep->sess_clp->lc_flags |= LCL_RECLAIMONEFS;
6090 /* Check to see if reclaim complete has already happened. */
6091 else if ((sep->sess_clp->lc_flags & LCL_RECLAIMCOMPLETE) != 0)
6092 error = NFSERR_COMPLETEALREADY;
6094 sep->sess_clp->lc_flags |= LCL_RECLAIMCOMPLETE;
6095 nfsrv_markreclaim(sep->sess_clp);
6097 NFSUNLOCKSESSION(shp);
6103 * Cache the reply in a session slot.
6106 nfsrv_cache_session(uint8_t *sessionid, uint32_t slotid, int repstat,
6109 struct nfsdsession *sep;
6110 struct nfssessionhash *shp;
6112 shp = NFSSESSIONHASH(sessionid);
6113 NFSLOCKSESSION(shp);
6114 sep = nfsrv_findsession(sessionid);
6116 NFSUNLOCKSESSION(shp);
6117 printf("nfsrv_cache_session: no session\n");
6121 nfsv4_seqsess_cacherep(slotid, sep->sess_slots, repstat, m);
6122 NFSUNLOCKSESSION(shp);
6126 * Search for a session that matches the sessionid.
6128 static struct nfsdsession *
6129 nfsrv_findsession(uint8_t *sessionid)
6131 struct nfsdsession *sep;
6132 struct nfssessionhash *shp;
6134 shp = NFSSESSIONHASH(sessionid);
6135 LIST_FOREACH(sep, &shp->list, sess_hash) {
6136 if (!NFSBCMP(sessionid, sep->sess_sessionid, NFSX_V4SESSIONID))
6143 * Destroy a session.
6146 nfsrv_destroysession(struct nfsrv_descript *nd, uint8_t *sessionid)
6148 int error, igotlock, samesess;
6151 if (!NFSBCMP(sessionid, nd->nd_sessionid, NFSX_V4SESSIONID) &&
6152 (nd->nd_flag & ND_HASSEQUENCE) != 0) {
6154 if ((nd->nd_flag & ND_LASTOP) == 0)
6155 return (NFSERR_BADSESSION);
6158 /* Lock out other nfsd threads */
6159 NFSLOCKV4ROOTMUTEX();
6160 nfsv4_relref(&nfsv4rootfs_lock);
6162 igotlock = nfsv4_lock(&nfsv4rootfs_lock, 1, NULL,
6163 NFSV4ROOTLOCKMUTEXPTR, NULL);
6164 } while (igotlock == 0);
6165 NFSUNLOCKV4ROOTMUTEX();
6167 error = nfsrv_freesession(NULL, sessionid);
6168 if (error == 0 && samesess != 0)
6169 nd->nd_flag &= ~ND_HASSEQUENCE;
6171 NFSLOCKV4ROOTMUTEX();
6172 nfsv4_unlock(&nfsv4rootfs_lock, 1);
6173 NFSUNLOCKV4ROOTMUTEX();
6178 * Bind a connection to a session.
6179 * For now, only certain variants are supported, since the current session
6180 * structure can only handle a single backchannel entry, which will be
6181 * applied to all connections if it is set.
6184 nfsrv_bindconnsess(struct nfsrv_descript *nd, uint8_t *sessionid, int *foreaftp)
6186 struct nfssessionhash *shp;
6187 struct nfsdsession *sep;
6188 struct nfsclient *clp;
6193 shp = NFSSESSIONHASH(sessionid);
6195 NFSLOCKSESSION(shp);
6196 sep = nfsrv_findsession(sessionid);
6198 clp = sep->sess_clp;
6199 if (*foreaftp == NFSCDFC4_BACK ||
6200 *foreaftp == NFSCDFC4_BACK_OR_BOTH ||
6201 *foreaftp == NFSCDFC4_FORE_OR_BOTH) {
6202 /* Try to set up a backchannel. */
6203 if (clp->lc_req.nr_client == NULL) {
6204 NFSD_DEBUG(2, "nfsrv_bindconnsess: acquire "
6206 clp->lc_req.nr_client = (struct __rpc_client *)
6207 clnt_bck_create(nd->nd_xprt->xp_socket,
6208 sep->sess_cbprogram, NFSV4_CBVERS);
6210 if (clp->lc_req.nr_client != NULL) {
6211 NFSD_DEBUG(2, "nfsrv_bindconnsess: set up "
6213 savxprt = sep->sess_cbsess.nfsess_xprt;
6214 SVC_ACQUIRE(nd->nd_xprt);
6215 nd->nd_xprt->xp_p2 =
6216 clp->lc_req.nr_client->cl_private;
6217 /* Disable idle timeout. */
6218 nd->nd_xprt->xp_idletimeout = 0;
6219 sep->sess_cbsess.nfsess_xprt = nd->nd_xprt;
6220 if (savxprt != NULL)
6221 SVC_RELEASE(savxprt);
6222 sep->sess_crflags |= NFSV4CRSESS_CONNBACKCHAN;
6223 clp->lc_flags |= LCL_DONEBINDCONN;
6224 if (*foreaftp == NFSCDFS4_BACK)
6225 *foreaftp = NFSCDFS4_BACK;
6227 *foreaftp = NFSCDFS4_BOTH;
6228 } else if (*foreaftp != NFSCDFC4_BACK) {
6229 NFSD_DEBUG(2, "nfsrv_bindconnsess: can't set "
6230 "up backchannel\n");
6231 sep->sess_crflags &= ~NFSV4CRSESS_CONNBACKCHAN;
6232 clp->lc_flags |= LCL_DONEBINDCONN;
6233 *foreaftp = NFSCDFS4_FORE;
6235 error = NFSERR_NOTSUPP;
6236 printf("nfsrv_bindconnsess: Can't add "
6240 NFSD_DEBUG(2, "nfsrv_bindconnsess: Set forechannel\n");
6241 clp->lc_flags |= LCL_DONEBINDCONN;
6242 *foreaftp = NFSCDFS4_FORE;
6245 error = NFSERR_BADSESSION;
6246 NFSUNLOCKSESSION(shp);
6252 * Free up a session structure.
6255 nfsrv_freesession(struct nfsdsession *sep, uint8_t *sessionid)
6257 struct nfssessionhash *shp;
6262 shp = NFSSESSIONHASH(sessionid);
6263 NFSLOCKSESSION(shp);
6264 sep = nfsrv_findsession(sessionid);
6266 shp = NFSSESSIONHASH(sep->sess_sessionid);
6267 NFSLOCKSESSION(shp);
6271 if (sep->sess_refcnt > 0) {
6272 NFSUNLOCKSESSION(shp);
6274 return (NFSERR_BACKCHANBUSY);
6276 LIST_REMOVE(sep, sess_hash);
6277 LIST_REMOVE(sep, sess_list);
6279 NFSUNLOCKSESSION(shp);
6282 return (NFSERR_BADSESSION);
6283 for (i = 0; i < NFSV4_SLOTS; i++)
6284 if (sep->sess_slots[i].nfssl_reply != NULL)
6285 m_freem(sep->sess_slots[i].nfssl_reply);
6286 if (sep->sess_cbsess.nfsess_xprt != NULL)
6287 SVC_RELEASE(sep->sess_cbsess.nfsess_xprt);
6288 free(sep, M_NFSDSESSION);
6294 * RFC5661 says that it should fail when there are associated opens, locks
6295 * or delegations. Since stateids represent opens, I don't see how you can
6296 * free an open stateid (it will be free'd when closed), so this function
6297 * only works for lock stateids (freeing the lock_owner) or delegations.
6300 nfsrv_freestateid(struct nfsrv_descript *nd, nfsv4stateid_t *stateidp,
6303 struct nfsclient *clp;
6304 struct nfsstate *stp;
6309 * Look up the stateid
6311 error = nfsrv_getclient((nfsquad_t)((u_quad_t)0), CLOPS_RENEW, &clp,
6312 NULL, (nfsquad_t)((u_quad_t)0), 0, nd, p);
6314 /* First, check for a delegation. */
6315 LIST_FOREACH(stp, &clp->lc_deleg, ls_list) {
6316 if (!NFSBCMP(stp->ls_stateid.other, stateidp->other,
6321 nfsrv_freedeleg(stp);
6326 /* Not a delegation, try for a lock_owner. */
6328 error = nfsrv_getstate(clp, stateidp, 0, &stp);
6329 if (error == 0 && ((stp->ls_flags & (NFSLCK_OPEN | NFSLCK_DELEGREAD |
6330 NFSLCK_DELEGWRITE)) != 0 || (stp->ls_flags & NFSLCK_LOCK) == 0))
6331 /* Not a lock_owner stateid. */
6332 error = NFSERR_LOCKSHELD;
6333 if (error == 0 && !LIST_EMPTY(&stp->ls_lock))
6334 error = NFSERR_LOCKSHELD;
6336 nfsrv_freelockowner(stp, NULL, 0, p);
6345 nfsrv_teststateid(struct nfsrv_descript *nd, nfsv4stateid_t *stateidp,
6348 struct nfsclient *clp;
6349 struct nfsstate *stp;
6354 * Look up the stateid
6356 error = nfsrv_getclient((nfsquad_t)((u_quad_t)0), CLOPS_RENEW, &clp,
6357 NULL, (nfsquad_t)((u_quad_t)0), 0, nd, p);
6359 error = nfsrv_getstate(clp, stateidp, 0, &stp);
6360 if (error == 0 && stateidp->seqid != 0 &&
6361 SEQ_LT(stateidp->seqid, stp->ls_stateid.seqid))
6362 error = NFSERR_OLDSTATEID;
6368 * Generate the xdr for an NFSv4.1 CBSequence Operation.
6371 nfsv4_setcbsequence(struct nfsrv_descript *nd, struct nfsclient *clp,
6372 int dont_replycache, struct nfsdsession **sepp)
6374 struct nfsdsession *sep;
6375 uint32_t *tl, slotseq = 0;
6376 int maxslot, slotpos;
6377 uint8_t sessionid[NFSX_V4SESSIONID];
6380 error = nfsv4_getcbsession(clp, sepp);
6384 (void)nfsv4_sequencelookup(NULL, &sep->sess_cbsess, &slotpos, &maxslot,
6385 &slotseq, sessionid);
6386 KASSERT(maxslot >= 0, ("nfsv4_setcbsequence neg maxslot"));
6388 /* Build the Sequence arguments. */
6389 NFSM_BUILD(tl, uint32_t *, NFSX_V4SESSIONID + 5 * NFSX_UNSIGNED);
6390 bcopy(sessionid, tl, NFSX_V4SESSIONID);
6391 tl += NFSX_V4SESSIONID / NFSX_UNSIGNED;
6392 nd->nd_slotseq = tl;
6393 *tl++ = txdr_unsigned(slotseq);
6394 *tl++ = txdr_unsigned(slotpos);
6395 *tl++ = txdr_unsigned(maxslot);
6396 if (dont_replycache == 0)
6397 *tl++ = newnfs_true;
6399 *tl++ = newnfs_false;
6400 *tl = 0; /* No referring call list, for now. */
6401 nd->nd_flag |= ND_HASSEQUENCE;
6406 * Get a session for the callback.
6409 nfsv4_getcbsession(struct nfsclient *clp, struct nfsdsession **sepp)
6411 struct nfsdsession *sep;
6414 LIST_FOREACH(sep, &clp->lc_session, sess_list) {
6415 if ((sep->sess_crflags & NFSV4CRSESS_CONNBACKCHAN) != 0)
6420 return (NFSERR_BADSESSION);
6429 * Free up all backchannel xprts. This needs to be done when the nfsd threads
6430 * exit, since those transports will all be going away.
6431 * This is only called after all the nfsd threads are done performing RPCs,
6432 * so locking shouldn't be an issue.
6435 nfsrv_freeallbackchannel_xprts(void)
6437 struct nfsdsession *sep;
6438 struct nfsclient *clp;
6442 for (i = 0; i < nfsrv_clienthashsize; i++) {
6443 LIST_FOREACH(clp, &nfsclienthash[i], lc_hash) {
6444 LIST_FOREACH(sep, &clp->lc_session, sess_list) {
6445 xprt = sep->sess_cbsess.nfsess_xprt;
6446 sep->sess_cbsess.nfsess_xprt = NULL;
6455 * Do a layout commit. Actually just call nfsrv_updatemdsattr().
6456 * I have no idea if the rest of these arguments will ever be useful?
6459 nfsrv_layoutcommit(struct nfsrv_descript *nd, vnode_t vp, int layouttype,
6460 int hasnewoff, uint64_t newoff, uint64_t offset, uint64_t len,
6461 int hasnewmtime, struct timespec *newmtimep, int reclaim,
6462 nfsv4stateid_t *stateidp, int maxcnt, char *layp, int *hasnewsizep,
6463 uint64_t *newsizep, struct ucred *cred, NFSPROC_T *p)
6468 error = nfsrv_updatemdsattr(vp, &na, p);
6471 *newsizep = na.na_size;
6477 * Try and get a layout.
6480 nfsrv_layoutget(struct nfsrv_descript *nd, vnode_t vp, struct nfsexstuff *exp,
6481 int layouttype, int *iomode, uint64_t *offset, uint64_t *len,
6482 uint64_t minlen, nfsv4stateid_t *stateidp, int maxcnt, int *retonclose,
6483 int *layoutlenp, char *layp, struct ucred *cred, NFSPROC_T *p)
6485 struct nfslayouthash *lhyp;
6486 struct nfslayout *lyp;
6488 fhandle_t fh, *dsfhp;
6489 int error, mirrorcnt;
6491 if (nfsrv_devidcnt == 0)
6492 return (NFSERR_UNKNLAYOUTTYPE);
6495 printf("nfsrv_layoutget: off=%ju len=%ju\n", (uintmax_t)*offset,
6497 error = nfsvno_getfh(vp, &fh, p);
6498 NFSD_DEBUG(4, "layoutget getfh=%d\n", error);
6503 * For now, all layouts are for entire files.
6504 * Only issue Read/Write layouts if requested for a non-readonly fs.
6506 if (NFSVNO_EXRDONLY(exp)) {
6507 if (*iomode == NFSLAYOUTIOMODE_RW)
6508 return (NFSERR_LAYOUTTRYLATER);
6509 *iomode = NFSLAYOUTIOMODE_READ;
6511 if (*iomode != NFSLAYOUTIOMODE_RW)
6512 *iomode = NFSLAYOUTIOMODE_READ;
6515 * Check to see if a write layout can be issued for this file.
6516 * This is used during mirror recovery to avoid RW layouts being
6517 * issued for a file while it is being copied to the recovered
6520 if (*iomode == NFSLAYOUTIOMODE_RW && nfsrv_dontlayout(&fh) != 0)
6521 return (NFSERR_LAYOUTTRYLATER);
6527 /* First, see if a layout already exists and return if found. */
6528 lhyp = NFSLAYOUTHASH(&fh);
6529 NFSLOCKLAYOUT(lhyp);
6530 error = nfsrv_findlayout(&nd->nd_clientid, &fh, layouttype, p, &lyp);
6531 NFSD_DEBUG(4, "layoutget findlay=%d\n", error);
6533 * Not sure if the seqid must be the same, so I won't check it.
6535 if (error == 0 && (stateidp->other[0] != lyp->lay_stateid.other[0] ||
6536 stateidp->other[1] != lyp->lay_stateid.other[1] ||
6537 stateidp->other[2] != lyp->lay_stateid.other[2])) {
6538 if ((lyp->lay_flags & NFSLAY_CALLB) == 0) {
6539 NFSUNLOCKLAYOUT(lhyp);
6540 NFSD_DEBUG(1, "ret bad stateid\n");
6541 return (NFSERR_BADSTATEID);
6544 * I believe we get here because there is a race between
6545 * the client processing the CBLAYOUTRECALL and the layout
6546 * being deleted here on the server.
6547 * The client has now done a LayoutGet with a non-layout
6548 * stateid, as it would when there is no layout.
6549 * As such, free this layout and set error == NFSERR_BADSTATEID
6550 * so the code below will create a new layout structure as
6551 * would happen if no layout was found.
6552 * "lyp" will be set before being used below, but set it NULL
6555 nfsrv_freelayout(&lhyp->list, lyp);
6557 error = NFSERR_BADSTATEID;
6560 if (lyp->lay_layoutlen > maxcnt) {
6561 NFSUNLOCKLAYOUT(lhyp);
6562 NFSD_DEBUG(1, "ret layout too small\n");
6563 return (NFSERR_TOOSMALL);
6565 if (*iomode == NFSLAYOUTIOMODE_RW)
6566 lyp->lay_flags |= NFSLAY_RW;
6568 lyp->lay_flags |= NFSLAY_READ;
6569 NFSBCOPY(lyp->lay_xdr, layp, lyp->lay_layoutlen);
6570 *layoutlenp = lyp->lay_layoutlen;
6571 if (++lyp->lay_stateid.seqid == 0)
6572 lyp->lay_stateid.seqid = 1;
6573 stateidp->seqid = lyp->lay_stateid.seqid;
6574 NFSUNLOCKLAYOUT(lhyp);
6575 NFSD_DEBUG(4, "ret fnd layout\n");
6578 NFSUNLOCKLAYOUT(lhyp);
6580 /* Find the device id and file handle. */
6581 dsfhp = malloc(sizeof(fhandle_t) * NFSDEV_MAXMIRRORS, M_TEMP, M_WAITOK);
6582 devid = malloc(NFSX_V4DEVICEID * NFSDEV_MAXMIRRORS, M_TEMP, M_WAITOK);
6583 error = nfsrv_dsgetdevandfh(vp, p, &mirrorcnt, dsfhp, devid);
6584 NFSD_DEBUG(4, "layoutget devandfh=%d\n", error);
6586 if (layouttype == NFSLAYOUT_NFSV4_1_FILES) {
6587 if (NFSX_V4FILELAYOUT > maxcnt)
6588 error = NFSERR_TOOSMALL;
6590 lyp = nfsrv_filelayout(nd, *iomode, &fh, dsfhp,
6591 devid, vp->v_mount->mnt_stat.f_fsid);
6593 if (NFSX_V4FLEXLAYOUT(mirrorcnt) > maxcnt)
6594 error = NFSERR_TOOSMALL;
6596 lyp = nfsrv_flexlayout(nd, *iomode, mirrorcnt,
6598 vp->v_mount->mnt_stat.f_fsid);
6601 free(dsfhp, M_TEMP);
6602 free(devid, M_TEMP);
6607 * Now, add this layout to the list.
6609 error = nfsrv_addlayout(nd, &lyp, stateidp, layp, layoutlenp, p);
6610 NFSD_DEBUG(4, "layoutget addl=%d\n", error);
6612 * The lyp will be set to NULL by nfsrv_addlayout() if it
6613 * linked the new structure into the lists.
6615 free(lyp, M_NFSDSTATE);
6620 * Generate a File Layout.
6622 static struct nfslayout *
6623 nfsrv_filelayout(struct nfsrv_descript *nd, int iomode, fhandle_t *fhp,
6624 fhandle_t *dsfhp, char *devid, fsid_t fs)
6627 struct nfslayout *lyp;
6628 uint64_t pattern_offset;
6630 lyp = malloc(sizeof(struct nfslayout) + NFSX_V4FILELAYOUT, M_NFSDSTATE,
6632 lyp->lay_type = NFSLAYOUT_NFSV4_1_FILES;
6633 if (iomode == NFSLAYOUTIOMODE_RW)
6634 lyp->lay_flags = NFSLAY_RW;
6636 lyp->lay_flags = NFSLAY_READ;
6637 NFSBCOPY(fhp, &lyp->lay_fh, sizeof(*fhp));
6638 lyp->lay_clientid.qval = nd->nd_clientid.qval;
6641 /* Fill in the xdr for the files layout. */
6642 tl = (uint32_t *)lyp->lay_xdr;
6643 NFSBCOPY(devid, tl, NFSX_V4DEVICEID); /* Device ID. */
6644 tl += (NFSX_V4DEVICEID / NFSX_UNSIGNED);
6647 * Make the stripe size as many 64K blocks as will fit in the stripe
6648 * mask. Since there is only one stripe, the stripe size doesn't really
6649 * matter, except that the Linux client will only handle an exact
6650 * multiple of their PAGE_SIZE (usually 4K). I chose 64K as a value
6651 * that should cover most/all arches w.r.t. PAGE_SIZE.
6653 *tl++ = txdr_unsigned(NFSFLAYUTIL_STRIPE_MASK & ~0xffff);
6654 *tl++ = 0; /* 1st stripe index. */
6656 txdr_hyper(pattern_offset, tl); tl += 2; /* Pattern offset. */
6657 *tl++ = txdr_unsigned(1); /* 1 file handle. */
6658 *tl++ = txdr_unsigned(NFSX_V4PNFSFH);
6659 NFSBCOPY(dsfhp, tl, sizeof(*dsfhp));
6660 lyp->lay_layoutlen = NFSX_V4FILELAYOUT;
6664 #define FLEX_OWNERID "999"
6665 #define FLEX_UID0 "0"
6667 * Generate a Flex File Layout.
6668 * The FLEX_OWNERID can be any string of 3 decimal digits. Although this
6669 * string goes on the wire, it isn't supposed to be used by the client,
6670 * since this server uses tight coupling.
6671 * Although not recommended by the spec., if vfs.nfsd.flexlinuxhack=1 use
6672 * a string of "0". This works around the Linux Flex File Layout driver bug
6673 * which uses the synthetic uid/gid strings for the "tightly coupled" case.
6675 static struct nfslayout *
6676 nfsrv_flexlayout(struct nfsrv_descript *nd, int iomode, int mirrorcnt,
6677 fhandle_t *fhp, fhandle_t *dsfhp, char *devid, fsid_t fs)
6680 struct nfslayout *lyp;
6684 lyp = malloc(sizeof(struct nfslayout) + NFSX_V4FLEXLAYOUT(mirrorcnt),
6685 M_NFSDSTATE, M_WAITOK | M_ZERO);
6686 lyp->lay_type = NFSLAYOUT_FLEXFILE;
6687 if (iomode == NFSLAYOUTIOMODE_RW)
6688 lyp->lay_flags = NFSLAY_RW;
6690 lyp->lay_flags = NFSLAY_READ;
6691 NFSBCOPY(fhp, &lyp->lay_fh, sizeof(*fhp));
6692 lyp->lay_clientid.qval = nd->nd_clientid.qval;
6694 lyp->lay_mirrorcnt = mirrorcnt;
6696 /* Fill in the xdr for the files layout. */
6697 tl = (uint32_t *)lyp->lay_xdr;
6699 txdr_hyper(lenval, tl); tl += 2; /* Stripe unit. */
6700 *tl++ = txdr_unsigned(mirrorcnt); /* # of mirrors. */
6701 for (i = 0; i < mirrorcnt; i++) {
6702 *tl++ = txdr_unsigned(1); /* One stripe. */
6703 NFSBCOPY(devid, tl, NFSX_V4DEVICEID); /* Device ID. */
6704 tl += (NFSX_V4DEVICEID / NFSX_UNSIGNED);
6705 devid += NFSX_V4DEVICEID;
6706 *tl++ = txdr_unsigned(1); /* Efficiency. */
6707 *tl++ = 0; /* Proxy Stateid. */
6711 *tl++ = txdr_unsigned(1); /* 1 file handle. */
6712 *tl++ = txdr_unsigned(NFSX_V4PNFSFH);
6713 NFSBCOPY(dsfhp, tl, sizeof(*dsfhp));
6714 tl += (NFSM_RNDUP(NFSX_V4PNFSFH) / NFSX_UNSIGNED);
6716 if (nfsrv_flexlinuxhack != 0) {
6717 *tl++ = txdr_unsigned(strlen(FLEX_UID0));
6718 *tl = 0; /* 0 pad string. */
6719 NFSBCOPY(FLEX_UID0, tl++, strlen(FLEX_UID0));
6720 *tl++ = txdr_unsigned(strlen(FLEX_UID0));
6721 *tl = 0; /* 0 pad string. */
6722 NFSBCOPY(FLEX_UID0, tl++, strlen(FLEX_UID0));
6724 *tl++ = txdr_unsigned(strlen(FLEX_OWNERID));
6725 NFSBCOPY(FLEX_OWNERID, tl++, NFSX_UNSIGNED);
6726 *tl++ = txdr_unsigned(strlen(FLEX_OWNERID));
6727 NFSBCOPY(FLEX_OWNERID, tl++, NFSX_UNSIGNED);
6730 *tl++ = txdr_unsigned(0); /* ff_flags. */
6731 *tl = txdr_unsigned(60); /* Status interval hint. */
6732 lyp->lay_layoutlen = NFSX_V4FLEXLAYOUT(mirrorcnt);
6737 * Parse and process Flex File errors returned via LayoutReturn.
6740 nfsrv_flexlayouterr(struct nfsrv_descript *nd, uint32_t *layp, int maxcnt,
6744 int cnt, errcnt, i, j, opnum, stat;
6745 char devid[NFSX_V4DEVICEID];
6748 cnt = fxdr_unsigned(int, *tl++);
6749 NFSD_DEBUG(4, "flexlayouterr cnt=%d\n", cnt);
6750 for (i = 0; i < cnt; i++) {
6751 /* Skip offset, length and stateid for now. */
6752 tl += (4 + NFSX_STATEID / NFSX_UNSIGNED);
6753 errcnt = fxdr_unsigned(int, *tl++);
6754 NFSD_DEBUG(4, "flexlayouterr errcnt=%d\n", errcnt);
6755 for (j = 0; j < errcnt; j++) {
6756 NFSBCOPY(tl, devid, NFSX_V4DEVICEID);
6757 tl += (NFSX_V4DEVICEID / NFSX_UNSIGNED);
6758 stat = fxdr_unsigned(int, *tl++);
6759 opnum = fxdr_unsigned(int, *tl++);
6760 NFSD_DEBUG(4, "flexlayouterr op=%d stat=%d\n", opnum,
6763 * Except for NFSERR_ACCES and NFSERR_STALE errors,
6764 * disable the mirror.
6766 if (stat != NFSERR_ACCES && stat != NFSERR_STALE)
6767 nfsrv_delds(devid, p);
6773 * This function removes all flex file layouts which has a mirror with
6774 * a device id that matches the argument.
6775 * Called when the DS represented by the device id has failed.
6778 nfsrv_flexmirrordel(char *devid, NFSPROC_T *p)
6781 struct nfslayout *lyp, *nlyp;
6782 struct nfslayouthash *lhyp;
6783 struct nfslayouthead loclyp;
6786 NFSD_DEBUG(4, "flexmirrordel\n");
6787 /* Move all layouts found onto a local list. */
6788 TAILQ_INIT(&loclyp);
6789 for (i = 0; i < nfsrv_layouthashsize; i++) {
6790 lhyp = &nfslayouthash[i];
6791 NFSLOCKLAYOUT(lhyp);
6792 TAILQ_FOREACH_SAFE(lyp, &lhyp->list, lay_list, nlyp) {
6793 if (lyp->lay_type == NFSLAYOUT_FLEXFILE &&
6794 lyp->lay_mirrorcnt > 1) {
6795 NFSD_DEBUG(4, "possible match\n");
6798 for (j = 0; j < lyp->lay_mirrorcnt; j++) {
6800 if (NFSBCMP(devid, tl, NFSX_V4DEVICEID)
6803 NFSD_DEBUG(4, "fnd one\n");
6804 TAILQ_REMOVE(&lhyp->list, lyp,
6806 TAILQ_INSERT_HEAD(&loclyp, lyp,
6810 tl += (NFSX_V4DEVICEID / NFSX_UNSIGNED +
6811 NFSM_RNDUP(NFSX_V4PNFSFH) /
6812 NFSX_UNSIGNED + 11 * NFSX_UNSIGNED);
6816 NFSUNLOCKLAYOUT(lhyp);
6819 /* Now, try to do a Layout recall for each one found. */
6820 TAILQ_FOREACH_SAFE(lyp, &loclyp, lay_list, nlyp) {
6821 NFSD_DEBUG(4, "do layout recall\n");
6823 * The layout stateid.seqid needs to be incremented
6824 * before doing a LAYOUT_RECALL callback.
6826 if (++lyp->lay_stateid.seqid == 0)
6827 lyp->lay_stateid.seqid = 1;
6828 nfsrv_recalllayout(lyp->lay_clientid, &lyp->lay_stateid,
6829 &lyp->lay_fh, lyp, 1, lyp->lay_type, p);
6830 nfsrv_freelayout(&loclyp, lyp);
6835 * Do a recall callback to the client for this layout.
6838 nfsrv_recalllayout(nfsquad_t clid, nfsv4stateid_t *stateidp, fhandle_t *fhp,
6839 struct nfslayout *lyp, int changed, int laytype, NFSPROC_T *p)
6841 struct nfsclient *clp;
6844 NFSD_DEBUG(4, "nfsrv_recalllayout\n");
6845 error = nfsrv_getclient(clid, 0, &clp, NULL, (nfsquad_t)((u_quad_t)0),
6847 NFSD_DEBUG(4, "aft nfsrv_getclient=%d\n", error);
6849 printf("nfsrv_recalllayout: getclient err=%d\n", error);
6852 if ((clp->lc_flags & LCL_NFSV41) != 0) {
6853 error = nfsrv_docallback(clp, NFSV4OP_CBLAYOUTRECALL,
6854 stateidp, changed, fhp, NULL, NULL, laytype, p);
6855 /* If lyp != NULL, handle an error return here. */
6856 if (error != 0 && lyp != NULL) {
6859 * Mark it returned, since no layout recall
6861 * All errors seem to be non-recoverable, although
6862 * NFSERR_NOMATCHLAYOUT is a normal event.
6864 if ((lyp->lay_flags & NFSLAY_RECALL) != 0) {
6865 lyp->lay_flags |= NFSLAY_RETURNED;
6869 if (error != NFSERR_NOMATCHLAYOUT)
6870 printf("nfsrv_recalllayout: err=%d\n", error);
6873 printf("nfsrv_recalllayout: clp not NFSv4.1\n");
6878 * Find a layout to recall when we exceed our high water mark.
6881 nfsrv_recalloldlayout(NFSPROC_T *p)
6883 struct nfslayouthash *lhyp;
6884 struct nfslayout *lyp;
6886 nfsv4stateid_t stateid;
6888 int error, laytype, ret;
6890 lhyp = &nfslayouthash[arc4random() % nfsrv_layouthashsize];
6891 NFSLOCKLAYOUT(lhyp);
6892 TAILQ_FOREACH_REVERSE(lyp, &lhyp->list, nfslayouthead, lay_list) {
6893 if ((lyp->lay_flags & NFSLAY_CALLB) == 0) {
6894 lyp->lay_flags |= NFSLAY_CALLB;
6896 * The layout stateid.seqid needs to be incremented
6897 * before doing a LAYOUT_RECALL callback.
6899 if (++lyp->lay_stateid.seqid == 0)
6900 lyp->lay_stateid.seqid = 1;
6901 clientid = lyp->lay_clientid;
6902 stateid = lyp->lay_stateid;
6903 NFSBCOPY(&lyp->lay_fh, &fh, sizeof(fh));
6904 laytype = lyp->lay_type;
6908 NFSUNLOCKLAYOUT(lhyp);
6910 error = nfsrv_recalllayout(clientid, &stateid, &fh, NULL, 0,
6912 if (error != 0 && error != NFSERR_NOMATCHLAYOUT)
6913 NFSD_DEBUG(4, "recallold=%d\n", error);
6915 NFSLOCKLAYOUT(lhyp);
6917 * Since the hash list was unlocked, we need to
6920 ret = nfsrv_findlayout(&clientid, &fh, laytype, p,
6923 (lyp->lay_flags & NFSLAY_CALLB) != 0 &&
6924 lyp->lay_stateid.other[0] == stateid.other[0] &&
6925 lyp->lay_stateid.other[1] == stateid.other[1] &&
6926 lyp->lay_stateid.other[2] == stateid.other[2]) {
6928 * The client no longer knows this layout, so
6929 * it can be free'd now.
6931 if (error == NFSERR_NOMATCHLAYOUT)
6932 nfsrv_freelayout(&lhyp->list, lyp);
6935 * Leave it to be tried later by
6936 * clearing NFSLAY_CALLB and moving
6937 * it to the head of the list, so it
6938 * won't be tried again for a while.
6940 lyp->lay_flags &= ~NFSLAY_CALLB;
6941 TAILQ_REMOVE(&lhyp->list, lyp,
6943 TAILQ_INSERT_HEAD(&lhyp->list, lyp,
6947 NFSUNLOCKLAYOUT(lhyp);
6953 * Try and return layout(s).
6956 nfsrv_layoutreturn(struct nfsrv_descript *nd, vnode_t vp,
6957 int layouttype, int iomode, uint64_t offset, uint64_t len, int reclaim,
6958 int kind, nfsv4stateid_t *stateidp, int maxcnt, uint32_t *layp, int *fndp,
6959 struct ucred *cred, NFSPROC_T *p)
6962 struct nfslayouthash *lhyp;
6963 struct nfslayout *lyp;
6968 if (kind == NFSV4LAYOUTRET_FILE) {
6969 error = nfsvno_getfh(vp, &fh, p);
6971 error = nfsrv_updatemdsattr(vp, &na, p);
6973 printf("nfsrv_layoutreturn: updatemdsattr"
6974 " failed=%d\n", error);
6977 if (reclaim == newnfs_true) {
6978 error = nfsrv_checkgrace(NULL, NULL,
6980 if (error != NFSERR_NOGRACE)
6984 lhyp = NFSLAYOUTHASH(&fh);
6986 NFSLOCKLAYOUT(lhyp);
6987 error = nfsrv_findlayout(&nd->nd_clientid, &fh,
6988 layouttype, p, &lyp);
6989 NFSD_DEBUG(4, "layoutret findlay=%d\n", error);
6991 stateidp->other[0] == lyp->lay_stateid.other[0] &&
6992 stateidp->other[1] == lyp->lay_stateid.other[1] &&
6993 stateidp->other[2] == lyp->lay_stateid.other[2]) {
6994 NFSD_DEBUG(4, "nfsrv_layoutreturn: stateid %d"
6995 " %x %x %x laystateid %d %x %x %x"
6996 " off=%ju len=%ju flgs=0x%x\n",
6997 stateidp->seqid, stateidp->other[0],
6998 stateidp->other[1], stateidp->other[2],
6999 lyp->lay_stateid.seqid,
7000 lyp->lay_stateid.other[0],
7001 lyp->lay_stateid.other[1],
7002 lyp->lay_stateid.other[2],
7003 (uintmax_t)offset, (uintmax_t)len,
7005 if (++lyp->lay_stateid.seqid == 0)
7006 lyp->lay_stateid.seqid = 1;
7007 stateidp->seqid = lyp->lay_stateid.seqid;
7008 if (offset == 0 && len == UINT64_MAX) {
7009 if ((iomode & NFSLAYOUTIOMODE_READ) !=
7011 lyp->lay_flags &= ~NFSLAY_READ;
7012 if ((iomode & NFSLAYOUTIOMODE_RW) != 0)
7013 lyp->lay_flags &= ~NFSLAY_RW;
7014 if ((lyp->lay_flags & (NFSLAY_READ |
7016 nfsrv_freelayout(&lhyp->list,
7023 NFSUNLOCKLAYOUT(lhyp);
7024 /* Search the nfsrv_recalllist for a match. */
7025 TAILQ_FOREACH(lyp, &nfsrv_recalllisthead, lay_list) {
7026 if (NFSBCMP(&lyp->lay_fh, &fh,
7028 lyp->lay_clientid.qval ==
7029 nd->nd_clientid.qval &&
7030 stateidp->other[0] ==
7031 lyp->lay_stateid.other[0] &&
7032 stateidp->other[1] ==
7033 lyp->lay_stateid.other[1] &&
7034 stateidp->other[2] ==
7035 lyp->lay_stateid.other[2]) {
7036 lyp->lay_flags |= NFSLAY_RETURNED;
7043 if (layouttype == NFSLAYOUT_FLEXFILE)
7044 nfsrv_flexlayouterr(nd, layp, maxcnt, p);
7045 } else if (kind == NFSV4LAYOUTRET_FSID)
7046 nfsrv_freelayouts(&nd->nd_clientid,
7047 &vp->v_mount->mnt_stat.f_fsid, layouttype, iomode);
7048 else if (kind == NFSV4LAYOUTRET_ALL)
7049 nfsrv_freelayouts(&nd->nd_clientid, NULL, layouttype, iomode);
7051 error = NFSERR_INVAL;
7058 * Look for an existing layout.
7061 nfsrv_findlayout(nfsquad_t *clientidp, fhandle_t *fhp, int laytype,
7062 NFSPROC_T *p, struct nfslayout **lypp)
7064 struct nfslayouthash *lhyp;
7065 struct nfslayout *lyp;
7070 lhyp = NFSLAYOUTHASH(fhp);
7071 TAILQ_FOREACH(lyp, &lhyp->list, lay_list) {
7072 if (NFSBCMP(&lyp->lay_fh, fhp, sizeof(*fhp)) == 0 &&
7073 lyp->lay_clientid.qval == clientidp->qval &&
7074 lyp->lay_type == laytype)
7085 * Add the new layout, as required.
7088 nfsrv_addlayout(struct nfsrv_descript *nd, struct nfslayout **lypp,
7089 nfsv4stateid_t *stateidp, char *layp, int *layoutlenp, NFSPROC_T *p)
7091 struct nfsclient *clp;
7092 struct nfslayouthash *lhyp;
7093 struct nfslayout *lyp, *nlyp;
7097 KASSERT((nd->nd_flag & ND_IMPLIEDCLID) != 0,
7098 ("nfsrv_layoutget: no nd_clientid\n"));
7102 error = nfsrv_getclient((nfsquad_t)((u_quad_t)0), CLOPS_RENEW, &clp,
7103 NULL, (nfsquad_t)((u_quad_t)0), 0, nd, p);
7108 lyp->lay_stateid.seqid = stateidp->seqid = 1;
7109 lyp->lay_stateid.other[0] = stateidp->other[0] =
7110 clp->lc_clientid.lval[0];
7111 lyp->lay_stateid.other[1] = stateidp->other[1] =
7112 clp->lc_clientid.lval[1];
7113 lyp->lay_stateid.other[2] = stateidp->other[2] =
7114 nfsrv_nextstateindex(clp);
7117 lhyp = NFSLAYOUTHASH(fhp);
7118 NFSLOCKLAYOUT(lhyp);
7119 TAILQ_FOREACH(nlyp, &lhyp->list, lay_list) {
7120 if (NFSBCMP(&nlyp->lay_fh, fhp, sizeof(*fhp)) == 0 &&
7121 nlyp->lay_clientid.qval == nd->nd_clientid.qval)
7125 /* A layout already exists, so use it. */
7126 nlyp->lay_flags |= (lyp->lay_flags & (NFSLAY_READ | NFSLAY_RW));
7127 NFSBCOPY(nlyp->lay_xdr, layp, nlyp->lay_layoutlen);
7128 *layoutlenp = nlyp->lay_layoutlen;
7129 if (++nlyp->lay_stateid.seqid == 0)
7130 nlyp->lay_stateid.seqid = 1;
7131 stateidp->seqid = nlyp->lay_stateid.seqid;
7132 stateidp->other[0] = nlyp->lay_stateid.other[0];
7133 stateidp->other[1] = nlyp->lay_stateid.other[1];
7134 stateidp->other[2] = nlyp->lay_stateid.other[2];
7135 NFSUNLOCKLAYOUT(lhyp);
7139 /* Insert the new layout in the lists. */
7141 atomic_add_int(&nfsrv_layoutcnt, 1);
7142 NFSBCOPY(lyp->lay_xdr, layp, lyp->lay_layoutlen);
7143 *layoutlenp = lyp->lay_layoutlen;
7144 TAILQ_INSERT_HEAD(&lhyp->list, lyp, lay_list);
7145 NFSUNLOCKLAYOUT(lhyp);
7150 * Get the devinfo for a deviceid.
7153 nfsrv_getdevinfo(char *devid, int layouttype, uint32_t *maxcnt,
7154 uint32_t *notify, int *devaddrlen, char **devaddr)
7156 struct nfsdevice *ds;
7158 if ((layouttype != NFSLAYOUT_NFSV4_1_FILES && layouttype !=
7159 NFSLAYOUT_FLEXFILE) ||
7160 (nfsrv_maxpnfsmirror > 1 && layouttype == NFSLAYOUT_NFSV4_1_FILES))
7161 return (NFSERR_UNKNLAYOUTTYPE);
7164 * Now, search for the device id. Note that the structures won't go
7165 * away, but the order changes in the list. As such, the lock only
7166 * needs to be held during the search through the list.
7169 TAILQ_FOREACH(ds, &nfsrv_devidhead, nfsdev_list) {
7170 if (NFSBCMP(devid, ds->nfsdev_deviceid, NFSX_V4DEVICEID) == 0 &&
7171 ds->nfsdev_nmp != NULL)
7176 return (NFSERR_NOENT);
7178 /* If the correct nfsdev_XXXXaddrlen is > 0, we have the device info. */
7180 if (layouttype == NFSLAYOUT_NFSV4_1_FILES) {
7181 *devaddrlen = ds->nfsdev_fileaddrlen;
7182 *devaddr = ds->nfsdev_fileaddr;
7183 } else if (layouttype == NFSLAYOUT_FLEXFILE) {
7184 *devaddrlen = ds->nfsdev_flexaddrlen;
7185 *devaddr = ds->nfsdev_flexaddr;
7187 if (*devaddrlen == 0)
7188 return (NFSERR_UNKNLAYOUTTYPE);
7191 * The XDR overhead is 3 unsigned values: layout_type,
7192 * length_of_address and notify bitmap.
7193 * If the notify array is changed to not all zeros, the
7194 * count of unsigned values must be increased.
7196 if (*maxcnt > 0 && *maxcnt < NFSM_RNDUP(*devaddrlen) +
7197 3 * NFSX_UNSIGNED) {
7198 *maxcnt = NFSM_RNDUP(*devaddrlen) + 3 * NFSX_UNSIGNED;
7199 return (NFSERR_TOOSMALL);
7205 * Free a list of layout state structures.
7208 nfsrv_freelayoutlist(nfsquad_t clientid)
7210 struct nfslayouthash *lhyp;
7211 struct nfslayout *lyp, *nlyp;
7214 for (i = 0; i < nfsrv_layouthashsize; i++) {
7215 lhyp = &nfslayouthash[i];
7216 NFSLOCKLAYOUT(lhyp);
7217 TAILQ_FOREACH_SAFE(lyp, &lhyp->list, lay_list, nlyp) {
7218 if (lyp->lay_clientid.qval == clientid.qval)
7219 nfsrv_freelayout(&lhyp->list, lyp);
7221 NFSUNLOCKLAYOUT(lhyp);
7229 nfsrv_freelayout(struct nfslayouthead *lhp, struct nfslayout *lyp)
7232 NFSD_DEBUG(4, "Freelayout=%p\n", lyp);
7233 atomic_add_int(&nfsrv_layoutcnt, -1);
7234 TAILQ_REMOVE(lhp, lyp, lay_list);
7235 free(lyp, M_NFSDSTATE);
7239 * Free up a device id.
7242 nfsrv_freeonedevid(struct nfsdevice *ds)
7246 atomic_add_int(&nfsrv_devidcnt, -1);
7247 vrele(ds->nfsdev_dvp);
7248 for (i = 0; i < nfsrv_dsdirsize; i++)
7249 if (ds->nfsdev_dsdir[i] != NULL)
7250 vrele(ds->nfsdev_dsdir[i]);
7251 free(ds->nfsdev_fileaddr, M_NFSDSTATE);
7252 free(ds->nfsdev_flexaddr, M_NFSDSTATE);
7253 free(ds->nfsdev_host, M_NFSDSTATE);
7254 free(ds, M_NFSDSTATE);
7258 * Free up a device id and its mirrors.
7261 nfsrv_freedevid(struct nfsdevice *ds)
7264 TAILQ_REMOVE(&nfsrv_devidhead, ds, nfsdev_list);
7265 nfsrv_freeonedevid(ds);
7269 * Free all layouts and device ids.
7270 * Done when the nfsd threads are shut down since there may be a new
7271 * modified device id list created when the nfsd is restarted.
7274 nfsrv_freealllayoutsanddevids(void)
7276 struct nfsdontlist *mrp, *nmrp;
7277 struct nfslayout *lyp, *nlyp;
7279 /* Get rid of the deviceid structures. */
7280 nfsrv_freealldevids();
7281 TAILQ_INIT(&nfsrv_devidhead);
7284 /* Get rid of all layouts. */
7285 nfsrv_freealllayouts();
7287 /* Get rid of any nfsdontlist entries. */
7288 LIST_FOREACH_SAFE(mrp, &nfsrv_dontlisthead, nfsmr_list, nmrp)
7289 free(mrp, M_NFSDSTATE);
7290 LIST_INIT(&nfsrv_dontlisthead);
7291 nfsrv_dontlistlen = 0;
7293 /* Free layouts in the recall list. */
7294 TAILQ_FOREACH_SAFE(lyp, &nfsrv_recalllisthead, lay_list, nlyp)
7295 nfsrv_freelayout(&nfsrv_recalllisthead, lyp);
7296 TAILQ_INIT(&nfsrv_recalllisthead);
7300 * Free layouts that match the arguments.
7303 nfsrv_freelayouts(nfsquad_t *clid, fsid_t *fs, int laytype, int iomode)
7305 struct nfslayouthash *lhyp;
7306 struct nfslayout *lyp, *nlyp;
7309 for (i = 0; i < nfsrv_layouthashsize; i++) {
7310 lhyp = &nfslayouthash[i];
7311 NFSLOCKLAYOUT(lhyp);
7312 TAILQ_FOREACH_SAFE(lyp, &lhyp->list, lay_list, nlyp) {
7313 if (clid->qval != lyp->lay_clientid.qval)
7315 if (fs != NULL && (fs->val[0] != lyp->lay_fsid.val[0] ||
7316 fs->val[1] != lyp->lay_fsid.val[1]))
7318 if (laytype != lyp->lay_type)
7320 if ((iomode & NFSLAYOUTIOMODE_READ) != 0)
7321 lyp->lay_flags &= ~NFSLAY_READ;
7322 if ((iomode & NFSLAYOUTIOMODE_RW) != 0)
7323 lyp->lay_flags &= ~NFSLAY_RW;
7324 if ((lyp->lay_flags & (NFSLAY_READ | NFSLAY_RW)) == 0)
7325 nfsrv_freelayout(&lhyp->list, lyp);
7327 NFSUNLOCKLAYOUT(lhyp);
7332 * Free all layouts for the argument file.
7335 nfsrv_freefilelayouts(fhandle_t *fhp)
7337 struct nfslayouthash *lhyp;
7338 struct nfslayout *lyp, *nlyp;
7340 lhyp = NFSLAYOUTHASH(fhp);
7341 NFSLOCKLAYOUT(lhyp);
7342 TAILQ_FOREACH_SAFE(lyp, &lhyp->list, lay_list, nlyp) {
7343 if (NFSBCMP(&lyp->lay_fh, fhp, sizeof(*fhp)) == 0)
7344 nfsrv_freelayout(&lhyp->list, lyp);
7346 NFSUNLOCKLAYOUT(lhyp);
7353 nfsrv_freealllayouts(void)
7355 struct nfslayouthash *lhyp;
7356 struct nfslayout *lyp, *nlyp;
7359 for (i = 0; i < nfsrv_layouthashsize; i++) {
7360 lhyp = &nfslayouthash[i];
7361 NFSLOCKLAYOUT(lhyp);
7362 TAILQ_FOREACH_SAFE(lyp, &lhyp->list, lay_list, nlyp)
7363 nfsrv_freelayout(&lhyp->list, lyp);
7364 NFSUNLOCKLAYOUT(lhyp);
7369 * Look up the mount path for the DS server.
7372 nfsrv_setdsserver(char *dspathp, char *mdspathp, NFSPROC_T *p,
7373 struct nfsdevice **dsp)
7375 struct nameidata nd;
7376 struct nfsdevice *ds;
7382 NFSD_DEBUG(4, "setdssrv path=%s\n", dspathp);
7384 NDINIT(&nd, LOOKUP, FOLLOW | LOCKSHARED | LOCKLEAF, UIO_SYSSPACE,
7387 NFSD_DEBUG(4, "lookup=%d\n", error);
7390 if (nd.ni_vp->v_type != VDIR) {
7392 NFSD_DEBUG(4, "dspath not dir\n");
7395 if (strcmp(nd.ni_vp->v_mount->mnt_vfc->vfc_name, "nfs") != 0) {
7397 NFSD_DEBUG(4, "dspath not an NFS mount\n");
7402 * Allocate a DS server structure with the NFS mounted directory
7403 * vnode reference counted, so that a non-forced dismount will
7405 * This structure is always linked into the list, even if an error
7406 * is being returned. The caller will free the entire list upon
7409 *dsp = ds = malloc(sizeof(*ds) + nfsrv_dsdirsize * sizeof(vnode_t),
7410 M_NFSDSTATE, M_WAITOK | M_ZERO);
7411 ds->nfsdev_dvp = nd.ni_vp;
7412 ds->nfsdev_nmp = VFSTONFS(nd.ni_vp->v_mount);
7413 NFSVOPUNLOCK(nd.ni_vp, 0);
7415 dsdirsize = strlen(dspathp) + 16;
7416 dsdirpath = malloc(dsdirsize, M_TEMP, M_WAITOK);
7417 /* Now, create the DS directory structures. */
7418 for (i = 0; i < nfsrv_dsdirsize; i++) {
7419 snprintf(dsdirpath, dsdirsize, "%s/ds%d", dspathp, i);
7420 NDINIT(&nd, LOOKUP, FOLLOW | LOCKSHARED | LOCKLEAF,
7421 UIO_SYSSPACE, dsdirpath, p);
7423 NFSD_DEBUG(4, "dsdirpath=%s lookup=%d\n", dsdirpath, error);
7426 if (nd.ni_vp->v_type != VDIR) {
7429 NFSD_DEBUG(4, "dsdirpath not a VDIR\n");
7432 if (strcmp(nd.ni_vp->v_mount->mnt_vfc->vfc_name, "nfs") != 0) {
7435 NFSD_DEBUG(4, "dsdirpath not an NFS mount\n");
7438 ds->nfsdev_dsdir[i] = nd.ni_vp;
7439 NFSVOPUNLOCK(nd.ni_vp, 0);
7441 free(dsdirpath, M_TEMP);
7443 if (strlen(mdspathp) > 0) {
7445 * This DS stores file for a specific MDS exported file
7448 NDINIT(&nd, LOOKUP, FOLLOW | LOCKSHARED | LOCKLEAF,
7449 UIO_SYSSPACE, mdspathp, p);
7451 NFSD_DEBUG(4, "mds lookup=%d\n", error);
7454 if (nd.ni_vp->v_type != VDIR) {
7457 NFSD_DEBUG(4, "mdspath not dir\n");
7460 mp = nd.ni_vp->v_mount;
7461 if ((mp->mnt_flag & MNT_EXPORTED) == 0) {
7464 NFSD_DEBUG(4, "mdspath not an exported fs\n");
7467 ds->nfsdev_mdsfsid = mp->mnt_stat.f_fsid;
7468 ds->nfsdev_mdsisset = 1;
7473 TAILQ_INSERT_TAIL(&nfsrv_devidhead, ds, nfsdev_list);
7474 atomic_add_int(&nfsrv_devidcnt, 1);
7479 * Look up the mount path for the DS server and delete it.
7482 nfsrv_deldsserver(int op, char *dspathp, NFSPROC_T *p)
7485 struct nfsmount *nmp;
7486 struct nfsdevice *ds;
7489 NFSD_DEBUG(4, "deldssrv path=%s\n", dspathp);
7491 * Search for the path in the mount list. Avoid looking the path
7492 * up, since this mount point may be hung, with associated locked
7494 * Set NFSMNTP_CANCELRPCS so that any forced dismount will be blocked
7495 * until this completes.
7496 * As noted in the man page, this should be done before any forced
7497 * dismount on the mount point, but at least the handshake on
7498 * NFSMNTP_CANCELRPCS should make it safe.
7503 mtx_lock(&mountlist_mtx);
7504 TAILQ_FOREACH(mp, &mountlist, mnt_list) {
7505 if (strcmp(mp->mnt_stat.f_mntonname, dspathp) == 0 &&
7506 strcmp(mp->mnt_stat.f_fstypename, "nfs") == 0 &&
7507 mp->mnt_data != NULL) {
7510 if ((nmp->nm_privflag & (NFSMNTP_FORCEDISM |
7511 NFSMNTP_CANCELRPCS)) == 0) {
7512 nmp->nm_privflag |= NFSMNTP_CANCELRPCS;
7521 mtx_unlock(&mountlist_mtx);
7524 ds = nfsrv_deldsnmp(op, nmp, p);
7525 NFSD_DEBUG(4, "deldsnmp=%p\n", ds);
7527 nfsrv_killrpcs(nmp);
7528 NFSD_DEBUG(4, "aft killrpcs\n");
7532 nmp->nm_privflag &= ~NFSMNTP_CANCELRPCS;
7541 * Search for and remove a DS entry which matches the "nmp" argument.
7542 * The nfsdevice structure pointer is returned so that the caller can
7543 * free it via nfsrv_freeonedevid().
7544 * For the forced case, do not try to do LayoutRecalls, since the server
7545 * must be shut down now anyhow.
7548 nfsrv_deldsnmp(int op, struct nfsmount *nmp, NFSPROC_T *p)
7550 struct nfsdevice *fndds;
7552 NFSD_DEBUG(4, "deldsdvp\n");
7554 if (op == PNFSDOP_FORCEDELDS)
7555 fndds = nfsv4_findmirror(nmp);
7557 fndds = nfsrv_findmirroredds(nmp);
7559 nfsrv_deleteds(fndds);
7561 if (fndds != NULL) {
7562 if (op != PNFSDOP_FORCEDELDS)
7563 nfsrv_flexmirrordel(fndds->nfsdev_deviceid, p);
7564 printf("pNFS server: mirror %s failed\n", fndds->nfsdev_host);
7570 * Similar to nfsrv_deldsnmp(), except that the DS is indicated by deviceid.
7571 * This function also calls nfsrv_killrpcs() to unblock RPCs on the mount
7573 * Also, returns an error instead of the nfsdevice found.
7576 nfsrv_delds(char *devid, NFSPROC_T *p)
7578 struct nfsdevice *ds, *fndds;
7579 struct nfsmount *nmp;
7582 NFSD_DEBUG(4, "delds\n");
7584 * Search the DS server list for a match with devid.
7585 * Remove the DS entry if found and there is a mirror.
7591 TAILQ_FOREACH(ds, &nfsrv_devidhead, nfsdev_list) {
7592 if (NFSBCMP(ds->nfsdev_deviceid, devid, NFSX_V4DEVICEID) == 0 &&
7593 ds->nfsdev_nmp != NULL) {
7594 NFSD_DEBUG(4, "fnd main ds\n");
7599 if (fndds == NULL) {
7603 if (fndds->nfsdev_mdsisset == 0 && nfsrv_faildscnt > 0)
7605 else if (fndds->nfsdev_mdsisset != 0) {
7606 /* For the fsid is set case, search for a mirror. */
7607 TAILQ_FOREACH(ds, &nfsrv_devidhead, nfsdev_list) {
7608 if (ds != fndds && ds->nfsdev_nmp != NULL &&
7609 ds->nfsdev_mdsisset != 0 &&
7610 ds->nfsdev_mdsfsid.val[0] ==
7611 fndds->nfsdev_mdsfsid.val[0] &&
7612 ds->nfsdev_mdsfsid.val[1] ==
7613 fndds->nfsdev_mdsfsid.val[1]) {
7619 if (fndmirror != 0) {
7620 nmp = fndds->nfsdev_nmp;
7622 if ((nmp->nm_privflag & (NFSMNTP_FORCEDISM |
7623 NFSMNTP_CANCELRPCS)) == 0) {
7624 nmp->nm_privflag |= NFSMNTP_CANCELRPCS;
7626 nfsrv_deleteds(fndds);
7634 nfsrv_flexmirrordel(fndds->nfsdev_deviceid, p);
7635 printf("pNFS server: mirror %s failed\n", fndds->nfsdev_host);
7636 nfsrv_killrpcs(nmp);
7638 nmp->nm_privflag &= ~NFSMNTP_CANCELRPCS;
7647 * Mark a DS as disabled by setting nfsdev_nmp = NULL.
7650 nfsrv_deleteds(struct nfsdevice *fndds)
7653 NFSD_DEBUG(4, "deleteds: deleting a mirror\n");
7654 fndds->nfsdev_nmp = NULL;
7655 if (fndds->nfsdev_mdsisset == 0)
7660 * Fill in the addr structures for the File and Flex File layouts.
7663 nfsrv_allocdevid(struct nfsdevice *ds, char *addr, char *dnshost)
7668 static uint64_t new_devid = 0;
7670 if (strchr(addr, ':') != NULL)
7675 /* Fill in the device id. */
7676 NFSBCOPY(&nfsdev_time, ds->nfsdev_deviceid, sizeof(nfsdev_time));
7678 NFSBCOPY(&new_devid, &ds->nfsdev_deviceid[sizeof(nfsdev_time)],
7682 * Fill in the file addr (actually the nfsv4_file_layout_ds_addr4
7683 * as defined in RFC5661) in XDR.
7685 addrlen = NFSM_RNDUP(strlen(addr)) + NFSM_RNDUP(strlen(netprot)) +
7687 NFSD_DEBUG(4, "hn=%s addr=%s netprot=%s\n", dnshost, addr, netprot);
7688 ds->nfsdev_fileaddrlen = addrlen;
7689 tl = malloc(addrlen, M_NFSDSTATE, M_WAITOK | M_ZERO);
7690 ds->nfsdev_fileaddr = (char *)tl;
7691 *tl++ = txdr_unsigned(1); /* One stripe with index 0. */
7693 *tl++ = txdr_unsigned(1); /* One multipath list */
7694 *tl++ = txdr_unsigned(1); /* with one entry in it. */
7695 /* The netaddr for this one entry. */
7696 *tl++ = txdr_unsigned(strlen(netprot));
7697 NFSBCOPY(netprot, tl, strlen(netprot));
7698 tl += (NFSM_RNDUP(strlen(netprot)) / NFSX_UNSIGNED);
7699 *tl++ = txdr_unsigned(strlen(addr));
7700 NFSBCOPY(addr, tl, strlen(addr));
7703 * Fill in the flex file addr (actually the ff_device_addr4
7704 * as defined for Flexible File Layout) in XDR.
7706 addrlen = NFSM_RNDUP(strlen(addr)) + NFSM_RNDUP(strlen(netprot)) +
7708 ds->nfsdev_flexaddrlen = addrlen;
7709 tl = malloc(addrlen, M_NFSDSTATE, M_WAITOK | M_ZERO);
7710 ds->nfsdev_flexaddr = (char *)tl;
7711 *tl++ = txdr_unsigned(1); /* One multipath entry. */
7712 /* The netaddr for this one entry. */
7713 *tl++ = txdr_unsigned(strlen(netprot));
7714 NFSBCOPY(netprot, tl, strlen(netprot));
7715 tl += (NFSM_RNDUP(strlen(netprot)) / NFSX_UNSIGNED);
7716 *tl++ = txdr_unsigned(strlen(addr));
7717 NFSBCOPY(addr, tl, strlen(addr));
7718 tl += (NFSM_RNDUP(strlen(addr)) / NFSX_UNSIGNED);
7719 *tl++ = txdr_unsigned(1); /* One NFS Version. */
7720 *tl++ = txdr_unsigned(NFS_VER4); /* NFSv4. */
7721 *tl++ = txdr_unsigned(NFSV41_MINORVERSION); /* Minor version 1. */
7722 *tl++ = txdr_unsigned(NFS_SRVMAXIO); /* DS max rsize. */
7723 *tl++ = txdr_unsigned(NFS_SRVMAXIO); /* DS max wsize. */
7724 *tl = newnfs_true; /* Tightly coupled. */
7726 ds->nfsdev_hostnamelen = strlen(dnshost);
7727 ds->nfsdev_host = malloc(ds->nfsdev_hostnamelen + 1, M_NFSDSTATE,
7729 NFSBCOPY(dnshost, ds->nfsdev_host, ds->nfsdev_hostnamelen + 1);
7734 * Create the device id list.
7735 * Return 0 if the nfsd threads are to run and ENXIO if the "-p" argument
7739 nfsrv_createdevids(struct nfsd_nfsd_args *args, NFSPROC_T *p)
7741 struct nfsdevice *ds;
7742 char *addrp, *dnshostp, *dspathp, *mdspathp;
7746 dnshostp = args->dnshost;
7747 dspathp = args->dspath;
7748 mdspathp = args->mdspath;
7749 nfsrv_maxpnfsmirror = args->mirrorcnt;
7750 if (addrp == NULL || dnshostp == NULL || dspathp == NULL ||
7755 * Loop around for each nul-terminated string in args->addr,
7756 * args->dnshost, args->dnspath and args->mdspath.
7758 while (addrp < (args->addr + args->addrlen) &&
7759 dnshostp < (args->dnshost + args->dnshostlen) &&
7760 dspathp < (args->dspath + args->dspathlen) &&
7761 mdspathp < (args->mdspath + args->mdspathlen)) {
7762 error = nfsrv_setdsserver(dspathp, mdspathp, p, &ds);
7764 /* Free all DS servers. */
7765 nfsrv_freealldevids();
7769 nfsrv_allocdevid(ds, addrp, dnshostp);
7770 addrp += (strlen(addrp) + 1);
7771 dnshostp += (strlen(dnshostp) + 1);
7772 dspathp += (strlen(dspathp) + 1);
7773 mdspathp += (strlen(mdspathp) + 1);
7775 if (nfsrv_devidcnt < nfsrv_maxpnfsmirror) {
7776 /* Free all DS servers. */
7777 nfsrv_freealldevids();
7779 nfsrv_maxpnfsmirror = 1;
7782 /* We can fail at most one less DS than the mirror level. */
7783 nfsrv_faildscnt = nfsrv_maxpnfsmirror - 1;
7786 * Allocate the nfslayout hash table now, since this is a pNFS server.
7787 * Make it 1% of the high water mark and at least 100.
7789 if (nfslayouthash == NULL) {
7790 nfsrv_layouthashsize = nfsrv_layouthighwater / 100;
7791 if (nfsrv_layouthashsize < 100)
7792 nfsrv_layouthashsize = 100;
7793 nfslayouthash = mallocarray(nfsrv_layouthashsize,
7794 sizeof(struct nfslayouthash), M_NFSDSESSION, M_WAITOK |
7796 for (i = 0; i < nfsrv_layouthashsize; i++) {
7797 mtx_init(&nfslayouthash[i].mtx, "nfslm", NULL, MTX_DEF);
7798 TAILQ_INIT(&nfslayouthash[i].list);
7805 * Free all device ids.
7808 nfsrv_freealldevids(void)
7810 struct nfsdevice *ds, *nds;
7812 TAILQ_FOREACH_SAFE(ds, &nfsrv_devidhead, nfsdev_list, nds)
7813 nfsrv_freedevid(ds);
7817 * Check to see if there is a Read/Write Layout plus either:
7818 * - A Write Delegation
7820 * - An Open with Write_access.
7821 * Return 1 if this is the case and 0 otherwise.
7822 * This function is used by nfsrv_proxyds() to decide if doing a Proxy
7823 * Getattr RPC to the Data Server (DS) is necessary.
7825 #define NFSCLIDVECSIZE 6
7827 nfsrv_checkdsattr(struct nfsrv_descript *nd, vnode_t vp, NFSPROC_T *p)
7829 fhandle_t fh, *tfhp;
7830 struct nfsstate *stp;
7831 struct nfslayout *lyp;
7832 struct nfslayouthash *lhyp;
7833 struct nfslockhashhead *hp;
7834 struct nfslockfile *lfp;
7835 nfsquad_t clid[NFSCLIDVECSIZE];
7838 ret = nfsvno_getfh(vp, &fh, p);
7842 /* First check for a Read/Write Layout. */
7844 lhyp = NFSLAYOUTHASH(&fh);
7845 NFSLOCKLAYOUT(lhyp);
7846 TAILQ_FOREACH(lyp, &lhyp->list, lay_list) {
7847 if (NFSBCMP(&lyp->lay_fh, &fh, sizeof(fh)) == 0 &&
7848 ((lyp->lay_flags & NFSLAY_RW) != 0 ||
7849 ((lyp->lay_flags & NFSLAY_READ) != 0 &&
7850 nfsrv_pnfsatime != 0))) {
7851 if (clidcnt < NFSCLIDVECSIZE)
7852 clid[clidcnt].qval = lyp->lay_clientid.qval;
7856 NFSUNLOCKLAYOUT(lhyp);
7858 /* None found, so return 0. */
7862 /* Get the nfslockfile for this fh. */
7864 hp = NFSLOCKHASH(&fh);
7865 LIST_FOREACH(lfp, hp, lf_hash) {
7867 if (NFSVNO_CMPFH(&fh, tfhp))
7871 /* None found, so return 0. */
7876 /* Now, look for a Write delegation for this clientid. */
7877 LIST_FOREACH(stp, &lfp->lf_deleg, ls_file) {
7878 if ((stp->ls_flags & NFSLCK_DELEGWRITE) != 0 &&
7879 nfsrv_fndclid(clid, stp->ls_clp->lc_clientid, clidcnt) != 0)
7883 /* Found one, so return 1. */
7888 /* No Write delegation, so look for an Open with Write_access. */
7889 LIST_FOREACH(stp, &lfp->lf_open, ls_file) {
7890 KASSERT((stp->ls_flags & NFSLCK_OPEN) != 0,
7891 ("nfsrv_checkdsattr: Non-open in Open list\n"));
7892 if ((stp->ls_flags & NFSLCK_WRITEACCESS) != 0 &&
7893 nfsrv_fndclid(clid, stp->ls_clp->lc_clientid, clidcnt) != 0)
7903 * Look for a matching clientid in the vector. Return 1 if one might match.
7906 nfsrv_fndclid(nfsquad_t *clidvec, nfsquad_t clid, int clidcnt)
7910 /* If too many for the vector, return 1 since there might be a match. */
7911 if (clidcnt > NFSCLIDVECSIZE)
7914 for (i = 0; i < clidcnt; i++)
7915 if (clidvec[i].qval == clid.qval)
7921 * Check the don't list for "vp" and see if issuing an rw layout is allowed.
7922 * Return 1 if issuing an rw layout isn't allowed, 0 otherwise.
7925 nfsrv_dontlayout(fhandle_t *fhp)
7927 struct nfsdontlist *mrp;
7930 if (nfsrv_dontlistlen == 0)
7934 LIST_FOREACH(mrp, &nfsrv_dontlisthead, nfsmr_list) {
7935 if (NFSBCMP(fhp, &mrp->nfsmr_fh, sizeof(*fhp)) == 0 &&
7936 (mrp->nfsmr_flags & NFSMR_DONTLAYOUT) != 0) {
7941 NFSDDONTLISTUNLOCK();
7945 #define PNFSDS_COPYSIZ 65536
7947 * Create a new file on a DS and copy the contents of an extant DS file to it.
7948 * This can be used for recovery of a DS file onto a recovered DS.
7950 * - When called, the MDS file's vnode is locked, blocking LayoutGet operations.
7951 * - Disable issuing of read/write layouts for the file via the nfsdontlist,
7952 * so that they will be disabled after the MDS file's vnode is unlocked.
7953 * - Set up the nfsrv_recalllist so that recall of read/write layouts can
7955 * - Unlock the MDS file's vnode, so that the client(s) can perform proxied
7956 * writes, LayoutCommits and LayoutReturns for the file when completing the
7957 * LayoutReturn requested by the LayoutRecall callback.
7958 * - Issue a LayoutRecall callback for all read/write layouts and wait for
7959 * them to be returned. (If the LayoutRecall callback replies
7960 * NFSERR_NOMATCHLAYOUT, they are gone and no LayoutReturn is needed.)
7961 * - Exclusively lock the MDS file's vnode. This ensures that no proxied
7962 * writes are in progress or can occur during the DS file copy.
7963 * It also blocks Setattr operations.
7964 * - Create the file on the recovered mirror.
7965 * - Copy the file from the operational DS.
7966 * - Copy any ACL from the MDS file to the new DS file.
7967 * - Set the modify time of the new DS file to that of the MDS file.
7968 * - Update the extended attribute for the MDS file.
7969 * - Enable issuing of rw layouts by deleting the nfsdontlist entry.
7970 * - The caller will unlock the MDS file's vnode allowing operations
7971 * to continue normally, since it is now on the mirror again.
7974 nfsrv_copymr(vnode_t vp, vnode_t fvp, vnode_t dvp, struct nfsdevice *ds,
7975 struct pnfsdsfile *pf, struct pnfsdsfile *wpf, int mirrorcnt,
7976 struct ucred *cred, NFSPROC_T *p)
7978 struct nfsdontlist *mrp, *nmrp;
7979 struct nfslayouthash *lhyp;
7980 struct nfslayout *lyp, *nlyp;
7981 struct nfslayouthead thl;
7982 struct mount *mp, *tvmp;
7985 struct timespec mtime;
7991 int didprintf, ret, retacl, xfer;
7993 ASSERT_VOP_LOCKED(fvp, "nfsrv_copymr fvp");
7994 ASSERT_VOP_LOCKED(vp, "nfsrv_copymr vp");
7996 * Allocate a nfsdontlist entry and set the NFSMR_DONTLAYOUT flag
7997 * so that no more RW layouts will get issued.
7999 ret = nfsvno_getfh(vp, &fh, p);
8001 NFSD_DEBUG(4, "nfsrv_copymr: getfh=%d\n", ret);
8004 nmrp = malloc(sizeof(*nmrp), M_NFSDSTATE, M_WAITOK);
8005 nmrp->nfsmr_flags = NFSMR_DONTLAYOUT;
8006 NFSBCOPY(&fh, &nmrp->nfsmr_fh, sizeof(fh));
8008 LIST_FOREACH(mrp, &nfsrv_dontlisthead, nfsmr_list) {
8009 if (NFSBCMP(&fh, &mrp->nfsmr_fh, sizeof(fh)) == 0)
8013 LIST_INSERT_HEAD(&nfsrv_dontlisthead, nmrp, nfsmr_list);
8016 nfsrv_dontlistlen++;
8017 NFSD_DEBUG(4, "nfsrv_copymr: in dontlist\n");
8019 NFSDDONTLISTUNLOCK();
8020 free(nmrp, M_NFSDSTATE);
8021 NFSD_DEBUG(4, "nfsrv_copymr: dup dontlist\n");
8024 NFSDDONTLISTUNLOCK();
8027 * Search for all RW layouts for this file. Move them to the
8028 * recall list, so they can be recalled and their return noted.
8030 lhyp = NFSLAYOUTHASH(&fh);
8032 NFSLOCKLAYOUT(lhyp);
8033 TAILQ_FOREACH_SAFE(lyp, &lhyp->list, lay_list, nlyp) {
8034 if (NFSBCMP(&lyp->lay_fh, &fh, sizeof(fh)) == 0 &&
8035 (lyp->lay_flags & NFSLAY_RW) != 0) {
8036 TAILQ_REMOVE(&lhyp->list, lyp, lay_list);
8037 TAILQ_INSERT_HEAD(&nfsrv_recalllisthead, lyp, lay_list);
8038 lyp->lay_trycnt = 0;
8041 NFSUNLOCKLAYOUT(lhyp);
8048 /* Unlock the MDS vp, so that a LayoutReturn can be done on it. */
8049 NFSVOPUNLOCK(vp, 0);
8050 /* Now, do a recall for all layouts not yet recalled. */
8053 TAILQ_FOREACH(lyp, &nfsrv_recalllisthead, lay_list) {
8054 if (NFSBCMP(&lyp->lay_fh, &fh, sizeof(fh)) == 0 &&
8055 (lyp->lay_flags & NFSLAY_RECALL) == 0) {
8056 lyp->lay_flags |= NFSLAY_RECALL;
8058 * The layout stateid.seqid needs to be incremented
8059 * before doing a LAYOUT_RECALL callback.
8061 if (++lyp->lay_stateid.seqid == 0)
8062 lyp->lay_stateid.seqid = 1;
8064 nfsrv_recalllayout(lyp->lay_clientid, &lyp->lay_stateid,
8065 &lyp->lay_fh, lyp, 0, lyp->lay_type, p);
8066 NFSD_DEBUG(4, "nfsrv_copymr: recalled layout\n");
8071 /* Now wait for them to be returned. */
8073 TAILQ_FOREACH(lyp, &nfsrv_recalllisthead, lay_list) {
8074 if (NFSBCMP(&lyp->lay_fh, &fh, sizeof(fh)) == 0) {
8075 if ((lyp->lay_flags & NFSLAY_RETURNED) != 0) {
8076 TAILQ_REMOVE(&nfsrv_recalllisthead, lyp,
8078 TAILQ_INSERT_HEAD(&thl, lyp, lay_list);
8080 "nfsrv_copymr: layout returned\n");
8083 ret = mtx_sleep(lyp, NFSDRECALLMUTEXPTR,
8084 PVFS | PCATCH, "nfsmrl", hz);
8085 NFSD_DEBUG(4, "nfsrv_copymr: aft sleep=%d\n",
8087 if (ret == EINTR || ret == ERESTART)
8089 if ((lyp->lay_flags & NFSLAY_RETURNED) == 0) {
8091 * Give up after 60sec and return
8092 * ENXIO, failing the copymr.
8093 * This layout will remain on the
8094 * recalllist. It can only be cleared
8095 * by restarting the nfsd.
8096 * This seems the safe way to handle
8097 * it, since it cannot be safely copied
8098 * with an outstanding RW layout.
8100 if (lyp->lay_trycnt >= 60) {
8104 if (didprintf == 0) {
8105 printf("nfsrv_copymr: layout "
8115 /* We can now get rid of the layouts that have been returned. */
8116 TAILQ_FOREACH_SAFE(lyp, &thl, lay_list, nlyp)
8117 nfsrv_freelayout(&thl, lyp);
8120 * Do the vn_start_write() calls here, before the MDS vnode is
8121 * locked and the tvp is created (locked) in the NFS file system
8123 * For tvmp, this probably isn't necessary, since it will be an
8124 * NFS mount and they are not suspendable at this time.
8127 ret = vn_start_write(vp, &mp, V_WAIT | PCATCH);
8129 tvmp = dvp->v_mount;
8130 ret = vn_start_write(NULL, &tvmp, V_WAIT | PCATCH);
8134 * LK_EXCLUSIVE lock the MDS vnode, so that any
8135 * proxied writes through the MDS will be blocked until we have
8136 * completed the copy and update of the extended attributes.
8137 * This will also ensure that any attributes and ACL will not be
8138 * changed until the copy is complete.
8140 NFSVOPLOCK(vp, LK_EXCLUSIVE | LK_RETRY);
8141 if (ret == 0 && (vp->v_iflag & VI_DOOMED) != 0) {
8142 NFSD_DEBUG(4, "nfsrv_copymr: lk_exclusive doomed\n");
8146 /* Create the data file on the recovered DS. */
8148 ret = nfsrv_createdsfile(vp, &fh, pf, dvp, ds, cred, p, &tvp);
8150 /* Copy the DS file, if created successfully. */
8153 * Get any NFSv4 ACL on the MDS file, so that it can be set
8154 * on the new DS file.
8156 aclp = acl_alloc(M_WAITOK | M_ZERO);
8157 retacl = VOP_GETACL(vp, ACL_TYPE_NFS4, aclp, cred, p);
8158 if (retacl != 0 && retacl != ENOATTR)
8159 NFSD_DEBUG(1, "nfsrv_copymr: vop_getacl=%d\n", retacl);
8160 dat = malloc(PNFSDS_COPYSIZ, M_TEMP, M_WAITOK);
8161 /* Malloc a block of 0s used to check for holes. */
8162 if (nfsrv_zeropnfsdat == NULL)
8163 nfsrv_zeropnfsdat = malloc(PNFSDS_COPYSIZ, M_TEMP,
8166 ret = VOP_GETATTR(fvp, &va, cred);
8168 while (ret == 0 && aresid == 0) {
8169 ret = vn_rdwr(UIO_READ, fvp, dat, PNFSDS_COPYSIZ,
8170 rdpos, UIO_SYSSPACE, IO_NODELOCKED, cred, NULL,
8172 xfer = PNFSDS_COPYSIZ - aresid;
8173 if (ret == 0 && xfer > 0) {
8176 * Skip the write for holes, except for the
8179 if (xfer < PNFSDS_COPYSIZ || rdpos ==
8180 va.va_size || NFSBCMP(dat,
8181 nfsrv_zeropnfsdat, PNFSDS_COPYSIZ) != 0)
8182 ret = vn_rdwr(UIO_WRITE, tvp, dat, xfer,
8183 wrpos, UIO_SYSSPACE, IO_NODELOCKED,
8184 cred, NULL, NULL, p);
8190 /* If there is an ACL and the copy succeeded, set the ACL. */
8191 if (ret == 0 && retacl == 0) {
8192 ret = VOP_SETACL(tvp, ACL_TYPE_NFS4, aclp, cred, p);
8194 * Don't consider these as errors, since VOP_GETACL()
8195 * can return an ACL when they are not actually
8196 * supported. For example, for UFS, VOP_GETACL()
8197 * will return a trivial ACL based on the uid/gid/mode
8198 * when there is no ACL on the file.
8199 * This case should be recognized as a trivial ACL
8200 * by UFS's VOP_SETACL() and succeed, but...
8202 if (ret == ENOATTR || ret == EOPNOTSUPP || ret == EPERM)
8207 ret = VOP_FSYNC(tvp, MNT_WAIT, p);
8209 /* Set the DS data file's modify time that of the MDS file. */
8211 ret = VOP_GETATTR(vp, &va, cred);
8213 mtime = va.va_mtime;
8215 va.va_mtime = mtime;
8216 ret = VOP_SETATTR(tvp, &va, cred);
8224 vn_finished_write(tvmp);
8226 /* Update the extended attributes for the newly created DS file. */
8228 ret = vn_extattr_set(vp, IO_NODELOCKED,
8229 EXTATTR_NAMESPACE_SYSTEM, "pnfsd.dsfile",
8230 sizeof(*wpf) * mirrorcnt, (char *)wpf, p);
8232 vn_finished_write(mp);
8234 /* Get rid of the dontlist entry, so that Layouts can be issued. */
8236 LIST_REMOVE(mrp, nfsmr_list);
8237 NFSDDONTLISTUNLOCK();
8238 free(mrp, M_NFSDSTATE);
8243 * Create a data storage file on the recovered DS.
8246 nfsrv_createdsfile(vnode_t vp, fhandle_t *fhp, struct pnfsdsfile *pf,
8247 vnode_t dvp, struct nfsdevice *ds, struct ucred *cred, NFSPROC_T *p,
8250 struct vattr va, nva;
8253 /* Make data file name based on FH. */
8254 error = VOP_GETATTR(vp, &va, cred);
8256 /* Set the attributes for "vp" to Setattr the DS vp. */
8258 nva.va_uid = va.va_uid;
8259 nva.va_gid = va.va_gid;
8260 nva.va_mode = va.va_mode;
8264 va.va_mode = nva.va_mode;
8265 NFSD_DEBUG(4, "nfsrv_dscreatefile: dvp=%p pf=%p\n", dvp, pf);
8266 error = nfsrv_dscreate(dvp, &va, &nva, fhp, pf, NULL,
8267 pf->dsf_filename, cred, p, tvpp);
8273 * Look up the MDS file shared locked, and then get the extended attribute
8274 * to find the extant DS file to be copied to the new mirror.
8275 * If successful, *vpp is set to the MDS file's vp and *nvpp is
8276 * set to a DS data file for the MDS file, both exclusively locked.
8277 * The "buf" argument has the pnfsdsfile structure from the MDS file
8278 * in it and buflen is set to its length.
8281 nfsrv_mdscopymr(char *mdspathp, char *dspathp, char *curdspathp, char *buf,
8282 int *buflenp, char *fname, NFSPROC_T *p, struct vnode **vpp,
8283 struct vnode **nvpp, struct pnfsdsfile **pfp, struct nfsdevice **dsp,
8284 struct nfsdevice **fdsp)
8286 struct nameidata nd;
8287 struct vnode *vp, *curvp;
8288 struct pnfsdsfile *pf;
8289 struct nfsmount *nmp, *curnmp;
8290 int dsdir, error, mirrorcnt, ippos;
8297 if (dspathp == NULL && curdspathp != NULL)
8301 * Look up the MDS file shared locked. The lock will be upgraded
8302 * to an exclusive lock after any rw layouts have been returned.
8304 NFSD_DEBUG(4, "mdsopen path=%s\n", mdspathp);
8305 NDINIT(&nd, LOOKUP, FOLLOW | LOCKSHARED | LOCKLEAF, UIO_SYSSPACE,
8308 NFSD_DEBUG(4, "lookup=%d\n", error);
8311 if (nd.ni_vp->v_type != VREG) {
8313 NFSD_DEBUG(4, "mdspath not reg\n");
8318 if (curdspathp != NULL) {
8320 * Look up the current DS path and find the nfsdev structure for
8323 NFSD_DEBUG(4, "curmdsdev path=%s\n", curdspathp);
8324 NDINIT(&nd, LOOKUP, FOLLOW | LOCKSHARED | LOCKLEAF,
8325 UIO_SYSSPACE, curdspathp, p);
8327 NFSD_DEBUG(4, "ds lookup=%d\n", error);
8332 if (nd.ni_vp->v_type != VDIR) {
8335 NFSD_DEBUG(4, "curdspath not dir\n");
8338 if (strcmp(nd.ni_vp->v_mount->mnt_vfc->vfc_name, "nfs") != 0) {
8341 NFSD_DEBUG(4, "curdspath not an NFS mount\n");
8344 curnmp = VFSTONFS(nd.ni_vp->v_mount);
8346 /* Search the nfsdev list for a match. */
8348 *fdsp = nfsv4_findmirror(curnmp);
8352 if (curnmp == NULL) {
8355 NFSD_DEBUG(4, "mdscopymr: no current ds\n");
8361 if (dspathp != NULL) {
8362 /* Look up the nfsdev path and find the nfsdev structure. */
8363 NFSD_DEBUG(4, "mdsdev path=%s\n", dspathp);
8364 NDINIT(&nd, LOOKUP, FOLLOW | LOCKSHARED | LOCKLEAF,
8365 UIO_SYSSPACE, dspathp, p);
8367 NFSD_DEBUG(4, "ds lookup=%d\n", error);
8374 if (nd.ni_vp->v_type != VDIR || nd.ni_vp == curvp) {
8379 NFSD_DEBUG(4, "dspath not dir\n");
8380 if (nd.ni_vp == curvp)
8384 if (strcmp(nd.ni_vp->v_mount->mnt_vfc->vfc_name, "nfs") != 0) {
8389 NFSD_DEBUG(4, "dspath not an NFS mount\n");
8392 nmp = VFSTONFS(nd.ni_vp->v_mount);
8395 * Search the nfsdevice list for a match. If curnmp == NULL,
8396 * this is a recovery and there must be a mirror.
8400 *dsp = nfsrv_findmirroredds(nmp);
8402 *dsp = nfsv4_findmirror(nmp);
8409 NFSD_DEBUG(4, "mdscopymr: no ds\n");
8418 * Get a vp for an available DS data file using the extended
8419 * attribute on the MDS file.
8420 * If there is a valid entry for the new DS in the extended attribute
8421 * on the MDS file (as checked via the nmp argument),
8422 * nfsrv_dsgetsockmnt() returns EEXIST, so no copying will occur.
8424 error = nfsrv_dsgetsockmnt(vp, 0, buf, buflenp, &mirrorcnt, p,
8425 NULL, NULL, NULL, fname, nvpp, &nmp, curnmp, &ippos, &dsdir);
8428 if (nd.ni_vp == NULL) {
8429 if (error == 0 && nmp != NULL) {
8430 /* Search the nfsdev list for a match. */
8432 *dsp = nfsrv_findmirroredds(nmp);
8435 if (error == 0 && (nmp == NULL || *dsp == NULL)) {
8436 if (nvpp != NULL && *nvpp != NULL) {
8446 * When dspathp != NULL and curdspathp == NULL, this is a recovery
8447 * and is only allowed if there is a 0.0.0.0 IP address entry.
8448 * When curdspathp != NULL, the ippos will be set to that entry.
8450 if (error == 0 && dspathp != NULL && ippos == -1) {
8451 if (nvpp != NULL && *nvpp != NULL) {
8460 pf = (struct pnfsdsfile *)buf;
8462 /* If no zeroip pnfsdsfile, add one. */
8463 ippos = *buflenp / sizeof(*pf);
8464 *buflenp += sizeof(*pf);
8466 pf->dsf_dir = dsdir;
8467 strlcpy(pf->dsf_filename, fname,
8468 sizeof(pf->dsf_filename));
8478 * Search for a matching pnfsd mirror device structure, base on the nmp arg.
8479 * Return one if found, NULL otherwise.
8481 static struct nfsdevice *
8482 nfsrv_findmirroredds(struct nfsmount *nmp)
8484 struct nfsdevice *ds, *fndds;
8487 mtx_assert(NFSDDSMUTEXPTR, MA_OWNED);
8489 * Search the DS server list for a match with nmp.
8490 * Remove the DS entry if found and there is a mirror.
8494 if (nfsrv_devidcnt == 0)
8496 TAILQ_FOREACH(ds, &nfsrv_devidhead, nfsdev_list) {
8497 if (ds->nfsdev_nmp == nmp) {
8498 NFSD_DEBUG(4, "nfsrv_findmirroredds: fnd main ds\n");
8505 if (fndds->nfsdev_mdsisset == 0 && nfsrv_faildscnt > 0)
8507 else if (fndds->nfsdev_mdsisset != 0) {
8508 /* For the fsid is set case, search for a mirror. */
8509 TAILQ_FOREACH(ds, &nfsrv_devidhead, nfsdev_list) {
8510 if (ds != fndds && ds->nfsdev_nmp != NULL &&
8511 ds->nfsdev_mdsisset != 0 &&
8512 ds->nfsdev_mdsfsid.val[0] ==
8513 fndds->nfsdev_mdsfsid.val[0] &&
8514 ds->nfsdev_mdsfsid.val[1] ==
8515 fndds->nfsdev_mdsfsid.val[1]) {
8521 if (fndmirror == 0) {
8522 NFSD_DEBUG(4, "nfsrv_findmirroredds: no mirror for DS\n");