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