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