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