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