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