nfs_nfsdstate.c revision 1.2.6.2 1 1.2.6.2 yamt /* $NetBSD: nfs_nfsdstate.c,v 1.2.6.2 2014/05/22 11:41:01 yamt Exp $ */
2 1.2.6.2 yamt /*-
3 1.2.6.2 yamt * Copyright (c) 2009 Rick Macklem, University of Guelph
4 1.2.6.2 yamt * All rights reserved.
5 1.2.6.2 yamt *
6 1.2.6.2 yamt * Redistribution and use in source and binary forms, with or without
7 1.2.6.2 yamt * modification, are permitted provided that the following conditions
8 1.2.6.2 yamt * are met:
9 1.2.6.2 yamt * 1. Redistributions of source code must retain the above copyright
10 1.2.6.2 yamt * notice, this list of conditions and the following disclaimer.
11 1.2.6.2 yamt * 2. Redistributions in binary form must reproduce the above copyright
12 1.2.6.2 yamt * notice, this list of conditions and the following disclaimer in the
13 1.2.6.2 yamt * documentation and/or other materials provided with the distribution.
14 1.2.6.2 yamt *
15 1.2.6.2 yamt * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 1.2.6.2 yamt * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 1.2.6.2 yamt * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 1.2.6.2 yamt * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 1.2.6.2 yamt * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 1.2.6.2 yamt * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 1.2.6.2 yamt * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 1.2.6.2 yamt * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 1.2.6.2 yamt * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 1.2.6.2 yamt * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25 1.2.6.2 yamt * SUCH DAMAGE.
26 1.2.6.2 yamt *
27 1.2.6.2 yamt */
28 1.2.6.2 yamt
29 1.2.6.2 yamt #include <sys/cdefs.h>
30 1.2.6.2 yamt /* __FBSDID("FreeBSD: head/sys/fs/nfsserver/nfs_nfsdstate.c 245909 2013-01-25 15:25:24Z jhb "); */
31 1.2.6.2 yamt __RCSID("$NetBSD: nfs_nfsdstate.c,v 1.2.6.2 2014/05/22 11:41:01 yamt Exp $");
32 1.2.6.2 yamt
33 1.2.6.2 yamt #ifndef APPLEKEXT
34 1.2.6.2 yamt #include <fs/nfs/nfsport.h>
35 1.2.6.2 yamt
36 1.2.6.2 yamt struct nfsrv_stablefirst nfsrv_stablefirst;
37 1.2.6.2 yamt int nfsrv_issuedelegs = 0;
38 1.2.6.2 yamt int nfsrv_dolocallocks = 0;
39 1.2.6.2 yamt struct nfsv4lock nfsv4rootfs_lock;
40 1.2.6.2 yamt
41 1.2.6.2 yamt extern int newnfs_numnfsd;
42 1.2.6.2 yamt extern struct nfsstats newnfsstats;
43 1.2.6.2 yamt extern int nfsrv_lease;
44 1.2.6.2 yamt extern struct timeval nfsboottime;
45 1.2.6.2 yamt extern u_int32_t newnfs_true, newnfs_false;
46 1.2.6.2 yamt NFSV4ROOTLOCKMUTEX;
47 1.2.6.2 yamt NFSSTATESPINLOCK;
48 1.2.6.2 yamt
49 1.2.6.2 yamt /*
50 1.2.6.2 yamt * Hash lists for nfs V4.
51 1.2.6.2 yamt * (Some would put them in the .h file, but I don't like declaring storage
52 1.2.6.2 yamt * in a .h)
53 1.2.6.2 yamt */
54 1.2.6.2 yamt struct nfsclienthashhead nfsclienthash[NFSCLIENTHASHSIZE];
55 1.2.6.2 yamt struct nfslockhashhead nfslockhash[NFSLOCKHASHSIZE];
56 1.2.6.2 yamt #endif /* !APPLEKEXT */
57 1.2.6.2 yamt
58 1.2.6.2 yamt static u_int32_t nfsrv_openpluslock = 0, nfsrv_delegatecnt = 0;
59 1.2.6.2 yamt static time_t nfsrvboottime;
60 1.2.6.2 yamt static int nfsrv_writedelegifpos = 1;
61 1.2.6.2 yamt static int nfsrv_returnoldstateid = 0, nfsrv_clients = 0;
62 1.2.6.2 yamt static int nfsrv_clienthighwater = NFSRV_CLIENTHIGHWATER;
63 1.2.6.2 yamt static int nfsrv_nogsscallback = 0;
64 1.2.6.2 yamt
65 1.2.6.2 yamt /* local functions */
66 1.2.6.2 yamt static void nfsrv_dumpaclient(struct nfsclient *clp,
67 1.2.6.2 yamt struct nfsd_dumpclients *dumpp);
68 1.2.6.2 yamt static void nfsrv_freeopenowner(struct nfsstate *stp, int cansleep,
69 1.2.6.2 yamt NFSPROC_T *p);
70 1.2.6.2 yamt static int nfsrv_freeopen(struct nfsstate *stp, vnode_t vp, int cansleep,
71 1.2.6.2 yamt NFSPROC_T *p);
72 1.2.6.2 yamt static void nfsrv_freelockowner(struct nfsstate *stp, vnode_t vp, int cansleep,
73 1.2.6.2 yamt NFSPROC_T *p);
74 1.2.6.2 yamt static void nfsrv_freeallnfslocks(struct nfsstate *stp, vnode_t vp,
75 1.2.6.2 yamt int cansleep, NFSPROC_T *p);
76 1.2.6.2 yamt static void nfsrv_freenfslock(struct nfslock *lop);
77 1.2.6.2 yamt static void nfsrv_freenfslockfile(struct nfslockfile *lfp);
78 1.2.6.2 yamt static void nfsrv_freedeleg(struct nfsstate *);
79 1.2.6.2 yamt static int nfsrv_getstate(struct nfsclient *clp, nfsv4stateid_t *stateidp,
80 1.2.6.2 yamt u_int32_t flags, struct nfsstate **stpp);
81 1.2.6.2 yamt static void nfsrv_getowner(struct nfsstatehead *hp, struct nfsstate *new_stp,
82 1.2.6.2 yamt struct nfsstate **stpp);
83 1.2.6.2 yamt static int nfsrv_getlockfh(vnode_t vp, u_short flags,
84 1.2.6.2 yamt struct nfslockfile **new_lfpp, fhandle_t *nfhp, NFSPROC_T *p);
85 1.2.6.2 yamt static int nfsrv_getlockfile(u_short flags, struct nfslockfile **new_lfpp,
86 1.2.6.2 yamt struct nfslockfile **lfpp, fhandle_t *nfhp, int lockit);
87 1.2.6.2 yamt static void nfsrv_insertlock(struct nfslock *new_lop,
88 1.2.6.2 yamt struct nfslock *insert_lop, struct nfsstate *stp, struct nfslockfile *lfp);
89 1.2.6.2 yamt static void nfsrv_updatelock(struct nfsstate *stp, struct nfslock **new_lopp,
90 1.2.6.2 yamt struct nfslock **other_lopp, struct nfslockfile *lfp);
91 1.2.6.2 yamt static int nfsrv_getipnumber(u_char *cp);
92 1.2.6.2 yamt static int nfsrv_checkrestart(nfsquad_t clientid, u_int32_t flags,
93 1.2.6.2 yamt nfsv4stateid_t *stateidp, int specialid);
94 1.2.6.2 yamt static int nfsrv_checkgrace(u_int32_t flags);
95 1.2.6.2 yamt static int nfsrv_docallback(struct nfsclient *clp, int procnum,
96 1.2.6.2 yamt nfsv4stateid_t *stateidp, int trunc, fhandle_t *fhp,
97 1.2.6.2 yamt struct nfsvattr *nap, nfsattrbit_t *attrbitp, NFSPROC_T *p);
98 1.2.6.2 yamt static u_int32_t nfsrv_nextclientindex(void);
99 1.2.6.2 yamt static u_int32_t nfsrv_nextstateindex(struct nfsclient *clp);
100 1.2.6.2 yamt static void nfsrv_markstable(struct nfsclient *clp);
101 1.2.6.2 yamt static int nfsrv_checkstable(struct nfsclient *clp);
102 1.2.6.2 yamt static int nfsrv_clientconflict(struct nfsclient *clp, int *haslockp, struct
103 1.2.6.2 yamt vnode *vp, NFSPROC_T *p);
104 1.2.6.2 yamt static int nfsrv_delegconflict(struct nfsstate *stp, int *haslockp,
105 1.2.6.2 yamt NFSPROC_T *p, vnode_t vp);
106 1.2.6.2 yamt static int nfsrv_cleandeleg(vnode_t vp, struct nfslockfile *lfp,
107 1.2.6.2 yamt struct nfsclient *clp, int *haslockp, NFSPROC_T *p);
108 1.2.6.2 yamt static int nfsrv_notsamecredname(struct nfsrv_descript *nd,
109 1.2.6.2 yamt struct nfsclient *clp);
110 1.2.6.2 yamt static time_t nfsrv_leaseexpiry(void);
111 1.2.6.2 yamt static void nfsrv_delaydelegtimeout(struct nfsstate *stp);
112 1.2.6.2 yamt static int nfsrv_checkseqid(struct nfsrv_descript *nd, u_int32_t seqid,
113 1.2.6.2 yamt struct nfsstate *stp, struct nfsrvcache *op);
114 1.2.6.2 yamt static int nfsrv_nootherstate(struct nfsstate *stp);
115 1.2.6.2 yamt static int nfsrv_locallock(vnode_t vp, struct nfslockfile *lfp, int flags,
116 1.2.6.2 yamt uint64_t first, uint64_t end, struct nfslockconflict *cfp, NFSPROC_T *p);
117 1.2.6.2 yamt static void nfsrv_localunlock(vnode_t vp, struct nfslockfile *lfp,
118 1.2.6.2 yamt uint64_t init_first, uint64_t init_end, NFSPROC_T *p);
119 1.2.6.2 yamt static int nfsrv_dolocal(vnode_t vp, struct nfslockfile *lfp, int flags,
120 1.2.6.2 yamt int oldflags, uint64_t first, uint64_t end, struct nfslockconflict *cfp,
121 1.2.6.2 yamt NFSPROC_T *p);
122 1.2.6.2 yamt static void nfsrv_locallock_rollback(vnode_t vp, struct nfslockfile *lfp,
123 1.2.6.2 yamt NFSPROC_T *p);
124 1.2.6.2 yamt static void nfsrv_locallock_commit(struct nfslockfile *lfp, int flags,
125 1.2.6.2 yamt uint64_t first, uint64_t end);
126 1.2.6.2 yamt static void nfsrv_locklf(struct nfslockfile *lfp);
127 1.2.6.2 yamt static void nfsrv_unlocklf(struct nfslockfile *lfp);
128 1.2.6.2 yamt
129 1.2.6.2 yamt /*
130 1.2.6.2 yamt * Scan the client list for a match and either return the current one,
131 1.2.6.2 yamt * create a new entry or return an error.
132 1.2.6.2 yamt * If returning a non-error, the clp structure must either be linked into
133 1.2.6.2 yamt * the client list or free'd.
134 1.2.6.2 yamt */
135 1.2.6.2 yamt APPLESTATIC int
136 1.2.6.2 yamt nfsrv_setclient(struct nfsrv_descript *nd, struct nfsclient **new_clpp,
137 1.2.6.2 yamt nfsquad_t *clientidp, nfsquad_t *confirmp, NFSPROC_T *p)
138 1.2.6.2 yamt {
139 1.2.6.2 yamt struct nfsclient *clp = NULL, *new_clp = *new_clpp;
140 1.2.6.2 yamt int i, error = 0;
141 1.2.6.2 yamt struct nfsstate *stp, *tstp;
142 1.2.6.2 yamt struct sockaddr_in *sad, *rad;
143 1.2.6.2 yamt int zapit = 0, gotit, hasstate = 0, igotlock;
144 1.2.6.2 yamt static u_int64_t confirm_index = 0;
145 1.2.6.2 yamt
146 1.2.6.2 yamt /*
147 1.2.6.2 yamt * Check for state resource limit exceeded.
148 1.2.6.2 yamt */
149 1.2.6.2 yamt if (nfsrv_openpluslock > NFSRV_V4STATELIMIT) {
150 1.2.6.2 yamt error = NFSERR_RESOURCE;
151 1.2.6.2 yamt goto out;
152 1.2.6.2 yamt }
153 1.2.6.2 yamt
154 1.2.6.2 yamt if (nfsrv_issuedelegs == 0 ||
155 1.2.6.2 yamt ((nd->nd_flag & ND_GSS) != 0 && nfsrv_nogsscallback != 0))
156 1.2.6.2 yamt /*
157 1.2.6.2 yamt * Don't do callbacks when delegations are disabled or
158 1.2.6.2 yamt * for AUTH_GSS unless enabled via nfsrv_nogsscallback.
159 1.2.6.2 yamt * If establishing a callback connection is attempted
160 1.2.6.2 yamt * when a firewall is blocking the callback path, the
161 1.2.6.2 yamt * server may wait too long for the connect attempt to
162 1.2.6.2 yamt * succeed during the Open. Some clients, such as Linux,
163 1.2.6.2 yamt * may timeout and give up on the Open before the server
164 1.2.6.2 yamt * replies. Also, since AUTH_GSS callbacks are not
165 1.2.6.2 yamt * yet interoperability tested, they might cause the
166 1.2.6.2 yamt * server to crap out, if they get past the Init call to
167 1.2.6.2 yamt * the client.
168 1.2.6.2 yamt */
169 1.2.6.2 yamt new_clp->lc_program = 0;
170 1.2.6.2 yamt
171 1.2.6.2 yamt /* Lock out other nfsd threads */
172 1.2.6.2 yamt NFSLOCKV4ROOTMUTEX();
173 1.2.6.2 yamt nfsv4_relref(&nfsv4rootfs_lock);
174 1.2.6.2 yamt do {
175 1.2.6.2 yamt igotlock = nfsv4_lock(&nfsv4rootfs_lock, 1, NULL,
176 1.2.6.2 yamt NFSV4ROOTLOCKMUTEXPTR, NULL);
177 1.2.6.2 yamt } while (!igotlock);
178 1.2.6.2 yamt NFSUNLOCKV4ROOTMUTEX();
179 1.2.6.2 yamt
180 1.2.6.2 yamt /*
181 1.2.6.2 yamt * Search for a match in the client list.
182 1.2.6.2 yamt */
183 1.2.6.2 yamt gotit = i = 0;
184 1.2.6.2 yamt while (i < NFSCLIENTHASHSIZE && !gotit) {
185 1.2.6.2 yamt LIST_FOREACH(clp, &nfsclienthash[i], lc_hash) {
186 1.2.6.2 yamt if (new_clp->lc_idlen == clp->lc_idlen &&
187 1.2.6.2 yamt !NFSBCMP(new_clp->lc_id, clp->lc_id, clp->lc_idlen)) {
188 1.2.6.2 yamt gotit = 1;
189 1.2.6.2 yamt break;
190 1.2.6.2 yamt }
191 1.2.6.2 yamt }
192 1.2.6.2 yamt i++;
193 1.2.6.2 yamt }
194 1.2.6.2 yamt if (!gotit ||
195 1.2.6.2 yamt (clp->lc_flags & (LCL_NEEDSCONFIRM | LCL_ADMINREVOKED))) {
196 1.2.6.2 yamt /*
197 1.2.6.2 yamt * Get rid of the old one.
198 1.2.6.2 yamt */
199 1.2.6.2 yamt if (i != NFSCLIENTHASHSIZE) {
200 1.2.6.2 yamt LIST_REMOVE(clp, lc_hash);
201 1.2.6.2 yamt nfsrv_cleanclient(clp, p);
202 1.2.6.2 yamt nfsrv_freedeleglist(&clp->lc_deleg);
203 1.2.6.2 yamt nfsrv_freedeleglist(&clp->lc_olddeleg);
204 1.2.6.2 yamt zapit = 1;
205 1.2.6.2 yamt }
206 1.2.6.2 yamt /*
207 1.2.6.2 yamt * Add it after assigning a client id to it.
208 1.2.6.2 yamt */
209 1.2.6.2 yamt new_clp->lc_flags |= LCL_NEEDSCONFIRM;
210 1.2.6.2 yamt confirmp->qval = new_clp->lc_confirm.qval = ++confirm_index;
211 1.2.6.2 yamt clientidp->lval[0] = new_clp->lc_clientid.lval[0] =
212 1.2.6.2 yamt (u_int32_t)nfsrvboottime;
213 1.2.6.2 yamt clientidp->lval[1] = new_clp->lc_clientid.lval[1] =
214 1.2.6.2 yamt nfsrv_nextclientindex();
215 1.2.6.2 yamt new_clp->lc_stateindex = 0;
216 1.2.6.2 yamt new_clp->lc_statemaxindex = 0;
217 1.2.6.2 yamt new_clp->lc_cbref = 0;
218 1.2.6.2 yamt new_clp->lc_expiry = nfsrv_leaseexpiry();
219 1.2.6.2 yamt LIST_INIT(&new_clp->lc_open);
220 1.2.6.2 yamt LIST_INIT(&new_clp->lc_deleg);
221 1.2.6.2 yamt LIST_INIT(&new_clp->lc_olddeleg);
222 1.2.6.2 yamt for (i = 0; i < NFSSTATEHASHSIZE; i++)
223 1.2.6.2 yamt LIST_INIT(&new_clp->lc_stateid[i]);
224 1.2.6.2 yamt LIST_INSERT_HEAD(NFSCLIENTHASH(new_clp->lc_clientid), new_clp,
225 1.2.6.2 yamt lc_hash);
226 1.2.6.2 yamt newnfsstats.srvclients++;
227 1.2.6.2 yamt nfsrv_openpluslock++;
228 1.2.6.2 yamt nfsrv_clients++;
229 1.2.6.2 yamt NFSLOCKV4ROOTMUTEX();
230 1.2.6.2 yamt nfsv4_unlock(&nfsv4rootfs_lock, 1);
231 1.2.6.2 yamt NFSUNLOCKV4ROOTMUTEX();
232 1.2.6.2 yamt if (zapit)
233 1.2.6.2 yamt nfsrv_zapclient(clp, p);
234 1.2.6.2 yamt *new_clpp = NULL;
235 1.2.6.2 yamt goto out;
236 1.2.6.2 yamt }
237 1.2.6.2 yamt
238 1.2.6.2 yamt /*
239 1.2.6.2 yamt * Now, handle the cases where the id is already issued.
240 1.2.6.2 yamt */
241 1.2.6.2 yamt if (nfsrv_notsamecredname(nd, clp)) {
242 1.2.6.2 yamt /*
243 1.2.6.2 yamt * Check to see if there is expired state that should go away.
244 1.2.6.2 yamt */
245 1.2.6.2 yamt if (clp->lc_expiry < NFSD_MONOSEC &&
246 1.2.6.2 yamt (!LIST_EMPTY(&clp->lc_open) || !LIST_EMPTY(&clp->lc_deleg))) {
247 1.2.6.2 yamt nfsrv_cleanclient(clp, p);
248 1.2.6.2 yamt nfsrv_freedeleglist(&clp->lc_deleg);
249 1.2.6.2 yamt }
250 1.2.6.2 yamt
251 1.2.6.2 yamt /*
252 1.2.6.2 yamt * If there is outstanding state, then reply NFSERR_CLIDINUSE per
253 1.2.6.2 yamt * RFC3530 Sec. 8.1.2 last para.
254 1.2.6.2 yamt */
255 1.2.6.2 yamt if (!LIST_EMPTY(&clp->lc_deleg)) {
256 1.2.6.2 yamt hasstate = 1;
257 1.2.6.2 yamt } else if (LIST_EMPTY(&clp->lc_open)) {
258 1.2.6.2 yamt hasstate = 0;
259 1.2.6.2 yamt } else {
260 1.2.6.2 yamt hasstate = 0;
261 1.2.6.2 yamt /* Look for an Open on the OpenOwner */
262 1.2.6.2 yamt LIST_FOREACH(stp, &clp->lc_open, ls_list) {
263 1.2.6.2 yamt if (!LIST_EMPTY(&stp->ls_open)) {
264 1.2.6.2 yamt hasstate = 1;
265 1.2.6.2 yamt break;
266 1.2.6.2 yamt }
267 1.2.6.2 yamt }
268 1.2.6.2 yamt }
269 1.2.6.2 yamt if (hasstate) {
270 1.2.6.2 yamt /*
271 1.2.6.2 yamt * If the uid doesn't match, return NFSERR_CLIDINUSE after
272 1.2.6.2 yamt * filling out the correct ipaddr and portnum.
273 1.2.6.2 yamt */
274 1.2.6.2 yamt sad = NFSSOCKADDR(new_clp->lc_req.nr_nam, struct sockaddr_in *);
275 1.2.6.2 yamt rad = NFSSOCKADDR(clp->lc_req.nr_nam, struct sockaddr_in *);
276 1.2.6.2 yamt sad->sin_addr.s_addr = rad->sin_addr.s_addr;
277 1.2.6.2 yamt sad->sin_port = rad->sin_port;
278 1.2.6.2 yamt NFSLOCKV4ROOTMUTEX();
279 1.2.6.2 yamt nfsv4_unlock(&nfsv4rootfs_lock, 1);
280 1.2.6.2 yamt NFSUNLOCKV4ROOTMUTEX();
281 1.2.6.2 yamt error = NFSERR_CLIDINUSE;
282 1.2.6.2 yamt goto out;
283 1.2.6.2 yamt }
284 1.2.6.2 yamt }
285 1.2.6.2 yamt
286 1.2.6.2 yamt if (NFSBCMP(new_clp->lc_verf, clp->lc_verf, NFSX_VERF)) {
287 1.2.6.2 yamt /*
288 1.2.6.2 yamt * If the verifier has changed, the client has rebooted
289 1.2.6.2 yamt * and a new client id is issued. The old state info
290 1.2.6.2 yamt * can be thrown away once the SETCLIENTID_CONFIRM occurs.
291 1.2.6.2 yamt */
292 1.2.6.2 yamt LIST_REMOVE(clp, lc_hash);
293 1.2.6.2 yamt new_clp->lc_flags |= LCL_NEEDSCONFIRM;
294 1.2.6.2 yamt confirmp->qval = new_clp->lc_confirm.qval = ++confirm_index;
295 1.2.6.2 yamt clientidp->lval[0] = new_clp->lc_clientid.lval[0] =
296 1.2.6.2 yamt nfsrvboottime;
297 1.2.6.2 yamt clientidp->lval[1] = new_clp->lc_clientid.lval[1] =
298 1.2.6.2 yamt nfsrv_nextclientindex();
299 1.2.6.2 yamt new_clp->lc_stateindex = 0;
300 1.2.6.2 yamt new_clp->lc_statemaxindex = 0;
301 1.2.6.2 yamt new_clp->lc_cbref = 0;
302 1.2.6.2 yamt new_clp->lc_expiry = nfsrv_leaseexpiry();
303 1.2.6.2 yamt
304 1.2.6.2 yamt /*
305 1.2.6.2 yamt * Save the state until confirmed.
306 1.2.6.2 yamt */
307 1.2.6.2 yamt LIST_NEWHEAD(&new_clp->lc_open, &clp->lc_open, ls_list);
308 1.2.6.2 yamt LIST_FOREACH(tstp, &new_clp->lc_open, ls_list)
309 1.2.6.2 yamt tstp->ls_clp = new_clp;
310 1.2.6.2 yamt LIST_NEWHEAD(&new_clp->lc_deleg, &clp->lc_deleg, ls_list);
311 1.2.6.2 yamt LIST_FOREACH(tstp, &new_clp->lc_deleg, ls_list)
312 1.2.6.2 yamt tstp->ls_clp = new_clp;
313 1.2.6.2 yamt LIST_NEWHEAD(&new_clp->lc_olddeleg, &clp->lc_olddeleg,
314 1.2.6.2 yamt ls_list);
315 1.2.6.2 yamt LIST_FOREACH(tstp, &new_clp->lc_olddeleg, ls_list)
316 1.2.6.2 yamt tstp->ls_clp = new_clp;
317 1.2.6.2 yamt for (i = 0; i < NFSSTATEHASHSIZE; i++) {
318 1.2.6.2 yamt LIST_NEWHEAD(&new_clp->lc_stateid[i],
319 1.2.6.2 yamt &clp->lc_stateid[i], ls_hash);
320 1.2.6.2 yamt LIST_FOREACH(tstp, &new_clp->lc_stateid[i], ls_hash)
321 1.2.6.2 yamt tstp->ls_clp = new_clp;
322 1.2.6.2 yamt }
323 1.2.6.2 yamt LIST_INSERT_HEAD(NFSCLIENTHASH(new_clp->lc_clientid), new_clp,
324 1.2.6.2 yamt lc_hash);
325 1.2.6.2 yamt newnfsstats.srvclients++;
326 1.2.6.2 yamt nfsrv_openpluslock++;
327 1.2.6.2 yamt nfsrv_clients++;
328 1.2.6.2 yamt NFSLOCKV4ROOTMUTEX();
329 1.2.6.2 yamt nfsv4_unlock(&nfsv4rootfs_lock, 1);
330 1.2.6.2 yamt NFSUNLOCKV4ROOTMUTEX();
331 1.2.6.2 yamt
332 1.2.6.2 yamt /*
333 1.2.6.2 yamt * Must wait until any outstanding callback on the old clp
334 1.2.6.2 yamt * completes.
335 1.2.6.2 yamt */
336 1.2.6.2 yamt NFSLOCKSTATE();
337 1.2.6.2 yamt while (clp->lc_cbref) {
338 1.2.6.2 yamt clp->lc_flags |= LCL_WAKEUPWANTED;
339 1.2.6.2 yamt (void)mtx_sleep(clp, NFSSTATEMUTEXPTR, PZERO - 1,
340 1.2.6.2 yamt "nfsd clp", 10 * hz);
341 1.2.6.2 yamt }
342 1.2.6.2 yamt NFSUNLOCKSTATE();
343 1.2.6.2 yamt nfsrv_zapclient(clp, p);
344 1.2.6.2 yamt *new_clpp = NULL;
345 1.2.6.2 yamt goto out;
346 1.2.6.2 yamt }
347 1.2.6.2 yamt /*
348 1.2.6.2 yamt * id and verifier match, so update the net address info
349 1.2.6.2 yamt * and get rid of any existing callback authentication
350 1.2.6.2 yamt * handle, so a new one will be acquired.
351 1.2.6.2 yamt */
352 1.2.6.2 yamt LIST_REMOVE(clp, lc_hash);
353 1.2.6.2 yamt new_clp->lc_flags |= (LCL_NEEDSCONFIRM | LCL_DONTCLEAN);
354 1.2.6.2 yamt new_clp->lc_expiry = nfsrv_leaseexpiry();
355 1.2.6.2 yamt confirmp->qval = new_clp->lc_confirm.qval = ++confirm_index;
356 1.2.6.2 yamt clientidp->lval[0] = new_clp->lc_clientid.lval[0] =
357 1.2.6.2 yamt clp->lc_clientid.lval[0];
358 1.2.6.2 yamt clientidp->lval[1] = new_clp->lc_clientid.lval[1] =
359 1.2.6.2 yamt clp->lc_clientid.lval[1];
360 1.2.6.2 yamt new_clp->lc_delegtime = clp->lc_delegtime;
361 1.2.6.2 yamt new_clp->lc_stateindex = clp->lc_stateindex;
362 1.2.6.2 yamt new_clp->lc_statemaxindex = clp->lc_statemaxindex;
363 1.2.6.2 yamt new_clp->lc_cbref = 0;
364 1.2.6.2 yamt LIST_NEWHEAD(&new_clp->lc_open, &clp->lc_open, ls_list);
365 1.2.6.2 yamt LIST_FOREACH(tstp, &new_clp->lc_open, ls_list)
366 1.2.6.2 yamt tstp->ls_clp = new_clp;
367 1.2.6.2 yamt LIST_NEWHEAD(&new_clp->lc_deleg, &clp->lc_deleg, ls_list);
368 1.2.6.2 yamt LIST_FOREACH(tstp, &new_clp->lc_deleg, ls_list)
369 1.2.6.2 yamt tstp->ls_clp = new_clp;
370 1.2.6.2 yamt LIST_NEWHEAD(&new_clp->lc_olddeleg, &clp->lc_olddeleg, ls_list);
371 1.2.6.2 yamt LIST_FOREACH(tstp, &new_clp->lc_olddeleg, ls_list)
372 1.2.6.2 yamt tstp->ls_clp = new_clp;
373 1.2.6.2 yamt for (i = 0; i < NFSSTATEHASHSIZE; i++) {
374 1.2.6.2 yamt LIST_NEWHEAD(&new_clp->lc_stateid[i], &clp->lc_stateid[i],
375 1.2.6.2 yamt ls_hash);
376 1.2.6.2 yamt LIST_FOREACH(tstp, &new_clp->lc_stateid[i], ls_hash)
377 1.2.6.2 yamt tstp->ls_clp = new_clp;
378 1.2.6.2 yamt }
379 1.2.6.2 yamt LIST_INSERT_HEAD(NFSCLIENTHASH(new_clp->lc_clientid), new_clp,
380 1.2.6.2 yamt lc_hash);
381 1.2.6.2 yamt newnfsstats.srvclients++;
382 1.2.6.2 yamt nfsrv_openpluslock++;
383 1.2.6.2 yamt nfsrv_clients++;
384 1.2.6.2 yamt NFSLOCKV4ROOTMUTEX();
385 1.2.6.2 yamt nfsv4_unlock(&nfsv4rootfs_lock, 1);
386 1.2.6.2 yamt NFSUNLOCKV4ROOTMUTEX();
387 1.2.6.2 yamt
388 1.2.6.2 yamt /*
389 1.2.6.2 yamt * Must wait until any outstanding callback on the old clp
390 1.2.6.2 yamt * completes.
391 1.2.6.2 yamt */
392 1.2.6.2 yamt NFSLOCKSTATE();
393 1.2.6.2 yamt while (clp->lc_cbref) {
394 1.2.6.2 yamt clp->lc_flags |= LCL_WAKEUPWANTED;
395 1.2.6.2 yamt (void)mtx_sleep(clp, NFSSTATEMUTEXPTR, PZERO - 1, "nfsd clp",
396 1.2.6.2 yamt 10 * hz);
397 1.2.6.2 yamt }
398 1.2.6.2 yamt NFSUNLOCKSTATE();
399 1.2.6.2 yamt nfsrv_zapclient(clp, p);
400 1.2.6.2 yamt *new_clpp = NULL;
401 1.2.6.2 yamt
402 1.2.6.2 yamt out:
403 1.2.6.2 yamt NFSEXITCODE2(error, nd);
404 1.2.6.2 yamt return (error);
405 1.2.6.2 yamt }
406 1.2.6.2 yamt
407 1.2.6.2 yamt /*
408 1.2.6.2 yamt * Check to see if the client id exists and optionally confirm it.
409 1.2.6.2 yamt */
410 1.2.6.2 yamt APPLESTATIC int
411 1.2.6.2 yamt nfsrv_getclient(nfsquad_t clientid, int opflags, struct nfsclient **clpp,
412 1.2.6.2 yamt nfsquad_t confirm, struct nfsrv_descript *nd, NFSPROC_T *p)
413 1.2.6.2 yamt {
414 1.2.6.2 yamt struct nfsclient *clp;
415 1.2.6.2 yamt struct nfsstate *stp;
416 1.2.6.2 yamt int i;
417 1.2.6.2 yamt struct nfsclienthashhead *hp;
418 1.2.6.2 yamt int error = 0, igotlock, doneok;
419 1.2.6.2 yamt
420 1.2.6.2 yamt if (clpp)
421 1.2.6.2 yamt *clpp = NULL;
422 1.2.6.2 yamt if (nfsrvboottime != clientid.lval[0]) {
423 1.2.6.2 yamt error = NFSERR_STALECLIENTID;
424 1.2.6.2 yamt goto out;
425 1.2.6.2 yamt }
426 1.2.6.2 yamt
427 1.2.6.2 yamt /*
428 1.2.6.2 yamt * If called with opflags == CLOPS_RENEW, the State Lock is
429 1.2.6.2 yamt * already held. Otherwise, we need to get either that or,
430 1.2.6.2 yamt * for the case of Confirm, lock out the nfsd threads.
431 1.2.6.2 yamt */
432 1.2.6.2 yamt if (opflags & CLOPS_CONFIRM) {
433 1.2.6.2 yamt NFSLOCKV4ROOTMUTEX();
434 1.2.6.2 yamt nfsv4_relref(&nfsv4rootfs_lock);
435 1.2.6.2 yamt do {
436 1.2.6.2 yamt igotlock = nfsv4_lock(&nfsv4rootfs_lock, 1, NULL,
437 1.2.6.2 yamt NFSV4ROOTLOCKMUTEXPTR, NULL);
438 1.2.6.2 yamt } while (!igotlock);
439 1.2.6.2 yamt NFSUNLOCKV4ROOTMUTEX();
440 1.2.6.2 yamt } else if (opflags != CLOPS_RENEW) {
441 1.2.6.2 yamt NFSLOCKSTATE();
442 1.2.6.2 yamt }
443 1.2.6.2 yamt
444 1.2.6.2 yamt hp = NFSCLIENTHASH(clientid);
445 1.2.6.2 yamt LIST_FOREACH(clp, hp, lc_hash) {
446 1.2.6.2 yamt if (clp->lc_clientid.lval[1] == clientid.lval[1])
447 1.2.6.2 yamt break;
448 1.2.6.2 yamt }
449 1.2.6.2 yamt if (clp == NULL) {
450 1.2.6.2 yamt if (opflags & CLOPS_CONFIRM)
451 1.2.6.2 yamt error = NFSERR_STALECLIENTID;
452 1.2.6.2 yamt else
453 1.2.6.2 yamt error = NFSERR_EXPIRED;
454 1.2.6.2 yamt } else if (clp->lc_flags & LCL_ADMINREVOKED) {
455 1.2.6.2 yamt /*
456 1.2.6.2 yamt * If marked admin revoked, just return the error.
457 1.2.6.2 yamt */
458 1.2.6.2 yamt error = NFSERR_ADMINREVOKED;
459 1.2.6.2 yamt }
460 1.2.6.2 yamt if (error) {
461 1.2.6.2 yamt if (opflags & CLOPS_CONFIRM) {
462 1.2.6.2 yamt NFSLOCKV4ROOTMUTEX();
463 1.2.6.2 yamt nfsv4_unlock(&nfsv4rootfs_lock, 1);
464 1.2.6.2 yamt NFSUNLOCKV4ROOTMUTEX();
465 1.2.6.2 yamt } else if (opflags != CLOPS_RENEW) {
466 1.2.6.2 yamt NFSUNLOCKSTATE();
467 1.2.6.2 yamt }
468 1.2.6.2 yamt goto out;
469 1.2.6.2 yamt }
470 1.2.6.2 yamt
471 1.2.6.2 yamt /*
472 1.2.6.2 yamt * Perform any operations specified by the opflags.
473 1.2.6.2 yamt */
474 1.2.6.2 yamt if (opflags & CLOPS_CONFIRM) {
475 1.2.6.2 yamt if (clp->lc_confirm.qval != confirm.qval)
476 1.2.6.2 yamt error = NFSERR_STALECLIENTID;
477 1.2.6.2 yamt else if (nfsrv_notsamecredname(nd, clp))
478 1.2.6.2 yamt error = NFSERR_CLIDINUSE;
479 1.2.6.2 yamt
480 1.2.6.2 yamt if (!error) {
481 1.2.6.2 yamt if ((clp->lc_flags & (LCL_NEEDSCONFIRM | LCL_DONTCLEAN)) ==
482 1.2.6.2 yamt LCL_NEEDSCONFIRM) {
483 1.2.6.2 yamt /*
484 1.2.6.2 yamt * Hang onto the delegations (as old delegations)
485 1.2.6.2 yamt * for an Open with CLAIM_DELEGATE_PREV unless in
486 1.2.6.2 yamt * grace, but get rid of the rest of the state.
487 1.2.6.2 yamt */
488 1.2.6.2 yamt nfsrv_cleanclient(clp, p);
489 1.2.6.2 yamt nfsrv_freedeleglist(&clp->lc_olddeleg);
490 1.2.6.2 yamt if (nfsrv_checkgrace(0)) {
491 1.2.6.2 yamt /* In grace, so just delete delegations */
492 1.2.6.2 yamt nfsrv_freedeleglist(&clp->lc_deleg);
493 1.2.6.2 yamt } else {
494 1.2.6.2 yamt LIST_FOREACH(stp, &clp->lc_deleg, ls_list)
495 1.2.6.2 yamt stp->ls_flags |= NFSLCK_OLDDELEG;
496 1.2.6.2 yamt clp->lc_delegtime = NFSD_MONOSEC +
497 1.2.6.2 yamt nfsrv_lease + NFSRV_LEASEDELTA;
498 1.2.6.2 yamt LIST_NEWHEAD(&clp->lc_olddeleg, &clp->lc_deleg,
499 1.2.6.2 yamt ls_list);
500 1.2.6.2 yamt }
501 1.2.6.2 yamt }
502 1.2.6.2 yamt clp->lc_flags &= ~(LCL_NEEDSCONFIRM | LCL_DONTCLEAN);
503 1.2.6.2 yamt if (clp->lc_program)
504 1.2.6.2 yamt clp->lc_flags |= LCL_NEEDSCBNULL;
505 1.2.6.2 yamt }
506 1.2.6.2 yamt } else if (clp->lc_flags & LCL_NEEDSCONFIRM) {
507 1.2.6.2 yamt error = NFSERR_EXPIRED;
508 1.2.6.2 yamt }
509 1.2.6.2 yamt
510 1.2.6.2 yamt /*
511 1.2.6.2 yamt * If called by the Renew Op, we must check the principal.
512 1.2.6.2 yamt */
513 1.2.6.2 yamt if (!error && (opflags & CLOPS_RENEWOP)) {
514 1.2.6.2 yamt if (nfsrv_notsamecredname(nd, clp)) {
515 1.2.6.2 yamt doneok = 0;
516 1.2.6.2 yamt for (i = 0; i < NFSSTATEHASHSIZE && doneok == 0; i++) {
517 1.2.6.2 yamt LIST_FOREACH(stp, &clp->lc_stateid[i], ls_hash) {
518 1.2.6.2 yamt if ((stp->ls_flags & NFSLCK_OPEN) &&
519 1.2.6.2 yamt stp->ls_uid == nd->nd_cred->cr_uid) {
520 1.2.6.2 yamt doneok = 1;
521 1.2.6.2 yamt break;
522 1.2.6.2 yamt }
523 1.2.6.2 yamt }
524 1.2.6.2 yamt }
525 1.2.6.2 yamt if (!doneok)
526 1.2.6.2 yamt error = NFSERR_ACCES;
527 1.2.6.2 yamt }
528 1.2.6.2 yamt if (!error && (clp->lc_flags & LCL_CBDOWN))
529 1.2.6.2 yamt error = NFSERR_CBPATHDOWN;
530 1.2.6.2 yamt }
531 1.2.6.2 yamt if ((!error || error == NFSERR_CBPATHDOWN) &&
532 1.2.6.2 yamt (opflags & CLOPS_RENEW)) {
533 1.2.6.2 yamt clp->lc_expiry = nfsrv_leaseexpiry();
534 1.2.6.2 yamt }
535 1.2.6.2 yamt if (opflags & CLOPS_CONFIRM) {
536 1.2.6.2 yamt NFSLOCKV4ROOTMUTEX();
537 1.2.6.2 yamt nfsv4_unlock(&nfsv4rootfs_lock, 1);
538 1.2.6.2 yamt NFSUNLOCKV4ROOTMUTEX();
539 1.2.6.2 yamt } else if (opflags != CLOPS_RENEW) {
540 1.2.6.2 yamt NFSUNLOCKSTATE();
541 1.2.6.2 yamt }
542 1.2.6.2 yamt if (clpp)
543 1.2.6.2 yamt *clpp = clp;
544 1.2.6.2 yamt
545 1.2.6.2 yamt out:
546 1.2.6.2 yamt NFSEXITCODE2(error, nd);
547 1.2.6.2 yamt return (error);
548 1.2.6.2 yamt }
549 1.2.6.2 yamt
550 1.2.6.2 yamt /*
551 1.2.6.2 yamt * Called from the new nfssvc syscall to admin revoke a clientid.
552 1.2.6.2 yamt * Returns 0 for success, error otherwise.
553 1.2.6.2 yamt */
554 1.2.6.2 yamt APPLESTATIC int
555 1.2.6.2 yamt nfsrv_adminrevoke(struct nfsd_clid *revokep, NFSPROC_T *p)
556 1.2.6.2 yamt {
557 1.2.6.2 yamt struct nfsclient *clp = NULL;
558 1.2.6.2 yamt int i, error = 0;
559 1.2.6.2 yamt int gotit, igotlock;
560 1.2.6.2 yamt
561 1.2.6.2 yamt /*
562 1.2.6.2 yamt * First, lock out the nfsd so that state won't change while the
563 1.2.6.2 yamt * revocation record is being written to the stable storage restart
564 1.2.6.2 yamt * file.
565 1.2.6.2 yamt */
566 1.2.6.2 yamt NFSLOCKV4ROOTMUTEX();
567 1.2.6.2 yamt do {
568 1.2.6.2 yamt igotlock = nfsv4_lock(&nfsv4rootfs_lock, 1, NULL,
569 1.2.6.2 yamt NFSV4ROOTLOCKMUTEXPTR, NULL);
570 1.2.6.2 yamt } while (!igotlock);
571 1.2.6.2 yamt NFSUNLOCKV4ROOTMUTEX();
572 1.2.6.2 yamt
573 1.2.6.2 yamt /*
574 1.2.6.2 yamt * Search for a match in the client list.
575 1.2.6.2 yamt */
576 1.2.6.2 yamt gotit = i = 0;
577 1.2.6.2 yamt while (i < NFSCLIENTHASHSIZE && !gotit) {
578 1.2.6.2 yamt LIST_FOREACH(clp, &nfsclienthash[i], lc_hash) {
579 1.2.6.2 yamt if (revokep->nclid_idlen == clp->lc_idlen &&
580 1.2.6.2 yamt !NFSBCMP(revokep->nclid_id, clp->lc_id, clp->lc_idlen)) {
581 1.2.6.2 yamt gotit = 1;
582 1.2.6.2 yamt break;
583 1.2.6.2 yamt }
584 1.2.6.2 yamt }
585 1.2.6.2 yamt i++;
586 1.2.6.2 yamt }
587 1.2.6.2 yamt if (!gotit) {
588 1.2.6.2 yamt NFSLOCKV4ROOTMUTEX();
589 1.2.6.2 yamt nfsv4_unlock(&nfsv4rootfs_lock, 0);
590 1.2.6.2 yamt NFSUNLOCKV4ROOTMUTEX();
591 1.2.6.2 yamt error = EPERM;
592 1.2.6.2 yamt goto out;
593 1.2.6.2 yamt }
594 1.2.6.2 yamt
595 1.2.6.2 yamt /*
596 1.2.6.2 yamt * Now, write out the revocation record
597 1.2.6.2 yamt */
598 1.2.6.2 yamt nfsrv_writestable(clp->lc_id, clp->lc_idlen, NFSNST_REVOKE, p);
599 1.2.6.2 yamt nfsrv_backupstable();
600 1.2.6.2 yamt
601 1.2.6.2 yamt /*
602 1.2.6.2 yamt * and clear out the state, marking the clientid revoked.
603 1.2.6.2 yamt */
604 1.2.6.2 yamt clp->lc_flags &= ~LCL_CALLBACKSON;
605 1.2.6.2 yamt clp->lc_flags |= LCL_ADMINREVOKED;
606 1.2.6.2 yamt nfsrv_cleanclient(clp, p);
607 1.2.6.2 yamt nfsrv_freedeleglist(&clp->lc_deleg);
608 1.2.6.2 yamt nfsrv_freedeleglist(&clp->lc_olddeleg);
609 1.2.6.2 yamt NFSLOCKV4ROOTMUTEX();
610 1.2.6.2 yamt nfsv4_unlock(&nfsv4rootfs_lock, 0);
611 1.2.6.2 yamt NFSUNLOCKV4ROOTMUTEX();
612 1.2.6.2 yamt
613 1.2.6.2 yamt out:
614 1.2.6.2 yamt NFSEXITCODE(error);
615 1.2.6.2 yamt return (error);
616 1.2.6.2 yamt }
617 1.2.6.2 yamt
618 1.2.6.2 yamt /*
619 1.2.6.2 yamt * Dump out stats for all clients. Called from nfssvc(2), that is used
620 1.2.6.2 yamt * newnfsstats.
621 1.2.6.2 yamt */
622 1.2.6.2 yamt APPLESTATIC void
623 1.2.6.2 yamt nfsrv_dumpclients(struct nfsd_dumpclients *dumpp, int maxcnt)
624 1.2.6.2 yamt {
625 1.2.6.2 yamt struct nfsclient *clp;
626 1.2.6.2 yamt int i = 0, cnt = 0;
627 1.2.6.2 yamt
628 1.2.6.2 yamt /*
629 1.2.6.2 yamt * First, get a reference on the nfsv4rootfs_lock so that an
630 1.2.6.2 yamt * exclusive lock cannot be acquired while dumping the clients.
631 1.2.6.2 yamt */
632 1.2.6.2 yamt NFSLOCKV4ROOTMUTEX();
633 1.2.6.2 yamt nfsv4_getref(&nfsv4rootfs_lock, NULL, NFSV4ROOTLOCKMUTEXPTR, NULL);
634 1.2.6.2 yamt NFSUNLOCKV4ROOTMUTEX();
635 1.2.6.2 yamt NFSLOCKSTATE();
636 1.2.6.2 yamt /*
637 1.2.6.2 yamt * Rattle through the client lists until done.
638 1.2.6.2 yamt */
639 1.2.6.2 yamt while (i < NFSCLIENTHASHSIZE && cnt < maxcnt) {
640 1.2.6.2 yamt clp = LIST_FIRST(&nfsclienthash[i]);
641 1.2.6.2 yamt while (clp != NULL && cnt < maxcnt) {
642 1.2.6.2 yamt nfsrv_dumpaclient(clp, &dumpp[cnt]);
643 1.2.6.2 yamt cnt++;
644 1.2.6.2 yamt clp = LIST_NEXT(clp, lc_hash);
645 1.2.6.2 yamt }
646 1.2.6.2 yamt i++;
647 1.2.6.2 yamt }
648 1.2.6.2 yamt if (cnt < maxcnt)
649 1.2.6.2 yamt dumpp[cnt].ndcl_clid.nclid_idlen = 0;
650 1.2.6.2 yamt NFSUNLOCKSTATE();
651 1.2.6.2 yamt NFSLOCKV4ROOTMUTEX();
652 1.2.6.2 yamt nfsv4_relref(&nfsv4rootfs_lock);
653 1.2.6.2 yamt NFSUNLOCKV4ROOTMUTEX();
654 1.2.6.2 yamt }
655 1.2.6.2 yamt
656 1.2.6.2 yamt /*
657 1.2.6.2 yamt * Dump stats for a client. Must be called with the NFSSTATELOCK and spl'd.
658 1.2.6.2 yamt */
659 1.2.6.2 yamt static void
660 1.2.6.2 yamt nfsrv_dumpaclient(struct nfsclient *clp, struct nfsd_dumpclients *dumpp)
661 1.2.6.2 yamt {
662 1.2.6.2 yamt struct nfsstate *stp, *openstp, *lckownstp;
663 1.2.6.2 yamt struct nfslock *lop;
664 1.2.6.2 yamt struct sockaddr *sad;
665 1.2.6.2 yamt struct sockaddr_in *rad;
666 1.2.6.2 yamt struct sockaddr_in6 *rad6;
667 1.2.6.2 yamt
668 1.2.6.2 yamt dumpp->ndcl_nopenowners = dumpp->ndcl_nlockowners = 0;
669 1.2.6.2 yamt dumpp->ndcl_nopens = dumpp->ndcl_nlocks = 0;
670 1.2.6.2 yamt dumpp->ndcl_ndelegs = dumpp->ndcl_nolddelegs = 0;
671 1.2.6.2 yamt dumpp->ndcl_flags = clp->lc_flags;
672 1.2.6.2 yamt dumpp->ndcl_clid.nclid_idlen = clp->lc_idlen;
673 1.2.6.2 yamt NFSBCOPY(clp->lc_id, dumpp->ndcl_clid.nclid_id, clp->lc_idlen);
674 1.2.6.2 yamt sad = NFSSOCKADDR(clp->lc_req.nr_nam, struct sockaddr *);
675 1.2.6.2 yamt dumpp->ndcl_addrfam = sad->sa_family;
676 1.2.6.2 yamt if (sad->sa_family == AF_INET) {
677 1.2.6.2 yamt rad = (struct sockaddr_in *)sad;
678 1.2.6.2 yamt dumpp->ndcl_cbaddr.sin_addr = rad->sin_addr;
679 1.2.6.2 yamt } else {
680 1.2.6.2 yamt rad6 = (struct sockaddr_in6 *)sad;
681 1.2.6.2 yamt dumpp->ndcl_cbaddr.sin6_addr = rad6->sin6_addr;
682 1.2.6.2 yamt }
683 1.2.6.2 yamt
684 1.2.6.2 yamt /*
685 1.2.6.2 yamt * Now, scan the state lists and total up the opens and locks.
686 1.2.6.2 yamt */
687 1.2.6.2 yamt LIST_FOREACH(stp, &clp->lc_open, ls_list) {
688 1.2.6.2 yamt dumpp->ndcl_nopenowners++;
689 1.2.6.2 yamt LIST_FOREACH(openstp, &stp->ls_open, ls_list) {
690 1.2.6.2 yamt dumpp->ndcl_nopens++;
691 1.2.6.2 yamt LIST_FOREACH(lckownstp, &openstp->ls_open, ls_list) {
692 1.2.6.2 yamt dumpp->ndcl_nlockowners++;
693 1.2.6.2 yamt LIST_FOREACH(lop, &lckownstp->ls_lock, lo_lckowner) {
694 1.2.6.2 yamt dumpp->ndcl_nlocks++;
695 1.2.6.2 yamt }
696 1.2.6.2 yamt }
697 1.2.6.2 yamt }
698 1.2.6.2 yamt }
699 1.2.6.2 yamt
700 1.2.6.2 yamt /*
701 1.2.6.2 yamt * and the delegation lists.
702 1.2.6.2 yamt */
703 1.2.6.2 yamt LIST_FOREACH(stp, &clp->lc_deleg, ls_list) {
704 1.2.6.2 yamt dumpp->ndcl_ndelegs++;
705 1.2.6.2 yamt }
706 1.2.6.2 yamt LIST_FOREACH(stp, &clp->lc_olddeleg, ls_list) {
707 1.2.6.2 yamt dumpp->ndcl_nolddelegs++;
708 1.2.6.2 yamt }
709 1.2.6.2 yamt }
710 1.2.6.2 yamt
711 1.2.6.2 yamt /*
712 1.2.6.2 yamt * Dump out lock stats for a file.
713 1.2.6.2 yamt */
714 1.2.6.2 yamt APPLESTATIC void
715 1.2.6.2 yamt nfsrv_dumplocks(vnode_t vp, struct nfsd_dumplocks *ldumpp, int maxcnt,
716 1.2.6.2 yamt NFSPROC_T *p)
717 1.2.6.2 yamt {
718 1.2.6.2 yamt struct nfsstate *stp;
719 1.2.6.2 yamt struct nfslock *lop;
720 1.2.6.2 yamt int cnt = 0;
721 1.2.6.2 yamt struct nfslockfile *lfp;
722 1.2.6.2 yamt struct sockaddr *sad;
723 1.2.6.2 yamt struct sockaddr_in *rad;
724 1.2.6.2 yamt struct sockaddr_in6 *rad6;
725 1.2.6.2 yamt int ret;
726 1.2.6.2 yamt fhandle_t nfh;
727 1.2.6.2 yamt
728 1.2.6.2 yamt ret = nfsrv_getlockfh(vp, 0, NULL, &nfh, p);
729 1.2.6.2 yamt /*
730 1.2.6.2 yamt * First, get a reference on the nfsv4rootfs_lock so that an
731 1.2.6.2 yamt * exclusive lock on it cannot be acquired while dumping the locks.
732 1.2.6.2 yamt */
733 1.2.6.2 yamt NFSLOCKV4ROOTMUTEX();
734 1.2.6.2 yamt nfsv4_getref(&nfsv4rootfs_lock, NULL, NFSV4ROOTLOCKMUTEXPTR, NULL);
735 1.2.6.2 yamt NFSUNLOCKV4ROOTMUTEX();
736 1.2.6.2 yamt NFSLOCKSTATE();
737 1.2.6.2 yamt if (!ret)
738 1.2.6.2 yamt ret = nfsrv_getlockfile(0, NULL, &lfp, &nfh, 0);
739 1.2.6.2 yamt if (ret) {
740 1.2.6.2 yamt ldumpp[0].ndlck_clid.nclid_idlen = 0;
741 1.2.6.2 yamt NFSUNLOCKSTATE();
742 1.2.6.2 yamt NFSLOCKV4ROOTMUTEX();
743 1.2.6.2 yamt nfsv4_relref(&nfsv4rootfs_lock);
744 1.2.6.2 yamt NFSUNLOCKV4ROOTMUTEX();
745 1.2.6.2 yamt return;
746 1.2.6.2 yamt }
747 1.2.6.2 yamt
748 1.2.6.2 yamt /*
749 1.2.6.2 yamt * For each open share on file, dump it out.
750 1.2.6.2 yamt */
751 1.2.6.2 yamt stp = LIST_FIRST(&lfp->lf_open);
752 1.2.6.2 yamt while (stp != NULL && cnt < maxcnt) {
753 1.2.6.2 yamt ldumpp[cnt].ndlck_flags = stp->ls_flags;
754 1.2.6.2 yamt ldumpp[cnt].ndlck_stateid.seqid = stp->ls_stateid.seqid;
755 1.2.6.2 yamt ldumpp[cnt].ndlck_stateid.other[0] = stp->ls_stateid.other[0];
756 1.2.6.2 yamt ldumpp[cnt].ndlck_stateid.other[1] = stp->ls_stateid.other[1];
757 1.2.6.2 yamt ldumpp[cnt].ndlck_stateid.other[2] = stp->ls_stateid.other[2];
758 1.2.6.2 yamt ldumpp[cnt].ndlck_owner.nclid_idlen =
759 1.2.6.2 yamt stp->ls_openowner->ls_ownerlen;
760 1.2.6.2 yamt NFSBCOPY(stp->ls_openowner->ls_owner,
761 1.2.6.2 yamt ldumpp[cnt].ndlck_owner.nclid_id,
762 1.2.6.2 yamt stp->ls_openowner->ls_ownerlen);
763 1.2.6.2 yamt ldumpp[cnt].ndlck_clid.nclid_idlen = stp->ls_clp->lc_idlen;
764 1.2.6.2 yamt NFSBCOPY(stp->ls_clp->lc_id, ldumpp[cnt].ndlck_clid.nclid_id,
765 1.2.6.2 yamt stp->ls_clp->lc_idlen);
766 1.2.6.2 yamt sad=NFSSOCKADDR(stp->ls_clp->lc_req.nr_nam, struct sockaddr *);
767 1.2.6.2 yamt ldumpp[cnt].ndlck_addrfam = sad->sa_family;
768 1.2.6.2 yamt if (sad->sa_family == AF_INET) {
769 1.2.6.2 yamt rad = (struct sockaddr_in *)sad;
770 1.2.6.2 yamt ldumpp[cnt].ndlck_cbaddr.sin_addr = rad->sin_addr;
771 1.2.6.2 yamt } else {
772 1.2.6.2 yamt rad6 = (struct sockaddr_in6 *)sad;
773 1.2.6.2 yamt ldumpp[cnt].ndlck_cbaddr.sin6_addr = rad6->sin6_addr;
774 1.2.6.2 yamt }
775 1.2.6.2 yamt stp = LIST_NEXT(stp, ls_file);
776 1.2.6.2 yamt cnt++;
777 1.2.6.2 yamt }
778 1.2.6.2 yamt
779 1.2.6.2 yamt /*
780 1.2.6.2 yamt * and all locks.
781 1.2.6.2 yamt */
782 1.2.6.2 yamt lop = LIST_FIRST(&lfp->lf_lock);
783 1.2.6.2 yamt while (lop != NULL && cnt < maxcnt) {
784 1.2.6.2 yamt stp = lop->lo_stp;
785 1.2.6.2 yamt ldumpp[cnt].ndlck_flags = lop->lo_flags;
786 1.2.6.2 yamt ldumpp[cnt].ndlck_first = lop->lo_first;
787 1.2.6.2 yamt ldumpp[cnt].ndlck_end = lop->lo_end;
788 1.2.6.2 yamt ldumpp[cnt].ndlck_stateid.seqid = stp->ls_stateid.seqid;
789 1.2.6.2 yamt ldumpp[cnt].ndlck_stateid.other[0] = stp->ls_stateid.other[0];
790 1.2.6.2 yamt ldumpp[cnt].ndlck_stateid.other[1] = stp->ls_stateid.other[1];
791 1.2.6.2 yamt ldumpp[cnt].ndlck_stateid.other[2] = stp->ls_stateid.other[2];
792 1.2.6.2 yamt ldumpp[cnt].ndlck_owner.nclid_idlen = stp->ls_ownerlen;
793 1.2.6.2 yamt NFSBCOPY(stp->ls_owner, ldumpp[cnt].ndlck_owner.nclid_id,
794 1.2.6.2 yamt stp->ls_ownerlen);
795 1.2.6.2 yamt ldumpp[cnt].ndlck_clid.nclid_idlen = stp->ls_clp->lc_idlen;
796 1.2.6.2 yamt NFSBCOPY(stp->ls_clp->lc_id, ldumpp[cnt].ndlck_clid.nclid_id,
797 1.2.6.2 yamt stp->ls_clp->lc_idlen);
798 1.2.6.2 yamt sad=NFSSOCKADDR(stp->ls_clp->lc_req.nr_nam, struct sockaddr *);
799 1.2.6.2 yamt ldumpp[cnt].ndlck_addrfam = sad->sa_family;
800 1.2.6.2 yamt if (sad->sa_family == AF_INET) {
801 1.2.6.2 yamt rad = (struct sockaddr_in *)sad;
802 1.2.6.2 yamt ldumpp[cnt].ndlck_cbaddr.sin_addr = rad->sin_addr;
803 1.2.6.2 yamt } else {
804 1.2.6.2 yamt rad6 = (struct sockaddr_in6 *)sad;
805 1.2.6.2 yamt ldumpp[cnt].ndlck_cbaddr.sin6_addr = rad6->sin6_addr;
806 1.2.6.2 yamt }
807 1.2.6.2 yamt lop = LIST_NEXT(lop, lo_lckfile);
808 1.2.6.2 yamt cnt++;
809 1.2.6.2 yamt }
810 1.2.6.2 yamt
811 1.2.6.2 yamt /*
812 1.2.6.2 yamt * and the delegations.
813 1.2.6.2 yamt */
814 1.2.6.2 yamt stp = LIST_FIRST(&lfp->lf_deleg);
815 1.2.6.2 yamt while (stp != NULL && cnt < maxcnt) {
816 1.2.6.2 yamt ldumpp[cnt].ndlck_flags = stp->ls_flags;
817 1.2.6.2 yamt ldumpp[cnt].ndlck_stateid.seqid = stp->ls_stateid.seqid;
818 1.2.6.2 yamt ldumpp[cnt].ndlck_stateid.other[0] = stp->ls_stateid.other[0];
819 1.2.6.2 yamt ldumpp[cnt].ndlck_stateid.other[1] = stp->ls_stateid.other[1];
820 1.2.6.2 yamt ldumpp[cnt].ndlck_stateid.other[2] = stp->ls_stateid.other[2];
821 1.2.6.2 yamt ldumpp[cnt].ndlck_owner.nclid_idlen = 0;
822 1.2.6.2 yamt ldumpp[cnt].ndlck_clid.nclid_idlen = stp->ls_clp->lc_idlen;
823 1.2.6.2 yamt NFSBCOPY(stp->ls_clp->lc_id, ldumpp[cnt].ndlck_clid.nclid_id,
824 1.2.6.2 yamt stp->ls_clp->lc_idlen);
825 1.2.6.2 yamt sad=NFSSOCKADDR(stp->ls_clp->lc_req.nr_nam, struct sockaddr *);
826 1.2.6.2 yamt ldumpp[cnt].ndlck_addrfam = sad->sa_family;
827 1.2.6.2 yamt if (sad->sa_family == AF_INET) {
828 1.2.6.2 yamt rad = (struct sockaddr_in *)sad;
829 1.2.6.2 yamt ldumpp[cnt].ndlck_cbaddr.sin_addr = rad->sin_addr;
830 1.2.6.2 yamt } else {
831 1.2.6.2 yamt rad6 = (struct sockaddr_in6 *)sad;
832 1.2.6.2 yamt ldumpp[cnt].ndlck_cbaddr.sin6_addr = rad6->sin6_addr;
833 1.2.6.2 yamt }
834 1.2.6.2 yamt stp = LIST_NEXT(stp, ls_file);
835 1.2.6.2 yamt cnt++;
836 1.2.6.2 yamt }
837 1.2.6.2 yamt
838 1.2.6.2 yamt /*
839 1.2.6.2 yamt * If list isn't full, mark end of list by setting the client name
840 1.2.6.2 yamt * to zero length.
841 1.2.6.2 yamt */
842 1.2.6.2 yamt if (cnt < maxcnt)
843 1.2.6.2 yamt ldumpp[cnt].ndlck_clid.nclid_idlen = 0;
844 1.2.6.2 yamt NFSUNLOCKSTATE();
845 1.2.6.2 yamt NFSLOCKV4ROOTMUTEX();
846 1.2.6.2 yamt nfsv4_relref(&nfsv4rootfs_lock);
847 1.2.6.2 yamt NFSUNLOCKV4ROOTMUTEX();
848 1.2.6.2 yamt }
849 1.2.6.2 yamt
850 1.2.6.2 yamt /*
851 1.2.6.2 yamt * Server timer routine. It can scan any linked list, so long
852 1.2.6.2 yamt * as it holds the spin/mutex lock and there is no exclusive lock on
853 1.2.6.2 yamt * nfsv4rootfs_lock.
854 1.2.6.2 yamt * (For OpenBSD, a kthread is ok. For FreeBSD, I think it is ok
855 1.2.6.2 yamt * to do this from a callout, since the spin locks work. For
856 1.2.6.2 yamt * Darwin, I'm not sure what will work correctly yet.)
857 1.2.6.2 yamt * Should be called once per second.
858 1.2.6.2 yamt */
859 1.2.6.2 yamt APPLESTATIC void
860 1.2.6.2 yamt nfsrv_servertimer(void)
861 1.2.6.2 yamt {
862 1.2.6.2 yamt struct nfsclient *clp, *nclp;
863 1.2.6.2 yamt struct nfsstate *stp, *nstp;
864 1.2.6.2 yamt int got_ref, i;
865 1.2.6.2 yamt
866 1.2.6.2 yamt /*
867 1.2.6.2 yamt * Make sure nfsboottime is set. This is used by V3 as well
868 1.2.6.2 yamt * as V4. Note that nfsboottime is not nfsrvboottime, which is
869 1.2.6.2 yamt * only used by the V4 server for leases.
870 1.2.6.2 yamt */
871 1.2.6.2 yamt if (nfsboottime.tv_sec == 0)
872 1.2.6.2 yamt NFSSETBOOTTIME(nfsboottime);
873 1.2.6.2 yamt
874 1.2.6.2 yamt /*
875 1.2.6.2 yamt * If server hasn't started yet, just return.
876 1.2.6.2 yamt */
877 1.2.6.2 yamt NFSLOCKSTATE();
878 1.2.6.2 yamt if (nfsrv_stablefirst.nsf_eograce == 0) {
879 1.2.6.2 yamt NFSUNLOCKSTATE();
880 1.2.6.2 yamt return;
881 1.2.6.2 yamt }
882 1.2.6.2 yamt if (!(nfsrv_stablefirst.nsf_flags & NFSNSF_UPDATEDONE)) {
883 1.2.6.2 yamt if (!(nfsrv_stablefirst.nsf_flags & NFSNSF_GRACEOVER) &&
884 1.2.6.2 yamt NFSD_MONOSEC > nfsrv_stablefirst.nsf_eograce)
885 1.2.6.2 yamt nfsrv_stablefirst.nsf_flags |=
886 1.2.6.2 yamt (NFSNSF_GRACEOVER | NFSNSF_NEEDLOCK);
887 1.2.6.2 yamt NFSUNLOCKSTATE();
888 1.2.6.2 yamt return;
889 1.2.6.2 yamt }
890 1.2.6.2 yamt
891 1.2.6.2 yamt /*
892 1.2.6.2 yamt * Try and get a reference count on the nfsv4rootfs_lock so that
893 1.2.6.2 yamt * no nfsd thread can acquire an exclusive lock on it before this
894 1.2.6.2 yamt * call is done. If it is already exclusively locked, just return.
895 1.2.6.2 yamt */
896 1.2.6.2 yamt NFSLOCKV4ROOTMUTEX();
897 1.2.6.2 yamt got_ref = nfsv4_getref_nonblock(&nfsv4rootfs_lock);
898 1.2.6.2 yamt NFSUNLOCKV4ROOTMUTEX();
899 1.2.6.2 yamt if (got_ref == 0) {
900 1.2.6.2 yamt NFSUNLOCKSTATE();
901 1.2.6.2 yamt return;
902 1.2.6.2 yamt }
903 1.2.6.2 yamt
904 1.2.6.2 yamt /*
905 1.2.6.2 yamt * For each client...
906 1.2.6.2 yamt */
907 1.2.6.2 yamt for (i = 0; i < NFSCLIENTHASHSIZE; i++) {
908 1.2.6.2 yamt clp = LIST_FIRST(&nfsclienthash[i]);
909 1.2.6.2 yamt while (clp != NULL) {
910 1.2.6.2 yamt nclp = LIST_NEXT(clp, lc_hash);
911 1.2.6.2 yamt if (!(clp->lc_flags & LCL_EXPIREIT)) {
912 1.2.6.2 yamt if (((clp->lc_expiry + NFSRV_STALELEASE) < NFSD_MONOSEC
913 1.2.6.2 yamt && ((LIST_EMPTY(&clp->lc_deleg)
914 1.2.6.2 yamt && LIST_EMPTY(&clp->lc_open)) ||
915 1.2.6.2 yamt nfsrv_clients > nfsrv_clienthighwater)) ||
916 1.2.6.2 yamt (clp->lc_expiry + NFSRV_MOULDYLEASE) < NFSD_MONOSEC ||
917 1.2.6.2 yamt (clp->lc_expiry < NFSD_MONOSEC &&
918 1.2.6.2 yamt (nfsrv_openpluslock * 10 / 9) > NFSRV_V4STATELIMIT)) {
919 1.2.6.2 yamt /*
920 1.2.6.2 yamt * Lease has expired several nfsrv_lease times ago:
921 1.2.6.2 yamt * PLUS
922 1.2.6.2 yamt * - no state is associated with it
923 1.2.6.2 yamt * OR
924 1.2.6.2 yamt * - above high water mark for number of clients
925 1.2.6.2 yamt * (nfsrv_clienthighwater should be large enough
926 1.2.6.2 yamt * that this only occurs when clients fail to
927 1.2.6.2 yamt * use the same nfs_client_id4.id. Maybe somewhat
928 1.2.6.2 yamt * higher that the maximum number of clients that
929 1.2.6.2 yamt * will mount this server?)
930 1.2.6.2 yamt * OR
931 1.2.6.2 yamt * Lease has expired a very long time ago
932 1.2.6.2 yamt * OR
933 1.2.6.2 yamt * Lease has expired PLUS the number of opens + locks
934 1.2.6.2 yamt * has exceeded 90% of capacity
935 1.2.6.2 yamt *
936 1.2.6.2 yamt * --> Mark for expiry. The actual expiry will be done
937 1.2.6.2 yamt * by an nfsd sometime soon.
938 1.2.6.2 yamt */
939 1.2.6.2 yamt clp->lc_flags |= LCL_EXPIREIT;
940 1.2.6.2 yamt nfsrv_stablefirst.nsf_flags |=
941 1.2.6.2 yamt (NFSNSF_NEEDLOCK | NFSNSF_EXPIREDCLIENT);
942 1.2.6.2 yamt } else {
943 1.2.6.2 yamt /*
944 1.2.6.2 yamt * If there are no opens, increment no open tick cnt
945 1.2.6.2 yamt * If time exceeds NFSNOOPEN, mark it to be thrown away
946 1.2.6.2 yamt * otherwise, if there is an open, reset no open time
947 1.2.6.2 yamt * Hopefully, this will avoid excessive re-creation
948 1.2.6.2 yamt * of open owners and subsequent open confirms.
949 1.2.6.2 yamt */
950 1.2.6.2 yamt stp = LIST_FIRST(&clp->lc_open);
951 1.2.6.2 yamt while (stp != NULL) {
952 1.2.6.2 yamt nstp = LIST_NEXT(stp, ls_list);
953 1.2.6.2 yamt if (LIST_EMPTY(&stp->ls_open)) {
954 1.2.6.2 yamt stp->ls_noopens++;
955 1.2.6.2 yamt if (stp->ls_noopens > NFSNOOPEN ||
956 1.2.6.2 yamt (nfsrv_openpluslock * 2) >
957 1.2.6.2 yamt NFSRV_V4STATELIMIT)
958 1.2.6.2 yamt nfsrv_stablefirst.nsf_flags |=
959 1.2.6.2 yamt NFSNSF_NOOPENS;
960 1.2.6.2 yamt } else {
961 1.2.6.2 yamt stp->ls_noopens = 0;
962 1.2.6.2 yamt }
963 1.2.6.2 yamt stp = nstp;
964 1.2.6.2 yamt }
965 1.2.6.2 yamt }
966 1.2.6.2 yamt }
967 1.2.6.2 yamt clp = nclp;
968 1.2.6.2 yamt }
969 1.2.6.2 yamt }
970 1.2.6.2 yamt NFSUNLOCKSTATE();
971 1.2.6.2 yamt NFSLOCKV4ROOTMUTEX();
972 1.2.6.2 yamt nfsv4_relref(&nfsv4rootfs_lock);
973 1.2.6.2 yamt NFSUNLOCKV4ROOTMUTEX();
974 1.2.6.2 yamt }
975 1.2.6.2 yamt
976 1.2.6.2 yamt /*
977 1.2.6.2 yamt * The following set of functions free up the various data structures.
978 1.2.6.2 yamt */
979 1.2.6.2 yamt /*
980 1.2.6.2 yamt * Clear out all open/lock state related to this nfsclient.
981 1.2.6.2 yamt * Caller must hold an exclusive lock on nfsv4rootfs_lock, so that
982 1.2.6.2 yamt * there are no other active nfsd threads.
983 1.2.6.2 yamt */
984 1.2.6.2 yamt APPLESTATIC void
985 1.2.6.2 yamt nfsrv_cleanclient(struct nfsclient *clp, NFSPROC_T *p)
986 1.2.6.2 yamt {
987 1.2.6.2 yamt struct nfsstate *stp, *nstp;
988 1.2.6.2 yamt
989 1.2.6.2 yamt LIST_FOREACH_SAFE(stp, &clp->lc_open, ls_list, nstp)
990 1.2.6.2 yamt nfsrv_freeopenowner(stp, 1, p);
991 1.2.6.2 yamt }
992 1.2.6.2 yamt
993 1.2.6.2 yamt /*
994 1.2.6.2 yamt * Free a client that has been cleaned. It should also already have been
995 1.2.6.2 yamt * removed from the lists.
996 1.2.6.2 yamt * (Just to be safe w.r.t. newnfs_disconnect(), call this function when
997 1.2.6.2 yamt * softclock interrupts are enabled.)
998 1.2.6.2 yamt */
999 1.2.6.2 yamt APPLESTATIC void
1000 1.2.6.2 yamt nfsrv_zapclient(struct nfsclient *clp, NFSPROC_T *p)
1001 1.2.6.2 yamt {
1002 1.2.6.2 yamt
1003 1.2.6.2 yamt #ifdef notyet
1004 1.2.6.2 yamt if ((clp->lc_flags & (LCL_GSS | LCL_CALLBACKSON)) ==
1005 1.2.6.2 yamt (LCL_GSS | LCL_CALLBACKSON) &&
1006 1.2.6.2 yamt (clp->lc_hand.nfsh_flag & NFSG_COMPLETE) &&
1007 1.2.6.2 yamt clp->lc_handlelen > 0) {
1008 1.2.6.2 yamt clp->lc_hand.nfsh_flag &= ~NFSG_COMPLETE;
1009 1.2.6.2 yamt clp->lc_hand.nfsh_flag |= NFSG_DESTROYED;
1010 1.2.6.2 yamt (void) nfsrv_docallback(clp, NFSV4PROC_CBNULL,
1011 1.2.6.2 yamt NULL, 0, NULL, NULL, NULL, p);
1012 1.2.6.2 yamt }
1013 1.2.6.2 yamt #endif
1014 1.2.6.2 yamt newnfs_disconnect(&clp->lc_req);
1015 1.2.6.2 yamt NFSSOCKADDRFREE(clp->lc_req.nr_nam);
1016 1.2.6.2 yamt NFSFREEMUTEX(&clp->lc_req.nr_mtx);
1017 1.2.6.2 yamt free((caddr_t)clp, M_NFSDCLIENT);
1018 1.2.6.2 yamt NFSLOCKSTATE();
1019 1.2.6.2 yamt newnfsstats.srvclients--;
1020 1.2.6.2 yamt nfsrv_openpluslock--;
1021 1.2.6.2 yamt nfsrv_clients--;
1022 1.2.6.2 yamt NFSUNLOCKSTATE();
1023 1.2.6.2 yamt }
1024 1.2.6.2 yamt
1025 1.2.6.2 yamt /*
1026 1.2.6.2 yamt * Free a list of delegation state structures.
1027 1.2.6.2 yamt * (This function will also free all nfslockfile structures that no
1028 1.2.6.2 yamt * longer have associated state.)
1029 1.2.6.2 yamt */
1030 1.2.6.2 yamt APPLESTATIC void
1031 1.2.6.2 yamt nfsrv_freedeleglist(struct nfsstatehead *sthp)
1032 1.2.6.2 yamt {
1033 1.2.6.2 yamt struct nfsstate *stp, *nstp;
1034 1.2.6.2 yamt
1035 1.2.6.2 yamt LIST_FOREACH_SAFE(stp, sthp, ls_list, nstp) {
1036 1.2.6.2 yamt nfsrv_freedeleg(stp);
1037 1.2.6.2 yamt }
1038 1.2.6.2 yamt LIST_INIT(sthp);
1039 1.2.6.2 yamt }
1040 1.2.6.2 yamt
1041 1.2.6.2 yamt /*
1042 1.2.6.2 yamt * Free up a delegation.
1043 1.2.6.2 yamt */
1044 1.2.6.2 yamt static void
1045 1.2.6.2 yamt nfsrv_freedeleg(struct nfsstate *stp)
1046 1.2.6.2 yamt {
1047 1.2.6.2 yamt struct nfslockfile *lfp;
1048 1.2.6.2 yamt
1049 1.2.6.2 yamt LIST_REMOVE(stp, ls_hash);
1050 1.2.6.2 yamt LIST_REMOVE(stp, ls_list);
1051 1.2.6.2 yamt LIST_REMOVE(stp, ls_file);
1052 1.2.6.2 yamt lfp = stp->ls_lfp;
1053 1.2.6.2 yamt if (LIST_EMPTY(&lfp->lf_open) &&
1054 1.2.6.2 yamt LIST_EMPTY(&lfp->lf_lock) && LIST_EMPTY(&lfp->lf_deleg) &&
1055 1.2.6.2 yamt LIST_EMPTY(&lfp->lf_locallock) && LIST_EMPTY(&lfp->lf_rollback) &&
1056 1.2.6.2 yamt lfp->lf_usecount == 0 &&
1057 1.2.6.2 yamt nfsv4_testlock(&lfp->lf_locallock_lck) == 0)
1058 1.2.6.2 yamt nfsrv_freenfslockfile(lfp);
1059 1.2.6.2 yamt FREE((caddr_t)stp, M_NFSDSTATE);
1060 1.2.6.2 yamt newnfsstats.srvdelegates--;
1061 1.2.6.2 yamt nfsrv_openpluslock--;
1062 1.2.6.2 yamt nfsrv_delegatecnt--;
1063 1.2.6.2 yamt }
1064 1.2.6.2 yamt
1065 1.2.6.2 yamt /*
1066 1.2.6.2 yamt * This function frees an open owner and all associated opens.
1067 1.2.6.2 yamt */
1068 1.2.6.2 yamt static void
1069 1.2.6.2 yamt nfsrv_freeopenowner(struct nfsstate *stp, int cansleep, NFSPROC_T *p)
1070 1.2.6.2 yamt {
1071 1.2.6.2 yamt struct nfsstate *nstp, *tstp;
1072 1.2.6.2 yamt
1073 1.2.6.2 yamt LIST_REMOVE(stp, ls_list);
1074 1.2.6.2 yamt /*
1075 1.2.6.2 yamt * Now, free all associated opens.
1076 1.2.6.2 yamt */
1077 1.2.6.2 yamt nstp = LIST_FIRST(&stp->ls_open);
1078 1.2.6.2 yamt while (nstp != NULL) {
1079 1.2.6.2 yamt tstp = nstp;
1080 1.2.6.2 yamt nstp = LIST_NEXT(nstp, ls_list);
1081 1.2.6.2 yamt (void) nfsrv_freeopen(tstp, NULL, cansleep, p);
1082 1.2.6.2 yamt }
1083 1.2.6.2 yamt if (stp->ls_op)
1084 1.2.6.2 yamt nfsrvd_derefcache(stp->ls_op);
1085 1.2.6.2 yamt FREE((caddr_t)stp, M_NFSDSTATE);
1086 1.2.6.2 yamt newnfsstats.srvopenowners--;
1087 1.2.6.2 yamt nfsrv_openpluslock--;
1088 1.2.6.2 yamt }
1089 1.2.6.2 yamt
1090 1.2.6.2 yamt /*
1091 1.2.6.2 yamt * This function frees an open (nfsstate open structure) with all associated
1092 1.2.6.2 yamt * lock_owners and locks. It also frees the nfslockfile structure iff there
1093 1.2.6.2 yamt * are no other opens on the file.
1094 1.2.6.2 yamt * Returns 1 if it free'd the nfslockfile, 0 otherwise.
1095 1.2.6.2 yamt */
1096 1.2.6.2 yamt static int
1097 1.2.6.2 yamt nfsrv_freeopen(struct nfsstate *stp, vnode_t vp, int cansleep, NFSPROC_T *p)
1098 1.2.6.2 yamt {
1099 1.2.6.2 yamt struct nfsstate *nstp, *tstp;
1100 1.2.6.2 yamt struct nfslockfile *lfp;
1101 1.2.6.2 yamt int ret;
1102 1.2.6.2 yamt
1103 1.2.6.2 yamt LIST_REMOVE(stp, ls_hash);
1104 1.2.6.2 yamt LIST_REMOVE(stp, ls_list);
1105 1.2.6.2 yamt LIST_REMOVE(stp, ls_file);
1106 1.2.6.2 yamt
1107 1.2.6.2 yamt lfp = stp->ls_lfp;
1108 1.2.6.2 yamt /*
1109 1.2.6.2 yamt * Now, free all lockowners associated with this open.
1110 1.2.6.2 yamt */
1111 1.2.6.2 yamt LIST_FOREACH_SAFE(tstp, &stp->ls_open, ls_list, nstp)
1112 1.2.6.2 yamt nfsrv_freelockowner(tstp, vp, cansleep, p);
1113 1.2.6.2 yamt
1114 1.2.6.2 yamt /*
1115 1.2.6.2 yamt * The nfslockfile is freed here if there are no locks
1116 1.2.6.2 yamt * associated with the open.
1117 1.2.6.2 yamt * If there are locks associated with the open, the
1118 1.2.6.2 yamt * nfslockfile structure can be freed via nfsrv_freelockowner().
1119 1.2.6.2 yamt * Acquire the state mutex to avoid races with calls to
1120 1.2.6.2 yamt * nfsrv_getlockfile().
1121 1.2.6.2 yamt */
1122 1.2.6.2 yamt if (cansleep != 0)
1123 1.2.6.2 yamt NFSLOCKSTATE();
1124 1.2.6.2 yamt if (lfp != NULL && LIST_EMPTY(&lfp->lf_open) &&
1125 1.2.6.2 yamt LIST_EMPTY(&lfp->lf_deleg) && LIST_EMPTY(&lfp->lf_lock) &&
1126 1.2.6.2 yamt LIST_EMPTY(&lfp->lf_locallock) && LIST_EMPTY(&lfp->lf_rollback) &&
1127 1.2.6.2 yamt lfp->lf_usecount == 0 &&
1128 1.2.6.2 yamt (cansleep != 0 || nfsv4_testlock(&lfp->lf_locallock_lck) == 0)) {
1129 1.2.6.2 yamt nfsrv_freenfslockfile(lfp);
1130 1.2.6.2 yamt ret = 1;
1131 1.2.6.2 yamt } else
1132 1.2.6.2 yamt ret = 0;
1133 1.2.6.2 yamt if (cansleep != 0)
1134 1.2.6.2 yamt NFSUNLOCKSTATE();
1135 1.2.6.2 yamt FREE((caddr_t)stp, M_NFSDSTATE);
1136 1.2.6.2 yamt newnfsstats.srvopens--;
1137 1.2.6.2 yamt nfsrv_openpluslock--;
1138 1.2.6.2 yamt return (ret);
1139 1.2.6.2 yamt }
1140 1.2.6.2 yamt
1141 1.2.6.2 yamt /*
1142 1.2.6.2 yamt * Frees a lockowner and all associated locks.
1143 1.2.6.2 yamt */
1144 1.2.6.2 yamt static void
1145 1.2.6.2 yamt nfsrv_freelockowner(struct nfsstate *stp, vnode_t vp, int cansleep,
1146 1.2.6.2 yamt NFSPROC_T *p)
1147 1.2.6.2 yamt {
1148 1.2.6.2 yamt
1149 1.2.6.2 yamt LIST_REMOVE(stp, ls_hash);
1150 1.2.6.2 yamt LIST_REMOVE(stp, ls_list);
1151 1.2.6.2 yamt nfsrv_freeallnfslocks(stp, vp, cansleep, p);
1152 1.2.6.2 yamt if (stp->ls_op)
1153 1.2.6.2 yamt nfsrvd_derefcache(stp->ls_op);
1154 1.2.6.2 yamt FREE((caddr_t)stp, M_NFSDSTATE);
1155 1.2.6.2 yamt newnfsstats.srvlockowners--;
1156 1.2.6.2 yamt nfsrv_openpluslock--;
1157 1.2.6.2 yamt }
1158 1.2.6.2 yamt
1159 1.2.6.2 yamt /*
1160 1.2.6.2 yamt * Free all the nfs locks on a lockowner.
1161 1.2.6.2 yamt */
1162 1.2.6.2 yamt static void
1163 1.2.6.2 yamt nfsrv_freeallnfslocks(struct nfsstate *stp, vnode_t vp, int cansleep,
1164 1.2.6.2 yamt NFSPROC_T *p)
1165 1.2.6.2 yamt {
1166 1.2.6.2 yamt struct nfslock *lop, *nlop;
1167 1.2.6.2 yamt struct nfsrollback *rlp, *nrlp;
1168 1.2.6.2 yamt struct nfslockfile *lfp = NULL;
1169 1.2.6.2 yamt int gottvp = 0;
1170 1.2.6.2 yamt vnode_t tvp = NULL;
1171 1.2.6.2 yamt uint64_t first, end;
1172 1.2.6.2 yamt
1173 1.2.6.2 yamt lop = LIST_FIRST(&stp->ls_lock);
1174 1.2.6.2 yamt while (lop != NULL) {
1175 1.2.6.2 yamt nlop = LIST_NEXT(lop, lo_lckowner);
1176 1.2.6.2 yamt /*
1177 1.2.6.2 yamt * Since all locks should be for the same file, lfp should
1178 1.2.6.2 yamt * not change.
1179 1.2.6.2 yamt */
1180 1.2.6.2 yamt if (lfp == NULL)
1181 1.2.6.2 yamt lfp = lop->lo_lfp;
1182 1.2.6.2 yamt else if (lfp != lop->lo_lfp)
1183 1.2.6.2 yamt panic("allnfslocks");
1184 1.2.6.2 yamt /*
1185 1.2.6.2 yamt * If vp is NULL and cansleep != 0, a vnode must be acquired
1186 1.2.6.2 yamt * from the file handle. This only occurs when called from
1187 1.2.6.2 yamt * nfsrv_cleanclient().
1188 1.2.6.2 yamt */
1189 1.2.6.2 yamt if (gottvp == 0) {
1190 1.2.6.2 yamt if (nfsrv_dolocallocks == 0)
1191 1.2.6.2 yamt tvp = NULL;
1192 1.2.6.2 yamt else if (vp == NULL && cansleep != 0)
1193 1.2.6.2 yamt tvp = nfsvno_getvp(&lfp->lf_fh);
1194 1.2.6.2 yamt else
1195 1.2.6.2 yamt tvp = vp;
1196 1.2.6.2 yamt gottvp = 1;
1197 1.2.6.2 yamt }
1198 1.2.6.2 yamt
1199 1.2.6.2 yamt if (tvp != NULL) {
1200 1.2.6.2 yamt if (cansleep == 0)
1201 1.2.6.2 yamt panic("allnfs2");
1202 1.2.6.2 yamt first = lop->lo_first;
1203 1.2.6.2 yamt end = lop->lo_end;
1204 1.2.6.2 yamt nfsrv_freenfslock(lop);
1205 1.2.6.2 yamt nfsrv_localunlock(tvp, lfp, first, end, p);
1206 1.2.6.2 yamt LIST_FOREACH_SAFE(rlp, &lfp->lf_rollback, rlck_list,
1207 1.2.6.2 yamt nrlp)
1208 1.2.6.2 yamt free(rlp, M_NFSDROLLBACK);
1209 1.2.6.2 yamt LIST_INIT(&lfp->lf_rollback);
1210 1.2.6.2 yamt } else
1211 1.2.6.2 yamt nfsrv_freenfslock(lop);
1212 1.2.6.2 yamt lop = nlop;
1213 1.2.6.2 yamt }
1214 1.2.6.2 yamt if (vp == NULL && tvp != NULL)
1215 1.2.6.2 yamt vput(tvp);
1216 1.2.6.2 yamt }
1217 1.2.6.2 yamt
1218 1.2.6.2 yamt /*
1219 1.2.6.2 yamt * Free an nfslock structure.
1220 1.2.6.2 yamt */
1221 1.2.6.2 yamt static void
1222 1.2.6.2 yamt nfsrv_freenfslock(struct nfslock *lop)
1223 1.2.6.2 yamt {
1224 1.2.6.2 yamt
1225 1.2.6.2 yamt if (lop->lo_lckfile.le_prev != NULL) {
1226 1.2.6.2 yamt LIST_REMOVE(lop, lo_lckfile);
1227 1.2.6.2 yamt newnfsstats.srvlocks--;
1228 1.2.6.2 yamt nfsrv_openpluslock--;
1229 1.2.6.2 yamt }
1230 1.2.6.2 yamt LIST_REMOVE(lop, lo_lckowner);
1231 1.2.6.2 yamt FREE((caddr_t)lop, M_NFSDLOCK);
1232 1.2.6.2 yamt }
1233 1.2.6.2 yamt
1234 1.2.6.2 yamt /*
1235 1.2.6.2 yamt * This function frees an nfslockfile structure.
1236 1.2.6.2 yamt */
1237 1.2.6.2 yamt static void
1238 1.2.6.2 yamt nfsrv_freenfslockfile(struct nfslockfile *lfp)
1239 1.2.6.2 yamt {
1240 1.2.6.2 yamt
1241 1.2.6.2 yamt LIST_REMOVE(lfp, lf_hash);
1242 1.2.6.2 yamt FREE((caddr_t)lfp, M_NFSDLOCKFILE);
1243 1.2.6.2 yamt }
1244 1.2.6.2 yamt
1245 1.2.6.2 yamt /*
1246 1.2.6.2 yamt * This function looks up an nfsstate structure via stateid.
1247 1.2.6.2 yamt */
1248 1.2.6.2 yamt static int
1249 1.2.6.2 yamt nfsrv_getstate(struct nfsclient *clp, nfsv4stateid_t *stateidp, __unused u_int32_t flags,
1250 1.2.6.2 yamt struct nfsstate **stpp)
1251 1.2.6.2 yamt {
1252 1.2.6.2 yamt struct nfsstate *stp;
1253 1.2.6.2 yamt struct nfsstatehead *hp;
1254 1.2.6.2 yamt int error = 0;
1255 1.2.6.2 yamt
1256 1.2.6.2 yamt *stpp = NULL;
1257 1.2.6.2 yamt hp = NFSSTATEHASH(clp, *stateidp);
1258 1.2.6.2 yamt LIST_FOREACH(stp, hp, ls_hash) {
1259 1.2.6.2 yamt if (!NFSBCMP(stp->ls_stateid.other, stateidp->other,
1260 1.2.6.2 yamt NFSX_STATEIDOTHER))
1261 1.2.6.2 yamt break;
1262 1.2.6.2 yamt }
1263 1.2.6.2 yamt
1264 1.2.6.2 yamt /*
1265 1.2.6.2 yamt * If no state id in list, return NFSERR_BADSTATEID.
1266 1.2.6.2 yamt */
1267 1.2.6.2 yamt if (stp == NULL) {
1268 1.2.6.2 yamt error = NFSERR_BADSTATEID;
1269 1.2.6.2 yamt goto out;
1270 1.2.6.2 yamt }
1271 1.2.6.2 yamt *stpp = stp;
1272 1.2.6.2 yamt
1273 1.2.6.2 yamt out:
1274 1.2.6.2 yamt NFSEXITCODE(error);
1275 1.2.6.2 yamt return (error);
1276 1.2.6.2 yamt }
1277 1.2.6.2 yamt
1278 1.2.6.2 yamt /*
1279 1.2.6.2 yamt * This function gets an nfsstate structure via owner string.
1280 1.2.6.2 yamt */
1281 1.2.6.2 yamt static void
1282 1.2.6.2 yamt nfsrv_getowner(struct nfsstatehead *hp, struct nfsstate *new_stp,
1283 1.2.6.2 yamt struct nfsstate **stpp)
1284 1.2.6.2 yamt {
1285 1.2.6.2 yamt struct nfsstate *stp;
1286 1.2.6.2 yamt
1287 1.2.6.2 yamt *stpp = NULL;
1288 1.2.6.2 yamt LIST_FOREACH(stp, hp, ls_list) {
1289 1.2.6.2 yamt if (new_stp->ls_ownerlen == stp->ls_ownerlen &&
1290 1.2.6.2 yamt !NFSBCMP(new_stp->ls_owner,stp->ls_owner,stp->ls_ownerlen)) {
1291 1.2.6.2 yamt *stpp = stp;
1292 1.2.6.2 yamt return;
1293 1.2.6.2 yamt }
1294 1.2.6.2 yamt }
1295 1.2.6.2 yamt }
1296 1.2.6.2 yamt
1297 1.2.6.2 yamt /*
1298 1.2.6.2 yamt * Lock control function called to update lock status.
1299 1.2.6.2 yamt * Returns 0 upon success, -1 if there is no lock and the flags indicate
1300 1.2.6.2 yamt * that one isn't to be created and an NFSERR_xxx for other errors.
1301 1.2.6.2 yamt * The structures new_stp and new_lop are passed in as pointers that should
1302 1.2.6.2 yamt * be set to NULL if the structure is used and shouldn't be free'd.
1303 1.2.6.2 yamt * For the NFSLCK_TEST and NFSLCK_CHECK cases, the structures are
1304 1.2.6.2 yamt * never used and can safely be allocated on the stack. For all other
1305 1.2.6.2 yamt * cases, *new_stpp and *new_lopp should be malloc'd before the call,
1306 1.2.6.2 yamt * in case they are used.
1307 1.2.6.2 yamt */
1308 1.2.6.2 yamt APPLESTATIC int
1309 1.2.6.2 yamt nfsrv_lockctrl(vnode_t vp, struct nfsstate **new_stpp,
1310 1.2.6.2 yamt struct nfslock **new_lopp, struct nfslockconflict *cfp,
1311 1.2.6.2 yamt nfsquad_t clientid, nfsv4stateid_t *stateidp,
1312 1.2.6.2 yamt __unused struct nfsexstuff *exp,
1313 1.2.6.2 yamt struct nfsrv_descript *nd, NFSPROC_T *p)
1314 1.2.6.2 yamt {
1315 1.2.6.2 yamt struct nfslock *lop;
1316 1.2.6.2 yamt struct nfsstate *new_stp = *new_stpp;
1317 1.2.6.2 yamt struct nfslock *new_lop = *new_lopp;
1318 1.2.6.2 yamt struct nfsstate *tstp, *mystp, *nstp;
1319 1.2.6.2 yamt int specialid = 0;
1320 1.2.6.2 yamt struct nfslockfile *lfp;
1321 1.2.6.2 yamt struct nfslock *other_lop = NULL;
1322 1.2.6.2 yamt struct nfsstate *stp, *lckstp = NULL;
1323 1.2.6.2 yamt struct nfsclient *clp = NULL;
1324 1.2.6.2 yamt u_int32_t bits;
1325 1.2.6.2 yamt int error = 0, haslock = 0, ret, reterr;
1326 1.2.6.2 yamt int getlckret, delegation = 0, filestruct_locked;
1327 1.2.6.2 yamt fhandle_t nfh;
1328 1.2.6.2 yamt uint64_t first, end;
1329 1.2.6.2 yamt uint32_t lock_flags;
1330 1.2.6.2 yamt
1331 1.2.6.2 yamt if (new_stp->ls_flags & (NFSLCK_CHECK | NFSLCK_SETATTR)) {
1332 1.2.6.2 yamt /*
1333 1.2.6.2 yamt * Note the special cases of "all 1s" or "all 0s" stateids and
1334 1.2.6.2 yamt * let reads with all 1s go ahead.
1335 1.2.6.2 yamt */
1336 1.2.6.2 yamt if (new_stp->ls_stateid.seqid == 0x0 &&
1337 1.2.6.2 yamt new_stp->ls_stateid.other[0] == 0x0 &&
1338 1.2.6.2 yamt new_stp->ls_stateid.other[1] == 0x0 &&
1339 1.2.6.2 yamt new_stp->ls_stateid.other[2] == 0x0)
1340 1.2.6.2 yamt specialid = 1;
1341 1.2.6.2 yamt else if (new_stp->ls_stateid.seqid == 0xffffffff &&
1342 1.2.6.2 yamt new_stp->ls_stateid.other[0] == 0xffffffff &&
1343 1.2.6.2 yamt new_stp->ls_stateid.other[1] == 0xffffffff &&
1344 1.2.6.2 yamt new_stp->ls_stateid.other[2] == 0xffffffff)
1345 1.2.6.2 yamt specialid = 2;
1346 1.2.6.2 yamt }
1347 1.2.6.2 yamt
1348 1.2.6.2 yamt /*
1349 1.2.6.2 yamt * Check for restart conditions (client and server).
1350 1.2.6.2 yamt */
1351 1.2.6.2 yamt error = nfsrv_checkrestart(clientid, new_stp->ls_flags,
1352 1.2.6.2 yamt &new_stp->ls_stateid, specialid);
1353 1.2.6.2 yamt if (error)
1354 1.2.6.2 yamt goto out;
1355 1.2.6.2 yamt
1356 1.2.6.2 yamt /*
1357 1.2.6.2 yamt * Check for state resource limit exceeded.
1358 1.2.6.2 yamt */
1359 1.2.6.2 yamt if ((new_stp->ls_flags & NFSLCK_LOCK) &&
1360 1.2.6.2 yamt nfsrv_openpluslock > NFSRV_V4STATELIMIT) {
1361 1.2.6.2 yamt error = NFSERR_RESOURCE;
1362 1.2.6.2 yamt goto out;
1363 1.2.6.2 yamt }
1364 1.2.6.2 yamt
1365 1.2.6.2 yamt /*
1366 1.2.6.2 yamt * For the lock case, get another nfslock structure,
1367 1.2.6.2 yamt * just in case we need it.
1368 1.2.6.2 yamt * Malloc now, before we start sifting through the linked lists,
1369 1.2.6.2 yamt * in case we have to wait for memory.
1370 1.2.6.2 yamt */
1371 1.2.6.2 yamt tryagain:
1372 1.2.6.2 yamt if (new_stp->ls_flags & NFSLCK_LOCK)
1373 1.2.6.2 yamt MALLOC(other_lop, struct nfslock *, sizeof (struct nfslock),
1374 1.2.6.2 yamt M_NFSDLOCK, M_WAITOK);
1375 1.2.6.2 yamt filestruct_locked = 0;
1376 1.2.6.2 yamt reterr = 0;
1377 1.2.6.2 yamt lfp = NULL;
1378 1.2.6.2 yamt
1379 1.2.6.2 yamt /*
1380 1.2.6.2 yamt * Get the lockfile structure for CFH now, so we can do a sanity
1381 1.2.6.2 yamt * check against the stateid, before incrementing the seqid#, since
1382 1.2.6.2 yamt * we want to return NFSERR_BADSTATEID on failure and the seqid#
1383 1.2.6.2 yamt * shouldn't be incremented for this case.
1384 1.2.6.2 yamt * If nfsrv_getlockfile() returns -1, it means "not found", which
1385 1.2.6.2 yamt * will be handled later.
1386 1.2.6.2 yamt * If we are doing Lock/LockU and local locking is enabled, sleep
1387 1.2.6.2 yamt * lock the nfslockfile structure.
1388 1.2.6.2 yamt */
1389 1.2.6.2 yamt getlckret = nfsrv_getlockfh(vp, new_stp->ls_flags, NULL, &nfh, p);
1390 1.2.6.2 yamt NFSLOCKSTATE();
1391 1.2.6.2 yamt if (getlckret == 0) {
1392 1.2.6.2 yamt if ((new_stp->ls_flags & (NFSLCK_LOCK | NFSLCK_UNLOCK)) != 0 &&
1393 1.2.6.2 yamt nfsrv_dolocallocks != 0 && nd->nd_repstat == 0) {
1394 1.2.6.2 yamt getlckret = nfsrv_getlockfile(new_stp->ls_flags, NULL,
1395 1.2.6.2 yamt &lfp, &nfh, 1);
1396 1.2.6.2 yamt if (getlckret == 0)
1397 1.2.6.2 yamt filestruct_locked = 1;
1398 1.2.6.2 yamt } else
1399 1.2.6.2 yamt getlckret = nfsrv_getlockfile(new_stp->ls_flags, NULL,
1400 1.2.6.2 yamt &lfp, &nfh, 0);
1401 1.2.6.2 yamt }
1402 1.2.6.2 yamt if (getlckret != 0 && getlckret != -1)
1403 1.2.6.2 yamt reterr = getlckret;
1404 1.2.6.2 yamt
1405 1.2.6.2 yamt if (filestruct_locked != 0) {
1406 1.2.6.2 yamt LIST_INIT(&lfp->lf_rollback);
1407 1.2.6.2 yamt if ((new_stp->ls_flags & NFSLCK_LOCK)) {
1408 1.2.6.2 yamt /*
1409 1.2.6.2 yamt * For local locking, do the advisory locking now, so
1410 1.2.6.2 yamt * that any conflict can be detected. A failure later
1411 1.2.6.2 yamt * can be rolled back locally. If an error is returned,
1412 1.2.6.2 yamt * struct nfslockfile has been unlocked and any local
1413 1.2.6.2 yamt * locking rolled back.
1414 1.2.6.2 yamt */
1415 1.2.6.2 yamt NFSUNLOCKSTATE();
1416 1.2.6.2 yamt reterr = nfsrv_locallock(vp, lfp,
1417 1.2.6.2 yamt (new_lop->lo_flags & (NFSLCK_READ | NFSLCK_WRITE)),
1418 1.2.6.2 yamt new_lop->lo_first, new_lop->lo_end, cfp, p);
1419 1.2.6.2 yamt NFSLOCKSTATE();
1420 1.2.6.2 yamt }
1421 1.2.6.2 yamt }
1422 1.2.6.2 yamt
1423 1.2.6.2 yamt if (specialid == 0) {
1424 1.2.6.2 yamt if (new_stp->ls_flags & NFSLCK_TEST) {
1425 1.2.6.2 yamt /*
1426 1.2.6.2 yamt * RFC 3530 does not list LockT as an op that renews a
1427 1.2.6.2 yamt * lease, but the concensus seems to be that it is ok
1428 1.2.6.2 yamt * for a server to do so.
1429 1.2.6.2 yamt */
1430 1.2.6.2 yamt error = nfsrv_getclient(clientid, CLOPS_RENEW, &clp,
1431 1.2.6.2 yamt (nfsquad_t)((u_quad_t)0), NULL, p);
1432 1.2.6.2 yamt
1433 1.2.6.2 yamt /*
1434 1.2.6.2 yamt * Since NFSERR_EXPIRED, NFSERR_ADMINREVOKED are not valid
1435 1.2.6.2 yamt * error returns for LockT, just go ahead and test for a lock,
1436 1.2.6.2 yamt * since there are no locks for this client, but other locks
1437 1.2.6.2 yamt * can conflict. (ie. same client will always be false)
1438 1.2.6.2 yamt */
1439 1.2.6.2 yamt if (error == NFSERR_EXPIRED || error == NFSERR_ADMINREVOKED)
1440 1.2.6.2 yamt error = 0;
1441 1.2.6.2 yamt lckstp = new_stp;
1442 1.2.6.2 yamt } else {
1443 1.2.6.2 yamt error = nfsrv_getclient(clientid, CLOPS_RENEW, &clp,
1444 1.2.6.2 yamt (nfsquad_t)((u_quad_t)0), NULL, p);
1445 1.2.6.2 yamt if (error == 0)
1446 1.2.6.2 yamt /*
1447 1.2.6.2 yamt * Look up the stateid
1448 1.2.6.2 yamt */
1449 1.2.6.2 yamt error = nfsrv_getstate(clp, &new_stp->ls_stateid,
1450 1.2.6.2 yamt new_stp->ls_flags, &stp);
1451 1.2.6.2 yamt /*
1452 1.2.6.2 yamt * do some sanity checks for an unconfirmed open or a
1453 1.2.6.2 yamt * stateid that refers to the wrong file, for an open stateid
1454 1.2.6.2 yamt */
1455 1.2.6.2 yamt if (error == 0 && (stp->ls_flags & NFSLCK_OPEN) &&
1456 1.2.6.2 yamt ((stp->ls_openowner->ls_flags & NFSLCK_NEEDSCONFIRM) ||
1457 1.2.6.2 yamt (getlckret == 0 && stp->ls_lfp != lfp)))
1458 1.2.6.2 yamt error = NFSERR_BADSTATEID;
1459 1.2.6.2 yamt if (error == 0 &&
1460 1.2.6.2 yamt (stp->ls_flags & (NFSLCK_DELEGREAD | NFSLCK_DELEGWRITE)) &&
1461 1.2.6.2 yamt getlckret == 0 && stp->ls_lfp != lfp)
1462 1.2.6.2 yamt error = NFSERR_BADSTATEID;
1463 1.2.6.2 yamt
1464 1.2.6.2 yamt /*
1465 1.2.6.2 yamt * If the lockowner stateid doesn't refer to the same file,
1466 1.2.6.2 yamt * I believe that is considered ok, since some clients will
1467 1.2.6.2 yamt * only create a single lockowner and use that for all locks
1468 1.2.6.2 yamt * on all files.
1469 1.2.6.2 yamt * For now, log it as a diagnostic, instead of considering it
1470 1.2.6.2 yamt * a BadStateid.
1471 1.2.6.2 yamt */
1472 1.2.6.2 yamt if (error == 0 && (stp->ls_flags &
1473 1.2.6.2 yamt (NFSLCK_OPEN | NFSLCK_DELEGREAD | NFSLCK_DELEGWRITE)) == 0 &&
1474 1.2.6.2 yamt getlckret == 0 && stp->ls_lfp != lfp) {
1475 1.2.6.2 yamt #ifdef DIAGNOSTIC
1476 1.2.6.2 yamt printf("Got a lock statid for different file open\n");
1477 1.2.6.2 yamt #endif
1478 1.2.6.2 yamt /*
1479 1.2.6.2 yamt error = NFSERR_BADSTATEID;
1480 1.2.6.2 yamt */
1481 1.2.6.2 yamt }
1482 1.2.6.2 yamt
1483 1.2.6.2 yamt if (error == 0) {
1484 1.2.6.2 yamt if (new_stp->ls_flags & NFSLCK_OPENTOLOCK) {
1485 1.2.6.2 yamt /*
1486 1.2.6.2 yamt * If haslock set, we've already checked the seqid.
1487 1.2.6.2 yamt */
1488 1.2.6.2 yamt if (!haslock) {
1489 1.2.6.2 yamt if (stp->ls_flags & NFSLCK_OPEN)
1490 1.2.6.2 yamt error = nfsrv_checkseqid(nd, new_stp->ls_seq,
1491 1.2.6.2 yamt stp->ls_openowner, new_stp->ls_op);
1492 1.2.6.2 yamt else
1493 1.2.6.2 yamt error = NFSERR_BADSTATEID;
1494 1.2.6.2 yamt }
1495 1.2.6.2 yamt if (!error)
1496 1.2.6.2 yamt nfsrv_getowner(&stp->ls_open, new_stp, &lckstp);
1497 1.2.6.2 yamt if (lckstp)
1498 1.2.6.2 yamt /*
1499 1.2.6.2 yamt * I believe this should be an error, but it
1500 1.2.6.2 yamt * isn't obvious what NFSERR_xxx would be
1501 1.2.6.2 yamt * appropriate, so I'll use NFSERR_INVAL for now.
1502 1.2.6.2 yamt */
1503 1.2.6.2 yamt error = NFSERR_INVAL;
1504 1.2.6.2 yamt else
1505 1.2.6.2 yamt lckstp = new_stp;
1506 1.2.6.2 yamt } else if (new_stp->ls_flags&(NFSLCK_LOCK|NFSLCK_UNLOCK)) {
1507 1.2.6.2 yamt /*
1508 1.2.6.2 yamt * If haslock set, ditto above.
1509 1.2.6.2 yamt */
1510 1.2.6.2 yamt if (!haslock) {
1511 1.2.6.2 yamt if (stp->ls_flags & NFSLCK_OPEN)
1512 1.2.6.2 yamt error = NFSERR_BADSTATEID;
1513 1.2.6.2 yamt else
1514 1.2.6.2 yamt error = nfsrv_checkseqid(nd, new_stp->ls_seq,
1515 1.2.6.2 yamt stp, new_stp->ls_op);
1516 1.2.6.2 yamt }
1517 1.2.6.2 yamt lckstp = stp;
1518 1.2.6.2 yamt } else {
1519 1.2.6.2 yamt lckstp = stp;
1520 1.2.6.2 yamt }
1521 1.2.6.2 yamt }
1522 1.2.6.2 yamt /*
1523 1.2.6.2 yamt * If the seqid part of the stateid isn't the same, return
1524 1.2.6.2 yamt * NFSERR_OLDSTATEID for cases other than I/O Ops.
1525 1.2.6.2 yamt * For I/O Ops, only return NFSERR_OLDSTATEID if
1526 1.2.6.2 yamt * nfsrv_returnoldstateid is set. (The concensus on the email
1527 1.2.6.2 yamt * list was that most clients would prefer to not receive
1528 1.2.6.2 yamt * NFSERR_OLDSTATEID for I/O Ops, but the RFC suggests that that
1529 1.2.6.2 yamt * is what will happen, so I use the nfsrv_returnoldstateid to
1530 1.2.6.2 yamt * allow for either server configuration.)
1531 1.2.6.2 yamt */
1532 1.2.6.2 yamt if (!error && stp->ls_stateid.seqid!=new_stp->ls_stateid.seqid &&
1533 1.2.6.2 yamt (!(new_stp->ls_flags & NFSLCK_CHECK) ||
1534 1.2.6.2 yamt nfsrv_returnoldstateid))
1535 1.2.6.2 yamt error = NFSERR_OLDSTATEID;
1536 1.2.6.2 yamt }
1537 1.2.6.2 yamt }
1538 1.2.6.2 yamt
1539 1.2.6.2 yamt /*
1540 1.2.6.2 yamt * Now we can check for grace.
1541 1.2.6.2 yamt */
1542 1.2.6.2 yamt if (!error)
1543 1.2.6.2 yamt error = nfsrv_checkgrace(new_stp->ls_flags);
1544 1.2.6.2 yamt if ((new_stp->ls_flags & NFSLCK_RECLAIM) && !error &&
1545 1.2.6.2 yamt nfsrv_checkstable(clp))
1546 1.2.6.2 yamt error = NFSERR_NOGRACE;
1547 1.2.6.2 yamt /*
1548 1.2.6.2 yamt * If we successfully Reclaimed state, note that.
1549 1.2.6.2 yamt */
1550 1.2.6.2 yamt if ((new_stp->ls_flags & NFSLCK_RECLAIM) && !error)
1551 1.2.6.2 yamt nfsrv_markstable(clp);
1552 1.2.6.2 yamt
1553 1.2.6.2 yamt /*
1554 1.2.6.2 yamt * At this point, either error == NFSERR_BADSTATEID or the
1555 1.2.6.2 yamt * seqid# has been updated, so we can return any error.
1556 1.2.6.2 yamt * If error == 0, there may be an error in:
1557 1.2.6.2 yamt * nd_repstat - Set by the calling function.
1558 1.2.6.2 yamt * reterr - Set above, if getting the nfslockfile structure
1559 1.2.6.2 yamt * or acquiring the local lock failed.
1560 1.2.6.2 yamt * (If both of these are set, nd_repstat should probably be
1561 1.2.6.2 yamt * returned, since that error was detected before this
1562 1.2.6.2 yamt * function call.)
1563 1.2.6.2 yamt */
1564 1.2.6.2 yamt if (error != 0 || nd->nd_repstat != 0 || reterr != 0) {
1565 1.2.6.2 yamt if (error == 0) {
1566 1.2.6.2 yamt if (nd->nd_repstat != 0)
1567 1.2.6.2 yamt error = nd->nd_repstat;
1568 1.2.6.2 yamt else
1569 1.2.6.2 yamt error = reterr;
1570 1.2.6.2 yamt }
1571 1.2.6.2 yamt if (filestruct_locked != 0) {
1572 1.2.6.2 yamt /* Roll back local locks. */
1573 1.2.6.2 yamt NFSUNLOCKSTATE();
1574 1.2.6.2 yamt nfsrv_locallock_rollback(vp, lfp, p);
1575 1.2.6.2 yamt NFSLOCKSTATE();
1576 1.2.6.2 yamt nfsrv_unlocklf(lfp);
1577 1.2.6.2 yamt }
1578 1.2.6.2 yamt NFSUNLOCKSTATE();
1579 1.2.6.2 yamt goto out;
1580 1.2.6.2 yamt }
1581 1.2.6.2 yamt
1582 1.2.6.2 yamt /*
1583 1.2.6.2 yamt * Check the nfsrv_getlockfile return.
1584 1.2.6.2 yamt * Returned -1 if no structure found.
1585 1.2.6.2 yamt */
1586 1.2.6.2 yamt if (getlckret == -1) {
1587 1.2.6.2 yamt error = NFSERR_EXPIRED;
1588 1.2.6.2 yamt /*
1589 1.2.6.2 yamt * Called from lockt, so no lock is OK.
1590 1.2.6.2 yamt */
1591 1.2.6.2 yamt if (new_stp->ls_flags & NFSLCK_TEST) {
1592 1.2.6.2 yamt error = 0;
1593 1.2.6.2 yamt } else if (new_stp->ls_flags &
1594 1.2.6.2 yamt (NFSLCK_CHECK | NFSLCK_SETATTR)) {
1595 1.2.6.2 yamt /*
1596 1.2.6.2 yamt * Called to check for a lock, OK if the stateid is all
1597 1.2.6.2 yamt * 1s or all 0s, but there should be an nfsstate
1598 1.2.6.2 yamt * otherwise.
1599 1.2.6.2 yamt * (ie. If there is no open, I'll assume no share
1600 1.2.6.2 yamt * deny bits.)
1601 1.2.6.2 yamt */
1602 1.2.6.2 yamt if (specialid)
1603 1.2.6.2 yamt error = 0;
1604 1.2.6.2 yamt else
1605 1.2.6.2 yamt error = NFSERR_BADSTATEID;
1606 1.2.6.2 yamt }
1607 1.2.6.2 yamt NFSUNLOCKSTATE();
1608 1.2.6.2 yamt goto out;
1609 1.2.6.2 yamt }
1610 1.2.6.2 yamt
1611 1.2.6.2 yamt /*
1612 1.2.6.2 yamt * For NFSLCK_CHECK and NFSLCK_LOCK, test for a share conflict.
1613 1.2.6.2 yamt * For NFSLCK_CHECK, allow a read if write access is granted,
1614 1.2.6.2 yamt * but check for a deny. For NFSLCK_LOCK, require correct access,
1615 1.2.6.2 yamt * which implies a conflicting deny can't exist.
1616 1.2.6.2 yamt */
1617 1.2.6.2 yamt if (new_stp->ls_flags & (NFSLCK_CHECK | NFSLCK_LOCK)) {
1618 1.2.6.2 yamt /*
1619 1.2.6.2 yamt * Four kinds of state id:
1620 1.2.6.2 yamt * - specialid (all 0s or all 1s), only for NFSLCK_CHECK
1621 1.2.6.2 yamt * - stateid for an open
1622 1.2.6.2 yamt * - stateid for a delegation
1623 1.2.6.2 yamt * - stateid for a lock owner
1624 1.2.6.2 yamt */
1625 1.2.6.2 yamt if (!specialid) {
1626 1.2.6.2 yamt if (stp->ls_flags & (NFSLCK_DELEGREAD | NFSLCK_DELEGWRITE)) {
1627 1.2.6.2 yamt delegation = 1;
1628 1.2.6.2 yamt mystp = stp;
1629 1.2.6.2 yamt nfsrv_delaydelegtimeout(stp);
1630 1.2.6.2 yamt } else if (stp->ls_flags & NFSLCK_OPEN) {
1631 1.2.6.2 yamt mystp = stp;
1632 1.2.6.2 yamt } else {
1633 1.2.6.2 yamt mystp = stp->ls_openstp;
1634 1.2.6.2 yamt }
1635 1.2.6.2 yamt /*
1636 1.2.6.2 yamt * If locking or checking, require correct access
1637 1.2.6.2 yamt * bit set.
1638 1.2.6.2 yamt */
1639 1.2.6.2 yamt if (((new_stp->ls_flags & NFSLCK_LOCK) &&
1640 1.2.6.2 yamt !((new_lop->lo_flags >> NFSLCK_LOCKSHIFT) &
1641 1.2.6.2 yamt mystp->ls_flags & NFSLCK_ACCESSBITS)) ||
1642 1.2.6.2 yamt ((new_stp->ls_flags & (NFSLCK_CHECK|NFSLCK_READACCESS)) ==
1643 1.2.6.2 yamt (NFSLCK_CHECK | NFSLCK_READACCESS) &&
1644 1.2.6.2 yamt !(mystp->ls_flags & NFSLCK_READACCESS)) ||
1645 1.2.6.2 yamt ((new_stp->ls_flags & (NFSLCK_CHECK|NFSLCK_WRITEACCESS)) ==
1646 1.2.6.2 yamt (NFSLCK_CHECK | NFSLCK_WRITEACCESS) &&
1647 1.2.6.2 yamt !(mystp->ls_flags & NFSLCK_WRITEACCESS))) {
1648 1.2.6.2 yamt if (filestruct_locked != 0) {
1649 1.2.6.2 yamt /* Roll back local locks. */
1650 1.2.6.2 yamt NFSUNLOCKSTATE();
1651 1.2.6.2 yamt nfsrv_locallock_rollback(vp, lfp, p);
1652 1.2.6.2 yamt NFSLOCKSTATE();
1653 1.2.6.2 yamt nfsrv_unlocklf(lfp);
1654 1.2.6.2 yamt }
1655 1.2.6.2 yamt NFSUNLOCKSTATE();
1656 1.2.6.2 yamt error = NFSERR_OPENMODE;
1657 1.2.6.2 yamt goto out;
1658 1.2.6.2 yamt }
1659 1.2.6.2 yamt } else
1660 1.2.6.2 yamt mystp = NULL;
1661 1.2.6.2 yamt if ((new_stp->ls_flags & NFSLCK_CHECK) && !delegation) {
1662 1.2.6.2 yamt /*
1663 1.2.6.2 yamt * Check for a conflicting deny bit.
1664 1.2.6.2 yamt */
1665 1.2.6.2 yamt LIST_FOREACH(tstp, &lfp->lf_open, ls_file) {
1666 1.2.6.2 yamt if (tstp != mystp) {
1667 1.2.6.2 yamt bits = tstp->ls_flags;
1668 1.2.6.2 yamt bits >>= NFSLCK_SHIFT;
1669 1.2.6.2 yamt if (new_stp->ls_flags & bits & NFSLCK_ACCESSBITS) {
1670 1.2.6.2 yamt ret = nfsrv_clientconflict(tstp->ls_clp, &haslock,
1671 1.2.6.2 yamt vp, p);
1672 1.2.6.2 yamt if (ret == 1) {
1673 1.2.6.2 yamt /*
1674 1.2.6.2 yamt * nfsrv_clientconflict unlocks state
1675 1.2.6.2 yamt * when it returns non-zero.
1676 1.2.6.2 yamt */
1677 1.2.6.2 yamt lckstp = NULL;
1678 1.2.6.2 yamt goto tryagain;
1679 1.2.6.2 yamt }
1680 1.2.6.2 yamt if (ret == 0)
1681 1.2.6.2 yamt NFSUNLOCKSTATE();
1682 1.2.6.2 yamt if (ret == 2)
1683 1.2.6.2 yamt error = NFSERR_PERM;
1684 1.2.6.2 yamt else
1685 1.2.6.2 yamt error = NFSERR_OPENMODE;
1686 1.2.6.2 yamt goto out;
1687 1.2.6.2 yamt }
1688 1.2.6.2 yamt }
1689 1.2.6.2 yamt }
1690 1.2.6.2 yamt
1691 1.2.6.2 yamt /* We're outta here */
1692 1.2.6.2 yamt NFSUNLOCKSTATE();
1693 1.2.6.2 yamt goto out;
1694 1.2.6.2 yamt }
1695 1.2.6.2 yamt }
1696 1.2.6.2 yamt
1697 1.2.6.2 yamt /*
1698 1.2.6.2 yamt * For setattr, just get rid of all the Delegations for other clients.
1699 1.2.6.2 yamt */
1700 1.2.6.2 yamt if (new_stp->ls_flags & NFSLCK_SETATTR) {
1701 1.2.6.2 yamt ret = nfsrv_cleandeleg(vp, lfp, clp, &haslock, p);
1702 1.2.6.2 yamt if (ret) {
1703 1.2.6.2 yamt /*
1704 1.2.6.2 yamt * nfsrv_cleandeleg() unlocks state when it
1705 1.2.6.2 yamt * returns non-zero.
1706 1.2.6.2 yamt */
1707 1.2.6.2 yamt if (ret == -1) {
1708 1.2.6.2 yamt lckstp = NULL;
1709 1.2.6.2 yamt goto tryagain;
1710 1.2.6.2 yamt }
1711 1.2.6.2 yamt error = ret;
1712 1.2.6.2 yamt goto out;
1713 1.2.6.2 yamt }
1714 1.2.6.2 yamt if (!(new_stp->ls_flags & NFSLCK_CHECK) ||
1715 1.2.6.2 yamt (LIST_EMPTY(&lfp->lf_open) && LIST_EMPTY(&lfp->lf_lock) &&
1716 1.2.6.2 yamt LIST_EMPTY(&lfp->lf_deleg))) {
1717 1.2.6.2 yamt NFSUNLOCKSTATE();
1718 1.2.6.2 yamt goto out;
1719 1.2.6.2 yamt }
1720 1.2.6.2 yamt }
1721 1.2.6.2 yamt
1722 1.2.6.2 yamt /*
1723 1.2.6.2 yamt * Check for a conflicting delegation. If one is found, call
1724 1.2.6.2 yamt * nfsrv_delegconflict() to handle it. If the v4root lock hasn't
1725 1.2.6.2 yamt * been set yet, it will get the lock. Otherwise, it will recall
1726 1.2.6.2 yamt * the delegation. Then, we try try again...
1727 1.2.6.2 yamt * I currently believe the conflict algorithm to be:
1728 1.2.6.2 yamt * For Lock Ops (Lock/LockT/LockU)
1729 1.2.6.2 yamt * - there is a conflict iff a different client has a write delegation
1730 1.2.6.2 yamt * For Reading (Read Op)
1731 1.2.6.2 yamt * - there is a conflict iff a different client has a write delegation
1732 1.2.6.2 yamt * (the specialids are always a different client)
1733 1.2.6.2 yamt * For Writing (Write/Setattr of size)
1734 1.2.6.2 yamt * - there is a conflict if a different client has any delegation
1735 1.2.6.2 yamt * - there is a conflict if the same client has a read delegation
1736 1.2.6.2 yamt * (I don't understand why this isn't allowed, but that seems to be
1737 1.2.6.2 yamt * the current concensus?)
1738 1.2.6.2 yamt */
1739 1.2.6.2 yamt tstp = LIST_FIRST(&lfp->lf_deleg);
1740 1.2.6.2 yamt while (tstp != NULL) {
1741 1.2.6.2 yamt nstp = LIST_NEXT(tstp, ls_file);
1742 1.2.6.2 yamt if ((((new_stp->ls_flags&(NFSLCK_LOCK|NFSLCK_UNLOCK|NFSLCK_TEST))||
1743 1.2.6.2 yamt ((new_stp->ls_flags & NFSLCK_CHECK) &&
1744 1.2.6.2 yamt (new_lop->lo_flags & NFSLCK_READ))) &&
1745 1.2.6.2 yamt clp != tstp->ls_clp &&
1746 1.2.6.2 yamt (tstp->ls_flags & NFSLCK_DELEGWRITE)) ||
1747 1.2.6.2 yamt ((new_stp->ls_flags & NFSLCK_CHECK) &&
1748 1.2.6.2 yamt (new_lop->lo_flags & NFSLCK_WRITE) &&
1749 1.2.6.2 yamt (clp != tstp->ls_clp ||
1750 1.2.6.2 yamt (tstp->ls_flags & NFSLCK_DELEGREAD)))) {
1751 1.2.6.2 yamt if (filestruct_locked != 0) {
1752 1.2.6.2 yamt /* Roll back local locks. */
1753 1.2.6.2 yamt NFSUNLOCKSTATE();
1754 1.2.6.2 yamt nfsrv_locallock_rollback(vp, lfp, p);
1755 1.2.6.2 yamt NFSLOCKSTATE();
1756 1.2.6.2 yamt nfsrv_unlocklf(lfp);
1757 1.2.6.2 yamt }
1758 1.2.6.2 yamt ret = nfsrv_delegconflict(tstp, &haslock, p, vp);
1759 1.2.6.2 yamt if (ret) {
1760 1.2.6.2 yamt /*
1761 1.2.6.2 yamt * nfsrv_delegconflict unlocks state when it
1762 1.2.6.2 yamt * returns non-zero, which it always does.
1763 1.2.6.2 yamt */
1764 1.2.6.2 yamt if (other_lop) {
1765 1.2.6.2 yamt FREE((caddr_t)other_lop, M_NFSDLOCK);
1766 1.2.6.2 yamt other_lop = NULL;
1767 1.2.6.2 yamt }
1768 1.2.6.2 yamt if (ret == -1) {
1769 1.2.6.2 yamt lckstp = NULL;
1770 1.2.6.2 yamt goto tryagain;
1771 1.2.6.2 yamt }
1772 1.2.6.2 yamt error = ret;
1773 1.2.6.2 yamt goto out;
1774 1.2.6.2 yamt }
1775 1.2.6.2 yamt /* Never gets here. */
1776 1.2.6.2 yamt }
1777 1.2.6.2 yamt tstp = nstp;
1778 1.2.6.2 yamt }
1779 1.2.6.2 yamt
1780 1.2.6.2 yamt /*
1781 1.2.6.2 yamt * Handle the unlock case by calling nfsrv_updatelock().
1782 1.2.6.2 yamt * (Should I have done some access checking above for unlock? For now,
1783 1.2.6.2 yamt * just let it happen.)
1784 1.2.6.2 yamt */
1785 1.2.6.2 yamt if (new_stp->ls_flags & NFSLCK_UNLOCK) {
1786 1.2.6.2 yamt first = new_lop->lo_first;
1787 1.2.6.2 yamt end = new_lop->lo_end;
1788 1.2.6.2 yamt nfsrv_updatelock(stp, new_lopp, &other_lop, lfp);
1789 1.2.6.2 yamt stateidp->seqid = ++(stp->ls_stateid.seqid);
1790 1.2.6.2 yamt stateidp->other[0] = stp->ls_stateid.other[0];
1791 1.2.6.2 yamt stateidp->other[1] = stp->ls_stateid.other[1];
1792 1.2.6.2 yamt stateidp->other[2] = stp->ls_stateid.other[2];
1793 1.2.6.2 yamt if (filestruct_locked != 0) {
1794 1.2.6.2 yamt NFSUNLOCKSTATE();
1795 1.2.6.2 yamt /* Update the local locks. */
1796 1.2.6.2 yamt nfsrv_localunlock(vp, lfp, first, end, p);
1797 1.2.6.2 yamt NFSLOCKSTATE();
1798 1.2.6.2 yamt nfsrv_unlocklf(lfp);
1799 1.2.6.2 yamt }
1800 1.2.6.2 yamt NFSUNLOCKSTATE();
1801 1.2.6.2 yamt goto out;
1802 1.2.6.2 yamt }
1803 1.2.6.2 yamt
1804 1.2.6.2 yamt /*
1805 1.2.6.2 yamt * Search for a conflicting lock. A lock conflicts if:
1806 1.2.6.2 yamt * - the lock range overlaps and
1807 1.2.6.2 yamt * - at least one lock is a write lock and
1808 1.2.6.2 yamt * - it is not owned by the same lock owner
1809 1.2.6.2 yamt */
1810 1.2.6.2 yamt if (!delegation) {
1811 1.2.6.2 yamt LIST_FOREACH(lop, &lfp->lf_lock, lo_lckfile) {
1812 1.2.6.2 yamt if (new_lop->lo_end > lop->lo_first &&
1813 1.2.6.2 yamt new_lop->lo_first < lop->lo_end &&
1814 1.2.6.2 yamt (new_lop->lo_flags == NFSLCK_WRITE ||
1815 1.2.6.2 yamt lop->lo_flags == NFSLCK_WRITE) &&
1816 1.2.6.2 yamt lckstp != lop->lo_stp &&
1817 1.2.6.2 yamt (clp != lop->lo_stp->ls_clp ||
1818 1.2.6.2 yamt lckstp->ls_ownerlen != lop->lo_stp->ls_ownerlen ||
1819 1.2.6.2 yamt NFSBCMP(lckstp->ls_owner, lop->lo_stp->ls_owner,
1820 1.2.6.2 yamt lckstp->ls_ownerlen))) {
1821 1.2.6.2 yamt if (other_lop) {
1822 1.2.6.2 yamt FREE((caddr_t)other_lop, M_NFSDLOCK);
1823 1.2.6.2 yamt other_lop = NULL;
1824 1.2.6.2 yamt }
1825 1.2.6.2 yamt ret = nfsrv_clientconflict(lop->lo_stp->ls_clp,&haslock,vp,p);
1826 1.2.6.2 yamt if (ret == 1) {
1827 1.2.6.2 yamt if (filestruct_locked != 0) {
1828 1.2.6.2 yamt /* Roll back local locks. */
1829 1.2.6.2 yamt nfsrv_locallock_rollback(vp, lfp, p);
1830 1.2.6.2 yamt NFSLOCKSTATE();
1831 1.2.6.2 yamt nfsrv_unlocklf(lfp);
1832 1.2.6.2 yamt NFSUNLOCKSTATE();
1833 1.2.6.2 yamt }
1834 1.2.6.2 yamt /*
1835 1.2.6.2 yamt * nfsrv_clientconflict() unlocks state when it
1836 1.2.6.2 yamt * returns non-zero.
1837 1.2.6.2 yamt */
1838 1.2.6.2 yamt lckstp = NULL;
1839 1.2.6.2 yamt goto tryagain;
1840 1.2.6.2 yamt }
1841 1.2.6.2 yamt /*
1842 1.2.6.2 yamt * Found a conflicting lock, so record the conflict and
1843 1.2.6.2 yamt * return the error.
1844 1.2.6.2 yamt */
1845 1.2.6.2 yamt if (cfp != NULL && ret == 0) {
1846 1.2.6.2 yamt cfp->cl_clientid.lval[0]=lop->lo_stp->ls_stateid.other[0];
1847 1.2.6.2 yamt cfp->cl_clientid.lval[1]=lop->lo_stp->ls_stateid.other[1];
1848 1.2.6.2 yamt cfp->cl_first = lop->lo_first;
1849 1.2.6.2 yamt cfp->cl_end = lop->lo_end;
1850 1.2.6.2 yamt cfp->cl_flags = lop->lo_flags;
1851 1.2.6.2 yamt cfp->cl_ownerlen = lop->lo_stp->ls_ownerlen;
1852 1.2.6.2 yamt NFSBCOPY(lop->lo_stp->ls_owner, cfp->cl_owner,
1853 1.2.6.2 yamt cfp->cl_ownerlen);
1854 1.2.6.2 yamt }
1855 1.2.6.2 yamt if (ret == 2)
1856 1.2.6.2 yamt error = NFSERR_PERM;
1857 1.2.6.2 yamt else if (new_stp->ls_flags & NFSLCK_RECLAIM)
1858 1.2.6.2 yamt error = NFSERR_RECLAIMCONFLICT;
1859 1.2.6.2 yamt else if (new_stp->ls_flags & NFSLCK_CHECK)
1860 1.2.6.2 yamt error = NFSERR_LOCKED;
1861 1.2.6.2 yamt else
1862 1.2.6.2 yamt error = NFSERR_DENIED;
1863 1.2.6.2 yamt if (filestruct_locked != 0 && ret == 0) {
1864 1.2.6.2 yamt /* Roll back local locks. */
1865 1.2.6.2 yamt NFSUNLOCKSTATE();
1866 1.2.6.2 yamt nfsrv_locallock_rollback(vp, lfp, p);
1867 1.2.6.2 yamt NFSLOCKSTATE();
1868 1.2.6.2 yamt nfsrv_unlocklf(lfp);
1869 1.2.6.2 yamt }
1870 1.2.6.2 yamt if (ret == 0)
1871 1.2.6.2 yamt NFSUNLOCKSTATE();
1872 1.2.6.2 yamt goto out;
1873 1.2.6.2 yamt }
1874 1.2.6.2 yamt }
1875 1.2.6.2 yamt }
1876 1.2.6.2 yamt
1877 1.2.6.2 yamt /*
1878 1.2.6.2 yamt * We only get here if there was no lock that conflicted.
1879 1.2.6.2 yamt */
1880 1.2.6.2 yamt if (new_stp->ls_flags & (NFSLCK_TEST | NFSLCK_CHECK)) {
1881 1.2.6.2 yamt NFSUNLOCKSTATE();
1882 1.2.6.2 yamt goto out;
1883 1.2.6.2 yamt }
1884 1.2.6.2 yamt
1885 1.2.6.2 yamt /*
1886 1.2.6.2 yamt * We only get here when we are creating or modifying a lock.
1887 1.2.6.2 yamt * There are two variants:
1888 1.2.6.2 yamt * - exist_lock_owner where lock_owner exists
1889 1.2.6.2 yamt * - open_to_lock_owner with new lock_owner
1890 1.2.6.2 yamt */
1891 1.2.6.2 yamt first = new_lop->lo_first;
1892 1.2.6.2 yamt end = new_lop->lo_end;
1893 1.2.6.2 yamt lock_flags = new_lop->lo_flags;
1894 1.2.6.2 yamt if (!(new_stp->ls_flags & NFSLCK_OPENTOLOCK)) {
1895 1.2.6.2 yamt nfsrv_updatelock(lckstp, new_lopp, &other_lop, lfp);
1896 1.2.6.2 yamt stateidp->seqid = ++(lckstp->ls_stateid.seqid);
1897 1.2.6.2 yamt stateidp->other[0] = lckstp->ls_stateid.other[0];
1898 1.2.6.2 yamt stateidp->other[1] = lckstp->ls_stateid.other[1];
1899 1.2.6.2 yamt stateidp->other[2] = lckstp->ls_stateid.other[2];
1900 1.2.6.2 yamt } else {
1901 1.2.6.2 yamt /*
1902 1.2.6.2 yamt * The new open_to_lock_owner case.
1903 1.2.6.2 yamt * Link the new nfsstate into the lists.
1904 1.2.6.2 yamt */
1905 1.2.6.2 yamt new_stp->ls_seq = new_stp->ls_opentolockseq;
1906 1.2.6.2 yamt nfsrvd_refcache(new_stp->ls_op);
1907 1.2.6.2 yamt stateidp->seqid = new_stp->ls_stateid.seqid = 1;
1908 1.2.6.2 yamt stateidp->other[0] = new_stp->ls_stateid.other[0] =
1909 1.2.6.2 yamt clp->lc_clientid.lval[0];
1910 1.2.6.2 yamt stateidp->other[1] = new_stp->ls_stateid.other[1] =
1911 1.2.6.2 yamt clp->lc_clientid.lval[1];
1912 1.2.6.2 yamt stateidp->other[2] = new_stp->ls_stateid.other[2] =
1913 1.2.6.2 yamt nfsrv_nextstateindex(clp);
1914 1.2.6.2 yamt new_stp->ls_clp = clp;
1915 1.2.6.2 yamt LIST_INIT(&new_stp->ls_lock);
1916 1.2.6.2 yamt new_stp->ls_openstp = stp;
1917 1.2.6.2 yamt new_stp->ls_lfp = lfp;
1918 1.2.6.2 yamt nfsrv_insertlock(new_lop, (struct nfslock *)new_stp, new_stp,
1919 1.2.6.2 yamt lfp);
1920 1.2.6.2 yamt LIST_INSERT_HEAD(NFSSTATEHASH(clp, new_stp->ls_stateid),
1921 1.2.6.2 yamt new_stp, ls_hash);
1922 1.2.6.2 yamt LIST_INSERT_HEAD(&stp->ls_open, new_stp, ls_list);
1923 1.2.6.2 yamt *new_lopp = NULL;
1924 1.2.6.2 yamt *new_stpp = NULL;
1925 1.2.6.2 yamt newnfsstats.srvlockowners++;
1926 1.2.6.2 yamt nfsrv_openpluslock++;
1927 1.2.6.2 yamt }
1928 1.2.6.2 yamt if (filestruct_locked != 0) {
1929 1.2.6.2 yamt NFSUNLOCKSTATE();
1930 1.2.6.2 yamt nfsrv_locallock_commit(lfp, lock_flags, first, end);
1931 1.2.6.2 yamt NFSLOCKSTATE();
1932 1.2.6.2 yamt nfsrv_unlocklf(lfp);
1933 1.2.6.2 yamt }
1934 1.2.6.2 yamt NFSUNLOCKSTATE();
1935 1.2.6.2 yamt
1936 1.2.6.2 yamt out:
1937 1.2.6.2 yamt if (haslock) {
1938 1.2.6.2 yamt NFSLOCKV4ROOTMUTEX();
1939 1.2.6.2 yamt nfsv4_unlock(&nfsv4rootfs_lock, 1);
1940 1.2.6.2 yamt NFSUNLOCKV4ROOTMUTEX();
1941 1.2.6.2 yamt }
1942 1.2.6.2 yamt if (other_lop)
1943 1.2.6.2 yamt FREE((caddr_t)other_lop, M_NFSDLOCK);
1944 1.2.6.2 yamt NFSEXITCODE2(error, nd);
1945 1.2.6.2 yamt return (error);
1946 1.2.6.2 yamt }
1947 1.2.6.2 yamt
1948 1.2.6.2 yamt /*
1949 1.2.6.2 yamt * Check for state errors for Open.
1950 1.2.6.2 yamt * repstat is passed back out as an error if more critical errors
1951 1.2.6.2 yamt * are not detected.
1952 1.2.6.2 yamt */
1953 1.2.6.2 yamt APPLESTATIC int
1954 1.2.6.2 yamt nfsrv_opencheck(nfsquad_t clientid, nfsv4stateid_t *stateidp,
1955 1.2.6.2 yamt struct nfsstate *new_stp, vnode_t vp, struct nfsrv_descript *nd,
1956 1.2.6.2 yamt NFSPROC_T *p, int repstat)
1957 1.2.6.2 yamt {
1958 1.2.6.2 yamt struct nfsstate *stp, *nstp;
1959 1.2.6.2 yamt struct nfsclient *clp;
1960 1.2.6.2 yamt struct nfsstate *ownerstp;
1961 1.2.6.2 yamt struct nfslockfile *lfp, *new_lfp;
1962 1.2.6.2 yamt int error = 0, haslock = 0, ret, readonly = 0, getfhret = 0;
1963 1.2.6.2 yamt
1964 1.2.6.2 yamt if ((new_stp->ls_flags & NFSLCK_SHAREBITS) == NFSLCK_READACCESS)
1965 1.2.6.2 yamt readonly = 1;
1966 1.2.6.2 yamt /*
1967 1.2.6.2 yamt * Check for restart conditions (client and server).
1968 1.2.6.2 yamt */
1969 1.2.6.2 yamt error = nfsrv_checkrestart(clientid, new_stp->ls_flags,
1970 1.2.6.2 yamt &new_stp->ls_stateid, 0);
1971 1.2.6.2 yamt if (error)
1972 1.2.6.2 yamt goto out;
1973 1.2.6.2 yamt
1974 1.2.6.2 yamt /*
1975 1.2.6.2 yamt * Check for state resource limit exceeded.
1976 1.2.6.2 yamt * Technically this should be SMP protected, but the worst
1977 1.2.6.2 yamt * case error is "out by one or two" on the count when it
1978 1.2.6.2 yamt * returns NFSERR_RESOURCE and the limit is just a rather
1979 1.2.6.2 yamt * arbitrary high water mark, so no harm is done.
1980 1.2.6.2 yamt */
1981 1.2.6.2 yamt if (nfsrv_openpluslock > NFSRV_V4STATELIMIT) {
1982 1.2.6.2 yamt error = NFSERR_RESOURCE;
1983 1.2.6.2 yamt goto out;
1984 1.2.6.2 yamt }
1985 1.2.6.2 yamt
1986 1.2.6.2 yamt tryagain:
1987 1.2.6.2 yamt MALLOC(new_lfp, struct nfslockfile *, sizeof (struct nfslockfile),
1988 1.2.6.2 yamt M_NFSDLOCKFILE, M_WAITOK);
1989 1.2.6.2 yamt if (vp)
1990 1.2.6.2 yamt getfhret = nfsrv_getlockfh(vp, new_stp->ls_flags, &new_lfp,
1991 1.2.6.2 yamt NULL, p);
1992 1.2.6.2 yamt NFSLOCKSTATE();
1993 1.2.6.2 yamt /*
1994 1.2.6.2 yamt * Get the nfsclient structure.
1995 1.2.6.2 yamt */
1996 1.2.6.2 yamt error = nfsrv_getclient(clientid, CLOPS_RENEW, &clp,
1997 1.2.6.2 yamt (nfsquad_t)((u_quad_t)0), NULL, p);
1998 1.2.6.2 yamt
1999 1.2.6.2 yamt /*
2000 1.2.6.2 yamt * Look up the open owner. See if it needs confirmation and
2001 1.2.6.2 yamt * check the seq#, as required.
2002 1.2.6.2 yamt */
2003 1.2.6.2 yamt if (!error)
2004 1.2.6.2 yamt nfsrv_getowner(&clp->lc_open, new_stp, &ownerstp);
2005 1.2.6.2 yamt
2006 1.2.6.2 yamt if (!error && ownerstp) {
2007 1.2.6.2 yamt error = nfsrv_checkseqid(nd, new_stp->ls_seq, ownerstp,
2008 1.2.6.2 yamt new_stp->ls_op);
2009 1.2.6.2 yamt /*
2010 1.2.6.2 yamt * If the OpenOwner hasn't been confirmed, assume the
2011 1.2.6.2 yamt * old one was a replay and this one is ok.
2012 1.2.6.2 yamt * See: RFC3530 Sec. 14.2.18.
2013 1.2.6.2 yamt */
2014 1.2.6.2 yamt if (error == NFSERR_BADSEQID &&
2015 1.2.6.2 yamt (ownerstp->ls_flags & NFSLCK_NEEDSCONFIRM))
2016 1.2.6.2 yamt error = 0;
2017 1.2.6.2 yamt }
2018 1.2.6.2 yamt
2019 1.2.6.2 yamt /*
2020 1.2.6.2 yamt * Check for grace.
2021 1.2.6.2 yamt */
2022 1.2.6.2 yamt if (!error)
2023 1.2.6.2 yamt error = nfsrv_checkgrace(new_stp->ls_flags);
2024 1.2.6.2 yamt if ((new_stp->ls_flags & NFSLCK_RECLAIM) && !error &&
2025 1.2.6.2 yamt nfsrv_checkstable(clp))
2026 1.2.6.2 yamt error = NFSERR_NOGRACE;
2027 1.2.6.2 yamt
2028 1.2.6.2 yamt /*
2029 1.2.6.2 yamt * If none of the above errors occurred, let repstat be
2030 1.2.6.2 yamt * returned.
2031 1.2.6.2 yamt */
2032 1.2.6.2 yamt if (repstat && !error)
2033 1.2.6.2 yamt error = repstat;
2034 1.2.6.2 yamt if (error) {
2035 1.2.6.2 yamt NFSUNLOCKSTATE();
2036 1.2.6.2 yamt if (haslock) {
2037 1.2.6.2 yamt NFSLOCKV4ROOTMUTEX();
2038 1.2.6.2 yamt nfsv4_unlock(&nfsv4rootfs_lock, 1);
2039 1.2.6.2 yamt NFSUNLOCKV4ROOTMUTEX();
2040 1.2.6.2 yamt }
2041 1.2.6.2 yamt free((caddr_t)new_lfp, M_NFSDLOCKFILE);
2042 1.2.6.2 yamt goto out;
2043 1.2.6.2 yamt }
2044 1.2.6.2 yamt
2045 1.2.6.2 yamt /*
2046 1.2.6.2 yamt * If vp == NULL, the file doesn't exist yet, so return ok.
2047 1.2.6.2 yamt * (This always happens on the first pass, so haslock must be 0.)
2048 1.2.6.2 yamt */
2049 1.2.6.2 yamt if (vp == NULL) {
2050 1.2.6.2 yamt NFSUNLOCKSTATE();
2051 1.2.6.2 yamt FREE((caddr_t)new_lfp, M_NFSDLOCKFILE);
2052 1.2.6.2 yamt goto out;
2053 1.2.6.2 yamt }
2054 1.2.6.2 yamt
2055 1.2.6.2 yamt /*
2056 1.2.6.2 yamt * Get the structure for the underlying file.
2057 1.2.6.2 yamt */
2058 1.2.6.2 yamt if (getfhret)
2059 1.2.6.2 yamt error = getfhret;
2060 1.2.6.2 yamt else
2061 1.2.6.2 yamt error = nfsrv_getlockfile(new_stp->ls_flags, &new_lfp, &lfp,
2062 1.2.6.2 yamt NULL, 0);
2063 1.2.6.2 yamt if (new_lfp)
2064 1.2.6.2 yamt FREE((caddr_t)new_lfp, M_NFSDLOCKFILE);
2065 1.2.6.2 yamt if (error) {
2066 1.2.6.2 yamt NFSUNLOCKSTATE();
2067 1.2.6.2 yamt if (haslock) {
2068 1.2.6.2 yamt NFSLOCKV4ROOTMUTEX();
2069 1.2.6.2 yamt nfsv4_unlock(&nfsv4rootfs_lock, 1);
2070 1.2.6.2 yamt NFSUNLOCKV4ROOTMUTEX();
2071 1.2.6.2 yamt }
2072 1.2.6.2 yamt goto out;
2073 1.2.6.2 yamt }
2074 1.2.6.2 yamt
2075 1.2.6.2 yamt /*
2076 1.2.6.2 yamt * Search for a conflicting open/share.
2077 1.2.6.2 yamt */
2078 1.2.6.2 yamt if (new_stp->ls_flags & NFSLCK_DELEGCUR) {
2079 1.2.6.2 yamt /*
2080 1.2.6.2 yamt * For Delegate_Cur, search for the matching Delegation,
2081 1.2.6.2 yamt * which indicates no conflict.
2082 1.2.6.2 yamt * An old delegation should have been recovered by the
2083 1.2.6.2 yamt * client doing a Claim_DELEGATE_Prev, so I won't let
2084 1.2.6.2 yamt * it match and return NFSERR_EXPIRED. Should I let it
2085 1.2.6.2 yamt * match?
2086 1.2.6.2 yamt */
2087 1.2.6.2 yamt LIST_FOREACH(stp, &lfp->lf_deleg, ls_file) {
2088 1.2.6.2 yamt if (!(stp->ls_flags & NFSLCK_OLDDELEG) &&
2089 1.2.6.2 yamt stateidp->seqid == stp->ls_stateid.seqid &&
2090 1.2.6.2 yamt !NFSBCMP(stateidp->other, stp->ls_stateid.other,
2091 1.2.6.2 yamt NFSX_STATEIDOTHER))
2092 1.2.6.2 yamt break;
2093 1.2.6.2 yamt }
2094 1.2.6.2 yamt if (stp == NULL ||
2095 1.2.6.2 yamt ((new_stp->ls_flags & NFSLCK_WRITEACCESS) &&
2096 1.2.6.2 yamt (stp->ls_flags & NFSLCK_DELEGREAD))) {
2097 1.2.6.2 yamt NFSUNLOCKSTATE();
2098 1.2.6.2 yamt if (haslock) {
2099 1.2.6.2 yamt NFSLOCKV4ROOTMUTEX();
2100 1.2.6.2 yamt nfsv4_unlock(&nfsv4rootfs_lock, 1);
2101 1.2.6.2 yamt NFSUNLOCKV4ROOTMUTEX();
2102 1.2.6.2 yamt }
2103 1.2.6.2 yamt error = NFSERR_EXPIRED;
2104 1.2.6.2 yamt goto out;
2105 1.2.6.2 yamt }
2106 1.2.6.2 yamt }
2107 1.2.6.2 yamt
2108 1.2.6.2 yamt /*
2109 1.2.6.2 yamt * Check for access/deny bit conflicts. I check for the same
2110 1.2.6.2 yamt * owner as well, in case the client didn't bother.
2111 1.2.6.2 yamt */
2112 1.2.6.2 yamt LIST_FOREACH(stp, &lfp->lf_open, ls_file) {
2113 1.2.6.2 yamt if (!(new_stp->ls_flags & NFSLCK_DELEGCUR) &&
2114 1.2.6.2 yamt (((new_stp->ls_flags & NFSLCK_ACCESSBITS) &
2115 1.2.6.2 yamt ((stp->ls_flags>>NFSLCK_SHIFT) & NFSLCK_ACCESSBITS))||
2116 1.2.6.2 yamt ((stp->ls_flags & NFSLCK_ACCESSBITS) &
2117 1.2.6.2 yamt ((new_stp->ls_flags>>NFSLCK_SHIFT)&NFSLCK_ACCESSBITS)))){
2118 1.2.6.2 yamt ret = nfsrv_clientconflict(stp->ls_clp,&haslock,vp,p);
2119 1.2.6.2 yamt if (ret == 1) {
2120 1.2.6.2 yamt /*
2121 1.2.6.2 yamt * nfsrv_clientconflict() unlocks
2122 1.2.6.2 yamt * state when it returns non-zero.
2123 1.2.6.2 yamt */
2124 1.2.6.2 yamt goto tryagain;
2125 1.2.6.2 yamt }
2126 1.2.6.2 yamt if (ret == 2)
2127 1.2.6.2 yamt error = NFSERR_PERM;
2128 1.2.6.2 yamt else if (new_stp->ls_flags & NFSLCK_RECLAIM)
2129 1.2.6.2 yamt error = NFSERR_RECLAIMCONFLICT;
2130 1.2.6.2 yamt else
2131 1.2.6.2 yamt error = NFSERR_SHAREDENIED;
2132 1.2.6.2 yamt if (ret == 0)
2133 1.2.6.2 yamt NFSUNLOCKSTATE();
2134 1.2.6.2 yamt if (haslock) {
2135 1.2.6.2 yamt NFSLOCKV4ROOTMUTEX();
2136 1.2.6.2 yamt nfsv4_unlock(&nfsv4rootfs_lock, 1);
2137 1.2.6.2 yamt NFSUNLOCKV4ROOTMUTEX();
2138 1.2.6.2 yamt }
2139 1.2.6.2 yamt goto out;
2140 1.2.6.2 yamt }
2141 1.2.6.2 yamt }
2142 1.2.6.2 yamt
2143 1.2.6.2 yamt /*
2144 1.2.6.2 yamt * Check for a conflicting delegation. If one is found, call
2145 1.2.6.2 yamt * nfsrv_delegconflict() to handle it. If the v4root lock hasn't
2146 1.2.6.2 yamt * been set yet, it will get the lock. Otherwise, it will recall
2147 1.2.6.2 yamt * the delegation. Then, we try try again...
2148 1.2.6.2 yamt * (If NFSLCK_DELEGCUR is set, it has a delegation, so there
2149 1.2.6.2 yamt * isn't a conflict.)
2150 1.2.6.2 yamt * I currently believe the conflict algorithm to be:
2151 1.2.6.2 yamt * For Open with Read Access and Deny None
2152 1.2.6.2 yamt * - there is a conflict iff a different client has a write delegation
2153 1.2.6.2 yamt * For Open with other Write Access or any Deny except None
2154 1.2.6.2 yamt * - there is a conflict if a different client has any delegation
2155 1.2.6.2 yamt * - there is a conflict if the same client has a read delegation
2156 1.2.6.2 yamt * (The current concensus is that this last case should be
2157 1.2.6.2 yamt * considered a conflict since the client with a read delegation
2158 1.2.6.2 yamt * could have done an Open with ReadAccess and WriteDeny
2159 1.2.6.2 yamt * locally and then not have checked for the WriteDeny.)
2160 1.2.6.2 yamt * Don't check for a Reclaim, since that will be dealt with
2161 1.2.6.2 yamt * by nfsrv_openctrl().
2162 1.2.6.2 yamt */
2163 1.2.6.2 yamt if (!(new_stp->ls_flags &
2164 1.2.6.2 yamt (NFSLCK_DELEGPREV | NFSLCK_DELEGCUR | NFSLCK_RECLAIM))) {
2165 1.2.6.2 yamt stp = LIST_FIRST(&lfp->lf_deleg);
2166 1.2.6.2 yamt while (stp != NULL) {
2167 1.2.6.2 yamt nstp = LIST_NEXT(stp, ls_file);
2168 1.2.6.2 yamt if ((readonly && stp->ls_clp != clp &&
2169 1.2.6.2 yamt (stp->ls_flags & NFSLCK_DELEGWRITE)) ||
2170 1.2.6.2 yamt (!readonly && (stp->ls_clp != clp ||
2171 1.2.6.2 yamt (stp->ls_flags & NFSLCK_DELEGREAD)))) {
2172 1.2.6.2 yamt ret = nfsrv_delegconflict(stp, &haslock, p, vp);
2173 1.2.6.2 yamt if (ret) {
2174 1.2.6.2 yamt /*
2175 1.2.6.2 yamt * nfsrv_delegconflict() unlocks state
2176 1.2.6.2 yamt * when it returns non-zero.
2177 1.2.6.2 yamt */
2178 1.2.6.2 yamt if (ret == -1)
2179 1.2.6.2 yamt goto tryagain;
2180 1.2.6.2 yamt error = ret;
2181 1.2.6.2 yamt goto out;
2182 1.2.6.2 yamt }
2183 1.2.6.2 yamt }
2184 1.2.6.2 yamt stp = nstp;
2185 1.2.6.2 yamt }
2186 1.2.6.2 yamt }
2187 1.2.6.2 yamt NFSUNLOCKSTATE();
2188 1.2.6.2 yamt if (haslock) {
2189 1.2.6.2 yamt NFSLOCKV4ROOTMUTEX();
2190 1.2.6.2 yamt nfsv4_unlock(&nfsv4rootfs_lock, 1);
2191 1.2.6.2 yamt NFSUNLOCKV4ROOTMUTEX();
2192 1.2.6.2 yamt }
2193 1.2.6.2 yamt
2194 1.2.6.2 yamt out:
2195 1.2.6.2 yamt NFSEXITCODE2(error, nd);
2196 1.2.6.2 yamt return (error);
2197 1.2.6.2 yamt }
2198 1.2.6.2 yamt
2199 1.2.6.2 yamt /*
2200 1.2.6.2 yamt * Open control function to create/update open state for an open.
2201 1.2.6.2 yamt */
2202 1.2.6.2 yamt APPLESTATIC int
2203 1.2.6.2 yamt nfsrv_openctrl(struct nfsrv_descript *nd, vnode_t vp,
2204 1.2.6.2 yamt struct nfsstate **new_stpp, nfsquad_t clientid, nfsv4stateid_t *stateidp,
2205 1.2.6.2 yamt nfsv4stateid_t *delegstateidp, u_int32_t *rflagsp, struct nfsexstuff *exp,
2206 1.2.6.2 yamt NFSPROC_T *p, u_quad_t filerev)
2207 1.2.6.2 yamt {
2208 1.2.6.2 yamt struct nfsstate *new_stp = *new_stpp;
2209 1.2.6.2 yamt struct nfsstate *stp, *nstp;
2210 1.2.6.2 yamt struct nfsstate *openstp = NULL, *new_open, *ownerstp, *new_deleg;
2211 1.2.6.2 yamt struct nfslockfile *lfp, *new_lfp;
2212 1.2.6.2 yamt struct nfsclient *clp;
2213 1.2.6.2 yamt int error = 0, haslock = 0, ret, delegate = 1, writedeleg = 1;
2214 1.2.6.2 yamt int readonly = 0, cbret = 1, getfhret = 0;
2215 1.2.6.2 yamt
2216 1.2.6.2 yamt if ((new_stp->ls_flags & NFSLCK_SHAREBITS) == NFSLCK_READACCESS)
2217 1.2.6.2 yamt readonly = 1;
2218 1.2.6.2 yamt /*
2219 1.2.6.2 yamt * Check for restart conditions (client and server).
2220 1.2.6.2 yamt * (Paranoia, should have been detected by nfsrv_opencheck().)
2221 1.2.6.2 yamt * If an error does show up, return NFSERR_EXPIRED, since the
2222 1.2.6.2 yamt * the seqid# has already been incremented.
2223 1.2.6.2 yamt */
2224 1.2.6.2 yamt error = nfsrv_checkrestart(clientid, new_stp->ls_flags,
2225 1.2.6.2 yamt &new_stp->ls_stateid, 0);
2226 1.2.6.2 yamt if (error) {
2227 1.2.6.2 yamt printf("Nfsd: openctrl unexpected restart err=%d\n",
2228 1.2.6.2 yamt error);
2229 1.2.6.2 yamt error = NFSERR_EXPIRED;
2230 1.2.6.2 yamt goto out;
2231 1.2.6.2 yamt }
2232 1.2.6.2 yamt
2233 1.2.6.2 yamt tryagain:
2234 1.2.6.2 yamt MALLOC(new_lfp, struct nfslockfile *, sizeof (struct nfslockfile),
2235 1.2.6.2 yamt M_NFSDLOCKFILE, M_WAITOK);
2236 1.2.6.2 yamt MALLOC(new_open, struct nfsstate *, sizeof (struct nfsstate),
2237 1.2.6.2 yamt M_NFSDSTATE, M_WAITOK);
2238 1.2.6.2 yamt MALLOC(new_deleg, struct nfsstate *, sizeof (struct nfsstate),
2239 1.2.6.2 yamt M_NFSDSTATE, M_WAITOK);
2240 1.2.6.2 yamt getfhret = nfsrv_getlockfh(vp, new_stp->ls_flags, &new_lfp,
2241 1.2.6.2 yamt NULL, p);
2242 1.2.6.2 yamt NFSLOCKSTATE();
2243 1.2.6.2 yamt /*
2244 1.2.6.2 yamt * Get the client structure. Since the linked lists could be changed
2245 1.2.6.2 yamt * by other nfsd processes if this process does a tsleep(), one of
2246 1.2.6.2 yamt * two things must be done.
2247 1.2.6.2 yamt * 1 - don't tsleep()
2248 1.2.6.2 yamt * or
2249 1.2.6.2 yamt * 2 - get the nfsv4_lock() { indicated by haslock == 1 }
2250 1.2.6.2 yamt * before using the lists, since this lock stops the other
2251 1.2.6.2 yamt * nfsd. This should only be used for rare cases, since it
2252 1.2.6.2 yamt * essentially single threads the nfsd.
2253 1.2.6.2 yamt * At this time, it is only done for cases where the stable
2254 1.2.6.2 yamt * storage file must be written prior to completion of state
2255 1.2.6.2 yamt * expiration.
2256 1.2.6.2 yamt */
2257 1.2.6.2 yamt error = nfsrv_getclient(clientid, CLOPS_RENEW, &clp,
2258 1.2.6.2 yamt (nfsquad_t)((u_quad_t)0), NULL, p);
2259 1.2.6.2 yamt if (!error && (clp->lc_flags & LCL_NEEDSCBNULL) &&
2260 1.2.6.2 yamt clp->lc_program) {
2261 1.2.6.2 yamt /*
2262 1.2.6.2 yamt * This happens on the first open for a client
2263 1.2.6.2 yamt * that supports callbacks.
2264 1.2.6.2 yamt */
2265 1.2.6.2 yamt NFSUNLOCKSTATE();
2266 1.2.6.2 yamt /*
2267 1.2.6.2 yamt * Although nfsrv_docallback() will sleep, clp won't
2268 1.2.6.2 yamt * go away, since they are only removed when the
2269 1.2.6.2 yamt * nfsv4_lock() has blocked the nfsd threads. The
2270 1.2.6.2 yamt * fields in clp can change, but having multiple
2271 1.2.6.2 yamt * threads do this Null callback RPC should be
2272 1.2.6.2 yamt * harmless.
2273 1.2.6.2 yamt */
2274 1.2.6.2 yamt cbret = nfsrv_docallback(clp, NFSV4PROC_CBNULL,
2275 1.2.6.2 yamt NULL, 0, NULL, NULL, NULL, p);
2276 1.2.6.2 yamt NFSLOCKSTATE();
2277 1.2.6.2 yamt clp->lc_flags &= ~LCL_NEEDSCBNULL;
2278 1.2.6.2 yamt if (!cbret)
2279 1.2.6.2 yamt clp->lc_flags |= LCL_CALLBACKSON;
2280 1.2.6.2 yamt }
2281 1.2.6.2 yamt
2282 1.2.6.2 yamt /*
2283 1.2.6.2 yamt * Look up the open owner. See if it needs confirmation and
2284 1.2.6.2 yamt * check the seq#, as required.
2285 1.2.6.2 yamt */
2286 1.2.6.2 yamt if (!error)
2287 1.2.6.2 yamt nfsrv_getowner(&clp->lc_open, new_stp, &ownerstp);
2288 1.2.6.2 yamt
2289 1.2.6.2 yamt if (error) {
2290 1.2.6.2 yamt NFSUNLOCKSTATE();
2291 1.2.6.2 yamt printf("Nfsd: openctrl unexpected state err=%d\n",
2292 1.2.6.2 yamt error);
2293 1.2.6.2 yamt free((caddr_t)new_lfp, M_NFSDLOCKFILE);
2294 1.2.6.2 yamt free((caddr_t)new_open, M_NFSDSTATE);
2295 1.2.6.2 yamt free((caddr_t)new_deleg, M_NFSDSTATE);
2296 1.2.6.2 yamt if (haslock) {
2297 1.2.6.2 yamt NFSLOCKV4ROOTMUTEX();
2298 1.2.6.2 yamt nfsv4_unlock(&nfsv4rootfs_lock, 1);
2299 1.2.6.2 yamt NFSUNLOCKV4ROOTMUTEX();
2300 1.2.6.2 yamt }
2301 1.2.6.2 yamt error = NFSERR_EXPIRED;
2302 1.2.6.2 yamt goto out;
2303 1.2.6.2 yamt }
2304 1.2.6.2 yamt
2305 1.2.6.2 yamt if (new_stp->ls_flags & NFSLCK_RECLAIM)
2306 1.2.6.2 yamt nfsrv_markstable(clp);
2307 1.2.6.2 yamt
2308 1.2.6.2 yamt /*
2309 1.2.6.2 yamt * Get the structure for the underlying file.
2310 1.2.6.2 yamt */
2311 1.2.6.2 yamt if (getfhret)
2312 1.2.6.2 yamt error = getfhret;
2313 1.2.6.2 yamt else
2314 1.2.6.2 yamt error = nfsrv_getlockfile(new_stp->ls_flags, &new_lfp, &lfp,
2315 1.2.6.2 yamt NULL, 0);
2316 1.2.6.2 yamt if (new_lfp)
2317 1.2.6.2 yamt FREE((caddr_t)new_lfp, M_NFSDLOCKFILE);
2318 1.2.6.2 yamt if (error) {
2319 1.2.6.2 yamt NFSUNLOCKSTATE();
2320 1.2.6.2 yamt printf("Nfsd openctrl unexpected getlockfile err=%d\n",
2321 1.2.6.2 yamt error);
2322 1.2.6.2 yamt free((caddr_t)new_open, M_NFSDSTATE);
2323 1.2.6.2 yamt free((caddr_t)new_deleg, M_NFSDSTATE);
2324 1.2.6.2 yamt if (haslock) {
2325 1.2.6.2 yamt NFSLOCKV4ROOTMUTEX();
2326 1.2.6.2 yamt nfsv4_unlock(&nfsv4rootfs_lock, 1);
2327 1.2.6.2 yamt NFSUNLOCKV4ROOTMUTEX();
2328 1.2.6.2 yamt }
2329 1.2.6.2 yamt goto out;
2330 1.2.6.2 yamt }
2331 1.2.6.2 yamt
2332 1.2.6.2 yamt /*
2333 1.2.6.2 yamt * Search for a conflicting open/share.
2334 1.2.6.2 yamt */
2335 1.2.6.2 yamt if (new_stp->ls_flags & NFSLCK_DELEGCUR) {
2336 1.2.6.2 yamt /*
2337 1.2.6.2 yamt * For Delegate_Cur, search for the matching Delegation,
2338 1.2.6.2 yamt * which indicates no conflict.
2339 1.2.6.2 yamt * An old delegation should have been recovered by the
2340 1.2.6.2 yamt * client doing a Claim_DELEGATE_Prev, so I won't let
2341 1.2.6.2 yamt * it match and return NFSERR_EXPIRED. Should I let it
2342 1.2.6.2 yamt * match?
2343 1.2.6.2 yamt */
2344 1.2.6.2 yamt LIST_FOREACH(stp, &lfp->lf_deleg, ls_file) {
2345 1.2.6.2 yamt if (!(stp->ls_flags & NFSLCK_OLDDELEG) &&
2346 1.2.6.2 yamt stateidp->seqid == stp->ls_stateid.seqid &&
2347 1.2.6.2 yamt !NFSBCMP(stateidp->other, stp->ls_stateid.other,
2348 1.2.6.2 yamt NFSX_STATEIDOTHER))
2349 1.2.6.2 yamt break;
2350 1.2.6.2 yamt }
2351 1.2.6.2 yamt if (stp == NULL ||
2352 1.2.6.2 yamt ((new_stp->ls_flags & NFSLCK_WRITEACCESS) &&
2353 1.2.6.2 yamt (stp->ls_flags & NFSLCK_DELEGREAD))) {
2354 1.2.6.2 yamt NFSUNLOCKSTATE();
2355 1.2.6.2 yamt printf("Nfsd openctrl unexpected expiry\n");
2356 1.2.6.2 yamt free((caddr_t)new_open, M_NFSDSTATE);
2357 1.2.6.2 yamt free((caddr_t)new_deleg, M_NFSDSTATE);
2358 1.2.6.2 yamt if (haslock) {
2359 1.2.6.2 yamt NFSLOCKV4ROOTMUTEX();
2360 1.2.6.2 yamt nfsv4_unlock(&nfsv4rootfs_lock, 1);
2361 1.2.6.2 yamt NFSUNLOCKV4ROOTMUTEX();
2362 1.2.6.2 yamt }
2363 1.2.6.2 yamt error = NFSERR_EXPIRED;
2364 1.2.6.2 yamt goto out;
2365 1.2.6.2 yamt }
2366 1.2.6.2 yamt
2367 1.2.6.2 yamt /*
2368 1.2.6.2 yamt * Don't issue a Delegation, since one already exists and
2369 1.2.6.2 yamt * delay delegation timeout, as required.
2370 1.2.6.2 yamt */
2371 1.2.6.2 yamt delegate = 0;
2372 1.2.6.2 yamt nfsrv_delaydelegtimeout(stp);
2373 1.2.6.2 yamt }
2374 1.2.6.2 yamt
2375 1.2.6.2 yamt /*
2376 1.2.6.2 yamt * Check for access/deny bit conflicts. I also check for the
2377 1.2.6.2 yamt * same owner, since the client might not have bothered to check.
2378 1.2.6.2 yamt * Also, note an open for the same file and owner, if found,
2379 1.2.6.2 yamt * which is all we do here for Delegate_Cur, since conflict
2380 1.2.6.2 yamt * checking is already done.
2381 1.2.6.2 yamt */
2382 1.2.6.2 yamt LIST_FOREACH(stp, &lfp->lf_open, ls_file) {
2383 1.2.6.2 yamt if (ownerstp && stp->ls_openowner == ownerstp)
2384 1.2.6.2 yamt openstp = stp;
2385 1.2.6.2 yamt if (!(new_stp->ls_flags & NFSLCK_DELEGCUR)) {
2386 1.2.6.2 yamt /*
2387 1.2.6.2 yamt * If another client has the file open, the only
2388 1.2.6.2 yamt * delegation that can be issued is a Read delegation
2389 1.2.6.2 yamt * and only if it is a Read open with Deny none.
2390 1.2.6.2 yamt */
2391 1.2.6.2 yamt if (clp != stp->ls_clp) {
2392 1.2.6.2 yamt if ((stp->ls_flags & NFSLCK_SHAREBITS) ==
2393 1.2.6.2 yamt NFSLCK_READACCESS)
2394 1.2.6.2 yamt writedeleg = 0;
2395 1.2.6.2 yamt else
2396 1.2.6.2 yamt delegate = 0;
2397 1.2.6.2 yamt }
2398 1.2.6.2 yamt if(((new_stp->ls_flags & NFSLCK_ACCESSBITS) &
2399 1.2.6.2 yamt ((stp->ls_flags>>NFSLCK_SHIFT) & NFSLCK_ACCESSBITS))||
2400 1.2.6.2 yamt ((stp->ls_flags & NFSLCK_ACCESSBITS) &
2401 1.2.6.2 yamt ((new_stp->ls_flags>>NFSLCK_SHIFT)&NFSLCK_ACCESSBITS))){
2402 1.2.6.2 yamt ret = nfsrv_clientconflict(stp->ls_clp,&haslock,vp,p);
2403 1.2.6.2 yamt if (ret == 1) {
2404 1.2.6.2 yamt /*
2405 1.2.6.2 yamt * nfsrv_clientconflict() unlocks state
2406 1.2.6.2 yamt * when it returns non-zero.
2407 1.2.6.2 yamt */
2408 1.2.6.2 yamt free((caddr_t)new_open, M_NFSDSTATE);
2409 1.2.6.2 yamt free((caddr_t)new_deleg, M_NFSDSTATE);
2410 1.2.6.2 yamt openstp = NULL;
2411 1.2.6.2 yamt goto tryagain;
2412 1.2.6.2 yamt }
2413 1.2.6.2 yamt if (ret == 2)
2414 1.2.6.2 yamt error = NFSERR_PERM;
2415 1.2.6.2 yamt else if (new_stp->ls_flags & NFSLCK_RECLAIM)
2416 1.2.6.2 yamt error = NFSERR_RECLAIMCONFLICT;
2417 1.2.6.2 yamt else
2418 1.2.6.2 yamt error = NFSERR_SHAREDENIED;
2419 1.2.6.2 yamt if (ret == 0)
2420 1.2.6.2 yamt NFSUNLOCKSTATE();
2421 1.2.6.2 yamt if (haslock) {
2422 1.2.6.2 yamt NFSLOCKV4ROOTMUTEX();
2423 1.2.6.2 yamt nfsv4_unlock(&nfsv4rootfs_lock, 1);
2424 1.2.6.2 yamt NFSUNLOCKV4ROOTMUTEX();
2425 1.2.6.2 yamt }
2426 1.2.6.2 yamt free((caddr_t)new_open, M_NFSDSTATE);
2427 1.2.6.2 yamt free((caddr_t)new_deleg, M_NFSDSTATE);
2428 1.2.6.2 yamt printf("nfsd openctrl unexpected client cnfl\n");
2429 1.2.6.2 yamt goto out;
2430 1.2.6.2 yamt }
2431 1.2.6.2 yamt }
2432 1.2.6.2 yamt }
2433 1.2.6.2 yamt
2434 1.2.6.2 yamt /*
2435 1.2.6.2 yamt * Check for a conflicting delegation. If one is found, call
2436 1.2.6.2 yamt * nfsrv_delegconflict() to handle it. If the v4root lock hasn't
2437 1.2.6.2 yamt * been set yet, it will get the lock. Otherwise, it will recall
2438 1.2.6.2 yamt * the delegation. Then, we try try again...
2439 1.2.6.2 yamt * (If NFSLCK_DELEGCUR is set, it has a delegation, so there
2440 1.2.6.2 yamt * isn't a conflict.)
2441 1.2.6.2 yamt * I currently believe the conflict algorithm to be:
2442 1.2.6.2 yamt * For Open with Read Access and Deny None
2443 1.2.6.2 yamt * - there is a conflict iff a different client has a write delegation
2444 1.2.6.2 yamt * For Open with other Write Access or any Deny except None
2445 1.2.6.2 yamt * - there is a conflict if a different client has any delegation
2446 1.2.6.2 yamt * - there is a conflict if the same client has a read delegation
2447 1.2.6.2 yamt * (The current concensus is that this last case should be
2448 1.2.6.2 yamt * considered a conflict since the client with a read delegation
2449 1.2.6.2 yamt * could have done an Open with ReadAccess and WriteDeny
2450 1.2.6.2 yamt * locally and then not have checked for the WriteDeny.)
2451 1.2.6.2 yamt */
2452 1.2.6.2 yamt if (!(new_stp->ls_flags & (NFSLCK_DELEGPREV | NFSLCK_DELEGCUR))) {
2453 1.2.6.2 yamt stp = LIST_FIRST(&lfp->lf_deleg);
2454 1.2.6.2 yamt while (stp != NULL) {
2455 1.2.6.2 yamt nstp = LIST_NEXT(stp, ls_file);
2456 1.2.6.2 yamt if (stp->ls_clp != clp && (stp->ls_flags & NFSLCK_DELEGREAD))
2457 1.2.6.2 yamt writedeleg = 0;
2458 1.2.6.2 yamt else
2459 1.2.6.2 yamt delegate = 0;
2460 1.2.6.2 yamt if ((readonly && stp->ls_clp != clp &&
2461 1.2.6.2 yamt (stp->ls_flags & NFSLCK_DELEGWRITE)) ||
2462 1.2.6.2 yamt (!readonly && (stp->ls_clp != clp ||
2463 1.2.6.2 yamt (stp->ls_flags & NFSLCK_DELEGREAD)))) {
2464 1.2.6.2 yamt if (new_stp->ls_flags & NFSLCK_RECLAIM) {
2465 1.2.6.2 yamt delegate = 2;
2466 1.2.6.2 yamt } else {
2467 1.2.6.2 yamt ret = nfsrv_delegconflict(stp, &haslock, p, vp);
2468 1.2.6.2 yamt if (ret) {
2469 1.2.6.2 yamt /*
2470 1.2.6.2 yamt * nfsrv_delegconflict() unlocks state
2471 1.2.6.2 yamt * when it returns non-zero.
2472 1.2.6.2 yamt */
2473 1.2.6.2 yamt printf("Nfsd openctrl unexpected deleg cnfl\n");
2474 1.2.6.2 yamt free((caddr_t)new_open, M_NFSDSTATE);
2475 1.2.6.2 yamt free((caddr_t)new_deleg, M_NFSDSTATE);
2476 1.2.6.2 yamt if (ret == -1) {
2477 1.2.6.2 yamt openstp = NULL;
2478 1.2.6.2 yamt goto tryagain;
2479 1.2.6.2 yamt }
2480 1.2.6.2 yamt error = ret;
2481 1.2.6.2 yamt goto out;
2482 1.2.6.2 yamt }
2483 1.2.6.2 yamt }
2484 1.2.6.2 yamt }
2485 1.2.6.2 yamt stp = nstp;
2486 1.2.6.2 yamt }
2487 1.2.6.2 yamt }
2488 1.2.6.2 yamt
2489 1.2.6.2 yamt /*
2490 1.2.6.2 yamt * We only get here if there was no open that conflicted.
2491 1.2.6.2 yamt * If an open for the owner exists, or in the access/deny bits.
2492 1.2.6.2 yamt * Otherwise it is a new open. If the open_owner hasn't been
2493 1.2.6.2 yamt * confirmed, replace the open with the new one needing confirmation,
2494 1.2.6.2 yamt * otherwise add the open.
2495 1.2.6.2 yamt */
2496 1.2.6.2 yamt if (new_stp->ls_flags & NFSLCK_DELEGPREV) {
2497 1.2.6.2 yamt /*
2498 1.2.6.2 yamt * Handle NFSLCK_DELEGPREV by searching the old delegations for
2499 1.2.6.2 yamt * a match. If found, just move the old delegation to the current
2500 1.2.6.2 yamt * delegation list and issue open. If not found, return
2501 1.2.6.2 yamt * NFSERR_EXPIRED.
2502 1.2.6.2 yamt */
2503 1.2.6.2 yamt LIST_FOREACH(stp, &clp->lc_olddeleg, ls_list) {
2504 1.2.6.2 yamt if (stp->ls_lfp == lfp) {
2505 1.2.6.2 yamt /* Found it */
2506 1.2.6.2 yamt if (stp->ls_clp != clp)
2507 1.2.6.2 yamt panic("olddeleg clp");
2508 1.2.6.2 yamt LIST_REMOVE(stp, ls_list);
2509 1.2.6.2 yamt LIST_REMOVE(stp, ls_hash);
2510 1.2.6.2 yamt stp->ls_flags &= ~NFSLCK_OLDDELEG;
2511 1.2.6.2 yamt stp->ls_stateid.seqid = delegstateidp->seqid = 0;
2512 1.2.6.2 yamt stp->ls_stateid.other[0] = delegstateidp->other[0] =
2513 1.2.6.2 yamt clp->lc_clientid.lval[0];
2514 1.2.6.2 yamt stp->ls_stateid.other[1] = delegstateidp->other[1] =
2515 1.2.6.2 yamt clp->lc_clientid.lval[1];
2516 1.2.6.2 yamt stp->ls_stateid.other[2] = delegstateidp->other[2] =
2517 1.2.6.2 yamt nfsrv_nextstateindex(clp);
2518 1.2.6.2 yamt stp->ls_compref = nd->nd_compref;
2519 1.2.6.2 yamt LIST_INSERT_HEAD(&clp->lc_deleg, stp, ls_list);
2520 1.2.6.2 yamt LIST_INSERT_HEAD(NFSSTATEHASH(clp,
2521 1.2.6.2 yamt stp->ls_stateid), stp, ls_hash);
2522 1.2.6.2 yamt if (stp->ls_flags & NFSLCK_DELEGWRITE)
2523 1.2.6.2 yamt *rflagsp |= NFSV4OPEN_WRITEDELEGATE;
2524 1.2.6.2 yamt else
2525 1.2.6.2 yamt *rflagsp |= NFSV4OPEN_READDELEGATE;
2526 1.2.6.2 yamt clp->lc_delegtime = NFSD_MONOSEC +
2527 1.2.6.2 yamt nfsrv_lease + NFSRV_LEASEDELTA;
2528 1.2.6.2 yamt
2529 1.2.6.2 yamt /*
2530 1.2.6.2 yamt * Now, do the associated open.
2531 1.2.6.2 yamt */
2532 1.2.6.2 yamt new_open->ls_stateid.seqid = 0;
2533 1.2.6.2 yamt new_open->ls_stateid.other[0] = clp->lc_clientid.lval[0];
2534 1.2.6.2 yamt new_open->ls_stateid.other[1] = clp->lc_clientid.lval[1];
2535 1.2.6.2 yamt new_open->ls_stateid.other[2] = nfsrv_nextstateindex(clp);
2536 1.2.6.2 yamt new_open->ls_flags = (new_stp->ls_flags&NFSLCK_DENYBITS)|
2537 1.2.6.2 yamt NFSLCK_OPEN;
2538 1.2.6.2 yamt if (stp->ls_flags & NFSLCK_DELEGWRITE)
2539 1.2.6.2 yamt new_open->ls_flags |= (NFSLCK_READACCESS |
2540 1.2.6.2 yamt NFSLCK_WRITEACCESS);
2541 1.2.6.2 yamt else
2542 1.2.6.2 yamt new_open->ls_flags |= NFSLCK_READACCESS;
2543 1.2.6.2 yamt new_open->ls_uid = new_stp->ls_uid;
2544 1.2.6.2 yamt new_open->ls_lfp = lfp;
2545 1.2.6.2 yamt new_open->ls_clp = clp;
2546 1.2.6.2 yamt LIST_INIT(&new_open->ls_open);
2547 1.2.6.2 yamt LIST_INSERT_HEAD(&lfp->lf_open, new_open, ls_file);
2548 1.2.6.2 yamt LIST_INSERT_HEAD(NFSSTATEHASH(clp, new_open->ls_stateid),
2549 1.2.6.2 yamt new_open, ls_hash);
2550 1.2.6.2 yamt /*
2551 1.2.6.2 yamt * and handle the open owner
2552 1.2.6.2 yamt */
2553 1.2.6.2 yamt if (ownerstp) {
2554 1.2.6.2 yamt new_open->ls_openowner = ownerstp;
2555 1.2.6.2 yamt LIST_INSERT_HEAD(&ownerstp->ls_open,new_open,ls_list);
2556 1.2.6.2 yamt } else {
2557 1.2.6.2 yamt new_open->ls_openowner = new_stp;
2558 1.2.6.2 yamt new_stp->ls_flags = 0;
2559 1.2.6.2 yamt nfsrvd_refcache(new_stp->ls_op);
2560 1.2.6.2 yamt new_stp->ls_noopens = 0;
2561 1.2.6.2 yamt LIST_INIT(&new_stp->ls_open);
2562 1.2.6.2 yamt LIST_INSERT_HEAD(&new_stp->ls_open, new_open, ls_list);
2563 1.2.6.2 yamt LIST_INSERT_HEAD(&clp->lc_open, new_stp, ls_list);
2564 1.2.6.2 yamt *new_stpp = NULL;
2565 1.2.6.2 yamt newnfsstats.srvopenowners++;
2566 1.2.6.2 yamt nfsrv_openpluslock++;
2567 1.2.6.2 yamt }
2568 1.2.6.2 yamt openstp = new_open;
2569 1.2.6.2 yamt new_open = NULL;
2570 1.2.6.2 yamt newnfsstats.srvopens++;
2571 1.2.6.2 yamt nfsrv_openpluslock++;
2572 1.2.6.2 yamt break;
2573 1.2.6.2 yamt }
2574 1.2.6.2 yamt }
2575 1.2.6.2 yamt if (stp == NULL)
2576 1.2.6.2 yamt error = NFSERR_EXPIRED;
2577 1.2.6.2 yamt } else if (new_stp->ls_flags & (NFSLCK_DELEGREAD | NFSLCK_DELEGWRITE)) {
2578 1.2.6.2 yamt /*
2579 1.2.6.2 yamt * Scan to see that no delegation for this client and file
2580 1.2.6.2 yamt * doesn't already exist.
2581 1.2.6.2 yamt * There also shouldn't yet be an Open for this file and
2582 1.2.6.2 yamt * openowner.
2583 1.2.6.2 yamt */
2584 1.2.6.2 yamt LIST_FOREACH(stp, &lfp->lf_deleg, ls_file) {
2585 1.2.6.2 yamt if (stp->ls_clp == clp)
2586 1.2.6.2 yamt break;
2587 1.2.6.2 yamt }
2588 1.2.6.2 yamt if (stp == NULL && openstp == NULL) {
2589 1.2.6.2 yamt /*
2590 1.2.6.2 yamt * This is the Claim_Previous case with a delegation
2591 1.2.6.2 yamt * type != Delegate_None.
2592 1.2.6.2 yamt */
2593 1.2.6.2 yamt /*
2594 1.2.6.2 yamt * First, add the delegation. (Although we must issue the
2595 1.2.6.2 yamt * delegation, we can also ask for an immediate return.)
2596 1.2.6.2 yamt */
2597 1.2.6.2 yamt new_deleg->ls_stateid.seqid = delegstateidp->seqid = 0;
2598 1.2.6.2 yamt new_deleg->ls_stateid.other[0] = delegstateidp->other[0] =
2599 1.2.6.2 yamt clp->lc_clientid.lval[0];
2600 1.2.6.2 yamt new_deleg->ls_stateid.other[1] = delegstateidp->other[1] =
2601 1.2.6.2 yamt clp->lc_clientid.lval[1];
2602 1.2.6.2 yamt new_deleg->ls_stateid.other[2] = delegstateidp->other[2] =
2603 1.2.6.2 yamt nfsrv_nextstateindex(clp);
2604 1.2.6.2 yamt if (new_stp->ls_flags & NFSLCK_DELEGWRITE) {
2605 1.2.6.2 yamt new_deleg->ls_flags = (NFSLCK_DELEGWRITE |
2606 1.2.6.2 yamt NFSLCK_READACCESS | NFSLCK_WRITEACCESS);
2607 1.2.6.2 yamt *rflagsp |= NFSV4OPEN_WRITEDELEGATE;
2608 1.2.6.2 yamt } else {
2609 1.2.6.2 yamt new_deleg->ls_flags = (NFSLCK_DELEGREAD |
2610 1.2.6.2 yamt NFSLCK_READACCESS);
2611 1.2.6.2 yamt *rflagsp |= NFSV4OPEN_READDELEGATE;
2612 1.2.6.2 yamt }
2613 1.2.6.2 yamt new_deleg->ls_uid = new_stp->ls_uid;
2614 1.2.6.2 yamt new_deleg->ls_lfp = lfp;
2615 1.2.6.2 yamt new_deleg->ls_clp = clp;
2616 1.2.6.2 yamt new_deleg->ls_filerev = filerev;
2617 1.2.6.2 yamt new_deleg->ls_compref = nd->nd_compref;
2618 1.2.6.2 yamt LIST_INSERT_HEAD(&lfp->lf_deleg, new_deleg, ls_file);
2619 1.2.6.2 yamt LIST_INSERT_HEAD(NFSSTATEHASH(clp,
2620 1.2.6.2 yamt new_deleg->ls_stateid), new_deleg, ls_hash);
2621 1.2.6.2 yamt LIST_INSERT_HEAD(&clp->lc_deleg, new_deleg, ls_list);
2622 1.2.6.2 yamt new_deleg = NULL;
2623 1.2.6.2 yamt if (delegate == 2 || nfsrv_issuedelegs == 0 ||
2624 1.2.6.2 yamt (clp->lc_flags & (LCL_CALLBACKSON | LCL_CBDOWN)) !=
2625 1.2.6.2 yamt LCL_CALLBACKSON ||
2626 1.2.6.2 yamt NFSRV_V4DELEGLIMIT(nfsrv_delegatecnt) ||
2627 1.2.6.2 yamt !NFSVNO_DELEGOK(vp))
2628 1.2.6.2 yamt *rflagsp |= NFSV4OPEN_RECALL;
2629 1.2.6.2 yamt newnfsstats.srvdelegates++;
2630 1.2.6.2 yamt nfsrv_openpluslock++;
2631 1.2.6.2 yamt nfsrv_delegatecnt++;
2632 1.2.6.2 yamt
2633 1.2.6.2 yamt /*
2634 1.2.6.2 yamt * Now, do the associated open.
2635 1.2.6.2 yamt */
2636 1.2.6.2 yamt new_open->ls_stateid.seqid = 0;
2637 1.2.6.2 yamt new_open->ls_stateid.other[0] = clp->lc_clientid.lval[0];
2638 1.2.6.2 yamt new_open->ls_stateid.other[1] = clp->lc_clientid.lval[1];
2639 1.2.6.2 yamt new_open->ls_stateid.other[2] = nfsrv_nextstateindex(clp);
2640 1.2.6.2 yamt new_open->ls_flags = (new_stp->ls_flags & NFSLCK_DENYBITS) |
2641 1.2.6.2 yamt NFSLCK_OPEN;
2642 1.2.6.2 yamt if (new_stp->ls_flags & NFSLCK_DELEGWRITE)
2643 1.2.6.2 yamt new_open->ls_flags |= (NFSLCK_READACCESS |
2644 1.2.6.2 yamt NFSLCK_WRITEACCESS);
2645 1.2.6.2 yamt else
2646 1.2.6.2 yamt new_open->ls_flags |= NFSLCK_READACCESS;
2647 1.2.6.2 yamt new_open->ls_uid = new_stp->ls_uid;
2648 1.2.6.2 yamt new_open->ls_lfp = lfp;
2649 1.2.6.2 yamt new_open->ls_clp = clp;
2650 1.2.6.2 yamt LIST_INIT(&new_open->ls_open);
2651 1.2.6.2 yamt LIST_INSERT_HEAD(&lfp->lf_open, new_open, ls_file);
2652 1.2.6.2 yamt LIST_INSERT_HEAD(NFSSTATEHASH(clp, new_open->ls_stateid),
2653 1.2.6.2 yamt new_open, ls_hash);
2654 1.2.6.2 yamt /*
2655 1.2.6.2 yamt * and handle the open owner
2656 1.2.6.2 yamt */
2657 1.2.6.2 yamt if (ownerstp) {
2658 1.2.6.2 yamt new_open->ls_openowner = ownerstp;
2659 1.2.6.2 yamt LIST_INSERT_HEAD(&ownerstp->ls_open, new_open, ls_list);
2660 1.2.6.2 yamt } else {
2661 1.2.6.2 yamt new_open->ls_openowner = new_stp;
2662 1.2.6.2 yamt new_stp->ls_flags = 0;
2663 1.2.6.2 yamt nfsrvd_refcache(new_stp->ls_op);
2664 1.2.6.2 yamt new_stp->ls_noopens = 0;
2665 1.2.6.2 yamt LIST_INIT(&new_stp->ls_open);
2666 1.2.6.2 yamt LIST_INSERT_HEAD(&new_stp->ls_open, new_open, ls_list);
2667 1.2.6.2 yamt LIST_INSERT_HEAD(&clp->lc_open, new_stp, ls_list);
2668 1.2.6.2 yamt *new_stpp = NULL;
2669 1.2.6.2 yamt newnfsstats.srvopenowners++;
2670 1.2.6.2 yamt nfsrv_openpluslock++;
2671 1.2.6.2 yamt }
2672 1.2.6.2 yamt openstp = new_open;
2673 1.2.6.2 yamt new_open = NULL;
2674 1.2.6.2 yamt newnfsstats.srvopens++;
2675 1.2.6.2 yamt nfsrv_openpluslock++;
2676 1.2.6.2 yamt } else {
2677 1.2.6.2 yamt error = NFSERR_RECLAIMCONFLICT;
2678 1.2.6.2 yamt }
2679 1.2.6.2 yamt } else if (ownerstp) {
2680 1.2.6.2 yamt if (ownerstp->ls_flags & NFSLCK_NEEDSCONFIRM) {
2681 1.2.6.2 yamt /* Replace the open */
2682 1.2.6.2 yamt if (ownerstp->ls_op)
2683 1.2.6.2 yamt nfsrvd_derefcache(ownerstp->ls_op);
2684 1.2.6.2 yamt ownerstp->ls_op = new_stp->ls_op;
2685 1.2.6.2 yamt nfsrvd_refcache(ownerstp->ls_op);
2686 1.2.6.2 yamt ownerstp->ls_seq = new_stp->ls_seq;
2687 1.2.6.2 yamt *rflagsp |= NFSV4OPEN_RESULTCONFIRM;
2688 1.2.6.2 yamt stp = LIST_FIRST(&ownerstp->ls_open);
2689 1.2.6.2 yamt stp->ls_flags = (new_stp->ls_flags & NFSLCK_SHAREBITS) |
2690 1.2.6.2 yamt NFSLCK_OPEN;
2691 1.2.6.2 yamt stp->ls_stateid.seqid = 0;
2692 1.2.6.2 yamt stp->ls_uid = new_stp->ls_uid;
2693 1.2.6.2 yamt if (lfp != stp->ls_lfp) {
2694 1.2.6.2 yamt LIST_REMOVE(stp, ls_file);
2695 1.2.6.2 yamt LIST_INSERT_HEAD(&lfp->lf_open, stp, ls_file);
2696 1.2.6.2 yamt stp->ls_lfp = lfp;
2697 1.2.6.2 yamt }
2698 1.2.6.2 yamt openstp = stp;
2699 1.2.6.2 yamt } else if (openstp) {
2700 1.2.6.2 yamt openstp->ls_flags |= (new_stp->ls_flags & NFSLCK_SHAREBITS);
2701 1.2.6.2 yamt openstp->ls_stateid.seqid++;
2702 1.2.6.2 yamt
2703 1.2.6.2 yamt /*
2704 1.2.6.2 yamt * This is where we can choose to issue a delegation.
2705 1.2.6.2 yamt */
2706 1.2.6.2 yamt if (delegate && nfsrv_issuedelegs &&
2707 1.2.6.2 yamt writedeleg && !NFSVNO_EXRDONLY(exp) &&
2708 1.2.6.2 yamt (nfsrv_writedelegifpos || !readonly) &&
2709 1.2.6.2 yamt (clp->lc_flags & (LCL_CALLBACKSON | LCL_CBDOWN)) ==
2710 1.2.6.2 yamt LCL_CALLBACKSON &&
2711 1.2.6.2 yamt !NFSRV_V4DELEGLIMIT(nfsrv_delegatecnt) &&
2712 1.2.6.2 yamt NFSVNO_DELEGOK(vp)) {
2713 1.2.6.2 yamt new_deleg->ls_stateid.seqid = delegstateidp->seqid = 0;
2714 1.2.6.2 yamt new_deleg->ls_stateid.other[0] = delegstateidp->other[0]
2715 1.2.6.2 yamt = clp->lc_clientid.lval[0];
2716 1.2.6.2 yamt new_deleg->ls_stateid.other[1] = delegstateidp->other[1]
2717 1.2.6.2 yamt = clp->lc_clientid.lval[1];
2718 1.2.6.2 yamt new_deleg->ls_stateid.other[2] = delegstateidp->other[2]
2719 1.2.6.2 yamt = nfsrv_nextstateindex(clp);
2720 1.2.6.2 yamt new_deleg->ls_flags = (NFSLCK_DELEGWRITE |
2721 1.2.6.2 yamt NFSLCK_READACCESS | NFSLCK_WRITEACCESS);
2722 1.2.6.2 yamt *rflagsp |= NFSV4OPEN_WRITEDELEGATE;
2723 1.2.6.2 yamt new_deleg->ls_uid = new_stp->ls_uid;
2724 1.2.6.2 yamt new_deleg->ls_lfp = lfp;
2725 1.2.6.2 yamt new_deleg->ls_clp = clp;
2726 1.2.6.2 yamt new_deleg->ls_filerev = filerev;
2727 1.2.6.2 yamt new_deleg->ls_compref = nd->nd_compref;
2728 1.2.6.2 yamt LIST_INSERT_HEAD(&lfp->lf_deleg, new_deleg, ls_file);
2729 1.2.6.2 yamt LIST_INSERT_HEAD(NFSSTATEHASH(clp,
2730 1.2.6.2 yamt new_deleg->ls_stateid), new_deleg, ls_hash);
2731 1.2.6.2 yamt LIST_INSERT_HEAD(&clp->lc_deleg, new_deleg, ls_list);
2732 1.2.6.2 yamt new_deleg = NULL;
2733 1.2.6.2 yamt newnfsstats.srvdelegates++;
2734 1.2.6.2 yamt nfsrv_openpluslock++;
2735 1.2.6.2 yamt nfsrv_delegatecnt++;
2736 1.2.6.2 yamt }
2737 1.2.6.2 yamt } else {
2738 1.2.6.2 yamt new_open->ls_stateid.seqid = 0;
2739 1.2.6.2 yamt new_open->ls_stateid.other[0] = clp->lc_clientid.lval[0];
2740 1.2.6.2 yamt new_open->ls_stateid.other[1] = clp->lc_clientid.lval[1];
2741 1.2.6.2 yamt new_open->ls_stateid.other[2] = nfsrv_nextstateindex(clp);
2742 1.2.6.2 yamt new_open->ls_flags = (new_stp->ls_flags & NFSLCK_SHAREBITS)|
2743 1.2.6.2 yamt NFSLCK_OPEN;
2744 1.2.6.2 yamt new_open->ls_uid = new_stp->ls_uid;
2745 1.2.6.2 yamt new_open->ls_openowner = ownerstp;
2746 1.2.6.2 yamt new_open->ls_lfp = lfp;
2747 1.2.6.2 yamt new_open->ls_clp = clp;
2748 1.2.6.2 yamt LIST_INIT(&new_open->ls_open);
2749 1.2.6.2 yamt LIST_INSERT_HEAD(&lfp->lf_open, new_open, ls_file);
2750 1.2.6.2 yamt LIST_INSERT_HEAD(&ownerstp->ls_open, new_open, ls_list);
2751 1.2.6.2 yamt LIST_INSERT_HEAD(NFSSTATEHASH(clp, new_open->ls_stateid),
2752 1.2.6.2 yamt new_open, ls_hash);
2753 1.2.6.2 yamt openstp = new_open;
2754 1.2.6.2 yamt new_open = NULL;
2755 1.2.6.2 yamt newnfsstats.srvopens++;
2756 1.2.6.2 yamt nfsrv_openpluslock++;
2757 1.2.6.2 yamt
2758 1.2.6.2 yamt /*
2759 1.2.6.2 yamt * This is where we can choose to issue a delegation.
2760 1.2.6.2 yamt */
2761 1.2.6.2 yamt if (delegate && nfsrv_issuedelegs &&
2762 1.2.6.2 yamt (writedeleg || readonly) &&
2763 1.2.6.2 yamt (clp->lc_flags & (LCL_CALLBACKSON | LCL_CBDOWN)) ==
2764 1.2.6.2 yamt LCL_CALLBACKSON &&
2765 1.2.6.2 yamt !NFSRV_V4DELEGLIMIT(nfsrv_delegatecnt) &&
2766 1.2.6.2 yamt NFSVNO_DELEGOK(vp)) {
2767 1.2.6.2 yamt new_deleg->ls_stateid.seqid = delegstateidp->seqid = 0;
2768 1.2.6.2 yamt new_deleg->ls_stateid.other[0] = delegstateidp->other[0]
2769 1.2.6.2 yamt = clp->lc_clientid.lval[0];
2770 1.2.6.2 yamt new_deleg->ls_stateid.other[1] = delegstateidp->other[1]
2771 1.2.6.2 yamt = clp->lc_clientid.lval[1];
2772 1.2.6.2 yamt new_deleg->ls_stateid.other[2] = delegstateidp->other[2]
2773 1.2.6.2 yamt = nfsrv_nextstateindex(clp);
2774 1.2.6.2 yamt if (writedeleg && !NFSVNO_EXRDONLY(exp) &&
2775 1.2.6.2 yamt (nfsrv_writedelegifpos || !readonly)) {
2776 1.2.6.2 yamt new_deleg->ls_flags = (NFSLCK_DELEGWRITE |
2777 1.2.6.2 yamt NFSLCK_READACCESS | NFSLCK_WRITEACCESS);
2778 1.2.6.2 yamt *rflagsp |= NFSV4OPEN_WRITEDELEGATE;
2779 1.2.6.2 yamt } else {
2780 1.2.6.2 yamt new_deleg->ls_flags = (NFSLCK_DELEGREAD |
2781 1.2.6.2 yamt NFSLCK_READACCESS);
2782 1.2.6.2 yamt *rflagsp |= NFSV4OPEN_READDELEGATE;
2783 1.2.6.2 yamt }
2784 1.2.6.2 yamt new_deleg->ls_uid = new_stp->ls_uid;
2785 1.2.6.2 yamt new_deleg->ls_lfp = lfp;
2786 1.2.6.2 yamt new_deleg->ls_clp = clp;
2787 1.2.6.2 yamt new_deleg->ls_filerev = filerev;
2788 1.2.6.2 yamt new_deleg->ls_compref = nd->nd_compref;
2789 1.2.6.2 yamt LIST_INSERT_HEAD(&lfp->lf_deleg, new_deleg, ls_file);
2790 1.2.6.2 yamt LIST_INSERT_HEAD(NFSSTATEHASH(clp,
2791 1.2.6.2 yamt new_deleg->ls_stateid), new_deleg, ls_hash);
2792 1.2.6.2 yamt LIST_INSERT_HEAD(&clp->lc_deleg, new_deleg, ls_list);
2793 1.2.6.2 yamt new_deleg = NULL;
2794 1.2.6.2 yamt newnfsstats.srvdelegates++;
2795 1.2.6.2 yamt nfsrv_openpluslock++;
2796 1.2.6.2 yamt nfsrv_delegatecnt++;
2797 1.2.6.2 yamt }
2798 1.2.6.2 yamt }
2799 1.2.6.2 yamt } else {
2800 1.2.6.2 yamt /*
2801 1.2.6.2 yamt * New owner case. Start the open_owner sequence with a
2802 1.2.6.2 yamt * Needs confirmation (unless a reclaim) and hang the
2803 1.2.6.2 yamt * new open off it.
2804 1.2.6.2 yamt */
2805 1.2.6.2 yamt new_open->ls_stateid.seqid = 0;
2806 1.2.6.2 yamt new_open->ls_stateid.other[0] = clp->lc_clientid.lval[0];
2807 1.2.6.2 yamt new_open->ls_stateid.other[1] = clp->lc_clientid.lval[1];
2808 1.2.6.2 yamt new_open->ls_stateid.other[2] = nfsrv_nextstateindex(clp);
2809 1.2.6.2 yamt new_open->ls_flags = (new_stp->ls_flags & NFSLCK_SHAREBITS) |
2810 1.2.6.2 yamt NFSLCK_OPEN;
2811 1.2.6.2 yamt new_open->ls_uid = new_stp->ls_uid;
2812 1.2.6.2 yamt LIST_INIT(&new_open->ls_open);
2813 1.2.6.2 yamt new_open->ls_openowner = new_stp;
2814 1.2.6.2 yamt new_open->ls_lfp = lfp;
2815 1.2.6.2 yamt new_open->ls_clp = clp;
2816 1.2.6.2 yamt LIST_INSERT_HEAD(&lfp->lf_open, new_open, ls_file);
2817 1.2.6.2 yamt if (new_stp->ls_flags & NFSLCK_RECLAIM) {
2818 1.2.6.2 yamt new_stp->ls_flags = 0;
2819 1.2.6.2 yamt } else {
2820 1.2.6.2 yamt *rflagsp |= NFSV4OPEN_RESULTCONFIRM;
2821 1.2.6.2 yamt new_stp->ls_flags = NFSLCK_NEEDSCONFIRM;
2822 1.2.6.2 yamt }
2823 1.2.6.2 yamt nfsrvd_refcache(new_stp->ls_op);
2824 1.2.6.2 yamt new_stp->ls_noopens = 0;
2825 1.2.6.2 yamt LIST_INIT(&new_stp->ls_open);
2826 1.2.6.2 yamt LIST_INSERT_HEAD(&new_stp->ls_open, new_open, ls_list);
2827 1.2.6.2 yamt LIST_INSERT_HEAD(&clp->lc_open, new_stp, ls_list);
2828 1.2.6.2 yamt LIST_INSERT_HEAD(NFSSTATEHASH(clp, new_open->ls_stateid),
2829 1.2.6.2 yamt new_open, ls_hash);
2830 1.2.6.2 yamt openstp = new_open;
2831 1.2.6.2 yamt new_open = NULL;
2832 1.2.6.2 yamt *new_stpp = NULL;
2833 1.2.6.2 yamt newnfsstats.srvopens++;
2834 1.2.6.2 yamt nfsrv_openpluslock++;
2835 1.2.6.2 yamt newnfsstats.srvopenowners++;
2836 1.2.6.2 yamt nfsrv_openpluslock++;
2837 1.2.6.2 yamt }
2838 1.2.6.2 yamt if (!error) {
2839 1.2.6.2 yamt stateidp->seqid = openstp->ls_stateid.seqid;
2840 1.2.6.2 yamt stateidp->other[0] = openstp->ls_stateid.other[0];
2841 1.2.6.2 yamt stateidp->other[1] = openstp->ls_stateid.other[1];
2842 1.2.6.2 yamt stateidp->other[2] = openstp->ls_stateid.other[2];
2843 1.2.6.2 yamt }
2844 1.2.6.2 yamt NFSUNLOCKSTATE();
2845 1.2.6.2 yamt if (haslock) {
2846 1.2.6.2 yamt NFSLOCKV4ROOTMUTEX();
2847 1.2.6.2 yamt nfsv4_unlock(&nfsv4rootfs_lock, 1);
2848 1.2.6.2 yamt NFSUNLOCKV4ROOTMUTEX();
2849 1.2.6.2 yamt }
2850 1.2.6.2 yamt if (new_open)
2851 1.2.6.2 yamt FREE((caddr_t)new_open, M_NFSDSTATE);
2852 1.2.6.2 yamt if (new_deleg)
2853 1.2.6.2 yamt FREE((caddr_t)new_deleg, M_NFSDSTATE);
2854 1.2.6.2 yamt
2855 1.2.6.2 yamt out:
2856 1.2.6.2 yamt NFSEXITCODE2(error, nd);
2857 1.2.6.2 yamt return (error);
2858 1.2.6.2 yamt }
2859 1.2.6.2 yamt
2860 1.2.6.2 yamt /*
2861 1.2.6.2 yamt * Open update. Does the confirm, downgrade and close.
2862 1.2.6.2 yamt */
2863 1.2.6.2 yamt APPLESTATIC int
2864 1.2.6.2 yamt nfsrv_openupdate(vnode_t vp, struct nfsstate *new_stp, nfsquad_t clientid,
2865 1.2.6.2 yamt nfsv4stateid_t *stateidp, struct nfsrv_descript *nd, NFSPROC_T *p)
2866 1.2.6.2 yamt {
2867 1.2.6.2 yamt struct nfsstate *stp, *ownerstp;
2868 1.2.6.2 yamt struct nfsclient *clp;
2869 1.2.6.2 yamt struct nfslockfile *lfp;
2870 1.2.6.2 yamt u_int32_t bits;
2871 1.2.6.2 yamt int error = 0, gotstate = 0, len = 0;
2872 1.2.6.2 yamt u_char client[NFSV4_OPAQUELIMIT];
2873 1.2.6.2 yamt
2874 1.2.6.2 yamt /*
2875 1.2.6.2 yamt * Check for restart conditions (client and server).
2876 1.2.6.2 yamt */
2877 1.2.6.2 yamt error = nfsrv_checkrestart(clientid, new_stp->ls_flags,
2878 1.2.6.2 yamt &new_stp->ls_stateid, 0);
2879 1.2.6.2 yamt if (error)
2880 1.2.6.2 yamt goto out;
2881 1.2.6.2 yamt
2882 1.2.6.2 yamt NFSLOCKSTATE();
2883 1.2.6.2 yamt /*
2884 1.2.6.2 yamt * Get the open structure via clientid and stateid.
2885 1.2.6.2 yamt */
2886 1.2.6.2 yamt error = nfsrv_getclient(clientid, CLOPS_RENEW, &clp,
2887 1.2.6.2 yamt (nfsquad_t)((u_quad_t)0), NULL, p);
2888 1.2.6.2 yamt if (!error)
2889 1.2.6.2 yamt error = nfsrv_getstate(clp, &new_stp->ls_stateid,
2890 1.2.6.2 yamt new_stp->ls_flags, &stp);
2891 1.2.6.2 yamt
2892 1.2.6.2 yamt /*
2893 1.2.6.2 yamt * Sanity check the open.
2894 1.2.6.2 yamt */
2895 1.2.6.2 yamt if (!error && (!(stp->ls_flags & NFSLCK_OPEN) ||
2896 1.2.6.2 yamt (!(new_stp->ls_flags & NFSLCK_CONFIRM) &&
2897 1.2.6.2 yamt (stp->ls_openowner->ls_flags & NFSLCK_NEEDSCONFIRM)) ||
2898 1.2.6.2 yamt ((new_stp->ls_flags & NFSLCK_CONFIRM) &&
2899 1.2.6.2 yamt (!(stp->ls_openowner->ls_flags & NFSLCK_NEEDSCONFIRM)))))
2900 1.2.6.2 yamt error = NFSERR_BADSTATEID;
2901 1.2.6.2 yamt
2902 1.2.6.2 yamt if (!error)
2903 1.2.6.2 yamt error = nfsrv_checkseqid(nd, new_stp->ls_seq,
2904 1.2.6.2 yamt stp->ls_openowner, new_stp->ls_op);
2905 1.2.6.2 yamt if (!error && stp->ls_stateid.seqid != new_stp->ls_stateid.seqid &&
2906 1.2.6.2 yamt !(new_stp->ls_flags & NFSLCK_CONFIRM))
2907 1.2.6.2 yamt error = NFSERR_OLDSTATEID;
2908 1.2.6.2 yamt if (!error && vnode_vtype(vp) != VREG) {
2909 1.2.6.2 yamt if (vnode_vtype(vp) == VDIR)
2910 1.2.6.2 yamt error = NFSERR_ISDIR;
2911 1.2.6.2 yamt else
2912 1.2.6.2 yamt error = NFSERR_INVAL;
2913 1.2.6.2 yamt }
2914 1.2.6.2 yamt
2915 1.2.6.2 yamt if (error) {
2916 1.2.6.2 yamt /*
2917 1.2.6.2 yamt * If a client tries to confirm an Open with a bad
2918 1.2.6.2 yamt * seqid# and there are no byte range locks or other Opens
2919 1.2.6.2 yamt * on the openowner, just throw it away, so the next use of the
2920 1.2.6.2 yamt * openowner will start a fresh seq#.
2921 1.2.6.2 yamt */
2922 1.2.6.2 yamt if (error == NFSERR_BADSEQID &&
2923 1.2.6.2 yamt (new_stp->ls_flags & NFSLCK_CONFIRM) &&
2924 1.2.6.2 yamt nfsrv_nootherstate(stp))
2925 1.2.6.2 yamt nfsrv_freeopenowner(stp->ls_openowner, 0, p);
2926 1.2.6.2 yamt NFSUNLOCKSTATE();
2927 1.2.6.2 yamt goto out;
2928 1.2.6.2 yamt }
2929 1.2.6.2 yamt
2930 1.2.6.2 yamt /*
2931 1.2.6.2 yamt * Set the return stateid.
2932 1.2.6.2 yamt */
2933 1.2.6.2 yamt stateidp->seqid = stp->ls_stateid.seqid + 1;
2934 1.2.6.2 yamt stateidp->other[0] = stp->ls_stateid.other[0];
2935 1.2.6.2 yamt stateidp->other[1] = stp->ls_stateid.other[1];
2936 1.2.6.2 yamt stateidp->other[2] = stp->ls_stateid.other[2];
2937 1.2.6.2 yamt /*
2938 1.2.6.2 yamt * Now, handle the three cases.
2939 1.2.6.2 yamt */
2940 1.2.6.2 yamt if (new_stp->ls_flags & NFSLCK_CONFIRM) {
2941 1.2.6.2 yamt /*
2942 1.2.6.2 yamt * If the open doesn't need confirmation, it seems to me that
2943 1.2.6.2 yamt * there is a client error, but I'll just log it and keep going?
2944 1.2.6.2 yamt */
2945 1.2.6.2 yamt if (!(stp->ls_openowner->ls_flags & NFSLCK_NEEDSCONFIRM))
2946 1.2.6.2 yamt printf("Nfsv4d: stray open confirm\n");
2947 1.2.6.2 yamt stp->ls_openowner->ls_flags = 0;
2948 1.2.6.2 yamt stp->ls_stateid.seqid++;
2949 1.2.6.2 yamt if (!(clp->lc_flags & LCL_STAMPEDSTABLE)) {
2950 1.2.6.2 yamt clp->lc_flags |= LCL_STAMPEDSTABLE;
2951 1.2.6.2 yamt len = clp->lc_idlen;
2952 1.2.6.2 yamt NFSBCOPY(clp->lc_id, client, len);
2953 1.2.6.2 yamt gotstate = 1;
2954 1.2.6.2 yamt }
2955 1.2.6.2 yamt NFSUNLOCKSTATE();
2956 1.2.6.2 yamt } else if (new_stp->ls_flags & NFSLCK_CLOSE) {
2957 1.2.6.2 yamt ownerstp = stp->ls_openowner;
2958 1.2.6.2 yamt lfp = stp->ls_lfp;
2959 1.2.6.2 yamt if (nfsrv_dolocallocks != 0 && !LIST_EMPTY(&stp->ls_open)) {
2960 1.2.6.2 yamt /* Get the lf lock */
2961 1.2.6.2 yamt nfsrv_locklf(lfp);
2962 1.2.6.2 yamt NFSUNLOCKSTATE();
2963 1.2.6.2 yamt if (nfsrv_freeopen(stp, vp, 1, p) == 0) {
2964 1.2.6.2 yamt NFSLOCKSTATE();
2965 1.2.6.2 yamt nfsrv_unlocklf(lfp);
2966 1.2.6.2 yamt NFSUNLOCKSTATE();
2967 1.2.6.2 yamt }
2968 1.2.6.2 yamt } else {
2969 1.2.6.2 yamt (void) nfsrv_freeopen(stp, NULL, 0, p);
2970 1.2.6.2 yamt NFSUNLOCKSTATE();
2971 1.2.6.2 yamt }
2972 1.2.6.2 yamt } else {
2973 1.2.6.2 yamt /*
2974 1.2.6.2 yamt * Update the share bits, making sure that the new set are a
2975 1.2.6.2 yamt * subset of the old ones.
2976 1.2.6.2 yamt */
2977 1.2.6.2 yamt bits = (new_stp->ls_flags & NFSLCK_SHAREBITS);
2978 1.2.6.2 yamt if (~(stp->ls_flags) & bits) {
2979 1.2.6.2 yamt NFSUNLOCKSTATE();
2980 1.2.6.2 yamt error = NFSERR_INVAL;
2981 1.2.6.2 yamt goto out;
2982 1.2.6.2 yamt }
2983 1.2.6.2 yamt stp->ls_flags = (bits | NFSLCK_OPEN);
2984 1.2.6.2 yamt stp->ls_stateid.seqid++;
2985 1.2.6.2 yamt NFSUNLOCKSTATE();
2986 1.2.6.2 yamt }
2987 1.2.6.2 yamt
2988 1.2.6.2 yamt /*
2989 1.2.6.2 yamt * If the client just confirmed its first open, write a timestamp
2990 1.2.6.2 yamt * to the stable storage file.
2991 1.2.6.2 yamt */
2992 1.2.6.2 yamt if (gotstate != 0) {
2993 1.2.6.2 yamt nfsrv_writestable(client, len, NFSNST_NEWSTATE, p);
2994 1.2.6.2 yamt nfsrv_backupstable();
2995 1.2.6.2 yamt }
2996 1.2.6.2 yamt
2997 1.2.6.2 yamt out:
2998 1.2.6.2 yamt NFSEXITCODE2(error, nd);
2999 1.2.6.2 yamt return (error);
3000 1.2.6.2 yamt }
3001 1.2.6.2 yamt
3002 1.2.6.2 yamt /*
3003 1.2.6.2 yamt * Delegation update. Does the purge and return.
3004 1.2.6.2 yamt */
3005 1.2.6.2 yamt APPLESTATIC int
3006 1.2.6.2 yamt nfsrv_delegupdate(nfsquad_t clientid, nfsv4stateid_t *stateidp,
3007 1.2.6.2 yamt vnode_t vp, int op, struct ucred *cred, NFSPROC_T *p)
3008 1.2.6.2 yamt {
3009 1.2.6.2 yamt struct nfsstate *stp;
3010 1.2.6.2 yamt struct nfsclient *clp;
3011 1.2.6.2 yamt int error = 0;
3012 1.2.6.2 yamt fhandle_t fh;
3013 1.2.6.2 yamt
3014 1.2.6.2 yamt /*
3015 1.2.6.2 yamt * Do a sanity check against the file handle for DelegReturn.
3016 1.2.6.2 yamt */
3017 1.2.6.2 yamt if (vp) {
3018 1.2.6.2 yamt error = nfsvno_getfh(vp, &fh, p);
3019 1.2.6.2 yamt if (error)
3020 1.2.6.2 yamt goto out;
3021 1.2.6.2 yamt }
3022 1.2.6.2 yamt /*
3023 1.2.6.2 yamt * Check for restart conditions (client and server).
3024 1.2.6.2 yamt */
3025 1.2.6.2 yamt if (op == NFSV4OP_DELEGRETURN)
3026 1.2.6.2 yamt error = nfsrv_checkrestart(clientid, NFSLCK_DELEGRETURN,
3027 1.2.6.2 yamt stateidp, 0);
3028 1.2.6.2 yamt else
3029 1.2.6.2 yamt error = nfsrv_checkrestart(clientid, NFSLCK_DELEGPURGE,
3030 1.2.6.2 yamt stateidp, 0);
3031 1.2.6.2 yamt
3032 1.2.6.2 yamt NFSLOCKSTATE();
3033 1.2.6.2 yamt /*
3034 1.2.6.2 yamt * Get the open structure via clientid and stateid.
3035 1.2.6.2 yamt */
3036 1.2.6.2 yamt if (!error)
3037 1.2.6.2 yamt error = nfsrv_getclient(clientid, CLOPS_RENEW, &clp,
3038 1.2.6.2 yamt (nfsquad_t)((u_quad_t)0), NULL, p);
3039 1.2.6.2 yamt if (error) {
3040 1.2.6.2 yamt if (error == NFSERR_CBPATHDOWN)
3041 1.2.6.2 yamt error = 0;
3042 1.2.6.2 yamt if (error == NFSERR_STALECLIENTID && op == NFSV4OP_DELEGRETURN)
3043 1.2.6.2 yamt error = NFSERR_STALESTATEID;
3044 1.2.6.2 yamt }
3045 1.2.6.2 yamt if (!error && op == NFSV4OP_DELEGRETURN) {
3046 1.2.6.2 yamt error = nfsrv_getstate(clp, stateidp, NFSLCK_DELEGRETURN, &stp);
3047 1.2.6.2 yamt if (!error && stp->ls_stateid.seqid != stateidp->seqid)
3048 1.2.6.2 yamt error = NFSERR_OLDSTATEID;
3049 1.2.6.2 yamt }
3050 1.2.6.2 yamt /*
3051 1.2.6.2 yamt * NFSERR_EXPIRED means that the state has gone away,
3052 1.2.6.2 yamt * so Delegations have been purged. Just return ok.
3053 1.2.6.2 yamt */
3054 1.2.6.2 yamt if (error == NFSERR_EXPIRED && op == NFSV4OP_DELEGPURGE) {
3055 1.2.6.2 yamt NFSUNLOCKSTATE();
3056 1.2.6.2 yamt error = 0;
3057 1.2.6.2 yamt goto out;
3058 1.2.6.2 yamt }
3059 1.2.6.2 yamt if (error) {
3060 1.2.6.2 yamt NFSUNLOCKSTATE();
3061 1.2.6.2 yamt goto out;
3062 1.2.6.2 yamt }
3063 1.2.6.2 yamt
3064 1.2.6.2 yamt if (op == NFSV4OP_DELEGRETURN) {
3065 1.2.6.2 yamt if (NFSBCMP((caddr_t)&fh, (caddr_t)&stp->ls_lfp->lf_fh,
3066 1.2.6.2 yamt sizeof (fhandle_t))) {
3067 1.2.6.2 yamt NFSUNLOCKSTATE();
3068 1.2.6.2 yamt error = NFSERR_BADSTATEID;
3069 1.2.6.2 yamt goto out;
3070 1.2.6.2 yamt }
3071 1.2.6.2 yamt nfsrv_freedeleg(stp);
3072 1.2.6.2 yamt } else {
3073 1.2.6.2 yamt nfsrv_freedeleglist(&clp->lc_olddeleg);
3074 1.2.6.2 yamt }
3075 1.2.6.2 yamt NFSUNLOCKSTATE();
3076 1.2.6.2 yamt error = 0;
3077 1.2.6.2 yamt
3078 1.2.6.2 yamt out:
3079 1.2.6.2 yamt NFSEXITCODE(error);
3080 1.2.6.2 yamt return (error);
3081 1.2.6.2 yamt }
3082 1.2.6.2 yamt
3083 1.2.6.2 yamt /*
3084 1.2.6.2 yamt * Release lock owner.
3085 1.2.6.2 yamt */
3086 1.2.6.2 yamt APPLESTATIC int
3087 1.2.6.2 yamt nfsrv_releaselckown(struct nfsstate *new_stp, nfsquad_t clientid,
3088 1.2.6.2 yamt NFSPROC_T *p)
3089 1.2.6.2 yamt {
3090 1.2.6.2 yamt struct nfsstate *stp, *nstp, *openstp, *ownstp;
3091 1.2.6.2 yamt struct nfsclient *clp;
3092 1.2.6.2 yamt int error = 0;
3093 1.2.6.2 yamt
3094 1.2.6.2 yamt /*
3095 1.2.6.2 yamt * Check for restart conditions (client and server).
3096 1.2.6.2 yamt */
3097 1.2.6.2 yamt error = nfsrv_checkrestart(clientid, new_stp->ls_flags,
3098 1.2.6.2 yamt &new_stp->ls_stateid, 0);
3099 1.2.6.2 yamt if (error)
3100 1.2.6.2 yamt goto out;
3101 1.2.6.2 yamt
3102 1.2.6.2 yamt NFSLOCKSTATE();
3103 1.2.6.2 yamt /*
3104 1.2.6.2 yamt * Get the lock owner by name.
3105 1.2.6.2 yamt */
3106 1.2.6.2 yamt error = nfsrv_getclient(clientid, CLOPS_RENEW, &clp,
3107 1.2.6.2 yamt (nfsquad_t)((u_quad_t)0), NULL, p);
3108 1.2.6.2 yamt if (error) {
3109 1.2.6.2 yamt NFSUNLOCKSTATE();
3110 1.2.6.2 yamt goto out;
3111 1.2.6.2 yamt }
3112 1.2.6.2 yamt LIST_FOREACH(ownstp, &clp->lc_open, ls_list) {
3113 1.2.6.2 yamt LIST_FOREACH(openstp, &ownstp->ls_open, ls_list) {
3114 1.2.6.2 yamt stp = LIST_FIRST(&openstp->ls_open);
3115 1.2.6.2 yamt while (stp != NULL) {
3116 1.2.6.2 yamt nstp = LIST_NEXT(stp, ls_list);
3117 1.2.6.2 yamt /*
3118 1.2.6.2 yamt * If the owner matches, check for locks and
3119 1.2.6.2 yamt * then free or return an error.
3120 1.2.6.2 yamt */
3121 1.2.6.2 yamt if (stp->ls_ownerlen == new_stp->ls_ownerlen &&
3122 1.2.6.2 yamt !NFSBCMP(stp->ls_owner, new_stp->ls_owner,
3123 1.2.6.2 yamt stp->ls_ownerlen)){
3124 1.2.6.2 yamt if (LIST_EMPTY(&stp->ls_lock)) {
3125 1.2.6.2 yamt nfsrv_freelockowner(stp, NULL, 0, p);
3126 1.2.6.2 yamt } else {
3127 1.2.6.2 yamt NFSUNLOCKSTATE();
3128 1.2.6.2 yamt error = NFSERR_LOCKSHELD;
3129 1.2.6.2 yamt goto out;
3130 1.2.6.2 yamt }
3131 1.2.6.2 yamt }
3132 1.2.6.2 yamt stp = nstp;
3133 1.2.6.2 yamt }
3134 1.2.6.2 yamt }
3135 1.2.6.2 yamt }
3136 1.2.6.2 yamt NFSUNLOCKSTATE();
3137 1.2.6.2 yamt
3138 1.2.6.2 yamt out:
3139 1.2.6.2 yamt NFSEXITCODE(error);
3140 1.2.6.2 yamt return (error);
3141 1.2.6.2 yamt }
3142 1.2.6.2 yamt
3143 1.2.6.2 yamt /*
3144 1.2.6.2 yamt * Get the file handle for a lock structure.
3145 1.2.6.2 yamt */
3146 1.2.6.2 yamt static int
3147 1.2.6.2 yamt nfsrv_getlockfh(vnode_t vp, u_short flags,
3148 1.2.6.2 yamt struct nfslockfile **new_lfpp, fhandle_t *nfhp, NFSPROC_T *p)
3149 1.2.6.2 yamt {
3150 1.2.6.2 yamt fhandle_t *fhp = NULL;
3151 1.2.6.2 yamt struct nfslockfile *new_lfp;
3152 1.2.6.2 yamt int error;
3153 1.2.6.2 yamt
3154 1.2.6.2 yamt /*
3155 1.2.6.2 yamt * For lock, use the new nfslock structure, otherwise just
3156 1.2.6.2 yamt * a fhandle_t on the stack.
3157 1.2.6.2 yamt */
3158 1.2.6.2 yamt if (flags & NFSLCK_OPEN) {
3159 1.2.6.2 yamt new_lfp = *new_lfpp;
3160 1.2.6.2 yamt fhp = &new_lfp->lf_fh;
3161 1.2.6.2 yamt } else if (nfhp) {
3162 1.2.6.2 yamt fhp = nfhp;
3163 1.2.6.2 yamt } else {
3164 1.2.6.2 yamt panic("nfsrv_getlockfh");
3165 1.2.6.2 yamt }
3166 1.2.6.2 yamt error = nfsvno_getfh(vp, fhp, p);
3167 1.2.6.2 yamt NFSEXITCODE(error);
3168 1.2.6.2 yamt return (error);
3169 1.2.6.2 yamt }
3170 1.2.6.2 yamt
3171 1.2.6.2 yamt /*
3172 1.2.6.2 yamt * Get an nfs lock structure. Allocate one, as required, and return a
3173 1.2.6.2 yamt * pointer to it.
3174 1.2.6.2 yamt * Returns an NFSERR_xxx upon failure or -1 to indicate no current lock.
3175 1.2.6.2 yamt */
3176 1.2.6.2 yamt static int
3177 1.2.6.2 yamt nfsrv_getlockfile(u_short flags, struct nfslockfile **new_lfpp,
3178 1.2.6.2 yamt struct nfslockfile **lfpp, fhandle_t *nfhp, int lockit)
3179 1.2.6.2 yamt {
3180 1.2.6.2 yamt struct nfslockfile *lfp;
3181 1.2.6.2 yamt fhandle_t *fhp = NULL, *tfhp;
3182 1.2.6.2 yamt struct nfslockhashhead *hp;
3183 1.2.6.2 yamt struct nfslockfile *new_lfp = NULL;
3184 1.2.6.2 yamt
3185 1.2.6.2 yamt /*
3186 1.2.6.2 yamt * For lock, use the new nfslock structure, otherwise just
3187 1.2.6.2 yamt * a fhandle_t on the stack.
3188 1.2.6.2 yamt */
3189 1.2.6.2 yamt if (flags & NFSLCK_OPEN) {
3190 1.2.6.2 yamt new_lfp = *new_lfpp;
3191 1.2.6.2 yamt fhp = &new_lfp->lf_fh;
3192 1.2.6.2 yamt } else if (nfhp) {
3193 1.2.6.2 yamt fhp = nfhp;
3194 1.2.6.2 yamt } else {
3195 1.2.6.2 yamt panic("nfsrv_getlockfile");
3196 1.2.6.2 yamt }
3197 1.2.6.2 yamt
3198 1.2.6.2 yamt hp = NFSLOCKHASH(fhp);
3199 1.2.6.2 yamt LIST_FOREACH(lfp, hp, lf_hash) {
3200 1.2.6.2 yamt tfhp = &lfp->lf_fh;
3201 1.2.6.2 yamt if (NFSVNO_CMPFH(fhp, tfhp)) {
3202 1.2.6.2 yamt if (lockit)
3203 1.2.6.2 yamt nfsrv_locklf(lfp);
3204 1.2.6.2 yamt *lfpp = lfp;
3205 1.2.6.2 yamt return (0);
3206 1.2.6.2 yamt }
3207 1.2.6.2 yamt }
3208 1.2.6.2 yamt if (!(flags & NFSLCK_OPEN))
3209 1.2.6.2 yamt return (-1);
3210 1.2.6.2 yamt
3211 1.2.6.2 yamt /*
3212 1.2.6.2 yamt * No match, so chain the new one into the list.
3213 1.2.6.2 yamt */
3214 1.2.6.2 yamt LIST_INIT(&new_lfp->lf_open);
3215 1.2.6.2 yamt LIST_INIT(&new_lfp->lf_lock);
3216 1.2.6.2 yamt LIST_INIT(&new_lfp->lf_deleg);
3217 1.2.6.2 yamt LIST_INIT(&new_lfp->lf_locallock);
3218 1.2.6.2 yamt LIST_INIT(&new_lfp->lf_rollback);
3219 1.2.6.2 yamt new_lfp->lf_locallock_lck.nfslock_usecnt = 0;
3220 1.2.6.2 yamt new_lfp->lf_locallock_lck.nfslock_lock = 0;
3221 1.2.6.2 yamt new_lfp->lf_usecount = 0;
3222 1.2.6.2 yamt LIST_INSERT_HEAD(hp, new_lfp, lf_hash);
3223 1.2.6.2 yamt *lfpp = new_lfp;
3224 1.2.6.2 yamt *new_lfpp = NULL;
3225 1.2.6.2 yamt return (0);
3226 1.2.6.2 yamt }
3227 1.2.6.2 yamt
3228 1.2.6.2 yamt /*
3229 1.2.6.2 yamt * This function adds a nfslock lock structure to the list for the associated
3230 1.2.6.2 yamt * nfsstate and nfslockfile structures. It will be inserted after the
3231 1.2.6.2 yamt * entry pointed at by insert_lop.
3232 1.2.6.2 yamt */
3233 1.2.6.2 yamt static void
3234 1.2.6.2 yamt nfsrv_insertlock(struct nfslock *new_lop, struct nfslock *insert_lop,
3235 1.2.6.2 yamt struct nfsstate *stp, struct nfslockfile *lfp)
3236 1.2.6.2 yamt {
3237 1.2.6.2 yamt struct nfslock *lop, *nlop;
3238 1.2.6.2 yamt
3239 1.2.6.2 yamt new_lop->lo_stp = stp;
3240 1.2.6.2 yamt new_lop->lo_lfp = lfp;
3241 1.2.6.2 yamt
3242 1.2.6.2 yamt if (stp != NULL) {
3243 1.2.6.2 yamt /* Insert in increasing lo_first order */
3244 1.2.6.2 yamt lop = LIST_FIRST(&lfp->lf_lock);
3245 1.2.6.2 yamt if (lop == NULL ||
3246 1.2.6.2 yamt new_lop->lo_first <= lop->lo_first) {
3247 1.2.6.2 yamt LIST_INSERT_HEAD(&lfp->lf_lock, new_lop, lo_lckfile);
3248 1.2.6.2 yamt } else {
3249 1.2.6.2 yamt nlop = LIST_NEXT(lop, lo_lckfile);
3250 1.2.6.2 yamt while (nlop != NULL &&
3251 1.2.6.2 yamt nlop->lo_first < new_lop->lo_first) {
3252 1.2.6.2 yamt lop = nlop;
3253 1.2.6.2 yamt nlop = LIST_NEXT(lop, lo_lckfile);
3254 1.2.6.2 yamt }
3255 1.2.6.2 yamt LIST_INSERT_AFTER(lop, new_lop, lo_lckfile);
3256 1.2.6.2 yamt }
3257 1.2.6.2 yamt } else {
3258 1.2.6.2 yamt new_lop->lo_lckfile.le_prev = NULL; /* list not used */
3259 1.2.6.2 yamt }
3260 1.2.6.2 yamt
3261 1.2.6.2 yamt /*
3262 1.2.6.2 yamt * Insert after insert_lop, which is overloaded as stp or lfp for
3263 1.2.6.2 yamt * an empty list.
3264 1.2.6.2 yamt */
3265 1.2.6.2 yamt if (stp == NULL && (struct nfslockfile *)insert_lop == lfp)
3266 1.2.6.2 yamt LIST_INSERT_HEAD(&lfp->lf_locallock, new_lop, lo_lckowner);
3267 1.2.6.2 yamt else if ((struct nfsstate *)insert_lop == stp)
3268 1.2.6.2 yamt LIST_INSERT_HEAD(&stp->ls_lock, new_lop, lo_lckowner);
3269 1.2.6.2 yamt else
3270 1.2.6.2 yamt LIST_INSERT_AFTER(insert_lop, new_lop, lo_lckowner);
3271 1.2.6.2 yamt if (stp != NULL) {
3272 1.2.6.2 yamt newnfsstats.srvlocks++;
3273 1.2.6.2 yamt nfsrv_openpluslock++;
3274 1.2.6.2 yamt }
3275 1.2.6.2 yamt }
3276 1.2.6.2 yamt
3277 1.2.6.2 yamt /*
3278 1.2.6.2 yamt * This function updates the locking for a lock owner and given file. It
3279 1.2.6.2 yamt * maintains a list of lock ranges ordered on increasing file offset that
3280 1.2.6.2 yamt * are NFSLCK_READ or NFSLCK_WRITE and non-overlapping (aka POSIX style).
3281 1.2.6.2 yamt * It always adds new_lop to the list and sometimes uses the one pointed
3282 1.2.6.2 yamt * at by other_lopp.
3283 1.2.6.2 yamt */
3284 1.2.6.2 yamt static void
3285 1.2.6.2 yamt nfsrv_updatelock(struct nfsstate *stp, struct nfslock **new_lopp,
3286 1.2.6.2 yamt struct nfslock **other_lopp, struct nfslockfile *lfp)
3287 1.2.6.2 yamt {
3288 1.2.6.2 yamt struct nfslock *new_lop = *new_lopp;
3289 1.2.6.2 yamt struct nfslock *lop, *tlop, *ilop;
3290 1.2.6.2 yamt struct nfslock *other_lop = *other_lopp;
3291 1.2.6.2 yamt int unlock = 0, myfile = 0;
3292 1.2.6.2 yamt u_int64_t tmp;
3293 1.2.6.2 yamt
3294 1.2.6.2 yamt /*
3295 1.2.6.2 yamt * Work down the list until the lock is merged.
3296 1.2.6.2 yamt */
3297 1.2.6.2 yamt if (new_lop->lo_flags & NFSLCK_UNLOCK)
3298 1.2.6.2 yamt unlock = 1;
3299 1.2.6.2 yamt if (stp != NULL) {
3300 1.2.6.2 yamt ilop = (struct nfslock *)stp;
3301 1.2.6.2 yamt lop = LIST_FIRST(&stp->ls_lock);
3302 1.2.6.2 yamt } else {
3303 1.2.6.2 yamt ilop = (struct nfslock *)lfp;
3304 1.2.6.2 yamt lop = LIST_FIRST(&lfp->lf_locallock);
3305 1.2.6.2 yamt }
3306 1.2.6.2 yamt while (lop != NULL) {
3307 1.2.6.2 yamt /*
3308 1.2.6.2 yamt * Only check locks for this file that aren't before the start of
3309 1.2.6.2 yamt * new lock's range.
3310 1.2.6.2 yamt */
3311 1.2.6.2 yamt if (lop->lo_lfp == lfp) {
3312 1.2.6.2 yamt myfile = 1;
3313 1.2.6.2 yamt if (lop->lo_end >= new_lop->lo_first) {
3314 1.2.6.2 yamt if (new_lop->lo_end < lop->lo_first) {
3315 1.2.6.2 yamt /*
3316 1.2.6.2 yamt * If the new lock ends before the start of the
3317 1.2.6.2 yamt * current lock's range, no merge, just insert
3318 1.2.6.2 yamt * the new lock.
3319 1.2.6.2 yamt */
3320 1.2.6.2 yamt break;
3321 1.2.6.2 yamt }
3322 1.2.6.2 yamt if (new_lop->lo_flags == lop->lo_flags ||
3323 1.2.6.2 yamt (new_lop->lo_first <= lop->lo_first &&
3324 1.2.6.2 yamt new_lop->lo_end >= lop->lo_end)) {
3325 1.2.6.2 yamt /*
3326 1.2.6.2 yamt * This lock can be absorbed by the new lock/unlock.
3327 1.2.6.2 yamt * This happens when it covers the entire range
3328 1.2.6.2 yamt * of the old lock or is contiguous
3329 1.2.6.2 yamt * with the old lock and is of the same type or an
3330 1.2.6.2 yamt * unlock.
3331 1.2.6.2 yamt */
3332 1.2.6.2 yamt if (lop->lo_first < new_lop->lo_first)
3333 1.2.6.2 yamt new_lop->lo_first = lop->lo_first;
3334 1.2.6.2 yamt if (lop->lo_end > new_lop->lo_end)
3335 1.2.6.2 yamt new_lop->lo_end = lop->lo_end;
3336 1.2.6.2 yamt tlop = lop;
3337 1.2.6.2 yamt lop = LIST_NEXT(lop, lo_lckowner);
3338 1.2.6.2 yamt nfsrv_freenfslock(tlop);
3339 1.2.6.2 yamt continue;
3340 1.2.6.2 yamt }
3341 1.2.6.2 yamt
3342 1.2.6.2 yamt /*
3343 1.2.6.2 yamt * All these cases are for contiguous locks that are not the
3344 1.2.6.2 yamt * same type, so they can't be merged.
3345 1.2.6.2 yamt */
3346 1.2.6.2 yamt if (new_lop->lo_first <= lop->lo_first) {
3347 1.2.6.2 yamt /*
3348 1.2.6.2 yamt * This case is where the new lock overlaps with the
3349 1.2.6.2 yamt * first part of the old lock. Move the start of the
3350 1.2.6.2 yamt * old lock to just past the end of the new lock. The
3351 1.2.6.2 yamt * new lock will be inserted in front of the old, since
3352 1.2.6.2 yamt * ilop hasn't been updated. (We are done now.)
3353 1.2.6.2 yamt */
3354 1.2.6.2 yamt lop->lo_first = new_lop->lo_end;
3355 1.2.6.2 yamt break;
3356 1.2.6.2 yamt }
3357 1.2.6.2 yamt if (new_lop->lo_end >= lop->lo_end) {
3358 1.2.6.2 yamt /*
3359 1.2.6.2 yamt * This case is where the new lock overlaps with the
3360 1.2.6.2 yamt * end of the old lock's range. Move the old lock's
3361 1.2.6.2 yamt * end to just before the new lock's first and insert
3362 1.2.6.2 yamt * the new lock after the old lock.
3363 1.2.6.2 yamt * Might not be done yet, since the new lock could
3364 1.2.6.2 yamt * overlap further locks with higher ranges.
3365 1.2.6.2 yamt */
3366 1.2.6.2 yamt lop->lo_end = new_lop->lo_first;
3367 1.2.6.2 yamt ilop = lop;
3368 1.2.6.2 yamt lop = LIST_NEXT(lop, lo_lckowner);
3369 1.2.6.2 yamt continue;
3370 1.2.6.2 yamt }
3371 1.2.6.2 yamt /*
3372 1.2.6.2 yamt * The final case is where the new lock's range is in the
3373 1.2.6.2 yamt * middle of the current lock's and splits the current lock
3374 1.2.6.2 yamt * up. Use *other_lopp to handle the second part of the
3375 1.2.6.2 yamt * split old lock range. (We are done now.)
3376 1.2.6.2 yamt * For unlock, we use new_lop as other_lop and tmp, since
3377 1.2.6.2 yamt * other_lop and new_lop are the same for this case.
3378 1.2.6.2 yamt * We noted the unlock case above, so we don't need
3379 1.2.6.2 yamt * new_lop->lo_flags any longer.
3380 1.2.6.2 yamt */
3381 1.2.6.2 yamt tmp = new_lop->lo_first;
3382 1.2.6.2 yamt if (other_lop == NULL) {
3383 1.2.6.2 yamt if (!unlock)
3384 1.2.6.2 yamt panic("nfsd srv update unlock");
3385 1.2.6.2 yamt other_lop = new_lop;
3386 1.2.6.2 yamt *new_lopp = NULL;
3387 1.2.6.2 yamt }
3388 1.2.6.2 yamt other_lop->lo_first = new_lop->lo_end;
3389 1.2.6.2 yamt other_lop->lo_end = lop->lo_end;
3390 1.2.6.2 yamt other_lop->lo_flags = lop->lo_flags;
3391 1.2.6.2 yamt other_lop->lo_stp = stp;
3392 1.2.6.2 yamt other_lop->lo_lfp = lfp;
3393 1.2.6.2 yamt lop->lo_end = tmp;
3394 1.2.6.2 yamt nfsrv_insertlock(other_lop, lop, stp, lfp);
3395 1.2.6.2 yamt *other_lopp = NULL;
3396 1.2.6.2 yamt ilop = lop;
3397 1.2.6.2 yamt break;
3398 1.2.6.2 yamt }
3399 1.2.6.2 yamt }
3400 1.2.6.2 yamt ilop = lop;
3401 1.2.6.2 yamt lop = LIST_NEXT(lop, lo_lckowner);
3402 1.2.6.2 yamt if (myfile && (lop == NULL || lop->lo_lfp != lfp))
3403 1.2.6.2 yamt break;
3404 1.2.6.2 yamt }
3405 1.2.6.2 yamt
3406 1.2.6.2 yamt /*
3407 1.2.6.2 yamt * Insert the new lock in the list at the appropriate place.
3408 1.2.6.2 yamt */
3409 1.2.6.2 yamt if (!unlock) {
3410 1.2.6.2 yamt nfsrv_insertlock(new_lop, ilop, stp, lfp);
3411 1.2.6.2 yamt *new_lopp = NULL;
3412 1.2.6.2 yamt }
3413 1.2.6.2 yamt }
3414 1.2.6.2 yamt
3415 1.2.6.2 yamt /*
3416 1.2.6.2 yamt * This function handles sequencing of locks, etc.
3417 1.2.6.2 yamt * It returns an error that indicates what the caller should do.
3418 1.2.6.2 yamt */
3419 1.2.6.2 yamt static int
3420 1.2.6.2 yamt nfsrv_checkseqid(struct nfsrv_descript *nd, u_int32_t seqid,
3421 1.2.6.2 yamt struct nfsstate *stp, struct nfsrvcache *op)
3422 1.2.6.2 yamt {
3423 1.2.6.2 yamt int error = 0;
3424 1.2.6.2 yamt
3425 1.2.6.2 yamt if (op != nd->nd_rp)
3426 1.2.6.2 yamt panic("nfsrvstate checkseqid");
3427 1.2.6.2 yamt if (!(op->rc_flag & RC_INPROG))
3428 1.2.6.2 yamt panic("nfsrvstate not inprog");
3429 1.2.6.2 yamt if (stp->ls_op && stp->ls_op->rc_refcnt <= 0) {
3430 1.2.6.2 yamt printf("refcnt=%d\n", stp->ls_op->rc_refcnt);
3431 1.2.6.2 yamt panic("nfsrvstate op refcnt");
3432 1.2.6.2 yamt }
3433 1.2.6.2 yamt if ((stp->ls_seq + 1) == seqid) {
3434 1.2.6.2 yamt if (stp->ls_op)
3435 1.2.6.2 yamt nfsrvd_derefcache(stp->ls_op);
3436 1.2.6.2 yamt stp->ls_op = op;
3437 1.2.6.2 yamt nfsrvd_refcache(op);
3438 1.2.6.2 yamt stp->ls_seq = seqid;
3439 1.2.6.2 yamt goto out;
3440 1.2.6.2 yamt } else if (stp->ls_seq == seqid && stp->ls_op &&
3441 1.2.6.2 yamt op->rc_xid == stp->ls_op->rc_xid &&
3442 1.2.6.2 yamt op->rc_refcnt == 0 &&
3443 1.2.6.2 yamt op->rc_reqlen == stp->ls_op->rc_reqlen &&
3444 1.2.6.2 yamt op->rc_cksum == stp->ls_op->rc_cksum) {
3445 1.2.6.2 yamt if (stp->ls_op->rc_flag & RC_INPROG) {
3446 1.2.6.2 yamt error = NFSERR_DONTREPLY;
3447 1.2.6.2 yamt goto out;
3448 1.2.6.2 yamt }
3449 1.2.6.2 yamt nd->nd_rp = stp->ls_op;
3450 1.2.6.2 yamt nd->nd_rp->rc_flag |= RC_INPROG;
3451 1.2.6.2 yamt nfsrvd_delcache(op);
3452 1.2.6.2 yamt error = NFSERR_REPLYFROMCACHE;
3453 1.2.6.2 yamt goto out;
3454 1.2.6.2 yamt }
3455 1.2.6.2 yamt error = NFSERR_BADSEQID;
3456 1.2.6.2 yamt
3457 1.2.6.2 yamt out:
3458 1.2.6.2 yamt NFSEXITCODE2(error, nd);
3459 1.2.6.2 yamt return (error);
3460 1.2.6.2 yamt }
3461 1.2.6.2 yamt
3462 1.2.6.2 yamt /*
3463 1.2.6.2 yamt * Get the client ip address for callbacks. If the strings can't be parsed,
3464 1.2.6.2 yamt * just set lc_program to 0 to indicate no callbacks are possible.
3465 1.2.6.2 yamt * (For cases where the address can't be parsed or is 0.0.0.0.0.0, set
3466 1.2.6.2 yamt * the address to the client's transport address. This won't be used
3467 1.2.6.2 yamt * for callbacks, but can be printed out by newnfsstats for info.)
3468 1.2.6.2 yamt * Return error if the xdr can't be parsed, 0 otherwise.
3469 1.2.6.2 yamt */
3470 1.2.6.2 yamt APPLESTATIC int
3471 1.2.6.2 yamt nfsrv_getclientipaddr(struct nfsrv_descript *nd, struct nfsclient *clp)
3472 1.2.6.2 yamt {
3473 1.2.6.2 yamt u_int32_t *tl;
3474 1.2.6.2 yamt u_char *cp, *cp2;
3475 1.2.6.2 yamt int i, j;
3476 1.2.6.2 yamt struct sockaddr_in *rad, *sad;
3477 1.2.6.2 yamt u_char protocol[5], addr[24];
3478 1.2.6.2 yamt int error = 0, cantparse = 0;
3479 1.2.6.2 yamt union {
3480 1.2.6.2 yamt u_long ival;
3481 1.2.6.2 yamt u_char cval[4];
3482 1.2.6.2 yamt } ip;
3483 1.2.6.2 yamt union {
3484 1.2.6.2 yamt u_short sval;
3485 1.2.6.2 yamt u_char cval[2];
3486 1.2.6.2 yamt } port;
3487 1.2.6.2 yamt
3488 1.2.6.2 yamt rad = NFSSOCKADDR(clp->lc_req.nr_nam, struct sockaddr_in *);
3489 1.2.6.2 yamt rad->sin_family = AF_INET;
3490 1.2.6.2 yamt rad->sin_len = sizeof (struct sockaddr_in);
3491 1.2.6.2 yamt rad->sin_addr.s_addr = 0;
3492 1.2.6.2 yamt rad->sin_port = 0;
3493 1.2.6.2 yamt clp->lc_req.nr_client = NULL;
3494 1.2.6.2 yamt clp->lc_req.nr_lock = 0;
3495 1.2.6.2 yamt NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
3496 1.2.6.2 yamt i = fxdr_unsigned(int, *tl);
3497 1.2.6.2 yamt if (i >= 3 && i <= 4) {
3498 1.2.6.2 yamt error = nfsrv_mtostr(nd, protocol, i);
3499 1.2.6.2 yamt if (error)
3500 1.2.6.2 yamt goto nfsmout;
3501 1.2.6.2 yamt if (!strcmp(protocol, "tcp")) {
3502 1.2.6.2 yamt clp->lc_flags |= LCL_TCPCALLBACK;
3503 1.2.6.2 yamt clp->lc_req.nr_sotype = SOCK_STREAM;
3504 1.2.6.2 yamt clp->lc_req.nr_soproto = IPPROTO_TCP;
3505 1.2.6.2 yamt } else if (!strcmp(protocol, "udp")) {
3506 1.2.6.2 yamt clp->lc_req.nr_sotype = SOCK_DGRAM;
3507 1.2.6.2 yamt clp->lc_req.nr_soproto = IPPROTO_UDP;
3508 1.2.6.2 yamt } else {
3509 1.2.6.2 yamt cantparse = 1;
3510 1.2.6.2 yamt }
3511 1.2.6.2 yamt } else {
3512 1.2.6.2 yamt cantparse = 1;
3513 1.2.6.2 yamt if (i > 0) {
3514 1.2.6.2 yamt error = nfsm_advance(nd, NFSM_RNDUP(i), -1);
3515 1.2.6.2 yamt if (error)
3516 1.2.6.2 yamt goto nfsmout;
3517 1.2.6.2 yamt }
3518 1.2.6.2 yamt }
3519 1.2.6.2 yamt NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
3520 1.2.6.2 yamt i = fxdr_unsigned(int, *tl);
3521 1.2.6.2 yamt if (i < 0) {
3522 1.2.6.2 yamt error = NFSERR_BADXDR;
3523 1.2.6.2 yamt goto nfsmout;
3524 1.2.6.2 yamt } else if (i == 0) {
3525 1.2.6.2 yamt cantparse = 1;
3526 1.2.6.2 yamt } else if (!cantparse && i <= 23 && i >= 11) {
3527 1.2.6.2 yamt error = nfsrv_mtostr(nd, addr, i);
3528 1.2.6.2 yamt if (error)
3529 1.2.6.2 yamt goto nfsmout;
3530 1.2.6.2 yamt
3531 1.2.6.2 yamt /*
3532 1.2.6.2 yamt * Parse out the address fields. We expect 6 decimal numbers
3533 1.2.6.2 yamt * separated by '.'s.
3534 1.2.6.2 yamt */
3535 1.2.6.2 yamt cp = addr;
3536 1.2.6.2 yamt i = 0;
3537 1.2.6.2 yamt while (*cp && i < 6) {
3538 1.2.6.2 yamt cp2 = cp;
3539 1.2.6.2 yamt while (*cp2 && *cp2 != '.')
3540 1.2.6.2 yamt cp2++;
3541 1.2.6.2 yamt if (*cp2)
3542 1.2.6.2 yamt *cp2++ = '\0';
3543 1.2.6.2 yamt else if (i != 5) {
3544 1.2.6.2 yamt cantparse = 1;
3545 1.2.6.2 yamt break;
3546 1.2.6.2 yamt }
3547 1.2.6.2 yamt j = nfsrv_getipnumber(cp);
3548 1.2.6.2 yamt if (j >= 0) {
3549 1.2.6.2 yamt if (i < 4)
3550 1.2.6.2 yamt ip.cval[3 - i] = j;
3551 1.2.6.2 yamt else
3552 1.2.6.2 yamt port.cval[5 - i] = j;
3553 1.2.6.2 yamt } else {
3554 1.2.6.2 yamt cantparse = 1;
3555 1.2.6.2 yamt break;
3556 1.2.6.2 yamt }
3557 1.2.6.2 yamt cp = cp2;
3558 1.2.6.2 yamt i++;
3559 1.2.6.2 yamt }
3560 1.2.6.2 yamt if (!cantparse) {
3561 1.2.6.2 yamt if (ip.ival != 0x0) {
3562 1.2.6.2 yamt rad->sin_addr.s_addr = htonl(ip.ival);
3563 1.2.6.2 yamt rad->sin_port = htons(port.sval);
3564 1.2.6.2 yamt } else {
3565 1.2.6.2 yamt cantparse = 1;
3566 1.2.6.2 yamt }
3567 1.2.6.2 yamt }
3568 1.2.6.2 yamt } else {
3569 1.2.6.2 yamt cantparse = 1;
3570 1.2.6.2 yamt if (i > 0) {
3571 1.2.6.2 yamt error = nfsm_advance(nd, NFSM_RNDUP(i), -1);
3572 1.2.6.2 yamt if (error)
3573 1.2.6.2 yamt goto nfsmout;
3574 1.2.6.2 yamt }
3575 1.2.6.2 yamt }
3576 1.2.6.2 yamt if (cantparse) {
3577 1.2.6.2 yamt sad = NFSSOCKADDR(nd->nd_nam, struct sockaddr_in *);
3578 1.2.6.2 yamt rad->sin_addr.s_addr = sad->sin_addr.s_addr;
3579 1.2.6.2 yamt rad->sin_port = 0x0;
3580 1.2.6.2 yamt clp->lc_program = 0;
3581 1.2.6.2 yamt }
3582 1.2.6.2 yamt nfsmout:
3583 1.2.6.2 yamt NFSEXITCODE2(error, nd);
3584 1.2.6.2 yamt return (error);
3585 1.2.6.2 yamt }
3586 1.2.6.2 yamt
3587 1.2.6.2 yamt /*
3588 1.2.6.2 yamt * Turn a string of up to three decimal digits into a number. Return -1 upon
3589 1.2.6.2 yamt * error.
3590 1.2.6.2 yamt */
3591 1.2.6.2 yamt static int
3592 1.2.6.2 yamt nfsrv_getipnumber(u_char *cp)
3593 1.2.6.2 yamt {
3594 1.2.6.2 yamt int i = 0, j = 0;
3595 1.2.6.2 yamt
3596 1.2.6.2 yamt while (*cp) {
3597 1.2.6.2 yamt if (j > 2 || *cp < '0' || *cp > '9')
3598 1.2.6.2 yamt return (-1);
3599 1.2.6.2 yamt i *= 10;
3600 1.2.6.2 yamt i += (*cp - '0');
3601 1.2.6.2 yamt cp++;
3602 1.2.6.2 yamt j++;
3603 1.2.6.2 yamt }
3604 1.2.6.2 yamt if (i < 256)
3605 1.2.6.2 yamt return (i);
3606 1.2.6.2 yamt return (-1);
3607 1.2.6.2 yamt }
3608 1.2.6.2 yamt
3609 1.2.6.2 yamt /*
3610 1.2.6.2 yamt * This function checks for restart conditions.
3611 1.2.6.2 yamt */
3612 1.2.6.2 yamt static int
3613 1.2.6.2 yamt nfsrv_checkrestart(nfsquad_t clientid, u_int32_t flags,
3614 1.2.6.2 yamt nfsv4stateid_t *stateidp, int specialid)
3615 1.2.6.2 yamt {
3616 1.2.6.2 yamt int ret = 0;
3617 1.2.6.2 yamt
3618 1.2.6.2 yamt /*
3619 1.2.6.2 yamt * First check for a server restart. Open, LockT, ReleaseLockOwner
3620 1.2.6.2 yamt * and DelegPurge have a clientid, the rest a stateid.
3621 1.2.6.2 yamt */
3622 1.2.6.2 yamt if (flags &
3623 1.2.6.2 yamt (NFSLCK_OPEN | NFSLCK_TEST | NFSLCK_RELEASE | NFSLCK_DELEGPURGE)) {
3624 1.2.6.2 yamt if (clientid.lval[0] != nfsrvboottime) {
3625 1.2.6.2 yamt ret = NFSERR_STALECLIENTID;
3626 1.2.6.2 yamt goto out;
3627 1.2.6.2 yamt }
3628 1.2.6.2 yamt } else if (stateidp->other[0] != nfsrvboottime &&
3629 1.2.6.2 yamt specialid == 0) {
3630 1.2.6.2 yamt ret = NFSERR_STALESTATEID;
3631 1.2.6.2 yamt goto out;
3632 1.2.6.2 yamt }
3633 1.2.6.2 yamt
3634 1.2.6.2 yamt /*
3635 1.2.6.2 yamt * Read, Write, Setattr and LockT can return NFSERR_GRACE and do
3636 1.2.6.2 yamt * not use a lock/open owner seqid#, so the check can be done now.
3637 1.2.6.2 yamt * (The others will be checked, as required, later.)
3638 1.2.6.2 yamt */
3639 1.2.6.2 yamt if (!(flags & (NFSLCK_CHECK | NFSLCK_TEST)))
3640 1.2.6.2 yamt goto out;
3641 1.2.6.2 yamt
3642 1.2.6.2 yamt NFSLOCKSTATE();
3643 1.2.6.2 yamt ret = nfsrv_checkgrace(flags);
3644 1.2.6.2 yamt NFSUNLOCKSTATE();
3645 1.2.6.2 yamt
3646 1.2.6.2 yamt out:
3647 1.2.6.2 yamt NFSEXITCODE(ret);
3648 1.2.6.2 yamt return (ret);
3649 1.2.6.2 yamt }
3650 1.2.6.2 yamt
3651 1.2.6.2 yamt /*
3652 1.2.6.2 yamt * Check for grace.
3653 1.2.6.2 yamt */
3654 1.2.6.2 yamt static int
3655 1.2.6.2 yamt nfsrv_checkgrace(u_int32_t flags)
3656 1.2.6.2 yamt {
3657 1.2.6.2 yamt int error = 0;
3658 1.2.6.2 yamt
3659 1.2.6.2 yamt if (nfsrv_stablefirst.nsf_flags & NFSNSF_GRACEOVER) {
3660 1.2.6.2 yamt if (flags & NFSLCK_RECLAIM) {
3661 1.2.6.2 yamt error = NFSERR_NOGRACE;
3662 1.2.6.2 yamt goto out;
3663 1.2.6.2 yamt }
3664 1.2.6.2 yamt } else {
3665 1.2.6.2 yamt if (!(flags & NFSLCK_RECLAIM)) {
3666 1.2.6.2 yamt error = NFSERR_GRACE;
3667 1.2.6.2 yamt goto out;
3668 1.2.6.2 yamt }
3669 1.2.6.2 yamt
3670 1.2.6.2 yamt /*
3671 1.2.6.2 yamt * If grace is almost over and we are still getting Reclaims,
3672 1.2.6.2 yamt * extend grace a bit.
3673 1.2.6.2 yamt */
3674 1.2.6.2 yamt if ((NFSD_MONOSEC + NFSRV_LEASEDELTA) >
3675 1.2.6.2 yamt nfsrv_stablefirst.nsf_eograce)
3676 1.2.6.2 yamt nfsrv_stablefirst.nsf_eograce = NFSD_MONOSEC +
3677 1.2.6.2 yamt NFSRV_LEASEDELTA;
3678 1.2.6.2 yamt }
3679 1.2.6.2 yamt
3680 1.2.6.2 yamt out:
3681 1.2.6.2 yamt NFSEXITCODE(error);
3682 1.2.6.2 yamt return (error);
3683 1.2.6.2 yamt }
3684 1.2.6.2 yamt
3685 1.2.6.2 yamt /*
3686 1.2.6.2 yamt * Do a server callback.
3687 1.2.6.2 yamt */
3688 1.2.6.2 yamt static int
3689 1.2.6.2 yamt nfsrv_docallback(struct nfsclient *clp, int procnum,
3690 1.2.6.2 yamt nfsv4stateid_t *stateidp, int trunc, fhandle_t *fhp,
3691 1.2.6.2 yamt struct nfsvattr *nap, nfsattrbit_t *attrbitp, NFSPROC_T *p)
3692 1.2.6.2 yamt {
3693 1.2.6.2 yamt mbuf_t m;
3694 1.2.6.2 yamt u_int32_t *tl;
3695 1.2.6.2 yamt struct nfsrv_descript nfsd, *nd = &nfsd;
3696 1.2.6.2 yamt struct ucred *cred;
3697 1.2.6.2 yamt int error = 0;
3698 1.2.6.2 yamt u_int32_t callback;
3699 1.2.6.2 yamt
3700 1.2.6.2 yamt cred = newnfs_getcred();
3701 1.2.6.2 yamt NFSLOCKSTATE(); /* mostly for lc_cbref++ */
3702 1.2.6.2 yamt if (clp->lc_flags & LCL_NEEDSCONFIRM) {
3703 1.2.6.2 yamt NFSUNLOCKSTATE();
3704 1.2.6.2 yamt panic("docallb");
3705 1.2.6.2 yamt }
3706 1.2.6.2 yamt clp->lc_cbref++;
3707 1.2.6.2 yamt
3708 1.2.6.2 yamt /*
3709 1.2.6.2 yamt * Fill the callback program# and version into the request
3710 1.2.6.2 yamt * structure for newnfs_connect() to use.
3711 1.2.6.2 yamt */
3712 1.2.6.2 yamt clp->lc_req.nr_prog = clp->lc_program;
3713 1.2.6.2 yamt clp->lc_req.nr_vers = NFSV4_CBVERS;
3714 1.2.6.2 yamt
3715 1.2.6.2 yamt /*
3716 1.2.6.2 yamt * First, fill in some of the fields of nd and cr.
3717 1.2.6.2 yamt */
3718 1.2.6.2 yamt nd->nd_flag = ND_NFSV4;
3719 1.2.6.2 yamt if (clp->lc_flags & LCL_GSS)
3720 1.2.6.2 yamt nd->nd_flag |= ND_KERBV;
3721 1.2.6.2 yamt nd->nd_repstat = 0;
3722 1.2.6.2 yamt cred->cr_uid = clp->lc_uid;
3723 1.2.6.2 yamt cred->cr_gid = clp->lc_gid;
3724 1.2.6.2 yamt callback = clp->lc_callback;
3725 1.2.6.2 yamt NFSUNLOCKSTATE();
3726 1.2.6.2 yamt cred->cr_ngroups = 1;
3727 1.2.6.2 yamt
3728 1.2.6.2 yamt /*
3729 1.2.6.2 yamt * Get the first mbuf for the request.
3730 1.2.6.2 yamt */
3731 1.2.6.2 yamt MGET(m, M_WAITOK, MT_DATA);
3732 1.2.6.2 yamt mbuf_setlen(m, 0);
3733 1.2.6.2 yamt nd->nd_mreq = nd->nd_mb = m;
3734 1.2.6.2 yamt nd->nd_bpos = NFSMTOD(m, caddr_t);
3735 1.2.6.2 yamt
3736 1.2.6.2 yamt /*
3737 1.2.6.2 yamt * and build the callback request.
3738 1.2.6.2 yamt */
3739 1.2.6.2 yamt if (procnum == NFSV4OP_CBGETATTR) {
3740 1.2.6.2 yamt nd->nd_procnum = NFSV4PROC_CBCOMPOUND;
3741 1.2.6.2 yamt (void) nfsm_strtom(nd, "CB Getattr", 10);
3742 1.2.6.2 yamt NFSM_BUILD(tl, u_int32_t *, 4 * NFSX_UNSIGNED);
3743 1.2.6.2 yamt *tl++ = txdr_unsigned(NFSV4_MINORVERSION);
3744 1.2.6.2 yamt *tl++ = txdr_unsigned(callback);
3745 1.2.6.2 yamt *tl++ = txdr_unsigned(1);
3746 1.2.6.2 yamt *tl = txdr_unsigned(NFSV4OP_CBGETATTR);
3747 1.2.6.2 yamt (void) nfsm_fhtom(nd, (u_int8_t *)fhp, NFSX_MYFH, 0);
3748 1.2.6.2 yamt (void) nfsrv_putattrbit(nd, attrbitp);
3749 1.2.6.2 yamt } else if (procnum == NFSV4OP_CBRECALL) {
3750 1.2.6.2 yamt nd->nd_procnum = NFSV4PROC_CBCOMPOUND;
3751 1.2.6.2 yamt (void) nfsm_strtom(nd, "CB Recall", 9);
3752 1.2.6.2 yamt NFSM_BUILD(tl, u_int32_t *, 5 * NFSX_UNSIGNED + NFSX_STATEID);
3753 1.2.6.2 yamt *tl++ = txdr_unsigned(NFSV4_MINORVERSION);
3754 1.2.6.2 yamt *tl++ = txdr_unsigned(callback);
3755 1.2.6.2 yamt *tl++ = txdr_unsigned(1);
3756 1.2.6.2 yamt *tl++ = txdr_unsigned(NFSV4OP_CBRECALL);
3757 1.2.6.2 yamt *tl++ = txdr_unsigned(stateidp->seqid);
3758 1.2.6.2 yamt NFSBCOPY((caddr_t)stateidp->other, (caddr_t)tl,
3759 1.2.6.2 yamt NFSX_STATEIDOTHER);
3760 1.2.6.2 yamt tl += (NFSX_STATEIDOTHER / NFSX_UNSIGNED);
3761 1.2.6.2 yamt if (trunc)
3762 1.2.6.2 yamt *tl = newnfs_true;
3763 1.2.6.2 yamt else
3764 1.2.6.2 yamt *tl = newnfs_false;
3765 1.2.6.2 yamt (void) nfsm_fhtom(nd, (u_int8_t *)fhp, NFSX_MYFH, 0);
3766 1.2.6.2 yamt } else {
3767 1.2.6.2 yamt nd->nd_procnum = NFSV4PROC_CBNULL;
3768 1.2.6.2 yamt }
3769 1.2.6.2 yamt
3770 1.2.6.2 yamt /*
3771 1.2.6.2 yamt * Call newnfs_connect(), as required, and then newnfs_request().
3772 1.2.6.2 yamt */
3773 1.2.6.2 yamt (void) newnfs_sndlock(&clp->lc_req.nr_lock);
3774 1.2.6.2 yamt if (clp->lc_req.nr_client == NULL) {
3775 1.2.6.2 yamt if (nd->nd_procnum == NFSV4PROC_CBNULL)
3776 1.2.6.2 yamt error = newnfs_connect(NULL, &clp->lc_req, cred,
3777 1.2.6.2 yamt NULL, 1);
3778 1.2.6.2 yamt else
3779 1.2.6.2 yamt error = newnfs_connect(NULL, &clp->lc_req, cred,
3780 1.2.6.2 yamt NULL, 3);
3781 1.2.6.2 yamt }
3782 1.2.6.2 yamt newnfs_sndunlock(&clp->lc_req.nr_lock);
3783 1.2.6.2 yamt if (!error) {
3784 1.2.6.2 yamt error = newnfs_request(nd, NULL, clp, &clp->lc_req, NULL,
3785 1.2.6.2 yamt NULL, cred, clp->lc_program, NFSV4_CBVERS, NULL, 1, NULL,
3786 1.2.6.2 yamt NULL);
3787 1.2.6.2 yamt }
3788 1.2.6.2 yamt NFSFREECRED(cred);
3789 1.2.6.2 yamt
3790 1.2.6.2 yamt /*
3791 1.2.6.2 yamt * If error is set here, the Callback path isn't working
3792 1.2.6.2 yamt * properly, so twiddle the appropriate LCL_ flags.
3793 1.2.6.2 yamt * (nd_repstat != 0 indicates the Callback path is working,
3794 1.2.6.2 yamt * but the callback failed on the client.)
3795 1.2.6.2 yamt */
3796 1.2.6.2 yamt if (error) {
3797 1.2.6.2 yamt /*
3798 1.2.6.2 yamt * Mark the callback pathway down, which disabled issuing
3799 1.2.6.2 yamt * of delegations and gets Renew to return NFSERR_CBPATHDOWN.
3800 1.2.6.2 yamt */
3801 1.2.6.2 yamt NFSLOCKSTATE();
3802 1.2.6.2 yamt clp->lc_flags |= LCL_CBDOWN;
3803 1.2.6.2 yamt NFSUNLOCKSTATE();
3804 1.2.6.2 yamt } else {
3805 1.2.6.2 yamt /*
3806 1.2.6.2 yamt * Callback worked. If the callback path was down, disable
3807 1.2.6.2 yamt * callbacks, so no more delegations will be issued. (This
3808 1.2.6.2 yamt * is done on the assumption that the callback pathway is
3809 1.2.6.2 yamt * flakey.)
3810 1.2.6.2 yamt */
3811 1.2.6.2 yamt NFSLOCKSTATE();
3812 1.2.6.2 yamt if (clp->lc_flags & LCL_CBDOWN)
3813 1.2.6.2 yamt clp->lc_flags &= ~(LCL_CBDOWN | LCL_CALLBACKSON);
3814 1.2.6.2 yamt NFSUNLOCKSTATE();
3815 1.2.6.2 yamt if (nd->nd_repstat)
3816 1.2.6.2 yamt error = nd->nd_repstat;
3817 1.2.6.2 yamt else if (procnum == NFSV4OP_CBGETATTR)
3818 1.2.6.2 yamt error = nfsv4_loadattr(nd, NULL, nap, NULL, NULL, 0,
3819 1.2.6.2 yamt NULL, NULL, NULL, NULL, NULL, 0, NULL, NULL, NULL,
3820 1.2.6.2 yamt p, NULL);
3821 1.2.6.2 yamt mbuf_freem(nd->nd_mrep);
3822 1.2.6.2 yamt }
3823 1.2.6.2 yamt NFSLOCKSTATE();
3824 1.2.6.2 yamt clp->lc_cbref--;
3825 1.2.6.2 yamt if ((clp->lc_flags & LCL_WAKEUPWANTED) && clp->lc_cbref == 0) {
3826 1.2.6.2 yamt clp->lc_flags &= ~LCL_WAKEUPWANTED;
3827 1.2.6.2 yamt wakeup(clp);
3828 1.2.6.2 yamt }
3829 1.2.6.2 yamt NFSUNLOCKSTATE();
3830 1.2.6.2 yamt
3831 1.2.6.2 yamt NFSEXITCODE(error);
3832 1.2.6.2 yamt return (error);
3833 1.2.6.2 yamt }
3834 1.2.6.2 yamt
3835 1.2.6.2 yamt /*
3836 1.2.6.2 yamt * Return the next index# for a clientid. Mostly just increment and return
3837 1.2.6.2 yamt * the next one, but... if the 32bit unsigned does actually wrap around,
3838 1.2.6.2 yamt * it should be rebooted.
3839 1.2.6.2 yamt * At an average rate of one new client per second, it will wrap around in
3840 1.2.6.2 yamt * approximately 136 years. (I think the server will have been shut
3841 1.2.6.2 yamt * down or rebooted before then.)
3842 1.2.6.2 yamt */
3843 1.2.6.2 yamt static u_int32_t
3844 1.2.6.2 yamt nfsrv_nextclientindex(void)
3845 1.2.6.2 yamt {
3846 1.2.6.2 yamt static u_int32_t client_index = 0;
3847 1.2.6.2 yamt
3848 1.2.6.2 yamt client_index++;
3849 1.2.6.2 yamt if (client_index != 0)
3850 1.2.6.2 yamt return (client_index);
3851 1.2.6.2 yamt
3852 1.2.6.2 yamt printf("%s: out of clientids\n", __func__);
3853 1.2.6.2 yamt return (client_index);
3854 1.2.6.2 yamt }
3855 1.2.6.2 yamt
3856 1.2.6.2 yamt /*
3857 1.2.6.2 yamt * Return the next index# for a stateid. Mostly just increment and return
3858 1.2.6.2 yamt * the next one, but... if the 32bit unsigned does actually wrap around
3859 1.2.6.2 yamt * (will a BSD server stay up that long?), find
3860 1.2.6.2 yamt * new start and end values.
3861 1.2.6.2 yamt */
3862 1.2.6.2 yamt static u_int32_t
3863 1.2.6.2 yamt nfsrv_nextstateindex(struct nfsclient *clp)
3864 1.2.6.2 yamt {
3865 1.2.6.2 yamt struct nfsstate *stp;
3866 1.2.6.2 yamt int i;
3867 1.2.6.2 yamt u_int32_t canuse, min_index, max_index;
3868 1.2.6.2 yamt
3869 1.2.6.2 yamt if (!(clp->lc_flags & LCL_INDEXNOTOK)) {
3870 1.2.6.2 yamt clp->lc_stateindex++;
3871 1.2.6.2 yamt if (clp->lc_stateindex != clp->lc_statemaxindex)
3872 1.2.6.2 yamt return (clp->lc_stateindex);
3873 1.2.6.2 yamt }
3874 1.2.6.2 yamt
3875 1.2.6.2 yamt /*
3876 1.2.6.2 yamt * Yuck, we've hit the end.
3877 1.2.6.2 yamt * Look for a new min and max.
3878 1.2.6.2 yamt */
3879 1.2.6.2 yamt min_index = 0;
3880 1.2.6.2 yamt max_index = 0xffffffff;
3881 1.2.6.2 yamt for (i = 0; i < NFSSTATEHASHSIZE; i++) {
3882 1.2.6.2 yamt LIST_FOREACH(stp, &clp->lc_stateid[i], ls_hash) {
3883 1.2.6.2 yamt if (stp->ls_stateid.other[2] > 0x80000000) {
3884 1.2.6.2 yamt if (stp->ls_stateid.other[2] < max_index)
3885 1.2.6.2 yamt max_index = stp->ls_stateid.other[2];
3886 1.2.6.2 yamt } else {
3887 1.2.6.2 yamt if (stp->ls_stateid.other[2] > min_index)
3888 1.2.6.2 yamt min_index = stp->ls_stateid.other[2];
3889 1.2.6.2 yamt }
3890 1.2.6.2 yamt }
3891 1.2.6.2 yamt }
3892 1.2.6.2 yamt
3893 1.2.6.2 yamt /*
3894 1.2.6.2 yamt * Yikes, highly unlikely, but I'll handle it anyhow.
3895 1.2.6.2 yamt */
3896 1.2.6.2 yamt if (min_index == 0x80000000 && max_index == 0x80000001) {
3897 1.2.6.2 yamt canuse = 0;
3898 1.2.6.2 yamt /*
3899 1.2.6.2 yamt * Loop around until we find an unused entry. Return that
3900 1.2.6.2 yamt * and set LCL_INDEXNOTOK, so the search will continue next time.
3901 1.2.6.2 yamt * (This is one of those rare cases where a goto is the
3902 1.2.6.2 yamt * cleanest way to code the loop.)
3903 1.2.6.2 yamt */
3904 1.2.6.2 yamt tryagain:
3905 1.2.6.2 yamt for (i = 0; i < NFSSTATEHASHSIZE; i++) {
3906 1.2.6.2 yamt LIST_FOREACH(stp, &clp->lc_stateid[i], ls_hash) {
3907 1.2.6.2 yamt if (stp->ls_stateid.other[2] == canuse) {
3908 1.2.6.2 yamt canuse++;
3909 1.2.6.2 yamt goto tryagain;
3910 1.2.6.2 yamt }
3911 1.2.6.2 yamt }
3912 1.2.6.2 yamt }
3913 1.2.6.2 yamt clp->lc_flags |= LCL_INDEXNOTOK;
3914 1.2.6.2 yamt return (canuse);
3915 1.2.6.2 yamt }
3916 1.2.6.2 yamt
3917 1.2.6.2 yamt /*
3918 1.2.6.2 yamt * Ok to start again from min + 1.
3919 1.2.6.2 yamt */
3920 1.2.6.2 yamt clp->lc_stateindex = min_index + 1;
3921 1.2.6.2 yamt clp->lc_statemaxindex = max_index;
3922 1.2.6.2 yamt clp->lc_flags &= ~LCL_INDEXNOTOK;
3923 1.2.6.2 yamt return (clp->lc_stateindex);
3924 1.2.6.2 yamt }
3925 1.2.6.2 yamt
3926 1.2.6.2 yamt /*
3927 1.2.6.2 yamt * The following functions handle the stable storage file that deals with
3928 1.2.6.2 yamt * the edge conditions described in RFC3530 Sec. 8.6.3.
3929 1.2.6.2 yamt * The file is as follows:
3930 1.2.6.2 yamt * - a single record at the beginning that has the lease time of the
3931 1.2.6.2 yamt * previous server instance (before the last reboot) and the nfsrvboottime
3932 1.2.6.2 yamt * values for the previous server boots.
3933 1.2.6.2 yamt * These previous boot times are used to ensure that the current
3934 1.2.6.2 yamt * nfsrvboottime does not, somehow, get set to a previous one.
3935 1.2.6.2 yamt * (This is important so that Stale ClientIDs and StateIDs can
3936 1.2.6.2 yamt * be recognized.)
3937 1.2.6.2 yamt * The number of previous nfsvrboottime values preceeds the list.
3938 1.2.6.2 yamt * - followed by some number of appended records with:
3939 1.2.6.2 yamt * - client id string
3940 1.2.6.2 yamt * - flag that indicates it is a record revoking state via lease
3941 1.2.6.2 yamt * expiration or similar
3942 1.2.6.2 yamt * OR has successfully acquired state.
3943 1.2.6.2 yamt * These structures vary in length, with the client string at the end, up
3944 1.2.6.2 yamt * to NFSV4_OPAQUELIMIT in size.
3945 1.2.6.2 yamt *
3946 1.2.6.2 yamt * At the end of the grace period, the file is truncated, the first
3947 1.2.6.2 yamt * record is rewritten with updated information and any acquired state
3948 1.2.6.2 yamt * records for successful reclaims of state are written.
3949 1.2.6.2 yamt *
3950 1.2.6.2 yamt * Subsequent records are appended when the first state is issued to
3951 1.2.6.2 yamt * a client and when state is revoked for a client.
3952 1.2.6.2 yamt *
3953 1.2.6.2 yamt * When reading the file in, state issued records that come later in
3954 1.2.6.2 yamt * the file override older ones, since the append log is in cronological order.
3955 1.2.6.2 yamt * If, for some reason, the file can't be read, the grace period is
3956 1.2.6.2 yamt * immediately terminated and all reclaims get NFSERR_NOGRACE.
3957 1.2.6.2 yamt */
3958 1.2.6.2 yamt
3959 1.2.6.2 yamt /*
3960 1.2.6.2 yamt * Read in the stable storage file. Called by nfssvc() before the nfsd
3961 1.2.6.2 yamt * processes start servicing requests.
3962 1.2.6.2 yamt */
3963 1.2.6.2 yamt APPLESTATIC void
3964 1.2.6.2 yamt nfsrv_setupstable(NFSPROC_T *p)
3965 1.2.6.2 yamt {
3966 1.2.6.2 yamt struct nfsrv_stablefirst *sf = &nfsrv_stablefirst;
3967 1.2.6.2 yamt struct nfsrv_stable *sp, *nsp;
3968 1.2.6.2 yamt struct nfst_rec *tsp;
3969 1.2.6.2 yamt int error, i, tryagain;
3970 1.2.6.2 yamt off_t off = 0;
3971 1.2.6.2 yamt ssize_t aresid, len;
3972 1.2.6.2 yamt
3973 1.2.6.2 yamt /*
3974 1.2.6.2 yamt * If NFSNSF_UPDATEDONE is set, this is a restart of the nfsds without
3975 1.2.6.2 yamt * a reboot, so state has not been lost.
3976 1.2.6.2 yamt */
3977 1.2.6.2 yamt if (sf->nsf_flags & NFSNSF_UPDATEDONE)
3978 1.2.6.2 yamt return;
3979 1.2.6.2 yamt /*
3980 1.2.6.2 yamt * Set Grace over just until the file reads successfully.
3981 1.2.6.2 yamt */
3982 1.2.6.2 yamt nfsrvboottime = time_second;
3983 1.2.6.2 yamt LIST_INIT(&sf->nsf_head);
3984 1.2.6.2 yamt sf->nsf_flags = (NFSNSF_GRACEOVER | NFSNSF_NEEDLOCK);
3985 1.2.6.2 yamt sf->nsf_eograce = NFSD_MONOSEC + NFSRV_LEASEDELTA;
3986 1.2.6.2 yamt if (sf->nsf_fp == NULL)
3987 1.2.6.2 yamt return;
3988 1.2.6.2 yamt error = NFSD_RDWR(UIO_READ, NFSFPVNODE(sf->nsf_fp),
3989 1.2.6.2 yamt (caddr_t)&sf->nsf_rec, sizeof (struct nfsf_rec), off, UIO_SYSSPACE,
3990 1.2.6.2 yamt 0, NFSFPCRED(sf->nsf_fp), &aresid, p);
3991 1.2.6.2 yamt if (error || aresid || sf->nsf_numboots == 0 ||
3992 1.2.6.2 yamt sf->nsf_numboots > NFSNSF_MAXNUMBOOTS)
3993 1.2.6.2 yamt return;
3994 1.2.6.2 yamt
3995 1.2.6.2 yamt /*
3996 1.2.6.2 yamt * Now, read in the boottimes.
3997 1.2.6.2 yamt */
3998 1.2.6.2 yamt sf->nsf_bootvals = (time_t *)malloc((sf->nsf_numboots + 1) *
3999 1.2.6.2 yamt sizeof (time_t), M_TEMP, M_WAITOK);
4000 1.2.6.2 yamt off = sizeof (struct nfsf_rec);
4001 1.2.6.2 yamt error = NFSD_RDWR(UIO_READ, NFSFPVNODE(sf->nsf_fp),
4002 1.2.6.2 yamt (caddr_t)sf->nsf_bootvals, sf->nsf_numboots * sizeof (time_t), off,
4003 1.2.6.2 yamt UIO_SYSSPACE, 0, NFSFPCRED(sf->nsf_fp), &aresid, p);
4004 1.2.6.2 yamt if (error || aresid) {
4005 1.2.6.2 yamt free((caddr_t)sf->nsf_bootvals, M_TEMP);
4006 1.2.6.2 yamt sf->nsf_bootvals = NULL;
4007 1.2.6.2 yamt return;
4008 1.2.6.2 yamt }
4009 1.2.6.2 yamt
4010 1.2.6.2 yamt /*
4011 1.2.6.2 yamt * Make sure this nfsrvboottime is different from all recorded
4012 1.2.6.2 yamt * previous ones.
4013 1.2.6.2 yamt */
4014 1.2.6.2 yamt do {
4015 1.2.6.2 yamt tryagain = 0;
4016 1.2.6.2 yamt for (i = 0; i < sf->nsf_numboots; i++) {
4017 1.2.6.2 yamt if (nfsrvboottime == sf->nsf_bootvals[i]) {
4018 1.2.6.2 yamt nfsrvboottime++;
4019 1.2.6.2 yamt tryagain = 1;
4020 1.2.6.2 yamt break;
4021 1.2.6.2 yamt }
4022 1.2.6.2 yamt }
4023 1.2.6.2 yamt } while (tryagain);
4024 1.2.6.2 yamt
4025 1.2.6.2 yamt sf->nsf_flags |= NFSNSF_OK;
4026 1.2.6.2 yamt off += (sf->nsf_numboots * sizeof (time_t));
4027 1.2.6.2 yamt
4028 1.2.6.2 yamt /*
4029 1.2.6.2 yamt * Read through the file, building a list of records for grace
4030 1.2.6.2 yamt * checking.
4031 1.2.6.2 yamt * Each record is between sizeof (struct nfst_rec) and
4032 1.2.6.2 yamt * sizeof (struct nfst_rec) + NFSV4_OPAQUELIMIT - 1
4033 1.2.6.2 yamt * and is actually sizeof (struct nfst_rec) + nst_len - 1.
4034 1.2.6.2 yamt */
4035 1.2.6.2 yamt tsp = (struct nfst_rec *)malloc(sizeof (struct nfst_rec) +
4036 1.2.6.2 yamt NFSV4_OPAQUELIMIT - 1, M_TEMP, M_WAITOK);
4037 1.2.6.2 yamt do {
4038 1.2.6.2 yamt error = NFSD_RDWR(UIO_READ, NFSFPVNODE(sf->nsf_fp),
4039 1.2.6.2 yamt (caddr_t)tsp, sizeof (struct nfst_rec) + NFSV4_OPAQUELIMIT - 1,
4040 1.2.6.2 yamt off, UIO_SYSSPACE, 0, NFSFPCRED(sf->nsf_fp), &aresid, p);
4041 1.2.6.2 yamt len = (sizeof (struct nfst_rec) + NFSV4_OPAQUELIMIT - 1) - aresid;
4042 1.2.6.2 yamt if (error || (len > 0 && (len < sizeof (struct nfst_rec) ||
4043 1.2.6.2 yamt len < (sizeof (struct nfst_rec) + tsp->len - 1)))) {
4044 1.2.6.2 yamt /*
4045 1.2.6.2 yamt * Yuck, the file has been corrupted, so just return
4046 1.2.6.2 yamt * after clearing out any restart state, so the grace period
4047 1.2.6.2 yamt * is over.
4048 1.2.6.2 yamt */
4049 1.2.6.2 yamt LIST_FOREACH_SAFE(sp, &sf->nsf_head, nst_list, nsp) {
4050 1.2.6.2 yamt LIST_REMOVE(sp, nst_list);
4051 1.2.6.2 yamt free((caddr_t)sp, M_TEMP);
4052 1.2.6.2 yamt }
4053 1.2.6.2 yamt free((caddr_t)tsp, M_TEMP);
4054 1.2.6.2 yamt sf->nsf_flags &= ~NFSNSF_OK;
4055 1.2.6.2 yamt free((caddr_t)sf->nsf_bootvals, M_TEMP);
4056 1.2.6.2 yamt sf->nsf_bootvals = NULL;
4057 1.2.6.2 yamt return;
4058 1.2.6.2 yamt }
4059 1.2.6.2 yamt if (len > 0) {
4060 1.2.6.2 yamt off += sizeof (struct nfst_rec) + tsp->len - 1;
4061 1.2.6.2 yamt /*
4062 1.2.6.2 yamt * Search the list for a matching client.
4063 1.2.6.2 yamt */
4064 1.2.6.2 yamt LIST_FOREACH(sp, &sf->nsf_head, nst_list) {
4065 1.2.6.2 yamt if (tsp->len == sp->nst_len &&
4066 1.2.6.2 yamt !NFSBCMP(tsp->client, sp->nst_client, tsp->len))
4067 1.2.6.2 yamt break;
4068 1.2.6.2 yamt }
4069 1.2.6.2 yamt if (sp == NULL) {
4070 1.2.6.2 yamt sp = (struct nfsrv_stable *)malloc(tsp->len +
4071 1.2.6.2 yamt sizeof (struct nfsrv_stable) - 1, M_TEMP,
4072 1.2.6.2 yamt M_WAITOK);
4073 1.2.6.2 yamt NFSBCOPY((caddr_t)tsp, (caddr_t)&sp->nst_rec,
4074 1.2.6.2 yamt sizeof (struct nfst_rec) + tsp->len - 1);
4075 1.2.6.2 yamt LIST_INSERT_HEAD(&sf->nsf_head, sp, nst_list);
4076 1.2.6.2 yamt } else {
4077 1.2.6.2 yamt if (tsp->flag == NFSNST_REVOKE)
4078 1.2.6.2 yamt sp->nst_flag |= NFSNST_REVOKE;
4079 1.2.6.2 yamt else
4080 1.2.6.2 yamt /*
4081 1.2.6.2 yamt * A subsequent timestamp indicates the client
4082 1.2.6.2 yamt * did a setclientid/confirm and any previous
4083 1.2.6.2 yamt * revoke is no longer relevant.
4084 1.2.6.2 yamt */
4085 1.2.6.2 yamt sp->nst_flag &= ~NFSNST_REVOKE;
4086 1.2.6.2 yamt }
4087 1.2.6.2 yamt }
4088 1.2.6.2 yamt } while (len > 0);
4089 1.2.6.2 yamt free((caddr_t)tsp, M_TEMP);
4090 1.2.6.2 yamt sf->nsf_flags = NFSNSF_OK;
4091 1.2.6.2 yamt sf->nsf_eograce = NFSD_MONOSEC + sf->nsf_lease +
4092 1.2.6.2 yamt NFSRV_LEASEDELTA;
4093 1.2.6.2 yamt }
4094 1.2.6.2 yamt
4095 1.2.6.2 yamt /*
4096 1.2.6.2 yamt * Update the stable storage file, now that the grace period is over.
4097 1.2.6.2 yamt */
4098 1.2.6.2 yamt APPLESTATIC void
4099 1.2.6.2 yamt nfsrv_updatestable(NFSPROC_T *p)
4100 1.2.6.2 yamt {
4101 1.2.6.2 yamt struct nfsrv_stablefirst *sf = &nfsrv_stablefirst;
4102 1.2.6.2 yamt struct nfsrv_stable *sp, *nsp;
4103 1.2.6.2 yamt int i;
4104 1.2.6.2 yamt struct nfsvattr nva;
4105 1.2.6.2 yamt vnode_t vp;
4106 1.2.6.2 yamt #if defined(__FreeBSD_version) && (__FreeBSD_version >= 500000)
4107 1.2.6.2 yamt mount_t mp = NULL;
4108 1.2.6.2 yamt #endif
4109 1.2.6.2 yamt int error;
4110 1.2.6.2 yamt
4111 1.2.6.2 yamt if (sf->nsf_fp == NULL || (sf->nsf_flags & NFSNSF_UPDATEDONE))
4112 1.2.6.2 yamt return;
4113 1.2.6.2 yamt sf->nsf_flags |= NFSNSF_UPDATEDONE;
4114 1.2.6.2 yamt /*
4115 1.2.6.2 yamt * Ok, we need to rewrite the stable storage file.
4116 1.2.6.2 yamt * - truncate to 0 length
4117 1.2.6.2 yamt * - write the new first structure
4118 1.2.6.2 yamt * - loop through the data structures, writing out any that
4119 1.2.6.2 yamt * have timestamps older than the old boot
4120 1.2.6.2 yamt */
4121 1.2.6.2 yamt if (sf->nsf_bootvals) {
4122 1.2.6.2 yamt sf->nsf_numboots++;
4123 1.2.6.2 yamt for (i = sf->nsf_numboots - 2; i >= 0; i--)
4124 1.2.6.2 yamt sf->nsf_bootvals[i + 1] = sf->nsf_bootvals[i];
4125 1.2.6.2 yamt } else {
4126 1.2.6.2 yamt sf->nsf_numboots = 1;
4127 1.2.6.2 yamt sf->nsf_bootvals = (time_t *)malloc(sizeof (time_t),
4128 1.2.6.2 yamt M_TEMP, M_WAITOK);
4129 1.2.6.2 yamt }
4130 1.2.6.2 yamt sf->nsf_bootvals[0] = nfsrvboottime;
4131 1.2.6.2 yamt sf->nsf_lease = nfsrv_lease;
4132 1.2.6.2 yamt NFSVNO_ATTRINIT(&nva);
4133 1.2.6.2 yamt NFSVNO_SETATTRVAL(&nva, size, 0);
4134 1.2.6.2 yamt vp = NFSFPVNODE(sf->nsf_fp);
4135 1.2.6.2 yamt vn_start_write(vp, &mp, V_WAIT);
4136 1.2.6.2 yamt if (NFSVOPLOCK(vp, LK_EXCLUSIVE) == 0) {
4137 1.2.6.2 yamt error = nfsvno_setattr(vp, &nva, NFSFPCRED(sf->nsf_fp), p,
4138 1.2.6.2 yamt NULL);
4139 1.2.6.2 yamt NFSVOPUNLOCK(vp, 0);
4140 1.2.6.2 yamt } else
4141 1.2.6.2 yamt error = EPERM;
4142 1.2.6.2 yamt vn_finished_write(mp);
4143 1.2.6.2 yamt if (!error)
4144 1.2.6.2 yamt error = NFSD_RDWR(UIO_WRITE, vp,
4145 1.2.6.2 yamt (caddr_t)&sf->nsf_rec, sizeof (struct nfsf_rec), (off_t)0,
4146 1.2.6.2 yamt UIO_SYSSPACE, IO_SYNC, NFSFPCRED(sf->nsf_fp), NULL, p);
4147 1.2.6.2 yamt if (!error)
4148 1.2.6.2 yamt error = NFSD_RDWR(UIO_WRITE, vp,
4149 1.2.6.2 yamt (caddr_t)sf->nsf_bootvals,
4150 1.2.6.2 yamt sf->nsf_numboots * sizeof (time_t),
4151 1.2.6.2 yamt (off_t)(sizeof (struct nfsf_rec)),
4152 1.2.6.2 yamt UIO_SYSSPACE, IO_SYNC, NFSFPCRED(sf->nsf_fp), NULL, p);
4153 1.2.6.2 yamt free((caddr_t)sf->nsf_bootvals, M_TEMP);
4154 1.2.6.2 yamt sf->nsf_bootvals = NULL;
4155 1.2.6.2 yamt if (error) {
4156 1.2.6.2 yamt sf->nsf_flags &= ~NFSNSF_OK;
4157 1.2.6.2 yamt printf("EEK! Can't write NfsV4 stable storage file\n");
4158 1.2.6.2 yamt return;
4159 1.2.6.2 yamt }
4160 1.2.6.2 yamt sf->nsf_flags |= NFSNSF_OK;
4161 1.2.6.2 yamt
4162 1.2.6.2 yamt /*
4163 1.2.6.2 yamt * Loop through the list and write out timestamp records for
4164 1.2.6.2 yamt * any clients that successfully reclaimed state.
4165 1.2.6.2 yamt */
4166 1.2.6.2 yamt LIST_FOREACH_SAFE(sp, &sf->nsf_head, nst_list, nsp) {
4167 1.2.6.2 yamt if (sp->nst_flag & NFSNST_GOTSTATE) {
4168 1.2.6.2 yamt nfsrv_writestable(sp->nst_client, sp->nst_len,
4169 1.2.6.2 yamt NFSNST_NEWSTATE, p);
4170 1.2.6.2 yamt sp->nst_clp->lc_flags |= LCL_STAMPEDSTABLE;
4171 1.2.6.2 yamt }
4172 1.2.6.2 yamt LIST_REMOVE(sp, nst_list);
4173 1.2.6.2 yamt free((caddr_t)sp, M_TEMP);
4174 1.2.6.2 yamt }
4175 1.2.6.2 yamt nfsrv_backupstable();
4176 1.2.6.2 yamt }
4177 1.2.6.2 yamt
4178 1.2.6.2 yamt /*
4179 1.2.6.2 yamt * Append a record to the stable storage file.
4180 1.2.6.2 yamt */
4181 1.2.6.2 yamt APPLESTATIC void
4182 1.2.6.2 yamt nfsrv_writestable(u_char *client, int len, int flag, NFSPROC_T *p)
4183 1.2.6.2 yamt {
4184 1.2.6.2 yamt struct nfsrv_stablefirst *sf = &nfsrv_stablefirst;
4185 1.2.6.2 yamt struct nfst_rec *sp;
4186 1.2.6.2 yamt int error;
4187 1.2.6.2 yamt
4188 1.2.6.2 yamt if (!(sf->nsf_flags & NFSNSF_OK) || sf->nsf_fp == NULL)
4189 1.2.6.2 yamt return;
4190 1.2.6.2 yamt sp = (struct nfst_rec *)malloc(sizeof (struct nfst_rec) +
4191 1.2.6.2 yamt len - 1, M_TEMP, M_WAITOK);
4192 1.2.6.2 yamt sp->len = len;
4193 1.2.6.2 yamt NFSBCOPY(client, sp->client, len);
4194 1.2.6.2 yamt sp->flag = flag;
4195 1.2.6.2 yamt error = NFSD_RDWR(UIO_WRITE, NFSFPVNODE(sf->nsf_fp),
4196 1.2.6.2 yamt (caddr_t)sp, sizeof (struct nfst_rec) + len - 1, (off_t)0,
4197 1.2.6.2 yamt UIO_SYSSPACE, (IO_SYNC | IO_APPEND), NFSFPCRED(sf->nsf_fp), NULL, p);
4198 1.2.6.2 yamt free((caddr_t)sp, M_TEMP);
4199 1.2.6.2 yamt if (error) {
4200 1.2.6.2 yamt sf->nsf_flags &= ~NFSNSF_OK;
4201 1.2.6.2 yamt printf("EEK! Can't write NfsV4 stable storage file\n");
4202 1.2.6.2 yamt }
4203 1.2.6.2 yamt }
4204 1.2.6.2 yamt
4205 1.2.6.2 yamt /*
4206 1.2.6.2 yamt * This function is called during the grace period to mark a client
4207 1.2.6.2 yamt * that successfully reclaimed state.
4208 1.2.6.2 yamt */
4209 1.2.6.2 yamt static void
4210 1.2.6.2 yamt nfsrv_markstable(struct nfsclient *clp)
4211 1.2.6.2 yamt {
4212 1.2.6.2 yamt struct nfsrv_stable *sp;
4213 1.2.6.2 yamt
4214 1.2.6.2 yamt /*
4215 1.2.6.2 yamt * First find the client structure.
4216 1.2.6.2 yamt */
4217 1.2.6.2 yamt LIST_FOREACH(sp, &nfsrv_stablefirst.nsf_head, nst_list) {
4218 1.2.6.2 yamt if (sp->nst_len == clp->lc_idlen &&
4219 1.2.6.2 yamt !NFSBCMP(sp->nst_client, clp->lc_id, sp->nst_len))
4220 1.2.6.2 yamt break;
4221 1.2.6.2 yamt }
4222 1.2.6.2 yamt if (sp == NULL)
4223 1.2.6.2 yamt return;
4224 1.2.6.2 yamt
4225 1.2.6.2 yamt /*
4226 1.2.6.2 yamt * Now, just mark it and set the nfsclient back pointer.
4227 1.2.6.2 yamt */
4228 1.2.6.2 yamt sp->nst_flag |= NFSNST_GOTSTATE;
4229 1.2.6.2 yamt sp->nst_clp = clp;
4230 1.2.6.2 yamt }
4231 1.2.6.2 yamt
4232 1.2.6.2 yamt /*
4233 1.2.6.2 yamt * This function is called for a reclaim, to see if it gets grace.
4234 1.2.6.2 yamt * It returns 0 if a reclaim is allowed, 1 otherwise.
4235 1.2.6.2 yamt */
4236 1.2.6.2 yamt static int
4237 1.2.6.2 yamt nfsrv_checkstable(struct nfsclient *clp)
4238 1.2.6.2 yamt {
4239 1.2.6.2 yamt struct nfsrv_stable *sp;
4240 1.2.6.2 yamt
4241 1.2.6.2 yamt /*
4242 1.2.6.2 yamt * First, find the entry for the client.
4243 1.2.6.2 yamt */
4244 1.2.6.2 yamt LIST_FOREACH(sp, &nfsrv_stablefirst.nsf_head, nst_list) {
4245 1.2.6.2 yamt if (sp->nst_len == clp->lc_idlen &&
4246 1.2.6.2 yamt !NFSBCMP(sp->nst_client, clp->lc_id, sp->nst_len))
4247 1.2.6.2 yamt break;
4248 1.2.6.2 yamt }
4249 1.2.6.2 yamt
4250 1.2.6.2 yamt /*
4251 1.2.6.2 yamt * If not in the list, state was revoked or no state was issued
4252 1.2.6.2 yamt * since the previous reboot, a reclaim is denied.
4253 1.2.6.2 yamt */
4254 1.2.6.2 yamt if (sp == NULL ||
4255 1.2.6.2 yamt (sp->nst_flag & NFSNST_REVOKE) ||
4256 1.2.6.2 yamt !(nfsrv_stablefirst.nsf_flags & NFSNSF_OK))
4257 1.2.6.2 yamt return (1);
4258 1.2.6.2 yamt return (0);
4259 1.2.6.2 yamt }
4260 1.2.6.2 yamt
4261 1.2.6.2 yamt /*
4262 1.2.6.2 yamt * Test for and try to clear out a conflicting client. This is called by
4263 1.2.6.2 yamt * nfsrv_lockctrl() and nfsrv_openctrl() when conflicts with other clients
4264 1.2.6.2 yamt * a found.
4265 1.2.6.2 yamt * The trick here is that it can't revoke a conflicting client with an
4266 1.2.6.2 yamt * expired lease unless it holds the v4root lock, so...
4267 1.2.6.2 yamt * If no v4root lock, get the lock and return 1 to indicate "try again".
4268 1.2.6.2 yamt * Return 0 to indicate the conflict can't be revoked and 1 to indicate
4269 1.2.6.2 yamt * the revocation worked and the conflicting client is "bye, bye", so it
4270 1.2.6.2 yamt * can be tried again.
4271 1.2.6.2 yamt * Return 2 to indicate that the vnode is VI_DOOMED after NFSVOPLOCK().
4272 1.2.6.2 yamt * Unlocks State before a non-zero value is returned.
4273 1.2.6.2 yamt */
4274 1.2.6.2 yamt static int
4275 1.2.6.2 yamt nfsrv_clientconflict(struct nfsclient *clp, int *haslockp, vnode_t vp,
4276 1.2.6.2 yamt NFSPROC_T *p)
4277 1.2.6.2 yamt {
4278 1.2.6.2 yamt int gotlock, lktype;
4279 1.2.6.2 yamt
4280 1.2.6.2 yamt /*
4281 1.2.6.2 yamt * If lease hasn't expired, we can't fix it.
4282 1.2.6.2 yamt */
4283 1.2.6.2 yamt if (clp->lc_expiry >= NFSD_MONOSEC ||
4284 1.2.6.2 yamt !(nfsrv_stablefirst.nsf_flags & NFSNSF_UPDATEDONE))
4285 1.2.6.2 yamt return (0);
4286 1.2.6.2 yamt if (*haslockp == 0) {
4287 1.2.6.2 yamt NFSUNLOCKSTATE();
4288 1.2.6.2 yamt lktype = NFSVOPISLOCKED(vp);
4289 1.2.6.2 yamt NFSVOPUNLOCK(vp, 0);
4290 1.2.6.2 yamt NFSLOCKV4ROOTMUTEX();
4291 1.2.6.2 yamt nfsv4_relref(&nfsv4rootfs_lock);
4292 1.2.6.2 yamt do {
4293 1.2.6.2 yamt gotlock = nfsv4_lock(&nfsv4rootfs_lock, 1, NULL,
4294 1.2.6.2 yamt NFSV4ROOTLOCKMUTEXPTR, NULL);
4295 1.2.6.2 yamt } while (!gotlock);
4296 1.2.6.2 yamt NFSUNLOCKV4ROOTMUTEX();
4297 1.2.6.2 yamt *haslockp = 1;
4298 1.2.6.2 yamt NFSVOPLOCK(vp, lktype | LK_RETRY);
4299 1.2.6.2 yamt if ((vp->v_iflag & VI_DOOMED) != 0)
4300 1.2.6.2 yamt return (2);
4301 1.2.6.2 yamt else
4302 1.2.6.2 yamt return (1);
4303 1.2.6.2 yamt }
4304 1.2.6.2 yamt NFSUNLOCKSTATE();
4305 1.2.6.2 yamt
4306 1.2.6.2 yamt /*
4307 1.2.6.2 yamt * Ok, we can expire the conflicting client.
4308 1.2.6.2 yamt */
4309 1.2.6.2 yamt nfsrv_writestable(clp->lc_id, clp->lc_idlen, NFSNST_REVOKE, p);
4310 1.2.6.2 yamt nfsrv_backupstable();
4311 1.2.6.2 yamt nfsrv_cleanclient(clp, p);
4312 1.2.6.2 yamt nfsrv_freedeleglist(&clp->lc_deleg);
4313 1.2.6.2 yamt nfsrv_freedeleglist(&clp->lc_olddeleg);
4314 1.2.6.2 yamt LIST_REMOVE(clp, lc_hash);
4315 1.2.6.2 yamt nfsrv_zapclient(clp, p);
4316 1.2.6.2 yamt return (1);
4317 1.2.6.2 yamt }
4318 1.2.6.2 yamt
4319 1.2.6.2 yamt /*
4320 1.2.6.2 yamt * Resolve a delegation conflict.
4321 1.2.6.2 yamt * Returns 0 to indicate the conflict was resolved without sleeping.
4322 1.2.6.2 yamt * Return -1 to indicate that the caller should check for conflicts again.
4323 1.2.6.2 yamt * Return > 0 for an error that should be returned, normally NFSERR_DELAY.
4324 1.2.6.2 yamt *
4325 1.2.6.2 yamt * Also, manipulate the nfsv4root_lock, as required. It isn't changed
4326 1.2.6.2 yamt * for a return of 0, since there was no sleep and it could be required
4327 1.2.6.2 yamt * later. It is released for a return of NFSERR_DELAY, since the caller
4328 1.2.6.2 yamt * will return that error. It is released when a sleep was done waiting
4329 1.2.6.2 yamt * for the delegation to be returned or expire (so that other nfsds can
4330 1.2.6.2 yamt * handle ops). Then, it must be acquired for the write to stable storage.
4331 1.2.6.2 yamt * (This function is somewhat similar to nfsrv_clientconflict(), but
4332 1.2.6.2 yamt * the semantics differ in a couple of subtle ways. The return of 0
4333 1.2.6.2 yamt * indicates the conflict was resolved without sleeping here, not
4334 1.2.6.2 yamt * that the conflict can't be resolved and the handling of nfsv4root_lock
4335 1.2.6.2 yamt * differs, as noted above.)
4336 1.2.6.2 yamt * Unlocks State before returning a non-zero value.
4337 1.2.6.2 yamt */
4338 1.2.6.2 yamt static int
4339 1.2.6.2 yamt nfsrv_delegconflict(struct nfsstate *stp, int *haslockp, NFSPROC_T *p,
4340 1.2.6.2 yamt vnode_t vp)
4341 1.2.6.2 yamt {
4342 1.2.6.2 yamt struct nfsclient *clp = stp->ls_clp;
4343 1.2.6.2 yamt int gotlock, error, lktype, retrycnt, zapped_clp;
4344 1.2.6.2 yamt nfsv4stateid_t tstateid;
4345 1.2.6.2 yamt fhandle_t tfh;
4346 1.2.6.2 yamt
4347 1.2.6.2 yamt /*
4348 1.2.6.2 yamt * If the conflict is with an old delegation...
4349 1.2.6.2 yamt */
4350 1.2.6.2 yamt if (stp->ls_flags & NFSLCK_OLDDELEG) {
4351 1.2.6.2 yamt /*
4352 1.2.6.2 yamt * You can delete it, if it has expired.
4353 1.2.6.2 yamt */
4354 1.2.6.2 yamt if (clp->lc_delegtime < NFSD_MONOSEC) {
4355 1.2.6.2 yamt nfsrv_freedeleg(stp);
4356 1.2.6.2 yamt NFSUNLOCKSTATE();
4357 1.2.6.2 yamt error = -1;
4358 1.2.6.2 yamt goto out;
4359 1.2.6.2 yamt }
4360 1.2.6.2 yamt NFSUNLOCKSTATE();
4361 1.2.6.2 yamt /*
4362 1.2.6.2 yamt * During this delay, the old delegation could expire or it
4363 1.2.6.2 yamt * could be recovered by the client via an Open with
4364 1.2.6.2 yamt * CLAIM_DELEGATE_PREV.
4365 1.2.6.2 yamt * Release the nfsv4root_lock, if held.
4366 1.2.6.2 yamt */
4367 1.2.6.2 yamt if (*haslockp) {
4368 1.2.6.2 yamt *haslockp = 0;
4369 1.2.6.2 yamt NFSLOCKV4ROOTMUTEX();
4370 1.2.6.2 yamt nfsv4_unlock(&nfsv4rootfs_lock, 1);
4371 1.2.6.2 yamt NFSUNLOCKV4ROOTMUTEX();
4372 1.2.6.2 yamt }
4373 1.2.6.2 yamt error = NFSERR_DELAY;
4374 1.2.6.2 yamt goto out;
4375 1.2.6.2 yamt }
4376 1.2.6.2 yamt
4377 1.2.6.2 yamt /*
4378 1.2.6.2 yamt * It's a current delegation, so:
4379 1.2.6.2 yamt * - check to see if the delegation has expired
4380 1.2.6.2 yamt * - if so, get the v4root lock and then expire it
4381 1.2.6.2 yamt */
4382 1.2.6.2 yamt if (!(stp->ls_flags & NFSLCK_DELEGRECALL)) {
4383 1.2.6.2 yamt /*
4384 1.2.6.2 yamt * - do a recall callback, since not yet done
4385 1.2.6.2 yamt * For now, never allow truncate to be set. To use
4386 1.2.6.2 yamt * truncate safely, it must be guaranteed that the
4387 1.2.6.2 yamt * Remove, Rename or Setattr with size of 0 will
4388 1.2.6.2 yamt * succeed and that would require major changes to
4389 1.2.6.2 yamt * the VFS/Vnode OPs.
4390 1.2.6.2 yamt * Set the expiry time large enough so that it won't expire
4391 1.2.6.2 yamt * until after the callback, then set it correctly, once
4392 1.2.6.2 yamt * the callback is done. (The delegation will now time
4393 1.2.6.2 yamt * out whether or not the Recall worked ok. The timeout
4394 1.2.6.2 yamt * will be extended when ops are done on the delegation
4395 1.2.6.2 yamt * stateid, up to the timelimit.)
4396 1.2.6.2 yamt */
4397 1.2.6.2 yamt stp->ls_delegtime = NFSD_MONOSEC + (2 * nfsrv_lease) +
4398 1.2.6.2 yamt NFSRV_LEASEDELTA;
4399 1.2.6.2 yamt stp->ls_delegtimelimit = NFSD_MONOSEC + (6 * nfsrv_lease) +
4400 1.2.6.2 yamt NFSRV_LEASEDELTA;
4401 1.2.6.2 yamt stp->ls_flags |= NFSLCK_DELEGRECALL;
4402 1.2.6.2 yamt
4403 1.2.6.2 yamt /*
4404 1.2.6.2 yamt * Loop NFSRV_CBRETRYCNT times while the CBRecall replies
4405 1.2.6.2 yamt * NFSERR_BADSTATEID or NFSERR_BADHANDLE. This is done
4406 1.2.6.2 yamt * in order to try and avoid a race that could happen
4407 1.2.6.2 yamt * when a CBRecall request passed the Open reply with
4408 1.2.6.2 yamt * the delegation in it when transitting the network.
4409 1.2.6.2 yamt * Since nfsrv_docallback will sleep, don't use stp after
4410 1.2.6.2 yamt * the call.
4411 1.2.6.2 yamt */
4412 1.2.6.2 yamt NFSBCOPY((caddr_t)&stp->ls_stateid, (caddr_t)&tstateid,
4413 1.2.6.2 yamt sizeof (tstateid));
4414 1.2.6.2 yamt NFSBCOPY((caddr_t)&stp->ls_lfp->lf_fh, (caddr_t)&tfh,
4415 1.2.6.2 yamt sizeof (tfh));
4416 1.2.6.2 yamt NFSUNLOCKSTATE();
4417 1.2.6.2 yamt if (*haslockp) {
4418 1.2.6.2 yamt *haslockp = 0;
4419 1.2.6.2 yamt NFSLOCKV4ROOTMUTEX();
4420 1.2.6.2 yamt nfsv4_unlock(&nfsv4rootfs_lock, 1);
4421 1.2.6.2 yamt NFSUNLOCKV4ROOTMUTEX();
4422 1.2.6.2 yamt }
4423 1.2.6.2 yamt retrycnt = 0;
4424 1.2.6.2 yamt do {
4425 1.2.6.2 yamt error = nfsrv_docallback(clp, NFSV4OP_CBRECALL,
4426 1.2.6.2 yamt &tstateid, 0, &tfh, NULL, NULL, p);
4427 1.2.6.2 yamt retrycnt++;
4428 1.2.6.2 yamt } while ((error == NFSERR_BADSTATEID ||
4429 1.2.6.2 yamt error == NFSERR_BADHANDLE) && retrycnt < NFSV4_CBRETRYCNT);
4430 1.2.6.2 yamt error = NFSERR_DELAY;
4431 1.2.6.2 yamt goto out;
4432 1.2.6.2 yamt }
4433 1.2.6.2 yamt
4434 1.2.6.2 yamt if (clp->lc_expiry >= NFSD_MONOSEC &&
4435 1.2.6.2 yamt stp->ls_delegtime >= NFSD_MONOSEC) {
4436 1.2.6.2 yamt NFSUNLOCKSTATE();
4437 1.2.6.2 yamt /*
4438 1.2.6.2 yamt * A recall has been done, but it has not yet expired.
4439 1.2.6.2 yamt * So, RETURN_DELAY.
4440 1.2.6.2 yamt */
4441 1.2.6.2 yamt if (*haslockp) {
4442 1.2.6.2 yamt *haslockp = 0;
4443 1.2.6.2 yamt NFSLOCKV4ROOTMUTEX();
4444 1.2.6.2 yamt nfsv4_unlock(&nfsv4rootfs_lock, 1);
4445 1.2.6.2 yamt NFSUNLOCKV4ROOTMUTEX();
4446 1.2.6.2 yamt }
4447 1.2.6.2 yamt error = NFSERR_DELAY;
4448 1.2.6.2 yamt goto out;
4449 1.2.6.2 yamt }
4450 1.2.6.2 yamt
4451 1.2.6.2 yamt /*
4452 1.2.6.2 yamt * If we don't yet have the lock, just get it and then return,
4453 1.2.6.2 yamt * since we need that before deleting expired state, such as
4454 1.2.6.2 yamt * this delegation.
4455 1.2.6.2 yamt * When getting the lock, unlock the vnode, so other nfsds that
4456 1.2.6.2 yamt * are in progress, won't get stuck waiting for the vnode lock.
4457 1.2.6.2 yamt */
4458 1.2.6.2 yamt if (*haslockp == 0) {
4459 1.2.6.2 yamt NFSUNLOCKSTATE();
4460 1.2.6.2 yamt lktype = NFSVOPISLOCKED(vp);
4461 1.2.6.2 yamt NFSVOPUNLOCK(vp, 0);
4462 1.2.6.2 yamt NFSLOCKV4ROOTMUTEX();
4463 1.2.6.2 yamt nfsv4_relref(&nfsv4rootfs_lock);
4464 1.2.6.2 yamt do {
4465 1.2.6.2 yamt gotlock = nfsv4_lock(&nfsv4rootfs_lock, 1, NULL,
4466 1.2.6.2 yamt NFSV4ROOTLOCKMUTEXPTR, NULL);
4467 1.2.6.2 yamt } while (!gotlock);
4468 1.2.6.2 yamt NFSUNLOCKV4ROOTMUTEX();
4469 1.2.6.2 yamt *haslockp = 1;
4470 1.2.6.2 yamt NFSVOPLOCK(vp, lktype | LK_RETRY);
4471 1.2.6.2 yamt if ((vp->v_iflag & VI_DOOMED) != 0) {
4472 1.2.6.2 yamt *haslockp = 0;
4473 1.2.6.2 yamt NFSLOCKV4ROOTMUTEX();
4474 1.2.6.2 yamt nfsv4_unlock(&nfsv4rootfs_lock, 1);
4475 1.2.6.2 yamt NFSUNLOCKV4ROOTMUTEX();
4476 1.2.6.2 yamt error = NFSERR_PERM;
4477 1.2.6.2 yamt goto out;
4478 1.2.6.2 yamt }
4479 1.2.6.2 yamt error = -1;
4480 1.2.6.2 yamt goto out;
4481 1.2.6.2 yamt }
4482 1.2.6.2 yamt
4483 1.2.6.2 yamt NFSUNLOCKSTATE();
4484 1.2.6.2 yamt /*
4485 1.2.6.2 yamt * Ok, we can delete the expired delegation.
4486 1.2.6.2 yamt * First, write the Revoke record to stable storage and then
4487 1.2.6.2 yamt * clear out the conflict.
4488 1.2.6.2 yamt * Since all other nfsd threads are now blocked, we can safely
4489 1.2.6.2 yamt * sleep without the state changing.
4490 1.2.6.2 yamt */
4491 1.2.6.2 yamt nfsrv_writestable(clp->lc_id, clp->lc_idlen, NFSNST_REVOKE, p);
4492 1.2.6.2 yamt nfsrv_backupstable();
4493 1.2.6.2 yamt if (clp->lc_expiry < NFSD_MONOSEC) {
4494 1.2.6.2 yamt nfsrv_cleanclient(clp, p);
4495 1.2.6.2 yamt nfsrv_freedeleglist(&clp->lc_deleg);
4496 1.2.6.2 yamt nfsrv_freedeleglist(&clp->lc_olddeleg);
4497 1.2.6.2 yamt LIST_REMOVE(clp, lc_hash);
4498 1.2.6.2 yamt zapped_clp = 1;
4499 1.2.6.2 yamt } else {
4500 1.2.6.2 yamt nfsrv_freedeleg(stp);
4501 1.2.6.2 yamt zapped_clp = 0;
4502 1.2.6.2 yamt }
4503 1.2.6.2 yamt if (zapped_clp)
4504 1.2.6.2 yamt nfsrv_zapclient(clp, p);
4505 1.2.6.2 yamt error = -1;
4506 1.2.6.2 yamt
4507 1.2.6.2 yamt out:
4508 1.2.6.2 yamt NFSEXITCODE(error);
4509 1.2.6.2 yamt return (error);
4510 1.2.6.2 yamt }
4511 1.2.6.2 yamt
4512 1.2.6.2 yamt /*
4513 1.2.6.2 yamt * Check for a remove allowed, if remove is set to 1 and get rid of
4514 1.2.6.2 yamt * delegations.
4515 1.2.6.2 yamt */
4516 1.2.6.2 yamt APPLESTATIC int
4517 1.2.6.2 yamt nfsrv_checkremove(vnode_t vp, int remove, NFSPROC_T *p)
4518 1.2.6.2 yamt {
4519 1.2.6.2 yamt struct nfsstate *stp;
4520 1.2.6.2 yamt struct nfslockfile *lfp;
4521 1.2.6.2 yamt int error, haslock = 0;
4522 1.2.6.2 yamt fhandle_t nfh;
4523 1.2.6.2 yamt
4524 1.2.6.2 yamt /*
4525 1.2.6.2 yamt * First, get the lock file structure.
4526 1.2.6.2 yamt * (A return of -1 means no associated state, so remove ok.)
4527 1.2.6.2 yamt */
4528 1.2.6.2 yamt error = nfsrv_getlockfh(vp, NFSLCK_CHECK, NULL, &nfh, p);
4529 1.2.6.2 yamt tryagain:
4530 1.2.6.2 yamt NFSLOCKSTATE();
4531 1.2.6.2 yamt if (!error)
4532 1.2.6.2 yamt error = nfsrv_getlockfile(NFSLCK_CHECK, NULL, &lfp, &nfh, 0);
4533 1.2.6.2 yamt if (error) {
4534 1.2.6.2 yamt NFSUNLOCKSTATE();
4535 1.2.6.2 yamt if (haslock) {
4536 1.2.6.2 yamt NFSLOCKV4ROOTMUTEX();
4537 1.2.6.2 yamt nfsv4_unlock(&nfsv4rootfs_lock, 1);
4538 1.2.6.2 yamt NFSUNLOCKV4ROOTMUTEX();
4539 1.2.6.2 yamt }
4540 1.2.6.2 yamt if (error == -1)
4541 1.2.6.2 yamt error = 0;
4542 1.2.6.2 yamt goto out;
4543 1.2.6.2 yamt }
4544 1.2.6.2 yamt
4545 1.2.6.2 yamt /*
4546 1.2.6.2 yamt * Now, we must Recall any delegations.
4547 1.2.6.2 yamt */
4548 1.2.6.2 yamt error = nfsrv_cleandeleg(vp, lfp, NULL, &haslock, p);
4549 1.2.6.2 yamt if (error) {
4550 1.2.6.2 yamt /*
4551 1.2.6.2 yamt * nfsrv_cleandeleg() unlocks state for non-zero
4552 1.2.6.2 yamt * return.
4553 1.2.6.2 yamt */
4554 1.2.6.2 yamt if (error == -1)
4555 1.2.6.2 yamt goto tryagain;
4556 1.2.6.2 yamt if (haslock) {
4557 1.2.6.2 yamt NFSLOCKV4ROOTMUTEX();
4558 1.2.6.2 yamt nfsv4_unlock(&nfsv4rootfs_lock, 1);
4559 1.2.6.2 yamt NFSUNLOCKV4ROOTMUTEX();
4560 1.2.6.2 yamt }
4561 1.2.6.2 yamt goto out;
4562 1.2.6.2 yamt }
4563 1.2.6.2 yamt
4564 1.2.6.2 yamt /*
4565 1.2.6.2 yamt * Now, look for a conflicting open share.
4566 1.2.6.2 yamt */
4567 1.2.6.2 yamt if (remove) {
4568 1.2.6.2 yamt LIST_FOREACH(stp, &lfp->lf_open, ls_file) {
4569 1.2.6.2 yamt if (stp->ls_flags & NFSLCK_WRITEDENY) {
4570 1.2.6.2 yamt error = NFSERR_FILEOPEN;
4571 1.2.6.2 yamt break;
4572 1.2.6.2 yamt }
4573 1.2.6.2 yamt }
4574 1.2.6.2 yamt }
4575 1.2.6.2 yamt
4576 1.2.6.2 yamt NFSUNLOCKSTATE();
4577 1.2.6.2 yamt if (haslock) {
4578 1.2.6.2 yamt NFSLOCKV4ROOTMUTEX();
4579 1.2.6.2 yamt nfsv4_unlock(&nfsv4rootfs_lock, 1);
4580 1.2.6.2 yamt NFSUNLOCKV4ROOTMUTEX();
4581 1.2.6.2 yamt }
4582 1.2.6.2 yamt
4583 1.2.6.2 yamt out:
4584 1.2.6.2 yamt NFSEXITCODE(error);
4585 1.2.6.2 yamt return (error);
4586 1.2.6.2 yamt }
4587 1.2.6.2 yamt
4588 1.2.6.2 yamt /*
4589 1.2.6.2 yamt * Clear out all delegations for the file referred to by lfp.
4590 1.2.6.2 yamt * May return NFSERR_DELAY, if there will be a delay waiting for
4591 1.2.6.2 yamt * delegations to expire.
4592 1.2.6.2 yamt * Returns -1 to indicate it slept while recalling a delegation.
4593 1.2.6.2 yamt * This function has the side effect of deleting the nfslockfile structure,
4594 1.2.6.2 yamt * if it no longer has associated state and didn't have to sleep.
4595 1.2.6.2 yamt * Unlocks State before a non-zero value is returned.
4596 1.2.6.2 yamt */
4597 1.2.6.2 yamt static int
4598 1.2.6.2 yamt nfsrv_cleandeleg(vnode_t vp, struct nfslockfile *lfp,
4599 1.2.6.2 yamt struct nfsclient *clp, int *haslockp, NFSPROC_T *p)
4600 1.2.6.2 yamt {
4601 1.2.6.2 yamt struct nfsstate *stp, *nstp;
4602 1.2.6.2 yamt int ret = 0;
4603 1.2.6.2 yamt
4604 1.2.6.2 yamt stp = LIST_FIRST(&lfp->lf_deleg);
4605 1.2.6.2 yamt while (stp != NULL) {
4606 1.2.6.2 yamt nstp = LIST_NEXT(stp, ls_file);
4607 1.2.6.2 yamt if (stp->ls_clp != clp) {
4608 1.2.6.2 yamt ret = nfsrv_delegconflict(stp, haslockp, p, vp);
4609 1.2.6.2 yamt if (ret) {
4610 1.2.6.2 yamt /*
4611 1.2.6.2 yamt * nfsrv_delegconflict() unlocks state
4612 1.2.6.2 yamt * when it returns non-zero.
4613 1.2.6.2 yamt */
4614 1.2.6.2 yamt goto out;
4615 1.2.6.2 yamt }
4616 1.2.6.2 yamt }
4617 1.2.6.2 yamt stp = nstp;
4618 1.2.6.2 yamt }
4619 1.2.6.2 yamt out:
4620 1.2.6.2 yamt NFSEXITCODE(ret);
4621 1.2.6.2 yamt return (ret);
4622 1.2.6.2 yamt }
4623 1.2.6.2 yamt
4624 1.2.6.2 yamt /*
4625 1.2.6.2 yamt * There are certain operations that, when being done outside of NFSv4,
4626 1.2.6.2 yamt * require that any NFSv4 delegation for the file be recalled.
4627 1.2.6.2 yamt * This function is to be called for those cases:
4628 1.2.6.2 yamt * VOP_RENAME() - When a delegation is being recalled for any reason,
4629 1.2.6.2 yamt * the client may have to do Opens against the server, using the file's
4630 1.2.6.2 yamt * final component name. If the file has been renamed on the server,
4631 1.2.6.2 yamt * that component name will be incorrect and the Open will fail.
4632 1.2.6.2 yamt * VOP_REMOVE() - Theoretically, a client could Open a file after it has
4633 1.2.6.2 yamt * been removed on the server, if there is a delegation issued to
4634 1.2.6.2 yamt * that client for the file. I say "theoretically" since clients
4635 1.2.6.2 yamt * normally do an Access Op before the Open and that Access Op will
4636 1.2.6.2 yamt * fail with ESTALE. Note that NFSv2 and 3 don't even do Opens, so
4637 1.2.6.2 yamt * they will detect the file's removal in the same manner. (There is
4638 1.2.6.2 yamt * one case where RFC3530 allows a client to do an Open without first
4639 1.2.6.2 yamt * doing an Access Op, which is passage of a check against the ACE
4640 1.2.6.2 yamt * returned with a Write delegation, but current practice is to ignore
4641 1.2.6.2 yamt * the ACE and always do an Access Op.)
4642 1.2.6.2 yamt * Since the functions can only be called with an unlocked vnode, this
4643 1.2.6.2 yamt * can't be done at this time.
4644 1.2.6.2 yamt * VOP_ADVLOCK() - When a client holds a delegation, it can issue byte range
4645 1.2.6.2 yamt * locks locally in the client, which are not visible to the server. To
4646 1.2.6.2 yamt * deal with this, issuing of delegations for a vnode must be disabled
4647 1.2.6.2 yamt * and all delegations for the vnode recalled. This is done via the
4648 1.2.6.2 yamt * second function, using the VV_DISABLEDELEG vflag on the vnode.
4649 1.2.6.2 yamt */
4650 1.2.6.2 yamt APPLESTATIC void
4651 1.2.6.2 yamt nfsd_recalldelegation(vnode_t vp, NFSPROC_T *p)
4652 1.2.6.2 yamt {
4653 1.2.6.2 yamt time_t starttime;
4654 1.2.6.2 yamt int error;
4655 1.2.6.2 yamt
4656 1.2.6.2 yamt /*
4657 1.2.6.2 yamt * First, check to see if the server is currently running and it has
4658 1.2.6.2 yamt * been called for a regular file when issuing delegations.
4659 1.2.6.2 yamt */
4660 1.2.6.2 yamt if (newnfs_numnfsd == 0 || vp->v_type != VREG ||
4661 1.2.6.2 yamt nfsrv_issuedelegs == 0)
4662 1.2.6.2 yamt return;
4663 1.2.6.2 yamt
4664 1.2.6.2 yamt KASSERT((NFSVOPISLOCKED(vp) != LK_EXCLUSIVE), ("vp %p is locked", vp));
4665 1.2.6.2 yamt /*
4666 1.2.6.2 yamt * First, get a reference on the nfsv4rootfs_lock so that an
4667 1.2.6.2 yamt * exclusive lock cannot be acquired by another thread.
4668 1.2.6.2 yamt */
4669 1.2.6.2 yamt NFSLOCKV4ROOTMUTEX();
4670 1.2.6.2 yamt nfsv4_getref(&nfsv4rootfs_lock, NULL, NFSV4ROOTLOCKMUTEXPTR, NULL);
4671 1.2.6.2 yamt NFSUNLOCKV4ROOTMUTEX();
4672 1.2.6.2 yamt
4673 1.2.6.2 yamt /*
4674 1.2.6.2 yamt * Now, call nfsrv_checkremove() in a loop while it returns
4675 1.2.6.2 yamt * NFSERR_DELAY. Return upon any other error or when timed out.
4676 1.2.6.2 yamt */
4677 1.2.6.2 yamt starttime = NFSD_MONOSEC;
4678 1.2.6.2 yamt do {
4679 1.2.6.2 yamt if (NFSVOPLOCK(vp, LK_EXCLUSIVE) == 0) {
4680 1.2.6.2 yamt error = nfsrv_checkremove(vp, 0, p);
4681 1.2.6.2 yamt NFSVOPUNLOCK(vp, 0);
4682 1.2.6.2 yamt } else
4683 1.2.6.2 yamt error = EPERM;
4684 1.2.6.2 yamt if (error == NFSERR_DELAY) {
4685 1.2.6.2 yamt if (NFSD_MONOSEC - starttime > NFS_REMOVETIMEO)
4686 1.2.6.2 yamt break;
4687 1.2.6.2 yamt /* Sleep for a short period of time */
4688 1.2.6.2 yamt (void) nfs_catnap(PZERO, 0, "nfsremove");
4689 1.2.6.2 yamt }
4690 1.2.6.2 yamt } while (error == NFSERR_DELAY);
4691 1.2.6.2 yamt NFSLOCKV4ROOTMUTEX();
4692 1.2.6.2 yamt nfsv4_relref(&nfsv4rootfs_lock);
4693 1.2.6.2 yamt NFSUNLOCKV4ROOTMUTEX();
4694 1.2.6.2 yamt }
4695 1.2.6.2 yamt
4696 1.2.6.2 yamt APPLESTATIC void
4697 1.2.6.2 yamt nfsd_disabledelegation(vnode_t vp, NFSPROC_T *p)
4698 1.2.6.2 yamt {
4699 1.2.6.2 yamt
4700 1.2.6.2 yamt #ifdef VV_DISABLEDELEG
4701 1.2.6.2 yamt /*
4702 1.2.6.2 yamt * First, flag issuance of delegations disabled.
4703 1.2.6.2 yamt */
4704 1.2.6.2 yamt atomic_set_long(&vp->v_vflag, VV_DISABLEDELEG);
4705 1.2.6.2 yamt #endif
4706 1.2.6.2 yamt
4707 1.2.6.2 yamt /*
4708 1.2.6.2 yamt * Then call nfsd_recalldelegation() to get rid of all extant
4709 1.2.6.2 yamt * delegations.
4710 1.2.6.2 yamt */
4711 1.2.6.2 yamt nfsd_recalldelegation(vp, p);
4712 1.2.6.2 yamt }
4713 1.2.6.2 yamt
4714 1.2.6.2 yamt /*
4715 1.2.6.2 yamt * Check for conflicting locks, etc. and then get rid of delegations.
4716 1.2.6.2 yamt * (At one point I thought that I should get rid of delegations for any
4717 1.2.6.2 yamt * Setattr, since it could potentially disallow the I/O op (read or write)
4718 1.2.6.2 yamt * allowed by the delegation. However, Setattr Ops that aren't changing
4719 1.2.6.2 yamt * the size get a stateid of all 0s, so you can't tell if it is a delegation
4720 1.2.6.2 yamt * for the same client or a different one, so I decided to only get rid
4721 1.2.6.2 yamt * of delegations for other clients when the size is being changed.)
4722 1.2.6.2 yamt * In general, a Setattr can disable NFS I/O Ops that are outstanding, such
4723 1.2.6.2 yamt * as Write backs, even if there is no delegation, so it really isn't any
4724 1.2.6.2 yamt * different?)
4725 1.2.6.2 yamt */
4726 1.2.6.2 yamt APPLESTATIC int
4727 1.2.6.2 yamt nfsrv_checksetattr(vnode_t vp, struct nfsrv_descript *nd,
4728 1.2.6.2 yamt nfsv4stateid_t *stateidp, struct nfsvattr *nvap, nfsattrbit_t *attrbitp,
4729 1.2.6.2 yamt struct nfsexstuff *exp, NFSPROC_T *p)
4730 1.2.6.2 yamt {
4731 1.2.6.2 yamt struct nfsstate st, *stp = &st;
4732 1.2.6.2 yamt struct nfslock lo, *lop = &lo;
4733 1.2.6.2 yamt int error = 0;
4734 1.2.6.2 yamt nfsquad_t clientid;
4735 1.2.6.2 yamt
4736 1.2.6.2 yamt if (NFSISSET_ATTRBIT(attrbitp, NFSATTRBIT_SIZE)) {
4737 1.2.6.2 yamt stp->ls_flags = (NFSLCK_CHECK | NFSLCK_WRITEACCESS);
4738 1.2.6.2 yamt lop->lo_first = nvap->na_size;
4739 1.2.6.2 yamt } else {
4740 1.2.6.2 yamt stp->ls_flags = 0;
4741 1.2.6.2 yamt lop->lo_first = 0;
4742 1.2.6.2 yamt }
4743 1.2.6.2 yamt if (NFSISSET_ATTRBIT(attrbitp, NFSATTRBIT_OWNER) ||
4744 1.2.6.2 yamt NFSISSET_ATTRBIT(attrbitp, NFSATTRBIT_OWNERGROUP) ||
4745 1.2.6.2 yamt NFSISSET_ATTRBIT(attrbitp, NFSATTRBIT_MODE) ||
4746 1.2.6.2 yamt NFSISSET_ATTRBIT(attrbitp, NFSATTRBIT_ACL))
4747 1.2.6.2 yamt stp->ls_flags |= NFSLCK_SETATTR;
4748 1.2.6.2 yamt if (stp->ls_flags == 0)
4749 1.2.6.2 yamt goto out;
4750 1.2.6.2 yamt lop->lo_end = NFS64BITSSET;
4751 1.2.6.2 yamt lop->lo_flags = NFSLCK_WRITE;
4752 1.2.6.2 yamt stp->ls_ownerlen = 0;
4753 1.2.6.2 yamt stp->ls_op = NULL;
4754 1.2.6.2 yamt stp->ls_uid = nd->nd_cred->cr_uid;
4755 1.2.6.2 yamt stp->ls_stateid.seqid = stateidp->seqid;
4756 1.2.6.2 yamt clientid.lval[0] = stp->ls_stateid.other[0] = stateidp->other[0];
4757 1.2.6.2 yamt clientid.lval[1] = stp->ls_stateid.other[1] = stateidp->other[1];
4758 1.2.6.2 yamt stp->ls_stateid.other[2] = stateidp->other[2];
4759 1.2.6.2 yamt error = nfsrv_lockctrl(vp, &stp, &lop, NULL, clientid,
4760 1.2.6.2 yamt stateidp, exp, nd, p);
4761 1.2.6.2 yamt
4762 1.2.6.2 yamt out:
4763 1.2.6.2 yamt NFSEXITCODE2(error, nd);
4764 1.2.6.2 yamt return (error);
4765 1.2.6.2 yamt }
4766 1.2.6.2 yamt
4767 1.2.6.2 yamt /*
4768 1.2.6.2 yamt * Check for a write delegation and do a CBGETATTR if there is one, updating
4769 1.2.6.2 yamt * the attributes, as required.
4770 1.2.6.2 yamt * Should I return an error if I can't get the attributes? (For now, I'll
4771 1.2.6.2 yamt * just return ok.
4772 1.2.6.2 yamt */
4773 1.2.6.2 yamt APPLESTATIC int
4774 1.2.6.2 yamt nfsrv_checkgetattr(struct nfsrv_descript *nd, vnode_t vp,
4775 1.2.6.2 yamt struct nfsvattr *nvap, nfsattrbit_t *attrbitp, struct ucred *cred,
4776 1.2.6.2 yamt NFSPROC_T *p)
4777 1.2.6.2 yamt {
4778 1.2.6.2 yamt struct nfsstate *stp;
4779 1.2.6.2 yamt struct nfslockfile *lfp;
4780 1.2.6.2 yamt struct nfsclient *clp;
4781 1.2.6.2 yamt struct nfsvattr nva;
4782 1.2.6.2 yamt fhandle_t nfh;
4783 1.2.6.2 yamt int error = 0;
4784 1.2.6.2 yamt nfsattrbit_t cbbits;
4785 1.2.6.2 yamt u_quad_t delegfilerev;
4786 1.2.6.2 yamt
4787 1.2.6.2 yamt NFSCBGETATTR_ATTRBIT(attrbitp, &cbbits);
4788 1.2.6.2 yamt if (!NFSNONZERO_ATTRBIT(&cbbits))
4789 1.2.6.2 yamt goto out;
4790 1.2.6.2 yamt
4791 1.2.6.2 yamt /*
4792 1.2.6.2 yamt * Get the lock file structure.
4793 1.2.6.2 yamt * (A return of -1 means no associated state, so return ok.)
4794 1.2.6.2 yamt */
4795 1.2.6.2 yamt error = nfsrv_getlockfh(vp, NFSLCK_CHECK, NULL, &nfh, p);
4796 1.2.6.2 yamt NFSLOCKSTATE();
4797 1.2.6.2 yamt if (!error)
4798 1.2.6.2 yamt error = nfsrv_getlockfile(NFSLCK_CHECK, NULL, &lfp, &nfh, 0);
4799 1.2.6.2 yamt if (error) {
4800 1.2.6.2 yamt NFSUNLOCKSTATE();
4801 1.2.6.2 yamt if (error == -1)
4802 1.2.6.2 yamt error = 0;
4803 1.2.6.2 yamt goto out;
4804 1.2.6.2 yamt }
4805 1.2.6.2 yamt
4806 1.2.6.2 yamt /*
4807 1.2.6.2 yamt * Now, look for a write delegation.
4808 1.2.6.2 yamt */
4809 1.2.6.2 yamt LIST_FOREACH(stp, &lfp->lf_deleg, ls_file) {
4810 1.2.6.2 yamt if (stp->ls_flags & NFSLCK_DELEGWRITE)
4811 1.2.6.2 yamt break;
4812 1.2.6.2 yamt }
4813 1.2.6.2 yamt if (stp == NULL) {
4814 1.2.6.2 yamt NFSUNLOCKSTATE();
4815 1.2.6.2 yamt goto out;
4816 1.2.6.2 yamt }
4817 1.2.6.2 yamt clp = stp->ls_clp;
4818 1.2.6.2 yamt delegfilerev = stp->ls_filerev;
4819 1.2.6.2 yamt
4820 1.2.6.2 yamt /*
4821 1.2.6.2 yamt * If the Write delegation was issued as a part of this Compound RPC
4822 1.2.6.2 yamt * or if we have an Implied Clientid (used in a previous Op in this
4823 1.2.6.2 yamt * compound) and it is the client the delegation was issued to,
4824 1.2.6.2 yamt * just return ok.
4825 1.2.6.2 yamt * I also assume that it is from the same client iff the network
4826 1.2.6.2 yamt * host IP address is the same as the callback address. (Not
4827 1.2.6.2 yamt * exactly correct by the RFC, but avoids a lot of Getattr
4828 1.2.6.2 yamt * callbacks.)
4829 1.2.6.2 yamt */
4830 1.2.6.2 yamt if (nd->nd_compref == stp->ls_compref ||
4831 1.2.6.2 yamt ((nd->nd_flag & ND_IMPLIEDCLID) &&
4832 1.2.6.2 yamt clp->lc_clientid.qval == nd->nd_clientid.qval) ||
4833 1.2.6.2 yamt nfsaddr2_match(clp->lc_req.nr_nam, nd->nd_nam)) {
4834 1.2.6.2 yamt NFSUNLOCKSTATE();
4835 1.2.6.2 yamt goto out;
4836 1.2.6.2 yamt }
4837 1.2.6.2 yamt
4838 1.2.6.2 yamt /*
4839 1.2.6.2 yamt * We are now done with the delegation state structure,
4840 1.2.6.2 yamt * so the statelock can be released and we can now tsleep().
4841 1.2.6.2 yamt */
4842 1.2.6.2 yamt
4843 1.2.6.2 yamt /*
4844 1.2.6.2 yamt * Now, we must do the CB Getattr callback, to see if Change or Size
4845 1.2.6.2 yamt * has changed.
4846 1.2.6.2 yamt */
4847 1.2.6.2 yamt if (clp->lc_expiry >= NFSD_MONOSEC) {
4848 1.2.6.2 yamt NFSUNLOCKSTATE();
4849 1.2.6.2 yamt NFSVNO_ATTRINIT(&nva);
4850 1.2.6.2 yamt nva.na_filerev = NFS64BITSSET;
4851 1.2.6.2 yamt error = nfsrv_docallback(clp, NFSV4OP_CBGETATTR, NULL,
4852 1.2.6.2 yamt 0, &nfh, &nva, &cbbits, p);
4853 1.2.6.2 yamt if (!error) {
4854 1.2.6.2 yamt if ((nva.na_filerev != NFS64BITSSET &&
4855 1.2.6.2 yamt nva.na_filerev > delegfilerev) ||
4856 1.2.6.2 yamt (NFSVNO_ISSETSIZE(&nva) &&
4857 1.2.6.2 yamt nva.na_size != nvap->na_size)) {
4858 1.2.6.2 yamt nfsvno_updfilerev(vp, nvap, cred, p);
4859 1.2.6.2 yamt if (NFSVNO_ISSETSIZE(&nva))
4860 1.2.6.2 yamt nvap->na_size = nva.na_size;
4861 1.2.6.2 yamt }
4862 1.2.6.2 yamt }
4863 1.2.6.2 yamt } else {
4864 1.2.6.2 yamt NFSUNLOCKSTATE();
4865 1.2.6.2 yamt }
4866 1.2.6.2 yamt error = 0;
4867 1.2.6.2 yamt
4868 1.2.6.2 yamt out:
4869 1.2.6.2 yamt NFSEXITCODE2(error, nd);
4870 1.2.6.2 yamt return (error);
4871 1.2.6.2 yamt }
4872 1.2.6.2 yamt
4873 1.2.6.2 yamt /*
4874 1.2.6.2 yamt * This function looks for openowners that haven't had any opens for
4875 1.2.6.2 yamt * a while and throws them away. Called by an nfsd when NFSNSF_NOOPENS
4876 1.2.6.2 yamt * is set.
4877 1.2.6.2 yamt */
4878 1.2.6.2 yamt APPLESTATIC void
4879 1.2.6.2 yamt nfsrv_throwawayopens(NFSPROC_T *p)
4880 1.2.6.2 yamt {
4881 1.2.6.2 yamt struct nfsclient *clp, *nclp;
4882 1.2.6.2 yamt struct nfsstate *stp, *nstp;
4883 1.2.6.2 yamt int i;
4884 1.2.6.2 yamt
4885 1.2.6.2 yamt NFSLOCKSTATE();
4886 1.2.6.2 yamt nfsrv_stablefirst.nsf_flags &= ~NFSNSF_NOOPENS;
4887 1.2.6.2 yamt /*
4888 1.2.6.2 yamt * For each client...
4889 1.2.6.2 yamt */
4890 1.2.6.2 yamt for (i = 0; i < NFSCLIENTHASHSIZE; i++) {
4891 1.2.6.2 yamt LIST_FOREACH_SAFE(clp, &nfsclienthash[i], lc_hash, nclp) {
4892 1.2.6.2 yamt LIST_FOREACH_SAFE(stp, &clp->lc_open, ls_list, nstp) {
4893 1.2.6.2 yamt if (LIST_EMPTY(&stp->ls_open) &&
4894 1.2.6.2 yamt (stp->ls_noopens > NFSNOOPEN ||
4895 1.2.6.2 yamt (nfsrv_openpluslock * 2) >
4896 1.2.6.2 yamt NFSRV_V4STATELIMIT))
4897 1.2.6.2 yamt nfsrv_freeopenowner(stp, 0, p);
4898 1.2.6.2 yamt }
4899 1.2.6.2 yamt }
4900 1.2.6.2 yamt }
4901 1.2.6.2 yamt NFSUNLOCKSTATE();
4902 1.2.6.2 yamt }
4903 1.2.6.2 yamt
4904 1.2.6.2 yamt /*
4905 1.2.6.2 yamt * This function checks to see if the credentials are the same.
4906 1.2.6.2 yamt * Returns 1 for not same, 0 otherwise.
4907 1.2.6.2 yamt */
4908 1.2.6.2 yamt static int
4909 1.2.6.2 yamt nfsrv_notsamecredname(struct nfsrv_descript *nd, struct nfsclient *clp)
4910 1.2.6.2 yamt {
4911 1.2.6.2 yamt
4912 1.2.6.2 yamt if (nd->nd_flag & ND_GSS) {
4913 1.2.6.2 yamt if (!(clp->lc_flags & LCL_GSS))
4914 1.2.6.2 yamt return (1);
4915 1.2.6.2 yamt if (clp->lc_flags & LCL_NAME) {
4916 1.2.6.2 yamt if (nd->nd_princlen != clp->lc_namelen ||
4917 1.2.6.2 yamt NFSBCMP(nd->nd_principal, clp->lc_name,
4918 1.2.6.2 yamt clp->lc_namelen))
4919 1.2.6.2 yamt return (1);
4920 1.2.6.2 yamt else
4921 1.2.6.2 yamt return (0);
4922 1.2.6.2 yamt }
4923 1.2.6.2 yamt if (nd->nd_cred->cr_uid == clp->lc_uid)
4924 1.2.6.2 yamt return (0);
4925 1.2.6.2 yamt else
4926 1.2.6.2 yamt return (1);
4927 1.2.6.2 yamt } else if (clp->lc_flags & LCL_GSS)
4928 1.2.6.2 yamt return (1);
4929 1.2.6.2 yamt /*
4930 1.2.6.2 yamt * For AUTH_SYS, allow the same uid or root. (This is underspecified
4931 1.2.6.2 yamt * in RFC3530, which talks about principals, but doesn't say anything
4932 1.2.6.2 yamt * about uids for AUTH_SYS.)
4933 1.2.6.2 yamt */
4934 1.2.6.2 yamt if (nd->nd_cred->cr_uid == clp->lc_uid || nd->nd_cred->cr_uid == 0)
4935 1.2.6.2 yamt return (0);
4936 1.2.6.2 yamt else
4937 1.2.6.2 yamt return (1);
4938 1.2.6.2 yamt }
4939 1.2.6.2 yamt
4940 1.2.6.2 yamt /*
4941 1.2.6.2 yamt * Calculate the lease expiry time.
4942 1.2.6.2 yamt */
4943 1.2.6.2 yamt static time_t
4944 1.2.6.2 yamt nfsrv_leaseexpiry(void)
4945 1.2.6.2 yamt {
4946 1.2.6.2 yamt
4947 1.2.6.2 yamt if (nfsrv_stablefirst.nsf_eograce > NFSD_MONOSEC)
4948 1.2.6.2 yamt return (NFSD_MONOSEC + 2 * (nfsrv_lease + NFSRV_LEASEDELTA));
4949 1.2.6.2 yamt return (NFSD_MONOSEC + nfsrv_lease + NFSRV_LEASEDELTA);
4950 1.2.6.2 yamt }
4951 1.2.6.2 yamt
4952 1.2.6.2 yamt /*
4953 1.2.6.2 yamt * Delay the delegation timeout as far as ls_delegtimelimit, as required.
4954 1.2.6.2 yamt */
4955 1.2.6.2 yamt static void
4956 1.2.6.2 yamt nfsrv_delaydelegtimeout(struct nfsstate *stp)
4957 1.2.6.2 yamt {
4958 1.2.6.2 yamt
4959 1.2.6.2 yamt if ((stp->ls_flags & NFSLCK_DELEGRECALL) == 0)
4960 1.2.6.2 yamt return;
4961 1.2.6.2 yamt
4962 1.2.6.2 yamt if ((stp->ls_delegtime + 15) > NFSD_MONOSEC &&
4963 1.2.6.2 yamt stp->ls_delegtime < stp->ls_delegtimelimit) {
4964 1.2.6.2 yamt stp->ls_delegtime += nfsrv_lease;
4965 1.2.6.2 yamt if (stp->ls_delegtime > stp->ls_delegtimelimit)
4966 1.2.6.2 yamt stp->ls_delegtime = stp->ls_delegtimelimit;
4967 1.2.6.2 yamt }
4968 1.2.6.2 yamt }
4969 1.2.6.2 yamt
4970 1.2.6.2 yamt /*
4971 1.2.6.2 yamt * This function checks to see if there is any other state associated
4972 1.2.6.2 yamt * with the openowner for this Open.
4973 1.2.6.2 yamt * It returns 1 if there is no other state, 0 otherwise.
4974 1.2.6.2 yamt */
4975 1.2.6.2 yamt static int
4976 1.2.6.2 yamt nfsrv_nootherstate(struct nfsstate *stp)
4977 1.2.6.2 yamt {
4978 1.2.6.2 yamt struct nfsstate *tstp;
4979 1.2.6.2 yamt
4980 1.2.6.2 yamt LIST_FOREACH(tstp, &stp->ls_openowner->ls_open, ls_list) {
4981 1.2.6.2 yamt if (tstp != stp || !LIST_EMPTY(&tstp->ls_lock))
4982 1.2.6.2 yamt return (0);
4983 1.2.6.2 yamt }
4984 1.2.6.2 yamt return (1);
4985 1.2.6.2 yamt }
4986 1.2.6.2 yamt
4987 1.2.6.2 yamt /*
4988 1.2.6.2 yamt * Create a list of lock deltas (changes to local byte range locking
4989 1.2.6.2 yamt * that can be rolled back using the list) and apply the changes via
4990 1.2.6.2 yamt * nfsvno_advlock(). Optionally, lock the list. It is expected that either
4991 1.2.6.2 yamt * the rollback or update function will be called after this.
4992 1.2.6.2 yamt * It returns an error (and rolls back, as required), if any nfsvno_advlock()
4993 1.2.6.2 yamt * call fails. If it returns an error, it will unlock the list.
4994 1.2.6.2 yamt */
4995 1.2.6.2 yamt static int
4996 1.2.6.2 yamt nfsrv_locallock(vnode_t vp, struct nfslockfile *lfp, int flags,
4997 1.2.6.2 yamt uint64_t first, uint64_t end, struct nfslockconflict *cfp, NFSPROC_T *p)
4998 1.2.6.2 yamt {
4999 1.2.6.2 yamt struct nfslock *lop, *nlop;
5000 1.2.6.2 yamt int error = 0;
5001 1.2.6.2 yamt
5002 1.2.6.2 yamt /* Loop through the list of locks. */
5003 1.2.6.2 yamt lop = LIST_FIRST(&lfp->lf_locallock);
5004 1.2.6.2 yamt while (first < end && lop != NULL) {
5005 1.2.6.2 yamt nlop = LIST_NEXT(lop, lo_lckowner);
5006 1.2.6.2 yamt if (first >= lop->lo_end) {
5007 1.2.6.2 yamt /* not there yet */
5008 1.2.6.2 yamt lop = nlop;
5009 1.2.6.2 yamt } else if (first < lop->lo_first) {
5010 1.2.6.2 yamt /* new one starts before entry in list */
5011 1.2.6.2 yamt if (end <= lop->lo_first) {
5012 1.2.6.2 yamt /* no overlap between old and new */
5013 1.2.6.2 yamt error = nfsrv_dolocal(vp, lfp, flags,
5014 1.2.6.2 yamt NFSLCK_UNLOCK, first, end, cfp, p);
5015 1.2.6.2 yamt if (error != 0)
5016 1.2.6.2 yamt break;
5017 1.2.6.2 yamt first = end;
5018 1.2.6.2 yamt } else {
5019 1.2.6.2 yamt /* handle fragment overlapped with new one */
5020 1.2.6.2 yamt error = nfsrv_dolocal(vp, lfp, flags,
5021 1.2.6.2 yamt NFSLCK_UNLOCK, first, lop->lo_first, cfp,
5022 1.2.6.2 yamt p);
5023 1.2.6.2 yamt if (error != 0)
5024 1.2.6.2 yamt break;
5025 1.2.6.2 yamt first = lop->lo_first;
5026 1.2.6.2 yamt }
5027 1.2.6.2 yamt } else {
5028 1.2.6.2 yamt /* new one overlaps this entry in list */
5029 1.2.6.2 yamt if (end <= lop->lo_end) {
5030 1.2.6.2 yamt /* overlaps all of new one */
5031 1.2.6.2 yamt error = nfsrv_dolocal(vp, lfp, flags,
5032 1.2.6.2 yamt lop->lo_flags, first, end, cfp, p);
5033 1.2.6.2 yamt if (error != 0)
5034 1.2.6.2 yamt break;
5035 1.2.6.2 yamt first = end;
5036 1.2.6.2 yamt } else {
5037 1.2.6.2 yamt /* handle fragment overlapped with new one */
5038 1.2.6.2 yamt error = nfsrv_dolocal(vp, lfp, flags,
5039 1.2.6.2 yamt lop->lo_flags, first, lop->lo_end, cfp, p);
5040 1.2.6.2 yamt if (error != 0)
5041 1.2.6.2 yamt break;
5042 1.2.6.2 yamt first = lop->lo_end;
5043 1.2.6.2 yamt lop = nlop;
5044 1.2.6.2 yamt }
5045 1.2.6.2 yamt }
5046 1.2.6.2 yamt }
5047 1.2.6.2 yamt if (first < end && error == 0)
5048 1.2.6.2 yamt /* handle fragment past end of list */
5049 1.2.6.2 yamt error = nfsrv_dolocal(vp, lfp, flags, NFSLCK_UNLOCK, first,
5050 1.2.6.2 yamt end, cfp, p);
5051 1.2.6.2 yamt
5052 1.2.6.2 yamt NFSEXITCODE(error);
5053 1.2.6.2 yamt return (error);
5054 1.2.6.2 yamt }
5055 1.2.6.2 yamt
5056 1.2.6.2 yamt /*
5057 1.2.6.2 yamt * Local lock unlock. Unlock all byte ranges that are no longer locked
5058 1.2.6.2 yamt * by NFSv4. To do this, unlock any subranges of first-->end that
5059 1.2.6.2 yamt * do not overlap with the byte ranges of any lock in the lfp->lf_lock
5060 1.2.6.2 yamt * list. This list has all locks for the file held by other
5061 1.2.6.2 yamt * <clientid, lockowner> tuples. The list is ordered by increasing
5062 1.2.6.2 yamt * lo_first value, but may have entries that overlap each other, for
5063 1.2.6.2 yamt * the case of read locks.
5064 1.2.6.2 yamt */
5065 1.2.6.2 yamt static void
5066 1.2.6.2 yamt nfsrv_localunlock(vnode_t vp, struct nfslockfile *lfp, uint64_t init_first,
5067 1.2.6.2 yamt uint64_t init_end, NFSPROC_T *p)
5068 1.2.6.2 yamt {
5069 1.2.6.2 yamt struct nfslock *lop;
5070 1.2.6.2 yamt uint64_t first, end, prevfirst;
5071 1.2.6.2 yamt
5072 1.2.6.2 yamt first = init_first;
5073 1.2.6.2 yamt end = init_end;
5074 1.2.6.2 yamt while (first < init_end) {
5075 1.2.6.2 yamt /* Loop through all nfs locks, adjusting first and end */
5076 1.2.6.2 yamt prevfirst = 0;
5077 1.2.6.2 yamt LIST_FOREACH(lop, &lfp->lf_lock, lo_lckfile) {
5078 1.2.6.2 yamt KASSERT(prevfirst <= lop->lo_first,
5079 1.2.6.2 yamt ("nfsv4 locks out of order"));
5080 1.2.6.2 yamt KASSERT(lop->lo_first < lop->lo_end,
5081 1.2.6.2 yamt ("nfsv4 bogus lock"));
5082 1.2.6.2 yamt prevfirst = lop->lo_first;
5083 1.2.6.2 yamt if (first >= lop->lo_first &&
5084 1.2.6.2 yamt first < lop->lo_end)
5085 1.2.6.2 yamt /*
5086 1.2.6.2 yamt * Overlaps with initial part, so trim
5087 1.2.6.2 yamt * off that initial part by moving first past
5088 1.2.6.2 yamt * it.
5089 1.2.6.2 yamt */
5090 1.2.6.2 yamt first = lop->lo_end;
5091 1.2.6.2 yamt else if (end > lop->lo_first &&
5092 1.2.6.2 yamt lop->lo_first > first) {
5093 1.2.6.2 yamt /*
5094 1.2.6.2 yamt * This lock defines the end of the
5095 1.2.6.2 yamt * segment to unlock, so set end to the
5096 1.2.6.2 yamt * start of it and break out of the loop.
5097 1.2.6.2 yamt */
5098 1.2.6.2 yamt end = lop->lo_first;
5099 1.2.6.2 yamt break;
5100 1.2.6.2 yamt }
5101 1.2.6.2 yamt if (first >= end)
5102 1.2.6.2 yamt /*
5103 1.2.6.2 yamt * There is no segment left to do, so
5104 1.2.6.2 yamt * break out of this loop and then exit
5105 1.2.6.2 yamt * the outer while() since first will be set
5106 1.2.6.2 yamt * to end, which must equal init_end here.
5107 1.2.6.2 yamt */
5108 1.2.6.2 yamt break;
5109 1.2.6.2 yamt }
5110 1.2.6.2 yamt if (first < end) {
5111 1.2.6.2 yamt /* Unlock this segment */
5112 1.2.6.2 yamt (void) nfsrv_dolocal(vp, lfp, NFSLCK_UNLOCK,
5113 1.2.6.2 yamt NFSLCK_READ, first, end, NULL, p);
5114 1.2.6.2 yamt nfsrv_locallock_commit(lfp, NFSLCK_UNLOCK,
5115 1.2.6.2 yamt first, end);
5116 1.2.6.2 yamt }
5117 1.2.6.2 yamt /*
5118 1.2.6.2 yamt * Now move past this segment and look for any further
5119 1.2.6.2 yamt * segment in the range, if there is one.
5120 1.2.6.2 yamt */
5121 1.2.6.2 yamt first = end;
5122 1.2.6.2 yamt end = init_end;
5123 1.2.6.2 yamt }
5124 1.2.6.2 yamt }
5125 1.2.6.2 yamt
5126 1.2.6.2 yamt /*
5127 1.2.6.2 yamt * Do the local lock operation and update the rollback list, as required.
5128 1.2.6.2 yamt * Perform the rollback and return the error if nfsvno_advlock() fails.
5129 1.2.6.2 yamt */
5130 1.2.6.2 yamt static int
5131 1.2.6.2 yamt nfsrv_dolocal(vnode_t vp, struct nfslockfile *lfp, int flags, int oldflags,
5132 1.2.6.2 yamt uint64_t first, uint64_t end, struct nfslockconflict *cfp, NFSPROC_T *p)
5133 1.2.6.2 yamt {
5134 1.2.6.2 yamt struct nfsrollback *rlp;
5135 1.2.6.2 yamt int error = 0, ltype, oldltype;
5136 1.2.6.2 yamt
5137 1.2.6.2 yamt if (flags & NFSLCK_WRITE)
5138 1.2.6.2 yamt ltype = F_WRLCK;
5139 1.2.6.2 yamt else if (flags & NFSLCK_READ)
5140 1.2.6.2 yamt ltype = F_RDLCK;
5141 1.2.6.2 yamt else
5142 1.2.6.2 yamt ltype = F_UNLCK;
5143 1.2.6.2 yamt if (oldflags & NFSLCK_WRITE)
5144 1.2.6.2 yamt oldltype = F_WRLCK;
5145 1.2.6.2 yamt else if (oldflags & NFSLCK_READ)
5146 1.2.6.2 yamt oldltype = F_RDLCK;
5147 1.2.6.2 yamt else
5148 1.2.6.2 yamt oldltype = F_UNLCK;
5149 1.2.6.2 yamt if (ltype == oldltype || (oldltype == F_WRLCK && ltype == F_RDLCK))
5150 1.2.6.2 yamt /* nothing to do */
5151 1.2.6.2 yamt goto out;
5152 1.2.6.2 yamt error = nfsvno_advlock(vp, ltype, first, end, p);
5153 1.2.6.2 yamt if (error != 0) {
5154 1.2.6.2 yamt if (cfp != NULL) {
5155 1.2.6.2 yamt cfp->cl_clientid.lval[0] = 0;
5156 1.2.6.2 yamt cfp->cl_clientid.lval[1] = 0;
5157 1.2.6.2 yamt cfp->cl_first = 0;
5158 1.2.6.2 yamt cfp->cl_end = NFS64BITSSET;
5159 1.2.6.2 yamt cfp->cl_flags = NFSLCK_WRITE;
5160 1.2.6.2 yamt cfp->cl_ownerlen = 5;
5161 1.2.6.2 yamt NFSBCOPY("LOCAL", cfp->cl_owner, 5);
5162 1.2.6.2 yamt }
5163 1.2.6.2 yamt nfsrv_locallock_rollback(vp, lfp, p);
5164 1.2.6.2 yamt } else if (ltype != F_UNLCK) {
5165 1.2.6.2 yamt rlp = malloc(sizeof (struct nfsrollback), M_NFSDROLLBACK,
5166 1.2.6.2 yamt M_WAITOK);
5167 1.2.6.2 yamt rlp->rlck_first = first;
5168 1.2.6.2 yamt rlp->rlck_end = end;
5169 1.2.6.2 yamt rlp->rlck_type = oldltype;
5170 1.2.6.2 yamt LIST_INSERT_HEAD(&lfp->lf_rollback, rlp, rlck_list);
5171 1.2.6.2 yamt }
5172 1.2.6.2 yamt
5173 1.2.6.2 yamt out:
5174 1.2.6.2 yamt NFSEXITCODE(error);
5175 1.2.6.2 yamt return (error);
5176 1.2.6.2 yamt }
5177 1.2.6.2 yamt
5178 1.2.6.2 yamt /*
5179 1.2.6.2 yamt * Roll back local lock changes and free up the rollback list.
5180 1.2.6.2 yamt */
5181 1.2.6.2 yamt static void
5182 1.2.6.2 yamt nfsrv_locallock_rollback(vnode_t vp, struct nfslockfile *lfp, NFSPROC_T *p)
5183 1.2.6.2 yamt {
5184 1.2.6.2 yamt struct nfsrollback *rlp, *nrlp;
5185 1.2.6.2 yamt
5186 1.2.6.2 yamt LIST_FOREACH_SAFE(rlp, &lfp->lf_rollback, rlck_list, nrlp) {
5187 1.2.6.2 yamt (void) nfsvno_advlock(vp, rlp->rlck_type, rlp->rlck_first,
5188 1.2.6.2 yamt rlp->rlck_end, p);
5189 1.2.6.2 yamt free(rlp, M_NFSDROLLBACK);
5190 1.2.6.2 yamt }
5191 1.2.6.2 yamt LIST_INIT(&lfp->lf_rollback);
5192 1.2.6.2 yamt }
5193 1.2.6.2 yamt
5194 1.2.6.2 yamt /*
5195 1.2.6.2 yamt * Update local lock list and delete rollback list (ie now committed to the
5196 1.2.6.2 yamt * local locks). Most of the work is done by the internal function.
5197 1.2.6.2 yamt */
5198 1.2.6.2 yamt static void
5199 1.2.6.2 yamt nfsrv_locallock_commit(struct nfslockfile *lfp, int flags, uint64_t first,
5200 1.2.6.2 yamt uint64_t end)
5201 1.2.6.2 yamt {
5202 1.2.6.2 yamt struct nfsrollback *rlp, *nrlp;
5203 1.2.6.2 yamt struct nfslock *new_lop, *other_lop;
5204 1.2.6.2 yamt
5205 1.2.6.2 yamt new_lop = malloc(sizeof (struct nfslock), M_NFSDLOCK, M_WAITOK);
5206 1.2.6.2 yamt if (flags & (NFSLCK_READ | NFSLCK_WRITE))
5207 1.2.6.2 yamt other_lop = malloc(sizeof (struct nfslock), M_NFSDLOCK,
5208 1.2.6.2 yamt M_WAITOK);
5209 1.2.6.2 yamt else
5210 1.2.6.2 yamt other_lop = NULL;
5211 1.2.6.2 yamt new_lop->lo_flags = flags;
5212 1.2.6.2 yamt new_lop->lo_first = first;
5213 1.2.6.2 yamt new_lop->lo_end = end;
5214 1.2.6.2 yamt nfsrv_updatelock(NULL, &new_lop, &other_lop, lfp);
5215 1.2.6.2 yamt if (new_lop != NULL)
5216 1.2.6.2 yamt free(new_lop, M_NFSDLOCK);
5217 1.2.6.2 yamt if (other_lop != NULL)
5218 1.2.6.2 yamt free(other_lop, M_NFSDLOCK);
5219 1.2.6.2 yamt
5220 1.2.6.2 yamt /* and get rid of the rollback list */
5221 1.2.6.2 yamt LIST_FOREACH_SAFE(rlp, &lfp->lf_rollback, rlck_list, nrlp)
5222 1.2.6.2 yamt free(rlp, M_NFSDROLLBACK);
5223 1.2.6.2 yamt LIST_INIT(&lfp->lf_rollback);
5224 1.2.6.2 yamt }
5225 1.2.6.2 yamt
5226 1.2.6.2 yamt /*
5227 1.2.6.2 yamt * Lock the struct nfslockfile for local lock updating.
5228 1.2.6.2 yamt */
5229 1.2.6.2 yamt static void
5230 1.2.6.2 yamt nfsrv_locklf(struct nfslockfile *lfp)
5231 1.2.6.2 yamt {
5232 1.2.6.2 yamt int gotlock;
5233 1.2.6.2 yamt
5234 1.2.6.2 yamt /* lf_usecount ensures *lfp won't be free'd */
5235 1.2.6.2 yamt lfp->lf_usecount++;
5236 1.2.6.2 yamt do {
5237 1.2.6.2 yamt gotlock = nfsv4_lock(&lfp->lf_locallock_lck, 1, NULL,
5238 1.2.6.2 yamt NFSSTATEMUTEXPTR, NULL);
5239 1.2.6.2 yamt } while (gotlock == 0);
5240 1.2.6.2 yamt lfp->lf_usecount--;
5241 1.2.6.2 yamt }
5242 1.2.6.2 yamt
5243 1.2.6.2 yamt /*
5244 1.2.6.2 yamt * Unlock the struct nfslockfile after local lock updating.
5245 1.2.6.2 yamt */
5246 1.2.6.2 yamt static void
5247 1.2.6.2 yamt nfsrv_unlocklf(struct nfslockfile *lfp)
5248 1.2.6.2 yamt {
5249 1.2.6.2 yamt
5250 1.2.6.2 yamt nfsv4_unlock(&lfp->lf_locallock_lck, 0);
5251 1.2.6.2 yamt }
5252 1.2.6.2 yamt
5253 1.2.6.2 yamt /*
5254 1.2.6.2 yamt * Clear out all state for the NFSv4 server.
5255 1.2.6.2 yamt * Must be called by a thread that can sleep when no nfsds are running.
5256 1.2.6.2 yamt */
5257 1.2.6.2 yamt void
5258 1.2.6.2 yamt nfsrv_throwawayallstate(NFSPROC_T *p)
5259 1.2.6.2 yamt {
5260 1.2.6.2 yamt struct nfsclient *clp, *nclp;
5261 1.2.6.2 yamt struct nfslockfile *lfp, *nlfp;
5262 1.2.6.2 yamt int i;
5263 1.2.6.2 yamt
5264 1.2.6.2 yamt /*
5265 1.2.6.2 yamt * For each client, clean out the state and then free the structure.
5266 1.2.6.2 yamt */
5267 1.2.6.2 yamt for (i = 0; i < NFSCLIENTHASHSIZE; i++) {
5268 1.2.6.2 yamt LIST_FOREACH_SAFE(clp, &nfsclienthash[i], lc_hash, nclp) {
5269 1.2.6.2 yamt nfsrv_cleanclient(clp, p);
5270 1.2.6.2 yamt nfsrv_freedeleglist(&clp->lc_deleg);
5271 1.2.6.2 yamt nfsrv_freedeleglist(&clp->lc_olddeleg);
5272 1.2.6.2 yamt free(clp, M_NFSDCLIENT);
5273 1.2.6.2 yamt }
5274 1.2.6.2 yamt }
5275 1.2.6.2 yamt
5276 1.2.6.2 yamt /*
5277 1.2.6.2 yamt * Also, free up any remaining lock file structures.
5278 1.2.6.2 yamt */
5279 1.2.6.2 yamt for (i = 0; i < NFSLOCKHASHSIZE; i++) {
5280 1.2.6.2 yamt LIST_FOREACH_SAFE(lfp, &nfslockhash[i], lf_hash, nlfp) {
5281 1.2.6.2 yamt printf("nfsd unload: fnd a lock file struct\n");
5282 1.2.6.2 yamt nfsrv_freenfslockfile(lfp);
5283 1.2.6.2 yamt }
5284 1.2.6.2 yamt }
5285 1.2.6.2 yamt }
5286 1.2.6.2 yamt
5287