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