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