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