]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - fs/nfsd/nfs4state.c
sunrpc: add some tracepoints around enqueue and dequeue of svc_xprt
[mirror_ubuntu-artful-kernel.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>
6282cd56 44#include <linux/hash.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
N
599 }
600 hash = arch_fast_hash(&fh->fh_base, fh->fh_size, 0);
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
619 hash = arch_fast_hash(&fh->fh_base, fh->fh_size, 0);
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
ac55fdc4
JL
1717static long long
1718compare_blob(const struct xdr_netobj *o1, const struct xdr_netobj *o2)
1719{
1720 long long res;
1721
1722 res = o1->len - o2->len;
1723 if (res)
1724 return res;
1725 return (long long)memcmp(o1->data, o2->data, o1->len);
1726}
1727
35bba9a3 1728static int same_name(const char *n1, const char *n2)
599e0a22 1729{
a55370a3 1730 return 0 == memcmp(n1, n2, HEXDIR_LEN);
1da177e4
LT
1731}
1732
1733static int
599e0a22
BF
1734same_verf(nfs4_verifier *v1, nfs4_verifier *v2)
1735{
1736 return 0 == memcmp(v1->data, v2->data, sizeof(v1->data));
1da177e4
LT
1737}
1738
1739static int
599e0a22
BF
1740same_clid(clientid_t *cl1, clientid_t *cl2)
1741{
1742 return (cl1->cl_boot == cl2->cl_boot) && (cl1->cl_id == cl2->cl_id);
1da177e4
LT
1743}
1744
8fbba96e
BF
1745static bool groups_equal(struct group_info *g1, struct group_info *g2)
1746{
1747 int i;
1748
1749 if (g1->ngroups != g2->ngroups)
1750 return false;
1751 for (i=0; i<g1->ngroups; i++)
6fab8779 1752 if (!gid_eq(GROUP_AT(g1, i), GROUP_AT(g2, i)))
8fbba96e
BF
1753 return false;
1754 return true;
1755}
1756
68eb3508
BF
1757/*
1758 * RFC 3530 language requires clid_inuse be returned when the
1759 * "principal" associated with a requests differs from that previously
1760 * used. We use uid, gid's, and gss principal string as our best
1761 * approximation. We also don't want to allow non-gss use of a client
1762 * established using gss: in theory cr_principal should catch that
1763 * change, but in practice cr_principal can be null even in the gss case
1764 * since gssd doesn't always pass down a principal string.
1765 */
1766static bool is_gss_cred(struct svc_cred *cr)
1767{
1768 /* Is cr_flavor one of the gss "pseudoflavors"?: */
1769 return (cr->cr_flavor > RPC_AUTH_MAXFLAVOR);
1770}
1771
1772
5559b50a 1773static bool
599e0a22
BF
1774same_creds(struct svc_cred *cr1, struct svc_cred *cr2)
1775{
68eb3508 1776 if ((is_gss_cred(cr1) != is_gss_cred(cr2))
6fab8779
EB
1777 || (!uid_eq(cr1->cr_uid, cr2->cr_uid))
1778 || (!gid_eq(cr1->cr_gid, cr2->cr_gid))
8fbba96e
BF
1779 || !groups_equal(cr1->cr_group_info, cr2->cr_group_info))
1780 return false;
1781 if (cr1->cr_principal == cr2->cr_principal)
1782 return true;
1783 if (!cr1->cr_principal || !cr2->cr_principal)
1784 return false;
5559b50a 1785 return 0 == strcmp(cr1->cr_principal, cr2->cr_principal);
1da177e4
LT
1786}
1787
57266a6e
BF
1788static bool svc_rqst_integrity_protected(struct svc_rqst *rqstp)
1789{
1790 struct svc_cred *cr = &rqstp->rq_cred;
1791 u32 service;
1792
c4720591
BF
1793 if (!cr->cr_gss_mech)
1794 return false;
57266a6e
BF
1795 service = gss_pseudoflavor_to_service(cr->cr_gss_mech, cr->cr_flavor);
1796 return service == RPC_GSS_SVC_INTEGRITY ||
1797 service == RPC_GSS_SVC_PRIVACY;
1798}
1799
1800static bool mach_creds_match(struct nfs4_client *cl, struct svc_rqst *rqstp)
1801{
1802 struct svc_cred *cr = &rqstp->rq_cred;
1803
1804 if (!cl->cl_mach_cred)
1805 return true;
1806 if (cl->cl_cred.cr_gss_mech != cr->cr_gss_mech)
1807 return false;
1808 if (!svc_rqst_integrity_protected(rqstp))
1809 return false;
1810 if (!cr->cr_principal)
1811 return false;
1812 return 0 == strcmp(cl->cl_cred.cr_principal, cr->cr_principal);
1813}
1814
294ac32e 1815static void gen_confirm(struct nfs4_client *clp, struct nfsd_net *nn)
deda2faa 1816{
ab4684d1 1817 __be32 verf[2];
1da177e4 1818
f419992c
JL
1819 /*
1820 * This is opaque to client, so no need to byte-swap. Use
1821 * __force to keep sparse happy
1822 */
1823 verf[0] = (__force __be32)get_seconds();
294ac32e 1824 verf[1] = (__force __be32)nn->clientid_counter;
ab4684d1 1825 memcpy(clp->cl_confirm.data, verf, sizeof(clp->cl_confirm.data));
1da177e4
LT
1826}
1827
294ac32e
JL
1828static void gen_clid(struct nfs4_client *clp, struct nfsd_net *nn)
1829{
1830 clp->cl_clientid.cl_boot = nn->boot_time;
1831 clp->cl_clientid.cl_id = nn->clientid_counter++;
1832 gen_confirm(clp, nn);
1833}
1834
4770d722
JL
1835static struct nfs4_stid *
1836find_stateid_locked(struct nfs4_client *cl, stateid_t *t)
4581d140 1837{
3abdb607
BF
1838 struct nfs4_stid *ret;
1839
1840 ret = idr_find(&cl->cl_stateids, t->si_opaque.so_id);
1841 if (!ret || !ret->sc_type)
1842 return NULL;
1843 return ret;
4d71ab87
BF
1844}
1845
4770d722
JL
1846static struct nfs4_stid *
1847find_stateid_by_type(struct nfs4_client *cl, stateid_t *t, char typemask)
f459e453
BF
1848{
1849 struct nfs4_stid *s;
4d71ab87 1850
4770d722
JL
1851 spin_lock(&cl->cl_lock);
1852 s = find_stateid_locked(cl, t);
2d3f9668
TM
1853 if (s != NULL) {
1854 if (typemask & s->sc_type)
1855 atomic_inc(&s->sc_count);
1856 else
1857 s = NULL;
1858 }
4770d722
JL
1859 spin_unlock(&cl->cl_lock);
1860 return s;
4581d140
BF
1861}
1862
2216d449 1863static struct nfs4_client *create_client(struct xdr_netobj name,
b09333c4
RL
1864 struct svc_rqst *rqstp, nfs4_verifier *verf)
1865{
1866 struct nfs4_client *clp;
1867 struct sockaddr *sa = svc_addr(rqstp);
03a4e1f6 1868 int ret;
c212cecf 1869 struct net *net = SVC_NET(rqstp);
b09333c4
RL
1870
1871 clp = alloc_client(name);
1872 if (clp == NULL)
1873 return NULL;
1874
03a4e1f6
BF
1875 ret = copy_cred(&clp->cl_cred, &rqstp->rq_cred);
1876 if (ret) {
03a4e1f6 1877 free_client(clp);
03a4e1f6 1878 return NULL;
b09333c4 1879 }
0162ac2b 1880 nfsd4_init_cb(&clp->cl_cb_null, clp, NULL, NFSPROC4_CLNT_CB_NULL);
07cd4909 1881 clp->cl_time = get_seconds();
b09333c4 1882 clear_bit(0, &clp->cl_cb_slot_busy);
b09333c4
RL
1883 copy_verf(clp, verf);
1884 rpc_copy_addr((struct sockaddr *) &clp->cl_addr, sa);
edd76786 1885 clp->cl_cb_session = NULL;
c212cecf 1886 clp->net = net;
b09333c4
RL
1887 return clp;
1888}
1889
fd39ca9a 1890static void
ac55fdc4
JL
1891add_clp_to_name_tree(struct nfs4_client *new_clp, struct rb_root *root)
1892{
1893 struct rb_node **new = &(root->rb_node), *parent = NULL;
1894 struct nfs4_client *clp;
1895
1896 while (*new) {
1897 clp = rb_entry(*new, struct nfs4_client, cl_namenode);
1898 parent = *new;
1899
1900 if (compare_blob(&clp->cl_name, &new_clp->cl_name) > 0)
1901 new = &((*new)->rb_left);
1902 else
1903 new = &((*new)->rb_right);
1904 }
1905
1906 rb_link_node(&new_clp->cl_namenode, parent, new);
1907 rb_insert_color(&new_clp->cl_namenode, root);
1908}
1909
1910static struct nfs4_client *
1911find_clp_in_name_tree(struct xdr_netobj *name, struct rb_root *root)
1912{
1913 long long cmp;
1914 struct rb_node *node = root->rb_node;
1915 struct nfs4_client *clp;
1916
1917 while (node) {
1918 clp = rb_entry(node, struct nfs4_client, cl_namenode);
1919 cmp = compare_blob(&clp->cl_name, name);
1920 if (cmp > 0)
1921 node = node->rb_left;
1922 else if (cmp < 0)
1923 node = node->rb_right;
1924 else
1925 return clp;
1926 }
1927 return NULL;
1928}
1929
1930static void
1931add_to_unconfirmed(struct nfs4_client *clp)
1da177e4
LT
1932{
1933 unsigned int idhashval;
0a7ec377 1934 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
1da177e4 1935
0a880a28
TM
1936 lockdep_assert_held(&nn->client_lock);
1937
ac55fdc4 1938 clear_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags);
a99454aa 1939 add_clp_to_name_tree(clp, &nn->unconf_name_tree);
1da177e4 1940 idhashval = clientid_hashval(clp->cl_clientid.cl_id);
0a7ec377 1941 list_add(&clp->cl_idhash, &nn->unconf_id_hashtbl[idhashval]);
3dbacee6 1942 renew_client_locked(clp);
1da177e4
LT
1943}
1944
fd39ca9a 1945static void
1da177e4
LT
1946move_to_confirmed(struct nfs4_client *clp)
1947{
1948 unsigned int idhashval = clientid_hashval(clp->cl_clientid.cl_id);
8daae4dc 1949 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
1da177e4 1950
0a880a28
TM
1951 lockdep_assert_held(&nn->client_lock);
1952
1da177e4 1953 dprintk("NFSD: move_to_confirm nfs4_client %p\n", clp);
8daae4dc 1954 list_move(&clp->cl_idhash, &nn->conf_id_hashtbl[idhashval]);
a99454aa 1955 rb_erase(&clp->cl_namenode, &nn->unconf_name_tree);
382a62e7 1956 add_clp_to_name_tree(clp, &nn->conf_name_tree);
ac55fdc4 1957 set_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags);
3dbacee6 1958 renew_client_locked(clp);
1da177e4
LT
1959}
1960
1961static struct nfs4_client *
bfa85e83 1962find_client_in_id_table(struct list_head *tbl, clientid_t *clid, bool sessions)
1da177e4
LT
1963{
1964 struct nfs4_client *clp;
1965 unsigned int idhashval = clientid_hashval(clid->cl_id);
1966
bfa85e83 1967 list_for_each_entry(clp, &tbl[idhashval], cl_idhash) {
a50d2ad1 1968 if (same_clid(&clp->cl_clientid, clid)) {
d15c077e
BF
1969 if ((bool)clp->cl_minorversion != sessions)
1970 return NULL;
3dbacee6 1971 renew_client_locked(clp);
1da177e4 1972 return clp;
a50d2ad1 1973 }
1da177e4
LT
1974 }
1975 return NULL;
1976}
1977
bfa85e83
BF
1978static struct nfs4_client *
1979find_confirmed_client(clientid_t *clid, bool sessions, struct nfsd_net *nn)
1980{
1981 struct list_head *tbl = nn->conf_id_hashtbl;
1982
0a880a28 1983 lockdep_assert_held(&nn->client_lock);
bfa85e83
BF
1984 return find_client_in_id_table(tbl, clid, sessions);
1985}
1986
1da177e4 1987static struct nfs4_client *
0a7ec377 1988find_unconfirmed_client(clientid_t *clid, bool sessions, struct nfsd_net *nn)
1da177e4 1989{
bfa85e83 1990 struct list_head *tbl = nn->unconf_id_hashtbl;
1da177e4 1991
0a880a28 1992 lockdep_assert_held(&nn->client_lock);
bfa85e83 1993 return find_client_in_id_table(tbl, clid, sessions);
1da177e4
LT
1994}
1995
6e5f15c9 1996static bool clp_used_exchangeid(struct nfs4_client *clp)
a1bcecd2 1997{
6e5f15c9 1998 return clp->cl_exchange_flags != 0;
e203d506 1999}
a1bcecd2 2000
28ce6054 2001static struct nfs4_client *
382a62e7 2002find_confirmed_client_by_name(struct xdr_netobj *name, struct nfsd_net *nn)
28ce6054 2003{
0a880a28 2004 lockdep_assert_held(&nn->client_lock);
382a62e7 2005 return find_clp_in_name_tree(name, &nn->conf_name_tree);
28ce6054
N
2006}
2007
2008static struct nfs4_client *
a99454aa 2009find_unconfirmed_client_by_name(struct xdr_netobj *name, struct nfsd_net *nn)
28ce6054 2010{
0a880a28 2011 lockdep_assert_held(&nn->client_lock);
a99454aa 2012 return find_clp_in_name_tree(name, &nn->unconf_name_tree);
28ce6054
N
2013}
2014
fd39ca9a 2015static void
6f3d772f 2016gen_callback(struct nfs4_client *clp, struct nfsd4_setclientid *se, struct svc_rqst *rqstp)
1da177e4 2017{
07263f1e 2018 struct nfs4_cb_conn *conn = &clp->cl_cb_conn;
6f3d772f
TU
2019 struct sockaddr *sa = svc_addr(rqstp);
2020 u32 scopeid = rpc_get_scope_id(sa);
7077ecba
JL
2021 unsigned short expected_family;
2022
2023 /* Currently, we only support tcp and tcp6 for the callback channel */
2024 if (se->se_callback_netid_len == 3 &&
2025 !memcmp(se->se_callback_netid_val, "tcp", 3))
2026 expected_family = AF_INET;
2027 else if (se->se_callback_netid_len == 4 &&
2028 !memcmp(se->se_callback_netid_val, "tcp6", 4))
2029 expected_family = AF_INET6;
2030 else
1da177e4
LT
2031 goto out_err;
2032
c212cecf 2033 conn->cb_addrlen = rpc_uaddr2sockaddr(clp->net, se->se_callback_addr_val,
aa9a4ec7 2034 se->se_callback_addr_len,
07263f1e
BF
2035 (struct sockaddr *)&conn->cb_addr,
2036 sizeof(conn->cb_addr));
aa9a4ec7 2037
07263f1e 2038 if (!conn->cb_addrlen || conn->cb_addr.ss_family != expected_family)
1da177e4 2039 goto out_err;
aa9a4ec7 2040
07263f1e
BF
2041 if (conn->cb_addr.ss_family == AF_INET6)
2042 ((struct sockaddr_in6 *)&conn->cb_addr)->sin6_scope_id = scopeid;
fbf4665f 2043
07263f1e
BF
2044 conn->cb_prog = se->se_callback_prog;
2045 conn->cb_ident = se->se_callback_ident;
849a1cf1 2046 memcpy(&conn->cb_saddr, &rqstp->rq_daddr, rqstp->rq_daddrlen);
1da177e4
LT
2047 return;
2048out_err:
07263f1e
BF
2049 conn->cb_addr.ss_family = AF_UNSPEC;
2050 conn->cb_addrlen = 0;
849823c5 2051 dprintk(KERN_INFO "NFSD: this client (clientid %08x/%08x) "
1da177e4
LT
2052 "will not receive delegations\n",
2053 clp->cl_clientid.cl_boot, clp->cl_clientid.cl_id);
2054
1da177e4
LT
2055 return;
2056}
2057
074fe897 2058/*
067e1ace 2059 * Cache a reply. nfsd4_check_resp_size() has bounded the cache size.
074fe897 2060 */
b607664e 2061static void
074fe897 2062nfsd4_store_cache_entry(struct nfsd4_compoundres *resp)
074fe897 2063{
f5236013 2064 struct xdr_buf *buf = resp->xdr.buf;
557ce264
AA
2065 struct nfsd4_slot *slot = resp->cstate.slot;
2066 unsigned int base;
074fe897 2067
557ce264 2068 dprintk("--> %s slot %p\n", __func__, slot);
074fe897 2069
557ce264
AA
2070 slot->sl_opcnt = resp->opcnt;
2071 slot->sl_status = resp->cstate.status;
074fe897 2072
bf5c43c8 2073 slot->sl_flags |= NFSD4_SLOT_INITIALIZED;
bf864a31 2074 if (nfsd4_not_cached(resp)) {
557ce264 2075 slot->sl_datalen = 0;
bf864a31 2076 return;
074fe897 2077 }
f5236013
BF
2078 base = resp->cstate.data_offset;
2079 slot->sl_datalen = buf->len - base;
2080 if (read_bytes_from_xdr_buf(buf, base, slot->sl_data, slot->sl_datalen))
557ce264
AA
2081 WARN("%s: sessions DRC could not cache compound\n", __func__);
2082 return;
074fe897
AA
2083}
2084
2085/*
abfabf8c
AA
2086 * Encode the replay sequence operation from the slot values.
2087 * If cachethis is FALSE encode the uncached rep error on the next
2088 * operation which sets resp->p and increments resp->opcnt for
2089 * nfs4svc_encode_compoundres.
074fe897 2090 *
074fe897 2091 */
abfabf8c
AA
2092static __be32
2093nfsd4_enc_sequence_replay(struct nfsd4_compoundargs *args,
2094 struct nfsd4_compoundres *resp)
074fe897 2095{
abfabf8c
AA
2096 struct nfsd4_op *op;
2097 struct nfsd4_slot *slot = resp->cstate.slot;
bf864a31 2098
abfabf8c
AA
2099 /* Encode the replayed sequence operation */
2100 op = &args->ops[resp->opcnt - 1];
2101 nfsd4_encode_operation(resp, op);
bf864a31 2102
abfabf8c 2103 /* Return nfserr_retry_uncached_rep in next operation. */
73e79482 2104 if (args->opcnt > 1 && !(slot->sl_flags & NFSD4_SLOT_CACHETHIS)) {
abfabf8c
AA
2105 op = &args->ops[resp->opcnt++];
2106 op->status = nfserr_retry_uncached_rep;
2107 nfsd4_encode_operation(resp, op);
074fe897 2108 }
abfabf8c 2109 return op->status;
074fe897
AA
2110}
2111
2112/*
557ce264
AA
2113 * The sequence operation is not cached because we can use the slot and
2114 * session values.
074fe897 2115 */
3ca2eb98 2116static __be32
bf864a31
AA
2117nfsd4_replay_cache_entry(struct nfsd4_compoundres *resp,
2118 struct nfsd4_sequence *seq)
074fe897 2119{
557ce264 2120 struct nfsd4_slot *slot = resp->cstate.slot;
f5236013
BF
2121 struct xdr_stream *xdr = &resp->xdr;
2122 __be32 *p;
074fe897
AA
2123 __be32 status;
2124
557ce264 2125 dprintk("--> %s slot %p\n", __func__, slot);
074fe897 2126
abfabf8c 2127 status = nfsd4_enc_sequence_replay(resp->rqstp->rq_argp, resp);
0da7b19c 2128 if (status)
abfabf8c 2129 return status;
074fe897 2130
f5236013
BF
2131 p = xdr_reserve_space(xdr, slot->sl_datalen);
2132 if (!p) {
2133 WARN_ON_ONCE(1);
2134 return nfserr_serverfault;
2135 }
2136 xdr_encode_opaque_fixed(p, slot->sl_data, slot->sl_datalen);
2137 xdr_commit_encode(xdr);
074fe897 2138
557ce264 2139 resp->opcnt = slot->sl_opcnt;
f5236013 2140 return slot->sl_status;
074fe897
AA
2141}
2142
0733d213
AA
2143/*
2144 * Set the exchange_id flags returned by the server.
2145 */
2146static void
2147nfsd4_set_ex_flags(struct nfs4_client *new, struct nfsd4_exchange_id *clid)
2148{
2149 /* pNFS is not supported */
2150 new->cl_exchange_flags |= EXCHGID4_FLAG_USE_NON_PNFS;
2151
2152 /* Referrals are supported, Migration is not. */
2153 new->cl_exchange_flags |= EXCHGID4_FLAG_SUPP_MOVED_REFER;
2154
2155 /* set the wire flags to return to client. */
2156 clid->flags = new->cl_exchange_flags;
2157}
2158
631fc9ea
BF
2159static bool client_has_state(struct nfs4_client *clp)
2160{
2161 /*
2162 * Note clp->cl_openowners check isn't quite right: there's no
2163 * need to count owners without stateid's.
2164 *
2165 * Also note we should probably be using this in 4.0 case too.
2166 */
6eccece9
BF
2167 return !list_empty(&clp->cl_openowners)
2168 || !list_empty(&clp->cl_delegations)
2169 || !list_empty(&clp->cl_sessions);
631fc9ea
BF
2170}
2171
069b6ad4
AA
2172__be32
2173nfsd4_exchange_id(struct svc_rqst *rqstp,
2174 struct nfsd4_compound_state *cstate,
2175 struct nfsd4_exchange_id *exid)
2176{
3dbacee6
TM
2177 struct nfs4_client *conf, *new;
2178 struct nfs4_client *unconf = NULL;
57b7b43b 2179 __be32 status;
363168b4 2180 char addr_str[INET6_ADDRSTRLEN];
0733d213 2181 nfs4_verifier verf = exid->verifier;
363168b4 2182 struct sockaddr *sa = svc_addr(rqstp);
83e08fd4 2183 bool update = exid->flags & EXCHGID4_FLAG_UPD_CONFIRMED_REC_A;
c212cecf 2184 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
0733d213 2185
363168b4 2186 rpc_ntop(sa, addr_str, sizeof(addr_str));
0733d213 2187 dprintk("%s rqstp=%p exid=%p clname.len=%u clname.data=%p "
363168b4 2188 "ip_addr=%s flags %x, spa_how %d\n",
0733d213 2189 __func__, rqstp, exid, exid->clname.len, exid->clname.data,
363168b4 2190 addr_str, exid->flags, exid->spa_how);
0733d213 2191
a084daf5 2192 if (exid->flags & ~EXCHGID4_FLAG_MASK_A)
0733d213
AA
2193 return nfserr_inval;
2194
0733d213 2195 switch (exid->spa_how) {
57266a6e
BF
2196 case SP4_MACH_CRED:
2197 if (!svc_rqst_integrity_protected(rqstp))
2198 return nfserr_inval;
0733d213
AA
2199 case SP4_NONE:
2200 break;
063b0fb9
BF
2201 default: /* checked by xdr code */
2202 WARN_ON_ONCE(1);
0733d213 2203 case SP4_SSV:
dd30333c 2204 return nfserr_encr_alg_unsupp;
0733d213
AA
2205 }
2206
5cc40fd7
TM
2207 new = create_client(exid->clname, rqstp, &verf);
2208 if (new == NULL)
2209 return nfserr_jukebox;
2210
2dbb269d 2211 /* Cases below refer to rfc 5661 section 18.35.4: */
3dbacee6 2212 spin_lock(&nn->client_lock);
382a62e7 2213 conf = find_confirmed_client_by_name(&exid->clname, nn);
0733d213 2214 if (conf) {
83e08fd4
BF
2215 bool creds_match = same_creds(&conf->cl_cred, &rqstp->rq_cred);
2216 bool verfs_match = same_verf(&verf, &conf->cl_verifier);
2217
136e658d
BF
2218 if (update) {
2219 if (!clp_used_exchangeid(conf)) { /* buggy client */
2dbb269d 2220 status = nfserr_inval;
1a308118
BF
2221 goto out;
2222 }
57266a6e
BF
2223 if (!mach_creds_match(conf, rqstp)) {
2224 status = nfserr_wrong_cred;
2225 goto out;
2226 }
136e658d 2227 if (!creds_match) { /* case 9 */
ea236d07 2228 status = nfserr_perm;
136e658d
BF
2229 goto out;
2230 }
2231 if (!verfs_match) { /* case 8 */
0733d213
AA
2232 status = nfserr_not_same;
2233 goto out;
2234 }
136e658d
BF
2235 /* case 6 */
2236 exid->flags |= EXCHGID4_FLAG_CONFIRMED_R;
136e658d 2237 goto out_copy;
0733d213 2238 }
136e658d 2239 if (!creds_match) { /* case 3 */
631fc9ea
BF
2240 if (client_has_state(conf)) {
2241 status = nfserr_clid_inuse;
0733d213
AA
2242 goto out;
2243 }
0733d213
AA
2244 goto out_new;
2245 }
136e658d 2246 if (verfs_match) { /* case 2 */
0f1ba0ef 2247 conf->cl_exchange_flags |= EXCHGID4_FLAG_CONFIRMED_R;
136e658d
BF
2248 goto out_copy;
2249 }
2250 /* case 5, client reboot */
3dbacee6 2251 conf = NULL;
136e658d 2252 goto out_new;
6ddbbbfe
MS
2253 }
2254
2dbb269d 2255 if (update) { /* case 7 */
6ddbbbfe
MS
2256 status = nfserr_noent;
2257 goto out;
0733d213
AA
2258 }
2259
a99454aa 2260 unconf = find_unconfirmed_client_by_name(&exid->clname, nn);
2dbb269d 2261 if (unconf) /* case 4, possible retry or client restart */
3dbacee6 2262 unhash_client_locked(unconf);
0733d213 2263
2dbb269d 2264 /* case 1 (normal case) */
0733d213 2265out_new:
fd699b8a
JL
2266 if (conf) {
2267 status = mark_client_expired_locked(conf);
2268 if (status)
2269 goto out;
2270 }
4f540e29 2271 new->cl_minorversion = cstate->minorversion;
57266a6e 2272 new->cl_mach_cred = (exid->spa_how == SP4_MACH_CRED);
0733d213 2273
c212cecf 2274 gen_clid(new, nn);
ac55fdc4 2275 add_to_unconfirmed(new);
3dbacee6 2276 swap(new, conf);
0733d213 2277out_copy:
5cc40fd7
TM
2278 exid->clientid.cl_boot = conf->cl_clientid.cl_boot;
2279 exid->clientid.cl_id = conf->cl_clientid.cl_id;
0733d213 2280
5cc40fd7
TM
2281 exid->seqid = conf->cl_cs_slot.sl_seqid + 1;
2282 nfsd4_set_ex_flags(conf, exid);
0733d213
AA
2283
2284 dprintk("nfsd4_exchange_id seqid %d flags %x\n",
5cc40fd7 2285 conf->cl_cs_slot.sl_seqid, conf->cl_exchange_flags);
0733d213
AA
2286 status = nfs_ok;
2287
2288out:
3dbacee6 2289 spin_unlock(&nn->client_lock);
5cc40fd7 2290 if (new)
3dbacee6
TM
2291 expire_client(new);
2292 if (unconf)
2293 expire_client(unconf);
0733d213 2294 return status;
069b6ad4
AA
2295}
2296
57b7b43b 2297static __be32
88e588d5 2298check_slot_seqid(u32 seqid, u32 slot_seqid, int slot_inuse)
b85d4c01 2299{
88e588d5
AA
2300 dprintk("%s enter. seqid %d slot_seqid %d\n", __func__, seqid,
2301 slot_seqid);
b85d4c01
BH
2302
2303 /* The slot is in use, and no response has been sent. */
88e588d5
AA
2304 if (slot_inuse) {
2305 if (seqid == slot_seqid)
b85d4c01
BH
2306 return nfserr_jukebox;
2307 else
2308 return nfserr_seq_misordered;
2309 }
f6d82485 2310 /* Note unsigned 32-bit arithmetic handles wraparound: */
88e588d5 2311 if (likely(seqid == slot_seqid + 1))
b85d4c01 2312 return nfs_ok;
88e588d5 2313 if (seqid == slot_seqid)
b85d4c01 2314 return nfserr_replay_cache;
b85d4c01
BH
2315 return nfserr_seq_misordered;
2316}
2317
49557cc7
AA
2318/*
2319 * Cache the create session result into the create session single DRC
2320 * slot cache by saving the xdr structure. sl_seqid has been set.
2321 * Do this for solo or embedded create session operations.
2322 */
2323static void
2324nfsd4_cache_create_session(struct nfsd4_create_session *cr_ses,
57b7b43b 2325 struct nfsd4_clid_slot *slot, __be32 nfserr)
49557cc7
AA
2326{
2327 slot->sl_status = nfserr;
2328 memcpy(&slot->sl_cr_ses, cr_ses, sizeof(*cr_ses));
2329}
2330
2331static __be32
2332nfsd4_replay_create_session(struct nfsd4_create_session *cr_ses,
2333 struct nfsd4_clid_slot *slot)
2334{
2335 memcpy(cr_ses, &slot->sl_cr_ses, sizeof(*cr_ses));
2336 return slot->sl_status;
2337}
2338
1b74c25b
MJ
2339#define NFSD_MIN_REQ_HDR_SEQ_SZ ((\
2340 2 * 2 + /* credential,verifier: AUTH_NULL, length 0 */ \
2341 1 + /* MIN tag is length with zero, only length */ \
2342 3 + /* version, opcount, opcode */ \
2343 XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \
2344 /* seqid, slotID, slotID, cache */ \
2345 4 ) * sizeof(__be32))
2346
2347#define NFSD_MIN_RESP_HDR_SEQ_SZ ((\
2348 2 + /* verifier: AUTH_NULL, length 0 */\
2349 1 + /* status */ \
2350 1 + /* MIN tag is length with zero, only length */ \
2351 3 + /* opcount, opcode, opstatus*/ \
2352 XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \
2353 /* seqid, slotID, slotID, slotID, status */ \
2354 5 ) * sizeof(__be32))
2355
55c760cf 2356static __be32 check_forechannel_attrs(struct nfsd4_channel_attrs *ca, struct nfsd_net *nn)
1b74c25b 2357{
55c760cf
BF
2358 u32 maxrpc = nn->nfsd_serv->sv_max_mesg;
2359
373cd409
BF
2360 if (ca->maxreq_sz < NFSD_MIN_REQ_HDR_SEQ_SZ)
2361 return nfserr_toosmall;
2362 if (ca->maxresp_sz < NFSD_MIN_RESP_HDR_SEQ_SZ)
2363 return nfserr_toosmall;
55c760cf
BF
2364 ca->headerpadsz = 0;
2365 ca->maxreq_sz = min_t(u32, ca->maxreq_sz, maxrpc);
2366 ca->maxresp_sz = min_t(u32, ca->maxresp_sz, maxrpc);
2367 ca->maxops = min_t(u32, ca->maxops, NFSD_MAX_OPS_PER_COMPOUND);
2368 ca->maxresp_cached = min_t(u32, ca->maxresp_cached,
2369 NFSD_SLOT_CACHE_SIZE + NFSD_MIN_HDR_SEQ_SZ);
2370 ca->maxreqs = min_t(u32, ca->maxreqs, NFSD_MAX_SLOTS_PER_SESSION);
2371 /*
2372 * Note decreasing slot size below client's request may make it
2373 * difficult for client to function correctly, whereas
2374 * decreasing the number of slots will (just?) affect
2375 * performance. When short on memory we therefore prefer to
2376 * decrease number of slots instead of their size. Clients that
2377 * request larger slots than they need will get poor results:
2378 */
2379 ca->maxreqs = nfsd4_get_drc_mem(ca);
2380 if (!ca->maxreqs)
2381 return nfserr_jukebox;
2382
373cd409 2383 return nfs_ok;
1b74c25b
MJ
2384}
2385
8a891633
KM
2386#define NFSD_CB_MAX_REQ_SZ ((NFS4_enc_cb_recall_sz + \
2387 RPC_MAX_HEADER_WITH_AUTH) * sizeof(__be32))
2388#define NFSD_CB_MAX_RESP_SZ ((NFS4_dec_cb_recall_sz + \
2389 RPC_MAX_REPHEADER_WITH_AUTH) * sizeof(__be32))
2390
06b332a5 2391static __be32 check_backchannel_attrs(struct nfsd4_channel_attrs *ca)
1b74c25b 2392{
06b332a5
BF
2393 ca->headerpadsz = 0;
2394
2395 /*
2396 * These RPC_MAX_HEADER macros are overkill, especially since we
2397 * don't even do gss on the backchannel yet. But this is still
2398 * less than 1k. Tighten up this estimate in the unlikely event
2399 * it turns out to be a problem for some client:
2400 */
8a891633 2401 if (ca->maxreq_sz < NFSD_CB_MAX_REQ_SZ)
06b332a5 2402 return nfserr_toosmall;
8a891633 2403 if (ca->maxresp_sz < NFSD_CB_MAX_RESP_SZ)
06b332a5
BF
2404 return nfserr_toosmall;
2405 ca->maxresp_cached = 0;
2406 if (ca->maxops < 2)
2407 return nfserr_toosmall;
2408
2409 return nfs_ok;
1b74c25b
MJ
2410}
2411
b78724b7
BF
2412static __be32 nfsd4_check_cb_sec(struct nfsd4_cb_sec *cbs)
2413{
2414 switch (cbs->flavor) {
2415 case RPC_AUTH_NULL:
2416 case RPC_AUTH_UNIX:
2417 return nfs_ok;
2418 default:
2419 /*
2420 * GSS case: the spec doesn't allow us to return this
2421 * error. But it also doesn't allow us not to support
2422 * GSS.
2423 * I'd rather this fail hard than return some error the
2424 * client might think it can already handle:
2425 */
2426 return nfserr_encr_alg_unsupp;
2427 }
2428}
2429
069b6ad4
AA
2430__be32
2431nfsd4_create_session(struct svc_rqst *rqstp,
2432 struct nfsd4_compound_state *cstate,
2433 struct nfsd4_create_session *cr_ses)
2434{
363168b4 2435 struct sockaddr *sa = svc_addr(rqstp);
ec6b5d7b 2436 struct nfs4_client *conf, *unconf;
d20c11d8 2437 struct nfs4_client *old = NULL;
ac7c46f2 2438 struct nfsd4_session *new;
81f0b2a4 2439 struct nfsd4_conn *conn;
49557cc7 2440 struct nfsd4_clid_slot *cs_slot = NULL;
57b7b43b 2441 __be32 status = 0;
8daae4dc 2442 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
ec6b5d7b 2443
a62573dc
MJ
2444 if (cr_ses->flags & ~SESSION4_FLAG_MASK_A)
2445 return nfserr_inval;
b78724b7
BF
2446 status = nfsd4_check_cb_sec(&cr_ses->cb_sec);
2447 if (status)
2448 return status;
55c760cf 2449 status = check_forechannel_attrs(&cr_ses->fore_channel, nn);
06b332a5
BF
2450 if (status)
2451 return status;
2452 status = check_backchannel_attrs(&cr_ses->back_channel);
373cd409 2453 if (status)
f403e450 2454 goto out_release_drc_mem;
81f0b2a4 2455 status = nfserr_jukebox;
60810e54 2456 new = alloc_session(&cr_ses->fore_channel, &cr_ses->back_channel);
55c760cf
BF
2457 if (!new)
2458 goto out_release_drc_mem;
81f0b2a4
BF
2459 conn = alloc_conn_from_crses(rqstp, cr_ses);
2460 if (!conn)
2461 goto out_free_session;
a62573dc 2462
d20c11d8 2463 spin_lock(&nn->client_lock);
0a7ec377 2464 unconf = find_unconfirmed_client(&cr_ses->clientid, true, nn);
8daae4dc 2465 conf = find_confirmed_client(&cr_ses->clientid, true, nn);
78389046 2466 WARN_ON_ONCE(conf && unconf);
ec6b5d7b
AA
2467
2468 if (conf) {
57266a6e
BF
2469 status = nfserr_wrong_cred;
2470 if (!mach_creds_match(conf, rqstp))
2471 goto out_free_conn;
49557cc7
AA
2472 cs_slot = &conf->cl_cs_slot;
2473 status = check_slot_seqid(cr_ses->seqid, cs_slot->sl_seqid, 0);
38eb76a5 2474 if (status == nfserr_replay_cache) {
49557cc7 2475 status = nfsd4_replay_create_session(cr_ses, cs_slot);
81f0b2a4 2476 goto out_free_conn;
49557cc7 2477 } else if (cr_ses->seqid != cs_slot->sl_seqid + 1) {
ec6b5d7b 2478 status = nfserr_seq_misordered;
81f0b2a4 2479 goto out_free_conn;
ec6b5d7b 2480 }
ec6b5d7b
AA
2481 } else if (unconf) {
2482 if (!same_creds(&unconf->cl_cred, &rqstp->rq_cred) ||
363168b4 2483 !rpc_cmp_addr(sa, (struct sockaddr *) &unconf->cl_addr)) {
ec6b5d7b 2484 status = nfserr_clid_inuse;
81f0b2a4 2485 goto out_free_conn;
ec6b5d7b 2486 }
57266a6e
BF
2487 status = nfserr_wrong_cred;
2488 if (!mach_creds_match(unconf, rqstp))
2489 goto out_free_conn;
49557cc7
AA
2490 cs_slot = &unconf->cl_cs_slot;
2491 status = check_slot_seqid(cr_ses->seqid, cs_slot->sl_seqid, 0);
38eb76a5
AA
2492 if (status) {
2493 /* an unconfirmed replay returns misordered */
ec6b5d7b 2494 status = nfserr_seq_misordered;
81f0b2a4 2495 goto out_free_conn;
ec6b5d7b 2496 }
382a62e7 2497 old = find_confirmed_client_by_name(&unconf->cl_name, nn);
221a6876 2498 if (old) {
d20c11d8 2499 status = mark_client_expired_locked(old);
7abea1e8
JL
2500 if (status) {
2501 old = NULL;
221a6876 2502 goto out_free_conn;
7abea1e8 2503 }
221a6876 2504 }
8f9d3d3b 2505 move_to_confirmed(unconf);
ec6b5d7b
AA
2506 conf = unconf;
2507 } else {
2508 status = nfserr_stale_clientid;
81f0b2a4 2509 goto out_free_conn;
ec6b5d7b 2510 }
81f0b2a4 2511 status = nfs_ok;
408b79bc
BF
2512 /*
2513 * We do not support RDMA or persistent sessions
2514 */
2515 cr_ses->flags &= ~SESSION4_PERSIST;
2516 cr_ses->flags &= ~SESSION4_RDMA;
2517
81f0b2a4 2518 init_session(rqstp, new, conf, cr_ses);
d20c11d8 2519 nfsd4_get_session_locked(new);
81f0b2a4 2520
ac7c46f2 2521 memcpy(cr_ses->sessionid.data, new->se_sessionid.data,
ec6b5d7b 2522 NFS4_MAX_SESSIONID_LEN);
86c3e16c 2523 cs_slot->sl_seqid++;
49557cc7 2524 cr_ses->seqid = cs_slot->sl_seqid;
ec6b5d7b 2525
d20c11d8 2526 /* cache solo and embedded create sessions under the client_lock */
49557cc7 2527 nfsd4_cache_create_session(cr_ses, cs_slot, status);
d20c11d8
JL
2528 spin_unlock(&nn->client_lock);
2529 /* init connection and backchannel */
2530 nfsd4_init_conn(rqstp, conn, new);
2531 nfsd4_put_session(new);
d20c11d8
JL
2532 if (old)
2533 expire_client(old);
ec6b5d7b 2534 return status;
81f0b2a4 2535out_free_conn:
d20c11d8 2536 spin_unlock(&nn->client_lock);
81f0b2a4 2537 free_conn(conn);
d20c11d8
JL
2538 if (old)
2539 expire_client(old);
81f0b2a4
BF
2540out_free_session:
2541 __free_session(new);
55c760cf
BF
2542out_release_drc_mem:
2543 nfsd4_put_drc_mem(&cr_ses->fore_channel);
1ca50792 2544 return status;
069b6ad4
AA
2545}
2546
1d1bc8f2
BF
2547static __be32 nfsd4_map_bcts_dir(u32 *dir)
2548{
2549 switch (*dir) {
2550 case NFS4_CDFC4_FORE:
2551 case NFS4_CDFC4_BACK:
2552 return nfs_ok;
2553 case NFS4_CDFC4_FORE_OR_BOTH:
2554 case NFS4_CDFC4_BACK_OR_BOTH:
2555 *dir = NFS4_CDFC4_BOTH;
2556 return nfs_ok;
2557 };
2558 return nfserr_inval;
2559}
2560
cb73a9f4
BF
2561__be32 nfsd4_backchannel_ctl(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_backchannel_ctl *bc)
2562{
2563 struct nfsd4_session *session = cstate->session;
c9a49628 2564 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
b78724b7 2565 __be32 status;
cb73a9f4 2566
b78724b7
BF
2567 status = nfsd4_check_cb_sec(&bc->bc_cb_sec);
2568 if (status)
2569 return status;
c9a49628 2570 spin_lock(&nn->client_lock);
cb73a9f4
BF
2571 session->se_cb_prog = bc->bc_cb_program;
2572 session->se_cb_sec = bc->bc_cb_sec;
c9a49628 2573 spin_unlock(&nn->client_lock);
cb73a9f4
BF
2574
2575 nfsd4_probe_callback(session->se_client);
2576
2577 return nfs_ok;
2578}
2579
1d1bc8f2
BF
2580__be32 nfsd4_bind_conn_to_session(struct svc_rqst *rqstp,
2581 struct nfsd4_compound_state *cstate,
2582 struct nfsd4_bind_conn_to_session *bcts)
2583{
2584 __be32 status;
3ba63671 2585 struct nfsd4_conn *conn;
4f6e6c17 2586 struct nfsd4_session *session;
d4e19e70
TM
2587 struct net *net = SVC_NET(rqstp);
2588 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
1d1bc8f2
BF
2589
2590 if (!nfsd4_last_compound_op(rqstp))
2591 return nfserr_not_only_op;
c9a49628 2592 spin_lock(&nn->client_lock);
d4e19e70 2593 session = find_in_sessionid_hashtbl(&bcts->sessionid, net, &status);
c9a49628 2594 spin_unlock(&nn->client_lock);
4f6e6c17 2595 if (!session)
d4e19e70 2596 goto out_no_session;
57266a6e
BF
2597 status = nfserr_wrong_cred;
2598 if (!mach_creds_match(session->se_client, rqstp))
2599 goto out;
1d1bc8f2 2600 status = nfsd4_map_bcts_dir(&bcts->dir);
3ba63671 2601 if (status)
4f6e6c17 2602 goto out;
3ba63671 2603 conn = alloc_conn(rqstp, bcts->dir);
4f6e6c17 2604 status = nfserr_jukebox;
3ba63671 2605 if (!conn)
4f6e6c17
BF
2606 goto out;
2607 nfsd4_init_conn(rqstp, conn, session);
2608 status = nfs_ok;
2609out:
d4e19e70
TM
2610 nfsd4_put_session(session);
2611out_no_session:
4f6e6c17 2612 return status;
1d1bc8f2
BF
2613}
2614
5d4cec2f
BF
2615static bool nfsd4_compound_in_session(struct nfsd4_session *session, struct nfs4_sessionid *sid)
2616{
2617 if (!session)
2618 return 0;
2619 return !memcmp(sid, &session->se_sessionid, sizeof(*sid));
2620}
2621
069b6ad4
AA
2622__be32
2623nfsd4_destroy_session(struct svc_rqst *r,
2624 struct nfsd4_compound_state *cstate,
2625 struct nfsd4_destroy_session *sessionid)
2626{
e10e0cfc 2627 struct nfsd4_session *ses;
abcdff09 2628 __be32 status;
f0f51f5c 2629 int ref_held_by_me = 0;
d4e19e70
TM
2630 struct net *net = SVC_NET(r);
2631 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
e10e0cfc 2632
abcdff09 2633 status = nfserr_not_only_op;
5d4cec2f 2634 if (nfsd4_compound_in_session(cstate->session, &sessionid->sessionid)) {
57716355 2635 if (!nfsd4_last_compound_op(r))
abcdff09 2636 goto out;
f0f51f5c 2637 ref_held_by_me++;
57716355 2638 }
e10e0cfc 2639 dump_sessionid(__func__, &sessionid->sessionid);
c9a49628 2640 spin_lock(&nn->client_lock);
d4e19e70 2641 ses = find_in_sessionid_hashtbl(&sessionid->sessionid, net, &status);
abcdff09
BF
2642 if (!ses)
2643 goto out_client_lock;
57266a6e
BF
2644 status = nfserr_wrong_cred;
2645 if (!mach_creds_match(ses->se_client, r))
d4e19e70 2646 goto out_put_session;
f0f51f5c 2647 status = mark_session_dead_locked(ses, 1 + ref_held_by_me);
66b2b9b2 2648 if (status)
f0f51f5c 2649 goto out_put_session;
e10e0cfc 2650 unhash_session(ses);
c9a49628 2651 spin_unlock(&nn->client_lock);
e10e0cfc 2652
84f5f7cc 2653 nfsd4_probe_callback_sync(ses->se_client);
19cf5c02 2654
c9a49628 2655 spin_lock(&nn->client_lock);
e10e0cfc 2656 status = nfs_ok;
f0f51f5c 2657out_put_session:
d4e19e70 2658 nfsd4_put_session_locked(ses);
abcdff09
BF
2659out_client_lock:
2660 spin_unlock(&nn->client_lock);
e10e0cfc 2661out:
e10e0cfc 2662 return status;
069b6ad4
AA
2663}
2664
a663bdd8 2665static struct nfsd4_conn *__nfsd4_find_conn(struct svc_xprt *xpt, struct nfsd4_session *s)
328ead28
BF
2666{
2667 struct nfsd4_conn *c;
2668
2669 list_for_each_entry(c, &s->se_conns, cn_persession) {
a663bdd8 2670 if (c->cn_xprt == xpt) {
328ead28
BF
2671 return c;
2672 }
2673 }
2674 return NULL;
2675}
2676
57266a6e 2677static __be32 nfsd4_sequence_check_conn(struct nfsd4_conn *new, struct nfsd4_session *ses)
328ead28
BF
2678{
2679 struct nfs4_client *clp = ses->se_client;
a663bdd8 2680 struct nfsd4_conn *c;
57266a6e 2681 __be32 status = nfs_ok;
21b75b01 2682 int ret;
328ead28
BF
2683
2684 spin_lock(&clp->cl_lock);
a663bdd8 2685 c = __nfsd4_find_conn(new->cn_xprt, ses);
57266a6e
BF
2686 if (c)
2687 goto out_free;
2688 status = nfserr_conn_not_bound_to_session;
2689 if (clp->cl_mach_cred)
2690 goto out_free;
328ead28
BF
2691 __nfsd4_hash_conn(new, ses);
2692 spin_unlock(&clp->cl_lock);
21b75b01
BF
2693 ret = nfsd4_register_conn(new);
2694 if (ret)
2695 /* oops; xprt is already down: */
2696 nfsd4_conn_lost(&new->cn_xpt_user);
57266a6e
BF
2697 return nfs_ok;
2698out_free:
2699 spin_unlock(&clp->cl_lock);
2700 free_conn(new);
2701 return status;
328ead28
BF
2702}
2703
868b89c3
MJ
2704static bool nfsd4_session_too_many_ops(struct svc_rqst *rqstp, struct nfsd4_session *session)
2705{
2706 struct nfsd4_compoundargs *args = rqstp->rq_argp;
2707
2708 return args->opcnt > session->se_fchannel.maxops;
2709}
2710
ae82a8d0
MJ
2711static bool nfsd4_request_too_big(struct svc_rqst *rqstp,
2712 struct nfsd4_session *session)
2713{
2714 struct xdr_buf *xb = &rqstp->rq_arg;
2715
2716 return xb->len > session->se_fchannel.maxreq_sz;
2717}
2718
069b6ad4 2719__be32
b85d4c01 2720nfsd4_sequence(struct svc_rqst *rqstp,
069b6ad4
AA
2721 struct nfsd4_compound_state *cstate,
2722 struct nfsd4_sequence *seq)
2723{
f9bb94c4 2724 struct nfsd4_compoundres *resp = rqstp->rq_resp;
47ee5298 2725 struct xdr_stream *xdr = &resp->xdr;
b85d4c01 2726 struct nfsd4_session *session;
221a6876 2727 struct nfs4_client *clp;
b85d4c01 2728 struct nfsd4_slot *slot;
a663bdd8 2729 struct nfsd4_conn *conn;
57b7b43b 2730 __be32 status;
47ee5298 2731 int buflen;
d4e19e70
TM
2732 struct net *net = SVC_NET(rqstp);
2733 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
b85d4c01 2734
f9bb94c4
AA
2735 if (resp->opcnt != 1)
2736 return nfserr_sequence_pos;
2737
a663bdd8
BF
2738 /*
2739 * Will be either used or freed by nfsd4_sequence_check_conn
2740 * below.
2741 */
2742 conn = alloc_conn(rqstp, NFS4_CDFC4_FORE);
2743 if (!conn)
2744 return nfserr_jukebox;
2745
c9a49628 2746 spin_lock(&nn->client_lock);
d4e19e70 2747 session = find_in_sessionid_hashtbl(&seq->sessionid, net, &status);
b85d4c01 2748 if (!session)
221a6876
BF
2749 goto out_no_session;
2750 clp = session->se_client;
b85d4c01 2751
868b89c3
MJ
2752 status = nfserr_too_many_ops;
2753 if (nfsd4_session_too_many_ops(rqstp, session))
66b2b9b2 2754 goto out_put_session;
868b89c3 2755
ae82a8d0
MJ
2756 status = nfserr_req_too_big;
2757 if (nfsd4_request_too_big(rqstp, session))
66b2b9b2 2758 goto out_put_session;
ae82a8d0 2759
b85d4c01 2760 status = nfserr_badslot;
6c18ba9f 2761 if (seq->slotid >= session->se_fchannel.maxreqs)
66b2b9b2 2762 goto out_put_session;
b85d4c01 2763
557ce264 2764 slot = session->se_slots[seq->slotid];
b85d4c01
BH
2765 dprintk("%s: slotid %d\n", __func__, seq->slotid);
2766
a8dfdaeb
AA
2767 /* We do not negotiate the number of slots yet, so set the
2768 * maxslots to the session maxreqs which is used to encode
2769 * sr_highest_slotid and the sr_target_slot id to maxslots */
2770 seq->maxslots = session->se_fchannel.maxreqs;
2771
73e79482
BF
2772 status = check_slot_seqid(seq->seqid, slot->sl_seqid,
2773 slot->sl_flags & NFSD4_SLOT_INUSE);
b85d4c01 2774 if (status == nfserr_replay_cache) {
bf5c43c8
BF
2775 status = nfserr_seq_misordered;
2776 if (!(slot->sl_flags & NFSD4_SLOT_INITIALIZED))
66b2b9b2 2777 goto out_put_session;
b85d4c01
BH
2778 cstate->slot = slot;
2779 cstate->session = session;
4b24ca7d 2780 cstate->clp = clp;
da3846a2 2781 /* Return the cached reply status and set cstate->status
557ce264 2782 * for nfsd4_proc_compound processing */
bf864a31 2783 status = nfsd4_replay_cache_entry(resp, seq);
da3846a2 2784 cstate->status = nfserr_replay_cache;
aaf84eb9 2785 goto out;
b85d4c01
BH
2786 }
2787 if (status)
66b2b9b2 2788 goto out_put_session;
b85d4c01 2789
57266a6e 2790 status = nfsd4_sequence_check_conn(conn, session);
a663bdd8 2791 conn = NULL;
57266a6e
BF
2792 if (status)
2793 goto out_put_session;
328ead28 2794
47ee5298
BF
2795 buflen = (seq->cachethis) ?
2796 session->se_fchannel.maxresp_cached :
2797 session->se_fchannel.maxresp_sz;
2798 status = (seq->cachethis) ? nfserr_rep_too_big_to_cache :
2799 nfserr_rep_too_big;
a5cddc88 2800 if (xdr_restrict_buflen(xdr, buflen - rqstp->rq_auth_slack))
47ee5298 2801 goto out_put_session;
32aaa62e 2802 svc_reserve(rqstp, buflen);
47ee5298
BF
2803
2804 status = nfs_ok;
b85d4c01 2805 /* Success! bump slot seqid */
b85d4c01 2806 slot->sl_seqid = seq->seqid;
bf5c43c8 2807 slot->sl_flags |= NFSD4_SLOT_INUSE;
73e79482
BF
2808 if (seq->cachethis)
2809 slot->sl_flags |= NFSD4_SLOT_CACHETHIS;
bf5c43c8
BF
2810 else
2811 slot->sl_flags &= ~NFSD4_SLOT_CACHETHIS;
b85d4c01
BH
2812
2813 cstate->slot = slot;
2814 cstate->session = session;
4b24ca7d 2815 cstate->clp = clp;
b85d4c01 2816
b85d4c01 2817out:
221a6876
BF
2818 switch (clp->cl_cb_state) {
2819 case NFSD4_CB_DOWN:
2820 seq->status_flags = SEQ4_STATUS_CB_PATH_DOWN;
2821 break;
2822 case NFSD4_CB_FAULT:
2823 seq->status_flags = SEQ4_STATUS_BACKCHANNEL_FAULT;
2824 break;
2825 default:
2826 seq->status_flags = 0;
aaf84eb9 2827 }
3bd64a5b
BF
2828 if (!list_empty(&clp->cl_revoked))
2829 seq->status_flags |= SEQ4_STATUS_RECALLABLE_STATE_REVOKED;
221a6876 2830out_no_session:
3f42d2c4
KM
2831 if (conn)
2832 free_conn(conn);
c9a49628 2833 spin_unlock(&nn->client_lock);
b85d4c01 2834 return status;
66b2b9b2 2835out_put_session:
d4e19e70 2836 nfsd4_put_session_locked(session);
221a6876 2837 goto out_no_session;
069b6ad4
AA
2838}
2839
b607664e
TM
2840void
2841nfsd4_sequence_done(struct nfsd4_compoundres *resp)
2842{
2843 struct nfsd4_compound_state *cs = &resp->cstate;
2844
2845 if (nfsd4_has_session(cs)) {
b607664e
TM
2846 if (cs->status != nfserr_replay_cache) {
2847 nfsd4_store_cache_entry(resp);
2848 cs->slot->sl_flags &= ~NFSD4_SLOT_INUSE;
2849 }
d4e19e70 2850 /* Drop session reference that was taken in nfsd4_sequence() */
b607664e 2851 nfsd4_put_session(cs->session);
4b24ca7d
JL
2852 } else if (cs->clp)
2853 put_client_renew(cs->clp);
b607664e
TM
2854}
2855
345c2842
MJ
2856__be32
2857nfsd4_destroy_clientid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_destroy_clientid *dc)
2858{
6b10ad19
TM
2859 struct nfs4_client *conf, *unconf;
2860 struct nfs4_client *clp = NULL;
57b7b43b 2861 __be32 status = 0;
8daae4dc 2862 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
345c2842 2863
6b10ad19 2864 spin_lock(&nn->client_lock);
0a7ec377 2865 unconf = find_unconfirmed_client(&dc->clientid, true, nn);
8daae4dc 2866 conf = find_confirmed_client(&dc->clientid, true, nn);
78389046 2867 WARN_ON_ONCE(conf && unconf);
345c2842
MJ
2868
2869 if (conf) {
c0293b01 2870 if (client_has_state(conf)) {
345c2842
MJ
2871 status = nfserr_clientid_busy;
2872 goto out;
2873 }
fd699b8a
JL
2874 status = mark_client_expired_locked(conf);
2875 if (status)
2876 goto out;
6b10ad19 2877 clp = conf;
345c2842
MJ
2878 } else if (unconf)
2879 clp = unconf;
2880 else {
2881 status = nfserr_stale_clientid;
2882 goto out;
2883 }
57266a6e 2884 if (!mach_creds_match(clp, rqstp)) {
6b10ad19 2885 clp = NULL;
57266a6e
BF
2886 status = nfserr_wrong_cred;
2887 goto out;
2888 }
6b10ad19 2889 unhash_client_locked(clp);
345c2842 2890out:
6b10ad19 2891 spin_unlock(&nn->client_lock);
6b10ad19
TM
2892 if (clp)
2893 expire_client(clp);
345c2842
MJ
2894 return status;
2895}
2896
4dc6ec00
BF
2897__be32
2898nfsd4_reclaim_complete(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_reclaim_complete *rc)
2899{
57b7b43b 2900 __be32 status = 0;
bcecf1cc 2901
4dc6ec00
BF
2902 if (rc->rca_one_fs) {
2903 if (!cstate->current_fh.fh_dentry)
2904 return nfserr_nofilehandle;
2905 /*
2906 * We don't take advantage of the rca_one_fs case.
2907 * That's OK, it's optional, we can safely ignore it.
2908 */
2909 return nfs_ok;
2910 }
bcecf1cc 2911
bcecf1cc 2912 status = nfserr_complete_already;
a52d726b
JL
2913 if (test_and_set_bit(NFSD4_CLIENT_RECLAIM_COMPLETE,
2914 &cstate->session->se_client->cl_flags))
bcecf1cc
MJ
2915 goto out;
2916
2917 status = nfserr_stale_clientid;
2918 if (is_client_expired(cstate->session->se_client))
4dc6ec00
BF
2919 /*
2920 * The following error isn't really legal.
2921 * But we only get here if the client just explicitly
2922 * destroyed the client. Surely it no longer cares what
2923 * error it gets back on an operation for the dead
2924 * client.
2925 */
bcecf1cc
MJ
2926 goto out;
2927
2928 status = nfs_ok;
2a4317c5 2929 nfsd4_client_record_create(cstate->session->se_client);
bcecf1cc 2930out:
bcecf1cc 2931 return status;
4dc6ec00
BF
2932}
2933
b37ad28b 2934__be32
b591480b
BF
2935nfsd4_setclientid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
2936 struct nfsd4_setclientid *setclid)
1da177e4 2937{
a084daf5 2938 struct xdr_netobj clname = setclid->se_name;
1da177e4 2939 nfs4_verifier clverifier = setclid->se_verf;
3dbacee6
TM
2940 struct nfs4_client *conf, *new;
2941 struct nfs4_client *unconf = NULL;
b37ad28b 2942 __be32 status;
c212cecf
SK
2943 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
2944
5cc40fd7
TM
2945 new = create_client(clname, rqstp, &clverifier);
2946 if (new == NULL)
2947 return nfserr_jukebox;
63db4632 2948 /* Cases below refer to rfc 3530 section 14.2.33: */
3dbacee6 2949 spin_lock(&nn->client_lock);
382a62e7 2950 conf = find_confirmed_client_by_name(&clname, nn);
28ce6054 2951 if (conf) {
63db4632 2952 /* case 0: */
1da177e4 2953 status = nfserr_clid_inuse;
e203d506
BF
2954 if (clp_used_exchangeid(conf))
2955 goto out;
026722c2 2956 if (!same_creds(&conf->cl_cred, &rqstp->rq_cred)) {
363168b4
JL
2957 char addr_str[INET6_ADDRSTRLEN];
2958 rpc_ntop((struct sockaddr *) &conf->cl_addr, addr_str,
2959 sizeof(addr_str));
2960 dprintk("NFSD: setclientid: string in use by client "
2961 "at %s\n", addr_str);
1da177e4
LT
2962 goto out;
2963 }
1da177e4 2964 }
a99454aa 2965 unconf = find_unconfirmed_client_by_name(&clname, nn);
8f930711 2966 if (unconf)
3dbacee6 2967 unhash_client_locked(unconf);
34b232bb 2968 if (conf && same_verf(&conf->cl_verifier, &clverifier))
63db4632 2969 /* case 1: probable callback update */
1da177e4 2970 copy_clid(new, conf);
34b232bb 2971 else /* case 4 (new client) or cases 2, 3 (client reboot): */
c212cecf 2972 gen_clid(new, nn);
8323c3b2 2973 new->cl_minorversion = 0;
6f3d772f 2974 gen_callback(new, setclid, rqstp);
ac55fdc4 2975 add_to_unconfirmed(new);
1da177e4
LT
2976 setclid->se_clientid.cl_boot = new->cl_clientid.cl_boot;
2977 setclid->se_clientid.cl_id = new->cl_clientid.cl_id;
2978 memcpy(setclid->se_confirm.data, new->cl_confirm.data, sizeof(setclid->se_confirm.data));
5cc40fd7 2979 new = NULL;
1da177e4
LT
2980 status = nfs_ok;
2981out:
3dbacee6 2982 spin_unlock(&nn->client_lock);
5cc40fd7
TM
2983 if (new)
2984 free_client(new);
3dbacee6
TM
2985 if (unconf)
2986 expire_client(unconf);
1da177e4
LT
2987 return status;
2988}
2989
2990
b37ad28b 2991__be32
b591480b
BF
2992nfsd4_setclientid_confirm(struct svc_rqst *rqstp,
2993 struct nfsd4_compound_state *cstate,
2994 struct nfsd4_setclientid_confirm *setclientid_confirm)
1da177e4 2995{
21ab45a4 2996 struct nfs4_client *conf, *unconf;
d20c11d8 2997 struct nfs4_client *old = NULL;
1da177e4
LT
2998 nfs4_verifier confirm = setclientid_confirm->sc_confirm;
2999 clientid_t * clid = &setclientid_confirm->sc_clientid;
b37ad28b 3000 __be32 status;
7f2210fa 3001 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
1da177e4 3002
2c142baa 3003 if (STALE_CLIENTID(clid, nn))
1da177e4 3004 return nfserr_stale_clientid;
21ab45a4 3005
d20c11d8 3006 spin_lock(&nn->client_lock);
8daae4dc 3007 conf = find_confirmed_client(clid, false, nn);
0a7ec377 3008 unconf = find_unconfirmed_client(clid, false, nn);
a186e767 3009 /*
8695b90a
BF
3010 * We try hard to give out unique clientid's, so if we get an
3011 * attempt to confirm the same clientid with a different cred,
3012 * there's a bug somewhere. Let's charitably assume it's our
3013 * bug.
a186e767 3014 */
8695b90a
BF
3015 status = nfserr_serverfault;
3016 if (unconf && !same_creds(&unconf->cl_cred, &rqstp->rq_cred))
3017 goto out;
3018 if (conf && !same_creds(&conf->cl_cred, &rqstp->rq_cred))
3019 goto out;
63db4632 3020 /* cases below refer to rfc 3530 section 14.2.34: */
90d700b7
BF
3021 if (!unconf || !same_verf(&confirm, &unconf->cl_confirm)) {
3022 if (conf && !unconf) /* case 2: probable retransmit */
1da177e4 3023 status = nfs_ok;
90d700b7
BF
3024 else /* case 4: client hasn't noticed we rebooted yet? */
3025 status = nfserr_stale_clientid;
3026 goto out;
3027 }
3028 status = nfs_ok;
3029 if (conf) { /* case 1: callback update */
d20c11d8
JL
3030 old = unconf;
3031 unhash_client_locked(old);
8695b90a 3032 nfsd4_change_callback(conf, &unconf->cl_cb_conn);
90d700b7 3033 } else { /* case 3: normal case; new or rebooted client */
d20c11d8
JL
3034 old = find_confirmed_client_by_name(&unconf->cl_name, nn);
3035 if (old) {
3036 status = mark_client_expired_locked(old);
7abea1e8
JL
3037 if (status) {
3038 old = NULL;
221a6876 3039 goto out;
7abea1e8 3040 }
221a6876 3041 }
8695b90a 3042 move_to_confirmed(unconf);
d20c11d8 3043 conf = unconf;
08e8987c 3044 }
d20c11d8
JL
3045 get_client_locked(conf);
3046 spin_unlock(&nn->client_lock);
3047 nfsd4_probe_callback(conf);
3048 spin_lock(&nn->client_lock);
3049 put_client_renew_locked(conf);
1da177e4 3050out:
d20c11d8
JL
3051 spin_unlock(&nn->client_lock);
3052 if (old)
3053 expire_client(old);
1da177e4
LT
3054 return status;
3055}
3056
32513b40
BF
3057static struct nfs4_file *nfsd4_alloc_file(void)
3058{
3059 return kmem_cache_alloc(file_slab, GFP_KERNEL);
3060}
3061
1da177e4 3062/* OPEN Share state helper functions */
5b095e99
JL
3063static void nfsd4_init_file(struct knfsd_fh *fh, unsigned int hashval,
3064 struct nfs4_file *fp)
1da177e4 3065{
950e0118
TM
3066 lockdep_assert_held(&state_lock);
3067
32513b40 3068 atomic_set(&fp->fi_ref, 1);
1d31a253 3069 spin_lock_init(&fp->fi_lock);
32513b40
BF
3070 INIT_LIST_HEAD(&fp->fi_stateids);
3071 INIT_LIST_HEAD(&fp->fi_delegations);
e2cf80d7 3072 fh_copy_shallow(&fp->fi_fhandle, fh);
0c637be8 3073 fp->fi_deleg_file = NULL;
32513b40 3074 fp->fi_had_conflict = false;
baeb4ff0 3075 fp->fi_share_deny = 0;
32513b40
BF
3076 memset(fp->fi_fds, 0, sizeof(fp->fi_fds));
3077 memset(fp->fi_access, 0, sizeof(fp->fi_access));
5b095e99 3078 hlist_add_head_rcu(&fp->fi_hash, &file_hashtbl[hashval]);
1da177e4
LT
3079}
3080
e8ff2a84 3081void
1da177e4
LT
3082nfsd4_free_slabs(void)
3083{
abf1135b
CH
3084 kmem_cache_destroy(openowner_slab);
3085 kmem_cache_destroy(lockowner_slab);
3086 kmem_cache_destroy(file_slab);
3087 kmem_cache_destroy(stateid_slab);
3088 kmem_cache_destroy(deleg_slab);
e60d4398 3089}
1da177e4 3090
72083396 3091int
e60d4398
N
3092nfsd4_init_slabs(void)
3093{
fe0750e5
BF
3094 openowner_slab = kmem_cache_create("nfsd4_openowners",
3095 sizeof(struct nfs4_openowner), 0, 0, NULL);
3096 if (openowner_slab == NULL)
abf1135b 3097 goto out;
fe0750e5 3098 lockowner_slab = kmem_cache_create("nfsd4_lockowners",
3c40794b 3099 sizeof(struct nfs4_lockowner), 0, 0, NULL);
fe0750e5 3100 if (lockowner_slab == NULL)
abf1135b 3101 goto out_free_openowner_slab;
e60d4398 3102 file_slab = kmem_cache_create("nfsd4_files",
20c2df83 3103 sizeof(struct nfs4_file), 0, 0, NULL);
e60d4398 3104 if (file_slab == NULL)
abf1135b 3105 goto out_free_lockowner_slab;
5ac049ac 3106 stateid_slab = kmem_cache_create("nfsd4_stateids",
dcef0413 3107 sizeof(struct nfs4_ol_stateid), 0, 0, NULL);
5ac049ac 3108 if (stateid_slab == NULL)
abf1135b 3109 goto out_free_file_slab;
5b2d21c1 3110 deleg_slab = kmem_cache_create("nfsd4_delegations",
20c2df83 3111 sizeof(struct nfs4_delegation), 0, 0, NULL);
5b2d21c1 3112 if (deleg_slab == NULL)
abf1135b 3113 goto out_free_stateid_slab;
e60d4398 3114 return 0;
abf1135b
CH
3115
3116out_free_stateid_slab:
3117 kmem_cache_destroy(stateid_slab);
3118out_free_file_slab:
3119 kmem_cache_destroy(file_slab);
3120out_free_lockowner_slab:
3121 kmem_cache_destroy(lockowner_slab);
3122out_free_openowner_slab:
3123 kmem_cache_destroy(openowner_slab);
3124out:
e60d4398
N
3125 dprintk("nfsd4: out of memory while initializing nfsv4\n");
3126 return -ENOMEM;
1da177e4
LT
3127}
3128
ff194bd9 3129static void init_nfs4_replay(struct nfs4_replay *rp)
1da177e4 3130{
ff194bd9
BF
3131 rp->rp_status = nfserr_serverfault;
3132 rp->rp_buflen = 0;
3133 rp->rp_buf = rp->rp_ibuf;
58fb12e6
JL
3134 mutex_init(&rp->rp_mutex);
3135}
3136
3137static void nfsd4_cstate_assign_replay(struct nfsd4_compound_state *cstate,
3138 struct nfs4_stateowner *so)
3139{
3140 if (!nfsd4_has_session(cstate)) {
3141 mutex_lock(&so->so_replay.rp_mutex);
b5971afa 3142 cstate->replay_owner = nfs4_get_stateowner(so);
58fb12e6
JL
3143 }
3144}
3145
3146void nfsd4_cstate_clear_replay(struct nfsd4_compound_state *cstate)
3147{
3148 struct nfs4_stateowner *so = cstate->replay_owner;
3149
3150 if (so != NULL) {
3151 cstate->replay_owner = NULL;
3152 mutex_unlock(&so->so_replay.rp_mutex);
3153 nfs4_put_stateowner(so);
3154 }
1da177e4
LT
3155}
3156
fe0750e5 3157static inline void *alloc_stateowner(struct kmem_cache *slab, struct xdr_netobj *owner, struct nfs4_client *clp)
ff194bd9 3158{
1da177e4 3159 struct nfs4_stateowner *sop;
1da177e4 3160
fe0750e5 3161 sop = kmem_cache_alloc(slab, GFP_KERNEL);
ff194bd9
BF
3162 if (!sop)
3163 return NULL;
3164
3165 sop->so_owner.data = kmemdup(owner->data, owner->len, GFP_KERNEL);
3166 if (!sop->so_owner.data) {
fe0750e5 3167 kmem_cache_free(slab, sop);
1da177e4 3168 return NULL;
ff194bd9
BF
3169 }
3170 sop->so_owner.len = owner->len;
3171
ea1da636 3172 INIT_LIST_HEAD(&sop->so_stateids);
ff194bd9
BF
3173 sop->so_client = clp;
3174 init_nfs4_replay(&sop->so_replay);
6b180f0b 3175 atomic_set(&sop->so_count, 1);
ff194bd9
BF
3176 return sop;
3177}
3178
fe0750e5 3179static void hash_openowner(struct nfs4_openowner *oo, struct nfs4_client *clp, unsigned int strhashval)
ff194bd9 3180{
d4f0489f 3181 lockdep_assert_held(&clp->cl_lock);
9b531137 3182
d4f0489f
TM
3183 list_add(&oo->oo_owner.so_strhash,
3184 &clp->cl_ownerstr_hashtbl[strhashval]);
fe0750e5 3185 list_add(&oo->oo_perclient, &clp->cl_openowners);
ff194bd9
BF
3186}
3187
8f4b54c5
JL
3188static void nfs4_unhash_openowner(struct nfs4_stateowner *so)
3189{
d4f0489f 3190 unhash_openowner_locked(openowner(so));
8f4b54c5
JL
3191}
3192
6b180f0b
JL
3193static void nfs4_free_openowner(struct nfs4_stateowner *so)
3194{
3195 struct nfs4_openowner *oo = openowner(so);
3196
3197 kmem_cache_free(openowner_slab, oo);
3198}
3199
3200static const struct nfs4_stateowner_operations openowner_ops = {
8f4b54c5
JL
3201 .so_unhash = nfs4_unhash_openowner,
3202 .so_free = nfs4_free_openowner,
6b180f0b
JL
3203};
3204
fe0750e5 3205static struct nfs4_openowner *
13d6f66b 3206alloc_init_open_stateowner(unsigned int strhashval, struct nfsd4_open *open,
db24b3b4
JL
3207 struct nfsd4_compound_state *cstate)
3208{
13d6f66b 3209 struct nfs4_client *clp = cstate->clp;
7ffb5880 3210 struct nfs4_openowner *oo, *ret;
ff194bd9 3211
fe0750e5
BF
3212 oo = alloc_stateowner(openowner_slab, &open->op_owner, clp);
3213 if (!oo)
ff194bd9 3214 return NULL;
6b180f0b 3215 oo->oo_owner.so_ops = &openowner_ops;
fe0750e5
BF
3216 oo->oo_owner.so_is_open_owner = 1;
3217 oo->oo_owner.so_seqid = open->op_seqid;
d3134b10 3218 oo->oo_flags = 0;
db24b3b4
JL
3219 if (nfsd4_has_session(cstate))
3220 oo->oo_flags |= NFS4_OO_CONFIRMED;
fe0750e5 3221 oo->oo_time = 0;
38c387b5 3222 oo->oo_last_closed_stid = NULL;
fe0750e5 3223 INIT_LIST_HEAD(&oo->oo_close_lru);
d4f0489f
TM
3224 spin_lock(&clp->cl_lock);
3225 ret = find_openstateowner_str_locked(strhashval, open, clp);
7ffb5880
TM
3226 if (ret == NULL) {
3227 hash_openowner(oo, clp, strhashval);
3228 ret = oo;
3229 } else
3230 nfs4_free_openowner(&oo->oo_owner);
d4f0489f 3231 spin_unlock(&clp->cl_lock);
fe0750e5 3232 return oo;
1da177e4
LT
3233}
3234
996e0938 3235static void init_open_stateid(struct nfs4_ol_stateid *stp, struct nfs4_file *fp, struct nfsd4_open *open) {
fe0750e5 3236 struct nfs4_openowner *oo = open->op_openowner;
1da177e4 3237
d6f2bc5d 3238 atomic_inc(&stp->st_stid.sc_count);
3abdb607 3239 stp->st_stid.sc_type = NFS4_OPEN_STID;
3c87b9b7 3240 INIT_LIST_HEAD(&stp->st_locks);
b5971afa 3241 stp->st_stateowner = nfs4_get_stateowner(&oo->oo_owner);
13cd2184 3242 get_nfs4_file(fp);
11b9164a 3243 stp->st_stid.sc_file = fp;
1da177e4
LT
3244 stp->st_access_bmap = 0;
3245 stp->st_deny_bmap = 0;
4c4cd222 3246 stp->st_openstp = NULL;
1c755dc1
JL
3247 spin_lock(&oo->oo_owner.so_client->cl_lock);
3248 list_add(&stp->st_perstateowner, &oo->oo_owner.so_stateids);
1d31a253
TM
3249 spin_lock(&fp->fi_lock);
3250 list_add(&stp->st_perfile, &fp->fi_stateids);
3251 spin_unlock(&fp->fi_lock);
1c755dc1 3252 spin_unlock(&oo->oo_owner.so_client->cl_lock);
1da177e4
LT
3253}
3254
d3134b10
JL
3255/*
3256 * In the 4.0 case we need to keep the owners around a little while to handle
3257 * CLOSE replay. We still do need to release any file access that is held by
3258 * them before returning however.
3259 */
fd39ca9a 3260static void
d3134b10 3261move_to_close_lru(struct nfs4_ol_stateid *s, struct net *net)
1da177e4 3262{
217526e7 3263 struct nfs4_ol_stateid *last;
d3134b10
JL
3264 struct nfs4_openowner *oo = openowner(s->st_stateowner);
3265 struct nfsd_net *nn = net_generic(s->st_stid.sc_client->net,
3266 nfsd_net_id);
73758fed 3267
fe0750e5 3268 dprintk("NFSD: move_to_close_lru nfs4_openowner %p\n", oo);
1da177e4 3269
b401be22
JL
3270 /*
3271 * We know that we hold one reference via nfsd4_close, and another
3272 * "persistent" reference for the client. If the refcount is higher
3273 * than 2, then there are still calls in progress that are using this
3274 * stateid. We can't put the sc_file reference until they are finished.
3275 * Wait for the refcount to drop to 2. Since it has been unhashed,
3276 * there should be no danger of the refcount going back up again at
3277 * this point.
3278 */
3279 wait_event(close_wq, atomic_read(&s->st_stid.sc_count) == 2);
3280
d3134b10
JL
3281 release_all_access(s);
3282 if (s->st_stid.sc_file) {
3283 put_nfs4_file(s->st_stid.sc_file);
3284 s->st_stid.sc_file = NULL;
3285 }
217526e7
JL
3286
3287 spin_lock(&nn->client_lock);
3288 last = oo->oo_last_closed_stid;
d3134b10 3289 oo->oo_last_closed_stid = s;
73758fed 3290 list_move_tail(&oo->oo_close_lru, &nn->close_lru);
fe0750e5 3291 oo->oo_time = get_seconds();
217526e7
JL
3292 spin_unlock(&nn->client_lock);
3293 if (last)
3294 nfs4_put_stid(&last->st_stid);
1da177e4
LT
3295}
3296
1da177e4
LT
3297/* search file_hashtbl[] for file */
3298static struct nfs4_file *
5b095e99 3299find_file_locked(struct knfsd_fh *fh, unsigned int hashval)
1da177e4 3300{
1da177e4
LT
3301 struct nfs4_file *fp;
3302
5b095e99 3303 hlist_for_each_entry_rcu(fp, &file_hashtbl[hashval], fi_hash) {
ca943217 3304 if (nfsd_fh_match(&fp->fi_fhandle, fh)) {
5b095e99
JL
3305 if (atomic_inc_not_zero(&fp->fi_ref))
3306 return fp;
13cd2184 3307 }
1da177e4
LT
3308 }
3309 return NULL;
3310}
3311
950e0118 3312static struct nfs4_file *
ca943217 3313find_file(struct knfsd_fh *fh)
950e0118
TM
3314{
3315 struct nfs4_file *fp;
5b095e99 3316 unsigned int hashval = file_hashval(fh);
950e0118 3317
5b095e99
JL
3318 rcu_read_lock();
3319 fp = find_file_locked(fh, hashval);
3320 rcu_read_unlock();
950e0118
TM
3321 return fp;
3322}
3323
3324static struct nfs4_file *
f9c00c3a 3325find_or_add_file(struct nfs4_file *new, struct knfsd_fh *fh)
950e0118
TM
3326{
3327 struct nfs4_file *fp;
5b095e99
JL
3328 unsigned int hashval = file_hashval(fh);
3329
3330 rcu_read_lock();
3331 fp = find_file_locked(fh, hashval);
3332 rcu_read_unlock();
3333 if (fp)
3334 return fp;
950e0118
TM
3335
3336 spin_lock(&state_lock);
5b095e99
JL
3337 fp = find_file_locked(fh, hashval);
3338 if (likely(fp == NULL)) {
3339 nfsd4_init_file(fh, hashval, new);
950e0118
TM
3340 fp = new;
3341 }
3342 spin_unlock(&state_lock);
3343
3344 return fp;
3345}
3346
1da177e4
LT
3347/*
3348 * Called to check deny when READ with all zero stateid or
3349 * WRITE with all zero or all one stateid
3350 */
b37ad28b 3351static __be32
1da177e4
LT
3352nfs4_share_conflict(struct svc_fh *current_fh, unsigned int deny_type)
3353{
1da177e4 3354 struct nfs4_file *fp;
baeb4ff0 3355 __be32 ret = nfs_ok;
1da177e4 3356
ca943217 3357 fp = find_file(&current_fh->fh_handle);
13cd2184 3358 if (!fp)
baeb4ff0
JL
3359 return ret;
3360 /* Check for conflicting share reservations */
1d31a253 3361 spin_lock(&fp->fi_lock);
baeb4ff0
JL
3362 if (fp->fi_share_deny & deny_type)
3363 ret = nfserr_locked;
1d31a253 3364 spin_unlock(&fp->fi_lock);
13cd2184
N
3365 put_nfs4_file(fp);
3366 return ret;
1da177e4
LT
3367}
3368
0162ac2b 3369static void nfsd4_cb_recall_prepare(struct nfsd4_callback *cb)
1da177e4 3370{
0162ac2b 3371 struct nfs4_delegation *dp = cb_to_delegation(cb);
11b9164a
TM
3372 struct nfsd_net *nn = net_generic(dp->dl_stid.sc_client->net,
3373 nfsd_net_id);
e8c69d17 3374
11b9164a 3375 block_delegations(&dp->dl_stid.sc_file->fi_fhandle);
f54fe962 3376
dff1399f 3377 /*
f54fe962
JL
3378 * We can't do this in nfsd_break_deleg_cb because it is
3379 * already holding inode->i_lock.
3380 *
dff1399f
JL
3381 * If the dl_time != 0, then we know that it has already been
3382 * queued for a lease break. Don't queue it again.
3383 */
f54fe962 3384 spin_lock(&state_lock);
dff1399f 3385 if (dp->dl_time == 0) {
dff1399f 3386 dp->dl_time = get_seconds();
02e1215f 3387 list_add_tail(&dp->dl_recall_lru, &nn->del_recall_lru);
dff1399f 3388 }
02e1215f
JL
3389 spin_unlock(&state_lock);
3390}
1da177e4 3391
0162ac2b
CH
3392static int nfsd4_cb_recall_done(struct nfsd4_callback *cb,
3393 struct rpc_task *task)
3394{
3395 struct nfs4_delegation *dp = cb_to_delegation(cb);
3396
3397 switch (task->tk_status) {
3398 case 0:
3399 return 1;
3400 case -EBADHANDLE:
3401 case -NFS4ERR_BAD_STATEID:
3402 /*
3403 * Race: client probably got cb_recall before open reply
3404 * granting delegation.
3405 */
3406 if (dp->dl_retries--) {
3407 rpc_delay(task, 2 * HZ);
3408 return 0;
3409 }
3410 /*FALLTHRU*/
3411 default:
3412 return -1;
3413 }
3414}
3415
3416static void nfsd4_cb_recall_release(struct nfsd4_callback *cb)
3417{
3418 struct nfs4_delegation *dp = cb_to_delegation(cb);
3419
3420 nfs4_put_stid(&dp->dl_stid);
3421}
3422
3423static struct nfsd4_callback_ops nfsd4_cb_recall_ops = {
3424 .prepare = nfsd4_cb_recall_prepare,
3425 .done = nfsd4_cb_recall_done,
3426 .release = nfsd4_cb_recall_release,
3427};
3428
02e1215f
JL
3429static void nfsd_break_one_deleg(struct nfs4_delegation *dp)
3430{
3431 /*
3432 * We're assuming the state code never drops its reference
3433 * without first removing the lease. Since we're in this lease
3434 * callback (and since the lease code is serialized by the kernel
3435 * lock) we know the server hasn't removed the lease yet, we know
3436 * it's safe to take a reference.
3437 */
72c0b0fb 3438 atomic_inc(&dp->dl_stid.sc_count);
f0b5de1b 3439 nfsd4_run_cb(&dp->dl_recall);
6b57d9c8
BF
3440}
3441
1c8c601a 3442/* Called from break_lease() with i_lock held. */
4d01b7f5
JL
3443static bool
3444nfsd_break_deleg_cb(struct file_lock *fl)
6b57d9c8 3445{
4d01b7f5 3446 bool ret = false;
acfdf5c3
BF
3447 struct nfs4_file *fp = (struct nfs4_file *)fl->fl_owner;
3448 struct nfs4_delegation *dp;
6b57d9c8 3449
7fa10cd1
BF
3450 if (!fp) {
3451 WARN(1, "(%p)->fl_owner NULL\n", fl);
4d01b7f5 3452 return ret;
7fa10cd1
BF
3453 }
3454 if (fp->fi_had_conflict) {
3455 WARN(1, "duplicate break on %p\n", fp);
4d01b7f5 3456 return ret;
7fa10cd1 3457 }
0272e1fd
BF
3458 /*
3459 * We don't want the locks code to timeout the lease for us;
acfdf5c3 3460 * we'll remove it ourself if a delegation isn't returned
6b57d9c8 3461 * in time:
0272e1fd
BF
3462 */
3463 fl->fl_break_time = 0;
1da177e4 3464
02e1215f 3465 spin_lock(&fp->fi_lock);
417c6629
JL
3466 fp->fi_had_conflict = true;
3467 /*
4d01b7f5
JL
3468 * If there are no delegations on the list, then return true
3469 * so that the lease code will go ahead and delete it.
417c6629
JL
3470 */
3471 if (list_empty(&fp->fi_delegations))
4d01b7f5 3472 ret = true;
417c6629
JL
3473 else
3474 list_for_each_entry(dp, &fp->fi_delegations, dl_perfile)
3475 nfsd_break_one_deleg(dp);
02e1215f 3476 spin_unlock(&fp->fi_lock);
4d01b7f5 3477 return ret;
1da177e4
LT
3478}
3479
c45198ed
JL
3480static int
3481nfsd_change_deleg_cb(struct file_lock **onlist, int arg, struct list_head *dispose)
1da177e4
LT
3482{
3483 if (arg & F_UNLCK)
c45198ed 3484 return lease_modify(onlist, arg, dispose);
1da177e4
LT
3485 else
3486 return -EAGAIN;
3487}
3488
7b021967 3489static const struct lock_manager_operations nfsd_lease_mng_ops = {
8fb47a4f
BF
3490 .lm_break = nfsd_break_deleg_cb,
3491 .lm_change = nfsd_change_deleg_cb,
1da177e4
LT
3492};
3493
7a8711c9
BF
3494static __be32 nfsd4_check_seqid(struct nfsd4_compound_state *cstate, struct nfs4_stateowner *so, u32 seqid)
3495{
3496 if (nfsd4_has_session(cstate))
3497 return nfs_ok;
3498 if (seqid == so->so_seqid - 1)
3499 return nfserr_replay_me;
3500 if (seqid == so->so_seqid)
3501 return nfs_ok;
3502 return nfserr_bad_seqid;
3503}
1da177e4 3504
4b24ca7d
JL
3505static __be32 lookup_clientid(clientid_t *clid,
3506 struct nfsd4_compound_state *cstate,
3507 struct nfsd_net *nn)
3508{
3509 struct nfs4_client *found;
3510
3511 if (cstate->clp) {
3512 found = cstate->clp;
3513 if (!same_clid(&found->cl_clientid, clid))
3514 return nfserr_stale_clientid;
3515 return nfs_ok;
3516 }
3517
3518 if (STALE_CLIENTID(clid, nn))
3519 return nfserr_stale_clientid;
3520
3521 /*
3522 * For v4.1+ we get the client in the SEQUENCE op. If we don't have one
3523 * cached already then we know this is for is for v4.0 and "sessions"
3524 * will be false.
3525 */
3526 WARN_ON_ONCE(cstate->session);
3e339f96 3527 spin_lock(&nn->client_lock);
4b24ca7d 3528 found = find_confirmed_client(clid, false, nn);
3e339f96
TM
3529 if (!found) {
3530 spin_unlock(&nn->client_lock);
4b24ca7d 3531 return nfserr_expired;
3e339f96
TM
3532 }
3533 atomic_inc(&found->cl_refcount);
3534 spin_unlock(&nn->client_lock);
4b24ca7d
JL
3535
3536 /* Cache the nfs4_client in cstate! */
3537 cstate->clp = found;
4b24ca7d
JL
3538 return nfs_ok;
3539}
3540
b37ad28b 3541__be32
6668958f 3542nfsd4_process_open1(struct nfsd4_compound_state *cstate,
3320fef1 3543 struct nfsd4_open *open, struct nfsd_net *nn)
1da177e4 3544{
1da177e4
LT
3545 clientid_t *clientid = &open->op_clientid;
3546 struct nfs4_client *clp = NULL;
3547 unsigned int strhashval;
fe0750e5 3548 struct nfs4_openowner *oo = NULL;
4cdc951b 3549 __be32 status;
1da177e4 3550
2c142baa 3551 if (STALE_CLIENTID(&open->op_clientid, nn))
1da177e4 3552 return nfserr_stale_clientid;
32513b40
BF
3553 /*
3554 * In case we need it later, after we've already created the
3555 * file and don't want to risk a further failure:
3556 */
3557 open->op_file = nfsd4_alloc_file();
3558 if (open->op_file == NULL)
3559 return nfserr_jukebox;
1da177e4 3560
2d91e895
TM
3561 status = lookup_clientid(clientid, cstate, nn);
3562 if (status)
3563 return status;
3564 clp = cstate->clp;
3565
d4f0489f
TM
3566 strhashval = ownerstr_hashval(&open->op_owner);
3567 oo = find_openstateowner_str(strhashval, open, clp);
fe0750e5
BF
3568 open->op_openowner = oo;
3569 if (!oo) {
bcf130f9 3570 goto new_owner;
1da177e4 3571 }
dad1c067 3572 if (!(oo->oo_flags & NFS4_OO_CONFIRMED)) {
0f442aa2 3573 /* Replace unconfirmed owners without checking for replay. */
fe0750e5
BF
3574 release_openowner(oo);
3575 open->op_openowner = NULL;
bcf130f9 3576 goto new_owner;
0f442aa2 3577 }
4cdc951b
BF
3578 status = nfsd4_check_seqid(cstate, &oo->oo_owner, open->op_seqid);
3579 if (status)
3580 return status;
4cdc951b 3581 goto alloc_stateid;
bcf130f9 3582new_owner:
13d6f66b 3583 oo = alloc_init_open_stateowner(strhashval, open, cstate);
bcf130f9
BF
3584 if (oo == NULL)
3585 return nfserr_jukebox;
3586 open->op_openowner = oo;
4cdc951b 3587alloc_stateid:
b49e084d 3588 open->op_stp = nfs4_alloc_open_stateid(clp);
4cdc951b
BF
3589 if (!open->op_stp)
3590 return nfserr_jukebox;
0f442aa2 3591 return nfs_ok;
1da177e4
LT
3592}
3593
b37ad28b 3594static inline __be32
4a6e43e6
N
3595nfs4_check_delegmode(struct nfs4_delegation *dp, int flags)
3596{
3597 if ((flags & WR_STATE) && (dp->dl_type == NFS4_OPEN_DELEGATE_READ))
3598 return nfserr_openmode;
3599 else
3600 return nfs_ok;
3601}
3602
f459e453 3603static int share_access_to_flags(u32 share_access)
52f4fb43 3604{
f459e453 3605 return share_access == NFS4_SHARE_ACCESS_READ ? RD_STATE : WR_STATE;
52f4fb43
N
3606}
3607
38c2f4b1 3608static struct nfs4_delegation *find_deleg_stateid(struct nfs4_client *cl, stateid_t *s)
24a0111e 3609{
f459e453 3610 struct nfs4_stid *ret;
24a0111e 3611
38c2f4b1 3612 ret = find_stateid_by_type(cl, s, NFS4_DELEG_STID);
f459e453
BF
3613 if (!ret)
3614 return NULL;
3615 return delegstateid(ret);
24a0111e
BF
3616}
3617
8b289b2c
BF
3618static bool nfsd4_is_deleg_cur(struct nfsd4_open *open)
3619{
3620 return open->op_claim_type == NFS4_OPEN_CLAIM_DELEGATE_CUR ||
3621 open->op_claim_type == NFS4_OPEN_CLAIM_DELEG_CUR_FH;
3622}
3623
b37ad28b 3624static __be32
41d22663 3625nfs4_check_deleg(struct nfs4_client *cl, struct nfsd4_open *open,
567d9829
N
3626 struct nfs4_delegation **dp)
3627{
3628 int flags;
b37ad28b 3629 __be32 status = nfserr_bad_stateid;
dcd94cc2 3630 struct nfs4_delegation *deleg;
567d9829 3631
dcd94cc2
TM
3632 deleg = find_deleg_stateid(cl, &open->op_delegate_stateid);
3633 if (deleg == NULL)
c44c5eeb 3634 goto out;
24a0111e 3635 flags = share_access_to_flags(open->op_share_access);
dcd94cc2
TM
3636 status = nfs4_check_delegmode(deleg, flags);
3637 if (status) {
3638 nfs4_put_stid(&deleg->dl_stid);
3639 goto out;
3640 }
3641 *dp = deleg;
c44c5eeb 3642out:
8b289b2c 3643 if (!nfsd4_is_deleg_cur(open))
c44c5eeb
N
3644 return nfs_ok;
3645 if (status)
3646 return status;
dad1c067 3647 open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED;
c44c5eeb 3648 return nfs_ok;
567d9829
N
3649}
3650
a46cb7f2
JL
3651static struct nfs4_ol_stateid *
3652nfsd4_find_existing_open(struct nfs4_file *fp, struct nfsd4_open *open)
1da177e4 3653{
a46cb7f2 3654 struct nfs4_ol_stateid *local, *ret = NULL;
fe0750e5 3655 struct nfs4_openowner *oo = open->op_openowner;
1da177e4 3656
1d31a253 3657 spin_lock(&fp->fi_lock);
8beefa24 3658 list_for_each_entry(local, &fp->fi_stateids, st_perfile) {
1da177e4
LT
3659 /* ignore lock owners */
3660 if (local->st_stateowner->so_is_open_owner == 0)
3661 continue;
baeb4ff0 3662 if (local->st_stateowner == &oo->oo_owner) {
a46cb7f2 3663 ret = local;
d6f2bc5d 3664 atomic_inc(&ret->st_stid.sc_count);
baeb4ff0 3665 break;
1d31a253 3666 }
1da177e4 3667 }
1d31a253 3668 spin_unlock(&fp->fi_lock);
a46cb7f2 3669 return ret;
1da177e4
LT
3670}
3671
21fb4016
BF
3672static inline int nfs4_access_to_access(u32 nfs4_access)
3673{
3674 int flags = 0;
3675
3676 if (nfs4_access & NFS4_SHARE_ACCESS_READ)
3677 flags |= NFSD_MAY_READ;
3678 if (nfs4_access & NFS4_SHARE_ACCESS_WRITE)
3679 flags |= NFSD_MAY_WRITE;
3680 return flags;
3681}
3682
7e6a72e5
CH
3683static inline __be32
3684nfsd4_truncate(struct svc_rqst *rqstp, struct svc_fh *fh,
3685 struct nfsd4_open *open)
3686{
3687 struct iattr iattr = {
3688 .ia_valid = ATTR_SIZE,
3689 .ia_size = 0,
3690 };
3691 if (!open->op_truncate)
3692 return 0;
3693 if (!(open->op_share_access & NFS4_SHARE_ACCESS_WRITE))
3694 return nfserr_inval;
3695 return nfsd_setattr(rqstp, fh, &iattr, 0, (time_t)0);
3696}
3697
0c12eaff 3698static __be32 nfs4_get_vfs_file(struct svc_rqst *rqstp, struct nfs4_file *fp,
6eb3a1d0
JL
3699 struct svc_fh *cur_fh, struct nfs4_ol_stateid *stp,
3700 struct nfsd4_open *open)
f9d7562f 3701{
de18643d 3702 struct file *filp = NULL;
f9d7562f 3703 __be32 status;
0c12eaff
CB
3704 int oflag = nfs4_access_to_omode(open->op_share_access);
3705 int access = nfs4_access_to_access(open->op_share_access);
baeb4ff0 3706 unsigned char old_access_bmap, old_deny_bmap;
0c12eaff 3707
de18643d 3708 spin_lock(&fp->fi_lock);
baeb4ff0
JL
3709
3710 /*
3711 * Are we trying to set a deny mode that would conflict with
3712 * current access?
3713 */
3714 status = nfs4_file_check_deny(fp, open->op_share_deny);
3715 if (status != nfs_ok) {
3716 spin_unlock(&fp->fi_lock);
3717 goto out;
3718 }
3719
3720 /* set access to the file */
3721 status = nfs4_file_get_access(fp, open->op_share_access);
3722 if (status != nfs_ok) {
3723 spin_unlock(&fp->fi_lock);
3724 goto out;
3725 }
3726
3727 /* Set access bits in stateid */
3728 old_access_bmap = stp->st_access_bmap;
3729 set_access(open->op_share_access, stp);
3730
3731 /* Set new deny mask */
3732 old_deny_bmap = stp->st_deny_bmap;
3733 set_deny(open->op_share_deny, stp);
3734 fp->fi_share_deny |= (open->op_share_deny & NFS4_SHARE_DENY_BOTH);
3735
f9d7562f 3736 if (!fp->fi_fds[oflag]) {
de18643d
TM
3737 spin_unlock(&fp->fi_lock);
3738 status = nfsd_open(rqstp, cur_fh, S_IFREG, access, &filp);
f9d7562f 3739 if (status)
baeb4ff0 3740 goto out_put_access;
de18643d
TM
3741 spin_lock(&fp->fi_lock);
3742 if (!fp->fi_fds[oflag]) {
3743 fp->fi_fds[oflag] = filp;
3744 filp = NULL;
3745 }
f9d7562f 3746 }
de18643d
TM
3747 spin_unlock(&fp->fi_lock);
3748 if (filp)
3749 fput(filp);
f9d7562f 3750
7e6a72e5
CH
3751 status = nfsd4_truncate(rqstp, cur_fh, open);
3752 if (status)
3753 goto out_put_access;
7e6a72e5
CH
3754out:
3755 return status;
baeb4ff0
JL
3756out_put_access:
3757 stp->st_access_bmap = old_access_bmap;
3758 nfs4_file_put_access(fp, open->op_share_access);
3759 reset_union_bmap_deny(bmap_to_share_mode(old_deny_bmap), stp);
3760 goto out;
1da177e4
LT
3761}
3762
b37ad28b 3763static __be32
dcef0413 3764nfs4_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 3765{
b37ad28b 3766 __be32 status;
baeb4ff0 3767 unsigned char old_deny_bmap;
1da177e4 3768
6eb3a1d0 3769 if (!test_access(open->op_share_access, stp))
baeb4ff0 3770 return nfs4_get_vfs_file(rqstp, fp, cur_fh, stp, open);
7e6a72e5 3771
baeb4ff0
JL
3772 /* test and set deny mode */
3773 spin_lock(&fp->fi_lock);
3774 status = nfs4_file_check_deny(fp, open->op_share_deny);
3775 if (status == nfs_ok) {
3776 old_deny_bmap = stp->st_deny_bmap;
3777 set_deny(open->op_share_deny, stp);
3778 fp->fi_share_deny |=
3779 (open->op_share_deny & NFS4_SHARE_DENY_BOTH);
3780 }
3781 spin_unlock(&fp->fi_lock);
3782
3783 if (status != nfs_ok)
1da177e4 3784 return status;
1da177e4 3785
baeb4ff0
JL
3786 status = nfsd4_truncate(rqstp, cur_fh, open);
3787 if (status != nfs_ok)
3788 reset_union_bmap_deny(old_deny_bmap, stp);
3789 return status;
3790}
1da177e4 3791
1da177e4 3792static void
1255a8f3 3793nfs4_set_claim_prev(struct nfsd4_open *open, bool has_session)
1da177e4 3794{
dad1c067 3795 open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED;
1da177e4
LT
3796}
3797
14a24e99
BF
3798/* Should we give out recallable state?: */
3799static bool nfsd4_cb_channel_good(struct nfs4_client *clp)
3800{
3801 if (clp->cl_cb_state == NFSD4_CB_UP)
3802 return true;
3803 /*
3804 * In the sessions case, since we don't have to establish a
3805 * separate connection for callbacks, we assume it's OK
3806 * until we hear otherwise:
3807 */
3808 return clp->cl_minorversion && clp->cl_cb_state == NFSD4_CB_UNKNOWN;
3809}
3810
d564fbec 3811static struct file_lock *nfs4_alloc_init_lease(struct nfs4_file *fp, int flag)
22d38c4c
BF
3812{
3813 struct file_lock *fl;
3814
3815 fl = locks_alloc_lock();
3816 if (!fl)
3817 return NULL;
22d38c4c 3818 fl->fl_lmops = &nfsd_lease_mng_ops;
617588d5 3819 fl->fl_flags = FL_DELEG;
22d38c4c
BF
3820 fl->fl_type = flag == NFS4_OPEN_DELEGATE_READ? F_RDLCK: F_WRLCK;
3821 fl->fl_end = OFFSET_MAX;
d564fbec 3822 fl->fl_owner = (fl_owner_t)fp;
22d38c4c 3823 fl->fl_pid = current->tgid;
22d38c4c
BF
3824 return fl;
3825}
3826
99c41515 3827static int nfs4_setlease(struct nfs4_delegation *dp)
edab9782 3828{
11b9164a 3829 struct nfs4_file *fp = dp->dl_stid.sc_file;
415b96c5 3830 struct file_lock *fl, *ret;
417c6629
JL
3831 struct file *filp;
3832 int status = 0;
edab9782 3833
d564fbec 3834 fl = nfs4_alloc_init_lease(fp, NFS4_OPEN_DELEGATE_READ);
edab9782
BF
3835 if (!fl)
3836 return -ENOMEM;
417c6629
JL
3837 filp = find_readable_file(fp);
3838 if (!filp) {
3839 /* We should always have a readable file here */
3840 WARN_ON_ONCE(1);
3841 return -EBADF;
3842 }
3843 fl->fl_file = filp;
415b96c5 3844 ret = fl;
e6f5c789 3845 status = vfs_setlease(filp, fl->fl_type, &fl, NULL);
1c7dd2ff 3846 if (fl)
417c6629 3847 locks_free_lock(fl);
1c7dd2ff 3848 if (status)
417c6629 3849 goto out_fput;
417c6629
JL
3850 spin_lock(&state_lock);
3851 spin_lock(&fp->fi_lock);
3852 /* Did the lease get broken before we took the lock? */
3853 status = -EAGAIN;
3854 if (fp->fi_had_conflict)
3855 goto out_unlock;
3856 /* Race breaker */
0c637be8 3857 if (fp->fi_deleg_file) {
417c6629
JL
3858 status = 0;
3859 atomic_inc(&fp->fi_delegees);
3860 hash_delegation_locked(dp, fp);
3861 goto out_unlock;
3862 }
417c6629 3863 fp->fi_deleg_file = filp;
acfdf5c3 3864 atomic_set(&fp->fi_delegees, 1);
931ee56c 3865 hash_delegation_locked(dp, fp);
417c6629 3866 spin_unlock(&fp->fi_lock);
cdc97505 3867 spin_unlock(&state_lock);
acfdf5c3 3868 return 0;
417c6629
JL
3869out_unlock:
3870 spin_unlock(&fp->fi_lock);
3871 spin_unlock(&state_lock);
3872out_fput:
3873 fput(filp);
e873088f 3874 return status;
acfdf5c3
BF
3875}
3876
0b26693c
JL
3877static struct nfs4_delegation *
3878nfs4_set_delegation(struct nfs4_client *clp, struct svc_fh *fh,
3879 struct nfs4_file *fp)
acfdf5c3 3880{
0b26693c
JL
3881 int status;
3882 struct nfs4_delegation *dp;
417c6629 3883
bf7bd3e9 3884 if (fp->fi_had_conflict)
0b26693c
JL
3885 return ERR_PTR(-EAGAIN);
3886
3887 dp = alloc_init_deleg(clp, fh);
3888 if (!dp)
3889 return ERR_PTR(-ENOMEM);
3890
bf7bd3e9 3891 get_nfs4_file(fp);
417c6629
JL
3892 spin_lock(&state_lock);
3893 spin_lock(&fp->fi_lock);
11b9164a 3894 dp->dl_stid.sc_file = fp;
0c637be8 3895 if (!fp->fi_deleg_file) {
417c6629
JL
3896 spin_unlock(&fp->fi_lock);
3897 spin_unlock(&state_lock);
0b26693c
JL
3898 status = nfs4_setlease(dp);
3899 goto out;
417c6629 3900 }
cbf7a75b 3901 atomic_inc(&fp->fi_delegees);
acfdf5c3 3902 if (fp->fi_had_conflict) {
417c6629
JL
3903 status = -EAGAIN;
3904 goto out_unlock;
acfdf5c3 3905 }
931ee56c 3906 hash_delegation_locked(dp, fp);
0b26693c 3907 status = 0;
417c6629
JL
3908out_unlock:
3909 spin_unlock(&fp->fi_lock);
cdc97505 3910 spin_unlock(&state_lock);
0b26693c
JL
3911out:
3912 if (status) {
6011695d 3913 nfs4_put_stid(&dp->dl_stid);
0b26693c
JL
3914 return ERR_PTR(status);
3915 }
3916 return dp;
edab9782
BF
3917}
3918
4aa8913c
BH
3919static void nfsd4_open_deleg_none_ext(struct nfsd4_open *open, int status)
3920{
3921 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
3922 if (status == -EAGAIN)
3923 open->op_why_no_deleg = WND4_CONTENTION;
3924 else {
3925 open->op_why_no_deleg = WND4_RESOURCE;
3926 switch (open->op_deleg_want) {
3927 case NFS4_SHARE_WANT_READ_DELEG:
3928 case NFS4_SHARE_WANT_WRITE_DELEG:
3929 case NFS4_SHARE_WANT_ANY_DELEG:
3930 break;
3931 case NFS4_SHARE_WANT_CANCEL:
3932 open->op_why_no_deleg = WND4_CANCELLED;
3933 break;
3934 case NFS4_SHARE_WANT_NO_DELEG:
063b0fb9 3935 WARN_ON_ONCE(1);
4aa8913c
BH
3936 }
3937 }
3938}
3939
1da177e4
LT
3940/*
3941 * Attempt to hand out a delegation.
99c41515
BF
3942 *
3943 * Note we don't support write delegations, and won't until the vfs has
3944 * proper support for them.
1da177e4
LT
3945 */
3946static void
4cf59221
JL
3947nfs4_open_delegation(struct svc_fh *fh, struct nfsd4_open *open,
3948 struct nfs4_ol_stateid *stp)
1da177e4
LT
3949{
3950 struct nfs4_delegation *dp;
4cf59221
JL
3951 struct nfs4_openowner *oo = openowner(stp->st_stateowner);
3952 struct nfs4_client *clp = stp->st_stid.sc_client;
14a24e99 3953 int cb_up;
99c41515 3954 int status = 0;
1da177e4 3955
fe0750e5 3956 cb_up = nfsd4_cb_channel_good(oo->oo_owner.so_client);
7b190fec
N
3957 open->op_recall = 0;
3958 switch (open->op_claim_type) {
3959 case NFS4_OPEN_CLAIM_PREVIOUS:
2bf23875 3960 if (!cb_up)
7b190fec 3961 open->op_recall = 1;
99c41515
BF
3962 if (open->op_delegate_type != NFS4_OPEN_DELEGATE_READ)
3963 goto out_no_deleg;
7b190fec
N
3964 break;
3965 case NFS4_OPEN_CLAIM_NULL:
ed47b062 3966 case NFS4_OPEN_CLAIM_FH:
99c41515
BF
3967 /*
3968 * Let's not give out any delegations till everyone's
3969 * had the chance to reclaim theirs....
3970 */
4cf59221 3971 if (locks_in_grace(clp->net))
99c41515 3972 goto out_no_deleg;
dad1c067 3973 if (!cb_up || !(oo->oo_flags & NFS4_OO_CONFIRMED))
99c41515 3974 goto out_no_deleg;
9a0590ae
SD
3975 /*
3976 * Also, if the file was opened for write or
3977 * create, there's a good chance the client's
3978 * about to write to it, resulting in an
3979 * immediate recall (since we don't support
3980 * write delegations):
3981 */
7b190fec 3982 if (open->op_share_access & NFS4_SHARE_ACCESS_WRITE)
99c41515
BF
3983 goto out_no_deleg;
3984 if (open->op_create == NFS4_OPEN_CREATE)
3985 goto out_no_deleg;
7b190fec
N
3986 break;
3987 default:
99c41515 3988 goto out_no_deleg;
7b190fec 3989 }
11b9164a 3990 dp = nfs4_set_delegation(clp, fh, stp->st_stid.sc_file);
0b26693c 3991 if (IS_ERR(dp))
dd239cc0 3992 goto out_no_deleg;
1da177e4 3993
d5477a8d 3994 memcpy(&open->op_delegate_stateid, &dp->dl_stid.sc_stateid, sizeof(dp->dl_stid.sc_stateid));
1da177e4 3995
8c10cbdb 3996 dprintk("NFSD: delegation stateid=" STATEID_FMT "\n",
d5477a8d 3997 STATEID_VAL(&dp->dl_stid.sc_stateid));
99c41515 3998 open->op_delegate_type = NFS4_OPEN_DELEGATE_READ;
67cb1279 3999 nfs4_put_stid(&dp->dl_stid);
dd239cc0 4000 return;
dd239cc0 4001out_no_deleg:
99c41515
BF
4002 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE;
4003 if (open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS &&
d08d32e6 4004 open->op_delegate_type != NFS4_OPEN_DELEGATE_NONE) {
99c41515 4005 dprintk("NFSD: WARNING: refusing delegation reclaim\n");
d08d32e6
BF
4006 open->op_recall = 1;
4007 }
99c41515
BF
4008
4009 /* 4.1 client asking for a delegation? */
4010 if (open->op_deleg_want)
4011 nfsd4_open_deleg_none_ext(open, status);
4012 return;
1da177e4
LT
4013}
4014
e27f49c3
BH
4015static void nfsd4_deleg_xgrade_none_ext(struct nfsd4_open *open,
4016 struct nfs4_delegation *dp)
4017{
4018 if (open->op_deleg_want == NFS4_SHARE_WANT_READ_DELEG &&
4019 dp->dl_type == NFS4_OPEN_DELEGATE_WRITE) {
4020 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
4021 open->op_why_no_deleg = WND4_NOT_SUPP_DOWNGRADE;
4022 } else if (open->op_deleg_want == NFS4_SHARE_WANT_WRITE_DELEG &&
4023 dp->dl_type == NFS4_OPEN_DELEGATE_WRITE) {
4024 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
4025 open->op_why_no_deleg = WND4_NOT_SUPP_UPGRADE;
4026 }
4027 /* Otherwise the client must be confused wanting a delegation
4028 * it already has, therefore we don't return
4029 * NFS4_OPEN_DELEGATE_NONE_EXT and reason.
4030 */
4031}
4032
b37ad28b 4033__be32
1da177e4
LT
4034nfsd4_process_open2(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_open *open)
4035{
6668958f 4036 struct nfsd4_compoundres *resp = rqstp->rq_resp;
38c2f4b1 4037 struct nfs4_client *cl = open->op_openowner->oo_owner.so_client;
1da177e4 4038 struct nfs4_file *fp = NULL;
dcef0413 4039 struct nfs4_ol_stateid *stp = NULL;
567d9829 4040 struct nfs4_delegation *dp = NULL;
b37ad28b 4041 __be32 status;
1da177e4 4042
1da177e4
LT
4043 /*
4044 * Lookup file; if found, lookup stateid and check open request,
4045 * and check for delegations in the process of being recalled.
4046 * If not found, create the nfs4_file struct
4047 */
f9c00c3a 4048 fp = find_or_add_file(open->op_file, &current_fh->fh_handle);
950e0118 4049 if (fp != open->op_file) {
41d22663 4050 status = nfs4_check_deleg(cl, open, &dp);
c44c5eeb
N
4051 if (status)
4052 goto out;
a46cb7f2 4053 stp = nfsd4_find_existing_open(fp, open);
1da177e4 4054 } else {
950e0118 4055 open->op_file = NULL;
c44c5eeb 4056 status = nfserr_bad_stateid;
8b289b2c 4057 if (nfsd4_is_deleg_cur(open))
c44c5eeb 4058 goto out;
3e772463 4059 status = nfserr_jukebox;
1da177e4
LT
4060 }
4061
4062 /*
4063 * OPEN the file, or upgrade an existing OPEN.
4064 * If truncate fails, the OPEN fails.
4065 */
4066 if (stp) {
4067 /* Stateid was found, this is an OPEN upgrade */
f9d7562f 4068 status = nfs4_upgrade_open(rqstp, fp, current_fh, stp, open);
1da177e4
LT
4069 if (status)
4070 goto out;
4071 } else {
4cdc951b
BF
4072 stp = open->op_stp;
4073 open->op_stp = NULL;
996e0938 4074 init_open_stateid(stp, fp, open);
6eb3a1d0
JL
4075 status = nfs4_get_vfs_file(rqstp, fp, current_fh, stp, open);
4076 if (status) {
4077 release_open_stateid(stp);
4078 goto out;
4079 }
1da177e4 4080 }
dcef0413
BF
4081 update_stateid(&stp->st_stid.sc_stateid);
4082 memcpy(&open->op_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
1da177e4 4083
d24433cd 4084 if (nfsd4_has_session(&resp->cstate)) {
d24433cd
BH
4085 if (open->op_deleg_want & NFS4_SHARE_WANT_NO_DELEG) {
4086 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
4087 open->op_why_no_deleg = WND4_NOT_WANTED;
4088 goto nodeleg;
4089 }
4090 }
4091
1da177e4
LT
4092 /*
4093 * Attempt to hand out a delegation. No error return, because the
4094 * OPEN succeeds even if we fail.
4095 */
4cf59221 4096 nfs4_open_delegation(current_fh, open, stp);
d24433cd 4097nodeleg:
1da177e4
LT
4098 status = nfs_ok;
4099
8c10cbdb 4100 dprintk("%s: stateid=" STATEID_FMT "\n", __func__,
dcef0413 4101 STATEID_VAL(&stp->st_stid.sc_stateid));
1da177e4 4102out:
d24433cd
BH
4103 /* 4.1 client trying to upgrade/downgrade delegation? */
4104 if (open->op_delegate_type == NFS4_OPEN_DELEGATE_NONE && dp &&
e27f49c3
BH
4105 open->op_deleg_want)
4106 nfsd4_deleg_xgrade_none_ext(open, dp);
d24433cd 4107
13cd2184
N
4108 if (fp)
4109 put_nfs4_file(fp);
37515177 4110 if (status == 0 && open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS)
1255a8f3 4111 nfs4_set_claim_prev(open, nfsd4_has_session(&resp->cstate));
1da177e4
LT
4112 /*
4113 * To finish the open response, we just need to set the rflags.
4114 */
4115 open->op_rflags = NFS4_OPEN_RESULT_LOCKTYPE_POSIX;
dad1c067 4116 if (!(open->op_openowner->oo_flags & NFS4_OO_CONFIRMED) &&
6668958f 4117 !nfsd4_has_session(&resp->cstate))
1da177e4 4118 open->op_rflags |= NFS4_OPEN_RESULT_CONFIRM;
dcd94cc2
TM
4119 if (dp)
4120 nfs4_put_stid(&dp->dl_stid);
d6f2bc5d
TM
4121 if (stp)
4122 nfs4_put_stid(&stp->st_stid);
1da177e4
LT
4123
4124 return status;
4125}
4126
58fb12e6
JL
4127void nfsd4_cleanup_open_state(struct nfsd4_compound_state *cstate,
4128 struct nfsd4_open *open, __be32 status)
d29b20cd
BF
4129{
4130 if (open->op_openowner) {
d3134b10
JL
4131 struct nfs4_stateowner *so = &open->op_openowner->oo_owner;
4132
4133 nfsd4_cstate_assign_replay(cstate, so);
4134 nfs4_put_stateowner(so);
d29b20cd 4135 }
32513b40 4136 if (open->op_file)
5b095e99 4137 kmem_cache_free(file_slab, open->op_file);
4cdc951b 4138 if (open->op_stp)
6011695d 4139 nfs4_put_stid(&open->op_stp->st_stid);
d29b20cd
BF
4140}
4141
b37ad28b 4142__be32
b591480b
BF
4143nfsd4_renew(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4144 clientid_t *clid)
1da177e4
LT
4145{
4146 struct nfs4_client *clp;
b37ad28b 4147 __be32 status;
7f2210fa 4148 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
1da177e4 4149
1da177e4
LT
4150 dprintk("process_renew(%08x/%08x): starting\n",
4151 clid->cl_boot, clid->cl_id);
4b24ca7d 4152 status = lookup_clientid(clid, cstate, nn);
9b2ef62b 4153 if (status)
1da177e4 4154 goto out;
4b24ca7d 4155 clp = cstate->clp;
1da177e4 4156 status = nfserr_cb_path_down;
ea1da636 4157 if (!list_empty(&clp->cl_delegations)
77a3569d 4158 && clp->cl_cb_state != NFSD4_CB_UP)
1da177e4
LT
4159 goto out;
4160 status = nfs_ok;
4161out:
1da177e4
LT
4162 return status;
4163}
4164
7f5ef2e9 4165void
12760c66 4166nfsd4_end_grace(struct nfsd_net *nn)
a76b4319 4167{
33dcc481 4168 /* do nothing if grace period already ended */
a51c84ed 4169 if (nn->grace_ended)
33dcc481
JL
4170 return;
4171
a76b4319 4172 dprintk("NFSD: end of grace period\n");
a51c84ed 4173 nn->grace_ended = true;
70b28235
BF
4174 /*
4175 * If the server goes down again right now, an NFSv4
4176 * client will still be allowed to reclaim after it comes back up,
4177 * even if it hasn't yet had a chance to reclaim state this time.
4178 *
4179 */
919b8049 4180 nfsd4_record_grace_done(nn);
70b28235
BF
4181 /*
4182 * At this point, NFSv4 clients can still reclaim. But if the
4183 * server crashes, any that have not yet reclaimed will be out
4184 * of luck on the next boot.
4185 *
4186 * (NFSv4.1+ clients are considered to have reclaimed once they
4187 * call RECLAIM_COMPLETE. NFSv4.0 clients are considered to
4188 * have reclaimed after their first OPEN.)
4189 */
5e1533c7 4190 locks_end_grace(&nn->nfsd4_manager);
70b28235
BF
4191 /*
4192 * At this point, and once lockd and/or any other containers
4193 * exit their grace period, further reclaims will fail and
4194 * regular locking can resume.
4195 */
a76b4319
N
4196}
4197
fd39ca9a 4198static time_t
09121281 4199nfs4_laundromat(struct nfsd_net *nn)
1da177e4
LT
4200{
4201 struct nfs4_client *clp;
fe0750e5 4202 struct nfs4_openowner *oo;
1da177e4 4203 struct nfs4_delegation *dp;
217526e7 4204 struct nfs4_ol_stateid *stp;
1da177e4 4205 struct list_head *pos, *next, reaplist;
3d733711 4206 time_t cutoff = get_seconds() - nn->nfsd4_lease;
a832e7ae 4207 time_t t, new_timeo = nn->nfsd4_lease;
1da177e4 4208
1da177e4 4209 dprintk("NFSD: laundromat service - starting\n");
12760c66 4210 nfsd4_end_grace(nn);
36acb66b 4211 INIT_LIST_HEAD(&reaplist);
c9a49628 4212 spin_lock(&nn->client_lock);
5ed58bb2 4213 list_for_each_safe(pos, next, &nn->client_lru) {
1da177e4
LT
4214 clp = list_entry(pos, struct nfs4_client, cl_lru);
4215 if (time_after((unsigned long)clp->cl_time, (unsigned long)cutoff)) {
4216 t = clp->cl_time - cutoff;
a832e7ae 4217 new_timeo = min(new_timeo, t);
1da177e4
LT
4218 break;
4219 }
221a6876 4220 if (mark_client_expired_locked(clp)) {
d7682988
BH
4221 dprintk("NFSD: client in use (clientid %08x)\n",
4222 clp->cl_clientid.cl_id);
4223 continue;
4224 }
4864af97 4225 list_add(&clp->cl_lru, &reaplist);
36acb66b 4226 }
c9a49628 4227 spin_unlock(&nn->client_lock);
36acb66b
BH
4228 list_for_each_safe(pos, next, &reaplist) {
4229 clp = list_entry(pos, struct nfs4_client, cl_lru);
1da177e4
LT
4230 dprintk("NFSD: purging unused client (clientid %08x)\n",
4231 clp->cl_clientid.cl_id);
4864af97 4232 list_del_init(&clp->cl_lru);
1da177e4
LT
4233 expire_client(clp);
4234 }
cdc97505 4235 spin_lock(&state_lock);
e8c69d17 4236 list_for_each_safe(pos, next, &nn->del_recall_lru) {
1da177e4 4237 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
4e37a7c2
SK
4238 if (net_generic(dp->dl_stid.sc_client->net, nfsd_net_id) != nn)
4239 continue;
1da177e4 4240 if (time_after((unsigned long)dp->dl_time, (unsigned long)cutoff)) {
a832e7ae
JL
4241 t = dp->dl_time - cutoff;
4242 new_timeo = min(new_timeo, t);
1da177e4
LT
4243 break;
4244 }
42690676
JL
4245 unhash_delegation_locked(dp);
4246 list_add(&dp->dl_recall_lru, &reaplist);
1da177e4 4247 }
cdc97505 4248 spin_unlock(&state_lock);
2d4a532d
JL
4249 while (!list_empty(&reaplist)) {
4250 dp = list_first_entry(&reaplist, struct nfs4_delegation,
4251 dl_recall_lru);
4252 list_del_init(&dp->dl_recall_lru);
3bd64a5b 4253 revoke_delegation(dp);
1da177e4 4254 }
217526e7
JL
4255
4256 spin_lock(&nn->client_lock);
4257 while (!list_empty(&nn->close_lru)) {
4258 oo = list_first_entry(&nn->close_lru, struct nfs4_openowner,
4259 oo_close_lru);
4260 if (time_after((unsigned long)oo->oo_time,
4261 (unsigned long)cutoff)) {
a832e7ae
JL
4262 t = oo->oo_time - cutoff;
4263 new_timeo = min(new_timeo, t);
1da177e4
LT
4264 break;
4265 }
217526e7
JL
4266 list_del_init(&oo->oo_close_lru);
4267 stp = oo->oo_last_closed_stid;
4268 oo->oo_last_closed_stid = NULL;
4269 spin_unlock(&nn->client_lock);
4270 nfs4_put_stid(&stp->st_stid);
4271 spin_lock(&nn->client_lock);
1da177e4 4272 }
217526e7
JL
4273 spin_unlock(&nn->client_lock);
4274
a832e7ae 4275 new_timeo = max_t(time_t, new_timeo, NFSD_LAUNDROMAT_MINTIMEOUT);
a832e7ae 4276 return new_timeo;
1da177e4
LT
4277}
4278
a254b246
HH
4279static struct workqueue_struct *laundry_wq;
4280static void laundromat_main(struct work_struct *);
a254b246
HH
4281
4282static void
09121281 4283laundromat_main(struct work_struct *laundry)
1da177e4
LT
4284{
4285 time_t t;
09121281
SK
4286 struct delayed_work *dwork = container_of(laundry, struct delayed_work,
4287 work);
4288 struct nfsd_net *nn = container_of(dwork, struct nfsd_net,
4289 laundromat_work);
1da177e4 4290
09121281 4291 t = nfs4_laundromat(nn);
1da177e4 4292 dprintk("NFSD: laundromat_main - sleeping for %ld seconds\n", t);
09121281 4293 queue_delayed_work(laundry_wq, &nn->laundromat_work, t*HZ);
1da177e4
LT
4294}
4295
f7a4d872 4296static inline __be32 nfs4_check_fh(struct svc_fh *fhp, struct nfs4_ol_stateid *stp)
1da177e4 4297{
11b9164a 4298 if (!nfsd_fh_match(&fhp->fh_handle, &stp->st_stid.sc_file->fi_fhandle))
f7a4d872
BF
4299 return nfserr_bad_stateid;
4300 return nfs_ok;
1da177e4
LT
4301}
4302
1da177e4 4303static inline int
82c5ff1b 4304access_permit_read(struct nfs4_ol_stateid *stp)
1da177e4 4305{
82c5ff1b
JL
4306 return test_access(NFS4_SHARE_ACCESS_READ, stp) ||
4307 test_access(NFS4_SHARE_ACCESS_BOTH, stp) ||
4308 test_access(NFS4_SHARE_ACCESS_WRITE, stp);
1da177e4
LT
4309}
4310
4311static inline int
82c5ff1b 4312access_permit_write(struct nfs4_ol_stateid *stp)
1da177e4 4313{
82c5ff1b
JL
4314 return test_access(NFS4_SHARE_ACCESS_WRITE, stp) ||
4315 test_access(NFS4_SHARE_ACCESS_BOTH, stp);
1da177e4
LT
4316}
4317
4318static
dcef0413 4319__be32 nfs4_check_openmode(struct nfs4_ol_stateid *stp, int flags)
1da177e4 4320{
b37ad28b 4321 __be32 status = nfserr_openmode;
1da177e4 4322
02921914
BF
4323 /* For lock stateid's, we test the parent open, not the lock: */
4324 if (stp->st_openstp)
4325 stp = stp->st_openstp;
82c5ff1b 4326 if ((flags & WR_STATE) && !access_permit_write(stp))
1da177e4 4327 goto out;
82c5ff1b 4328 if ((flags & RD_STATE) && !access_permit_read(stp))
1da177e4
LT
4329 goto out;
4330 status = nfs_ok;
4331out:
4332 return status;
4333}
4334
b37ad28b 4335static inline __be32
5ccb0066 4336check_special_stateids(struct net *net, svc_fh *current_fh, stateid_t *stateid, int flags)
1da177e4 4337{
203a8c8e 4338 if (ONE_STATEID(stateid) && (flags & RD_STATE))
1da177e4 4339 return nfs_ok;
5ccb0066 4340 else if (locks_in_grace(net)) {
25985edc 4341 /* Answer in remaining cases depends on existence of
1da177e4
LT
4342 * conflicting state; so we must wait out the grace period. */
4343 return nfserr_grace;
4344 } else if (flags & WR_STATE)
4345 return nfs4_share_conflict(current_fh,
4346 NFS4_SHARE_DENY_WRITE);
4347 else /* (flags & RD_STATE) && ZERO_STATEID(stateid) */
4348 return nfs4_share_conflict(current_fh,
4349 NFS4_SHARE_DENY_READ);
4350}
4351
4352/*
4353 * Allow READ/WRITE during grace period on recovered state only for files
4354 * that are not able to provide mandatory locking.
4355 */
4356static inline int
5ccb0066 4357grace_disallows_io(struct net *net, struct inode *inode)
1da177e4 4358{
5ccb0066 4359 return locks_in_grace(net) && mandatory_lock(inode);
1da177e4
LT
4360}
4361
81b82965
BF
4362/* Returns true iff a is later than b: */
4363static bool stateid_generation_after(stateid_t *a, stateid_t *b)
4364{
1a9357f4 4365 return (s32)(a->si_generation - b->si_generation) > 0;
81b82965
BF
4366}
4367
57b7b43b 4368static __be32 check_stateid_generation(stateid_t *in, stateid_t *ref, bool has_session)
0836f587 4369{
6668958f
AA
4370 /*
4371 * When sessions are used the stateid generation number is ignored
4372 * when it is zero.
4373 */
28dde241 4374 if (has_session && in->si_generation == 0)
81b82965
BF
4375 return nfs_ok;
4376
4377 if (in->si_generation == ref->si_generation)
4378 return nfs_ok;
6668958f 4379
0836f587 4380 /* If the client sends us a stateid from the future, it's buggy: */
81b82965 4381 if (stateid_generation_after(in, ref))
0836f587
BF
4382 return nfserr_bad_stateid;
4383 /*
81b82965
BF
4384 * However, we could see a stateid from the past, even from a
4385 * non-buggy client. For example, if the client sends a lock
4386 * while some IO is outstanding, the lock may bump si_generation
4387 * while the IO is still in flight. The client could avoid that
4388 * situation by waiting for responses on all the IO requests,
4389 * but better performance may result in retrying IO that
4390 * receives an old_stateid error if requests are rarely
4391 * reordered in flight:
0836f587 4392 */
81b82965 4393 return nfserr_old_stateid;
0836f587
BF
4394}
4395
7df302f7 4396static __be32 nfsd4_validate_stateid(struct nfs4_client *cl, stateid_t *stateid)
17456804 4397{
97b7e3b6
BF
4398 struct nfs4_stid *s;
4399 struct nfs4_ol_stateid *ols;
1af71cc8 4400 __be32 status = nfserr_bad_stateid;
17456804 4401
7df302f7 4402 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid))
1af71cc8 4403 return status;
7df302f7
CL
4404 /* Client debugging aid. */
4405 if (!same_clid(&stateid->si_opaque.so_clid, &cl->cl_clientid)) {
4406 char addr_str[INET6_ADDRSTRLEN];
4407 rpc_ntop((struct sockaddr *)&cl->cl_addr, addr_str,
4408 sizeof(addr_str));
4409 pr_warn_ratelimited("NFSD: client %s testing state ID "
4410 "with incorrect client ID\n", addr_str);
1af71cc8 4411 return status;
7df302f7 4412 }
1af71cc8
JL
4413 spin_lock(&cl->cl_lock);
4414 s = find_stateid_locked(cl, stateid);
97b7e3b6 4415 if (!s)
1af71cc8 4416 goto out_unlock;
36279ac1 4417 status = check_stateid_generation(stateid, &s->sc_stateid, 1);
17456804 4418 if (status)
1af71cc8 4419 goto out_unlock;
23340032
BF
4420 switch (s->sc_type) {
4421 case NFS4_DELEG_STID:
1af71cc8
JL
4422 status = nfs_ok;
4423 break;
3bd64a5b 4424 case NFS4_REVOKED_DELEG_STID:
1af71cc8
JL
4425 status = nfserr_deleg_revoked;
4426 break;
23340032
BF
4427 case NFS4_OPEN_STID:
4428 case NFS4_LOCK_STID:
4429 ols = openlockstateid(s);
4430 if (ols->st_stateowner->so_is_open_owner
4431 && !(openowner(ols->st_stateowner)->oo_flags
4432 & NFS4_OO_CONFIRMED))
1af71cc8
JL
4433 status = nfserr_bad_stateid;
4434 else
4435 status = nfs_ok;
4436 break;
23340032
BF
4437 default:
4438 printk("unknown stateid type %x\n", s->sc_type);
b0fc29d6 4439 /* Fallthrough */
23340032 4440 case NFS4_CLOSED_STID:
b0fc29d6 4441 case NFS4_CLOSED_DELEG_STID:
1af71cc8 4442 status = nfserr_bad_stateid;
23340032 4443 }
1af71cc8
JL
4444out_unlock:
4445 spin_unlock(&cl->cl_lock);
4446 return status;
17456804
BS
4447}
4448
2dd6e458
TM
4449static __be32
4450nfsd4_lookup_stateid(struct nfsd4_compound_state *cstate,
4451 stateid_t *stateid, unsigned char typemask,
4452 struct nfs4_stid **s, struct nfsd_net *nn)
38c2f4b1 4453{
0eb6f20a 4454 __be32 status;
38c2f4b1
BF
4455
4456 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid))
4457 return nfserr_bad_stateid;
4b24ca7d 4458 status = lookup_clientid(&stateid->si_opaque.so_clid, cstate, nn);
a8a7c677 4459 if (status == nfserr_stale_clientid) {
4b24ca7d 4460 if (cstate->session)
a8a7c677 4461 return nfserr_bad_stateid;
38c2f4b1 4462 return nfserr_stale_stateid;
a8a7c677 4463 }
0eb6f20a
BF
4464 if (status)
4465 return status;
4b24ca7d 4466 *s = find_stateid_by_type(cstate->clp, stateid, typemask);
38c2f4b1
BF
4467 if (!*s)
4468 return nfserr_bad_stateid;
4469 return nfs_ok;
38c2f4b1
BF
4470}
4471
1da177e4
LT
4472/*
4473* Checks for stateid operations
4474*/
b37ad28b 4475__be32
5ccb0066 4476nfs4_preprocess_stateid_op(struct net *net, struct nfsd4_compound_state *cstate,
dd453dfd 4477 stateid_t *stateid, int flags, struct file **filpp)
1da177e4 4478{
69064a27 4479 struct nfs4_stid *s;
dcef0413 4480 struct nfs4_ol_stateid *stp = NULL;
1da177e4 4481 struct nfs4_delegation *dp = NULL;
dd453dfd 4482 struct svc_fh *current_fh = &cstate->current_fh;
1da177e4 4483 struct inode *ino = current_fh->fh_dentry->d_inode;
3320fef1 4484 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
14bcab1a 4485 struct file *file = NULL;
b37ad28b 4486 __be32 status;
1da177e4 4487
1da177e4
LT
4488 if (filpp)
4489 *filpp = NULL;
4490
5ccb0066 4491 if (grace_disallows_io(net, ino))
1da177e4
LT
4492 return nfserr_grace;
4493
4494 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid))
5ccb0066 4495 return check_special_stateids(net, current_fh, stateid, flags);
1da177e4 4496
2dd6e458 4497 status = nfsd4_lookup_stateid(cstate, stateid,
db24b3b4 4498 NFS4_DELEG_STID|NFS4_OPEN_STID|NFS4_LOCK_STID,
2dd6e458 4499 &s, nn);
38c2f4b1 4500 if (status)
c2d1d6a8 4501 return status;
69064a27
BF
4502 status = check_stateid_generation(stateid, &s->sc_stateid, nfsd4_has_session(cstate));
4503 if (status)
4504 goto out;
f7a4d872
BF
4505 switch (s->sc_type) {
4506 case NFS4_DELEG_STID:
69064a27 4507 dp = delegstateid(s);
dc9bf700
BF
4508 status = nfs4_check_delegmode(dp, flags);
4509 if (status)
4510 goto out;
43b0178e 4511 if (filpp) {
11b9164a 4512 file = dp->dl_stid.sc_file->fi_deleg_file;
14bcab1a 4513 if (!file) {
063b0fb9
BF
4514 WARN_ON_ONCE(1);
4515 status = nfserr_serverfault;
4516 goto out;
4517 }
de18643d 4518 get_file(file);
43b0178e 4519 }
f7a4d872
BF
4520 break;
4521 case NFS4_OPEN_STID:
4522 case NFS4_LOCK_STID:
69064a27 4523 stp = openlockstateid(s);
f7a4d872
BF
4524 status = nfs4_check_fh(current_fh, stp);
4525 if (status)
1da177e4 4526 goto out;
fe0750e5 4527 if (stp->st_stateowner->so_is_open_owner
dad1c067 4528 && !(openowner(stp->st_stateowner)->oo_flags & NFS4_OO_CONFIRMED))
1da177e4 4529 goto out;
a4455be0
BF
4530 status = nfs4_check_openmode(stp, flags);
4531 if (status)
1da177e4 4532 goto out;
f9d7562f 4533 if (filpp) {
11b9164a
TM
4534 struct nfs4_file *fp = stp->st_stid.sc_file;
4535
f9d7562f 4536 if (flags & RD_STATE)
11b9164a 4537 file = find_readable_file(fp);
f9d7562f 4538 else
11b9164a 4539 file = find_writeable_file(fp);
f9d7562f 4540 }
f7a4d872
BF
4541 break;
4542 default:
14bcab1a
TM
4543 status = nfserr_bad_stateid;
4544 goto out;
1da177e4
LT
4545 }
4546 status = nfs_ok;
14bcab1a 4547 if (file)
de18643d 4548 *filpp = file;
1da177e4 4549out:
fd911011 4550 nfs4_put_stid(s);
1da177e4
LT
4551 return status;
4552}
4553
17456804
BS
4554/*
4555 * Test if the stateid is valid
4556 */
4557__be32
4558nfsd4_test_stateid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4559 struct nfsd4_test_stateid *test_stateid)
4560{
03cfb420
BS
4561 struct nfsd4_test_stateid_id *stateid;
4562 struct nfs4_client *cl = cstate->session->se_client;
4563
03cfb420 4564 list_for_each_entry(stateid, &test_stateid->ts_stateid_list, ts_id_list)
7df302f7
CL
4565 stateid->ts_id_status =
4566 nfsd4_validate_stateid(cl, &stateid->ts_id_stateid);
03cfb420 4567
17456804
BS
4568 return nfs_ok;
4569}
4570
e1ca12df
BS
4571__be32
4572nfsd4_free_stateid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4573 struct nfsd4_free_stateid *free_stateid)
4574{
4575 stateid_t *stateid = &free_stateid->fr_stateid;
2da1cec7 4576 struct nfs4_stid *s;
3bd64a5b 4577 struct nfs4_delegation *dp;
fc5a96c3 4578 struct nfs4_ol_stateid *stp;
38c2f4b1 4579 struct nfs4_client *cl = cstate->session->se_client;
2da1cec7 4580 __be32 ret = nfserr_bad_stateid;
e1ca12df 4581
1af71cc8
JL
4582 spin_lock(&cl->cl_lock);
4583 s = find_stateid_locked(cl, stateid);
2da1cec7 4584 if (!s)
1af71cc8 4585 goto out_unlock;
2da1cec7
BF
4586 switch (s->sc_type) {
4587 case NFS4_DELEG_STID:
e1ca12df 4588 ret = nfserr_locks_held;
1af71cc8 4589 break;
2da1cec7 4590 case NFS4_OPEN_STID:
2da1cec7
BF
4591 ret = check_stateid_generation(stateid, &s->sc_stateid, 1);
4592 if (ret)
1af71cc8
JL
4593 break;
4594 ret = nfserr_locks_held;
f7a4d872 4595 break;
1af71cc8
JL
4596 case NFS4_LOCK_STID:
4597 ret = check_stateid_generation(stateid, &s->sc_stateid, 1);
4598 if (ret)
4599 break;
fc5a96c3
JL
4600 stp = openlockstateid(s);
4601 ret = nfserr_locks_held;
4602 if (check_for_locks(stp->st_stid.sc_file,
4603 lockowner(stp->st_stateowner)))
4604 break;
4605 unhash_lock_stateid(stp);
1af71cc8 4606 spin_unlock(&cl->cl_lock);
fc5a96c3
JL
4607 nfs4_put_stid(s);
4608 ret = nfs_ok;
1af71cc8 4609 goto out;
3bd64a5b
BF
4610 case NFS4_REVOKED_DELEG_STID:
4611 dp = delegstateid(s);
2d4a532d
JL
4612 list_del_init(&dp->dl_recall_lru);
4613 spin_unlock(&cl->cl_lock);
6011695d 4614 nfs4_put_stid(s);
3bd64a5b 4615 ret = nfs_ok;
1af71cc8
JL
4616 goto out;
4617 /* Default falls through and returns nfserr_bad_stateid */
e1ca12df 4618 }
1af71cc8
JL
4619out_unlock:
4620 spin_unlock(&cl->cl_lock);
e1ca12df 4621out:
e1ca12df
BS
4622 return ret;
4623}
4624
4c4cd222
N
4625static inline int
4626setlkflg (int type)
4627{
4628 return (type == NFS4_READW_LT || type == NFS4_READ_LT) ?
4629 RD_STATE : WR_STATE;
4630}
1da177e4 4631
dcef0413 4632static __be32 nfs4_seqid_op_checks(struct nfsd4_compound_state *cstate, stateid_t *stateid, u32 seqid, struct nfs4_ol_stateid *stp)
c0a5d93e
BF
4633{
4634 struct svc_fh *current_fh = &cstate->current_fh;
4635 struct nfs4_stateowner *sop = stp->st_stateowner;
4636 __be32 status;
4637
c0a5d93e
BF
4638 status = nfsd4_check_seqid(cstate, sop, seqid);
4639 if (status)
4640 return status;
3bd64a5b
BF
4641 if (stp->st_stid.sc_type == NFS4_CLOSED_STID
4642 || stp->st_stid.sc_type == NFS4_REVOKED_DELEG_STID)
f7a4d872
BF
4643 /*
4644 * "Closed" stateid's exist *only* to return
3bd64a5b
BF
4645 * nfserr_replay_me from the previous step, and
4646 * revoked delegations are kept only for free_stateid.
f7a4d872
BF
4647 */
4648 return nfserr_bad_stateid;
4649 status = check_stateid_generation(stateid, &stp->st_stid.sc_stateid, nfsd4_has_session(cstate));
4650 if (status)
4651 return status;
4652 return nfs4_check_fh(current_fh, stp);
c0a5d93e
BF
4653}
4654
1da177e4
LT
4655/*
4656 * Checks for sequence id mutating operations.
4657 */
b37ad28b 4658static __be32
dd453dfd 4659nfs4_preprocess_seqid_op(struct nfsd4_compound_state *cstate, u32 seqid,
2288d0e3 4660 stateid_t *stateid, char typemask,
3320fef1
SK
4661 struct nfs4_ol_stateid **stpp,
4662 struct nfsd_net *nn)
1da177e4 4663{
0836f587 4664 __be32 status;
38c2f4b1 4665 struct nfs4_stid *s;
e17f99b7 4666 struct nfs4_ol_stateid *stp = NULL;
1da177e4 4667
8c10cbdb
BH
4668 dprintk("NFSD: %s: seqid=%d stateid = " STATEID_FMT "\n", __func__,
4669 seqid, STATEID_VAL(stateid));
3a4f98bb 4670
1da177e4 4671 *stpp = NULL;
2dd6e458 4672 status = nfsd4_lookup_stateid(cstate, stateid, typemask, &s, nn);
c0a5d93e
BF
4673 if (status)
4674 return status;
e17f99b7 4675 stp = openlockstateid(s);
58fb12e6 4676 nfsd4_cstate_assign_replay(cstate, stp->st_stateowner);
1da177e4 4677
e17f99b7 4678 status = nfs4_seqid_op_checks(cstate, stateid, seqid, stp);
fd911011 4679 if (!status)
e17f99b7 4680 *stpp = stp;
fd911011
TM
4681 else
4682 nfs4_put_stid(&stp->st_stid);
e17f99b7 4683 return status;
c0a5d93e 4684}
39325bd0 4685
3320fef1
SK
4686static __be32 nfs4_preprocess_confirmed_seqid_op(struct nfsd4_compound_state *cstate, u32 seqid,
4687 stateid_t *stateid, struct nfs4_ol_stateid **stpp, struct nfsd_net *nn)
c0a5d93e
BF
4688{
4689 __be32 status;
4690 struct nfs4_openowner *oo;
4cbfc9f7 4691 struct nfs4_ol_stateid *stp;
1da177e4 4692
c0a5d93e 4693 status = nfs4_preprocess_seqid_op(cstate, seqid, stateid,
4cbfc9f7 4694 NFS4_OPEN_STID, &stp, nn);
7a8711c9
BF
4695 if (status)
4696 return status;
4cbfc9f7
TM
4697 oo = openowner(stp->st_stateowner);
4698 if (!(oo->oo_flags & NFS4_OO_CONFIRMED)) {
4699 nfs4_put_stid(&stp->st_stid);
3a4f98bb 4700 return nfserr_bad_stateid;
4cbfc9f7
TM
4701 }
4702 *stpp = stp;
3a4f98bb 4703 return nfs_ok;
1da177e4
LT
4704}
4705
b37ad28b 4706__be32
ca364317 4707nfsd4_open_confirm(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
a4f1706a 4708 struct nfsd4_open_confirm *oc)
1da177e4 4709{
b37ad28b 4710 __be32 status;
fe0750e5 4711 struct nfs4_openowner *oo;
dcef0413 4712 struct nfs4_ol_stateid *stp;
3320fef1 4713 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
1da177e4 4714
a6a9f18f
AV
4715 dprintk("NFSD: nfsd4_open_confirm on file %pd\n",
4716 cstate->current_fh.fh_dentry);
1da177e4 4717
ca364317 4718 status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0);
a8cddc5d
BF
4719 if (status)
4720 return status;
1da177e4 4721
9072d5c6 4722 status = nfs4_preprocess_seqid_op(cstate,
ca364317 4723 oc->oc_seqid, &oc->oc_req_stateid,
3320fef1 4724 NFS4_OPEN_STID, &stp, nn);
9072d5c6 4725 if (status)
68b66e82 4726 goto out;
fe0750e5 4727 oo = openowner(stp->st_stateowner);
68b66e82 4728 status = nfserr_bad_stateid;
dad1c067 4729 if (oo->oo_flags & NFS4_OO_CONFIRMED)
2585fc79 4730 goto put_stateid;
dad1c067 4731 oo->oo_flags |= NFS4_OO_CONFIRMED;
dcef0413
BF
4732 update_stateid(&stp->st_stid.sc_stateid);
4733 memcpy(&oc->oc_resp_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
8c10cbdb 4734 dprintk("NFSD: %s: success, seqid=%d stateid=" STATEID_FMT "\n",
dcef0413 4735 __func__, oc->oc_seqid, STATEID_VAL(&stp->st_stid.sc_stateid));
c7b9a459 4736
2a4317c5 4737 nfsd4_client_record_create(oo->oo_owner.so_client);
68b66e82 4738 status = nfs_ok;
2585fc79
TM
4739put_stateid:
4740 nfs4_put_stid(&stp->st_stid);
1da177e4 4741out:
9411b1d4 4742 nfsd4_bump_seqid(cstate, status);
1da177e4
LT
4743 return status;
4744}
4745
6409a5a6 4746static inline void nfs4_stateid_downgrade_bit(struct nfs4_ol_stateid *stp, u32 access)
1da177e4 4747{
82c5ff1b 4748 if (!test_access(access, stp))
6409a5a6 4749 return;
11b9164a 4750 nfs4_file_put_access(stp->st_stid.sc_file, access);
82c5ff1b 4751 clear_access(access, stp);
6409a5a6 4752}
f197c271 4753
6409a5a6
BF
4754static inline void nfs4_stateid_downgrade(struct nfs4_ol_stateid *stp, u32 to_access)
4755{
4756 switch (to_access) {
4757 case NFS4_SHARE_ACCESS_READ:
4758 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_WRITE);
4759 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_BOTH);
4760 break;
4761 case NFS4_SHARE_ACCESS_WRITE:
4762 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_READ);
4763 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_BOTH);
4764 break;
4765 case NFS4_SHARE_ACCESS_BOTH:
4766 break;
4767 default:
063b0fb9 4768 WARN_ON_ONCE(1);
1da177e4
LT
4769 }
4770}
4771
b37ad28b 4772__be32
ca364317
BF
4773nfsd4_open_downgrade(struct svc_rqst *rqstp,
4774 struct nfsd4_compound_state *cstate,
a4f1706a 4775 struct nfsd4_open_downgrade *od)
1da177e4 4776{
b37ad28b 4777 __be32 status;
dcef0413 4778 struct nfs4_ol_stateid *stp;
3320fef1 4779 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
1da177e4 4780
a6a9f18f
AV
4781 dprintk("NFSD: nfsd4_open_downgrade on file %pd\n",
4782 cstate->current_fh.fh_dentry);
1da177e4 4783
c30e92df 4784 /* We don't yet support WANT bits: */
2c8bd7e0
BH
4785 if (od->od_deleg_want)
4786 dprintk("NFSD: %s: od_deleg_want=0x%x ignored\n", __func__,
4787 od->od_deleg_want);
1da177e4 4788
c0a5d93e 4789 status = nfs4_preprocess_confirmed_seqid_op(cstate, od->od_seqid,
3320fef1 4790 &od->od_stateid, &stp, nn);
9072d5c6 4791 if (status)
1da177e4 4792 goto out;
1da177e4 4793 status = nfserr_inval;
82c5ff1b 4794 if (!test_access(od->od_share_access, stp)) {
c11c591f 4795 dprintk("NFSD: access not a subset of current bitmap: 0x%hhx, input access=%08x\n",
1da177e4 4796 stp->st_access_bmap, od->od_share_access);
0667b1e9 4797 goto put_stateid;
1da177e4 4798 }
ce0fc43c 4799 if (!test_deny(od->od_share_deny, stp)) {
c11c591f 4800 dprintk("NFSD: deny not a subset of current bitmap: 0x%hhx, input deny=%08x\n",
1da177e4 4801 stp->st_deny_bmap, od->od_share_deny);
0667b1e9 4802 goto put_stateid;
1da177e4 4803 }
6409a5a6 4804 nfs4_stateid_downgrade(stp, od->od_share_access);
1da177e4 4805
ce0fc43c 4806 reset_union_bmap_deny(od->od_share_deny, stp);
1da177e4 4807
dcef0413
BF
4808 update_stateid(&stp->st_stid.sc_stateid);
4809 memcpy(&od->od_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
1da177e4 4810 status = nfs_ok;
0667b1e9
TM
4811put_stateid:
4812 nfs4_put_stid(&stp->st_stid);
1da177e4 4813out:
9411b1d4 4814 nfsd4_bump_seqid(cstate, status);
1da177e4
LT
4815 return status;
4816}
4817
f7a4d872
BF
4818static void nfsd4_close_open_stateid(struct nfs4_ol_stateid *s)
4819{
acf9295b 4820 struct nfs4_client *clp = s->st_stid.sc_client;
d83017f9 4821 LIST_HEAD(reaplist);
acf9295b 4822
f7a4d872 4823 s->st_stid.sc_type = NFS4_CLOSED_STID;
2c41beb0 4824 spin_lock(&clp->cl_lock);
d83017f9 4825 unhash_open_stateid(s, &reaplist);
acf9295b 4826
d83017f9
JL
4827 if (clp->cl_minorversion) {
4828 put_ol_stateid_locked(s, &reaplist);
4829 spin_unlock(&clp->cl_lock);
4830 free_ol_stateid_reaplist(&reaplist);
4831 } else {
4832 spin_unlock(&clp->cl_lock);
4833 free_ol_stateid_reaplist(&reaplist);
d3134b10 4834 move_to_close_lru(s, clp->net);
d83017f9 4835 }
38c387b5
BF
4836}
4837
1da177e4
LT
4838/*
4839 * nfs4_unlock_state() called after encode
4840 */
b37ad28b 4841__be32
ca364317 4842nfsd4_close(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
a4f1706a 4843 struct nfsd4_close *close)
1da177e4 4844{
b37ad28b 4845 __be32 status;
dcef0413 4846 struct nfs4_ol_stateid *stp;
3320fef1
SK
4847 struct net *net = SVC_NET(rqstp);
4848 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
1da177e4 4849
a6a9f18f
AV
4850 dprintk("NFSD: nfsd4_close on file %pd\n",
4851 cstate->current_fh.fh_dentry);
1da177e4 4852
f7a4d872
BF
4853 status = nfs4_preprocess_seqid_op(cstate, close->cl_seqid,
4854 &close->cl_stateid,
4855 NFS4_OPEN_STID|NFS4_CLOSED_STID,
3320fef1 4856 &stp, nn);
9411b1d4 4857 nfsd4_bump_seqid(cstate, status);
9072d5c6 4858 if (status)
1da177e4 4859 goto out;
dcef0413
BF
4860 update_stateid(&stp->st_stid.sc_stateid);
4861 memcpy(&close->cl_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
1da177e4 4862
f7a4d872 4863 nfsd4_close_open_stateid(stp);
8a0b589d
TM
4864
4865 /* put reference from nfs4_preprocess_seqid_op */
4866 nfs4_put_stid(&stp->st_stid);
1da177e4 4867out:
1da177e4
LT
4868 return status;
4869}
4870
b37ad28b 4871__be32
ca364317
BF
4872nfsd4_delegreturn(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4873 struct nfsd4_delegreturn *dr)
1da177e4 4874{
203a8c8e
BF
4875 struct nfs4_delegation *dp;
4876 stateid_t *stateid = &dr->dr_stateid;
38c2f4b1 4877 struct nfs4_stid *s;
b37ad28b 4878 __be32 status;
3320fef1 4879 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
1da177e4 4880
ca364317 4881 if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0)))
203a8c8e 4882 return status;
1da177e4 4883
2dd6e458 4884 status = nfsd4_lookup_stateid(cstate, stateid, NFS4_DELEG_STID, &s, nn);
38c2f4b1 4885 if (status)
203a8c8e 4886 goto out;
38c2f4b1 4887 dp = delegstateid(s);
d5477a8d 4888 status = check_stateid_generation(stateid, &dp->dl_stid.sc_stateid, nfsd4_has_session(cstate));
203a8c8e 4889 if (status)
fd911011 4890 goto put_stateid;
203a8c8e 4891
3bd64a5b 4892 destroy_delegation(dp);
fd911011
TM
4893put_stateid:
4894 nfs4_put_stid(&dp->dl_stid);
1da177e4
LT
4895out:
4896 return status;
4897}
4898
4899
1da177e4 4900#define LOFF_OVERFLOW(start, len) ((u64)(len) > ~(u64)(start))
1da177e4 4901
87df4de8
BH
4902static inline u64
4903end_offset(u64 start, u64 len)
4904{
4905 u64 end;
4906
4907 end = start + len;
4908 return end >= start ? end: NFS4_MAX_UINT64;
4909}
4910
4911/* last octet in a range */
4912static inline u64
4913last_byte_offset(u64 start, u64 len)
4914{
4915 u64 end;
4916
063b0fb9 4917 WARN_ON_ONCE(!len);
87df4de8
BH
4918 end = start + len;
4919 return end > start ? end - 1: NFS4_MAX_UINT64;
4920}
4921
1da177e4
LT
4922/*
4923 * TODO: Linux file offsets are _signed_ 64-bit quantities, which means that
4924 * we can't properly handle lock requests that go beyond the (2^63 - 1)-th
4925 * byte, because of sign extension problems. Since NFSv4 calls for 64-bit
4926 * locking, this prevents us from being completely protocol-compliant. The
4927 * real solution to this problem is to start using unsigned file offsets in
4928 * the VFS, but this is a very deep change!
4929 */
4930static inline void
4931nfs4_transform_lock_offset(struct file_lock *lock)
4932{
4933 if (lock->fl_start < 0)
4934 lock->fl_start = OFFSET_MAX;
4935 if (lock->fl_end < 0)
4936 lock->fl_end = OFFSET_MAX;
4937}
4938
aef9583b
KM
4939static void nfsd4_fl_get_owner(struct file_lock *dst, struct file_lock *src)
4940{
4941 struct nfs4_lockowner *lo = (struct nfs4_lockowner *)src->fl_owner;
4942 dst->fl_owner = (fl_owner_t)lockowner(nfs4_get_stateowner(&lo->lo_owner));
4943}
4944
4945static void nfsd4_fl_put_owner(struct file_lock *fl)
4946{
4947 struct nfs4_lockowner *lo = (struct nfs4_lockowner *)fl->fl_owner;
4948
4949 if (lo) {
4950 nfs4_put_stateowner(&lo->lo_owner);
4951 fl->fl_owner = NULL;
4952 }
4953}
4954
7b021967 4955static const struct lock_manager_operations nfsd_posix_mng_ops = {
aef9583b
KM
4956 .lm_get_owner = nfsd4_fl_get_owner,
4957 .lm_put_owner = nfsd4_fl_put_owner,
d5b9026a 4958};
1da177e4
LT
4959
4960static inline void
4961nfs4_set_lock_denied(struct file_lock *fl, struct nfsd4_lock_denied *deny)
4962{
fe0750e5 4963 struct nfs4_lockowner *lo;
1da177e4 4964
d5b9026a 4965 if (fl->fl_lmops == &nfsd_posix_mng_ops) {
fe0750e5
BF
4966 lo = (struct nfs4_lockowner *) fl->fl_owner;
4967 deny->ld_owner.data = kmemdup(lo->lo_owner.so_owner.data,
4968 lo->lo_owner.so_owner.len, GFP_KERNEL);
7c13f344
BF
4969 if (!deny->ld_owner.data)
4970 /* We just don't care that much */
4971 goto nevermind;
fe0750e5
BF
4972 deny->ld_owner.len = lo->lo_owner.so_owner.len;
4973 deny->ld_clientid = lo->lo_owner.so_client->cl_clientid;
d5b9026a 4974 } else {
7c13f344
BF
4975nevermind:
4976 deny->ld_owner.len = 0;
4977 deny->ld_owner.data = NULL;
d5b9026a
N
4978 deny->ld_clientid.cl_boot = 0;
4979 deny->ld_clientid.cl_id = 0;
1da177e4
LT
4980 }
4981 deny->ld_start = fl->fl_start;
87df4de8
BH
4982 deny->ld_length = NFS4_MAX_UINT64;
4983 if (fl->fl_end != NFS4_MAX_UINT64)
1da177e4
LT
4984 deny->ld_length = fl->fl_end - fl->fl_start + 1;
4985 deny->ld_type = NFS4_READ_LT;
4986 if (fl->fl_type != F_RDLCK)
4987 deny->ld_type = NFS4_WRITE_LT;
4988}
4989
fe0750e5 4990static struct nfs4_lockowner *
c58c6610 4991find_lockowner_str_locked(clientid_t *clid, struct xdr_netobj *owner,
d4f0489f 4992 struct nfs4_client *clp)
1da177e4 4993{
d4f0489f 4994 unsigned int strhashval = ownerstr_hashval(owner);
b3c32bcd 4995 struct nfs4_stateowner *so;
1da177e4 4996
0a880a28
TM
4997 lockdep_assert_held(&clp->cl_lock);
4998
d4f0489f
TM
4999 list_for_each_entry(so, &clp->cl_ownerstr_hashtbl[strhashval],
5000 so_strhash) {
b3c32bcd
TM
5001 if (so->so_is_open_owner)
5002 continue;
b5971afa
KM
5003 if (same_owner_str(so, owner))
5004 return lockowner(nfs4_get_stateowner(so));
1da177e4
LT
5005 }
5006 return NULL;
5007}
5008
c58c6610
TM
5009static struct nfs4_lockowner *
5010find_lockowner_str(clientid_t *clid, struct xdr_netobj *owner,
d4f0489f 5011 struct nfs4_client *clp)
c58c6610
TM
5012{
5013 struct nfs4_lockowner *lo;
5014
d4f0489f
TM
5015 spin_lock(&clp->cl_lock);
5016 lo = find_lockowner_str_locked(clid, owner, clp);
5017 spin_unlock(&clp->cl_lock);
c58c6610
TM
5018 return lo;
5019}
5020
8f4b54c5
JL
5021static void nfs4_unhash_lockowner(struct nfs4_stateowner *sop)
5022{
c58c6610 5023 unhash_lockowner_locked(lockowner(sop));
8f4b54c5
JL
5024}
5025
6b180f0b
JL
5026static void nfs4_free_lockowner(struct nfs4_stateowner *sop)
5027{
5028 struct nfs4_lockowner *lo = lockowner(sop);
5029
5030 kmem_cache_free(lockowner_slab, lo);
5031}
5032
5033static const struct nfs4_stateowner_operations lockowner_ops = {
8f4b54c5
JL
5034 .so_unhash = nfs4_unhash_lockowner,
5035 .so_free = nfs4_free_lockowner,
6b180f0b
JL
5036};
5037
1da177e4
LT
5038/*
5039 * Alloc a lock owner structure.
5040 * Called in nfsd4_lock - therefore, OPEN and OPEN_CONFIRM (if needed) has
25985edc 5041 * occurred.
1da177e4 5042 *
16bfdaaf 5043 * strhashval = ownerstr_hashval
1da177e4 5044 */
fe0750e5 5045static struct nfs4_lockowner *
c58c6610
TM
5046alloc_init_lock_stateowner(unsigned int strhashval, struct nfs4_client *clp,
5047 struct nfs4_ol_stateid *open_stp,
5048 struct nfsd4_lock *lock)
5049{
c58c6610 5050 struct nfs4_lockowner *lo, *ret;
1da177e4 5051
fe0750e5
BF
5052 lo = alloc_stateowner(lockowner_slab, &lock->lk_new_owner, clp);
5053 if (!lo)
1da177e4 5054 return NULL;
fe0750e5
BF
5055 INIT_LIST_HEAD(&lo->lo_owner.so_stateids);
5056 lo->lo_owner.so_is_open_owner = 0;
5db1c03f 5057 lo->lo_owner.so_seqid = lock->lk_new_lock_seqid;
6b180f0b 5058 lo->lo_owner.so_ops = &lockowner_ops;
d4f0489f 5059 spin_lock(&clp->cl_lock);
c58c6610 5060 ret = find_lockowner_str_locked(&clp->cl_clientid,
d4f0489f 5061 &lock->lk_new_owner, clp);
c58c6610
TM
5062 if (ret == NULL) {
5063 list_add(&lo->lo_owner.so_strhash,
d4f0489f 5064 &clp->cl_ownerstr_hashtbl[strhashval]);
c58c6610
TM
5065 ret = lo;
5066 } else
5067 nfs4_free_lockowner(&lo->lo_owner);
d4f0489f 5068 spin_unlock(&clp->cl_lock);
fe0750e5 5069 return lo;
1da177e4
LT
5070}
5071
356a95ec
JL
5072static void
5073init_lock_stateid(struct nfs4_ol_stateid *stp, struct nfs4_lockowner *lo,
5074 struct nfs4_file *fp, struct inode *inode,
5075 struct nfs4_ol_stateid *open_stp)
1da177e4 5076{
d3b313a4 5077 struct nfs4_client *clp = lo->lo_owner.so_client;
1da177e4 5078
356a95ec
JL
5079 lockdep_assert_held(&clp->cl_lock);
5080
3d0fabd5 5081 atomic_inc(&stp->st_stid.sc_count);
3abdb607 5082 stp->st_stid.sc_type = NFS4_LOCK_STID;
b5971afa 5083 stp->st_stateowner = nfs4_get_stateowner(&lo->lo_owner);
13cd2184 5084 get_nfs4_file(fp);
11b9164a 5085 stp->st_stid.sc_file = fp;
b49e084d 5086 stp->st_stid.sc_free = nfs4_free_lock_stateid;
0997b173 5087 stp->st_access_bmap = 0;
1da177e4 5088 stp->st_deny_bmap = open_stp->st_deny_bmap;
4c4cd222 5089 stp->st_openstp = open_stp;
3c87b9b7 5090 list_add(&stp->st_locks, &open_stp->st_locks);
1c755dc1 5091 list_add(&stp->st_perstateowner, &lo->lo_owner.so_stateids);
1d31a253
TM
5092 spin_lock(&fp->fi_lock);
5093 list_add(&stp->st_perfile, &fp->fi_stateids);
5094 spin_unlock(&fp->fi_lock);
1da177e4
LT
5095}
5096
c53530da
JL
5097static struct nfs4_ol_stateid *
5098find_lock_stateid(struct nfs4_lockowner *lo, struct nfs4_file *fp)
5099{
5100 struct nfs4_ol_stateid *lst;
356a95ec
JL
5101 struct nfs4_client *clp = lo->lo_owner.so_client;
5102
5103 lockdep_assert_held(&clp->cl_lock);
c53530da
JL
5104
5105 list_for_each_entry(lst, &lo->lo_owner.so_stateids, st_perstateowner) {
3d0fabd5
TM
5106 if (lst->st_stid.sc_file == fp) {
5107 atomic_inc(&lst->st_stid.sc_count);
c53530da 5108 return lst;
3d0fabd5 5109 }
c53530da
JL
5110 }
5111 return NULL;
5112}
5113
356a95ec
JL
5114static struct nfs4_ol_stateid *
5115find_or_create_lock_stateid(struct nfs4_lockowner *lo, struct nfs4_file *fi,
5116 struct inode *inode, struct nfs4_ol_stateid *ost,
5117 bool *new)
5118{
5119 struct nfs4_stid *ns = NULL;
5120 struct nfs4_ol_stateid *lst;
5121 struct nfs4_openowner *oo = openowner(ost->st_stateowner);
5122 struct nfs4_client *clp = oo->oo_owner.so_client;
5123
5124 spin_lock(&clp->cl_lock);
5125 lst = find_lock_stateid(lo, fi);
5126 if (lst == NULL) {
5127 spin_unlock(&clp->cl_lock);
5128 ns = nfs4_alloc_stid(clp, stateid_slab);
5129 if (ns == NULL)
5130 return NULL;
5131
5132 spin_lock(&clp->cl_lock);
5133 lst = find_lock_stateid(lo, fi);
5134 if (likely(!lst)) {
5135 lst = openlockstateid(ns);
5136 init_lock_stateid(lst, lo, fi, inode, ost);
5137 ns = NULL;
5138 *new = true;
5139 }
5140 }
5141 spin_unlock(&clp->cl_lock);
5142 if (ns)
5143 nfs4_put_stid(ns);
5144 return lst;
5145}
c53530da 5146
fd39ca9a 5147static int
1da177e4
LT
5148check_lock_length(u64 offset, u64 length)
5149{
87df4de8 5150 return ((length == 0) || ((length != NFS4_MAX_UINT64) &&
1da177e4
LT
5151 LOFF_OVERFLOW(offset, length)));
5152}
5153
dcef0413 5154static void get_lock_access(struct nfs4_ol_stateid *lock_stp, u32 access)
0997b173 5155{
11b9164a 5156 struct nfs4_file *fp = lock_stp->st_stid.sc_file;
0997b173 5157
7214e860
JL
5158 lockdep_assert_held(&fp->fi_lock);
5159
82c5ff1b 5160 if (test_access(access, lock_stp))
0997b173 5161 return;
12659651 5162 __nfs4_file_get_access(fp, access);
82c5ff1b 5163 set_access(access, lock_stp);
0997b173
BF
5164}
5165
356a95ec
JL
5166static __be32
5167lookup_or_create_lock_state(struct nfsd4_compound_state *cstate,
5168 struct nfs4_ol_stateid *ost,
5169 struct nfsd4_lock *lock,
5170 struct nfs4_ol_stateid **lst, bool *new)
64a284d0 5171{
5db1c03f 5172 __be32 status;
11b9164a 5173 struct nfs4_file *fi = ost->st_stid.sc_file;
64a284d0
BF
5174 struct nfs4_openowner *oo = openowner(ost->st_stateowner);
5175 struct nfs4_client *cl = oo->oo_owner.so_client;
f9c00c3a 5176 struct inode *inode = cstate->current_fh.fh_dentry->d_inode;
64a284d0
BF
5177 struct nfs4_lockowner *lo;
5178 unsigned int strhashval;
5179
d4f0489f 5180 lo = find_lockowner_str(&cl->cl_clientid, &lock->v.new.owner, cl);
c53530da 5181 if (!lo) {
d4f0489f 5182 strhashval = ownerstr_hashval(&lock->v.new.owner);
c53530da
JL
5183 lo = alloc_init_lock_stateowner(strhashval, cl, ost, lock);
5184 if (lo == NULL)
5185 return nfserr_jukebox;
5186 } else {
5187 /* with an existing lockowner, seqids must be the same */
5db1c03f 5188 status = nfserr_bad_seqid;
c53530da
JL
5189 if (!cstate->minorversion &&
5190 lock->lk_new_lock_seqid != lo->lo_owner.so_seqid)
5db1c03f 5191 goto out;
64a284d0 5192 }
c53530da 5193
356a95ec 5194 *lst = find_or_create_lock_stateid(lo, fi, inode, ost, new);
64a284d0 5195 if (*lst == NULL) {
5db1c03f
JL
5196 status = nfserr_jukebox;
5197 goto out;
64a284d0 5198 }
5db1c03f
JL
5199 status = nfs_ok;
5200out:
5201 nfs4_put_stateowner(&lo->lo_owner);
5202 return status;
64a284d0
BF
5203}
5204
1da177e4
LT
5205/*
5206 * LOCK operation
5207 */
b37ad28b 5208__be32
ca364317 5209nfsd4_lock(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
a4f1706a 5210 struct nfsd4_lock *lock)
1da177e4 5211{
fe0750e5
BF
5212 struct nfs4_openowner *open_sop = NULL;
5213 struct nfs4_lockowner *lock_sop = NULL;
3d0fabd5 5214 struct nfs4_ol_stateid *lock_stp = NULL;
0667b1e9 5215 struct nfs4_ol_stateid *open_stp = NULL;
7214e860 5216 struct nfs4_file *fp;
7d947842 5217 struct file *filp = NULL;
21179d81
JL
5218 struct file_lock *file_lock = NULL;
5219 struct file_lock *conflock = NULL;
b37ad28b 5220 __be32 status = 0;
b34f27aa 5221 int lkflg;
b8dd7b9a 5222 int err;
5db1c03f 5223 bool new = false;
3320fef1
SK
5224 struct net *net = SVC_NET(rqstp);
5225 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
1da177e4
LT
5226
5227 dprintk("NFSD: nfsd4_lock: start=%Ld length=%Ld\n",
5228 (long long) lock->lk_offset,
5229 (long long) lock->lk_length);
5230
1da177e4
LT
5231 if (check_lock_length(lock->lk_offset, lock->lk_length))
5232 return nfserr_inval;
5233
ca364317 5234 if ((status = fh_verify(rqstp, &cstate->current_fh,
8837abca 5235 S_IFREG, NFSD_MAY_LOCK))) {
a6f6ef2f
AA
5236 dprintk("NFSD: nfsd4_lock: permission denied!\n");
5237 return status;
5238 }
5239
1da177e4 5240 if (lock->lk_is_new) {
684e5638
BF
5241 if (nfsd4_has_session(cstate))
5242 /* See rfc 5661 18.10.3: given clientid is ignored: */
5243 memcpy(&lock->v.new.clientid,
5244 &cstate->session->se_client->cl_clientid,
5245 sizeof(clientid_t));
5246
1da177e4 5247 status = nfserr_stale_clientid;
2c142baa 5248 if (STALE_CLIENTID(&lock->lk_new_clientid, nn))
1da177e4 5249 goto out;
1da177e4 5250
1da177e4 5251 /* validate and update open stateid and open seqid */
c0a5d93e 5252 status = nfs4_preprocess_confirmed_seqid_op(cstate,
1da177e4
LT
5253 lock->lk_new_open_seqid,
5254 &lock->lk_new_open_stateid,
3320fef1 5255 &open_stp, nn);
37515177 5256 if (status)
1da177e4 5257 goto out;
fe0750e5 5258 open_sop = openowner(open_stp->st_stateowner);
b34f27aa 5259 status = nfserr_bad_stateid;
684e5638 5260 if (!same_clid(&open_sop->oo_owner.so_client->cl_clientid,
b34f27aa
BF
5261 &lock->v.new.clientid))
5262 goto out;
64a284d0 5263 status = lookup_or_create_lock_state(cstate, open_stp, lock,
5db1c03f 5264 &lock_stp, &new);
3d0fabd5 5265 } else {
dd453dfd 5266 status = nfs4_preprocess_seqid_op(cstate,
fe0750e5
BF
5267 lock->lk_old_lock_seqid,
5268 &lock->lk_old_lock_stateid,
3320fef1 5269 NFS4_LOCK_STID, &lock_stp, nn);
3d0fabd5 5270 }
e1aaa891
BF
5271 if (status)
5272 goto out;
64a284d0 5273 lock_sop = lockowner(lock_stp->st_stateowner);
1da177e4 5274
b34f27aa
BF
5275 lkflg = setlkflg(lock->lk_type);
5276 status = nfs4_check_openmode(lock_stp, lkflg);
5277 if (status)
5278 goto out;
5279
0dd395dc 5280 status = nfserr_grace;
3320fef1 5281 if (locks_in_grace(net) && !lock->lk_reclaim)
0dd395dc
N
5282 goto out;
5283 status = nfserr_no_grace;
3320fef1 5284 if (!locks_in_grace(net) && lock->lk_reclaim)
0dd395dc
N
5285 goto out;
5286
21179d81
JL
5287 file_lock = locks_alloc_lock();
5288 if (!file_lock) {
5289 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
5290 status = nfserr_jukebox;
5291 goto out;
5292 }
5293
11b9164a 5294 fp = lock_stp->st_stid.sc_file;
1da177e4
LT
5295 switch (lock->lk_type) {
5296 case NFS4_READ_LT:
5297 case NFS4_READW_LT:
7214e860
JL
5298 spin_lock(&fp->fi_lock);
5299 filp = find_readable_file_locked(fp);
0997b173
BF
5300 if (filp)
5301 get_lock_access(lock_stp, NFS4_SHARE_ACCESS_READ);
7214e860 5302 spin_unlock(&fp->fi_lock);
21179d81 5303 file_lock->fl_type = F_RDLCK;
529d7b2a 5304 break;
1da177e4
LT
5305 case NFS4_WRITE_LT:
5306 case NFS4_WRITEW_LT:
7214e860
JL
5307 spin_lock(&fp->fi_lock);
5308 filp = find_writeable_file_locked(fp);
0997b173
BF
5309 if (filp)
5310 get_lock_access(lock_stp, NFS4_SHARE_ACCESS_WRITE);
7214e860 5311 spin_unlock(&fp->fi_lock);
21179d81 5312 file_lock->fl_type = F_WRLCK;
529d7b2a 5313 break;
1da177e4
LT
5314 default:
5315 status = nfserr_inval;
5316 goto out;
5317 }
f9d7562f
BF
5318 if (!filp) {
5319 status = nfserr_openmode;
5320 goto out;
5321 }
aef9583b
KM
5322
5323 file_lock->fl_owner = (fl_owner_t)lockowner(nfs4_get_stateowner(&lock_sop->lo_owner));
21179d81
JL
5324 file_lock->fl_pid = current->tgid;
5325 file_lock->fl_file = filp;
5326 file_lock->fl_flags = FL_POSIX;
5327 file_lock->fl_lmops = &nfsd_posix_mng_ops;
5328 file_lock->fl_start = lock->lk_offset;
5329 file_lock->fl_end = last_byte_offset(lock->lk_offset, lock->lk_length);
5330 nfs4_transform_lock_offset(file_lock);
5331
5332 conflock = locks_alloc_lock();
5333 if (!conflock) {
5334 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
5335 status = nfserr_jukebox;
5336 goto out;
5337 }
1da177e4 5338
21179d81 5339 err = vfs_lock_file(filp, F_SETLK, file_lock, conflock);
b8dd7b9a 5340 switch (-err) {
1da177e4 5341 case 0: /* success! */
dcef0413
BF
5342 update_stateid(&lock_stp->st_stid.sc_stateid);
5343 memcpy(&lock->lk_resp_stateid, &lock_stp->st_stid.sc_stateid,
1da177e4 5344 sizeof(stateid_t));
b8dd7b9a 5345 status = 0;
eb76b3fd
AA
5346 break;
5347 case (EAGAIN): /* conflock holds conflicting lock */
5348 status = nfserr_denied;
5349 dprintk("NFSD: nfsd4_lock: conflicting lock found!\n");
21179d81 5350 nfs4_set_lock_denied(conflock, &lock->lk_denied);
eb76b3fd 5351 break;
1da177e4
LT
5352 case (EDEADLK):
5353 status = nfserr_deadlock;
eb76b3fd 5354 break;
3e772463 5355 default:
fd85b817 5356 dprintk("NFSD: nfsd4_lock: vfs_lock_file() failed! status %d\n",err);
3e772463 5357 status = nfserrno(err);
eb76b3fd 5358 break;
1da177e4 5359 }
1da177e4 5360out:
de18643d
TM
5361 if (filp)
5362 fput(filp);
5db1c03f
JL
5363 if (lock_stp) {
5364 /* Bump seqid manually if the 4.0 replay owner is openowner */
5365 if (cstate->replay_owner &&
5366 cstate->replay_owner != &lock_sop->lo_owner &&
5367 seqid_mutating_err(ntohl(status)))
5368 lock_sop->lo_owner.so_seqid++;
5369
5370 /*
5371 * If this is a new, never-before-used stateid, and we are
5372 * returning an error, then just go ahead and release it.
5373 */
5374 if (status && new)
5375 release_lock_stateid(lock_stp);
5376
3d0fabd5 5377 nfs4_put_stid(&lock_stp->st_stid);
5db1c03f 5378 }
0667b1e9
TM
5379 if (open_stp)
5380 nfs4_put_stid(&open_stp->st_stid);
9411b1d4 5381 nfsd4_bump_seqid(cstate, status);
21179d81
JL
5382 if (file_lock)
5383 locks_free_lock(file_lock);
5384 if (conflock)
5385 locks_free_lock(conflock);
1da177e4
LT
5386 return status;
5387}
5388
55ef1274
BF
5389/*
5390 * The NFSv4 spec allows a client to do a LOCKT without holding an OPEN,
5391 * so we do a temporary open here just to get an open file to pass to
5392 * vfs_test_lock. (Arguably perhaps test_lock should be done with an
5393 * inode operation.)
5394 */
04da6e9d 5395static __be32 nfsd_test_lock(struct svc_rqst *rqstp, struct svc_fh *fhp, struct file_lock *lock)
55ef1274
BF
5396{
5397 struct file *file;
04da6e9d
AV
5398 __be32 err = nfsd_open(rqstp, fhp, S_IFREG, NFSD_MAY_READ, &file);
5399 if (!err) {
5400 err = nfserrno(vfs_test_lock(file, lock));
5401 nfsd_close(file);
5402 }
55ef1274
BF
5403 return err;
5404}
5405
1da177e4
LT
5406/*
5407 * LOCKT operation
5408 */
b37ad28b 5409__be32
ca364317
BF
5410nfsd4_lockt(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
5411 struct nfsd4_lockt *lockt)
1da177e4 5412{
21179d81 5413 struct file_lock *file_lock = NULL;
5db1c03f 5414 struct nfs4_lockowner *lo = NULL;
b37ad28b 5415 __be32 status;
7f2210fa 5416 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
1da177e4 5417
5ccb0066 5418 if (locks_in_grace(SVC_NET(rqstp)))
1da177e4
LT
5419 return nfserr_grace;
5420
5421 if (check_lock_length(lockt->lt_offset, lockt->lt_length))
5422 return nfserr_inval;
5423
9b2ef62b 5424 if (!nfsd4_has_session(cstate)) {
4b24ca7d 5425 status = lookup_clientid(&lockt->lt_clientid, cstate, nn);
9b2ef62b
BF
5426 if (status)
5427 goto out;
5428 }
1da177e4 5429
75c096f7 5430 if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0)))
1da177e4 5431 goto out;
1da177e4 5432
21179d81
JL
5433 file_lock = locks_alloc_lock();
5434 if (!file_lock) {
5435 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
5436 status = nfserr_jukebox;
5437 goto out;
5438 }
6cd90662 5439
1da177e4
LT
5440 switch (lockt->lt_type) {
5441 case NFS4_READ_LT:
5442 case NFS4_READW_LT:
21179d81 5443 file_lock->fl_type = F_RDLCK;
1da177e4
LT
5444 break;
5445 case NFS4_WRITE_LT:
5446 case NFS4_WRITEW_LT:
21179d81 5447 file_lock->fl_type = F_WRLCK;
1da177e4
LT
5448 break;
5449 default:
2fdada03 5450 dprintk("NFSD: nfs4_lockt: bad lock type!\n");
1da177e4
LT
5451 status = nfserr_inval;
5452 goto out;
5453 }
5454
d4f0489f
TM
5455 lo = find_lockowner_str(&lockt->lt_clientid, &lockt->lt_owner,
5456 cstate->clp);
fe0750e5 5457 if (lo)
21179d81
JL
5458 file_lock->fl_owner = (fl_owner_t)lo;
5459 file_lock->fl_pid = current->tgid;
5460 file_lock->fl_flags = FL_POSIX;
1da177e4 5461
21179d81
JL
5462 file_lock->fl_start = lockt->lt_offset;
5463 file_lock->fl_end = last_byte_offset(lockt->lt_offset, lockt->lt_length);
1da177e4 5464
21179d81 5465 nfs4_transform_lock_offset(file_lock);
1da177e4 5466
21179d81 5467 status = nfsd_test_lock(rqstp, &cstate->current_fh, file_lock);
04da6e9d 5468 if (status)
fd85b817 5469 goto out;
04da6e9d 5470
21179d81 5471 if (file_lock->fl_type != F_UNLCK) {
1da177e4 5472 status = nfserr_denied;
21179d81 5473 nfs4_set_lock_denied(file_lock, &lockt->lt_denied);
1da177e4
LT
5474 }
5475out:
5db1c03f
JL
5476 if (lo)
5477 nfs4_put_stateowner(&lo->lo_owner);
21179d81
JL
5478 if (file_lock)
5479 locks_free_lock(file_lock);
1da177e4
LT
5480 return status;
5481}
5482
b37ad28b 5483__be32
ca364317 5484nfsd4_locku(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
a4f1706a 5485 struct nfsd4_locku *locku)
1da177e4 5486{
dcef0413 5487 struct nfs4_ol_stateid *stp;
1da177e4 5488 struct file *filp = NULL;
21179d81 5489 struct file_lock *file_lock = NULL;
b37ad28b 5490 __be32 status;
b8dd7b9a 5491 int err;
3320fef1
SK
5492 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
5493
1da177e4
LT
5494 dprintk("NFSD: nfsd4_locku: start=%Ld length=%Ld\n",
5495 (long long) locku->lu_offset,
5496 (long long) locku->lu_length);
5497
5498 if (check_lock_length(locku->lu_offset, locku->lu_length))
5499 return nfserr_inval;
5500
9072d5c6 5501 status = nfs4_preprocess_seqid_op(cstate, locku->lu_seqid,
3320fef1
SK
5502 &locku->lu_stateid, NFS4_LOCK_STID,
5503 &stp, nn);
9072d5c6 5504 if (status)
1da177e4 5505 goto out;
11b9164a 5506 filp = find_any_file(stp->st_stid.sc_file);
f9d7562f
BF
5507 if (!filp) {
5508 status = nfserr_lock_range;
858cc573 5509 goto put_stateid;
f9d7562f 5510 }
21179d81
JL
5511 file_lock = locks_alloc_lock();
5512 if (!file_lock) {
5513 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
5514 status = nfserr_jukebox;
de18643d 5515 goto fput;
21179d81 5516 }
6cd90662 5517
21179d81 5518 file_lock->fl_type = F_UNLCK;
aef9583b 5519 file_lock->fl_owner = (fl_owner_t)lockowner(nfs4_get_stateowner(stp->st_stateowner));
21179d81
JL
5520 file_lock->fl_pid = current->tgid;
5521 file_lock->fl_file = filp;
5522 file_lock->fl_flags = FL_POSIX;
5523 file_lock->fl_lmops = &nfsd_posix_mng_ops;
5524 file_lock->fl_start = locku->lu_offset;
5525
5526 file_lock->fl_end = last_byte_offset(locku->lu_offset,
5527 locku->lu_length);
5528 nfs4_transform_lock_offset(file_lock);
1da177e4 5529
21179d81 5530 err = vfs_lock_file(filp, F_SETLK, file_lock, NULL);
b8dd7b9a 5531 if (err) {
fd85b817 5532 dprintk("NFSD: nfs4_locku: vfs_lock_file failed!\n");
1da177e4
LT
5533 goto out_nfserr;
5534 }
dcef0413
BF
5535 update_stateid(&stp->st_stid.sc_stateid);
5536 memcpy(&locku->lu_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
de18643d
TM
5537fput:
5538 fput(filp);
858cc573
TM
5539put_stateid:
5540 nfs4_put_stid(&stp->st_stid);
1da177e4 5541out:
9411b1d4 5542 nfsd4_bump_seqid(cstate, status);
21179d81
JL
5543 if (file_lock)
5544 locks_free_lock(file_lock);
1da177e4
LT
5545 return status;
5546
5547out_nfserr:
b8dd7b9a 5548 status = nfserrno(err);
de18643d 5549 goto fput;
1da177e4
LT
5550}
5551
5552/*
5553 * returns
f9c00c3a
JL
5554 * true: locks held by lockowner
5555 * false: no locks held by lockowner
1da177e4 5556 */
f9c00c3a
JL
5557static bool
5558check_for_locks(struct nfs4_file *fp, struct nfs4_lockowner *lowner)
1da177e4
LT
5559{
5560 struct file_lock **flpp;
f9c00c3a
JL
5561 int status = false;
5562 struct file *filp = find_any_file(fp);
5563 struct inode *inode;
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);
1da177e4 5572
1c8c601a 5573 spin_lock(&inode->i_lock);
1da177e4 5574 for (flpp = &inode->i_flock; *flpp != NULL; flpp = &(*flpp)->fl_next) {
796dadfd 5575 if ((*flpp)->fl_owner == (fl_owner_t)lowner) {
f9c00c3a
JL
5576 status = true;
5577 break;
796dadfd 5578 }
1da177e4 5579 }
1c8c601a 5580 spin_unlock(&inode->i_lock);
f9c00c3a 5581 fput(filp);
1da177e4
LT
5582 return status;
5583}
5584
b37ad28b 5585__be32
b591480b
BF
5586nfsd4_release_lockowner(struct svc_rqst *rqstp,
5587 struct nfsd4_compound_state *cstate,
5588 struct nfsd4_release_lockowner *rlockowner)
1da177e4
LT
5589{
5590 clientid_t *clid = &rlockowner->rl_clientid;
882e9d25
JL
5591 struct nfs4_stateowner *sop;
5592 struct nfs4_lockowner *lo = NULL;
dcef0413 5593 struct nfs4_ol_stateid *stp;
1da177e4 5594 struct xdr_netobj *owner = &rlockowner->rl_owner;
d4f0489f 5595 unsigned int hashval = ownerstr_hashval(owner);
b37ad28b 5596 __be32 status;
7f2210fa 5597 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
c58c6610 5598 struct nfs4_client *clp;
1da177e4
LT
5599
5600 dprintk("nfsd4_release_lockowner clientid: (%08x/%08x):\n",
5601 clid->cl_boot, clid->cl_id);
5602
4b24ca7d 5603 status = lookup_clientid(clid, cstate, nn);
9b2ef62b 5604 if (status)
51f5e783 5605 return status;
9b2ef62b 5606
d4f0489f 5607 clp = cstate->clp;
fd44907c 5608 /* Find the matching lock stateowner */
d4f0489f 5609 spin_lock(&clp->cl_lock);
882e9d25 5610 list_for_each_entry(sop, &clp->cl_ownerstr_hashtbl[hashval],
d4f0489f 5611 so_strhash) {
fd44907c 5612
882e9d25
JL
5613 if (sop->so_is_open_owner || !same_owner_str(sop, owner))
5614 continue;
fd44907c 5615
882e9d25
JL
5616 /* see if there are still any locks associated with it */
5617 lo = lockowner(sop);
5618 list_for_each_entry(stp, &sop->so_stateids, st_perstateowner) {
5619 if (check_for_locks(stp->st_stid.sc_file, lo)) {
5620 status = nfserr_locks_held;
5621 spin_unlock(&clp->cl_lock);
51f5e783 5622 return status;
882e9d25 5623 }
5adfd885 5624 }
882e9d25 5625
b5971afa 5626 nfs4_get_stateowner(sop);
882e9d25 5627 break;
1da177e4 5628 }
c58c6610 5629 spin_unlock(&clp->cl_lock);
882e9d25
JL
5630 if (lo)
5631 release_lockowner(lo);
1da177e4
LT
5632 return status;
5633}
5634
5635static inline struct nfs4_client_reclaim *
a55370a3 5636alloc_reclaim(void)
1da177e4 5637{
a55370a3 5638 return kmalloc(sizeof(struct nfs4_client_reclaim), GFP_KERNEL);
1da177e4
LT
5639}
5640
0ce0c2b5 5641bool
52e19c09 5642nfs4_has_reclaimed_state(const char *name, struct nfsd_net *nn)
c7b9a459 5643{
0ce0c2b5 5644 struct nfs4_client_reclaim *crp;
c7b9a459 5645
52e19c09 5646 crp = nfsd4_find_reclaim_client(name, nn);
0ce0c2b5 5647 return (crp && crp->cr_clp);
c7b9a459
N
5648}
5649
1da177e4
LT
5650/*
5651 * failure => all reset bets are off, nfserr_no_grace...
5652 */
772a9bbb 5653struct nfs4_client_reclaim *
52e19c09 5654nfs4_client_to_reclaim(const char *name, struct nfsd_net *nn)
1da177e4
LT
5655{
5656 unsigned int strhashval;
772a9bbb 5657 struct nfs4_client_reclaim *crp;
1da177e4 5658
a55370a3
N
5659 dprintk("NFSD nfs4_client_to_reclaim NAME: %.*s\n", HEXDIR_LEN, name);
5660 crp = alloc_reclaim();
772a9bbb
JL
5661 if (crp) {
5662 strhashval = clientstr_hashval(name);
5663 INIT_LIST_HEAD(&crp->cr_strhash);
52e19c09 5664 list_add(&crp->cr_strhash, &nn->reclaim_str_hashtbl[strhashval]);
772a9bbb 5665 memcpy(crp->cr_recdir, name, HEXDIR_LEN);
0ce0c2b5 5666 crp->cr_clp = NULL;
52e19c09 5667 nn->reclaim_str_hashtbl_size++;
772a9bbb
JL
5668 }
5669 return crp;
1da177e4
LT
5670}
5671
ce30e539 5672void
52e19c09 5673nfs4_remove_reclaim_record(struct nfs4_client_reclaim *crp, struct nfsd_net *nn)
ce30e539
JL
5674{
5675 list_del(&crp->cr_strhash);
5676 kfree(crp);
52e19c09 5677 nn->reclaim_str_hashtbl_size--;
ce30e539
JL
5678}
5679
2a4317c5 5680void
52e19c09 5681nfs4_release_reclaim(struct nfsd_net *nn)
1da177e4
LT
5682{
5683 struct nfs4_client_reclaim *crp = NULL;
5684 int i;
5685
1da177e4 5686 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
52e19c09
SK
5687 while (!list_empty(&nn->reclaim_str_hashtbl[i])) {
5688 crp = list_entry(nn->reclaim_str_hashtbl[i].next,
1da177e4 5689 struct nfs4_client_reclaim, cr_strhash);
52e19c09 5690 nfs4_remove_reclaim_record(crp, nn);
1da177e4
LT
5691 }
5692 }
063b0fb9 5693 WARN_ON_ONCE(nn->reclaim_str_hashtbl_size);
1da177e4
LT
5694}
5695
5696/*
5697 * called from OPEN, CLAIM_PREVIOUS with a new clientid. */
2a4317c5 5698struct nfs4_client_reclaim *
52e19c09 5699nfsd4_find_reclaim_client(const char *recdir, struct nfsd_net *nn)
1da177e4
LT
5700{
5701 unsigned int strhashval;
1da177e4
LT
5702 struct nfs4_client_reclaim *crp = NULL;
5703
278c931c 5704 dprintk("NFSD: nfs4_find_reclaim_client for recdir %s\n", recdir);
1da177e4 5705
278c931c 5706 strhashval = clientstr_hashval(recdir);
52e19c09 5707 list_for_each_entry(crp, &nn->reclaim_str_hashtbl[strhashval], cr_strhash) {
278c931c 5708 if (same_name(crp->cr_recdir, recdir)) {
1da177e4
LT
5709 return crp;
5710 }
5711 }
5712 return NULL;
5713}
5714
5715/*
5716* Called from OPEN. Look for clientid in reclaim list.
5717*/
b37ad28b 5718__be32
0fe492db
TM
5719nfs4_check_open_reclaim(clientid_t *clid,
5720 struct nfsd4_compound_state *cstate,
5721 struct nfsd_net *nn)
1da177e4 5722{
0fe492db 5723 __be32 status;
a52d726b
JL
5724
5725 /* find clientid in conf_id_hashtbl */
0fe492db
TM
5726 status = lookup_clientid(clid, cstate, nn);
5727 if (status)
a52d726b
JL
5728 return nfserr_reclaim_bad;
5729
3b3e7b72
JL
5730 if (test_bit(NFSD4_CLIENT_RECLAIM_COMPLETE, &cstate->clp->cl_flags))
5731 return nfserr_no_grace;
5732
0fe492db
TM
5733 if (nfsd4_client_record_check(cstate->clp))
5734 return nfserr_reclaim_bad;
5735
5736 return nfs_ok;
1da177e4
LT
5737}
5738
65178db4 5739#ifdef CONFIG_NFSD_FAULT_INJECTION
016200c3
JL
5740static inline void
5741put_client(struct nfs4_client *clp)
5742{
5743 atomic_dec(&clp->cl_refcount);
5744}
5745
285abdee
JL
5746static struct nfs4_client *
5747nfsd_find_client(struct sockaddr_storage *addr, size_t addr_size)
5748{
5749 struct nfs4_client *clp;
5750 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
5751 nfsd_net_id);
5752
5753 if (!nfsd_netns_ready(nn))
5754 return NULL;
5755
5756 list_for_each_entry(clp, &nn->client_lru, cl_lru) {
5757 if (memcmp(&clp->cl_addr, addr, addr_size) == 0)
5758 return clp;
5759 }
5760 return NULL;
5761}
5762
7ec0e36f 5763u64
285abdee 5764nfsd_inject_print_clients(void)
7ec0e36f
JL
5765{
5766 struct nfs4_client *clp;
5767 u64 count = 0;
5768 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
5769 nfsd_net_id);
5770 char buf[INET6_ADDRSTRLEN];
5771
5772 if (!nfsd_netns_ready(nn))
5773 return 0;
5774
5775 spin_lock(&nn->client_lock);
5776 list_for_each_entry(clp, &nn->client_lru, cl_lru) {
5777 rpc_ntop((struct sockaddr *)&clp->cl_addr, buf, sizeof(buf));
5778 pr_info("NFS Client: %s\n", buf);
5779 ++count;
5780 }
5781 spin_unlock(&nn->client_lock);
5782
5783 return count;
5784}
65178db4 5785
a0926d15 5786u64
285abdee 5787nfsd_inject_forget_client(struct sockaddr_storage *addr, size_t addr_size)
a0926d15
JL
5788{
5789 u64 count = 0;
5790 struct nfs4_client *clp;
5791 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
5792 nfsd_net_id);
5793
5794 if (!nfsd_netns_ready(nn))
5795 return count;
5796
5797 spin_lock(&nn->client_lock);
5798 clp = nfsd_find_client(addr, addr_size);
5799 if (clp) {
5800 if (mark_client_expired_locked(clp) == nfs_ok)
5801 ++count;
5802 else
5803 clp = NULL;
5804 }
5805 spin_unlock(&nn->client_lock);
5806
5807 if (clp)
5808 expire_client(clp);
5809
5810 return count;
5811}
5812
69fc9edf 5813u64
285abdee 5814nfsd_inject_forget_clients(u64 max)
69fc9edf
JL
5815{
5816 u64 count = 0;
5817 struct nfs4_client *clp, *next;
5818 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
5819 nfsd_net_id);
5820 LIST_HEAD(reaplist);
5821
5822 if (!nfsd_netns_ready(nn))
5823 return count;
5824
5825 spin_lock(&nn->client_lock);
5826 list_for_each_entry_safe(clp, next, &nn->client_lru, cl_lru) {
5827 if (mark_client_expired_locked(clp) == nfs_ok) {
5828 list_add(&clp->cl_lru, &reaplist);
5829 if (max != 0 && ++count >= max)
5830 break;
5831 }
5832 }
5833 spin_unlock(&nn->client_lock);
5834
5835 list_for_each_entry_safe(clp, next, &reaplist, cl_lru)
5836 expire_client(clp);
5837
5838 return count;
5839}
5840
184c1847
BS
5841static void nfsd_print_count(struct nfs4_client *clp, unsigned int count,
5842 const char *type)
5843{
5844 char buf[INET6_ADDRSTRLEN];
0a5c33e2 5845 rpc_ntop((struct sockaddr *)&clp->cl_addr, buf, sizeof(buf));
184c1847
BS
5846 printk(KERN_INFO "NFS Client: %s has %u %s\n", buf, count, type);
5847}
5848
016200c3
JL
5849static void
5850nfsd_inject_add_lock_to_list(struct nfs4_ol_stateid *lst,
5851 struct list_head *collect)
5852{
5853 struct nfs4_client *clp = lst->st_stid.sc_client;
5854 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
5855 nfsd_net_id);
5856
5857 if (!collect)
5858 return;
5859
5860 lockdep_assert_held(&nn->client_lock);
5861 atomic_inc(&clp->cl_refcount);
5862 list_add(&lst->st_locks, collect);
5863}
5864
3c87b9b7 5865static u64 nfsd_foreach_client_lock(struct nfs4_client *clp, u64 max,
3738d50e 5866 struct list_head *collect,
3c87b9b7 5867 void (*func)(struct nfs4_ol_stateid *))
fc29171f
BS
5868{
5869 struct nfs4_openowner *oop;
fc29171f 5870 struct nfs4_ol_stateid *stp, *st_next;
3c87b9b7 5871 struct nfs4_ol_stateid *lst, *lst_next;
fc29171f
BS
5872 u64 count = 0;
5873
016200c3 5874 spin_lock(&clp->cl_lock);
fc29171f 5875 list_for_each_entry(oop, &clp->cl_openowners, oo_perclient) {
3c87b9b7
TM
5876 list_for_each_entry_safe(stp, st_next,
5877 &oop->oo_owner.so_stateids, st_perstateowner) {
5878 list_for_each_entry_safe(lst, lst_next,
5879 &stp->st_locks, st_locks) {
3738d50e 5880 if (func) {
3c87b9b7 5881 func(lst);
016200c3
JL
5882 nfsd_inject_add_lock_to_list(lst,
5883 collect);
3738d50e 5884 }
016200c3
JL
5885 ++count;
5886 /*
5887 * Despite the fact that these functions deal
5888 * with 64-bit integers for "count", we must
5889 * ensure that it doesn't blow up the
5890 * clp->cl_refcount. Throw a warning if we
5891 * start to approach INT_MAX here.
5892 */
5893 WARN_ON_ONCE(count == (INT_MAX / 2));
5894 if (count == max)
5895 goto out;
fc29171f
BS
5896 }
5897 }
5898 }
016200c3
JL
5899out:
5900 spin_unlock(&clp->cl_lock);
fc29171f
BS
5901
5902 return count;
5903}
5904
016200c3
JL
5905static u64
5906nfsd_collect_client_locks(struct nfs4_client *clp, struct list_head *collect,
5907 u64 max)
fc29171f 5908{
016200c3 5909 return nfsd_foreach_client_lock(clp, max, collect, unhash_lock_stateid);
fc29171f
BS
5910}
5911
016200c3
JL
5912static u64
5913nfsd_print_client_locks(struct nfs4_client *clp)
184c1847 5914{
016200c3 5915 u64 count = nfsd_foreach_client_lock(clp, 0, NULL, NULL);
184c1847
BS
5916 nfsd_print_count(clp, count, "locked files");
5917 return count;
5918}
5919
016200c3 5920u64
285abdee 5921nfsd_inject_print_locks(void)
016200c3
JL
5922{
5923 struct nfs4_client *clp;
5924 u64 count = 0;
5925 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
5926 nfsd_net_id);
5927
5928 if (!nfsd_netns_ready(nn))
5929 return 0;
5930
5931 spin_lock(&nn->client_lock);
5932 list_for_each_entry(clp, &nn->client_lru, cl_lru)
5933 count += nfsd_print_client_locks(clp);
5934 spin_unlock(&nn->client_lock);
5935
5936 return count;
5937}
5938
5939static void
5940nfsd_reap_locks(struct list_head *reaplist)
5941{
5942 struct nfs4_client *clp;
5943 struct nfs4_ol_stateid *stp, *next;
5944
5945 list_for_each_entry_safe(stp, next, reaplist, st_locks) {
5946 list_del_init(&stp->st_locks);
5947 clp = stp->st_stid.sc_client;
5948 nfs4_put_stid(&stp->st_stid);
5949 put_client(clp);
5950 }
5951}
5952
5953u64
285abdee 5954nfsd_inject_forget_client_locks(struct sockaddr_storage *addr, size_t addr_size)
016200c3
JL
5955{
5956 unsigned int count = 0;
5957 struct nfs4_client *clp;
5958 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
5959 nfsd_net_id);
5960 LIST_HEAD(reaplist);
5961
5962 if (!nfsd_netns_ready(nn))
5963 return count;
5964
5965 spin_lock(&nn->client_lock);
5966 clp = nfsd_find_client(addr, addr_size);
5967 if (clp)
5968 count = nfsd_collect_client_locks(clp, &reaplist, 0);
5969 spin_unlock(&nn->client_lock);
5970 nfsd_reap_locks(&reaplist);
5971 return count;
5972}
5973
5974u64
285abdee 5975nfsd_inject_forget_locks(u64 max)
016200c3
JL
5976{
5977 u64 count = 0;
5978 struct nfs4_client *clp;
5979 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
5980 nfsd_net_id);
5981 LIST_HEAD(reaplist);
5982
5983 if (!nfsd_netns_ready(nn))
5984 return count;
5985
5986 spin_lock(&nn->client_lock);
5987 list_for_each_entry(clp, &nn->client_lru, cl_lru) {
5988 count += nfsd_collect_client_locks(clp, &reaplist, max - count);
5989 if (max != 0 && count >= max)
5990 break;
5991 }
5992 spin_unlock(&nn->client_lock);
5993 nfsd_reap_locks(&reaplist);
5994 return count;
5995}
5996
82e05efa
JL
5997static u64
5998nfsd_foreach_client_openowner(struct nfs4_client *clp, u64 max,
5999 struct list_head *collect,
6000 void (*func)(struct nfs4_openowner *))
4dbdbda8
BS
6001{
6002 struct nfs4_openowner *oop, *next;
82e05efa
JL
6003 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6004 nfsd_net_id);
4dbdbda8
BS
6005 u64 count = 0;
6006
82e05efa
JL
6007 lockdep_assert_held(&nn->client_lock);
6008
6009 spin_lock(&clp->cl_lock);
4dbdbda8 6010 list_for_each_entry_safe(oop, next, &clp->cl_openowners, oo_perclient) {
82e05efa 6011 if (func) {
4dbdbda8 6012 func(oop);
82e05efa
JL
6013 if (collect) {
6014 atomic_inc(&clp->cl_refcount);
6015 list_add(&oop->oo_perclient, collect);
6016 }
6017 }
6018 ++count;
6019 /*
6020 * Despite the fact that these functions deal with
6021 * 64-bit integers for "count", we must ensure that
6022 * it doesn't blow up the clp->cl_refcount. Throw a
6023 * warning if we start to approach INT_MAX here.
6024 */
6025 WARN_ON_ONCE(count == (INT_MAX / 2));
6026 if (count == max)
4dbdbda8
BS
6027 break;
6028 }
82e05efa
JL
6029 spin_unlock(&clp->cl_lock);
6030
6031 return count;
6032}
6033
6034static u64
6035nfsd_print_client_openowners(struct nfs4_client *clp)
6036{
6037 u64 count = nfsd_foreach_client_openowner(clp, 0, NULL, NULL);
6038
6039 nfsd_print_count(clp, count, "openowners");
6040 return count;
6041}
6042
6043static u64
6044nfsd_collect_client_openowners(struct nfs4_client *clp,
6045 struct list_head *collect, u64 max)
6046{
6047 return nfsd_foreach_client_openowner(clp, max, collect,
6048 unhash_openowner_locked);
6049}
6050
6051u64
285abdee 6052nfsd_inject_print_openowners(void)
82e05efa
JL
6053{
6054 struct nfs4_client *clp;
6055 u64 count = 0;
6056 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6057 nfsd_net_id);
6058
6059 if (!nfsd_netns_ready(nn))
6060 return 0;
6061
6062 spin_lock(&nn->client_lock);
6063 list_for_each_entry(clp, &nn->client_lru, cl_lru)
6064 count += nfsd_print_client_openowners(clp);
6065 spin_unlock(&nn->client_lock);
4dbdbda8
BS
6066
6067 return count;
6068}
6069
82e05efa
JL
6070static void
6071nfsd_reap_openowners(struct list_head *reaplist)
6072{
6073 struct nfs4_client *clp;
6074 struct nfs4_openowner *oop, *next;
6075
6076 list_for_each_entry_safe(oop, next, reaplist, oo_perclient) {
6077 list_del_init(&oop->oo_perclient);
6078 clp = oop->oo_owner.so_client;
6079 release_openowner(oop);
6080 put_client(clp);
6081 }
6082}
6083
6084u64
285abdee
JL
6085nfsd_inject_forget_client_openowners(struct sockaddr_storage *addr,
6086 size_t addr_size)
4dbdbda8 6087{
82e05efa
JL
6088 unsigned int count = 0;
6089 struct nfs4_client *clp;
6090 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6091 nfsd_net_id);
6092 LIST_HEAD(reaplist);
6093
6094 if (!nfsd_netns_ready(nn))
6095 return count;
6096
6097 spin_lock(&nn->client_lock);
6098 clp = nfsd_find_client(addr, addr_size);
6099 if (clp)
6100 count = nfsd_collect_client_openowners(clp, &reaplist, 0);
6101 spin_unlock(&nn->client_lock);
6102 nfsd_reap_openowners(&reaplist);
6103 return count;
4dbdbda8
BS
6104}
6105
82e05efa 6106u64
285abdee 6107nfsd_inject_forget_openowners(u64 max)
184c1847 6108{
82e05efa
JL
6109 u64 count = 0;
6110 struct nfs4_client *clp;
6111 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6112 nfsd_net_id);
6113 LIST_HEAD(reaplist);
6114
6115 if (!nfsd_netns_ready(nn))
6116 return count;
6117
6118 spin_lock(&nn->client_lock);
6119 list_for_each_entry(clp, &nn->client_lru, cl_lru) {
6120 count += nfsd_collect_client_openowners(clp, &reaplist,
6121 max - count);
6122 if (max != 0 && count >= max)
6123 break;
6124 }
6125 spin_unlock(&nn->client_lock);
6126 nfsd_reap_openowners(&reaplist);
184c1847
BS
6127 return count;
6128}
6129
269de30f
BS
6130static u64 nfsd_find_all_delegations(struct nfs4_client *clp, u64 max,
6131 struct list_head *victims)
6132{
6133 struct nfs4_delegation *dp, *next;
98d5c7c5
JL
6134 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6135 nfsd_net_id);
269de30f
BS
6136 u64 count = 0;
6137
98d5c7c5
JL
6138 lockdep_assert_held(&nn->client_lock);
6139
6140 spin_lock(&state_lock);
269de30f 6141 list_for_each_entry_safe(dp, next, &clp->cl_delegations, dl_perclnt) {
dff1399f
JL
6142 if (victims) {
6143 /*
6144 * It's not safe to mess with delegations that have a
6145 * non-zero dl_time. They might have already been broken
6146 * and could be processed by the laundromat outside of
6147 * the state_lock. Just leave them be.
6148 */
6149 if (dp->dl_time != 0)
6150 continue;
6151
98d5c7c5 6152 atomic_inc(&clp->cl_refcount);
42690676
JL
6153 unhash_delegation_locked(dp);
6154 list_add(&dp->dl_recall_lru, victims);
dff1399f 6155 }
98d5c7c5
JL
6156 ++count;
6157 /*
6158 * Despite the fact that these functions deal with
6159 * 64-bit integers for "count", we must ensure that
6160 * it doesn't blow up the clp->cl_refcount. Throw a
6161 * warning if we start to approach INT_MAX here.
6162 */
6163 WARN_ON_ONCE(count == (INT_MAX / 2));
6164 if (count == max)
269de30f
BS
6165 break;
6166 }
98d5c7c5 6167 spin_unlock(&state_lock);
269de30f
BS
6168 return count;
6169}
6170
98d5c7c5
JL
6171static u64
6172nfsd_print_client_delegations(struct nfs4_client *clp)
269de30f 6173{
98d5c7c5 6174 u64 count = nfsd_find_all_delegations(clp, 0, NULL);
269de30f 6175
98d5c7c5
JL
6176 nfsd_print_count(clp, count, "delegations");
6177 return count;
6178}
6179
6180u64
285abdee 6181nfsd_inject_print_delegations(void)
98d5c7c5
JL
6182{
6183 struct nfs4_client *clp;
6184 u64 count = 0;
6185 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6186 nfsd_net_id);
6187
6188 if (!nfsd_netns_ready(nn))
6189 return 0;
269de30f 6190
98d5c7c5
JL
6191 spin_lock(&nn->client_lock);
6192 list_for_each_entry(clp, &nn->client_lru, cl_lru)
6193 count += nfsd_print_client_delegations(clp);
6194 spin_unlock(&nn->client_lock);
6195
6196 return count;
6197}
6198
6199static void
6200nfsd_forget_delegations(struct list_head *reaplist)
6201{
6202 struct nfs4_client *clp;
6203 struct nfs4_delegation *dp, *next;
6204
6205 list_for_each_entry_safe(dp, next, reaplist, dl_recall_lru) {
2d4a532d 6206 list_del_init(&dp->dl_recall_lru);
98d5c7c5 6207 clp = dp->dl_stid.sc_client;
3bd64a5b 6208 revoke_delegation(dp);
98d5c7c5 6209 put_client(clp);
2d4a532d 6210 }
98d5c7c5
JL
6211}
6212
6213u64
285abdee
JL
6214nfsd_inject_forget_client_delegations(struct sockaddr_storage *addr,
6215 size_t addr_size)
98d5c7c5
JL
6216{
6217 u64 count = 0;
6218 struct nfs4_client *clp;
6219 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6220 nfsd_net_id);
6221 LIST_HEAD(reaplist);
6222
6223 if (!nfsd_netns_ready(nn))
6224 return count;
6225
6226 spin_lock(&nn->client_lock);
6227 clp = nfsd_find_client(addr, addr_size);
6228 if (clp)
6229 count = nfsd_find_all_delegations(clp, 0, &reaplist);
6230 spin_unlock(&nn->client_lock);
6231
6232 nfsd_forget_delegations(&reaplist);
6233 return count;
6234}
269de30f 6235
98d5c7c5 6236u64
285abdee 6237nfsd_inject_forget_delegations(u64 max)
98d5c7c5
JL
6238{
6239 u64 count = 0;
6240 struct nfs4_client *clp;
6241 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6242 nfsd_net_id);
6243 LIST_HEAD(reaplist);
6244
6245 if (!nfsd_netns_ready(nn))
6246 return count;
6247
6248 spin_lock(&nn->client_lock);
6249 list_for_each_entry(clp, &nn->client_lru, cl_lru) {
6250 count += nfsd_find_all_delegations(clp, max - count, &reaplist);
6251 if (max != 0 && count >= max)
6252 break;
6253 }
6254 spin_unlock(&nn->client_lock);
6255 nfsd_forget_delegations(&reaplist);
269de30f
BS
6256 return count;
6257}
6258
98d5c7c5
JL
6259static void
6260nfsd_recall_delegations(struct list_head *reaplist)
269de30f 6261{
98d5c7c5
JL
6262 struct nfs4_client *clp;
6263 struct nfs4_delegation *dp, *next;
269de30f 6264
98d5c7c5 6265 list_for_each_entry_safe(dp, next, reaplist, dl_recall_lru) {
dff1399f 6266 list_del_init(&dp->dl_recall_lru);
98d5c7c5
JL
6267 clp = dp->dl_stid.sc_client;
6268 /*
6269 * We skipped all entries that had a zero dl_time before,
6270 * so we can now reset the dl_time back to 0. If a delegation
6271 * break comes in now, then it won't make any difference since
6272 * we're recalling it either way.
6273 */
6274 spin_lock(&state_lock);
dff1399f 6275 dp->dl_time = 0;
98d5c7c5 6276 spin_unlock(&state_lock);
269de30f 6277 nfsd_break_one_deleg(dp);
98d5c7c5 6278 put_client(clp);
dff1399f 6279 }
98d5c7c5 6280}
269de30f 6281
98d5c7c5 6282u64
285abdee 6283nfsd_inject_recall_client_delegations(struct sockaddr_storage *addr,
98d5c7c5
JL
6284 size_t addr_size)
6285{
6286 u64 count = 0;
6287 struct nfs4_client *clp;
6288 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6289 nfsd_net_id);
6290 LIST_HEAD(reaplist);
6291
6292 if (!nfsd_netns_ready(nn))
6293 return count;
6294
6295 spin_lock(&nn->client_lock);
6296 clp = nfsd_find_client(addr, addr_size);
6297 if (clp)
6298 count = nfsd_find_all_delegations(clp, 0, &reaplist);
6299 spin_unlock(&nn->client_lock);
6300
6301 nfsd_recall_delegations(&reaplist);
269de30f
BS
6302 return count;
6303}
6304
98d5c7c5 6305u64
285abdee 6306nfsd_inject_recall_delegations(u64 max)
184c1847
BS
6307{
6308 u64 count = 0;
98d5c7c5
JL
6309 struct nfs4_client *clp, *next;
6310 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6311 nfsd_net_id);
6312 LIST_HEAD(reaplist);
184c1847 6313
98d5c7c5
JL
6314 if (!nfsd_netns_ready(nn))
6315 return count;
184c1847 6316
98d5c7c5
JL
6317 spin_lock(&nn->client_lock);
6318 list_for_each_entry_safe(clp, next, &nn->client_lru, cl_lru) {
6319 count += nfsd_find_all_delegations(clp, max - count, &reaplist);
6320 if (max != 0 && ++count >= max)
6321 break;
6322 }
6323 spin_unlock(&nn->client_lock);
6324 nfsd_recall_delegations(&reaplist);
184c1847
BS
6325 return count;
6326}
65178db4
BS
6327#endif /* CONFIG_NFSD_FAULT_INJECTION */
6328
c2f1a551
MS
6329/*
6330 * Since the lifetime of a delegation isn't limited to that of an open, a
6331 * client may quite reasonably hang on to a delegation as long as it has
6332 * the inode cached. This becomes an obvious problem the first time a
6333 * client's inode cache approaches the size of the server's total memory.
6334 *
6335 * For now we avoid this problem by imposing a hard limit on the number
6336 * of delegations, which varies according to the server's memory size.
6337 */
6338static void
6339set_max_delegations(void)
6340{
6341 /*
6342 * Allow at most 4 delegations per megabyte of RAM. Quick
6343 * estimates suggest that in the worst case (where every delegation
6344 * is for a different inode), a delegation could take about 1.5K,
6345 * giving a worst case usage of about 6% of memory.
6346 */
6347 max_delegations = nr_free_buffer_pages() >> (20 - 2 - PAGE_SHIFT);
6348}
6349
d85ed443 6350static int nfs4_state_create_net(struct net *net)
8daae4dc
SK
6351{
6352 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
6353 int i;
6354
6355 nn->conf_id_hashtbl = kmalloc(sizeof(struct list_head) *
6356 CLIENT_HASH_SIZE, GFP_KERNEL);
6357 if (!nn->conf_id_hashtbl)
382a62e7 6358 goto err;
0a7ec377
SK
6359 nn->unconf_id_hashtbl = kmalloc(sizeof(struct list_head) *
6360 CLIENT_HASH_SIZE, GFP_KERNEL);
6361 if (!nn->unconf_id_hashtbl)
6362 goto err_unconf_id;
1872de0e
SK
6363 nn->sessionid_hashtbl = kmalloc(sizeof(struct list_head) *
6364 SESSION_HASH_SIZE, GFP_KERNEL);
6365 if (!nn->sessionid_hashtbl)
6366 goto err_sessionid;
8daae4dc 6367
382a62e7 6368 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
8daae4dc 6369 INIT_LIST_HEAD(&nn->conf_id_hashtbl[i]);
0a7ec377 6370 INIT_LIST_HEAD(&nn->unconf_id_hashtbl[i]);
382a62e7 6371 }
1872de0e
SK
6372 for (i = 0; i < SESSION_HASH_SIZE; i++)
6373 INIT_LIST_HEAD(&nn->sessionid_hashtbl[i]);
382a62e7 6374 nn->conf_name_tree = RB_ROOT;
a99454aa 6375 nn->unconf_name_tree = RB_ROOT;
5ed58bb2 6376 INIT_LIST_HEAD(&nn->client_lru);
73758fed 6377 INIT_LIST_HEAD(&nn->close_lru);
e8c69d17 6378 INIT_LIST_HEAD(&nn->del_recall_lru);
c9a49628 6379 spin_lock_init(&nn->client_lock);
8daae4dc 6380
09121281 6381 INIT_DELAYED_WORK(&nn->laundromat_work, laundromat_main);
d85ed443 6382 get_net(net);
09121281 6383
8daae4dc 6384 return 0;
382a62e7 6385
1872de0e 6386err_sessionid:
9b531137 6387 kfree(nn->unconf_id_hashtbl);
0a7ec377
SK
6388err_unconf_id:
6389 kfree(nn->conf_id_hashtbl);
382a62e7
SK
6390err:
6391 return -ENOMEM;
8daae4dc
SK
6392}
6393
6394static void
4dce0ac9 6395nfs4_state_destroy_net(struct net *net)
8daae4dc
SK
6396{
6397 int i;
6398 struct nfs4_client *clp = NULL;
6399 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
6400
6401 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
6402 while (!list_empty(&nn->conf_id_hashtbl[i])) {
6403 clp = list_entry(nn->conf_id_hashtbl[i].next, struct nfs4_client, cl_idhash);
6404 destroy_client(clp);
6405 }
6406 }
a99454aa 6407
2b905635
KM
6408 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
6409 while (!list_empty(&nn->unconf_id_hashtbl[i])) {
6410 clp = list_entry(nn->unconf_id_hashtbl[i].next, struct nfs4_client, cl_idhash);
6411 destroy_client(clp);
6412 }
a99454aa
SK
6413 }
6414
1872de0e 6415 kfree(nn->sessionid_hashtbl);
0a7ec377 6416 kfree(nn->unconf_id_hashtbl);
8daae4dc 6417 kfree(nn->conf_id_hashtbl);
4dce0ac9 6418 put_net(net);
8daae4dc
SK
6419}
6420
f252bc68 6421int
d85ed443 6422nfs4_state_start_net(struct net *net)
ac4d8ff2 6423{
5e1533c7 6424 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
b5a1a81e
BF
6425 int ret;
6426
d85ed443 6427 ret = nfs4_state_create_net(net);
8daae4dc
SK
6428 if (ret)
6429 return ret;
2c142baa 6430 nn->boot_time = get_seconds();
a51c84ed 6431 nn->grace_ended = false;
d4318acd
JL
6432 locks_start_grace(net, &nn->nfsd4_manager);
6433 nfsd4_client_tracking_init(net);
d85ed443 6434 printk(KERN_INFO "NFSD: starting %ld-second grace period (net %p)\n",
5284b44e
SK
6435 nn->nfsd4_grace, net);
6436 queue_delayed_work(laundry_wq, &nn->laundromat_work, nn->nfsd4_grace * HZ);
d85ed443
SK
6437 return 0;
6438}
6439
6440/* initialization to perform when the nfsd service is started: */
6441
6442int
6443nfs4_state_start(void)
6444{
6445 int ret;
6446
b5a1a81e 6447 ret = set_callback_cred();
d85ed443
SK
6448 if (ret)
6449 return -ENOMEM;
58da282b 6450 laundry_wq = create_singlethread_workqueue("nfsd4");
a6d6b781
JL
6451 if (laundry_wq == NULL) {
6452 ret = -ENOMEM;
6453 goto out_recovery;
6454 }
b5a1a81e
BF
6455 ret = nfsd4_create_callback_queue();
6456 if (ret)
6457 goto out_free_laundry;
09121281 6458
c2f1a551 6459 set_max_delegations();
d85ed443 6460
b5a1a81e 6461 return 0;
d85ed443 6462
b5a1a81e
BF
6463out_free_laundry:
6464 destroy_workqueue(laundry_wq);
a6d6b781 6465out_recovery:
b5a1a81e 6466 return ret;
1da177e4
LT
6467}
6468
f252bc68 6469void
4dce0ac9 6470nfs4_state_shutdown_net(struct net *net)
1da177e4 6471{
1da177e4 6472 struct nfs4_delegation *dp = NULL;
1da177e4 6473 struct list_head *pos, *next, reaplist;
4dce0ac9 6474 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
1da177e4 6475
4dce0ac9
SK
6476 cancel_delayed_work_sync(&nn->laundromat_work);
6477 locks_end_grace(&nn->nfsd4_manager);
ac55fdc4 6478
1da177e4 6479 INIT_LIST_HEAD(&reaplist);
cdc97505 6480 spin_lock(&state_lock);
e8c69d17 6481 list_for_each_safe(pos, next, &nn->del_recall_lru) {
1da177e4 6482 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
42690676
JL
6483 unhash_delegation_locked(dp);
6484 list_add(&dp->dl_recall_lru, &reaplist);
1da177e4 6485 }
cdc97505 6486 spin_unlock(&state_lock);
1da177e4
LT
6487 list_for_each_safe(pos, next, &reaplist) {
6488 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
42690676 6489 list_del_init(&dp->dl_recall_lru);
afbda402 6490 nfs4_put_deleg_lease(dp->dl_stid.sc_file);
6011695d 6491 nfs4_put_stid(&dp->dl_stid);
1da177e4
LT
6492 }
6493
3320fef1 6494 nfsd4_client_tracking_exit(net);
4dce0ac9 6495 nfs4_state_destroy_net(net);
1da177e4
LT
6496}
6497
6498void
6499nfs4_state_shutdown(void)
6500{
5e8d5c29 6501 destroy_workqueue(laundry_wq);
c3935e30 6502 nfsd4_destroy_callback_queue();
1da177e4 6503}
8b70484c
TM
6504
6505static void
6506get_stateid(struct nfsd4_compound_state *cstate, stateid_t *stateid)
6507{
37c593c5
TM
6508 if (HAS_STATE_ID(cstate, CURRENT_STATE_ID_FLAG) && CURRENT_STATEID(stateid))
6509 memcpy(stateid, &cstate->current_stateid, sizeof(stateid_t));
8b70484c
TM
6510}
6511
6512static void
6513put_stateid(struct nfsd4_compound_state *cstate, stateid_t *stateid)
6514{
37c593c5
TM
6515 if (cstate->minorversion) {
6516 memcpy(&cstate->current_stateid, stateid, sizeof(stateid_t));
6517 SET_STATE_ID(cstate, CURRENT_STATE_ID_FLAG);
6518 }
6519}
6520
6521void
6522clear_current_stateid(struct nfsd4_compound_state *cstate)
6523{
6524 CLEAR_STATE_ID(cstate, CURRENT_STATE_ID_FLAG);
8b70484c
TM
6525}
6526
62cd4a59
TM
6527/*
6528 * functions to set current state id
6529 */
9428fe1a
TM
6530void
6531nfsd4_set_opendowngradestateid(struct nfsd4_compound_state *cstate, struct nfsd4_open_downgrade *odp)
6532{
6533 put_stateid(cstate, &odp->od_stateid);
6534}
6535
8b70484c
TM
6536void
6537nfsd4_set_openstateid(struct nfsd4_compound_state *cstate, struct nfsd4_open *open)
6538{
6539 put_stateid(cstate, &open->op_stateid);
6540}
6541
62cd4a59
TM
6542void
6543nfsd4_set_closestateid(struct nfsd4_compound_state *cstate, struct nfsd4_close *close)
6544{
6545 put_stateid(cstate, &close->cl_stateid);
6546}
6547
6548void
6549nfsd4_set_lockstateid(struct nfsd4_compound_state *cstate, struct nfsd4_lock *lock)
6550{
6551 put_stateid(cstate, &lock->lk_resp_stateid);
6552}
6553
6554/*
6555 * functions to consume current state id
6556 */
1e97b519 6557
9428fe1a
TM
6558void
6559nfsd4_get_opendowngradestateid(struct nfsd4_compound_state *cstate, struct nfsd4_open_downgrade *odp)
6560{
6561 get_stateid(cstate, &odp->od_stateid);
6562}
6563
6564void
6565nfsd4_get_delegreturnstateid(struct nfsd4_compound_state *cstate, struct nfsd4_delegreturn *drp)
6566{
6567 get_stateid(cstate, &drp->dr_stateid);
6568}
6569
1e97b519
TM
6570void
6571nfsd4_get_freestateid(struct nfsd4_compound_state *cstate, struct nfsd4_free_stateid *fsp)
6572{
6573 get_stateid(cstate, &fsp->fr_stateid);
6574}
6575
6576void
6577nfsd4_get_setattrstateid(struct nfsd4_compound_state *cstate, struct nfsd4_setattr *setattr)
6578{
6579 get_stateid(cstate, &setattr->sa_stateid);
6580}
6581
8b70484c
TM
6582void
6583nfsd4_get_closestateid(struct nfsd4_compound_state *cstate, struct nfsd4_close *close)
6584{
6585 get_stateid(cstate, &close->cl_stateid);
6586}
6587
6588void
62cd4a59 6589nfsd4_get_lockustateid(struct nfsd4_compound_state *cstate, struct nfsd4_locku *locku)
8b70484c 6590{
62cd4a59 6591 get_stateid(cstate, &locku->lu_stateid);
8b70484c 6592}
30813e27
TM
6593
6594void
6595nfsd4_get_readstateid(struct nfsd4_compound_state *cstate, struct nfsd4_read *read)
6596{
6597 get_stateid(cstate, &read->rd_stateid);
6598}
6599
6600void
6601nfsd4_get_writestateid(struct nfsd4_compound_state *cstate, struct nfsd4_write *write)
6602{
6603 get_stateid(cstate, &write->wr_stateid);
6604}