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