]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - fs/nfsd/nfs4state.c
nfsd: Protect nfsd4_destroy_clientid using client_lock
[mirror_ubuntu-artful-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{
6b10ad19
TM
2829 struct nfs4_client *conf, *unconf;
2830 struct nfs4_client *clp = NULL;
57b7b43b 2831 __be32 status = 0;
8daae4dc 2832 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
345c2842
MJ
2833
2834 nfs4_lock_state();
6b10ad19 2835 spin_lock(&nn->client_lock);
0a7ec377 2836 unconf = find_unconfirmed_client(&dc->clientid, true, nn);
8daae4dc 2837 conf = find_confirmed_client(&dc->clientid, true, nn);
78389046 2838 WARN_ON_ONCE(conf && unconf);
345c2842
MJ
2839
2840 if (conf) {
c0293b01 2841 if (client_has_state(conf)) {
345c2842
MJ
2842 status = nfserr_clientid_busy;
2843 goto out;
2844 }
6b10ad19 2845 clp = conf;
345c2842
MJ
2846 } else if (unconf)
2847 clp = unconf;
2848 else {
2849 status = nfserr_stale_clientid;
2850 goto out;
2851 }
57266a6e 2852 if (!mach_creds_match(clp, rqstp)) {
6b10ad19 2853 clp = NULL;
57266a6e
BF
2854 status = nfserr_wrong_cred;
2855 goto out;
2856 }
6b10ad19 2857 unhash_client_locked(clp);
345c2842 2858out:
6b10ad19 2859 spin_unlock(&nn->client_lock);
345c2842 2860 nfs4_unlock_state();
6b10ad19
TM
2861 if (clp)
2862 expire_client(clp);
345c2842
MJ
2863 return status;
2864}
2865
4dc6ec00
BF
2866__be32
2867nfsd4_reclaim_complete(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_reclaim_complete *rc)
2868{
57b7b43b 2869 __be32 status = 0;
bcecf1cc 2870
4dc6ec00
BF
2871 if (rc->rca_one_fs) {
2872 if (!cstate->current_fh.fh_dentry)
2873 return nfserr_nofilehandle;
2874 /*
2875 * We don't take advantage of the rca_one_fs case.
2876 * That's OK, it's optional, we can safely ignore it.
2877 */
2878 return nfs_ok;
2879 }
bcecf1cc 2880
4dc6ec00 2881 nfs4_lock_state();
bcecf1cc 2882 status = nfserr_complete_already;
a52d726b
JL
2883 if (test_and_set_bit(NFSD4_CLIENT_RECLAIM_COMPLETE,
2884 &cstate->session->se_client->cl_flags))
bcecf1cc
MJ
2885 goto out;
2886
2887 status = nfserr_stale_clientid;
2888 if (is_client_expired(cstate->session->se_client))
4dc6ec00
BF
2889 /*
2890 * The following error isn't really legal.
2891 * But we only get here if the client just explicitly
2892 * destroyed the client. Surely it no longer cares what
2893 * error it gets back on an operation for the dead
2894 * client.
2895 */
bcecf1cc
MJ
2896 goto out;
2897
2898 status = nfs_ok;
2a4317c5 2899 nfsd4_client_record_create(cstate->session->se_client);
bcecf1cc 2900out:
4dc6ec00 2901 nfs4_unlock_state();
bcecf1cc 2902 return status;
4dc6ec00
BF
2903}
2904
b37ad28b 2905__be32
b591480b
BF
2906nfsd4_setclientid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
2907 struct nfsd4_setclientid *setclid)
1da177e4 2908{
a084daf5 2909 struct xdr_netobj clname = setclid->se_name;
1da177e4 2910 nfs4_verifier clverifier = setclid->se_verf;
3dbacee6
TM
2911 struct nfs4_client *conf, *new;
2912 struct nfs4_client *unconf = NULL;
b37ad28b 2913 __be32 status;
c212cecf
SK
2914 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
2915
5cc40fd7
TM
2916 new = create_client(clname, rqstp, &clverifier);
2917 if (new == NULL)
2918 return nfserr_jukebox;
63db4632 2919 /* Cases below refer to rfc 3530 section 14.2.33: */
1da177e4 2920 nfs4_lock_state();
3dbacee6 2921 spin_lock(&nn->client_lock);
382a62e7 2922 conf = find_confirmed_client_by_name(&clname, nn);
28ce6054 2923 if (conf) {
63db4632 2924 /* case 0: */
1da177e4 2925 status = nfserr_clid_inuse;
e203d506
BF
2926 if (clp_used_exchangeid(conf))
2927 goto out;
026722c2 2928 if (!same_creds(&conf->cl_cred, &rqstp->rq_cred)) {
363168b4
JL
2929 char addr_str[INET6_ADDRSTRLEN];
2930 rpc_ntop((struct sockaddr *) &conf->cl_addr, addr_str,
2931 sizeof(addr_str));
2932 dprintk("NFSD: setclientid: string in use by client "
2933 "at %s\n", addr_str);
1da177e4
LT
2934 goto out;
2935 }
1da177e4 2936 }
a99454aa 2937 unconf = find_unconfirmed_client_by_name(&clname, nn);
8f930711 2938 if (unconf)
3dbacee6 2939 unhash_client_locked(unconf);
34b232bb 2940 if (conf && same_verf(&conf->cl_verifier, &clverifier))
63db4632 2941 /* case 1: probable callback update */
1da177e4 2942 copy_clid(new, conf);
34b232bb 2943 else /* case 4 (new client) or cases 2, 3 (client reboot): */
c212cecf 2944 gen_clid(new, nn);
8323c3b2 2945 new->cl_minorversion = 0;
6f3d772f 2946 gen_callback(new, setclid, rqstp);
ac55fdc4 2947 add_to_unconfirmed(new);
1da177e4
LT
2948 setclid->se_clientid.cl_boot = new->cl_clientid.cl_boot;
2949 setclid->se_clientid.cl_id = new->cl_clientid.cl_id;
2950 memcpy(setclid->se_confirm.data, new->cl_confirm.data, sizeof(setclid->se_confirm.data));
5cc40fd7 2951 new = NULL;
1da177e4
LT
2952 status = nfs_ok;
2953out:
3dbacee6 2954 spin_unlock(&nn->client_lock);
1da177e4 2955 nfs4_unlock_state();
5cc40fd7
TM
2956 if (new)
2957 free_client(new);
3dbacee6
TM
2958 if (unconf)
2959 expire_client(unconf);
1da177e4
LT
2960 return status;
2961}
2962
2963
b37ad28b 2964__be32
b591480b
BF
2965nfsd4_setclientid_confirm(struct svc_rqst *rqstp,
2966 struct nfsd4_compound_state *cstate,
2967 struct nfsd4_setclientid_confirm *setclientid_confirm)
1da177e4 2968{
21ab45a4 2969 struct nfs4_client *conf, *unconf;
d20c11d8 2970 struct nfs4_client *old = NULL;
1da177e4
LT
2971 nfs4_verifier confirm = setclientid_confirm->sc_confirm;
2972 clientid_t * clid = &setclientid_confirm->sc_clientid;
b37ad28b 2973 __be32 status;
7f2210fa 2974 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
1da177e4 2975
2c142baa 2976 if (STALE_CLIENTID(clid, nn))
1da177e4 2977 return nfserr_stale_clientid;
1da177e4 2978 nfs4_lock_state();
21ab45a4 2979
d20c11d8 2980 spin_lock(&nn->client_lock);
8daae4dc 2981 conf = find_confirmed_client(clid, false, nn);
0a7ec377 2982 unconf = find_unconfirmed_client(clid, false, nn);
a186e767 2983 /*
8695b90a
BF
2984 * We try hard to give out unique clientid's, so if we get an
2985 * attempt to confirm the same clientid with a different cred,
2986 * there's a bug somewhere. Let's charitably assume it's our
2987 * bug.
a186e767 2988 */
8695b90a
BF
2989 status = nfserr_serverfault;
2990 if (unconf && !same_creds(&unconf->cl_cred, &rqstp->rq_cred))
2991 goto out;
2992 if (conf && !same_creds(&conf->cl_cred, &rqstp->rq_cred))
2993 goto out;
63db4632 2994 /* cases below refer to rfc 3530 section 14.2.34: */
90d700b7
BF
2995 if (!unconf || !same_verf(&confirm, &unconf->cl_confirm)) {
2996 if (conf && !unconf) /* case 2: probable retransmit */
1da177e4 2997 status = nfs_ok;
90d700b7
BF
2998 else /* case 4: client hasn't noticed we rebooted yet? */
2999 status = nfserr_stale_clientid;
3000 goto out;
3001 }
3002 status = nfs_ok;
3003 if (conf) { /* case 1: callback update */
d20c11d8
JL
3004 old = unconf;
3005 unhash_client_locked(old);
8695b90a 3006 nfsd4_change_callback(conf, &unconf->cl_cb_conn);
90d700b7 3007 } else { /* case 3: normal case; new or rebooted client */
d20c11d8
JL
3008 old = find_confirmed_client_by_name(&unconf->cl_name, nn);
3009 if (old) {
3010 status = mark_client_expired_locked(old);
221a6876
BF
3011 if (status)
3012 goto out;
d20c11d8 3013 unhash_client_locked(old);
221a6876 3014 }
8695b90a 3015 move_to_confirmed(unconf);
d20c11d8 3016 conf = unconf;
08e8987c 3017 }
d20c11d8
JL
3018 get_client_locked(conf);
3019 spin_unlock(&nn->client_lock);
3020 nfsd4_probe_callback(conf);
3021 spin_lock(&nn->client_lock);
3022 put_client_renew_locked(conf);
1da177e4 3023out:
d20c11d8
JL
3024 spin_unlock(&nn->client_lock);
3025 if (old)
3026 expire_client(old);
1da177e4
LT
3027 nfs4_unlock_state();
3028 return status;
3029}
3030
32513b40
BF
3031static struct nfs4_file *nfsd4_alloc_file(void)
3032{
3033 return kmem_cache_alloc(file_slab, GFP_KERNEL);
3034}
3035
1da177e4 3036/* OPEN Share state helper functions */
f9c00c3a 3037static void nfsd4_init_file(struct nfs4_file *fp, struct knfsd_fh *fh)
1da177e4 3038{
ca943217 3039 unsigned int hashval = file_hashval(fh);
1da177e4 3040
950e0118
TM
3041 lockdep_assert_held(&state_lock);
3042
32513b40 3043 atomic_set(&fp->fi_ref, 1);
1d31a253 3044 spin_lock_init(&fp->fi_lock);
32513b40
BF
3045 INIT_LIST_HEAD(&fp->fi_stateids);
3046 INIT_LIST_HEAD(&fp->fi_delegations);
e2cf80d7 3047 fh_copy_shallow(&fp->fi_fhandle, fh);
32513b40
BF
3048 fp->fi_had_conflict = false;
3049 fp->fi_lease = NULL;
baeb4ff0 3050 fp->fi_share_deny = 0;
32513b40
BF
3051 memset(fp->fi_fds, 0, sizeof(fp->fi_fds));
3052 memset(fp->fi_access, 0, sizeof(fp->fi_access));
89876f8c 3053 hlist_add_head(&fp->fi_hash, &file_hashtbl[hashval]);
1da177e4
LT
3054}
3055
e8ff2a84 3056void
1da177e4
LT
3057nfsd4_free_slabs(void)
3058{
abf1135b
CH
3059 kmem_cache_destroy(openowner_slab);
3060 kmem_cache_destroy(lockowner_slab);
3061 kmem_cache_destroy(file_slab);
3062 kmem_cache_destroy(stateid_slab);
3063 kmem_cache_destroy(deleg_slab);
e60d4398 3064}
1da177e4 3065
72083396 3066int
e60d4398
N
3067nfsd4_init_slabs(void)
3068{
fe0750e5
BF
3069 openowner_slab = kmem_cache_create("nfsd4_openowners",
3070 sizeof(struct nfs4_openowner), 0, 0, NULL);
3071 if (openowner_slab == NULL)
abf1135b 3072 goto out;
fe0750e5 3073 lockowner_slab = kmem_cache_create("nfsd4_lockowners",
3c40794b 3074 sizeof(struct nfs4_lockowner), 0, 0, NULL);
fe0750e5 3075 if (lockowner_slab == NULL)
abf1135b 3076 goto out_free_openowner_slab;
e60d4398 3077 file_slab = kmem_cache_create("nfsd4_files",
20c2df83 3078 sizeof(struct nfs4_file), 0, 0, NULL);
e60d4398 3079 if (file_slab == NULL)
abf1135b 3080 goto out_free_lockowner_slab;
5ac049ac 3081 stateid_slab = kmem_cache_create("nfsd4_stateids",
dcef0413 3082 sizeof(struct nfs4_ol_stateid), 0, 0, NULL);
5ac049ac 3083 if (stateid_slab == NULL)
abf1135b 3084 goto out_free_file_slab;
5b2d21c1 3085 deleg_slab = kmem_cache_create("nfsd4_delegations",
20c2df83 3086 sizeof(struct nfs4_delegation), 0, 0, NULL);
5b2d21c1 3087 if (deleg_slab == NULL)
abf1135b 3088 goto out_free_stateid_slab;
e60d4398 3089 return 0;
abf1135b
CH
3090
3091out_free_stateid_slab:
3092 kmem_cache_destroy(stateid_slab);
3093out_free_file_slab:
3094 kmem_cache_destroy(file_slab);
3095out_free_lockowner_slab:
3096 kmem_cache_destroy(lockowner_slab);
3097out_free_openowner_slab:
3098 kmem_cache_destroy(openowner_slab);
3099out:
e60d4398
N
3100 dprintk("nfsd4: out of memory while initializing nfsv4\n");
3101 return -ENOMEM;
1da177e4
LT
3102}
3103
ff194bd9 3104static void init_nfs4_replay(struct nfs4_replay *rp)
1da177e4 3105{
ff194bd9
BF
3106 rp->rp_status = nfserr_serverfault;
3107 rp->rp_buflen = 0;
3108 rp->rp_buf = rp->rp_ibuf;
58fb12e6
JL
3109 mutex_init(&rp->rp_mutex);
3110}
3111
3112static void nfsd4_cstate_assign_replay(struct nfsd4_compound_state *cstate,
3113 struct nfs4_stateowner *so)
3114{
3115 if (!nfsd4_has_session(cstate)) {
3116 mutex_lock(&so->so_replay.rp_mutex);
3117 cstate->replay_owner = so;
3118 atomic_inc(&so->so_count);
3119 }
3120}
3121
3122void nfsd4_cstate_clear_replay(struct nfsd4_compound_state *cstate)
3123{
3124 struct nfs4_stateowner *so = cstate->replay_owner;
3125
3126 if (so != NULL) {
3127 cstate->replay_owner = NULL;
3128 mutex_unlock(&so->so_replay.rp_mutex);
3129 nfs4_put_stateowner(so);
3130 }
1da177e4
LT
3131}
3132
fe0750e5 3133static inline void *alloc_stateowner(struct kmem_cache *slab, struct xdr_netobj *owner, struct nfs4_client *clp)
ff194bd9 3134{
1da177e4 3135 struct nfs4_stateowner *sop;
1da177e4 3136
fe0750e5 3137 sop = kmem_cache_alloc(slab, GFP_KERNEL);
ff194bd9
BF
3138 if (!sop)
3139 return NULL;
3140
3141 sop->so_owner.data = kmemdup(owner->data, owner->len, GFP_KERNEL);
3142 if (!sop->so_owner.data) {
fe0750e5 3143 kmem_cache_free(slab, sop);
1da177e4 3144 return NULL;
ff194bd9
BF
3145 }
3146 sop->so_owner.len = owner->len;
3147
ea1da636 3148 INIT_LIST_HEAD(&sop->so_stateids);
ff194bd9
BF
3149 sop->so_client = clp;
3150 init_nfs4_replay(&sop->so_replay);
6b180f0b 3151 atomic_set(&sop->so_count, 1);
ff194bd9
BF
3152 return sop;
3153}
3154
fe0750e5 3155static void hash_openowner(struct nfs4_openowner *oo, struct nfs4_client *clp, unsigned int strhashval)
ff194bd9 3156{
d4f0489f 3157 lockdep_assert_held(&clp->cl_lock);
9b531137 3158
d4f0489f
TM
3159 list_add(&oo->oo_owner.so_strhash,
3160 &clp->cl_ownerstr_hashtbl[strhashval]);
fe0750e5 3161 list_add(&oo->oo_perclient, &clp->cl_openowners);
ff194bd9
BF
3162}
3163
8f4b54c5
JL
3164static void nfs4_unhash_openowner(struct nfs4_stateowner *so)
3165{
d4f0489f 3166 unhash_openowner_locked(openowner(so));
8f4b54c5
JL
3167}
3168
6b180f0b
JL
3169static void nfs4_free_openowner(struct nfs4_stateowner *so)
3170{
3171 struct nfs4_openowner *oo = openowner(so);
3172
3173 kmem_cache_free(openowner_slab, oo);
3174}
3175
3176static const struct nfs4_stateowner_operations openowner_ops = {
8f4b54c5
JL
3177 .so_unhash = nfs4_unhash_openowner,
3178 .so_free = nfs4_free_openowner,
6b180f0b
JL
3179};
3180
fe0750e5 3181static struct nfs4_openowner *
13d6f66b 3182alloc_init_open_stateowner(unsigned int strhashval, struct nfsd4_open *open,
db24b3b4
JL
3183 struct nfsd4_compound_state *cstate)
3184{
13d6f66b 3185 struct nfs4_client *clp = cstate->clp;
7ffb5880 3186 struct nfs4_openowner *oo, *ret;
ff194bd9 3187
fe0750e5
BF
3188 oo = alloc_stateowner(openowner_slab, &open->op_owner, clp);
3189 if (!oo)
ff194bd9 3190 return NULL;
6b180f0b 3191 oo->oo_owner.so_ops = &openowner_ops;
fe0750e5
BF
3192 oo->oo_owner.so_is_open_owner = 1;
3193 oo->oo_owner.so_seqid = open->op_seqid;
d3134b10 3194 oo->oo_flags = 0;
db24b3b4
JL
3195 if (nfsd4_has_session(cstate))
3196 oo->oo_flags |= NFS4_OO_CONFIRMED;
fe0750e5 3197 oo->oo_time = 0;
38c387b5 3198 oo->oo_last_closed_stid = NULL;
fe0750e5 3199 INIT_LIST_HEAD(&oo->oo_close_lru);
d4f0489f
TM
3200 spin_lock(&clp->cl_lock);
3201 ret = find_openstateowner_str_locked(strhashval, open, clp);
7ffb5880
TM
3202 if (ret == NULL) {
3203 hash_openowner(oo, clp, strhashval);
3204 ret = oo;
3205 } else
3206 nfs4_free_openowner(&oo->oo_owner);
d4f0489f 3207 spin_unlock(&clp->cl_lock);
fe0750e5 3208 return oo;
1da177e4
LT
3209}
3210
996e0938 3211static void init_open_stateid(struct nfs4_ol_stateid *stp, struct nfs4_file *fp, struct nfsd4_open *open) {
fe0750e5 3212 struct nfs4_openowner *oo = open->op_openowner;
1da177e4 3213
d6f2bc5d 3214 atomic_inc(&stp->st_stid.sc_count);
3abdb607 3215 stp->st_stid.sc_type = NFS4_OPEN_STID;
3c87b9b7 3216 INIT_LIST_HEAD(&stp->st_locks);
fe0750e5 3217 stp->st_stateowner = &oo->oo_owner;
d3134b10 3218 atomic_inc(&stp->st_stateowner->so_count);
13cd2184 3219 get_nfs4_file(fp);
11b9164a 3220 stp->st_stid.sc_file = fp;
1da177e4
LT
3221 stp->st_access_bmap = 0;
3222 stp->st_deny_bmap = 0;
4c4cd222 3223 stp->st_openstp = NULL;
1c755dc1
JL
3224 spin_lock(&oo->oo_owner.so_client->cl_lock);
3225 list_add(&stp->st_perstateowner, &oo->oo_owner.so_stateids);
1d31a253
TM
3226 spin_lock(&fp->fi_lock);
3227 list_add(&stp->st_perfile, &fp->fi_stateids);
3228 spin_unlock(&fp->fi_lock);
1c755dc1 3229 spin_unlock(&oo->oo_owner.so_client->cl_lock);
1da177e4
LT
3230}
3231
d3134b10
JL
3232/*
3233 * In the 4.0 case we need to keep the owners around a little while to handle
3234 * CLOSE replay. We still do need to release any file access that is held by
3235 * them before returning however.
3236 */
fd39ca9a 3237static void
d3134b10 3238move_to_close_lru(struct nfs4_ol_stateid *s, struct net *net)
1da177e4 3239{
d3134b10
JL
3240 struct nfs4_openowner *oo = openowner(s->st_stateowner);
3241 struct nfsd_net *nn = net_generic(s->st_stid.sc_client->net,
3242 nfsd_net_id);
73758fed 3243
fe0750e5 3244 dprintk("NFSD: move_to_close_lru nfs4_openowner %p\n", oo);
1da177e4 3245
b401be22
JL
3246 /*
3247 * We know that we hold one reference via nfsd4_close, and another
3248 * "persistent" reference for the client. If the refcount is higher
3249 * than 2, then there are still calls in progress that are using this
3250 * stateid. We can't put the sc_file reference until they are finished.
3251 * Wait for the refcount to drop to 2. Since it has been unhashed,
3252 * there should be no danger of the refcount going back up again at
3253 * this point.
3254 */
3255 wait_event(close_wq, atomic_read(&s->st_stid.sc_count) == 2);
3256
d3134b10
JL
3257 release_all_access(s);
3258 if (s->st_stid.sc_file) {
3259 put_nfs4_file(s->st_stid.sc_file);
3260 s->st_stid.sc_file = NULL;
3261 }
3262 release_last_closed_stateid(oo);
3263 oo->oo_last_closed_stid = s;
73758fed 3264 list_move_tail(&oo->oo_close_lru, &nn->close_lru);
fe0750e5 3265 oo->oo_time = get_seconds();
1da177e4
LT
3266}
3267
1da177e4
LT
3268/* search file_hashtbl[] for file */
3269static struct nfs4_file *
ca943217 3270find_file_locked(struct knfsd_fh *fh)
1da177e4 3271{
ca943217 3272 unsigned int hashval = file_hashval(fh);
1da177e4
LT
3273 struct nfs4_file *fp;
3274
950e0118
TM
3275 lockdep_assert_held(&state_lock);
3276
89876f8c 3277 hlist_for_each_entry(fp, &file_hashtbl[hashval], fi_hash) {
ca943217 3278 if (nfsd_fh_match(&fp->fi_fhandle, fh)) {
13cd2184 3279 get_nfs4_file(fp);
1da177e4 3280 return fp;
13cd2184 3281 }
1da177e4
LT
3282 }
3283 return NULL;
3284}
3285
950e0118 3286static struct nfs4_file *
ca943217 3287find_file(struct knfsd_fh *fh)
950e0118
TM
3288{
3289 struct nfs4_file *fp;
3290
3291 spin_lock(&state_lock);
ca943217 3292 fp = find_file_locked(fh);
950e0118
TM
3293 spin_unlock(&state_lock);
3294 return fp;
3295}
3296
3297static struct nfs4_file *
f9c00c3a 3298find_or_add_file(struct nfs4_file *new, struct knfsd_fh *fh)
950e0118
TM
3299{
3300 struct nfs4_file *fp;
3301
3302 spin_lock(&state_lock);
ca943217 3303 fp = find_file_locked(fh);
950e0118 3304 if (fp == NULL) {
f9c00c3a 3305 nfsd4_init_file(new, fh);
950e0118
TM
3306 fp = new;
3307 }
3308 spin_unlock(&state_lock);
3309
3310 return fp;
3311}
3312
1da177e4
LT
3313/*
3314 * Called to check deny when READ with all zero stateid or
3315 * WRITE with all zero or all one stateid
3316 */
b37ad28b 3317static __be32
1da177e4
LT
3318nfs4_share_conflict(struct svc_fh *current_fh, unsigned int deny_type)
3319{
1da177e4 3320 struct nfs4_file *fp;
baeb4ff0 3321 __be32 ret = nfs_ok;
1da177e4 3322
ca943217 3323 fp = find_file(&current_fh->fh_handle);
13cd2184 3324 if (!fp)
baeb4ff0
JL
3325 return ret;
3326 /* Check for conflicting share reservations */
1d31a253 3327 spin_lock(&fp->fi_lock);
baeb4ff0
JL
3328 if (fp->fi_share_deny & deny_type)
3329 ret = nfserr_locked;
1d31a253 3330 spin_unlock(&fp->fi_lock);
13cd2184
N
3331 put_nfs4_file(fp);
3332 return ret;
1da177e4
LT
3333}
3334
02e1215f 3335void nfsd4_prepare_cb_recall(struct nfs4_delegation *dp)
1da177e4 3336{
11b9164a
TM
3337 struct nfsd_net *nn = net_generic(dp->dl_stid.sc_client->net,
3338 nfsd_net_id);
e8c69d17 3339
11b9164a 3340 block_delegations(&dp->dl_stid.sc_file->fi_fhandle);
f54fe962 3341
dff1399f 3342 /*
f54fe962
JL
3343 * We can't do this in nfsd_break_deleg_cb because it is
3344 * already holding inode->i_lock.
3345 *
dff1399f
JL
3346 * If the dl_time != 0, then we know that it has already been
3347 * queued for a lease break. Don't queue it again.
3348 */
f54fe962 3349 spin_lock(&state_lock);
dff1399f 3350 if (dp->dl_time == 0) {
dff1399f 3351 dp->dl_time = get_seconds();
02e1215f 3352 list_add_tail(&dp->dl_recall_lru, &nn->del_recall_lru);
dff1399f 3353 }
02e1215f
JL
3354 spin_unlock(&state_lock);
3355}
1da177e4 3356
02e1215f
JL
3357static void nfsd_break_one_deleg(struct nfs4_delegation *dp)
3358{
3359 /*
3360 * We're assuming the state code never drops its reference
3361 * without first removing the lease. Since we're in this lease
3362 * callback (and since the lease code is serialized by the kernel
3363 * lock) we know the server hasn't removed the lease yet, we know
3364 * it's safe to take a reference.
3365 */
72c0b0fb 3366 atomic_inc(&dp->dl_stid.sc_count);
6b57d9c8
BF
3367 nfsd4_cb_recall(dp);
3368}
3369
1c8c601a 3370/* Called from break_lease() with i_lock held. */
6b57d9c8
BF
3371static void nfsd_break_deleg_cb(struct file_lock *fl)
3372{
acfdf5c3
BF
3373 struct nfs4_file *fp = (struct nfs4_file *)fl->fl_owner;
3374 struct nfs4_delegation *dp;
6b57d9c8 3375
7fa10cd1
BF
3376 if (!fp) {
3377 WARN(1, "(%p)->fl_owner NULL\n", fl);
3378 return;
3379 }
3380 if (fp->fi_had_conflict) {
3381 WARN(1, "duplicate break on %p\n", fp);
3382 return;
3383 }
0272e1fd
BF
3384 /*
3385 * We don't want the locks code to timeout the lease for us;
acfdf5c3 3386 * we'll remove it ourself if a delegation isn't returned
6b57d9c8 3387 * in time:
0272e1fd
BF
3388 */
3389 fl->fl_break_time = 0;
1da177e4 3390
02e1215f 3391 spin_lock(&fp->fi_lock);
417c6629
JL
3392 fp->fi_had_conflict = true;
3393 /*
3394 * If there are no delegations on the list, then we can't count on this
3395 * lease ever being cleaned up. Set the fl_break_time to jiffies so that
3396 * time_out_leases will do it ASAP. The fact that fi_had_conflict is now
3397 * true should keep any new delegations from being hashed.
3398 */
3399 if (list_empty(&fp->fi_delegations))
3400 fl->fl_break_time = jiffies;
3401 else
3402 list_for_each_entry(dp, &fp->fi_delegations, dl_perfile)
3403 nfsd_break_one_deleg(dp);
02e1215f 3404 spin_unlock(&fp->fi_lock);
1da177e4
LT
3405}
3406
1da177e4
LT
3407static
3408int nfsd_change_deleg_cb(struct file_lock **onlist, int arg)
3409{
3410 if (arg & F_UNLCK)
3411 return lease_modify(onlist, arg);
3412 else
3413 return -EAGAIN;
3414}
3415
7b021967 3416static const struct lock_manager_operations nfsd_lease_mng_ops = {
8fb47a4f
BF
3417 .lm_break = nfsd_break_deleg_cb,
3418 .lm_change = nfsd_change_deleg_cb,
1da177e4
LT
3419};
3420
7a8711c9
BF
3421static __be32 nfsd4_check_seqid(struct nfsd4_compound_state *cstate, struct nfs4_stateowner *so, u32 seqid)
3422{
3423 if (nfsd4_has_session(cstate))
3424 return nfs_ok;
3425 if (seqid == so->so_seqid - 1)
3426 return nfserr_replay_me;
3427 if (seqid == so->so_seqid)
3428 return nfs_ok;
3429 return nfserr_bad_seqid;
3430}
1da177e4 3431
4b24ca7d
JL
3432static __be32 lookup_clientid(clientid_t *clid,
3433 struct nfsd4_compound_state *cstate,
3434 struct nfsd_net *nn)
3435{
3436 struct nfs4_client *found;
3437
3438 if (cstate->clp) {
3439 found = cstate->clp;
3440 if (!same_clid(&found->cl_clientid, clid))
3441 return nfserr_stale_clientid;
3442 return nfs_ok;
3443 }
3444
3445 if (STALE_CLIENTID(clid, nn))
3446 return nfserr_stale_clientid;
3447
3448 /*
3449 * For v4.1+ we get the client in the SEQUENCE op. If we don't have one
3450 * cached already then we know this is for is for v4.0 and "sessions"
3451 * will be false.
3452 */
3453 WARN_ON_ONCE(cstate->session);
3454 found = find_confirmed_client(clid, false, nn);
3455 if (!found)
3456 return nfserr_expired;
3457
3458 /* Cache the nfs4_client in cstate! */
3459 cstate->clp = found;
3460 atomic_inc(&found->cl_refcount);
3461 return nfs_ok;
3462}
3463
b37ad28b 3464__be32
6668958f 3465nfsd4_process_open1(struct nfsd4_compound_state *cstate,
3320fef1 3466 struct nfsd4_open *open, struct nfsd_net *nn)
1da177e4 3467{
1da177e4
LT
3468 clientid_t *clientid = &open->op_clientid;
3469 struct nfs4_client *clp = NULL;
3470 unsigned int strhashval;
fe0750e5 3471 struct nfs4_openowner *oo = NULL;
4cdc951b 3472 __be32 status;
1da177e4 3473
2c142baa 3474 if (STALE_CLIENTID(&open->op_clientid, nn))
1da177e4 3475 return nfserr_stale_clientid;
32513b40
BF
3476 /*
3477 * In case we need it later, after we've already created the
3478 * file and don't want to risk a further failure:
3479 */
3480 open->op_file = nfsd4_alloc_file();
3481 if (open->op_file == NULL)
3482 return nfserr_jukebox;
1da177e4 3483
2d91e895
TM
3484 status = lookup_clientid(clientid, cstate, nn);
3485 if (status)
3486 return status;
3487 clp = cstate->clp;
3488
d4f0489f
TM
3489 strhashval = ownerstr_hashval(&open->op_owner);
3490 oo = find_openstateowner_str(strhashval, open, clp);
fe0750e5
BF
3491 open->op_openowner = oo;
3492 if (!oo) {
bcf130f9 3493 goto new_owner;
1da177e4 3494 }
dad1c067 3495 if (!(oo->oo_flags & NFS4_OO_CONFIRMED)) {
0f442aa2 3496 /* Replace unconfirmed owners without checking for replay. */
fe0750e5
BF
3497 release_openowner(oo);
3498 open->op_openowner = NULL;
bcf130f9 3499 goto new_owner;
0f442aa2 3500 }
4cdc951b
BF
3501 status = nfsd4_check_seqid(cstate, &oo->oo_owner, open->op_seqid);
3502 if (status)
3503 return status;
4cdc951b 3504 goto alloc_stateid;
bcf130f9 3505new_owner:
13d6f66b 3506 oo = alloc_init_open_stateowner(strhashval, open, cstate);
bcf130f9
BF
3507 if (oo == NULL)
3508 return nfserr_jukebox;
3509 open->op_openowner = oo;
4cdc951b 3510alloc_stateid:
b49e084d 3511 open->op_stp = nfs4_alloc_open_stateid(clp);
4cdc951b
BF
3512 if (!open->op_stp)
3513 return nfserr_jukebox;
0f442aa2 3514 return nfs_ok;
1da177e4
LT
3515}
3516
b37ad28b 3517static inline __be32
4a6e43e6
N
3518nfs4_check_delegmode(struct nfs4_delegation *dp, int flags)
3519{
3520 if ((flags & WR_STATE) && (dp->dl_type == NFS4_OPEN_DELEGATE_READ))
3521 return nfserr_openmode;
3522 else
3523 return nfs_ok;
3524}
3525
f459e453 3526static int share_access_to_flags(u32 share_access)
52f4fb43 3527{
f459e453 3528 return share_access == NFS4_SHARE_ACCESS_READ ? RD_STATE : WR_STATE;
52f4fb43
N
3529}
3530
38c2f4b1 3531static struct nfs4_delegation *find_deleg_stateid(struct nfs4_client *cl, stateid_t *s)
24a0111e 3532{
f459e453 3533 struct nfs4_stid *ret;
24a0111e 3534
38c2f4b1 3535 ret = find_stateid_by_type(cl, s, NFS4_DELEG_STID);
f459e453
BF
3536 if (!ret)
3537 return NULL;
3538 return delegstateid(ret);
24a0111e
BF
3539}
3540
8b289b2c
BF
3541static bool nfsd4_is_deleg_cur(struct nfsd4_open *open)
3542{
3543 return open->op_claim_type == NFS4_OPEN_CLAIM_DELEGATE_CUR ||
3544 open->op_claim_type == NFS4_OPEN_CLAIM_DELEG_CUR_FH;
3545}
3546
b37ad28b 3547static __be32
41d22663 3548nfs4_check_deleg(struct nfs4_client *cl, struct nfsd4_open *open,
567d9829
N
3549 struct nfs4_delegation **dp)
3550{
3551 int flags;
b37ad28b 3552 __be32 status = nfserr_bad_stateid;
dcd94cc2 3553 struct nfs4_delegation *deleg;
567d9829 3554
dcd94cc2
TM
3555 deleg = find_deleg_stateid(cl, &open->op_delegate_stateid);
3556 if (deleg == NULL)
c44c5eeb 3557 goto out;
24a0111e 3558 flags = share_access_to_flags(open->op_share_access);
dcd94cc2
TM
3559 status = nfs4_check_delegmode(deleg, flags);
3560 if (status) {
3561 nfs4_put_stid(&deleg->dl_stid);
3562 goto out;
3563 }
3564 *dp = deleg;
c44c5eeb 3565out:
8b289b2c 3566 if (!nfsd4_is_deleg_cur(open))
c44c5eeb
N
3567 return nfs_ok;
3568 if (status)
3569 return status;
dad1c067 3570 open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED;
c44c5eeb 3571 return nfs_ok;
567d9829
N
3572}
3573
a46cb7f2
JL
3574static struct nfs4_ol_stateid *
3575nfsd4_find_existing_open(struct nfs4_file *fp, struct nfsd4_open *open)
1da177e4 3576{
a46cb7f2 3577 struct nfs4_ol_stateid *local, *ret = NULL;
fe0750e5 3578 struct nfs4_openowner *oo = open->op_openowner;
1da177e4 3579
1d31a253 3580 spin_lock(&fp->fi_lock);
8beefa24 3581 list_for_each_entry(local, &fp->fi_stateids, st_perfile) {
1da177e4
LT
3582 /* ignore lock owners */
3583 if (local->st_stateowner->so_is_open_owner == 0)
3584 continue;
baeb4ff0 3585 if (local->st_stateowner == &oo->oo_owner) {
a46cb7f2 3586 ret = local;
d6f2bc5d 3587 atomic_inc(&ret->st_stid.sc_count);
baeb4ff0 3588 break;
1d31a253 3589 }
1da177e4 3590 }
1d31a253 3591 spin_unlock(&fp->fi_lock);
a46cb7f2 3592 return ret;
1da177e4
LT
3593}
3594
21fb4016
BF
3595static inline int nfs4_access_to_access(u32 nfs4_access)
3596{
3597 int flags = 0;
3598
3599 if (nfs4_access & NFS4_SHARE_ACCESS_READ)
3600 flags |= NFSD_MAY_READ;
3601 if (nfs4_access & NFS4_SHARE_ACCESS_WRITE)
3602 flags |= NFSD_MAY_WRITE;
3603 return flags;
3604}
3605
7e6a72e5
CH
3606static inline __be32
3607nfsd4_truncate(struct svc_rqst *rqstp, struct svc_fh *fh,
3608 struct nfsd4_open *open)
3609{
3610 struct iattr iattr = {
3611 .ia_valid = ATTR_SIZE,
3612 .ia_size = 0,
3613 };
3614 if (!open->op_truncate)
3615 return 0;
3616 if (!(open->op_share_access & NFS4_SHARE_ACCESS_WRITE))
3617 return nfserr_inval;
3618 return nfsd_setattr(rqstp, fh, &iattr, 0, (time_t)0);
3619}
3620
0c12eaff 3621static __be32 nfs4_get_vfs_file(struct svc_rqst *rqstp, struct nfs4_file *fp,
6eb3a1d0
JL
3622 struct svc_fh *cur_fh, struct nfs4_ol_stateid *stp,
3623 struct nfsd4_open *open)
f9d7562f 3624{
de18643d 3625 struct file *filp = NULL;
f9d7562f 3626 __be32 status;
0c12eaff
CB
3627 int oflag = nfs4_access_to_omode(open->op_share_access);
3628 int access = nfs4_access_to_access(open->op_share_access);
baeb4ff0 3629 unsigned char old_access_bmap, old_deny_bmap;
0c12eaff 3630
de18643d 3631 spin_lock(&fp->fi_lock);
baeb4ff0
JL
3632
3633 /*
3634 * Are we trying to set a deny mode that would conflict with
3635 * current access?
3636 */
3637 status = nfs4_file_check_deny(fp, open->op_share_deny);
3638 if (status != nfs_ok) {
3639 spin_unlock(&fp->fi_lock);
3640 goto out;
3641 }
3642
3643 /* set access to the file */
3644 status = nfs4_file_get_access(fp, open->op_share_access);
3645 if (status != nfs_ok) {
3646 spin_unlock(&fp->fi_lock);
3647 goto out;
3648 }
3649
3650 /* Set access bits in stateid */
3651 old_access_bmap = stp->st_access_bmap;
3652 set_access(open->op_share_access, stp);
3653
3654 /* Set new deny mask */
3655 old_deny_bmap = stp->st_deny_bmap;
3656 set_deny(open->op_share_deny, stp);
3657 fp->fi_share_deny |= (open->op_share_deny & NFS4_SHARE_DENY_BOTH);
3658
f9d7562f 3659 if (!fp->fi_fds[oflag]) {
de18643d
TM
3660 spin_unlock(&fp->fi_lock);
3661 status = nfsd_open(rqstp, cur_fh, S_IFREG, access, &filp);
f9d7562f 3662 if (status)
baeb4ff0 3663 goto out_put_access;
de18643d
TM
3664 spin_lock(&fp->fi_lock);
3665 if (!fp->fi_fds[oflag]) {
3666 fp->fi_fds[oflag] = filp;
3667 filp = NULL;
3668 }
f9d7562f 3669 }
de18643d
TM
3670 spin_unlock(&fp->fi_lock);
3671 if (filp)
3672 fput(filp);
f9d7562f 3673
7e6a72e5
CH
3674 status = nfsd4_truncate(rqstp, cur_fh, open);
3675 if (status)
3676 goto out_put_access;
7e6a72e5
CH
3677out:
3678 return status;
baeb4ff0
JL
3679out_put_access:
3680 stp->st_access_bmap = old_access_bmap;
3681 nfs4_file_put_access(fp, open->op_share_access);
3682 reset_union_bmap_deny(bmap_to_share_mode(old_deny_bmap), stp);
3683 goto out;
1da177e4
LT
3684}
3685
b37ad28b 3686static __be32
dcef0413 3687nfs4_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 3688{
b37ad28b 3689 __be32 status;
baeb4ff0 3690 unsigned char old_deny_bmap;
1da177e4 3691
6eb3a1d0 3692 if (!test_access(open->op_share_access, stp))
baeb4ff0 3693 return nfs4_get_vfs_file(rqstp, fp, cur_fh, stp, open);
7e6a72e5 3694
baeb4ff0
JL
3695 /* test and set deny mode */
3696 spin_lock(&fp->fi_lock);
3697 status = nfs4_file_check_deny(fp, open->op_share_deny);
3698 if (status == nfs_ok) {
3699 old_deny_bmap = stp->st_deny_bmap;
3700 set_deny(open->op_share_deny, stp);
3701 fp->fi_share_deny |=
3702 (open->op_share_deny & NFS4_SHARE_DENY_BOTH);
3703 }
3704 spin_unlock(&fp->fi_lock);
3705
3706 if (status != nfs_ok)
1da177e4 3707 return status;
1da177e4 3708
baeb4ff0
JL
3709 status = nfsd4_truncate(rqstp, cur_fh, open);
3710 if (status != nfs_ok)
3711 reset_union_bmap_deny(old_deny_bmap, stp);
3712 return status;
3713}
1da177e4 3714
1da177e4 3715static void
1255a8f3 3716nfs4_set_claim_prev(struct nfsd4_open *open, bool has_session)
1da177e4 3717{
dad1c067 3718 open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED;
1da177e4
LT
3719}
3720
14a24e99
BF
3721/* Should we give out recallable state?: */
3722static bool nfsd4_cb_channel_good(struct nfs4_client *clp)
3723{
3724 if (clp->cl_cb_state == NFSD4_CB_UP)
3725 return true;
3726 /*
3727 * In the sessions case, since we don't have to establish a
3728 * separate connection for callbacks, we assume it's OK
3729 * until we hear otherwise:
3730 */
3731 return clp->cl_minorversion && clp->cl_cb_state == NFSD4_CB_UNKNOWN;
3732}
3733
d564fbec 3734static struct file_lock *nfs4_alloc_init_lease(struct nfs4_file *fp, int flag)
22d38c4c
BF
3735{
3736 struct file_lock *fl;
3737
3738 fl = locks_alloc_lock();
3739 if (!fl)
3740 return NULL;
3741 locks_init_lock(fl);
3742 fl->fl_lmops = &nfsd_lease_mng_ops;
617588d5 3743 fl->fl_flags = FL_DELEG;
22d38c4c
BF
3744 fl->fl_type = flag == NFS4_OPEN_DELEGATE_READ? F_RDLCK: F_WRLCK;
3745 fl->fl_end = OFFSET_MAX;
d564fbec 3746 fl->fl_owner = (fl_owner_t)fp;
22d38c4c 3747 fl->fl_pid = current->tgid;
22d38c4c
BF
3748 return fl;
3749}
3750
99c41515 3751static int nfs4_setlease(struct nfs4_delegation *dp)
edab9782 3752{
11b9164a 3753 struct nfs4_file *fp = dp->dl_stid.sc_file;
edab9782 3754 struct file_lock *fl;
417c6629
JL
3755 struct file *filp;
3756 int status = 0;
edab9782 3757
d564fbec 3758 fl = nfs4_alloc_init_lease(fp, NFS4_OPEN_DELEGATE_READ);
edab9782
BF
3759 if (!fl)
3760 return -ENOMEM;
417c6629
JL
3761 filp = find_readable_file(fp);
3762 if (!filp) {
3763 /* We should always have a readable file here */
3764 WARN_ON_ONCE(1);
3765 return -EBADF;
3766 }
3767 fl->fl_file = filp;
3768 status = vfs_setlease(filp, fl->fl_type, &fl);
3769 if (status) {
3770 locks_free_lock(fl);
3771 goto out_fput;
3772 }
3773 spin_lock(&state_lock);
3774 spin_lock(&fp->fi_lock);
3775 /* Did the lease get broken before we took the lock? */
3776 status = -EAGAIN;
3777 if (fp->fi_had_conflict)
3778 goto out_unlock;
3779 /* Race breaker */
3780 if (fp->fi_lease) {
3781 status = 0;
3782 atomic_inc(&fp->fi_delegees);
3783 hash_delegation_locked(dp, fp);
3784 goto out_unlock;
3785 }
acfdf5c3 3786 fp->fi_lease = fl;
417c6629 3787 fp->fi_deleg_file = filp;
acfdf5c3 3788 atomic_set(&fp->fi_delegees, 1);
931ee56c 3789 hash_delegation_locked(dp, fp);
417c6629 3790 spin_unlock(&fp->fi_lock);
cdc97505 3791 spin_unlock(&state_lock);
acfdf5c3 3792 return 0;
417c6629
JL
3793out_unlock:
3794 spin_unlock(&fp->fi_lock);
3795 spin_unlock(&state_lock);
3796out_fput:
3797 fput(filp);
e873088f 3798 return status;
acfdf5c3
BF
3799}
3800
0b26693c
JL
3801static struct nfs4_delegation *
3802nfs4_set_delegation(struct nfs4_client *clp, struct svc_fh *fh,
3803 struct nfs4_file *fp)
acfdf5c3 3804{
0b26693c
JL
3805 int status;
3806 struct nfs4_delegation *dp;
417c6629 3807
bf7bd3e9 3808 if (fp->fi_had_conflict)
0b26693c
JL
3809 return ERR_PTR(-EAGAIN);
3810
3811 dp = alloc_init_deleg(clp, fh);
3812 if (!dp)
3813 return ERR_PTR(-ENOMEM);
3814
bf7bd3e9 3815 get_nfs4_file(fp);
417c6629
JL
3816 spin_lock(&state_lock);
3817 spin_lock(&fp->fi_lock);
11b9164a 3818 dp->dl_stid.sc_file = fp;
417c6629
JL
3819 if (!fp->fi_lease) {
3820 spin_unlock(&fp->fi_lock);
3821 spin_unlock(&state_lock);
0b26693c
JL
3822 status = nfs4_setlease(dp);
3823 goto out;
417c6629 3824 }
cbf7a75b 3825 atomic_inc(&fp->fi_delegees);
acfdf5c3 3826 if (fp->fi_had_conflict) {
417c6629
JL
3827 status = -EAGAIN;
3828 goto out_unlock;
acfdf5c3 3829 }
931ee56c 3830 hash_delegation_locked(dp, fp);
0b26693c 3831 status = 0;
417c6629
JL
3832out_unlock:
3833 spin_unlock(&fp->fi_lock);
cdc97505 3834 spin_unlock(&state_lock);
0b26693c
JL
3835out:
3836 if (status) {
6011695d 3837 nfs4_put_stid(&dp->dl_stid);
0b26693c
JL
3838 return ERR_PTR(status);
3839 }
3840 return dp;
edab9782
BF
3841}
3842
4aa8913c
BH
3843static void nfsd4_open_deleg_none_ext(struct nfsd4_open *open, int status)
3844{
3845 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
3846 if (status == -EAGAIN)
3847 open->op_why_no_deleg = WND4_CONTENTION;
3848 else {
3849 open->op_why_no_deleg = WND4_RESOURCE;
3850 switch (open->op_deleg_want) {
3851 case NFS4_SHARE_WANT_READ_DELEG:
3852 case NFS4_SHARE_WANT_WRITE_DELEG:
3853 case NFS4_SHARE_WANT_ANY_DELEG:
3854 break;
3855 case NFS4_SHARE_WANT_CANCEL:
3856 open->op_why_no_deleg = WND4_CANCELLED;
3857 break;
3858 case NFS4_SHARE_WANT_NO_DELEG:
063b0fb9 3859 WARN_ON_ONCE(1);
4aa8913c
BH
3860 }
3861 }
3862}
3863
1da177e4
LT
3864/*
3865 * Attempt to hand out a delegation.
99c41515
BF
3866 *
3867 * Note we don't support write delegations, and won't until the vfs has
3868 * proper support for them.
1da177e4
LT
3869 */
3870static void
4cf59221
JL
3871nfs4_open_delegation(struct svc_fh *fh, struct nfsd4_open *open,
3872 struct nfs4_ol_stateid *stp)
1da177e4
LT
3873{
3874 struct nfs4_delegation *dp;
4cf59221
JL
3875 struct nfs4_openowner *oo = openowner(stp->st_stateowner);
3876 struct nfs4_client *clp = stp->st_stid.sc_client;
14a24e99 3877 int cb_up;
99c41515 3878 int status = 0;
1da177e4 3879
fe0750e5 3880 cb_up = nfsd4_cb_channel_good(oo->oo_owner.so_client);
7b190fec
N
3881 open->op_recall = 0;
3882 switch (open->op_claim_type) {
3883 case NFS4_OPEN_CLAIM_PREVIOUS:
2bf23875 3884 if (!cb_up)
7b190fec 3885 open->op_recall = 1;
99c41515
BF
3886 if (open->op_delegate_type != NFS4_OPEN_DELEGATE_READ)
3887 goto out_no_deleg;
7b190fec
N
3888 break;
3889 case NFS4_OPEN_CLAIM_NULL:
ed47b062 3890 case NFS4_OPEN_CLAIM_FH:
99c41515
BF
3891 /*
3892 * Let's not give out any delegations till everyone's
3893 * had the chance to reclaim theirs....
3894 */
4cf59221 3895 if (locks_in_grace(clp->net))
99c41515 3896 goto out_no_deleg;
dad1c067 3897 if (!cb_up || !(oo->oo_flags & NFS4_OO_CONFIRMED))
99c41515 3898 goto out_no_deleg;
9a0590ae
SD
3899 /*
3900 * Also, if the file was opened for write or
3901 * create, there's a good chance the client's
3902 * about to write to it, resulting in an
3903 * immediate recall (since we don't support
3904 * write delegations):
3905 */
7b190fec 3906 if (open->op_share_access & NFS4_SHARE_ACCESS_WRITE)
99c41515
BF
3907 goto out_no_deleg;
3908 if (open->op_create == NFS4_OPEN_CREATE)
3909 goto out_no_deleg;
7b190fec
N
3910 break;
3911 default:
99c41515 3912 goto out_no_deleg;
7b190fec 3913 }
11b9164a 3914 dp = nfs4_set_delegation(clp, fh, stp->st_stid.sc_file);
0b26693c 3915 if (IS_ERR(dp))
dd239cc0 3916 goto out_no_deleg;
1da177e4 3917
d5477a8d 3918 memcpy(&open->op_delegate_stateid, &dp->dl_stid.sc_stateid, sizeof(dp->dl_stid.sc_stateid));
1da177e4 3919
8c10cbdb 3920 dprintk("NFSD: delegation stateid=" STATEID_FMT "\n",
d5477a8d 3921 STATEID_VAL(&dp->dl_stid.sc_stateid));
99c41515 3922 open->op_delegate_type = NFS4_OPEN_DELEGATE_READ;
67cb1279 3923 nfs4_put_stid(&dp->dl_stid);
dd239cc0 3924 return;
dd239cc0 3925out_no_deleg:
99c41515
BF
3926 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE;
3927 if (open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS &&
d08d32e6 3928 open->op_delegate_type != NFS4_OPEN_DELEGATE_NONE) {
99c41515 3929 dprintk("NFSD: WARNING: refusing delegation reclaim\n");
d08d32e6
BF
3930 open->op_recall = 1;
3931 }
99c41515
BF
3932
3933 /* 4.1 client asking for a delegation? */
3934 if (open->op_deleg_want)
3935 nfsd4_open_deleg_none_ext(open, status);
3936 return;
1da177e4
LT
3937}
3938
e27f49c3
BH
3939static void nfsd4_deleg_xgrade_none_ext(struct nfsd4_open *open,
3940 struct nfs4_delegation *dp)
3941{
3942 if (open->op_deleg_want == NFS4_SHARE_WANT_READ_DELEG &&
3943 dp->dl_type == NFS4_OPEN_DELEGATE_WRITE) {
3944 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
3945 open->op_why_no_deleg = WND4_NOT_SUPP_DOWNGRADE;
3946 } else if (open->op_deleg_want == NFS4_SHARE_WANT_WRITE_DELEG &&
3947 dp->dl_type == NFS4_OPEN_DELEGATE_WRITE) {
3948 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
3949 open->op_why_no_deleg = WND4_NOT_SUPP_UPGRADE;
3950 }
3951 /* Otherwise the client must be confused wanting a delegation
3952 * it already has, therefore we don't return
3953 * NFS4_OPEN_DELEGATE_NONE_EXT and reason.
3954 */
3955}
3956
1da177e4
LT
3957/*
3958 * called with nfs4_lock_state() held.
3959 */
b37ad28b 3960__be32
1da177e4
LT
3961nfsd4_process_open2(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_open *open)
3962{
6668958f 3963 struct nfsd4_compoundres *resp = rqstp->rq_resp;
38c2f4b1 3964 struct nfs4_client *cl = open->op_openowner->oo_owner.so_client;
1da177e4 3965 struct nfs4_file *fp = NULL;
dcef0413 3966 struct nfs4_ol_stateid *stp = NULL;
567d9829 3967 struct nfs4_delegation *dp = NULL;
b37ad28b 3968 __be32 status;
1da177e4 3969
1da177e4
LT
3970 /*
3971 * Lookup file; if found, lookup stateid and check open request,
3972 * and check for delegations in the process of being recalled.
3973 * If not found, create the nfs4_file struct
3974 */
f9c00c3a 3975 fp = find_or_add_file(open->op_file, &current_fh->fh_handle);
950e0118 3976 if (fp != open->op_file) {
41d22663 3977 status = nfs4_check_deleg(cl, open, &dp);
c44c5eeb
N
3978 if (status)
3979 goto out;
a46cb7f2 3980 stp = nfsd4_find_existing_open(fp, open);
1da177e4 3981 } else {
950e0118 3982 open->op_file = NULL;
c44c5eeb 3983 status = nfserr_bad_stateid;
8b289b2c 3984 if (nfsd4_is_deleg_cur(open))
c44c5eeb 3985 goto out;
3e772463 3986 status = nfserr_jukebox;
1da177e4
LT
3987 }
3988
3989 /*
3990 * OPEN the file, or upgrade an existing OPEN.
3991 * If truncate fails, the OPEN fails.
3992 */
3993 if (stp) {
3994 /* Stateid was found, this is an OPEN upgrade */
f9d7562f 3995 status = nfs4_upgrade_open(rqstp, fp, current_fh, stp, open);
1da177e4
LT
3996 if (status)
3997 goto out;
3998 } else {
4cdc951b
BF
3999 stp = open->op_stp;
4000 open->op_stp = NULL;
996e0938 4001 init_open_stateid(stp, fp, open);
6eb3a1d0
JL
4002 status = nfs4_get_vfs_file(rqstp, fp, current_fh, stp, open);
4003 if (status) {
4004 release_open_stateid(stp);
4005 goto out;
4006 }
1da177e4 4007 }
dcef0413
BF
4008 update_stateid(&stp->st_stid.sc_stateid);
4009 memcpy(&open->op_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
1da177e4 4010
d24433cd 4011 if (nfsd4_has_session(&resp->cstate)) {
d24433cd
BH
4012 if (open->op_deleg_want & NFS4_SHARE_WANT_NO_DELEG) {
4013 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
4014 open->op_why_no_deleg = WND4_NOT_WANTED;
4015 goto nodeleg;
4016 }
4017 }
4018
1da177e4
LT
4019 /*
4020 * Attempt to hand out a delegation. No error return, because the
4021 * OPEN succeeds even if we fail.
4022 */
4cf59221 4023 nfs4_open_delegation(current_fh, open, stp);
d24433cd 4024nodeleg:
1da177e4
LT
4025 status = nfs_ok;
4026
8c10cbdb 4027 dprintk("%s: stateid=" STATEID_FMT "\n", __func__,
dcef0413 4028 STATEID_VAL(&stp->st_stid.sc_stateid));
1da177e4 4029out:
d24433cd
BH
4030 /* 4.1 client trying to upgrade/downgrade delegation? */
4031 if (open->op_delegate_type == NFS4_OPEN_DELEGATE_NONE && dp &&
e27f49c3
BH
4032 open->op_deleg_want)
4033 nfsd4_deleg_xgrade_none_ext(open, dp);
d24433cd 4034
13cd2184
N
4035 if (fp)
4036 put_nfs4_file(fp);
37515177 4037 if (status == 0 && open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS)
1255a8f3 4038 nfs4_set_claim_prev(open, nfsd4_has_session(&resp->cstate));
1da177e4
LT
4039 /*
4040 * To finish the open response, we just need to set the rflags.
4041 */
4042 open->op_rflags = NFS4_OPEN_RESULT_LOCKTYPE_POSIX;
dad1c067 4043 if (!(open->op_openowner->oo_flags & NFS4_OO_CONFIRMED) &&
6668958f 4044 !nfsd4_has_session(&resp->cstate))
1da177e4 4045 open->op_rflags |= NFS4_OPEN_RESULT_CONFIRM;
dcd94cc2
TM
4046 if (dp)
4047 nfs4_put_stid(&dp->dl_stid);
d6f2bc5d
TM
4048 if (stp)
4049 nfs4_put_stid(&stp->st_stid);
1da177e4
LT
4050
4051 return status;
4052}
4053
58fb12e6
JL
4054void nfsd4_cleanup_open_state(struct nfsd4_compound_state *cstate,
4055 struct nfsd4_open *open, __be32 status)
d29b20cd
BF
4056{
4057 if (open->op_openowner) {
d3134b10
JL
4058 struct nfs4_stateowner *so = &open->op_openowner->oo_owner;
4059
4060 nfsd4_cstate_assign_replay(cstate, so);
4061 nfs4_put_stateowner(so);
d29b20cd 4062 }
32513b40
BF
4063 if (open->op_file)
4064 nfsd4_free_file(open->op_file);
4cdc951b 4065 if (open->op_stp)
6011695d 4066 nfs4_put_stid(&open->op_stp->st_stid);
d29b20cd
BF
4067}
4068
b37ad28b 4069__be32
b591480b
BF
4070nfsd4_renew(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4071 clientid_t *clid)
1da177e4
LT
4072{
4073 struct nfs4_client *clp;
b37ad28b 4074 __be32 status;
7f2210fa 4075 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
1da177e4
LT
4076
4077 nfs4_lock_state();
4078 dprintk("process_renew(%08x/%08x): starting\n",
4079 clid->cl_boot, clid->cl_id);
4b24ca7d 4080 status = lookup_clientid(clid, cstate, nn);
9b2ef62b 4081 if (status)
1da177e4 4082 goto out;
4b24ca7d 4083 clp = cstate->clp;
1da177e4 4084 status = nfserr_cb_path_down;
ea1da636 4085 if (!list_empty(&clp->cl_delegations)
77a3569d 4086 && clp->cl_cb_state != NFSD4_CB_UP)
1da177e4
LT
4087 goto out;
4088 status = nfs_ok;
4089out:
4090 nfs4_unlock_state();
4091 return status;
4092}
4093
a76b4319 4094static void
12760c66 4095nfsd4_end_grace(struct nfsd_net *nn)
a76b4319 4096{
33dcc481 4097 /* do nothing if grace period already ended */
a51c84ed 4098 if (nn->grace_ended)
33dcc481
JL
4099 return;
4100
a76b4319 4101 dprintk("NFSD: end of grace period\n");
a51c84ed 4102 nn->grace_ended = true;
12760c66 4103 nfsd4_record_grace_done(nn, nn->boot_time);
5e1533c7 4104 locks_end_grace(&nn->nfsd4_manager);
e46b498c
BF
4105 /*
4106 * Now that every NFSv4 client has had the chance to recover and
4107 * to see the (possibly new, possibly shorter) lease time, we
4108 * can safely set the next grace time to the current lease time:
4109 */
5284b44e 4110 nn->nfsd4_grace = nn->nfsd4_lease;
a76b4319
N
4111}
4112
fd39ca9a 4113static time_t
09121281 4114nfs4_laundromat(struct nfsd_net *nn)
1da177e4
LT
4115{
4116 struct nfs4_client *clp;
fe0750e5 4117 struct nfs4_openowner *oo;
1da177e4
LT
4118 struct nfs4_delegation *dp;
4119 struct list_head *pos, *next, reaplist;
3d733711 4120 time_t cutoff = get_seconds() - nn->nfsd4_lease;
a832e7ae 4121 time_t t, new_timeo = nn->nfsd4_lease;
1da177e4
LT
4122
4123 nfs4_lock_state();
4124
4125 dprintk("NFSD: laundromat service - starting\n");
12760c66 4126 nfsd4_end_grace(nn);
36acb66b 4127 INIT_LIST_HEAD(&reaplist);
c9a49628 4128 spin_lock(&nn->client_lock);
5ed58bb2 4129 list_for_each_safe(pos, next, &nn->client_lru) {
1da177e4
LT
4130 clp = list_entry(pos, struct nfs4_client, cl_lru);
4131 if (time_after((unsigned long)clp->cl_time, (unsigned long)cutoff)) {
4132 t = clp->cl_time - cutoff;
a832e7ae 4133 new_timeo = min(new_timeo, t);
1da177e4
LT
4134 break;
4135 }
221a6876 4136 if (mark_client_expired_locked(clp)) {
d7682988
BH
4137 dprintk("NFSD: client in use (clientid %08x)\n",
4138 clp->cl_clientid.cl_id);
4139 continue;
4140 }
4864af97
TM
4141 unhash_client_locked(clp);
4142 list_add(&clp->cl_lru, &reaplist);
36acb66b 4143 }
c9a49628 4144 spin_unlock(&nn->client_lock);
36acb66b
BH
4145 list_for_each_safe(pos, next, &reaplist) {
4146 clp = list_entry(pos, struct nfs4_client, cl_lru);
1da177e4
LT
4147 dprintk("NFSD: purging unused client (clientid %08x)\n",
4148 clp->cl_clientid.cl_id);
4864af97 4149 list_del_init(&clp->cl_lru);
1da177e4
LT
4150 expire_client(clp);
4151 }
cdc97505 4152 spin_lock(&state_lock);
e8c69d17 4153 list_for_each_safe(pos, next, &nn->del_recall_lru) {
1da177e4 4154 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
4e37a7c2
SK
4155 if (net_generic(dp->dl_stid.sc_client->net, nfsd_net_id) != nn)
4156 continue;
1da177e4 4157 if (time_after((unsigned long)dp->dl_time, (unsigned long)cutoff)) {
a832e7ae
JL
4158 t = dp->dl_time - cutoff;
4159 new_timeo = min(new_timeo, t);
1da177e4
LT
4160 break;
4161 }
42690676
JL
4162 unhash_delegation_locked(dp);
4163 list_add(&dp->dl_recall_lru, &reaplist);
1da177e4 4164 }
cdc97505 4165 spin_unlock(&state_lock);
2d4a532d
JL
4166 while (!list_empty(&reaplist)) {
4167 dp = list_first_entry(&reaplist, struct nfs4_delegation,
4168 dl_recall_lru);
4169 list_del_init(&dp->dl_recall_lru);
3bd64a5b 4170 revoke_delegation(dp);
1da177e4 4171 }
73758fed 4172 list_for_each_safe(pos, next, &nn->close_lru) {
fe0750e5
BF
4173 oo = container_of(pos, struct nfs4_openowner, oo_close_lru);
4174 if (time_after((unsigned long)oo->oo_time, (unsigned long)cutoff)) {
a832e7ae
JL
4175 t = oo->oo_time - cutoff;
4176 new_timeo = min(new_timeo, t);
1da177e4
LT
4177 break;
4178 }
d3134b10 4179 release_last_closed_stateid(oo);
1da177e4 4180 }
a832e7ae 4181 new_timeo = max_t(time_t, new_timeo, NFSD_LAUNDROMAT_MINTIMEOUT);
1da177e4 4182 nfs4_unlock_state();
a832e7ae 4183 return new_timeo;
1da177e4
LT
4184}
4185
a254b246
HH
4186static struct workqueue_struct *laundry_wq;
4187static void laundromat_main(struct work_struct *);
a254b246
HH
4188
4189static void
09121281 4190laundromat_main(struct work_struct *laundry)
1da177e4
LT
4191{
4192 time_t t;
09121281
SK
4193 struct delayed_work *dwork = container_of(laundry, struct delayed_work,
4194 work);
4195 struct nfsd_net *nn = container_of(dwork, struct nfsd_net,
4196 laundromat_work);
1da177e4 4197
09121281 4198 t = nfs4_laundromat(nn);
1da177e4 4199 dprintk("NFSD: laundromat_main - sleeping for %ld seconds\n", t);
09121281 4200 queue_delayed_work(laundry_wq, &nn->laundromat_work, t*HZ);
1da177e4
LT
4201}
4202
f7a4d872 4203static inline __be32 nfs4_check_fh(struct svc_fh *fhp, struct nfs4_ol_stateid *stp)
1da177e4 4204{
11b9164a 4205 if (!nfsd_fh_match(&fhp->fh_handle, &stp->st_stid.sc_file->fi_fhandle))
f7a4d872
BF
4206 return nfserr_bad_stateid;
4207 return nfs_ok;
1da177e4
LT
4208}
4209
1da177e4 4210static inline int
82c5ff1b 4211access_permit_read(struct nfs4_ol_stateid *stp)
1da177e4 4212{
82c5ff1b
JL
4213 return test_access(NFS4_SHARE_ACCESS_READ, stp) ||
4214 test_access(NFS4_SHARE_ACCESS_BOTH, stp) ||
4215 test_access(NFS4_SHARE_ACCESS_WRITE, stp);
1da177e4
LT
4216}
4217
4218static inline int
82c5ff1b 4219access_permit_write(struct nfs4_ol_stateid *stp)
1da177e4 4220{
82c5ff1b
JL
4221 return test_access(NFS4_SHARE_ACCESS_WRITE, stp) ||
4222 test_access(NFS4_SHARE_ACCESS_BOTH, stp);
1da177e4
LT
4223}
4224
4225static
dcef0413 4226__be32 nfs4_check_openmode(struct nfs4_ol_stateid *stp, int flags)
1da177e4 4227{
b37ad28b 4228 __be32 status = nfserr_openmode;
1da177e4 4229
02921914
BF
4230 /* For lock stateid's, we test the parent open, not the lock: */
4231 if (stp->st_openstp)
4232 stp = stp->st_openstp;
82c5ff1b 4233 if ((flags & WR_STATE) && !access_permit_write(stp))
1da177e4 4234 goto out;
82c5ff1b 4235 if ((flags & RD_STATE) && !access_permit_read(stp))
1da177e4
LT
4236 goto out;
4237 status = nfs_ok;
4238out:
4239 return status;
4240}
4241
b37ad28b 4242static inline __be32
5ccb0066 4243check_special_stateids(struct net *net, svc_fh *current_fh, stateid_t *stateid, int flags)
1da177e4 4244{
203a8c8e 4245 if (ONE_STATEID(stateid) && (flags & RD_STATE))
1da177e4 4246 return nfs_ok;
5ccb0066 4247 else if (locks_in_grace(net)) {
25985edc 4248 /* Answer in remaining cases depends on existence of
1da177e4
LT
4249 * conflicting state; so we must wait out the grace period. */
4250 return nfserr_grace;
4251 } else if (flags & WR_STATE)
4252 return nfs4_share_conflict(current_fh,
4253 NFS4_SHARE_DENY_WRITE);
4254 else /* (flags & RD_STATE) && ZERO_STATEID(stateid) */
4255 return nfs4_share_conflict(current_fh,
4256 NFS4_SHARE_DENY_READ);
4257}
4258
4259/*
4260 * Allow READ/WRITE during grace period on recovered state only for files
4261 * that are not able to provide mandatory locking.
4262 */
4263static inline int
5ccb0066 4264grace_disallows_io(struct net *net, struct inode *inode)
1da177e4 4265{
5ccb0066 4266 return locks_in_grace(net) && mandatory_lock(inode);
1da177e4
LT
4267}
4268
81b82965
BF
4269/* Returns true iff a is later than b: */
4270static bool stateid_generation_after(stateid_t *a, stateid_t *b)
4271{
1a9357f4 4272 return (s32)(a->si_generation - b->si_generation) > 0;
81b82965
BF
4273}
4274
57b7b43b 4275static __be32 check_stateid_generation(stateid_t *in, stateid_t *ref, bool has_session)
0836f587 4276{
6668958f
AA
4277 /*
4278 * When sessions are used the stateid generation number is ignored
4279 * when it is zero.
4280 */
28dde241 4281 if (has_session && in->si_generation == 0)
81b82965
BF
4282 return nfs_ok;
4283
4284 if (in->si_generation == ref->si_generation)
4285 return nfs_ok;
6668958f 4286
0836f587 4287 /* If the client sends us a stateid from the future, it's buggy: */
81b82965 4288 if (stateid_generation_after(in, ref))
0836f587
BF
4289 return nfserr_bad_stateid;
4290 /*
81b82965
BF
4291 * However, we could see a stateid from the past, even from a
4292 * non-buggy client. For example, if the client sends a lock
4293 * while some IO is outstanding, the lock may bump si_generation
4294 * while the IO is still in flight. The client could avoid that
4295 * situation by waiting for responses on all the IO requests,
4296 * but better performance may result in retrying IO that
4297 * receives an old_stateid error if requests are rarely
4298 * reordered in flight:
0836f587 4299 */
81b82965 4300 return nfserr_old_stateid;
0836f587
BF
4301}
4302
7df302f7 4303static __be32 nfsd4_validate_stateid(struct nfs4_client *cl, stateid_t *stateid)
17456804 4304{
97b7e3b6
BF
4305 struct nfs4_stid *s;
4306 struct nfs4_ol_stateid *ols;
1af71cc8 4307 __be32 status = nfserr_bad_stateid;
17456804 4308
7df302f7 4309 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid))
1af71cc8 4310 return status;
7df302f7
CL
4311 /* Client debugging aid. */
4312 if (!same_clid(&stateid->si_opaque.so_clid, &cl->cl_clientid)) {
4313 char addr_str[INET6_ADDRSTRLEN];
4314 rpc_ntop((struct sockaddr *)&cl->cl_addr, addr_str,
4315 sizeof(addr_str));
4316 pr_warn_ratelimited("NFSD: client %s testing state ID "
4317 "with incorrect client ID\n", addr_str);
1af71cc8 4318 return status;
7df302f7 4319 }
1af71cc8
JL
4320 spin_lock(&cl->cl_lock);
4321 s = find_stateid_locked(cl, stateid);
97b7e3b6 4322 if (!s)
1af71cc8 4323 goto out_unlock;
36279ac1 4324 status = check_stateid_generation(stateid, &s->sc_stateid, 1);
17456804 4325 if (status)
1af71cc8 4326 goto out_unlock;
23340032
BF
4327 switch (s->sc_type) {
4328 case NFS4_DELEG_STID:
1af71cc8
JL
4329 status = nfs_ok;
4330 break;
3bd64a5b 4331 case NFS4_REVOKED_DELEG_STID:
1af71cc8
JL
4332 status = nfserr_deleg_revoked;
4333 break;
23340032
BF
4334 case NFS4_OPEN_STID:
4335 case NFS4_LOCK_STID:
4336 ols = openlockstateid(s);
4337 if (ols->st_stateowner->so_is_open_owner
4338 && !(openowner(ols->st_stateowner)->oo_flags
4339 & NFS4_OO_CONFIRMED))
1af71cc8
JL
4340 status = nfserr_bad_stateid;
4341 else
4342 status = nfs_ok;
4343 break;
23340032
BF
4344 default:
4345 printk("unknown stateid type %x\n", s->sc_type);
b0fc29d6 4346 /* Fallthrough */
23340032 4347 case NFS4_CLOSED_STID:
b0fc29d6 4348 case NFS4_CLOSED_DELEG_STID:
1af71cc8 4349 status = nfserr_bad_stateid;
23340032 4350 }
1af71cc8
JL
4351out_unlock:
4352 spin_unlock(&cl->cl_lock);
4353 return status;
17456804
BS
4354}
4355
2dd6e458
TM
4356static __be32
4357nfsd4_lookup_stateid(struct nfsd4_compound_state *cstate,
4358 stateid_t *stateid, unsigned char typemask,
4359 struct nfs4_stid **s, struct nfsd_net *nn)
38c2f4b1 4360{
0eb6f20a 4361 __be32 status;
38c2f4b1
BF
4362
4363 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid))
4364 return nfserr_bad_stateid;
4b24ca7d 4365 status = lookup_clientid(&stateid->si_opaque.so_clid, cstate, nn);
a8a7c677 4366 if (status == nfserr_stale_clientid) {
4b24ca7d 4367 if (cstate->session)
a8a7c677 4368 return nfserr_bad_stateid;
38c2f4b1 4369 return nfserr_stale_stateid;
a8a7c677 4370 }
0eb6f20a
BF
4371 if (status)
4372 return status;
4b24ca7d 4373 *s = find_stateid_by_type(cstate->clp, stateid, typemask);
38c2f4b1
BF
4374 if (!*s)
4375 return nfserr_bad_stateid;
4376 return nfs_ok;
38c2f4b1
BF
4377}
4378
1da177e4
LT
4379/*
4380* Checks for stateid operations
4381*/
b37ad28b 4382__be32
5ccb0066 4383nfs4_preprocess_stateid_op(struct net *net, struct nfsd4_compound_state *cstate,
dd453dfd 4384 stateid_t *stateid, int flags, struct file **filpp)
1da177e4 4385{
69064a27 4386 struct nfs4_stid *s;
dcef0413 4387 struct nfs4_ol_stateid *stp = NULL;
1da177e4 4388 struct nfs4_delegation *dp = NULL;
dd453dfd 4389 struct svc_fh *current_fh = &cstate->current_fh;
1da177e4 4390 struct inode *ino = current_fh->fh_dentry->d_inode;
3320fef1 4391 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
14bcab1a 4392 struct file *file = NULL;
b37ad28b 4393 __be32 status;
1da177e4 4394
1da177e4
LT
4395 if (filpp)
4396 *filpp = NULL;
4397
5ccb0066 4398 if (grace_disallows_io(net, ino))
1da177e4
LT
4399 return nfserr_grace;
4400
4401 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid))
5ccb0066 4402 return check_special_stateids(net, current_fh, stateid, flags);
1da177e4 4403
14bcab1a
TM
4404 nfs4_lock_state();
4405
2dd6e458 4406 status = nfsd4_lookup_stateid(cstate, stateid,
db24b3b4 4407 NFS4_DELEG_STID|NFS4_OPEN_STID|NFS4_LOCK_STID,
2dd6e458 4408 &s, nn);
38c2f4b1 4409 if (status)
fd911011 4410 goto unlock_state;
69064a27
BF
4411 status = check_stateid_generation(stateid, &s->sc_stateid, nfsd4_has_session(cstate));
4412 if (status)
4413 goto out;
f7a4d872
BF
4414 switch (s->sc_type) {
4415 case NFS4_DELEG_STID:
69064a27 4416 dp = delegstateid(s);
dc9bf700
BF
4417 status = nfs4_check_delegmode(dp, flags);
4418 if (status)
4419 goto out;
43b0178e 4420 if (filpp) {
11b9164a 4421 file = dp->dl_stid.sc_file->fi_deleg_file;
14bcab1a 4422 if (!file) {
063b0fb9
BF
4423 WARN_ON_ONCE(1);
4424 status = nfserr_serverfault;
4425 goto out;
4426 }
de18643d 4427 get_file(file);
43b0178e 4428 }
f7a4d872
BF
4429 break;
4430 case NFS4_OPEN_STID:
4431 case NFS4_LOCK_STID:
69064a27 4432 stp = openlockstateid(s);
f7a4d872
BF
4433 status = nfs4_check_fh(current_fh, stp);
4434 if (status)
1da177e4 4435 goto out;
fe0750e5 4436 if (stp->st_stateowner->so_is_open_owner
dad1c067 4437 && !(openowner(stp->st_stateowner)->oo_flags & NFS4_OO_CONFIRMED))
1da177e4 4438 goto out;
a4455be0
BF
4439 status = nfs4_check_openmode(stp, flags);
4440 if (status)
1da177e4 4441 goto out;
f9d7562f 4442 if (filpp) {
11b9164a
TM
4443 struct nfs4_file *fp = stp->st_stid.sc_file;
4444
f9d7562f 4445 if (flags & RD_STATE)
11b9164a 4446 file = find_readable_file(fp);
f9d7562f 4447 else
11b9164a 4448 file = find_writeable_file(fp);
f9d7562f 4449 }
f7a4d872
BF
4450 break;
4451 default:
14bcab1a
TM
4452 status = nfserr_bad_stateid;
4453 goto out;
1da177e4
LT
4454 }
4455 status = nfs_ok;
14bcab1a 4456 if (file)
de18643d 4457 *filpp = file;
1da177e4 4458out:
fd911011
TM
4459 nfs4_put_stid(s);
4460unlock_state:
14bcab1a 4461 nfs4_unlock_state();
1da177e4
LT
4462 return status;
4463}
4464
17456804
BS
4465/*
4466 * Test if the stateid is valid
4467 */
4468__be32
4469nfsd4_test_stateid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4470 struct nfsd4_test_stateid *test_stateid)
4471{
03cfb420
BS
4472 struct nfsd4_test_stateid_id *stateid;
4473 struct nfs4_client *cl = cstate->session->se_client;
4474
4475 nfs4_lock_state();
4476 list_for_each_entry(stateid, &test_stateid->ts_stateid_list, ts_id_list)
7df302f7
CL
4477 stateid->ts_id_status =
4478 nfsd4_validate_stateid(cl, &stateid->ts_id_stateid);
03cfb420
BS
4479 nfs4_unlock_state();
4480
17456804
BS
4481 return nfs_ok;
4482}
4483
e1ca12df
BS
4484__be32
4485nfsd4_free_stateid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4486 struct nfsd4_free_stateid *free_stateid)
4487{
4488 stateid_t *stateid = &free_stateid->fr_stateid;
2da1cec7 4489 struct nfs4_stid *s;
3bd64a5b 4490 struct nfs4_delegation *dp;
fc5a96c3 4491 struct nfs4_ol_stateid *stp;
38c2f4b1 4492 struct nfs4_client *cl = cstate->session->se_client;
2da1cec7 4493 __be32 ret = nfserr_bad_stateid;
e1ca12df
BS
4494
4495 nfs4_lock_state();
1af71cc8
JL
4496 spin_lock(&cl->cl_lock);
4497 s = find_stateid_locked(cl, stateid);
2da1cec7 4498 if (!s)
1af71cc8 4499 goto out_unlock;
2da1cec7
BF
4500 switch (s->sc_type) {
4501 case NFS4_DELEG_STID:
e1ca12df 4502 ret = nfserr_locks_held;
1af71cc8 4503 break;
2da1cec7 4504 case NFS4_OPEN_STID:
2da1cec7
BF
4505 ret = check_stateid_generation(stateid, &s->sc_stateid, 1);
4506 if (ret)
1af71cc8
JL
4507 break;
4508 ret = nfserr_locks_held;
f7a4d872 4509 break;
1af71cc8
JL
4510 case NFS4_LOCK_STID:
4511 ret = check_stateid_generation(stateid, &s->sc_stateid, 1);
4512 if (ret)
4513 break;
fc5a96c3
JL
4514 stp = openlockstateid(s);
4515 ret = nfserr_locks_held;
4516 if (check_for_locks(stp->st_stid.sc_file,
4517 lockowner(stp->st_stateowner)))
4518 break;
4519 unhash_lock_stateid(stp);
1af71cc8 4520 spin_unlock(&cl->cl_lock);
fc5a96c3
JL
4521 nfs4_put_stid(s);
4522 ret = nfs_ok;
1af71cc8 4523 goto out;
3bd64a5b
BF
4524 case NFS4_REVOKED_DELEG_STID:
4525 dp = delegstateid(s);
2d4a532d
JL
4526 list_del_init(&dp->dl_recall_lru);
4527 spin_unlock(&cl->cl_lock);
6011695d 4528 nfs4_put_stid(s);
3bd64a5b 4529 ret = nfs_ok;
1af71cc8
JL
4530 goto out;
4531 /* Default falls through and returns nfserr_bad_stateid */
e1ca12df 4532 }
1af71cc8
JL
4533out_unlock:
4534 spin_unlock(&cl->cl_lock);
e1ca12df
BS
4535out:
4536 nfs4_unlock_state();
4537 return ret;
4538}
4539
4c4cd222
N
4540static inline int
4541setlkflg (int type)
4542{
4543 return (type == NFS4_READW_LT || type == NFS4_READ_LT) ?
4544 RD_STATE : WR_STATE;
4545}
1da177e4 4546
dcef0413 4547static __be32 nfs4_seqid_op_checks(struct nfsd4_compound_state *cstate, stateid_t *stateid, u32 seqid, struct nfs4_ol_stateid *stp)
c0a5d93e
BF
4548{
4549 struct svc_fh *current_fh = &cstate->current_fh;
4550 struct nfs4_stateowner *sop = stp->st_stateowner;
4551 __be32 status;
4552
c0a5d93e
BF
4553 status = nfsd4_check_seqid(cstate, sop, seqid);
4554 if (status)
4555 return status;
3bd64a5b
BF
4556 if (stp->st_stid.sc_type == NFS4_CLOSED_STID
4557 || stp->st_stid.sc_type == NFS4_REVOKED_DELEG_STID)
f7a4d872
BF
4558 /*
4559 * "Closed" stateid's exist *only* to return
3bd64a5b
BF
4560 * nfserr_replay_me from the previous step, and
4561 * revoked delegations are kept only for free_stateid.
f7a4d872
BF
4562 */
4563 return nfserr_bad_stateid;
4564 status = check_stateid_generation(stateid, &stp->st_stid.sc_stateid, nfsd4_has_session(cstate));
4565 if (status)
4566 return status;
4567 return nfs4_check_fh(current_fh, stp);
c0a5d93e
BF
4568}
4569
1da177e4
LT
4570/*
4571 * Checks for sequence id mutating operations.
4572 */
b37ad28b 4573static __be32
dd453dfd 4574nfs4_preprocess_seqid_op(struct nfsd4_compound_state *cstate, u32 seqid,
2288d0e3 4575 stateid_t *stateid, char typemask,
3320fef1
SK
4576 struct nfs4_ol_stateid **stpp,
4577 struct nfsd_net *nn)
1da177e4 4578{
0836f587 4579 __be32 status;
38c2f4b1 4580 struct nfs4_stid *s;
e17f99b7 4581 struct nfs4_ol_stateid *stp = NULL;
1da177e4 4582
8c10cbdb
BH
4583 dprintk("NFSD: %s: seqid=%d stateid = " STATEID_FMT "\n", __func__,
4584 seqid, STATEID_VAL(stateid));
3a4f98bb 4585
1da177e4 4586 *stpp = NULL;
2dd6e458 4587 status = nfsd4_lookup_stateid(cstate, stateid, typemask, &s, nn);
c0a5d93e
BF
4588 if (status)
4589 return status;
e17f99b7 4590 stp = openlockstateid(s);
58fb12e6 4591 nfsd4_cstate_assign_replay(cstate, stp->st_stateowner);
1da177e4 4592
e17f99b7 4593 status = nfs4_seqid_op_checks(cstate, stateid, seqid, stp);
fd911011 4594 if (!status)
e17f99b7 4595 *stpp = stp;
fd911011
TM
4596 else
4597 nfs4_put_stid(&stp->st_stid);
e17f99b7 4598 return status;
c0a5d93e 4599}
39325bd0 4600
3320fef1
SK
4601static __be32 nfs4_preprocess_confirmed_seqid_op(struct nfsd4_compound_state *cstate, u32 seqid,
4602 stateid_t *stateid, struct nfs4_ol_stateid **stpp, struct nfsd_net *nn)
c0a5d93e
BF
4603{
4604 __be32 status;
4605 struct nfs4_openowner *oo;
4cbfc9f7 4606 struct nfs4_ol_stateid *stp;
1da177e4 4607
c0a5d93e 4608 status = nfs4_preprocess_seqid_op(cstate, seqid, stateid,
4cbfc9f7 4609 NFS4_OPEN_STID, &stp, nn);
7a8711c9
BF
4610 if (status)
4611 return status;
4cbfc9f7
TM
4612 oo = openowner(stp->st_stateowner);
4613 if (!(oo->oo_flags & NFS4_OO_CONFIRMED)) {
4614 nfs4_put_stid(&stp->st_stid);
3a4f98bb 4615 return nfserr_bad_stateid;
4cbfc9f7
TM
4616 }
4617 *stpp = stp;
3a4f98bb 4618 return nfs_ok;
1da177e4
LT
4619}
4620
b37ad28b 4621__be32
ca364317 4622nfsd4_open_confirm(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
a4f1706a 4623 struct nfsd4_open_confirm *oc)
1da177e4 4624{
b37ad28b 4625 __be32 status;
fe0750e5 4626 struct nfs4_openowner *oo;
dcef0413 4627 struct nfs4_ol_stateid *stp;
3320fef1 4628 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
1da177e4 4629
a6a9f18f
AV
4630 dprintk("NFSD: nfsd4_open_confirm on file %pd\n",
4631 cstate->current_fh.fh_dentry);
1da177e4 4632
ca364317 4633 status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0);
a8cddc5d
BF
4634 if (status)
4635 return status;
1da177e4
LT
4636
4637 nfs4_lock_state();
4638
9072d5c6 4639 status = nfs4_preprocess_seqid_op(cstate,
ca364317 4640 oc->oc_seqid, &oc->oc_req_stateid,
3320fef1 4641 NFS4_OPEN_STID, &stp, nn);
9072d5c6 4642 if (status)
68b66e82 4643 goto out;
fe0750e5 4644 oo = openowner(stp->st_stateowner);
68b66e82 4645 status = nfserr_bad_stateid;
dad1c067 4646 if (oo->oo_flags & NFS4_OO_CONFIRMED)
2585fc79 4647 goto put_stateid;
dad1c067 4648 oo->oo_flags |= NFS4_OO_CONFIRMED;
dcef0413
BF
4649 update_stateid(&stp->st_stid.sc_stateid);
4650 memcpy(&oc->oc_resp_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
8c10cbdb 4651 dprintk("NFSD: %s: success, seqid=%d stateid=" STATEID_FMT "\n",
dcef0413 4652 __func__, oc->oc_seqid, STATEID_VAL(&stp->st_stid.sc_stateid));
c7b9a459 4653
2a4317c5 4654 nfsd4_client_record_create(oo->oo_owner.so_client);
68b66e82 4655 status = nfs_ok;
2585fc79
TM
4656put_stateid:
4657 nfs4_put_stid(&stp->st_stid);
1da177e4 4658out:
9411b1d4 4659 nfsd4_bump_seqid(cstate, status);
58fb12e6 4660 nfs4_unlock_state();
1da177e4
LT
4661 return status;
4662}
4663
6409a5a6 4664static inline void nfs4_stateid_downgrade_bit(struct nfs4_ol_stateid *stp, u32 access)
1da177e4 4665{
82c5ff1b 4666 if (!test_access(access, stp))
6409a5a6 4667 return;
11b9164a 4668 nfs4_file_put_access(stp->st_stid.sc_file, access);
82c5ff1b 4669 clear_access(access, stp);
6409a5a6 4670}
f197c271 4671
6409a5a6
BF
4672static inline void nfs4_stateid_downgrade(struct nfs4_ol_stateid *stp, u32 to_access)
4673{
4674 switch (to_access) {
4675 case NFS4_SHARE_ACCESS_READ:
4676 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_WRITE);
4677 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_BOTH);
4678 break;
4679 case NFS4_SHARE_ACCESS_WRITE:
4680 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_READ);
4681 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_BOTH);
4682 break;
4683 case NFS4_SHARE_ACCESS_BOTH:
4684 break;
4685 default:
063b0fb9 4686 WARN_ON_ONCE(1);
1da177e4
LT
4687 }
4688}
4689
b37ad28b 4690__be32
ca364317
BF
4691nfsd4_open_downgrade(struct svc_rqst *rqstp,
4692 struct nfsd4_compound_state *cstate,
a4f1706a 4693 struct nfsd4_open_downgrade *od)
1da177e4 4694{
b37ad28b 4695 __be32 status;
dcef0413 4696 struct nfs4_ol_stateid *stp;
3320fef1 4697 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
1da177e4 4698
a6a9f18f
AV
4699 dprintk("NFSD: nfsd4_open_downgrade on file %pd\n",
4700 cstate->current_fh.fh_dentry);
1da177e4 4701
c30e92df 4702 /* We don't yet support WANT bits: */
2c8bd7e0
BH
4703 if (od->od_deleg_want)
4704 dprintk("NFSD: %s: od_deleg_want=0x%x ignored\n", __func__,
4705 od->od_deleg_want);
1da177e4
LT
4706
4707 nfs4_lock_state();
c0a5d93e 4708 status = nfs4_preprocess_confirmed_seqid_op(cstate, od->od_seqid,
3320fef1 4709 &od->od_stateid, &stp, nn);
9072d5c6 4710 if (status)
1da177e4 4711 goto out;
1da177e4 4712 status = nfserr_inval;
82c5ff1b 4713 if (!test_access(od->od_share_access, stp)) {
c11c591f 4714 dprintk("NFSD: access not a subset of current bitmap: 0x%hhx, input access=%08x\n",
1da177e4 4715 stp->st_access_bmap, od->od_share_access);
0667b1e9 4716 goto put_stateid;
1da177e4 4717 }
ce0fc43c 4718 if (!test_deny(od->od_share_deny, stp)) {
c11c591f 4719 dprintk("NFSD: deny not a subset of current bitmap: 0x%hhx, input deny=%08x\n",
1da177e4 4720 stp->st_deny_bmap, od->od_share_deny);
0667b1e9 4721 goto put_stateid;
1da177e4 4722 }
6409a5a6 4723 nfs4_stateid_downgrade(stp, od->od_share_access);
1da177e4 4724
ce0fc43c 4725 reset_union_bmap_deny(od->od_share_deny, stp);
1da177e4 4726
dcef0413
BF
4727 update_stateid(&stp->st_stid.sc_stateid);
4728 memcpy(&od->od_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
1da177e4 4729 status = nfs_ok;
0667b1e9
TM
4730put_stateid:
4731 nfs4_put_stid(&stp->st_stid);
1da177e4 4732out:
9411b1d4 4733 nfsd4_bump_seqid(cstate, status);
58fb12e6 4734 nfs4_unlock_state();
1da177e4
LT
4735 return status;
4736}
4737
f7a4d872
BF
4738static void nfsd4_close_open_stateid(struct nfs4_ol_stateid *s)
4739{
acf9295b 4740 struct nfs4_client *clp = s->st_stid.sc_client;
d83017f9 4741 LIST_HEAD(reaplist);
acf9295b 4742
f7a4d872 4743 s->st_stid.sc_type = NFS4_CLOSED_STID;
2c41beb0 4744 spin_lock(&clp->cl_lock);
d83017f9 4745 unhash_open_stateid(s, &reaplist);
acf9295b 4746
d83017f9
JL
4747 if (clp->cl_minorversion) {
4748 put_ol_stateid_locked(s, &reaplist);
4749 spin_unlock(&clp->cl_lock);
4750 free_ol_stateid_reaplist(&reaplist);
4751 } else {
4752 spin_unlock(&clp->cl_lock);
4753 free_ol_stateid_reaplist(&reaplist);
d3134b10 4754 move_to_close_lru(s, clp->net);
d83017f9 4755 }
38c387b5
BF
4756}
4757
1da177e4
LT
4758/*
4759 * nfs4_unlock_state() called after encode
4760 */
b37ad28b 4761__be32
ca364317 4762nfsd4_close(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
a4f1706a 4763 struct nfsd4_close *close)
1da177e4 4764{
b37ad28b 4765 __be32 status;
dcef0413 4766 struct nfs4_ol_stateid *stp;
3320fef1
SK
4767 struct net *net = SVC_NET(rqstp);
4768 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
1da177e4 4769
a6a9f18f
AV
4770 dprintk("NFSD: nfsd4_close on file %pd\n",
4771 cstate->current_fh.fh_dentry);
1da177e4
LT
4772
4773 nfs4_lock_state();
f7a4d872
BF
4774 status = nfs4_preprocess_seqid_op(cstate, close->cl_seqid,
4775 &close->cl_stateid,
4776 NFS4_OPEN_STID|NFS4_CLOSED_STID,
3320fef1 4777 &stp, nn);
9411b1d4 4778 nfsd4_bump_seqid(cstate, status);
9072d5c6 4779 if (status)
1da177e4 4780 goto out;
dcef0413
BF
4781 update_stateid(&stp->st_stid.sc_stateid);
4782 memcpy(&close->cl_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
1da177e4 4783
f7a4d872 4784 nfsd4_close_open_stateid(stp);
8a0b589d
TM
4785
4786 /* put reference from nfs4_preprocess_seqid_op */
4787 nfs4_put_stid(&stp->st_stid);
1da177e4 4788out:
58fb12e6 4789 nfs4_unlock_state();
1da177e4
LT
4790 return status;
4791}
4792
b37ad28b 4793__be32
ca364317
BF
4794nfsd4_delegreturn(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4795 struct nfsd4_delegreturn *dr)
1da177e4 4796{
203a8c8e
BF
4797 struct nfs4_delegation *dp;
4798 stateid_t *stateid = &dr->dr_stateid;
38c2f4b1 4799 struct nfs4_stid *s;
b37ad28b 4800 __be32 status;
3320fef1 4801 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
1da177e4 4802
ca364317 4803 if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0)))
203a8c8e 4804 return status;
1da177e4
LT
4805
4806 nfs4_lock_state();
2dd6e458 4807 status = nfsd4_lookup_stateid(cstate, stateid, NFS4_DELEG_STID, &s, nn);
38c2f4b1 4808 if (status)
203a8c8e 4809 goto out;
38c2f4b1 4810 dp = delegstateid(s);
d5477a8d 4811 status = check_stateid_generation(stateid, &dp->dl_stid.sc_stateid, nfsd4_has_session(cstate));
203a8c8e 4812 if (status)
fd911011 4813 goto put_stateid;
203a8c8e 4814
3bd64a5b 4815 destroy_delegation(dp);
fd911011
TM
4816put_stateid:
4817 nfs4_put_stid(&dp->dl_stid);
1da177e4 4818out:
203a8c8e
BF
4819 nfs4_unlock_state();
4820
1da177e4
LT
4821 return status;
4822}
4823
4824
1da177e4 4825#define LOFF_OVERFLOW(start, len) ((u64)(len) > ~(u64)(start))
1da177e4 4826
87df4de8
BH
4827static inline u64
4828end_offset(u64 start, u64 len)
4829{
4830 u64 end;
4831
4832 end = start + len;
4833 return end >= start ? end: NFS4_MAX_UINT64;
4834}
4835
4836/* last octet in a range */
4837static inline u64
4838last_byte_offset(u64 start, u64 len)
4839{
4840 u64 end;
4841
063b0fb9 4842 WARN_ON_ONCE(!len);
87df4de8
BH
4843 end = start + len;
4844 return end > start ? end - 1: NFS4_MAX_UINT64;
4845}
4846
1da177e4
LT
4847/*
4848 * TODO: Linux file offsets are _signed_ 64-bit quantities, which means that
4849 * we can't properly handle lock requests that go beyond the (2^63 - 1)-th
4850 * byte, because of sign extension problems. Since NFSv4 calls for 64-bit
4851 * locking, this prevents us from being completely protocol-compliant. The
4852 * real solution to this problem is to start using unsigned file offsets in
4853 * the VFS, but this is a very deep change!
4854 */
4855static inline void
4856nfs4_transform_lock_offset(struct file_lock *lock)
4857{
4858 if (lock->fl_start < 0)
4859 lock->fl_start = OFFSET_MAX;
4860 if (lock->fl_end < 0)
4861 lock->fl_end = OFFSET_MAX;
4862}
4863
d5b9026a
N
4864/* Hack!: For now, we're defining this just so we can use a pointer to it
4865 * as a unique cookie to identify our (NFSv4's) posix locks. */
7b021967 4866static const struct lock_manager_operations nfsd_posix_mng_ops = {
d5b9026a 4867};
1da177e4
LT
4868
4869static inline void
4870nfs4_set_lock_denied(struct file_lock *fl, struct nfsd4_lock_denied *deny)
4871{
fe0750e5 4872 struct nfs4_lockowner *lo;
1da177e4 4873
d5b9026a 4874 if (fl->fl_lmops == &nfsd_posix_mng_ops) {
fe0750e5
BF
4875 lo = (struct nfs4_lockowner *) fl->fl_owner;
4876 deny->ld_owner.data = kmemdup(lo->lo_owner.so_owner.data,
4877 lo->lo_owner.so_owner.len, GFP_KERNEL);
7c13f344
BF
4878 if (!deny->ld_owner.data)
4879 /* We just don't care that much */
4880 goto nevermind;
fe0750e5
BF
4881 deny->ld_owner.len = lo->lo_owner.so_owner.len;
4882 deny->ld_clientid = lo->lo_owner.so_client->cl_clientid;
d5b9026a 4883 } else {
7c13f344
BF
4884nevermind:
4885 deny->ld_owner.len = 0;
4886 deny->ld_owner.data = NULL;
d5b9026a
N
4887 deny->ld_clientid.cl_boot = 0;
4888 deny->ld_clientid.cl_id = 0;
1da177e4
LT
4889 }
4890 deny->ld_start = fl->fl_start;
87df4de8
BH
4891 deny->ld_length = NFS4_MAX_UINT64;
4892 if (fl->fl_end != NFS4_MAX_UINT64)
1da177e4
LT
4893 deny->ld_length = fl->fl_end - fl->fl_start + 1;
4894 deny->ld_type = NFS4_READ_LT;
4895 if (fl->fl_type != F_RDLCK)
4896 deny->ld_type = NFS4_WRITE_LT;
4897}
4898
fe0750e5 4899static struct nfs4_lockowner *
c58c6610 4900find_lockowner_str_locked(clientid_t *clid, struct xdr_netobj *owner,
d4f0489f 4901 struct nfs4_client *clp)
1da177e4 4902{
d4f0489f 4903 unsigned int strhashval = ownerstr_hashval(owner);
b3c32bcd 4904 struct nfs4_stateowner *so;
1da177e4 4905
d4f0489f
TM
4906 list_for_each_entry(so, &clp->cl_ownerstr_hashtbl[strhashval],
4907 so_strhash) {
b3c32bcd
TM
4908 if (so->so_is_open_owner)
4909 continue;
d4f0489f 4910 if (!same_owner_str(so, owner))
b3c32bcd 4911 continue;
5db1c03f 4912 atomic_inc(&so->so_count);
b3c32bcd 4913 return lockowner(so);
1da177e4
LT
4914 }
4915 return NULL;
4916}
4917
c58c6610
TM
4918static struct nfs4_lockowner *
4919find_lockowner_str(clientid_t *clid, struct xdr_netobj *owner,
d4f0489f 4920 struct nfs4_client *clp)
c58c6610
TM
4921{
4922 struct nfs4_lockowner *lo;
4923
d4f0489f
TM
4924 spin_lock(&clp->cl_lock);
4925 lo = find_lockowner_str_locked(clid, owner, clp);
4926 spin_unlock(&clp->cl_lock);
c58c6610
TM
4927 return lo;
4928}
4929
8f4b54c5
JL
4930static void nfs4_unhash_lockowner(struct nfs4_stateowner *sop)
4931{
c58c6610 4932 unhash_lockowner_locked(lockowner(sop));
8f4b54c5
JL
4933}
4934
6b180f0b
JL
4935static void nfs4_free_lockowner(struct nfs4_stateowner *sop)
4936{
4937 struct nfs4_lockowner *lo = lockowner(sop);
4938
4939 kmem_cache_free(lockowner_slab, lo);
4940}
4941
4942static const struct nfs4_stateowner_operations lockowner_ops = {
8f4b54c5
JL
4943 .so_unhash = nfs4_unhash_lockowner,
4944 .so_free = nfs4_free_lockowner,
6b180f0b
JL
4945};
4946
1da177e4
LT
4947/*
4948 * Alloc a lock owner structure.
4949 * Called in nfsd4_lock - therefore, OPEN and OPEN_CONFIRM (if needed) has
25985edc 4950 * occurred.
1da177e4 4951 *
16bfdaaf 4952 * strhashval = ownerstr_hashval
1da177e4 4953 */
fe0750e5 4954static struct nfs4_lockowner *
c58c6610
TM
4955alloc_init_lock_stateowner(unsigned int strhashval, struct nfs4_client *clp,
4956 struct nfs4_ol_stateid *open_stp,
4957 struct nfsd4_lock *lock)
4958{
c58c6610 4959 struct nfs4_lockowner *lo, *ret;
1da177e4 4960
fe0750e5
BF
4961 lo = alloc_stateowner(lockowner_slab, &lock->lk_new_owner, clp);
4962 if (!lo)
1da177e4 4963 return NULL;
fe0750e5
BF
4964 INIT_LIST_HEAD(&lo->lo_owner.so_stateids);
4965 lo->lo_owner.so_is_open_owner = 0;
5db1c03f 4966 lo->lo_owner.so_seqid = lock->lk_new_lock_seqid;
6b180f0b 4967 lo->lo_owner.so_ops = &lockowner_ops;
d4f0489f 4968 spin_lock(&clp->cl_lock);
c58c6610 4969 ret = find_lockowner_str_locked(&clp->cl_clientid,
d4f0489f 4970 &lock->lk_new_owner, clp);
c58c6610
TM
4971 if (ret == NULL) {
4972 list_add(&lo->lo_owner.so_strhash,
d4f0489f 4973 &clp->cl_ownerstr_hashtbl[strhashval]);
c58c6610
TM
4974 ret = lo;
4975 } else
4976 nfs4_free_lockowner(&lo->lo_owner);
d4f0489f 4977 spin_unlock(&clp->cl_lock);
fe0750e5 4978 return lo;
1da177e4
LT
4979}
4980
356a95ec
JL
4981static void
4982init_lock_stateid(struct nfs4_ol_stateid *stp, struct nfs4_lockowner *lo,
4983 struct nfs4_file *fp, struct inode *inode,
4984 struct nfs4_ol_stateid *open_stp)
1da177e4 4985{
d3b313a4 4986 struct nfs4_client *clp = lo->lo_owner.so_client;
1da177e4 4987
356a95ec
JL
4988 lockdep_assert_held(&clp->cl_lock);
4989
3d0fabd5 4990 atomic_inc(&stp->st_stid.sc_count);
3abdb607 4991 stp->st_stid.sc_type = NFS4_LOCK_STID;
fe0750e5 4992 stp->st_stateowner = &lo->lo_owner;
e4f1dd7f 4993 atomic_inc(&lo->lo_owner.so_count);
13cd2184 4994 get_nfs4_file(fp);
11b9164a 4995 stp->st_stid.sc_file = fp;
b49e084d 4996 stp->st_stid.sc_free = nfs4_free_lock_stateid;
0997b173 4997 stp->st_access_bmap = 0;
1da177e4 4998 stp->st_deny_bmap = open_stp->st_deny_bmap;
4c4cd222 4999 stp->st_openstp = open_stp;
3c87b9b7 5000 list_add(&stp->st_locks, &open_stp->st_locks);
1c755dc1 5001 list_add(&stp->st_perstateowner, &lo->lo_owner.so_stateids);
1d31a253
TM
5002 spin_lock(&fp->fi_lock);
5003 list_add(&stp->st_perfile, &fp->fi_stateids);
5004 spin_unlock(&fp->fi_lock);
1da177e4
LT
5005}
5006
c53530da
JL
5007static struct nfs4_ol_stateid *
5008find_lock_stateid(struct nfs4_lockowner *lo, struct nfs4_file *fp)
5009{
5010 struct nfs4_ol_stateid *lst;
356a95ec
JL
5011 struct nfs4_client *clp = lo->lo_owner.so_client;
5012
5013 lockdep_assert_held(&clp->cl_lock);
c53530da
JL
5014
5015 list_for_each_entry(lst, &lo->lo_owner.so_stateids, st_perstateowner) {
3d0fabd5
TM
5016 if (lst->st_stid.sc_file == fp) {
5017 atomic_inc(&lst->st_stid.sc_count);
c53530da 5018 return lst;
3d0fabd5 5019 }
c53530da
JL
5020 }
5021 return NULL;
5022}
5023
356a95ec
JL
5024static struct nfs4_ol_stateid *
5025find_or_create_lock_stateid(struct nfs4_lockowner *lo, struct nfs4_file *fi,
5026 struct inode *inode, struct nfs4_ol_stateid *ost,
5027 bool *new)
5028{
5029 struct nfs4_stid *ns = NULL;
5030 struct nfs4_ol_stateid *lst;
5031 struct nfs4_openowner *oo = openowner(ost->st_stateowner);
5032 struct nfs4_client *clp = oo->oo_owner.so_client;
5033
5034 spin_lock(&clp->cl_lock);
5035 lst = find_lock_stateid(lo, fi);
5036 if (lst == NULL) {
5037 spin_unlock(&clp->cl_lock);
5038 ns = nfs4_alloc_stid(clp, stateid_slab);
5039 if (ns == NULL)
5040 return NULL;
5041
5042 spin_lock(&clp->cl_lock);
5043 lst = find_lock_stateid(lo, fi);
5044 if (likely(!lst)) {
5045 lst = openlockstateid(ns);
5046 init_lock_stateid(lst, lo, fi, inode, ost);
5047 ns = NULL;
5048 *new = true;
5049 }
5050 }
5051 spin_unlock(&clp->cl_lock);
5052 if (ns)
5053 nfs4_put_stid(ns);
5054 return lst;
5055}
c53530da 5056
fd39ca9a 5057static int
1da177e4
LT
5058check_lock_length(u64 offset, u64 length)
5059{
87df4de8 5060 return ((length == 0) || ((length != NFS4_MAX_UINT64) &&
1da177e4
LT
5061 LOFF_OVERFLOW(offset, length)));
5062}
5063
dcef0413 5064static void get_lock_access(struct nfs4_ol_stateid *lock_stp, u32 access)
0997b173 5065{
11b9164a 5066 struct nfs4_file *fp = lock_stp->st_stid.sc_file;
0997b173 5067
7214e860
JL
5068 lockdep_assert_held(&fp->fi_lock);
5069
82c5ff1b 5070 if (test_access(access, lock_stp))
0997b173 5071 return;
12659651 5072 __nfs4_file_get_access(fp, access);
82c5ff1b 5073 set_access(access, lock_stp);
0997b173
BF
5074}
5075
356a95ec
JL
5076static __be32
5077lookup_or_create_lock_state(struct nfsd4_compound_state *cstate,
5078 struct nfs4_ol_stateid *ost,
5079 struct nfsd4_lock *lock,
5080 struct nfs4_ol_stateid **lst, bool *new)
64a284d0 5081{
5db1c03f 5082 __be32 status;
11b9164a 5083 struct nfs4_file *fi = ost->st_stid.sc_file;
64a284d0
BF
5084 struct nfs4_openowner *oo = openowner(ost->st_stateowner);
5085 struct nfs4_client *cl = oo->oo_owner.so_client;
f9c00c3a 5086 struct inode *inode = cstate->current_fh.fh_dentry->d_inode;
64a284d0
BF
5087 struct nfs4_lockowner *lo;
5088 unsigned int strhashval;
5089
d4f0489f 5090 lo = find_lockowner_str(&cl->cl_clientid, &lock->v.new.owner, cl);
c53530da 5091 if (!lo) {
d4f0489f 5092 strhashval = ownerstr_hashval(&lock->v.new.owner);
c53530da
JL
5093 lo = alloc_init_lock_stateowner(strhashval, cl, ost, lock);
5094 if (lo == NULL)
5095 return nfserr_jukebox;
5096 } else {
5097 /* with an existing lockowner, seqids must be the same */
5db1c03f 5098 status = nfserr_bad_seqid;
c53530da
JL
5099 if (!cstate->minorversion &&
5100 lock->lk_new_lock_seqid != lo->lo_owner.so_seqid)
5db1c03f 5101 goto out;
64a284d0 5102 }
c53530da 5103
356a95ec 5104 *lst = find_or_create_lock_stateid(lo, fi, inode, ost, new);
64a284d0 5105 if (*lst == NULL) {
5db1c03f
JL
5106 status = nfserr_jukebox;
5107 goto out;
64a284d0 5108 }
5db1c03f
JL
5109 status = nfs_ok;
5110out:
5111 nfs4_put_stateowner(&lo->lo_owner);
5112 return status;
64a284d0
BF
5113}
5114
1da177e4
LT
5115/*
5116 * LOCK operation
5117 */
b37ad28b 5118__be32
ca364317 5119nfsd4_lock(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
a4f1706a 5120 struct nfsd4_lock *lock)
1da177e4 5121{
fe0750e5
BF
5122 struct nfs4_openowner *open_sop = NULL;
5123 struct nfs4_lockowner *lock_sop = NULL;
3d0fabd5 5124 struct nfs4_ol_stateid *lock_stp = NULL;
0667b1e9 5125 struct nfs4_ol_stateid *open_stp = NULL;
7214e860 5126 struct nfs4_file *fp;
7d947842 5127 struct file *filp = NULL;
21179d81
JL
5128 struct file_lock *file_lock = NULL;
5129 struct file_lock *conflock = NULL;
b37ad28b 5130 __be32 status = 0;
b34f27aa 5131 int lkflg;
b8dd7b9a 5132 int err;
5db1c03f 5133 bool new = false;
3320fef1
SK
5134 struct net *net = SVC_NET(rqstp);
5135 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
1da177e4
LT
5136
5137 dprintk("NFSD: nfsd4_lock: start=%Ld length=%Ld\n",
5138 (long long) lock->lk_offset,
5139 (long long) lock->lk_length);
5140
1da177e4
LT
5141 if (check_lock_length(lock->lk_offset, lock->lk_length))
5142 return nfserr_inval;
5143
ca364317 5144 if ((status = fh_verify(rqstp, &cstate->current_fh,
8837abca 5145 S_IFREG, NFSD_MAY_LOCK))) {
a6f6ef2f
AA
5146 dprintk("NFSD: nfsd4_lock: permission denied!\n");
5147 return status;
5148 }
5149
1da177e4
LT
5150 nfs4_lock_state();
5151
5152 if (lock->lk_is_new) {
684e5638
BF
5153 if (nfsd4_has_session(cstate))
5154 /* See rfc 5661 18.10.3: given clientid is ignored: */
5155 memcpy(&lock->v.new.clientid,
5156 &cstate->session->se_client->cl_clientid,
5157 sizeof(clientid_t));
5158
1da177e4 5159 status = nfserr_stale_clientid;
2c142baa 5160 if (STALE_CLIENTID(&lock->lk_new_clientid, nn))
1da177e4 5161 goto out;
1da177e4 5162
1da177e4 5163 /* validate and update open stateid and open seqid */
c0a5d93e 5164 status = nfs4_preprocess_confirmed_seqid_op(cstate,
1da177e4
LT
5165 lock->lk_new_open_seqid,
5166 &lock->lk_new_open_stateid,
3320fef1 5167 &open_stp, nn);
37515177 5168 if (status)
1da177e4 5169 goto out;
fe0750e5 5170 open_sop = openowner(open_stp->st_stateowner);
b34f27aa 5171 status = nfserr_bad_stateid;
684e5638 5172 if (!same_clid(&open_sop->oo_owner.so_client->cl_clientid,
b34f27aa
BF
5173 &lock->v.new.clientid))
5174 goto out;
64a284d0 5175 status = lookup_or_create_lock_state(cstate, open_stp, lock,
5db1c03f 5176 &lock_stp, &new);
3d0fabd5 5177 } else {
dd453dfd 5178 status = nfs4_preprocess_seqid_op(cstate,
fe0750e5
BF
5179 lock->lk_old_lock_seqid,
5180 &lock->lk_old_lock_stateid,
3320fef1 5181 NFS4_LOCK_STID, &lock_stp, nn);
3d0fabd5 5182 }
e1aaa891
BF
5183 if (status)
5184 goto out;
64a284d0 5185 lock_sop = lockowner(lock_stp->st_stateowner);
1da177e4 5186
b34f27aa
BF
5187 lkflg = setlkflg(lock->lk_type);
5188 status = nfs4_check_openmode(lock_stp, lkflg);
5189 if (status)
5190 goto out;
5191
0dd395dc 5192 status = nfserr_grace;
3320fef1 5193 if (locks_in_grace(net) && !lock->lk_reclaim)
0dd395dc
N
5194 goto out;
5195 status = nfserr_no_grace;
3320fef1 5196 if (!locks_in_grace(net) && lock->lk_reclaim)
0dd395dc
N
5197 goto out;
5198
21179d81
JL
5199 file_lock = locks_alloc_lock();
5200 if (!file_lock) {
5201 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
5202 status = nfserr_jukebox;
5203 goto out;
5204 }
5205
11b9164a 5206 fp = lock_stp->st_stid.sc_file;
21179d81 5207 locks_init_lock(file_lock);
1da177e4
LT
5208 switch (lock->lk_type) {
5209 case NFS4_READ_LT:
5210 case NFS4_READW_LT:
7214e860
JL
5211 spin_lock(&fp->fi_lock);
5212 filp = find_readable_file_locked(fp);
0997b173
BF
5213 if (filp)
5214 get_lock_access(lock_stp, NFS4_SHARE_ACCESS_READ);
7214e860 5215 spin_unlock(&fp->fi_lock);
21179d81 5216 file_lock->fl_type = F_RDLCK;
529d7b2a 5217 break;
1da177e4
LT
5218 case NFS4_WRITE_LT:
5219 case NFS4_WRITEW_LT:
7214e860
JL
5220 spin_lock(&fp->fi_lock);
5221 filp = find_writeable_file_locked(fp);
0997b173
BF
5222 if (filp)
5223 get_lock_access(lock_stp, NFS4_SHARE_ACCESS_WRITE);
7214e860 5224 spin_unlock(&fp->fi_lock);
21179d81 5225 file_lock->fl_type = F_WRLCK;
529d7b2a 5226 break;
1da177e4
LT
5227 default:
5228 status = nfserr_inval;
5229 goto out;
5230 }
f9d7562f
BF
5231 if (!filp) {
5232 status = nfserr_openmode;
5233 goto out;
5234 }
21179d81
JL
5235 file_lock->fl_owner = (fl_owner_t)lock_sop;
5236 file_lock->fl_pid = current->tgid;
5237 file_lock->fl_file = filp;
5238 file_lock->fl_flags = FL_POSIX;
5239 file_lock->fl_lmops = &nfsd_posix_mng_ops;
5240 file_lock->fl_start = lock->lk_offset;
5241 file_lock->fl_end = last_byte_offset(lock->lk_offset, lock->lk_length);
5242 nfs4_transform_lock_offset(file_lock);
5243
5244 conflock = locks_alloc_lock();
5245 if (!conflock) {
5246 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
5247 status = nfserr_jukebox;
5248 goto out;
5249 }
1da177e4 5250
21179d81 5251 err = vfs_lock_file(filp, F_SETLK, file_lock, conflock);
b8dd7b9a 5252 switch (-err) {
1da177e4 5253 case 0: /* success! */
dcef0413
BF
5254 update_stateid(&lock_stp->st_stid.sc_stateid);
5255 memcpy(&lock->lk_resp_stateid, &lock_stp->st_stid.sc_stateid,
1da177e4 5256 sizeof(stateid_t));
b8dd7b9a 5257 status = 0;
eb76b3fd
AA
5258 break;
5259 case (EAGAIN): /* conflock holds conflicting lock */
5260 status = nfserr_denied;
5261 dprintk("NFSD: nfsd4_lock: conflicting lock found!\n");
21179d81 5262 nfs4_set_lock_denied(conflock, &lock->lk_denied);
eb76b3fd 5263 break;
1da177e4
LT
5264 case (EDEADLK):
5265 status = nfserr_deadlock;
eb76b3fd 5266 break;
3e772463 5267 default:
fd85b817 5268 dprintk("NFSD: nfsd4_lock: vfs_lock_file() failed! status %d\n",err);
3e772463 5269 status = nfserrno(err);
eb76b3fd 5270 break;
1da177e4 5271 }
1da177e4 5272out:
de18643d
TM
5273 if (filp)
5274 fput(filp);
5db1c03f
JL
5275 if (lock_stp) {
5276 /* Bump seqid manually if the 4.0 replay owner is openowner */
5277 if (cstate->replay_owner &&
5278 cstate->replay_owner != &lock_sop->lo_owner &&
5279 seqid_mutating_err(ntohl(status)))
5280 lock_sop->lo_owner.so_seqid++;
5281
5282 /*
5283 * If this is a new, never-before-used stateid, and we are
5284 * returning an error, then just go ahead and release it.
5285 */
5286 if (status && new)
5287 release_lock_stateid(lock_stp);
5288
3d0fabd5 5289 nfs4_put_stid(&lock_stp->st_stid);
5db1c03f 5290 }
0667b1e9
TM
5291 if (open_stp)
5292 nfs4_put_stid(&open_stp->st_stid);
9411b1d4 5293 nfsd4_bump_seqid(cstate, status);
58fb12e6 5294 nfs4_unlock_state();
21179d81
JL
5295 if (file_lock)
5296 locks_free_lock(file_lock);
5297 if (conflock)
5298 locks_free_lock(conflock);
1da177e4
LT
5299 return status;
5300}
5301
55ef1274
BF
5302/*
5303 * The NFSv4 spec allows a client to do a LOCKT without holding an OPEN,
5304 * so we do a temporary open here just to get an open file to pass to
5305 * vfs_test_lock. (Arguably perhaps test_lock should be done with an
5306 * inode operation.)
5307 */
04da6e9d 5308static __be32 nfsd_test_lock(struct svc_rqst *rqstp, struct svc_fh *fhp, struct file_lock *lock)
55ef1274
BF
5309{
5310 struct file *file;
04da6e9d
AV
5311 __be32 err = nfsd_open(rqstp, fhp, S_IFREG, NFSD_MAY_READ, &file);
5312 if (!err) {
5313 err = nfserrno(vfs_test_lock(file, lock));
5314 nfsd_close(file);
5315 }
55ef1274
BF
5316 return err;
5317}
5318
1da177e4
LT
5319/*
5320 * LOCKT operation
5321 */
b37ad28b 5322__be32
ca364317
BF
5323nfsd4_lockt(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
5324 struct nfsd4_lockt *lockt)
1da177e4 5325{
21179d81 5326 struct file_lock *file_lock = NULL;
5db1c03f 5327 struct nfs4_lockowner *lo = NULL;
b37ad28b 5328 __be32 status;
7f2210fa 5329 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
1da177e4 5330
5ccb0066 5331 if (locks_in_grace(SVC_NET(rqstp)))
1da177e4
LT
5332 return nfserr_grace;
5333
5334 if (check_lock_length(lockt->lt_offset, lockt->lt_length))
5335 return nfserr_inval;
5336
1da177e4
LT
5337 nfs4_lock_state();
5338
9b2ef62b 5339 if (!nfsd4_has_session(cstate)) {
4b24ca7d 5340 status = lookup_clientid(&lockt->lt_clientid, cstate, nn);
9b2ef62b
BF
5341 if (status)
5342 goto out;
5343 }
1da177e4 5344
75c096f7 5345 if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0)))
1da177e4 5346 goto out;
1da177e4 5347
21179d81
JL
5348 file_lock = locks_alloc_lock();
5349 if (!file_lock) {
5350 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
5351 status = nfserr_jukebox;
5352 goto out;
5353 }
5354 locks_init_lock(file_lock);
1da177e4
LT
5355 switch (lockt->lt_type) {
5356 case NFS4_READ_LT:
5357 case NFS4_READW_LT:
21179d81 5358 file_lock->fl_type = F_RDLCK;
1da177e4
LT
5359 break;
5360 case NFS4_WRITE_LT:
5361 case NFS4_WRITEW_LT:
21179d81 5362 file_lock->fl_type = F_WRLCK;
1da177e4
LT
5363 break;
5364 default:
2fdada03 5365 dprintk("NFSD: nfs4_lockt: bad lock type!\n");
1da177e4
LT
5366 status = nfserr_inval;
5367 goto out;
5368 }
5369
d4f0489f
TM
5370 lo = find_lockowner_str(&lockt->lt_clientid, &lockt->lt_owner,
5371 cstate->clp);
fe0750e5 5372 if (lo)
21179d81
JL
5373 file_lock->fl_owner = (fl_owner_t)lo;
5374 file_lock->fl_pid = current->tgid;
5375 file_lock->fl_flags = FL_POSIX;
1da177e4 5376
21179d81
JL
5377 file_lock->fl_start = lockt->lt_offset;
5378 file_lock->fl_end = last_byte_offset(lockt->lt_offset, lockt->lt_length);
1da177e4 5379
21179d81 5380 nfs4_transform_lock_offset(file_lock);
1da177e4 5381
21179d81 5382 status = nfsd_test_lock(rqstp, &cstate->current_fh, file_lock);
04da6e9d 5383 if (status)
fd85b817 5384 goto out;
04da6e9d 5385
21179d81 5386 if (file_lock->fl_type != F_UNLCK) {
1da177e4 5387 status = nfserr_denied;
21179d81 5388 nfs4_set_lock_denied(file_lock, &lockt->lt_denied);
1da177e4
LT
5389 }
5390out:
5db1c03f
JL
5391 if (lo)
5392 nfs4_put_stateowner(&lo->lo_owner);
1da177e4 5393 nfs4_unlock_state();
21179d81
JL
5394 if (file_lock)
5395 locks_free_lock(file_lock);
1da177e4
LT
5396 return status;
5397}
5398
b37ad28b 5399__be32
ca364317 5400nfsd4_locku(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
a4f1706a 5401 struct nfsd4_locku *locku)
1da177e4 5402{
dcef0413 5403 struct nfs4_ol_stateid *stp;
1da177e4 5404 struct file *filp = NULL;
21179d81 5405 struct file_lock *file_lock = NULL;
b37ad28b 5406 __be32 status;
b8dd7b9a 5407 int err;
3320fef1
SK
5408 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
5409
1da177e4
LT
5410 dprintk("NFSD: nfsd4_locku: start=%Ld length=%Ld\n",
5411 (long long) locku->lu_offset,
5412 (long long) locku->lu_length);
5413
5414 if (check_lock_length(locku->lu_offset, locku->lu_length))
5415 return nfserr_inval;
5416
5417 nfs4_lock_state();
5418
9072d5c6 5419 status = nfs4_preprocess_seqid_op(cstate, locku->lu_seqid,
3320fef1
SK
5420 &locku->lu_stateid, NFS4_LOCK_STID,
5421 &stp, nn);
9072d5c6 5422 if (status)
1da177e4 5423 goto out;
11b9164a 5424 filp = find_any_file(stp->st_stid.sc_file);
f9d7562f
BF
5425 if (!filp) {
5426 status = nfserr_lock_range;
858cc573 5427 goto put_stateid;
f9d7562f 5428 }
21179d81
JL
5429 file_lock = locks_alloc_lock();
5430 if (!file_lock) {
5431 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
5432 status = nfserr_jukebox;
de18643d 5433 goto fput;
21179d81
JL
5434 }
5435 locks_init_lock(file_lock);
5436 file_lock->fl_type = F_UNLCK;
0a262ffb 5437 file_lock->fl_owner = (fl_owner_t)lockowner(stp->st_stateowner);
21179d81
JL
5438 file_lock->fl_pid = current->tgid;
5439 file_lock->fl_file = filp;
5440 file_lock->fl_flags = FL_POSIX;
5441 file_lock->fl_lmops = &nfsd_posix_mng_ops;
5442 file_lock->fl_start = locku->lu_offset;
5443
5444 file_lock->fl_end = last_byte_offset(locku->lu_offset,
5445 locku->lu_length);
5446 nfs4_transform_lock_offset(file_lock);
1da177e4 5447
21179d81 5448 err = vfs_lock_file(filp, F_SETLK, file_lock, NULL);
b8dd7b9a 5449 if (err) {
fd85b817 5450 dprintk("NFSD: nfs4_locku: vfs_lock_file failed!\n");
1da177e4
LT
5451 goto out_nfserr;
5452 }
dcef0413
BF
5453 update_stateid(&stp->st_stid.sc_stateid);
5454 memcpy(&locku->lu_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
de18643d
TM
5455fput:
5456 fput(filp);
858cc573
TM
5457put_stateid:
5458 nfs4_put_stid(&stp->st_stid);
1da177e4 5459out:
9411b1d4 5460 nfsd4_bump_seqid(cstate, status);
58fb12e6 5461 nfs4_unlock_state();
21179d81
JL
5462 if (file_lock)
5463 locks_free_lock(file_lock);
1da177e4
LT
5464 return status;
5465
5466out_nfserr:
b8dd7b9a 5467 status = nfserrno(err);
de18643d 5468 goto fput;
1da177e4
LT
5469}
5470
5471/*
5472 * returns
f9c00c3a
JL
5473 * true: locks held by lockowner
5474 * false: no locks held by lockowner
1da177e4 5475 */
f9c00c3a
JL
5476static bool
5477check_for_locks(struct nfs4_file *fp, struct nfs4_lockowner *lowner)
1da177e4
LT
5478{
5479 struct file_lock **flpp;
f9c00c3a
JL
5480 int status = false;
5481 struct file *filp = find_any_file(fp);
5482 struct inode *inode;
5483
5484 if (!filp) {
5485 /* Any valid lock stateid should have some sort of access */
5486 WARN_ON_ONCE(1);
5487 return status;
5488 }
5489
5490 inode = file_inode(filp);
1da177e4 5491
1c8c601a 5492 spin_lock(&inode->i_lock);
1da177e4 5493 for (flpp = &inode->i_flock; *flpp != NULL; flpp = &(*flpp)->fl_next) {
796dadfd 5494 if ((*flpp)->fl_owner == (fl_owner_t)lowner) {
f9c00c3a
JL
5495 status = true;
5496 break;
796dadfd 5497 }
1da177e4 5498 }
1c8c601a 5499 spin_unlock(&inode->i_lock);
f9c00c3a 5500 fput(filp);
1da177e4
LT
5501 return status;
5502}
5503
b37ad28b 5504__be32
b591480b
BF
5505nfsd4_release_lockowner(struct svc_rqst *rqstp,
5506 struct nfsd4_compound_state *cstate,
5507 struct nfsd4_release_lockowner *rlockowner)
1da177e4
LT
5508{
5509 clientid_t *clid = &rlockowner->rl_clientid;
882e9d25
JL
5510 struct nfs4_stateowner *sop;
5511 struct nfs4_lockowner *lo = NULL;
dcef0413 5512 struct nfs4_ol_stateid *stp;
1da177e4 5513 struct xdr_netobj *owner = &rlockowner->rl_owner;
d4f0489f 5514 unsigned int hashval = ownerstr_hashval(owner);
b37ad28b 5515 __be32 status;
7f2210fa 5516 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
c58c6610 5517 struct nfs4_client *clp;
1da177e4
LT
5518
5519 dprintk("nfsd4_release_lockowner clientid: (%08x/%08x):\n",
5520 clid->cl_boot, clid->cl_id);
5521
1da177e4
LT
5522 nfs4_lock_state();
5523
4b24ca7d 5524 status = lookup_clientid(clid, cstate, nn);
9b2ef62b
BF
5525 if (status)
5526 goto out;
5527
d4f0489f 5528 clp = cstate->clp;
fd44907c 5529 /* Find the matching lock stateowner */
d4f0489f 5530 spin_lock(&clp->cl_lock);
882e9d25 5531 list_for_each_entry(sop, &clp->cl_ownerstr_hashtbl[hashval],
d4f0489f 5532 so_strhash) {
fd44907c 5533
882e9d25
JL
5534 if (sop->so_is_open_owner || !same_owner_str(sop, owner))
5535 continue;
fd44907c 5536
882e9d25
JL
5537 /* see if there are still any locks associated with it */
5538 lo = lockowner(sop);
5539 list_for_each_entry(stp, &sop->so_stateids, st_perstateowner) {
5540 if (check_for_locks(stp->st_stid.sc_file, lo)) {
5541 status = nfserr_locks_held;
5542 spin_unlock(&clp->cl_lock);
5543 goto out;
5544 }
5adfd885 5545 }
882e9d25
JL
5546
5547 atomic_inc(&sop->so_count);
5548 break;
1da177e4 5549 }
c58c6610 5550 spin_unlock(&clp->cl_lock);
882e9d25
JL
5551 if (lo)
5552 release_lockowner(lo);
1da177e4
LT
5553out:
5554 nfs4_unlock_state();
5555 return status;
5556}
5557
5558static inline struct nfs4_client_reclaim *
a55370a3 5559alloc_reclaim(void)
1da177e4 5560{
a55370a3 5561 return kmalloc(sizeof(struct nfs4_client_reclaim), GFP_KERNEL);
1da177e4
LT
5562}
5563
0ce0c2b5 5564bool
52e19c09 5565nfs4_has_reclaimed_state(const char *name, struct nfsd_net *nn)
c7b9a459 5566{
0ce0c2b5 5567 struct nfs4_client_reclaim *crp;
c7b9a459 5568
52e19c09 5569 crp = nfsd4_find_reclaim_client(name, nn);
0ce0c2b5 5570 return (crp && crp->cr_clp);
c7b9a459
N
5571}
5572
1da177e4
LT
5573/*
5574 * failure => all reset bets are off, nfserr_no_grace...
5575 */
772a9bbb 5576struct nfs4_client_reclaim *
52e19c09 5577nfs4_client_to_reclaim(const char *name, struct nfsd_net *nn)
1da177e4
LT
5578{
5579 unsigned int strhashval;
772a9bbb 5580 struct nfs4_client_reclaim *crp;
1da177e4 5581
a55370a3
N
5582 dprintk("NFSD nfs4_client_to_reclaim NAME: %.*s\n", HEXDIR_LEN, name);
5583 crp = alloc_reclaim();
772a9bbb
JL
5584 if (crp) {
5585 strhashval = clientstr_hashval(name);
5586 INIT_LIST_HEAD(&crp->cr_strhash);
52e19c09 5587 list_add(&crp->cr_strhash, &nn->reclaim_str_hashtbl[strhashval]);
772a9bbb 5588 memcpy(crp->cr_recdir, name, HEXDIR_LEN);
0ce0c2b5 5589 crp->cr_clp = NULL;
52e19c09 5590 nn->reclaim_str_hashtbl_size++;
772a9bbb
JL
5591 }
5592 return crp;
1da177e4
LT
5593}
5594
ce30e539 5595void
52e19c09 5596nfs4_remove_reclaim_record(struct nfs4_client_reclaim *crp, struct nfsd_net *nn)
ce30e539
JL
5597{
5598 list_del(&crp->cr_strhash);
5599 kfree(crp);
52e19c09 5600 nn->reclaim_str_hashtbl_size--;
ce30e539
JL
5601}
5602
2a4317c5 5603void
52e19c09 5604nfs4_release_reclaim(struct nfsd_net *nn)
1da177e4
LT
5605{
5606 struct nfs4_client_reclaim *crp = NULL;
5607 int i;
5608
1da177e4 5609 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
52e19c09
SK
5610 while (!list_empty(&nn->reclaim_str_hashtbl[i])) {
5611 crp = list_entry(nn->reclaim_str_hashtbl[i].next,
1da177e4 5612 struct nfs4_client_reclaim, cr_strhash);
52e19c09 5613 nfs4_remove_reclaim_record(crp, nn);
1da177e4
LT
5614 }
5615 }
063b0fb9 5616 WARN_ON_ONCE(nn->reclaim_str_hashtbl_size);
1da177e4
LT
5617}
5618
5619/*
5620 * called from OPEN, CLAIM_PREVIOUS with a new clientid. */
2a4317c5 5621struct nfs4_client_reclaim *
52e19c09 5622nfsd4_find_reclaim_client(const char *recdir, struct nfsd_net *nn)
1da177e4
LT
5623{
5624 unsigned int strhashval;
1da177e4
LT
5625 struct nfs4_client_reclaim *crp = NULL;
5626
278c931c 5627 dprintk("NFSD: nfs4_find_reclaim_client for recdir %s\n", recdir);
1da177e4 5628
278c931c 5629 strhashval = clientstr_hashval(recdir);
52e19c09 5630 list_for_each_entry(crp, &nn->reclaim_str_hashtbl[strhashval], cr_strhash) {
278c931c 5631 if (same_name(crp->cr_recdir, recdir)) {
1da177e4
LT
5632 return crp;
5633 }
5634 }
5635 return NULL;
5636}
5637
5638/*
5639* Called from OPEN. Look for clientid in reclaim list.
5640*/
b37ad28b 5641__be32
0fe492db
TM
5642nfs4_check_open_reclaim(clientid_t *clid,
5643 struct nfsd4_compound_state *cstate,
5644 struct nfsd_net *nn)
1da177e4 5645{
0fe492db 5646 __be32 status;
a52d726b
JL
5647
5648 /* find clientid in conf_id_hashtbl */
0fe492db
TM
5649 status = lookup_clientid(clid, cstate, nn);
5650 if (status)
a52d726b
JL
5651 return nfserr_reclaim_bad;
5652
0fe492db
TM
5653 if (nfsd4_client_record_check(cstate->clp))
5654 return nfserr_reclaim_bad;
5655
5656 return nfs_ok;
1da177e4
LT
5657}
5658
65178db4
BS
5659#ifdef CONFIG_NFSD_FAULT_INJECTION
5660
44e34da6
BS
5661u64 nfsd_forget_client(struct nfs4_client *clp, u64 max)
5662{
d20c11d8
JL
5663 __be32 ret;
5664 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
5665
5666 spin_lock(&nn->client_lock);
5667 ret = mark_client_expired_locked(clp);
5668 spin_unlock(&nn->client_lock);
5669 if (ret != nfs_ok)
221a6876 5670 return 0;
44e34da6
BS
5671 expire_client(clp);
5672 return 1;
5673}
5674
184c1847
BS
5675u64 nfsd_print_client(struct nfs4_client *clp, u64 num)
5676{
5677 char buf[INET6_ADDRSTRLEN];
0a5c33e2 5678 rpc_ntop((struct sockaddr *)&clp->cl_addr, buf, sizeof(buf));
184c1847
BS
5679 printk(KERN_INFO "NFS Client: %s\n", buf);
5680 return 1;
5681}
5682
5683static void nfsd_print_count(struct nfs4_client *clp, unsigned int count,
5684 const char *type)
5685{
5686 char buf[INET6_ADDRSTRLEN];
0a5c33e2 5687 rpc_ntop((struct sockaddr *)&clp->cl_addr, buf, sizeof(buf));
184c1847
BS
5688 printk(KERN_INFO "NFS Client: %s has %u %s\n", buf, count, type);
5689}
5690
3c87b9b7
TM
5691static u64 nfsd_foreach_client_lock(struct nfs4_client *clp, u64 max,
5692 void (*func)(struct nfs4_ol_stateid *))
fc29171f
BS
5693{
5694 struct nfs4_openowner *oop;
fc29171f 5695 struct nfs4_ol_stateid *stp, *st_next;
3c87b9b7 5696 struct nfs4_ol_stateid *lst, *lst_next;
fc29171f
BS
5697 u64 count = 0;
5698
5699 list_for_each_entry(oop, &clp->cl_openowners, oo_perclient) {
3c87b9b7
TM
5700 list_for_each_entry_safe(stp, st_next,
5701 &oop->oo_owner.so_stateids, st_perstateowner) {
5702 list_for_each_entry_safe(lst, lst_next,
5703 &stp->st_locks, st_locks) {
fc29171f 5704 if (func)
3c87b9b7 5705 func(lst);
fc29171f
BS
5706 if (++count == max)
5707 return count;
5708 }
5709 }
5710 }
5711
5712 return count;
5713}
5714
5715u64 nfsd_forget_client_locks(struct nfs4_client *clp, u64 max)
5716{
3c87b9b7 5717 return nfsd_foreach_client_lock(clp, max, release_lock_stateid);
fc29171f
BS
5718}
5719
184c1847
BS
5720u64 nfsd_print_client_locks(struct nfs4_client *clp, u64 max)
5721{
5722 u64 count = nfsd_foreach_client_lock(clp, max, NULL);
5723 nfsd_print_count(clp, count, "locked files");
5724 return count;
5725}
5726
4dbdbda8
BS
5727static u64 nfsd_foreach_client_open(struct nfs4_client *clp, u64 max, void (*func)(struct nfs4_openowner *))
5728{
5729 struct nfs4_openowner *oop, *next;
5730 u64 count = 0;
5731
5732 list_for_each_entry_safe(oop, next, &clp->cl_openowners, oo_perclient) {
5733 if (func)
5734 func(oop);
5735 if (++count == max)
5736 break;
5737 }
5738
5739 return count;
5740}
5741
5742u64 nfsd_forget_client_openowners(struct nfs4_client *clp, u64 max)
5743{
5744 return nfsd_foreach_client_open(clp, max, release_openowner);
5745}
5746
184c1847
BS
5747u64 nfsd_print_client_openowners(struct nfs4_client *clp, u64 max)
5748{
5749 u64 count = nfsd_foreach_client_open(clp, max, NULL);
5750 nfsd_print_count(clp, count, "open files");
5751 return count;
5752}
5753
269de30f
BS
5754static u64 nfsd_find_all_delegations(struct nfs4_client *clp, u64 max,
5755 struct list_head *victims)
5756{
5757 struct nfs4_delegation *dp, *next;
5758 u64 count = 0;
5759
cdc97505 5760 lockdep_assert_held(&state_lock);
269de30f 5761 list_for_each_entry_safe(dp, next, &clp->cl_delegations, dl_perclnt) {
dff1399f
JL
5762 if (victims) {
5763 /*
5764 * It's not safe to mess with delegations that have a
5765 * non-zero dl_time. They might have already been broken
5766 * and could be processed by the laundromat outside of
5767 * the state_lock. Just leave them be.
5768 */
5769 if (dp->dl_time != 0)
5770 continue;
5771
42690676
JL
5772 unhash_delegation_locked(dp);
5773 list_add(&dp->dl_recall_lru, victims);
dff1399f 5774 }
269de30f
BS
5775 if (++count == max)
5776 break;
5777 }
5778 return count;
5779}
5780
5781u64 nfsd_forget_client_delegations(struct nfs4_client *clp, u64 max)
5782{
5783 struct nfs4_delegation *dp, *next;
5784 LIST_HEAD(victims);
5785 u64 count;
5786
cdc97505 5787 spin_lock(&state_lock);
269de30f 5788 count = nfsd_find_all_delegations(clp, max, &victims);
cdc97505 5789 spin_unlock(&state_lock);
269de30f 5790
2d4a532d
JL
5791 list_for_each_entry_safe(dp, next, &victims, dl_recall_lru) {
5792 list_del_init(&dp->dl_recall_lru);
3bd64a5b 5793 revoke_delegation(dp);
2d4a532d 5794 }
269de30f
BS
5795
5796 return count;
5797}
5798
5799u64 nfsd_recall_client_delegations(struct nfs4_client *clp, u64 max)
5800{
dff1399f 5801 struct nfs4_delegation *dp;
269de30f
BS
5802 LIST_HEAD(victims);
5803 u64 count;
5804
cdc97505 5805 spin_lock(&state_lock);
269de30f 5806 count = nfsd_find_all_delegations(clp, max, &victims);
dff1399f
JL
5807 while (!list_empty(&victims)) {
5808 dp = list_first_entry(&victims, struct nfs4_delegation,
5809 dl_recall_lru);
5810 list_del_init(&dp->dl_recall_lru);
5811 dp->dl_time = 0;
269de30f 5812 nfsd_break_one_deleg(dp);
dff1399f 5813 }
cdc97505 5814 spin_unlock(&state_lock);
269de30f
BS
5815
5816 return count;
5817}
5818
184c1847
BS
5819u64 nfsd_print_client_delegations(struct nfs4_client *clp, u64 max)
5820{
5821 u64 count = 0;
5822
cdc97505 5823 spin_lock(&state_lock);
184c1847 5824 count = nfsd_find_all_delegations(clp, max, NULL);
cdc97505 5825 spin_unlock(&state_lock);
184c1847
BS
5826
5827 nfsd_print_count(clp, count, "delegations");
5828 return count;
5829}
5830
44e34da6 5831u64 nfsd_for_n_state(u64 max, u64 (*func)(struct nfs4_client *, u64))
65178db4
BS
5832{
5833 struct nfs4_client *clp, *next;
44e34da6 5834 u64 count = 0;
3320fef1 5835 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns, nfsd_net_id);
65178db4 5836
44e34da6
BS
5837 if (!nfsd_netns_ready(nn))
5838 return 0;
5839
5ed58bb2 5840 list_for_each_entry_safe(clp, next, &nn->client_lru, cl_lru) {
44e34da6
BS
5841 count += func(clp, max - count);
5842 if ((max != 0) && (count >= max))
65178db4
BS
5843 break;
5844 }
65178db4 5845
44e34da6
BS
5846 return count;
5847}
5848
6c1e82a4
BS
5849struct nfs4_client *nfsd_find_client(struct sockaddr_storage *addr, size_t addr_size)
5850{
5851 struct nfs4_client *clp;
5852 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns, nfsd_net_id);
5853
5854 if (!nfsd_netns_ready(nn))
5855 return NULL;
5856
5857 list_for_each_entry(clp, &nn->client_lru, cl_lru) {
5858 if (memcmp(&clp->cl_addr, addr, addr_size) == 0)
5859 return clp;
5860 }
5861 return NULL;
5862}
5863
65178db4
BS
5864#endif /* CONFIG_NFSD_FAULT_INJECTION */
5865
c2f1a551
MS
5866/*
5867 * Since the lifetime of a delegation isn't limited to that of an open, a
5868 * client may quite reasonably hang on to a delegation as long as it has
5869 * the inode cached. This becomes an obvious problem the first time a
5870 * client's inode cache approaches the size of the server's total memory.
5871 *
5872 * For now we avoid this problem by imposing a hard limit on the number
5873 * of delegations, which varies according to the server's memory size.
5874 */
5875static void
5876set_max_delegations(void)
5877{
5878 /*
5879 * Allow at most 4 delegations per megabyte of RAM. Quick
5880 * estimates suggest that in the worst case (where every delegation
5881 * is for a different inode), a delegation could take about 1.5K,
5882 * giving a worst case usage of about 6% of memory.
5883 */
5884 max_delegations = nr_free_buffer_pages() >> (20 - 2 - PAGE_SHIFT);
5885}
5886
d85ed443 5887static int nfs4_state_create_net(struct net *net)
8daae4dc
SK
5888{
5889 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
5890 int i;
5891
5892 nn->conf_id_hashtbl = kmalloc(sizeof(struct list_head) *
5893 CLIENT_HASH_SIZE, GFP_KERNEL);
5894 if (!nn->conf_id_hashtbl)
382a62e7 5895 goto err;
0a7ec377
SK
5896 nn->unconf_id_hashtbl = kmalloc(sizeof(struct list_head) *
5897 CLIENT_HASH_SIZE, GFP_KERNEL);
5898 if (!nn->unconf_id_hashtbl)
5899 goto err_unconf_id;
1872de0e
SK
5900 nn->sessionid_hashtbl = kmalloc(sizeof(struct list_head) *
5901 SESSION_HASH_SIZE, GFP_KERNEL);
5902 if (!nn->sessionid_hashtbl)
5903 goto err_sessionid;
8daae4dc 5904
382a62e7 5905 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
8daae4dc 5906 INIT_LIST_HEAD(&nn->conf_id_hashtbl[i]);
0a7ec377 5907 INIT_LIST_HEAD(&nn->unconf_id_hashtbl[i]);
382a62e7 5908 }
1872de0e
SK
5909 for (i = 0; i < SESSION_HASH_SIZE; i++)
5910 INIT_LIST_HEAD(&nn->sessionid_hashtbl[i]);
382a62e7 5911 nn->conf_name_tree = RB_ROOT;
a99454aa 5912 nn->unconf_name_tree = RB_ROOT;
5ed58bb2 5913 INIT_LIST_HEAD(&nn->client_lru);
73758fed 5914 INIT_LIST_HEAD(&nn->close_lru);
e8c69d17 5915 INIT_LIST_HEAD(&nn->del_recall_lru);
c9a49628 5916 spin_lock_init(&nn->client_lock);
8daae4dc 5917
09121281 5918 INIT_DELAYED_WORK(&nn->laundromat_work, laundromat_main);
d85ed443 5919 get_net(net);
09121281 5920
8daae4dc 5921 return 0;
382a62e7 5922
1872de0e 5923err_sessionid:
9b531137 5924 kfree(nn->unconf_id_hashtbl);
0a7ec377
SK
5925err_unconf_id:
5926 kfree(nn->conf_id_hashtbl);
382a62e7
SK
5927err:
5928 return -ENOMEM;
8daae4dc
SK
5929}
5930
5931static void
4dce0ac9 5932nfs4_state_destroy_net(struct net *net)
8daae4dc
SK
5933{
5934 int i;
5935 struct nfs4_client *clp = NULL;
5936 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
5937
5938 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
5939 while (!list_empty(&nn->conf_id_hashtbl[i])) {
5940 clp = list_entry(nn->conf_id_hashtbl[i].next, struct nfs4_client, cl_idhash);
5941 destroy_client(clp);
5942 }
5943 }
a99454aa 5944
2b905635
KM
5945 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
5946 while (!list_empty(&nn->unconf_id_hashtbl[i])) {
5947 clp = list_entry(nn->unconf_id_hashtbl[i].next, struct nfs4_client, cl_idhash);
5948 destroy_client(clp);
5949 }
a99454aa
SK
5950 }
5951
1872de0e 5952 kfree(nn->sessionid_hashtbl);
0a7ec377 5953 kfree(nn->unconf_id_hashtbl);
8daae4dc 5954 kfree(nn->conf_id_hashtbl);
4dce0ac9 5955 put_net(net);
8daae4dc
SK
5956}
5957
f252bc68 5958int
d85ed443 5959nfs4_state_start_net(struct net *net)
ac4d8ff2 5960{
5e1533c7 5961 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
b5a1a81e
BF
5962 int ret;
5963
d85ed443 5964 ret = nfs4_state_create_net(net);
8daae4dc
SK
5965 if (ret)
5966 return ret;
5e1533c7 5967 nfsd4_client_tracking_init(net);
2c142baa 5968 nn->boot_time = get_seconds();
5ccb0066 5969 locks_start_grace(net, &nn->nfsd4_manager);
a51c84ed 5970 nn->grace_ended = false;
d85ed443 5971 printk(KERN_INFO "NFSD: starting %ld-second grace period (net %p)\n",
5284b44e
SK
5972 nn->nfsd4_grace, net);
5973 queue_delayed_work(laundry_wq, &nn->laundromat_work, nn->nfsd4_grace * HZ);
d85ed443
SK
5974 return 0;
5975}
5976
5977/* initialization to perform when the nfsd service is started: */
5978
5979int
5980nfs4_state_start(void)
5981{
5982 int ret;
5983
b5a1a81e 5984 ret = set_callback_cred();
d85ed443
SK
5985 if (ret)
5986 return -ENOMEM;
58da282b 5987 laundry_wq = create_singlethread_workqueue("nfsd4");
a6d6b781
JL
5988 if (laundry_wq == NULL) {
5989 ret = -ENOMEM;
5990 goto out_recovery;
5991 }
b5a1a81e
BF
5992 ret = nfsd4_create_callback_queue();
5993 if (ret)
5994 goto out_free_laundry;
09121281 5995
c2f1a551 5996 set_max_delegations();
d85ed443 5997
b5a1a81e 5998 return 0;
d85ed443 5999
b5a1a81e
BF
6000out_free_laundry:
6001 destroy_workqueue(laundry_wq);
a6d6b781 6002out_recovery:
b5a1a81e 6003 return ret;
1da177e4
LT
6004}
6005
f252bc68 6006void
4dce0ac9 6007nfs4_state_shutdown_net(struct net *net)
1da177e4 6008{
1da177e4 6009 struct nfs4_delegation *dp = NULL;
1da177e4 6010 struct list_head *pos, *next, reaplist;
4dce0ac9 6011 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
1da177e4 6012
4dce0ac9
SK
6013 cancel_delayed_work_sync(&nn->laundromat_work);
6014 locks_end_grace(&nn->nfsd4_manager);
ac55fdc4 6015
e50a26dc 6016 nfs4_lock_state();
1da177e4 6017 INIT_LIST_HEAD(&reaplist);
cdc97505 6018 spin_lock(&state_lock);
e8c69d17 6019 list_for_each_safe(pos, next, &nn->del_recall_lru) {
1da177e4 6020 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
42690676
JL
6021 unhash_delegation_locked(dp);
6022 list_add(&dp->dl_recall_lru, &reaplist);
1da177e4 6023 }
cdc97505 6024 spin_unlock(&state_lock);
1da177e4
LT
6025 list_for_each_safe(pos, next, &reaplist) {
6026 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
42690676 6027 list_del_init(&dp->dl_recall_lru);
6011695d 6028 nfs4_put_stid(&dp->dl_stid);
1da177e4
LT
6029 }
6030
3320fef1 6031 nfsd4_client_tracking_exit(net);
4dce0ac9 6032 nfs4_state_destroy_net(net);
e50a26dc 6033 nfs4_unlock_state();
1da177e4
LT
6034}
6035
6036void
6037nfs4_state_shutdown(void)
6038{
5e8d5c29 6039 destroy_workqueue(laundry_wq);
c3935e30 6040 nfsd4_destroy_callback_queue();
1da177e4 6041}
8b70484c
TM
6042
6043static void
6044get_stateid(struct nfsd4_compound_state *cstate, stateid_t *stateid)
6045{
37c593c5
TM
6046 if (HAS_STATE_ID(cstate, CURRENT_STATE_ID_FLAG) && CURRENT_STATEID(stateid))
6047 memcpy(stateid, &cstate->current_stateid, sizeof(stateid_t));
8b70484c
TM
6048}
6049
6050static void
6051put_stateid(struct nfsd4_compound_state *cstate, stateid_t *stateid)
6052{
37c593c5
TM
6053 if (cstate->minorversion) {
6054 memcpy(&cstate->current_stateid, stateid, sizeof(stateid_t));
6055 SET_STATE_ID(cstate, CURRENT_STATE_ID_FLAG);
6056 }
6057}
6058
6059void
6060clear_current_stateid(struct nfsd4_compound_state *cstate)
6061{
6062 CLEAR_STATE_ID(cstate, CURRENT_STATE_ID_FLAG);
8b70484c
TM
6063}
6064
62cd4a59
TM
6065/*
6066 * functions to set current state id
6067 */
9428fe1a
TM
6068void
6069nfsd4_set_opendowngradestateid(struct nfsd4_compound_state *cstate, struct nfsd4_open_downgrade *odp)
6070{
6071 put_stateid(cstate, &odp->od_stateid);
6072}
6073
8b70484c
TM
6074void
6075nfsd4_set_openstateid(struct nfsd4_compound_state *cstate, struct nfsd4_open *open)
6076{
6077 put_stateid(cstate, &open->op_stateid);
6078}
6079
62cd4a59
TM
6080void
6081nfsd4_set_closestateid(struct nfsd4_compound_state *cstate, struct nfsd4_close *close)
6082{
6083 put_stateid(cstate, &close->cl_stateid);
6084}
6085
6086void
6087nfsd4_set_lockstateid(struct nfsd4_compound_state *cstate, struct nfsd4_lock *lock)
6088{
6089 put_stateid(cstate, &lock->lk_resp_stateid);
6090}
6091
6092/*
6093 * functions to consume current state id
6094 */
1e97b519 6095
9428fe1a
TM
6096void
6097nfsd4_get_opendowngradestateid(struct nfsd4_compound_state *cstate, struct nfsd4_open_downgrade *odp)
6098{
6099 get_stateid(cstate, &odp->od_stateid);
6100}
6101
6102void
6103nfsd4_get_delegreturnstateid(struct nfsd4_compound_state *cstate, struct nfsd4_delegreturn *drp)
6104{
6105 get_stateid(cstate, &drp->dr_stateid);
6106}
6107
1e97b519
TM
6108void
6109nfsd4_get_freestateid(struct nfsd4_compound_state *cstate, struct nfsd4_free_stateid *fsp)
6110{
6111 get_stateid(cstate, &fsp->fr_stateid);
6112}
6113
6114void
6115nfsd4_get_setattrstateid(struct nfsd4_compound_state *cstate, struct nfsd4_setattr *setattr)
6116{
6117 get_stateid(cstate, &setattr->sa_stateid);
6118}
6119
8b70484c
TM
6120void
6121nfsd4_get_closestateid(struct nfsd4_compound_state *cstate, struct nfsd4_close *close)
6122{
6123 get_stateid(cstate, &close->cl_stateid);
6124}
6125
6126void
62cd4a59 6127nfsd4_get_lockustateid(struct nfsd4_compound_state *cstate, struct nfsd4_locku *locku)
8b70484c 6128{
62cd4a59 6129 get_stateid(cstate, &locku->lu_stateid);
8b70484c 6130}
30813e27
TM
6131
6132void
6133nfsd4_get_readstateid(struct nfsd4_compound_state *cstate, struct nfsd4_read *read)
6134{
6135 get_stateid(cstate, &read->rd_stateid);
6136}
6137
6138void
6139nfsd4_get_writestateid(struct nfsd4_compound_state *cstate, struct nfsd4_write *write)
6140{
6141 get_stateid(cstate, &write->wr_stateid);
6142}