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