Commit | Line | Data |
---|---|---|
1da177e4 | 1 | /* |
1da177e4 LT |
2 | * Copyright (c) 2001 The Regents of the University of Michigan. |
3 | * All rights reserved. | |
4 | * | |
5 | * Kendrick Smith <kmsmith@umich.edu> | |
6 | * Andy Adamson <kandros@umich.edu> | |
7 | * | |
8 | * Redistribution and use in source and binary forms, with or without | |
9 | * modification, are permitted provided that the following conditions | |
10 | * are met: | |
11 | * | |
12 | * 1. Redistributions of source code must retain the above copyright | |
13 | * notice, this list of conditions and the following disclaimer. | |
14 | * 2. Redistributions in binary form must reproduce the above copyright | |
15 | * notice, this list of conditions and the following disclaimer in the | |
16 | * documentation and/or other materials provided with the distribution. | |
17 | * 3. Neither the name of the University nor the names of its | |
18 | * contributors may be used to endorse or promote products derived | |
19 | * from this software without specific prior written permission. | |
20 | * | |
21 | * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED | |
22 | * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF | |
23 | * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | |
24 | * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | |
25 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | |
26 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | |
27 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR | |
28 | * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF | |
29 | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING | |
30 | * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS | |
31 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
32 | * | |
33 | */ | |
34 | ||
aceaf78d | 35 | #include <linux/file.h> |
b89f4321 | 36 | #include <linux/fs.h> |
5a0e3ad6 | 37 | #include <linux/slab.h> |
0964a3d3 | 38 | #include <linux/namei.h> |
c2f1a551 | 39 | #include <linux/swap.h> |
17456804 | 40 | #include <linux/pagemap.h> |
7df302f7 | 41 | #include <linux/ratelimit.h> |
68e76ad0 | 42 | #include <linux/sunrpc/svcauth_gss.h> |
5976687a | 43 | #include <linux/sunrpc/addr.h> |
87545899 | 44 | #include <linux/jhash.h> |
169319f1 | 45 | #include <linux/string_helpers.h> |
9a74af21 | 46 | #include "xdr4.h" |
06b332a5 | 47 | #include "xdr4cb.h" |
0a3adade | 48 | #include "vfs.h" |
bfa4b365 | 49 | #include "current_stateid.h" |
1da177e4 | 50 | |
5e1533c7 | 51 | #include "netns.h" |
9cf514cc | 52 | #include "pnfs.h" |
fd4f83fd | 53 | #include "filecache.h" |
dd5e3fbc | 54 | #include "trace.h" |
5e1533c7 | 55 | |
1da177e4 LT |
56 | #define NFSDDBG_FACILITY NFSDDBG_PROC |
57 | ||
f32f3c2d BF |
58 | #define all_ones {{~0,~0},~0} |
59 | static const stateid_t one_stateid = { | |
60 | .si_generation = ~0, | |
61 | .si_opaque = all_ones, | |
62 | }; | |
63 | static const stateid_t zero_stateid = { | |
64 | /* all fields zero */ | |
65 | }; | |
19ff0f28 TM |
66 | static const stateid_t currentstateid = { |
67 | .si_generation = 1, | |
68 | }; | |
fb500a7c TM |
69 | static const stateid_t close_stateid = { |
70 | .si_generation = 0xffffffffU, | |
71 | }; | |
f32f3c2d | 72 | |
ec6b5d7b | 73 | static u64 current_sessionid = 1; |
fd39ca9a | 74 | |
f32f3c2d BF |
75 | #define ZERO_STATEID(stateid) (!memcmp((stateid), &zero_stateid, sizeof(stateid_t))) |
76 | #define ONE_STATEID(stateid) (!memcmp((stateid), &one_stateid, sizeof(stateid_t))) | |
19ff0f28 | 77 | #define CURRENT_STATEID(stateid) (!memcmp((stateid), ¤tstateid, sizeof(stateid_t))) |
ae254dac | 78 | #define CLOSE_STATEID(stateid) (!memcmp((stateid), &close_stateid, sizeof(stateid_t))) |
1da177e4 | 79 | |
1da177e4 | 80 | /* forward declarations */ |
f9c00c3a | 81 | static bool check_for_locks(struct nfs4_file *fp, struct nfs4_lockowner *lowner); |
6011695d | 82 | static void nfs4_free_ol_stateid(struct nfs4_stid *stid); |
362063a5 | 83 | void nfsd4_end_grace(struct nfsd_net *nn); |
624322f1 | 84 | static void _free_cpntf_state_locked(struct nfsd_net *nn, struct nfs4_cpntf_state *cps); |
1da177e4 | 85 | |
8b671b80 BF |
86 | /* Locking: */ |
87 | ||
8b671b80 BF |
88 | /* |
89 | * Currently used for the del_recall_lru and file hash table. In an | |
90 | * effort to decrease the scope of the client_mutex, this spinlock may | |
91 | * eventually cover more: | |
92 | */ | |
cdc97505 | 93 | static DEFINE_SPINLOCK(state_lock); |
8b671b80 | 94 | |
4f34bd05 AE |
95 | enum nfsd4_st_mutex_lock_subclass { |
96 | OPEN_STATEID_MUTEX = 0, | |
97 | LOCK_STATEID_MUTEX = 1, | |
98 | }; | |
99 | ||
b401be22 JL |
100 | /* |
101 | * A waitqueue for all in-progress 4.0 CLOSE operations that are waiting for | |
102 | * the refcount on the open stateid to drop. | |
103 | */ | |
104 | static DECLARE_WAIT_QUEUE_HEAD(close_wq); | |
105 | ||
89c905be BF |
106 | /* |
107 | * A waitqueue where a writer to clients/#/ctl destroying a client can | |
108 | * wait for cl_rpc_users to drop to 0 and then for the client to be | |
109 | * unhashed. | |
110 | */ | |
111 | static DECLARE_WAIT_QUEUE_HEAD(expiry_wq); | |
112 | ||
9258a2d5 | 113 | static struct kmem_cache *client_slab; |
abf1135b CH |
114 | static struct kmem_cache *openowner_slab; |
115 | static struct kmem_cache *lockowner_slab; | |
116 | static struct kmem_cache *file_slab; | |
117 | static struct kmem_cache *stateid_slab; | |
118 | static struct kmem_cache *deleg_slab; | |
8287f009 | 119 | static struct kmem_cache *odstate_slab; |
e60d4398 | 120 | |
66b2b9b2 | 121 | static void free_session(struct nfsd4_session *); |
508dc6e1 | 122 | |
c4cb8974 | 123 | static const struct nfsd4_callback_ops nfsd4_cb_recall_ops; |
76d348fa | 124 | static const struct nfsd4_callback_ops nfsd4_cb_notify_lock_ops; |
0162ac2b | 125 | |
f0f51f5c | 126 | static bool is_session_dead(struct nfsd4_session *ses) |
66b2b9b2 | 127 | { |
f0f51f5c | 128 | return ses->se_flags & NFS4_SESSION_DEAD; |
66b2b9b2 BF |
129 | } |
130 | ||
f0f51f5c | 131 | static __be32 mark_session_dead_locked(struct nfsd4_session *ses, int ref_held_by_me) |
508dc6e1 | 132 | { |
f0f51f5c | 133 | if (atomic_read(&ses->se_ref) > ref_held_by_me) |
66b2b9b2 BF |
134 | return nfserr_jukebox; |
135 | ses->se_flags |= NFS4_SESSION_DEAD; | |
136 | return nfs_ok; | |
508dc6e1 BH |
137 | } |
138 | ||
221a6876 BF |
139 | static bool is_client_expired(struct nfs4_client *clp) |
140 | { | |
141 | return clp->cl_time == 0; | |
142 | } | |
143 | ||
221a6876 BF |
144 | static __be32 get_client_locked(struct nfs4_client *clp) |
145 | { | |
0a880a28 TM |
146 | struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id); |
147 | ||
148 | lockdep_assert_held(&nn->client_lock); | |
149 | ||
221a6876 BF |
150 | if (is_client_expired(clp)) |
151 | return nfserr_expired; | |
14ed14cc | 152 | atomic_inc(&clp->cl_rpc_users); |
221a6876 BF |
153 | return nfs_ok; |
154 | } | |
155 | ||
156 | /* must be called under the client_lock */ | |
157 | static inline void | |
158 | renew_client_locked(struct nfs4_client *clp) | |
159 | { | |
160 | struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id); | |
161 | ||
162 | if (is_client_expired(clp)) { | |
163 | WARN_ON(1); | |
164 | printk("%s: client (clientid %08x/%08x) already expired\n", | |
165 | __func__, | |
166 | clp->cl_clientid.cl_boot, | |
167 | clp->cl_clientid.cl_id); | |
168 | return; | |
169 | } | |
170 | ||
221a6876 | 171 | list_move_tail(&clp->cl_lru, &nn->client_lru); |
20b7d86f | 172 | clp->cl_time = ktime_get_boottime_seconds(); |
221a6876 BF |
173 | } |
174 | ||
ba138435 | 175 | static void put_client_renew_locked(struct nfs4_client *clp) |
221a6876 | 176 | { |
0a880a28 TM |
177 | struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id); |
178 | ||
179 | lockdep_assert_held(&nn->client_lock); | |
180 | ||
14ed14cc | 181 | if (!atomic_dec_and_test(&clp->cl_rpc_users)) |
221a6876 BF |
182 | return; |
183 | if (!is_client_expired(clp)) | |
184 | renew_client_locked(clp); | |
89c905be BF |
185 | else |
186 | wake_up_all(&expiry_wq); | |
221a6876 BF |
187 | } |
188 | ||
4b24ca7d JL |
189 | static void put_client_renew(struct nfs4_client *clp) |
190 | { | |
191 | struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id); | |
192 | ||
14ed14cc | 193 | if (!atomic_dec_and_lock(&clp->cl_rpc_users, &nn->client_lock)) |
d6c249b4 JL |
194 | return; |
195 | if (!is_client_expired(clp)) | |
196 | renew_client_locked(clp); | |
89c905be BF |
197 | else |
198 | wake_up_all(&expiry_wq); | |
4b24ca7d JL |
199 | spin_unlock(&nn->client_lock); |
200 | } | |
201 | ||
d4e19e70 TM |
202 | static __be32 nfsd4_get_session_locked(struct nfsd4_session *ses) |
203 | { | |
204 | __be32 status; | |
205 | ||
206 | if (is_session_dead(ses)) | |
207 | return nfserr_badsession; | |
208 | status = get_client_locked(ses->se_client); | |
209 | if (status) | |
210 | return status; | |
211 | atomic_inc(&ses->se_ref); | |
212 | return nfs_ok; | |
213 | } | |
214 | ||
215 | static void nfsd4_put_session_locked(struct nfsd4_session *ses) | |
216 | { | |
217 | struct nfs4_client *clp = ses->se_client; | |
0a880a28 TM |
218 | struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id); |
219 | ||
220 | lockdep_assert_held(&nn->client_lock); | |
d4e19e70 TM |
221 | |
222 | if (atomic_dec_and_test(&ses->se_ref) && is_session_dead(ses)) | |
223 | free_session(ses); | |
224 | put_client_renew_locked(clp); | |
225 | } | |
226 | ||
227 | static void nfsd4_put_session(struct nfsd4_session *ses) | |
228 | { | |
229 | struct nfs4_client *clp = ses->se_client; | |
230 | struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id); | |
231 | ||
232 | spin_lock(&nn->client_lock); | |
233 | nfsd4_put_session_locked(ses); | |
234 | spin_unlock(&nn->client_lock); | |
235 | } | |
236 | ||
76d348fa JL |
237 | static struct nfsd4_blocked_lock * |
238 | find_blocked_lock(struct nfs4_lockowner *lo, struct knfsd_fh *fh, | |
239 | struct nfsd_net *nn) | |
240 | { | |
241 | struct nfsd4_blocked_lock *cur, *found = NULL; | |
242 | ||
0cc11a61 | 243 | spin_lock(&nn->blocked_locks_lock); |
76d348fa JL |
244 | list_for_each_entry(cur, &lo->lo_blocked, nbl_list) { |
245 | if (fh_match(fh, &cur->nbl_fh)) { | |
246 | list_del_init(&cur->nbl_list); | |
7919d0a2 | 247 | list_del_init(&cur->nbl_lru); |
76d348fa JL |
248 | found = cur; |
249 | break; | |
250 | } | |
251 | } | |
0cc11a61 | 252 | spin_unlock(&nn->blocked_locks_lock); |
76d348fa | 253 | if (found) |
cb03f94f | 254 | locks_delete_block(&found->nbl_lock); |
76d348fa JL |
255 | return found; |
256 | } | |
257 | ||
258 | static struct nfsd4_blocked_lock * | |
259 | find_or_allocate_block(struct nfs4_lockowner *lo, struct knfsd_fh *fh, | |
260 | struct nfsd_net *nn) | |
261 | { | |
262 | struct nfsd4_blocked_lock *nbl; | |
263 | ||
264 | nbl = find_blocked_lock(lo, fh, nn); | |
265 | if (!nbl) { | |
266 | nbl= kmalloc(sizeof(*nbl), GFP_KERNEL); | |
267 | if (nbl) { | |
e1e8399e VA |
268 | INIT_LIST_HEAD(&nbl->nbl_list); |
269 | INIT_LIST_HEAD(&nbl->nbl_lru); | |
76d348fa JL |
270 | fh_copy_shallow(&nbl->nbl_fh, fh); |
271 | locks_init_lock(&nbl->nbl_lock); | |
272 | nfsd4_init_cb(&nbl->nbl_cb, lo->lo_owner.so_client, | |
273 | &nfsd4_cb_notify_lock_ops, | |
274 | NFSPROC4_CLNT_CB_NOTIFY_LOCK); | |
275 | } | |
276 | } | |
277 | return nbl; | |
278 | } | |
279 | ||
280 | static void | |
281 | free_blocked_lock(struct nfsd4_blocked_lock *nbl) | |
282 | { | |
6aaafc43 | 283 | locks_delete_block(&nbl->nbl_lock); |
76d348fa JL |
284 | locks_release_private(&nbl->nbl_lock); |
285 | kfree(nbl); | |
286 | } | |
287 | ||
68ef3bc3 JL |
288 | static void |
289 | remove_blocked_locks(struct nfs4_lockowner *lo) | |
290 | { | |
291 | struct nfs4_client *clp = lo->lo_owner.so_client; | |
292 | struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id); | |
293 | struct nfsd4_blocked_lock *nbl; | |
294 | LIST_HEAD(reaplist); | |
295 | ||
296 | /* Dequeue all blocked locks */ | |
297 | spin_lock(&nn->blocked_locks_lock); | |
298 | while (!list_empty(&lo->lo_blocked)) { | |
299 | nbl = list_first_entry(&lo->lo_blocked, | |
300 | struct nfsd4_blocked_lock, | |
301 | nbl_list); | |
302 | list_del_init(&nbl->nbl_list); | |
303 | list_move(&nbl->nbl_lru, &reaplist); | |
304 | } | |
305 | spin_unlock(&nn->blocked_locks_lock); | |
306 | ||
307 | /* Now free them */ | |
308 | while (!list_empty(&reaplist)) { | |
309 | nbl = list_first_entry(&reaplist, struct nfsd4_blocked_lock, | |
310 | nbl_lru); | |
311 | list_del_init(&nbl->nbl_lru); | |
68ef3bc3 JL |
312 | free_blocked_lock(nbl); |
313 | } | |
314 | } | |
315 | ||
f456458e JL |
316 | static void |
317 | nfsd4_cb_notify_lock_prepare(struct nfsd4_callback *cb) | |
318 | { | |
319 | struct nfsd4_blocked_lock *nbl = container_of(cb, | |
320 | struct nfsd4_blocked_lock, nbl_cb); | |
321 | locks_delete_block(&nbl->nbl_lock); | |
322 | } | |
323 | ||
76d348fa JL |
324 | static int |
325 | nfsd4_cb_notify_lock_done(struct nfsd4_callback *cb, struct rpc_task *task) | |
326 | { | |
327 | /* | |
328 | * Since this is just an optimization, we don't try very hard if it | |
329 | * turns out not to succeed. We'll requeue it on NFS4ERR_DELAY, and | |
330 | * just quit trying on anything else. | |
331 | */ | |
332 | switch (task->tk_status) { | |
333 | case -NFS4ERR_DELAY: | |
334 | rpc_delay(task, 1 * HZ); | |
335 | return 0; | |
336 | default: | |
337 | return 1; | |
338 | } | |
339 | } | |
340 | ||
341 | static void | |
342 | nfsd4_cb_notify_lock_release(struct nfsd4_callback *cb) | |
343 | { | |
344 | struct nfsd4_blocked_lock *nbl = container_of(cb, | |
345 | struct nfsd4_blocked_lock, nbl_cb); | |
346 | ||
347 | free_blocked_lock(nbl); | |
348 | } | |
349 | ||
350 | static const struct nfsd4_callback_ops nfsd4_cb_notify_lock_ops = { | |
f456458e | 351 | .prepare = nfsd4_cb_notify_lock_prepare, |
76d348fa JL |
352 | .done = nfsd4_cb_notify_lock_done, |
353 | .release = nfsd4_cb_notify_lock_release, | |
354 | }; | |
355 | ||
b5971afa KM |
356 | static inline struct nfs4_stateowner * |
357 | nfs4_get_stateowner(struct nfs4_stateowner *sop) | |
358 | { | |
359 | atomic_inc(&sop->so_count); | |
360 | return sop; | |
361 | } | |
362 | ||
7ffb5880 | 363 | static int |
d4f0489f | 364 | same_owner_str(struct nfs4_stateowner *sop, struct xdr_netobj *owner) |
7ffb5880 TM |
365 | { |
366 | return (sop->so_owner.len == owner->len) && | |
d4f0489f | 367 | 0 == memcmp(sop->so_owner.data, owner->data, owner->len); |
7ffb5880 TM |
368 | } |
369 | ||
370 | static struct nfs4_openowner * | |
371 | find_openstateowner_str_locked(unsigned int hashval, struct nfsd4_open *open, | |
d4f0489f | 372 | struct nfs4_client *clp) |
7ffb5880 TM |
373 | { |
374 | struct nfs4_stateowner *so; | |
7ffb5880 | 375 | |
d4f0489f | 376 | lockdep_assert_held(&clp->cl_lock); |
7ffb5880 | 377 | |
d4f0489f TM |
378 | list_for_each_entry(so, &clp->cl_ownerstr_hashtbl[hashval], |
379 | so_strhash) { | |
7ffb5880 TM |
380 | if (!so->so_is_open_owner) |
381 | continue; | |
b5971afa KM |
382 | if (same_owner_str(so, &open->op_owner)) |
383 | return openowner(nfs4_get_stateowner(so)); | |
7ffb5880 TM |
384 | } |
385 | return NULL; | |
386 | } | |
387 | ||
388 | static struct nfs4_openowner * | |
389 | find_openstateowner_str(unsigned int hashval, struct nfsd4_open *open, | |
d4f0489f | 390 | struct nfs4_client *clp) |
7ffb5880 TM |
391 | { |
392 | struct nfs4_openowner *oo; | |
393 | ||
d4f0489f TM |
394 | spin_lock(&clp->cl_lock); |
395 | oo = find_openstateowner_str_locked(hashval, open, clp); | |
396 | spin_unlock(&clp->cl_lock); | |
7ffb5880 TM |
397 | return oo; |
398 | } | |
399 | ||
1da177e4 LT |
400 | static inline u32 |
401 | opaque_hashval(const void *ptr, int nbytes) | |
402 | { | |
403 | unsigned char *cptr = (unsigned char *) ptr; | |
404 | ||
405 | u32 x = 0; | |
406 | while (nbytes--) { | |
407 | x *= 37; | |
408 | x += *cptr++; | |
409 | } | |
410 | return x; | |
411 | } | |
412 | ||
5b095e99 | 413 | static void nfsd4_free_file_rcu(struct rcu_head *rcu) |
32513b40 | 414 | { |
5b095e99 JL |
415 | struct nfs4_file *fp = container_of(rcu, struct nfs4_file, fi_rcu); |
416 | ||
417 | kmem_cache_free(file_slab, fp); | |
32513b40 BF |
418 | } |
419 | ||
e6ba76e1 | 420 | void |
13cd2184 N |
421 | put_nfs4_file(struct nfs4_file *fi) |
422 | { | |
02e1215f JL |
423 | might_lock(&state_lock); |
424 | ||
818a34eb | 425 | if (refcount_dec_and_lock(&fi->fi_ref, &state_lock)) { |
5b095e99 | 426 | hlist_del_rcu(&fi->fi_hash); |
cdc97505 | 427 | spin_unlock(&state_lock); |
8287f009 | 428 | WARN_ON_ONCE(!list_empty(&fi->fi_clnt_odstate)); |
5b095e99 JL |
429 | WARN_ON_ONCE(!list_empty(&fi->fi_delegations)); |
430 | call_rcu(&fi->fi_rcu, nfsd4_free_file_rcu); | |
8b671b80 | 431 | } |
13cd2184 N |
432 | } |
433 | ||
eb82dd39 | 434 | static struct nfsd_file * |
de18643d TM |
435 | __nfs4_get_fd(struct nfs4_file *f, int oflag) |
436 | { | |
437 | if (f->fi_fds[oflag]) | |
eb82dd39 | 438 | return nfsd_file_get(f->fi_fds[oflag]); |
de18643d TM |
439 | return NULL; |
440 | } | |
441 | ||
eb82dd39 | 442 | static struct nfsd_file * |
de18643d TM |
443 | find_writeable_file_locked(struct nfs4_file *f) |
444 | { | |
eb82dd39 | 445 | struct nfsd_file *ret; |
de18643d TM |
446 | |
447 | lockdep_assert_held(&f->fi_lock); | |
448 | ||
449 | ret = __nfs4_get_fd(f, O_WRONLY); | |
450 | if (!ret) | |
451 | ret = __nfs4_get_fd(f, O_RDWR); | |
452 | return ret; | |
453 | } | |
454 | ||
eb82dd39 | 455 | static struct nfsd_file * |
de18643d TM |
456 | find_writeable_file(struct nfs4_file *f) |
457 | { | |
eb82dd39 | 458 | struct nfsd_file *ret; |
de18643d TM |
459 | |
460 | spin_lock(&f->fi_lock); | |
461 | ret = find_writeable_file_locked(f); | |
462 | spin_unlock(&f->fi_lock); | |
463 | ||
464 | return ret; | |
465 | } | |
466 | ||
eb82dd39 JL |
467 | static struct nfsd_file * |
468 | find_readable_file_locked(struct nfs4_file *f) | |
de18643d | 469 | { |
eb82dd39 | 470 | struct nfsd_file *ret; |
de18643d TM |
471 | |
472 | lockdep_assert_held(&f->fi_lock); | |
473 | ||
474 | ret = __nfs4_get_fd(f, O_RDONLY); | |
475 | if (!ret) | |
476 | ret = __nfs4_get_fd(f, O_RDWR); | |
477 | return ret; | |
478 | } | |
479 | ||
eb82dd39 | 480 | static struct nfsd_file * |
de18643d TM |
481 | find_readable_file(struct nfs4_file *f) |
482 | { | |
eb82dd39 | 483 | struct nfsd_file *ret; |
de18643d TM |
484 | |
485 | spin_lock(&f->fi_lock); | |
486 | ret = find_readable_file_locked(f); | |
487 | spin_unlock(&f->fi_lock); | |
488 | ||
489 | return ret; | |
490 | } | |
491 | ||
eb82dd39 | 492 | struct nfsd_file * |
de18643d TM |
493 | find_any_file(struct nfs4_file *f) |
494 | { | |
eb82dd39 | 495 | struct nfsd_file *ret; |
de18643d | 496 | |
a451b123 TM |
497 | if (!f) |
498 | return NULL; | |
de18643d TM |
499 | spin_lock(&f->fi_lock); |
500 | ret = __nfs4_get_fd(f, O_RDWR); | |
501 | if (!ret) { | |
502 | ret = __nfs4_get_fd(f, O_WRONLY); | |
503 | if (!ret) | |
504 | ret = __nfs4_get_fd(f, O_RDONLY); | |
505 | } | |
506 | spin_unlock(&f->fi_lock); | |
507 | return ret; | |
508 | } | |
509 | ||
9affa435 BF |
510 | static struct nfsd_file *find_deleg_file(struct nfs4_file *f) |
511 | { | |
512 | struct nfsd_file *ret = NULL; | |
513 | ||
514 | spin_lock(&f->fi_lock); | |
515 | if (f->fi_deleg_file) | |
516 | ret = nfsd_file_get(f->fi_deleg_file); | |
517 | spin_unlock(&f->fi_lock); | |
518 | return ret; | |
519 | } | |
520 | ||
02a3508d | 521 | static atomic_long_t num_delegations; |
697ce9be | 522 | unsigned long max_delegations; |
ef0f3390 N |
523 | |
524 | /* | |
525 | * Open owner state (share locks) | |
526 | */ | |
527 | ||
16bfdaaf BF |
528 | /* hash tables for lock and open owners */ |
529 | #define OWNER_HASH_BITS 8 | |
530 | #define OWNER_HASH_SIZE (1 << OWNER_HASH_BITS) | |
531 | #define OWNER_HASH_MASK (OWNER_HASH_SIZE - 1) | |
ef0f3390 | 532 | |
d4f0489f | 533 | static unsigned int ownerstr_hashval(struct xdr_netobj *ownername) |
ddc04c41 BF |
534 | { |
535 | unsigned int ret; | |
536 | ||
537 | ret = opaque_hashval(ownername->data, ownername->len); | |
16bfdaaf | 538 | return ret & OWNER_HASH_MASK; |
ddc04c41 | 539 | } |
ef0f3390 | 540 | |
ef0f3390 N |
541 | /* hash table for nfs4_file */ |
542 | #define FILE_HASH_BITS 8 | |
543 | #define FILE_HASH_SIZE (1 << FILE_HASH_BITS) | |
35079582 | 544 | |
ca943217 | 545 | static unsigned int nfsd_fh_hashval(struct knfsd_fh *fh) |
ddc04c41 | 546 | { |
ca943217 TM |
547 | return jhash2(fh->fh_base.fh_pad, XDR_QUADLEN(fh->fh_size), 0); |
548 | } | |
549 | ||
550 | static unsigned int file_hashval(struct knfsd_fh *fh) | |
551 | { | |
552 | return nfsd_fh_hashval(fh) & (FILE_HASH_SIZE - 1); | |
553 | } | |
554 | ||
89876f8c | 555 | static struct hlist_head file_hashtbl[FILE_HASH_SIZE]; |
ef0f3390 | 556 | |
12659651 JL |
557 | static void |
558 | __nfs4_file_get_access(struct nfs4_file *fp, u32 access) | |
3477565e | 559 | { |
7214e860 JL |
560 | lockdep_assert_held(&fp->fi_lock); |
561 | ||
12659651 JL |
562 | if (access & NFS4_SHARE_ACCESS_WRITE) |
563 | atomic_inc(&fp->fi_access[O_WRONLY]); | |
564 | if (access & NFS4_SHARE_ACCESS_READ) | |
565 | atomic_inc(&fp->fi_access[O_RDONLY]); | |
3477565e BF |
566 | } |
567 | ||
12659651 JL |
568 | static __be32 |
569 | nfs4_file_get_access(struct nfs4_file *fp, u32 access) | |
998db52c | 570 | { |
7214e860 JL |
571 | lockdep_assert_held(&fp->fi_lock); |
572 | ||
12659651 JL |
573 | /* Does this access mode make sense? */ |
574 | if (access & ~NFS4_SHARE_ACCESS_BOTH) | |
575 | return nfserr_inval; | |
576 | ||
baeb4ff0 JL |
577 | /* Does it conflict with a deny mode already set? */ |
578 | if ((access & fp->fi_share_deny) != 0) | |
579 | return nfserr_share_denied; | |
580 | ||
12659651 JL |
581 | __nfs4_file_get_access(fp, access); |
582 | return nfs_ok; | |
998db52c BF |
583 | } |
584 | ||
baeb4ff0 JL |
585 | static __be32 nfs4_file_check_deny(struct nfs4_file *fp, u32 deny) |
586 | { | |
587 | /* Common case is that there is no deny mode. */ | |
588 | if (deny) { | |
589 | /* Does this deny mode make sense? */ | |
590 | if (deny & ~NFS4_SHARE_DENY_BOTH) | |
591 | return nfserr_inval; | |
592 | ||
593 | if ((deny & NFS4_SHARE_DENY_READ) && | |
594 | atomic_read(&fp->fi_access[O_RDONLY])) | |
595 | return nfserr_share_denied; | |
596 | ||
597 | if ((deny & NFS4_SHARE_DENY_WRITE) && | |
598 | atomic_read(&fp->fi_access[O_WRONLY])) | |
599 | return nfserr_share_denied; | |
600 | } | |
601 | return nfs_ok; | |
602 | } | |
603 | ||
998db52c | 604 | static void __nfs4_file_put_access(struct nfs4_file *fp, int oflag) |
f9d7562f | 605 | { |
de18643d TM |
606 | might_lock(&fp->fi_lock); |
607 | ||
608 | if (atomic_dec_and_lock(&fp->fi_access[oflag], &fp->fi_lock)) { | |
fd4f83fd JL |
609 | struct nfsd_file *f1 = NULL; |
610 | struct nfsd_file *f2 = NULL; | |
de18643d | 611 | |
6d338b51 | 612 | swap(f1, fp->fi_fds[oflag]); |
0c7c3e67 | 613 | if (atomic_read(&fp->fi_access[1 - oflag]) == 0) |
6d338b51 | 614 | swap(f2, fp->fi_fds[O_RDWR]); |
de18643d TM |
615 | spin_unlock(&fp->fi_lock); |
616 | if (f1) | |
fd4f83fd | 617 | nfsd_file_put(f1); |
de18643d | 618 | if (f2) |
fd4f83fd | 619 | nfsd_file_put(f2); |
f9d7562f BF |
620 | } |
621 | } | |
622 | ||
12659651 | 623 | static void nfs4_file_put_access(struct nfs4_file *fp, u32 access) |
998db52c | 624 | { |
12659651 JL |
625 | WARN_ON_ONCE(access & ~NFS4_SHARE_ACCESS_BOTH); |
626 | ||
627 | if (access & NFS4_SHARE_ACCESS_WRITE) | |
998db52c | 628 | __nfs4_file_put_access(fp, O_WRONLY); |
12659651 JL |
629 | if (access & NFS4_SHARE_ACCESS_READ) |
630 | __nfs4_file_put_access(fp, O_RDONLY); | |
998db52c BF |
631 | } |
632 | ||
8287f009 SB |
633 | /* |
634 | * Allocate a new open/delegation state counter. This is needed for | |
635 | * pNFS for proper return on close semantics. | |
636 | * | |
637 | * Note that we only allocate it for pNFS-enabled exports, otherwise | |
638 | * all pointers to struct nfs4_clnt_odstate are always NULL. | |
639 | */ | |
640 | static struct nfs4_clnt_odstate * | |
641 | alloc_clnt_odstate(struct nfs4_client *clp) | |
642 | { | |
643 | struct nfs4_clnt_odstate *co; | |
644 | ||
645 | co = kmem_cache_zalloc(odstate_slab, GFP_KERNEL); | |
646 | if (co) { | |
647 | co->co_client = clp; | |
cff7cb2e | 648 | refcount_set(&co->co_odcount, 1); |
8287f009 SB |
649 | } |
650 | return co; | |
651 | } | |
652 | ||
653 | static void | |
654 | hash_clnt_odstate_locked(struct nfs4_clnt_odstate *co) | |
655 | { | |
656 | struct nfs4_file *fp = co->co_file; | |
657 | ||
658 | lockdep_assert_held(&fp->fi_lock); | |
659 | list_add(&co->co_perfile, &fp->fi_clnt_odstate); | |
660 | } | |
661 | ||
662 | static inline void | |
663 | get_clnt_odstate(struct nfs4_clnt_odstate *co) | |
664 | { | |
665 | if (co) | |
cff7cb2e | 666 | refcount_inc(&co->co_odcount); |
8287f009 SB |
667 | } |
668 | ||
669 | static void | |
670 | put_clnt_odstate(struct nfs4_clnt_odstate *co) | |
671 | { | |
672 | struct nfs4_file *fp; | |
673 | ||
674 | if (!co) | |
675 | return; | |
676 | ||
677 | fp = co->co_file; | |
cff7cb2e | 678 | if (refcount_dec_and_lock(&co->co_odcount, &fp->fi_lock)) { |
8287f009 SB |
679 | list_del(&co->co_perfile); |
680 | spin_unlock(&fp->fi_lock); | |
681 | ||
682 | nfsd4_return_all_file_layouts(co->co_client, fp); | |
683 | kmem_cache_free(odstate_slab, co); | |
684 | } | |
685 | } | |
686 | ||
687 | static struct nfs4_clnt_odstate * | |
688 | find_or_hash_clnt_odstate(struct nfs4_file *fp, struct nfs4_clnt_odstate *new) | |
689 | { | |
690 | struct nfs4_clnt_odstate *co; | |
691 | struct nfs4_client *cl; | |
692 | ||
693 | if (!new) | |
694 | return NULL; | |
695 | ||
696 | cl = new->co_client; | |
697 | ||
698 | spin_lock(&fp->fi_lock); | |
699 | list_for_each_entry(co, &fp->fi_clnt_odstate, co_perfile) { | |
700 | if (co->co_client == cl) { | |
701 | get_clnt_odstate(co); | |
702 | goto out; | |
703 | } | |
704 | } | |
705 | co = new; | |
706 | co->co_file = fp; | |
707 | hash_clnt_odstate_locked(new); | |
708 | out: | |
709 | spin_unlock(&fp->fi_lock); | |
710 | return co; | |
711 | } | |
712 | ||
d19fb70d KM |
713 | struct nfs4_stid *nfs4_alloc_stid(struct nfs4_client *cl, struct kmem_cache *slab, |
714 | void (*sc_free)(struct nfs4_stid *)) | |
2a74aba7 | 715 | { |
3abdb607 | 716 | struct nfs4_stid *stid; |
6136d2b4 | 717 | int new_id; |
2a74aba7 | 718 | |
f8338834 | 719 | stid = kmem_cache_zalloc(slab, GFP_KERNEL); |
3abdb607 BF |
720 | if (!stid) |
721 | return NULL; | |
722 | ||
4770d722 JL |
723 | idr_preload(GFP_KERNEL); |
724 | spin_lock(&cl->cl_lock); | |
78599c42 BF |
725 | /* Reserving 0 for start of file in nfsdfs "states" file: */ |
726 | new_id = idr_alloc_cyclic(&cl->cl_stateids, stid, 1, 0, GFP_NOWAIT); | |
4770d722 JL |
727 | spin_unlock(&cl->cl_lock); |
728 | idr_preload_end(); | |
ebd6c707 | 729 | if (new_id < 0) |
3abdb607 | 730 | goto out_free; |
d19fb70d KM |
731 | |
732 | stid->sc_free = sc_free; | |
2a74aba7 | 733 | stid->sc_client = cl; |
3abdb607 BF |
734 | stid->sc_stateid.si_opaque.so_id = new_id; |
735 | stid->sc_stateid.si_opaque.so_clid = cl->cl_clientid; | |
2a74aba7 | 736 | /* Will be incremented before return to client: */ |
a15dfcd5 | 737 | refcount_set(&stid->sc_count, 1); |
9767feb2 | 738 | spin_lock_init(&stid->sc_lock); |
624322f1 | 739 | INIT_LIST_HEAD(&stid->sc_cp_list); |
996e0938 | 740 | |
996e0938 | 741 | /* |
3abdb607 BF |
742 | * It shouldn't be a problem to reuse an opaque stateid value. |
743 | * I don't think it is for 4.1. But with 4.0 I worry that, for | |
744 | * example, a stray write retransmission could be accepted by | |
745 | * the server when it should have been rejected. Therefore, | |
746 | * adopt a trick from the sctp code to attempt to maximize the | |
747 | * amount of time until an id is reused, by ensuring they always | |
748 | * "increase" (mod INT_MAX): | |
996e0938 | 749 | */ |
3abdb607 BF |
750 | return stid; |
751 | out_free: | |
2c44a234 | 752 | kmem_cache_free(slab, stid); |
3abdb607 | 753 | return NULL; |
2a74aba7 BF |
754 | } |
755 | ||
e0639dc5 OK |
756 | /* |
757 | * Create a unique stateid_t to represent each COPY. | |
758 | */ | |
624322f1 OK |
759 | static int nfs4_init_cp_state(struct nfsd_net *nn, copy_stateid_t *stid, |
760 | unsigned char sc_type) | |
e0639dc5 OK |
761 | { |
762 | int new_id; | |
763 | ||
9cc76801 | 764 | stid->stid.si_opaque.so_clid.cl_boot = (u32)nn->boot_time; |
624322f1 OK |
765 | stid->stid.si_opaque.so_clid.cl_id = nn->s2s_cp_cl_id; |
766 | stid->sc_type = sc_type; | |
767 | ||
e0639dc5 OK |
768 | idr_preload(GFP_KERNEL); |
769 | spin_lock(&nn->s2s_cp_lock); | |
624322f1 OK |
770 | new_id = idr_alloc_cyclic(&nn->s2s_cp_stateids, stid, 0, 0, GFP_NOWAIT); |
771 | stid->stid.si_opaque.so_id = new_id; | |
e0639dc5 OK |
772 | spin_unlock(&nn->s2s_cp_lock); |
773 | idr_preload_end(); | |
774 | if (new_id < 0) | |
775 | return 0; | |
e0639dc5 OK |
776 | return 1; |
777 | } | |
778 | ||
624322f1 OK |
779 | int nfs4_init_copy_state(struct nfsd_net *nn, struct nfsd4_copy *copy) |
780 | { | |
781 | return nfs4_init_cp_state(nn, ©->cp_stateid, NFS4_COPY_STID); | |
782 | } | |
783 | ||
784 | struct nfs4_cpntf_state *nfs4_alloc_init_cpntf_state(struct nfsd_net *nn, | |
785 | struct nfs4_stid *p_stid) | |
786 | { | |
787 | struct nfs4_cpntf_state *cps; | |
788 | ||
789 | cps = kzalloc(sizeof(struct nfs4_cpntf_state), GFP_KERNEL); | |
790 | if (!cps) | |
791 | return NULL; | |
20b7d86f | 792 | cps->cpntf_time = ktime_get_boottime_seconds(); |
624322f1 OK |
793 | refcount_set(&cps->cp_stateid.sc_count, 1); |
794 | if (!nfs4_init_cp_state(nn, &cps->cp_stateid, NFS4_COPYNOTIFY_STID)) | |
795 | goto out_free; | |
796 | spin_lock(&nn->s2s_cp_lock); | |
797 | list_add(&cps->cp_list, &p_stid->sc_cp_list); | |
798 | spin_unlock(&nn->s2s_cp_lock); | |
799 | return cps; | |
800 | out_free: | |
801 | kfree(cps); | |
802 | return NULL; | |
803 | } | |
804 | ||
805 | void nfs4_free_copy_state(struct nfsd4_copy *copy) | |
e0639dc5 OK |
806 | { |
807 | struct nfsd_net *nn; | |
808 | ||
624322f1 | 809 | WARN_ON_ONCE(copy->cp_stateid.sc_type != NFS4_COPY_STID); |
e0639dc5 OK |
810 | nn = net_generic(copy->cp_clp->net, nfsd_net_id); |
811 | spin_lock(&nn->s2s_cp_lock); | |
624322f1 OK |
812 | idr_remove(&nn->s2s_cp_stateids, |
813 | copy->cp_stateid.stid.si_opaque.so_id); | |
814 | spin_unlock(&nn->s2s_cp_lock); | |
815 | } | |
816 | ||
817 | static void nfs4_free_cpntf_statelist(struct net *net, struct nfs4_stid *stid) | |
818 | { | |
819 | struct nfs4_cpntf_state *cps; | |
820 | struct nfsd_net *nn; | |
821 | ||
822 | nn = net_generic(net, nfsd_net_id); | |
823 | spin_lock(&nn->s2s_cp_lock); | |
824 | while (!list_empty(&stid->sc_cp_list)) { | |
825 | cps = list_first_entry(&stid->sc_cp_list, | |
826 | struct nfs4_cpntf_state, cp_list); | |
827 | _free_cpntf_state_locked(nn, cps); | |
828 | } | |
e0639dc5 OK |
829 | spin_unlock(&nn->s2s_cp_lock); |
830 | } | |
831 | ||
b49e084d | 832 | static struct nfs4_ol_stateid * nfs4_alloc_open_stateid(struct nfs4_client *clp) |
4cdc951b | 833 | { |
6011695d | 834 | struct nfs4_stid *stid; |
6011695d | 835 | |
d19fb70d | 836 | stid = nfs4_alloc_stid(clp, stateid_slab, nfs4_free_ol_stateid); |
6011695d TM |
837 | if (!stid) |
838 | return NULL; | |
839 | ||
d19fb70d | 840 | return openlockstateid(stid); |
6011695d TM |
841 | } |
842 | ||
843 | static void nfs4_free_deleg(struct nfs4_stid *stid) | |
844 | { | |
6011695d TM |
845 | kmem_cache_free(deleg_slab, stid); |
846 | atomic_long_dec(&num_delegations); | |
4cdc951b BF |
847 | } |
848 | ||
6282cd56 N |
849 | /* |
850 | * When we recall a delegation, we should be careful not to hand it | |
851 | * out again straight away. | |
852 | * To ensure this we keep a pair of bloom filters ('new' and 'old') | |
853 | * in which the filehandles of recalled delegations are "stored". | |
854 | * If a filehandle appear in either filter, a delegation is blocked. | |
855 | * When a delegation is recalled, the filehandle is stored in the "new" | |
856 | * filter. | |
857 | * Every 30 seconds we swap the filters and clear the "new" one, | |
858 | * unless both are empty of course. | |
859 | * | |
860 | * Each filter is 256 bits. We hash the filehandle to 32bit and use the | |
861 | * low 3 bytes as hash-table indices. | |
862 | * | |
f54fe962 | 863 | * 'blocked_delegations_lock', which is always taken in block_delegations(), |
6282cd56 N |
864 | * is used to manage concurrent access. Testing does not need the lock |
865 | * except when swapping the two filters. | |
866 | */ | |
f54fe962 | 867 | static DEFINE_SPINLOCK(blocked_delegations_lock); |
6282cd56 N |
868 | static struct bloom_pair { |
869 | int entries, old_entries; | |
b3f255ef | 870 | time64_t swap_time; |
6282cd56 N |
871 | int new; /* index into 'set' */ |
872 | DECLARE_BITMAP(set[2], 256); | |
873 | } blocked_delegations; | |
874 | ||
875 | static int delegation_blocked(struct knfsd_fh *fh) | |
876 | { | |
877 | u32 hash; | |
878 | struct bloom_pair *bd = &blocked_delegations; | |
879 | ||
880 | if (bd->entries == 0) | |
881 | return 0; | |
b3f255ef | 882 | if (ktime_get_seconds() - bd->swap_time > 30) { |
f54fe962 | 883 | spin_lock(&blocked_delegations_lock); |
b3f255ef | 884 | if (ktime_get_seconds() - bd->swap_time > 30) { |
6282cd56 N |
885 | bd->entries -= bd->old_entries; |
886 | bd->old_entries = bd->entries; | |
887 | memset(bd->set[bd->new], 0, | |
888 | sizeof(bd->set[0])); | |
889 | bd->new = 1-bd->new; | |
b3f255ef | 890 | bd->swap_time = ktime_get_seconds(); |
6282cd56 | 891 | } |
f54fe962 | 892 | spin_unlock(&blocked_delegations_lock); |
6282cd56 | 893 | } |
87545899 | 894 | hash = jhash(&fh->fh_base, fh->fh_size, 0); |
6282cd56 N |
895 | if (test_bit(hash&255, bd->set[0]) && |
896 | test_bit((hash>>8)&255, bd->set[0]) && | |
897 | test_bit((hash>>16)&255, bd->set[0])) | |
898 | return 1; | |
899 | ||
900 | if (test_bit(hash&255, bd->set[1]) && | |
901 | test_bit((hash>>8)&255, bd->set[1]) && | |
902 | test_bit((hash>>16)&255, bd->set[1])) | |
903 | return 1; | |
904 | ||
905 | return 0; | |
906 | } | |
907 | ||
908 | static void block_delegations(struct knfsd_fh *fh) | |
909 | { | |
910 | u32 hash; | |
911 | struct bloom_pair *bd = &blocked_delegations; | |
912 | ||
87545899 | 913 | hash = jhash(&fh->fh_base, fh->fh_size, 0); |
6282cd56 | 914 | |
f54fe962 | 915 | spin_lock(&blocked_delegations_lock); |
6282cd56 N |
916 | __set_bit(hash&255, bd->set[bd->new]); |
917 | __set_bit((hash>>8)&255, bd->set[bd->new]); | |
918 | __set_bit((hash>>16)&255, bd->set[bd->new]); | |
919 | if (bd->entries == 0) | |
b3f255ef | 920 | bd->swap_time = ktime_get_seconds(); |
6282cd56 | 921 | bd->entries += 1; |
f54fe962 | 922 | spin_unlock(&blocked_delegations_lock); |
6282cd56 N |
923 | } |
924 | ||
1da177e4 | 925 | static struct nfs4_delegation * |
86d29b10 BF |
926 | alloc_init_deleg(struct nfs4_client *clp, struct nfs4_file *fp, |
927 | struct svc_fh *current_fh, | |
8287f009 | 928 | struct nfs4_clnt_odstate *odstate) |
1da177e4 LT |
929 | { |
930 | struct nfs4_delegation *dp; | |
02a3508d | 931 | long n; |
1da177e4 LT |
932 | |
933 | dprintk("NFSD alloc_init_deleg\n"); | |
02a3508d TM |
934 | n = atomic_long_inc_return(&num_delegations); |
935 | if (n < 0 || n > max_delegations) | |
936 | goto out_dec; | |
6282cd56 | 937 | if (delegation_blocked(¤t_fh->fh_handle)) |
02a3508d | 938 | goto out_dec; |
d19fb70d | 939 | dp = delegstateid(nfs4_alloc_stid(clp, deleg_slab, nfs4_free_deleg)); |
5b2d21c1 | 940 | if (dp == NULL) |
02a3508d | 941 | goto out_dec; |
6011695d | 942 | |
2a74aba7 BF |
943 | /* |
944 | * delegation seqid's are never incremented. The 4.1 special | |
6136d2b4 BF |
945 | * meaning of seqid 0 isn't meaningful, really, but let's avoid |
946 | * 0 anyway just for consistency and use 1: | |
2a74aba7 BF |
947 | */ |
948 | dp->dl_stid.sc_stateid.si_generation = 1; | |
ea1da636 N |
949 | INIT_LIST_HEAD(&dp->dl_perfile); |
950 | INIT_LIST_HEAD(&dp->dl_perclnt); | |
1da177e4 | 951 | INIT_LIST_HEAD(&dp->dl_recall_lru); |
8287f009 SB |
952 | dp->dl_clnt_odstate = odstate; |
953 | get_clnt_odstate(odstate); | |
99c41515 | 954 | dp->dl_type = NFS4_OPEN_DELEGATE_READ; |
f0b5de1b CH |
955 | dp->dl_retries = 1; |
956 | nfsd4_init_cb(&dp->dl_recall, dp->dl_stid.sc_client, | |
0162ac2b | 957 | &nfsd4_cb_recall_ops, NFSPROC4_CLNT_CB_RECALL); |
86d29b10 BF |
958 | get_nfs4_file(fp); |
959 | dp->dl_stid.sc_file = fp; | |
1da177e4 | 960 | return dp; |
02a3508d TM |
961 | out_dec: |
962 | atomic_long_dec(&num_delegations); | |
963 | return NULL; | |
1da177e4 LT |
964 | } |
965 | ||
966 | void | |
6011695d | 967 | nfs4_put_stid(struct nfs4_stid *s) |
1da177e4 | 968 | { |
11b9164a | 969 | struct nfs4_file *fp = s->sc_file; |
6011695d TM |
970 | struct nfs4_client *clp = s->sc_client; |
971 | ||
4770d722 JL |
972 | might_lock(&clp->cl_lock); |
973 | ||
a15dfcd5 | 974 | if (!refcount_dec_and_lock(&s->sc_count, &clp->cl_lock)) { |
b401be22 | 975 | wake_up_all(&close_wq); |
6011695d | 976 | return; |
b401be22 | 977 | } |
6011695d | 978 | idr_remove(&clp->cl_stateids, s->sc_stateid.si_opaque.so_id); |
624322f1 | 979 | nfs4_free_cpntf_statelist(clp->net, s); |
4770d722 | 980 | spin_unlock(&clp->cl_lock); |
6011695d | 981 | s->sc_free(s); |
11b9164a TM |
982 | if (fp) |
983 | put_nfs4_file(fp); | |
1da177e4 LT |
984 | } |
985 | ||
9767feb2 JL |
986 | void |
987 | nfs4_inc_and_copy_stateid(stateid_t *dst, struct nfs4_stid *stid) | |
988 | { | |
989 | stateid_t *src = &stid->sc_stateid; | |
990 | ||
991 | spin_lock(&stid->sc_lock); | |
992 | if (unlikely(++src->si_generation == 0)) | |
993 | src->si_generation = 1; | |
994 | memcpy(dst, src, sizeof(*dst)); | |
995 | spin_unlock(&stid->sc_lock); | |
996 | } | |
997 | ||
353601e7 | 998 | static void put_deleg_file(struct nfs4_file *fp) |
1da177e4 | 999 | { |
eb82dd39 | 1000 | struct nfsd_file *nf = NULL; |
b8232d33 | 1001 | |
6bcc034e | 1002 | spin_lock(&fp->fi_lock); |
353601e7 | 1003 | if (--fp->fi_delegees == 0) |
eb82dd39 | 1004 | swap(nf, fp->fi_deleg_file); |
6bcc034e JL |
1005 | spin_unlock(&fp->fi_lock); |
1006 | ||
eb82dd39 JL |
1007 | if (nf) |
1008 | nfsd_file_put(nf); | |
353601e7 BF |
1009 | } |
1010 | ||
1011 | static void nfs4_unlock_deleg_lease(struct nfs4_delegation *dp) | |
1012 | { | |
1013 | struct nfs4_file *fp = dp->dl_stid.sc_file; | |
eb82dd39 | 1014 | struct nfsd_file *nf = fp->fi_deleg_file; |
353601e7 BF |
1015 | |
1016 | WARN_ON_ONCE(!fp->fi_delegees); | |
1017 | ||
eb82dd39 | 1018 | vfs_setlease(nf->nf_file, F_UNLCK, NULL, (void **)&dp); |
353601e7 | 1019 | put_deleg_file(fp); |
1da177e4 LT |
1020 | } |
1021 | ||
0af6e690 BF |
1022 | static void destroy_unhashed_deleg(struct nfs4_delegation *dp) |
1023 | { | |
1024 | put_clnt_odstate(dp->dl_clnt_odstate); | |
353601e7 | 1025 | nfs4_unlock_deleg_lease(dp); |
0af6e690 BF |
1026 | nfs4_put_stid(&dp->dl_stid); |
1027 | } | |
1028 | ||
cd61c522 | 1029 | void nfs4_unhash_stid(struct nfs4_stid *s) |
6136d2b4 | 1030 | { |
3abdb607 | 1031 | s->sc_type = 0; |
6136d2b4 BF |
1032 | } |
1033 | ||
34ed9872 | 1034 | /** |
68b18f52 | 1035 | * nfs4_delegation_exists - Discover if this delegation already exists |
34ed9872 AE |
1036 | * @clp: a pointer to the nfs4_client we're granting a delegation to |
1037 | * @fp: a pointer to the nfs4_file we're granting a delegation on | |
1038 | * | |
1039 | * Return: | |
68b18f52 | 1040 | * On success: true iff an existing delegation is found |
34ed9872 AE |
1041 | */ |
1042 | ||
68b18f52 BF |
1043 | static bool |
1044 | nfs4_delegation_exists(struct nfs4_client *clp, struct nfs4_file *fp) | |
34ed9872 AE |
1045 | { |
1046 | struct nfs4_delegation *searchdp = NULL; | |
1047 | struct nfs4_client *searchclp = NULL; | |
1048 | ||
1049 | lockdep_assert_held(&state_lock); | |
1050 | lockdep_assert_held(&fp->fi_lock); | |
1051 | ||
1052 | list_for_each_entry(searchdp, &fp->fi_delegations, dl_perfile) { | |
1053 | searchclp = searchdp->dl_stid.sc_client; | |
1054 | if (clp == searchclp) { | |
51d87bc2 | 1055 | return true; |
34ed9872 AE |
1056 | } |
1057 | } | |
51d87bc2 | 1058 | return false; |
34ed9872 AE |
1059 | } |
1060 | ||
1061 | /** | |
1062 | * hash_delegation_locked - Add a delegation to the appropriate lists | |
1063 | * @dp: a pointer to the nfs4_delegation we are adding. | |
1064 | * @fp: a pointer to the nfs4_file we're granting a delegation on | |
1065 | * | |
1066 | * Return: | |
1067 | * On success: NULL if the delegation was successfully hashed. | |
1068 | * | |
1069 | * On error: -EAGAIN if one was previously granted to this | |
1070 | * nfs4_client for this nfs4_file. Delegation is not hashed. | |
1071 | * | |
1072 | */ | |
1073 | ||
1074 | static int | |
931ee56c BH |
1075 | hash_delegation_locked(struct nfs4_delegation *dp, struct nfs4_file *fp) |
1076 | { | |
34ed9872 AE |
1077 | struct nfs4_client *clp = dp->dl_stid.sc_client; |
1078 | ||
cdc97505 | 1079 | lockdep_assert_held(&state_lock); |
417c6629 | 1080 | lockdep_assert_held(&fp->fi_lock); |
931ee56c | 1081 | |
68b18f52 BF |
1082 | if (nfs4_delegation_exists(clp, fp)) |
1083 | return -EAGAIN; | |
a15dfcd5 | 1084 | refcount_inc(&dp->dl_stid.sc_count); |
3fb87d13 | 1085 | dp->dl_stid.sc_type = NFS4_DELEG_STID; |
931ee56c | 1086 | list_add(&dp->dl_perfile, &fp->fi_delegations); |
34ed9872 AE |
1087 | list_add(&dp->dl_perclnt, &clp->cl_delegations); |
1088 | return 0; | |
931ee56c BH |
1089 | } |
1090 | ||
3fcbbd24 | 1091 | static bool |
42690676 | 1092 | unhash_delegation_locked(struct nfs4_delegation *dp) |
1da177e4 | 1093 | { |
11b9164a | 1094 | struct nfs4_file *fp = dp->dl_stid.sc_file; |
02e1215f | 1095 | |
42690676 JL |
1096 | lockdep_assert_held(&state_lock); |
1097 | ||
3fcbbd24 JL |
1098 | if (list_empty(&dp->dl_perfile)) |
1099 | return false; | |
1100 | ||
b0fc29d6 | 1101 | dp->dl_stid.sc_type = NFS4_CLOSED_DELEG_STID; |
d55a166c JL |
1102 | /* Ensure that deleg break won't try to requeue it */ |
1103 | ++dp->dl_time; | |
417c6629 | 1104 | spin_lock(&fp->fi_lock); |
931ee56c | 1105 | list_del_init(&dp->dl_perclnt); |
1da177e4 | 1106 | list_del_init(&dp->dl_recall_lru); |
02e1215f JL |
1107 | list_del_init(&dp->dl_perfile); |
1108 | spin_unlock(&fp->fi_lock); | |
3fcbbd24 | 1109 | return true; |
3bd64a5b BF |
1110 | } |
1111 | ||
3bd64a5b BF |
1112 | static void destroy_delegation(struct nfs4_delegation *dp) |
1113 | { | |
3fcbbd24 JL |
1114 | bool unhashed; |
1115 | ||
42690676 | 1116 | spin_lock(&state_lock); |
3fcbbd24 | 1117 | unhashed = unhash_delegation_locked(dp); |
42690676 | 1118 | spin_unlock(&state_lock); |
0af6e690 BF |
1119 | if (unhashed) |
1120 | destroy_unhashed_deleg(dp); | |
3bd64a5b BF |
1121 | } |
1122 | ||
1123 | static void revoke_delegation(struct nfs4_delegation *dp) | |
1124 | { | |
1125 | struct nfs4_client *clp = dp->dl_stid.sc_client; | |
1126 | ||
2d4a532d JL |
1127 | WARN_ON(!list_empty(&dp->dl_recall_lru)); |
1128 | ||
0af6e690 | 1129 | if (clp->cl_minorversion) { |
3bd64a5b | 1130 | dp->dl_stid.sc_type = NFS4_REVOKED_DELEG_STID; |
0af6e690 | 1131 | refcount_inc(&dp->dl_stid.sc_count); |
2d4a532d JL |
1132 | spin_lock(&clp->cl_lock); |
1133 | list_add(&dp->dl_recall_lru, &clp->cl_revoked); | |
1134 | spin_unlock(&clp->cl_lock); | |
3bd64a5b | 1135 | } |
0af6e690 | 1136 | destroy_unhashed_deleg(dp); |
3bd64a5b BF |
1137 | } |
1138 | ||
1da177e4 LT |
1139 | /* |
1140 | * SETCLIENTID state | |
1141 | */ | |
1142 | ||
ddc04c41 BF |
1143 | static unsigned int clientid_hashval(u32 id) |
1144 | { | |
1145 | return id & CLIENT_HASH_MASK; | |
1146 | } | |
1147 | ||
6b189105 | 1148 | static unsigned int clientstr_hashval(struct xdr_netobj name) |
ddc04c41 | 1149 | { |
6b189105 | 1150 | return opaque_hashval(name.data, 8) & CLIENT_HASH_MASK; |
ddc04c41 BF |
1151 | } |
1152 | ||
f9d7562f BF |
1153 | /* |
1154 | * We store the NONE, READ, WRITE, and BOTH bits separately in the | |
1155 | * st_{access,deny}_bmap field of the stateid, in order to track not | |
1156 | * only what share bits are currently in force, but also what | |
1157 | * combinations of share bits previous opens have used. This allows us | |
1158 | * to enforce the recommendation of rfc 3530 14.2.19 that the server | |
1159 | * return an error if the client attempt to downgrade to a combination | |
1160 | * of share bits not explicable by closing some of its previous opens. | |
1161 | * | |
1162 | * XXX: This enforcement is actually incomplete, since we don't keep | |
1163 | * track of access/deny bit combinations; so, e.g., we allow: | |
1164 | * | |
1165 | * OPEN allow read, deny write | |
1166 | * OPEN allow both, deny none | |
1167 | * DOWNGRADE allow read, deny none | |
1168 | * | |
1169 | * which we should reject. | |
1170 | */ | |
5ae037e5 JL |
1171 | static unsigned int |
1172 | bmap_to_share_mode(unsigned long bmap) { | |
f9d7562f | 1173 | int i; |
5ae037e5 | 1174 | unsigned int access = 0; |
f9d7562f | 1175 | |
f9d7562f BF |
1176 | for (i = 1; i < 4; i++) { |
1177 | if (test_bit(i, &bmap)) | |
5ae037e5 | 1178 | access |= i; |
f9d7562f | 1179 | } |
5ae037e5 | 1180 | return access; |
f9d7562f BF |
1181 | } |
1182 | ||
82c5ff1b JL |
1183 | /* set share access for a given stateid */ |
1184 | static inline void | |
1185 | set_access(u32 access, struct nfs4_ol_stateid *stp) | |
1186 | { | |
c11c591f JL |
1187 | unsigned char mask = 1 << access; |
1188 | ||
1189 | WARN_ON_ONCE(access > NFS4_SHARE_ACCESS_BOTH); | |
1190 | stp->st_access_bmap |= mask; | |
82c5ff1b JL |
1191 | } |
1192 | ||
1193 | /* clear share access for a given stateid */ | |
1194 | static inline void | |
1195 | clear_access(u32 access, struct nfs4_ol_stateid *stp) | |
1196 | { | |
c11c591f JL |
1197 | unsigned char mask = 1 << access; |
1198 | ||
1199 | WARN_ON_ONCE(access > NFS4_SHARE_ACCESS_BOTH); | |
1200 | stp->st_access_bmap &= ~mask; | |
82c5ff1b JL |
1201 | } |
1202 | ||
1203 | /* test whether a given stateid has access */ | |
1204 | static inline bool | |
1205 | test_access(u32 access, struct nfs4_ol_stateid *stp) | |
1206 | { | |
c11c591f JL |
1207 | unsigned char mask = 1 << access; |
1208 | ||
1209 | return (bool)(stp->st_access_bmap & mask); | |
82c5ff1b JL |
1210 | } |
1211 | ||
ce0fc43c JL |
1212 | /* set share deny for a given stateid */ |
1213 | static inline void | |
c11c591f | 1214 | set_deny(u32 deny, struct nfs4_ol_stateid *stp) |
ce0fc43c | 1215 | { |
c11c591f JL |
1216 | unsigned char mask = 1 << deny; |
1217 | ||
1218 | WARN_ON_ONCE(deny > NFS4_SHARE_DENY_BOTH); | |
1219 | stp->st_deny_bmap |= mask; | |
ce0fc43c JL |
1220 | } |
1221 | ||
1222 | /* clear share deny for a given stateid */ | |
1223 | static inline void | |
c11c591f | 1224 | clear_deny(u32 deny, struct nfs4_ol_stateid *stp) |
ce0fc43c | 1225 | { |
c11c591f JL |
1226 | unsigned char mask = 1 << deny; |
1227 | ||
1228 | WARN_ON_ONCE(deny > NFS4_SHARE_DENY_BOTH); | |
1229 | stp->st_deny_bmap &= ~mask; | |
ce0fc43c JL |
1230 | } |
1231 | ||
1232 | /* test whether a given stateid is denying specific access */ | |
1233 | static inline bool | |
c11c591f | 1234 | test_deny(u32 deny, struct nfs4_ol_stateid *stp) |
ce0fc43c | 1235 | { |
c11c591f JL |
1236 | unsigned char mask = 1 << deny; |
1237 | ||
1238 | return (bool)(stp->st_deny_bmap & mask); | |
f9d7562f BF |
1239 | } |
1240 | ||
1241 | static int nfs4_access_to_omode(u32 access) | |
1242 | { | |
8f34a430 | 1243 | switch (access & NFS4_SHARE_ACCESS_BOTH) { |
f9d7562f BF |
1244 | case NFS4_SHARE_ACCESS_READ: |
1245 | return O_RDONLY; | |
1246 | case NFS4_SHARE_ACCESS_WRITE: | |
1247 | return O_WRONLY; | |
1248 | case NFS4_SHARE_ACCESS_BOTH: | |
1249 | return O_RDWR; | |
1250 | } | |
063b0fb9 BF |
1251 | WARN_ON_ONCE(1); |
1252 | return O_RDONLY; | |
f9d7562f BF |
1253 | } |
1254 | ||
baeb4ff0 JL |
1255 | /* |
1256 | * A stateid that had a deny mode associated with it is being released | |
1257 | * or downgraded. Recalculate the deny mode on the file. | |
1258 | */ | |
1259 | static void | |
1260 | recalculate_deny_mode(struct nfs4_file *fp) | |
1261 | { | |
1262 | struct nfs4_ol_stateid *stp; | |
1263 | ||
1264 | spin_lock(&fp->fi_lock); | |
1265 | fp->fi_share_deny = 0; | |
1266 | list_for_each_entry(stp, &fp->fi_stateids, st_perfile) | |
1267 | fp->fi_share_deny |= bmap_to_share_mode(stp->st_deny_bmap); | |
1268 | spin_unlock(&fp->fi_lock); | |
1269 | } | |
1270 | ||
1271 | static void | |
1272 | reset_union_bmap_deny(u32 deny, struct nfs4_ol_stateid *stp) | |
1273 | { | |
1274 | int i; | |
1275 | bool change = false; | |
1276 | ||
1277 | for (i = 1; i < 4; i++) { | |
1278 | if ((i & deny) != i) { | |
1279 | change = true; | |
1280 | clear_deny(i, stp); | |
1281 | } | |
1282 | } | |
1283 | ||
1284 | /* Recalculate per-file deny mode if there was a change */ | |
1285 | if (change) | |
11b9164a | 1286 | recalculate_deny_mode(stp->st_stid.sc_file); |
baeb4ff0 JL |
1287 | } |
1288 | ||
82c5ff1b JL |
1289 | /* release all access and file references for a given stateid */ |
1290 | static void | |
1291 | release_all_access(struct nfs4_ol_stateid *stp) | |
1292 | { | |
1293 | int i; | |
11b9164a | 1294 | struct nfs4_file *fp = stp->st_stid.sc_file; |
baeb4ff0 JL |
1295 | |
1296 | if (fp && stp->st_deny_bmap != 0) | |
1297 | recalculate_deny_mode(fp); | |
82c5ff1b JL |
1298 | |
1299 | for (i = 1; i < 4; i++) { | |
1300 | if (test_access(i, stp)) | |
11b9164a | 1301 | nfs4_file_put_access(stp->st_stid.sc_file, i); |
82c5ff1b JL |
1302 | clear_access(i, stp); |
1303 | } | |
1304 | } | |
1305 | ||
d50ffded KM |
1306 | static inline void nfs4_free_stateowner(struct nfs4_stateowner *sop) |
1307 | { | |
1308 | kfree(sop->so_owner.data); | |
1309 | sop->so_ops->so_free(sop); | |
1310 | } | |
1311 | ||
6b180f0b JL |
1312 | static void nfs4_put_stateowner(struct nfs4_stateowner *sop) |
1313 | { | |
a819ecc1 JL |
1314 | struct nfs4_client *clp = sop->so_client; |
1315 | ||
1316 | might_lock(&clp->cl_lock); | |
1317 | ||
1318 | if (!atomic_dec_and_lock(&sop->so_count, &clp->cl_lock)) | |
6b180f0b | 1319 | return; |
8f4b54c5 | 1320 | sop->so_ops->so_unhash(sop); |
a819ecc1 | 1321 | spin_unlock(&clp->cl_lock); |
d50ffded | 1322 | nfs4_free_stateowner(sop); |
6b180f0b JL |
1323 | } |
1324 | ||
a451b123 TM |
1325 | static bool |
1326 | nfs4_ol_stateid_unhashed(const struct nfs4_ol_stateid *stp) | |
1327 | { | |
1328 | return list_empty(&stp->st_perfile); | |
1329 | } | |
1330 | ||
e8568739 | 1331 | static bool unhash_ol_stateid(struct nfs4_ol_stateid *stp) |
529d7b2a | 1332 | { |
11b9164a | 1333 | struct nfs4_file *fp = stp->st_stid.sc_file; |
1d31a253 | 1334 | |
1c755dc1 JL |
1335 | lockdep_assert_held(&stp->st_stateowner->so_client->cl_lock); |
1336 | ||
e8568739 JL |
1337 | if (list_empty(&stp->st_perfile)) |
1338 | return false; | |
1339 | ||
1d31a253 | 1340 | spin_lock(&fp->fi_lock); |
e8568739 | 1341 | list_del_init(&stp->st_perfile); |
1d31a253 | 1342 | spin_unlock(&fp->fi_lock); |
529d7b2a | 1343 | list_del(&stp->st_perstateowner); |
e8568739 | 1344 | return true; |
529d7b2a BF |
1345 | } |
1346 | ||
6011695d | 1347 | static void nfs4_free_ol_stateid(struct nfs4_stid *stid) |
529d7b2a | 1348 | { |
6011695d | 1349 | struct nfs4_ol_stateid *stp = openlockstateid(stid); |
4665e2ba | 1350 | |
8287f009 | 1351 | put_clnt_odstate(stp->st_clnt_odstate); |
6011695d | 1352 | release_all_access(stp); |
d3134b10 JL |
1353 | if (stp->st_stateowner) |
1354 | nfs4_put_stateowner(stp->st_stateowner); | |
6011695d | 1355 | kmem_cache_free(stateid_slab, stid); |
529d7b2a BF |
1356 | } |
1357 | ||
b49e084d | 1358 | static void nfs4_free_lock_stateid(struct nfs4_stid *stid) |
529d7b2a | 1359 | { |
b49e084d JL |
1360 | struct nfs4_ol_stateid *stp = openlockstateid(stid); |
1361 | struct nfs4_lockowner *lo = lockowner(stp->st_stateowner); | |
eb82dd39 | 1362 | struct nfsd_file *nf; |
529d7b2a | 1363 | |
eb82dd39 JL |
1364 | nf = find_any_file(stp->st_stid.sc_file); |
1365 | if (nf) { | |
1366 | get_file(nf->nf_file); | |
1367 | filp_close(nf->nf_file, (fl_owner_t)lo); | |
1368 | nfsd_file_put(nf); | |
1369 | } | |
b49e084d JL |
1370 | nfs4_free_ol_stateid(stid); |
1371 | } | |
1372 | ||
2c41beb0 JL |
1373 | /* |
1374 | * Put the persistent reference to an already unhashed generic stateid, while | |
1375 | * holding the cl_lock. If it's the last reference, then put it onto the | |
1376 | * reaplist for later destruction. | |
1377 | */ | |
1378 | static void put_ol_stateid_locked(struct nfs4_ol_stateid *stp, | |
1379 | struct list_head *reaplist) | |
1380 | { | |
1381 | struct nfs4_stid *s = &stp->st_stid; | |
1382 | struct nfs4_client *clp = s->sc_client; | |
1383 | ||
1384 | lockdep_assert_held(&clp->cl_lock); | |
1385 | ||
1386 | WARN_ON_ONCE(!list_empty(&stp->st_locks)); | |
1387 | ||
a15dfcd5 | 1388 | if (!refcount_dec_and_test(&s->sc_count)) { |
2c41beb0 JL |
1389 | wake_up_all(&close_wq); |
1390 | return; | |
1391 | } | |
1392 | ||
1393 | idr_remove(&clp->cl_stateids, s->sc_stateid.si_opaque.so_id); | |
1394 | list_add(&stp->st_locks, reaplist); | |
1395 | } | |
1396 | ||
e8568739 | 1397 | static bool unhash_lock_stateid(struct nfs4_ol_stateid *stp) |
b49e084d | 1398 | { |
f46c445b | 1399 | lockdep_assert_held(&stp->st_stid.sc_client->cl_lock); |
3c1c995c | 1400 | |
a451b123 TM |
1401 | if (!unhash_ol_stateid(stp)) |
1402 | return false; | |
3c1c995c | 1403 | list_del_init(&stp->st_locks); |
cd61c522 | 1404 | nfs4_unhash_stid(&stp->st_stid); |
a451b123 | 1405 | return true; |
3c1c995c JL |
1406 | } |
1407 | ||
1408 | static void release_lock_stateid(struct nfs4_ol_stateid *stp) | |
1409 | { | |
f46c445b | 1410 | struct nfs4_client *clp = stp->st_stid.sc_client; |
e8568739 | 1411 | bool unhashed; |
3c1c995c | 1412 | |
f46c445b | 1413 | spin_lock(&clp->cl_lock); |
e8568739 | 1414 | unhashed = unhash_lock_stateid(stp); |
f46c445b | 1415 | spin_unlock(&clp->cl_lock); |
e8568739 JL |
1416 | if (unhashed) |
1417 | nfs4_put_stid(&stp->st_stid); | |
529d7b2a BF |
1418 | } |
1419 | ||
c58c6610 | 1420 | static void unhash_lockowner_locked(struct nfs4_lockowner *lo) |
8f4b54c5 | 1421 | { |
d4f0489f | 1422 | struct nfs4_client *clp = lo->lo_owner.so_client; |
c58c6610 | 1423 | |
d4f0489f | 1424 | lockdep_assert_held(&clp->cl_lock); |
c58c6610 | 1425 | |
8f4b54c5 JL |
1426 | list_del_init(&lo->lo_owner.so_strhash); |
1427 | } | |
1428 | ||
2c41beb0 JL |
1429 | /* |
1430 | * Free a list of generic stateids that were collected earlier after being | |
1431 | * fully unhashed. | |
1432 | */ | |
1433 | static void | |
1434 | free_ol_stateid_reaplist(struct list_head *reaplist) | |
1435 | { | |
1436 | struct nfs4_ol_stateid *stp; | |
fb94d766 | 1437 | struct nfs4_file *fp; |
2c41beb0 JL |
1438 | |
1439 | might_sleep(); | |
1440 | ||
1441 | while (!list_empty(reaplist)) { | |
1442 | stp = list_first_entry(reaplist, struct nfs4_ol_stateid, | |
1443 | st_locks); | |
1444 | list_del(&stp->st_locks); | |
fb94d766 | 1445 | fp = stp->st_stid.sc_file; |
2c41beb0 | 1446 | stp->st_stid.sc_free(&stp->st_stid); |
fb94d766 KM |
1447 | if (fp) |
1448 | put_nfs4_file(fp); | |
2c41beb0 JL |
1449 | } |
1450 | } | |
1451 | ||
d83017f9 JL |
1452 | static void release_open_stateid_locks(struct nfs4_ol_stateid *open_stp, |
1453 | struct list_head *reaplist) | |
3c87b9b7 TM |
1454 | { |
1455 | struct nfs4_ol_stateid *stp; | |
1456 | ||
e8568739 JL |
1457 | lockdep_assert_held(&open_stp->st_stid.sc_client->cl_lock); |
1458 | ||
3c87b9b7 TM |
1459 | while (!list_empty(&open_stp->st_locks)) { |
1460 | stp = list_entry(open_stp->st_locks.next, | |
1461 | struct nfs4_ol_stateid, st_locks); | |
e8568739 | 1462 | WARN_ON(!unhash_lock_stateid(stp)); |
d83017f9 | 1463 | put_ol_stateid_locked(stp, reaplist); |
529d7b2a BF |
1464 | } |
1465 | } | |
1466 | ||
e8568739 | 1467 | static bool unhash_open_stateid(struct nfs4_ol_stateid *stp, |
d83017f9 | 1468 | struct list_head *reaplist) |
2283963f | 1469 | { |
2c41beb0 JL |
1470 | lockdep_assert_held(&stp->st_stid.sc_client->cl_lock); |
1471 | ||
a451b123 TM |
1472 | if (!unhash_ol_stateid(stp)) |
1473 | return false; | |
d83017f9 | 1474 | release_open_stateid_locks(stp, reaplist); |
a451b123 | 1475 | return true; |
38c387b5 BF |
1476 | } |
1477 | ||
1478 | static void release_open_stateid(struct nfs4_ol_stateid *stp) | |
1479 | { | |
2c41beb0 JL |
1480 | LIST_HEAD(reaplist); |
1481 | ||
1482 | spin_lock(&stp->st_stid.sc_client->cl_lock); | |
e8568739 JL |
1483 | if (unhash_open_stateid(stp, &reaplist)) |
1484 | put_ol_stateid_locked(stp, &reaplist); | |
2c41beb0 JL |
1485 | spin_unlock(&stp->st_stid.sc_client->cl_lock); |
1486 | free_ol_stateid_reaplist(&reaplist); | |
2283963f BF |
1487 | } |
1488 | ||
7ffb5880 | 1489 | static void unhash_openowner_locked(struct nfs4_openowner *oo) |
f1d110ca | 1490 | { |
d4f0489f | 1491 | struct nfs4_client *clp = oo->oo_owner.so_client; |
7ffb5880 | 1492 | |
d4f0489f | 1493 | lockdep_assert_held(&clp->cl_lock); |
7ffb5880 | 1494 | |
8f4b54c5 JL |
1495 | list_del_init(&oo->oo_owner.so_strhash); |
1496 | list_del_init(&oo->oo_perclient); | |
f1d110ca BF |
1497 | } |
1498 | ||
f7a4d872 BF |
1499 | static void release_last_closed_stateid(struct nfs4_openowner *oo) |
1500 | { | |
217526e7 JL |
1501 | struct nfsd_net *nn = net_generic(oo->oo_owner.so_client->net, |
1502 | nfsd_net_id); | |
1503 | struct nfs4_ol_stateid *s; | |
f7a4d872 | 1504 | |
217526e7 JL |
1505 | spin_lock(&nn->client_lock); |
1506 | s = oo->oo_last_closed_stid; | |
f7a4d872 | 1507 | if (s) { |
d3134b10 | 1508 | list_del_init(&oo->oo_close_lru); |
f7a4d872 BF |
1509 | oo->oo_last_closed_stid = NULL; |
1510 | } | |
217526e7 JL |
1511 | spin_unlock(&nn->client_lock); |
1512 | if (s) | |
1513 | nfs4_put_stid(&s->st_stid); | |
f7a4d872 BF |
1514 | } |
1515 | ||
2c41beb0 | 1516 | static void release_openowner(struct nfs4_openowner *oo) |
8f4b54c5 JL |
1517 | { |
1518 | struct nfs4_ol_stateid *stp; | |
d4f0489f | 1519 | struct nfs4_client *clp = oo->oo_owner.so_client; |
2c41beb0 | 1520 | struct list_head reaplist; |
7ffb5880 | 1521 | |
2c41beb0 | 1522 | INIT_LIST_HEAD(&reaplist); |
8f4b54c5 | 1523 | |
2c41beb0 JL |
1524 | spin_lock(&clp->cl_lock); |
1525 | unhash_openowner_locked(oo); | |
8f4b54c5 JL |
1526 | while (!list_empty(&oo->oo_owner.so_stateids)) { |
1527 | stp = list_first_entry(&oo->oo_owner.so_stateids, | |
1528 | struct nfs4_ol_stateid, st_perstateowner); | |
e8568739 JL |
1529 | if (unhash_open_stateid(stp, &reaplist)) |
1530 | put_ol_stateid_locked(stp, &reaplist); | |
8f4b54c5 | 1531 | } |
d4f0489f | 1532 | spin_unlock(&clp->cl_lock); |
2c41beb0 | 1533 | free_ol_stateid_reaplist(&reaplist); |
f7a4d872 | 1534 | release_last_closed_stateid(oo); |
6b180f0b | 1535 | nfs4_put_stateowner(&oo->oo_owner); |
f1d110ca BF |
1536 | } |
1537 | ||
5282fd72 ME |
1538 | static inline int |
1539 | hash_sessionid(struct nfs4_sessionid *sessionid) | |
1540 | { | |
1541 | struct nfsd4_sessionid *sid = (struct nfsd4_sessionid *)sessionid; | |
1542 | ||
1543 | return sid->sequence % SESSION_HASH_SIZE; | |
1544 | } | |
1545 | ||
135dd002 | 1546 | #ifdef CONFIG_SUNRPC_DEBUG |
5282fd72 ME |
1547 | static inline void |
1548 | dump_sessionid(const char *fn, struct nfs4_sessionid *sessionid) | |
1549 | { | |
1550 | u32 *ptr = (u32 *)(&sessionid->data[0]); | |
1551 | dprintk("%s: %u:%u:%u:%u\n", fn, ptr[0], ptr[1], ptr[2], ptr[3]); | |
1552 | } | |
8f199b82 TM |
1553 | #else |
1554 | static inline void | |
1555 | dump_sessionid(const char *fn, struct nfs4_sessionid *sessionid) | |
1556 | { | |
1557 | } | |
1558 | #endif | |
1559 | ||
9411b1d4 BF |
1560 | /* |
1561 | * Bump the seqid on cstate->replay_owner, and clear replay_owner if it | |
1562 | * won't be used for replay. | |
1563 | */ | |
1564 | void nfsd4_bump_seqid(struct nfsd4_compound_state *cstate, __be32 nfserr) | |
1565 | { | |
1566 | struct nfs4_stateowner *so = cstate->replay_owner; | |
1567 | ||
1568 | if (nfserr == nfserr_replay_me) | |
1569 | return; | |
1570 | ||
1571 | if (!seqid_mutating_err(ntohl(nfserr))) { | |
58fb12e6 | 1572 | nfsd4_cstate_clear_replay(cstate); |
9411b1d4 BF |
1573 | return; |
1574 | } | |
1575 | if (!so) | |
1576 | return; | |
1577 | if (so->so_is_open_owner) | |
1578 | release_last_closed_stateid(openowner(so)); | |
1579 | so->so_seqid++; | |
1580 | return; | |
1581 | } | |
5282fd72 | 1582 | |
ec6b5d7b AA |
1583 | static void |
1584 | gen_sessionid(struct nfsd4_session *ses) | |
1585 | { | |
1586 | struct nfs4_client *clp = ses->se_client; | |
1587 | struct nfsd4_sessionid *sid; | |
1588 | ||
1589 | sid = (struct nfsd4_sessionid *)ses->se_sessionid.data; | |
1590 | sid->clientid = clp->cl_clientid; | |
1591 | sid->sequence = current_sessionid++; | |
1592 | sid->reserved = 0; | |
1593 | } | |
1594 | ||
1595 | /* | |
a649637c AA |
1596 | * The protocol defines ca_maxresponssize_cached to include the size of |
1597 | * the rpc header, but all we need to cache is the data starting after | |
1598 | * the end of the initial SEQUENCE operation--the rest we regenerate | |
1599 | * each time. Therefore we can advertise a ca_maxresponssize_cached | |
1600 | * value that is the number of bytes in our cache plus a few additional | |
1601 | * bytes. In order to stay on the safe side, and not promise more than | |
1602 | * we can cache, those additional bytes must be the minimum possible: 24 | |
1603 | * bytes of rpc header (xid through accept state, with AUTH_NULL | |
1604 | * verifier), 12 for the compound header (with zero-length tag), and 44 | |
1605 | * for the SEQUENCE op response: | |
1606 | */ | |
1607 | #define NFSD_MIN_HDR_SEQ_SZ (24 + 12 + 44) | |
1608 | ||
557ce264 AA |
1609 | static void |
1610 | free_session_slots(struct nfsd4_session *ses) | |
1611 | { | |
1612 | int i; | |
1613 | ||
53da6a53 BF |
1614 | for (i = 0; i < ses->se_fchannel.maxreqs; i++) { |
1615 | free_svc_cred(&ses->se_slots[i]->sl_cred); | |
557ce264 | 1616 | kfree(ses->se_slots[i]); |
53da6a53 | 1617 | } |
557ce264 AA |
1618 | } |
1619 | ||
a649637c | 1620 | /* |
efe0cb6d BF |
1621 | * We don't actually need to cache the rpc and session headers, so we |
1622 | * can allocate a little less for each slot: | |
1623 | */ | |
55c760cf | 1624 | static inline u32 slot_bytes(struct nfsd4_channel_attrs *ca) |
efe0cb6d | 1625 | { |
55c760cf | 1626 | u32 size; |
efe0cb6d | 1627 | |
55c760cf BF |
1628 | if (ca->maxresp_cached < NFSD_MIN_HDR_SEQ_SZ) |
1629 | size = 0; | |
1630 | else | |
1631 | size = ca->maxresp_cached - NFSD_MIN_HDR_SEQ_SZ; | |
1632 | return size + sizeof(struct nfsd4_slot); | |
5b6feee9 | 1633 | } |
ec6b5d7b | 1634 | |
5b6feee9 BF |
1635 | /* |
1636 | * XXX: If we run out of reserved DRC memory we could (up to a point) | |
a649637c | 1637 | * re-negotiate active sessions and reduce their slot usage to make |
42b2aa86 | 1638 | * room for new connections. For now we just fail the create session. |
ec6b5d7b | 1639 | */ |
2030ca56 | 1640 | static u32 nfsd4_get_drc_mem(struct nfsd4_channel_attrs *ca, struct nfsd_net *nn) |
ec6b5d7b | 1641 | { |
55c760cf BF |
1642 | u32 slotsize = slot_bytes(ca); |
1643 | u32 num = ca->maxreqs; | |
c54f24e3 | 1644 | unsigned long avail, total_avail; |
2030ca56 | 1645 | unsigned int scale_factor; |
ec6b5d7b | 1646 | |
5b6feee9 | 1647 | spin_lock(&nfsd_drc_lock); |
7f49fd5d N |
1648 | if (nfsd_drc_max_mem > nfsd_drc_mem_used) |
1649 | total_avail = nfsd_drc_max_mem - nfsd_drc_mem_used; | |
1650 | else | |
1651 | /* We have handed out more space than we chose in | |
1652 | * set_max_drc() to allow. That isn't really a | |
1653 | * problem as long as that doesn't make us think we | |
1654 | * have lots more due to integer overflow. | |
1655 | */ | |
1656 | total_avail = 0; | |
c54f24e3 | 1657 | avail = min((unsigned long)NFSD_MAX_MEM_PER_SESSION, total_avail); |
de766e57 | 1658 | /* |
2030ca56 | 1659 | * Never use more than a fraction of the remaining memory, |
7f49fd5d | 1660 | * unless it's the only way to give this client a slot. |
2030ca56 N |
1661 | * The chosen fraction is either 1/8 or 1/number of threads, |
1662 | * whichever is smaller. This ensures there are adequate | |
1663 | * slots to support multiple clients per thread. | |
7f49fd5d N |
1664 | * Give the client one slot even if that would require |
1665 | * over-allocation--it is better than failure. | |
de766e57 | 1666 | */ |
2030ca56 N |
1667 | scale_factor = max_t(unsigned int, 8, nn->nfsd_serv->sv_nrthreads); |
1668 | ||
1669 | avail = clamp_t(unsigned long, avail, slotsize, | |
1670 | total_avail/scale_factor); | |
5b6feee9 | 1671 | num = min_t(int, num, avail / slotsize); |
7f49fd5d | 1672 | num = max_t(int, num, 1); |
5b6feee9 BF |
1673 | nfsd_drc_mem_used += num * slotsize; |
1674 | spin_unlock(&nfsd_drc_lock); | |
ec6b5d7b | 1675 | |
5b6feee9 BF |
1676 | return num; |
1677 | } | |
ec6b5d7b | 1678 | |
55c760cf | 1679 | static void nfsd4_put_drc_mem(struct nfsd4_channel_attrs *ca) |
5b6feee9 | 1680 | { |
55c760cf BF |
1681 | int slotsize = slot_bytes(ca); |
1682 | ||
4bd9b0f4 | 1683 | spin_lock(&nfsd_drc_lock); |
55c760cf | 1684 | nfsd_drc_mem_used -= slotsize * ca->maxreqs; |
4bd9b0f4 | 1685 | spin_unlock(&nfsd_drc_lock); |
5b6feee9 | 1686 | } |
ec6b5d7b | 1687 | |
60810e54 KM |
1688 | static struct nfsd4_session *alloc_session(struct nfsd4_channel_attrs *fattrs, |
1689 | struct nfsd4_channel_attrs *battrs) | |
5b6feee9 | 1690 | { |
60810e54 KM |
1691 | int numslots = fattrs->maxreqs; |
1692 | int slotsize = slot_bytes(fattrs); | |
5b6feee9 BF |
1693 | struct nfsd4_session *new; |
1694 | int mem, i; | |
a649637c | 1695 | |
5b6feee9 BF |
1696 | BUILD_BUG_ON(NFSD_MAX_SLOTS_PER_SESSION * sizeof(struct nfsd4_slot *) |
1697 | + sizeof(struct nfsd4_session) > PAGE_SIZE); | |
1698 | mem = numslots * sizeof(struct nfsd4_slot *); | |
ec6b5d7b | 1699 | |
5b6feee9 BF |
1700 | new = kzalloc(sizeof(*new) + mem, GFP_KERNEL); |
1701 | if (!new) | |
1702 | return NULL; | |
557ce264 | 1703 | /* allocate each struct nfsd4_slot and data cache in one piece */ |
5b6feee9 | 1704 | for (i = 0; i < numslots; i++) { |
55c760cf | 1705 | new->se_slots[i] = kzalloc(slotsize, GFP_KERNEL); |
5b6feee9 | 1706 | if (!new->se_slots[i]) |
557ce264 | 1707 | goto out_free; |
557ce264 | 1708 | } |
60810e54 KM |
1709 | |
1710 | memcpy(&new->se_fchannel, fattrs, sizeof(struct nfsd4_channel_attrs)); | |
1711 | memcpy(&new->se_bchannel, battrs, sizeof(struct nfsd4_channel_attrs)); | |
1712 | ||
5b6feee9 BF |
1713 | return new; |
1714 | out_free: | |
1715 | while (i--) | |
1716 | kfree(new->se_slots[i]); | |
1717 | kfree(new); | |
1718 | return NULL; | |
ec6b5d7b AA |
1719 | } |
1720 | ||
19cf5c02 BF |
1721 | static void free_conn(struct nfsd4_conn *c) |
1722 | { | |
1723 | svc_xprt_put(c->cn_xprt); | |
1724 | kfree(c); | |
1725 | } | |
ec6b5d7b | 1726 | |
19cf5c02 BF |
1727 | static void nfsd4_conn_lost(struct svc_xpt_user *u) |
1728 | { | |
1729 | struct nfsd4_conn *c = container_of(u, struct nfsd4_conn, cn_xpt_user); | |
1730 | struct nfs4_client *clp = c->cn_session->se_client; | |
ec6b5d7b | 1731 | |
19cf5c02 BF |
1732 | spin_lock(&clp->cl_lock); |
1733 | if (!list_empty(&c->cn_persession)) { | |
1734 | list_del(&c->cn_persession); | |
1735 | free_conn(c); | |
1736 | } | |
eea49806 | 1737 | nfsd4_probe_callback(clp); |
2e4b7239 | 1738 | spin_unlock(&clp->cl_lock); |
19cf5c02 | 1739 | } |
ec6b5d7b | 1740 | |
d29c374c | 1741 | static struct nfsd4_conn *alloc_conn(struct svc_rqst *rqstp, u32 flags) |
c7662518 | 1742 | { |
c7662518 | 1743 | struct nfsd4_conn *conn; |
ec6b5d7b | 1744 | |
c7662518 BF |
1745 | conn = kmalloc(sizeof(struct nfsd4_conn), GFP_KERNEL); |
1746 | if (!conn) | |
db90681d | 1747 | return NULL; |
c7662518 BF |
1748 | svc_xprt_get(rqstp->rq_xprt); |
1749 | conn->cn_xprt = rqstp->rq_xprt; | |
d29c374c | 1750 | conn->cn_flags = flags; |
db90681d BF |
1751 | INIT_LIST_HEAD(&conn->cn_xpt_user.list); |
1752 | return conn; | |
1753 | } | |
a649637c | 1754 | |
328ead28 BF |
1755 | static void __nfsd4_hash_conn(struct nfsd4_conn *conn, struct nfsd4_session *ses) |
1756 | { | |
1757 | conn->cn_session = ses; | |
1758 | list_add(&conn->cn_persession, &ses->se_conns); | |
ec6b5d7b AA |
1759 | } |
1760 | ||
db90681d | 1761 | static void nfsd4_hash_conn(struct nfsd4_conn *conn, struct nfsd4_session *ses) |
557ce264 | 1762 | { |
db90681d | 1763 | struct nfs4_client *clp = ses->se_client; |
557ce264 | 1764 | |
c7662518 | 1765 | spin_lock(&clp->cl_lock); |
328ead28 | 1766 | __nfsd4_hash_conn(conn, ses); |
c7662518 | 1767 | spin_unlock(&clp->cl_lock); |
557ce264 AA |
1768 | } |
1769 | ||
21b75b01 | 1770 | static int nfsd4_register_conn(struct nfsd4_conn *conn) |
efe0cb6d | 1771 | { |
19cf5c02 | 1772 | conn->cn_xpt_user.callback = nfsd4_conn_lost; |
21b75b01 | 1773 | return register_xpt_user(conn->cn_xprt, &conn->cn_xpt_user); |
efe0cb6d BF |
1774 | } |
1775 | ||
e1ff371f | 1776 | static void nfsd4_init_conn(struct svc_rqst *rqstp, struct nfsd4_conn *conn, struct nfsd4_session *ses) |
ec6b5d7b | 1777 | { |
21b75b01 | 1778 | int ret; |
ec6b5d7b | 1779 | |
db90681d | 1780 | nfsd4_hash_conn(conn, ses); |
21b75b01 BF |
1781 | ret = nfsd4_register_conn(conn); |
1782 | if (ret) | |
1783 | /* oops; xprt is already down: */ | |
1784 | nfsd4_conn_lost(&conn->cn_xpt_user); | |
57a37144 BF |
1785 | /* We may have gained or lost a callback channel: */ |
1786 | nfsd4_probe_callback_sync(ses->se_client); | |
c7662518 | 1787 | } |
ec6b5d7b | 1788 | |
e1ff371f | 1789 | static struct nfsd4_conn *alloc_conn_from_crses(struct svc_rqst *rqstp, struct nfsd4_create_session *cses) |
1d1bc8f2 BF |
1790 | { |
1791 | u32 dir = NFS4_CDFC4_FORE; | |
1792 | ||
e1ff371f | 1793 | if (cses->flags & SESSION4_BACK_CHAN) |
1d1bc8f2 | 1794 | dir |= NFS4_CDFC4_BACK; |
e1ff371f | 1795 | return alloc_conn(rqstp, dir); |
1d1bc8f2 BF |
1796 | } |
1797 | ||
1798 | /* must be called under client_lock */ | |
19cf5c02 | 1799 | static void nfsd4_del_conns(struct nfsd4_session *s) |
c7662518 | 1800 | { |
19cf5c02 BF |
1801 | struct nfs4_client *clp = s->se_client; |
1802 | struct nfsd4_conn *c; | |
ec6b5d7b | 1803 | |
19cf5c02 BF |
1804 | spin_lock(&clp->cl_lock); |
1805 | while (!list_empty(&s->se_conns)) { | |
1806 | c = list_first_entry(&s->se_conns, struct nfsd4_conn, cn_persession); | |
1807 | list_del_init(&c->cn_persession); | |
1808 | spin_unlock(&clp->cl_lock); | |
557ce264 | 1809 | |
19cf5c02 BF |
1810 | unregister_xpt_user(c->cn_xprt, &c->cn_xpt_user); |
1811 | free_conn(c); | |
ec6b5d7b | 1812 | |
19cf5c02 BF |
1813 | spin_lock(&clp->cl_lock); |
1814 | } | |
1815 | spin_unlock(&clp->cl_lock); | |
c7662518 | 1816 | } |
ec6b5d7b | 1817 | |
1377b69e BF |
1818 | static void __free_session(struct nfsd4_session *ses) |
1819 | { | |
1377b69e BF |
1820 | free_session_slots(ses); |
1821 | kfree(ses); | |
1822 | } | |
1823 | ||
66b2b9b2 | 1824 | static void free_session(struct nfsd4_session *ses) |
c7662518 | 1825 | { |
19cf5c02 | 1826 | nfsd4_del_conns(ses); |
55c760cf | 1827 | nfsd4_put_drc_mem(&ses->se_fchannel); |
1377b69e | 1828 | __free_session(ses); |
c7662518 BF |
1829 | } |
1830 | ||
135ae827 | 1831 | static void init_session(struct svc_rqst *rqstp, struct nfsd4_session *new, struct nfs4_client *clp, struct nfsd4_create_session *cses) |
a827bcb2 | 1832 | { |
a827bcb2 | 1833 | int idx; |
1872de0e | 1834 | struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id); |
a827bcb2 | 1835 | |
ec6b5d7b AA |
1836 | new->se_client = clp; |
1837 | gen_sessionid(new); | |
ec6b5d7b | 1838 | |
c7662518 BF |
1839 | INIT_LIST_HEAD(&new->se_conns); |
1840 | ||
ac7c46f2 | 1841 | new->se_cb_seq_nr = 1; |
ec6b5d7b | 1842 | new->se_flags = cses->flags; |
8b5ce5cd | 1843 | new->se_cb_prog = cses->callback_prog; |
c6bb3ca2 | 1844 | new->se_cb_sec = cses->cb_sec; |
66b2b9b2 | 1845 | atomic_set(&new->se_ref, 0); |
5b6feee9 | 1846 | idx = hash_sessionid(&new->se_sessionid); |
1872de0e | 1847 | list_add(&new->se_hash, &nn->sessionid_hashtbl[idx]); |
4c649378 | 1848 | spin_lock(&clp->cl_lock); |
ec6b5d7b | 1849 | list_add(&new->se_perclnt, &clp->cl_sessions); |
4c649378 | 1850 | spin_unlock(&clp->cl_lock); |
60810e54 | 1851 | |
b0d2e42c | 1852 | { |
edd76786 | 1853 | struct sockaddr *sa = svc_addr(rqstp); |
dcbeaa68 BF |
1854 | /* |
1855 | * This is a little silly; with sessions there's no real | |
1856 | * use for the callback address. Use the peer address | |
1857 | * as a reasonable default for now, but consider fixing | |
1858 | * the rpc client not to require an address in the | |
1859 | * future: | |
1860 | */ | |
edd76786 BF |
1861 | rpc_copy_addr((struct sockaddr *)&clp->cl_cb_conn.cb_addr, sa); |
1862 | clp->cl_cb_conn.cb_addrlen = svc_addr_len(sa); | |
edd76786 | 1863 | } |
ec6b5d7b AA |
1864 | } |
1865 | ||
9089f1b4 | 1866 | /* caller must hold client_lock */ |
5282fd72 | 1867 | static struct nfsd4_session * |
d4e19e70 | 1868 | __find_in_sessionid_hashtbl(struct nfs4_sessionid *sessionid, struct net *net) |
5282fd72 ME |
1869 | { |
1870 | struct nfsd4_session *elem; | |
1871 | int idx; | |
1872de0e | 1872 | struct nfsd_net *nn = net_generic(net, nfsd_net_id); |
5282fd72 | 1873 | |
0a880a28 TM |
1874 | lockdep_assert_held(&nn->client_lock); |
1875 | ||
5282fd72 ME |
1876 | dump_sessionid(__func__, sessionid); |
1877 | idx = hash_sessionid(sessionid); | |
5282fd72 | 1878 | /* Search in the appropriate list */ |
1872de0e | 1879 | list_for_each_entry(elem, &nn->sessionid_hashtbl[idx], se_hash) { |
5282fd72 ME |
1880 | if (!memcmp(elem->se_sessionid.data, sessionid->data, |
1881 | NFS4_MAX_SESSIONID_LEN)) { | |
1882 | return elem; | |
1883 | } | |
1884 | } | |
1885 | ||
1886 | dprintk("%s: session not found\n", __func__); | |
1887 | return NULL; | |
1888 | } | |
1889 | ||
d4e19e70 TM |
1890 | static struct nfsd4_session * |
1891 | find_in_sessionid_hashtbl(struct nfs4_sessionid *sessionid, struct net *net, | |
1892 | __be32 *ret) | |
1893 | { | |
1894 | struct nfsd4_session *session; | |
1895 | __be32 status = nfserr_badsession; | |
1896 | ||
1897 | session = __find_in_sessionid_hashtbl(sessionid, net); | |
1898 | if (!session) | |
1899 | goto out; | |
1900 | status = nfsd4_get_session_locked(session); | |
1901 | if (status) | |
1902 | session = NULL; | |
1903 | out: | |
1904 | *ret = status; | |
1905 | return session; | |
1906 | } | |
1907 | ||
9089f1b4 | 1908 | /* caller must hold client_lock */ |
7116ed6b | 1909 | static void |
5282fd72 | 1910 | unhash_session(struct nfsd4_session *ses) |
7116ed6b | 1911 | { |
0a880a28 TM |
1912 | struct nfs4_client *clp = ses->se_client; |
1913 | struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id); | |
1914 | ||
1915 | lockdep_assert_held(&nn->client_lock); | |
1916 | ||
7116ed6b | 1917 | list_del(&ses->se_hash); |
4c649378 | 1918 | spin_lock(&ses->se_client->cl_lock); |
7116ed6b | 1919 | list_del(&ses->se_perclnt); |
4c649378 | 1920 | spin_unlock(&ses->se_client->cl_lock); |
5282fd72 ME |
1921 | } |
1922 | ||
1da177e4 LT |
1923 | /* SETCLIENTID and SETCLIENTID_CONFIRM Helper functions */ |
1924 | static int | |
2c142baa | 1925 | STALE_CLIENTID(clientid_t *clid, struct nfsd_net *nn) |
1da177e4 | 1926 | { |
bbc7f33a BF |
1927 | /* |
1928 | * We're assuming the clid was not given out from a boot | |
1929 | * precisely 2^32 (about 136 years) before this one. That seems | |
1930 | * a safe assumption: | |
1931 | */ | |
1932 | if (clid->cl_boot == (u32)nn->boot_time) | |
1da177e4 | 1933 | return 0; |
dd5e3fbc | 1934 | trace_nfsd_clid_stale(clid); |
1da177e4 LT |
1935 | return 1; |
1936 | } | |
1937 | ||
1938 | /* | |
1939 | * XXX Should we use a slab cache ? | |
1940 | * This type of memory management is somewhat inefficient, but we use it | |
1941 | * anyway since SETCLIENTID is not a common operation. | |
1942 | */ | |
35bba9a3 | 1943 | static struct nfs4_client *alloc_client(struct xdr_netobj name) |
1da177e4 LT |
1944 | { |
1945 | struct nfs4_client *clp; | |
d4f0489f | 1946 | int i; |
1da177e4 | 1947 | |
9258a2d5 | 1948 | clp = kmem_cache_zalloc(client_slab, GFP_KERNEL); |
35bba9a3 BF |
1949 | if (clp == NULL) |
1950 | return NULL; | |
6f4859b8 | 1951 | xdr_netobj_dup(&clp->cl_name, &name, GFP_KERNEL); |
d4f0489f TM |
1952 | if (clp->cl_name.data == NULL) |
1953 | goto err_no_name; | |
6da2ec56 KC |
1954 | clp->cl_ownerstr_hashtbl = kmalloc_array(OWNER_HASH_SIZE, |
1955 | sizeof(struct list_head), | |
1956 | GFP_KERNEL); | |
d4f0489f TM |
1957 | if (!clp->cl_ownerstr_hashtbl) |
1958 | goto err_no_hashtbl; | |
1959 | for (i = 0; i < OWNER_HASH_SIZE; i++) | |
1960 | INIT_LIST_HEAD(&clp->cl_ownerstr_hashtbl[i]); | |
5694c93e TM |
1961 | INIT_LIST_HEAD(&clp->cl_sessions); |
1962 | idr_init(&clp->cl_stateids); | |
14ed14cc | 1963 | atomic_set(&clp->cl_rpc_users, 0); |
5694c93e TM |
1964 | clp->cl_cb_state = NFSD4_CB_UNKNOWN; |
1965 | INIT_LIST_HEAD(&clp->cl_idhash); | |
1966 | INIT_LIST_HEAD(&clp->cl_openowners); | |
1967 | INIT_LIST_HEAD(&clp->cl_delegations); | |
1968 | INIT_LIST_HEAD(&clp->cl_lru); | |
5694c93e | 1969 | INIT_LIST_HEAD(&clp->cl_revoked); |
9cf514cc CH |
1970 | #ifdef CONFIG_NFSD_PNFS |
1971 | INIT_LIST_HEAD(&clp->cl_lo_states); | |
1972 | #endif | |
e0639dc5 OK |
1973 | INIT_LIST_HEAD(&clp->async_copies); |
1974 | spin_lock_init(&clp->async_lock); | |
5694c93e TM |
1975 | spin_lock_init(&clp->cl_lock); |
1976 | rpc_init_wait_queue(&clp->cl_cb_waitq, "Backchannel slot table"); | |
1da177e4 | 1977 | return clp; |
d4f0489f TM |
1978 | err_no_hashtbl: |
1979 | kfree(clp->cl_name.data); | |
1980 | err_no_name: | |
9258a2d5 | 1981 | kmem_cache_free(client_slab, clp); |
d4f0489f | 1982 | return NULL; |
1da177e4 LT |
1983 | } |
1984 | ||
59f8e91b BF |
1985 | static void __free_client(struct kref *k) |
1986 | { | |
e8a79fb1 BF |
1987 | struct nfsdfs_client *c = container_of(k, struct nfsdfs_client, cl_ref); |
1988 | struct nfs4_client *clp = container_of(c, struct nfs4_client, cl_nfsdfs); | |
59f8e91b BF |
1989 | |
1990 | free_svc_cred(&clp->cl_cred); | |
1991 | kfree(clp->cl_ownerstr_hashtbl); | |
1992 | kfree(clp->cl_name.data); | |
79123444 BF |
1993 | kfree(clp->cl_nii_domain.data); |
1994 | kfree(clp->cl_nii_name.data); | |
59f8e91b | 1995 | idr_destroy(&clp->cl_stateids); |
59f8e91b BF |
1996 | kmem_cache_free(client_slab, clp); |
1997 | } | |
1998 | ||
297e57a2 | 1999 | static void drop_client(struct nfs4_client *clp) |
59f8e91b | 2000 | { |
e8a79fb1 | 2001 | kref_put(&clp->cl_nfsdfs.cl_ref, __free_client); |
59f8e91b BF |
2002 | } |
2003 | ||
4dd86e15 | 2004 | static void |
1da177e4 LT |
2005 | free_client(struct nfs4_client *clp) |
2006 | { | |
792c95dd BF |
2007 | while (!list_empty(&clp->cl_sessions)) { |
2008 | struct nfsd4_session *ses; | |
2009 | ses = list_entry(clp->cl_sessions.next, struct nfsd4_session, | |
2010 | se_perclnt); | |
2011 | list_del(&ses->se_perclnt); | |
66b2b9b2 BF |
2012 | WARN_ON_ONCE(atomic_read(&ses->se_ref)); |
2013 | free_session(ses); | |
792c95dd | 2014 | } |
4cb57e30 | 2015 | rpc_destroy_wait_queue(&clp->cl_cb_waitq); |
89c905be | 2016 | if (clp->cl_nfsd_dentry) { |
e8a79fb1 | 2017 | nfsd_client_rmdir(clp->cl_nfsd_dentry); |
89c905be BF |
2018 | clp->cl_nfsd_dentry = NULL; |
2019 | wake_up_all(&expiry_wq); | |
2020 | } | |
59f8e91b | 2021 | drop_client(clp); |
1da177e4 LT |
2022 | } |
2023 | ||
84d38ac9 | 2024 | /* must be called under the client_lock */ |
4beb345b | 2025 | static void |
84d38ac9 BH |
2026 | unhash_client_locked(struct nfs4_client *clp) |
2027 | { | |
4beb345b | 2028 | struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id); |
792c95dd BF |
2029 | struct nfsd4_session *ses; |
2030 | ||
0a880a28 TM |
2031 | lockdep_assert_held(&nn->client_lock); |
2032 | ||
4beb345b TM |
2033 | /* Mark the client as expired! */ |
2034 | clp->cl_time = 0; | |
2035 | /* Make it invisible */ | |
2036 | if (!list_empty(&clp->cl_idhash)) { | |
2037 | list_del_init(&clp->cl_idhash); | |
2038 | if (test_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags)) | |
2039 | rb_erase(&clp->cl_namenode, &nn->conf_name_tree); | |
2040 | else | |
2041 | rb_erase(&clp->cl_namenode, &nn->unconf_name_tree); | |
2042 | } | |
2043 | list_del_init(&clp->cl_lru); | |
4c649378 | 2044 | spin_lock(&clp->cl_lock); |
792c95dd BF |
2045 | list_for_each_entry(ses, &clp->cl_sessions, se_perclnt) |
2046 | list_del_init(&ses->se_hash); | |
4c649378 | 2047 | spin_unlock(&clp->cl_lock); |
84d38ac9 BH |
2048 | } |
2049 | ||
1da177e4 | 2050 | static void |
4beb345b TM |
2051 | unhash_client(struct nfs4_client *clp) |
2052 | { | |
2053 | struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id); | |
2054 | ||
2055 | spin_lock(&nn->client_lock); | |
2056 | unhash_client_locked(clp); | |
2057 | spin_unlock(&nn->client_lock); | |
2058 | } | |
2059 | ||
97403d95 JL |
2060 | static __be32 mark_client_expired_locked(struct nfs4_client *clp) |
2061 | { | |
14ed14cc | 2062 | if (atomic_read(&clp->cl_rpc_users)) |
97403d95 JL |
2063 | return nfserr_jukebox; |
2064 | unhash_client_locked(clp); | |
2065 | return nfs_ok; | |
2066 | } | |
2067 | ||
4beb345b TM |
2068 | static void |
2069 | __destroy_client(struct nfs4_client *clp) | |
1da177e4 | 2070 | { |
68ef3bc3 | 2071 | int i; |
fe0750e5 | 2072 | struct nfs4_openowner *oo; |
1da177e4 | 2073 | struct nfs4_delegation *dp; |
1da177e4 LT |
2074 | struct list_head reaplist; |
2075 | ||
1da177e4 | 2076 | INIT_LIST_HEAD(&reaplist); |
cdc97505 | 2077 | spin_lock(&state_lock); |
ea1da636 N |
2078 | while (!list_empty(&clp->cl_delegations)) { |
2079 | dp = list_entry(clp->cl_delegations.next, struct nfs4_delegation, dl_perclnt); | |
3fcbbd24 | 2080 | WARN_ON(!unhash_delegation_locked(dp)); |
42690676 | 2081 | list_add(&dp->dl_recall_lru, &reaplist); |
1da177e4 | 2082 | } |
cdc97505 | 2083 | spin_unlock(&state_lock); |
1da177e4 LT |
2084 | while (!list_empty(&reaplist)) { |
2085 | dp = list_entry(reaplist.next, struct nfs4_delegation, dl_recall_lru); | |
42690676 | 2086 | list_del_init(&dp->dl_recall_lru); |
0af6e690 | 2087 | destroy_unhashed_deleg(dp); |
1da177e4 | 2088 | } |
2d4a532d | 2089 | while (!list_empty(&clp->cl_revoked)) { |
c876486b | 2090 | dp = list_entry(clp->cl_revoked.next, struct nfs4_delegation, dl_recall_lru); |
2d4a532d | 2091 | list_del_init(&dp->dl_recall_lru); |
6011695d | 2092 | nfs4_put_stid(&dp->dl_stid); |
956c4fee | 2093 | } |
ea1da636 | 2094 | while (!list_empty(&clp->cl_openowners)) { |
fe0750e5 | 2095 | oo = list_entry(clp->cl_openowners.next, struct nfs4_openowner, oo_perclient); |
b5971afa | 2096 | nfs4_get_stateowner(&oo->oo_owner); |
fe0750e5 | 2097 | release_openowner(oo); |
1da177e4 | 2098 | } |
68ef3bc3 JL |
2099 | for (i = 0; i < OWNER_HASH_SIZE; i++) { |
2100 | struct nfs4_stateowner *so, *tmp; | |
2101 | ||
2102 | list_for_each_entry_safe(so, tmp, &clp->cl_ownerstr_hashtbl[i], | |
2103 | so_strhash) { | |
2104 | /* Should be no openowners at this point */ | |
2105 | WARN_ON_ONCE(so->so_is_open_owner); | |
2106 | remove_blocked_locks(lockowner(so)); | |
2107 | } | |
2108 | } | |
9cf514cc | 2109 | nfsd4_return_all_client_layouts(clp); |
e0639dc5 | 2110 | nfsd4_shutdown_copy(clp); |
6ff8da08 | 2111 | nfsd4_shutdown_callback(clp); |
84d38ac9 BH |
2112 | if (clp->cl_cb_conn.cb_xprt) |
2113 | svc_xprt_put(clp->cl_cb_conn.cb_xprt); | |
221a6876 | 2114 | free_client(clp); |
89c905be | 2115 | wake_up_all(&expiry_wq); |
1da177e4 LT |
2116 | } |
2117 | ||
4beb345b TM |
2118 | static void |
2119 | destroy_client(struct nfs4_client *clp) | |
2120 | { | |
2121 | unhash_client(clp); | |
2122 | __destroy_client(clp); | |
2123 | } | |
2124 | ||
362063a5 SM |
2125 | static void inc_reclaim_complete(struct nfs4_client *clp) |
2126 | { | |
2127 | struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id); | |
2128 | ||
2129 | if (!nn->track_reclaim_completes) | |
2130 | return; | |
2131 | if (!nfsd4_find_reclaim_client(clp->cl_name, nn)) | |
2132 | return; | |
2133 | if (atomic_inc_return(&nn->nr_reclaim_complete) == | |
2134 | nn->reclaim_str_hashtbl_size) { | |
2135 | printk(KERN_INFO "NFSD: all clients done reclaiming, ending NFSv4 grace period (net %x)\n", | |
2136 | clp->net->ns.inum); | |
2137 | nfsd4_end_grace(nn); | |
2138 | } | |
2139 | } | |
2140 | ||
0d22f68f BF |
2141 | static void expire_client(struct nfs4_client *clp) |
2142 | { | |
4beb345b | 2143 | unhash_client(clp); |
0d22f68f | 2144 | nfsd4_client_record_remove(clp); |
4beb345b | 2145 | __destroy_client(clp); |
0d22f68f BF |
2146 | } |
2147 | ||
35bba9a3 BF |
2148 | static void copy_verf(struct nfs4_client *target, nfs4_verifier *source) |
2149 | { | |
2150 | memcpy(target->cl_verifier.data, source->data, | |
2151 | sizeof(target->cl_verifier.data)); | |
1da177e4 LT |
2152 | } |
2153 | ||
35bba9a3 BF |
2154 | static void copy_clid(struct nfs4_client *target, struct nfs4_client *source) |
2155 | { | |
1da177e4 LT |
2156 | target->cl_clientid.cl_boot = source->cl_clientid.cl_boot; |
2157 | target->cl_clientid.cl_id = source->cl_clientid.cl_id; | |
2158 | } | |
2159 | ||
50043859 BF |
2160 | static int copy_cred(struct svc_cred *target, struct svc_cred *source) |
2161 | { | |
2f10fdcb N |
2162 | target->cr_principal = kstrdup(source->cr_principal, GFP_KERNEL); |
2163 | target->cr_raw_principal = kstrdup(source->cr_raw_principal, | |
2164 | GFP_KERNEL); | |
9abdda5d CL |
2165 | target->cr_targ_princ = kstrdup(source->cr_targ_princ, GFP_KERNEL); |
2166 | if ((source->cr_principal && !target->cr_principal) || | |
2167 | (source->cr_raw_principal && !target->cr_raw_principal) || | |
2168 | (source->cr_targ_princ && !target->cr_targ_princ)) | |
2f10fdcb | 2169 | return -ENOMEM; |
50043859 | 2170 | |
d5497fc6 | 2171 | target->cr_flavor = source->cr_flavor; |
1da177e4 LT |
2172 | target->cr_uid = source->cr_uid; |
2173 | target->cr_gid = source->cr_gid; | |
2174 | target->cr_group_info = source->cr_group_info; | |
2175 | get_group_info(target->cr_group_info); | |
0dc1531a BF |
2176 | target->cr_gss_mech = source->cr_gss_mech; |
2177 | if (source->cr_gss_mech) | |
2178 | gss_mech_get(source->cr_gss_mech); | |
03a4e1f6 | 2179 | return 0; |
1da177e4 LT |
2180 | } |
2181 | ||
ef17af2a | 2182 | static int |
ac55fdc4 JL |
2183 | compare_blob(const struct xdr_netobj *o1, const struct xdr_netobj *o2) |
2184 | { | |
ef17af2a RV |
2185 | if (o1->len < o2->len) |
2186 | return -1; | |
2187 | if (o1->len > o2->len) | |
2188 | return 1; | |
2189 | return memcmp(o1->data, o2->data, o1->len); | |
ac55fdc4 JL |
2190 | } |
2191 | ||
1da177e4 | 2192 | static int |
599e0a22 BF |
2193 | same_verf(nfs4_verifier *v1, nfs4_verifier *v2) |
2194 | { | |
2195 | return 0 == memcmp(v1->data, v2->data, sizeof(v1->data)); | |
1da177e4 LT |
2196 | } |
2197 | ||
2198 | static int | |
599e0a22 BF |
2199 | same_clid(clientid_t *cl1, clientid_t *cl2) |
2200 | { | |
2201 | return (cl1->cl_boot == cl2->cl_boot) && (cl1->cl_id == cl2->cl_id); | |
1da177e4 LT |
2202 | } |
2203 | ||
8fbba96e BF |
2204 | static bool groups_equal(struct group_info *g1, struct group_info *g2) |
2205 | { | |
2206 | int i; | |
2207 | ||
2208 | if (g1->ngroups != g2->ngroups) | |
2209 | return false; | |
2210 | for (i=0; i<g1->ngroups; i++) | |
81243eac | 2211 | if (!gid_eq(g1->gid[i], g2->gid[i])) |
8fbba96e BF |
2212 | return false; |
2213 | return true; | |
2214 | } | |
2215 | ||
68eb3508 BF |
2216 | /* |
2217 | * RFC 3530 language requires clid_inuse be returned when the | |
2218 | * "principal" associated with a requests differs from that previously | |
2219 | * used. We use uid, gid's, and gss principal string as our best | |
2220 | * approximation. We also don't want to allow non-gss use of a client | |
2221 | * established using gss: in theory cr_principal should catch that | |
2222 | * change, but in practice cr_principal can be null even in the gss case | |
2223 | * since gssd doesn't always pass down a principal string. | |
2224 | */ | |
2225 | static bool is_gss_cred(struct svc_cred *cr) | |
2226 | { | |
2227 | /* Is cr_flavor one of the gss "pseudoflavors"?: */ | |
2228 | return (cr->cr_flavor > RPC_AUTH_MAXFLAVOR); | |
2229 | } | |
2230 | ||
2231 | ||
5559b50a | 2232 | static bool |
599e0a22 BF |
2233 | same_creds(struct svc_cred *cr1, struct svc_cred *cr2) |
2234 | { | |
68eb3508 | 2235 | if ((is_gss_cred(cr1) != is_gss_cred(cr2)) |
6fab8779 EB |
2236 | || (!uid_eq(cr1->cr_uid, cr2->cr_uid)) |
2237 | || (!gid_eq(cr1->cr_gid, cr2->cr_gid)) | |
8fbba96e BF |
2238 | || !groups_equal(cr1->cr_group_info, cr2->cr_group_info)) |
2239 | return false; | |
9abdda5d | 2240 | /* XXX: check that cr_targ_princ fields match ? */ |
8fbba96e BF |
2241 | if (cr1->cr_principal == cr2->cr_principal) |
2242 | return true; | |
2243 | if (!cr1->cr_principal || !cr2->cr_principal) | |
2244 | return false; | |
5559b50a | 2245 | return 0 == strcmp(cr1->cr_principal, cr2->cr_principal); |
1da177e4 LT |
2246 | } |
2247 | ||
57266a6e BF |
2248 | static bool svc_rqst_integrity_protected(struct svc_rqst *rqstp) |
2249 | { | |
2250 | struct svc_cred *cr = &rqstp->rq_cred; | |
2251 | u32 service; | |
2252 | ||
c4720591 BF |
2253 | if (!cr->cr_gss_mech) |
2254 | return false; | |
57266a6e BF |
2255 | service = gss_pseudoflavor_to_service(cr->cr_gss_mech, cr->cr_flavor); |
2256 | return service == RPC_GSS_SVC_INTEGRITY || | |
2257 | service == RPC_GSS_SVC_PRIVACY; | |
2258 | } | |
2259 | ||
dedeb13f | 2260 | bool nfsd4_mach_creds_match(struct nfs4_client *cl, struct svc_rqst *rqstp) |
57266a6e BF |
2261 | { |
2262 | struct svc_cred *cr = &rqstp->rq_cred; | |
2263 | ||
2264 | if (!cl->cl_mach_cred) | |
2265 | return true; | |
2266 | if (cl->cl_cred.cr_gss_mech != cr->cr_gss_mech) | |
2267 | return false; | |
2268 | if (!svc_rqst_integrity_protected(rqstp)) | |
2269 | return false; | |
414ca017 BF |
2270 | if (cl->cl_cred.cr_raw_principal) |
2271 | return 0 == strcmp(cl->cl_cred.cr_raw_principal, | |
2272 | cr->cr_raw_principal); | |
57266a6e BF |
2273 | if (!cr->cr_principal) |
2274 | return false; | |
2275 | return 0 == strcmp(cl->cl_cred.cr_principal, cr->cr_principal); | |
2276 | } | |
2277 | ||
294ac32e | 2278 | static void gen_confirm(struct nfs4_client *clp, struct nfsd_net *nn) |
deda2faa | 2279 | { |
ab4684d1 | 2280 | __be32 verf[2]; |
1da177e4 | 2281 | |
f419992c JL |
2282 | /* |
2283 | * This is opaque to client, so no need to byte-swap. Use | |
2284 | * __force to keep sparse happy | |
2285 | */ | |
9104ae49 | 2286 | verf[0] = (__force __be32)(u32)ktime_get_real_seconds(); |
19311aa8 | 2287 | verf[1] = (__force __be32)nn->clverifier_counter++; |
ab4684d1 | 2288 | memcpy(clp->cl_confirm.data, verf, sizeof(clp->cl_confirm.data)); |
1da177e4 LT |
2289 | } |
2290 | ||
294ac32e JL |
2291 | static void gen_clid(struct nfs4_client *clp, struct nfsd_net *nn) |
2292 | { | |
9cc76801 | 2293 | clp->cl_clientid.cl_boot = (u32)nn->boot_time; |
294ac32e JL |
2294 | clp->cl_clientid.cl_id = nn->clientid_counter++; |
2295 | gen_confirm(clp, nn); | |
2296 | } | |
2297 | ||
4770d722 JL |
2298 | static struct nfs4_stid * |
2299 | find_stateid_locked(struct nfs4_client *cl, stateid_t *t) | |
4581d140 | 2300 | { |
3abdb607 BF |
2301 | struct nfs4_stid *ret; |
2302 | ||
2303 | ret = idr_find(&cl->cl_stateids, t->si_opaque.so_id); | |
2304 | if (!ret || !ret->sc_type) | |
2305 | return NULL; | |
2306 | return ret; | |
4d71ab87 BF |
2307 | } |
2308 | ||
4770d722 JL |
2309 | static struct nfs4_stid * |
2310 | find_stateid_by_type(struct nfs4_client *cl, stateid_t *t, char typemask) | |
f459e453 BF |
2311 | { |
2312 | struct nfs4_stid *s; | |
4d71ab87 | 2313 | |
4770d722 JL |
2314 | spin_lock(&cl->cl_lock); |
2315 | s = find_stateid_locked(cl, t); | |
2d3f9668 TM |
2316 | if (s != NULL) { |
2317 | if (typemask & s->sc_type) | |
a15dfcd5 | 2318 | refcount_inc(&s->sc_count); |
2d3f9668 TM |
2319 | else |
2320 | s = NULL; | |
2321 | } | |
4770d722 JL |
2322 | spin_unlock(&cl->cl_lock); |
2323 | return s; | |
4581d140 BF |
2324 | } |
2325 | ||
a204f25e BF |
2326 | static struct nfs4_client *get_nfsdfs_clp(struct inode *inode) |
2327 | { | |
2328 | struct nfsdfs_client *nc; | |
2329 | nc = get_nfsdfs_client(inode); | |
2330 | if (!nc) | |
2331 | return NULL; | |
2332 | return container_of(nc, struct nfs4_client, cl_nfsdfs); | |
2333 | } | |
2334 | ||
169319f1 BF |
2335 | static void seq_quote_mem(struct seq_file *m, char *data, int len) |
2336 | { | |
2337 | seq_printf(m, "\""); | |
2338 | seq_escape_mem_ascii(m, data, len); | |
2339 | seq_printf(m, "\""); | |
2340 | } | |
2341 | ||
97ad4031 BF |
2342 | static int client_info_show(struct seq_file *m, void *v) |
2343 | { | |
2344 | struct inode *inode = m->private; | |
97ad4031 BF |
2345 | struct nfs4_client *clp; |
2346 | u64 clid; | |
2347 | ||
a204f25e BF |
2348 | clp = get_nfsdfs_clp(inode); |
2349 | if (!clp) | |
97ad4031 | 2350 | return -ENXIO; |
97ad4031 BF |
2351 | memcpy(&clid, &clp->cl_clientid, sizeof(clid)); |
2352 | seq_printf(m, "clientid: 0x%llx\n", clid); | |
169319f1 BF |
2353 | seq_printf(m, "address: \"%pISpc\"\n", (struct sockaddr *)&clp->cl_addr); |
2354 | seq_printf(m, "name: "); | |
2355 | seq_quote_mem(m, clp->cl_name.data, clp->cl_name.len); | |
2356 | seq_printf(m, "\nminor version: %d\n", clp->cl_minorversion); | |
79123444 BF |
2357 | if (clp->cl_nii_domain.data) { |
2358 | seq_printf(m, "Implementation domain: "); | |
2359 | seq_quote_mem(m, clp->cl_nii_domain.data, | |
2360 | clp->cl_nii_domain.len); | |
2361 | seq_printf(m, "\nImplementation name: "); | |
2362 | seq_quote_mem(m, clp->cl_nii_name.data, clp->cl_nii_name.len); | |
e29f4703 | 2363 | seq_printf(m, "\nImplementation time: [%lld, %ld]\n", |
79123444 BF |
2364 | clp->cl_nii_time.tv_sec, clp->cl_nii_time.tv_nsec); |
2365 | } | |
97ad4031 BF |
2366 | drop_client(clp); |
2367 | ||
2368 | return 0; | |
2369 | } | |
2370 | ||
2371 | static int client_info_open(struct inode *inode, struct file *file) | |
2372 | { | |
2373 | return single_open(file, client_info_show, inode); | |
2374 | } | |
2375 | ||
2376 | static const struct file_operations client_info_fops = { | |
2377 | .open = client_info_open, | |
2378 | .read = seq_read, | |
2379 | .llseek = seq_lseek, | |
2380 | .release = single_release, | |
2381 | }; | |
2382 | ||
78599c42 BF |
2383 | static void *states_start(struct seq_file *s, loff_t *pos) |
2384 | __acquires(&clp->cl_lock) | |
2385 | { | |
2386 | struct nfs4_client *clp = s->private; | |
2387 | unsigned long id = *pos; | |
2388 | void *ret; | |
2389 | ||
2390 | spin_lock(&clp->cl_lock); | |
2391 | ret = idr_get_next_ul(&clp->cl_stateids, &id); | |
2392 | *pos = id; | |
2393 | return ret; | |
2394 | } | |
2395 | ||
2396 | static void *states_next(struct seq_file *s, void *v, loff_t *pos) | |
2397 | { | |
2398 | struct nfs4_client *clp = s->private; | |
2399 | unsigned long id = *pos; | |
2400 | void *ret; | |
2401 | ||
2402 | id = *pos; | |
2403 | id++; | |
2404 | ret = idr_get_next_ul(&clp->cl_stateids, &id); | |
2405 | *pos = id; | |
2406 | return ret; | |
2407 | } | |
2408 | ||
2409 | static void states_stop(struct seq_file *s, void *v) | |
2410 | __releases(&clp->cl_lock) | |
2411 | { | |
2412 | struct nfs4_client *clp = s->private; | |
2413 | ||
2414 | spin_unlock(&clp->cl_lock); | |
2415 | } | |
2416 | ||
580da465 AG |
2417 | static void nfs4_show_fname(struct seq_file *s, struct nfsd_file *f) |
2418 | { | |
2419 | seq_printf(s, "filename: \"%pD2\"", f->nf_file); | |
2420 | } | |
2421 | ||
fd4f83fd | 2422 | static void nfs4_show_superblock(struct seq_file *s, struct nfsd_file *f) |
78599c42 | 2423 | { |
fd4f83fd | 2424 | struct inode *inode = f->nf_inode; |
78599c42 BF |
2425 | |
2426 | seq_printf(s, "superblock: \"%02x:%02x:%ld\"", | |
2427 | MAJOR(inode->i_sb->s_dev), | |
2428 | MINOR(inode->i_sb->s_dev), | |
2429 | inode->i_ino); | |
2430 | } | |
2431 | ||
2432 | static void nfs4_show_owner(struct seq_file *s, struct nfs4_stateowner *oo) | |
2433 | { | |
2434 | seq_printf(s, "owner: "); | |
2435 | seq_quote_mem(s, oo->so_owner.data, oo->so_owner.len); | |
2436 | } | |
2437 | ||
ace7ade4 BF |
2438 | static void nfs4_show_stateid(struct seq_file *s, stateid_t *stid) |
2439 | { | |
ee590d25 BF |
2440 | seq_printf(s, "0x%.8x", stid->si_generation); |
2441 | seq_printf(s, "%12phN", &stid->si_opaque); | |
ace7ade4 BF |
2442 | } |
2443 | ||
78599c42 BF |
2444 | static int nfs4_show_open(struct seq_file *s, struct nfs4_stid *st) |
2445 | { | |
2446 | struct nfs4_ol_stateid *ols; | |
2447 | struct nfs4_file *nf; | |
fd4f83fd | 2448 | struct nfsd_file *file; |
78599c42 BF |
2449 | struct nfs4_stateowner *oo; |
2450 | unsigned int access, deny; | |
2451 | ||
2452 | if (st->sc_type != NFS4_OPEN_STID && st->sc_type != NFS4_LOCK_STID) | |
2453 | return 0; /* XXX: or SEQ_SKIP? */ | |
2454 | ols = openlockstateid(st); | |
2455 | oo = ols->st_stateowner; | |
2456 | nf = st->sc_file; | |
2457 | file = find_any_file(nf); | |
9affa435 BF |
2458 | if (!file) |
2459 | return 0; | |
78599c42 | 2460 | |
ace7ade4 BF |
2461 | seq_printf(s, "- "); |
2462 | nfs4_show_stateid(s, &st->sc_stateid); | |
2463 | seq_printf(s, ": { type: open, "); | |
78599c42 BF |
2464 | |
2465 | access = bmap_to_share_mode(ols->st_access_bmap); | |
2466 | deny = bmap_to_share_mode(ols->st_deny_bmap); | |
2467 | ||
c4b77edb | 2468 | seq_printf(s, "access: %s%s, ", |
78599c42 BF |
2469 | access & NFS4_SHARE_ACCESS_READ ? "r" : "-", |
2470 | access & NFS4_SHARE_ACCESS_WRITE ? "w" : "-"); | |
c4b77edb | 2471 | seq_printf(s, "deny: %s%s, ", |
78599c42 BF |
2472 | deny & NFS4_SHARE_ACCESS_READ ? "r" : "-", |
2473 | deny & NFS4_SHARE_ACCESS_WRITE ? "w" : "-"); | |
2474 | ||
2475 | nfs4_show_superblock(s, file); | |
2476 | seq_printf(s, ", "); | |
580da465 AG |
2477 | nfs4_show_fname(s, file); |
2478 | seq_printf(s, ", "); | |
78599c42 BF |
2479 | nfs4_show_owner(s, oo); |
2480 | seq_printf(s, " }\n"); | |
fd4f83fd | 2481 | nfsd_file_put(file); |
16d36e09 BF |
2482 | |
2483 | return 0; | |
2484 | } | |
2485 | ||
2486 | static int nfs4_show_lock(struct seq_file *s, struct nfs4_stid *st) | |
2487 | { | |
2488 | struct nfs4_ol_stateid *ols; | |
2489 | struct nfs4_file *nf; | |
fd4f83fd | 2490 | struct nfsd_file *file; |
16d36e09 BF |
2491 | struct nfs4_stateowner *oo; |
2492 | ||
2493 | ols = openlockstateid(st); | |
2494 | oo = ols->st_stateowner; | |
2495 | nf = st->sc_file; | |
2496 | file = find_any_file(nf); | |
9affa435 BF |
2497 | if (!file) |
2498 | return 0; | |
16d36e09 | 2499 | |
ace7ade4 BF |
2500 | seq_printf(s, "- "); |
2501 | nfs4_show_stateid(s, &st->sc_stateid); | |
2502 | seq_printf(s, ": { type: lock, "); | |
16d36e09 BF |
2503 | |
2504 | /* | |
2505 | * Note: a lock stateid isn't really the same thing as a lock, | |
2506 | * it's the locking state held by one owner on a file, and there | |
2507 | * may be multiple (or no) lock ranges associated with it. | |
2508 | * (Same for the matter is true of open stateids.) | |
2509 | */ | |
78599c42 | 2510 | |
16d36e09 BF |
2511 | nfs4_show_superblock(s, file); |
2512 | /* XXX: open stateid? */ | |
2513 | seq_printf(s, ", "); | |
580da465 AG |
2514 | nfs4_show_fname(s, file); |
2515 | seq_printf(s, ", "); | |
16d36e09 BF |
2516 | nfs4_show_owner(s, oo); |
2517 | seq_printf(s, " }\n"); | |
fd4f83fd | 2518 | nfsd_file_put(file); |
78599c42 BF |
2519 | |
2520 | return 0; | |
2521 | } | |
2522 | ||
16d36e09 BF |
2523 | static int nfs4_show_deleg(struct seq_file *s, struct nfs4_stid *st) |
2524 | { | |
2525 | struct nfs4_delegation *ds; | |
2526 | struct nfs4_file *nf; | |
eb82dd39 | 2527 | struct nfsd_file *file; |
16d36e09 BF |
2528 | |
2529 | ds = delegstateid(st); | |
2530 | nf = st->sc_file; | |
9affa435 BF |
2531 | file = find_deleg_file(nf); |
2532 | if (!file) | |
2533 | return 0; | |
16d36e09 | 2534 | |
ace7ade4 BF |
2535 | seq_printf(s, "- "); |
2536 | nfs4_show_stateid(s, &st->sc_stateid); | |
2537 | seq_printf(s, ": { type: deleg, "); | |
16d36e09 BF |
2538 | |
2539 | /* Kinda dead code as long as we only support read delegs: */ | |
2540 | seq_printf(s, "access: %s, ", | |
2541 | ds->dl_type == NFS4_OPEN_DELEGATE_READ ? "r" : "w"); | |
2542 | ||
2543 | /* XXX: lease time, whether it's being recalled. */ | |
2544 | ||
2545 | nfs4_show_superblock(s, file); | |
580da465 AG |
2546 | seq_printf(s, ", "); |
2547 | nfs4_show_fname(s, file); | |
16d36e09 | 2548 | seq_printf(s, " }\n"); |
9affa435 | 2549 | nfsd_file_put(file); |
16d36e09 BF |
2550 | |
2551 | return 0; | |
2552 | } | |
2553 | ||
0c4b62b0 BF |
2554 | static int nfs4_show_layout(struct seq_file *s, struct nfs4_stid *st) |
2555 | { | |
2556 | struct nfs4_layout_stateid *ls; | |
eb82dd39 | 2557 | struct nfsd_file *file; |
0c4b62b0 BF |
2558 | |
2559 | ls = container_of(st, struct nfs4_layout_stateid, ls_stid); | |
2560 | file = ls->ls_file; | |
2561 | ||
ace7ade4 BF |
2562 | seq_printf(s, "- "); |
2563 | nfs4_show_stateid(s, &st->sc_stateid); | |
2564 | seq_printf(s, ": { type: layout, "); | |
0c4b62b0 BF |
2565 | |
2566 | /* XXX: What else would be useful? */ | |
2567 | ||
2568 | nfs4_show_superblock(s, file); | |
580da465 AG |
2569 | seq_printf(s, ", "); |
2570 | nfs4_show_fname(s, file); | |
0c4b62b0 BF |
2571 | seq_printf(s, " }\n"); |
2572 | ||
2573 | return 0; | |
2574 | } | |
2575 | ||
78599c42 BF |
2576 | static int states_show(struct seq_file *s, void *v) |
2577 | { | |
2578 | struct nfs4_stid *st = v; | |
2579 | ||
2580 | switch (st->sc_type) { | |
2581 | case NFS4_OPEN_STID: | |
2582 | return nfs4_show_open(s, st); | |
16d36e09 BF |
2583 | case NFS4_LOCK_STID: |
2584 | return nfs4_show_lock(s, st); | |
2585 | case NFS4_DELEG_STID: | |
2586 | return nfs4_show_deleg(s, st); | |
0c4b62b0 BF |
2587 | case NFS4_LAYOUT_STID: |
2588 | return nfs4_show_layout(s, st); | |
78599c42 BF |
2589 | default: |
2590 | return 0; /* XXX: or SEQ_SKIP? */ | |
2591 | } | |
16d36e09 | 2592 | /* XXX: copy stateids? */ |
78599c42 BF |
2593 | } |
2594 | ||
2595 | static struct seq_operations states_seq_ops = { | |
2596 | .start = states_start, | |
2597 | .next = states_next, | |
2598 | .stop = states_stop, | |
2599 | .show = states_show | |
2600 | }; | |
2601 | ||
2602 | static int client_states_open(struct inode *inode, struct file *file) | |
2603 | { | |
78599c42 BF |
2604 | struct seq_file *s; |
2605 | struct nfs4_client *clp; | |
2606 | int ret; | |
2607 | ||
a204f25e BF |
2608 | clp = get_nfsdfs_clp(inode); |
2609 | if (!clp) | |
78599c42 | 2610 | return -ENXIO; |
78599c42 BF |
2611 | |
2612 | ret = seq_open(file, &states_seq_ops); | |
2613 | if (ret) | |
2614 | return ret; | |
2615 | s = file->private_data; | |
2616 | s->private = clp; | |
2617 | return 0; | |
2618 | } | |
2619 | ||
2620 | static int client_opens_release(struct inode *inode, struct file *file) | |
2621 | { | |
2622 | struct seq_file *m = file->private_data; | |
2623 | struct nfs4_client *clp = m->private; | |
2624 | ||
2625 | /* XXX: alternatively, we could get/drop in seq start/stop */ | |
2626 | drop_client(clp); | |
2627 | return 0; | |
2628 | } | |
2629 | ||
2630 | static const struct file_operations client_states_fops = { | |
2631 | .open = client_states_open, | |
2632 | .read = seq_read, | |
2633 | .llseek = seq_lseek, | |
2634 | .release = client_opens_release, | |
2635 | }; | |
2636 | ||
89c905be BF |
2637 | /* |
2638 | * Normally we refuse to destroy clients that are in use, but here the | |
2639 | * administrator is telling us to just do it. We also want to wait | |
2640 | * so the caller has a guarantee that the client's locks are gone by | |
2641 | * the time the write returns: | |
2642 | */ | |
297e57a2 | 2643 | static void force_expire_client(struct nfs4_client *clp) |
89c905be BF |
2644 | { |
2645 | struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id); | |
2646 | bool already_expired; | |
2647 | ||
2648 | spin_lock(&clp->cl_lock); | |
2649 | clp->cl_time = 0; | |
2650 | spin_unlock(&clp->cl_lock); | |
2651 | ||
2652 | wait_event(expiry_wq, atomic_read(&clp->cl_rpc_users) == 0); | |
2653 | spin_lock(&nn->client_lock); | |
2654 | already_expired = list_empty(&clp->cl_lru); | |
2655 | if (!already_expired) | |
2656 | unhash_client_locked(clp); | |
2657 | spin_unlock(&nn->client_lock); | |
2658 | ||
2659 | if (!already_expired) | |
2660 | expire_client(clp); | |
2661 | else | |
2662 | wait_event(expiry_wq, clp->cl_nfsd_dentry == NULL); | |
2663 | } | |
2664 | ||
2665 | static ssize_t client_ctl_write(struct file *file, const char __user *buf, | |
2666 | size_t size, loff_t *pos) | |
2667 | { | |
2668 | char *data; | |
2669 | struct nfs4_client *clp; | |
2670 | ||
2671 | data = simple_transaction_get(file, buf, size); | |
2672 | if (IS_ERR(data)) | |
2673 | return PTR_ERR(data); | |
2674 | if (size != 7 || 0 != memcmp(data, "expire\n", 7)) | |
2675 | return -EINVAL; | |
2676 | clp = get_nfsdfs_clp(file_inode(file)); | |
2677 | if (!clp) | |
2678 | return -ENXIO; | |
2679 | force_expire_client(clp); | |
2680 | drop_client(clp); | |
2681 | return 7; | |
2682 | } | |
2683 | ||
2684 | static const struct file_operations client_ctl_fops = { | |
2685 | .write = client_ctl_write, | |
2686 | .release = simple_transaction_release, | |
2687 | }; | |
2688 | ||
97ad4031 BF |
2689 | static const struct tree_descr client_files[] = { |
2690 | [0] = {"info", &client_info_fops, S_IRUSR}, | |
78599c42 | 2691 | [1] = {"states", &client_states_fops, S_IRUSR}, |
6cbfad5f | 2692 | [2] = {"ctl", &client_ctl_fops, S_IWUSR}, |
78599c42 | 2693 | [3] = {""}, |
97ad4031 BF |
2694 | }; |
2695 | ||
2216d449 | 2696 | static struct nfs4_client *create_client(struct xdr_netobj name, |
b09333c4 RL |
2697 | struct svc_rqst *rqstp, nfs4_verifier *verf) |
2698 | { | |
2699 | struct nfs4_client *clp; | |
2700 | struct sockaddr *sa = svc_addr(rqstp); | |
03a4e1f6 | 2701 | int ret; |
c212cecf | 2702 | struct net *net = SVC_NET(rqstp); |
e8a79fb1 | 2703 | struct nfsd_net *nn = net_generic(net, nfsd_net_id); |
b09333c4 RL |
2704 | |
2705 | clp = alloc_client(name); | |
2706 | if (clp == NULL) | |
2707 | return NULL; | |
2708 | ||
03a4e1f6 BF |
2709 | ret = copy_cred(&clp->cl_cred, &rqstp->rq_cred); |
2710 | if (ret) { | |
03a4e1f6 | 2711 | free_client(clp); |
03a4e1f6 | 2712 | return NULL; |
b09333c4 | 2713 | } |
e8a79fb1 BF |
2714 | gen_clid(clp, nn); |
2715 | kref_init(&clp->cl_nfsdfs.cl_ref); | |
0162ac2b | 2716 | nfsd4_init_cb(&clp->cl_cb_null, clp, NULL, NFSPROC4_CLNT_CB_NULL); |
20b7d86f | 2717 | clp->cl_time = ktime_get_boottime_seconds(); |
b09333c4 | 2718 | clear_bit(0, &clp->cl_cb_slot_busy); |
b09333c4 | 2719 | copy_verf(clp, verf); |
3bade247 | 2720 | memcpy(&clp->cl_addr, sa, sizeof(struct sockaddr_storage)); |
edd76786 | 2721 | clp->cl_cb_session = NULL; |
c212cecf | 2722 | clp->net = net; |
e8a79fb1 | 2723 | clp->cl_nfsd_dentry = nfsd_client_mkdir(nn, &clp->cl_nfsdfs, |
97ad4031 BF |
2724 | clp->cl_clientid.cl_id - nn->clientid_base, |
2725 | client_files); | |
e8a79fb1 BF |
2726 | if (!clp->cl_nfsd_dentry) { |
2727 | free_client(clp); | |
2728 | return NULL; | |
2729 | } | |
b09333c4 RL |
2730 | return clp; |
2731 | } | |
2732 | ||
fd39ca9a | 2733 | static void |
ac55fdc4 JL |
2734 | add_clp_to_name_tree(struct nfs4_client *new_clp, struct rb_root *root) |
2735 | { | |
2736 | struct rb_node **new = &(root->rb_node), *parent = NULL; | |
2737 | struct nfs4_client *clp; | |
2738 | ||
2739 | while (*new) { | |
2740 | clp = rb_entry(*new, struct nfs4_client, cl_namenode); | |
2741 | parent = *new; | |
2742 | ||
2743 | if (compare_blob(&clp->cl_name, &new_clp->cl_name) > 0) | |
2744 | new = &((*new)->rb_left); | |
2745 | else | |
2746 | new = &((*new)->rb_right); | |
2747 | } | |
2748 | ||
2749 | rb_link_node(&new_clp->cl_namenode, parent, new); | |
2750 | rb_insert_color(&new_clp->cl_namenode, root); | |
2751 | } | |
2752 | ||
2753 | static struct nfs4_client * | |
2754 | find_clp_in_name_tree(struct xdr_netobj *name, struct rb_root *root) | |
2755 | { | |
ef17af2a | 2756 | int cmp; |
ac55fdc4 JL |
2757 | struct rb_node *node = root->rb_node; |
2758 | struct nfs4_client *clp; | |
2759 | ||
2760 | while (node) { | |
2761 | clp = rb_entry(node, struct nfs4_client, cl_namenode); | |
2762 | cmp = compare_blob(&clp->cl_name, name); | |
2763 | if (cmp > 0) | |
2764 | node = node->rb_left; | |
2765 | else if (cmp < 0) | |
2766 | node = node->rb_right; | |
2767 | else | |
2768 | return clp; | |
2769 | } | |
2770 | return NULL; | |
2771 | } | |
2772 | ||
2773 | static void | |
2774 | add_to_unconfirmed(struct nfs4_client *clp) | |
1da177e4 LT |
2775 | { |
2776 | unsigned int idhashval; | |
0a7ec377 | 2777 | struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id); |
1da177e4 | 2778 | |
0a880a28 TM |
2779 | lockdep_assert_held(&nn->client_lock); |
2780 | ||
ac55fdc4 | 2781 | clear_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags); |
a99454aa | 2782 | add_clp_to_name_tree(clp, &nn->unconf_name_tree); |
1da177e4 | 2783 | idhashval = clientid_hashval(clp->cl_clientid.cl_id); |
0a7ec377 | 2784 | list_add(&clp->cl_idhash, &nn->unconf_id_hashtbl[idhashval]); |
3dbacee6 | 2785 | renew_client_locked(clp); |
1da177e4 LT |
2786 | } |
2787 | ||
fd39ca9a | 2788 | static void |
1da177e4 LT |
2789 | move_to_confirmed(struct nfs4_client *clp) |
2790 | { | |
2791 | unsigned int idhashval = clientid_hashval(clp->cl_clientid.cl_id); | |
8daae4dc | 2792 | struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id); |
1da177e4 | 2793 | |
0a880a28 TM |
2794 | lockdep_assert_held(&nn->client_lock); |
2795 | ||
1da177e4 | 2796 | dprintk("NFSD: move_to_confirm nfs4_client %p\n", clp); |
8daae4dc | 2797 | list_move(&clp->cl_idhash, &nn->conf_id_hashtbl[idhashval]); |
a99454aa | 2798 | rb_erase(&clp->cl_namenode, &nn->unconf_name_tree); |
382a62e7 | 2799 | add_clp_to_name_tree(clp, &nn->conf_name_tree); |
ac55fdc4 | 2800 | set_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags); |
3dbacee6 | 2801 | renew_client_locked(clp); |
1da177e4 LT |
2802 | } |
2803 | ||
2804 | static struct nfs4_client * | |
bfa85e83 | 2805 | find_client_in_id_table(struct list_head *tbl, clientid_t *clid, bool sessions) |
1da177e4 LT |
2806 | { |
2807 | struct nfs4_client *clp; | |
2808 | unsigned int idhashval = clientid_hashval(clid->cl_id); | |
2809 | ||
bfa85e83 | 2810 | list_for_each_entry(clp, &tbl[idhashval], cl_idhash) { |
a50d2ad1 | 2811 | if (same_clid(&clp->cl_clientid, clid)) { |
d15c077e BF |
2812 | if ((bool)clp->cl_minorversion != sessions) |
2813 | return NULL; | |
3dbacee6 | 2814 | renew_client_locked(clp); |
1da177e4 | 2815 | return clp; |
a50d2ad1 | 2816 | } |
1da177e4 LT |
2817 | } |
2818 | return NULL; | |
2819 | } | |
2820 | ||
bfa85e83 BF |
2821 | static struct nfs4_client * |
2822 | find_confirmed_client(clientid_t *clid, bool sessions, struct nfsd_net *nn) | |
2823 | { | |
2824 | struct list_head *tbl = nn->conf_id_hashtbl; | |
2825 | ||
0a880a28 | 2826 | lockdep_assert_held(&nn->client_lock); |
bfa85e83 BF |
2827 | return find_client_in_id_table(tbl, clid, sessions); |
2828 | } | |
2829 | ||
1da177e4 | 2830 | static struct nfs4_client * |
0a7ec377 | 2831 | find_unconfirmed_client(clientid_t *clid, bool sessions, struct nfsd_net *nn) |
1da177e4 | 2832 | { |
bfa85e83 | 2833 | struct list_head *tbl = nn->unconf_id_hashtbl; |
1da177e4 | 2834 | |
0a880a28 | 2835 | lockdep_assert_held(&nn->client_lock); |
bfa85e83 | 2836 | return find_client_in_id_table(tbl, clid, sessions); |
1da177e4 LT |
2837 | } |
2838 | ||
6e5f15c9 | 2839 | static bool clp_used_exchangeid(struct nfs4_client *clp) |
a1bcecd2 | 2840 | { |
6e5f15c9 | 2841 | return clp->cl_exchange_flags != 0; |
e203d506 | 2842 | } |
a1bcecd2 | 2843 | |
28ce6054 | 2844 | static struct nfs4_client * |
382a62e7 | 2845 | find_confirmed_client_by_name(struct xdr_netobj *name, struct nfsd_net *nn) |
28ce6054 | 2846 | { |
0a880a28 | 2847 | lockdep_assert_held(&nn->client_lock); |
382a62e7 | 2848 | return find_clp_in_name_tree(name, &nn->conf_name_tree); |
28ce6054 N |
2849 | } |
2850 | ||
2851 | static struct nfs4_client * | |
a99454aa | 2852 | find_unconfirmed_client_by_name(struct xdr_netobj *name, struct nfsd_net *nn) |
28ce6054 | 2853 | { |
0a880a28 | 2854 | lockdep_assert_held(&nn->client_lock); |
a99454aa | 2855 | return find_clp_in_name_tree(name, &nn->unconf_name_tree); |
28ce6054 N |
2856 | } |
2857 | ||
fd39ca9a | 2858 | static void |
6f3d772f | 2859 | gen_callback(struct nfs4_client *clp, struct nfsd4_setclientid *se, struct svc_rqst *rqstp) |
1da177e4 | 2860 | { |
07263f1e | 2861 | struct nfs4_cb_conn *conn = &clp->cl_cb_conn; |
6f3d772f TU |
2862 | struct sockaddr *sa = svc_addr(rqstp); |
2863 | u32 scopeid = rpc_get_scope_id(sa); | |
7077ecba JL |
2864 | unsigned short expected_family; |
2865 | ||
2866 | /* Currently, we only support tcp and tcp6 for the callback channel */ | |
2867 | if (se->se_callback_netid_len == 3 && | |
2868 | !memcmp(se->se_callback_netid_val, "tcp", 3)) | |
2869 | expected_family = AF_INET; | |
2870 | else if (se->se_callback_netid_len == 4 && | |
2871 | !memcmp(se->se_callback_netid_val, "tcp6", 4)) | |
2872 | expected_family = AF_INET6; | |
2873 | else | |
1da177e4 LT |
2874 | goto out_err; |
2875 | ||
c212cecf | 2876 | conn->cb_addrlen = rpc_uaddr2sockaddr(clp->net, se->se_callback_addr_val, |
aa9a4ec7 | 2877 | se->se_callback_addr_len, |
07263f1e BF |
2878 | (struct sockaddr *)&conn->cb_addr, |
2879 | sizeof(conn->cb_addr)); | |
aa9a4ec7 | 2880 | |
07263f1e | 2881 | if (!conn->cb_addrlen || conn->cb_addr.ss_family != expected_family) |
1da177e4 | 2882 | goto out_err; |
aa9a4ec7 | 2883 | |
07263f1e BF |
2884 | if (conn->cb_addr.ss_family == AF_INET6) |
2885 | ((struct sockaddr_in6 *)&conn->cb_addr)->sin6_scope_id = scopeid; | |
fbf4665f | 2886 | |
07263f1e BF |
2887 | conn->cb_prog = se->se_callback_prog; |
2888 | conn->cb_ident = se->se_callback_ident; | |
849a1cf1 | 2889 | memcpy(&conn->cb_saddr, &rqstp->rq_daddr, rqstp->rq_daddrlen); |
1eace0d1 | 2890 | trace_nfsd_cb_args(clp, conn); |
1da177e4 LT |
2891 | return; |
2892 | out_err: | |
07263f1e BF |
2893 | conn->cb_addr.ss_family = AF_UNSPEC; |
2894 | conn->cb_addrlen = 0; | |
1eace0d1 | 2895 | trace_nfsd_cb_nodelegs(clp); |
1da177e4 LT |
2896 | return; |
2897 | } | |
2898 | ||
074fe897 | 2899 | /* |
067e1ace | 2900 | * Cache a reply. nfsd4_check_resp_size() has bounded the cache size. |
074fe897 | 2901 | */ |
b607664e | 2902 | static void |
074fe897 | 2903 | nfsd4_store_cache_entry(struct nfsd4_compoundres *resp) |
074fe897 | 2904 | { |
f5236013 | 2905 | struct xdr_buf *buf = resp->xdr.buf; |
557ce264 AA |
2906 | struct nfsd4_slot *slot = resp->cstate.slot; |
2907 | unsigned int base; | |
074fe897 | 2908 | |
557ce264 | 2909 | dprintk("--> %s slot %p\n", __func__, slot); |
074fe897 | 2910 | |
085def3a | 2911 | slot->sl_flags |= NFSD4_SLOT_INITIALIZED; |
557ce264 AA |
2912 | slot->sl_opcnt = resp->opcnt; |
2913 | slot->sl_status = resp->cstate.status; | |
53da6a53 BF |
2914 | free_svc_cred(&slot->sl_cred); |
2915 | copy_cred(&slot->sl_cred, &resp->rqstp->rq_cred); | |
074fe897 | 2916 | |
085def3a BF |
2917 | if (!nfsd4_cache_this(resp)) { |
2918 | slot->sl_flags &= ~NFSD4_SLOT_CACHED; | |
bf864a31 | 2919 | return; |
074fe897 | 2920 | } |
085def3a BF |
2921 | slot->sl_flags |= NFSD4_SLOT_CACHED; |
2922 | ||
f5236013 BF |
2923 | base = resp->cstate.data_offset; |
2924 | slot->sl_datalen = buf->len - base; | |
2925 | if (read_bytes_from_xdr_buf(buf, base, slot->sl_data, slot->sl_datalen)) | |
d3f03403 DC |
2926 | WARN(1, "%s: sessions DRC could not cache compound\n", |
2927 | __func__); | |
557ce264 | 2928 | return; |
074fe897 AA |
2929 | } |
2930 | ||
2931 | /* | |
abfabf8c AA |
2932 | * Encode the replay sequence operation from the slot values. |
2933 | * If cachethis is FALSE encode the uncached rep error on the next | |
2934 | * operation which sets resp->p and increments resp->opcnt for | |
2935 | * nfs4svc_encode_compoundres. | |
074fe897 | 2936 | * |
074fe897 | 2937 | */ |
abfabf8c AA |
2938 | static __be32 |
2939 | nfsd4_enc_sequence_replay(struct nfsd4_compoundargs *args, | |
2940 | struct nfsd4_compoundres *resp) | |
074fe897 | 2941 | { |
abfabf8c AA |
2942 | struct nfsd4_op *op; |
2943 | struct nfsd4_slot *slot = resp->cstate.slot; | |
bf864a31 | 2944 | |
abfabf8c AA |
2945 | /* Encode the replayed sequence operation */ |
2946 | op = &args->ops[resp->opcnt - 1]; | |
2947 | nfsd4_encode_operation(resp, op); | |
bf864a31 | 2948 | |
085def3a BF |
2949 | if (slot->sl_flags & NFSD4_SLOT_CACHED) |
2950 | return op->status; | |
2951 | if (args->opcnt == 1) { | |
2952 | /* | |
2953 | * The original operation wasn't a solo sequence--we | |
2954 | * always cache those--so this retry must not match the | |
2955 | * original: | |
2956 | */ | |
2957 | op->status = nfserr_seq_false_retry; | |
2958 | } else { | |
abfabf8c AA |
2959 | op = &args->ops[resp->opcnt++]; |
2960 | op->status = nfserr_retry_uncached_rep; | |
2961 | nfsd4_encode_operation(resp, op); | |
074fe897 | 2962 | } |
abfabf8c | 2963 | return op->status; |
074fe897 AA |
2964 | } |
2965 | ||
2966 | /* | |
557ce264 AA |
2967 | * The sequence operation is not cached because we can use the slot and |
2968 | * session values. | |
074fe897 | 2969 | */ |
3ca2eb98 | 2970 | static __be32 |
bf864a31 AA |
2971 | nfsd4_replay_cache_entry(struct nfsd4_compoundres *resp, |
2972 | struct nfsd4_sequence *seq) | |
074fe897 | 2973 | { |
557ce264 | 2974 | struct nfsd4_slot *slot = resp->cstate.slot; |
f5236013 BF |
2975 | struct xdr_stream *xdr = &resp->xdr; |
2976 | __be32 *p; | |
074fe897 AA |
2977 | __be32 status; |
2978 | ||
557ce264 | 2979 | dprintk("--> %s slot %p\n", __func__, slot); |
074fe897 | 2980 | |
abfabf8c | 2981 | status = nfsd4_enc_sequence_replay(resp->rqstp->rq_argp, resp); |
0da7b19c | 2982 | if (status) |
abfabf8c | 2983 | return status; |
074fe897 | 2984 | |
f5236013 BF |
2985 | p = xdr_reserve_space(xdr, slot->sl_datalen); |
2986 | if (!p) { | |
2987 | WARN_ON_ONCE(1); | |
2988 | return nfserr_serverfault; | |
2989 | } | |
2990 | xdr_encode_opaque_fixed(p, slot->sl_data, slot->sl_datalen); | |
2991 | xdr_commit_encode(xdr); | |
074fe897 | 2992 | |
557ce264 | 2993 | resp->opcnt = slot->sl_opcnt; |
f5236013 | 2994 | return slot->sl_status; |
074fe897 AA |
2995 | } |
2996 | ||
0733d213 AA |
2997 | /* |
2998 | * Set the exchange_id flags returned by the server. | |
2999 | */ | |
3000 | static void | |
3001 | nfsd4_set_ex_flags(struct nfs4_client *new, struct nfsd4_exchange_id *clid) | |
3002 | { | |
9cf514cc CH |
3003 | #ifdef CONFIG_NFSD_PNFS |
3004 | new->cl_exchange_flags |= EXCHGID4_FLAG_USE_PNFS_MDS; | |
3005 | #else | |
0733d213 | 3006 | new->cl_exchange_flags |= EXCHGID4_FLAG_USE_NON_PNFS; |
9cf514cc | 3007 | #endif |
0733d213 AA |
3008 | |
3009 | /* Referrals are supported, Migration is not. */ | |
3010 | new->cl_exchange_flags |= EXCHGID4_FLAG_SUPP_MOVED_REFER; | |
3011 | ||
3012 | /* set the wire flags to return to client. */ | |
3013 | clid->flags = new->cl_exchange_flags; | |
3014 | } | |
3015 | ||
4eaea134 BF |
3016 | static bool client_has_openowners(struct nfs4_client *clp) |
3017 | { | |
3018 | struct nfs4_openowner *oo; | |
3019 | ||
3020 | list_for_each_entry(oo, &clp->cl_openowners, oo_perclient) { | |
3021 | if (!list_empty(&oo->oo_owner.so_stateids)) | |
3022 | return true; | |
3023 | } | |
3024 | return false; | |
3025 | } | |
3026 | ||
631fc9ea BF |
3027 | static bool client_has_state(struct nfs4_client *clp) |
3028 | { | |
4eaea134 | 3029 | return client_has_openowners(clp) |
47e970be KM |
3030 | #ifdef CONFIG_NFSD_PNFS |
3031 | || !list_empty(&clp->cl_lo_states) | |
3032 | #endif | |
6eccece9 | 3033 | || !list_empty(&clp->cl_delegations) |
e0639dc5 OK |
3034 | || !list_empty(&clp->cl_sessions) |
3035 | || !list_empty(&clp->async_copies); | |
631fc9ea BF |
3036 | } |
3037 | ||
79123444 BF |
3038 | static __be32 copy_impl_id(struct nfs4_client *clp, |
3039 | struct nfsd4_exchange_id *exid) | |
3040 | { | |
3041 | if (!exid->nii_domain.data) | |
3042 | return 0; | |
3043 | xdr_netobj_dup(&clp->cl_nii_domain, &exid->nii_domain, GFP_KERNEL); | |
3044 | if (!clp->cl_nii_domain.data) | |
3045 | return nfserr_jukebox; | |
3046 | xdr_netobj_dup(&clp->cl_nii_name, &exid->nii_name, GFP_KERNEL); | |
3047 | if (!clp->cl_nii_name.data) | |
3048 | return nfserr_jukebox; | |
e29f4703 | 3049 | clp->cl_nii_time = exid->nii_time; |
79123444 BF |
3050 | return 0; |
3051 | } | |
3052 | ||
069b6ad4 | 3053 | __be32 |
eb69853d CH |
3054 | nfsd4_exchange_id(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, |
3055 | union nfsd4_op_u *u) | |
069b6ad4 | 3056 | { |
eb69853d | 3057 | struct nfsd4_exchange_id *exid = &u->exchange_id; |
3dbacee6 TM |
3058 | struct nfs4_client *conf, *new; |
3059 | struct nfs4_client *unconf = NULL; | |
57b7b43b | 3060 | __be32 status; |
363168b4 | 3061 | char addr_str[INET6_ADDRSTRLEN]; |
0733d213 | 3062 | nfs4_verifier verf = exid->verifier; |
363168b4 | 3063 | struct sockaddr *sa = svc_addr(rqstp); |
83e08fd4 | 3064 | bool update = exid->flags & EXCHGID4_FLAG_UPD_CONFIRMED_REC_A; |
c212cecf | 3065 | struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id); |
0733d213 | 3066 | |
363168b4 | 3067 | rpc_ntop(sa, addr_str, sizeof(addr_str)); |
0733d213 | 3068 | dprintk("%s rqstp=%p exid=%p clname.len=%u clname.data=%p " |
363168b4 | 3069 | "ip_addr=%s flags %x, spa_how %d\n", |
0733d213 | 3070 | __func__, rqstp, exid, exid->clname.len, exid->clname.data, |
363168b4 | 3071 | addr_str, exid->flags, exid->spa_how); |
0733d213 | 3072 | |
a084daf5 | 3073 | if (exid->flags & ~EXCHGID4_FLAG_MASK_A) |
0733d213 AA |
3074 | return nfserr_inval; |
3075 | ||
50c7b948 BF |
3076 | new = create_client(exid->clname, rqstp, &verf); |
3077 | if (new == NULL) | |
3078 | return nfserr_jukebox; | |
79123444 BF |
3079 | status = copy_impl_id(new, exid); |
3080 | if (status) | |
3081 | goto out_nolock; | |
50c7b948 | 3082 | |
0733d213 | 3083 | switch (exid->spa_how) { |
57266a6e | 3084 | case SP4_MACH_CRED: |
ed941643 AE |
3085 | exid->spo_must_enforce[0] = 0; |
3086 | exid->spo_must_enforce[1] = ( | |
3087 | 1 << (OP_BIND_CONN_TO_SESSION - 32) | | |
3088 | 1 << (OP_EXCHANGE_ID - 32) | | |
3089 | 1 << (OP_CREATE_SESSION - 32) | | |
3090 | 1 << (OP_DESTROY_SESSION - 32) | | |
3091 | 1 << (OP_DESTROY_CLIENTID - 32)); | |
3092 | ||
3093 | exid->spo_must_allow[0] &= (1 << (OP_CLOSE) | | |
3094 | 1 << (OP_OPEN_DOWNGRADE) | | |
3095 | 1 << (OP_LOCKU) | | |
3096 | 1 << (OP_DELEGRETURN)); | |
3097 | ||
3098 | exid->spo_must_allow[1] &= ( | |
3099 | 1 << (OP_TEST_STATEID - 32) | | |
3100 | 1 << (OP_FREE_STATEID - 32)); | |
50c7b948 BF |
3101 | if (!svc_rqst_integrity_protected(rqstp)) { |
3102 | status = nfserr_inval; | |
3103 | goto out_nolock; | |
920dd9bb BF |
3104 | } |
3105 | /* | |
3106 | * Sometimes userspace doesn't give us a principal. | |
3107 | * Which is a bug, really. Anyway, we can't enforce | |
3108 | * MACH_CRED in that case, better to give up now: | |
3109 | */ | |
414ca017 BF |
3110 | if (!new->cl_cred.cr_principal && |
3111 | !new->cl_cred.cr_raw_principal) { | |
920dd9bb BF |
3112 | status = nfserr_serverfault; |
3113 | goto out_nolock; | |
50c7b948 BF |
3114 | } |
3115 | new->cl_mach_cred = true; | |
0733d213 AA |
3116 | case SP4_NONE: |
3117 | break; | |
063b0fb9 BF |
3118 | default: /* checked by xdr code */ |
3119 | WARN_ON_ONCE(1); | |
df561f66 | 3120 | fallthrough; |
0733d213 | 3121 | case SP4_SSV: |
8edf4b02 KM |
3122 | status = nfserr_encr_alg_unsupp; |
3123 | goto out_nolock; | |
0733d213 AA |
3124 | } |
3125 | ||
2dbb269d | 3126 | /* Cases below refer to rfc 5661 section 18.35.4: */ |
3dbacee6 | 3127 | spin_lock(&nn->client_lock); |
382a62e7 | 3128 | conf = find_confirmed_client_by_name(&exid->clname, nn); |
0733d213 | 3129 | if (conf) { |
83e08fd4 BF |
3130 | bool creds_match = same_creds(&conf->cl_cred, &rqstp->rq_cred); |
3131 | bool verfs_match = same_verf(&verf, &conf->cl_verifier); | |
3132 | ||
136e658d BF |
3133 | if (update) { |
3134 | if (!clp_used_exchangeid(conf)) { /* buggy client */ | |
2dbb269d | 3135 | status = nfserr_inval; |
1a308118 BF |
3136 | goto out; |
3137 | } | |
dedeb13f | 3138 | if (!nfsd4_mach_creds_match(conf, rqstp)) { |
57266a6e BF |
3139 | status = nfserr_wrong_cred; |
3140 | goto out; | |
3141 | } | |
136e658d | 3142 | if (!creds_match) { /* case 9 */ |
ea236d07 | 3143 | status = nfserr_perm; |
136e658d BF |
3144 | goto out; |
3145 | } | |
3146 | if (!verfs_match) { /* case 8 */ | |
0733d213 AA |
3147 | status = nfserr_not_same; |
3148 | goto out; | |
3149 | } | |
136e658d BF |
3150 | /* case 6 */ |
3151 | exid->flags |= EXCHGID4_FLAG_CONFIRMED_R; | |
136e658d | 3152 | goto out_copy; |
0733d213 | 3153 | } |
136e658d | 3154 | if (!creds_match) { /* case 3 */ |
631fc9ea BF |
3155 | if (client_has_state(conf)) { |
3156 | status = nfserr_clid_inuse; | |
0733d213 AA |
3157 | goto out; |
3158 | } | |
0733d213 AA |
3159 | goto out_new; |
3160 | } | |
136e658d | 3161 | if (verfs_match) { /* case 2 */ |
0f1ba0ef | 3162 | conf->cl_exchange_flags |= EXCHGID4_FLAG_CONFIRMED_R; |
136e658d BF |
3163 | goto out_copy; |
3164 | } | |
3165 | /* case 5, client reboot */ | |
3dbacee6 | 3166 | conf = NULL; |
136e658d | 3167 | goto out_new; |
6ddbbbfe MS |
3168 | } |
3169 | ||
2dbb269d | 3170 | if (update) { /* case 7 */ |
6ddbbbfe MS |
3171 | status = nfserr_noent; |
3172 | goto out; | |
0733d213 AA |
3173 | } |
3174 | ||
a99454aa | 3175 | unconf = find_unconfirmed_client_by_name(&exid->clname, nn); |
2dbb269d | 3176 | if (unconf) /* case 4, possible retry or client restart */ |
3dbacee6 | 3177 | unhash_client_locked(unconf); |
0733d213 | 3178 | |
2dbb269d | 3179 | /* case 1 (normal case) */ |
0733d213 | 3180 | out_new: |
fd699b8a JL |
3181 | if (conf) { |
3182 | status = mark_client_expired_locked(conf); | |
3183 | if (status) | |
3184 | goto out; | |
3185 | } | |
4f540e29 | 3186 | new->cl_minorversion = cstate->minorversion; |
ed941643 AE |
3187 | new->cl_spo_must_allow.u.words[0] = exid->spo_must_allow[0]; |
3188 | new->cl_spo_must_allow.u.words[1] = exid->spo_must_allow[1]; | |
0733d213 | 3189 | |
ac55fdc4 | 3190 | add_to_unconfirmed(new); |
3dbacee6 | 3191 | swap(new, conf); |
0733d213 | 3192 | out_copy: |
5cc40fd7 TM |
3193 | exid->clientid.cl_boot = conf->cl_clientid.cl_boot; |
3194 | exid->clientid.cl_id = conf->cl_clientid.cl_id; | |
0733d213 | 3195 | |
5cc40fd7 TM |
3196 | exid->seqid = conf->cl_cs_slot.sl_seqid + 1; |
3197 | nfsd4_set_ex_flags(conf, exid); | |
0733d213 AA |
3198 | |
3199 | dprintk("nfsd4_exchange_id seqid %d flags %x\n", | |
5cc40fd7 | 3200 | conf->cl_cs_slot.sl_seqid, conf->cl_exchange_flags); |
0733d213 AA |
3201 | status = nfs_ok; |
3202 | ||
3203 | out: | |
3dbacee6 | 3204 | spin_unlock(&nn->client_lock); |
50c7b948 | 3205 | out_nolock: |
5cc40fd7 | 3206 | if (new) |
3dbacee6 TM |
3207 | expire_client(new); |
3208 | if (unconf) | |
3209 | expire_client(unconf); | |
0733d213 | 3210 | return status; |
069b6ad4 AA |
3211 | } |
3212 | ||
57b7b43b | 3213 | static __be32 |
88e588d5 | 3214 | check_slot_seqid(u32 seqid, u32 slot_seqid, int slot_inuse) |
b85d4c01 | 3215 | { |
88e588d5 AA |
3216 | dprintk("%s enter. seqid %d slot_seqid %d\n", __func__, seqid, |
3217 | slot_seqid); | |
b85d4c01 BH |
3218 | |
3219 | /* The slot is in use, and no response has been sent. */ | |
88e588d5 AA |
3220 | if (slot_inuse) { |
3221 | if (seqid == slot_seqid) | |
b85d4c01 BH |
3222 | return nfserr_jukebox; |
3223 | else | |
3224 | return nfserr_seq_misordered; | |
3225 | } | |
f6d82485 | 3226 | /* Note unsigned 32-bit arithmetic handles wraparound: */ |
88e588d5 | 3227 | if (likely(seqid == slot_seqid + 1)) |
b85d4c01 | 3228 | return nfs_ok; |
88e588d5 | 3229 | if (seqid == slot_seqid) |
b85d4c01 | 3230 | return nfserr_replay_cache; |
b85d4c01 BH |
3231 | return nfserr_seq_misordered; |
3232 | } | |
3233 | ||
49557cc7 AA |
3234 | /* |
3235 | * Cache the create session result into the create session single DRC | |
3236 | * slot cache by saving the xdr structure. sl_seqid has been set. | |
3237 | * Do this for solo or embedded create session operations. | |
3238 | */ | |
3239 | static void | |
3240 | nfsd4_cache_create_session(struct nfsd4_create_session *cr_ses, | |
57b7b43b | 3241 | struct nfsd4_clid_slot *slot, __be32 nfserr) |
49557cc7 AA |
3242 | { |
3243 | slot->sl_status = nfserr; | |
3244 | memcpy(&slot->sl_cr_ses, cr_ses, sizeof(*cr_ses)); | |
3245 | } | |
3246 | ||
3247 | static __be32 | |
3248 | nfsd4_replay_create_session(struct nfsd4_create_session *cr_ses, | |
3249 | struct nfsd4_clid_slot *slot) | |
3250 | { | |
3251 | memcpy(cr_ses, &slot->sl_cr_ses, sizeof(*cr_ses)); | |
3252 | return slot->sl_status; | |
3253 | } | |
3254 | ||
1b74c25b MJ |
3255 | #define NFSD_MIN_REQ_HDR_SEQ_SZ ((\ |
3256 | 2 * 2 + /* credential,verifier: AUTH_NULL, length 0 */ \ | |
3257 | 1 + /* MIN tag is length with zero, only length */ \ | |
3258 | 3 + /* version, opcount, opcode */ \ | |
3259 | XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \ | |
3260 | /* seqid, slotID, slotID, cache */ \ | |
3261 | 4 ) * sizeof(__be32)) | |
3262 | ||
3263 | #define NFSD_MIN_RESP_HDR_SEQ_SZ ((\ | |
3264 | 2 + /* verifier: AUTH_NULL, length 0 */\ | |
3265 | 1 + /* status */ \ | |
3266 | 1 + /* MIN tag is length with zero, only length */ \ | |
3267 | 3 + /* opcount, opcode, opstatus*/ \ | |
3268 | XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \ | |
3269 | /* seqid, slotID, slotID, slotID, status */ \ | |
3270 | 5 ) * sizeof(__be32)) | |
3271 | ||
55c760cf | 3272 | static __be32 check_forechannel_attrs(struct nfsd4_channel_attrs *ca, struct nfsd_net *nn) |
1b74c25b | 3273 | { |
55c760cf BF |
3274 | u32 maxrpc = nn->nfsd_serv->sv_max_mesg; |
3275 | ||
373cd409 BF |
3276 | if (ca->maxreq_sz < NFSD_MIN_REQ_HDR_SEQ_SZ) |
3277 | return nfserr_toosmall; | |
3278 | if (ca->maxresp_sz < NFSD_MIN_RESP_HDR_SEQ_SZ) | |
3279 | return nfserr_toosmall; | |
55c760cf BF |
3280 | ca->headerpadsz = 0; |
3281 | ca->maxreq_sz = min_t(u32, ca->maxreq_sz, maxrpc); | |
3282 | ca->maxresp_sz = min_t(u32, ca->maxresp_sz, maxrpc); | |
3283 | ca->maxops = min_t(u32, ca->maxops, NFSD_MAX_OPS_PER_COMPOUND); | |
3284 | ca->maxresp_cached = min_t(u32, ca->maxresp_cached, | |
3285 | NFSD_SLOT_CACHE_SIZE + NFSD_MIN_HDR_SEQ_SZ); | |
3286 | ca->maxreqs = min_t(u32, ca->maxreqs, NFSD_MAX_SLOTS_PER_SESSION); | |
3287 | /* | |
3288 | * Note decreasing slot size below client's request may make it | |
3289 | * difficult for client to function correctly, whereas | |
3290 | * decreasing the number of slots will (just?) affect | |
3291 | * performance. When short on memory we therefore prefer to | |
3292 | * decrease number of slots instead of their size. Clients that | |
3293 | * request larger slots than they need will get poor results: | |
7f49fd5d N |
3294 | * Note that we always allow at least one slot, because our |
3295 | * accounting is soft and provides no guarantees either way. | |
55c760cf | 3296 | */ |
2030ca56 | 3297 | ca->maxreqs = nfsd4_get_drc_mem(ca, nn); |
55c760cf | 3298 | |
373cd409 | 3299 | return nfs_ok; |
1b74c25b MJ |
3300 | } |
3301 | ||
4500632f CL |
3302 | /* |
3303 | * Server's NFSv4.1 backchannel support is AUTH_SYS-only for now. | |
3304 | * These are based on similar macros in linux/sunrpc/msg_prot.h . | |
3305 | */ | |
3306 | #define RPC_MAX_HEADER_WITH_AUTH_SYS \ | |
3307 | (RPC_CALLHDRSIZE + 2 * (2 + UNX_CALLSLACK)) | |
3308 | ||
3309 | #define RPC_MAX_REPHEADER_WITH_AUTH_SYS \ | |
3310 | (RPC_REPHDRSIZE + (2 + NUL_REPLYSLACK)) | |
3311 | ||
8a891633 | 3312 | #define NFSD_CB_MAX_REQ_SZ ((NFS4_enc_cb_recall_sz + \ |
4500632f | 3313 | RPC_MAX_HEADER_WITH_AUTH_SYS) * sizeof(__be32)) |
8a891633 | 3314 | #define NFSD_CB_MAX_RESP_SZ ((NFS4_dec_cb_recall_sz + \ |
4500632f CL |
3315 | RPC_MAX_REPHEADER_WITH_AUTH_SYS) * \ |
3316 | sizeof(__be32)) | |
8a891633 | 3317 | |
06b332a5 | 3318 | static __be32 check_backchannel_attrs(struct nfsd4_channel_attrs *ca) |
1b74c25b | 3319 | { |
06b332a5 BF |
3320 | ca->headerpadsz = 0; |
3321 | ||
8a891633 | 3322 | if (ca->maxreq_sz < NFSD_CB_MAX_REQ_SZ) |
06b332a5 | 3323 | return nfserr_toosmall; |
8a891633 | 3324 | if (ca->maxresp_sz < NFSD_CB_MAX_RESP_SZ) |
06b332a5 BF |
3325 | return nfserr_toosmall; |
3326 | ca->maxresp_cached = 0; | |
3327 | if (ca->maxops < 2) | |
3328 | return nfserr_toosmall; | |
3329 | ||
3330 | return nfs_ok; | |
1b74c25b MJ |
3331 | } |
3332 | ||
b78724b7 BF |
3333 | static __be32 nfsd4_check_cb_sec(struct nfsd4_cb_sec *cbs) |
3334 | { | |
3335 | switch (cbs->flavor) { | |
3336 | case RPC_AUTH_NULL: | |
3337 | case RPC_AUTH_UNIX: | |
3338 | return nfs_ok; | |
3339 | default: | |
3340 | /* | |
3341 | * GSS case: the spec doesn't allow us to return this | |
3342 | * error. But it also doesn't allow us not to support | |
3343 | * GSS. | |
3344 | * I'd rather this fail hard than return some error the | |
3345 | * client might think it can already handle: | |
3346 | */ | |
3347 | return nfserr_encr_alg_unsupp; | |
3348 | } | |
3349 | } | |
3350 | ||
069b6ad4 AA |
3351 | __be32 |
3352 | nfsd4_create_session(struct svc_rqst *rqstp, | |
eb69853d | 3353 | struct nfsd4_compound_state *cstate, union nfsd4_op_u *u) |
069b6ad4 | 3354 | { |
eb69853d | 3355 | struct nfsd4_create_session *cr_ses = &u->create_session; |
363168b4 | 3356 | struct sockaddr *sa = svc_addr(rqstp); |
ec6b5d7b | 3357 | struct nfs4_client *conf, *unconf; |
d20c11d8 | 3358 | struct nfs4_client *old = NULL; |
ac7c46f2 | 3359 | struct nfsd4_session *new; |
81f0b2a4 | 3360 | struct nfsd4_conn *conn; |
49557cc7 | 3361 | struct nfsd4_clid_slot *cs_slot = NULL; |
57b7b43b | 3362 | __be32 status = 0; |
8daae4dc | 3363 | struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id); |
ec6b5d7b | 3364 | |
a62573dc MJ |
3365 | if (cr_ses->flags & ~SESSION4_FLAG_MASK_A) |
3366 | return nfserr_inval; | |
b78724b7 BF |
3367 | status = nfsd4_check_cb_sec(&cr_ses->cb_sec); |
3368 | if (status) | |
3369 | return status; | |
55c760cf | 3370 | status = check_forechannel_attrs(&cr_ses->fore_channel, nn); |
06b332a5 BF |
3371 | if (status) |
3372 | return status; | |
3373 | status = check_backchannel_attrs(&cr_ses->back_channel); | |
373cd409 | 3374 | if (status) |
f403e450 | 3375 | goto out_release_drc_mem; |
81f0b2a4 | 3376 | status = nfserr_jukebox; |
60810e54 | 3377 | new = alloc_session(&cr_ses->fore_channel, &cr_ses->back_channel); |
55c760cf BF |
3378 | if (!new) |
3379 | goto out_release_drc_mem; | |
81f0b2a4 BF |
3380 | conn = alloc_conn_from_crses(rqstp, cr_ses); |
3381 | if (!conn) | |
3382 | goto out_free_session; | |
a62573dc | 3383 | |
d20c11d8 | 3384 | spin_lock(&nn->client_lock); |
0a7ec377 | 3385 | unconf = find_unconfirmed_client(&cr_ses->clientid, true, nn); |
8daae4dc | 3386 | conf = find_confirmed_client(&cr_ses->clientid, true, nn); |
78389046 | 3387 | WARN_ON_ONCE(conf && unconf); |
ec6b5d7b AA |
3388 | |
3389 | if (conf) { | |
57266a6e | 3390 | status = nfserr_wrong_cred; |
dedeb13f | 3391 | if (!nfsd4_mach_creds_match(conf, rqstp)) |
57266a6e | 3392 | goto out_free_conn; |
49557cc7 AA |
3393 | cs_slot = &conf->cl_cs_slot; |
3394 | status = check_slot_seqid(cr_ses->seqid, cs_slot->sl_seqid, 0); | |
f5e22bb6 KM |
3395 | if (status) { |
3396 | if (status == nfserr_replay_cache) | |
3397 | status = nfsd4_replay_create_session(cr_ses, cs_slot); | |
81f0b2a4 | 3398 | goto out_free_conn; |
ec6b5d7b | 3399 | } |
ec6b5d7b AA |
3400 | } else if (unconf) { |
3401 | if (!same_creds(&unconf->cl_cred, &rqstp->rq_cred) || | |
363168b4 | 3402 | !rpc_cmp_addr(sa, (struct sockaddr *) &unconf->cl_addr)) { |
ec6b5d7b | 3403 | status = nfserr_clid_inuse; |
81f0b2a4 | 3404 | goto out_free_conn; |
ec6b5d7b | 3405 | } |
57266a6e | 3406 | status = nfserr_wrong_cred; |
dedeb13f | 3407 | if (!nfsd4_mach_creds_match(unconf, rqstp)) |
57266a6e | 3408 | goto out_free_conn; |
49557cc7 AA |
3409 | cs_slot = &unconf->cl_cs_slot; |
3410 | status = check_slot_seqid(cr_ses->seqid, cs_slot->sl_seqid, 0); | |
38eb76a5 AA |
3411 | if (status) { |
3412 | /* an unconfirmed replay returns misordered */ | |
ec6b5d7b | 3413 | status = nfserr_seq_misordered; |
81f0b2a4 | 3414 | goto out_free_conn; |
ec6b5d7b | 3415 | } |
382a62e7 | 3416 | old = find_confirmed_client_by_name(&unconf->cl_name, nn); |
221a6876 | 3417 | if (old) { |
d20c11d8 | 3418 | status = mark_client_expired_locked(old); |
7abea1e8 JL |
3419 | if (status) { |
3420 | old = NULL; | |
221a6876 | 3421 | goto out_free_conn; |
7abea1e8 | 3422 | } |
221a6876 | 3423 | } |
8f9d3d3b | 3424 | move_to_confirmed(unconf); |
ec6b5d7b AA |
3425 | conf = unconf; |
3426 | } else { | |
3427 | status = nfserr_stale_clientid; | |
81f0b2a4 | 3428 | goto out_free_conn; |
ec6b5d7b | 3429 | } |
81f0b2a4 | 3430 | status = nfs_ok; |
4ce85c8c | 3431 | /* Persistent sessions are not supported */ |
408b79bc | 3432 | cr_ses->flags &= ~SESSION4_PERSIST; |
4ce85c8c | 3433 | /* Upshifting from TCP to RDMA is not supported */ |
408b79bc BF |
3434 | cr_ses->flags &= ~SESSION4_RDMA; |
3435 | ||
81f0b2a4 | 3436 | init_session(rqstp, new, conf, cr_ses); |
d20c11d8 | 3437 | nfsd4_get_session_locked(new); |
81f0b2a4 | 3438 | |
ac7c46f2 | 3439 | memcpy(cr_ses->sessionid.data, new->se_sessionid.data, |
ec6b5d7b | 3440 | NFS4_MAX_SESSIONID_LEN); |
86c3e16c | 3441 | cs_slot->sl_seqid++; |
49557cc7 | 3442 | cr_ses->seqid = cs_slot->sl_seqid; |
ec6b5d7b | 3443 | |
d20c11d8 | 3444 | /* cache solo and embedded create sessions under the client_lock */ |
49557cc7 | 3445 | nfsd4_cache_create_session(cr_ses, cs_slot, status); |
d20c11d8 JL |
3446 | spin_unlock(&nn->client_lock); |
3447 | /* init connection and backchannel */ | |
3448 | nfsd4_init_conn(rqstp, conn, new); | |
3449 | nfsd4_put_session(new); | |
d20c11d8 JL |
3450 | if (old) |
3451 | expire_client(old); | |
ec6b5d7b | 3452 | return status; |
81f0b2a4 | 3453 | out_free_conn: |
d20c11d8 | 3454 | spin_unlock(&nn->client_lock); |
81f0b2a4 | 3455 | free_conn(conn); |
d20c11d8 JL |
3456 | if (old) |
3457 | expire_client(old); | |
81f0b2a4 BF |
3458 | out_free_session: |
3459 | __free_session(new); | |
55c760cf BF |
3460 | out_release_drc_mem: |
3461 | nfsd4_put_drc_mem(&cr_ses->fore_channel); | |
1ca50792 | 3462 | return status; |
069b6ad4 AA |
3463 | } |
3464 | ||
1d1bc8f2 BF |
3465 | static __be32 nfsd4_map_bcts_dir(u32 *dir) |
3466 | { | |
3467 | switch (*dir) { | |
3468 | case NFS4_CDFC4_FORE: | |
3469 | case NFS4_CDFC4_BACK: | |
3470 | return nfs_ok; | |
3471 | case NFS4_CDFC4_FORE_OR_BOTH: | |
3472 | case NFS4_CDFC4_BACK_OR_BOTH: | |
3473 | *dir = NFS4_CDFC4_BOTH; | |
3474 | return nfs_ok; | |
fc5fc5d7 | 3475 | } |
1d1bc8f2 BF |
3476 | return nfserr_inval; |
3477 | } | |
3478 | ||
eb69853d CH |
3479 | __be32 nfsd4_backchannel_ctl(struct svc_rqst *rqstp, |
3480 | struct nfsd4_compound_state *cstate, | |
3481 | union nfsd4_op_u *u) | |
cb73a9f4 | 3482 | { |
eb69853d | 3483 | struct nfsd4_backchannel_ctl *bc = &u->backchannel_ctl; |
cb73a9f4 | 3484 | struct nfsd4_session *session = cstate->session; |
c9a49628 | 3485 | struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id); |
b78724b7 | 3486 | __be32 status; |
cb73a9f4 | 3487 | |
b78724b7 BF |
3488 | status = nfsd4_check_cb_sec(&bc->bc_cb_sec); |
3489 | if (status) | |
3490 | return status; | |
c9a49628 | 3491 | spin_lock(&nn->client_lock); |
cb73a9f4 BF |
3492 | session->se_cb_prog = bc->bc_cb_program; |
3493 | session->se_cb_sec = bc->bc_cb_sec; | |
c9a49628 | 3494 | spin_unlock(&nn->client_lock); |
cb73a9f4 BF |
3495 | |
3496 | nfsd4_probe_callback(session->se_client); | |
3497 | ||
3498 | return nfs_ok; | |
3499 | } | |
3500 | ||
c2d715a1 BF |
3501 | static struct nfsd4_conn *__nfsd4_find_conn(struct svc_xprt *xpt, struct nfsd4_session *s) |
3502 | { | |
3503 | struct nfsd4_conn *c; | |
3504 | ||
3505 | list_for_each_entry(c, &s->se_conns, cn_persession) { | |
3506 | if (c->cn_xprt == xpt) { | |
3507 | return c; | |
3508 | } | |
3509 | } | |
3510 | return NULL; | |
3511 | } | |
3512 | ||
3513 | static __be32 nfsd4_match_existing_connection(struct svc_rqst *rqst, | |
3514 | struct nfsd4_session *session, u32 req) | |
3515 | { | |
3516 | struct nfs4_client *clp = session->se_client; | |
3517 | struct svc_xprt *xpt = rqst->rq_xprt; | |
3518 | struct nfsd4_conn *c; | |
3519 | __be32 status; | |
3520 | ||
3521 | /* Following the last paragraph of RFC 5661 Section 18.34.3: */ | |
3522 | spin_lock(&clp->cl_lock); | |
3523 | c = __nfsd4_find_conn(xpt, session); | |
3524 | if (!c) | |
3525 | status = nfserr_noent; | |
3526 | else if (req == c->cn_flags) | |
3527 | status = nfs_ok; | |
3528 | else if (req == NFS4_CDFC4_FORE_OR_BOTH && | |
3529 | c->cn_flags != NFS4_CDFC4_BACK) | |
3530 | status = nfs_ok; | |
3531 | else if (req == NFS4_CDFC4_BACK_OR_BOTH && | |
3532 | c->cn_flags != NFS4_CDFC4_FORE) | |
3533 | status = nfs_ok; | |
3534 | else | |
3535 | status = nfserr_inval; | |
3536 | spin_unlock(&clp->cl_lock); | |
3537 | return status; | |
3538 | } | |
3539 | ||
1d1bc8f2 BF |
3540 | __be32 nfsd4_bind_conn_to_session(struct svc_rqst *rqstp, |
3541 | struct nfsd4_compound_state *cstate, | |
eb69853d | 3542 | union nfsd4_op_u *u) |
1d1bc8f2 | 3543 | { |
eb69853d | 3544 | struct nfsd4_bind_conn_to_session *bcts = &u->bind_conn_to_session; |
1d1bc8f2 | 3545 | __be32 status; |
3ba63671 | 3546 | struct nfsd4_conn *conn; |
4f6e6c17 | 3547 | struct nfsd4_session *session; |
d4e19e70 TM |
3548 | struct net *net = SVC_NET(rqstp); |
3549 | struct nfsd_net *nn = net_generic(net, nfsd_net_id); | |
1d1bc8f2 BF |
3550 | |
3551 | if (!nfsd4_last_compound_op(rqstp)) | |
3552 | return nfserr_not_only_op; | |
c9a49628 | 3553 | spin_lock(&nn->client_lock); |
d4e19e70 | 3554 | session = find_in_sessionid_hashtbl(&bcts->sessionid, net, &status); |
c9a49628 | 3555 | spin_unlock(&nn->client_lock); |
4f6e6c17 | 3556 | if (!session) |
d4e19e70 | 3557 | goto out_no_session; |
57266a6e | 3558 | status = nfserr_wrong_cred; |
dedeb13f | 3559 | if (!nfsd4_mach_creds_match(session->se_client, rqstp)) |
57266a6e | 3560 | goto out; |
c2d715a1 BF |
3561 | status = nfsd4_match_existing_connection(rqstp, session, bcts->dir); |
3562 | if (status == nfs_ok || status == nfserr_inval) | |
3563 | goto out; | |
1d1bc8f2 | 3564 | status = nfsd4_map_bcts_dir(&bcts->dir); |
3ba63671 | 3565 | if (status) |
4f6e6c17 | 3566 | goto out; |
3ba63671 | 3567 | conn = alloc_conn(rqstp, bcts->dir); |
4f6e6c17 | 3568 | status = nfserr_jukebox; |
3ba63671 | 3569 | if (!conn) |
4f6e6c17 BF |
3570 | goto out; |
3571 | nfsd4_init_conn(rqstp, conn, session); | |
3572 | status = nfs_ok; | |
3573 | out: | |
d4e19e70 TM |
3574 | nfsd4_put_session(session); |
3575 | out_no_session: | |
4f6e6c17 | 3576 | return status; |
1d1bc8f2 BF |
3577 | } |
3578 | ||
665d5072 | 3579 | static bool nfsd4_compound_in_session(struct nfsd4_compound_state *cstate, struct nfs4_sessionid *sid) |
5d4cec2f | 3580 | { |
665d5072 | 3581 | if (!cstate->session) |
51d87bc2 | 3582 | return false; |
665d5072 | 3583 | return !memcmp(sid, &cstate->session->se_sessionid, sizeof(*sid)); |
5d4cec2f BF |
3584 | } |
3585 | ||
069b6ad4 | 3586 | __be32 |
eb69853d CH |
3587 | nfsd4_destroy_session(struct svc_rqst *r, struct nfsd4_compound_state *cstate, |
3588 | union nfsd4_op_u *u) | |
069b6ad4 | 3589 | { |
ca0552f4 | 3590 | struct nfs4_sessionid *sessionid = &u->destroy_session.sessionid; |
e10e0cfc | 3591 | struct nfsd4_session *ses; |
abcdff09 | 3592 | __be32 status; |
f0f51f5c | 3593 | int ref_held_by_me = 0; |
d4e19e70 TM |
3594 | struct net *net = SVC_NET(r); |
3595 | struct nfsd_net *nn = net_generic(net, nfsd_net_id); | |
e10e0cfc | 3596 | |
abcdff09 | 3597 | status = nfserr_not_only_op; |
ca0552f4 | 3598 | if (nfsd4_compound_in_session(cstate, sessionid)) { |
57716355 | 3599 | if (!nfsd4_last_compound_op(r)) |
abcdff09 | 3600 | goto out; |
f0f51f5c | 3601 | ref_held_by_me++; |
57716355 | 3602 | } |
ca0552f4 | 3603 | dump_sessionid(__func__, sessionid); |
c9a49628 | 3604 | spin_lock(&nn->client_lock); |
ca0552f4 | 3605 | ses = find_in_sessionid_hashtbl(sessionid, net, &status); |
abcdff09 BF |
3606 | if (!ses) |
3607 | goto out_client_lock; | |
57266a6e | 3608 | status = nfserr_wrong_cred; |
dedeb13f | 3609 | if (!nfsd4_mach_creds_match(ses->se_client, r)) |
d4e19e70 | 3610 | goto out_put_session; |
f0f51f5c | 3611 | status = mark_session_dead_locked(ses, 1 + ref_held_by_me); |
66b2b9b2 | 3612 | if (status) |
f0f51f5c | 3613 | goto out_put_session; |
e10e0cfc | 3614 | unhash_session(ses); |
c9a49628 | 3615 | spin_unlock(&nn->client_lock); |
e10e0cfc | 3616 | |
84f5f7cc | 3617 | nfsd4_probe_callback_sync(ses->se_client); |
19cf5c02 | 3618 | |
c9a49628 | 3619 | spin_lock(&nn->client_lock); |
e10e0cfc | 3620 | status = nfs_ok; |
f0f51f5c | 3621 | out_put_session: |
d4e19e70 | 3622 | nfsd4_put_session_locked(ses); |
abcdff09 BF |
3623 | out_client_lock: |
3624 | spin_unlock(&nn->client_lock); | |
e10e0cfc | 3625 | out: |
e10e0cfc | 3626 | return status; |
069b6ad4 AA |
3627 | } |
3628 | ||
57266a6e | 3629 | static __be32 nfsd4_sequence_check_conn(struct nfsd4_conn *new, struct nfsd4_session *ses) |
328ead28 BF |
3630 | { |
3631 | struct nfs4_client *clp = ses->se_client; | |
a663bdd8 | 3632 | struct nfsd4_conn *c; |
57266a6e | 3633 | __be32 status = nfs_ok; |
21b75b01 | 3634 | int ret; |
328ead28 BF |
3635 | |
3636 | spin_lock(&clp->cl_lock); | |
a663bdd8 | 3637 | c = __nfsd4_find_conn(new->cn_xprt, ses); |
57266a6e BF |
3638 | if (c) |
3639 | goto out_free; | |
3640 | status = nfserr_conn_not_bound_to_session; | |
3641 | if (clp->cl_mach_cred) | |
3642 | goto out_free; | |
328ead28 BF |
3643 | __nfsd4_hash_conn(new, ses); |
3644 | spin_unlock(&clp->cl_lock); | |
21b75b01 BF |
3645 | ret = nfsd4_register_conn(new); |
3646 | if (ret) | |
3647 | /* oops; xprt is already down: */ | |
3648 | nfsd4_conn_lost(&new->cn_xpt_user); | |
57266a6e BF |
3649 | return nfs_ok; |
3650 | out_free: | |
3651 | spin_unlock(&clp->cl_lock); | |
3652 | free_conn(new); | |
3653 | return status; | |
328ead28 BF |
3654 | } |
3655 | ||
868b89c3 MJ |
3656 | static bool nfsd4_session_too_many_ops(struct svc_rqst *rqstp, struct nfsd4_session *session) |
3657 | { | |
3658 | struct nfsd4_compoundargs *args = rqstp->rq_argp; | |
3659 | ||
3660 | return args->opcnt > session->se_fchannel.maxops; | |
3661 | } | |
3662 | ||
ae82a8d0 MJ |
3663 | static bool nfsd4_request_too_big(struct svc_rqst *rqstp, |
3664 | struct nfsd4_session *session) | |
3665 | { | |
3666 | struct xdr_buf *xb = &rqstp->rq_arg; | |
3667 | ||
3668 | return xb->len > session->se_fchannel.maxreq_sz; | |
3669 | } | |
3670 | ||
53da6a53 BF |
3671 | static bool replay_matches_cache(struct svc_rqst *rqstp, |
3672 | struct nfsd4_sequence *seq, struct nfsd4_slot *slot) | |
3673 | { | |
3674 | struct nfsd4_compoundargs *argp = rqstp->rq_argp; | |
3675 | ||
3676 | if ((bool)(slot->sl_flags & NFSD4_SLOT_CACHETHIS) != | |
3677 | (bool)seq->cachethis) | |
3678 | return false; | |
3679 | /* | |
6e73e92b SM |
3680 | * If there's an error then the reply can have fewer ops than |
3681 | * the call. | |
53da6a53 | 3682 | */ |
6e73e92b SM |
3683 | if (slot->sl_opcnt < argp->opcnt && !slot->sl_status) |
3684 | return false; | |
3685 | /* | |
3686 | * But if we cached a reply with *more* ops than the call you're | |
3687 | * sending us now, then this new call is clearly not really a | |
3688 | * replay of the old one: | |
3689 | */ | |
3690 | if (slot->sl_opcnt > argp->opcnt) | |
53da6a53 BF |
3691 | return false; |
3692 | /* This is the only check explicitly called by spec: */ | |
3693 | if (!same_creds(&rqstp->rq_cred, &slot->sl_cred)) | |
3694 | return false; | |
3695 | /* | |
3696 | * There may be more comparisons we could actually do, but the | |
3697 | * spec doesn't require us to catch every case where the calls | |
3698 | * don't match (that would require caching the call as well as | |
3699 | * the reply), so we don't bother. | |
3700 | */ | |
3701 | return true; | |
3702 | } | |
3703 | ||
069b6ad4 | 3704 | __be32 |
eb69853d CH |
3705 | nfsd4_sequence(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, |
3706 | union nfsd4_op_u *u) | |
069b6ad4 | 3707 | { |
eb69853d | 3708 | struct nfsd4_sequence *seq = &u->sequence; |
f9bb94c4 | 3709 | struct nfsd4_compoundres *resp = rqstp->rq_resp; |
47ee5298 | 3710 | struct xdr_stream *xdr = &resp->xdr; |
b85d4c01 | 3711 | struct nfsd4_session *session; |
221a6876 | 3712 | struct nfs4_client *clp; |
b85d4c01 | 3713 | struct nfsd4_slot *slot; |
a663bdd8 | 3714 | struct nfsd4_conn *conn; |
57b7b43b | 3715 | __be32 status; |
47ee5298 | 3716 | int buflen; |
d4e19e70 TM |
3717 | struct net *net = SVC_NET(rqstp); |
3718 | struct nfsd_net *nn = net_generic(net, nfsd_net_id); | |
b85d4c01 | 3719 | |
f9bb94c4 AA |
3720 | if (resp->opcnt != 1) |
3721 | return nfserr_sequence_pos; | |
3722 | ||
a663bdd8 BF |
3723 | /* |
3724 | * Will be either used or freed by nfsd4_sequence_check_conn | |
3725 | * below. | |
3726 | */ | |
3727 | conn = alloc_conn(rqstp, NFS4_CDFC4_FORE); | |
3728 | if (!conn) | |
3729 | return nfserr_jukebox; | |
3730 | ||
c9a49628 | 3731 | spin_lock(&nn->client_lock); |
d4e19e70 | 3732 | session = find_in_sessionid_hashtbl(&seq->sessionid, net, &status); |
b85d4c01 | 3733 | if (!session) |
221a6876 BF |
3734 | goto out_no_session; |
3735 | clp = session->se_client; | |
b85d4c01 | 3736 | |
868b89c3 MJ |
3737 | status = nfserr_too_many_ops; |
3738 | if (nfsd4_session_too_many_ops(rqstp, session)) | |
66b2b9b2 | 3739 | goto out_put_session; |
868b89c3 | 3740 | |
ae82a8d0 MJ |
3741 | status = nfserr_req_too_big; |
3742 | if (nfsd4_request_too_big(rqstp, session)) | |
66b2b9b2 | 3743 | goto out_put_session; |
ae82a8d0 | 3744 | |
b85d4c01 | 3745 | status = nfserr_badslot; |
6c18ba9f | 3746 | if (seq->slotid >= session->se_fchannel.maxreqs) |
66b2b9b2 | 3747 | goto out_put_session; |
b85d4c01 | 3748 | |
557ce264 | 3749 | slot = session->se_slots[seq->slotid]; |
b85d4c01 BH |
3750 | dprintk("%s: slotid %d\n", __func__, seq->slotid); |
3751 | ||
a8dfdaeb AA |
3752 | /* We do not negotiate the number of slots yet, so set the |
3753 | * maxslots to the session maxreqs which is used to encode | |
3754 | * sr_highest_slotid and the sr_target_slot id to maxslots */ | |
3755 | seq->maxslots = session->se_fchannel.maxreqs; | |
3756 | ||
73e79482 BF |
3757 | status = check_slot_seqid(seq->seqid, slot->sl_seqid, |
3758 | slot->sl_flags & NFSD4_SLOT_INUSE); | |
b85d4c01 | 3759 | if (status == nfserr_replay_cache) { |
bf5c43c8 BF |
3760 | status = nfserr_seq_misordered; |
3761 | if (!(slot->sl_flags & NFSD4_SLOT_INITIALIZED)) | |
66b2b9b2 | 3762 | goto out_put_session; |
53da6a53 BF |
3763 | status = nfserr_seq_false_retry; |
3764 | if (!replay_matches_cache(rqstp, seq, slot)) | |
3765 | goto out_put_session; | |
b85d4c01 BH |
3766 | cstate->slot = slot; |
3767 | cstate->session = session; | |
4b24ca7d | 3768 | cstate->clp = clp; |
da3846a2 | 3769 | /* Return the cached reply status and set cstate->status |
557ce264 | 3770 | * for nfsd4_proc_compound processing */ |
bf864a31 | 3771 | status = nfsd4_replay_cache_entry(resp, seq); |
da3846a2 | 3772 | cstate->status = nfserr_replay_cache; |
aaf84eb9 | 3773 | goto out; |
b85d4c01 BH |
3774 | } |
3775 | if (status) | |
66b2b9b2 | 3776 | goto out_put_session; |
b85d4c01 | 3777 | |
57266a6e | 3778 | status = nfsd4_sequence_check_conn(conn, session); |
a663bdd8 | 3779 | conn = NULL; |
57266a6e BF |
3780 | if (status) |
3781 | goto out_put_session; | |
328ead28 | 3782 | |
47ee5298 BF |
3783 | buflen = (seq->cachethis) ? |
3784 | session->se_fchannel.maxresp_cached : | |
3785 | session->se_fchannel.maxresp_sz; | |
3786 | status = (seq->cachethis) ? nfserr_rep_too_big_to_cache : | |
3787 | nfserr_rep_too_big; | |
a5cddc88 | 3788 | if (xdr_restrict_buflen(xdr, buflen - rqstp->rq_auth_slack)) |
47ee5298 | 3789 | goto out_put_session; |
32aaa62e | 3790 | svc_reserve(rqstp, buflen); |
47ee5298 BF |
3791 | |
3792 | status = nfs_ok; | |
b85d4c01 | 3793 | /* Success! bump slot seqid */ |
b85d4c01 | 3794 | slot->sl_seqid = seq->seqid; |
bf5c43c8 | 3795 | slot->sl_flags |= NFSD4_SLOT_INUSE; |
73e79482 BF |
3796 | if (seq->cachethis) |
3797 | slot->sl_flags |= NFSD4_SLOT_CACHETHIS; | |
bf5c43c8 BF |
3798 | else |
3799 | slot->sl_flags &= ~NFSD4_SLOT_CACHETHIS; | |
b85d4c01 BH |
3800 | |
3801 | cstate->slot = slot; | |
3802 | cstate->session = session; | |
4b24ca7d | 3803 | cstate->clp = clp; |
b85d4c01 | 3804 | |
b85d4c01 | 3805 | out: |
221a6876 BF |
3806 | switch (clp->cl_cb_state) { |
3807 | case NFSD4_CB_DOWN: | |
3808 | seq->status_flags = SEQ4_STATUS_CB_PATH_DOWN; | |
3809 | break; | |
3810 | case NFSD4_CB_FAULT: | |
3811 | seq->status_flags = SEQ4_STATUS_BACKCHANNEL_FAULT; | |
3812 | break; | |
3813 | default: | |
3814 | seq->status_flags = 0; | |
aaf84eb9 | 3815 | } |
3bd64a5b BF |
3816 | if (!list_empty(&clp->cl_revoked)) |
3817 | seq->status_flags |= SEQ4_STATUS_RECALLABLE_STATE_REVOKED; | |
221a6876 | 3818 | out_no_session: |
3f42d2c4 KM |
3819 | if (conn) |
3820 | free_conn(conn); | |
c9a49628 | 3821 | spin_unlock(&nn->client_lock); |
b85d4c01 | 3822 | return status; |
66b2b9b2 | 3823 | out_put_session: |
d4e19e70 | 3824 | nfsd4_put_session_locked(session); |
221a6876 | 3825 | goto out_no_session; |
069b6ad4 AA |
3826 | } |
3827 | ||
b607664e TM |
3828 | void |
3829 | nfsd4_sequence_done(struct nfsd4_compoundres *resp) | |
3830 | { | |
3831 | struct nfsd4_compound_state *cs = &resp->cstate; | |
3832 | ||
3833 | if (nfsd4_has_session(cs)) { | |
b607664e TM |
3834 | if (cs->status != nfserr_replay_cache) { |
3835 | nfsd4_store_cache_entry(resp); | |
3836 | cs->slot->sl_flags &= ~NFSD4_SLOT_INUSE; | |
3837 | } | |
d4e19e70 | 3838 | /* Drop session reference that was taken in nfsd4_sequence() */ |
b607664e | 3839 | nfsd4_put_session(cs->session); |
4b24ca7d JL |
3840 | } else if (cs->clp) |
3841 | put_client_renew(cs->clp); | |
b607664e TM |
3842 | } |
3843 | ||
345c2842 | 3844 | __be32 |
eb69853d CH |
3845 | nfsd4_destroy_clientid(struct svc_rqst *rqstp, |
3846 | struct nfsd4_compound_state *cstate, | |
3847 | union nfsd4_op_u *u) | |
345c2842 | 3848 | { |
eb69853d | 3849 | struct nfsd4_destroy_clientid *dc = &u->destroy_clientid; |
6b10ad19 TM |
3850 | struct nfs4_client *conf, *unconf; |
3851 | struct nfs4_client *clp = NULL; | |
57b7b43b | 3852 | __be32 status = 0; |
8daae4dc | 3853 | struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id); |
345c2842 | 3854 | |
6b10ad19 | 3855 | spin_lock(&nn->client_lock); |
0a7ec377 | 3856 | unconf = find_unconfirmed_client(&dc->clientid, true, nn); |
8daae4dc | 3857 | conf = find_confirmed_client(&dc->clientid, true, nn); |
78389046 | 3858 | WARN_ON_ONCE(conf && unconf); |
345c2842 MJ |
3859 | |
3860 | if (conf) { | |
c0293b01 | 3861 | if (client_has_state(conf)) { |
345c2842 MJ |
3862 | status = nfserr_clientid_busy; |
3863 | goto out; | |
3864 | } | |
fd699b8a JL |
3865 | status = mark_client_expired_locked(conf); |
3866 | if (status) | |
3867 | goto out; | |
6b10ad19 | 3868 | clp = conf; |
345c2842 MJ |
3869 | } else if (unconf) |
3870 | clp = unconf; | |
3871 | else { | |
3872 | status = nfserr_stale_clientid; | |
3873 | goto out; | |
3874 | } | |
dedeb13f | 3875 | if (!nfsd4_mach_creds_match(clp, rqstp)) { |
6b10ad19 | 3876 | clp = NULL; |
57266a6e BF |
3877 | status = nfserr_wrong_cred; |
3878 | goto out; | |
3879 | } | |
6b10ad19 | 3880 | unhash_client_locked(clp); |
345c2842 | 3881 | out: |
6b10ad19 | 3882 | spin_unlock(&nn->client_lock); |
6b10ad19 TM |
3883 | if (clp) |
3884 | expire_client(clp); | |
345c2842 MJ |
3885 | return status; |
3886 | } | |
3887 | ||
4dc6ec00 | 3888 | __be32 |
eb69853d CH |
3889 | nfsd4_reclaim_complete(struct svc_rqst *rqstp, |
3890 | struct nfsd4_compound_state *cstate, union nfsd4_op_u *u) | |
4dc6ec00 | 3891 | { |
eb69853d | 3892 | struct nfsd4_reclaim_complete *rc = &u->reclaim_complete; |
57b7b43b | 3893 | __be32 status = 0; |
bcecf1cc | 3894 | |
4dc6ec00 BF |
3895 | if (rc->rca_one_fs) { |
3896 | if (!cstate->current_fh.fh_dentry) | |
3897 | return nfserr_nofilehandle; | |
3898 | /* | |
3899 | * We don't take advantage of the rca_one_fs case. | |
3900 | * That's OK, it's optional, we can safely ignore it. | |
3901 | */ | |
d28c442f | 3902 | return nfs_ok; |
4dc6ec00 | 3903 | } |
bcecf1cc | 3904 | |
bcecf1cc | 3905 | status = nfserr_complete_already; |
a52d726b JL |
3906 | if (test_and_set_bit(NFSD4_CLIENT_RECLAIM_COMPLETE, |
3907 | &cstate->session->se_client->cl_flags)) | |
bcecf1cc MJ |
3908 | goto out; |
3909 | ||
3910 | status = nfserr_stale_clientid; | |
3911 | if (is_client_expired(cstate->session->se_client)) | |
4dc6ec00 BF |
3912 | /* |
3913 | * The following error isn't really legal. | |
3914 | * But we only get here if the client just explicitly | |
3915 | * destroyed the client. Surely it no longer cares what | |
3916 | * error it gets back on an operation for the dead | |
3917 | * client. | |
3918 | */ | |
bcecf1cc MJ |
3919 | goto out; |
3920 | ||
3921 | status = nfs_ok; | |
2a4317c5 | 3922 | nfsd4_client_record_create(cstate->session->se_client); |
362063a5 | 3923 | inc_reclaim_complete(cstate->session->se_client); |
bcecf1cc | 3924 | out: |
bcecf1cc | 3925 | return status; |
4dc6ec00 BF |
3926 | } |
3927 | ||
b37ad28b | 3928 | __be32 |
b591480b | 3929 | nfsd4_setclientid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, |
eb69853d | 3930 | union nfsd4_op_u *u) |
1da177e4 | 3931 | { |
eb69853d | 3932 | struct nfsd4_setclientid *setclid = &u->setclientid; |
a084daf5 | 3933 | struct xdr_netobj clname = setclid->se_name; |
1da177e4 | 3934 | nfs4_verifier clverifier = setclid->se_verf; |
3dbacee6 TM |
3935 | struct nfs4_client *conf, *new; |
3936 | struct nfs4_client *unconf = NULL; | |
b37ad28b | 3937 | __be32 status; |
c212cecf SK |
3938 | struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id); |
3939 | ||
5cc40fd7 TM |
3940 | new = create_client(clname, rqstp, &clverifier); |
3941 | if (new == NULL) | |
3942 | return nfserr_jukebox; | |
63db4632 | 3943 | /* Cases below refer to rfc 3530 section 14.2.33: */ |
3dbacee6 | 3944 | spin_lock(&nn->client_lock); |
382a62e7 | 3945 | conf = find_confirmed_client_by_name(&clname, nn); |
2b634821 | 3946 | if (conf && client_has_state(conf)) { |
63db4632 | 3947 | /* case 0: */ |
1da177e4 | 3948 | status = nfserr_clid_inuse; |
e203d506 BF |
3949 | if (clp_used_exchangeid(conf)) |
3950 | goto out; | |
026722c2 | 3951 | if (!same_creds(&conf->cl_cred, &rqstp->rq_cred)) { |
dd5e3fbc | 3952 | trace_nfsd_clid_inuse_err(conf); |
1da177e4 LT |
3953 | goto out; |
3954 | } | |
1da177e4 | 3955 | } |
a99454aa | 3956 | unconf = find_unconfirmed_client_by_name(&clname, nn); |
8f930711 | 3957 | if (unconf) |
3dbacee6 | 3958 | unhash_client_locked(unconf); |
45f56da8 | 3959 | /* We need to handle only case 1: probable callback update */ |
41eb1670 | 3960 | if (conf && same_verf(&conf->cl_verifier, &clverifier)) { |
1da177e4 | 3961 | copy_clid(new, conf); |
41eb1670 | 3962 | gen_confirm(new, nn); |
45f56da8 | 3963 | } |
8323c3b2 | 3964 | new->cl_minorversion = 0; |
6f3d772f | 3965 | gen_callback(new, setclid, rqstp); |
ac55fdc4 | 3966 | add_to_unconfirmed(new); |
1da177e4 LT |
3967 | setclid->se_clientid.cl_boot = new->cl_clientid.cl_boot; |
3968 | setclid->se_clientid.cl_id = new->cl_clientid.cl_id; | |
3969 | memcpy(setclid->se_confirm.data, new->cl_confirm.data, sizeof(setclid->se_confirm.data)); | |
5cc40fd7 | 3970 | new = NULL; |
1da177e4 LT |
3971 | status = nfs_ok; |
3972 | out: | |
3dbacee6 | 3973 | spin_unlock(&nn->client_lock); |
5cc40fd7 TM |
3974 | if (new) |
3975 | free_client(new); | |
3dbacee6 TM |
3976 | if (unconf) |
3977 | expire_client(unconf); | |
1da177e4 LT |
3978 | return status; |
3979 | } | |
3980 | ||
3981 | ||
b37ad28b | 3982 | __be32 |
b591480b | 3983 | nfsd4_setclientid_confirm(struct svc_rqst *rqstp, |
eb69853d CH |
3984 | struct nfsd4_compound_state *cstate, |
3985 | union nfsd4_op_u *u) | |
1da177e4 | 3986 | { |
eb69853d CH |
3987 | struct nfsd4_setclientid_confirm *setclientid_confirm = |
3988 | &u->setclientid_confirm; | |
21ab45a4 | 3989 | struct nfs4_client *conf, *unconf; |
d20c11d8 | 3990 | struct nfs4_client *old = NULL; |
1da177e4 LT |
3991 | nfs4_verifier confirm = setclientid_confirm->sc_confirm; |
3992 | clientid_t * clid = &setclientid_confirm->sc_clientid; | |
b37ad28b | 3993 | __be32 status; |
7f2210fa | 3994 | struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id); |
1da177e4 | 3995 | |
2c142baa | 3996 | if (STALE_CLIENTID(clid, nn)) |
1da177e4 | 3997 | return nfserr_stale_clientid; |
21ab45a4 | 3998 | |
d20c11d8 | 3999 | spin_lock(&nn->client_lock); |
8daae4dc | 4000 | conf = find_confirmed_client(clid, false, nn); |
0a7ec377 | 4001 | unconf = find_unconfirmed_client(clid, false, nn); |
a186e767 | 4002 | /* |
8695b90a BF |
4003 | * We try hard to give out unique clientid's, so if we get an |
4004 | * attempt to confirm the same clientid with a different cred, | |
f984a7ce BF |
4005 | * the client may be buggy; this should never happen. |
4006 | * | |
4007 | * Nevertheless, RFC 7530 recommends INUSE for this case: | |
a186e767 | 4008 | */ |
f984a7ce | 4009 | status = nfserr_clid_inuse; |
8695b90a BF |
4010 | if (unconf && !same_creds(&unconf->cl_cred, &rqstp->rq_cred)) |
4011 | goto out; | |
4012 | if (conf && !same_creds(&conf->cl_cred, &rqstp->rq_cred)) | |
4013 | goto out; | |
63db4632 | 4014 | /* cases below refer to rfc 3530 section 14.2.34: */ |
90d700b7 | 4015 | if (!unconf || !same_verf(&confirm, &unconf->cl_confirm)) { |
7d22fc11 BF |
4016 | if (conf && same_verf(&confirm, &conf->cl_confirm)) { |
4017 | /* case 2: probable retransmit */ | |
1da177e4 | 4018 | status = nfs_ok; |
7d22fc11 | 4019 | } else /* case 4: client hasn't noticed we rebooted yet? */ |
90d700b7 BF |
4020 | status = nfserr_stale_clientid; |
4021 | goto out; | |
4022 | } | |
4023 | status = nfs_ok; | |
4024 | if (conf) { /* case 1: callback update */ | |
d20c11d8 JL |
4025 | old = unconf; |
4026 | unhash_client_locked(old); | |
8695b90a | 4027 | nfsd4_change_callback(conf, &unconf->cl_cb_conn); |
90d700b7 | 4028 | } else { /* case 3: normal case; new or rebooted client */ |
d20c11d8 JL |
4029 | old = find_confirmed_client_by_name(&unconf->cl_name, nn); |
4030 | if (old) { | |
2b634821 BF |
4031 | status = nfserr_clid_inuse; |
4032 | if (client_has_state(old) | |
4033 | && !same_creds(&unconf->cl_cred, | |
4034 | &old->cl_cred)) | |
4035 | goto out; | |
d20c11d8 | 4036 | status = mark_client_expired_locked(old); |
7abea1e8 JL |
4037 | if (status) { |
4038 | old = NULL; | |
221a6876 | 4039 | goto out; |
7abea1e8 | 4040 | } |
221a6876 | 4041 | } |
8695b90a | 4042 | move_to_confirmed(unconf); |
d20c11d8 | 4043 | conf = unconf; |
08e8987c | 4044 | } |
d20c11d8 JL |
4045 | get_client_locked(conf); |
4046 | spin_unlock(&nn->client_lock); | |
4047 | nfsd4_probe_callback(conf); | |
4048 | spin_lock(&nn->client_lock); | |
4049 | put_client_renew_locked(conf); | |
1da177e4 | 4050 | out: |
d20c11d8 JL |
4051 | spin_unlock(&nn->client_lock); |
4052 | if (old) | |
4053 | expire_client(old); | |
1da177e4 LT |
4054 | return status; |
4055 | } | |
4056 | ||
32513b40 BF |
4057 | static struct nfs4_file *nfsd4_alloc_file(void) |
4058 | { | |
4059 | return kmem_cache_alloc(file_slab, GFP_KERNEL); | |
4060 | } | |
4061 | ||
1da177e4 | 4062 | /* OPEN Share state helper functions */ |
5b095e99 JL |
4063 | static void nfsd4_init_file(struct knfsd_fh *fh, unsigned int hashval, |
4064 | struct nfs4_file *fp) | |
1da177e4 | 4065 | { |
950e0118 TM |
4066 | lockdep_assert_held(&state_lock); |
4067 | ||
818a34eb | 4068 | refcount_set(&fp->fi_ref, 1); |
1d31a253 | 4069 | spin_lock_init(&fp->fi_lock); |
32513b40 BF |
4070 | INIT_LIST_HEAD(&fp->fi_stateids); |
4071 | INIT_LIST_HEAD(&fp->fi_delegations); | |
8287f009 | 4072 | INIT_LIST_HEAD(&fp->fi_clnt_odstate); |
e2cf80d7 | 4073 | fh_copy_shallow(&fp->fi_fhandle, fh); |
0c637be8 | 4074 | fp->fi_deleg_file = NULL; |
32513b40 | 4075 | fp->fi_had_conflict = false; |
baeb4ff0 | 4076 | fp->fi_share_deny = 0; |
32513b40 BF |
4077 | memset(fp->fi_fds, 0, sizeof(fp->fi_fds)); |
4078 | memset(fp->fi_access, 0, sizeof(fp->fi_access)); | |
9cf514cc CH |
4079 | #ifdef CONFIG_NFSD_PNFS |
4080 | INIT_LIST_HEAD(&fp->fi_lo_states); | |
c5c707f9 | 4081 | atomic_set(&fp->fi_lo_recalls, 0); |
9cf514cc | 4082 | #endif |
5b095e99 | 4083 | hlist_add_head_rcu(&fp->fi_hash, &file_hashtbl[hashval]); |
1da177e4 LT |
4084 | } |
4085 | ||
e8ff2a84 | 4086 | void |
1da177e4 LT |
4087 | nfsd4_free_slabs(void) |
4088 | { | |
9258a2d5 | 4089 | kmem_cache_destroy(client_slab); |
abf1135b CH |
4090 | kmem_cache_destroy(openowner_slab); |
4091 | kmem_cache_destroy(lockowner_slab); | |
4092 | kmem_cache_destroy(file_slab); | |
4093 | kmem_cache_destroy(stateid_slab); | |
4094 | kmem_cache_destroy(deleg_slab); | |
9258a2d5 | 4095 | kmem_cache_destroy(odstate_slab); |
e60d4398 | 4096 | } |
1da177e4 | 4097 | |
72083396 | 4098 | int |
e60d4398 N |
4099 | nfsd4_init_slabs(void) |
4100 | { | |
9258a2d5 JL |
4101 | client_slab = kmem_cache_create("nfsd4_clients", |
4102 | sizeof(struct nfs4_client), 0, 0, NULL); | |
4103 | if (client_slab == NULL) | |
4104 | goto out; | |
fe0750e5 BF |
4105 | openowner_slab = kmem_cache_create("nfsd4_openowners", |
4106 | sizeof(struct nfs4_openowner), 0, 0, NULL); | |
4107 | if (openowner_slab == NULL) | |
9258a2d5 | 4108 | goto out_free_client_slab; |
fe0750e5 | 4109 | lockowner_slab = kmem_cache_create("nfsd4_lockowners", |
3c40794b | 4110 | sizeof(struct nfs4_lockowner), 0, 0, NULL); |
fe0750e5 | 4111 | if (lockowner_slab == NULL) |
abf1135b | 4112 | goto out_free_openowner_slab; |
e60d4398 | 4113 | file_slab = kmem_cache_create("nfsd4_files", |
20c2df83 | 4114 | sizeof(struct nfs4_file), 0, 0, NULL); |
e60d4398 | 4115 | if (file_slab == NULL) |
abf1135b | 4116 | goto out_free_lockowner_slab; |
5ac049ac | 4117 | stateid_slab = kmem_cache_create("nfsd4_stateids", |
dcef0413 | 4118 | sizeof(struct nfs4_ol_stateid), 0, 0, NULL); |
5ac049ac | 4119 | if (stateid_slab == NULL) |
abf1135b | 4120 | goto out_free_file_slab; |
5b2d21c1 | 4121 | deleg_slab = kmem_cache_create("nfsd4_delegations", |
20c2df83 | 4122 | sizeof(struct nfs4_delegation), 0, 0, NULL); |
5b2d21c1 | 4123 | if (deleg_slab == NULL) |
abf1135b | 4124 | goto out_free_stateid_slab; |
8287f009 SB |
4125 | odstate_slab = kmem_cache_create("nfsd4_odstate", |
4126 | sizeof(struct nfs4_clnt_odstate), 0, 0, NULL); | |
4127 | if (odstate_slab == NULL) | |
4128 | goto out_free_deleg_slab; | |
e60d4398 | 4129 | return 0; |
abf1135b | 4130 | |
8287f009 SB |
4131 | out_free_deleg_slab: |
4132 | kmem_cache_destroy(deleg_slab); | |
abf1135b CH |
4133 | out_free_stateid_slab: |
4134 | kmem_cache_destroy(stateid_slab); | |
4135 | out_free_file_slab: | |
4136 | kmem_cache_destroy(file_slab); | |
4137 | out_free_lockowner_slab: | |
4138 | kmem_cache_destroy(lockowner_slab); | |
4139 | out_free_openowner_slab: | |
4140 | kmem_cache_destroy(openowner_slab); | |
9258a2d5 JL |
4141 | out_free_client_slab: |
4142 | kmem_cache_destroy(client_slab); | |
abf1135b | 4143 | out: |
e60d4398 | 4144 | return -ENOMEM; |
1da177e4 LT |
4145 | } |
4146 | ||
ff194bd9 | 4147 | static void init_nfs4_replay(struct nfs4_replay *rp) |
1da177e4 | 4148 | { |
ff194bd9 BF |
4149 | rp->rp_status = nfserr_serverfault; |
4150 | rp->rp_buflen = 0; | |
4151 | rp->rp_buf = rp->rp_ibuf; | |
58fb12e6 JL |
4152 | mutex_init(&rp->rp_mutex); |
4153 | } | |
4154 | ||
4155 | static void nfsd4_cstate_assign_replay(struct nfsd4_compound_state *cstate, | |
4156 | struct nfs4_stateowner *so) | |
4157 | { | |
4158 | if (!nfsd4_has_session(cstate)) { | |
4159 | mutex_lock(&so->so_replay.rp_mutex); | |
b5971afa | 4160 | cstate->replay_owner = nfs4_get_stateowner(so); |
58fb12e6 JL |
4161 | } |
4162 | } | |
4163 | ||
4164 | void nfsd4_cstate_clear_replay(struct nfsd4_compound_state *cstate) | |
4165 | { | |
4166 | struct nfs4_stateowner *so = cstate->replay_owner; | |
4167 | ||
4168 | if (so != NULL) { | |
4169 | cstate->replay_owner = NULL; | |
4170 | mutex_unlock(&so->so_replay.rp_mutex); | |
4171 | nfs4_put_stateowner(so); | |
4172 | } | |
1da177e4 LT |
4173 | } |
4174 | ||
fe0750e5 | 4175 | static inline void *alloc_stateowner(struct kmem_cache *slab, struct xdr_netobj *owner, struct nfs4_client *clp) |
ff194bd9 | 4176 | { |
1da177e4 | 4177 | struct nfs4_stateowner *sop; |
1da177e4 | 4178 | |
fe0750e5 | 4179 | sop = kmem_cache_alloc(slab, GFP_KERNEL); |
ff194bd9 BF |
4180 | if (!sop) |
4181 | return NULL; | |
4182 | ||
6f4859b8 | 4183 | xdr_netobj_dup(&sop->so_owner, owner, GFP_KERNEL); |
ff194bd9 | 4184 | if (!sop->so_owner.data) { |
fe0750e5 | 4185 | kmem_cache_free(slab, sop); |
1da177e4 | 4186 | return NULL; |
ff194bd9 | 4187 | } |
ff194bd9 | 4188 | |
ea1da636 | 4189 | INIT_LIST_HEAD(&sop->so_stateids); |
ff194bd9 BF |
4190 | sop->so_client = clp; |
4191 | init_nfs4_replay(&sop->so_replay); | |
6b180f0b | 4192 | atomic_set(&sop->so_count, 1); |
ff194bd9 BF |
4193 | return sop; |
4194 | } | |
4195 | ||
fe0750e5 | 4196 | static void hash_openowner(struct nfs4_openowner *oo, struct nfs4_client *clp, unsigned int strhashval) |
ff194bd9 | 4197 | { |
d4f0489f | 4198 | lockdep_assert_held(&clp->cl_lock); |
9b531137 | 4199 | |
d4f0489f TM |
4200 | list_add(&oo->oo_owner.so_strhash, |
4201 | &clp->cl_ownerstr_hashtbl[strhashval]); | |
fe0750e5 | 4202 | list_add(&oo->oo_perclient, &clp->cl_openowners); |
ff194bd9 BF |
4203 | } |
4204 | ||
8f4b54c5 JL |
4205 | static void nfs4_unhash_openowner(struct nfs4_stateowner *so) |
4206 | { | |
d4f0489f | 4207 | unhash_openowner_locked(openowner(so)); |
8f4b54c5 JL |
4208 | } |
4209 | ||
6b180f0b JL |
4210 | static void nfs4_free_openowner(struct nfs4_stateowner *so) |
4211 | { | |
4212 | struct nfs4_openowner *oo = openowner(so); | |
4213 | ||
4214 | kmem_cache_free(openowner_slab, oo); | |
4215 | } | |
4216 | ||
4217 | static const struct nfs4_stateowner_operations openowner_ops = { | |
8f4b54c5 JL |
4218 | .so_unhash = nfs4_unhash_openowner, |
4219 | .so_free = nfs4_free_openowner, | |
6b180f0b JL |
4220 | }; |
4221 | ||
7fc0564e AE |
4222 | static struct nfs4_ol_stateid * |
4223 | nfsd4_find_existing_open(struct nfs4_file *fp, struct nfsd4_open *open) | |
4224 | { | |
4225 | struct nfs4_ol_stateid *local, *ret = NULL; | |
4226 | struct nfs4_openowner *oo = open->op_openowner; | |
4227 | ||
4228 | lockdep_assert_held(&fp->fi_lock); | |
4229 | ||
4230 | list_for_each_entry(local, &fp->fi_stateids, st_perfile) { | |
4231 | /* ignore lock owners */ | |
4232 | if (local->st_stateowner->so_is_open_owner == 0) | |
4233 | continue; | |
15ca08d3 TM |
4234 | if (local->st_stateowner != &oo->oo_owner) |
4235 | continue; | |
4236 | if (local->st_stid.sc_type == NFS4_OPEN_STID) { | |
7fc0564e | 4237 | ret = local; |
a15dfcd5 | 4238 | refcount_inc(&ret->st_stid.sc_count); |
7fc0564e AE |
4239 | break; |
4240 | } | |
4241 | } | |
4242 | return ret; | |
4243 | } | |
4244 | ||
15ca08d3 TM |
4245 | static __be32 |
4246 | nfsd4_verify_open_stid(struct nfs4_stid *s) | |
4247 | { | |
4248 | __be32 ret = nfs_ok; | |
4249 | ||
4250 | switch (s->sc_type) { | |
4251 | default: | |
4252 | break; | |
4f176417 | 4253 | case 0: |
15ca08d3 TM |
4254 | case NFS4_CLOSED_STID: |
4255 | case NFS4_CLOSED_DELEG_STID: | |
4256 | ret = nfserr_bad_stateid; | |
4257 | break; | |
4258 | case NFS4_REVOKED_DELEG_STID: | |
4259 | ret = nfserr_deleg_revoked; | |
4260 | } | |
4261 | return ret; | |
4262 | } | |
4263 | ||
4264 | /* Lock the stateid st_mutex, and deal with races with CLOSE */ | |
4265 | static __be32 | |
4266 | nfsd4_lock_ol_stateid(struct nfs4_ol_stateid *stp) | |
4267 | { | |
4268 | __be32 ret; | |
4269 | ||
4f34bd05 | 4270 | mutex_lock_nested(&stp->st_mutex, LOCK_STATEID_MUTEX); |
15ca08d3 TM |
4271 | ret = nfsd4_verify_open_stid(&stp->st_stid); |
4272 | if (ret != nfs_ok) | |
4273 | mutex_unlock(&stp->st_mutex); | |
4274 | return ret; | |
4275 | } | |
4276 | ||
4277 | static struct nfs4_ol_stateid * | |
4278 | nfsd4_find_and_lock_existing_open(struct nfs4_file *fp, struct nfsd4_open *open) | |
4279 | { | |
4280 | struct nfs4_ol_stateid *stp; | |
4281 | for (;;) { | |
4282 | spin_lock(&fp->fi_lock); | |
4283 | stp = nfsd4_find_existing_open(fp, open); | |
4284 | spin_unlock(&fp->fi_lock); | |
4285 | if (!stp || nfsd4_lock_ol_stateid(stp) == nfs_ok) | |
4286 | break; | |
4287 | nfs4_put_stid(&stp->st_stid); | |
4288 | } | |
4289 | return stp; | |
4290 | } | |
4291 | ||
fe0750e5 | 4292 | static struct nfs4_openowner * |
13d6f66b | 4293 | alloc_init_open_stateowner(unsigned int strhashval, struct nfsd4_open *open, |
db24b3b4 JL |
4294 | struct nfsd4_compound_state *cstate) |
4295 | { | |
13d6f66b | 4296 | struct nfs4_client *clp = cstate->clp; |
7ffb5880 | 4297 | struct nfs4_openowner *oo, *ret; |
ff194bd9 | 4298 | |
fe0750e5 BF |
4299 | oo = alloc_stateowner(openowner_slab, &open->op_owner, clp); |
4300 | if (!oo) | |
ff194bd9 | 4301 | return NULL; |
6b180f0b | 4302 | oo->oo_owner.so_ops = &openowner_ops; |
fe0750e5 BF |
4303 | oo->oo_owner.so_is_open_owner = 1; |
4304 | oo->oo_owner.so_seqid = open->op_seqid; | |
d3134b10 | 4305 | oo->oo_flags = 0; |
db24b3b4 JL |
4306 | if (nfsd4_has_session(cstate)) |
4307 | oo->oo_flags |= NFS4_OO_CONFIRMED; | |
fe0750e5 | 4308 | oo->oo_time = 0; |
38c387b5 | 4309 | oo->oo_last_closed_stid = NULL; |
fe0750e5 | 4310 | INIT_LIST_HEAD(&oo->oo_close_lru); |
d4f0489f TM |
4311 | spin_lock(&clp->cl_lock); |
4312 | ret = find_openstateowner_str_locked(strhashval, open, clp); | |
7ffb5880 TM |
4313 | if (ret == NULL) { |
4314 | hash_openowner(oo, clp, strhashval); | |
4315 | ret = oo; | |
4316 | } else | |
d50ffded KM |
4317 | nfs4_free_stateowner(&oo->oo_owner); |
4318 | ||
d4f0489f | 4319 | spin_unlock(&clp->cl_lock); |
c5952338 | 4320 | return ret; |
1da177e4 LT |
4321 | } |
4322 | ||
7fc0564e | 4323 | static struct nfs4_ol_stateid * |
8c7245ab | 4324 | init_open_stateid(struct nfs4_file *fp, struct nfsd4_open *open) |
7fc0564e AE |
4325 | { |
4326 | ||
fe0750e5 | 4327 | struct nfs4_openowner *oo = open->op_openowner; |
7fc0564e | 4328 | struct nfs4_ol_stateid *retstp = NULL; |
8c7245ab | 4329 | struct nfs4_ol_stateid *stp; |
1da177e4 | 4330 | |
8c7245ab | 4331 | stp = open->op_stp; |
5cc1fb2a OD |
4332 | /* We are moving these outside of the spinlocks to avoid the warnings */ |
4333 | mutex_init(&stp->st_mutex); | |
4f34bd05 | 4334 | mutex_lock_nested(&stp->st_mutex, OPEN_STATEID_MUTEX); |
5cc1fb2a | 4335 | |
15ca08d3 | 4336 | retry: |
7fc0564e AE |
4337 | spin_lock(&oo->oo_owner.so_client->cl_lock); |
4338 | spin_lock(&fp->fi_lock); | |
4339 | ||
4340 | retstp = nfsd4_find_existing_open(fp, open); | |
4341 | if (retstp) | |
4342 | goto out_unlock; | |
8c7245ab OD |
4343 | |
4344 | open->op_stp = NULL; | |
a15dfcd5 | 4345 | refcount_inc(&stp->st_stid.sc_count); |
3abdb607 | 4346 | stp->st_stid.sc_type = NFS4_OPEN_STID; |
3c87b9b7 | 4347 | INIT_LIST_HEAD(&stp->st_locks); |
b5971afa | 4348 | stp->st_stateowner = nfs4_get_stateowner(&oo->oo_owner); |
13cd2184 | 4349 | get_nfs4_file(fp); |
11b9164a | 4350 | stp->st_stid.sc_file = fp; |
1da177e4 LT |
4351 | stp->st_access_bmap = 0; |
4352 | stp->st_deny_bmap = 0; | |
4c4cd222 | 4353 | stp->st_openstp = NULL; |
1c755dc1 | 4354 | list_add(&stp->st_perstateowner, &oo->oo_owner.so_stateids); |
1d31a253 | 4355 | list_add(&stp->st_perfile, &fp->fi_stateids); |
7fc0564e AE |
4356 | |
4357 | out_unlock: | |
1d31a253 | 4358 | spin_unlock(&fp->fi_lock); |
1c755dc1 | 4359 | spin_unlock(&oo->oo_owner.so_client->cl_lock); |
5cc1fb2a | 4360 | if (retstp) { |
15ca08d3 TM |
4361 | /* Handle races with CLOSE */ |
4362 | if (nfsd4_lock_ol_stateid(retstp) != nfs_ok) { | |
4363 | nfs4_put_stid(&retstp->st_stid); | |
4364 | goto retry; | |
4365 | } | |
8c7245ab | 4366 | /* To keep mutex tracking happy */ |
5cc1fb2a | 4367 | mutex_unlock(&stp->st_mutex); |
8c7245ab | 4368 | stp = retstp; |
5cc1fb2a | 4369 | } |
8c7245ab | 4370 | return stp; |
1da177e4 LT |
4371 | } |
4372 | ||
d3134b10 JL |
4373 | /* |
4374 | * In the 4.0 case we need to keep the owners around a little while to handle | |
4375 | * CLOSE replay. We still do need to release any file access that is held by | |
4376 | * them before returning however. | |
4377 | */ | |
fd39ca9a | 4378 | static void |
d3134b10 | 4379 | move_to_close_lru(struct nfs4_ol_stateid *s, struct net *net) |
1da177e4 | 4380 | { |
217526e7 | 4381 | struct nfs4_ol_stateid *last; |
d3134b10 JL |
4382 | struct nfs4_openowner *oo = openowner(s->st_stateowner); |
4383 | struct nfsd_net *nn = net_generic(s->st_stid.sc_client->net, | |
4384 | nfsd_net_id); | |
73758fed | 4385 | |
fe0750e5 | 4386 | dprintk("NFSD: move_to_close_lru nfs4_openowner %p\n", oo); |
1da177e4 | 4387 | |
b401be22 JL |
4388 | /* |
4389 | * We know that we hold one reference via nfsd4_close, and another | |
4390 | * "persistent" reference for the client. If the refcount is higher | |
4391 | * than 2, then there are still calls in progress that are using this | |
4392 | * stateid. We can't put the sc_file reference until they are finished. | |
4393 | * Wait for the refcount to drop to 2. Since it has been unhashed, | |
4394 | * there should be no danger of the refcount going back up again at | |
4395 | * this point. | |
4396 | */ | |
a15dfcd5 | 4397 | wait_event(close_wq, refcount_read(&s->st_stid.sc_count) == 2); |
b401be22 | 4398 | |
d3134b10 JL |
4399 | release_all_access(s); |
4400 | if (s->st_stid.sc_file) { | |
4401 | put_nfs4_file(s->st_stid.sc_file); | |
4402 | s->st_stid.sc_file = NULL; | |
4403 | } | |
217526e7 JL |
4404 | |
4405 | spin_lock(&nn->client_lock); | |
4406 | last = oo->oo_last_closed_stid; | |
d3134b10 | 4407 | oo->oo_last_closed_stid = s; |
73758fed | 4408 | list_move_tail(&oo->oo_close_lru, &nn->close_lru); |
20b7d86f | 4409 | oo->oo_time = ktime_get_boottime_seconds(); |
217526e7 JL |
4410 | spin_unlock(&nn->client_lock); |
4411 | if (last) | |
4412 | nfs4_put_stid(&last->st_stid); | |
1da177e4 LT |
4413 | } |
4414 | ||
1da177e4 LT |
4415 | /* search file_hashtbl[] for file */ |
4416 | static struct nfs4_file * | |
5b095e99 | 4417 | find_file_locked(struct knfsd_fh *fh, unsigned int hashval) |
1da177e4 | 4418 | { |
1da177e4 LT |
4419 | struct nfs4_file *fp; |
4420 | ||
36a80491 MB |
4421 | hlist_for_each_entry_rcu(fp, &file_hashtbl[hashval], fi_hash, |
4422 | lockdep_is_held(&state_lock)) { | |
4d94c2ef | 4423 | if (fh_match(&fp->fi_fhandle, fh)) { |
818a34eb | 4424 | if (refcount_inc_not_zero(&fp->fi_ref)) |
5b095e99 | 4425 | return fp; |
13cd2184 | 4426 | } |
1da177e4 LT |
4427 | } |
4428 | return NULL; | |
4429 | } | |
4430 | ||
e6ba76e1 | 4431 | struct nfs4_file * |
ca943217 | 4432 | find_file(struct knfsd_fh *fh) |
950e0118 TM |
4433 | { |
4434 | struct nfs4_file *fp; | |
5b095e99 | 4435 | unsigned int hashval = file_hashval(fh); |
950e0118 | 4436 | |
5b095e99 JL |
4437 | rcu_read_lock(); |
4438 | fp = find_file_locked(fh, hashval); | |
4439 | rcu_read_unlock(); | |
950e0118 TM |
4440 | return fp; |
4441 | } | |
4442 | ||
4443 | static struct nfs4_file * | |
f9c00c3a | 4444 | find_or_add_file(struct nfs4_file *new, struct knfsd_fh *fh) |
950e0118 TM |
4445 | { |
4446 | struct nfs4_file *fp; | |
5b095e99 JL |
4447 | unsigned int hashval = file_hashval(fh); |
4448 | ||
4449 | rcu_read_lock(); | |
4450 | fp = find_file_locked(fh, hashval); | |
4451 | rcu_read_unlock(); | |
4452 | if (fp) | |
4453 | return fp; | |
950e0118 TM |
4454 | |
4455 | spin_lock(&state_lock); | |
5b095e99 JL |
4456 | fp = find_file_locked(fh, hashval); |
4457 | if (likely(fp == NULL)) { | |
4458 | nfsd4_init_file(fh, hashval, new); | |
950e0118 TM |
4459 | fp = new; |
4460 | } | |
4461 | spin_unlock(&state_lock); | |
4462 | ||
4463 | return fp; | |
4464 | } | |
4465 | ||
1da177e4 LT |
4466 | /* |
4467 | * Called to check deny when READ with all zero stateid or | |
4468 | * WRITE with all zero or all one stateid | |
4469 | */ | |
b37ad28b | 4470 | static __be32 |
1da177e4 LT |
4471 | nfs4_share_conflict(struct svc_fh *current_fh, unsigned int deny_type) |
4472 | { | |
1da177e4 | 4473 | struct nfs4_file *fp; |
baeb4ff0 | 4474 | __be32 ret = nfs_ok; |
1da177e4 | 4475 | |
ca943217 | 4476 | fp = find_file(¤t_fh->fh_handle); |
13cd2184 | 4477 | if (!fp) |
baeb4ff0 JL |
4478 | return ret; |
4479 | /* Check for conflicting share reservations */ | |
1d31a253 | 4480 | spin_lock(&fp->fi_lock); |
baeb4ff0 JL |
4481 | if (fp->fi_share_deny & deny_type) |
4482 | ret = nfserr_locked; | |
1d31a253 | 4483 | spin_unlock(&fp->fi_lock); |
13cd2184 N |
4484 | put_nfs4_file(fp); |
4485 | return ret; | |
1da177e4 LT |
4486 | } |
4487 | ||
0162ac2b | 4488 | static void nfsd4_cb_recall_prepare(struct nfsd4_callback *cb) |
1da177e4 | 4489 | { |
0162ac2b | 4490 | struct nfs4_delegation *dp = cb_to_delegation(cb); |
11b9164a TM |
4491 | struct nfsd_net *nn = net_generic(dp->dl_stid.sc_client->net, |
4492 | nfsd_net_id); | |
e8c69d17 | 4493 | |
11b9164a | 4494 | block_delegations(&dp->dl_stid.sc_file->fi_fhandle); |
f54fe962 | 4495 | |
dff1399f | 4496 | /* |
f54fe962 JL |
4497 | * We can't do this in nfsd_break_deleg_cb because it is |
4498 | * already holding inode->i_lock. | |
4499 | * | |
dff1399f JL |
4500 | * If the dl_time != 0, then we know that it has already been |
4501 | * queued for a lease break. Don't queue it again. | |
4502 | */ | |
f54fe962 | 4503 | spin_lock(&state_lock); |
dff1399f | 4504 | if (dp->dl_time == 0) { |
20b7d86f | 4505 | dp->dl_time = ktime_get_boottime_seconds(); |
02e1215f | 4506 | list_add_tail(&dp->dl_recall_lru, &nn->del_recall_lru); |
dff1399f | 4507 | } |
02e1215f JL |
4508 | spin_unlock(&state_lock); |
4509 | } | |
1da177e4 | 4510 | |
0162ac2b CH |
4511 | static int nfsd4_cb_recall_done(struct nfsd4_callback *cb, |
4512 | struct rpc_task *task) | |
4513 | { | |
4514 | struct nfs4_delegation *dp = cb_to_delegation(cb); | |
4515 | ||
12ed22f3 BF |
4516 | if (dp->dl_stid.sc_type == NFS4_CLOSED_DELEG_STID || |
4517 | dp->dl_stid.sc_type == NFS4_REVOKED_DELEG_STID) | |
a457974f AE |
4518 | return 1; |
4519 | ||
0162ac2b CH |
4520 | switch (task->tk_status) { |
4521 | case 0: | |
4522 | return 1; | |
1c73b9d2 SM |
4523 | case -NFS4ERR_DELAY: |
4524 | rpc_delay(task, 2 * HZ); | |
4525 | return 0; | |
0162ac2b CH |
4526 | case -EBADHANDLE: |
4527 | case -NFS4ERR_BAD_STATEID: | |
4528 | /* | |
4529 | * Race: client probably got cb_recall before open reply | |
4530 | * granting delegation. | |
4531 | */ | |
4532 | if (dp->dl_retries--) { | |
4533 | rpc_delay(task, 2 * HZ); | |
4534 | return 0; | |
4535 | } | |
df561f66 | 4536 | fallthrough; |
0162ac2b | 4537 | default: |
1c73b9d2 | 4538 | return 1; |
0162ac2b CH |
4539 | } |
4540 | } | |
4541 | ||
4542 | static void nfsd4_cb_recall_release(struct nfsd4_callback *cb) | |
4543 | { | |
4544 | struct nfs4_delegation *dp = cb_to_delegation(cb); | |
4545 | ||
4546 | nfs4_put_stid(&dp->dl_stid); | |
4547 | } | |
4548 | ||
c4cb8974 | 4549 | static const struct nfsd4_callback_ops nfsd4_cb_recall_ops = { |
0162ac2b CH |
4550 | .prepare = nfsd4_cb_recall_prepare, |
4551 | .done = nfsd4_cb_recall_done, | |
4552 | .release = nfsd4_cb_recall_release, | |
4553 | }; | |
4554 | ||
02e1215f JL |
4555 | static void nfsd_break_one_deleg(struct nfs4_delegation *dp) |
4556 | { | |
4557 | /* | |
4558 | * We're assuming the state code never drops its reference | |
4559 | * without first removing the lease. Since we're in this lease | |
4a269efb BF |
4560 | * callback (and since the lease code is serialized by the |
4561 | * i_lock) we know the server hasn't removed the lease yet, and | |
4562 | * we know it's safe to take a reference. | |
02e1215f | 4563 | */ |
a15dfcd5 | 4564 | refcount_inc(&dp->dl_stid.sc_count); |
f0b5de1b | 4565 | nfsd4_run_cb(&dp->dl_recall); |
6b57d9c8 BF |
4566 | } |
4567 | ||
1c8c601a | 4568 | /* Called from break_lease() with i_lock held. */ |
4d01b7f5 JL |
4569 | static bool |
4570 | nfsd_break_deleg_cb(struct file_lock *fl) | |
6b57d9c8 | 4571 | { |
4d01b7f5 | 4572 | bool ret = false; |
653e514e BF |
4573 | struct nfs4_delegation *dp = (struct nfs4_delegation *)fl->fl_owner; |
4574 | struct nfs4_file *fp = dp->dl_stid.sc_file; | |
6b57d9c8 | 4575 | |
dd5e3fbc CL |
4576 | trace_nfsd_deleg_break(&dp->dl_stid.sc_stateid); |
4577 | ||
0272e1fd BF |
4578 | /* |
4579 | * We don't want the locks code to timeout the lease for us; | |
acfdf5c3 | 4580 | * we'll remove it ourself if a delegation isn't returned |
6b57d9c8 | 4581 | * in time: |
0272e1fd BF |
4582 | */ |
4583 | fl->fl_break_time = 0; | |
1da177e4 | 4584 | |
02e1215f | 4585 | spin_lock(&fp->fi_lock); |
417c6629 | 4586 | fp->fi_had_conflict = true; |
353601e7 | 4587 | nfsd_break_one_deleg(dp); |
02e1215f | 4588 | spin_unlock(&fp->fi_lock); |
4d01b7f5 | 4589 | return ret; |
1da177e4 LT |
4590 | } |
4591 | ||
28df3d15 BF |
4592 | static bool nfsd_breaker_owns_lease(struct file_lock *fl) |
4593 | { | |
4594 | struct nfs4_delegation *dl = fl->fl_owner; | |
4595 | struct svc_rqst *rqst; | |
4596 | struct nfs4_client *clp; | |
4597 | ||
4598 | if (!i_am_nfsd()) | |
4599 | return NULL; | |
4600 | rqst = kthread_data(current); | |
34b09af4 BF |
4601 | if (!rqst->rq_lease_breaker) |
4602 | return NULL; | |
28df3d15 BF |
4603 | clp = *(rqst->rq_lease_breaker); |
4604 | return dl->dl_stid.sc_client == clp; | |
4605 | } | |
4606 | ||
c45198ed | 4607 | static int |
7448cc37 JL |
4608 | nfsd_change_deleg_cb(struct file_lock *onlist, int arg, |
4609 | struct list_head *dispose) | |
1da177e4 LT |
4610 | { |
4611 | if (arg & F_UNLCK) | |
c45198ed | 4612 | return lease_modify(onlist, arg, dispose); |
1da177e4 LT |
4613 | else |
4614 | return -EAGAIN; | |
4615 | } | |
4616 | ||
7b021967 | 4617 | static const struct lock_manager_operations nfsd_lease_mng_ops = { |
28df3d15 | 4618 | .lm_breaker_owns_lease = nfsd_breaker_owns_lease, |
8fb47a4f BF |
4619 | .lm_break = nfsd_break_deleg_cb, |
4620 | .lm_change = nfsd_change_deleg_cb, | |
1da177e4 LT |
4621 | }; |
4622 | ||
7a8711c9 BF |
4623 | static __be32 nfsd4_check_seqid(struct nfsd4_compound_state *cstate, struct nfs4_stateowner *so, u32 seqid) |
4624 | { | |
4625 | if (nfsd4_has_session(cstate)) | |
4626 | return nfs_ok; | |
4627 | if (seqid == so->so_seqid - 1) | |
4628 | return nfserr_replay_me; | |
4629 | if (seqid == so->so_seqid) | |
4630 | return nfs_ok; | |
4631 | return nfserr_bad_seqid; | |
4632 | } | |
1da177e4 | 4633 | |
4b24ca7d JL |
4634 | static __be32 lookup_clientid(clientid_t *clid, |
4635 | struct nfsd4_compound_state *cstate, | |
b7342204 OK |
4636 | struct nfsd_net *nn, |
4637 | bool sessions) | |
4b24ca7d JL |
4638 | { |
4639 | struct nfs4_client *found; | |
4640 | ||
4641 | if (cstate->clp) { | |
4642 | found = cstate->clp; | |
4643 | if (!same_clid(&found->cl_clientid, clid)) | |
4644 | return nfserr_stale_clientid; | |
4645 | return nfs_ok; | |
4646 | } | |
4647 | ||
4648 | if (STALE_CLIENTID(clid, nn)) | |
4649 | return nfserr_stale_clientid; | |
4650 | ||
4651 | /* | |
4652 | * For v4.1+ we get the client in the SEQUENCE op. If we don't have one | |
4653 | * cached already then we know this is for is for v4.0 and "sessions" | |
4654 | * will be false. | |
4655 | */ | |
4656 | WARN_ON_ONCE(cstate->session); | |
3e339f96 | 4657 | spin_lock(&nn->client_lock); |
b7342204 | 4658 | found = find_confirmed_client(clid, sessions, nn); |
3e339f96 TM |
4659 | if (!found) { |
4660 | spin_unlock(&nn->client_lock); | |
4b24ca7d | 4661 | return nfserr_expired; |
3e339f96 | 4662 | } |
14ed14cc | 4663 | atomic_inc(&found->cl_rpc_users); |
3e339f96 | 4664 | spin_unlock(&nn->client_lock); |
4b24ca7d JL |
4665 | |
4666 | /* Cache the nfs4_client in cstate! */ | |
4667 | cstate->clp = found; | |
4b24ca7d JL |
4668 | return nfs_ok; |
4669 | } | |
4670 | ||
b37ad28b | 4671 | __be32 |
6668958f | 4672 | nfsd4_process_open1(struct nfsd4_compound_state *cstate, |
3320fef1 | 4673 | struct nfsd4_open *open, struct nfsd_net *nn) |
1da177e4 | 4674 | { |
1da177e4 LT |
4675 | clientid_t *clientid = &open->op_clientid; |
4676 | struct nfs4_client *clp = NULL; | |
4677 | unsigned int strhashval; | |
fe0750e5 | 4678 | struct nfs4_openowner *oo = NULL; |
4cdc951b | 4679 | __be32 status; |
1da177e4 | 4680 | |
2c142baa | 4681 | if (STALE_CLIENTID(&open->op_clientid, nn)) |
1da177e4 | 4682 | return nfserr_stale_clientid; |
32513b40 BF |
4683 | /* |
4684 | * In case we need it later, after we've already created the | |
4685 | * file and don't want to risk a further failure: | |
4686 | */ | |
4687 | open->op_file = nfsd4_alloc_file(); | |
4688 | if (open->op_file == NULL) | |
4689 | return nfserr_jukebox; | |
1da177e4 | 4690 | |
b7342204 | 4691 | status = lookup_clientid(clientid, cstate, nn, false); |
2d91e895 TM |
4692 | if (status) |
4693 | return status; | |
4694 | clp = cstate->clp; | |
4695 | ||
d4f0489f TM |
4696 | strhashval = ownerstr_hashval(&open->op_owner); |
4697 | oo = find_openstateowner_str(strhashval, open, clp); | |
fe0750e5 BF |
4698 | open->op_openowner = oo; |
4699 | if (!oo) { | |
bcf130f9 | 4700 | goto new_owner; |
1da177e4 | 4701 | } |
dad1c067 | 4702 | if (!(oo->oo_flags & NFS4_OO_CONFIRMED)) { |
0f442aa2 | 4703 | /* Replace unconfirmed owners without checking for replay. */ |
fe0750e5 BF |
4704 | release_openowner(oo); |
4705 | open->op_openowner = NULL; | |
bcf130f9 | 4706 | goto new_owner; |
0f442aa2 | 4707 | } |
4cdc951b BF |
4708 | status = nfsd4_check_seqid(cstate, &oo->oo_owner, open->op_seqid); |
4709 | if (status) | |
4710 | return status; | |
4cdc951b | 4711 | goto alloc_stateid; |
bcf130f9 | 4712 | new_owner: |
13d6f66b | 4713 | oo = alloc_init_open_stateowner(strhashval, open, cstate); |
bcf130f9 BF |
4714 | if (oo == NULL) |
4715 | return nfserr_jukebox; | |
4716 | open->op_openowner = oo; | |
4cdc951b | 4717 | alloc_stateid: |
b49e084d | 4718 | open->op_stp = nfs4_alloc_open_stateid(clp); |
4cdc951b BF |
4719 | if (!open->op_stp) |
4720 | return nfserr_jukebox; | |
8287f009 SB |
4721 | |
4722 | if (nfsd4_has_session(cstate) && | |
4723 | (cstate->current_fh.fh_export->ex_flags & NFSEXP_PNFS)) { | |
4724 | open->op_odstate = alloc_clnt_odstate(clp); | |
4725 | if (!open->op_odstate) | |
4726 | return nfserr_jukebox; | |
4727 | } | |
4728 | ||
0f442aa2 | 4729 | return nfs_ok; |
1da177e4 LT |
4730 | } |
4731 | ||
b37ad28b | 4732 | static inline __be32 |
4a6e43e6 N |
4733 | nfs4_check_delegmode(struct nfs4_delegation *dp, int flags) |
4734 | { | |
4735 | if ((flags & WR_STATE) && (dp->dl_type == NFS4_OPEN_DELEGATE_READ)) | |
4736 | return nfserr_openmode; | |
4737 | else | |
4738 | return nfs_ok; | |
4739 | } | |
4740 | ||
f459e453 | 4741 | static int share_access_to_flags(u32 share_access) |
52f4fb43 | 4742 | { |
f459e453 | 4743 | return share_access == NFS4_SHARE_ACCESS_READ ? RD_STATE : WR_STATE; |
52f4fb43 N |
4744 | } |
4745 | ||
38c2f4b1 | 4746 | static struct nfs4_delegation *find_deleg_stateid(struct nfs4_client *cl, stateid_t *s) |
24a0111e | 4747 | { |
f459e453 | 4748 | struct nfs4_stid *ret; |
24a0111e | 4749 | |
95da1b3a AE |
4750 | ret = find_stateid_by_type(cl, s, |
4751 | NFS4_DELEG_STID|NFS4_REVOKED_DELEG_STID); | |
f459e453 BF |
4752 | if (!ret) |
4753 | return NULL; | |
4754 | return delegstateid(ret); | |
24a0111e BF |
4755 | } |
4756 | ||
8b289b2c BF |
4757 | static bool nfsd4_is_deleg_cur(struct nfsd4_open *open) |
4758 | { | |
4759 | return open->op_claim_type == NFS4_OPEN_CLAIM_DELEGATE_CUR || | |
4760 | open->op_claim_type == NFS4_OPEN_CLAIM_DELEG_CUR_FH; | |
4761 | } | |
4762 | ||
b37ad28b | 4763 | static __be32 |
41d22663 | 4764 | nfs4_check_deleg(struct nfs4_client *cl, struct nfsd4_open *open, |
567d9829 N |
4765 | struct nfs4_delegation **dp) |
4766 | { | |
4767 | int flags; | |
b37ad28b | 4768 | __be32 status = nfserr_bad_stateid; |
dcd94cc2 | 4769 | struct nfs4_delegation *deleg; |
567d9829 | 4770 | |
dcd94cc2 TM |
4771 | deleg = find_deleg_stateid(cl, &open->op_delegate_stateid); |
4772 | if (deleg == NULL) | |
c44c5eeb | 4773 | goto out; |
95da1b3a AE |
4774 | if (deleg->dl_stid.sc_type == NFS4_REVOKED_DELEG_STID) { |
4775 | nfs4_put_stid(&deleg->dl_stid); | |
4776 | if (cl->cl_minorversion) | |
4777 | status = nfserr_deleg_revoked; | |
4778 | goto out; | |
4779 | } | |
24a0111e | 4780 | flags = share_access_to_flags(open->op_share_access); |
dcd94cc2 TM |
4781 | status = nfs4_check_delegmode(deleg, flags); |
4782 | if (status) { | |
4783 | nfs4_put_stid(&deleg->dl_stid); | |
4784 | goto out; | |
4785 | } | |
4786 | *dp = deleg; | |
c44c5eeb | 4787 | out: |
8b289b2c | 4788 | if (!nfsd4_is_deleg_cur(open)) |
c44c5eeb N |
4789 | return nfs_ok; |
4790 | if (status) | |
4791 | return status; | |
dad1c067 | 4792 | open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED; |
c44c5eeb | 4793 | return nfs_ok; |
567d9829 N |
4794 | } |
4795 | ||
21fb4016 BF |
4796 | static inline int nfs4_access_to_access(u32 nfs4_access) |
4797 | { | |
4798 | int flags = 0; | |
4799 | ||
4800 | if (nfs4_access & NFS4_SHARE_ACCESS_READ) | |
4801 | flags |= NFSD_MAY_READ; | |
4802 | if (nfs4_access & NFS4_SHARE_ACCESS_WRITE) | |
4803 | flags |= NFSD_MAY_WRITE; | |
4804 | return flags; | |
4805 | } | |
4806 | ||
7e6a72e5 CH |
4807 | static inline __be32 |
4808 | nfsd4_truncate(struct svc_rqst *rqstp, struct svc_fh *fh, | |
4809 | struct nfsd4_open *open) | |
4810 | { | |
4811 | struct iattr iattr = { | |
4812 | .ia_valid = ATTR_SIZE, | |
4813 | .ia_size = 0, | |
4814 | }; | |
4815 | if (!open->op_truncate) | |
4816 | return 0; | |
4817 | if (!(open->op_share_access & NFS4_SHARE_ACCESS_WRITE)) | |
4818 | return nfserr_inval; | |
2a1aa489 | 4819 | return nfsd_setattr(rqstp, fh, &iattr, 0, (time64_t)0); |
7e6a72e5 CH |
4820 | } |
4821 | ||
0c12eaff | 4822 | static __be32 nfs4_get_vfs_file(struct svc_rqst *rqstp, struct nfs4_file *fp, |
6eb3a1d0 JL |
4823 | struct svc_fh *cur_fh, struct nfs4_ol_stateid *stp, |
4824 | struct nfsd4_open *open) | |
f9d7562f | 4825 | { |
fd4f83fd | 4826 | struct nfsd_file *nf = NULL; |
f9d7562f | 4827 | __be32 status; |
0c12eaff CB |
4828 | int oflag = nfs4_access_to_omode(open->op_share_access); |
4829 | int access = nfs4_access_to_access(open->op_share_access); | |
baeb4ff0 | 4830 | unsigned char old_access_bmap, old_deny_bmap; |
0c12eaff | 4831 | |
de18643d | 4832 | spin_lock(&fp->fi_lock); |
baeb4ff0 JL |
4833 | |
4834 | /* | |
4835 | * Are we trying to set a deny mode that would conflict with | |
4836 | * current access? | |
4837 | */ | |
4838 | status = nfs4_file_check_deny(fp, open->op_share_deny); | |
4839 | if (status != nfs_ok) { | |
4840 | spin_unlock(&fp->fi_lock); | |
4841 | goto out; | |
4842 | } | |
4843 | ||
4844 | /* set access to the file */ | |
4845 | status = nfs4_file_get_access(fp, open->op_share_access); | |
4846 | if (status != nfs_ok) { | |
4847 | spin_unlock(&fp->fi_lock); | |
4848 | goto out; | |
4849 | } | |
4850 | ||
4851 | /* Set access bits in stateid */ | |
4852 | old_access_bmap = stp->st_access_bmap; | |
4853 | set_access(open->op_share_access, stp); | |
4854 | ||
4855 | /* Set new deny mask */ | |
4856 | old_deny_bmap = stp->st_deny_bmap; | |
4857 | set_deny(open->op_share_deny, stp); | |
4858 | fp->fi_share_deny |= (open->op_share_deny & NFS4_SHARE_DENY_BOTH); | |
4859 | ||
f9d7562f | 4860 | if (!fp->fi_fds[oflag]) { |
de18643d | 4861 | spin_unlock(&fp->fi_lock); |
fd4f83fd | 4862 | status = nfsd_file_acquire(rqstp, cur_fh, access, &nf); |
f9d7562f | 4863 | if (status) |
baeb4ff0 | 4864 | goto out_put_access; |
de18643d TM |
4865 | spin_lock(&fp->fi_lock); |
4866 | if (!fp->fi_fds[oflag]) { | |
fd4f83fd JL |
4867 | fp->fi_fds[oflag] = nf; |
4868 | nf = NULL; | |
de18643d | 4869 | } |
f9d7562f | 4870 | } |
de18643d | 4871 | spin_unlock(&fp->fi_lock); |
fd4f83fd JL |
4872 | if (nf) |
4873 | nfsd_file_put(nf); | |
f9d7562f | 4874 | |
7e6a72e5 CH |
4875 | status = nfsd4_truncate(rqstp, cur_fh, open); |
4876 | if (status) | |
4877 | goto out_put_access; | |
7e6a72e5 CH |
4878 | out: |
4879 | return status; | |
baeb4ff0 JL |
4880 | out_put_access: |
4881 | stp->st_access_bmap = old_access_bmap; | |
4882 | nfs4_file_put_access(fp, open->op_share_access); | |
4883 | reset_union_bmap_deny(bmap_to_share_mode(old_deny_bmap), stp); | |
4884 | goto out; | |
1da177e4 LT |
4885 | } |
4886 | ||
b37ad28b | 4887 | static __be32 |
dcef0413 | 4888 | nfs4_upgrade_open(struct svc_rqst *rqstp, struct nfs4_file *fp, struct svc_fh *cur_fh, struct nfs4_ol_stateid *stp, struct nfsd4_open *open) |
1da177e4 | 4889 | { |
b37ad28b | 4890 | __be32 status; |
6ac75368 | 4891 | unsigned char old_deny_bmap = stp->st_deny_bmap; |
1da177e4 | 4892 | |
6eb3a1d0 | 4893 | if (!test_access(open->op_share_access, stp)) |
baeb4ff0 | 4894 | return nfs4_get_vfs_file(rqstp, fp, cur_fh, stp, open); |
7e6a72e5 | 4895 | |
baeb4ff0 JL |
4896 | /* test and set deny mode */ |
4897 | spin_lock(&fp->fi_lock); | |
4898 | status = nfs4_file_check_deny(fp, open->op_share_deny); | |
4899 | if (status == nfs_ok) { | |
baeb4ff0 JL |
4900 | set_deny(open->op_share_deny, stp); |
4901 | fp->fi_share_deny |= | |
4902 | (open->op_share_deny & NFS4_SHARE_DENY_BOTH); | |
4903 | } | |
4904 | spin_unlock(&fp->fi_lock); | |
4905 | ||
4906 | if (status != nfs_ok) | |
1da177e4 | 4907 | return status; |
1da177e4 | 4908 | |
baeb4ff0 JL |
4909 | status = nfsd4_truncate(rqstp, cur_fh, open); |
4910 | if (status != nfs_ok) | |
4911 | reset_union_bmap_deny(old_deny_bmap, stp); | |
4912 | return status; | |
4913 | } | |
1da177e4 | 4914 | |
14a24e99 BF |
4915 | /* Should we give out recallable state?: */ |
4916 | static bool nfsd4_cb_channel_good(struct nfs4_client *clp) | |
4917 | { | |
4918 | if (clp->cl_cb_state == NFSD4_CB_UP) | |
4919 | return true; | |
4920 | /* | |
4921 | * In the sessions case, since we don't have to establish a | |
4922 | * separate connection for callbacks, we assume it's OK | |
4923 | * until we hear otherwise: | |
4924 | */ | |
4925 | return clp->cl_minorversion && clp->cl_cb_state == NFSD4_CB_UNKNOWN; | |
4926 | } | |
4927 | ||
653e514e BF |
4928 | static struct file_lock *nfs4_alloc_init_lease(struct nfs4_delegation *dp, |
4929 | int flag) | |
22d38c4c BF |
4930 | { |
4931 | struct file_lock *fl; | |
4932 | ||
4933 | fl = locks_alloc_lock(); | |
4934 | if (!fl) | |
4935 | return NULL; | |
22d38c4c | 4936 | fl->fl_lmops = &nfsd_lease_mng_ops; |
617588d5 | 4937 | fl->fl_flags = FL_DELEG; |
22d38c4c BF |
4938 | fl->fl_type = flag == NFS4_OPEN_DELEGATE_READ? F_RDLCK: F_WRLCK; |
4939 | fl->fl_end = OFFSET_MAX; | |
653e514e | 4940 | fl->fl_owner = (fl_owner_t)dp; |
22d38c4c | 4941 | fl->fl_pid = current->tgid; |
eb82dd39 | 4942 | fl->fl_file = dp->dl_stid.sc_file->fi_deleg_file->nf_file; |
22d38c4c BF |
4943 | return fl; |
4944 | } | |
4945 | ||
94415b06 BF |
4946 | static int nfsd4_check_conflicting_opens(struct nfs4_client *clp, |
4947 | struct nfs4_file *fp) | |
4948 | { | |
4949 | struct nfs4_clnt_odstate *co; | |
4950 | struct file *f = fp->fi_deleg_file->nf_file; | |
4951 | struct inode *ino = locks_inode(f); | |
4952 | int writes = atomic_read(&ino->i_writecount); | |
4953 | ||
4954 | if (fp->fi_fds[O_WRONLY]) | |
4955 | writes--; | |
4956 | if (fp->fi_fds[O_RDWR]) | |
4957 | writes--; | |
4958 | WARN_ON_ONCE(writes < 0); | |
4959 | if (writes > 0) | |
4960 | return -EAGAIN; | |
4961 | spin_lock(&fp->fi_lock); | |
4962 | list_for_each_entry(co, &fp->fi_clnt_odstate, co_perfile) { | |
4963 | if (co->co_client != clp) { | |
4964 | spin_unlock(&fp->fi_lock); | |
4965 | return -EAGAIN; | |
4966 | } | |
4967 | } | |
4968 | spin_unlock(&fp->fi_lock); | |
4969 | return 0; | |
4970 | } | |
4971 | ||
0b26693c JL |
4972 | static struct nfs4_delegation * |
4973 | nfs4_set_delegation(struct nfs4_client *clp, struct svc_fh *fh, | |
8287f009 | 4974 | struct nfs4_file *fp, struct nfs4_clnt_odstate *odstate) |
acfdf5c3 | 4975 | { |
68b18f52 | 4976 | int status = 0; |
0b26693c | 4977 | struct nfs4_delegation *dp; |
eb82dd39 | 4978 | struct nfsd_file *nf; |
353601e7 | 4979 | struct file_lock *fl; |
417c6629 | 4980 | |
353601e7 BF |
4981 | /* |
4982 | * The fi_had_conflict and nfs_get_existing_delegation checks | |
4983 | * here are just optimizations; we'll need to recheck them at | |
4984 | * the end: | |
4985 | */ | |
bf7bd3e9 | 4986 | if (fp->fi_had_conflict) |
0b26693c JL |
4987 | return ERR_PTR(-EAGAIN); |
4988 | ||
eb82dd39 JL |
4989 | nf = find_readable_file(fp); |
4990 | if (!nf) { | |
94415b06 BF |
4991 | /* |
4992 | * We probably could attempt another open and get a read | |
4993 | * delegation, but for now, don't bother until the | |
4994 | * client actually sends us one. | |
4995 | */ | |
4996 | return ERR_PTR(-EAGAIN); | |
353601e7 | 4997 | } |
34ed9872 AE |
4998 | spin_lock(&state_lock); |
4999 | spin_lock(&fp->fi_lock); | |
68b18f52 BF |
5000 | if (nfs4_delegation_exists(clp, fp)) |
5001 | status = -EAGAIN; | |
353601e7 | 5002 | else if (!fp->fi_deleg_file) { |
eb82dd39 | 5003 | fp->fi_deleg_file = nf; |
353601e7 BF |
5004 | /* increment early to prevent fi_deleg_file from being |
5005 | * cleared */ | |
5006 | fp->fi_delegees = 1; | |
eb82dd39 | 5007 | nf = NULL; |
353601e7 BF |
5008 | } else |
5009 | fp->fi_delegees++; | |
34ed9872 AE |
5010 | spin_unlock(&fp->fi_lock); |
5011 | spin_unlock(&state_lock); | |
eb82dd39 JL |
5012 | if (nf) |
5013 | nfsd_file_put(nf); | |
34ed9872 AE |
5014 | if (status) |
5015 | return ERR_PTR(status); | |
5016 | ||
353601e7 | 5017 | status = -ENOMEM; |
86d29b10 | 5018 | dp = alloc_init_deleg(clp, fp, fh, odstate); |
0b26693c | 5019 | if (!dp) |
353601e7 BF |
5020 | goto out_delegees; |
5021 | ||
5022 | fl = nfs4_alloc_init_lease(dp, NFS4_OPEN_DELEGATE_READ); | |
5023 | if (!fl) | |
bd8d7250 | 5024 | goto out_clnt_odstate; |
353601e7 | 5025 | |
94415b06 BF |
5026 | status = nfsd4_check_conflicting_opens(clp, fp); |
5027 | if (status) { | |
5028 | locks_free_lock(fl); | |
5029 | goto out_clnt_odstate; | |
5030 | } | |
eb82dd39 | 5031 | status = vfs_setlease(fp->fi_deleg_file->nf_file, fl->fl_type, &fl, NULL); |
353601e7 BF |
5032 | if (fl) |
5033 | locks_free_lock(fl); | |
5034 | if (status) | |
5035 | goto out_clnt_odstate; | |
94415b06 BF |
5036 | status = nfsd4_check_conflicting_opens(clp, fp); |
5037 | if (status) | |
5038 | goto out_clnt_odstate; | |
0b26693c | 5039 | |
417c6629 JL |
5040 | spin_lock(&state_lock); |
5041 | spin_lock(&fp->fi_lock); | |
353601e7 | 5042 | if (fp->fi_had_conflict) |
417c6629 | 5043 | status = -EAGAIN; |
353601e7 BF |
5044 | else |
5045 | status = hash_delegation_locked(dp, fp); | |
417c6629 | 5046 | spin_unlock(&fp->fi_lock); |
cdc97505 | 5047 | spin_unlock(&state_lock); |
353601e7 BF |
5048 | |
5049 | if (status) | |
692ad280 AE |
5050 | goto out_unlock; |
5051 | ||
0b26693c | 5052 | return dp; |
692ad280 | 5053 | out_unlock: |
eb82dd39 | 5054 | vfs_setlease(fp->fi_deleg_file->nf_file, F_UNLCK, NULL, (void **)&dp); |
353601e7 BF |
5055 | out_clnt_odstate: |
5056 | put_clnt_odstate(dp->dl_clnt_odstate); | |
353601e7 BF |
5057 | nfs4_put_stid(&dp->dl_stid); |
5058 | out_delegees: | |
5059 | put_deleg_file(fp); | |
5060 | return ERR_PTR(status); | |
edab9782 BF |
5061 | } |
5062 | ||
4aa8913c BH |
5063 | static void nfsd4_open_deleg_none_ext(struct nfsd4_open *open, int status) |
5064 | { | |
5065 | open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT; | |
5066 | if (status == -EAGAIN) | |
5067 | open->op_why_no_deleg = WND4_CONTENTION; | |
5068 | else { | |
5069 | open->op_why_no_deleg = WND4_RESOURCE; | |
5070 | switch (open->op_deleg_want) { | |
5071 | case NFS4_SHARE_WANT_READ_DELEG: | |
5072 | case NFS4_SHARE_WANT_WRITE_DELEG: | |
5073 | case NFS4_SHARE_WANT_ANY_DELEG: | |
5074 | break; | |
5075 | case NFS4_SHARE_WANT_CANCEL: | |
5076 | open->op_why_no_deleg = WND4_CANCELLED; | |
5077 | break; | |
5078 | case NFS4_SHARE_WANT_NO_DELEG: | |
063b0fb9 | 5079 | WARN_ON_ONCE(1); |
4aa8913c BH |
5080 | } |
5081 | } | |
5082 | } | |
5083 | ||
1da177e4 LT |
5084 | /* |
5085 | * Attempt to hand out a delegation. | |
99c41515 BF |
5086 | * |
5087 | * Note we don't support write delegations, and won't until the vfs has | |
5088 | * proper support for them. | |
1da177e4 LT |
5089 | */ |
5090 | static void | |
4cf59221 JL |
5091 | nfs4_open_delegation(struct svc_fh *fh, struct nfsd4_open *open, |
5092 | struct nfs4_ol_stateid *stp) | |
1da177e4 LT |
5093 | { |
5094 | struct nfs4_delegation *dp; | |
4cf59221 JL |
5095 | struct nfs4_openowner *oo = openowner(stp->st_stateowner); |
5096 | struct nfs4_client *clp = stp->st_stid.sc_client; | |
14a24e99 | 5097 | int cb_up; |
99c41515 | 5098 | int status = 0; |
1da177e4 | 5099 | |
fe0750e5 | 5100 | cb_up = nfsd4_cb_channel_good(oo->oo_owner.so_client); |
7b190fec N |
5101 | open->op_recall = 0; |
5102 | switch (open->op_claim_type) { | |
5103 | case NFS4_OPEN_CLAIM_PREVIOUS: | |
2bf23875 | 5104 | if (!cb_up) |
7b190fec | 5105 | open->op_recall = 1; |
99c41515 BF |
5106 | if (open->op_delegate_type != NFS4_OPEN_DELEGATE_READ) |
5107 | goto out_no_deleg; | |
7b190fec N |
5108 | break; |
5109 | case NFS4_OPEN_CLAIM_NULL: | |
ed47b062 | 5110 | case NFS4_OPEN_CLAIM_FH: |
99c41515 BF |
5111 | /* |
5112 | * Let's not give out any delegations till everyone's | |
c87fb4a3 BF |
5113 | * had the chance to reclaim theirs, *and* until |
5114 | * NLM locks have all been reclaimed: | |
99c41515 | 5115 | */ |
4cf59221 | 5116 | if (locks_in_grace(clp->net)) |
99c41515 | 5117 | goto out_no_deleg; |
dad1c067 | 5118 | if (!cb_up || !(oo->oo_flags & NFS4_OO_CONFIRMED)) |
99c41515 | 5119 | goto out_no_deleg; |
7b190fec N |
5120 | break; |
5121 | default: | |
99c41515 | 5122 | goto out_no_deleg; |
7b190fec | 5123 | } |
8287f009 | 5124 | dp = nfs4_set_delegation(clp, fh, stp->st_stid.sc_file, stp->st_clnt_odstate); |
0b26693c | 5125 | if (IS_ERR(dp)) |
dd239cc0 | 5126 | goto out_no_deleg; |
1da177e4 | 5127 | |
d5477a8d | 5128 | memcpy(&open->op_delegate_stateid, &dp->dl_stid.sc_stateid, sizeof(dp->dl_stid.sc_stateid)); |
1da177e4 | 5129 | |
dd5e3fbc | 5130 | trace_nfsd_deleg_open(&dp->dl_stid.sc_stateid); |
99c41515 | 5131 | open->op_delegate_type = NFS4_OPEN_DELEGATE_READ; |
67cb1279 | 5132 | nfs4_put_stid(&dp->dl_stid); |
dd239cc0 | 5133 | return; |
dd239cc0 | 5134 | out_no_deleg: |
99c41515 BF |
5135 | open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE; |
5136 | if (open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS && | |
d08d32e6 | 5137 | open->op_delegate_type != NFS4_OPEN_DELEGATE_NONE) { |
99c41515 | 5138 | dprintk("NFSD: WARNING: refusing delegation reclaim\n"); |
d08d32e6 BF |
5139 | open->op_recall = 1; |
5140 | } | |
99c41515 BF |
5141 | |
5142 | /* 4.1 client asking for a delegation? */ | |
5143 | if (open->op_deleg_want) | |
5144 | nfsd4_open_deleg_none_ext(open, status); | |
5145 | return; | |
1da177e4 LT |
5146 | } |
5147 | ||
e27f49c3 BH |
5148 | static void nfsd4_deleg_xgrade_none_ext(struct nfsd4_open *open, |
5149 | struct nfs4_delegation *dp) | |
5150 | { | |
5151 | if (open->op_deleg_want == NFS4_SHARE_WANT_READ_DELEG && | |
5152 | dp->dl_type == NFS4_OPEN_DELEGATE_WRITE) { | |
5153 | open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT; | |
5154 | open->op_why_no_deleg = WND4_NOT_SUPP_DOWNGRADE; | |
5155 | } else if (open->op_deleg_want == NFS4_SHARE_WANT_WRITE_DELEG && | |
5156 | dp->dl_type == NFS4_OPEN_DELEGATE_WRITE) { | |
5157 | open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT; | |
5158 | open->op_why_no_deleg = WND4_NOT_SUPP_UPGRADE; | |
5159 | } | |
5160 | /* Otherwise the client must be confused wanting a delegation | |
5161 | * it already has, therefore we don't return | |
5162 | * NFS4_OPEN_DELEGATE_NONE_EXT and reason. | |
5163 | */ | |
5164 | } | |
5165 | ||
b37ad28b | 5166 | __be32 |
1da177e4 LT |
5167 | nfsd4_process_open2(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_open *open) |
5168 | { | |
6668958f | 5169 | struct nfsd4_compoundres *resp = rqstp->rq_resp; |
38c2f4b1 | 5170 | struct nfs4_client *cl = open->op_openowner->oo_owner.so_client; |
1da177e4 | 5171 | struct nfs4_file *fp = NULL; |
dcef0413 | 5172 | struct nfs4_ol_stateid *stp = NULL; |
567d9829 | 5173 | struct nfs4_delegation *dp = NULL; |
b37ad28b | 5174 | __be32 status; |
d8a1a000 | 5175 | bool new_stp = false; |
1da177e4 | 5176 | |
1da177e4 LT |
5177 | /* |
5178 | * Lookup file; if found, lookup stateid and check open request, | |
5179 | * and check for delegations in the process of being recalled. | |
5180 | * If not found, create the nfs4_file struct | |
5181 | */ | |
f9c00c3a | 5182 | fp = find_or_add_file(open->op_file, ¤t_fh->fh_handle); |
950e0118 | 5183 | if (fp != open->op_file) { |
41d22663 | 5184 | status = nfs4_check_deleg(cl, open, &dp); |
c44c5eeb N |
5185 | if (status) |
5186 | goto out; | |
15ca08d3 | 5187 | stp = nfsd4_find_and_lock_existing_open(fp, open); |
1da177e4 | 5188 | } else { |
950e0118 | 5189 | open->op_file = NULL; |
c44c5eeb | 5190 | status = nfserr_bad_stateid; |
8b289b2c | 5191 | if (nfsd4_is_deleg_cur(open)) |
c44c5eeb | 5192 | goto out; |
1da177e4 LT |
5193 | } |
5194 | ||
d8a1a000 TM |
5195 | if (!stp) { |
5196 | stp = init_open_stateid(fp, open); | |
5197 | if (!open->op_stp) | |
5198 | new_stp = true; | |
5199 | } | |
5200 | ||
1da177e4 LT |
5201 | /* |
5202 | * OPEN the file, or upgrade an existing OPEN. | |
5203 | * If truncate fails, the OPEN fails. | |
d8a1a000 TM |
5204 | * |
5205 | * stp is already locked. | |
1da177e4 | 5206 | */ |
d8a1a000 | 5207 | if (!new_stp) { |
1da177e4 | 5208 | /* Stateid was found, this is an OPEN upgrade */ |
f9d7562f | 5209 | status = nfs4_upgrade_open(rqstp, fp, current_fh, stp, open); |
35a92fe8 | 5210 | if (status) { |
feb9dad5 | 5211 | mutex_unlock(&stp->st_mutex); |
1da177e4 | 5212 | goto out; |
35a92fe8 | 5213 | } |
1da177e4 | 5214 | } else { |
6eb3a1d0 JL |
5215 | status = nfs4_get_vfs_file(rqstp, fp, current_fh, stp, open); |
5216 | if (status) { | |
d8a1a000 | 5217 | stp->st_stid.sc_type = NFS4_CLOSED_STID; |
6eb3a1d0 | 5218 | release_open_stateid(stp); |
d8a1a000 | 5219 | mutex_unlock(&stp->st_mutex); |
6eb3a1d0 JL |
5220 | goto out; |
5221 | } | |
8287f009 SB |
5222 | |
5223 | stp->st_clnt_odstate = find_or_hash_clnt_odstate(fp, | |
5224 | open->op_odstate); | |
5225 | if (stp->st_clnt_odstate == open->op_odstate) | |
5226 | open->op_odstate = NULL; | |
1da177e4 | 5227 | } |
d8a1a000 | 5228 | |
9767feb2 | 5229 | nfs4_inc_and_copy_stateid(&open->op_stateid, &stp->st_stid); |
feb9dad5 | 5230 | mutex_unlock(&stp->st_mutex); |
1da177e4 | 5231 | |
d24433cd | 5232 | if (nfsd4_has_session(&resp->cstate)) { |
d24433cd BH |
5233 | if (open->op_deleg_want & NFS4_SHARE_WANT_NO_DELEG) { |
5234 | open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT; | |
5235 | open->op_why_no_deleg = WND4_NOT_WANTED; | |
5236 | goto nodeleg; | |
5237 | } | |
5238 | } | |
5239 | ||
1da177e4 LT |
5240 | /* |
5241 | * Attempt to hand out a delegation. No error return, because the | |
5242 | * OPEN succeeds even if we fail. | |
5243 | */ | |
4cf59221 | 5244 | nfs4_open_delegation(current_fh, open, stp); |
d24433cd | 5245 | nodeleg: |
1da177e4 | 5246 | status = nfs_ok; |
dd5e3fbc | 5247 | trace_nfsd_deleg_none(&stp->st_stid.sc_stateid); |
1da177e4 | 5248 | out: |
d24433cd BH |
5249 | /* 4.1 client trying to upgrade/downgrade delegation? */ |
5250 | if (open->op_delegate_type == NFS4_OPEN_DELEGATE_NONE && dp && | |
e27f49c3 BH |
5251 | open->op_deleg_want) |
5252 | nfsd4_deleg_xgrade_none_ext(open, dp); | |
d24433cd | 5253 | |
13cd2184 N |
5254 | if (fp) |
5255 | put_nfs4_file(fp); | |
37515177 | 5256 | if (status == 0 && open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS) |
87186022 | 5257 | open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED; |
1da177e4 LT |
5258 | /* |
5259 | * To finish the open response, we just need to set the rflags. | |
5260 | */ | |
5261 | open->op_rflags = NFS4_OPEN_RESULT_LOCKTYPE_POSIX; | |
19e4c347 JL |
5262 | if (nfsd4_has_session(&resp->cstate)) |
5263 | open->op_rflags |= NFS4_OPEN_RESULT_MAY_NOTIFY_LOCK; | |
5264 | else if (!(open->op_openowner->oo_flags & NFS4_OO_CONFIRMED)) | |
1da177e4 | 5265 | open->op_rflags |= NFS4_OPEN_RESULT_CONFIRM; |
19e4c347 | 5266 | |
dcd94cc2 TM |
5267 | if (dp) |
5268 | nfs4_put_stid(&dp->dl_stid); | |
d6f2bc5d TM |
5269 | if (stp) |
5270 | nfs4_put_stid(&stp->st_stid); | |
1da177e4 LT |
5271 | |
5272 | return status; | |
5273 | } | |
5274 | ||
58fb12e6 | 5275 | void nfsd4_cleanup_open_state(struct nfsd4_compound_state *cstate, |
42297899 | 5276 | struct nfsd4_open *open) |
d29b20cd BF |
5277 | { |
5278 | if (open->op_openowner) { | |
d3134b10 JL |
5279 | struct nfs4_stateowner *so = &open->op_openowner->oo_owner; |
5280 | ||
5281 | nfsd4_cstate_assign_replay(cstate, so); | |
5282 | nfs4_put_stateowner(so); | |
d29b20cd | 5283 | } |
32513b40 | 5284 | if (open->op_file) |
5b095e99 | 5285 | kmem_cache_free(file_slab, open->op_file); |
4cdc951b | 5286 | if (open->op_stp) |
6011695d | 5287 | nfs4_put_stid(&open->op_stp->st_stid); |
8287f009 SB |
5288 | if (open->op_odstate) |
5289 | kmem_cache_free(odstate_slab, open->op_odstate); | |
d29b20cd BF |
5290 | } |
5291 | ||
b37ad28b | 5292 | __be32 |
b591480b | 5293 | nfsd4_renew(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, |
eb69853d | 5294 | union nfsd4_op_u *u) |
1da177e4 | 5295 | { |
eb69853d | 5296 | clientid_t *clid = &u->renew; |
1da177e4 | 5297 | struct nfs4_client *clp; |
b37ad28b | 5298 | __be32 status; |
7f2210fa | 5299 | struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id); |
1da177e4 | 5300 | |
dd5e3fbc | 5301 | trace_nfsd_clid_renew(clid); |
b7342204 | 5302 | status = lookup_clientid(clid, cstate, nn, false); |
9b2ef62b | 5303 | if (status) |
1da177e4 | 5304 | goto out; |
4b24ca7d | 5305 | clp = cstate->clp; |
1da177e4 | 5306 | status = nfserr_cb_path_down; |
ea1da636 | 5307 | if (!list_empty(&clp->cl_delegations) |
77a3569d | 5308 | && clp->cl_cb_state != NFSD4_CB_UP) |
1da177e4 LT |
5309 | goto out; |
5310 | status = nfs_ok; | |
5311 | out: | |
1da177e4 LT |
5312 | return status; |
5313 | } | |
5314 | ||
7f5ef2e9 | 5315 | void |
12760c66 | 5316 | nfsd4_end_grace(struct nfsd_net *nn) |
a76b4319 | 5317 | { |
33dcc481 | 5318 | /* do nothing if grace period already ended */ |
a51c84ed | 5319 | if (nn->grace_ended) |
33dcc481 JL |
5320 | return; |
5321 | ||
dd5e3fbc | 5322 | trace_nfsd_grace_complete(nn); |
a51c84ed | 5323 | nn->grace_ended = true; |
70b28235 BF |
5324 | /* |
5325 | * If the server goes down again right now, an NFSv4 | |
5326 | * client will still be allowed to reclaim after it comes back up, | |
5327 | * even if it hasn't yet had a chance to reclaim state this time. | |
5328 | * | |
5329 | */ | |
919b8049 | 5330 | nfsd4_record_grace_done(nn); |
70b28235 BF |
5331 | /* |
5332 | * At this point, NFSv4 clients can still reclaim. But if the | |
5333 | * server crashes, any that have not yet reclaimed will be out | |
5334 | * of luck on the next boot. | |
5335 | * | |
5336 | * (NFSv4.1+ clients are considered to have reclaimed once they | |
5337 | * call RECLAIM_COMPLETE. NFSv4.0 clients are considered to | |
5338 | * have reclaimed after their first OPEN.) | |
5339 | */ | |
5e1533c7 | 5340 | locks_end_grace(&nn->nfsd4_manager); |
70b28235 BF |
5341 | /* |
5342 | * At this point, and once lockd and/or any other containers | |
5343 | * exit their grace period, further reclaims will fail and | |
5344 | * regular locking can resume. | |
5345 | */ | |
a76b4319 N |
5346 | } |
5347 | ||
03f318ca BF |
5348 | /* |
5349 | * If we've waited a lease period but there are still clients trying to | |
5350 | * reclaim, wait a little longer to give them a chance to finish. | |
5351 | */ | |
5352 | static bool clients_still_reclaiming(struct nfsd_net *nn) | |
5353 | { | |
20b7d86f AB |
5354 | time64_t double_grace_period_end = nn->boot_time + |
5355 | 2 * nn->nfsd4_lease; | |
03f318ca | 5356 | |
362063a5 SM |
5357 | if (nn->track_reclaim_completes && |
5358 | atomic_read(&nn->nr_reclaim_complete) == | |
5359 | nn->reclaim_str_hashtbl_size) | |
5360 | return false; | |
03f318ca BF |
5361 | if (!nn->somebody_reclaimed) |
5362 | return false; | |
5363 | nn->somebody_reclaimed = false; | |
5364 | /* | |
5365 | * If we've given them *two* lease times to reclaim, and they're | |
5366 | * still not done, give up: | |
5367 | */ | |
20b7d86f | 5368 | if (ktime_get_boottime_seconds() > double_grace_period_end) |
03f318ca BF |
5369 | return false; |
5370 | return true; | |
5371 | } | |
5372 | ||
20b7d86f | 5373 | static time64_t |
09121281 | 5374 | nfs4_laundromat(struct nfsd_net *nn) |
1da177e4 LT |
5375 | { |
5376 | struct nfs4_client *clp; | |
fe0750e5 | 5377 | struct nfs4_openowner *oo; |
1da177e4 | 5378 | struct nfs4_delegation *dp; |
217526e7 | 5379 | struct nfs4_ol_stateid *stp; |
7919d0a2 | 5380 | struct nfsd4_blocked_lock *nbl; |
1da177e4 | 5381 | struct list_head *pos, *next, reaplist; |
20b7d86f AB |
5382 | time64_t cutoff = ktime_get_boottime_seconds() - nn->nfsd4_lease; |
5383 | time64_t t, new_timeo = nn->nfsd4_lease; | |
624322f1 OK |
5384 | struct nfs4_cpntf_state *cps; |
5385 | copy_stateid_t *cps_t; | |
5386 | int i; | |
1da177e4 | 5387 | |
03f318ca BF |
5388 | if (clients_still_reclaiming(nn)) { |
5389 | new_timeo = 0; | |
5390 | goto out; | |
5391 | } | |
12760c66 | 5392 | nfsd4_end_grace(nn); |
36acb66b | 5393 | INIT_LIST_HEAD(&reaplist); |
624322f1 OK |
5394 | |
5395 | spin_lock(&nn->s2s_cp_lock); | |
5396 | idr_for_each_entry(&nn->s2s_cp_stateids, cps_t, i) { | |
5397 | cps = container_of(cps_t, struct nfs4_cpntf_state, cp_stateid); | |
5398 | if (cps->cp_stateid.sc_type == NFS4_COPYNOTIFY_STID && | |
20b7d86f | 5399 | cps->cpntf_time > cutoff) |
624322f1 OK |
5400 | _free_cpntf_state_locked(nn, cps); |
5401 | } | |
5402 | spin_unlock(&nn->s2s_cp_lock); | |
5403 | ||
c9a49628 | 5404 | spin_lock(&nn->client_lock); |
5ed58bb2 | 5405 | list_for_each_safe(pos, next, &nn->client_lru) { |
1da177e4 | 5406 | clp = list_entry(pos, struct nfs4_client, cl_lru); |
20b7d86f | 5407 | if (clp->cl_time > cutoff) { |
1da177e4 | 5408 | t = clp->cl_time - cutoff; |
a832e7ae | 5409 | new_timeo = min(new_timeo, t); |
1da177e4 LT |
5410 | break; |
5411 | } | |
221a6876 | 5412 | if (mark_client_expired_locked(clp)) { |
dd5e3fbc | 5413 | trace_nfsd_clid_expired(&clp->cl_clientid); |
d7682988 BH |
5414 | continue; |
5415 | } | |
4864af97 | 5416 | list_add(&clp->cl_lru, &reaplist); |
36acb66b | 5417 | } |
c9a49628 | 5418 | spin_unlock(&nn->client_lock); |
36acb66b BH |
5419 | list_for_each_safe(pos, next, &reaplist) { |
5420 | clp = list_entry(pos, struct nfs4_client, cl_lru); | |
dd5e3fbc | 5421 | trace_nfsd_clid_purged(&clp->cl_clientid); |
4864af97 | 5422 | list_del_init(&clp->cl_lru); |
1da177e4 LT |
5423 | expire_client(clp); |
5424 | } | |
cdc97505 | 5425 | spin_lock(&state_lock); |
e8c69d17 | 5426 | list_for_each_safe(pos, next, &nn->del_recall_lru) { |
1da177e4 | 5427 | dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru); |
20b7d86f | 5428 | if (dp->dl_time > cutoff) { |
a832e7ae JL |
5429 | t = dp->dl_time - cutoff; |
5430 | new_timeo = min(new_timeo, t); | |
1da177e4 LT |
5431 | break; |
5432 | } | |
3fcbbd24 | 5433 | WARN_ON(!unhash_delegation_locked(dp)); |
42690676 | 5434 | list_add(&dp->dl_recall_lru, &reaplist); |
1da177e4 | 5435 | } |
cdc97505 | 5436 | spin_unlock(&state_lock); |
2d4a532d JL |
5437 | while (!list_empty(&reaplist)) { |
5438 | dp = list_first_entry(&reaplist, struct nfs4_delegation, | |
5439 | dl_recall_lru); | |
5440 | list_del_init(&dp->dl_recall_lru); | |
3bd64a5b | 5441 | revoke_delegation(dp); |
1da177e4 | 5442 | } |
217526e7 JL |
5443 | |
5444 | spin_lock(&nn->client_lock); | |
5445 | while (!list_empty(&nn->close_lru)) { | |
5446 | oo = list_first_entry(&nn->close_lru, struct nfs4_openowner, | |
5447 | oo_close_lru); | |
20b7d86f | 5448 | if (oo->oo_time > cutoff) { |
a832e7ae JL |
5449 | t = oo->oo_time - cutoff; |
5450 | new_timeo = min(new_timeo, t); | |
1da177e4 LT |
5451 | break; |
5452 | } | |
217526e7 JL |
5453 | list_del_init(&oo->oo_close_lru); |
5454 | stp = oo->oo_last_closed_stid; | |
5455 | oo->oo_last_closed_stid = NULL; | |
5456 | spin_unlock(&nn->client_lock); | |
5457 | nfs4_put_stid(&stp->st_stid); | |
5458 | spin_lock(&nn->client_lock); | |
1da177e4 | 5459 | } |
217526e7 JL |
5460 | spin_unlock(&nn->client_lock); |
5461 | ||
7919d0a2 JL |
5462 | /* |
5463 | * It's possible for a client to try and acquire an already held lock | |
5464 | * that is being held for a long time, and then lose interest in it. | |
5465 | * So, we clean out any un-revisited request after a lease period | |
5466 | * under the assumption that the client is no longer interested. | |
5467 | * | |
5468 | * RFC5661, sec. 9.6 states that the client must not rely on getting | |
5469 | * notifications and must continue to poll for locks, even when the | |
5470 | * server supports them. Thus this shouldn't lead to clients blocking | |
5471 | * indefinitely once the lock does become free. | |
5472 | */ | |
5473 | BUG_ON(!list_empty(&reaplist)); | |
0cc11a61 | 5474 | spin_lock(&nn->blocked_locks_lock); |
7919d0a2 JL |
5475 | while (!list_empty(&nn->blocked_locks_lru)) { |
5476 | nbl = list_first_entry(&nn->blocked_locks_lru, | |
5477 | struct nfsd4_blocked_lock, nbl_lru); | |
20b7d86f | 5478 | if (nbl->nbl_time > cutoff) { |
7919d0a2 JL |
5479 | t = nbl->nbl_time - cutoff; |
5480 | new_timeo = min(new_timeo, t); | |
5481 | break; | |
5482 | } | |
5483 | list_move(&nbl->nbl_lru, &reaplist); | |
5484 | list_del_init(&nbl->nbl_list); | |
5485 | } | |
0cc11a61 | 5486 | spin_unlock(&nn->blocked_locks_lock); |
7919d0a2 JL |
5487 | |
5488 | while (!list_empty(&reaplist)) { | |
64ebe124 | 5489 | nbl = list_first_entry(&reaplist, |
7919d0a2 JL |
5490 | struct nfsd4_blocked_lock, nbl_lru); |
5491 | list_del_init(&nbl->nbl_lru); | |
7919d0a2 JL |
5492 | free_blocked_lock(nbl); |
5493 | } | |
03f318ca | 5494 | out: |
20b7d86f | 5495 | new_timeo = max_t(time64_t, new_timeo, NFSD_LAUNDROMAT_MINTIMEOUT); |
a832e7ae | 5496 | return new_timeo; |
1da177e4 LT |
5497 | } |
5498 | ||
a254b246 HH |
5499 | static struct workqueue_struct *laundry_wq; |
5500 | static void laundromat_main(struct work_struct *); | |
a254b246 HH |
5501 | |
5502 | static void | |
09121281 | 5503 | laundromat_main(struct work_struct *laundry) |
1da177e4 | 5504 | { |
20b7d86f | 5505 | time64_t t; |
2e55f3ab | 5506 | struct delayed_work *dwork = to_delayed_work(laundry); |
09121281 SK |
5507 | struct nfsd_net *nn = container_of(dwork, struct nfsd_net, |
5508 | laundromat_work); | |
1da177e4 | 5509 | |
09121281 | 5510 | t = nfs4_laundromat(nn); |
09121281 | 5511 | queue_delayed_work(laundry_wq, &nn->laundromat_work, t*HZ); |
1da177e4 LT |
5512 | } |
5513 | ||
8fcd461d | 5514 | static inline __be32 nfs4_check_fh(struct svc_fh *fhp, struct nfs4_stid *stp) |
1da177e4 | 5515 | { |
8fcd461d | 5516 | if (!fh_match(&fhp->fh_handle, &stp->sc_file->fi_fhandle)) |
f7a4d872 BF |
5517 | return nfserr_bad_stateid; |
5518 | return nfs_ok; | |
1da177e4 LT |
5519 | } |
5520 | ||
1da177e4 | 5521 | static inline int |
82c5ff1b | 5522 | access_permit_read(struct nfs4_ol_stateid *stp) |
1da177e4 | 5523 | { |
82c5ff1b JL |
5524 | return test_access(NFS4_SHARE_ACCESS_READ, stp) || |
5525 | test_access(NFS4_SHARE_ACCESS_BOTH, stp) || | |
5526 | test_access(NFS4_SHARE_ACCESS_WRITE, stp); | |
1da177e4 LT |
5527 | } |
5528 | ||
5529 | static inline int | |
82c5ff1b | 5530 | access_permit_write(struct nfs4_ol_stateid *stp) |
1da177e4 | 5531 | { |
82c5ff1b JL |
5532 | return test_access(NFS4_SHARE_ACCESS_WRITE, stp) || |
5533 | test_access(NFS4_SHARE_ACCESS_BOTH, stp); | |
1da177e4 LT |
5534 | } |
5535 | ||
5536 | static | |
dcef0413 | 5537 | __be32 nfs4_check_openmode(struct nfs4_ol_stateid *stp, int flags) |
1da177e4 | 5538 | { |
b37ad28b | 5539 | __be32 status = nfserr_openmode; |
1da177e4 | 5540 | |
02921914 BF |
5541 | /* For lock stateid's, we test the parent open, not the lock: */ |
5542 | if (stp->st_openstp) | |
5543 | stp = stp->st_openstp; | |
82c5ff1b | 5544 | if ((flags & WR_STATE) && !access_permit_write(stp)) |
1da177e4 | 5545 | goto out; |
82c5ff1b | 5546 | if ((flags & RD_STATE) && !access_permit_read(stp)) |
1da177e4 LT |
5547 | goto out; |
5548 | status = nfs_ok; | |
5549 | out: | |
5550 | return status; | |
5551 | } | |
5552 | ||
b37ad28b | 5553 | static inline __be32 |
5ccb0066 | 5554 | check_special_stateids(struct net *net, svc_fh *current_fh, stateid_t *stateid, int flags) |
1da177e4 | 5555 | { |
203a8c8e | 5556 | if (ONE_STATEID(stateid) && (flags & RD_STATE)) |
1da177e4 | 5557 | return nfs_ok; |
c87fb4a3 | 5558 | else if (opens_in_grace(net)) { |
25985edc | 5559 | /* Answer in remaining cases depends on existence of |
1da177e4 LT |
5560 | * conflicting state; so we must wait out the grace period. */ |
5561 | return nfserr_grace; | |
5562 | } else if (flags & WR_STATE) | |
5563 | return nfs4_share_conflict(current_fh, | |
5564 | NFS4_SHARE_DENY_WRITE); | |
5565 | else /* (flags & RD_STATE) && ZERO_STATEID(stateid) */ | |
5566 | return nfs4_share_conflict(current_fh, | |
5567 | NFS4_SHARE_DENY_READ); | |
5568 | } | |
5569 | ||
5570 | /* | |
5571 | * Allow READ/WRITE during grace period on recovered state only for files | |
5572 | * that are not able to provide mandatory locking. | |
5573 | */ | |
5574 | static inline int | |
5ccb0066 | 5575 | grace_disallows_io(struct net *net, struct inode *inode) |
1da177e4 | 5576 | { |
c87fb4a3 | 5577 | return opens_in_grace(net) && mandatory_lock(inode); |
1da177e4 LT |
5578 | } |
5579 | ||
57b7b43b | 5580 | static __be32 check_stateid_generation(stateid_t *in, stateid_t *ref, bool has_session) |
0836f587 | 5581 | { |
6668958f AA |
5582 | /* |
5583 | * When sessions are used the stateid generation number is ignored | |
5584 | * when it is zero. | |
5585 | */ | |
28dde241 | 5586 | if (has_session && in->si_generation == 0) |
81b82965 BF |
5587 | return nfs_ok; |
5588 | ||
5589 | if (in->si_generation == ref->si_generation) | |
5590 | return nfs_ok; | |
6668958f | 5591 | |
0836f587 | 5592 | /* If the client sends us a stateid from the future, it's buggy: */ |
14b7f4a1 | 5593 | if (nfsd4_stateid_generation_after(in, ref)) |
0836f587 BF |
5594 | return nfserr_bad_stateid; |
5595 | /* | |
81b82965 BF |
5596 | * However, we could see a stateid from the past, even from a |
5597 | * non-buggy client. For example, if the client sends a lock | |
5598 | * while some IO is outstanding, the lock may bump si_generation | |
5599 | * while the IO is still in flight. The client could avoid that | |
5600 | * situation by waiting for responses on all the IO requests, | |
5601 | * but better performance may result in retrying IO that | |
5602 | * receives an old_stateid error if requests are rarely | |
5603 | * reordered in flight: | |
0836f587 | 5604 | */ |
81b82965 | 5605 | return nfserr_old_stateid; |
0836f587 BF |
5606 | } |
5607 | ||
03da3169 TM |
5608 | static __be32 nfsd4_stid_check_stateid_generation(stateid_t *in, struct nfs4_stid *s, bool has_session) |
5609 | { | |
5610 | __be32 ret; | |
5611 | ||
5612 | spin_lock(&s->sc_lock); | |
5613 | ret = nfsd4_verify_open_stid(s); | |
5614 | if (ret == nfs_ok) | |
5615 | ret = check_stateid_generation(in, &s->sc_stateid, has_session); | |
5616 | spin_unlock(&s->sc_lock); | |
5617 | return ret; | |
5618 | } | |
5619 | ||
ebe9cb3b CH |
5620 | static __be32 nfsd4_check_openowner_confirmed(struct nfs4_ol_stateid *ols) |
5621 | { | |
5622 | if (ols->st_stateowner->so_is_open_owner && | |
5623 | !(openowner(ols->st_stateowner)->oo_flags & NFS4_OO_CONFIRMED)) | |
5624 | return nfserr_bad_stateid; | |
5625 | return nfs_ok; | |
5626 | } | |
5627 | ||
7df302f7 | 5628 | static __be32 nfsd4_validate_stateid(struct nfs4_client *cl, stateid_t *stateid) |
17456804 | 5629 | { |
97b7e3b6 | 5630 | struct nfs4_stid *s; |
1af71cc8 | 5631 | __be32 status = nfserr_bad_stateid; |
17456804 | 5632 | |
ae254dac AE |
5633 | if (ZERO_STATEID(stateid) || ONE_STATEID(stateid) || |
5634 | CLOSE_STATEID(stateid)) | |
1af71cc8 | 5635 | return status; |
663e36f0 | 5636 | if (!same_clid(&stateid->si_opaque.so_clid, &cl->cl_clientid)) |
1af71cc8 | 5637 | return status; |
1af71cc8 JL |
5638 | spin_lock(&cl->cl_lock); |
5639 | s = find_stateid_locked(cl, stateid); | |
97b7e3b6 | 5640 | if (!s) |
1af71cc8 | 5641 | goto out_unlock; |
03da3169 | 5642 | status = nfsd4_stid_check_stateid_generation(stateid, s, 1); |
17456804 | 5643 | if (status) |
1af71cc8 | 5644 | goto out_unlock; |
23340032 BF |
5645 | switch (s->sc_type) { |
5646 | case NFS4_DELEG_STID: | |
1af71cc8 JL |
5647 | status = nfs_ok; |
5648 | break; | |
3bd64a5b | 5649 | case NFS4_REVOKED_DELEG_STID: |
1af71cc8 JL |
5650 | status = nfserr_deleg_revoked; |
5651 | break; | |
23340032 BF |
5652 | case NFS4_OPEN_STID: |
5653 | case NFS4_LOCK_STID: | |
ebe9cb3b | 5654 | status = nfsd4_check_openowner_confirmed(openlockstateid(s)); |
1af71cc8 | 5655 | break; |
23340032 BF |
5656 | default: |
5657 | printk("unknown stateid type %x\n", s->sc_type); | |
df561f66 | 5658 | fallthrough; |
23340032 | 5659 | case NFS4_CLOSED_STID: |
b0fc29d6 | 5660 | case NFS4_CLOSED_DELEG_STID: |
1af71cc8 | 5661 | status = nfserr_bad_stateid; |
23340032 | 5662 | } |
1af71cc8 JL |
5663 | out_unlock: |
5664 | spin_unlock(&cl->cl_lock); | |
5665 | return status; | |
17456804 BS |
5666 | } |
5667 | ||
cd61c522 | 5668 | __be32 |
2dd6e458 TM |
5669 | nfsd4_lookup_stateid(struct nfsd4_compound_state *cstate, |
5670 | stateid_t *stateid, unsigned char typemask, | |
5671 | struct nfs4_stid **s, struct nfsd_net *nn) | |
38c2f4b1 | 5672 | { |
0eb6f20a | 5673 | __be32 status; |
95da1b3a AE |
5674 | bool return_revoked = false; |
5675 | ||
5676 | /* | |
5677 | * only return revoked delegations if explicitly asked. | |
5678 | * otherwise we report revoked or bad_stateid status. | |
5679 | */ | |
5680 | if (typemask & NFS4_REVOKED_DELEG_STID) | |
5681 | return_revoked = true; | |
5682 | else if (typemask & NFS4_DELEG_STID) | |
5683 | typemask |= NFS4_REVOKED_DELEG_STID; | |
38c2f4b1 | 5684 | |
ae254dac AE |
5685 | if (ZERO_STATEID(stateid) || ONE_STATEID(stateid) || |
5686 | CLOSE_STATEID(stateid)) | |
38c2f4b1 | 5687 | return nfserr_bad_stateid; |
b7342204 OK |
5688 | status = lookup_clientid(&stateid->si_opaque.so_clid, cstate, nn, |
5689 | false); | |
a8a7c677 | 5690 | if (status == nfserr_stale_clientid) { |
4b24ca7d | 5691 | if (cstate->session) |
a8a7c677 | 5692 | return nfserr_bad_stateid; |
38c2f4b1 | 5693 | return nfserr_stale_stateid; |
a8a7c677 | 5694 | } |
0eb6f20a BF |
5695 | if (status) |
5696 | return status; | |
4b24ca7d | 5697 | *s = find_stateid_by_type(cstate->clp, stateid, typemask); |
38c2f4b1 BF |
5698 | if (!*s) |
5699 | return nfserr_bad_stateid; | |
95da1b3a AE |
5700 | if (((*s)->sc_type == NFS4_REVOKED_DELEG_STID) && !return_revoked) { |
5701 | nfs4_put_stid(*s); | |
5702 | if (cstate->minorversion) | |
5703 | return nfserr_deleg_revoked; | |
5704 | return nfserr_bad_stateid; | |
5705 | } | |
38c2f4b1 | 5706 | return nfs_ok; |
38c2f4b1 BF |
5707 | } |
5708 | ||
eb82dd39 | 5709 | static struct nfsd_file * |
a0649b2d CH |
5710 | nfs4_find_file(struct nfs4_stid *s, int flags) |
5711 | { | |
af90f707 CH |
5712 | if (!s) |
5713 | return NULL; | |
5714 | ||
a0649b2d CH |
5715 | switch (s->sc_type) { |
5716 | case NFS4_DELEG_STID: | |
5717 | if (WARN_ON_ONCE(!s->sc_file->fi_deleg_file)) | |
5718 | return NULL; | |
eb82dd39 | 5719 | return nfsd_file_get(s->sc_file->fi_deleg_file); |
a0649b2d CH |
5720 | case NFS4_OPEN_STID: |
5721 | case NFS4_LOCK_STID: | |
5722 | if (flags & RD_STATE) | |
5723 | return find_readable_file(s->sc_file); | |
5724 | else | |
5725 | return find_writeable_file(s->sc_file); | |
5726 | break; | |
5727 | } | |
5728 | ||
5729 | return NULL; | |
5730 | } | |
5731 | ||
5732 | static __be32 | |
d8836f77 | 5733 | nfs4_check_olstateid(struct nfs4_ol_stateid *ols, int flags) |
a0649b2d CH |
5734 | { |
5735 | __be32 status; | |
5736 | ||
a0649b2d CH |
5737 | status = nfsd4_check_openowner_confirmed(ols); |
5738 | if (status) | |
5739 | return status; | |
5740 | return nfs4_check_openmode(ols, flags); | |
5741 | } | |
5742 | ||
af90f707 CH |
5743 | static __be32 |
5744 | nfs4_check_file(struct svc_rqst *rqstp, struct svc_fh *fhp, struct nfs4_stid *s, | |
5c4583b2 | 5745 | struct nfsd_file **nfp, int flags) |
af90f707 CH |
5746 | { |
5747 | int acc = (flags & RD_STATE) ? NFSD_MAY_READ : NFSD_MAY_WRITE; | |
eb82dd39 | 5748 | struct nfsd_file *nf; |
af90f707 CH |
5749 | __be32 status; |
5750 | ||
eb82dd39 JL |
5751 | nf = nfs4_find_file(s, flags); |
5752 | if (nf) { | |
af90f707 CH |
5753 | status = nfsd_permission(rqstp, fhp->fh_export, fhp->fh_dentry, |
5754 | acc | NFSD_MAY_OWNER_OVERRIDE); | |
5c4583b2 JL |
5755 | if (status) { |
5756 | nfsd_file_put(nf); | |
eb82dd39 | 5757 | goto out; |
5c4583b2 | 5758 | } |
af90f707 | 5759 | } else { |
eb82dd39 | 5760 | status = nfsd_file_acquire(rqstp, fhp, acc, &nf); |
af90f707 CH |
5761 | if (status) |
5762 | return status; | |
af90f707 | 5763 | } |
5c4583b2 | 5764 | *nfp = nf; |
eb82dd39 | 5765 | out: |
eb82dd39 | 5766 | return status; |
af90f707 | 5767 | } |
624322f1 OK |
5768 | static void |
5769 | _free_cpntf_state_locked(struct nfsd_net *nn, struct nfs4_cpntf_state *cps) | |
5770 | { | |
5771 | WARN_ON_ONCE(cps->cp_stateid.sc_type != NFS4_COPYNOTIFY_STID); | |
5772 | if (!refcount_dec_and_test(&cps->cp_stateid.sc_count)) | |
5773 | return; | |
5774 | list_del(&cps->cp_list); | |
5775 | idr_remove(&nn->s2s_cp_stateids, | |
5776 | cps->cp_stateid.stid.si_opaque.so_id); | |
5777 | kfree(cps); | |
5778 | } | |
b7342204 OK |
5779 | /* |
5780 | * A READ from an inter server to server COPY will have a | |
5781 | * copy stateid. Look up the copy notify stateid from the | |
5782 | * idr structure and take a reference on it. | |
5783 | */ | |
ce0887ac OK |
5784 | __be32 manage_cpntf_state(struct nfsd_net *nn, stateid_t *st, |
5785 | struct nfs4_client *clp, | |
5786 | struct nfs4_cpntf_state **cps) | |
b7342204 OK |
5787 | { |
5788 | copy_stateid_t *cps_t; | |
5789 | struct nfs4_cpntf_state *state = NULL; | |
5790 | ||
5791 | if (st->si_opaque.so_clid.cl_id != nn->s2s_cp_cl_id) | |
5792 | return nfserr_bad_stateid; | |
5793 | spin_lock(&nn->s2s_cp_lock); | |
5794 | cps_t = idr_find(&nn->s2s_cp_stateids, st->si_opaque.so_id); | |
5795 | if (cps_t) { | |
5796 | state = container_of(cps_t, struct nfs4_cpntf_state, | |
5797 | cp_stateid); | |
5277a79e DC |
5798 | if (state->cp_stateid.sc_type != NFS4_COPYNOTIFY_STID) { |
5799 | state = NULL; | |
5800 | goto unlock; | |
5801 | } | |
ce0887ac OK |
5802 | if (!clp) |
5803 | refcount_inc(&state->cp_stateid.sc_count); | |
5804 | else | |
5805 | _free_cpntf_state_locked(nn, state); | |
b7342204 | 5806 | } |
5277a79e | 5807 | unlock: |
b7342204 OK |
5808 | spin_unlock(&nn->s2s_cp_lock); |
5809 | if (!state) | |
5810 | return nfserr_bad_stateid; | |
ce0887ac OK |
5811 | if (!clp && state) |
5812 | *cps = state; | |
b7342204 OK |
5813 | return 0; |
5814 | } | |
5815 | ||
5816 | static __be32 find_cpntf_state(struct nfsd_net *nn, stateid_t *st, | |
5817 | struct nfs4_stid **stid) | |
5818 | { | |
5819 | __be32 status; | |
5820 | struct nfs4_cpntf_state *cps = NULL; | |
5821 | struct nfsd4_compound_state cstate; | |
5822 | ||
ce0887ac | 5823 | status = manage_cpntf_state(nn, st, NULL, &cps); |
b7342204 OK |
5824 | if (status) |
5825 | return status; | |
5826 | ||
20b7d86f | 5827 | cps->cpntf_time = ktime_get_boottime_seconds(); |
b7342204 OK |
5828 | memset(&cstate, 0, sizeof(cstate)); |
5829 | status = lookup_clientid(&cps->cp_p_clid, &cstate, nn, true); | |
5830 | if (status) | |
5831 | goto out; | |
5832 | status = nfsd4_lookup_stateid(&cstate, &cps->cp_p_stateid, | |
5833 | NFS4_DELEG_STID|NFS4_OPEN_STID|NFS4_LOCK_STID, | |
5834 | stid, nn); | |
5835 | put_client_renew(cstate.clp); | |
5836 | out: | |
5837 | nfs4_put_cpntf_state(nn, cps); | |
5838 | return status; | |
5839 | } | |
624322f1 OK |
5840 | |
5841 | void nfs4_put_cpntf_state(struct nfsd_net *nn, struct nfs4_cpntf_state *cps) | |
5842 | { | |
5843 | spin_lock(&nn->s2s_cp_lock); | |
5844 | _free_cpntf_state_locked(nn, cps); | |
5845 | spin_unlock(&nn->s2s_cp_lock); | |
5846 | } | |
af90f707 | 5847 | |
1da177e4 | 5848 | /* |
a0649b2d CH |
5849 | * Checks for stateid operations |
5850 | */ | |
b37ad28b | 5851 | __be32 |
af90f707 | 5852 | nfs4_preprocess_stateid_op(struct svc_rqst *rqstp, |
aa0d6aed | 5853 | struct nfsd4_compound_state *cstate, struct svc_fh *fhp, |
624322f1 OK |
5854 | stateid_t *stateid, int flags, struct nfsd_file **nfp, |
5855 | struct nfs4_stid **cstid) | |
1da177e4 | 5856 | { |
a0649b2d | 5857 | struct inode *ino = d_inode(fhp->fh_dentry); |
af90f707 | 5858 | struct net *net = SVC_NET(rqstp); |
3320fef1 | 5859 | struct nfsd_net *nn = net_generic(net, nfsd_net_id); |
af90f707 | 5860 | struct nfs4_stid *s = NULL; |
b37ad28b | 5861 | __be32 status; |
1da177e4 | 5862 | |
5c4583b2 JL |
5863 | if (nfp) |
5864 | *nfp = NULL; | |
1da177e4 | 5865 | |
5ccb0066 | 5866 | if (grace_disallows_io(net, ino)) |
1da177e4 LT |
5867 | return nfserr_grace; |
5868 | ||
af90f707 CH |
5869 | if (ZERO_STATEID(stateid) || ONE_STATEID(stateid)) { |
5870 | status = check_special_stateids(net, fhp, stateid, flags); | |
5871 | goto done; | |
5872 | } | |
1da177e4 | 5873 | |
2dd6e458 | 5874 | status = nfsd4_lookup_stateid(cstate, stateid, |
db24b3b4 | 5875 | NFS4_DELEG_STID|NFS4_OPEN_STID|NFS4_LOCK_STID, |
2dd6e458 | 5876 | &s, nn); |
b7342204 OK |
5877 | if (status == nfserr_bad_stateid) |
5878 | status = find_cpntf_state(nn, stateid, &s); | |
38c2f4b1 | 5879 | if (status) |
c2d1d6a8 | 5880 | return status; |
03da3169 | 5881 | status = nfsd4_stid_check_stateid_generation(stateid, s, |
a0649b2d | 5882 | nfsd4_has_session(cstate)); |
69064a27 BF |
5883 | if (status) |
5884 | goto out; | |
a0649b2d | 5885 | |
f7a4d872 BF |
5886 | switch (s->sc_type) { |
5887 | case NFS4_DELEG_STID: | |
a0649b2d | 5888 | status = nfs4_check_delegmode(delegstateid(s), flags); |
f7a4d872 BF |
5889 | break; |
5890 | case NFS4_OPEN_STID: | |
5891 | case NFS4_LOCK_STID: | |
d8836f77 | 5892 | status = nfs4_check_olstateid(openlockstateid(s), flags); |
f7a4d872 BF |
5893 | break; |
5894 | default: | |
14bcab1a | 5895 | status = nfserr_bad_stateid; |
a0649b2d CH |
5896 | break; |
5897 | } | |
8fcd461d JL |
5898 | if (status) |
5899 | goto out; | |
5900 | status = nfs4_check_fh(fhp, s); | |
a0649b2d | 5901 | |
af90f707 | 5902 | done: |
5c4583b2 JL |
5903 | if (status == nfs_ok && nfp) |
5904 | status = nfs4_check_file(rqstp, fhp, s, nfp, flags); | |
1da177e4 | 5905 | out: |
624322f1 OK |
5906 | if (s) { |
5907 | if (!status && cstid) | |
5908 | *cstid = s; | |
5909 | else | |
5910 | nfs4_put_stid(s); | |
5911 | } | |
1da177e4 LT |
5912 | return status; |
5913 | } | |
5914 | ||
17456804 BS |
5915 | /* |
5916 | * Test if the stateid is valid | |
5917 | */ | |
5918 | __be32 | |
5919 | nfsd4_test_stateid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, | |
eb69853d | 5920 | union nfsd4_op_u *u) |
17456804 | 5921 | { |
eb69853d | 5922 | struct nfsd4_test_stateid *test_stateid = &u->test_stateid; |
03cfb420 BS |
5923 | struct nfsd4_test_stateid_id *stateid; |
5924 | struct nfs4_client *cl = cstate->session->se_client; | |
5925 | ||
03cfb420 | 5926 | list_for_each_entry(stateid, &test_stateid->ts_stateid_list, ts_id_list) |
7df302f7 CL |
5927 | stateid->ts_id_status = |
5928 | nfsd4_validate_stateid(cl, &stateid->ts_id_stateid); | |
03cfb420 | 5929 | |
17456804 BS |
5930 | return nfs_ok; |
5931 | } | |
5932 | ||
42691398 CL |
5933 | static __be32 |
5934 | nfsd4_free_lock_stateid(stateid_t *stateid, struct nfs4_stid *s) | |
5935 | { | |
5936 | struct nfs4_ol_stateid *stp = openlockstateid(s); | |
5937 | __be32 ret; | |
5938 | ||
659aefb6 TM |
5939 | ret = nfsd4_lock_ol_stateid(stp); |
5940 | if (ret) | |
5941 | goto out_put_stid; | |
42691398 CL |
5942 | |
5943 | ret = check_stateid_generation(stateid, &s->sc_stateid, 1); | |
5944 | if (ret) | |
5945 | goto out; | |
5946 | ||
5947 | ret = nfserr_locks_held; | |
5948 | if (check_for_locks(stp->st_stid.sc_file, | |
5949 | lockowner(stp->st_stateowner))) | |
5950 | goto out; | |
5951 | ||
5952 | release_lock_stateid(stp); | |
5953 | ret = nfs_ok; | |
5954 | ||
5955 | out: | |
5956 | mutex_unlock(&stp->st_mutex); | |
659aefb6 | 5957 | out_put_stid: |
42691398 CL |
5958 | nfs4_put_stid(s); |
5959 | return ret; | |
5960 | } | |
5961 | ||
e1ca12df BS |
5962 | __be32 |
5963 | nfsd4_free_stateid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, | |
eb69853d | 5964 | union nfsd4_op_u *u) |
e1ca12df | 5965 | { |
eb69853d | 5966 | struct nfsd4_free_stateid *free_stateid = &u->free_stateid; |
e1ca12df | 5967 | stateid_t *stateid = &free_stateid->fr_stateid; |
2da1cec7 | 5968 | struct nfs4_stid *s; |
3bd64a5b | 5969 | struct nfs4_delegation *dp; |
38c2f4b1 | 5970 | struct nfs4_client *cl = cstate->session->se_client; |
2da1cec7 | 5971 | __be32 ret = nfserr_bad_stateid; |
e1ca12df | 5972 | |
1af71cc8 JL |
5973 | spin_lock(&cl->cl_lock); |
5974 | s = find_stateid_locked(cl, stateid); | |
2da1cec7 | 5975 | if (!s) |
1af71cc8 | 5976 | goto out_unlock; |
03da3169 | 5977 | spin_lock(&s->sc_lock); |
2da1cec7 BF |
5978 | switch (s->sc_type) { |
5979 | case NFS4_DELEG_STID: | |
e1ca12df | 5980 | ret = nfserr_locks_held; |
1af71cc8 | 5981 | break; |
2da1cec7 | 5982 | case NFS4_OPEN_STID: |
2da1cec7 BF |
5983 | ret = check_stateid_generation(stateid, &s->sc_stateid, 1); |
5984 | if (ret) | |
1af71cc8 JL |
5985 | break; |
5986 | ret = nfserr_locks_held; | |
f7a4d872 | 5987 | break; |
1af71cc8 | 5988 | case NFS4_LOCK_STID: |
03da3169 | 5989 | spin_unlock(&s->sc_lock); |
a15dfcd5 | 5990 | refcount_inc(&s->sc_count); |
1af71cc8 | 5991 | spin_unlock(&cl->cl_lock); |
42691398 | 5992 | ret = nfsd4_free_lock_stateid(stateid, s); |
1af71cc8 | 5993 | goto out; |
3bd64a5b | 5994 | case NFS4_REVOKED_DELEG_STID: |
03da3169 | 5995 | spin_unlock(&s->sc_lock); |
3bd64a5b | 5996 | dp = delegstateid(s); |
2d4a532d JL |
5997 | list_del_init(&dp->dl_recall_lru); |
5998 | spin_unlock(&cl->cl_lock); | |
6011695d | 5999 | nfs4_put_stid(s); |
3bd64a5b | 6000 | ret = nfs_ok; |
1af71cc8 JL |
6001 | goto out; |
6002 | /* Default falls through and returns nfserr_bad_stateid */ | |
e1ca12df | 6003 | } |
03da3169 | 6004 | spin_unlock(&s->sc_lock); |
1af71cc8 JL |
6005 | out_unlock: |
6006 | spin_unlock(&cl->cl_lock); | |
e1ca12df | 6007 | out: |
e1ca12df BS |
6008 | return ret; |
6009 | } | |
6010 | ||
4c4cd222 N |
6011 | static inline int |
6012 | setlkflg (int type) | |
6013 | { | |
6014 | return (type == NFS4_READW_LT || type == NFS4_READ_LT) ? | |
6015 | RD_STATE : WR_STATE; | |
6016 | } | |
1da177e4 | 6017 | |
dcef0413 | 6018 | static __be32 nfs4_seqid_op_checks(struct nfsd4_compound_state *cstate, stateid_t *stateid, u32 seqid, struct nfs4_ol_stateid *stp) |
c0a5d93e BF |
6019 | { |
6020 | struct svc_fh *current_fh = &cstate->current_fh; | |
6021 | struct nfs4_stateowner *sop = stp->st_stateowner; | |
6022 | __be32 status; | |
6023 | ||
c0a5d93e BF |
6024 | status = nfsd4_check_seqid(cstate, sop, seqid); |
6025 | if (status) | |
6026 | return status; | |
9271d7e5 TM |
6027 | status = nfsd4_lock_ol_stateid(stp); |
6028 | if (status != nfs_ok) | |
6029 | return status; | |
f7a4d872 | 6030 | status = check_stateid_generation(stateid, &stp->st_stid.sc_stateid, nfsd4_has_session(cstate)); |
35a92fe8 JL |
6031 | if (status == nfs_ok) |
6032 | status = nfs4_check_fh(current_fh, &stp->st_stid); | |
6033 | if (status != nfs_ok) | |
feb9dad5 | 6034 | mutex_unlock(&stp->st_mutex); |
35a92fe8 | 6035 | return status; |
c0a5d93e BF |
6036 | } |
6037 | ||
1da177e4 LT |
6038 | /* |
6039 | * Checks for sequence id mutating operations. | |
6040 | */ | |
b37ad28b | 6041 | static __be32 |
dd453dfd | 6042 | nfs4_preprocess_seqid_op(struct nfsd4_compound_state *cstate, u32 seqid, |
2288d0e3 | 6043 | stateid_t *stateid, char typemask, |
3320fef1 SK |
6044 | struct nfs4_ol_stateid **stpp, |
6045 | struct nfsd_net *nn) | |
1da177e4 | 6046 | { |
0836f587 | 6047 | __be32 status; |
38c2f4b1 | 6048 | struct nfs4_stid *s; |
e17f99b7 | 6049 | struct nfs4_ol_stateid *stp = NULL; |
1da177e4 | 6050 | |
dd5e3fbc | 6051 | trace_nfsd_preprocess(seqid, stateid); |
3a4f98bb | 6052 | |
1da177e4 | 6053 | *stpp = NULL; |
2dd6e458 | 6054 | status = nfsd4_lookup_stateid(cstate, stateid, typemask, &s, nn); |
c0a5d93e BF |
6055 | if (status) |
6056 | return status; | |
e17f99b7 | 6057 | stp = openlockstateid(s); |
58fb12e6 | 6058 | nfsd4_cstate_assign_replay(cstate, stp->st_stateowner); |
1da177e4 | 6059 | |
e17f99b7 | 6060 | status = nfs4_seqid_op_checks(cstate, stateid, seqid, stp); |
fd911011 | 6061 | if (!status) |
e17f99b7 | 6062 | *stpp = stp; |
fd911011 TM |
6063 | else |
6064 | nfs4_put_stid(&stp->st_stid); | |
e17f99b7 | 6065 | return status; |
c0a5d93e | 6066 | } |
39325bd0 | 6067 | |
3320fef1 SK |
6068 | static __be32 nfs4_preprocess_confirmed_seqid_op(struct nfsd4_compound_state *cstate, u32 seqid, |
6069 | stateid_t *stateid, struct nfs4_ol_stateid **stpp, struct nfsd_net *nn) | |
c0a5d93e BF |
6070 | { |
6071 | __be32 status; | |
6072 | struct nfs4_openowner *oo; | |
4cbfc9f7 | 6073 | struct nfs4_ol_stateid *stp; |
1da177e4 | 6074 | |
c0a5d93e | 6075 | status = nfs4_preprocess_seqid_op(cstate, seqid, stateid, |
4cbfc9f7 | 6076 | NFS4_OPEN_STID, &stp, nn); |
7a8711c9 BF |
6077 | if (status) |
6078 | return status; | |
4cbfc9f7 TM |
6079 | oo = openowner(stp->st_stateowner); |
6080 | if (!(oo->oo_flags & NFS4_OO_CONFIRMED)) { | |
feb9dad5 | 6081 | mutex_unlock(&stp->st_mutex); |
4cbfc9f7 | 6082 | nfs4_put_stid(&stp->st_stid); |
3a4f98bb | 6083 | return nfserr_bad_stateid; |
4cbfc9f7 TM |
6084 | } |
6085 | *stpp = stp; | |
3a4f98bb | 6086 | return nfs_ok; |
1da177e4 LT |
6087 | } |
6088 | ||
b37ad28b | 6089 | __be32 |
ca364317 | 6090 | nfsd4_open_confirm(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, |
eb69853d | 6091 | union nfsd4_op_u *u) |
1da177e4 | 6092 | { |
eb69853d | 6093 | struct nfsd4_open_confirm *oc = &u->open_confirm; |
b37ad28b | 6094 | __be32 status; |
fe0750e5 | 6095 | struct nfs4_openowner *oo; |
dcef0413 | 6096 | struct nfs4_ol_stateid *stp; |
3320fef1 | 6097 | struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id); |
1da177e4 | 6098 | |
a6a9f18f AV |
6099 | dprintk("NFSD: nfsd4_open_confirm on file %pd\n", |
6100 | cstate->current_fh.fh_dentry); | |
1da177e4 | 6101 | |
ca364317 | 6102 | status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0); |
a8cddc5d BF |
6103 | if (status) |
6104 | return status; | |
1da177e4 | 6105 | |
9072d5c6 | 6106 | status = nfs4_preprocess_seqid_op(cstate, |
ca364317 | 6107 | oc->oc_seqid, &oc->oc_req_stateid, |
3320fef1 | 6108 | NFS4_OPEN_STID, &stp, nn); |
9072d5c6 | 6109 | if (status) |
68b66e82 | 6110 | goto out; |
fe0750e5 | 6111 | oo = openowner(stp->st_stateowner); |
68b66e82 | 6112 | status = nfserr_bad_stateid; |
35a92fe8 | 6113 | if (oo->oo_flags & NFS4_OO_CONFIRMED) { |
feb9dad5 | 6114 | mutex_unlock(&stp->st_mutex); |
2585fc79 | 6115 | goto put_stateid; |
35a92fe8 | 6116 | } |
dad1c067 | 6117 | oo->oo_flags |= NFS4_OO_CONFIRMED; |
9767feb2 | 6118 | nfs4_inc_and_copy_stateid(&oc->oc_resp_stateid, &stp->st_stid); |
feb9dad5 | 6119 | mutex_unlock(&stp->st_mutex); |
dd5e3fbc | 6120 | trace_nfsd_open_confirm(oc->oc_seqid, &stp->st_stid.sc_stateid); |
2a4317c5 | 6121 | nfsd4_client_record_create(oo->oo_owner.so_client); |
68b66e82 | 6122 | status = nfs_ok; |
2585fc79 TM |
6123 | put_stateid: |
6124 | nfs4_put_stid(&stp->st_stid); | |
1da177e4 | 6125 | out: |
9411b1d4 | 6126 | nfsd4_bump_seqid(cstate, status); |
1da177e4 LT |
6127 | return status; |
6128 | } | |
6129 | ||
6409a5a6 | 6130 | static inline void nfs4_stateid_downgrade_bit(struct nfs4_ol_stateid *stp, u32 access) |
1da177e4 | 6131 | { |
82c5ff1b | 6132 | if (!test_access(access, stp)) |
6409a5a6 | 6133 | return; |
11b9164a | 6134 | nfs4_file_put_access(stp->st_stid.sc_file, access); |
82c5ff1b | 6135 | clear_access(access, stp); |
6409a5a6 | 6136 | } |
f197c271 | 6137 | |
6409a5a6 BF |
6138 | static inline void nfs4_stateid_downgrade(struct nfs4_ol_stateid *stp, u32 to_access) |
6139 | { | |
6140 | switch (to_access) { | |
6141 | case NFS4_SHARE_ACCESS_READ: | |
6142 | nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_WRITE); | |
6143 | nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_BOTH); | |
6144 | break; | |
6145 | case NFS4_SHARE_ACCESS_WRITE: | |
6146 | nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_READ); | |
6147 | nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_BOTH); | |
6148 | break; | |
6149 | case NFS4_SHARE_ACCESS_BOTH: | |
6150 | break; | |
6151 | default: | |
063b0fb9 | 6152 | WARN_ON_ONCE(1); |
1da177e4 LT |
6153 | } |
6154 | } | |
6155 | ||
b37ad28b | 6156 | __be32 |
ca364317 | 6157 | nfsd4_open_downgrade(struct svc_rqst *rqstp, |
eb69853d | 6158 | struct nfsd4_compound_state *cstate, union nfsd4_op_u *u) |
1da177e4 | 6159 | { |
eb69853d | 6160 | struct nfsd4_open_downgrade *od = &u->open_downgrade; |
b37ad28b | 6161 | __be32 status; |
dcef0413 | 6162 | struct nfs4_ol_stateid *stp; |
3320fef1 | 6163 | struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id); |
1da177e4 | 6164 | |
a6a9f18f AV |
6165 | dprintk("NFSD: nfsd4_open_downgrade on file %pd\n", |
6166 | cstate->current_fh.fh_dentry); | |
1da177e4 | 6167 | |
c30e92df | 6168 | /* We don't yet support WANT bits: */ |
2c8bd7e0 BH |
6169 | if (od->od_deleg_want) |
6170 | dprintk("NFSD: %s: od_deleg_want=0x%x ignored\n", __func__, | |
6171 | od->od_deleg_want); | |
1da177e4 | 6172 | |
c0a5d93e | 6173 | status = nfs4_preprocess_confirmed_seqid_op(cstate, od->od_seqid, |
3320fef1 | 6174 | &od->od_stateid, &stp, nn); |
9072d5c6 | 6175 | if (status) |
1da177e4 | 6176 | goto out; |
1da177e4 | 6177 | status = nfserr_inval; |
82c5ff1b | 6178 | if (!test_access(od->od_share_access, stp)) { |
c11c591f | 6179 | dprintk("NFSD: access not a subset of current bitmap: 0x%hhx, input access=%08x\n", |
1da177e4 | 6180 | stp->st_access_bmap, od->od_share_access); |
0667b1e9 | 6181 | goto put_stateid; |
1da177e4 | 6182 | } |
ce0fc43c | 6183 | if (!test_deny(od->od_share_deny, stp)) { |
c11c591f | 6184 | dprintk("NFSD: deny not a subset of current bitmap: 0x%hhx, input deny=%08x\n", |
1da177e4 | 6185 | stp->st_deny_bmap, od->od_share_deny); |
0667b1e9 | 6186 | goto put_stateid; |
1da177e4 | 6187 | } |
6409a5a6 | 6188 | nfs4_stateid_downgrade(stp, od->od_share_access); |
ce0fc43c | 6189 | reset_union_bmap_deny(od->od_share_deny, stp); |
9767feb2 | 6190 | nfs4_inc_and_copy_stateid(&od->od_stateid, &stp->st_stid); |
1da177e4 | 6191 | status = nfs_ok; |
0667b1e9 | 6192 | put_stateid: |
feb9dad5 | 6193 | mutex_unlock(&stp->st_mutex); |
0667b1e9 | 6194 | nfs4_put_stid(&stp->st_stid); |
1da177e4 | 6195 | out: |
9411b1d4 | 6196 | nfsd4_bump_seqid(cstate, status); |
1da177e4 LT |
6197 | return status; |
6198 | } | |
6199 | ||
f7a4d872 BF |
6200 | static void nfsd4_close_open_stateid(struct nfs4_ol_stateid *s) |
6201 | { | |
acf9295b | 6202 | struct nfs4_client *clp = s->st_stid.sc_client; |
e8568739 | 6203 | bool unhashed; |
d83017f9 | 6204 | LIST_HEAD(reaplist); |
acf9295b | 6205 | |
2c41beb0 | 6206 | spin_lock(&clp->cl_lock); |
e8568739 | 6207 | unhashed = unhash_open_stateid(s, &reaplist); |
acf9295b | 6208 | |
d83017f9 | 6209 | if (clp->cl_minorversion) { |
e8568739 JL |
6210 | if (unhashed) |
6211 | put_ol_stateid_locked(s, &reaplist); | |
d83017f9 JL |
6212 | spin_unlock(&clp->cl_lock); |
6213 | free_ol_stateid_reaplist(&reaplist); | |
6214 | } else { | |
6215 | spin_unlock(&clp->cl_lock); | |
6216 | free_ol_stateid_reaplist(&reaplist); | |
e8568739 JL |
6217 | if (unhashed) |
6218 | move_to_close_lru(s, clp->net); | |
d83017f9 | 6219 | } |
38c387b5 BF |
6220 | } |
6221 | ||
1da177e4 LT |
6222 | /* |
6223 | * nfs4_unlock_state() called after encode | |
6224 | */ | |
b37ad28b | 6225 | __be32 |
ca364317 | 6226 | nfsd4_close(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, |
eb69853d | 6227 | union nfsd4_op_u *u) |
1da177e4 | 6228 | { |
eb69853d | 6229 | struct nfsd4_close *close = &u->close; |
b37ad28b | 6230 | __be32 status; |
dcef0413 | 6231 | struct nfs4_ol_stateid *stp; |
3320fef1 SK |
6232 | struct net *net = SVC_NET(rqstp); |
6233 | struct nfsd_net *nn = net_generic(net, nfsd_net_id); | |
1da177e4 | 6234 | |
a6a9f18f AV |
6235 | dprintk("NFSD: nfsd4_close on file %pd\n", |
6236 | cstate->current_fh.fh_dentry); | |
1da177e4 | 6237 | |
f7a4d872 BF |
6238 | status = nfs4_preprocess_seqid_op(cstate, close->cl_seqid, |
6239 | &close->cl_stateid, | |
6240 | NFS4_OPEN_STID|NFS4_CLOSED_STID, | |
3320fef1 | 6241 | &stp, nn); |
9411b1d4 | 6242 | nfsd4_bump_seqid(cstate, status); |
9072d5c6 | 6243 | if (status) |
1da177e4 | 6244 | goto out; |
15ca08d3 TM |
6245 | |
6246 | stp->st_stid.sc_type = NFS4_CLOSED_STID; | |
bd2decac JL |
6247 | |
6248 | /* | |
6249 | * Technically we don't _really_ have to increment or copy it, since | |
6250 | * it should just be gone after this operation and we clobber the | |
6251 | * copied value below, but we continue to do so here just to ensure | |
6252 | * that racing ops see that there was a state change. | |
6253 | */ | |
9767feb2 | 6254 | nfs4_inc_and_copy_stateid(&close->cl_stateid, &stp->st_stid); |
1da177e4 | 6255 | |
f7a4d872 | 6256 | nfsd4_close_open_stateid(stp); |
15ca08d3 | 6257 | mutex_unlock(&stp->st_mutex); |
8a0b589d | 6258 | |
bd2decac JL |
6259 | /* v4.1+ suggests that we send a special stateid in here, since the |
6260 | * clients should just ignore this anyway. Since this is not useful | |
6261 | * for v4.0 clients either, we set it to the special close_stateid | |
6262 | * universally. | |
6263 | * | |
6264 | * See RFC5661 section 18.2.4, and RFC7530 section 16.2.5 | |
6265 | */ | |
6266 | memcpy(&close->cl_stateid, &close_stateid, sizeof(close->cl_stateid)); | |
fb500a7c | 6267 | |
8a0b589d TM |
6268 | /* put reference from nfs4_preprocess_seqid_op */ |
6269 | nfs4_put_stid(&stp->st_stid); | |
1da177e4 | 6270 | out: |
1da177e4 LT |
6271 | return status; |
6272 | } | |
6273 | ||
b37ad28b | 6274 | __be32 |
ca364317 | 6275 | nfsd4_delegreturn(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, |
eb69853d | 6276 | union nfsd4_op_u *u) |
1da177e4 | 6277 | { |
eb69853d | 6278 | struct nfsd4_delegreturn *dr = &u->delegreturn; |
203a8c8e BF |
6279 | struct nfs4_delegation *dp; |
6280 | stateid_t *stateid = &dr->dr_stateid; | |
38c2f4b1 | 6281 | struct nfs4_stid *s; |
b37ad28b | 6282 | __be32 status; |
3320fef1 | 6283 | struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id); |
1da177e4 | 6284 | |
ca364317 | 6285 | if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0))) |
203a8c8e | 6286 | return status; |
1da177e4 | 6287 | |
2dd6e458 | 6288 | status = nfsd4_lookup_stateid(cstate, stateid, NFS4_DELEG_STID, &s, nn); |
38c2f4b1 | 6289 | if (status) |
203a8c8e | 6290 | goto out; |
38c2f4b1 | 6291 | dp = delegstateid(s); |
03da3169 | 6292 | status = nfsd4_stid_check_stateid_generation(stateid, &dp->dl_stid, nfsd4_has_session(cstate)); |
203a8c8e | 6293 | if (status) |
fd911011 | 6294 | goto put_stateid; |
203a8c8e | 6295 | |
3bd64a5b | 6296 | destroy_delegation(dp); |
fd911011 TM |
6297 | put_stateid: |
6298 | nfs4_put_stid(&dp->dl_stid); | |
1da177e4 LT |
6299 | out: |
6300 | return status; | |
6301 | } | |
6302 | ||
87df4de8 BH |
6303 | static inline u64 |
6304 | end_offset(u64 start, u64 len) | |
6305 | { | |
6306 | u64 end; | |
6307 | ||
6308 | end = start + len; | |
6309 | return end >= start ? end: NFS4_MAX_UINT64; | |
6310 | } | |
6311 | ||
6312 | /* last octet in a range */ | |
6313 | static inline u64 | |
6314 | last_byte_offset(u64 start, u64 len) | |
6315 | { | |
6316 | u64 end; | |
6317 | ||
063b0fb9 | 6318 | WARN_ON_ONCE(!len); |
87df4de8 BH |
6319 | end = start + len; |
6320 | return end > start ? end - 1: NFS4_MAX_UINT64; | |
6321 | } | |
6322 | ||
1da177e4 LT |
6323 | /* |
6324 | * TODO: Linux file offsets are _signed_ 64-bit quantities, which means that | |
6325 | * we can't properly handle lock requests that go beyond the (2^63 - 1)-th | |
6326 | * byte, because of sign extension problems. Since NFSv4 calls for 64-bit | |
6327 | * locking, this prevents us from being completely protocol-compliant. The | |
6328 | * real solution to this problem is to start using unsigned file offsets in | |
6329 | * the VFS, but this is a very deep change! | |
6330 | */ | |
6331 | static inline void | |
6332 | nfs4_transform_lock_offset(struct file_lock *lock) | |
6333 | { | |
6334 | if (lock->fl_start < 0) | |
6335 | lock->fl_start = OFFSET_MAX; | |
6336 | if (lock->fl_end < 0) | |
6337 | lock->fl_end = OFFSET_MAX; | |
6338 | } | |
6339 | ||
cae80b30 JL |
6340 | static fl_owner_t |
6341 | nfsd4_fl_get_owner(fl_owner_t owner) | |
aef9583b | 6342 | { |
cae80b30 JL |
6343 | struct nfs4_lockowner *lo = (struct nfs4_lockowner *)owner; |
6344 | ||
6345 | nfs4_get_stateowner(&lo->lo_owner); | |
6346 | return owner; | |
aef9583b KM |
6347 | } |
6348 | ||
cae80b30 JL |
6349 | static void |
6350 | nfsd4_fl_put_owner(fl_owner_t owner) | |
aef9583b | 6351 | { |
cae80b30 | 6352 | struct nfs4_lockowner *lo = (struct nfs4_lockowner *)owner; |
aef9583b | 6353 | |
cae80b30 | 6354 | if (lo) |
aef9583b | 6355 | nfs4_put_stateowner(&lo->lo_owner); |
aef9583b KM |
6356 | } |
6357 | ||
76d348fa JL |
6358 | static void |
6359 | nfsd4_lm_notify(struct file_lock *fl) | |
6360 | { | |
6361 | struct nfs4_lockowner *lo = (struct nfs4_lockowner *)fl->fl_owner; | |
6362 | struct net *net = lo->lo_owner.so_client->net; | |
6363 | struct nfsd_net *nn = net_generic(net, nfsd_net_id); | |
6364 | struct nfsd4_blocked_lock *nbl = container_of(fl, | |
6365 | struct nfsd4_blocked_lock, nbl_lock); | |
6366 | bool queue = false; | |
6367 | ||
7919d0a2 | 6368 | /* An empty list means that something else is going to be using it */ |
0cc11a61 | 6369 | spin_lock(&nn->blocked_locks_lock); |
76d348fa JL |
6370 | if (!list_empty(&nbl->nbl_list)) { |
6371 | list_del_init(&nbl->nbl_list); | |
7919d0a2 | 6372 | list_del_init(&nbl->nbl_lru); |
76d348fa JL |
6373 | queue = true; |
6374 | } | |
0cc11a61 | 6375 | spin_unlock(&nn->blocked_locks_lock); |
76d348fa JL |
6376 | |
6377 | if (queue) | |
6378 | nfsd4_run_cb(&nbl->nbl_cb); | |
6379 | } | |
6380 | ||
7b021967 | 6381 | static const struct lock_manager_operations nfsd_posix_mng_ops = { |
76d348fa | 6382 | .lm_notify = nfsd4_lm_notify, |
aef9583b KM |
6383 | .lm_get_owner = nfsd4_fl_get_owner, |
6384 | .lm_put_owner = nfsd4_fl_put_owner, | |
d5b9026a | 6385 | }; |
1da177e4 LT |
6386 | |
6387 | static inline void | |
6388 | nfs4_set_lock_denied(struct file_lock *fl, struct nfsd4_lock_denied *deny) | |
6389 | { | |
fe0750e5 | 6390 | struct nfs4_lockowner *lo; |
1da177e4 | 6391 | |
d5b9026a | 6392 | if (fl->fl_lmops == &nfsd_posix_mng_ops) { |
fe0750e5 | 6393 | lo = (struct nfs4_lockowner *) fl->fl_owner; |
6f4859b8 BF |
6394 | xdr_netobj_dup(&deny->ld_owner, &lo->lo_owner.so_owner, |
6395 | GFP_KERNEL); | |
7c13f344 BF |
6396 | if (!deny->ld_owner.data) |
6397 | /* We just don't care that much */ | |
6398 | goto nevermind; | |
fe0750e5 | 6399 | deny->ld_clientid = lo->lo_owner.so_client->cl_clientid; |
d5b9026a | 6400 | } else { |
7c13f344 BF |
6401 | nevermind: |
6402 | deny->ld_owner.len = 0; | |
6403 | deny->ld_owner.data = NULL; | |
d5b9026a N |
6404 | deny->ld_clientid.cl_boot = 0; |
6405 | deny->ld_clientid.cl_id = 0; | |
1da177e4 LT |
6406 | } |
6407 | deny->ld_start = fl->fl_start; | |
87df4de8 BH |
6408 | deny->ld_length = NFS4_MAX_UINT64; |
6409 | if (fl->fl_end != NFS4_MAX_UINT64) | |
1da177e4 LT |
6410 | deny->ld_length = fl->fl_end - fl->fl_start + 1; |
6411 | deny->ld_type = NFS4_READ_LT; | |
6412 | if (fl->fl_type != F_RDLCK) | |
6413 | deny->ld_type = NFS4_WRITE_LT; | |
6414 | } | |
6415 | ||
fe0750e5 | 6416 | static struct nfs4_lockowner * |
c8623999 | 6417 | find_lockowner_str_locked(struct nfs4_client *clp, struct xdr_netobj *owner) |
1da177e4 | 6418 | { |
d4f0489f | 6419 | unsigned int strhashval = ownerstr_hashval(owner); |
b3c32bcd | 6420 | struct nfs4_stateowner *so; |
1da177e4 | 6421 | |
0a880a28 TM |
6422 | lockdep_assert_held(&clp->cl_lock); |
6423 | ||
d4f0489f TM |
6424 | list_for_each_entry(so, &clp->cl_ownerstr_hashtbl[strhashval], |
6425 | so_strhash) { | |
b3c32bcd TM |
6426 | if (so->so_is_open_owner) |
6427 | continue; | |
b5971afa KM |
6428 | if (same_owner_str(so, owner)) |
6429 | return lockowner(nfs4_get_stateowner(so)); | |
1da177e4 LT |
6430 | } |
6431 | return NULL; | |
6432 | } | |
6433 | ||
c58c6610 | 6434 | static struct nfs4_lockowner * |
c8623999 | 6435 | find_lockowner_str(struct nfs4_client *clp, struct xdr_netobj *owner) |
c58c6610 TM |
6436 | { |
6437 | struct nfs4_lockowner *lo; | |
6438 | ||
d4f0489f | 6439 | spin_lock(&clp->cl_lock); |
c8623999 | 6440 | lo = find_lockowner_str_locked(clp, owner); |
d4f0489f | 6441 | spin_unlock(&clp->cl_lock); |
c58c6610 TM |
6442 | return lo; |
6443 | } | |
6444 | ||
8f4b54c5 JL |
6445 | static void nfs4_unhash_lockowner(struct nfs4_stateowner *sop) |
6446 | { | |
c58c6610 | 6447 | unhash_lockowner_locked(lockowner(sop)); |
8f4b54c5 JL |
6448 | } |
6449 | ||
6b180f0b JL |
6450 | static void nfs4_free_lockowner(struct nfs4_stateowner *sop) |
6451 | { | |
6452 | struct nfs4_lockowner *lo = lockowner(sop); | |
6453 | ||
6454 | kmem_cache_free(lockowner_slab, lo); | |
6455 | } | |
6456 | ||
6457 | static const struct nfs4_stateowner_operations lockowner_ops = { | |
8f4b54c5 JL |
6458 | .so_unhash = nfs4_unhash_lockowner, |
6459 | .so_free = nfs4_free_lockowner, | |
6b180f0b JL |
6460 | }; |
6461 | ||
1da177e4 LT |
6462 | /* |
6463 | * Alloc a lock owner structure. | |
6464 | * Called in nfsd4_lock - therefore, OPEN and OPEN_CONFIRM (if needed) has | |
25985edc | 6465 | * occurred. |
1da177e4 | 6466 | * |
16bfdaaf | 6467 | * strhashval = ownerstr_hashval |
1da177e4 | 6468 | */ |
fe0750e5 | 6469 | static struct nfs4_lockowner * |
c58c6610 TM |
6470 | alloc_init_lock_stateowner(unsigned int strhashval, struct nfs4_client *clp, |
6471 | struct nfs4_ol_stateid *open_stp, | |
6472 | struct nfsd4_lock *lock) | |
6473 | { | |
c58c6610 | 6474 | struct nfs4_lockowner *lo, *ret; |
1da177e4 | 6475 | |
fe0750e5 BF |
6476 | lo = alloc_stateowner(lockowner_slab, &lock->lk_new_owner, clp); |
6477 | if (!lo) | |
1da177e4 | 6478 | return NULL; |
76d348fa | 6479 | INIT_LIST_HEAD(&lo->lo_blocked); |
fe0750e5 BF |
6480 | INIT_LIST_HEAD(&lo->lo_owner.so_stateids); |
6481 | lo->lo_owner.so_is_open_owner = 0; | |
5db1c03f | 6482 | lo->lo_owner.so_seqid = lock->lk_new_lock_seqid; |
6b180f0b | 6483 | lo->lo_owner.so_ops = &lockowner_ops; |
d4f0489f | 6484 | spin_lock(&clp->cl_lock); |
c8623999 | 6485 | ret = find_lockowner_str_locked(clp, &lock->lk_new_owner); |
c58c6610 TM |
6486 | if (ret == NULL) { |
6487 | list_add(&lo->lo_owner.so_strhash, | |
d4f0489f | 6488 | &clp->cl_ownerstr_hashtbl[strhashval]); |
c58c6610 TM |
6489 | ret = lo; |
6490 | } else | |
d50ffded KM |
6491 | nfs4_free_stateowner(&lo->lo_owner); |
6492 | ||
d4f0489f | 6493 | spin_unlock(&clp->cl_lock); |
340f0ba1 | 6494 | return ret; |
1da177e4 LT |
6495 | } |
6496 | ||
fd1fd685 | 6497 | static struct nfs4_ol_stateid * |
a451b123 TM |
6498 | find_lock_stateid(const struct nfs4_lockowner *lo, |
6499 | const struct nfs4_ol_stateid *ost) | |
fd1fd685 TM |
6500 | { |
6501 | struct nfs4_ol_stateid *lst; | |
fd1fd685 | 6502 | |
a451b123 | 6503 | lockdep_assert_held(&ost->st_stid.sc_client->cl_lock); |
fd1fd685 | 6504 | |
a451b123 TM |
6505 | /* If ost is not hashed, ost->st_locks will not be valid */ |
6506 | if (!nfs4_ol_stateid_unhashed(ost)) | |
6507 | list_for_each_entry(lst, &ost->st_locks, st_locks) { | |
6508 | if (lst->st_stateowner == &lo->lo_owner) { | |
6509 | refcount_inc(&lst->st_stid.sc_count); | |
6510 | return lst; | |
6511 | } | |
fd1fd685 | 6512 | } |
fd1fd685 TM |
6513 | return NULL; |
6514 | } | |
6515 | ||
beeca19c | 6516 | static struct nfs4_ol_stateid * |
356a95ec JL |
6517 | init_lock_stateid(struct nfs4_ol_stateid *stp, struct nfs4_lockowner *lo, |
6518 | struct nfs4_file *fp, struct inode *inode, | |
6519 | struct nfs4_ol_stateid *open_stp) | |
1da177e4 | 6520 | { |
d3b313a4 | 6521 | struct nfs4_client *clp = lo->lo_owner.so_client; |
beeca19c | 6522 | struct nfs4_ol_stateid *retstp; |
1da177e4 | 6523 | |
beeca19c | 6524 | mutex_init(&stp->st_mutex); |
4f34bd05 | 6525 | mutex_lock_nested(&stp->st_mutex, OPEN_STATEID_MUTEX); |
beeca19c TM |
6526 | retry: |
6527 | spin_lock(&clp->cl_lock); | |
a451b123 TM |
6528 | if (nfs4_ol_stateid_unhashed(open_stp)) |
6529 | goto out_close; | |
6530 | retstp = find_lock_stateid(lo, open_stp); | |
beeca19c | 6531 | if (retstp) |
a451b123 | 6532 | goto out_found; |
a15dfcd5 | 6533 | refcount_inc(&stp->st_stid.sc_count); |
3abdb607 | 6534 | stp->st_stid.sc_type = NFS4_LOCK_STID; |
b5971afa | 6535 | stp->st_stateowner = nfs4_get_stateowner(&lo->lo_owner); |
13cd2184 | 6536 | get_nfs4_file(fp); |
11b9164a | 6537 | stp->st_stid.sc_file = fp; |
0997b173 | 6538 | stp->st_access_bmap = 0; |
1da177e4 | 6539 | stp->st_deny_bmap = open_stp->st_deny_bmap; |
4c4cd222 | 6540 | stp->st_openstp = open_stp; |
a451b123 | 6541 | spin_lock(&fp->fi_lock); |
3c87b9b7 | 6542 | list_add(&stp->st_locks, &open_stp->st_locks); |
1c755dc1 | 6543 | list_add(&stp->st_perstateowner, &lo->lo_owner.so_stateids); |
1d31a253 TM |
6544 | list_add(&stp->st_perfile, &fp->fi_stateids); |
6545 | spin_unlock(&fp->fi_lock); | |
beeca19c | 6546 | spin_unlock(&clp->cl_lock); |
beeca19c | 6547 | return stp; |
a451b123 TM |
6548 | out_found: |
6549 | spin_unlock(&clp->cl_lock); | |
6550 | if (nfsd4_lock_ol_stateid(retstp) != nfs_ok) { | |
6551 | nfs4_put_stid(&retstp->st_stid); | |
6552 | goto retry; | |
6553 | } | |
6554 | /* To keep mutex tracking happy */ | |
6555 | mutex_unlock(&stp->st_mutex); | |
6556 | return retstp; | |
6557 | out_close: | |
6558 | spin_unlock(&clp->cl_lock); | |
6559 | mutex_unlock(&stp->st_mutex); | |
6560 | return NULL; | |
1da177e4 LT |
6561 | } |
6562 | ||
356a95ec JL |
6563 | static struct nfs4_ol_stateid * |
6564 | find_or_create_lock_stateid(struct nfs4_lockowner *lo, struct nfs4_file *fi, | |
6565 | struct inode *inode, struct nfs4_ol_stateid *ost, | |
6566 | bool *new) | |
6567 | { | |
6568 | struct nfs4_stid *ns = NULL; | |
6569 | struct nfs4_ol_stateid *lst; | |
6570 | struct nfs4_openowner *oo = openowner(ost->st_stateowner); | |
6571 | struct nfs4_client *clp = oo->oo_owner.so_client; | |
6572 | ||
beeca19c | 6573 | *new = false; |
356a95ec | 6574 | spin_lock(&clp->cl_lock); |
a451b123 | 6575 | lst = find_lock_stateid(lo, ost); |
356a95ec | 6576 | spin_unlock(&clp->cl_lock); |
beeca19c TM |
6577 | if (lst != NULL) { |
6578 | if (nfsd4_lock_ol_stateid(lst) == nfs_ok) | |
6579 | goto out; | |
6580 | nfs4_put_stid(&lst->st_stid); | |
6581 | } | |
6582 | ns = nfs4_alloc_stid(clp, stateid_slab, nfs4_free_lock_stateid); | |
6583 | if (ns == NULL) | |
6584 | return NULL; | |
6585 | ||
6586 | lst = init_lock_stateid(openlockstateid(ns), lo, fi, inode, ost); | |
6587 | if (lst == openlockstateid(ns)) | |
6588 | *new = true; | |
6589 | else | |
356a95ec | 6590 | nfs4_put_stid(ns); |
beeca19c | 6591 | out: |
356a95ec JL |
6592 | return lst; |
6593 | } | |
c53530da | 6594 | |
fd39ca9a | 6595 | static int |
1da177e4 LT |
6596 | check_lock_length(u64 offset, u64 length) |
6597 | { | |
e7969315 KM |
6598 | return ((length == 0) || ((length != NFS4_MAX_UINT64) && |
6599 | (length > ~offset))); | |
1da177e4 LT |
6600 | } |
6601 | ||
dcef0413 | 6602 | static void get_lock_access(struct nfs4_ol_stateid *lock_stp, u32 access) |
0997b173 | 6603 | { |
11b9164a | 6604 | struct nfs4_file *fp = lock_stp->st_stid.sc_file; |
0997b173 | 6605 | |
7214e860 JL |
6606 | lockdep_assert_held(&fp->fi_lock); |
6607 | ||
82c5ff1b | 6608 | if (test_access(access, lock_stp)) |
0997b173 | 6609 | return; |
12659651 | 6610 | __nfs4_file_get_access(fp, access); |
82c5ff1b | 6611 | set_access(access, lock_stp); |
0997b173 BF |
6612 | } |
6613 | ||
356a95ec JL |
6614 | static __be32 |
6615 | lookup_or_create_lock_state(struct nfsd4_compound_state *cstate, | |
6616 | struct nfs4_ol_stateid *ost, | |
6617 | struct nfsd4_lock *lock, | |
dd257933 | 6618 | struct nfs4_ol_stateid **plst, bool *new) |
64a284d0 | 6619 | { |
5db1c03f | 6620 | __be32 status; |
11b9164a | 6621 | struct nfs4_file *fi = ost->st_stid.sc_file; |
64a284d0 BF |
6622 | struct nfs4_openowner *oo = openowner(ost->st_stateowner); |
6623 | struct nfs4_client *cl = oo->oo_owner.so_client; | |
2b0143b5 | 6624 | struct inode *inode = d_inode(cstate->current_fh.fh_dentry); |
64a284d0 | 6625 | struct nfs4_lockowner *lo; |
dd257933 | 6626 | struct nfs4_ol_stateid *lst; |
64a284d0 BF |
6627 | unsigned int strhashval; |
6628 | ||
c8623999 | 6629 | lo = find_lockowner_str(cl, &lock->lk_new_owner); |
c53530da | 6630 | if (!lo) { |
76f6c9e1 | 6631 | strhashval = ownerstr_hashval(&lock->lk_new_owner); |
c53530da JL |
6632 | lo = alloc_init_lock_stateowner(strhashval, cl, ost, lock); |
6633 | if (lo == NULL) | |
6634 | return nfserr_jukebox; | |
6635 | } else { | |
6636 | /* with an existing lockowner, seqids must be the same */ | |
5db1c03f | 6637 | status = nfserr_bad_seqid; |
c53530da JL |
6638 | if (!cstate->minorversion && |
6639 | lock->lk_new_lock_seqid != lo->lo_owner.so_seqid) | |
5db1c03f | 6640 | goto out; |
64a284d0 | 6641 | } |
c53530da | 6642 | |
dd257933 JL |
6643 | lst = find_or_create_lock_stateid(lo, fi, inode, ost, new); |
6644 | if (lst == NULL) { | |
5db1c03f JL |
6645 | status = nfserr_jukebox; |
6646 | goto out; | |
64a284d0 | 6647 | } |
dd257933 | 6648 | |
5db1c03f | 6649 | status = nfs_ok; |
dd257933 | 6650 | *plst = lst; |
5db1c03f JL |
6651 | out: |
6652 | nfs4_put_stateowner(&lo->lo_owner); | |
6653 | return status; | |
64a284d0 BF |
6654 | } |
6655 | ||
1da177e4 LT |
6656 | /* |
6657 | * LOCK operation | |
6658 | */ | |
b37ad28b | 6659 | __be32 |
ca364317 | 6660 | nfsd4_lock(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, |
eb69853d | 6661 | union nfsd4_op_u *u) |
1da177e4 | 6662 | { |
eb69853d | 6663 | struct nfsd4_lock *lock = &u->lock; |
fe0750e5 BF |
6664 | struct nfs4_openowner *open_sop = NULL; |
6665 | struct nfs4_lockowner *lock_sop = NULL; | |
3d0fabd5 | 6666 | struct nfs4_ol_stateid *lock_stp = NULL; |
0667b1e9 | 6667 | struct nfs4_ol_stateid *open_stp = NULL; |
7214e860 | 6668 | struct nfs4_file *fp; |
eb82dd39 | 6669 | struct nfsd_file *nf = NULL; |
76d348fa | 6670 | struct nfsd4_blocked_lock *nbl = NULL; |
21179d81 JL |
6671 | struct file_lock *file_lock = NULL; |
6672 | struct file_lock *conflock = NULL; | |
b37ad28b | 6673 | __be32 status = 0; |
b34f27aa | 6674 | int lkflg; |
b8dd7b9a | 6675 | int err; |
5db1c03f | 6676 | bool new = false; |
76d348fa JL |
6677 | unsigned char fl_type; |
6678 | unsigned int fl_flags = FL_POSIX; | |
3320fef1 SK |
6679 | struct net *net = SVC_NET(rqstp); |
6680 | struct nfsd_net *nn = net_generic(net, nfsd_net_id); | |
1da177e4 LT |
6681 | |
6682 | dprintk("NFSD: nfsd4_lock: start=%Ld length=%Ld\n", | |
6683 | (long long) lock->lk_offset, | |
6684 | (long long) lock->lk_length); | |
6685 | ||
1da177e4 LT |
6686 | if (check_lock_length(lock->lk_offset, lock->lk_length)) |
6687 | return nfserr_inval; | |
6688 | ||
ca364317 | 6689 | if ((status = fh_verify(rqstp, &cstate->current_fh, |
8837abca | 6690 | S_IFREG, NFSD_MAY_LOCK))) { |
a6f6ef2f AA |
6691 | dprintk("NFSD: nfsd4_lock: permission denied!\n"); |
6692 | return status; | |
6693 | } | |
6694 | ||
1da177e4 | 6695 | if (lock->lk_is_new) { |
684e5638 BF |
6696 | if (nfsd4_has_session(cstate)) |
6697 | /* See rfc 5661 18.10.3: given clientid is ignored: */ | |
76f6c9e1 | 6698 | memcpy(&lock->lk_new_clientid, |
684e5638 BF |
6699 | &cstate->session->se_client->cl_clientid, |
6700 | sizeof(clientid_t)); | |
6701 | ||
1da177e4 | 6702 | status = nfserr_stale_clientid; |
2c142baa | 6703 | if (STALE_CLIENTID(&lock->lk_new_clientid, nn)) |
1da177e4 | 6704 | goto out; |
1da177e4 | 6705 | |
1da177e4 | 6706 | /* validate and update open stateid and open seqid */ |
c0a5d93e | 6707 | status = nfs4_preprocess_confirmed_seqid_op(cstate, |
1da177e4 LT |
6708 | lock->lk_new_open_seqid, |
6709 | &lock->lk_new_open_stateid, | |
3320fef1 | 6710 | &open_stp, nn); |
37515177 | 6711 | if (status) |
1da177e4 | 6712 | goto out; |
feb9dad5 | 6713 | mutex_unlock(&open_stp->st_mutex); |
fe0750e5 | 6714 | open_sop = openowner(open_stp->st_stateowner); |
b34f27aa | 6715 | status = nfserr_bad_stateid; |
684e5638 | 6716 | if (!same_clid(&open_sop->oo_owner.so_client->cl_clientid, |
76f6c9e1 | 6717 | &lock->lk_new_clientid)) |
b34f27aa | 6718 | goto out; |
64a284d0 | 6719 | status = lookup_or_create_lock_state(cstate, open_stp, lock, |
5db1c03f | 6720 | &lock_stp, &new); |
3d0fabd5 | 6721 | } else { |
dd453dfd | 6722 | status = nfs4_preprocess_seqid_op(cstate, |
fe0750e5 BF |
6723 | lock->lk_old_lock_seqid, |
6724 | &lock->lk_old_lock_stateid, | |
3320fef1 | 6725 | NFS4_LOCK_STID, &lock_stp, nn); |
3d0fabd5 | 6726 | } |
e1aaa891 BF |
6727 | if (status) |
6728 | goto out; | |
64a284d0 | 6729 | lock_sop = lockowner(lock_stp->st_stateowner); |
1da177e4 | 6730 | |
b34f27aa BF |
6731 | lkflg = setlkflg(lock->lk_type); |
6732 | status = nfs4_check_openmode(lock_stp, lkflg); | |
6733 | if (status) | |
6734 | goto out; | |
6735 | ||
0dd395dc | 6736 | status = nfserr_grace; |
3320fef1 | 6737 | if (locks_in_grace(net) && !lock->lk_reclaim) |
0dd395dc N |
6738 | goto out; |
6739 | status = nfserr_no_grace; | |
3320fef1 | 6740 | if (!locks_in_grace(net) && lock->lk_reclaim) |
0dd395dc N |
6741 | goto out; |
6742 | ||
11b9164a | 6743 | fp = lock_stp->st_stid.sc_file; |
1da177e4 | 6744 | switch (lock->lk_type) { |
1da177e4 | 6745 | case NFS4_READW_LT: |
76d348fa JL |
6746 | if (nfsd4_has_session(cstate)) |
6747 | fl_flags |= FL_SLEEP; | |
df561f66 | 6748 | fallthrough; |
76d348fa | 6749 | case NFS4_READ_LT: |
7214e860 | 6750 | spin_lock(&fp->fi_lock); |
eb82dd39 JL |
6751 | nf = find_readable_file_locked(fp); |
6752 | if (nf) | |
0997b173 | 6753 | get_lock_access(lock_stp, NFS4_SHARE_ACCESS_READ); |
7214e860 | 6754 | spin_unlock(&fp->fi_lock); |
76d348fa | 6755 | fl_type = F_RDLCK; |
529d7b2a | 6756 | break; |
1da177e4 | 6757 | case NFS4_WRITEW_LT: |
76d348fa JL |
6758 | if (nfsd4_has_session(cstate)) |
6759 | fl_flags |= FL_SLEEP; | |
df561f66 | 6760 | fallthrough; |
76d348fa | 6761 | case NFS4_WRITE_LT: |
7214e860 | 6762 | spin_lock(&fp->fi_lock); |
eb82dd39 JL |
6763 | nf = find_writeable_file_locked(fp); |
6764 | if (nf) | |
0997b173 | 6765 | get_lock_access(lock_stp, NFS4_SHARE_ACCESS_WRITE); |
7214e860 | 6766 | spin_unlock(&fp->fi_lock); |
76d348fa | 6767 | fl_type = F_WRLCK; |
529d7b2a | 6768 | break; |
1da177e4 LT |
6769 | default: |
6770 | status = nfserr_inval; | |
6771 | goto out; | |
6772 | } | |
76d348fa | 6773 | |
eb82dd39 | 6774 | if (!nf) { |
f9d7562f BF |
6775 | status = nfserr_openmode; |
6776 | goto out; | |
6777 | } | |
aef9583b | 6778 | |
76d348fa JL |
6779 | nbl = find_or_allocate_block(lock_sop, &fp->fi_fhandle, nn); |
6780 | if (!nbl) { | |
6781 | dprintk("NFSD: %s: unable to allocate block!\n", __func__); | |
6782 | status = nfserr_jukebox; | |
6783 | goto out; | |
6784 | } | |
6785 | ||
6786 | file_lock = &nbl->nbl_lock; | |
6787 | file_lock->fl_type = fl_type; | |
aef9583b | 6788 | file_lock->fl_owner = (fl_owner_t)lockowner(nfs4_get_stateowner(&lock_sop->lo_owner)); |
21179d81 | 6789 | file_lock->fl_pid = current->tgid; |
eb82dd39 | 6790 | file_lock->fl_file = nf->nf_file; |
76d348fa | 6791 | file_lock->fl_flags = fl_flags; |
21179d81 JL |
6792 | file_lock->fl_lmops = &nfsd_posix_mng_ops; |
6793 | file_lock->fl_start = lock->lk_offset; | |
6794 | file_lock->fl_end = last_byte_offset(lock->lk_offset, lock->lk_length); | |
6795 | nfs4_transform_lock_offset(file_lock); | |
6796 | ||
6797 | conflock = locks_alloc_lock(); | |
6798 | if (!conflock) { | |
6799 | dprintk("NFSD: %s: unable to allocate lock!\n", __func__); | |
6800 | status = nfserr_jukebox; | |
6801 | goto out; | |
6802 | } | |
1da177e4 | 6803 | |
76d348fa | 6804 | if (fl_flags & FL_SLEEP) { |
20b7d86f | 6805 | nbl->nbl_time = ktime_get_boottime_seconds(); |
0cc11a61 | 6806 | spin_lock(&nn->blocked_locks_lock); |
76d348fa | 6807 | list_add_tail(&nbl->nbl_list, &lock_sop->lo_blocked); |
7919d0a2 | 6808 | list_add_tail(&nbl->nbl_lru, &nn->blocked_locks_lru); |
0cc11a61 | 6809 | spin_unlock(&nn->blocked_locks_lock); |
76d348fa JL |
6810 | } |
6811 | ||
eb82dd39 | 6812 | err = vfs_lock_file(nf->nf_file, F_SETLK, file_lock, conflock); |
76d348fa | 6813 | switch (err) { |
1da177e4 | 6814 | case 0: /* success! */ |
9767feb2 | 6815 | nfs4_inc_and_copy_stateid(&lock->lk_resp_stateid, &lock_stp->st_stid); |
b8dd7b9a | 6816 | status = 0; |
03f318ca BF |
6817 | if (lock->lk_reclaim) |
6818 | nn->somebody_reclaimed = true; | |
eb76b3fd | 6819 | break; |
76d348fa JL |
6820 | case FILE_LOCK_DEFERRED: |
6821 | nbl = NULL; | |
df561f66 | 6822 | fallthrough; |
76d348fa | 6823 | case -EAGAIN: /* conflock holds conflicting lock */ |
eb76b3fd AA |
6824 | status = nfserr_denied; |
6825 | dprintk("NFSD: nfsd4_lock: conflicting lock found!\n"); | |
21179d81 | 6826 | nfs4_set_lock_denied(conflock, &lock->lk_denied); |
eb76b3fd | 6827 | break; |
76d348fa | 6828 | case -EDEADLK: |
1da177e4 | 6829 | status = nfserr_deadlock; |
eb76b3fd | 6830 | break; |
3e772463 | 6831 | default: |
fd85b817 | 6832 | dprintk("NFSD: nfsd4_lock: vfs_lock_file() failed! status %d\n",err); |
3e772463 | 6833 | status = nfserrno(err); |
eb76b3fd | 6834 | break; |
1da177e4 | 6835 | } |
1da177e4 | 6836 | out: |
76d348fa JL |
6837 | if (nbl) { |
6838 | /* dequeue it if we queued it before */ | |
6839 | if (fl_flags & FL_SLEEP) { | |
0cc11a61 | 6840 | spin_lock(&nn->blocked_locks_lock); |
76d348fa | 6841 | list_del_init(&nbl->nbl_list); |
7919d0a2 | 6842 | list_del_init(&nbl->nbl_lru); |
0cc11a61 | 6843 | spin_unlock(&nn->blocked_locks_lock); |
76d348fa JL |
6844 | } |
6845 | free_blocked_lock(nbl); | |
6846 | } | |
eb82dd39 JL |
6847 | if (nf) |
6848 | nfsd_file_put(nf); | |
5db1c03f JL |
6849 | if (lock_stp) { |
6850 | /* Bump seqid manually if the 4.0 replay owner is openowner */ | |
6851 | if (cstate->replay_owner && | |
6852 | cstate->replay_owner != &lock_sop->lo_owner && | |
6853 | seqid_mutating_err(ntohl(status))) | |
6854 | lock_sop->lo_owner.so_seqid++; | |
6855 | ||
6856 | /* | |
6857 | * If this is a new, never-before-used stateid, and we are | |
6858 | * returning an error, then just go ahead and release it. | |
6859 | */ | |
25020720 | 6860 | if (status && new) |
5db1c03f | 6861 | release_lock_stateid(lock_stp); |
beeca19c TM |
6862 | |
6863 | mutex_unlock(&lock_stp->st_mutex); | |
5db1c03f | 6864 | |
3d0fabd5 | 6865 | nfs4_put_stid(&lock_stp->st_stid); |
5db1c03f | 6866 | } |
0667b1e9 TM |
6867 | if (open_stp) |
6868 | nfs4_put_stid(&open_stp->st_stid); | |
9411b1d4 | 6869 | nfsd4_bump_seqid(cstate, status); |
21179d81 JL |
6870 | if (conflock) |
6871 | locks_free_lock(conflock); | |
1da177e4 LT |
6872 | return status; |
6873 | } | |
6874 | ||
55ef1274 BF |
6875 | /* |
6876 | * The NFSv4 spec allows a client to do a LOCKT without holding an OPEN, | |
6877 | * so we do a temporary open here just to get an open file to pass to | |
6878 | * vfs_test_lock. (Arguably perhaps test_lock should be done with an | |
6879 | * inode operation.) | |
6880 | */ | |
04da6e9d | 6881 | static __be32 nfsd_test_lock(struct svc_rqst *rqstp, struct svc_fh *fhp, struct file_lock *lock) |
55ef1274 | 6882 | { |
6b556ca2 JL |
6883 | struct nfsd_file *nf; |
6884 | __be32 err = nfsd_file_acquire(rqstp, fhp, NFSD_MAY_READ, &nf); | |
04da6e9d | 6885 | if (!err) { |
6b556ca2 JL |
6886 | err = nfserrno(vfs_test_lock(nf->nf_file, lock)); |
6887 | nfsd_file_put(nf); | |
04da6e9d | 6888 | } |
55ef1274 BF |
6889 | return err; |
6890 | } | |
6891 | ||
1da177e4 LT |
6892 | /* |
6893 | * LOCKT operation | |
6894 | */ | |
b37ad28b | 6895 | __be32 |
ca364317 | 6896 | nfsd4_lockt(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, |
eb69853d | 6897 | union nfsd4_op_u *u) |
1da177e4 | 6898 | { |
eb69853d | 6899 | struct nfsd4_lockt *lockt = &u->lockt; |
21179d81 | 6900 | struct file_lock *file_lock = NULL; |
5db1c03f | 6901 | struct nfs4_lockowner *lo = NULL; |
b37ad28b | 6902 | __be32 status; |
7f2210fa | 6903 | struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id); |
1da177e4 | 6904 | |
5ccb0066 | 6905 | if (locks_in_grace(SVC_NET(rqstp))) |
1da177e4 LT |
6906 | return nfserr_grace; |
6907 | ||
6908 | if (check_lock_length(lockt->lt_offset, lockt->lt_length)) | |
6909 | return nfserr_inval; | |
6910 | ||
9b2ef62b | 6911 | if (!nfsd4_has_session(cstate)) { |
b7342204 OK |
6912 | status = lookup_clientid(&lockt->lt_clientid, cstate, nn, |
6913 | false); | |
9b2ef62b BF |
6914 | if (status) |
6915 | goto out; | |
6916 | } | |
1da177e4 | 6917 | |
75c096f7 | 6918 | if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0))) |
1da177e4 | 6919 | goto out; |
1da177e4 | 6920 | |
21179d81 JL |
6921 | file_lock = locks_alloc_lock(); |
6922 | if (!file_lock) { | |
6923 | dprintk("NFSD: %s: unable to allocate lock!\n", __func__); | |
6924 | status = nfserr_jukebox; | |
6925 | goto out; | |
6926 | } | |
6cd90662 | 6927 | |
1da177e4 LT |
6928 | switch (lockt->lt_type) { |
6929 | case NFS4_READ_LT: | |
6930 | case NFS4_READW_LT: | |
21179d81 | 6931 | file_lock->fl_type = F_RDLCK; |
f50c9d79 | 6932 | break; |
1da177e4 LT |
6933 | case NFS4_WRITE_LT: |
6934 | case NFS4_WRITEW_LT: | |
21179d81 | 6935 | file_lock->fl_type = F_WRLCK; |
f50c9d79 | 6936 | break; |
1da177e4 | 6937 | default: |
2fdada03 | 6938 | dprintk("NFSD: nfs4_lockt: bad lock type!\n"); |
1da177e4 | 6939 | status = nfserr_inval; |
f50c9d79 | 6940 | goto out; |
1da177e4 LT |
6941 | } |
6942 | ||
c8623999 | 6943 | lo = find_lockowner_str(cstate->clp, &lockt->lt_owner); |
fe0750e5 | 6944 | if (lo) |
21179d81 JL |
6945 | file_lock->fl_owner = (fl_owner_t)lo; |
6946 | file_lock->fl_pid = current->tgid; | |
6947 | file_lock->fl_flags = FL_POSIX; | |
1da177e4 | 6948 | |
21179d81 JL |
6949 | file_lock->fl_start = lockt->lt_offset; |
6950 | file_lock->fl_end = last_byte_offset(lockt->lt_offset, lockt->lt_length); | |
1da177e4 | 6951 | |
21179d81 | 6952 | nfs4_transform_lock_offset(file_lock); |
1da177e4 | 6953 | |
21179d81 | 6954 | status = nfsd_test_lock(rqstp, &cstate->current_fh, file_lock); |
04da6e9d | 6955 | if (status) |
fd85b817 | 6956 | goto out; |
04da6e9d | 6957 | |
21179d81 | 6958 | if (file_lock->fl_type != F_UNLCK) { |
1da177e4 | 6959 | status = nfserr_denied; |
21179d81 | 6960 | nfs4_set_lock_denied(file_lock, &lockt->lt_denied); |
1da177e4 LT |
6961 | } |
6962 | out: | |
5db1c03f JL |
6963 | if (lo) |
6964 | nfs4_put_stateowner(&lo->lo_owner); | |
21179d81 JL |
6965 | if (file_lock) |
6966 | locks_free_lock(file_lock); | |
1da177e4 LT |
6967 | return status; |
6968 | } | |
6969 | ||
b37ad28b | 6970 | __be32 |
ca364317 | 6971 | nfsd4_locku(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, |
eb69853d | 6972 | union nfsd4_op_u *u) |
1da177e4 | 6973 | { |
eb69853d | 6974 | struct nfsd4_locku *locku = &u->locku; |
dcef0413 | 6975 | struct nfs4_ol_stateid *stp; |
eb82dd39 | 6976 | struct nfsd_file *nf = NULL; |
21179d81 | 6977 | struct file_lock *file_lock = NULL; |
b37ad28b | 6978 | __be32 status; |
b8dd7b9a | 6979 | int err; |
3320fef1 SK |
6980 | struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id); |
6981 | ||
1da177e4 LT |
6982 | dprintk("NFSD: nfsd4_locku: start=%Ld length=%Ld\n", |
6983 | (long long) locku->lu_offset, | |
6984 | (long long) locku->lu_length); | |
6985 | ||
6986 | if (check_lock_length(locku->lu_offset, locku->lu_length)) | |
6987 | return nfserr_inval; | |
6988 | ||
9072d5c6 | 6989 | status = nfs4_preprocess_seqid_op(cstate, locku->lu_seqid, |
3320fef1 SK |
6990 | &locku->lu_stateid, NFS4_LOCK_STID, |
6991 | &stp, nn); | |
9072d5c6 | 6992 | if (status) |
1da177e4 | 6993 | goto out; |
eb82dd39 JL |
6994 | nf = find_any_file(stp->st_stid.sc_file); |
6995 | if (!nf) { | |
f9d7562f | 6996 | status = nfserr_lock_range; |
858cc573 | 6997 | goto put_stateid; |
f9d7562f | 6998 | } |
21179d81 JL |
6999 | file_lock = locks_alloc_lock(); |
7000 | if (!file_lock) { | |
7001 | dprintk("NFSD: %s: unable to allocate lock!\n", __func__); | |
7002 | status = nfserr_jukebox; | |
eb82dd39 | 7003 | goto put_file; |
21179d81 | 7004 | } |
6cd90662 | 7005 | |
21179d81 | 7006 | file_lock->fl_type = F_UNLCK; |
aef9583b | 7007 | file_lock->fl_owner = (fl_owner_t)lockowner(nfs4_get_stateowner(stp->st_stateowner)); |
21179d81 | 7008 | file_lock->fl_pid = current->tgid; |
eb82dd39 | 7009 | file_lock->fl_file = nf->nf_file; |
21179d81 JL |
7010 | file_lock->fl_flags = FL_POSIX; |
7011 | file_lock->fl_lmops = &nfsd_posix_mng_ops; | |
7012 | file_lock->fl_start = locku->lu_offset; | |
7013 | ||
7014 | file_lock->fl_end = last_byte_offset(locku->lu_offset, | |
7015 | locku->lu_length); | |
7016 | nfs4_transform_lock_offset(file_lock); | |
1da177e4 | 7017 | |
eb82dd39 | 7018 | err = vfs_lock_file(nf->nf_file, F_SETLK, file_lock, NULL); |
b8dd7b9a | 7019 | if (err) { |
fd85b817 | 7020 | dprintk("NFSD: nfs4_locku: vfs_lock_file failed!\n"); |
1da177e4 LT |
7021 | goto out_nfserr; |
7022 | } | |
9767feb2 | 7023 | nfs4_inc_and_copy_stateid(&locku->lu_stateid, &stp->st_stid); |
eb82dd39 JL |
7024 | put_file: |
7025 | nfsd_file_put(nf); | |
858cc573 | 7026 | put_stateid: |
feb9dad5 | 7027 | mutex_unlock(&stp->st_mutex); |
858cc573 | 7028 | nfs4_put_stid(&stp->st_stid); |
1da177e4 | 7029 | out: |
9411b1d4 | 7030 | nfsd4_bump_seqid(cstate, status); |
21179d81 JL |
7031 | if (file_lock) |
7032 | locks_free_lock(file_lock); | |
1da177e4 LT |
7033 | return status; |
7034 | ||
7035 | out_nfserr: | |
b8dd7b9a | 7036 | status = nfserrno(err); |
eb82dd39 | 7037 | goto put_file; |
1da177e4 LT |
7038 | } |
7039 | ||
7040 | /* | |
7041 | * returns | |
f9c00c3a JL |
7042 | * true: locks held by lockowner |
7043 | * false: no locks held by lockowner | |
1da177e4 | 7044 | */ |
f9c00c3a JL |
7045 | static bool |
7046 | check_for_locks(struct nfs4_file *fp, struct nfs4_lockowner *lowner) | |
1da177e4 | 7047 | { |
bd61e0a9 | 7048 | struct file_lock *fl; |
f9c00c3a | 7049 | int status = false; |
eb82dd39 | 7050 | struct nfsd_file *nf = find_any_file(fp); |
f9c00c3a | 7051 | struct inode *inode; |
bd61e0a9 | 7052 | struct file_lock_context *flctx; |
f9c00c3a | 7053 | |
eb82dd39 | 7054 | if (!nf) { |
f9c00c3a JL |
7055 | /* Any valid lock stateid should have some sort of access */ |
7056 | WARN_ON_ONCE(1); | |
7057 | return status; | |
7058 | } | |
7059 | ||
eb82dd39 | 7060 | inode = locks_inode(nf->nf_file); |
bd61e0a9 JL |
7061 | flctx = inode->i_flctx; |
7062 | ||
7063 | if (flctx && !list_empty_careful(&flctx->flc_posix)) { | |
6109c850 | 7064 | spin_lock(&flctx->flc_lock); |
bd61e0a9 JL |
7065 | list_for_each_entry(fl, &flctx->flc_posix, fl_list) { |
7066 | if (fl->fl_owner == (fl_owner_t)lowner) { | |
7067 | status = true; | |
7068 | break; | |
7069 | } | |
796dadfd | 7070 | } |
6109c850 | 7071 | spin_unlock(&flctx->flc_lock); |
1da177e4 | 7072 | } |
eb82dd39 | 7073 | nfsd_file_put(nf); |
1da177e4 LT |
7074 | return status; |
7075 | } | |
7076 | ||
b37ad28b | 7077 | __be32 |
b591480b BF |
7078 | nfsd4_release_lockowner(struct svc_rqst *rqstp, |
7079 | struct nfsd4_compound_state *cstate, | |
eb69853d | 7080 | union nfsd4_op_u *u) |
1da177e4 | 7081 | { |
eb69853d | 7082 | struct nfsd4_release_lockowner *rlockowner = &u->release_lockowner; |
1da177e4 | 7083 | clientid_t *clid = &rlockowner->rl_clientid; |
882e9d25 JL |
7084 | struct nfs4_stateowner *sop; |
7085 | struct nfs4_lockowner *lo = NULL; | |
dcef0413 | 7086 | struct nfs4_ol_stateid *stp; |
1da177e4 | 7087 | struct xdr_netobj *owner = &rlockowner->rl_owner; |
d4f0489f | 7088 | unsigned int hashval = ownerstr_hashval(owner); |
b37ad28b | 7089 | __be32 status; |
7f2210fa | 7090 | struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id); |
c58c6610 | 7091 | struct nfs4_client *clp; |
88584818 | 7092 | LIST_HEAD (reaplist); |
1da177e4 LT |
7093 | |
7094 | dprintk("nfsd4_release_lockowner clientid: (%08x/%08x):\n", | |
7095 | clid->cl_boot, clid->cl_id); | |
7096 | ||
b7342204 | 7097 | status = lookup_clientid(clid, cstate, nn, false); |
9b2ef62b | 7098 | if (status) |
51f5e783 | 7099 | return status; |
9b2ef62b | 7100 | |
d4f0489f | 7101 | clp = cstate->clp; |
fd44907c | 7102 | /* Find the matching lock stateowner */ |
d4f0489f | 7103 | spin_lock(&clp->cl_lock); |
882e9d25 | 7104 | list_for_each_entry(sop, &clp->cl_ownerstr_hashtbl[hashval], |
d4f0489f | 7105 | so_strhash) { |
fd44907c | 7106 | |
882e9d25 JL |
7107 | if (sop->so_is_open_owner || !same_owner_str(sop, owner)) |
7108 | continue; | |
fd44907c | 7109 | |
882e9d25 JL |
7110 | /* see if there are still any locks associated with it */ |
7111 | lo = lockowner(sop); | |
7112 | list_for_each_entry(stp, &sop->so_stateids, st_perstateowner) { | |
7113 | if (check_for_locks(stp->st_stid.sc_file, lo)) { | |
7114 | status = nfserr_locks_held; | |
7115 | spin_unlock(&clp->cl_lock); | |
51f5e783 | 7116 | return status; |
882e9d25 | 7117 | } |
5adfd885 | 7118 | } |
882e9d25 | 7119 | |
b5971afa | 7120 | nfs4_get_stateowner(sop); |
882e9d25 | 7121 | break; |
1da177e4 | 7122 | } |
88584818 CL |
7123 | if (!lo) { |
7124 | spin_unlock(&clp->cl_lock); | |
7125 | return status; | |
7126 | } | |
7127 | ||
7128 | unhash_lockowner_locked(lo); | |
7129 | while (!list_empty(&lo->lo_owner.so_stateids)) { | |
7130 | stp = list_first_entry(&lo->lo_owner.so_stateids, | |
7131 | struct nfs4_ol_stateid, | |
7132 | st_perstateowner); | |
7133 | WARN_ON(!unhash_lock_stateid(stp)); | |
7134 | put_ol_stateid_locked(stp, &reaplist); | |
7135 | } | |
c58c6610 | 7136 | spin_unlock(&clp->cl_lock); |
88584818 | 7137 | free_ol_stateid_reaplist(&reaplist); |
68ef3bc3 | 7138 | remove_blocked_locks(lo); |
88584818 CL |
7139 | nfs4_put_stateowner(&lo->lo_owner); |
7140 | ||
1da177e4 LT |
7141 | return status; |
7142 | } | |
7143 | ||
7144 | static inline struct nfs4_client_reclaim * | |
a55370a3 | 7145 | alloc_reclaim(void) |
1da177e4 | 7146 | { |
a55370a3 | 7147 | return kmalloc(sizeof(struct nfs4_client_reclaim), GFP_KERNEL); |
1da177e4 LT |
7148 | } |
7149 | ||
0ce0c2b5 | 7150 | bool |
6b189105 | 7151 | nfs4_has_reclaimed_state(struct xdr_netobj name, struct nfsd_net *nn) |
c7b9a459 | 7152 | { |
0ce0c2b5 | 7153 | struct nfs4_client_reclaim *crp; |
c7b9a459 | 7154 | |
52e19c09 | 7155 | crp = nfsd4_find_reclaim_client(name, nn); |
0ce0c2b5 | 7156 | return (crp && crp->cr_clp); |
c7b9a459 N |
7157 | } |
7158 | ||
1da177e4 LT |
7159 | /* |
7160 | * failure => all reset bets are off, nfserr_no_grace... | |
6b189105 SM |
7161 | * |
7162 | * The caller is responsible for freeing name.data if NULL is returned (it | |
7163 | * will be freed in nfs4_remove_reclaim_record in the normal case). | |
1da177e4 | 7164 | */ |
772a9bbb | 7165 | struct nfs4_client_reclaim * |
6ee95d1c SM |
7166 | nfs4_client_to_reclaim(struct xdr_netobj name, struct xdr_netobj princhash, |
7167 | struct nfsd_net *nn) | |
1da177e4 LT |
7168 | { |
7169 | unsigned int strhashval; | |
772a9bbb | 7170 | struct nfs4_client_reclaim *crp; |
1da177e4 | 7171 | |
dd5e3fbc | 7172 | trace_nfsd_clid_reclaim(nn, name.len, name.data); |
a55370a3 | 7173 | crp = alloc_reclaim(); |
772a9bbb JL |
7174 | if (crp) { |
7175 | strhashval = clientstr_hashval(name); | |
7176 | INIT_LIST_HEAD(&crp->cr_strhash); | |
52e19c09 | 7177 | list_add(&crp->cr_strhash, &nn->reclaim_str_hashtbl[strhashval]); |
6b189105 SM |
7178 | crp->cr_name.data = name.data; |
7179 | crp->cr_name.len = name.len; | |
6ee95d1c SM |
7180 | crp->cr_princhash.data = princhash.data; |
7181 | crp->cr_princhash.len = princhash.len; | |
0ce0c2b5 | 7182 | crp->cr_clp = NULL; |
52e19c09 | 7183 | nn->reclaim_str_hashtbl_size++; |
772a9bbb JL |
7184 | } |
7185 | return crp; | |
1da177e4 LT |
7186 | } |
7187 | ||
ce30e539 | 7188 | void |
52e19c09 | 7189 | nfs4_remove_reclaim_record(struct nfs4_client_reclaim *crp, struct nfsd_net *nn) |
ce30e539 JL |
7190 | { |
7191 | list_del(&crp->cr_strhash); | |
6b189105 | 7192 | kfree(crp->cr_name.data); |
6ee95d1c | 7193 | kfree(crp->cr_princhash.data); |
ce30e539 | 7194 | kfree(crp); |
52e19c09 | 7195 | nn->reclaim_str_hashtbl_size--; |
ce30e539 JL |
7196 | } |
7197 | ||
2a4317c5 | 7198 | void |
52e19c09 | 7199 | nfs4_release_reclaim(struct nfsd_net *nn) |
1da177e4 LT |
7200 | { |
7201 | struct nfs4_client_reclaim *crp = NULL; | |
7202 | int i; | |
7203 | ||
1da177e4 | 7204 | for (i = 0; i < CLIENT_HASH_SIZE; i++) { |
52e19c09 SK |
7205 | while (!list_empty(&nn->reclaim_str_hashtbl[i])) { |
7206 | crp = list_entry(nn->reclaim_str_hashtbl[i].next, | |
1da177e4 | 7207 | struct nfs4_client_reclaim, cr_strhash); |
52e19c09 | 7208 | nfs4_remove_reclaim_record(crp, nn); |
1da177e4 LT |
7209 | } |
7210 | } | |
063b0fb9 | 7211 | WARN_ON_ONCE(nn->reclaim_str_hashtbl_size); |
1da177e4 LT |
7212 | } |
7213 | ||
7214 | /* | |
7215 | * called from OPEN, CLAIM_PREVIOUS with a new clientid. */ | |
2a4317c5 | 7216 | struct nfs4_client_reclaim * |
6b189105 | 7217 | nfsd4_find_reclaim_client(struct xdr_netobj name, struct nfsd_net *nn) |
1da177e4 LT |
7218 | { |
7219 | unsigned int strhashval; | |
1da177e4 LT |
7220 | struct nfs4_client_reclaim *crp = NULL; |
7221 | ||
dd5e3fbc | 7222 | trace_nfsd_clid_find(nn, name.len, name.data); |
1da177e4 | 7223 | |
6b189105 | 7224 | strhashval = clientstr_hashval(name); |
52e19c09 | 7225 | list_for_each_entry(crp, &nn->reclaim_str_hashtbl[strhashval], cr_strhash) { |
6b189105 | 7226 | if (compare_blob(&crp->cr_name, &name) == 0) { |
1da177e4 LT |
7227 | return crp; |
7228 | } | |
7229 | } | |
7230 | return NULL; | |
7231 | } | |
7232 | ||
7233 | /* | |
7234 | * Called from OPEN. Look for clientid in reclaim list. | |
7235 | */ | |
b37ad28b | 7236 | __be32 |
0fe492db TM |
7237 | nfs4_check_open_reclaim(clientid_t *clid, |
7238 | struct nfsd4_compound_state *cstate, | |
7239 | struct nfsd_net *nn) | |
1da177e4 | 7240 | { |
0fe492db | 7241 | __be32 status; |
a52d726b JL |
7242 | |
7243 | /* find clientid in conf_id_hashtbl */ | |
b7342204 | 7244 | status = lookup_clientid(clid, cstate, nn, false); |
0fe492db | 7245 | if (status) |
a52d726b JL |
7246 | return nfserr_reclaim_bad; |
7247 | ||
3b3e7b72 JL |
7248 | if (test_bit(NFSD4_CLIENT_RECLAIM_COMPLETE, &cstate->clp->cl_flags)) |
7249 | return nfserr_no_grace; | |
7250 | ||
0fe492db TM |
7251 | if (nfsd4_client_record_check(cstate->clp)) |
7252 | return nfserr_reclaim_bad; | |
7253 | ||
7254 | return nfs_ok; | |
1da177e4 LT |
7255 | } |
7256 | ||
c2f1a551 MS |
7257 | /* |
7258 | * Since the lifetime of a delegation isn't limited to that of an open, a | |
7259 | * client may quite reasonably hang on to a delegation as long as it has | |
7260 | * the inode cached. This becomes an obvious problem the first time a | |
7261 | * client's inode cache approaches the size of the server's total memory. | |
7262 | * | |
7263 | * For now we avoid this problem by imposing a hard limit on the number | |
7264 | * of delegations, which varies according to the server's memory size. | |
7265 | */ | |
7266 | static void | |
7267 | set_max_delegations(void) | |
7268 | { | |
7269 | /* | |
7270 | * Allow at most 4 delegations per megabyte of RAM. Quick | |
7271 | * estimates suggest that in the worst case (where every delegation | |
7272 | * is for a different inode), a delegation could take about 1.5K, | |
7273 | * giving a worst case usage of about 6% of memory. | |
7274 | */ | |
7275 | max_delegations = nr_free_buffer_pages() >> (20 - 2 - PAGE_SHIFT); | |
7276 | } | |
7277 | ||
d85ed443 | 7278 | static int nfs4_state_create_net(struct net *net) |
8daae4dc SK |
7279 | { |
7280 | struct nfsd_net *nn = net_generic(net, nfsd_net_id); | |
7281 | int i; | |
7282 | ||
6da2ec56 KC |
7283 | nn->conf_id_hashtbl = kmalloc_array(CLIENT_HASH_SIZE, |
7284 | sizeof(struct list_head), | |
7285 | GFP_KERNEL); | |
8daae4dc | 7286 | if (!nn->conf_id_hashtbl) |
382a62e7 | 7287 | goto err; |
6da2ec56 KC |
7288 | nn->unconf_id_hashtbl = kmalloc_array(CLIENT_HASH_SIZE, |
7289 | sizeof(struct list_head), | |
7290 | GFP_KERNEL); | |
0a7ec377 SK |
7291 | if (!nn->unconf_id_hashtbl) |
7292 | goto err_unconf_id; | |
6da2ec56 KC |
7293 | nn->sessionid_hashtbl = kmalloc_array(SESSION_HASH_SIZE, |
7294 | sizeof(struct list_head), | |
7295 | GFP_KERNEL); | |
1872de0e SK |
7296 | if (!nn->sessionid_hashtbl) |
7297 | goto err_sessionid; | |
8daae4dc | 7298 | |
382a62e7 | 7299 | for (i = 0; i < CLIENT_HASH_SIZE; i++) { |
8daae4dc | 7300 | INIT_LIST_HEAD(&nn->conf_id_hashtbl[i]); |
0a7ec377 | 7301 | INIT_LIST_HEAD(&nn->unconf_id_hashtbl[i]); |
382a62e7 | 7302 | } |
1872de0e SK |
7303 | for (i = 0; i < SESSION_HASH_SIZE; i++) |
7304 | INIT_LIST_HEAD(&nn->sessionid_hashtbl[i]); | |
382a62e7 | 7305 | nn->conf_name_tree = RB_ROOT; |
a99454aa | 7306 | nn->unconf_name_tree = RB_ROOT; |
9cc76801 | 7307 | nn->boot_time = ktime_get_real_seconds(); |
81833de1 VA |
7308 | nn->grace_ended = false; |
7309 | nn->nfsd4_manager.block_opens = true; | |
7310 | INIT_LIST_HEAD(&nn->nfsd4_manager.list); | |
5ed58bb2 | 7311 | INIT_LIST_HEAD(&nn->client_lru); |
73758fed | 7312 | INIT_LIST_HEAD(&nn->close_lru); |
e8c69d17 | 7313 | INIT_LIST_HEAD(&nn->del_recall_lru); |
c9a49628 | 7314 | spin_lock_init(&nn->client_lock); |
e0639dc5 OK |
7315 | spin_lock_init(&nn->s2s_cp_lock); |
7316 | idr_init(&nn->s2s_cp_stateids); | |
8daae4dc | 7317 | |
0cc11a61 JL |
7318 | spin_lock_init(&nn->blocked_locks_lock); |
7319 | INIT_LIST_HEAD(&nn->blocked_locks_lru); | |
7320 | ||
09121281 | 7321 | INIT_DELAYED_WORK(&nn->laundromat_work, laundromat_main); |
d85ed443 | 7322 | get_net(net); |
09121281 | 7323 | |
8daae4dc | 7324 | return 0; |
382a62e7 | 7325 | |
1872de0e | 7326 | err_sessionid: |
9b531137 | 7327 | kfree(nn->unconf_id_hashtbl); |
0a7ec377 SK |
7328 | err_unconf_id: |
7329 | kfree(nn->conf_id_hashtbl); | |
382a62e7 SK |
7330 | err: |
7331 | return -ENOMEM; | |
8daae4dc SK |
7332 | } |
7333 | ||
7334 | static void | |
4dce0ac9 | 7335 | nfs4_state_destroy_net(struct net *net) |
8daae4dc SK |
7336 | { |
7337 | int i; | |
7338 | struct nfs4_client *clp = NULL; | |
7339 | struct nfsd_net *nn = net_generic(net, nfsd_net_id); | |
7340 | ||
7341 | for (i = 0; i < CLIENT_HASH_SIZE; i++) { | |
7342 | while (!list_empty(&nn->conf_id_hashtbl[i])) { | |
7343 | clp = list_entry(nn->conf_id_hashtbl[i].next, struct nfs4_client, cl_idhash); | |
7344 | destroy_client(clp); | |
7345 | } | |
7346 | } | |
a99454aa | 7347 | |
68ef3bc3 JL |
7348 | WARN_ON(!list_empty(&nn->blocked_locks_lru)); |
7349 | ||
2b905635 KM |
7350 | for (i = 0; i < CLIENT_HASH_SIZE; i++) { |
7351 | while (!list_empty(&nn->unconf_id_hashtbl[i])) { | |
7352 | clp = list_entry(nn->unconf_id_hashtbl[i].next, struct nfs4_client, cl_idhash); | |
7353 | destroy_client(clp); | |
7354 | } | |
a99454aa SK |
7355 | } |
7356 | ||
1872de0e | 7357 | kfree(nn->sessionid_hashtbl); |
0a7ec377 | 7358 | kfree(nn->unconf_id_hashtbl); |
8daae4dc | 7359 | kfree(nn->conf_id_hashtbl); |
4dce0ac9 | 7360 | put_net(net); |
8daae4dc SK |
7361 | } |
7362 | ||
f252bc68 | 7363 | int |
d85ed443 | 7364 | nfs4_state_start_net(struct net *net) |
ac4d8ff2 | 7365 | { |
5e1533c7 | 7366 | struct nfsd_net *nn = net_generic(net, nfsd_net_id); |
b5a1a81e BF |
7367 | int ret; |
7368 | ||
681370f4 | 7369 | ret = get_nfsdfs(net); |
8daae4dc SK |
7370 | if (ret) |
7371 | return ret; | |
681370f4 BF |
7372 | ret = nfs4_state_create_net(net); |
7373 | if (ret) { | |
7374 | mntput(nn->nfsd_mnt); | |
7375 | return ret; | |
7376 | } | |
d4318acd JL |
7377 | locks_start_grace(net, &nn->nfsd4_manager); |
7378 | nfsd4_client_tracking_init(net); | |
362063a5 SM |
7379 | if (nn->track_reclaim_completes && nn->reclaim_str_hashtbl_size == 0) |
7380 | goto skip_grace; | |
20b7d86f | 7381 | printk(KERN_INFO "NFSD: starting %lld-second grace period (net %x)\n", |
7e981a8a | 7382 | nn->nfsd4_grace, net->ns.inum); |
dd5e3fbc | 7383 | trace_nfsd_grace_start(nn); |
5284b44e | 7384 | queue_delayed_work(laundry_wq, &nn->laundromat_work, nn->nfsd4_grace * HZ); |
d85ed443 | 7385 | return 0; |
362063a5 SM |
7386 | |
7387 | skip_grace: | |
7388 | printk(KERN_INFO "NFSD: no clients to reclaim, skipping NFSv4 grace period (net %x)\n", | |
7389 | net->ns.inum); | |
7390 | queue_delayed_work(laundry_wq, &nn->laundromat_work, nn->nfsd4_lease * HZ); | |
7391 | nfsd4_end_grace(nn); | |
7392 | return 0; | |
d85ed443 SK |
7393 | } |
7394 | ||
7395 | /* initialization to perform when the nfsd service is started: */ | |
7396 | ||
7397 | int | |
7398 | nfs4_state_start(void) | |
7399 | { | |
7400 | int ret; | |
7401 | ||
51a54568 | 7402 | laundry_wq = alloc_workqueue("%s", WQ_UNBOUND, 0, "nfsd4"); |
a6d6b781 JL |
7403 | if (laundry_wq == NULL) { |
7404 | ret = -ENOMEM; | |
a26dd64f | 7405 | goto out; |
a6d6b781 | 7406 | } |
b5a1a81e BF |
7407 | ret = nfsd4_create_callback_queue(); |
7408 | if (ret) | |
7409 | goto out_free_laundry; | |
09121281 | 7410 | |
c2f1a551 | 7411 | set_max_delegations(); |
b5a1a81e | 7412 | return 0; |
d85ed443 | 7413 | |
b5a1a81e BF |
7414 | out_free_laundry: |
7415 | destroy_workqueue(laundry_wq); | |
a26dd64f | 7416 | out: |
b5a1a81e | 7417 | return ret; |
1da177e4 LT |
7418 | } |
7419 | ||
f252bc68 | 7420 | void |
4dce0ac9 | 7421 | nfs4_state_shutdown_net(struct net *net) |
1da177e4 | 7422 | { |
1da177e4 | 7423 | struct nfs4_delegation *dp = NULL; |
1da177e4 | 7424 | struct list_head *pos, *next, reaplist; |
4dce0ac9 | 7425 | struct nfsd_net *nn = net_generic(net, nfsd_net_id); |
1da177e4 | 7426 | |
4dce0ac9 SK |
7427 | cancel_delayed_work_sync(&nn->laundromat_work); |
7428 | locks_end_grace(&nn->nfsd4_manager); | |
ac55fdc4 | 7429 | |
1da177e4 | 7430 | INIT_LIST_HEAD(&reaplist); |
cdc97505 | 7431 | spin_lock(&state_lock); |
e8c69d17 | 7432 | list_for_each_safe(pos, next, &nn->del_recall_lru) { |
1da177e4 | 7433 | dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru); |
3fcbbd24 | 7434 | WARN_ON(!unhash_delegation_locked(dp)); |
42690676 | 7435 | list_add(&dp->dl_recall_lru, &reaplist); |
1da177e4 | 7436 | } |
cdc97505 | 7437 | spin_unlock(&state_lock); |
1da177e4 LT |
7438 | list_for_each_safe(pos, next, &reaplist) { |
7439 | dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru); | |
42690676 | 7440 | list_del_init(&dp->dl_recall_lru); |
0af6e690 | 7441 | destroy_unhashed_deleg(dp); |
1da177e4 LT |
7442 | } |
7443 | ||
3320fef1 | 7444 | nfsd4_client_tracking_exit(net); |
4dce0ac9 | 7445 | nfs4_state_destroy_net(net); |
681370f4 | 7446 | mntput(nn->nfsd_mnt); |
1da177e4 LT |
7447 | } |
7448 | ||
7449 | void | |
7450 | nfs4_state_shutdown(void) | |
7451 | { | |
5e8d5c29 | 7452 | destroy_workqueue(laundry_wq); |
c3935e30 | 7453 | nfsd4_destroy_callback_queue(); |
1da177e4 | 7454 | } |
8b70484c TM |
7455 | |
7456 | static void | |
7457 | get_stateid(struct nfsd4_compound_state *cstate, stateid_t *stateid) | |
7458 | { | |
51100d2b OK |
7459 | if (HAS_CSTATE_FLAG(cstate, CURRENT_STATE_ID_FLAG) && |
7460 | CURRENT_STATEID(stateid)) | |
37c593c5 | 7461 | memcpy(stateid, &cstate->current_stateid, sizeof(stateid_t)); |
8b70484c TM |
7462 | } |
7463 | ||
7464 | static void | |
7465 | put_stateid(struct nfsd4_compound_state *cstate, stateid_t *stateid) | |
7466 | { | |
37c593c5 TM |
7467 | if (cstate->minorversion) { |
7468 | memcpy(&cstate->current_stateid, stateid, sizeof(stateid_t)); | |
51100d2b | 7469 | SET_CSTATE_FLAG(cstate, CURRENT_STATE_ID_FLAG); |
37c593c5 TM |
7470 | } |
7471 | } | |
7472 | ||
7473 | void | |
7474 | clear_current_stateid(struct nfsd4_compound_state *cstate) | |
7475 | { | |
51100d2b | 7476 | CLEAR_CSTATE_FLAG(cstate, CURRENT_STATE_ID_FLAG); |
8b70484c TM |
7477 | } |
7478 | ||
62cd4a59 TM |
7479 | /* |
7480 | * functions to set current state id | |
7481 | */ | |
9428fe1a | 7482 | void |
b60e9859 CH |
7483 | nfsd4_set_opendowngradestateid(struct nfsd4_compound_state *cstate, |
7484 | union nfsd4_op_u *u) | |
9428fe1a | 7485 | { |
b60e9859 | 7486 | put_stateid(cstate, &u->open_downgrade.od_stateid); |
9428fe1a TM |
7487 | } |
7488 | ||
8b70484c | 7489 | void |
b60e9859 CH |
7490 | nfsd4_set_openstateid(struct nfsd4_compound_state *cstate, |
7491 | union nfsd4_op_u *u) | |
8b70484c | 7492 | { |
b60e9859 | 7493 | put_stateid(cstate, &u->open.op_stateid); |
8b70484c TM |
7494 | } |
7495 | ||
62cd4a59 | 7496 | void |
b60e9859 CH |
7497 | nfsd4_set_closestateid(struct nfsd4_compound_state *cstate, |
7498 | union nfsd4_op_u *u) | |
62cd4a59 | 7499 | { |
b60e9859 | 7500 | put_stateid(cstate, &u->close.cl_stateid); |
62cd4a59 TM |
7501 | } |
7502 | ||
7503 | void | |
b60e9859 CH |
7504 | nfsd4_set_lockstateid(struct nfsd4_compound_state *cstate, |
7505 | union nfsd4_op_u *u) | |
62cd4a59 | 7506 | { |
b60e9859 | 7507 | put_stateid(cstate, &u->lock.lk_resp_stateid); |
62cd4a59 TM |
7508 | } |
7509 | ||
7510 | /* | |
7511 | * functions to consume current state id | |
7512 | */ | |
1e97b519 | 7513 | |
9428fe1a | 7514 | void |
57832e7b CH |
7515 | nfsd4_get_opendowngradestateid(struct nfsd4_compound_state *cstate, |
7516 | union nfsd4_op_u *u) | |
9428fe1a | 7517 | { |
57832e7b | 7518 | get_stateid(cstate, &u->open_downgrade.od_stateid); |
9428fe1a TM |
7519 | } |
7520 | ||
7521 | void | |
57832e7b CH |
7522 | nfsd4_get_delegreturnstateid(struct nfsd4_compound_state *cstate, |
7523 | union nfsd4_op_u *u) | |
9428fe1a | 7524 | { |
57832e7b | 7525 | get_stateid(cstate, &u->delegreturn.dr_stateid); |
9428fe1a TM |
7526 | } |
7527 | ||
1e97b519 | 7528 | void |
57832e7b CH |
7529 | nfsd4_get_freestateid(struct nfsd4_compound_state *cstate, |
7530 | union nfsd4_op_u *u) | |
1e97b519 | 7531 | { |
57832e7b | 7532 | get_stateid(cstate, &u->free_stateid.fr_stateid); |
1e97b519 TM |
7533 | } |
7534 | ||
7535 | void | |
57832e7b CH |
7536 | nfsd4_get_setattrstateid(struct nfsd4_compound_state *cstate, |
7537 | union nfsd4_op_u *u) | |
1e97b519 | 7538 | { |
57832e7b | 7539 | get_stateid(cstate, &u->setattr.sa_stateid); |
1e97b519 TM |
7540 | } |
7541 | ||
8b70484c | 7542 | void |
57832e7b CH |
7543 | nfsd4_get_closestateid(struct nfsd4_compound_state *cstate, |
7544 | union nfsd4_op_u *u) | |
8b70484c | 7545 | { |
57832e7b | 7546 | get_stateid(cstate, &u->close.cl_stateid); |
8b70484c TM |
7547 | } |
7548 | ||
7549 | void | |
57832e7b CH |
7550 | nfsd4_get_lockustateid(struct nfsd4_compound_state *cstate, |
7551 | union nfsd4_op_u *u) | |
8b70484c | 7552 | { |
57832e7b | 7553 | get_stateid(cstate, &u->locku.lu_stateid); |
8b70484c | 7554 | } |
30813e27 TM |
7555 | |
7556 | void | |
57832e7b CH |
7557 | nfsd4_get_readstateid(struct nfsd4_compound_state *cstate, |
7558 | union nfsd4_op_u *u) | |
30813e27 | 7559 | { |
57832e7b | 7560 | get_stateid(cstate, &u->read.rd_stateid); |
30813e27 TM |
7561 | } |
7562 | ||
7563 | void | |
57832e7b CH |
7564 | nfsd4_get_writestateid(struct nfsd4_compound_state *cstate, |
7565 | union nfsd4_op_u *u) | |
30813e27 | 7566 | { |
57832e7b | 7567 | get_stateid(cstate, &u->write.wr_stateid); |
30813e27 | 7568 | } |