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