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