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