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