lockd: move lockd's grace period handling into its own module
[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
7ffb5880 219static int
d4f0489f 220same_owner_str(struct nfs4_stateowner *sop, struct xdr_netobj *owner)
7ffb5880
TM
221{
222 return (sop->so_owner.len == owner->len) &&
d4f0489f 223 0 == memcmp(sop->so_owner.data, owner->data, owner->len);
7ffb5880
TM
224}
225
226static struct nfs4_openowner *
227find_openstateowner_str_locked(unsigned int hashval, struct nfsd4_open *open,
d4f0489f 228 struct nfs4_client *clp)
7ffb5880
TM
229{
230 struct nfs4_stateowner *so;
7ffb5880 231
d4f0489f 232 lockdep_assert_held(&clp->cl_lock);
7ffb5880 233
d4f0489f
TM
234 list_for_each_entry(so, &clp->cl_ownerstr_hashtbl[hashval],
235 so_strhash) {
7ffb5880
TM
236 if (!so->so_is_open_owner)
237 continue;
d4f0489f 238 if (same_owner_str(so, &open->op_owner)) {
7ffb5880 239 atomic_inc(&so->so_count);
d4f0489f 240 return openowner(so);
7ffb5880
TM
241 }
242 }
243 return NULL;
244}
245
246static struct nfs4_openowner *
247find_openstateowner_str(unsigned int hashval, struct nfsd4_open *open,
d4f0489f 248 struct nfs4_client *clp)
7ffb5880
TM
249{
250 struct nfs4_openowner *oo;
251
d4f0489f
TM
252 spin_lock(&clp->cl_lock);
253 oo = find_openstateowner_str_locked(hashval, open, clp);
254 spin_unlock(&clp->cl_lock);
7ffb5880
TM
255 return oo;
256}
257
1da177e4
LT
258static inline u32
259opaque_hashval(const void *ptr, int nbytes)
260{
261 unsigned char *cptr = (unsigned char *) ptr;
262
263 u32 x = 0;
264 while (nbytes--) {
265 x *= 37;
266 x += *cptr++;
267 }
268 return x;
269}
270
32513b40
BF
271static void nfsd4_free_file(struct nfs4_file *f)
272{
273 kmem_cache_free(file_slab, f);
274}
275
13cd2184
N
276static inline void
277put_nfs4_file(struct nfs4_file *fi)
278{
02e1215f
JL
279 might_lock(&state_lock);
280
cdc97505 281 if (atomic_dec_and_lock(&fi->fi_ref, &state_lock)) {
89876f8c 282 hlist_del(&fi->fi_hash);
cdc97505 283 spin_unlock(&state_lock);
32513b40 284 nfsd4_free_file(fi);
8b671b80 285 }
13cd2184
N
286}
287
288static inline void
289get_nfs4_file(struct nfs4_file *fi)
290{
8b671b80 291 atomic_inc(&fi->fi_ref);
13cd2184
N
292}
293
de18643d
TM
294static struct file *
295__nfs4_get_fd(struct nfs4_file *f, int oflag)
296{
297 if (f->fi_fds[oflag])
298 return get_file(f->fi_fds[oflag]);
299 return NULL;
300}
301
302static struct file *
303find_writeable_file_locked(struct nfs4_file *f)
304{
305 struct file *ret;
306
307 lockdep_assert_held(&f->fi_lock);
308
309 ret = __nfs4_get_fd(f, O_WRONLY);
310 if (!ret)
311 ret = __nfs4_get_fd(f, O_RDWR);
312 return ret;
313}
314
315static struct file *
316find_writeable_file(struct nfs4_file *f)
317{
318 struct file *ret;
319
320 spin_lock(&f->fi_lock);
321 ret = find_writeable_file_locked(f);
322 spin_unlock(&f->fi_lock);
323
324 return ret;
325}
326
327static struct file *find_readable_file_locked(struct nfs4_file *f)
328{
329 struct file *ret;
330
331 lockdep_assert_held(&f->fi_lock);
332
333 ret = __nfs4_get_fd(f, O_RDONLY);
334 if (!ret)
335 ret = __nfs4_get_fd(f, O_RDWR);
336 return ret;
337}
338
339static struct file *
340find_readable_file(struct nfs4_file *f)
341{
342 struct file *ret;
343
344 spin_lock(&f->fi_lock);
345 ret = find_readable_file_locked(f);
346 spin_unlock(&f->fi_lock);
347
348 return ret;
349}
350
351static struct file *
352find_any_file(struct nfs4_file *f)
353{
354 struct file *ret;
355
356 spin_lock(&f->fi_lock);
357 ret = __nfs4_get_fd(f, O_RDWR);
358 if (!ret) {
359 ret = __nfs4_get_fd(f, O_WRONLY);
360 if (!ret)
361 ret = __nfs4_get_fd(f, O_RDONLY);
362 }
363 spin_unlock(&f->fi_lock);
364 return ret;
365}
366
02a3508d 367static atomic_long_t num_delegations;
697ce9be 368unsigned long max_delegations;
ef0f3390
N
369
370/*
371 * Open owner state (share locks)
372 */
373
16bfdaaf
BF
374/* hash tables for lock and open owners */
375#define OWNER_HASH_BITS 8
376#define OWNER_HASH_SIZE (1 << OWNER_HASH_BITS)
377#define OWNER_HASH_MASK (OWNER_HASH_SIZE - 1)
ef0f3390 378
d4f0489f 379static unsigned int ownerstr_hashval(struct xdr_netobj *ownername)
ddc04c41
BF
380{
381 unsigned int ret;
382
383 ret = opaque_hashval(ownername->data, ownername->len);
16bfdaaf 384 return ret & OWNER_HASH_MASK;
ddc04c41 385}
ef0f3390 386
ef0f3390
N
387/* hash table for nfs4_file */
388#define FILE_HASH_BITS 8
389#define FILE_HASH_SIZE (1 << FILE_HASH_BITS)
35079582 390
ca943217 391static unsigned int nfsd_fh_hashval(struct knfsd_fh *fh)
ddc04c41 392{
ca943217
TM
393 return jhash2(fh->fh_base.fh_pad, XDR_QUADLEN(fh->fh_size), 0);
394}
395
396static unsigned int file_hashval(struct knfsd_fh *fh)
397{
398 return nfsd_fh_hashval(fh) & (FILE_HASH_SIZE - 1);
399}
400
401static bool nfsd_fh_match(struct knfsd_fh *fh1, struct knfsd_fh *fh2)
402{
403 return fh1->fh_size == fh2->fh_size &&
404 !memcmp(fh1->fh_base.fh_pad,
405 fh2->fh_base.fh_pad,
406 fh1->fh_size);
ddc04c41
BF
407}
408
89876f8c 409static struct hlist_head file_hashtbl[FILE_HASH_SIZE];
ef0f3390 410
12659651
JL
411static void
412__nfs4_file_get_access(struct nfs4_file *fp, u32 access)
3477565e 413{
7214e860
JL
414 lockdep_assert_held(&fp->fi_lock);
415
12659651
JL
416 if (access & NFS4_SHARE_ACCESS_WRITE)
417 atomic_inc(&fp->fi_access[O_WRONLY]);
418 if (access & NFS4_SHARE_ACCESS_READ)
419 atomic_inc(&fp->fi_access[O_RDONLY]);
3477565e
BF
420}
421
12659651
JL
422static __be32
423nfs4_file_get_access(struct nfs4_file *fp, u32 access)
998db52c 424{
7214e860
JL
425 lockdep_assert_held(&fp->fi_lock);
426
12659651
JL
427 /* Does this access mode make sense? */
428 if (access & ~NFS4_SHARE_ACCESS_BOTH)
429 return nfserr_inval;
430
baeb4ff0
JL
431 /* Does it conflict with a deny mode already set? */
432 if ((access & fp->fi_share_deny) != 0)
433 return nfserr_share_denied;
434
12659651
JL
435 __nfs4_file_get_access(fp, access);
436 return nfs_ok;
998db52c
BF
437}
438
baeb4ff0
JL
439static __be32 nfs4_file_check_deny(struct nfs4_file *fp, u32 deny)
440{
441 /* Common case is that there is no deny mode. */
442 if (deny) {
443 /* Does this deny mode make sense? */
444 if (deny & ~NFS4_SHARE_DENY_BOTH)
445 return nfserr_inval;
446
447 if ((deny & NFS4_SHARE_DENY_READ) &&
448 atomic_read(&fp->fi_access[O_RDONLY]))
449 return nfserr_share_denied;
450
451 if ((deny & NFS4_SHARE_DENY_WRITE) &&
452 atomic_read(&fp->fi_access[O_WRONLY]))
453 return nfserr_share_denied;
454 }
455 return nfs_ok;
456}
457
998db52c 458static void __nfs4_file_put_access(struct nfs4_file *fp, int oflag)
f9d7562f 459{
de18643d
TM
460 might_lock(&fp->fi_lock);
461
462 if (atomic_dec_and_lock(&fp->fi_access[oflag], &fp->fi_lock)) {
463 struct file *f1 = NULL;
464 struct file *f2 = NULL;
465
6d338b51 466 swap(f1, fp->fi_fds[oflag]);
0c7c3e67 467 if (atomic_read(&fp->fi_access[1 - oflag]) == 0)
6d338b51 468 swap(f2, fp->fi_fds[O_RDWR]);
de18643d
TM
469 spin_unlock(&fp->fi_lock);
470 if (f1)
471 fput(f1);
472 if (f2)
473 fput(f2);
f9d7562f
BF
474 }
475}
476
12659651 477static void nfs4_file_put_access(struct nfs4_file *fp, u32 access)
998db52c 478{
12659651
JL
479 WARN_ON_ONCE(access & ~NFS4_SHARE_ACCESS_BOTH);
480
481 if (access & NFS4_SHARE_ACCESS_WRITE)
998db52c 482 __nfs4_file_put_access(fp, O_WRONLY);
12659651
JL
483 if (access & NFS4_SHARE_ACCESS_READ)
484 __nfs4_file_put_access(fp, O_RDONLY);
998db52c
BF
485}
486
6011695d
TM
487static struct nfs4_stid *nfs4_alloc_stid(struct nfs4_client *cl,
488 struct kmem_cache *slab)
2a74aba7 489{
3abdb607 490 struct nfs4_stid *stid;
6136d2b4 491 int new_id;
2a74aba7 492
f8338834 493 stid = kmem_cache_zalloc(slab, GFP_KERNEL);
3abdb607
BF
494 if (!stid)
495 return NULL;
496
4770d722
JL
497 idr_preload(GFP_KERNEL);
498 spin_lock(&cl->cl_lock);
499 new_id = idr_alloc_cyclic(&cl->cl_stateids, stid, 0, 0, GFP_NOWAIT);
500 spin_unlock(&cl->cl_lock);
501 idr_preload_end();
ebd6c707 502 if (new_id < 0)
3abdb607 503 goto out_free;
2a74aba7 504 stid->sc_client = cl;
3abdb607
BF
505 stid->sc_stateid.si_opaque.so_id = new_id;
506 stid->sc_stateid.si_opaque.so_clid = cl->cl_clientid;
2a74aba7 507 /* Will be incremented before return to client: */
72c0b0fb 508 atomic_set(&stid->sc_count, 1);
996e0938 509
996e0938 510 /*
3abdb607
BF
511 * It shouldn't be a problem to reuse an opaque stateid value.
512 * I don't think it is for 4.1. But with 4.0 I worry that, for
513 * example, a stray write retransmission could be accepted by
514 * the server when it should have been rejected. Therefore,
515 * adopt a trick from the sctp code to attempt to maximize the
516 * amount of time until an id is reused, by ensuring they always
517 * "increase" (mod INT_MAX):
996e0938 518 */
3abdb607
BF
519 return stid;
520out_free:
2c44a234 521 kmem_cache_free(slab, stid);
3abdb607 522 return NULL;
2a74aba7
BF
523}
524
b49e084d 525static struct nfs4_ol_stateid * nfs4_alloc_open_stateid(struct nfs4_client *clp)
4cdc951b 526{
6011695d
TM
527 struct nfs4_stid *stid;
528 struct nfs4_ol_stateid *stp;
529
530 stid = nfs4_alloc_stid(clp, stateid_slab);
531 if (!stid)
532 return NULL;
533
534 stp = openlockstateid(stid);
535 stp->st_stid.sc_free = nfs4_free_ol_stateid;
536 return stp;
537}
538
539static void nfs4_free_deleg(struct nfs4_stid *stid)
540{
6011695d
TM
541 kmem_cache_free(deleg_slab, stid);
542 atomic_long_dec(&num_delegations);
4cdc951b
BF
543}
544
6282cd56
N
545/*
546 * When we recall a delegation, we should be careful not to hand it
547 * out again straight away.
548 * To ensure this we keep a pair of bloom filters ('new' and 'old')
549 * in which the filehandles of recalled delegations are "stored".
550 * If a filehandle appear in either filter, a delegation is blocked.
551 * When a delegation is recalled, the filehandle is stored in the "new"
552 * filter.
553 * Every 30 seconds we swap the filters and clear the "new" one,
554 * unless both are empty of course.
555 *
556 * Each filter is 256 bits. We hash the filehandle to 32bit and use the
557 * low 3 bytes as hash-table indices.
558 *
f54fe962 559 * 'blocked_delegations_lock', which is always taken in block_delegations(),
6282cd56
N
560 * is used to manage concurrent access. Testing does not need the lock
561 * except when swapping the two filters.
562 */
f54fe962 563static DEFINE_SPINLOCK(blocked_delegations_lock);
6282cd56
N
564static struct bloom_pair {
565 int entries, old_entries;
566 time_t swap_time;
567 int new; /* index into 'set' */
568 DECLARE_BITMAP(set[2], 256);
569} blocked_delegations;
570
571static int delegation_blocked(struct knfsd_fh *fh)
572{
573 u32 hash;
574 struct bloom_pair *bd = &blocked_delegations;
575
576 if (bd->entries == 0)
577 return 0;
578 if (seconds_since_boot() - bd->swap_time > 30) {
f54fe962 579 spin_lock(&blocked_delegations_lock);
6282cd56
N
580 if (seconds_since_boot() - bd->swap_time > 30) {
581 bd->entries -= bd->old_entries;
582 bd->old_entries = bd->entries;
583 memset(bd->set[bd->new], 0,
584 sizeof(bd->set[0]));
585 bd->new = 1-bd->new;
586 bd->swap_time = seconds_since_boot();
587 }
f54fe962 588 spin_unlock(&blocked_delegations_lock);
6282cd56
N
589 }
590 hash = arch_fast_hash(&fh->fh_base, fh->fh_size, 0);
591 if (test_bit(hash&255, bd->set[0]) &&
592 test_bit((hash>>8)&255, bd->set[0]) &&
593 test_bit((hash>>16)&255, bd->set[0]))
594 return 1;
595
596 if (test_bit(hash&255, bd->set[1]) &&
597 test_bit((hash>>8)&255, bd->set[1]) &&
598 test_bit((hash>>16)&255, bd->set[1]))
599 return 1;
600
601 return 0;
602}
603
604static void block_delegations(struct knfsd_fh *fh)
605{
606 u32 hash;
607 struct bloom_pair *bd = &blocked_delegations;
608
609 hash = arch_fast_hash(&fh->fh_base, fh->fh_size, 0);
610
f54fe962 611 spin_lock(&blocked_delegations_lock);
6282cd56
N
612 __set_bit(hash&255, bd->set[bd->new]);
613 __set_bit((hash>>8)&255, bd->set[bd->new]);
614 __set_bit((hash>>16)&255, bd->set[bd->new]);
615 if (bd->entries == 0)
616 bd->swap_time = seconds_since_boot();
617 bd->entries += 1;
f54fe962 618 spin_unlock(&blocked_delegations_lock);
6282cd56
N
619}
620
1da177e4 621static struct nfs4_delegation *
f9416e28 622alloc_init_deleg(struct nfs4_client *clp, struct svc_fh *current_fh)
1da177e4
LT
623{
624 struct nfs4_delegation *dp;
02a3508d 625 long n;
1da177e4
LT
626
627 dprintk("NFSD alloc_init_deleg\n");
02a3508d
TM
628 n = atomic_long_inc_return(&num_delegations);
629 if (n < 0 || n > max_delegations)
630 goto out_dec;
6282cd56 631 if (delegation_blocked(&current_fh->fh_handle))
02a3508d 632 goto out_dec;
996e0938 633 dp = delegstateid(nfs4_alloc_stid(clp, deleg_slab));
5b2d21c1 634 if (dp == NULL)
02a3508d 635 goto out_dec;
6011695d
TM
636
637 dp->dl_stid.sc_free = nfs4_free_deleg;
2a74aba7
BF
638 /*
639 * delegation seqid's are never incremented. The 4.1 special
6136d2b4
BF
640 * meaning of seqid 0 isn't meaningful, really, but let's avoid
641 * 0 anyway just for consistency and use 1:
2a74aba7
BF
642 */
643 dp->dl_stid.sc_stateid.si_generation = 1;
ea1da636
N
644 INIT_LIST_HEAD(&dp->dl_perfile);
645 INIT_LIST_HEAD(&dp->dl_perclnt);
1da177e4 646 INIT_LIST_HEAD(&dp->dl_recall_lru);
99c41515 647 dp->dl_type = NFS4_OPEN_DELEGATE_READ;
02e1215f 648 INIT_WORK(&dp->dl_recall.cb_work, nfsd4_run_cb_recall);
1da177e4 649 return dp;
02a3508d
TM
650out_dec:
651 atomic_long_dec(&num_delegations);
652 return NULL;
1da177e4
LT
653}
654
655void
6011695d 656nfs4_put_stid(struct nfs4_stid *s)
1da177e4 657{
11b9164a 658 struct nfs4_file *fp = s->sc_file;
6011695d
TM
659 struct nfs4_client *clp = s->sc_client;
660
4770d722
JL
661 might_lock(&clp->cl_lock);
662
b401be22
JL
663 if (!atomic_dec_and_lock(&s->sc_count, &clp->cl_lock)) {
664 wake_up_all(&close_wq);
6011695d 665 return;
b401be22 666 }
6011695d 667 idr_remove(&clp->cl_stateids, s->sc_stateid.si_opaque.so_id);
4770d722 668 spin_unlock(&clp->cl_lock);
6011695d 669 s->sc_free(s);
11b9164a
TM
670 if (fp)
671 put_nfs4_file(fp);
1da177e4
LT
672}
673
acfdf5c3 674static void nfs4_put_deleg_lease(struct nfs4_file *fp)
1da177e4 675{
6bcc034e
JL
676 struct file *filp = NULL;
677 struct file_lock *fl;
417c6629 678
6bcc034e
JL
679 spin_lock(&fp->fi_lock);
680 if (fp->fi_lease && atomic_dec_and_test(&fp->fi_delegees)) {
681 swap(filp, fp->fi_deleg_file);
682 fl = fp->fi_lease;
acfdf5c3 683 fp->fi_lease = NULL;
6bcc034e
JL
684 }
685 spin_unlock(&fp->fi_lock);
686
687 if (filp) {
688 vfs_setlease(filp, F_UNLCK, &fl);
689 fput(filp);
acfdf5c3 690 }
1da177e4
LT
691}
692
6136d2b4
BF
693static void unhash_stid(struct nfs4_stid *s)
694{
3abdb607 695 s->sc_type = 0;
6136d2b4
BF
696}
697
931ee56c
BH
698static void
699hash_delegation_locked(struct nfs4_delegation *dp, struct nfs4_file *fp)
700{
cdc97505 701 lockdep_assert_held(&state_lock);
417c6629 702 lockdep_assert_held(&fp->fi_lock);
931ee56c 703
67cb1279 704 atomic_inc(&dp->dl_stid.sc_count);
3fb87d13 705 dp->dl_stid.sc_type = NFS4_DELEG_STID;
931ee56c
BH
706 list_add(&dp->dl_perfile, &fp->fi_delegations);
707 list_add(&dp->dl_perclnt, &dp->dl_stid.sc_client->cl_delegations);
708}
709
1da177e4 710static void
42690676 711unhash_delegation_locked(struct nfs4_delegation *dp)
1da177e4 712{
11b9164a 713 struct nfs4_file *fp = dp->dl_stid.sc_file;
02e1215f 714
42690676
JL
715 lockdep_assert_held(&state_lock);
716
b0fc29d6 717 dp->dl_stid.sc_type = NFS4_CLOSED_DELEG_STID;
d55a166c
JL
718 /* Ensure that deleg break won't try to requeue it */
719 ++dp->dl_time;
417c6629 720 spin_lock(&fp->fi_lock);
931ee56c 721 list_del_init(&dp->dl_perclnt);
1da177e4 722 list_del_init(&dp->dl_recall_lru);
02e1215f
JL
723 list_del_init(&dp->dl_perfile);
724 spin_unlock(&fp->fi_lock);
3bd64a5b
BF
725}
726
3bd64a5b
BF
727static void destroy_delegation(struct nfs4_delegation *dp)
728{
42690676
JL
729 spin_lock(&state_lock);
730 unhash_delegation_locked(dp);
731 spin_unlock(&state_lock);
afbda402 732 nfs4_put_deleg_lease(dp->dl_stid.sc_file);
6011695d 733 nfs4_put_stid(&dp->dl_stid);
3bd64a5b
BF
734}
735
736static void revoke_delegation(struct nfs4_delegation *dp)
737{
738 struct nfs4_client *clp = dp->dl_stid.sc_client;
739
2d4a532d
JL
740 WARN_ON(!list_empty(&dp->dl_recall_lru));
741
afbda402
JL
742 nfs4_put_deleg_lease(dp->dl_stid.sc_file);
743
3bd64a5b 744 if (clp->cl_minorversion == 0)
6011695d 745 nfs4_put_stid(&dp->dl_stid);
3bd64a5b 746 else {
3bd64a5b 747 dp->dl_stid.sc_type = NFS4_REVOKED_DELEG_STID;
2d4a532d
JL
748 spin_lock(&clp->cl_lock);
749 list_add(&dp->dl_recall_lru, &clp->cl_revoked);
750 spin_unlock(&clp->cl_lock);
3bd64a5b
BF
751 }
752}
753
1da177e4
LT
754/*
755 * SETCLIENTID state
756 */
757
ddc04c41
BF
758static unsigned int clientid_hashval(u32 id)
759{
760 return id & CLIENT_HASH_MASK;
761}
762
763static unsigned int clientstr_hashval(const char *name)
764{
765 return opaque_hashval(name, 8) & CLIENT_HASH_MASK;
766}
767
f9d7562f
BF
768/*
769 * We store the NONE, READ, WRITE, and BOTH bits separately in the
770 * st_{access,deny}_bmap field of the stateid, in order to track not
771 * only what share bits are currently in force, but also what
772 * combinations of share bits previous opens have used. This allows us
773 * to enforce the recommendation of rfc 3530 14.2.19 that the server
774 * return an error if the client attempt to downgrade to a combination
775 * of share bits not explicable by closing some of its previous opens.
776 *
777 * XXX: This enforcement is actually incomplete, since we don't keep
778 * track of access/deny bit combinations; so, e.g., we allow:
779 *
780 * OPEN allow read, deny write
781 * OPEN allow both, deny none
782 * DOWNGRADE allow read, deny none
783 *
784 * which we should reject.
785 */
5ae037e5
JL
786static unsigned int
787bmap_to_share_mode(unsigned long bmap) {
f9d7562f 788 int i;
5ae037e5 789 unsigned int access = 0;
f9d7562f 790
f9d7562f
BF
791 for (i = 1; i < 4; i++) {
792 if (test_bit(i, &bmap))
5ae037e5 793 access |= i;
f9d7562f 794 }
5ae037e5 795 return access;
f9d7562f
BF
796}
797
82c5ff1b
JL
798/* set share access for a given stateid */
799static inline void
800set_access(u32 access, struct nfs4_ol_stateid *stp)
801{
c11c591f
JL
802 unsigned char mask = 1 << access;
803
804 WARN_ON_ONCE(access > NFS4_SHARE_ACCESS_BOTH);
805 stp->st_access_bmap |= mask;
82c5ff1b
JL
806}
807
808/* clear share access for a given stateid */
809static inline void
810clear_access(u32 access, struct nfs4_ol_stateid *stp)
811{
c11c591f
JL
812 unsigned char mask = 1 << access;
813
814 WARN_ON_ONCE(access > NFS4_SHARE_ACCESS_BOTH);
815 stp->st_access_bmap &= ~mask;
82c5ff1b
JL
816}
817
818/* test whether a given stateid has access */
819static inline bool
820test_access(u32 access, struct nfs4_ol_stateid *stp)
821{
c11c591f
JL
822 unsigned char mask = 1 << access;
823
824 return (bool)(stp->st_access_bmap & mask);
82c5ff1b
JL
825}
826
ce0fc43c
JL
827/* set share deny for a given stateid */
828static inline void
c11c591f 829set_deny(u32 deny, struct nfs4_ol_stateid *stp)
ce0fc43c 830{
c11c591f
JL
831 unsigned char mask = 1 << deny;
832
833 WARN_ON_ONCE(deny > NFS4_SHARE_DENY_BOTH);
834 stp->st_deny_bmap |= mask;
ce0fc43c
JL
835}
836
837/* clear share deny for a given stateid */
838static inline void
c11c591f 839clear_deny(u32 deny, struct nfs4_ol_stateid *stp)
ce0fc43c 840{
c11c591f
JL
841 unsigned char mask = 1 << deny;
842
843 WARN_ON_ONCE(deny > NFS4_SHARE_DENY_BOTH);
844 stp->st_deny_bmap &= ~mask;
ce0fc43c
JL
845}
846
847/* test whether a given stateid is denying specific access */
848static inline bool
c11c591f 849test_deny(u32 deny, struct nfs4_ol_stateid *stp)
ce0fc43c 850{
c11c591f
JL
851 unsigned char mask = 1 << deny;
852
853 return (bool)(stp->st_deny_bmap & mask);
f9d7562f
BF
854}
855
856static int nfs4_access_to_omode(u32 access)
857{
8f34a430 858 switch (access & NFS4_SHARE_ACCESS_BOTH) {
f9d7562f
BF
859 case NFS4_SHARE_ACCESS_READ:
860 return O_RDONLY;
861 case NFS4_SHARE_ACCESS_WRITE:
862 return O_WRONLY;
863 case NFS4_SHARE_ACCESS_BOTH:
864 return O_RDWR;
865 }
063b0fb9
BF
866 WARN_ON_ONCE(1);
867 return O_RDONLY;
f9d7562f
BF
868}
869
baeb4ff0
JL
870/*
871 * A stateid that had a deny mode associated with it is being released
872 * or downgraded. Recalculate the deny mode on the file.
873 */
874static void
875recalculate_deny_mode(struct nfs4_file *fp)
876{
877 struct nfs4_ol_stateid *stp;
878
879 spin_lock(&fp->fi_lock);
880 fp->fi_share_deny = 0;
881 list_for_each_entry(stp, &fp->fi_stateids, st_perfile)
882 fp->fi_share_deny |= bmap_to_share_mode(stp->st_deny_bmap);
883 spin_unlock(&fp->fi_lock);
884}
885
886static void
887reset_union_bmap_deny(u32 deny, struct nfs4_ol_stateid *stp)
888{
889 int i;
890 bool change = false;
891
892 for (i = 1; i < 4; i++) {
893 if ((i & deny) != i) {
894 change = true;
895 clear_deny(i, stp);
896 }
897 }
898
899 /* Recalculate per-file deny mode if there was a change */
900 if (change)
11b9164a 901 recalculate_deny_mode(stp->st_stid.sc_file);
baeb4ff0
JL
902}
903
82c5ff1b
JL
904/* release all access and file references for a given stateid */
905static void
906release_all_access(struct nfs4_ol_stateid *stp)
907{
908 int i;
11b9164a 909 struct nfs4_file *fp = stp->st_stid.sc_file;
baeb4ff0
JL
910
911 if (fp && stp->st_deny_bmap != 0)
912 recalculate_deny_mode(fp);
82c5ff1b
JL
913
914 for (i = 1; i < 4; i++) {
915 if (test_access(i, stp))
11b9164a 916 nfs4_file_put_access(stp->st_stid.sc_file, i);
82c5ff1b
JL
917 clear_access(i, stp);
918 }
919}
920
6b180f0b
JL
921static void nfs4_put_stateowner(struct nfs4_stateowner *sop)
922{
a819ecc1
JL
923 struct nfs4_client *clp = sop->so_client;
924
925 might_lock(&clp->cl_lock);
926
927 if (!atomic_dec_and_lock(&sop->so_count, &clp->cl_lock))
6b180f0b 928 return;
8f4b54c5 929 sop->so_ops->so_unhash(sop);
a819ecc1 930 spin_unlock(&clp->cl_lock);
6b180f0b
JL
931 kfree(sop->so_owner.data);
932 sop->so_ops->so_free(sop);
933}
934
4ae098d3 935static void unhash_ol_stateid(struct nfs4_ol_stateid *stp)
529d7b2a 936{
11b9164a 937 struct nfs4_file *fp = stp->st_stid.sc_file;
1d31a253 938
1c755dc1
JL
939 lockdep_assert_held(&stp->st_stateowner->so_client->cl_lock);
940
1d31a253 941 spin_lock(&fp->fi_lock);
529d7b2a 942 list_del(&stp->st_perfile);
1d31a253 943 spin_unlock(&fp->fi_lock);
529d7b2a
BF
944 list_del(&stp->st_perstateowner);
945}
946
6011695d 947static void nfs4_free_ol_stateid(struct nfs4_stid *stid)
529d7b2a 948{
6011695d 949 struct nfs4_ol_stateid *stp = openlockstateid(stid);
4665e2ba 950
6011695d 951 release_all_access(stp);
d3134b10
JL
952 if (stp->st_stateowner)
953 nfs4_put_stateowner(stp->st_stateowner);
6011695d 954 kmem_cache_free(stateid_slab, stid);
529d7b2a
BF
955}
956
b49e084d 957static void nfs4_free_lock_stateid(struct nfs4_stid *stid)
529d7b2a 958{
b49e084d
JL
959 struct nfs4_ol_stateid *stp = openlockstateid(stid);
960 struct nfs4_lockowner *lo = lockowner(stp->st_stateowner);
529d7b2a
BF
961 struct file *file;
962
b49e084d
JL
963 file = find_any_file(stp->st_stid.sc_file);
964 if (file)
965 filp_close(file, (fl_owner_t)lo);
966 nfs4_free_ol_stateid(stid);
967}
968
2c41beb0
JL
969/*
970 * Put the persistent reference to an already unhashed generic stateid, while
971 * holding the cl_lock. If it's the last reference, then put it onto the
972 * reaplist for later destruction.
973 */
974static void put_ol_stateid_locked(struct nfs4_ol_stateid *stp,
975 struct list_head *reaplist)
976{
977 struct nfs4_stid *s = &stp->st_stid;
978 struct nfs4_client *clp = s->sc_client;
979
980 lockdep_assert_held(&clp->cl_lock);
981
982 WARN_ON_ONCE(!list_empty(&stp->st_locks));
983
984 if (!atomic_dec_and_test(&s->sc_count)) {
985 wake_up_all(&close_wq);
986 return;
987 }
988
989 idr_remove(&clp->cl_stateids, s->sc_stateid.si_opaque.so_id);
990 list_add(&stp->st_locks, reaplist);
991}
992
3c1c995c 993static void unhash_lock_stateid(struct nfs4_ol_stateid *stp)
b49e084d 994{
1c755dc1
JL
995 struct nfs4_openowner *oo = openowner(stp->st_openstp->st_stateowner);
996
3c1c995c
JL
997 lockdep_assert_held(&oo->oo_owner.so_client->cl_lock);
998
999 list_del_init(&stp->st_locks);
4ae098d3 1000 unhash_ol_stateid(stp);
6136d2b4 1001 unhash_stid(&stp->st_stid);
3c1c995c
JL
1002}
1003
1004static void release_lock_stateid(struct nfs4_ol_stateid *stp)
1005{
1006 struct nfs4_openowner *oo = openowner(stp->st_openstp->st_stateowner);
1007
1008 spin_lock(&oo->oo_owner.so_client->cl_lock);
1009 unhash_lock_stateid(stp);
1c755dc1 1010 spin_unlock(&oo->oo_owner.so_client->cl_lock);
6011695d 1011 nfs4_put_stid(&stp->st_stid);
529d7b2a
BF
1012}
1013
c58c6610 1014static void unhash_lockowner_locked(struct nfs4_lockowner *lo)
8f4b54c5 1015{
d4f0489f 1016 struct nfs4_client *clp = lo->lo_owner.so_client;
c58c6610 1017
d4f0489f 1018 lockdep_assert_held(&clp->cl_lock);
c58c6610 1019
8f4b54c5
JL
1020 list_del_init(&lo->lo_owner.so_strhash);
1021}
1022
2c41beb0
JL
1023/*
1024 * Free a list of generic stateids that were collected earlier after being
1025 * fully unhashed.
1026 */
1027static void
1028free_ol_stateid_reaplist(struct list_head *reaplist)
1029{
1030 struct nfs4_ol_stateid *stp;
fb94d766 1031 struct nfs4_file *fp;
2c41beb0
JL
1032
1033 might_sleep();
1034
1035 while (!list_empty(reaplist)) {
1036 stp = list_first_entry(reaplist, struct nfs4_ol_stateid,
1037 st_locks);
1038 list_del(&stp->st_locks);
fb94d766 1039 fp = stp->st_stid.sc_file;
2c41beb0 1040 stp->st_stid.sc_free(&stp->st_stid);
fb94d766
KM
1041 if (fp)
1042 put_nfs4_file(fp);
2c41beb0
JL
1043 }
1044}
1045
3c1c995c 1046static void release_lockowner(struct nfs4_lockowner *lo)
529d7b2a 1047{
d4f0489f 1048 struct nfs4_client *clp = lo->lo_owner.so_client;
dcef0413 1049 struct nfs4_ol_stateid *stp;
3c1c995c 1050 struct list_head reaplist;
529d7b2a 1051
3c1c995c 1052 INIT_LIST_HEAD(&reaplist);
c58c6610 1053
3c1c995c
JL
1054 spin_lock(&clp->cl_lock);
1055 unhash_lockowner_locked(lo);
fe0750e5
BF
1056 while (!list_empty(&lo->lo_owner.so_stateids)) {
1057 stp = list_first_entry(&lo->lo_owner.so_stateids,
dcef0413 1058 struct nfs4_ol_stateid, st_perstateowner);
3c1c995c 1059 unhash_lock_stateid(stp);
2c41beb0 1060 put_ol_stateid_locked(stp, &reaplist);
529d7b2a 1061 }
d4f0489f 1062 spin_unlock(&clp->cl_lock);
2c41beb0 1063 free_ol_stateid_reaplist(&reaplist);
6b180f0b 1064 nfs4_put_stateowner(&lo->lo_owner);
529d7b2a
BF
1065}
1066
d83017f9
JL
1067static void release_open_stateid_locks(struct nfs4_ol_stateid *open_stp,
1068 struct list_head *reaplist)
3c87b9b7
TM
1069{
1070 struct nfs4_ol_stateid *stp;
1071
1072 while (!list_empty(&open_stp->st_locks)) {
1073 stp = list_entry(open_stp->st_locks.next,
1074 struct nfs4_ol_stateid, st_locks);
d83017f9
JL
1075 unhash_lock_stateid(stp);
1076 put_ol_stateid_locked(stp, reaplist);
529d7b2a
BF
1077 }
1078}
1079
d83017f9
JL
1080static void unhash_open_stateid(struct nfs4_ol_stateid *stp,
1081 struct list_head *reaplist)
2283963f 1082{
2c41beb0
JL
1083 lockdep_assert_held(&stp->st_stid.sc_client->cl_lock);
1084
4ae098d3 1085 unhash_ol_stateid(stp);
d83017f9 1086 release_open_stateid_locks(stp, reaplist);
38c387b5
BF
1087}
1088
1089static void release_open_stateid(struct nfs4_ol_stateid *stp)
1090{
2c41beb0
JL
1091 LIST_HEAD(reaplist);
1092
1093 spin_lock(&stp->st_stid.sc_client->cl_lock);
d83017f9 1094 unhash_open_stateid(stp, &reaplist);
2c41beb0
JL
1095 put_ol_stateid_locked(stp, &reaplist);
1096 spin_unlock(&stp->st_stid.sc_client->cl_lock);
1097 free_ol_stateid_reaplist(&reaplist);
2283963f
BF
1098}
1099
7ffb5880 1100static void unhash_openowner_locked(struct nfs4_openowner *oo)
f1d110ca 1101{
d4f0489f 1102 struct nfs4_client *clp = oo->oo_owner.so_client;
7ffb5880 1103
d4f0489f 1104 lockdep_assert_held(&clp->cl_lock);
7ffb5880 1105
8f4b54c5
JL
1106 list_del_init(&oo->oo_owner.so_strhash);
1107 list_del_init(&oo->oo_perclient);
f1d110ca
BF
1108}
1109
f7a4d872
BF
1110static void release_last_closed_stateid(struct nfs4_openowner *oo)
1111{
217526e7
JL
1112 struct nfsd_net *nn = net_generic(oo->oo_owner.so_client->net,
1113 nfsd_net_id);
1114 struct nfs4_ol_stateid *s;
f7a4d872 1115
217526e7
JL
1116 spin_lock(&nn->client_lock);
1117 s = oo->oo_last_closed_stid;
f7a4d872 1118 if (s) {
d3134b10 1119 list_del_init(&oo->oo_close_lru);
f7a4d872
BF
1120 oo->oo_last_closed_stid = NULL;
1121 }
217526e7
JL
1122 spin_unlock(&nn->client_lock);
1123 if (s)
1124 nfs4_put_stid(&s->st_stid);
f7a4d872
BF
1125}
1126
2c41beb0 1127static void release_openowner(struct nfs4_openowner *oo)
8f4b54c5
JL
1128{
1129 struct nfs4_ol_stateid *stp;
d4f0489f 1130 struct nfs4_client *clp = oo->oo_owner.so_client;
2c41beb0 1131 struct list_head reaplist;
7ffb5880 1132
2c41beb0 1133 INIT_LIST_HEAD(&reaplist);
8f4b54c5 1134
2c41beb0
JL
1135 spin_lock(&clp->cl_lock);
1136 unhash_openowner_locked(oo);
8f4b54c5
JL
1137 while (!list_empty(&oo->oo_owner.so_stateids)) {
1138 stp = list_first_entry(&oo->oo_owner.so_stateids,
1139 struct nfs4_ol_stateid, st_perstateowner);
d83017f9 1140 unhash_open_stateid(stp, &reaplist);
2c41beb0 1141 put_ol_stateid_locked(stp, &reaplist);
8f4b54c5 1142 }
d4f0489f 1143 spin_unlock(&clp->cl_lock);
2c41beb0 1144 free_ol_stateid_reaplist(&reaplist);
f7a4d872 1145 release_last_closed_stateid(oo);
6b180f0b 1146 nfs4_put_stateowner(&oo->oo_owner);
f1d110ca
BF
1147}
1148
5282fd72
ME
1149static inline int
1150hash_sessionid(struct nfs4_sessionid *sessionid)
1151{
1152 struct nfsd4_sessionid *sid = (struct nfsd4_sessionid *)sessionid;
1153
1154 return sid->sequence % SESSION_HASH_SIZE;
1155}
1156
8f199b82 1157#ifdef NFSD_DEBUG
5282fd72
ME
1158static inline void
1159dump_sessionid(const char *fn, struct nfs4_sessionid *sessionid)
1160{
1161 u32 *ptr = (u32 *)(&sessionid->data[0]);
1162 dprintk("%s: %u:%u:%u:%u\n", fn, ptr[0], ptr[1], ptr[2], ptr[3]);
1163}
8f199b82
TM
1164#else
1165static inline void
1166dump_sessionid(const char *fn, struct nfs4_sessionid *sessionid)
1167{
1168}
1169#endif
1170
9411b1d4
BF
1171/*
1172 * Bump the seqid on cstate->replay_owner, and clear replay_owner if it
1173 * won't be used for replay.
1174 */
1175void nfsd4_bump_seqid(struct nfsd4_compound_state *cstate, __be32 nfserr)
1176{
1177 struct nfs4_stateowner *so = cstate->replay_owner;
1178
1179 if (nfserr == nfserr_replay_me)
1180 return;
1181
1182 if (!seqid_mutating_err(ntohl(nfserr))) {
58fb12e6 1183 nfsd4_cstate_clear_replay(cstate);
9411b1d4
BF
1184 return;
1185 }
1186 if (!so)
1187 return;
1188 if (so->so_is_open_owner)
1189 release_last_closed_stateid(openowner(so));
1190 so->so_seqid++;
1191 return;
1192}
5282fd72 1193
ec6b5d7b
AA
1194static void
1195gen_sessionid(struct nfsd4_session *ses)
1196{
1197 struct nfs4_client *clp = ses->se_client;
1198 struct nfsd4_sessionid *sid;
1199
1200 sid = (struct nfsd4_sessionid *)ses->se_sessionid.data;
1201 sid->clientid = clp->cl_clientid;
1202 sid->sequence = current_sessionid++;
1203 sid->reserved = 0;
1204}
1205
1206/*
a649637c
AA
1207 * The protocol defines ca_maxresponssize_cached to include the size of
1208 * the rpc header, but all we need to cache is the data starting after
1209 * the end of the initial SEQUENCE operation--the rest we regenerate
1210 * each time. Therefore we can advertise a ca_maxresponssize_cached
1211 * value that is the number of bytes in our cache plus a few additional
1212 * bytes. In order to stay on the safe side, and not promise more than
1213 * we can cache, those additional bytes must be the minimum possible: 24
1214 * bytes of rpc header (xid through accept state, with AUTH_NULL
1215 * verifier), 12 for the compound header (with zero-length tag), and 44
1216 * for the SEQUENCE op response:
1217 */
1218#define NFSD_MIN_HDR_SEQ_SZ (24 + 12 + 44)
1219
557ce264
AA
1220static void
1221free_session_slots(struct nfsd4_session *ses)
1222{
1223 int i;
1224
1225 for (i = 0; i < ses->se_fchannel.maxreqs; i++)
1226 kfree(ses->se_slots[i]);
1227}
1228
a649637c 1229/*
efe0cb6d
BF
1230 * We don't actually need to cache the rpc and session headers, so we
1231 * can allocate a little less for each slot:
1232 */
55c760cf 1233static inline u32 slot_bytes(struct nfsd4_channel_attrs *ca)
efe0cb6d 1234{
55c760cf 1235 u32 size;
efe0cb6d 1236
55c760cf
BF
1237 if (ca->maxresp_cached < NFSD_MIN_HDR_SEQ_SZ)
1238 size = 0;
1239 else
1240 size = ca->maxresp_cached - NFSD_MIN_HDR_SEQ_SZ;
1241 return size + sizeof(struct nfsd4_slot);
5b6feee9 1242}
ec6b5d7b 1243
5b6feee9
BF
1244/*
1245 * XXX: If we run out of reserved DRC memory we could (up to a point)
a649637c 1246 * re-negotiate active sessions and reduce their slot usage to make
42b2aa86 1247 * room for new connections. For now we just fail the create session.
ec6b5d7b 1248 */
55c760cf 1249static u32 nfsd4_get_drc_mem(struct nfsd4_channel_attrs *ca)
ec6b5d7b 1250{
55c760cf
BF
1251 u32 slotsize = slot_bytes(ca);
1252 u32 num = ca->maxreqs;
5b6feee9 1253 int avail;
ec6b5d7b 1254
5b6feee9 1255 spin_lock(&nfsd_drc_lock);
697ce9be
ZY
1256 avail = min((unsigned long)NFSD_MAX_MEM_PER_SESSION,
1257 nfsd_drc_max_mem - nfsd_drc_mem_used);
5b6feee9
BF
1258 num = min_t(int, num, avail / slotsize);
1259 nfsd_drc_mem_used += num * slotsize;
1260 spin_unlock(&nfsd_drc_lock);
ec6b5d7b 1261
5b6feee9
BF
1262 return num;
1263}
ec6b5d7b 1264
55c760cf 1265static void nfsd4_put_drc_mem(struct nfsd4_channel_attrs *ca)
5b6feee9 1266{
55c760cf
BF
1267 int slotsize = slot_bytes(ca);
1268
4bd9b0f4 1269 spin_lock(&nfsd_drc_lock);
55c760cf 1270 nfsd_drc_mem_used -= slotsize * ca->maxreqs;
4bd9b0f4 1271 spin_unlock(&nfsd_drc_lock);
5b6feee9 1272}
ec6b5d7b 1273
60810e54
KM
1274static struct nfsd4_session *alloc_session(struct nfsd4_channel_attrs *fattrs,
1275 struct nfsd4_channel_attrs *battrs)
5b6feee9 1276{
60810e54
KM
1277 int numslots = fattrs->maxreqs;
1278 int slotsize = slot_bytes(fattrs);
5b6feee9
BF
1279 struct nfsd4_session *new;
1280 int mem, i;
a649637c 1281
5b6feee9
BF
1282 BUILD_BUG_ON(NFSD_MAX_SLOTS_PER_SESSION * sizeof(struct nfsd4_slot *)
1283 + sizeof(struct nfsd4_session) > PAGE_SIZE);
1284 mem = numslots * sizeof(struct nfsd4_slot *);
ec6b5d7b 1285
5b6feee9
BF
1286 new = kzalloc(sizeof(*new) + mem, GFP_KERNEL);
1287 if (!new)
1288 return NULL;
557ce264 1289 /* allocate each struct nfsd4_slot and data cache in one piece */
5b6feee9 1290 for (i = 0; i < numslots; i++) {
55c760cf 1291 new->se_slots[i] = kzalloc(slotsize, GFP_KERNEL);
5b6feee9 1292 if (!new->se_slots[i])
557ce264 1293 goto out_free;
557ce264 1294 }
60810e54
KM
1295
1296 memcpy(&new->se_fchannel, fattrs, sizeof(struct nfsd4_channel_attrs));
1297 memcpy(&new->se_bchannel, battrs, sizeof(struct nfsd4_channel_attrs));
1298
5b6feee9
BF
1299 return new;
1300out_free:
1301 while (i--)
1302 kfree(new->se_slots[i]);
1303 kfree(new);
1304 return NULL;
ec6b5d7b
AA
1305}
1306
19cf5c02
BF
1307static void free_conn(struct nfsd4_conn *c)
1308{
1309 svc_xprt_put(c->cn_xprt);
1310 kfree(c);
1311}
ec6b5d7b 1312
19cf5c02
BF
1313static void nfsd4_conn_lost(struct svc_xpt_user *u)
1314{
1315 struct nfsd4_conn *c = container_of(u, struct nfsd4_conn, cn_xpt_user);
1316 struct nfs4_client *clp = c->cn_session->se_client;
ec6b5d7b 1317
19cf5c02
BF
1318 spin_lock(&clp->cl_lock);
1319 if (!list_empty(&c->cn_persession)) {
1320 list_del(&c->cn_persession);
1321 free_conn(c);
1322 }
eea49806 1323 nfsd4_probe_callback(clp);
2e4b7239 1324 spin_unlock(&clp->cl_lock);
19cf5c02 1325}
ec6b5d7b 1326
d29c374c 1327static struct nfsd4_conn *alloc_conn(struct svc_rqst *rqstp, u32 flags)
c7662518 1328{
c7662518 1329 struct nfsd4_conn *conn;
ec6b5d7b 1330
c7662518
BF
1331 conn = kmalloc(sizeof(struct nfsd4_conn), GFP_KERNEL);
1332 if (!conn)
db90681d 1333 return NULL;
c7662518
BF
1334 svc_xprt_get(rqstp->rq_xprt);
1335 conn->cn_xprt = rqstp->rq_xprt;
d29c374c 1336 conn->cn_flags = flags;
db90681d
BF
1337 INIT_LIST_HEAD(&conn->cn_xpt_user.list);
1338 return conn;
1339}
a649637c 1340
328ead28
BF
1341static void __nfsd4_hash_conn(struct nfsd4_conn *conn, struct nfsd4_session *ses)
1342{
1343 conn->cn_session = ses;
1344 list_add(&conn->cn_persession, &ses->se_conns);
ec6b5d7b
AA
1345}
1346
db90681d 1347static void nfsd4_hash_conn(struct nfsd4_conn *conn, struct nfsd4_session *ses)
557ce264 1348{
db90681d 1349 struct nfs4_client *clp = ses->se_client;
557ce264 1350
c7662518 1351 spin_lock(&clp->cl_lock);
328ead28 1352 __nfsd4_hash_conn(conn, ses);
c7662518 1353 spin_unlock(&clp->cl_lock);
557ce264
AA
1354}
1355
21b75b01 1356static int nfsd4_register_conn(struct nfsd4_conn *conn)
efe0cb6d 1357{
19cf5c02 1358 conn->cn_xpt_user.callback = nfsd4_conn_lost;
21b75b01 1359 return register_xpt_user(conn->cn_xprt, &conn->cn_xpt_user);
efe0cb6d
BF
1360}
1361
e1ff371f 1362static void nfsd4_init_conn(struct svc_rqst *rqstp, struct nfsd4_conn *conn, struct nfsd4_session *ses)
ec6b5d7b 1363{
21b75b01 1364 int ret;
ec6b5d7b 1365
db90681d 1366 nfsd4_hash_conn(conn, ses);
21b75b01
BF
1367 ret = nfsd4_register_conn(conn);
1368 if (ret)
1369 /* oops; xprt is already down: */
1370 nfsd4_conn_lost(&conn->cn_xpt_user);
57a37144
BF
1371 /* We may have gained or lost a callback channel: */
1372 nfsd4_probe_callback_sync(ses->se_client);
c7662518 1373}
ec6b5d7b 1374
e1ff371f 1375static struct nfsd4_conn *alloc_conn_from_crses(struct svc_rqst *rqstp, struct nfsd4_create_session *cses)
1d1bc8f2
BF
1376{
1377 u32 dir = NFS4_CDFC4_FORE;
1378
e1ff371f 1379 if (cses->flags & SESSION4_BACK_CHAN)
1d1bc8f2 1380 dir |= NFS4_CDFC4_BACK;
e1ff371f 1381 return alloc_conn(rqstp, dir);
1d1bc8f2
BF
1382}
1383
1384/* must be called under client_lock */
19cf5c02 1385static void nfsd4_del_conns(struct nfsd4_session *s)
c7662518 1386{
19cf5c02
BF
1387 struct nfs4_client *clp = s->se_client;
1388 struct nfsd4_conn *c;
ec6b5d7b 1389
19cf5c02
BF
1390 spin_lock(&clp->cl_lock);
1391 while (!list_empty(&s->se_conns)) {
1392 c = list_first_entry(&s->se_conns, struct nfsd4_conn, cn_persession);
1393 list_del_init(&c->cn_persession);
1394 spin_unlock(&clp->cl_lock);
557ce264 1395
19cf5c02
BF
1396 unregister_xpt_user(c->cn_xprt, &c->cn_xpt_user);
1397 free_conn(c);
ec6b5d7b 1398
19cf5c02
BF
1399 spin_lock(&clp->cl_lock);
1400 }
1401 spin_unlock(&clp->cl_lock);
c7662518 1402}
ec6b5d7b 1403
1377b69e
BF
1404static void __free_session(struct nfsd4_session *ses)
1405{
1377b69e
BF
1406 free_session_slots(ses);
1407 kfree(ses);
1408}
1409
66b2b9b2 1410static void free_session(struct nfsd4_session *ses)
c7662518 1411{
19cf5c02 1412 nfsd4_del_conns(ses);
55c760cf 1413 nfsd4_put_drc_mem(&ses->se_fchannel);
1377b69e 1414 __free_session(ses);
c7662518
BF
1415}
1416
135ae827 1417static void init_session(struct svc_rqst *rqstp, struct nfsd4_session *new, struct nfs4_client *clp, struct nfsd4_create_session *cses)
a827bcb2 1418{
a827bcb2 1419 int idx;
1872de0e 1420 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
a827bcb2 1421
ec6b5d7b
AA
1422 new->se_client = clp;
1423 gen_sessionid(new);
ec6b5d7b 1424
c7662518
BF
1425 INIT_LIST_HEAD(&new->se_conns);
1426
ac7c46f2 1427 new->se_cb_seq_nr = 1;
ec6b5d7b 1428 new->se_flags = cses->flags;
8b5ce5cd 1429 new->se_cb_prog = cses->callback_prog;
c6bb3ca2 1430 new->se_cb_sec = cses->cb_sec;
66b2b9b2 1431 atomic_set(&new->se_ref, 0);
5b6feee9 1432 idx = hash_sessionid(&new->se_sessionid);
1872de0e 1433 list_add(&new->se_hash, &nn->sessionid_hashtbl[idx]);
4c649378 1434 spin_lock(&clp->cl_lock);
ec6b5d7b 1435 list_add(&new->se_perclnt, &clp->cl_sessions);
4c649378 1436 spin_unlock(&clp->cl_lock);
60810e54 1437
dcbeaa68 1438 if (cses->flags & SESSION4_BACK_CHAN) {
edd76786 1439 struct sockaddr *sa = svc_addr(rqstp);
dcbeaa68
BF
1440 /*
1441 * This is a little silly; with sessions there's no real
1442 * use for the callback address. Use the peer address
1443 * as a reasonable default for now, but consider fixing
1444 * the rpc client not to require an address in the
1445 * future:
1446 */
edd76786
BF
1447 rpc_copy_addr((struct sockaddr *)&clp->cl_cb_conn.cb_addr, sa);
1448 clp->cl_cb_conn.cb_addrlen = svc_addr_len(sa);
edd76786 1449 }
ec6b5d7b
AA
1450}
1451
9089f1b4 1452/* caller must hold client_lock */
5282fd72 1453static struct nfsd4_session *
d4e19e70 1454__find_in_sessionid_hashtbl(struct nfs4_sessionid *sessionid, struct net *net)
5282fd72
ME
1455{
1456 struct nfsd4_session *elem;
1457 int idx;
1872de0e 1458 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
5282fd72 1459
0a880a28
TM
1460 lockdep_assert_held(&nn->client_lock);
1461
5282fd72
ME
1462 dump_sessionid(__func__, sessionid);
1463 idx = hash_sessionid(sessionid);
5282fd72 1464 /* Search in the appropriate list */
1872de0e 1465 list_for_each_entry(elem, &nn->sessionid_hashtbl[idx], se_hash) {
5282fd72
ME
1466 if (!memcmp(elem->se_sessionid.data, sessionid->data,
1467 NFS4_MAX_SESSIONID_LEN)) {
1468 return elem;
1469 }
1470 }
1471
1472 dprintk("%s: session not found\n", __func__);
1473 return NULL;
1474}
1475
d4e19e70
TM
1476static struct nfsd4_session *
1477find_in_sessionid_hashtbl(struct nfs4_sessionid *sessionid, struct net *net,
1478 __be32 *ret)
1479{
1480 struct nfsd4_session *session;
1481 __be32 status = nfserr_badsession;
1482
1483 session = __find_in_sessionid_hashtbl(sessionid, net);
1484 if (!session)
1485 goto out;
1486 status = nfsd4_get_session_locked(session);
1487 if (status)
1488 session = NULL;
1489out:
1490 *ret = status;
1491 return session;
1492}
1493
9089f1b4 1494/* caller must hold client_lock */
7116ed6b 1495static void
5282fd72 1496unhash_session(struct nfsd4_session *ses)
7116ed6b 1497{
0a880a28
TM
1498 struct nfs4_client *clp = ses->se_client;
1499 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
1500
1501 lockdep_assert_held(&nn->client_lock);
1502
7116ed6b 1503 list_del(&ses->se_hash);
4c649378 1504 spin_lock(&ses->se_client->cl_lock);
7116ed6b 1505 list_del(&ses->se_perclnt);
4c649378 1506 spin_unlock(&ses->se_client->cl_lock);
5282fd72
ME
1507}
1508
1da177e4
LT
1509/* SETCLIENTID and SETCLIENTID_CONFIRM Helper functions */
1510static int
2c142baa 1511STALE_CLIENTID(clientid_t *clid, struct nfsd_net *nn)
1da177e4 1512{
2c142baa 1513 if (clid->cl_boot == nn->boot_time)
1da177e4 1514 return 0;
60adfc50 1515 dprintk("NFSD stale clientid (%08x/%08x) boot_time %08lx\n",
2c142baa 1516 clid->cl_boot, clid->cl_id, nn->boot_time);
1da177e4
LT
1517 return 1;
1518}
1519
1520/*
1521 * XXX Should we use a slab cache ?
1522 * This type of memory management is somewhat inefficient, but we use it
1523 * anyway since SETCLIENTID is not a common operation.
1524 */
35bba9a3 1525static struct nfs4_client *alloc_client(struct xdr_netobj name)
1da177e4
LT
1526{
1527 struct nfs4_client *clp;
d4f0489f 1528 int i;
1da177e4 1529
35bba9a3
BF
1530 clp = kzalloc(sizeof(struct nfs4_client), GFP_KERNEL);
1531 if (clp == NULL)
1532 return NULL;
67114fe6 1533 clp->cl_name.data = kmemdup(name.data, name.len, GFP_KERNEL);
d4f0489f
TM
1534 if (clp->cl_name.data == NULL)
1535 goto err_no_name;
1536 clp->cl_ownerstr_hashtbl = kmalloc(sizeof(struct list_head) *
1537 OWNER_HASH_SIZE, GFP_KERNEL);
1538 if (!clp->cl_ownerstr_hashtbl)
1539 goto err_no_hashtbl;
1540 for (i = 0; i < OWNER_HASH_SIZE; i++)
1541 INIT_LIST_HEAD(&clp->cl_ownerstr_hashtbl[i]);
35bba9a3 1542 clp->cl_name.len = name.len;
5694c93e
TM
1543 INIT_LIST_HEAD(&clp->cl_sessions);
1544 idr_init(&clp->cl_stateids);
1545 atomic_set(&clp->cl_refcount, 0);
1546 clp->cl_cb_state = NFSD4_CB_UNKNOWN;
1547 INIT_LIST_HEAD(&clp->cl_idhash);
1548 INIT_LIST_HEAD(&clp->cl_openowners);
1549 INIT_LIST_HEAD(&clp->cl_delegations);
1550 INIT_LIST_HEAD(&clp->cl_lru);
1551 INIT_LIST_HEAD(&clp->cl_callbacks);
1552 INIT_LIST_HEAD(&clp->cl_revoked);
1553 spin_lock_init(&clp->cl_lock);
1554 rpc_init_wait_queue(&clp->cl_cb_waitq, "Backchannel slot table");
1da177e4 1555 return clp;
d4f0489f
TM
1556err_no_hashtbl:
1557 kfree(clp->cl_name.data);
1558err_no_name:
1559 kfree(clp);
1560 return NULL;
1da177e4
LT
1561}
1562
4dd86e15 1563static void
1da177e4
LT
1564free_client(struct nfs4_client *clp)
1565{
792c95dd
BF
1566 while (!list_empty(&clp->cl_sessions)) {
1567 struct nfsd4_session *ses;
1568 ses = list_entry(clp->cl_sessions.next, struct nfsd4_session,
1569 se_perclnt);
1570 list_del(&ses->se_perclnt);
66b2b9b2
BF
1571 WARN_ON_ONCE(atomic_read(&ses->se_ref));
1572 free_session(ses);
792c95dd 1573 }
4cb57e30 1574 rpc_destroy_wait_queue(&clp->cl_cb_waitq);
03a4e1f6 1575 free_svc_cred(&clp->cl_cred);
d4f0489f 1576 kfree(clp->cl_ownerstr_hashtbl);
1da177e4 1577 kfree(clp->cl_name.data);
2d32b29a 1578 idr_destroy(&clp->cl_stateids);
1da177e4
LT
1579 kfree(clp);
1580}
1581
84d38ac9 1582/* must be called under the client_lock */
4beb345b 1583static void
84d38ac9
BH
1584unhash_client_locked(struct nfs4_client *clp)
1585{
4beb345b 1586 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
792c95dd
BF
1587 struct nfsd4_session *ses;
1588
0a880a28
TM
1589 lockdep_assert_held(&nn->client_lock);
1590
4beb345b
TM
1591 /* Mark the client as expired! */
1592 clp->cl_time = 0;
1593 /* Make it invisible */
1594 if (!list_empty(&clp->cl_idhash)) {
1595 list_del_init(&clp->cl_idhash);
1596 if (test_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags))
1597 rb_erase(&clp->cl_namenode, &nn->conf_name_tree);
1598 else
1599 rb_erase(&clp->cl_namenode, &nn->unconf_name_tree);
1600 }
1601 list_del_init(&clp->cl_lru);
4c649378 1602 spin_lock(&clp->cl_lock);
792c95dd
BF
1603 list_for_each_entry(ses, &clp->cl_sessions, se_perclnt)
1604 list_del_init(&ses->se_hash);
4c649378 1605 spin_unlock(&clp->cl_lock);
84d38ac9
BH
1606}
1607
1da177e4 1608static void
4beb345b
TM
1609unhash_client(struct nfs4_client *clp)
1610{
1611 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
1612
1613 spin_lock(&nn->client_lock);
1614 unhash_client_locked(clp);
1615 spin_unlock(&nn->client_lock);
1616}
1617
97403d95
JL
1618static __be32 mark_client_expired_locked(struct nfs4_client *clp)
1619{
1620 if (atomic_read(&clp->cl_refcount))
1621 return nfserr_jukebox;
1622 unhash_client_locked(clp);
1623 return nfs_ok;
1624}
1625
4beb345b
TM
1626static void
1627__destroy_client(struct nfs4_client *clp)
1da177e4 1628{
fe0750e5 1629 struct nfs4_openowner *oo;
1da177e4 1630 struct nfs4_delegation *dp;
1da177e4
LT
1631 struct list_head reaplist;
1632
1da177e4 1633 INIT_LIST_HEAD(&reaplist);
cdc97505 1634 spin_lock(&state_lock);
ea1da636
N
1635 while (!list_empty(&clp->cl_delegations)) {
1636 dp = list_entry(clp->cl_delegations.next, struct nfs4_delegation, dl_perclnt);
42690676
JL
1637 unhash_delegation_locked(dp);
1638 list_add(&dp->dl_recall_lru, &reaplist);
1da177e4 1639 }
cdc97505 1640 spin_unlock(&state_lock);
1da177e4
LT
1641 while (!list_empty(&reaplist)) {
1642 dp = list_entry(reaplist.next, struct nfs4_delegation, dl_recall_lru);
42690676 1643 list_del_init(&dp->dl_recall_lru);
afbda402 1644 nfs4_put_deleg_lease(dp->dl_stid.sc_file);
6011695d 1645 nfs4_put_stid(&dp->dl_stid);
1da177e4 1646 }
2d4a532d 1647 while (!list_empty(&clp->cl_revoked)) {
956c4fee 1648 dp = list_entry(reaplist.next, struct nfs4_delegation, dl_recall_lru);
2d4a532d 1649 list_del_init(&dp->dl_recall_lru);
6011695d 1650 nfs4_put_stid(&dp->dl_stid);
956c4fee 1651 }
ea1da636 1652 while (!list_empty(&clp->cl_openowners)) {
fe0750e5 1653 oo = list_entry(clp->cl_openowners.next, struct nfs4_openowner, oo_perclient);
d3134b10 1654 atomic_inc(&oo->oo_owner.so_count);
fe0750e5 1655 release_openowner(oo);
1da177e4 1656 }
6ff8da08 1657 nfsd4_shutdown_callback(clp);
84d38ac9
BH
1658 if (clp->cl_cb_conn.cb_xprt)
1659 svc_xprt_put(clp->cl_cb_conn.cb_xprt);
221a6876 1660 free_client(clp);
1da177e4
LT
1661}
1662
4beb345b
TM
1663static void
1664destroy_client(struct nfs4_client *clp)
1665{
1666 unhash_client(clp);
1667 __destroy_client(clp);
1668}
1669
0d22f68f
BF
1670static void expire_client(struct nfs4_client *clp)
1671{
4beb345b 1672 unhash_client(clp);
0d22f68f 1673 nfsd4_client_record_remove(clp);
4beb345b 1674 __destroy_client(clp);
0d22f68f
BF
1675}
1676
35bba9a3
BF
1677static void copy_verf(struct nfs4_client *target, nfs4_verifier *source)
1678{
1679 memcpy(target->cl_verifier.data, source->data,
1680 sizeof(target->cl_verifier.data));
1da177e4
LT
1681}
1682
35bba9a3
BF
1683static void copy_clid(struct nfs4_client *target, struct nfs4_client *source)
1684{
1da177e4
LT
1685 target->cl_clientid.cl_boot = source->cl_clientid.cl_boot;
1686 target->cl_clientid.cl_id = source->cl_clientid.cl_id;
1687}
1688
03a4e1f6 1689static int copy_cred(struct svc_cred *target, struct svc_cred *source)
35bba9a3 1690{
03a4e1f6
BF
1691 if (source->cr_principal) {
1692 target->cr_principal =
1693 kstrdup(source->cr_principal, GFP_KERNEL);
1694 if (target->cr_principal == NULL)
1695 return -ENOMEM;
1696 } else
1697 target->cr_principal = NULL;
d5497fc6 1698 target->cr_flavor = source->cr_flavor;
1da177e4
LT
1699 target->cr_uid = source->cr_uid;
1700 target->cr_gid = source->cr_gid;
1701 target->cr_group_info = source->cr_group_info;
1702 get_group_info(target->cr_group_info);
0dc1531a
BF
1703 target->cr_gss_mech = source->cr_gss_mech;
1704 if (source->cr_gss_mech)
1705 gss_mech_get(source->cr_gss_mech);
03a4e1f6 1706 return 0;
1da177e4
LT
1707}
1708
ac55fdc4
JL
1709static long long
1710compare_blob(const struct xdr_netobj *o1, const struct xdr_netobj *o2)
1711{
1712 long long res;
1713
1714 res = o1->len - o2->len;
1715 if (res)
1716 return res;
1717 return (long long)memcmp(o1->data, o2->data, o1->len);
1718}
1719
35bba9a3 1720static int same_name(const char *n1, const char *n2)
599e0a22 1721{
a55370a3 1722 return 0 == memcmp(n1, n2, HEXDIR_LEN);
1da177e4
LT
1723}
1724
1725static int
599e0a22
BF
1726same_verf(nfs4_verifier *v1, nfs4_verifier *v2)
1727{
1728 return 0 == memcmp(v1->data, v2->data, sizeof(v1->data));
1da177e4
LT
1729}
1730
1731static int
599e0a22
BF
1732same_clid(clientid_t *cl1, clientid_t *cl2)
1733{
1734 return (cl1->cl_boot == cl2->cl_boot) && (cl1->cl_id == cl2->cl_id);
1da177e4
LT
1735}
1736
8fbba96e
BF
1737static bool groups_equal(struct group_info *g1, struct group_info *g2)
1738{
1739 int i;
1740
1741 if (g1->ngroups != g2->ngroups)
1742 return false;
1743 for (i=0; i<g1->ngroups; i++)
6fab8779 1744 if (!gid_eq(GROUP_AT(g1, i), GROUP_AT(g2, i)))
8fbba96e
BF
1745 return false;
1746 return true;
1747}
1748
68eb3508
BF
1749/*
1750 * RFC 3530 language requires clid_inuse be returned when the
1751 * "principal" associated with a requests differs from that previously
1752 * used. We use uid, gid's, and gss principal string as our best
1753 * approximation. We also don't want to allow non-gss use of a client
1754 * established using gss: in theory cr_principal should catch that
1755 * change, but in practice cr_principal can be null even in the gss case
1756 * since gssd doesn't always pass down a principal string.
1757 */
1758static bool is_gss_cred(struct svc_cred *cr)
1759{
1760 /* Is cr_flavor one of the gss "pseudoflavors"?: */
1761 return (cr->cr_flavor > RPC_AUTH_MAXFLAVOR);
1762}
1763
1764
5559b50a 1765static bool
599e0a22
BF
1766same_creds(struct svc_cred *cr1, struct svc_cred *cr2)
1767{
68eb3508 1768 if ((is_gss_cred(cr1) != is_gss_cred(cr2))
6fab8779
EB
1769 || (!uid_eq(cr1->cr_uid, cr2->cr_uid))
1770 || (!gid_eq(cr1->cr_gid, cr2->cr_gid))
8fbba96e
BF
1771 || !groups_equal(cr1->cr_group_info, cr2->cr_group_info))
1772 return false;
1773 if (cr1->cr_principal == cr2->cr_principal)
1774 return true;
1775 if (!cr1->cr_principal || !cr2->cr_principal)
1776 return false;
5559b50a 1777 return 0 == strcmp(cr1->cr_principal, cr2->cr_principal);
1da177e4
LT
1778}
1779
57266a6e
BF
1780static bool svc_rqst_integrity_protected(struct svc_rqst *rqstp)
1781{
1782 struct svc_cred *cr = &rqstp->rq_cred;
1783 u32 service;
1784
c4720591
BF
1785 if (!cr->cr_gss_mech)
1786 return false;
57266a6e
BF
1787 service = gss_pseudoflavor_to_service(cr->cr_gss_mech, cr->cr_flavor);
1788 return service == RPC_GSS_SVC_INTEGRITY ||
1789 service == RPC_GSS_SVC_PRIVACY;
1790}
1791
1792static bool mach_creds_match(struct nfs4_client *cl, struct svc_rqst *rqstp)
1793{
1794 struct svc_cred *cr = &rqstp->rq_cred;
1795
1796 if (!cl->cl_mach_cred)
1797 return true;
1798 if (cl->cl_cred.cr_gss_mech != cr->cr_gss_mech)
1799 return false;
1800 if (!svc_rqst_integrity_protected(rqstp))
1801 return false;
1802 if (!cr->cr_principal)
1803 return false;
1804 return 0 == strcmp(cl->cl_cred.cr_principal, cr->cr_principal);
1805}
1806
294ac32e 1807static void gen_confirm(struct nfs4_client *clp, struct nfsd_net *nn)
deda2faa 1808{
ab4684d1 1809 __be32 verf[2];
1da177e4 1810
f419992c
JL
1811 /*
1812 * This is opaque to client, so no need to byte-swap. Use
1813 * __force to keep sparse happy
1814 */
1815 verf[0] = (__force __be32)get_seconds();
294ac32e 1816 verf[1] = (__force __be32)nn->clientid_counter;
ab4684d1 1817 memcpy(clp->cl_confirm.data, verf, sizeof(clp->cl_confirm.data));
1da177e4
LT
1818}
1819
294ac32e
JL
1820static void gen_clid(struct nfs4_client *clp, struct nfsd_net *nn)
1821{
1822 clp->cl_clientid.cl_boot = nn->boot_time;
1823 clp->cl_clientid.cl_id = nn->clientid_counter++;
1824 gen_confirm(clp, nn);
1825}
1826
4770d722
JL
1827static struct nfs4_stid *
1828find_stateid_locked(struct nfs4_client *cl, stateid_t *t)
4581d140 1829{
3abdb607
BF
1830 struct nfs4_stid *ret;
1831
1832 ret = idr_find(&cl->cl_stateids, t->si_opaque.so_id);
1833 if (!ret || !ret->sc_type)
1834 return NULL;
1835 return ret;
4d71ab87
BF
1836}
1837
4770d722
JL
1838static struct nfs4_stid *
1839find_stateid_by_type(struct nfs4_client *cl, stateid_t *t, char typemask)
f459e453
BF
1840{
1841 struct nfs4_stid *s;
4d71ab87 1842
4770d722
JL
1843 spin_lock(&cl->cl_lock);
1844 s = find_stateid_locked(cl, t);
2d3f9668
TM
1845 if (s != NULL) {
1846 if (typemask & s->sc_type)
1847 atomic_inc(&s->sc_count);
1848 else
1849 s = NULL;
1850 }
4770d722
JL
1851 spin_unlock(&cl->cl_lock);
1852 return s;
4581d140
BF
1853}
1854
2216d449 1855static struct nfs4_client *create_client(struct xdr_netobj name,
b09333c4
RL
1856 struct svc_rqst *rqstp, nfs4_verifier *verf)
1857{
1858 struct nfs4_client *clp;
1859 struct sockaddr *sa = svc_addr(rqstp);
03a4e1f6 1860 int ret;
c212cecf 1861 struct net *net = SVC_NET(rqstp);
b09333c4
RL
1862
1863 clp = alloc_client(name);
1864 if (clp == NULL)
1865 return NULL;
1866
03a4e1f6
BF
1867 ret = copy_cred(&clp->cl_cred, &rqstp->rq_cred);
1868 if (ret) {
03a4e1f6 1869 free_client(clp);
03a4e1f6 1870 return NULL;
b09333c4 1871 }
02e1215f 1872 INIT_WORK(&clp->cl_cb_null.cb_work, nfsd4_run_cb_null);
07cd4909 1873 clp->cl_time = get_seconds();
b09333c4 1874 clear_bit(0, &clp->cl_cb_slot_busy);
b09333c4
RL
1875 copy_verf(clp, verf);
1876 rpc_copy_addr((struct sockaddr *) &clp->cl_addr, sa);
edd76786 1877 clp->cl_cb_session = NULL;
c212cecf 1878 clp->net = net;
b09333c4
RL
1879 return clp;
1880}
1881
fd39ca9a 1882static void
ac55fdc4
JL
1883add_clp_to_name_tree(struct nfs4_client *new_clp, struct rb_root *root)
1884{
1885 struct rb_node **new = &(root->rb_node), *parent = NULL;
1886 struct nfs4_client *clp;
1887
1888 while (*new) {
1889 clp = rb_entry(*new, struct nfs4_client, cl_namenode);
1890 parent = *new;
1891
1892 if (compare_blob(&clp->cl_name, &new_clp->cl_name) > 0)
1893 new = &((*new)->rb_left);
1894 else
1895 new = &((*new)->rb_right);
1896 }
1897
1898 rb_link_node(&new_clp->cl_namenode, parent, new);
1899 rb_insert_color(&new_clp->cl_namenode, root);
1900}
1901
1902static struct nfs4_client *
1903find_clp_in_name_tree(struct xdr_netobj *name, struct rb_root *root)
1904{
1905 long long cmp;
1906 struct rb_node *node = root->rb_node;
1907 struct nfs4_client *clp;
1908
1909 while (node) {
1910 clp = rb_entry(node, struct nfs4_client, cl_namenode);
1911 cmp = compare_blob(&clp->cl_name, name);
1912 if (cmp > 0)
1913 node = node->rb_left;
1914 else if (cmp < 0)
1915 node = node->rb_right;
1916 else
1917 return clp;
1918 }
1919 return NULL;
1920}
1921
1922static void
1923add_to_unconfirmed(struct nfs4_client *clp)
1da177e4
LT
1924{
1925 unsigned int idhashval;
0a7ec377 1926 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
1da177e4 1927
0a880a28
TM
1928 lockdep_assert_held(&nn->client_lock);
1929
ac55fdc4 1930 clear_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags);
a99454aa 1931 add_clp_to_name_tree(clp, &nn->unconf_name_tree);
1da177e4 1932 idhashval = clientid_hashval(clp->cl_clientid.cl_id);
0a7ec377 1933 list_add(&clp->cl_idhash, &nn->unconf_id_hashtbl[idhashval]);
3dbacee6 1934 renew_client_locked(clp);
1da177e4
LT
1935}
1936
fd39ca9a 1937static void
1da177e4
LT
1938move_to_confirmed(struct nfs4_client *clp)
1939{
1940 unsigned int idhashval = clientid_hashval(clp->cl_clientid.cl_id);
8daae4dc 1941 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
1da177e4 1942
0a880a28
TM
1943 lockdep_assert_held(&nn->client_lock);
1944
1da177e4 1945 dprintk("NFSD: move_to_confirm nfs4_client %p\n", clp);
8daae4dc 1946 list_move(&clp->cl_idhash, &nn->conf_id_hashtbl[idhashval]);
a99454aa 1947 rb_erase(&clp->cl_namenode, &nn->unconf_name_tree);
382a62e7 1948 add_clp_to_name_tree(clp, &nn->conf_name_tree);
ac55fdc4 1949 set_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags);
3dbacee6 1950 renew_client_locked(clp);
1da177e4
LT
1951}
1952
1953static struct nfs4_client *
bfa85e83 1954find_client_in_id_table(struct list_head *tbl, clientid_t *clid, bool sessions)
1da177e4
LT
1955{
1956 struct nfs4_client *clp;
1957 unsigned int idhashval = clientid_hashval(clid->cl_id);
1958
bfa85e83 1959 list_for_each_entry(clp, &tbl[idhashval], cl_idhash) {
a50d2ad1 1960 if (same_clid(&clp->cl_clientid, clid)) {
d15c077e
BF
1961 if ((bool)clp->cl_minorversion != sessions)
1962 return NULL;
3dbacee6 1963 renew_client_locked(clp);
1da177e4 1964 return clp;
a50d2ad1 1965 }
1da177e4
LT
1966 }
1967 return NULL;
1968}
1969
bfa85e83
BF
1970static struct nfs4_client *
1971find_confirmed_client(clientid_t *clid, bool sessions, struct nfsd_net *nn)
1972{
1973 struct list_head *tbl = nn->conf_id_hashtbl;
1974
0a880a28 1975 lockdep_assert_held(&nn->client_lock);
bfa85e83
BF
1976 return find_client_in_id_table(tbl, clid, sessions);
1977}
1978
1da177e4 1979static struct nfs4_client *
0a7ec377 1980find_unconfirmed_client(clientid_t *clid, bool sessions, struct nfsd_net *nn)
1da177e4 1981{
bfa85e83 1982 struct list_head *tbl = nn->unconf_id_hashtbl;
1da177e4 1983
0a880a28 1984 lockdep_assert_held(&nn->client_lock);
bfa85e83 1985 return find_client_in_id_table(tbl, clid, sessions);
1da177e4
LT
1986}
1987
6e5f15c9 1988static bool clp_used_exchangeid(struct nfs4_client *clp)
a1bcecd2 1989{
6e5f15c9 1990 return clp->cl_exchange_flags != 0;
e203d506 1991}
a1bcecd2 1992
28ce6054 1993static struct nfs4_client *
382a62e7 1994find_confirmed_client_by_name(struct xdr_netobj *name, struct nfsd_net *nn)
28ce6054 1995{
0a880a28 1996 lockdep_assert_held(&nn->client_lock);
382a62e7 1997 return find_clp_in_name_tree(name, &nn->conf_name_tree);
28ce6054
N
1998}
1999
2000static struct nfs4_client *
a99454aa 2001find_unconfirmed_client_by_name(struct xdr_netobj *name, struct nfsd_net *nn)
28ce6054 2002{
0a880a28 2003 lockdep_assert_held(&nn->client_lock);
a99454aa 2004 return find_clp_in_name_tree(name, &nn->unconf_name_tree);
28ce6054
N
2005}
2006
fd39ca9a 2007static void
6f3d772f 2008gen_callback(struct nfs4_client *clp, struct nfsd4_setclientid *se, struct svc_rqst *rqstp)
1da177e4 2009{
07263f1e 2010 struct nfs4_cb_conn *conn = &clp->cl_cb_conn;
6f3d772f
TU
2011 struct sockaddr *sa = svc_addr(rqstp);
2012 u32 scopeid = rpc_get_scope_id(sa);
7077ecba
JL
2013 unsigned short expected_family;
2014
2015 /* Currently, we only support tcp and tcp6 for the callback channel */
2016 if (se->se_callback_netid_len == 3 &&
2017 !memcmp(se->se_callback_netid_val, "tcp", 3))
2018 expected_family = AF_INET;
2019 else if (se->se_callback_netid_len == 4 &&
2020 !memcmp(se->se_callback_netid_val, "tcp6", 4))
2021 expected_family = AF_INET6;
2022 else
1da177e4
LT
2023 goto out_err;
2024
c212cecf 2025 conn->cb_addrlen = rpc_uaddr2sockaddr(clp->net, se->se_callback_addr_val,
aa9a4ec7 2026 se->se_callback_addr_len,
07263f1e
BF
2027 (struct sockaddr *)&conn->cb_addr,
2028 sizeof(conn->cb_addr));
aa9a4ec7 2029
07263f1e 2030 if (!conn->cb_addrlen || conn->cb_addr.ss_family != expected_family)
1da177e4 2031 goto out_err;
aa9a4ec7 2032
07263f1e
BF
2033 if (conn->cb_addr.ss_family == AF_INET6)
2034 ((struct sockaddr_in6 *)&conn->cb_addr)->sin6_scope_id = scopeid;
fbf4665f 2035
07263f1e
BF
2036 conn->cb_prog = se->se_callback_prog;
2037 conn->cb_ident = se->se_callback_ident;
849a1cf1 2038 memcpy(&conn->cb_saddr, &rqstp->rq_daddr, rqstp->rq_daddrlen);
1da177e4
LT
2039 return;
2040out_err:
07263f1e
BF
2041 conn->cb_addr.ss_family = AF_UNSPEC;
2042 conn->cb_addrlen = 0;
849823c5 2043 dprintk(KERN_INFO "NFSD: this client (clientid %08x/%08x) "
1da177e4
LT
2044 "will not receive delegations\n",
2045 clp->cl_clientid.cl_boot, clp->cl_clientid.cl_id);
2046
1da177e4
LT
2047 return;
2048}
2049
074fe897 2050/*
067e1ace 2051 * Cache a reply. nfsd4_check_resp_size() has bounded the cache size.
074fe897 2052 */
b607664e 2053static void
074fe897 2054nfsd4_store_cache_entry(struct nfsd4_compoundres *resp)
074fe897 2055{
f5236013 2056 struct xdr_buf *buf = resp->xdr.buf;
557ce264
AA
2057 struct nfsd4_slot *slot = resp->cstate.slot;
2058 unsigned int base;
074fe897 2059
557ce264 2060 dprintk("--> %s slot %p\n", __func__, slot);
074fe897 2061
557ce264
AA
2062 slot->sl_opcnt = resp->opcnt;
2063 slot->sl_status = resp->cstate.status;
074fe897 2064
bf5c43c8 2065 slot->sl_flags |= NFSD4_SLOT_INITIALIZED;
bf864a31 2066 if (nfsd4_not_cached(resp)) {
557ce264 2067 slot->sl_datalen = 0;
bf864a31 2068 return;
074fe897 2069 }
f5236013
BF
2070 base = resp->cstate.data_offset;
2071 slot->sl_datalen = buf->len - base;
2072 if (read_bytes_from_xdr_buf(buf, base, slot->sl_data, slot->sl_datalen))
557ce264
AA
2073 WARN("%s: sessions DRC could not cache compound\n", __func__);
2074 return;
074fe897
AA
2075}
2076
2077/*
abfabf8c
AA
2078 * Encode the replay sequence operation from the slot values.
2079 * If cachethis is FALSE encode the uncached rep error on the next
2080 * operation which sets resp->p and increments resp->opcnt for
2081 * nfs4svc_encode_compoundres.
074fe897 2082 *
074fe897 2083 */
abfabf8c
AA
2084static __be32
2085nfsd4_enc_sequence_replay(struct nfsd4_compoundargs *args,
2086 struct nfsd4_compoundres *resp)
074fe897 2087{
abfabf8c
AA
2088 struct nfsd4_op *op;
2089 struct nfsd4_slot *slot = resp->cstate.slot;
bf864a31 2090
abfabf8c
AA
2091 /* Encode the replayed sequence operation */
2092 op = &args->ops[resp->opcnt - 1];
2093 nfsd4_encode_operation(resp, op);
bf864a31 2094
abfabf8c 2095 /* Return nfserr_retry_uncached_rep in next operation. */
73e79482 2096 if (args->opcnt > 1 && !(slot->sl_flags & NFSD4_SLOT_CACHETHIS)) {
abfabf8c
AA
2097 op = &args->ops[resp->opcnt++];
2098 op->status = nfserr_retry_uncached_rep;
2099 nfsd4_encode_operation(resp, op);
074fe897 2100 }
abfabf8c 2101 return op->status;
074fe897
AA
2102}
2103
2104/*
557ce264
AA
2105 * The sequence operation is not cached because we can use the slot and
2106 * session values.
074fe897 2107 */
3ca2eb98 2108static __be32
bf864a31
AA
2109nfsd4_replay_cache_entry(struct nfsd4_compoundres *resp,
2110 struct nfsd4_sequence *seq)
074fe897 2111{
557ce264 2112 struct nfsd4_slot *slot = resp->cstate.slot;
f5236013
BF
2113 struct xdr_stream *xdr = &resp->xdr;
2114 __be32 *p;
074fe897
AA
2115 __be32 status;
2116
557ce264 2117 dprintk("--> %s slot %p\n", __func__, slot);
074fe897 2118
abfabf8c 2119 status = nfsd4_enc_sequence_replay(resp->rqstp->rq_argp, resp);
0da7b19c 2120 if (status)
abfabf8c 2121 return status;
074fe897 2122
f5236013
BF
2123 p = xdr_reserve_space(xdr, slot->sl_datalen);
2124 if (!p) {
2125 WARN_ON_ONCE(1);
2126 return nfserr_serverfault;
2127 }
2128 xdr_encode_opaque_fixed(p, slot->sl_data, slot->sl_datalen);
2129 xdr_commit_encode(xdr);
074fe897 2130
557ce264 2131 resp->opcnt = slot->sl_opcnt;
f5236013 2132 return slot->sl_status;
074fe897
AA
2133}
2134
0733d213
AA
2135/*
2136 * Set the exchange_id flags returned by the server.
2137 */
2138static void
2139nfsd4_set_ex_flags(struct nfs4_client *new, struct nfsd4_exchange_id *clid)
2140{
2141 /* pNFS is not supported */
2142 new->cl_exchange_flags |= EXCHGID4_FLAG_USE_NON_PNFS;
2143
2144 /* Referrals are supported, Migration is not. */
2145 new->cl_exchange_flags |= EXCHGID4_FLAG_SUPP_MOVED_REFER;
2146
2147 /* set the wire flags to return to client. */
2148 clid->flags = new->cl_exchange_flags;
2149}
2150
631fc9ea
BF
2151static bool client_has_state(struct nfs4_client *clp)
2152{
2153 /*
2154 * Note clp->cl_openowners check isn't quite right: there's no
2155 * need to count owners without stateid's.
2156 *
2157 * Also note we should probably be using this in 4.0 case too.
2158 */
6eccece9
BF
2159 return !list_empty(&clp->cl_openowners)
2160 || !list_empty(&clp->cl_delegations)
2161 || !list_empty(&clp->cl_sessions);
631fc9ea
BF
2162}
2163
069b6ad4
AA
2164__be32
2165nfsd4_exchange_id(struct svc_rqst *rqstp,
2166 struct nfsd4_compound_state *cstate,
2167 struct nfsd4_exchange_id *exid)
2168{
3dbacee6
TM
2169 struct nfs4_client *conf, *new;
2170 struct nfs4_client *unconf = NULL;
57b7b43b 2171 __be32 status;
363168b4 2172 char addr_str[INET6_ADDRSTRLEN];
0733d213 2173 nfs4_verifier verf = exid->verifier;
363168b4 2174 struct sockaddr *sa = svc_addr(rqstp);
83e08fd4 2175 bool update = exid->flags & EXCHGID4_FLAG_UPD_CONFIRMED_REC_A;
c212cecf 2176 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
0733d213 2177
363168b4 2178 rpc_ntop(sa, addr_str, sizeof(addr_str));
0733d213 2179 dprintk("%s rqstp=%p exid=%p clname.len=%u clname.data=%p "
363168b4 2180 "ip_addr=%s flags %x, spa_how %d\n",
0733d213 2181 __func__, rqstp, exid, exid->clname.len, exid->clname.data,
363168b4 2182 addr_str, exid->flags, exid->spa_how);
0733d213 2183
a084daf5 2184 if (exid->flags & ~EXCHGID4_FLAG_MASK_A)
0733d213
AA
2185 return nfserr_inval;
2186
0733d213 2187 switch (exid->spa_how) {
57266a6e
BF
2188 case SP4_MACH_CRED:
2189 if (!svc_rqst_integrity_protected(rqstp))
2190 return nfserr_inval;
0733d213
AA
2191 case SP4_NONE:
2192 break;
063b0fb9
BF
2193 default: /* checked by xdr code */
2194 WARN_ON_ONCE(1);
0733d213 2195 case SP4_SSV:
dd30333c 2196 return nfserr_encr_alg_unsupp;
0733d213
AA
2197 }
2198
5cc40fd7
TM
2199 new = create_client(exid->clname, rqstp, &verf);
2200 if (new == NULL)
2201 return nfserr_jukebox;
2202
2dbb269d 2203 /* Cases below refer to rfc 5661 section 18.35.4: */
3dbacee6 2204 spin_lock(&nn->client_lock);
382a62e7 2205 conf = find_confirmed_client_by_name(&exid->clname, nn);
0733d213 2206 if (conf) {
83e08fd4
BF
2207 bool creds_match = same_creds(&conf->cl_cred, &rqstp->rq_cred);
2208 bool verfs_match = same_verf(&verf, &conf->cl_verifier);
2209
136e658d
BF
2210 if (update) {
2211 if (!clp_used_exchangeid(conf)) { /* buggy client */
2dbb269d 2212 status = nfserr_inval;
1a308118
BF
2213 goto out;
2214 }
57266a6e
BF
2215 if (!mach_creds_match(conf, rqstp)) {
2216 status = nfserr_wrong_cred;
2217 goto out;
2218 }
136e658d 2219 if (!creds_match) { /* case 9 */
ea236d07 2220 status = nfserr_perm;
136e658d
BF
2221 goto out;
2222 }
2223 if (!verfs_match) { /* case 8 */
0733d213
AA
2224 status = nfserr_not_same;
2225 goto out;
2226 }
136e658d
BF
2227 /* case 6 */
2228 exid->flags |= EXCHGID4_FLAG_CONFIRMED_R;
136e658d 2229 goto out_copy;
0733d213 2230 }
136e658d 2231 if (!creds_match) { /* case 3 */
631fc9ea
BF
2232 if (client_has_state(conf)) {
2233 status = nfserr_clid_inuse;
0733d213
AA
2234 goto out;
2235 }
0733d213
AA
2236 goto out_new;
2237 }
136e658d 2238 if (verfs_match) { /* case 2 */
0f1ba0ef 2239 conf->cl_exchange_flags |= EXCHGID4_FLAG_CONFIRMED_R;
136e658d
BF
2240 goto out_copy;
2241 }
2242 /* case 5, client reboot */
3dbacee6 2243 conf = NULL;
136e658d 2244 goto out_new;
6ddbbbfe
MS
2245 }
2246
2dbb269d 2247 if (update) { /* case 7 */
6ddbbbfe
MS
2248 status = nfserr_noent;
2249 goto out;
0733d213
AA
2250 }
2251
a99454aa 2252 unconf = find_unconfirmed_client_by_name(&exid->clname, nn);
2dbb269d 2253 if (unconf) /* case 4, possible retry or client restart */
3dbacee6 2254 unhash_client_locked(unconf);
0733d213 2255
2dbb269d 2256 /* case 1 (normal case) */
0733d213 2257out_new:
fd699b8a
JL
2258 if (conf) {
2259 status = mark_client_expired_locked(conf);
2260 if (status)
2261 goto out;
2262 }
4f540e29 2263 new->cl_minorversion = cstate->minorversion;
57266a6e 2264 new->cl_mach_cred = (exid->spa_how == SP4_MACH_CRED);
0733d213 2265
c212cecf 2266 gen_clid(new, nn);
ac55fdc4 2267 add_to_unconfirmed(new);
3dbacee6 2268 swap(new, conf);
0733d213 2269out_copy:
5cc40fd7
TM
2270 exid->clientid.cl_boot = conf->cl_clientid.cl_boot;
2271 exid->clientid.cl_id = conf->cl_clientid.cl_id;
0733d213 2272
5cc40fd7
TM
2273 exid->seqid = conf->cl_cs_slot.sl_seqid + 1;
2274 nfsd4_set_ex_flags(conf, exid);
0733d213
AA
2275
2276 dprintk("nfsd4_exchange_id seqid %d flags %x\n",
5cc40fd7 2277 conf->cl_cs_slot.sl_seqid, conf->cl_exchange_flags);
0733d213
AA
2278 status = nfs_ok;
2279
2280out:
3dbacee6 2281 spin_unlock(&nn->client_lock);
5cc40fd7 2282 if (new)
3dbacee6
TM
2283 expire_client(new);
2284 if (unconf)
2285 expire_client(unconf);
0733d213 2286 return status;
069b6ad4
AA
2287}
2288
57b7b43b 2289static __be32
88e588d5 2290check_slot_seqid(u32 seqid, u32 slot_seqid, int slot_inuse)
b85d4c01 2291{
88e588d5
AA
2292 dprintk("%s enter. seqid %d slot_seqid %d\n", __func__, seqid,
2293 slot_seqid);
b85d4c01
BH
2294
2295 /* The slot is in use, and no response has been sent. */
88e588d5
AA
2296 if (slot_inuse) {
2297 if (seqid == slot_seqid)
b85d4c01
BH
2298 return nfserr_jukebox;
2299 else
2300 return nfserr_seq_misordered;
2301 }
f6d82485 2302 /* Note unsigned 32-bit arithmetic handles wraparound: */
88e588d5 2303 if (likely(seqid == slot_seqid + 1))
b85d4c01 2304 return nfs_ok;
88e588d5 2305 if (seqid == slot_seqid)
b85d4c01 2306 return nfserr_replay_cache;
b85d4c01
BH
2307 return nfserr_seq_misordered;
2308}
2309
49557cc7
AA
2310/*
2311 * Cache the create session result into the create session single DRC
2312 * slot cache by saving the xdr structure. sl_seqid has been set.
2313 * Do this for solo or embedded create session operations.
2314 */
2315static void
2316nfsd4_cache_create_session(struct nfsd4_create_session *cr_ses,
57b7b43b 2317 struct nfsd4_clid_slot *slot, __be32 nfserr)
49557cc7
AA
2318{
2319 slot->sl_status = nfserr;
2320 memcpy(&slot->sl_cr_ses, cr_ses, sizeof(*cr_ses));
2321}
2322
2323static __be32
2324nfsd4_replay_create_session(struct nfsd4_create_session *cr_ses,
2325 struct nfsd4_clid_slot *slot)
2326{
2327 memcpy(cr_ses, &slot->sl_cr_ses, sizeof(*cr_ses));
2328 return slot->sl_status;
2329}
2330
1b74c25b
MJ
2331#define NFSD_MIN_REQ_HDR_SEQ_SZ ((\
2332 2 * 2 + /* credential,verifier: AUTH_NULL, length 0 */ \
2333 1 + /* MIN tag is length with zero, only length */ \
2334 3 + /* version, opcount, opcode */ \
2335 XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \
2336 /* seqid, slotID, slotID, cache */ \
2337 4 ) * sizeof(__be32))
2338
2339#define NFSD_MIN_RESP_HDR_SEQ_SZ ((\
2340 2 + /* verifier: AUTH_NULL, length 0 */\
2341 1 + /* status */ \
2342 1 + /* MIN tag is length with zero, only length */ \
2343 3 + /* opcount, opcode, opstatus*/ \
2344 XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \
2345 /* seqid, slotID, slotID, slotID, status */ \
2346 5 ) * sizeof(__be32))
2347
55c760cf 2348static __be32 check_forechannel_attrs(struct nfsd4_channel_attrs *ca, struct nfsd_net *nn)
1b74c25b 2349{
55c760cf
BF
2350 u32 maxrpc = nn->nfsd_serv->sv_max_mesg;
2351
373cd409
BF
2352 if (ca->maxreq_sz < NFSD_MIN_REQ_HDR_SEQ_SZ)
2353 return nfserr_toosmall;
2354 if (ca->maxresp_sz < NFSD_MIN_RESP_HDR_SEQ_SZ)
2355 return nfserr_toosmall;
55c760cf
BF
2356 ca->headerpadsz = 0;
2357 ca->maxreq_sz = min_t(u32, ca->maxreq_sz, maxrpc);
2358 ca->maxresp_sz = min_t(u32, ca->maxresp_sz, maxrpc);
2359 ca->maxops = min_t(u32, ca->maxops, NFSD_MAX_OPS_PER_COMPOUND);
2360 ca->maxresp_cached = min_t(u32, ca->maxresp_cached,
2361 NFSD_SLOT_CACHE_SIZE + NFSD_MIN_HDR_SEQ_SZ);
2362 ca->maxreqs = min_t(u32, ca->maxreqs, NFSD_MAX_SLOTS_PER_SESSION);
2363 /*
2364 * Note decreasing slot size below client's request may make it
2365 * difficult for client to function correctly, whereas
2366 * decreasing the number of slots will (just?) affect
2367 * performance. When short on memory we therefore prefer to
2368 * decrease number of slots instead of their size. Clients that
2369 * request larger slots than they need will get poor results:
2370 */
2371 ca->maxreqs = nfsd4_get_drc_mem(ca);
2372 if (!ca->maxreqs)
2373 return nfserr_jukebox;
2374
373cd409 2375 return nfs_ok;
1b74c25b
MJ
2376}
2377
8a891633
KM
2378#define NFSD_CB_MAX_REQ_SZ ((NFS4_enc_cb_recall_sz + \
2379 RPC_MAX_HEADER_WITH_AUTH) * sizeof(__be32))
2380#define NFSD_CB_MAX_RESP_SZ ((NFS4_dec_cb_recall_sz + \
2381 RPC_MAX_REPHEADER_WITH_AUTH) * sizeof(__be32))
2382
06b332a5 2383static __be32 check_backchannel_attrs(struct nfsd4_channel_attrs *ca)
1b74c25b 2384{
06b332a5
BF
2385 ca->headerpadsz = 0;
2386
2387 /*
2388 * These RPC_MAX_HEADER macros are overkill, especially since we
2389 * don't even do gss on the backchannel yet. But this is still
2390 * less than 1k. Tighten up this estimate in the unlikely event
2391 * it turns out to be a problem for some client:
2392 */
8a891633 2393 if (ca->maxreq_sz < NFSD_CB_MAX_REQ_SZ)
06b332a5 2394 return nfserr_toosmall;
8a891633 2395 if (ca->maxresp_sz < NFSD_CB_MAX_RESP_SZ)
06b332a5
BF
2396 return nfserr_toosmall;
2397 ca->maxresp_cached = 0;
2398 if (ca->maxops < 2)
2399 return nfserr_toosmall;
2400
2401 return nfs_ok;
1b74c25b
MJ
2402}
2403
b78724b7
BF
2404static __be32 nfsd4_check_cb_sec(struct nfsd4_cb_sec *cbs)
2405{
2406 switch (cbs->flavor) {
2407 case RPC_AUTH_NULL:
2408 case RPC_AUTH_UNIX:
2409 return nfs_ok;
2410 default:
2411 /*
2412 * GSS case: the spec doesn't allow us to return this
2413 * error. But it also doesn't allow us not to support
2414 * GSS.
2415 * I'd rather this fail hard than return some error the
2416 * client might think it can already handle:
2417 */
2418 return nfserr_encr_alg_unsupp;
2419 }
2420}
2421
069b6ad4
AA
2422__be32
2423nfsd4_create_session(struct svc_rqst *rqstp,
2424 struct nfsd4_compound_state *cstate,
2425 struct nfsd4_create_session *cr_ses)
2426{
363168b4 2427 struct sockaddr *sa = svc_addr(rqstp);
ec6b5d7b 2428 struct nfs4_client *conf, *unconf;
d20c11d8 2429 struct nfs4_client *old = NULL;
ac7c46f2 2430 struct nfsd4_session *new;
81f0b2a4 2431 struct nfsd4_conn *conn;
49557cc7 2432 struct nfsd4_clid_slot *cs_slot = NULL;
57b7b43b 2433 __be32 status = 0;
8daae4dc 2434 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
ec6b5d7b 2435
a62573dc
MJ
2436 if (cr_ses->flags & ~SESSION4_FLAG_MASK_A)
2437 return nfserr_inval;
b78724b7
BF
2438 status = nfsd4_check_cb_sec(&cr_ses->cb_sec);
2439 if (status)
2440 return status;
55c760cf 2441 status = check_forechannel_attrs(&cr_ses->fore_channel, nn);
06b332a5
BF
2442 if (status)
2443 return status;
2444 status = check_backchannel_attrs(&cr_ses->back_channel);
373cd409 2445 if (status)
f403e450 2446 goto out_release_drc_mem;
81f0b2a4 2447 status = nfserr_jukebox;
60810e54 2448 new = alloc_session(&cr_ses->fore_channel, &cr_ses->back_channel);
55c760cf
BF
2449 if (!new)
2450 goto out_release_drc_mem;
81f0b2a4
BF
2451 conn = alloc_conn_from_crses(rqstp, cr_ses);
2452 if (!conn)
2453 goto out_free_session;
a62573dc 2454
d20c11d8 2455 spin_lock(&nn->client_lock);
0a7ec377 2456 unconf = find_unconfirmed_client(&cr_ses->clientid, true, nn);
8daae4dc 2457 conf = find_confirmed_client(&cr_ses->clientid, true, nn);
78389046 2458 WARN_ON_ONCE(conf && unconf);
ec6b5d7b
AA
2459
2460 if (conf) {
57266a6e
BF
2461 status = nfserr_wrong_cred;
2462 if (!mach_creds_match(conf, rqstp))
2463 goto out_free_conn;
49557cc7
AA
2464 cs_slot = &conf->cl_cs_slot;
2465 status = check_slot_seqid(cr_ses->seqid, cs_slot->sl_seqid, 0);
38eb76a5 2466 if (status == nfserr_replay_cache) {
49557cc7 2467 status = nfsd4_replay_create_session(cr_ses, cs_slot);
81f0b2a4 2468 goto out_free_conn;
49557cc7 2469 } else if (cr_ses->seqid != cs_slot->sl_seqid + 1) {
ec6b5d7b 2470 status = nfserr_seq_misordered;
81f0b2a4 2471 goto out_free_conn;
ec6b5d7b 2472 }
ec6b5d7b
AA
2473 } else if (unconf) {
2474 if (!same_creds(&unconf->cl_cred, &rqstp->rq_cred) ||
363168b4 2475 !rpc_cmp_addr(sa, (struct sockaddr *) &unconf->cl_addr)) {
ec6b5d7b 2476 status = nfserr_clid_inuse;
81f0b2a4 2477 goto out_free_conn;
ec6b5d7b 2478 }
57266a6e
BF
2479 status = nfserr_wrong_cred;
2480 if (!mach_creds_match(unconf, rqstp))
2481 goto out_free_conn;
49557cc7
AA
2482 cs_slot = &unconf->cl_cs_slot;
2483 status = check_slot_seqid(cr_ses->seqid, cs_slot->sl_seqid, 0);
38eb76a5
AA
2484 if (status) {
2485 /* an unconfirmed replay returns misordered */
ec6b5d7b 2486 status = nfserr_seq_misordered;
81f0b2a4 2487 goto out_free_conn;
ec6b5d7b 2488 }
382a62e7 2489 old = find_confirmed_client_by_name(&unconf->cl_name, nn);
221a6876 2490 if (old) {
d20c11d8 2491 status = mark_client_expired_locked(old);
7abea1e8
JL
2492 if (status) {
2493 old = NULL;
221a6876 2494 goto out_free_conn;
7abea1e8 2495 }
221a6876 2496 }
8f9d3d3b 2497 move_to_confirmed(unconf);
ec6b5d7b
AA
2498 conf = unconf;
2499 } else {
2500 status = nfserr_stale_clientid;
81f0b2a4 2501 goto out_free_conn;
ec6b5d7b 2502 }
81f0b2a4 2503 status = nfs_ok;
408b79bc
BF
2504 /*
2505 * We do not support RDMA or persistent sessions
2506 */
2507 cr_ses->flags &= ~SESSION4_PERSIST;
2508 cr_ses->flags &= ~SESSION4_RDMA;
2509
81f0b2a4 2510 init_session(rqstp, new, conf, cr_ses);
d20c11d8 2511 nfsd4_get_session_locked(new);
81f0b2a4 2512
ac7c46f2 2513 memcpy(cr_ses->sessionid.data, new->se_sessionid.data,
ec6b5d7b 2514 NFS4_MAX_SESSIONID_LEN);
86c3e16c 2515 cs_slot->sl_seqid++;
49557cc7 2516 cr_ses->seqid = cs_slot->sl_seqid;
ec6b5d7b 2517
d20c11d8 2518 /* cache solo and embedded create sessions under the client_lock */
49557cc7 2519 nfsd4_cache_create_session(cr_ses, cs_slot, status);
d20c11d8
JL
2520 spin_unlock(&nn->client_lock);
2521 /* init connection and backchannel */
2522 nfsd4_init_conn(rqstp, conn, new);
2523 nfsd4_put_session(new);
d20c11d8
JL
2524 if (old)
2525 expire_client(old);
ec6b5d7b 2526 return status;
81f0b2a4 2527out_free_conn:
d20c11d8 2528 spin_unlock(&nn->client_lock);
81f0b2a4 2529 free_conn(conn);
d20c11d8
JL
2530 if (old)
2531 expire_client(old);
81f0b2a4
BF
2532out_free_session:
2533 __free_session(new);
55c760cf
BF
2534out_release_drc_mem:
2535 nfsd4_put_drc_mem(&cr_ses->fore_channel);
1ca50792 2536 return status;
069b6ad4
AA
2537}
2538
1d1bc8f2
BF
2539static __be32 nfsd4_map_bcts_dir(u32 *dir)
2540{
2541 switch (*dir) {
2542 case NFS4_CDFC4_FORE:
2543 case NFS4_CDFC4_BACK:
2544 return nfs_ok;
2545 case NFS4_CDFC4_FORE_OR_BOTH:
2546 case NFS4_CDFC4_BACK_OR_BOTH:
2547 *dir = NFS4_CDFC4_BOTH;
2548 return nfs_ok;
2549 };
2550 return nfserr_inval;
2551}
2552
cb73a9f4
BF
2553__be32 nfsd4_backchannel_ctl(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_backchannel_ctl *bc)
2554{
2555 struct nfsd4_session *session = cstate->session;
c9a49628 2556 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
b78724b7 2557 __be32 status;
cb73a9f4 2558
b78724b7
BF
2559 status = nfsd4_check_cb_sec(&bc->bc_cb_sec);
2560 if (status)
2561 return status;
c9a49628 2562 spin_lock(&nn->client_lock);
cb73a9f4
BF
2563 session->se_cb_prog = bc->bc_cb_program;
2564 session->se_cb_sec = bc->bc_cb_sec;
c9a49628 2565 spin_unlock(&nn->client_lock);
cb73a9f4
BF
2566
2567 nfsd4_probe_callback(session->se_client);
2568
2569 return nfs_ok;
2570}
2571
1d1bc8f2
BF
2572__be32 nfsd4_bind_conn_to_session(struct svc_rqst *rqstp,
2573 struct nfsd4_compound_state *cstate,
2574 struct nfsd4_bind_conn_to_session *bcts)
2575{
2576 __be32 status;
3ba63671 2577 struct nfsd4_conn *conn;
4f6e6c17 2578 struct nfsd4_session *session;
d4e19e70
TM
2579 struct net *net = SVC_NET(rqstp);
2580 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
1d1bc8f2
BF
2581
2582 if (!nfsd4_last_compound_op(rqstp))
2583 return nfserr_not_only_op;
c9a49628 2584 spin_lock(&nn->client_lock);
d4e19e70 2585 session = find_in_sessionid_hashtbl(&bcts->sessionid, net, &status);
c9a49628 2586 spin_unlock(&nn->client_lock);
4f6e6c17 2587 if (!session)
d4e19e70 2588 goto out_no_session;
57266a6e
BF
2589 status = nfserr_wrong_cred;
2590 if (!mach_creds_match(session->se_client, rqstp))
2591 goto out;
1d1bc8f2 2592 status = nfsd4_map_bcts_dir(&bcts->dir);
3ba63671 2593 if (status)
4f6e6c17 2594 goto out;
3ba63671 2595 conn = alloc_conn(rqstp, bcts->dir);
4f6e6c17 2596 status = nfserr_jukebox;
3ba63671 2597 if (!conn)
4f6e6c17
BF
2598 goto out;
2599 nfsd4_init_conn(rqstp, conn, session);
2600 status = nfs_ok;
2601out:
d4e19e70
TM
2602 nfsd4_put_session(session);
2603out_no_session:
4f6e6c17 2604 return status;
1d1bc8f2
BF
2605}
2606
5d4cec2f
BF
2607static bool nfsd4_compound_in_session(struct nfsd4_session *session, struct nfs4_sessionid *sid)
2608{
2609 if (!session)
2610 return 0;
2611 return !memcmp(sid, &session->se_sessionid, sizeof(*sid));
2612}
2613
069b6ad4
AA
2614__be32
2615nfsd4_destroy_session(struct svc_rqst *r,
2616 struct nfsd4_compound_state *cstate,
2617 struct nfsd4_destroy_session *sessionid)
2618{
e10e0cfc 2619 struct nfsd4_session *ses;
abcdff09 2620 __be32 status;
f0f51f5c 2621 int ref_held_by_me = 0;
d4e19e70
TM
2622 struct net *net = SVC_NET(r);
2623 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
e10e0cfc 2624
abcdff09 2625 status = nfserr_not_only_op;
5d4cec2f 2626 if (nfsd4_compound_in_session(cstate->session, &sessionid->sessionid)) {
57716355 2627 if (!nfsd4_last_compound_op(r))
abcdff09 2628 goto out;
f0f51f5c 2629 ref_held_by_me++;
57716355 2630 }
e10e0cfc 2631 dump_sessionid(__func__, &sessionid->sessionid);
c9a49628 2632 spin_lock(&nn->client_lock);
d4e19e70 2633 ses = find_in_sessionid_hashtbl(&sessionid->sessionid, net, &status);
abcdff09
BF
2634 if (!ses)
2635 goto out_client_lock;
57266a6e
BF
2636 status = nfserr_wrong_cred;
2637 if (!mach_creds_match(ses->se_client, r))
d4e19e70 2638 goto out_put_session;
f0f51f5c 2639 status = mark_session_dead_locked(ses, 1 + ref_held_by_me);
66b2b9b2 2640 if (status)
f0f51f5c 2641 goto out_put_session;
e10e0cfc 2642 unhash_session(ses);
c9a49628 2643 spin_unlock(&nn->client_lock);
e10e0cfc 2644
84f5f7cc 2645 nfsd4_probe_callback_sync(ses->se_client);
19cf5c02 2646
c9a49628 2647 spin_lock(&nn->client_lock);
e10e0cfc 2648 status = nfs_ok;
f0f51f5c 2649out_put_session:
d4e19e70 2650 nfsd4_put_session_locked(ses);
abcdff09
BF
2651out_client_lock:
2652 spin_unlock(&nn->client_lock);
e10e0cfc 2653out:
e10e0cfc 2654 return status;
069b6ad4
AA
2655}
2656
a663bdd8 2657static struct nfsd4_conn *__nfsd4_find_conn(struct svc_xprt *xpt, struct nfsd4_session *s)
328ead28
BF
2658{
2659 struct nfsd4_conn *c;
2660
2661 list_for_each_entry(c, &s->se_conns, cn_persession) {
a663bdd8 2662 if (c->cn_xprt == xpt) {
328ead28
BF
2663 return c;
2664 }
2665 }
2666 return NULL;
2667}
2668
57266a6e 2669static __be32 nfsd4_sequence_check_conn(struct nfsd4_conn *new, struct nfsd4_session *ses)
328ead28
BF
2670{
2671 struct nfs4_client *clp = ses->se_client;
a663bdd8 2672 struct nfsd4_conn *c;
57266a6e 2673 __be32 status = nfs_ok;
21b75b01 2674 int ret;
328ead28
BF
2675
2676 spin_lock(&clp->cl_lock);
a663bdd8 2677 c = __nfsd4_find_conn(new->cn_xprt, ses);
57266a6e
BF
2678 if (c)
2679 goto out_free;
2680 status = nfserr_conn_not_bound_to_session;
2681 if (clp->cl_mach_cred)
2682 goto out_free;
328ead28
BF
2683 __nfsd4_hash_conn(new, ses);
2684 spin_unlock(&clp->cl_lock);
21b75b01
BF
2685 ret = nfsd4_register_conn(new);
2686 if (ret)
2687 /* oops; xprt is already down: */
2688 nfsd4_conn_lost(&new->cn_xpt_user);
57266a6e
BF
2689 return nfs_ok;
2690out_free:
2691 spin_unlock(&clp->cl_lock);
2692 free_conn(new);
2693 return status;
328ead28
BF
2694}
2695
868b89c3
MJ
2696static bool nfsd4_session_too_many_ops(struct svc_rqst *rqstp, struct nfsd4_session *session)
2697{
2698 struct nfsd4_compoundargs *args = rqstp->rq_argp;
2699
2700 return args->opcnt > session->se_fchannel.maxops;
2701}
2702
ae82a8d0
MJ
2703static bool nfsd4_request_too_big(struct svc_rqst *rqstp,
2704 struct nfsd4_session *session)
2705{
2706 struct xdr_buf *xb = &rqstp->rq_arg;
2707
2708 return xb->len > session->se_fchannel.maxreq_sz;
2709}
2710
069b6ad4 2711__be32
b85d4c01 2712nfsd4_sequence(struct svc_rqst *rqstp,
069b6ad4
AA
2713 struct nfsd4_compound_state *cstate,
2714 struct nfsd4_sequence *seq)
2715{
f9bb94c4 2716 struct nfsd4_compoundres *resp = rqstp->rq_resp;
47ee5298 2717 struct xdr_stream *xdr = &resp->xdr;
b85d4c01 2718 struct nfsd4_session *session;
221a6876 2719 struct nfs4_client *clp;
b85d4c01 2720 struct nfsd4_slot *slot;
a663bdd8 2721 struct nfsd4_conn *conn;
57b7b43b 2722 __be32 status;
47ee5298 2723 int buflen;
d4e19e70
TM
2724 struct net *net = SVC_NET(rqstp);
2725 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
b85d4c01 2726
f9bb94c4
AA
2727 if (resp->opcnt != 1)
2728 return nfserr_sequence_pos;
2729
a663bdd8
BF
2730 /*
2731 * Will be either used or freed by nfsd4_sequence_check_conn
2732 * below.
2733 */
2734 conn = alloc_conn(rqstp, NFS4_CDFC4_FORE);
2735 if (!conn)
2736 return nfserr_jukebox;
2737
c9a49628 2738 spin_lock(&nn->client_lock);
d4e19e70 2739 session = find_in_sessionid_hashtbl(&seq->sessionid, net, &status);
b85d4c01 2740 if (!session)
221a6876
BF
2741 goto out_no_session;
2742 clp = session->se_client;
b85d4c01 2743
868b89c3
MJ
2744 status = nfserr_too_many_ops;
2745 if (nfsd4_session_too_many_ops(rqstp, session))
66b2b9b2 2746 goto out_put_session;
868b89c3 2747
ae82a8d0
MJ
2748 status = nfserr_req_too_big;
2749 if (nfsd4_request_too_big(rqstp, session))
66b2b9b2 2750 goto out_put_session;
ae82a8d0 2751
b85d4c01 2752 status = nfserr_badslot;
6c18ba9f 2753 if (seq->slotid >= session->se_fchannel.maxreqs)
66b2b9b2 2754 goto out_put_session;
b85d4c01 2755
557ce264 2756 slot = session->se_slots[seq->slotid];
b85d4c01
BH
2757 dprintk("%s: slotid %d\n", __func__, seq->slotid);
2758
a8dfdaeb
AA
2759 /* We do not negotiate the number of slots yet, so set the
2760 * maxslots to the session maxreqs which is used to encode
2761 * sr_highest_slotid and the sr_target_slot id to maxslots */
2762 seq->maxslots = session->se_fchannel.maxreqs;
2763
73e79482
BF
2764 status = check_slot_seqid(seq->seqid, slot->sl_seqid,
2765 slot->sl_flags & NFSD4_SLOT_INUSE);
b85d4c01 2766 if (status == nfserr_replay_cache) {
bf5c43c8
BF
2767 status = nfserr_seq_misordered;
2768 if (!(slot->sl_flags & NFSD4_SLOT_INITIALIZED))
66b2b9b2 2769 goto out_put_session;
b85d4c01
BH
2770 cstate->slot = slot;
2771 cstate->session = session;
4b24ca7d 2772 cstate->clp = clp;
da3846a2 2773 /* Return the cached reply status and set cstate->status
557ce264 2774 * for nfsd4_proc_compound processing */
bf864a31 2775 status = nfsd4_replay_cache_entry(resp, seq);
da3846a2 2776 cstate->status = nfserr_replay_cache;
aaf84eb9 2777 goto out;
b85d4c01
BH
2778 }
2779 if (status)
66b2b9b2 2780 goto out_put_session;
b85d4c01 2781
57266a6e 2782 status = nfsd4_sequence_check_conn(conn, session);
a663bdd8 2783 conn = NULL;
57266a6e
BF
2784 if (status)
2785 goto out_put_session;
328ead28 2786
47ee5298
BF
2787 buflen = (seq->cachethis) ?
2788 session->se_fchannel.maxresp_cached :
2789 session->se_fchannel.maxresp_sz;
2790 status = (seq->cachethis) ? nfserr_rep_too_big_to_cache :
2791 nfserr_rep_too_big;
a5cddc88 2792 if (xdr_restrict_buflen(xdr, buflen - rqstp->rq_auth_slack))
47ee5298 2793 goto out_put_session;
32aaa62e 2794 svc_reserve(rqstp, buflen);
47ee5298
BF
2795
2796 status = nfs_ok;
b85d4c01 2797 /* Success! bump slot seqid */
b85d4c01 2798 slot->sl_seqid = seq->seqid;
bf5c43c8 2799 slot->sl_flags |= NFSD4_SLOT_INUSE;
73e79482
BF
2800 if (seq->cachethis)
2801 slot->sl_flags |= NFSD4_SLOT_CACHETHIS;
bf5c43c8
BF
2802 else
2803 slot->sl_flags &= ~NFSD4_SLOT_CACHETHIS;
b85d4c01
BH
2804
2805 cstate->slot = slot;
2806 cstate->session = session;
4b24ca7d 2807 cstate->clp = clp;
b85d4c01 2808
b85d4c01 2809out:
221a6876
BF
2810 switch (clp->cl_cb_state) {
2811 case NFSD4_CB_DOWN:
2812 seq->status_flags = SEQ4_STATUS_CB_PATH_DOWN;
2813 break;
2814 case NFSD4_CB_FAULT:
2815 seq->status_flags = SEQ4_STATUS_BACKCHANNEL_FAULT;
2816 break;
2817 default:
2818 seq->status_flags = 0;
aaf84eb9 2819 }
3bd64a5b
BF
2820 if (!list_empty(&clp->cl_revoked))
2821 seq->status_flags |= SEQ4_STATUS_RECALLABLE_STATE_REVOKED;
221a6876 2822out_no_session:
3f42d2c4
KM
2823 if (conn)
2824 free_conn(conn);
c9a49628 2825 spin_unlock(&nn->client_lock);
b85d4c01 2826 return status;
66b2b9b2 2827out_put_session:
d4e19e70 2828 nfsd4_put_session_locked(session);
221a6876 2829 goto out_no_session;
069b6ad4
AA
2830}
2831
b607664e
TM
2832void
2833nfsd4_sequence_done(struct nfsd4_compoundres *resp)
2834{
2835 struct nfsd4_compound_state *cs = &resp->cstate;
2836
2837 if (nfsd4_has_session(cs)) {
b607664e
TM
2838 if (cs->status != nfserr_replay_cache) {
2839 nfsd4_store_cache_entry(resp);
2840 cs->slot->sl_flags &= ~NFSD4_SLOT_INUSE;
2841 }
d4e19e70 2842 /* Drop session reference that was taken in nfsd4_sequence() */
b607664e 2843 nfsd4_put_session(cs->session);
4b24ca7d
JL
2844 } else if (cs->clp)
2845 put_client_renew(cs->clp);
b607664e
TM
2846}
2847
345c2842
MJ
2848__be32
2849nfsd4_destroy_clientid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_destroy_clientid *dc)
2850{
6b10ad19
TM
2851 struct nfs4_client *conf, *unconf;
2852 struct nfs4_client *clp = NULL;
57b7b43b 2853 __be32 status = 0;
8daae4dc 2854 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
345c2842 2855
6b10ad19 2856 spin_lock(&nn->client_lock);
0a7ec377 2857 unconf = find_unconfirmed_client(&dc->clientid, true, nn);
8daae4dc 2858 conf = find_confirmed_client(&dc->clientid, true, nn);
78389046 2859 WARN_ON_ONCE(conf && unconf);
345c2842
MJ
2860
2861 if (conf) {
c0293b01 2862 if (client_has_state(conf)) {
345c2842
MJ
2863 status = nfserr_clientid_busy;
2864 goto out;
2865 }
fd699b8a
JL
2866 status = mark_client_expired_locked(conf);
2867 if (status)
2868 goto out;
6b10ad19 2869 clp = conf;
345c2842
MJ
2870 } else if (unconf)
2871 clp = unconf;
2872 else {
2873 status = nfserr_stale_clientid;
2874 goto out;
2875 }
57266a6e 2876 if (!mach_creds_match(clp, rqstp)) {
6b10ad19 2877 clp = NULL;
57266a6e
BF
2878 status = nfserr_wrong_cred;
2879 goto out;
2880 }
6b10ad19 2881 unhash_client_locked(clp);
345c2842 2882out:
6b10ad19 2883 spin_unlock(&nn->client_lock);
6b10ad19
TM
2884 if (clp)
2885 expire_client(clp);
345c2842
MJ
2886 return status;
2887}
2888
4dc6ec00
BF
2889__be32
2890nfsd4_reclaim_complete(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_reclaim_complete *rc)
2891{
57b7b43b 2892 __be32 status = 0;
bcecf1cc 2893
4dc6ec00
BF
2894 if (rc->rca_one_fs) {
2895 if (!cstate->current_fh.fh_dentry)
2896 return nfserr_nofilehandle;
2897 /*
2898 * We don't take advantage of the rca_one_fs case.
2899 * That's OK, it's optional, we can safely ignore it.
2900 */
2901 return nfs_ok;
2902 }
bcecf1cc 2903
bcecf1cc 2904 status = nfserr_complete_already;
a52d726b
JL
2905 if (test_and_set_bit(NFSD4_CLIENT_RECLAIM_COMPLETE,
2906 &cstate->session->se_client->cl_flags))
bcecf1cc
MJ
2907 goto out;
2908
2909 status = nfserr_stale_clientid;
2910 if (is_client_expired(cstate->session->se_client))
4dc6ec00
BF
2911 /*
2912 * The following error isn't really legal.
2913 * But we only get here if the client just explicitly
2914 * destroyed the client. Surely it no longer cares what
2915 * error it gets back on an operation for the dead
2916 * client.
2917 */
bcecf1cc
MJ
2918 goto out;
2919
2920 status = nfs_ok;
2a4317c5 2921 nfsd4_client_record_create(cstate->session->se_client);
bcecf1cc 2922out:
bcecf1cc 2923 return status;
4dc6ec00
BF
2924}
2925
b37ad28b 2926__be32
b591480b
BF
2927nfsd4_setclientid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
2928 struct nfsd4_setclientid *setclid)
1da177e4 2929{
a084daf5 2930 struct xdr_netobj clname = setclid->se_name;
1da177e4 2931 nfs4_verifier clverifier = setclid->se_verf;
3dbacee6
TM
2932 struct nfs4_client *conf, *new;
2933 struct nfs4_client *unconf = NULL;
b37ad28b 2934 __be32 status;
c212cecf
SK
2935 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
2936
5cc40fd7
TM
2937 new = create_client(clname, rqstp, &clverifier);
2938 if (new == NULL)
2939 return nfserr_jukebox;
63db4632 2940 /* Cases below refer to rfc 3530 section 14.2.33: */
3dbacee6 2941 spin_lock(&nn->client_lock);
382a62e7 2942 conf = find_confirmed_client_by_name(&clname, nn);
28ce6054 2943 if (conf) {
63db4632 2944 /* case 0: */
1da177e4 2945 status = nfserr_clid_inuse;
e203d506
BF
2946 if (clp_used_exchangeid(conf))
2947 goto out;
026722c2 2948 if (!same_creds(&conf->cl_cred, &rqstp->rq_cred)) {
363168b4
JL
2949 char addr_str[INET6_ADDRSTRLEN];
2950 rpc_ntop((struct sockaddr *) &conf->cl_addr, addr_str,
2951 sizeof(addr_str));
2952 dprintk("NFSD: setclientid: string in use by client "
2953 "at %s\n", addr_str);
1da177e4
LT
2954 goto out;
2955 }
1da177e4 2956 }
a99454aa 2957 unconf = find_unconfirmed_client_by_name(&clname, nn);
8f930711 2958 if (unconf)
3dbacee6 2959 unhash_client_locked(unconf);
34b232bb 2960 if (conf && same_verf(&conf->cl_verifier, &clverifier))
63db4632 2961 /* case 1: probable callback update */
1da177e4 2962 copy_clid(new, conf);
34b232bb 2963 else /* case 4 (new client) or cases 2, 3 (client reboot): */
c212cecf 2964 gen_clid(new, nn);
8323c3b2 2965 new->cl_minorversion = 0;
6f3d772f 2966 gen_callback(new, setclid, rqstp);
ac55fdc4 2967 add_to_unconfirmed(new);
1da177e4
LT
2968 setclid->se_clientid.cl_boot = new->cl_clientid.cl_boot;
2969 setclid->se_clientid.cl_id = new->cl_clientid.cl_id;
2970 memcpy(setclid->se_confirm.data, new->cl_confirm.data, sizeof(setclid->se_confirm.data));
5cc40fd7 2971 new = NULL;
1da177e4
LT
2972 status = nfs_ok;
2973out:
3dbacee6 2974 spin_unlock(&nn->client_lock);
5cc40fd7
TM
2975 if (new)
2976 free_client(new);
3dbacee6
TM
2977 if (unconf)
2978 expire_client(unconf);
1da177e4
LT
2979 return status;
2980}
2981
2982
b37ad28b 2983__be32
b591480b
BF
2984nfsd4_setclientid_confirm(struct svc_rqst *rqstp,
2985 struct nfsd4_compound_state *cstate,
2986 struct nfsd4_setclientid_confirm *setclientid_confirm)
1da177e4 2987{
21ab45a4 2988 struct nfs4_client *conf, *unconf;
d20c11d8 2989 struct nfs4_client *old = NULL;
1da177e4
LT
2990 nfs4_verifier confirm = setclientid_confirm->sc_confirm;
2991 clientid_t * clid = &setclientid_confirm->sc_clientid;
b37ad28b 2992 __be32 status;
7f2210fa 2993 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
1da177e4 2994
2c142baa 2995 if (STALE_CLIENTID(clid, nn))
1da177e4 2996 return nfserr_stale_clientid;
21ab45a4 2997
d20c11d8 2998 spin_lock(&nn->client_lock);
8daae4dc 2999 conf = find_confirmed_client(clid, false, nn);
0a7ec377 3000 unconf = find_unconfirmed_client(clid, false, nn);
a186e767 3001 /*
8695b90a
BF
3002 * We try hard to give out unique clientid's, so if we get an
3003 * attempt to confirm the same clientid with a different cred,
3004 * there's a bug somewhere. Let's charitably assume it's our
3005 * bug.
a186e767 3006 */
8695b90a
BF
3007 status = nfserr_serverfault;
3008 if (unconf && !same_creds(&unconf->cl_cred, &rqstp->rq_cred))
3009 goto out;
3010 if (conf && !same_creds(&conf->cl_cred, &rqstp->rq_cred))
3011 goto out;
63db4632 3012 /* cases below refer to rfc 3530 section 14.2.34: */
90d700b7
BF
3013 if (!unconf || !same_verf(&confirm, &unconf->cl_confirm)) {
3014 if (conf && !unconf) /* case 2: probable retransmit */
1da177e4 3015 status = nfs_ok;
90d700b7
BF
3016 else /* case 4: client hasn't noticed we rebooted yet? */
3017 status = nfserr_stale_clientid;
3018 goto out;
3019 }
3020 status = nfs_ok;
3021 if (conf) { /* case 1: callback update */
d20c11d8
JL
3022 old = unconf;
3023 unhash_client_locked(old);
8695b90a 3024 nfsd4_change_callback(conf, &unconf->cl_cb_conn);
90d700b7 3025 } else { /* case 3: normal case; new or rebooted client */
d20c11d8
JL
3026 old = find_confirmed_client_by_name(&unconf->cl_name, nn);
3027 if (old) {
3028 status = mark_client_expired_locked(old);
7abea1e8
JL
3029 if (status) {
3030 old = NULL;
221a6876 3031 goto out;
7abea1e8 3032 }
221a6876 3033 }
8695b90a 3034 move_to_confirmed(unconf);
d20c11d8 3035 conf = unconf;
08e8987c 3036 }
d20c11d8
JL
3037 get_client_locked(conf);
3038 spin_unlock(&nn->client_lock);
3039 nfsd4_probe_callback(conf);
3040 spin_lock(&nn->client_lock);
3041 put_client_renew_locked(conf);
1da177e4 3042out:
d20c11d8
JL
3043 spin_unlock(&nn->client_lock);
3044 if (old)
3045 expire_client(old);
1da177e4
LT
3046 return status;
3047}
3048
32513b40
BF
3049static struct nfs4_file *nfsd4_alloc_file(void)
3050{
3051 return kmem_cache_alloc(file_slab, GFP_KERNEL);
3052}
3053
1da177e4 3054/* OPEN Share state helper functions */
f9c00c3a 3055static void nfsd4_init_file(struct nfs4_file *fp, struct knfsd_fh *fh)
1da177e4 3056{
ca943217 3057 unsigned int hashval = file_hashval(fh);
1da177e4 3058
950e0118
TM
3059 lockdep_assert_held(&state_lock);
3060
32513b40 3061 atomic_set(&fp->fi_ref, 1);
1d31a253 3062 spin_lock_init(&fp->fi_lock);
32513b40
BF
3063 INIT_LIST_HEAD(&fp->fi_stateids);
3064 INIT_LIST_HEAD(&fp->fi_delegations);
e2cf80d7 3065 fh_copy_shallow(&fp->fi_fhandle, fh);
32513b40
BF
3066 fp->fi_had_conflict = false;
3067 fp->fi_lease = NULL;
baeb4ff0 3068 fp->fi_share_deny = 0;
32513b40
BF
3069 memset(fp->fi_fds, 0, sizeof(fp->fi_fds));
3070 memset(fp->fi_access, 0, sizeof(fp->fi_access));
89876f8c 3071 hlist_add_head(&fp->fi_hash, &file_hashtbl[hashval]);
1da177e4
LT
3072}
3073
e8ff2a84 3074void
1da177e4
LT
3075nfsd4_free_slabs(void)
3076{
abf1135b
CH
3077 kmem_cache_destroy(openowner_slab);
3078 kmem_cache_destroy(lockowner_slab);
3079 kmem_cache_destroy(file_slab);
3080 kmem_cache_destroy(stateid_slab);
3081 kmem_cache_destroy(deleg_slab);
e60d4398 3082}
1da177e4 3083
72083396 3084int
e60d4398
N
3085nfsd4_init_slabs(void)
3086{
fe0750e5
BF
3087 openowner_slab = kmem_cache_create("nfsd4_openowners",
3088 sizeof(struct nfs4_openowner), 0, 0, NULL);
3089 if (openowner_slab == NULL)
abf1135b 3090 goto out;
fe0750e5 3091 lockowner_slab = kmem_cache_create("nfsd4_lockowners",
3c40794b 3092 sizeof(struct nfs4_lockowner), 0, 0, NULL);
fe0750e5 3093 if (lockowner_slab == NULL)
abf1135b 3094 goto out_free_openowner_slab;
e60d4398 3095 file_slab = kmem_cache_create("nfsd4_files",
20c2df83 3096 sizeof(struct nfs4_file), 0, 0, NULL);
e60d4398 3097 if (file_slab == NULL)
abf1135b 3098 goto out_free_lockowner_slab;
5ac049ac 3099 stateid_slab = kmem_cache_create("nfsd4_stateids",
dcef0413 3100 sizeof(struct nfs4_ol_stateid), 0, 0, NULL);
5ac049ac 3101 if (stateid_slab == NULL)
abf1135b 3102 goto out_free_file_slab;
5b2d21c1 3103 deleg_slab = kmem_cache_create("nfsd4_delegations",
20c2df83 3104 sizeof(struct nfs4_delegation), 0, 0, NULL);
5b2d21c1 3105 if (deleg_slab == NULL)
abf1135b 3106 goto out_free_stateid_slab;
e60d4398 3107 return 0;
abf1135b
CH
3108
3109out_free_stateid_slab:
3110 kmem_cache_destroy(stateid_slab);
3111out_free_file_slab:
3112 kmem_cache_destroy(file_slab);
3113out_free_lockowner_slab:
3114 kmem_cache_destroy(lockowner_slab);
3115out_free_openowner_slab:
3116 kmem_cache_destroy(openowner_slab);
3117out:
e60d4398
N
3118 dprintk("nfsd4: out of memory while initializing nfsv4\n");
3119 return -ENOMEM;
1da177e4
LT
3120}
3121
ff194bd9 3122static void init_nfs4_replay(struct nfs4_replay *rp)
1da177e4 3123{
ff194bd9
BF
3124 rp->rp_status = nfserr_serverfault;
3125 rp->rp_buflen = 0;
3126 rp->rp_buf = rp->rp_ibuf;
58fb12e6
JL
3127 mutex_init(&rp->rp_mutex);
3128}
3129
3130static void nfsd4_cstate_assign_replay(struct nfsd4_compound_state *cstate,
3131 struct nfs4_stateowner *so)
3132{
3133 if (!nfsd4_has_session(cstate)) {
3134 mutex_lock(&so->so_replay.rp_mutex);
3135 cstate->replay_owner = so;
3136 atomic_inc(&so->so_count);
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);
fe0750e5 3235 stp->st_stateowner = &oo->oo_owner;
d3134b10 3236 atomic_inc(&stp->st_stateowner->so_count);
13cd2184 3237 get_nfs4_file(fp);
11b9164a 3238 stp->st_stid.sc_file = fp;
1da177e4
LT
3239 stp->st_access_bmap = 0;
3240 stp->st_deny_bmap = 0;
4c4cd222 3241 stp->st_openstp = NULL;
1c755dc1
JL
3242 spin_lock(&oo->oo_owner.so_client->cl_lock);
3243 list_add(&stp->st_perstateowner, &oo->oo_owner.so_stateids);
1d31a253
TM
3244 spin_lock(&fp->fi_lock);
3245 list_add(&stp->st_perfile, &fp->fi_stateids);
3246 spin_unlock(&fp->fi_lock);
1c755dc1 3247 spin_unlock(&oo->oo_owner.so_client->cl_lock);
1da177e4
LT
3248}
3249
d3134b10
JL
3250/*
3251 * In the 4.0 case we need to keep the owners around a little while to handle
3252 * CLOSE replay. We still do need to release any file access that is held by
3253 * them before returning however.
3254 */
fd39ca9a 3255static void
d3134b10 3256move_to_close_lru(struct nfs4_ol_stateid *s, struct net *net)
1da177e4 3257{
217526e7 3258 struct nfs4_ol_stateid *last;
d3134b10
JL
3259 struct nfs4_openowner *oo = openowner(s->st_stateowner);
3260 struct nfsd_net *nn = net_generic(s->st_stid.sc_client->net,
3261 nfsd_net_id);
73758fed 3262
fe0750e5 3263 dprintk("NFSD: move_to_close_lru nfs4_openowner %p\n", oo);
1da177e4 3264
b401be22
JL
3265 /*
3266 * We know that we hold one reference via nfsd4_close, and another
3267 * "persistent" reference for the client. If the refcount is higher
3268 * than 2, then there are still calls in progress that are using this
3269 * stateid. We can't put the sc_file reference until they are finished.
3270 * Wait for the refcount to drop to 2. Since it has been unhashed,
3271 * there should be no danger of the refcount going back up again at
3272 * this point.
3273 */
3274 wait_event(close_wq, atomic_read(&s->st_stid.sc_count) == 2);
3275
d3134b10
JL
3276 release_all_access(s);
3277 if (s->st_stid.sc_file) {
3278 put_nfs4_file(s->st_stid.sc_file);
3279 s->st_stid.sc_file = NULL;
3280 }
217526e7
JL
3281
3282 spin_lock(&nn->client_lock);
3283 last = oo->oo_last_closed_stid;
d3134b10 3284 oo->oo_last_closed_stid = s;
73758fed 3285 list_move_tail(&oo->oo_close_lru, &nn->close_lru);
fe0750e5 3286 oo->oo_time = get_seconds();
217526e7
JL
3287 spin_unlock(&nn->client_lock);
3288 if (last)
3289 nfs4_put_stid(&last->st_stid);
1da177e4
LT
3290}
3291
1da177e4
LT
3292/* search file_hashtbl[] for file */
3293static struct nfs4_file *
ca943217 3294find_file_locked(struct knfsd_fh *fh)
1da177e4 3295{
ca943217 3296 unsigned int hashval = file_hashval(fh);
1da177e4
LT
3297 struct nfs4_file *fp;
3298
950e0118
TM
3299 lockdep_assert_held(&state_lock);
3300
89876f8c 3301 hlist_for_each_entry(fp, &file_hashtbl[hashval], fi_hash) {
ca943217 3302 if (nfsd_fh_match(&fp->fi_fhandle, fh)) {
13cd2184 3303 get_nfs4_file(fp);
1da177e4 3304 return fp;
13cd2184 3305 }
1da177e4
LT
3306 }
3307 return NULL;
3308}
3309
950e0118 3310static struct nfs4_file *
ca943217 3311find_file(struct knfsd_fh *fh)
950e0118
TM
3312{
3313 struct nfs4_file *fp;
3314
3315 spin_lock(&state_lock);
ca943217 3316 fp = find_file_locked(fh);
950e0118
TM
3317 spin_unlock(&state_lock);
3318 return fp;
3319}
3320
3321static struct nfs4_file *
f9c00c3a 3322find_or_add_file(struct nfs4_file *new, struct knfsd_fh *fh)
950e0118
TM
3323{
3324 struct nfs4_file *fp;
3325
3326 spin_lock(&state_lock);
ca943217 3327 fp = find_file_locked(fh);
950e0118 3328 if (fp == NULL) {
f9c00c3a 3329 nfsd4_init_file(new, fh);
950e0118
TM
3330 fp = new;
3331 }
3332 spin_unlock(&state_lock);
3333
3334 return fp;
3335}
3336
1da177e4
LT
3337/*
3338 * Called to check deny when READ with all zero stateid or
3339 * WRITE with all zero or all one stateid
3340 */
b37ad28b 3341static __be32
1da177e4
LT
3342nfs4_share_conflict(struct svc_fh *current_fh, unsigned int deny_type)
3343{
1da177e4 3344 struct nfs4_file *fp;
baeb4ff0 3345 __be32 ret = nfs_ok;
1da177e4 3346
ca943217 3347 fp = find_file(&current_fh->fh_handle);
13cd2184 3348 if (!fp)
baeb4ff0
JL
3349 return ret;
3350 /* Check for conflicting share reservations */
1d31a253 3351 spin_lock(&fp->fi_lock);
baeb4ff0
JL
3352 if (fp->fi_share_deny & deny_type)
3353 ret = nfserr_locked;
1d31a253 3354 spin_unlock(&fp->fi_lock);
13cd2184
N
3355 put_nfs4_file(fp);
3356 return ret;
1da177e4
LT
3357}
3358
02e1215f 3359void nfsd4_prepare_cb_recall(struct nfs4_delegation *dp)
1da177e4 3360{
11b9164a
TM
3361 struct nfsd_net *nn = net_generic(dp->dl_stid.sc_client->net,
3362 nfsd_net_id);
e8c69d17 3363
11b9164a 3364 block_delegations(&dp->dl_stid.sc_file->fi_fhandle);
f54fe962 3365
dff1399f 3366 /*
f54fe962
JL
3367 * We can't do this in nfsd_break_deleg_cb because it is
3368 * already holding inode->i_lock.
3369 *
dff1399f
JL
3370 * If the dl_time != 0, then we know that it has already been
3371 * queued for a lease break. Don't queue it again.
3372 */
f54fe962 3373 spin_lock(&state_lock);
dff1399f 3374 if (dp->dl_time == 0) {
dff1399f 3375 dp->dl_time = get_seconds();
02e1215f 3376 list_add_tail(&dp->dl_recall_lru, &nn->del_recall_lru);
dff1399f 3377 }
02e1215f
JL
3378 spin_unlock(&state_lock);
3379}
1da177e4 3380
02e1215f
JL
3381static void nfsd_break_one_deleg(struct nfs4_delegation *dp)
3382{
3383 /*
3384 * We're assuming the state code never drops its reference
3385 * without first removing the lease. Since we're in this lease
3386 * callback (and since the lease code is serialized by the kernel
3387 * lock) we know the server hasn't removed the lease yet, we know
3388 * it's safe to take a reference.
3389 */
72c0b0fb 3390 atomic_inc(&dp->dl_stid.sc_count);
6b57d9c8
BF
3391 nfsd4_cb_recall(dp);
3392}
3393
1c8c601a 3394/* Called from break_lease() with i_lock held. */
6b57d9c8
BF
3395static void nfsd_break_deleg_cb(struct file_lock *fl)
3396{
acfdf5c3
BF
3397 struct nfs4_file *fp = (struct nfs4_file *)fl->fl_owner;
3398 struct nfs4_delegation *dp;
6b57d9c8 3399
7fa10cd1
BF
3400 if (!fp) {
3401 WARN(1, "(%p)->fl_owner NULL\n", fl);
3402 return;
3403 }
3404 if (fp->fi_had_conflict) {
3405 WARN(1, "duplicate break on %p\n", fp);
3406 return;
3407 }
0272e1fd
BF
3408 /*
3409 * We don't want the locks code to timeout the lease for us;
acfdf5c3 3410 * we'll remove it ourself if a delegation isn't returned
6b57d9c8 3411 * in time:
0272e1fd
BF
3412 */
3413 fl->fl_break_time = 0;
1da177e4 3414
02e1215f 3415 spin_lock(&fp->fi_lock);
417c6629
JL
3416 fp->fi_had_conflict = true;
3417 /*
3418 * If there are no delegations on the list, then we can't count on this
3419 * lease ever being cleaned up. Set the fl_break_time to jiffies so that
3420 * time_out_leases will do it ASAP. The fact that fi_had_conflict is now
3421 * true should keep any new delegations from being hashed.
3422 */
3423 if (list_empty(&fp->fi_delegations))
3424 fl->fl_break_time = jiffies;
3425 else
3426 list_for_each_entry(dp, &fp->fi_delegations, dl_perfile)
3427 nfsd_break_one_deleg(dp);
02e1215f 3428 spin_unlock(&fp->fi_lock);
1da177e4
LT
3429}
3430
1da177e4
LT
3431static
3432int nfsd_change_deleg_cb(struct file_lock **onlist, int arg)
3433{
3434 if (arg & F_UNLCK)
3435 return lease_modify(onlist, arg);
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;
edab9782 3781 struct file_lock *fl;
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;
3795 status = vfs_setlease(filp, fl->fl_type, &fl);
3796 if (status) {
3797 locks_free_lock(fl);
3798 goto out_fput;
3799 }
3800 spin_lock(&state_lock);
3801 spin_lock(&fp->fi_lock);
3802 /* Did the lease get broken before we took the lock? */
3803 status = -EAGAIN;
3804 if (fp->fi_had_conflict)
3805 goto out_unlock;
3806 /* Race breaker */
3807 if (fp->fi_lease) {
3808 status = 0;
3809 atomic_inc(&fp->fi_delegees);
3810 hash_delegation_locked(dp, fp);
3811 goto out_unlock;
3812 }
acfdf5c3 3813 fp->fi_lease = fl;
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;
417c6629
JL
3846 if (!fp->fi_lease) {
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
d5b9026a
N
4876/* Hack!: For now, we're defining this just so we can use a pointer to it
4877 * as a unique cookie to identify our (NFSv4's) posix locks. */
7b021967 4878static const struct lock_manager_operations nfsd_posix_mng_ops = {
d5b9026a 4879};
1da177e4
LT
4880
4881static inline void
4882nfs4_set_lock_denied(struct file_lock *fl, struct nfsd4_lock_denied *deny)
4883{
fe0750e5 4884 struct nfs4_lockowner *lo;
1da177e4 4885
d5b9026a 4886 if (fl->fl_lmops == &nfsd_posix_mng_ops) {
fe0750e5
BF
4887 lo = (struct nfs4_lockowner *) fl->fl_owner;
4888 deny->ld_owner.data = kmemdup(lo->lo_owner.so_owner.data,
4889 lo->lo_owner.so_owner.len, GFP_KERNEL);
7c13f344
BF
4890 if (!deny->ld_owner.data)
4891 /* We just don't care that much */
4892 goto nevermind;
fe0750e5
BF
4893 deny->ld_owner.len = lo->lo_owner.so_owner.len;
4894 deny->ld_clientid = lo->lo_owner.so_client->cl_clientid;
d5b9026a 4895 } else {
7c13f344
BF
4896nevermind:
4897 deny->ld_owner.len = 0;
4898 deny->ld_owner.data = NULL;
d5b9026a
N
4899 deny->ld_clientid.cl_boot = 0;
4900 deny->ld_clientid.cl_id = 0;
1da177e4
LT
4901 }
4902 deny->ld_start = fl->fl_start;
87df4de8
BH
4903 deny->ld_length = NFS4_MAX_UINT64;
4904 if (fl->fl_end != NFS4_MAX_UINT64)
1da177e4
LT
4905 deny->ld_length = fl->fl_end - fl->fl_start + 1;
4906 deny->ld_type = NFS4_READ_LT;
4907 if (fl->fl_type != F_RDLCK)
4908 deny->ld_type = NFS4_WRITE_LT;
4909}
4910
fe0750e5 4911static struct nfs4_lockowner *
c58c6610 4912find_lockowner_str_locked(clientid_t *clid, struct xdr_netobj *owner,
d4f0489f 4913 struct nfs4_client *clp)
1da177e4 4914{
d4f0489f 4915 unsigned int strhashval = ownerstr_hashval(owner);
b3c32bcd 4916 struct nfs4_stateowner *so;
1da177e4 4917
0a880a28
TM
4918 lockdep_assert_held(&clp->cl_lock);
4919
d4f0489f
TM
4920 list_for_each_entry(so, &clp->cl_ownerstr_hashtbl[strhashval],
4921 so_strhash) {
b3c32bcd
TM
4922 if (so->so_is_open_owner)
4923 continue;
d4f0489f 4924 if (!same_owner_str(so, owner))
b3c32bcd 4925 continue;
5db1c03f 4926 atomic_inc(&so->so_count);
b3c32bcd 4927 return lockowner(so);
1da177e4
LT
4928 }
4929 return NULL;
4930}
4931
c58c6610
TM
4932static struct nfs4_lockowner *
4933find_lockowner_str(clientid_t *clid, struct xdr_netobj *owner,
d4f0489f 4934 struct nfs4_client *clp)
c58c6610
TM
4935{
4936 struct nfs4_lockowner *lo;
4937
d4f0489f
TM
4938 spin_lock(&clp->cl_lock);
4939 lo = find_lockowner_str_locked(clid, owner, clp);
4940 spin_unlock(&clp->cl_lock);
c58c6610
TM
4941 return lo;
4942}
4943
8f4b54c5
JL
4944static void nfs4_unhash_lockowner(struct nfs4_stateowner *sop)
4945{
c58c6610 4946 unhash_lockowner_locked(lockowner(sop));
8f4b54c5
JL
4947}
4948
6b180f0b
JL
4949static void nfs4_free_lockowner(struct nfs4_stateowner *sop)
4950{
4951 struct nfs4_lockowner *lo = lockowner(sop);
4952
4953 kmem_cache_free(lockowner_slab, lo);
4954}
4955
4956static const struct nfs4_stateowner_operations lockowner_ops = {
8f4b54c5
JL
4957 .so_unhash = nfs4_unhash_lockowner,
4958 .so_free = nfs4_free_lockowner,
6b180f0b
JL
4959};
4960
1da177e4
LT
4961/*
4962 * Alloc a lock owner structure.
4963 * Called in nfsd4_lock - therefore, OPEN and OPEN_CONFIRM (if needed) has
25985edc 4964 * occurred.
1da177e4 4965 *
16bfdaaf 4966 * strhashval = ownerstr_hashval
1da177e4 4967 */
fe0750e5 4968static struct nfs4_lockowner *
c58c6610
TM
4969alloc_init_lock_stateowner(unsigned int strhashval, struct nfs4_client *clp,
4970 struct nfs4_ol_stateid *open_stp,
4971 struct nfsd4_lock *lock)
4972{
c58c6610 4973 struct nfs4_lockowner *lo, *ret;
1da177e4 4974
fe0750e5
BF
4975 lo = alloc_stateowner(lockowner_slab, &lock->lk_new_owner, clp);
4976 if (!lo)
1da177e4 4977 return NULL;
fe0750e5
BF
4978 INIT_LIST_HEAD(&lo->lo_owner.so_stateids);
4979 lo->lo_owner.so_is_open_owner = 0;
5db1c03f 4980 lo->lo_owner.so_seqid = lock->lk_new_lock_seqid;
6b180f0b 4981 lo->lo_owner.so_ops = &lockowner_ops;
d4f0489f 4982 spin_lock(&clp->cl_lock);
c58c6610 4983 ret = find_lockowner_str_locked(&clp->cl_clientid,
d4f0489f 4984 &lock->lk_new_owner, clp);
c58c6610
TM
4985 if (ret == NULL) {
4986 list_add(&lo->lo_owner.so_strhash,
d4f0489f 4987 &clp->cl_ownerstr_hashtbl[strhashval]);
c58c6610
TM
4988 ret = lo;
4989 } else
4990 nfs4_free_lockowner(&lo->lo_owner);
d4f0489f 4991 spin_unlock(&clp->cl_lock);
fe0750e5 4992 return lo;
1da177e4
LT
4993}
4994
356a95ec
JL
4995static void
4996init_lock_stateid(struct nfs4_ol_stateid *stp, struct nfs4_lockowner *lo,
4997 struct nfs4_file *fp, struct inode *inode,
4998 struct nfs4_ol_stateid *open_stp)
1da177e4 4999{
d3b313a4 5000 struct nfs4_client *clp = lo->lo_owner.so_client;
1da177e4 5001
356a95ec
JL
5002 lockdep_assert_held(&clp->cl_lock);
5003
3d0fabd5 5004 atomic_inc(&stp->st_stid.sc_count);
3abdb607 5005 stp->st_stid.sc_type = NFS4_LOCK_STID;
fe0750e5 5006 stp->st_stateowner = &lo->lo_owner;
e4f1dd7f 5007 atomic_inc(&lo->lo_owner.so_count);
13cd2184 5008 get_nfs4_file(fp);
11b9164a 5009 stp->st_stid.sc_file = fp;
b49e084d 5010 stp->st_stid.sc_free = nfs4_free_lock_stateid;
0997b173 5011 stp->st_access_bmap = 0;
1da177e4 5012 stp->st_deny_bmap = open_stp->st_deny_bmap;
4c4cd222 5013 stp->st_openstp = open_stp;
3c87b9b7 5014 list_add(&stp->st_locks, &open_stp->st_locks);
1c755dc1 5015 list_add(&stp->st_perstateowner, &lo->lo_owner.so_stateids);
1d31a253
TM
5016 spin_lock(&fp->fi_lock);
5017 list_add(&stp->st_perfile, &fp->fi_stateids);
5018 spin_unlock(&fp->fi_lock);
1da177e4
LT
5019}
5020
c53530da
JL
5021static struct nfs4_ol_stateid *
5022find_lock_stateid(struct nfs4_lockowner *lo, struct nfs4_file *fp)
5023{
5024 struct nfs4_ol_stateid *lst;
356a95ec
JL
5025 struct nfs4_client *clp = lo->lo_owner.so_client;
5026
5027 lockdep_assert_held(&clp->cl_lock);
c53530da
JL
5028
5029 list_for_each_entry(lst, &lo->lo_owner.so_stateids, st_perstateowner) {
3d0fabd5
TM
5030 if (lst->st_stid.sc_file == fp) {
5031 atomic_inc(&lst->st_stid.sc_count);
c53530da 5032 return lst;
3d0fabd5 5033 }
c53530da
JL
5034 }
5035 return NULL;
5036}
5037
356a95ec
JL
5038static struct nfs4_ol_stateid *
5039find_or_create_lock_stateid(struct nfs4_lockowner *lo, struct nfs4_file *fi,
5040 struct inode *inode, struct nfs4_ol_stateid *ost,
5041 bool *new)
5042{
5043 struct nfs4_stid *ns = NULL;
5044 struct nfs4_ol_stateid *lst;
5045 struct nfs4_openowner *oo = openowner(ost->st_stateowner);
5046 struct nfs4_client *clp = oo->oo_owner.so_client;
5047
5048 spin_lock(&clp->cl_lock);
5049 lst = find_lock_stateid(lo, fi);
5050 if (lst == NULL) {
5051 spin_unlock(&clp->cl_lock);
5052 ns = nfs4_alloc_stid(clp, stateid_slab);
5053 if (ns == NULL)
5054 return NULL;
5055
5056 spin_lock(&clp->cl_lock);
5057 lst = find_lock_stateid(lo, fi);
5058 if (likely(!lst)) {
5059 lst = openlockstateid(ns);
5060 init_lock_stateid(lst, lo, fi, inode, ost);
5061 ns = NULL;
5062 *new = true;
5063 }
5064 }
5065 spin_unlock(&clp->cl_lock);
5066 if (ns)
5067 nfs4_put_stid(ns);
5068 return lst;
5069}
c53530da 5070
fd39ca9a 5071static int
1da177e4
LT
5072check_lock_length(u64 offset, u64 length)
5073{
87df4de8 5074 return ((length == 0) || ((length != NFS4_MAX_UINT64) &&
1da177e4
LT
5075 LOFF_OVERFLOW(offset, length)));
5076}
5077
dcef0413 5078static void get_lock_access(struct nfs4_ol_stateid *lock_stp, u32 access)
0997b173 5079{
11b9164a 5080 struct nfs4_file *fp = lock_stp->st_stid.sc_file;
0997b173 5081
7214e860
JL
5082 lockdep_assert_held(&fp->fi_lock);
5083
82c5ff1b 5084 if (test_access(access, lock_stp))
0997b173 5085 return;
12659651 5086 __nfs4_file_get_access(fp, access);
82c5ff1b 5087 set_access(access, lock_stp);
0997b173
BF
5088}
5089
356a95ec
JL
5090static __be32
5091lookup_or_create_lock_state(struct nfsd4_compound_state *cstate,
5092 struct nfs4_ol_stateid *ost,
5093 struct nfsd4_lock *lock,
5094 struct nfs4_ol_stateid **lst, bool *new)
64a284d0 5095{
5db1c03f 5096 __be32 status;
11b9164a 5097 struct nfs4_file *fi = ost->st_stid.sc_file;
64a284d0
BF
5098 struct nfs4_openowner *oo = openowner(ost->st_stateowner);
5099 struct nfs4_client *cl = oo->oo_owner.so_client;
f9c00c3a 5100 struct inode *inode = cstate->current_fh.fh_dentry->d_inode;
64a284d0
BF
5101 struct nfs4_lockowner *lo;
5102 unsigned int strhashval;
5103
d4f0489f 5104 lo = find_lockowner_str(&cl->cl_clientid, &lock->v.new.owner, cl);
c53530da 5105 if (!lo) {
d4f0489f 5106 strhashval = ownerstr_hashval(&lock->v.new.owner);
c53530da
JL
5107 lo = alloc_init_lock_stateowner(strhashval, cl, ost, lock);
5108 if (lo == NULL)
5109 return nfserr_jukebox;
5110 } else {
5111 /* with an existing lockowner, seqids must be the same */
5db1c03f 5112 status = nfserr_bad_seqid;
c53530da
JL
5113 if (!cstate->minorversion &&
5114 lock->lk_new_lock_seqid != lo->lo_owner.so_seqid)
5db1c03f 5115 goto out;
64a284d0 5116 }
c53530da 5117
356a95ec 5118 *lst = find_or_create_lock_stateid(lo, fi, inode, ost, new);
64a284d0 5119 if (*lst == NULL) {
5db1c03f
JL
5120 status = nfserr_jukebox;
5121 goto out;
64a284d0 5122 }
5db1c03f
JL
5123 status = nfs_ok;
5124out:
5125 nfs4_put_stateowner(&lo->lo_owner);
5126 return status;
64a284d0
BF
5127}
5128
1da177e4
LT
5129/*
5130 * LOCK operation
5131 */
b37ad28b 5132__be32
ca364317 5133nfsd4_lock(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
a4f1706a 5134 struct nfsd4_lock *lock)
1da177e4 5135{
fe0750e5
BF
5136 struct nfs4_openowner *open_sop = NULL;
5137 struct nfs4_lockowner *lock_sop = NULL;
3d0fabd5 5138 struct nfs4_ol_stateid *lock_stp = NULL;
0667b1e9 5139 struct nfs4_ol_stateid *open_stp = NULL;
7214e860 5140 struct nfs4_file *fp;
7d947842 5141 struct file *filp = NULL;
21179d81
JL
5142 struct file_lock *file_lock = NULL;
5143 struct file_lock *conflock = NULL;
b37ad28b 5144 __be32 status = 0;
b34f27aa 5145 int lkflg;
b8dd7b9a 5146 int err;
5db1c03f 5147 bool new = false;
3320fef1
SK
5148 struct net *net = SVC_NET(rqstp);
5149 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
1da177e4
LT
5150
5151 dprintk("NFSD: nfsd4_lock: start=%Ld length=%Ld\n",
5152 (long long) lock->lk_offset,
5153 (long long) lock->lk_length);
5154
1da177e4
LT
5155 if (check_lock_length(lock->lk_offset, lock->lk_length))
5156 return nfserr_inval;
5157
ca364317 5158 if ((status = fh_verify(rqstp, &cstate->current_fh,
8837abca 5159 S_IFREG, NFSD_MAY_LOCK))) {
a6f6ef2f
AA
5160 dprintk("NFSD: nfsd4_lock: permission denied!\n");
5161 return status;
5162 }
5163
1da177e4 5164 if (lock->lk_is_new) {
684e5638
BF
5165 if (nfsd4_has_session(cstate))
5166 /* See rfc 5661 18.10.3: given clientid is ignored: */
5167 memcpy(&lock->v.new.clientid,
5168 &cstate->session->se_client->cl_clientid,
5169 sizeof(clientid_t));
5170
1da177e4 5171 status = nfserr_stale_clientid;
2c142baa 5172 if (STALE_CLIENTID(&lock->lk_new_clientid, nn))
1da177e4 5173 goto out;
1da177e4 5174
1da177e4 5175 /* validate and update open stateid and open seqid */
c0a5d93e 5176 status = nfs4_preprocess_confirmed_seqid_op(cstate,
1da177e4
LT
5177 lock->lk_new_open_seqid,
5178 &lock->lk_new_open_stateid,
3320fef1 5179 &open_stp, nn);
37515177 5180 if (status)
1da177e4 5181 goto out;
fe0750e5 5182 open_sop = openowner(open_stp->st_stateowner);
b34f27aa 5183 status = nfserr_bad_stateid;
684e5638 5184 if (!same_clid(&open_sop->oo_owner.so_client->cl_clientid,
b34f27aa
BF
5185 &lock->v.new.clientid))
5186 goto out;
64a284d0 5187 status = lookup_or_create_lock_state(cstate, open_stp, lock,
5db1c03f 5188 &lock_stp, &new);
3d0fabd5 5189 } else {
dd453dfd 5190 status = nfs4_preprocess_seqid_op(cstate,
fe0750e5
BF
5191 lock->lk_old_lock_seqid,
5192 &lock->lk_old_lock_stateid,
3320fef1 5193 NFS4_LOCK_STID, &lock_stp, nn);
3d0fabd5 5194 }
e1aaa891
BF
5195 if (status)
5196 goto out;
64a284d0 5197 lock_sop = lockowner(lock_stp->st_stateowner);
1da177e4 5198
b34f27aa
BF
5199 lkflg = setlkflg(lock->lk_type);
5200 status = nfs4_check_openmode(lock_stp, lkflg);
5201 if (status)
5202 goto out;
5203
0dd395dc 5204 status = nfserr_grace;
3320fef1 5205 if (locks_in_grace(net) && !lock->lk_reclaim)
0dd395dc
N
5206 goto out;
5207 status = nfserr_no_grace;
3320fef1 5208 if (!locks_in_grace(net) && lock->lk_reclaim)
0dd395dc
N
5209 goto out;
5210
21179d81
JL
5211 file_lock = locks_alloc_lock();
5212 if (!file_lock) {
5213 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
5214 status = nfserr_jukebox;
5215 goto out;
5216 }
5217
11b9164a 5218 fp = lock_stp->st_stid.sc_file;
1da177e4
LT
5219 switch (lock->lk_type) {
5220 case NFS4_READ_LT:
5221 case NFS4_READW_LT:
7214e860
JL
5222 spin_lock(&fp->fi_lock);
5223 filp = find_readable_file_locked(fp);
0997b173
BF
5224 if (filp)
5225 get_lock_access(lock_stp, NFS4_SHARE_ACCESS_READ);
7214e860 5226 spin_unlock(&fp->fi_lock);
21179d81 5227 file_lock->fl_type = F_RDLCK;
529d7b2a 5228 break;
1da177e4
LT
5229 case NFS4_WRITE_LT:
5230 case NFS4_WRITEW_LT:
7214e860
JL
5231 spin_lock(&fp->fi_lock);
5232 filp = find_writeable_file_locked(fp);
0997b173
BF
5233 if (filp)
5234 get_lock_access(lock_stp, NFS4_SHARE_ACCESS_WRITE);
7214e860 5235 spin_unlock(&fp->fi_lock);
21179d81 5236 file_lock->fl_type = F_WRLCK;
529d7b2a 5237 break;
1da177e4
LT
5238 default:
5239 status = nfserr_inval;
5240 goto out;
5241 }
f9d7562f
BF
5242 if (!filp) {
5243 status = nfserr_openmode;
5244 goto out;
5245 }
21179d81
JL
5246 file_lock->fl_owner = (fl_owner_t)lock_sop;
5247 file_lock->fl_pid = current->tgid;
5248 file_lock->fl_file = filp;
5249 file_lock->fl_flags = FL_POSIX;
5250 file_lock->fl_lmops = &nfsd_posix_mng_ops;
5251 file_lock->fl_start = lock->lk_offset;
5252 file_lock->fl_end = last_byte_offset(lock->lk_offset, lock->lk_length);
5253 nfs4_transform_lock_offset(file_lock);
5254
5255 conflock = locks_alloc_lock();
5256 if (!conflock) {
5257 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
5258 status = nfserr_jukebox;
5259 goto out;
5260 }
1da177e4 5261
21179d81 5262 err = vfs_lock_file(filp, F_SETLK, file_lock, conflock);
b8dd7b9a 5263 switch (-err) {
1da177e4 5264 case 0: /* success! */
dcef0413
BF
5265 update_stateid(&lock_stp->st_stid.sc_stateid);
5266 memcpy(&lock->lk_resp_stateid, &lock_stp->st_stid.sc_stateid,
1da177e4 5267 sizeof(stateid_t));
b8dd7b9a 5268 status = 0;
eb76b3fd
AA
5269 break;
5270 case (EAGAIN): /* conflock holds conflicting lock */
5271 status = nfserr_denied;
5272 dprintk("NFSD: nfsd4_lock: conflicting lock found!\n");
21179d81 5273 nfs4_set_lock_denied(conflock, &lock->lk_denied);
eb76b3fd 5274 break;
1da177e4
LT
5275 case (EDEADLK):
5276 status = nfserr_deadlock;
eb76b3fd 5277 break;
3e772463 5278 default:
fd85b817 5279 dprintk("NFSD: nfsd4_lock: vfs_lock_file() failed! status %d\n",err);
3e772463 5280 status = nfserrno(err);
eb76b3fd 5281 break;
1da177e4 5282 }
1da177e4 5283out:
de18643d
TM
5284 if (filp)
5285 fput(filp);
5db1c03f
JL
5286 if (lock_stp) {
5287 /* Bump seqid manually if the 4.0 replay owner is openowner */
5288 if (cstate->replay_owner &&
5289 cstate->replay_owner != &lock_sop->lo_owner &&
5290 seqid_mutating_err(ntohl(status)))
5291 lock_sop->lo_owner.so_seqid++;
5292
5293 /*
5294 * If this is a new, never-before-used stateid, and we are
5295 * returning an error, then just go ahead and release it.
5296 */
5297 if (status && new)
5298 release_lock_stateid(lock_stp);
5299
3d0fabd5 5300 nfs4_put_stid(&lock_stp->st_stid);
5db1c03f 5301 }
0667b1e9
TM
5302 if (open_stp)
5303 nfs4_put_stid(&open_stp->st_stid);
9411b1d4 5304 nfsd4_bump_seqid(cstate, status);
21179d81
JL
5305 if (file_lock)
5306 locks_free_lock(file_lock);
5307 if (conflock)
5308 locks_free_lock(conflock);
1da177e4
LT
5309 return status;
5310}
5311
55ef1274
BF
5312/*
5313 * The NFSv4 spec allows a client to do a LOCKT without holding an OPEN,
5314 * so we do a temporary open here just to get an open file to pass to
5315 * vfs_test_lock. (Arguably perhaps test_lock should be done with an
5316 * inode operation.)
5317 */
04da6e9d 5318static __be32 nfsd_test_lock(struct svc_rqst *rqstp, struct svc_fh *fhp, struct file_lock *lock)
55ef1274
BF
5319{
5320 struct file *file;
04da6e9d
AV
5321 __be32 err = nfsd_open(rqstp, fhp, S_IFREG, NFSD_MAY_READ, &file);
5322 if (!err) {
5323 err = nfserrno(vfs_test_lock(file, lock));
5324 nfsd_close(file);
5325 }
55ef1274
BF
5326 return err;
5327}
5328
1da177e4
LT
5329/*
5330 * LOCKT operation
5331 */
b37ad28b 5332__be32
ca364317
BF
5333nfsd4_lockt(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
5334 struct nfsd4_lockt *lockt)
1da177e4 5335{
21179d81 5336 struct file_lock *file_lock = NULL;
5db1c03f 5337 struct nfs4_lockowner *lo = NULL;
b37ad28b 5338 __be32 status;
7f2210fa 5339 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
1da177e4 5340
5ccb0066 5341 if (locks_in_grace(SVC_NET(rqstp)))
1da177e4
LT
5342 return nfserr_grace;
5343
5344 if (check_lock_length(lockt->lt_offset, lockt->lt_length))
5345 return nfserr_inval;
5346
9b2ef62b 5347 if (!nfsd4_has_session(cstate)) {
4b24ca7d 5348 status = lookup_clientid(&lockt->lt_clientid, cstate, nn);
9b2ef62b
BF
5349 if (status)
5350 goto out;
5351 }
1da177e4 5352
75c096f7 5353 if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0)))
1da177e4 5354 goto out;
1da177e4 5355
21179d81
JL
5356 file_lock = locks_alloc_lock();
5357 if (!file_lock) {
5358 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
5359 status = nfserr_jukebox;
5360 goto out;
5361 }
6cd90662 5362
1da177e4
LT
5363 switch (lockt->lt_type) {
5364 case NFS4_READ_LT:
5365 case NFS4_READW_LT:
21179d81 5366 file_lock->fl_type = F_RDLCK;
1da177e4
LT
5367 break;
5368 case NFS4_WRITE_LT:
5369 case NFS4_WRITEW_LT:
21179d81 5370 file_lock->fl_type = F_WRLCK;
1da177e4
LT
5371 break;
5372 default:
2fdada03 5373 dprintk("NFSD: nfs4_lockt: bad lock type!\n");
1da177e4
LT
5374 status = nfserr_inval;
5375 goto out;
5376 }
5377
d4f0489f
TM
5378 lo = find_lockowner_str(&lockt->lt_clientid, &lockt->lt_owner,
5379 cstate->clp);
fe0750e5 5380 if (lo)
21179d81
JL
5381 file_lock->fl_owner = (fl_owner_t)lo;
5382 file_lock->fl_pid = current->tgid;
5383 file_lock->fl_flags = FL_POSIX;
1da177e4 5384
21179d81
JL
5385 file_lock->fl_start = lockt->lt_offset;
5386 file_lock->fl_end = last_byte_offset(lockt->lt_offset, lockt->lt_length);
1da177e4 5387
21179d81 5388 nfs4_transform_lock_offset(file_lock);
1da177e4 5389
21179d81 5390 status = nfsd_test_lock(rqstp, &cstate->current_fh, file_lock);
04da6e9d 5391 if (status)
fd85b817 5392 goto out;
04da6e9d 5393
21179d81 5394 if (file_lock->fl_type != F_UNLCK) {
1da177e4 5395 status = nfserr_denied;
21179d81 5396 nfs4_set_lock_denied(file_lock, &lockt->lt_denied);
1da177e4
LT
5397 }
5398out:
5db1c03f
JL
5399 if (lo)
5400 nfs4_put_stateowner(&lo->lo_owner);
21179d81
JL
5401 if (file_lock)
5402 locks_free_lock(file_lock);
1da177e4
LT
5403 return status;
5404}
5405
b37ad28b 5406__be32
ca364317 5407nfsd4_locku(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
a4f1706a 5408 struct nfsd4_locku *locku)
1da177e4 5409{
dcef0413 5410 struct nfs4_ol_stateid *stp;
1da177e4 5411 struct file *filp = NULL;
21179d81 5412 struct file_lock *file_lock = NULL;
b37ad28b 5413 __be32 status;
b8dd7b9a 5414 int err;
3320fef1
SK
5415 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
5416
1da177e4
LT
5417 dprintk("NFSD: nfsd4_locku: start=%Ld length=%Ld\n",
5418 (long long) locku->lu_offset,
5419 (long long) locku->lu_length);
5420
5421 if (check_lock_length(locku->lu_offset, locku->lu_length))
5422 return nfserr_inval;
5423
9072d5c6 5424 status = nfs4_preprocess_seqid_op(cstate, locku->lu_seqid,
3320fef1
SK
5425 &locku->lu_stateid, NFS4_LOCK_STID,
5426 &stp, nn);
9072d5c6 5427 if (status)
1da177e4 5428 goto out;
11b9164a 5429 filp = find_any_file(stp->st_stid.sc_file);
f9d7562f
BF
5430 if (!filp) {
5431 status = nfserr_lock_range;
858cc573 5432 goto put_stateid;
f9d7562f 5433 }
21179d81
JL
5434 file_lock = locks_alloc_lock();
5435 if (!file_lock) {
5436 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
5437 status = nfserr_jukebox;
de18643d 5438 goto fput;
21179d81 5439 }
6cd90662 5440
21179d81 5441 file_lock->fl_type = F_UNLCK;
0a262ffb 5442 file_lock->fl_owner = (fl_owner_t)lockowner(stp->st_stateowner);
21179d81
JL
5443 file_lock->fl_pid = current->tgid;
5444 file_lock->fl_file = filp;
5445 file_lock->fl_flags = FL_POSIX;
5446 file_lock->fl_lmops = &nfsd_posix_mng_ops;
5447 file_lock->fl_start = locku->lu_offset;
5448
5449 file_lock->fl_end = last_byte_offset(locku->lu_offset,
5450 locku->lu_length);
5451 nfs4_transform_lock_offset(file_lock);
1da177e4 5452
21179d81 5453 err = vfs_lock_file(filp, F_SETLK, file_lock, NULL);
b8dd7b9a 5454 if (err) {
fd85b817 5455 dprintk("NFSD: nfs4_locku: vfs_lock_file failed!\n");
1da177e4
LT
5456 goto out_nfserr;
5457 }
dcef0413
BF
5458 update_stateid(&stp->st_stid.sc_stateid);
5459 memcpy(&locku->lu_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
de18643d
TM
5460fput:
5461 fput(filp);
858cc573
TM
5462put_stateid:
5463 nfs4_put_stid(&stp->st_stid);
1da177e4 5464out:
9411b1d4 5465 nfsd4_bump_seqid(cstate, status);
21179d81
JL
5466 if (file_lock)
5467 locks_free_lock(file_lock);
1da177e4
LT
5468 return status;
5469
5470out_nfserr:
b8dd7b9a 5471 status = nfserrno(err);
de18643d 5472 goto fput;
1da177e4
LT
5473}
5474
5475/*
5476 * returns
f9c00c3a
JL
5477 * true: locks held by lockowner
5478 * false: no locks held by lockowner
1da177e4 5479 */
f9c00c3a
JL
5480static bool
5481check_for_locks(struct nfs4_file *fp, struct nfs4_lockowner *lowner)
1da177e4
LT
5482{
5483 struct file_lock **flpp;
f9c00c3a
JL
5484 int status = false;
5485 struct file *filp = find_any_file(fp);
5486 struct inode *inode;
5487
5488 if (!filp) {
5489 /* Any valid lock stateid should have some sort of access */
5490 WARN_ON_ONCE(1);
5491 return status;
5492 }
5493
5494 inode = file_inode(filp);
1da177e4 5495
1c8c601a 5496 spin_lock(&inode->i_lock);
1da177e4 5497 for (flpp = &inode->i_flock; *flpp != NULL; flpp = &(*flpp)->fl_next) {
796dadfd 5498 if ((*flpp)->fl_owner == (fl_owner_t)lowner) {
f9c00c3a
JL
5499 status = true;
5500 break;
796dadfd 5501 }
1da177e4 5502 }
1c8c601a 5503 spin_unlock(&inode->i_lock);
f9c00c3a 5504 fput(filp);
1da177e4
LT
5505 return status;
5506}
5507
b37ad28b 5508__be32
b591480b
BF
5509nfsd4_release_lockowner(struct svc_rqst *rqstp,
5510 struct nfsd4_compound_state *cstate,
5511 struct nfsd4_release_lockowner *rlockowner)
1da177e4
LT
5512{
5513 clientid_t *clid = &rlockowner->rl_clientid;
882e9d25
JL
5514 struct nfs4_stateowner *sop;
5515 struct nfs4_lockowner *lo = NULL;
dcef0413 5516 struct nfs4_ol_stateid *stp;
1da177e4 5517 struct xdr_netobj *owner = &rlockowner->rl_owner;
d4f0489f 5518 unsigned int hashval = ownerstr_hashval(owner);
b37ad28b 5519 __be32 status;
7f2210fa 5520 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
c58c6610 5521 struct nfs4_client *clp;
1da177e4
LT
5522
5523 dprintk("nfsd4_release_lockowner clientid: (%08x/%08x):\n",
5524 clid->cl_boot, clid->cl_id);
5525
4b24ca7d 5526 status = lookup_clientid(clid, cstate, nn);
9b2ef62b 5527 if (status)
51f5e783 5528 return status;
9b2ef62b 5529
d4f0489f 5530 clp = cstate->clp;
fd44907c 5531 /* Find the matching lock stateowner */
d4f0489f 5532 spin_lock(&clp->cl_lock);
882e9d25 5533 list_for_each_entry(sop, &clp->cl_ownerstr_hashtbl[hashval],
d4f0489f 5534 so_strhash) {
fd44907c 5535
882e9d25
JL
5536 if (sop->so_is_open_owner || !same_owner_str(sop, owner))
5537 continue;
fd44907c 5538
882e9d25
JL
5539 /* see if there are still any locks associated with it */
5540 lo = lockowner(sop);
5541 list_for_each_entry(stp, &sop->so_stateids, st_perstateowner) {
5542 if (check_for_locks(stp->st_stid.sc_file, lo)) {
5543 status = nfserr_locks_held;
5544 spin_unlock(&clp->cl_lock);
51f5e783 5545 return status;
882e9d25 5546 }
5adfd885 5547 }
882e9d25
JL
5548
5549 atomic_inc(&sop->so_count);
5550 break;
1da177e4 5551 }
c58c6610 5552 spin_unlock(&clp->cl_lock);
882e9d25
JL
5553 if (lo)
5554 release_lockowner(lo);
1da177e4
LT
5555 return status;
5556}
5557
5558static inline struct nfs4_client_reclaim *
a55370a3 5559alloc_reclaim(void)
1da177e4 5560{
a55370a3 5561 return kmalloc(sizeof(struct nfs4_client_reclaim), GFP_KERNEL);
1da177e4
LT
5562}
5563
0ce0c2b5 5564bool
52e19c09 5565nfs4_has_reclaimed_state(const char *name, struct nfsd_net *nn)
c7b9a459 5566{
0ce0c2b5 5567 struct nfs4_client_reclaim *crp;
c7b9a459 5568
52e19c09 5569 crp = nfsd4_find_reclaim_client(name, nn);
0ce0c2b5 5570 return (crp && crp->cr_clp);
c7b9a459
N
5571}
5572
1da177e4
LT
5573/*
5574 * failure => all reset bets are off, nfserr_no_grace...
5575 */
772a9bbb 5576struct nfs4_client_reclaim *
52e19c09 5577nfs4_client_to_reclaim(const char *name, struct nfsd_net *nn)
1da177e4
LT
5578{
5579 unsigned int strhashval;
772a9bbb 5580 struct nfs4_client_reclaim *crp;
1da177e4 5581
a55370a3
N
5582 dprintk("NFSD nfs4_client_to_reclaim NAME: %.*s\n", HEXDIR_LEN, name);
5583 crp = alloc_reclaim();
772a9bbb
JL
5584 if (crp) {
5585 strhashval = clientstr_hashval(name);
5586 INIT_LIST_HEAD(&crp->cr_strhash);
52e19c09 5587 list_add(&crp->cr_strhash, &nn->reclaim_str_hashtbl[strhashval]);
772a9bbb 5588 memcpy(crp->cr_recdir, name, HEXDIR_LEN);
0ce0c2b5 5589 crp->cr_clp = NULL;
52e19c09 5590 nn->reclaim_str_hashtbl_size++;
772a9bbb
JL
5591 }
5592 return crp;
1da177e4
LT
5593}
5594
ce30e539 5595void
52e19c09 5596nfs4_remove_reclaim_record(struct nfs4_client_reclaim *crp, struct nfsd_net *nn)
ce30e539
JL
5597{
5598 list_del(&crp->cr_strhash);
5599 kfree(crp);
52e19c09 5600 nn->reclaim_str_hashtbl_size--;
ce30e539
JL
5601}
5602
2a4317c5 5603void
52e19c09 5604nfs4_release_reclaim(struct nfsd_net *nn)
1da177e4
LT
5605{
5606 struct nfs4_client_reclaim *crp = NULL;
5607 int i;
5608
1da177e4 5609 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
52e19c09
SK
5610 while (!list_empty(&nn->reclaim_str_hashtbl[i])) {
5611 crp = list_entry(nn->reclaim_str_hashtbl[i].next,
1da177e4 5612 struct nfs4_client_reclaim, cr_strhash);
52e19c09 5613 nfs4_remove_reclaim_record(crp, nn);
1da177e4
LT
5614 }
5615 }
063b0fb9 5616 WARN_ON_ONCE(nn->reclaim_str_hashtbl_size);
1da177e4
LT
5617}
5618
5619/*
5620 * called from OPEN, CLAIM_PREVIOUS with a new clientid. */
2a4317c5 5621struct nfs4_client_reclaim *
52e19c09 5622nfsd4_find_reclaim_client(const char *recdir, struct nfsd_net *nn)
1da177e4
LT
5623{
5624 unsigned int strhashval;
1da177e4
LT
5625 struct nfs4_client_reclaim *crp = NULL;
5626
278c931c 5627 dprintk("NFSD: nfs4_find_reclaim_client for recdir %s\n", recdir);
1da177e4 5628
278c931c 5629 strhashval = clientstr_hashval(recdir);
52e19c09 5630 list_for_each_entry(crp, &nn->reclaim_str_hashtbl[strhashval], cr_strhash) {
278c931c 5631 if (same_name(crp->cr_recdir, recdir)) {
1da177e4
LT
5632 return crp;
5633 }
5634 }
5635 return NULL;
5636}
5637
5638/*
5639* Called from OPEN. Look for clientid in reclaim list.
5640*/
b37ad28b 5641__be32
0fe492db
TM
5642nfs4_check_open_reclaim(clientid_t *clid,
5643 struct nfsd4_compound_state *cstate,
5644 struct nfsd_net *nn)
1da177e4 5645{
0fe492db 5646 __be32 status;
a52d726b
JL
5647
5648 /* find clientid in conf_id_hashtbl */
0fe492db
TM
5649 status = lookup_clientid(clid, cstate, nn);
5650 if (status)
a52d726b
JL
5651 return nfserr_reclaim_bad;
5652
0fe492db
TM
5653 if (nfsd4_client_record_check(cstate->clp))
5654 return nfserr_reclaim_bad;
5655
5656 return nfs_ok;
1da177e4
LT
5657}
5658
65178db4 5659#ifdef CONFIG_NFSD_FAULT_INJECTION
016200c3
JL
5660static inline void
5661put_client(struct nfs4_client *clp)
5662{
5663 atomic_dec(&clp->cl_refcount);
5664}
5665
285abdee
JL
5666static struct nfs4_client *
5667nfsd_find_client(struct sockaddr_storage *addr, size_t addr_size)
5668{
5669 struct nfs4_client *clp;
5670 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
5671 nfsd_net_id);
5672
5673 if (!nfsd_netns_ready(nn))
5674 return NULL;
5675
5676 list_for_each_entry(clp, &nn->client_lru, cl_lru) {
5677 if (memcmp(&clp->cl_addr, addr, addr_size) == 0)
5678 return clp;
5679 }
5680 return NULL;
5681}
5682
7ec0e36f 5683u64
285abdee 5684nfsd_inject_print_clients(void)
7ec0e36f
JL
5685{
5686 struct nfs4_client *clp;
5687 u64 count = 0;
5688 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
5689 nfsd_net_id);
5690 char buf[INET6_ADDRSTRLEN];
5691
5692 if (!nfsd_netns_ready(nn))
5693 return 0;
5694
5695 spin_lock(&nn->client_lock);
5696 list_for_each_entry(clp, &nn->client_lru, cl_lru) {
5697 rpc_ntop((struct sockaddr *)&clp->cl_addr, buf, sizeof(buf));
5698 pr_info("NFS Client: %s\n", buf);
5699 ++count;
5700 }
5701 spin_unlock(&nn->client_lock);
5702
5703 return count;
5704}
65178db4 5705
a0926d15 5706u64
285abdee 5707nfsd_inject_forget_client(struct sockaddr_storage *addr, size_t addr_size)
a0926d15
JL
5708{
5709 u64 count = 0;
5710 struct nfs4_client *clp;
5711 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
5712 nfsd_net_id);
5713
5714 if (!nfsd_netns_ready(nn))
5715 return count;
5716
5717 spin_lock(&nn->client_lock);
5718 clp = nfsd_find_client(addr, addr_size);
5719 if (clp) {
5720 if (mark_client_expired_locked(clp) == nfs_ok)
5721 ++count;
5722 else
5723 clp = NULL;
5724 }
5725 spin_unlock(&nn->client_lock);
5726
5727 if (clp)
5728 expire_client(clp);
5729
5730 return count;
5731}
5732
69fc9edf 5733u64
285abdee 5734nfsd_inject_forget_clients(u64 max)
69fc9edf
JL
5735{
5736 u64 count = 0;
5737 struct nfs4_client *clp, *next;
5738 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
5739 nfsd_net_id);
5740 LIST_HEAD(reaplist);
5741
5742 if (!nfsd_netns_ready(nn))
5743 return count;
5744
5745 spin_lock(&nn->client_lock);
5746 list_for_each_entry_safe(clp, next, &nn->client_lru, cl_lru) {
5747 if (mark_client_expired_locked(clp) == nfs_ok) {
5748 list_add(&clp->cl_lru, &reaplist);
5749 if (max != 0 && ++count >= max)
5750 break;
5751 }
5752 }
5753 spin_unlock(&nn->client_lock);
5754
5755 list_for_each_entry_safe(clp, next, &reaplist, cl_lru)
5756 expire_client(clp);
5757
5758 return count;
5759}
5760
184c1847
BS
5761static void nfsd_print_count(struct nfs4_client *clp, unsigned int count,
5762 const char *type)
5763{
5764 char buf[INET6_ADDRSTRLEN];
0a5c33e2 5765 rpc_ntop((struct sockaddr *)&clp->cl_addr, buf, sizeof(buf));
184c1847
BS
5766 printk(KERN_INFO "NFS Client: %s has %u %s\n", buf, count, type);
5767}
5768
016200c3
JL
5769static void
5770nfsd_inject_add_lock_to_list(struct nfs4_ol_stateid *lst,
5771 struct list_head *collect)
5772{
5773 struct nfs4_client *clp = lst->st_stid.sc_client;
5774 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
5775 nfsd_net_id);
5776
5777 if (!collect)
5778 return;
5779
5780 lockdep_assert_held(&nn->client_lock);
5781 atomic_inc(&clp->cl_refcount);
5782 list_add(&lst->st_locks, collect);
5783}
5784
3c87b9b7 5785static u64 nfsd_foreach_client_lock(struct nfs4_client *clp, u64 max,
3738d50e 5786 struct list_head *collect,
3c87b9b7 5787 void (*func)(struct nfs4_ol_stateid *))
fc29171f
BS
5788{
5789 struct nfs4_openowner *oop;
fc29171f 5790 struct nfs4_ol_stateid *stp, *st_next;
3c87b9b7 5791 struct nfs4_ol_stateid *lst, *lst_next;
fc29171f
BS
5792 u64 count = 0;
5793
016200c3 5794 spin_lock(&clp->cl_lock);
fc29171f 5795 list_for_each_entry(oop, &clp->cl_openowners, oo_perclient) {
3c87b9b7
TM
5796 list_for_each_entry_safe(stp, st_next,
5797 &oop->oo_owner.so_stateids, st_perstateowner) {
5798 list_for_each_entry_safe(lst, lst_next,
5799 &stp->st_locks, st_locks) {
3738d50e 5800 if (func) {
3c87b9b7 5801 func(lst);
016200c3
JL
5802 nfsd_inject_add_lock_to_list(lst,
5803 collect);
3738d50e 5804 }
016200c3
JL
5805 ++count;
5806 /*
5807 * Despite the fact that these functions deal
5808 * with 64-bit integers for "count", we must
5809 * ensure that it doesn't blow up the
5810 * clp->cl_refcount. Throw a warning if we
5811 * start to approach INT_MAX here.
5812 */
5813 WARN_ON_ONCE(count == (INT_MAX / 2));
5814 if (count == max)
5815 goto out;
fc29171f
BS
5816 }
5817 }
5818 }
016200c3
JL
5819out:
5820 spin_unlock(&clp->cl_lock);
fc29171f
BS
5821
5822 return count;
5823}
5824
016200c3
JL
5825static u64
5826nfsd_collect_client_locks(struct nfs4_client *clp, struct list_head *collect,
5827 u64 max)
fc29171f 5828{
016200c3 5829 return nfsd_foreach_client_lock(clp, max, collect, unhash_lock_stateid);
fc29171f
BS
5830}
5831
016200c3
JL
5832static u64
5833nfsd_print_client_locks(struct nfs4_client *clp)
184c1847 5834{
016200c3 5835 u64 count = nfsd_foreach_client_lock(clp, 0, NULL, NULL);
184c1847
BS
5836 nfsd_print_count(clp, count, "locked files");
5837 return count;
5838}
5839
016200c3 5840u64
285abdee 5841nfsd_inject_print_locks(void)
016200c3
JL
5842{
5843 struct nfs4_client *clp;
5844 u64 count = 0;
5845 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
5846 nfsd_net_id);
5847
5848 if (!nfsd_netns_ready(nn))
5849 return 0;
5850
5851 spin_lock(&nn->client_lock);
5852 list_for_each_entry(clp, &nn->client_lru, cl_lru)
5853 count += nfsd_print_client_locks(clp);
5854 spin_unlock(&nn->client_lock);
5855
5856 return count;
5857}
5858
5859static void
5860nfsd_reap_locks(struct list_head *reaplist)
5861{
5862 struct nfs4_client *clp;
5863 struct nfs4_ol_stateid *stp, *next;
5864
5865 list_for_each_entry_safe(stp, next, reaplist, st_locks) {
5866 list_del_init(&stp->st_locks);
5867 clp = stp->st_stid.sc_client;
5868 nfs4_put_stid(&stp->st_stid);
5869 put_client(clp);
5870 }
5871}
5872
5873u64
285abdee 5874nfsd_inject_forget_client_locks(struct sockaddr_storage *addr, size_t addr_size)
016200c3
JL
5875{
5876 unsigned int count = 0;
5877 struct nfs4_client *clp;
5878 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
5879 nfsd_net_id);
5880 LIST_HEAD(reaplist);
5881
5882 if (!nfsd_netns_ready(nn))
5883 return count;
5884
5885 spin_lock(&nn->client_lock);
5886 clp = nfsd_find_client(addr, addr_size);
5887 if (clp)
5888 count = nfsd_collect_client_locks(clp, &reaplist, 0);
5889 spin_unlock(&nn->client_lock);
5890 nfsd_reap_locks(&reaplist);
5891 return count;
5892}
5893
5894u64
285abdee 5895nfsd_inject_forget_locks(u64 max)
016200c3
JL
5896{
5897 u64 count = 0;
5898 struct nfs4_client *clp;
5899 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
5900 nfsd_net_id);
5901 LIST_HEAD(reaplist);
5902
5903 if (!nfsd_netns_ready(nn))
5904 return count;
5905
5906 spin_lock(&nn->client_lock);
5907 list_for_each_entry(clp, &nn->client_lru, cl_lru) {
5908 count += nfsd_collect_client_locks(clp, &reaplist, max - count);
5909 if (max != 0 && count >= max)
5910 break;
5911 }
5912 spin_unlock(&nn->client_lock);
5913 nfsd_reap_locks(&reaplist);
5914 return count;
5915}
5916
82e05efa
JL
5917static u64
5918nfsd_foreach_client_openowner(struct nfs4_client *clp, u64 max,
5919 struct list_head *collect,
5920 void (*func)(struct nfs4_openowner *))
4dbdbda8
BS
5921{
5922 struct nfs4_openowner *oop, *next;
82e05efa
JL
5923 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
5924 nfsd_net_id);
4dbdbda8
BS
5925 u64 count = 0;
5926
82e05efa
JL
5927 lockdep_assert_held(&nn->client_lock);
5928
5929 spin_lock(&clp->cl_lock);
4dbdbda8 5930 list_for_each_entry_safe(oop, next, &clp->cl_openowners, oo_perclient) {
82e05efa 5931 if (func) {
4dbdbda8 5932 func(oop);
82e05efa
JL
5933 if (collect) {
5934 atomic_inc(&clp->cl_refcount);
5935 list_add(&oop->oo_perclient, collect);
5936 }
5937 }
5938 ++count;
5939 /*
5940 * Despite the fact that these functions deal with
5941 * 64-bit integers for "count", we must ensure that
5942 * it doesn't blow up the clp->cl_refcount. Throw a
5943 * warning if we start to approach INT_MAX here.
5944 */
5945 WARN_ON_ONCE(count == (INT_MAX / 2));
5946 if (count == max)
4dbdbda8
BS
5947 break;
5948 }
82e05efa
JL
5949 spin_unlock(&clp->cl_lock);
5950
5951 return count;
5952}
5953
5954static u64
5955nfsd_print_client_openowners(struct nfs4_client *clp)
5956{
5957 u64 count = nfsd_foreach_client_openowner(clp, 0, NULL, NULL);
5958
5959 nfsd_print_count(clp, count, "openowners");
5960 return count;
5961}
5962
5963static u64
5964nfsd_collect_client_openowners(struct nfs4_client *clp,
5965 struct list_head *collect, u64 max)
5966{
5967 return nfsd_foreach_client_openowner(clp, max, collect,
5968 unhash_openowner_locked);
5969}
5970
5971u64
285abdee 5972nfsd_inject_print_openowners(void)
82e05efa
JL
5973{
5974 struct nfs4_client *clp;
5975 u64 count = 0;
5976 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
5977 nfsd_net_id);
5978
5979 if (!nfsd_netns_ready(nn))
5980 return 0;
5981
5982 spin_lock(&nn->client_lock);
5983 list_for_each_entry(clp, &nn->client_lru, cl_lru)
5984 count += nfsd_print_client_openowners(clp);
5985 spin_unlock(&nn->client_lock);
4dbdbda8
BS
5986
5987 return count;
5988}
5989
82e05efa
JL
5990static void
5991nfsd_reap_openowners(struct list_head *reaplist)
5992{
5993 struct nfs4_client *clp;
5994 struct nfs4_openowner *oop, *next;
5995
5996 list_for_each_entry_safe(oop, next, reaplist, oo_perclient) {
5997 list_del_init(&oop->oo_perclient);
5998 clp = oop->oo_owner.so_client;
5999 release_openowner(oop);
6000 put_client(clp);
6001 }
6002}
6003
6004u64
285abdee
JL
6005nfsd_inject_forget_client_openowners(struct sockaddr_storage *addr,
6006 size_t addr_size)
4dbdbda8 6007{
82e05efa
JL
6008 unsigned int count = 0;
6009 struct nfs4_client *clp;
6010 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6011 nfsd_net_id);
6012 LIST_HEAD(reaplist);
6013
6014 if (!nfsd_netns_ready(nn))
6015 return count;
6016
6017 spin_lock(&nn->client_lock);
6018 clp = nfsd_find_client(addr, addr_size);
6019 if (clp)
6020 count = nfsd_collect_client_openowners(clp, &reaplist, 0);
6021 spin_unlock(&nn->client_lock);
6022 nfsd_reap_openowners(&reaplist);
6023 return count;
4dbdbda8
BS
6024}
6025
82e05efa 6026u64
285abdee 6027nfsd_inject_forget_openowners(u64 max)
184c1847 6028{
82e05efa
JL
6029 u64 count = 0;
6030 struct nfs4_client *clp;
6031 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6032 nfsd_net_id);
6033 LIST_HEAD(reaplist);
6034
6035 if (!nfsd_netns_ready(nn))
6036 return count;
6037
6038 spin_lock(&nn->client_lock);
6039 list_for_each_entry(clp, &nn->client_lru, cl_lru) {
6040 count += nfsd_collect_client_openowners(clp, &reaplist,
6041 max - count);
6042 if (max != 0 && count >= max)
6043 break;
6044 }
6045 spin_unlock(&nn->client_lock);
6046 nfsd_reap_openowners(&reaplist);
184c1847
BS
6047 return count;
6048}
6049
269de30f
BS
6050static u64 nfsd_find_all_delegations(struct nfs4_client *clp, u64 max,
6051 struct list_head *victims)
6052{
6053 struct nfs4_delegation *dp, *next;
98d5c7c5
JL
6054 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6055 nfsd_net_id);
269de30f
BS
6056 u64 count = 0;
6057
98d5c7c5
JL
6058 lockdep_assert_held(&nn->client_lock);
6059
6060 spin_lock(&state_lock);
269de30f 6061 list_for_each_entry_safe(dp, next, &clp->cl_delegations, dl_perclnt) {
dff1399f
JL
6062 if (victims) {
6063 /*
6064 * It's not safe to mess with delegations that have a
6065 * non-zero dl_time. They might have already been broken
6066 * and could be processed by the laundromat outside of
6067 * the state_lock. Just leave them be.
6068 */
6069 if (dp->dl_time != 0)
6070 continue;
6071
98d5c7c5 6072 atomic_inc(&clp->cl_refcount);
42690676
JL
6073 unhash_delegation_locked(dp);
6074 list_add(&dp->dl_recall_lru, victims);
dff1399f 6075 }
98d5c7c5
JL
6076 ++count;
6077 /*
6078 * Despite the fact that these functions deal with
6079 * 64-bit integers for "count", we must ensure that
6080 * it doesn't blow up the clp->cl_refcount. Throw a
6081 * warning if we start to approach INT_MAX here.
6082 */
6083 WARN_ON_ONCE(count == (INT_MAX / 2));
6084 if (count == max)
269de30f
BS
6085 break;
6086 }
98d5c7c5 6087 spin_unlock(&state_lock);
269de30f
BS
6088 return count;
6089}
6090
98d5c7c5
JL
6091static u64
6092nfsd_print_client_delegations(struct nfs4_client *clp)
269de30f 6093{
98d5c7c5 6094 u64 count = nfsd_find_all_delegations(clp, 0, NULL);
269de30f 6095
98d5c7c5
JL
6096 nfsd_print_count(clp, count, "delegations");
6097 return count;
6098}
6099
6100u64
285abdee 6101nfsd_inject_print_delegations(void)
98d5c7c5
JL
6102{
6103 struct nfs4_client *clp;
6104 u64 count = 0;
6105 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6106 nfsd_net_id);
6107
6108 if (!nfsd_netns_ready(nn))
6109 return 0;
269de30f 6110
98d5c7c5
JL
6111 spin_lock(&nn->client_lock);
6112 list_for_each_entry(clp, &nn->client_lru, cl_lru)
6113 count += nfsd_print_client_delegations(clp);
6114 spin_unlock(&nn->client_lock);
6115
6116 return count;
6117}
6118
6119static void
6120nfsd_forget_delegations(struct list_head *reaplist)
6121{
6122 struct nfs4_client *clp;
6123 struct nfs4_delegation *dp, *next;
6124
6125 list_for_each_entry_safe(dp, next, reaplist, dl_recall_lru) {
2d4a532d 6126 list_del_init(&dp->dl_recall_lru);
98d5c7c5 6127 clp = dp->dl_stid.sc_client;
3bd64a5b 6128 revoke_delegation(dp);
98d5c7c5 6129 put_client(clp);
2d4a532d 6130 }
98d5c7c5
JL
6131}
6132
6133u64
285abdee
JL
6134nfsd_inject_forget_client_delegations(struct sockaddr_storage *addr,
6135 size_t addr_size)
98d5c7c5
JL
6136{
6137 u64 count = 0;
6138 struct nfs4_client *clp;
6139 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6140 nfsd_net_id);
6141 LIST_HEAD(reaplist);
6142
6143 if (!nfsd_netns_ready(nn))
6144 return count;
6145
6146 spin_lock(&nn->client_lock);
6147 clp = nfsd_find_client(addr, addr_size);
6148 if (clp)
6149 count = nfsd_find_all_delegations(clp, 0, &reaplist);
6150 spin_unlock(&nn->client_lock);
6151
6152 nfsd_forget_delegations(&reaplist);
6153 return count;
6154}
269de30f 6155
98d5c7c5 6156u64
285abdee 6157nfsd_inject_forget_delegations(u64 max)
98d5c7c5
JL
6158{
6159 u64 count = 0;
6160 struct nfs4_client *clp;
6161 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6162 nfsd_net_id);
6163 LIST_HEAD(reaplist);
6164
6165 if (!nfsd_netns_ready(nn))
6166 return count;
6167
6168 spin_lock(&nn->client_lock);
6169 list_for_each_entry(clp, &nn->client_lru, cl_lru) {
6170 count += nfsd_find_all_delegations(clp, max - count, &reaplist);
6171 if (max != 0 && count >= max)
6172 break;
6173 }
6174 spin_unlock(&nn->client_lock);
6175 nfsd_forget_delegations(&reaplist);
269de30f
BS
6176 return count;
6177}
6178
98d5c7c5
JL
6179static void
6180nfsd_recall_delegations(struct list_head *reaplist)
269de30f 6181{
98d5c7c5
JL
6182 struct nfs4_client *clp;
6183 struct nfs4_delegation *dp, *next;
269de30f 6184
98d5c7c5 6185 list_for_each_entry_safe(dp, next, reaplist, dl_recall_lru) {
dff1399f 6186 list_del_init(&dp->dl_recall_lru);
98d5c7c5
JL
6187 clp = dp->dl_stid.sc_client;
6188 /*
6189 * We skipped all entries that had a zero dl_time before,
6190 * so we can now reset the dl_time back to 0. If a delegation
6191 * break comes in now, then it won't make any difference since
6192 * we're recalling it either way.
6193 */
6194 spin_lock(&state_lock);
dff1399f 6195 dp->dl_time = 0;
98d5c7c5 6196 spin_unlock(&state_lock);
269de30f 6197 nfsd_break_one_deleg(dp);
98d5c7c5 6198 put_client(clp);
dff1399f 6199 }
98d5c7c5 6200}
269de30f 6201
98d5c7c5 6202u64
285abdee 6203nfsd_inject_recall_client_delegations(struct sockaddr_storage *addr,
98d5c7c5
JL
6204 size_t addr_size)
6205{
6206 u64 count = 0;
6207 struct nfs4_client *clp;
6208 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6209 nfsd_net_id);
6210 LIST_HEAD(reaplist);
6211
6212 if (!nfsd_netns_ready(nn))
6213 return count;
6214
6215 spin_lock(&nn->client_lock);
6216 clp = nfsd_find_client(addr, addr_size);
6217 if (clp)
6218 count = nfsd_find_all_delegations(clp, 0, &reaplist);
6219 spin_unlock(&nn->client_lock);
6220
6221 nfsd_recall_delegations(&reaplist);
269de30f
BS
6222 return count;
6223}
6224
98d5c7c5 6225u64
285abdee 6226nfsd_inject_recall_delegations(u64 max)
184c1847
BS
6227{
6228 u64 count = 0;
98d5c7c5
JL
6229 struct nfs4_client *clp, *next;
6230 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6231 nfsd_net_id);
6232 LIST_HEAD(reaplist);
184c1847 6233
98d5c7c5
JL
6234 if (!nfsd_netns_ready(nn))
6235 return count;
184c1847 6236
98d5c7c5
JL
6237 spin_lock(&nn->client_lock);
6238 list_for_each_entry_safe(clp, next, &nn->client_lru, cl_lru) {
6239 count += nfsd_find_all_delegations(clp, max - count, &reaplist);
6240 if (max != 0 && ++count >= max)
6241 break;
6242 }
6243 spin_unlock(&nn->client_lock);
6244 nfsd_recall_delegations(&reaplist);
184c1847
BS
6245 return count;
6246}
65178db4
BS
6247#endif /* CONFIG_NFSD_FAULT_INJECTION */
6248
c2f1a551
MS
6249/*
6250 * Since the lifetime of a delegation isn't limited to that of an open, a
6251 * client may quite reasonably hang on to a delegation as long as it has
6252 * the inode cached. This becomes an obvious problem the first time a
6253 * client's inode cache approaches the size of the server's total memory.
6254 *
6255 * For now we avoid this problem by imposing a hard limit on the number
6256 * of delegations, which varies according to the server's memory size.
6257 */
6258static void
6259set_max_delegations(void)
6260{
6261 /*
6262 * Allow at most 4 delegations per megabyte of RAM. Quick
6263 * estimates suggest that in the worst case (where every delegation
6264 * is for a different inode), a delegation could take about 1.5K,
6265 * giving a worst case usage of about 6% of memory.
6266 */
6267 max_delegations = nr_free_buffer_pages() >> (20 - 2 - PAGE_SHIFT);
6268}
6269
d85ed443 6270static int nfs4_state_create_net(struct net *net)
8daae4dc
SK
6271{
6272 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
6273 int i;
6274
6275 nn->conf_id_hashtbl = kmalloc(sizeof(struct list_head) *
6276 CLIENT_HASH_SIZE, GFP_KERNEL);
6277 if (!nn->conf_id_hashtbl)
382a62e7 6278 goto err;
0a7ec377
SK
6279 nn->unconf_id_hashtbl = kmalloc(sizeof(struct list_head) *
6280 CLIENT_HASH_SIZE, GFP_KERNEL);
6281 if (!nn->unconf_id_hashtbl)
6282 goto err_unconf_id;
1872de0e
SK
6283 nn->sessionid_hashtbl = kmalloc(sizeof(struct list_head) *
6284 SESSION_HASH_SIZE, GFP_KERNEL);
6285 if (!nn->sessionid_hashtbl)
6286 goto err_sessionid;
8daae4dc 6287
382a62e7 6288 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
8daae4dc 6289 INIT_LIST_HEAD(&nn->conf_id_hashtbl[i]);
0a7ec377 6290 INIT_LIST_HEAD(&nn->unconf_id_hashtbl[i]);
382a62e7 6291 }
1872de0e
SK
6292 for (i = 0; i < SESSION_HASH_SIZE; i++)
6293 INIT_LIST_HEAD(&nn->sessionid_hashtbl[i]);
382a62e7 6294 nn->conf_name_tree = RB_ROOT;
a99454aa 6295 nn->unconf_name_tree = RB_ROOT;
5ed58bb2 6296 INIT_LIST_HEAD(&nn->client_lru);
73758fed 6297 INIT_LIST_HEAD(&nn->close_lru);
e8c69d17 6298 INIT_LIST_HEAD(&nn->del_recall_lru);
c9a49628 6299 spin_lock_init(&nn->client_lock);
8daae4dc 6300
09121281 6301 INIT_DELAYED_WORK(&nn->laundromat_work, laundromat_main);
d85ed443 6302 get_net(net);
09121281 6303
8daae4dc 6304 return 0;
382a62e7 6305
1872de0e 6306err_sessionid:
9b531137 6307 kfree(nn->unconf_id_hashtbl);
0a7ec377
SK
6308err_unconf_id:
6309 kfree(nn->conf_id_hashtbl);
382a62e7
SK
6310err:
6311 return -ENOMEM;
8daae4dc
SK
6312}
6313
6314static void
4dce0ac9 6315nfs4_state_destroy_net(struct net *net)
8daae4dc
SK
6316{
6317 int i;
6318 struct nfs4_client *clp = NULL;
6319 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
6320
6321 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
6322 while (!list_empty(&nn->conf_id_hashtbl[i])) {
6323 clp = list_entry(nn->conf_id_hashtbl[i].next, struct nfs4_client, cl_idhash);
6324 destroy_client(clp);
6325 }
6326 }
a99454aa 6327
2b905635
KM
6328 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
6329 while (!list_empty(&nn->unconf_id_hashtbl[i])) {
6330 clp = list_entry(nn->unconf_id_hashtbl[i].next, struct nfs4_client, cl_idhash);
6331 destroy_client(clp);
6332 }
a99454aa
SK
6333 }
6334
1872de0e 6335 kfree(nn->sessionid_hashtbl);
0a7ec377 6336 kfree(nn->unconf_id_hashtbl);
8daae4dc 6337 kfree(nn->conf_id_hashtbl);
4dce0ac9 6338 put_net(net);
8daae4dc
SK
6339}
6340
f252bc68 6341int
d85ed443 6342nfs4_state_start_net(struct net *net)
ac4d8ff2 6343{
5e1533c7 6344 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
b5a1a81e
BF
6345 int ret;
6346
d85ed443 6347 ret = nfs4_state_create_net(net);
8daae4dc
SK
6348 if (ret)
6349 return ret;
5e1533c7 6350 nfsd4_client_tracking_init(net);
2c142baa 6351 nn->boot_time = get_seconds();
5ccb0066 6352 locks_start_grace(net, &nn->nfsd4_manager);
a51c84ed 6353 nn->grace_ended = false;
d85ed443 6354 printk(KERN_INFO "NFSD: starting %ld-second grace period (net %p)\n",
5284b44e
SK
6355 nn->nfsd4_grace, net);
6356 queue_delayed_work(laundry_wq, &nn->laundromat_work, nn->nfsd4_grace * HZ);
d85ed443
SK
6357 return 0;
6358}
6359
6360/* initialization to perform when the nfsd service is started: */
6361
6362int
6363nfs4_state_start(void)
6364{
6365 int ret;
6366
b5a1a81e 6367 ret = set_callback_cred();
d85ed443
SK
6368 if (ret)
6369 return -ENOMEM;
58da282b 6370 laundry_wq = create_singlethread_workqueue("nfsd4");
a6d6b781
JL
6371 if (laundry_wq == NULL) {
6372 ret = -ENOMEM;
6373 goto out_recovery;
6374 }
b5a1a81e
BF
6375 ret = nfsd4_create_callback_queue();
6376 if (ret)
6377 goto out_free_laundry;
09121281 6378
c2f1a551 6379 set_max_delegations();
d85ed443 6380
b5a1a81e 6381 return 0;
d85ed443 6382
b5a1a81e
BF
6383out_free_laundry:
6384 destroy_workqueue(laundry_wq);
a6d6b781 6385out_recovery:
b5a1a81e 6386 return ret;
1da177e4
LT
6387}
6388
f252bc68 6389void
4dce0ac9 6390nfs4_state_shutdown_net(struct net *net)
1da177e4 6391{
1da177e4 6392 struct nfs4_delegation *dp = NULL;
1da177e4 6393 struct list_head *pos, *next, reaplist;
4dce0ac9 6394 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
1da177e4 6395
4dce0ac9
SK
6396 cancel_delayed_work_sync(&nn->laundromat_work);
6397 locks_end_grace(&nn->nfsd4_manager);
ac55fdc4 6398
1da177e4 6399 INIT_LIST_HEAD(&reaplist);
cdc97505 6400 spin_lock(&state_lock);
e8c69d17 6401 list_for_each_safe(pos, next, &nn->del_recall_lru) {
1da177e4 6402 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
42690676
JL
6403 unhash_delegation_locked(dp);
6404 list_add(&dp->dl_recall_lru, &reaplist);
1da177e4 6405 }
cdc97505 6406 spin_unlock(&state_lock);
1da177e4
LT
6407 list_for_each_safe(pos, next, &reaplist) {
6408 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
42690676 6409 list_del_init(&dp->dl_recall_lru);
afbda402 6410 nfs4_put_deleg_lease(dp->dl_stid.sc_file);
6011695d 6411 nfs4_put_stid(&dp->dl_stid);
1da177e4
LT
6412 }
6413
3320fef1 6414 nfsd4_client_tracking_exit(net);
4dce0ac9 6415 nfs4_state_destroy_net(net);
1da177e4
LT
6416}
6417
6418void
6419nfs4_state_shutdown(void)
6420{
5e8d5c29 6421 destroy_workqueue(laundry_wq);
c3935e30 6422 nfsd4_destroy_callback_queue();
1da177e4 6423}
8b70484c
TM
6424
6425static void
6426get_stateid(struct nfsd4_compound_state *cstate, stateid_t *stateid)
6427{
37c593c5
TM
6428 if (HAS_STATE_ID(cstate, CURRENT_STATE_ID_FLAG) && CURRENT_STATEID(stateid))
6429 memcpy(stateid, &cstate->current_stateid, sizeof(stateid_t));
8b70484c
TM
6430}
6431
6432static void
6433put_stateid(struct nfsd4_compound_state *cstate, stateid_t *stateid)
6434{
37c593c5
TM
6435 if (cstate->minorversion) {
6436 memcpy(&cstate->current_stateid, stateid, sizeof(stateid_t));
6437 SET_STATE_ID(cstate, CURRENT_STATE_ID_FLAG);
6438 }
6439}
6440
6441void
6442clear_current_stateid(struct nfsd4_compound_state *cstate)
6443{
6444 CLEAR_STATE_ID(cstate, CURRENT_STATE_ID_FLAG);
8b70484c
TM
6445}
6446
62cd4a59
TM
6447/*
6448 * functions to set current state id
6449 */
9428fe1a
TM
6450void
6451nfsd4_set_opendowngradestateid(struct nfsd4_compound_state *cstate, struct nfsd4_open_downgrade *odp)
6452{
6453 put_stateid(cstate, &odp->od_stateid);
6454}
6455
8b70484c
TM
6456void
6457nfsd4_set_openstateid(struct nfsd4_compound_state *cstate, struct nfsd4_open *open)
6458{
6459 put_stateid(cstate, &open->op_stateid);
6460}
6461
62cd4a59
TM
6462void
6463nfsd4_set_closestateid(struct nfsd4_compound_state *cstate, struct nfsd4_close *close)
6464{
6465 put_stateid(cstate, &close->cl_stateid);
6466}
6467
6468void
6469nfsd4_set_lockstateid(struct nfsd4_compound_state *cstate, struct nfsd4_lock *lock)
6470{
6471 put_stateid(cstate, &lock->lk_resp_stateid);
6472}
6473
6474/*
6475 * functions to consume current state id
6476 */
1e97b519 6477
9428fe1a
TM
6478void
6479nfsd4_get_opendowngradestateid(struct nfsd4_compound_state *cstate, struct nfsd4_open_downgrade *odp)
6480{
6481 get_stateid(cstate, &odp->od_stateid);
6482}
6483
6484void
6485nfsd4_get_delegreturnstateid(struct nfsd4_compound_state *cstate, struct nfsd4_delegreturn *drp)
6486{
6487 get_stateid(cstate, &drp->dr_stateid);
6488}
6489
1e97b519
TM
6490void
6491nfsd4_get_freestateid(struct nfsd4_compound_state *cstate, struct nfsd4_free_stateid *fsp)
6492{
6493 get_stateid(cstate, &fsp->fr_stateid);
6494}
6495
6496void
6497nfsd4_get_setattrstateid(struct nfsd4_compound_state *cstate, struct nfsd4_setattr *setattr)
6498{
6499 get_stateid(cstate, &setattr->sa_stateid);
6500}
6501
8b70484c
TM
6502void
6503nfsd4_get_closestateid(struct nfsd4_compound_state *cstate, struct nfsd4_close *close)
6504{
6505 get_stateid(cstate, &close->cl_stateid);
6506}
6507
6508void
62cd4a59 6509nfsd4_get_lockustateid(struct nfsd4_compound_state *cstate, struct nfsd4_locku *locku)
8b70484c 6510{
62cd4a59 6511 get_stateid(cstate, &locku->lu_stateid);
8b70484c 6512}
30813e27
TM
6513
6514void
6515nfsd4_get_readstateid(struct nfsd4_compound_state *cstate, struct nfsd4_read *read)
6516{
6517 get_stateid(cstate, &read->rd_stateid);
6518}
6519
6520void
6521nfsd4_get_writestateid(struct nfsd4_compound_state *cstate, struct nfsd4_write *write)
6522{
6523 get_stateid(cstate, &write->wr_stateid);
6524}
This page took 1.292712 seconds and 5 git commands to generate.