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