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