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