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