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