nfsd: introduce a generic nfsd4_cb
[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);
326129d0
CH
3391 dp->dl_retries = 1;
3392 nfsd4_cb(&dp->dl_recall, dp->dl_stid.sc_client,
3393 NFSPROC4_CLNT_CB_RECALL);
6b57d9c8
BF
3394}
3395
1c8c601a 3396/* Called from break_lease() with i_lock held. */
6b57d9c8
BF
3397static void nfsd_break_deleg_cb(struct file_lock *fl)
3398{
acfdf5c3
BF
3399 struct nfs4_file *fp = (struct nfs4_file *)fl->fl_owner;
3400 struct nfs4_delegation *dp;
6b57d9c8 3401
7fa10cd1
BF
3402 if (!fp) {
3403 WARN(1, "(%p)->fl_owner NULL\n", fl);
3404 return;
3405 }
3406 if (fp->fi_had_conflict) {
3407 WARN(1, "duplicate break on %p\n", fp);
3408 return;
3409 }
0272e1fd
BF
3410 /*
3411 * We don't want the locks code to timeout the lease for us;
acfdf5c3 3412 * we'll remove it ourself if a delegation isn't returned
6b57d9c8 3413 * in time:
0272e1fd
BF
3414 */
3415 fl->fl_break_time = 0;
1da177e4 3416
02e1215f 3417 spin_lock(&fp->fi_lock);
417c6629
JL
3418 fp->fi_had_conflict = true;
3419 /*
3420 * If there are no delegations on the list, then we can't count on this
3421 * lease ever being cleaned up. Set the fl_break_time to jiffies so that
3422 * time_out_leases will do it ASAP. The fact that fi_had_conflict is now
3423 * true should keep any new delegations from being hashed.
3424 */
3425 if (list_empty(&fp->fi_delegations))
3426 fl->fl_break_time = jiffies;
3427 else
3428 list_for_each_entry(dp, &fp->fi_delegations, dl_perfile)
3429 nfsd_break_one_deleg(dp);
02e1215f 3430 spin_unlock(&fp->fi_lock);
1da177e4
LT
3431}
3432
1da177e4
LT
3433static
3434int nfsd_change_deleg_cb(struct file_lock **onlist, int arg)
3435{
3436 if (arg & F_UNLCK)
3437 return lease_modify(onlist, arg);
3438 else
3439 return -EAGAIN;
3440}
3441
7b021967 3442static const struct lock_manager_operations nfsd_lease_mng_ops = {
8fb47a4f
BF
3443 .lm_break = nfsd_break_deleg_cb,
3444 .lm_change = nfsd_change_deleg_cb,
1da177e4
LT
3445};
3446
7a8711c9
BF
3447static __be32 nfsd4_check_seqid(struct nfsd4_compound_state *cstate, struct nfs4_stateowner *so, u32 seqid)
3448{
3449 if (nfsd4_has_session(cstate))
3450 return nfs_ok;
3451 if (seqid == so->so_seqid - 1)
3452 return nfserr_replay_me;
3453 if (seqid == so->so_seqid)
3454 return nfs_ok;
3455 return nfserr_bad_seqid;
3456}
1da177e4 3457
4b24ca7d
JL
3458static __be32 lookup_clientid(clientid_t *clid,
3459 struct nfsd4_compound_state *cstate,
3460 struct nfsd_net *nn)
3461{
3462 struct nfs4_client *found;
3463
3464 if (cstate->clp) {
3465 found = cstate->clp;
3466 if (!same_clid(&found->cl_clientid, clid))
3467 return nfserr_stale_clientid;
3468 return nfs_ok;
3469 }
3470
3471 if (STALE_CLIENTID(clid, nn))
3472 return nfserr_stale_clientid;
3473
3474 /*
3475 * For v4.1+ we get the client in the SEQUENCE op. If we don't have one
3476 * cached already then we know this is for is for v4.0 and "sessions"
3477 * will be false.
3478 */
3479 WARN_ON_ONCE(cstate->session);
3e339f96 3480 spin_lock(&nn->client_lock);
4b24ca7d 3481 found = find_confirmed_client(clid, false, nn);
3e339f96
TM
3482 if (!found) {
3483 spin_unlock(&nn->client_lock);
4b24ca7d 3484 return nfserr_expired;
3e339f96
TM
3485 }
3486 atomic_inc(&found->cl_refcount);
3487 spin_unlock(&nn->client_lock);
4b24ca7d
JL
3488
3489 /* Cache the nfs4_client in cstate! */
3490 cstate->clp = found;
4b24ca7d
JL
3491 return nfs_ok;
3492}
3493
b37ad28b 3494__be32
6668958f 3495nfsd4_process_open1(struct nfsd4_compound_state *cstate,
3320fef1 3496 struct nfsd4_open *open, struct nfsd_net *nn)
1da177e4 3497{
1da177e4
LT
3498 clientid_t *clientid = &open->op_clientid;
3499 struct nfs4_client *clp = NULL;
3500 unsigned int strhashval;
fe0750e5 3501 struct nfs4_openowner *oo = NULL;
4cdc951b 3502 __be32 status;
1da177e4 3503
2c142baa 3504 if (STALE_CLIENTID(&open->op_clientid, nn))
1da177e4 3505 return nfserr_stale_clientid;
32513b40
BF
3506 /*
3507 * In case we need it later, after we've already created the
3508 * file and don't want to risk a further failure:
3509 */
3510 open->op_file = nfsd4_alloc_file();
3511 if (open->op_file == NULL)
3512 return nfserr_jukebox;
1da177e4 3513
2d91e895
TM
3514 status = lookup_clientid(clientid, cstate, nn);
3515 if (status)
3516 return status;
3517 clp = cstate->clp;
3518
d4f0489f
TM
3519 strhashval = ownerstr_hashval(&open->op_owner);
3520 oo = find_openstateowner_str(strhashval, open, clp);
fe0750e5
BF
3521 open->op_openowner = oo;
3522 if (!oo) {
bcf130f9 3523 goto new_owner;
1da177e4 3524 }
dad1c067 3525 if (!(oo->oo_flags & NFS4_OO_CONFIRMED)) {
0f442aa2 3526 /* Replace unconfirmed owners without checking for replay. */
fe0750e5
BF
3527 release_openowner(oo);
3528 open->op_openowner = NULL;
bcf130f9 3529 goto new_owner;
0f442aa2 3530 }
4cdc951b
BF
3531 status = nfsd4_check_seqid(cstate, &oo->oo_owner, open->op_seqid);
3532 if (status)
3533 return status;
4cdc951b 3534 goto alloc_stateid;
bcf130f9 3535new_owner:
13d6f66b 3536 oo = alloc_init_open_stateowner(strhashval, open, cstate);
bcf130f9
BF
3537 if (oo == NULL)
3538 return nfserr_jukebox;
3539 open->op_openowner = oo;
4cdc951b 3540alloc_stateid:
b49e084d 3541 open->op_stp = nfs4_alloc_open_stateid(clp);
4cdc951b
BF
3542 if (!open->op_stp)
3543 return nfserr_jukebox;
0f442aa2 3544 return nfs_ok;
1da177e4
LT
3545}
3546
b37ad28b 3547static inline __be32
4a6e43e6
N
3548nfs4_check_delegmode(struct nfs4_delegation *dp, int flags)
3549{
3550 if ((flags & WR_STATE) && (dp->dl_type == NFS4_OPEN_DELEGATE_READ))
3551 return nfserr_openmode;
3552 else
3553 return nfs_ok;
3554}
3555
f459e453 3556static int share_access_to_flags(u32 share_access)
52f4fb43 3557{
f459e453 3558 return share_access == NFS4_SHARE_ACCESS_READ ? RD_STATE : WR_STATE;
52f4fb43
N
3559}
3560
38c2f4b1 3561static struct nfs4_delegation *find_deleg_stateid(struct nfs4_client *cl, stateid_t *s)
24a0111e 3562{
f459e453 3563 struct nfs4_stid *ret;
24a0111e 3564
38c2f4b1 3565 ret = find_stateid_by_type(cl, s, NFS4_DELEG_STID);
f459e453
BF
3566 if (!ret)
3567 return NULL;
3568 return delegstateid(ret);
24a0111e
BF
3569}
3570
8b289b2c
BF
3571static bool nfsd4_is_deleg_cur(struct nfsd4_open *open)
3572{
3573 return open->op_claim_type == NFS4_OPEN_CLAIM_DELEGATE_CUR ||
3574 open->op_claim_type == NFS4_OPEN_CLAIM_DELEG_CUR_FH;
3575}
3576
b37ad28b 3577static __be32
41d22663 3578nfs4_check_deleg(struct nfs4_client *cl, struct nfsd4_open *open,
567d9829
N
3579 struct nfs4_delegation **dp)
3580{
3581 int flags;
b37ad28b 3582 __be32 status = nfserr_bad_stateid;
dcd94cc2 3583 struct nfs4_delegation *deleg;
567d9829 3584
dcd94cc2
TM
3585 deleg = find_deleg_stateid(cl, &open->op_delegate_stateid);
3586 if (deleg == NULL)
c44c5eeb 3587 goto out;
24a0111e 3588 flags = share_access_to_flags(open->op_share_access);
dcd94cc2
TM
3589 status = nfs4_check_delegmode(deleg, flags);
3590 if (status) {
3591 nfs4_put_stid(&deleg->dl_stid);
3592 goto out;
3593 }
3594 *dp = deleg;
c44c5eeb 3595out:
8b289b2c 3596 if (!nfsd4_is_deleg_cur(open))
c44c5eeb
N
3597 return nfs_ok;
3598 if (status)
3599 return status;
dad1c067 3600 open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED;
c44c5eeb 3601 return nfs_ok;
567d9829
N
3602}
3603
a46cb7f2
JL
3604static struct nfs4_ol_stateid *
3605nfsd4_find_existing_open(struct nfs4_file *fp, struct nfsd4_open *open)
1da177e4 3606{
a46cb7f2 3607 struct nfs4_ol_stateid *local, *ret = NULL;
fe0750e5 3608 struct nfs4_openowner *oo = open->op_openowner;
1da177e4 3609
1d31a253 3610 spin_lock(&fp->fi_lock);
8beefa24 3611 list_for_each_entry(local, &fp->fi_stateids, st_perfile) {
1da177e4
LT
3612 /* ignore lock owners */
3613 if (local->st_stateowner->so_is_open_owner == 0)
3614 continue;
baeb4ff0 3615 if (local->st_stateowner == &oo->oo_owner) {
a46cb7f2 3616 ret = local;
d6f2bc5d 3617 atomic_inc(&ret->st_stid.sc_count);
baeb4ff0 3618 break;
1d31a253 3619 }
1da177e4 3620 }
1d31a253 3621 spin_unlock(&fp->fi_lock);
a46cb7f2 3622 return ret;
1da177e4
LT
3623}
3624
21fb4016
BF
3625static inline int nfs4_access_to_access(u32 nfs4_access)
3626{
3627 int flags = 0;
3628
3629 if (nfs4_access & NFS4_SHARE_ACCESS_READ)
3630 flags |= NFSD_MAY_READ;
3631 if (nfs4_access & NFS4_SHARE_ACCESS_WRITE)
3632 flags |= NFSD_MAY_WRITE;
3633 return flags;
3634}
3635
7e6a72e5
CH
3636static inline __be32
3637nfsd4_truncate(struct svc_rqst *rqstp, struct svc_fh *fh,
3638 struct nfsd4_open *open)
3639{
3640 struct iattr iattr = {
3641 .ia_valid = ATTR_SIZE,
3642 .ia_size = 0,
3643 };
3644 if (!open->op_truncate)
3645 return 0;
3646 if (!(open->op_share_access & NFS4_SHARE_ACCESS_WRITE))
3647 return nfserr_inval;
3648 return nfsd_setattr(rqstp, fh, &iattr, 0, (time_t)0);
3649}
3650
0c12eaff 3651static __be32 nfs4_get_vfs_file(struct svc_rqst *rqstp, struct nfs4_file *fp,
6eb3a1d0
JL
3652 struct svc_fh *cur_fh, struct nfs4_ol_stateid *stp,
3653 struct nfsd4_open *open)
f9d7562f 3654{
de18643d 3655 struct file *filp = NULL;
f9d7562f 3656 __be32 status;
0c12eaff
CB
3657 int oflag = nfs4_access_to_omode(open->op_share_access);
3658 int access = nfs4_access_to_access(open->op_share_access);
baeb4ff0 3659 unsigned char old_access_bmap, old_deny_bmap;
0c12eaff 3660
de18643d 3661 spin_lock(&fp->fi_lock);
baeb4ff0
JL
3662
3663 /*
3664 * Are we trying to set a deny mode that would conflict with
3665 * current access?
3666 */
3667 status = nfs4_file_check_deny(fp, open->op_share_deny);
3668 if (status != nfs_ok) {
3669 spin_unlock(&fp->fi_lock);
3670 goto out;
3671 }
3672
3673 /* set access to the file */
3674 status = nfs4_file_get_access(fp, open->op_share_access);
3675 if (status != nfs_ok) {
3676 spin_unlock(&fp->fi_lock);
3677 goto out;
3678 }
3679
3680 /* Set access bits in stateid */
3681 old_access_bmap = stp->st_access_bmap;
3682 set_access(open->op_share_access, stp);
3683
3684 /* Set new deny mask */
3685 old_deny_bmap = stp->st_deny_bmap;
3686 set_deny(open->op_share_deny, stp);
3687 fp->fi_share_deny |= (open->op_share_deny & NFS4_SHARE_DENY_BOTH);
3688
f9d7562f 3689 if (!fp->fi_fds[oflag]) {
de18643d
TM
3690 spin_unlock(&fp->fi_lock);
3691 status = nfsd_open(rqstp, cur_fh, S_IFREG, access, &filp);
f9d7562f 3692 if (status)
baeb4ff0 3693 goto out_put_access;
de18643d
TM
3694 spin_lock(&fp->fi_lock);
3695 if (!fp->fi_fds[oflag]) {
3696 fp->fi_fds[oflag] = filp;
3697 filp = NULL;
3698 }
f9d7562f 3699 }
de18643d
TM
3700 spin_unlock(&fp->fi_lock);
3701 if (filp)
3702 fput(filp);
f9d7562f 3703
7e6a72e5
CH
3704 status = nfsd4_truncate(rqstp, cur_fh, open);
3705 if (status)
3706 goto out_put_access;
7e6a72e5
CH
3707out:
3708 return status;
baeb4ff0
JL
3709out_put_access:
3710 stp->st_access_bmap = old_access_bmap;
3711 nfs4_file_put_access(fp, open->op_share_access);
3712 reset_union_bmap_deny(bmap_to_share_mode(old_deny_bmap), stp);
3713 goto out;
1da177e4
LT
3714}
3715
b37ad28b 3716static __be32
dcef0413 3717nfs4_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 3718{
b37ad28b 3719 __be32 status;
baeb4ff0 3720 unsigned char old_deny_bmap;
1da177e4 3721
6eb3a1d0 3722 if (!test_access(open->op_share_access, stp))
baeb4ff0 3723 return nfs4_get_vfs_file(rqstp, fp, cur_fh, stp, open);
7e6a72e5 3724
baeb4ff0
JL
3725 /* test and set deny mode */
3726 spin_lock(&fp->fi_lock);
3727 status = nfs4_file_check_deny(fp, open->op_share_deny);
3728 if (status == nfs_ok) {
3729 old_deny_bmap = stp->st_deny_bmap;
3730 set_deny(open->op_share_deny, stp);
3731 fp->fi_share_deny |=
3732 (open->op_share_deny & NFS4_SHARE_DENY_BOTH);
3733 }
3734 spin_unlock(&fp->fi_lock);
3735
3736 if (status != nfs_ok)
1da177e4 3737 return status;
1da177e4 3738
baeb4ff0
JL
3739 status = nfsd4_truncate(rqstp, cur_fh, open);
3740 if (status != nfs_ok)
3741 reset_union_bmap_deny(old_deny_bmap, stp);
3742 return status;
3743}
1da177e4 3744
1da177e4 3745static void
1255a8f3 3746nfs4_set_claim_prev(struct nfsd4_open *open, bool has_session)
1da177e4 3747{
dad1c067 3748 open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED;
1da177e4
LT
3749}
3750
14a24e99
BF
3751/* Should we give out recallable state?: */
3752static bool nfsd4_cb_channel_good(struct nfs4_client *clp)
3753{
3754 if (clp->cl_cb_state == NFSD4_CB_UP)
3755 return true;
3756 /*
3757 * In the sessions case, since we don't have to establish a
3758 * separate connection for callbacks, we assume it's OK
3759 * until we hear otherwise:
3760 */
3761 return clp->cl_minorversion && clp->cl_cb_state == NFSD4_CB_UNKNOWN;
3762}
3763
d564fbec 3764static struct file_lock *nfs4_alloc_init_lease(struct nfs4_file *fp, int flag)
22d38c4c
BF
3765{
3766 struct file_lock *fl;
3767
3768 fl = locks_alloc_lock();
3769 if (!fl)
3770 return NULL;
22d38c4c 3771 fl->fl_lmops = &nfsd_lease_mng_ops;
617588d5 3772 fl->fl_flags = FL_DELEG;
22d38c4c
BF
3773 fl->fl_type = flag == NFS4_OPEN_DELEGATE_READ? F_RDLCK: F_WRLCK;
3774 fl->fl_end = OFFSET_MAX;
d564fbec 3775 fl->fl_owner = (fl_owner_t)fp;
22d38c4c 3776 fl->fl_pid = current->tgid;
22d38c4c
BF
3777 return fl;
3778}
3779
99c41515 3780static int nfs4_setlease(struct nfs4_delegation *dp)
edab9782 3781{
11b9164a 3782 struct nfs4_file *fp = dp->dl_stid.sc_file;
edab9782 3783 struct file_lock *fl;
417c6629
JL
3784 struct file *filp;
3785 int status = 0;
edab9782 3786
d564fbec 3787 fl = nfs4_alloc_init_lease(fp, NFS4_OPEN_DELEGATE_READ);
edab9782
BF
3788 if (!fl)
3789 return -ENOMEM;
417c6629
JL
3790 filp = find_readable_file(fp);
3791 if (!filp) {
3792 /* We should always have a readable file here */
3793 WARN_ON_ONCE(1);
3794 return -EBADF;
3795 }
3796 fl->fl_file = filp;
3797 status = vfs_setlease(filp, fl->fl_type, &fl);
3798 if (status) {
3799 locks_free_lock(fl);
3800 goto out_fput;
3801 }
3802 spin_lock(&state_lock);
3803 spin_lock(&fp->fi_lock);
3804 /* Did the lease get broken before we took the lock? */
3805 status = -EAGAIN;
3806 if (fp->fi_had_conflict)
3807 goto out_unlock;
3808 /* Race breaker */
3809 if (fp->fi_lease) {
3810 status = 0;
3811 atomic_inc(&fp->fi_delegees);
3812 hash_delegation_locked(dp, fp);
3813 goto out_unlock;
3814 }
acfdf5c3 3815 fp->fi_lease = fl;
417c6629 3816 fp->fi_deleg_file = filp;
acfdf5c3 3817 atomic_set(&fp->fi_delegees, 1);
931ee56c 3818 hash_delegation_locked(dp, fp);
417c6629 3819 spin_unlock(&fp->fi_lock);
cdc97505 3820 spin_unlock(&state_lock);
acfdf5c3 3821 return 0;
417c6629
JL
3822out_unlock:
3823 spin_unlock(&fp->fi_lock);
3824 spin_unlock(&state_lock);
3825out_fput:
3826 fput(filp);
e873088f 3827 return status;
acfdf5c3
BF
3828}
3829
0b26693c
JL
3830static struct nfs4_delegation *
3831nfs4_set_delegation(struct nfs4_client *clp, struct svc_fh *fh,
3832 struct nfs4_file *fp)
acfdf5c3 3833{
0b26693c
JL
3834 int status;
3835 struct nfs4_delegation *dp;
417c6629 3836
bf7bd3e9 3837 if (fp->fi_had_conflict)
0b26693c
JL
3838 return ERR_PTR(-EAGAIN);
3839
3840 dp = alloc_init_deleg(clp, fh);
3841 if (!dp)
3842 return ERR_PTR(-ENOMEM);
3843
bf7bd3e9 3844 get_nfs4_file(fp);
417c6629
JL
3845 spin_lock(&state_lock);
3846 spin_lock(&fp->fi_lock);
11b9164a 3847 dp->dl_stid.sc_file = fp;
417c6629
JL
3848 if (!fp->fi_lease) {
3849 spin_unlock(&fp->fi_lock);
3850 spin_unlock(&state_lock);
0b26693c
JL
3851 status = nfs4_setlease(dp);
3852 goto out;
417c6629 3853 }
cbf7a75b 3854 atomic_inc(&fp->fi_delegees);
acfdf5c3 3855 if (fp->fi_had_conflict) {
417c6629
JL
3856 status = -EAGAIN;
3857 goto out_unlock;
acfdf5c3 3858 }
931ee56c 3859 hash_delegation_locked(dp, fp);
0b26693c 3860 status = 0;
417c6629
JL
3861out_unlock:
3862 spin_unlock(&fp->fi_lock);
cdc97505 3863 spin_unlock(&state_lock);
0b26693c
JL
3864out:
3865 if (status) {
6011695d 3866 nfs4_put_stid(&dp->dl_stid);
0b26693c
JL
3867 return ERR_PTR(status);
3868 }
3869 return dp;
edab9782
BF
3870}
3871
4aa8913c
BH
3872static void nfsd4_open_deleg_none_ext(struct nfsd4_open *open, int status)
3873{
3874 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
3875 if (status == -EAGAIN)
3876 open->op_why_no_deleg = WND4_CONTENTION;
3877 else {
3878 open->op_why_no_deleg = WND4_RESOURCE;
3879 switch (open->op_deleg_want) {
3880 case NFS4_SHARE_WANT_READ_DELEG:
3881 case NFS4_SHARE_WANT_WRITE_DELEG:
3882 case NFS4_SHARE_WANT_ANY_DELEG:
3883 break;
3884 case NFS4_SHARE_WANT_CANCEL:
3885 open->op_why_no_deleg = WND4_CANCELLED;
3886 break;
3887 case NFS4_SHARE_WANT_NO_DELEG:
063b0fb9 3888 WARN_ON_ONCE(1);
4aa8913c
BH
3889 }
3890 }
3891}
3892
1da177e4
LT
3893/*
3894 * Attempt to hand out a delegation.
99c41515
BF
3895 *
3896 * Note we don't support write delegations, and won't until the vfs has
3897 * proper support for them.
1da177e4
LT
3898 */
3899static void
4cf59221
JL
3900nfs4_open_delegation(struct svc_fh *fh, struct nfsd4_open *open,
3901 struct nfs4_ol_stateid *stp)
1da177e4
LT
3902{
3903 struct nfs4_delegation *dp;
4cf59221
JL
3904 struct nfs4_openowner *oo = openowner(stp->st_stateowner);
3905 struct nfs4_client *clp = stp->st_stid.sc_client;
14a24e99 3906 int cb_up;
99c41515 3907 int status = 0;
1da177e4 3908
fe0750e5 3909 cb_up = nfsd4_cb_channel_good(oo->oo_owner.so_client);
7b190fec
N
3910 open->op_recall = 0;
3911 switch (open->op_claim_type) {
3912 case NFS4_OPEN_CLAIM_PREVIOUS:
2bf23875 3913 if (!cb_up)
7b190fec 3914 open->op_recall = 1;
99c41515
BF
3915 if (open->op_delegate_type != NFS4_OPEN_DELEGATE_READ)
3916 goto out_no_deleg;
7b190fec
N
3917 break;
3918 case NFS4_OPEN_CLAIM_NULL:
ed47b062 3919 case NFS4_OPEN_CLAIM_FH:
99c41515
BF
3920 /*
3921 * Let's not give out any delegations till everyone's
3922 * had the chance to reclaim theirs....
3923 */
4cf59221 3924 if (locks_in_grace(clp->net))
99c41515 3925 goto out_no_deleg;
dad1c067 3926 if (!cb_up || !(oo->oo_flags & NFS4_OO_CONFIRMED))
99c41515 3927 goto out_no_deleg;
9a0590ae
SD
3928 /*
3929 * Also, if the file was opened for write or
3930 * create, there's a good chance the client's
3931 * about to write to it, resulting in an
3932 * immediate recall (since we don't support
3933 * write delegations):
3934 */
7b190fec 3935 if (open->op_share_access & NFS4_SHARE_ACCESS_WRITE)
99c41515
BF
3936 goto out_no_deleg;
3937 if (open->op_create == NFS4_OPEN_CREATE)
3938 goto out_no_deleg;
7b190fec
N
3939 break;
3940 default:
99c41515 3941 goto out_no_deleg;
7b190fec 3942 }
11b9164a 3943 dp = nfs4_set_delegation(clp, fh, stp->st_stid.sc_file);
0b26693c 3944 if (IS_ERR(dp))
dd239cc0 3945 goto out_no_deleg;
1da177e4 3946
d5477a8d 3947 memcpy(&open->op_delegate_stateid, &dp->dl_stid.sc_stateid, sizeof(dp->dl_stid.sc_stateid));
1da177e4 3948
8c10cbdb 3949 dprintk("NFSD: delegation stateid=" STATEID_FMT "\n",
d5477a8d 3950 STATEID_VAL(&dp->dl_stid.sc_stateid));
99c41515 3951 open->op_delegate_type = NFS4_OPEN_DELEGATE_READ;
67cb1279 3952 nfs4_put_stid(&dp->dl_stid);
dd239cc0 3953 return;
dd239cc0 3954out_no_deleg:
99c41515
BF
3955 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE;
3956 if (open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS &&
d08d32e6 3957 open->op_delegate_type != NFS4_OPEN_DELEGATE_NONE) {
99c41515 3958 dprintk("NFSD: WARNING: refusing delegation reclaim\n");
d08d32e6
BF
3959 open->op_recall = 1;
3960 }
99c41515
BF
3961
3962 /* 4.1 client asking for a delegation? */
3963 if (open->op_deleg_want)
3964 nfsd4_open_deleg_none_ext(open, status);
3965 return;
1da177e4
LT
3966}
3967
e27f49c3
BH
3968static void nfsd4_deleg_xgrade_none_ext(struct nfsd4_open *open,
3969 struct nfs4_delegation *dp)
3970{
3971 if (open->op_deleg_want == NFS4_SHARE_WANT_READ_DELEG &&
3972 dp->dl_type == NFS4_OPEN_DELEGATE_WRITE) {
3973 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
3974 open->op_why_no_deleg = WND4_NOT_SUPP_DOWNGRADE;
3975 } else if (open->op_deleg_want == NFS4_SHARE_WANT_WRITE_DELEG &&
3976 dp->dl_type == NFS4_OPEN_DELEGATE_WRITE) {
3977 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
3978 open->op_why_no_deleg = WND4_NOT_SUPP_UPGRADE;
3979 }
3980 /* Otherwise the client must be confused wanting a delegation
3981 * it already has, therefore we don't return
3982 * NFS4_OPEN_DELEGATE_NONE_EXT and reason.
3983 */
3984}
3985
b37ad28b 3986__be32
1da177e4
LT
3987nfsd4_process_open2(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_open *open)
3988{
6668958f 3989 struct nfsd4_compoundres *resp = rqstp->rq_resp;
38c2f4b1 3990 struct nfs4_client *cl = open->op_openowner->oo_owner.so_client;
1da177e4 3991 struct nfs4_file *fp = NULL;
dcef0413 3992 struct nfs4_ol_stateid *stp = NULL;
567d9829 3993 struct nfs4_delegation *dp = NULL;
b37ad28b 3994 __be32 status;
1da177e4 3995
1da177e4
LT
3996 /*
3997 * Lookup file; if found, lookup stateid and check open request,
3998 * and check for delegations in the process of being recalled.
3999 * If not found, create the nfs4_file struct
4000 */
f9c00c3a 4001 fp = find_or_add_file(open->op_file, &current_fh->fh_handle);
950e0118 4002 if (fp != open->op_file) {
41d22663 4003 status = nfs4_check_deleg(cl, open, &dp);
c44c5eeb
N
4004 if (status)
4005 goto out;
a46cb7f2 4006 stp = nfsd4_find_existing_open(fp, open);
1da177e4 4007 } else {
950e0118 4008 open->op_file = NULL;
c44c5eeb 4009 status = nfserr_bad_stateid;
8b289b2c 4010 if (nfsd4_is_deleg_cur(open))
c44c5eeb 4011 goto out;
3e772463 4012 status = nfserr_jukebox;
1da177e4
LT
4013 }
4014
4015 /*
4016 * OPEN the file, or upgrade an existing OPEN.
4017 * If truncate fails, the OPEN fails.
4018 */
4019 if (stp) {
4020 /* Stateid was found, this is an OPEN upgrade */
f9d7562f 4021 status = nfs4_upgrade_open(rqstp, fp, current_fh, stp, open);
1da177e4
LT
4022 if (status)
4023 goto out;
4024 } else {
4cdc951b
BF
4025 stp = open->op_stp;
4026 open->op_stp = NULL;
996e0938 4027 init_open_stateid(stp, fp, open);
6eb3a1d0
JL
4028 status = nfs4_get_vfs_file(rqstp, fp, current_fh, stp, open);
4029 if (status) {
4030 release_open_stateid(stp);
4031 goto out;
4032 }
1da177e4 4033 }
dcef0413
BF
4034 update_stateid(&stp->st_stid.sc_stateid);
4035 memcpy(&open->op_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
1da177e4 4036
d24433cd 4037 if (nfsd4_has_session(&resp->cstate)) {
d24433cd
BH
4038 if (open->op_deleg_want & NFS4_SHARE_WANT_NO_DELEG) {
4039 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
4040 open->op_why_no_deleg = WND4_NOT_WANTED;
4041 goto nodeleg;
4042 }
4043 }
4044
1da177e4
LT
4045 /*
4046 * Attempt to hand out a delegation. No error return, because the
4047 * OPEN succeeds even if we fail.
4048 */
4cf59221 4049 nfs4_open_delegation(current_fh, open, stp);
d24433cd 4050nodeleg:
1da177e4
LT
4051 status = nfs_ok;
4052
8c10cbdb 4053 dprintk("%s: stateid=" STATEID_FMT "\n", __func__,
dcef0413 4054 STATEID_VAL(&stp->st_stid.sc_stateid));
1da177e4 4055out:
d24433cd
BH
4056 /* 4.1 client trying to upgrade/downgrade delegation? */
4057 if (open->op_delegate_type == NFS4_OPEN_DELEGATE_NONE && dp &&
e27f49c3
BH
4058 open->op_deleg_want)
4059 nfsd4_deleg_xgrade_none_ext(open, dp);
d24433cd 4060
13cd2184
N
4061 if (fp)
4062 put_nfs4_file(fp);
37515177 4063 if (status == 0 && open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS)
1255a8f3 4064 nfs4_set_claim_prev(open, nfsd4_has_session(&resp->cstate));
1da177e4
LT
4065 /*
4066 * To finish the open response, we just need to set the rflags.
4067 */
4068 open->op_rflags = NFS4_OPEN_RESULT_LOCKTYPE_POSIX;
dad1c067 4069 if (!(open->op_openowner->oo_flags & NFS4_OO_CONFIRMED) &&
6668958f 4070 !nfsd4_has_session(&resp->cstate))
1da177e4 4071 open->op_rflags |= NFS4_OPEN_RESULT_CONFIRM;
dcd94cc2
TM
4072 if (dp)
4073 nfs4_put_stid(&dp->dl_stid);
d6f2bc5d
TM
4074 if (stp)
4075 nfs4_put_stid(&stp->st_stid);
1da177e4
LT
4076
4077 return status;
4078}
4079
58fb12e6
JL
4080void nfsd4_cleanup_open_state(struct nfsd4_compound_state *cstate,
4081 struct nfsd4_open *open, __be32 status)
d29b20cd
BF
4082{
4083 if (open->op_openowner) {
d3134b10
JL
4084 struct nfs4_stateowner *so = &open->op_openowner->oo_owner;
4085
4086 nfsd4_cstate_assign_replay(cstate, so);
4087 nfs4_put_stateowner(so);
d29b20cd 4088 }
32513b40
BF
4089 if (open->op_file)
4090 nfsd4_free_file(open->op_file);
4cdc951b 4091 if (open->op_stp)
6011695d 4092 nfs4_put_stid(&open->op_stp->st_stid);
d29b20cd
BF
4093}
4094
b37ad28b 4095__be32
b591480b
BF
4096nfsd4_renew(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4097 clientid_t *clid)
1da177e4
LT
4098{
4099 struct nfs4_client *clp;
b37ad28b 4100 __be32 status;
7f2210fa 4101 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
1da177e4 4102
1da177e4
LT
4103 dprintk("process_renew(%08x/%08x): starting\n",
4104 clid->cl_boot, clid->cl_id);
4b24ca7d 4105 status = lookup_clientid(clid, cstate, nn);
9b2ef62b 4106 if (status)
1da177e4 4107 goto out;
4b24ca7d 4108 clp = cstate->clp;
1da177e4 4109 status = nfserr_cb_path_down;
ea1da636 4110 if (!list_empty(&clp->cl_delegations)
77a3569d 4111 && clp->cl_cb_state != NFSD4_CB_UP)
1da177e4
LT
4112 goto out;
4113 status = nfs_ok;
4114out:
1da177e4
LT
4115 return status;
4116}
4117
7f5ef2e9 4118void
12760c66 4119nfsd4_end_grace(struct nfsd_net *nn)
a76b4319 4120{
33dcc481 4121 /* do nothing if grace period already ended */
a51c84ed 4122 if (nn->grace_ended)
33dcc481
JL
4123 return;
4124
a76b4319 4125 dprintk("NFSD: end of grace period\n");
a51c84ed 4126 nn->grace_ended = true;
70b28235
BF
4127 /*
4128 * If the server goes down again right now, an NFSv4
4129 * client will still be allowed to reclaim after it comes back up,
4130 * even if it hasn't yet had a chance to reclaim state this time.
4131 *
4132 */
919b8049 4133 nfsd4_record_grace_done(nn);
70b28235
BF
4134 /*
4135 * At this point, NFSv4 clients can still reclaim. But if the
4136 * server crashes, any that have not yet reclaimed will be out
4137 * of luck on the next boot.
4138 *
4139 * (NFSv4.1+ clients are considered to have reclaimed once they
4140 * call RECLAIM_COMPLETE. NFSv4.0 clients are considered to
4141 * have reclaimed after their first OPEN.)
4142 */
5e1533c7 4143 locks_end_grace(&nn->nfsd4_manager);
70b28235
BF
4144 /*
4145 * At this point, and once lockd and/or any other containers
4146 * exit their grace period, further reclaims will fail and
4147 * regular locking can resume.
4148 */
a76b4319
N
4149}
4150
fd39ca9a 4151static time_t
09121281 4152nfs4_laundromat(struct nfsd_net *nn)
1da177e4
LT
4153{
4154 struct nfs4_client *clp;
fe0750e5 4155 struct nfs4_openowner *oo;
1da177e4 4156 struct nfs4_delegation *dp;
217526e7 4157 struct nfs4_ol_stateid *stp;
1da177e4 4158 struct list_head *pos, *next, reaplist;
3d733711 4159 time_t cutoff = get_seconds() - nn->nfsd4_lease;
a832e7ae 4160 time_t t, new_timeo = nn->nfsd4_lease;
1da177e4 4161
1da177e4 4162 dprintk("NFSD: laundromat service - starting\n");
12760c66 4163 nfsd4_end_grace(nn);
36acb66b 4164 INIT_LIST_HEAD(&reaplist);
c9a49628 4165 spin_lock(&nn->client_lock);
5ed58bb2 4166 list_for_each_safe(pos, next, &nn->client_lru) {
1da177e4
LT
4167 clp = list_entry(pos, struct nfs4_client, cl_lru);
4168 if (time_after((unsigned long)clp->cl_time, (unsigned long)cutoff)) {
4169 t = clp->cl_time - cutoff;
a832e7ae 4170 new_timeo = min(new_timeo, t);
1da177e4
LT
4171 break;
4172 }
221a6876 4173 if (mark_client_expired_locked(clp)) {
d7682988
BH
4174 dprintk("NFSD: client in use (clientid %08x)\n",
4175 clp->cl_clientid.cl_id);
4176 continue;
4177 }
4864af97 4178 list_add(&clp->cl_lru, &reaplist);
36acb66b 4179 }
c9a49628 4180 spin_unlock(&nn->client_lock);
36acb66b
BH
4181 list_for_each_safe(pos, next, &reaplist) {
4182 clp = list_entry(pos, struct nfs4_client, cl_lru);
1da177e4
LT
4183 dprintk("NFSD: purging unused client (clientid %08x)\n",
4184 clp->cl_clientid.cl_id);
4864af97 4185 list_del_init(&clp->cl_lru);
1da177e4
LT
4186 expire_client(clp);
4187 }
cdc97505 4188 spin_lock(&state_lock);
e8c69d17 4189 list_for_each_safe(pos, next, &nn->del_recall_lru) {
1da177e4 4190 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
4e37a7c2
SK
4191 if (net_generic(dp->dl_stid.sc_client->net, nfsd_net_id) != nn)
4192 continue;
1da177e4 4193 if (time_after((unsigned long)dp->dl_time, (unsigned long)cutoff)) {
a832e7ae
JL
4194 t = dp->dl_time - cutoff;
4195 new_timeo = min(new_timeo, t);
1da177e4
LT
4196 break;
4197 }
42690676
JL
4198 unhash_delegation_locked(dp);
4199 list_add(&dp->dl_recall_lru, &reaplist);
1da177e4 4200 }
cdc97505 4201 spin_unlock(&state_lock);
2d4a532d
JL
4202 while (!list_empty(&reaplist)) {
4203 dp = list_first_entry(&reaplist, struct nfs4_delegation,
4204 dl_recall_lru);
4205 list_del_init(&dp->dl_recall_lru);
3bd64a5b 4206 revoke_delegation(dp);
1da177e4 4207 }
217526e7
JL
4208
4209 spin_lock(&nn->client_lock);
4210 while (!list_empty(&nn->close_lru)) {
4211 oo = list_first_entry(&nn->close_lru, struct nfs4_openowner,
4212 oo_close_lru);
4213 if (time_after((unsigned long)oo->oo_time,
4214 (unsigned long)cutoff)) {
a832e7ae
JL
4215 t = oo->oo_time - cutoff;
4216 new_timeo = min(new_timeo, t);
1da177e4
LT
4217 break;
4218 }
217526e7
JL
4219 list_del_init(&oo->oo_close_lru);
4220 stp = oo->oo_last_closed_stid;
4221 oo->oo_last_closed_stid = NULL;
4222 spin_unlock(&nn->client_lock);
4223 nfs4_put_stid(&stp->st_stid);
4224 spin_lock(&nn->client_lock);
1da177e4 4225 }
217526e7
JL
4226 spin_unlock(&nn->client_lock);
4227
a832e7ae 4228 new_timeo = max_t(time_t, new_timeo, NFSD_LAUNDROMAT_MINTIMEOUT);
a832e7ae 4229 return new_timeo;
1da177e4
LT
4230}
4231
a254b246
HH
4232static struct workqueue_struct *laundry_wq;
4233static void laundromat_main(struct work_struct *);
a254b246
HH
4234
4235static void
09121281 4236laundromat_main(struct work_struct *laundry)
1da177e4
LT
4237{
4238 time_t t;
09121281
SK
4239 struct delayed_work *dwork = container_of(laundry, struct delayed_work,
4240 work);
4241 struct nfsd_net *nn = container_of(dwork, struct nfsd_net,
4242 laundromat_work);
1da177e4 4243
09121281 4244 t = nfs4_laundromat(nn);
1da177e4 4245 dprintk("NFSD: laundromat_main - sleeping for %ld seconds\n", t);
09121281 4246 queue_delayed_work(laundry_wq, &nn->laundromat_work, t*HZ);
1da177e4
LT
4247}
4248
f7a4d872 4249static inline __be32 nfs4_check_fh(struct svc_fh *fhp, struct nfs4_ol_stateid *stp)
1da177e4 4250{
11b9164a 4251 if (!nfsd_fh_match(&fhp->fh_handle, &stp->st_stid.sc_file->fi_fhandle))
f7a4d872
BF
4252 return nfserr_bad_stateid;
4253 return nfs_ok;
1da177e4
LT
4254}
4255
1da177e4 4256static inline int
82c5ff1b 4257access_permit_read(struct nfs4_ol_stateid *stp)
1da177e4 4258{
82c5ff1b
JL
4259 return test_access(NFS4_SHARE_ACCESS_READ, stp) ||
4260 test_access(NFS4_SHARE_ACCESS_BOTH, stp) ||
4261 test_access(NFS4_SHARE_ACCESS_WRITE, stp);
1da177e4
LT
4262}
4263
4264static inline int
82c5ff1b 4265access_permit_write(struct nfs4_ol_stateid *stp)
1da177e4 4266{
82c5ff1b
JL
4267 return test_access(NFS4_SHARE_ACCESS_WRITE, stp) ||
4268 test_access(NFS4_SHARE_ACCESS_BOTH, stp);
1da177e4
LT
4269}
4270
4271static
dcef0413 4272__be32 nfs4_check_openmode(struct nfs4_ol_stateid *stp, int flags)
1da177e4 4273{
b37ad28b 4274 __be32 status = nfserr_openmode;
1da177e4 4275
02921914
BF
4276 /* For lock stateid's, we test the parent open, not the lock: */
4277 if (stp->st_openstp)
4278 stp = stp->st_openstp;
82c5ff1b 4279 if ((flags & WR_STATE) && !access_permit_write(stp))
1da177e4 4280 goto out;
82c5ff1b 4281 if ((flags & RD_STATE) && !access_permit_read(stp))
1da177e4
LT
4282 goto out;
4283 status = nfs_ok;
4284out:
4285 return status;
4286}
4287
b37ad28b 4288static inline __be32
5ccb0066 4289check_special_stateids(struct net *net, svc_fh *current_fh, stateid_t *stateid, int flags)
1da177e4 4290{
203a8c8e 4291 if (ONE_STATEID(stateid) && (flags & RD_STATE))
1da177e4 4292 return nfs_ok;
5ccb0066 4293 else if (locks_in_grace(net)) {
25985edc 4294 /* Answer in remaining cases depends on existence of
1da177e4
LT
4295 * conflicting state; so we must wait out the grace period. */
4296 return nfserr_grace;
4297 } else if (flags & WR_STATE)
4298 return nfs4_share_conflict(current_fh,
4299 NFS4_SHARE_DENY_WRITE);
4300 else /* (flags & RD_STATE) && ZERO_STATEID(stateid) */
4301 return nfs4_share_conflict(current_fh,
4302 NFS4_SHARE_DENY_READ);
4303}
4304
4305/*
4306 * Allow READ/WRITE during grace period on recovered state only for files
4307 * that are not able to provide mandatory locking.
4308 */
4309static inline int
5ccb0066 4310grace_disallows_io(struct net *net, struct inode *inode)
1da177e4 4311{
5ccb0066 4312 return locks_in_grace(net) && mandatory_lock(inode);
1da177e4
LT
4313}
4314
81b82965
BF
4315/* Returns true iff a is later than b: */
4316static bool stateid_generation_after(stateid_t *a, stateid_t *b)
4317{
1a9357f4 4318 return (s32)(a->si_generation - b->si_generation) > 0;
81b82965
BF
4319}
4320
57b7b43b 4321static __be32 check_stateid_generation(stateid_t *in, stateid_t *ref, bool has_session)
0836f587 4322{
6668958f
AA
4323 /*
4324 * When sessions are used the stateid generation number is ignored
4325 * when it is zero.
4326 */
28dde241 4327 if (has_session && in->si_generation == 0)
81b82965
BF
4328 return nfs_ok;
4329
4330 if (in->si_generation == ref->si_generation)
4331 return nfs_ok;
6668958f 4332
0836f587 4333 /* If the client sends us a stateid from the future, it's buggy: */
81b82965 4334 if (stateid_generation_after(in, ref))
0836f587
BF
4335 return nfserr_bad_stateid;
4336 /*
81b82965
BF
4337 * However, we could see a stateid from the past, even from a
4338 * non-buggy client. For example, if the client sends a lock
4339 * while some IO is outstanding, the lock may bump si_generation
4340 * while the IO is still in flight. The client could avoid that
4341 * situation by waiting for responses on all the IO requests,
4342 * but better performance may result in retrying IO that
4343 * receives an old_stateid error if requests are rarely
4344 * reordered in flight:
0836f587 4345 */
81b82965 4346 return nfserr_old_stateid;
0836f587
BF
4347}
4348
7df302f7 4349static __be32 nfsd4_validate_stateid(struct nfs4_client *cl, stateid_t *stateid)
17456804 4350{
97b7e3b6
BF
4351 struct nfs4_stid *s;
4352 struct nfs4_ol_stateid *ols;
1af71cc8 4353 __be32 status = nfserr_bad_stateid;
17456804 4354
7df302f7 4355 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid))
1af71cc8 4356 return status;
7df302f7
CL
4357 /* Client debugging aid. */
4358 if (!same_clid(&stateid->si_opaque.so_clid, &cl->cl_clientid)) {
4359 char addr_str[INET6_ADDRSTRLEN];
4360 rpc_ntop((struct sockaddr *)&cl->cl_addr, addr_str,
4361 sizeof(addr_str));
4362 pr_warn_ratelimited("NFSD: client %s testing state ID "
4363 "with incorrect client ID\n", addr_str);
1af71cc8 4364 return status;
7df302f7 4365 }
1af71cc8
JL
4366 spin_lock(&cl->cl_lock);
4367 s = find_stateid_locked(cl, stateid);
97b7e3b6 4368 if (!s)
1af71cc8 4369 goto out_unlock;
36279ac1 4370 status = check_stateid_generation(stateid, &s->sc_stateid, 1);
17456804 4371 if (status)
1af71cc8 4372 goto out_unlock;
23340032
BF
4373 switch (s->sc_type) {
4374 case NFS4_DELEG_STID:
1af71cc8
JL
4375 status = nfs_ok;
4376 break;
3bd64a5b 4377 case NFS4_REVOKED_DELEG_STID:
1af71cc8
JL
4378 status = nfserr_deleg_revoked;
4379 break;
23340032
BF
4380 case NFS4_OPEN_STID:
4381 case NFS4_LOCK_STID:
4382 ols = openlockstateid(s);
4383 if (ols->st_stateowner->so_is_open_owner
4384 && !(openowner(ols->st_stateowner)->oo_flags
4385 & NFS4_OO_CONFIRMED))
1af71cc8
JL
4386 status = nfserr_bad_stateid;
4387 else
4388 status = nfs_ok;
4389 break;
23340032
BF
4390 default:
4391 printk("unknown stateid type %x\n", s->sc_type);
b0fc29d6 4392 /* Fallthrough */
23340032 4393 case NFS4_CLOSED_STID:
b0fc29d6 4394 case NFS4_CLOSED_DELEG_STID:
1af71cc8 4395 status = nfserr_bad_stateid;
23340032 4396 }
1af71cc8
JL
4397out_unlock:
4398 spin_unlock(&cl->cl_lock);
4399 return status;
17456804
BS
4400}
4401
2dd6e458
TM
4402static __be32
4403nfsd4_lookup_stateid(struct nfsd4_compound_state *cstate,
4404 stateid_t *stateid, unsigned char typemask,
4405 struct nfs4_stid **s, struct nfsd_net *nn)
38c2f4b1 4406{
0eb6f20a 4407 __be32 status;
38c2f4b1
BF
4408
4409 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid))
4410 return nfserr_bad_stateid;
4b24ca7d 4411 status = lookup_clientid(&stateid->si_opaque.so_clid, cstate, nn);
a8a7c677 4412 if (status == nfserr_stale_clientid) {
4b24ca7d 4413 if (cstate->session)
a8a7c677 4414 return nfserr_bad_stateid;
38c2f4b1 4415 return nfserr_stale_stateid;
a8a7c677 4416 }
0eb6f20a
BF
4417 if (status)
4418 return status;
4b24ca7d 4419 *s = find_stateid_by_type(cstate->clp, stateid, typemask);
38c2f4b1
BF
4420 if (!*s)
4421 return nfserr_bad_stateid;
4422 return nfs_ok;
38c2f4b1
BF
4423}
4424
1da177e4
LT
4425/*
4426* Checks for stateid operations
4427*/
b37ad28b 4428__be32
5ccb0066 4429nfs4_preprocess_stateid_op(struct net *net, struct nfsd4_compound_state *cstate,
dd453dfd 4430 stateid_t *stateid, int flags, struct file **filpp)
1da177e4 4431{
69064a27 4432 struct nfs4_stid *s;
dcef0413 4433 struct nfs4_ol_stateid *stp = NULL;
1da177e4 4434 struct nfs4_delegation *dp = NULL;
dd453dfd 4435 struct svc_fh *current_fh = &cstate->current_fh;
1da177e4 4436 struct inode *ino = current_fh->fh_dentry->d_inode;
3320fef1 4437 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
14bcab1a 4438 struct file *file = NULL;
b37ad28b 4439 __be32 status;
1da177e4 4440
1da177e4
LT
4441 if (filpp)
4442 *filpp = NULL;
4443
5ccb0066 4444 if (grace_disallows_io(net, ino))
1da177e4
LT
4445 return nfserr_grace;
4446
4447 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid))
5ccb0066 4448 return check_special_stateids(net, current_fh, stateid, flags);
1da177e4 4449
2dd6e458 4450 status = nfsd4_lookup_stateid(cstate, stateid,
db24b3b4 4451 NFS4_DELEG_STID|NFS4_OPEN_STID|NFS4_LOCK_STID,
2dd6e458 4452 &s, nn);
38c2f4b1 4453 if (status)
c2d1d6a8 4454 return status;
69064a27
BF
4455 status = check_stateid_generation(stateid, &s->sc_stateid, nfsd4_has_session(cstate));
4456 if (status)
4457 goto out;
f7a4d872
BF
4458 switch (s->sc_type) {
4459 case NFS4_DELEG_STID:
69064a27 4460 dp = delegstateid(s);
dc9bf700
BF
4461 status = nfs4_check_delegmode(dp, flags);
4462 if (status)
4463 goto out;
43b0178e 4464 if (filpp) {
11b9164a 4465 file = dp->dl_stid.sc_file->fi_deleg_file;
14bcab1a 4466 if (!file) {
063b0fb9
BF
4467 WARN_ON_ONCE(1);
4468 status = nfserr_serverfault;
4469 goto out;
4470 }
de18643d 4471 get_file(file);
43b0178e 4472 }
f7a4d872
BF
4473 break;
4474 case NFS4_OPEN_STID:
4475 case NFS4_LOCK_STID:
69064a27 4476 stp = openlockstateid(s);
f7a4d872
BF
4477 status = nfs4_check_fh(current_fh, stp);
4478 if (status)
1da177e4 4479 goto out;
fe0750e5 4480 if (stp->st_stateowner->so_is_open_owner
dad1c067 4481 && !(openowner(stp->st_stateowner)->oo_flags & NFS4_OO_CONFIRMED))
1da177e4 4482 goto out;
a4455be0
BF
4483 status = nfs4_check_openmode(stp, flags);
4484 if (status)
1da177e4 4485 goto out;
f9d7562f 4486 if (filpp) {
11b9164a
TM
4487 struct nfs4_file *fp = stp->st_stid.sc_file;
4488
f9d7562f 4489 if (flags & RD_STATE)
11b9164a 4490 file = find_readable_file(fp);
f9d7562f 4491 else
11b9164a 4492 file = find_writeable_file(fp);
f9d7562f 4493 }
f7a4d872
BF
4494 break;
4495 default:
14bcab1a
TM
4496 status = nfserr_bad_stateid;
4497 goto out;
1da177e4
LT
4498 }
4499 status = nfs_ok;
14bcab1a 4500 if (file)
de18643d 4501 *filpp = file;
1da177e4 4502out:
fd911011 4503 nfs4_put_stid(s);
1da177e4
LT
4504 return status;
4505}
4506
17456804
BS
4507/*
4508 * Test if the stateid is valid
4509 */
4510__be32
4511nfsd4_test_stateid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4512 struct nfsd4_test_stateid *test_stateid)
4513{
03cfb420
BS
4514 struct nfsd4_test_stateid_id *stateid;
4515 struct nfs4_client *cl = cstate->session->se_client;
4516
03cfb420 4517 list_for_each_entry(stateid, &test_stateid->ts_stateid_list, ts_id_list)
7df302f7
CL
4518 stateid->ts_id_status =
4519 nfsd4_validate_stateid(cl, &stateid->ts_id_stateid);
03cfb420 4520
17456804
BS
4521 return nfs_ok;
4522}
4523
e1ca12df
BS
4524__be32
4525nfsd4_free_stateid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4526 struct nfsd4_free_stateid *free_stateid)
4527{
4528 stateid_t *stateid = &free_stateid->fr_stateid;
2da1cec7 4529 struct nfs4_stid *s;
3bd64a5b 4530 struct nfs4_delegation *dp;
fc5a96c3 4531 struct nfs4_ol_stateid *stp;
38c2f4b1 4532 struct nfs4_client *cl = cstate->session->se_client;
2da1cec7 4533 __be32 ret = nfserr_bad_stateid;
e1ca12df 4534
1af71cc8
JL
4535 spin_lock(&cl->cl_lock);
4536 s = find_stateid_locked(cl, stateid);
2da1cec7 4537 if (!s)
1af71cc8 4538 goto out_unlock;
2da1cec7
BF
4539 switch (s->sc_type) {
4540 case NFS4_DELEG_STID:
e1ca12df 4541 ret = nfserr_locks_held;
1af71cc8 4542 break;
2da1cec7 4543 case NFS4_OPEN_STID:
2da1cec7
BF
4544 ret = check_stateid_generation(stateid, &s->sc_stateid, 1);
4545 if (ret)
1af71cc8
JL
4546 break;
4547 ret = nfserr_locks_held;
f7a4d872 4548 break;
1af71cc8
JL
4549 case NFS4_LOCK_STID:
4550 ret = check_stateid_generation(stateid, &s->sc_stateid, 1);
4551 if (ret)
4552 break;
fc5a96c3
JL
4553 stp = openlockstateid(s);
4554 ret = nfserr_locks_held;
4555 if (check_for_locks(stp->st_stid.sc_file,
4556 lockowner(stp->st_stateowner)))
4557 break;
4558 unhash_lock_stateid(stp);
1af71cc8 4559 spin_unlock(&cl->cl_lock);
fc5a96c3
JL
4560 nfs4_put_stid(s);
4561 ret = nfs_ok;
1af71cc8 4562 goto out;
3bd64a5b
BF
4563 case NFS4_REVOKED_DELEG_STID:
4564 dp = delegstateid(s);
2d4a532d
JL
4565 list_del_init(&dp->dl_recall_lru);
4566 spin_unlock(&cl->cl_lock);
6011695d 4567 nfs4_put_stid(s);
3bd64a5b 4568 ret = nfs_ok;
1af71cc8
JL
4569 goto out;
4570 /* Default falls through and returns nfserr_bad_stateid */
e1ca12df 4571 }
1af71cc8
JL
4572out_unlock:
4573 spin_unlock(&cl->cl_lock);
e1ca12df 4574out:
e1ca12df
BS
4575 return ret;
4576}
4577
4c4cd222
N
4578static inline int
4579setlkflg (int type)
4580{
4581 return (type == NFS4_READW_LT || type == NFS4_READ_LT) ?
4582 RD_STATE : WR_STATE;
4583}
1da177e4 4584
dcef0413 4585static __be32 nfs4_seqid_op_checks(struct nfsd4_compound_state *cstate, stateid_t *stateid, u32 seqid, struct nfs4_ol_stateid *stp)
c0a5d93e
BF
4586{
4587 struct svc_fh *current_fh = &cstate->current_fh;
4588 struct nfs4_stateowner *sop = stp->st_stateowner;
4589 __be32 status;
4590
c0a5d93e
BF
4591 status = nfsd4_check_seqid(cstate, sop, seqid);
4592 if (status)
4593 return status;
3bd64a5b
BF
4594 if (stp->st_stid.sc_type == NFS4_CLOSED_STID
4595 || stp->st_stid.sc_type == NFS4_REVOKED_DELEG_STID)
f7a4d872
BF
4596 /*
4597 * "Closed" stateid's exist *only* to return
3bd64a5b
BF
4598 * nfserr_replay_me from the previous step, and
4599 * revoked delegations are kept only for free_stateid.
f7a4d872
BF
4600 */
4601 return nfserr_bad_stateid;
4602 status = check_stateid_generation(stateid, &stp->st_stid.sc_stateid, nfsd4_has_session(cstate));
4603 if (status)
4604 return status;
4605 return nfs4_check_fh(current_fh, stp);
c0a5d93e
BF
4606}
4607
1da177e4
LT
4608/*
4609 * Checks for sequence id mutating operations.
4610 */
b37ad28b 4611static __be32
dd453dfd 4612nfs4_preprocess_seqid_op(struct nfsd4_compound_state *cstate, u32 seqid,
2288d0e3 4613 stateid_t *stateid, char typemask,
3320fef1
SK
4614 struct nfs4_ol_stateid **stpp,
4615 struct nfsd_net *nn)
1da177e4 4616{
0836f587 4617 __be32 status;
38c2f4b1 4618 struct nfs4_stid *s;
e17f99b7 4619 struct nfs4_ol_stateid *stp = NULL;
1da177e4 4620
8c10cbdb
BH
4621 dprintk("NFSD: %s: seqid=%d stateid = " STATEID_FMT "\n", __func__,
4622 seqid, STATEID_VAL(stateid));
3a4f98bb 4623
1da177e4 4624 *stpp = NULL;
2dd6e458 4625 status = nfsd4_lookup_stateid(cstate, stateid, typemask, &s, nn);
c0a5d93e
BF
4626 if (status)
4627 return status;
e17f99b7 4628 stp = openlockstateid(s);
58fb12e6 4629 nfsd4_cstate_assign_replay(cstate, stp->st_stateowner);
1da177e4 4630
e17f99b7 4631 status = nfs4_seqid_op_checks(cstate, stateid, seqid, stp);
fd911011 4632 if (!status)
e17f99b7 4633 *stpp = stp;
fd911011
TM
4634 else
4635 nfs4_put_stid(&stp->st_stid);
e17f99b7 4636 return status;
c0a5d93e 4637}
39325bd0 4638
3320fef1
SK
4639static __be32 nfs4_preprocess_confirmed_seqid_op(struct nfsd4_compound_state *cstate, u32 seqid,
4640 stateid_t *stateid, struct nfs4_ol_stateid **stpp, struct nfsd_net *nn)
c0a5d93e
BF
4641{
4642 __be32 status;
4643 struct nfs4_openowner *oo;
4cbfc9f7 4644 struct nfs4_ol_stateid *stp;
1da177e4 4645
c0a5d93e 4646 status = nfs4_preprocess_seqid_op(cstate, seqid, stateid,
4cbfc9f7 4647 NFS4_OPEN_STID, &stp, nn);
7a8711c9
BF
4648 if (status)
4649 return status;
4cbfc9f7
TM
4650 oo = openowner(stp->st_stateowner);
4651 if (!(oo->oo_flags & NFS4_OO_CONFIRMED)) {
4652 nfs4_put_stid(&stp->st_stid);
3a4f98bb 4653 return nfserr_bad_stateid;
4cbfc9f7
TM
4654 }
4655 *stpp = stp;
3a4f98bb 4656 return nfs_ok;
1da177e4
LT
4657}
4658
b37ad28b 4659__be32
ca364317 4660nfsd4_open_confirm(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
a4f1706a 4661 struct nfsd4_open_confirm *oc)
1da177e4 4662{
b37ad28b 4663 __be32 status;
fe0750e5 4664 struct nfs4_openowner *oo;
dcef0413 4665 struct nfs4_ol_stateid *stp;
3320fef1 4666 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
1da177e4 4667
a6a9f18f
AV
4668 dprintk("NFSD: nfsd4_open_confirm on file %pd\n",
4669 cstate->current_fh.fh_dentry);
1da177e4 4670
ca364317 4671 status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0);
a8cddc5d
BF
4672 if (status)
4673 return status;
1da177e4 4674
9072d5c6 4675 status = nfs4_preprocess_seqid_op(cstate,
ca364317 4676 oc->oc_seqid, &oc->oc_req_stateid,
3320fef1 4677 NFS4_OPEN_STID, &stp, nn);
9072d5c6 4678 if (status)
68b66e82 4679 goto out;
fe0750e5 4680 oo = openowner(stp->st_stateowner);
68b66e82 4681 status = nfserr_bad_stateid;
dad1c067 4682 if (oo->oo_flags & NFS4_OO_CONFIRMED)
2585fc79 4683 goto put_stateid;
dad1c067 4684 oo->oo_flags |= NFS4_OO_CONFIRMED;
dcef0413
BF
4685 update_stateid(&stp->st_stid.sc_stateid);
4686 memcpy(&oc->oc_resp_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
8c10cbdb 4687 dprintk("NFSD: %s: success, seqid=%d stateid=" STATEID_FMT "\n",
dcef0413 4688 __func__, oc->oc_seqid, STATEID_VAL(&stp->st_stid.sc_stateid));
c7b9a459 4689
2a4317c5 4690 nfsd4_client_record_create(oo->oo_owner.so_client);
68b66e82 4691 status = nfs_ok;
2585fc79
TM
4692put_stateid:
4693 nfs4_put_stid(&stp->st_stid);
1da177e4 4694out:
9411b1d4 4695 nfsd4_bump_seqid(cstate, status);
1da177e4
LT
4696 return status;
4697}
4698
6409a5a6 4699static inline void nfs4_stateid_downgrade_bit(struct nfs4_ol_stateid *stp, u32 access)
1da177e4 4700{
82c5ff1b 4701 if (!test_access(access, stp))
6409a5a6 4702 return;
11b9164a 4703 nfs4_file_put_access(stp->st_stid.sc_file, access);
82c5ff1b 4704 clear_access(access, stp);
6409a5a6 4705}
f197c271 4706
6409a5a6
BF
4707static inline void nfs4_stateid_downgrade(struct nfs4_ol_stateid *stp, u32 to_access)
4708{
4709 switch (to_access) {
4710 case NFS4_SHARE_ACCESS_READ:
4711 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_WRITE);
4712 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_BOTH);
4713 break;
4714 case NFS4_SHARE_ACCESS_WRITE:
4715 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_READ);
4716 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_BOTH);
4717 break;
4718 case NFS4_SHARE_ACCESS_BOTH:
4719 break;
4720 default:
063b0fb9 4721 WARN_ON_ONCE(1);
1da177e4
LT
4722 }
4723}
4724
b37ad28b 4725__be32
ca364317
BF
4726nfsd4_open_downgrade(struct svc_rqst *rqstp,
4727 struct nfsd4_compound_state *cstate,
a4f1706a 4728 struct nfsd4_open_downgrade *od)
1da177e4 4729{
b37ad28b 4730 __be32 status;
dcef0413 4731 struct nfs4_ol_stateid *stp;
3320fef1 4732 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
1da177e4 4733
a6a9f18f
AV
4734 dprintk("NFSD: nfsd4_open_downgrade on file %pd\n",
4735 cstate->current_fh.fh_dentry);
1da177e4 4736
c30e92df 4737 /* We don't yet support WANT bits: */
2c8bd7e0
BH
4738 if (od->od_deleg_want)
4739 dprintk("NFSD: %s: od_deleg_want=0x%x ignored\n", __func__,
4740 od->od_deleg_want);
1da177e4 4741
c0a5d93e 4742 status = nfs4_preprocess_confirmed_seqid_op(cstate, od->od_seqid,
3320fef1 4743 &od->od_stateid, &stp, nn);
9072d5c6 4744 if (status)
1da177e4 4745 goto out;
1da177e4 4746 status = nfserr_inval;
82c5ff1b 4747 if (!test_access(od->od_share_access, stp)) {
c11c591f 4748 dprintk("NFSD: access not a subset of current bitmap: 0x%hhx, input access=%08x\n",
1da177e4 4749 stp->st_access_bmap, od->od_share_access);
0667b1e9 4750 goto put_stateid;
1da177e4 4751 }
ce0fc43c 4752 if (!test_deny(od->od_share_deny, stp)) {
c11c591f 4753 dprintk("NFSD: deny not a subset of current bitmap: 0x%hhx, input deny=%08x\n",
1da177e4 4754 stp->st_deny_bmap, od->od_share_deny);
0667b1e9 4755 goto put_stateid;
1da177e4 4756 }
6409a5a6 4757 nfs4_stateid_downgrade(stp, od->od_share_access);
1da177e4 4758
ce0fc43c 4759 reset_union_bmap_deny(od->od_share_deny, stp);
1da177e4 4760
dcef0413
BF
4761 update_stateid(&stp->st_stid.sc_stateid);
4762 memcpy(&od->od_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
1da177e4 4763 status = nfs_ok;
0667b1e9
TM
4764put_stateid:
4765 nfs4_put_stid(&stp->st_stid);
1da177e4 4766out:
9411b1d4 4767 nfsd4_bump_seqid(cstate, status);
1da177e4
LT
4768 return status;
4769}
4770
f7a4d872
BF
4771static void nfsd4_close_open_stateid(struct nfs4_ol_stateid *s)
4772{
acf9295b 4773 struct nfs4_client *clp = s->st_stid.sc_client;
d83017f9 4774 LIST_HEAD(reaplist);
acf9295b 4775
f7a4d872 4776 s->st_stid.sc_type = NFS4_CLOSED_STID;
2c41beb0 4777 spin_lock(&clp->cl_lock);
d83017f9 4778 unhash_open_stateid(s, &reaplist);
acf9295b 4779
d83017f9
JL
4780 if (clp->cl_minorversion) {
4781 put_ol_stateid_locked(s, &reaplist);
4782 spin_unlock(&clp->cl_lock);
4783 free_ol_stateid_reaplist(&reaplist);
4784 } else {
4785 spin_unlock(&clp->cl_lock);
4786 free_ol_stateid_reaplist(&reaplist);
d3134b10 4787 move_to_close_lru(s, clp->net);
d83017f9 4788 }
38c387b5
BF
4789}
4790
1da177e4
LT
4791/*
4792 * nfs4_unlock_state() called after encode
4793 */
b37ad28b 4794__be32
ca364317 4795nfsd4_close(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
a4f1706a 4796 struct nfsd4_close *close)
1da177e4 4797{
b37ad28b 4798 __be32 status;
dcef0413 4799 struct nfs4_ol_stateid *stp;
3320fef1
SK
4800 struct net *net = SVC_NET(rqstp);
4801 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
1da177e4 4802
a6a9f18f
AV
4803 dprintk("NFSD: nfsd4_close on file %pd\n",
4804 cstate->current_fh.fh_dentry);
1da177e4 4805
f7a4d872
BF
4806 status = nfs4_preprocess_seqid_op(cstate, close->cl_seqid,
4807 &close->cl_stateid,
4808 NFS4_OPEN_STID|NFS4_CLOSED_STID,
3320fef1 4809 &stp, nn);
9411b1d4 4810 nfsd4_bump_seqid(cstate, status);
9072d5c6 4811 if (status)
1da177e4 4812 goto out;
dcef0413
BF
4813 update_stateid(&stp->st_stid.sc_stateid);
4814 memcpy(&close->cl_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
1da177e4 4815
f7a4d872 4816 nfsd4_close_open_stateid(stp);
8a0b589d
TM
4817
4818 /* put reference from nfs4_preprocess_seqid_op */
4819 nfs4_put_stid(&stp->st_stid);
1da177e4 4820out:
1da177e4
LT
4821 return status;
4822}
4823
b37ad28b 4824__be32
ca364317
BF
4825nfsd4_delegreturn(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4826 struct nfsd4_delegreturn *dr)
1da177e4 4827{
203a8c8e
BF
4828 struct nfs4_delegation *dp;
4829 stateid_t *stateid = &dr->dr_stateid;
38c2f4b1 4830 struct nfs4_stid *s;
b37ad28b 4831 __be32 status;
3320fef1 4832 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
1da177e4 4833
ca364317 4834 if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0)))
203a8c8e 4835 return status;
1da177e4 4836
2dd6e458 4837 status = nfsd4_lookup_stateid(cstate, stateid, NFS4_DELEG_STID, &s, nn);
38c2f4b1 4838 if (status)
203a8c8e 4839 goto out;
38c2f4b1 4840 dp = delegstateid(s);
d5477a8d 4841 status = check_stateid_generation(stateid, &dp->dl_stid.sc_stateid, nfsd4_has_session(cstate));
203a8c8e 4842 if (status)
fd911011 4843 goto put_stateid;
203a8c8e 4844
3bd64a5b 4845 destroy_delegation(dp);
fd911011
TM
4846put_stateid:
4847 nfs4_put_stid(&dp->dl_stid);
1da177e4
LT
4848out:
4849 return status;
4850}
4851
4852
1da177e4 4853#define LOFF_OVERFLOW(start, len) ((u64)(len) > ~(u64)(start))
1da177e4 4854
87df4de8
BH
4855static inline u64
4856end_offset(u64 start, u64 len)
4857{
4858 u64 end;
4859
4860 end = start + len;
4861 return end >= start ? end: NFS4_MAX_UINT64;
4862}
4863
4864/* last octet in a range */
4865static inline u64
4866last_byte_offset(u64 start, u64 len)
4867{
4868 u64 end;
4869
063b0fb9 4870 WARN_ON_ONCE(!len);
87df4de8
BH
4871 end = start + len;
4872 return end > start ? end - 1: NFS4_MAX_UINT64;
4873}
4874
1da177e4
LT
4875/*
4876 * TODO: Linux file offsets are _signed_ 64-bit quantities, which means that
4877 * we can't properly handle lock requests that go beyond the (2^63 - 1)-th
4878 * byte, because of sign extension problems. Since NFSv4 calls for 64-bit
4879 * locking, this prevents us from being completely protocol-compliant. The
4880 * real solution to this problem is to start using unsigned file offsets in
4881 * the VFS, but this is a very deep change!
4882 */
4883static inline void
4884nfs4_transform_lock_offset(struct file_lock *lock)
4885{
4886 if (lock->fl_start < 0)
4887 lock->fl_start = OFFSET_MAX;
4888 if (lock->fl_end < 0)
4889 lock->fl_end = OFFSET_MAX;
4890}
4891
d5b9026a
N
4892/* Hack!: For now, we're defining this just so we can use a pointer to it
4893 * as a unique cookie to identify our (NFSv4's) posix locks. */
7b021967 4894static const struct lock_manager_operations nfsd_posix_mng_ops = {
d5b9026a 4895};
1da177e4
LT
4896
4897static inline void
4898nfs4_set_lock_denied(struct file_lock *fl, struct nfsd4_lock_denied *deny)
4899{
fe0750e5 4900 struct nfs4_lockowner *lo;
1da177e4 4901
d5b9026a 4902 if (fl->fl_lmops == &nfsd_posix_mng_ops) {
fe0750e5
BF
4903 lo = (struct nfs4_lockowner *) fl->fl_owner;
4904 deny->ld_owner.data = kmemdup(lo->lo_owner.so_owner.data,
4905 lo->lo_owner.so_owner.len, GFP_KERNEL);
7c13f344
BF
4906 if (!deny->ld_owner.data)
4907 /* We just don't care that much */
4908 goto nevermind;
fe0750e5
BF
4909 deny->ld_owner.len = lo->lo_owner.so_owner.len;
4910 deny->ld_clientid = lo->lo_owner.so_client->cl_clientid;
d5b9026a 4911 } else {
7c13f344
BF
4912nevermind:
4913 deny->ld_owner.len = 0;
4914 deny->ld_owner.data = NULL;
d5b9026a
N
4915 deny->ld_clientid.cl_boot = 0;
4916 deny->ld_clientid.cl_id = 0;
1da177e4
LT
4917 }
4918 deny->ld_start = fl->fl_start;
87df4de8
BH
4919 deny->ld_length = NFS4_MAX_UINT64;
4920 if (fl->fl_end != NFS4_MAX_UINT64)
1da177e4
LT
4921 deny->ld_length = fl->fl_end - fl->fl_start + 1;
4922 deny->ld_type = NFS4_READ_LT;
4923 if (fl->fl_type != F_RDLCK)
4924 deny->ld_type = NFS4_WRITE_LT;
4925}
4926
fe0750e5 4927static struct nfs4_lockowner *
c58c6610 4928find_lockowner_str_locked(clientid_t *clid, struct xdr_netobj *owner,
d4f0489f 4929 struct nfs4_client *clp)
1da177e4 4930{
d4f0489f 4931 unsigned int strhashval = ownerstr_hashval(owner);
b3c32bcd 4932 struct nfs4_stateowner *so;
1da177e4 4933
0a880a28
TM
4934 lockdep_assert_held(&clp->cl_lock);
4935
d4f0489f
TM
4936 list_for_each_entry(so, &clp->cl_ownerstr_hashtbl[strhashval],
4937 so_strhash) {
b3c32bcd
TM
4938 if (so->so_is_open_owner)
4939 continue;
d4f0489f 4940 if (!same_owner_str(so, owner))
b3c32bcd 4941 continue;
5db1c03f 4942 atomic_inc(&so->so_count);
b3c32bcd 4943 return lockowner(so);
1da177e4
LT
4944 }
4945 return NULL;
4946}
4947
c58c6610
TM
4948static struct nfs4_lockowner *
4949find_lockowner_str(clientid_t *clid, struct xdr_netobj *owner,
d4f0489f 4950 struct nfs4_client *clp)
c58c6610
TM
4951{
4952 struct nfs4_lockowner *lo;
4953
d4f0489f
TM
4954 spin_lock(&clp->cl_lock);
4955 lo = find_lockowner_str_locked(clid, owner, clp);
4956 spin_unlock(&clp->cl_lock);
c58c6610
TM
4957 return lo;
4958}
4959
8f4b54c5
JL
4960static void nfs4_unhash_lockowner(struct nfs4_stateowner *sop)
4961{
c58c6610 4962 unhash_lockowner_locked(lockowner(sop));
8f4b54c5
JL
4963}
4964
6b180f0b
JL
4965static void nfs4_free_lockowner(struct nfs4_stateowner *sop)
4966{
4967 struct nfs4_lockowner *lo = lockowner(sop);
4968
4969 kmem_cache_free(lockowner_slab, lo);
4970}
4971
4972static const struct nfs4_stateowner_operations lockowner_ops = {
8f4b54c5
JL
4973 .so_unhash = nfs4_unhash_lockowner,
4974 .so_free = nfs4_free_lockowner,
6b180f0b
JL
4975};
4976
1da177e4
LT
4977/*
4978 * Alloc a lock owner structure.
4979 * Called in nfsd4_lock - therefore, OPEN and OPEN_CONFIRM (if needed) has
25985edc 4980 * occurred.
1da177e4 4981 *
16bfdaaf 4982 * strhashval = ownerstr_hashval
1da177e4 4983 */
fe0750e5 4984static struct nfs4_lockowner *
c58c6610
TM
4985alloc_init_lock_stateowner(unsigned int strhashval, struct nfs4_client *clp,
4986 struct nfs4_ol_stateid *open_stp,
4987 struct nfsd4_lock *lock)
4988{
c58c6610 4989 struct nfs4_lockowner *lo, *ret;
1da177e4 4990
fe0750e5
BF
4991 lo = alloc_stateowner(lockowner_slab, &lock->lk_new_owner, clp);
4992 if (!lo)
1da177e4 4993 return NULL;
fe0750e5
BF
4994 INIT_LIST_HEAD(&lo->lo_owner.so_stateids);
4995 lo->lo_owner.so_is_open_owner = 0;
5db1c03f 4996 lo->lo_owner.so_seqid = lock->lk_new_lock_seqid;
6b180f0b 4997 lo->lo_owner.so_ops = &lockowner_ops;
d4f0489f 4998 spin_lock(&clp->cl_lock);
c58c6610 4999 ret = find_lockowner_str_locked(&clp->cl_clientid,
d4f0489f 5000 &lock->lk_new_owner, clp);
c58c6610
TM
5001 if (ret == NULL) {
5002 list_add(&lo->lo_owner.so_strhash,
d4f0489f 5003 &clp->cl_ownerstr_hashtbl[strhashval]);
c58c6610
TM
5004 ret = lo;
5005 } else
5006 nfs4_free_lockowner(&lo->lo_owner);
d4f0489f 5007 spin_unlock(&clp->cl_lock);
fe0750e5 5008 return lo;
1da177e4
LT
5009}
5010
356a95ec
JL
5011static void
5012init_lock_stateid(struct nfs4_ol_stateid *stp, struct nfs4_lockowner *lo,
5013 struct nfs4_file *fp, struct inode *inode,
5014 struct nfs4_ol_stateid *open_stp)
1da177e4 5015{
d3b313a4 5016 struct nfs4_client *clp = lo->lo_owner.so_client;
1da177e4 5017
356a95ec
JL
5018 lockdep_assert_held(&clp->cl_lock);
5019
3d0fabd5 5020 atomic_inc(&stp->st_stid.sc_count);
3abdb607 5021 stp->st_stid.sc_type = NFS4_LOCK_STID;
fe0750e5 5022 stp->st_stateowner = &lo->lo_owner;
e4f1dd7f 5023 atomic_inc(&lo->lo_owner.so_count);
13cd2184 5024 get_nfs4_file(fp);
11b9164a 5025 stp->st_stid.sc_file = fp;
b49e084d 5026 stp->st_stid.sc_free = nfs4_free_lock_stateid;
0997b173 5027 stp->st_access_bmap = 0;
1da177e4 5028 stp->st_deny_bmap = open_stp->st_deny_bmap;
4c4cd222 5029 stp->st_openstp = open_stp;
3c87b9b7 5030 list_add(&stp->st_locks, &open_stp->st_locks);
1c755dc1 5031 list_add(&stp->st_perstateowner, &lo->lo_owner.so_stateids);
1d31a253
TM
5032 spin_lock(&fp->fi_lock);
5033 list_add(&stp->st_perfile, &fp->fi_stateids);
5034 spin_unlock(&fp->fi_lock);
1da177e4
LT
5035}
5036
c53530da
JL
5037static struct nfs4_ol_stateid *
5038find_lock_stateid(struct nfs4_lockowner *lo, struct nfs4_file *fp)
5039{
5040 struct nfs4_ol_stateid *lst;
356a95ec
JL
5041 struct nfs4_client *clp = lo->lo_owner.so_client;
5042
5043 lockdep_assert_held(&clp->cl_lock);
c53530da
JL
5044
5045 list_for_each_entry(lst, &lo->lo_owner.so_stateids, st_perstateowner) {
3d0fabd5
TM
5046 if (lst->st_stid.sc_file == fp) {
5047 atomic_inc(&lst->st_stid.sc_count);
c53530da 5048 return lst;
3d0fabd5 5049 }
c53530da
JL
5050 }
5051 return NULL;
5052}
5053
356a95ec
JL
5054static struct nfs4_ol_stateid *
5055find_or_create_lock_stateid(struct nfs4_lockowner *lo, struct nfs4_file *fi,
5056 struct inode *inode, struct nfs4_ol_stateid *ost,
5057 bool *new)
5058{
5059 struct nfs4_stid *ns = NULL;
5060 struct nfs4_ol_stateid *lst;
5061 struct nfs4_openowner *oo = openowner(ost->st_stateowner);
5062 struct nfs4_client *clp = oo->oo_owner.so_client;
5063
5064 spin_lock(&clp->cl_lock);
5065 lst = find_lock_stateid(lo, fi);
5066 if (lst == NULL) {
5067 spin_unlock(&clp->cl_lock);
5068 ns = nfs4_alloc_stid(clp, stateid_slab);
5069 if (ns == NULL)
5070 return NULL;
5071
5072 spin_lock(&clp->cl_lock);
5073 lst = find_lock_stateid(lo, fi);
5074 if (likely(!lst)) {
5075 lst = openlockstateid(ns);
5076 init_lock_stateid(lst, lo, fi, inode, ost);
5077 ns = NULL;
5078 *new = true;
5079 }
5080 }
5081 spin_unlock(&clp->cl_lock);
5082 if (ns)
5083 nfs4_put_stid(ns);
5084 return lst;
5085}
c53530da 5086
fd39ca9a 5087static int
1da177e4
LT
5088check_lock_length(u64 offset, u64 length)
5089{
87df4de8 5090 return ((length == 0) || ((length != NFS4_MAX_UINT64) &&
1da177e4
LT
5091 LOFF_OVERFLOW(offset, length)));
5092}
5093
dcef0413 5094static void get_lock_access(struct nfs4_ol_stateid *lock_stp, u32 access)
0997b173 5095{
11b9164a 5096 struct nfs4_file *fp = lock_stp->st_stid.sc_file;
0997b173 5097
7214e860
JL
5098 lockdep_assert_held(&fp->fi_lock);
5099
82c5ff1b 5100 if (test_access(access, lock_stp))
0997b173 5101 return;
12659651 5102 __nfs4_file_get_access(fp, access);
82c5ff1b 5103 set_access(access, lock_stp);
0997b173
BF
5104}
5105
356a95ec
JL
5106static __be32
5107lookup_or_create_lock_state(struct nfsd4_compound_state *cstate,
5108 struct nfs4_ol_stateid *ost,
5109 struct nfsd4_lock *lock,
5110 struct nfs4_ol_stateid **lst, bool *new)
64a284d0 5111{
5db1c03f 5112 __be32 status;
11b9164a 5113 struct nfs4_file *fi = ost->st_stid.sc_file;
64a284d0
BF
5114 struct nfs4_openowner *oo = openowner(ost->st_stateowner);
5115 struct nfs4_client *cl = oo->oo_owner.so_client;
f9c00c3a 5116 struct inode *inode = cstate->current_fh.fh_dentry->d_inode;
64a284d0
BF
5117 struct nfs4_lockowner *lo;
5118 unsigned int strhashval;
5119
d4f0489f 5120 lo = find_lockowner_str(&cl->cl_clientid, &lock->v.new.owner, cl);
c53530da 5121 if (!lo) {
d4f0489f 5122 strhashval = ownerstr_hashval(&lock->v.new.owner);
c53530da
JL
5123 lo = alloc_init_lock_stateowner(strhashval, cl, ost, lock);
5124 if (lo == NULL)
5125 return nfserr_jukebox;
5126 } else {
5127 /* with an existing lockowner, seqids must be the same */
5db1c03f 5128 status = nfserr_bad_seqid;
c53530da
JL
5129 if (!cstate->minorversion &&
5130 lock->lk_new_lock_seqid != lo->lo_owner.so_seqid)
5db1c03f 5131 goto out;
64a284d0 5132 }
c53530da 5133
356a95ec 5134 *lst = find_or_create_lock_stateid(lo, fi, inode, ost, new);
64a284d0 5135 if (*lst == NULL) {
5db1c03f
JL
5136 status = nfserr_jukebox;
5137 goto out;
64a284d0 5138 }
5db1c03f
JL
5139 status = nfs_ok;
5140out:
5141 nfs4_put_stateowner(&lo->lo_owner);
5142 return status;
64a284d0
BF
5143}
5144
1da177e4
LT
5145/*
5146 * LOCK operation
5147 */
b37ad28b 5148__be32
ca364317 5149nfsd4_lock(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
a4f1706a 5150 struct nfsd4_lock *lock)
1da177e4 5151{
fe0750e5
BF
5152 struct nfs4_openowner *open_sop = NULL;
5153 struct nfs4_lockowner *lock_sop = NULL;
3d0fabd5 5154 struct nfs4_ol_stateid *lock_stp = NULL;
0667b1e9 5155 struct nfs4_ol_stateid *open_stp = NULL;
7214e860 5156 struct nfs4_file *fp;
7d947842 5157 struct file *filp = NULL;
21179d81
JL
5158 struct file_lock *file_lock = NULL;
5159 struct file_lock *conflock = NULL;
b37ad28b 5160 __be32 status = 0;
b34f27aa 5161 int lkflg;
b8dd7b9a 5162 int err;
5db1c03f 5163 bool new = false;
3320fef1
SK
5164 struct net *net = SVC_NET(rqstp);
5165 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
1da177e4
LT
5166
5167 dprintk("NFSD: nfsd4_lock: start=%Ld length=%Ld\n",
5168 (long long) lock->lk_offset,
5169 (long long) lock->lk_length);
5170
1da177e4
LT
5171 if (check_lock_length(lock->lk_offset, lock->lk_length))
5172 return nfserr_inval;
5173
ca364317 5174 if ((status = fh_verify(rqstp, &cstate->current_fh,
8837abca 5175 S_IFREG, NFSD_MAY_LOCK))) {
a6f6ef2f
AA
5176 dprintk("NFSD: nfsd4_lock: permission denied!\n");
5177 return status;
5178 }
5179
1da177e4 5180 if (lock->lk_is_new) {
684e5638
BF
5181 if (nfsd4_has_session(cstate))
5182 /* See rfc 5661 18.10.3: given clientid is ignored: */
5183 memcpy(&lock->v.new.clientid,
5184 &cstate->session->se_client->cl_clientid,
5185 sizeof(clientid_t));
5186
1da177e4 5187 status = nfserr_stale_clientid;
2c142baa 5188 if (STALE_CLIENTID(&lock->lk_new_clientid, nn))
1da177e4 5189 goto out;
1da177e4 5190
1da177e4 5191 /* validate and update open stateid and open seqid */
c0a5d93e 5192 status = nfs4_preprocess_confirmed_seqid_op(cstate,
1da177e4
LT
5193 lock->lk_new_open_seqid,
5194 &lock->lk_new_open_stateid,
3320fef1 5195 &open_stp, nn);
37515177 5196 if (status)
1da177e4 5197 goto out;
fe0750e5 5198 open_sop = openowner(open_stp->st_stateowner);
b34f27aa 5199 status = nfserr_bad_stateid;
684e5638 5200 if (!same_clid(&open_sop->oo_owner.so_client->cl_clientid,
b34f27aa
BF
5201 &lock->v.new.clientid))
5202 goto out;
64a284d0 5203 status = lookup_or_create_lock_state(cstate, open_stp, lock,
5db1c03f 5204 &lock_stp, &new);
3d0fabd5 5205 } else {
dd453dfd 5206 status = nfs4_preprocess_seqid_op(cstate,
fe0750e5
BF
5207 lock->lk_old_lock_seqid,
5208 &lock->lk_old_lock_stateid,
3320fef1 5209 NFS4_LOCK_STID, &lock_stp, nn);
3d0fabd5 5210 }
e1aaa891
BF
5211 if (status)
5212 goto out;
64a284d0 5213 lock_sop = lockowner(lock_stp->st_stateowner);
1da177e4 5214
b34f27aa
BF
5215 lkflg = setlkflg(lock->lk_type);
5216 status = nfs4_check_openmode(lock_stp, lkflg);
5217 if (status)
5218 goto out;
5219
0dd395dc 5220 status = nfserr_grace;
3320fef1 5221 if (locks_in_grace(net) && !lock->lk_reclaim)
0dd395dc
N
5222 goto out;
5223 status = nfserr_no_grace;
3320fef1 5224 if (!locks_in_grace(net) && lock->lk_reclaim)
0dd395dc
N
5225 goto out;
5226
21179d81
JL
5227 file_lock = locks_alloc_lock();
5228 if (!file_lock) {
5229 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
5230 status = nfserr_jukebox;
5231 goto out;
5232 }
5233
11b9164a 5234 fp = lock_stp->st_stid.sc_file;
1da177e4
LT
5235 switch (lock->lk_type) {
5236 case NFS4_READ_LT:
5237 case NFS4_READW_LT:
7214e860
JL
5238 spin_lock(&fp->fi_lock);
5239 filp = find_readable_file_locked(fp);
0997b173
BF
5240 if (filp)
5241 get_lock_access(lock_stp, NFS4_SHARE_ACCESS_READ);
7214e860 5242 spin_unlock(&fp->fi_lock);
21179d81 5243 file_lock->fl_type = F_RDLCK;
529d7b2a 5244 break;
1da177e4
LT
5245 case NFS4_WRITE_LT:
5246 case NFS4_WRITEW_LT:
7214e860
JL
5247 spin_lock(&fp->fi_lock);
5248 filp = find_writeable_file_locked(fp);
0997b173
BF
5249 if (filp)
5250 get_lock_access(lock_stp, NFS4_SHARE_ACCESS_WRITE);
7214e860 5251 spin_unlock(&fp->fi_lock);
21179d81 5252 file_lock->fl_type = F_WRLCK;
529d7b2a 5253 break;
1da177e4
LT
5254 default:
5255 status = nfserr_inval;
5256 goto out;
5257 }
f9d7562f
BF
5258 if (!filp) {
5259 status = nfserr_openmode;
5260 goto out;
5261 }
21179d81
JL
5262 file_lock->fl_owner = (fl_owner_t)lock_sop;
5263 file_lock->fl_pid = current->tgid;
5264 file_lock->fl_file = filp;
5265 file_lock->fl_flags = FL_POSIX;
5266 file_lock->fl_lmops = &nfsd_posix_mng_ops;
5267 file_lock->fl_start = lock->lk_offset;
5268 file_lock->fl_end = last_byte_offset(lock->lk_offset, lock->lk_length);
5269 nfs4_transform_lock_offset(file_lock);
5270
5271 conflock = locks_alloc_lock();
5272 if (!conflock) {
5273 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
5274 status = nfserr_jukebox;
5275 goto out;
5276 }
1da177e4 5277
21179d81 5278 err = vfs_lock_file(filp, F_SETLK, file_lock, conflock);
b8dd7b9a 5279 switch (-err) {
1da177e4 5280 case 0: /* success! */
dcef0413
BF
5281 update_stateid(&lock_stp->st_stid.sc_stateid);
5282 memcpy(&lock->lk_resp_stateid, &lock_stp->st_stid.sc_stateid,
1da177e4 5283 sizeof(stateid_t));
b8dd7b9a 5284 status = 0;
eb76b3fd
AA
5285 break;
5286 case (EAGAIN): /* conflock holds conflicting lock */
5287 status = nfserr_denied;
5288 dprintk("NFSD: nfsd4_lock: conflicting lock found!\n");
21179d81 5289 nfs4_set_lock_denied(conflock, &lock->lk_denied);
eb76b3fd 5290 break;
1da177e4
LT
5291 case (EDEADLK):
5292 status = nfserr_deadlock;
eb76b3fd 5293 break;
3e772463 5294 default:
fd85b817 5295 dprintk("NFSD: nfsd4_lock: vfs_lock_file() failed! status %d\n",err);
3e772463 5296 status = nfserrno(err);
eb76b3fd 5297 break;
1da177e4 5298 }
1da177e4 5299out:
de18643d
TM
5300 if (filp)
5301 fput(filp);
5db1c03f
JL
5302 if (lock_stp) {
5303 /* Bump seqid manually if the 4.0 replay owner is openowner */
5304 if (cstate->replay_owner &&
5305 cstate->replay_owner != &lock_sop->lo_owner &&
5306 seqid_mutating_err(ntohl(status)))
5307 lock_sop->lo_owner.so_seqid++;
5308
5309 /*
5310 * If this is a new, never-before-used stateid, and we are
5311 * returning an error, then just go ahead and release it.
5312 */
5313 if (status && new)
5314 release_lock_stateid(lock_stp);
5315
3d0fabd5 5316 nfs4_put_stid(&lock_stp->st_stid);
5db1c03f 5317 }
0667b1e9
TM
5318 if (open_stp)
5319 nfs4_put_stid(&open_stp->st_stid);
9411b1d4 5320 nfsd4_bump_seqid(cstate, status);
21179d81
JL
5321 if (file_lock)
5322 locks_free_lock(file_lock);
5323 if (conflock)
5324 locks_free_lock(conflock);
1da177e4
LT
5325 return status;
5326}
5327
55ef1274
BF
5328/*
5329 * The NFSv4 spec allows a client to do a LOCKT without holding an OPEN,
5330 * so we do a temporary open here just to get an open file to pass to
5331 * vfs_test_lock. (Arguably perhaps test_lock should be done with an
5332 * inode operation.)
5333 */
04da6e9d 5334static __be32 nfsd_test_lock(struct svc_rqst *rqstp, struct svc_fh *fhp, struct file_lock *lock)
55ef1274
BF
5335{
5336 struct file *file;
04da6e9d
AV
5337 __be32 err = nfsd_open(rqstp, fhp, S_IFREG, NFSD_MAY_READ, &file);
5338 if (!err) {
5339 err = nfserrno(vfs_test_lock(file, lock));
5340 nfsd_close(file);
5341 }
55ef1274
BF
5342 return err;
5343}
5344
1da177e4
LT
5345/*
5346 * LOCKT operation
5347 */
b37ad28b 5348__be32
ca364317
BF
5349nfsd4_lockt(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
5350 struct nfsd4_lockt *lockt)
1da177e4 5351{
21179d81 5352 struct file_lock *file_lock = NULL;
5db1c03f 5353 struct nfs4_lockowner *lo = NULL;
b37ad28b 5354 __be32 status;
7f2210fa 5355 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
1da177e4 5356
5ccb0066 5357 if (locks_in_grace(SVC_NET(rqstp)))
1da177e4
LT
5358 return nfserr_grace;
5359
5360 if (check_lock_length(lockt->lt_offset, lockt->lt_length))
5361 return nfserr_inval;
5362
9b2ef62b 5363 if (!nfsd4_has_session(cstate)) {
4b24ca7d 5364 status = lookup_clientid(&lockt->lt_clientid, cstate, nn);
9b2ef62b
BF
5365 if (status)
5366 goto out;
5367 }
1da177e4 5368
75c096f7 5369 if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0)))
1da177e4 5370 goto out;
1da177e4 5371
21179d81
JL
5372 file_lock = locks_alloc_lock();
5373 if (!file_lock) {
5374 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
5375 status = nfserr_jukebox;
5376 goto out;
5377 }
6cd90662 5378
1da177e4
LT
5379 switch (lockt->lt_type) {
5380 case NFS4_READ_LT:
5381 case NFS4_READW_LT:
21179d81 5382 file_lock->fl_type = F_RDLCK;
1da177e4
LT
5383 break;
5384 case NFS4_WRITE_LT:
5385 case NFS4_WRITEW_LT:
21179d81 5386 file_lock->fl_type = F_WRLCK;
1da177e4
LT
5387 break;
5388 default:
2fdada03 5389 dprintk("NFSD: nfs4_lockt: bad lock type!\n");
1da177e4
LT
5390 status = nfserr_inval;
5391 goto out;
5392 }
5393
d4f0489f
TM
5394 lo = find_lockowner_str(&lockt->lt_clientid, &lockt->lt_owner,
5395 cstate->clp);
fe0750e5 5396 if (lo)
21179d81
JL
5397 file_lock->fl_owner = (fl_owner_t)lo;
5398 file_lock->fl_pid = current->tgid;
5399 file_lock->fl_flags = FL_POSIX;
1da177e4 5400
21179d81
JL
5401 file_lock->fl_start = lockt->lt_offset;
5402 file_lock->fl_end = last_byte_offset(lockt->lt_offset, lockt->lt_length);
1da177e4 5403
21179d81 5404 nfs4_transform_lock_offset(file_lock);
1da177e4 5405
21179d81 5406 status = nfsd_test_lock(rqstp, &cstate->current_fh, file_lock);
04da6e9d 5407 if (status)
fd85b817 5408 goto out;
04da6e9d 5409
21179d81 5410 if (file_lock->fl_type != F_UNLCK) {
1da177e4 5411 status = nfserr_denied;
21179d81 5412 nfs4_set_lock_denied(file_lock, &lockt->lt_denied);
1da177e4
LT
5413 }
5414out:
5db1c03f
JL
5415 if (lo)
5416 nfs4_put_stateowner(&lo->lo_owner);
21179d81
JL
5417 if (file_lock)
5418 locks_free_lock(file_lock);
1da177e4
LT
5419 return status;
5420}
5421
b37ad28b 5422__be32
ca364317 5423nfsd4_locku(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
a4f1706a 5424 struct nfsd4_locku *locku)
1da177e4 5425{
dcef0413 5426 struct nfs4_ol_stateid *stp;
1da177e4 5427 struct file *filp = NULL;
21179d81 5428 struct file_lock *file_lock = NULL;
b37ad28b 5429 __be32 status;
b8dd7b9a 5430 int err;
3320fef1
SK
5431 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
5432
1da177e4
LT
5433 dprintk("NFSD: nfsd4_locku: start=%Ld length=%Ld\n",
5434 (long long) locku->lu_offset,
5435 (long long) locku->lu_length);
5436
5437 if (check_lock_length(locku->lu_offset, locku->lu_length))
5438 return nfserr_inval;
5439
9072d5c6 5440 status = nfs4_preprocess_seqid_op(cstate, locku->lu_seqid,
3320fef1
SK
5441 &locku->lu_stateid, NFS4_LOCK_STID,
5442 &stp, nn);
9072d5c6 5443 if (status)
1da177e4 5444 goto out;
11b9164a 5445 filp = find_any_file(stp->st_stid.sc_file);
f9d7562f
BF
5446 if (!filp) {
5447 status = nfserr_lock_range;
858cc573 5448 goto put_stateid;
f9d7562f 5449 }
21179d81
JL
5450 file_lock = locks_alloc_lock();
5451 if (!file_lock) {
5452 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
5453 status = nfserr_jukebox;
de18643d 5454 goto fput;
21179d81 5455 }
6cd90662 5456
21179d81 5457 file_lock->fl_type = F_UNLCK;
0a262ffb 5458 file_lock->fl_owner = (fl_owner_t)lockowner(stp->st_stateowner);
21179d81
JL
5459 file_lock->fl_pid = current->tgid;
5460 file_lock->fl_file = filp;
5461 file_lock->fl_flags = FL_POSIX;
5462 file_lock->fl_lmops = &nfsd_posix_mng_ops;
5463 file_lock->fl_start = locku->lu_offset;
5464
5465 file_lock->fl_end = last_byte_offset(locku->lu_offset,
5466 locku->lu_length);
5467 nfs4_transform_lock_offset(file_lock);
1da177e4 5468
21179d81 5469 err = vfs_lock_file(filp, F_SETLK, file_lock, NULL);
b8dd7b9a 5470 if (err) {
fd85b817 5471 dprintk("NFSD: nfs4_locku: vfs_lock_file failed!\n");
1da177e4
LT
5472 goto out_nfserr;
5473 }
dcef0413
BF
5474 update_stateid(&stp->st_stid.sc_stateid);
5475 memcpy(&locku->lu_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
de18643d
TM
5476fput:
5477 fput(filp);
858cc573
TM
5478put_stateid:
5479 nfs4_put_stid(&stp->st_stid);
1da177e4 5480out:
9411b1d4 5481 nfsd4_bump_seqid(cstate, status);
21179d81
JL
5482 if (file_lock)
5483 locks_free_lock(file_lock);
1da177e4
LT
5484 return status;
5485
5486out_nfserr:
b8dd7b9a 5487 status = nfserrno(err);
de18643d 5488 goto fput;
1da177e4
LT
5489}
5490
5491/*
5492 * returns
f9c00c3a
JL
5493 * true: locks held by lockowner
5494 * false: no locks held by lockowner
1da177e4 5495 */
f9c00c3a
JL
5496static bool
5497check_for_locks(struct nfs4_file *fp, struct nfs4_lockowner *lowner)
1da177e4
LT
5498{
5499 struct file_lock **flpp;
f9c00c3a
JL
5500 int status = false;
5501 struct file *filp = find_any_file(fp);
5502 struct inode *inode;
5503
5504 if (!filp) {
5505 /* Any valid lock stateid should have some sort of access */
5506 WARN_ON_ONCE(1);
5507 return status;
5508 }
5509
5510 inode = file_inode(filp);
1da177e4 5511
1c8c601a 5512 spin_lock(&inode->i_lock);
1da177e4 5513 for (flpp = &inode->i_flock; *flpp != NULL; flpp = &(*flpp)->fl_next) {
796dadfd 5514 if ((*flpp)->fl_owner == (fl_owner_t)lowner) {
f9c00c3a
JL
5515 status = true;
5516 break;
796dadfd 5517 }
1da177e4 5518 }
1c8c601a 5519 spin_unlock(&inode->i_lock);
f9c00c3a 5520 fput(filp);
1da177e4
LT
5521 return status;
5522}
5523
b37ad28b 5524__be32
b591480b
BF
5525nfsd4_release_lockowner(struct svc_rqst *rqstp,
5526 struct nfsd4_compound_state *cstate,
5527 struct nfsd4_release_lockowner *rlockowner)
1da177e4
LT
5528{
5529 clientid_t *clid = &rlockowner->rl_clientid;
882e9d25
JL
5530 struct nfs4_stateowner *sop;
5531 struct nfs4_lockowner *lo = NULL;
dcef0413 5532 struct nfs4_ol_stateid *stp;
1da177e4 5533 struct xdr_netobj *owner = &rlockowner->rl_owner;
d4f0489f 5534 unsigned int hashval = ownerstr_hashval(owner);
b37ad28b 5535 __be32 status;
7f2210fa 5536 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
c58c6610 5537 struct nfs4_client *clp;
1da177e4
LT
5538
5539 dprintk("nfsd4_release_lockowner clientid: (%08x/%08x):\n",
5540 clid->cl_boot, clid->cl_id);
5541
4b24ca7d 5542 status = lookup_clientid(clid, cstate, nn);
9b2ef62b 5543 if (status)
51f5e783 5544 return status;
9b2ef62b 5545
d4f0489f 5546 clp = cstate->clp;
fd44907c 5547 /* Find the matching lock stateowner */
d4f0489f 5548 spin_lock(&clp->cl_lock);
882e9d25 5549 list_for_each_entry(sop, &clp->cl_ownerstr_hashtbl[hashval],
d4f0489f 5550 so_strhash) {
fd44907c 5551
882e9d25
JL
5552 if (sop->so_is_open_owner || !same_owner_str(sop, owner))
5553 continue;
fd44907c 5554
882e9d25
JL
5555 /* see if there are still any locks associated with it */
5556 lo = lockowner(sop);
5557 list_for_each_entry(stp, &sop->so_stateids, st_perstateowner) {
5558 if (check_for_locks(stp->st_stid.sc_file, lo)) {
5559 status = nfserr_locks_held;
5560 spin_unlock(&clp->cl_lock);
51f5e783 5561 return status;
882e9d25 5562 }
5adfd885 5563 }
882e9d25
JL
5564
5565 atomic_inc(&sop->so_count);
5566 break;
1da177e4 5567 }
c58c6610 5568 spin_unlock(&clp->cl_lock);
882e9d25
JL
5569 if (lo)
5570 release_lockowner(lo);
1da177e4
LT
5571 return status;
5572}
5573
5574static inline struct nfs4_client_reclaim *
a55370a3 5575alloc_reclaim(void)
1da177e4 5576{
a55370a3 5577 return kmalloc(sizeof(struct nfs4_client_reclaim), GFP_KERNEL);
1da177e4
LT
5578}
5579
0ce0c2b5 5580bool
52e19c09 5581nfs4_has_reclaimed_state(const char *name, struct nfsd_net *nn)
c7b9a459 5582{
0ce0c2b5 5583 struct nfs4_client_reclaim *crp;
c7b9a459 5584
52e19c09 5585 crp = nfsd4_find_reclaim_client(name, nn);
0ce0c2b5 5586 return (crp && crp->cr_clp);
c7b9a459
N
5587}
5588
1da177e4
LT
5589/*
5590 * failure => all reset bets are off, nfserr_no_grace...
5591 */
772a9bbb 5592struct nfs4_client_reclaim *
52e19c09 5593nfs4_client_to_reclaim(const char *name, struct nfsd_net *nn)
1da177e4
LT
5594{
5595 unsigned int strhashval;
772a9bbb 5596 struct nfs4_client_reclaim *crp;
1da177e4 5597
a55370a3
N
5598 dprintk("NFSD nfs4_client_to_reclaim NAME: %.*s\n", HEXDIR_LEN, name);
5599 crp = alloc_reclaim();
772a9bbb
JL
5600 if (crp) {
5601 strhashval = clientstr_hashval(name);
5602 INIT_LIST_HEAD(&crp->cr_strhash);
52e19c09 5603 list_add(&crp->cr_strhash, &nn->reclaim_str_hashtbl[strhashval]);
772a9bbb 5604 memcpy(crp->cr_recdir, name, HEXDIR_LEN);
0ce0c2b5 5605 crp->cr_clp = NULL;
52e19c09 5606 nn->reclaim_str_hashtbl_size++;
772a9bbb
JL
5607 }
5608 return crp;
1da177e4
LT
5609}
5610
ce30e539 5611void
52e19c09 5612nfs4_remove_reclaim_record(struct nfs4_client_reclaim *crp, struct nfsd_net *nn)
ce30e539
JL
5613{
5614 list_del(&crp->cr_strhash);
5615 kfree(crp);
52e19c09 5616 nn->reclaim_str_hashtbl_size--;
ce30e539
JL
5617}
5618
2a4317c5 5619void
52e19c09 5620nfs4_release_reclaim(struct nfsd_net *nn)
1da177e4
LT
5621{
5622 struct nfs4_client_reclaim *crp = NULL;
5623 int i;
5624
1da177e4 5625 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
52e19c09
SK
5626 while (!list_empty(&nn->reclaim_str_hashtbl[i])) {
5627 crp = list_entry(nn->reclaim_str_hashtbl[i].next,
1da177e4 5628 struct nfs4_client_reclaim, cr_strhash);
52e19c09 5629 nfs4_remove_reclaim_record(crp, nn);
1da177e4
LT
5630 }
5631 }
063b0fb9 5632 WARN_ON_ONCE(nn->reclaim_str_hashtbl_size);
1da177e4
LT
5633}
5634
5635/*
5636 * called from OPEN, CLAIM_PREVIOUS with a new clientid. */
2a4317c5 5637struct nfs4_client_reclaim *
52e19c09 5638nfsd4_find_reclaim_client(const char *recdir, struct nfsd_net *nn)
1da177e4
LT
5639{
5640 unsigned int strhashval;
1da177e4
LT
5641 struct nfs4_client_reclaim *crp = NULL;
5642
278c931c 5643 dprintk("NFSD: nfs4_find_reclaim_client for recdir %s\n", recdir);
1da177e4 5644
278c931c 5645 strhashval = clientstr_hashval(recdir);
52e19c09 5646 list_for_each_entry(crp, &nn->reclaim_str_hashtbl[strhashval], cr_strhash) {
278c931c 5647 if (same_name(crp->cr_recdir, recdir)) {
1da177e4
LT
5648 return crp;
5649 }
5650 }
5651 return NULL;
5652}
5653
5654/*
5655* Called from OPEN. Look for clientid in reclaim list.
5656*/
b37ad28b 5657__be32
0fe492db
TM
5658nfs4_check_open_reclaim(clientid_t *clid,
5659 struct nfsd4_compound_state *cstate,
5660 struct nfsd_net *nn)
1da177e4 5661{
0fe492db 5662 __be32 status;
a52d726b
JL
5663
5664 /* find clientid in conf_id_hashtbl */
0fe492db
TM
5665 status = lookup_clientid(clid, cstate, nn);
5666 if (status)
a52d726b
JL
5667 return nfserr_reclaim_bad;
5668
3b3e7b72
JL
5669 if (test_bit(NFSD4_CLIENT_RECLAIM_COMPLETE, &cstate->clp->cl_flags))
5670 return nfserr_no_grace;
5671
0fe492db
TM
5672 if (nfsd4_client_record_check(cstate->clp))
5673 return nfserr_reclaim_bad;
5674
5675 return nfs_ok;
1da177e4
LT
5676}
5677
65178db4 5678#ifdef CONFIG_NFSD_FAULT_INJECTION
016200c3
JL
5679static inline void
5680put_client(struct nfs4_client *clp)
5681{
5682 atomic_dec(&clp->cl_refcount);
5683}
5684
285abdee
JL
5685static struct nfs4_client *
5686nfsd_find_client(struct sockaddr_storage *addr, size_t addr_size)
5687{
5688 struct nfs4_client *clp;
5689 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
5690 nfsd_net_id);
5691
5692 if (!nfsd_netns_ready(nn))
5693 return NULL;
5694
5695 list_for_each_entry(clp, &nn->client_lru, cl_lru) {
5696 if (memcmp(&clp->cl_addr, addr, addr_size) == 0)
5697 return clp;
5698 }
5699 return NULL;
5700}
5701
7ec0e36f 5702u64
285abdee 5703nfsd_inject_print_clients(void)
7ec0e36f
JL
5704{
5705 struct nfs4_client *clp;
5706 u64 count = 0;
5707 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
5708 nfsd_net_id);
5709 char buf[INET6_ADDRSTRLEN];
5710
5711 if (!nfsd_netns_ready(nn))
5712 return 0;
5713
5714 spin_lock(&nn->client_lock);
5715 list_for_each_entry(clp, &nn->client_lru, cl_lru) {
5716 rpc_ntop((struct sockaddr *)&clp->cl_addr, buf, sizeof(buf));
5717 pr_info("NFS Client: %s\n", buf);
5718 ++count;
5719 }
5720 spin_unlock(&nn->client_lock);
5721
5722 return count;
5723}
65178db4 5724
a0926d15 5725u64
285abdee 5726nfsd_inject_forget_client(struct sockaddr_storage *addr, size_t addr_size)
a0926d15
JL
5727{
5728 u64 count = 0;
5729 struct nfs4_client *clp;
5730 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
5731 nfsd_net_id);
5732
5733 if (!nfsd_netns_ready(nn))
5734 return count;
5735
5736 spin_lock(&nn->client_lock);
5737 clp = nfsd_find_client(addr, addr_size);
5738 if (clp) {
5739 if (mark_client_expired_locked(clp) == nfs_ok)
5740 ++count;
5741 else
5742 clp = NULL;
5743 }
5744 spin_unlock(&nn->client_lock);
5745
5746 if (clp)
5747 expire_client(clp);
5748
5749 return count;
5750}
5751
69fc9edf 5752u64
285abdee 5753nfsd_inject_forget_clients(u64 max)
69fc9edf
JL
5754{
5755 u64 count = 0;
5756 struct nfs4_client *clp, *next;
5757 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
5758 nfsd_net_id);
5759 LIST_HEAD(reaplist);
5760
5761 if (!nfsd_netns_ready(nn))
5762 return count;
5763
5764 spin_lock(&nn->client_lock);
5765 list_for_each_entry_safe(clp, next, &nn->client_lru, cl_lru) {
5766 if (mark_client_expired_locked(clp) == nfs_ok) {
5767 list_add(&clp->cl_lru, &reaplist);
5768 if (max != 0 && ++count >= max)
5769 break;
5770 }
5771 }
5772 spin_unlock(&nn->client_lock);
5773
5774 list_for_each_entry_safe(clp, next, &reaplist, cl_lru)
5775 expire_client(clp);
5776
5777 return count;
5778}
5779
184c1847
BS
5780static void nfsd_print_count(struct nfs4_client *clp, unsigned int count,
5781 const char *type)
5782{
5783 char buf[INET6_ADDRSTRLEN];
0a5c33e2 5784 rpc_ntop((struct sockaddr *)&clp->cl_addr, buf, sizeof(buf));
184c1847
BS
5785 printk(KERN_INFO "NFS Client: %s has %u %s\n", buf, count, type);
5786}
5787
016200c3
JL
5788static void
5789nfsd_inject_add_lock_to_list(struct nfs4_ol_stateid *lst,
5790 struct list_head *collect)
5791{
5792 struct nfs4_client *clp = lst->st_stid.sc_client;
5793 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
5794 nfsd_net_id);
5795
5796 if (!collect)
5797 return;
5798
5799 lockdep_assert_held(&nn->client_lock);
5800 atomic_inc(&clp->cl_refcount);
5801 list_add(&lst->st_locks, collect);
5802}
5803
3c87b9b7 5804static u64 nfsd_foreach_client_lock(struct nfs4_client *clp, u64 max,
3738d50e 5805 struct list_head *collect,
3c87b9b7 5806 void (*func)(struct nfs4_ol_stateid *))
fc29171f
BS
5807{
5808 struct nfs4_openowner *oop;
fc29171f 5809 struct nfs4_ol_stateid *stp, *st_next;
3c87b9b7 5810 struct nfs4_ol_stateid *lst, *lst_next;
fc29171f
BS
5811 u64 count = 0;
5812
016200c3 5813 spin_lock(&clp->cl_lock);
fc29171f 5814 list_for_each_entry(oop, &clp->cl_openowners, oo_perclient) {
3c87b9b7
TM
5815 list_for_each_entry_safe(stp, st_next,
5816 &oop->oo_owner.so_stateids, st_perstateowner) {
5817 list_for_each_entry_safe(lst, lst_next,
5818 &stp->st_locks, st_locks) {
3738d50e 5819 if (func) {
3c87b9b7 5820 func(lst);
016200c3
JL
5821 nfsd_inject_add_lock_to_list(lst,
5822 collect);
3738d50e 5823 }
016200c3
JL
5824 ++count;
5825 /*
5826 * Despite the fact that these functions deal
5827 * with 64-bit integers for "count", we must
5828 * ensure that it doesn't blow up the
5829 * clp->cl_refcount. Throw a warning if we
5830 * start to approach INT_MAX here.
5831 */
5832 WARN_ON_ONCE(count == (INT_MAX / 2));
5833 if (count == max)
5834 goto out;
fc29171f
BS
5835 }
5836 }
5837 }
016200c3
JL
5838out:
5839 spin_unlock(&clp->cl_lock);
fc29171f
BS
5840
5841 return count;
5842}
5843
016200c3
JL
5844static u64
5845nfsd_collect_client_locks(struct nfs4_client *clp, struct list_head *collect,
5846 u64 max)
fc29171f 5847{
016200c3 5848 return nfsd_foreach_client_lock(clp, max, collect, unhash_lock_stateid);
fc29171f
BS
5849}
5850
016200c3
JL
5851static u64
5852nfsd_print_client_locks(struct nfs4_client *clp)
184c1847 5853{
016200c3 5854 u64 count = nfsd_foreach_client_lock(clp, 0, NULL, NULL);
184c1847
BS
5855 nfsd_print_count(clp, count, "locked files");
5856 return count;
5857}
5858
016200c3 5859u64
285abdee 5860nfsd_inject_print_locks(void)
016200c3
JL
5861{
5862 struct nfs4_client *clp;
5863 u64 count = 0;
5864 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
5865 nfsd_net_id);
5866
5867 if (!nfsd_netns_ready(nn))
5868 return 0;
5869
5870 spin_lock(&nn->client_lock);
5871 list_for_each_entry(clp, &nn->client_lru, cl_lru)
5872 count += nfsd_print_client_locks(clp);
5873 spin_unlock(&nn->client_lock);
5874
5875 return count;
5876}
5877
5878static void
5879nfsd_reap_locks(struct list_head *reaplist)
5880{
5881 struct nfs4_client *clp;
5882 struct nfs4_ol_stateid *stp, *next;
5883
5884 list_for_each_entry_safe(stp, next, reaplist, st_locks) {
5885 list_del_init(&stp->st_locks);
5886 clp = stp->st_stid.sc_client;
5887 nfs4_put_stid(&stp->st_stid);
5888 put_client(clp);
5889 }
5890}
5891
5892u64
285abdee 5893nfsd_inject_forget_client_locks(struct sockaddr_storage *addr, size_t addr_size)
016200c3
JL
5894{
5895 unsigned int count = 0;
5896 struct nfs4_client *clp;
5897 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
5898 nfsd_net_id);
5899 LIST_HEAD(reaplist);
5900
5901 if (!nfsd_netns_ready(nn))
5902 return count;
5903
5904 spin_lock(&nn->client_lock);
5905 clp = nfsd_find_client(addr, addr_size);
5906 if (clp)
5907 count = nfsd_collect_client_locks(clp, &reaplist, 0);
5908 spin_unlock(&nn->client_lock);
5909 nfsd_reap_locks(&reaplist);
5910 return count;
5911}
5912
5913u64
285abdee 5914nfsd_inject_forget_locks(u64 max)
016200c3
JL
5915{
5916 u64 count = 0;
5917 struct nfs4_client *clp;
5918 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
5919 nfsd_net_id);
5920 LIST_HEAD(reaplist);
5921
5922 if (!nfsd_netns_ready(nn))
5923 return count;
5924
5925 spin_lock(&nn->client_lock);
5926 list_for_each_entry(clp, &nn->client_lru, cl_lru) {
5927 count += nfsd_collect_client_locks(clp, &reaplist, max - count);
5928 if (max != 0 && count >= max)
5929 break;
5930 }
5931 spin_unlock(&nn->client_lock);
5932 nfsd_reap_locks(&reaplist);
5933 return count;
5934}
5935
82e05efa
JL
5936static u64
5937nfsd_foreach_client_openowner(struct nfs4_client *clp, u64 max,
5938 struct list_head *collect,
5939 void (*func)(struct nfs4_openowner *))
4dbdbda8
BS
5940{
5941 struct nfs4_openowner *oop, *next;
82e05efa
JL
5942 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
5943 nfsd_net_id);
4dbdbda8
BS
5944 u64 count = 0;
5945
82e05efa
JL
5946 lockdep_assert_held(&nn->client_lock);
5947
5948 spin_lock(&clp->cl_lock);
4dbdbda8 5949 list_for_each_entry_safe(oop, next, &clp->cl_openowners, oo_perclient) {
82e05efa 5950 if (func) {
4dbdbda8 5951 func(oop);
82e05efa
JL
5952 if (collect) {
5953 atomic_inc(&clp->cl_refcount);
5954 list_add(&oop->oo_perclient, collect);
5955 }
5956 }
5957 ++count;
5958 /*
5959 * Despite the fact that these functions deal with
5960 * 64-bit integers for "count", we must ensure that
5961 * it doesn't blow up the clp->cl_refcount. Throw a
5962 * warning if we start to approach INT_MAX here.
5963 */
5964 WARN_ON_ONCE(count == (INT_MAX / 2));
5965 if (count == max)
4dbdbda8
BS
5966 break;
5967 }
82e05efa
JL
5968 spin_unlock(&clp->cl_lock);
5969
5970 return count;
5971}
5972
5973static u64
5974nfsd_print_client_openowners(struct nfs4_client *clp)
5975{
5976 u64 count = nfsd_foreach_client_openowner(clp, 0, NULL, NULL);
5977
5978 nfsd_print_count(clp, count, "openowners");
5979 return count;
5980}
5981
5982static u64
5983nfsd_collect_client_openowners(struct nfs4_client *clp,
5984 struct list_head *collect, u64 max)
5985{
5986 return nfsd_foreach_client_openowner(clp, max, collect,
5987 unhash_openowner_locked);
5988}
5989
5990u64
285abdee 5991nfsd_inject_print_openowners(void)
82e05efa
JL
5992{
5993 struct nfs4_client *clp;
5994 u64 count = 0;
5995 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
5996 nfsd_net_id);
5997
5998 if (!nfsd_netns_ready(nn))
5999 return 0;
6000
6001 spin_lock(&nn->client_lock);
6002 list_for_each_entry(clp, &nn->client_lru, cl_lru)
6003 count += nfsd_print_client_openowners(clp);
6004 spin_unlock(&nn->client_lock);
4dbdbda8
BS
6005
6006 return count;
6007}
6008
82e05efa
JL
6009static void
6010nfsd_reap_openowners(struct list_head *reaplist)
6011{
6012 struct nfs4_client *clp;
6013 struct nfs4_openowner *oop, *next;
6014
6015 list_for_each_entry_safe(oop, next, reaplist, oo_perclient) {
6016 list_del_init(&oop->oo_perclient);
6017 clp = oop->oo_owner.so_client;
6018 release_openowner(oop);
6019 put_client(clp);
6020 }
6021}
6022
6023u64
285abdee
JL
6024nfsd_inject_forget_client_openowners(struct sockaddr_storage *addr,
6025 size_t addr_size)
4dbdbda8 6026{
82e05efa
JL
6027 unsigned int count = 0;
6028 struct nfs4_client *clp;
6029 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6030 nfsd_net_id);
6031 LIST_HEAD(reaplist);
6032
6033 if (!nfsd_netns_ready(nn))
6034 return count;
6035
6036 spin_lock(&nn->client_lock);
6037 clp = nfsd_find_client(addr, addr_size);
6038 if (clp)
6039 count = nfsd_collect_client_openowners(clp, &reaplist, 0);
6040 spin_unlock(&nn->client_lock);
6041 nfsd_reap_openowners(&reaplist);
6042 return count;
4dbdbda8
BS
6043}
6044
82e05efa 6045u64
285abdee 6046nfsd_inject_forget_openowners(u64 max)
184c1847 6047{
82e05efa
JL
6048 u64 count = 0;
6049 struct nfs4_client *clp;
6050 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6051 nfsd_net_id);
6052 LIST_HEAD(reaplist);
6053
6054 if (!nfsd_netns_ready(nn))
6055 return count;
6056
6057 spin_lock(&nn->client_lock);
6058 list_for_each_entry(clp, &nn->client_lru, cl_lru) {
6059 count += nfsd_collect_client_openowners(clp, &reaplist,
6060 max - count);
6061 if (max != 0 && count >= max)
6062 break;
6063 }
6064 spin_unlock(&nn->client_lock);
6065 nfsd_reap_openowners(&reaplist);
184c1847
BS
6066 return count;
6067}
6068
269de30f
BS
6069static u64 nfsd_find_all_delegations(struct nfs4_client *clp, u64 max,
6070 struct list_head *victims)
6071{
6072 struct nfs4_delegation *dp, *next;
98d5c7c5
JL
6073 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6074 nfsd_net_id);
269de30f
BS
6075 u64 count = 0;
6076
98d5c7c5
JL
6077 lockdep_assert_held(&nn->client_lock);
6078
6079 spin_lock(&state_lock);
269de30f 6080 list_for_each_entry_safe(dp, next, &clp->cl_delegations, dl_perclnt) {
dff1399f
JL
6081 if (victims) {
6082 /*
6083 * It's not safe to mess with delegations that have a
6084 * non-zero dl_time. They might have already been broken
6085 * and could be processed by the laundromat outside of
6086 * the state_lock. Just leave them be.
6087 */
6088 if (dp->dl_time != 0)
6089 continue;
6090
98d5c7c5 6091 atomic_inc(&clp->cl_refcount);
42690676
JL
6092 unhash_delegation_locked(dp);
6093 list_add(&dp->dl_recall_lru, victims);
dff1399f 6094 }
98d5c7c5
JL
6095 ++count;
6096 /*
6097 * Despite the fact that these functions deal with
6098 * 64-bit integers for "count", we must ensure that
6099 * it doesn't blow up the clp->cl_refcount. Throw a
6100 * warning if we start to approach INT_MAX here.
6101 */
6102 WARN_ON_ONCE(count == (INT_MAX / 2));
6103 if (count == max)
269de30f
BS
6104 break;
6105 }
98d5c7c5 6106 spin_unlock(&state_lock);
269de30f
BS
6107 return count;
6108}
6109
98d5c7c5
JL
6110static u64
6111nfsd_print_client_delegations(struct nfs4_client *clp)
269de30f 6112{
98d5c7c5 6113 u64 count = nfsd_find_all_delegations(clp, 0, NULL);
269de30f 6114
98d5c7c5
JL
6115 nfsd_print_count(clp, count, "delegations");
6116 return count;
6117}
6118
6119u64
285abdee 6120nfsd_inject_print_delegations(void)
98d5c7c5
JL
6121{
6122 struct nfs4_client *clp;
6123 u64 count = 0;
6124 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6125 nfsd_net_id);
6126
6127 if (!nfsd_netns_ready(nn))
6128 return 0;
269de30f 6129
98d5c7c5
JL
6130 spin_lock(&nn->client_lock);
6131 list_for_each_entry(clp, &nn->client_lru, cl_lru)
6132 count += nfsd_print_client_delegations(clp);
6133 spin_unlock(&nn->client_lock);
6134
6135 return count;
6136}
6137
6138static void
6139nfsd_forget_delegations(struct list_head *reaplist)
6140{
6141 struct nfs4_client *clp;
6142 struct nfs4_delegation *dp, *next;
6143
6144 list_for_each_entry_safe(dp, next, reaplist, dl_recall_lru) {
2d4a532d 6145 list_del_init(&dp->dl_recall_lru);
98d5c7c5 6146 clp = dp->dl_stid.sc_client;
3bd64a5b 6147 revoke_delegation(dp);
98d5c7c5 6148 put_client(clp);
2d4a532d 6149 }
98d5c7c5
JL
6150}
6151
6152u64
285abdee
JL
6153nfsd_inject_forget_client_delegations(struct sockaddr_storage *addr,
6154 size_t addr_size)
98d5c7c5
JL
6155{
6156 u64 count = 0;
6157 struct nfs4_client *clp;
6158 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6159 nfsd_net_id);
6160 LIST_HEAD(reaplist);
6161
6162 if (!nfsd_netns_ready(nn))
6163 return count;
6164
6165 spin_lock(&nn->client_lock);
6166 clp = nfsd_find_client(addr, addr_size);
6167 if (clp)
6168 count = nfsd_find_all_delegations(clp, 0, &reaplist);
6169 spin_unlock(&nn->client_lock);
6170
6171 nfsd_forget_delegations(&reaplist);
6172 return count;
6173}
269de30f 6174
98d5c7c5 6175u64
285abdee 6176nfsd_inject_forget_delegations(u64 max)
98d5c7c5
JL
6177{
6178 u64 count = 0;
6179 struct nfs4_client *clp;
6180 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6181 nfsd_net_id);
6182 LIST_HEAD(reaplist);
6183
6184 if (!nfsd_netns_ready(nn))
6185 return count;
6186
6187 spin_lock(&nn->client_lock);
6188 list_for_each_entry(clp, &nn->client_lru, cl_lru) {
6189 count += nfsd_find_all_delegations(clp, max - count, &reaplist);
6190 if (max != 0 && count >= max)
6191 break;
6192 }
6193 spin_unlock(&nn->client_lock);
6194 nfsd_forget_delegations(&reaplist);
269de30f
BS
6195 return count;
6196}
6197
98d5c7c5
JL
6198static void
6199nfsd_recall_delegations(struct list_head *reaplist)
269de30f 6200{
98d5c7c5
JL
6201 struct nfs4_client *clp;
6202 struct nfs4_delegation *dp, *next;
269de30f 6203
98d5c7c5 6204 list_for_each_entry_safe(dp, next, reaplist, dl_recall_lru) {
dff1399f 6205 list_del_init(&dp->dl_recall_lru);
98d5c7c5
JL
6206 clp = dp->dl_stid.sc_client;
6207 /*
6208 * We skipped all entries that had a zero dl_time before,
6209 * so we can now reset the dl_time back to 0. If a delegation
6210 * break comes in now, then it won't make any difference since
6211 * we're recalling it either way.
6212 */
6213 spin_lock(&state_lock);
dff1399f 6214 dp->dl_time = 0;
98d5c7c5 6215 spin_unlock(&state_lock);
269de30f 6216 nfsd_break_one_deleg(dp);
98d5c7c5 6217 put_client(clp);
dff1399f 6218 }
98d5c7c5 6219}
269de30f 6220
98d5c7c5 6221u64
285abdee 6222nfsd_inject_recall_client_delegations(struct sockaddr_storage *addr,
98d5c7c5
JL
6223 size_t addr_size)
6224{
6225 u64 count = 0;
6226 struct nfs4_client *clp;
6227 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6228 nfsd_net_id);
6229 LIST_HEAD(reaplist);
6230
6231 if (!nfsd_netns_ready(nn))
6232 return count;
6233
6234 spin_lock(&nn->client_lock);
6235 clp = nfsd_find_client(addr, addr_size);
6236 if (clp)
6237 count = nfsd_find_all_delegations(clp, 0, &reaplist);
6238 spin_unlock(&nn->client_lock);
6239
6240 nfsd_recall_delegations(&reaplist);
269de30f
BS
6241 return count;
6242}
6243
98d5c7c5 6244u64
285abdee 6245nfsd_inject_recall_delegations(u64 max)
184c1847
BS
6246{
6247 u64 count = 0;
98d5c7c5
JL
6248 struct nfs4_client *clp, *next;
6249 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6250 nfsd_net_id);
6251 LIST_HEAD(reaplist);
184c1847 6252
98d5c7c5
JL
6253 if (!nfsd_netns_ready(nn))
6254 return count;
184c1847 6255
98d5c7c5
JL
6256 spin_lock(&nn->client_lock);
6257 list_for_each_entry_safe(clp, next, &nn->client_lru, cl_lru) {
6258 count += nfsd_find_all_delegations(clp, max - count, &reaplist);
6259 if (max != 0 && ++count >= max)
6260 break;
6261 }
6262 spin_unlock(&nn->client_lock);
6263 nfsd_recall_delegations(&reaplist);
184c1847
BS
6264 return count;
6265}
65178db4
BS
6266#endif /* CONFIG_NFSD_FAULT_INJECTION */
6267
c2f1a551
MS
6268/*
6269 * Since the lifetime of a delegation isn't limited to that of an open, a
6270 * client may quite reasonably hang on to a delegation as long as it has
6271 * the inode cached. This becomes an obvious problem the first time a
6272 * client's inode cache approaches the size of the server's total memory.
6273 *
6274 * For now we avoid this problem by imposing a hard limit on the number
6275 * of delegations, which varies according to the server's memory size.
6276 */
6277static void
6278set_max_delegations(void)
6279{
6280 /*
6281 * Allow at most 4 delegations per megabyte of RAM. Quick
6282 * estimates suggest that in the worst case (where every delegation
6283 * is for a different inode), a delegation could take about 1.5K,
6284 * giving a worst case usage of about 6% of memory.
6285 */
6286 max_delegations = nr_free_buffer_pages() >> (20 - 2 - PAGE_SHIFT);
6287}
6288
d85ed443 6289static int nfs4_state_create_net(struct net *net)
8daae4dc
SK
6290{
6291 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
6292 int i;
6293
6294 nn->conf_id_hashtbl = kmalloc(sizeof(struct list_head) *
6295 CLIENT_HASH_SIZE, GFP_KERNEL);
6296 if (!nn->conf_id_hashtbl)
382a62e7 6297 goto err;
0a7ec377
SK
6298 nn->unconf_id_hashtbl = kmalloc(sizeof(struct list_head) *
6299 CLIENT_HASH_SIZE, GFP_KERNEL);
6300 if (!nn->unconf_id_hashtbl)
6301 goto err_unconf_id;
1872de0e
SK
6302 nn->sessionid_hashtbl = kmalloc(sizeof(struct list_head) *
6303 SESSION_HASH_SIZE, GFP_KERNEL);
6304 if (!nn->sessionid_hashtbl)
6305 goto err_sessionid;
8daae4dc 6306
382a62e7 6307 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
8daae4dc 6308 INIT_LIST_HEAD(&nn->conf_id_hashtbl[i]);
0a7ec377 6309 INIT_LIST_HEAD(&nn->unconf_id_hashtbl[i]);
382a62e7 6310 }
1872de0e
SK
6311 for (i = 0; i < SESSION_HASH_SIZE; i++)
6312 INIT_LIST_HEAD(&nn->sessionid_hashtbl[i]);
382a62e7 6313 nn->conf_name_tree = RB_ROOT;
a99454aa 6314 nn->unconf_name_tree = RB_ROOT;
5ed58bb2 6315 INIT_LIST_HEAD(&nn->client_lru);
73758fed 6316 INIT_LIST_HEAD(&nn->close_lru);
e8c69d17 6317 INIT_LIST_HEAD(&nn->del_recall_lru);
c9a49628 6318 spin_lock_init(&nn->client_lock);
8daae4dc 6319
09121281 6320 INIT_DELAYED_WORK(&nn->laundromat_work, laundromat_main);
d85ed443 6321 get_net(net);
09121281 6322
8daae4dc 6323 return 0;
382a62e7 6324
1872de0e 6325err_sessionid:
9b531137 6326 kfree(nn->unconf_id_hashtbl);
0a7ec377
SK
6327err_unconf_id:
6328 kfree(nn->conf_id_hashtbl);
382a62e7
SK
6329err:
6330 return -ENOMEM;
8daae4dc
SK
6331}
6332
6333static void
4dce0ac9 6334nfs4_state_destroy_net(struct net *net)
8daae4dc
SK
6335{
6336 int i;
6337 struct nfs4_client *clp = NULL;
6338 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
6339
6340 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
6341 while (!list_empty(&nn->conf_id_hashtbl[i])) {
6342 clp = list_entry(nn->conf_id_hashtbl[i].next, struct nfs4_client, cl_idhash);
6343 destroy_client(clp);
6344 }
6345 }
a99454aa 6346
2b905635
KM
6347 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
6348 while (!list_empty(&nn->unconf_id_hashtbl[i])) {
6349 clp = list_entry(nn->unconf_id_hashtbl[i].next, struct nfs4_client, cl_idhash);
6350 destroy_client(clp);
6351 }
a99454aa
SK
6352 }
6353
1872de0e 6354 kfree(nn->sessionid_hashtbl);
0a7ec377 6355 kfree(nn->unconf_id_hashtbl);
8daae4dc 6356 kfree(nn->conf_id_hashtbl);
4dce0ac9 6357 put_net(net);
8daae4dc
SK
6358}
6359
f252bc68 6360int
d85ed443 6361nfs4_state_start_net(struct net *net)
ac4d8ff2 6362{
5e1533c7 6363 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
b5a1a81e
BF
6364 int ret;
6365
d85ed443 6366 ret = nfs4_state_create_net(net);
8daae4dc
SK
6367 if (ret)
6368 return ret;
2c142baa 6369 nn->boot_time = get_seconds();
a51c84ed 6370 nn->grace_ended = false;
d4318acd
JL
6371 locks_start_grace(net, &nn->nfsd4_manager);
6372 nfsd4_client_tracking_init(net);
d85ed443 6373 printk(KERN_INFO "NFSD: starting %ld-second grace period (net %p)\n",
5284b44e
SK
6374 nn->nfsd4_grace, net);
6375 queue_delayed_work(laundry_wq, &nn->laundromat_work, nn->nfsd4_grace * HZ);
d85ed443
SK
6376 return 0;
6377}
6378
6379/* initialization to perform when the nfsd service is started: */
6380
6381int
6382nfs4_state_start(void)
6383{
6384 int ret;
6385
b5a1a81e 6386 ret = set_callback_cred();
d85ed443
SK
6387 if (ret)
6388 return -ENOMEM;
58da282b 6389 laundry_wq = create_singlethread_workqueue("nfsd4");
a6d6b781
JL
6390 if (laundry_wq == NULL) {
6391 ret = -ENOMEM;
6392 goto out_recovery;
6393 }
b5a1a81e
BF
6394 ret = nfsd4_create_callback_queue();
6395 if (ret)
6396 goto out_free_laundry;
09121281 6397
c2f1a551 6398 set_max_delegations();
d85ed443 6399
b5a1a81e 6400 return 0;
d85ed443 6401
b5a1a81e
BF
6402out_free_laundry:
6403 destroy_workqueue(laundry_wq);
a6d6b781 6404out_recovery:
b5a1a81e 6405 return ret;
1da177e4
LT
6406}
6407
f252bc68 6408void
4dce0ac9 6409nfs4_state_shutdown_net(struct net *net)
1da177e4 6410{
1da177e4 6411 struct nfs4_delegation *dp = NULL;
1da177e4 6412 struct list_head *pos, *next, reaplist;
4dce0ac9 6413 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
1da177e4 6414
4dce0ac9
SK
6415 cancel_delayed_work_sync(&nn->laundromat_work);
6416 locks_end_grace(&nn->nfsd4_manager);
ac55fdc4 6417
1da177e4 6418 INIT_LIST_HEAD(&reaplist);
cdc97505 6419 spin_lock(&state_lock);
e8c69d17 6420 list_for_each_safe(pos, next, &nn->del_recall_lru) {
1da177e4 6421 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
42690676
JL
6422 unhash_delegation_locked(dp);
6423 list_add(&dp->dl_recall_lru, &reaplist);
1da177e4 6424 }
cdc97505 6425 spin_unlock(&state_lock);
1da177e4
LT
6426 list_for_each_safe(pos, next, &reaplist) {
6427 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
42690676 6428 list_del_init(&dp->dl_recall_lru);
afbda402 6429 nfs4_put_deleg_lease(dp->dl_stid.sc_file);
6011695d 6430 nfs4_put_stid(&dp->dl_stid);
1da177e4
LT
6431 }
6432
3320fef1 6433 nfsd4_client_tracking_exit(net);
4dce0ac9 6434 nfs4_state_destroy_net(net);
1da177e4
LT
6435}
6436
6437void
6438nfs4_state_shutdown(void)
6439{
5e8d5c29 6440 destroy_workqueue(laundry_wq);
c3935e30 6441 nfsd4_destroy_callback_queue();
1da177e4 6442}
8b70484c
TM
6443
6444static void
6445get_stateid(struct nfsd4_compound_state *cstate, stateid_t *stateid)
6446{
37c593c5
TM
6447 if (HAS_STATE_ID(cstate, CURRENT_STATE_ID_FLAG) && CURRENT_STATEID(stateid))
6448 memcpy(stateid, &cstate->current_stateid, sizeof(stateid_t));
8b70484c
TM
6449}
6450
6451static void
6452put_stateid(struct nfsd4_compound_state *cstate, stateid_t *stateid)
6453{
37c593c5
TM
6454 if (cstate->minorversion) {
6455 memcpy(&cstate->current_stateid, stateid, sizeof(stateid_t));
6456 SET_STATE_ID(cstate, CURRENT_STATE_ID_FLAG);
6457 }
6458}
6459
6460void
6461clear_current_stateid(struct nfsd4_compound_state *cstate)
6462{
6463 CLEAR_STATE_ID(cstate, CURRENT_STATE_ID_FLAG);
8b70484c
TM
6464}
6465
62cd4a59
TM
6466/*
6467 * functions to set current state id
6468 */
9428fe1a
TM
6469void
6470nfsd4_set_opendowngradestateid(struct nfsd4_compound_state *cstate, struct nfsd4_open_downgrade *odp)
6471{
6472 put_stateid(cstate, &odp->od_stateid);
6473}
6474
8b70484c
TM
6475void
6476nfsd4_set_openstateid(struct nfsd4_compound_state *cstate, struct nfsd4_open *open)
6477{
6478 put_stateid(cstate, &open->op_stateid);
6479}
6480
62cd4a59
TM
6481void
6482nfsd4_set_closestateid(struct nfsd4_compound_state *cstate, struct nfsd4_close *close)
6483{
6484 put_stateid(cstate, &close->cl_stateid);
6485}
6486
6487void
6488nfsd4_set_lockstateid(struct nfsd4_compound_state *cstate, struct nfsd4_lock *lock)
6489{
6490 put_stateid(cstate, &lock->lk_resp_stateid);
6491}
6492
6493/*
6494 * functions to consume current state id
6495 */
1e97b519 6496
9428fe1a
TM
6497void
6498nfsd4_get_opendowngradestateid(struct nfsd4_compound_state *cstate, struct nfsd4_open_downgrade *odp)
6499{
6500 get_stateid(cstate, &odp->od_stateid);
6501}
6502
6503void
6504nfsd4_get_delegreturnstateid(struct nfsd4_compound_state *cstate, struct nfsd4_delegreturn *drp)
6505{
6506 get_stateid(cstate, &drp->dr_stateid);
6507}
6508
1e97b519
TM
6509void
6510nfsd4_get_freestateid(struct nfsd4_compound_state *cstate, struct nfsd4_free_stateid *fsp)
6511{
6512 get_stateid(cstate, &fsp->fr_stateid);
6513}
6514
6515void
6516nfsd4_get_setattrstateid(struct nfsd4_compound_state *cstate, struct nfsd4_setattr *setattr)
6517{
6518 get_stateid(cstate, &setattr->sa_stateid);
6519}
6520
8b70484c
TM
6521void
6522nfsd4_get_closestateid(struct nfsd4_compound_state *cstate, struct nfsd4_close *close)
6523{
6524 get_stateid(cstate, &close->cl_stateid);
6525}
6526
6527void
62cd4a59 6528nfsd4_get_lockustateid(struct nfsd4_compound_state *cstate, struct nfsd4_locku *locku)
8b70484c 6529{
62cd4a59 6530 get_stateid(cstate, &locku->lu_stateid);
8b70484c 6531}
30813e27
TM
6532
6533void
6534nfsd4_get_readstateid(struct nfsd4_compound_state *cstate, struct nfsd4_read *read)
6535{
6536 get_stateid(cstate, &read->rd_stateid);
6537}
6538
6539void
6540nfsd4_get_writestateid(struct nfsd4_compound_state *cstate, struct nfsd4_write *write)
6541{
6542 get_stateid(cstate, &write->wr_stateid);
6543}
This page took 1.427488 seconds and 5 git commands to generate.