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