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